Hex Artifact Content
Not logged in

Artifact bc27f16f3675657951da9271386877a199b77e4a:

File src/sqlite3.c part of check-in [9f654b9984] - Update SQLite to version 3.6.21 final. by drh on 2009-12-07 20:23:03. Also file src/sqlite3.c part of check-in [1c2d878d12] - Merge with trunk by btheado on 2009-12-13 01:16:13.

0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  ** This file is 
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65  of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20   C source files 
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76  from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 32 31 2e 20 20  ersion 3.6.21.  
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c  By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20   the individual 
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74  C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c  o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68  e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61  e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73  n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69   a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69  on.** unit.  Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f  s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70  mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74  timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a   would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65   possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70   files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d    Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66  provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65   5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77   commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f  hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67  mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f  le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79  is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69  ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75  le SQLite.  To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68  se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20  er.** programs, 
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69  you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69  le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69  te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a  le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69  ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  ng interface to 
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20  ry.  (If you do 
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65  not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61   "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64  der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20  , you will find 
0340: 61 20 63 6f 70 79 20 65 6d 62 65 64 64 65 64 20  a copy embedded 
0350: 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 74 65  within.** the te
0360: 78 74 20 6f 66 20 74 68 69 73 20 66 69 6c 65 2e  xt of this file.
0370: 20 20 53 65 61 72 63 68 20 66 6f 72 20 22 42 65    Search for "Be
0380: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33  gin file sqlite3
0390: 2e 68 22 20 74 6f 20 66 69 6e 64 20 74 68 65 20  .h" to find the 
03a0: 73 74 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20  start.** of the 
03b0: 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74 65 33  embedded sqlite3
03c0: 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 2e 29  .h header file.)
03d0: 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65   Additional code
03e0: 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20 6e 65   files may be ne
03f0: 65 64 65 64 0a 2a 2a 20 69 66 20 79 6f 75 20 77  eded.** if you w
0400: 61 6e 74 20 61 20 77 72 61 70 70 65 72 20 74 6f  ant a wrapper to
0410: 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74   interface SQLit
0420: 65 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69  e with your choi
0430: 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d 69 6e  ce of programmin
0440: 67 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e 20 54  g.** language. T
0450: 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  he code for the 
0460: 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e  "sqlite3" comman
0470: 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69 73 20  d-line shell is 
0480: 61 6c 73 6f 20 69 6e 20 61 0a 2a 2a 20 73 65 70  also in a.** sep
0490: 61 72 61 74 65 20 66 69 6c 65 2e 20 54 68 69 73  arate file. This
04a0: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f   file contains o
04b0: 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65  nly code for the
04c0: 20 63 6f 72 65 20 53 51 4c 69 74 65 20 6c 69 62   core SQLite lib
04d0: 72 61 72 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rary..*/.#define
04e0: 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 0a 23   SQLITE_CORE 1.#
04f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4d  define SQLITE_AM
0500: 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23 69 66  ALGAMATION 1.#if
0510: 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 49 56  ndef SQLITE_PRIV
0520: 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ATE.# define SQL
0530: 49 54 45 5f 50 52 49 56 41 54 45 20 73 74 61 74  ITE_PRIVATE stat
0540: 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  ic.#endif.#ifnde
0550: 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23 20 64  f SQLITE_API.# d
0560: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 50 49  efine SQLITE_API
0570: 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a 2a 2a  .#endif./*******
0580: 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
0590: 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  le sqliteInt.h *
05a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
05c0: 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
05d0: 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
05e0: 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
05f0: 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
0600: 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
0610: 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
0620: 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
0630: 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
0640: 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
0650: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
0660: 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
0670: 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
0680: 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
0690: 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
06a0: 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
06b0: 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
06c0: 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
06d0: 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
06e0: 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
06f0: 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
0700: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0710: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0720: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0730: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
0740: 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74 65 72  * Internal inter
0750: 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f 6e 73  face definitions
0760: 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a   for SQLite..**.
0770: 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49  */.#ifndef _SQLI
0780: 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69 6e 65  TEINT_H_.#define
0790: 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f 0a 0a   _SQLITEINT_H_..
07a0: 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65 66  /*.** These #def
07b0: 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61 62  ines should enab
07c0: 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75 70  le >2GB file sup
07d0: 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20 69 66  port on POSIX if
07e0: 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79 69   the.** underlyi
07f0: 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  ng operating sys
0800: 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74 2e  tem supports it.
0810: 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63 6b    If the OS lack
0820: 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65 20  s.** large file 
0830: 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66 20 74  support, or if t
0840: 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f 77 73  he OS is windows
0850: 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62  , these should b
0860: 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20  e no-ops..**.** 
0870: 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20 20 54  Ticket #2739:  T
0880: 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f  he _LARGEFILE_SO
0890: 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73 74 20  URCE macro must 
08a0: 61 70 70 65 61 72 20 62 65 66 6f 72 65 20 61 6e  appear before an
08b0: 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69 6e 63  y.** system #inc
08c0: 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c 20 74  ludes.  Hence, t
08d0: 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64  his block of cod
08e0: 65 20 6d 75 73 74 20 62 65 20 74 68 65 20 76 65  e must be the ve
08f0: 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f 64 65  ry first.** code
0900: 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65 20 66   in all source f
0910: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67  iles..**.** Larg
0920: 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 63  e file support c
0930: 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64 20 75  an be disabled u
0940: 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c 49 54  sing the -DSQLIT
0950: 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20 73 77  E_DISABLE_LFS sw
0960: 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65 20 63  itch.** on the c
0970: 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e 64 20  ompiler command 
0980: 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73 20 6e  line.  This is n
0990: 65 63 65 73 73 61 72 79 20 69 66 20 79 6f 75 20  ecessary if you 
09a0: 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a  are compiling.**
09b0: 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d 61 63   on a recent mac
09c0: 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20 48 61  hine (ex: Red Ha
09d0: 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20 77  t 7.2) but you w
09e0: 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74 6f  ant your code to
09f0: 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20 6f   work.** on an o
0a00: 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65 78  lder machine (ex
0a10: 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29 2e 20  : Red Hat 6.0). 
0a20: 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20   If you compile 
0a30: 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32 0a 2a  on Red Hat 7.2.*
0a40: 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f  * without this o
0a50: 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e  ption, LFS is en
0a60: 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64  able.  But LFS d
0a70: 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e  oes not exist in
0a80: 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69   the kernel.** i
0a90: 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c 20 73  n Red Hat 6.0, s
0aa0: 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74  o the code won't
0ab0: 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66   work.  Hence, f
0ac0: 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72  or maximum binar
0ad0: 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79  y.** portability
0ae0: 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74   you should omit
0af0: 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69 6d 69   LFS..**.** Simi
0b00: 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f 72 20  lar is true for 
0b10: 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53 20 69  Mac OS X.  LFS i
0b20: 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64  s only supported
0b30: 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39 20 61   on Mac OS X 9 a
0b40: 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 69 66  nd later..*/.#if
0b50: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41  ndef SQLITE_DISA
0b60: 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65  BLE_LFS.# define
0b70: 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20   _LARGE_FILE    
0b80: 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46     1.# ifndef _F
0b90: 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a  ILE_OFFSET_BITS.
0ba0: 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45  #   define _FILE
0bb0: 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a  _OFFSET_BITS 64.
0bc0: 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65  # endif.# define
0bd0: 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52   _LARGEFILE_SOUR
0be0: 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  CE 1.#endif../*.
0bf0: 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 20 63  ** Include the c
0c00: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 65 61  onfiguration hea
0c10: 64 65 72 20 6f 75 74 70 75 74 20 62 79 20 27 63  der output by 'c
0c20: 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77 65 27  onfigure' if we'
0c30: 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  re using the.** 
0c40: 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 20 62  autoconf-based b
0c50: 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 20 5f  uild.*/.#ifdef _
0c60: 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f 4e 46  HAVE_SQLITE_CONF
0c70: 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 22 63  IG_H.#include "c
0c80: 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 66 0a  onfig.h".#endif.
0c90: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
0ca0: 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 4c   Include sqliteL
0cb0: 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 6d 69  imit.h in the mi
0cc0: 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
0cd0: 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  t.h ***********/
0ce0: 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
0cf0: 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69   Begin file sqli
0d00: 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a  teLimit.h ******
0d10: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d20: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
0d30: 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20  ./*.** 2007 May 
0d40: 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
0d50: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
0d60: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
0d70: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
0d80: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
0d90: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
0da0: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
0db0: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
0dc0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
0dd0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
0de0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
0df0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
0e00: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
0e10: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
0e20: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
0e30: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
0e40: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
0e50: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
0e60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0e70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0e80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0e90: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ea0: 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 69 6c  .** .** This fil
0eb0: 65 20 64 65 66 69 6e 65 73 20 76 61 72 69 6f 75  e defines variou
0ec0: 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 61 74  s limits of what
0ed0: 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72 6f 63   SQLite can proc
0ee0: 65 73 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ess..*/../*.** T
0ef0: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
0f00: 68 20 6f 66 20 61 20 54 45 58 54 20 6f 72 20 42  h of a TEXT or B
0f10: 4c 4f 42 20 69 6e 20 62 79 74 65 73 2e 20 20 20  LOB in bytes.   
0f20: 54 68 69 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69 6d  This also.** lim
0f30: 69 74 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20  its the size of 
0f40: 61 20 72 6f 77 20 69 6e 20 61 20 74 61 62 6c 65  a row in a table
0f50: 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a   or index..**.**
0f60: 20 54 68 65 20 68 61 72 64 20 6c 69 6d 69 74 20   The hard limit 
0f70: 69 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 6f  is the ability o
0f80: 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e 65  f a 32-bit signe
0f90: 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 6f 20  d integer.** to 
0fa0: 63 6f 75 6e 74 20 74 68 65 20 73 69 7a 65 3a 20  count the size: 
0fb0: 32 5e 33 31 2d 31 20 6f 72 20 32 31 34 37 34 38  2^31-1 or 214748
0fc0: 33 36 34 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  3647..*/.#ifndef
0fd0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47   SQLITE_MAX_LENG
0fe0: 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  TH.# define SQLI
0ff0: 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 31 30  TE_MAX_LENGTH 10
1000: 30 30 30 30 30 30 30 30 0a 23 65 6e 64 69 66 0a  00000000.#endif.
1010: 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ./*.** This is t
1020: 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1030: 72 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  r of.**.**    * 
1040: 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 61 62  Columns in a tab
1050: 6c 65 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d  le.**    * Colum
1060: 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a  ns in an index.*
1070: 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69  *    * Columns i
1080: 6e 20 61 20 76 69 65 77 0a 2a 2a 20 20 20 20 2a  n a view.**    *
1090: 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 53 45   Terms in the SE
10a0: 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e 20 55  T clause of an U
10b0: 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 0a  PDATE statement.
10c0: 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e  **    * Terms in
10d0: 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
10e0: 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
10f0: 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 65  ement.**    * Te
1100: 72 6d 73 20 69 6e 20 74 68 65 20 47 52 4f 55 50  rms in the GROUP
1110: 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59 20   BY or ORDER BY 
1120: 63 6c 61 75 73 65 73 20 6f 66 20 61 20 53 45 4c  clauses of a SEL
1130: 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
1140: 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20  *    * Terms in 
1150: 74 68 65 20 56 41 4c 55 45 53 20 63 6c 61 75 73  the VALUES claus
1160: 65 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 73  e of an INSERT s
1170: 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 54  tatement.**.** T
1180: 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69  he hard upper li
1190: 6d 69 74 20 68 65 72 65 20 69 73 20 33 32 36 37  mit here is 3267
11a0: 36 2e 20 20 4d 6f 73 74 20 64 61 74 61 62 61 73  6.  Most databas
11b0: 65 20 70 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a 2a  e people will.**
11c0: 20 74 65 6c 6c 20 79 6f 75 20 74 68 61 74 20 69   tell you that i
11d0: 6e 20 61 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c 69  n a well-normali
11e0: 7a 65 64 20 64 61 74 61 62 61 73 65 2c 20 79 6f  zed database, yo
11f0: 75 20 75 73 75 61 6c 6c 79 20 73 68 6f 75 6c 64  u usually should
1200: 0a 2a 2a 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72  .** not have mor
1210: 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e 20 6f  e than a dozen o
1220: 72 20 73 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r so columns in 
1230: 61 6e 79 20 74 61 62 6c 65 2e 20 20 41 6e 64 20  any table.  And 
1240: 69 66 0a 2a 2a 20 74 68 61 74 20 69 73 20 74 68  if.** that is th
1250: 65 20 63 61 73 65 2c 20 74 68 65 72 65 20 69 73  e case, there is
1260: 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 68 61 76   no point in hav
1270: 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 61 20  ing more than a 
1280: 66 65 77 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 6c  few.** dozen val
1290: 75 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 68  ues in any of th
12a0: 65 20 6f 74 68 65 72 20 73 69 74 75 61 74 69 6f  e other situatio
12b0: 6e 73 20 64 65 73 63 72 69 62 65 64 20 61 62 6f  ns described abo
12c0: 76 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ve..*/.#ifndef S
12d0: 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e  QLITE_MAX_COLUMN
12e0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
12f0: 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 30  _MAX_COLUMN 2000
1300: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
1310: 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  he maximum lengt
1320: 68 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 53 51  h of a single SQ
1330: 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 62  L statement in b
1340: 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 75  ytes..**.** It u
1350: 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 63 61  sed to be the ca
1360: 73 65 20 74 68 61 74 20 73 65 74 74 69 6e 67 20  se that setting 
1370: 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 7a 65  this value to ze
1380: 72 6f 20 77 6f 75 6c 64 0a 2a 2a 20 74 75 72 6e  ro would.** turn
1390: 20 74 68 65 20 6c 69 6d 69 74 20 6f 66 66 2e 20   the limit off. 
13a0: 20 54 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67   That is no long
13b0: 65 72 20 74 72 75 65 2e 20 20 49 74 20 69 73 20  er true.  It is 
13c0: 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20  not possible.** 
13d0: 74 6f 20 74 75 72 6e 20 74 68 69 73 20 6c 69 6d  to turn this lim
13e0: 69 74 20 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e 64  it off..*/.#ifnd
13f0: 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  ef SQLITE_MAX_SQ
1400: 4c 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e  L_LENGTH.# defin
1410: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c  e SQLITE_MAX_SQL
1420: 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30 30  _LENGTH 10000000
1430: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1440: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65 70   The maximum dep
1450: 74 68 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73  th of an express
1460: 69 6f 6e 20 74 72 65 65 2e 20 54 68 69 73 20 69  ion tree. This i
1470: 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 0a 2a 2a  s limited to .**
1480: 20 73 6f 6d 65 20 65 78 74 65 6e 74 20 62 79 20   some extent by 
1490: 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
14a0: 45 4e 47 54 48 2e 20 42 75 74 20 73 6f 6d 65 74  ENGTH. But somet
14b0: 69 6d 65 20 79 6f 75 20 6d 69 67 68 74 20 0a 2a  ime you might .*
14c0: 2a 20 77 61 6e 74 20 74 6f 20 70 6c 61 63 65 20  * want to place 
14d0: 6d 6f 72 65 20 73 65 76 65 72 65 20 6c 69 6d 69  more severe limi
14e0: 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 6c 65  ts on the comple
14f0: 78 69 74 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 65  xity of an .** e
1500: 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  xpression..**.**
1510: 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 75 73   A value of 0 us
1520: 65 64 20 74 6f 20 6d 65 61 6e 20 74 68 61 74 20  ed to mean that 
1530: 74 68 65 20 6c 69 6d 69 74 20 77 61 73 20 6e 6f  the limit was no
1540: 74 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 42  t enforced..** B
1550: 75 74 20 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f  ut that is no lo
1560: 6e 67 65 72 20 74 72 75 65 2e 20 20 54 68 65 20  nger true.  The 
1570: 6c 69 6d 69 74 20 69 73 20 6e 6f 77 20 73 74 72  limit is now str
1580: 69 63 74 6c 79 20 65 6e 66 6f 72 63 65 64 0a 2a  ictly enforced.*
1590: 2a 20 61 74 20 61 6c 6c 20 74 69 6d 65 73 2e 0a  * at all times..
15a0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
15b0: 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48  E_MAX_EXPR_DEPTH
15c0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
15d0: 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 20  _MAX_EXPR_DEPTH 
15e0: 31 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  1000.#endif../*.
15f0: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ** The maximum n
1600: 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69  umber of terms i
1610: 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c  n a compound SEL
1620: 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
1630: 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65 72  * The code gener
1640: 61 74 6f 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e  ator for compoun
1650: 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  d SELECT stateme
1660: 6e 74 73 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a 20  nts does one.** 
1670: 6c 65 76 65 6c 20 6f 66 20 72 65 63 75 72 73 69  level of recursi
1680: 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d  on for each term
1690: 2e 20 20 41 20 73 74 61 63 6b 20 6f 76 65 72 66  .  A stack overf
16a0: 6c 6f 77 20 63 61 6e 20 72 65 73 75 6c 74 0a 2a  low can result.*
16b0: 2a 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20  * if the number 
16c0: 6f 66 20 74 65 72 6d 73 20 69 73 20 74 6f 6f 20  of terms is too 
16d0: 6c 61 72 67 65 2e 20 20 49 6e 20 70 72 61 63 74  large.  In pract
16e0: 69 63 65 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a 2a  ice, most SQL.**
16f0: 20 6e 65 76 65 72 20 68 61 73 20 6d 6f 72 65 20   never has more 
1700: 74 68 61 6e 20 33 20 6f 72 20 34 20 74 65 72 6d  than 3 or 4 term
1710: 73 2e 20 20 55 73 65 20 61 20 76 61 6c 75 65 20  s.  Use a value 
1720: 6f 66 20 30 20 74 6f 20 64 69 73 61 62 6c 65 0a  of 0 to disable.
1730: 2a 2a 20 61 6e 79 20 6c 69 6d 69 74 20 6f 6e 20  ** any limit on 
1740: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65  the number of te
1750: 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e  rms in a compoun
1760: 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 66  t SELECT..*/.#if
1770: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1780: 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a  COMPOUND_SELECT.
1790: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
17a0: 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c  MAX_COMPOUND_SEL
17b0: 45 43 54 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a  ECT 500.#endif..
17c0: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  /*.** The maximu
17d0: 6d 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f  m number of opco
17e0: 64 65 73 20 69 6e 20 61 20 56 44 42 45 20 70 72  des in a VDBE pr
17f0: 6f 67 72 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 75  ogram..** Not cu
1800: 72 72 65 6e 74 6c 79 20 65 6e 66 6f 72 63 65 64  rrently enforced
1810: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1820: 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 0a  ITE_MAX_VDBE_OP.
1830: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1840: 4d 41 58 5f 56 44 42 45 5f 4f 50 20 32 35 30 30  MAX_VDBE_OP 2500
1850: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
1860: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1870: 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
1880: 74 6f 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  to an SQL functi
1890: 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  on..*/.#ifndef S
18a0: 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49  QLITE_MAX_FUNCTI
18b0: 4f 4e 5f 41 52 47 0a 23 20 64 65 66 69 6e 65 20  ON_ARG.# define 
18c0: 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54  SQLITE_MAX_FUNCT
18d0: 49 4f 4e 5f 41 52 47 20 31 32 37 0a 23 65 6e 64  ION_ARG 127.#end
18e0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61  if../*.** The ma
18f0: 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1900: 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20  in-memory pages 
1910: 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 6d  to use for the m
1920: 61 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  ain database.** 
1930: 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 74 65  table and for te
1940: 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20  mporary tables. 
1950: 20 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41   The SQLITE_DEFA
1960: 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a 2a  ULT_CACHE_SIZE.*
1970: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1980: 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
1990: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  IZE.# define SQL
19a0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48  ITE_DEFAULT_CACH
19b0: 45 5f 53 49 5a 45 20 20 32 30 30 30 0a 23 65 6e  E_SIZE  2000.#en
19c0: 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
19d0: 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f  TE_DEFAULT_TEMP_
19e0: 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 65 66  CACHE_SIZE.# def
19f0: 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ine SQLITE_DEFAU
1a00: 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49  LT_TEMP_CACHE_SI
1a10: 5a 45 20 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a  ZE  500.#endif..
1a20: 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  /*.** The maximu
1a30: 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 74 74 61  m number of atta
1a40: 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e 20  ched databases. 
1a50: 20 54 68 69 73 20 6d 75 73 74 20 62 65 20 62 65   This must be be
1a60: 74 77 65 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 33  tween 0.** and 3
1a70: 30 2e 20 20 54 68 65 20 75 70 70 65 72 20 62 6f  0.  The upper bo
1a80: 75 6e 64 20 6f 6e 20 33 30 20 69 73 20 62 65 63  und on 30 is bec
1a90: 61 75 73 65 20 61 20 33 32 2d 62 69 74 20 69 6e  ause a 32-bit in
1aa0: 74 65 67 65 72 20 62 69 74 6d 61 70 0a 2a 2a 20  teger bitmap.** 
1ab0: 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c  is used internal
1ac0: 6c 79 20 74 6f 20 74 72 61 63 6b 20 61 74 74 61  ly to track atta
1ad0: 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e 0a  ched databases..
1ae0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1af0: 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 0a 23  E_MAX_ATTACHED.#
1b00: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
1b10: 41 58 5f 41 54 54 41 43 48 45 44 20 31 30 0a 23  AX_ATTACHED 10.#
1b20: 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  endif.../*.** Th
1b30: 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20  e maximum value 
1b40: 6f 66 20 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 61  of a ?nnn wildca
1b50: 72 64 20 74 68 61 74 20 74 68 65 20 70 61 72 73  rd that the pars
1b60: 65 72 20 77 69 6c 6c 20 61 63 63 65 70 74 2e 0a  er will accept..
1b70: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1b80: 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e  E_MAX_VARIABLE_N
1b90: 55 4d 42 45 52 0a 23 20 64 65 66 69 6e 65 20 53  UMBER.# define S
1ba0: 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42  QLITE_MAX_VARIAB
1bb0: 4c 45 5f 4e 55 4d 42 45 52 20 39 39 39 0a 23 65  LE_NUMBER 999.#e
1bc0: 6e 64 69 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75 6d  ndif../* Maximum
1bd0: 20 70 61 67 65 20 73 69 7a 65 2e 20 20 54 68 65   page size.  The
1be0: 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20   upper bound on 
1bf0: 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 33 32  this value is 32
1c00: 37 36 38 2e 20 20 54 68 69 73 20 61 20 6c 69 6d  768.  This a lim
1c10: 69 74 0a 2a 2a 20 69 6d 70 6f 73 65 64 20 62 79  it.** imposed by
1c20: 20 74 68 65 20 6e 65 63 65 73 73 69 74 79 20 6f   the necessity o
1c30: 66 20 73 74 6f 72 69 6e 67 20 74 68 65 20 76 61  f storing the va
1c40: 6c 75 65 20 69 6e 20 61 20 32 2d 62 79 74 65 20  lue in a 2-byte 
1c50: 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
1c60: 0a 2a 2a 20 61 6e 64 20 74 68 65 20 66 61 63 74  .** and the fact
1c70: 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 73   that the page s
1c80: 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f  ize must be a po
1c90: 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20  wer of 2..**.** 
1ca0: 49 66 20 74 68 69 73 20 6c 69 6d 69 74 20 69 73  If this limit is
1cb0: 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 74   changed, then t
1cc0: 68 65 20 63 6f 6d 70 69 6c 65 64 20 6c 69 62 72  he compiled libr
1cd0: 61 72 79 20 69 73 20 74 65 63 68 6e 69 63 61 6c  ary is technical
1ce0: 6c 79 0a 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62  ly.** incompatib
1cf0: 6c 65 20 77 69 74 68 20 61 6e 20 53 51 4c 69 74  le with an SQLit
1d00: 65 20 6c 69 62 72 61 72 79 20 63 6f 6d 70 69 6c  e library compil
1d10: 65 64 20 77 69 74 68 20 61 20 64 69 66 66 65 72  ed with a differ
1d20: 65 6e 74 20 6c 69 6d 69 74 2e 20 49 66 0a 2a 2a  ent limit. If.**
1d30: 20 61 20 70 72 6f 63 65 73 73 20 6f 70 65 72 61   a process opera
1d40: 74 69 6e 67 20 6f 6e 20 61 20 64 61 74 61 62 61  ting on a databa
1d50: 73 65 20 77 69 74 68 20 61 20 70 61 67 65 2d 73  se with a page-s
1d60: 69 7a 65 20 6f 66 20 36 35 35 33 36 20 62 79 74  ize of 65536 byt
1d70: 65 73 20 0a 2a 2a 20 63 72 61 73 68 65 73 2c 20  es .** crashes, 
1d80: 74 68 65 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65  then an instance
1d90: 20 6f 66 20 53 51 4c 69 74 65 20 63 6f 6d 70 69   of SQLite compi
1da0: 6c 65 64 20 77 69 74 68 20 74 68 65 20 64 65 66  led with the def
1db0: 61 75 6c 74 20 70 61 67 65 2d 73 69 7a 65 20 0a  ault page-size .
1dc0: 2a 2a 20 6c 69 6d 69 74 20 77 69 6c 6c 20 6e 6f  ** limit will no
1dd0: 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c  t be able to rol
1de0: 6c 62 61 63 6b 20 74 68 65 20 61 62 6f 72 74 65  lback the aborte
1df0: 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54  d transaction. T
1e00: 68 69 73 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65 61  his could.** lea
1e10: 64 20 74 6f 20 64 61 74 61 62 61 73 65 20 63 6f  d to database co
1e20: 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66  rruption..*/.#if
1e30: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1e40: 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69  PAGE_SIZE.# defi
1e50: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  ne SQLITE_MAX_PA
1e60: 47 45 5f 53 49 5a 45 20 33 32 37 36 38 0a 23 65  GE_SIZE 32768.#e
1e70: 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ndif.../*.** The
1e80: 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66   default size of
1e90: 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65   a database page
1ea0: 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1eb0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  ITE_DEFAULT_PAGE
1ec0: 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53  _SIZE.# define S
1ed0: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
1ee0: 47 45 5f 53 49 5a 45 20 31 30 32 34 0a 23 65 6e  GE_SIZE 1024.#en
1ef0: 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 44  dif.#if SQLITE_D
1f00: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1f10: 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  >SQLITE_MAX_PAGE
1f20: 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51  _SIZE.# undef SQ
1f30: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
1f40: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20  E_SIZE.# define 
1f50: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
1f60: 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54 45 5f  AGE_SIZE SQLITE_
1f70: 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23 65  MAX_PAGE_SIZE.#e
1f80: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 69  ndif../*.** Ordi
1f90: 6e 61 72 69 6c 79 2c 20 69 66 20 6e 6f 20 76 61  narily, if no va
1fa0: 6c 75 65 20 69 73 20 65 78 70 6c 69 63 69 74 6c  lue is explicitl
1fb0: 79 20 70 72 6f 76 69 64 65 64 2c 20 53 51 4c 69  y provided, SQLi
1fc0: 74 65 20 63 72 65 61 74 65 73 20 64 61 74 61 62  te creates datab
1fd0: 61 73 65 73 0a 2a 2a 20 77 69 74 68 20 70 61 67  ases.** with pag
1fe0: 65 20 73 69 7a 65 20 53 51 4c 49 54 45 5f 44 45  e size SQLITE_DE
1ff0: 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2e  FAULT_PAGE_SIZE.
2000: 20 48 6f 77 65 76 65 72 2c 20 62 61 73 65 64 20   However, based 
2010: 6f 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 64 65  on certain.** de
2020: 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73  vice characteris
2030: 74 69 63 73 20 28 73 65 63 74 6f 72 2d 73 69 7a  tics (sector-siz
2040: 65 20 61 6e 64 20 61 74 6f 6d 69 63 20 77 72 69  e and atomic wri
2050: 74 65 28 29 20 73 75 70 70 6f 72 74 29 2c 0a 2a  te() support),.*
2060: 2a 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68 6f  * SQLite may cho
2070: 6f 73 65 20 61 20 6c 61 72 67 65 72 20 76 61 6c  ose a larger val
2080: 75 65 2e 20 54 68 69 73 20 63 6f 6e 73 74 61 6e  ue. This constan
2090: 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  t is the maximum
20a0: 20 76 61 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 65   value.** SQLite
20b0: 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e 20   will choose on 
20c0: 69 74 73 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 6e  its own..*/.#ifn
20d0: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44  def SQLITE_MAX_D
20e0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
20f0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
2100: 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
2110: 45 5f 53 49 5a 45 20 38 31 39 32 0a 23 65 6e 64  E_SIZE 8192.#end
2120: 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41  if.#if SQLITE_MA
2130: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  X_DEFAULT_PAGE_S
2140: 49 5a 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50  IZE>SQLITE_MAX_P
2150: 41 47 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66  AGE_SIZE.# undef
2160: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41   SQLITE_MAX_DEFA
2170: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20  ULT_PAGE_SIZE.# 
2180: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
2190: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  X_DEFAULT_PAGE_S
21a0: 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  IZE SQLITE_MAX_P
21b0: 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a  AGE_SIZE.#endif.
21c0: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20  ../*.** Maximum 
21d0: 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
21e0: 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  in one database 
21f0: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  file..**.** This
2200: 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20   is really just 
2210: 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
2220: 65 20 66 6f 72 20 74 68 65 20 6d 61 78 5f 70 61  e for the max_pa
2230: 67 65 5f 63 6f 75 6e 74 20 70 72 61 67 6d 61 2e  ge_count pragma.
2240: 0a 2a 2a 20 54 68 69 73 20 76 61 6c 75 65 20 63  .** This value c
2250: 61 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 28 6f  an be lowered (o
2260: 72 20 72 61 69 73 65 64 29 20 61 74 20 72 75 6e  r raised) at run
2270: 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68 61 74  -time using that
2280: 20 74 68 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 65   the.** max_page
2290: 5f 63 6f 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a 2f  _count macro..*/
22a0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22b0: 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 23  MAX_PAGE_COUNT.#
22c0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
22d0: 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 20 31 30  AX_PAGE_COUNT 10
22e0: 37 33 37 34 31 38 32 33 0a 23 65 6e 64 69 66 0a  73741823.#endif.
22f0: 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c  ./*.** Maximum l
2300: 65 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73 29  ength (in bytes)
2310: 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20   of the pattern 
2320: 69 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f  in a LIKE or GLO
2330: 42 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  B.** operator..*
2340: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2350: 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52  _MAX_LIKE_PATTER
2360: 4e 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e  N_LENGTH.# defin
2370: 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  e SQLITE_MAX_LIK
2380: 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
2390: 20 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f   50000.#endif../
23a0: 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70  *.** Maximum dep
23b0: 74 68 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20  th of recursion 
23c0: 66 6f 72 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a  for triggers..**
23d0: 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 31  .** A value of 1
23e0: 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 74 72   means that a tr
23f0: 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 77 69  igger program wi
2400: 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74  ll not be able t
2410: 6f 20 69 74 73 65 6c 66 0a 2a 2a 20 66 69 72 65  o itself.** fire
2420: 20 61 6e 79 20 74 72 69 67 67 65 72 73 2e 20 41   any triggers. A
2430: 20 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e   value of 0 mean
2440: 73 20 74 68 61 74 20 6e 6f 20 74 72 69 67 67 65  s that no trigge
2450: 72 20 70 72 6f 67 72 61 6d 73 20 61 74 20 61 6c  r programs at al
2460: 6c 20 0a 2a 2a 20 6d 61 79 20 62 65 20 65 78 65  l .** may be exe
2470: 63 75 74 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  cuted..*/.#ifnde
2480: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49  f SQLITE_MAX_TRI
2490: 47 47 45 52 5f 44 45 50 54 48 0a 23 20 64 65 66  GGER_DEPTH.# def
24a0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54  ine SQLITE_MAX_T
24b0: 52 49 47 47 45 52 5f 44 45 50 54 48 20 31 30 30  RIGGER_DEPTH 100
24c0: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  0.#endif../*****
24d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
24e0: 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a   sqliteLimit.h *
24f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2500: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2510: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2520: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
2530: 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
2540: 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
2550: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
2560: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69  ********/../* Di
2570: 73 61 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77  sable nuisance w
2580: 61 72 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61  arnings on Borla
2590: 6e 64 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a  nd compilers */.
25a0: 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f  #if defined(__BO
25b0: 52 4c 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d  RLANDC__).#pragm
25c0: 61 20 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75  a warn -rch /* u
25d0: 6e 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20  nreachable code 
25e0: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20  */.#pragma warn 
25f0: 2d 63 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f  -ccc /* Conditio
2600: 6e 20 69 73 20 61 6c 77 61 79 73 20 74 72 75 65  n is always true
2610: 20 6f 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72   or false */.#pr
2620: 61 67 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f  agma warn -aus /
2630: 2a 20 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65  * Assigned value
2640: 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a   is never used *
2650: 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d  /.#pragma warn -
2660: 63 73 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67  csu /* Comparing
2670: 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69   signed and unsi
2680: 67 6e 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20  gned */.#pragma 
2690: 77 61 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73  warn -spa /* Sus
26a0: 70 69 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20  picious pointer 
26b0: 61 72 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65  arithmetic */.#e
26c0: 6e 64 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20  ndif../* Needed 
26d0: 66 6f 72 20 76 61 72 69 6f 75 73 20 64 65 66 69  for various defi
26e0: 6e 69 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69  nitions... */.#i
26f0: 66 6e 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43  fndef _GNU_SOURC
2700: 45 0a 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f  E.# define _GNU_
2710: 53 4f 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f  SOURCE.#endif../
2720: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61  *.** Include sta
2730: 6e 64 61 72 64 20 68 65 61 64 65 72 20 66 69 6c  ndard header fil
2740: 65 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a  es as necessary.
2750: 2a 2f 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53  */.#ifdef HAVE_S
2760: 54 44 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65  TDINT_H.#include
2770: 20 3c 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64   <stdint.h>.#end
2780: 69 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49  if.#ifdef HAVE_I
2790: 4e 54 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75  NTTYPES_H.#inclu
27a0: 64 65 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a  de <inttypes.h>.
27b0: 23 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20  #endif..#define 
27c0: 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d  SQLITE_INDEX_SAM
27d0: 50 4c 45 53 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 54  PLES 10../*.** T
27e0: 68 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65  his macro is use
27f0: 64 20 74 6f 20 22 68 69 64 65 22 20 73 6f 6d 65  d to "hide" some
2800: 20 75 67 6c 69 6e 65 73 73 20 69 6e 20 63 61 73   ugliness in cas
2810: 74 69 6e 67 20 61 6e 20 69 6e 74 0a 2a 2a 20 76  ting an int.** v
2820: 61 6c 75 65 20 74 6f 20 61 20 70 74 72 20 76 61  alue to a ptr va
2830: 6c 75 65 20 75 6e 64 65 72 20 74 68 65 20 4d 53  lue under the MS
2840: 56 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c  VC 64-bit compil
2850: 65 72 2e 20 20 20 43 61 73 74 69 6e 67 0a 2a 2a  er.   Casting.**
2860: 20 6e 6f 6e 20 36 34 2d 62 69 74 20 76 61 6c 75   non 64-bit valu
2870: 65 73 20 74 6f 20 70 74 72 20 74 79 70 65 73 20  es to ptr types 
2880: 72 65 73 75 6c 74 73 20 69 6e 20 61 20 22 68 61  results in a "ha
2890: 72 64 22 20 65 72 72 6f 72 20 77 69 74 68 20 0a  rd" error with .
28a0: 2a 2a 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62  ** the MSVC 64-b
28b0: 69 74 20 63 6f 6d 70 69 6c 65 72 20 77 68 69 63  it compiler whic
28c0: 68 20 74 68 69 73 20 61 74 74 65 6d 70 74 73 20  h this attempts 
28d0: 74 6f 20 61 76 6f 69 64 2e 20 20 0a 2a 2a 0a 2a  to avoid.  .**.*
28e0: 2a 20 41 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69  * A simple compi
28f0: 6c 65 72 20 70 72 61 67 6d 61 20 6f 72 20 63 61  ler pragma or ca
2900: 73 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63  sting sequence c
2910: 6f 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e  ould not be foun
2920: 64 0a 2a 2a 20 74 6f 20 63 6f 72 72 65 63 74 20  d.** to correct 
2930: 74 68 69 73 20 69 6e 20 61 6c 6c 20 73 69 74 75  this in all situ
2940: 61 74 69 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20  ations, so this 
2950: 6d 61 63 72 6f 20 77 61 73 20 69 6e 74 72 6f 64  macro was introd
2960: 75 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 63  uced..**.** It c
2970: 6f 75 6c 64 20 62 65 20 61 72 67 75 65 64 20 74  ould be argued t
2980: 68 61 74 20 74 68 65 20 69 6e 74 70 74 72 5f 74  hat the intptr_t
2990: 20 74 79 70 65 20 63 6f 75 6c 64 20 62 65 20 75   type could be u
29a0: 73 65 64 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63  sed in this.** c
29b0: 61 73 65 2c 20 62 75 74 20 74 68 61 74 20 74 79  ase, but that ty
29c0: 70 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  pe is not availa
29d0: 62 6c 65 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69  ble on all compi
29e0: 6c 65 72 73 2c 20 6f 72 20 0a 2a 2a 20 72 65 71  lers, or .** req
29f0: 75 69 72 65 73 20 74 68 65 20 23 69 6e 63 6c 75  uires the #inclu
2a00: 64 65 20 6f 66 20 73 70 65 63 69 66 69 63 20 68  de of specific h
2a10: 65 61 64 65 72 73 20 77 68 69 63 68 20 64 69 66  eaders which dif
2a20: 66 65 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20  fers between.** 
2a30: 70 6c 61 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a  platforms..**.**
2a40: 20 54 69 63 6b 65 74 20 23 33 38 36 30 3a 20 20   Ticket #3860:  
2a50: 54 68 65 20 6c 6c 76 6d 2d 67 63 63 2d 34 2e 32  The llvm-gcc-4.2
2a60: 20 63 6f 6d 70 69 6c 65 72 20 66 72 6f 6d 20 41   compiler from A
2a70: 70 70 6c 65 20 63 68 6f 6b 65 73 20 6f 6e 0a 2a  pple chokes on.*
2a80: 2a 20 74 68 65 20 28 28 76 6f 69 64 2a 29 26 28  * the ((void*)&(
2a90: 28 63 68 61 72 2a 29 30 29 5b 58 5d 29 20 63 6f  (char*)0)[X]) co
2aa0: 6e 73 74 72 75 63 74 2e 20 20 42 75 74 20 4d 53  nstruct.  But MS
2ab0: 56 43 20 63 68 6f 6b 65 73 20 6f 6e 20 28 28 76  VC chokes on ((v
2ac0: 6f 69 64 2a 29 28 58 29 29 2e 0a 2a 2a 20 53 6f  oid*)(X))..** So
2ad0: 20 77 65 20 68 61 76 65 20 74 6f 20 64 65 66 69   we have to defi
2ae0: 6e 65 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e  ne the macros in
2af0: 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20   different ways 
2b00: 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65  depending on the
2b10: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2f  .** compiler..*/
2b20: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47  .#if defined(__G
2b30: 4e 55 43 5f 5f 29 0a 23 20 69 66 20 64 65 66 69  NUC__).# if defi
2b40: 6e 65 64 28 48 41 56 45 5f 53 54 44 49 4e 54 5f  ned(HAVE_STDINT_
2b50: 48 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51  H).#   define SQ
2b60: 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
2b70: 58 29 20 20 28 28 76 6f 69 64 2a 29 28 69 6e 74  X)  ((void*)(int
2b80: 70 74 72 5f 74 29 28 58 29 29 0a 23 20 20 20 64  ptr_t)(X)).#   d
2b90: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52  efine SQLITE_PTR
2ba0: 5f 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e  _TO_INT(X)  ((in
2bb0: 74 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29  t)(intptr_t)(X))
2bc0: 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69  .# else.#   defi
2bd0: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f  ne SQLITE_INT_TO
2be0: 5f 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a  _PTR(X)  ((void*
2bf0: 29 28 58 29 29 0a 23 20 20 20 64 65 66 69 6e 65  )(X)).#   define
2c00: 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
2c10: 4e 54 28 58 29 20 20 28 28 69 6e 74 29 28 58 29  NT(X)  ((int)(X)
2c20: 29 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a  ).# endif.#else.
2c30: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
2c40: 49 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 20  INT_TO_PTR(X)   
2c50: 28 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a  ((void*)&((char*
2c60: 29 30 29 5b 58 5d 29 0a 23 20 64 65 66 69 6e 65  )0)[X]).# define
2c70: 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49   SQLITE_PTR_TO_I
2c80: 4e 54 28 58 29 20 20 20 28 28 69 6e 74 29 28 28  NT(X)   ((int)((
2c90: 28 63 68 61 72 2a 29 58 29 2d 28 63 68 61 72 2a  (char*)X)-(char*
2ca0: 29 30 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  )0)).#endif.../*
2cb0: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54  .** The SQLITE_T
2cc0: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20  HREADSAFE macro 
2cd0: 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20  must be defined 
2ce0: 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31  as either 0 or 1
2cf0: 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69  ..** Older versi
2d00: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73  ons of SQLite us
2d10: 65 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54  ed an optional T
2d20: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e  HREADSAFE macro.
2d30: 0a 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74  .** We support t
2d40: 68 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a  hat for legacy.*
2d50: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
2d60: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
2d70: 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48  ).#if defined(TH
2d80: 52 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69  READSAFE).# defi
2d90: 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ne SQLITE_THREAD
2da0: 53 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a  SAFE THREADSAFE.
2db0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
2dc0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
2dd0: 20 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66   1.#endif.#endif
2de0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ../*.** The SQLI
2df0: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54  TE_DEFAULT_MEMST
2e00: 41 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20  ATUS macro must 
2e10: 62 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69  be defined as ei
2e20: 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20  ther 0 or 1..** 
2e30: 49 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  It determines wh
2e40: 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
2e50: 20 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65   features relate
2e60: 64 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  d to .** SQLITE_
2e70: 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53  CONFIG_MEMSTATUS
2e80: 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 62   are available b
2e90: 79 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74  y default or not
2ea0: 2e 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e  . This value can
2eb0: 0a 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65  .** be overridde
2ec0: 6e 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69  n at runtime usi
2ed0: 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  ng the sqlite3_c
2ee0: 6f 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a  onfig() API..*/.
2ef0: 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
2f00: 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53  ITE_DEFAULT_MEMS
2f10: 54 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20  TATUS).# define 
2f20: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d  SQLITE_DEFAULT_M
2f30: 45 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69  EMSTATUS 1.#endi
2f40: 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79  f../*.** Exactly
2f50: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
2f60: 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73  owing macros mus
2f70: 74 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20  t be defined in 
2f80: 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63  order to.** spec
2f90: 69 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79  ify which memory
2fa0: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
2fb0: 79 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a  ystem to use..**
2fc0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53  .**     SQLITE_S
2fd0: 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20  YSTEM_MALLOC    
2fe0: 20 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72        // Use nor
2ff0: 6d 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f  mal system mallo
3000: 63 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54  c().**     SQLIT
3010: 45 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20  E_MEMDEBUG      
3020: 20 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75           // Debu
3030: 67 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66  gging version of
3040: 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29   system malloc()
3050: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d  .**     SQLITE_M
3060: 45 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20  EMORY_SIZE      
3070: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61        // interna
3080: 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a  l allocator #1.*
3090: 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41  *     SQLITE_MMA
30a0: 50 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20  P_HEAP_SIZE     
30b0: 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20      // internal 
30c0: 6d 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72  mmap() allocator
30d0: 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50  .**     SQLITE_P
30e0: 4f 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20  OW2_MEMORY_SIZE 
30f0: 20 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61        // interna
3100: 6c 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61  l power-of-two a
3110: 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49  llocator.**.** I
3120: 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62  f none of the ab
3130: 6f 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c  ove are defined,
3140: 20 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45   then set SQLITE
3150: 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61  _SYSTEM_MALLOC a
3160: 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  s.** the default
3170: 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
3180: 28 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d  (SQLITE_SYSTEM_M
3190: 41 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53  ALLOC)+defined(S
31a0: 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b  QLITE_MEMDEBUG)+
31b0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
31c0: 4c 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45  LITE_MEMORY_SIZE
31d0: 29 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  )+defined(SQLITE
31e0: 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29  _MMAP_HEAP_SIZE)
31f0: 2b 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53  +\.    defined(S
3200: 51 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52  QLITE_POW2_MEMOR
3210: 59 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f  Y_SIZE)>1.# erro
3220: 72 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f  r "At most one o
3230: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
3240: 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e  compile-time con
3250: 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
3260: 6e 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20  ns\. is allows: 
3270: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
3280: 4c 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d  LLOC, SQLITE_MEM
3290: 44 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45  DEBUG, SQLITE_ME
32a0: 4d 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c  MORY_SIZE,\. SQL
32b0: 49 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49  ITE_MMAP_HEAP_SI
32c0: 5a 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f  ZE, SQLITE_POW2_
32d0: 4d 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e  MEMORY_SIZE".#en
32e0: 64 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28  dif.#if defined(
32f0: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
3300: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51  LLOC)+defined(SQ
3310: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c  LITE_MEMDEBUG)+\
3320: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c  .    defined(SQL
3330: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29  ITE_MEMORY_SIZE)
3340: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  +defined(SQLITE_
3350: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b  MMAP_HEAP_SIZE)+
3360: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
3370: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59  LITE_POW2_MEMORY
3380: 5f 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69  _SIZE)==0.# defi
3390: 6e 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d  ne SQLITE_SYSTEM
33a0: 5f 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66  _MALLOC 1.#endif
33b0: 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ../*.** If SQLIT
33c0: 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49  E_MALLOC_SOFT_LI
33d0: 4d 49 54 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c  MIT is not zero,
33e0: 20 74 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65   then try to kee
33f0: 70 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f  p the.** sizes o
3400: 66 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  f memory allocat
3410: 69 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20  ions below this 
3420: 76 61 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73  value where poss
3430: 69 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  ible..*/.#if !de
3440: 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c  fined(SQLITE_MAL
3450: 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a  LOC_SOFT_LIMIT).
3460: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
3470: 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49  MALLOC_SOFT_LIMI
3480: 54 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f  T 1024.#endif../
3490: 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20  *.** We need to 
34a0: 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f  define _XOPEN_SO
34b0: 55 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20  URCE as follows 
34c0: 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62  in order to enab
34d0: 6c 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 20  le.** recursive 
34e0: 6d 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20  mutexes on most 
34f0: 55 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 42  Unix systems.  B
3500: 75 74 20 4d 61 63 20 4f 53 20 58 20 69 73 20 64  ut Mac OS X is d
3510: 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65  ifferent..** The
3520: 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64   _XOPEN_SOURCE d
3530: 65 66 69 6e 65 20 63 61 75 73 65 73 20 70 72 6f  efine causes pro
3540: 62 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53  blems for Mac OS
3550: 20 58 20 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a   X we are told,.
3560: 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d 69 74  ** so it is omit
3570: 74 65 64 20 74 68 65 72 65 2e 20 20 53 65 65 20  ted there.  See 
3580: 74 69 63 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a  ticket #2673..**
3590: 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c 65 61  .** Later we lea
35a0: 72 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53  rn that _XOPEN_S
35b0: 4f 55 52 43 45 20 69 73 20 70 6f 6f 72 6c 79 20  OURCE is poorly 
35c0: 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a  or incorrectly.*
35d0: 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e  * implemented on
35e0: 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 20 20   some systems.  
35f0: 53 6f 20 77 65 20 61 76 6f 69 64 20 64 65 66 69  So we avoid defi
3600: 6e 69 6e 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a  ning it at all.*
3610: 2a 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61  * if it is alrea
3620: 64 79 20 64 65 66 69 6e 65 64 20 6f 72 20 69 66  dy defined or if
3630: 20 69 74 20 69 73 20 75 6e 6e 65 65 64 65 64 20   it is unneeded 
3640: 62 65 63 61 75 73 65 20 77 65 20 61 72 65 0a 2a  because we are.*
3650: 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 74 68  * not doing a th
3660: 72 65 61 64 73 61 66 65 20 62 75 69 6c 64 2e 20  readsafe build. 
3670: 20 54 69 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a   Ticket #2681..*
3680: 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 69  *.** See also ti
3690: 63 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23  cket #2741..*/.#
36a0: 69 66 20 21 64 65 66 69 6e 65 64 28 5f 58 4f 50  if !defined(_XOP
36b0: 45 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 21 64  EN_SOURCE) && !d
36c0: 65 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f  efined(__DARWIN_
36d0: 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f  _) && !defined(_
36e0: 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
36f0: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23  ITE_THREADSAFE.#
3700: 20 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f    define _XOPEN_
3710: 53 4f 55 52 43 45 20 35 30 30 20 20 2f 2a 20 4e  SOURCE 500  /* N
3720: 65 65 64 65 64 20 74 6f 20 65 6e 61 62 6c 65 20  eeded to enable 
3730: 70 74 68 72 65 61 64 20 72 65 63 75 72 73 69 76  pthread recursiv
3740: 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e  e mutexes */.#en
3750: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54  dif../*.** The T
3760: 43 4c 20 68 65 61 64 65 72 73 20 61 72 65 20 6f  CL headers are o
3770: 6e 6c 79 20 6e 65 65 64 65 64 20 77 68 65 6e 20  nly needed when 
3780: 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 54 43  compiling the TC
3790: 4c 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23  L bindings..*/.#
37a0: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
37b0: 45 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65  E_TCL) || define
37c0: 64 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75  d(TCLSH).# inclu
37d0: 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69  de <tcl.h>.#endi
37e0: 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65  f../*.** Many pe
37f0: 6f 70 6c 65 20 61 72 65 20 66 61 69 6c 69 6e 67  ople are failing
3800: 20 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 55 47   to set -DNDEBUG
3810: 3d 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e  =1 when compilin
3820: 67 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74  g SQLite..** Set
3830: 74 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b 65  ting NDEBUG make
3840: 73 20 74 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c  s the code small
3850: 65 72 20 61 6e 64 20 72 75 6e 20 66 61 73 74 65  er and run faste
3860: 72 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f  r.  So the follo
3870: 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72  wing.** lines ar
3880: 65 20 61 64 64 65 64 20 74 6f 20 61 75 74 6f 6d  e added to autom
3890: 61 74 69 63 61 6c 6c 79 20 73 65 74 20 4e 44 45  atically set NDE
38a0: 42 55 47 20 75 6e 6c 65 73 73 20 74 68 65 20 2d  BUG unless the -
38b0: 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a  DSQLITE_DEBUG=1.
38c0: 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74  ** option is set
38d0: 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47 20 62  .  Thus NDEBUG b
38e0: 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e  ecomes an opt-in
38f0: 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e 20   rather than an 
3900: 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75  opt-out.** featu
3910: 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  re..*/.#if !defi
3920: 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21  ned(NDEBUG) && !
3930: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
3940: 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20  EBUG) .# define 
3950: 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a  NDEBUG 1.#endif.
3960: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 63  ./*.** The testc
3970: 61 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 75  ase() macro is u
3980: 73 65 64 20 74 6f 20 61 69 64 20 69 6e 20 63 6f  sed to aid in co
3990: 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 2e 20  verage testing. 
39a0: 20 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20   When .** doing 
39b0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67  coverage testing
39c0: 2c 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20  , the condition 
39d0: 69 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 6d  inside the argum
39e0: 65 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61  ent to.** testca
39f0: 73 65 28 29 20 6d 75 73 74 20 62 65 20 65 76 61  se() must be eva
3a00: 6c 75 61 74 65 64 20 62 6f 74 68 20 74 72 75 65  luated both true
3a10: 20 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72   and false in or
3a20: 64 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75  der to.** get fu
3a30: 6c 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72 61  ll branch covera
3a40: 67 65 2e 20 20 54 68 65 20 74 65 73 74 63 61 73  ge.  The testcas
3a50: 65 28 29 20 6d 61 63 72 6f 20 69 73 20 69 6e 73  e() macro is ins
3a60: 65 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70  erted.** to help
3a70: 20 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65   ensure adequate
3a80: 20 74 65 73 74 20 63 6f 76 65 72 61 67 65 20 69   test coverage i
3a90: 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 73  n places where s
3aa0: 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69  imple.** conditi
3ab0: 6f 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65  on/decision cove
3ac0: 72 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61  rage is inadequa
3ad0: 74 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  te.  For example
3ae0: 2c 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20  , testcase().** 
3af0: 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6d  can be used to m
3b00: 61 6b 65 20 73 75 72 65 20 62 6f 75 6e 64 61 72  ake sure boundar
3b10: 79 20 76 61 6c 75 65 73 20 61 72 65 20 74 65 73  y values are tes
3b20: 74 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74  ted.  For.** bit
3b30: 6d 61 73 6b 20 74 65 73 74 73 2c 20 74 65 73 74  mask tests, test
3b40: 63 61 73 65 28 29 20 63 61 6e 20 62 65 20 75 73  case() can be us
3b50: 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ed to make sure 
3b60: 65 61 63 68 20 62 69 74 0a 2a 2a 20 69 73 20 73  each bit.** is s
3b70: 69 67 6e 69 66 69 63 61 6e 74 20 61 6e 64 20 75  ignificant and u
3b80: 73 65 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63  sed at least onc
3b90: 65 2e 20 20 4f 6e 20 73 77 69 74 63 68 20 73 74  e.  On switch st
3ba0: 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72  atements.** wher
3bb0: 65 20 6d 75 6c 74 69 70 6c 65 20 63 61 73 65 73  e multiple cases
3bc0: 20 67 6f 20 74 6f 20 74 68 65 20 73 61 6d 65 20   go to the same 
3bd0: 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74  block of code, t
3be0: 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e  estcase().** can
3bf0: 20 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c   insure that all
3c00: 20 63 61 73 65 73 20 61 72 65 20 65 76 61 6c 75   cases are evalu
3c10: 61 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64  ated..**.*/.#ifd
3c20: 65 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41  ef SQLITE_COVERA
3c30: 47 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  GE_TEST.SQLITE_P
3c40: 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
3c50: 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e  lite3Coverage(in
3c60: 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73  t);.# define tes
3c70: 74 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20  tcase(X)  if( X 
3c80: 29 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61  ){ sqlite3Covera
3c90: 67 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a  ge(__LINE__); }.
3ca0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74  #else.# define t
3cb0: 65 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69  estcase(X).#endi
3cc0: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53  f../*.** The TES
3cd0: 54 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 20 75  TONLY macro is u
3ce0: 73 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76  sed to enclose v
3cf0: 61 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74  ariable declarat
3d00: 69 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72  ions or.** other
3d10: 20 62 69 74 73 20 6f 66 20 63 6f 64 65 20 74 68   bits of code th
3d20: 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f  at are needed to
3d30: 20 73 75 70 70 6f 72 74 20 74 68 65 20 61 72 67   support the arg
3d40: 75 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  uments.** within
3d50: 20 74 65 73 74 63 61 73 65 28 29 20 61 6e 64 20   testcase() and 
3d60: 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e  assert() macros.
3d70: 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
3d80: 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69  (NDEBUG) || defi
3d90: 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52  ned(SQLITE_COVER
3da0: 41 47 45 5f 54 45 53 54 29 0a 23 20 64 65 66 69  AGE_TEST).# defi
3db0: 6e 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 20 20  ne TESTONLY(X)  
3dc0: 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  X.#else.# define
3dd0: 20 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e   TESTONLY(X).#en
3de0: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74  dif../*.** Somet
3df0: 69 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73  imes we need a s
3e00: 6d 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63  mall amount of c
3e10: 6f 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61  ode such as a va
3e20: 72 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a  riable initializ
3e30: 61 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75  ation.** to setu
3e40: 70 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73  p for a later as
3e50: 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
3e60: 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e  .  We do not wan
3e70: 74 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a  t this code to.*
3e80: 2a 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73  * appear when as
3e90: 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c  sert() is disabl
3ea0: 65 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ed.  The followi
3eb0: 6e 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72  ng macro is ther
3ec0: 65 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f  efore.** used to
3ed0: 20 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65   contain that se
3ee0: 74 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22  tup code.  The "
3ef0: 56 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61  VVA" acronym sta
3f00: 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69  nds for.** "Veri
3f10: 66 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61  fication, Valida
3f20: 74 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64  tion, and Accred
3f30: 69 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74  itation".  In ot
3f40: 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a  her words, the.*
3f50: 2a 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56  * code within VV
3f60: 41 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e  A_ONLY() will on
3f70: 6c 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65  ly run during ve
3f80: 72 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65  rification proce
3f90: 73 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  sses..*/.#ifndef
3fa0: 20 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65   NDEBUG.# define
3fb0: 20 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a   VVA_ONLY(X)  X.
3fc0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56  #else.# define V
3fd0: 56 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69  VA_ONLY(X).#endi
3fe0: 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57  f../*.** The ALW
3ff0: 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61  AYS and NEVER ma
4000: 63 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f  cros surround bo
4010: 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  olean expression
4020: 73 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20  s which .** are 
4030: 69 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61  intended to alwa
4040: 79 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61  ys be true or fa
4050: 6c 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c  lse, respectivel
4060: 79 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72  y.  Such.** expr
4070: 65 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65  essions could be
4080: 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68   omitted from th
4090: 65 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c  e code completel
40a0: 79 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20  y.  But they.** 
40b0: 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  are included in 
40c0: 61 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f  a few cases in o
40d0: 72 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20  rder to enhance 
40e0: 74 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a  the resilience.*
40f0: 2a 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75  * of SQLite to u
4100: 6e 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69  nexpected behavi
4110: 6f 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65  or - to make the
4120: 20 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c   code "self-heal
4130: 69 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74  ing".** or "duct
4140: 69 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e  ile" rather than
4150: 20 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22   being "brittle"
4160: 20 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74   and crashing at
4170: 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69   the first.** hi
4180: 6e 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20  nt of unplanned 
4190: 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
41a0: 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
41b0: 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52  ALWAYS and NEVER
41c0: 20 61 72 65 20 61 64 64 65 64 20 66 6f 72 20 64   are added for d
41d0: 65 66 65 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a  efensive code..*
41e0: 2a 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20  *.** When doing 
41f0: 63 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67  coverage testing
4200: 20 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45   ALWAYS and NEVE
4210: 52 20 61 72 65 20 68 61 72 64 2d 63 6f 64 65 64  R are hard-coded
4220: 20 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65 20 61   to.** be true a
4230: 6e 64 20 66 61 6c 73 65 20 73 6f 20 74 68 61 74  nd false so that
4240: 20 74 68 65 20 75 6e 72 65 61 63 68 61 62 6c 65   the unreachable
4250: 20 63 6f 64 65 20 74 68 65 6e 20 73 70 65 63 69   code then speci
4260: 66 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62  fy will.** not b
4270: 65 20 63 6f 75 6e 74 65 64 20 61 73 20 75 6e 74  e counted as unt
4280: 65 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23  ested code..*/.#
4290: 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
42a0: 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29  E_COVERAGE_TEST)
42b0: 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53  .# define ALWAYS
42c0: 28 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64  (X)      (1).# d
42d0: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20  efine NEVER(X)  
42e0: 20 20 20 20 20 28 30 29 0a 23 65 6c 69 66 20 21       (0).#elif !
42f0: 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a  defined(NDEBUG).
4300: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28  # define ALWAYS(
4310: 58 29 20 20 20 20 20 20 28 28 58 29 3f 31 3a 28  X)      ((X)?1:(
4320: 61 73 73 65 72 74 28 30 29 2c 30 29 29 0a 23 20  assert(0),0)).# 
4330: 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20  define NEVER(X) 
4340: 20 20 20 20 20 20 28 28 58 29 3f 28 61 73 73 65        ((X)?(asse
4350: 72 74 28 30 29 2c 31 29 3a 30 29 0a 23 65 6c 73  rt(0),1):0).#els
4360: 65 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59  e.# define ALWAY
4370: 53 28 58 29 20 20 20 20 20 20 28 58 29 0a 23 20  S(X)      (X).# 
4380: 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20  define NEVER(X) 
4390: 20 20 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66        (X).#endif
43a0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72  ../*.** The macr
43b0: 6f 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20  o unlikely() is 
43c0: 61 20 68 69 6e 74 20 74 68 61 74 20 73 75 72 72  a hint that surr
43d0: 6f 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a  ounds a boolean.
43e0: 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  ** expression th
43f0: 61 74 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61  at is usually fa
4400: 6c 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65  lse.  Macro like
4410: 6c 79 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a  ly() surrounds.*
4420: 2a 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72  * a boolean expr
4430: 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75  ession that is u
4440: 73 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43  sually true.  GC
4450: 43 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20  C is able to.** 
4460: 75 73 65 20 74 68 65 73 65 20 68 69 6e 74 73 20  use these hints 
4470: 74 6f 20 67 65 6e 65 72 61 74 65 20 62 65 74 74  to generate bett
4480: 65 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d  er code, sometim
4490: 65 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  es..*/.#if defin
44a0: 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
44b0: 30 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c  0.# define likel
44c0: 79 28 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69  y(X)    __builti
44d0: 6e 5f 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a  n_expect((X),1).
44e0: 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c  # define unlikel
44f0: 79 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f  y(X)  __builtin_
4500: 65 78 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65  expect((X),0).#e
4510: 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b  lse.# define lik
4520: 65 6c 79 28 58 29 20 20 20 20 21 21 28 58 29 0a  ely(X)    !!(X).
4530: 23 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c  # define unlikel
4540: 79 28 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64  y(X)  !!(X).#end
4550: 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
4560: 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69  *** Include sqli
4570: 74 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  te3.h in the mid
4580: 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
4590: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
45a0: 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
45b0: 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73  *** Begin file s
45c0: 71 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a  qlite3.h *******
45d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
45f0: 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
4600: 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
4610: 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
4620: 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
4630: 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
4640: 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
4650: 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
4660: 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
4670: 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
4680: 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
4690: 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
46a0: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
46b0: 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
46c0: 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
46d0: 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
46e0: 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
46f0: 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
4700: 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
4710: 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
4720: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
4730: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4750: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
4770: 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64  is header file d
4780: 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
4790: 66 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51  face that the SQ
47a0: 4c 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20  Lite library.** 
47b0: 70 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65  presents to clie
47c0: 6e 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66  nt programs.  If
47d0: 20 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73   a C-function, s
47e0: 74 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79  tructure, dataty
47f0: 70 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61  pe,.** or consta
4800: 6e 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f  nt definition do
4810: 65 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e  es not appear in
4820: 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e   this file, then
4830: 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20   it is.** not a 
4840: 70 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66  published API of
4850: 20 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a   SQLite, is subj
4860: 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69  ect to change wi
4870: 74 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c  thout.** notice,
4880: 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20   and should not 
4890: 62 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  be referenced by
48a0: 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75   programs that u
48b0: 73 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  se SQLite..**.**
48c0: 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66   Some of the def
48d0: 69 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72  initions that ar
48e0: 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61  e in this file a
48f0: 72 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20  re marked as.** 
4900: 22 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20  "experimental". 
4910: 20 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e   Experimental in
4920: 74 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72  terfaces are nor
4930: 6d 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61  mally new.** fea
4940: 74 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61  tures recently a
4950: 64 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20  dded to SQLite. 
4960: 20 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63   We do not antic
4970: 69 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a  ipate changes.**
4980: 20 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c   to experimental
4990: 20 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20   interfaces but 
49a0: 72 65 73 65 72 76 65 20 74 68 65 20 72 69 67 68  reserve the righ
49b0: 74 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20  t to make minor 
49c0: 63 68 61 6e 67 65 73 0a 2a 2a 20 69 66 20 65 78  changes.** if ex
49d0: 70 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 75 73  perience from us
49e0: 65 20 22 69 6e 20 74 68 65 20 77 69 6c 64 22 20  e "in the wild" 
49f0: 73 75 67 67 65 73 74 20 73 75 63 68 20 63 68 61  suggest such cha
4a00: 6e 67 65 73 20 61 72 65 20 70 72 75 64 65 6e 74  nges are prudent
4a10: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69  ..**.** The offi
4a20: 63 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20  cial C-language 
4a30: 41 50 49 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  API documentatio
4a40: 6e 20 66 6f 72 20 53 51 4c 69 74 65 20 69 73 20  n for SQLite is 
4a50: 64 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20  derived.** from 
4a60: 63 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 69 73  comments in this
4a70: 20 66 69 6c 65 2e 20 20 54 68 69 73 20 66 69 6c   file.  This fil
4a80: 65 20 69 73 20 74 68 65 20 61 75 74 68 6f 72 69  e is the authori
4a90: 74 61 74 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a  tative source.**
4aa0: 20 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65 20 69   on how SQLite i
4ab0: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 73 75  nterfaces are su
4ac0: 70 70 6f 73 65 20 74 6f 20 6f 70 65 72 61 74 65  ppose to operate
4ad0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65  ..**.** The name
4ae0: 20 6f 66 20 74 68 69 73 20 66 69 6c 65 20 75 6e   of this file un
4af0: 64 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  der configuratio
4b00: 6e 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69 73 20  n management is 
4b10: 22 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a  "sqlite.h.in"..*
4b20: 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d  * The makefile m
4b30: 61 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20  akes some minor 
4b40: 63 68 61 6e 67 65 73 20 74 6f 20 74 68 69 73 20  changes to this 
4b50: 66 69 6c 65 20 28 73 75 63 68 20 61 73 20 69 6e  file (such as in
4b60: 73 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76  serting.** the v
4b70: 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61  ersion number) a
4b80: 6e 64 20 63 68 61 6e 67 65 73 20 69 74 73 20 6e  nd changes its n
4b90: 61 6d 65 20 74 6f 20 22 73 71 6c 69 74 65 33 2e  ame to "sqlite3.
4ba0: 68 22 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66  h" as.** part of
4bb0: 20 74 68 65 20 62 75 69 6c 64 20 70 72 6f 63 65   the build proce
4bc0: 73 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  ss..*/.#ifndef _
4bd0: 53 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 66 69  SQLITE3_H_.#defi
4be0: 6e 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23  ne _SQLITE3_H_.#
4bf0: 69 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e  include <stdarg.
4c00: 68 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 65 64  h>     /* Needed
4c10: 20 66 6f 72 20 74 68 65 20 64 65 66 69 6e 69 74   for the definit
4c20: 69 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a  ion of va_list *
4c30: 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  /../*.** Make su
4c40: 72 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 74  re we can call t
4c50: 68 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20 43  his stuff from C
4c60: 2b 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74  ++..*/.#if 0.ext
4c70: 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66  ern "C" {.#endif
4c80: 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65  .../*.** Add the
4c90: 20 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 72   ability to over
4ca0: 72 69 64 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f  ride 'extern'.*/
4cb0: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
4cc0: 45 58 54 45 52 4e 0a 23 20 64 65 66 69 6e 65 20  EXTERN.# define 
4cd0: 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 65 78  SQLITE_EXTERN ex
4ce0: 74 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 23 69 66  tern.#endif..#if
4cf0: 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a  ndef SQLITE_API.
4d00: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
4d10: 41 50 49 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  API.#endif.../*.
4d20: 2a 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d  ** These no-op m
4d30: 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 69  acros are used i
4d40: 6e 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72  n front of inter
4d50: 66 61 63 65 73 20 74 6f 20 6d 61 72 6b 20 74 68  faces to mark th
4d60: 6f 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ose.** interface
4d70: 73 20 61 73 20 65 69 74 68 65 72 20 64 65 70 72  s as either depr
4d80: 65 63 61 74 65 64 20 6f 72 20 65 78 70 65 72 69  ecated or experi
4d90: 6d 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61 70 70  mental.  New app
4da0: 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f  lications.** sho
4db0: 75 6c 64 20 6e 6f 74 20 75 73 65 20 64 65 70 72  uld not use depr
4dc0: 65 63 61 74 65 64 20 69 6e 74 65 72 66 61 63 65  ecated interface
4dd0: 73 20 2d 20 74 68 65 79 20 61 72 65 20 73 75 70  s - they are sup
4de0: 70 6f 72 74 20 66 6f 72 20 62 61 63 6b 77 61 72  port for backwar
4df0: 64 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c  ds.** compatibil
4e00: 69 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69  ity only.  Appli
4e10: 63 61 74 69 6f 6e 20 77 72 69 74 65 72 73 20 73  cation writers s
4e20: 68 6f 75 6c 64 20 62 65 20 61 77 61 72 65 20 74  hould be aware t
4e30: 68 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e  hat.** experimen
4e40: 74 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61  tal interfaces a
4e50: 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68  re subject to ch
4e60: 61 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20 72 65  ange in point re
4e70: 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  leases..**.** Th
4e80: 65 73 65 20 6d 61 63 72 6f 73 20 75 73 65 64 20  ese macros used 
4e90: 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20 76 61  to resolve to va
4ea0: 72 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 63  rious kinds of c
4eb0: 6f 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 74 68  ompiler magic th
4ec0: 61 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65  at.** would gene
4ed0: 72 61 74 65 20 77 61 72 6e 69 6e 67 20 6d 65 73  rate warning mes
4ee0: 73 61 67 65 73 20 77 68 65 6e 20 74 68 65 79 20  sages when they 
4ef0: 77 65 72 65 20 75 73 65 64 2e 20 20 42 75 74 20  were used.  But 
4f00: 74 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72  that.** compiler
4f10: 20 6d 61 67 69 63 20 65 6e 64 65 64 20 75 70 20   magic ended up 
4f20: 67 65 6e 65 72 61 74 69 6e 67 20 73 75 63 68 20  generating such 
4f30: 61 20 66 6c 75 72 72 79 20 6f 66 20 62 75 67 20  a flurry of bug 
4f40: 72 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20  reports.** that 
4f50: 77 65 20 68 61 76 65 20 74 61 6b 65 6e 20 69 74  we have taken it
4f60: 20 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e   all out and gon
4f70: 65 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20  e back to using 
4f80: 73 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d  simple.** noop m
4f90: 61 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  acros..*/.#defin
4fa0: 65 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41  e SQLITE_DEPRECA
4fb0: 54 45 44 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  TED.#define SQLI
4fc0: 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  TE_EXPERIMENTAL.
4fd0: 0a 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68  ./*.** Ensure th
4fe0: 65 73 65 20 73 79 6d 62 6f 6c 73 20 77 65 72 65  ese symbols were
4ff0: 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20   not defined by 
5000: 73 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65  some previous he
5010: 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69  ader file..*/.#i
5020: 66 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53  fdef SQLITE_VERS
5030: 49 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51 4c 49  ION.# undef SQLI
5040: 54 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69  TE_VERSION.#endi
5050: 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
5060: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23  VERSION_NUMBER.#
5070: 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56 45   undef SQLITE_VE
5080: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e  RSION_NUMBER.#en
5090: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  dif../*.** CAPI3
50a0: 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d  REF: Compile-Tim
50b0: 65 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 6f  e Library Versio
50c0: 6e 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 31  n Numbers {H1001
50d0: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S60100>.**.*
50e0: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52  * The SQLITE_VER
50f0: 53 49 4f 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f  SION and SQLITE_
5100: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23  VERSION_NUMBER #
5110: 64 65 66 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68  defines in.** th
5120: 65 20 73 71 6c 69 74 65 33 2e 68 20 66 69 6c 65  e sqlite3.h file
5130: 20 73 70 65 63 69 66 79 20 74 68 65 20 76 65 72   specify the ver
5140: 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 77  sion of SQLite w
5150: 69 74 68 20 77 68 69 63 68 0a 2a 2a 20 74 68 61  ith which.** tha
5160: 74 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73  t header file is
5170: 20 61 73 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a   associated..**.
5180: 2a 2a 20 54 68 65 20 22 76 65 72 73 69 6f 6e 22  ** The "version"
5190: 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 61 20   of SQLite is a 
51a0: 73 74 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6f  string of the fo
51b0: 72 6d 20 22 57 2e 58 2e 59 22 20 6f 72 20 22 57  rm "W.X.Y" or "W
51c0: 2e 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20  .X.Y.Z"..** The 
51d0: 57 20 76 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72  W value is major
51e0: 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20   version number 
51f0: 61 6e 64 20 69 73 20 61 6c 77 61 79 73 20 33 20  and is always 3 
5200: 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54  in SQLite3..** T
5210: 68 65 20 57 20 76 61 6c 75 65 20 6f 6e 6c 79 20  he W value only 
5220: 63 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61 63  changes when bac
5230: 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
5240: 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65  lity is.** broke
5250: 6e 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 20  n and we intend 
5260: 74 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20 62  to never break b
5270: 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
5280: 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20 58  bility..** The X
5290: 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 69   value is the mi
52a0: 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  nor version numb
52b0: 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e  er and only chan
52c0: 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72  ges when.** ther
52d0: 65 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 74  e are major feat
52e0: 75 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73  ure enhancements
52f0: 20 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 72   that are forwar
5300: 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a  ds compatible.**
5310: 20 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 72   but not backwar
5320: 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a  ds compatible..*
5330: 2a 20 54 68 65 20 59 20 76 61 6c 75 65 20 69 73  * The Y value is
5340: 20 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 6d   the release num
5350: 62 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72 65  ber and is incre
5360: 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 65  mented with.** e
5370: 61 63 68 20 72 65 6c 65 61 73 65 20 62 75 74 20  ach release but 
5380: 72 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20 30  resets back to 0
5390: 20 77 68 65 6e 65 76 65 72 20 58 20 69 73 20 69   whenever X is i
53a0: 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 20 54  ncremented..** T
53b0: 68 65 20 5a 20 76 61 6c 75 65 20 6f 6e 6c 79 20  he Z value only 
53c0: 61 70 70 65 61 72 73 20 6f 6e 20 62 72 61 6e 63  appears on branc
53d0: 68 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a  h releases..**.*
53e0: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52  * The SQLITE_VER
53f0: 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 69 73 20 61  SION_NUMBER is a
5400: 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69  n integer that i
5410: 73 20 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a  s computed as.**
5420: 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
5430: 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65  <blockquote><pre
5440: 3e 0a 2a 2a 20 53 51 4c 49 54 45 5f 56 45 52 53  >.** SQLITE_VERS
5450: 49 4f 4e 5f 4e 55 4d 42 45 52 20 3d 20 57 2a 31  ION_NUMBER = W*1
5460: 30 30 30 30 30 30 20 2b 20 58 2a 31 30 30 30 20  000000 + X*1000 
5470: 2b 20 59 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  + Y.** </pre></b
5480: 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
5490: 20 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 33   Since version 3
54a0: 2e 36 2e 31 38 2c 20 53 51 4c 69 74 65 20 73 6f  .6.18, SQLite so
54b0: 75 72 63 65 20 63 6f 64 65 20 68 61 73 20 62 65  urce code has be
54c0: 65 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  en stored in the
54d0: 0a 2a 2a 20 3c 61 20 68 72 65 66 3d 22 68 74 74  .** <a href="htt
54e0: 70 3a 2f 2f 77 77 77 2e 66 6f 73 73 69 6c 2d 73  p://www.fossil-s
54f0: 63 6d 2e 6f 72 67 2f 22 3e 66 6f 73 73 69 6c 20  cm.org/">fossil 
5500: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61  configuration ma
5510: 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 73 79 73 74  nagement.** syst
5520: 65 6d 3c 2f 61 3e 2e 20 20 54 68 65 20 53 51 4c  em</a>.  The SQL
5530: 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a  ITE_SOURCE_ID.**
5540: 20 6d 61 63 72 6f 20 69 73 20 61 20 73 74 72 69   macro is a stri
5550: 6e 67 20 77 68 69 63 68 20 69 64 65 6e 74 69 66  ng which identif
5560: 69 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72  ies a particular
5570: 20 63 68 65 63 6b 2d 69 6e 20 6f 66 20 53 51 4c   check-in of SQL
5580: 69 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 69 74  ite.** within it
5590: 73 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  s configuration 
55a0: 6d 61 6e 61 67 65 6d 65 6e 74 20 73 79 73 74 65  management syste
55b0: 6d 2e 20 20 54 68 65 20 73 74 72 69 6e 67 20 63  m.  The string c
55c0: 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 64  ontains the.** d
55d0: 61 74 65 20 61 6e 64 20 74 69 6d 65 20 6f 66 20  ate and time of 
55e0: 74 68 65 20 63 68 65 63 6b 2d 69 6e 20 28 55 54  the check-in (UT
55f0: 43 29 20 61 6e 64 20 61 6e 20 53 48 41 31 20 68  C) and an SHA1 h
5600: 61 73 68 20 6f 66 20 74 68 65 20 65 6e 74 69 72  ash of the entir
5610: 65 0a 2a 2a 20 73 6f 75 72 63 65 20 74 72 65 65  e.** source tree
5620: 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
5630: 3a 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65  : [sqlite3_libve
5640: 72 73 69 6f 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71  rsion()],.** [sq
5650: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
5660: 5f 6e 75 6d 62 65 72 28 29 5d 2c 20 5b 73 71 6c  _number()], [sql
5670: 69 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 5d  ite3_sourceid()]
5680: 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 76 65 72  ,.** [sqlite_ver
5690: 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c  sion()] and [sql
56a0: 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d  ite_source_id()]
56b0: 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
56c0: 65 6e 74 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b  ents: [H10011] [
56d0: 48 31 30 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69  H10014].*/.#defi
56e0: 6e 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  ne SQLITE_VERSIO
56f0: 4e 20 20 20 20 20 20 20 20 22 33 2e 36 2e 32 31  N        "3.6.21
5700: 22 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ".#define SQLITE
5710: 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20  _VERSION_NUMBER 
5720: 33 30 30 36 30 32 31 0a 23 64 65 66 69 6e 65 20  3006021.#define 
5730: 53 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44  SQLITE_SOURCE_ID
5740: 20 20 20 20 20 20 22 32 30 30 39 2d 31 32 2d 30        "2009-12-0
5750: 37 20 31 36 3a 33 39 3a 31 33 20 31 65 64 38 38  7 16:39:13 1ed88
5760: 65 39 64 30 31 65 39 65 64 61 35 63 62 63 36 32  e9d01e9eda5cbc62
5770: 32 65 37 36 31 34 32 37 37 66 32 39 62 63 63 35  2e7614277f29bcc5
5780: 35 31 63 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  51c"../*.** CAPI
5790: 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c  3REF: Run-Time L
57a0: 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e  ibrary Version N
57b0: 75 6d 62 65 72 73 20 7b 48 31 30 30 32 30 7d 20  umbers {H10020} 
57c0: 3c 53 36 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57  <S60100>.** KEYW
57d0: 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 65  ORDS: sqlite3_ve
57e0: 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  rsion.**.** Thes
57f0: 65 20 69 6e 74 65 72 66 61 63 65 73 20 70 72 6f  e interfaces pro
5800: 76 69 64 65 20 74 68 65 20 73 61 6d 65 20 69 6e  vide the same in
5810: 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20 74 68 65  formation as the
5820: 20 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e   [SQLITE_VERSION
5830: 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45  ],.** [SQLITE_VE
5840: 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61  RSION_NUMBER], a
5850: 6e 64 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43  nd [SQLITE_SOURC
5860: 45 5f 49 44 5d 20 23 64 65 66 69 6e 65 73 20 69  E_ID] #defines i
5870: 6e 20 74 68 65 20 68 65 61 64 65 72 2c 0a 2a 2a  n the header,.**
5880: 20 62 75 74 20 61 72 65 20 61 73 73 6f 63 69 61   but are associa
5890: 74 65 64 20 77 69 74 68 20 74 68 65 20 6c 69 62  ted with the lib
58a0: 72 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20  rary instead of 
58b0: 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e  the header file.
58c0: 20 20 43 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72    Cautious.** pr
58d0: 6f 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20  ogrammers might 
58e0: 69 6e 63 6c 75 64 65 20 61 73 73 65 72 74 28 29  include assert()
58f0: 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74   statements in t
5900: 68 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f 6e  heir application
5910: 20 74 6f 0a 2a 2a 20 76 65 72 69 66 79 20 74 68   to.** verify th
5920: 61 74 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e  at values return
5930: 65 64 20 62 79 20 74 68 65 73 65 20 69 6e 74 65  ed by these inte
5940: 72 66 61 63 65 73 20 6d 61 74 63 68 20 74 68 65  rfaces match the
5950: 20 6d 61 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68   macros in.** th
5960: 65 20 68 65 61 64 65 72 2c 20 61 6e 64 20 74 68  e header, and th
5970: 75 73 20 69 6e 73 75 72 65 20 74 68 61 74 20 74  us insure that t
5980: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69  he application i
5990: 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69  s.** compiled wi
59a0: 74 68 20 6d 61 74 63 68 69 6e 67 20 6c 69 62 72  th matching libr
59b0: 61 72 79 20 61 6e 64 20 68 65 61 64 65 72 20 66  ary and header f
59c0: 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  iles..**.** <blo
59d0: 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
59e0: 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
59f0: 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62  _libversion_numb
5a00: 65 72 28 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52  er()==SQLITE_VER
5a10: 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a  SION_NUMBER );.*
5a20: 2a 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70  * assert( strcmp
5a30: 28 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69  (sqlite3_sourcei
5a40: 64 28 29 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43  d(),SQLITE_SOURC
5a50: 45 5f 49 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61  E_ID)==0 );.** a
5a60: 73 73 65 72 74 28 20 73 74 72 63 6d 70 28 73 71  ssert( strcmp(sq
5a70: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
5a80: 2c 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29  ,SQLITE_VERSION)
5a90: 3d 3d 30 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e  ==0 );.** </pre>
5aa0: 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
5ab0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
5ac0: 6c 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e  libversion() fun
5ad0: 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
5ae0: 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69  e same informati
5af0: 6f 6e 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74  on as is.** in t
5b00: 68 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69  he sqlite3_versi
5b10: 6f 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73  on[] string cons
5b20: 74 61 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74  tant.  The funct
5b30: 69 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a  ion is provided.
5b40: 2a 2a 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c  ** for use in DL
5b50: 4c 73 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65  Ls since DLL use
5b60: 72 73 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f  rs usually do no
5b70: 74 20 68 61 76 65 20 64 69 72 65 63 74 20 61 63  t have direct ac
5b80: 63 65 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a  cess to string.*
5b90: 2a 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68  * constants with
5ba0: 69 6e 20 74 68 65 20 44 4c 4c 2e 20 20 53 69 6d  in the DLL.  Sim
5bb0: 69 6c 61 72 6c 79 2c 20 74 68 65 20 73 71 6c 69  ilarly, the sqli
5bc0: 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 20 66  te3_sourceid() f
5bd0: 75 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72  unction.** retur
5be0: 6e 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f  ns the same info
5bf0: 72 6d 61 74 69 6f 6e 20 61 73 20 69 73 20 69 6e  rmation as is in
5c00: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 53 4f 55   the [SQLITE_SOU
5c10: 52 43 45 5f 49 44 5d 20 23 64 65 66 69 6e 65 20  RCE_ID] #define 
5c20: 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72  of.** the header
5c30: 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65   file..**.** See
5c40: 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 5f 76   also: [sqlite_v
5c50: 65 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73  ersion()] and [s
5c60: 71 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28  qlite_source_id(
5c70: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  )]..**.** Requir
5c80: 65 6d 65 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d  ements: [H10021]
5c90: 20 5b 48 31 30 30 32 32 5d 20 5b 48 31 30 30 32   [H10022] [H1002
5ca0: 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  3].*/.SQLITE_API
5cb0: 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69   const char sqli
5cc0: 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20  te3_version[] = 
5cd0: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a  SQLITE_VERSION;.
5ce0: 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
5cf0: 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c   char *sqlite3_l
5d00: 69 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b  ibversion(void);
5d10: 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
5d20: 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
5d30: 73 6f 75 72 63 65 69 64 28 76 6f 69 64 29 3b 0a  sourceid(void);.
5d40: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
5d50: 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
5d60: 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a  n_number(void);.
5d70: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
5d80: 20 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20   Test To See If 
5d90: 54 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54  The Library Is T
5da0: 68 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30  hreadsafe {H1010
5db0: 30 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S60100>.**.*
5dc0: 2a 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20  * SQLite can be 
5dd0: 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72  compiled with or
5de0: 20 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73   without mutexes
5df0: 2e 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b  .  When.** the [
5e00: 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
5e10: 45 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f  E] C preprocesso
5e20: 72 20 6d 61 63 72 6f 20 69 73 20 31 20 6f 72 20  r macro is 1 or 
5e30: 32 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72  2, mutexes.** ar
5e40: 65 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 53 51  e enabled and SQ
5e50: 4c 69 74 65 20 69 73 20 74 68 72 65 61 64 73 61  Lite is threadsa
5e60: 66 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a  fe.  When the.**
5e70: 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53   [SQLITE_THREADS
5e80: 41 46 45 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c  AFE] macro is 0,
5e90: 20 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73   .** the mutexes
5ea0: 20 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 20 57   are omitted.  W
5eb0: 69 74 68 6f 75 74 20 74 68 65 20 6d 75 74 65 78  ithout the mutex
5ec0: 65 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 73 61  es, it is not sa
5ed0: 66 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c  fe.** to use SQL
5ee0: 69 74 65 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79  ite concurrently
5ef0: 20 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20   from more than 
5f00: 6f 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a  one thread..**.*
5f10: 2a 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78  * Enabling mutex
5f20: 65 73 20 69 6e 63 75 72 73 20 61 20 6d 65 61 73  es incurs a meas
5f30: 75 72 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e  urable performan
5f40: 63 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53  ce penalty..** S
5f50: 6f 20 69 66 20 73 70 65 65 64 20 69 73 20 6f 66  o if speed is of
5f60: 20 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e   utmost importan
5f70: 63 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 65 6e  ce, it makes sen
5f80: 73 65 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a  se to disable.**
5f90: 20 74 68 65 20 6d 75 74 65 78 65 73 2e 20 20 42   the mutexes.  B
5fa0: 75 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73  ut for maximum s
5fb0: 61 66 65 74 79 2c 20 6d 75 74 65 78 65 73 20 73  afety, mutexes s
5fc0: 68 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c 65 64  hould be enabled
5fd0: 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74  ..** The default
5fe0: 20 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72   behavior is for
5ff0: 20 6d 75 74 65 78 65 73 20 74 6f 20 62 65 20 65   mutexes to be e
6000: 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  nabled..**.** Th
6010: 69 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e  is interface can
6020: 20 62 65 20 75 73 65 64 20 62 79 20 61 6e 20 61   be used by an a
6030: 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 6d 61  pplication to ma
6040: 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
6050: 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 6f 66 20 53  .** version of S
6060: 51 4c 69 74 65 20 74 68 61 74 20 69 74 20 69 73  QLite that it is
6070: 20 6c 69 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74   linking against
6080: 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69   was compiled wi
6090: 74 68 0a 2a 2a 20 74 68 65 20 64 65 73 69 72 65  th.** the desire
60a0: 64 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65  d setting of the
60b0: 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53   [SQLITE_THREADS
60c0: 41 46 45 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a  AFE] macro..**.*
60d0: 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
60e0: 20 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e   only reports on
60f0: 20 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d   the compile-tim
6100: 65 20 6d 75 74 65 78 20 73 65 74 74 69 6e 67 0a  e mutex setting.
6110: 2a 2a 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54  ** of the [SQLIT
6120: 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20 66 6c  E_THREADSAFE] fl
6130: 61 67 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69  ag.  If SQLite i
6140: 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a  s compiled with.
6150: 2a 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ** SQLITE_THREAD
6160: 53 41 46 45 3d 31 20 74 68 65 6e 20 6d 75 74 65  SAFE=1 then mute
6170: 78 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20  xes are enabled 
6180: 62 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a 2a  by default but.*
6190: 2a 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f  * can be fully o
61a0: 72 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73 61  r partially disa
61b0: 62 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61 6c  bled using a cal
61c0: 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f  l to [sqlite3_co
61d0: 6e 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20  nfig()].** with 
61e0: 74 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49 54  the verbs [SQLIT
61f0: 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54  E_CONFIG_SINGLET
6200: 48 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f  HREAD], [SQLITE_
6210: 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45  CONFIG_MULTITHRE
6220: 41 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49  AD],.** or [SQLI
6230: 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d  TE_CONFIG_MUTEX]
6240: 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
6250: 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63  lue of this func
6260: 74 69 6f 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e  tion shows.** on
6270: 6c 79 20 74 68 65 20 64 65 66 61 75 6c 74 20 63  ly the default c
6280: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74 74  ompile-time sett
6290: 69 6e 67 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e  ing, not any run
62a0: 2d 74 69 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a  -time changes.**
62b0: 20 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e 67   to that setting
62c0: 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20  ..**.** See the 
62d0: 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d  [threading mode]
62e0: 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
62f0: 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
6300: 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  formation..**.**
6310: 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b   Requirements: [
6320: 48 31 30 31 30 31 5d 20 5b 48 31 30 31 30 32 5d  H10101] [H10102]
6330: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
6340: 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  nt sqlite3_threa
6350: 64 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a  dsafe(void);../*
6360: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61  .** CAPI3REF: Da
6370: 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
6380: 6e 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30 30  n Handle {H12000
6390: 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45  } <S40200>.** KE
63a0: 59 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 73  YWORDS: {databas
63b0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64  e connection} {d
63c0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
63d0: 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  ons}.**.** Each 
63e0: 6f 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61  open SQLite data
63f0: 62 61 73 65 20 69 73 20 72 65 70 72 65 73 65 6e  base is represen
6400: 74 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 72  ted by a pointer
6410: 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   to an instance 
6420: 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 65  of.** the opaque
6430: 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64   structure named
6440: 20 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 20   "sqlite3".  It 
6450: 69 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 69  is useful to thi
6460: 6e 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33  nk of an sqlite3
6470: 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 61  .** pointer as a
6480: 6e 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 5b  n object.  The [
6490: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c  sqlite3_open()],
64a0: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36   [sqlite3_open16
64b0: 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c  ()], and.** [sql
64c0: 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20  ite3_open_v2()] 
64d0: 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69  interfaces are i
64e0: 74 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c  ts constructors,
64f0: 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c   and [sqlite3_cl
6500: 6f 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73  ose()].** is its
6510: 20 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54 68   destructor.  Th
6520: 65 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74 68  ere are many oth
6530: 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 28 73  er interfaces (s
6540: 75 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74  uch as.** [sqlit
6550: 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
6560: 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  , [sqlite3_creat
6570: 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61  e_function()], a
6580: 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62  nd.** [sqlite3_b
6590: 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74  usy_timeout()] t
65a0: 6f 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65 65  o name but three
65b0: 29 20 74 68 61 74 20 61 72 65 20 6d 65 74 68 6f  ) that are metho
65c0: 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69  ds on an.** sqli
65d0: 74 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74  te3 object..*/.t
65e0: 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
65f0: 6c 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a  lite3 sqlite3;..
6600: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
6610: 36 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20 54  64-Bit Integer T
6620: 79 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c 53  ypes {H10200} <S
6630: 31 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  10110>.** KEYWOR
6640: 44 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  DS: sqlite_int64
6650: 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a   sqlite_uint64.*
6660: 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65  *.** Because the
6670: 72 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70  re is no cross-p
6680: 6c 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20 73  latform way to s
6690: 70 65 63 69 66 79 20 36 34 2d 62 69 74 20 69 6e  pecify 64-bit in
66a0: 74 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20 53  teger types.** S
66b0: 51 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20 74  QLite includes t
66c0: 79 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d 62  ypedefs for 64-b
66d0: 69 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e  it signed and un
66e0: 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e  signed integers.
66f0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
6700: 65 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c  e3_int64 and sql
6710: 69 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65 20  ite3_uint64 are 
6720: 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 79  the preferred ty
6730: 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a  pe definitions..
6740: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69 6e  ** The sqlite_in
6750: 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f 75  t64 and sqlite_u
6760: 69 6e 74 36 34 20 74 79 70 65 73 20 61 72 65 20  int64 types are 
6770: 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62 61  supported for ba
6780: 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61  ckwards.** compa
6790: 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a  tibility only..*
67a0: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
67b0: 73 3a 20 5b 48 31 30 32 30 31 5d 20 5b 48 31 30  s: [H10201] [H10
67c0: 32 30 32 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53  202].*/.#ifdef S
67d0: 51 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45  QLITE_INT64_TYPE
67e0: 0a 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 54  .  typedef SQLIT
67f0: 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c  E_INT64_TYPE sql
6800: 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70  ite_int64;.  typ
6810: 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51  edef unsigned SQ
6820: 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20  LITE_INT64_TYPE 
6830: 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23  sqlite_uint64;.#
6840: 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53  elif defined(_MS
6850: 43 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65  C_VER) || define
6860: 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a  d(__BORLANDC__).
6870: 20 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36    typedef __int6
6880: 34 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a  4 sqlite_int64;.
6890: 20 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e    typedef unsign
68a0: 65 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74  ed __int64 sqlit
68b0: 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a  e_uint64;.#else.
68c0: 20 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c    typedef long l
68d0: 6f 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69  ong int sqlite_i
68e0: 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20  nt64;.  typedef 
68f0: 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
6900: 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69  ng int sqlite_ui
6910: 6e 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70  nt64;.#endif.typ
6920: 65 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36  edef sqlite_int6
6930: 34 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b  4 sqlite3_int64;
6940: 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f  .typedef sqlite_
6950: 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75  uint64 sqlite3_u
6960: 69 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66  int64;../*.** If
6970: 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61   compiling for a
6980: 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20   processor that 
6990: 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70  lacks floating p
69a0: 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a  oint support,.**
69b0: 20 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65   substitute inte
69c0: 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67  ger for floating
69d0: 2d 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65  -point..*/.#ifde
69e0: 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
69f0: 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64  OATING_POINT.# d
6a00: 65 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c  efine double sql
6a10: 69 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69  ite3_int64.#endi
6a20: 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  f../*.** CAPI3RE
6a30: 46 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74  F: Closing A Dat
6a40: 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e  abase Connection
6a50: 20 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 30   {H12010} <S3010
6a60: 30 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a  0><S40200>.**.**
6a70: 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
6a80: 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
6a90: 66 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33  for the [sqlite3
6aa0: 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  ] object..**.** 
6ab0: 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 6d 75 73  Applications mus
6ac0: 74 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  t [sqlite3_final
6ad0: 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20  ize | finalize] 
6ae0: 61 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 74  all [prepared st
6af0: 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64  atements].** and
6b00: 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63   [sqlite3_blob_c
6b10: 6c 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c  lose | close] al
6b20: 6c 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d  l [BLOB handles]
6b30: 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
6b40: 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33  .** the [sqlite3
6b50: 5d 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74  ] object prior t
6b60: 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20  o attempting to 
6b70: 63 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74  close the object
6b80: 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69  ..**.** If [sqli
6b90: 74 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20  te3_close()] is 
6ba0: 69 6e 76 6f 6b 65 64 20 77 68 69 6c 65 20 61 20  invoked while a 
6bb0: 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
6bc0: 70 65 6e 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e  pen,.** the tran
6bd0: 73 61 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d  saction is autom
6be0: 61 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20  atically rolled 
6bf0: 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  back..**.** The 
6c00: 43 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  C parameter to [
6c10: 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29  sqlite3_close(C)
6c20: 5d 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  ] must be either
6c30: 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74   a NULL.** point
6c40: 65 72 20 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65  er or an [sqlite
6c50: 33 5d 20 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65  3] object pointe
6c60: 72 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  r obtained.** fr
6c70: 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  om [sqlite3_open
6c80: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ()], [sqlite3_op
6c90: 65 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b  en16()], or.** [
6ca0: 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
6cb0: 29 5d 2c 20 61 6e 64 20 6e 6f 74 20 70 72 65 76  )], and not prev
6cc0: 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a  iously closed..*
6cd0: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
6ce0: 73 3a 0a 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b  s:.** [H12011] [
6cf0: 48 31 32 30 31 32 5d 20 5b 48 31 32 30 31 33 5d  H12012] [H12013]
6d00: 20 5b 48 31 32 30 31 34 5d 20 5b 48 31 32 30 31   [H12014] [H1201
6d10: 35 5d 20 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53  5] [H12019].*/.S
6d20: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
6d30: 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69  lite3_close(sqli
6d40: 74 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54  te3 *);../*.** T
6d50: 68 65 20 74 79 70 65 20 66 6f 72 20 61 20 63 61  he type for a ca
6d60: 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e  llback function.
6d70: 0a 2a 2a 20 54 68 69 73 20 69 73 20 6c 65 67 61  .** This is lega
6d80: 63 79 20 61 6e 64 20 64 65 70 72 65 63 61 74 65  cy and deprecate
6d90: 64 2e 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64  d.  It is includ
6da0: 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61  ed for historica
6db0: 6c 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69  l.** compatibili
6dc0: 74 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f  ty and is not do
6dd0: 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70  cumented..*/.typ
6de0: 65 64 65 66 20 69 6e 74 20 28 2a 73 71 6c 69 74  edef int (*sqlit
6df0: 65 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69  e3_callback)(voi
6e00: 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63  d*,int,char**, c
6e10: 68 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  har**);../*.** C
6e20: 41 50 49 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65  API3REF: One-Ste
6e30: 70 20 51 75 65 72 79 20 45 78 65 63 75 74 69 6f  p Query Executio
6e40: 6e 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 32  n Interface {H12
6e50: 31 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a  100} <S10000>.**
6e60: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
6e70: 65 78 65 63 28 29 20 69 6e 74 65 72 66 61 63 65  exec() interface
6e80: 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74   is a convenient
6e90: 20 77 61 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20   way of running 
6ea0: 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53  one or more.** S
6eb0: 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69  QL statements wi
6ec0: 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
6ed0: 77 72 69 74 65 20 61 20 6c 6f 74 20 6f 66 20 43  write a lot of C
6ee0: 20 63 6f 64 65 2e 20 20 54 68 65 20 55 54 46 2d   code.  The UTF-
6ef0: 38 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c  8 encoded.** SQL
6f00: 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20   statements are 
6f10: 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65  passed in as the
6f20: 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
6f30: 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65  r to sqlite3_exe
6f40: 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74  c()..** The stat
6f50: 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75  ements are evalu
6f60: 61 74 65 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20  ated one by one 
6f70: 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 6e 20  until either an 
6f80: 65 72 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69  error or.** an i
6f90: 6e 74 65 72 72 75 70 74 20 69 73 20 65 6e 63 6f  nterrupt is enco
6fa0: 75 6e 74 65 72 65 64 2c 20 6f 72 20 75 6e 74 69  untered, or unti
6fb0: 6c 20 74 68 65 79 20 61 72 65 20 61 6c 6c 20 64  l they are all d
6fc0: 6f 6e 65 2e 20 20 54 68 65 20 33 72 64 20 70 61  one.  The 3rd pa
6fd0: 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e  rameter.** is an
6fe0: 20 6f 70 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61   optional callba
6ff0: 63 6b 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b  ck that is invok
7000: 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  ed once for each
7010: 20 72 6f 77 20 6f 66 20 61 6e 79 20 71 75 65 72   row of any quer
7020: 79 0a 2a 2a 20 72 65 73 75 6c 74 73 20 70 72 6f  y.** results pro
7030: 64 75 63 65 64 20 62 79 20 74 68 65 20 53 51 4c  duced by the SQL
7040: 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68   statements.  Th
7050: 65 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 5th parameter 
7060: 74 65 6c 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74  tells where.** t
7070: 6f 20 77 72 69 74 65 20 61 6e 79 20 65 72 72 6f  o write any erro
7080: 72 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a  r messages..**.*
7090: 2a 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73  * The error mess
70a0: 61 67 65 20 70 61 73 73 65 64 20 62 61 63 6b 20  age passed back 
70b0: 74 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20  through the 5th 
70c0: 70 61 72 61 6d 65 74 65 72 20 69 73 20 68 65 6c  parameter is hel
70d0: 64 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f  d.** in memory o
70e0: 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71  btained from [sq
70f0: 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e  lite3_malloc()].
7100: 20 20 54 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d    To avoid a mem
7110: 6f 72 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65  ory leak,.** the
7120: 20 63 61 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61   calling applica
7130: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c  tion should call
7140: 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
7150: 5d 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a  ] on any error.*
7160: 2a 20 6d 65 73 73 61 67 65 20 72 65 74 75 72 6e  * message return
7170: 65 64 20 74 68 72 6f 75 67 68 20 74 68 65 20 35  ed through the 5
7180: 74 68 20 70 61 72 61 6d 65 74 65 72 20 77 68 65  th parameter whe
7190: 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65  n it has finishe
71a0: 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65  d using.** the e
71b0: 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a  rror message..**
71c0: 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73  .** If the SQL s
71d0: 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  tatement in the 
71e0: 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  2nd parameter is
71f0: 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74   NULL or an empt
7200: 79 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61  y string.** or a
7210: 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69   string containi
7220: 6e 67 20 6f 6e 6c 79 20 77 68 69 74 65 73 70 61  ng only whitespa
7230: 63 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c  ce and comments,
7240: 20 74 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20   then no SQL.** 
7250: 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65  statements are e
7260: 76 61 6c 75 61 74 65 64 20 61 6e 64 20 74 68 65  valuated and the
7270: 20 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74   database is not
7280: 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20   changed..**.** 
7290: 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63  The sqlite3_exec
72a0: 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  () interface is 
72b0: 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74  implemented in t
72c0: 65 72 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69  erms of.** [sqli
72d0: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
72e0: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ], [sqlite3_step
72f0: 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
7300: 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a  3_finalize()]..*
7310: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78  * The sqlite3_ex
7320: 65 63 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65  ec() routine doe
7330: 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65  s nothing to the
7340: 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63   database that c
7350: 61 6e 6e 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a  annot be done.**
7360: 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   by [sqlite3_pre
7370: 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c  pare_v2()], [sql
7380: 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e  ite3_step()], an
7390: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  d [sqlite3_final
73a0: 69 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  ize()]..**.** Th
73b0: 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
73c0: 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78  r to [sqlite3_ex
73d0: 65 63 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e  ec()] must be an
73e0: 20 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a   valid and open.
73f0: 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ** [database con
7400: 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20  nection]..**.** 
7410: 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
7420: 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74  nection must not
7430: 20 62 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65   be closed while
7440: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65  .** [sqlite3_exe
7450: 63 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e  c()] is running.
7460: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69  .**.** The calli
7470: 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  ng function shou
7480: 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f  ld use [sqlite3_
7490: 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a  free()] to free.
74a0: 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68  ** the memory th
74b0: 61 74 20 2a 65 72 72 6d 73 67 20 69 73 20 6c 65  at *errmsg is le
74c0: 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f  ft pointing at o
74d0: 6e 63 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a  nce the error.**
74e0: 20 6d 65 73 73 61 67 65 20 69 73 20 6e 6f 20 6c   message is no l
74f0: 6f 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a  onger needed..**
7500: 0a 2a 2a 20 54 68 65 20 53 51 4c 20 73 74 61 74  .** The SQL stat
7510: 65 6d 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68  ement text in th
7520: 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  e 2nd parameter 
7530: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  to [sqlite3_exec
7540: 28 29 5d 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61  ()].** must rema
7550: 69 6e 20 75 6e 63 68 61 6e 67 65 64 20 77 68 69  in unchanged whi
7560: 6c 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  le [sqlite3_exec
7570: 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a  ()] is running..
7580: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
7590: 74 73 3a 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20  ts:.** [H12101] 
75a0: 5b 48 31 32 31 30 32 5d 20 5b 48 31 32 31 30 34  [H12102] [H12104
75b0: 5d 20 5b 48 31 32 31 30 35 5d 20 5b 48 31 32 31  ] [H12105] [H121
75c0: 30 37 5d 20 5b 48 31 32 31 31 30 5d 20 5b 48 31  07] [H12110] [H1
75d0: 32 31 31 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a  2113] [H12116].*
75e0: 2a 20 5b 48 31 32 31 31 39 5d 20 5b 48 31 32 31  * [H12119] [H121
75f0: 32 32 5d 20 5b 48 31 32 31 32 35 5d 20 5b 48 31  22] [H12125] [H1
7600: 32 31 33 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b  2131] [H12134] [
7610: 48 31 32 31 33 37 5d 20 5b 48 31 32 31 33 38 5d  H12137] [H12138]
7620: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
7630: 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  nt sqlite3_exec(
7640: 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 20 20 20  .  sqlite3*,    
7650: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7670: 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73   An open databas
7680: 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
7690: 72 20 2a 73 71 6c 2c 20 20 20 20 20 20 20 20 20  r *sql,         
76a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76b0: 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65    /* SQL to be e
76c0: 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e  valuated */.  in
76d0: 74 20 28 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f  t (*callback)(vo
76e0: 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63  id*,int,char**,c
76f0: 68 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c  har**),  /* Call
7700: 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  back function */
7710: 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20  .  void *,      
7720: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7730: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7740: 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f   1st argument to
7750: 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63   callback */.  c
7760: 68 61 72 20 2a 2a 65 72 72 6d 73 67 20 20 20 20  har **errmsg    
7770: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7780: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
7790: 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68  or msg written h
77a0: 65 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ere */.);../*.**
77b0: 20 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c   CAPI3REF: Resul
77c0: 74 20 43 6f 64 65 73 20 7b 48 31 30 32 31 30 7d  t Codes {H10210}
77d0: 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59   <S10700>.** KEY
77e0: 57 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b  WORDS: SQLITE_OK
77f0: 20 7b 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65   {error code} {e
7800: 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b  rror codes}.** K
7810: 45 59 57 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74  EYWORDS: {result
7820: 20 63 6f 64 65 7d 20 7b 72 65 73 75 6c 74 20 63   code} {result c
7830: 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79  odes}.**.** Many
7840: 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e   SQLite function
7850: 73 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65  s return an inte
7860: 67 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20  ger result code 
7870: 66 72 6f 6d 20 74 68 65 20 73 65 74 20 73 68 6f  from the set sho
7880: 77 6e 0a 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72  wn.** here in or
7890: 64 65 72 20 74 6f 20 69 6e 64 69 63 61 74 65 73  der to indicates
78a0: 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c   success or fail
78b0: 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65  ure..**.** New e
78c0: 72 72 6f 72 20 63 6f 64 65 73 20 6d 61 79 20 62  rror codes may b
78d0: 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72  e added in futur
78e0: 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
78f0: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  Lite..**.** See 
7900: 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f  also: [SQLITE_IO
7910: 45 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e  ERR_READ | exten
7920: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73  ded result codes
7930: 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ].*/.#define SQL
7940: 49 54 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20  ITE_OK          
7950: 20 30 20 20 20 2f 2a 20 53 75 63 63 65 73 73 66   0   /* Successf
7960: 75 6c 20 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20  ul result */./* 
7970: 62 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72  beginning-of-err
7980: 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66  or-codes */.#def
7990: 69 6e 65 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  ine SQLITE_ERROR
79a0: 20 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 53          1   /* S
79b0: 51 4c 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73  QL error or miss
79c0: 69 6e 67 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  ing database */.
79d0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
79e0: 4e 54 45 52 4e 41 4c 20 20 20 20 20 32 20 20 20  NTERNAL     2   
79f0: 2f 2a 20 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69  /* Internal logi
7a00: 63 20 65 72 72 6f 72 20 69 6e 20 53 51 4c 69 74  c error in SQLit
7a10: 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
7a20: 49 54 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20  ITE_PERM        
7a30: 20 33 20 20 20 2f 2a 20 41 63 63 65 73 73 20 70   3   /* Access p
7a40: 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65 64  ermission denied
7a50: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
7a60: 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20  TE_ABORT        
7a70: 34 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20  4   /* Callback 
7a80: 72 6f 75 74 69 6e 65 20 72 65 71 75 65 73 74 65  routine requeste
7a90: 64 20 61 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64  d an abort */.#d
7aa0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 55 53  efine SQLITE_BUS
7ab0: 59 20 20 20 20 20 20 20 20 20 35 20 20 20 2f 2a  Y         5   /*
7ac0: 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69   The database fi
7ad0: 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a  le is locked */.
7ae0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
7af0: 4f 43 4b 45 44 20 20 20 20 20 20 20 36 20 20 20  OCKED       6   
7b00: 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68  /* A table in th
7b10: 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f  e database is lo
7b20: 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cked */.#define 
7b30: 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20  SQLITE_NOMEM    
7b40: 20 20 20 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c      7   /* A mal
7b50: 6c 6f 63 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a  loc() failed */.
7b60: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
7b70: 45 41 44 4f 4e 4c 59 20 20 20 20 20 38 20 20 20  EADONLY     8   
7b80: 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 77 72  /* Attempt to wr
7b90: 69 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64  ite a readonly d
7ba0: 61 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69  atabase */.#defi
7bb0: 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  ne SQLITE_INTERR
7bc0: 55 50 54 20 20 20 20 39 20 20 20 2f 2a 20 4f 70  UPT    9   /* Op
7bd0: 65 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74  eration terminat
7be0: 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e  ed by sqlite3_in
7bf0: 74 65 72 72 75 70 74 28 29 2a 2f 0a 23 64 65 66  terrupt()*/.#def
7c00: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
7c10: 20 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 53         10   /* S
7c20: 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b  ome kind of disk
7c30: 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72   I/O error occur
7c40: 72 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  red */.#define S
7c50: 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20  QLITE_CORRUPT   
7c60: 20 20 31 31 20 20 20 2f 2a 20 54 68 65 20 64 61    11   /* The da
7c70: 74 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67  tabase disk imag
7c80: 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a  e is malformed *
7c90: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7ca0: 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20 31 32 20  _NOTFOUND    12 
7cb0: 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54    /* NOT USED. T
7cc0: 61 62 6c 65 20 6f 72 20 72 65 63 6f 72 64 20 6e  able or record n
7cd0: 6f 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66  ot found */.#def
7ce0: 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  ine SQLITE_FULL 
7cf0: 20 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 49         13   /* I
7d00: 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c 65 64 20  nsertion failed 
7d10: 62 65 63 61 75 73 65 20 64 61 74 61 62 61 73 65  because database
7d20: 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66   is full */.#def
7d30: 69 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  ine SQLITE_CANTO
7d40: 50 45 4e 20 20 20 20 31 34 20 20 20 2f 2a 20 55  PEN    14   /* U
7d50: 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68  nable to open th
7d60: 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
7d70: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7d80: 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35  E_PROTOCOL    15
7d90: 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20     /* NOT USED. 
7da0: 44 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 70 72  Database lock pr
7db0: 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a  otocol error */.
7dc0: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45  #define SQLITE_E
7dd0: 4d 50 54 59 20 20 20 20 20 20 20 31 36 20 20 20  MPTY       16   
7de0: 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 65  /* Database is e
7df0: 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mpty */.#define 
7e00: 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 20 20  SQLITE_SCHEMA   
7e10: 20 20 20 31 37 20 20 20 2f 2a 20 54 68 65 20 64     17   /* The d
7e20: 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63  atabase schema c
7e30: 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e  hanged */.#defin
7e40: 65 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20  e SQLITE_TOOBIG 
7e50: 20 20 20 20 20 31 38 20 20 20 2f 2a 20 53 74 72       18   /* Str
7e60: 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 63 65  ing or BLOB exce
7e70: 65 64 73 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a  eds size limit *
7e80: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7e90: 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 31 39 20  _CONSTRAINT  19 
7ea0: 20 20 2f 2a 20 41 62 6f 72 74 20 64 75 65 20 74    /* Abort due t
7eb0: 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  o constraint vio
7ec0: 6c 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  lation */.#defin
7ed0: 65 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43  e SQLITE_MISMATC
7ee0: 48 20 20 20 20 32 30 20 20 20 2f 2a 20 44 61 74  H    20   /* Dat
7ef0: 61 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68 20  a type mismatch 
7f00: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7f10: 45 5f 4d 49 53 55 53 45 20 20 20 20 20 20 32 31  E_MISUSE      21
7f20: 20 20 20 2f 2a 20 4c 69 62 72 61 72 79 20 75 73     /* Library us
7f30: 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a  ed incorrectly *
7f40: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7f50: 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20 32 32 20  _NOLFS       22 
7f60: 20 20 2f 2a 20 55 73 65 73 20 4f 53 20 66 65 61    /* Uses OS fea
7f70: 74 75 72 65 73 20 6e 6f 74 20 73 75 70 70 6f 72  tures not suppor
7f80: 74 65 64 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23  ted on host */.#
7f90: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 55  define SQLITE_AU
7fa0: 54 48 20 20 20 20 20 20 20 20 32 33 20 20 20 2f  TH        23   /
7fb0: 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  * Authorization 
7fc0: 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e  denied */.#defin
7fd0: 65 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20  e SQLITE_FORMAT 
7fe0: 20 20 20 20 20 32 34 20 20 20 2f 2a 20 41 75 78       24   /* Aux
7ff0: 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20  iliary database 
8000: 66 6f 72 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a  format error */.
8010: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52  #define SQLITE_R
8020: 41 4e 47 45 20 20 20 20 20 20 20 32 35 20 20 20  ANGE       25   
8030: 2f 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72  /* 2nd parameter
8040: 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64   to sqlite3_bind
8050: 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f   out of range */
8060: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8070: 4e 4f 54 41 44 42 20 20 20 20 20 20 32 36 20 20  NOTADB      26  
8080: 20 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 65 64 20   /* File opened 
8090: 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20 64 61  that is not a da
80a0: 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23  tabase file */.#
80b0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f  define SQLITE_RO
80c0: 57 20 20 20 20 20 20 20 20 20 31 30 30 20 20 2f  W         100  /
80d0: 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  * sqlite3_step()
80e0: 20 68 61 73 20 61 6e 6f 74 68 65 72 20 72 6f 77   has another row
80f0: 20 72 65 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e   ready */.#defin
8100: 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20  e SQLITE_DONE   
8110: 20 20 20 20 20 31 30 31 20 20 2f 2a 20 73 71 6c       101  /* sql
8120: 69 74 65 33 5f 73 74 65 70 28 29 20 68 61 73 20  ite3_step() has 
8130: 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69  finished executi
8140: 6e 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d  ng */./* end-of-
8150: 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a  error-codes */..
8160: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
8170: 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20  Extended Result 
8180: 43 6f 64 65 73 20 7b 48 31 30 32 32 30 7d 20 3c  Codes {H10220} <
8190: 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S10700>.** KEYWO
81a0: 52 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 65  RDS: {extended e
81b0: 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 78 74 65  rror code} {exte
81c0: 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73  nded error codes
81d0: 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  }.** KEYWORDS: {
81e0: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
81f0: 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20  code} {extended 
8200: 72 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a  result codes}.**
8210: 0a 2a 2a 20 49 6e 20 69 74 73 20 64 65 66 61 75  .** In its defau
8220: 6c 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  lt configuration
8230: 2c 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75  , SQLite API rou
8240: 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e 65  tines return one
8250: 20 6f 66 20 32 36 20 69 6e 74 65 67 65 72 0a 2a   of 26 integer.*
8260: 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72  * [SQLITE_OK | r
8270: 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48  esult codes].  H
8280: 6f 77 65 76 65 72 2c 20 65 78 70 65 72 69 65 6e  owever, experien
8290: 63 65 20 68 61 73 20 73 68 6f 77 6e 20 74 68 61  ce has shown tha
82a0: 74 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65  t many of.** the
82b0: 73 65 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  se result codes 
82c0: 61 72 65 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67  are too coarse-g
82d0: 72 61 69 6e 65 64 2e 20 20 54 68 65 79 20 64 6f  rained.  They do
82e0: 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 73 0a   not provide as.
82f0: 2a 2a 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74  ** much informat
8300: 69 6f 6e 20 61 62 6f 75 74 20 70 72 6f 62 6c 65  ion about proble
8310: 6d 73 20 61 73 20 70 72 6f 67 72 61 6d 6d 65 72  ms as programmer
8320: 73 20 6d 69 67 68 74 20 6c 69 6b 65 2e 20 20 49  s might like.  I
8330: 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a  n an effort to.*
8340: 2a 20 61 64 64 72 65 73 73 20 74 68 69 73 2c 20  * address this, 
8350: 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f  newer versions o
8360: 66 20 53 51 4c 69 74 65 20 28 76 65 72 73 69 6f  f SQLite (versio
8370: 6e 20 33 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65  n 3.3.8 and late
8380: 72 29 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75  r) include.** su
8390: 70 70 6f 72 74 20 66 6f 72 20 61 64 64 69 74 69  pport for additi
83a0: 6f 6e 61 6c 20 72 65 73 75 6c 74 20 63 6f 64 65  onal result code
83b0: 73 20 74 68 61 74 20 70 72 6f 76 69 64 65 20 6d  s that provide m
83c0: 6f 72 65 20 64 65 74 61 69 6c 65 64 20 69 6e 66  ore detailed inf
83d0: 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75  ormation.** abou
83e0: 74 20 65 72 72 6f 72 73 2e 20 54 68 65 20 65 78  t errors. The ex
83f0: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
8400: 64 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20  des are enabled 
8410: 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f  or disabled.** o
8420: 6e 20 61 20 70 65 72 20 64 61 74 61 62 61 73 65  n a per database
8430: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69   connection basi
8440: 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b  s using the.** [
8450: 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
8460: 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d  _result_codes()]
8470: 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65   API..**.** Some
8480: 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c   of the availabl
8490: 65 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c  e extended resul
84a0: 74 20 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74  t codes are list
84b0: 65 64 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20  ed here..** One 
84c0: 6d 61 79 20 65 78 70 65 63 74 20 74 68 65 20 6e  may expect the n
84d0: 75 6d 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65  umber of extende
84e0: 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77  d result codes w
84f0: 69 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a  ill be expand.**
8500: 20 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 6f 66   over time.  Sof
8510: 74 77 61 72 65 20 74 68 61 74 20 75 73 65 73 20  tware that uses 
8520: 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
8530: 63 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 78 70  codes should exp
8540: 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65  ect.** to see ne
8550: 77 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 69  w result codes i
8560: 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
8570: 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  s of SQLite..**.
8580: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b  ** The SQLITE_OK
8590: 20 72 65 73 75 6c 74 20 63 6f 64 65 20 77 69 6c   result code wil
85a0: 6c 20 6e 65 76 65 72 20 62 65 20 65 78 74 65 6e  l never be exten
85b0: 64 65 64 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c  ded.  It will al
85c0: 77 61 79 73 0a 2a 2a 20 62 65 20 65 78 61 63 74  ways.** be exact
85d0: 6c 79 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66  ly zero..*/.#def
85e0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
85f0: 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20  _READ           
8600: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
8610: 20 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69   | (1<<8)).#defi
8620: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ne SQLITE_IOERR_
8630: 53 48 4f 52 54 5f 52 45 41 44 20 20 20 20 20 20  SHORT_READ      
8640: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
8650: 7c 20 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e  | (2<<8)).#defin
8660: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57  e SQLITE_IOERR_W
8670: 52 49 54 45 20 20 20 20 20 20 20 20 20 20 20 20  RITE            
8680: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8690: 20 28 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65   (3<<8)).#define
86a0: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53   SQLITE_IOERR_FS
86b0: 59 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20  YNC             
86c0: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
86d0: 28 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (4<<8)).#define 
86e0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52  SQLITE_IOERR_DIR
86f0: 5f 46 53 59 4e 43 20 20 20 20 20 20 20 20 20 28  _FSYNC         (
8700: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
8710: 35 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53  5<<8)).#define S
8720: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e  QLITE_IOERR_TRUN
8730: 43 41 54 45 20 20 20 20 20 20 20 20 20 20 28 53  CATE          (S
8740: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36  QLITE_IOERR | (6
8750: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
8760: 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54  LITE_IOERR_FSTAT
8770: 20 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51               (SQ
8780: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c  LITE_IOERR | (7<
8790: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
87a0: 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
87b0: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
87c0: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c  ITE_IOERR | (8<<
87d0: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
87e0: 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20  TE_IOERR_RDLOCK 
87f0: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49             (SQLI
8800: 54 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38  TE_IOERR | (9<<8
8810: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8820: 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 20  E_IOERR_DELETE  
8830: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8840: 45 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38  E_IOERR | (10<<8
8850: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8860: 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20  E_IOERR_BLOCKED 
8870: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8880: 45 5f 49 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38  E_IOERR | (11<<8
8890: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
88a0: 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20  E_IOERR_NOMEM   
88b0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
88c0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38  E_IOERR | (12<<8
88d0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
88e0: 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 20  E_IOERR_ACCESS  
88f0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8900: 45 5f 49 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38  E_IOERR | (13<<8
8910: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8920: 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
8930: 45 52 56 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54  ERVEDLOCK (SQLIT
8940: 45 5f 49 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38  E_IOERR | (14<<8
8950: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8960: 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20  E_IOERR_LOCK    
8970: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8980: 45 5f 49 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38  E_IOERR | (15<<8
8990: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
89a0: 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20  E_IOERR_CLOSE   
89b0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
89c0: 45 5f 49 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38  E_IOERR | (16<<8
89d0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
89e0: 45 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53  E_IOERR_DIR_CLOS
89f0: 45 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54  E         (SQLIT
8a00: 45 5f 49 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38  E_IOERR | (17<<8
8a10: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8a20: 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43  E_LOCKED_SHAREDC
8a30: 41 43 48 45 20 20 20 20 20 20 28 53 51 4c 49 54  ACHE      (SQLIT
8a40: 45 5f 4c 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38  E_LOCKED | (1<<8
8a50: 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  ) )../*.** CAPI3
8a60: 52 45 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46  REF: Flags For F
8a70: 69 6c 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69  ile Open Operati
8a80: 6f 6e 73 20 7b 48 31 30 32 33 30 7d 20 3c 48 31  ons {H10230} <H1
8a90: 31 31 32 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a  1120> <H12700>.*
8aa0: 2a 0a 2a 2a 20 54 68 65 73 65 20 62 69 74 20 76  *.** These bit v
8ab0: 61 6c 75 65 73 20 61 72 65 20 69 6e 74 65 6e 64  alues are intend
8ac0: 65 64 20 66 6f 72 20 75 73 65 20 69 6e 20 74 68  ed for use in th
8ad0: 65 0a 2a 2a 20 33 72 64 20 70 61 72 61 6d 65 74  e.** 3rd paramet
8ae0: 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  er to the [sqlit
8af0: 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e  e3_open_v2()] in
8b00: 74 65 72 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69  terface and.** i
8b10: 6e 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65  n the 4th parame
8b20: 74 65 72 20 74 6f 20 74 68 65 20 78 4f 70 65 6e  ter to the xOpen
8b30: 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a   method of the.*
8b40: 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20  * [sqlite3_vfs] 
8b50: 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69  object..*/.#defi
8b60: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  ne SQLITE_OPEN_R
8b70: 45 41 44 4f 4e 4c 59 20 20 20 20 20 20 20 20 20  EADONLY         
8b80: 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 4f  0x00000001  /* O
8b90: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8ba0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8bb0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  ne SQLITE_OPEN_R
8bc0: 45 41 44 57 52 49 54 45 20 20 20 20 20 20 20 20  EADWRITE        
8bd0: 30 78 30 30 30 30 30 30 30 32 20 20 2f 2a 20 4f  0x00000002  /* O
8be0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8bf0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8c00: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  ne SQLITE_OPEN_C
8c10: 52 45 41 54 45 20 20 20 20 20 20 20 20 20 20 20  REATE           
8c20: 30 78 30 30 30 30 30 30 30 34 20 20 2f 2a 20 4f  0x00000004  /* O
8c30: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8c40: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8c50: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44  ne SQLITE_OPEN_D
8c60: 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20  ELETEONCLOSE    
8c70: 30 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 56  0x00000008  /* V
8c80: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8c90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45  ne SQLITE_OPEN_E
8ca0: 58 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20  XCLUSIVE        
8cb0: 30 78 30 30 30 30 30 30 31 30 20 20 2f 2a 20 56  0x00000010  /* V
8cc0: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8cd0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  ne SQLITE_OPEN_M
8ce0: 41 49 4e 5f 44 42 20 20 20 20 20 20 20 20 20 20  AIN_DB          
8cf0: 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 56  0x00000100  /* V
8d00: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8d10: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  ne SQLITE_OPEN_T
8d20: 45 4d 50 5f 44 42 20 20 20 20 20 20 20 20 20 20  EMP_DB          
8d30: 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 56  0x00000200  /* V
8d40: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8d50: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  ne SQLITE_OPEN_T
8d60: 52 41 4e 53 49 45 4e 54 5f 44 42 20 20 20 20 20  RANSIENT_DB     
8d70: 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 56  0x00000400  /* V
8d80: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8d90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  ne SQLITE_OPEN_M
8da0: 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20  AIN_JOURNAL     
8db0: 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 56  0x00000800  /* V
8dc0: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8dd0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  ne SQLITE_OPEN_T
8de0: 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20  EMP_JOURNAL     
8df0: 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 56  0x00001000  /* V
8e00: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8e10: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  ne SQLITE_OPEN_S
8e20: 55 42 4a 4f 55 52 4e 41 4c 20 20 20 20 20 20 20  UBJOURNAL       
8e30: 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 56  0x00002000  /* V
8e40: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8e50: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  ne SQLITE_OPEN_M
8e60: 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20  ASTER_JOURNAL   
8e70: 30 78 30 30 30 30 34 30 30 30 20 20 2f 2a 20 56  0x00004000  /* V
8e80: 46 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  FS only */.#defi
8e90: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e  ne SQLITE_OPEN_N
8ea0: 4f 4d 55 54 45 58 20 20 20 20 20 20 20 20 20 20  OMUTEX          
8eb0: 30 78 30 30 30 30 38 30 30 30 20 20 2f 2a 20 4f  0x00008000  /* O
8ec0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8ed0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8ee0: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46  ne SQLITE_OPEN_F
8ef0: 55 4c 4c 4d 55 54 45 58 20 20 20 20 20 20 20 20  ULLMUTEX        
8f00: 30 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 4f  0x00010000  /* O
8f10: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8f20: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8f30: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  ne SQLITE_OPEN_S
8f40: 48 41 52 45 44 43 41 43 48 45 20 20 20 20 20 20  HAREDCACHE      
8f50: 30 78 30 30 30 32 30 30 30 30 20 20 2f 2a 20 4f  0x00020000  /* O
8f60: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8f70: 65 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69  en_v2() */.#defi
8f80: 6e 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50  ne SQLITE_OPEN_P
8f90: 52 49 56 41 54 45 43 41 43 48 45 20 20 20 20 20  RIVATECACHE     
8fa0: 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 4f  0x00040000  /* O
8fb0: 6b 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70  k for sqlite3_op
8fc0: 65 6e 5f 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a  en_v2() */../*.*
8fd0: 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 76 69  * CAPI3REF: Devi
8fe0: 63 65 20 43 68 61 72 61 63 74 65 72 69 73 74 69  ce Characteristi
8ff0: 63 73 20 7b 48 31 30 32 34 30 7d 20 3c 48 31 31  cs {H10240} <H11
9000: 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  120>.**.** The x
9010: 44 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69  DeviceCapabiliti
9020: 65 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  es method of the
9030: 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74   [sqlite3_io_met
9040: 68 6f 64 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20  hods].** object 
9050: 72 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67  returns an integ
9060: 65 72 20 77 68 69 63 68 20 69 73 20 61 20 76 65  er which is a ve
9070: 63 74 6f 72 20 6f 66 20 74 68 65 20 74 68 65 73  ctor of the thes
9080: 65 0a 2a 2a 20 62 69 74 20 76 61 6c 75 65 73 20  e.** bit values 
9090: 65 78 70 72 65 73 73 69 6e 67 20 49 2f 4f 20 63  expressing I/O c
90a0: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f  haracteristics o
90b0: 66 20 74 68 65 20 6d 61 73 73 20 73 74 6f 72 61  f the mass stora
90c0: 67 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61  ge.** device tha
90d0: 74 20 68 6f 6c 64 73 20 74 68 65 20 66 69 6c 65  t holds the file
90e0: 20 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74   that the [sqlit
90f0: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a  e3_io_methods].*
9100: 2a 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a  * refers to..**.
9110: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f  ** The SQLITE_IO
9120: 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65  CAP_ATOMIC prope
9130: 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61  rty means that a
9140: 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20  ll writes of.** 
9150: 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f  any size are ato
9160: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45  mic.  The SQLITE
9170: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e  _IOCAP_ATOMICnnn
9180: 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20   values.** mean 
9190: 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62  that writes of b
91a0: 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e  locks that are n
91b0: 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  nn bytes in size
91c0: 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67   and.** are alig
91d0: 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73  ned to an addres
91e0: 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e  s which is an in
91f0: 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f  teger multiple o
9200: 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f  f.** nnn are ato
9210: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45  mic.  The SQLITE
9220: 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45  _IOCAP_SAFE_APPE
9230: 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a  ND value means.*
9240: 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61  * that when data
9250: 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   is appended to 
9260: 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61  a file, the data
9270: 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20   is appended.** 
9280: 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73  first then the s
9290: 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
92a0: 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76  is extended, nev
92b0: 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20  er the other.** 
92c0: 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65  way around.  The
92d0: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45   SQLITE_IOCAP_SE
92e0: 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74  QUENTIAL propert
92f0: 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20  y means that.** 
9300: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77  information is w
9310: 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69  ritten to disk i
9320: 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72  n the same order
9330: 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20   as calls.** to 
9340: 78 57 72 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65  xWrite()..*/.#de
9350: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9360: 50 5f 41 54 4f 4d 49 43 20 20 20 20 20 20 20 20  P_ATOMIC        
9370: 20 20 30 78 30 30 30 30 30 30 30 31 0a 23 64 65    0x00000001.#de
9380: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9390: 50 5f 41 54 4f 4d 49 43 35 31 32 20 20 20 20 20  P_ATOMIC512     
93a0: 20 20 30 78 30 30 30 30 30 30 30 32 0a 23 64 65    0x00000002.#de
93b0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
93c0: 50 5f 41 54 4f 4d 49 43 31 4b 20 20 20 20 20 20  P_ATOMIC1K      
93d0: 20 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 65    0x00000004.#de
93e0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
93f0: 50 5f 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20  P_ATOMIC2K      
9400: 20 20 30 78 30 30 30 30 30 30 30 38 0a 23 64 65    0x00000008.#de
9410: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9420: 50 5f 41 54 4f 4d 49 43 34 4b 20 20 20 20 20 20  P_ATOMIC4K      
9430: 20 20 30 78 30 30 30 30 30 30 31 30 0a 23 64 65    0x00000010.#de
9440: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9450: 50 5f 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20  P_ATOMIC8K      
9460: 20 20 30 78 30 30 30 30 30 30 32 30 0a 23 64 65    0x00000020.#de
9470: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9480: 50 5f 41 54 4f 4d 49 43 31 36 4b 20 20 20 20 20  P_ATOMIC16K     
9490: 20 20 30 78 30 30 30 30 30 30 34 30 0a 23 64 65    0x00000040.#de
94a0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
94b0: 50 5f 41 54 4f 4d 49 43 33 32 4b 20 20 20 20 20  P_ATOMIC32K     
94c0: 20 20 30 78 30 30 30 30 30 30 38 30 0a 23 64 65    0x00000080.#de
94d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
94e0: 50 5f 41 54 4f 4d 49 43 36 34 4b 20 20 20 20 20  P_ATOMIC64K     
94f0: 20 20 30 78 30 30 30 30 30 31 30 30 0a 23 64 65    0x00000100.#de
9500: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9510: 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 20 20  P_SAFE_APPEND   
9520: 20 20 30 78 30 30 30 30 30 32 30 30 0a 23 64 65    0x00000200.#de
9530: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41  fine SQLITE_IOCA
9540: 50 5f 53 45 51 55 45 4e 54 49 41 4c 20 20 20 20  P_SEQUENTIAL    
9550: 20 20 30 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a    0x00000400../*
9560: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69  .** CAPI3REF: Fi
9570: 6c 65 20 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c  le Locking Level
9580: 73 20 7b 48 31 30 32 35 30 7d 20 3c 48 31 31 31  s {H10250} <H111
9590: 32 30 3e 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a  20> <H11310>.**.
95a0: 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 6f  ** SQLite uses o
95b0: 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65  ne of these inte
95c0: 67 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68  ger values as th
95d0: 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75  e second.** argu
95e0: 6d 65 6e 74 20 74 6f 20 63 61 6c 6c 73 20 69 74  ment to calls it
95f0: 20 6d 61 6b 65 73 20 74 6f 20 74 68 65 20 78 4c   makes to the xL
9600: 6f 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63  ock() and xUnloc
9610: 6b 28 29 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f  k() methods.** o
9620: 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f  f an [sqlite3_io
9630: 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74  _methods] object
9640: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
9650: 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20  ITE_LOCK_NONE   
9660: 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65         0.#define
9670: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41   SQLITE_LOCK_SHA
9680: 52 45 44 20 20 20 20 20 20 20 20 31 0a 23 64 65  RED        1.#de
9690: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  fine SQLITE_LOCK
96a0: 5f 52 45 53 45 52 56 45 44 20 20 20 20 20 20 32  _RESERVED      2
96b0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
96c0: 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20  LOCK_PENDING    
96d0: 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c     3.#define SQL
96e0: 49 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49  ITE_LOCK_EXCLUSI
96f0: 56 45 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20  VE     4../*.** 
9700: 43 41 50 49 33 52 45 46 3a 20 53 79 6e 63 68 72  CAPI3REF: Synchr
9710: 6f 6e 69 7a 61 74 69 6f 6e 20 54 79 70 65 20 46  onization Type F
9720: 6c 61 67 73 20 7b 48 31 30 32 36 30 7d 20 3c 48  lags {H10260} <H
9730: 31 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65  11120>.**.** Whe
9740: 6e 20 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73  n SQLite invokes
9750: 20 74 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74   the xSync() met
9760: 68 6f 64 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71  hod of an.** [sq
9770: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
9780: 5d 20 6f 62 6a 65 63 74 20 69 74 20 75 73 65 73  ] object it uses
9790: 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f   a combination o
97a0: 66 0a 2a 2a 20 74 68 65 73 65 20 69 6e 74 65 67  f.** these integ
97b0: 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65  er values as the
97c0: 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
97d0: 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
97e0: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54   SQLITE_SYNC_DAT
97f0: 41 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 75 73  AONLY flag is us
9800: 65 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ed, it means tha
9810: 74 20 74 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70  t the.** sync op
9820: 65 72 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65  eration only nee
9830: 64 73 20 74 6f 20 66 6c 75 73 68 20 64 61 74 61  ds to flush data
9840: 20 74 6f 20 6d 61 73 73 20 73 74 6f 72 61 67 65   to mass storage
9850: 2e 20 20 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f  .  Inode.** info
9860: 72 6d 61 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74  rmation need not
9870: 20 62 65 20 66 6c 75 73 68 65 64 2e 20 49 66 20   be flushed. If 
9880: 74 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62  the lower four b
9890: 69 74 73 20 6f 66 20 74 68 65 20 66 6c 61 67 0a  its of the flag.
98a0: 2a 2a 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f  ** equal SQLITE_
98b0: 53 59 4e 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61  SYNC_NORMAL, tha
98c0: 74 20 6d 65 61 6e 73 20 74 6f 20 75 73 65 20 6e  t means to use n
98d0: 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20 73 65  ormal fsync() se
98e0: 6d 61 6e 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74  mantics..** If t
98f0: 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69  he lower four bi
9900: 74 73 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f  ts equal SQLITE_
9910: 53 59 4e 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20  SYNC_FULL, that 
9920: 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20  means.** to use 
9930: 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66  Mac OS X style f
9940: 75 6c 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20  ullsync instead 
9950: 6f 66 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23  of fsync()..*/.#
9960: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59  define SQLITE_SY
9970: 4e 43 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20  NC_NORMAL       
9980: 20 30 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65   0x00002.#define
9990: 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c   SQLITE_SYNC_FUL
99a0: 4c 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30  L          0x000
99b0: 30 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  03.#define SQLIT
99c0: 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20  E_SYNC_DATAONLY 
99d0: 20 20 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a       0x00010../*
99e0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53  .** CAPI3REF: OS
99f0: 20 49 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20   Interface Open 
9a00: 46 69 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31  File Handle {H11
9a10: 31 31 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a  110} <S20110>.**
9a20: 0a 2a 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f  .** An [sqlite3_
9a30: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70  file] object rep
9a40: 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20  resents an open 
9a50: 66 69 6c 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20  file in the .** 
9a60: 5b 73 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f  [sqlite3_vfs | O
9a70: 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65  S interface laye
9a80: 72 5d 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20  r].  Individual 
9a90: 4f 53 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  OS interface.** 
9aa0: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
9ab0: 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20  will.** want to 
9ac0: 73 75 62 63 6c 61 73 73 20 74 68 69 73 20 6f 62  subclass this ob
9ad0: 6a 65 63 74 20 62 79 20 61 70 70 65 6e 64 69 6e  ject by appendin
9ae0: 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65  g additional fie
9af0: 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72  lds.** for their
9b00: 20 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20 70   own use.  The p
9b10: 4d 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69 73  Methods entry is
9b20: 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
9b30: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  .** [sqlite3_io_
9b40: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
9b50: 74 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65 74  that defines met
9b60: 68 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  hods for perform
9b70: 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61  ing.** I/O opera
9b80: 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65  tions on the ope
9b90: 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64  n file..*/.typed
9ba0: 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
9bb0: 33 5f 66 69 6c 65 20 73 71 6c 69 74 65 33 5f 66  3_file sqlite3_f
9bc0: 69 6c 65 3b 0a 73 74 72 75 63 74 20 73 71 6c 69  ile;.struct sqli
9bd0: 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e  te3_file {.  con
9be0: 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  st struct sqlite
9bf0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
9c00: 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68  ethods;  /* Meth
9c10: 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20  ods for an open 
9c20: 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  file */.};../*.*
9c30: 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49  * CAPI3REF: OS I
9c40: 6e 74 65 72 66 61 63 65 20 46 69 6c 65 20 56 69  nterface File Vi
9c50: 72 74 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62  rtual Methods Ob
9c60: 6a 65 63 74 20 7b 48 31 31 31 32 30 7d 20 3c 53  ject {H11120} <S
9c70: 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65  20110>.**.** Eve
9c80: 72 79 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 62  ry file opened b
9c90: 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  y the [sqlite3_v
9ca0: 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64  fs] xOpen method
9cb0: 20 70 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a   populates an.**
9cc0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20   [sqlite3_file] 
9cd0: 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65  object (or, more
9ce0: 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62   commonly, a sub
9cf0: 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20  class of the.** 
9d00: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f  [sqlite3_file] o
9d10: 62 6a 65 63 74 29 20 77 69 74 68 20 61 20 70 6f  bject) with a po
9d20: 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74  inter to an inst
9d30: 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a  ance of this obj
9d40: 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a  ect..** This obj
9d50: 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20  ect defines the 
9d60: 6d 65 74 68 6f 64 73 20 75 73 65 64 20 74 6f 20  methods used to 
9d70: 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20  perform various 
9d80: 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67  operations.** ag
9d90: 61 69 6e 73 74 20 74 68 65 20 6f 70 65 6e 20 66  ainst the open f
9da0: 69 6c 65 20 72 65 70 72 65 73 65 6e 74 65 64 20  ile represented 
9db0: 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  by the [sqlite3_
9dc0: 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  file] object..**
9dd0: 0a 2a 2a 20 49 66 20 74 68 65 20 78 4f 70 65 6e  .** If the xOpen
9de0: 20 6d 65 74 68 6f 64 20 73 65 74 73 20 74 68 65   method sets the
9df0: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d   sqlite3_file.pM
9e00: 65 74 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a  ethods element .
9e10: 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c  ** to a non-NULL
9e20: 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 74   pointer, then t
9e30: 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
9e40: 74 68 6f 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74  thods.xClose met
9e50: 68 6f 64 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e  hod.** may be in
9e60: 76 6f 6b 65 64 20 65 76 65 6e 20 69 66 20 74 68  voked even if th
9e70: 65 20 78 4f 70 65 6e 20 72 65 70 6f 72 74 65 64  e xOpen reported
9e80: 20 74 68 61 74 20 69 74 20 66 61 69 6c 65 64 2e   that it failed.
9e90: 20 20 54 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61    The.** only wa
9ea0: 79 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 63  y to prevent a c
9eb0: 61 6c 6c 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f  all to xClose fo
9ec0: 6c 6c 6f 77 69 6e 67 20 61 20 66 61 69 6c 65 64  llowing a failed
9ed0: 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72   xOpen.** is for
9ee0: 20 74 68 65 20 78 4f 70 65 6e 20 74 6f 20 73 65   the xOpen to se
9ef0: 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69  t the sqlite3_fi
9f00: 6c 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d  le.pMethods elem
9f10: 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ent to NULL..**.
9f20: 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67  ** The flags arg
9f30: 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d  ument to xSync m
9f40: 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51  ay be one of [SQ
9f50: 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
9f60: 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ] or.** [SQLITE_
9f70: 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65  SYNC_FULL].  The
9f80: 20 66 69 72 73 74 20 63 68 6f 69 63 65 20 69 73   first choice is
9f90: 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e   the normal fsyn
9fa0: 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f  c()..** The seco
9fb0: 6e 64 20 63 68 6f 69 63 65 20 69 73 20 61 20 4d  nd choice is a M
9fc0: 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75  ac OS X style fu
9fd0: 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20 5b 53 51  llsync.  The [SQ
9fe0: 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e  LITE_SYNC_DATAON
9ff0: 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20  LY].** flag may 
a000: 62 65 20 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e  be ORed in to in
a010: 64 69 63 61 74 65 20 74 68 61 74 20 6f 6e 6c 79  dicate that only
a020: 20 74 68 65 20 64 61 74 61 20 6f 66 20 74 68 65   the data of the
a030: 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74   file.** and not
a040: 20 69 74 73 20 69 6e 6f 64 65 20 6e 65 65 64 73   its inode needs
a050: 20 74 6f 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a   to be synced..*
a060: 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72  *.** The integer
a070: 20 76 61 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b   values to xLock
a080: 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29  () and xUnlock()
a090: 20 61 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c   are one of.** <
a0a0: 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c  ul>.** <li> [SQL
a0b0: 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a  ITE_LOCK_NONE],.
a0c0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a0d0: 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a  LOCK_SHARED],.**
a0e0: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f   <li> [SQLITE_LO
a0f0: 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a  CK_RESERVED],.**
a100: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f   <li> [SQLITE_LO
a110: 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a  CK_PENDING], or.
a120: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a130: 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e  LOCK_EXCLUSIVE].
a140: 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f  .** </ul>.** xLo
a150: 63 6b 28 29 20 69 6e 63 72 65 61 73 65 73 20 74  ck() increases t
a160: 68 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b  he lock. xUnlock
a170: 28 29 20 64 65 63 72 65 61 73 65 73 20 74 68 65  () decreases the
a180: 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43   lock..** The xC
a190: 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
a1a0: 28 29 20 6d 65 74 68 6f 64 20 63 68 65 63 6b 73  () method checks
a1b0: 20 77 68 65 74 68 65 72 20 61 6e 79 20 64 61 74   whether any dat
a1c0: 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
a1d0: 2c 0a 2a 2a 20 65 69 74 68 65 72 20 69 6e 20 74  ,.** either in t
a1e0: 68 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20 69  his process or i
a1f0: 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  n some other pro
a200: 63 65 73 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67  cess, is holding
a210: 20 61 20 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20   a RESERVED,.** 
a220: 50 45 4e 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c  PENDING, or EXCL
a230: 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68  USIVE lock on th
a240: 65 20 66 69 6c 65 2e 20 20 49 74 20 72 65 74 75  e file.  It retu
a250: 72 6e 73 20 74 72 75 65 0a 2a 2a 20 69 66 20 73  rns true.** if s
a260: 75 63 68 20 61 20 6c 6f 63 6b 20 65 78 69 73 74  uch a lock exist
a270: 73 20 61 6e 64 20 66 61 6c 73 65 20 6f 74 68 65  s and false othe
a280: 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rwise..**.** The
a290: 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20   xFileControl() 
a2a0: 6d 65 74 68 6f 64 20 69 73 20 61 20 67 65 6e 65  method is a gene
a2b0: 72 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 68  ric interface th
a2c0: 61 74 20 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d  at allows custom
a2d0: 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e  .** VFS implemen
a2e0: 74 61 74 69 6f 6e 73 20 74 6f 20 64 69 72 65 63  tations to direc
a2f0: 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f  tly control an o
a300: 70 65 6e 20 66 69 6c 65 20 75 73 69 6e 67 20 74  pen file using t
a310: 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66  he.** [sqlite3_f
a320: 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69  ile_control()] i
a330: 6e 74 65 72 66 61 63 65 2e 20 20 54 68 65 20 73  nterface.  The s
a340: 65 63 6f 6e 64 20 22 6f 70 22 20 61 72 67 75 6d  econd "op" argum
a350: 65 6e 74 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74  ent is an.** int
a360: 65 67 65 72 20 6f 70 63 6f 64 65 2e 20 20 54 68  eger opcode.  Th
a370: 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
a380: 20 69 73 20 61 20 67 65 6e 65 72 69 63 20 70 6f   is a generic po
a390: 69 6e 74 65 72 20 69 6e 74 65 6e 64 65 64 20 74  inter intended t
a3a0: 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20  o.** point to a 
a3b0: 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6d  structure that m
a3c0: 61 79 20 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d  ay contain argum
a3d0: 65 6e 74 73 20 6f 72 20 73 70 61 63 65 20 69 6e  ents or space in
a3e0: 20 77 68 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69   which to.** wri
a3f0: 74 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73  te return values
a400: 2e 20 20 50 6f 74 65 6e 74 69 61 6c 20 75 73 65  .  Potential use
a410: 73 20 66 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72  s for xFileContr
a420: 6f 6c 28 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a  ol() might be.**
a430: 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e   functions to en
a440: 61 62 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f  able blocking lo
a450: 63 6b 73 20 77 69 74 68 20 74 69 6d 65 6f 75 74  cks with timeout
a460: 73 2c 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65  s, to change the
a470: 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61  .** locking stra
a480: 74 65 67 79 20 28 66 6f 72 20 65 78 61 6d 70 6c  tegy (for exampl
a490: 65 20 74 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c  e to use dot-fil
a4a0: 65 20 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71  e locks), to inq
a4b0: 75 69 72 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68  uire.** about th
a4c0: 65 20 73 74 61 74 75 73 20 6f 66 20 61 20 6c 6f  e status of a lo
a4d0: 63 6b 2c 20 6f 72 20 74 6f 20 62 72 65 61 6b 20  ck, or to break 
a4e0: 73 74 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68  stale locks.  Th
a4f0: 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65  e SQLite.** core
a500: 20 72 65 73 65 72 76 65 73 20 61 6c 6c 20 6f 70   reserves all op
a510: 63 6f 64 65 73 20 6c 65 73 73 20 74 68 61 6e 20  codes less than 
a520: 31 30 30 20 66 6f 72 20 69 74 73 20 6f 77 6e 20  100 for its own 
a530: 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54  use..** A [SQLIT
a540: 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
a550: 45 20 7c 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f  E | list of opco
a560: 64 65 73 5d 20 6c 65 73 73 20 74 68 61 6e 20 31  des] less than 1
a570: 30 30 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e  00 is available.
a580: 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  .** Applications
a590: 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 20 63   that define a c
a5a0: 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72  ustom xFileContr
a5b0: 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64  ol method should
a5c0: 20 75 73 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20   use opcodes.** 
a5d0: 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 30 30  greater than 100
a5e0: 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69   to avoid confli
a5f0: 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  cts..**.** The x
a600: 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d 65 74  SectorSize() met
a610: 68 6f 64 20 72 65 74 75 72 6e 73 20 74 68 65 20  hod returns the 
a620: 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74  sector size of t
a630: 68 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61  he.** device tha
a640: 74 20 75 6e 64 65 72 6c 69 65 73 20 74 68 65 20  t underlies the 
a650: 66 69 6c 65 2e 20 20 54 68 65 20 73 65 63 74 6f  file.  The secto
a660: 72 20 73 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a  r size is the.**
a670: 20 6d 69 6e 69 6d 75 6d 20 77 72 69 74 65 20 74   minimum write t
a680: 68 61 74 20 63 61 6e 20 62 65 20 70 65 72 66 6f  hat can be perfo
a690: 72 6d 65 64 20 77 69 74 68 6f 75 74 20 64 69 73  rmed without dis
a6a0: 74 75 72 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72  turbing.** other
a6b0: 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 66 69   bytes in the fi
a6c0: 6c 65 2e 20 20 54 68 65 20 78 44 65 76 69 63 65  le.  The xDevice
a6d0: 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
a6e0: 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75  ).** method retu
a6f0: 72 6e 73 20 61 20 62 69 74 20 76 65 63 74 6f 72  rns a bit vector
a700: 20 64 65 73 63 72 69 62 69 6e 67 20 62 65 68 61   describing beha
a710: 76 69 6f 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20  viors of the.** 
a720: 75 6e 64 65 72 6c 79 69 6e 67 20 64 65 76 69 63  underlying devic
a730: 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  e:.**.** <ul>.**
a740: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f   <li> [SQLITE_IO
a750: 43 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c  CAP_ATOMIC].** <
a760: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
a770: 50 5f 41 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20  P_ATOMIC512].** 
a780: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a790: 41 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20  AP_ATOMIC1K].** 
a7a0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a7b0: 41 50 5f 41 54 4f 4d 49 43 32 4b 5d 0a 2a 2a 20  AP_ATOMIC2K].** 
a7c0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a7d0: 41 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20  AP_ATOMIC4K].** 
a7e0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a7f0: 41 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20  AP_ATOMIC8K].** 
a800: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a810: 41 50 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a  AP_ATOMIC16K].**
a820: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f   <li> [SQLITE_IO
a830: 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a  CAP_ATOMIC32K].*
a840: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
a850: 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a  OCAP_ATOMIC64K].
a860: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a870: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e  IOCAP_SAFE_APPEN
a880: 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  D].** <li> [SQLI
a890: 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54  TE_IOCAP_SEQUENT
a8a0: 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  IAL].** </ul>.**
a8b0: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49  .** The SQLITE_I
a8c0: 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70  OCAP_ATOMIC prop
a8d0: 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20  erty means that 
a8e0: 61 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a  all writes of.**
a8f0: 20 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74   any size are at
a900: 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54  omic.  The SQLIT
a910: 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e  E_IOCAP_ATOMICnn
a920: 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e  n values.** mean
a930: 20 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20   that writes of 
a940: 62 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20  blocks that are 
a950: 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a  nnn bytes in siz
a960: 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69  e and.** are ali
a970: 67 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65  gned to an addre
a980: 73 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69  ss which is an i
a990: 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20  nteger multiple 
a9a0: 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74  of.** nnn are at
a9b0: 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54  omic.  The SQLIT
a9c0: 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50  E_IOCAP_SAFE_APP
a9d0: 45 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a  END value means.
a9e0: 2a 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74  ** that when dat
a9f0: 61 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f  a is appended to
aa00: 20 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74   a file, the dat
aa10: 61 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a  a is appended.**
aa20: 20 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20   first then the 
aa30: 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
aa40: 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65   is extended, ne
aa50: 76 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a  ver the other.**
aa60: 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68   way around.  Th
aa70: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  e SQLITE_IOCAP_S
aa80: 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72  EQUENTIAL proper
aa90: 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a  ty means that.**
aaa0: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
aab0: 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20  written to disk 
aac0: 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65  in the same orde
aad0: 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f  r as calls.** to
aae0: 20 78 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a   xWrite()..**.**
aaf0: 20 49 66 20 78 52 65 61 64 28 29 20 72 65 74 75   If xRead() retu
ab00: 72 6e 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  rns SQLITE_IOERR
ab10: 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 74 20 6d  _SHORT_READ it m
ab20: 75 73 74 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a  ust also fill.**
ab30: 20 69 6e 20 74 68 65 20 75 6e 72 65 61 64 20 70   in the unread p
ab40: 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62  ortions of the b
ab50: 75 66 66 65 72 20 77 69 74 68 20 7a 65 72 6f 73  uffer with zeros
ab60: 2e 20 20 41 20 56 46 53 20 74 68 61 74 0a 2a 2a  .  A VFS that.**
ab70: 20 66 61 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66   fails to zero-f
ab80: 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20  ill short reads 
ab90: 6d 69 67 68 74 20 73 65 65 6d 20 74 6f 20 77 6f  might seem to wo
aba0: 72 6b 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a  rk.  However,.**
abb0: 20 66 61 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f   failure to zero
abc0: 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64  -fill short read
abd0: 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c  s will eventuall
abe0: 79 20 6c 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74  y lead to.** dat
abf0: 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  abase corruption
ac00: 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
ac10: 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  uct sqlite3_io_m
ac20: 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 69  ethods sqlite3_i
ac30: 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63  o_methods;.struc
ac40: 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
ac50: 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 69 56 65  hods {.  int iVe
ac60: 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78  rsion;.  int (*x
ac70: 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 66  Close)(sqlite3_f
ac80: 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  ile*);.  int (*x
ac90: 52 65 61 64 29 28 73 71 6c 69 74 65 33 5f 66 69  Read)(sqlite3_fi
aca0: 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20  le*, void*, int 
acb0: 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e  iAmt, sqlite3_in
acc0: 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e  t64 iOfst);.  in
acd0: 74 20 28 2a 78 57 72 69 74 65 29 28 73 71 6c 69  t (*xWrite)(sqli
ace0: 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74  te3_file*, const
acf0: 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74   void*, int iAmt
ad00: 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
ad10: 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a  iOfst);.  int (*
ad20: 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74  xTruncate)(sqlit
ad30: 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65  e3_file*, sqlite
ad40: 33 5f 69 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20  3_int64 size);. 
ad50: 20 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71   int (*xSync)(sq
ad60: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
ad70: 20 66 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28   flags);.  int (
ad80: 2a 78 46 69 6c 65 53 69 7a 65 29 28 73 71 6c 69  *xFileSize)(sqli
ad90: 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74  te3_file*, sqlit
ada0: 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29  e3_int64 *pSize)
adb0: 3b 0a 20 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29  ;.  int (*xLock)
adc0: 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
add0: 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55  int);.  int (*xU
ade0: 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66  nlock)(sqlite3_f
adf0: 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e  ile*, int);.  in
ae00: 74 20 28 2a 78 43 68 65 63 6b 52 65 73 65 72 76  t (*xCheckReserv
ae10: 65 64 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f  edLock)(sqlite3_
ae20: 66 69 6c 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73  file*, int *pRes
ae30: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46  Out);.  int (*xF
ae40: 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69  ileControl)(sqli
ae50: 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f  te3_file*, int o
ae60: 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a  p, void *pArg);.
ae70: 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53    int (*xSectorS
ae80: 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ize)(sqlite3_fil
ae90: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65  e*);.  int (*xDe
aea0: 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
aeb0: 69 63 73 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ics)(sqlite3_fil
aec0: 65 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69  e*);.  /* Additi
aed0: 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79  onal methods may
aee0: 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
aef0: 75 72 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a  ure releases */.
af00: 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  };../*.** CAPI3R
af10: 45 46 3a 20 53 74 61 6e 64 61 72 64 20 46 69 6c  EF: Standard Fil
af20: 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65  e Control Opcode
af30: 73 20 7b 48 31 31 33 31 30 7d 20 3c 53 33 30 38  s {H11310} <S308
af40: 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  00>.**.** These 
af50: 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
af60: 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f  s are opcodes fo
af70: 72 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72  r the xFileContr
af80: 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20  ol method.** of 
af90: 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f  the [sqlite3_io_
afa0: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20  methods] object 
afb0: 61 6e 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c  and for the [sql
afc0: 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f  ite3_file_contro
afd0: 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63  l()].** interfac
afe0: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51  e..**.** The [SQ
aff0: 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
b000: 54 41 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20  TATE] opcode is 
b010: 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69  used for debuggi
b020: 6e 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63  ng.  This.** opc
b030: 6f 64 65 20 63 61 75 73 65 73 20 74 68 65 20 78  ode causes the x
b040: 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68  FileControl meth
b050: 6f 64 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  od to write the 
b060: 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
b070: 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e  .** the lock (on
b080: 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  e of [SQLITE_LOC
b090: 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45  K_NONE], [SQLITE
b0a0: 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a  _LOCK_SHARED],.*
b0b0: 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52  * [SQLITE_LOCK_R
b0c0: 45 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54  ESERVED], [SQLIT
b0d0: 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c  E_LOCK_PENDING],
b0e0: 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b   or [SQLITE_LOCK
b0f0: 5f 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20  _EXCLUSIVE]).** 
b100: 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  into an integer 
b110: 74 68 61 74 20 74 68 65 20 70 41 72 67 20 61 72  that the pArg ar
b120: 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f  gument points to
b130: 2e 20 54 68 69 73 20 63 61 70 61 62 69 6c 69 74  . This capabilit
b140: 79 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72  y.** is used dur
b150: 69 6e 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20  ing testing and 
b160: 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65  only needs to be
b170: 20 73 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20   supported when 
b180: 53 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69  SQLITE_TEST.** i
b190: 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64  s defined..*/.#d
b1a0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e  efine SQLITE_FCN
b1b0: 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 20 20 20  TL_LOCKSTATE    
b1c0: 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
b1d0: 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f  LITE_GET_LOCKPRO
b1e0: 58 59 46 49 4c 45 20 20 20 20 20 20 32 0a 23 64  XYFILE      2.#d
b1f0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 54  efine SQLITE_SET
b200: 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20  _LOCKPROXYFILE  
b210: 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51      3.#define SQ
b220: 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20  LITE_LAST_ERRNO 
b230: 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 0a 2f              4../
b240: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d  *.** CAPI3REF: M
b250: 75 74 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37  utex Handle {H17
b260: 31 31 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a  110} <S20130>.**
b270: 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f  .** The mutex mo
b280: 64 75 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69  dule within SQLi
b290: 74 65 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69  te defines [sqli
b2a0: 74 65 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65  te3_mutex] to be
b2b0: 20 61 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20   an.** abstract 
b2c0: 74 79 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78  type for a mutex
b2d0: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51   object.  The SQ
b2e0: 4c 69 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20  Lite core never 
b2f0: 6c 6f 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20  looks.** at the 
b300: 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65  internal represe
b310: 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73  ntation of an [s
b320: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20  qlite3_mutex].  
b330: 49 74 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73  It only.** deals
b340: 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
b350: 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  o the [sqlite3_m
b360: 75 74 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  utex] object..**
b370: 0a 2a 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20  .** Mutexes are 
b380: 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73  created using [s
b390: 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
b3a0: 6f 63 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65  oc()]..*/.typede
b3b0: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
b3c0: 5f 6d 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d  _mutex sqlite3_m
b3d0: 75 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  utex;../*.** CAP
b3e0: 49 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66  I3REF: OS Interf
b3f0: 61 63 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31  ace Object {H111
b400: 34 30 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a  40} <S20100>.**.
b410: 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
b420: 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66  f the sqlite3_vf
b430: 73 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73  s object defines
b440: 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 62   the interface b
b450: 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51  etween.** the SQ
b460: 4c 69 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68  Lite core and th
b470: 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65  e underlying ope
b480: 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20  rating system.  
b490: 54 68 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20  The "vfs".** in 
b4a0: 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
b4b0: 6f 62 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f  object stands fo
b4c0: 72 20 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20  r "virtual file 
b4d0: 73 79 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54  system"..**.** T
b4e0: 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
b4f0: 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69  iVersion field i
b500: 73 20 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75  s initially 1 bu
b510: 74 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20  t may be larger 
b520: 69 6e 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72  in.** future ver
b530: 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e  sions of SQLite.
b540: 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65    Additional fie
b550: 6c 64 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e  lds may be appen
b560: 64 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f  ded to this.** o
b570: 62 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 69  bject when the i
b580: 56 65 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73  Version value is
b590: 20 69 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74   increased.  Not
b5a0: 65 20 74 68 61 74 20 74 68 65 20 73 74 72 75 63  e that the struc
b5b0: 74 75 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73  ture.** of the s
b5c0: 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
b5d0: 74 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65  t changes in the
b5e0: 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74   transaction bet
b5f0: 77 65 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76  ween.** SQLite v
b600: 65 72 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64  ersion 3.5.9 and
b610: 20 33 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74   3.6.0 and yet t
b620: 68 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c  he iVersion fiel
b630: 64 20 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64  d was not.** mod
b640: 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ified..**.** The
b650: 20 73 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20   szOsFile field 
b660: 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  is the size of t
b670: 68 65 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73  he subclassed [s
b680: 71 6c 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20  qlite3_file].** 
b690: 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20 62  structure used b
b6a0: 79 20 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50  y this VFS.  mxP
b6b0: 61 74 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d  athname is the m
b6c0: 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
b6d0: 0a 2a 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69  .** a pathname i
b6e0: 6e 20 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a  n this VFS..**.*
b6f0: 2a 20 52 65 67 69 73 74 65 72 65 64 20 73 71 6c  * Registered sql
b700: 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73  ite3_vfs objects
b710: 20 61 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c   are kept on a l
b720: 69 6e 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65  inked list forme
b730: 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78  d by.** the pNex
b740: 74 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20  t pointer.  The 
b750: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67  [sqlite3_vfs_reg
b760: 69 73 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20  ister()].** and 
b770: 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72  [sqlite3_vfs_unr
b780: 65 67 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72  egister()] inter
b790: 66 61 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69  faces manage thi
b7a0: 73 20 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74  s list.** in a t
b7b0: 68 72 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20  hread-safe way. 
b7c0: 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   The [sqlite3_vf
b7d0: 73 5f 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66  s_find()] interf
b7e0: 61 63 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20  ace.** searches 
b7f0: 74 68 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68  the list.  Neith
b800: 65 72 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  er the applicati
b810: 6f 6e 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20  on code nor the 
b820: 56 46 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  VFS.** implement
b830: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65  ation should use
b840: 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74   the pNext point
b850: 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e  er..**.** The pN
b860: 65 78 74 20 66 69 65 6c 64 20 69 73 20 74 68 65  ext field is the
b870: 20 6f 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74   only field in t
b880: 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a  he sqlite3_vfs.*
b890: 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  * structure that
b8a0: 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65   SQLite will eve
b8b0: 72 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74  r modify.  SQLit
b8c0: 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65  e will only acce
b8d0: 73 73 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20  ss.** or modify 
b8e0: 74 68 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65  this field while
b8f0: 20 68 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69   holding a parti
b900: 63 75 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74  cular static mut
b910: 65 78 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69  ex..** The appli
b920: 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65  cation should ne
b930: 76 65 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68  ver modify anyth
b940: 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 73  ing within the s
b950: 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62  qlite3_vfs.** ob
b960: 6a 65 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62  ject once the ob
b970: 6a 65 63 74 20 68 61 73 20 62 65 65 6e 20 72 65  ject has been re
b980: 67 69 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  gistered..**.** 
b990: 54 68 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20  The zName field 
b9a0: 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f  holds the name o
b9b0: 66 20 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65  f the VFS module
b9c0: 2e 20 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74  .  The name must
b9d0: 0a 2a 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63  .** be unique ac
b9e0: 72 6f 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64  ross all VFS mod
b9f0: 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  ules..**.** SQLi
ba00: 74 65 20 77 69 6c 6c 20 67 75 61 72 61 6e 74 65  te will guarante
ba10: 65 20 74 68 61 74 20 74 68 65 20 7a 46 69 6c 65  e that the zFile
ba20: 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74  name parameter t
ba30: 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69  o xOpen.** is ei
ba40: 74 68 65 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  ther a NULL poin
ba50: 74 65 72 20 6f 72 20 73 74 72 69 6e 67 20 6f 62  ter or string ob
ba60: 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78  tained.** from x
ba70: 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20  FullPathname(). 
ba80: 20 53 51 4c 69 74 65 20 66 75 72 74 68 65 72 20   SQLite further 
ba90: 67 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a  guarantees that.
baa0: 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 77 69  ** the string wi
bab0: 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20  ll be valid and 
bac0: 75 6e 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20  unchanged until 
bad0: 78 43 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63  xClose() is.** c
bae0: 61 6c 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f  alled. Because o
baf0: 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73  f the previous s
bb00: 65 6e 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20  entence,.** the 
bb10: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63  [sqlite3_file] c
bb20: 61 6e 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20  an safely store 
bb30: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
bb40: 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20  .** filename if 
bb50: 69 74 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65  it needs to reme
bb60: 6d 62 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d  mber the filenam
bb70: 65 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f  e for some reaso
bb80: 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69  n..** If the zFi
bb90: 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72  lename parameter
bba0: 20 69 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e   is xOpen is a N
bbb0: 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e  ULL pointer then
bbc0: 20 78 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69   xOpen.** must i
bbd0: 6e 76 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65  nvent its own te
bbe0: 6d 70 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72  mporary name for
bbf0: 20 74 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e   the file.  When
bc00: 65 76 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69  ever the .** xFi
bc10: 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72  lename parameter
bc20: 20 69 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c   is NULL it will
bc30: 20 61 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73   also be the cas
bc40: 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c  e that the.** fl
bc50: 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 77 69  ags parameter wi
bc60: 6c 6c 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49  ll include [SQLI
bc70: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
bc80: 43 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  CLOSE]..**.** Th
bc90: 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74  e flags argument
bca0: 20 74 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c   to xOpen() incl
bcb0: 75 64 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65  udes all bits se
bcc0: 74 20 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67  t in.** the flag
bcd0: 73 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73  s argument to [s
bce0: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
bcf0: 5d 2e 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74  ].  Or if [sqlit
bd00: 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72  e3_open()].** or
bd10: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36   [sqlite3_open16
bd20: 28 29 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65  ()] is used, the
bd30: 6e 20 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73  n flags includes
bd40: 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51   at least.** [SQ
bd50: 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
bd60: 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f  ITE] | [SQLITE_O
bd70: 50 45 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a  PEN_CREATE]. .**
bd80: 20 49 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e   If xOpen() open
bd90: 73 20 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e  s a file read-on
bda0: 6c 79 20 74 68 65 6e 20 69 74 20 73 65 74 73 20  ly then it sets 
bdb0: 2a 70 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a  *pOutFlags to.**
bdc0: 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45   include [SQLITE
bdd0: 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e  _OPEN_READONLY].
bde0: 20 20 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20    Other bits in 
bdf0: 2a 70 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62  *pOutFlags may b
be00: 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  e set..**.** SQL
be10: 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64  ite will also ad
be20: 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c  d one of the fol
be30: 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20  lowing flags to 
be40: 74 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63  the xOpen().** c
be50: 61 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f  all, depending o
be60: 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65 69  n the object bei
be70: 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a  ng opened:.**.**
be80: 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b   <ul>.** <li>  [
be90: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
bea0: 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  _DB].** <li>  [S
beb0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
bec0: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e  JOURNAL].** <li>
bed0: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54    [SQLITE_OPEN_T
bee0: 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20  EMP_DB].** <li> 
bef0: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45   [SQLITE_OPEN_TE
bf00: 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c  MP_JOURNAL].** <
bf10: 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45  li>  [SQLITE_OPE
bf20: 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a  N_TRANSIENT_DB].
bf30: 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45  ** <li>  [SQLITE
bf40: 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c  _OPEN_SUBJOURNAL
bf50: 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49  ].** <li>  [SQLI
bf60: 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a  TE_OPEN_MASTER_J
bf70: 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e  OURNAL].** </ul>
bf80: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20  .**.** The file 
bf90: 49 2f 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  I/O implementati
bfa0: 6f 6e 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f  on can use the o
bfb0: 62 6a 65 63 74 20 74 79 70 65 20 66 6c 61 67 73  bject type flags
bfc0: 20 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68   to.** change th
bfd0: 65 20 77 61 79 20 69 74 20 64 65 61 6c 73 20 77  e way it deals w
bfe0: 69 74 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20  ith files.  For 
bff0: 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c  example, an appl
c000: 69 63 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20  ication.** that 
c010: 64 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62  does not care ab
c020: 6f 75 74 20 63 72 61 73 68 20 72 65 63 6f 76 65  out crash recove
c030: 72 79 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d  ry or rollback m
c040: 69 67 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65  ight make.** the
c050: 20 6f 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e   open of a journ
c060: 61 6c 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e  al file a no-op.
c070: 20 20 57 72 69 74 65 73 20 74 6f 20 74 68 69 73    Writes to this
c080: 20 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a   journal would.*
c090: 2a 20 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73  * also be no-ops
c0a0: 2c 20 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70  , and any attemp
c0b0: 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f  t to read the jo
c0c0: 75 72 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75  urnal would retu
c0d0: 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45  rn.** SQLITE_IOE
c0e0: 52 52 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c  RR.  Or the impl
c0f0: 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74  ementation might
c100: 20 72 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20   recognize that 
c110: 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  a database.** fi
c120: 6c 65 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67  le will be doing
c130: 20 70 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65   page-aligned se
c140: 63 74 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77  ctor reads and w
c150: 72 69 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f  rites in a rando
c160: 6d 0a 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73  m.** order and s
c170: 65 74 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75  et up its I/O su
c180: 62 73 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e  bsystem accordin
c190: 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  gly..**.** SQLit
c1a0: 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64  e might also add
c1b0: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
c1c0: 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74  owing flags to t
c1d0: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a  he xOpen method:
c1e0: 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c  .**.** <ul>.** <
c1f0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  li> [SQLITE_OPEN
c200: 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a  _DELETEONCLOSE].
c210: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
c220: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a  OPEN_EXCLUSIVE].
c230: 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54  ** </ul>.**.** T
c240: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  he [SQLITE_OPEN_
c250: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66  DELETEONCLOSE] f
c260: 6c 61 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69  lag means the fi
c270: 6c 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20  le should be.** 
c280: 64 65 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20  deleted when it 
c290: 69 73 20 63 6c 6f 73 65 64 2e 20 20 54 68 65 20  is closed.  The 
c2a0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c  [SQLITE_OPEN_DEL
c2b0: 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77  ETEONCLOSE].** w
c2c0: 69 6c 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54  ill be set for T
c2d0: 45 4d 50 20 20 64 61 74 61 62 61 73 65 73 2c 20  EMP  databases, 
c2e0: 6a 6f 75 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72  journals and for
c2f0: 20 73 75 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a   subjournals..**
c300: 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f  .** The [SQLITE_
c310: 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20  OPEN_EXCLUSIVE] 
c320: 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20 75  flag is always u
c330: 73 65 64 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69  sed in conjuncti
c340: 6f 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b  on.** with the [
c350: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
c360: 54 45 5d 20 66 6c 61 67 2c 20 77 68 69 63 68 20  TE] flag, which 
c370: 61 72 65 20 62 6f 74 68 20 64 69 72 65 63 74 6c  are both directl
c380: 79 0a 2a 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74  y.** analogous t
c390: 6f 20 74 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64  o the O_EXCL and
c3a0: 20 4f 5f 43 52 45 41 54 20 66 6c 61 67 73 20 6f   O_CREAT flags o
c3b0: 66 20 74 68 65 20 50 4f 53 49 58 20 6f 70 65 6e  f the POSIX open
c3c0: 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20  ().** API.  The 
c3d0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
c3e0: 55 53 49 56 45 20 66 6c 61 67 2c 20 77 68 65 6e  USIVE flag, when
c3f0: 20 70 61 69 72 65 64 20 77 69 74 68 20 74 68 65   paired with the
c400: 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e   .** SQLITE_OPEN
c410: 5f 43 52 45 41 54 45 2c 20 69 73 20 75 73 65 64  _CREATE, is used
c420: 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61   to indicate tha
c430: 74 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c  t file should al
c440: 77 61 79 73 0a 2a 2a 20 62 65 20 63 72 65 61 74  ways.** be creat
c450: 65 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20  ed, and that it 
c460: 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 69  is an error if i
c470: 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  t already exists
c480: 2e 0a 2a 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f  ..** It is <i>no
c490: 74 3c 2f 69 3e 20 75 73 65 64 20 74 6f 20 69 6e  t</i> used to in
c4a0: 64 69 63 61 74 65 20 74 68 65 20 66 69 6c 65 20  dicate the file 
c4b0: 73 68 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65 64  should be opened
c4c0: 20 0a 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69   .** for exclusi
c4d0: 76 65 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a  ve access..**.**
c4e0: 20 41 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69   At least szOsFi
c4f0: 6c 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  le bytes of memo
c500: 72 79 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  ry are allocated
c510: 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f   by SQLite.** to
c520: 20 68 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69   hold the  [sqli
c530: 74 65 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74  te3_file] struct
c540: 75 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68  ure passed as th
c550: 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d  e third.** argum
c560: 65 6e 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54  ent to xOpen.  T
c570: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20  he xOpen method 
c580: 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f  does not have to
c590: 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65  .** allocate the
c5a0: 20 73 74 72 75 63 74 75 72 65 3b 20 69 74 20 73   structure; it s
c5b0: 68 6f 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20  hould just fill 
c5c0: 69 74 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61  it in.  Note tha
c5d0: 74 0a 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d  t.** the xOpen m
c5e0: 65 74 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74  ethod must set t
c5f0: 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e  he sqlite3_file.
c600: 70 4d 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68  pMethods to eith
c610: 65 72 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73  er.** a valid [s
c620: 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
c630: 73 5d 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20  s] object or to 
c640: 4e 55 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73  NULL.  xOpen mus
c650: 74 20 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65  t do.** this eve
c660: 6e 20 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61  n if the open fa
c670: 69 6c 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70  ils.  SQLite exp
c680: 65 63 74 73 20 74 68 61 74 20 74 68 65 20 73 71  ects that the sq
c690: 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68  lite3_file.pMeth
c6a0: 6f 64 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77  ods.** element w
c6b0: 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74  ill be valid aft
c6c0: 65 72 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73  er xOpen returns
c6d0: 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74   regardless of t
c6e0: 68 65 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72  he success.** or
c6f0: 20 66 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20   failure of the 
c700: 78 4f 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a  xOpen call..**.*
c710: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75  * The flags argu
c720: 6d 65 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28  ment to xAccess(
c730: 29 20 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45  ) may be [SQLITE
c740: 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a  _ACCESS_EXISTS].
c750: 2a 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74  ** to test for t
c760: 68 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20  he existence of 
c770: 61 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49  a file, or [SQLI
c780: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
c790: 49 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20  ITE] to.** test 
c7a0: 77 68 65 74 68 65 72 20 61 20 66 69 6c 65 20 69  whether a file i
c7b0: 73 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77  s readable and w
c7c0: 72 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c  ritable, or [SQL
c7d0: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d  ITE_ACCESS_READ]
c7e0: 0a 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74  .** to test whet
c7f0: 68 65 72 20 61 20 66 69 6c 65 20 69 73 20 61 74  her a file is at
c800: 20 6c 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e   least readable.
c810: 20 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e 20     The file can 
c820: 62 65 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72  be a.** director
c830: 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  y..**.** SQLite 
c840: 77 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f  will always allo
c850: 63 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78  cate at least mx
c860: 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73  Pathname+1 bytes
c870: 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70   for the.** outp
c880: 75 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50  ut buffer xFullP
c890: 61 74 68 6e 61 6d 65 2e 20 20 54 68 65 20 65 78  athname.  The ex
c8a0: 61 63 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20  act size of the 
c8b0: 6f 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a  output buffer.**
c8c0: 20 69 73 20 61 6c 73 6f 20 70 61 73 73 65 64 20   is also passed 
c8d0: 61 73 20 61 20 70 61 72 61 6d 65 74 65 72 20 74  as a parameter t
c8e0: 6f 20 62 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e  o both  methods.
c8f0: 20 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 62   If the output b
c900: 75 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20  uffer.** is not 
c910: 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53  large enough, [S
c920: 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20  QLITE_CANTOPEN] 
c930: 73 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e  should be return
c940: 65 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69  ed. Since this i
c950: 73 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20  s.** handled as 
c960: 61 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79  a fatal error by
c970: 20 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70   SQLite, vfs imp
c980: 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f  lementations sho
c990: 75 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20  uld endeavor.** 
c9a0: 74 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 20  to prevent this 
c9b0: 62 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74  by setting mxPat
c9c0: 68 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69  hname to a suffi
c9d0: 63 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61  ciently large va
c9e0: 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  lue..**.** The x
c9f0: 52 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53  Randomness(), xS
ca00: 6c 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72  leep(), and xCur
ca10: 72 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72  rentTime() inter
ca20: 66 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74  faces.** are not
ca30: 20 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74   strictly a part
ca40: 20 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74   of the filesyst
ca50: 65 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65  em, but they are
ca60: 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  .** included in 
ca70: 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72  the VFS structur
ca80: 65 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65  e for completene
ca90: 73 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64  ss..** The xRand
caa0: 6f 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f  omness() functio
cab0: 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65  n attempts to re
cac0: 74 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65  turn nBytes byte
cad0: 73 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61  s.** of good-qua
cae0: 6c 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20  lity randomness 
caf0: 69 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20  into zOut.  The 
cb00: 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a  return value is.
cb10: 2a 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75  ** the actual nu
cb20: 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
cb30: 20 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61   randomness obta
cb40: 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c  ined..** The xSl
cb50: 65 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75  eep() method cau
cb60: 73 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  ses the calling 
cb70: 74 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20  thread to sleep 
cb80: 66 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20  for at.** least 
cb90: 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  the number of mi
cba0: 63 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e  croseconds given
cbb0: 2e 20 20 54 68 65 20 78 43 75 72 72 65 6e 74 54  .  The xCurrentT
cbc0: 69 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20  ime().** method 
cbd0: 72 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e  returns a Julian
cbe0: 20 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20   Day Number for 
cbf0: 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65  the current date
cc00: 20 61 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f   and time..**.*/
cc10: 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
cc20: 73 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c 69  sqlite3_vfs sqli
cc30: 74 65 33 5f 76 66 73 3b 0a 73 74 72 75 63 74 20  te3_vfs;.struct 
cc40: 73 71 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20 20  sqlite3_vfs {.  
cc50: 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 20  int iVersion;   
cc60: 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 75           /* Stru
cc70: 63 74 75 72 65 20 76 65 72 73 69 6f 6e 20 6e 75  cture version nu
cc80: 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  mber */.  int sz
cc90: 4f 73 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20  OsFile;         
cca0: 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75     /* Size of su
ccb0: 62 63 6c 61 73 73 65 64 20 73 71 6c 69 74 65 33  bclassed sqlite3
ccc0: 5f 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d  _file */.  int m
ccd0: 78 50 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 20  xPathname;      
cce0: 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66      /* Maximum f
ccf0: 69 6c 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e  ile pathname len
cd00: 67 74 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  gth */.  sqlite3
cd10: 5f 76 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 20  _vfs *pNext;    
cd20: 20 20 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 74    /* Next regist
cd30: 65 72 65 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f  ered VFS */.  co
cd40: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
cd50: 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
cd60: 66 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 66  f this virtual f
cd70: 69 6c 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20  ile system */.  
cd80: 76 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20  void *pAppData; 
cd90: 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
cda0: 74 65 72 20 74 6f 20 61 70 70 6c 69 63 61 74 69  ter to applicati
cdb0: 6f 6e 2d 73 70 65 63 69 66 69 63 20 64 61 74 61  on-specific data
cdc0: 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65   */.  int (*xOpe
cdd0: 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  n)(sqlite3_vfs*,
cde0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
cdf0: 6d 65 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  me, sqlite3_file
ce00: 2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  *,.             
ce10: 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74    int flags, int
ce20: 20 2a 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20   *pOutFlags);.  
ce30: 69 6e 74 20 28 2a 78 44 65 6c 65 74 65 29 28 73  int (*xDelete)(s
ce40: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e  qlite3_vfs*, con
ce50: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
ce60: 69 6e 74 20 73 79 6e 63 44 69 72 29 3b 0a 20 20  int syncDir);.  
ce70: 69 6e 74 20 28 2a 78 41 63 63 65 73 73 29 28 73  int (*xAccess)(s
ce80: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e  qlite3_vfs*, con
ce90: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
cea0: 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a  int flags, int *
ceb0: 70 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20  pResOut);.  int 
cec0: 28 2a 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  (*xFullPathname)
ced0: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63  (sqlite3_vfs*, c
cee0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
cef0: 2c 20 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72  , int nOut, char
cf00: 20 2a 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20   *zOut);.  void 
cf10: 2a 28 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69  *(*xDlOpen)(sqli
cf20: 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20  te3_vfs*, const 
cf30: 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
cf40: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 45 72  ;.  void (*xDlEr
cf50: 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73  ror)(sqlite3_vfs
cf60: 2a 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68  *, int nByte, ch
cf70: 61 72 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20  ar *zErrMsg);.  
cf80: 76 6f 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29  void (*(*xDlSym)
cf90: 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f  (sqlite3_vfs*,vo
cfa0: 69 64 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  id*, const char 
cfb0: 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29  *zSymbol))(void)
cfc0: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c  ;.  void (*xDlCl
cfd0: 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73  ose)(sqlite3_vfs
cfe0: 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74  *, void*);.  int
cff0: 20 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28   (*xRandomness)(
d000: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e  sqlite3_vfs*, in
d010: 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a  t nByte, char *z
d020: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53  Out);.  int (*xS
d030: 6c 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66  leep)(sqlite3_vf
d040: 73 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  s*, int microsec
d050: 6f 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78  onds);.  int (*x
d060: 43 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c  CurrentTime)(sql
d070: 69 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c  ite3_vfs*, doubl
d080: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65  e*);.  int (*xGe
d090: 74 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69  tLastError)(sqli
d0a0: 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63  te3_vfs*, int, c
d0b0: 68 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77  har *);.  /* New
d0c0: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61   fields may be a
d0d0: 70 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72  ppended in figur
d0e0: 65 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65  e versions.  The
d0f0: 20 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76   iVersion.  ** v
d100: 61 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d  alue will increm
d110: 65 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68 69  ent whenever thi
d120: 73 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b  s happens. */.};
d130: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
d140: 3a 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20  : Flags for the 
d150: 78 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 68  xAccess VFS meth
d160: 6f 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31  od {H11190} <H11
d170: 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  140>.**.** These
d180: 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
d190: 74 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 61  ts can be used a
d1a0: 73 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61  s the third para
d1b0: 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20  meter to.** the 
d1c0: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 6f  xAccess method o
d1d0: 66 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66  f an [sqlite3_vf
d1e0: 73 5d 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d  s] object. {END}
d1f0: 20 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65    They determine
d200: 0a 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66  .** what kind of
d210: 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65   permissions the
d220: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20   xAccess method 
d230: 69 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a  is looking for..
d240: 2a 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41  ** With SQLITE_A
d250: 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68  CCESS_EXISTS, th
d260: 65 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64  e xAccess method
d270: 0a 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b  .** simply check
d280: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69  s whether the fi
d290: 6c 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69  le exists..** Wi
d2a0: 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  th SQLITE_ACCESS
d2b0: 5f 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20  _READWRITE, the 
d2c0: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a  xAccess method.*
d2d0: 2a 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72  * checks whether
d2e0: 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74   the file is bot
d2f0: 68 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77  h readable and w
d300: 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68  ritable..** With
d310: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52   SQLITE_ACCESS_R
d320: 45 41 44 2c 20 74 68 65 20 78 41 63 63 65 73 73  EAD, the xAccess
d330: 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b   method.** check
d340: 73 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69  s whether the fi
d350: 6c 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0a  le is readable..
d360: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
d370: 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20  E_ACCESS_EXISTS 
d380: 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
d390: 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57  ITE_ACCESS_READW
d3a0: 52 49 54 45 20 31 0a 23 64 65 66 69 6e 65 20 53  RITE 1.#define S
d3b0: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
d3c0: 44 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20  D      2../*.** 
d3d0: 43 41 50 49 33 52 45 46 3a 20 49 6e 69 74 69 61  CAPI3REF: Initia
d3e0: 6c 69 7a 65 20 54 68 65 20 53 51 4c 69 74 65 20  lize The SQLite 
d3f0: 4c 69 62 72 61 72 79 20 7b 48 31 30 31 33 30 7d  Library {H10130}
d400: 20 3c 53 32 30 30 30 30 3e 3c 53 33 30 31 30 30   <S20000><S30100
d410: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
d420: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
d430: 20 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c   routine initial
d440: 69 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69  izes the.** SQLi
d450: 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65  te library.  The
d460: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
d470: 6e 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64  n() routine.** d
d480: 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72  eallocates any r
d490: 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 77 65  esources that we
d4a0: 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  re allocated by 
d4b0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
d4c0: 7a 65 28 29 2e 0a 2a 2a 20 54 68 65 73 65 20 72  ze()..** These r
d4d0: 6f 75 74 69 6e 65 73 20 61 72 65 20 64 65 73 69  outines are desi
d4e0: 67 6e 65 64 20 74 6f 20 61 69 64 20 69 6e 20 70  gned to aid in p
d4f0: 72 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a  rocess initializ
d500: 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 68 75  ation and.** shu
d510: 74 64 6f 77 6e 20 6f 6e 20 65 6d 62 65 64 64 65  tdown on embedde
d520: 64 20 73 79 73 74 65 6d 73 2e 20 20 57 6f 72 6b  d systems.  Work
d530: 73 74 61 74 69 6f 6e 20 61 70 70 6c 69 63 61 74  station applicat
d540: 69 6f 6e 73 20 75 73 69 6e 67 0a 2a 2a 20 53 51  ions using.** SQ
d550: 4c 69 74 65 20 6e 6f 72 6d 61 6c 6c 79 20 64 6f  Lite normally do
d560: 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 69 6e 76   not need to inv
d570: 6f 6b 65 20 65 69 74 68 65 72 20 6f 66 20 74 68  oke either of th
d580: 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  ese routines..**
d590: 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71  .** A call to sq
d5a0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
d5b0: 28 29 20 69 73 20 61 6e 20 22 65 66 66 65 63 74  () is an "effect
d5c0: 69 76 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20  ive" call if it 
d5d0: 69 73 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  is.** the first 
d5e0: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  time sqlite3_ini
d5f0: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76  tialize() is inv
d600: 6f 6b 65 64 20 64 75 72 69 6e 67 20 74 68 65 20  oked during the 
d610: 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74  lifetime of.** t
d620: 68 65 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69  he process, or i
d630: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73  f it is the firs
d640: 74 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69  t time sqlite3_i
d650: 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69  nitialize() is i
d660: 6e 76 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77  nvoked.** follow
d670: 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ing a call to sq
d680: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
d690: 2e 20 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63  .  Only an effec
d6a0: 74 69 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20  tive call.** of 
d6b0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
d6c0: 7a 65 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e  ze() does any in
d6d0: 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41  itialization.  A
d6e0: 6c 6c 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a  ll other calls.*
d6f0: 2a 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e  * are harmless n
d700: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63  o-ops..**.** A c
d710: 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73  all to sqlite3_s
d720: 68 75 74 64 6f 77 6e 28 29 20 69 73 20 61 6e 20  hutdown() is an 
d730: 22 65 66 66 65 63 74 69 76 65 22 20 63 61 6c 6c  "effective" call
d740: 20 69 66 20 69 74 20 69 73 20 74 68 65 20 66 69   if it is the fi
d750: 72 73 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73  rst.** call to s
d760: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
d770: 29 20 73 69 6e 63 65 20 74 68 65 20 6c 61 73 74  ) since the last
d780: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
d790: 69 7a 65 28 29 2e 20 20 4f 6e 6c 79 0a 2a 2a 20  ize().  Only.** 
d7a0: 61 6e 20 65 66 66 65 63 74 69 76 65 20 63 61 6c  an effective cal
d7b0: 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75  l to sqlite3_shu
d7c0: 74 64 6f 77 6e 28 29 20 64 6f 65 73 20 61 6e 79  tdown() does any
d7d0: 20 64 65 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f   deinitializatio
d7e0: 6e 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20  n..** All other 
d7f0: 76 61 6c 69 64 20 63 61 6c 6c 73 20 74 6f 20 73  valid calls to s
d800: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
d810: 29 20 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e  ) are harmless n
d820: 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  o-ops..**.** The
d830: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
d840: 69 7a 65 28 29 20 69 6e 74 65 72 66 61 63 65 20  ize() interface 
d850: 69 73 20 74 68 72 65 61 64 73 61 66 65 2c 20 62  is threadsafe, b
d860: 75 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  ut sqlite3_shutd
d870: 6f 77 6e 28 29 0a 2a 2a 20 69 73 20 6e 6f 74 2e  own().** is not.
d880: 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 68    The sqlite3_sh
d890: 75 74 64 6f 77 6e 28 29 20 69 6e 74 65 72 66 61  utdown() interfa
d8a0: 63 65 20 6d 75 73 74 20 6f 6e 6c 79 20 62 65 20  ce must only be 
d8b0: 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 0a 2a 2a  called from a.**
d8c0: 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 20   single thread. 
d8d0: 20 41 6c 6c 20 6f 70 65 6e 20 5b 64 61 74 61 62   All open [datab
d8e0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d  ase connections]
d8f0: 20 6d 75 73 74 20 62 65 20 63 6c 6f 73 65 64 20   must be closed 
d900: 61 6e 64 20 61 6c 6c 0a 2a 2a 20 6f 74 68 65 72  and all.** other
d910: 20 53 51 4c 69 74 65 20 72 65 73 6f 75 72 63 65   SQLite resource
d920: 73 20 6d 75 73 74 20 62 65 20 64 65 61 6c 6c 6f  s must be deallo
d930: 63 61 74 65 64 20 70 72 69 6f 72 20 74 6f 20 69  cated prior to i
d940: 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  nvoking.** sqlit
d950: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 0a 2a  e3_shutdown()..*
d960: 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f 74 68 65 72  *.** Among other
d970: 20 74 68 69 6e 67 73 2c 20 73 71 6c 69 74 65 33   things, sqlite3
d980: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 77 69  _initialize() wi
d990: 6c 6c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c  ll invoke.** sql
d9a0: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20  ite3_os_init(). 
d9b0: 20 53 69 6d 69 6c 61 72 6c 79 2c 20 73 71 6c 69   Similarly, sqli
d9c0: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a  te3_shutdown().*
d9d0: 2a 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 73 71  * will invoke sq
d9e0: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a  lite3_os_end()..
d9f0: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
da00: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72  3_initialize() r
da10: 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 5b  outine returns [
da20: 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75  SQLITE_OK] on su
da30: 63 63 65 73 73 2e 0a 2a 2a 20 49 66 20 66 6f 72  ccess..** If for
da40: 20 73 6f 6d 65 20 72 65 61 73 6f 6e 2c 20 73 71   some reason, sq
da50: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
da60: 28 29 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20  () is unable to 
da70: 69 6e 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 68  initialize.** th
da80: 65 20 6c 69 62 72 61 72 79 20 28 70 65 72 68 61  e library (perha
da90: 70 73 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20  ps it is unable 
daa0: 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  to allocate a ne
dab0: 65 64 65 64 20 72 65 73 6f 75 72 63 65 20 73 75  eded resource su
dac0: 63 68 0a 2a 2a 20 61 73 20 61 20 6d 75 74 65 78  ch.** as a mutex
dad0: 29 20 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20  ) it returns an 
dae0: 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 74 68  [error code] oth
daf0: 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f  er than [SQLITE_
db00: 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  OK]..**.** The s
db10: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
db20: 65 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63  e() routine is c
db30: 61 6c 6c 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79  alled internally
db40: 20 62 79 20 6d 61 6e 79 20 6f 74 68 65 72 0a 2a   by many other.*
db50: 2a 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  * SQLite interfa
db60: 63 65 73 20 73 6f 20 74 68 61 74 20 61 6e 20 61  ces so that an a
db70: 70 70 6c 69 63 61 74 69 6f 6e 20 75 73 75 61 6c  pplication usual
db80: 6c 79 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  ly does not need
db90: 20 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71   to.** invoke sq
dba0: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
dbb0: 28 29 20 64 69 72 65 63 74 6c 79 2e 20 20 46 6f  () directly.  Fo
dbc0: 72 20 65 78 61 6d 70 6c 65 2c 20 5b 73 71 6c 69  r example, [sqli
dbd0: 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63  te3_open()].** c
dbe0: 61 6c 6c 73 20 73 71 6c 69 74 65 33 5f 69 6e 69  alls sqlite3_ini
dbf0: 74 69 61 6c 69 7a 65 28 29 20 73 6f 20 74 68 65  tialize() so the
dc00: 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20   SQLite library 
dc10: 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
dc20: 63 61 6c 6c 79 0a 2a 2a 20 69 6e 69 74 69 61 6c  cally.** initial
dc30: 69 7a 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74  ized when [sqlit
dc40: 65 33 5f 6f 70 65 6e 28 29 5d 20 69 73 20 63 61  e3_open()] is ca
dc50: 6c 6c 65 64 20 69 66 20 69 74 20 68 61 73 20 6e  lled if it has n
dc60: 6f 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ot be initialize
dc70: 64 0a 2a 2a 20 61 6c 72 65 61 64 79 2e 20 20 48  d.** already.  H
dc80: 6f 77 65 76 65 72 2c 20 69 66 20 53 51 4c 69 74  owever, if SQLit
dc90: 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  e is compiled wi
dca0: 74 68 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f  th the [SQLITE_O
dcb0: 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a  MIT_AUTOINIT].**
dcc0: 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70   compile-time op
dcd0: 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 61  tion, then the a
dce0: 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c 73 20 74  utomatic calls t
dcf0: 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  o sqlite3_initia
dd00: 6c 69 7a 65 28 29 0a 2a 2a 20 61 72 65 20 6f 6d  lize().** are om
dd10: 69 74 74 65 64 20 61 6e 64 20 74 68 65 20 61 70  itted and the ap
dd20: 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 63  plication must c
dd30: 61 6c 6c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  all sqlite3_init
dd40: 69 61 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c  ialize() directl
dd50: 79 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 75 73  y.** prior to us
dd60: 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51  ing any other SQ
dd70: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20  Lite interface. 
dd80: 20 46 6f 72 20 6d 61 78 69 6d 75 6d 20 70 6f 72   For maximum por
dd90: 74 61 62 69 6c 69 74 79 2c 0a 2a 2a 20 69 74 20  tability,.** it 
dda0: 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74  is recommended t
ddb0: 68 61 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  hat applications
ddc0: 20 61 6c 77 61 79 73 20 69 6e 76 6f 6b 65 20 73   always invoke s
ddd0: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
dde0: 65 28 29 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20  e().** directly 
ddf0: 70 72 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61  prior to using a
de00: 6e 79 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20  ny other SQLite 
de10: 69 6e 74 65 72 66 61 63 65 2e 20 20 46 75 74 75  interface.  Futu
de20: 72 65 20 72 65 6c 65 61 73 65 73 0a 2a 2a 20 6f  re releases.** o
de30: 66 20 53 51 4c 69 74 65 20 6d 61 79 20 72 65 71  f SQLite may req
de40: 75 69 72 65 20 74 68 69 73 2e 20 20 49 6e 20 6f  uire this.  In o
de50: 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20  ther words, the 
de60: 62 65 68 61 76 69 6f 72 20 65 78 68 69 62 69 74  behavior exhibit
de70: 65 64 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74  ed.** when SQLit
de80: 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  e is compiled wi
de90: 74 68 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  th [SQLITE_OMIT_
dea0: 41 55 54 4f 49 4e 49 54 5d 20 6d 69 67 68 74 20  AUTOINIT] might 
deb0: 62 65 63 6f 6d 65 20 74 68 65 0a 2a 2a 20 64 65  become the.** de
dec0: 66 61 75 6c 74 20 62 65 68 61 76 69 6f 72 20 69  fault behavior i
ded0: 6e 20 73 6f 6d 65 20 66 75 74 75 72 65 20 72 65  n some future re
dee0: 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e  lease of SQLite.
def0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
df00: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 72 6f 75  e3_os_init() rou
df10: 74 69 6e 65 20 64 6f 65 73 20 6f 70 65 72 61 74  tine does operat
df20: 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69  ing-system speci
df30: 66 69 63 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a  fic.** initializ
df40: 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c  ation of the SQL
df50: 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68  ite library.  Th
df60: 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  e sqlite3_os_end
df70: 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 75 6e  ().** routine un
df80: 64 6f 65 73 20 74 68 65 20 65 66 66 65 63 74 20  does the effect 
df90: 6f 66 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  of sqlite3_os_in
dfa0: 69 74 28 29 2e 20 20 54 79 70 69 63 61 6c 20 74  it().  Typical t
dfb0: 61 73 6b 73 0a 2a 2a 20 70 65 72 66 6f 72 6d 65  asks.** performe
dfc0: 64 20 62 79 20 74 68 65 73 65 20 72 6f 75 74 69  d by these routi
dfd0: 6e 65 73 20 69 6e 63 6c 75 64 65 20 61 6c 6c 6f  nes include allo
dfe0: 63 61 74 69 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f  cation or deallo
dff0: 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61  cation.** of sta
e000: 74 69 63 20 72 65 73 6f 75 72 63 65 73 2c 20 69  tic resources, i
e010: 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66  nitialization of
e020: 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
e030: 73 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 75 70  s,.** setting up
e040: 20 61 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69   a default [sqli
e050: 74 65 33 5f 76 66 73 5d 20 6d 6f 64 75 6c 65 2c  te3_vfs] module,
e060: 20 6f 72 20 73 65 74 74 69 6e 67 20 75 70 0a 2a   or setting up.*
e070: 2a 20 61 20 64 65 66 61 75 6c 74 20 63 6f 6e 66  * a default conf
e080: 69 67 75 72 61 74 69 6f 6e 20 75 73 69 6e 67 20  iguration using 
e090: 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28  [sqlite3_config(
e0a0: 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70  )]..**.** The ap
e0b0: 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64  plication should
e0c0: 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 65 69   never invoke ei
e0d0: 74 68 65 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f  ther sqlite3_os_
e0e0: 69 6e 69 74 28 29 0a 2a 2a 20 6f 72 20 73 71 6c  init().** or sql
e0f0: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 64 69  ite3_os_end() di
e100: 72 65 63 74 6c 79 2e 20 20 54 68 65 20 61 70 70  rectly.  The app
e110: 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20  lication should 
e120: 6f 6e 6c 79 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73  only invoke.** s
e130: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
e140: 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  e() and sqlite3_
e150: 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 54 68 65  shutdown().  The
e160: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
e170: 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  ().** interface 
e180: 69 73 20 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61  is called automa
e190: 74 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74  tically by sqlit
e1a0: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
e1b0: 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f  and.** sqlite3_o
e1c0: 73 5f 65 6e 64 28 29 20 69 73 20 63 61 6c 6c 65  s_end() is calle
e1d0: 64 20 62 79 20 73 71 6c 69 74 65 33 5f 73 68 75  d by sqlite3_shu
e1e0: 74 64 6f 77 6e 28 29 2e 20 20 41 70 70 72 6f 70  tdown().  Approp
e1f0: 72 69 61 74 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65  riate.** impleme
e200: 6e 74 61 74 69 6f 6e 73 20 66 6f 72 20 73 71 6c  ntations for sql
e210: 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61  ite3_os_init() a
e220: 6e 64 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  nd sqlite3_os_en
e230: 64 28 29 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74  d().** are built
e240: 20 69 6e 74 6f 20 53 51 4c 69 74 65 20 77 68 65   into SQLite whe
e250: 6e 20 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64  n it is compiled
e260: 20 66 6f 72 20 55 6e 69 78 2c 20 57 69 6e 64 6f   for Unix, Windo
e270: 77 73 2c 20 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20  ws, or OS/2..** 
e280: 57 68 65 6e 20 5b 63 75 73 74 6f 6d 20 62 75 69  When [custom bui
e290: 6c 64 73 20 7c 20 62 75 69 6c 74 20 66 6f 72 20  lds | built for 
e2a0: 6f 74 68 65 72 20 70 6c 61 74 66 6f 72 6d 73 5d  other platforms]
e2b0: 0a 2a 2a 20 28 75 73 69 6e 67 20 74 68 65 20 5b  .** (using the [
e2c0: 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d  SQLITE_OS_OTHER=
e2d0: 31 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a  1] compile-time.
e2e0: 2a 2a 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61  ** option) the a
e2f0: 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20  pplication must 
e300: 73 75 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c  supply a suitabl
e310: 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
e320: 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   for.** sqlite3_
e330: 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71  os_init() and sq
e340: 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20  lite3_os_end(). 
e350: 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   An application-
e360: 73 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c  supplied.** impl
e370: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71  ementation of sq
e380: 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20  lite3_os_init() 
e390: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  or sqlite3_os_en
e3a0: 64 28 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75  d().** must retu
e3b0: 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f  rn [SQLITE_OK] o
e3c0: 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f  n success and so
e3d0: 6d 65 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20  me other [error 
e3e0: 63 6f 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61  code] upon.** fa
e3f0: 69 6c 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ilure..*/.SQLITE
e400: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
e410: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64  _initialize(void
e420: 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
e430: 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  t sqlite3_shutdo
e440: 77 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  wn(void);.SQLITE
e450: 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
e460: 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a  _os_init(void);.
e470: 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
e480: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f  qlite3_os_end(vo
e490: 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  id);../*.** CAPI
e4a0: 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e  3REF: Configurin
e4b0: 67 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62  g The SQLite Lib
e4c0: 72 61 72 79 20 7b 48 31 34 31 30 30 7d 20 3c 53  rary {H14100} <S
e4d0: 32 30 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a 2a  20000><S30200>.*
e4e0: 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
e4f0: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
e500: 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66  _config() interf
e510: 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d  ace is used to m
e520: 61 6b 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69  ake global confi
e530: 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e  guration.** chan
e540: 67 65 73 20 74 6f 20 53 51 4c 69 74 65 20 69 6e  ges to SQLite in
e550: 20 6f 72 64 65 72 20 74 6f 20 74 75 6e 65 20 53   order to tune S
e560: 51 4c 69 74 65 20 74 6f 20 74 68 65 20 73 70 65  QLite to the spe
e570: 63 69 66 69 63 20 6e 65 65 64 73 20 6f 66 0a 2a  cific needs of.*
e580: 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * the applicatio
e590: 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  n.  The default 
e5a0: 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 73  configuration is
e5b0: 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72   recommended for
e5c0: 20 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 61   most.** applica
e5d0: 74 69 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 69  tions and so thi
e5e0: 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75  s routine is usu
e5f0: 61 6c 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 61  ally not necessa
e600: 72 79 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72  ry.  It is.** pr
e610: 6f 76 69 64 65 64 20 74 6f 20 73 75 70 70 6f 72  ovided to suppor
e620: 74 20 72 61 72 65 20 61 70 70 6c 69 63 61 74 69  t rare applicati
e630: 6f 6e 73 20 77 69 74 68 20 75 6e 75 73 75 61 6c  ons with unusual
e640: 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   needs..**.** Th
e650: 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  e sqlite3_config
e660: 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  () interface is 
e670: 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e 20  not threadsafe. 
e680: 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   The application
e690: 0a 2a 2a 20 6d 75 73 74 20 69 6e 73 75 72 65 20  .** must insure 
e6a0: 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 53 51  that no other SQ
e6b0: 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20  Lite interfaces 
e6c0: 61 72 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 6f  are invoked by o
e6d0: 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 20  ther.** threads 
e6e0: 77 68 69 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f  while sqlite3_co
e6f0: 6e 66 69 67 28 29 20 69 73 20 72 75 6e 6e 69 6e  nfig() is runnin
e700: 67 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c  g.  Furthermore,
e710: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
e720: 29 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65  ).** may only be
e730: 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74   invoked prior t
e740: 6f 20 6c 69 62 72 61 72 79 20 69 6e 69 74 69 61  o library initia
e750: 6c 69 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a  lization using.*
e760: 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  * [sqlite3_initi
e770: 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65  alize()] or afte
e780: 72 20 73 68 75 74 64 6f 77 6e 20 62 79 20 5b 73  r shutdown by [s
e790: 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28  qlite3_shutdown(
e7a0: 29 5d 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77  )]..** Note, how
e7b0: 65 76 65 72 2c 20 74 68 61 74 20 73 71 6c 69 74  ever, that sqlit
e7c0: 65 33 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e 20  e3_config() can 
e7d0: 62 65 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72  be called as par
e7e0: 74 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70 6c  t of the.** impl
e7f0: 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e  ementation of an
e800: 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
e810: 69 6e 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f 73  ined [sqlite3_os
e820: 5f 69 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  _init()]..**.** 
e830: 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
e840: 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  nt to sqlite3_co
e850: 6e 66 69 67 28 29 20 69 73 20 61 6e 20 69 6e 74  nfig() is an int
e860: 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  eger.** [SQLITE_
e870: 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52  CONFIG_SINGLETHR
e880: 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74  EAD | configurat
e890: 69 6f 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74  ion option] that
e8a0: 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77   determines.** w
e8b0: 68 61 74 20 70 72 6f 70 65 72 74 79 20 6f 66 20  hat property of 
e8c0: 53 51 4c 69 74 65 20 69 73 20 74 6f 20 62 65 20  SQLite is to be 
e8d0: 63 6f 6e 66 69 67 75 72 65 64 2e 20 20 53 75 62  configured.  Sub
e8e0: 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74  sequent argument
e8f0: 73 0a 2a 2a 20 76 61 72 79 20 64 65 70 65 6e 64  s.** vary depend
e900: 69 6e 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c 49  ing on the [SQLI
e910: 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45  TE_CONFIG_SINGLE
e920: 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75  THREAD | configu
e930: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a  ration option].*
e940: 2a 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61  * in the first a
e950: 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57  rgument..**.** W
e960: 68 65 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 74  hen a configurat
e970: 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 65  ion option is se
e980: 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  t, sqlite3_confi
e990: 67 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  g() returns [SQL
e9a0: 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74  ITE_OK]..** If t
e9b0: 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e 6b  he option is unk
e9c0: 6e 6f 77 6e 20 6f 72 20 53 51 4c 69 74 65 20 69  nown or SQLite i
e9d0: 73 20 75 6e 61 62 6c 65 20 74 6f 20 73 65 74 20  s unable to set 
e9e0: 74 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68  the option.** th
e9f0: 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
ea00: 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65  returns a non-ze
ea10: 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e  ro [error code].
ea20: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
ea30: 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 31 30 33 5d  nts:.** [H14103]
ea40: 20 5b 48 31 34 31 30 36 5d 20 5b 48 31 34 31 32   [H14106] [H1412
ea50: 30 5d 20 5b 48 31 34 31 32 33 5d 20 5b 48 31 34  0] [H14123] [H14
ea60: 31 32 36 5d 20 5b 48 31 34 31 32 39 5d 20 5b 48  126] [H14129] [H
ea70: 31 34 31 33 32 5d 20 5b 48 31 34 31 33 35 5d 0a  14132] [H14135].
ea80: 2a 2a 20 5b 48 31 34 31 33 38 5d 20 5b 48 31 34  ** [H14138] [H14
ea90: 31 34 31 5d 20 5b 48 31 34 31 34 34 5d 20 5b 48  141] [H14144] [H
eaa0: 31 34 31 34 37 5d 20 5b 48 31 34 31 35 30 5d 20  14147] [H14150] 
eab0: 5b 48 31 34 31 35 33 5d 20 5b 48 31 34 31 35 36  [H14153] [H14156
eac0: 5d 20 5b 48 31 34 31 35 39 5d 0a 2a 2a 20 5b 48  ] [H14159].** [H
ead0: 31 34 31 36 32 5d 20 5b 48 31 34 31 36 35 5d 20  14162] [H14165] 
eae0: 5b 48 31 34 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49  [H14168].*/.SQLI
eaf0: 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
eb00: 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73  PERIMENTAL int s
eb10: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e  qlite3_config(in
eb20: 74 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t, ...);../*.** 
eb30: 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67  CAPI3REF: Config
eb40: 75 72 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ure database con
eb50: 6e 65 63 74 69 6f 6e 73 20 20 7b 48 31 34 32 30  nections  {H1420
eb60: 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45  0} <S20000>.** E
eb70: 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
eb80: 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62  * The sqlite3_db
eb90: 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66  _config() interf
eba0: 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d  ace is used to m
ebb0: 61 6b 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  ake configuratio
ebc0: 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20  n.** changes to 
ebd0: 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  a [database conn
ebe0: 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 69 6e  ection].  The in
ebf0: 74 65 72 66 61 63 65 20 69 73 20 73 69 6d 69 6c  terface is simil
ec00: 61 72 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  ar to.** [sqlite
ec10: 33 5f 63 6f 6e 66 69 67 28 29 5d 20 65 78 63 65  3_config()] exce
ec20: 70 74 20 74 68 61 74 20 74 68 65 20 63 68 61 6e  pt that the chan
ec30: 67 65 73 20 61 70 70 6c 79 20 74 6f 20 61 20 73  ges apply to a s
ec40: 69 6e 67 6c 65 0a 2a 2a 20 5b 64 61 74 61 62 61  ingle.** [databa
ec50: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28  se connection] (
ec60: 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
ec70: 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 29   first argument)
ec80: 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65  .  The.** sqlite
ec90: 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e  3_db_config() in
eca0: 74 65 72 66 61 63 65 20 63 61 6e 20 6f 6e 6c 79  terface can only
ecb0: 20 62 65 20 75 73 65 64 20 69 6d 6d 65 64 69 61   be used immedia
ecc0: 74 65 6c 79 20 61 66 74 65 72 0a 2a 2a 20 74 68  tely after.** th
ecd0: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
ece0: 63 74 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64  ction is created
ecf0: 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
ed00: 6f 70 65 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  open()],.** [sql
ed10: 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20  ite3_open16()], 
ed20: 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  or [sqlite3_open
ed30: 5f 76 32 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20  _v2()].  .**.** 
ed40: 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
ed50: 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 64  ent to sqlite3_d
ed60: 62 5f 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e  b_config(D,V,...
ed70: 29 20 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6e  )  is the.** con
ed80: 66 69 67 75 72 61 74 69 6f 6e 20 76 65 72 62 20  figuration verb 
ed90: 2d 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64  - an integer cod
eda0: 65 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73  e that indicates
edb0: 20 77 68 61 74 0a 2a 2a 20 61 73 70 65 63 74 20   what.** aspect 
edc0: 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  of the [database
edd0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20   connection] is 
ede0: 62 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64  being configured
edf0: 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68  ..** The only ch
ee00: 6f 69 63 65 20 66 6f 72 20 74 68 69 73 20 76 61  oice for this va
ee10: 6c 75 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44  lue is [SQLITE_D
ee20: 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44  BCONFIG_LOOKASID
ee30: 45 5d 2e 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73  E]..** New verbs
ee40: 20 61 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62   are likely to b
ee50: 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72  e added in futur
ee60: 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51  e releases of SQ
ee70: 4c 69 74 65 2e 0a 2a 2a 20 41 64 64 69 74 69 6f  Lite..** Additio
ee80: 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 64 65  nal arguments de
ee90: 70 65 6e 64 20 6f 6e 20 74 68 65 20 76 65 72 62  pend on the verb
eea0: 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
eeb0: 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 34 32 30 33  ents:.** [H14203
eec0: 5d 20 5b 48 31 34 32 30 36 5d 20 5b 48 31 34 32  ] [H14206] [H142
eed0: 30 39 5d 20 5b 48 31 34 32 31 32 5d 20 5b 48 31  09] [H14212] [H1
eee0: 34 32 31 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  4215].*/.SQLITE_
eef0: 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52  API SQLITE_EXPER
ef00: 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69  IMENTAL int sqli
ef10: 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71  te3_db_config(sq
ef20: 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20  lite3*, int op, 
ef30: 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ...);../*.** CAP
ef40: 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c  I3REF: Memory Al
ef50: 6c 6f 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65  location Routine
ef60: 73 20 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31  s {H10155} <S201
ef70: 32 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  20>.** EXPERIMEN
ef80: 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73  TAL.**.** An ins
ef90: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
efa0: 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65  ject defines the
efb0: 20 69 6e 74 65 72 66 61 63 65 20 62 65 74 77 65   interface betwe
efc0: 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64  en SQLite.** and
efd0: 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72   low-level memor
efe0: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75  y allocation rou
eff0: 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  tines..**.** Thi
f000: 73 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64  s object is used
f010: 20 69 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61   in only one pla
f020: 63 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65  ce in the SQLite
f030: 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41   interface..** A
f040: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
f050: 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
f060: 6f 62 6a 65 63 74 20 69 73 20 74 68 65 20 61 72  object is the ar
f070: 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71  gument to.** [sq
f080: 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20  lite3_config()] 
f090: 77 68 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75  when the configu
f0a0: 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73  ration option is
f0b0: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  .** [SQLITE_CONF
f0c0: 49 47 5f 4d 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53  IG_MALLOC] or [S
f0d0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
f0e0: 4d 41 4c 4c 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79  MALLOC].  .** By
f0f0: 20 63 72 65 61 74 69 6e 67 20 61 6e 20 69 6e 73   creating an ins
f100: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
f110: 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73  ject.** and pass
f120: 69 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74  ing it to [sqlit
f130: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49  e3_config]([SQLI
f140: 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43  TE_CONFIG_MALLOC
f150: 5d 29 0a 2a 2a 20 64 75 72 69 6e 67 20 63 6f 6e  ]).** during con
f160: 66 69 67 75 72 61 74 69 6f 6e 2c 20 61 6e 20 61  figuration, an a
f170: 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 73  pplication can s
f180: 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e  pecify an altern
f190: 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20  ative.** memory 
f1a0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
f1b0: 73 74 65 6d 20 66 6f 72 20 53 51 4c 69 74 65 20  stem for SQLite 
f1c0: 74 6f 20 75 73 65 20 66 6f 72 20 61 6c 6c 20 6f  to use for all o
f1d0: 66 20 69 74 73 0a 2a 2a 20 64 79 6e 61 6d 69 63  f its.** dynamic
f1e0: 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 2e 0a 2a   memory needs..*
f1f0: 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 53  *.** Note that S
f200: 51 4c 69 74 65 20 63 6f 6d 65 73 20 77 69 74 68  QLite comes with
f210: 20 73 65 76 65 72 61 6c 20 5b 62 75 69 6c 74 2d   several [built-
f220: 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  in memory alloca
f230: 74 6f 72 73 5d 0a 2a 2a 20 74 68 61 74 20 61 72  tors].** that ar
f240: 65 20 70 65 72 66 65 63 74 6c 79 20 61 64 65 71  e perfectly adeq
f250: 75 61 74 65 20 66 6f 72 20 74 68 65 20 6f 76 65  uate for the ove
f260: 72 77 68 65 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69  rwhelming majori
f270: 74 79 20 6f 66 20 61 70 70 6c 69 63 61 74 69 6f  ty of applicatio
f280: 6e 73 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74  ns.** and that t
f290: 68 69 73 20 6f 62 6a 65 63 74 20 69 73 20 6f 6e  his object is on
f2a0: 6c 79 20 75 73 65 66 75 6c 20 74 6f 20 61 20 74  ly useful to a t
f2b0: 69 6e 79 20 6d 69 6e 6f 72 69 74 79 20 6f 66 20  iny minority of 
f2c0: 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20  applications.** 
f2d0: 77 69 74 68 20 73 70 65 63 69 61 6c 69 7a 65 64  with specialized
f2e0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
f2f0: 6f 6e 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2e  on requirements.
f300: 20 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73    This object is
f310: 0a 2a 2a 20 61 6c 73 6f 20 75 73 65 64 20 64 75  .** also used du
f320: 72 69 6e 67 20 74 65 73 74 69 6e 67 20 6f 66 20  ring testing of 
f330: 53 51 4c 69 74 65 20 69 6e 20 6f 72 64 65 72 20  SQLite in order 
f340: 74 6f 20 73 70 65 63 69 66 79 20 61 6e 20 61 6c  to specify an al
f350: 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 6d 65 6d  ternative.** mem
f360: 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68  ory allocator th
f370: 61 74 20 73 69 6d 75 6c 61 74 65 73 20 6d 65 6d  at simulates mem
f380: 6f 72 79 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72  ory out-of-memor
f390: 79 20 63 6f 6e 64 69 74 69 6f 6e 73 20 69 6e 0a  y conditions in.
f3a0: 2a 2a 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69  ** order to veri
f3b0: 66 79 20 74 68 61 74 20 53 51 4c 69 74 65 20 72  fy that SQLite r
f3c0: 65 63 6f 76 65 72 73 20 67 72 61 63 65 66 75 6c  ecovers graceful
f3d0: 6c 79 20 66 72 6f 6d 20 73 75 63 68 0a 2a 2a 20  ly from such.** 
f3e0: 63 6f 6e 64 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  conditions..**.*
f3f0: 2a 20 54 68 65 20 78 4d 61 6c 6c 6f 63 20 61 6e  * The xMalloc an
f400: 64 20 78 46 72 65 65 20 6d 65 74 68 6f 64 73 20  d xFree methods 
f410: 6d 75 73 74 20 77 6f 72 6b 20 6c 69 6b 65 20 74  must work like t
f420: 68 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 61  he.** malloc() a
f430: 6e 64 20 66 72 65 65 28 29 20 66 75 6e 63 74 69  nd free() functi
f440: 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ons from the sta
f450: 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e  ndard C library.
f460: 0a 2a 2a 20 54 68 65 20 78 52 65 61 6c 6c 6f 63  .** The xRealloc
f470: 20 6d 65 74 68 6f 64 20 6d 75 73 74 20 77 6f 72   method must wor
f480: 6b 20 6c 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29  k like realloc()
f490: 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61   from the standa
f4a0: 72 64 20 43 20 6c 69 62 72 61 72 79 0a 2a 2a 20  rd C library.** 
f4b0: 77 69 74 68 20 74 68 65 20 65 78 63 65 70 74 69  with the excepti
f4c0: 6f 6e 20 74 68 61 74 20 69 66 20 74 68 65 20 73  on that if the s
f4d0: 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
f4e0: 6f 20 78 52 65 61 6c 6c 6f 63 20 69 73 20 7a 65  o xRealloc is ze
f4f0: 72 6f 2c 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 20  ro,.** xRealloc 
f500: 6d 75 73 74 20 62 65 20 61 20 6e 6f 2d 6f 70 20  must be a no-op 
f510: 2d 20 69 74 20 6d 75 73 74 20 6e 6f 74 20 70 65  - it must not pe
f520: 72 66 6f 72 6d 20 61 6e 79 20 61 6c 6c 6f 63 61  rform any alloca
f530: 74 69 6f 6e 20 6f 72 0a 2a 2a 20 64 65 61 6c 6c  tion or.** deall
f540: 6f 63 61 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65  ocation.  SQLite
f550: 20 67 75 61 72 61 6e 74 65 65 64 73 20 74 68 61   guaranteeds tha
f560: 74 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  t the second arg
f570: 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 78 52 65 61  ument to.** xRea
f580: 6c 6c 6f 63 20 69 73 20 61 6c 77 61 79 73 20 61  lloc is always a
f590: 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
f5a0: 62 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20  by a prior call 
f5b0: 74 6f 20 78 52 6f 75 6e 64 75 70 2e 0a 2a 2a 20  to xRoundup..** 
f5c0: 41 6e 64 20 73 6f 20 69 6e 20 63 61 73 65 73 20  And so in cases 
f5d0: 77 68 65 72 65 20 78 52 6f 75 6e 64 75 70 20 61  where xRoundup a
f5e0: 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 61 20  lways returns a 
f5f0: 70 6f 73 69 74 69 76 65 20 6e 75 6d 62 65 72 2c  positive number,
f600: 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 63 61 6e  .** xRealloc can
f610: 20 70 65 72 66 6f 72 6d 20 65 78 61 63 74 6c 79   perform exactly
f620: 20 61 73 20 74 68 65 20 73 74 61 6e 64 61 72 64   as the standard
f630: 20 6c 69 62 72 61 72 79 20 72 65 61 6c 6c 6f 63   library realloc
f640: 28 29 20 61 6e 64 0a 2a 2a 20 73 74 69 6c 6c 20  () and.** still 
f650: 62 65 20 69 6e 20 63 6f 6d 70 6c 69 61 6e 63 65  be in compliance
f660: 20 77 69 74 68 20 74 68 69 73 20 73 70 65 63 69   with this speci
f670: 66 69 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  fication..**.** 
f680: 78 53 69 7a 65 20 73 68 6f 75 6c 64 20 72 65 74  xSize should ret
f690: 75 72 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 65  urn the allocate
f6a0: 64 20 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f  d size of a memo
f6b0: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  ry allocation.**
f6c0: 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61   previously obta
f6d0: 69 6e 65 64 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f  ined from xMallo
f6e0: 63 20 6f 72 20 78 52 65 61 6c 6c 6f 63 2e 20 20  c or xRealloc.  
f6f0: 54 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69  The allocated si
f700: 7a 65 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20  ze.** is always 
f710: 61 74 20 6c 65 61 73 74 20 61 73 20 62 69 67 20  at least as big 
f720: 61 73 20 74 68 65 20 72 65 71 75 65 73 74 65 64  as the requested
f730: 20 73 69 7a 65 20 62 75 74 20 6d 61 79 20 62 65   size but may be
f740: 20 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54   larger..**.** T
f750: 68 65 20 78 52 6f 75 6e 64 75 70 20 6d 65 74 68  he xRoundup meth
f760: 6f 64 20 72 65 74 75 72 6e 73 20 77 68 61 74 20  od returns what 
f770: 77 6f 75 6c 64 20 62 65 20 74 68 65 20 61 6c 6c  would be the all
f780: 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 0a 2a  ocated size of.*
f790: 2a 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  * a memory alloc
f7a0: 61 74 69 6f 6e 20 67 69 76 65 6e 20 61 20 70 61  ation given a pa
f7b0: 72 74 69 63 75 6c 61 72 20 72 65 71 75 65 73 74  rticular request
f7c0: 65 64 20 73 69 7a 65 2e 20 20 4d 6f 73 74 20 6d  ed size.  Most m
f7d0: 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
f7e0: 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 6d 65 6d  ors round up mem
f7f0: 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ory allocations 
f800: 61 74 20 6c 65 61 73 74 20 74 6f 20 74 68 65 20  at least to the 
f810: 6e 65 78 74 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  next multiple.**
f820: 20 6f 66 20 38 2e 20 20 53 6f 6d 65 20 61 6c 6c   of 8.  Some all
f830: 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 70  ocators round up
f840: 20 74 6f 20 61 20 6c 61 72 67 65 72 20 6d 75 6c   to a larger mul
f850: 74 69 70 6c 65 20 6f 72 20 74 6f 20 61 20 70 6f  tiple or to a po
f860: 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 20 45 76 65  wer of 2..** Eve
f870: 72 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ry memory alloca
f880: 74 69 6f 6e 20 72 65 71 75 65 73 74 20 63 6f 6d  tion request com
f890: 69 6e 67 20 69 6e 20 74 68 72 6f 75 67 68 20 5b  ing in through [
f8a0: 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
f8b0: 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33  ].** or [sqlite3
f8c0: 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 66 69 72 73  _realloc()] firs
f8d0: 74 20 63 61 6c 6c 73 20 78 52 6f 75 6e 64 75 70  t calls xRoundup
f8e0: 2e 20 20 49 66 20 78 52 6f 75 6e 64 75 70 20 72  .  If xRoundup r
f8f0: 65 74 75 72 6e 73 20 30 2c 20 0a 2a 2a 20 74 68  eturns 0, .** th
f900: 61 74 20 63 61 75 73 65 73 20 74 68 65 20 63 6f  at causes the co
f910: 72 72 65 73 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f  rresponding memo
f920: 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f  ry allocation to
f930: 20 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   fail..**.** The
f940: 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 69 6e   xInit method in
f950: 69 74 69 61 6c 69 7a 65 73 20 74 68 65 20 6d 65  itializes the me
f960: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20  mory allocator. 
f970: 20 28 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a   (For example,.*
f980: 2a 20 69 74 20 6d 69 67 68 74 20 61 6c 6c 6f 63  * it might alloc
f990: 61 74 65 20 61 6e 79 20 72 65 71 75 69 72 65 20  ate any require 
f9a0: 6d 75 74 65 78 65 73 20 6f 72 20 69 6e 69 74 69  mutexes or initi
f9b0: 61 6c 69 7a 65 20 69 6e 74 65 72 6e 61 6c 20 64  alize internal d
f9c0: 61 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  ata.** structure
f9d0: 73 2e 20 20 54 68 65 20 78 53 68 75 74 64 6f 77  s.  The xShutdow
f9e0: 6e 20 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f  n method is invo
f9f0: 6b 65 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29  ked (indirectly)
fa00: 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   by.** [sqlite3_
fa10: 73 68 75 74 64 6f 77 6e 28 29 5d 20 61 6e 64 20  shutdown()] and 
fa20: 73 68 6f 75 6c 64 20 64 65 61 6c 6c 6f 63 61 74  should deallocat
fa30: 65 20 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20  e any resources 
fa40: 61 63 71 75 69 72 65 64 0a 2a 2a 20 62 79 20 78  acquired.** by x
fa50: 49 6e 69 74 2e 20 20 54 68 65 20 70 41 70 70 44  Init.  The pAppD
fa60: 61 74 61 20 70 6f 69 6e 74 65 72 20 69 73 20 75  ata pointer is u
fa70: 73 65 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20  sed as the only 
fa80: 70 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20  parameter to.** 
fa90: 78 49 6e 69 74 20 61 6e 64 20 78 53 68 75 74 64  xInit and xShutd
faa0: 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  own..**.** SQLit
fab0: 65 20 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c  e holds the [SQL
fac0: 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
fad0: 5f 4d 41 53 54 45 52 5d 20 6d 75 74 65 78 20 77  _MASTER] mutex w
fae0: 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 0a 2a  hen it invokes.*
faf0: 2a 20 74 68 65 20 78 49 6e 69 74 20 6d 65 74 68  * the xInit meth
fb00: 6f 64 2c 20 73 6f 20 74 68 65 20 78 49 6e 69 74  od, so the xInit
fb10: 20 6d 65 74 68 6f 64 20 6e 65 65 64 20 6e 6f 74   method need not
fb20: 20 62 65 20 74 68 72 65 61 64 73 61 66 65 2e 20   be threadsafe. 
fb30: 20 54 68 65 0a 2a 2a 20 78 53 68 75 74 64 6f 77   The.** xShutdow
fb40: 6e 20 6d 65 74 68 6f 64 20 69 73 20 6f 6e 6c 79  n method is only
fb50: 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 5b 73 71   called from [sq
fb60: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
fb70: 5d 20 73 6f 20 69 74 20 64 6f 65 73 0a 2a 2a 20  ] so it does.** 
fb80: 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 74  not need to be t
fb90: 68 72 65 61 64 73 61 66 65 20 65 69 74 68 65 72  hreadsafe either
fba0: 2e 20 20 46 6f 72 20 61 6c 6c 20 6f 74 68 65 72  .  For all other
fbb0: 20 6d 65 74 68 6f 64 73 2c 20 53 51 4c 69 74 65   methods, SQLite
fbc0: 0a 2a 2a 20 68 6f 6c 64 73 20 74 68 65 20 5b 53  .** holds the [S
fbd0: 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
fbe0: 49 43 5f 4d 45 4d 5d 20 6d 75 74 65 78 20 61 73  IC_MEM] mutex as
fbf0: 20 6c 6f 6e 67 20 61 73 20 74 68 65 0a 2a 2a 20   long as the.** 
fc00: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
fc10: 45 4d 53 54 41 54 55 53 5d 20 63 6f 6e 66 69 67  EMSTATUS] config
fc20: 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69  uration option i
fc30: 73 20 74 75 72 6e 65 64 20 6f 6e 20 28 77 68 69  s turned on (whi
fc40: 63 68 0a 2a 2a 20 69 74 20 69 73 20 62 79 20 64  ch.** it is by d
fc50: 65 66 61 75 6c 74 29 20 61 6e 64 20 73 6f 20 74  efault) and so t
fc60: 68 65 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61  he methods are a
fc70: 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 72  utomatically ser
fc80: 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65  ialized..** Howe
fc90: 76 65 72 2c 20 69 66 20 5b 53 51 4c 49 54 45 5f  ver, if [SQLITE_
fca0: 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53  CONFIG_MEMSTATUS
fcb0: 5d 20 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74  ] is disabled, t
fcc0: 68 65 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a  hen the other.**
fcd0: 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65   methods must be
fce0: 20 74 68 72 65 61 64 73 61 66 65 20 6f 72 20 65   threadsafe or e
fcf0: 6c 73 65 20 6d 61 6b 65 20 74 68 65 69 72 20 6f  lse make their o
fd00: 77 6e 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20  wn arrangements 
fd10: 66 6f 72 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 61  for.** serializa
fd20: 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  tion..**.** SQLi
fd30: 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e  te will never in
fd40: 76 6f 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72  voke xInit() mor
fd50: 65 20 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68  e than once with
fd60: 6f 75 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69  out an interveni
fd70: 6e 67 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53  ng.** call to xS
fd80: 68 75 74 64 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79  hutdown()..*/.ty
fd90: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
fda0: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
fdb0: 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
fdc0: 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c  hods;.struct sql
fdd0: 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
fde0: 20 7b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 4d 61   {.  void *(*xMa
fdf0: 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 20 20 20 20  lloc)(int);     
fe00: 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c      /* Memory al
fe10: 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  location functio
fe20: 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46  n */.  void (*xF
fe30: 72 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20  ree)(void*);    
fe40: 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 61 20        /* Free a 
fe50: 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  prior allocation
fe60: 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52   */.  void *(*xR
fe70: 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e  ealloc)(void*,in
fe80: 74 29 3b 20 20 2f 2a 20 52 65 73 69 7a 65 20 61  t);  /* Resize a
fe90: 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a  n allocation */.
fea0: 20 20 69 6e 74 20 28 2a 78 53 69 7a 65 29 28 76    int (*xSize)(v
feb0: 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20  oid*);          
fec0: 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73   /* Return the s
fed0: 69 7a 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61  ize of an alloca
fee0: 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  tion */.  int (*
fef0: 78 52 6f 75 6e 64 75 70 29 28 69 6e 74 29 3b 20  xRoundup)(int); 
ff00: 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e           /* Roun
ff10: 64 20 75 70 20 72 65 71 75 65 73 74 20 73 69 7a  d up request siz
ff20: 65 20 74 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e to allocation 
ff30: 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  size */.  int (*
ff40: 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 20 20  xInit)(void*);  
ff50: 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74           /* Init
ff60: 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
ff70: 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20  y allocator */. 
ff80: 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77   void (*xShutdow
ff90: 6e 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20  n)(void*);      
ffa0: 2f 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20  /* Deinitialize 
ffb0: 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
ffc0: 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ator */.  void *
ffd0: 70 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20  pAppData;       
ffe0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
fff0: 6d 65 6e 74 20 74 6f 20 78 49 6e 69 74 28 29 20  ment to xInit() 
10000 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 28 29 20  and xShutdown() 
10010 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.};../*.** CAP
10020 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61  I3REF: Configura
10030 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31  tion Options {H1
10040 30 31 36 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a  0160} <S20000>.*
10050 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
10060 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74  *.** These const
10070 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 61  ants are the ava
10080 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63  ilable integer c
10090 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
100a0 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e  ions that.** can
100b0 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68   be passed as th
100c0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
100d0 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
100e0 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72  _config()] inter
100f0 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20  face..**.** New 
10100 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
10110 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64  tions may be add
10120 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c  ed in future rel
10130 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e  eases of SQLite.
10140 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e  .** Existing con
10150 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
10160 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 63  ns might be disc
10170 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69  ontinued.  Appli
10180 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c  cations.** shoul
10190 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75  d check the retu
101a0 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71  rn code from [sq
101b0 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20  lite3_config()] 
101c0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
101d0 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f  t.** the call wo
101e0 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69  rked.  The [sqli
101f0 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e  te3_config()] in
10200 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74  terface will ret
10210 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72  urn a.** non-zer
10220 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  o [error code] i
10230 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64  f a discontinued
10240 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20   or unsupported 
10250 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
10260 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b  tion.** is invok
10270 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a  ed..**.** <dl>.*
10280 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
10290 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
102a0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
102b0 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65  re are no argume
102c0 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69  nts to this opti
102d0 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e  on.  This option
102e0 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c   disables.** all
102f0 20 6d 75 74 65 78 69 6e 67 20 61 6e 64 20 70 75   mutexing and pu
10300 74 73 20 53 51 4c 69 74 65 20 69 6e 74 6f 20 61  ts SQLite into a
10310 20 6d 6f 64 65 20 77 68 65 72 65 20 69 74 20 63   mode where it c
10320 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 0a  an only be used.
10330 2a 2a 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74  ** by a single t
10340 68 72 65 61 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  hread.</dd>.**.*
10350 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
10360 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 3c  FIG_MULTITHREAD<
10370 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72  /dt>.** <dd>Ther
10380 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e  e are no argumen
10390 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f  ts to this optio
103a0 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20  n.  This option 
103b0 64 69 73 61 62 6c 65 73 0a 2a 2a 20 6d 75 74 65  disables.** mute
103c0 78 69 6e 67 20 6f 6e 20 5b 64 61 74 61 62 61 73  xing on [databas
103d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e  e connection] an
103e0 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  d [prepared stat
103f0 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a  ement] objects..
10400 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69  ** The applicati
10410 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  on is responsibl
10420 65 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e  e for serializin
10430 67 20 61 63 63 65 73 73 20 74 6f 0a 2a 2a 20 5b  g access to.** [
10440 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
10450 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61  ions] and [prepa
10460 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 2e  red statements].
10470 20 20 42 75 74 20 6f 74 68 65 72 20 6d 75 74 65    But other mute
10480 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c  xes.** are enabl
10490 65 64 20 73 6f 20 74 68 61 74 20 53 51 4c 69 74  ed so that SQLit
104a0 65 20 77 69 6c 6c 20 62 65 20 73 61 66 65 20 74  e will be safe t
104b0 6f 20 75 73 65 20 69 6e 20 61 20 6d 75 6c 74 69  o use in a multi
104c0 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 65 6e 76  -threaded.** env
104d0 69 72 6f 6e 6d 65 6e 74 20 61 73 20 6c 6f 6e 67  ironment as long
104e0 20 61 73 20 6e 6f 20 74 77 6f 20 74 68 72 65 61   as no two threa
104f0 64 73 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ds attempt to us
10500 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 5b 64  e the same.** [d
10510 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
10520 6f 6e 5d 20 61 74 20 74 68 65 20 73 61 6d 65 20  on] at the same 
10530 74 69 6d 65 2e 20 20 53 65 65 20 74 68 65 20 5b  time.  See the [
10540 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 0a  threading mode].
10550 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  ** documentation
10560 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
10570 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64  information.</dd
10580 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
10590 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c  TE_CONFIG_SERIAL
105a0 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  IZED</dt>.** <dd
105b0 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72  >There are no ar
105c0 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20  guments to this 
105d0 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70  option.  This op
105e0 74 69 6f 6e 20 65 6e 61 62 6c 65 73 0a 2a 2a 20  tion enables.** 
105f0 61 6c 6c 20 6d 75 74 65 78 65 73 20 69 6e 63 6c  all mutexes incl
10600 75 64 69 6e 67 20 74 68 65 20 72 65 63 75 72 73  uding the recurs
10610 69 76 65 0a 2a 2a 20 6d 75 74 65 78 65 73 20 6f  ive.** mutexes o
10620 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  n [database conn
10630 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65  ection] and [pre
10640 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
10650 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 49 6e 20   objects..** In 
10660 74 68 69 73 20 6d 6f 64 65 20 28 77 68 69 63 68  this mode (which
10670 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20   is the default 
10680 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63  when SQLite is c
10690 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20  ompiled with.** 
106a0 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
106b0 46 45 3d 31 5d 29 20 74 68 65 20 53 51 4c 69 74  FE=1]) the SQLit
106c0 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 69  e library will i
106d0 74 73 65 6c 66 20 73 65 72 69 61 6c 69 7a 65 20  tself serialize 
106e0 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 5b 64 61  access.** to [da
106f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
10700 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65  ns] and [prepare
10710 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 73 6f  d statements] so
10720 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 70 70   that the.** app
10730 6c 69 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  lication is free
10740 20 74 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65   to use the same
10750 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
10760 63 74 69 6f 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a  ction] or the.**
10770 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20   same [prepared 
10780 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 64 69  statement] in di
10790 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 73 20  fferent threads 
107a0 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
107b0 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68  ..** See the [th
107c0 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f  reading mode] do
107d0 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  cumentation for 
107e0 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
107f0 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a  mation.</dd>.**.
10800 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
10810 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e  NFIG_MALLOC</dt>
10820 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
10830 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
10840 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
10850 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
10860 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  o an.** instance
10870 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
10880 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74  _mem_methods] st
10890 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 61 72  ructure.  The ar
108a0 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73  gument specifies
108b0 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20  .** alternative 
108c0 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
108d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74   allocation rout
108e0 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 20  ines to be used 
108f0 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 74  in place of.** t
10900 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
10910 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 62 75  tion routines bu
10920 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e  ilt into SQLite.
10930 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
10940 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
10950 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20  TMALLOC</dt>.** 
10960 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
10970 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
10980 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73  rgument which is
10990 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
109a0 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
109b0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d  the [sqlite3_mem
109c0 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74  _methods] struct
109d0 75 72 65 2e 20 20 54 68 65 20 5b 73 71 6c 69 74  ure.  The [sqlit
109e0 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a  e3_mem_methods].
109f0 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ** structure is 
10a00 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20  filled with the 
10a10 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65  currently define
10a20 64 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  d memory allocat
10a30 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  ion routines..**
10a40 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e   This option can
10a50 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72   be used to over
10a60 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c 74  load the default
10a70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
10a80 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77  on.** routines w
10a90 69 74 68 20 61 20 77 72 61 70 70 65 72 20 74 68  ith a wrapper th
10aa0 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e 73 20 6d  at simulations m
10ab0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
10ac0 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a 2a 20 74   failure or.** t
10ad0 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20 75 73 61  racks memory usa
10ae0 67 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e  ge, for example.
10af0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
10b00 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45  SQLITE_CONFIG_ME
10b10 4d 53 54 41 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20  MSTATUS</dt>.** 
10b20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
10b30 74 61 6b 65 73 20 73 69 6e 67 6c 65 20 61 72 67  takes single arg
10b40 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e  ument of type in
10b50 74 2c 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  t, interpreted a
10b60 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c  s a .** boolean,
10b70 20 77 68 69 63 68 20 65 6e 61 62 6c 65 73 20 6f   which enables o
10b80 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63  r disables the c
10b90 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 6d 65 6d  ollection of mem
10ba0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a  ory allocation .
10bb0 2a 2a 20 73 74 61 74 69 73 74 69 63 73 2e 20 57  ** statistics. W
10bc0 68 65 6e 20 64 69 73 61 62 6c 65 64 2c 20 74 68  hen disabled, th
10bd0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 69  e following SQLi
10be0 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 62 65  te interfaces be
10bf0 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65  come .** non-ope
10c00 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c  rational:.**   <
10c10 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73  ul>.**   <li> [s
10c20 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
10c30 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20  ed()].**   <li> 
10c40 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f  [sqlite3_memory_
10c50 68 69 67 68 77 61 74 65 72 28 29 5d 0a 2a 2a 20  highwater()].** 
10c60 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f    <li> [sqlite3_
10c70 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28  soft_heap_limit(
10c80 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71  )].**   <li> [sq
10c90 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 0a  lite3_status()].
10ca0 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f  **   </ul>.** </
10cb0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
10cc0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41  LITE_CONFIG_SCRA
10cd0 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TCH</dt>.** <dd>
10ce0 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63  This option spec
10cf0 69 66 69 65 73 20 61 20 73 74 61 74 69 63 20 6d  ifies a static m
10d00 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 61  emory buffer tha
10d10 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65  t SQLite can use
10d20 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74 63 68 20   for.** scratch 
10d30 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72 65 20 61  memory.  There a
10d40 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e  re three argumen
10d50 74 73 3a 20 20 41 20 70 6f 69 6e 74 65 72 20 61  ts:  A pointer a
10d60 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 61 6c 69 67  n 8-byte.** alig
10d70 6e 65 64 20 6d 65 6d 6f 72 79 20 62 75 66 66 65  ned memory buffe
10d80 72 20 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65  r from which the
10d90 20 73 63 72 61 63 68 20 61 6c 6c 6f 63 61 74 69   scrach allocati
10da0 6f 6e 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64  ons will be.** d
10db0 72 61 77 6e 2c 20 74 68 65 20 73 69 7a 65 20 6f  rawn, the size o
10dc0 66 20 65 61 63 68 20 73 63 72 61 74 63 68 20 61  f each scratch a
10dd0 6c 6c 6f 63 61 74 69 6f 6e 20 28 73 7a 29 2c 0a  llocation (sz),.
10de0 2a 2a 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d  ** and the maxim
10df0 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 73 63 72  um number of scr
10e00 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  atch allocations
10e10 20 28 4e 29 2e 20 20 54 68 65 20 73 7a 0a 2a 2a   (N).  The sz.**
10e20 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62   argument must b
10e30 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20  e a multiple of 
10e40 31 36 2e 20 54 68 65 20 73 7a 20 70 61 72 61 6d  16. The sz param
10e50 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 61  eter should be a
10e60 20 66 65 77 20 62 79 74 65 73 0a 2a 2a 20 6c 61   few bytes.** la
10e70 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 61 63  rger than the ac
10e80 74 75 61 6c 20 73 63 72 61 74 63 68 20 73 70 61  tual scratch spa
10e90 63 65 20 72 65 71 75 69 72 65 64 20 64 75 65 20  ce required due 
10ea0 74 6f 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72  to internal over
10eb0 68 65 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72  head..** The fir
10ec0 73 74 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75  st argument shou
10ed0 6c 64 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e  ld pointer to an
10ee0 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20   8-byte aligned 
10ef0 62 75 66 66 65 72 0a 2a 2a 20 6f 66 20 61 74 20  buffer.** of at 
10f00 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79 74 65 73  least sz*N bytes
10f10 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53   of memory..** S
10f20 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 6e  QLite will use n
10f30 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  o more than one 
10f40 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 61  scratch buffer a
10f50 74 20 6f 6e 63 65 20 70 65 72 20 74 68 72 65 61  t once per threa
10f60 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c  d, so.** N shoul
10f70 64 20 62 65 20 73 65 74 20 74 6f 20 74 68 65 20  d be set to the 
10f80 65 78 70 65 63 74 65 64 20 6d 61 78 69 6d 75 6d  expected maximum
10f90 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61   number of threa
10fa0 64 73 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 70  ds.  The sz.** p
10fb0 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20  arameter should 
10fc0 62 65 20 36 20 74 69 6d 65 73 20 74 68 65 20 73  be 6 times the s
10fd0 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65  ize of the large
10fe0 73 74 20 64 61 74 61 62 61 73 65 20 70 61 67 65  st database page
10ff0 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 72 61 74 63   size..** Scratc
11000 68 20 62 75 66 66 65 72 73 20 61 72 65 20 75 73  h buffers are us
11010 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
11020 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 65 20  e btree balance 
11030 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 0a 2a  operation.  If.*
11040 2a 20 54 68 65 20 62 74 72 65 65 20 62 61 6c 61  * The btree bala
11050 6e 63 65 72 20 6e 65 65 64 73 20 61 64 64 69 74  ncer needs addit
11060 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 62 65 79  ional memory bey
11070 6f 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76  ond what is prov
11080 69 64 65 64 20 62 79 0a 2a 2a 20 73 63 72 61 74  ided by.** scrat
11090 63 68 20 62 75 66 66 65 72 73 20 6f 72 20 69 66  ch buffers or if
110a0 20 6e 6f 20 73 63 72 61 74 63 68 20 62 75 66 66   no scratch buff
110b0 65 72 20 73 70 61 63 65 20 69 73 20 73 70 65 63  er space is spec
110c0 69 66 69 65 64 2c 20 74 68 65 6e 20 53 51 4c 69  ified, then SQLi
110d0 74 65 0a 2a 2a 20 67 6f 65 73 20 74 6f 20 5b 73  te.** goes to [s
110e0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
110f0 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d   to obtain the m
11100 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 3c  emory it needs.<
11110 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
11120 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
11130 45 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c  ECACHE</dt>.** <
11140 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73  dd>This option s
11150 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69  pecifies a stati
11160 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  c memory buffer 
11170 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20  that SQLite can 
11180 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 64  use for.** the d
11190 61 74 61 62 61 73 65 20 70 61 67 65 20 63 61 63  atabase page cac
111a0 68 65 20 77 69 74 68 20 74 68 65 20 64 65 66 61  he with the defa
111b0 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 20 69  ult page cache i
111c0 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a  mplemenation.  .
111d0 2a 2a 20 54 68 69 73 20 63 6f 6e 66 69 67 75 72  ** This configur
111e0 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74  ation should not
111f0 20 62 65 20 75 73 65 64 20 69 66 20 61 6e 20 61   be used if an a
11200 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
11210 65 20 70 61 67 65 0a 2a 2a 20 63 61 63 68 65 20  e page.** cache 
11220 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
11230 73 20 6c 6f 61 64 65 64 20 75 73 69 6e 67 20 74  s loaded using t
11240 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  he SQLITE_CONFIG
11250 5f 50 43 41 43 48 45 20 6f 70 74 69 6f 6e 2e 0a  _PCACHE option..
11260 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72  ** There are thr
11270 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ee arguments to 
11280 74 68 69 73 20 6f 70 74 69 6f 6e 3a 20 41 20 70  this option: A p
11290 6f 69 6e 74 65 72 20 74 6f 20 38 2d 62 79 74 65  ointer to 8-byte
112a0 20 61 6c 69 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f   aligned.** memo
112b0 72 79 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ry, the size of 
112c0 65 61 63 68 20 70 61 67 65 20 62 75 66 66 65 72  each page buffer
112d0 20 28 73 7a 29 2c 20 61 6e 64 20 74 68 65 20 6e   (sz), and the n
112e0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 28  umber of pages (
112f0 4e 29 2e 0a 2a 2a 20 54 68 65 20 73 7a 20 61 72  N)..** The sz ar
11300 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64 20 62 65  gument should be
11310 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
11320 20 6c 61 72 67 65 73 74 20 64 61 74 61 62 61 73   largest databas
11330 65 20 70 61 67 65 0a 2a 2a 20 28 61 20 70 6f 77  e page.** (a pow
11340 65 72 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65  er of two betwee
11350 6e 20 35 31 32 20 61 6e 64 20 33 32 37 36 38 29  n 512 and 32768)
11360 20 70 6c 75 73 20 61 20 6c 69 74 74 6c 65 20 65   plus a little e
11370 78 74 72 61 20 66 6f 72 20 65 61 63 68 0a 2a 2a  xtra for each.**
11380 20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 54   page header.  T
11390 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 73  he page header s
113a0 69 7a 65 20 69 73 20 32 30 20 74 6f 20 34 30 20  ize is 20 to 40 
113b0 62 79 74 65 73 20 64 65 70 65 6e 64 69 6e 67 20  bytes depending 
113c0 6f 6e 0a 2a 2a 20 74 68 65 20 68 6f 73 74 20 61  on.** the host a
113d0 72 63 68 69 74 65 63 74 75 72 65 2e 20 20 49 74  rchitecture.  It
113e0 20 69 73 20 68 61 72 6d 6c 65 73 73 2c 20 61 70   is harmless, ap
113f0 61 72 74 20 66 72 6f 6d 20 74 68 65 20 77 61 73  art from the was
11400 74 65 64 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74  ted memory,.** t
11410 6f 20 6d 61 6b 65 20 73 7a 20 61 20 6c 69 74 74  o make sz a litt
11420 6c 65 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 54  le too large.  T
11430 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75  he first.** argu
11440 6d 65 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e  ment should poin
11450 74 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69  t to an allocati
11460 6f 6e 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73  on of at least s
11470 7a 2a 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  z*N bytes of mem
11480 6f 72 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77  ory..** SQLite w
11490 69 6c 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f  ill use the memo
114a0 72 79 20 70 72 6f 76 69 64 65 64 20 62 79 20 74  ry provided by t
114b0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
114c0 74 20 74 6f 20 73 61 74 69 73 66 79 20 69 74 73  t to satisfy its
114d0 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73  .** memory needs
114e0 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e   for the first N
114f0 20 70 61 67 65 73 20 74 68 61 74 20 69 74 20 61   pages that it a
11500 64 64 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49  dds to cache.  I
11510 66 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  f additional.** 
11520 70 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72  page cache memor
11530 79 20 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f  y is needed beyo
11540 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69  nd what is provi
11550 64 65 64 20 62 79 20 74 68 69 73 20 6f 70 74 69  ded by this opti
11560 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69  on, then.** SQLi
11570 74 65 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69  te goes to [sqli
11580 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f  te3_malloc()] fo
11590 72 20 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c  r the additional
115a0 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a   storage space..
115b0 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ** The implement
115c0 61 74 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20  ation might use 
115d0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74  one or more of t
115e0 68 65 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20  he N buffers to 
115f0 68 6f 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20  hold .** memory 
11600 61 63 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72  accounting infor
11610 6d 61 74 69 6f 6e 2e 20 54 68 65 20 70 6f 69 6e  mation. The poin
11620 74 65 72 20 69 6e 20 74 68 65 20 66 69 72 73 74  ter in the first
11630 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 0a 2a   argument must.*
11640 2a 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20  * be aligned to 
11650 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61  an 8-byte bounda
11660 72 79 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74  ry or subsequent
11670 20 62 65 68 61 76 69 6f 72 20 6f 66 20 53 51 4c   behavior of SQL
11680 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 75  ite.** will be u
11690 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a  ndefined.</dd>.*
116a0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
116b0 43 4f 4e 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e  CONFIG_HEAP</dt>
116c0 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
116d0 69 6f 6e 20 73 70 65 63 69 66 69 65 73 20 61 20  ion specifies a 
116e0 73 74 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75  static memory bu
116f0 66 66 65 72 20 74 68 61 74 20 53 51 4c 69 74 65  ffer that SQLite
11700 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72   will use.** for
11710 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61   all of its dyna
11720 6d 69 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  mic memory alloc
11730 61 74 69 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f  ation needs beyo
11740 6e 64 20 74 68 6f 73 65 20 70 72 6f 76 69 64 65  nd those provide
11750 64 0a 2a 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c  d.** for by [SQL
11760 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54  ITE_CONFIG_SCRAT
11770 43 48 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f  CH] and [SQLITE_
11780 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
11790 5d 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  ]..** There are 
117a0 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a  three arguments:
117b0 20 41 6e 20 38 2d 62 79 74 65 20 61 6c 69 67 6e   An 8-byte align
117c0 65 64 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68  ed pointer to th
117d0 65 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 68 65  e memory,.** the
117e0 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
117f0 20 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 62   in the memory b
11800 75 66 66 65 72 2c 20 61 6e 64 20 74 68 65 20 6d  uffer, and the m
11810 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f  inimum allocatio
11820 6e 20 73 69 7a 65 2e 0a 2a 2a 20 49 66 20 74 68  n size..** If th
11830 65 20 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20  e first pointer 
11840 28 74 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e  (the memory poin
11850 74 65 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68  ter) is NULL, th
11860 65 6e 20 53 51 4c 69 74 65 20 72 65 76 65 72 74  en SQLite revert
11870 73 0a 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74  s.** to using it
11880 73 20 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79  s default memory
11890 20 61 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20   allocator (the 
118a0 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20  system malloc() 
118b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c  implementation),
118c0 0a 2a 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20  .** undoing any 
118d0 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e  prior invocation
118e0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46   of [SQLITE_CONF
118f0 49 47 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20  IG_MALLOC].  If 
11900 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f  the.** memory po
11910 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c  inter is not NUL
11920 4c 20 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51  L and either [SQ
11930 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
11940 59 53 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49  YS3] or.** [SQLI
11950 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
11960 35 5d 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20  5] are defined, 
11970 74 68 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61  then the alterna
11980 74 69 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  tive memory.** a
11990 6c 6c 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61  llocator is enga
119a0 67 65 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c  ged to handle al
119b0 6c 20 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d  l of SQLites mem
119c0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e  ory allocation n
119d0 65 65 64 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72  eeds..** The fir
119e0 73 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65 20  st pointer (the 
119f0 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 20  memory pointer) 
11a00 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20  must be aligned 
11a10 74 6f 20 61 6e 20 38 2d 62 79 74 65 0a 2a 2a 20  to an 8-byte.** 
11a20 62 6f 75 6e 64 61 72 79 20 6f 72 20 73 75 62 73  boundary or subs
11a30 65 71 75 65 6e 74 20 62 65 68 61 76 69 6f 72 20  equent behavior 
11a40 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 62  of SQLite will b
11a50 65 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64  e undefined.</dd
11a60 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
11a70 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3c  TE_CONFIG_MUTEX<
11a80 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
11a90 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
11aa0 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
11ab0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
11ac0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74  er to an.** inst
11ad0 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c  ance of the [sql
11ae0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
11af0 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20  ds] structure.  
11b00 54 68 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65  The argument spe
11b10 63 69 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e  cifies.** altern
11b20 61 74 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20  ative low-level 
11b30 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 74  mutex routines t
11b40 6f 20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61  o be used in pla
11b50 63 65 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20  ce.** the mutex 
11b60 72 6f 75 74 69 6e 65 73 20 62 75 69 6c 74 20 69  routines built i
11b70 6e 74 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e  nto SQLite.</dd>
11b80 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  .**.** <dt>SQLIT
11b90 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45  E_CONFIG_GETMUTE
11ba0 58 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  X</dt>.** <dd>Th
11bb0 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20  is option takes 
11bc0 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e  a single argumen
11bd0 74 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69  t which is a poi
11be0 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e  nter to an.** in
11bf0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73  stance of the [s
11c00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
11c10 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e  hods] structure.
11c20 20 20 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65    The.** [sqlite
11c30 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d  3_mutex_methods]
11c40 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73  .** structure is
11c50 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65   filled with the
11c60 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e   currently defin
11c70 65 64 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65  ed mutex routine
11c80 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f  s..** This optio
11c90 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  n can be used to
11ca0 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65   overload the de
11cb0 66 61 75 6c 74 20 6d 75 74 65 78 20 61 6c 6c 6f  fault mutex allo
11cc0 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e  cation.** routin
11cd0 65 73 20 77 69 74 68 20 61 20 77 72 61 70 70 65  es with a wrappe
11ce0 72 20 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20  r used to track 
11cf0 6d 75 74 65 78 20 75 73 61 67 65 20 66 6f 72 20  mutex usage for 
11d00 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70  performance.** p
11d10 72 6f 66 69 6c 69 6e 67 20 6f 72 20 74 65 73 74  rofiling or test
11d20 69 6e 67 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  ing, for example
11d30 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
11d40 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c  >SQLITE_CONFIG_L
11d50 4f 4f 4b 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a  OOKASIDE</dt>.**
11d60 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e   <dd>This option
11d70 20 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d   takes two argum
11d80 65 6e 74 73 20 74 68 61 74 20 64 65 74 65 72 6d  ents that determ
11d90 69 6e 65 20 74 68 65 20 64 65 66 61 75 6c 74 0a  ine the default.
11da0 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** memory alloca
11db0 74 69 6f 6e 20 6c 6f 6f 6b 61 73 69 64 65 20 6f  tion lookaside o
11dc0 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68  ptimization.  Th
11dd0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
11de0 20 69 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20   is the.** size 
11df0 6f 66 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64  of each lookasid
11e00 65 20 62 75 66 66 65 72 20 73 6c 6f 74 20 61 6e  e buffer slot an
11e10 64 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20  d the second is 
11e20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
11e30 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64   slots allocated
11e40 20 74 6f 20 65 61 63 68 20 64 61 74 61 62 61 73   to each databas
11e50 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54  e connection.  T
11e60 68 69 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20  his option sets 
11e70 74 68 65 0a 2a 2a 20 3c 69 3e 64 65 66 61 75 6c  the.** <i>defaul
11e80 74 3c 2f 69 3e 20 6c 6f 6f 6b 61 73 69 64 65 20  t</i> lookaside 
11e90 73 69 7a 65 2e 20 20 54 68 65 20 5b 53 51 4c 49  size.  The [SQLI
11ea0 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b  TE_DBCONFIG_LOOK
11eb0 41 53 49 44 45 5d 0a 2a 2a 20 76 65 72 62 20 74  ASIDE].** verb t
11ec0 6f 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f  o [sqlite3_db_co
11ed0 6e 66 69 67 28 29 5d 20 63 61 6e 20 62 65 20 75  nfig()] can be u
11ee0 73 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68  sed to change th
11ef0 65 20 6c 6f 6f 6b 61 73 69 64 65 0a 2a 2a 20 63  e lookaside.** c
11f00 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 6e 20  onfiguration on 
11f10 69 6e 64 69 76 69 64 75 61 6c 20 63 6f 6e 6e 65  individual conne
11f20 63 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a  ctions.</dd>.**.
11f30 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
11f40 4e 46 49 47 5f 50 43 41 43 48 45 3c 2f 64 74 3e  NFIG_PCACHE</dt>
11f50 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
11f60 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
11f70 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
11f80 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
11f90 6f 0a 2a 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33  o.** an [sqlite3
11fa0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d  _pcache_methods]
11fb0 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 6f   object.  This o
11fc0 62 6a 65 63 74 20 73 70 65 63 69 66 69 65 73 20  bject specifies 
11fd0 74 68 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a  the interface.**
11fe0 20 74 6f 20 61 20 63 75 73 74 6f 6d 20 70 61 67   to a custom pag
11ff0 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  e cache implemen
12000 74 61 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20  tation.  SQLite 
12010 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20  makes a copy of 
12020 74 68 65 0a 2a 2a 20 6f 62 6a 65 63 74 20 61 6e  the.** object an
12030 64 20 75 73 65 73 20 69 74 20 66 6f 72 20 70 61  d uses it for pa
12040 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20  ge cache memory 
12050 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 3c 2f 64 64  allocations.</dd
12060 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
12070 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41  TE_CONFIG_GETPCA
12080 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  CHE</dt>.** <dd>
12090 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65  This option take
120a0 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d  s a single argum
120b0 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 70  ent which is a p
120c0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20  ointer to an.** 
120d0 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f  [sqlite3_pcache_
120e0 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e  methods] object.
120f0 20 20 53 51 4c 69 74 65 20 63 6f 70 69 65 73 20    SQLite copies 
12100 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  of the current.*
12110 2a 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70  * page cache imp
12120 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f  lementation into
12130 20 74 68 61 74 20 6f 62 6a 65 63 74 2e 3c 2f 64   that object.</d
12140 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a  d>.**.** </dl>.*
12150 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
12160 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
12170 52 45 41 44 20 20 31 20 20 2f 2a 20 6e 69 6c 20  READ  1  /* nil 
12180 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
12190 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48  E_CONFIG_MULTITH
121a0 52 45 41 44 20 20 20 32 20 20 2f 2a 20 6e 69 6c  READ   2  /* nil
121b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
121c0 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c  TE_CONFIG_SERIAL
121d0 49 5a 45 44 20 20 20 20 33 20 20 2f 2a 20 6e 69  IZED    3  /* ni
121e0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  l */.#define SQL
121f0 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f  ITE_CONFIG_MALLO
12200 43 20 20 20 20 20 20 20 20 34 20 20 2f 2a 20 73  C        4  /* s
12210 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
12220 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ds* */.#define S
12230 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
12240 4d 41 4c 4c 4f 43 20 20 20 20 20 35 20 20 2f 2a  MALLOC     5  /*
12250 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
12260 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65  hods* */.#define
12270 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53   SQLITE_CONFIG_S
12280 43 52 41 54 43 48 20 20 20 20 20 20 20 36 20 20  CRATCH       6  
12290 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a  /* void*, int sz
122a0 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69  , int N */.#defi
122b0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
122c0 5f 50 41 47 45 43 41 43 48 45 20 20 20 20 20 37  _PAGECACHE     7
122d0 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20    /* void*, int 
122e0 73 7a 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65  sz, int N */.#de
122f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
12300 49 47 5f 48 45 41 50 20 20 20 20 20 20 20 20 20  IG_HEAP         
12310 20 38 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e   8  /* void*, in
12320 74 20 6e 42 79 74 65 2c 20 69 6e 74 20 6d 69 6e  t nByte, int min
12330 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
12340 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41  TE_CONFIG_MEMSTA
12350 54 55 53 20 20 20 20 20 39 20 20 2f 2a 20 62 6f  TUS     9  /* bo
12360 6f 6c 65 61 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  olean */.#define
12370 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d   SQLITE_CONFIG_M
12380 55 54 45 58 20 20 20 20 20 20 20 20 31 30 20 20  UTEX        10  
12390 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  /* sqlite3_mutex
123a0 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65  _methods* */.#de
123b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
123c0 49 47 5f 47 45 54 4d 55 54 45 58 20 20 20 20 20  IG_GETMUTEX     
123d0 31 31 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d  11  /* sqlite3_m
123e0 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f  utex_methods* */
123f0 0a 2f 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 53  ./* previously S
12400 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 43 48 55  QLITE_CONFIG_CHU
12410 4e 4b 41 4c 4c 4f 43 20 31 32 20 77 68 69 63 68  NKALLOC 12 which
12420 20 69 73 20 6e 6f 77 20 75 6e 75 73 65 64 2e 20   is now unused. 
12430 2a 2f 20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  */ .#define SQLI
12440 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53  TE_CONFIG_LOOKAS
12450 49 44 45 20 20 20 20 31 33 20 20 2f 2a 20 69 6e  IDE    13  /* in
12460 74 20 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  t int */.#define
12470 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50   SQLITE_CONFIG_P
12480 43 41 43 48 45 20 20 20 20 20 20 20 31 34 20 20  CACHE       14  
12490 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  /* sqlite3_pcach
124a0 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64  e_methods* */.#d
124b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
124c0 46 49 47 5f 47 45 54 50 43 41 43 48 45 20 20 20  FIG_GETPCACHE   
124d0 20 31 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f   15  /* sqlite3_
124e0 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20  pcache_methods* 
124f0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  */../*.** CAPI3R
12500 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f  EF: Configuratio
12510 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 37  n Options {H1017
12520 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45  0} <S20000>.** E
12530 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
12540 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74  * These constant
12550 73 20 61 72 65 20 74 68 65 20 61 76 61 69 6c 61  s are the availa
12560 62 6c 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66  ble integer conf
12570 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
12580 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65  s that.** can be
12590 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
125a0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
125b0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64  o the [sqlite3_d
125c0 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65  b_config()] inte
125d0 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77  rface..**.** New
125e0 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
125f0 70 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64  ptions may be ad
12600 64 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65  ded in future re
12610 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65  leases of SQLite
12620 2e 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f  ..** Existing co
12630 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
12640 6f 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 73  ons might be dis
12650 63 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c  continued.  Appl
12660 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75  ications.** shou
12670 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 74  ld check the ret
12680 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73  urn code from [s
12690 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
126a0 28 29 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ()] to make sure
126b0 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c   that.** the cal
126c0 6c 20 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b  l worked.  The [
126d0 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69  sqlite3_db_confi
126e0 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77  g()] interface w
126f0 69 6c 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20  ill return a.** 
12700 6e 6f 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20  non-zero [error 
12710 63 6f 64 65 5d 20 69 66 20 61 20 64 69 73 63 6f  code] if a disco
12720 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e 73 75 70  ntinued or unsup
12730 70 6f 72 74 65 64 20 63 6f 6e 66 69 67 75 72 61  ported configura
12740 74 69 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69  tion option.** i
12750 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a  s invoked..**.**
12760 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c   <dl>.** <dt>SQL
12770 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f  ITE_DBCONFIG_LOO
12780 4b 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c  KASIDE</dt>.** <
12790 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74  dd>This option t
127a0 61 6b 65 73 20 74 68 72 65 65 20 61 64 64 69 74  akes three addit
127b0 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 20  ional arguments 
127c0 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74  that determine t
127d0 68 65 20 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64  he .** [lookasid
127e0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
127f0 6f 72 5d 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  or] configuratio
12800 6e 20 66 6f 72 20 74 68 65 20 5b 64 61 74 61 62  n for the [datab
12810 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e  ase connection].
12820 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72  .** The first ar
12830 67 75 6d 65 6e 74 20 28 74 68 65 20 74 68 69 72  gument (the thir
12840 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  d parameter to [
12850 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69  sqlite3_db_confi
12860 67 28 29 5d 20 69 73 20 61 0a 2a 2a 20 70 6f 69  g()] is a.** poi
12870 6e 74 65 72 20 74 6f 20 61 6e 20 6d 65 6d 6f 72  nter to an memor
12880 79 20 62 75 66 66 65 72 20 74 6f 20 75 73 65 20  y buffer to use 
12890 66 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  for lookaside me
128a0 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65 20 66 69 72  mory..** The fir
128b0 73 74 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20  st argument may 
128c0 62 65 20 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68  be NULL in which
128d0 20 63 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c   case SQLite wil
128e0 6c 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 0a 2a  l allocate the.*
128f0 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66  * lookaside buff
12900 65 72 20 69 74 73 65 6c 66 20 75 73 69 6e 67 20  er itself using 
12910 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
12920 29 5d 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  )].  The second 
12930 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a  argument is the.
12940 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20  ** size of each 
12950 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72  lookaside buffer
12960 20 73 6c 6f 74 20 61 6e 64 20 74 68 65 20 74 68   slot and the th
12970 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ird argument is 
12980 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
12990 20 73 6c 6f 74 73 2e 20 20 54 68 65 20 73 69 7a   slots.  The siz
129a0 65 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20  e of the buffer 
129b0 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67  in the first arg
129c0 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 67 72  ument must be gr
129d0 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a 20 6f 72  eater than.** or
129e0 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 70 72   equal to the pr
129f0 6f 64 75 63 74 20 6f 66 20 74 68 65 20 73 65 63  oduct of the sec
12a00 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20 61 72  ond and third ar
12a10 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 20 62 75  guments.  The bu
12a20 66 66 65 72 0a 2a 2a 20 6d 75 73 74 20 62 65 20  ffer.** must be 
12a30 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d  aligned to an 8-
12a40 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 20  byte boundary.  
12a50 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
12a60 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 0a 2a 2a  gument is not.**
12a70 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38   a multiple of 8
12a80 2c 20 69 74 20 69 73 20 69 6e 74 65 72 6e 61 6c  , it is internal
12a90 6c 79 20 72 6f 75 6e 64 65 64 20 64 6f 77 6e 20  ly rounded down 
12aa0 74 6f 20 74 68 65 20 6e 65 78 74 20 73 6d 61 6c  to the next smal
12ab0 6c 65 72 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20  ler.** multiple 
12ac0 6f 66 20 38 2e 20 20 53 65 65 20 61 6c 73 6f 3a  of 8.  See also:
12ad0 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
12ae0 4c 4f 4f 4b 41 53 49 44 45 5d 3c 2f 64 64 3e 0a  LOOKASIDE]</dd>.
12af0 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23  **.** </dl>.*/.#
12b00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42  define SQLITE_DB
12b10 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
12b20 20 20 20 20 31 30 30 31 20 20 2f 2a 20 76 6f 69      1001  /* voi
12b30 64 2a 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a  d* int int */...
12b40 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
12b50 45 6e 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c  Enable Or Disabl
12b60 65 20 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c  e Extended Resul
12b70 74 20 43 6f 64 65 73 20 7b 48 31 32 32 30 30 7d  t Codes {H12200}
12b80 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
12b90 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65  The sqlite3_exte
12ba0 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65  nded_result_code
12bb0 73 28 29 20 72 6f 75 74 69 6e 65 20 65 6e 61 62  s() routine enab
12bc0 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20  les or disables 
12bd0 74 68 65 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64  the.** [extended
12be0 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66   result codes] f
12bf0 65 61 74 75 72 65 20 6f 66 20 53 51 4c 69 74 65  eature of SQLite
12c00 2e 20 54 68 65 20 65 78 74 65 6e 64 65 64 20 72  . The extended r
12c10 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 73 20 61  esult.** codes a
12c20 72 65 20 64 69 73 61 62 6c 65 64 20 62 79 20 64  re disabled by d
12c30 65 66 61 75 6c 74 20 66 6f 72 20 68 69 73 74 6f  efault for histo
12c40 72 69 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c  rical compatibil
12c50 69 74 79 20 63 6f 6e 73 69 64 65 72 61 74 69 6f  ity consideratio
12c60 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ns..**.** Requir
12c70 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32  ements:.** [H122
12c80 30 31 5d 20 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a  01] [H12202].*/.
12c90 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
12ca0 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
12cb0 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c  result_codes(sql
12cc0 69 74 65 33 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66  ite3*, int onoff
12cd0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
12ce0 45 46 3a 20 4c 61 73 74 20 49 6e 73 65 72 74 20  EF: Last Insert 
12cf0 52 6f 77 69 64 20 7b 48 31 32 32 32 30 7d 20 3c  Rowid {H12220} <
12d00 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61  S10700>.**.** Ea
12d10 63 68 20 65 6e 74 72 79 20 69 6e 20 61 6e 20 53  ch entry in an S
12d20 51 4c 69 74 65 20 74 61 62 6c 65 20 68 61 73 20  QLite table has 
12d30 61 20 75 6e 69 71 75 65 20 36 34 2d 62 69 74 20  a unique 64-bit 
12d40 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65  signed.** intege
12d50 72 20 6b 65 79 20 63 61 6c 6c 65 64 20 74 68 65  r key called the
12d60 20 5b 52 4f 57 49 44 20 7c 20 22 72 6f 77 69 64   [ROWID | "rowid
12d70 22 5d 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73  "]. The rowid is
12d80 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c   always availabl
12d90 65 0a 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63  e.** as an undec
12da0 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  lared column nam
12db0 65 64 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f  ed ROWID, OID, o
12dc0 72 20 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e  r _ROWID_ as lon
12dd0 67 20 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61  g as those.** na
12de0 6d 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f  mes are not also
12df0 20 75 73 65 64 20 62 79 20 65 78 70 6c 69 63 69   used by explici
12e00 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c  tly declared col
12e10 75 6d 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20  umns. If.** the 
12e20 74 61 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75  table has a colu
12e30 6d 6e 20 6f 66 20 74 79 70 65 20 5b 49 4e 54 45  mn of type [INTE
12e40 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d  GER PRIMARY KEY]
12e50 20 74 68 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d   then that colum
12e60 6e 0a 2a 2a 20 69 73 20 61 6e 6f 74 68 65 72 20  n.** is another 
12e70 61 6c 69 61 73 20 66 6f 72 20 74 68 65 20 72 6f  alias for the ro
12e80 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  wid..**.** This 
12e90 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
12ea0 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74  the [rowid] of t
12eb0 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a  he most recent.*
12ec0 2a 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e  * successful [IN
12ed0 53 45 52 54 5d 20 69 6e 74 6f 20 74 68 65 20 64  SERT] into the d
12ee0 61 74 61 62 61 73 65 20 66 72 6f 6d 20 74 68 65  atabase from the
12ef0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
12f00 63 74 69 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65  ction].** in the
12f10 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
12f20 20 20 49 66 20 6e 6f 20 73 75 63 63 65 73 73 66    If no successf
12f30 75 6c 20 5b 49 4e 53 45 52 54 5d 73 0a 2a 2a 20  ul [INSERT]s.** 
12f40 68 61 76 65 20 65 76 65 72 20 6f 63 63 75 72 72  have ever occurr
12f50 65 64 20 6f 6e 20 74 68 61 74 20 64 61 74 61 62  ed on that datab
12f60 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20  ase connection, 
12f70 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64  zero is returned
12f80 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 5b 49  ..**.** If an [I
12f90 4e 53 45 52 54 5d 20 6f 63 63 75 72 73 20 77 69  NSERT] occurs wi
12fa0 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 2c 20  thin a trigger, 
12fb0 74 68 65 6e 20 74 68 65 20 5b 72 6f 77 69 64 5d  then the [rowid]
12fc0 20 6f 66 20 74 68 65 20 69 6e 73 65 72 74 65 64   of the inserted
12fd0 0a 2a 2a 20 72 6f 77 20 69 73 20 72 65 74 75 72  .** row is retur
12fe0 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ned by this rout
12ff0 69 6e 65 20 61 73 20 6c 6f 6e 67 20 61 73 20 74  ine as long as t
13000 68 65 20 74 72 69 67 67 65 72 20 69 73 20 72 75  he trigger is ru
13010 6e 6e 69 6e 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e  nning..** But on
13020 63 65 20 74 68 65 20 74 72 69 67 67 65 72 20 74  ce the trigger t
13030 65 72 6d 69 6e 61 74 65 73 2c 20 74 68 65 20 76  erminates, the v
13040 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
13050 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
13060 20 72 65 76 65 72 74 73 20 74 6f 20 74 68 65 20   reverts to the 
13070 6c 61 73 74 20 76 61 6c 75 65 20 69 6e 73 65 72  last value inser
13080 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 74  ted before the t
13090 72 69 67 67 65 72 20 66 69 72 65 64 2e 0a 2a 2a  rigger fired..**
130a0 0a 2a 2a 20 41 6e 20 5b 49 4e 53 45 52 54 5d 20  .** An [INSERT] 
130b0 74 68 61 74 20 66 61 69 6c 73 20 64 75 65 20 74  that fails due t
130c0 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76  o a constraint v
130d0 69 6f 6c 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20  iolation is not 
130e0 61 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20  a.** successful 
130f0 5b 49 4e 53 45 52 54 5d 20 61 6e 64 20 64 6f 65  [INSERT] and doe
13100 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65  s not change the
13110 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
13120 62 79 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  by this.** routi
13130 6e 65 2e 20 20 54 68 75 73 20 49 4e 53 45 52 54  ne.  Thus INSERT
13140 20 4f 52 20 46 41 49 4c 2c 20 49 4e 53 45 52 54   OR FAIL, INSERT
13150 20 4f 52 20 49 47 4e 4f 52 45 2c 20 49 4e 53 45   OR IGNORE, INSE
13160 52 54 20 4f 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a  RT OR ROLLBACK,.
13170 2a 2a 20 61 6e 64 20 49 4e 53 45 52 54 20 4f 52  ** and INSERT OR
13180 20 41 42 4f 52 54 20 6d 61 6b 65 20 6e 6f 20 63   ABORT make no c
13190 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 72 65  hanges to the re
131a0 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
131b0 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 68  is.** routine wh
131c0 65 6e 20 74 68 65 69 72 20 69 6e 73 65 72 74 69  en their inserti
131d0 6f 6e 20 66 61 69 6c 73 2e 20 20 57 68 65 6e 20  on fails.  When 
131e0 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43  INSERT OR REPLAC
131f0 45 0a 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 73 20  E.** encounters 
13200 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  a constraint vio
13210 6c 61 74 69 6f 6e 2c 20 69 74 20 64 6f 65 73 20  lation, it does 
13220 6e 6f 74 20 66 61 69 6c 2e 20 20 54 68 65 0a 2a  not fail.  The.*
13230 2a 20 49 4e 53 45 52 54 20 63 6f 6e 74 69 6e 75  * INSERT continu
13240 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e  es to completion
13250 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20   after deleting 
13260 72 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 64  rows that caused
13270 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69  .** the constrai
13280 6e 74 20 70 72 6f 62 6c 65 6d 20 73 6f 20 49 4e  nt problem so IN
13290 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20  SERT OR REPLACE 
132a0 77 69 6c 6c 20 61 6c 77 61 79 73 20 63 68 61 6e  will always chan
132b0 67 65 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e  ge.** the return
132c0 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 69   value of this i
132d0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
132e0 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73  For the purposes
132f0 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
13300 2c 20 61 6e 20 5b 49 4e 53 45 52 54 5d 20 69 73  , an [INSERT] is
13310 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a   considered to.*
13320 2a 20 62 65 20 73 75 63 63 65 73 73 66 75 6c 20  * be successful 
13330 65 76 65 6e 20 69 66 20 69 74 20 69 73 20 73 75  even if it is su
13340 62 73 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65  bsequently rolle
13350 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  d back..**.** Th
13360 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
13370 63 63 65 73 73 69 62 6c 65 20 74 6f 20 53 51 4c  ccessible to SQL
13380 20 73 74 61 74 65 6d 65 6e 74 73 20 76 69 61 20   statements via 
13390 74 68 65 0a 2a 2a 20 5b 6c 61 73 74 5f 69 6e 73  the.** [last_ins
133a0 65 72 74 5f 72 6f 77 69 64 28 29 20 53 51 4c 20  ert_rowid() SQL 
133b0 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a  function]..**.**
133c0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
133d0 2a 20 5b 48 31 32 32 32 31 5d 20 5b 48 31 32 32  * [H12221] [H122
133e0 32 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73  23].**.** If a s
133f0 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 70  eparate thread p
13400 65 72 66 6f 72 6d 73 20 61 20 6e 65 77 20 5b 49  erforms a new [I
13410 4e 53 45 52 54 5d 20 6f 6e 20 74 68 65 20 73 61  NSERT] on the sa
13420 6d 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  me.** database c
13430 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20  onnection while 
13440 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73  the [sqlite3_las
13450 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29  t_insert_rowid()
13460 5d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73  ].** function is
13470 20 72 75 6e 6e 69 6e 67 20 61 6e 64 20 74 68 75   running and thu
13480 73 20 63 68 61 6e 67 65 73 20 74 68 65 20 6c 61  s changes the la
13490 73 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69 64  st insert [rowid
134a0 5d 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76  ],.** then the v
134b0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
134c0 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69   [sqlite3_last_i
134d0 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 20 69  nsert_rowid()] i
134e0 73 0a 2a 2a 20 75 6e 70 72 65 64 69 63 74 61 62  s.** unpredictab
134f0 6c 65 20 61 6e 64 20 6d 69 67 68 74 20 6e 6f 74  le and might not
13500 20 65 71 75 61 6c 20 65 69 74 68 65 72 20 74 68   equal either th
13510 65 20 6f 6c 64 20 6f 72 20 74 68 65 20 6e 65 77  e old or the new
13520 0a 2a 2a 20 6c 61 73 74 20 69 6e 73 65 72 74 20  .** last insert 
13530 5b 72 6f 77 69 64 5d 2e 0a 2a 2f 0a 53 51 4c 49  [rowid]..*/.SQLI
13540 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69  TE_API sqlite3_i
13550 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6c 61 73  nt64 sqlite3_las
13560 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 73  t_insert_rowid(s
13570 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  qlite3*);../*.**
13580 20 43 41 50 49 33 52 45 46 3a 20 43 6f 75 6e 74   CAPI3REF: Count
13590 20 54 68 65 20 4e 75 6d 62 65 72 20 4f 66 20 52   The Number Of R
135a0 6f 77 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31  ows Modified {H1
135b0 32 32 34 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a  2240} <S10600>.*
135c0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
135d0 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  on returns the n
135e0 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73  umber of databas
135f0 65 20 72 6f 77 73 20 74 68 61 74 20 77 65 72 65  e rows that were
13600 20 63 68 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69   changed.** or i
13610 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74  nserted or delet
13620 65 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72  ed by the most r
13630 65 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65  ecently complete
13640 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a  d SQL statement.
13650 2a 2a 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62  ** on the [datab
13660 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
13670 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
13680 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
13690 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65  ..** Only change
136a0 73 20 74 68 61 74 20 61 72 65 20 64 69 72 65 63  s that are direc
136b0 74 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 79  tly specified by
136c0 20 74 68 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b   the [INSERT], [
136d0 55 50 44 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b  UPDATE],.** or [
136e0 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e  DELETE] statemen
136f0 74 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 20 20  t are counted.  
13700 41 75 78 69 6c 69 61 72 79 20 63 68 61 6e 67 65  Auxiliary change
13710 73 20 63 61 75 73 65 64 20 62 79 0a 2a 2a 20 74  s caused by.** t
13720 72 69 67 67 65 72 73 20 6f 72 20 5b 66 6f 72 65  riggers or [fore
13730 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 5d  ign key actions]
13740 20 61 72 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64   are not counted
13750 2e 20 55 73 65 20 74 68 65 0a 2a 2a 20 5b 73 71  . Use the.** [sq
13760 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
13770 67 65 73 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20  ges()] function 
13780 74 6f 20 66 69 6e 64 20 74 68 65 20 74 6f 74 61  to find the tota
13790 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e  l number of chan
137a0 67 65 73 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e 67  ges.** including
137b0 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 20   changes caused 
137c0 62 79 20 74 72 69 67 67 65 72 73 20 61 6e 64 20  by triggers and 
137d0 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69  foreign key acti
137e0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67  ons..**.** Chang
137f0 65 73 20 74 6f 20 61 20 76 69 65 77 20 74 68 61  es to a view tha
13800 74 20 61 72 65 20 73 69 6d 75 6c 61 74 65 64 20  t are simulated 
13810 62 79 20 61 6e 20 5b 49 4e 53 54 45 41 44 20 4f  by an [INSTEAD O
13820 46 20 74 72 69 67 67 65 72 5d 0a 2a 2a 20 61 72  F trigger].** ar
13830 65 20 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 20 20  e not counted.  
13840 4f 6e 6c 79 20 72 65 61 6c 20 74 61 62 6c 65 20  Only real table 
13850 63 68 61 6e 67 65 73 20 61 72 65 20 63 6f 75 6e  changes are coun
13860 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 72 6f  ted..**.** A "ro
13870 77 20 63 68 61 6e 67 65 22 20 69 73 20 61 20 63  w change" is a c
13880 68 61 6e 67 65 20 74 6f 20 61 20 73 69 6e 67 6c  hange to a singl
13890 65 20 72 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c  e row of a singl
138a0 65 20 74 61 62 6c 65 0a 2a 2a 20 63 61 75 73 65  e table.** cause
138b0 64 20 62 79 20 61 6e 20 49 4e 53 45 52 54 2c 20  d by an INSERT, 
138c0 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54  DELETE, or UPDAT
138d0 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 52 6f  E statement.  Ro
138e0 77 73 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 63  ws that.** are c
138f0 68 61 6e 67 65 64 20 61 73 20 73 69 64 65 20 65  hanged as side e
13900 66 66 65 63 74 73 20 6f 66 20 5b 52 45 50 4c 41  ffects of [REPLA
13910 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 20 72  CE] constraint r
13920 65 73 6f 6c 75 74 69 6f 6e 2c 0a 2a 2a 20 72 6f  esolution,.** ro
13930 6c 6c 62 61 63 6b 2c 20 41 42 4f 52 54 20 70 72  llback, ABORT pr
13940 6f 63 65 73 73 69 6e 67 2c 20 5b 44 52 4f 50 20  ocessing, [DROP 
13950 54 41 42 4c 45 5d 2c 20 6f 72 20 62 79 20 61 6e  TABLE], or by an
13960 79 20 6f 74 68 65 72 0a 2a 2a 20 6d 65 63 68 61  y other.** mecha
13970 6e 69 73 6d 73 20 64 6f 20 6e 6f 74 20 63 6f 75  nisms do not cou
13980 6e 74 20 61 73 20 64 69 72 65 63 74 20 72 6f 77  nt as direct row
13990 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20   changes..**.** 
139a0 41 20 22 74 72 69 67 67 65 72 20 63 6f 6e 74 65  A "trigger conte
139b0 78 74 22 20 69 73 20 61 20 73 63 6f 70 65 20 6f  xt" is a scope o
139c0 66 20 65 78 65 63 75 74 69 6f 6e 20 74 68 61 74  f execution that
139d0 20 62 65 67 69 6e 73 20 61 6e 64 0a 2a 2a 20 65   begins and.** e
139e0 6e 64 73 20 77 69 74 68 20 74 68 65 20 73 63 72  nds with the scr
139f0 69 70 74 20 6f 66 20 61 20 5b 43 52 45 41 54 45  ipt of a [CREATE
13a00 20 54 52 49 47 47 45 52 20 7c 20 74 72 69 67 67   TRIGGER | trigg
13a10 65 72 5d 2e 20 0a 2a 2a 20 4d 6f 73 74 20 53 51  er]. .** Most SQ
13a20 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65  L statements are
13a30 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6f 75  .** evaluated ou
13a40 74 73 69 64 65 20 6f 66 20 61 6e 79 20 74 72 69  tside of any tri
13a50 67 67 65 72 2e 20 20 54 68 69 73 20 69 73 20 74  gger.  This is t
13a60 68 65 20 22 74 6f 70 20 6c 65 76 65 6c 22 0a 2a  he "top level".*
13a70 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78  * trigger contex
13a80 74 2e 20 20 49 66 20 61 20 74 72 69 67 67 65 72  t.  If a trigger
13a90 20 66 69 72 65 73 20 66 72 6f 6d 20 74 68 65 20   fires from the 
13aa0 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a 2a 2a 20  top level, a.** 
13ab0 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e 74  new trigger cont
13ac0 65 78 74 20 69 73 20 65 6e 74 65 72 65 64 20 66  ext is entered f
13ad0 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20  or the duration 
13ae0 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a 2a 20 74  of that one.** t
13af0 72 69 67 67 65 72 2e 20 20 53 75 62 74 72 69 67  rigger.  Subtrig
13b00 67 65 72 73 20 63 72 65 61 74 65 20 73 75 62 63  gers create subc
13b10 6f 6e 74 65 78 74 73 20 66 6f 72 20 74 68 65 69  ontexts for thei
13b20 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  r duration..**.*
13b30 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74  * Calling [sqlit
13b40 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 20 5b 73  e3_exec()] or [s
13b50 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 72  qlite3_step()] r
13b60 65 63 75 72 73 69 76 65 6c 79 20 64 6f 65 73 0a  ecursively does.
13b70 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 61 20  ** not create a 
13b80 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e 74  new trigger cont
13b90 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ext..**.** This 
13ba0 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
13bb0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   the number of d
13bc0 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65  irect row change
13bd0 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74  s in the.** most
13be0 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20   recent INSERT, 
13bf0 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45 54  UPDATE, or DELET
13c00 45 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74 68  E statement with
13c10 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74  in the same.** t
13c20 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a  rigger context..
13c30 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77 68 65 6e  **.** Thus, when
13c40 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
13c50 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 69 73   top level, this
13c60 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
13c70 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  s the.** number 
13c80 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68  of changes in th
13c90 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49 4e  e most recent IN
13ca0 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72  SERT, UPDATE, or
13cb0 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20   DELETE.** that 
13cc0 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 61 74  also occurred at
13cd0 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e 20   the top level. 
13ce0 20 57 69 74 68 69 6e 20 74 68 65 20 62 6f 64 79   Within the body
13cf0 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c 0a 2a   of a trigger,.*
13d00 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 68  * the sqlite3_ch
13d10 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66 61 63  anges() interfac
13d20 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20  e can be called 
13d30 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d 62  to find the numb
13d40 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 65 73  er of.** changes
13d50 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63   in the most rec
13d60 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20  ently completed 
13d70 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20  INSERT, UPDATE, 
13d80 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74 61  or DELETE.** sta
13d90 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68  tement within th
13da0 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 73 61  e body of the sa
13db0 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a 20 48  me trigger..** H
13dc0 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75 6d 62  owever, the numb
13dd0 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f 65 73  er returned does
13de0 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61   not include cha
13df0 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64 20 62  nges.** caused b
13e00 79 20 73 75 62 74 72 69 67 67 65 72 73 20 73 69  y subtriggers si
13e10 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65 20 74  nce those have t
13e20 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65 78 74  heir own context
13e30 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
13e40 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f   the [sqlite3_to
13e50 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 69  tal_changes()] i
13e60 6e 74 65 72 66 61 63 65 2c 20 74 68 65 0a 2a 2a  nterface, the.**
13e70 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20   [count_changes 
13e80 70 72 61 67 6d 61 5d 2c 20 61 6e 64 20 74 68 65  pragma], and the
13e90 20 5b 63 68 61 6e 67 65 73 28 29 20 53 51 4c 20   [changes() SQL 
13ea0 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a  function]..**.**
13eb0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
13ec0 2a 20 5b 48 31 32 32 34 31 5d 20 5b 48 31 32 32  * [H12241] [H122
13ed0 34 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73  43].**.** If a s
13ee0 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 6d  eparate thread m
13ef0 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20  akes changes on 
13f00 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
13f10 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
13f20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 63  while [sqlite3_c
13f30 68 61 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e  hanges()] is run
13f40 6e 69 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61  ning then the va
13f50 6c 75 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  lue returned.** 
13f60 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65  is unpredictable
13f70 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67   and not meaning
13f80 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ful..*/.SQLITE_A
13f90 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
13fa0 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 2a 29  hanges(sqlite3*)
13fb0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
13fc0 46 3a 20 54 6f 74 61 6c 20 4e 75 6d 62 65 72 20  F: Total Number 
13fd0 4f 66 20 52 6f 77 73 20 4d 6f 64 69 66 69 65 64  Of Rows Modified
13fe0 20 7b 48 31 32 32 36 30 7d 20 3c 53 31 30 36 30   {H12260} <S1060
13ff0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  0>.**.** This fu
14000 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
14010 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
14020 20 63 68 61 6e 67 65 73 20 63 61 75 73 65 64 20   changes caused 
14030 62 79 20 5b 49 4e 53 45 52 54 5d 2c 0a 2a 2a 20  by [INSERT],.** 
14040 5b 55 50 44 41 54 45 5d 20 6f 72 20 5b 44 45 4c  [UPDATE] or [DEL
14050 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74 73 20  ETE] statements 
14060 73 69 6e 63 65 20 74 68 65 20 5b 64 61 74 61 62  since the [datab
14070 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
14080 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54  was opened..** T
14090 68 65 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64 65  he count include
140a0 73 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 66 72  s all changes fr
140b0 6f 6d 20 61 6c 6c 20 5b 43 52 45 41 54 45 20 54  om all [CREATE T
140c0 52 49 47 47 45 52 20 7c 20 74 72 69 67 67 65 72  RIGGER | trigger
140d0 5d 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 73 20 61  ] .** contexts a
140e0 6e 64 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20  nd changes made 
140f0 62 79 20 5b 66 6f 72 65 69 67 6e 20 6b 65 79 20  by [foreign key 
14100 61 63 74 69 6f 6e 73 5d 2e 20 48 6f 77 65 76 65  actions]. Howeve
14110 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74 20  r,.** the count 
14120 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65  does not include
14130 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74 6f   changes used to
14140 20 69 6d 70 6c 65 6d 65 6e 74 20 5b 52 45 50 4c   implement [REPL
14150 41 43 45 5d 20 63 6f 6e 73 74 72 61 69 6e 74 73  ACE] constraints
14160 2c 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b  ,.** do rollback
14170 73 20 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 65  s or ABORT proce
14180 73 73 69 6e 67 2c 20 6f 72 20 5b 44 52 4f 50 20  ssing, or [DROP 
14190 54 41 42 4c 45 5d 20 70 72 6f 63 65 73 73 69 6e  TABLE] processin
141a0 67 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 75 6e 74  g.  The.** count
141b0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
141c0 65 20 72 6f 77 73 20 6f 66 20 76 69 65 77 73 20  e rows of views 
141d0 74 68 61 74 20 66 69 72 65 20 61 6e 20 5b 49 4e  that fire an [IN
141e0 53 54 45 41 44 20 4f 46 20 74 72 69 67 67 65 72  STEAD OF trigger
141f0 5d 2c 0a 2a 2a 20 74 68 6f 75 67 68 20 69 66 20  ],.** though if 
14200 74 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20 74  the INSTEAD OF t
14210 72 69 67 67 65 72 20 6d 61 6b 65 73 20 63 68 61  rigger makes cha
14220 6e 67 65 73 20 6f 66 20 69 74 73 20 6f 77 6e 2c  nges of its own,
14230 20 74 68 6f 73 65 20 63 68 61 6e 67 65 73 20 0a   those changes .
14240 2a 2a 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a  ** are counted..
14250 2a 2a 20 54 68 65 20 63 68 61 6e 67 65 73 20 61  ** The changes a
14260 72 65 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f  re counted as so
14270 6f 6e 20 61 73 20 74 68 65 20 73 74 61 74 65 6d  on as the statem
14280 65 6e 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74  ent that makes t
14290 68 65 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65  hem is.** comple
142a0 74 65 64 20 28 77 68 65 6e 20 74 68 65 20 73 74  ted (when the st
142b0 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69  atement handle i
142c0 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c  s passed to [sql
142d0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72  ite3_reset()] or
142e0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  .** [sqlite3_fin
142f0 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a  alize()])..**.**
14300 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 73   See also the [s
14310 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29  qlite3_changes()
14320 5d 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65  ] interface, the
14330 0a 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67  .** [count_chang
14340 65 73 20 70 72 61 67 6d 61 5d 2c 20 61 6e 64 20  es pragma], and 
14350 74 68 65 20 5b 74 6f 74 61 6c 5f 63 68 61 6e 67  the [total_chang
14360 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  es() SQL functio
14370 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  n]..**.** Requir
14380 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32  ements:.** [H122
14390 36 31 5d 20 5b 48 31 32 32 36 33 5d 0a 2a 2a 0a  61] [H12263].**.
143a0 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61 74 65  ** If a separate
143b0 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68   thread makes ch
143c0 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d  anges on the sam
143d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
143e0 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c 65 20 5b  ction.** while [
143f0 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68  sqlite3_total_ch
14400 61 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e  anges()] is runn
14410 69 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c  ing then the val
14420 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69  ue.** returned i
14430 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20  s unpredictable 
14440 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66  and not meaningf
14450 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ul..*/.SQLITE_AP
14460 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f  I int sqlite3_to
14470 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69  tal_changes(sqli
14480 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  te3*);../*.** CA
14490 50 49 33 52 45 46 3a 20 49 6e 74 65 72 72 75 70  PI3REF: Interrup
144a0 74 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69 6e 67  t A Long-Running
144b0 20 51 75 65 72 79 20 7b 48 31 32 32 37 30 7d 20   Query {H12270} 
144c0 3c 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S30500>.**.** T
144d0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 75  his function cau
144e0 73 65 73 20 61 6e 79 20 70 65 6e 64 69 6e 67 20  ses any pending 
144f0 64 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69  database operati
14500 6f 6e 20 74 6f 20 61 62 6f 72 74 20 61 6e 64 0a  on to abort and.
14510 2a 2a 20 72 65 74 75 72 6e 20 61 74 20 69 74 73  ** return at its
14520 20 65 61 72 6c 69 65 73 74 20 6f 70 70 6f 72 74   earliest opport
14530 75 6e 69 74 79 2e 20 54 68 69 73 20 72 6f 75 74  unity. This rout
14540 69 6e 65 20 69 73 20 74 79 70 69 63 61 6c 6c 79  ine is typically
14550 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20 72 65  .** called in re
14560 73 70 6f 6e 73 65 20 74 6f 20 61 20 75 73 65 72  sponse to a user
14570 20 61 63 74 69 6f 6e 20 73 75 63 68 20 61 73 20   action such as 
14580 70 72 65 73 73 69 6e 67 20 22 43 61 6e 63 65 6c  pressing "Cancel
14590 22 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43 20 77  ".** or Ctrl-C w
145a0 68 65 72 65 20 74 68 65 20 75 73 65 72 20 77 61  here the user wa
145b0 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75 65 72 79  nts a long query
145c0 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 68 61   operation to ha
145d0 6c 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  lt.** immediatel
145e0 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 73  y..**.** It is s
145f0 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73  afe to call this
14600 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 61 20   routine from a 
14610 74 68 72 65 61 64 20 64 69 66 66 65 72 65 6e 74  thread different
14620 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 74 68 72   from the.** thr
14630 65 61 64 20 74 68 61 74 20 69 73 20 63 75 72 72  ead that is curr
14640 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 20 74 68  ently running th
14650 65 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61  e database opera
14660 74 69 6f 6e 2e 20 20 42 75 74 20 69 74 0a 2a 2a  tion.  But it.**
14670 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20   is not safe to 
14680 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  call this routin
14690 65 20 77 69 74 68 20 61 20 5b 64 61 74 61 62 61  e with a [databa
146a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74  se connection] t
146b0 68 61 74 0a 2a 2a 20 69 73 20 63 6c 6f 73 65 64  hat.** is closed
146c0 20 6f 72 20 6d 69 67 68 74 20 63 6c 6f 73 65 20   or might close 
146d0 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33 5f 69  before sqlite3_i
146e0 6e 74 65 72 72 75 70 74 28 29 20 72 65 74 75 72  nterrupt() retur
146f0 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ns..**.** If an 
14700 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  SQL operation is
14710 20 76 65 72 79 20 6e 65 61 72 6c 79 20 66 69 6e   very nearly fin
14720 69 73 68 65 64 20 61 74 20 74 68 65 20 74 69 6d  ished at the tim
14730 65 20 77 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65  e when.** sqlite
14740 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 69 73  3_interrupt() is
14750 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 69 74   called, then it
14760 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65 20   might not have 
14770 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 0a 2a  an opportunity.*
14780 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72 75 70  * to be interrup
14790 74 65 64 20 61 6e 64 20 6d 69 67 68 74 20 63 6f  ted and might co
147a0 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6d 70 6c 65  ntinue to comple
147b0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 53  tion..**.** An S
147c0 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  QL operation tha
147d0 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64  t is interrupted
147e0 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51   will return [SQ
147f0 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 2e  LITE_INTERRUPT].
14800 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 74 65 72  .** If the inter
14810 72 75 70 74 65 64 20 53 51 4c 20 6f 70 65 72 61  rupted SQL opera
14820 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53 45 52  tion is an INSER
14830 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45  T, UPDATE, or DE
14840 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 69 73 20  LETE.** that is 
14850 69 6e 73 69 64 65 20 61 6e 20 65 78 70 6c 69 63  inside an explic
14860 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  it transaction, 
14870 74 68 65 6e 20 74 68 65 20 65 6e 74 69 72 65 20  then the entire 
14880 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 77  transaction.** w
14890 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  ill be rolled ba
148a0 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ck automatically
148b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
148c0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 44 29  te3_interrupt(D)
148d0 20 63 61 6c 6c 20 69 73 20 69 6e 20 65 66 66 65   call is in effe
148e0 63 74 20 75 6e 74 69 6c 20 61 6c 6c 20 63 75 72  ct until all cur
148f0 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 0a 2a  rently running.*
14900 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  * SQL statements
14910 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f   on [database co
14920 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 63 6f 6d 70  nnection] D comp
14930 6c 65 74 65 2e 20 20 41 6e 79 20 6e 65 77 20 53  lete.  Any new S
14940 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  QL statements.**
14950 20 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65   that are starte
14960 64 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69  d after the sqli
14970 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20  te3_interrupt() 
14980 63 61 6c 6c 20 61 6e 64 20 62 65 66 6f 72 65 20  call and before 
14990 74 68 65 20 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20  the .** running 
149a0 73 74 61 74 65 6d 65 6e 74 73 20 72 65 61 63 68  statements reach
149b0 65 73 20 7a 65 72 6f 20 61 72 65 20 69 6e 74 65  es zero are inte
149c0 72 72 75 70 74 65 64 20 61 73 20 69 66 20 74 68  rrupted as if th
149d0 65 79 20 68 61 64 20 62 65 65 6e 0a 2a 2a 20 72  ey had been.** r
149e0 75 6e 6e 69 6e 67 20 70 72 69 6f 72 20 74 6f 20  unning prior to 
149f0 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  the sqlite3_inte
14a00 72 72 75 70 74 28 29 20 63 61 6c 6c 2e 20 20 4e  rrupt() call.  N
14a10 65 77 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ew SQL statement
14a20 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 73 74  s.** that are st
14a30 61 72 74 65 64 20 61 66 74 65 72 20 74 68 65 20  arted after the 
14a40 72 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e  running statemen
14a50 74 20 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20  t count reaches 
14a60 7a 65 72 6f 20 61 72 65 0a 2a 2a 20 6e 6f 74 20  zero are.** not 
14a70 65 66 66 65 63 74 65 64 20 62 79 20 74 68 65 20  effected by the 
14a80 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
14a90 74 28 29 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74  t()..** A call t
14aa0 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  o sqlite3_interr
14ab0 75 70 74 28 44 29 20 74 68 61 74 20 6f 63 63 75  upt(D) that occu
14ac0 72 73 20 77 68 65 6e 20 74 68 65 72 65 20 61 72  rs when there ar
14ad0 65 20 6e 6f 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20  e no running.** 
14ae0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69  SQL statements i
14af0 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 68 61  s a no-op and ha
14b00 73 20 6e 6f 20 65 66 66 65 63 74 20 6f 6e 20 53  s no effect on S
14b10 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  QL statements.**
14b20 20 74 68 61 74 20 61 72 65 20 73 74 61 72 74 65   that are starte
14b30 64 20 61 66 74 65 72 20 74 68 65 20 73 71 6c 69  d after the sqli
14b40 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20  te3_interrupt() 
14b50 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 2a 2a  call returns..**
14b60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
14b70 3a 0a 2a 2a 20 5b 48 31 32 32 37 31 5d 20 5b 48  :.** [H12271] [H
14b80 31 32 32 37 32 5d 0a 2a 2a 0a 2a 2a 20 49 66 20  12272].**.** If 
14b90 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
14ba0 6e 65 63 74 69 6f 6e 20 63 6c 6f 73 65 73 20 77  nection closes w
14bb0 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e  hile [sqlite3_in
14bc0 74 65 72 72 75 70 74 28 29 5d 0a 2a 2a 20 69 73  terrupt()].** is
14bd0 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20 62 61   running then ba
14be0 64 20 74 68 69 6e 67 73 20 77 69 6c 6c 20 6c 69  d things will li
14bf0 6b 65 6c 79 20 68 61 70 70 65 6e 2e 0a 2a 2f 0a  kely happen..*/.
14c00 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
14c10 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
14c20 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a  t(sqlite3*);../*
14c30 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65  .** CAPI3REF: De
14c40 74 65 72 6d 69 6e 65 20 49 66 20 41 6e 20 53 51  termine If An SQ
14c50 4c 20 53 74 61 74 65 6d 65 6e 74 20 49 73 20 43  L Statement Is C
14c60 6f 6d 70 6c 65 74 65 20 7b 48 31 30 35 31 30 7d  omplete {H10510}
14c70 20 3c 53 37 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S70200>.**.** 
14c80 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
14c90 72 65 20 75 73 65 66 75 6c 20 64 75 72 69 6e 67  re useful during
14ca0 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69 6e   command-line in
14cb0 70 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  put to determine
14cc0 20 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65   if the.** curre
14cd0 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65 78  ntly entered tex
14ce0 74 20 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d 20  t seems to form 
14cf0 61 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73  a complete SQL s
14d00 74 61 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20 69  tatement or.** i
14d10 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 70  f additional inp
14d20 75 74 20 69 73 20 6e 65 65 64 65 64 20 62 65 66  ut is needed bef
14d30 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 68 65 20  ore sending the 
14d40 74 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51 4c  text into.** SQL
14d50 69 74 65 20 66 6f 72 20 70 61 72 73 69 6e 67 2e  ite for parsing.
14d60 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73    These routines
14d70 20 72 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   return 1 if the
14d80 20 69 6e 70 75 74 20 73 74 72 69 6e 67 0a 2a 2a   input string.**
14d90 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 61   appears to be a
14da0 20 63 6f 6d 70 6c 65 74 65 20 53 51 4c 20 73 74   complete SQL st
14db0 61 74 65 6d 65 6e 74 2e 20 20 41 20 73 74 61 74  atement.  A stat
14dc0 65 6d 65 6e 74 20 69 73 20 6a 75 64 67 65 64 20  ement is judged 
14dd0 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74  to be.** complet
14de0 65 20 69 66 20 69 74 20 65 6e 64 73 20 77 69 74  e if it ends wit
14df0 68 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 6f  h a semicolon to
14e00 6b 65 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 61  ken and is not a
14e10 20 70 72 65 66 69 78 20 6f 66 20 61 0a 2a 2a 20   prefix of a.** 
14e20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 43 52 45 41  well-formed CREA
14e30 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65  TE TRIGGER state
14e40 6d 65 6e 74 2e 20 20 53 65 6d 69 63 6f 6c 6f 6e  ment.  Semicolon
14e50 73 20 74 68 61 74 20 61 72 65 20 65 6d 62 65 64  s that are embed
14e60 64 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20 73 74  ded within.** st
14e70 72 69 6e 67 20 6c 69 74 65 72 61 6c 73 20 6f 72  ring literals or
14e80 20 71 75 6f 74 65 64 20 69 64 65 6e 74 69 66 69   quoted identifi
14e90 65 72 20 6e 61 6d 65 73 20 6f 72 20 63 6f 6d 6d  er names or comm
14ea0 65 6e 74 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20  ents are not.** 
14eb0 69 6e 64 65 70 65 6e 64 65 6e 74 20 74 6f 6b 65  independent toke
14ec0 6e 73 20 28 74 68 65 79 20 61 72 65 20 70 61 72  ns (they are par
14ed0 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69  t of the token i
14ee0 6e 20 77 68 69 63 68 20 74 68 65 79 20 61 72 65  n which they are
14ef0 0a 2a 2a 20 65 6d 62 65 64 64 65 64 29 20 61 6e  .** embedded) an
14f00 64 20 74 68 75 73 20 64 6f 20 6e 6f 74 20 63 6f  d thus do not co
14f10 75 6e 74 20 61 73 20 61 20 73 74 61 74 65 6d 65  unt as a stateme
14f20 6e 74 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20  nt terminator.  
14f30 57 68 69 74 65 73 70 61 63 65 0a 2a 2a 20 61 6e  Whitespace.** an
14f40 64 20 63 6f 6d 6d 65 6e 74 73 20 74 68 61 74 20  d comments that 
14f50 66 6f 6c 6c 6f 77 20 74 68 65 20 66 69 6e 61 6c  follow the final
14f60 20 73 65 6d 69 63 6f 6c 6f 6e 20 61 72 65 20 69   semicolon are i
14f70 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  gnored..**.** Th
14f80 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74  ese routines ret
14f90 75 72 6e 20 30 20 69 66 20 74 68 65 20 73 74 61  urn 0 if the sta
14fa0 74 65 6d 65 6e 74 20 69 73 20 69 6e 63 6f 6d 70  tement is incomp
14fb0 6c 65 74 65 2e 20 20 49 66 20 61 0a 2a 2a 20 6d  lete.  If a.** m
14fc0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
14fd0 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53 51 4c   fails, then SQL
14fe0 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74  ITE_NOMEM is ret
14ff0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urned..**.** The
15000 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 20 6e  se routines do n
15010 6f 74 20 70 61 72 73 65 20 74 68 65 20 53 51 4c  ot parse the SQL
15020 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68 75 73   statements thus
15030 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 64 65 74  .** will not det
15040 65 63 74 20 73 79 6e 74 61 63 74 69 63 61 6c 6c  ect syntacticall
15050 79 20 69 6e 63 6f 72 72 65 63 74 20 53 51 4c 2e  y incorrect SQL.
15060 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65  .**.** If SQLite
15070 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 69 6e   has not been in
15080 69 74 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20  itialized using 
15090 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c  [sqlite3_initial
150a0 69 7a 65 28 29 5d 20 70 72 69 6f 72 20 0a 2a 2a  ize()] prior .**
150b0 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c   to invoking sql
150c0 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28  ite3_complete16(
150d0 29 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 69  ) then sqlite3_i
150e0 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69  nitialize() is i
150f0 6e 76 6f 6b 65 64 0a 2a 2a 20 61 75 74 6f 6d 61  nvoked.** automa
15100 74 69 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74  tically by sqlit
15110 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 2e  e3_complete16().
15120 20 20 49 66 20 74 68 61 74 20 69 6e 69 74 69 61    If that initia
15130 6c 69 7a 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a  lization fails,.
15140 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 74 75  ** then the retu
15150 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71  rn value from sq
15160 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36  lite3_complete16
15170 28 29 20 77 69 6c 6c 20 62 65 20 6e 6f 6e 2d 7a  () will be non-z
15180 65 72 6f 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73  ero.** regardles
15190 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20  s of whether or 
151a0 6e 6f 74 20 74 68 65 20 69 6e 70 75 74 20 53 51  not the input SQ
151b0 4c 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a  L is complete..*
151c0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
151d0 73 3a 20 5b 48 31 30 35 31 31 5d 20 5b 48 31 30  s: [H10511] [H10
151e0 35 31 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  512].**.** The i
151f0 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  nput to [sqlite3
15200 5f 63 6f 6d 70 6c 65 74 65 28 29 5d 20 6d 75 73  _complete()] mus
15210 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 65 72 6d  t be a zero-term
15220 69 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 38 20  inated.** UTF-8 
15230 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  string..**.** Th
15240 65 20 69 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69  e input to [sqli
15250 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29  te3_complete16()
15260 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65 72 6f  ] must be a zero
15270 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55  -terminated.** U
15280 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69 6e 20  TF-16 string in 
15290 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
152a0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
152b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6d   int sqlite3_com
152c0 70 6c 65 74 65 28 63 6f 6e 73 74 20 63 68 61 72  plete(const char
152d0 20 2a 73 71 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   *sql);.SQLITE_A
152e0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
152f0 6f 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20  omplete16(const 
15300 76 6f 69 64 20 2a 73 71 6c 29 3b 0a 0a 2f 2a 0a  void *sql);../*.
15310 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67  ** CAPI3REF: Reg
15320 69 73 74 65 72 20 41 20 43 61 6c 6c 62 61 63 6b  ister A Callback
15330 20 54 6f 20 48 61 6e 64 6c 65 20 53 51 4c 49 54   To Handle SQLIT
15340 45 5f 42 55 53 59 20 45 72 72 6f 72 73 20 7b 48  E_BUSY Errors {H
15350 31 32 33 31 30 7d 20 3c 53 34 30 34 30 30 3e 0a  12310} <S40400>.
15360 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
15370 6e 65 20 73 65 74 73 20 61 20 63 61 6c 6c 62 61  ne sets a callba
15380 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  ck function that
15390 20 6d 69 67 68 74 20 62 65 20 69 6e 76 6f 6b 65   might be invoke
153a0 64 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 61 6e  d whenever.** an
153b0 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
153c0 20 74 6f 20 6f 70 65 6e 20 61 20 64 61 74 61 62   to open a datab
153d0 61 73 65 20 74 61 62 6c 65 20 74 68 61 74 20 61  ase table that a
153e0 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a 2a  nother thread.**
153f0 20 6f 72 20 70 72 6f 63 65 73 73 20 68 61 73 20   or process has 
15400 6c 6f 63 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  locked..**.** If
15410 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61   the busy callba
15420 63 6b 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  ck is NULL, then
15430 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f   [SQLITE_BUSY] o
15440 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  r [SQLITE_IOERR_
15450 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 69 73 20 72  BLOCKED].** is r
15460 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74  eturned immediat
15470 65 6c 79 20 75 70 6f 6e 20 65 6e 63 6f 75 6e 74  ely upon encount
15480 65 72 69 6e 67 20 74 68 65 20 6c 6f 63 6b 2e 20  ering the lock. 
15490 49 66 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  If the busy call
154a0 62 61 63 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e  back.** is not N
154b0 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 63 61  ULL, then the ca
154c0 6c 6c 62 61 63 6b 20 77 69 6c 6c 20 62 65 20 69  llback will be i
154d0 6e 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20  nvoked with two 
154e0 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  arguments..**.**
154f0 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
15500 65 6e 74 20 74 6f 20 74 68 65 20 68 61 6e 64 6c  ent to the handl
15510 65 72 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20  er is a copy of 
15520 74 68 65 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65  the void* pointe
15530 72 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 68  r which.** is th
15540 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
15550 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 75 73 79   to sqlite3_busy
15560 5f 68 61 6e 64 6c 65 72 28 29 2e 20 20 54 68 65  _handler().  The
15570 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
15580 20 74 6f 0a 2a 2a 20 74 68 65 20 68 61 6e 64 6c   to.** the handl
15590 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 74  er callback is t
155a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d  he number of tim
155b0 65 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79  es that the busy
155c0 20 68 61 6e 64 6c 65 72 20 68 61 73 0a 2a 2a 20   handler has.** 
155d0 62 65 65 6e 20 69 6e 76 6f 6b 65 64 20 66 6f 72  been invoked for
155e0 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 65 76   this locking ev
155f0 65 6e 74 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  ent.  If the.** 
15600 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65  busy callback re
15610 74 75 72 6e 73 20 30 2c 20 74 68 65 6e 20 6e 6f  turns 0, then no
15620 20 61 64 64 69 74 69 6f 6e 61 6c 20 61 74 74 65   additional atte
15630 6d 70 74 73 20 61 72 65 20 6d 61 64 65 20 74 6f  mpts are made to
15640 0a 2a 2a 20 61 63 63 65 73 73 20 74 68 65 20 64  .** access the d
15650 61 74 61 62 61 73 65 20 61 6e 64 20 5b 53 51 4c  atabase and [SQL
15660 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51  ITE_BUSY] or [SQ
15670 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
15680 45 44 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ED] is returned.
15690 0a 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62  .** If the callb
156a0 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d  ack returns non-
156b0 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e 6f 74 68  zero, then anoth
156c0 65 72 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69 73  er attempt.** is
156d0 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 74 68   made to open th
156e0 65 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 72  e database for r
156f0 65 61 64 69 6e 67 20 61 6e 64 20 74 68 65 20 63  eading and the c
15700 79 63 6c 65 20 72 65 70 65 61 74 73 2e 0a 2a 2a  ycle repeats..**
15710 0a 2a 2a 20 54 68 65 20 70 72 65 73 65 6e 63 65  .** The presence
15720 20 6f 66 20 61 20 62 75 73 79 20 68 61 6e 64 6c   of a busy handl
15730 65 72 20 64 6f 65 73 20 6e 6f 74 20 67 75 61 72  er does not guar
15740 61 6e 74 65 65 20 74 68 61 74 20 69 74 20 77 69  antee that it wi
15750 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a  ll be invoked.**
15760 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 6c   when there is l
15770 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 20  ock contention. 
15780 49 66 20 53 51 4c 69 74 65 20 64 65 74 65 72 6d  If SQLite determ
15790 69 6e 65 73 20 74 68 61 74 20 69 6e 76 6f 6b 69  ines that invoki
157a0 6e 67 20 74 68 65 20 62 75 73 79 0a 2a 2a 20 68  ng the busy.** h
157b0 61 6e 64 6c 65 72 20 63 6f 75 6c 64 20 72 65 73  andler could res
157c0 75 6c 74 20 69 6e 20 61 20 64 65 61 64 6c 6f 63  ult in a deadloc
157d0 6b 2c 20 69 74 20 77 69 6c 6c 20 67 6f 20 61 68  k, it will go ah
157e0 65 61 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b  ead and return [
157f0 53 51 4c 49 54 45 5f 42 55 53 59 5d 0a 2a 2a 20  SQLITE_BUSY].** 
15800 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  or [SQLITE_IOERR
15810 5f 42 4c 4f 43 4b 45 44 5d 20 69 6e 73 74 65 61  _BLOCKED] instea
15820 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68  d of invoking th
15830 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a  e busy handler..
15840 2a 2a 20 43 6f 6e 73 69 64 65 72 20 61 20 73 63  ** Consider a sc
15850 65 6e 61 72 69 6f 20 77 68 65 72 65 20 6f 6e 65  enario where one
15860 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64   process is hold
15870 69 6e 67 20 61 20 72 65 61 64 20 6c 6f 63 6b 20  ing a read lock 
15880 74 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 74 72  that.** it is tr
15890 79 69 6e 67 20 74 6f 20 70 72 6f 6d 6f 74 65 20  ying to promote 
158a0 74 6f 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f  to a reserved lo
158b0 63 6b 20 61 6e 64 0a 2a 2a 20 61 20 73 65 63 6f  ck and.** a seco
158c0 6e 64 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f  nd process is ho
158d0 6c 64 69 6e 67 20 61 20 72 65 73 65 72 76 65 64  lding a reserved
158e0 20 6c 6f 63 6b 20 74 68 61 74 20 69 74 20 69 73   lock that it is
158f0 20 74 72 79 69 6e 67 0a 2a 2a 20 74 6f 20 70 72   trying.** to pr
15900 6f 6d 6f 74 65 20 74 6f 20 61 6e 20 65 78 63 6c  omote to an excl
15910 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 54 68 65  usive lock.  The
15920 20 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 63   first process c
15930 61 6e 6e 6f 74 20 70 72 6f 63 65 65 64 0a 2a 2a  annot proceed.**
15940 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 62   because it is b
15950 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 20 73 65  locked by the se
15960 63 6f 6e 64 20 61 6e 64 20 74 68 65 20 73 65 63  cond and the sec
15970 6f 6e 64 20 70 72 6f 63 65 73 73 20 63 61 6e 6e  ond process cann
15980 6f 74 0a 2a 2a 20 70 72 6f 63 65 65 64 20 62 65  ot.** proceed be
15990 63 61 75 73 65 20 69 74 20 69 73 20 62 6c 6f 63  cause it is bloc
159a0 6b 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74  ked by the first
159b0 2e 20 20 49 66 20 62 6f 74 68 20 70 72 6f 63 65  .  If both proce
159c0 73 73 65 73 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74  sses.** invoke t
159d0 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73  he busy handlers
159e0 2c 20 6e 65 69 74 68 65 72 20 77 69 6c 6c 20 6d  , neither will m
159f0 61 6b 65 20 61 6e 79 20 70 72 6f 67 72 65 73 73  ake any progress
15a00 2e 20 20 54 68 65 72 65 66 6f 72 65 2c 0a 2a 2a  .  Therefore,.**
15a10 20 53 51 4c 69 74 65 20 72 65 74 75 72 6e 73 20   SQLite returns 
15a20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 66 6f  [SQLITE_BUSY] fo
15a30 72 20 74 68 65 20 66 69 72 73 74 20 70 72 6f 63  r the first proc
15a40 65 73 73 2c 20 68 6f 70 69 6e 67 20 74 68 61 74  ess, hoping that
15a50 20 74 68 69 73 0a 2a 2a 20 77 69 6c 6c 20 69 6e   this.** will in
15a60 64 75 63 65 20 74 68 65 20 66 69 72 73 74 20 70  duce the first p
15a70 72 6f 63 65 73 73 20 74 6f 20 72 65 6c 65 61 73  rocess to releas
15a80 65 20 69 74 73 20 72 65 61 64 20 6c 6f 63 6b 20  e its read lock 
15a90 61 6e 64 20 61 6c 6c 6f 77 0a 2a 2a 20 74 68 65  and allow.** the
15aa0 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73 20   second process 
15ab0 74 6f 20 70 72 6f 63 65 65 64 2e 0a 2a 2a 0a 2a  to proceed..**.*
15ac0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 62 75  * The default bu
15ad0 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e  sy callback is N
15ae0 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  ULL..**.** The [
15af0 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72  SQLITE_BUSY] err
15b00 6f 72 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20  or is converted 
15b10 74 6f 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  to [SQLITE_IOERR
15b20 5f 42 4c 4f 43 4b 45 44 5d 0a 2a 2a 20 77 68 65  _BLOCKED].** whe
15b30 6e 20 53 51 4c 69 74 65 20 69 73 20 69 6e 20 74  n SQLite is in t
15b40 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20 6c  he middle of a l
15b50 61 72 67 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  arge transaction
15b60 20 77 68 65 72 65 20 61 6c 6c 20 74 68 65 0a 2a   where all the.*
15b70 2a 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 6e  * changes will n
15b80 6f 74 20 66 69 74 20 69 6e 74 6f 20 74 68 65 20  ot fit into the 
15b90 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e  in-memory cache.
15ba0 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a 2a    SQLite will.**
15bb0 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20   already hold a 
15bc0 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e  RESERVED lock on
15bd0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
15be0 6c 65 2c 20 62 75 74 20 69 74 20 6e 65 65 64 73  le, but it needs
15bf0 0a 2a 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74  .** to promote t
15c00 68 69 73 20 6c 6f 63 6b 20 74 6f 20 45 58 43 4c  his lock to EXCL
15c10 55 53 49 56 45 20 73 6f 20 74 68 61 74 20 69 74  USIVE so that it
15c20 20 63 61 6e 20 73 70 69 6c 6c 20 63 61 63 68 65   can spill cache
15c30 0a 2a 2a 20 70 61 67 65 73 20 69 6e 74 6f 20 74  .** pages into t
15c40 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
15c50 20 77 69 74 68 6f 75 74 20 68 61 72 6d 20 74 6f   without harm to
15c60 20 63 6f 6e 63 75 72 72 65 6e 74 0a 2a 2a 20 72   concurrent.** r
15c70 65 61 64 65 72 73 2e 20 20 49 66 20 69 74 20 69  eaders.  If it i
15c80 73 20 75 6e 61 62 6c 65 20 74 6f 20 70 72 6f 6d  s unable to prom
15c90 6f 74 65 20 74 68 65 20 6c 6f 63 6b 2c 20 74 68  ote the lock, th
15ca0 65 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  en the in-memory
15cb0 0a 2a 2a 20 63 61 63 68 65 20 77 69 6c 6c 20 62  .** cache will b
15cc0 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63  e left in an inc
15cd0 6f 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 20  onsistent state 
15ce0 61 6e 64 20 73 6f 20 74 68 65 20 65 72 72 6f 72  and so the error
15cf0 0a 2a 2a 20 63 6f 64 65 20 69 73 20 70 72 6f 6d  .** code is prom
15d00 6f 74 65 64 20 66 72 6f 6d 20 74 68 65 20 72 65  oted from the re
15d10 6c 61 74 69 76 65 6c 79 20 62 65 6e 69 67 6e 20  latively benign 
15d20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 74 6f  [SQLITE_BUSY] to
15d30 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73 65 76  .** the more sev
15d40 65 72 65 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52  ere [SQLITE_IOER
15d50 52 5f 42 4c 4f 43 4b 45 44 5d 2e 20 20 54 68 69  R_BLOCKED].  Thi
15d60 73 20 65 72 72 6f 72 20 63 6f 64 65 20 70 72 6f  s error code pro
15d70 6d 6f 74 69 6f 6e 0a 2a 2a 20 66 6f 72 63 65 73  motion.** forces
15d80 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f   an automatic ro
15d90 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 63 68  llback of the ch
15da0 61 6e 67 65 73 2e 20 20 53 65 65 20 74 68 65 0a  anges.  See the.
15db0 2a 2a 20 3c 61 20 68 72 65 66 3d 22 2f 63 76 73  ** <a href="/cvs
15dc0 74 72 61 63 2f 77 69 6b 69 3f 70 3d 43 6f 72 72  trac/wiki?p=Corr
15dd0 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e 67 42  uptionFollowingB
15de0 75 73 79 45 72 72 6f 72 22 3e 0a 2a 2a 20 43 6f  usyError">.** Co
15df0 72 72 75 70 74 69 6f 6e 46 6f 6c 6c 6f 77 69 6e  rruptionFollowin
15e00 67 42 75 73 79 45 72 72 6f 72 3c 2f 61 3e 20 77  gBusyError</a> w
15e10 69 6b 69 20 70 61 67 65 20 66 6f 72 20 61 20 64  iki page for a d
15e20 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 77 68 79  iscussion of why
15e30 0a 2a 2a 20 74 68 69 73 20 69 73 20 69 6d 70 6f  .** this is impo
15e40 72 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  rtant..**.** The
15e50 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 61  re can only be a
15e60 20 73 69 6e 67 6c 65 20 62 75 73 79 20 68 61 6e   single busy han
15e70 64 6c 65 72 20 64 65 66 69 6e 65 64 20 66 6f 72  dler defined for
15e80 20 65 61 63 68 0a 2a 2a 20 5b 64 61 74 61 62 61   each.** [databa
15e90 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20  se connection]. 
15ea0 20 53 65 74 74 69 6e 67 20 61 20 6e 65 77 20 62   Setting a new b
15eb0 75 73 79 20 68 61 6e 64 6c 65 72 20 63 6c 65 61  usy handler clea
15ec0 72 73 20 61 6e 79 0a 2a 2a 20 70 72 65 76 69 6f  rs any.** previo
15ed0 75 73 6c 79 20 73 65 74 20 68 61 6e 64 6c 65 72  usly set handler
15ee0 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 63 61 6c  .  Note that cal
15ef0 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 62 75  ling [sqlite3_bu
15f00 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 0a 2a 2a  sy_timeout()].**
15f10 20 77 69 6c 6c 20 61 6c 73 6f 20 73 65 74 20 6f   will also set o
15f20 72 20 63 6c 65 61 72 20 74 68 65 20 62 75 73 79  r clear the busy
15f30 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20   handler..**.** 
15f40 54 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63  The busy callbac
15f50 6b 20 73 68 6f 75 6c 64 20 6e 6f 74 20 74 61 6b  k should not tak
15f60 65 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 77 68  e any actions wh
15f70 69 63 68 20 6d 6f 64 69 66 79 20 74 68 65 0a 2a  ich modify the.*
15f80 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
15f90 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b  ction that invok
15fa0 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  ed the busy hand
15fb0 6c 65 72 2e 20 20 41 6e 79 20 73 75 63 68 20 61  ler.  Any such a
15fc0 63 74 69 6f 6e 73 0a 2a 2a 20 72 65 73 75 6c 74  ctions.** result
15fd0 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 62 65   in undefined be
15fe0 68 61 76 69 6f 72 2e 0a 2a 2a 20 0a 2a 2a 20 52  havior..** .** R
15ff0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
16000 5b 48 31 32 33 31 31 5d 20 5b 48 31 32 33 31 32  [H12311] [H12312
16010 5d 20 5b 48 31 32 33 31 34 5d 20 5b 48 31 32 33  ] [H12314] [H123
16020 31 36 5d 20 5b 48 31 32 33 31 38 5d 0a 2a 2a 0a  16] [H12318].**.
16030 2a 2a 20 41 20 62 75 73 79 20 68 61 6e 64 6c 65  ** A busy handle
16040 72 20 6d 75 73 74 20 6e 6f 74 20 63 6c 6f 73 65  r must not close
16050 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
16060 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f 72 20 5b  nnection.** or [
16070 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
16080 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f 6b 65 64  nt] that invoked
16090 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
160a0 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
160b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73   int sqlite3_bus
160c0 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65  y_handler(sqlite
160d0 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a  3*, int(*)(void*
160e0 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a  ,int), void*);..
160f0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
16100 53 65 74 20 41 20 42 75 73 79 20 54 69 6d 65 6f  Set A Busy Timeo
16110 75 74 20 7b 48 31 32 33 34 30 7d 20 3c 53 34 30  ut {H12340} <S40
16120 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  410>.**.** This 
16130 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20 5b  routine sets a [
16140 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e  sqlite3_busy_han
16150 64 6c 65 72 20 7c 20 62 75 73 79 20 68 61 6e 64  dler | busy hand
16160 6c 65 72 5d 20 74 68 61 74 20 73 6c 65 65 70 73  ler] that sleeps
16170 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65 63 69 66  .** for a specif
16180 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69  ied amount of ti
16190 6d 65 20 77 68 65 6e 20 61 20 74 61 62 6c 65 20  me when a table 
161a0 69 73 20 6c 6f 63 6b 65 64 2e 20 20 54 68 65 20  is locked.  The 
161b0 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69 6c 6c 20  handler.** will 
161c0 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c 65 20 74  sleep multiple t
161d0 69 6d 65 73 20 75 6e 74 69 6c 20 61 74 20 6c 65  imes until at le
161e0 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c 69 73 65  ast "ms" millise
161f0 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 69 6e  conds of sleepin
16200 67 0a 2a 2a 20 68 61 76 65 20 61 63 63 75 6d 75  g.** have accumu
16210 6c 61 74 65 64 2e 20 7b 48 31 32 33 34 33 7d 20  lated. {H12343} 
16220 41 66 74 65 72 20 22 6d 73 22 20 6d 69 6c 6c 69  After "ms" milli
16230 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70  seconds of sleep
16240 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 68 61 6e 64  ing,.** the hand
16250 6c 65 72 20 72 65 74 75 72 6e 73 20 30 20 77 68  ler returns 0 wh
16260 69 63 68 20 63 61 75 73 65 73 20 5b 73 71 6c 69  ich causes [sqli
16270 74 65 33 5f 73 74 65 70 28 29 5d 20 74 6f 20 72  te3_step()] to r
16280 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45  eturn.** [SQLITE
16290 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54  _BUSY] or [SQLIT
162a0 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d  E_IOERR_BLOCKED]
162b0 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20  ..**.** Calling 
162c0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74  this routine wit
162d0 68 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 6c 65  h an argument le
162e0 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
162f0 20 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74 75 72 6e   to zero.** turn
16300 73 20 6f 66 66 20 61 6c 6c 20 62 75 73 79 20 68  s off all busy h
16310 61 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54  andlers..**.** T
16320 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65  here can only be
16330 20 61 20 73 69 6e 67 6c 65 20 62 75 73 79 20 68   a single busy h
16340 61 6e 64 6c 65 72 20 66 6f 72 20 61 20 70 61 72  andler for a par
16350 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61  ticular.** [data
16360 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
16370 20 61 6e 79 20 61 6e 79 20 67 69 76 65 6e 20 6d   any any given m
16380 6f 6d 65 6e 74 2e 20 20 49 66 20 61 6e 6f 74 68  oment.  If anoth
16390 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a  er busy handler.
163a0 2a 2a 20 77 61 73 20 64 65 66 69 6e 65 64 20 20  ** was defined  
163b0 28 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  (using [sqlite3_
163c0 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 29  busy_handler()])
163d0 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e   prior to callin
163e0 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  g.** this routin
163f0 65 2c 20 74 68 61 74 20 6f 74 68 65 72 20 62 75  e, that other bu
16400 73 79 20 68 61 6e 64 6c 65 72 20 69 73 20 63 6c  sy handler is cl
16410 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  eared..**.** Req
16420 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
16430 31 32 33 34 31 5d 20 5b 48 31 32 33 34 33 5d 20  12341] [H12343] 
16440 5b 48 31 32 33 34 34 5d 0a 2a 2f 0a 53 51 4c 49  [H12344].*/.SQLI
16450 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
16460 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28  e3_busy_timeout(
16470 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 6d 73  sqlite3*, int ms
16480 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
16490 45 46 3a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20  EF: Convenience 
164a0 52 6f 75 74 69 6e 65 73 20 46 6f 72 20 52 75 6e  Routines For Run
164b0 6e 69 6e 67 20 51 75 65 72 69 65 73 20 7b 48 31  ning Queries {H1
164c0 32 33 37 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a  2370} <S10000>.*
164d0 2a 0a 2a 2a 20 44 65 66 69 6e 69 74 69 6f 6e 3a  *.** Definition:
164e0 20 41 20 3c 62 3e 72 65 73 75 6c 74 20 74 61 62   A <b>result tab
164f0 6c 65 3c 2f 62 3e 20 69 73 20 6d 65 6d 6f 72 79  le</b> is memory
16500 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
16510 63 72 65 61 74 65 64 20 62 79 20 74 68 65 0a 2a  created by the.*
16520 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  * [sqlite3_get_t
16530 61 62 6c 65 28 29 5d 20 69 6e 74 65 72 66 61 63  able()] interfac
16540 65 2e 20 20 41 20 72 65 73 75 6c 74 20 74 61 62  e.  A result tab
16550 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a  le records the.*
16560 2a 20 63 6f 6d 70 6c 65 74 65 20 71 75 65 72 79  * complete query
16570 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 6f 6e   results from on
16580 65 20 6f 72 20 6d 6f 72 65 20 71 75 65 72 69 65  e or more querie
16590 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62  s..**.** The tab
165a0 6c 65 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20  le conceptually 
165b0 68 61 73 20 61 20 6e 75 6d 62 65 72 20 6f 66 20  has a number of 
165c0 72 6f 77 73 20 61 6e 64 20 63 6f 6c 75 6d 6e 73  rows and columns
165d0 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 73 65 20  .  But.** these 
165e0 6e 75 6d 62 65 72 73 20 61 72 65 20 6e 6f 74 20  numbers are not 
165f0 70 61 72 74 20 6f 66 20 74 68 65 20 72 65 73 75  part of the resu
16600 6c 74 20 74 61 62 6c 65 20 69 74 73 65 6c 66 2e  lt table itself.
16610 20 20 54 68 65 73 65 0a 2a 2a 20 6e 75 6d 62 65    These.** numbe
16620 72 73 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20  rs are obtained 
16630 73 65 70 61 72 61 74 65 6c 79 2e 20 20 4c 65 74  separately.  Let
16640 20 4e 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72   N be the number
16650 20 6f 66 20 72 6f 77 73 0a 2a 2a 20 61 6e 64 20   of rows.** and 
16660 4d 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20  M be the number 
16670 6f 66 20 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a  of columns..**.*
16680 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c 65  * A result table
16690 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20   is an array of 
166a0 70 6f 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f  pointers to zero
166b0 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
166c0 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 54 68  8 strings..** Th
166d0 65 72 65 20 61 72 65 20 28 4e 2b 31 29 2a 4d 20  ere are (N+1)*M 
166e0 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20  elements in the 
166f0 61 72 72 61 79 2e 20 20 54 68 65 20 66 69 72 73  array.  The firs
16700 74 20 4d 20 70 6f 69 6e 74 65 72 73 20 70 6f 69  t M pointers poi
16710 6e 74 0a 2a 2a 20 74 6f 20 7a 65 72 6f 2d 74 65  nt.** to zero-te
16720 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73  rminated strings
16730 20 74 68 61 74 20 20 63 6f 6e 74 61 69 6e 20 74   that  contain t
16740 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20  he names of the 
16750 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 20 54 68 65 20  columns..** The 
16760 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69 65  remaining entrie
16770 73 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20 71  s all point to q
16780 75 65 72 79 20 72 65 73 75 6c 74 73 2e 20 20 4e  uery results.  N
16790 55 4c 4c 20 76 61 6c 75 65 73 20 72 65 73 75 6c  ULL values resul
167a0 74 0a 2a 2a 20 69 6e 20 4e 55 4c 4c 20 70 6f 69  t.** in NULL poi
167b0 6e 74 65 72 73 2e 20 20 41 6c 6c 20 6f 74 68 65  nters.  All othe
167c0 72 20 76 61 6c 75 65 73 20 61 72 65 20 69 6e 20  r values are in 
167d0 74 68 65 69 72 20 55 54 46 2d 38 20 7a 65 72 6f  their UTF-8 zero
167e0 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73  -terminated.** s
167f0 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
16800 74 69 6f 6e 20 61 73 20 72 65 74 75 72 6e 65 64  tion as returned
16810 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c   by [sqlite3_col
16820 75 6d 6e 5f 74 65 78 74 28 29 5d 2e 0a 2a 2a 0a  umn_text()]..**.
16830 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62 6c  ** A result tabl
16840 65 20 6d 69 67 68 74 20 63 6f 6e 73 69 73 74 20  e might consist 
16850 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6d  of one or more m
16860 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
16870 73 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20  s..** It is not 
16880 73 61 66 65 20 74 6f 20 70 61 73 73 20 61 20 72  safe to pass a r
16890 65 73 75 6c 74 20 74 61 62 6c 65 20 64 69 72 65  esult table dire
168a0 63 74 6c 79 20 74 6f 20 5b 73 71 6c 69 74 65 33  ctly to [sqlite3
168b0 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 20 41 20 72  _free()]..** A r
168c0 65 73 75 6c 74 20 74 61 62 6c 65 20 73 68 6f 75  esult table shou
168d0 6c 64 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65  ld be deallocate
168e0 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
168f0 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a  _free_table()]..
16900 2a 2a 0a 2a 2a 20 41 73 20 61 6e 20 65 78 61 6d  **.** As an exam
16910 70 6c 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c  ple of the resul
16920 74 20 74 61 62 6c 65 20 66 6f 72 6d 61 74 2c 20  t table format, 
16930 73 75 70 70 6f 73 65 20 61 20 71 75 65 72 79 20  suppose a query 
16940 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20 61 73 20  result.** is as 
16950 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c  follows:.**.** <
16960 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
16970 0a 2a 2a 20 20 20 20 20 20 20 20 4e 61 6d 65 20  .**        Name 
16980 20 20 20 20 20 20 20 7c 20 41 67 65 0a 2a 2a 20         | Age.** 
16990 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d         ---------
169a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
169b0 2a 20 20 20 20 20 20 20 20 41 6c 69 63 65 20 20  *        Alice  
169c0 20 20 20 20 20 7c 20 34 33 0a 2a 2a 20 20 20 20       | 43.**    
169d0 20 20 20 20 42 6f 62 20 20 20 20 20 20 20 20 20      Bob         
169e0 7c 20 32 38 0a 2a 2a 20 20 20 20 20 20 20 20 43  | 28.**        C
169f0 69 6e 64 79 20 20 20 20 20 20 20 7c 20 32 31 0a  indy       | 21.
16a00 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b  ** </pre></block
16a10 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  quote>.**.** The
16a20 72 65 20 61 72 65 20 74 77 6f 20 63 6f 6c 75 6d  re are two colum
16a30 6e 20 28 4d 3d 3d 32 29 20 61 6e 64 20 74 68 72  n (M==2) and thr
16a40 65 65 20 72 6f 77 73 20 28 4e 3d 3d 33 29 2e 20  ee rows (N==3). 
16a50 20 54 68 75 73 20 74 68 65 0a 2a 2a 20 72 65 73   Thus the.** res
16a60 75 6c 74 20 74 61 62 6c 65 20 68 61 73 20 38 20  ult table has 8 
16a70 65 6e 74 72 69 65 73 2e 20 20 53 75 70 70 6f 73  entries.  Suppos
16a80 65 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62  e the result tab
16a90 6c 65 20 69 73 20 73 74 6f 72 65 64 0a 2a 2a 20  le is stored.** 
16aa0 69 6e 20 61 6e 20 61 72 72 61 79 20 6e 61 6d 65  in an array name
16ab0 73 20 61 7a 52 65 73 75 6c 74 2e 20 20 54 68 65  s azResult.  The
16ac0 6e 20 61 7a 52 65 73 75 6c 74 20 68 6f 6c 64 73  n azResult holds
16ad0 20 74 68 69 73 20 63 6f 6e 74 65 6e 74 3a 0a 2a   this content:.*
16ae0 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
16af0 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20 20  ><pre>.**       
16b00 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 30 5d   azResult&#91;0]
16b10 20 3d 20 22 4e 61 6d 65 22 3b 0a 2a 2a 20 20 20   = "Name";.**   
16b20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39       azResult&#9
16b30 31 3b 31 5d 20 3d 20 22 41 67 65 22 3b 0a 2a 2a  1;1] = "Age";.**
16b40 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
16b50 26 23 39 31 3b 32 5d 20 3d 20 22 41 6c 69 63 65  &#91;2] = "Alice
16b60 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
16b70 65 73 75 6c 74 26 23 39 31 3b 33 5d 20 3d 20 22  esult&#91;3] = "
16b80 34 33 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61  43";.**        a
16b90 7a 52 65 73 75 6c 74 26 23 39 31 3b 34 5d 20 3d  zResult&#91;4] =
16ba0 20 22 42 6f 62 22 3b 0a 2a 2a 20 20 20 20 20 20   "Bob";.**      
16bb0 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 35    azResult&#91;5
16bc0 5d 20 3d 20 22 32 38 22 3b 0a 2a 2a 20 20 20 20  ] = "28";.**    
16bd0 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31      azResult&#91
16be0 3b 36 5d 20 3d 20 22 43 69 6e 64 79 22 3b 0a 2a  ;6] = "Cindy";.*
16bf0 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c  *        azResul
16c00 74 26 23 39 31 3b 37 5d 20 3d 20 22 32 31 22 3b  t&#91;7] = "21";
16c10 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
16c20 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68  kquote>.**.** Th
16c30 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  e sqlite3_get_ta
16c40 62 6c 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 65  ble() function e
16c50 76 61 6c 75 61 74 65 73 20 6f 6e 65 20 6f 72 20  valuates one or 
16c60 6d 6f 72 65 0a 2a 2a 20 73 65 6d 69 63 6f 6c 6f  more.** semicolo
16c70 6e 2d 73 65 70 61 72 61 74 65 64 20 53 51 4c 20  n-separated SQL 
16c80 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68  statements in th
16c90 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  e zero-terminate
16ca0 64 20 55 54 46 2d 38 0a 2a 2a 20 73 74 72 69 6e  d UTF-8.** strin
16cb0 67 20 6f 66 20 69 74 73 20 32 6e 64 20 70 61 72  g of its 2nd par
16cc0 61 6d 65 74 65 72 2e 20 20 49 74 20 72 65 74 75  ameter.  It retu
16cd0 72 6e 73 20 61 20 72 65 73 75 6c 74 20 74 61 62  rns a result tab
16ce0 6c 65 20 74 6f 20 74 68 65 0a 2a 2a 20 70 6f 69  le to the.** poi
16cf0 6e 74 65 72 20 67 69 76 65 6e 20 69 6e 20 69 74  nter given in it
16d00 73 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 2e  s 3rd parameter.
16d10 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65  .**.** After the
16d20 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f   calling functio
16d30 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 75  n has finished u
16d40 73 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 2c  sing the result,
16d50 20 69 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 70 61   it should.** pa
16d60 73 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  ss the pointer t
16d70 6f 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62  o the result tab
16d80 6c 65 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72  le to sqlite3_fr
16d90 65 65 5f 74 61 62 6c 65 28 29 20 69 6e 20 6f 72  ee_table() in or
16da0 64 65 72 20 74 6f 0a 2a 2a 20 72 65 6c 65 61 73  der to.** releas
16db0 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61  e the memory tha
16dc0 74 20 77 61 73 20 6d 61 6c 6c 6f 63 65 64 2e 20  t was malloced. 
16dd0 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20   Because of the 
16de0 77 61 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69  way the.** [sqli
16df0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 68 61  te3_malloc()] ha
16e00 70 70 65 6e 73 20 77 69 74 68 69 6e 20 73 71 6c  ppens within sql
16e10 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29  ite3_get_table()
16e20 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a  , the calling.**
16e30 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 6e   function must n
16e40 6f 74 20 74 72 79 20 74 6f 20 63 61 6c 6c 20 5b  ot try to call [
16e50 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20  sqlite3_free()] 
16e60 64 69 72 65 63 74 6c 79 2e 20 20 4f 6e 6c 79 0a  directly.  Only.
16e70 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  ** [sqlite3_free
16e80 5f 74 61 62 6c 65 28 29 5d 20 69 73 20 61 62 6c  _table()] is abl
16e90 65 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65  e to release the
16ea0 20 6d 65 6d 6f 72 79 20 70 72 6f 70 65 72 6c 79   memory properly
16eb0 20 61 6e 64 20 73 61 66 65 6c 79 2e 0a 2a 2a 0a   and safely..**.
16ec0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67  ** The sqlite3_g
16ed0 65 74 5f 74 61 62 6c 65 28 29 20 69 6e 74 65 72  et_table() inter
16ee0 66 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65 6e  face is implemen
16ef0 74 65 64 20 61 73 20 61 20 77 72 61 70 70 65 72  ted as a wrapper
16f00 20 61 72 6f 75 6e 64 0a 2a 2a 20 5b 73 71 6c 69   around.** [sqli
16f10 74 65 33 5f 65 78 65 63 28 29 5d 2e 20 20 54 68  te3_exec()].  Th
16f20 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  e sqlite3_get_ta
16f30 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 20 64 6f  ble() routine do
16f40 65 73 20 6e 6f 74 20 68 61 76 65 20 61 63 63 65  es not have acce
16f50 73 73 0a 2a 2a 20 74 6f 20 61 6e 79 20 69 6e 74  ss.** to any int
16f60 65 72 6e 61 6c 20 64 61 74 61 20 73 74 72 75 63  ernal data struc
16f70 74 75 72 65 73 20 6f 66 20 53 51 4c 69 74 65 2e  tures of SQLite.
16f80 20 20 49 74 20 75 73 65 73 20 6f 6e 6c 79 20 74    It uses only t
16f90 68 65 20 70 75 62 6c 69 63 0a 2a 2a 20 69 6e 74  he public.** int
16fa0 65 72 66 61 63 65 20 64 65 66 69 6e 65 64 20 68  erface defined h
16fb0 65 72 65 2e 20 20 41 73 20 61 20 63 6f 6e 73 65  ere.  As a conse
16fc0 71 75 65 6e 63 65 2c 20 65 72 72 6f 72 73 20 74  quence, errors t
16fd0 68 61 74 20 6f 63 63 75 72 20 69 6e 20 74 68 65  hat occur in the
16fe0 0a 2a 2a 20 77 72 61 70 70 65 72 20 6c 61 79 65  .** wrapper laye
16ff0 72 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65  r outside of the
17000 20 69 6e 74 65 72 6e 61 6c 20 5b 73 71 6c 69 74   internal [sqlit
17010 65 33 5f 65 78 65 63 28 29 5d 20 63 61 6c 6c 20  e3_exec()] call 
17020 61 72 65 20 6e 6f 74 0a 2a 2a 20 72 65 66 6c 65  are not.** refle
17030 63 74 65 64 20 69 6e 20 73 75 62 73 65 71 75 65  cted in subseque
17040 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c  nt calls to [sql
17050 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20  ite3_errcode()] 
17060 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  or [sqlite3_errm
17070 73 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  sg()]..**.** Req
17080 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
17090 31 32 33 37 31 5d 20 5b 48 31 32 33 37 33 5d 20  12371] [H12373] 
170a0 5b 48 31 32 33 37 34 5d 20 5b 48 31 32 33 37 36  [H12374] [H12376
170b0 5d 20 5b 48 31 32 33 37 39 5d 20 5b 48 31 32 33  ] [H12379] [H123
170c0 38 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  82].*/.SQLITE_AP
170d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65  I int sqlite3_ge
170e0 74 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74  t_table(.  sqlit
170f0 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
17100 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61   /* An open data
17110 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  base */.  const 
17120 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
17130 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61  /* SQL to be eva
17140 6c 75 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72  luated */.  char
17150 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20   ***pazResult,  
17160 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20    /* Results of 
17170 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69  the query */.  i
17180 6e 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20  nt *pnRow,      
17190 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
171a0 66 20 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72  f result rows wr
171b0 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20  itten here */.  
171c0 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20  int *pnColumn,  
171d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
171e0 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  of result column
171f0 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a  s written here *
17200 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
17210 6d 73 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72  msg       /* Err
17220 6f 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68  or msg written h
17230 65 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45  ere */.);.SQLITE
17240 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
17250 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61  3_free_table(cha
17260 72 20 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a  r **result);../*
17270 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f  .** CAPI3REF: Fo
17280 72 6d 61 74 74 65 64 20 53 74 72 69 6e 67 20 50  rmatted String P
17290 72 69 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e  rinting Function
172a0 73 20 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30  s {H17400} <S700
172b0 30 30 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a  00><S20000>.**.*
172c0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
172d0 20 61 72 65 20 77 6f 72 6b 2d 61 6c 69 6b 65 73   are work-alikes
172e0 20 6f 66 20 74 68 65 20 22 70 72 69 6e 74 66 28   of the "printf(
172f0 29 22 20 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e  )" family of fun
17300 63 74 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74  ctions.** from t
17310 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69  he standard C li
17320 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  brary..**.** The
17330 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
17340 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76  () and sqlite3_v
17350 6d 70 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e  mprintf() routin
17360 65 73 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a  es write their.*
17370 2a 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d  * results into m
17380 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
17390 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
173a0 6c 6f 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73  loc()]..** The s
173b0 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20  trings returned 
173c0 62 79 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75  by these two rou
173d0 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a  tines should be.
173e0 2a 2a 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b  ** released by [
173f0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e  sqlite3_free()].
17400 20 20 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20    Both routines 
17410 72 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c  return a.** NULL
17420 20 70 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c   pointer if [sql
17430 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69  ite3_malloc()] i
17440 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f  s unable to allo
17450 63 61 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d  cate enough.** m
17460 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68  emory to hold th
17470 65 20 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69  e resulting stri
17480 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c  ng..**.** In sql
17490 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20  ite3_snprintf() 
174a0 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c  routine is simil
174b0 61 72 20 74 6f 20 22 73 6e 70 72 69 6e 74 66 28  ar to "snprintf(
174c0 29 22 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73  )" from.** the s
174d0 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72  tandard C librar
174e0 79 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69  y.  The result i
174f0 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  s written into t
17500 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 73 75 70  he.** buffer sup
17510 70 6c 69 65 64 20 61 73 20 74 68 65 20 73 65 63  plied as the sec
17520 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 77 68  ond parameter wh
17530 6f 73 65 20 73 69 7a 65 20 69 73 20 67 69 76 65  ose size is give
17540 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 69 72 73  n by.** the firs
17550 74 20 70 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74  t parameter. Not
17560 65 20 74 68 61 74 20 74 68 65 20 6f 72 64 65 72  e that the order
17570 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74   of the.** first
17580 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72 73 20   two parameters 
17590 69 73 20 72 65 76 65 72 73 65 64 20 66 72 6f 6d  is reversed from
175a0 20 73 6e 70 72 69 6e 74 66 28 29 2e 20 20 54 68   snprintf().  Th
175b0 69 73 20 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74  is is an.** hist
175c0 6f 72 69 63 61 6c 20 61 63 63 69 64 65 6e 74 20  orical accident 
175d0 74 68 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 66  that cannot be f
175e0 69 78 65 64 20 77 69 74 68 6f 75 74 20 62 72 65  ixed without bre
175f0 61 6b 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72  aking.** backwar
17600 64 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  ds compatibility
17610 2e 20 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61  .  Note also tha
17620 74 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e  t sqlite3_snprin
17630 74 66 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20  tf().** returns 
17640 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73  a pointer to its
17650 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20   buffer instead 
17660 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
17670 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 61  .** characters a
17680 63 74 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20  ctually written 
17690 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e  into the buffer.
176a0 20 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 0a    We admit that.
176b0 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
176c0 20 63 68 61 72 61 63 74 65 72 73 20 77 72 69 74   characters writ
176d0 74 65 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d  ten would be a m
176e0 6f 72 65 20 75 73 65 66 75 6c 20 72 65 74 75 72  ore useful retur
176f0 6e 0a 2a 2a 20 76 61 6c 75 65 20 62 75 74 20 77  n.** value but w
17700 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20  e cannot change 
17710 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
17720 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e  on of sqlite3_sn
17730 70 72 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20  printf().** now 
17740 77 69 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67  without breaking
17750 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a   compatibility..
17760 2a 2a 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73  **.** As long as
17770 20 74 68 65 20 62 75 66 66 65 72 20 73 69 7a 65   the buffer size
17780 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
17790 20 7a 65 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73   zero, sqlite3_s
177a0 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61  nprintf().** gua
177b0 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68 65  rantees that the
177c0 20 62 75 66 66 65 72 20 69 73 20 61 6c 77 61 79   buffer is alway
177d0 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  s zero-terminate
177e0 64 2e 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a  d.  The first.**
177f0 20 70 61 72 61 6d 65 74 65 72 20 22 6e 22 20 69   parameter "n" i
17800 73 20 74 68 65 20 74 6f 74 61 6c 20 73 69 7a 65  s the total size
17810 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 2c 20   of the buffer, 
17820 69 6e 63 6c 75 64 69 6e 67 20 73 70 61 63 65 20  including space 
17830 66 6f 72 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20  for.** the zero 
17840 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20  terminator.  So 
17850 74 68 65 20 6c 6f 6e 67 65 73 74 20 73 74 72 69  the longest stri
17860 6e 67 20 74 68 61 74 20 63 61 6e 20 62 65 20 63  ng that can be c
17870 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69  ompletely.** wri
17880 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31  tten will be n-1
17890 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a   characters..**.
178a0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
178b0 73 20 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20  s all implement 
178c0 73 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20  some additional 
178d0 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70  formatting.** op
178e0 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75  tions that are u
178f0 73 65 66 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72  seful for constr
17900 75 63 74 69 6e 67 20 53 51 4c 20 73 74 61 74 65  ucting SQL state
17910 6d 65 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66  ments..** All of
17920 20 74 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74   the usual print
17930 66 28 29 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f  f() formatting o
17940 70 74 69 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49  ptions apply.  I
17950 6e 20 61 64 64 69 74 69 6f 6e 2c 20 74 68 65 72  n addition, ther
17960 65 0a 2a 2a 20 69 73 20 61 72 65 20 22 25 71 22  e.** is are "%q"
17970 2c 20 22 25 51 22 2c 20 61 6e 64 20 22 25 7a 22  , "%Q", and "%z"
17980 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20   options..**.** 
17990 54 68 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f  The %q option wo
179a0 72 6b 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74  rks like %s in t
179b0 68 61 74 20 69 74 20 73 75 62 73 74 69 74 75 74  hat it substitut
179c0 65 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e  es a null-termin
179d0 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66  ated.** string f
179e0 72 6f 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74  rom the argument
179f0 20 6c 69 73 74 2e 20 20 42 75 74 20 25 71 20 61   list.  But %q a
17a00 6c 73 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72  lso doubles ever
17a10 79 20 27 5c 27 27 20 63 68 61 72 61 63 74 65 72  y '\'' character
17a20 2e 0a 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67  ..** %q is desig
17a30 6e 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69  ned for use insi
17a40 64 65 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65  de a string lite
17a50 72 61 6c 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e  ral.  By doublin
17a60 67 20 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63  g each '\''.** c
17a70 68 61 72 61 63 74 65 72 20 69 74 20 65 73 63 61  haracter it esca
17a80 70 65 73 20 74 68 61 74 20 63 68 61 72 61 63 74  pes that charact
17a90 65 72 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74  er and allows it
17aa0 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20   to be inserted 
17ab0 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69  into.** the stri
17ac0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78  ng..**.** For ex
17ad0 61 6d 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68  ample, assume th
17ae0 65 20 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c  e string variabl
17af0 65 20 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73  e zText contains
17b00 20 74 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73   text as follows
17b10 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
17b20 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68  ote><pre>.**  ch
17b30 61 72 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27  ar *zText = "It'
17b40 73 20 61 20 68 61 70 70 79 20 64 61 79 21 22 3b  s a happy day!";
17b50 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63  .** </pre></bloc
17b60 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e  kquote>.**.** On
17b70 65 20 63 61 6e 20 75 73 65 20 74 68 69 73 20 74  e can use this t
17b80 65 78 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74  ext in an SQL st
17b90 61 74 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f  atement as follo
17ba0 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  ws:.**.** <block
17bb0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
17bc0 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c  char *zSQL = sql
17bd0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e  ite3_mprintf("IN
17be0 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20  SERT INTO table 
17bf0 56 41 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a  VALUES('%q')", z
17c00 54 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74  Text);.**  sqlit
17c10 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c  e3_exec(db, zSQL
17c20 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20  , 0, 0, 0);.**  
17c30 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51  sqlite3_free(zSQ
17c40 4c 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  L);.** </pre></b
17c50 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
17c60 20 42 65 63 61 75 73 65 20 74 68 65 20 25 71 20   Because the %q 
17c70 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73  format string is
17c80 20 75 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20   used, the '\'' 
17c90 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65  character in zTe
17ca0 78 74 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64  xt.** is escaped
17cb0 20 61 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e   and the SQL gen
17cc0 65 72 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c  erated is as fol
17cd0 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  lows:.**.** <blo
17ce0 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
17cf0 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61    INSERT INTO ta
17d00 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49 74 27  ble1 VALUES('It'
17d10 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21 27  's a happy day!'
17d20 29 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  ).** </pre></blo
17d30 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54  ckquote>.**.** T
17d40 68 69 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20  his is correct. 
17d50 20 48 61 64 20 77 65 20 75 73 65 64 20 25 73 20   Had we used %s 
17d60 69 6e 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74  instead of %q, t
17d70 68 65 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c  he generated SQL
17d80 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c  .** would have l
17d90 6f 6f 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a  ooked like this:
17da0 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
17db0 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53  te><pre>.**  INS
17dc0 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20  ERT INTO table1 
17dd0 56 41 4c 55 45 53 28 27 49 74 27 73 20 61 20 68  VALUES('It's a h
17de0 61 70 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20  appy day!');.** 
17df0 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f  </pre></blockquo
17e00 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73  te>.**.** This s
17e10 65 63 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73  econd example is
17e20 20 61 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65   an SQL syntax e
17e30 72 72 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65  rror.  As a gene
17e40 72 61 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f  ral rule you sho
17e50 75 6c 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73  uld.** always us
17e60 65 20 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20  e %q instead of 
17e70 25 73 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e  %s when insertin
17e80 67 20 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74  g text into a st
17e90 72 69 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a  ring literal..**
17ea0 0a 2a 2a 20 54 68 65 20 25 51 20 6f 70 74 69 6f  .** The %Q optio
17eb0 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20  n works like %q 
17ec0 65 78 63 65 70 74 20 69 74 20 61 6c 73 6f 20 61  except it also a
17ed0 64 64 73 20 73 69 6e 67 6c 65 20 71 75 6f 74 65  dds single quote
17ee0 73 20 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20  s around.** the 
17ef0 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 74  outside of the t
17f00 6f 74 61 6c 20 73 74 72 69 6e 67 2e 20 20 41 64  otal string.  Ad
17f10 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74  ditionally, if t
17f20 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20  he parameter in 
17f30 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  the.** argument 
17f40 6c 69 73 74 20 69 73 20 61 20 4e 55 4c 4c 20 70  list is a NULL p
17f50 6f 69 6e 74 65 72 2c 20 25 51 20 73 75 62 73 74  ointer, %Q subst
17f60 69 74 75 74 65 73 20 74 68 65 20 74 65 78 74 20  itutes the text 
17f70 22 4e 55 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a  "NULL" (without.
17f80 2a 2a 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73  ** single quotes
17f90 29 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68  ) in place of th
17fa0 65 20 25 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f  e %Q option.  So
17fb0 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f  , for example, o
17fc0 6e 65 20 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a  ne could say:.**
17fd0 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e  .** <blockquote>
17fe0 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a  <pre>.**  char *
17ff0 7a 53 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zSQL = sqlite3_m
18000 70 72 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49  printf("INSERT I
18010 4e 54 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53  NTO table VALUES
18020 28 25 51 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a  (%Q)", zText);.*
18030 2a 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  *  sqlite3_exec(
18040 64 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20  db, zSQL, 0, 0, 
18050 30 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f  0);.**  sqlite3_
18060 66 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c  free(zSQL);.** <
18070 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74  /pre></blockquot
18080 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  e>.**.** The cod
18090 65 20 61 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e  e above will ren
180a0 64 65 72 20 61 20 63 6f 72 72 65 63 74 20 53 51  der a correct SQ
180b0 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74  L statement in t
180c0 68 65 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61  he zSQL.** varia
180d0 62 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65 20  ble even if the 
180e0 7a 54 65 78 74 20 76 61 72 69 61 62 6c 65 20 69  zText variable i
180f0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
18100 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22  ..**.** The "%z"
18110 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69   formatting opti
18120 6f 6e 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79  on works exactly
18130 20 6c 69 6b 65 20 22 25 73 22 20 77 69 74 68 20   like "%s" with 
18140 74 68 65 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20  the.** addition 
18150 74 68 61 74 20 61 66 74 65 72 20 74 68 65 20 73  that after the s
18160 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20 72  tring has been r
18170 65 61 64 20 61 6e 64 20 63 6f 70 69 65 64 20 69  ead and copied i
18180 6e 74 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  nto.** the resul
18190 74 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  t, [sqlite3_free
181a0 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  ()] is called on
181b0 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e   the input strin
181c0 67 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 52  g. {END}.**.** R
181d0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
181e0 5b 48 31 37 34 30 33 5d 20 5b 48 31 37 34 30 36  [H17403] [H17406
181f0 5d 20 5b 48 31 37 34 30 37 5d 0a 2a 2f 0a 53 51  ] [H17407].*/.SQ
18200 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
18210 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 63  qlite3_mprintf(c
18220 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
18230 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72  .SQLITE_API char
18240 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e   *sqlite3_vmprin
18250 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  tf(const char*, 
18260 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45  va_list);.SQLITE
18270 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74  _API char *sqlit
18280 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 2c  e3_snprintf(int,
18290 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
182a0 2a 2c 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *, ...);../*.** 
182b0 43 41 50 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79  CAPI3REF: Memory
182c0 20 41 6c 6c 6f 63 61 74 69 6f 6e 20 53 75 62 73   Allocation Subs
182d0 79 73 74 65 6d 20 7b 48 31 37 33 30 30 7d 20 3c  ystem {H17300} <
182e0 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20000>.**.** Th
182f0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 20 75  e SQLite core  u
18300 73 65 73 20 74 68 65 73 65 20 74 68 72 65 65 20  ses these three 
18310 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 61 6c 6c  routines for all
18320 20 6f 66 20 69 74 73 20 6f 77 6e 0a 2a 2a 20 69   of its own.** i
18330 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 61  nternal memory a
18340 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e  llocation needs.
18350 20 22 43 6f 72 65 22 20 69 6e 20 74 68 65 20 70   "Core" in the p
18360 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65  revious sentence
18370 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63  .** does not inc
18380 6c 75 64 65 20 6f 70 65 72 61 74 69 6e 67 2d 73  lude operating-s
18390 79 73 74 65 6d 20 73 70 65 63 69 66 69 63 20 56  ystem specific V
183a0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  FS implementatio
183b0 6e 2e 20 20 54 68 65 0a 2a 2a 20 57 69 6e 64 6f  n.  The.** Windo
183c0 77 73 20 56 46 53 20 75 73 65 73 20 6e 61 74 69  ws VFS uses nati
183d0 76 65 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  ve malloc() and 
183e0 66 72 65 65 28 29 20 66 6f 72 20 73 6f 6d 65 20  free() for some 
183f0 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  operations..**.*
18400 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 61  * The sqlite3_ma
18410 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 72  lloc() routine r
18420 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
18430 20 74 6f 20 61 20 62 6c 6f 63 6b 0a 2a 2a 20 6f   to a block.** o
18440 66 20 6d 65 6d 6f 72 79 20 61 74 20 6c 65 61 73  f memory at leas
18450 74 20 4e 20 62 79 74 65 73 20 69 6e 20 6c 65 6e  t N bytes in len
18460 67 74 68 2c 20 77 68 65 72 65 20 4e 20 69 73 20  gth, where N is 
18470 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  the parameter..*
18480 2a 20 49 66 20 73 71 6c 69 74 65 33 5f 6d 61 6c  * If sqlite3_mal
18490 6c 6f 63 28 29 20 69 73 20 75 6e 61 62 6c 65 20  loc() is unable 
184a0 74 6f 20 6f 62 74 61 69 6e 20 73 75 66 66 69 63  to obtain suffic
184b0 69 65 6e 74 20 66 72 65 65 0a 2a 2a 20 6d 65 6d  ient free.** mem
184c0 6f 72 79 2c 20 69 74 20 72 65 74 75 72 6e 73 20  ory, it returns 
184d0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20  a NULL pointer. 
184e0 20 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   If the paramete
184f0 72 20 4e 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  r N to.** sqlite
18500 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65  3_malloc() is ze
18510 72 6f 20 6f 72 20 6e 65 67 61 74 69 76 65 20 74  ro or negative t
18520 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  hen sqlite3_mall
18530 6f 63 28 29 20 72 65 74 75 72 6e 73 0a 2a 2a 20  oc() returns.** 
18540 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
18550 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71  **.** Calling sq
18560 6c 69 74 65 33 5f 66 72 65 65 28 29 20 77 69 74  lite3_free() wit
18570 68 20 61 20 70 6f 69 6e 74 65 72 20 70 72 65 76  h a pointer prev
18580 69 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64 0a  iously returned.
18590 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 61  ** by sqlite3_ma
185a0 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65  lloc() or sqlite
185b0 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 6c 65  3_realloc() rele
185c0 61 73 65 73 20 74 68 61 74 20 6d 65 6d 6f 72 79  ases that memory
185d0 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74 20 6d   so.** that it m
185e0 69 67 68 74 20 62 65 20 72 65 75 73 65 64 2e 20  ight be reused. 
185f0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 72 65   The sqlite3_fre
18600 65 28 29 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a  e() routine is.*
18610 2a 20 61 20 6e 6f 2d 6f 70 20 69 66 20 69 73 20  * a no-op if is 
18620 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e 55  called with a NU
18630 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 20 50 61 73  LL pointer.  Pas
18640 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  sing a NULL poin
18650 74 65 72 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  ter.** to sqlite
18660 33 5f 66 72 65 65 28 29 20 69 73 20 68 61 72 6d  3_free() is harm
18670 6c 65 73 73 2e 20 20 41 66 74 65 72 20 62 65 69  less.  After bei
18680 6e 67 20 66 72 65 65 64 2c 20 6d 65 6d 6f 72 79  ng freed, memory
18690 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 65 69 74 68  .** should neith
186a0 65 72 20 62 65 20 72 65 61 64 20 6e 6f 72 20 77  er be read nor w
186b0 72 69 74 74 65 6e 2e 20 20 45 76 65 6e 20 72 65  ritten.  Even re
186c0 61 64 69 6e 67 20 70 72 65 76 69 6f 75 73 6c 79  ading previously
186d0 20 66 72 65 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79   freed.** memory
186e0 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e   might result in
186f0 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f 6e 20   a segmentation 
18700 66 61 75 6c 74 20 6f 72 20 6f 74 68 65 72 20 73  fault or other s
18710 65 76 65 72 65 20 65 72 72 6f 72 2e 0a 2a 2a 20  evere error..** 
18720 4d 65 6d 6f 72 79 20 63 6f 72 72 75 70 74 69 6f  Memory corruptio
18730 6e 2c 20 61 20 73 65 67 6d 65 6e 74 61 74 69 6f  n, a segmentatio
18740 6e 20 66 61 75 6c 74 2c 20 6f 72 20 6f 74 68 65  n fault, or othe
18750 72 20 73 65 76 65 72 65 20 65 72 72 6f 72 0a 2a  r severe error.*
18760 2a 20 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69  * might result i
18770 66 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  f sqlite3_free()
18780 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20   is called with 
18790 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74  a non-NULL point
187a0 65 72 20 74 68 61 74 0a 2a 2a 20 77 61 73 20 6e  er that.** was n
187b0 6f 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ot obtained from
187c0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
187d0 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61  ) or sqlite3_rea
187e0 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  lloc()..**.** Th
187f0 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  e sqlite3_reallo
18800 63 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74  c() interface at
18810 74 65 6d 70 74 73 20 74 6f 20 72 65 73 69 7a 65  tempts to resize
18820 20 61 0a 2a 2a 20 70 72 69 6f 72 20 6d 65 6d 6f   a.** prior memo
18830 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f  ry allocation to
18840 20 62 65 20 61 74 20 6c 65 61 73 74 20 4e 20 62   be at least N b
18850 79 74 65 73 2c 20 77 68 65 72 65 20 4e 20 69 73  ytes, where N is
18860 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70   the.** second p
18870 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20 6d  arameter.  The m
18880 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
18890 20 74 6f 20 62 65 20 72 65 73 69 7a 65 64 20 69   to be resized i
188a0 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 70  s the first.** p
188b0 61 72 61 6d 65 74 65 72 2e 20 20 49 66 20 74 68  arameter.  If th
188c0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
188d0 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61  r to sqlite3_rea
188e0 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 20 4e  lloc().** is a N
188f0 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e  ULL pointer then
18900 20 69 74 73 20 62 65 68 61 76 69 6f 72 20 69 73   its behavior is
18910 20 69 64 65 6e 74 69 63 61 6c 20 74 6f 20 63 61   identical to ca
18920 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  lling.** sqlite3
18930 5f 6d 61 6c 6c 6f 63 28 4e 29 20 77 68 65 72 65  _malloc(N) where
18940 20 4e 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64   N is the second
18950 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
18960 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e  lite3_realloc().
18970 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 63 6f 6e  .** If the secon
18980 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  d parameter to s
18990 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
189a0 20 69 73 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e   is zero or.** n
189b0 65 67 61 74 69 76 65 20 74 68 65 6e 20 74 68 65  egative then the
189c0 20 62 65 68 61 76 69 6f 72 20 69 73 20 65 78 61   behavior is exa
189d0 63 74 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73  ctly the same as
189e0 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69   calling.** sqli
189f0 74 65 33 5f 66 72 65 65 28 50 29 20 77 68 65 72  te3_free(P) wher
18a00 65 20 50 20 69 73 20 74 68 65 20 66 69 72 73 74  e P is the first
18a10 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
18a20 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e  lite3_realloc().
18a30 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  .** sqlite3_real
18a40 6c 6f 63 28 29 20 72 65 74 75 72 6e 73 20 61 20  loc() returns a 
18a50 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6d 65 6d  pointer to a mem
18a60 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  ory allocation.*
18a70 2a 20 6f 66 20 61 74 20 6c 65 61 73 74 20 4e 20  * of at least N 
18a80 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 6f 72  bytes in size or
18a90 20 4e 55 4c 4c 20 69 66 20 73 75 66 66 69 63 69   NULL if suffici
18aa0 65 6e 74 20 6d 65 6d 6f 72 79 20 69 73 20 75 6e  ent memory is un
18ab0 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 49 66  available..** If
18ac0 20 4d 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f   M is the size o
18ad0 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f  f the prior allo
18ae0 63 61 74 69 6f 6e 2c 20 74 68 65 6e 20 6d 69 6e  cation, then min
18af0 28 4e 2c 4d 29 20 62 79 74 65 73 0a 2a 2a 20 6f  (N,M) bytes.** o
18b00 66 20 74 68 65 20 70 72 69 6f 72 20 61 6c 6c 6f  f the prior allo
18b10 63 61 74 69 6f 6e 20 61 72 65 20 63 6f 70 69 65  cation are copie
18b20 64 20 69 6e 74 6f 20 74 68 65 20 62 65 67 69 6e  d into the begin
18b30 6e 69 6e 67 20 6f 66 20 62 75 66 66 65 72 20 72  ning of buffer r
18b40 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71  eturned.** by sq
18b50 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20  lite3_realloc() 
18b60 61 6e 64 20 74 68 65 20 70 72 69 6f 72 20 61 6c  and the prior al
18b70 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  location is free
18b80 64 2e 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33  d..** If sqlite3
18b90 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74 75 72  _realloc() retur
18ba0 6e 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68  ns NULL, then th
18bb0 65 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69  e prior allocati
18bc0 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 66 72 65  on.** is not fre
18bd0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65  ed..**.** The me
18be0 6d 6f 72 79 20 72 65 74 75 72 6e 65 64 20 62 79  mory returned by
18bf0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
18c00 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65  ) and sqlite3_re
18c10 61 6c 6c 6f 63 28 29 0a 2a 2a 20 69 73 20 61 6c  alloc().** is al
18c20 77 61 79 73 20 61 6c 69 67 6e 65 64 20 74 6f 20  ways aligned to 
18c30 61 74 20 6c 65 61 73 74 20 61 6e 20 38 20 62 79  at least an 8 by
18c40 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 7b 45 4e  te boundary. {EN
18c50 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66  D}.**.** The def
18c60 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ault implementat
18c70 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
18c80 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
18c90 73 79 73 74 65 6d 20 75 73 65 73 0a 2a 2a 20 74  system uses.** t
18ca0 68 65 20 6d 61 6c 6c 6f 63 28 29 2c 20 72 65 61  he malloc(), rea
18cb0 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28  lloc() and free(
18cc0 29 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68  ) provided by th
18cd0 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62  e standard C lib
18ce0 72 61 72 79 2e 0a 2a 2a 20 7b 48 31 37 33 38 32  rary..** {H17382
18cf0 7d 20 48 6f 77 65 76 65 72 2c 20 69 66 20 53 51  } However, if SQ
18d00 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
18d10 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 53 51 4c   with the.** SQL
18d20 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 3d  ITE_MEMORY_SIZE=
18d30 3c 69 3e 4e 4e 4e 3c 2f 69 3e 20 43 20 70 72 65  <i>NNN</i> C pre
18d40 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20  processor macro 
18d50 28 77 68 65 72 65 20 3c 69 3e 4e 4e 4e 3c 2f 69  (where <i>NNN</i
18d60 3e 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67  >.** is an integ
18d70 65 72 29 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  er), then SQLite
18d80 20 63 72 65 61 74 65 20 61 20 73 74 61 74 69 63   create a static
18d90 20 61 72 72 61 79 20 6f 66 20 61 74 20 6c 65 61   array of at lea
18da0 73 74 0a 2a 2a 20 3c 69 3e 4e 4e 4e 3c 2f 69 3e  st.** <i>NNN</i>
18db0 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20 61   bytes in size a
18dc0 6e 64 20 75 73 65 73 20 74 68 61 74 20 61 72 72  nd uses that arr
18dd0 61 79 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74  ay for all of it
18de0 73 20 64 79 6e 61 6d 69 63 0a 2a 2a 20 6d 65 6d  s dynamic.** mem
18df0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e  ory allocation n
18e00 65 65 64 73 2e 20 7b 45 4e 44 7d 20 20 41 64 64  eeds. {END}  Add
18e10 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 61  itional memory a
18e20 6c 6c 6f 63 61 74 6f 72 20 6f 70 74 69 6f 6e 73  llocator options
18e30 0a 2a 2a 20 6d 61 79 20 62 65 20 61 64 64 65 64  .** may be added
18e40 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61   in future relea
18e50 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 53 51  ses..**.** In SQ
18e60 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35  Lite version 3.5
18e70 2e 30 20 61 6e 64 20 33 2e 35 2e 31 2c 20 69 74  .0 and 3.5.1, it
18e80 20 77 61 73 20 70 6f 73 73 69 62 6c 65 20 74 6f   was possible to
18e90 20 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53   define.** the S
18ea0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52  QLITE_OMIT_MEMOR
18eb0 59 5f 41 4c 4c 4f 43 41 54 49 4f 4e 20 77 68 69  Y_ALLOCATION whi
18ec0 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20 74  ch would cause t
18ed0 68 65 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 69  he built-in.** i
18ee0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
18ef0 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
18f00 74 6f 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20  to be omitted.  
18f10 54 68 61 74 20 63 61 70 61 62 69 6c 69 74 79 0a  That capability.
18f20 2a 2a 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  ** is no longer 
18f30 70 72 6f 76 69 64 65 64 2e 20 20 4f 6e 6c 79 20  provided.  Only 
18f40 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20  built-in memory 
18f50 61 6c 6c 6f 63 61 74 6f 72 73 20 63 61 6e 20 62  allocators can b
18f60 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  e used..**.** Th
18f70 65 20 57 69 6e 64 6f 77 73 20 4f 53 20 69 6e 74  e Windows OS int
18f80 65 72 66 61 63 65 20 6c 61 79 65 72 20 63 61 6c  erface layer cal
18f90 6c 73 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d  ls.** the system
18fa0 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 66 72   malloc() and fr
18fb0 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 77 68  ee() directly wh
18fc0 65 6e 20 63 6f 6e 76 65 72 74 69 6e 67 0a 2a 2a  en converting.**
18fd0 20 66 69 6c 65 6e 61 6d 65 73 20 62 65 74 77 65   filenames betwe
18fe0 65 6e 20 74 68 65 20 55 54 46 2d 38 20 65 6e 63  en the UTF-8 enc
18ff0 6f 64 69 6e 67 20 75 73 65 64 20 62 79 20 53 51  oding used by SQ
19000 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 77 68 61 74  Lite.** and what
19010 65 76 65 72 20 66 69 6c 65 6e 61 6d 65 20 65 6e  ever filename en
19020 63 6f 64 69 6e 67 20 69 73 20 75 73 65 64 20 62  coding is used b
19030 79 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72  y the particular
19040 20 57 69 6e 64 6f 77 73 0a 2a 2a 20 69 6e 73 74   Windows.** inst
19050 61 6c 6c 61 74 69 6f 6e 2e 20 20 4d 65 6d 6f 72  allation.  Memor
19060 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
19070 6f 72 73 20 61 72 65 20 64 65 74 65 63 74 65 64  ors are detected
19080 2c 20 62 75 74 0a 2a 2a 20 74 68 65 79 20 61 72  , but.** they ar
19090 65 20 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20  e reported back 
190a0 61 73 20 5b 53 51 4c 49 54 45 5f 43 41 4e 54 4f  as [SQLITE_CANTO
190b0 50 45 4e 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49  PEN] or.** [SQLI
190c0 54 45 5f 49 4f 45 52 52 5d 20 72 61 74 68 65 72  TE_IOERR] rather
190d0 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f   than [SQLITE_NO
190e0 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  MEM]..**.** Requ
190f0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
19100 37 33 30 33 5d 20 5b 48 31 37 33 30 34 5d 20 5b  7303] [H17304] [
19110 48 31 37 33 30 35 5d 20 5b 48 31 37 33 30 36 5d  H17305] [H17306]
19120 20 5b 48 31 37 33 31 30 5d 20 5b 48 31 37 33 31   [H17310] [H1731
19130 32 5d 20 5b 48 31 37 33 31 35 5d 20 5b 48 31 37  2] [H17315] [H17
19140 33 31 38 5d 0a 2a 2a 20 5b 48 31 37 33 32 31 5d  318].** [H17321]
19150 20 5b 48 31 37 33 32 32 5d 20 5b 48 31 37 33 32   [H17322] [H1732
19160 33 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69  3].**.** The poi
19170 6e 74 65 72 20 61 72 67 75 6d 65 6e 74 73 20 74  nter arguments t
19180 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  o [sqlite3_free(
19190 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
191a0 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 6d 75  realloc()].** mu
191b0 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 55 4c  st be either NUL
191c0 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e 74 65  L or else pointe
191d0 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  rs obtained from
191e0 20 61 20 70 72 69 6f 72 0a 2a 2a 20 69 6e 76 6f   a prior.** invo
191f0 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74  cation of [sqlit
19200 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20  e3_malloc()] or 
19210 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
19220 28 29 5d 20 74 68 61 74 20 68 61 76 65 0a 2a 2a  ()] that have.**
19230 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 72 65   not yet been re
19240 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  leased..**.** Th
19250 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75  e application mu
19260 73 74 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77  st not read or w
19270 72 69 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66  rite any part of
19280 0a 2a 2a 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d  .** a block of m
19290 65 6d 6f 72 79 20 61 66 74 65 72 20 69 74 20 68  emory after it h
192a0 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64  as been released
192b0 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74   using.** [sqlit
192c0 65 33 5f 66 72 65 65 28 29 5d 20 6f 72 20 5b 73  e3_free()] or [s
192d0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
192e0 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ]..*/.SQLITE_API
192f0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d   void *sqlite3_m
19300 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49  alloc(int);.SQLI
19310 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
19320 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69  ite3_realloc(voi
19330 64 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  d*, int);.SQLITE
19340 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
19350 33 5f 66 72 65 65 28 76 6f 69 64 2a 29 3b 0a 0a  3_free(void*);..
19360 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
19370 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 6f 72  Memory Allocator
19380 20 53 74 61 74 69 73 74 69 63 73 20 7b 48 31 37   Statistics {H17
19390 33 37 30 7d 20 3c 53 33 30 32 31 30 3e 0a 2a 2a  370} <S30210>.**
193a0 0a 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 76 69  .** SQLite provi
193b0 64 65 73 20 74 68 65 73 65 20 74 77 6f 20 69 6e  des these two in
193c0 74 65 72 66 61 63 65 73 20 66 6f 72 20 72 65 70  terfaces for rep
193d0 6f 72 74 69 6e 67 20 6f 6e 20 74 68 65 20 73 74  orting on the st
193e0 61 74 75 73 0a 2a 2a 20 6f 66 20 74 68 65 20 5b  atus.** of the [
193f0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
19400 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  ], [sqlite3_free
19410 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
19420 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 0a 2a 2a 20  3_realloc()].** 
19430 72 6f 75 74 69 6e 65 73 2c 20 77 68 69 63 68 20  routines, which 
19440 66 6f 72 6d 20 74 68 65 20 62 75 69 6c 74 2d 69  form the built-i
19450 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  n memory allocat
19460 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a  ion subsystem..*
19470 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
19480 73 3a 0a 2a 2a 20 5b 48 31 37 33 37 31 5d 20 5b  s:.** [H17371] [
19490 48 31 37 33 37 33 5d 20 5b 48 31 37 33 37 34 5d  H17373] [H17374]
194a0 20 5b 48 31 37 33 37 35 5d 0a 2a 2f 0a 53 51 4c   [H17375].*/.SQL
194b0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
194c0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65  int64 sqlite3_me
194d0 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b  mory_used(void);
194e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
194f0 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
19500 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
19510 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67  er(int resetFlag
19520 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
19530 45 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f  EF: Pseudo-Rando
19540 6d 20 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74  m Number Generat
19550 6f 72 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30  or {H17390} <S20
19560 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  000>.**.** SQLit
19570 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67  e contains a hig
19580 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f  h-quality pseudo
19590 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67  -random number g
195a0 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20  enerator (PRNG) 
195b0 75 73 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63  used to.** selec
195c0 74 20 72 61 6e 64 6f 6d 20 5b 52 4f 57 49 44 20  t random [ROWID 
195d0 7c 20 52 4f 57 49 44 73 5d 20 77 68 65 6e 20 69  | ROWIDs] when i
195e0 6e 73 65 72 74 69 6e 67 20 6e 65 77 20 72 65 63  nserting new rec
195f0 6f 72 64 73 20 69 6e 74 6f 20 61 20 74 61 62 6c  ords into a tabl
19600 65 20 74 68 61 74 0a 2a 2a 20 61 6c 72 65 61 64  e that.** alread
19610 79 20 75 73 65 73 20 74 68 65 20 6c 61 72 67 65  y uses the large
19620 73 74 20 70 6f 73 73 69 62 6c 65 20 5b 52 4f 57  st possible [ROW
19630 49 44 5d 2e 20 20 54 68 65 20 50 52 4e 47 20 69  ID].  The PRNG i
19640 73 20 61 6c 73 6f 20 75 73 65 64 20 66 6f 72 0a  s also used for.
19650 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20  ** the build-in 
19660 72 61 6e 64 6f 6d 28 29 20 61 6e 64 20 72 61 6e  random() and ran
19670 64 6f 6d 62 6c 6f 62 28 29 20 53 51 4c 20 66 75  domblob() SQL fu
19680 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69  nctions.  This i
19690 6e 74 65 72 66 61 63 65 20 61 6c 6c 6f 77 73 0a  nterface allows.
196a0 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ** applications 
196b0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 61  to access the sa
196c0 6d 65 20 50 52 4e 47 20 66 6f 72 20 6f 74 68 65  me PRNG for othe
196d0 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2a 0a 2a  r purposes..**.*
196e0 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73  * A call to this
196f0 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20   routine stores 
19700 4e 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f  N bytes of rando
19710 6d 6e 65 73 73 20 69 6e 74 6f 20 62 75 66 66 65  mness into buffe
19720 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  r P..**.** The f
19730 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72  irst time this r
19740 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65  outine is invoke
19750 64 20 28 65 69 74 68 65 72 20 69 6e 74 65 72 6e  d (either intern
19760 61 6c 6c 79 20 6f 72 20 62 79 0a 2a 2a 20 74 68  ally or by.** th
19770 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 29 20 74  e application) t
19780 68 65 20 50 52 4e 47 20 69 73 20 73 65 65 64 65  he PRNG is seede
19790 64 20 75 73 69 6e 67 20 72 61 6e 64 6f 6d 6e 65  d using randomne
197a0 73 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66  ss obtained.** f
197b0 72 6f 6d 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e  rom the xRandomn
197c0 65 73 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  ess method of th
197d0 65 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74  e default [sqlit
197e0 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a  e3_vfs] object..
197f0 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75 62 73 65 71  ** On all subseq
19800 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73  uent invocations
19810 2c 20 74 68 65 20 70 73 65 75 64 6f 2d 72 61 6e  , the pseudo-ran
19820 64 6f 6d 6e 65 73 73 20 69 73 20 67 65 6e 65 72  domness is gener
19830 61 74 65 64 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c  ated.** internal
19840 6c 79 20 61 6e 64 20 77 69 74 68 6f 75 74 20 72  ly and without r
19850 65 63 6f 75 72 73 65 20 74 6f 20 74 68 65 20 5b  ecourse to the [
19860 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 78 52 61  sqlite3_vfs] xRa
19870 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20 6d 65 74 68  ndomness.** meth
19880 6f 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  od..**.** Requir
19890 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 33  ements:.** [H173
198a0 39 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  92].*/.SQLITE_AP
198b0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
198c0 61 6e 64 6f 6d 6e 65 73 73 28 69 6e 74 20 4e 2c  andomness(int N,
198d0 20 76 6f 69 64 20 2a 50 29 3b 0a 0a 2f 2a 0a 2a   void *P);../*.*
198e0 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70  * CAPI3REF: Comp
198f0 69 6c 65 2d 54 69 6d 65 20 41 75 74 68 6f 72 69  ile-Time Authori
19900 7a 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73  zation Callbacks
19910 20 7b 48 31 32 35 30 30 7d 20 3c 53 37 30 31 30   {H12500} <S7010
19920 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  0>.**.** This ro
19930 75 74 69 6e 65 20 72 65 67 69 73 74 65 72 73 20  utine registers 
19940 61 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  a authorizer cal
19950 6c 62 61 63 6b 20 77 69 74 68 20 61 20 70 61 72  lback with a par
19960 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64 61 74 61  ticular.** [data
19970 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
19980 2c 20 73 75 70 70 6c 69 65 64 20 69 6e 20 74 68  , supplied in th
19990 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
199a0 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69  ..** The authori
199b0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  zer callback is 
199c0 69 6e 76 6f 6b 65 64 20 61 73 20 53 51 4c 20 73  invoked as SQL s
199d0 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 62 65  tatements are be
199e0 69 6e 67 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20  ing compiled.** 
199f0 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  by [sqlite3_prep
19a00 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61  are()] or its va
19a10 72 69 61 6e 74 73 20 5b 73 71 6c 69 74 65 33 5f  riants [sqlite3_
19a20 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c 0a 2a  prepare_v2()],.*
19a30 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
19a40 72 65 31 36 28 29 5d 20 61 6e 64 20 5b 73 71 6c  re16()] and [sql
19a50 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
19a60 32 28 29 5d 2e 20 20 41 74 20 76 61 72 69 6f 75  2()].  At variou
19a70 73 0a 2a 2a 20 70 6f 69 6e 74 73 20 64 75 72 69  s.** points duri
19a80 6e 67 20 74 68 65 20 63 6f 6d 70 69 6c 61 74 69  ng the compilati
19a90 6f 6e 20 70 72 6f 63 65 73 73 2c 20 61 73 20 6c  on process, as l
19aa0 6f 67 69 63 20 69 73 20 62 65 69 6e 67 20 63 72  ogic is being cr
19ab0 65 61 74 65 64 0a 2a 2a 20 74 6f 20 70 65 72 66  eated.** to perf
19ac0 6f 72 6d 20 76 61 72 69 6f 75 73 20 61 63 74 69  orm various acti
19ad0 6f 6e 73 2c 20 74 68 65 20 61 75 74 68 6f 72 69  ons, the authori
19ae0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  zer callback is 
19af0 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 73 65  invoked to.** se
19b00 65 20 69 66 20 74 68 6f 73 65 20 61 63 74 69 6f  e if those actio
19b10 6e 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 20  ns are allowed. 
19b20 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   The authorizer 
19b30 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 0a  callback should.
19b40 2a 2a 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54  ** return [SQLIT
19b50 45 5f 4f 4b 5d 20 74 6f 20 61 6c 6c 6f 77 20 74  E_OK] to allow t
19b60 68 65 20 61 63 74 69 6f 6e 2c 20 5b 53 51 4c 49  he action, [SQLI
19b70 54 45 5f 49 47 4e 4f 52 45 5d 20 74 6f 20 64 69  TE_IGNORE] to di
19b80 73 61 6c 6c 6f 77 20 74 68 65 0a 2a 2a 20 73 70  sallow the.** sp
19b90 65 63 69 66 69 63 20 61 63 74 69 6f 6e 20 62 75  ecific action bu
19ba0 74 20 61 6c 6c 6f 77 20 74 68 65 20 53 51 4c 20  t allow the SQL 
19bb0 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 63 6f 6e  statement to con
19bc0 74 69 6e 75 65 20 74 6f 20 62 65 0a 2a 2a 20 63  tinue to be.** c
19bd0 6f 6d 70 69 6c 65 64 2c 20 6f 72 20 5b 53 51 4c  ompiled, or [SQL
19be0 49 54 45 5f 44 45 4e 59 5d 20 74 6f 20 63 61 75  ITE_DENY] to cau
19bf0 73 65 20 74 68 65 20 65 6e 74 69 72 65 20 53 51  se the entire SQ
19c00 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  L statement to b
19c10 65 0a 2a 2a 20 72 65 6a 65 63 74 65 64 20 77 69  e.** rejected wi
19c20 74 68 20 61 6e 20 65 72 72 6f 72 2e 20 20 49 66  th an error.  If
19c30 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
19c40 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
19c50 0a 2a 2a 20 61 6e 79 20 76 61 6c 75 65 20 6f 74  .** any value ot
19c60 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45  her than [SQLITE
19c70 5f 49 47 4e 4f 52 45 5d 2c 20 5b 53 51 4c 49 54  _IGNORE], [SQLIT
19c80 45 5f 4f 4b 5d 2c 20 6f 72 20 5b 53 51 4c 49 54  E_OK], or [SQLIT
19c90 45 5f 44 45 4e 59 5d 0a 2a 2a 20 74 68 65 6e 20  E_DENY].** then 
19ca0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  the [sqlite3_pre
19cb0 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71  pare_v2()] or eq
19cc0 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68  uivalent call th
19cd0 61 74 20 74 72 69 67 67 65 72 65 64 0a 2a 2a 20  at triggered.** 
19ce0 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 77  the authorizer w
19cf0 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e  ill fail with an
19d00 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
19d10 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63  **.** When the c
19d20 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
19d30 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 74 68 61  [SQLITE_OK], tha
19d40 74 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72  t means the oper
19d50 61 74 69 6f 6e 0a 2a 2a 20 72 65 71 75 65 73 74  ation.** request
19d60 65 64 20 69 73 20 6f 6b 2e 20 20 57 68 65 6e 20  ed is ok.  When 
19d70 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  the callback ret
19d80 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e  urns [SQLITE_DEN
19d90 59 5d 2c 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69  Y], the.** [sqli
19da0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
19db0 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20  ] or equivalent 
19dc0 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65  call that trigge
19dd0 72 65 64 20 74 68 65 0a 2a 2a 20 61 75 74 68 6f  red the.** autho
19de0 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20  rizer will fail 
19df0 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65  with an error me
19e00 73 73 61 67 65 20 65 78 70 6c 61 69 6e 69 6e 67  ssage explaining
19e10 20 74 68 61 74 0a 2a 2a 20 61 63 63 65 73 73 20   that.** access 
19e20 69 73 20 64 65 6e 69 65 64 2e 20 0a 2a 2a 0a 2a  is denied. .**.*
19e30 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61  * The first para
19e40 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75 74  meter to the aut
19e50 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
19e60 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68   is a copy of th
19e70 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d  e third.** param
19e80 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69  eter to the sqli
19e90 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a  te3_set_authoriz
19ea0 65 72 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20  er() interface. 
19eb0 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  The second param
19ec0 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 63  eter.** to the c
19ed0 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69 6e  allback is an in
19ee0 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f 43 4f  teger [SQLITE_CO
19ef0 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f 64 65  PY | action code
19f00 5d 20 74 68 61 74 20 73 70 65 63 69 66 69 65 73  ] that specifies
19f10 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63 75 6c  .** the particul
19f20 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20  ar action to be 
19f30 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68 65 20  authorized. The 
19f40 74 68 69 72 64 20 74 68 72 6f 75 67 68 20 73 69  third through si
19f50 78 74 68 20 70 61 72 61 6d 65 74 65 72 73 0a 2a  xth parameters.*
19f60 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  * to the callbac
19f70 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72 6d 69  k are zero-termi
19f80 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20 74 68  nated strings th
19f90 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74  at contain addit
19fa0 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69 6c 73  ional.** details
19fb0 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f   about the actio
19fc0 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a  n to be authoriz
19fd0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
19fe0 20 61 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20   action code is 
19ff0 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a  [SQLITE_READ].**
1a000 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63   and the callbac
1a010 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  k returns [SQLIT
1a020 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74  E_IGNORE] then t
1a030 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20  he.** [prepared 
1a040 73 74 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65  statement] state
1a050 6d 65 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63  ment is construc
1a060 74 65 64 20 74 6f 20 73 75 62 73 74 69 74 75 74  ted to substitut
1a070 65 0a 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75  e.** a NULL valu
1a080 65 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68  e in place of th
1a090 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74  e table column t
1a0a0 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a  hat would have.*
1a0b0 2a 20 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b  * been read if [
1a0c0 53 51 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62  SQLITE_OK] had b
1a0d0 65 65 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54  een returned.  T
1a0e0 68 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52  he [SQLITE_IGNOR
1a0f0 45 5d 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e  E].** return can
1a100 20 62 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79   be used to deny
1a110 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73   an untrusted us
1a120 65 72 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64  er access to ind
1a130 69 76 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d  ividual.** colum
1a140 6e 73 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a  ns of a table..*
1a150 2a 20 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20  * If the action 
1a160 63 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f  code is [SQLITE_
1a170 44 45 4c 45 54 45 5d 20 61 6e 64 20 74 68 65 20  DELETE] and the 
1a180 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
1a190 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f  .** [SQLITE_IGNO
1a1a0 52 45 5d 20 74 68 65 6e 20 74 68 65 20 5b 44 45  RE] then the [DE
1a1b0 4c 45 54 45 5d 20 6f 70 65 72 61 74 69 6f 6e 20  LETE] operation 
1a1c0 70 72 6f 63 65 65 64 73 20 62 75 74 20 74 68 65  proceeds but the
1a1d0 0a 2a 2a 20 5b 74 72 75 6e 63 61 74 65 20 6f 70  .** [truncate op
1a1e0 74 69 6d 69 7a 61 74 69 6f 6e 5d 20 69 73 20 64  timization] is d
1a1f0 69 73 61 62 6c 65 64 20 61 6e 64 20 61 6c 6c 20  isabled and all 
1a200 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65 64  rows are deleted
1a210 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 2e 0a 2a   individually..*
1a220 2a 0a 2a 2a 20 41 6e 20 61 75 74 68 6f 72 69 7a  *.** An authoriz
1a230 65 72 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  er is used when 
1a240 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
1a250 20 7c 20 70 72 65 70 61 72 69 6e 67 5d 0a 2a 2a   | preparing].**
1a260 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
1a270 66 72 6f 6d 20 61 6e 20 75 6e 74 72 75 73 74 65  from an untruste
1a280 64 20 73 6f 75 72 63 65 2c 20 74 6f 20 65 6e 73  d source, to ens
1a290 75 72 65 20 74 68 61 74 20 74 68 65 20 53 51 4c  ure that the SQL
1a2a0 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 64   statements.** d
1a2b0 6f 20 6e 6f 74 20 74 72 79 20 74 6f 20 61 63 63  o not try to acc
1a2c0 65 73 73 20 64 61 74 61 20 74 68 65 79 20 61 72  ess data they ar
1a2d0 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f  e not allowed to
1a2e0 20 73 65 65 2c 20 6f 72 20 74 68 61 74 20 74 68   see, or that th
1a2f0 65 79 20 64 6f 20 6e 6f 74 0a 2a 2a 20 74 72 79  ey do not.** try
1a300 20 74 6f 20 65 78 65 63 75 74 65 20 6d 61 6c 69   to execute mali
1a310 63 69 6f 75 73 20 73 74 61 74 65 6d 65 6e 74 73  cious statements
1a320 20 74 68 61 74 20 64 61 6d 61 67 65 20 74 68 65   that damage the
1a330 20 64 61 74 61 62 61 73 65 2e 20 20 46 6f 72 0a   database.  For.
1a340 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 6e 20 61  ** example, an a
1a350 70 70 6c 69 63 61 74 69 6f 6e 20 6d 61 79 20 61  pplication may a
1a360 6c 6c 6f 77 20 61 20 75 73 65 72 20 74 6f 20 65  llow a user to e
1a370 6e 74 65 72 20 61 72 62 69 74 72 61 72 79 0a 2a  nter arbitrary.*
1a380 2a 20 53 51 4c 20 71 75 65 72 69 65 73 20 66 6f  * SQL queries fo
1a390 72 20 65 76 61 6c 75 61 74 69 6f 6e 20 62 79 20  r evaluation by 
1a3a0 61 20 64 61 74 61 62 61 73 65 2e 20 20 42 75 74  a database.  But
1a3b0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
1a3c0 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e   does.** not wan
1a3d0 74 20 74 68 65 20 75 73 65 72 20 74 6f 20 62 65  t the user to be
1a3e0 20 61 62 6c 65 20 74 6f 20 6d 61 6b 65 20 61 72   able to make ar
1a3f0 62 69 74 72 61 72 79 20 63 68 61 6e 67 65 73 20  bitrary changes 
1a400 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  to the.** databa
1a410 73 65 2e 20 20 41 6e 20 61 75 74 68 6f 72 69 7a  se.  An authoriz
1a420 65 72 20 63 6f 75 6c 64 20 74 68 65 6e 20 62 65  er could then be
1a430 20 70 75 74 20 69 6e 20 70 6c 61 63 65 20 77 68   put in place wh
1a440 69 6c 65 20 74 68 65 0a 2a 2a 20 75 73 65 72 2d  ile the.** user-
1a450 65 6e 74 65 72 65 64 20 53 51 4c 20 69 73 20 62  entered SQL is b
1a460 65 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72  eing [sqlite3_pr
1a470 65 70 61 72 65 20 7c 20 70 72 65 70 61 72 65 64  epare | prepared
1a480 5d 20 74 68 61 74 0a 2a 2a 20 64 69 73 61 6c 6c  ] that.** disall
1a490 6f 77 73 20 65 76 65 72 79 74 68 69 6e 67 20 65  ows everything e
1a4a0 78 63 65 70 74 20 5b 53 45 4c 45 43 54 5d 20 73  xcept [SELECT] s
1a4b0 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
1a4c0 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   Applications th
1a4d0 61 74 20 6e 65 65 64 20 74 6f 20 70 72 6f 63 65  at need to proce
1a4e0 73 73 20 53 51 4c 20 66 72 6f 6d 20 75 6e 74 72  ss SQL from untr
1a4f0 75 73 74 65 64 20 73 6f 75 72 63 65 73 0a 2a 2a  usted sources.**
1a500 20 6d 69 67 68 74 20 61 6c 73 6f 20 63 6f 6e 73   might also cons
1a510 69 64 65 72 20 6c 6f 77 65 72 69 6e 67 20 72 65  ider lowering re
1a520 73 6f 75 72 63 65 20 6c 69 6d 69 74 73 20 75 73  source limits us
1a530 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d  ing [sqlite3_lim
1a540 69 74 28 29 5d 0a 2a 2a 20 61 6e 64 20 6c 69 6d  it()].** and lim
1a550 69 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 73  iting database s
1a560 69 7a 65 20 75 73 69 6e 67 20 74 68 65 20 5b 6d  ize using the [m
1a570 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b  ax_page_count] [
1a580 50 52 41 47 4d 41 5d 0a 2a 2a 20 69 6e 20 61 64  PRAGMA].** in ad
1a590 64 69 74 69 6f 6e 20 74 6f 20 75 73 69 6e 67 20  dition to using 
1a5a0 61 6e 20 61 75 74 68 6f 72 69 7a 65 72 2e 0a 2a  an authorizer..*
1a5b0 2a 0a 2a 2a 20 4f 6e 6c 79 20 61 20 73 69 6e 67  *.** Only a sing
1a5c0 6c 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61  le authorizer ca
1a5d0 6e 20 62 65 20 69 6e 20 70 6c 61 63 65 20 6f 6e  n be in place on
1a5e0 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e   a database conn
1a5f0 65 63 74 69 6f 6e 0a 2a 2a 20 61 74 20 61 20 74  ection.** at a t
1a600 69 6d 65 2e 20 20 45 61 63 68 20 63 61 6c 6c 20  ime.  Each call 
1a610 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  to sqlite3_set_a
1a620 75 74 68 6f 72 69 7a 65 72 20 6f 76 65 72 72 69  uthorizer overri
1a630 64 65 73 20 74 68 65 0a 2a 2a 20 70 72 65 76 69  des the.** previ
1a640 6f 75 73 20 63 61 6c 6c 2e 20 20 44 69 73 61 62  ous call.  Disab
1a650 6c 65 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  le the authorize
1a660 72 20 62 79 20 69 6e 73 74 61 6c 6c 69 6e 67 20  r by installing 
1a670 61 20 4e 55 4c 4c 20 63 61 6c 6c 62 61 63 6b 2e  a NULL callback.
1a680 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 69 7a  .** The authoriz
1a690 65 72 20 69 73 20 64 69 73 61 62 6c 65 64 20 62  er is disabled b
1a6a0 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a  y default..**.**
1a6b0 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   The authorizer 
1a6c0 63 61 6c 6c 62 61 63 6b 20 6d 75 73 74 20 6e 6f  callback must no
1a6d0 74 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68  t do anything th
1a6e0 61 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a  at will modify.*
1a6f0 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  * the database c
1a700 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69  onnection that i
1a710 6e 76 6f 6b 65 64 20 74 68 65 20 61 75 74 68 6f  nvoked the autho
1a720 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 2e 0a  rizer callback..
1a730 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71  ** Note that [sq
1a740 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1a750 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
1a760 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f  _step()] both mo
1a770 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61  dify their.** da
1a780 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1a790 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69  ns for the meani
1a7a0 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69  ng of "modify" i
1a7b0 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68  n this paragraph
1a7c0 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 5b 73 71  ..**.** When [sq
1a7d0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1a7e0 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f 20 70  ()] is used to p
1a7f0 72 65 70 61 72 65 20 61 20 73 74 61 74 65 6d 65  repare a stateme
1a800 6e 74 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65  nt, the.** state
1a810 6d 65 6e 74 20 6d 69 67 68 74 20 62 65 20 72 65  ment might be re
1a820 2d 70 72 65 70 61 72 65 64 20 64 75 72 69 6e 67  -prepared during
1a830 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1a840 5d 20 64 75 65 20 74 6f 20 61 20 0a 2a 2a 20 73  ] due to a .** s
1a850 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 20 20 48  chema change.  H
1a860 65 6e 63 65 2c 20 74 68 65 20 61 70 70 6c 69 63  ence, the applic
1a870 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 65 6e 73  ation should ens
1a880 75 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ure that the.** 
1a890 63 6f 72 72 65 63 74 20 61 75 74 68 6f 72 69 7a  correct authoriz
1a8a0 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 6d 61  er callback rema
1a8b0 69 6e 73 20 69 6e 20 70 6c 61 63 65 20 64 75 72  ins in place dur
1a8c0 69 6e 67 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ing the [sqlite3
1a8d0 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  _step()]..**.** 
1a8e0 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 61 75  Note that the au
1a8f0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1a900 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c  k is invoked onl
1a910 79 20 64 75 72 69 6e 67 0a 2a 2a 20 5b 73 71 6c  y during.** [sql
1a920 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
1a930 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 2e  or its variants.
1a940 20 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20    Authorization 
1a950 69 73 20 6e 6f 74 0a 2a 2a 20 70 65 72 66 6f 72  is not.** perfor
1a960 6d 65 64 20 64 75 72 69 6e 67 20 73 74 61 74 65  med during state
1a970 6d 65 6e 74 20 65 76 61 6c 75 61 74 69 6f 6e 20  ment evaluation 
1a980 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  in [sqlite3_step
1a990 28 29 5d 2c 20 75 6e 6c 65 73 73 0a 2a 2a 20 61  ()], unless.** a
1a9a0 73 20 73 74 61 74 65 64 20 69 6e 20 74 68 65 20  s stated in the 
1a9b0 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72 61  previous paragra
1a9c0 70 68 2c 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ph, sqlite3_step
1a9d0 28 29 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20 73 71  () invokes.** sq
1a9e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1a9f0 28 29 20 74 6f 20 72 65 70 72 65 70 61 72 65 20  () to reprepare 
1aa00 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66 74 65  a statement afte
1aa10 72 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67  r a schema chang
1aa20 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  e..**.** Require
1aa30 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 30  ments:.** [H1250
1aa40 31 5d 20 5b 48 31 32 35 30 32 5d 20 5b 48 31 32  1] [H12502] [H12
1aa50 35 30 33 5d 20 5b 48 31 32 35 30 34 5d 20 5b 48  503] [H12504] [H
1aa60 31 32 35 30 35 5d 20 5b 48 31 32 35 30 36 5d 20  12505] [H12506] 
1aa70 5b 48 31 32 35 30 37 5d 20 5b 48 31 32 35 31 30  [H12507] [H12510
1aa80 5d 0a 2a 2a 20 5b 48 31 32 35 31 31 5d 20 5b 48  ].** [H12511] [H
1aa90 31 32 35 31 32 5d 20 5b 48 31 32 35 32 30 5d 20  12512] [H12520] 
1aaa0 5b 48 31 32 35 32 31 5d 20 5b 48 31 32 35 32 32  [H12521] [H12522
1aab0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
1aac0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  int sqlite3_set_
1aad0 61 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71  authorizer(.  sq
1aae0 6c 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a  lite3*,.  int (*
1aaf0 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74  xAuth)(void*,int
1ab00 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
1ab10 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
1ab20 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
1ab30 29 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72  ),.  void *pUser
1ab40 44 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  Data.);../*.** C
1ab50 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69  API3REF: Authori
1ab60 7a 65 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73  zer Return Codes
1ab70 20 7b 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30   {H12590} <H1250
1ab80 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71  0>.**.** The [sq
1ab90 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
1aba0 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65  izer | authorize
1abb0 72 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  r callback funct
1abc0 69 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74  ion] must.** ret
1abd0 75 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49  urn either [SQLI
1abe0 54 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66  TE_OK] or one of
1abf0 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74   these two const
1ac00 61 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a  ants in order.**
1ac10 20 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74   to signal SQLit
1ac20 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
1ac30 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70   the action is p
1ac40 65 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74  ermitted.  See t
1ac50 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73  he.** [sqlite3_s
1ac60 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20  et_authorizer | 
1ac70 61 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d  authorizer docum
1ac80 65 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64  entation] for ad
1ac90 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f  ditional.** info
1aca0 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66  rmation..*/.#def
1acb0 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20  ine SQLITE_DENY 
1acc0 20 20 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74    1   /* Abort t
1acd0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1ace0 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a   with an error *
1acf0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1ad00 5f 49 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44  _IGNORE 2   /* D
1ad10 6f 6e 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73  on't allow acces
1ad20 73 2c 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e  s, but don't gen
1ad30 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a  erate an error *
1ad40 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  /../*.** CAPI3RE
1ad50 46 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63  F: Authorizer Ac
1ad60 74 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35  tion Codes {H125
1ad70 35 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a  50} <H12500>.**.
1ad80 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
1ad90 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29  set_authorizer()
1ada0 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69  ] interface regi
1adb0 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b  sters a callback
1adc0 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61   function.** tha
1add0 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20  t is invoked to 
1ade0 61 75 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69  authorize certai
1adf0 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  n SQL statement 
1ae00 61 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a  actions.  The.**
1ae10 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
1ae20 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  r to the callbac
1ae30 6b 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  k is an integer 
1ae40 63 6f 64 65 20 74 68 61 74 20 73 70 65 63 69 66  code that specif
1ae50 69 65 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69  ies.** what acti
1ae60 6f 6e 20 69 73 20 62 65 69 6e 67 20 61 75 74 68  on is being auth
1ae70 6f 72 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61  orized.  These a
1ae80 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 61  re the integer a
1ae90 63 74 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74  ction codes that
1aea0 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a  .** the authoriz
1aeb0 65 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20  er callback may 
1aec0 62 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a  be passed..**.**
1aed0 20 54 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f   These action co
1aee0 64 65 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66  de values signif
1aef0 79 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f  y what kind of o
1af00 70 65 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62  peration is to b
1af10 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e  e.** authorized.
1af20 20 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 74    The 3rd and 4t
1af30 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  h parameters to 
1af40 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  the authorizatio
1af50 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75  n.** callback fu
1af60 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70  nction will be p
1af70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c  arameters or NUL
1af80 4c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77  L depending on w
1af90 68 69 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a  hich of these.**
1afa0 20 63 6f 64 65 73 20 69 73 20 75 73 65 64 20 61   codes is used a
1afb0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  s the second par
1afc0 61 6d 65 74 65 72 2e 20 20 54 68 65 20 35 74 68  ameter.  The 5th
1afd0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
1afe0 65 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20  e.** authorizer 
1aff0 63 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20  callback is the 
1b000 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
1b010 62 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74  base ("main", "t
1b020 65 6d 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69  emp",.** etc.) i
1b030 66 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54  f applicable.  T
1b040 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 72  he 6th parameter
1b050 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a   to the authoriz
1b060 65 72 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69  er callback.** i
1b070 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
1b080 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69  e inner-most tri
1b090 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 61  gger or view tha
1b0a0 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65  t is responsible
1b0b0 20 66 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65   for.** the acce
1b0c0 73 73 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55  ss attempt or NU
1b0d0 4c 4c 20 69 66 20 74 68 69 73 20 61 63 63 65 73  LL if this acces
1b0e0 73 20 61 74 74 65 6d 70 74 20 69 73 20 64 69 72  s attempt is dir
1b0f0 65 63 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f  ectly from.** to
1b100 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65  p-level SQL code
1b110 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
1b120 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 35 35 31  ents:.** [H12551
1b130 5d 20 5b 48 31 32 35 35 32 5d 20 5b 48 31 32 35  ] [H12552] [H125
1b140 35 33 5d 20 5b 48 31 32 35 35 34 5d 0a 2a 2f 0a  53] [H12554].*/.
1b150 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
1b160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 33 72 64  ************ 3rd
1b180 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 34 74   ************ 4t
1b190 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23  h ***********/.#
1b1a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
1b1b0 45 41 54 45 5f 49 4e 44 45 58 20 20 20 20 20 20  EATE_INDEX      
1b1c0 20 20 20 20 31 20 20 20 2f 2a 20 49 6e 64 65 78      1   /* Index
1b1d0 20 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65   Name      Table
1b1e0 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64   Name      */.#d
1b1f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45  efine SQLITE_CRE
1b200 41 54 45 5f 54 41 42 4c 45 20 20 20 20 20 20 20  ATE_TABLE       
1b210 20 20 20 32 20 20 20 2f 2a 20 54 61 62 6c 65 20     2   /* Table 
1b220 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20  Name      NULL  
1b230 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1b240 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41  fine SQLITE_CREA
1b250 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 20 20 20  TE_TEMP_INDEX   
1b260 20 20 33 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e    3   /* Index N
1b270 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e  ame      Table N
1b280 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
1b290 69 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54  ine SQLITE_CREAT
1b2a0 45 5f 54 45 4d 50 5f 54 41 42 4c 45 20 20 20 20  E_TEMP_TABLE    
1b2b0 20 34 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61   4   /* Table Na
1b2c0 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1b2d0 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1b2e0 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
1b2f0 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 20 20 20  _TEMP_TRIGGER   
1b300 35 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e  5   /* Trigger N
1b310 61 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  ame    Table Nam
1b320 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
1b330 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
1b340 54 45 4d 50 5f 56 49 45 57 20 20 20 20 20 20 36  TEMP_VIEW      6
1b350 20 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20     /* View Name 
1b360 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1b370 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b380 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
1b390 52 49 47 47 45 52 20 20 20 20 20 20 20 20 37 20  RIGGER        7 
1b3a0 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d    /* Trigger Nam
1b3b0 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20  e    Table Name 
1b3c0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b3d0 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49  SQLITE_CREATE_VI
1b3e0 45 57 20 20 20 20 20 20 20 20 20 20 20 38 20 20  EW           8  
1b3f0 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20   /* View Name   
1b400 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
1b410 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b420 51 4c 49 54 45 5f 44 45 4c 45 54 45 20 20 20 20  QLITE_DELETE    
1b430 20 20 20 20 20 20 20 20 20 20 20 20 39 20 20 20              9   
1b440 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20  /* Table Name   
1b450 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
1b460 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b470 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 20  LITE_DROP_INDEX 
1b480 20 20 20 20 20 20 20 20 20 20 31 30 20 20 20 2f            10   /
1b490 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20  * Index Name    
1b4a0 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
1b4b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b4c0 49 54 45 5f 44 52 4f 50 5f 54 41 42 4c 45 20 20  ITE_DROP_TABLE  
1b4d0 20 20 20 20 20 20 20 20 20 31 31 20 20 20 2f 2a           11   /*
1b4e0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1b4f0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1b500 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b510 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44  TE_DROP_TEMP_IND
1b520 45 58 20 20 20 20 20 20 31 32 20 20 20 2f 2a 20  EX      12   /* 
1b530 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20  Index Name      
1b540 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b550 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b560 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 41 42 4c  E_DROP_TEMP_TABL
1b570 45 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 54  E      13   /* T
1b580 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
1b590 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1b5a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b5b0 5f 44 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47  _DROP_TEMP_TRIGG
1b5c0 45 52 20 20 20 20 31 34 20 20 20 2f 2a 20 54 72  ER    14   /* Tr
1b5d0 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61  igger Name    Ta
1b5e0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
1b5f0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b600 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 20 20  DROP_TEMP_VIEW  
1b610 20 20 20 20 20 31 35 20 20 20 2f 2a 20 56 69 65       15   /* Vie
1b620 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c  w Name       NUL
1b630 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1b640 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
1b650 52 4f 50 5f 54 52 49 47 47 45 52 20 20 20 20 20  ROP_TRIGGER     
1b660 20 20 20 20 31 36 20 20 20 2f 2a 20 54 72 69 67      16   /* Trig
1b670 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
1b680 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23  e Name      */.#
1b690 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52  define SQLITE_DR
1b6a0 4f 50 5f 56 49 45 57 20 20 20 20 20 20 20 20 20  OP_VIEW         
1b6b0 20 20 20 31 37 20 20 20 2f 2a 20 56 69 65 77 20     17   /* View 
1b6c0 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20  Name       NULL 
1b6d0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1b6e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 53  efine SQLITE_INS
1b6f0 45 52 54 20 20 20 20 20 20 20 20 20 20 20 20 20  ERT             
1b700 20 20 31 38 20 20 20 2f 2a 20 54 61 62 6c 65 20    18   /* Table 
1b710 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20  Name      NULL  
1b720 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1b730 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 41 47  fine SQLITE_PRAG
1b740 4d 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MA              
1b750 20 31 39 20 20 20 2f 2a 20 50 72 61 67 6d 61 20   19   /* Pragma 
1b760 4e 61 6d 65 20 20 20 20 20 31 73 74 20 61 72 67  Name     1st arg
1b770 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66   or NULL */.#def
1b780 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 41 44 20  ine SQLITE_READ 
1b790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b7a0 32 30 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  20   /* Table Na
1b7b0 6d 65 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e  me      Column N
1b7c0 61 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  ame     */.#defi
1b7d0 6e 65 20 53 51 4c 49 54 45 5f 53 45 4c 45 43 54  ne SQLITE_SELECT
1b7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
1b7f0 31 20 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20  1   /* NULL     
1b800 20 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20         NULL     
1b810 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
1b820 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43  e SQLITE_TRANSAC
1b830 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 32 32  TION          22
1b840 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20     /* Operation 
1b850 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1b860 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b870 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 20   SQLITE_UPDATE  
1b880 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33 20               23 
1b890 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
1b8a0 20 20 20 20 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65       Column Name
1b8b0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b8c0 53 51 4c 49 54 45 5f 41 54 54 41 43 48 20 20 20  SQLITE_ATTACH   
1b8d0 20 20 20 20 20 20 20 20 20 20 20 20 32 34 20 20              24  
1b8e0 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 20 20 20   /* Filename    
1b8f0 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
1b900 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b910 51 4c 49 54 45 5f 44 45 54 41 43 48 20 20 20 20  QLITE_DETACH    
1b920 20 20 20 20 20 20 20 20 20 20 20 32 35 20 20 20             25   
1b930 2f 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d 65  /* Database Name
1b940 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
1b950 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b960 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45  LITE_ALTER_TABLE
1b970 20 20 20 20 20 20 20 20 20 20 32 36 20 20 20 2f            26   /
1b980 2a 20 44 61 74 61 62 61 73 65 20 4e 61 6d 65 20  * Database Name 
1b990 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
1b9a0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b9b0 49 54 45 5f 52 45 49 4e 44 45 58 20 20 20 20 20  ITE_REINDEX     
1b9c0 20 20 20 20 20 20 20 20 20 32 37 20 20 20 2f 2a           27   /*
1b9d0 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20   Index Name     
1b9e0 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1b9f0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1ba00 54 45 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 20  TE_ANALYZE      
1ba10 20 20 20 20 20 20 20 20 32 38 20 20 20 2f 2a 20          28   /* 
1ba20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1ba30 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
1ba40 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1ba50 45 5f 43 52 45 41 54 45 5f 56 54 41 42 4c 45 20  E_CREATE_VTABLE 
1ba60 20 20 20 20 20 20 20 32 39 20 20 20 2f 2a 20 54         29   /* T
1ba70 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4d  able Name      M
1ba80 6f 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a  odule Name     *
1ba90 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1baa0 5f 44 52 4f 50 5f 56 54 41 42 4c 45 20 20 20 20  _DROP_VTABLE    
1bab0 20 20 20 20 20 20 33 30 20 20 20 2f 2a 20 54 61        30   /* Ta
1bac0 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4d 6f  ble Name      Mo
1bad0 64 75 6c 65 20 4e 61 6d 65 20 20 20 20 20 2a 2f  dule Name     */
1bae0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1baf0 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20 20  FUNCTION        
1bb00 20 20 20 20 20 33 31 20 20 20 2f 2a 20 4e 55 4c       31   /* NUL
1bb10 4c 20 20 20 20 20 20 20 20 20 20 20 20 46 75 6e  L            Fun
1bb20 63 74 69 6f 6e 20 4e 61 6d 65 20 20 20 2a 2f 0a  ction Name   */.
1bb30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
1bb40 41 56 45 50 4f 49 4e 54 20 20 20 20 20 20 20 20  AVEPOINT        
1bb50 20 20 20 20 33 32 20 20 20 2f 2a 20 4f 70 65 72      32   /* Oper
1bb60 61 74 69 6f 6e 20 20 20 20 20 20 20 53 61 76 65  ation       Save
1bb70 70 6f 69 6e 74 20 4e 61 6d 65 20 20 2a 2f 0a 23  point Name  */.#
1bb80 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
1bb90 50 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20  PY              
1bba0 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 20 6c 6f      0   /* No lo
1bbb0 6e 67 65 72 20 75 73 65 64 20 2a 2f 0a 0a 2f 2a  nger used */../*
1bbc0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 72  .** CAPI3REF: Tr
1bbd0 61 63 69 6e 67 20 41 6e 64 20 50 72 6f 66 69 6c  acing And Profil
1bbe0 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48  ing Functions {H
1bbf0 31 32 32 38 30 7d 20 3c 53 36 30 34 30 30 3e 0a  12280} <S60400>.
1bc00 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
1bc10 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
1bc20 69 6e 65 73 20 72 65 67 69 73 74 65 72 20 63 61  ines register ca
1bc30 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 73  llback functions
1bc40 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
1bc50 64 20 66 6f 72 0a 2a 2a 20 74 72 61 63 69 6e 67  d for.** tracing
1bc60 20 61 6e 64 20 70 72 6f 66 69 6c 69 6e 67 20 74   and profiling t
1bc70 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  he execution of 
1bc80 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  SQL statements..
1bc90 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61  **.** The callba
1bca0 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69  ck function regi
1bcb0 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 74 65  stered by sqlite
1bcc0 33 5f 74 72 61 63 65 28 29 20 69 73 20 69 6e 76  3_trace() is inv
1bcd0 6f 6b 65 64 20 61 74 0a 2a 2a 20 76 61 72 69 6f  oked at.** vario
1bce0 75 73 20 74 69 6d 65 73 20 77 68 65 6e 20 61 6e  us times when an
1bcf0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69   SQL statement i
1bd00 73 20 62 65 69 6e 67 20 72 75 6e 20 62 79 20 5b  s being run by [
1bd10 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e  sqlite3_step()].
1bd20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b  .** The callback
1bd30 20 72 65 74 75 72 6e 73 20 61 20 55 54 46 2d 38   returns a UTF-8
1bd40 20 72 65 6e 64 65 72 69 6e 67 20 6f 66 20 74 68   rendering of th
1bd50 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
1bd60 74 65 78 74 0a 2a 2a 20 61 73 20 74 68 65 20 73  text.** as the s
1bd70 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74 20 62  tatement first b
1bd80 65 67 69 6e 73 20 65 78 65 63 75 74 69 6e 67 2e  egins executing.
1bd90 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c    Additional cal
1bda0 6c 62 61 63 6b 73 20 6f 63 63 75 72 0a 2a 2a 20  lbacks occur.** 
1bdb0 61 73 20 65 61 63 68 20 74 72 69 67 67 65 72 65  as each triggere
1bdc0 64 20 73 75 62 70 72 6f 67 72 61 6d 20 69 73 20  d subprogram is 
1bdd0 65 6e 74 65 72 65 64 2e 20 20 54 68 65 20 63 61  entered.  The ca
1bde0 6c 6c 62 61 63 6b 73 20 66 6f 72 20 74 72 69 67  llbacks for trig
1bdf0 67 65 72 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20  gers.** contain 
1be00 61 20 55 54 46 2d 38 20 53 51 4c 20 63 6f 6d 6d  a UTF-8 SQL comm
1be10 65 6e 74 20 74 68 61 74 20 69 64 65 6e 74 69 66  ent that identif
1be20 69 65 73 20 74 68 65 20 74 72 69 67 67 65 72 2e  ies the trigger.
1be30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62  .**.** The callb
1be40 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 67  ack function reg
1be50 69 73 74 65 72 65 64 20 62 79 20 73 71 6c 69 74  istered by sqlit
1be60 65 33 5f 70 72 6f 66 69 6c 65 28 29 20 69 73 20  e3_profile() is 
1be70 69 6e 76 6f 6b 65 64 0a 2a 2a 20 61 73 20 65 61  invoked.** as ea
1be80 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ch SQL statement
1be90 20 66 69 6e 69 73 68 65 73 2e 20 20 54 68 65 20   finishes.  The 
1bea0 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b  profile callback
1beb0 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65   contains.** the
1bec0 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 6d   original statem
1bed0 65 6e 74 20 74 65 78 74 20 61 6e 64 20 61 6e 20  ent text and an 
1bee0 65 73 74 69 6d 61 74 65 20 6f 66 20 77 61 6c 6c  estimate of wall
1bef0 2d 63 6c 6f 63 6b 20 74 69 6d 65 0a 2a 2a 20 6f  -clock time.** o
1bf00 66 20 68 6f 77 20 6c 6f 6e 67 20 74 68 61 74 20  f how long that 
1bf10 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f 6b 20 74  statement took t
1bf20 6f 20 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  o run..**.** Req
1bf30 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
1bf40 31 32 32 38 31 5d 20 5b 48 31 32 32 38 32 5d 20  12281] [H12282] 
1bf50 5b 48 31 32 32 38 33 5d 20 5b 48 31 32 32 38 34  [H12283] [H12284
1bf60 5d 20 5b 48 31 32 32 38 35 5d 20 5b 48 31 32 32  ] [H12285] [H122
1bf70 38 37 5d 20 5b 48 31 32 32 38 38 5d 20 5b 48 31  87] [H12288] [H1
1bf80 32 32 38 39 5d 0a 2a 2a 20 5b 48 31 32 32 39 30  2289].** [H12290
1bf90 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
1bfa0 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e  SQLITE_EXPERIMEN
1bfb0 54 41 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  TAL void *sqlite
1bfc0 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33 2a  3_trace(sqlite3*
1bfd0 2c 20 76 6f 69 64 28 2a 78 54 72 61 63 65 29 28  , void(*xTrace)(
1bfe0 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
1bff0 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  *), void*);.SQLI
1c000 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
1c010 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20  PERIMENTAL void 
1c020 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65  *sqlite3_profile
1c030 28 73 71 6c 69 74 65 33 2a 2c 0a 20 20 20 76 6f  (sqlite3*,.   vo
1c040 69 64 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f  id(*xProfile)(vo
1c050 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  id*,const char*,
1c060 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 29 2c  sqlite3_uint64),
1c070 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20   void*);../*.** 
1c080 43 41 50 49 33 52 45 46 3a 20 51 75 65 72 79 20  CAPI3REF: Query 
1c090 50 72 6f 67 72 65 73 73 20 43 61 6c 6c 62 61 63  Progress Callbac
1c0a0 6b 73 20 7b 48 31 32 39 31 30 7d 20 3c 53 36 30  ks {H12910} <S60
1c0b0 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  400>.**.** This 
1c0c0 72 6f 75 74 69 6e 65 20 63 6f 6e 66 69 67 75 72  routine configur
1c0d0 65 73 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 75  es a callback fu
1c0e0 6e 63 74 69 6f 6e 20 2d 20 74 68 65 0a 2a 2a 20  nction - the.** 
1c0f0 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63  progress callbac
1c100 6b 20 2d 20 74 68 61 74 20 69 73 20 69 6e 76 6f  k - that is invo
1c110 6b 65 64 20 70 65 72 69 6f 64 69 63 61 6c 6c 79  ked periodically
1c120 20 64 75 72 69 6e 67 20 6c 6f 6e 67 0a 2a 2a 20   during long.** 
1c130 72 75 6e 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f  running calls to
1c140 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
1c150 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  ], [sqlite3_step
1c160 28 29 5d 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69  ()] and.** [sqli
1c170 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d  te3_get_table()]
1c180 2e 20 20 41 6e 20 65 78 61 6d 70 6c 65 20 75 73  .  An example us
1c190 65 20 66 6f 72 20 74 68 69 73 0a 2a 2a 20 69 6e  e for this.** in
1c1a0 74 65 72 66 61 63 65 20 69 73 20 74 6f 20 6b 65  terface is to ke
1c1b0 65 70 20 61 20 47 55 49 20 75 70 64 61 74 65 64  ep a GUI updated
1c1c0 20 64 75 72 69 6e 67 20 61 20 6c 61 72 67 65 20   during a large 
1c1d0 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  query..**.** If 
1c1e0 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
1c1f0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f  lback returns no
1c200 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6f 70 65 72  n-zero, the oper
1c210 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 74 65  ation is.** inte
1c220 72 72 75 70 74 65 64 2e 20 20 54 68 69 73 20 66  rrupted.  This f
1c230 65 61 74 75 72 65 20 63 61 6e 20 62 65 20 75 73  eature can be us
1c240 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
1c250 61 0a 2a 2a 20 22 43 61 6e 63 65 6c 22 20 62 75  a.** "Cancel" bu
1c260 74 74 6f 6e 20 6f 6e 20 61 20 47 55 49 20 70 72  tton on a GUI pr
1c270 6f 67 72 65 73 73 20 64 69 61 6c 6f 67 20 62 6f  ogress dialog bo
1c280 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72 6f  x..**.** The pro
1c290 67 72 65 73 73 20 68 61 6e 64 6c 65 72 20 6d 75  gress handler mu
1c2a0 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69  st not do anythi
1c2b0 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64  ng that will mod
1c2c0 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  ify.** the datab
1c2d0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
1c2e0 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20  hat invoked the 
1c2f0 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c 65 72  progress handler
1c300 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b  ..** Note that [
1c310 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
1c320 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  v2()] and [sqlit
1c330 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20  e3_step()] both 
1c340 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20  modify their.** 
1c350 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1c360 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61  ions for the mea
1c370 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22  ning of "modify"
1c380 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61   in this paragra
1c390 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ph..**.** Requir
1c3a0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 39  ements:.** [H129
1c3b0 31 31 5d 20 5b 48 31 32 39 31 32 5d 20 5b 48 31  11] [H12912] [H1
1c3c0 32 39 31 33 5d 20 5b 48 31 32 39 31 34 5d 20 5b  2913] [H12914] [
1c3d0 48 31 32 39 31 35 5d 20 5b 48 31 32 39 31 36 5d  H12915] [H12916]
1c3e0 20 5b 48 31 32 39 31 37 5d 20 5b 48 31 32 39 31   [H12917] [H1291
1c3f0 38 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  8].**.*/.SQLITE_
1c400 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1c410 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65  _progress_handle
1c420 72 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c  r(sqlite3*, int,
1c430 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20   int(*)(void*), 
1c440 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  void*);../*.** C
1c450 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 69 6e 67  API3REF: Opening
1c460 20 41 20 4e 65 77 20 44 61 74 61 62 61 73 65 20   A New Database 
1c470 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 32 37  Connection {H127
1c480 30 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a  00} <S40200>.**.
1c490 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
1c4a0 73 20 6f 70 65 6e 20 61 6e 20 53 51 4c 69 74 65  s open an SQLite
1c4b0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
1c4c0 68 6f 73 65 20 6e 61 6d 65 20 69 73 20 67 69 76  hose name is giv
1c4d0 65 6e 20 62 79 20 74 68 65 0a 2a 2a 20 66 69 6c  en by the.** fil
1c4e0 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 2e 20  ename argument. 
1c4f0 54 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67  The filename arg
1c500 75 6d 65 6e 74 20 69 73 20 69 6e 74 65 72 70 72  ument is interpr
1c510 65 74 65 64 20 61 73 20 55 54 46 2d 38 20 66 6f  eted as UTF-8 fo
1c520 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65  r.** sqlite3_ope
1c530 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  n() and sqlite3_
1c540 6f 70 65 6e 5f 76 32 28 29 20 61 6e 64 20 61 73  open_v2() and as
1c550 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e   UTF-16 in the n
1c560 61 74 69 76 65 20 62 79 74 65 0a 2a 2a 20 6f 72  ative byte.** or
1c570 64 65 72 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  der for sqlite3_
1c580 6f 70 65 6e 31 36 28 29 2e 20 41 20 5b 64 61 74  open16(). A [dat
1c590 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1c5a0 5d 20 68 61 6e 64 6c 65 20 69 73 20 75 73 75 61  ] handle is usua
1c5b0 6c 6c 79 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  lly.** returned 
1c5c0 69 6e 20 2a 70 70 44 62 2c 20 65 76 65 6e 20 69  in *ppDb, even i
1c5d0 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
1c5e0 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 65 78 63  s.  The only exc
1c5f0 65 70 74 69 6f 6e 20 69 73 20 74 68 61 74 0a 2a  eption is that.*
1c600 2a 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 75  * if SQLite is u
1c610 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74  nable to allocat
1c620 65 20 6d 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64  e memory to hold
1c630 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f   the [sqlite3] o
1c640 62 6a 65 63 74 2c 0a 2a 2a 20 61 20 4e 55 4c 4c  bject,.** a NULL
1c650 20 77 69 6c 6c 20 62 65 20 77 72 69 74 74 65 6e   will be written
1c660 20 69 6e 74 6f 20 2a 70 70 44 62 20 69 6e 73 74   into *ppDb inst
1c670 65 61 64 20 6f 66 20 61 20 70 6f 69 6e 74 65 72  ead of a pointer
1c680 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
1c690 5d 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 49 66 20  ].** object. If 
1c6a0 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
1c6b0 6f 70 65 6e 65 64 20 28 61 6e 64 2f 6f 72 20 63  opened (and/or c
1c6c0 72 65 61 74 65 64 29 20 73 75 63 63 65 73 73 66  reated) successf
1c6d0 75 6c 6c 79 2c 20 74 68 65 6e 0a 2a 2a 20 5b 53  ully, then.** [S
1c6e0 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74  QLITE_OK] is ret
1c6f0 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69 73  urned.  Otherwis
1c700 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  e an [error code
1c710 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  ] is returned.  
1c720 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  The.** [sqlite3_
1c730 65 72 72 6d 73 67 28 29 5d 20 6f 72 20 5b 73 71  errmsg()] or [sq
1c740 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29  lite3_errmsg16()
1c750 5d 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20 62  ] routines can b
1c760 65 20 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e  e used to obtain
1c770 0a 2a 2a 20 61 6e 20 45 6e 67 6c 69 73 68 20 6c  .** an English l
1c780 61 6e 67 75 61 67 65 20 64 65 73 63 72 69 70 74  anguage descript
1c790 69 6f 6e 20 6f 66 20 74 68 65 20 65 72 72 6f 72  ion of the error
1c7a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  ..**.** The defa
1c7b0 75 6c 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72  ult encoding for
1c7c0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 69   the database wi
1c7d0 6c 6c 20 62 65 20 55 54 46 2d 38 20 69 66 0a 2a  ll be UTF-8 if.*
1c7e0 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  * sqlite3_open()
1c7f0 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   or sqlite3_open
1c800 5f 76 32 28 29 20 69 73 20 63 61 6c 6c 65 64 20  _v2() is called 
1c810 61 6e 64 0a 2a 2a 20 55 54 46 2d 31 36 20 69 6e  and.** UTF-16 in
1c820 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65   the native byte
1c830 20 6f 72 64 65 72 20 69 66 20 73 71 6c 69 74 65   order if sqlite
1c840 33 5f 6f 70 65 6e 31 36 28 29 20 69 73 20 75 73  3_open16() is us
1c850 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65  ed..**.** Whethe
1c860 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f  r or not an erro
1c870 72 20 6f 63 63 75 72 73 20 77 68 65 6e 20 69 74  r occurs when it
1c880 20 69 73 20 6f 70 65 6e 65 64 2c 20 72 65 73 6f   is opened, reso
1c890 75 72 63 65 73 0a 2a 2a 20 61 73 73 6f 63 69 61  urces.** associa
1c8a0 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64 61  ted with the [da
1c8b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1c8c0 6e 5d 20 68 61 6e 64 6c 65 20 73 68 6f 75 6c 64  n] handle should
1c8d0 20 62 65 20 72 65 6c 65 61 73 65 64 20 62 79 0a   be released by.
1c8e0 2a 2a 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f  ** passing it to
1c8f0 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28   [sqlite3_close(
1c900 29 5d 20 77 68 65 6e 20 69 74 20 69 73 20 6e 6f  )] when it is no
1c910 20 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64   longer required
1c920 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
1c930 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 6e  te3_open_v2() in
1c940 74 65 72 66 61 63 65 20 77 6f 72 6b 73 20 6c 69  terface works li
1c950 6b 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ke sqlite3_open(
1c960 29 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74  ).** except that
1c970 20 69 74 20 61 63 63 65 70 74 73 20 74 77 6f 20   it accepts two 
1c980 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d  additional param
1c990 65 74 65 72 73 20 66 6f 72 20 61 64 64 69 74 69  eters for additi
1c9a0 6f 6e 61 6c 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20  onal control.** 
1c9b0 6f 76 65 72 20 74 68 65 20 6e 65 77 20 64 61 74  over the new dat
1c9c0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1c9d0 2e 20 20 54 68 65 20 66 6c 61 67 73 20 70 61 72  .  The flags par
1c9e0 61 6d 65 74 65 72 20 63 61 6e 20 74 61 6b 65 20  ameter can take 
1c9f0 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f  one of.** the fo
1ca00 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65 20 76 61  llowing three va
1ca10 6c 75 65 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c 79  lues, optionally
1ca20 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 74   combined with t
1ca30 68 65 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f  he .** [SQLITE_O
1ca40 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 2c 20 5b 53  PEN_NOMUTEX], [S
1ca50 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d  QLITE_OPEN_FULLM
1ca60 55 54 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f  UTEX], [SQLITE_O
1ca70 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 5d  PEN_SHAREDCACHE]
1ca80 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b 53 51 4c  ,.** and/or [SQL
1ca90 49 54 45 5f 4f 50 45 4e 5f 50 52 49 56 41 54 45  ITE_OPEN_PRIVATE
1caa0 43 41 43 48 45 5d 20 66 6c 61 67 73 3a 0a 2a 2a  CACHE] flags:.**
1cab0 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e  .** <dl>.** <dt>
1cac0 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  [SQLITE_OPEN_REA
1cad0 44 4f 4e 4c 59 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c  DONLY]</dt>.** <
1cae0 64 64 3e 54 68 65 20 64 61 74 61 62 61 73 65 20  dd>The database 
1caf0 69 73 20 6f 70 65 6e 65 64 20 69 6e 20 72 65 61  is opened in rea
1cb00 64 2d 6f 6e 6c 79 20 6d 6f 64 65 2e 20 20 49 66  d-only mode.  If
1cb10 20 74 68 65 20 64 61 74 61 62 61 73 65 20 64 6f   the database do
1cb20 65 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64  es not.** alread
1cb30 79 20 65 78 69 73 74 2c 20 61 6e 20 65 72 72 6f  y exist, an erro
1cb40 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f  r is returned.</
1cb50 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53  dd>.**.** <dt>[S
1cb60 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
1cb70 52 49 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  RITE]</dt>.** <d
1cb80 64 3e 54 68 65 20 64 61 74 61 62 61 73 65 20 69  d>The database i
1cb90 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61  s opened for rea
1cba0 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67  ding and writing
1cbb0 20 69 66 20 70 6f 73 73 69 62 6c 65 2c 20 6f 72   if possible, or
1cbc0 20 72 65 61 64 69 6e 67 0a 2a 2a 20 6f 6e 6c 79   reading.** only
1cbd0 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   if the file is 
1cbe0 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 64 20  write protected 
1cbf0 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  by the operating
1cc00 20 73 79 73 74 65 6d 2e 20 20 49 6e 20 65 69 74   system.  In eit
1cc10 68 65 72 0a 2a 2a 20 63 61 73 65 20 74 68 65 20  her.** case the 
1cc20 64 61 74 61 62 61 73 65 20 6d 75 73 74 20 61 6c  database must al
1cc30 72 65 61 64 79 20 65 78 69 73 74 2c 20 6f 74 68  ready exist, oth
1cc40 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20  erwise an error 
1cc50 69 73 20 72 65 74 75 72 6e 65 64 2e 3c 2f 64 64  is returned.</dd
1cc60 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c  >.**.** <dt>[SQL
1cc70 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
1cc80 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50  TE] | [SQLITE_OP
1cc90 45 4e 5f 43 52 45 41 54 45 5d 3c 2f 64 74 3e 0a  EN_CREATE]</dt>.
1cca0 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74 61 62  ** <dd>The datab
1ccb0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f  ase is opened fo
1ccc0 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72  r reading and wr
1ccd0 69 74 69 6e 67 2c 20 61 6e 64 20 69 73 20 63 72  iting, and is cr
1cce0 65 61 74 65 73 20 69 74 20 69 66 0a 2a 2a 20 69  eates it if.** i
1ccf0 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61  t does not alrea
1cd00 64 79 20 65 78 69 73 74 2e 20 54 68 69 73 20 69  dy exist. This i
1cd10 73 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 74  s the behavior t
1cd20 68 61 74 20 69 73 20 61 6c 77 61 79 73 20 75 73  hat is always us
1cd30 65 64 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  ed for.** sqlite
1cd40 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c  3_open() and sql
1cd50 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e 3c 2f  ite3_open16().</
1cd60 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a  dd>.** </dl>.**.
1cd70 2a 2a 20 49 66 20 74 68 65 20 33 72 64 20 70 61  ** If the 3rd pa
1cd80 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
1cd90 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20  e3_open_v2() is 
1cda0 6e 6f 74 20 6f 6e 65 20 6f 66 20 74 68 65 0a 2a  not one of the.*
1cdb0 2a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73  * combinations s
1cdc0 68 6f 77 6e 20 61 62 6f 76 65 20 6f 72 20 6f 6e  hown above or on
1cdd0 65 20 6f 66 20 74 68 65 20 63 6f 6d 62 69 6e 61  e of the combina
1cde0 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76  tions shown abov
1cdf0 65 20 63 6f 6d 62 69 6e 65 64 0a 2a 2a 20 77 69  e combined.** wi
1ce00 74 68 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f  th the [SQLITE_O
1ce10 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 2c 20 5b 53  PEN_NOMUTEX], [S
1ce20 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d  QLITE_OPEN_FULLM
1ce30 55 54 45 58 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54  UTEX],.** [SQLIT
1ce40 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43  E_OPEN_SHAREDCAC
1ce50 48 45 5d 20 61 6e 64 2f 6f 72 20 5b 53 51 4c 49  HE] and/or [SQLI
1ce60 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
1ce70 43 48 45 5d 20 66 6c 61 67 73 2c 0a 2a 2a 20 74  CHE] flags,.** t
1ce80 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72  hen the behavior
1ce90 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
1cea0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 53 51 4c  *.** If the [SQL
1ceb0 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58  ITE_OPEN_NOMUTEX
1cec0 5d 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74  ] flag is set, t
1ced0 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
1cee0 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f   connection.** o
1cef0 70 65 6e 73 20 69 6e 20 74 68 65 20 6d 75 6c 74  pens in the mult
1cf00 69 2d 74 68 72 65 61 64 20 5b 74 68 72 65 61 64  i-thread [thread
1cf10 69 6e 67 20 6d 6f 64 65 5d 20 61 73 20 6c 6f 6e  ing mode] as lon
1cf20 67 20 61 73 20 74 68 65 20 73 69 6e 67 6c 65 2d  g as the single-
1cf30 74 68 72 65 61 64 0a 2a 2a 20 6d 6f 64 65 20 68  thread.** mode h
1cf40 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 20  as not been set 
1cf50 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20  at compile-time 
1cf60 6f 72 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20  or start-time.  
1cf70 49 66 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54  If the.** [SQLIT
1cf80 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58  E_OPEN_FULLMUTEX
1cf90 5d 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 68  ] flag is set th
1cfa0 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
1cfb0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65 6e 73  connection opens
1cfc0 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65 72 69 61  .** in the seria
1cfd0 6c 69 7a 65 64 20 5b 74 68 72 65 61 64 69 6e 67  lized [threading
1cfe0 20 6d 6f 64 65 5d 20 75 6e 6c 65 73 73 20 73 69   mode] unless si
1cff0 6e 67 6c 65 2d 74 68 72 65 61 64 20 77 61 73 0a  ngle-thread was.
1d000 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65  ** previously se
1d010 6c 65 63 74 65 64 20 61 74 20 63 6f 6d 70 69 6c  lected at compil
1d020 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d  e-time or start-
1d030 74 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 5b 53 51  time..** The [SQ
1d040 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44  LITE_OPEN_SHARED
1d050 43 41 43 48 45 5d 20 66 6c 61 67 20 63 61 75 73  CACHE] flag caus
1d060 65 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  es the database 
1d070 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 62 65  connection to be
1d080 0a 2a 2a 20 65 6c 69 67 69 62 6c 65 20 74 6f 20  .** eligible to 
1d090 75 73 65 20 5b 73 68 61 72 65 64 20 63 61 63 68  use [shared cach
1d0a0 65 20 6d 6f 64 65 5d 2c 20 72 65 67 61 72 64 6c  e mode], regardl
1d0b0 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f  ess of whether o
1d0c0 72 20 6e 6f 74 20 73 68 61 72 65 64 0a 2a 2a 20  r not shared.** 
1d0d0 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c 65 64  cache is enabled
1d0e0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
1d0f0 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61  enable_shared_ca
1d100 63 68 65 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20  che()].  The.** 
1d110 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52 49  [SQLITE_OPEN_PRI
1d120 56 41 54 45 43 41 43 48 45 5d 20 66 6c 61 67 20  VATECACHE] flag 
1d130 63 61 75 73 65 73 20 74 68 65 20 64 61 74 61 62  causes the datab
1d140 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  ase connection t
1d150 6f 20 6e 6f 74 0a 2a 2a 20 70 61 72 74 69 63 69  o not.** partici
1d160 70 61 74 65 20 69 6e 20 5b 73 68 61 72 65 64 20  pate in [shared 
1d170 63 61 63 68 65 20 6d 6f 64 65 5d 20 65 76 65 6e  cache mode] even
1d180 20 69 66 20 69 74 20 69 73 20 65 6e 61 62 6c 65   if it is enable
1d190 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
1d1a0 66 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65  filename is ":me
1d1b0 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70  mory:", then a p
1d1c0 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72  rivate, temporar
1d1d0 79 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  y in-memory data
1d1e0 62 61 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74  base.** is creat
1d1f0 65 64 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65  ed for the conne
1d200 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d  ction.  This in-
1d210 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20  memory database 
1d220 77 69 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e  will vanish when
1d230 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
1d240 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63   connection is c
1d250 6c 6f 73 65 64 2e 20 20 46 75 74 75 72 65 20 76  losed.  Future v
1d260 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
1d270 65 20 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20  e might.** make 
1d280 75 73 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61  use of additiona
1d290 6c 20 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61  l special filena
1d2a0 6d 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77  mes that begin w
1d2b0 69 74 68 20 74 68 65 20 22 3a 22 20 63 68 61 72  ith the ":" char
1d2c0 61 63 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20  acter..** It is 
1d2d0 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74  recommended that
1d2e0 20 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 65   when a database
1d2f0 20 66 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c   filename actual
1d300 6c 79 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69  ly does begin wi
1d310 74 68 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72  th.** a ":" char
1d320 61 63 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64  acter you should
1d330 20 70 72 65 66 69 78 20 74 68 65 20 66 69 6c 65   prefix the file
1d340 6e 61 6d 65 20 77 69 74 68 20 61 20 70 61 74 68  name with a path
1d350 6e 61 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20  name such as.** 
1d360 22 2e 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d  "./" to avoid am
1d370 62 69 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49  biguity..**.** I
1d380 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69  f the filename i
1d390 73 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e  s an empty strin
1d3a0 67 2c 20 74 68 65 6e 20 61 20 70 72 69 76 61 74  g, then a privat
1d3b0 65 2c 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20  e, temporary.** 
1d3c0 6f 6e 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65  on-disk database
1d3d0 20 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64   will be created
1d3e0 2e 20 20 54 68 69 73 20 70 72 69 76 61 74 65 20  .  This private 
1d3f0 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65  database will be
1d400 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  .** automaticall
1d410 79 20 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f  y deleted as soo
1d420 6e 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73  n as the databas
1d430 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
1d440 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  closed..**.** Th
1d450 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74  e fourth paramet
1d460 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70  er to sqlite3_op
1d470 65 6e 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e  en_v2() is the n
1d480 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73  ame of the.** [s
1d490 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
1d4a0 63 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  ct that defines 
1d4b0 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
1d4c0 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74  stem interface t
1d4d0 68 61 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64  hat.** the new d
1d4e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1d4f0 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20  on should use.  
1d500 49 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61  If the fourth pa
1d510 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20  rameter is.** a 
1d520 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65  NULL pointer the
1d530 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73  n the default [s
1d540 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
1d550 63 74 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a  ct is used..**.*
1d560 2a 20 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e  * <b>Note to Win
1d570 64 6f 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20  dows users:</b> 
1d580 20 54 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73   The encoding us
1d590 65 64 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e  ed for the filen
1d5a0 61 6d 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ame argument.** 
1d5b0 6f 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  of sqlite3_open(
1d5c0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70  ) and sqlite3_op
1d5d0 65 6e 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20  en_v2() must be 
1d5e0 55 54 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65  UTF-8, not whate
1d5f0 76 65 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20  ver.** codepage 
1d600 69 73 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66  is currently def
1d610 69 6e 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73  ined.  Filenames
1d620 20 63 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65   containing inte
1d630 72 6e 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61  rnational.** cha
1d640 72 61 63 74 65 72 73 20 6d 75 73 74 20 62 65 20  racters must be 
1d650 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46  converted to UTF
1d660 2d 38 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73  -8 prior to pass
1d670 69 6e 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a  ing them into.**
1d680 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20   sqlite3_open() 
1d690 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  or sqlite3_open_
1d6a0 76 32 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  v2()..**.** Requ
1d6b0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
1d6c0 32 37 30 31 5d 20 5b 48 31 32 37 30 32 5d 20 5b  2701] [H12702] [
1d6d0 48 31 32 37 30 33 5d 20 5b 48 31 32 37 30 34 5d  H12703] [H12704]
1d6e0 20 5b 48 31 32 37 30 36 5d 20 5b 48 31 32 37 30   [H12706] [H1270
1d6f0 37 5d 20 5b 48 31 32 37 30 39 5d 20 5b 48 31 32  7] [H12709] [H12
1d700 37 31 31 5d 0a 2a 2a 20 5b 48 31 32 37 31 32 5d  711].** [H12712]
1d710 20 5b 48 31 32 37 31 33 5d 20 5b 48 31 32 37 31   [H12713] [H1271
1d720 34 5d 20 5b 48 31 32 37 31 37 5d 20 5b 48 31 32  4] [H12717] [H12
1d730 37 31 39 5d 20 5b 48 31 32 37 32 31 5d 20 5b 48  719] [H12721] [H
1d740 31 32 37 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12723].*/.SQLITE
1d750 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1d760 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63  _open(.  const c
1d770 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20  har *filename,  
1d780 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
1d790 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f  ename (UTF-8) */
1d7a0 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44  .  sqlite3 **ppD
1d7b0 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  b          /* OU
1d7c0 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e  T: SQLite db han
1d7d0 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45  dle */.);.SQLITE
1d7e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1d7f0 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74  _open16(.  const
1d800 20 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c   void *filename,
1d810 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66     /* Database f
1d820 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29  ilename (UTF-16)
1d830 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a   */.  sqlite3 **
1d840 70 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a  ppDb          /*
1d850 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20   OUT: SQLite db 
1d860 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c  handle */.);.SQL
1d870 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1d880 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63  te3_open_v2(.  c
1d890 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e  onst char *filen
1d8a0 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61  ame,   /* Databa
1d8b0 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46  se filename (UTF
1d8c0 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -8) */.  sqlite3
1d8d0 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20   **ppDb,        
1d8e0 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20   /* OUT: SQLite 
1d8f0 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69  db handle */.  i
1d900 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
1d910 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
1d920 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1d930 2a 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20  *zVfs        /* 
1d940 4e 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75  Name of VFS modu
1d950 6c 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a  le to use */.);.
1d960 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1d970 20 45 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64   Error Codes And
1d980 20 4d 65 73 73 61 67 65 73 20 7b 48 31 32 38 30   Messages {H1280
1d990 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a  0} <S60200>.**.*
1d9a0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72  * The sqlite3_er
1d9b0 72 63 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63  rcode() interfac
1d9c0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  e returns the nu
1d9d0 6d 65 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f  meric [result co
1d9e0 64 65 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e  de] or.** [exten
1d9f0 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d  ded result code]
1da00 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65   for the most re
1da10 63 65 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69  cent failed sqli
1da20 74 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a  te3_* API call.*
1da30 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
1da40 68 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f  h a [database co
1da50 6e 6e 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20  nnection]. If a 
1da60 70 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66  prior API call f
1da70 61 69 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65  ailed.** but the
1da80 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49   most recent API
1da90 20 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c   call succeeded,
1daa0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
1dab0 65 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65  e from.** sqlite
1dac0 33 5f 65 72 72 63 6f 64 65 28 29 20 69 73 20 75  3_errcode() is u
1dad0 6e 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73  ndefined.  The s
1dae0 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
1daf0 65 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74  errcode().** int
1db00 65 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61  erface is the sa
1db10 6d 65 20 65 78 63 65 70 74 20 74 68 61 74 20 69  me except that i
1db20 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  t always returns
1db30 20 74 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64   the .** [extend
1db40 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20  ed result code] 
1db50 65 76 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64  even when extend
1db60 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  ed result codes 
1db70 61 72 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e  are.** disabled.
1db80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
1db90 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20  e3_errmsg() and 
1dba0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
1dbb0 28 29 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73  () return Englis
1dbc0 68 2d 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65  h-language.** te
1dbd0 78 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65  xt that describe
1dbe0 73 20 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20  s the error, as 
1dbf0 65 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20  either UTF-8 or 
1dc00 55 54 46 2d 31 36 20 72 65 73 70 65 63 74 69 76  UTF-16 respectiv
1dc10 65 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74  ely..** Memory t
1dc20 6f 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72  o hold the error
1dc30 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20   message string 
1dc40 69 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72  is managed inter
1dc50 6e 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70  nally..** The ap
1dc60 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e  plication does n
1dc70 6f 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79  ot need to worry
1dc80 20 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74   about freeing t
1dc90 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f  he result..** Ho
1dca0 77 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72  wever, the error
1dcb0 20 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65   string might be
1dcc0 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20   overwritten or 
1dcd0 64 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a  deallocated by.*
1dce0 2a 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  * subsequent cal
1dcf0 6c 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69  ls to other SQLi
1dd00 74 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e  te interface fun
1dd10 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68  ctions..**.** Wh
1dd20 65 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65  en the serialize
1dd30 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  d [threading mod
1dd40 65 5d 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74  e] is in use, it
1dd50 20 6d 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a   might be the.**
1dd60 20 63 61 73 65 20 74 68 61 74 20 61 20 73 65 63   case that a sec
1dd70 6f 6e 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ond error occurs
1dd80 20 6f 6e 20 61 20 73 65 70 61 72 61 74 65 20 74   on a separate t
1dd90 68 72 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e  hread in between
1dda0 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20  .** the time of 
1ddb0 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20  the first error 
1ddc0 61 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  and the call to 
1ddd0 74 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73  these interfaces
1dde0 2e 0a 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68  ..** When that h
1ddf0 61 70 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f  appens, the seco
1de00 6e 64 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65  nd error will be
1de10 20 72 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20   reported since 
1de20 74 68 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61  these.** interfa
1de30 63 65 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72  ces always repor
1de40 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  t the most recen
1de50 74 20 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76  t result.  To av
1de60 6f 69 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63  oid.** this, eac
1de70 68 20 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74  h thread can obt
1de80 61 69 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73  ain exclusive us
1de90 65 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61  e of the [databa
1dea0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
1deb0 0a 2a 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20  .** by invoking 
1dec0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65  [sqlite3_mutex_e
1ded0 6e 74 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64  nter]([sqlite3_d
1dee0 62 5f 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66  b_mutex](D)) bef
1def0 6f 72 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a  ore beginning.**
1df00 20 74 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e   to use D and in
1df10 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  voking [sqlite3_
1df20 6d 75 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71  mutex_leave]([sq
1df30 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28  lite3_db_mutex](
1df40 44 29 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c  D)) after.** all
1df50 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e   calls to the in
1df60 74 65 72 66 61 63 65 73 20 6c 69 73 74 65 64 20  terfaces listed 
1df70 68 65 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74  here are complet
1df80 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ed..**.** If an 
1df90 69 6e 74 65 72 66 61 63 65 20 66 61 69 6c 73 20  interface fails 
1dfa0 77 69 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55  with SQLITE_MISU
1dfb0 53 45 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  SE, that means t
1dfc0 68 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  he interface.** 
1dfd0 77 61 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f  was invoked inco
1dfe0 72 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 61  rrectly by the a
1dff0 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20  pplication.  In 
1e000 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a  that case, the.*
1e010 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  * error code and
1e020 20 6d 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20   message may or 
1e030 6d 61 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a  may not be set..
1e040 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
1e050 74 73 3a 0a 2a 2a 20 5b 48 31 32 38 30 31 5d 20  ts:.** [H12801] 
1e060 5b 48 31 32 38 30 32 5d 20 5b 48 31 32 38 30 33  [H12802] [H12803
1e070 5d 20 5b 48 31 32 38 30 37 5d 20 5b 48 31 32 38  ] [H12807] [H128
1e080 30 38 5d 20 5b 48 31 32 38 30 39 5d 0a 2a 2f 0a  08] [H12809].*/.
1e090 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1e0a0 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 73  qlite3_errcode(s
1e0b0 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c  qlite3 *db);.SQL
1e0c0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1e0d0 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72  te3_extended_err
1e0e0 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62  code(sqlite3 *db
1e0f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
1e100 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
1e110 33 5f 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33  3_errmsg(sqlite3
1e120 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  *);.SQLITE_API c
1e130 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
1e140 65 33 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69  e3_errmsg16(sqli
1e150 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  te3*);../*.** CA
1e160 50 49 33 52 45 46 3a 20 53 51 4c 20 53 74 61 74  PI3REF: SQL Stat
1e170 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31  ement Object {H1
1e180 33 30 30 30 7d 20 3c 48 31 33 30 31 30 3e 0a 2a  3000} <H13010>.*
1e190 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 65  * KEYWORDS: {pre
1e1a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 7d  pared statement}
1e1b0 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   {prepared state
1e1c0 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a 2a 20 41 6e 20  ments}.**.** An 
1e1d0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
1e1e0 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e   object represen
1e1f0 74 73 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20  ts a single SQL 
1e200 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54 68  statement..** Th
1e210 69 73 20 6f 62 6a 65 63 74 20 69 73 20 76 61 72  is object is var
1e220 69 6f 75 73 6c 79 20 6b 6e 6f 77 6e 20 61 73 20  iously known as 
1e230 61 20 22 70 72 65 70 61 72 65 64 20 73 74 61 74  a "prepared stat
1e240 65 6d 65 6e 74 22 20 6f 72 20 61 0a 2a 2a 20 22  ement" or a.** "
1e250 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61  compiled SQL sta
1e260 74 65 6d 65 6e 74 22 20 6f 72 20 73 69 6d 70 6c  tement" or simpl
1e270 79 20 61 73 20 61 20 22 73 74 61 74 65 6d 65 6e  y as a "statemen
1e280 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69  t"..**.** The li
1e290 66 65 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e  fe of a statemen
1e2a0 74 20 6f 62 6a 65 63 74 20 67 6f 65 73 20 73 6f  t object goes so
1e2b0 6d 65 74 68 69 6e 67 20 6c 69 6b 65 20 74 68 69  mething like thi
1e2c0 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a 2a 2a  s:.**.** <ol>.**
1e2d0 20 3c 6c 69 3e 20 43 72 65 61 74 65 20 74 68 65   <li> Create the
1e2e0 20 6f 62 6a 65 63 74 20 75 73 69 6e 67 20 5b 73   object using [s
1e2f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
1e300 32 28 29 5d 20 6f 72 20 61 20 72 65 6c 61 74 65  2()] or a relate
1e310 64 0a 2a 2a 20 20 20 20 20 20 66 75 6e 63 74 69  d.**      functi
1e320 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e 20 42 69 6e 64  on..** <li> Bind
1e330 20 76 61 6c 75 65 73 20 74 6f 20 5b 68 6f 73 74   values to [host
1e340 20 70 61 72 61 6d 65 74 65 72 73 5d 20 75 73 69   parameters] usi
1e350 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  ng the sqlite3_b
1e360 69 6e 64 5f 2a 28 29 0a 2a 2a 20 20 20 20 20 20  ind_*().**      
1e370 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 20 3c  interfaces..** <
1e380 6c 69 3e 20 52 75 6e 20 74 68 65 20 53 51 4c 20  li> Run the SQL 
1e390 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  by calling [sqli
1e3a0 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 6e 65 20  te3_step()] one 
1e3b0 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a  or more times..*
1e3c0 2a 20 3c 6c 69 3e 20 52 65 73 65 74 20 74 68 65  * <li> Reset the
1e3d0 20 73 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67   statement using
1e3e0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
1e3f0 29 5d 20 74 68 65 6e 20 67 6f 20 62 61 63 6b 0a  )] then go back.
1e400 2a 2a 20 20 20 20 20 20 74 6f 20 73 74 65 70 20  **      to step 
1e410 32 2e 20 20 44 6f 20 74 68 69 73 20 7a 65 72 6f  2.  Do this zero
1e420 20 6f 72 20 6d 6f 72 65 20 74 69 6d 65 73 2e 0a   or more times..
1e430 2a 2a 20 3c 6c 69 3e 20 44 65 73 74 72 6f 79 20  ** <li> Destroy 
1e440 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69 6e 67  the object using
1e450 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
1e460 7a 65 28 29 5d 2e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a  ze()]..** </ol>.
1e470 2a 2a 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 64  **.** Refer to d
1e480 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e 20  ocumentation on 
1e490 69 6e 64 69 76 69 64 75 61 6c 20 6d 65 74 68 6f  individual metho
1e4a0 64 73 20 61 62 6f 76 65 20 66 6f 72 20 61 64 64  ds above for add
1e4b0 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72  itional.** infor
1e4c0 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64  mation..*/.typed
1e4d0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
1e4e0 33 5f 73 74 6d 74 20 73 71 6c 69 74 65 33 5f 73  3_stmt sqlite3_s
1e4f0 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  tmt;../*.** CAPI
1e500 33 52 45 46 3a 20 52 75 6e 2d 74 69 6d 65 20 4c  3REF: Run-time L
1e510 69 6d 69 74 73 20 7b 48 31 32 37 36 30 7d 20 3c  imits {H12760} <
1e520 53 32 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20600>.**.** Th
1e530 69 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c  is interface all
1e540 6f 77 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ows the size of 
1e550 76 61 72 69 6f 75 73 20 63 6f 6e 73 74 72 75 63  various construc
1e560 74 73 20 74 6f 20 62 65 20 6c 69 6d 69 74 65 64  ts to be limited
1e570 0a 2a 2a 20 6f 6e 20 61 20 63 6f 6e 6e 65 63 74  .** on a connect
1e580 69 6f 6e 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f  ion by connectio
1e590 6e 20 62 61 73 69 73 2e 20 20 54 68 65 20 66 69  n basis.  The fi
1e5a0 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  rst parameter is
1e5b0 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73   the.** [databas
1e5c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 68  e connection] wh
1e5d0 6f 73 65 20 6c 69 6d 69 74 20 69 73 20 74 6f 20  ose limit is to 
1e5e0 62 65 20 73 65 74 20 6f 72 20 71 75 65 72 69 65  be set or querie
1e5f0 64 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e  d.  The.** secon
1e600 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6f  d parameter is o
1e610 6e 65 20 6f 66 20 74 68 65 20 5b 6c 69 6d 69 74  ne of the [limit
1e620 20 63 61 74 65 67 6f 72 69 65 73 5d 20 74 68 61   categories] tha
1e630 74 20 64 65 66 69 6e 65 20 61 0a 2a 2a 20 63 6c  t define a.** cl
1e640 61 73 73 20 6f 66 20 63 6f 6e 73 74 72 75 63 74  ass of construct
1e650 73 20 74 6f 20 62 65 20 73 69 7a 65 20 6c 69 6d  s to be size lim
1e660 69 74 65 64 2e 20 20 54 68 65 20 74 68 69 72 64  ited.  The third
1e670 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
1e680 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20 66  e.** new limit f
1e690 6f 72 20 74 68 61 74 20 63 6f 6e 73 74 72 75 63  or that construc
1e6a0 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e  t.  The function
1e6b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6f 6c 64   returns the old
1e6c0 20 6c 69 6d 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66   limit..**.** If
1e6d0 20 74 68 65 20 6e 65 77 20 6c 69 6d 69 74 20 69   the new limit i
1e6e0 73 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  s a negative num
1e6f0 62 65 72 2c 20 74 68 65 20 6c 69 6d 69 74 20 69  ber, the limit i
1e700 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 20  s unchanged..** 
1e710 46 6f 72 20 74 68 65 20 6c 69 6d 69 74 20 63 61  For the limit ca
1e720 74 65 67 6f 72 79 20 6f 66 20 53 51 4c 49 54 45  tegory of SQLITE
1e730 5f 4c 49 4d 49 54 5f 58 59 5a 20 74 68 65 72 65  _LIMIT_XYZ there
1e740 20 69 73 20 61 20 0a 2a 2a 20 5b 6c 69 6d 69 74   is a .** [limit
1e750 73 20 7c 20 68 61 72 64 20 75 70 70 65 72 20 62  s | hard upper b
1e760 6f 75 6e 64 5d 0a 2a 2a 20 73 65 74 20 62 79 20  ound].** set by 
1e770 61 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 43  a compile-time C
1e780 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61   preprocessor ma
1e790 63 72 6f 20 6e 61 6d 65 64 20 0a 2a 2a 20 5b 6c  cro named .** [l
1e7a0 69 6d 69 74 73 20 7c 20 53 51 4c 49 54 45 5f 4d  imits | SQLITE_M
1e7b0 41 58 5f 58 59 5a 5d 2e 0a 2a 2a 20 28 54 68 65  AX_XYZ]..** (The
1e7c0 20 22 5f 4c 49 4d 49 54 5f 22 20 69 6e 20 74 68   "_LIMIT_" in th
1e7d0 65 20 6e 61 6d 65 20 69 73 20 63 68 61 6e 67 65  e name is change
1e7e0 64 20 74 6f 20 22 5f 4d 41 58 5f 22 2e 29 0a 2a  d to "_MAX_".).*
1e7f0 2a 20 41 74 74 65 6d 70 74 73 20 74 6f 20 69 6e  * Attempts to in
1e800 63 72 65 61 73 65 20 61 20 6c 69 6d 69 74 20 61  crease a limit a
1e810 62 6f 76 65 20 69 74 73 20 68 61 72 64 20 75 70  bove its hard up
1e820 70 65 72 20 62 6f 75 6e 64 20 61 72 65 0a 2a 2a  per bound are.**
1e830 20 73 69 6c 65 6e 74 6c 79 20 74 72 75 6e 63 61   silently trunca
1e840 74 65 64 20 74 6f 20 74 68 65 20 68 61 72 64 20  ted to the hard 
1e850 75 70 70 65 72 20 6c 69 6d 69 74 2e 0a 2a 2a 0a  upper limit..**.
1e860 2a 2a 20 52 75 6e 20 74 69 6d 65 20 6c 69 6d 69  ** Run time limi
1e870 74 73 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20  ts are intended 
1e880 66 6f 72 20 75 73 65 20 69 6e 20 61 70 70 6c 69  for use in appli
1e890 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6e  cations that man
1e8a0 61 67 65 0a 2a 2a 20 62 6f 74 68 20 74 68 65 69  age.** both thei
1e8b0 72 20 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 64  r own internal d
1e8c0 61 74 61 62 61 73 65 20 61 6e 64 20 61 6c 73 6f  atabase and also
1e8d0 20 64 61 74 61 62 61 73 65 73 20 74 68 61 74 20   databases that 
1e8e0 61 72 65 20 63 6f 6e 74 72 6f 6c 6c 65 64 0a 2a  are controlled.*
1e8f0 2a 20 62 79 20 75 6e 74 72 75 73 74 65 64 20 65  * by untrusted e
1e900 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 73 2e  xternal sources.
1e910 20 20 41 6e 20 65 78 61 6d 70 6c 65 20 61 70 70    An example app
1e920 6c 69 63 61 74 69 6f 6e 20 6d 69 67 68 74 20 62  lication might b
1e930 65 20 61 0a 2a 2a 20 77 65 62 20 62 72 6f 77 73  e a.** web brows
1e940 65 72 20 74 68 61 74 20 68 61 73 20 69 74 73 20  er that has its 
1e950 6f 77 6e 20 64 61 74 61 62 61 73 65 73 20 66 6f  own databases fo
1e960 72 20 73 74 6f 72 69 6e 67 20 68 69 73 74 6f 72  r storing histor
1e970 79 20 61 6e 64 0a 2a 2a 20 73 65 70 61 72 61 74  y and.** separat
1e980 65 20 64 61 74 61 62 61 73 65 73 20 63 6f 6e 74  e databases cont
1e990 72 6f 6c 6c 65 64 20 62 79 20 4a 61 76 61 53 63  rolled by JavaSc
1e9a0 72 69 70 74 20 61 70 70 6c 69 63 61 74 69 6f 6e  ript application
1e9b0 73 20 64 6f 77 6e 6c 6f 61 64 65 64 0a 2a 2a 20  s downloaded.** 
1e9c0 6f 66 66 20 74 68 65 20 49 6e 74 65 72 6e 65 74  off the Internet
1e9d0 2e 20 20 54 68 65 20 69 6e 74 65 72 6e 61 6c 20  .  The internal 
1e9e0 64 61 74 61 62 61 73 65 73 20 63 61 6e 20 62 65  databases can be
1e9f0 20 67 69 76 65 6e 20 74 68 65 0a 2a 2a 20 6c 61   given the.** la
1ea00 72 67 65 2c 20 64 65 66 61 75 6c 74 20 6c 69 6d  rge, default lim
1ea10 69 74 73 2e 20 20 44 61 74 61 62 61 73 65 73 20  its.  Databases 
1ea20 6d 61 6e 61 67 65 64 20 62 79 20 65 78 74 65 72  managed by exter
1ea30 6e 61 6c 20 73 6f 75 72 63 65 73 20 63 61 6e 0a  nal sources can.
1ea40 2a 2a 20 62 65 20 67 69 76 65 6e 20 6d 75 63 68  ** be given much
1ea50 20 73 6d 61 6c 6c 65 72 20 6c 69 6d 69 74 73 20   smaller limits 
1ea60 64 65 73 69 67 6e 65 64 20 74 6f 20 70 72 65 76  designed to prev
1ea70 65 6e 74 20 61 20 64 65 6e 69 61 6c 20 6f 66 20  ent a denial of 
1ea80 73 65 72 76 69 63 65 0a 2a 2a 20 61 74 74 61 63  service.** attac
1ea90 6b 2e 20 20 44 65 76 65 6c 6f 70 65 72 73 20 6d  k.  Developers m
1eaa0 69 67 68 74 20 61 6c 73 6f 20 77 61 6e 74 20 74  ight also want t
1eab0 6f 20 75 73 65 20 74 68 65 20 5b 73 71 6c 69 74  o use the [sqlit
1eac0 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
1ead0 72 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63  r()].** interfac
1eae0 65 20 74 6f 20 66 75 72 74 68 65 72 20 63 6f 6e  e to further con
1eaf0 74 72 6f 6c 20 75 6e 74 72 75 73 74 65 64 20 53  trol untrusted S
1eb00 51 4c 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66  QL.  The size of
1eb10 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
1eb20 20 63 72 65 61 74 65 64 20 62 79 20 61 6e 20 75   created by an u
1eb30 6e 74 72 75 73 74 65 64 20 73 63 72 69 70 74 20  ntrusted script 
1eb40 63 61 6e 20 62 65 20 63 6f 6e 74 61 69 6e 65 64  can be contained
1eb50 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 6d   using the.** [m
1eb60 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 5d 20 5b  ax_page_count] [
1eb70 50 52 41 47 4d 41 5d 2e 0a 2a 2a 0a 2a 2a 20 4e  PRAGMA]..**.** N
1eb80 65 77 20 72 75 6e 2d 74 69 6d 65 20 6c 69 6d 69  ew run-time limi
1eb90 74 20 63 61 74 65 67 6f 72 69 65 73 20 6d 61 79  t categories may
1eba0 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
1ebb0 75 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a  ure releases..**
1ebc0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
1ebd0 3a 0a 2a 2a 20 5b 48 31 32 37 36 32 5d 20 5b 48  :.** [H12762] [H
1ebe0 31 32 37 36 36 5d 20 5b 48 31 32 37 36 39 5d 0a  12766] [H12769].
1ebf0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1ec00 74 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28  t sqlite3_limit(
1ec10 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64  sqlite3*, int id
1ec20 2c 20 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a  , int newVal);..
1ec30 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1ec40 52 75 6e 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43  Run-Time Limit C
1ec50 61 74 65 67 6f 72 69 65 73 20 7b 48 31 32 37 39  ategories {H1279
1ec60 30 7d 20 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b  0} <H12760>.** K
1ec70 45 59 57 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20  EYWORDS: {limit 
1ec80 63 61 74 65 67 6f 72 79 7d 20 7b 2a 6c 69 6d 69  category} {*limi
1ec90 74 20 63 61 74 65 67 6f 72 69 65 73 7d 0a 2a 2a  t categories}.**
1eca0 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61  .** These consta
1ecb0 6e 74 73 20 64 65 66 69 6e 65 20 76 61 72 69 6f  nts define vario
1ecc0 75 73 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 6c  us performance l
1ecd0 69 6d 69 74 73 0a 2a 2a 20 74 68 61 74 20 63 61  imits.** that ca
1ece0 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 61 74 20  n be lowered at 
1ecf0 72 75 6e 2d 74 69 6d 65 20 75 73 69 6e 67 20 5b  run-time using [
1ed00 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d  sqlite3_limit()]
1ed10 2e 0a 2a 2a 20 54 68 65 20 73 79 6e 6f 70 73 69  ..** The synopsi
1ed20 73 20 6f 66 20 74 68 65 20 6d 65 61 6e 69 6e 67  s of the meaning
1ed30 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73  s of the various
1ed40 20 6c 69 6d 69 74 73 20 69 73 20 73 68 6f 77 6e   limits is shown
1ed50 20 62 65 6c 6f 77 2e 0a 2a 2a 20 41 64 64 69 74   below..** Addit
1ed60 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
1ed70 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 61  n is available a
1ed80 74 20 5b 6c 69 6d 69 74 73 20 7c 20 4c 69 6d 69  t [limits | Limi
1ed90 74 73 20 69 6e 20 53 51 4c 69 74 65 5d 2e 0a 2a  ts in SQLite]..*
1eda0 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74  *.** <dl>.** <dt
1edb0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45  >SQLITE_LIMIT_LE
1edc0 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  NGTH</dt>.** <dd
1edd0 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a  >The maximum siz
1ede0 65 20 6f 66 20 61 6e 79 20 73 74 72 69 6e 67 20  e of any string 
1edf0 6f 72 20 42 4c 4f 42 20 6f 72 20 74 61 62 6c 65  or BLOB or table
1ee00 20 72 6f 77 2e 3c 64 64 3e 0a 2a 2a 0a 2a 2a 20   row.<dd>.**.** 
1ee10 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1ee20 5f 53 51 4c 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e  _SQL_LENGTH</dt>
1ee30 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1ee40 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e  mum length of an
1ee50 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 3c   SQL statement.<
1ee60 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
1ee70 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55  QLITE_LIMIT_COLU
1ee80 4d 4e 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  MN</dt>.** <dd>T
1ee90 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1eea0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1eeb0 61 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69  a table definiti
1eec0 6f 6e 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20  on or in the.** 
1eed0 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20  result set of a 
1eee0 5b 53 45 4c 45 43 54 5d 20 6f 72 20 74 68 65 20  [SELECT] or the 
1eef0 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
1ef00 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 20  f columns in an 
1ef10 69 6e 64 65 78 0a 2a 2a 20 6f 72 20 69 6e 20 61  index.** or in a
1ef20 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52  n ORDER BY or GR
1ef30 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e 3c 2f  OUP BY clause.</
1ef40 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
1ef50 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f  LITE_LIMIT_EXPR_
1ef60 44 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  DEPTH</dt>.** <d
1ef70 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65  d>The maximum de
1ef80 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 73 65  pth of the parse
1ef90 20 74 72 65 65 20 6f 6e 20 61 6e 79 20 65 78 70   tree on any exp
1efa0 72 65 73 73 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a  ression.</dd>.**
1efb0 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
1efc0 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  IMIT_COMPOUND_SE
1efd0 4c 45 43 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  LECT</dt>.** <dd
1efe0 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d  >The maximum num
1eff0 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20  ber of terms in 
1f000 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43  a compound SELEC
1f010 54 20 73 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64  T statement.</dd
1f020 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
1f030 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f 4f 50  TE_LIMIT_VDBE_OP
1f040 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
1f050 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1f060 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  of instructions 
1f070 69 6e 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63  in a virtual mac
1f080 68 69 6e 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20  hine program.** 
1f090 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
1f0a0 74 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  t an SQL stateme
1f0b0 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  nt.</dd>.**.** <
1f0c0 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
1f0d0 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c 2f 64 74  FUNCTION_ARG</dt
1f0e0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
1f0f0 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61  imum number of a
1f100 72 67 75 6d 65 6e 74 73 20 6f 6e 20 61 20 66 75  rguments on a fu
1f110 6e 63 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a  nction.</dd>.**.
1f120 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
1f130 4d 49 54 5f 41 54 54 41 43 48 45 44 3c 2f 64 74  MIT_ATTACHED</dt
1f140 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
1f150 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 5b  imum number of [
1f160 41 54 54 41 43 48 20 7c 20 61 74 74 61 63 68 65  ATTACH | attache
1f170 64 20 64 61 74 61 62 61 73 65 73 5d 2e 3c 2f 64  d databases].</d
1f180 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
1f190 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50  ITE_LIMIT_LIKE_P
1f1a0 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 3c 2f 64  ATTERN_LENGTH</d
1f1b0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
1f1c0 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
1f1d0 74 68 65 20 70 61 74 74 65 72 6e 20 61 72 67 75  the pattern argu
1f1e0 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 4c 49 4b  ment to the [LIK
1f1f0 45 5d 20 6f 72 0a 2a 2a 20 5b 47 4c 4f 42 5d 20  E] or.** [GLOB] 
1f200 6f 70 65 72 61 74 6f 72 73 2e 3c 2f 64 64 3e 0a  operators.</dd>.
1f210 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
1f220 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f  _LIMIT_VARIABLE_
1f230 4e 55 4d 42 45 52 3c 2f 64 74 3e 0a 2a 2a 20 3c  NUMBER</dt>.** <
1f240 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  dd>The maximum n
1f250 75 6d 62 65 72 20 6f 66 20 76 61 72 69 61 62 6c  umber of variabl
1f260 65 73 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61  es in an SQL sta
1f270 74 65 6d 65 6e 74 20 74 68 61 74 20 63 61 6e 0a  tement that can.
1f280 2a 2a 20 62 65 20 62 6f 75 6e 64 2e 3c 2f 64 64  ** be bound.</dd
1f290 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
1f2a0 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45 52  TE_LIMIT_TRIGGER
1f2b0 5f 44 45 50 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c  _DEPTH</dt>.** <
1f2c0 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 64  dd>The maximum d
1f2d0 65 70 74 68 20 6f 66 20 72 65 63 75 72 73 69 6f  epth of recursio
1f2e0 6e 20 66 6f 72 20 74 72 69 67 67 65 72 73 2e 3c  n for triggers.<
1f2f0 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f  /dd>.** </dl>.*/
1f300 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1f310 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 20 20 20 20  LIMIT_LENGTH    
1f320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f330 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  0.#define SQLITE
1f340 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54  _LIMIT_SQL_LENGT
1f350 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
1f360 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   1.#define SQLIT
1f370 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 20 20  E_LIMIT_COLUMN  
1f380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f390 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    2.#define SQLI
1f3a0 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45  TE_LIMIT_EXPR_DE
1f3b0 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20  PTH             
1f3c0 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c     3.#define SQL
1f3d0 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55  ITE_LIMIT_COMPOU
1f3e0 4e 44 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20  ND_SELECT       
1f3f0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51      4.#define SQ
1f400 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 44 42 45 5f  LITE_LIMIT_VDBE_
1f410 4f 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OP              
1f420 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53       5.#define S
1f430 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43  QLITE_LIMIT_FUNC
1f440 54 49 4f 4e 5f 41 52 47 20 20 20 20 20 20 20 20  TION_ARG        
1f450 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20        6.#define 
1f460 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54  SQLITE_LIMIT_ATT
1f470 41 43 48 45 44 20 20 20 20 20 20 20 20 20 20 20  ACHED           
1f480 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e 65         7.#define
1f490 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49   SQLITE_LIMIT_LI
1f4a0 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
1f4b0 48 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e  H       8.#defin
1f4c0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  e SQLITE_LIMIT_V
1f4d0 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 20 20  ARIABLE_NUMBER  
1f4e0 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69           9.#defi
1f4f0 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
1f500 54 52 49 47 47 45 52 5f 44 45 50 54 48 20 20 20  TRIGGER_DEPTH   
1f510 20 20 20 20 20 20 20 20 20 31 30 0a 0a 2f 2a 0a           10../*.
1f520 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d  ** CAPI3REF: Com
1f530 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c 20 53 74  piling An SQL St
1f540 61 74 65 6d 65 6e 74 20 7b 48 31 33 30 31 30 7d  atement {H13010}
1f550 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20 4b 45 59   <S10000>.** KEY
1f560 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73 74 61 74  WORDS: {SQL stat
1f570 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65 72 7d 0a  ement compiler}.
1f580 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63 75 74 65  **.** To execute
1f590 20 61 6e 20 53 51 4c 20 71 75 65 72 79 2c 20 69   an SQL query, i
1f5a0 74 20 6d 75 73 74 20 66 69 72 73 74 20 62 65 20  t must first be 
1f5b0 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f 20 61 20  compiled into a 
1f5c0 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20 70 72 6f  byte-code.** pro
1f5d0 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e 65 20 6f  gram using one o
1f5e0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
1f5f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
1f600 74 20 61 72 67 75 6d 65 6e 74 2c 20 22 64 62 22  t argument, "db"
1f610 2c 20 69 73 20 61 20 5b 64 61 74 61 62 61 73 65  , is a [database
1f620 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 74   connection] obt
1f630 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20  ained from a.** 
1f640 70 72 69 6f 72 20 73 75 63 63 65 73 73 66 75 6c  prior successful
1f650 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
1f660 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69  3_open()], [sqli
1f670 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 6f  te3_open_v2()] o
1f680 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70  r.** [sqlite3_op
1f690 65 6e 31 36 28 29 5d 2e 20 20 54 68 65 20 64 61  en16()].  The da
1f6a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1f6b0 6e 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20  n must not have 
1f6c0 62 65 65 6e 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a  been closed..**.
1f6d0 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  ** The second ar
1f6e0 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c 20  gument, "zSql", 
1f6f0 69 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  is the statement
1f700 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64 2c   to be compiled,
1f710 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20 65   encoded.** as e
1f720 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20 55  ither UTF-8 or U
1f730 54 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c 69  TF-16.  The sqli
1f740 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 61 6e  te3_prepare() an
1f750 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  d sqlite3_prepar
1f760 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72 66  e_v2().** interf
1f770 61 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c 20  aces use UTF-8, 
1f780 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70  and sqlite3_prep
1f790 61 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c 69  are16() and sqli
1f7a0 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
1f7b0 28 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31 36  ().** use UTF-16
1f7c0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ..**.** If the n
1f7d0 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69 73  Byte argument is
1f7e0 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c   less than zero,
1f7f0 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72 65   then zSql is re
1f800 61 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a 20  ad up to the.** 
1f810 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69  first zero termi
1f820 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65 20  nator. If nByte 
1f830 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c  is non-negative,
1f840 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20   then it is the 
1f850 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62 65  maximum.** numbe
1f860 72 20 6f 66 20 20 62 79 74 65 73 20 72 65 61 64  r of  bytes read
1f870 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68 65   from zSql.  Whe
1f880 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e  n nByte is non-n
1f890 65 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a 20  egative, the.** 
1f8a0 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64 73  zSql string ends
1f8b0 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20 66   at either the f
1f8c0 69 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20 27  irst '\000' or '
1f8d0 5c 75 30 30 30 30 27 20 63 68 61 72 61 63 74 65  \u0000' characte
1f8e0 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79 74  r or.** the nByt
1f8f0 65 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63 68  e-th byte, which
1f900 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74  ever comes first
1f910 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  . If the caller 
1f920 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74 68  knows.** that th
1f930 65 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 6e  e supplied strin
1f940 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  g is nul-termina
1f950 74 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20  ted, then there 
1f960 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70 65  is a small.** pe
1f970 72 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e 74  rformance advant
1f980 61 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65 64  age to be gained
1f990 20 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20 6e   by passing an n
1f9a0 42 79 74 65 20 70 61 72 61 6d 65 74 65 72 20 74  Byte parameter t
1f9b0 68 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20  hat.** is equal 
1f9c0 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
1f9d0 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 69 6e   bytes in the in
1f9e0 70 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69 6e  put string <i>in
1f9f0 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20 74  cluding</i>.** t
1fa00 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  he nul-terminato
1fa10 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  r bytes..**.** I
1fa20 66 20 70 7a 54 61 69 6c 20 69 73 20 6e 6f 74 20  f pzTail is not 
1fa30 4e 55 4c 4c 20 74 68 65 6e 20 2a 70 7a 54 61 69  NULL then *pzTai
1fa40 6c 20 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69  l is made to poi
1fa50 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  nt to the first 
1fa60 62 79 74 65 0a 2a 2a 20 70 61 73 74 20 74 68 65  byte.** past the
1fa70 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 72 73   end of the firs
1fa80 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  t SQL statement 
1fa90 69 6e 20 7a 53 71 6c 2e 20 20 54 68 65 73 65 20  in zSql.  These 
1faa0 72 6f 75 74 69 6e 65 73 20 6f 6e 6c 79 0a 2a 2a  routines only.**
1fab0 20 63 6f 6d 70 69 6c 65 20 74 68 65 20 66 69 72   compile the fir
1fac0 73 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  st statement in 
1fad0 7a 53 71 6c 2c 20 73 6f 20 2a 70 7a 54 61 69 6c  zSql, so *pzTail
1fae0 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
1faf0 67 20 74 6f 0a 2a 2a 20 77 68 61 74 20 72 65 6d  g to.** what rem
1fb00 61 69 6e 73 20 75 6e 63 6f 6d 70 69 6c 65 64 2e  ains uncompiled.
1fb10 0a 2a 2a 0a 2a 2a 20 2a 70 70 53 74 6d 74 20 69  .**.** *ppStmt i
1fb20 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
1fb30 74 6f 20 61 20 63 6f 6d 70 69 6c 65 64 20 5b 70  to a compiled [p
1fb40 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1fb50 74 5d 20 74 68 61 74 20 63 61 6e 20 62 65 0a 2a  t] that can be.*
1fb60 2a 20 65 78 65 63 75 74 65 64 20 75 73 69 6e 67  * executed using
1fb70 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1fb80 5d 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20  ].  If there is 
1fb90 61 6e 20 65 72 72 6f 72 2c 20 2a 70 70 53 74 6d  an error, *ppStm
1fba0 74 20 69 73 20 73 65 74 0a 2a 2a 20 74 6f 20 4e  t is set.** to N
1fbb0 55 4c 4c 2e 20 20 49 66 20 74 68 65 20 69 6e 70  ULL.  If the inp
1fbc0 75 74 20 74 65 78 74 20 63 6f 6e 74 61 69 6e 73  ut text contains
1fbd0 20 6e 6f 20 53 51 4c 20 28 69 66 20 74 68 65 20   no SQL (if the 
1fbe0 69 6e 70 75 74 20 69 73 20 61 6e 20 65 6d 70 74  input is an empt
1fbf0 79 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 72 20 61  y.** string or a
1fc00 20 63 6f 6d 6d 65 6e 74 29 20 74 68 65 6e 20 2a   comment) then *
1fc10 70 70 53 74 6d 74 20 69 73 20 73 65 74 20 74 6f  ppStmt is set to
1fc20 20 4e 55 4c 4c 2e 0a 2a 2a 20 54 68 65 20 63 61   NULL..** The ca
1fc30 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20  lling procedure 
1fc40 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
1fc50 6f 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20  or deleting the 
1fc60 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20  compiled.** SQL 
1fc70 73 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20  statement using 
1fc80 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
1fc90 65 28 29 5d 20 61 66 74 65 72 20 69 74 20 68 61  e()] after it ha
1fca0 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20  s finished with 
1fcb0 69 74 2e 0a 2a 2a 20 70 70 53 74 6d 74 20 6d 61  it..** ppStmt ma
1fcc0 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 2a  y not be NULL..*
1fcd0 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c  *.** On success,
1fce0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 73 20   [SQLITE_OK] is 
1fcf0 72 65 74 75 72 6e 65 64 2c 20 6f 74 68 65 72 77  returned, otherw
1fd00 69 73 65 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f  ise an [error co
1fd10 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e  de] is returned.
1fd20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
1fd30 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 20  e3_prepare_v2() 
1fd40 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70  and sqlite3_prep
1fd50 61 72 65 31 36 5f 76 32 28 29 20 69 6e 74 65 72  are16_v2() inter
1fd60 66 61 63 65 73 20 61 72 65 0a 2a 2a 20 72 65 63  faces are.** rec
1fd70 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 61 6c 6c  ommended for all
1fd80 20 6e 65 77 20 70 72 6f 67 72 61 6d 73 2e 20 54   new programs. T
1fd90 68 65 20 74 77 6f 20 6f 6c 64 65 72 20 69 6e 74  he two older int
1fda0 65 72 66 61 63 65 73 20 61 72 65 20 72 65 74 61  erfaces are reta
1fdb0 69 6e 65 64 0a 2a 2a 20 66 6f 72 20 62 61 63 6b  ined.** for back
1fdc0 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
1fdd0 69 74 79 2c 20 62 75 74 20 74 68 65 69 72 20 75  ity, but their u
1fde0 73 65 20 69 73 20 64 69 73 63 6f 75 72 61 67 65  se is discourage
1fdf0 64 2e 0a 2a 2a 20 49 6e 20 74 68 65 20 22 76 32  d..** In the "v2
1fe00 22 20 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68  " interfaces, th
1fe10 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
1fe20 6d 65 6e 74 0a 2a 2a 20 74 68 61 74 20 69 73 20  ment.** that is 
1fe30 72 65 74 75 72 6e 65 64 20 28 74 68 65 20 5b 73  returned (the [s
1fe40 71 6c 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a  qlite3_stmt] obj
1fe50 65 63 74 29 20 63 6f 6e 74 61 69 6e 73 20 61 20  ect) contains a 
1fe60 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 6f  copy of the.** o
1fe70 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74  riginal SQL text
1fe80 2e 20 54 68 69 73 20 63 61 75 73 65 73 20 74 68  . This causes th
1fe90 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  e [sqlite3_step(
1fea0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 74 6f 0a  )] interface to.
1feb0 2a 2a 20 62 65 68 61 76 65 20 64 69 66 66 65 72  ** behave differ
1fec0 65 6e 74 6c 79 20 69 6e 20 74 68 72 65 65 20 77  ently in three w
1fed0 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f 6c 3e 0a  ays:.**.** <ol>.
1fee0 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49 66 20 74 68  ** <li>.** If th
1fef0 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
1ff00 61 20 63 68 61 6e 67 65 73 2c 20 69 6e 73 74 65  a changes, inste
1ff10 61 64 20 6f 66 20 72 65 74 75 72 6e 69 6e 67 20  ad of returning 
1ff20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20  [SQLITE_SCHEMA] 
1ff30 61 73 20 69 74 0a 2a 2a 20 61 6c 77 61 79 73 20  as it.** always 
1ff40 75 73 65 64 20 74 6f 20 64 6f 2c 20 5b 73 71 6c  used to do, [sql
1ff50 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c  ite3_step()] wil
1ff60 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  l automatically 
1ff70 72 65 63 6f 6d 70 69 6c 65 20 74 68 65 20 53 51  recompile the SQ
1ff80 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 61  L.** statement a
1ff90 6e 64 20 74 72 79 20 74 6f 20 72 75 6e 20 69 74  nd try to run it
1ffa0 20 61 67 61 69 6e 2e 20 20 49 66 20 74 68 65 20   again.  If the 
1ffb0 73 63 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67  schema has chang
1ffc0 65 64 20 69 6e 0a 2a 2a 20 61 20 77 61 79 20 74  ed in.** a way t
1ffd0 68 61 74 20 6d 61 6b 65 73 20 74 68 65 20 73 74  hat makes the st
1ffe0 61 74 65 6d 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65  atement no longe
1fff0 72 20 76 61 6c 69 64 2c 20 5b 73 71 6c 69 74 65  r valid, [sqlite
20000 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 73  3_step()] will s
20010 74 69 6c 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 5b  till.** return [
20020 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 2e 20  SQLITE_SCHEMA]. 
20030 20 42 75 74 20 75 6e 6c 69 6b 65 20 74 68 65 20   But unlike the 
20040 6c 65 67 61 63 79 20 62 65 68 61 76 69 6f 72 2c  legacy behavior,
20050 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d   [SQLITE_SCHEMA]
20060 20 69 73 0a 2a 2a 20 6e 6f 77 20 61 20 66 61 74   is.** now a fat
20070 61 6c 20 65 72 72 6f 72 2e 20 20 43 61 6c 6c 69  al error.  Calli
20080 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ng [sqlite3_prep
20090 61 72 65 5f 76 32 28 29 5d 20 61 67 61 69 6e 20  are_v2()] again 
200a0 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b 65 20 74 68  will not make th
200b0 65 0a 2a 2a 20 65 72 72 6f 72 20 67 6f 20 61 77  e.** error go aw
200c0 61 79 2e 20 20 4e 6f 74 65 3a 20 75 73 65 20 5b  ay.  Note: use [
200d0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
200e0 5d 20 74 6f 20 66 69 6e 64 20 74 68 65 20 74 65  ] to find the te
200f0 78 74 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 72  xt.** of the par
20100 73 69 6e 67 20 65 72 72 6f 72 20 74 68 61 74 20  sing error that 
20110 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 5b 53  results in an [S
20120 51 4c 49 54 45 5f 53 43 48 45 4d 41 5d 20 72 65  QLITE_SCHEMA] re
20130 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a  turn..** </li>.*
20140 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 57 68 65  *.** <li>.** Whe
20150 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  n an error occur
20160 73 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  s, [sqlite3_step
20170 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ()] will return 
20180 6f 6e 65 20 6f 66 20 74 68 65 20 64 65 74 61 69  one of the detai
20190 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f 72 20 63 6f  led.** [error co
201a0 64 65 73 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65  des] or [extende
201b0 64 20 65 72 72 6f 72 20 63 6f 64 65 73 5d 2e 20  d error codes]. 
201c0 20 54 68 65 20 6c 65 67 61 63 79 20 62 65 68 61   The legacy beha
201d0 76 69 6f 72 20 77 61 73 20 74 68 61 74 0a 2a 2a  vior was that.**
201e0 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
201f0 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 72 65 74  ] would only ret
20200 75 72 6e 20 61 20 67 65 6e 65 72 69 63 20 5b 53  urn a generic [S
20210 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 72 65 73  QLITE_ERROR] res
20220 75 6c 74 20 63 6f 64 65 0a 2a 2a 20 61 6e 64 20  ult code.** and 
20230 79 6f 75 20 77 6f 75 6c 64 20 68 61 76 65 20 74  you would have t
20240 6f 20 6d 61 6b 65 20 61 20 73 65 63 6f 6e 64 20  o make a second 
20250 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
20260 5f 72 65 73 65 74 28 29 5d 20 69 6e 20 6f 72 64  _reset()] in ord
20270 65 72 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68  er.** to find th
20280 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 63 61 75  e underlying cau
20290 73 65 20 6f 66 20 74 68 65 20 70 72 6f 62 6c 65  se of the proble
202a0 6d 2e 20 57 69 74 68 20 74 68 65 20 22 76 32 22  m. With the "v2"
202b0 20 70 72 65 70 61 72 65 0a 2a 2a 20 69 6e 74 65   prepare.** inte
202c0 72 66 61 63 65 73 2c 20 74 68 65 20 75 6e 64 65  rfaces, the unde
202d0 72 6c 79 69 6e 67 20 72 65 61 73 6f 6e 20 66 6f  rlying reason fo
202e0 72 20 74 68 65 20 65 72 72 6f 72 20 69 73 20 72  r the error is r
202f0 65 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74  eturned immediat
20300 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a  ely..** </li>.**
20310 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 5e 49 66 20  .** <li>.** ^If 
20320 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 5b  the value of a [
20330 70 61 72 61 6d 65 74 65 72 20 7c 20 68 6f 73 74  parameter | host
20340 20 70 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 74   parameter] in t
20350 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
20360 6d 69 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65 20  might.** change 
20370 74 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20 66  the query plan f
20380 6f 72 20 61 20 73 74 61 74 65 6d 65 6e 74 2c 20  or a statement, 
20390 74 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65  then the stateme
203a0 6e 74 20 6d 61 79 20 62 65 0a 2a 2a 20 61 75 74  nt may be.** aut
203b0 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d  omatically recom
203c0 70 69 6c 65 64 20 28 61 73 20 69 66 20 74 68 65  piled (as if the
203d0 72 65 20 68 61 64 20 62 65 65 6e 20 61 20 73 63  re had been a sc
203e0 68 65 6d 61 20 63 68 61 6e 67 65 29 20 6f 6e 20  hema change) on 
203f0 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20 5b 73  the first .** [s
20400 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63  qlite3_step()] c
20410 61 6c 6c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e  all following an
20420 79 20 63 68 61 6e 67 65 20 74 6f 20 74 68 65 20  y change to the 
20430 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
20440 64 5f 74 65 78 74 20 7c 20 62 69 6e 64 69 6e 67  d_text | binding
20450 73 5d 20 6f 66 20 74 68 65 20 5b 70 61 72 61 6d  s] of the [param
20460 65 74 65 72 5d 2e 20 0a 2a 2a 20 3c 2f 6c 69 3e  eter]. .** </li>
20470 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ol>.**.** 
20480 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
20490 20 5b 48 31 33 30 31 31 5d 20 5b 48 31 33 30 31   [H13011] [H1301
204a0 32 5d 20 5b 48 31 33 30 31 33 5d 20 5b 48 31 33  2] [H13013] [H13
204b0 30 31 34 5d 20 5b 48 31 33 30 31 35 5d 20 5b 48  014] [H13015] [H
204c0 31 33 30 31 36 5d 20 5b 48 31 33 30 31 39 5d 20  13016] [H13019] 
204d0 5b 48 31 33 30 32 31 5d 0a 2a 2a 0a 2a 2f 0a 53  [H13021].**.*/.S
204e0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
204f0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 0a 20  lite3_prepare(. 
20500 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
20510 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
20520 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
20530 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
20540 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20  l,       /* SQL 
20550 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d 38  statement, UTF-8
20560 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e   encoded */.  in
20570 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20  t nByte,        
20580 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
20590 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   length of zSql 
205a0 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73  in bytes. */.  s
205b0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
205c0 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53  Stmt,  /* OUT: S
205d0 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20  tatement handle 
205e0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
205f0 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20  **pzTail     /* 
20600 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20  OUT: Pointer to 
20610 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f  unused portion o
20620 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51 4c  f zSql */.);.SQL
20630 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
20640 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0a  te3_prepare_v2(.
20650 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
20660 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
20670 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
20680 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
20690 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ql,       /* SQL
206a0 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d   statement, UTF-
206b0 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69  8 encoded */.  i
206c0 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20  nt nByte,       
206d0 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
206e0 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  m length of zSql
206f0 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
20700 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70  sqlite3_stmt **p
20710 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20  pStmt,  /* OUT: 
20720 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65  Statement handle
20730 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
20740 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a   **pzTail     /*
20750 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f   OUT: Pointer to
20760 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e 20   unused portion 
20770 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53 51  of zSql */.);.SQ
20780 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
20790 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 0a  ite3_prepare16(.
207a0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
207b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
207c0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
207d0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53    const void *zS
207e0 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ql,       /* SQL
207f0 20 73 74 61 74 65 6d 65 6e 74 2c 20 55 54 46 2d   statement, UTF-
20800 31 36 20 65 6e 63 6f 64 65 64 20 2a 2f 0a 20 20  16 encoded */.  
20810 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20  int nByte,      
20820 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
20830 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a 53 71  um length of zSq
20840 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
20850 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
20860 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a  ppStmt,  /* OUT:
20870 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c   Statement handl
20880 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  e */.  const voi
20890 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 2f  d **pzTail     /
208a0 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74  * OUT: Pointer t
208b0 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69 6f 6e  o unused portion
208c0 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 53   of zSql */.);.S
208d0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
208e0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
208f0 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v2(.  sqlite3 *d
20900 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
20910 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
20920 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
20930 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a   *zSql,       /*
20940 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
20950 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a  UTF-16 encoded *
20960 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20  /.  int nByte,  
20970 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
20980 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
20990 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
209a0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
209b0 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20  t **ppStmt,  /* 
209c0 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68  OUT: Statement h
209d0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
209e0 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20   void **pzTail  
209f0 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74     /* OUT: Point
20a00 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72  er to unused por
20a10 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a  tion of zSql */.
20a20 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
20a30 45 46 3a 20 52 65 74 72 69 65 76 69 6e 67 20 53  EF: Retrieving S
20a40 74 61 74 65 6d 65 6e 74 20 53 51 4c 20 7b 48 31  tatement SQL {H1
20a50 33 31 30 30 7d 20 3c 48 31 33 30 30 30 3e 0a 2a  3100} <H13000>.*
20a60 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
20a70 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64 20  ace can be used 
20a80 74 6f 20 72 65 74 72 69 65 76 65 20 61 20 73 61  to retrieve a sa
20a90 76 65 64 20 63 6f 70 79 20 6f 66 20 74 68 65 20  ved copy of the 
20aa0 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 53 51 4c 20  original.** SQL 
20ab0 74 65 78 74 20 75 73 65 64 20 74 6f 20 63 72 65  text used to cre
20ac0 61 74 65 20 61 20 5b 70 72 65 70 61 72 65 64 20  ate a [prepared 
20ad0 73 74 61 74 65 6d 65 6e 74 5d 20 69 66 20 74 68  statement] if th
20ae0 61 74 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73  at statement was
20af0 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 75 73 69  .** compiled usi
20b00 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74  ng either [sqlit
20b10 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
20b20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   or [sqlite3_pre
20b30 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a  pare16_v2()]..**
20b40 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
20b50 3a 0a 2a 2a 20 5b 48 31 33 31 30 31 5d 20 5b 48  :.** [H13101] [H
20b60 31 33 31 30 32 5d 20 5b 48 31 33 31 30 33 5d 0a  13102] [H13103].
20b70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
20b80 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
20b90 33 5f 73 71 6c 28 73 71 6c 69 74 65 33 5f 73 74  3_sql(sqlite3_st
20ba0 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a  mt *pStmt);../*.
20bb0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 79 6e  ** CAPI3REF: Dyn
20bc0 61 6d 69 63 61 6c 6c 79 20 54 79 70 65 64 20 56  amically Typed V
20bd0 61 6c 75 65 20 4f 62 6a 65 63 74 20 7b 48 31 35  alue Object {H15
20be0 30 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a  000} <S20200>.**
20bf0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 72 6f 74   KEYWORDS: {prot
20c00 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
20c10 6c 75 65 7d 20 7b 75 6e 70 72 6f 74 65 63 74 65  lue} {unprotecte
20c20 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d  d sqlite3_value}
20c30 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73  .**.** SQLite us
20c40 65 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  es the sqlite3_v
20c50 61 6c 75 65 20 6f 62 6a 65 63 74 20 74 6f 20 72  alue object to r
20c60 65 70 72 65 73 65 6e 74 20 61 6c 6c 20 76 61 6c  epresent all val
20c70 75 65 73 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20  ues.** that can 
20c80 62 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 64  be stored in a d
20c90 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 20 53  atabase table. S
20ca0 51 4c 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d  QLite uses dynam
20cb0 69 63 20 74 79 70 69 6e 67 0a 2a 2a 20 66 6f 72  ic typing.** for
20cc0 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 73   the values it s
20cd0 74 6f 72 65 73 2e 20 56 61 6c 75 65 73 20 73 74  tores. Values st
20ce0 6f 72 65 64 20 69 6e 20 73 71 6c 69 74 65 33 5f  ored in sqlite3_
20cf0 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 0a 2a 2a  value objects.**
20d00 20 63 61 6e 20 62 65 20 69 6e 74 65 67 65 72 73   can be integers
20d10 2c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  , floating point
20d20 20 76 61 6c 75 65 73 2c 20 73 74 72 69 6e 67 73   values, strings
20d30 2c 20 42 4c 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c  , BLOBs, or NULL
20d40 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 73 71 6c 69 74  ..**.** An sqlit
20d50 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20  e3_value object 
20d60 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 22 70  may be either "p
20d70 72 6f 74 65 63 74 65 64 22 20 6f 72 20 22 75 6e  rotected" or "un
20d80 70 72 6f 74 65 63 74 65 64 22 2e 0a 2a 2a 20 53  protected"..** S
20d90 6f 6d 65 20 69 6e 74 65 72 66 61 63 65 73 20 72  ome interfaces r
20da0 65 71 75 69 72 65 20 61 20 70 72 6f 74 65 63 74  equire a protect
20db0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
20dc0 2e 20 20 4f 74 68 65 72 20 69 6e 74 65 72 66 61  .  Other interfa
20dd0 63 65 73 0a 2a 2a 20 77 69 6c 6c 20 61 63 63 65  ces.** will acce
20de0 70 74 20 65 69 74 68 65 72 20 61 20 70 72 6f 74  pt either a prot
20df0 65 63 74 65 64 20 6f 72 20 61 6e 20 75 6e 70 72  ected or an unpr
20e00 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
20e10 76 61 6c 75 65 2e 0a 2a 2a 20 45 76 65 72 79 20  value..** Every 
20e20 69 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 61  interface that a
20e30 63 63 65 70 74 73 20 73 71 6c 69 74 65 33 5f 76  ccepts sqlite3_v
20e40 61 6c 75 65 20 61 72 67 75 6d 65 6e 74 73 20 73  alue arguments s
20e50 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68 65 74  pecifies.** whet
20e60 68 65 72 20 6f 72 20 6e 6f 74 20 69 74 20 72 65  her or not it re
20e70 71 75 69 72 65 73 20 61 20 70 72 6f 74 65 63 74  quires a protect
20e80 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
20e90 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d  ..**.** The term
20ea0 73 20 22 70 72 6f 74 65 63 74 65 64 22 20 61 6e  s "protected" an
20eb0 64 20 22 75 6e 70 72 6f 74 65 63 74 65 64 22 20  d "unprotected" 
20ec0 72 65 66 65 72 20 74 6f 20 77 68 65 74 68 65 72  refer to whether
20ed0 20 6f 72 20 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74   or not.** a mut
20ee0 65 78 20 69 73 20 68 65 6c 64 2e 20 20 41 20 69  ex is held.  A i
20ef0 6e 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 73  nternal mutex is
20f00 20 68 65 6c 64 20 66 6f 72 20 61 20 70 72 6f 74   held for a prot
20f10 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ected.** sqlite3
20f20 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 62 75  _value object bu
20f30 74 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 68 65  t no mutex is he
20f40 6c 64 20 66 6f 72 20 61 6e 20 75 6e 70 72 6f 74  ld for an unprot
20f50 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ected.** sqlite3
20f60 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 20 20  _value object.  
20f70 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  If SQLite is com
20f80 70 69 6c 65 64 20 74 6f 20 62 65 20 73 69 6e 67  piled to be sing
20f90 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 28  le-threaded.** (
20fa0 77 69 74 68 20 5b 53 51 4c 49 54 45 5f 54 48 52  with [SQLITE_THR
20fb0 45 41 44 53 41 46 45 3d 30 5d 20 61 6e 64 20 77  EADSAFE=0] and w
20fc0 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 74 68 72  ith [sqlite3_thr
20fd0 65 61 64 73 61 66 65 28 29 5d 20 72 65 74 75 72  eadsafe()] retur
20fe0 6e 69 6e 67 20 30 29 0a 2a 2a 20 6f 72 20 69 66  ning 0).** or if
20ff0 20 53 51 4c 69 74 65 20 69 73 20 72 75 6e 20 69   SQLite is run i
21000 6e 20 6f 6e 65 20 6f 66 20 72 65 64 75 63 65 64  n one of reduced
21010 20 6d 75 74 65 78 20 6d 6f 64 65 73 20 0a 2a 2a   mutex modes .**
21020 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
21030 53 49 4e 47 4c 45 54 48 52 45 41 44 5d 20 6f 72  SINGLETHREAD] or
21040 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
21050 4d 55 4c 54 49 54 48 52 45 41 44 5d 0a 2a 2a 20  MULTITHREAD].** 
21060 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e 6f  then there is no
21070 20 64 69 73 74 69 6e 63 74 69 6f 6e 20 62 65 74   distinction bet
21080 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64 20 61  ween protected a
21090 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a  nd unprotected.*
210a0 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  * sqlite3_value 
210b0 6f 62 6a 65 63 74 73 20 61 6e 64 20 74 68 65 79  objects and they
210c0 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 74   can be used int
210d0 65 72 63 68 61 6e 67 65 61 62 6c 79 2e 20 20 48  erchangeably.  H
210e0 6f 77 65 76 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d  owever,.** for m
210f0 61 78 69 6d 75 6d 20 63 6f 64 65 20 70 6f 72 74  aximum code port
21100 61 62 69 6c 69 74 79 20 69 74 20 69 73 20 72 65  ability it is re
21110 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20 61  commended that a
21120 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73  pplications.** s
21130 74 69 6c 6c 20 6d 61 6b 65 20 74 68 65 20 64 69  till make the di
21140 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65  stinction betwee
21150 6e 20 62 65 74 77 65 65 6e 20 70 72 6f 74 65 63  n between protec
21160 74 65 64 20 61 6e 64 20 75 6e 70 72 6f 74 65 63  ted and unprotec
21170 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76  ted.** sqlite3_v
21180 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 65 76 65  alue objects eve
21190 6e 20 77 68 65 6e 20 6e 6f 74 20 73 74 72 69 63  n when not stric
211a0 74 6c 79 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a  tly required..**
211b0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
211c0 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 74 68  value objects th
211d0 61 74 20 61 72 65 20 70 61 73 73 65 64 20 61 73  at are passed as
211e0 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 74 6f   parameters into
211f0 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e   the.** implemen
21200 74 61 74 69 6f 6e 20 6f 66 20 5b 61 70 70 6c 69  tation of [appli
21210 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53  cation-defined S
21220 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 20 61 72  QL functions] ar
21230 65 20 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20  e protected..** 
21240 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  The sqlite3_valu
21250 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65  e object returne
21260 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  d by.** [sqlite3
21270 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d  _column_value()]
21280 20 69 73 20 75 6e 70 72 6f 74 65 63 74 65 64 2e   is unprotected.
21290 0a 2a 2a 20 55 6e 70 72 6f 74 65 63 74 65 64 20  .** Unprotected 
212a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
212b0 6a 65 63 74 73 20 6d 61 79 20 6f 6e 6c 79 20 62  jects may only b
212c0 65 20 75 73 65 64 20 77 69 74 68 0a 2a 2a 20 5b  e used with.** [
212d0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
212e0 61 6c 75 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c  alue()] and [sql
212f0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28  ite3_bind_value(
21300 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69  )]..** The [sqli
21310 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 7c  te3_value_blob |
21320 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
21330 79 70 65 28 29 5d 20 66 61 6d 69 6c 79 20 6f 66  ype()] family of
21340 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 72  .** interfaces r
21350 65 71 75 69 72 65 20 70 72 6f 74 65 63 74 65 64  equire protected
21360 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
21370 62 6a 65 63 74 73 2e 0a 2a 2f 0a 74 79 70 65 64  bjects..*/.typed
21380 65 66 20 73 74 72 75 63 74 20 4d 65 6d 20 73 71  ef struct Mem sq
21390 6c 69 74 65 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a  lite3_value;../*
213a0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51  .** CAPI3REF: SQ
213b0 4c 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65  L Function Conte
213c0 78 74 20 4f 62 6a 65 63 74 20 7b 48 31 36 30 30  xt Object {H1600
213d0 31 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a  1} <S20200>.**.*
213e0 2a 20 54 68 65 20 63 6f 6e 74 65 78 74 20 69 6e  * The context in
213f0 20 77 68 69 63 68 20 61 6e 20 53 51 4c 20 66 75   which an SQL fu
21400 6e 63 74 69 6f 6e 20 65 78 65 63 75 74 65 73 20  nction executes 
21410 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 6e 0a  is stored in an.
21420 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ** sqlite3_conte
21430 78 74 20 6f 62 6a 65 63 74 2e 20 20 41 20 70 6f  xt object.  A po
21440 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69  inter to an sqli
21450 74 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65  te3_context obje
21460 63 74 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20  ct.** is always 
21470 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
21480 74 6f 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d  to [application-
21490 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63  defined SQL func
214a0 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 54 68 65 20 61  tions]..** The a
214b0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
214c0 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ed SQL function 
214d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
214e0 69 6c 6c 20 70 61 73 73 20 74 68 69 73 0a 2a 2a  ill pass this.**
214f0 20 70 6f 69 6e 74 65 72 20 74 68 72 6f 75 67 68   pointer through
21500 20 69 6e 74 6f 20 63 61 6c 6c 73 20 74 6f 20 5b   into calls to [
21510 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
21520 6e 74 20 7c 20 73 71 6c 69 74 65 33 5f 72 65 73  nt | sqlite3_res
21530 75 6c 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  ult()],.** [sqli
21540 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
21550 6e 74 65 78 74 28 29 5d 2c 20 5b 73 71 6c 69 74  ntext()], [sqlit
21560 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2c  e3_user_data()],
21570 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  .** [sqlite3_con
21580 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29  text_db_handle()
21590 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f  ], [sqlite3_get_
215a0 61 75 78 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 61  auxdata()],.** a
215b0 6e 64 2f 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73  nd/or [sqlite3_s
215c0 65 74 5f 61 75 78 64 61 74 61 28 29 5d 2e 0a 2a  et_auxdata()]..*
215d0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
215e0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
215f0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
21600 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
21610 46 3a 20 42 69 6e 64 69 6e 67 20 56 61 6c 75 65  F: Binding Value
21620 73 20 54 6f 20 50 72 65 70 61 72 65 64 20 53 74  s To Prepared St
21630 61 74 65 6d 65 6e 74 73 20 7b 48 31 33 35 30 30  atements {H13500
21640 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 20 4b 45  } <S70300>.** KE
21650 59 57 4f 52 44 53 3a 20 7b 68 6f 73 74 20 70 61  YWORDS: {host pa
21660 72 61 6d 65 74 65 72 7d 20 7b 68 6f 73 74 20 70  rameter} {host p
21670 61 72 61 6d 65 74 65 72 73 7d 20 7b 68 6f 73 74  arameters} {host
21680 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 7d   parameter name}
21690 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53  .** KEYWORDS: {S
216a0 51 4c 20 70 61 72 61 6d 65 74 65 72 7d 20 7b 53  QL parameter} {S
216b0 51 4c 20 70 61 72 61 6d 65 74 65 72 73 7d 20 7b  QL parameters} {
216c0 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e  parameter bindin
216d0 67 7d 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  g}.**.** In the 
216e0 53 51 4c 20 73 74 72 69 6e 67 73 20 69 6e 70 75  SQL strings inpu
216f0 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72  t to [sqlite3_pr
21700 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20  epare_v2()] and 
21710 69 74 73 20 76 61 72 69 61 6e 74 73 2c 0a 2a 2a  its variants,.**
21720 20 6c 69 74 65 72 61 6c 73 20 6d 61 79 20 62 65   literals may be
21730 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 20 5b   replaced by a [
21740 70 61 72 61 6d 65 74 65 72 5d 20 74 68 61 74 20  parameter] that 
21750 6d 61 74 63 68 65 73 20 6f 6e 65 20 6f 66 20 66  matches one of f
21760 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 74 65 6d 70  ollowing.** temp
21770 6c 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  lates:.**.** <ul
21780 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 0a 2a 2a 20  >.** <li>  ?.** 
21790 3c 6c 69 3e 20 20 3f 4e 4e 4e 0a 2a 2a 20 3c 6c  <li>  ?NNN.** <l
217a0 69 3e 20 20 3a 56 56 56 0a 2a 2a 20 3c 6c 69 3e  i>  :VVV.** <li>
217b0 20 20 40 56 56 56 0a 2a 2a 20 3c 6c 69 3e 20 20    @VVV.** <li>  
217c0 24 56 56 56 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a  $VVV.** </ul>.**
217d0 0a 2a 2a 20 49 6e 20 74 68 65 20 74 65 6d 70 6c  .** In the templ
217e0 61 74 65 73 20 61 62 6f 76 65 2c 20 4e 4e 4e 20  ates above, NNN 
217f0 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e  represents an in
21800 74 65 67 65 72 20 6c 69 74 65 72 61 6c 2c 0a 2a  teger literal,.*
21810 2a 20 61 6e 64 20 56 56 56 20 72 65 70 72 65 73  * and VVV repres
21820 65 6e 74 73 20 61 6e 20 61 6c 70 68 61 6e 75 6d  ents an alphanum
21830 65 72 69 63 20 69 64 65 6e 74 69 66 65 72 2e 20  eric identifer. 
21840 20 54 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74   The values of t
21850 68 65 73 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65  hese.** paramete
21860 72 73 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20  rs (also called 
21870 22 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20  "host parameter 
21880 6e 61 6d 65 73 22 20 6f 72 20 22 53 51 4c 20 70  names" or "SQL p
21890 61 72 61 6d 65 74 65 72 73 22 29 0a 2a 2a 20 63  arameters").** c
218a0 61 6e 20 62 65 20 73 65 74 20 75 73 69 6e 67 20  an be set using 
218b0 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  the sqlite3_bind
218c0 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 64 65  _*() routines de
218d0 66 69 6e 65 64 20 68 65 72 65 2e 0a 2a 2a 0a 2a  fined here..**.*
218e0 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
218f0 6d 65 6e 74 20 74 6f 20 74 68 65 20 73 71 6c 69  ment to the sqli
21900 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75  te3_bind_*() rou
21910 74 69 6e 65 73 20 69 73 20 61 6c 77 61 79 73 0a  tines is always.
21920 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
21930 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d  the [sqlite3_stm
21940 74 5d 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e  t] object return
21950 65 64 20 66 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69  ed from.** [sqli
21960 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
21970 5d 20 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74  ] or its variant
21980 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  s..**.** The sec
21990 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ond argument is 
219a0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
219b0 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 74   SQL parameter t
219c0 6f 20 62 65 20 73 65 74 2e 0a 2a 2a 20 54 68 65  o be set..** The
219d0 20 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 70 61   leftmost SQL pa
219e0 72 61 6d 65 74 65 72 20 68 61 73 20 61 6e 20 69  rameter has an i
219f0 6e 64 65 78 20 6f 66 20 31 2e 20 20 57 68 65 6e  ndex of 1.  When
21a00 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 64 0a   the same named.
21a10 2a 2a 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  ** SQL parameter
21a20 20 69 73 20 75 73 65 64 20 6d 6f 72 65 20 74 68   is used more th
21a30 61 6e 20 6f 6e 63 65 2c 20 73 65 63 6f 6e 64 20  an once, second 
21a40 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 0a 2a  and subsequent.*
21a50 2a 20 6f 63 63 75 72 72 65 6e 63 65 73 20 68 61  * occurrences ha
21a60 76 65 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65  ve the same inde
21a70 78 20 61 73 20 74 68 65 20 66 69 72 73 74 20 6f  x as the first o
21a80 63 63 75 72 72 65 6e 63 65 2e 0a 2a 2a 20 54 68  ccurrence..** Th
21a90 65 20 69 6e 64 65 78 20 66 6f 72 20 6e 61 6d 65  e index for name
21aa0 64 20 70 61 72 61 6d 65 74 65 72 73 20 63 61 6e  d parameters can
21ab0 20 62 65 20 6c 6f 6f 6b 65 64 20 75 70 20 75 73   be looked up us
21ac0 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69  ing the.** [sqli
21ad0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
21ae0 65 72 5f 69 6e 64 65 78 28 29 5d 20 41 50 49 20  er_index()] API 
21af0 69 66 20 64 65 73 69 72 65 64 2e 20 20 54 68 65  if desired.  The
21b00 20 69 6e 64 65 78 0a 2a 2a 20 66 6f 72 20 22 3f   index.** for "?
21b10 4e 4e 4e 22 20 70 61 72 61 6d 65 74 65 72 73 20  NNN" parameters 
21b20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  is the value of 
21b30 4e 4e 4e 2e 0a 2a 2a 20 54 68 65 20 4e 4e 4e 20  NNN..** The NNN 
21b40 76 61 6c 75 65 20 6d 75 73 74 20 62 65 20 62 65  value must be be
21b50 74 77 65 65 6e 20 31 20 61 6e 64 20 74 68 65 20  tween 1 and the 
21b60 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29  [sqlite3_limit()
21b70 5d 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 5b  ].** parameter [
21b80 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
21b90 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 28 64  IABLE_NUMBER] (d
21ba0 65 66 61 75 6c 74 20 76 61 6c 75 65 3a 20 39 39  efault value: 99
21bb0 39 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68  9)..**.** The th
21bc0 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ird argument is 
21bd0 74 68 65 20 76 61 6c 75 65 20 74 6f 20 62 69 6e  the value to bin
21be0 64 20 74 6f 20 74 68 65 20 70 61 72 61 6d 65 74  d to the paramet
21bf0 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 6f  er..**.** In tho
21c00 73 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  se routines that
21c10 20 68 61 76 65 20 61 20 66 6f 75 72 74 68 20 61   have a fourth a
21c20 72 67 75 6d 65 6e 74 2c 20 69 74 73 20 76 61 6c  rgument, its val
21c30 75 65 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d  ue is the.** num
21c40 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
21c50 74 68 65 20 70 61 72 61 6d 65 74 65 72 2e 20 20  the parameter.  
21c60 54 6f 20 62 65 20 63 6c 65 61 72 3a 20 74 68 65  To be clear: the
21c70 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a 2a   value is the.**
21c80 20 6e 75 6d 62 65 72 20 6f 66 20 3c 75 3e 62 79   number of <u>by
21c90 74 65 73 3c 2f 75 3e 20 69 6e 20 74 68 65 20 76  tes</u> in the v
21ca0 61 6c 75 65 2c 20 6e 6f 74 20 74 68 65 20 6e 75  alue, not the nu
21cb0 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
21cc0 72 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f  rs..** If the fo
21cd0 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69  urth parameter i
21ce0 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20  s negative, the 
21cf0 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74  length of the st
21d00 72 69 6e 67 20 69 73 0a 2a 2a 20 74 68 65 20 6e  ring is.** the n
21d10 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 75  umber of bytes u
21d20 70 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a  p to the first z
21d30 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a  ero terminator..
21d40 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 66 74 68 20  **.** The fifth 
21d50 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
21d60 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 29 2c  te3_bind_blob(),
21d70 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
21d80 78 74 28 29 2c 20 61 6e 64 0a 2a 2a 20 73 71 6c  xt(), and.** sql
21d90 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
21da0 28 29 20 69 73 20 61 20 64 65 73 74 72 75 63 74  () is a destruct
21db0 6f 72 20 75 73 65 64 20 74 6f 20 64 69 73 70 6f  or used to dispo
21dc0 73 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 6f  se of the BLOB o
21dd0 72 0a 2a 2a 20 73 74 72 69 6e 67 20 61 66 74 65  r.** string afte
21de0 72 20 53 51 4c 69 74 65 20 68 61 73 20 66 69 6e  r SQLite has fin
21df0 69 73 68 65 64 20 77 69 74 68 20 69 74 2e 20 49  ished with it. I
21e00 66 20 74 68 65 20 66 69 66 74 68 20 61 72 67 75  f the fifth argu
21e10 6d 65 6e 74 20 69 73 0a 2a 2a 20 74 68 65 20 73  ment is.** the s
21e20 70 65 63 69 61 6c 20 76 61 6c 75 65 20 5b 53 51  pecial value [SQ
21e30 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c 20 74 68  LITE_STATIC], th
21e40 65 6e 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65  en SQLite assume
21e50 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 69 6e  s that the.** in
21e60 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 69 6e 20  formation is in 
21e70 73 74 61 74 69 63 2c 20 75 6e 6d 61 6e 61 67 65  static, unmanage
21e80 64 20 73 70 61 63 65 20 61 6e 64 20 64 6f 65 73  d space and does
21e90 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
21ea0 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65  freed..** If the
21eb0 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20   fifth argument 
21ec0 68 61 73 20 74 68 65 20 76 61 6c 75 65 20 5b 53  has the value [S
21ed0 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 5d  QLITE_TRANSIENT]
21ee0 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65  , then.** SQLite
21ef0 20 6d 61 6b 65 73 20 69 74 73 20 6f 77 6e 20 70   makes its own p
21f00 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 74  rivate copy of t
21f10 68 65 20 64 61 74 61 20 69 6d 6d 65 64 69 61 74  he data immediat
21f20 65 6c 79 2c 20 62 65 66 6f 72 65 0a 2a 2a 20 74  ely, before.** t
21f30 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  he sqlite3_bind_
21f40 2a 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74 75  *() routine retu
21f50 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  rns..**.** The s
21f60 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f  qlite3_bind_zero
21f70 62 6c 6f 62 28 29 20 72 6f 75 74 69 6e 65 20 62  blob() routine b
21f80 69 6e 64 73 20 61 20 42 4c 4f 42 20 6f 66 20 6c  inds a BLOB of l
21f90 65 6e 67 74 68 20 4e 20 74 68 61 74 0a 2a 2a 20  ength N that.** 
21fa0 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 7a  is filled with z
21fb0 65 72 6f 65 73 2e 20 20 41 20 7a 65 72 6f 62 6c  eroes.  A zerobl
21fc0 6f 62 20 75 73 65 73 20 61 20 66 69 78 65 64 20  ob uses a fixed 
21fd0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
21fe0 0a 2a 2a 20 28 6a 75 73 74 20 61 6e 20 69 6e 74  .** (just an int
21ff0 65 67 65 72 20 74 6f 20 68 6f 6c 64 20 69 74 73  eger to hold its
22000 20 73 69 7a 65 29 20 77 68 69 6c 65 20 69 74 20   size) while it 
22010 69 73 20 62 65 69 6e 67 20 70 72 6f 63 65 73 73  is being process
22020 65 64 2e 0a 2a 2a 20 5a 65 72 6f 62 6c 6f 62 73  ed..** Zeroblobs
22030 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f   are intended to
22040 20 73 65 72 76 65 20 61 73 20 70 6c 61 63 65 68   serve as placeh
22050 6f 6c 64 65 72 73 20 66 6f 72 20 42 4c 4f 42 73  olders for BLOBs
22060 20 77 68 6f 73 65 0a 2a 2a 20 63 6f 6e 74 65 6e   whose.** conten
22070 74 20 69 73 20 6c 61 74 65 72 20 77 72 69 74 74  t is later writt
22080 65 6e 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c  en using.** [sql
22090 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 7c  ite3_blob_open |
220a0 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42 4c 4f   incremental BLO
220b0 42 20 49 2f 4f 5d 20 72 6f 75 74 69 6e 65 73 2e  B I/O] routines.
220c0 0a 2a 2a 20 41 20 6e 65 67 61 74 69 76 65 20 76  .** A negative v
220d0 61 6c 75 65 20 66 6f 72 20 74 68 65 20 7a 65 72  alue for the zer
220e0 6f 62 6c 6f 62 20 72 65 73 75 6c 74 73 20 69 6e  oblob results in
220f0 20 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42   a zero-length B
22100 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  LOB..**.** The s
22110 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20  qlite3_bind_*() 
22120 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65  routines must be
22130 20 63 61 6c 6c 65 64 20 61 66 74 65 72 0a 2a 2a   called after.**
22140 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
22150 65 5f 76 32 28 29 5d 20 28 61 6e 64 20 69 74 73  e_v2()] (and its
22160 20 76 61 72 69 61 6e 74 73 29 20 6f 72 20 5b 73   variants) or [s
22170 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
22180 61 6e 64 0a 2a 2a 20 62 65 66 6f 72 65 20 5b 73  and.** before [s
22190 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a  qlite3_step()]..
221a0 2a 2a 20 42 69 6e 64 69 6e 67 73 20 61 72 65 20  ** Bindings are 
221b0 6e 6f 74 20 63 6c 65 61 72 65 64 20 62 79 20 74  not cleared by t
221c0 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  he [sqlite3_rese
221d0 74 28 29 5d 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a  t()] routine..**
221e0 20 55 6e 62 6f 75 6e 64 20 70 61 72 61 6d 65 74   Unbound paramet
221f0 65 72 73 20 61 72 65 20 69 6e 74 65 72 70 72 65  ers are interpre
22200 74 65 64 20 61 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ted as NULL..**.
22210 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
22220 73 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45  s return [SQLITE
22230 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73 20  _OK] on success 
22240 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  or an error code
22250 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20   if.** anything 
22260 67 6f 65 73 20 77 72 6f 6e 67 2e 20 20 5b 53 51  goes wrong.  [SQ
22270 4c 49 54 45 5f 52 41 4e 47 45 5d 20 69 73 20 72  LITE_RANGE] is r
22280 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 70  eturned if the p
22290 61 72 61 6d 65 74 65 72 0a 2a 2a 20 69 6e 64 65  arameter.** inde
222a0 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  x is out of rang
222b0 65 2e 20 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45  e.  [SQLITE_NOME
222c0 4d 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  M] is returned i
222d0 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  f malloc() fails
222e0 2e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53  ..** [SQLITE_MIS
222f0 55 53 45 5d 20 6d 69 67 68 74 20 62 65 20 72 65  USE] might be re
22300 74 75 72 6e 65 64 20 69 66 20 74 68 65 73 65 20  turned if these 
22310 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c  routines are cal
22320 6c 65 64 20 6f 6e 20 61 0a 2a 2a 20 76 69 72 74  led on a.** virt
22330 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 61 74  ual machine that
22340 20 69 73 20 74 68 65 20 77 72 6f 6e 67 20 73 74   is the wrong st
22350 61 74 65 20 6f 72 20 77 68 69 63 68 20 68 61 73  ate or which has
22360 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66 69   already been fi
22370 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 44 65 74 65  nalized..** Dete
22380 63 74 69 6f 6e 20 6f 66 20 6d 69 73 75 73 65 20  ction of misuse 
22390 69 73 20 75 6e 72 65 6c 69 61 62 6c 65 2e 20 20  is unreliable.  
223a0 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f  Applications sho
223b0 75 6c 64 20 6e 6f 74 20 64 65 70 65 6e 64 0a 2a  uld not depend.*
223c0 2a 20 6f 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  * on SQLITE_MISU
223d0 53 45 20 72 65 74 75 72 6e 73 2e 20 20 53 51 4c  SE returns.  SQL
223e0 49 54 45 5f 4d 49 53 55 53 45 20 69 73 20 69 6e  ITE_MISUSE is in
223f0 74 65 6e 64 65 64 20 74 6f 20 69 6e 64 69 63 61  tended to indica
22400 74 65 20 61 0a 2a 2a 20 61 20 6c 6f 67 69 63 20  te a.** a logic 
22410 65 72 72 6f 72 20 69 6e 20 74 68 65 20 61 70 70  error in the app
22420 6c 69 63 61 74 69 6f 6e 2e 20 20 46 75 74 75 72  lication.  Futur
22430 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
22440 4c 69 74 65 20 6d 69 67 68 74 0a 2a 2a 20 70 61  Lite might.** pa
22450 6e 69 63 20 72 61 74 68 65 72 20 74 68 61 6e 20  nic rather than 
22460 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
22470 53 55 53 45 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  SUSE..**.** See 
22480 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62  also: [sqlite3_b
22490 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
224a0 75 6e 74 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  unt()],.** [sqli
224b0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
224c0 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e 64 20  er_name()], and 
224d0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
224e0 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d  rameter_index()]
224f0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
22500 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 35 30 36  ents:.** [H13506
22510 5d 20 5b 48 31 33 35 30 39 5d 20 5b 48 31 33 35  ] [H13509] [H135
22520 31 32 5d 20 5b 48 31 33 35 31 35 5d 20 5b 48 31  12] [H13515] [H1
22530 33 35 31 38 5d 20 5b 48 31 33 35 32 31 5d 20 5b  3518] [H13521] [
22540 48 31 33 35 32 34 5d 20 5b 48 31 33 35 32 37 5d  H13524] [H13527]
22550 0a 2a 2a 20 5b 48 31 33 35 33 30 5d 20 5b 48 31  .** [H13530] [H1
22560 33 35 33 33 5d 20 5b 48 31 33 35 33 36 5d 20 5b  3533] [H13536] [
22570 48 31 33 35 33 39 5d 20 5b 48 31 33 35 34 32 5d  H13539] [H13542]
22580 20 5b 48 31 33 35 34 35 5d 20 5b 48 31 33 35 34   [H13545] [H1354
22590 38 5d 20 5b 48 31 33 35 35 31 5d 0a 2a 2a 0a 2a  8] [H13551].**.*
225a0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
225b0 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
225c0 6f 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  ob(sqlite3_stmt*
225d0 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69  , int, const voi
225e0 64 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 64 28  d*, int n, void(
225f0 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.SQLI
22600 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22610 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 28 73  e3_bind_double(s
22620 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
22630 74 2c 20 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49  t, double);.SQLI
22640 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22650 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69  e3_bind_int(sqli
22660 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20  te3_stmt*, int, 
22670 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
22680 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
22690 64 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f  d_int64(sqlite3_
226a0 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 73 71 6c 69  stmt*, int, sqli
226b0 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c 49  te3_int64);.SQLI
226c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
226d0 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 73 71 6c  e3_bind_null(sql
226e0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 29  ite3_stmt*, int)
226f0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
22700 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
22710 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  xt(sqlite3_stmt*
22720 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61  , int, const cha
22730 72 2a 2c 20 69 6e 74 20 6e 2c 20 76 6f 69 64 28  r*, int n, void(
22740 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49  *)(void*));.SQLI
22750 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
22760 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 73  e3_bind_text16(s
22770 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
22780 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  t, const void*, 
22790 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69  int, void(*)(voi
227a0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
227b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
227c0 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  d_value(sqlite3_
227d0 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73  stmt*, int, cons
227e0 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t sqlite3_value*
227f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
22800 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a  t sqlite3_bind_z
22810 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  eroblob(sqlite3_
22820 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 20  stmt*, int, int 
22830 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  n);../*.** CAPI3
22840 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66 20 53  REF: Number Of S
22850 51 4c 20 50 61 72 61 6d 65 74 65 72 73 20 7b 48  QL Parameters {H
22860 31 33 36 30 30 7d 20 3c 53 37 30 33 30 30 3e 0a  13600} <S70300>.
22870 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
22880 6e 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  ne can be used t
22890 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d 62 65  o find the numbe
228a0 72 20 6f 66 20 5b 53 51 4c 20 70 61 72 61 6d 65  r of [SQL parame
228b0 74 65 72 73 5d 0a 2a 2a 20 69 6e 20 61 20 5b 70  ters].** in a [p
228c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
228d0 74 5d 2e 20 20 53 51 4c 20 70 61 72 61 6d 65 74  t].  SQL paramet
228e0 65 72 73 20 61 72 65 20 74 6f 6b 65 6e 73 20 6f  ers are tokens o
228f0 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20 22 3f  f the.** form "?
22900 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 3a 41 41 41  ", "?NNN", ":AAA
22910 22 2c 20 22 24 41 41 41 22 2c 20 6f 72 20 22 40  ", "$AAA", or "@
22920 41 41 41 22 20 74 68 61 74 20 73 65 72 76 65 20  AAA" that serve 
22930 61 73 0a 2a 2a 20 70 6c 61 63 65 68 6f 6c 64 65  as.** placeholde
22940 72 73 20 66 6f 72 20 76 61 6c 75 65 73 20 74 68  rs for values th
22950 61 74 20 61 72 65 20 5b 73 71 6c 69 74 65 33 5f  at are [sqlite3_
22960 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 6f 75 6e  bind_blob | boun
22970 64 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61 72  d].** to the par
22980 61 6d 65 74 65 72 73 20 61 74 20 61 20 6c 61 74  ameters at a lat
22990 65 72 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54  er time..**.** T
229a0 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 74 75  his routine actu
229b0 61 6c 6c 79 20 72 65 74 75 72 6e 73 20 74 68 65  ally returns the
229c0 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6c 61   index of the la
229d0 72 67 65 73 74 20 28 72 69 67 68 74 6d 6f 73 74  rgest (rightmost
229e0 29 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e 20  ).** parameter. 
229f0 46 6f 72 20 61 6c 6c 20 66 6f 72 6d 73 20 65 78  For all forms ex
22a00 63 65 70 74 20 3f 4e 4e 4e 2c 20 74 68 69 73 20  cept ?NNN, this 
22a10 77 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e 64 20  will correspond 
22a20 74 6f 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  to the.** number
22a30 20 6f 66 20 75 6e 69 71 75 65 20 70 61 72 61 6d   of unique param
22a40 65 74 65 72 73 2e 20 20 49 66 20 70 61 72 61 6d  eters.  If param
22a50 65 74 65 72 73 20 6f 66 20 74 68 65 20 3f 4e 4e  eters of the ?NN
22a60 4e 20 61 72 65 20 75 73 65 64 2c 0a 2a 2a 20 74  N are used,.** t
22a70 68 65 72 65 20 6d 61 79 20 62 65 20 67 61 70 73  here may be gaps
22a80 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a   in the list..**
22a90 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
22aa0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
22ab0 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d  |sqlite3_bind()]
22ac0 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  ,.** [sqlite3_bi
22ad0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
22ae0 65 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71  e()], and.** [sq
22af0 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
22b00 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a  eter_index()]..*
22b10 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
22b20 73 3a 0a 2a 2a 20 5b 48 31 33 36 30 31 5d 0a 2a  s:.** [H13601].*
22b30 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
22b40 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61   sqlite3_bind_pa
22b50 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73 71  rameter_count(sq
22b60 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f  lite3_stmt*);../
22b70 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e  *.** CAPI3REF: N
22b80 61 6d 65 20 4f 66 20 41 20 48 6f 73 74 20 50 61  ame Of A Host Pa
22b90 72 61 6d 65 74 65 72 20 7b 48 31 33 36 32 30 7d  rameter {H13620}
22ba0 20 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S70300>.**.** 
22bb0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
22bc0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
22bd0 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  o the name of th
22be0 65 20 6e 2d 74 68 0a 2a 2a 20 5b 53 51 4c 20 70  e n-th.** [SQL p
22bf0 61 72 61 6d 65 74 65 72 5d 20 69 6e 20 61 20 5b  arameter] in a [
22c00 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
22c10 6e 74 5d 2e 0a 2a 2a 20 53 51 4c 20 70 61 72 61  nt]..** SQL para
22c20 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 66 6f  meters of the fo
22c30 72 6d 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41  rm "?NNN" or ":A
22c40 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72  AA" or "@AAA" or
22c50 20 22 24 41 41 41 22 0a 2a 2a 20 68 61 76 65 20   "$AAA".** have 
22c60 61 20 6e 61 6d 65 20 77 68 69 63 68 20 69 73 20  a name which is 
22c70 74 68 65 20 73 74 72 69 6e 67 20 22 3f 4e 4e 4e  the string "?NNN
22c80 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22  " or ":AAA" or "
22c90 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a  @AAA" or "$AAA".
22ca0 2a 2a 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  ** respectively.
22cb0 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  .** In other wor
22cc0 64 73 2c 20 74 68 65 20 69 6e 69 74 69 61 6c 20  ds, the initial 
22cd0 22 3a 22 20 6f 72 20 22 24 22 20 6f 72 20 22 40  ":" or "$" or "@
22ce0 22 20 6f 72 20 22 3f 22 0a 2a 2a 20 69 73 20 69  " or "?".** is i
22cf0 6e 63 6c 75 64 65 64 20 61 73 20 70 61 72 74 20  ncluded as part 
22d00 6f 66 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 20  of the name..** 
22d10 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74 68  Parameters of th
22d20 65 20 66 6f 72 6d 20 22 3f 22 20 77 69 74 68 6f  e form "?" witho
22d30 75 74 20 61 20 66 6f 6c 6c 6f 77 69 6e 67 20 69  ut a following i
22d40 6e 74 65 67 65 72 20 68 61 76 65 20 6e 6f 20 6e  nteger have no n
22d50 61 6d 65 0a 2a 2a 20 61 6e 64 20 61 72 65 20 61  ame.** and are a
22d60 6c 73 6f 20 72 65 66 65 72 72 65 64 20 74 6f 20  lso referred to 
22d70 61 73 20 22 61 6e 6f 6e 79 6d 6f 75 73 20 70 61  as "anonymous pa
22d80 72 61 6d 65 74 65 72 73 22 2e 0a 2a 2a 0a 2a 2a  rameters"..**.**
22d90 20 54 68 65 20 66 69 72 73 74 20 68 6f 73 74 20   The first host 
22da0 70 61 72 61 6d 65 74 65 72 20 68 61 73 20 61 6e  parameter has an
22db0 20 69 6e 64 65 78 20 6f 66 20 31 2c 20 6e 6f 74   index of 1, not
22dc0 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65   0..**.** If the
22dd0 20 76 61 6c 75 65 20 6e 20 69 73 20 6f 75 74 20   value n is out 
22de0 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 20 74  of range or if t
22df0 68 65 20 6e 2d 74 68 20 70 61 72 61 6d 65 74 65  he n-th paramete
22e00 72 20 69 73 0a 2a 2a 20 6e 61 6d 65 6c 65 73 73  r is.** nameless
22e10 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72  , then NULL is r
22e20 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20 72 65  eturned.  The re
22e30 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73  turned string is
22e40 0a 2a 2a 20 61 6c 77 61 79 73 20 69 6e 20 55 54  .** always in UT
22e50 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 65 76 65  F-8 encoding eve
22e60 6e 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 70  n if the named p
22e70 61 72 61 6d 65 74 65 72 20 77 61 73 0a 2a 2a 20  arameter was.** 
22e80 6f 72 69 67 69 6e 61 6c 6c 79 20 73 70 65 63 69  originally speci
22e90 66 69 65 64 20 61 73 20 55 54 46 2d 31 36 20 69  fied as UTF-16 i
22ea0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  n [sqlite3_prepa
22eb0 72 65 31 36 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73  re16()] or.** [s
22ec0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
22ed0 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65  _v2()]..**.** Se
22ee0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
22ef0 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74  _bind_blob|sqlit
22f00 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b  e3_bind()],.** [
22f10 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
22f20 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c  ameter_count()],
22f30 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   and.** [sqlite3
22f40 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
22f50 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  index()]..**.** 
22f60 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
22f70 20 5b 48 31 33 36 32 31 5d 0a 2a 2f 0a 53 51 4c   [H13621].*/.SQL
22f80 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
22f90 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69 6e 64  ar *sqlite3_bind
22fa0 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28  _parameter_name(
22fb0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
22fc0 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  nt);../*.** CAPI
22fd0 33 52 45 46 3a 20 49 6e 64 65 78 20 4f 66 20 41  3REF: Index Of A
22fe0 20 50 61 72 61 6d 65 74 65 72 20 57 69 74 68 20   Parameter With 
22ff0 41 20 47 69 76 65 6e 20 4e 61 6d 65 20 7b 48 31  A Given Name {H1
23000 33 36 34 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a  3640} <S70300>.*
23010 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
23020 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 4c 20  index of an SQL 
23030 70 61 72 61 6d 65 74 65 72 20 67 69 76 65 6e 20  parameter given 
23040 69 74 73 20 6e 61 6d 65 2e 20 20 54 68 65 0a 2a  its name.  The.*
23050 2a 20 69 6e 64 65 78 20 76 61 6c 75 65 20 72 65  * index value re
23060 74 75 72 6e 65 64 20 69 73 20 73 75 69 74 61 62  turned is suitab
23070 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20 74 68  le for use as th
23080 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61 72 61  e second.** para
23090 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
230a0 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69  3_bind_blob|sqli
230b0 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20 20 41 20  te3_bind()].  A 
230c0 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65 74 75 72  zero.** is retur
230d0 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74 63 68 69  ned if no matchi
230e0 6e 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ng parameter is 
230f0 66 6f 75 6e 64 2e 20 20 54 68 65 20 70 61 72 61  found.  The para
23100 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65 20 6d 75  meter.** name mu
23110 73 74 20 62 65 20 67 69 76 65 6e 20 69 6e 20 55  st be given in U
23120 54 46 2d 38 20 65 76 65 6e 20 69 66 20 74 68 65  TF-8 even if the
23130 20 6f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 6d   original statem
23140 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72 65 70 61  ent.** was prepa
23150 72 65 64 20 66 72 6f 6d 20 55 54 46 2d 31 36 20  red from UTF-16 
23160 74 65 78 74 20 75 73 69 6e 67 20 5b 73 71 6c 69  text using [sqli
23170 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
23180 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ()]..**.** See a
23190 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62 69  lso: [sqlite3_bi
231a0 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33 5f  nd_blob|sqlite3_
231b0 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  bind()],.** [sql
231c0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
231d0 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 20 61 6e  ter_count()], an
231e0 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  d.** [sqlite3_bi
231f0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
23200 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  ex()]..**.** Req
23210 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
23220 31 33 36 34 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45  13641].*/.SQLITE
23230 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
23240 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
23250 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74  index(sqlite3_st
23260 6d 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  mt*, const char 
23270 2a 7a 4e 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *zName);../*.** 
23280 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20  CAPI3REF: Reset 
23290 41 6c 6c 20 42 69 6e 64 69 6e 67 73 20 4f 6e 20  All Bindings On 
232a0 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65  A Prepared State
232b0 6d 65 6e 74 20 7b 48 31 33 36 36 30 7d 20 3c 53  ment {H13660} <S
232c0 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e  70300>.**.** Con
232d0 74 72 61 72 79 20 74 6f 20 74 68 65 20 69 6e 74  trary to the int
232e0 75 69 74 69 6f 6e 20 6f 66 20 6d 61 6e 79 2c 20  uition of many, 
232f0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
23300 5d 20 64 6f 65 73 20 6e 6f 74 20 72 65 73 65 74  ] does not reset
23310 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33  .** the [sqlite3
23320 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20 62 69 6e  _bind_blob | bin
23330 64 69 6e 67 73 5d 20 6f 6e 20 61 20 5b 70 72 65  dings] on a [pre
23340 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
23350 2e 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 72 6f  ..** Use this ro
23360 75 74 69 6e 65 20 74 6f 20 72 65 73 65 74 20 61  utine to reset a
23370 6c 6c 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  ll host paramete
23380 72 73 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  rs to NULL..**.*
23390 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
233a0 2a 2a 20 5b 48 31 33 36 36 31 5d 0a 2a 2f 0a 53  ** [H13661].*/.S
233b0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
233c0 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64  lite3_clear_bind
233d0 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ings(sqlite3_stm
233e0 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  t*);../*.** CAPI
233f0 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f 66 20  3REF: Number Of 
23400 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52 65 73  Columns In A Res
23410 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31 30 7d  ult Set {H13710}
23420 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
23430 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
23440 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
23450 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 72  the result set r
23460 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a 2a  eturned by the.*
23470 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  * [prepared stat
23480 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72 6f 75  ement]. This rou
23490 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 20 69  tine returns 0 i
234a0 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20 53 51  f pStmt is an SQ
234b0 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74  L.** statement t
234c0 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 74  hat does not ret
234d0 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20 65 78  urn data (for ex
234e0 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41 54 45  ample an [UPDATE
234f0 5d 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  ])..**.** Requir
23500 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37  ements:.** [H137
23510 31 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  11].*/.SQLITE_AP
23520 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  I int sqlite3_co
23530 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69 74  lumn_count(sqlit
23540 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b  e3_stmt *pStmt);
23550 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
23560 3a 20 43 6f 6c 75 6d 6e 20 4e 61 6d 65 73 20 49  : Column Names I
23570 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 20 7b  n A Result Set {
23580 48 31 33 37 32 30 7d 20 3c 53 31 30 37 30 30 3e  H13720} <S10700>
23590 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
235a0 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74 68 65  tines return the
235b0 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74   name assigned t
235c0 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 63  o a particular c
235d0 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 20 74 68 65 20  olumn.** in the 
235e0 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20  result set of a 
235f0 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
23600 6e 74 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  nt.  The sqlite3
23610 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 0a 2a  _column_name().*
23620 2a 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  * interface retu
23630 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
23640 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74   a zero-terminat
23650 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a  ed UTF-8 string.
23660 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63  ** and sqlite3_c
23670 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 20 72  olumn_name16() r
23680 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
23690 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69   to a zero-termi
236a0 6e 61 74 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20  nated.** UTF-16 
236b0 73 74 72 69 6e 67 2e 20 20 54 68 65 20 66 69 72  string.  The fir
236c0 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  st parameter is 
236d0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  the [prepared st
236e0 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 74 68 61 74  atement].** that
236f0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
23700 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
23710 6e 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70  nt. The second p
23720 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 0a  arameter is the.
23730 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  ** column number
23740 2e 20 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20  .  The leftmost 
23750 63 6f 6c 75 6d 6e 20 69 73 20 6e 75 6d 62 65 72  column is number
23760 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65   0..**.** The re
23770 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 70 6f  turned string po
23780 69 6e 74 65 72 20 69 73 20 76 61 6c 69 64 20 75  inter is valid u
23790 6e 74 69 6c 20 65 69 74 68 65 72 20 74 68 65 20  ntil either the 
237a0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
237b0 65 6e 74 5d 0a 2a 2a 20 69 73 20 64 65 73 74 72  ent].** is destr
237c0 6f 79 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  oyed by [sqlite3
237d0 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20  _finalize()] or 
237e0 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20 63  until the next c
237f0 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  all to.** sqlite
23800 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20  3_column_name() 
23810 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  or sqlite3_colum
23820 6e 5f 6e 61 6d 65 31 36 28 29 20 6f 6e 20 74 68  n_name16() on th
23830 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 2e 0a 2a  e same column..*
23840 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f  *.** If sqlite3_
23850 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 64  malloc() fails d
23860 75 72 69 6e 67 20 74 68 65 20 70 72 6f 63 65 73  uring the proces
23870 73 69 6e 67 20 6f 66 20 65 69 74 68 65 72 20 72  sing of either r
23880 6f 75 74 69 6e 65 0a 2a 2a 20 28 66 6f 72 20 65  outine.** (for e
23890 78 61 6d 70 6c 65 20 64 75 72 69 6e 67 20 61 20  xample during a 
238a0 63 6f 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20  conversion from 
238b0 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31 36 29  UTF-8 to UTF-16)
238c0 20 74 68 65 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20   then a.** NULL 
238d0 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72  pointer is retur
238e0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  ned..**.** The n
238f0 61 6d 65 20 6f 66 20 61 20 72 65 73 75 6c 74 20  ame of a result 
23900 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 76 61  column is the va
23910 6c 75 65 20 6f 66 20 74 68 65 20 22 41 53 22 20  lue of the "AS" 
23920 63 6c 61 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68  clause for.** th
23930 61 74 20 63 6f 6c 75 6d 6e 2c 20 69 66 20 74 68  at column, if th
23940 65 72 65 20 69 73 20 61 6e 20 41 53 20 63 6c 61  ere is an AS cla
23950 75 73 65 2e 20 20 49 66 20 74 68 65 72 65 20 69  use.  If there i
23960 73 20 6e 6f 20 41 53 20 63 6c 61 75 73 65 0a 2a  s no AS clause.*
23970 2a 20 74 68 65 6e 20 74 68 65 20 6e 61 6d 65 20  * then the name 
23980 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73  of the column is
23990 20 75 6e 73 70 65 63 69 66 69 65 64 20 61 6e 64   unspecified and
239a0 20 6d 61 79 20 63 68 61 6e 67 65 20 66 72 6f 6d   may change from
239b0 0a 2a 2a 20 6f 6e 65 20 72 65 6c 65 61 73 65 20  .** one release 
239c0 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 74 68 65  of SQLite to the
239d0 20 6e 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71   next..**.** Req
239e0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
239f0 31 33 37 32 31 5d 20 5b 48 31 33 37 32 33 5d 20  13721] [H13723] 
23a00 5b 48 31 33 37 32 34 5d 20 5b 48 31 33 37 32 35  [H13724] [H13725
23a10 5d 20 5b 48 31 33 37 32 36 5d 20 5b 48 31 33 37  ] [H13726] [H137
23a20 32 37 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  27].*/.SQLITE_AP
23a30 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
23a40 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
23a50 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
23a60 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f   int N);.SQLITE_
23a70 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
23a80 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
23a90 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74  ame16(sqlite3_st
23aa0 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 0a 2f 2a  mt*, int N);../*
23ab0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 6f  .** CAPI3REF: So
23ac0 75 72 63 65 20 4f 66 20 44 61 74 61 20 49 6e 20  urce Of Data In 
23ad0 41 20 51 75 65 72 79 20 52 65 73 75 6c 74 20 7b  A Query Result {
23ae0 48 31 33 37 34 30 7d 20 3c 53 31 30 37 30 30 3e  H13740} <S10700>
23af0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
23b00 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20 61 20  tines provide a 
23b10 6d 65 61 6e 73 20 74 6f 20 64 65 74 65 72 6d 69  means to determi
23b20 6e 65 20 77 68 61 74 20 63 6f 6c 75 6d 6e 20 6f  ne what column o
23b30 66 20 77 68 61 74 0a 2a 2a 20 74 61 62 6c 65 20  f what.** table 
23b40 69 6e 20 77 68 69 63 68 20 64 61 74 61 62 61 73  in which databas
23b50 65 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 20  e a result of a 
23b60 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d 65  [SELECT] stateme
23b70 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 2e 0a 2a  nt comes from..*
23b80 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
23b90 65 20 64 61 74 61 62 61 73 65 20 6f 72 20 74 61  e database or ta
23ba0 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 63 61  ble or column ca
23bb0 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20 61 73  n be returned as
23bc0 0a 2a 2a 20 65 69 74 68 65 72 20 61 20 55 54 46  .** either a UTF
23bd0 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 73 74 72  -8 or UTF-16 str
23be0 69 6e 67 2e 20 20 54 68 65 20 5f 64 61 74 61 62  ing.  The _datab
23bf0 61 73 65 5f 20 72 6f 75 74 69 6e 65 73 20 72 65  ase_ routines re
23c00 74 75 72 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61  turn.** the data
23c10 62 61 73 65 20 6e 61 6d 65 2c 20 74 68 65 20 5f  base name, the _
23c20 74 61 62 6c 65 5f 20 72 6f 75 74 69 6e 65 73 20  table_ routines 
23c30 72 65 74 75 72 6e 20 74 68 65 20 74 61 62 6c 65  return the table
23c40 20 6e 61 6d 65 2c 20 61 6e 64 0a 2a 2a 20 74 68   name, and.** th
23c50 65 20 6f 72 69 67 69 6e 5f 20 72 6f 75 74 69 6e  e origin_ routin
23c60 65 73 20 72 65 74 75 72 6e 20 74 68 65 20 63 6f  es return the co
23c70 6c 75 6d 6e 20 6e 61 6d 65 2e 0a 2a 2a 20 54 68  lumn name..** Th
23c80 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
23c90 67 20 69 73 20 76 61 6c 69 64 20 75 6e 74 69 6c  g is valid until
23ca0 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73   the [prepared s
23cb0 74 61 74 65 6d 65 6e 74 5d 20 69 73 20 64 65 73  tatement] is des
23cc0 74 72 6f 79 65 64 0a 2a 2a 20 75 73 69 6e 67 20  troyed.** using 
23cd0 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
23ce0 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68  e()] or until th
23cf0 65 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69  e same informati
23d00 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 0a  on is requested.
23d10 2a 2a 20 61 67 61 69 6e 20 69 6e 20 61 20 64 69  ** again in a di
23d20 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67  fferent encoding
23d30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65  ..**.** The name
23d40 73 20 72 65 74 75 72 6e 65 64 20 61 72 65 20 74  s returned are t
23d50 68 65 20 6f 72 69 67 69 6e 61 6c 20 75 6e 2d 61  he original un-a
23d60 6c 69 61 73 65 64 20 6e 61 6d 65 73 20 6f 66 20  liased names of 
23d70 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2c  the.** database,
23d80 20 74 61 62 6c 65 2c 20 61 6e 64 20 63 6f 6c 75   table, and colu
23d90 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  mn..**.** The fi
23da0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
23db0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61  the following ca
23dc0 6c 6c 73 20 69 73 20 61 20 5b 70 72 65 70 61 72  lls is a [prepar
23dd0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a  ed statement]..*
23de0 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  * These function
23df0 73 20 72 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61  s return informa
23e00 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4e  tion about the N
23e10 74 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72 6e  th column return
23e20 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73 74 61  ed by.** the sta
23e30 74 65 6d 65 6e 74 2c 20 77 68 65 72 65 20 4e 20  tement, where N 
23e40 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 66 75  is the second fu
23e50 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 2e  nction argument.
23e60 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4e 74  .**.** If the Nt
23e70 68 20 63 6f 6c 75 6d 6e 20 72 65 74 75 72 6e 65  h column returne
23e80 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65  d by the stateme
23e90 6e 74 20 69 73 20 61 6e 20 65 78 70 72 65 73 73  nt is an express
23ea0 69 6f 6e 20 6f 72 0a 2a 2a 20 73 75 62 71 75 65  ion or.** subque
23eb0 72 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 61 20  ry and is not a 
23ec0 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 74 68  column value, th
23ed0 65 6e 20 61 6c 6c 20 6f 66 20 74 68 65 73 65 20  en all of these 
23ee0 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e  functions return
23ef0 0a 2a 2a 20 4e 55 4c 4c 2e 20 20 54 68 65 73 65  .** NULL.  These
23f00 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 61   routine might a
23f10 6c 73 6f 20 72 65 74 75 72 6e 20 4e 55 4c 4c 20  lso return NULL 
23f20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  if a memory allo
23f30 63 61 74 69 6f 6e 20 65 72 72 6f 72 0a 2a 2a 20  cation error.** 
23f40 6f 63 63 75 72 73 2e 20 20 4f 74 68 65 72 77 69  occurs.  Otherwi
23f50 73 65 2c 20 74 68 65 79 20 72 65 74 75 72 6e 20  se, they return 
23f60 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
23f70 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
23f80 65 2c 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20  e, table.** and 
23f90 63 6f 6c 75 6d 6e 20 74 68 61 74 20 71 75 65 72  column that quer
23fa0 79 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20  y result column 
23fb0 77 61 73 20 65 78 74 72 61 63 74 65 64 20 66 72  was extracted fr
23fc0 6f 6d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20 77 69 74  om..**.** As wit
23fd0 68 20 61 6c 6c 20 6f 74 68 65 72 20 53 51 4c 69  h all other SQLi
23fe0 74 65 20 41 50 49 73 2c 20 74 68 6f 73 65 20 70  te APIs, those p
23ff0 6f 73 74 66 69 78 65 64 20 77 69 74 68 20 22 31  ostfixed with "1
24000 36 22 20 72 65 74 75 72 6e 0a 2a 2a 20 55 54 46  6" return.** UTF
24010 2d 31 36 20 65 6e 63 6f 64 65 64 20 73 74 72 69  -16 encoded stri
24020 6e 67 73 2c 20 74 68 65 20 6f 74 68 65 72 20 66  ngs, the other f
24030 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20  unctions return 
24040 55 54 46 2d 38 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a  UTF-8. {END}.**.
24050 2a 2a 20 54 68 65 73 65 20 41 50 49 73 20 61 72  ** These APIs ar
24060 65 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  e only available
24070 20 69 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   if the library 
24080 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  was compiled wit
24090 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45  h the.** [SQLITE
240a0 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
240b0 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65 70 72  ETADATA] C-prepr
240c0 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c 20 64  ocessor symbol d
240d0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 41  efined..**.** {A
240e0 31 33 37 35 31 7d 0a 2a 2a 20 49 66 20 74 77 6f  13751}.** If two
240f0 20 6f 72 20 6d 6f 72 65 20 74 68 72 65 61 64 73   or more threads
24100 20 63 61 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72   call one or mor
24110 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  e of these routi
24120 6e 65 73 20 61 67 61 69 6e 73 74 20 74 68 65 20  nes against the 
24130 73 61 6d 65 0a 2a 2a 20 70 72 65 70 61 72 65 64  same.** prepared
24140 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 63   statement and c
24150 6f 6c 75 6d 6e 20 61 74 20 74 68 65 20 73 61 6d  olumn at the sam
24160 65 20 74 69 6d 65 20 74 68 65 6e 20 74 68 65 20  e time then the 
24170 72 65 73 75 6c 74 73 20 61 72 65 0a 2a 2a 20 75  results are.** u
24180 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
24190 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
241a0 20 5b 48 31 33 37 34 31 5d 20 5b 48 31 33 37 34   [H13741] [H1374
241b0 32 5d 20 5b 48 31 33 37 34 33 5d 20 5b 48 31 33  2] [H13743] [H13
241c0 37 34 34 5d 20 5b 48 31 33 37 34 35 5d 20 5b 48  744] [H13745] [H
241d0 31 33 37 34 36 5d 20 5b 48 31 33 37 34 38 5d 0a  13746] [H13748].
241e0 2a 2a 0a 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20  **.** If two or 
241f0 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61 6c  more threads cal
24200 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  l one or more.**
24210 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
24220 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 20 7c  _database_name |
24230 20 63 6f 6c 75 6d 6e 20 6d 65 74 61 64 61 74 61   column metadata
24240 20 69 6e 74 65 72 66 61 63 65 73 5d 0a 2a 2a 20   interfaces].** 
24250 66 6f 72 20 74 68 65 20 73 61 6d 65 20 5b 70 72  for the same [pr
24260 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
24270 5d 20 61 6e 64 20 72 65 73 75 6c 74 20 63 6f 6c  ] and result col
24280 75 6d 6e 0a 2a 2a 20 61 74 20 74 68 65 20 73 61  umn.** at the sa
24290 6d 65 20 74 69 6d 65 20 74 68 65 6e 20 74 68 65  me time then the
242a0 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64   results are und
242b0 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
242c0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
242d0 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
242e0 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73  _database_name(s
242f0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
24300 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
24310 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
24320 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  3_column_databas
24330 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33  e_name16(sqlite3
24340 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c  _stmt*,int);.SQL
24350 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
24360 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar *sqlite3_colu
24370 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 28 73 71  mn_table_name(sq
24380 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29  lite3_stmt*,int)
24390 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
243a0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
243b0 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
243c0 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
243d0 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
243e0 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
243f0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
24400 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74  rigin_name(sqlit
24410 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53  e3_stmt*,int);.S
24420 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
24430 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
24440 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
24450 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  16(sqlite3_stmt*
24460 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ,int);../*.** CA
24470 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65 64  PI3REF: Declared
24480 20 44 61 74 61 74 79 70 65 20 4f 66 20 41 20 51   Datatype Of A Q
24490 75 65 72 79 20 52 65 73 75 6c 74 20 7b 48 31 33  uery Result {H13
244a0 37 36 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a  760} <S10700>.**
244b0 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
244c0 72 61 6d 65 74 65 72 20 69 73 20 61 20 5b 70 72  rameter is a [pr
244d0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
244e0 5d 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 73 74  ]..** If this st
244f0 61 74 65 6d 65 6e 74 20 69 73 20 61 20 5b 53 45  atement is a [SE
24500 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 20  LECT] statement 
24510 61 6e 64 20 74 68 65 20 4e 74 68 20 63 6f 6c 75  and the Nth colu
24520 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 74  mn of the.** ret
24530 75 72 6e 65 64 20 72 65 73 75 6c 74 20 73 65 74  urned result set
24540 20 6f 66 20 74 68 61 74 20 5b 53 45 4c 45 43 54   of that [SELECT
24550 5d 20 69 73 20 61 20 74 61 62 6c 65 20 63 6f 6c  ] is a table col
24560 75 6d 6e 20 28 6e 6f 74 20 61 6e 0a 2a 2a 20 65  umn (not an.** e
24570 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73 75 62  xpression or sub
24580 71 75 65 72 79 29 20 74 68 65 6e 20 74 68 65 20  query) then the 
24590 64 65 63 6c 61 72 65 64 20 74 79 70 65 20 6f 66  declared type of
245a0 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 63 6f   the table.** co
245b0 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64  lumn is returned
245c0 2e 20 20 49 66 20 74 68 65 20 4e 74 68 20 63 6f  .  If the Nth co
245d0 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75  lumn of the resu
245e0 6c 74 20 73 65 74 20 69 73 20 61 6e 0a 2a 2a 20  lt set is an.** 
245f0 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 73 75  expression or su
24600 62 71 75 65 72 79 2c 20 74 68 65 6e 20 61 20 4e  bquery, then a N
24610 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20 72  ULL pointer is r
24620 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20  eturned..** The 
24630 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
24640 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 20  is always UTF-8 
24650 65 6e 63 6f 64 65 64 2e 20 7b 45 4e 44 7d 0a 2a  encoded. {END}.*
24660 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65  *.** For example
24670 2c 20 67 69 76 65 6e 20 74 68 65 20 64 61 74 61  , given the data
24680 62 61 73 65 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a  base schema:.**.
24690 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
246a0 74 31 28 63 31 20 56 41 52 49 41 4e 54 29 3b 0a  t1(c1 VARIANT);.
246b0 2a 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 66 6f  **.** and the fo
246c0 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e  llowing statemen
246d0 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64  t to be compiled
246e0 3a 0a 2a 2a 0a 2a 2a 20 53 45 4c 45 43 54 20 63  :.**.** SELECT c
246f0 31 20 2b 20 31 2c 20 63 31 20 46 52 4f 4d 20 74  1 + 1, c1 FROM t
24700 31 3b 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 72 6f  1;.**.** this ro
24710 75 74 69 6e 65 20 77 6f 75 6c 64 20 72 65 74 75  utine would retu
24720 72 6e 20 74 68 65 20 73 74 72 69 6e 67 20 22 56  rn the string "V
24730 41 52 49 41 4e 54 22 20 66 6f 72 20 74 68 65 20  ARIANT" for the 
24740 73 65 63 6f 6e 64 20 72 65 73 75 6c 74 0a 2a 2a  second result.**
24750 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 31 29 2c 20   column (i==1), 
24760 61 6e 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  and a NULL point
24770 65 72 20 66 6f 72 20 74 68 65 20 66 69 72 73 74  er for the first
24780 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 28   result column (
24790 69 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  i==0)..**.** SQL
247a0 69 74 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63  ite uses dynamic
247b0 20 72 75 6e 2d 74 69 6d 65 20 74 79 70 69 6e 67   run-time typing
247c0 2e 20 20 53 6f 20 6a 75 73 74 20 62 65 63 61 75  .  So just becau
247d0 73 65 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69  se a column.** i
247e0 73 20 64 65 63 6c 61 72 65 64 20 74 6f 20 63 6f  s declared to co
247f0 6e 74 61 69 6e 20 61 20 70 61 72 74 69 63 75 6c  ntain a particul
24800 61 72 20 74 79 70 65 20 64 6f 65 73 20 6e 6f 74  ar type does not
24810 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 0a 2a   mean that the.*
24820 2a 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e  * data stored in
24830 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20   that column is 
24840 6f 66 20 74 68 65 20 64 65 63 6c 61 72 65 64 20  of the declared 
24850 74 79 70 65 2e 20 20 53 51 4c 69 74 65 20 69 73  type.  SQLite is
24860 0a 2a 2a 20 73 74 72 6f 6e 67 6c 79 20 74 79 70  .** strongly typ
24870 65 64 2c 20 62 75 74 20 74 68 65 20 74 79 70 69  ed, but the typi
24880 6e 67 20 69 73 20 64 79 6e 61 6d 69 63 20 6e 6f  ng is dynamic no
24890 74 20 73 74 61 74 69 63 2e 20 20 54 79 70 65 0a  t static.  Type.
248a0 2a 2a 20 69 73 20 61 73 73 6f 63 69 61 74 65 64  ** is associated
248b0 20 77 69 74 68 20 69 6e 64 69 76 69 64 75 61 6c   with individual
248c0 20 76 61 6c 75 65 73 2c 20 6e 6f 74 20 77 69 74   values, not wit
248d0 68 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 73  h the containers
248e0 0a 2a 2a 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  .** used to hold
248f0 20 74 68 6f 73 65 20 76 61 6c 75 65 73 2e 0a 2a   those values..*
24900 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
24910 73 3a 0a 2a 2a 20 5b 48 31 33 37 36 31 5d 20 5b  s:.** [H13761] [
24920 48 31 33 37 36 32 5d 20 5b 48 31 33 37 36 33 5d  H13762] [H13763]
24930 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
24940 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
24950 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
24960 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  pe(sqlite3_stmt*
24970 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ,int);.SQLITE_AP
24980 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
24990 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63  lite3_column_dec
249a0 6c 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f  ltype16(sqlite3_
249b0 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a  stmt*,int);../*.
249c0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 76 61  ** CAPI3REF: Eva
249d0 6c 75 61 74 65 20 41 6e 20 53 51 4c 20 53 74 61  luate An SQL Sta
249e0 74 65 6d 65 6e 74 20 7b 48 31 33 32 30 30 7d 20  tement {H13200} 
249f0 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 41  <S10000>.**.** A
24a00 66 74 65 72 20 61 20 5b 70 72 65 70 61 72 65 64  fter a [prepared
24a10 20 73 74 61 74 65 6d 65 6e 74 5d 20 68 61 73 20   statement] has 
24a20 62 65 65 6e 20 70 72 65 70 61 72 65 64 20 75 73  been prepared us
24a30 69 6e 67 20 65 69 74 68 65 72 0a 2a 2a 20 5b 73  ing either.** [s
24a40 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
24a50 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  2()] or [sqlite3
24a60 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d  _prepare16_v2()]
24a70 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   or one of the l
24a80 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61  egacy.** interfa
24a90 63 65 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  ces [sqlite3_pre
24aa0 70 61 72 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69  pare()] or [sqli
24ab0 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d  te3_prepare16()]
24ac0 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  , this function.
24ad0 2a 2a 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  ** must be calle
24ae0 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69  d one or more ti
24af0 6d 65 73 20 74 6f 20 65 76 61 6c 75 61 74 65 20  mes to evaluate 
24b00 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  the statement..*
24b10 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73  *.** The details
24b20 20 6f 66 20 74 68 65 20 62 65 68 61 76 69 6f 72   of the behavior
24b30 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
24b40 73 74 65 70 28 29 20 69 6e 74 65 72 66 61 63 65  step() interface
24b50 20 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 77 68   depend.** on wh
24b60 65 74 68 65 72 20 74 68 65 20 73 74 61 74 65 6d  ether the statem
24b70 65 6e 74 20 77 61 73 20 70 72 65 70 61 72 65 64  ent was prepared
24b80 20 75 73 69 6e 67 20 74 68 65 20 6e 65 77 65 72   using the newer
24b90 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 0a   "v2" interface.
24ba0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
24bb0 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73  are_v2()] and [s
24bc0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
24bd0 5f 76 32 28 29 5d 20 6f 72 20 74 68 65 20 6f 6c  _v2()] or the ol
24be0 64 65 72 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e  der legacy.** in
24bf0 74 65 72 66 61 63 65 20 5b 73 71 6c 69 74 65 33  terface [sqlite3
24c00 5f 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20  _prepare()] and 
24c10 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
24c20 31 36 28 29 5d 2e 20 20 54 68 65 20 75 73 65 20  16()].  The use 
24c30 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 22 76  of the.** new "v
24c40 32 22 20 69 6e 74 65 72 66 61 63 65 20 69 73 20  2" interface is 
24c50 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20  recommended for 
24c60 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  new applications
24c70 20 62 75 74 20 74 68 65 20 6c 65 67 61 63 79 0a   but the legacy.
24c80 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c  ** interface wil
24c90 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65  l continue to be
24ca0 20 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a   supported..**.*
24cb0 2a 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20  * In the legacy 
24cc0 69 6e 74 65 72 66 61 63 65 2c 20 74 68 65 20 72  interface, the r
24cd0 65 74 75 72 6e 20 76 61 6c 75 65 20 77 69 6c 6c  eturn value will
24ce0 20 62 65 20 65 69 74 68 65 72 20 5b 53 51 4c 49   be either [SQLI
24cf0 54 45 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b 53 51  TE_BUSY],.** [SQ
24d00 4c 49 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c  LITE_DONE], [SQL
24d10 49 54 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c 49 54  ITE_ROW], [SQLIT
24d20 45 5f 45 52 52 4f 52 5d 2c 20 6f 72 20 5b 53 51  E_ERROR], or [SQ
24d30 4c 49 54 45 5f 4d 49 53 55 53 45 5d 2e 0a 2a 2a  LITE_MISUSE]..**
24d40 20 57 69 74 68 20 74 68 65 20 22 76 32 22 20 69   With the "v2" i
24d50 6e 74 65 72 66 61 63 65 2c 20 61 6e 79 20 6f 66  nterface, any of
24d60 20 74 68 65 20 6f 74 68 65 72 20 5b 72 65 73 75   the other [resu
24d70 6c 74 20 63 6f 64 65 73 5d 20 6f 72 0a 2a 2a 20  lt codes] or.** 
24d80 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74  [extended result
24d90 20 63 6f 64 65 73 5d 20 6d 69 67 68 74 20 62 65   codes] might be
24da0 20 72 65 74 75 72 6e 65 64 20 61 73 20 77 65 6c   returned as wel
24db0 6c 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45  l..**.** [SQLITE
24dc0 5f 42 55 53 59 5d 20 6d 65 61 6e 73 20 74 68 61  _BUSY] means tha
24dd0 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65  t the database e
24de0 6e 67 69 6e 65 20 77 61 73 20 75 6e 61 62 6c 65  ngine was unable
24df0 20 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 0a   to acquire the.
24e00 2a 2a 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b  ** database lock
24e10 73 20 69 74 20 6e 65 65 64 73 20 74 6f 20 64 6f  s it needs to do
24e20 20 69 74 73 20 6a 6f 62 2e 20 20 49 66 20 74 68   its job.  If th
24e30 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61  e statement is a
24e40 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20   [COMMIT].** or 
24e50 6f 63 63 75 72 73 20 6f 75 74 73 69 64 65 20 6f  occurs outside o
24e60 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72  f an explicit tr
24e70 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ansaction, then 
24e80 79 6f 75 20 63 61 6e 20 72 65 74 72 79 20 74 68  you can retry th
24e90 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20  e.** statement. 
24ea0 20 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e   If the statemen
24eb0 74 20 69 73 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d  t is not a [COMM
24ec0 49 54 5d 20 61 6e 64 20 6f 63 63 75 72 73 20 77  IT] and occurs w
24ed0 69 74 68 69 6e 20 61 0a 2a 2a 20 65 78 70 6c 69  ithin a.** expli
24ee0 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  cit transaction 
24ef0 74 68 65 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20  then you should 
24f00 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61  rollback the tra
24f10 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a  nsaction before.
24f20 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a  ** continuing..*
24f30 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e  *.** [SQLITE_DON
24f40 45 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  E] means that th
24f50 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  e statement has 
24f60 66 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69  finished executi
24f70 6e 67 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c  ng.** successful
24f80 6c 79 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65  ly.  sqlite3_ste
24f90 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  p() should not b
24fa0 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f  e called again o
24fb0 6e 20 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a  n this virtual.*
24fc0 2a 20 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75  * machine withou
24fd0 74 20 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20  t first calling 
24fe0 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
24ff0 5d 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 76  ] to reset the v
25000 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e  irtual.** machin
25010 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e  e back to its in
25020 69 74 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a  itial state..**.
25030 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74  ** If the SQL st
25040 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78  atement being ex
25050 65 63 75 74 65 64 20 72 65 74 75 72 6e 73 20 61  ecuted returns a
25060 6e 79 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53  ny data, then [S
25070 51 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73  QLITE_ROW].** is
25080 20 72 65 74 75 72 6e 65 64 20 65 61 63 68 20 74   returned each t
25090 69 6d 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66  ime a new row of
250a0 20 64 61 74 61 20 69 73 20 72 65 61 64 79 20 66   data is ready f
250b0 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79  or processing by
250c0 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20   the.** caller. 
250d0 54 68 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62  The values may b
250e0 65 20 61 63 63 65 73 73 65 64 20 75 73 69 6e 67  e accessed using
250f0 20 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63   the [column acc
25100 65 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a  ess functions]..
25110 2a 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  ** sqlite3_step(
25120 29 20 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69  ) is called agai
25130 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  n to retrieve th
25140 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61  e next row of da
25150 74 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54  ta..**.** [SQLIT
25160 45 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74  E_ERROR] means t
25170 68 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65  hat a run-time e
25180 72 72 6f 72 20 28 73 75 63 68 20 61 73 20 61 20  rror (such as a 
25190 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69  constraint.** vi
251a0 6f 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63  olation) has occ
251b0 75 72 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f  urred.  sqlite3_
251c0 73 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f  step() should no
251d0 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69  t be called agai
251e0 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20  n on.** the VM. 
251f0 4d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  More information
25200 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79   may be found by
25210 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65   calling [sqlite
25220 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20  3_errmsg()]..** 
25230 57 69 74 68 20 74 68 65 20 6c 65 67 61 63 79 20  With the legacy 
25240 69 6e 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72  interface, a mor
25250 65 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72  e specific error
25260 20 63 6f 64 65 20 28 66 6f 72 20 65 78 61 6d 70   code (for examp
25270 6c 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49  le,.** [SQLITE_I
25280 4e 54 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49  NTERRUPT], [SQLI
25290 54 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c  TE_SCHEMA], [SQL
252a0 49 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e  ITE_CORRUPT], an
252b0 64 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 63  d so forth).** c
252c0 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62  an be obtained b
252d0 79 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74  y calling [sqlit
252e0 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 74  e3_reset()] on t
252f0 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20  he.** [prepared 
25300 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 6e 20  statement].  In 
25310 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61  the "v2" interfa
25320 63 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20  ce,.** the more 
25330 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 63  specific error c
25340 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
25350 64 69 72 65 63 74 6c 79 20 62 79 20 73 71 6c 69  directly by sqli
25360 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a 0a 2a  te3_step()..**.*
25370 2a 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  * [SQLITE_MISUSE
25380 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  ] means that the
25390 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   this routine wa
253a0 73 20 63 61 6c 6c 65 64 20 69 6e 61 70 70 72 6f  s called inappro
253b0 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 65 72  priately..** Per
253c0 68 61 70 73 20 69 74 20 77 61 73 20 63 61 6c 6c  haps it was call
253d0 65 64 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65  ed on a [prepare
253e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61  d statement] tha
253f0 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79  t has.** already
25400 20 62 65 65 6e 20 5b 73 71 6c 69 74 65 33 5f 66   been [sqlite3_f
25410 69 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69  inalize | finali
25420 7a 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 20 74  zed] or on one t
25430 68 61 74 20 68 61 64 0a 2a 2a 20 70 72 65 76 69  hat had.** previ
25440 6f 75 73 6c 79 20 72 65 74 75 72 6e 65 64 20 5b  ously returned [
25450 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6f 72  SQLITE_ERROR] or
25460 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20   [SQLITE_DONE]. 
25470 20 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a 2a 20   Or it could.** 
25480 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
25490 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
254a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
254b0 20 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 74   being used by t
254c0 77 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 68  wo or.** more th
254d0 72 65 61 64 73 20 61 74 20 74 68 65 20 73 61 6d  reads at the sam
254e0 65 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 6d 65  e moment in time
254f0 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79  ..**.** <b>Goofy
25500 20 49 6e 74 65 72 66 61 63 65 20 41 6c 65 72 74   Interface Alert
25510 3a 3c 2f 62 3e 20 49 6e 20 74 68 65 20 6c 65 67  :</b> In the leg
25520 61 63 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74  acy interface, t
25530 68 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  he sqlite3_step(
25540 29 0a 2a 2a 20 41 50 49 20 61 6c 77 61 79 73 20  ).** API always 
25550 72 65 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69  returns a generi
25560 63 20 65 72 72 6f 72 20 63 6f 64 65 2c 20 5b 53  c error code, [S
25570 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66 6f  QLITE_ERROR], fo
25580 6c 6c 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a 20 65  llowing any.** e
25590 72 72 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20  rror other than 
255a0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 61 6e  [SQLITE_BUSY] an
255b0 64 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  d [SQLITE_MISUSE
255c0 5d 2e 20 20 59 6f 75 20 6d 75 73 74 20 63 61 6c  ].  You must cal
255d0 6c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65  l.** [sqlite3_re
255e0 73 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  set()] or [sqlit
255f0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69  e3_finalize()] i
25600 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20  n order to find 
25610 6f 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 70  one of the.** sp
25620 65 63 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f  ecific [error co
25630 64 65 73 5d 20 74 68 61 74 20 62 65 74 74 65 72  des] that better
25640 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65   describes the e
25650 72 72 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 6d 69  rror..** We admi
25660 74 20 74 68 61 74 20 74 68 69 73 20 69 73 20 61  t that this is a
25670 20 67 6f 6f 66 79 20 64 65 73 69 67 6e 2e 20 20   goofy design.  
25680 54 68 65 20 70 72 6f 62 6c 65 6d 20 68 61 73 20  The problem has 
25690 62 65 65 6e 20 66 69 78 65 64 0a 2a 2a 20 77 69  been fixed.** wi
256a0 74 68 20 74 68 65 20 22 76 32 22 20 69 6e 74 65  th the "v2" inte
256b0 72 66 61 63 65 2e 20 20 49 66 20 79 6f 75 20 70  rface.  If you p
256c0 72 65 70 61 72 65 20 61 6c 6c 20 6f 66 20 79 6f  repare all of yo
256d0 75 72 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ur SQL statement
256e0 73 0a 2a 2a 20 75 73 69 6e 67 20 65 69 74 68 65  s.** using eithe
256f0 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  r [sqlite3_prepa
25700 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c  re_v2()] or [sql
25710 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
25720 32 28 29 5d 20 69 6e 73 74 65 61 64 0a 2a 2a 20  2()] instead.** 
25730 6f 66 20 74 68 65 20 6c 65 67 61 63 79 20 5b 73  of the legacy [s
25740 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
25750 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70  ] and [sqlite3_p
25760 72 65 70 61 72 65 31 36 28 29 5d 20 69 6e 74 65  repare16()] inte
25770 72 66 61 63 65 73 2c 0a 2a 2a 20 74 68 65 6e 20  rfaces,.** then 
25780 74 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69  the more specifi
25790 63 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20  c [error codes] 
257a0 61 72 65 20 72 65 74 75 72 6e 65 64 20 64 69 72  are returned dir
257b0 65 63 74 6c 79 0a 2a 2a 20 62 79 20 73 71 6c 69  ectly.** by sqli
257c0 74 65 33 5f 73 74 65 70 28 29 2e 20 20 54 68 65  te3_step().  The
257d0 20 75 73 65 20 6f 66 20 74 68 65 20 22 76 32 22   use of the "v2"
257e0 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65   interface is re
257f0 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a  commended..**.**
25800 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
25810 2a 20 5b 48 31 33 32 30 32 5d 20 5b 48 31 35 33  * [H13202] [H153
25820 30 34 5d 20 5b 48 31 35 33 30 36 5d 20 5b 48 31  04] [H15306] [H1
25830 35 33 30 38 5d 20 5b 48 31 35 33 31 30 5d 0a 2a  5308] [H15310].*
25840 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
25850 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71   sqlite3_step(sq
25860 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f  lite3_stmt*);../
25870 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e  *.** CAPI3REF: N
25880 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
25890 20 69 6e 20 61 20 72 65 73 75 6c 74 20 73 65 74   in a result set
258a0 20 7b 48 31 33 37 37 30 7d 20 3c 53 31 30 37 30   {H13770} <S1070
258b0 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73  0>.**.** Returns
258c0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76   the number of v
258d0 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63 75 72  alues in the cur
258e0 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20  rent row of the 
258f0 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a  result set..**.*
25900 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
25910 2a 2a 20 5b 48 31 33 37 37 31 5d 20 5b 48 31 33  ** [H13771] [H13
25920 37 37 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  772].*/.SQLITE_A
25930 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
25940 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ata_count(sqlite
25950 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a  3_stmt *pStmt);.
25960 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
25970 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74   Fundamental Dat
25980 61 74 79 70 65 73 20 7b 48 31 30 32 36 35 7d 20  atypes {H10265} 
25990 3c 53 31 30 31 31 30 3e 3c 53 31 30 31 32 30 3e  <S10110><S10120>
259a0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51  .** KEYWORDS: SQ
259b0 4c 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a 2a 20  LITE_TEXT.**.** 
259c0 7b 48 31 30 32 36 36 7d 20 45 76 65 72 79 20 76  {H10266} Every v
259d0 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20 68  alue in SQLite h
259e0 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20 66  as one of five f
259f0 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61 74  undamental datat
25a00 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  ypes:.**.** <ul>
25a10 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20  .** <li> 64-bit 
25a20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a  signed integer.*
25a30 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49 45  * <li> 64-bit IE
25a40 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  EE floating poin
25a50 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e  t number.** <li>
25a60 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20   string.** <li> 
25a70 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c  BLOB.** <li> NUL
25a80 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d  L.** </ul> {END}
25a90 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  .**.** These con
25aa0 73 74 61 6e 74 73 20 61 72 65 20 63 6f 64 65 73  stants are codes
25ab0 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 6f   for each of tho
25ac0 73 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20  se types..**.** 
25ad0 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 53 51  Note that the SQ
25ae0 4c 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 74 61  LITE_TEXT consta
25af0 6e 74 20 77 61 73 20 61 6c 73 6f 20 75 73 65 64  nt was also used
25b00 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69   in SQLite versi
25b10 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f  on 2.** for a co
25b20 6d 70 6c 65 74 65 6c 79 20 64 69 66 66 65 72 65  mpletely differe
25b30 6e 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66  nt meaning.  Sof
25b40 74 77 61 72 65 20 74 68 61 74 20 6c 69 6e 6b 73  tware that links
25b50 20 61 67 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a   against both.**
25b60 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
25b70 32 20 61 6e 64 20 53 51 4c 69 74 65 20 76 65 72  2 and SQLite ver
25b80 73 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 75 73  sion 3 should us
25b90 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54 2c 20  e SQLITE3_TEXT, 
25ba0 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45  not.** SQLITE_TE
25bb0 58 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  XT..*/.#define S
25bc0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 20 31  QLITE_INTEGER  1
25bd0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
25be0 46 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 66 69  FLOAT    2.#defi
25bf0 6e 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20  ne SQLITE_BLOB  
25c00 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c     4.#define SQL
25c10 49 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23  ITE_NULL     5.#
25c20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58  ifdef SQLITE_TEX
25c30 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45  T.# undef SQLITE
25c40 5f 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 64 65  _TEXT.#else.# de
25c50 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 58 54  fine SQLITE_TEXT
25c60 20 20 20 20 20 33 0a 23 65 6e 64 69 66 0a 23 64       3.#endif.#d
25c70 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 54 45  efine SQLITE3_TE
25c80 58 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20  XT     3../*.** 
25c90 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74  CAPI3REF: Result
25ca0 20 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 20 51   Values From A Q
25cb0 75 65 72 79 20 7b 48 31 33 38 30 30 7d 20 3c 53  uery {H13800} <S
25cc0 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  10700>.** KEYWOR
25cd0 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 63 65  DS: {column acce
25ce0 73 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a  ss functions}.**
25cf0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
25d00 65 73 20 66 6f 72 6d 20 74 68 65 20 22 72 65 73  es form the "res
25d10 75 6c 74 20 73 65 74 20 71 75 65 72 79 22 20 69  ult set query" i
25d20 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
25d30 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72  These routines r
25d40 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  eturn informatio
25d50 6e 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65  n about a single
25d60 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63   column of the c
25d70 75 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 6c 74  urrent.** result
25d80 20 72 6f 77 20 6f 66 20 61 20 71 75 65 72 79 2e   row of a query.
25d90 20 20 49 6e 20 65 76 65 72 79 20 63 61 73 65 20    In every case 
25da0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
25db0 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a  nt is a pointer.
25dc0 2a 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 70 61  ** to the [prepa
25dd0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74  red statement] t
25de0 68 61 74 20 69 73 20 62 65 69 6e 67 20 65 76 61  hat is being eva
25df0 6c 75 61 74 65 64 20 28 74 68 65 20 5b 73 71 6c  luated (the [sql
25e00 69 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a 20 74  ite3_stmt*].** t
25e10 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64  hat was returned
25e20 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 70   from [sqlite3_p
25e30 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
25e40 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 72 69 61  one of its varia
25e50 6e 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20  nts).** and the 
25e60 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
25e70 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
25e80 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77  the column for w
25e90 68 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hich information
25ea0 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65  .** should be re
25eb0 74 75 72 6e 65 64 2e 20 20 54 68 65 20 6c 65 66  turned.  The lef
25ec0 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  tmost column of 
25ed0 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 68  the result set h
25ee0 61 73 20 74 68 65 20 69 6e 64 65 78 20 30 2e 0a  as the index 0..
25ef0 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** The number of
25f00 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
25f10 72 65 73 75 6c 74 20 63 61 6e 20 62 65 20 64 65  result can be de
25f20 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 0a 2a  termined using.*
25f30 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  * [sqlite3_colum
25f40 6e 5f 63 6f 75 6e 74 28 29 5d 2e 0a 2a 2a 0a 2a  n_count()]..**.*
25f50 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61  * If the SQL sta
25f60 74 65 6d 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20  tement does not 
25f70 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20  currently point 
25f80 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 2c 20  to a valid row, 
25f90 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 63 6f 6c  or if the.** col
25fa0 75 6d 6e 20 69 6e 64 65 78 20 69 73 20 6f 75 74  umn index is out
25fb0 20 6f 66 20 72 61 6e 67 65 2c 20 74 68 65 20 72   of range, the r
25fc0 65 73 75 6c 74 20 69 73 20 75 6e 64 65 66 69 6e  esult is undefin
25fd0 65 64 2e 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  ed..** These rou
25fe0 74 69 6e 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62  tines may only b
25ff0 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  e called when th
26000 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61  e most recent ca
26010 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  ll to.** [sqlite
26020 33 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65  3_step()] has re
26030 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 52  turned [SQLITE_R
26040 4f 57 5d 20 61 6e 64 20 6e 65 69 74 68 65 72 0a  OW] and neither.
26050 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  ** [sqlite3_rese
26060 74 28 29 5d 20 6e 6f 72 20 5b 73 71 6c 69 74 65  t()] nor [sqlite
26070 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 68 61  3_finalize()] ha
26080 76 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 73  ve been called s
26090 75 62 73 65 71 75 65 6e 74 6c 79 2e 0a 2a 2a 20  ubsequently..** 
260a0 49 66 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20  If any of these 
260b0 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c  routines are cal
260c0 6c 65 64 20 61 66 74 65 72 20 5b 73 71 6c 69 74  led after [sqlit
260d0 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a  e3_reset()] or.*
260e0 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  * [sqlite3_final
260f0 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20  ize()] or after 
26100 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
26110 20 68 61 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a   has returned.**
26120 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72   something other
26130 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 52 4f   than [SQLITE_RO
26140 57 5d 2c 20 74 68 65 20 72 65 73 75 6c 74 73 20  W], the results 
26150 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a  are undefined..*
26160 2a 20 49 66 20 5b 73 71 6c 69 74 65 33 5f 73 74  * If [sqlite3_st
26170 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  ep()] or [sqlite
26180 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 20 5b 73  3_reset()] or [s
26190 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
261a0 29 5d 0a 2a 2a 20 61 72 65 20 63 61 6c 6c 65 64  )].** are called
261b0 20 66 72 6f 6d 20 61 20 64 69 66 66 65 72 65 6e   from a differen
261c0 74 20 74 68 72 65 61 64 20 77 68 69 6c 65 20 61  t thread while a
261d0 6e 79 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74  ny of these rout
261e0 69 6e 65 73 0a 2a 2a 20 61 72 65 20 70 65 6e 64  ines.** are pend
261f0 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ing, then the re
26200 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69  sults are undefi
26210 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ned..**.** The s
26220 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
26230 70 65 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74  pe() routine ret
26240 75 72 6e 73 20 74 68 65 0a 2a 2a 20 5b 53 51 4c  urns the.** [SQL
26250 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61  ITE_INTEGER | da
26260 74 61 74 79 70 65 20 63 6f 64 65 5d 20 66 6f 72  tatype code] for
26270 20 74 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74   the initial dat
26280 61 20 74 79 70 65 0a 2a 2a 20 6f 66 20 74 68 65  a type.** of the
26290 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 2e 20   result column. 
262a0 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61   The returned va
262b0 6c 75 65 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53  lue is one of [S
262c0 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c 0a  QLITE_INTEGER],.
262d0 2a 2a 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ** [SQLITE_FLOAT
262e0 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58 54 5d  ], [SQLITE_TEXT]
262f0 2c 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 2c  , [SQLITE_BLOB],
26300 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c   or [SQLITE_NULL
26310 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a  ].  The value.**
26320 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
26330 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
26340 28 29 20 69 73 20 6f 6e 6c 79 20 6d 65 61 6e 69  () is only meani
26350 6e 67 66 75 6c 20 69 66 20 6e 6f 20 74 79 70 65  ngful if no type
26360 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  .** conversions 
26370 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 61 73  have occurred as
26380 20 64 65 73 63 72 69 62 65 64 20 62 65 6c 6f 77   described below
26390 2e 20 20 41 66 74 65 72 20 61 20 74 79 70 65 20  .  After a type 
263a0 63 6f 6e 76 65 72 73 69 6f 6e 2c 0a 2a 2a 20 74  conversion,.** t
263b0 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
263c0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c  d by sqlite3_col
263d0 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 75 6e  umn_type() is un
263e0 64 65 66 69 6e 65 64 2e 20 20 46 75 74 75 72 65  defined.  Future
263f0 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  .** versions of 
26400 53 51 4c 69 74 65 20 6d 61 79 20 63 68 61 6e 67  SQLite may chang
26410 65 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f  e the behavior o
26420 66 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  f sqlite3_column
26430 5f 74 79 70 65 28 29 0a 2a 2a 20 66 6f 6c 6c 6f  _type().** follo
26440 77 69 6e 67 20 61 20 74 79 70 65 20 63 6f 6e 76  wing a type conv
26450 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  ersion..**.** If
26460 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   the result is a
26470 20 42 4c 4f 42 20 6f 72 20 55 54 46 2d 38 20 73   BLOB or UTF-8 s
26480 74 72 69 6e 67 20 74 68 65 6e 20 74 68 65 20 73  tring then the s
26490 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
264a0 74 65 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  tes().** routine
264b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
264c0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
264d0 74 68 61 74 20 42 4c 4f 42 20 6f 72 20 73 74 72  that BLOB or str
264e0 69 6e 67 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72  ing..** If the r
264f0 65 73 75 6c 74 20 69 73 20 61 20 55 54 46 2d 31  esult is a UTF-1
26500 36 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 73  6 string, then s
26510 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
26520 74 65 73 28 29 20 63 6f 6e 76 65 72 74 73 0a 2a  tes() converts.*
26530 2a 20 74 68 65 20 73 74 72 69 6e 67 20 74 6f 20  * the string to 
26540 55 54 46 2d 38 20 61 6e 64 20 74 68 65 6e 20 72  UTF-8 and then r
26550 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
26560 72 20 6f 66 20 62 79 74 65 73 2e 0a 2a 2a 20 49  r of bytes..** I
26570 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  f the result is 
26580 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20  a numeric value 
26590 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c  then sqlite3_col
265a0 75 6d 6e 5f 62 79 74 65 73 28 29 20 75 73 65 73  umn_bytes() uses
265b0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70  .** [sqlite3_snp
265c0 72 69 6e 74 66 28 29 5d 20 74 6f 20 63 6f 6e 76  rintf()] to conv
265d0 65 72 74 20 74 68 61 74 20 76 61 6c 75 65 20 74  ert that value t
265e0 6f 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e 67  o a UTF-8 string
265f0 20 61 6e 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20   and returns.** 
26600 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
26610 74 65 73 20 69 6e 20 74 68 61 74 20 73 74 72 69  tes in that stri
26620 6e 67 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  ng..** The value
26630 20 72 65 74 75 72 6e 65 64 20 64 6f 65 73 20 6e   returned does n
26640 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 7a  ot include the z
26650 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 61  ero terminator a
26660 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20  t the end.** of 
26670 74 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f 72  the string.  For
26680 20 63 6c 61 72 69 74 79 3a 20 74 68 65 20 76 61   clarity: the va
26690 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20  lue returned is 
266a0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
266b0 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 73 74   bytes in the st
266c0 72 69 6e 67 2c 20 6e 6f 74 20 74 68 65 20 6e 75  ring, not the nu
266d0 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
266e0 72 73 2e 0a 2a 2a 0a 2a 2a 20 53 74 72 69 6e 67  rs..**.** String
266f0 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  s returned by sq
26700 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
26710 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  t() and sqlite3_
26720 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c  column_text16(),
26730 0a 2a 2a 20 65 76 65 6e 20 65 6d 70 74 79 20 73  .** even empty s
26740 74 72 69 6e 67 73 2c 20 61 72 65 20 61 6c 77 61  trings, are alwa
26750 79 73 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  ys zero terminat
26760 65 64 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a  ed.  The return.
26770 2a 2a 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 71  ** value from sq
26780 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
26790 62 28 29 20 66 6f 72 20 61 20 7a 65 72 6f 2d 6c  b() for a zero-l
267a0 65 6e 67 74 68 20 42 4c 4f 42 20 69 73 20 61 6e  ength BLOB is an
267b0 20 61 72 62 69 74 72 61 72 79 0a 2a 2a 20 70 6f   arbitrary.** po
267c0 69 6e 74 65 72 2c 20 70 6f 73 73 69 62 6c 79 20  inter, possibly 
267d0 65 76 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  even a NULL poin
267e0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ter..**.** The s
267f0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
26800 74 65 73 31 36 28 29 20 72 6f 75 74 69 6e 65 20  tes16() routine 
26810 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 73 71  is similar to sq
26820 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
26830 65 73 28 29 0a 2a 2a 20 62 75 74 20 6c 65 61 76  es().** but leav
26840 65 73 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  es the result in
26850 20 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76   UTF-16 in nativ
26860 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 6e 73  e byte order ins
26870 74 65 61 64 20 6f 66 20 55 54 46 2d 38 2e 0a 2a  tead of UTF-8..*
26880 2a 20 54 68 65 20 7a 65 72 6f 20 74 65 72 6d 69  * The zero termi
26890 6e 61 74 6f 72 20 69 73 20 6e 6f 74 20 69 6e 63  nator is not inc
268a0 6c 75 64 65 64 20 69 6e 20 74 68 69 73 20 63 6f  luded in this co
268b0 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f  unt..**.** The o
268c0 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62  bject returned b
268d0 79 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  y [sqlite3_colum
268e0 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73 20 61 6e  n_value()] is an
268f0 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64  .** [unprotected
26900 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20   sqlite3_value] 
26910 6f 62 6a 65 63 74 2e 20 20 41 6e 20 75 6e 70 72  object.  An unpr
26920 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
26930 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2a 20  value object.** 
26940 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64  may only be used
26950 20 77 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 62   with [sqlite3_b
26960 69 6e 64 5f 76 61 6c 75 65 28 29 5d 20 61 6e 64  ind_value()] and
26970 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
26980 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 49 66  _value()]..** If
26990 20 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65   the [unprotecte
269a0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
269b0 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64   object returned
269c0 20 62 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   by.** [sqlite3_
269d0 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20  column_value()] 
269e0 69 73 20 75 73 65 64 20 69 6e 20 61 6e 79 20 6f  is used in any o
269f0 74 68 65 72 20 77 61 79 2c 20 69 6e 63 6c 75 64  ther way, includ
26a00 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20  ing calls.** to 
26a10 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73  routines like [s
26a20 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
26a30 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61  ()], [sqlite3_va
26a40 6c 75 65 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20  lue_text()],.** 
26a50 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  or [sqlite3_valu
26a60 65 5f 62 79 74 65 73 28 29 5d 2c 20 74 68 65 6e  e_bytes()], then
26a70 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73   the behavior is
26a80 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a   undefined..**.*
26a90 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
26aa0 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76   attempt to conv
26ab0 65 72 74 20 74 68 65 20 76 61 6c 75 65 20 77 68  ert the value wh
26ac0 65 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e  ere appropriate.
26ad0 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c 65    For.** example
26ae0 2c 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61  , if the interna
26af0 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  l representation
26b00 20 69 73 20 46 4c 4f 41 54 20 61 6e 64 20 61 20   is FLOAT and a 
26b10 74 65 78 74 20 72 65 73 75 6c 74 0a 2a 2a 20 69  text result.** i
26b20 73 20 72 65 71 75 65 73 74 65 64 2c 20 5b 73 71  s requested, [sq
26b30 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
26b40 5d 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e  ] is used intern
26b50 61 6c 6c 79 20 74 6f 20 70 65 72 66 6f 72 6d 20  ally to perform 
26b60 74 68 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f  the.** conversio
26b70 6e 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e  n automatically.
26b80 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
26b90 74 61 62 6c 65 20 64 65 74 61 69 6c 73 20 74 68  table details th
26ba0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 0a 2a 2a  e conversions.**
26bb0 20 74 68 61 74 20 61 72 65 20 61 70 70 6c 69 65   that are applie
26bc0 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71  d:.**.** <blockq
26bd0 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20  uote>.** <table 
26be0 62 6f 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c  border="1">.** <
26bf0 74 72 3e 3c 74 68 3e 20 49 6e 74 65 72 6e 61 6c  tr><th> Internal
26c00 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 52 65  <br>Type <th> Re
26c10 71 75 65 73 74 65 64 3c 62 72 3e 54 79 70 65 20  quested<br>Type 
26c20 3c 74 68 3e 20 20 43 6f 6e 76 65 72 73 69 6f 6e  <th>  Conversion
26c30 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  .**.** <tr><td> 
26c40 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 49 4e   NULL    <td> IN
26c50 54 45 47 45 52 20 20 20 3c 74 64 3e 20 52 65 73  TEGER   <td> Res
26c60 75 6c 74 20 69 73 20 30 0a 2a 2a 20 3c 74 72 3e  ult is 0.** <tr>
26c70 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74  <td>  NULL    <t
26c80 64 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64  d>  FLOAT    <td
26c90 3e 20 52 65 73 75 6c 74 20 69 73 20 30 2e 30 0a  > Result is 0.0.
26ca0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c  ** <tr><td>  NUL
26cb0 4c 20 20 20 20 3c 74 64 3e 20 20 20 54 45 58 54  L    <td>   TEXT
26cc0 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20      <td> Result 
26cd0 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a  is NULL pointer.
26ce0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c  ** <tr><td>  NUL
26cf0 4c 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42  L    <td>   BLOB
26d00 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74 20      <td> Result 
26d10 69 73 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a  is NULL pointer.
26d20 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45  ** <tr><td> INTE
26d30 47 45 52 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54  GER  <td>  FLOAT
26d40 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74      <td> Convert
26d50 20 66 72 6f 6d 20 69 6e 74 65 67 65 72 20 74 6f   from integer to
26d60 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74   float.** <tr><t
26d70 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e  d> INTEGER  <td>
26d80 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20     TEXT    <td> 
26d90 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20  ASCII rendering 
26da0 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  of the integer.*
26db0 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47  * <tr><td> INTEG
26dc0 45 52 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20  ER  <td>   BLOB 
26dd0 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20     <td> Same as 
26de0 49 4e 54 45 47 45 52 2d 3e 54 45 58 54 0a 2a 2a  INTEGER->TEXT.**
26df0 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54   <tr><td>  FLOAT
26e00 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20     <td> INTEGER 
26e10 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 66    <td> Convert f
26e20 72 6f 6d 20 66 6c 6f 61 74 20 74 6f 20 69 6e 74  rom float to int
26e30 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  eger.** <tr><td>
26e40 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20    FLOAT   <td>  
26e50 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 53   TEXT    <td> AS
26e60 43 49 49 20 72 65 6e 64 65 72 69 6e 67 20 6f 66  CII rendering of
26e70 20 74 68 65 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74   the float.** <t
26e80 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20  r><td>  FLOAT   
26e90 3c 74 64 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c  <td>   BLOB    <
26ea0 74 64 3e 20 53 61 6d 65 20 61 73 20 46 4c 4f 41  td> Same as FLOA
26eb0 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c  T->TEXT.** <tr><
26ec0 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64  td>  TEXT    <td
26ed0 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e  > INTEGER   <td>
26ee0 20 55 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c   Use atoi().** <
26ef0 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20  tr><td>  TEXT   
26f00 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
26f10 3c 74 64 3e 20 55 73 65 20 61 74 6f 66 28 29 0a  <td> Use atof().
26f20 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58  ** <tr><td>  TEX
26f30 54 20 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42  T    <td>   BLOB
26f40 20 20 20 20 3c 74 64 3e 20 4e 6f 20 63 68 61 6e      <td> No chan
26f50 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  ge.** <tr><td>  
26f60 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 49 4e 54  BLOB    <td> INT
26f70 45 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76  EGER   <td> Conv
26f80 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65 6e  ert to TEXT then
26f90 20 75 73 65 20 61 74 6f 69 28 29 0a 2a 2a 20 3c   use atoi().** <
26fa0 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20  tr><td>  BLOB   
26fb0 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 20   <td>  FLOAT    
26fc0 3c 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20  <td> Convert to 
26fd0 54 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74  TEXT then use at
26fe0 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e  of().** <tr><td>
26ff0 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20    BLOB    <td>  
27000 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 41 64   TEXT    <td> Ad
27010 64 20 61 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61  d a zero termina
27020 74 6f 72 20 69 66 20 6e 65 65 64 65 64 0a 2a 2a  tor if needed.**
27030 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20 3c 2f 62   </table>.** </b
27040 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
27050 20 54 68 65 20 74 61 62 6c 65 20 61 62 6f 76 65   The table above
27060 20 6d 61 6b 65 73 20 72 65 66 65 72 65 6e 63 65   makes reference
27070 20 74 6f 20 73 74 61 6e 64 61 72 64 20 43 20 6c   to standard C l
27080 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 73  ibrary functions
27090 20 61 74 6f 69 28 29 0a 2a 2a 20 61 6e 64 20 61   atoi().** and a
270a0 74 6f 66 28 29 2e 20 20 53 51 4c 69 74 65 20 64  tof().  SQLite d
270b0 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 75  oes not really u
270c0 73 65 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  se these functio
270d0 6e 73 2e 20 20 49 74 20 68 61 73 20 69 74 73 0a  ns.  It has its.
270e0 2a 2a 20 6f 77 6e 20 65 71 75 69 76 61 6c 65 6e  ** own equivalen
270f0 74 20 69 6e 74 65 72 6e 61 6c 20 72 6f 75 74 69  t internal routi
27100 6e 65 73 2e 20 20 54 68 65 20 61 74 6f 69 28 29  nes.  The atoi()
27110 20 61 6e 64 20 61 74 6f 66 28 29 20 6e 61 6d 65   and atof() name
27120 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 69 6e  s are.** used in
27130 20 74 68 65 20 74 61 62 6c 65 20 66 6f 72 20 62   the table for b
27140 72 65 76 69 74 79 20 61 6e 64 20 62 65 63 61 75  revity and becau
27150 73 65 20 74 68 65 79 20 61 72 65 20 66 61 6d 69  se they are fami
27160 6c 69 61 72 20 74 6f 20 6d 6f 73 74 0a 2a 2a 20  liar to most.** 
27170 43 20 70 72 6f 67 72 61 6d 6d 65 72 73 2e 0a 2a  C programmers..*
27180 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77  *.** Note that w
27190 68 65 6e 20 74 79 70 65 20 63 6f 6e 76 65 72 73  hen type convers
271a0 69 6f 6e 73 20 6f 63 63 75 72 2c 20 70 6f 69 6e  ions occur, poin
271b0 74 65 72 73 20 72 65 74 75 72 6e 65 64 20 62 79  ters returned by
271c0 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c 73 20   prior.** calls 
271d0 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  to sqlite3_colum
271e0 6e 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65  n_blob(), sqlite
271f0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c  3_column_text(),
27200 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73 71 6c 69 74   and/or.** sqlit
27210 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
27220 28 29 20 6d 61 79 20 62 65 20 69 6e 76 61 6c 69  () may be invali
27230 64 61 74 65 64 2e 0a 2a 2a 20 54 79 70 65 20 63  dated..** Type c
27240 6f 6e 76 65 72 73 69 6f 6e 73 20 61 6e 64 20 70  onversions and p
27250 6f 69 6e 74 65 72 20 69 6e 76 61 6c 69 64 61 74  ointer invalidat
27260 69 6f 6e 73 20 6d 69 67 68 74 20 6f 63 63 75 72  ions might occur
27270 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  .** in the follo
27280 77 69 6e 67 20 63 61 73 65 73 3a 0a 2a 2a 0a 2a  wing cases:.**.*
27290 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 54  * <ul>.** <li> T
272a0 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65  he initial conte
272b0 6e 74 20 69 73 20 61 20 42 4c 4f 42 20 61 6e 64  nt is a BLOB and
272c0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
272d0 74 65 78 74 28 29 20 6f 72 0a 2a 2a 20 20 20 20  text() or.**    
272e0 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
272f0 5f 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c  _text16() is cal
27300 6c 65 64 2e 20 20 41 20 7a 65 72 6f 2d 74 65 72  led.  A zero-ter
27310 6d 69 6e 61 74 6f 72 20 6d 69 67 68 74 0a 2a 2a  minator might.**
27320 20 20 20 20 20 20 6e 65 65 64 20 74 6f 20 62 65        need to be
27330 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 73 74   added to the st
27340 72 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c  ring.</li>.** <l
27350 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63  i> The initial c
27360 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 38 20  ontent is UTF-8 
27370 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65 33  text and sqlite3
27380 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28  _column_bytes16(
27390 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c  ) or.**      sql
273a0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
273b0 31 36 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 20  16() is called. 
273c0 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73   The content mus
273d0 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a  t be converted.*
273e0 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d 31 36  *      to UTF-16
273f0 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54  .</li>.** <li> T
27400 68 65 20 69 6e 69 74 69 61 6c 20 63 6f 6e 74 65  he initial conte
27410 6e 74 20 69 73 20 55 54 46 2d 31 36 20 74 65 78  nt is UTF-16 tex
27420 74 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f  t and sqlite3_co
27430 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a  lumn_bytes() or.
27440 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  **      sqlite3_
27450 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 69 73  column_text() is
27460 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f   called.  The co
27470 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f  ntent must be co
27480 6e 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20  nverted.**      
27490 74 6f 20 55 54 46 2d 38 2e 3c 2f 6c 69 3e 0a 2a  to UTF-8.</li>.*
274a0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 43 6f  * </ul>.**.** Co
274b0 6e 76 65 72 73 69 6f 6e 73 20 62 65 74 77 65 65  nversions betwee
274c0 6e 20 55 54 46 2d 31 36 62 65 20 61 6e 64 20 55  n UTF-16be and U
274d0 54 46 2d 31 36 6c 65 20 61 72 65 20 61 6c 77 61  TF-16le are alwa
274e0 79 73 20 64 6f 6e 65 20 69 6e 20 70 6c 61 63 65  ys done in place
274f0 20 61 6e 64 20 64 6f 0a 2a 2a 20 6e 6f 74 20 69   and do.** not i
27500 6e 76 61 6c 69 64 61 74 65 20 61 20 70 72 69 6f  nvalidate a prio
27510 72 20 70 6f 69 6e 74 65 72 2c 20 74 68 6f 75 67  r pointer, thoug
27520 68 20 6f 66 20 63 6f 75 72 73 65 20 74 68 65 20  h of course the 
27530 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 62  content of the b
27540 75 66 66 65 72 0a 2a 2a 20 74 68 61 74 20 74 68  uffer.** that th
27550 65 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 20  e prior pointer 
27560 70 6f 69 6e 74 73 20 74 6f 20 77 69 6c 6c 20 68  points to will h
27570 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  ave been modifie
27580 64 2e 20 20 4f 74 68 65 72 20 6b 69 6e 64 73 0a  d.  Other kinds.
27590 2a 2a 20 6f 66 20 63 6f 6e 76 65 72 73 69 6f 6e  ** of conversion
275a0 20 61 72 65 20 64 6f 6e 65 20 69 6e 20 70 6c 61   are done in pla
275b0 63 65 20 77 68 65 6e 20 69 74 20 69 73 20 70 6f  ce when it is po
275c0 73 73 69 62 6c 65 2c 20 62 75 74 20 73 6f 6d 65  ssible, but some
275d0 74 69 6d 65 73 20 74 68 65 79 0a 2a 2a 20 61 72  times they.** ar
275e0 65 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 61  e not possible a
275f0 6e 64 20 69 6e 20 74 68 6f 73 65 20 63 61 73 65  nd in those case
27600 73 20 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 73  s prior pointers
27610 20 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64   are invalidated
27620 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 66 65  ..**.** The safe
27630 73 74 20 61 6e 64 20 65 61 73 69 65 73 74 20 74  st and easiest t
27640 6f 20 72 65 6d 65 6d 62 65 72 20 70 6f 6c 69 63  o remember polic
27650 79 20 69 73 20 74 6f 20 69 6e 76 6f 6b 65 20 74  y is to invoke t
27660 68 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a  hese routines.**
27670 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66   in one of the f
27680 6f 6c 6c 6f 77 69 6e 67 20 77 61 79 73 3a 0a 2a  ollowing ways:.*
27690 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 20 3c 6c  *.** <ul>.**  <l
276a0 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  i>sqlite3_column
276b0 5f 74 65 78 74 28 29 20 66 6f 6c 6c 6f 77 65 64  _text() followed
276c0 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   by sqlite3_colu
276d0 6d 6e 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a  mn_bytes()</li>.
276e0 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f  **  <li>sqlite3_
276f0 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f  column_blob() fo
27700 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65  llowed by sqlite
27710 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
27720 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71  </li>.**  <li>sq
27730 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
27740 74 31 36 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62  t16() followed b
27750 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  y sqlite3_column
27760 5f 62 79 74 65 73 31 36 28 29 3c 2f 6c 69 3e 0a  _bytes16()</li>.
27770 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49  ** </ul>.**.** I
27780 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 79  n other words, y
27790 6f 75 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73  ou should call s
277a0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
277b0 78 74 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  xt(),.** sqlite3
277c0 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20  _column_blob(), 
277d0 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  or sqlite3_colum
277e0 6e 5f 74 65 78 74 31 36 28 29 20 66 69 72 73 74  n_text16() first
277f0 20 74 6f 20 66 6f 72 63 65 20 74 68 65 20 72 65   to force the re
27800 73 75 6c 74 0a 2a 2a 20 69 6e 74 6f 20 74 68 65  sult.** into the
27810 20 64 65 73 69 72 65 64 20 66 6f 72 6d 61 74 2c   desired format,
27820 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 73 71 6c   then invoke sql
27830 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
27840 73 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  s() or.** sqlite
27850 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
27860 28 29 20 74 6f 20 66 69 6e 64 20 74 68 65 20 73  () to find the s
27870 69 7a 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c  ize of the resul
27880 74 2e 20 20 44 6f 20 6e 6f 74 20 6d 69 78 20 63  t.  Do not mix c
27890 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  alls.** to sqlit
278a0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
278b0 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   or sqlite3_colu
278c0 6d 6e 5f 62 6c 6f 62 28 29 20 77 69 74 68 20 63  mn_blob() with c
278d0 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74  alls to.** sqlit
278e0 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
278f0 36 28 29 2c 20 61 6e 64 20 64 6f 20 6e 6f 74 20  6(), and do not 
27900 6d 69 78 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  mix calls to sql
27910 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
27920 31 36 28 29 0a 2a 2a 20 77 69 74 68 20 63 61 6c  16().** with cal
27930 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ls to sqlite3_co
27940 6c 75 6d 6e 5f 62 79 74 65 73 28 29 2e 0a 2a 2a  lumn_bytes()..**
27950 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73  .** The pointers
27960 20 72 65 74 75 72 6e 65 64 20 61 72 65 20 76 61   returned are va
27970 6c 69 64 20 75 6e 74 69 6c 20 61 20 74 79 70 65  lid until a type
27980 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75   conversion occu
27990 72 73 20 61 73 0a 2a 2a 20 64 65 73 63 72 69 62  rs as.** describ
279a0 65 64 20 61 62 6f 76 65 2c 20 6f 72 20 75 6e 74  ed above, or unt
279b0 69 6c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70  il [sqlite3_step
279c0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
279d0 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20 5b  reset()] or.** [
279e0 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
279f0 28 29 5d 20 69 73 20 63 61 6c 6c 65 64 2e 20 20  ()] is called.  
27a00 54 68 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65  The memory space
27a10 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 73 74   used to hold st
27a20 72 69 6e 67 73 0a 2a 2a 20 61 6e 64 20 42 4c 4f  rings.** and BLO
27a30 42 73 20 69 73 20 66 72 65 65 64 20 61 75 74 6f  Bs is freed auto
27a40 6d 61 74 69 63 61 6c 6c 79 2e 20 20 44 6f 20 3c  matically.  Do <
27a50 62 3e 6e 6f 74 3c 2f 62 3e 20 70 61 73 73 20 74  b>not</b> pass t
27a60 68 65 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75  he pointers retu
27a70 72 6e 65 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  rned.** [sqlite3
27a80 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 5d 2c  _column_blob()],
27a90 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
27aa0 5f 74 65 78 74 28 29 5d 2c 20 65 74 63 2e 20 69  _text()], etc. i
27ab0 6e 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  nto.** [sqlite3_
27ac0 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49  free()]..**.** I
27ad0 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
27ae0 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75  ation error occu
27af0 72 73 20 64 75 72 69 6e 67 20 74 68 65 20 65 76  rs during the ev
27b00 61 6c 75 61 74 69 6f 6e 20 6f 66 20 61 6e 79 0a  aluation of any.
27b10 2a 2a 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74  ** of these rout
27b20 69 6e 65 73 2c 20 61 20 64 65 66 61 75 6c 74 20  ines, a default 
27b30 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65  value is returne
27b40 64 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  d.  The default 
27b50 76 61 6c 75 65 0a 2a 2a 20 69 73 20 65 69 74 68  value.** is eith
27b60 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 20 30  er the integer 0
27b70 2c 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20 70  , the floating p
27b80 6f 69 6e 74 20 6e 75 6d 62 65 72 20 30 2e 30 2c  oint number 0.0,
27b90 20 6f 72 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f   or a NULL.** po
27ba0 69 6e 74 65 72 2e 20 20 53 75 62 73 65 71 75 65  inter.  Subseque
27bb0 6e 74 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c  nt calls to [sql
27bc0 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 20  ite3_errcode()] 
27bd0 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 5b  will return.** [
27be0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a  SQLITE_NOMEM]..*
27bf0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
27c00 73 3a 0a 2a 2a 20 5b 48 31 33 38 30 33 5d 20 5b  s:.** [H13803] [
27c10 48 31 33 38 30 36 5d 20 5b 48 31 33 38 30 39 5d  H13806] [H13809]
27c20 20 5b 48 31 33 38 31 32 5d 20 5b 48 31 33 38 31   [H13812] [H1381
27c30 35 5d 20 5b 48 31 33 38 31 38 5d 20 5b 48 31 33  5] [H13818] [H13
27c40 38 32 31 5d 20 5b 48 31 33 38 32 34 5d 0a 2a 2a  821] [H13824].**
27c50 20 5b 48 31 33 38 32 37 5d 20 5b 48 31 33 38 33   [H13827] [H1383
27c60 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  0].*/.SQLITE_API
27c70 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
27c80 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
27c90 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
27ca0 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54  int iCol);.SQLIT
27cb0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
27cc0 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73  3_column_bytes(s
27cd0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
27ce0 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f  t iCol);.SQLITE_
27cf0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
27d00 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 73  column_bytes16(s
27d10 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
27d20 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f  t iCol);.SQLITE_
27d30 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74  API double sqlit
27d40 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65  e3_column_double
27d50 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20  (sqlite3_stmt*, 
27d60 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54  int iCol);.SQLIT
27d70 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
27d80 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c  3_column_int(sql
27d90 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
27da0 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  iCol);.SQLITE_AP
27db0 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  I sqlite3_int64 
27dc0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
27dd0 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d  nt64(sqlite3_stm
27de0 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
27df0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
27e00 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73  unsigned char *s
27e10 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
27e20 78 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  xt(sqlite3_stmt*
27e30 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c  , int iCol);.SQL
27e40 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
27e50 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
27e60 6d 6e 5f 74 65 78 74 31 36 28 73 71 6c 69 74 65  mn_text16(sqlite
27e70 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
27e80 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
27e90 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
27ea0 6e 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73  n_type(sqlite3_s
27eb0 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
27ec0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
27ed0 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74  te3_value *sqlit
27ee0 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
27ef0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
27f00 6e 74 20 69 43 6f 6c 29 3b 0a 0a 2f 2a 0a 2a 2a  nt iCol);../*.**
27f10 20 43 41 50 49 33 52 45 46 3a 20 44 65 73 74 72   CAPI3REF: Destr
27f20 6f 79 20 41 20 50 72 65 70 61 72 65 64 20 53 74  oy A Prepared St
27f30 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b  atement Object {
27f40 48 31 33 33 30 30 7d 20 3c 53 37 30 33 30 30 3e  H13300} <S70300>
27f50 3c 53 33 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S30100>.**.** T
27f60 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  he sqlite3_final
27f70 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69  ize() function i
27f80 73 20 63 61 6c 6c 65 64 20 74 6f 20 64 65 6c 65  s called to dele
27f90 74 65 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  te a [prepared s
27fa0 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66  tatement]..** If
27fb0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 77   the statement w
27fc0 61 73 20 65 78 65 63 75 74 65 64 20 73 75 63 63  as executed succ
27fd0 65 73 73 66 75 6c 6c 79 20 6f 72 20 6e 6f 74 20  essfully or not 
27fe0 65 78 65 63 75 74 65 64 20 61 74 20 61 6c 6c 2c  executed at all,
27ff0 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f   then.** SQLITE_
28000 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  OK is returned. 
28010 49 66 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20  If execution of 
28020 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 66 61  the statement fa
28030 69 6c 65 64 20 74 68 65 6e 20 61 6e 0a 2a 2a 20  iled then an.** 
28040 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20  [error code] or 
28050 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20  [extended error 
28060 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e 65  code] is returne
28070 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
28080 75 74 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c  utine can be cal
28090 6c 65 64 20 61 74 20 61 6e 79 20 70 6f 69 6e 74  led at any point
280a0 20 64 75 72 69 6e 67 20 74 68 65 20 65 78 65 63   during the exec
280b0 75 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20  ution of the.** 
280c0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
280d0 65 6e 74 5d 2e 20 20 49 66 20 74 68 65 20 76 69  ent].  If the vi
280e0 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61  rtual machine ha
280f0 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 70 6c 65 74  s not.** complet
28100 65 64 20 65 78 65 63 75 74 69 6f 6e 20 77 68 65  ed execution whe
28110 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
28120 73 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 20 69  s called, that i
28130 73 20 6c 69 6b 65 0a 2a 2a 20 65 6e 63 6f 75 6e  s like.** encoun
28140 74 65 72 69 6e 67 20 61 6e 20 65 72 72 6f 72 20  tering an error 
28150 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69  or an [sqlite3_i
28160 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74 65 72  nterrupt | inter
28170 72 75 70 74 5d 2e 0a 2a 2a 20 49 6e 63 6f 6d 70  rupt]..** Incomp
28180 6c 65 74 65 20 75 70 64 61 74 65 73 20 6d 61 79  lete updates may
28190 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20   be rolled back 
281a0 61 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  and transactions
281b0 20 63 61 6e 63 65 6c 65 64 2c 0a 2a 2a 20 64 65   canceled,.** de
281c0 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 63  pending on the c
281d0 69 72 63 75 6d 73 74 61 6e 63 65 73 2c 20 61 6e  ircumstances, an
281e0 64 20 74 68 65 0a 2a 2a 20 5b 65 72 72 6f 72 20  d the.** [error 
281f0 63 6f 64 65 5d 20 72 65 74 75 72 6e 65 64 20 77  code] returned w
28200 69 6c 6c 20 62 65 20 5b 53 51 4c 49 54 45 5f 41  ill be [SQLITE_A
28210 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  BORT]..**.** Req
28220 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
28230 31 31 33 30 32 5d 20 5b 48 31 31 33 30 34 5d 0a  11302] [H11304].
28240 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
28250 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  t sqlite3_finali
28260 7a 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ze(sqlite3_stmt 
28270 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *pStmt);../*.** 
28280 43 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20  CAPI3REF: Reset 
28290 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65  A Prepared State
282a0 6d 65 6e 74 20 4f 62 6a 65 63 74 20 7b 48 31 33  ment Object {H13
282b0 33 33 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a 2a  330} <S70300>.**
282c0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
282d0 72 65 73 65 74 28 29 20 66 75 6e 63 74 69 6f 6e  reset() function
282e0 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 65   is called to re
282f0 73 65 74 20 61 20 5b 70 72 65 70 61 72 65 64 20  set a [prepared 
28300 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 6f 62  statement].** ob
28310 6a 65 63 74 20 62 61 63 6b 20 74 6f 20 69 74 73  ject back to its
28320 20 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2c 20   initial state, 
28330 72 65 61 64 79 20 74 6f 20 62 65 20 72 65 2d 65  ready to be re-e
28340 78 65 63 75 74 65 64 2e 0a 2a 2a 20 41 6e 79 20  xecuted..** Any 
28350 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61  SQL statement va
28360 72 69 61 62 6c 65 73 20 74 68 61 74 20 68 61 64  riables that had
28370 20 76 61 6c 75 65 73 20 62 6f 75 6e 64 20 74 6f   values bound to
28380 20 74 68 65 6d 20 75 73 69 6e 67 0a 2a 2a 20 74   them using.** t
28390 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  he [sqlite3_bind
283a0 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33 5f  _blob | sqlite3_
283b0 62 69 6e 64 5f 2a 28 29 20 41 50 49 5d 20 72 65  bind_*() API] re
283c0 74 61 69 6e 20 74 68 65 69 72 20 76 61 6c 75 65  tain their value
283d0 73 2e 0a 2a 2a 20 55 73 65 20 5b 73 71 6c 69 74  s..** Use [sqlit
283e0 65 33 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67  e3_clear_binding
283f0 73 28 29 5d 20 74 6f 20 72 65 73 65 74 20 74 68  s()] to reset th
28400 65 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a  e bindings..**.*
28410 2a 20 7b 48 31 31 33 33 32 7d 20 54 68 65 20 5b  * {H11332} The [
28420 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29  sqlite3_reset(S)
28430 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 73 65  ] interface rese
28440 74 73 20 74 68 65 20 5b 70 72 65 70 61 72 65 64  ts the [prepared
28450 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 0a 2a 2a   statement] S.**
28460 20 20 20 20 20 20 20 20 20 20 62 61 63 6b 20 74            back t
28470 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  o the beginning 
28480 6f 66 20 69 74 73 20 70 72 6f 67 72 61 6d 2e 0a  of its program..
28490 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 34 7d 20 49  **.** {H11334} I
284a0 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  f the most recen
284b0 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  t call to [sqlit
284c0 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20  e3_step(S)] for 
284d0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
284e0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
284f0 65 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64 20  ent] S returned 
28500 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20  [SQLITE_ROW] or 
28510 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c 0a 2a  [SQLITE_DONE],.*
28520 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 69 66  *          or if
28530 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
28540 29 5d 20 68 61 73 20 6e 65 76 65 72 20 62 65 66  )] has never bef
28550 6f 72 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  ore been called 
28560 6f 6e 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20  on S,.**        
28570 20 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f    then [sqlite3_
28580 72 65 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e  reset(S)] return
28590 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a  s [SQLITE_OK]..*
285a0 2a 0a 2a 2a 20 7b 48 31 31 33 33 36 7d 20 49 66  *.** {H11336} If
285b0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
285c0 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
285d0 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72 20 74  3_step(S)] for t
285e0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
285f0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
28600 6e 74 5d 20 53 20 69 6e 64 69 63 61 74 65 64 20  nt] S indicated 
28610 61 6e 20 65 72 72 6f 72 2c 20 74 68 65 6e 0a 2a  an error, then.*
28620 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
28630 74 65 33 5f 72 65 73 65 74 28 53 29 5d 20 72 65  te3_reset(S)] re
28640 74 75 72 6e 73 20 61 6e 20 61 70 70 72 6f 70 72  turns an appropr
28650 69 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65  iate [error code
28660 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33 38  ]..**.** {H11338
28670 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72  } The [sqlite3_r
28680 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66 61  eset(S)] interfa
28690 63 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e  ce does not chan
286a0 67 65 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a  ge the values.**
286b0 20 20 20 20 20 20 20 20 20 20 6f 66 20 61 6e 79            of any
286c0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
286d0 6c 6f 62 7c 62 69 6e 64 69 6e 67 73 5d 20 6f 6e  lob|bindings] on
286e0 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73   the [prepared s
286f0 74 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a  tatement] S..*/.
28700 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
28710 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 71 6c  qlite3_reset(sql
28720 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
28730 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
28740 45 46 3a 20 43 72 65 61 74 65 20 4f 72 20 52 65  EF: Create Or Re
28750 64 65 66 69 6e 65 20 53 51 4c 20 46 75 6e 63 74  define SQL Funct
28760 69 6f 6e 73 20 7b 48 31 36 31 30 30 7d 20 3c 53  ions {H16100} <S
28770 32 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  20200>.** KEYWOR
28780 44 53 3a 20 7b 66 75 6e 63 74 69 6f 6e 20 63 72  DS: {function cr
28790 65 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 7d  eation routines}
287a0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61  .** KEYWORDS: {a
287b0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
287c0 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 7d  ed SQL function}
287d0 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 61  .** KEYWORDS: {a
287e0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
287f0 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  ed SQL functions
28800 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 74 77  }.**.** These tw
28810 6f 20 66 75 6e 63 74 69 6f 6e 73 20 28 63 6f 6c  o functions (col
28820 6c 65 63 74 69 76 65 6c 79 20 6b 6e 6f 77 6e 20  lectively known 
28830 61 73 20 22 66 75 6e 63 74 69 6f 6e 20 63 72 65  as "function cre
28840 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 22 29  ation routines")
28850 0a 2a 2a 20 61 72 65 20 75 73 65 64 20 74 6f 20  .** are used to 
28860 61 64 64 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  add SQL function
28870 73 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 20  s or aggregates 
28880 6f 72 20 74 6f 20 72 65 64 65 66 69 6e 65 20 74  or to redefine t
28890 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f  he behavior.** o
288a0 66 20 65 78 69 73 74 69 6e 67 20 53 51 4c 20 66  f existing SQL f
288b0 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67 72  unctions or aggr
288c0 65 67 61 74 65 73 2e 20 20 54 68 65 20 6f 6e 6c  egates.  The onl
288d0 79 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74  y difference bet
288e0 77 65 65 6e 20 74 68 65 0a 2a 2a 20 74 77 6f 20  ween the.** two 
288f0 69 73 20 74 68 61 74 20 74 68 65 20 73 65 63 6f  is that the seco
28900 6e 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68  nd parameter, th
28910 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 28 73  e name of the (s
28920 63 61 6c 61 72 29 20 66 75 6e 63 74 69 6f 6e 20  calar) function 
28930 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65 2c  or.** aggregate,
28940 20 69 73 20 65 6e 63 6f 64 65 64 20 69 6e 20 55   is encoded in U
28950 54 46 2d 38 20 66 6f 72 20 73 71 6c 69 74 65 33  TF-8 for sqlite3
28960 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
28970 28 29 20 61 6e 64 20 55 54 46 2d 31 36 0a 2a 2a  () and UTF-16.**
28980 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65   for sqlite3_cre
28990 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
289a0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
289b0 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  t parameter is t
289c0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
289d0 6e 65 63 74 69 6f 6e 5d 20 74 6f 20 77 68 69 63  nection] to whic
289e0 68 20 74 68 65 20 53 51 4c 0a 2a 2a 20 66 75 6e  h the SQL.** fun
289f0 63 74 69 6f 6e 20 69 73 20 74 6f 20 62 65 20 61  ction is to be a
28a00 64 64 65 64 2e 20 20 49 66 20 61 20 73 69 6e 67  dded.  If a sing
28a10 6c 65 20 70 72 6f 67 72 61 6d 20 75 73 65 73 20  le program uses 
28a20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 64 61  more than one da
28a30 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
28a40 74 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2c  tion internally,
28a50 20 74 68 65 6e 20 53 51 4c 20 66 75 6e 63 74 69   then SQL functi
28a60 6f 6e 73 20 6d 75 73 74 20 62 65 20 61 64 64 65  ons must be adde
28a70 64 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 74  d individually t
28a80 6f 0a 2a 2a 20 65 61 63 68 20 64 61 74 61 62 61  o.** each databa
28a90 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
28aa0 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  *.** The second 
28ab0 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
28ac0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 53 51 4c   name of the SQL
28ad0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20   function to be 
28ae0 63 72 65 61 74 65 64 20 6f 72 0a 2a 2a 20 72 65  created or.** re
28af0 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 6c 65  defined.  The le
28b00 6e 67 74 68 20 6f 66 20 74 68 65 20 6e 61 6d 65  ngth of the name
28b10 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 32   is limited to 2
28b20 35 35 20 62 79 74 65 73 2c 20 65 78 63 6c 75 73  55 bytes, exclus
28b30 69 76 65 20 6f 66 0a 2a 2a 20 74 68 65 20 7a 65  ive of.** the ze
28b40 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 20 20  ro-terminator.  
28b50 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6e 61  Note that the na
28b60 6d 65 20 6c 65 6e 67 74 68 20 6c 69 6d 69 74 20  me length limit 
28b70 69 73 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 74  is in bytes, not
28b80 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 2e 20  .** characters. 
28b90 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   Any attempt to 
28ba0 63 72 65 61 74 65 20 61 20 66 75 6e 63 74 69 6f  create a functio
28bb0 6e 20 77 69 74 68 20 61 20 6c 6f 6e 67 65 72 20  n with a longer 
28bc0 6e 61 6d 65 0a 2a 2a 20 77 69 6c 6c 20 72 65 73  name.** will res
28bd0 75 6c 74 20 69 6e 20 5b 53 51 4c 49 54 45 5f 45  ult in [SQLITE_E
28be0 52 52 4f 52 5d 20 62 65 69 6e 67 20 72 65 74 75  RROR] being retu
28bf0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rned..**.** The 
28c00 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
28c10 28 6e 41 72 67 29 0a 2a 2a 20 69 73 20 74 68 65  (nArg).** is the
28c20 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   number of argum
28c30 65 6e 74 73 20 74 68 61 74 20 74 68 65 20 53 51  ents that the SQ
28c40 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a  L function or.**
28c50 20 61 67 67 72 65 67 61 74 65 20 74 61 6b 65 73   aggregate takes
28c60 2e 20 49 66 20 74 68 69 73 20 70 61 72 61 6d 65  . If this parame
28c70 74 65 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20  ter is -1, then 
28c80 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
28c90 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74 65   or.** aggregate
28ca0 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e 75   may take any nu
28cb0 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
28cc0 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20  s between 0 and 
28cd0 74 68 65 20 6c 69 6d 69 74 0a 2a 2a 20 73 65 74  the limit.** set
28ce0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d   by [sqlite3_lim
28cf0 69 74 5d 28 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  it]([SQLITE_LIMI
28d00 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 5d 29  T_FUNCTION_ARG])
28d10 2e 20 20 49 66 20 74 68 65 20 74 68 69 72 64 0a  .  If the third.
28d20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ** parameter is 
28d30 6c 65 73 73 20 74 68 61 6e 20 2d 31 20 6f 72 20  less than -1 or 
28d40 67 72 65 61 74 65 72 20 74 68 61 6e 20 31 32 37  greater than 127
28d50 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69   then the behavi
28d60 6f 72 20 69 73 0a 2a 2a 20 75 6e 64 65 66 69 6e  or is.** undefin
28d70 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ed..**.** The fo
28d80 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20  urth parameter, 
28d90 65 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66  eTextRep, specif
28da0 69 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c  ies what.** [SQL
28db0 49 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20  ITE_UTF8 | text 
28dc0 65 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53  encoding] this S
28dd0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66  QL function pref
28de0 65 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70  ers for.** its p
28df0 61 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20  arameters.  Any 
28e00 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  SQL function imp
28e10 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75  lementation shou
28e20 6c 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f  ld be able to wo
28e30 72 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20  rk.** work with 
28e40 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c  UTF-8, UTF-16le,
28e50 20 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42   or UTF-16be.  B
28e60 75 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e  ut some implemen
28e70 74 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a  tations may be.*
28e80 2a 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74  * more efficient
28e90 20 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69   with one encodi
28ea0 6e 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e  ng than another.
28eb0 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e    An application
28ec0 20 6d 61 79 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73   may.** invoke s
28ed0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
28ee0 6e 63 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69  nction() or sqli
28ef0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
28f00 69 6f 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65  ion16() multiple
28f10 0a 2a 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74  .** times with t
28f20 68 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e  he same function
28f30 20 62 75 74 20 77 69 74 68 20 64 69 66 66 65 72   but with differ
28f40 65 6e 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54  ent values of eT
28f50 65 78 74 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20  extRep..** When 
28f60 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65  multiple impleme
28f70 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ntations of the 
28f80 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72  same function ar
28f90 65 20 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c  e available, SQL
28fa0 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b  ite.** will pick
28fb0 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20 69 6e   the one that in
28fc0 76 6f 6c 76 65 73 20 74 68 65 20 6c 65 61 73 74  volves the least
28fd0 20 61 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20   amount of data 
28fe0 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49  conversion..** I
28ff0 66 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20  f there is only 
29000 61 20 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65  a single impleme
29010 6e 74 61 74 69 6f 6e 20 77 68 69 63 68 20 64 6f  ntation which do
29020 65 73 20 6e 6f 74 20 63 61 72 65 20 77 68 61 74  es not care what
29030 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e   text.** encodin
29040 67 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20  g is used, then 
29050 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d  the fourth argum
29060 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 5b 53  ent should be [S
29070 51 4c 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a  QLITE_ANY]..**.*
29080 2a 20 54 68 65 20 66 69 66 74 68 20 70 61 72 61  * The fifth para
29090 6d 65 74 65 72 20 69 73 20 61 6e 20 61 72 62 69  meter is an arbi
290a0 74 72 61 72 79 20 70 6f 69 6e 74 65 72 2e 20 20  trary pointer.  
290b0 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  The implementati
290c0 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e  on of the.** fun
290d0 63 74 69 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61  ction can gain a
290e0 63 63 65 73 73 20 74 6f 20 74 68 69 73 20 70 6f  ccess to this po
290f0 69 6e 74 65 72 20 75 73 69 6e 67 20 5b 73 71 6c  inter using [sql
29100 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29  ite3_user_data()
29110 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76  ]..**.** The sev
29120 65 6e 74 68 2c 20 65 69 67 68 74 68 20 61 6e 64  enth, eighth and
29130 20 6e 69 6e 74 68 20 70 61 72 61 6d 65 74 65 72   ninth parameter
29140 73 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 20  s, xFunc, xStep 
29150 61 6e 64 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a  and xFinal, are.
29160 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 43  ** pointers to C
29170 2d 6c 61 6e 67 75 61 67 65 20 66 75 6e 63 74 69  -language functi
29180 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
29190 6e 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  nt the SQL funct
291a0 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67  ion or.** aggreg
291b0 61 74 65 2e 20 41 20 73 63 61 6c 61 72 20 53 51  ate. A scalar SQ
291c0 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69  L function requi
291d0 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74  res an implement
291e0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 78 46 75  ation of the xFu
291f0 6e 63 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f  nc.** callback o
29200 6e 6c 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  nly, NULL pointe
29210 72 73 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73  rs should be pas
29220 73 65 64 20 61 73 20 74 68 65 20 78 53 74 65 70  sed as the xStep
29230 20 61 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70   and xFinal.** p
29240 61 72 61 6d 65 74 65 72 73 2e 20 41 6e 20 61 67  arameters. An ag
29250 67 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63  gregate SQL func
29260 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e  tion requires an
29270 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
29280 6f 66 20 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20  of xStep.** and 
29290 78 46 69 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20  xFinal and NULL 
292a0 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
292b0 20 66 6f 72 20 78 46 75 6e 63 2e 20 54 6f 20 64   for xFunc. To d
292c0 65 6c 65 74 65 20 61 6e 20 65 78 69 73 74 69 6e  elete an existin
292d0 67 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  g.** SQL functio
292e0 6e 20 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20  n or aggregate, 
292f0 70 61 73 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c  pass NULL for al
29300 6c 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e  l three function
29310 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a   callbacks..**.*
29320 2a 20 49 74 20 69 73 20 70 65 72 6d 69 74 74 65  * It is permitte
29330 64 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d 75  d to register mu
29340 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  ltiple implement
29350 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61  ations of the sa
29360 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  me.** functions 
29370 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61  with the same na
29380 6d 65 20 62 75 74 20 77 69 74 68 20 65 69 74 68  me but with eith
29390 65 72 20 64 69 66 66 65 72 69 6e 67 20 6e 75 6d  er differing num
293a0 62 65 72 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d  bers of.** argum
293b0 65 6e 74 73 20 6f 72 20 64 69 66 66 65 72 69 6e  ents or differin
293c0 67 20 70 72 65 66 65 72 72 65 64 20 74 65 78 74  g preferred text
293d0 20 65 6e 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c   encodings.  SQL
293e0 69 74 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20  ite will use.** 
293f0 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
29400 6f 6e 20 74 68 61 74 20 6d 6f 73 74 20 63 6c 6f  on that most clo
29410 73 65 6c 79 20 6d 61 74 63 68 65 73 20 74 68 65  sely matches the
29420 20 77 61 79 20 69 6e 20 77 68 69 63 68 20 74 68   way in which th
29430 65 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f  e.** SQL functio
29440 6e 20 69 73 20 75 73 65 64 2e 20 20 41 20 66 75  n is used.  A fu
29450 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
29460 61 74 69 6f 6e 20 77 69 74 68 20 61 20 6e 6f 6e  ation with a non
29470 2d 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6e 41 72  -negative.** nAr
29480 67 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  g parameter is a
29490 20 62 65 74 74 65 72 20 6d 61 74 63 68 20 74 68   better match th
294a0 61 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 69 6d  an a function im
294b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74  plementation wit
294c0 68 0a 2a 2a 20 61 20 6e 65 67 61 74 69 76 65 20  h.** a negative 
294d0 6e 41 72 67 2e 20 20 41 20 66 75 6e 63 74 69 6f  nArg.  A functio
294e0 6e 20 77 68 65 72 65 20 74 68 65 20 70 72 65 66  n where the pref
294f0 65 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64  erred text encod
29500 69 6e 67 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74  ing.** matches t
29510 68 65 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f  he database enco
29520 64 69 6e 67 20 69 73 20 61 20 62 65 74 74 65 72  ding is a better
29530 0a 2a 2a 20 6d 61 74 63 68 20 74 68 61 6e 20 61  .** match than a
29540 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20   function where 
29550 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  the encoding is 
29560 64 69 66 66 65 72 65 6e 74 2e 20 20 0a 2a 2a 20  different.  .** 
29570 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65  A function where
29580 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 64 69   the encoding di
29590 66 66 65 72 65 6e 63 65 20 69 73 20 62 65 74 77  fference is betw
295a0 65 65 6e 20 55 54 46 31 36 6c 65 20 61 6e 64 20  een UTF16le and 
295b0 55 54 46 31 36 62 65 0a 2a 2a 20 69 73 20 61 20  UTF16be.** is a 
295c0 63 6c 6f 73 65 72 20 6d 61 74 63 68 20 74 68 61  closer match tha
295d0 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 68 65  n a function whe
295e0 72 65 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  re the encoding 
295f0 64 69 66 66 65 72 65 6e 63 65 20 69 73 0a 2a 2a  difference is.**
29600 20 62 65 74 77 65 65 6e 20 55 54 46 38 20 61 6e   between UTF8 an
29610 64 20 55 54 46 31 36 2e 0a 2a 2a 0a 2a 2a 20 42  d UTF16..**.** B
29620 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  uilt-in function
29630 73 20 6d 61 79 20 62 65 20 6f 76 65 72 6c 6f 61  s may be overloa
29640 64 65 64 20 62 79 20 6e 65 77 20 61 70 70 6c 69  ded by new appli
29650 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66  cation-defined f
29660 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65  unctions..** The
29670 20 66 69 72 73 74 20 61 70 70 6c 69 63 61 74 69   first applicati
29680 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
29690 69 6f 6e 20 77 69 74 68 20 61 20 67 69 76 65 6e  ion with a given
296a0 20 6e 61 6d 65 20 6f 76 65 72 72 69 64 65 73 20   name overrides 
296b0 61 6c 6c 0a 2a 2a 20 62 75 69 6c 74 2d 69 6e 20  all.** built-in 
296c0 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65  functions in the
296d0 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20   same [database 
296e0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 69 74 68  connection] with
296f0 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 0a   the same name..
29700 2a 2a 20 53 75 62 73 65 71 75 65 6e 74 20 61 70  ** Subsequent ap
29710 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
29720 64 20 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 74  d functions of t
29730 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 6f 6e 6c  he same name onl
29740 79 20 6f 76 65 72 72 69 64 65 20 0a 2a 2a 20 70  y override .** p
29750 72 69 6f 72 20 61 70 70 6c 69 63 61 74 69 6f 6e  rior application
29760 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
29770 6e 73 20 74 68 61 74 20 61 72 65 20 61 6e 20 65  ns that are an e
29780 78 61 63 74 20 6d 61 74 63 68 20 66 6f 72 20 74  xact match for t
29790 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
297a0 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20 70  parameters and p
297b0 72 65 66 65 72 72 65 64 20 65 6e 63 6f 64 69 6e  referred encodin
297c0 67 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 70 70 6c  g..**.** An appl
297d0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
297e0 66 75 6e 63 74 69 6f 6e 20 69 73 20 70 65 72 6d  function is perm
297f0 69 74 74 65 64 20 74 6f 20 63 61 6c 6c 20 6f 74  itted to call ot
29800 68 65 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6e  her.** SQLite in
29810 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65 76  terfaces.  Howev
29820 65 72 2c 20 73 75 63 68 20 63 61 6c 6c 73 20 6d  er, such calls m
29830 75 73 74 20 6e 6f 74 0a 2a 2a 20 63 6c 6f 73 65  ust not.** close
29840 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
29850 6e 6e 65 63 74 69 6f 6e 20 6e 6f 72 20 66 69 6e  nnection nor fin
29860 61 6c 69 7a 65 20 6f 72 20 72 65 73 65 74 20 74  alize or reset t
29870 68 65 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 73  he prepared.** s
29880 74 61 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63  tatement in whic
29890 68 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  h the function i
298a0 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a  s running..**.**
298b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
298c0 2a 20 5b 48 31 36 31 30 33 5d 20 5b 48 31 36 31  * [H16103] [H161
298d0 30 36 5d 20 5b 48 31 36 31 30 39 5d 20 5b 48 31  06] [H16109] [H1
298e0 36 31 31 32 5d 20 5b 48 31 36 31 31 38 5d 20 5b  6112] [H16118] [
298f0 48 31 36 31 32 31 5d 20 5b 48 31 36 31 32 37 5d  H16121] [H16127]
29900 0a 2a 2a 20 5b 48 31 36 31 33 30 5d 20 5b 48 31  .** [H16130] [H1
29910 36 31 33 33 5d 20 5b 48 31 36 31 33 36 5d 20 5b  6133] [H16136] [
29920 48 31 36 31 33 39 5d 20 5b 48 31 36 31 34 32 5d  H16139] [H16142]
29930 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
29940 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
29950 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71  e_function(.  sq
29960 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e  lite3 *db,.  con
29970 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69  st char *zFuncti
29980 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41  onName,.  int nA
29990 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52  rg,.  int eTextR
299a0 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41 70 70  ep,.  void *pApp
299b0 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63  ,.  void (*xFunc
299c0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
299d0 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
299e0 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20  alue**),.  void 
299f0 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33  (*xStep)(sqlite3
29a00 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
29a10 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a  lite3_value**),.
29a20 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29    void (*xFinal)
29a30 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
29a40 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  *).);.SQLITE_API
29a50 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
29a60 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 0a  ate_function16(.
29a70 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
29a80 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 75   const void *zFu
29a90 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e  nctionName,.  in
29aa0 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54  t nArg,.  int eT
29ab0 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a  extRep,.  void *
29ac0 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78  pApp,.  void (*x
29ad0 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
29ae0 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
29af0 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76  e3_value**),.  v
29b00 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
29b10 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
29b20 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
29b30 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69  *),.  void (*xFi
29b40 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nal)(sqlite3_con
29b50 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  text*).);../*.**
29b60 20 43 41 50 49 33 52 45 46 3a 20 54 65 78 74 20   CAPI3REF: Text 
29b70 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30 32 36  Encodings {H1026
29b80 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48 31 36  7} <S50200> <H16
29b90 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  100>.**.** These
29ba0 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69 6e 65   constant define
29bb0 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20 74   integer codes t
29bc0 68 61 74 20 72 65 70 72 65 73 65 6e 74 20 74 68  hat represent th
29bd0 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74 65 78  e various.** tex
29be0 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75 70 70  t encodings supp
29bf0 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e  orted by SQLite.
29c00 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
29c10 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 20  TE_UTF8         
29c20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    1.#define SQLI
29c30 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20 20  TE_UTF16LE      
29c40 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    2.#define SQLI
29c50 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20 20  TE_UTF16BE      
29c60 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    3.#define SQLI
29c70 54 45 5f 55 54 46 31 36 20 20 20 20 20 20 20 20  TE_UTF16        
29c80 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20 6e 61    4    /* Use na
29c90 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20  tive byte order 
29ca0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
29cb0 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20 20 20  E_ANY           
29cc0 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33   5    /* sqlite3
29cd0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
29ce0 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
29cf0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c   SQLITE_UTF16_AL
29d00 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a 20 73  IGNED  8    /* s
29d10 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
29d20 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a  llation only */.
29d30 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
29d40 20 44 65 70 72 65 63 61 74 65 64 20 46 75 6e 63   Deprecated Func
29d50 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45 43 41  tions.** DEPRECA
29d60 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  TED.**.** These 
29d70 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 5b 64  functions are [d
29d80 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49 6e 20  eprecated].  In 
29d90 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69  order to maintai
29da0 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73 20 63  n.** backwards c
29db0 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74  ompatibility wit
29dc0 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20 74 68  h older code, th
29dd0 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f  ese functions co
29de0 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20 62 65  ntinue .** to be
29df0 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48 6f 77   supported.  How
29e00 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c 69 63  ever, new applic
29e10 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 61 76  ations should av
29e20 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65 20 6f  oid.** the use o
29e30 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  f these function
29e40 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e 63 6f  s.  To help enco
29e50 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74 6f 20  urage people to 
29e60 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67 20 74  avoid.** using t
29e70 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 2c 20  hese functions, 
29e80 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69 6e 67  we are not going
29e90 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77 68 61   to tell you wha
29ea0 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a 23 69  t they do..*/.#i
29eb0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29ec0 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53 51 4c  T_DEPRECATED.SQL
29ed0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44  ITE_API SQLITE_D
29ee0 45 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71  EPRECATED int sq
29ef0 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
29f00 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f  count(sqlite3_co
29f10 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  ntext*);.SQLITE_
29f20 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52 45  API SQLITE_DEPRE
29f30 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74 65  CATED int sqlite
29f40 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74 65  3_expired(sqlite
29f50 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49 54 45  3_stmt*);.SQLITE
29f60 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50 52  _API SQLITE_DEPR
29f70 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69 74  ECATED int sqlit
29f80 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64  e3_transfer_bind
29f90 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d  ings(sqlite3_stm
29fa0 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  t*, sqlite3_stmt
29fb0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  *);.SQLITE_API S
29fc0 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44  QLITE_DEPRECATED
29fd0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f   int sqlite3_glo
29fe0 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 64  bal_recover(void
29ff0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  );.SQLITE_API SQ
2a000 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20  LITE_DEPRECATED 
2a010 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68 72  void sqlite3_thr
2a020 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64  ead_cleanup(void
2a030 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  );.SQLITE_API SQ
2a040 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20  LITE_DEPRECATED 
2a050 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  int sqlite3_memo
2a060 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28 2a 29  ry_alarm(void(*)
2a070 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69  (void*,sqlite3_i
2a080 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64 2a 2c  nt64,int),void*,
2a090 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a  sqlite3_int64);.
2a0a0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41  #endif../*.** CA
2a0b0 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e 69 6e  PI3REF: Obtainin
2a0c0 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 50  g SQL Function P
2a0d0 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65 73 20  arameter Values 
2a0e0 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32 30 30  {H15100} <S20200
2a0f0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d 6c 61  >.**.** The C-la
2a100 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65 6e 74  nguage implement
2a110 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66 75 6e  ation of SQL fun
2a120 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72 65  ctions and aggre
2a130 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20 74 68  gates uses.** th
2a140 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65 72 66  is set of interf
2a150 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 20  ace routines to 
2a160 61 63 63 65 73 73 20 74 68 65 20 70 61 72 61 6d  access the param
2a170 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e 0a 2a  eter values on.*
2a180 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 6f  * the function o
2a190 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a  r aggregate..**.
2a1a0 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28 66 6f  ** The xFunc (fo
2a1b0 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  r scalar functio
2a1c0 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28 66 6f  ns) or xStep (fo
2a1d0 72 20 61 67 67 72 65 67 61 74 65 73 29 20 70 61  r aggregates) pa
2a1e0 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f 20 5b  rameters.** to [
2a1f0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2a200 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20 5b  unction()] and [
2a210 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
2a220 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a 20  unction16()].** 
2a230 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63 6b 73  define callbacks
2a240 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
2a250 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
2a260 73 20 61 6e 64 20 61 67 67 72 65 67 61 74 65 73  s and aggregates
2a270 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70 61 72  ..** The 4th par
2a280 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73 65 20  ameter to these 
2a290 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61 6e 20  callbacks is an 
2a2a0 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72  array of pointer
2a2b0 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65 63 74  s to.** [protect
2a2c0 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
2a2d0 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 72  ] objects.  Ther
2a2e0 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69 74 65  e is one [sqlite
2a2f0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20  3_value] object 
2a300 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61 72 61  for.** each para
2a310 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53 51 4c  meter to the SQL
2a320 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73   function.  Thes
2a330 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75  e routines are u
2a340 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72 61 63  sed to.** extrac
2a350 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68  t values from th
2a360 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
2a370 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a  ] objects..**.**
2a380 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
2a390 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68 20 5b  work only with [
2a3a0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
2a3b0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 73  3_value] objects
2a3c0 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d 70 74  ..** Any attempt
2a3d0 20 74 6f 20 75 73 65 20 74 68 65 73 65 20 72 6f   to use these ro
2a3e0 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b 75 6e  utines on an [un
2a3f0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
2a400 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62 6a 65  3_value].** obje
2a410 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e  ct results in un
2a420 64 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72  defined behavior
2a430 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
2a440 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75 73 74  utines work just
2a450 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72 65 73   like the corres
2a460 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d 6e 20  ponding [column 
2a470 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f 6e 73  access functions
2a480 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74  ].** except that
2a490 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73    these routines
2a4a0 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65 20 5b   take a single [
2a4b0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
2a4c0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 0a  3_value] object.
2a4d0 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65  ** pointer inste
2a4e0 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74 65 33  ad of a [sqlite3
2a4f0 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65 72 20  _stmt*] pointer 
2a500 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72 20 63  and an integer c
2a510 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a  olumn number..**
2a520 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2a530 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 20 69  value_text16() i
2a540 6e 74 65 72 66 61 63 65 20 65 78 74 72 61 63 74  nterface extract
2a550 73 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e  s a UTF-16 strin
2a560 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61 74 69  g.** in the nati
2a570 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20 6f 66  ve byte-order of
2a580 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68 69 6e   the host machin
2a590 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74  e.  The.** sqlit
2a5a0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62  e3_value_text16b
2a5b0 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  e() and sqlite3_
2a5c0 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 29  value_text16le()
2a5d0 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 65   interfaces.** e
2a5e0 78 74 72 61 63 74 20 55 54 46 2d 31 36 20 73 74  xtract UTF-16 st
2a5f0 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65 6e 64  rings as big-end
2a600 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65 2d 65  ian and little-e
2a610 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69 76 65  ndian respective
2a620 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ly..**.** The sq
2a630 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65  lite3_value_nume
2a640 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74 65 72  ric_type() inter
2a650 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f  face attempts to
2a660 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65 72 69   apply.** numeri
2a670 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20 74 68  c affinity to th
2a680 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 6d  e value.  This m
2a690 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61 74 74  eans that an att
2a6a0 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64 65 20  empt is.** made 
2a6b0 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20 76  to convert the v
2a6c0 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74 65 67  alue to an integ
2a6d0 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67 20 70  er or floating p
2a6e0 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73 75 63  oint.  If.** suc
2a6f0 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  h a conversion i
2a700 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68 6f  s possible witho
2a710 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72  ut loss of infor
2a720 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68 65 72  mation (in other
2a730 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20 74 68  .** words, if th
2a740 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72  e value is a str
2a750 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73 20 6c  ing that looks l
2a760 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a 2a 2a  ike a number).**
2a770 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76 65 72   then the conver
2a780 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65  sion is performe
2a790 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 6e 6f  d.  Otherwise no
2a7a0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63 63 75   conversion occu
2a7b0 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51 4c 49  rs..** The [SQLI
2a7c0 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64 61 74  TE_INTEGER | dat
2a7d0 61 74 79 70 65 5d 20 61 66 74 65 72 20 63 6f 6e  atype] after con
2a7e0 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74 75 72  version is retur
2a7f0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65 61 73  ned..**.** Pleas
2a800 65 20 70 61 79 20 70 61 72 74 69 63 75 6c 61 72  e pay particular
2a810 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20 74 68   attention to th
2a820 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65 20  e fact that the 
2a830 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64  pointer returned
2a840 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  .** from [sqlite
2a850 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29 5d 2c  3_value_blob()],
2a860 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
2a870 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b  text()], or.** [
2a880 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2a890 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65 20 69  xt16()] can be i
2a8a0 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 61 20  nvalidated by a 
2a8b0 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 20  subsequent call 
2a8c0 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76  to.** [sqlite3_v
2a8d0 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20 5b  alue_bytes()], [
2a8e0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
2a8f0 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c 69 74  tes16()], [sqlit
2a900 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d  e3_value_text()]
2a910 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33  ,.** or [sqlite3
2a920 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d  _value_text16()]
2a930 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
2a940 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65 20 63  utines must be c
2a950 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73  alled from the s
2a960 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a 2a 2a  ame thread as.**
2a970 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
2a980 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65 64 20  n that supplied 
2a990 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  the [sqlite3_val
2a9a0 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72 73 2e  ue*] parameters.
2a9b0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
2a9c0 6e 74 73 3a 0a 2a 2a 20 5b 48 31 35 31 30 33 5d  nts:.** [H15103]
2a9d0 20 5b 48 31 35 31 30 36 5d 20 5b 48 31 35 31 30   [H15106] [H1510
2a9e0 39 5d 20 5b 48 31 35 31 31 32 5d 20 5b 48 31 35  9] [H15112] [H15
2a9f0 31 31 35 5d 20 5b 48 31 35 31 31 38 5d 20 5b 48  115] [H15118] [H
2aa00 31 35 31 32 31 5d 20 5b 48 31 35 31 32 34 5d 0a  15121] [H15124].
2aa10 2a 2a 20 5b 48 31 35 31 32 37 5d 20 5b 48 31 35  ** [H15127] [H15
2aa20 31 33 30 5d 20 5b 48 31 35 31 33 33 5d 20 5b 48  130] [H15133] [H
2aa30 31 35 31 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45  15136].*/.SQLITE
2aa40 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
2aa50 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  *sqlite3_value_b
2aa60 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  lob(sqlite3_valu
2aa70 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
2aa80 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  int sqlite3_valu
2aa90 65 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f  e_bytes(sqlite3_
2aaa0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
2aab0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2aac0 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71  value_bytes16(sq
2aad0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2aae0 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65  QLITE_API double
2aaf0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64   sqlite3_value_d
2ab00 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61  ouble(sqlite3_va
2ab10 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2ab20 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
2ab30 6c 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f  lue_int(sqlite3_
2ab40 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
2ab50 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  API sqlite3_int6
2ab60 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  4 sqlite3_value_
2ab70 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61  int64(sqlite3_va
2ab80 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2ab90 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
2aba0 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76   char *sqlite3_v
2abb0 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65  alue_text(sqlite
2abc0 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
2abd0 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
2abe0 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   *sqlite3_value_
2abf0 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76  text16(sqlite3_v
2ac00 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41  alue*);.SQLITE_A
2ac10 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
2ac20 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2ac30 74 31 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61  t16le(sqlite3_va
2ac40 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2ac50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
2ac60 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2ac70 31 36 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c  16be(sqlite3_val
2ac80 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
2ac90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
2aca0 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f  ue_type(sqlite3_
2acb0 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
2acc0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2acd0 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79  value_numeric_ty
2ace0 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  pe(sqlite3_value
2acf0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
2ad00 52 45 46 3a 20 4f 62 74 61 69 6e 20 41 67 67 72  REF: Obtain Aggr
2ad10 65 67 61 74 65 20 46 75 6e 63 74 69 6f 6e 20 43  egate Function C
2ad20 6f 6e 74 65 78 74 20 7b 48 31 36 32 31 30 7d 20  ontext {H16210} 
2ad30 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20200>.**.** T
2ad40 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
2ad50 6e 20 6f 66 20 61 67 67 72 65 67 61 74 65 20 53  n of aggregate S
2ad60 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65  QL functions use
2ad70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
2ad80 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73   allocate.** a s
2ad90 74 72 75 63 74 75 72 65 20 66 6f 72 20 73 74 6f  tructure for sto
2ada0 72 69 6e 67 20 74 68 65 69 72 20 73 74 61 74 65  ring their state
2adb0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
2adc0 74 20 74 69 6d 65 20 74 68 65 20 73 71 6c 69 74  t time the sqlit
2add0 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
2ade0 74 65 78 74 28 29 20 72 6f 75 74 69 6e 65 20 69  text() routine i
2adf0 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a  s called for a.*
2ae00 2a 20 70 61 72 74 69 63 75 6c 61 72 20 61 67 67  * particular agg
2ae10 72 65 67 61 74 65 2c 20 53 51 4c 69 74 65 20 61  regate, SQLite a
2ae20 6c 6c 6f 63 61 74 65 73 20 6e 42 79 74 65 73 20  llocates nBytes 
2ae30 6f 66 20 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65  of memory, zeroe
2ae40 73 20 6f 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65  s out that.** me
2ae50 6d 6f 72 79 2c 20 61 6e 64 20 72 65 74 75 72 6e  mory, and return
2ae60 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
2ae70 74 2e 20 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64  t. On second and
2ae80 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
2ae90 73 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  s to.** sqlite3_
2aea0 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
2aeb0 74 28 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65  t() for the same
2aec0 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
2aed0 69 6f 6e 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68  ion index,.** th
2aee0 65 20 73 61 6d 65 20 62 75 66 66 65 72 20 69 73  e same buffer is
2aef0 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 69   returned. The i
2af00 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
2af10 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 63   the aggregate c
2af20 61 6e 20 75 73 65 0a 2a 2a 20 74 68 65 20 72 65  an use.** the re
2af30 74 75 72 6e 65 64 20 62 75 66 66 65 72 20 74 6f  turned buffer to
2af40 20 61 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61   accumulate data
2af50 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61  ..**.** SQLite a
2af60 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66 72 65  utomatically fre
2af70 65 73 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64  es the allocated
2af80 20 62 75 66 66 65 72 20 77 68 65 6e 20 74 68 65   buffer when the
2af90 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20 71 75   aggregate.** qu
2afa0 65 72 79 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a  ery concludes..*
2afb0 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70  *.** The first p
2afc0 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20  arameter should 
2afd0 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  be a copy of the
2afe0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  .** [sqlite3_con
2aff0 74 65 78 74 20 7c 20 53 51 4c 20 66 75 6e 63 74  text | SQL funct
2b000 69 6f 6e 20 63 6f 6e 74 65 78 74 5d 20 74 68 61  ion context] tha
2b010 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20 70  t is the first p
2b020 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74  arameter.** to t
2b030 68 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74  he callback rout
2b040 69 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ine that impleme
2b050 6e 74 73 20 74 68 65 20 61 67 67 72 65 67 61 74  nts the aggregat
2b060 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  e function..**.*
2b070 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  * This routine m
2b080 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72  ust be called fr
2b090 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  om the same thre
2b0a0 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74  ad in which.** t
2b0b0 68 65 20 61 67 67 72 65 67 61 74 65 20 53 51 4c  he aggregate SQL
2b0c0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e   function is run
2b0d0 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ning..**.** Requ
2b0e0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
2b0f0 36 32 31 31 5d 20 5b 48 31 36 32 31 33 5d 20 5b  6211] [H16213] [
2b100 48 31 36 32 31 35 5d 20 5b 48 31 36 32 31 37 5d  H16215] [H16217]
2b110 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
2b120 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67  oid *sqlite3_agg
2b130 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73  regate_context(s
2b140 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2b150 20 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f   int nBytes);../
2b160 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55  *.** CAPI3REF: U
2b170 73 65 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e  ser Data For Fun
2b180 63 74 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20  ctions {H16240} 
2b190 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20200>.**.** T
2b1a0 68 65 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  he sqlite3_user_
2b1b0 64 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65  data() interface
2b1c0 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20   returns a copy 
2b1d0 6f 66 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65  of.** the pointe
2b1e0 72 20 74 68 61 74 20 77 61 73 20 74 68 65 20 70  r that was the p
2b1f0 55 73 65 72 44 61 74 61 20 70 61 72 61 6d 65 74  UserData paramet
2b200 65 72 20 28 74 68 65 20 35 74 68 20 70 61 72 61  er (the 5th para
2b210 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65  meter).** of the
2b220 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
2b230 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20  _function()].** 
2b240 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  and [sqlite3_cre
2b250 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29  ate_function16()
2b260 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  ] routines that 
2b270 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65  originally.** re
2b280 67 69 73 74 65 72 65 64 20 74 68 65 20 61 70 70  gistered the app
2b290 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64  lication defined
2b2a0 20 66 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d   function. {END}
2b2b0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2b2c0 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  ine must be call
2b2d0 65 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65  ed from the same
2b2e0 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68   thread in which
2b2f0 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74  .** the applicat
2b300 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2b310 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e  tion is running.
2b320 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
2b330 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 32 34 33 5d  nts:.** [H16243]
2b340 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
2b350 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73 65  oid *sqlite3_use
2b360 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63  r_data(sqlite3_c
2b370 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  ontext*);../*.**
2b380 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61 62   CAPI3REF: Datab
2b390 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 46  ase Connection F
2b3a0 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31  or Functions {H1
2b3b0 36 32 35 30 7d 20 3c 53 36 30 36 30 30 3e 3c 53  6250} <S60600><S
2b3c0 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20200>.**.** The
2b3d0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2b3e0 5f 64 62 5f 68 61 6e 64 6c 65 28 29 20 69 6e 74  _db_handle() int
2b3f0 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61  erface returns a
2b400 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20   copy of.** the 
2b410 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b  pointer to the [
2b420 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2b430 69 6f 6e 5d 20 28 74 68 65 20 31 73 74 20 70 61  ion] (the 1st pa
2b440 72 61 6d 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74  rameter).** of t
2b450 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  he [sqlite3_crea
2b460 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a  te_function()].*
2b470 2a 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63  * and [sqlite3_c
2b480 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
2b490 28 29 5d 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ()] routines tha
2b4a0 74 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20  t originally.** 
2b4b0 72 65 67 69 73 74 65 72 65 64 20 74 68 65 20 61  registered the a
2b4c0 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e  pplication defin
2b4d0 65 64 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  ed function..**.
2b4e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2b4f0 0a 2a 2a 20 5b 48 31 36 32 35 33 5d 0a 2a 2f 0a  .** [H16253].*/.
2b500 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
2b510 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74  e3 *sqlite3_cont
2b520 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71  ext_db_handle(sq
2b530 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
2b540 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
2b550 3a 20 46 75 6e 63 74 69 6f 6e 20 41 75 78 69 6c  : Function Auxil
2b560 69 61 72 79 20 44 61 74 61 20 7b 48 31 36 32 37  iary Data {H1627
2b570 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a  0} <S20200>.**.*
2b580 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2b590 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61  two functions ma
2b5a0 79 20 62 65 20 75 73 65 64 20 62 79 20 73 63 61  y be used by sca
2b5b0 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  lar SQL function
2b5c0 73 20 74 6f 0a 2a 2a 20 61 73 73 6f 63 69 61 74  s to.** associat
2b5d0 65 20 6d 65 74 61 64 61 74 61 20 77 69 74 68 20  e metadata with 
2b5e0 61 72 67 75 6d 65 6e 74 20 76 61 6c 75 65 73 2e  argument values.
2b5f0 20 49 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c   If the same val
2b600 75 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 0a  ue is passed to.
2b610 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 69 6e 76 6f  ** multiple invo
2b620 63 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73  cations of the s
2b630 61 6d 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  ame SQL function
2b640 20 64 75 72 69 6e 67 20 71 75 65 72 79 20 65 78   during query ex
2b650 65 63 75 74 69 6f 6e 2c 20 75 6e 64 65 72 0a 2a  ecution, under.*
2b660 2a 20 73 6f 6d 65 20 63 69 72 63 75 6d 73 74 61  * some circumsta
2b670 6e 63 65 73 20 74 68 65 20 61 73 73 6f 63 69 61  nces the associa
2b680 74 65 64 20 6d 65 74 61 64 61 74 61 20 6d 61 79  ted metadata may
2b690 20 62 65 20 70 72 65 73 65 72 76 65 64 2e 20 54   be preserved. T
2b6a0 68 69 73 20 6d 61 79 0a 2a 2a 20 62 65 20 75 73  his may.** be us
2b6b0 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  ed, for example,
2b6c0 20 74 6f 20 61 64 64 20 61 20 72 65 67 75 6c 61   to add a regula
2b6d0 72 2d 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 74  r-expression mat
2b6e0 63 68 69 6e 67 20 73 63 61 6c 61 72 0a 2a 2a 20  ching scalar.** 
2b6f0 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 63 6f  function. The co
2b700 6d 70 69 6c 65 64 20 76 65 72 73 69 6f 6e 20 6f  mpiled version o
2b710 66 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  f the regular ex
2b720 70 72 65 73 73 69 6f 6e 20 69 73 20 73 74 6f 72  pression is stor
2b730 65 64 20 61 73 0a 2a 2a 20 6d 65 74 61 64 61 74  ed as.** metadat
2b740 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
2b750 68 20 74 68 65 20 53 51 4c 20 76 61 6c 75 65 20  h the SQL value 
2b760 70 61 73 73 65 64 20 61 73 20 74 68 65 20 72 65  passed as the re
2b770 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
2b780 0a 2a 2a 20 70 61 74 74 65 72 6e 2e 20 20 54 68  .** pattern.  Th
2b790 65 20 63 6f 6d 70 69 6c 65 64 20 72 65 67 75 6c  e compiled regul
2b7a0 61 72 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61  ar expression ca
2b7b0 6e 20 62 65 20 72 65 75 73 65 64 20 6f 6e 20 6d  n be reused on m
2b7c0 75 6c 74 69 70 6c 65 0a 2a 2a 20 69 6e 76 6f 63  ultiple.** invoc
2b7d0 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61  ations of the sa
2b7e0 6d 65 20 66 75 6e 63 74 69 6f 6e 20 73 6f 20 74  me function so t
2b7f0 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  hat the original
2b800 20 70 61 74 74 65 72 6e 20 73 74 72 69 6e 67 0a   pattern string.
2b810 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  ** does not need
2b820 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65   to be recompile
2b830 64 20 6f 6e 20 65 61 63 68 20 69 6e 76 6f 63 61  d on each invoca
2b840 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tion..**.** The 
2b850 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
2b860 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20  ata() interface 
2b870 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
2b880 72 20 74 6f 20 74 68 65 20 6d 65 74 61 64 61 74  r to the metadat
2b890 61 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20  a.** associated 
2b8a0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  by the sqlite3_s
2b8b0 65 74 5f 61 75 78 64 61 74 61 28 29 20 66 75 6e  et_auxdata() fun
2b8c0 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 4e  ction with the N
2b8d0 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 76  th argument.** v
2b8e0 61 6c 75 65 20 74 6f 20 74 68 65 20 61 70 70 6c  alue to the appl
2b8f0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2b900 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  function. If no 
2b910 6d 65 74 61 64 61 74 61 20 68 61 73 20 62 65 65  metadata has bee
2b920 6e 20 65 76 65 72 0a 2a 2a 20 62 65 65 6e 20 73  n ever.** been s
2b930 65 74 20 66 6f 72 20 74 68 65 20 4e 74 68 20 61  et for the Nth a
2b940 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 66  rgument of the f
2b950 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 69 66 20 74  unction, or if t
2b960 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
2b970 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 70 61 72  .** function par
2b980 61 6d 65 74 65 72 20 68 61 73 20 63 68 61 6e 67  ameter has chang
2b990 65 64 20 73 69 6e 63 65 20 74 68 65 20 6d 65 74  ed since the met
2b9a0 61 2d 64 61 74 61 20 77 61 73 20 73 65 74 2c 0a  a-data was set,.
2b9b0 2a 2a 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f  ** then sqlite3_
2b9c0 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 72 65  get_auxdata() re
2b9d0 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69  turns a NULL poi
2b9e0 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nter..**.** The 
2b9f0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
2ba00 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20  ata() interface 
2ba10 73 61 76 65 73 20 74 68 65 20 6d 65 74 61 64 61  saves the metada
2ba20 74 61 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f  ta.** pointed to
2ba30 20 62 79 20 69 74 73 20 33 72 64 20 70 61 72 61   by its 3rd para
2ba40 6d 65 74 65 72 20 61 73 20 74 68 65 20 6d 65 74  meter as the met
2ba50 61 64 61 74 61 20 66 6f 72 20 74 68 65 20 4e 2d  adata for the N-
2ba60 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6f  th.** argument o
2ba70 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  f the applicatio
2ba80 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
2ba90 6f 6e 2e 20 20 53 75 62 73 65 71 75 65 6e 74 0a  on.  Subsequent.
2baa0 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  ** calls to sqli
2bab0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
2bac0 29 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 74  ) might return t
2bad0 68 69 73 20 64 61 74 61 2c 20 69 66 20 69 74 20  his data, if it 
2bae0 68 61 73 0a 2a 2a 20 6e 6f 74 20 62 65 65 6e 20  has.** not been 
2baf0 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 20 49 66  destroyed..** If
2bb00 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   it is not NULL,
2bb10 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76   SQLite will inv
2bb20 6f 6b 65 20 74 68 65 20 64 65 73 74 72 75 63 74  oke the destruct
2bb30 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 67  or.** function g
2bb40 69 76 65 6e 20 62 79 20 74 68 65 20 34 74 68 20  iven by the 4th 
2bb50 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
2bb60 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
2bb70 28 29 20 6f 6e 0a 2a 2a 20 74 68 65 20 6d 65 74  () on.** the met
2bb80 61 64 61 74 61 20 77 68 65 6e 20 74 68 65 20 63  adata when the c
2bb90 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 66 75 6e  orresponding fun
2bba0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20  ction parameter 
2bbb0 63 68 61 6e 67 65 73 0a 2a 2a 20 6f 72 20 77 68  changes.** or wh
2bbc0 65 6e 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  en the SQL state
2bbd0 6d 65 6e 74 20 63 6f 6d 70 6c 65 74 65 73 2c 20  ment completes, 
2bbe0 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20  whichever comes 
2bbf0 66 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  first..**.** SQL
2bc00 69 74 65 20 69 73 20 66 72 65 65 20 74 6f 20 63  ite is free to c
2bc10 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74  all the destruct
2bc20 6f 72 20 61 6e 64 20 64 72 6f 70 20 6d 65 74 61  or and drop meta
2bc30 64 61 74 61 20 6f 6e 20 61 6e 79 0a 2a 2a 20 70  data on any.** p
2bc40 61 72 61 6d 65 74 65 72 20 6f 66 20 61 6e 79 20  arameter of any 
2bc50 66 75 6e 63 74 69 6f 6e 20 61 74 20 61 6e 79 20  function at any 
2bc60 74 69 6d 65 2e 20 20 54 68 65 20 6f 6e 6c 79 20  time.  The only 
2bc70 67 75 61 72 61 6e 74 65 65 20 69 73 20 74 68 61  guarantee is tha
2bc80 74 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 75 63  t.** the destruc
2bc90 74 6f 72 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  tor will be call
2bca0 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 6d 65  ed before the me
2bcb0 74 61 64 61 74 61 20 69 73 20 64 72 6f 70 70 65  tadata is droppe
2bcc0 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 70 72 61 63  d..**.** In prac
2bcd0 74 69 63 65 2c 20 6d 65 74 61 64 61 74 61 20 69  tice, metadata i
2bce0 73 20 70 72 65 73 65 72 76 65 64 20 62 65 74 77  s preserved betw
2bcf0 65 65 6e 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  een function cal
2bd00 6c 73 20 66 6f 72 0a 2a 2a 20 65 78 70 72 65 73  ls for.** expres
2bd10 73 69 6f 6e 73 20 74 68 61 74 20 61 72 65 20 63  sions that are c
2bd20 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d 70 69  onstant at compi
2bd30 6c 65 20 74 69 6d 65 2e 20 54 68 69 73 20 69 6e  le time. This in
2bd40 63 6c 75 64 65 73 20 6c 69 74 65 72 61 6c 0a 2a  cludes literal.*
2bd50 2a 20 76 61 6c 75 65 73 20 61 6e 64 20 53 51 4c  * values and SQL
2bd60 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a 0a 2a   variables..**.*
2bd70 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
2bd80 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
2bd90 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68  from the same th
2bda0 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a 2a 2a  read in which.**
2bdb0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
2bdc0 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a  n is running..**
2bdd0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
2bde0 3a 0a 2a 2a 20 5b 48 31 36 32 37 32 5d 20 5b 48  :.** [H16272] [H
2bdf0 31 36 32 37 34 5d 20 5b 48 31 36 32 37 36 5d 20  16274] [H16276] 
2be00 5b 48 31 36 32 37 37 5d 20 5b 48 31 36 32 37 38  [H16277] [H16278
2be10 5d 20 5b 48 31 36 32 37 39 5d 0a 2a 2f 0a 53 51  ] [H16279].*/.SQ
2be20 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
2be30 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61  qlite3_get_auxda
2be40 74 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ta(sqlite3_conte
2be50 78 74 2a 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c  xt*, int N);.SQL
2be60 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2be70 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
2be80 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2be90 2a 2c 20 69 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c  *, int N, void*,
2bea0 20 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 29   void (*)(void*)
2beb0 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  );.../*.** CAPI3
2bec0 52 45 46 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44  REF: Constants D
2bed0 65 66 69 6e 69 6e 67 20 53 70 65 63 69 61 6c 20  efining Special 
2bee0 44 65 73 74 72 75 63 74 6f 72 20 42 65 68 61 76  Destructor Behav
2bef0 69 6f 72 20 7b 48 31 30 32 38 30 7d 20 3c 53 33  ior {H10280} <S3
2bf00 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  0100>.**.** Thes
2bf10 65 20 61 72 65 20 73 70 65 63 69 61 6c 20 76 61  e are special va
2bf20 6c 75 65 73 20 66 6f 72 20 74 68 65 20 64 65 73  lues for the des
2bf30 74 72 75 63 74 6f 72 20 74 68 61 74 20 69 73 20  tructor that is 
2bf40 70 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65  passed in as the
2bf50 0a 2a 2a 20 66 69 6e 61 6c 20 61 72 67 75 6d 65  .** final argume
2bf60 6e 74 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c  nt to routines l
2bf70 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  ike [sqlite3_res
2bf80 75 6c 74 5f 62 6c 6f 62 28 29 5d 2e 20 20 49 66  ult_blob()].  If
2bf90 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 0a   the destructor.
2bfa0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53  ** argument is S
2bfb0 51 4c 49 54 45 5f 53 54 41 54 49 43 2c 20 69 74  QLITE_STATIC, it
2bfc0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2bfd0 63 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65 72 20  content pointer 
2bfe0 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61  is constant.** a
2bff0 6e 64 20 77 69 6c 6c 20 6e 65 76 65 72 20 63 68  nd will never ch
2c000 61 6e 67 65 2e 20 20 49 74 20 64 6f 65 73 20 6e  ange.  It does n
2c010 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 64 65  ot need to be de
2c020 73 74 72 6f 79 65 64 2e 20 20 54 68 65 0a 2a 2a  stroyed.  The.**
2c030 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
2c040 54 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 74 68  T value means th
2c050 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77  at the content w
2c060 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 68 61 6e 67  ill likely chang
2c070 65 20 69 6e 0a 2a 2a 20 74 68 65 20 6e 65 61 72  e in.** the near
2c080 20 66 75 74 75 72 65 20 61 6e 64 20 74 68 61 74   future and that
2c090 20 53 51 4c 69 74 65 20 73 68 6f 75 6c 64 20 6d   SQLite should m
2c0a0 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72 69 76  ake its own priv
2c0b0 61 74 65 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74  ate copy of.** t
2c0c0 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 66 6f 72  he content befor
2c0d0 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a  e returning..**.
2c0e0 2a 2a 20 54 68 65 20 74 79 70 65 64 65 66 20 69  ** The typedef i
2c0f0 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77  s necessary to w
2c100 6f 72 6b 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c  ork around probl
2c110 65 6d 73 20 69 6e 20 63 65 72 74 61 69 6e 0a 2a  ems in certain.*
2c120 2a 20 43 2b 2b 20 63 6f 6d 70 69 6c 65 72 73 2e  * C++ compilers.
2c130 20 20 53 65 65 20 74 69 63 6b 65 74 20 23 32 31    See ticket #21
2c140 39 31 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76  91..*/.typedef v
2c150 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 5f 64 65  oid (*sqlite3_de
2c160 73 74 72 75 63 74 6f 72 5f 74 79 70 65 29 28 76  structor_type)(v
2c170 6f 69 64 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53  oid*);.#define S
2c180 51 4c 49 54 45 5f 53 54 41 54 49 43 20 20 20 20  QLITE_STATIC    
2c190 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74    ((sqlite3_dest
2c1a0 72 75 63 74 6f 72 5f 74 79 70 65 29 30 29 0a 23  ructor_type)0).#
2c1b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 52  define SQLITE_TR
2c1c0 41 4e 53 49 45 4e 54 20 20 20 28 28 73 71 6c 69  ANSIENT   ((sqli
2c1d0 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74  te3_destructor_t
2c1e0 79 70 65 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43  ype)-1)../*.** C
2c1f0 41 50 49 33 52 45 46 3a 20 53 65 74 74 69 6e 67  API3REF: Setting
2c200 20 54 68 65 20 52 65 73 75 6c 74 20 4f 66 20 41   The Result Of A
2c210 6e 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 20 7b  n SQL Function {
2c220 48 31 36 34 30 30 7d 20 3c 53 32 30 32 30 30 3e  H16400} <S20200>
2c230 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
2c240 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 62  tines are used b
2c250 79 20 74 68 65 20 78 46 75 6e 63 20 6f 72 20 78  y the xFunc or x
2c260 46 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20  Final callbacks 
2c270 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  that.** implemen
2c280 74 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20  t SQL functions 
2c290 61 6e 64 20 61 67 67 72 65 67 61 74 65 73 2e 20  and aggregates. 
2c2a0 20 53 65 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   See.** [sqlite3
2c2b0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
2c2c0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
2c2d0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
2c2e0 31 36 28 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64  16()].** for add
2c2f0 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2c300 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ion..**.** These
2c310 20 66 75 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20   functions work 
2c320 76 65 72 79 20 6d 75 63 68 20 6c 69 6b 65 20 74  very much like t
2c330 68 65 20 5b 70 61 72 61 6d 65 74 65 72 20 62 69  he [parameter bi
2c340 6e 64 69 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66  nding] family of
2c350 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 75 73  .** functions us
2c360 65 64 20 74 6f 20 62 69 6e 64 20 76 61 6c 75 65  ed to bind value
2c370 73 20 74 6f 20 68 6f 73 74 20 70 61 72 61 6d 65  s to host parame
2c380 74 65 72 73 20 69 6e 20 70 72 65 70 61 72 65 64  ters in prepared
2c390 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20   statements..** 
2c3a0 52 65 66 65 72 20 74 6f 20 74 68 65 20 5b 53 51  Refer to the [SQ
2c3b0 4c 20 70 61 72 61 6d 65 74 65 72 5d 20 64 6f 63  L parameter] doc
2c3c0 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61  umentation for a
2c3d0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
2c3e0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ation..**.** The
2c3f0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2c400 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63 65  blob() interface
2c410 20 73 65 74 73 20 74 68 65 20 72 65 73 75 6c 74   sets the result
2c420 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c   from.** an appl
2c430 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2c440 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 74  function to be t
2c450 68 65 20 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f  he BLOB whose co
2c460 6e 74 65 6e 74 20 69 73 20 70 6f 69 6e 74 65 64  ntent is pointed
2c470 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20 73 65  .** to by the se
2c480 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61  cond parameter a
2c490 6e 64 20 77 68 69 63 68 20 69 73 20 4e 20 62 79  nd which is N by
2c4a0 74 65 73 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e  tes long where N
2c4b0 20 69 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64   is the.** third
2c4c0 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
2c4d0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2c4e0 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 20  sult_zeroblob() 
2c4f0 69 6e 74 65 72 66 61 63 65 73 20 73 65 74 20 74  interfaces set t
2c500 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20  he result of.** 
2c510 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
2c520 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2c530 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 63 6f   to be a BLOB co
2c540 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72  ntaining all zer
2c550 6f 0a 2a 2a 20 62 79 74 65 73 20 61 6e 64 20 4e  o.** bytes and N
2c560 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20   bytes in size, 
2c570 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 76  where N is the v
2c580 61 6c 75 65 20 6f 66 20 74 68 65 20 32 6e 64 20  alue of the 2nd 
2c590 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a  parameter..**.**
2c5a0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
2c5b0 75 6c 74 5f 64 6f 75 62 6c 65 28 29 20 69 6e 74  ult_double() int
2c5c0 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20  erface sets the 
2c5d0 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61  result from.** a
2c5e0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  n application-de
2c5f0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
2c600 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 20  o be a floating 
2c610 70 6f 69 6e 74 20 76 61 6c 75 65 20 73 70 65 63  point value spec
2c620 69 66 69 65 64 0a 2a 2a 20 62 79 20 69 74 73 20  ified.** by its 
2c630 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  2nd argument..**
2c640 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2c650 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61  result_error() a
2c660 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  nd sqlite3_resul
2c670 74 5f 65 72 72 6f 72 31 36 28 29 20 66 75 6e 63  t_error16() func
2c680 74 69 6f 6e 73 0a 2a 2a 20 63 61 75 73 65 20 74  tions.** cause t
2c690 68 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 53  he implemented S
2c6a0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74  QL function to t
2c6b0 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f  hrow an exceptio
2c6c0 6e 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65  n..** SQLite use
2c6d0 73 20 74 68 65 20 73 74 72 69 6e 67 20 70 6f 69  s the string poi
2c6e0 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 0a 2a  nted to by the.*
2c6f0 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  * 2nd parameter 
2c700 6f 66 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  of sqlite3_resul
2c710 74 5f 65 72 72 6f 72 28 29 20 6f 72 20 73 71 6c  t_error() or sql
2c720 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2c730 72 31 36 28 29 0a 2a 2a 20 61 73 20 74 68 65 20  r16().** as the 
2c740 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72 6f 72  text of an error
2c750 20 6d 65 73 73 61 67 65 2e 20 20 53 51 4c 69 74   message.  SQLit
2c760 65 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65  e interprets the
2c770 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67   error.** messag
2c780 65 20 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71  e string from sq
2c790 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2c7a0 6f 72 28 29 20 61 73 20 55 54 46 2d 38 2e 20 53  or() as UTF-8. S
2c7b0 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 70 72  QLite.** interpr
2c7c0 65 74 73 20 74 68 65 20 73 74 72 69 6e 67 20 66  ets the string f
2c7d0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 75  rom sqlite3_resu
2c7e0 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 61 73 20  lt_error16() as 
2c7f0 55 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65  UTF-16 in native
2c800 0a 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20  .** byte order. 
2c810 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61   If the third pa
2c820 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74  rameter to sqlit
2c830 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2c840 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f  ).** or sqlite3_
2c850 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29  result_error16()
2c860 20 69 73 20 6e 65 67 61 74 69 76 65 20 74 68 65   is negative the
2c870 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 61  n SQLite takes a
2c880 73 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d  s the error.** m
2c890 65 73 73 61 67 65 20 61 6c 6c 20 74 65 78 74 20  essage all text 
2c8a0 75 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 66  up through the f
2c8b0 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72 61 63  irst zero charac
2c8c0 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74  ter..** If the t
2c8d0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74  hird parameter t
2c8e0 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  o sqlite3_result
2c8f0 5f 65 72 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73  _error() or.** s
2c900 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2c910 72 6f 72 31 36 28 29 20 69 73 20 6e 6f 6e 2d 6e  ror16() is non-n
2c920 65 67 61 74 69 76 65 20 74 68 65 6e 20 53 51 4c  egative then SQL
2c930 69 74 65 20 74 61 6b 65 73 20 74 68 61 74 20 6d  ite takes that m
2c940 61 6e 79 0a 2a 2a 20 62 79 74 65 73 20 28 6e 6f  any.** bytes (no
2c950 74 20 63 68 61 72 61 63 74 65 72 73 29 20 66 72  t characters) fr
2c960 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d  om the 2nd param
2c970 65 74 65 72 20 61 73 20 74 68 65 20 65 72 72 6f  eter as the erro
2c980 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 20 54 68  r message..** Th
2c990 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
2c9a0 5f 65 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c  _error() and sql
2c9b0 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2c9c0 72 31 36 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  r16().** routine
2c9d0 73 20 6d 61 6b 65 20 61 20 70 72 69 76 61 74 65  s make a private
2c9e0 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 72 72   copy of the err
2c9f0 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 20  or message text 
2ca00 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 79 20 72  before.** they r
2ca10 65 74 75 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74  eturn.  Hence, t
2ca20 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
2ca30 69 6f 6e 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61  ion can dealloca
2ca40 74 65 20 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20  te or.** modify 
2ca50 74 68 65 20 74 65 78 74 20 61 66 74 65 72 20 74  the text after t
2ca60 68 65 79 20 72 65 74 75 72 6e 20 77 69 74 68 6f  hey return witho
2ca70 75 74 20 68 61 72 6d 2e 0a 2a 2a 20 54 68 65 20  ut harm..** The 
2ca80 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2ca90 72 72 6f 72 5f 63 6f 64 65 28 29 20 66 75 6e 63  rror_code() func
2caa0 74 69 6f 6e 20 63 68 61 6e 67 65 73 20 74 68 65  tion changes the
2cab0 20 65 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 72   error code.** r
2cac0 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 69 74  eturned by SQLit
2cad0 65 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f 66  e as a result of
2cae0 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 61 20 66   an error in a f
2caf0 75 6e 63 74 69 6f 6e 2e 20 20 42 79 20 64 65 66  unction.  By def
2cb00 61 75 6c 74 2c 0a 2a 2a 20 74 68 65 20 65 72 72  ault,.** the err
2cb10 6f 72 20 63 6f 64 65 20 69 73 20 53 51 4c 49 54  or code is SQLIT
2cb20 45 5f 45 52 52 4f 52 2e 20 20 41 20 73 75 62 73  E_ERROR.  A subs
2cb30 65 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73  equent call to s
2cb40 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2cb50 72 6f 72 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69  ror().** or sqli
2cb60 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2cb70 31 36 28 29 20 72 65 73 65 74 73 20 74 68 65 20  16() resets the 
2cb80 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51  error code to SQ
2cb90 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a  LITE_ERROR..**.*
2cba0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
2cbb0 73 75 6c 74 5f 74 6f 6f 62 69 67 28 29 20 69 6e  sult_toobig() in
2cbc0 74 65 72 66 61 63 65 20 63 61 75 73 65 73 20 53  terface causes S
2cbd0 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61  QLite to throw a
2cbe0 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63  n error.** indic
2cbf0 61 74 69 6e 67 20 74 68 61 74 20 61 20 73 74 72  ating that a str
2cc00 69 6e 67 20 6f 72 20 42 4c 4f 42 20 69 73 20 74  ing or BLOB is t
2cc10 6f 20 6c 6f 6e 67 20 74 6f 20 72 65 70 72 65 73  o long to repres
2cc20 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ent..**.** The s
2cc30 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 6f  qlite3_result_no
2cc40 6d 65 6d 28 29 20 69 6e 74 65 72 66 61 63 65 20  mem() interface 
2cc50 63 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f  causes SQLite to
2cc60 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a   throw an error.
2cc70 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  ** indicating th
2cc80 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  at a memory allo
2cc90 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a  cation failed..*
2cca0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2ccb0 5f 72 65 73 75 6c 74 5f 69 6e 74 28 29 20 69 6e  _result_int() in
2ccc0 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68 65  terface sets the
2ccd0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
2cce0 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   of the applicat
2ccf0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2cd00 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 33  tion to be the 3
2cd10 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  2-bit signed int
2cd20 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69  eger.** value gi
2cd30 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20 61  ven in the 2nd a
2cd40 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20  rgument..** The 
2cd50 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
2cd60 6e 74 36 34 28 29 20 69 6e 74 65 72 66 61 63 65  nt64() interface
2cd70 20 73 65 74 73 20 74 68 65 20 72 65 74 75 72 6e   sets the return
2cd80 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65   value.** of the
2cd90 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
2cda0 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ined function to
2cdb0 20 62 65 20 74 68 65 20 36 34 2d 62 69 74 20 73   be the 64-bit s
2cdc0 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
2cdd0 20 76 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20   value given in 
2cde0 74 68 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74  the 2nd argument
2cdf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2ce00 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28  te3_result_null(
2ce10 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74 73  ) interface sets
2ce20 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
2ce30 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70 70 6c  e.** of the appl
2ce40 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2ce50 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e  function to be N
2ce60 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ULL..**.** The s
2ce70 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2ce80 78 74 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65  xt(), sqlite3_re
2ce90 73 75 6c 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a  sult_text16(),.*
2cea0 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  * sqlite3_result
2ceb0 5f 74 65 78 74 31 36 6c 65 28 29 2c 20 61 6e 64  _text16le(), and
2cec0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2ced0 74 65 78 74 31 36 62 65 28 29 20 69 6e 74 65 72  text16be() inter
2cee0 66 61 63 65 73 0a 2a 2a 20 73 65 74 20 74 68 65  faces.** set the
2cef0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
2cf00 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2cf10 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
2cf20 6e 20 74 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78  n to be.** a tex
2cf30 74 20 73 74 72 69 6e 67 20 77 68 69 63 68 20 69  t string which i
2cf40 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 61 73  s represented as
2cf50 20 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e   UTF-8, UTF-16 n
2cf60 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
2cf70 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74  ,.** UTF-16 litt
2cf80 6c 65 20 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54  le endian, or UT
2cf90 46 2d 31 36 20 62 69 67 20 65 6e 64 69 61 6e 2c  F-16 big endian,
2cfa0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a   respectively..*
2cfb0 2a 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 74  * SQLite takes t
2cfc0 68 65 20 74 65 78 74 20 72 65 73 75 6c 74 20 66  he text result f
2cfd0 72 6f 6d 20 74 68 65 20 61 70 70 6c 69 63 61 74  rom the applicat
2cfe0 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ion from.** the 
2cff0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66  2nd parameter of
2d000 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   the sqlite3_res
2d010 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66  ult_text* interf
2d020 61 63 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20  aces..** If the 
2d030 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  3rd parameter to
2d040 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   the sqlite3_res
2d050 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66  ult_text* interf
2d060 61 63 65 73 0a 2a 2a 20 69 73 20 6e 65 67 61 74  aces.** is negat
2d070 69 76 65 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  ive, then SQLite
2d080 20 74 61 6b 65 73 20 72 65 73 75 6c 74 20 74 65   takes result te
2d090 78 74 20 66 72 6f 6d 20 74 68 65 20 32 6e 64 20  xt from the 2nd 
2d0a0 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74 68 72  parameter.** thr
2d0b0 6f 75 67 68 20 74 68 65 20 66 69 72 73 74 20 7a  ough the first z
2d0c0 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a  ero character..*
2d0d0 2a 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72  * If the 3rd par
2d0e0 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
2d0f0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2d100 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  t* interfaces.**
2d110 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65   is non-negative
2d120 2c 20 74 68 65 6e 20 61 73 20 6d 61 6e 79 20 62  , then as many b
2d130 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63  ytes (not charac
2d140 74 65 72 73 29 20 6f 66 20 74 68 65 20 74 65 78  ters) of the tex
2d150 74 0a 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20  t.** pointed to 
2d160 62 79 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d  by the 2nd param
2d170 65 74 65 72 20 61 72 65 20 74 61 6b 65 6e 20 61  eter are taken a
2d180 73 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  s the applicatio
2d190 6e 2d 64 65 66 69 6e 65 64 0a 2a 2a 20 66 75 6e  n-defined.** fun
2d1a0 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a  ction result..**
2d1b0 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72 61   If the 4th para
2d1c0 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c  meter to the sql
2d1d0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2d1e0 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  * interfaces.** 
2d1f0 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  or sqlite3_resul
2d200 74 5f 62 6c 6f 62 20 69 73 20 61 20 6e 6f 6e 2d  t_blob is a non-
2d210 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68  NULL pointer, th
2d220 65 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20  en SQLite calls 
2d230 74 68 61 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  that.** function
2d240 20 61 73 20 74 68 65 20 64 65 73 74 72 75 63 74   as the destruct
2d250 6f 72 20 6f 6e 20 74 68 65 20 74 65 78 74 20 6f  or on the text o
2d260 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20 77 68  r BLOB result wh
2d270 65 6e 20 69 74 20 68 61 73 0a 2a 2a 20 66 69 6e  en it has.** fin
2d280 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 61 74  ished using that
2d290 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74   result..** If t
2d2a0 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72  he 4th parameter
2d2b0 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f   to the sqlite3_
2d2c0 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74  result_text* int
2d2d0 65 72 66 61 63 65 73 20 6f 72 20 74 6f 0a 2a 2a  erfaces or to.**
2d2e0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2d2f0 62 6c 6f 62 20 69 73 20 74 68 65 20 73 70 65 63  blob is the spec
2d300 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c  ial constant SQL
2d310 49 54 45 5f 53 54 41 54 49 43 2c 20 74 68 65 6e  ITE_STATIC, then
2d320 20 53 51 4c 69 74 65 0a 2a 2a 20 61 73 73 75 6d   SQLite.** assum
2d330 65 73 20 74 68 61 74 20 74 68 65 20 74 65 78 74  es that the text
2d340 20 6f 72 20 42 4c 4f 42 20 72 65 73 75 6c 74 20   or BLOB result 
2d350 69 73 20 69 6e 20 63 6f 6e 73 74 61 6e 74 20 73  is in constant s
2d360 70 61 63 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f  pace and does no
2d370 74 0a 2a 2a 20 63 6f 70 79 20 74 68 65 20 63 6f  t.** copy the co
2d380 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 72  ntent of the par
2d390 61 6d 65 74 65 72 20 6e 6f 72 20 63 61 6c 6c 20  ameter nor call 
2d3a0 61 20 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20  a destructor on 
2d3b0 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 77  the content.** w
2d3c0 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69 73  hen it has finis
2d3d0 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72  hed using that r
2d3e0 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65  esult..** If the
2d3f0 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
2d400 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
2d410 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
2d420 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69  faces.** or sqli
2d430 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20  te3_result_blob 
2d440 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63  is the special c
2d450 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 54  onstant SQLITE_T
2d460 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65 6e  RANSIENT.** then
2d470 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20   SQLite makes a 
2d480 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75  copy of the resu
2d490 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62  lt into space ob
2d4a0 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 66  tained from.** f
2d4b0 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
2d4c0 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69 74  loc()] before it
2d4d0 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20   returns..**.** 
2d4e0 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
2d4f0 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72  lt_value() inter
2d500 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65  face sets the re
2d510 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61  sult of.** the a
2d520 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
2d530 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
2d540 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20  e a copy the.** 
2d550 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c  [unprotected sql
2d560 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
2d570 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ct specified by 
2d580 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
2d590 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74  r.  The.** sqlit
2d5a0 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
2d5b0 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b 65  ) interface make
2d5c0 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
2d5d0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 0a  [sqlite3_value].
2d5e0 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20 5b  ** so that the [
2d5f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 73  sqlite3_value] s
2d600 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
2d610 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63 68  parameter may ch
2d620 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64 65  ange or.** be de
2d630 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72 20  allocated after 
2d640 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76  sqlite3_result_v
2d650 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20 77  alue() returns w
2d660 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a 20  ithout harm..** 
2d670 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c  A [protected sql
2d680 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
2d690 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62 65  ct may always be
2d6a0 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a 2a   used where an.*
2d6b0 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73  * [unprotected s
2d6c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
2d6d0 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65 64  ject is required
2d6e0 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20 6b  , so either.** k
2d6f0 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f  ind of [sqlite3_
2d700 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63 61  value] object ca
2d710 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74  n be used with t
2d720 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a  his interface..*
2d730 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72 6f  *.** If these ro
2d740 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c 65  utines are calle
2d750 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68  d from within th
2d760 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65  e different thre
2d770 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 6f  ad.** than the o
2d780 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ne containing th
2d790 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
2d7a0 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
2d7b0 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a 20  hat received.** 
2d7c0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  the [sqlite3_con
2d7d0 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20 74  text] pointer, t
2d7e0 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75  he results are u
2d7f0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
2d800 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
2d810 20 5b 48 31 36 34 30 33 5d 20 5b 48 31 36 34 30   [H16403] [H1640
2d820 36 5d 20 5b 48 31 36 34 30 39 5d 20 5b 48 31 36  6] [H16409] [H16
2d830 34 31 32 5d 20 5b 48 31 36 34 31 35 5d 20 5b 48  412] [H16415] [H
2d840 31 36 34 31 38 5d 20 5b 48 31 36 34 32 31 5d 20  16418] [H16421] 
2d850 5b 48 31 36 34 32 34 5d 0a 2a 2a 20 5b 48 31 36  [H16424].** [H16
2d860 34 32 37 5d 20 5b 48 31 36 34 33 30 5d 20 5b 48  427] [H16430] [H
2d870 31 36 34 33 33 5d 20 5b 48 31 36 34 33 36 5d 20  16433] [H16436] 
2d880 5b 48 31 36 34 33 39 5d 20 5b 48 31 36 34 34 32  [H16439] [H16442
2d890 5d 20 5b 48 31 36 34 34 35 5d 20 5b 48 31 36 34  ] [H16445] [H164
2d8a0 34 38 5d 0a 2a 2a 20 5b 48 31 36 34 35 31 5d 20  48].** [H16451] 
2d8b0 5b 48 31 36 34 35 34 5d 20 5b 48 31 36 34 35 37  [H16454] [H16457
2d8c0 5d 20 5b 48 31 36 34 36 30 5d 20 5b 48 31 36 34  ] [H16460] [H164
2d8d0 36 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  63].*/.SQLITE_AP
2d8e0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2d8f0 65 73 75 6c 74 5f 62 6c 6f 62 28 73 71 6c 69 74  esult_blob(sqlit
2d900 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
2d910 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76  st void*, int, v
2d920 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
2d930 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2d940 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
2d950 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f  ouble(sqlite3_co
2d960 6e 74 65 78 74 2a 2c 20 64 6f 75 62 6c 65 29 3b  ntext*, double);
2d970 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2d980 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2d990 65 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f  error(sqlite3_co
2d9a0 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68  ntext*, const ch
2d9b0 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  ar*, int);.SQLIT
2d9c0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2d9d0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
2d9e0 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  6(sqlite3_contex
2d9f0 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  t*, const void*,
2da00 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50   int);.SQLITE_AP
2da10 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2da20 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
2da30 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ig(sqlite3_conte
2da40 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  xt*);.SQLITE_API
2da50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
2da60 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
2da70 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2da80 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  *);.SQLITE_API v
2da90 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
2daa0 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28 73 71  lt_error_code(sq
2dab0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2dac0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
2dad0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
2dae0 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33  sult_int(sqlite3
2daf0 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b  _context*, int);
2db00 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
2db10 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2db20 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f  int64(sqlite3_co
2db30 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33 5f  ntext*, sqlite3_
2db40 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45 5f 41  int64);.SQLITE_A
2db50 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2db60 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69  result_null(sqli
2db70 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53  te3_context*);.S
2db80 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2db90 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2dba0 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  xt(sqlite3_conte
2dbb0 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  xt*, const char*
2dbc0 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76  , int, void(*)(v
2dbd0 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41  oid*));.SQLITE_A
2dbe0 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2dbf0 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 73 71  result_text16(sq
2dc00 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2dc10 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
2dc20 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  , void(*)(void*)
2dc30 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
2dc40 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
2dc50 74 5f 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74  t_text16le(sqlit
2dc60 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
2dc70 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f  st void*, int,vo
2dc80 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53  id(*)(void*));.S
2dc90 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2dca0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
2dcb0 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f 63  xt16be(sqlite3_c
2dcc0 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76  ontext*, const v
2dcd0 6f 69 64 2a 2c 20 69 6e 74 2c 76 6f 69 64 28 2a  oid*, int,void(*
2dce0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
2dcf0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2dd00 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
2dd10 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
2dd20 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  , sqlite3_value*
2dd30 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
2dd40 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
2dd50 74 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74  t_zeroblob(sqlit
2dd60 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74  e3_context*, int
2dd70 20 6e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49   n);../*.** CAPI
2dd80 33 52 45 46 3a 20 44 65 66 69 6e 65 20 4e 65 77  3REF: Define New
2dd90 20 43 6f 6c 6c 61 74 69 6e 67 20 53 65 71 75 65   Collating Seque
2dda0 6e 63 65 73 20 7b 48 31 36 36 30 30 7d 20 3c 53  nces {H16600} <S
2ddb0 32 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20300>.**.** The
2ddc0 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  se functions are
2ddd0 20 75 73 65 64 20 74 6f 20 61 64 64 20 6e 65 77   used to add new
2dde0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2ddf0 6e 63 65 73 20 74 6f 20 74 68 65 0a 2a 2a 20 5b  nces to the.** [
2de00 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2de10 69 6f 6e 5d 20 73 70 65 63 69 66 69 65 64 20 61  ion] specified a
2de20 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
2de30 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
2de40 6e 61 6d 65 20 6f 66 20 74 68 65 20 6e 65 77 20  name of the new 
2de50 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2de60 63 65 20 69 73 20 73 70 65 63 69 66 69 65 64 20  ce is specified 
2de70 61 73 20 61 20 55 54 46 2d 38 20 73 74 72 69 6e  as a UTF-8 strin
2de80 67 0a 2a 2a 20 66 6f 72 20 73 71 6c 69 74 65 33  g.** for sqlite3
2de90 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2dea0 6e 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  n() and sqlite3_
2deb0 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
2dec0 5f 76 32 28 29 0a 2a 2a 20 61 6e 64 20 61 20 55  _v2().** and a U
2ded0 54 46 2d 31 36 20 73 74 72 69 6e 67 20 66 6f 72  TF-16 string for
2dee0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2def0 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 20 49  collation16(). I
2df00 6e 20 61 6c 6c 20 63 61 73 65 73 0a 2a 2a 20 74  n all cases.** t
2df10 68 65 20 6e 61 6d 65 20 69 73 20 70 61 73 73 65  he name is passe
2df20 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
2df30 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
2df40 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69  t..**.** The thi
2df50 72 64 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20  rd argument may 
2df60 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f  be one of the co
2df70 6e 73 74 61 6e 74 73 20 5b 53 51 4c 49 54 45 5f  nstants [SQLITE_
2df80 55 54 46 38 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54  UTF8],.** [SQLIT
2df90 45 5f 55 54 46 31 36 4c 45 5d 2c 20 6f 72 20 5b  E_UTF16LE], or [
2dfa0 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c  SQLITE_UTF16BE],
2dfb0 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
2dfc0 20 74 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69   the user-suppli
2dfd0 65 64 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 65 78  ed.** routine ex
2dfe0 70 65 63 74 73 20 74 6f 20 62 65 20 70 61 73 73  pects to be pass
2dff0 65 64 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73  ed pointers to s
2e000 74 72 69 6e 67 73 20 65 6e 63 6f 64 65 64 20 75  trings encoded u
2e010 73 69 6e 67 20 55 54 46 2d 38 2c 0a 2a 2a 20 55  sing UTF-8,.** U
2e020 54 46 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64  TF-16 little-end
2e030 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62  ian, or UTF-16 b
2e040 69 67 2d 65 6e 64 69 61 6e 2c 20 72 65 73 70 65  ig-endian, respe
2e050 63 74 69 76 65 6c 79 2e 20 54 68 65 0a 2a 2a 20  ctively. The.** 
2e060 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 6d  third argument m
2e070 69 67 68 74 20 61 6c 73 6f 20 62 65 20 5b 53 51  ight also be [SQ
2e080 4c 49 54 45 5f 55 54 46 31 36 5d 20 74 6f 20 69  LITE_UTF16] to i
2e090 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65  ndicate that the
2e0a0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 78 70 65   routine.** expe
2e0b0 63 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  cts pointers to 
2e0c0 62 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67  be UTF-16 string
2e0d0 73 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20  s in the native 
2e0e0 62 79 74 65 20 6f 72 64 65 72 2c 20 6f 72 20 74  byte order, or t
2e0f0 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 63  he.** argument c
2e100 61 6e 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54  an be [SQLITE_UT
2e110 46 31 36 5f 41 4c 49 47 4e 45 44 5d 20 69 66 20  F16_ALIGNED] if 
2e120 74 68 65 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69  the.** the routi
2e130 6e 65 20 65 78 70 65 63 74 73 20 70 6f 69 6e 74  ne expects point
2e140 65 72 73 20 74 6f 20 31 36 2d 62 69 74 20 77 6f  ers to 16-bit wo
2e150 72 64 20 61 6c 69 67 6e 65 64 20 73 74 72 69 6e  rd aligned strin
2e160 67 73 0a 2a 2a 20 6f 66 20 55 54 46 2d 31 36 20  gs.** of UTF-16 
2e170 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79  in the native by
2e180 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20  te order..**.** 
2e190 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
2e1a0 20 75 73 65 72 20 73 75 70 70 6c 69 65 64 20 72   user supplied r
2e1b0 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 70  outine must be p
2e1c0 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69 66  assed as the fif
2e1d0 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20  th.** argument. 
2e1e0 20 49 66 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20   If it is NULL, 
2e1f0 74 68 69 73 20 69 73 20 74 68 65 20 73 61 6d 65  this is the same
2e200 20 61 73 20 64 65 6c 65 74 69 6e 67 20 74 68 65   as deleting the
2e210 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65   collation.** se
2e220 71 75 65 6e 63 65 20 28 73 6f 20 74 68 61 74 20  quence (so that 
2e230 53 51 4c 69 74 65 20 63 61 6e 6e 6f 74 20 63 61  SQLite cannot ca
2e240 6c 6c 20 69 74 20 61 6e 79 6d 6f 72 65 29 2e 0a  ll it anymore)..
2e250 2a 2a 20 45 61 63 68 20 74 69 6d 65 20 74 68 65  ** Each time the
2e260 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 75 70   application sup
2e270 70 6c 69 65 64 20 66 75 6e 63 74 69 6f 6e 20 69  plied function i
2e280 73 20 69 6e 76 6f 6b 65 64 2c 20 69 74 20 69 73  s invoked, it is
2e290 20 70 61 73 73 65 64 0a 2a 2a 20 61 73 20 69 74   passed.** as it
2e2a0 73 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  s first paramete
2e2b0 72 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  r a copy of the 
2e2c0 76 6f 69 64 2a 20 70 61 73 73 65 64 20 61 73 20  void* passed as 
2e2d0 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d  the fourth argum
2e2e0 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65  ent.** to sqlite
2e2f0 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
2e300 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  on() or sqlite3_
2e310 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
2e320 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  16()..**.** The 
2e330 72 65 6d 61 69 6e 69 6e 67 20 61 72 67 75 6d 65  remaining argume
2e340 6e 74 73 20 74 6f 20 74 68 65 20 61 70 70 6c 69  nts to the appli
2e350 63 61 74 69 6f 6e 2d 73 75 70 70 6c 69 65 64 20  cation-supplied 
2e360 72 6f 75 74 69 6e 65 20 61 72 65 20 74 77 6f 20  routine are two 
2e370 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 65 61 63 68  strings,.** each
2e380 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20   represented by 
2e390 61 20 28 6c 65 6e 67 74 68 2c 20 64 61 74 61 29  a (length, data)
2e3a0 20 70 61 69 72 20 61 6e 64 20 65 6e 63 6f 64 65   pair and encode
2e3b0 64 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e  d in the encodin
2e3c0 67 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 70 61  g.** that was pa
2e3d0 73 73 65 64 20 61 73 20 74 68 65 20 74 68 69 72  ssed as the thir
2e3e0 64 20 61 72 67 75 6d 65 6e 74 20 77 68 65 6e 20  d argument when 
2e3f0 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
2e400 71 75 65 6e 63 65 20 77 61 73 0a 2a 2a 20 72 65  quence was.** re
2e410 67 69 73 74 65 72 65 64 2e 20 7b 45 4e 44 7d 20  gistered. {END} 
2e420 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   The application
2e430 20 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69   defined collati
2e440 6f 6e 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c  on routine shoul
2e450 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 65 67 61  d.** return nega
2e460 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f  tive, zero or po
2e470 73 69 74 69 76 65 20 69 66 20 74 68 65 20 66 69  sitive if the fi
2e480 72 73 74 20 73 74 72 69 6e 67 20 69 73 20 6c 65  rst string is le
2e490 73 73 20 74 68 61 6e 2c 0a 2a 2a 20 65 71 75 61  ss than,.** equa
2e4a0 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72  l to, or greater
2e4b0 20 74 68 61 6e 20 74 68 65 20 73 65 63 6f 6e 64   than the second
2e4c0 20 73 74 72 69 6e 67 2e 20 69 2e 65 2e 20 28 53   string. i.e. (S
2e4d0 54 52 49 4e 47 31 20 2d 20 53 54 52 49 4e 47 32  TRING1 - STRING2
2e4e0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  )..**.** The sql
2e4f0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
2e500 61 74 69 6f 6e 5f 76 32 28 29 20 77 6f 72 6b 73  ation_v2() works
2e510 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 63 72   like sqlite3_cr
2e520 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  eate_collation()
2e530 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20  .** except that 
2e540 69 74 20 74 61 6b 65 73 20 61 6e 20 65 78 74 72  it takes an extr
2e550 61 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68  a argument which
2e560 20 69 73 20 61 20 64 65 73 74 72 75 63 74 6f 72   is a destructor
2e570 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c   for.** the coll
2e580 61 74 69 6f 6e 2e 20 20 54 68 65 20 64 65 73 74  ation.  The dest
2e590 72 75 63 74 6f 72 20 69 73 20 63 61 6c 6c 65 64  ructor is called
2e5a0 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c 61 74   when the collat
2e5b0 69 6f 6e 20 69 73 0a 2a 2a 20 64 65 73 74 72 6f  ion is.** destro
2e5c0 79 65 64 20 61 6e 64 20 69 73 20 70 61 73 73 65  yed and is passe
2e5d0 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  d a copy of the 
2e5e0 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65 72  fourth parameter
2e5f0 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 0a 2a   void* pointer.*
2e600 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  * of the sqlite3
2e610 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2e620 6e 5f 76 32 28 29 2e 0a 2a 2a 20 43 6f 6c 6c 61  n_v2()..** Colla
2e630 74 69 6f 6e 73 20 61 72 65 20 64 65 73 74 72 6f  tions are destro
2e640 79 65 64 20 77 68 65 6e 20 74 68 65 79 20 61 72  yed when they ar
2e650 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20  e overridden by 
2e660 6c 61 74 65 72 20 63 61 6c 6c 73 20 74 6f 20 74  later calls to t
2e670 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20  he.** collation 
2e680 63 72 65 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  creation functio
2e690 6e 73 20 6f 72 20 77 68 65 6e 20 74 68 65 20 5b  ns or when the [
2e6a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2e6b0 69 6f 6e 5d 20 69 73 20 63 6c 6f 73 65 64 0a 2a  ion] is closed.*
2e6c0 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  * using [sqlite3
2e6d0 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a  _close()]..**.**
2e6e0 20 53 65 65 20 61 6c 73 6f 3a 20 20 5b 73 71 6c   See also:  [sql
2e6f0 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
2e700 65 65 64 65 64 28 29 5d 20 61 6e 64 20 5b 73 71  eeded()] and [sq
2e710 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
2e720 6e 65 65 64 65 64 31 36 28 29 5d 2e 0a 2a 2a 0a  needed16()]..**.
2e730 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2e740 0a 2a 2a 20 5b 48 31 36 36 30 33 5d 20 5b 48 31  .** [H16603] [H1
2e750 36 36 30 34 5d 20 5b 48 31 36 36 30 36 5d 20 5b  6604] [H16606] [
2e760 48 31 36 36 30 39 5d 20 5b 48 31 36 36 31 32 5d  H16609] [H16612]
2e770 20 5b 48 31 36 36 31 35 5d 20 5b 48 31 36 36 31   [H16615] [H1661
2e780 38 5d 20 5b 48 31 36 36 32 31 5d 0a 2a 2a 20 5b  8] [H16621].** [
2e790 48 31 36 36 32 34 5d 20 5b 48 31 36 36 32 37 5d  H16624] [H16627]
2e7a0 20 5b 48 31 36 36 33 30 5d 0a 2a 2f 0a 53 51 4c   [H16630].*/.SQL
2e7b0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2e7c0 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
2e7d0 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a  tion(.  sqlite3*
2e7e0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
2e7f0 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65  *zName, .  int e
2e800 54 65 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64  TextRep, .  void
2e810 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61  *,.  int(*xCompa
2e820 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
2e830 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
2e840 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 53 51  nst void*).);.SQ
2e850 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2e860 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
2e870 61 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69  ation_v2(.  sqli
2e880 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 63  te3*, .  const c
2e890 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69  har *zName, .  i
2e8a0 6e 74 20 65 54 65 78 74 52 65 70 2c 20 0a 20 20  nt eTextRep, .  
2e8b0 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28 2a 78 43  void*,.  int(*xC
2e8c0 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e  ompare)(void*,in
2e8d0 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
2e8e0 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a  t,const void*),.
2e8f0 20 20 76 6f 69 64 28 2a 78 44 65 73 74 72 6f 79    void(*xDestroy
2e900 29 28 76 6f 69 64 2a 29 0a 29 3b 0a 53 51 4c 49  )(void*).);.SQLI
2e910 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2e920 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2e930 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65 33  ion16(.  sqlite3
2e940 2a 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  *, .  const void
2e950 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 65   *zName,.  int e
2e960 54 65 78 74 52 65 70 2c 20 0a 20 20 76 6f 69 64  TextRep, .  void
2e970 2a 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61  *,.  int(*xCompa
2e980 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
2e990 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
2e9a0 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a 2f  nst void*).);../
2e9b0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
2e9c0 6f 6c 6c 61 74 69 6f 6e 20 4e 65 65 64 65 64 20  ollation Needed 
2e9d0 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 36 37 30  Callbacks {H1670
2e9e0 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a 0a 2a  0} <S20300>.**.*
2e9f0 2a 20 54 6f 20 61 76 6f 69 64 20 68 61 76 69 6e  * To avoid havin
2ea00 67 20 74 6f 20 72 65 67 69 73 74 65 72 20 61 6c  g to register al
2ea10 6c 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  l collation sequ
2ea20 65 6e 63 65 73 20 62 65 66 6f 72 65 20 61 20 64  ences before a d
2ea30 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 62  atabase.** can b
2ea40 65 20 75 73 65 64 2c 20 61 20 73 69 6e 67 6c 65  e used, a single
2ea50 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
2ea60 6f 6e 20 6d 61 79 20 62 65 20 72 65 67 69 73 74  on may be regist
2ea70 65 72 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a  ered with the.**
2ea80 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
2ea90 63 74 69 6f 6e 5d 20 74 6f 20 62 65 20 63 61 6c  ction] to be cal
2eaa0 6c 65 64 20 77 68 65 6e 65 76 65 72 20 61 6e 20  led whenever an 
2eab0 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74  undefined collat
2eac0 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ion.** sequence 
2ead0 69 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a  is required..**.
2eae0 2a 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69  ** If the functi
2eaf0 6f 6e 20 69 73 20 72 65 67 69 73 74 65 72 65 64  on is registered
2eb00 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74   using the sqlit
2eb10 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
2eb20 64 65 64 28 29 20 41 50 49 2c 0a 2a 2a 20 74 68  ded() API,.** th
2eb30 65 6e 20 69 74 20 69 73 20 70 61 73 73 65 64 20  en it is passed 
2eb40 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 75 6e 64  the names of und
2eb50 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  efined collation
2eb60 20 73 65 71 75 65 6e 63 65 73 20 61 73 20 73 74   sequences as st
2eb70 72 69 6e 67 73 0a 2a 2a 20 65 6e 63 6f 64 65 64  rings.** encoded
2eb80 20 69 6e 20 55 54 46 2d 38 2e 20 7b 48 31 36 37   in UTF-8. {H167
2eb90 30 33 7d 20 49 66 20 73 71 6c 69 74 65 33 5f 63  03} If sqlite3_c
2eba0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
2ebb0 36 28 29 20 69 73 20 75 73 65 64 2c 0a 2a 2a 20  6() is used,.** 
2ebc0 74 68 65 20 6e 61 6d 65 73 20 61 72 65 20 70 61  the names are pa
2ebd0 73 73 65 64 20 61 73 20 55 54 46 2d 31 36 20 69  ssed as UTF-16 i
2ebe0 6e 20 6d 61 63 68 69 6e 65 20 6e 61 74 69 76 65  n machine native
2ebf0 20 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 20   byte order..** 
2ec00 41 20 63 61 6c 6c 20 74 6f 20 65 69 74 68 65 72  A call to either
2ec10 20 66 75 6e 63 74 69 6f 6e 20 72 65 70 6c 61 63   function replac
2ec20 65 73 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20  es any existing 
2ec30 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  callback..**.** 
2ec40 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63  When the callbac
2ec50 6b 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 74 68  k is invoked, th
2ec60 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
2ec70 20 70 61 73 73 65 64 20 69 73 20 61 20 63 6f 70   passed is a cop
2ec80 79 0a 2a 2a 20 6f 66 20 74 68 65 20 73 65 63 6f  y.** of the seco
2ec90 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73  nd argument to s
2eca0 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2ecb0 5f 6e 65 65 64 65 64 28 29 20 6f 72 0a 2a 2a 20  _needed() or.** 
2ecc0 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
2ecd0 6e 5f 6e 65 65 64 65 64 31 36 28 29 2e 20 20 54  n_needed16().  T
2ece0 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
2ecf0 6e 74 20 69 73 20 74 68 65 20 64 61 74 61 62 61  nt is the databa
2ed00 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
2ed10 2e 20 20 54 68 65 20 74 68 69 72 64 20 61 72 67  .  The third arg
2ed20 75 6d 65 6e 74 20 69 73 20 6f 6e 65 20 6f 66 20  ument is one of 
2ed30 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 20 5b  [SQLITE_UTF8], [
2ed40 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2c  SQLITE_UTF16BE],
2ed50 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55  .** or [SQLITE_U
2ed60 54 46 31 36 4c 45 5d 2c 20 69 6e 64 69 63 61 74  TF16LE], indicat
2ed70 69 6e 67 20 74 68 65 20 6d 6f 73 74 20 64 65 73  ing the most des
2ed80 69 72 61 62 6c 65 20 66 6f 72 6d 20 6f 66 20 74  irable form of t
2ed90 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20  he collation.** 
2eda0 73 65 71 75 65 6e 63 65 20 66 75 6e 63 74 69 6f  sequence functio
2edb0 6e 20 72 65 71 75 69 72 65 64 2e 20 20 54 68 65  n required.  The
2edc0 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65   fourth paramete
2edd0 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  r is the name of
2ede0 20 74 68 65 0a 2a 2a 20 72 65 71 75 69 72 65 64   the.** required
2edf0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2ee00 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  nce..**.** The c
2ee10 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
2ee20 20 73 68 6f 75 6c 64 20 72 65 67 69 73 74 65 72   should register
2ee30 20 74 68 65 20 64 65 73 69 72 65 64 20 63 6f 6c   the desired col
2ee40 6c 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20  lation using.** 
2ee50 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
2ee60 63 6f 6c 6c 61 74 69 6f 6e 28 29 5d 2c 20 5b 73  collation()], [s
2ee70 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
2ee80 6c 6c 61 74 69 6f 6e 31 36 28 29 5d 2c 20 6f 72  llation16()], or
2ee90 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72 65  .** [sqlite3_cre
2eea0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
2eeb0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ()]..**.** Requi
2eec0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36  rements:.** [H16
2eed0 37 30 32 5d 20 5b 48 31 36 37 30 34 5d 20 5b 48  702] [H16704] [H
2eee0 31 36 37 30 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45  16706].*/.SQLITE
2eef0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2ef00 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
2ef10 64 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  d(.  sqlite3*, .
2ef20 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69 64    void*, .  void
2ef30 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
2ef40 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
2ef50 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b 0a  const char*).);.
2ef60 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2ef70 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2ef80 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c  _needed16(.  sql
2ef90 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c  ite3*, .  void*,
2efa0 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  .  void(*)(void*
2efb0 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54  ,sqlite3*,int eT
2efc0 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69  extRep,const voi
2efd0 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 70  d*).);../*.** Sp
2efe0 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66 6f  ecify the key fo
2eff0 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64  r an encrypted d
2f000 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 72  atabase.  This r
2f010 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65  outine should be
2f020 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68 74  .** called right
2f030 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f 6f   after sqlite3_o
2f040 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  pen()..**.** The
2f050 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65   code to impleme
2f060 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20 6e  nt this API is n
2f070 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20  ot available in 
2f080 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61  the public relea
2f090 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e  se.** of SQLite.
2f0a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2f0b0 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 0a  nt sqlite3_key(.
2f0c0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2f0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f0e0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20   /* Database to 
2f0f0 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20  be rekeyed */.  
2f100 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
2f110 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f  , int nKey     /
2f120 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b 0a  * The key */.);.
2f130 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
2f140 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65 6e  e key on an open
2f150 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 74   database.  If t
2f160 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 62  he current datab
2f170 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 6e  ase is not.** en
2f180 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72 6f  crypted, this ro
2f190 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72 79  utine will encry
2f1a0 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77 3d  pt it.  If pNew=
2f1b0 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20 74  =0 or nNew==0, t
2f1c0 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69  he.** database i
2f1d0 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a 0a  s decrypted..**.
2f1e0 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20 69  ** The code to i
2f1f0 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41 50  mplement this AP
2f200 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  I is not availab
2f210 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69 63  le in the public
2f220 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20 53   release.** of S
2f230 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  QLite..*/.SQLITE
2f240 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2f250 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65  _rekey(.  sqlite
2f260 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2f270 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2f280 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79  base to be rekey
2f290 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  ed */.  const vo
2f2a0 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
2f2b0 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65  ey     /* The ne
2f2c0 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  w key */.);../*.
2f2d0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 75 73  ** CAPI3REF: Sus
2f2e0 70 65 6e 64 20 45 78 65 63 75 74 69 6f 6e 20 46  pend Execution F
2f2f0 6f 72 20 41 20 53 68 6f 72 74 20 54 69 6d 65 20  or A Short Time 
2f300 7b 48 31 30 35 33 30 7d 20 3c 53 34 30 34 31 30  {H10530} <S40410
2f310 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
2f320 74 65 33 5f 73 6c 65 65 70 28 29 20 66 75 6e 63  te3_sleep() func
2f330 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65 20  tion causes the 
2f340 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 74  current thread t
2f350 6f 20 73 75 73 70 65 6e 64 20 65 78 65 63 75 74  o suspend execut
2f360 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c 65  ion.** for at le
2f370 61 73 74 20 61 20 6e 75 6d 62 65 72 20 6f 66 20  ast a number of 
2f380 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70 65  milliseconds spe
2f390 63 69 66 69 65 64 20 69 6e 20 69 74 73 20 70 61  cified in its pa
2f3a0 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49  rameter..**.** I
2f3b0 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20  f the operating 
2f3c0 73 79 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74 20  system does not 
2f3d0 73 75 70 70 6f 72 74 20 73 6c 65 65 70 20 72 65  support sleep re
2f3e0 71 75 65 73 74 73 20 77 69 74 68 0a 2a 2a 20 6d  quests with.** m
2f3f0 69 6c 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65 20  illisecond time 
2f400 72 65 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65 6e  resolution, then
2f410 20 74 68 65 20 74 69 6d 65 20 77 69 6c 6c 20 62   the time will b
2f420 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f 0a  e rounded up to.
2f430 2a 2a 20 74 68 65 20 6e 65 61 72 65 73 74 20 73  ** the nearest s
2f440 65 63 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62 65  econd. The numbe
2f450 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e 64  r of millisecond
2f460 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61  s of sleep actua
2f470 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  lly.** requested
2f480 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 72 61 74   from the operat
2f490 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 72 65  ing system is re
2f4a0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51  turned..**.** SQ
2f4b0 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  Lite implements 
2f4c0 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 62  this interface b
2f4d0 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78 53  y calling the xS
2f4e0 6c 65 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f 64  leep().** method
2f4f0 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
2f500 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
2f510 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ject..**.** Requ
2f520 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 33  irements: [H1053
2f530 33 5d 20 5b 48 31 30 35 33 36 5d 0a 2a 2f 0a 53  3] [H10536].*/.S
2f540 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2f550 6c 69 74 65 33 5f 73 6c 65 65 70 28 69 6e 74 29  lite3_sleep(int)
2f560 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2f570 46 3a 20 4e 61 6d 65 20 4f 66 20 54 68 65 20 46  F: Name Of The F
2f580 6f 6c 64 65 72 20 48 6f 6c 64 69 6e 67 20 54 65  older Holding Te
2f590 6d 70 6f 72 61 72 79 20 46 69 6c 65 73 20 7b 48  mporary Files {H
2f5a0 31 30 33 31 30 7d 20 3c 53 32 30 30 30 30 3e 0a  10310} <S20000>.
2f5b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 67 6c  **.** If this gl
2f5c0 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73  obal variable is
2f5d0 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74   made to point t
2f5e0 6f 20 61 20 73 74 72 69 6e 67 20 77 68 69 63 68  o a string which
2f5f0 20 69 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20   is.** the name 
2f600 6f 66 20 61 20 66 6f 6c 64 65 72 20 28 61 2e 6b  of a folder (a.k
2f610 2e 61 2e 20 64 69 72 65 63 74 6f 72 79 29 2c 20  .a. directory), 
2f620 74 68 65 6e 20 61 6c 6c 20 74 65 6d 70 6f 72 61  then all tempora
2f630 72 79 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65 61  ry files.** crea
2f640 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 77 69  ted by SQLite wi
2f650 6c 6c 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20  ll be placed in 
2f660 74 68 61 74 20 64 69 72 65 63 74 6f 72 79 2e 20  that directory. 
2f670 20 49 66 20 74 68 69 73 20 76 61 72 69 61 62 6c   If this variabl
2f680 65 0a 2a 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70  e.** is a NULL p
2f690 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c  ointer, then SQL
2f6a0 69 74 65 20 70 65 72 66 6f 72 6d 73 20 61 20 73  ite performs a s
2f6b0 65 61 72 63 68 20 66 6f 72 20 61 6e 20 61 70 70  earch for an app
2f6c0 72 6f 70 72 69 61 74 65 0a 2a 2a 20 74 65 6d 70  ropriate.** temp
2f6d0 6f 72 61 72 79 20 66 69 6c 65 20 64 69 72 65 63  orary file direc
2f6e0 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  tory..**.** It i
2f6f0 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65  s not safe to re
2f700 61 64 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69  ad or modify thi
2f710 73 20 76 61 72 69 61 62 6c 65 20 69 6e 20 6d 6f  s variable in mo
2f720 72 65 20 74 68 61 6e 20 6f 6e 65 0a 2a 2a 20 74  re than one.** t
2f730 68 72 65 61 64 20 61 74 20 61 20 74 69 6d 65 2e  hread at a time.
2f740 20 20 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65    It is not safe
2f750 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f 64 69   to read or modi
2f760 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  fy this variable
2f770 0a 2a 2a 20 69 66 20 61 20 5b 64 61 74 61 62 61  .** if a [databa
2f780 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69  se connection] i
2f790 73 20 62 65 69 6e 67 20 75 73 65 64 20 61 74 20  s being used at 
2f7a0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 69 6e  the same time in
2f7b0 20 61 20 73 65 70 61 72 61 74 65 0a 2a 2a 20 74   a separate.** t
2f7c0 68 72 65 61 64 2e 0a 2a 2a 20 49 74 20 69 73 20  hread..** It is 
2f7d0 69 6e 74 65 6e 64 65 64 20 74 68 61 74 20 74 68  intended that th
2f7e0 69 73 20 76 61 72 69 61 62 6c 65 20 62 65 20 73  is variable be s
2f7f0 65 74 20 6f 6e 63 65 0a 2a 2a 20 61 73 20 70 61  et once.** as pa
2f800 72 74 20 6f 66 20 70 72 6f 63 65 73 73 20 69 6e  rt of process in
2f810 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 6e 64  itialization and
2f820 20 62 65 66 6f 72 65 20 61 6e 79 20 53 51 4c 69   before any SQLi
2f830 74 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  te interface.** 
2f840 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 62 65  routines have be
2f850 65 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20 74 68  en called and th
2f860 61 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  at this variable
2f870 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65   remain unchange
2f880 64 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e  d.** thereafter.
2f890 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 74 65 6d 70  .**.** The [temp
2f8a0 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79  _store_directory
2f8b0 20 70 72 61 67 6d 61 5d 20 6d 61 79 20 6d 6f 64   pragma] may mod
2f8c0 69 66 79 20 74 68 69 73 20 76 61 72 69 61 62 6c  ify this variabl
2f8d0 65 20 61 6e 64 20 63 61 75 73 65 0a 2a 2a 20 69  e and cause.** i
2f8e0 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 6d 65  t to point to me
2f8f0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
2f900 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  om [sqlite3_mall
2f910 6f 63 5d 2e 20 20 46 75 72 74 68 65 72 6d 6f 72  oc].  Furthermor
2f920 65 2c 0a 2a 2a 20 74 68 65 20 5b 74 65 6d 70 5f  e,.** the [temp_
2f930 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20  store_directory 
2f940 70 72 61 67 6d 61 5d 20 61 6c 77 61 79 73 20 61  pragma] always a
2f950 73 73 75 6d 65 73 20 74 68 61 74 20 61 6e 79 20  ssumes that any 
2f960 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74  string.** that t
2f970 68 69 73 20 76 61 72 69 61 62 6c 65 20 70 6f 69  his variable poi
2f980 6e 74 73 20 74 6f 20 69 73 20 68 65 6c 64 20 69  nts to is held i
2f990 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  n memory obtaine
2f9a0 64 20 66 72 6f 6d 20 0a 2a 2a 20 5b 73 71 6c 69  d from .** [sqli
2f9b0 74 65 33 5f 6d 61 6c 6c 6f 63 5d 20 61 6e 64 20  te3_malloc] and 
2f9c0 74 68 65 20 70 72 61 67 6d 61 20 6d 61 79 20 61  the pragma may a
2f9d0 74 74 65 6d 70 74 20 74 6f 20 66 72 65 65 20 74  ttempt to free t
2f9e0 68 61 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 75 73  hat memory.** us
2f9f0 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66 72 65  ing [sqlite3_fre
2fa00 65 5d 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 69 66  e]..** Hence, if
2fa10 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20 69   this variable i
2fa20 73 20 6d 6f 64 69 66 69 65 64 20 64 69 72 65 63  s modified direc
2fa30 74 6c 79 2c 20 65 69 74 68 65 72 20 69 74 20 73  tly, either it s
2fa40 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 6d 61 64 65  hould be.** made
2fa50 20 4e 55 4c 4c 20 6f 72 20 6d 61 64 65 20 74 6f   NULL or made to
2fa60 20 70 6f 69 6e 74 20 74 6f 20 6d 65 6d 6f 72 79   point to memory
2fa70 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b   obtained from [
2fa80 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 5d 0a  sqlite3_malloc].
2fa90 2a 2a 20 6f 72 20 65 6c 73 65 20 74 68 65 20 75  ** or else the u
2faa0 73 65 20 6f 66 20 74 68 65 20 5b 74 65 6d 70 5f  se of the [temp_
2fab0 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20  store_directory 
2fac0 70 72 61 67 6d 61 5d 20 73 68 6f 75 6c 64 20 62  pragma] should b
2fad0 65 20 61 76 6f 69 64 65 64 2e 0a 2a 2f 0a 53 51  e avoided..*/.SQ
2fae0 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
2faf0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
2fb00 63 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ctory;../*.** CA
2fb10 50 49 33 52 45 46 3a 20 54 65 73 74 20 46 6f 72  PI3REF: Test For
2fb20 20 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d 6f 64   Auto-Commit Mod
2fb30 65 20 7b 48 31 32 39 33 30 7d 20 3c 53 36 30 32  e {H12930} <S602
2fb40 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  00>.** KEYWORDS:
2fb50 20 7b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64   {autocommit mod
2fb60 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  e}.**.** The sql
2fb70 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d  ite3_get_autocom
2fb80 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 20  mit() interface 
2fb90 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f  returns non-zero
2fba0 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 69 66 20 74   or.** zero if t
2fbb0 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61 73  he given databas
2fbc0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
2fbd0 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75 74  or is not in aut
2fbe0 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a 2a 2a  ocommit mode,.**
2fbf0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20   respectively.  
2fc00 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20  Autocommit mode 
2fc10 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74  is on by default
2fc20 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20  ..** Autocommit 
2fc30 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c 65 64  mode is disabled
2fc40 20 62 79 20 61 20 5b 42 45 47 49 4e 5d 20 73 74   by a [BEGIN] st
2fc50 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75 74 6f  atement..** Auto
2fc60 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 72  commit mode is r
2fc70 65 2d 65 6e 61 62 6c 65 64 20 62 79 20 61 20 5b  e-enabled by a [
2fc80 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f 4c 4c  COMMIT] or [ROLL
2fc90 42 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  BACK]..**.** If 
2fca0 63 65 72 74 61 69 6e 20 6b 69 6e 64 73 20 6f 66  certain kinds of
2fcb0 20 65 72 72 6f 72 73 20 6f 63 63 75 72 20 6f 6e   errors occur on
2fcc0 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74   a statement wit
2fcd0 68 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74 61 74  hin a multi-stat
2fce0 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63  ement.** transac
2fcf0 74 69 6f 6e 20 28 65 72 72 6f 72 73 20 69 6e 63  tion (errors inc
2fd00 6c 75 64 69 6e 67 20 5b 53 51 4c 49 54 45 5f 46  luding [SQLITE_F
2fd10 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f 49 4f  ULL], [SQLITE_IO
2fd20 45 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45  ERR],.** [SQLITE
2fd30 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45  _NOMEM], [SQLITE
2fd40 5f 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53 51 4c  _BUSY], and [SQL
2fd50 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d 29 20  ITE_INTERRUPT]) 
2fd60 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72 61 6e  then the.** tran
2fd70 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20 62 65  saction might be
2fd80 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 75 74   rolled back aut
2fd90 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54 68 65  omatically.  The
2fda0 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a 2a 20   only way to.** 
2fdb0 66 69 6e 64 20 6f 75 74 20 77 68 65 74 68 65 72  find out whether
2fdc0 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69   SQLite automati
2fdd0 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63  cally rolled bac
2fde0 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  k the transactio
2fdf0 6e 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20 65 72  n after.** an er
2fe00 72 6f 72 20 69 73 20 74 6f 20 75 73 65 20 74 68  ror is to use th
2fe10 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  is function..**.
2fe20 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  ** If another th
2fe30 72 65 61 64 20 63 68 61 6e 67 65 73 20 74 68 65  read changes the
2fe40 20 61 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74   autocommit stat
2fe50 75 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  us of the databa
2fe60 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
2fe70 20 77 68 69 6c 65 20 74 68 69 73 20 72 6f 75 74   while this rout
2fe80 69 6e 65 20 69 73 20 72 75 6e 6e 69 6e 67 2c 20  ine is running, 
2fe90 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 20  then the return 
2fea0 76 61 6c 75 65 0a 2a 2a 20 69 73 20 75 6e 64 65  value.** is unde
2feb0 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  fined..**.** Req
2fec0 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 32 39  uirements: [H129
2fed0 33 31 5d 20 5b 48 31 32 39 33 32 5d 20 5b 48 31  31] [H12932] [H1
2fee0 32 39 33 33 5d 20 5b 48 31 32 39 33 34 5d 0a 2a  2933] [H12934].*
2fef0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2ff00 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74   sqlite3_get_aut
2ff10 6f 63 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 2a  ocommit(sqlite3*
2ff20 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
2ff30 45 46 3a 20 46 69 6e 64 20 54 68 65 20 44 61 74  EF: Find The Dat
2ff40 61 62 61 73 65 20 48 61 6e 64 6c 65 20 4f 66 20  abase Handle Of 
2ff50 41 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65  A Prepared State
2ff60 6d 65 6e 74 20 7b 48 31 33 31 32 30 7d 20 3c 53  ment {H13120} <S
2ff70 36 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  60600>.**.** The
2ff80 20 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64   sqlite3_db_hand
2ff90 6c 65 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  le interface ret
2ffa0 75 72 6e 73 20 74 68 65 20 5b 64 61 74 61 62 61  urns the [databa
2ffb0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68  se connection] h
2ffc0 61 6e 64 6c 65 0a 2a 2a 20 74 6f 20 77 68 69 63  andle.** to whic
2ffd0 68 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  h a [prepared st
2ffe0 61 74 65 6d 65 6e 74 5d 20 62 65 6c 6f 6e 67 73  atement] belongs
2fff0 2e 20 20 54 68 65 20 5b 64 61 74 61 62 61 73 65  .  The [database
30000 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20   connection].** 
30010 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
30020 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 73  te3_db_handle is
30030 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62   the same [datab
30040 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
30050 74 68 61 74 20 77 61 73 20 74 68 65 20 66 69 72  that was the fir
30060 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  st argument.** t
30070 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 70  o the [sqlite3_p
30080 72 65 70 61 72 65 5f 76 32 28 29 5d 20 63 61 6c  repare_v2()] cal
30090 6c 20 28 6f 72 20 69 74 73 20 76 61 72 69 61 6e  l (or its varian
300a0 74 73 29 20 74 68 61 74 20 77 61 73 20 75 73 65  ts) that was use
300b0 64 20 74 6f 0a 2a 2a 20 63 72 65 61 74 65 20 74  d to.** create t
300c0 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  he statement in 
300d0 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e  the first place.
300e0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
300f0 6e 74 73 3a 20 5b 48 31 33 31 32 33 5d 0a 2a 2f  nts: [H13123].*/
30100 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
30110 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f  te3 *sqlite3_db_
30120 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 73  handle(sqlite3_s
30130 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tmt*);../*.** CA
30140 50 49 33 52 45 46 3a 20 46 69 6e 64 20 74 68 65  PI3REF: Find the
30150 20 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73   next prepared s
30160 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 31 34 30  tatement {H13140
30170 7d 20 3c 53 36 30 36 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60600>.**.**
30180 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
30190 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
301a0 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 5b 70  r to the next [p
301b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
301c0 74 5d 20 61 66 74 65 72 0a 2a 2a 20 70 53 74 6d  t] after.** pStm
301d0 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  t associated wit
301e0 68 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  h the [database 
301f0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 44 62 2e  connection] pDb.
30200 20 20 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55    If pStmt is NU
30210 4c 4c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20  LL.** then this 
30220 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
30230 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
30240 68 65 20 66 69 72 73 74 20 70 72 65 70 61 72 65  he first prepare
30250 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61  d statement.** a
30260 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
30270 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
30280 65 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20  ection pDb.  If 
30290 6e 6f 20 70 72 65 70 61 72 65 64 20 73 74 61 74  no prepared stat
302a0 65 6d 65 6e 74 0a 2a 2a 20 73 61 74 69 73 66 69  ement.** satisfi
302b0 65 73 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  es the condition
302c0 73 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e  s of this routin
302d0 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  e, it returns NU
302e0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 64  LL..**.** The [d
302f0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
30300 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20 69 6e  on] pointer D in
30310 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73   a call to.** [s
30320 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74  qlite3_next_stmt
30330 28 44 2c 53 29 5d 20 6d 75 73 74 20 72 65 66 65  (D,S)] must refe
30340 72 20 74 6f 20 61 6e 20 6f 70 65 6e 20 64 61 74  r to an open dat
30350 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74  abase.** connect
30360 69 6f 6e 20 61 6e 64 20 69 6e 20 70 61 72 74 69  ion and in parti
30370 63 75 6c 61 72 20 6d 75 73 74 20 6e 6f 74 20 62  cular must not b
30380 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  e a NULL pointer
30390 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
303a0 65 6e 74 73 3a 20 5b 48 31 33 31 34 33 5d 20 5b  ents: [H13143] [
303b0 48 31 33 31 34 36 5d 20 5b 48 31 33 31 34 39 5d  H13146] [H13149]
303c0 20 5b 48 31 33 31 35 32 5d 0a 2a 2f 0a 53 51 4c   [H13152].*/.SQL
303d0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
303e0 73 74 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65  stmt *sqlite3_ne
303f0 78 74 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20  xt_stmt(sqlite3 
30400 2a 70 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74  *pDb, sqlite3_st
30410 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a  mt *pStmt);../*.
30420 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d  ** CAPI3REF: Com
30430 6d 69 74 20 41 6e 64 20 52 6f 6c 6c 62 61 63 6b  mit And Rollback
30440 20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 43 61   Notification Ca
30450 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 35 30 7d  llbacks {H12950}
30460 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S60400>.**.** 
30470 54 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d  The sqlite3_comm
30480 69 74 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66  it_hook() interf
30490 61 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20  ace registers a 
304a0 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63  callback.** func
304b0 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b  tion to be invok
304c0 65 64 20 77 68 65 6e 65 76 65 72 20 61 20 74 72  ed whenever a tr
304d0 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 5b 43 4f  ansaction is [CO
304e0 4d 4d 49 54 20 7c 20 63 6f 6d 6d 69 74 74 65 64  MMIT | committed
304f0 5d 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61  ]..** Any callba
30500 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76  ck set by a prev
30510 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c  ious call to sql
30520 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
30530 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61  ().** for the sa
30540 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
30550 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69  ection is overri
30560 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  dden..** The sql
30570 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f  ite3_rollback_ho
30580 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ok() interface r
30590 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62  egisters a callb
305a0 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  ack.** function 
305b0 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68  to be invoked wh
305c0 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61 63  enever a transac
305d0 74 69 6f 6e 20 69 73 20 5b 52 4f 4c 4c 42 41 43  tion is [ROLLBAC
305e0 4b 20 7c 20 72 6f 6c 6c 65 64 20 62 61 63 6b 5d  K | rolled back]
305f0 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63  ..** Any callbac
30600 6b 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69  k set by a previ
30610 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ous call to sqli
30620 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
30630 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 61 6d  ).** for the sam
30640 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
30650 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64  ction is overrid
30660 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41 72 67  den..** The pArg
30670 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70 61 73   argument is pas
30680 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 74  sed through to t
30690 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20  he callback..** 
306a0 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  If the callback 
306b0 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b  on a commit hook
306c0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
306d0 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74  s non-zero,.** t
306e0 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 69  hen the commit i
306f0 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  s converted into
30700 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a   a rollback..**.
30710 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75  ** If another fu
30720 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69  nction was previ
30730 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64  ously registered
30740 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20 76 61  , its.** pArg va
30750 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  lue is returned.
30760 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c    Otherwise NULL
30770 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
30780 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b  .** The callback
30790 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
307a0 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74  must not do anyt
307b0 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d  hing that will m
307c0 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74  odify.** the dat
307d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
307e0 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68   that invoked th
307f0 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41 6e 79  e callback.  Any
30800 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d   actions.** to m
30810 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61  odify the databa
30820 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75  se connection mu
30830 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75  st be deferred u
30840 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a  ntil after the.*
30850 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20  * completion of 
30860 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  the [sqlite3_ste
30870 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74  p()] call that t
30880 72 69 67 67 65 72 65 64 20 74 68 65 20 63 6f 6d  riggered the com
30890 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 62 61  mit.** or rollba
308a0 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65 20 66  ck hook in the f
308b0 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a 20 4e  irst place..** N
308c0 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65  ote that [sqlite
308d0 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
308e0 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  and [sqlite3_ste
308f0 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79  p()] both modify
30900 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61   their.** databa
30910 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66  se connections f
30920 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f  or the meaning o
30930 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68  f "modify" in th
30940 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a  is paragraph..**
30950 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e 67 20  .** Registering 
30960 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f 6e 20  a NULL function 
30970 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 61 6c  disables the cal
30980 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  lback..**.** Whe
30990 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f  n the commit hoo
309a0 6b 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69  k callback routi
309b0 6e 65 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c  ne returns zero,
309c0 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a   the [COMMIT].**
309d0 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6c   operation is al
309e0 6c 6f 77 65 64 20 74 6f 20 63 6f 6e 74 69 6e 75  lowed to continu
309f0 65 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 49 66 20  e normally.  If 
30a00 74 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 0a  the commit hook.
30a10 2a 2a 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  ** returns non-z
30a20 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 5b 43  ero, then the [C
30a30 4f 4d 4d 49 54 5d 20 69 73 20 63 6f 6e 76 65 72  OMMIT] is conver
30a40 74 65 64 20 69 6e 74 6f 20 61 20 5b 52 4f 4c 4c  ted into a [ROLL
30a50 42 41 43 4b 5d 2e 0a 2a 2a 20 54 68 65 20 72 6f  BACK]..** The ro
30a60 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20 69 73 20 69  llback hook is i
30a70 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 72 6f 6c 6c  nvoked on a roll
30a80 62 61 63 6b 20 74 68 61 74 20 72 65 73 75 6c 74  back that result
30a90 73 20 66 72 6f 6d 20 61 20 63 6f 6d 6d 69 74 0a  s from a commit.
30aa0 2a 2a 20 68 6f 6f 6b 20 72 65 74 75 72 6e 69 6e  ** hook returnin
30ab0 67 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6a 75 73 74  g non-zero, just
30ac0 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20   as it would be 
30ad0 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 72  with any other r
30ae0 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46  ollback..**.** F
30af0 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20  or the purposes 
30b00 6f 66 20 74 68 69 73 20 41 50 49 2c 20 61 20 74  of this API, a t
30b10 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61  ransaction is sa
30b20 69 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a  id to have been.
30b30 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69  ** rolled back i
30b40 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52  f an explicit "R
30b50 4f 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65  OLLBACK" stateme
30b60 6e 74 20 69 73 20 65 78 65 63 75 74 65 64 2c 20  nt is executed, 
30b70 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f  or.** an error o
30b80 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75  r constraint cau
30b90 73 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20  ses an implicit 
30ba0 72 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75  rollback to occu
30bb0 72 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61  r..** The rollba
30bc0 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e  ck callback is n
30bd0 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20  ot invoked if a 
30be0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a  transaction is.*
30bf0 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
30c00 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61  rolled back beca
30c10 75 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  use the database
30c20 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63   connection is c
30c30 6c 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f  losed..** The ro
30c40 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20  llback callback 
30c50 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69  is not invoked i
30c60 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
30c70 69 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63  is.** rolled bac
30c80 6b 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d  k because a comm
30c90 69 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  it callback retu
30ca0 72 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a  rned non-zero..*
30cb0 2a 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f  * <todo> Check o
30cc0 6e 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a  n this </todo>.*
30cd0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68  *.** See also th
30ce0 65 20 5b 73 71 6c 69 74 65 33 5f 75 70 64 61 74  e [sqlite3_updat
30cf0 65 5f 68 6f 6f 6b 28 29 5d 20 69 6e 74 65 72 66  e_hook()] interf
30d00 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ace..**.** Requi
30d10 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
30d20 39 35 31 5d 20 5b 48 31 32 39 35 32 5d 20 5b 48  951] [H12952] [H
30d30 31 32 39 35 33 5d 20 5b 48 31 32 39 35 34 5d 20  12953] [H12954] 
30d40 5b 48 31 32 39 35 35 5d 0a 2a 2a 20 5b 48 31 32  [H12955].** [H12
30d50 39 36 31 5d 20 5b 48 31 32 39 36 32 5d 20 5b 48  961] [H12962] [H
30d60 31 32 39 36 33 5d 20 5b 48 31 32 39 36 34 5d 0a  12963] [H12964].
30d70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
30d80 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6d 6d  id *sqlite3_comm
30d90 69 74 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a  it_hook(sqlite3*
30da0 2c 20 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c  , int(*)(void*),
30db0 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
30dc0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
30dd0 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28  3_rollback_hook(
30de0 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a  sqlite3*, void(*
30df0 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64 2a  )(void *), void*
30e00 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
30e10 45 46 3a 20 44 61 74 61 20 43 68 61 6e 67 65 20  EF: Data Change 
30e20 4e 6f 74 69 66 69 63 61 74 69 6f 6e 20 43 61 6c  Notification Cal
30e30 6c 62 61 63 6b 73 20 7b 48 31 32 39 37 30 7d 20  lbacks {H12970} 
30e40 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S60400>.**.** T
30e50 68 65 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74  he sqlite3_updat
30e60 65 5f 68 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61  e_hook() interfa
30e70 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63  ce registers a c
30e80 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
30e90 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 64 61  .** with the [da
30ea0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
30eb0 6e 5d 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  n] identified by
30ec0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
30ed0 65 6e 74 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76  ent.** to be inv
30ee0 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61 20  oked whenever a 
30ef0 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c 20  row is updated, 
30f00 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65  inserted or dele
30f10 74 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c  ted..** Any call
30f20 62 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72  back set by a pr
30f30 65 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 74  evious call to t
30f40 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  his function.** 
30f50 66 6f 72 20 74 68 65 20 73 61 6d 65 20 64 61 74  for the same dat
30f60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
30f70 20 69 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a   is overridden..
30f80 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  **.** The second
30f90 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
30fa0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75  ointer to the fu
30fb0 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65  nction to invoke
30fc0 20 77 68 65 6e 20 61 0a 2a 2a 20 72 6f 77 20 69   when a.** row i
30fd0 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65 72  s updated, inser
30fe0 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a  ted or deleted..
30ff0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67  ** The first arg
31000 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 63 61 6c  ument to the cal
31010 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70 79 20  lback is a copy 
31020 6f 66 20 74 68 65 20 74 68 69 72 64 20 61 72 67  of the third arg
31030 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69  ument.** to sqli
31040 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28  te3_update_hook(
31050 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  )..** The second
31060 20 63 61 6c 6c 62 61 63 6b 20 61 72 67 75 6d 65   callback argume
31070 6e 74 20 69 73 20 6f 6e 65 20 6f 66 20 5b 53 51  nt is one of [SQ
31080 4c 49 54 45 5f 49 4e 53 45 52 54 5d 2c 20 5b 53  LITE_INSERT], [S
31090 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d 2c 0a 2a  QLITE_DELETE],.*
310a0 2a 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 50 44  * or [SQLITE_UPD
310b0 41 54 45 5d 2c 20 64 65 70 65 6e 64 69 6e 67 20  ATE], depending 
310c0 6f 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  on the operation
310d0 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65   that caused the
310e0 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 74 6f 20   callback.** to 
310f0 62 65 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 54  be invoked..** T
31100 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75  he third and fou
31110 72 74 68 20 61 72 67 75 6d 65 6e 74 73 20 74 6f  rth arguments to
31120 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 63 6f   the callback co
31130 6e 74 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74  ntain pointers t
31140 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  o the.** databas
31150 65 20 61 6e 64 20 74 61 62 6c 65 20 6e 61 6d 65  e and table name
31160 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
31170 61 66 66 65 63 74 65 64 20 72 6f 77 2e 0a 2a 2a  affected row..**
31180 20 54 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62   The final callb
31190 61 63 6b 20 70 61 72 61 6d 65 74 65 72 20 69 73  ack parameter is
311a0 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20   the [rowid] of 
311b0 74 68 65 20 72 6f 77 2e 0a 2a 2a 20 49 6e 20 74  the row..** In t
311c0 68 65 20 63 61 73 65 20 6f 66 20 61 6e 20 75 70  he case of an up
311d0 64 61 74 65 2c 20 74 68 69 73 20 69 73 20 74 68  date, this is th
311e0 65 20 5b 72 6f 77 69 64 5d 20 61 66 74 65 72 20  e [rowid] after 
311f0 74 68 65 20 75 70 64 61 74 65 20 74 61 6b 65 73  the update takes
31200 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   place..**.** Th
31210 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 73  e update hook is
31220 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 77 68 65   not invoked whe
31230 6e 20 69 6e 74 65 72 6e 61 6c 20 73 79 73 74 65  n internal syste
31240 6d 20 74 61 62 6c 65 73 20 61 72 65 0a 2a 2a 20  m tables are.** 
31250 6d 6f 64 69 66 69 65 64 20 28 69 2e 65 2e 20 73  modified (i.e. s
31260 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64  qlite_master and
31270 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
31280 29 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  )..**.** In the 
31290 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e  current implemen
312a0 74 61 74 69 6f 6e 2c 20 74 68 65 20 75 70 64 61  tation, the upda
312b0 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 73 20 6e 6f  te hook.** is no
312c0 74 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 64  t invoked when d
312d0 75 70 6c 69 63 61 74 69 6f 6e 20 72 6f 77 73 20  uplication rows 
312e0 61 72 65 20 64 65 6c 65 74 65 64 20 62 65 63 61  are deleted beca
312f0 75 73 65 20 6f 66 20 61 6e 0a 2a 2a 20 5b 4f 4e  use of an.** [ON
31300 20 43 4f 4e 46 4c 49 43 54 20 7c 20 4f 4e 20 43   CONFLICT | ON C
31310 4f 4e 46 4c 49 43 54 20 52 45 50 4c 41 43 45 5d  ONFLICT REPLACE]
31320 20 63 6c 61 75 73 65 2e 20 20 4e 6f 72 20 69 73   clause.  Nor is
31330 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   the update hook
31340 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e  .** invoked when
31350 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65 74 65   rows are delete
31360 64 20 75 73 69 6e 67 20 74 68 65 20 5b 74 72 75  d using the [tru
31370 6e 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69  ncate optimizati
31380 6f 6e 5d 2e 0a 2a 2a 20 54 68 65 20 65 78 63 65  on]..** The exce
31390 70 74 69 6f 6e 73 20 64 65 66 69 6e 65 64 20 69  ptions defined i
313a0 6e 20 74 68 69 73 20 70 61 72 61 67 72 61 70 68  n this paragraph
313b0 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e   might change in
313c0 20 61 20 66 75 74 75 72 65 0a 2a 2a 20 72 65 6c   a future.** rel
313d0 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ease of SQLite..
313e0 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65  **.** The update
313f0 20 68 6f 6f 6b 20 69 6d 70 6c 65 6d 65 6e 74 61   hook implementa
31400 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f  tion must not do
31410 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77   anything that w
31420 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68  ill modify.** th
31430 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
31440 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b  ction that invok
31450 65 64 20 74 68 65 20 75 70 64 61 74 65 20 68 6f  ed the update ho
31460 6f 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73  ok.  Any actions
31470 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  .** to modify th
31480 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
31490 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65  ction must be de
314a0 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74  ferred until aft
314b0 65 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65  er the.** comple
314c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c  tion of the [sql
314d0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c  ite3_step()] cal
314e0 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64  l that triggered
314f0 20 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   the update hook
31500 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 5b  ..** Note that [
31510 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
31520 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  v2()] and [sqlit
31530 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74 68 20  e3_step()] both 
31540 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a 2a 20  modify their.** 
31550 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
31560 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d 65 61  ions for the mea
31570 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66 79 22  ning of "modify"
31580 20 69 6e 20 74 68 69 73 20 70 61 72 61 67 72 61   in this paragra
31590 70 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f  ph..**.** If ano
315a0 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77 61  ther function wa
315b0 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67  s previously reg
315c0 69 73 74 65 72 65 64 2c 20 69 74 73 20 70 41 72  istered, its pAr
315d0 67 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 72 65  g value.** is re
315e0 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77 69  turned.  Otherwi
315f0 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72  se NULL is retur
31600 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ned..**.** See a
31610 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33  lso the [sqlite3
31620 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20  _commit_hook()] 
31630 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c  and [sqlite3_rol
31640 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 5d 0a 2a 2a  lback_hook()].**
31650 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a 0a   interfaces..**.
31660 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
31670 0a 2a 2a 20 5b 48 31 32 39 37 31 5d 20 5b 48 31  .** [H12971] [H1
31680 32 39 37 33 5d 20 5b 48 31 32 39 37 35 5d 20 5b  2973] [H12975] [
31690 48 31 32 39 37 37 5d 20 5b 48 31 32 39 37 39 5d  H12977] [H12979]
316a0 20 5b 48 31 32 39 38 31 5d 20 5b 48 31 32 39 38   [H12981] [H1298
316b0 33 5d 20 5b 48 31 32 39 38 36 5d 0a 2a 2f 0a 53  3] [H12986].*/.S
316c0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
316d0 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68  sqlite3_update_h
316e0 6f 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c  ook(.  sqlite3*,
316f0 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64   .  void(*)(void
31700 20 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e   *,int ,char con
31710 73 74 20 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20  st *,char const 
31720 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  *,sqlite3_int64)
31730 2c 0a 20 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a  ,.  void*.);../*
31740 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e  .** CAPI3REF: En
31750 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20  able Or Disable 
31760 53 68 61 72 65 64 20 50 61 67 65 72 20 43 61 63  Shared Pager Cac
31770 68 65 20 7b 48 31 30 33 33 30 7d 20 3c 53 33 30  he {H10330} <S30
31780 39 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  900>.** KEYWORDS
31790 3a 20 7b 73 68 61 72 65 64 20 63 61 63 68 65 7d  : {shared cache}
317a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
317b0 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64  ine enables or d
317c0 69 73 61 62 6c 65 73 20 74 68 65 20 73 68 61 72  isables the shar
317d0 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61 62  ing of the datab
317e0 61 73 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e 64  ase cache.** and
317f0 20 73 63 68 65 6d 61 20 64 61 74 61 20 73 74 72   schema data str
31800 75 63 74 75 72 65 73 20 62 65 74 77 65 65 6e 20  uctures between 
31810 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
31820 74 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69 6f  tion | connectio
31830 6e 73 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73 61  ns].** to the sa
31840 6d 65 20 64 61 74 61 62 61 73 65 2e 20 53 68 61  me database. Sha
31850 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20  ring is enabled 
31860 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  if the argument 
31870 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20 64  is true.** and d
31880 69 73 61 62 6c 65 64 20 69 66 20 74 68 65 20 61  isabled if the a
31890 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65  rgument is false
318a0 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73 68  ..**.** Cache sh
318b0 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64  aring is enabled
318c0 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 66 6f   and disabled fo
318d0 72 20 61 6e 20 65 6e 74 69 72 65 20 70 72 6f 63  r an entire proc
318e0 65 73 73 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  ess..** This is 
318f0 61 20 63 68 61 6e 67 65 20 61 73 20 6f 66 20 53  a change as of S
31900 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e  QLite version 3.
31910 35 2e 30 2e 20 49 6e 20 70 72 69 6f 72 20 76 65  5.0. In prior ve
31920 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
31930 2c 0a 2a 2a 20 73 68 61 72 69 6e 67 20 77 61 73  ,.** sharing was
31940 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61   enabled or disa
31950 62 6c 65 64 20 66 6f 72 20 65 61 63 68 20 74 68  bled for each th
31960 72 65 61 64 20 73 65 70 61 72 61 74 65 6c 79 2e  read separately.
31970 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68 65  .**.** The cache
31980 20 73 68 61 72 69 6e 67 20 6d 6f 64 65 20 73 65   sharing mode se
31990 74 20 62 79 20 74 68 69 73 20 69 6e 74 65 72 66  t by this interf
319a0 61 63 65 20 65 66 66 65 63 74 73 20 61 6c 6c 20  ace effects all 
319b0 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61  subsequent.** ca
319c0 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
319d0 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65  open()], [sqlite
319e0 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2c 20 61 6e  3_open_v2()], an
319f0 64 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  d [sqlite3_open1
31a00 36 28 29 5d 2e 0a 2a 2a 20 45 78 69 73 74 69 6e  6()]..** Existin
31a10 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  g database conne
31a20 63 74 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20  ctions continue 
31a30 75 73 65 20 74 68 65 20 73 68 61 72 69 6e 67 20  use the sharing 
31a40 6d 6f 64 65 0a 2a 2a 20 74 68 61 74 20 77 61 73  mode.** that was
31a50 20 69 6e 20 65 66 66 65 63 74 20 61 74 20 74 68   in effect at th
31a60 65 20 74 69 6d 65 20 74 68 65 79 20 77 65 72 65  e time they were
31a70 20 6f 70 65 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 56   opened..**.** V
31a80 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61  irtual tables ca
31a90 6e 6e 6f 74 20 62 65 20 75 73 65 64 20 77 69 74  nnot be used wit
31aa0 68 20 61 20 73 68 61 72 65 64 20 63 61 63 68 65  h a shared cache
31ab0 2e 20 20 57 68 65 6e 20 73 68 61 72 65 64 0a 2a  .  When shared.*
31ac0 2a 20 63 61 63 68 65 20 69 73 20 65 6e 61 62 6c  * cache is enabl
31ad0 65 64 2c 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ed, the [sqlite3
31ae0 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29  _create_module()
31af0 5d 20 41 50 49 20 75 73 65 64 20 74 6f 20 72 65  ] API used to re
31b00 67 69 73 74 65 72 0a 2a 2a 20 76 69 72 74 75 61  gister.** virtua
31b10 6c 20 74 61 62 6c 65 73 20 77 69 6c 6c 20 61 6c  l tables will al
31b20 77 61 79 73 20 72 65 74 75 72 6e 20 61 6e 20 65  ways return an e
31b30 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rror..**.** This
31b40 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
31b50 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69 66 20   [SQLITE_OK] if 
31b60 73 68 61 72 65 64 20 63 61 63 68 65 20 77 61 73  shared cache was
31b70 20 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61   enabled or disa
31b80 62 6c 65 64 0a 2a 2a 20 73 75 63 63 65 73 73 66  bled.** successf
31b90 75 6c 6c 79 2e 20 20 41 6e 20 5b 65 72 72 6f 72  ully.  An [error
31ba0 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
31bb0 65 64 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a  ed otherwise..**
31bc0 0a 2a 2a 20 53 68 61 72 65 64 20 63 61 63 68 65  .** Shared cache
31bd0 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79 20   is disabled by 
31be0 64 65 66 61 75 6c 74 2e 20 42 75 74 20 74 68 69  default. But thi
31bf0 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69  s might change i
31c00 6e 0a 2a 2a 20 66 75 74 75 72 65 20 72 65 6c 65  n.** future rele
31c10 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 20  ases of SQLite. 
31c20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68   Applications th
31c30 61 74 20 63 61 72 65 20 61 62 6f 75 74 20 73 68  at care about sh
31c40 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 73 65  ared.** cache se
31c50 74 74 69 6e 67 20 73 68 6f 75 6c 64 20 73 65 74  tting should set
31c60 20 69 74 20 65 78 70 6c 69 63 69 74 6c 79 2e 0a   it explicitly..
31c70 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20  **.** See Also: 
31c80 20 5b 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d   [SQLite Shared-
31c90 43 61 63 68 65 20 4d 6f 64 65 5d 0a 2a 2a 0a 2a  Cache Mode].**.*
31ca0 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20  * Requirements: 
31cb0 5b 48 31 30 33 33 31 5d 20 5b 48 31 30 33 33 36  [H10331] [H10336
31cc0 5d 20 5b 48 31 30 33 33 37 5d 20 5b 48 31 30 33  ] [H10337] [H103
31cd0 33 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  39].*/.SQLITE_AP
31ce0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e  I int sqlite3_en
31cf0 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
31d00 65 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  e(int);../*.** C
31d10 41 50 49 33 52 45 46 3a 20 41 74 74 65 6d 70 74  API3REF: Attempt
31d20 20 54 6f 20 46 72 65 65 20 48 65 61 70 20 4d 65   To Free Heap Me
31d30 6d 6f 72 79 20 7b 48 31 37 33 34 30 7d 20 3c 53  mory {H17340} <S
31d40 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  30220>.**.** The
31d50 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
31d60 5f 6d 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66  _memory() interf
31d70 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ace attempts to 
31d80 66 72 65 65 20 4e 20 62 79 74 65 73 0a 2a 2a 20  free N bytes.** 
31d90 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62  of heap memory b
31da0 79 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e  y deallocating n
31db0 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d  on-essential mem
31dc0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a  ory allocations.
31dd0 2a 2a 20 68 65 6c 64 20 62 79 20 74 68 65 20 64  ** held by the d
31de0 61 74 61 62 61 73 65 20 6c 69 62 72 61 72 79 2e  atabase library.
31df0 20 7b 45 4e 44 7d 20 20 4d 65 6d 6f 72 79 20 75   {END}  Memory u
31e00 73 65 64 20 74 6f 20 63 61 63 68 65 20 64 61 74  sed to cache dat
31e10 61 62 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74  abase.** pages t
31e20 6f 20 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72  o improve perfor
31e30 6d 61 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d  mance is an exam
31e40 70 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e  ple of non-essen
31e50 74 69 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20  tial memory..** 
31e60 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
31e70 6d 65 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73  memory() returns
31e80 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
31e90 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72  ytes actually fr
31ea0 65 65 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69  eed,.** which mi
31eb0 67 68 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c  ght be more or l
31ec0 65 73 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f  ess than the amo
31ed0 75 6e 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a  unt requested..*
31ee0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
31ef0 73 3a 20 5b 48 31 37 33 34 31 5d 20 5b 48 31 37  s: [H17341] [H17
31f00 33 34 32 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  342].*/.SQLITE_A
31f10 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72  PI int sqlite3_r
31f20 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e  elease_memory(in
31f30 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
31f40 52 45 46 3a 20 49 6d 70 6f 73 65 20 41 20 4c 69  REF: Impose A Li
31f50 6d 69 74 20 4f 6e 20 48 65 61 70 20 53 69 7a 65  mit On Heap Size
31f60 20 7b 48 31 37 33 35 30 7d 20 3c 53 33 30 32 32   {H17350} <S3022
31f70 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  0>.**.** The sql
31f80 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c  ite3_soft_heap_l
31f90 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65  imit() interface
31fa0 20 70 6c 61 63 65 73 20 61 20 22 73 6f 66 74 22   places a "soft"
31fb0 20 6c 69 6d 69 74 0a 2a 2a 20 6f 6e 20 74 68 65   limit.** on the
31fc0 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65 61 70 20   amount of heap 
31fd0 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 61 79 20  memory that may 
31fe0 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  be allocated by 
31ff0 53 51 4c 69 74 65 2e 0a 2a 2a 20 49 66 20 61 6e  SQLite..** If an
32000 20 69 6e 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61   internal alloca
32010 74 69 6f 6e 20 69 73 20 72 65 71 75 65 73 74 65  tion is requeste
32020 64 20 74 68 61 74 20 77 6f 75 6c 64 20 65 78 63  d that would exc
32030 65 65 64 20 74 68 65 0a 2a 2a 20 73 6f 66 74 20  eed the.** soft 
32040 68 65 61 70 20 6c 69 6d 69 74 2c 20 5b 73 71 6c  heap limit, [sql
32050 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
32060 6f 72 79 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65  ory()] is invoke
32070 64 20 6f 6e 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65  d one or.** more
32080 20 74 69 6d 65 73 20 74 6f 20 66 72 65 65 20 75   times to free u
32090 70 20 73 6f 6d 65 20 73 70 61 63 65 20 62 65 66  p some space bef
320a0 6f 72 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  ore the allocati
320b0 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e  on is performed.
320c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 69 6d 69 74  .**.** The limit
320d0 20 69 73 20 63 61 6c 6c 65 64 20 22 73 6f 66 74   is called "soft
320e0 22 2c 20 62 65 63 61 75 73 65 20 69 66 20 5b 73  ", because if [s
320f0 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
32100 65 6d 6f 72 79 28 29 5d 0a 2a 2a 20 63 61 6e 6e  emory()].** cann
32110 6f 74 20 66 72 65 65 20 73 75 66 66 69 63 69 65  ot free sufficie
32120 6e 74 20 6d 65 6d 6f 72 79 20 74 6f 20 70 72 65  nt memory to pre
32130 76 65 6e 74 20 74 68 65 20 6c 69 6d 69 74 20 66  vent the limit f
32140 72 6f 6d 20 62 65 69 6e 67 20 65 78 63 65 65 64  rom being exceed
32150 65 64 2c 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72  ed,.** the memor
32160 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  y is allocated a
32170 6e 79 77 61 79 20 61 6e 64 20 74 68 65 20 63 75  nyway and the cu
32180 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrent operation 
32190 70 72 6f 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20  proceeds..**.** 
321a0 41 20 6e 65 67 61 74 69 76 65 20 6f 72 20 7a 65  A negative or ze
321b0 72 6f 20 76 61 6c 75 65 20 66 6f 72 20 4e 20 6d  ro value for N m
321c0 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20  eans that there 
321d0 69 73 20 6e 6f 20 73 6f 66 74 20 68 65 61 70 20  is no soft heap 
321e0 6c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20 5b 73 71  limit and.** [sq
321f0 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
32200 6d 6f 72 79 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c  mory()] will onl
32210 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  y be called when
32220 20 6d 65 6d 6f 72 79 20 69 73 20 65 78 68 61 75   memory is exhau
32230 73 74 65 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66  sted..** The def
32240 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74  ault value for t
32250 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  he soft heap lim
32260 69 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a  it is zero..**.*
32270 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61  * SQLite makes a
32280 20 62 65 73 74 20 65 66 66 6f 72 74 20 74 6f 20   best effort to 
32290 68 6f 6e 6f 72 20 74 68 65 20 73 6f 66 74 20 68  honor the soft h
322a0 65 61 70 20 6c 69 6d 69 74 2e 0a 2a 2a 20 42 75  eap limit..** Bu
322b0 74 20 69 66 20 74 68 65 20 73 6f 66 74 20 68 65  t if the soft he
322c0 61 70 20 6c 69 6d 69 74 20 63 61 6e 6e 6f 74 20  ap limit cannot 
322d0 62 65 20 68 6f 6e 6f 72 65 64 2c 20 65 78 65 63  be honored, exec
322e0 75 74 69 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 63 6f  ution will.** co
322f0 6e 74 69 6e 75 65 20 77 69 74 68 6f 75 74 20 65  ntinue without e
32300 72 72 6f 72 20 6f 72 20 6e 6f 74 69 66 69 63 61  rror or notifica
32310 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 77  tion.  This is w
32320 68 79 20 74 68 65 20 6c 69 6d 69 74 20 69 73 0a  hy the limit is.
32330 2a 2a 20 63 61 6c 6c 65 64 20 61 20 22 73 6f 66  ** called a "sof
32340 74 22 20 6c 69 6d 69 74 2e 20 20 49 74 20 69 73  t" limit.  It is
32350 20 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a   advisory only..
32360 2a 2a 0a 2a 2a 20 50 72 69 6f 72 20 74 6f 20 53  **.** Prior to S
32370 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e  QLite version 3.
32380 35 2e 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  5.0, this routin
32390 65 20 6f 6e 6c 79 20 63 6f 6e 73 74 72 61 69 6e  e only constrain
323a0 65 64 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a  ed the memory.**
323b0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 61 20   allocated by a 
323c0 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20 2d 20  single thread - 
323d0 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
323e0 69 6e 20 77 68 69 63 68 20 74 68 69 73 20 72 6f  in which this ro
323f0 75 74 69 6e 65 0a 2a 2a 20 72 75 6e 73 2e 20 20  utine.** runs.  
32400 42 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53  Beginning with S
32410 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 2e  QLite version 3.
32420 35 2e 30 2c 20 74 68 65 20 73 6f 66 74 20 68 65  5.0, the soft he
32430 61 70 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 61  ap limit is.** a
32440 70 70 6c 69 65 64 20 74 6f 20 61 6c 6c 20 74 68  pplied to all th
32450 72 65 61 64 73 2e 20 54 68 65 20 76 61 6c 75 65  reads. The value
32460 20 73 70 65 63 69 66 69 65 64 20 66 6f 72 20 74   specified for t
32470 68 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  he soft heap lim
32480 69 74 0a 2a 2a 20 69 73 20 61 6e 20 75 70 70 65  it.** is an uppe
32490 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 65 20 74  r bound on the t
324a0 6f 74 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  otal memory allo
324b0 63 61 74 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 74  cation for all t
324c0 68 72 65 61 64 73 2e 20 49 6e 0a 2a 2a 20 76 65  hreads. In.** ve
324d0 72 73 69 6f 6e 20 33 2e 35 2e 30 20 74 68 65 72  rsion 3.5.0 ther
324e0 65 20 69 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73  e is no mechanis
324f0 6d 20 66 6f 72 20 6c 69 6d 69 74 69 6e 67 20 74  m for limiting t
32500 68 65 20 68 65 61 70 20 75 73 61 67 65 20 66 6f  he heap usage fo
32510 72 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20  r.** individual 
32520 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 52  threads..**.** R
32530 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
32540 5b 48 31 36 33 35 31 5d 20 5b 48 31 36 33 35 32  [H16351] [H16352
32550 5d 20 5b 48 31 36 33 35 33 5d 20 5b 48 31 36 33  ] [H16353] [H163
32560 35 34 5d 20 5b 48 31 36 33 35 35 5d 20 5b 48 31  54] [H16355] [H1
32570 36 33 35 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  6358].*/.SQLITE_
32580 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
32590 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
325a0 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  (int);../*.** CA
325b0 50 49 33 52 45 46 3a 20 45 78 74 72 61 63 74 20  PI3REF: Extract 
325c0 4d 65 74 61 64 61 74 61 20 41 62 6f 75 74 20 41  Metadata About A
325d0 20 43 6f 6c 75 6d 6e 20 4f 66 20 41 20 54 61 62   Column Of A Tab
325e0 6c 65 20 7b 48 31 32 38 35 30 7d 20 3c 53 36 30  le {H12850} <S60
325f0 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  300>.**.** This 
32600 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
32610 6d 65 74 61 64 61 74 61 20 61 62 6f 75 74 20 61  metadata about a
32620 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e   specific column
32630 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 0a 2a   of a specific.*
32640 2a 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  * database table
32650 20 61 63 63 65 73 73 69 62 6c 65 20 75 73 69 6e   accessible usin
32660 67 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  g the [database 
32670 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64  connection] hand
32680 6c 65 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20  le.** passed as 
32690 74 68 65 20 66 69 72 73 74 20 66 75 6e 63 74 69  the first functi
326a0 6f 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  on argument..**.
326b0 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 69 73  ** The column is
326c0 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
326d0 68 65 20 73 65 63 6f 6e 64 2c 20 74 68 69 72 64  he second, third
326e0 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61   and fourth para
326f0 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69  meters to.** thi
32700 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20  s function. The 
32710 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
32720 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 6e   is either the n
32730 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
32740 61 73 65 0a 2a 2a 20 28 69 2e 65 2e 20 22 6d 61  ase.** (i.e. "ma
32750 69 6e 22 2c 20 22 74 65 6d 70 22 20 6f 72 20 61  in", "temp" or a
32760 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  n attached datab
32770 61 73 65 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ase) containing 
32780 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
32790 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 2e 20   table or NULL. 
327a0 49 66 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 74  If it is NULL, t
327b0 68 65 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64  hen all attached
327c0 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20 73   databases are s
327d0 65 61 72 63 68 65 64 0a 2a 2a 20 66 6f 72 20 74  earched.** for t
327e0 68 65 20 74 61 62 6c 65 20 75 73 69 6e 67 20 74  he table using t
327f0 68 65 20 73 61 6d 65 20 61 6c 67 6f 72 69 74 68  he same algorith
32800 6d 20 75 73 65 64 20 62 79 20 74 68 65 20 64 61  m used by the da
32810 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 74 6f  tabase engine to
32820 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 75 6e 71 75  .** resolve unqu
32830 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 72 65  alified table re
32840 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20  ferences..**.** 
32850 54 68 65 20 74 68 69 72 64 20 61 6e 64 20 66 6f  The third and fo
32860 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20  urth parameters 
32870 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
32880 20 61 72 65 20 74 68 65 20 74 61 62 6c 65 20 61   are the table a
32890 6e 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d  nd column.** nam
328a0 65 20 6f 66 20 74 68 65 20 64 65 73 69 72 65 64  e of the desired
328b0 20 63 6f 6c 75 6d 6e 2c 20 72 65 73 70 65 63 74   column, respect
328c0 69 76 65 6c 79 2e 20 4e 65 69 74 68 65 72 20 6f  ively. Neither o
328d0 66 20 74 68 65 73 65 20 70 61 72 61 6d 65 74 65  f these paramete
328e0 72 73 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c  rs.** may be NUL
328f0 4c 2e 0a 2a 2a 0a 2a 2a 20 4d 65 74 61 64 61 74  L..**.** Metadat
32900 61 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79  a is returned by
32910 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20   writing to the 
32920 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
32930 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 35   passed as the 5
32940 74 68 0a 2a 2a 20 61 6e 64 20 73 75 62 73 65 71  th.** and subseq
32950 75 65 6e 74 20 70 61 72 61 6d 65 74 65 72 73 20  uent parameters 
32960 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
32970 2e 20 41 6e 79 20 6f 66 20 74 68 65 73 65 20 61  . Any of these a
32980 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 0a  rguments may be.
32990 2a 2a 20 4e 55 4c 4c 2c 20 69 6e 20 77 68 69 63  ** NULL, in whic
329a0 68 20 63 61 73 65 20 74 68 65 20 63 6f 72 72 65  h case the corre
329b0 73 70 6f 6e 64 69 6e 67 20 65 6c 65 6d 65 6e 74  sponding element
329c0 20 6f 66 20 6d 65 74 61 64 61 74 61 20 69 73 20   of metadata is 
329d0 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c  omitted..**.** <
329e0 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c  blockquote>.** <
329f0 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22  table border="1"
32a00 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 50 61  >.** <tr><th> Pa
32a10 72 61 6d 65 74 65 72 20 3c 74 68 3e 20 4f 75 74  rameter <th> Out
32a20 70 75 74 3c 62 72 3e 54 79 70 65 20 3c 74 68 3e  put<br>Type <th>
32a30 20 20 44 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a    Description.**
32a40 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 35 74 68  .** <tr><td> 5th
32a50 20 3c 74 64 3e 20 63 6f 6e 73 74 20 63 68 61 72   <td> const char
32a60 2a 20 3c 74 64 3e 20 44 61 74 61 20 74 79 70 65  * <td> Data type
32a70 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 36 74 68  .** <tr><td> 6th
32a80 20 3c 74 64 3e 20 63 6f 6e 73 74 20 63 68 61 72   <td> const char
32a90 2a 20 3c 74 64 3e 20 4e 61 6d 65 20 6f 66 20 64  * <td> Name of d
32aa0 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e  efault collation
32ab0 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20 3c 74 72   sequence.** <tr
32ac0 3e 3c 74 64 3e 20 37 74 68 20 3c 74 64 3e 20 69  ><td> 7th <td> i
32ad0 6e 74 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20  nt         <td> 
32ae0 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 68  True if column h
32af0 61 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  as a NOT NULL co
32b00 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 3c 74 72 3e  nstraint.** <tr>
32b10 3c 74 64 3e 20 38 74 68 20 3c 74 64 3e 20 69 6e  <td> 8th <td> in
32b20 74 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54  t         <td> T
32b30 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73  rue if column is
32b40 20 70 61 72 74 20 6f 66 20 74 68 65 20 50 52 49   part of the PRI
32b50 4d 41 52 59 20 4b 45 59 0a 2a 2a 20 3c 74 72 3e  MARY KEY.** <tr>
32b60 3c 74 64 3e 20 39 74 68 20 3c 74 64 3e 20 69 6e  <td> 9th <td> in
32b70 74 20 20 20 20 20 20 20 20 20 3c 74 64 3e 20 54  t         <td> T
32b80 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73  rue if column is
32b90 20 5b 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 5d   [AUTOINCREMENT]
32ba0 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a 20  .** </table>.** 
32bb0 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
32bc0 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 70  .** The memory p
32bd0 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65  ointed to by the
32be0 20 63 68 61 72 61 63 74 65 72 20 70 6f 69 6e 74   character point
32bf0 65 72 73 20 72 65 74 75 72 6e 65 64 20 66 6f 72  ers returned for
32c00 20 74 68 65 0a 2a 2a 20 64 65 63 6c 61 72 61 74   the.** declarat
32c10 69 6f 6e 20 74 79 70 65 20 61 6e 64 20 63 6f 6c  ion type and col
32c20 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
32c30 69 73 20 76 61 6c 69 64 20 6f 6e 6c 79 20 75 6e  is valid only un
32c40 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20  til the next.** 
32c50 63 61 6c 6c 20 74 6f 20 61 6e 79 20 53 51 4c 69  call to any SQLi
32c60 74 65 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e  te API function.
32c70 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 70  .**.** If the sp
32c80 65 63 69 66 69 65 64 20 74 61 62 6c 65 20 69 73  ecified table is
32c90 20 61 63 74 75 61 6c 6c 79 20 61 20 76 69 65 77   actually a view
32ca0 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  , an [error code
32cb0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  ] is returned..*
32cc0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 70 65 63  *.** If the spec
32cd0 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20  ified column is 
32ce0 22 72 6f 77 69 64 22 2c 20 22 6f 69 64 22 20 6f  "rowid", "oid" o
32cf0 72 20 22 5f 72 6f 77 69 64 5f 22 20 61 6e 64 20  r "_rowid_" and 
32d00 61 6e 0a 2a 2a 20 5b 49 4e 54 45 47 45 52 20 50  an.** [INTEGER P
32d10 52 49 4d 41 52 59 20 4b 45 59 5d 20 63 6f 6c 75  RIMARY KEY] colu
32d20 6d 6e 20 68 61 73 20 62 65 65 6e 20 65 78 70 6c  mn has been expl
32d30 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 2c  icitly declared,
32d40 20 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74   then the output
32d50 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61  .** parameters a
32d60 72 65 20 73 65 74 20 66 6f 72 20 74 68 65 20 65  re set for the e
32d70 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72  xplicitly declar
32d80 65 64 20 63 6f 6c 75 6d 6e 2e 20 49 66 20 74 68  ed column. If th
32d90 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20 65 78 70  ere is no.** exp
32da0 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64  licitly declared
32db0 20 5b 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52   [INTEGER PRIMAR
32dc0 59 20 4b 45 59 5d 20 63 6f 6c 75 6d 6e 2c 20 74  Y KEY] column, t
32dd0 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a 2a  hen the output.*
32de0 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  * parameters are
32df0 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a   set as follows:
32e00 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20  .**.** <pre>.** 
32e10 20 20 20 20 64 61 74 61 20 74 79 70 65 3a 20 22      data type: "
32e20 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20 20 20 20  INTEGER".**     
32e30 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
32e40 63 65 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a 20  ce: "BINARY".** 
32e50 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30 0a      not null: 0.
32e60 2a 2a 20 20 20 20 20 70 72 69 6d 61 72 79 20 6b  **     primary k
32e70 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75 74  ey: 1.**     aut
32e80 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a 2a  o increment: 0.*
32e90 2a 20 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 54  * </pre>.**.** T
32ea0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
32eb0 20 6c 6f 61 64 20 6f 6e 65 20 6f 72 20 6d 6f 72   load one or mor
32ec0 65 20 73 63 68 65 6d 61 73 20 66 72 6f 6d 20 64  e schemas from d
32ed0 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20 49  atabase files. I
32ee0 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f 63  f an.** error oc
32ef0 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 69 73  curs during this
32f00 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20   process, or if 
32f10 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74 61  the requested ta
32f20 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 0a 2a 2a  ble or column.**
32f30 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
32f40 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  , an [error code
32f50 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
32f60 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  d an error messa
32f70 67 65 20 6c 65 66 74 0a 2a 2a 20 69 6e 20 74 68  ge left.** in th
32f80 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
32f90 65 63 74 69 6f 6e 5d 20 28 74 6f 20 62 65 20 72  ection] (to be r
32fa0 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 73  etrieved using s
32fb0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 29  qlite3_errmsg())
32fc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49  ..**.** This API
32fd0 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62   is only availab
32fe0 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61 72  le if the librar
32ff0 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77  y was compiled w
33000 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c 49  ith the.** [SQLI
33010 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e  TE_ENABLE_COLUMN
33020 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72 65  _METADATA] C-pre
33030 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f 6c  processor symbol
33040 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c   defined..*/.SQL
33050 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
33060 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e  te3_table_column
33070 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71 6c  _metadata(.  sql
33080 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
33090 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e           /* Conn
330a0 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a 2f  ection handle */
330b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
330c0 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f  DbName,        /
330d0 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65 20  * Database name 
330e0 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e  or NULL */.  con
330f0 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65 4e  st char *zTableN
33100 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c  ame,     /* Tabl
33110 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73  e name */.  cons
33120 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 4e  t char *zColumnN
33130 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  ame,    /* Colum
33140 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61 72  n name */.  char
33150 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61 54   const **pzDataT
33160 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50 55  ype,    /* OUTPU
33170 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74 61  T: Declared data
33180 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72 20   type */.  char 
33190 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53 65  const **pzCollSe
331a0 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54  q,     /* OUTPUT
331b0 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  : Collation sequ
331c0 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69  ence name */.  i
331d0 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20 20  nt *pNotNull,   
331e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
331f0 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e 4f  TPUT: True if NO
33200 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e  T NULL constrain
33210 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69 6e  t exists */.  in
33220 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c 20  t *pPrimaryKey, 
33230 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
33240 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c  PUT: True if col
33250 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20 2a  umn part of PK *
33260 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69 6e  /.  int *pAutoin
33270 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
33280 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20  /* OUTPUT: True 
33290 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75 74  if column is aut
332a0 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 29  o-increment */.)
332b0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
332c0 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78 74 65 6e  F: Load An Exten
332d0 73 69 6f 6e 20 7b 48 31 32 36 30 30 7d 20 3c 53  sion {H12600} <S
332e0 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  20500>.**.** Thi
332f0 73 20 69 6e 74 65 72 66 61 63 65 20 6c 6f 61 64  s interface load
33300 73 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74 65  s an SQLite exte
33310 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 66 72  nsion library fr
33320 6f 6d 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c  om the named fil
33330 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 31  e..**.** {H12601
33340 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f  } The sqlite3_lo
33350 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69  ad_extension() i
33360 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74  nterface attempt
33370 73 20 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a 20  s to load an.** 
33380 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20           SQLite 
33390 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72  extension librar
333a0 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  y contained in t
333b0 68 65 20 66 69 6c 65 20 7a 46 69 6c 65 2e 0a 2a  he file zFile..*
333c0 2a 0a 2a 2a 20 7b 48 31 32 36 30 32 7d 20 54 68  *.** {H12602} Th
333d0 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73  e entry point is
333e0 20 7a 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b 48   zProc..**.** {H
333f0 31 32 36 30 33 7d 20 7a 50 72 6f 63 20 6d 61 79  12603} zProc may
33400 20 62 65 20 30 2c 20 69 6e 20 77 68 69 63 68 20   be 0, in which 
33410 63 61 73 65 20 74 68 65 20 6e 61 6d 65 20 6f 66  case the name of
33420 20 74 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74   the entry point
33430 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 65 66  .**          def
33440 61 75 6c 74 73 20 74 6f 20 22 73 71 6c 69 74 65  aults to "sqlite
33450 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
33460 22 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 34  "..**.** {H12604
33470 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c 6f  } The sqlite3_lo
33480 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 69  ad_extension() i
33490 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72  nterface shall r
334a0 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20  eturn.**        
334b0 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e    [SQLITE_OK] on
334c0 20 73 75 63 63 65 73 73 20 61 6e 64 20 5b 53 51   success and [SQ
334d0 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20 73  LITE_ERROR] if s
334e0 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72  omething goes wr
334f0 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36  ong..**.** {H126
33500 30 35 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20  05} If an error 
33510 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72  occurs and pzErr
33520 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68  Msg is not 0, th
33530 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  en the.**       
33540 20 20 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64     [sqlite3_load
33550 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 69 6e  _extension()] in
33560 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 61 74  terface shall at
33570 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 20 20  tempt to.**     
33580 20 20 20 20 20 66 69 6c 6c 20 2a 70 7a 45 72 72       fill *pzErr
33590 4d 73 67 20 77 69 74 68 20 65 72 72 6f 72 20 6d  Msg with error m
335a0 65 73 73 61 67 65 20 74 65 78 74 20 73 74 6f 72  essage text stor
335b0 65 64 20 69 6e 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ed in memory.** 
335c0 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65           obtaine
335d0 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  d from [sqlite3_
335e0 6d 61 6c 6c 6f 63 28 29 5d 2e 20 7b 45 4e 44 7d  malloc()]. {END}
335f0 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
33600 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  nction.**       
33610 20 20 20 73 68 6f 75 6c 64 20 66 72 65 65 20 74     should free t
33620 68 69 73 20 6d 65 6d 6f 72 79 20 62 79 20 63 61  his memory by ca
33630 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66  lling [sqlite3_f
33640 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ree()]..**.** {H
33650 31 32 36 30 36 7d 20 45 78 74 65 6e 73 69 6f 6e  12606} Extension
33660 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74 20 62 65   loading must be
33670 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 0a 2a   enabled using.*
33680 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
33690 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f  te3_enable_load_
336a0 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69  extension()] pri
336b0 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68  or to calling th
336c0 69 73 20 41 50 49 2c 0a 2a 2a 20 20 20 20 20 20  is API,.**      
336d0 20 20 20 20 6f 74 68 65 72 77 69 73 65 20 61 6e      otherwise an
336e0 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20 72   error will be r
336f0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53  eturned..**.** S
33700 65 65 20 61 6c 73 6f 20 74 68 65 20 5b 6c 6f 61  ee also the [loa
33710 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 53 51  d_extension() SQ
33720 4c 20 66 75 6e 63 74 69 6f 6e 5d 2e 0a 2a 2f 0a  L function]..*/.
33730 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
33740 71 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65  qlite3_load_exte
33750 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33  nsion(.  sqlite3
33760 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
33770 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e  * Load the exten
33780 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64  sion into this d
33790 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
337a0 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  on */.  const ch
337b0 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a  ar *zFile,    /*
337c0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61   Name of the sha
337d0 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74  red library cont
337e0 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e  aining extension
337f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
33800 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45   *zProc,    /* E
33810 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 44 65 72  ntry point.  Der
33820 69 76 65 64 20 66 72 6f 6d 20 7a 46 69 6c 65 20  ived from zFile 
33830 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a  if 0 */.  char *
33840 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20  *pzErrMsg       
33850 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65 73  /* Put error mes
33860 73 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f 74  sage here if not
33870 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20   0 */.);../*.** 
33880 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65  CAPI3REF: Enable
33890 20 4f 72 20 44 69 73 61 62 6c 65 20 45 78 74 65   Or Disable Exte
338a0 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48  nsion Loading {H
338b0 31 32 36 32 30 7d 20 3c 53 32 30 35 30 30 3e 0a  12620} <S20500>.
338c0 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20 6e 6f 74 20  **.** So as not 
338d0 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79  to open security
338e0 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20   holes in older 
338f0 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  applications tha
33900 74 20 61 72 65 0a 2a 2a 20 75 6e 70 72 65 70 61  t are.** unprepa
33910 72 65 64 20 74 6f 20 64 65 61 6c 20 77 69 74 68  red to deal with
33920 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69   extension loadi
33930 6e 67 2c 20 61 6e 64 20 61 73 20 61 20 6d 65 61  ng, and as a mea
33940 6e 73 20 6f 66 20 64 69 73 61 62 6c 69 6e 67 0a  ns of disabling.
33950 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61  ** extension loa
33960 64 69 6e 67 20 77 68 69 6c 65 20 65 76 61 6c 75  ding while evalu
33970 61 74 69 6e 67 20 75 73 65 72 2d 65 6e 74 65 72  ating user-enter
33980 65 64 20 53 51 4c 2c 20 74 68 65 20 66 6f 6c 6c  ed SQL, the foll
33990 6f 77 69 6e 67 20 41 50 49 0a 2a 2a 20 69 73 20  owing API.** is 
339a0 70 72 6f 76 69 64 65 64 20 74 6f 20 74 75 72 6e  provided to turn
339b0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f   the [sqlite3_lo
339c0 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20  ad_extension()] 
339d0 6d 65 63 68 61 6e 69 73 6d 20 6f 6e 20 61 6e 64  mechanism on and
339e0 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65   off..**.** Exte
339f0 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73  nsion loading is
33a00 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e   off by default.
33a10 20 53 65 65 20 74 69 63 6b 65 74 20 23 31 38 36   See ticket #186
33a20 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 31  3..**.** {H12621
33a30 7d 20 43 61 6c 6c 20 74 68 65 20 73 71 6c 69 74  } Call the sqlit
33a40 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65  e3_enable_load_e
33a50 78 74 65 6e 73 69 6f 6e 28 29 20 72 6f 75 74 69  xtension() routi
33a60 6e 65 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 31  ne with onoff==1
33a70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20  .**          to 
33a80 74 75 72 6e 20 65 78 74 65 6e 73 69 6f 6e 20 6c  turn extension l
33a90 6f 61 64 69 6e 67 20 6f 6e 20 61 6e 64 20 63 61  oading on and ca
33aa0 6c 6c 20 69 74 20 77 69 74 68 20 6f 6e 6f 66 66  ll it with onoff
33ab0 3d 3d 30 20 74 6f 20 74 75 72 6e 0a 2a 2a 20 20  ==0 to turn.**  
33ac0 20 20 20 20 20 20 20 20 69 74 20 62 61 63 6b 20          it back 
33ad0 6f 66 66 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a  off again..**.**
33ae0 20 7b 48 31 32 36 32 32 7d 20 45 78 74 65 6e 73   {H12622} Extens
33af0 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f  ion loading is o
33b00 66 66 20 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a  ff by default..*
33b10 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
33b20 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
33b30 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73  load_extension(s
33b40 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
33b50 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  onoff);../*.** C
33b60 41 50 49 33 52 45 46 3a 20 41 75 74 6f 6d 61 74  API3REF: Automat
33b70 69 63 61 6c 6c 79 20 4c 6f 61 64 20 41 6e 20 45  ically Load An E
33b80 78 74 65 6e 73 69 6f 6e 73 20 7b 48 31 32 36 34  xtensions {H1264
33b90 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a  0} <S20500>.**.*
33ba0 2a 20 54 68 69 73 20 41 50 49 20 63 61 6e 20 62  * This API can b
33bb0 65 20 69 6e 76 6f 6b 65 64 20 61 74 20 70 72 6f  e invoked at pro
33bc0 67 72 61 6d 20 73 74 61 72 74 75 70 20 69 6e 20  gram startup in 
33bd0 6f 72 64 65 72 20 74 6f 20 72 65 67 69 73 74 65  order to registe
33be0 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  r.** one or more
33bf0 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b   statically link
33c00 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 74 68  ed extensions th
33c10 61 74 20 77 69 6c 6c 20 62 65 20 61 76 61 69 6c  at will be avail
33c20 61 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e  able.** to all n
33c30 65 77 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ew [database con
33c40 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b 45 4e 44 7d  nections]. {END}
33c50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
33c60 69 6e 65 20 73 74 6f 72 65 73 20 61 20 70 6f 69  ine stores a poi
33c70 6e 74 65 72 20 74 6f 20 74 68 65 20 65 78 74 65  nter to the exte
33c80 6e 73 69 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61  nsion in an arra
33c90 79 20 74 68 61 74 20 69 73 0a 2a 2a 20 6f 62 74  y that is.** obt
33ca0 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69  ained from [sqli
33cb0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20  te3_malloc()].  
33cc0 49 66 20 79 6f 75 20 72 75 6e 20 61 20 6d 65 6d  If you run a mem
33cd0 6f 72 79 20 6c 65 61 6b 20 63 68 65 63 6b 65 72  ory leak checker
33ce0 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20 70 72 6f 67  .** on your prog
33cf0 72 61 6d 20 61 6e 64 20 69 74 20 72 65 70 6f 72  ram and it repor
33d00 74 73 20 61 20 6c 65 61 6b 20 62 65 63 61 75 73  ts a leak becaus
33d10 65 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 2c  e of this array,
33d20 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69   invoke.** [sqli
33d30 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65  te3_reset_auto_e
33d40 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f  xtension()] prio
33d50 72 20 74 6f 20 73 68 75 74 64 6f 77 6e 20 74 6f  r to shutdown to
33d60 20 66 72 65 65 20 74 68 65 20 6d 65 6d 6f 72 79   free the memory
33d70 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 31 7d  ..**.** {H12641}
33d80 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
33d90 65 67 69 73 74 65 72 73 20 61 6e 20 65 78 74 65  egisters an exte
33da0 6e 73 69 6f 6e 20 65 6e 74 72 79 20 70 6f 69 6e  nsion entry poin
33db0 74 20 74 68 61 74 20 69 73 0a 2a 2a 20 20 20 20  t that is.**    
33dc0 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 61        automatica
33dd0 6c 6c 79 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e  lly invoked when
33de0 65 76 65 72 20 61 20 6e 65 77 20 5b 64 61 74 61  ever a new [data
33df0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
33e00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20  .**          is 
33e10 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 5b 73 71  opened using [sq
33e20 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b  lite3_open()], [
33e30 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29  sqlite3_open16()
33e40 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  ],.**          o
33e50 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  r [sqlite3_open_
33e60 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  v2()]..**.** {H1
33e70 32 36 34 32 7d 20 44 75 70 6c 69 63 61 74 65 20  2642} Duplicate 
33e80 65 78 74 65 6e 73 69 6f 6e 73 20 61 72 65 20 64  extensions are d
33e90 65 74 65 63 74 65 64 20 73 6f 20 63 61 6c 6c 69  etected so calli
33ea0 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  ng this routine.
33eb0 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 75 6c 74  **          mult
33ec0 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68 20  iple times with 
33ed0 74 68 65 20 73 61 6d 65 20 65 78 74 65 6e 73 69  the same extensi
33ee0 6f 6e 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 0a  on is harmless..
33ef0 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 33 7d 20 54  **.** {H12643} T
33f00 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f 72  his routine stor
33f10 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  es a pointer to 
33f20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e  the extension in
33f30 20 61 6e 20 61 72 72 61 79 0a 2a 2a 20 20 20 20   an array.**    
33f40 20 20 20 20 20 20 74 68 61 74 20 69 73 20 6f 62        that is ob
33f50 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  tained from [sql
33f60 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a  ite3_malloc()]..
33f70 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34 34 7d 20 41  **.** {H12644} A
33f80 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69  utomatic extensi
33f90 6f 6e 73 20 61 70 70 6c 79 20 61 63 72 6f 73 73  ons apply across
33fa0 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f   all threads..*/
33fb0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
33fc0 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74  sqlite3_auto_ext
33fd0 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a 78 45  ension(void (*xE
33fe0 6e 74 72 79 50 6f 69 6e 74 29 28 76 6f 69 64 29  ntryPoint)(void)
33ff0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
34000 45 46 3a 20 52 65 73 65 74 20 41 75 74 6f 6d 61  EF: Reset Automa
34010 74 69 63 20 45 78 74 65 6e 73 69 6f 6e 20 4c 6f  tic Extension Lo
34020 61 64 69 6e 67 20 7b 48 31 32 36 36 30 7d 20 3c  ading {H12660} <
34030 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20500>.**.** Th
34040 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61  is function disa
34050 62 6c 65 73 20 61 6c 6c 20 70 72 65 76 69 6f 75  bles all previou
34060 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 61  sly registered a
34070 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 65 78 74 65  utomatic.** exte
34080 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44 7d 20 20 49  nsions. {END}  I
34090 74 20 75 6e 64 6f 65 73 20 74 68 65 20 65 66 66  t undoes the eff
340a0 65 63 74 20 6f 66 20 61 6c 6c 20 70 72 69 6f 72  ect of all prior
340b0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 75 74  .** [sqlite3_aut
340c0 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 63  o_extension()] c
340d0 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  alls..**.** {H12
340e0 36 36 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69  661} This functi
340f0 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20  on disables all 
34100 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73  previously regis
34110 74 65 72 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  tered.**        
34120 20 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65    automatic exte
34130 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48  nsions..**.** {H
34140 31 32 36 36 32 7d 20 54 68 69 73 20 66 75 6e 63  12662} This func
34150 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20 61 75  tion disables au
34160 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
34170 6e 73 20 69 6e 20 61 6c 6c 20 74 68 72 65 61 64  ns in all thread
34180 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
34190 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
341a0 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69  set_auto_extensi
341b0 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a  on(void);../*.**
341c0 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  **** EXPERIMENTA
341d0 4c 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63  L - subject to c
341e0 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f  hange without no
341f0 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tice ***********
34200 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  ***.**.** The in
34210 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 76  terface to the v
34220 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63  irtual-table mec
34230 68 61 6e 69 73 6d 20 69 73 20 63 75 72 72 65 6e  hanism is curren
34240 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a  tly considered.*
34250 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d 65  * to be experime
34260 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65 72  ntal.  The inter
34270 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e 67  face might chang
34280 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c  e in incompatibl
34290 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68  e ways..** If th
342a0 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d 20  is is a problem 
342b0 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20  for you, do not 
342c0 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61 63  use the interfac
342d0 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a  e at this time..
342e0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 76  **.** When the v
342f0 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63  irtual-table mec
34300 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a 65  hanism stabilize
34310 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c 61  s, we will decla
34320 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66  re the.** interf
34330 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70 6f  ace fixed, suppo
34340 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74 65  rt it indefinite
34350 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20 74  ly, and remove t
34360 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a  his comment..*/.
34370 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65  ./*.** Structure
34380 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76 69  s used by the vi
34390 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 74 65  rtual table inte
343a0 72 66 61 63 65 0a 2a 2f 0a 74 79 70 65 64 65 66  rface.*/.typedef
343b0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
343c0 76 74 61 62 20 73 71 6c 69 74 65 33 5f 76 74 61  vtab sqlite3_vta
343d0 62 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  b;.typedef struc
343e0 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
343f0 69 6e 66 6f 20 73 71 6c 69 74 65 33 5f 69 6e 64  info sqlite3_ind
34400 65 78 5f 69 6e 66 6f 3b 0a 74 79 70 65 64 65 66  ex_info;.typedef
34410 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
34420 76 74 61 62 5f 63 75 72 73 6f 72 20 73 71 6c 69  vtab_cursor sqli
34430 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 3b  te3_vtab_cursor;
34440 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
34450 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 73  sqlite3_module s
34460 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 3b 0a 0a  qlite3_module;..
34470 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
34480 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 4f 62  Virtual Table Ob
34490 6a 65 63 74 20 7b 48 31 38 30 30 30 7d 20 3c 53  ject {H18000} <S
344a0 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  20400>.** KEYWOR
344b0 44 53 3a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  DS: sqlite3_modu
344c0 6c 65 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c  le {virtual tabl
344d0 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a 20 45 58 50  e module}.** EXP
344e0 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
344f0 54 68 69 73 20 73 74 72 75 63 74 75 72 65 2c 20  This structure, 
34500 73 6f 6d 65 74 69 6d 65 73 20 63 61 6c 6c 65 64  sometimes called
34510 20 61 20 61 20 22 76 69 72 74 75 61 6c 20 74 61   a a "virtual ta
34520 62 6c 65 20 6d 6f 64 75 6c 65 22 2c 20 0a 2a 2a  ble module", .**
34530 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6d 70   defines the imp
34540 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
34550 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73   [virtual tables
34560 5d 2e 20 20 0a 2a 2a 20 54 68 69 73 20 73 74 72  ].  .** This str
34570 75 63 74 75 72 65 20 63 6f 6e 73 69 73 74 73 20  ucture consists 
34580 6d 6f 73 74 6c 79 20 6f 66 20 6d 65 74 68 6f 64  mostly of method
34590 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65  s for the module
345a0 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75 61  ..**.** A virtua
345b0 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 69  l table module i
345c0 73 20 63 72 65 61 74 65 64 20 62 79 20 66 69 6c  s created by fil
345d0 6c 69 6e 67 20 69 6e 20 61 20 70 65 72 73 69 73  ling in a persis
345e0 74 65 6e 74 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  tent.** instance
345f0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
34600 72 65 20 61 6e 64 20 70 61 73 73 69 6e 67 20 61  re and passing a
34610 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74   pointer to that
34620 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 74 6f 20   instance.** to 
34630 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
34640 6d 6f 64 75 6c 65 28 29 5d 20 6f 72 20 5b 73 71  module()] or [sq
34650 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
34660 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a 2a 20 54 68  ule_v2()]..** Th
34670 65 20 72 65 67 69 73 74 72 61 74 69 6f 6e 20 72  e registration r
34680 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74  emains valid unt
34690 69 6c 20 69 74 20 69 73 20 72 65 70 6c 61 63 65  il it is replace
346a0 64 20 62 79 20 61 20 64 69 66 66 65 72 65 6e 74  d by a different
346b0 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f 72 20 75 6e  .** module or un
346c0 74 69 6c 20 74 68 65 20 5b 64 61 74 61 62 61 73  til the [databas
346d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 63 6c  e connection] cl
346e0 6f 73 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65  oses.  The conte
346f0 6e 74 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74  nt.** of this st
34700 72 75 63 74 75 72 65 20 6d 75 73 74 20 6e 6f 74  ructure must not
34710 20 63 68 61 6e 67 65 20 77 68 69 6c 65 20 69 74   change while it
34720 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20 77   is registered w
34730 69 74 68 0a 2a 2a 20 61 6e 79 20 64 61 74 61 62  ith.** any datab
34740 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ase connection..
34750 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
34760 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20 69 6e 74  3_module {.  int
34770 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74   iVersion;.  int
34780 20 28 2a 78 43 72 65 61 74 65 29 28 73 71 6c 69   (*xCreate)(sqli
34790 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70 41 75 78  te3*, void *pAux
347a0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
347b0 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74   int argc, const
347c0 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67   char *const*arg
347d0 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  v,.             
347e0 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
347f0 2a 70 70 56 54 61 62 2c 20 63 68 61 72 2a 2a 29  *ppVTab, char**)
34800 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e 6e 65  ;.  int (*xConne
34810 63 74 29 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f  ct)(sqlite3*, vo
34820 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20  id *pAux,.      
34830 20 20 20 20 20 20 20 20 20 69 6e 74 20 61 72 67           int arg
34840 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
34850 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20 20 20  onst*argv,.     
34860 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
34870 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c  3_vtab **ppVTab,
34880 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20   char**);.  int 
34890 28 2a 78 42 65 73 74 49 6e 64 65 78 29 28 73 71  (*xBestIndex)(sq
348a0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
348b0 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  b, sqlite3_index
348c0 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e 74 20 28  _info*);.  int (
348d0 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29 28 73 71  *xDisconnect)(sq
348e0 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
348f0 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 73  b);.  int (*xDes
34900 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 76 74  troy)(sqlite3_vt
34910 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e  ab *pVTab);.  in
34920 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74  t (*xOpen)(sqlit
34930 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20  e3_vtab *pVTab, 
34940 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
34950 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 3b  sor **ppCursor);
34960 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f 73 65 29  .  int (*xClose)
34970 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
34980 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  rsor*);.  int (*
34990 78 46 69 6c 74 65 72 29 28 73 71 6c 69 74 65 33  xFilter)(sqlite3
349a0 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20 69  _vtab_cursor*, i
349b0 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74  nt idxNum, const
349c0 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c 0a 20   char *idxStr,. 
349d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
349e0 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
349f0 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 3b 0a  _value **argv);.
34a00 20 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 73    int (*xNext)(s
34a10 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
34a20 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 45  or*);.  int (*xE
34a30 6f 66 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  of)(sqlite3_vtab
34a40 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74  _cursor*);.  int
34a50 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73 71 6c 69   (*xColumn)(sqli
34a60 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a  te3_vtab_cursor*
34a70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  , sqlite3_contex
34a80 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20  t*, int);.  int 
34a90 28 2a 78 52 6f 77 69 64 29 28 73 71 6c 69 74 65  (*xRowid)(sqlite
34aa0 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a 2c 20  3_vtab_cursor*, 
34ab0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70  sqlite3_int64 *p
34ac0 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74 20 28 2a  Rowid);.  int (*
34ad0 78 55 70 64 61 74 65 29 28 73 71 6c 69 74 65 33  xUpdate)(sqlite3
34ae0 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c 20 73 71  _vtab *, int, sq
34af0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 2c 20  lite3_value **, 
34b00 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 29  sqlite3_int64 *)
34b10 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65 67 69 6e  ;.  int (*xBegin
34b20 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
34b30 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a  pVTab);.  int (*
34b40 78 53 79 6e 63 29 28 73 71 6c 69 74 65 33 5f 76  xSync)(sqlite3_v
34b50 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69  tab *pVTab);.  i
34b60 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29 28 73 71  nt (*xCommit)(sq
34b70 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
34b80 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52 6f 6c  b);.  int (*xRol
34b90 6c 62 61 63 6b 29 28 73 71 6c 69 74 65 33 5f 76  lback)(sqlite3_v
34ba0 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69  tab *pVTab);.  i
34bb0 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e 63 74 69  nt (*xFindFuncti
34bc0 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  on)(sqlite3_vtab
34bd0 20 2a 70 56 74 61 62 2c 20 69 6e 74 20 6e 41 72   *pVtab, int nAr
34be0 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  g, const char *z
34bf0 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Name,.          
34c00 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69               voi
34c10 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c  d (**pxFunc)(sql
34c20 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
34c30 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
34c40 2a 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  *),.            
34c50 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 20             void 
34c60 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69 6e 74 20  **ppArg);.  int 
34c70 28 2a 78 52 65 6e 61 6d 65 29 28 73 71 6c 69 74  (*xRename)(sqlit
34c80 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20  e3_vtab *pVtab, 
34c90 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 77  const char *zNew
34ca0 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  );.};../*.** CAP
34cb0 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54  I3REF: Virtual T
34cc0 61 62 6c 65 20 49 6e 64 65 78 69 6e 67 20 49 6e  able Indexing In
34cd0 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31 38 31 30  formation {H1810
34ce0 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b  0} <S20400>.** K
34cf0 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33  EYWORDS: sqlite3
34d00 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20 45  _index_info.** E
34d10 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
34d20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  * The sqlite3_in
34d30 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
34d40 72 65 20 61 6e 64 20 69 74 73 20 73 75 62 73 74  re and its subst
34d50 72 75 63 74 75 72 65 73 20 69 73 20 75 73 65 64  ructures is used
34d60 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69 6e 66 6f   to.** pass info
34d70 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 61 6e 64  rmation into and
34d80 20 72 65 63 65 69 76 65 20 74 68 65 20 72 65 70   receive the rep
34d90 6c 79 20 66 72 6f 6d 20 74 68 65 20 5b 78 42 65  ly from the [xBe
34da0 73 74 49 6e 64 65 78 5d 0a 2a 2a 20 6d 65 74 68  stIndex].** meth
34db0 6f 64 20 6f 66 20 61 20 5b 76 69 72 74 75 61 6c  od of a [virtual
34dc0 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20   table module]. 
34dd0 20 54 68 65 20 66 69 65 6c 64 73 20 75 6e 64 65   The fields unde
34de0 72 20 2a 2a 49 6e 70 75 74 73 2a 2a 20 61 72 65  r **Inputs** are
34df0 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 73 20 74   the.** inputs t
34e00 6f 20 78 42 65 73 74 49 6e 64 65 78 20 61 6e 64  o xBestIndex and
34e10 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79 2e 20   are read-only. 
34e20 20 78 42 65 73 74 49 6e 64 65 78 20 69 6e 73 65   xBestIndex inse
34e30 72 74 73 20 69 74 73 0a 2a 2a 20 72 65 73 75 6c  rts its.** resul
34e40 74 73 20 69 6e 74 6f 20 74 68 65 20 2a 2a 4f 75  ts into the **Ou
34e50 74 70 75 74 73 2a 2a 20 66 69 65 6c 64 73 2e 0a  tputs** fields..
34e60 2a 2a 0a 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74  **.** The aConst
34e70 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20 72 65  raint[] array re
34e80 63 6f 72 64 73 20 57 48 45 52 45 20 63 6c 61 75  cords WHERE clau
34e90 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f  se constraints o
34ea0 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a  f the form:.**.*
34eb0 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d 6e 20 4f 50  * <pre>column OP
34ec0 20 65 78 70 72 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a   expr</pre>.**.*
34ed0 2a 20 77 68 65 72 65 20 4f 50 20 69 73 20 3d 2c  * where OP is =,
34ee0 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d 2c 20 26 67   &lt;, &lt;=, &g
34ef0 74 3b 2c 20 6f 72 20 26 67 74 3b 3d 2e 20 20 54  t;, or &gt;=.  T
34f00 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 6f 70  he particular op
34f10 65 72 61 74 6f 72 20 69 73 0a 2a 2a 20 73 74 6f  erator is.** sto
34f20 72 65 64 20 69 6e 20 61 43 6f 6e 73 74 72 61 69  red in aConstrai
34f30 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68 65 20 69 6e  nt[].op.  The in
34f40 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d  dex of the colum
34f50 6e 20 69 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a  n is stored in.*
34f60 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e  * aConstraint[].
34f70 69 43 6f 6c 75 6d 6e 2e 20 20 61 43 6f 6e 73 74  iColumn.  aConst
34f80 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20 69  raint[].usable i
34f90 73 20 54 52 55 45 20 69 66 20 74 68 65 0a 2a 2a  s TRUE if the.**
34fa0 20 65 78 70 72 20 6f 6e 20 74 68 65 20 72 69 67   expr on the rig
34fb0 68 74 2d 68 61 6e 64 20 73 69 64 65 20 63 61 6e  ht-hand side can
34fc0 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 28 61   be evaluated (a
34fd0 6e 64 20 74 68 75 73 20 74 68 65 20 63 6f 6e 73  nd thus the cons
34fe0 74 72 61 69 6e 74 0a 2a 2a 20 69 73 20 75 73 61  traint.** is usa
34ff0 62 6c 65 29 20 61 6e 64 20 66 61 6c 73 65 20 69  ble) and false i
35000 66 20 69 74 20 63 61 6e 6e 6f 74 2e 0a 2a 2a 0a  f it cannot..**.
35010 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72  ** The optimizer
35020 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69   automatically i
35030 6e 76 65 72 74 73 20 74 65 72 6d 73 20 6f 66 20  nverts terms of 
35040 74 68 65 20 66 6f 72 6d 20 22 65 78 70 72 20 4f  the form "expr O
35050 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a 20 61 6e 64  P column".** and
35060 20 6d 61 6b 65 73 20 6f 74 68 65 72 20 73 69 6d   makes other sim
35070 70 6c 69 66 69 63 61 74 69 6f 6e 73 20 74 6f 20  plifications to 
35080 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
35090 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 74   in an attempt t
350a0 6f 0a 2a 2a 20 67 65 74 20 61 73 20 6d 61 6e 79  o.** get as many
350b0 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
350c0 72 6d 73 20 69 6e 74 6f 20 74 68 65 20 66 6f 72  rms into the for
350d0 6d 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 61 73  m shown above as
350e0 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 20 54 68   possible..** Th
350f0 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20  e aConstraint[] 
35100 61 72 72 61 79 20 6f 6e 6c 79 20 72 65 70 6f 72  array only repor
35110 74 73 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  ts WHERE clause 
35120 74 65 72 6d 73 20 69 6e 20 74 68 65 20 63 6f 72  terms in the cor
35130 72 65 63 74 0a 2a 2a 20 66 6f 72 6d 20 74 68 61  rect.** form tha
35140 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70  t refer to the p
35150 61 72 74 69 63 75 6c 61 72 20 76 69 72 74 75 61  articular virtua
35160 6c 20 74 61 62 6c 65 20 62 65 69 6e 67 20 71 75  l table being qu
35170 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 66  eried..**.** Inf
35180 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
35190 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
351a0 73 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  se is stored in 
351b0 61 4f 72 64 65 72 42 79 5b 5d 2e 0a 2a 2a 20 45  aOrderBy[]..** E
351c0 61 63 68 20 74 65 72 6d 20 6f 66 20 61 4f 72 64  ach term of aOrd
351d0 65 72 42 79 20 72 65 63 6f 72 64 73 20 61 20 63  erBy records a c
351e0 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 4f 52 44  olumn of the ORD
351f0 45 52 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a  ER BY clause..**
35200 0a 2a 2a 20 54 68 65 20 5b 78 42 65 73 74 49 6e  .** The [xBestIn
35210 64 65 78 5d 20 6d 65 74 68 6f 64 20 6d 75 73 74  dex] method must
35220 20 66 69 6c 6c 20 61 43 6f 6e 73 74 72 61 69 6e   fill aConstrain
35230 74 55 73 61 67 65 5b 5d 20 77 69 74 68 20 69 6e  tUsage[] with in
35240 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f  formation.** abo
35250 75 74 20 77 68 61 74 20 70 61 72 61 6d 65 74 65  ut what paramete
35260 72 73 20 74 6f 20 70 61 73 73 20 74 6f 20 78 46  rs to pass to xF
35270 69 6c 74 65 72 2e 20 20 49 66 20 61 72 67 76 49  ilter.  If argvI
35280 6e 64 65 78 3e 30 20 74 68 65 6e 0a 2a 2a 20 74  ndex>0 then.** t
35290 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
352a0 64 65 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73  de of the corres
352b0 70 6f 6e 64 69 6e 67 20 61 43 6f 6e 73 74 72 61  ponding aConstra
352c0 69 6e 74 5b 5d 20 69 73 20 65 76 61 6c 75 61 74  int[] is evaluat
352d0 65 64 0a 2a 2a 20 61 6e 64 20 62 65 63 6f 6d 65  ed.** and become
352e0 73 20 74 68 65 20 61 72 67 76 49 6e 64 65 78 2d  s the argvIndex-
352f0 74 68 20 65 6e 74 72 79 20 69 6e 20 61 72 67 76  th entry in argv
35300 2e 20 20 49 66 20 61 43 6f 6e 73 74 72 61 69 6e  .  If aConstrain
35310 74 55 73 61 67 65 5b 5d 2e 6f 6d 69 74 0a 2a 2a  tUsage[].omit.**
35320 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
35330 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  he constraint is
35340 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20 66   assumed to be f
35350 75 6c 6c 79 20 68 61 6e 64 6c 65 64 20 62 79 20  ully handled by 
35360 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74  the.** virtual t
35370 61 62 6c 65 20 61 6e 64 20 69 73 20 6e 6f 74 20  able and is not 
35380 63 68 65 63 6b 65 64 20 61 67 61 69 6e 20 62 79  checked again by
35390 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54   SQLite..**.** T
353a0 68 65 20 69 64 78 4e 75 6d 20 61 6e 64 20 69 64  he idxNum and id
353b0 78 50 74 72 20 76 61 6c 75 65 73 20 61 72 65 20  xPtr values are 
353c0 72 65 63 6f 72 64 65 64 20 61 6e 64 20 70 61 73  recorded and pas
353d0 73 65 64 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20  sed into the.** 
353e0 5b 78 46 69 6c 74 65 72 5d 20 6d 65 74 68 6f 64  [xFilter] method
353f0 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 72  ..** [sqlite3_fr
35400 65 65 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f  ee()] is used to
35410 20 66 72 65 65 20 69 64 78 50 74 72 20 69 66 20   free idxPtr if 
35420 61 6e 64 20 6f 6e 6c 79 20 69 66 66 0a 2a 2a 20  and only iff.** 
35430 6e 65 65 64 54 6f 46 72 65 65 49 64 78 50 74 72  needToFreeIdxPtr
35440 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20   is true..**.** 
35450 54 68 65 20 6f 72 64 65 72 42 79 43 6f 6e 73 75  The orderByConsu
35460 6d 65 64 20 6d 65 61 6e 73 20 74 68 61 74 20 6f  med means that o
35470 75 74 70 75 74 20 66 72 6f 6d 20 5b 78 46 69 6c  utput from [xFil
35480 74 65 72 5d 2f 5b 78 4e 65 78 74 5d 20 77 69 6c  ter]/[xNext] wil
35490 6c 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 74 68  l occur in.** th
354a0 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72 20  e correct order 
354b0 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 4f  to satisfy the O
354c0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 73  RDER BY clause s
354d0 6f 20 74 68 61 74 20 6e 6f 20 73 65 70 61 72 61  o that no separa
354e0 74 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 73 74  te.** sorting st
354f0 65 70 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a  ep is required..
35500 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61  **.** The estima
35510 74 65 64 43 6f 73 74 20 76 61 6c 75 65 20 69 73  tedCost value is
35520 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20   an estimate of 
35530 74 68 65 20 63 6f 73 74 20 6f 66 20 64 6f 69 6e  the cost of doin
35540 67 20 74 68 65 0a 2a 2a 20 70 61 72 74 69 63 75  g the.** particu
35550 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20 20 41 20 66  lar lookup.  A f
35560 75 6c 6c 20 73 63 61 6e 20 6f 66 20 61 20 74 61  ull scan of a ta
35570 62 6c 65 20 77 69 74 68 20 4e 20 65 6e 74 72 69  ble with N entri
35580 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 0a 2a  es should have.*
35590 2a 20 61 20 63 6f 73 74 20 6f 66 20 4e 2e 20 20  * a cost of N.  
355a0 41 20 62 69 6e 61 72 79 20 73 65 61 72 63 68 20  A binary search 
355b0 6f 66 20 61 20 74 61 62 6c 65 20 6f 66 20 4e 20  of a table of N 
355c0 65 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68  entries should h
355d0 61 76 65 20 61 0a 2a 2a 20 63 6f 73 74 20 6f 66  ave a.** cost of
355e0 20 61 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 6c   approximately l
355f0 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74 72 75 63 74  og(N)..*/.struct
35600 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
35610 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49 6e 70 75 74  nfo {.  /* Input
35620 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73  s */.  int nCons
35630 74 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20  traint;         
35640 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
35650 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6e 73 74  ntries in aConst
35660 72 61 69 6e 74 20 2a 2f 0a 20 20 73 74 72 75 63  raint */.  struc
35670 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
35680 63 6f 6e 73 74 72 61 69 6e 74 20 7b 0a 20 20 20  constraint {.   
35690 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
356a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
356b0 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66 74 2d 68 61  olumn on left-ha
356c0 6e 64 20 73 69 64 65 20 6f 66 20 63 6f 6e 73 74  nd side of const
356d0 72 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 75 6e  raint */.     un
356e0 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70 3b 20  signed char op; 
356f0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74          /* Const
35700 72 61 69 6e 74 20 6f 70 65 72 61 74 6f 72 20 2a  raint operator *
35710 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  /.     unsigned 
35720 63 68 61 72 20 75 73 61 62 6c 65 3b 20 20 20 20  char usable;    
35730 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
35740 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75   constraint is u
35750 73 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 69 6e  sable */.     in
35760 74 20 69 54 65 72 6d 4f 66 66 73 65 74 3b 20 20  t iTermOffset;  
35770 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20          /* Used 
35780 69 6e 74 65 72 6e 61 6c 6c 79 20 2d 20 78 42 65  internally - xBe
35790 73 74 49 6e 64 65 78 20 73 68 6f 75 6c 64 20 69  stIndex should i
357a0 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d 20 2a 61 43  gnore */.  } *aC
357b0 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20  onstraint;      
357c0 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 6f        /* Table o
357d0 66 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 63  f WHERE clause c
357e0 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
357f0 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20  int nOrderBy;   
35800 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
35810 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e  mber of terms in
35820 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
35830 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  ause */.  struct
35840 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f   sqlite3_index_o
35850 72 64 65 72 62 79 20 7b 0a 20 20 20 20 20 69 6e  rderby {.     in
35860 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t iColumn;      
35870 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
35880 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  n number */.    
35890 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 64   unsigned char d
358a0 65 73 63 3b 20 20 20 20 20 20 20 2f 2a 20 54 72  esc;       /* Tr
358b0 75 65 20 66 6f 72 20 44 45 53 43 2e 20 20 46 61  ue for DESC.  Fa
358c0 6c 73 65 20 66 6f 72 20 41 53 43 2e 20 2a 2f 0a  lse for ASC. */.
358d0 20 20 7d 20 2a 61 4f 72 64 65 72 42 79 3b 20 20    } *aOrderBy;  
358e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
358f0 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
35900 75 73 65 20 2a 2f 0a 20 20 2f 2a 20 4f 75 74 70  use */.  /* Outp
35910 75 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  uts */.  struct 
35920 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
35930 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 20 7b  nstraint_usage {
35940 0a 20 20 20 20 69 6e 74 20 61 72 67 76 49 6e 64  .    int argvInd
35950 65 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ex;           /*
35960 20 69 66 20 3e 30 2c 20 63 6f 6e 73 74 72 61 69   if >0, constrai
35970 6e 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 72  nt is part of ar
35980 67 76 20 74 6f 20 78 46 69 6c 74 65 72 20 2a 2f  gv to xFilter */
35990 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
359a0 61 72 20 6f 6d 69 74 3b 20 20 20 20 20 20 2f 2a  ar omit;      /*
359b0 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20 61 20 74   Do not code a t
359c0 65 73 74 20 66 6f 72 20 74 68 69 73 20 63 6f 6e  est for this con
359d0 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 7d 20 2a  straint */.  } *
359e0 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
359f0 3b 0a 20 20 69 6e 74 20 69 64 78 4e 75 6d 3b 20  ;.  int idxNum; 
35a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35a10 2a 20 4e 75 6d 62 65 72 20 75 73 65 64 20 74 6f  * Number used to
35a20 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 69 6e   identify the in
35a30 64 65 78 20 2a 2f 0a 20 20 63 68 61 72 20 2a 69  dex */.  char *i
35a40 64 78 53 74 72 3b 20 20 20 20 20 20 20 20 20 20  dxStr;          
35a50 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 2c 20 70      /* String, p
35a60 6f 73 73 69 62 6c 79 20 6f 62 74 61 69 6e 65 64  ossibly obtained
35a70 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
35a80 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e 74 20 6e 65  lloc */.  int ne
35a90 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 3b 20  edToFreeIdxStr; 
35aa0 20 20 20 20 20 2f 2a 20 46 72 65 65 20 69 64 78       /* Free idx
35ab0 53 74 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65  Str using sqlite
35ac0 33 5f 66 72 65 65 28 29 20 69 66 20 74 72 75 65  3_free() if true
35ad0 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 64 65 72 42   */.  int orderB
35ae0 79 43 6f 6e 73 75 6d 65 64 3b 20 20 20 20 20 20  yConsumed;      
35af0 20 2f 2a 20 54 72 75 65 20 69 66 20 6f 75 74 70   /* True if outp
35b00 75 74 20 69 73 20 61 6c 72 65 61 64 79 20 6f 72  ut is already or
35b10 64 65 72 65 64 20 2a 2f 0a 20 20 64 6f 75 62 6c  dered */.  doubl
35b20 65 20 65 73 74 69 6d 61 74 65 64 43 6f 73 74 3b  e estimatedCost;
35b30 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d 61 74        /* Estimat
35b40 65 64 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67  ed cost of using
35b50 20 74 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 7d   this index */.}
35b60 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ;.#define SQLITE
35b70 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
35b80 54 5f 45 51 20 20 20 20 32 0a 23 64 65 66 69 6e  T_EQ    2.#defin
35b90 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
35ba0 4f 4e 53 54 52 41 49 4e 54 5f 47 54 20 20 20 20  ONSTRAINT_GT    
35bb0 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  4.#define SQLITE
35bc0 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
35bd0 54 5f 4c 45 20 20 20 20 38 0a 23 64 65 66 69 6e  T_LE    8.#defin
35be0 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
35bf0 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 20 20 20  ONSTRAINT_LT    
35c00 31 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  16.#define SQLIT
35c10 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
35c20 4e 54 5f 47 45 20 20 20 20 33 32 0a 23 64 65 66  NT_GE    32.#def
35c30 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  ine SQLITE_INDEX
35c40 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43  _CONSTRAINT_MATC
35c50 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  H 64../*.** CAPI
35c60 33 52 45 46 3a 20 52 65 67 69 73 74 65 72 20 41  3REF: Register A
35c70 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 49   Virtual Table I
35c80 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 7b 48  mplementation {H
35c90 31 38 32 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a  18200} <S20400>.
35ca0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
35cb0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
35cc0 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ne is used to re
35cd0 67 69 73 74 65 72 20 61 20 6e 65 77 20 5b 76 69  gister a new [vi
35ce0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
35cf0 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a 20 4d 6f 64  le] name..** Mod
35d00 75 6c 65 20 6e 61 6d 65 73 20 6d 75 73 74 20 62  ule names must b
35d10 65 20 72 65 67 69 73 74 65 72 65 64 20 62 65 66  e registered bef
35d20 6f 72 65 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20  ore.** creating 
35d30 61 20 6e 65 77 20 5b 76 69 72 74 75 61 6c 20 74  a new [virtual t
35d40 61 62 6c 65 5d 20 75 73 69 6e 67 20 74 68 65 20  able] using the 
35d50 6d 6f 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72  module, or befor
35d60 65 20 75 73 69 6e 67 20 61 0a 2a 2a 20 70 72 65  e using a.** pre
35d70 65 78 69 73 74 69 6e 67 20 5b 76 69 72 74 75 61  existing [virtua
35d80 6c 20 74 61 62 6c 65 5d 20 66 6f 72 20 74 68 65  l table] for the
35d90 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54   module..**.** T
35da0 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69  he module name i
35db0 73 20 72 65 67 69 73 74 65 72 65 64 20 6f 6e 20  s registered on 
35dc0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
35dd0 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69 66  nnection] specif
35de0 69 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 66 69  ied.** by the fi
35df0 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20 20  rst parameter.  
35e00 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
35e10 6d 6f 64 75 6c 65 20 69 73 20 67 69 76 65 6e 20  module is given 
35e20 62 79 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e  by the .** secon
35e30 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68  d parameter.  Th
35e40 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
35e50 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  r is a pointer t
35e60 6f 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65  o.** the impleme
35e70 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b  ntation of the [
35e80 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
35e90 64 75 6c 65 5d 2e 20 20 20 54 68 65 20 66 6f 75  dule].   The fou
35ea0 72 74 68 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  rth.** parameter
35eb0 20 69 73 20 61 6e 20 61 72 62 69 74 72 61 72 79   is an arbitrary
35ec0 20 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f 69   client data poi
35ed0 6e 74 65 72 20 74 68 61 74 20 69 73 20 70 61 73  nter that is pas
35ee0 73 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 69  sed through.** i
35ef0 6e 74 6f 20 74 68 65 20 5b 78 43 72 65 61 74 65  nto the [xCreate
35f00 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65 63 74 5d  ] and [xConnect]
35f10 20 6d 65 74 68 6f 64 73 20 6f 66 20 74 68 65 20   methods of the 
35f20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
35f30 64 75 6c 65 0a 2a 2a 20 77 68 65 6e 20 61 20 6e  dule.** when a n
35f40 65 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ew virtual table
35f50 20 69 73 20 62 65 20 62 65 69 6e 67 20 63 72 65   is be being cre
35f60 61 74 65 64 20 6f 72 20 72 65 69 6e 69 74 69 61  ated or reinitia
35f70 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  lized..**.** Thi
35f80 73 20 69 6e 74 65 72 66 61 63 65 20 68 61 73 20  s interface has 
35f90 65 78 61 63 74 6c 79 20 74 68 65 20 73 61 6d 65  exactly the same
35fa0 20 65 66 66 65 63 74 20 61 73 20 63 61 6c 6c 69   effect as calli
35fb0 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63  ng.** [sqlite3_c
35fc0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28  reate_module_v2(
35fd0 29 5d 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 63  )] with a NULL c
35fe0 6c 69 65 6e 74 20 64 61 74 61 20 64 65 73 74 72  lient data destr
35ff0 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  uctor..*/.SQLITE
36000 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
36010 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c  RIMENTAL int sql
36020 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
36030 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  le(.  sqlite3 *d
36040 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
36050 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65   /* SQLite conne
36060 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65  ction to registe
36070 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f  r module with */
36080 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
36090 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  Name,         /*
360a0 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64   Name of the mod
360b0 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ule */.  const s
360c0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
360d0 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66  ,   /* Methods f
360e0 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  or the module */
360f0 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74  .  void *pClient
36100 44 61 74 61 20 20 20 20 20 20 20 20 20 20 2f 2a  Data          /*
36110 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72   Client data for
36120 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63   xCreate/xConnec
36130 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  t */.);../*.** C
36140 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74 65  API3REF: Registe
36150 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62 6c  r A Virtual Tabl
36160 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e Implementation
36170 20 7b 48 31 38 32 31 30 7d 20 3c 53 32 30 34 30   {H18210} <S2040
36180 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
36190 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  AL.**.** This ro
361a0 75 74 69 6e 65 20 69 73 20 69 64 65 6e 74 69 63  utine is identic
361b0 61 6c 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  al to the [sqlit
361c0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
361d0 28 29 5d 20 6d 65 74 68 6f 64 2c 0a 2a 2a 20 65  ()] method,.** e
361e0 78 63 65 70 74 20 74 68 61 74 20 69 74 20 68 61  xcept that it ha
361f0 73 20 61 6e 20 65 78 74 72 61 20 70 61 72 61 6d  s an extra param
36200 65 74 65 72 20 74 6f 20 73 70 65 63 69 66 79 20  eter to specify 
36210 0a 2a 2a 20 61 20 64 65 73 74 72 75 63 74 6f 72  .** a destructor
36220 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68   function for th
36230 65 20 63 6c 69 65 6e 74 20 64 61 74 61 20 70 6f  e client data po
36240 69 6e 74 65 72 2e 20 20 53 51 4c 69 74 65 20 77  inter.  SQLite w
36250 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  ill.** invoke th
36260 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  e destructor fun
36270 63 74 69 6f 6e 20 28 69 66 20 69 74 20 69 73 20  ction (if it is 
36280 6e 6f 74 20 4e 55 4c 4c 29 20 77 68 65 6e 20 53  not NULL) when S
36290 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67  QLite.** no long
362a0 65 72 20 6e 65 65 64 73 20 74 68 65 20 70 43 6c  er needs the pCl
362b0 69 65 6e 74 44 61 74 61 20 70 6f 69 6e 74 65 72  ientData pointer
362c0 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  .  .*/.SQLITE_AP
362d0 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  I SQLITE_EXPERIM
362e0 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65  ENTAL int sqlite
362f0 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
36300 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v2(.  sqlite3 *d
36310 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
36320 20 2f 2a 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65   /* SQLite conne
36330 63 74 69 6f 6e 20 74 6f 20 72 65 67 69 73 74 65  ction to registe
36340 72 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f  r module with */
36350 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
36360 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  Name,         /*
36370 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64   Name of the mod
36380 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73  ule */.  const s
36390 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
363a0 2c 20 20 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66  ,   /* Methods f
363b0 6f 72 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  or the module */
363c0 0a 20 20 76 6f 69 64 20 2a 70 43 6c 69 65 6e 74  .  void *pClient
363d0 44 61 74 61 2c 20 20 20 20 20 20 20 20 20 2f 2a  Data,         /*
363e0 20 43 6c 69 65 6e 74 20 64 61 74 61 20 66 6f 72   Client data for
363f0 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63   xCreate/xConnec
36400 74 20 2a 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65  t */.  void(*xDe
36410 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 20 20 20  stroy)(void*)   
36420 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74    /* Module dest
36430 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ructor function 
36440 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.);../*.** CAP
36450 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54  I3REF: Virtual T
36460 61 62 6c 65 20 49 6e 73 74 61 6e 63 65 20 4f 62  able Instance Ob
36470 6a 65 63 74 20 7b 48 31 38 30 31 30 7d 20 3c 53  ject {H18010} <S
36480 32 30 34 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  20400>.** KEYWOR
36490 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 74 61 62  DS: sqlite3_vtab
364a0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
364b0 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 5b 76 69  .**.** Every [vi
364c0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
364d0 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  le] implementati
364e0 6f 6e 20 75 73 65 73 20 61 20 73 75 62 63 6c 61  on uses a subcla
364f0 73 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ss.** of the fol
36500 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
36510 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70   to describe a p
36520 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61 6e  articular instan
36530 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 76 69  ce.** of the [vi
36540 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e 20 20 45  rtual table].  E
36550 61 63 68 20 73 75 62 63 6c 61 73 73 20 77 69 6c  ach subclass wil
36560 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 65 64  l.** be tailored
36570 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 63   to the specific
36580 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20 6d 6f   needs of the mo
36590 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  dule implementat
365a0 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 72 70  ion..** The purp
365b0 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75 70 65  ose of this supe
365c0 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64 65 66  rclass is to def
365d0 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69 65 6c  ine certain fiel
365e0 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 63  ds that are.** c
365f0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d 6f 64  ommon to all mod
36600 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ule implementati
36610 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75  ons..**.** Virtu
36620 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 6f 64  al tables method
36630 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65 72 72  s can set an err
36640 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20 61 73  or message by as
36650 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 74 72  signing a.** str
36660 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ing obtained fro
36670 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e  m [sqlite3_mprin
36680 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d 73 67  tf()] to zErrMsg
36690 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20 73 68  .  The method sh
366a0 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 61 72  ould.** take car
366b0 65 20 74 68 61 74 20 61 6e 79 20 70 72 69 6f 72  e that any prior
366c0 20 73 74 72 69 6e 67 20 69 73 20 66 72 65 65 64   string is freed
366d0 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73   by a call to [s
366e0 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 0a 2a  qlite3_free()].*
366f0 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69 67  * prior to assig
36700 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69 6e  ning a new strin
36710 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20 41  g to zErrMsg.  A
36720 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 6d  fter the error m
36730 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65 6c  essage.** is del
36740 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 68 65  ivered up to the
36750 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61 74   client applicat
36760 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67 20  ion, the string 
36770 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69  will be automati
36780 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20 62  cally.** freed b
36790 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29  y sqlite3_free()
367a0 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73 67   and the zErrMsg
367b0 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20 7a   field will be z
367c0 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74  eroed..*/.struct
367d0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 7b 0a   sqlite3_vtab {.
367e0 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
367f0 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
36800 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20    /* The module 
36810 66 6f 72 20 74 68 69 73 20 76 69 72 74 75 61 6c  for this virtual
36820 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
36830 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
36840 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
36850 4f 20 4c 4f 4e 47 45 52 20 55 53 45 44 20 2a 2f  O LONGER USED */
36860 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
36870 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36880 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
36890 61 67 65 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  age from sqlite3
368a0 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a 20 20  _mprintf() */.  
368b0 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
368c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
368d0 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
368e0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  add additional f
368f0 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ields */.};../*.
36900 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72  ** CAPI3REF: Vir
36910 74 75 61 6c 20 54 61 62 6c 65 20 43 75 72 73 6f  tual Table Curso
36920 72 20 4f 62 6a 65 63 74 20 20 7b 48 31 38 30 32  r Object  {H1802
36930 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b  0} <S20400>.** K
36940 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33  EYWORDS: sqlite3
36950 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 76 69  _vtab_cursor {vi
36960 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73  rtual table curs
36970 6f 72 7d 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  or}.** EXPERIMEN
36980 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20  TAL.**.** Every 
36990 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d  [virtual table m
369a0 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e 74  odule] implement
369b0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62  ation uses a sub
369c0 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20  class of the.** 
369d0 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
369e0 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65 20  ure to describe 
369f0 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69  cursors that poi
36a00 6e 74 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 5b  nt into the.** [
36a10 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 61  virtual table] a
36a20 6e 64 20 61 72 65 20 75 73 65 64 0a 2a 2a 20 74  nd are used.** t
36a30 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  o loop through t
36a40 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
36a50 2e 20 20 43 75 72 73 6f 72 73 20 61 72 65 20 63  .  Cursors are c
36a60 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 65  reated using the
36a70 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 6f 64  .** [sqlite3_mod
36a80 75 6c 65 2e 78 4f 70 65 6e 20 7c 20 78 4f 70 65  ule.xOpen | xOpe
36a90 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  n] method of the
36aa0 20 6d 6f 64 75 6c 65 20 61 6e 64 20 61 72 65 20   module and are 
36ab0 64 65 73 74 72 6f 79 65 64 0a 2a 2a 20 62 79 20  destroyed.** by 
36ac0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 6f 64  the [sqlite3_mod
36ad0 75 6c 65 2e 78 43 6c 6f 73 65 20 7c 20 78 43 6c  ule.xClose | xCl
36ae0 6f 73 65 5d 20 6d 65 74 68 6f 64 2e 20 20 43 75  ose] method.  Cu
36af0 73 73 6f 72 73 20 61 72 65 20 75 73 65 64 0a 2a  ssors are used.*
36b00 2a 20 62 79 20 74 68 65 20 5b 78 46 69 6c 74 65  * by the [xFilte
36b10 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c 20 5b 78 45  r], [xNext], [xE
36b20 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d 6e 5d 2c 20  of], [xColumn], 
36b30 61 6e 64 20 5b 78 52 6f 77 69 64 5d 20 6d 65 74  and [xRowid] met
36b40 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 65 20 6d  hods.** of the m
36b50 6f 64 75 6c 65 2e 20 20 45 61 63 68 20 6d 6f 64  odule.  Each mod
36b60 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ule implementati
36b70 6f 6e 20 77 69 6c 6c 20 64 65 66 69 6e 65 0a 2a  on will define.*
36b80 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
36b90 20 61 20 63 75 72 73 6f 72 20 73 74 72 75 63 74   a cursor struct
36ba0 75 72 65 20 74 6f 20 73 75 69 74 20 69 74 73 20  ure to suit its 
36bb0 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a  own needs..**.**
36bc0 20 54 68 69 73 20 73 75 70 65 72 63 6c 61 73 73   This superclass
36bd0 20 65 78 69 73 74 73 20 69 6e 20 6f 72 64 65 72   exists in order
36be0 20 74 6f 20 64 65 66 69 6e 65 20 66 69 65 6c 64   to define field
36bf0 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 20  s of the cursor 
36c00 74 68 61 74 0a 2a 2a 20 61 72 65 20 63 6f 6d 6d  that.** are comm
36c10 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d 70 6c 65 6d  on to all implem
36c20 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  entations..*/.st
36c30 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61  ruct sqlite3_vta
36c40 62 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  b_cursor {.  sql
36c50 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
36c60 3b 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61  ;      /* Virtua
36c70 6c 20 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20  l table of this 
36c80 63 75 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56  cursor */.  /* V
36c90 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70  irtual table imp
36ca0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c  lementations wil
36cb0 6c 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20  l typically add 
36cc0 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64  additional field
36cd0 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43  s */.};../*.** C
36ce0 41 50 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65  API3REF: Declare
36cf0 20 54 68 65 20 53 63 68 65 6d 61 20 4f 66 20 41   The Schema Of A
36d00 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b   Virtual Table {
36d10 48 31 38 32 38 30 7d 20 3c 53 32 30 34 30 30 3e  H18280} <S20400>
36d20 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
36d30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 43 72 65  .**.** The [xCre
36d40 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e 65  ate] and [xConne
36d50 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20 61  ct] methods of a
36d60 0a 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62  .** [virtual tab
36d70 6c 65 20 6d 6f 64 75 6c 65 5d 20 63 61 6c 6c 20  le module] call 
36d80 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 0a 2a  this interface.*
36d90 2a 20 74 6f 20 64 65 63 6c 61 72 65 20 74 68 65  * to declare the
36da0 20 66 6f 72 6d 61 74 20 28 74 68 65 20 6e 61 6d   format (the nam
36db0 65 73 20 61 6e 64 20 64 61 74 61 74 79 70 65 73  es and datatypes
36dc0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 29   of the columns)
36dd0 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75   of.** the virtu
36de0 61 6c 20 74 61 62 6c 65 73 20 74 68 65 79 20 69  al tables they i
36df0 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  mplement..*/.SQL
36e00 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45  ITE_API SQLITE_E
36e10 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20  XPERIMENTAL int 
36e20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
36e30 76 74 61 62 28 73 71 6c 69 74 65 33 2a 2c 20 63  vtab(sqlite3*, c
36e40 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 51 4c 29  onst char *zSQL)
36e50 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
36e60 46 3a 20 4f 76 65 72 6c 6f 61 64 20 41 20 46 75  F: Overload A Fu
36e70 6e 63 74 69 6f 6e 20 46 6f 72 20 41 20 56 69 72  nction For A Vir
36e80 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31 38 33  tual Table {H183
36e90 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20  00} <S20400>.** 
36ea0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
36eb0 2a 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  ** Virtual table
36ec0 73 20 63 61 6e 20 70 72 6f 76 69 64 65 20 61 6c  s can provide al
36ed0 74 65 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d  ternative implem
36ee0 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 66 75 6e  entations of fun
36ef0 63 74 69 6f 6e 73 0a 2a 2a 20 75 73 69 6e 67 20  ctions.** using 
36f00 74 68 65 20 5b 78 46 69 6e 64 46 75 6e 63 74 69  the [xFindFuncti
36f10 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  on] method of th
36f20 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65  e [virtual table
36f30 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a 2a 2a 20 42   module].  .** B
36f40 75 74 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f  ut global versio
36f50 6e 73 20 6f 66 20 74 68 6f 73 65 20 66 75 6e 63  ns of those func
36f60 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73 74 20 65 78  tions.** must ex
36f70 69 73 74 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ist in order to 
36f80 62 65 20 6f 76 65 72 6c 6f 61 64 65 64 2e 0a 2a  be overloaded..*
36f90 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 6d 61  *.** This API ma
36fa0 6b 65 73 20 73 75 72 65 20 61 20 67 6c 6f 62 61  kes sure a globa
36fb0 6c 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 20 66  l version of a f
36fc0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61 20 70  unction with a p
36fd0 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 6e 61 6d  articular.** nam
36fe0 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20  e and number of 
36ff0 70 61 72 61 6d 65 74 65 72 73 20 65 78 69 73 74  parameters exist
37000 73 2e 20 20 49 66 20 6e 6f 20 73 75 63 68 20 66  s.  If no such f
37010 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 0a 2a  unction exists.*
37020 2a 20 62 65 66 6f 72 65 20 74 68 69 73 20 41 50  * before this AP
37030 49 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 20 6e  I is called, a n
37040 65 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  ew function is c
37050 72 65 61 74 65 64 2e 20 20 54 68 65 20 69 6d 70  reated.  The imp
37060 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f  lementation.** o
37070 66 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74 69  f the new functi
37080 6f 6e 20 61 6c 77 61 79 73 20 63 61 75 73 65 73  on always causes
37090 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 74 6f   an exception to
370a0 20 62 65 20 74 68 72 6f 77 6e 2e 20 20 53 6f 0a   be thrown.  So.
370b0 2a 2a 20 74 68 65 20 6e 65 77 20 66 75 6e 63 74  ** the new funct
370c0 69 6f 6e 20 69 73 20 6e 6f 74 20 67 6f 6f 64 20  ion is not good 
370d0 66 6f 72 20 61 6e 79 74 68 69 6e 67 20 62 79 20  for anything by 
370e0 69 74 73 65 6c 66 2e 20 20 49 74 73 20 6f 6e 6c  itself.  Its onl
370f0 79 0a 2a 2a 20 70 75 72 70 6f 73 65 20 69 73 20  y.** purpose is 
37100 74 6f 20 62 65 20 61 20 70 6c 61 63 65 68 6f 6c  to be a placehol
37110 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  der function tha
37120 74 20 63 61 6e 20 62 65 20 6f 76 65 72 6c 6f 61  t can be overloa
37130 64 65 64 0a 2a 2a 20 62 79 20 61 20 5b 76 69 72  ded.** by a [vir
37140 74 75 61 6c 20 74 61 62 6c 65 5d 2e 0a 2a 2f 0a  tual table]..*/.
37150 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
37160 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69  E_EXPERIMENTAL i
37170 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c  nt sqlite3_overl
37180 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c  oad_function(sql
37190 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61  ite3*, const cha
371a0 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e  r *zFuncName, in
371b0 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t nArg);../*.** 
371c0 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f  The interface to
371d0 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62   the virtual-tab
371e0 6c 65 20 6d 65 63 68 61 6e 69 73 6d 20 64 65 66  le mechanism def
371f0 69 6e 65 64 20 61 62 6f 76 65 20 28 62 61 63 6b  ined above (back
37200 20 75 70 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d   up.** to a comm
37210 65 6e 74 20 72 65 6d 61 72 6b 61 62 6c 79 20 73  ent remarkably s
37220 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f  imilar to this o
37230 6e 65 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ne) is currently
37240 20 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74   considered.** t
37250 6f 20 62 65 20 65 78 70 65 72 69 6d 65 6e 74 61  o be experimenta
37260 6c 2e 20 20 54 68 65 20 69 6e 74 65 72 66 61 63  l.  The interfac
37270 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69  e might change i
37280 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77  n incompatible w
37290 61 79 73 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20  ays..** If this 
372a0 69 73 20 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72  is a problem for
372b0 20 79 6f 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65   you, do not use
372c0 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 61   the interface a
372d0 74 20 74 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a  t this time..**.
372e0 2a 2a 20 57 68 65 6e 20 74 68 65 20 76 69 72 74  ** When the virt
372f0 75 61 6c 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e  ual-table mechan
37300 69 73 6d 20 73 74 61 62 69 6c 69 7a 65 73 2c 20  ism stabilizes, 
37310 77 65 20 77 69 6c 6c 20 64 65 63 6c 61 72 65 20  we will declare 
37320 74 68 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  the.** interface
37330 20 66 69 78 65 64 2c 20 73 75 70 70 6f 72 74 20   fixed, support 
37340 69 74 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c  it indefinitely,
37350 20 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 69 73   and remove this
37360 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a   comment..**.***
37370 2a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  *** EXPERIMENTAL
37380 20 2d 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68   - subject to ch
37390 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74  ange without not
373a0 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ice ************
373b0 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  **.*/../*.** CAP
373c0 49 33 52 45 46 3a 20 41 20 48 61 6e 64 6c 65 20  I3REF: A Handle 
373d0 54 6f 20 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20  To An Open BLOB 
373e0 7b 48 31 37 38 30 30 7d 20 3c 53 33 30 32 33 30  {H17800} <S30230
373f0 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
37400 42 4c 4f 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c  BLOB handle} {BL
37410 4f 42 20 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a  OB handles}.**.*
37420 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
37430 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70   this object rep
37440 72 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20  resents an open 
37450 42 4c 4f 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a  BLOB on which.**
37460 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
37470 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61  pen | incrementa
37480 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20  l BLOB I/O] can 
37490 62 65 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a  be performed..**
374a0 20 4f 62 6a 65 63 74 73 20 6f 66 20 74 68 69 73   Objects of this
374b0 20 74 79 70 65 20 61 72 65 20 63 72 65 61 74 65   type are create
374c0 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c  d by [sqlite3_bl
374d0 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e  ob_open()].** an
374e0 64 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b  d destroyed by [
374f0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
37500 73 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73  se()]..** The [s
37510 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64  qlite3_blob_read
37520 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
37530 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 20 69  _blob_write()] i
37540 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e  nterfaces.** can
37550 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 61 64   be used to read
37560 20 6f 72 20 77 72 69 74 65 20 73 6d 61 6c 6c 20   or write small 
37570 73 75 62 73 65 63 74 69 6f 6e 73 20 6f 66 20 74  subsections of t
37580 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20  he BLOB..** The 
37590 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79  [sqlite3_blob_by
375a0 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tes()] interface
375b0 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a   returns the siz
375c0 65 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 69 6e  e of the BLOB in
375d0 20 62 79 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64   bytes..*/.typed
375e0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
375f0 33 5f 62 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62  3_blob sqlite3_b
37600 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lob;../*.** CAPI
37610 33 52 45 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f  3REF: Open A BLO
37620 42 20 46 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61  B For Incrementa
37630 6c 20 49 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c  l I/O {H17810} <
37640 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30230>.**.** Th
37650 69 73 20 69 6e 74 65 72 66 61 63 65 73 20 6f 70  is interfaces op
37660 65 6e 73 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64  ens a [BLOB hand
37670 6c 65 20 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20  le | handle] to 
37680 74 68 65 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64  the BLOB located
37690 0a 2a 2a 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c  .** in row iRow,
376a0 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c   column zColumn,
376b0 20 74 61 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e   table zTable in
376c0 20 64 61 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a   database zDb;.*
376d0 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * in other words
376e0 2c 20 74 68 65 20 73 61 6d 65 20 42 4c 4f 42 20  , the same BLOB 
376f0 74 68 61 74 20 77 6f 75 6c 64 20 62 65 20 73 65  that would be se
37700 6c 65 63 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a  lected by:.**.**
37710 20 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45   <pre>.**     SE
37720 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f  LECT zColumn FRO
37730 4d 20 7a 44 62 2e 7a 54 61 62 6c 65 20 57 48 45  M zDb.zTable WHE
37740 52 45 20 5b 72 6f 77 69 64 5d 20 3d 20 69 52 6f  RE [rowid] = iRo
37750 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 20 7b 45 4e  w;.** </pre> {EN
37760 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  D}.**.** If the 
37770 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
37780 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
37790 6e 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70  n the BLOB is op
377a0 65 6e 65 64 20 66 6f 72 20 72 65 61 64 0a 2a 2a  ened for read.**
377b0 20 61 6e 64 20 77 72 69 74 65 20 61 63 63 65 73   and write acces
377c0 73 2e 20 49 66 20 69 74 20 69 73 20 7a 65 72 6f  s. If it is zero
377d0 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20 6f 70  , the BLOB is op
377e0 65 6e 65 64 20 66 6f 72 20 72 65 61 64 20 61 63  ened for read ac
377f0 63 65 73 73 2e 0a 2a 2a 20 49 74 20 69 73 20 6e  cess..** It is n
37800 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f  ot possible to o
37810 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61  pen a column tha
37820 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20  t is part of an 
37830 69 6e 64 65 78 20 6f 72 20 70 72 69 6d 61 72 79  index or primary
37840 20 0a 2a 2a 20 6b 65 79 20 66 6f 72 20 77 72 69   .** key for wri
37850 74 69 6e 67 2e 20 5e 49 66 20 5b 66 6f 72 65 69  ting. ^If [forei
37860 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
37870 74 73 5d 20 61 72 65 20 65 6e 61 62 6c 65 64 2c  ts] are enabled,
37880 20 69 74 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 70   it is .** not p
37890 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 65 6e 20  ossible to open 
378a0 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73  a column that is
378b0 20 70 61 72 74 20 6f 66 20 61 20 5b 63 68 69 6c   part of a [chil
378c0 64 20 6b 65 79 5d 20 66 6f 72 20 77 72 69 74 69  d key] for writi
378d0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  ng..**.** Note t
378e0 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
378f0 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 74 68 65   name is not the
37900 20 66 69 6c 65 6e 61 6d 65 20 74 68 61 74 20 63   filename that c
37910 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 64  ontains.** the d
37920 61 74 61 62 61 73 65 20 62 75 74 20 72 61 74 68  atabase but rath
37930 65 72 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20  er the symbolic 
37940 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
37950 62 61 73 65 20 74 68 61 74 0a 2a 2a 20 69 73 20  base that.** is 
37960 61 73 73 69 67 6e 65 64 20 77 68 65 6e 20 74 68  assigned when th
37970 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f  e database is co
37980 6e 6e 65 63 74 65 64 20 75 73 69 6e 67 20 5b 41  nnected using [A
37990 54 54 41 43 48 5d 2e 0a 2a 2a 20 46 6f 72 20 74  TTACH]..** For t
379a0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
379b0 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 62   file, the datab
379c0 61 73 65 20 6e 61 6d 65 20 69 73 20 22 6d 61 69  ase name is "mai
379d0 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54 45 4d 50 20  n"..** For TEMP 
379e0 74 61 62 6c 65 73 2c 20 74 68 65 20 64 61 74 61  tables, the data
379f0 62 61 73 65 20 6e 61 6d 65 20 69 73 20 22 74 65  base name is "te
37a00 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75  mp"..**.** On su
37a10 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f  ccess, [SQLITE_O
37a20 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  K] is returned a
37a30 6e 64 20 74 68 65 20 6e 65 77 20 5b 42 4c 4f 42  nd the new [BLOB
37a40 20 68 61 6e 64 6c 65 5d 20 69 73 20 77 72 69 74   handle] is writ
37a50 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f  ten.** to *ppBlo
37a60 62 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20  b. Otherwise an 
37a70 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20  [error code] is 
37a80 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70  returned and *pp
37a90 42 6c 6f 62 20 69 73 20 73 65 74 0a 2a 2a 20 74  Blob is set.** t
37aa0 6f 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f 69 6e  o be a null poin
37ab0 74 65 72 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ter..** This fun
37ac0 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 5b  ction sets the [
37ad0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
37ae0 69 6f 6e 5d 20 65 72 72 6f 72 20 63 6f 64 65 20  ion] error code 
37af0 61 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a 20 61  and message.** a
37b00 63 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b 73  ccessible via [s
37b10 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29  qlite3_errcode()
37b20 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65  ] and [sqlite3_e
37b30 72 72 6d 73 67 28 29 5d 20 61 6e 64 20 72 65 6c  rrmsg()] and rel
37b40 61 74 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ated.** function
37b50 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68  s.  Note that th
37b60 65 20 2a 70 70 42 6c 6f 62 20 76 61 72 69 61 62  e *ppBlob variab
37b70 6c 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 69  le is always ini
37b80 74 69 61 6c 69 7a 65 64 20 69 6e 20 61 0a 2a 2a  tialized in a.**
37b90 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65 73 20   way that makes 
37ba0 69 74 20 73 61 66 65 20 74 6f 20 69 6e 76 6f 6b  it safe to invok
37bb0 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
37bc0 63 6c 6f 73 65 28 29 5d 20 6f 6e 20 2a 70 70 42  close()] on *ppB
37bd0 6c 6f 62 0a 2a 2a 20 72 65 67 61 72 64 6c 65 73  lob.** regardles
37be0 73 20 6f 66 20 74 68 65 20 73 75 63 63 65 73 73  s of the success
37bf0 20 6f 72 20 66 61 69 6c 75 72 65 20 6f 66 20 74   or failure of t
37c00 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a  his routine..**.
37c10 2a 2a 20 49 66 20 74 68 65 20 72 6f 77 20 74 68  ** If the row th
37c20 61 74 20 61 20 42 4c 4f 42 20 68 61 6e 64 6c 65  at a BLOB handle
37c30 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 6d 6f   points to is mo
37c40 64 69 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20  dified by an.** 
37c50 5b 55 50 44 41 54 45 5d 2c 20 5b 44 45 4c 45 54  [UPDATE], [DELET
37c60 45 5d 2c 20 6f 72 20 62 79 20 5b 4f 4e 20 43 4f  E], or by [ON CO
37c70 4e 46 4c 49 43 54 5d 20 73 69 64 65 2d 65 66 66  NFLICT] side-eff
37c80 65 63 74 73 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ects.** then the
37c90 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 69 73 20   BLOB handle is 
37ca0 6d 61 72 6b 65 64 20 61 73 20 22 65 78 70 69 72  marked as "expir
37cb0 65 64 22 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  ed"..** This is 
37cc0 74 72 75 65 20 69 66 20 61 6e 79 20 63 6f 6c 75  true if any colu
37cd0 6d 6e 20 6f 66 20 74 68 65 20 72 6f 77 20 69 73  mn of the row is
37ce0 20 63 68 61 6e 67 65 64 2c 20 65 76 65 6e 20 61   changed, even a
37cf0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 74 68 65 72   column.** other
37d00 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 74 68   than the one th
37d10 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20 69 73  e BLOB handle is
37d20 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a 20 43 61 6c   open on..** Cal
37d30 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62  ls to [sqlite3_b
37d40 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61 6e 64 20  lob_read()] and 
37d50 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72  [sqlite3_blob_wr
37d60 69 74 65 28 29 5d 20 66 6f 72 0a 2a 2a 20 61 20  ite()] for.** a 
37d70 65 78 70 69 72 65 64 20 42 4c 4f 42 20 68 61 6e  expired BLOB han
37d80 64 6c 65 20 66 61 69 6c 20 77 69 74 68 20 61 6e  dle fail with an
37d90 20 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20   return code of 
37da0 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a  [SQLITE_ABORT]..
37db0 2a 2a 20 43 68 61 6e 67 65 73 20 77 72 69 74 74  ** Changes writt
37dc0 65 6e 20 69 6e 74 6f 20 61 20 42 4c 4f 42 20 70  en into a BLOB p
37dd0 72 69 6f 72 20 74 6f 20 74 68 65 20 42 4c 4f 42  rior to the BLOB
37de0 20 65 78 70 69 72 69 6e 67 20 61 72 65 20 6e 6f   expiring are no
37df0 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 62 79  t.** rollback by
37e00 20 74 68 65 20 65 78 70 69 72 61 74 69 6f 6e 20   the expiration 
37e10 6f 66 20 74 68 65 20 42 4c 4f 42 2e 20 20 53 75  of the BLOB.  Su
37e20 63 68 20 63 68 61 6e 67 65 73 20 77 69 6c 6c 20  ch changes will 
37e30 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 63 6f  eventually.** co
37e40 6d 6d 69 74 20 69 66 20 74 68 65 20 74 72 61 6e  mmit if the tran
37e50 73 61 63 74 69 6f 6e 20 63 6f 6e 74 69 6e 75 65  saction continue
37e60 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e  s to completion.
37e70 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 5b  .**.** Use the [
37e80 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
37e90 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  es()] interface 
37ea0 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
37eb0 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74 68 65 20   size of.** the 
37ec0 6f 70 65 6e 65 64 20 62 6c 6f 62 2e 20 20 54 68  opened blob.  Th
37ed0 65 20 73 69 7a 65 20 6f 66 20 61 20 62 6c 6f 62  e size of a blob
37ee0 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61 6e   may not be chan
37ef0 67 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 69  ged by this.** i
37f00 6e 74 65 72 66 61 63 65 2e 20 20 55 73 65 20 74  nterface.  Use t
37f10 68 65 20 5b 55 50 44 41 54 45 5d 20 53 51 4c 20  he [UPDATE] SQL 
37f20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63 68 61 6e 67  command to chang
37f30 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 0a  e the size of a.
37f40 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 54  ** blob..**.** T
37f50 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  he [sqlite3_bind
37f60 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 61 6e 64  _zeroblob()] and
37f70 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
37f80 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d 20 69 6e 74  _zeroblob()] int
37f90 65 72 66 61 63 65 73 0a 2a 2a 20 61 6e 64 20 74  erfaces.** and t
37fa0 68 65 20 62 75 69 6c 74 2d 69 6e 20 5b 7a 65 72  he built-in [zer
37fb0 6f 62 6c 6f 62 5d 20 53 51 4c 20 66 75 6e 63 74  oblob] SQL funct
37fc0 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65 64 2c  ion can be used,
37fd0 20 69 66 20 64 65 73 69 72 65 64 2c 0a 2a 2a 20   if desired,.** 
37fe0 74 6f 20 63 72 65 61 74 65 20 61 6e 20 65 6d 70  to create an emp
37ff0 74 79 2c 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20  ty, zero-filled 
38000 62 6c 6f 62 20 69 6e 20 77 68 69 63 68 20 74 6f  blob in which to
38010 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 75   read or write u
38020 73 69 6e 67 0a 2a 2a 20 74 68 69 73 20 69 6e 74  sing.** this int
38030 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f  erface..**.** To
38040 20 61 76 6f 69 64 20 61 20 72 65 73 6f 75 72 63   avoid a resourc
38050 65 20 6c 65 61 6b 2c 20 65 76 65 72 79 20 6f 70  e leak, every op
38060 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  en [BLOB handle]
38070 20 73 68 6f 75 6c 64 20 65 76 65 6e 74 75 61 6c   should eventual
38080 6c 79 0a 2a 2a 20 62 65 20 72 65 6c 65 61 73 65  ly.** be release
38090 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b  d by a call to [
380a0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
380b0 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  se()]..**.** Req
380c0 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
380d0 31 37 38 31 33 5d 20 5b 48 31 37 38 31 34 5d 20  17813] [H17814] 
380e0 5b 48 31 37 38 31 36 5d 20 5b 48 31 37 38 31 39  [H17816] [H17819
380f0 5d 20 5b 48 31 37 38 32 31 5d 20 5b 48 31 37 38  ] [H17821] [H178
38100 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  24].*/.SQLITE_AP
38110 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
38120 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74  ob_open(.  sqlit
38130 65 33 2a 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  e3*,.  const cha
38140 72 20 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73 74 20  r *zDb,.  const 
38150 63 68 61 72 20 2a 7a 54 61 62 6c 65 2c 0a 20 20  char *zTable,.  
38160 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
38170 75 6d 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69  umn,.  sqlite3_i
38180 6e 74 36 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74  nt64 iRow,.  int
38190 20 66 6c 61 67 73 2c 0a 20 20 73 71 6c 69 74 65   flags,.  sqlite
381a0 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a  3_blob **ppBlob.
381b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
381c0 45 46 3a 20 43 6c 6f 73 65 20 41 20 42 4c 4f 42  EF: Close A BLOB
381d0 20 48 61 6e 64 6c 65 20 7b 48 31 37 38 33 30 7d   Handle {H17830}
381e0 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20   <S30230>.**.** 
381f0 43 6c 6f 73 65 73 20 61 6e 20 6f 70 65 6e 20 5b  Closes an open [
38200 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 2e 0a 2a 2a  BLOB handle]..**
38210 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 61 20 42 4c  .** Closing a BL
38220 4f 42 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74  OB shall cause t
38230 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
38240 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74  action to commit
38250 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72 65  .** if there are
38260 20 6e 6f 20 6f 74 68 65 72 20 42 4c 4f 42 73 2c   no other BLOBs,
38270 20 6e 6f 20 70 65 6e 64 69 6e 67 20 70 72 65 70   no pending prep
38280 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2c  ared statements,
38290 20 61 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74 61   and the.** data
382a0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
382b0 69 73 20 69 6e 20 5b 61 75 74 6f 63 6f 6d 6d 69  is in [autocommi
382c0 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 66 20 61  t mode]..** If a
382d0 6e 79 20 77 72 69 74 65 73 20 77 65 72 65 20 6d  ny writes were m
382e0 61 64 65 20 74 6f 20 74 68 65 20 42 4c 4f 42 2c  ade to the BLOB,
382f0 20 74 68 65 79 20 6d 69 67 68 74 20 62 65 20 68   they might be h
38300 65 6c 64 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20  eld in cache.** 
38310 75 6e 74 69 6c 20 74 68 65 20 63 6c 6f 73 65 20  until the close 
38320 6f 70 65 72 61 74 69 6f 6e 20 69 66 20 74 68 65  operation if the
38330 79 20 77 69 6c 6c 20 66 69 74 2e 0a 2a 2a 0a 2a  y will fit..**.*
38340 2a 20 43 6c 6f 73 69 6e 67 20 74 68 65 20 42 4c  * Closing the BL
38350 4f 42 20 6f 66 74 65 6e 20 66 6f 72 63 65 73 20  OB often forces 
38360 74 68 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20 6f  the changes.** o
38370 75 74 20 74 6f 20 64 69 73 6b 20 61 6e 64 20 73  ut to disk and s
38380 6f 20 69 66 20 61 6e 79 20 49 2f 4f 20 65 72 72  o if any I/O err
38390 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 65 79 20  ors occur, they 
383a0 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 6f 63 63 75  will likely occu
383b0 72 0a 2a 2a 20 61 74 20 74 68 65 20 74 69 6d 65  r.** at the time
383c0 20 77 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69   when the BLOB i
383d0 73 20 63 6c 6f 73 65 64 2e 20 20 41 6e 79 20 65  s closed.  Any e
383e0 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72  rrors that occur
383f0 20 64 75 72 69 6e 67 0a 2a 2a 20 63 6c 6f 73 69   during.** closi
38400 6e 67 20 61 72 65 20 72 65 70 6f 72 74 65 64 20  ng are reported 
38410 61 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65  as a non-zero re
38420 74 75 72 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  turn value..**.*
38430 2a 20 54 68 65 20 42 4c 4f 42 20 69 73 20 63 6c  * The BLOB is cl
38440 6f 73 65 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e  osed uncondition
38450 61 6c 6c 79 2e 20 20 45 76 65 6e 20 69 66 20 74  ally.  Even if t
38460 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
38470 72 6e 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20  rns.** an error 
38480 63 6f 64 65 2c 20 74 68 65 20 42 4c 4f 42 20 69  code, the BLOB i
38490 73 20 73 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 0a  s still closed..
384a0 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74 68  **.** Calling th
384b0 69 73 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20  is routine with 
384c0 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 20 28  a null pointer (
384d0 77 68 69 63 68 20 61 73 20 77 6f 75 6c 64 20 62  which as would b
384e0 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79  e returned.** by
384f0 20 66 61 69 6c 65 64 20 63 61 6c 6c 20 74 6f 20   failed call to 
38500 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70  [sqlite3_blob_op
38510 65 6e 28 29 5d 29 20 69 73 20 61 20 68 61 72 6d  en()]) is a harm
38520 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  less no-op..**.*
38530 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
38540 2a 2a 20 5b 48 31 37 38 33 33 5d 20 5b 48 31 37  ** [H17833] [H17
38550 38 33 36 5d 20 5b 48 31 37 38 33 39 5d 0a 2a 2f  836] [H17839].*/
38560 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
38570 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
38580 73 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  se(sqlite3_blob 
38590 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
385a0 52 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65 20  REF: Return The 
385b0 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70 65 6e 20  Size Of An Open 
385c0 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d 20 3c 53  BLOB {H17840} <S
385d0 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74  30230>.**.** Ret
385e0 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 69 6e  urns the size in
385f0 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42 4c   bytes of the BL
38600 4f 42 20 61 63 63 65 73 73 69 62 6c 65 20 76 69  OB accessible vi
38610 61 20 74 68 65 20 0a 2a 2a 20 73 75 63 63 65 73  a the .** succes
38620 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20 5b 42  sfully opened [B
38630 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 20 69  LOB handle] in i
38640 74 73 20 6f 6e 6c 79 20 61 72 67 75 6d 65 6e 74  ts only argument
38650 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 63 72 65 6d  .  The.** increm
38660 65 6e 74 61 6c 20 62 6c 6f 62 20 49 2f 4f 20 72  ental blob I/O r
38670 6f 75 74 69 6e 65 73 20 63 61 6e 20 6f 6e 6c 79  outines can only
38680 20 72 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69   read or overwri
38690 74 69 6e 67 20 65 78 69 73 74 69 6e 67 0a 2a 2a  ting existing.**
386a0 20 62 6c 6f 62 20 63 6f 6e 74 65 6e 74 3b 20 74   blob content; t
386b0 68 65 79 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67  hey cannot chang
386c0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20  e the size of a 
386d0 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  blob..**.** This
386e0 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f   routine only wo
386f0 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68  rks on a [BLOB h
38700 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73  andle] which has
38710 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a 2a 2a   been created.**
38720 20 62 79 20 61 20 70 72 69 6f 72 20 73 75 63 63   by a prior succ
38730 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b  essful call to [
38740 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
38750 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68 20 68  n()] and which h
38760 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 63  as not.** been c
38770 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65  losed by [sqlite
38780 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e  3_blob_close()].
38790 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20 6f 74    Passing any ot
387a0 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a 2a  her pointer in.*
387b0 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  * to this routin
387c0 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  e results in und
387d0 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61  efined and proba
387e0 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c 65 20  bly undesirable 
387f0 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
38800 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
38810 20 5b 48 31 37 38 34 33 5d 0a 2a 2f 0a 53 51 4c   [H17843].*/.SQL
38820 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
38830 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 73  te3_blob_bytes(s
38840 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 3b 0a  qlite3_blob *);.
38850 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
38860 20 52 65 61 64 20 44 61 74 61 20 46 72 6f 6d 20   Read Data From 
38870 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74  A BLOB Increment
38880 61 6c 6c 79 20 7b 48 31 37 38 35 30 7d 20 3c 53  ally {H17850} <S
38890 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  30230>.**.** Thi
388a0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
388b0 65 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20  ed to read data 
388c0 66 72 6f 6d 20 61 6e 20 6f 70 65 6e 20 5b 42 4c  from an open [BL
388d0 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e 74 6f 20  OB handle] into 
388e0 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70  a.** caller-supp
388f0 6c 69 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62  lied buffer. N b
38900 79 74 65 73 20 6f 66 20 64 61 74 61 20 61 72 65  ytes of data are
38910 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 75 66   copied into buf
38920 66 65 72 20 5a 0a 2a 2a 20 66 72 6f 6d 20 74 68  fer Z.** from th
38930 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61  e open BLOB, sta
38940 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20  rting at offset 
38950 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49  iOffset..**.** I
38960 66 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74  f offset iOffset
38970 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 20   is less than N 
38980 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 65  bytes from the e
38990 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a  nd of the BLOB,.
389a0 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52  ** [SQLITE_ERROR
389b0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  ] is returned an
389c0 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72 65 61  d no data is rea
389d0 64 2e 20 20 49 66 20 4e 20 6f 72 20 69 4f 66 66  d.  If N or iOff
389e0 73 65 74 20 69 73 0a 2a 2a 20 6c 65 73 73 20 74  set is.** less t
389f0 68 61 6e 20 7a 65 72 6f 2c 20 5b 53 51 4c 49 54  han zero, [SQLIT
38a00 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75  E_ERROR] is retu
38a10 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61  rned and no data
38a20 20 69 73 20 72 65 61 64 2e 0a 2a 2a 20 54 68 65   is read..** The
38a30 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f   size of the blo
38a40 62 20 28 61 6e 64 20 68 65 6e 63 65 20 74 68 65  b (and hence the
38a50 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f   maximum value o
38a60 66 20 4e 2b 69 4f 66 66 73 65 74 29 0a 2a 2a 20  f N+iOffset).** 
38a70 63 61 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65  can be determine
38a80 64 20 75 73 69 6e 67 20 74 68 65 20 5b 73 71 6c  d using the [sql
38a90 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28  ite3_blob_bytes(
38aa0 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  )] interface..**
38ab0 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 74  .** An attempt t
38ac0 6f 20 72 65 61 64 20 66 72 6f 6d 20 61 6e 20 65  o read from an e
38ad0 78 70 69 72 65 64 20 5b 42 4c 4f 42 20 68 61 6e  xpired [BLOB han
38ae0 64 6c 65 5d 20 66 61 69 6c 73 20 77 69 74 68 20  dle] fails with 
38af0 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65  an.** error code
38b00 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52   of [SQLITE_ABOR
38b10 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63  T]..**.** On suc
38b20 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  cess, SQLITE_OK 
38b30 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
38b40 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 5b 65  Otherwise, an [e
38b50 72 72 6f 72 20 63 6f 64 65 5d 20 6f 72 20 61 6e  rror code] or an
38b60 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72   [extended error
38b70 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
38b80 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ed..**.** This r
38b90 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b  outine only work
38ba0 73 20 6f 6e 20 61 20 5b 42 4c 4f 42 20 68 61 6e  s on a [BLOB han
38bb0 64 6c 65 5d 20 77 68 69 63 68 20 68 61 73 20 62  dle] which has b
38bc0 65 65 6e 20 63 72 65 61 74 65 64 0a 2a 2a 20 62  een created.** b
38bd0 79 20 61 20 70 72 69 6f 72 20 73 75 63 63 65 73  y a prior succes
38be0 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71  sful call to [sq
38bf0 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28  lite3_blob_open(
38c00 29 5d 20 61 6e 64 20 77 68 69 63 68 20 68 61 73  )] and which has
38c10 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e 20 63 6c 6f   not.** been clo
38c20 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f  sed by [sqlite3_
38c30 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 2e 20 20  blob_close()].  
38c40 50 61 73 73 69 6e 67 20 61 6e 79 20 6f 74 68 65  Passing any othe
38c50 72 20 70 6f 69 6e 74 65 72 20 69 6e 0a 2a 2a 20  r pointer in.** 
38c60 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
38c70 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66  results in undef
38c80 69 6e 65 64 20 61 6e 64 20 70 72 6f 62 61 62 6c  ined and probabl
38c90 79 20 75 6e 64 65 73 69 72 61 62 6c 65 20 62 65  y undesirable be
38ca0 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 53 65  havior..**.** Se
38cb0 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
38cc0 5f 62 6c 6f 62 5f 77 72 69 74 65 28 29 5d 2e 0a  _blob_write()]..
38cd0 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
38ce0 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 35 33 5d 20  ts:.** [H17853] 
38cf0 5b 48 31 37 38 35 36 5d 20 5b 48 31 37 38 35 39  [H17856] [H17859
38d00 5d 20 5b 48 31 37 38 36 32 5d 20 5b 48 31 37 38  ] [H17862] [H178
38d10 36 33 5d 20 5b 48 31 37 38 36 35 5d 20 5b 48 31  63] [H17865] [H1
38d20 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  7868].*/.SQLITE_
38d30 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
38d40 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74 65  blob_read(sqlite
38d50 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f 69 64 20 2a  3_blob *, void *
38d60 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e 74 20 69 4f  Z, int N, int iO
38d70 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ffset);../*.** C
38d80 41 50 49 33 52 45 46 3a 20 57 72 69 74 65 20 44  API3REF: Write D
38d90 61 74 61 20 49 6e 74 6f 20 41 20 42 4c 4f 42 20  ata Into A BLOB 
38da0 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 7b 48  Incrementally {H
38db0 31 37 38 37 30 7d 20 3c 53 33 30 32 33 30 3e 0a  17870} <S30230>.
38dc0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
38dd0 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 77  ion is used to w
38de0 72 69 74 65 20 64 61 74 61 20 69 6e 74 6f 20 61  rite data into a
38df0 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e  n open [BLOB han
38e00 64 6c 65 5d 20 66 72 6f 6d 20 61 0a 2a 2a 20 63  dle] from a.** c
38e10 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20 62  aller-supplied b
38e20 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20 6f  uffer. N bytes o
38e30 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69 65  f data are copie
38e40 64 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65  d from the buffe
38e50 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20  r Z.** into the 
38e60 6f 70 65 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74  open BLOB, start
38e70 69 6e 67 20 61 74 20 6f 66 66 73 65 74 20 69 4f  ing at offset iO
38e80 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ffset..**.** If 
38e90 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  the [BLOB handle
38ea0 5d 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  ] passed as the 
38eb0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 77  first argument w
38ec0 61 73 20 6e 6f 74 20 6f 70 65 6e 65 64 20 66 6f  as not opened fo
38ed0 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 28 74 68  r.** writing (th
38ee0 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  e flags paramete
38ef0 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c  r to [sqlite3_bl
38f00 6f 62 5f 6f 70 65 6e 28 29 5d 20 77 61 73 20 7a  ob_open()] was z
38f10 65 72 6f 29 2c 0a 2a 2a 20 74 68 69 73 20 66 75  ero),.** this fu
38f20 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b  nction returns [
38f30 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d  SQLITE_READONLY]
38f40 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
38f50 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 6d  ction may only m
38f60 6f 64 69 66 79 20 74 68 65 20 63 6f 6e 74 65 6e  odify the conten
38f70 74 73 20 6f 66 20 74 68 65 20 42 4c 4f 42 3b 20  ts of the BLOB; 
38f80 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 70 6f 73  it is.** not pos
38f90 73 69 62 6c 65 20 74 6f 20 69 6e 63 72 65 61 73  sible to increas
38fa0 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20  e the size of a 
38fb0 42 4c 4f 42 20 75 73 69 6e 67 20 74 68 69 73 20  BLOB using this 
38fc0 41 50 49 2e 0a 2a 2a 20 49 66 20 6f 66 66 73 65  API..** If offse
38fd0 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c 65 73  t iOffset is les
38fe0 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73 20 66  s than N bytes f
38ff0 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74  rom the end of t
39000 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53 51 4c  he BLOB,.** [SQL
39010 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65  ITE_ERROR] is re
39020 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61  turned and no da
39030 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e 20 20  ta is written.  
39040 49 66 20 4e 20 69 73 0a 2a 2a 20 6c 65 73 73 20  If N is.** less 
39050 74 68 61 6e 20 7a 65 72 6f 20 5b 53 51 4c 49 54  than zero [SQLIT
39060 45 5f 45 52 52 4f 52 5d 20 69 73 20 72 65 74 75  E_ERROR] is retu
39070 72 6e 65 64 20 61 6e 64 20 6e 6f 20 64 61 74 61  rned and no data
39080 20 69 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 20   is written..** 
39090 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  The size of the 
390a0 42 4c 4f 42 20 28 61 6e 64 20 68 65 6e 63 65 20  BLOB (and hence 
390b0 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75  the maximum valu
390c0 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29 0a  e of N+iOffset).
390d0 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72 6d  ** can be determ
390e0 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b  ined using the [
390f0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
39100 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  es()] interface.
39110 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d 70  .**.** An attemp
39120 74 20 74 6f 20 77 72 69 74 65 20 74 6f 20 61 6e  t to write to an
39130 20 65 78 70 69 72 65 64 20 5b 42 4c 4f 42 20 68   expired [BLOB h
39140 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 77 69 74  andle] fails wit
39150 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f  h an.** error co
39160 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42  de of [SQLITE_AB
39170 4f 52 54 5d 2e 20 20 57 72 69 74 65 73 20 74 6f  ORT].  Writes to
39180 20 74 68 65 20 42 4c 4f 42 20 74 68 61 74 20 6f   the BLOB that o
39190 63 63 75 72 72 65 64 0a 2a 2a 20 62 65 66 6f 72  ccurred.** befor
391a0 65 20 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64  e the [BLOB hand
391b0 6c 65 5d 20 65 78 70 69 72 65 64 20 61 72 65 20  le] expired are 
391c0 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  not rolled back 
391d0 62 79 20 74 68 65 0a 2a 2a 20 65 78 70 69 72 61  by the.** expira
391e0 74 69 6f 6e 20 6f 66 20 74 68 65 20 68 61 6e 64  tion of the hand
391f0 6c 65 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f  le, though of co
39200 75 72 73 65 20 74 68 6f 73 65 20 63 68 61 6e 67  urse those chang
39210 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61 76 65  es might.** have
39220 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65   been overwritte
39230 6e 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65  n by the stateme
39240 6e 74 20 74 68 61 74 20 65 78 70 69 72 65 64 20  nt that expired 
39250 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 0a  the BLOB handle.
39260 2a 2a 20 6f 72 20 62 79 20 6f 74 68 65 72 20 69  ** or by other i
39270 6e 64 65 70 65 6e 64 65 6e 74 20 73 74 61 74 65  ndependent state
39280 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20  ments..**.** On 
39290 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f  success, SQLITE_
392a0 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  OK is returned..
392b0 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e  ** Otherwise, an
392c0 20 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f    [error code] o
392d0 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65  r an [extended e
392e0 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
392f0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  turned..**.** Th
39300 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  is routine only 
39310 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42  works on a [BLOB
39320 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68   handle] which h
39330 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a  as been created.
39340 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75  ** by a prior su
39350 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
39360 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
39370 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68  pen()] and which
39380 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e   has not.** been
39390 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69   closed by [sqli
393a0 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29  te3_blob_close()
393b0 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20  ].  Passing any 
393c0 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e  other pointer in
393d0 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74  .** to this rout
393e0 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75  ine results in u
393f0 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f  ndefined and pro
39400 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c  bably undesirabl
39410 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a  e behavior..**.*
39420 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
39430 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29  ite3_blob_read()
39440 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
39450 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 37  ments:.** [H1787
39460 33 5d 20 5b 48 31 37 38 37 34 5d 20 5b 48 31 37  3] [H17874] [H17
39470 38 37 35 5d 20 5b 48 31 37 38 37 36 5d 20 5b 48  875] [H17876] [H
39480 31 37 38 37 37 5d 20 5b 48 31 37 38 37 39 5d 20  17877] [H17879] 
39490 5b 48 31 37 38 38 32 5d 20 5b 48 31 37 38 38 35  [H17882] [H17885
394a0 5d 0a 2a 2a 20 5b 48 31 37 38 38 38 5d 0a 2a 2f  ].** [H17888].*/
394b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
394c0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69  sqlite3_blob_wri
394d0 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  te(sqlite3_blob 
394e0 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  *, const void *z
394f0 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66  , int n, int iOf
39500 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  fset);../*.** CA
39510 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20  PI3REF: Virtual 
39520 46 69 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65  File System Obje
39530 63 74 73 20 7b 48 31 31 32 30 30 7d 20 3c 53 32  cts {H11200} <S2
39540 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69  0100>.**.** A vi
39550 72 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65 6d  rtual filesystem
39560 20 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73 71   (VFS) is an [sq
39570 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63  lite3_vfs] objec
39580 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65  t.** that SQLite
39590 20 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 63   uses to interac
395a0 74 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 6e  t.** with the un
395b0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
395c0 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74  ng system.  Most
395d0 20 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 63   SQLite builds c
395e0 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73 69  ome with a.** si
395f0 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46 53  ngle default VFS
39600 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72   that is appropr
39610 69 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f 73  iate for the hos
39620 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e  t computer..** N
39630 65 77 20 56 46 53 65 73 20 63 61 6e 20 62 65 20  ew VFSes can be 
39640 72 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 65  registered and e
39650 78 69 73 74 69 6e 67 20 56 46 53 65 73 20 63 61  xisting VFSes ca
39660 6e 20 62 65 20 75 6e 72 65 67 69 73 74 65 72 65  n be unregistere
39670 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  d..** The follow
39680 69 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 61  ing interfaces a
39690 72 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a  re provided..**.
396a0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76  ** The sqlite3_v
396b0 66 73 5f 66 69 6e 64 28 29 20 69 6e 74 65 72 66  fs_find() interf
396c0 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ace returns a po
396d0 69 6e 74 65 72 20 74 6f 20 61 20 56 46 53 20 67  inter to a VFS g
396e0 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a  iven its name..*
396f0 2a 20 4e 61 6d 65 73 20 61 72 65 20 63 61 73 65  * Names are case
39700 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e   sensitive..** N
39710 61 6d 65 73 20 61 72 65 20 7a 65 72 6f 2d 74 65  ames are zero-te
39720 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73  rminated UTF-8 s
39730 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68  trings..** If th
39740 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c  ere is no match,
39750 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
39760 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
39770 49 66 20 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e  If zVfsName is N
39780 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 65 66  ULL then the def
39790 61 75 6c 74 20 56 46 53 20 69 73 20 72 65 74 75  ault VFS is retu
397a0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20  rned..**.** New 
397b0 56 46 53 65 73 20 61 72 65 20 72 65 67 69 73 74  VFSes are regist
397c0 65 72 65 64 20 77 69 74 68 20 73 71 6c 69 74 65  ered with sqlite
397d0 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29  3_vfs_register()
397e0 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 56 46  ..** Each new VF
397f0 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65  S becomes the de
39800 66 61 75 6c 74 20 56 46 53 20 69 66 20 74 68 65  fault VFS if the
39810 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 69   makeDflt flag i
39820 73 20 73 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61  s set..** The sa
39830 6d 65 20 56 46 53 20 63 61 6e 20 62 65 20 72 65  me VFS can be re
39840 67 69 73 74 65 72 65 64 20 6d 75 6c 74 69 70 6c  gistered multipl
39850 65 20 74 69 6d 65 73 20 77 69 74 68 6f 75 74 20  e times without 
39860 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61  injury..** To ma
39870 6b 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 56  ke an existing V
39880 46 53 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61  FS into the defa
39890 75 6c 74 20 56 46 53 2c 20 72 65 67 69 73 74 65  ult VFS, registe
398a0 72 20 69 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69  r it again.** wi
398b0 74 68 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20  th the makeDflt 
398c0 66 6c 61 67 20 73 65 74 2e 20 20 49 66 20 74 77  flag set.  If tw
398d0 6f 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 65  o different VFSe
398e0 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 61  s with the.** sa
398f0 6d 65 20 6e 61 6d 65 20 61 72 65 20 72 65 67 69  me name are regi
39900 73 74 65 72 65 64 2c 20 74 68 65 20 62 65 68 61  stered, the beha
39910 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
39920 64 2e 20 20 49 66 20 61 0a 2a 2a 20 56 46 53 20  d.  If a.** VFS 
39930 69 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69  is registered wi
39940 74 68 20 61 20 6e 61 6d 65 20 74 68 61 74 20 69  th a name that i
39950 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70  s NULL or an emp
39960 74 79 20 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68  ty string,.** th
39970 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  en the behavior 
39980 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a  is undefined..**
39990 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61  .** Unregister a
399a0 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73 71   VFS with the sq
399b0 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
399c0 73 74 65 72 28 29 20 69 6e 74 65 72 66 61 63 65  ster() interface
399d0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 61  ..** If the defa
399e0 75 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65 67  ult VFS is unreg
399f0 69 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65 72  istered, another
39a00 20 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20 61   VFS is chosen a
39a10 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  s.** the default
39a20 2e 20 20 54 68 65 20 63 68 6f 69 63 65 20 66 6f  .  The choice fo
39a30 72 20 74 68 65 20 6e 65 77 20 56 46 53 20 69 73  r the new VFS is
39a40 20 61 72 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a   arbitrary..**.*
39a50 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
39a60 2a 2a 20 5b 48 31 31 32 30 33 5d 20 5b 48 31 31  ** [H11203] [H11
39a70 32 30 36 5d 20 5b 48 31 31 32 30 39 5d 20 5b 48  206] [H11209] [H
39a80 31 31 32 31 32 5d 20 5b 48 31 31 32 31 35 5d 20  11212] [H11215] 
39a90 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a 53 51 4c 49  [H11218].*/.SQLI
39aa0 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76  TE_API sqlite3_v
39ab0 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f  fs *sqlite3_vfs_
39ac0 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20  find(const char 
39ad0 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53 51 4c 49  *zVfsName);.SQLI
39ae0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
39af0 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28  e3_vfs_register(
39b00 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e  sqlite3_vfs*, in
39b10 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a 53 51 4c  t makeDflt);.SQL
39b20 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
39b30 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74  te3_vfs_unregist
39b40 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 29  er(sqlite3_vfs*)
39b50 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
39b60 46 3a 20 4d 75 74 65 78 65 73 20 7b 48 31 37 30  F: Mutexes {H170
39b70 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a  00} <S20000>.**.
39b80 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  ** The SQLite co
39b90 72 65 20 75 73 65 73 20 74 68 65 73 65 20 72 6f  re uses these ro
39ba0 75 74 69 6e 65 73 20 66 6f 72 20 74 68 72 65 61  utines for threa
39bb0 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e 69 7a 61  d.** synchroniza
39bc0 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20 74 68 65  tion. Though the
39bd0 79 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66  y are intended f
39be0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
39bf0 73 65 20 62 79 20 53 51 4c 69 74 65 2c 20 63 6f  se by SQLite, co
39c00 64 65 20 74 68 61 74 20 6c 69 6e 6b 73 20 61 67  de that links ag
39c10 61 69 6e 73 74 20 53 51 4c 69 74 65 20 69 73 0a  ainst SQLite is.
39c20 2a 2a 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20  ** permitted to 
39c30 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65  use any of these
39c40 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a   routines..**.**
39c50 20 54 68 65 20 53 51 4c 69 74 65 20 73 6f 75 72   The SQLite sour
39c60 63 65 20 63 6f 64 65 20 63 6f 6e 74 61 69 6e 73  ce code contains
39c70 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d   multiple implem
39c80 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20  entations.** of 
39c90 74 68 65 73 65 20 6d 75 74 65 78 20 72 6f 75 74  these mutex rout
39ca0 69 6e 65 73 2e 20 20 41 6e 20 61 70 70 72 6f 70  ines.  An approp
39cb0 72 69 61 74 65 20 69 6d 70 6c 65 6d 65 6e 74 61  riate implementa
39cc0 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65 6c 65 63  tion.** is selec
39cd0 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ted automaticall
39ce0 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  y at compile-tim
39cf0 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  e.  The followin
39d00 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  g.** implementat
39d10 69 6f 6e 73 20 61 72 65 20 61 76 61 69 6c 61 62  ions are availab
39d20 6c 65 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65  le in the SQLite
39d30 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c   core:.**.** <ul
39d40 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49  >.** <li>   SQLI
39d50 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 2a 2a 20  TE_MUTEX_OS2.** 
39d60 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55  <li>   SQLITE_MU
39d70 54 45 58 5f 50 54 48 52 45 41 44 0a 2a 2a 20 3c  TEX_PTHREAD.** <
39d80 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54  li>   SQLITE_MUT
39d90 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69 3e 20 20  EX_W32.** <li>  
39da0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f   SQLITE_MUTEX_NO
39db0 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a  OP.** </ul>.**.*
39dc0 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54  * The SQLITE_MUT
39dd0 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65 6d 65 6e  EX_NOOP implemen
39de0 74 61 74 69 6f 6e 20 69 73 20 61 20 73 65 74 20  tation is a set 
39df0 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 74  of routines.** t
39e00 68 61 74 20 64 6f 65 73 20 6e 6f 20 72 65 61 6c  hat does no real
39e10 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 69 73 20   locking and is 
39e20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
39e30 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73 69 6e 67  use in.** a sing
39e40 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c  le-threaded appl
39e50 69 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 53 51  ication.  The SQ
39e60 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 2c 0a  LITE_MUTEX_OS2,.
39e70 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ** SQLITE_MUTEX_
39e80 50 54 48 52 45 41 44 2c 20 61 6e 64 20 53 51 4c  PTHREAD, and SQL
39e90 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 69 6d  ITE_MUTEX_W32 im
39ea0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a  plementations.**
39eb0 20 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65   are appropriate
39ec0 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f 53 2f 32   for use on OS/2
39ed0 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57 69 6e 64  , Unix, and Wind
39ee0 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51  ows..**.** If SQ
39ef0 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
39f00 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45   with the SQLITE
39f10 5f 4d 55 54 45 58 5f 41 50 50 44 45 46 20 70 72  _MUTEX_APPDEF pr
39f20 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a 20 6d 61  eprocessor.** ma
39f30 63 72 6f 20 64 65 66 69 6e 65 64 20 28 77 69 74  cro defined (wit
39f40 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d 55 54 45  h "-DSQLITE_MUTE
39f50 58 5f 41 50 50 44 45 46 3d 31 22 29 2c 20 74 68  X_APPDEF=1"), th
39f60 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a 2a 20 69  en no mutex.** i
39f70 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73  mplementation is
39f80 20 69 6e 63 6c 75 64 65 64 20 77 69 74 68 20 74   included with t
39f90 68 65 20 6c 69 62 72 61 72 79 2e 20 49 6e 20 74  he library. In t
39fa0 68 69 73 20 63 61 73 65 20 74 68 65 0a 2a 2a 20  his case the.** 
39fb0 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74  application must
39fc0 20 73 75 70 70 6c 79 20 61 20 63 75 73 74 6f 6d   supply a custom
39fd0 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74   mutex implement
39fe0 61 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 0a  ation using the.
39ff0 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ** [SQLITE_CONFI
3a000 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 20  G_MUTEX] option 
3a010 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  of the sqlite3_c
3a020 6f 6e 66 69 67 28 29 20 66 75 6e 63 74 69 6f 6e  onfig() function
3a030 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  .** before calli
3a040 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ng sqlite3_initi
3a050 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e 79 20 6f  alize() or any o
3a060 74 68 65 72 20 70 75 62 6c 69 63 20 73 71 6c 69  ther public sqli
3a070 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  te3_.** function
3a080 20 74 68 61 74 20 63 61 6c 6c 73 20 73 71 6c 69   that calls sqli
3a090 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
3a0a0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 31 7d  ..**.** {H17011}
3a0b0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
3a0c0 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69  ex_alloc() routi
3a0d0 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e  ne allocates a n
3a0e0 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20  ew.** mutex and 
3a0f0 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
3a100 72 20 74 6f 20 69 74 2e 20 7b 48 31 37 30 31 32  r to it. {H17012
3a110 7d 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20  } If it returns 
3a120 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61  NULL.** that mea
3a130 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20  ns that a mutex 
3a140 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c  could not be all
3a150 6f 63 61 74 65 64 2e 20 7b 48 31 37 30 31 33 7d  ocated. {H17013}
3a160 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20   SQLite.** will 
3a170 75 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b  unwind its stack
3a180 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65   and return an e
3a190 72 72 6f 72 2e 20 7b 48 31 37 30 31 34 7d 20 54  rror. {H17014} T
3a1a0 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  he argument.** t
3a1b0 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
3a1c0 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20 6f  alloc() is one o
3a1d0 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72 20  f these integer 
3a1e0 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a  constants:.**.**
3a1f0 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53   <ul>.** <li>  S
3a200 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
3a210 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
3a220 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
3a230 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
3a240 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
3a250 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  STER.** <li>  SQ
3a260 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3a270 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53  C_MEM.** <li>  S
3a280 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
3a290 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20  IC_MEM2.** <li> 
3a2a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
3a2b0 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69  ATIC_PRNG.** <li
3a2c0 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
3a2d0 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c  STATIC_LRU.** <l
3a2e0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
3a2f0 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a 20  _STATIC_LRU2.** 
3a300 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  </ul>.**.** {H17
3a310 30 31 35 7d 20 54 68 65 20 66 69 72 73 74 20 74  015} The first t
3a320 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75  wo constants cau
3a330 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  se sqlite3_mutex
3a340 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61  _alloc() to crea
3a350 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65  te.** a new mute
3a360 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65  x.  The new mute
3a370 78 20 69 73 20 72 65 63 75 72 73 69 76 65 20 77  x is recursive w
3a380 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  hen SQLITE_MUTEX
3a390 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73  _RECURSIVE.** is
3a3a0 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65   used but not ne
3a3b0 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65  cessarily so whe
3a3c0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  n SQLITE_MUTEX_F
3a3d0 41 53 54 20 69 73 20 75 73 65 64 2e 20 7b 45 4e  AST is used. {EN
3a3e0 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20  D}.** The mutex 
3a3f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64  implementation d
3a400 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  oes not need to 
3a410 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63 74 69  make a distincti
3a420 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20 53 51  on.** between SQ
3a430 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
3a440 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54 45 5f  SIVE and SQLITE_
3a450 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20 69 74  MUTEX_FAST if it
3a460 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e   does.** not wan
3a470 74 20 74 6f 2e 20 20 7b 48 31 37 30 31 36 7d 20  t to.  {H17016} 
3a480 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  But SQLite will 
3a490 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72  only request a r
3a4a0 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69  ecursive mutex i
3a4b0 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65  n.** cases where
3a4c0 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73   it really needs
3a4d0 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20 49 66 20   one.  {END} If 
3a4e0 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63  a faster non-rec
3a4f0 75 72 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20  ursive mutex.** 
3a500 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
3a510 73 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74  s available on t
3a520 68 65 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d  he host platform
3a530 2c 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73  , the mutex subs
3a540 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72  ystem.** might r
3a550 65 74 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74  eturn such a mut
3a560 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74  ex in response t
3a570 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  o SQLITE_MUTEX_F
3a580 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  AST..**.** {H170
3a590 31 37 7d 20 54 68 65 20 6f 74 68 65 72 20 61 6c  17} The other al
3a5a0 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73  lowed parameters
3a5b0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
3a5c0 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72  x_alloc() each r
3a5d0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
3a5e0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70  er to a static p
3a5f0 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78  reexisting mutex
3a600 2e 20 7b 45 4e 44 7d 20 20 53 69 78 20 73 74 61  . {END}  Six sta
3a610 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a  tic mutexes are.
3a620 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63  ** used by the c
3a630 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f  urrent version o
3a640 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72  f SQLite.  Futur
3a650 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
3a660 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20  Lite.** may add 
3a670 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69  additional stati
3a680 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74  c mutexes.  Stat
3a690 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66  ic mutexes are f
3a6a0 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
3a6b0 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c  se by SQLite onl
3a6c0 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  y.  Applications
3a6d0 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65   that use SQLite
3a6e0 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a   mutexes should.
3a6f0 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20  ** use only the 
3a700 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20  dynamic mutexes 
3a710 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49  returned by SQLI
3a720 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72  TE_MUTEX_FAST or
3a730 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  .** SQLITE_MUTEX
3a740 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a  _RECURSIVE..**.*
3a750 2a 20 7b 48 31 37 30 31 38 7d 20 4e 6f 74 65 20  * {H17018} Note 
3a760 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74  that if one of t
3a770 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78  he dynamic mutex
3a780 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c   parameters (SQL
3a790 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a  ITE_MUTEX_FAST.*
3a7a0 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45  * or SQLITE_MUTE
3a7b0 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20  X_RECURSIVE) is 
3a7c0 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65  used then sqlite
3a7d0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a  3_mutex_alloc().
3a7e0 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66  ** returns a dif
3a7f0 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20  ferent mutex on 
3a800 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 7b 48 31  every call.  {H1
3a810 37 30 33 34 7d 20 42 75 74 20 66 6f 72 20 74 68  7034} But for th
3a820 65 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65  e static.** mute
3a830 78 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d  x types, the sam
3a840 65 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72  e mutex is retur
3a850 6e 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  ned on every cal
3a860 6c 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68  l that has.** th
3a870 65 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62  e same type numb
3a880 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31  er..**.** {H1701
3a890 39 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  9} The sqlite3_m
3a8a0 75 74 65 78 5f 66 72 65 65 28 29 20 72 6f 75 74  utex_free() rout
3a8b0 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20  ine deallocates 
3a8c0 61 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20  a previously.** 
3a8d0 61 6c 6c 6f 63 61 74 65 64 20 64 79 6e 61 6d 69  allocated dynami
3a8e0 63 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32 30  c mutex. {H17020
3a8f0 7d 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65  } SQLite is care
3a900 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74  ful to deallocat
3a910 65 20 65 76 65 72 79 0a 2a 2a 20 64 79 6e 61 6d  e every.** dynam
3a920 69 63 20 6d 75 74 65 78 20 74 68 61 74 20 69 74  ic mutex that it
3a930 20 61 6c 6c 6f 63 61 74 65 73 2e 20 7b 41 31 37   allocates. {A17
3a940 30 32 31 7d 20 54 68 65 20 64 79 6e 61 6d 69 63  021} The dynamic
3a950 20 6d 75 74 65 78 65 73 20 6d 75 73 74 20 6e 6f   mutexes must no
3a960 74 20 62 65 20 69 6e 0a 2a 2a 20 75 73 65 20 77  t be in.** use w
3a970 68 65 6e 20 74 68 65 79 20 61 72 65 20 64 65 61  hen they are dea
3a980 6c 6c 6f 63 61 74 65 64 2e 20 7b 41 31 37 30 32  llocated. {A1702
3a990 32 7d 20 41 74 74 65 6d 70 74 69 6e 67 20 74 6f  2} Attempting to
3a9a0 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 20 73 74   deallocate a st
3a9b0 61 74 69 63 0a 2a 2a 20 6d 75 74 65 78 20 72 65  atic.** mutex re
3a9c0 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66 69 6e  sults in undefin
3a9d0 65 64 20 62 65 68 61 76 69 6f 72 2e 20 7b 48 31  ed behavior. {H1
3a9e0 37 30 32 33 7d 20 53 51 4c 69 74 65 20 6e 65 76  7023} SQLite nev
3a9f0 65 72 20 64 65 61 6c 6c 6f 63 61 74 65 73 0a 2a  er deallocates.*
3aa00 2a 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78  * a static mutex
3aa10 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68  . {END}.**.** Th
3aa20 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
3aa30 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69  enter() and sqli
3aa40 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
3aa50 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74  routines attempt
3aa60 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d  .** to enter a m
3aa70 75 74 65 78 2e 20 7b 48 31 37 30 32 34 7d 20 49  utex. {H17024} I
3aa80 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  f another thread
3aa90 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68   is already with
3aaa0 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a  in the mutex,.**
3aab0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
3aac0 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63  nter() will bloc
3aad0 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  k and sqlite3_mu
3aae0 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72  tex_try() will r
3aaf0 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f  eturn.** SQLITE_
3ab00 42 55 53 59 2e 20 7b 48 31 37 30 32 35 7d 20 20  BUSY. {H17025}  
3ab10 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
3ab20 78 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63  x_try() interfac
3ab30 65 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  e returns [SQLIT
3ab40 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f 6e 20 73 75  E_OK].** upon su
3ab50 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e 20  ccessful entry. 
3ab60 20 7b 48 31 37 30 32 36 7d 20 4d 75 74 65 78 65   {H17026} Mutexe
3ab70 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a  s created using.
3ab80 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ** SQLITE_MUTEX_
3ab90 52 45 43 55 52 53 49 56 45 20 63 61 6e 20 62 65  RECURSIVE can be
3aba0 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c   entered multipl
3abb0 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73  e times by the s
3abc0 61 6d 65 20 74 68 72 65 61 64 2e 0a 2a 2a 20 7b  ame thread..** {
3abd0 48 31 37 30 32 37 7d 20 49 6e 20 73 75 63 68 20  H17027} In such 
3abe0 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75  cases the,.** mu
3abf0 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74  tex must be exit
3ac00 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62  ed an equal numb
3ac10 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f  er of times befo
3ac20 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61  re another threa
3ac30 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20  d.** can enter. 
3ac40 20 7b 41 31 37 30 32 38 7d 20 49 66 20 74 68 65   {A17028} If the
3ac50 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69   same thread tri
3ac60 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20  es to enter any 
3ac70 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66  other.** kind of
3ac80 20 6d 75 74 65 78 20 6d 6f 72 65 20 74 68 61 6e   mutex more than
3ac90 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76   once, the behav
3aca0 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
3acb0 2e 0a 2a 2a 20 7b 48 31 37 30 32 39 7d 20 53 51  ..** {H17029} SQ
3acc0 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  Lite will never 
3acd0 65 78 68 69 62 69 74 0a 2a 2a 20 73 75 63 68 20  exhibit.** such 
3ace0 62 65 68 61 76 69 6f 72 20 69 6e 20 69 74 73 20  behavior in its 
3acf0 6f 77 6e 20 75 73 65 20 6f 66 20 6d 75 74 65 78  own use of mutex
3ad00 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 73  es..**.** Some s
3ad10 79 73 74 65 6d 73 20 28 66 6f 72 20 65 78 61 6d  ystems (for exam
3ad20 70 6c 65 2c 20 57 69 6e 64 6f 77 73 20 39 35 29  ple, Windows 95)
3ad30 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20   do not support 
3ad40 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a  the operation.**
3ad50 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20   implemented by 
3ad60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
3ad70 79 28 29 2e 20 20 4f 6e 20 74 68 6f 73 65 20 73  y().  On those s
3ad80 79 73 74 65 6d 73 2c 20 73 71 6c 69 74 65 33 5f  ystems, sqlite3_
3ad90 6d 75 74 65 78 5f 74 72 79 28 29 0a 2a 2a 20 77  mutex_try().** w
3ada0 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72  ill always retur
3adb0 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20  n SQLITE_BUSY.  
3adc0 7b 48 31 37 30 33 30 7d 20 54 68 65 20 53 51 4c  {H17030} The SQL
3add0 69 74 65 20 63 6f 72 65 20 6f 6e 6c 79 20 65 76  ite core only ev
3ade0 65 72 20 75 73 65 73 0a 2a 2a 20 73 71 6c 69 74  er uses.** sqlit
3adf0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 61  e3_mutex_try() a
3ae00 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
3ae10 6e 20 73 6f 20 74 68 69 73 20 69 73 20 61 63 63  n so this is acc
3ae20 65 70 74 61 62 6c 65 20 62 65 68 61 76 69 6f 72  eptable behavior
3ae30 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30 33 31 7d  ..**.** {H17031}
3ae40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
3ae50 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69  ex_leave() routi
3ae60 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78  ne exits a mutex
3ae70 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65   that was.** pre
3ae80 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20  viously entered 
3ae90 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
3aea0 61 64 2e 20 20 7b 41 31 37 30 33 32 7d 20 54 68  ad.  {A17032} Th
3aeb0 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73  e behavior.** is
3aec0 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
3aed0 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63  e mutex is not c
3aee0 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64  urrently entered
3aef0 20 62 79 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69   by the.** calli
3af00 6e 67 20 74 68 72 65 61 64 20 6f 72 20 69 73 20  ng thread or is 
3af10 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61 6c  not currently al
3af20 6c 6f 63 61 74 65 64 2e 20 20 7b 48 31 37 30 33  located.  {H1703
3af30 33 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 0a 2a  3} SQLite will.*
3af40 2a 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65  * never do eithe
3af50 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49  r. {END}.**.** I
3af60 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74  f the argument t
3af70 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
3af80 65 6e 74 65 72 28 29 2c 20 73 71 6c 69 74 65 33  enter(), sqlite3
3af90 5f 6d 75 74 65 78 5f 74 72 79 28 29 2c 20 6f 72  _mutex_try(), or
3afa0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
3afb0 78 5f 6c 65 61 76 65 28 29 20 69 73 20 61 20 4e  x_leave() is a N
3afc0 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65  ULL pointer, the
3afd0 6e 20 61 6c 6c 20 74 68 72 65 65 20 72 6f 75 74  n all three rout
3afe0 69 6e 65 73 0a 2a 2a 20 62 65 68 61 76 65 20 61  ines.** behave a
3aff0 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20  s no-ops..**.** 
3b000 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
3b010 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d  e3_mutex_held()]
3b020 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 6d 75   and [sqlite3_mu
3b030 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 5d 2e 0a  tex_notheld()]..
3b040 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
3b050 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c  lite3_mutex *sql
3b060 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
3b070 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  (int);.SQLITE_AP
3b080 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d  I void sqlite3_m
3b090 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65  utex_free(sqlite
3b0a0 33 5f 6d 75 74 65 78 2a 29 3b 0a 53 51 4c 49 54  3_mutex*);.SQLIT
3b0b0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
3b0c0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73  e3_mutex_enter(s
3b0d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
3b0e0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3b0f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
3b100 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
3b110 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
3b120 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
3b130 6c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75  leave(sqlite3_mu
3b140 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tex*);../*.** CA
3b150 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 4d 65  PI3REF: Mutex Me
3b160 74 68 6f 64 73 20 4f 62 6a 65 63 74 20 7b 48 31  thods Object {H1
3b170 37 31 32 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a  7120} <S20130>.*
3b180 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3b190 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
3b1a0 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
3b1b0 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 6c  re defines the l
3b1c0 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65  ow-level routine
3b1d0 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 61 6c 6c  s.** used to all
3b1e0 6f 63 61 74 65 20 61 6e 64 20 75 73 65 20 6d 75  ocate and use mu
3b1f0 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 55 73 75  texes..**.** Usu
3b200 61 6c 6c 79 2c 20 74 68 65 20 64 65 66 61 75 6c  ally, the defaul
3b210 74 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  t mutex implemen
3b220 74 61 74 69 6f 6e 73 20 70 72 6f 76 69 64 65 64  tations provided
3b230 20 62 79 20 53 51 4c 69 74 65 20 61 72 65 0a 2a   by SQLite are.*
3b240 2a 20 73 75 66 66 69 63 69 65 6e 74 2c 20 68 6f  * sufficient, ho
3b250 77 65 76 65 72 20 74 68 65 20 75 73 65 72 20 68  wever the user h
3b260 61 73 20 74 68 65 20 6f 70 74 69 6f 6e 20 6f 66  as the option of
3b270 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 61 20   substituting a 
3b280 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d 70 6c 65 6d  custom.** implem
3b290 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 73 70 65  entation for spe
3b2a0 63 69 61 6c 69 7a 65 64 20 64 65 70 6c 6f 79 6d  cialized deploym
3b2b0 65 6e 74 73 20 6f 72 20 73 79 73 74 65 6d 73 20  ents or systems 
3b2c0 66 6f 72 20 77 68 69 63 68 20 53 51 4c 69 74 65  for which SQLite
3b2d0 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f  .** does not pro
3b2e0 76 69 64 65 20 61 20 73 75 69 74 61 62 6c 65 20  vide a suitable 
3b2f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20  implementation. 
3b300 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 68  In this case, th
3b310 65 20 75 73 65 72 0a 2a 2a 20 63 72 65 61 74 65  e user.** create
3b320 73 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 73 20  s and populates 
3b330 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
3b340 68 69 73 20 73 74 72 75 63 74 75 72 65 20 74 6f  his structure to
3b350 20 70 61 73 73 0a 2a 2a 20 74 6f 20 73 71 6c 69   pass.** to sqli
3b360 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 61 6c 6f  te3_config() alo
3b370 6e 67 20 77 69 74 68 20 74 68 65 20 5b 53 51 4c  ng with the [SQL
3b380 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58  ITE_CONFIG_MUTEX
3b390 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 20 41 64 64  ] option..** Add
3b3a0 69 74 69 6f 6e 61 6c 6c 79 2c 20 61 6e 20 69 6e  itionally, an in
3b3b0 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
3b3c0 74 72 75 63 74 75 72 65 20 63 61 6e 20 62 65 20  tructure can be 
3b3d0 75 73 65 64 20 61 73 20 61 6e 0a 2a 2a 20 6f 75  used as an.** ou
3b3e0 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 77 68  tput variable wh
3b3f0 65 6e 20 71 75 65 72 79 69 6e 67 20 74 68 65 20  en querying the 
3b400 73 79 73 74 65 6d 20 66 6f 72 20 74 68 65 20 63  system for the c
3b410 75 72 72 65 6e 74 20 6d 75 74 65 78 0a 2a 2a 20  urrent mutex.** 
3b420 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
3b430 75 73 69 6e 67 20 74 68 65 20 5b 53 51 4c 49 54  using the [SQLIT
3b440 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45  E_CONFIG_GETMUTE
3b450 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  X] option..**.**
3b460 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74 20   The xMutexInit 
3b470 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64 20 62  method defined b
3b480 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  y this structure
3b490 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73 0a 2a   is invoked as.*
3b4a0 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74 65 6d  * part of system
3b4b0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
3b4c0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  by the sqlite3_i
3b4d0 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 75 6e 63  nitialize() func
3b4e0 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31 37 30 30 31  tion..** {H17001
3b4f0 7d 20 54 68 65 20 78 4d 75 74 65 78 49 6e 69 74  } The xMutexInit
3b500 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 62   routine shall b
3b510 65 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69  e called by SQLi
3b520 74 65 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68  te once for each
3b530 0a 2a 2a 20 65 66 66 65 63 74 69 76 65 20 63 61  .** effective ca
3b540 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69  ll to [sqlite3_i
3b550 6e 69 74 69 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a  nitialize()]..**
3b560 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 45 6e  .** The xMutexEn
3b570 64 20 6d 65 74 68 6f 64 20 64 65 66 69 6e 65 64  d method defined
3b580 20 62 79 20 74 68 69 73 20 73 74 72 75 63 74 75   by this structu
3b590 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 73  re is invoked as
3b5a0 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79 73 74  .** part of syst
3b5b0 65 6d 20 73 68 75 74 64 6f 77 6e 20 62 79 20 74  em shutdown by t
3b5c0 68 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  he sqlite3_shutd
3b5d0 6f 77 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20  own() function. 
3b5e0 54 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  The.** implement
3b5f0 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6d 65  ation of this me
3b600 74 68 6f 64 20 69 73 20 65 78 70 65 63 74 65 64  thod is expected
3b610 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20   to release all 
3b620 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20 72  outstanding.** r
3b630 65 73 6f 75 72 63 65 73 20 6f 62 74 61 69 6e 65  esources obtaine
3b640 64 20 62 79 20 74 68 65 20 6d 75 74 65 78 20 6d  d by the mutex m
3b650 65 74 68 6f 64 73 20 69 6d 70 6c 65 6d 65 6e 74  ethods implement
3b660 61 74 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c  ation, especiall
3b670 79 0a 2a 2a 20 74 68 6f 73 65 20 6f 62 74 61 69  y.** those obtai
3b680 6e 65 64 20 62 79 20 74 68 65 20 78 4d 75 74 65  ned by the xMute
3b690 78 49 6e 69 74 20 6d 65 74 68 6f 64 2e 20 7b 48  xInit method. {H
3b6a0 31 37 30 30 33 7d 20 54 68 65 20 78 4d 75 74 65  17003} The xMute
3b6b0 78 45 6e 64 28 29 0a 2a 2a 20 69 6e 74 65 72 66  xEnd().** interf
3b6c0 61 63 65 20 73 68 61 6c 6c 20 62 65 20 69 6e 76  ace shall be inv
3b6d0 6f 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  oked once for ea
3b6e0 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ch call to [sqli
3b6f0 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2e  te3_shutdown()].
3b700 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69  .**.** The remai
3b710 6e 69 6e 67 20 73 65 76 65 6e 20 6d 65 74 68 6f  ning seven metho
3b720 64 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68  ds defined by th
3b730 69 73 20 73 74 72 75 63 74 75 72 65 20 28 78 4d  is structure (xM
3b740 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a 2a 20 78 4d  utexAlloc,.** xM
3b750 75 74 65 78 46 72 65 65 2c 20 78 4d 75 74 65 78  utexFree, xMutex
3b760 45 6e 74 65 72 2c 20 78 4d 75 74 65 78 54 72 79  Enter, xMutexTry
3b770 2c 20 78 4d 75 74 65 78 4c 65 61 76 65 2c 20 78  , xMutexLeave, x
3b780 4d 75 74 65 78 48 65 6c 64 20 61 6e 64 0a 2a 2a  MutexHeld and.**
3b790 20 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64 29 20   xMutexNotheld) 
3b7a0 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66 6f  implement the fo
3b7b0 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63  llowing interfac
3b7c0 65 73 20 28 72 65 73 70 65 63 74 69 76 65 6c 79  es (respectively
3b7d0 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  ):.**.** <ul>.**
3b7e0 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65     <li>  [sqlite
3b7f0 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d  3_mutex_alloc()]
3b800 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e   </li>.**   <li>
3b810 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78    [sqlite3_mutex
3b820 5f 66 72 65 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a  _free()] </li>.*
3b830 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74  *   <li>  [sqlit
3b840 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29  e3_mutex_enter()
3b850 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69  ] </li>.**   <li
3b860 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  >  [sqlite3_mute
3b870 78 5f 74 72 79 28 29 5d 20 3c 2f 6c 69 3e 0a 2a  x_try()] </li>.*
3b880 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74  *   <li>  [sqlit
3b890 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29  e3_mutex_leave()
3b8a0 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69  ] </li>.**   <li
3b8b0 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  >  [sqlite3_mute
3b8c0 78 5f 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a  x_held()] </li>.
3b8d0 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71 6c 69  **   <li>  [sqli
3b8e0 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
3b8f0 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f  d()] </li>.** </
3b900 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e  ul>.**.** The on
3b910 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20 69 73  ly difference is
3b920 20 74 68 61 74 20 74 68 65 20 70 75 62 6c 69 63   that the public
3b930 20 73 71 6c 69 74 65 33 5f 58 58 58 20 66 75 6e   sqlite3_XXX fun
3b940 63 74 69 6f 6e 73 20 65 6e 75 6d 65 72 61 74 65  ctions enumerate
3b950 64 0a 2a 2a 20 61 62 6f 76 65 20 73 69 6c 65 6e  d.** above silen
3b960 74 6c 79 20 69 67 6e 6f 72 65 20 61 6e 79 20 69  tly ignore any i
3b970 6e 76 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20  nvocations that 
3b980 70 61 73 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  pass a NULL poin
3b990 74 65 72 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f  ter instead.** o
3b9a0 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20  f a valid mutex 
3b9b0 68 61 6e 64 6c 65 2e 20 54 68 65 20 69 6d 70 6c  handle. The impl
3b9c0 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74  ementations of t
3b9d0 68 65 20 6d 65 74 68 6f 64 73 20 64 65 66 69 6e  he methods defin
3b9e0 65 64 0a 2a 2a 20 62 79 20 74 68 69 73 20 73 74  ed.** by this st
3b9f0 72 75 63 74 75 72 65 20 61 72 65 20 6e 6f 74 20  ructure are not 
3ba00 72 65 71 75 69 72 65 64 20 74 6f 20 68 61 6e 64  required to hand
3ba10 6c 65 20 74 68 69 73 20 63 61 73 65 2c 20 74 68  le this case, th
3ba20 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 6f 66 20  e results.** of 
3ba30 70 61 73 73 69 6e 67 20 61 20 4e 55 4c 4c 20 70  passing a NULL p
3ba40 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 20 6f  ointer instead o
3ba50 66 20 61 20 76 61 6c 69 64 20 6d 75 74 65 78 20  f a valid mutex 
3ba60 68 61 6e 64 6c 65 20 61 72 65 20 75 6e 64 65 66  handle are undef
3ba70 69 6e 65 64 0a 2a 2a 20 28 69 2e 65 2e 20 69 74  ined.** (i.e. it
3ba80 20 69 73 20 61 63 63 65 70 74 61 62 6c 65 20 74   is acceptable t
3ba90 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 69 6d 70  o provide an imp
3baa0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74  lementation that
3bab0 20 73 65 67 66 61 75 6c 74 73 20 69 66 0a 2a 2a   segfaults if.**
3bac0 20 69 74 20 69 73 20 70 61 73 73 65 64 20 61 20   it is passed a 
3bad0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 29 2e 0a 2a  NULL pointer)..*
3bae0 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49  *.** The xMutexI
3baf0 6e 69 74 28 29 20 6d 65 74 68 6f 64 20 6d 75 73  nit() method mus
3bb00 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65 2e  t be threadsafe.
3bb10 20 20 49 74 20 6d 75 73 74 20 62 65 20 68 61 72    It must be har
3bb20 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 69 6e 76 6f  mless to.** invo
3bb30 6b 65 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20  ke xMutexInit() 
3bb40 6d 75 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69  mutiple times wi
3bb50 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72  thin the same pr
3bb60 6f 63 65 73 73 20 61 6e 64 20 77 69 74 68 6f 75  ocess and withou
3bb70 74 0a 2a 2a 20 69 6e 74 65 72 76 65 6e 69 6e 67  t.** intervening
3bb80 20 63 61 6c 6c 73 20 74 6f 20 78 4d 75 74 65 78   calls to xMutex
3bb90 45 6e 64 28 29 2e 20 20 53 65 63 6f 6e 64 20 61  End().  Second a
3bba0 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  nd subsequent ca
3bbb0 6c 6c 73 20 74 6f 0a 2a 2a 20 78 4d 75 74 65 78  lls to.** xMutex
3bbc0 49 6e 69 74 28 29 20 6d 75 73 74 20 62 65 20 6e  Init() must be n
3bbd0 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 78 4d 75  o-ops..**.** xMu
3bbe0 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20 6e  texInit() must n
3bbf0 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65  ot use SQLite me
3bc00 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
3bc10 28 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ([sqlite3_malloc
3bc20 28 29 5d 0a 2a 2a 20 61 6e 64 20 69 74 73 20 61  ()].** and its a
3bc30 73 73 6f 63 69 61 74 65 73 29 2e 20 20 53 69 6d  ssociates).  Sim
3bc40 69 6c 61 72 6c 79 2c 20 78 4d 75 74 65 78 41 6c  ilarly, xMutexAl
3bc50 6c 6f 63 28 29 20 6d 75 73 74 20 6e 6f 74 20 75  loc() must not u
3bc60 73 65 20 53 51 4c 69 74 65 20 6d 65 6d 6f 72 79  se SQLite memory
3bc70 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  .** allocation f
3bc80 6f 72 20 61 20 73 74 61 74 69 63 20 6d 75 74 65  or a static mute
3bc90 78 2e 20 20 48 6f 77 65 76 65 72 20 78 4d 75 74  x.  However xMut
3bca0 65 78 41 6c 6c 6f 63 28 29 20 6d 61 79 20 75 73  exAlloc() may us
3bcb0 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 65 6d 6f  e SQLite.** memo
3bcc0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 6f  ry allocation fo
3bcd0 72 20 61 20 66 61 73 74 20 6f 72 20 72 65 63 75  r a fast or recu
3bce0 72 73 69 76 65 20 6d 75 74 65 78 2e 0a 2a 2a 0a  rsive mutex..**.
3bcf0 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69  ** SQLite will i
3bd00 6e 76 6f 6b 65 20 74 68 65 20 78 4d 75 74 65 78  nvoke the xMutex
3bd10 45 6e 64 28 29 20 6d 65 74 68 6f 64 20 77 68 65  End() method whe
3bd20 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74 64  n [sqlite3_shutd
3bd30 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a 20 63 61 6c  own()] is.** cal
3bd40 6c 65 64 2c 20 62 75 74 20 6f 6e 6c 79 20 69 66  led, but only if
3bd50 20 74 68 65 20 70 72 69 6f 72 20 63 61 6c 6c 20   the prior call 
3bd60 74 6f 20 78 4d 75 74 65 78 49 6e 69 74 20 72 65  to xMutexInit re
3bd70 74 75 72 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4b  turned SQLITE_OK
3bd80 2e 0a 2a 2a 20 49 66 20 78 4d 75 74 65 78 49 6e  ..** If xMutexIn
3bd90 69 74 20 66 61 69 6c 73 20 69 6e 20 61 6e 79 20  it fails in any 
3bda0 77 61 79 2c 20 69 74 20 69 73 20 65 78 70 65 63  way, it is expec
3bdb0 74 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20  ted to clean up 
3bdc0 61 66 74 65 72 20 69 74 73 65 6c 66 0a 2a 2a 20  after itself.** 
3bdd0 70 72 69 6f 72 20 74 6f 20 72 65 74 75 72 6e 69  prior to returni
3bde0 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ng..*/.typedef s
3bdf0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75  truct sqlite3_mu
3be00 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69  tex_methods sqli
3be10 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
3be20 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  s;.struct sqlite
3be30 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
3be40 7b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78  {.  int (*xMutex
3be50 49 6e 69 74 29 28 76 6f 69 64 29 3b 0a 20 20 69  Init)(void);.  i
3be60 6e 74 20 28 2a 78 4d 75 74 65 78 45 6e 64 29 28  nt (*xMutexEnd)(
3be70 76 6f 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  void);.  sqlite3
3be80 5f 6d 75 74 65 78 20 2a 28 2a 78 4d 75 74 65 78  _mutex *(*xMutex
3be90 41 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76  Alloc)(int);.  v
3bea0 6f 69 64 20 28 2a 78 4d 75 74 65 78 46 72 65 65  oid (*xMutexFree
3beb0 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  )(sqlite3_mutex 
3bec0 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75  *);.  void (*xMu
3bed0 74 65 78 45 6e 74 65 72 29 28 73 71 6c 69 74 65  texEnter)(sqlite
3bee0 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e  3_mutex *);.  in
3bef0 74 20 28 2a 78 4d 75 74 65 78 54 72 79 29 28 73  t (*xMutexTry)(s
3bf00 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b  qlite3_mutex *);
3bf10 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78  .  void (*xMutex
3bf20 4c 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d  Leave)(sqlite3_m
3bf30 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28  utex *);.  int (
3bf40 2a 78 4d 75 74 65 78 48 65 6c 64 29 28 73 71 6c  *xMutexHeld)(sql
3bf50 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20  ite3_mutex *);. 
3bf60 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 4e 6f 74   int (*xMutexNot
3bf70 68 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75  held)(sqlite3_mu
3bf80 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  tex *);.};../*.*
3bf90 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
3bfa0 78 20 56 65 72 69 66 69 63 61 74 69 6f 6e 20 52  x Verification R
3bfb0 6f 75 74 69 6e 65 73 20 7b 48 31 37 30 38 30 7d  outines {H17080}
3bfc0 20 3c 53 32 30 31 33 30 3e 20 3c 53 33 30 38 30   <S20130> <S3080
3bfd0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  0>.**.** The sql
3bfe0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
3bff0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  ) and sqlite3_mu
3c000 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f  tex_notheld() ro
3c010 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 69 6e  utines.** are in
3c020 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69  tended for use i
3c030 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73  nside assert() s
3c040 74 61 74 65 6d 65 6e 74 73 2e 20 7b 48 31 37 30  tatements. {H170
3c050 38 31 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63  81} The SQLite c
3c060 6f 72 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73 65  ore.** never use
3c070 73 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  s these routines
3c080 20 65 78 63 65 70 74 20 69 6e 73 69 64 65 20 61   except inside a
3c090 6e 20 61 73 73 65 72 74 28 29 20 61 6e 64 20 61  n assert() and a
3c0a0 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61  pplications.** a
3c0b0 72 65 20 61 64 76 69 73 65 64 20 74 6f 20 66 6f  re advised to fo
3c0c0 6c 6c 6f 77 20 74 68 65 20 6c 65 61 64 20 6f 66  llow the lead of
3c0d0 20 74 68 65 20 63 6f 72 65 2e 20 20 7b 48 31 37   the core.  {H17
3c0e0 30 38 32 7d 20 54 68 65 20 63 6f 72 65 20 6f 6e  082} The core on
3c0f0 6c 79 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69  ly.** provides i
3c100 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66  mplementations f
3c110 6f 72 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  or these routine
3c120 73 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d  s when it is com
3c130 70 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  piled.** with th
3c140 65 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66  e SQLITE_DEBUG f
3c150 6c 61 67 2e 20 20 7b 41 31 37 30 38 37 7d 20 45  lag.  {A17087} E
3c160 78 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 6d  xternal mutex im
3c170 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a  plementations.**
3c180 20 61 72 65 20 6f 6e 6c 79 20 72 65 71 75 69 72   are only requir
3c190 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68  ed to provide th
3c1a0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69 66 20  ese routines if 
3c1b0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 0a  SQLITE_DEBUG is.
3c1c0 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 69  ** defined and i
3c1d0 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20  f NDEBUG is not 
3c1e0 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b  defined..**.** {
3c1f0 48 31 37 30 38 33 7d 20 54 68 65 73 65 20 72 6f  H17083} These ro
3c200 75 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65  utines should re
3c210 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
3c220 20 6d 75 74 65 78 20 69 6e 20 74 68 65 69 72 20   mutex in their 
3c230 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68  argument.** is h
3c240 65 6c 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c  eld or not held,
3c250 20 72 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62   respectively, b
3c260 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68  y the calling th
3c270 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 58 31 37  read..**.** {X17
3c280 30 38 34 7d 20 54 68 65 20 69 6d 70 6c 65 6d 65  084} The impleme
3c290 6e 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72  ntation is not r
3c2a0 65 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69  equired to provi
3c2b0 64 65 64 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ded versions of 
3c2c0 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65  these.** routine
3c2d0 73 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20  s that actually 
3c2e0 77 6f 72 6b 2e 20 49 66 20 74 68 65 20 69 6d 70  work. If the imp
3c2f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73  lementation does
3c300 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 77 6f 72   not provide wor
3c310 6b 69 6e 67 0a 2a 2a 20 76 65 72 73 69 6f 6e 73  king.** versions
3c320 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e   of these routin
3c330 65 73 2c 20 69 74 20 73 68 6f 75 6c 64 20 61 74  es, it should at
3c340 20 6c 65 61 73 74 20 70 72 6f 76 69 64 65 20 73   least provide s
3c350 74 75 62 73 20 74 68 61 74 20 61 6c 77 61 79 73  tubs that always
3c360 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20  .** return true 
3c370 73 6f 20 74 68 61 74 20 6f 6e 65 20 64 6f 65 73  so that one does
3c380 20 6e 6f 74 20 67 65 74 20 73 70 75 72 69 6f 75   not get spuriou
3c390 73 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c  s assertion fail
3c3a0 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  ures..**.** {H17
3c3b0 30 38 35 7d 20 49 66 20 74 68 65 20 61 72 67 75  085} If the argu
3c3c0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f  ment to sqlite3_
3c3d0 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20  mutex_held() is 
3c3e0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74  a NULL pointer t
3c3f0 68 65 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69  hen.** the routi
3c400 6e 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e  ne should return
3c410 20 31 2e 20 20 7b 45 4e 44 7d 20 54 68 69 73 20   1.  {END} This 
3c420 73 65 65 6d 73 20 63 6f 75 6e 74 65 72 2d 69 6e  seems counter-in
3c430 74 75 69 74 69 76 65 20 73 69 6e 63 65 0a 2a 2a  tuitive since.**
3c440 20 63 6c 65 61 72 6c 79 20 74 68 65 20 6d 75 74   clearly the mut
3c450 65 78 20 63 61 6e 6e 6f 74 20 62 65 20 68 65 6c  ex cannot be hel
3c460 64 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74  d if it does not
3c470 20 65 78 69 73 74 2e 20 20 42 75 74 20 74 68 65   exist.  But the
3c480 0a 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 20 74  .** the reason t
3c490 68 65 20 6d 75 74 65 78 20 64 6f 65 73 20 6e 6f  he mutex does no
3c4a0 74 20 65 78 69 73 74 20 69 73 20 62 65 63 61 75  t exist is becau
3c4b0 73 65 20 74 68 65 20 62 75 69 6c 64 20 69 73 20  se the build is 
3c4c0 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67 20 6d 75 74  not.** using mut
3c4d0 65 78 65 73 2e 20 20 41 6e 64 20 77 65 20 64 6f  exes.  And we do
3c4e0 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 61 73   not want the as
3c4f0 73 65 72 74 28 29 20 63 6f 6e 74 61 69 6e 69 6e  sert() containin
3c500 67 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f  g the.** call to
3c510 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
3c520 65 6c 64 28 29 20 74 6f 20 66 61 69 6c 2c 20 73  eld() to fail, s
3c530 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74  o a non-zero ret
3c540 75 72 6e 20 69 73 0a 2a 2a 20 74 68 65 20 61 70  urn is.** the ap
3c550 70 72 6f 70 72 69 61 74 65 20 74 68 69 6e 67 20  propriate thing 
3c560 74 6f 20 64 6f 2e 20 20 7b 48 31 37 30 38 36 7d  to do.  {H17086}
3c570 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
3c580 65 78 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20  ex_notheld().** 
3c590 69 6e 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64  interface should
3c5a0 20 61 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77   also return 1 w
3c5b0 68 65 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c  hen given a NULL
3c5c0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c   pointer..*/.SQL
3c5d0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
3c5e0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73  te3_mutex_held(s
3c5f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
3c600 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3c610 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
3c620 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
3c630 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ex*);../*.** CAP
3c640 49 33 52 45 46 3a 20 4d 75 74 65 78 20 54 79 70  I3REF: Mutex Typ
3c650 65 73 20 7b 48 31 37 30 30 31 7d 20 3c 48 31 37  es {H17001} <H17
3c660 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  000>.**.** The [
3c670 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
3c680 6c 6f 63 28 29 5d 20 69 6e 74 65 72 66 61 63 65  loc()] interface
3c690 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20   takes a single 
3c6a0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 69 63  argument.** whic
3c6b0 68 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73  h is one of thes
3c6c0 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61  e integer consta
3c6d0 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  nts..**.** The s
3c6e0 65 74 20 6f 66 20 73 74 61 74 69 63 20 6d 75 74  et of static mut
3c6f0 65 78 65 73 20 6d 61 79 20 63 68 61 6e 67 65 20  exes may change 
3c700 66 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 65 20  from one SQLite 
3c710 72 65 6c 65 61 73 65 20 74 6f 20 74 68 65 0a 2a  release to the.*
3c720 2a 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61  * next.  Applica
3c730 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 72  tions that overr
3c740 69 64 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e  ide the built-in
3c750 20 6d 75 74 65 78 20 6c 6f 67 69 63 20 6d 75 73   mutex logic mus
3c760 74 20 62 65 0a 2a 2a 20 70 72 65 70 61 72 65 64  t be.** prepared
3c770 20 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20   to accommodate 
3c780 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69  additional stati
3c790 63 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 64  c mutexes..*/.#d
3c7a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
3c7b0 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20  EX_FAST         
3c7c0 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51      0.#define SQ
3c7d0 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
3c7e0 53 49 56 45 20 20 20 20 20 20 20 20 31 0a 23 64  SIVE        1.#d
3c7f0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
3c800 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
3c810 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51      2.#define SQ
3c820 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3c830 43 5f 4d 45 4d 20 20 20 20 20 20 20 33 20 20 2f  C_MEM       3  /
3c840 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  * sqlite3_malloc
3c850 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  () */.#define SQ
3c860 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3c870 43 5f 4d 45 4d 32 20 20 20 20 20 20 34 20 20 2f  C_MEM2      4  /
3c880 2a 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 23 64  * NOT USED */.#d
3c890 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54  efine SQLITE_MUT
3c8a0 45 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 20 20  EX_STATIC_OPEN  
3c8b0 20 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65      4  /* sqlite
3c8c0 33 42 74 72 65 65 4f 70 65 6e 28 29 20 2a 2f 0a  3BtreeOpen() */.
3c8d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
3c8e0 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47  UTEX_STATIC_PRNG
3c8f0 20 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69        5  /* sqli
3c900 74 65 33 5f 72 61 6e 64 6f 6d 28 29 20 2a 2f 0a  te3_random() */.
3c910 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d  #define SQLITE_M
3c920 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 20  UTEX_STATIC_LRU 
3c930 20 20 20 20 20 20 36 20 20 2f 2a 20 6c 72 75 20        6  /* lru 
3c940 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 23 64 65  page list */.#de
3c950 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
3c960 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 20 20  X_STATIC_LRU2   
3c970 20 20 20 37 20 20 2f 2a 20 6c 72 75 20 70 61 67     7  /* lru pag
3c980 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  e list */../*.**
3c990 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72 69   CAPI3REF: Retri
3c9a0 65 76 65 20 74 68 65 20 6d 75 74 65 78 20 66 6f  eve the mutex fo
3c9b0 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  r a database con
3c9c0 6e 65 63 74 69 6f 6e 20 7b 48 31 37 30 30 32 7d  nection {H17002}
3c9d0 20 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20   <H17000>.**.** 
3c9e0 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 72  This interface r
3c9f0 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
3ca00 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75   the [sqlite3_mu
3ca10 74 65 78 5d 20 6f 62 6a 65 63 74 20 74 68 61 74  tex] object that
3ca20 20 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 65 73 20   .** serializes 
3ca30 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 5b 64  access to the [d
3ca40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3ca50 6f 6e 5d 20 67 69 76 65 6e 20 69 6e 20 74 68 65  on] given in the
3ca60 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 65   argument.** whe
3ca70 6e 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67  n the [threading
3ca80 20 6d 6f 64 65 5d 20 69 73 20 53 65 72 69 61 6c   mode] is Serial
3ca90 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20  ized..** If the 
3caa0 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d  [threading mode]
3cab0 20 69 73 20 53 69 6e 67 6c 65 2d 74 68 72 65 61   is Single-threa
3cac0 64 20 6f 72 20 4d 75 6c 74 69 2d 74 68 72 65 61  d or Multi-threa
3cad0 64 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72  d then this.** r
3cae0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61  outine returns a
3caf0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
3cb00 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
3cb10 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69  ite3_mutex *sqli
3cb20 74 65 33 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c  te3_db_mutex(sql
3cb30 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  ite3*);../*.** C
3cb40 41 50 49 33 52 45 46 3a 20 4c 6f 77 2d 4c 65 76  API3REF: Low-Lev
3cb50 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f 66 20 44 61  el Control Of Da
3cb60 74 61 62 61 73 65 20 46 69 6c 65 73 20 7b 48 31  tabase Files {H1
3cb70 31 33 30 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a  1300} <S30800>.*
3cb80 2a 0a 2a 2a 20 7b 48 31 31 33 30 31 7d 20 54 68  *.** {H11301} Th
3cb90 65 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  e [sqlite3_file_
3cba0 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72  control()] inter
3cbb0 66 61 63 65 20 6d 61 6b 65 73 20 61 20 64 69 72  face makes a dir
3cbc0 65 63 74 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a  ect call to the.
3cbd0 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  ** xFileControl 
3cbe0 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 65 20 5b  method for the [
3cbf0 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
3cc00 64 73 5d 20 6f 62 6a 65 63 74 20 61 73 73 6f 63  ds] object assoc
3cc10 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 61 20  iated.** with a 
3cc20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62  particular datab
3cc30 61 73 65 20 69 64 65 6e 74 69 66 69 65 64 20 62  ase identified b
3cc40 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  y the second arg
3cc50 75 6d 65 6e 74 2e 20 7b 48 31 31 33 30 32 7d 20  ument. {H11302} 
3cc60 54 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74  The.** name of t
3cc70 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 74  he database is t
3cc80 68 65 20 6e 61 6d 65 20 61 73 73 69 67 6e 65 64  he name assigned
3cc90 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
3cca0 20 62 79 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72   by the.** <a hr
3ccb0 65 66 3d 22 6c 61 6e 67 5f 61 74 74 61 63 68 2e  ef="lang_attach.
3ccc0 68 74 6d 6c 22 3e 41 54 54 41 43 48 3c 2f 61 3e  html">ATTACH</a>
3ccd0 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 68 61   SQL command tha
3cce0 74 20 6f 70 65 6e 65 64 20 74 68 65 0a 2a 2a 20  t opened the.** 
3ccf0 64 61 74 61 62 61 73 65 2e 20 7b 48 31 31 33 30  database. {H1130
3cd00 33 7d 20 54 6f 20 63 6f 6e 74 72 6f 6c 20 74 68  3} To control th
3cd10 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
3cd20 66 69 6c 65 2c 20 75 73 65 20 74 68 65 20 6e 61  file, use the na
3cd30 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a 20 6f 72 20  me "main".** or 
3cd40 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20  a NULL pointer. 
3cd50 7b 48 31 31 33 30 34 7d 20 54 68 65 20 74 68 69  {H11304} The thi
3cd60 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61  rd and fourth pa
3cd70 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73  rameters to this
3cd80 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20   routine.** are 
3cd90 70 61 73 73 65 64 20 64 69 72 65 63 74 6c 79 20  passed directly 
3cda0 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73  through to the s
3cdb0 65 63 6f 6e 64 20 61 6e 64 20 74 68 69 72 64 20  econd and third 
3cdc0 70 61 72 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a  parameters of.**
3cdd0 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   the xFileContro
3cde0 6c 20 6d 65 74 68 6f 64 2e 20 20 7b 48 31 31 33  l method.  {H113
3cdf0 30 35 7d 20 54 68 65 20 72 65 74 75 72 6e 20 76  05} The return v
3ce00 61 6c 75 65 20 6f 66 20 74 68 65 20 78 46 69 6c  alue of the xFil
3ce10 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20 6d 65 74 68  eControl.** meth
3ce20 6f 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72  od becomes the r
3ce30 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74  eturn value of t
3ce40 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a  his routine..**.
3ce50 2a 2a 20 7b 48 31 31 33 30 36 7d 20 49 66 20 74  ** {H11306} If t
3ce60 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
3ce70 74 65 72 20 28 7a 44 62 4e 61 6d 65 29 20 64 6f  ter (zDbName) do
3ce80 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65  es not match the
3ce90 20 6e 61 6d 65 20 6f 66 20 61 6e 79 0a 2a 2a 20   name of any.** 
3cea0 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69  open database fi
3ceb0 6c 65 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f  le, then SQLITE_
3cec0 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65  ERROR is returne
3ced0 64 2e 20 7b 48 31 31 33 30 37 7d 20 54 68 69 73  d. {H11307} This
3cee0 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69   error.** code i
3cef0 73 20 6e 6f 74 20 72 65 6d 65 6d 62 65 72 65 64  s not remembered
3cf00 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65   and will not be
3cf10 20 72 65 63 61 6c 6c 65 64 20 62 79 20 5b 73 71   recalled by [sq
3cf20 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d  lite3_errcode()]
3cf30 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  .** or [sqlite3_
3cf40 65 72 72 6d 73 67 28 29 5d 2e 20 7b 41 31 31 33  errmsg()]. {A113
3cf50 30 38 7d 20 54 68 65 20 75 6e 64 65 72 6c 79 69  08} The underlyi
3cf60 6e 67 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20  ng xFileControl 
3cf70 6d 65 74 68 6f 64 20 6d 69 67 68 74 0a 2a 2a 20  method might.** 
3cf80 61 6c 73 6f 20 72 65 74 75 72 6e 20 53 51 4c 49  also return SQLI
3cf90 54 45 5f 45 52 52 4f 52 2e 20 20 7b 41 31 31 33  TE_ERROR.  {A113
3cfa0 30 39 7d 20 54 68 65 72 65 20 69 73 20 6e 6f 20  09} There is no 
3cfb0 77 61 79 20 74 6f 20 64 69 73 74 69 6e 67 75 69  way to distingui
3cfc0 73 68 20 62 65 74 77 65 65 6e 0a 2a 2a 20 61 6e  sh between.** an
3cfd0 20 69 6e 63 6f 72 72 65 63 74 20 7a 44 62 4e 61   incorrect zDbNa
3cfe0 6d 65 20 61 6e 64 20 61 6e 20 53 51 4c 49 54 45  me and an SQLITE
3cff0 5f 45 52 52 4f 52 20 72 65 74 75 72 6e 20 66 72  _ERROR return fr
3d000 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  om the underlyin
3d010 67 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f  g.** xFileContro
3d020 6c 20 6d 65 74 68 6f 64 2e 20 7b 45 4e 44 7d 0a  l method. {END}.
3d030 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
3d040 5b 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f  [SQLITE_FCNTL_LO
3d050 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49  CKSTATE].*/.SQLI
3d060 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
3d070 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28  e3_file_control(
3d080 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
3d090 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69  char *zDbName, i
3d0a0 6e 74 20 6f 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a  nt op, void*);..
3d0b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3d0c0 54 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61 63  Testing Interfac
3d0d0 65 20 7b 48 31 31 34 30 30 7d 20 3c 53 33 30 38  e {H11400} <S308
3d0e0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  00>.**.** The sq
3d0f0 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
3d100 6f 6c 28 29 20 69 6e 74 65 72 66 61 63 65 20 69  ol() interface i
3d110 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f  s used to read o
3d120 75 74 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 73  ut internal.** s
3d130 74 61 74 65 20 6f 66 20 53 51 4c 69 74 65 20 61  tate of SQLite a
3d140 6e 64 20 74 6f 20 69 6e 6a 65 63 74 20 66 61 75  nd to inject fau
3d150 6c 74 73 20 69 6e 74 6f 20 53 51 4c 69 74 65 20  lts into SQLite 
3d160 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70  for testing.** p
3d170 75 72 70 6f 73 65 73 2e 20 20 54 68 65 20 66 69  urposes.  The fi
3d180 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  rst parameter is
3d190 20 61 6e 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f   an operation co
3d1a0 64 65 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e  de that determin
3d1b0 65 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  es.** the number
3d1c0 2c 20 6d 65 61 6e 69 6e 67 2c 20 61 6e 64 20 6f  , meaning, and o
3d1d0 70 65 72 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20  peration of all 
3d1e0 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61 6d  subsequent param
3d1f0 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  eters..**.** Thi
3d200 73 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e  s interface is n
3d210 6f 74 20 66 6f 72 20 75 73 65 20 62 79 20 61 70  ot for use by ap
3d220 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 49 74 20  plications.  It 
3d230 65 78 69 73 74 73 20 73 6f 6c 65 6c 79 0a 2a 2a  exists solely.**
3d240 20 66 6f 72 20 76 65 72 69 66 79 69 6e 67 20 74   for verifying t
3d250 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61  he correct opera
3d260 74 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c 69  tion of the SQLi
3d270 74 65 20 6c 69 62 72 61 72 79 2e 20 20 44 65 70  te library.  Dep
3d280 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 68 6f 77  ending.** on how
3d290 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
3d2a0 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 2c  ary is compiled,
3d2b0 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   this interface 
3d2c0 6d 69 67 68 74 20 6e 6f 74 20 65 78 69 73 74 2e  might not exist.
3d2d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 74 61 69  .**.** The detai
3d2e0 6c 73 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  ls of the operat
3d2f0 69 6f 6e 20 63 6f 64 65 73 2c 20 74 68 65 69 72  ion codes, their
3d300 20 6d 65 61 6e 69 6e 67 73 2c 20 74 68 65 20 70   meanings, the p
3d310 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 65  arameters.** the
3d320 79 20 74 61 6b 65 2c 20 61 6e 64 20 77 68 61 74  y take, and what
3d330 20 74 68 65 79 20 64 6f 20 61 72 65 20 61 6c 6c   they do are all
3d340 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
3d350 67 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63  ge without notic
3d360 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65 20 6d 6f 73  e..** Unlike mos
3d370 74 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20  t of the SQLite 
3d380 41 50 49 2c 20 74 68 69 73 20 66 75 6e 63 74 69  API, this functi
3d390 6f 6e 20 69 73 20 6e 6f 74 20 67 75 61 72 61 6e  on is not guaran
3d3a0 74 65 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 72 61  teed to.** opera
3d3b0 74 65 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20  te consistently 
3d3c0 66 72 6f 6d 20 6f 6e 65 20 72 65 6c 65 61 73 65  from one release
3d3d0 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2f   to the next..*/
3d3e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3d3f0 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
3d400 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e  trol(int op, ...
3d410 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
3d420 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65  EF: Testing Inte
3d430 72 66 61 63 65 20 4f 70 65 72 61 74 69 6f 6e 20  rface Operation 
3d440 43 6f 64 65 73 20 7b 48 31 31 34 31 30 7d 20 3c  Codes {H11410} <
3d450 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  H11400>.**.** Th
3d460 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  ese constants ar
3d470 65 20 74 68 65 20 76 61 6c 69 64 20 6f 70 65 72  e the valid oper
3d480 61 74 69 6f 6e 20 63 6f 64 65 20 70 61 72 61 6d  ation code param
3d490 65 74 65 72 73 20 75 73 65 64 0a 2a 2a 20 61 73  eters used.** as
3d4a0 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
3d4b0 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ent to [sqlite3_
3d4c0 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 2e  test_control()].
3d4d0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 70 61 72  .**.** These par
3d4e0 61 6d 65 74 65 72 73 20 61 6e 64 20 74 68 65 69  ameters and thei
3d4f0 72 20 6d 65 61 6e 69 6e 67 73 20 61 72 65 20 73  r meanings are s
3d500 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
3d510 0a 2a 2a 20 77 69 74 68 6f 75 74 20 6e 6f 74 69  .** without noti
3d520 63 65 2e 20 20 54 68 65 73 65 20 76 61 6c 75 65  ce.  These value
3d530 73 20 61 72 65 20 66 6f 72 20 74 65 73 74 69 6e  s are for testin
3d540 67 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e  g purposes only.
3d550 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  .** Applications
3d560 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20   should not use 
3d570 61 6e 79 20 6f 66 20 74 68 65 73 65 20 70 61 72  any of these par
3d580 61 6d 65 74 65 72 73 20 6f 72 20 74 68 65 0a 2a  ameters or the.*
3d590 2a 20 5b 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  * [sqlite3_test_
3d5a0 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72  control()] inter
3d5b0 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  face..*/.#define
3d5c0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
3d5d0 5f 50 52 4e 47 5f 53 41 56 45 20 20 20 20 20 20  _PRNG_SAVE      
3d5e0 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66            5.#def
3d5f0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ine SQLITE_TESTC
3d600 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45  TRL_PRNG_RESTORE
3d610 20 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23               6.#
3d620 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
3d630 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45  STCTRL_PRNG_RESE
3d640 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
3d650 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  7.#define SQLITE
3d660 5f 54 45 53 54 43 54 52 4c 5f 42 49 54 56 45 43  _TESTCTRL_BITVEC
3d670 5f 54 45 53 54 20 20 20 20 20 20 20 20 20 20 20  _TEST           
3d680 20 20 20 38 0a 23 64 65 66 69 6e 65 20 53 51 4c     8.#define SQL
3d690 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 46 41 55  ITE_TESTCTRL_FAU
3d6a0 4c 54 5f 49 4e 53 54 41 4c 4c 20 20 20 20 20 20  LT_INSTALL      
3d6b0 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20        9.#define 
3d6c0 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3d6d0 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f  BENIGN_MALLOC_HO
3d6e0 4f 4b 53 20 20 20 20 20 31 30 0a 23 64 65 66 69  OKS     10.#defi
3d6f0 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ne SQLITE_TESTCT
3d700 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  RL_PENDING_BYTE 
3d710 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 64             11.#d
3d720 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53  efine SQLITE_TES
3d730 54 43 54 52 4c 5f 41 53 53 45 52 54 20 20 20 20  TCTRL_ASSERT    
3d740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
3d750 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3d760 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59 53 20  TESTCTRL_ALWAYS 
3d770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d780 20 31 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   13.#define SQLI
3d790 54 45 5f 54 45 53 54 43 54 52 4c 5f 52 45 53 45  TE_TESTCTRL_RESE
3d7a0 52 56 45 20 20 20 20 20 20 20 20 20 20 20 20 20  RVE             
3d7b0 20 20 20 20 31 34 0a 0a 2f 2a 0a 2a 2a 20 43 41      14../*.** CA
3d7c0 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65 20 52  PI3REF: SQLite R
3d7d0 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20 7b 48  untime Status {H
3d7e0 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a  17200} <S60200>.
3d7f0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
3d800 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72  **.** This inter
3d810 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20  face is used to 
3d820 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65  retrieve runtime
3d830 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74   status informat
3d840 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65  ion.** about the
3d850 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f 66 20   preformance of 
3d860 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70 74 69  SQLite, and opti
3d870 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65 74 20  onally to reset 
3d880 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67 68 77  various.** highw
3d890 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54 68 65  ater marks.  The
3d8a0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
3d8b0 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f  is an integer co
3d8c0 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70  de for.** the sp
3d8d0 65 63 69 66 69 63 20 70 61 72 61 6d 65 74 65 72  ecific parameter
3d8e0 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20 52 65   to measure.  Re
3d8f0 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67 65 72  cognized integer
3d900 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20 6f 66   codes.** are of
3d910 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c 49 54   the form [SQLIT
3d920 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
3d930 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f 53 54  USED | SQLITE_ST
3d940 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20 54 68  ATUS_...]..** Th
3d950 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
3d960 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  of the parameter
3d970 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 74   is returned int
3d980 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 20  o *pCurrent..** 
3d990 54 68 65 20 68 69 67 68 65 73 74 20 72 65 63 6f  The highest reco
3d9a0 72 64 65 64 20 76 61 6c 75 65 20 69 73 20 72 65  rded value is re
3d9b0 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69 67 68  turned in *pHigh
3d9c0 77 61 74 65 72 2e 20 20 49 66 20 74 68 65 0a 2a  water.  If the.*
3d9d0 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73 20 74  * resetFlag is t
3d9e0 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69  rue, then the hi
3d9f0 67 68 65 73 74 20 72 65 63 6f 72 64 20 76 61 6c  ghest record val
3da00 75 65 20 69 73 20 72 65 73 65 74 20 61 66 74 65  ue is reset afte
3da10 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72  r.** *pHighwater
3da20 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53 6f 6d   is written. Som
3da30 65 20 70 61 72 61 6d 65 74 65 72 73 20 64 6f 20  e parameters do 
3da40 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65 20 68  not record the h
3da50 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75 65 2e  ighest.** value.
3da60 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61 72 61    For those para
3da70 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68 69 6e  meters.** nothin
3da80 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  g is written int
3da90 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 6e  o *pHighwater an
3daa0 64 20 74 68 65 20 72 65 73 65 74 46 6c 61 67 20  d the resetFlag 
3dab0 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 4f  is ignored..** O
3dac0 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73 20  ther parameters 
3dad0 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65 20  record only the 
3dae0 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20 61  highwater mark a
3daf0 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72 65  nd not the curre
3db00 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46 6f  nt.** value.  Fo
3db10 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20 70  r these latter p
3db20 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69 6e  arameters nothin
3db30 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74  g is written int
3db40 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a 0a  o *pCurrent..**.
3db50 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
3db60 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
3db70 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  K on success and
3db80 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 5b   a non-zero.** [
3db90 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66  error code] on f
3dba0 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ailure..**.** Th
3dbb0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
3dbc0 72 65 61 64 73 61 66 65 20 62 75 74 20 69 73 20  readsafe but is 
3dbd0 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54 68 69  not atomic.  Thi
3dbe0 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65  s routine can be
3dbf0 0a 2a 2a 20 63 61 6c 6c 65 64 20 77 68 69 6c 65  .** called while
3dc00 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61   other threads a
3dc10 72 65 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 73  re running the s
3dc20 61 6d 65 20 6f 72 20 64 69 66 66 65 72 65 6e 74  ame or different
3dc30 20 53 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72   SQLite.** inter
3dc40 66 61 63 65 73 2e 20 20 48 6f 77 65 76 65 72 20  faces.  However 
3dc50 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72  the values retur
3dc60 6e 65 64 20 69 6e 20 2a 70 43 75 72 72 65 6e 74  ned in *pCurrent
3dc70 20 61 6e 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61   and.** *pHighwa
3dc80 74 65 72 20 72 65 66 6c 65 63 74 20 74 68 65 20  ter reflect the 
3dc90 73 74 61 74 75 73 20 6f 66 20 53 51 4c 69 74 65  status of SQLite
3dca0 20 61 74 20 64 69 66 66 65 72 65 6e 74 20 70 6f   at different po
3dcb0 69 6e 74 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20  ints in time.** 
3dcc0 61 6e 64 20 69 74 20 69 73 20 70 6f 73 73 69 62  and it is possib
3dcd0 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20  le that another 
3dce0 74 68 72 65 61 64 20 6d 69 67 68 74 20 63 68 61  thread might cha
3dcf0 6e 67 65 20 74 68 65 20 70 61 72 61 6d 65 74 65  nge the paramete
3dd00 72 0a 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e 20  r.** in between 
3dd10 74 68 65 20 74 69 6d 65 73 20 77 68 65 6e 20 2a  the times when *
3dd20 70 43 75 72 72 65 6e 74 20 61 6e 64 20 2a 70 48  pCurrent and *pH
3dd30 69 67 68 77 61 74 65 72 20 61 72 65 20 77 72 69  ighwater are wri
3dd40 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  tten..**.** See 
3dd50 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64  also: [sqlite3_d
3dd60 62 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53  b_status()].*/.S
3dd70 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
3dd80 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e  _EXPERIMENTAL in
3dd90 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73  t sqlite3_status
3dda0 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43  (int op, int *pC
3ddb0 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69  urrent, int *pHi
3ddc0 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73  ghwater, int res
3ddd0 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a  etFlag);.../*.**
3dde0 20 43 41 50 49 33 52 45 46 3a 20 53 74 61 74 75   CAPI3REF: Statu
3ddf0 73 20 50 61 72 61 6d 65 74 65 72 73 20 7b 48 31  s Parameters {H1
3de00 37 32 35 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a  7250} <H17200>.*
3de10 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3de20 2a 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67  *.** These integ
3de30 65 72 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 73  er constants des
3de40 69 67 6e 61 74 65 20 76 61 72 69 6f 75 73 20 72  ignate various r
3de50 75 6e 2d 74 69 6d 65 20 73 74 61 74 75 73 20 70  un-time status p
3de60 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61  arameters.** tha
3de70 74 20 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65  t can be returne
3de80 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74  d by [sqlite3_st
3de90 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c  atus()]..**.** <
3dea0 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  dl>.** <dt>SQLIT
3deb0 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
3dec0 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  USED</dt>.** <dd
3ded0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3dee0 69 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 61  is the current a
3def0 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
3df00 63 68 65 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75  checked out.** u
3df10 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61  sing [sqlite3_ma
3df20 6c 6c 6f 63 28 29 5d 2c 20 65 69 74 68 65 72 20  lloc()], either 
3df30 64 69 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69  directly or indi
3df40 72 65 63 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20  rectly.  The.** 
3df50 66 69 67 75 72 65 20 69 6e 63 6c 75 64 65 73 20  figure includes 
3df60 63 61 6c 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73  calls made to [s
3df70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
3df80 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74   by the applicat
3df90 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72  ion.** and inter
3dfa0 6e 61 6c 20 6d 65 6d 6f 72 79 20 75 73 61 67 65  nal memory usage
3dfb0 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20 6c   by the SQLite l
3dfc0 69 62 72 61 72 79 2e 20 20 53 63 72 61 74 63 68  ibrary.  Scratch
3dfd0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72   memory.** contr
3dfe0 6f 6c 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45  olled by [SQLITE
3dff0 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d  _CONFIG_SCRATCH]
3e000 20 61 6e 64 20 61 75 78 69 6c 69 61 72 79 20 70   and auxiliary p
3e010 61 67 65 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d  age-cache.** mem
3e020 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62  ory controlled b
3e030 79 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  y [SQLITE_CONFIG
3e040 5f 50 41 47 45 43 41 43 48 45 5d 20 69 73 20 6e  _PAGECACHE] is n
3e050 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a  ot included in.*
3e060 2a 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72  * this parameter
3e070 2e 20 20 54 68 65 20 61 6d 6f 75 6e 74 20 72 65  .  The amount re
3e080 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 73 75  turned is the su
3e090 6d 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  m of the allocat
3e0a0 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20  ion.** sizes as 
3e0b0 72 65 70 6f 72 74 65 64 20 62 79 20 74 68 65 20  reported by the 
3e0c0 78 53 69 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20  xSize method in 
3e0d0 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74  [sqlite3_mem_met
3e0e0 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  hods].</dd>.**.*
3e0f0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41  * <dt>SQLITE_STA
3e100 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c  TUS_MALLOC_SIZE<
3e110 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
3e120 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72   parameter recor
3e130 64 73 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d  ds the largest m
3e140 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
3e150 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64   request.** hand
3e160 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d  ed to [sqlite3_m
3e170 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c  alloc()] or [sql
3e180 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20  ite3_realloc()] 
3e190 28 6f 72 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74  (or their.** int
3e1a0 65 72 6e 61 6c 20 65 71 75 69 76 61 6c 65 6e 74  ernal equivalent
3e1b0 73 29 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61  s).  Only the va
3e1c0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20  lue returned in 
3e1d0 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74  the.** *pHighwat
3e1e0 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  er parameter to 
3e1f0 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28  [sqlite3_status(
3e200 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73  )] is of interes
3e210 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75  t.  .** The valu
3e220 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
3e230 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72  he *pCurrent par
3e240 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69  ameter is undefi
3e250 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ned.</dd>.**.** 
3e260 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55  <dt>SQLITE_STATU
3e270 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44  S_PAGECACHE_USED
3e280 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
3e290 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75  s parameter retu
3e2a0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
3e2b0 66 20 70 61 67 65 73 20 75 73 65 64 20 6f 75 74  f pages used out
3e2c0 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65   of the.** [page
3e2d0 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  cache memory all
3e2e0 6f 63 61 74 6f 72 5d 20 74 68 61 74 20 77 61 73  ocator] that was
3e2f0 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e   configured usin
3e300 67 20 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f  g .** [SQLITE_CO
3e310 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e  NFIG_PAGECACHE].
3e320 20 20 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72    The.** value r
3e330 65 74 75 72 6e 65 64 20 69 73 20 69 6e 20 70 61  eturned is in pa
3e340 67 65 73 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65  ges, not in byte
3e350 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  s.</dd>.**.** <d
3e360 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t>SQLITE_STATUS_
3e370 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c  PAGECACHE_OVERFL
3e380 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  OW</dt>.** <dd>T
3e390 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
3e3a0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
3e3b0 20 6f 66 20 62 79 74 65 73 20 6f 66 20 70 61 67   of bytes of pag
3e3c0 65 20 63 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63  e cache.** alloc
3e3d0 61 74 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c  ation which coul
3e3e0 64 20 6e 6f 74 20 62 65 20 73 74 61 74 69 73 66  d not be statisf
3e3f0 69 65 64 20 62 79 20 74 68 65 20 5b 53 51 4c 49  ied by the [SQLI
3e400 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
3e410 43 48 45 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61  CHE].** buffer a
3e420 6e 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20  nd where forced 
3e430 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b  to overflow to [
3e440 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
3e450 5d 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72  ].  The.** retur
3e460 6e 65 64 20 76 61 6c 75 65 20 69 6e 63 6c 75 64  ned value includ
3e470 65 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74  es allocations t
3e480 68 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62  hat overflowed b
3e490 65 63 61 75 73 65 20 74 68 65 79 0a 2a 2a 20 77  ecause they.** w
3e4a0 68 65 72 65 20 74 6f 6f 20 6c 61 72 67 65 20 28  here too large (
3e4b0 74 68 65 79 20 77 65 72 65 20 6c 61 72 67 65 72  they were larger
3e4c0 20 74 68 61 6e 20 74 68 65 20 22 73 7a 22 20 70   than the "sz" p
3e4d0 61 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b  arameter to.** [
3e4e0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
3e4f0 47 45 43 41 43 48 45 5d 29 20 61 6e 64 20 61 6c  GECACHE]) and al
3e500 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f  locations that o
3e510 76 65 72 66 6c 6f 77 65 64 20 62 65 63 61 75 73  verflowed becaus
3e520 65 0a 2a 2a 20 6e 6f 20 73 70 61 63 65 20 77 61  e.** no space wa
3e530 73 20 6c 65 66 74 20 69 6e 20 74 68 65 20 70 61  s left in the pa
3e540 67 65 20 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a  ge cache.</dd>.*
3e550 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3e560 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
3e570 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  _SIZE</dt>.** <d
3e580 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72  d>This parameter
3e590 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72   records the lar
3e5a0 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  gest memory allo
3e5b0 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a  cation request.*
3e5c0 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 70 61 67  * handed to [pag
3e5d0 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ecache memory al
3e5e0 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20  locator].  Only 
3e5f0 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
3e600 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48  ed in the.** *pH
3e610 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74  ighwater paramet
3e620 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73  er to [sqlite3_s
3e630 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69  tatus()] is of i
3e640 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68  nterest.  .** Th
3e650 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20  e value written 
3e660 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65  into the *pCurre
3e670 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nt parameter is 
3e680 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a  undefined.</dd>.
3e690 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
3e6a0 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f  _STATUS_SCRATCH_
3e6b0 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  USED</dt>.** <dd
3e6c0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3e6d0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
3e6e0 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e  er of allocation
3e6f0 73 20 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68  s used out of th
3e700 65 0a 2a 2a 20 5b 73 63 72 61 74 63 68 20 6d 65  e.** [scratch me
3e710 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20  mory allocator] 
3e720 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67  configured using
3e730 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  .** [SQLITE_CONF
3e740 49 47 5f 53 43 52 41 54 43 48 5d 2e 20 20 54 68  IG_SCRATCH].  Th
3e750 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
3e760 20 69 73 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f   is in allocatio
3e770 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79  ns, not.** in by
3e780 74 65 73 2e 20 20 53 69 6e 63 65 20 61 20 73 69  tes.  Since a si
3e790 6e 67 6c 65 20 74 68 72 65 61 64 20 6d 61 79 20  ngle thread may 
3e7a0 6f 6e 6c 79 20 68 61 76 65 20 6f 6e 65 20 73 63  only have one sc
3e7b0 72 61 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e  ratch allocation
3e7c0 0a 2a 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  .** outstanding 
3e7d0 61 74 20 74 69 6d 65 2c 20 74 68 69 73 20 70 61  at time, this pa
3e7e0 72 61 6d 65 74 65 72 20 61 6c 73 6f 20 72 65 70  rameter also rep
3e7f0 6f 72 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20  orts the number 
3e800 6f 66 20 74 68 72 65 61 64 73 0a 2a 2a 20 75 73  of threads.** us
3e810 69 6e 67 20 73 63 72 61 74 63 68 20 6d 65 6d 6f  ing scratch memo
3e820 72 79 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ry at the same t
3e830 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ime.</dd>.**.** 
3e840 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55  <dt>SQLITE_STATU
3e850 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c  S_SCRATCH_OVERFL
3e860 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  OW</dt>.** <dd>T
3e870 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
3e880 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
3e890 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 63 72   of bytes of scr
3e8a0 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  atch memory.** a
3e8b0 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20  llocation which 
3e8c0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61  could not be sta
3e8d0 74 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b  tisfied by the [
3e8e0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43  SQLITE_CONFIG_SC
3e8f0 52 41 54 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72  RATCH].** buffer
3e900 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72 63 65   and where force
3e910 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f  d to overflow to
3e920 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
3e930 28 29 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 73  ()].  The values
3e940 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 63  .** returned inc
3e950 6c 75 64 65 20 6f 76 65 72 66 6c 6f 77 73 20 62  lude overflows b
3e960 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75 65  ecause the reque
3e970 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  sted allocation 
3e980 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65  was too.** large
3e990 72 20 28 74 68 61 74 20 69 73 2c 20 62 65 63 61  r (that is, beca
3e9a0 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74 65  use the requeste
3e9b0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73  d allocation was
3e9c0 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
3e9d0 0a 2a 2a 20 22 73 7a 22 20 70 61 72 61 6d 65 74  .** "sz" paramet
3e9e0 65 72 20 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f  er to [SQLITE_CO
3e9f0 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 29 20 61  NFIG_SCRATCH]) a
3ea00 6e 64 20 62 65 63 61 75 73 65 20 6e 6f 20 73 63  nd because no sc
3ea10 72 61 74 63 68 20 62 75 66 66 65 72 0a 2a 2a 20  ratch buffer.** 
3ea20 73 6c 6f 74 73 20 77 65 72 65 20 61 76 61 69 6c  slots were avail
3ea30 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a  able..** </dd>.*
3ea40 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
3ea50 53 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53  STATUS_SCRATCH_S
3ea60 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  IZE</dt>.** <dd>
3ea70 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
3ea80 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65  ecords the large
3ea90 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  st memory alloca
3eaa0 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20  tion request.** 
3eab0 68 61 6e 64 65 64 20 74 6f 20 5b 73 63 72 61 74  handed to [scrat
3eac0 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ch memory alloca
3ead0 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20  tor].  Only the 
3eae0 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
3eaf0 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77  n the.** *pHighw
3eb00 61 74 65 72 20 70 61 72 61 6d 65 74 65 72 20 74  ater parameter t
3eb10 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75  o [sqlite3_statu
3eb20 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72  s()] is of inter
3eb30 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61  est.  .** The va
3eb40 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  lue written into
3eb50 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70   the *pCurrent p
3eb60 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65  arameter is unde
3eb70 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  fined.</dd>.**.*
3eb80 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41  * <dt>SQLITE_STA
3eb90 54 55 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b  TUS_PARSER_STACK
3eba0 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
3ebb0 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f  s parameter reco
3ebc0 72 64 73 20 74 68 65 20 64 65 65 70 65 73 74 20  rds the deepest 
3ebd0 70 61 72 73 65 72 20 73 74 61 63 6b 2e 20 20 49  parser stack.  I
3ebe0 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61  t is only.** mea
3ebf0 6e 69 6e 67 66 75 6c 20 69 66 20 53 51 4c 69 74  ningful if SQLit
3ec00 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  e is compiled wi
3ec10 74 68 20 5b 59 59 54 52 41 43 4b 4d 41 58 53 54  th [YYTRACKMAXST
3ec20 41 43 4b 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a  ACKDEPTH].</dd>.
3ec30 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e  ** </dl>.**.** N
3ec40 65 77 20 73 74 61 74 75 73 20 70 61 72 61 6d 65  ew status parame
3ec50 74 65 72 73 20 6d 61 79 20 62 65 20 61 64 64 65  ters may be adde
3ec60 64 20 66 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74  d from time to t
3ec70 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ime..*/.#define 
3ec80 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45  SQLITE_STATUS_ME
3ec90 4d 4f 52 59 5f 55 53 45 44 20 20 20 20 20 20 20  MORY_USED       
3eca0 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
3ecb0 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
3ecc0 41 43 48 45 5f 55 53 45 44 20 20 20 20 20 20 20  ACHE_USED       
3ecd0 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
3ece0 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
3ecf0 45 5f 4f 56 45 52 46 4c 4f 57 20 20 20 32 0a 23  E_OVERFLOW   2.#
3ed00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
3ed10 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45  ATUS_SCRATCH_USE
3ed20 44 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66  D         3.#def
3ed30 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
3ed40 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c  S_SCRATCH_OVERFL
3ed50 4f 57 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65  OW     4.#define
3ed60 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d   SQLITE_STATUS_M
3ed70 41 4c 4c 4f 43 5f 53 49 5a 45 20 20 20 20 20 20  ALLOC_SIZE      
3ed80 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51      5.#define SQ
3ed90 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53  LITE_STATUS_PARS
3eda0 45 52 5f 53 54 41 43 4b 20 20 20 20 20 20 20 20  ER_STACK        
3edb0 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   6.#define SQLIT
3edc0 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43  E_STATUS_PAGECAC
3edd0 48 45 5f 53 49 5a 45 20 20 20 20 20 20 20 37 0a  HE_SIZE       7.
3ede0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
3edf0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49  TATUS_SCRATCH_SI
3ee00 5a 45 20 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a  ZE         8../*
3ee10 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61  .** CAPI3REF: Da
3ee20 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
3ee30 6e 20 53 74 61 74 75 73 20 7b 48 31 37 35 30 30  n Status {H17500
3ee40 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58  } <S60200>.** EX
3ee50 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
3ee60 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
3ee70 69 73 20 75 73 65 64 20 74 6f 20 72 65 74 72 69  is used to retri
3ee80 65 76 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74  eve runtime stat
3ee90 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a  us information .
3eea0 2a 2a 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c  ** about a singl
3eeb0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
3eec0 65 63 74 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69  ection].  The fi
3eed0 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
3eee0 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
3eef0 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63  connection objec
3ef00 74 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67  t to be interrog
3ef10 61 74 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e  ated.  The secon
3ef20 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73  d argument.** is
3ef30 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 74   the parameter t
3ef40 6f 20 69 6e 74 65 72 72 6f 67 61 74 65 2e 20 20  o interrogate.  
3ef50 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f  Currently, the o
3ef60 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75  nly allowed valu
3ef70 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73 65 63  e.** for the sec
3ef80 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73  ond parameter is
3ef90 20 5b 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55   [SQLITE_DBSTATU
3efa0 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44  S_LOOKASIDE_USED
3efb0 5d 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c  ]..** Additional
3efc0 20 6f 70 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69   options will li
3efd0 6b 65 6c 79 20 61 70 70 65 61 72 20 69 6e 20 66  kely appear in f
3efe0 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f  uture releases o
3eff0 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  f SQLite..**.** 
3f000 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  The current valu
3f010 65 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  e of the request
3f020 65 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  ed parameter is 
3f030 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43  written into *pC
3f040 75 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 68 69  ur.** and the hi
3f050 67 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65  ghest instantane
3f060 6f 75 73 20 76 61 6c 75 65 20 69 73 20 77 72 69  ous value is wri
3f070 74 74 65 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74  tten into *pHiwt
3f080 72 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 72 65  r.  If.** the re
3f090 73 65 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20  setFlg is true, 
3f0a0 74 68 65 6e 20 74 68 65 20 68 69 67 68 65 73 74  then the highest
3f0b0 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76   instantaneous v
3f0c0 61 6c 75 65 20 69 73 0a 2a 2a 20 72 65 73 65 74  alue is.** reset
3f0d0 20 62 61 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68   back down to th
3f0e0 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 2e  e current value.
3f0f0 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
3f100 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73   [sqlite3_status
3f110 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
3f120 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e  _stmt_status()].
3f130 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
3f140 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
3f150 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  AL int sqlite3_d
3f160 62 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33  b_status(sqlite3
3f170 2a 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a  *, int op, int *
3f180 70 43 75 72 2c 20 69 6e 74 20 2a 70 48 69 77 74  pCur, int *pHiwt
3f190 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 67 29  r, int resetFlg)
3f1a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3f1b0 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65  F: Status Parame
3f1c0 74 65 72 73 20 66 6f 72 20 64 61 74 61 62 61 73  ters for databas
3f1d0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48  e connections {H
3f1e0 31 37 35 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a  17520} <H17500>.
3f1f0 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
3f200 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73  **.** These cons
3f210 74 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76  tants are the av
3f220 61 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20  ailable integer 
3f230 22 76 65 72 62 73 22 20 74 68 61 74 20 63 61 6e  "verbs" that can
3f240 20 62 65 20 70 61 73 73 65 64 20 61 73 0a 2a 2a   be passed as.**
3f250 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
3f260 6d 65 6e 74 20 74 6f 20 74 68 65 20 5b 73 71 6c  ment to the [sql
3f270 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29  ite3_db_status()
3f280 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  ] interface..**.
3f290 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 6d 61 79  ** New verbs may
3f2a0 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
3f2b0 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20  ure releases of 
3f2c0 53 51 4c 69 74 65 2e 20 45 78 69 73 74 69 6e 67  SQLite. Existing
3f2d0 20 76 65 72 62 73 0a 2a 2a 20 6d 69 67 68 74 20   verbs.** might 
3f2e0 62 65 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 2e  be discontinued.
3f2f0 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68   Applications sh
3f300 6f 75 6c 64 20 63 68 65 63 6b 20 74 68 65 20 72  ould check the r
3f310 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 0a  eturn code from.
3f320 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73  ** [sqlite3_db_s
3f330 74 61 74 75 73 28 29 5d 20 74 6f 20 6d 61 6b 65  tatus()] to make
3f340 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 63   sure that the c
3f350 61 6c 6c 20 77 6f 72 6b 65 64 2e 0a 2a 2a 20 54  all worked..** T
3f360 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73  he [sqlite3_db_s
3f370 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61  tatus()] interfa
3f380 63 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 61  ce will return a
3f390 20 6e 6f 6e 2d 7a 65 72 6f 20 65 72 72 6f 72 20   non-zero error 
3f3a0 63 6f 64 65 0a 2a 2a 20 69 66 20 61 20 64 69 73  code.** if a dis
3f3b0 63 6f 6e 74 69 6e 75 65 64 20 6f 72 20 75 6e 73  continued or uns
3f3c0 75 70 70 6f 72 74 65 64 20 76 65 72 62 20 69 73  upported verb is
3f3d0 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20   invoked..**.** 
3f3e0 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  <dl>.** <dt>SQLI
3f3f0 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b  TE_DBSTATUS_LOOK
3f400 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 74 3e 0a  ASIDE_USED</dt>.
3f410 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61  ** <dd>This para
3f420 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20 74 68  meter returns th
3f430 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 6f 6b  e number of look
3f440 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 73 6c 6f  aside memory slo
3f450 74 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  ts currently.** 
3f460 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f 64 64  checked out.</dd
3f470 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64  >.** </dl>.*/.#d
3f480 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 53  efine SQLITE_DBS
3f490 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f  TATUS_LOOKASIDE_
3f4a0 55 53 45 44 20 20 20 20 20 30 0a 0a 0a 2f 2a 0a  USED     0.../*.
3f4b0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50 72 65  ** CAPI3REF: Pre
3f4c0 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20  pared Statement 
3f4d0 53 74 61 74 75 73 20 7b 48 31 37 35 35 30 7d 20  Status {H17550} 
3f4e0 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45  <S60200>.** EXPE
3f4f0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45  RIMENTAL.**.** E
3f500 61 63 68 20 70 72 65 70 61 72 65 64 20 73 74 61  ach prepared sta
3f510 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 69 6e 73  tement maintains
3f520 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b 53 51 4c   various.** [SQL
3f530 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53  ITE_STMTSTATUS_S
3f540 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 73 5d 20  ORT | counters] 
3f550 74 68 61 74 20 6d 65 61 73 75 72 65 20 74 68 65  that measure the
3f560 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74 69   number.** of ti
3f570 6d 65 73 20 69 74 20 68 61 73 20 70 65 72 66 6f  mes it has perfo
3f580 72 6d 65 64 20 73 70 65 63 69 66 69 63 20 6f 70  rmed specific op
3f590 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 65 73 65  erations.  These
3f5a0 20 63 6f 75 6e 74 65 72 73 20 63 61 6e 0a 2a 2a   counters can.**
3f5b0 20 62 65 20 75 73 65 64 20 74 6f 20 6d 6f 6e 69   be used to moni
3f5c0 74 6f 72 20 74 68 65 20 70 65 72 66 6f 72 6d 61  tor the performa
3f5d0 6e 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74  nce characterist
3f5e0 69 63 73 20 6f 66 20 74 68 65 20 70 72 65 70 61  ics of the prepa
3f5f0 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  red.** statement
3f600 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  s.  For example,
3f610 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   if the number o
3f620 66 20 74 61 62 6c 65 20 73 74 65 70 73 20 67 72  f table steps gr
3f630 65 61 74 6c 79 20 65 78 63 65 65 64 73 0a 2a 2a  eatly exceeds.**
3f640 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
3f650 61 62 6c 65 20 73 65 61 72 63 68 65 73 20 6f 72  able searches or
3f660 20 72 65 73 75 6c 74 20 72 6f 77 73 2c 20 74 68   result rows, th
3f670 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 20 74 6f  at would tend to
3f680 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68 61   indicate.** tha
3f690 74 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  t the prepared s
3f6a0 74 61 74 65 6d 65 6e 74 20 69 73 20 75 73 69 6e  tatement is usin
3f6b0 67 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73  g a full table s
3f6c0 63 61 6e 20 72 61 74 68 65 72 20 74 68 61 6e 0a  can rather than.
3f6d0 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20 0a 2a  ** an index.  .*
3f6e0 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
3f6f0 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72  ace is used to r
3f700 65 74 72 69 65 76 65 20 61 6e 64 20 72 65 73 65  etrieve and rese
3f710 74 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73  t counter values
3f720 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 72 65 70   from.** a [prep
3f730 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e  ared statement].
3f740 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75    The first argu
3f750 6d 65 6e 74 20 69 73 20 74 68 65 20 70 72 65 70  ment is the prep
3f760 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ared statement.*
3f770 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 69  * object to be i
3f780 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20 54 68  nterrogated.  Th
3f790 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
3f7a0 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74 65 67  t.** is an integ
3f7b0 65 72 20 63 6f 64 65 20 66 6f 72 20 61 20 73 70  er code for a sp
3f7c0 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45 5f 53  ecific [SQLITE_S
3f7d0 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 7c  TMTSTATUS_SORT |
3f7e0 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74 6f 20   counter].** to 
3f7f0 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e  be interrogated.
3f800 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74   .** The current
3f810 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65   value of the re
3f820 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65 72 20  quested counter 
3f830 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
3f840 49 66 20 74 68 65 20 72 65 73 65 74 46 6c 67 20  If the resetFlg 
3f850 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
3f860 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72 65 73  e counter is res
3f870 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72  et to zero after
3f880 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72 66 61   this.** interfa
3f890 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e  ce call returns.
3f8a0 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
3f8b0 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73   [sqlite3_status
3f8c0 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
3f8d0 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a  _db_status()]..*
3f8e0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
3f8f0 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
3f900 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 6d   int sqlite3_stm
3f910 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74 65 33  t_status(sqlite3
3f920 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f 70 2c 69  _stmt*, int op,i
3f930 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a 0a 2f  nt resetFlg);../
3f940 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53  *.** CAPI3REF: S
3f950 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73  tatus Parameters
3f960 20 66 6f 72 20 70 72 65 70 61 72 65 64 20 73 74   for prepared st
3f970 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 35 37 30  atements {H17570
3f980 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 20 45 58  } <H17550>.** EX
3f990 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
3f9a0 20 54 68 65 73 65 20 70 72 65 70 72 6f 63 65 73   These preproces
3f9b0 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e  sor macros defin
3f9c0 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73 20  e integer codes 
3f9d0 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e 74 65  that name counte
3f9e0 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73 73 6f  r.** values asso
3f9f0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
3fa00 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74  [sqlite3_stmt_st
3fa10 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63  atus()] interfac
3fa20 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69 6e  e..** The meanin
3fa30 67 73 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75  gs of the variou
3fa40 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65 20 61  s counters are a
3fa50 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
3fa60 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c   <dl>.** <dt>SQL
3fa70 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 46  ITE_STMTSTATUS_F
3fa80 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c 2f 64 74  ULLSCAN_STEP</dt
3fa90 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 69 73  >.** <dd>This is
3faa0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
3fab0 69 6d 65 73 20 74 68 61 74 20 53 51 4c 69 74 65  imes that SQLite
3fac0 20 68 61 73 20 73 74 65 70 70 65 64 20 66 6f 72   has stepped for
3fad0 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 74 61 62  ward in.** a tab
3fae0 6c 65 20 61 73 20 70 61 72 74 20 6f 66 20 61 20  le as part of a 
3faf0 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e  full table scan.
3fb00 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 72 73 20    Large numbers 
3fb10 66 6f 72 20 74 68 69 73 20 63 6f 75 6e 74 65 72  for this counter
3fb20 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 61 74 65  .** may indicate
3fb30 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 73 20 66   opportunities f
3fb40 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69  or performance i
3fb50 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 72 6f 75  mprovement throu
3fb60 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 6c 20 75  gh .** careful u
3fb70 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f  se of indices.</
3fb80 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
3fb90 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f  LITE_STMTSTATUS_
3fba0 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  SORT</dt>.** <dd
3fbb0 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e 75 6d  >This is the num
3fbc0 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70 65 72  ber of sort oper
3fbd0 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65  ations that have
3fbe0 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 41 20   occurred..** A 
3fbf0 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 69  non-zero value i
3fc00 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 6d  n this counter m
3fc10 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e 20 6f  ay indicate an o
3fc20 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a 2a 2a  pportunity to.**
3fc30 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70 65 72   improvement per
3fc40 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75 67 68  formance through
3fc50 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f 66 20   careful use of 
3fc60 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a 2a 2a  indices.</dd>.**
3fc70 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65  .** </dl>.*/.#de
3fc80 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54  fine SQLITE_STMT
3fc90 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f  STATUS_FULLSCAN_
3fca0 53 54 45 50 20 20 20 20 20 31 0a 23 64 65 66 69  STEP     1.#defi
3fcb0 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54 53 54  ne SQLITE_STMTST
3fcc0 41 54 55 53 5f 53 4f 52 54 20 20 20 20 20 20 20  ATUS_SORT       
3fcd0 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20         2../*.** 
3fce0 43 41 50 49 33 52 45 46 3a 20 43 75 73 74 6f 6d  CAPI3REF: Custom
3fcf0 20 50 61 67 65 20 43 61 63 68 65 20 4f 62 6a 65   Page Cache Obje
3fd00 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  ct.** EXPERIMENT
3fd10 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  AL.**.** The sql
3fd20 69 74 65 33 5f 70 63 61 63 68 65 20 74 79 70 65  ite3_pcache type
3fd30 20 69 73 20 6f 70 61 71 75 65 2e 20 20 49 74 20   is opaque.  It 
3fd40 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62  is implemented b
3fd50 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67 67 61 62  y.** the pluggab
3fd60 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20  le module.  The 
3fd70 53 51 4c 69 74 65 20 63 6f 72 65 20 68 61 73 20  SQLite core has 
3fd80 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20 6f 66 0a  no knowledge of.
3fd90 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f 72 20 69  ** its size or i
3fda0 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75 72  nternal structur
3fdb0 65 20 61 6e 64 20 6e 65 76 65 72 20 64 65 61 6c  e and never deal
3fdc0 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 71  s with the.** sq
3fdd0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 6f 62 6a  lite3_pcache obj
3fde0 65 63 74 20 65 78 63 65 70 74 20 62 79 20 68 6f  ect except by ho
3fdf0 6c 64 69 6e 67 20 61 6e 64 20 70 61 73 73 69 6e  lding and passin
3fe00 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20 74 6f  g pointers.** to
3fe10 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a   the object..**.
3fe20 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74 65 33 5f  ** See [sqlite3_
3fe30 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20  pcache_methods] 
3fe40 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
3fe50 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74  nformation..*/.t
3fe60 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
3fe70 6c 69 74 65 33 5f 70 63 61 63 68 65 20 73 71 6c  lite3_pcache sql
3fe80 69 74 65 33 5f 70 63 61 63 68 65 3b 0a 0a 2f 2a  ite3_pcache;../*
3fe90 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 70  .** CAPI3REF: Ap
3fea0 70 6c 69 63 61 74 69 6f 6e 20 44 65 66 69 6e 65  plication Define
3feb0 64 20 50 61 67 65 20 43 61 63 68 65 2e 0a 2a 2a  d Page Cache..**
3fec0 20 4b 45 59 57 4f 52 44 53 3a 20 7b 70 61 67 65   KEYWORDS: {page
3fed0 20 63 61 63 68 65 7d 0a 2a 2a 20 45 58 50 45 52   cache}.** EXPER
3fee0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68  IMENTAL.**.** Th
3fef0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  e [sqlite3_confi
3ff00 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  g]([SQLITE_CONFI
3ff10 47 5f 50 43 41 43 48 45 5d 2c 20 2e 2e 2e 29 20  G_PCACHE], ...) 
3ff20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 0a 2a 2a  interface can.**
3ff30 20 72 65 67 69 73 74 65 72 20 61 6e 20 61 6c 74   register an alt
3ff40 65 72 6e 61 74 69 76 65 20 70 61 67 65 20 63 61  ernative page ca
3ff50 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
3ff60 6f 6e 20 62 79 20 70 61 73 73 69 6e 67 20 69 6e  on by passing in
3ff70 20 61 6e 20 0a 2a 2a 20 69 6e 73 74 61 6e 63 65   an .** instance
3ff80 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
3ff90 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73  pcache_methods s
3ffa0 74 72 75 63 74 75 72 65 2e 20 54 68 65 20 6d 61  tructure. The ma
3ffb0 6a 6f 72 69 74 79 20 6f 66 20 74 68 65 20 0a 2a  jority of the .*
3ffc0 2a 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 75 73  * heap memory us
3ffd0 65 64 20 62 79 20 53 51 4c 69 74 65 20 69 73 20  ed by SQLite is 
3ffe0 75 73 65 64 20 62 79 20 74 68 65 20 70 61 67 65  used by the page
3fff0 20 63 61 63 68 65 20 74 6f 20 63 61 63 68 65 20   cache to cache 
40000 64 61 74 61 20 72 65 61 64 20 0a 2a 2a 20 66 72  data read .** fr
40010 6f 6d 2c 20 6f 72 20 72 65 61 64 79 20 74 6f 20  om, or ready to 
40020 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 74  be written to, t
40030 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
40040 2e 20 42 79 20 69 6d 70 6c 65 6d 65 6e 74 69 6e  . By implementin
40050 67 20 61 20 0a 2a 2a 20 63 75 73 74 6f 6d 20 70  g a .** custom p
40060 61 67 65 20 63 61 63 68 65 20 75 73 69 6e 67 20  age cache using 
40070 74 68 69 73 20 41 50 49 2c 20 61 6e 20 61 70 70  this API, an app
40080 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 63 6f 6e  lication can con
40090 74 72 6f 6c 20 6d 6f 72 65 20 0a 2a 2a 20 70 72  trol more .** pr
400a0 65 63 69 73 65 6c 79 20 74 68 65 20 61 6d 6f 75  ecisely the amou
400b0 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 6f 6e  nt of memory con
400c0 73 75 6d 65 64 20 62 79 20 53 51 4c 69 74 65 2c  sumed by SQLite,
400d0 20 74 68 65 20 77 61 79 20 69 6e 20 77 68 69 63   the way in whic
400e0 68 20 0a 2a 2a 20 74 68 61 74 20 6d 65 6d 6f 72  h .** that memor
400f0 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61  y is allocated a
40100 6e 64 20 72 65 6c 65 61 73 65 64 2c 20 61 6e 64  nd released, and
40110 20 74 68 65 20 70 6f 6c 69 63 69 65 73 20 75 73   the policies us
40120 65 64 20 74 6f 20 0a 2a 2a 20 64 65 74 65 72 6d  ed to .** determ
40130 69 6e 65 20 65 78 61 63 74 6c 79 20 77 68 69 63  ine exactly whic
40140 68 20 70 61 72 74 73 20 6f 66 20 61 20 64 61 74  h parts of a dat
40150 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 63  abase file are c
40160 61 63 68 65 64 20 61 6e 64 20 66 6f 72 20 0a 2a  ached and for .*
40170 2a 20 68 6f 77 20 6c 6f 6e 67 2e 0a 2a 2a 0a 2a  * how long..**.*
40180 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  * The contents o
40190 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63  f the sqlite3_pc
401a0 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 74 72  ache_methods str
401b0 75 63 74 75 72 65 20 61 72 65 20 63 6f 70 69 65  ucture are copie
401c0 64 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 74 65 72  d to an.** inter
401d0 6e 61 6c 20 62 75 66 66 65 72 20 62 79 20 53 51  nal buffer by SQ
401e0 4c 69 74 65 20 77 69 74 68 69 6e 20 74 68 65 20  Lite within the 
401f0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
40200 5f 63 6f 6e 66 69 67 5d 2e 20 20 48 65 6e 63 65  _config].  Hence
40210 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74  .** the applicat
40220 69 6f 6e 20 6d 61 79 20 64 69 73 63 61 72 64 20  ion may discard 
40230 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 61 66  the parameter af
40240 74 65 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 0a  ter the call to.
40250 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ** [sqlite3_conf
40260 69 67 28 29 5d 20 72 65 74 75 72 6e 73 2e 0a 2a  ig()] returns..*
40270 2a 0a 2a 2a 20 54 68 65 20 78 49 6e 69 74 28 29  *.** The xInit()
40280 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c 6c 65   method is calle
40290 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  d once for each 
402a0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
402b0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 0a 2a  _initialize()].*
402c0 2a 20 28 75 73 75 61 6c 6c 79 20 6f 6e 6c 79 20  * (usually only 
402d0 6f 6e 63 65 20 64 75 72 69 6e 67 20 74 68 65 20  once during the 
402e0 6c 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20  lifetime of the 
402f0 70 72 6f 63 65 73 73 29 2e 20 49 74 20 69 73 20  process). It is 
40300 70 61 73 73 65 64 0a 2a 2a 20 61 20 63 6f 70 79  passed.** a copy
40310 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
40320 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 2e 70  pcache_methods.p
40330 41 72 67 20 76 61 6c 75 65 2e 20 49 74 20 63 61  Arg value. It ca
40340 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 74  n be used to set
40350 0a 2a 2a 20 75 70 20 67 6c 6f 62 61 6c 20 73 74  .** up global st
40360 72 75 63 74 75 72 65 73 20 61 6e 64 20 6d 75 74  ructures and mut
40370 65 78 65 73 20 72 65 71 75 69 72 65 64 20 62 79  exes required by
40380 20 74 68 65 20 63 75 73 74 6f 6d 20 70 61 67 65   the custom page
40390 20 63 61 63 68 65 20 0a 2a 2a 20 69 6d 70 6c 65   cache .** imple
403a0 6d 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a  mentation. .**.*
403b0 2a 20 54 68 65 20 78 53 68 75 74 64 6f 77 6e 28  * The xShutdown(
403c0 29 20 6d 65 74 68 6f 64 20 69 73 20 63 61 6c 6c  ) method is call
403d0 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 5b  ed from within [
403e0 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
403f0 28 29 5d 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20  ()], .** if the 
40400 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f  application invo
40410 6b 65 73 20 74 68 69 73 20 41 50 49 2e 20 49 74  kes this API. It
40420 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
40430 63 6c 65 61 6e 20 75 70 20 0a 2a 2a 20 61 6e 79  clean up .** any
40440 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65 73   outstanding res
40450 6f 75 72 63 65 73 20 62 65 66 6f 72 65 20 70 72  ources before pr
40460 6f 63 65 73 73 20 73 68 75 74 64 6f 77 6e 2c 20  ocess shutdown, 
40470 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a  if required..**.
40480 2a 2a 20 53 51 4c 69 74 65 20 68 6f 6c 64 73 20  ** SQLite holds 
40490 61 20 5b 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  a [SQLITE_MUTEX_
404a0 52 45 43 55 52 53 49 56 45 5d 20 6d 75 74 65 78  RECURSIVE] mutex
404b0 20 77 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73   when it invokes
404c0 0a 2a 2a 20 74 68 65 20 78 49 6e 69 74 20 6d 65  .** the xInit me
404d0 74 68 6f 64 2c 20 73 6f 20 74 68 65 20 78 49 6e  thod, so the xIn
404e0 69 74 20 6d 65 74 68 6f 64 20 6e 65 65 64 20 6e  it method need n
404f0 6f 74 20 62 65 20 74 68 72 65 61 64 73 61 66 65  ot be threadsafe
40500 2e 20 20 54 68 65 0a 2a 2a 20 78 53 68 75 74 64  .  The.** xShutd
40510 6f 77 6e 20 6d 65 74 68 6f 64 20 69 73 20 6f 6e  own method is on
40520 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 5b  ly called from [
40530 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
40540 28 29 5d 20 73 6f 20 69 74 20 64 6f 65 73 0a 2a  ()] so it does.*
40550 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  * not need to be
40560 20 74 68 72 65 61 64 73 61 66 65 20 65 69 74 68   threadsafe eith
40570 65 72 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 6d  er.  All other m
40580 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65 20 74  ethods must be t
40590 68 72 65 61 64 73 61 66 65 0a 2a 2a 20 69 6e 20  hreadsafe.** in 
405a0 6d 75 6c 74 69 74 68 72 65 61 64 65 64 20 61 70  multithreaded ap
405b0 70 6c 69 63 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  plications..**.*
405c0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65  * SQLite will ne
405d0 76 65 72 20 69 6e 76 6f 6b 65 20 78 49 6e 69 74  ver invoke xInit
405e0 28 29 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  () more than onc
405f0 65 20 77 69 74 68 6f 75 74 20 61 6e 20 69 6e 74  e without an int
40600 65 72 76 65 6e 69 6e 67 0a 2a 2a 20 63 61 6c 6c  ervening.** call
40610 20 74 6f 20 78 53 68 75 74 64 6f 77 6e 28 29 2e   to xShutdown().
40620 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61  .**.** The xCrea
40630 74 65 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75  te() method is u
40640 73 65 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74  sed to construct
40650 20 61 20 6e 65 77 20 63 61 63 68 65 20 69 6e 73   a new cache ins
40660 74 61 6e 63 65 2e 20 20 53 51 4c 69 74 65 0a 2a  tance.  SQLite.*
40670 2a 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79  * will typically
40680 20 63 72 65 61 74 65 20 6f 6e 65 20 63 61 63 68   create one cach
40690 65 20 69 6e 73 74 61 6e 63 65 20 66 6f 72 20 65  e instance for e
406a0 61 63 68 20 6f 70 65 6e 20 64 61 74 61 62 61 73  ach open databas
406b0 65 20 66 69 6c 65 2c 0a 2a 2a 20 74 68 6f 75 67  e file,.** thoug
406c0 68 20 74 68 69 73 20 69 73 20 6e 6f 74 20 67 75  h this is not gu
406d0 61 72 61 6e 74 65 65 64 2e 20 54 68 65 0a 2a 2a  aranteed. The.**
406e0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
406f0 2c 20 73 7a 50 61 67 65 2c 20 69 73 20 74 68 65  , szPage, is the
40700 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f   size in bytes o
40710 66 20 74 68 65 20 70 61 67 65 73 20 74 68 61 74  f the pages that
40720 20 6d 75 73 74 0a 2a 2a 20 62 65 20 61 6c 6c 6f   must.** be allo
40730 63 61 74 65 64 20 62 79 20 74 68 65 20 63 61 63  cated by the cac
40740 68 65 2e 20 20 73 7a 50 61 67 65 20 77 69 6c 6c  he.  szPage will
40750 20 6e 6f 74 20 62 65 20 61 20 70 6f 77 65 72 20   not be a power 
40760 6f 66 20 74 77 6f 2e 20 20 73 7a 50 61 67 65 0a  of two.  szPage.
40770 2a 2a 20 77 69 6c 6c 20 74 68 65 20 70 61 67 65  ** will the page
40780 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
40790 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20  abase file that 
407a0 69 73 20 74 6f 20 62 65 20 63 61 63 68 65 64 20  is to be cached 
407b0 70 6c 75 73 20 61 6e 0a 2a 2a 20 69 6e 63 72 65  plus an.** incre
407c0 6d 65 6e 74 20 28 68 65 72 65 20 63 61 6c 6c 65  ment (here calle
407d0 64 20 22 52 22 29 20 6f 66 20 61 62 6f 75 74 20  d "R") of about 
407e0 31 30 30 20 6f 72 20 32 30 30 2e 20 20 53 51 4c  100 or 200.  SQL
407f0 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65  ite will use the
40800 0a 2a 2a 20 65 78 74 72 61 20 52 20 62 79 74 65  .** extra R byte
40810 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 74  s on each page t
40820 6f 20 73 74 6f 72 65 20 6d 65 74 61 64 61 74 61  o store metadata
40830 20 61 62 6f 75 74 20 74 68 65 20 75 6e 64 65 72   about the under
40840 6c 79 69 6e 67 0a 2a 2a 20 64 61 74 61 62 61 73  lying.** databas
40850 65 20 70 61 67 65 20 6f 6e 20 64 69 73 6b 2e 20  e page on disk. 
40860 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 52 20   The value of R 
40870 64 65 70 65 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68  depends.** on th
40880 65 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e  e SQLite version
40890 2c 20 74 68 65 20 74 61 72 67 65 74 20 70 6c 61  , the target pla
408a0 74 66 6f 72 6d 2c 20 61 6e 64 20 68 6f 77 20 53  tform, and how S
408b0 51 4c 69 74 65 20 77 61 73 20 63 6f 6d 70 69 6c  QLite was compil
408c0 65 64 2e 0a 2a 2a 20 52 20 69 73 20 63 6f 6e 73  ed..** R is cons
408d0 74 61 6e 74 20 66 6f 72 20 61 20 70 61 72 74 69  tant for a parti
408e0 63 75 6c 61 72 20 62 75 69 6c 64 20 6f 66 20 53  cular build of S
408f0 51 4c 69 74 65 2e 20 20 54 68 65 20 73 65 63 6f  QLite.  The seco
40900 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a  nd argument to.*
40910 2a 20 78 43 72 65 61 74 65 28 29 2c 20 62 50 75  * xCreate(), bPu
40920 72 67 65 61 62 6c 65 2c 20 69 73 20 74 72 75 65  rgeable, is true
40930 20 69 66 20 74 68 65 20 63 61 63 68 65 20 62 65   if the cache be
40940 69 6e 67 20 63 72 65 61 74 65 64 20 77 69 6c 6c  ing created will
40950 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 63  .** be used to c
40960 61 63 68 65 20 64 61 74 61 62 61 73 65 20 70 61  ache database pa
40970 67 65 73 20 6f 66 20 61 20 66 69 6c 65 20 73 74  ges of a file st
40980 6f 72 65 64 20 6f 6e 20 64 69 73 6b 2c 20 6f 72  ored on disk, or
40990 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74 20  .** false if it 
409a0 69 73 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69  is used for an i
409b0 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
409c0 65 2e 20 54 68 65 20 63 61 63 68 65 20 69 6d 70  e. The cache imp
409d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 64  lementation.** d
409e0 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20  oes not have to 
409f0 64 6f 20 61 6e 79 74 68 69 6e 67 20 73 70 65 63  do anything spec
40a00 69 61 6c 20 62 61 73 65 64 20 77 69 74 68 20 74  ial based with t
40a10 68 65 20 76 61 6c 75 65 20 6f 66 20 62 50 75 72  he value of bPur
40a20 67 65 61 62 6c 65 3b 0a 2a 2a 20 69 74 20 69 73  geable;.** it is
40a30 20 70 75 72 65 6c 79 20 61 64 76 69 73 6f 72 79   purely advisory
40a40 2e 20 20 4f 6e 20 61 20 63 61 63 68 65 20 77 68  .  On a cache wh
40a50 65 72 65 20 62 50 75 72 67 65 61 62 6c 65 20 69  ere bPurgeable i
40a60 73 20 66 61 6c 73 65 2c 20 53 51 4c 69 74 65 20  s false, SQLite 
40a70 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 69 6e  will.** never in
40a80 76 6f 6b 65 20 78 55 6e 70 69 6e 28 29 20 65 78  voke xUnpin() ex
40a90 63 65 70 74 20 74 6f 20 64 65 6c 69 62 65 72 61  cept to delibera
40aa0 74 65 6c 79 20 64 65 6c 65 74 65 20 61 20 70 61  tely delete a pa
40ab0 67 65 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20  ge..** In other 
40ac0 77 6f 72 64 73 2c 20 61 20 63 61 63 68 65 20 63  words, a cache c
40ad0 72 65 61 74 65 64 20 77 69 74 68 20 62 50 75 72  reated with bPur
40ae0 67 65 61 62 6c 65 20 73 65 74 20 74 6f 20 66 61  geable set to fa
40af0 6c 73 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65  lse will.** neve
40b00 72 20 63 6f 6e 74 61 69 6e 20 61 6e 79 20 75 6e  r contain any un
40b10 70 69 6e 6e 65 64 20 70 61 67 65 73 2e 0a 2a 2a  pinned pages..**
40b20 0a 2a 2a 20 54 68 65 20 78 43 61 63 68 65 73 69  .** The xCachesi
40b30 7a 65 28 29 20 6d 65 74 68 6f 64 20 6d 61 79 20  ze() method may 
40b40 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61 6e 79  be called at any
40b50 20 74 69 6d 65 20 62 79 20 53 51 4c 69 74 65 20   time by SQLite 
40b60 74 6f 20 73 65 74 20 74 68 65 0a 2a 2a 20 73 75  to set the.** su
40b70 67 67 65 73 74 65 64 20 6d 61 78 69 6d 75 6d 20  ggested maximum 
40b80 63 61 63 68 65 2d 73 69 7a 65 20 28 6e 75 6d 62  cache-size (numb
40b90 65 72 20 6f 66 20 70 61 67 65 73 20 73 74 6f 72  er of pages stor
40ba0 65 64 20 62 79 29 20 74 68 65 20 63 61 63 68 65  ed by) the cache
40bb0 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 70 61 73  .** instance pas
40bc0 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
40bd0 20 61 72 67 75 6d 65 6e 74 2e 20 54 68 69 73 20   argument. This 
40be0 69 73 20 74 68 65 20 76 61 6c 75 65 20 63 6f 6e  is the value con
40bf0 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a 2a  figured using.**
40c00 20 74 68 65 20 53 51 4c 69 74 65 20 22 5b 50 52   the SQLite "[PR
40c10 41 47 4d 41 20 63 61 63 68 65 5f 73 69 7a 65 5d  AGMA cache_size]
40c20 22 20 63 6f 6d 6d 61 6e 64 2e 20 41 73 20 77 69  " command. As wi
40c30 74 68 20 74 68 65 20 62 50 75 72 67 65 61 62 6c  th the bPurgeabl
40c40 65 20 70 61 72 61 6d 65 74 65 72 2c 0a 2a 2a 20  e parameter,.** 
40c50 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
40c60 6f 6e 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  on is not requir
40c70 65 64 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e  ed to do anythin
40c80 67 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20 76  g with this.** v
40c90 61 6c 75 65 3b 20 69 74 20 69 73 20 61 64 76 69  alue; it is advi
40ca0 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a  sory only..**.**
40cb0 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e 74 28   The xPagecount(
40cc0 29 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20  ) method should 
40cd0 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  return the numbe
40ce0 72 20 6f 66 20 70 61 67 65 73 20 63 75 72 72 65  r of pages curre
40cf0 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64 20 69  ntly.** stored i
40d00 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2a 20  n the cache..** 
40d10 0a 2a 2a 20 54 68 65 20 78 46 65 74 63 68 28 29  .** The xFetch()
40d20 20 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20   method is used 
40d30 74 6f 20 66 65 74 63 68 20 61 20 70 61 67 65 20  to fetch a page 
40d40 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69  and return a poi
40d50 6e 74 65 72 20 74 6f 20 69 74 2e 20 0a 2a 2a 20  nter to it. .** 
40d60 41 20 27 70 61 67 65 27 2c 20 69 6e 20 74 68 69  A 'page', in thi
40d70 73 20 63 6f 6e 74 65 78 74 2c 20 69 73 20 61 20  s context, is a 
40d80 62 75 66 66 65 72 20 6f 66 20 73 7a 50 61 67 65  buffer of szPage
40d90 20 62 79 74 65 73 20 61 6c 69 67 6e 65 64 20 61   bytes aligned a
40da0 74 20 61 6e 0a 2a 2a 20 38 2d 62 79 74 65 20 62  t an.** 8-byte b
40db0 6f 75 6e 64 61 72 79 2e 20 54 68 65 20 70 61 67  oundary. The pag
40dc0 65 20 74 6f 20 62 65 20 66 65 74 63 68 65 64 20  e to be fetched 
40dd0 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
40de0 20 74 68 65 20 6b 65 79 2e 20 54 68 65 0a 2a 2a   the key. The.**
40df0 20 6d 69 6d 69 6d 75 6d 20 6b 65 79 20 76 61 6c   mimimum key val
40e00 75 65 20 69 73 20 31 2e 20 41 66 74 65 72 20 69  ue is 1. After i
40e10 74 20 68 61 73 20 62 65 65 6e 20 72 65 74 72 69  t has been retri
40e20 65 76 65 64 20 75 73 69 6e 67 20 78 46 65 74 63  eved using xFetc
40e30 68 2c 20 74 68 65 20 70 61 67 65 20 0a 2a 2a 20  h, the page .** 
40e40 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f  is considered to
40e50 20 62 65 20 22 70 69 6e 6e 65 64 22 2e 0a 2a 2a   be "pinned"..**
40e60 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71 75 65  .** If the reque
40e70 73 74 65 64 20 70 61 67 65 20 69 73 20 61 6c 72  sted page is alr
40e80 65 61 64 79 20 69 6e 20 74 68 65 20 70 61 67 65  eady in the page
40e90 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65   cache, then the
40ea0 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 69   page cache.** i
40eb0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75  mplementation mu
40ec0 73 74 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  st return a poin
40ed0 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20  ter to the page 
40ee0 62 75 66 66 65 72 20 77 69 74 68 20 69 74 73 20  buffer with its 
40ef0 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 6e 74 61 63  content.** intac
40f00 74 2e 20 20 49 66 20 74 68 65 20 72 65 71 75 65  t.  If the reque
40f10 73 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74  sted page is not
40f20 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
40f30 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65 0a  cache, then the.
40f40 2a 2a 20 62 65 68 61 76 69 6f 72 20 6f 66 20 74  ** behavior of t
40f50 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65  he cache impleme
40f60 6e 74 61 74 69 6f 6e 20 69 73 20 64 65 74 65 72  ntation is deter
40f70 6d 69 6e 65 64 20 62 79 20 74 68 65 20 76 61 6c  mined by the val
40f80 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 72 65  ue of the.** cre
40f90 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65  ateFlag paramete
40fa0 72 20 70 61 73 73 65 64 20 74 6f 20 78 46 65 74  r passed to xFet
40fb0 63 68 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  ch, according to
40fc0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
40fd0 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c 74 61 62  able:.**.** <tab
40fe0 6c 65 20 62 6f 72 64 65 72 3d 31 20 77 69 64 74  le border=1 widt
40ff0 68 3d 38 35 25 20 61 6c 69 67 6e 3d 63 65 6e 74  h=85% align=cent
41000 65 72 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20  er>.** <tr><th> 
41010 63 72 65 61 74 65 46 6c 61 67 20 3c 74 68 3e 20  createFlag <th> 
41020 42 65 68 61 76 69 6f 75 72 20 77 68 65 6e 20 70  Behaviour when p
41030 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  age is not alrea
41040 64 79 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20 3c  dy in cache.** <
41050 74 72 3e 3c 74 64 3e 20 30 20 3c 74 64 3e 20 44  tr><td> 0 <td> D
41060 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 20 61  o not allocate a
41070 20 6e 65 77 20 70 61 67 65 2e 20 20 52 65 74 75   new page.  Retu
41080 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20 3c 74 72 3e  rn NULL..** <tr>
41090 3c 74 64 3e 20 31 20 3c 74 64 3e 20 41 6c 6c 6f  <td> 1 <td> Allo
410a0 63 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20  cate a new page 
410b0 69 66 20 69 74 20 65 61 73 79 20 61 6e 64 20 63  if it easy and c
410c0 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64 6f 20  onvenient to do 
410d0 73 6f 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  so..**          
410e0 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 73 65         Otherwise
410f0 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a   return NULL..**
41100 20 3c 74 72 3e 3c 74 64 3e 20 32 20 3c 74 64 3e   <tr><td> 2 <td>
41110 20 4d 61 6b 65 20 65 76 65 72 79 20 65 66 66 6f   Make every effo
41120 72 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  rt to allocate a
41130 20 6e 65 77 20 70 61 67 65 2e 20 20 4f 6e 6c 79   new page.  Only
41140 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20   return.**      
41150 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 20             NULL 
41160 69 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20  if allocating a 
41170 6e 65 77 20 70 61 67 65 20 69 73 20 65 66 66 65  new page is effe
41180 63 74 69 76 65 6c 79 20 69 6d 70 6f 73 73 69 62  ctively impossib
41190 6c 65 2e 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a  le..** </table>.
411a0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  **.** SQLite wil
411b0 6c 20 6e 6f 72 6d 61 6c 6c 79 20 69 6e 76 6f 6b  l normally invok
411c0 65 20 78 46 65 74 63 68 28 29 20 77 69 74 68 20  e xFetch() with 
411d0 61 20 63 72 65 61 74 65 46 6c 61 67 20 6f 66 20  a createFlag of 
411e0 30 20 6f 72 20 31 2e 20 20 49 66 0a 2a 2a 20 61  0 or 1.  If.** a
411f0 20 63 61 6c 6c 20 74 6f 20 78 46 65 74 63 68 28   call to xFetch(
41200 29 20 77 69 74 68 20 63 72 65 61 74 65 46 6c 61  ) with createFla
41210 67 3d 3d 31 20 72 65 74 75 72 6e 73 20 4e 55 4c  g==1 returns NUL
41220 4c 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 77  L, then SQLite w
41230 69 6c 6c 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74  ill.** attempt t
41240 6f 20 75 6e 70 69 6e 20 6f 6e 65 20 6f 72 20 6d  o unpin one or m
41250 6f 72 65 20 63 61 63 68 65 20 70 61 67 65 73 20  ore cache pages 
41260 62 79 20 73 70 69 6c 6c 69 6e 67 20 74 68 65 20  by spilling the 
41270 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 70 69  content of.** pi
41280 6e 6e 65 64 20 70 61 67 65 73 20 74 6f 20 64 69  nned pages to di
41290 73 6b 20 61 6e 64 20 73 79 6e 63 68 69 6e 67 20  sk and synching 
412a0 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
412b0 73 74 65 6d 20 64 69 73 6b 20 63 61 63 68 65 2e  stem disk cache.
412c0 20 41 66 74 65 72 0a 2a 2a 20 61 74 74 65 6d 70   After.** attemp
412d0 74 69 6e 67 20 74 6f 20 75 6e 70 69 6e 20 70 61  ting to unpin pa
412e0 67 65 73 2c 20 74 68 65 20 78 46 65 74 63 68 28  ges, the xFetch(
412f0 29 20 6d 65 74 68 6f 64 20 77 69 6c 6c 20 62 65  ) method will be
41300 20 69 6e 76 6f 6b 65 64 20 61 67 61 69 6e 20 77   invoked again w
41310 69 74 68 0a 2a 2a 20 61 20 63 72 65 61 74 65 46  ith.** a createF
41320 6c 61 67 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20  lag of 2..**.** 
41330 78 55 6e 70 69 6e 28 29 20 69 73 20 63 61 6c 6c  xUnpin() is call
41340 65 64 20 62 79 20 53 51 4c 69 74 65 20 77 69 74  ed by SQLite wit
41350 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  h a pointer to a
41360 20 63 75 72 72 65 6e 74 6c 79 20 70 69 6e 6e 65   currently pinne
41370 64 20 70 61 67 65 0a 2a 2a 20 61 73 20 69 74 73  d page.** as its
41380 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
41390 2e 20 49 66 20 74 68 65 20 74 68 69 72 64 20 70  . If the third p
413a0 61 72 61 6d 65 74 65 72 2c 20 64 69 73 63 61 72  arameter, discar
413b0 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a  d, is non-zero,.
413c0 2a 2a 20 74 68 65 6e 20 74 68 65 20 70 61 67 65  ** then the page
413d0 20 73 68 6f 75 6c 64 20 62 65 20 65 76 69 63 74   should be evict
413e0 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68  ed from the cach
413f0 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
41400 53 51 4c 69 74 65 20 0a 2a 2a 20 61 73 73 75 6d  SQLite .** assum
41410 65 73 20 74 68 61 74 20 74 68 65 20 6e 65 78 74  es that the next
41420 20 74 69 6d 65 20 74 68 65 20 70 61 67 65 20 69   time the page i
41430 73 20 72 65 74 72 69 65 76 65 64 20 66 72 6f 6d  s retrieved from
41440 20 74 68 65 20 63 61 63 68 65 20 75 73 69 6e 67   the cache using
41450 0a 2a 2a 20 74 68 65 20 78 46 65 74 63 68 28 29  .** the xFetch()
41460 20 6d 65 74 68 6f 64 2c 20 69 74 20 77 69 6c 6c   method, it will
41470 20 62 65 20 7a 65 72 6f 65 64 2e 20 49 66 20 74   be zeroed. If t
41480 68 65 20 64 69 73 63 61 72 64 20 70 61 72 61 6d  he discard param
41490 65 74 65 72 20 69 73 0a 2a 2a 20 7a 65 72 6f 2c  eter is.** zero,
414a0 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 69   then the page i
414b0 73 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20  s considered to 
414c0 62 65 20 75 6e 70 69 6e 6e 65 64 2e 20 54 68 65  be unpinned. The
414d0 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
414e0 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79 20 63 68 6f  ation.** may cho
414f0 6f 73 65 20 74 6f 20 65 76 69 63 74 20 75 6e 70  ose to evict unp
41500 69 6e 6e 65 64 20 70 61 67 65 73 20 61 74 20 61  inned pages at a
41510 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54  ny time..**.** T
41520 68 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74 20  he cache is not 
41530 72 65 71 75 69 72 65 64 20 74 6f 20 70 65 72 66  required to perf
41540 6f 72 6d 20 61 6e 79 20 72 65 66 65 72 65 6e 63  orm any referenc
41550 65 20 63 6f 75 6e 74 69 6e 67 2e 20 41 20 73 69  e counting. A si
41560 6e 67 6c 65 20 0a 2a 2a 20 63 61 6c 6c 20 74 6f  ngle .** call to
41570 20 78 55 6e 70 69 6e 28 29 20 75 6e 70 69 6e 73   xUnpin() unpins
41580 20 74 68 65 20 70 61 67 65 20 72 65 67 61 72 64   the page regard
41590 6c 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d 62  less of the numb
415a0 65 72 20 6f 66 20 70 72 69 6f 72 20 63 61 6c 6c  er of prior call
415b0 73 20 0a 2a 2a 20 74 6f 20 78 46 65 74 63 68 28  s .** to xFetch(
415c0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 65  )..**.** The xRe
415d0 6b 65 79 28 29 20 6d 65 74 68 6f 64 20 69 73 20  key() method is 
415e0 75 73 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74  used to change t
415f0 68 65 20 6b 65 79 20 76 61 6c 75 65 20 61 73 73  he key value ass
41600 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
41610 0a 2a 2a 20 70 61 67 65 20 70 61 73 73 65 64 20  .** page passed 
41620 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
41630 67 75 6d 65 6e 74 20 66 72 6f 6d 20 6f 6c 64 4b  gument from oldK
41640 65 79 20 74 6f 20 6e 65 77 4b 65 79 2e 20 49 66  ey to newKey. If
41650 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 70 72   the cache.** pr
41660 65 76 69 6f 75 73 6c 79 20 63 6f 6e 74 61 69 6e  eviously contain
41670 73 20 61 6e 20 65 6e 74 72 79 20 61 73 73 6f 63  s an entry assoc
41680 69 61 74 65 64 20 77 69 74 68 20 6e 65 77 4b 65  iated with newKe
41690 79 2c 20 69 74 20 73 68 6f 75 6c 64 20 62 65 0a  y, it should be.
416a0 2a 2a 20 64 69 73 63 61 72 64 65 64 2e 20 41 6e  ** discarded. An
416b0 79 20 70 72 69 6f 72 20 63 61 63 68 65 20 65 6e  y prior cache en
416c0 74 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  try associated w
416d0 69 74 68 20 6e 65 77 4b 65 79 20 69 73 20 67 75  ith newKey is gu
416e0 61 72 61 6e 74 65 65 64 20 6e 6f 74 0a 2a 2a 20  aranteed not.** 
416f0 74 6f 20 62 65 20 70 69 6e 6e 65 64 2e 0a 2a 2a  to be pinned..**
41700 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74 65 20  .** When SQLite 
41710 63 61 6c 6c 73 20 74 68 65 20 78 54 72 75 6e 63  calls the xTrunc
41720 61 74 65 28 29 20 6d 65 74 68 6f 64 2c 20 74 68  ate() method, th
41730 65 20 63 61 63 68 65 20 6d 75 73 74 20 64 69 73  e cache must dis
41740 63 61 72 64 20 61 6c 6c 0a 2a 2a 20 65 78 69 73  card all.** exis
41750 74 69 6e 67 20 63 61 63 68 65 20 65 6e 74 72 69  ting cache entri
41760 65 73 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d  es with page num
41770 62 65 72 73 20 28 6b 65 79 73 29 20 67 72 65 61  bers (keys) grea
41780 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
41790 6c 0a 2a 2a 20 74 6f 20 74 68 65 20 76 61 6c 75  l.** to the valu
417a0 65 20 6f 66 20 74 68 65 20 69 4c 69 6d 69 74 20  e of the iLimit 
417b0 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65 64  parameter passed
417c0 20 74 6f 20 78 54 72 75 6e 63 61 74 65 28 29 2e   to xTruncate().
417d0 20 49 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68   If any.** of th
417e0 65 73 65 20 70 61 67 65 73 20 61 72 65 20 70 69  ese pages are pi
417f0 6e 6e 65 64 2c 20 74 68 65 79 20 61 72 65 20 69  nned, they are i
41800 6d 70 6c 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e  mplicitly unpinn
41810 65 64 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74  ed, meaning that
41820 0a 2a 2a 20 74 68 65 79 20 63 61 6e 20 62 65 20  .** they can be 
41830 73 61 66 65 6c 79 20 64 69 73 63 61 72 64 65 64  safely discarded
41840 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44 65 73  ..**.** The xDes
41850 74 72 6f 79 28 29 20 6d 65 74 68 6f 64 20 69 73  troy() method is
41860 20 75 73 65 64 20 74 6f 20 64 65 6c 65 74 65 20   used to delete 
41870 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61 74 65  a cache allocate
41880 64 20 62 79 20 78 43 72 65 61 74 65 28 29 2e 0a  d by xCreate()..
41890 2a 2a 20 41 6c 6c 20 72 65 73 6f 75 72 63 65 73  ** All resources
418a0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
418b0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63   the specified c
418c0 61 63 68 65 20 73 68 6f 75 6c 64 20 62 65 20 66  ache should be f
418d0 72 65 65 64 2e 20 41 66 74 65 72 0a 2a 2a 20 63  reed. After.** c
418e0 61 6c 6c 69 6e 67 20 74 68 65 20 78 44 65 73 74  alling the xDest
418f0 72 6f 79 28 29 20 6d 65 74 68 6f 64 2c 20 53 51  roy() method, SQ
41900 4c 69 74 65 20 63 6f 6e 73 69 64 65 72 73 20 74  Lite considers t
41910 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63  he [sqlite3_pcac
41920 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64 6c 65 20 69  he*].** handle i
41930 6e 76 61 6c 69 64 2c 20 61 6e 64 20 77 69 6c 6c  nvalid, and will
41940 20 6e 6f 74 20 75 73 65 20 69 74 20 77 69 74 68   not use it with
41950 20 61 6e 79 20 6f 74 68 65 72 20 73 71 6c 69 74   any other sqlit
41960 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
41970 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  s.** functions..
41980 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
41990 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  t sqlite3_pcache
419a0 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33  _methods sqlite3
419b0 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 3b  _pcache_methods;
419c0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
419d0 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 7b  pcache_methods {
419e0 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20  .  void *pArg;. 
419f0 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 76 6f   int (*xInit)(vo
41a00 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  id*);.  void (*x
41a10 53 68 75 74 64 6f 77 6e 29 28 76 6f 69 64 2a 29  Shutdown)(void*)
41a20 3b 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63  ;.  sqlite3_pcac
41a30 68 65 20 2a 28 2a 78 43 72 65 61 74 65 29 28 69  he *(*xCreate)(i
41a40 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e 74 20 62  nt szPage, int b
41a50 50 75 72 67 65 61 62 6c 65 29 3b 0a 20 20 76 6f  Purgeable);.  vo
41a60 69 64 20 28 2a 78 43 61 63 68 65 73 69 7a 65 29  id (*xCachesize)
41a70 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
41a80 2c 20 69 6e 74 20 6e 43 61 63 68 65 73 69 7a 65  , int nCachesize
41a90 29 3b 0a 20 20 69 6e 74 20 28 2a 78 50 61 67 65  );.  int (*xPage
41aa0 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 70  count)(sqlite3_p
41ab0 63 61 63 68 65 2a 29 3b 0a 20 20 76 6f 69 64 20  cache*);.  void 
41ac0 2a 28 2a 78 46 65 74 63 68 29 28 73 71 6c 69 74  *(*xFetch)(sqlit
41ad0 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69  e3_pcache*, unsi
41ae0 67 6e 65 64 20 6b 65 79 2c 20 69 6e 74 20 63 72  gned key, int cr
41af0 65 61 74 65 46 6c 61 67 29 3b 0a 20 20 76 6f 69  eateFlag);.  voi
41b00 64 20 28 2a 78 55 6e 70 69 6e 29 28 73 71 6c 69  d (*xUnpin)(sqli
41b10 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69  te3_pcache*, voi
41b20 64 2a 2c 20 69 6e 74 20 64 69 73 63 61 72 64 29  d*, int discard)
41b30 3b 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 6b 65  ;.  void (*xReke
41b40 79 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  y)(sqlite3_pcach
41b50 65 2a 2c 20 76 6f 69 64 2a 2c 20 75 6e 73 69 67  e*, void*, unsig
41b60 6e 65 64 20 6f 6c 64 4b 65 79 2c 20 75 6e 73 69  ned oldKey, unsi
41b70 67 6e 65 64 20 6e 65 77 4b 65 79 29 3b 0a 20 20  gned newKey);.  
41b80 76 6f 69 64 20 28 2a 78 54 72 75 6e 63 61 74 65  void (*xTruncate
41b90 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  )(sqlite3_pcache
41ba0 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69 4c 69 6d  *, unsigned iLim
41bb0 69 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44  it);.  void (*xD
41bc0 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33 5f  estroy)(sqlite3_
41bd0 70 63 61 63 68 65 2a 29 3b 0a 7d 3b 0a 0a 2f 2a  pcache*);.};../*
41be0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 6e  .** CAPI3REF: On
41bf0 6c 69 6e 65 20 42 61 63 6b 75 70 20 4f 62 6a 65  line Backup Obje
41c00 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  ct.** EXPERIMENT
41c10 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  AL.**.** The sql
41c20 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65  ite3_backup obje
41c30 63 74 20 72 65 63 6f 72 64 73 20 73 74 61 74 65  ct records state
41c40 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
41c50 75 74 20 61 6e 20 6f 6e 67 6f 69 6e 67 0a 2a 2a  ut an ongoing.**
41c60 20 6f 6e 6c 69 6e 65 20 62 61 63 6b 75 70 20 6f   online backup o
41c70 70 65 72 61 74 69 6f 6e 2e 20 20 54 68 65 20 73  peration.  The s
41c80 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62  qlite3_backup ob
41c90 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 20  ject is created 
41ca0 62 79 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20  by.** a call to 
41cb0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  [sqlite3_backup_
41cc0 69 6e 69 74 28 29 5d 20 61 6e 64 20 69 73 20 64  init()] and is d
41cd0 65 73 74 72 6f 79 65 64 20 62 79 20 61 20 63 61  estroyed by a ca
41ce0 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65  ll to.** [sqlite
41cf0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
41d00 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c  )]..**.** See Al
41d10 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53  so: [Using the S
41d20 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 63  QLite Online Bac
41d30 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a 74 79 70 65  kup API].*/.type
41d40 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
41d50 65 33 5f 62 61 63 6b 75 70 20 73 71 6c 69 74 65  e3_backup sqlite
41d60 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f 2a 0a 2a 2a  3_backup;../*.**
41d70 20 43 41 50 49 33 52 45 46 3a 20 4f 6e 6c 69 6e   CAPI3REF: Onlin
41d80 65 20 42 61 63 6b 75 70 20 41 50 49 2e 0a 2a 2a  e Backup API..**
41d90 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
41da0 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20  .** This API is 
41db0 75 73 65 64 20 74 6f 20 6f 76 65 72 77 72 69 74  used to overwrit
41dc0 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  e the contents o
41dd0 66 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 77  f one database w
41de0 69 74 68 20 74 68 61 74 0a 2a 2a 20 6f 66 20 61  ith that.** of a
41df0 6e 6f 74 68 65 72 2e 20 49 74 20 69 73 20 75 73  nother. It is us
41e00 65 66 75 6c 20 65 69 74 68 65 72 20 66 6f 72 20  eful either for 
41e10 63 72 65 61 74 69 6e 67 20 62 61 63 6b 75 70 73  creating backups
41e20 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 6f 72   of databases or
41e30 0a 2a 2a 20 66 6f 72 20 63 6f 70 79 69 6e 67 20  .** for copying 
41e40 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
41e50 73 65 73 20 74 6f 20 6f 72 20 66 72 6f 6d 20 70  ses to or from p
41e60 65 72 73 69 73 74 65 6e 74 20 66 69 6c 65 73 2e  ersistent files.
41e70 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f   .**.** See Also
41e80 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51 4c  : [Using the SQL
41e90 69 74 65 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75  ite Online Backu
41ea0 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a 20 45 78 63  p API].**.** Exc
41eb0 6c 75 73 69 76 65 20 61 63 63 65 73 73 20 69 73  lusive access is
41ec0 20 72 65 71 75 69 72 65 64 20 74 6f 20 74 68 65   required to the
41ed0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
41ee0 61 62 61 73 65 20 66 6f 72 20 74 68 65 20 0a 2a  abase for the .*
41ef0 2a 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68  * duration of th
41f00 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 48 6f 77  e operation. How
41f10 65 76 65 72 20 74 68 65 20 73 6f 75 72 63 65 20  ever the source 
41f20 64 61 74 61 62 61 73 65 20 69 73 20 6f 6e 6c 79  database is only
41f30 0a 2a 2a 20 72 65 61 64 2d 6c 6f 63 6b 65 64 20  .** read-locked 
41f40 77 68 69 6c 65 20 69 74 20 69 73 20 61 63 74 75  while it is actu
41f50 61 6c 6c 79 20 62 65 69 6e 67 20 72 65 61 64 2c  ally being read,
41f60 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65   it is not locke
41f70 64 0a 2a 2a 20 63 6f 6e 74 69 6e 75 6f 75 73 6c  d.** continuousl
41f80 79 20 66 6f 72 20 74 68 65 20 65 6e 74 69 72 65  y for the entire
41f90 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 75 73   operation. Thus
41fa0 2c 20 74 68 65 20 62 61 63 6b 75 70 20 6d 61 79  , the backup may
41fb0 20 62 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64   be.** performed
41fc0 20 6f 6e 20 61 20 6c 69 76 65 20 64 61 74 61 62   on a live datab
41fd0 61 73 65 20 77 69 74 68 6f 75 74 20 70 72 65 76  ase without prev
41fe0 65 6e 74 69 6e 67 20 6f 74 68 65 72 20 75 73 65  enting other use
41ff0 72 73 20 66 72 6f 6d 0a 2a 2a 20 77 72 69 74 69  rs from.** writi
42000 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
42010 73 65 20 66 6f 72 20 61 6e 20 65 78 74 65 6e 64  se for an extend
42020 65 64 20 70 65 72 69 6f 64 20 6f 66 20 74 69 6d  ed period of tim
42030 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 6f 20 70 65 72  e..** .** To per
42040 66 6f 72 6d 20 61 20 62 61 63 6b 75 70 20 6f 70  form a backup op
42050 65 72 61 74 69 6f 6e 3a 20 0a 2a 2a 20 20 20 3c  eration: .**   <
42060 6f 6c 3e 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c  ol>.**     <li><
42070 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  b>sqlite3_backup
42080 5f 69 6e 69 74 28 29 3c 2f 62 3e 20 69 73 20 63  _init()</b> is c
42090 61 6c 6c 65 64 20 6f 6e 63 65 20 74 6f 20 69 6e  alled once to in
420a0 69 74 69 61 6c 69 7a 65 20 74 68 65 0a 2a 2a 20  itialize the.** 
420b0 20 20 20 20 20 20 20 20 62 61 63 6b 75 70 2c 20          backup, 
420c0 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73  .**     <li><b>s
420d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
420e0 65 70 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c  ep()</b> is call
420f0 65 64 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74  ed one or more t
42100 69 6d 65 73 20 74 6f 20 74 72 61 6e 73 66 65 72  imes to transfer
42110 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 68 65   .**         the
42120 20 64 61 74 61 20 62 65 74 77 65 65 6e 20 74 68   data between th
42130 65 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 2c  e two databases,
42140 20 61 6e 64 20 66 69 6e 61 6c 6c 79 0a 2a 2a 20   and finally.** 
42150 20 20 20 20 3c 6c 69 3e 3c 62 3e 73 71 6c 69 74      <li><b>sqlit
42160 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
42170 28 29 3c 2f 62 3e 20 69 73 20 63 61 6c 6c 65 64  ()</b> is called
42180 20 74 6f 20 72 65 6c 65 61 73 65 20 61 6c 6c 20   to release all 
42190 72 65 73 6f 75 72 63 65 73 20 0a 2a 2a 20 20 20  resources .**   
421a0 20 20 20 20 20 20 61 73 73 6f 63 69 61 74 65 64        associated
421b0 20 77 69 74 68 20 74 68 65 20 62 61 63 6b 75 70   with the backup
421c0 20 6f 70 65 72 61 74 69 6f 6e 2e 20 0a 2a 2a 20   operation. .** 
421d0 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20 54 68 65 72 65    </ol>.** There
421e0 20 73 68 6f 75 6c 64 20 62 65 20 65 78 61 63 74   should be exact
421f0 6c 79 20 6f 6e 65 20 63 61 6c 6c 20 74 6f 20 73  ly one call to s
42200 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
42210 6e 69 73 68 28 29 20 66 6f 72 20 65 61 63 68 0a  nish() for each.
42220 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 63 61  ** successful ca
42230 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61  ll to sqlite3_ba
42240 63 6b 75 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a  ckup_init()..**.
42250 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61  ** <b>sqlite3_ba
42260 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 0a  ckup_init()</b>.
42270 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
42280 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 70 61  two arguments pa
42290 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33  ssed to [sqlite3
422a0 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 5d 20  _backup_init()] 
422b0 61 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  are the database
422c0 0a 2a 2a 20 68 61 6e 64 6c 65 20 61 73 73 6f 63  .** handle assoc
422d0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 64  iated with the d
422e0 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
422f0 61 73 65 20 61 6e 64 20 74 68 65 20 64 61 74 61  ase and the data
42300 62 61 73 65 20 6e 61 6d 65 20 0a 2a 2a 20 75 73  base name .** us
42310 65 64 20 74 6f 20 61 74 74 61 63 68 20 74 68 65  ed to attach the
42320 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
42330 61 62 61 73 65 20 74 6f 20 74 68 65 20 68 61 6e  abase to the han
42340 64 6c 65 2e 20 54 68 65 20 64 61 74 61 62 61 73  dle. The databas
42350 65 20 6e 61 6d 65 0a 2a 2a 20 69 73 20 22 6d 61  e name.** is "ma
42360 69 6e 22 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  in" for the main
42370 20 64 61 74 61 62 61 73 65 2c 20 22 74 65 6d 70   database, "temp
42380 22 20 66 6f 72 20 74 68 65 20 74 65 6d 70 6f 72  " for the tempor
42390 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 6f 72  ary database, or
423a0 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 73 70 65  .** the name spe
423b0 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20 6f  cified as part o
423c0 66 20 74 68 65 20 5b 41 54 54 41 43 48 5d 20 73  f the [ATTACH] s
423d0 74 61 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20  tatement if the 
423e0 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 0a 2a  destination is.*
423f0 2a 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  * an attached da
42400 74 61 62 61 73 65 2e 20 54 68 65 20 74 68 69 72  tabase. The thir
42410 64 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67  d and fourth arg
42420 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f  uments passed to
42430 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63   .** sqlite3_bac
42440 6b 75 70 5f 69 6e 69 74 28 29 20 69 64 65 6e 74  kup_init() ident
42450 69 66 79 20 74 68 65 20 5b 64 61 74 61 62 61 73  ify the [databas
42460 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a  e connection].**
42470 20 61 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61   and database na
42480 6d 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 61 63  me used.** to ac
42490 63 65 73 73 20 74 68 65 20 73 6f 75 72 63 65 20  cess the source 
424a0 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 76 61  database. The va
424b0 6c 75 65 73 20 70 61 73 73 65 64 20 66 6f 72 20  lues passed for 
424c0 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20 0a  the source and .
424d0 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b  ** destination [
424e0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
424f0 69 6f 6e 5d 20 70 61 72 61 6d 65 74 65 72 73 20  ion] parameters 
42500 6d 75 73 74 20 6e 6f 74 20 62 65 20 74 68 65 20  must not be the 
42510 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  same..**.** If a
42520 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  n error occurs w
42530 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 62 61  ithin sqlite3_ba
42540 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 74 68 65  ckup_init(), the
42550 6e 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  n NULL is return
42560 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 65 72 72  ed.** and an err
42570 6f 72 20 63 6f 64 65 20 61 6e 64 20 65 72 72 6f  or code and erro
42580 72 20 6d 65 73 73 61 67 65 20 77 72 69 74 74 65  r message writte
42590 6e 20 69 6e 74 6f 20 74 68 65 20 5b 64 61 74 61  n into the [data
425a0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
425b0 20 0a 2a 2a 20 70 61 73 73 65 64 20 61 73 20 74   .** passed as t
425c0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
425d0 74 2e 20 54 68 65 79 20 6d 61 79 20 62 65 20 72  t. They may be r
425e0 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 74  etrieved using t
425f0 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65  he.** [sqlite3_e
42600 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69  rrcode()], [sqli
42610 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 61  te3_errmsg()], a
42620 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  nd [sqlite3_errm
42630 73 67 31 36 28 29 5d 20 66 75 6e 63 74 69 6f 6e  sg16()] function
42640 73 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  s..** Otherwise,
42650 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   if successful, 
42660 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
42670 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d  [sqlite3_backup]
42680 20 6f 62 6a 65 63 74 20 69 73 0a 2a 2a 20 72 65   object is.** re
42690 74 75 72 6e 65 64 2e 20 54 68 69 73 20 70 6f 69  turned. This poi
426a0 6e 74 65 72 20 6d 61 79 20 62 65 20 75 73 65 64  nter may be used
426b0 20 77 69 74 68 20 74 68 65 20 73 71 6c 69 74 65   with the sqlite
426c0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
426d0 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62  and.** sqlite3_b
426e0 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20 66  ackup_finish() f
426f0 75 6e 63 74 69 6f 6e 73 20 74 6f 20 70 65 72 66  unctions to perf
42700 6f 72 6d 20 74 68 65 20 73 70 65 63 69 66 69 65  orm the specifie
42710 64 20 62 61 63 6b 75 70 20 0a 2a 2a 20 6f 70 65  d backup .** ope
42720 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 3c 62  ration..**.** <b
42730 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f  >sqlite3_backup_
42740 73 74 65 70 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a  step()</b>.**.**
42750 20 46 75 6e 63 74 69 6f 6e 20 5b 73 71 6c 69 74   Function [sqlit
42760 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
42770 5d 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 70  ] is used to cop
42780 79 20 75 70 20 74 6f 20 6e 50 61 67 65 20 70 61  y up to nPage pa
42790 67 65 73 20 62 65 74 77 65 65 6e 20 0a 2a 2a 20  ges between .** 
427a0 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64 20 64  the source and d
427b0 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
427c0 61 73 65 73 2c 20 77 68 65 72 65 20 6e 50 61 67  ases, where nPag
427d0 65 20 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f  e is the value o
427e0 66 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64  f the .** second
427f0 20 70 61 72 61 6d 65 74 65 72 20 70 61 73 73 65   parameter passe
42800 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  d to sqlite3_bac
42810 6b 75 70 5f 73 74 65 70 28 29 2e 20 49 66 20 6e  kup_step(). If n
42820 50 61 67 65 20 69 73 20 61 20 6e 65 67 61 74 69  Page is a negati
42830 76 65 0a 2a 2a 20 76 61 6c 75 65 2c 20 61 6c 6c  ve.** value, all
42840 20 72 65 6d 61 69 6e 69 6e 67 20 73 6f 75 72 63   remaining sourc
42850 65 20 70 61 67 65 73 20 61 72 65 20 63 6f 70 69  e pages are copi
42860 65 64 2e 20 49 66 20 74 68 65 20 72 65 71 75 69  ed. If the requi
42870 72 65 64 20 70 61 67 65 73 20 61 72 65 20 0a 2a  red pages are .*
42880 2a 20 73 75 63 63 65 73 66 75 6c 6c 79 20 63 6f  * succesfully co
42890 70 69 65 64 2c 20 62 75 74 20 74 68 65 72 65 20  pied, but there 
428a0 61 72 65 20 73 74 69 6c 6c 20 6d 6f 72 65 20 70  are still more p
428b0 61 67 65 73 20 74 6f 20 63 6f 70 79 20 62 65 66  ages to copy bef
428c0 6f 72 65 20 74 68 65 20 0a 2a 2a 20 62 61 63 6b  ore the .** back
428d0 75 70 20 69 73 20 63 6f 6d 70 6c 65 74 65 2c 20  up is complete, 
428e0 69 74 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  it returns [SQLI
428f0 54 45 5f 4f 4b 5d 2e 20 49 66 20 6e 6f 20 65 72  TE_OK]. If no er
42900 72 6f 72 20 6f 63 63 75 72 65 64 20 61 6e 64 20  ror occured and 
42910 74 68 65 72 65 20 0a 2a 2a 20 61 72 65 20 6e 6f  there .** are no
42920 20 6d 6f 72 65 20 70 61 67 65 73 20 74 6f 20 63   more pages to c
42930 6f 70 79 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54  opy, then [SQLIT
42940 45 5f 44 4f 4e 45 5d 20 69 73 20 72 65 74 75 72  E_DONE] is retur
42950 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ned. If an error
42960 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20 74 68 65   .** occurs, the
42970 6e 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  n an SQLite erro
42980 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
42990 65 64 2e 20 41 73 20 77 65 6c 6c 20 61 73 20 5b  ed. As well as [
429a0 53 51 4c 49 54 45 5f 4f 4b 5d 20 61 6e 64 0a 2a  SQLITE_OK] and.*
429b0 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2c  * [SQLITE_DONE],
429c0 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
429d0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
429e0 20 6d 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c   may return [SQL
429f0 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 2c 0a 2a  ITE_READONLY],.*
42a00 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d  * [SQLITE_NOMEM]
42a10 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c  , [SQLITE_BUSY],
42a20 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d   [SQLITE_LOCKED]
42a30 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51 4c 49  , or an.** [SQLI
42a40 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20  TE_IOERR_ACCESS 
42a50 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58  | SQLITE_IOERR_X
42a60 58 58 5d 20 65 78 74 65 6e 64 65 64 20 65 72 72  XX] extended err
42a70 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41  or code..**.** A
42a80 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 63 61  s well as the ca
42a90 73 65 20 77 68 65 72 65 20 74 68 65 20 64 65 73  se where the des
42aa0 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
42ab0 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e 65  e file was opene
42ac0 64 20 66 6f 72 0a 2a 2a 20 72 65 61 64 2d 6f 6e  d for.** read-on
42ad0 6c 79 20 61 63 63 65 73 73 2c 20 73 71 6c 69 74  ly access, sqlit
42ae0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
42af0 20 6d 61 79 20 72 65 74 75 72 6e 20 5b 53 51 4c   may return [SQL
42b00 49 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 66  ITE_READONLY] if
42b10 0a 2a 2a 20 74 68 65 20 64 65 73 74 69 6e 61 74  .** the destinat
42b20 69 6f 6e 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d  ion is an in-mem
42b30 6f 72 79 20 64 61 74 61 62 61 73 65 20 77 69 74  ory database wit
42b40 68 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 61  h a different pa
42b50 67 65 20 73 69 7a 65 0a 2a 2a 20 66 72 6f 6d 20  ge size.** from 
42b60 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
42b70 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71  ase..**.** If sq
42b80 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
42b90 70 28 29 20 63 61 6e 6e 6f 74 20 6f 62 74 61 69  p() cannot obtai
42ba0 6e 20 61 20 72 65 71 75 69 72 65 64 20 66 69 6c  n a required fil
42bb0 65 2d 73 79 73 74 65 6d 20 6c 6f 63 6b 2c 20 74  e-system lock, t
42bc0 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69  hen.** the [sqli
42bd0 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
42be0 20 7c 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20   | busy-handler 
42bf0 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 20  function].** is 
42c00 69 6e 76 6f 6b 65 64 20 28 69 66 20 6f 6e 65 20  invoked (if one 
42c10 69 73 20 73 70 65 63 69 66 69 65 64 29 2e 20 49  is specified). I
42c20 66 20 74 68 65 20 0a 2a 2a 20 62 75 73 79 2d 68  f the .** busy-h
42c30 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 6e  andler returns n
42c40 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f 72 65 20 74  on-zero before t
42c50 68 65 20 6c 6f 63 6b 20 69 73 20 61 76 61 69 6c  he lock is avail
42c60 61 62 6c 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 5b  able, then .** [
42c70 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 69 73 20  SQLITE_BUSY] is 
42c80 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
42c90 63 61 6c 6c 65 72 2e 20 49 6e 20 74 68 69 73 20  caller. In this 
42ca0 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74 6f  case the call to
42cb0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
42cc0 75 70 5f 73 74 65 70 28 29 20 63 61 6e 20 62 65  up_step() can be
42cd0 20 72 65 74 72 69 65 64 20 6c 61 74 65 72 2e 20   retried later. 
42ce0 49 66 20 74 68 65 20 73 6f 75 72 63 65 0a 2a 2a  If the source.**
42cf0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
42d00 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73 20 62 65 69  ction].** is bei
42d10 6e 67 20 75 73 65 64 20 74 6f 20 77 72 69 74 65  ng used to write
42d20 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64   to the source d
42d30 61 74 61 62 61 73 65 20 77 68 65 6e 20 73 71 6c  atabase when sql
42d40 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
42d50 28 29 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2c  ().** is called,
42d60 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f   then [SQLITE_LO
42d70 43 4b 45 44 5d 20 69 73 20 72 65 74 75 72 6e 65  CKED] is returne
42d80 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 41  d immediately. A
42d90 67 61 69 6e 2c 20 69 6e 20 74 68 69 73 0a 2a 2a  gain, in this.**
42da0 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74   case the call t
42db0 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  o sqlite3_backup
42dc0 5f 73 74 65 70 28 29 20 63 61 6e 20 62 65 20 72  _step() can be r
42dd0 65 74 72 69 65 64 20 6c 61 74 65 72 20 6f 6e 2e  etried later on.
42de0 20 49 66 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49   If.** [SQLITE_I
42df0 4f 45 52 52 5f 41 43 43 45 53 53 20 7c 20 53 51  OERR_ACCESS | SQ
42e00 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 5d 2c  LITE_IOERR_XXX],
42e10 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c   [SQLITE_NOMEM],
42e20 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 52   or.** [SQLITE_R
42e30 45 41 44 4f 4e 4c 59 5d 20 69 73 20 72 65 74 75  EADONLY] is retu
42e40 72 6e 65 64 2c 20 74 68 65 6e 20 0a 2a 2a 20 74  rned, then .** t
42e50 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 69 6e 74  here is no point
42e60 20 69 6e 20 72 65 74 72 79 69 6e 67 20 74 68 65   in retrying the
42e70 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
42e80 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20  _backup_step(). 
42e90 54 68 65 73 65 20 0a 2a 2a 20 65 72 72 6f 72 73  These .** errors
42ea0 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20   are considered 
42eb0 66 61 74 61 6c 2e 20 41 74 20 74 68 69 73 20 70  fatal. At this p
42ec0 6f 69 6e 74 20 74 68 65 20 61 70 70 6c 69 63 61  oint the applica
42ed0 74 69 6f 6e 20 6d 75 73 74 20 61 63 63 65 70 74  tion must accept
42ee0 20 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 62 61   .** that the ba
42ef0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 20 68  ckup operation h
42f00 61 73 20 66 61 69 6c 65 64 20 61 6e 64 20 70 61  as failed and pa
42f10 73 73 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70  ss the backup op
42f20 65 72 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 0a  eration handle .
42f30 2a 2a 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  ** to the sqlite
42f40 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
42f50 29 20 74 6f 20 72 65 6c 65 61 73 65 20 61 73 73  ) to release ass
42f60 6f 63 69 61 74 65 64 20 72 65 73 6f 75 72 63 65  ociated resource
42f70 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 6c 6c 6f 77 69  s..**.** Followi
42f80 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  ng the first cal
42f90 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63  l to sqlite3_bac
42fa0 6b 75 70 5f 73 74 65 70 28 29 2c 20 61 6e 20 65  kup_step(), an e
42fb0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73  xclusive lock is
42fc0 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20  .** obtained on 
42fd0 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
42fe0 66 69 6c 65 2e 20 49 74 20 69 73 20 6e 6f 74 20  file. It is not 
42ff0 72 65 6c 65 61 73 65 64 20 75 6e 74 69 6c 20 65  released until e
43000 69 74 68 65 72 20 0a 2a 2a 20 73 71 6c 69 74 65  ither .** sqlite
43010 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
43020 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 72 20 74  ) is called or t
43030 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74  he backup operat
43040 69 6f 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 20  ion is complete 
43050 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  .** and sqlite3_
43060 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 72 65  backup_step() re
43070 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44 4f  turns [SQLITE_DO
43080 4e 45 5d 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c  NE]. Additionall
43090 79 2c 20 65 61 63 68 20 74 69 6d 65 20 0a 2a 2a  y, each time .**
430a0 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
430b0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
430c0 20 69 73 20 6d 61 64 65 20 61 20 5b 73 68 61 72   is made a [shar
430d0 65 64 20 6c 6f 63 6b 5d 20 69 73 20 6f 62 74 61  ed lock] is obta
430e0 69 6e 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 73  ined on.** the s
430f0 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66  ource database f
43100 69 6c 65 2e 20 54 68 69 73 20 6c 6f 63 6b 20 69  ile. This lock i
43110 73 20 72 65 6c 65 61 73 65 64 20 62 65 66 6f 72  s released befor
43120 65 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  e the.** sqlite3
43130 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63  _backup_step() c
43140 61 6c 6c 20 72 65 74 75 72 6e 73 2e 20 42 65 63  all returns. Bec
43150 61 75 73 65 20 74 68 65 20 73 6f 75 72 63 65 20  ause the source 
43160 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a  database is not.
43170 2a 2a 20 6c 6f 63 6b 65 64 20 62 65 74 77 65 65  ** locked betwee
43180 6e 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  n calls to sqlit
43190 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29  e3_backup_step()
431a0 2c 20 69 74 20 6d 61 79 20 62 65 20 6d 6f 64 69  , it may be modi
431b0 66 69 65 64 20 6d 69 64 2d 77 61 79 0a 2a 2a 20  fied mid-way.** 
431c0 74 68 72 6f 75 67 68 20 74 68 65 20 62 61 63 6b  through the back
431d0 75 70 20 70 72 6f 63 65 64 75 72 65 2e 20 49 66  up procedure. If
431e0 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
431f0 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64  base is modified
43200 20 62 79 20 61 6e 0a 2a 2a 20 65 78 74 65 72 6e   by an.** extern
43210 61 6c 20 70 72 6f 63 65 73 73 20 6f 72 20 76 69  al process or vi
43220 61 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  a a database con
43230 6e 65 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68  nection other th
43240 61 6e 20 74 68 65 20 6f 6e 65 20 62 65 69 6e 67  an the one being
43250 0a 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20  .** used by the 
43260 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
43270 2c 20 74 68 65 6e 20 74 68 65 20 62 61 63 6b 75  , then the backu
43280 70 20 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 70  p will be transp
43290 61 72 65 6e 74 6c 79 0a 2a 2a 20 72 65 73 74 61  arently.** resta
432a0 72 74 65 64 20 62 79 20 74 68 65 20 6e 65 78 74  rted by the next
432b0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
432c0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20  _backup_step(). 
432d0 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 0a 2a  If the source .*
432e0 2a 20 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f  * database is mo
432f0 64 69 66 69 65 64 20 62 79 20 74 68 65 20 75 73  dified by the us
43300 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74  ing the same dat
43310 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
43320 20 61 73 20 69 73 20 75 73 65 64 0a 2a 2a 20 62   as is used.** b
43330 79 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65  y the backup ope
43340 72 61 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65  ration, then the
43350 20 62 61 63 6b 75 70 20 64 61 74 61 62 61 73 65   backup database
43360 20 69 73 20 74 72 61 6e 73 70 61 72 65 6e 74 6c   is transparentl
43370 79 20 0a 2a 2a 20 75 70 64 61 74 65 64 20 61 74  y .** updated at
43380 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a   the same time..
43390 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33  **.** <b>sqlite3
433a0 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
433b0 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20  </b>.**.** Once 
433c0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
433d0 74 65 70 28 29 20 68 61 73 20 72 65 74 75 72 6e  tep() has return
433e0 65 64 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d  ed [SQLITE_DONE]
433f0 2c 20 6f 72 20 77 68 65 6e 20 74 68 65 20 0a 2a  , or when the .*
43400 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 77 69  * application wi
43410 73 68 65 73 20 74 6f 20 61 62 61 6e 64 6f 6e 20  shes to abandon 
43420 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61  the backup opera
43430 74 69 6f 6e 2c 20 74 68 65 20 5b 73 71 6c 69 74  tion, the [sqlit
43440 65 33 5f 62 61 63 6b 75 70 5d 0a 2a 2a 20 6f 62  e3_backup].** ob
43450 6a 65 63 74 20 73 68 6f 75 6c 64 20 62 65 20 70  ject should be p
43460 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33  assed to sqlite3
43470 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
43480 2e 20 54 68 69 73 20 72 65 6c 65 61 73 65 73 20  . This releases 
43490 61 6c 6c 0a 2a 2a 20 72 65 73 6f 75 72 63 65 73  all.** resources
434a0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
434b0 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72   the backup oper
434c0 61 74 69 6f 6e 2e 20 49 66 20 73 71 6c 69 74 65  ation. If sqlite
434d0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 0a  3_backup_step().
434e0 2a 2a 20 68 61 73 20 6e 6f 74 20 79 65 74 20 72  ** has not yet r
434f0 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f  eturned [SQLITE_
43500 44 4f 4e 45 5d 2c 20 74 68 65 6e 20 61 6e 79 20  DONE], then any 
43510 61 63 74 69 76 65 20 77 72 69 74 65 2d 74 72 61  active write-tra
43520 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 0a  nsaction on the.
43530 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  ** destination d
43540 61 74 61 62 61 73 65 20 69 73 20 72 6f 6c 6c 65  atabase is rolle
43550 64 20 62 61 63 6b 2e 20 54 68 65 20 5b 73 71 6c  d back. The [sql
43560 69 74 65 33 5f 62 61 63 6b 75 70 5d 20 6f 62 6a  ite3_backup] obj
43570 65 63 74 20 69 73 20 69 6e 76 61 6c 69 64 0a 2a  ect is invalid.*
43580 2a 20 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65  * and may not be
43590 20 75 73 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20   used following 
435a0 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
435b0 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28  3_backup_finish(
435c0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c  )..**.** The val
435d0 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
435e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
435f0 6e 69 73 68 20 69 73 20 5b 53 51 4c 49 54 45 5f  nish is [SQLITE_
43600 4f 4b 5d 20 69 66 20 6e 6f 20 65 72 72 6f 72 0a  OK] if no error.
43610 2a 2a 20 6f 63 63 75 72 72 65 64 2c 20 72 65 67  ** occurred, reg
43620 61 72 64 6c 65 73 73 20 6f 72 20 77 68 65 74 68  ardless or wheth
43630 65 72 20 6f 72 20 6e 6f 74 20 73 71 6c 69 74 65  er or not sqlite
43640 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
43650 77 61 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20  was called.** a 
43660 73 75 66 66 69 63 69 65 6e 74 20 6e 75 6d 62 65  sufficient numbe
43670 72 20 6f 66 20 74 69 6d 65 73 20 74 6f 20 63 6f  r of times to co
43680 6d 70 6c 65 74 65 20 74 68 65 20 62 61 63 6b 75  mplete the backu
43690 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 4f 72 2c  p operation. Or,
436a0 20 69 66 0a 2a 2a 20 61 6e 20 6f 75 74 2d 6f 66   if.** an out-of
436b0 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69 6f  -memory conditio
436c0 6e 20 6f 72 20 49 4f 20 65 72 72 6f 72 20 6f 63  n or IO error oc
436d0 63 75 72 65 64 20 64 75 72 69 6e 67 20 61 20 63  cured during a c
436e0 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  all to.** sqlite
436f0 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20  3_backup_step() 
43700 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d  then [SQLITE_NOM
43710 45 4d 5d 20 6f 72 20 61 6e 0a 2a 2a 20 5b 53 51  EM] or an.** [SQ
43720 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53  LITE_IOERR_ACCES
43730 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  S | SQLITE_IOERR
43740 5f 58 58 58 5d 20 65 72 72 6f 72 20 63 6f 64 65  _XXX] error code
43750 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e  .** is returned.
43760 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
43770 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  e error code and
43780 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
43790 65 20 61 72 65 0a 2a 2a 20 77 72 69 74 74 65 6e  e are.** written
437a0 20 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74   to the destinat
437b0 69 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f  ion [database co
437c0 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a  nnection]..**.**
437d0 20 41 20 72 65 74 75 72 6e 20 6f 66 20 5b 53 51   A return of [SQ
437e0 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53  LITE_BUSY] or [S
437f0 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5d 20 66 72  QLITE_LOCKED] fr
43800 6f 6d 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  om sqlite3_backu
43810 70 5f 73 74 65 70 28 29 20 69 73 0a 2a 2a 20 6e  p_step() is.** n
43820 6f 74 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 65  ot a permanent e
43830 72 72 6f 72 20 61 6e 64 20 64 6f 65 73 20 6e 6f  rror and does no
43840 74 20 61 66 66 65 63 74 20 74 68 65 20 72 65 74  t affect the ret
43850 75 72 6e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20  urn value of.** 
43860 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
43870 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 3c  inish()..**.** <
43880 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  b>sqlite3_backup
43890 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c 20 73 71  _remaining(), sq
438a0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70 61 67  lite3_backup_pag
438b0 65 63 6f 75 6e 74 28 29 3c 2f 62 3e 0a 2a 2a 0a  ecount()</b>.**.
438c0 2a 2a 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20  ** Each call to 
438d0 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73  sqlite3_backup_s
438e0 74 65 70 28 29 20 73 65 74 73 20 74 77 6f 20 76  tep() sets two v
438f0 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 74  alues stored int
43900 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 61 6e  ernally.** by an
43910 20 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70   [sqlite3_backup
43920 5d 20 6f 62 6a 65 63 74 2e 20 54 68 65 20 6e 75  ] object. The nu
43930 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 74  mber of pages st
43940 69 6c 6c 20 74 6f 20 62 65 20 62 61 63 6b 65 64  ill to be backed
43950 0a 2a 2a 20 75 70 2c 20 77 68 69 63 68 20 6d 61  .** up, which ma
43960 79 20 62 65 20 71 75 65 72 69 65 64 20 62 79 20  y be queried by 
43970 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 72  sqlite3_backup_r
43980 65 6d 61 69 6e 69 6e 67 28 29 2c 20 61 6e 64 20  emaining(), and 
43990 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 6e 75 6d  the total.** num
439a0 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
439b0 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
439c0 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68 20  ase file, which 
439d0 6d 61 79 20 62 65 20 71 75 65 72 69 65 64 20 62  may be queried b
439e0 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63  y.** sqlite3_bac
439f0 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e  kup_pagecount().
43a00 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65  .**.** The value
43a10 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  s returned by th
43a20 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  ese functions ar
43a30 65 20 6f 6e 6c 79 20 75 70 64 61 74 65 64 20 62  e only updated b
43a40 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63  y.** sqlite3_bac
43a50 6b 75 70 5f 73 74 65 70 28 29 2e 20 49 66 20 74  kup_step(). If t
43a60 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
43a70 73 65 20 69 73 20 6d 6f 64 69 66 69 65 64 20 64  se is modified d
43a80 75 72 69 6e 67 20 61 20 62 61 63 6b 75 70 0a 2a  uring a backup.*
43a90 2a 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65  * operation, the
43aa0 6e 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65  n the values are
43ab0 20 6e 6f 74 20 75 70 64 61 74 65 64 20 74 6f 20   not updated to 
43ac0 61 63 63 6f 75 6e 74 20 66 6f 72 20 61 6e 79 20  account for any 
43ad0 65 78 74 72 61 0a 2a 2a 20 70 61 67 65 73 20 74  extra.** pages t
43ae0 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20 75  hat need to be u
43af0 70 64 61 74 65 64 20 6f 72 20 74 68 65 20 73 69  pdated or the si
43b00 7a 65 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65  ze of the source
43b10 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a   database file.*
43b20 2a 20 63 68 61 6e 67 69 6e 67 2e 0a 2a 2a 0a 2a  * changing..**.*
43b30 2a 20 3c 62 3e 43 6f 6e 63 75 72 72 65 6e 74 20  * <b>Concurrent 
43b40 55 73 61 67 65 20 6f 66 20 44 61 74 61 62 61 73  Usage of Databas
43b50 65 20 48 61 6e 64 6c 65 73 3c 2f 62 3e 0a 2a 2a  e Handles</b>.**
43b60 0a 2a 2a 20 54 68 65 20 73 6f 75 72 63 65 20 5b  .** The source [
43b70 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
43b80 69 6f 6e 5d 20 6d 61 79 20 62 65 20 75 73 65 64  ion] may be used
43b90 20 62 79 20 74 68 65 20 61 70 70 6c 69 63 61 74   by the applicat
43ba0 69 6f 6e 20 66 6f 72 20 6f 74 68 65 72 0a 2a 2a  ion for other.**
43bb0 20 70 75 72 70 6f 73 65 73 20 77 68 69 6c 65 20   purposes while 
43bc0 61 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  a backup operati
43bd0 6f 6e 20 69 73 20 75 6e 64 65 72 77 61 79 20 6f  on is underway o
43be0 72 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69  r being initiali
43bf0 7a 65 64 2e 0a 2a 2a 20 49 66 20 53 51 4c 69 74  zed..** If SQLit
43c00 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 61 6e  e is compiled an
43c10 64 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20  d configured to 
43c20 73 75 70 70 6f 72 74 20 74 68 72 65 61 64 73 61  support threadsa
43c30 66 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  fe database.** c
43c40 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20 74 68 65 6e  onnections, then
43c50 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
43c60 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
43c70 6d 61 79 20 62 65 20 75 73 65 64 20 63 6f 6e 63  may be used conc
43c80 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 66 72 6f 6d  urrently.** from
43c90 20 77 69 74 68 69 6e 20 6f 74 68 65 72 20 74 68   within other th
43ca0 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 48 6f 77  reads..**.** How
43cb0 65 76 65 72 2c 20 74 68 65 20 61 70 70 6c 69 63  ever, the applic
43cc0 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61  ation must guara
43cd0 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 64 65  ntee that the de
43ce0 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
43cf0 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
43d00 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 70   handle is not p
43d10 61 73 73 65 64 20 74 6f 20 61 6e 79 20 6f 74 68  assed to any oth
43d20 65 72 20 41 50 49 20 28 62 79 20 61 6e 79 20 74  er API (by any t
43d30 68 72 65 61 64 29 20 61 66 74 65 72 20 0a 2a 2a  hread) after .**
43d40 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
43d50 69 6e 69 74 28 29 20 69 73 20 63 61 6c 6c 65 64  init() is called
43d60 20 61 6e 64 20 62 65 66 6f 72 65 20 74 68 65 20   and before the 
43d70 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 63 61  corresponding ca
43d80 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ll to.** sqlite3
43d90 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29  _backup_finish()
43da0 2e 20 55 6e 66 6f 72 74 75 6e 61 74 65 6c 79 20  . Unfortunately 
43db0 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20  SQLite does not 
43dc0 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 0a  currently check.
43dd0 2a 2a 20 66 6f 72 20 74 68 69 73 2c 20 69 66 20  ** for this, if 
43de0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
43df0 64 6f 65 73 20 75 73 65 20 74 68 65 20 64 65 73  does use the des
43e00 74 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61  tination [databa
43e10 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a  se connection].*
43e20 2a 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72  * for some other
43e30 20 70 75 72 70 6f 73 65 20 64 75 72 69 6e 67 20   purpose during 
43e40 61 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  a backup operati
43e50 6f 6e 2c 20 74 68 69 6e 67 73 20 6d 61 79 20 61  on, things may a
43e60 70 70 65 61 72 20 74 6f 0a 2a 2a 20 77 6f 72 6b  ppear to.** work
43e70 20 63 6f 72 72 65 63 74 6c 79 20 62 75 74 20 69   correctly but i
43e80 6e 20 66 61 63 74 20 62 65 20 73 75 62 74 6c 79  n fact be subtly
43e90 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 69 6e 67 2e   malfunctioning.
43ea0 20 20 55 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20    Use of the.** 
43eb0 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
43ec0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
43ed0 77 68 69 6c 65 20 61 20 62 61 63 6b 75 70 20 69  while a backup i
43ee0 73 20 69 6e 20 70 72 6f 67 72 65 73 73 20 6d 69  s in progress mi
43ef0 67 68 74 0a 2a 2a 20 61 6c 73 6f 20 63 61 75 73  ght.** also caus
43f00 65 20 61 20 6d 75 74 65 78 20 64 65 61 64 6c 6f  e a mutex deadlo
43f10 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 75 72 74 68 65  ck..**.** Furthe
43f20 72 6d 6f 72 65 2c 20 69 66 20 72 75 6e 6e 69 6e  rmore, if runnin
43f30 67 20 69 6e 20 5b 73 68 61 72 65 64 20 63 61 63  g in [shared cac
43f40 68 65 20 6d 6f 64 65 5d 2c 20 74 68 65 20 61 70  he mode], the ap
43f50 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 0a 2a  plication must.*
43f60 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74  * guarantee that
43f70 20 74 68 65 20 73 68 61 72 65 64 20 63 61 63 68   the shared cach
43f80 65 20 75 73 65 64 20 62 79 20 74 68 65 20 64 65  e used by the de
43f90 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
43fa0 73 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 63 63  se.** is not acc
43fb0 65 73 73 65 64 20 77 68 69 6c 65 20 74 68 65 20  essed while the 
43fc0 62 61 63 6b 75 70 20 69 73 20 72 75 6e 6e 69 6e  backup is runnin
43fd0 67 2e 20 49 6e 20 70 72 61 63 74 69 63 65 20 74  g. In practice t
43fe0 68 69 73 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61  his means.** tha
43ff0 74 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  t the applicatio
44000 6e 20 6d 75 73 74 20 67 75 61 72 61 6e 74 65 65  n must guarantee
44010 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 2d 73   that the file-s
44020 79 73 74 65 6d 20 66 69 6c 65 20 62 65 69 6e 67  ystem file being
44030 20 0a 2a 2a 20 62 61 63 6b 65 64 20 75 70 20 74   .** backed up t
44040 6f 20 69 73 20 6e 6f 74 20 61 63 63 65 73 73 65  o is not accesse
44050 64 20 62 79 20 61 6e 79 20 63 6f 6e 6e 65 63 74  d by any connect
44060 69 6f 6e 20 77 69 74 68 69 6e 20 74 68 65 20 70  ion within the p
44070 72 6f 63 65 73 73 2c 0a 2a 2a 20 6e 6f 74 20 6a  rocess,.** not j
44080 75 73 74 20 74 68 65 20 73 70 65 63 69 66 69 63  ust the specific
44090 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
440a0 20 77 61 73 20 70 61 73 73 65 64 20 74 6f 20 73   was passed to s
440b0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e  qlite3_backup_in
440c0 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  it()..**.** The 
440d0 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d  [sqlite3_backup]
440e0 20 6f 62 6a 65 63 74 20 69 74 73 65 6c 66 20 69   object itself i
440f0 73 20 70 61 72 74 69 61 6c 6c 79 20 74 68 72 65  s partially thre
44100 61 64 73 61 66 65 2e 20 4d 75 6c 74 69 70 6c 65  adsafe. Multiple
44110 20 0a 2a 2a 20 74 68 72 65 61 64 73 20 6d 61 79   .** threads may
44120 20 73 61 66 65 6c 79 20 6d 61 6b 65 20 6d 75 6c   safely make mul
44130 74 69 70 6c 65 20 63 6f 6e 63 75 72 72 65 6e 74  tiple concurrent
44140 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
44150 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e  3_backup_step().
44160 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65  .** However, the
44170 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
44180 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e 64 20  remaining() and 
44190 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 70  sqlite3_backup_p
441a0 61 67 65 63 6f 75 6e 74 28 29 0a 2a 2a 20 41 50  agecount().** AP
441b0 49 73 20 61 72 65 20 6e 6f 74 20 73 74 72 69 63  Is are not stric
441c0 74 6c 79 20 73 70 65 61 6b 69 6e 67 20 74 68 72  tly speaking thr
441d0 65 61 64 73 61 66 65 2e 20 49 66 20 74 68 65 79  eadsafe. If they
441e0 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 61 74 20   are invoked at 
441f0 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 69 6d 65  the.** same time
44200 20 61 73 20 61 6e 6f 74 68 65 72 20 74 68 72 65   as another thre
44210 61 64 20 69 73 20 69 6e 76 6f 6b 69 6e 67 20 73  ad is invoking s
44220 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
44230 65 70 28 29 20 69 74 20 69 73 0a 2a 2a 20 70 6f  ep() it is.** po
44240 73 73 69 62 6c 65 20 74 68 61 74 20 74 68 65 79  ssible that they
44250 20 72 65 74 75 72 6e 20 69 6e 76 61 6c 69 64 20   return invalid 
44260 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  values..*/.SQLIT
44270 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 62 61  E_API sqlite3_ba
44280 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f 62 61  ckup *sqlite3_ba
44290 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73 71 6c  ckup_init(.  sql
442a0 69 74 65 33 20 2a 70 44 65 73 74 2c 20 20 20 20  ite3 *pDest,    
442b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
442c0 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69      /* Destinati
442d0 6f 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  on database hand
442e0 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  le */.  const ch
442f0 61 72 20 2a 7a 44 65 73 74 4e 61 6d 65 2c 20 20  ar *zDestName,  
44300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
44310 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  * Destination da
44320 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20  tabase name */. 
44330 20 73 71 6c 69 74 65 33 20 2a 70 53 6f 75 72 63   sqlite3 *pSourc
44340 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
44350 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63          /* Sourc
44360 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
44370 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
44380 72 20 2a 7a 53 6f 75 72 63 65 4e 61 6d 65 20 20  r *zSourceName  
44390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
443a0 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   Source database
443b0 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49   name */.);.SQLI
443c0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
443d0 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 73  e3_backup_step(s
443e0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70  qlite3_backup *p
443f0 2c 20 69 6e 74 20 6e 50 61 67 65 29 3b 0a 53 51  , int nPage);.SQ
44400 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
44410 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
44420 73 68 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  sh(sqlite3_backu
44430 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50  p *p);.SQLITE_AP
44440 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61  I int sqlite3_ba
44450 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 73  ckup_remaining(s
44460 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70  qlite3_backup *p
44470 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
44480 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t sqlite3_backup
44490 5f 70 61 67 65 63 6f 75 6e 74 28 73 71 6c 69 74  _pagecount(sqlit
444a0 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 3b 0a 0a  e3_backup *p);..
444b0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
444c0 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61 74  Unlock Notificat
444d0 69 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  ion.** EXPERIMEN
444e0 54 41 4c 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 72  TAL.**.** When r
444f0 75 6e 6e 69 6e 67 20 69 6e 20 73 68 61 72 65 64  unning in shared
44500 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 61 20 64  -cache mode, a d
44510 61 74 61 62 61 73 65 20 6f 70 65 72 61 74 69 6f  atabase operatio
44520 6e 20 6d 61 79 20 66 61 69 6c 20 77 69 74 68 0a  n may fail with.
44530 2a 2a 20 61 6e 20 5b 53 51 4c 49 54 45 5f 4c 4f  ** an [SQLITE_LO
44540 43 4b 45 44 5d 20 65 72 72 6f 72 20 69 66 20 74  CKED] error if t
44550 68 65 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b  he required lock
44560 73 20 6f 6e 20 74 68 65 20 73 68 61 72 65 64 2d  s on the shared-
44570 63 61 63 68 65 20 6f 72 0a 2a 2a 20 69 6e 64 69  cache or.** indi
44580 76 69 64 75 61 6c 20 74 61 62 6c 65 73 20 77 69  vidual tables wi
44590 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 2d  thin the shared-
445a0 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65 20  cache cannot be 
445b0 6f 62 74 61 69 6e 65 64 2e 20 53 65 65 0a 2a 2a  obtained. See.**
445c0 20 5b 53 51 4c 69 74 65 20 53 68 61 72 65 64 2d   [SQLite Shared-
445d0 43 61 63 68 65 20 4d 6f 64 65 5d 20 66 6f 72 20  Cache Mode] for 
445e0 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  a description of
445f0 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 6f   shared-cache lo
44600 63 6b 69 6e 67 2e 20 0a 2a 2a 20 54 68 69 73 20  cking. .** This 
44610 41 50 49 20 6d 61 79 20 62 65 20 75 73 65 64 20  API may be used 
44620 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 63 61  to register a ca
44630 6c 6c 62 61 63 6b 20 74 68 61 74 20 53 51 4c 69  llback that SQLi
44640 74 65 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 0a  te will invoke .
44650 2a 2a 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 6e  ** when the conn
44660 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79  ection currently
44670 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 71   holding the req
44680 75 69 72 65 64 20 6c 6f 63 6b 20 72 65 6c 69 6e  uired lock relin
44690 71 75 69 73 68 65 73 20 69 74 2e 0a 2a 2a 20 54  quishes it..** T
446a0 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79 20  his API is only 
446b0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65  available if the
446c0 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d   library was com
446d0 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a  piled with the.*
446e0 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  * [SQLITE_ENABLE
446f0 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 5d 20  _UNLOCK_NOTIFY] 
44700 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20 73  C-preprocessor s
44710 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a 2a  ymbol defined..*
44720 2a 0a 2a 2a 20 53 65 65 20 41 6c 73 6f 3a 20 5b  *.** See Also: [
44730 55 73 69 6e 67 20 74 68 65 20 53 51 4c 69 74 65  Using the SQLite
44740 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66 69 63 61   Unlock Notifica
44750 74 69 6f 6e 20 46 65 61 74 75 72 65 5d 2e 0a 2a  tion Feature]..*
44760 2a 0a 2a 2a 20 53 68 61 72 65 64 2d 63 61 63 68  *.** Shared-cach
44770 65 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65  e locks are rele
44780 61 73 65 64 20 77 68 65 6e 20 61 20 64 61 74 61  ased when a data
44790 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
447a0 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a 20 69 74 73  concludes.** its
447b0 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
447c0 74 69 6f 6e 2c 20 65 69 74 68 65 72 20 62 79 20  tion, either by 
447d0 63 6f 6d 6d 69 74 74 69 6e 67 20 69 74 20 6f 72  committing it or
447e0 20 72 6f 6c 6c 69 6e 67 20 69 74 20 62 61 63 6b   rolling it back
447f0 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20  . .**.** When a 
44800 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 6b 6e 6f 77  connection (know
44810 6e 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 65 64  n as the blocked
44820 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 66 61 69   connection) fai
44830 6c 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 0a 2a  ls to obtain a.*
44840 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c  * shared-cache l
44850 6f 63 6b 20 61 6e 64 20 53 51 4c 49 54 45 5f 4c  ock and SQLITE_L
44860 4f 43 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65  OCKED is returne
44870 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2c  d to the caller,
44880 20 74 68 65 0a 2a 2a 20 69 64 65 6e 74 69 74 79   the.** identity
44890 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
448a0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 74 68 65   connection (the
448b0 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63   blocking connec
448c0 74 69 6f 6e 29 20 74 68 61 74 0a 2a 2a 20 68 61  tion) that.** ha
448d0 73 20 6c 6f 63 6b 65 64 20 74 68 65 20 72 65 71  s locked the req
448e0 75 69 72 65 64 20 72 65 73 6f 75 72 63 65 20 69  uired resource i
448f0 73 20 73 74 6f 72 65 64 20 69 6e 74 65 72 6e 61  s stored interna
44900 6c 6c 79 2e 20 41 66 74 65 72 20 61 6e 20 0a 2a  lly. After an .*
44910 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 72 65  * application re
44920 63 65 69 76 65 73 20 61 6e 20 53 51 4c 49 54 45  ceives an SQLITE
44930 5f 4c 4f 43 4b 45 44 20 65 72 72 6f 72 2c 20 69  _LOCKED error, i
44940 74 20 6d 61 79 20 63 61 6c 6c 20 74 68 65 0a 2a  t may call the.*
44950 2a 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b  * sqlite3_unlock
44960 5f 6e 6f 74 69 66 79 28 29 20 6d 65 74 68 6f 64  _notify() method
44970 20 77 69 74 68 20 74 68 65 20 62 6c 6f 63 6b 65   with the blocke
44980 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  d connection han
44990 64 6c 65 20 61 73 20 0a 2a 2a 20 74 68 65 20 66  dle as .** the f
449a0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
449b0 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 61 20   register for a 
449c0 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 77 69  callback that wi
449d0 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a  ll be invoked.**
449e0 20 77 68 65 6e 20 74 68 65 20 62 6c 6f 63 6b 69   when the blocki
449f0 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63  ng connections c
44a00 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
44a10 6f 6e 20 69 73 20 63 6f 6e 63 6c 75 64 65 64 2e  on is concluded.
44a20 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b   The.** callback
44a30 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 72 6f 6d   is invoked from
44a40 20 77 69 74 68 69 6e 20 74 68 65 20 5b 73 71 6c   within the [sql
44a50 69 74 65 33 5f 73 74 65 70 5d 20 6f 72 20 5b 73  ite3_step] or [s
44a60 71 6c 69 74 65 33 5f 63 6c 6f 73 65 5d 0a 2a 2a  qlite3_close].**
44a70 20 63 61 6c 6c 20 74 68 61 74 20 63 6f 6e 63 6c   call that concl
44a80 75 64 65 73 20 74 68 65 20 62 6c 6f 63 6b 69 6e  udes the blockin
44a90 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72  g connections tr
44aa0 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ansaction..**.**
44ab0 20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f   If sqlite3_unlo
44ac0 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63  ck_notify() is c
44ad0 61 6c 6c 65 64 20 69 6e 20 61 20 6d 75 6c 74 69  alled in a multi
44ae0 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63  -threaded applic
44af0 61 74 69 6f 6e 2c 0a 2a 2a 20 74 68 65 72 65 20  ation,.** there 
44b00 69 73 20 61 20 63 68 61 6e 63 65 20 74 68 61 74  is a chance that
44b10 20 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f   the blocking co
44b20 6e 6e 65 63 74 69 6f 6e 20 77 69 6c 6c 20 68 61  nnection will ha
44b30 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a 20 63 6f  ve already.** co
44b40 6e 63 6c 75 64 65 64 20 69 74 73 20 74 72 61 6e  ncluded its tran
44b50 73 61 63 74 69 6f 6e 20 62 79 20 74 68 65 20 74  saction by the t
44b60 69 6d 65 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f  ime sqlite3_unlo
44b70 63 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 69  ck_notify() is i
44b80 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68  nvoked..** If th
44b90 69 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e  is happens, then
44ba0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63   the specified c
44bb0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
44bc0 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 0a  ed immediately,.
44bd0 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74  ** from within t
44be0 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
44bf0 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79  e3_unlock_notify
44c00 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ()..**.** If the
44c10 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
44c20 69 6f 6e 20 69 73 20 61 74 74 65 6d 70 74 69 6e  ion is attemptin
44c30 67 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 77 72  g to obtain a wr
44c40 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 0a 2a 2a  ite-lock on a.**
44c50 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 74 61   shared-cache ta
44c60 62 6c 65 2c 20 61 6e 64 20 6d 6f 72 65 20 74 68  ble, and more th
44c70 61 6e 20 6f 6e 65 20 6f 74 68 65 72 20 63 6f 6e  an one other con
44c80 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c  nection currentl
44c90 79 20 68 6f 6c 64 73 0a 2a 2a 20 61 20 72 65 61  y holds.** a rea
44ca0 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 73 61  d-lock on the sa
44cb0 6d 65 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 53  me table, then S
44cc0 51 4c 69 74 65 20 61 72 62 69 74 72 61 72 69 6c  QLite arbitraril
44cd0 79 20 73 65 6c 65 63 74 73 20 6f 6e 65 20 6f 66  y selects one of
44ce0 20 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 63   .** the other c
44cf0 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 75 73  onnections to us
44d00 65 20 61 73 20 74 68 65 20 62 6c 6f 63 6b 69 6e  e as the blockin
44d10 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a  g connection..**
44d20 0a 2a 2a 20 54 68 65 72 65 20 6d 61 79 20 62 65  .** There may be
44d30 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 75 6e 6c   at most one unl
44d40 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
44d50 61 63 6b 20 72 65 67 69 73 74 65 72 65 64 20 62  ack registered b
44d60 79 20 61 20 0a 2a 2a 20 62 6c 6f 63 6b 65 64 20  y a .** blocked 
44d70 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66 20 73  connection. If s
44d80 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
44d90 74 69 66 79 28 29 20 69 73 20 63 61 6c 6c 65 64  tify() is called
44da0 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 62 6c 6f   when the.** blo
44db0 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  cked connection 
44dc0 61 6c 72 65 61 64 79 20 68 61 73 20 61 20 72 65  already has a re
44dd0 67 69 73 74 65 72 65 64 20 75 6e 6c 6f 63 6b 2d  gistered unlock-
44de0 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 2c  notify callback,
44df0 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 6e 65 77  .** then the new
44e00 20 63 61 6c 6c 62 61 63 6b 20 72 65 70 6c 61 63   callback replac
44e10 65 73 20 74 68 65 20 6f 6c 64 2e 20 49 66 20 73  es the old. If s
44e20 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
44e30 74 69 66 79 28 29 20 69 73 0a 2a 2a 20 63 61 6c  tify() is.** cal
44e40 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  led with a NULL 
44e50 70 6f 69 6e 74 65 72 20 61 73 20 69 74 73 20 73  pointer as its s
44e60 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20  econd argument, 
44e70 74 68 65 6e 20 61 6e 79 20 65 78 69 73 74 69 6e  then any existin
44e80 67 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69  g.** unlock-noti
44e90 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63  fy callback is c
44ea0 61 6e 63 65 6c 6c 65 64 2e 20 54 68 65 20 62 6c  ancelled. The bl
44eb0 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
44ec0 73 20 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  s .** unlock-not
44ed0 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79  ify callback may
44ee0 20 61 6c 73 6f 20 62 65 20 63 61 6e 63 65 6c 65   also be cancele
44ef0 64 20 62 79 20 63 6c 6f 73 69 6e 67 20 74 68 65  d by closing the
44f00 20 62 6c 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 6e   blocked.** conn
44f10 65 63 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73 71  ection using [sq
44f20 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e 0a  lite3_close()]..
44f30 2a 2a 0a 2a 2a 20 54 68 65 20 75 6e 6c 6f 63 6b  **.** The unlock
44f40 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
44f50 20 69 73 20 6e 6f 74 20 72 65 65 6e 74 72 61 6e   is not reentran
44f60 74 2e 20 49 66 20 61 6e 20 61 70 70 6c 69 63 61  t. If an applica
44f70 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20  tion invokes.** 
44f80 61 6e 79 20 73 71 6c 69 74 65 33 5f 78 78 78 20  any sqlite3_xxx 
44f90 41 50 49 20 66 75 6e 63 74 69 6f 6e 73 20 66 72  API functions fr
44fa0 6f 6d 20 77 69 74 68 69 6e 20 61 6e 20 75 6e 6c  om within an unl
44fb0 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
44fc0 61 63 6b 2c 20 61 0a 2a 2a 20 63 72 61 73 68 20  ack, a.** crash 
44fd0 6f 72 20 64 65 61 64 6c 6f 63 6b 20 6d 61 79 20  or deadlock may 
44fe0 62 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a  be the result..*
44ff0 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 64 65 61 64  *.** Unless dead
45000 6c 6f 63 6b 20 69 73 20 64 65 74 65 63 74 65 64  lock is detected
45010 20 28 73 65 65 20 62 65 6c 6f 77 29 2c 20 73 71   (see below), sq
45020 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
45030 69 66 79 28 29 20 61 6c 77 61 79 73 0a 2a 2a 20  ify() always.** 
45040 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
45050 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 43 61 6c 6c  K..**.** <b>Call
45060 62 61 63 6b 20 49 6e 76 6f 63 61 74 69 6f 6e 20  back Invocation 
45070 44 65 74 61 69 6c 73 3c 2f 62 3e 0a 2a 2a 0a 2a  Details</b>.**.*
45080 2a 20 57 68 65 6e 20 61 6e 20 75 6e 6c 6f 63 6b  * When an unlock
45090 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
450a0 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2c 20   is registered, 
450b0 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
450c0 70 72 6f 76 69 64 65 73 20 61 20 0a 2a 2a 20 73  provides a .** s
450d0 69 6e 67 6c 65 20 76 6f 69 64 2a 20 70 6f 69 6e  ingle void* poin
450e0 74 65 72 20 74 68 61 74 20 69 73 20 70 61 73 73  ter that is pass
450f0 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  ed to the callba
45100 63 6b 20 77 68 65 6e 20 69 74 20 69 73 20 69 6e  ck when it is in
45110 76 6f 6b 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65  voked..** Howeve
45120 72 2c 20 74 68 65 20 73 69 67 6e 61 74 75 72 65  r, the signature
45130 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   of the callback
45140 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 77 73   function allows
45150 20 53 51 4c 69 74 65 20 74 6f 20 70 61 73 73 0a   SQLite to pass.
45160 2a 2a 20 69 74 20 61 6e 20 61 72 72 61 79 20 6f  ** it an array o
45170 66 20 76 6f 69 64 2a 20 63 6f 6e 74 65 78 74 20  f void* context 
45180 70 6f 69 6e 74 65 72 73 2e 20 54 68 65 20 66 69  pointers. The fi
45190 72 73 74 20 61 72 67 75 6d 65 6e 74 20 70 61 73  rst argument pas
451a0 73 65 64 20 74 6f 0a 2a 2a 20 61 6e 20 75 6e 6c  sed to.** an unl
451b0 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
451c0 61 63 6b 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ack is a pointer
451d0 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20   to an array of 
451e0 76 6f 69 64 2a 20 70 6f 69 6e 74 65 72 73 2c 0a  void* pointers,.
451f0 2a 2a 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  ** and the secon
45200 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  d is the number 
45210 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  of entries in th
45220 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 57  e array..**.** W
45230 68 65 6e 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63  hen a blocking c
45240 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 72 61 6e 73  onnections trans
45250 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75  action is conclu
45260 64 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62  ded, there may b
45270 65 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f  e.** more than o
45280 6e 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65  ne blocked conne
45290 63 74 69 6f 6e 20 74 68 61 74 20 68 61 73 20 72  ction that has r
452a0 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e  egistered for an
452b0 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 0a 2a   unlock-notify.*
452c0 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 49 66 20 74  * callback. If t
452d0 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75 63 68 20  wo or more such 
452e0 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
452f0 6f 6e 73 20 68 61 76 65 20 73 70 65 63 69 66 69  ons have specifi
45300 65 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63  ed the.** same c
45310 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
45320 2c 20 74 68 65 6e 20 69 6e 73 74 65 61 64 20 6f  , then instead o
45330 66 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63  f invoking the c
45340 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
45350 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  .** multiple tim
45360 65 73 2c 20 69 74 20 69 73 20 69 6e 76 6f 6b 65  es, it is invoke
45370 64 20 6f 6e 63 65 20 77 69 74 68 20 74 68 65 20  d once with the 
45380 73 65 74 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e  set of void* con
45390 74 65 78 74 20 70 6f 69 6e 74 65 72 73 0a 2a 2a  text pointers.**
453a0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68   specified by th
453b0 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  e blocked connec
453c0 74 69 6f 6e 73 20 62 75 6e 64 6c 65 64 20 74 6f  tions bundled to
453d0 67 65 74 68 65 72 20 69 6e 74 6f 20 61 6e 20 61  gether into an a
453e0 72 72 61 79 2e 0a 2a 2a 20 54 68 69 73 20 67 69  rray..** This gi
453f0 76 65 73 20 74 68 65 20 61 70 70 6c 69 63 61 74  ves the applicat
45400 69 6f 6e 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69  ion an opportuni
45410 74 79 20 74 6f 20 70 72 69 6f 72 69 74 69 7a 65  ty to prioritize
45420 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 0a 2a 2a   any actions .**
45430 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68 65 20   related to the 
45440 73 65 74 20 6f 66 20 75 6e 62 6c 6f 63 6b 65 64  set of unblocked
45450 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
45460 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e  tions..**.** <b>
45470 44 65 61 64 6c 6f 63 6b 20 44 65 74 65 63 74 69  Deadlock Detecti
45480 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 41 73 73  on</b>.**.** Ass
45490 75 6d 69 6e 67 20 74 68 61 74 20 61 66 74 65 72  uming that after
454a0 20 72 65 67 69 73 74 65 72 69 6e 67 20 66 6f 72   registering for
454b0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
454c0 79 20 63 61 6c 6c 62 61 63 6b 20 61 20 0a 2a 2a  y callback a .**
454d0 20 64 61 74 61 62 61 73 65 20 77 61 69 74 73 20   database waits 
454e0 66 6f 72 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  for the callback
454f0 20 74 6f 20 62 65 20 69 73 73 75 65 64 20 62 65   to be issued be
45500 66 6f 72 65 20 74 61 6b 69 6e 67 20 61 6e 79 20  fore taking any 
45510 66 75 72 74 68 65 72 0a 2a 2a 20 61 63 74 69 6f  further.** actio
45520 6e 20 28 61 20 72 65 61 73 6f 6e 61 62 6c 65 20  n (a reasonable 
45530 61 73 73 75 6d 70 74 69 6f 6e 29 2c 20 74 68 65  assumption), the
45540 6e 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49  n using this API
45550 20 6d 61 79 20 63 61 75 73 65 20 74 68 65 0a 2a   may cause the.*
45560 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f  * application to
45570 20 64 65 61 64 6c 6f 63 6b 2e 20 46 6f 72 20 65   deadlock. For e
45580 78 61 6d 70 6c 65 2c 20 69 66 20 63 6f 6e 6e 65  xample, if conne
45590 63 74 69 6f 6e 20 58 20 69 73 20 77 61 69 74 69  ction X is waiti
455a0 6e 67 20 66 6f 72 0a 2a 2a 20 63 6f 6e 6e 65 63  ng for.** connec
455b0 74 69 6f 6e 20 59 27 73 20 74 72 61 6e 73 61 63  tion Y's transac
455c0 74 69 6f 6e 20 74 6f 20 62 65 20 63 6f 6e 63 6c  tion to be concl
455d0 75 64 65 64 2c 20 61 6e 64 20 73 69 6d 69 6c 61  uded, and simila
455e0 72 6c 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  rly connection.*
455f0 2a 20 59 20 69 73 20 77 61 69 74 69 6e 67 20 6f  * Y is waiting o
45600 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 58 27 73  n connection X's
45610 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68   transaction, th
45620 65 6e 20 6e 65 69 74 68 65 72 20 63 6f 6e 6e 65  en neither conne
45630 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 70 72  ction.** will pr
45640 6f 63 65 65 64 20 61 6e 64 20 74 68 65 20 73 79  oceed and the sy
45650 73 74 65 6d 20 6d 61 79 20 72 65 6d 61 69 6e 20  stem may remain 
45660 64 65 61 64 6c 6f 63 6b 65 64 20 69 6e 64 65 66  deadlocked indef
45670 69 6e 69 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  initely..**.** T
45680 6f 20 61 76 6f 69 64 20 74 68 69 73 20 73 63 65  o avoid this sce
45690 6e 61 72 69 6f 2c 20 74 68 65 20 73 71 6c 69 74  nario, the sqlit
456a0 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79  e3_unlock_notify
456b0 28 29 20 70 65 72 66 6f 72 6d 73 20 64 65 61 64  () performs dead
456c0 6c 6f 63 6b 0a 2a 2a 20 64 65 74 65 63 74 69 6f  lock.** detectio
456d0 6e 2e 20 49 66 20 61 20 67 69 76 65 6e 20 63 61  n. If a given ca
456e0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 6e  ll to sqlite3_un
456f0 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 77 6f  lock_notify() wo
45700 75 6c 64 20 70 75 74 20 74 68 65 0a 2a 2a 20 73  uld put the.** s
45710 79 73 74 65 6d 20 69 6e 20 61 20 64 65 61 64 6c  ystem in a deadl
45720 6f 63 6b 65 64 20 73 74 61 74 65 2c 20 74 68 65  ocked state, the
45730 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  n SQLITE_LOCKED 
45740 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
45750 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  no.** unlock-not
45760 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ify callback is 
45770 72 65 67 69 73 74 65 72 65 64 2e 20 54 68 65 20  registered. The 
45780 73 79 73 74 65 6d 20 69 73 20 73 61 69 64 20 74  system is said t
45790 6f 20 62 65 20 69 6e 0a 2a 2a 20 61 20 64 65 61  o be in.** a dea
457a0 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 20 69 66  dlocked state if
457b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 41 20 68 61   connection A ha
457c0 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72  s registered for
457d0 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
457e0 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e  y.** callback on
457f0 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20   the conclusion 
45800 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42 27  of connection B'
45810 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61  s transaction, a
45820 6e 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  nd connection.**
45830 20 42 20 68 61 73 20 69 74 73 65 6c 66 20 72 65   B has itself re
45840 67 69 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20  gistered for an 
45850 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
45860 6c 6c 62 61 63 6b 20 77 68 65 6e 20 63 6f 6e 6e  llback when conn
45870 65 63 74 69 6f 6e 0a 2a 2a 20 41 27 73 20 74 72  ection.** A's tr
45880 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e  ansaction is con
45890 63 6c 75 64 65 64 2e 20 49 6e 64 69 72 65 63 74  cluded. Indirect
458a0 20 64 65 61 64 6c 6f 63 6b 20 69 73 20 61 6c 73   deadlock is als
458b0 6f 20 64 65 74 65 63 74 65 64 2c 20 73 6f 0a 2a  o detected, so.*
458c0 2a 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20  * the system is 
458d0 61 6c 73 6f 20 63 6f 6e 73 69 64 65 72 65 64 20  also considered 
458e0 74 6f 20 62 65 20 64 65 61 64 6c 6f 63 6b 65 64  to be deadlocked
458f0 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 42   if connection B
45900 20 68 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72   has.** register
45910 65 64 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b  ed for an unlock
45920 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
45930 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73 69   on the conclusi
45940 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f 6e  on of connection
45950 0a 2a 2a 20 43 27 73 20 74 72 61 6e 73 61 63 74  .** C's transact
45960 69 6f 6e 2c 20 77 68 65 72 65 20 63 6f 6e 6e 65  ion, where conne
45970 63 74 69 6f 6e 20 43 20 69 73 20 77 61 69 74 69  ction C is waiti
45980 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e  ng on connection
45990 20 41 2e 20 41 6e 79 0a 2a 2a 20 6e 75 6d 62 65   A. Any.** numbe
459a0 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20 69  r of levels of i
459b0 6e 64 69 72 65 63 74 69 6f 6e 20 61 72 65 20 61  ndirection are a
459c0 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62  llowed..**.** <b
459d0 3e 54 68 65 20 22 44 52 4f 50 20 54 41 42 4c 45  >The "DROP TABLE
459e0 22 20 45 78 63 65 70 74 69 6f 6e 3c 2f 62 3e 0a  " Exception</b>.
459f0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61 6c  **.** When a cal
45a00 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  l to [sqlite3_st
45a10 65 70 28 29 5d 20 72 65 74 75 72 6e 73 20 53 51  ep()] returns SQ
45a20 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20 69 74 20  LITE_LOCKED, it 
45a30 69 73 20 61 6c 6d 6f 73 74 20 0a 2a 2a 20 61 6c  is almost .** al
45a40 77 61 79 73 20 61 70 70 72 6f 70 72 69 61 74 65  ways appropriate
45a50 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33   to call sqlite3
45a60 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
45a70 2e 20 54 68 65 72 65 20 69 73 20 68 6f 77 65 76  . There is howev
45a80 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65 78 63 65 70  er,.** one excep
45a90 74 69 6f 6e 2e 20 57 68 65 6e 20 65 78 65 63 75  tion. When execu
45aa0 74 69 6e 67 20 61 20 22 44 52 4f 50 20 54 41 42  ting a "DROP TAB
45ab0 4c 45 22 20 6f 72 20 22 44 52 4f 50 20 49 4e 44  LE" or "DROP IND
45ac0 45 58 22 20 73 74 61 74 65 6d 65 6e 74 2c 0a 2a  EX" statement,.*
45ad0 2a 20 53 51 4c 69 74 65 20 63 68 65 63 6b 73 20  * SQLite checks 
45ae0 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
45af0 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75   currently execu
45b00 74 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74  ting SELECT stat
45b10 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 62  ements.** that b
45b20 65 6c 6f 6e 67 20 74 6f 20 74 68 65 20 73 61 6d  elong to the sam
45b30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 66  e connection. If
45b40 20 74 68 65 72 65 20 61 72 65 2c 20 53 51 4c 49   there are, SQLI
45b50 54 45 5f 4c 4f 43 4b 45 44 20 69 73 0a 2a 2a 20  TE_LOCKED is.** 
45b60 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69  returned. In thi
45b70 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20  s case there is 
45b80 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e  no "blocking con
45b90 6e 65 63 74 69 6f 6e 22 2c 20 73 6f 20 69 6e 76  nection", so inv
45ba0 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  oking.** sqlite3
45bb0 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
45bc0 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20   results in the 
45bd0 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
45be0 6c 6c 62 61 63 6b 20 62 65 69 6e 67 0a 2a 2a 20  llback being.** 
45bf0 69 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74  invoked immediat
45c00 65 6c 79 2e 20 49 66 20 74 68 65 20 61 70 70 6c  ely. If the appl
45c10 69 63 61 74 69 6f 6e 20 74 68 65 6e 20 72 65 2d  ication then re-
45c20 61 74 74 65 6d 70 74 73 20 74 68 65 20 22 44 52  attempts the "DR
45c30 4f 50 20 54 41 42 4c 45 22 0a 2a 2a 20 6f 72 20  OP TABLE".** or 
45c40 22 44 52 4f 50 20 49 4e 44 45 58 22 20 71 75 65  "DROP INDEX" que
45c50 72 79 2c 20 61 6e 20 69 6e 66 69 6e 69 74 65 20  ry, an infinite 
45c60 6c 6f 6f 70 20 6d 69 67 68 74 20 62 65 20 74 68  loop might be th
45c70 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20  e result..**.** 
45c80 4f 6e 65 20 77 61 79 20 61 72 6f 75 6e 64 20 74  One way around t
45c90 68 69 73 20 70 72 6f 62 6c 65 6d 20 69 73 20 74  his problem is t
45ca0 6f 20 63 68 65 63 6b 20 74 68 65 20 65 78 74 65  o check the exte
45cb0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20  nded error code 
45cc0 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 61  returned.** by a
45cd0 6e 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  n sqlite3_step()
45ce0 20 63 61 6c 6c 2e 20 49 66 20 74 68 65 72 65 20   call. If there 
45cf0 69 73 20 61 20 62 6c 6f 63 6b 69 6e 67 20 63 6f  is a blocking co
45d00 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74  nnection, then t
45d10 68 65 0a 2a 2a 20 65 78 74 65 6e 64 65 64 20 65  he.** extended e
45d20 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74  rror code is set
45d30 20 74 6f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45   to SQLITE_LOCKE
45d40 44 5f 53 48 41 52 45 44 43 41 43 48 45 2e 20 4f  D_SHAREDCACHE. O
45d50 74 68 65 72 77 69 73 65 2c 20 69 6e 0a 2a 2a 20  therwise, in.** 
45d60 74 68 65 20 73 70 65 63 69 61 6c 20 22 44 52 4f  the special "DRO
45d70 50 20 54 41 42 4c 45 2f 49 4e 44 45 58 22 20 63  P TABLE/INDEX" c
45d80 61 73 65 2c 20 74 68 65 20 65 78 74 65 6e 64 65  ase, the extende
45d90 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  d error code is 
45da0 6a 75 73 74 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  just .** SQLITE_
45db0 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54  LOCKED..*/.SQLIT
45dc0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
45dd0 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28  3_unlock_notify(
45de0 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f  .  sqlite3 *pBlo
45df0 63 6b 65 64 2c 20 20 20 20 20 20 20 20 20 20 20  cked,           
45e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
45e10 2a 20 57 61 69 74 69 6e 67 20 63 6f 6e 6e 65 63  * Waiting connec
45e20 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28  tion */.  void (
45e30 2a 78 4e 6f 74 69 66 79 29 28 76 6f 69 64 20 2a  *xNotify)(void *
45e40 2a 61 70 41 72 67 2c 20 69 6e 74 20 6e 41 72 67  *apArg, int nArg
45e50 29 2c 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63  ),    /* Callbac
45e60 6b 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e  k function to in
45e70 76 6f 6b 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  voke */.  void *
45e80 70 4e 6f 74 69 66 79 41 72 67 20 20 20 20 20 20  pNotifyArg      
45e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45ea0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
45eb0 74 20 74 6f 20 70 61 73 73 20 74 6f 20 78 4e 6f  t to pass to xNo
45ec0 74 69 66 79 20 2a 2f 0a 29 3b 0a 0a 0a 2f 2a 0a  tify */.);.../*.
45ed0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 74 72  ** CAPI3REF: Str
45ee0 69 6e 67 20 43 6f 6d 70 61 72 69 73 6f 6e 0a 2a  ing Comparison.*
45ef0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
45f00 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65  *.** The [sqlite
45f10 33 5f 73 74 72 6e 69 63 6d 70 28 29 5d 20 41 50  3_strnicmp()] AP
45f20 49 20 61 6c 6c 6f 77 73 20 61 70 70 6c 69 63 61  I allows applica
45f30 74 69 6f 6e 73 20 61 6e 64 20 65 78 74 65 6e 73  tions and extens
45f40 69 6f 6e 73 20 74 6f 0a 2a 2a 20 63 6f 6d 70 61  ions to.** compa
45f50 72 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  re the contents 
45f60 6f 66 20 74 77 6f 20 62 75 66 66 65 72 73 20 63  of two buffers c
45f70 6f 6e 74 61 69 6e 69 6e 67 20 55 54 46 2d 38 20  ontaining UTF-8 
45f80 73 74 72 69 6e 67 73 20 69 6e 20 61 0a 2a 2a 20  strings in a.** 
45f90 63 61 73 65 2d 69 6e 64 65 6e 64 65 6e 74 20 66  case-indendent f
45fa0 61 73 68 69 6f 6e 2c 20 75 73 69 6e 67 20 74 68  ashion, using th
45fb0 65 20 73 61 6d 65 20 64 65 66 69 6e 69 74 69 6f  e same definitio
45fc0 6e 20 6f 66 20 63 61 73 65 20 69 6e 64 65 70 65  n of case indepe
45fd0 6e 64 65 6e 63 65 20 0a 2a 2a 20 74 68 61 74 20  ndence .** that 
45fe0 53 51 4c 69 74 65 20 75 73 65 73 20 69 6e 74 65  SQLite uses inte
45ff0 72 6e 61 6c 6c 79 20 77 68 65 6e 20 63 6f 6d 70  rnally when comp
46000 61 72 69 6e 67 20 69 64 65 6e 74 69 66 69 65 72  aring identifier
46010 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
46020 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 72   int sqlite3_str
46030 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  nicmp(const char
46040 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
46050 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 55  , int);../*.** U
46060 6e 64 6f 20 74 68 65 20 68 61 63 6b 20 74 68 61  ndo the hack tha
46070 74 20 63 6f 6e 76 65 72 74 73 20 66 6c 6f 61 74  t converts float
46080 69 6e 67 20 70 6f 69 6e 74 20 74 79 70 65 73 20  ing point types 
46090 74 6f 20 69 6e 74 65 67 65 72 20 66 6f 72 0a 2a  to integer for.*
460a0 2a 20 62 75 69 6c 64 73 20 6f 6e 20 70 72 6f 63  * builds on proc
460b0 65 73 73 6f 72 73 20 77 69 74 68 6f 75 74 20 66  essors without f
460c0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75  loating point su
460d0 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64 65 66  pport..*/.#ifdef
460e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
460f0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 75 6e  ATING_POINT.# un
46100 64 65 66 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69  def double.#endi
46110 66 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 45  f..#if 0.}  /* E
46120 6e 64 20 6f 66 20 74 68 65 20 27 65 78 74 65 72  nd of the 'exter
46130 6e 20 22 43 22 27 20 62 6c 6f 63 6b 20 2a 2f 0a  n "C"' block */.
46140 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 0a  #endif.#endif...
46150 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
46160 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 2e 68  End of sqlite3.h
46170 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
46180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
461a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
461b0 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
461c0 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
461d0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
461e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
461f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
46200 49 6e 63 6c 75 64 65 20 68 61 73 68 2e 68 20 69  Include hash.h i
46210 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
46220 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
46230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
46240 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
46250 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e  Begin file hash.
46260 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
46270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
46290 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
462a0 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  mber 22.**.** Th
462b0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
462c0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
462d0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
462e0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
462f0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
46300 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
46310 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
46320 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
46330 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
46340 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
46350 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
46360 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
46370 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
46380 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
46390 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
463a0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
463b0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
463c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46400 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ******.** This i
46410 73 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c  s the header fil
46420 65 20 66 6f 72 20 74 68 65 20 67 65 6e 65 72 69  e for the generi
46430 63 20 68 61 73 68 2d 74 61 62 6c 65 20 69 6d 70  c hash-table imp
46440 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73  lemenation.** us
46450 65 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f  ed in SQLite..*/
46460 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45  .#ifndef _SQLITE
46470 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e 65  _HASH_H_.#define
46480 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f   _SQLITE_HASH_H_
46490 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
464a0 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72  larations of str
464b0 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65  uctures. */.type
464c0 64 65 66 20 73 74 72 75 63 74 20 48 61 73 68 20  def struct Hash 
464d0 48 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74  Hash;.typedef st
464e0 72 75 63 74 20 48 61 73 68 45 6c 65 6d 20 48 61  ruct HashElem Ha
464f0 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f  shElem;../* A co
46500 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c  mplete hash tabl
46510 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
46520 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
46530 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  g structure..** 
46540 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66  The internals of
46550 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
46560 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20  are intended to 
46570 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69  be opaque -- cli
46580 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75  ent.** code shou
46590 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74  ld not attempt t
465a0 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69  o access or modi
465b0 66 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66  fy the fields of
465c0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   this structure.
465d0 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43 68  ** directly.  Ch
465e0 61 6e 67 65 20 74 68 69 73 20 73 74 72 75 63 74  ange this struct
465f0 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e  ure only by usin
46600 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 62  g the routines b
46610 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72  elow..** However
46620 2c 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 22 70  , some of the "p
46630 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20 22  rocedures" and "
46640 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d  functions" for m
46650 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20  odifying and.** 
46660 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 73  accessing this s
46670 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65 61  tructure are rea
46680 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77  lly macros, so w
46690 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d  e can't really m
466a0 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72 75  ake.** this stru
466b0 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2a  cture opaque..**
466c0 0a 2a 2a 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73  .** All elements
466d0 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62   of the hash tab
466e0 6c 65 20 61 72 65 20 6f 6e 20 61 20 73 69 6e 67  le are on a sing
466f0 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65 64  le doubly-linked
46700 20 6c 69 73 74 2e 0a 2a 2a 20 48 61 73 68 2e 66   list..** Hash.f
46710 69 72 73 74 20 70 6f 69 6e 74 73 20 74 6f 20 74  irst points to t
46720 68 65 20 68 65 61 64 20 6f 66 20 74 68 69 73 20  he head of this 
46730 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  list..**.** Ther
46740 65 20 61 72 65 20 48 61 73 68 2e 68 74 73 69 7a  e are Hash.htsiz
46750 65 20 62 75 63 6b 65 74 73 2e 20 20 45 61 63 68  e buckets.  Each
46760 20 62 75 63 6b 65 74 20 70 6f 69 6e 74 73 20 74   bucket points t
46770 6f 20 61 20 73 70 6f 74 20 69 6e 0a 2a 2a 20 74  o a spot in.** t
46780 68 65 20 67 6c 6f 62 61 6c 20 64 6f 75 62 6c 79  he global doubly
46790 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20 54  -linked list.  T
467a0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
467b0 68 65 20 62 75 63 6b 65 74 20 61 72 65 20 74 68  he bucket are th
467c0 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 70 6f 69  e.** element poi
467d0 6e 74 65 64 20 74 6f 20 70 6c 75 73 20 74 68 65  nted to plus the
467e0 20 6e 65 78 74 20 5f 68 74 2e 63 6f 75 6e 74 2d   next _ht.count-
467f0 31 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  1 elements in th
46800 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 48 61  e list..**.** Ha
46810 73 68 2e 68 74 73 69 7a 65 20 61 6e 64 20 48 61  sh.htsize and Ha
46820 73 68 2e 68 74 20 6d 61 79 20 62 65 20 7a 65 72  sh.ht may be zer
46830 6f 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65  o.  In that case
46840 20 6c 6f 6f 6b 75 70 20 69 73 20 64 6f 6e 65 0a   lookup is done.
46850 2a 2a 20 62 79 20 61 20 6c 69 6e 65 61 72 20 73  ** by a linear s
46860 65 61 72 63 68 20 6f 66 20 74 68 65 20 67 6c 6f  earch of the glo
46870 62 61 6c 20 6c 69 73 74 2e 20 20 46 6f 72 20 73  bal list.  For s
46880 6d 61 6c 6c 20 74 61 62 6c 65 73 2c 20 74 68 65  mall tables, the
46890 20 0a 2a 2a 20 48 61 73 68 2e 68 74 20 74 61 62   .** Hash.ht tab
468a0 6c 65 20 69 73 20 6e 65 76 65 72 20 61 6c 6c 6f  le is never allo
468b0 63 61 74 65 64 20 62 65 63 61 75 73 65 20 69 66  cated because if
468c0 20 74 68 65 72 65 20 61 72 65 20 66 65 77 20 65   there are few e
468d0 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68  lements.** in th
468e0 65 20 74 61 62 6c 65 2c 20 69 74 20 69 73 20 66  e table, it is f
468f0 61 73 74 65 72 20 74 6f 20 64 6f 20 61 20 6c 69  aster to do a li
46900 6e 65 61 72 20 73 65 61 72 63 68 20 74 68 61 6e  near search than
46910 20 74 6f 20 6d 61 6e 61 67 65 0a 2a 2a 20 74 68   to manage.** th
46920 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f  e hash table..*/
46930 0a 73 74 72 75 63 74 20 48 61 73 68 20 7b 0a 20  .struct Hash {. 
46940 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 74   unsigned int ht
46950 73 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75  size;      /* Nu
46960 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74 73 20  mber of buckets 
46970 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
46980 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
46990 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20  int count;      
469a0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
469b0 74 72 69 65 73 20 69 6e 20 74 68 69 73 20 74 61  tries in this ta
469c0 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  ble */.  HashEle
469d0 6d 20 2a 66 69 72 73 74 3b 20 20 20 20 20 20 20  m *first;       
469e0 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20     /* The first 
469f0 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61  element of the a
46a00 72 72 61 79 20 2a 2f 0a 20 20 73 74 72 75 63 74  rray */.  struct
46a10 20 5f 68 74 20 7b 20 20 20 20 20 20 20 20 20 20   _ht {          
46a20 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20      /* the hash 
46a30 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74  table */.    int
46a40 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20   count;         
46a50 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
46a60 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74  r of entries wit
46a70 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20  h this hash */. 
46a80 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 63 68 61     HashElem *cha
46a90 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  in;           /*
46aa0 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73   Pointer to firs
46ab0 74 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 69  t entry with thi
46ac0 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68  s hash */.  } *h
46ad0 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65  t;.};../* Each e
46ae0 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61  lement in the ha
46af0 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69  sh table is an i
46b00 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
46b10 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72  ollowing .** str
46b20 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65  ucture.  All ele
46b30 6d 65 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64  ments are stored
46b40 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75   on a single dou
46b50 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e  bly-linked list.
46b60 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68  .**.** Again, th
46b70 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
46b80 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f  intended to be o
46b90 70 61 71 75 65 2c 20 62 75 74 20 69 74 20 63 61  paque, but it ca
46ba0 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65  n't really.** be
46bb0 20 6f 70 61 71 75 65 20 62 65 63 61 75 73 65 20   opaque because 
46bc0 69 74 20 69 73 20 75 73 65 64 20 62 79 20 6d 61  it is used by ma
46bd0 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  cros..*/.struct 
46be0 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20 48 61 73  HashElem {.  Has
46bf0 68 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72  hElem *next, *pr
46c00 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  ev;       /* Nex
46c10 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73 20 65  t and previous e
46c20 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 74  lements in the t
46c30 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  able */.  void *
46c40 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  data;           
46c50 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61         /* Data a
46c60 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
46c70 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20  his element */. 
46c80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65   const char *pKe
46c90 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 2f 2a  y; int nKey;  /*
46ca0 20 4b 65 79 20 61 73 73 6f 63 69 61 74 65 64 20   Key associated 
46cb0 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e  with this elemen
46cc0 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  t */.};../*.** A
46cd0 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73 2e 20  ccess routines. 
46ce0 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73 65   To delete, inse
46cf0 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  rt a NULL pointe
46d00 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
46d10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
46d20 33 48 61 73 68 49 6e 69 74 28 48 61 73 68 2a 29  3HashInit(Hash*)
46d30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
46d40 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61   void *sqlite3Ha
46d50 73 68 49 6e 73 65 72 74 28 48 61 73 68 2a 2c 20  shInsert(Hash*, 
46d60 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79  const char *pKey
46d70 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64  , int nKey, void
46d80 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45   *pData);.SQLITE
46d90 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
46da0 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 63  qlite3HashFind(c
46db0 6f 6e 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e 73  onst Hash*, cons
46dc0 74 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e  t char *pKey, in
46dd0 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f  t nKey);.SQLITE_
46de0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
46df0 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48 61  ite3HashClear(Ha
46e00 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  sh*);../*.** Mac
46e10 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20  ros for looping 
46e20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74  over all element
46e30 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c  s of a hash tabl
46e40 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73  e.  The idiom is
46e50 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  .** like this:.*
46e60 2a 0a 2a 2a 20 20 20 48 61 73 68 20 68 3b 0a 2a  *.**   Hash h;.*
46e70 2a 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 3b  *   HashElem *p;
46e80 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66  .**   ....**   f
46e90 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46  or(p=sqliteHashF
46ea0 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d 73  irst(&h); p; p=s
46eb0 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 29  qliteHashNext(p)
46ec0 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74  ){.**     SomeSt
46ed0 72 75 63 74 75 72 65 20 2a 70 44 61 74 61 20 3d  ructure *pData =
46ee0 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28   sqliteHashData(
46ef0 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f  p);.**     // do
46f00 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20   something with 
46f10 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a  pData.**   }.*/.
46f20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61  #define sqliteHa
46f30 73 68 46 69 72 73 74 28 48 29 20 20 28 28 48 29  shFirst(H)  ((H)
46f40 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 69 6e 65  ->first).#define
46f50 20 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28   sqliteHashNext(
46f60 45 29 20 20 20 28 28 45 29 2d 3e 6e 65 78 74 29  E)   ((E)->next)
46f70 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48  .#define sqliteH
46f80 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28 45  ashData(E)   ((E
46f90 29 2d 3e 64 61 74 61 29 0a 2f 2a 20 23 64 65 66  )->data)./* #def
46fa0 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 4b 65  ine sqliteHashKe
46fb0 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e 70 4b  y(E)    ((E)->pK
46fc0 65 79 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20  ey) // NOT USED 
46fd0 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73 71  */./* #define sq
46fe0 6c 69 74 65 48 61 73 68 4b 65 79 73 69 7a 65 28  liteHashKeysize(
46ff0 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 20 20  E) ((E)->nKey)  
47000 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 0a  // NOT USED */..
47010 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20  /*.** Number of 
47020 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61 73  entries in a has
47030 68 20 74 61 62 6c 65 0a 2a 2f 0a 2f 2a 20 23 64  h table.*/./* #d
47040 65 66 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68  efine sqliteHash
47050 43 6f 75 6e 74 28 48 29 20 20 28 28 48 29 2d 3e  Count(H)  ((H)->
47060 63 6f 75 6e 74 29 20 2f 2f 20 4e 4f 54 20 55 53  count) // NOT US
47070 45 44 20 2a 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a  ED */..#endif /*
47080 20 5f 53 51 4c 49 54 45 5f 48 41 53 48 5f 48 5f   _SQLITE_HASH_H_
47090 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
470a0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61 73 68  **** End of hash
470b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
470c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
470e0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
470f0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
47100 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
47110 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  f in sqliteInt.h
47120 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
47130 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
47140 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61 72  **** Include par
47150 73 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  se.h in the midd
47160 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  le of sqliteInt.
47170 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
47180 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
47190 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
471a0 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  parse.h ********
471b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
471c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
471d0 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4b 5f  ***/.#define TK_
471e0 53 45 4d 49 20 20 20 20 20 20 20 20 20 20 20 20  SEMI            
471f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47200 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 50  1.#define TK_EXP
47210 4c 41 49 4e 20 20 20 20 20 20 20 20 20 20 20 20  LAIN            
47220 20 20 20 20 20 20 20 20 20 20 20 20 20 32 0a 23               2.#
47230 64 65 66 69 6e 65 20 54 4b 5f 51 55 45 52 59 20  define TK_QUERY 
47240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47250 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66            3.#def
47260 69 6e 65 20 54 4b 5f 50 4c 41 4e 20 20 20 20 20  ine TK_PLAN     
47270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47280 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65         4.#define
47290 20 54 4b 5f 42 45 47 49 4e 20 20 20 20 20 20 20   TK_BEGIN       
472a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472b0 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 54 4b      5.#define TK
472c0 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20  _TRANSACTION    
472d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472e0 20 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45   6.#define TK_DE
472f0 46 45 52 52 45 44 20 20 20 20 20 20 20 20 20 20  FERRED          
47300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a                7.
47310 23 64 65 66 69 6e 65 20 54 4b 5f 49 4d 4d 45 44  #define TK_IMMED
47320 49 41 54 45 20 20 20 20 20 20 20 20 20 20 20 20  IATE            
47330 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65             8.#de
47340 66 69 6e 65 20 54 4b 5f 45 58 43 4c 55 53 49 56  fine TK_EXCLUSIV
47350 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
47360 20 20 20 20 20 20 20 20 39 0a 23 64 65 66 69 6e          9.#defin
47370 65 20 54 4b 5f 43 4f 4d 4d 49 54 20 20 20 20 20  e TK_COMMIT     
47380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47390 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20 54      10.#define T
473a0 4b 5f 45 4e 44 20 20 20 20 20 20 20 20 20 20 20  K_END           
473b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473c0 20 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   11.#define TK_R
473d0 4f 4c 4c 42 41 43 4b 20 20 20 20 20 20 20 20 20  OLLBACK         
473e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
473f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 41 56 45  .#define TK_SAVE
47400 50 4f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20  POINT           
47410 20 20 20 20 20 20 20 20 20 20 20 31 33 0a 23 64             13.#d
47420 65 66 69 6e 65 20 54 4b 5f 52 45 4c 45 41 53 45  efine TK_RELEASE
47430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47440 20 20 20 20 20 20 20 20 31 34 0a 23 64 65 66 69          14.#defi
47450 6e 65 20 54 4b 5f 54 4f 20 20 20 20 20 20 20 20  ne TK_TO        
47460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47470 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20       15.#define 
47480 54 4b 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20  TK_TABLE        
47490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474a0 20 20 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f    16.#define TK_
474b0 43 52 45 41 54 45 20 20 20 20 20 20 20 20 20 20  CREATE          
474c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
474d0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 46 20  7.#define TK_IF 
474e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474f0 20 20 20 20 20 20 20 20 20 20 20 20 31 38 0a 23              18.#
47500 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54 20 20 20  define TK_NOT   
47510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47520 20 20 20 20 20 20 20 20 20 31 39 0a 23 64 65 66           19.#def
47530 69 6e 65 20 54 4b 5f 45 58 49 53 54 53 20 20 20  ine TK_EXISTS   
47540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47550 20 20 20 20 20 20 32 30 0a 23 64 65 66 69 6e 65        20.#define
47560 20 54 4b 5f 54 45 4d 50 20 20 20 20 20 20 20 20   TK_TEMP        
47570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47580 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20 54 4b     21.#define TK
47590 5f 4c 50 20 20 20 20 20 20 20 20 20 20 20 20 20  _LP             
475a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475b0 32 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 50  22.#define TK_RP
475c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475d0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 33 0a               23.
475e0 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 20 20 20  #define TK_AS   
475f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47600 20 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 65            24.#de
47610 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41 20 20 20  fine TK_COMMA   
47620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47630 20 20 20 20 20 20 20 32 35 0a 23 64 65 66 69 6e         25.#defin
47640 65 20 54 4b 5f 49 44 20 20 20 20 20 20 20 20 20  e TK_ID         
47650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47660 20 20 20 20 32 36 0a 23 64 65 66 69 6e 65 20 54      26.#define T
47670 4b 5f 49 4e 44 45 58 45 44 20 20 20 20 20 20 20  K_INDEXED       
47680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47690 20 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41   27.#define TK_A
476a0 42 4f 52 54 20 20 20 20 20 20 20 20 20 20 20 20  BORT            
476b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 38                28
476c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 43 54 49  .#define TK_ACTI
476d0 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ON              
476e0 20 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64             29.#d
476f0 65 66 69 6e 65 20 54 4b 5f 41 46 54 45 52 20 20  efine TK_AFTER  
47700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47710 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 69          30.#defi
47720 6e 65 20 54 4b 5f 41 4e 41 4c 59 5a 45 20 20 20  ne TK_ANALYZE   
47730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47740 20 20 20 20 20 33 31 0a 23 64 65 66 69 6e 65 20       31.#define 
47750 54 4b 5f 41 53 43 20 20 20 20 20 20 20 20 20 20  TK_ASC          
47760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47770 20 20 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f    32.#define TK_
47780 41 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20  ATTACH          
47790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
477a0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45 46  3.#define TK_BEF
477b0 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ORE             
477c0 20 20 20 20 20 20 20 20 20 20 20 20 33 34 0a 23              34.#
477d0 64 65 66 69 6e 65 20 54 4b 5f 42 59 20 20 20 20  define TK_BY    
477e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
477f0 20 20 20 20 20 20 20 20 20 33 35 0a 23 64 65 66           35.#def
47800 69 6e 65 20 54 4b 5f 43 41 53 43 41 44 45 20 20  ine TK_CASCADE  
47810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47820 20 20 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65        36.#define
47830 20 54 4b 5f 43 41 53 54 20 20 20 20 20 20 20 20   TK_CAST        
47840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47850 20 20 20 33 37 0a 23 64 65 66 69 6e 65 20 54 4b     37.#define TK
47860 5f 43 4f 4c 55 4d 4e 4b 57 20 20 20 20 20 20 20  _COLUMNKW       
47870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47880 33 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  38.#define TK_CO
47890 4e 46 4c 49 43 54 20 20 20 20 20 20 20 20 20 20  NFLICT          
478a0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39 0a               39.
478b0 23 64 65 66 69 6e 65 20 54 4b 5f 44 41 54 41 42  #define TK_DATAB
478c0 41 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ASE             
478d0 20 20 20 20 20 20 20 20 20 20 34 30 0a 23 64 65            40.#de
478e0 66 69 6e 65 20 54 4b 5f 44 45 53 43 20 20 20 20  fine TK_DESC    
478f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47900 20 20 20 20 20 20 20 34 31 0a 23 64 65 66 69 6e         41.#defin
47910 65 20 54 4b 5f 44 45 54 41 43 48 20 20 20 20 20  e TK_DETACH     
47920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47930 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 54      42.#define T
47940 4b 5f 45 41 43 48 20 20 20 20 20 20 20 20 20 20  K_EACH          
47950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47960 20 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46   43.#define TK_F
47970 41 49 4c 20 20 20 20 20 20 20 20 20 20 20 20 20  AIL             
47980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 34                44
47990 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 20  .#define TK_FOR 
479a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479b0 20 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64             45.#d
479c0 65 66 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45 20  efine TK_IGNORE 
479d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479e0 20 20 20 20 20 20 20 20 34 36 0a 23 64 65 66 69          46.#defi
479f0 6e 65 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 20  ne TK_INITIALLY 
47a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a10 20 20 20 20 20 34 37 0a 23 64 65 66 69 6e 65 20       47.#define 
47a20 54 4b 5f 49 4e 53 54 45 41 44 20 20 20 20 20 20  TK_INSTEAD      
47a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a40 20 20 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f    48.#define TK_
47a50 4c 49 4b 45 5f 4b 57 20 20 20 20 20 20 20 20 20  LIKE_KW         
47a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
47a70 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41 54  9.#define TK_MAT
47a80 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CH              
47a90 20 20 20 20 20 20 20 20 20 20 20 20 35 30 0a 23              50.#
47aa0 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 20 20 20 20  define TK_NO    
47ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ac0 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 66           51.#def
47ad0 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20 20 20 20  ine TK_KEY      
47ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47af0 20 20 20 20 20 20 35 32 0a 23 64 65 66 69 6e 65        52.#define
47b00 20 54 4b 5f 4f 46 20 20 20 20 20 20 20 20 20 20   TK_OF          
47b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b20 20 20 20 35 33 0a 23 64 65 66 69 6e 65 20 54 4b     53.#define TK
47b30 5f 4f 46 46 53 45 54 20 20 20 20 20 20 20 20 20  _OFFSET         
47b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b50 35 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 52  54.#define TK_PR
47b60 41 47 4d 41 20 20 20 20 20 20 20 20 20 20 20 20  AGMA            
47b70 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a               55.
47b80 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 49 53 45  #define TK_RAISE
47b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ba0 20 20 20 20 20 20 20 20 20 20 35 36 0a 23 64 65            56.#de
47bb0 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41 43 45 20  fine TK_REPLACE 
47bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bd0 20 20 20 20 20 20 20 35 37 0a 23 64 65 66 69 6e         57.#defin
47be0 65 20 54 4b 5f 52 45 53 54 52 49 43 54 20 20 20  e TK_RESTRICT   
47bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c00 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 54      58.#define T
47c10 4b 5f 52 4f 57 20 20 20 20 20 20 20 20 20 20 20  K_ROW           
47c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c30 20 35 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54   59.#define TK_T
47c40 52 49 47 47 45 52 20 20 20 20 20 20 20 20 20 20  RIGGER          
47c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 30                60
47c60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 43 55  .#define TK_VACU
47c70 55 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  UM              
47c80 20 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64             61.#d
47c90 65 66 69 6e 65 20 54 4b 5f 56 49 45 57 20 20 20  efine TK_VIEW   
47ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47cb0 20 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69          62.#defi
47cc0 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c 20 20 20  ne TK_VIRTUAL   
47cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ce0 20 20 20 20 20 36 33 0a 23 64 65 66 69 6e 65 20       63.#define 
47cf0 54 4b 5f 52 45 49 4e 44 45 58 20 20 20 20 20 20  TK_REINDEX      
47d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d10 20 20 36 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f    64.#define TK_
47d20 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20 20 20  RENAME          
47d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
47d40 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 54 49  5.#define TK_CTI
47d50 4d 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20 20  ME_KW           
47d60 20 20 20 20 20 20 20 20 20 20 20 20 36 36 0a 23              66.#
47d70 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59 20 20 20  define TK_ANY   
47d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d90 20 20 20 20 20 20 20 20 20 36 37 0a 23 64 65 66           67.#def
47da0 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20 20 20 20  ine TK_OR       
47db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47dc0 20 20 20 20 20 20 36 38 0a 23 64 65 66 69 6e 65        68.#define
47dd0 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 20 20 20   TK_AND         
47de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47df0 20 20 20 36 39 0a 23 64 65 66 69 6e 65 20 54 4b     69.#define TK
47e00 5f 49 53 20 20 20 20 20 20 20 20 20 20 20 20 20  _IS             
47e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e20 37 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45  70.#define TK_BE
47e30 54 57 45 45 4e 20 20 20 20 20 20 20 20 20 20 20  TWEEN           
47e40 20 20 20 20 20 20 20 20 20 20 20 20 20 37 31 0a               71.
47e50 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 20 20 20  #define TK_IN   
47e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e70 20 20 20 20 20 20 20 20 20 20 37 32 0a 23 64 65            72.#de
47e80 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c 20 20  fine TK_ISNULL  
47e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ea0 20 20 20 20 20 20 20 37 33 0a 23 64 65 66 69 6e         73.#defin
47eb0 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20  e TK_NOTNULL    
47ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ed0 20 20 20 20 37 34 0a 23 64 65 66 69 6e 65 20 54      74.#define T
47ee0 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20 20  K_NE            
47ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f00 20 37 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45   75.#define TK_E
47f10 51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Q               
47f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36                76
47f30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 54 20 20  .#define TK_GT  
47f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f50 20 20 20 20 20 20 20 20 20 20 20 37 37 0a 23 64             77.#d
47f60 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20 20 20 20  efine TK_LE     
47f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f80 20 20 20 20 20 20 20 20 37 38 0a 23 64 65 66 69          78.#defi
47f90 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20  ne TK_LT        
47fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47fb0 20 20 20 20 20 37 39 0a 23 64 65 66 69 6e 65 20       79.#define 
47fc0 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20 20  TK_GE           
47fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47fe0 20 20 38 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f    80.#define TK_
47ff0 45 53 43 41 50 45 20 20 20 20 20 20 20 20 20 20  ESCAPE          
48000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
48010 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54  1.#define TK_BIT
48020 41 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 20  AND             
48030 20 20 20 20 20 20 20 20 20 20 20 20 38 32 0a 23              82.#
48040 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4f 52 20  define TK_BITOR 
48050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48060 20 20 20 20 20 20 20 20 20 38 33 0a 23 64 65 66           83.#def
48070 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54 20 20 20  ine TK_LSHIFT   
48080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48090 20 20 20 20 20 20 38 34 0a 23 64 65 66 69 6e 65        84.#define
480a0 20 54 4b 5f 52 53 48 49 46 54 20 20 20 20 20 20   TK_RSHIFT      
480b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480c0 20 20 20 38 35 0a 23 64 65 66 69 6e 65 20 54 4b     85.#define TK
480d0 5f 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20  _PLUS           
480e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480f0 38 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 49  86.#define TK_MI
48100 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20 20  NUS             
48110 20 20 20 20 20 20 20 20 20 20 20 20 20 38 37 0a               87.
48120 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 41 52 20  #define TK_STAR 
48130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48140 20 20 20 20 20 20 20 20 20 20 38 38 0a 23 64 65            88.#de
48150 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48 20 20 20  fine TK_SLASH   
48160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48170 20 20 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e         89.#defin
48180 65 20 54 4b 5f 52 45 4d 20 20 20 20 20 20 20 20  e TK_REM        
48190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481a0 20 20 20 20 39 30 0a 23 64 65 66 69 6e 65 20 54      90.#define T
481b0 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20 20 20 20  K_CONCAT        
481c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481d0 20 39 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   91.#define TK_C
481e0 4f 4c 4c 41 54 45 20 20 20 20 20 20 20 20 20 20  OLLATE          
481f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 32                92
48200 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4e  .#define TK_BITN
48210 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OT              
48220 20 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 64             93.#d
48230 65 66 69 6e 65 20 54 4b 5f 53 54 52 49 4e 47 20  efine TK_STRING 
48240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48250 20 20 20 20 20 20 20 20 39 34 0a 23 64 65 66 69          94.#defi
48260 6e 65 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 20  ne TK_JOIN_KW   
48270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48280 20 20 20 20 20 39 35 0a 23 64 65 66 69 6e 65 20       95.#define 
48290 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 20  TK_CONSTRAINT   
482a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
482b0 20 20 39 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f    96.#define TK_
482c0 44 45 46 41 55 4c 54 20 20 20 20 20 20 20 20 20  DEFAULT         
482d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
482e0 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 55 4c  7.#define TK_NUL
482f0 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
48300 20 20 20 20 20 20 20 20 20 20 20 20 39 38 0a 23              98.#
48310 64 65 66 69 6e 65 20 54 4b 5f 50 52 49 4d 41 52  define TK_PRIMAR
48320 59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Y               
48330 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65 66           99.#def
48340 69 6e 65 20 54 4b 5f 55 4e 49 51 55 45 20 20 20  ine TK_UNIQUE   
48350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48360 20 20 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e        100.#defin
48370 65 20 54 4b 5f 43 48 45 43 4b 20 20 20 20 20 20  e TK_CHECK      
48380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48390 20 20 20 20 31 30 31 0a 23 64 65 66 69 6e 65 20      101.#define 
483a0 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 20 20 20  TK_REFERENCES   
483b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483c0 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 54 4b    102.#define TK
483d0 5f 41 55 54 4f 49 4e 43 52 20 20 20 20 20 20 20  _AUTOINCR       
483e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483f0 31 30 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f  103.#define TK_O
48400 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
48410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
48420 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 4c  4.#define TK_DEL
48430 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ETE             
48440 20 20 20 20 20 20 20 20 20 20 20 20 31 30 35 0a              105.
48450 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 44 41 54  #define TK_UPDAT
48460 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
48470 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23 64            106.#d
48480 65 66 69 6e 65 20 54 4b 5f 53 45 54 20 20 20 20  efine TK_SET    
48490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484a0 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66          107.#def
484b0 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 41 42 4c  ine TK_DEFERRABL
484c0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
484d0 20 20 20 20 20 20 31 30 38 0a 23 64 65 66 69 6e        108.#defin
484e0 65 20 54 4b 5f 46 4f 52 45 49 47 4e 20 20 20 20  e TK_FOREIGN    
484f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48500 20 20 20 20 31 30 39 0a 23 64 65 66 69 6e 65 20      109.#define 
48510 54 4b 5f 44 52 4f 50 20 20 20 20 20 20 20 20 20  TK_DROP         
48520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48530 20 20 31 31 30 0a 23 64 65 66 69 6e 65 20 54 4b    110.#define TK
48540 5f 55 4e 49 4f 4e 20 20 20 20 20 20 20 20 20 20  _UNION          
48550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48560 31 31 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41  111.#define TK_A
48570 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LL              
48580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
48590 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 58 43  2.#define TK_EXC
485a0 45 50 54 20 20 20 20 20 20 20 20 20 20 20 20 20  EPT             
485b0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 33 0a              113.
485c0 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54 45 52  #define TK_INTER
485d0 53 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20  SECT            
485e0 20 20 20 20 20 20 20 20 20 20 31 31 34 0a 23 64            114.#d
485f0 65 66 69 6e 65 20 54 4b 5f 53 45 4c 45 43 54 20  efine TK_SELECT 
48600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48610 20 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66          115.#def
48620 69 6e 65 20 54 4b 5f 44 49 53 54 49 4e 43 54 20  ine TK_DISTINCT 
48630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48640 20 20 20 20 20 20 31 31 36 0a 23 64 65 66 69 6e        116.#defin
48650 65 20 54 4b 5f 44 4f 54 20 20 20 20 20 20 20 20  e TK_DOT        
48660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48670 20 20 20 20 31 31 37 0a 23 64 65 66 69 6e 65 20      117.#define 
48680 54 4b 5f 46 52 4f 4d 20 20 20 20 20 20 20 20 20  TK_FROM         
48690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486a0 20 20 31 31 38 0a 23 64 65 66 69 6e 65 20 54 4b    118.#define TK
486b0 5f 4a 4f 49 4e 20 20 20 20 20 20 20 20 20 20 20  _JOIN           
486c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
486d0 31 31 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55  119.#define TK_U
486e0 53 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20  SING            
486f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
48700 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 44  0.#define TK_ORD
48710 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ER              
48720 20 20 20 20 20 20 20 20 20 20 20 20 31 32 31 0a              121.
48730 23 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55 50  #define TK_GROUP
48740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48750 20 20 20 20 20 20 20 20 20 20 31 32 32 0a 23 64            122.#d
48760 65 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47 20  efine TK_HAVING 
48770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48780 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65 66          123.#def
48790 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20 20  ine TK_LIMIT    
487a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487b0 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e        124.#defin
487c0 65 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20 20  e TK_WHERE      
487d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487e0 20 20 20 20 31 32 35 0a 23 64 65 66 69 6e 65 20      125.#define 
487f0 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20 20  TK_INTO         
48800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48810 20 20 31 32 36 0a 23 64 65 66 69 6e 65 20 54 4b    126.#define TK
48820 5f 56 41 4c 55 45 53 20 20 20 20 20 20 20 20 20  _VALUES         
48830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48840 31 32 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49  127.#define TK_I
48850 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20  NSERT           
48860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
48870 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 54  8.#define TK_INT
48880 45 47 45 52 20 20 20 20 20 20 20 20 20 20 20 20  EGER            
48890 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39 0a              129.
488a0 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c 4f 41 54  #define TK_FLOAT
488b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488c0 20 20 20 20 20 20 20 20 20 20 31 33 30 0a 23 64            130.#d
488d0 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42 20 20 20  efine TK_BLOB   
488e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488f0 20 20 20 20 20 20 20 20 31 33 31 0a 23 64 65 66          131.#def
48900 69 6e 65 20 54 4b 5f 52 45 47 49 53 54 45 52 20  ine TK_REGISTER 
48910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48920 20 20 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e        132.#defin
48930 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 20 20 20  e TK_VARIABLE   
48940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48950 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20      133.#define 
48960 54 4b 5f 43 41 53 45 20 20 20 20 20 20 20 20 20  TK_CASE         
48970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48980 20 20 31 33 34 0a 23 64 65 66 69 6e 65 20 54 4b    134.#define TK
48990 5f 57 48 45 4e 20 20 20 20 20 20 20 20 20 20 20  _WHEN           
489a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
489b0 31 33 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54  135.#define TK_T
489c0 48 45 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  HEN             
489d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
489e0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4c 53  6.#define TK_ELS
489f0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
48a00 20 20 20 20 20 20 20 20 20 20 20 20 31 33 37 0a              137.
48a10 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44 45 58  #define TK_INDEX
48a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a30 20 20 20 20 20 20 20 20 20 20 31 33 38 0a 23 64            138.#d
48a40 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45 52 20 20  efine TK_ALTER  
48a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a60 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65 66          139.#def
48a70 69 6e 65 20 54 4b 5f 41 44 44 20 20 20 20 20 20  ine TK_ADD      
48a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a90 20 20 20 20 20 20 31 34 30 0a 23 64 65 66 69 6e        140.#defin
48aa0 65 20 54 4b 5f 54 4f 5f 54 45 58 54 20 20 20 20  e TK_TO_TEXT    
48ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48ac0 20 20 20 20 31 34 31 0a 23 64 65 66 69 6e 65 20      141.#define 
48ad0 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 20 20 20 20  TK_TO_BLOB      
48ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48af0 20 20 31 34 32 0a 23 64 65 66 69 6e 65 20 54 4b    142.#define TK
48b00 5f 54 4f 5f 4e 55 4d 45 52 49 43 20 20 20 20 20  _TO_NUMERIC     
48b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48b20 31 34 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54  143.#define TK_T
48b30 4f 5f 49 4e 54 20 20 20 20 20 20 20 20 20 20 20  O_INT           
48b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
48b50 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f  4.#define TK_TO_
48b60 52 45 41 4c 20 20 20 20 20 20 20 20 20 20 20 20  REAL            
48b70 20 20 20 20 20 20 20 20 20 20 20 20 31 34 35 0a              145.
48b80 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 4f 54  #define TK_ISNOT
48b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48ba0 20 20 20 20 20 20 20 20 20 20 31 34 36 0a 23 64            146.#d
48bb0 65 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 5f  efine TK_END_OF_
48bc0 46 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  FILE            
48bd0 20 20 20 20 20 20 20 20 31 34 37 0a 23 64 65 66          147.#def
48be0 69 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 20  ine TK_ILLEGAL  
48bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48c00 20 20 20 20 20 20 31 34 38 0a 23 64 65 66 69 6e        148.#defin
48c10 65 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20 20  e TK_SPACE      
48c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48c30 20 20 20 20 31 34 39 0a 23 64 65 66 69 6e 65 20      149.#define 
48c40 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52 49  TK_UNCLOSED_STRI
48c50 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NG              
48c60 20 20 31 35 30 0a 23 64 65 66 69 6e 65 20 54 4b    150.#define TK
48c70 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20  _FUNCTION       
48c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48c90 31 35 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43  151.#define TK_C
48ca0 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20  OLUMN           
48cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35                15
48cc0 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47  2.#define TK_AGG
48cd0 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20  _FUNCTION       
48ce0 20 20 20 20 20 20 20 20 20 20 20 20 31 35 33 0a              153.
48cf0 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f 43  #define TK_AGG_C
48d00 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20  OLUMN           
48d10 20 20 20 20 20 20 20 20 20 20 31 35 34 0a 23 64            154.#d
48d20 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f 46  efine TK_CONST_F
48d30 55 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20  UNC             
48d40 20 20 20 20 20 20 20 20 31 35 35 0a 23 64 65 66          155.#def
48d50 69 6e 65 20 54 4b 5f 55 4d 49 4e 55 53 20 20 20  ine TK_UMINUS   
48d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48d70 20 20 20 20 20 20 31 35 36 0a 23 64 65 66 69 6e        156.#defin
48d80 65 20 54 4b 5f 55 50 4c 55 53 20 20 20 20 20 20  e TK_UPLUS      
48d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48da0 20 20 20 20 31 35 37 0a 0a 2f 2a 2a 2a 2a 2a 2a      157../******
48db0 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
48dc0 70 61 72 73 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  parse.h ********
48dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48df0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
48e00 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
48e10 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
48e20 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49  t off in sqliteI
48e30 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
48e40 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e 63 6c 75 64  *******/.#includ
48e50 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 23 69 6e 63  e <stdio.h>.#inc
48e60 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e 0a  lude <stdlib.h>.
48e70 23 69 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67  #include <string
48e80 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73  .h>.#include <as
48e90 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  sert.h>.#include
48ea0 20 3c 73 74 64 64 65 66 2e 68 3e 0a 0a 2f 2a 0a   <stddef.h>../*.
48eb0 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 69 6e 67 20  ** If compiling 
48ec0 66 6f 72 20 61 20 70 72 6f 63 65 73 73 6f 72 20  for a processor 
48ed0 74 68 61 74 20 6c 61 63 6b 73 20 66 6c 6f 61 74  that lacks float
48ee0 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f 72  ing point suppor
48ef0 74 2c 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65  t,.** substitute
48f00 20 69 6e 74 65 67 65 72 20 66 6f 72 20 66 6c 6f   integer for flo
48f10 61 74 69 6e 67 2d 70 6f 69 6e 74 0a 2a 2f 0a 23  ating-point.*/.#
48f20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
48f30 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
48f40 0a 23 20 64 65 66 69 6e 65 20 64 6f 75 62 6c 65  .# define double
48f50 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23 20   sqlite_int64.# 
48f60 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c  define LONGDOUBL
48f70 45 5f 54 59 50 45 20 73 71 6c 69 74 65 5f 69 6e  E_TYPE sqlite_in
48f80 74 36 34 0a 23 20 69 66 6e 64 65 66 20 53 51 4c  t64.# ifndef SQL
48f90 49 54 45 5f 42 49 47 5f 44 42 4c 0a 23 20 20 20  ITE_BIG_DBL.#   
48fa0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49  define SQLITE_BI
48fb0 47 5f 44 42 4c 20 28 28 28 73 71 6c 69 74 65 33  G_DBL (((sqlite3
48fc0 5f 69 6e 74 36 34 29 31 29 3c 3c 35 30 29 0a 23  _int64)1)<<50).#
48fd0 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20   endif.# define 
48fe0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45  SQLITE_OMIT_DATE
48ff0 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20 64  TIME_FUNCS 1.# d
49000 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49  efine SQLITE_OMI
49010 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64 65  T_TRACE 1.# unde
49020 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45  f SQLITE_MIXED_E
49030 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41  NDIAN_64BIT_FLOA
49040 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45  T.# undef SQLITE
49050 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 65 6e 64  _HAVE_ISNAN.#end
49060 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
49070 45 5f 42 49 47 5f 44 42 4c 0a 23 20 64 65 66 69  E_BIG_DBL.# defi
49080 6e 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42  ne SQLITE_BIG_DB
49090 4c 20 28 31 65 39 39 29 0a 23 65 6e 64 69 66 0a  L (1e99).#endif.
490a0 0a 2f 2a 0a 2a 2a 20 4f 4d 49 54 5f 54 45 4d 50  ./*.** OMIT_TEMP
490b0 44 42 20 69 73 20 73 65 74 20 74 6f 20 31 20 69  DB is set to 1 i
490c0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45  f SQLITE_OMIT_TE
490d0 4d 50 44 42 20 69 73 20 64 65 66 69 6e 65 64 2c  MPDB is defined,
490e0 20 6f 72 20 30 0a 2a 2a 20 61 66 74 65 72 77 61   or 0.** afterwa
490f0 72 64 2e 20 48 61 76 69 6e 67 20 74 68 69 73 20  rd. Having this 
49100 6d 61 63 72 6f 20 61 6c 6c 6f 77 73 20 75 73 20  macro allows us 
49110 74 6f 20 63 61 75 73 65 20 74 68 65 20 43 20 63  to cause the C c
49120 6f 6d 70 69 6c 65 72 20 0a 2a 2a 20 74 6f 20 6f  ompiler .** to o
49130 6d 69 74 20 63 6f 64 65 20 75 73 65 64 20 62 79  mit code used by
49140 20 54 45 4d 50 20 74 61 62 6c 65 73 20 77 69 74   TEMP tables wit
49150 68 6f 75 74 20 6d 65 73 73 79 20 23 69 66 6e 64  hout messy #ifnd
49160 65 66 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  ef statements..*
49170 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
49180 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 23 64 65 66  OMIT_TEMPDB.#def
49190 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20  ine OMIT_TEMPDB 
491a0 31 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  1.#else.#define 
491b0 4f 4d 49 54 5f 54 45 4d 50 44 42 20 30 0a 23 65  OMIT_TEMPDB 0.#e
491c0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  ndif../*.** If t
491d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
491e0 72 6f 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20  ro is set to 1, 
491f0 74 68 65 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73  then NULL values
49200 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 0a   are considered.
49210 2a 2a 20 64 69 73 74 69 6e 63 74 20 77 68 65 6e  ** distinct when
49220 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65   determining whe
49230 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 77 6f 20  ther or not two 
49240 65 6e 74 72 69 65 73 20 61 72 65 20 74 68 65 20  entries are the 
49250 73 61 6d 65 0a 2a 2a 20 69 6e 20 61 20 55 4e 49  same.** in a UNI
49260 51 55 45 20 69 6e 64 65 78 2e 20 20 54 68 69 73  QUE index.  This
49270 20 69 73 20 74 68 65 20 77 61 79 20 50 6f 73 74   is the way Post
49280 67 72 65 53 51 4c 2c 20 4f 72 61 63 6c 65 2c 20  greSQL, Oracle, 
49290 44 42 32 2c 20 4d 79 53 51 4c 2c 0a 2a 2a 20 4f  DB2, MySQL,.** O
492a0 43 45 4c 4f 54 2c 20 61 6e 64 20 46 69 72 65 62  CELOT, and Fireb
492b0 69 72 64 20 61 6c 6c 20 77 6f 72 6b 2e 20 20 54  ird all work.  T
492c0 68 65 20 53 51 4c 39 32 20 73 70 65 63 20 65 78  he SQL92 spec ex
492d0 70 6c 69 63 69 74 6c 79 20 73 61 79 73 20 74 68  plicitly says th
492e0 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 77 61 79  is.** is the way
492f0 20 74 68 69 6e 67 73 20 61 72 65 20 73 75 70 70   things are supp
49300 6f 73 65 20 74 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a  ose to work..**.
49310 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** If the follow
49320 69 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74  ing macro is set
49330 20 74 6f 20 30 2c 20 74 68 65 20 4e 55 4c 4c 73   to 0, the NULLs
49340 20 61 72 65 20 69 6e 64 69 73 74 69 6e 63 74 20   are indistinct 
49350 66 6f 72 0a 2a 2a 20 61 20 55 4e 49 51 55 45 20  for.** a UNIQUE 
49360 69 6e 64 65 78 2e 20 20 49 6e 20 74 68 69 73 20  index.  In this 
49370 6d 6f 64 65 2c 20 79 6f 75 20 63 61 6e 20 6f 6e  mode, you can on
49380 6c 79 20 68 61 76 65 20 61 20 73 69 6e 67 6c 65  ly have a single
49390 20 4e 55 4c 4c 20 65 6e 74 72 79 0a 2a 2a 20 66   NULL entry.** f
493a0 6f 72 20 61 20 63 6f 6c 75 6d 6e 20 64 65 63 6c  or a column decl
493b0 61 72 65 64 20 55 4e 49 51 55 45 2e 20 20 54 68  ared UNIQUE.  Th
493c0 69 73 20 69 73 20 74 68 65 20 77 61 79 20 49 6e  is is the way In
493d0 66 6f 72 6d 69 78 20 61 6e 64 20 53 51 4c 20 53  formix and SQL S
493e0 65 72 76 65 72 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a  erver.** work..*
493f0 2f 0a 23 64 65 66 69 6e 65 20 4e 55 4c 4c 5f 44  /.#define NULL_D
49400 49 53 54 49 4e 43 54 5f 46 4f 52 5f 55 4e 49 51  ISTINCT_FOR_UNIQ
49410 55 45 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  UE 1../*.** The 
49420 22 66 69 6c 65 20 66 6f 72 6d 61 74 22 20 6e 75  "file format" nu
49430 6d 62 65 72 20 69 73 20 61 6e 20 69 6e 74 65 67  mber is an integ
49440 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72 65  er that is incre
49450 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 0a  mented whenever.
49460 2a 2a 20 74 68 65 20 56 44 42 45 2d 6c 65 76 65  ** the VDBE-leve
49470 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 63 68  l file format ch
49480 61 6e 67 65 73 2e 20 20 54 68 65 20 66 6f 6c 6c  anges.  The foll
49490 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 64 65 66  owing macros def
494a0 69 6e 65 20 74 68 65 0a 2a 2a 20 74 68 65 20 64  ine the.** the d
494b0 65 66 61 75 6c 74 20 66 69 6c 65 20 66 6f 72 6d  efault file form
494c0 61 74 20 66 6f 72 20 6e 65 77 20 64 61 74 61 62  at for new datab
494d0 61 73 65 73 20 61 6e 64 20 74 68 65 20 6d 61 78  ases and the max
494e0 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d 61 74  imum file format
494f0 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6c 69 62  .** that the lib
49500 72 61 72 79 20 63 61 6e 20 72 65 61 64 2e 0a 2a  rary can read..*
49510 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
49520 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  _MAX_FILE_FORMAT
49530 20 34 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   4.#ifndef SQLIT
49540 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46  E_DEFAULT_FILE_F
49550 4f 52 4d 41 54 0a 23 20 64 65 66 69 6e 65 20 53  ORMAT.# define S
49560 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49  QLITE_DEFAULT_FI
49570 4c 45 5f 46 4f 52 4d 41 54 20 31 0a 23 65 6e 64  LE_FORMAT 1.#end
49580 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
49590 54 45 5f 44 45 46 41 55 4c 54 5f 52 45 43 55 52  TE_DEFAULT_RECUR
495a0 53 49 56 45 5f 54 52 49 47 47 45 52 53 0a 23 20  SIVE_TRIGGERS.# 
495b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
495c0 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 5f  FAULT_RECURSIVE_
495d0 54 52 49 47 47 45 52 53 20 30 0a 23 65 6e 64 69  TRIGGERS 0.#endi
495e0 66 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 76 69 64 65  f../*.** Provide
495f0 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65   a default value
49600 20 66 6f 72 20 53 51 4c 49 54 45 5f 54 45 4d 50   for SQLITE_TEMP
49610 5f 53 54 4f 52 45 20 69 6e 20 63 61 73 65 20 69  _STORE in case i
49620 74 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69  t is not specifi
49630 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d  ed.** on the com
49640 6d 61 6e 64 2d 6c 69 6e 65 0a 2a 2f 0a 23 69 66  mand-line.*/.#if
49650 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50  ndef SQLITE_TEMP
49660 5f 53 54 4f 52 45 0a 23 20 64 65 66 69 6e 65 20  _STORE.# define 
49670 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
49680 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  E 1.#endif../*.*
49690 2a 20 47 43 43 20 64 6f 65 73 20 6e 6f 74 20 64  * GCC does not d
496a0 65 66 69 6e 65 20 74 68 65 20 6f 66 66 73 65 74  efine the offset
496b0 6f 66 28 29 20 6d 61 63 72 6f 20 73 6f 20 77 65  of() macro so we
496c0 27 6c 6c 20 68 61 76 65 20 74 6f 20 64 6f 20 69  'll have to do i
496d0 74 0a 2a 2a 20 6f 75 72 73 65 6c 76 65 73 2e 0a  t.** ourselves..
496e0 2a 2f 0a 23 69 66 6e 64 65 66 20 6f 66 66 73 65  */.#ifndef offse
496f0 74 6f 66 0a 23 64 65 66 69 6e 65 20 6f 66 66 73  tof.#define offs
49700 65 74 6f 66 28 53 54 52 55 43 54 55 52 45 2c 46  etof(STRUCTURE,F
49710 49 45 4c 44 29 20 28 28 69 6e 74 29 28 28 63 68  IELD) ((int)((ch
49720 61 72 2a 29 26 28 28 53 54 52 55 43 54 55 52 45  ar*)&((STRUCTURE
49730 2a 29 30 29 2d 3e 46 49 45 4c 44 29 29 0a 23 65  *)0)->FIELD)).#e
49740 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ndif../*.** Chec
49750 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69 73  k to see if this
49760 20 6d 61 63 68 69 6e 65 20 75 73 65 73 20 45 42   machine uses EB
49770 43 44 49 43 2e 20 20 28 59 65 73 2c 20 62 65 6c  CDIC.  (Yes, bel
49780 69 65 76 65 20 69 74 20 6f 72 0a 2a 2a 20 6e 6f  ieve it or.** no
49790 74 2c 20 74 68 65 72 65 20 61 72 65 20 73 74 69  t, there are sti
497a0 6c 6c 20 6d 61 63 68 69 6e 65 73 20 6f 75 74 20  ll machines out 
497b0 74 68 65 72 65 20 74 68 61 74 20 75 73 65 20 45  there that use E
497c0 42 43 44 49 43 2e 29 0a 2a 2f 0a 23 69 66 20 27  BCDIC.).*/.#if '
497d0 41 27 20 3d 3d 20 27 5c 33 30 31 27 0a 23 20 64  A' == '\301'.# d
497e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 42 43  efine SQLITE_EBC
497f0 44 49 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65  DIC 1.#else.# de
49800 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 53 43 49  fine SQLITE_ASCI
49810 49 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  I 1.#endif../*.*
49820 2a 20 49 6e 74 65 67 65 72 73 20 6f 66 20 6b 6e  * Integers of kn
49830 6f 77 6e 20 73 69 7a 65 73 2e 20 20 54 68 65 73  own sizes.  Thes
49840 65 20 74 79 70 65 64 65 66 73 20 6d 69 67 68 74  e typedefs might
49850 20 63 68 61 6e 67 65 20 66 6f 72 20 61 72 63 68   change for arch
49860 69 74 65 63 74 75 72 65 73 0a 2a 2a 20 77 68 65  itectures.** whe
49870 72 65 20 74 68 65 20 73 69 7a 65 73 20 76 65 72  re the sizes ver
49880 79 2e 20 20 50 72 65 70 72 6f 63 65 73 73 6f 72  y.  Preprocessor
49890 20 6d 61 63 72 6f 73 20 61 72 65 20 61 76 61 69   macros are avai
498a0 6c 61 62 6c 65 20 73 6f 20 74 68 61 74 20 74 68  lable so that th
498b0 65 0a 2a 2a 20 74 79 70 65 73 20 63 61 6e 20 62  e.** types can b
498c0 65 20 63 6f 6e 76 65 6e 69 65 6e 74 6c 79 20 72  e conveniently r
498d0 65 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70  edefined at comp
498e0 69 6c 65 2d 74 79 70 65 2e 20 20 4c 69 6b 65 20  ile-type.  Like 
498f0 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
49900 20 20 20 20 63 63 20 27 2d 44 55 49 4e 54 50 54      cc '-DUINTPT
49910 52 5f 54 59 50 45 3d 6c 6f 6e 67 20 6c 6f 6e 67  R_TYPE=long long
49920 20 69 6e 74 27 20 2e 2e 2e 0a 2a 2f 0a 23 69 66   int' ....*/.#if
49930 6e 64 65 66 20 55 49 4e 54 33 32 5f 54 59 50 45  ndef UINT32_TYPE
49940 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 55 49  .# ifdef HAVE_UI
49950 4e 54 33 32 5f 54 0a 23 20 20 64 65 66 69 6e 65  NT32_T.#  define
49960 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 69 6e   UINT32_TYPE uin
49970 74 33 32 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20  t32_t.# else.#  
49980 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54 59  define UINT32_TY
49990 50 45 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 0a  PE unsigned int.
499a0 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23  # endif.#endif.#
499b0 69 66 6e 64 65 66 20 55 49 4e 54 31 36 5f 54 59  ifndef UINT16_TY
499c0 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f  PE.# ifdef HAVE_
499d0 55 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66 69  UINT16_T.#  defi
499e0 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ne UINT16_TYPE u
499f0 69 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23  int16_t.# else.#
49a00 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36 5f    define UINT16_
49a10 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 68  TYPE unsigned sh
49a20 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a  ort int.# endif.
49a30 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49  #endif.#ifndef I
49a40 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65  NT16_TYPE.# ifde
49a50 66 20 48 41 56 45 5f 49 4e 54 31 36 5f 54 0a 23  f HAVE_INT16_T.#
49a60 20 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54    define INT16_T
49a70 59 50 45 20 69 6e 74 31 36 5f 74 0a 23 20 65 6c  YPE int16_t.# el
49a80 73 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54  se.#  define INT
49a90 31 36 5f 54 59 50 45 20 73 68 6f 72 74 20 69 6e  16_TYPE short in
49aa0 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  t.# endif.#endif
49ab0 0a 23 69 66 6e 64 65 66 20 55 49 4e 54 38 5f 54  .#ifndef UINT8_T
49ac0 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56 45  YPE.# ifdef HAVE
49ad0 5f 55 49 4e 54 38 5f 54 0a 23 20 20 64 65 66 69  _UINT8_T.#  defi
49ae0 6e 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 69  ne UINT8_TYPE ui
49af0 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20  nt8_t.# else.#  
49b00 64 65 66 69 6e 65 20 55 49 4e 54 38 5f 54 59 50  define UINT8_TYP
49b10 45 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 0a  E unsigned char.
49b20 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23  # endif.#endif.#
49b30 69 66 6e 64 65 66 20 49 4e 54 38 5f 54 59 50 45  ifndef INT8_TYPE
49b40 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e  .# ifdef HAVE_IN
49b50 54 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 49  T8_T.#  define I
49b60 4e 54 38 5f 54 59 50 45 20 69 6e 74 38 5f 74 0a  NT8_TYPE int8_t.
49b70 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
49b80 20 49 4e 54 38 5f 54 59 50 45 20 73 69 67 6e 65   INT8_TYPE signe
49b90 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23  d char.# endif.#
49ba0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4c 4f  endif.#ifndef LO
49bb0 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 0a 23 20  NGDOUBLE_TYPE.# 
49bc0 64 65 66 69 6e 65 20 4c 4f 4e 47 44 4f 55 42 4c  define LONGDOUBL
49bd0 45 5f 54 59 50 45 20 6c 6f 6e 67 20 64 6f 75 62  E_TYPE long doub
49be0 6c 65 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65  le.#endif.typede
49bf0 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  f sqlite_int64 i
49c00 36 34 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  64;          /* 
49c10 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  8-byte signed in
49c20 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66  teger */.typedef
49c30 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 75   sqlite_uint64 u
49c40 36 34 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 38  64;         /* 8
49c50 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20 69  -byte unsigned i
49c60 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65  nteger */.typede
49c70 66 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 33  f UINT32_TYPE u3
49c80 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  2;           /* 
49c90 34 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64 20  4-byte unsigned 
49ca0 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
49cb0 65 66 20 55 49 4e 54 31 36 5f 54 59 50 45 20 75  ef UINT16_TYPE u
49cc0 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  16;           /*
49cd0 20 32 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   2-byte unsigned
49ce0 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
49cf0 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45 20 69  def INT16_TYPE i
49d00 31 36 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  16;            /
49d10 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 2-byte signed 
49d20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64  integer */.typed
49d30 65 66 20 55 49 4e 54 38 5f 54 59 50 45 20 75 38  ef UINT8_TYPE u8
49d40 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
49d50 20 31 2d 62 79 74 65 20 75 6e 73 69 67 6e 65 64   1-byte unsigned
49d60 20 69 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65   integer */.type
49d70 64 65 66 20 49 4e 54 38 5f 54 59 50 45 20 69 38  def INT8_TYPE i8
49d80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
49d90 2a 20 31 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 1-byte signed 
49da0 69 6e 74 65 67 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a  integer */../*.*
49db0 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32  * SQLITE_MAX_U32
49dc0 20 69 73 20 61 20 75 36 34 20 63 6f 6e 73 74 61   is a u64 consta
49dd0 6e 74 20 74 68 61 74 20 69 73 20 74 68 65 20 6d  nt that is the m
49de0 61 78 69 6d 75 6d 20 75 36 34 20 76 61 6c 75 65  aximum u64 value
49df0 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20  .** that can be 
49e00 73 74 6f 72 65 64 20 69 6e 20 61 20 75 33 32 20  stored in a u32 
49e10 77 69 74 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20  without loss of 
49e20 64 61 74 61 2e 20 20 54 68 65 20 76 61 6c 75 65  data.  The value
49e30 0a 2a 2a 20 69 73 20 30 78 30 30 30 30 30 30 30  .** is 0x0000000
49e40 30 66 66 66 66 66 66 66 66 2e 20 20 42 75 74 20  0ffffffff.  But 
49e50 62 65 63 61 75 73 65 20 6f 66 20 71 75 69 72 6b  because of quirk
49e60 73 20 6f 66 20 73 6f 6d 65 20 63 6f 6d 70 69 6c  s of some compil
49e70 65 72 73 2c 20 77 65 0a 2a 2a 20 68 61 76 65 20  ers, we.** have 
49e80 74 6f 20 73 70 65 63 69 66 79 20 74 68 65 20 76  to specify the v
49e90 61 6c 75 65 20 69 6e 20 74 68 65 20 6c 65 73 73  alue in the less
49ea0 20 69 6e 74 75 69 74 69 76 65 20 6d 61 6e 6e 65   intuitive manne
49eb0 72 20 73 68 6f 77 6e 3a 0a 2a 2f 0a 23 64 65 66  r shown:.*/.#def
49ec0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55  ine SQLITE_MAX_U
49ed0 33 32 20 20 28 28 28 28 75 36 34 29 31 29 3c 3c  32  ((((u64)1)<<
49ee0 33 32 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61  32)-1)../*.** Ma
49ef0 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e  cros to determin
49f00 65 20 77 68 65 74 68 65 72 20 74 68 65 20 6d 61  e whether the ma
49f10 63 68 69 6e 65 20 69 73 20 62 69 67 20 6f 72 20  chine is big or 
49f20 6c 69 74 74 6c 65 20 65 6e 64 69 61 6e 2c 0a 2a  little endian,.*
49f30 2a 20 65 76 61 6c 75 61 74 65 64 20 61 74 20 72  * evaluated at r
49f40 75 6e 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65  untime..*/.#ifde
49f50 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d  f SQLITE_AMALGAM
49f60 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49  ATION.SQLITE_PRI
49f70 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73  VATE const int s
49f80 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b 0a 23  qlite3one = 1;.#
49f90 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56  else.SQLITE_PRIV
49fa0 41 54 45 20 63 6f 6e 73 74 20 69 6e 74 20 73 71  ATE const int sq
49fb0 6c 69 74 65 33 6f 6e 65 3b 0a 23 65 6e 64 69 66  lite3one;.#endif
49fc0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 69 33 38  .#if defined(i38
49fd0 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  6) || defined(__
49fe0 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e  i386__) || defin
49ff0 65 64 28 5f 4d 5f 49 58 38 36 29 5c 0a 20 20 20  ed(_M_IX86)\.   
4a000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a010 20 20 20 20 20 20 20 20 20 20 7c 7c 20 64 65 66            || def
4a020 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 29 20 7c  ined(__x86_64) |
4a030 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f  | defined(__x86_
4a040 36 34 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 53  64__).# define S
4a050 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20  QLITE_BIGENDIAN 
4a060 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 53 51     0.# define SQ
4a070 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49 41  LITE_LITTLEENDIA
4a080 4e 20 31 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  N 1.# define SQL
4a090 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
4a0a0 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 0a   SQLITE_UTF16LE.
4a0b0 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
4a0c0 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e 20  QLITE_BIGENDIAN 
4a0d0 20 20 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73     (*(char *)(&s
4a0e0 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 30 29 0a 23  qlite3one)==0).#
4a0f0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c   define SQLITE_L
4a100 49 54 54 4c 45 45 4e 44 49 41 4e 20 28 2a 28 63  ITTLEENDIAN (*(c
4a110 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65 33 6f  har *)(&sqlite3o
4a120 6e 65 29 3d 3d 31 29 0a 23 20 64 65 66 69 6e 65  ne)==1).# define
4a130 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
4a140 49 56 45 20 28 53 51 4c 49 54 45 5f 42 49 47 45  IVE (SQLITE_BIGE
4a150 4e 44 49 41 4e 3f 53 51 4c 49 54 45 5f 55 54 46  NDIAN?SQLITE_UTF
4a160 31 36 42 45 3a 53 51 4c 49 54 45 5f 55 54 46 31  16BE:SQLITE_UTF1
4a170 36 4c 45 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  6LE).#endif../*.
4a180 2a 2a 20 43 6f 6e 73 74 61 6e 74 73 20 66 6f 72  ** Constants for
4a190 20 74 68 65 20 6c 61 72 67 65 73 74 20 61 6e 64   the largest and
4a1a0 20 73 6d 61 6c 6c 65 73 74 20 70 6f 73 73 69 62   smallest possib
4a1b0 6c 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  le 64-bit signed
4a1c0 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68   integers..** Th
4a1d0 65 73 65 20 6d 61 63 72 6f 73 20 61 72 65 20 64  ese macros are d
4a1e0 65 73 69 67 6e 65 64 20 74 6f 20 77 6f 72 6b 20  esigned to work 
4a1f0 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 62 6f 74  correctly on bot
4a200 68 20 33 32 2d 62 69 74 20 61 6e 64 20 36 34 2d  h 32-bit and 64-
4a210 62 69 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 73  bit.** compilers
4a220 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 41 52  ..*/.#define LAR
4a230 47 45 53 54 5f 49 4e 54 36 34 20 20 28 30 78 66  GEST_INT64  (0xf
4a240 66 66 66 66 66 66 66 7c 28 28 28 69 36 34 29 30  fffffff|(((i64)0
4a250 78 37 66 66 66 66 66 66 66 29 3c 3c 33 32 29 29  x7fffffff)<<32))
4a260 0a 23 64 65 66 69 6e 65 20 53 4d 41 4c 4c 45 53  .#define SMALLES
4a270 54 5f 49 4e 54 36 34 20 28 28 28 69 36 34 29 2d  T_INT64 (((i64)-
4a280 31 29 20 2d 20 4c 41 52 47 45 53 54 5f 49 4e 54  1) - LARGEST_INT
4a290 36 34 29 0a 0a 2f 2a 20 0a 2a 2a 20 52 6f 75 6e  64)../* .** Roun
4a2a0 64 20 75 70 20 61 20 6e 75 6d 62 65 72 20 74 6f  d up a number to
4a2b0 20 74 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72   the next larger
4a2c0 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2e 20   multiple of 8. 
4a2d0 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a 2a   This is used.**
4a2e0 20 74 6f 20 66 6f 72 63 65 20 38 2d 62 79 74 65   to force 8-byte
4a2f0 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 6e 20 36 34   alignment on 64
4a300 2d 62 69 74 20 61 72 63 68 69 74 65 63 74 75 72  -bit architectur
4a310 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  es..*/.#define R
4a320 4f 55 4e 44 38 28 78 29 20 20 20 20 20 28 28 28  OUND8(x)     (((
4a330 78 29 2b 37 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a  x)+7)&~7)../*.**
4a340 20 52 6f 75 6e 64 20 64 6f 77 6e 20 74 6f 20 74   Round down to t
4a350 68 65 20 6e 65 61 72 65 73 74 20 6d 75 6c 74 69  he nearest multi
4a360 70 6c 65 20 6f 66 20 38 0a 2a 2f 0a 23 64 65 66  ple of 8.*/.#def
4a370 69 6e 65 20 52 4f 55 4e 44 44 4f 57 4e 38 28 78  ine ROUNDDOWN8(x
4a380 29 20 28 28 78 29 26 7e 37 29 0a 0a 2f 2a 0a 2a  ) ((x)&~7)../*.*
4a390 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68  * Assert that th
4a3a0 65 20 70 6f 69 6e 74 65 72 20 58 20 69 73 20 61  e pointer X is a
4a3b0 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62  ligned to an 8-b
4a3c0 79 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 20 54  yte boundary.  T
4a3d0 68 69 73 0a 2a 2a 20 6d 61 63 72 6f 20 69 73 20  his.** macro is 
4a3e0 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 69 6e  used only within
4a3f0 20 61 73 73 65 72 74 28 29 20 74 6f 20 76 65 72   assert() to ver
4a400 69 66 79 20 74 68 61 74 20 74 68 65 20 63 6f 64  ify that the cod
4a410 65 20 67 65 74 73 0a 2a 2a 20 61 6c 6c 20 61 6c  e gets.** all al
4a420 69 67 6e 6d 65 6e 74 20 72 65 73 74 72 69 63 74  ignment restrict
4a430 69 6f 6e 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a  ions correct..**
4a440 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 53  .** Except, if S
4a450 51 4c 49 54 45 5f 34 5f 42 59 54 45 5f 41 4c 49  QLITE_4_BYTE_ALI
4a460 47 4e 45 44 5f 4d 41 4c 4c 4f 43 20 69 73 20 64  GNED_MALLOC is d
4a470 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
4a480 0a 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d  .** underlying m
4a490 61 6c 6c 6f 63 28 29 20 69 6d 70 6c 65 6d 65 6e  alloc() implemen
4a4a0 74 69 6f 6e 20 6d 69 67 68 74 20 72 65 74 75 72  tion might retur
4a4b0 6e 20 75 73 20 34 2d 62 79 74 65 20 61 6c 69 67  n us 4-byte alig
4a4c0 6e 65 64 0a 2a 2a 20 70 6f 69 6e 74 65 72 73 2e  ned.** pointers.
4a4d0 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20    In that case, 
4a4e0 6f 6e 6c 79 20 76 65 72 69 66 79 20 34 2d 62 79  only verify 4-by
4a4f0 74 65 20 61 6c 69 67 6e 6d 65 6e 74 2e 0a 2a 2f  te alignment..*/
4a500 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 34  .#ifdef SQLITE_4
4a510 5f 42 59 54 45 5f 41 4c 49 47 4e 45 44 5f 4d 41  _BYTE_ALIGNED_MA
4a520 4c 4c 4f 43 0a 23 20 64 65 66 69 6e 65 20 45 49  LLOC.# define EI
4a530 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45  GHT_BYTE_ALIGNME
4a540 4e 54 28 58 29 20 20 20 28 28 28 28 63 68 61 72  NT(X)   ((((char
4a550 2a 29 28 58 29 20 2d 20 28 63 68 61 72 2a 29 30  *)(X) - (char*)0
4a560 29 26 33 29 3d 3d 30 29 0a 23 65 6c 73 65 0a 23  )&3)==0).#else.#
4a570 20 64 65 66 69 6e 65 20 45 49 47 48 54 5f 42 59   define EIGHT_BY
4a580 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58 29 20  TE_ALIGNMENT(X) 
4a590 20 20 28 28 28 28 63 68 61 72 2a 29 28 58 29 20    ((((char*)(X) 
4a5a0 2d 20 28 63 68 61 72 2a 29 30 29 26 37 29 3d 3d  - (char*)0)&7)==
4a5b0 30 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  0).#endif.../*.*
4a5c0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
4a5d0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
4a5e0 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
4a5f0 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 75   to store the bu
4a600 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61  sy-handler.** ca
4a610 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 67 69 76  llback for a giv
4a620 65 6e 20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65  en sqlite handle
4a630 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  . .**.** The sql
4a640 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72 20  ite.busyHandler 
4a650 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 73 71  member of the sq
4a660 6c 69 74 65 20 73 74 72 75 63 74 20 63 6f 6e 74  lite struct cont
4a670 61 69 6e 73 20 74 68 65 20 62 75 73 79 0a 2a 2a  ains the busy.**
4a680 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68   callback for th
4a690 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
4a6a0 65 2e 20 45 61 63 68 20 70 61 67 65 72 20 6f 70  e. Each pager op
4a6b0 65 6e 65 64 20 76 69 61 20 74 68 65 20 73 71 6c  ened via the sql
4a6c0 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 73  ite.** handle is
4a6d0 20 70 61 73 73 65 64 20 61 20 70 6f 69 6e 74 65   passed a pointe
4a6e0 72 20 74 6f 20 73 71 6c 69 74 65 2e 62 75 73 79  r to sqlite.busy
4a6f0 48 61 6e 64 6c 65 72 2e 20 54 68 65 20 62 75 73  Handler. The bus
4a700 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c  y-handler.** cal
4a710 6c 62 61 63 6b 20 69 73 20 63 75 72 72 65 6e 74  lback is current
4a720 6c 79 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20  ly invoked only 
4a730 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67 65  from within page
4a740 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  r.c..*/.typedef 
4a750 73 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c  struct BusyHandl
4a760 65 72 20 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a  er BusyHandler;.
4a770 73 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c  struct BusyHandl
4a780 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 46 75  er {.  int (*xFu
4a790 6e 63 29 28 76 6f 69 64 20 2a 2c 69 6e 74 29 3b  nc)(void *,int);
4a7a0 20 20 2f 2a 20 54 68 65 20 62 75 73 79 20 63 61    /* The busy ca
4a7b0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64  llback */.  void
4a7c0 20 2a 70 41 72 67 3b 20 20 20 20 20 20 20 20 20   *pArg;         
4a7d0 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
4a7e0 61 72 67 20 74 6f 20 62 75 73 79 20 63 61 6c 6c  arg to busy call
4a7f0 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  back */.  int nB
4a800 75 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  usy;            
4a810 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e       /* Incremen
4a820 74 65 64 20 77 69 74 68 20 65 61 63 68 20 62 75  ted with each bu
4a830 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f  sy call */.};../
4a840 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65  *.** Name of the
4a850 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65   master database
4a860 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6d 61 73   table.  The mas
4a870 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62  ter database tab
4a880 6c 65 0a 2a 2a 20 69 73 20 61 20 73 70 65 63 69  le.** is a speci
4a890 61 6c 20 74 61 62 6c 65 20 74 68 61 74 20 68 6f  al table that ho
4a8a0 6c 64 73 20 74 68 65 20 6e 61 6d 65 73 20 61 6e  lds the names an
4a8b0 64 20 61 74 74 72 69 62 75 74 65 73 20 6f 66 20  d attributes of 
4a8c0 61 6c 6c 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c  all.** user tabl
4a8d0 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 0a  es and indices..
4a8e0 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45  */.#define MASTE
4a8f0 52 5f 4e 41 4d 45 20 20 20 20 20 20 20 22 73 71  R_NAME       "sq
4a900 6c 69 74 65 5f 6d 61 73 74 65 72 22 0a 23 64 65  lite_master".#de
4a910 66 69 6e 65 20 54 45 4d 50 5f 4d 41 53 54 45 52  fine TEMP_MASTER
4a920 5f 4e 41 4d 45 20 20 22 73 71 6c 69 74 65 5f 74  _NAME  "sqlite_t
4a930 65 6d 70 5f 6d 61 73 74 65 72 22 0a 0a 2f 2a 0a  emp_master"../*.
4a940 2a 2a 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 65  ** The root-page
4a950 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 64   of the master d
4a960 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a  atabase table..*
4a970 2f 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52  /.#define MASTER
4a980 5f 52 4f 4f 54 20 20 20 20 20 20 20 31 0a 0a 2f  _ROOT       1../
4a990 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  *.** The name of
4a9a0 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c   the schema tabl
4a9b0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 43  e..*/.#define SC
4a9c0 48 45 4d 41 5f 54 41 42 4c 45 28 78 29 20 20 28  HEMA_TABLE(x)  (
4a9d0 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 29 26 26  (!OMIT_TEMPDB)&&
4a9e0 28 78 3d 3d 31 29 3f 54 45 4d 50 5f 4d 41 53 54  (x==1)?TEMP_MAST
4a9f0 45 52 5f 4e 41 4d 45 3a 4d 41 53 54 45 52 5f 4e  ER_NAME:MASTER_N
4aa00 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f  AME)../*.** A co
4aa10 6e 76 65 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20  nvenience macro 
4aa20 74 68 61 74 20 72 65 74 75 72 6e 73 20 74 68 65  that returns the
4aa30 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   number of eleme
4aa40 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e 20 61 72 72  nts in.** an arr
4aa50 61 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 41  ay..*/.#define A
4aa60 72 72 61 79 53 69 7a 65 28 58 29 20 20 20 20 28  rraySize(X)    (
4aa70 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 58 29 2f  (int)(sizeof(X)/
4aa80 73 69 7a 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a  sizeof(X[0])))..
4aa90 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
4aaa0 69 6e 67 20 76 61 6c 75 65 20 61 73 20 61 20 64  ing value as a d
4aab0 65 73 74 72 75 63 74 6f 72 20 6d 65 61 6e 73 20  estructor means 
4aac0 74 6f 20 75 73 65 20 73 71 6c 69 74 65 33 44 62  to use sqlite3Db
4aad0 46 72 65 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20  Free()..** This 
4aae0 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65  is an internal e
4aaf0 78 74 65 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49  xtension to SQLI
4ab00 54 45 5f 53 54 41 54 49 43 20 61 6e 64 20 53 51  TE_STATIC and SQ
4ab10 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a  LITE_TRANSIENT..
4ab20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
4ab30 45 5f 44 59 4e 41 4d 49 43 20 20 20 28 28 73 71  E_DYNAMIC   ((sq
4ab40 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72  lite3_destructor
4ab50 5f 74 79 70 65 29 73 71 6c 69 74 65 33 44 62 46  _type)sqlite3DbF
4ab60 72 65 65 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  ree)../*.** When
4ab70 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
4ab80 20 69 73 20 64 65 66 69 6e 65 64 2c 20 69 74 20   is defined, it 
4ab90 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74  means that the t
4aba0 61 72 67 65 74 20 70 6c 61 74 66 6f 72 6d 20 64  arget platform d
4abb0 6f 65 73 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f  oes.** not suppo
4abc0 72 74 20 57 72 69 74 61 62 6c 65 20 53 74 61 74  rt Writable Stat
4abd0 69 63 20 44 61 74 61 20 28 57 53 44 29 20 73 75  ic Data (WSD) su
4abe0 63 68 20 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64  ch as global and
4abf0 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65   static variable
4ac00 73 2e 0a 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62  s..** All variab
4ac10 6c 65 73 20 6d 75 73 74 20 65 69 74 68 65 72 20  les must either 
4ac20 62 65 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20  be on the stack 
4ac30 6f 72 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61  or dynamically a
4ac40 6c 6c 6f 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a  llocated from.**
4ac50 20 74 68 65 20 68 65 61 70 2e 20 20 57 68 65 6e   the heap.  When
4ac60 20 57 53 44 20 69 73 20 75 6e 73 75 70 70 6f 72   WSD is unsuppor
4ac70 74 65 64 2c 20 74 68 65 20 76 61 72 69 61 62 6c  ted, the variabl
4ac80 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 73  e declarations s
4ac90 63 61 74 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f  cattered.** thro
4aca0 75 67 68 6f 75 74 20 74 68 65 20 53 51 4c 69 74  ughout the SQLit
4acb0 65 20 63 6f 64 65 20 6d 75 73 74 20 62 65 63 6f  e code must beco
4acc0 6d 65 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73  me constants ins
4acd0 74 65 61 64 2e 20 20 54 68 65 20 53 51 4c 49 54  tead.  The SQLIT
4ace0 45 5f 57 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69  E_WSD.** macro i
4acf0 73 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20  s used for this 
4ad00 70 75 72 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e  purpose.  And in
4ad10 73 74 65 61 64 20 6f 66 20 72 65 66 65 72 65 6e  stead of referen
4ad20 63 69 6e 67 20 74 68 65 20 76 61 72 69 61 62 6c  cing the variabl
4ad30 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 77  e.** directly, w
4ad40 65 20 75 73 65 20 69 74 73 20 63 6f 6e 73 74 61  e use its consta
4ad50 6e 74 20 61 73 20 61 20 6b 65 79 20 74 6f 20 6c  nt as a key to l
4ad60 6f 6f 6b 75 70 20 74 68 65 20 72 75 6e 2d 74 69  ookup the run-ti
4ad70 6d 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20  me allocated.** 
4ad80 62 75 66 66 65 72 20 74 68 61 74 20 68 6f 6c 64  buffer that hold
4ad90 73 20 72 65 61 6c 20 76 61 72 69 61 62 6c 65 2e  s real variable.
4ada0 20 20 54 68 65 20 63 6f 6e 73 74 61 6e 74 20 69    The constant i
4adb0 73 20 61 6c 73 6f 20 74 68 65 20 69 6e 69 74 69  s also the initi
4adc0 61 6c 69 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68  alizer.** for th
4add0 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63  e run-time alloc
4ade0 61 74 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a  ated buffer..**.
4adf0 2a 2a 20 49 6e 20 74 68 65 20 75 73 75 61 6c 20  ** In the usual 
4ae00 63 61 73 65 20 77 68 65 72 65 20 57 53 44 20 69  case where WSD i
4ae10 73 20 73 75 70 70 6f 72 74 65 64 2c 20 74 68 65  s supported, the
4ae20 20 53 51 4c 49 54 45 5f 57 53 44 20 61 6e 64 20   SQLITE_WSD and 
4ae30 47 4c 4f 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73  GLOBAL.** macros
4ae40 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61   become no-ops a
4ae50 6e 64 20 68 61 76 65 20 7a 65 72 6f 20 70 65 72  nd have zero per
4ae60 66 6f 72 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e  formance impact.
4ae70 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
4ae80 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 23 64 65  E_OMIT_WSD.  #de
4ae90 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20  fine SQLITE_WSD 
4aea0 63 6f 6e 73 74 0a 20 20 23 64 65 66 69 6e 65 20  const.  #define 
4aeb0 47 4c 4f 42 41 4c 28 74 2c 76 29 20 28 2a 28 74  GLOBAL(t,v) (*(t
4aec0 2a 29 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 69  *)sqlite3_wsd_fi
4aed0 6e 64 28 28 76 6f 69 64 2a 29 26 28 76 29 2c 20  nd((void*)&(v), 
4aee0 73 69 7a 65 6f 66 28 76 29 29 29 0a 20 20 23 64  sizeof(v))).  #d
4aef0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f  efine sqlite3Glo
4af00 62 61 6c 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c  balConfig GLOBAL
4af10 28 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43  (struct Sqlite3C
4af20 6f 6e 66 69 67 2c 20 73 71 6c 69 74 65 33 43 6f  onfig, sqlite3Co
4af30 6e 66 69 67 29 0a 53 51 4c 49 54 45 5f 41 50 49  nfig).SQLITE_API
4af40 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77     int sqlite3_w
4af50 73 64 5f 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69  sd_init(int N, i
4af60 6e 74 20 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  nt J);.SQLITE_AP
4af70 49 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  I   void *sqlite
4af80 33 5f 77 73 64 5f 66 69 6e 64 28 76 6f 69 64 20  3_wsd_find(void 
4af90 2a 4b 2c 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73  *K, int L);.#els
4afa0 65 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49  e.  #define SQLI
4afb0 54 45 5f 57 53 44 20 0a 20 20 23 64 65 66 69 6e  TE_WSD .  #defin
4afc0 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a  e GLOBAL(t,v) v.
4afd0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
4afe0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71  3GlobalConfig sq
4aff0 6c 69 74 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64  lite3Config.#end
4b000 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  if../*.** The fo
4b010 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 61  llowing macros a
4b020 72 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 72  re used to suppr
4b030 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 77 61 72  ess compiler war
4b040 6e 69 6e 67 73 20 61 6e 64 20 74 6f 0a 2a 2a 20  nings and to.** 
4b050 6d 61 6b 65 20 69 74 20 63 6c 65 61 72 20 74 6f  make it clear to
4b060 20 68 75 6d 61 6e 20 72 65 61 64 65 72 73 20 77   human readers w
4b070 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 70  hen a function p
4b080 61 72 61 6d 65 74 65 72 20 69 73 20 64 65 6c 69  arameter is deli
4b090 62 65 72 61 74 65 6c 79 20 0a 2a 2a 20 6c 65 66  berately .** lef
4b0a0 74 20 75 6e 75 73 65 64 20 77 69 74 68 69 6e 20  t unused within 
4b0b0 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75  the body of a fu
4b0c0 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 75 73 75  nction. This usu
4b0d0 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65  ally happens whe
4b0e0 6e 0a 2a 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20  n.** a function 
4b0f0 69 73 20 63 61 6c 6c 65 64 20 76 69 61 20 61 20  is called via a 
4b100 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
4b110 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 68  . For example th
4b120 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  e .** implementa
4b130 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 61  tion of an SQL a
4b140 67 67 72 65 67 61 74 65 20 73 74 65 70 20 63 61  ggregate step ca
4b150 6c 6c 62 61 63 6b 20 6d 61 79 20 6e 6f 74 20 75  llback may not u
4b160 73 65 20 74 68 65 0a 2a 2a 20 70 61 72 61 6d 65  se the.** parame
4b170 74 65 72 20 69 6e 64 69 63 61 74 69 6e 67 20 74  ter indicating t
4b180 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  he number of arg
4b190 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f  uments passed to
4b1a0 20 74 68 65 20 61 67 67 72 65 67 61 74 65 2c 0a   the aggregate,.
4b1b0 2a 2a 20 69 66 20 69 74 20 6b 6e 6f 77 73 20 74  ** if it knows t
4b1c0 68 61 74 20 74 68 69 73 20 69 73 20 65 6e 66 6f  hat this is enfo
4b1d0 72 63 65 64 20 65 6c 73 65 77 68 65 72 65 2e 0a  rced elsewhere..
4b1e0 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e  **.** When a fun
4b1f0 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20  ction parameter 
4b200 69 73 20 6e 6f 74 20 75 73 65 64 20 61 74 20 61  is not used at a
4b210 6c 6c 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f  ll within the bo
4b220 64 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e  dy of a function
4b230 2c 0a 2a 2a 20 69 74 20 69 73 20 67 65 6e 65 72  ,.** it is gener
4b240 61 6c 6c 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55  ally named "NotU
4b250 73 65 64 22 20 6f 72 20 22 4e 6f 74 55 73 65 64  sed" or "NotUsed
4b260 32 22 20 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67  2" to make thing
4b270 73 20 65 76 65 6e 20 63 6c 65 61 72 65 72 2e 0a  s even clearer..
4b280 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 73  ** However, thes
4b290 65 20 6d 61 63 72 6f 73 20 6d 61 79 20 61 6c 73  e macros may als
4b2a0 6f 20 62 65 20 75 73 65 64 20 74 6f 20 73 75 70  o be used to sup
4b2b0 70 72 65 73 73 20 77 61 72 6e 69 6e 67 73 20 72  press warnings r
4b2c0 65 6c 61 74 65 64 20 74 6f 0a 2a 2a 20 70 61 72  elated to.** par
4b2d0 61 6d 65 74 65 72 73 20 74 68 61 74 20 6d 61 79  ameters that may
4b2e0 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 75   or may not be u
4b2f0 73 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  sed depending on
4b300 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74   compilation opt
4b310 69 6f 6e 73 2e 0a 2a 2a 20 46 6f 72 20 65 78 61  ions..** For exa
4b320 6d 70 6c 65 20 74 68 6f 73 65 20 70 61 72 61 6d  mple those param
4b330 65 74 65 72 73 20 6f 6e 6c 79 20 75 73 65 64 20  eters only used 
4b340 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74  in assert() stat
4b350 65 6d 65 6e 74 73 2e 20 49 6e 20 74 68 65 73 65  ements. In these
4b360 0a 2a 2a 20 63 61 73 65 73 20 74 68 65 20 70 61  .** cases the pa
4b370 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 61 6d  rameters are nam
4b380 65 64 20 61 73 20 70 65 72 20 74 68 65 20 75 73  ed as per the us
4b390 75 61 6c 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2e  ual conventions.
4b3a0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 55 53  .*/.#define UNUS
4b3b0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29 20  ED_PARAMETER(x) 
4b3c0 28 76 6f 69 64 29 28 78 29 0a 23 64 65 66 69 6e  (void)(x).#defin
4b3d0 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54  e UNUSED_PARAMET
4b3e0 45 52 32 28 78 2c 79 29 20 55 4e 55 53 45 44 5f  ER2(x,y) UNUSED_
4b3f0 50 41 52 41 4d 45 54 45 52 28 78 29 2c 55 4e 55  PARAMETER(x),UNU
4b400 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79 29  SED_PARAMETER(y)
4b410 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20  ../*.** Forward 
4b420 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 73 74  references to st
4b430 72 75 63 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65  ructures.*/.type
4b440 64 65 66 20 73 74 72 75 63 74 20 41 67 67 49 6e  def struct AggIn
4b450 66 6f 20 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65  fo AggInfo;.type
4b460 64 65 66 20 73 74 72 75 63 74 20 41 75 74 68 43  def struct AuthC
4b470 6f 6e 74 65 78 74 20 41 75 74 68 43 6f 6e 74 65  ontext AuthConte
4b480 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  xt;.typedef stru
4b490 63 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 41  ct AutoincInfo A
4b4a0 75 74 6f 69 6e 63 49 6e 66 6f 3b 0a 74 79 70 65  utoincInfo;.type
4b4b0 64 65 66 20 73 74 72 75 63 74 20 42 69 74 76 65  def struct Bitve
4b4c0 63 20 42 69 74 76 65 63 3b 0a 74 79 70 65 64 65  c Bitvec;.typede
4b4d0 66 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 20  f struct RowSet 
4b4e0 52 6f 77 53 65 74 3b 0a 74 79 70 65 64 65 66 20  RowSet;.typedef 
4b4f0 73 74 72 75 63 74 20 43 6f 6c 6c 53 65 71 20 43  struct CollSeq C
4b500 6f 6c 6c 53 65 71 3b 0a 74 79 70 65 64 65 66 20  ollSeq;.typedef 
4b510 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 43 6f  struct Column Co
4b520 6c 75 6d 6e 3b 0a 74 79 70 65 64 65 66 20 73 74  lumn;.typedef st
4b530 72 75 63 74 20 44 62 20 44 62 3b 0a 74 79 70 65  ruct Db Db;.type
4b540 64 65 66 20 73 74 72 75 63 74 20 53 63 68 65 6d  def struct Schem
4b550 61 20 53 63 68 65 6d 61 3b 0a 74 79 70 65 64 65  a Schema;.typede
4b560 66 20 73 74 72 75 63 74 20 45 78 70 72 20 45 78  f struct Expr Ex
4b570 70 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  pr;.typedef stru
4b580 63 74 20 45 78 70 72 4c 69 73 74 20 45 78 70 72  ct ExprList Expr
4b590 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73 74  List;.typedef st
4b5a0 72 75 63 74 20 45 78 70 72 53 70 61 6e 20 45 78  ruct ExprSpan Ex
4b5b0 70 72 53 70 61 6e 3b 0a 74 79 70 65 64 65 66 20  prSpan;.typedef 
4b5c0 73 74 72 75 63 74 20 46 4b 65 79 20 46 4b 65 79  struct FKey FKey
4b5d0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b5e0 20 46 75 6e 63 44 65 66 20 46 75 6e 63 44 65 66   FuncDef FuncDef
4b5f0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b600 20 46 75 6e 63 44 65 66 48 61 73 68 20 46 75 6e   FuncDefHash Fun
4b610 63 44 65 66 48 61 73 68 3b 0a 74 79 70 65 64 65  cDefHash;.typede
4b620 66 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 20  f struct IdList 
4b630 49 64 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20  IdList;.typedef 
4b640 73 74 72 75 63 74 20 49 6e 64 65 78 20 49 6e 64  struct Index Ind
4b650 65 78 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ex;.typedef stru
4b660 63 74 20 49 6e 64 65 78 53 61 6d 70 6c 65 20 49  ct IndexSample I
4b670 6e 64 65 78 53 61 6d 70 6c 65 3b 0a 74 79 70 65  ndexSample;.type
4b680 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 43 6c  def struct KeyCl
4b690 61 73 73 20 4b 65 79 43 6c 61 73 73 3b 0a 74 79  ass KeyClass;.ty
4b6a0 70 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 79  pedef struct Key
4b6b0 49 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b 0a 74 79  Info KeyInfo;.ty
4b6c0 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f  pedef struct Loo
4b6d0 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 73 69 64 65  kaside Lookaside
4b6e0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b6f0 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 4c   LookasideSlot L
4b700 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0a 74 79  ookasideSlot;.ty
4b710 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 6f 64  pedef struct Mod
4b720 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 79 70 65  ule Module;.type
4b730 64 65 66 20 73 74 72 75 63 74 20 4e 61 6d 65 43  def struct NameC
4b740 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e 74 65  ontext NameConte
4b750 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  xt;.typedef stru
4b760 63 74 20 50 61 72 73 65 20 50 61 72 73 65 3b 0a  ct Parse Parse;.
4b770 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
4b780 61 76 65 70 6f 69 6e 74 20 53 61 76 65 70 6f 69  avepoint Savepoi
4b790 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  nt;.typedef stru
4b7a0 63 74 20 53 65 6c 65 63 74 20 53 65 6c 65 63 74  ct Select Select
4b7b0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b7c0 20 53 72 63 4c 69 73 74 20 53 72 63 4c 69 73 74   SrcList SrcList
4b7d0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4b7e0 20 53 74 72 41 63 63 75 6d 20 53 74 72 41 63 63   StrAccum StrAcc
4b7f0 75 6d 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  um;.typedef stru
4b800 63 74 20 54 61 62 6c 65 20 54 61 62 6c 65 3b 0a  ct Table Table;.
4b810 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54  typedef struct T
4b820 61 62 6c 65 4c 6f 63 6b 20 54 61 62 6c 65 4c 6f  ableLock TableLo
4b830 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ck;.typedef stru
4b840 63 74 20 54 6f 6b 65 6e 20 54 6f 6b 65 6e 3b 0a  ct Token Token;.
4b850 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54  typedef struct T
4b860 72 69 67 67 65 72 50 72 67 20 54 72 69 67 67 65  riggerPrg Trigge
4b870 72 50 72 67 3b 0a 74 79 70 65 64 65 66 20 73 74  rPrg;.typedef st
4b880 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70  ruct TriggerStep
4b890 20 54 72 69 67 67 65 72 53 74 65 70 3b 0a 74 79   TriggerStep;.ty
4b8a0 70 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69  pedef struct Tri
4b8b0 67 67 65 72 20 54 72 69 67 67 65 72 3b 0a 74 79  gger Trigger;.ty
4b8c0 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e 70  pedef struct Unp
4b8d0 61 63 6b 65 64 52 65 63 6f 72 64 20 55 6e 70 61  ackedRecord Unpa
4b8e0 63 6b 65 64 52 65 63 6f 72 64 3b 0a 74 79 70 65  ckedRecord;.type
4b8f0 64 65 66 20 73 74 72 75 63 74 20 56 54 61 62 6c  def struct VTabl
4b900 65 20 56 54 61 62 6c 65 3b 0a 74 79 70 65 64 65  e VTable;.typede
4b910 66 20 73 74 72 75 63 74 20 57 61 6c 6b 65 72 20  f struct Walker 
4b920 57 61 6c 6b 65 72 3b 0a 74 79 70 65 64 65 66 20  Walker;.typedef 
4b930 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e  struct WherePlan
4b940 20 57 68 65 72 65 50 6c 61 6e 3b 0a 74 79 70 65   WherePlan;.type
4b950 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65  def struct Where
4b960 49 6e 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a  Info WhereInfo;.
4b970 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
4b980 68 65 72 65 4c 65 76 65 6c 20 57 68 65 72 65 4c  hereLevel WhereL
4b990 65 76 65 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  evel;../*.** Def
4b9a0 65 72 20 73 6f 75 72 63 69 6e 67 20 76 64 62 65  er sourcing vdbe
4b9b0 2e 68 20 61 6e 64 20 62 74 72 65 65 2e 68 20 75  .h and btree.h u
4b9c0 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 20 22  ntil after the "
4b9d0 75 38 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73  u8" and .** "Bus
4b9e0 79 48 61 6e 64 6c 65 72 22 20 74 79 70 65 64 65  yHandler" typede
4b9f0 66 73 2e 20 76 64 62 65 2e 68 20 61 6c 73 6f 20  fs. vdbe.h also 
4ba00 72 65 71 75 69 72 65 73 20 61 20 66 65 77 20 6f  requires a few o
4ba10 66 20 74 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20  f the opaque.** 
4ba20 70 6f 69 6e 74 65 72 20 74 79 70 65 73 20 28 69  pointer types (i
4ba30 2e 65 2e 20 46 75 6e 63 44 65 66 29 20 64 65 66  .e. FuncDef) def
4ba40 69 6e 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f  ined above..*/./
4ba50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
4ba60 6e 63 6c 75 64 65 20 62 74 72 65 65 2e 68 20 69  nclude btree.h i
4ba70 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
4ba80 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
4ba90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
4baa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
4bab0 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e  egin file btree.
4bac0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4bad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
4baf0 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
4bb00 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
4bb10 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
4bb20 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
4bb30 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
4bb40 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
4bb50 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
4bb60 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
4bb70 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
4bb80 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
4bb90 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
4bba0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
4bbb0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
4bbc0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
4bbd0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
4bbe0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
4bbf0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
4bc00 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
4bc10 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
4bc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bc30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bc60 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65  *****.** This he
4bc70 61 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65  ader file define
4bc80 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  s the interface 
4bc90 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20  that the sqlite 
4bca0 42 2d 54 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73  B-Tree file.** s
4bcb0 75 62 73 79 73 74 65 6d 2e 20 20 53 65 65 20 63  ubsystem.  See c
4bcc0 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 73  omments in the s
4bcd0 6f 75 72 63 65 20 63 6f 64 65 20 66 6f 72 20 61  ource code for a
4bce0 20 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69   detailed descri
4bcf0 70 74 69 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74  ption.** of what
4bd00 20 65 61 63 68 20 69 6e 74 65 72 66 61 63 65 20   each interface 
4bd10 72 6f 75 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2f  routine does..*/
4bd20 0a 23 69 66 6e 64 65 66 20 5f 42 54 52 45 45 5f  .#ifndef _BTREE_
4bd30 48 5f 0a 23 64 65 66 69 6e 65 20 5f 42 54 52 45  H_.#define _BTRE
4bd40 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 3a 20 54  E_H_../* TODO: T
4bd50 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 20 69  his definition i
4bd60 73 20 6a 75 73 74 20 69 6e 63 6c 75 64 65 64 20  s just included 
4bd70 73 6f 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73  so other modules
4bd80 20 63 6f 6d 70 69 6c 65 2e 20 49 74 0a 2a 2a 20   compile. It.** 
4bd90 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 76 69  needs to be revi
4bda0 73 69 74 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  sited..*/.#defin
4bdb0 65 20 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45  e SQLITE_N_BTREE
4bdc0 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a 2a 2a 20  _META 10../*.** 
4bdd0 49 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f  If defined as no
4bde0 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63  n-zero, auto-vac
4bdf0 75 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62  uum is enabled b
4be00 79 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72  y default. Other
4be10 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20  wise.** it must 
4be20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72  be turned on for
4be30 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75   each database u
4be40 73 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74  sing "PRAGMA aut
4be50 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a  o_vacuum = 1"..*
4be60 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
4be70 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43  _DEFAULT_AUTOVAC
4be80 55 55 4d 0a 20 20 23 64 65 66 69 6e 65 20 53 51  UUM.  #define SQ
4be90 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54  LITE_DEFAULT_AUT
4bea0 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66  OVACUUM 0.#endif
4beb0 0a 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  ..#define BTREE_
4bec0 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 20  AUTOVACUUM_NONE 
4bed0 30 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e  0        /* Do n
4bee0 6f 74 20 64 6f 20 61 75 74 6f 2d 76 61 63 75 75  ot do auto-vacuu
4bef0 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52  m */.#define BTR
4bf00 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55  EE_AUTOVACUUM_FU
4bf10 4c 4c 20 31 20 20 20 20 20 20 20 20 2f 2a 20 44  LL 1        /* D
4bf20 6f 20 66 75 6c 6c 20 61 75 74 6f 2d 76 61 63 75  o full auto-vacu
4bf30 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  um */.#define BT
4bf40 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49  REE_AUTOVACUUM_I
4bf50 4e 43 52 20 32 20 20 20 20 20 20 20 20 2f 2a 20  NCR 2        /* 
4bf60 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75  Incremental vacu
4bf70 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72  um */../*.** For
4bf80 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
4bf90 73 20 6f 66 20 73 74 72 75 63 74 75 72 65 0a 2a  s of structure.*
4bfa0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
4bfb0 20 42 74 72 65 65 20 42 74 72 65 65 3b 0a 74 79   Btree Btree;.ty
4bfc0 70 65 64 65 66 20 73 74 72 75 63 74 20 42 74 43  pedef struct BtC
4bfd0 75 72 73 6f 72 20 42 74 43 75 72 73 6f 72 3b 0a  ursor BtCursor;.
4bfe0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 42  typedef struct B
4bff0 74 53 68 61 72 65 64 20 42 74 53 68 61 72 65 64  tShared BtShared
4c000 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4c010 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79   BtreeMutexArray
4c020 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79   BtreeMutexArray
4c030 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74  ;../*.** This st
4c040 72 75 63 74 75 72 65 20 72 65 63 6f 72 64 73 20  ructure records 
4c050 61 6c 6c 20 6f 66 20 74 68 65 20 42 74 72 65 65  all of the Btree
4c060 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 68  s that need to h
4c070 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74 65 78 20 62  old.** a mutex b
4c080 65 66 6f 72 65 20 77 65 20 65 6e 74 65 72 20 73  efore we enter s
4c090 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29  qlite3VdbeExec()
4c0a0 2e 20 20 54 68 65 20 42 74 72 65 65 73 20 61 72  .  The Btrees ar
4c0b0 65 0a 2a 2a 20 61 72 65 20 70 6c 61 63 65 64 20  e.** are placed 
4c0c0 69 6e 20 61 42 74 72 65 65 5b 5d 20 69 6e 20 6f  in aBtree[] in o
4c0d0 72 64 65 72 20 6f 66 20 61 42 74 72 65 65 5b 5d  rder of aBtree[]
4c0e0 2d 3e 70 42 74 2e 20 20 54 68 61 74 20 77 61 79  ->pBt.  That way
4c0f0 2c 0a 2a 2a 20 77 65 20 63 61 6e 20 61 6c 77 61  ,.** we can alwa
4c100 79 73 20 6c 6f 63 6b 20 61 6e 64 20 75 6e 6c 6f  ys lock and unlo
4c110 63 6b 20 74 68 65 6d 20 61 6c 6c 20 71 75 69 63  ck them all quic
4c120 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42  kly..*/.struct B
4c130 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 7b  treeMutexArray {
4c140 0a 20 20 69 6e 74 20 6e 4d 75 74 65 78 3b 0a 20  .  int nMutex;. 
4c150 20 42 74 72 65 65 20 2a 61 42 74 72 65 65 5b 53   Btree *aBtree[S
4c160 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
4c170 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53 51 4c 49  ED+1];.};...SQLI
4c180 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c190 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28  qlite3BtreeOpen(
4c1a0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
4c1b0 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e  Filename,   /* N
4c1c0 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
4c1d0 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  file to open */.
4c1e0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
4c1f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73             /* As
4c200 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73  sociated databas
4c210 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
4c220 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65    Btree **ppBtre
4c230 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  e,         /* Re
4c240 74 75 72 6e 20 6f 70 65 6e 20 42 74 72 65 65 2a  turn open Btree*
4c250 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66   here */.  int f
4c260 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
4c270 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a      /* Flags */.
4c280 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20    int vfsFlags  
4c290 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
4c2a0 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75  ags passed throu
4c2b0 67 68 20 74 6f 20 56 46 53 20 6f 70 65 6e 20 2a  gh to VFS open *
4c2c0 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61  /.);../* The fla
4c2d0 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  gs parameter to 
4c2e0 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e  sqlite3BtreeOpen
4c2f0 20 63 61 6e 20 62 65 20 74 68 65 20 62 69 74 77   can be the bitw
4c300 69 73 65 20 6f 72 20 6f 66 20 74 68 65 0a 2a 2a  ise or of the.**
4c310 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65   following value
4c320 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20  s..**.** NOTE:  
4c330 54 68 65 73 65 20 76 61 6c 75 65 73 20 6d 75 73  These values mus
4c340 74 20 6d 61 74 63 68 20 74 68 65 20 63 6f 72 72  t match the corr
4c350 65 73 70 6f 6e 64 69 6e 67 20 50 41 47 45 52 5f  esponding PAGER_
4c360 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 70 61   values in.** pa
4c370 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ger.h..*/.#defin
4c380 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55  e BTREE_OMIT_JOU
4c390 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44 6f 20 6e  RNAL  1  /* Do n
4c3a0 6f 74 20 75 73 65 20 6a 6f 75 72 6e 61 6c 2e 20  ot use journal. 
4c3b0 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a   No argument */.
4c3c0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4e 4f  #define BTREE_NO
4c3d0 5f 52 45 41 44 4c 4f 43 4b 20 20 20 32 20 20 2f  _READLOCK   2  /
4c3e0 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73  * Omit readlocks
4c3f0 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c   on readonly fil
4c400 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  es */.#define BT
4c410 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20 20  REE_MEMORY      
4c420 20 20 34 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72    4  /* In-memor
4c430 79 20 44 42 2e 20 20 4e 6f 20 61 72 67 75 6d 65  y DB.  No argume
4c440 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  nt */.#define BT
4c450 52 45 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20  REE_READONLY    
4c460 20 20 38 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65    8  /* Open the
4c470 20 64 61 74 61 62 61 73 65 20 69 6e 20 72 65 61   database in rea
4c480 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a 2f 0a 23  d-only mode */.#
4c490 64 65 66 69 6e 65 20 42 54 52 45 45 5f 52 45 41  define BTREE_REA
4c4a0 44 57 52 49 54 45 20 20 20 20 31 36 20 20 2f 2a  DWRITE    16  /*
4c4b0 20 4f 70 65 6e 20 66 6f 72 20 62 6f 74 68 20 72   Open for both r
4c4c0 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69 74 69  eading and writi
4c4d0 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54  ng */.#define BT
4c4e0 52 45 45 5f 43 52 45 41 54 45 20 20 20 20 20 20  REE_CREATE      
4c4f0 20 33 32 20 20 2f 2a 20 43 72 65 61 74 65 20 74   32  /* Create t
4c500 68 65 20 64 61 74 61 62 61 73 65 20 69 66 20 69  he database if i
4c510 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  t does not exist
4c520 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56   */..SQLITE_PRIV
4c530 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4c540 74 72 65 65 43 6c 6f 73 65 28 42 74 72 65 65 2a  treeClose(Btree*
4c550 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c560 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c570 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 42  eeSetCacheSize(B
4c580 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  tree*,int);.SQLI
4c590 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c5a0 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61  qlite3BtreeSetSa
4c5b0 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65 65 2a  fetyLevel(Btree*
4c5c0 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,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 53 79 6e 63 44 69  lite3BtreeSyncDi
4c5f0 73 61 62 6c 65 64 28 42 74 72 65 65 2a 29 3b 0a  sabled(Btree*);.
4c600 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c610 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
4c620 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65  etPageSize(Btree
4c630 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 73 69   *p, int nPagesi
4c640 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65  ze, int nReserve
4c650 2c 20 69 6e 74 20 65 46 69 78 29 3b 0a 53 51 4c  , int eFix);.SQL
4c660 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c670 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
4c680 61 67 65 53 69 7a 65 28 42 74 72 65 65 2a 29 3b  ageSize(Btree*);
4c690 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4c6a0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4c6b0 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74 72  MaxPageCount(Btr
4c6c0 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ee*,int);.SQLITE
4c6d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c6e0 69 74 65 33 42 74 72 65 65 47 65 74 52 65 73 65  ite3BtreeGetRese
4c6f0 72 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  rve(Btree*);.SQL
4c700 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c710 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41  sqlite3BtreeSetA
4c720 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20  utoVacuum(Btree 
4c730 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
4c740 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c750 74 65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56  te3BtreeGetAutoV
4c760 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29 3b 0a  acuum(Btree *);.
4c770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c780 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 42  nt sqlite3BtreeB
4c790 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65 2a  eginTrans(Btree*
4c7a0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
4c7b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c7c0 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
4c7d0 65 4f 6e 65 28 42 74 72 65 65 2a 2c 20 63 6f 6e  eOne(Btree*, con
4c7e0 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  st char *zMaster
4c7f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c800 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c810 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
4c820 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
4c830 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c840 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28  ite3BtreeCommit(
4c850 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
4c860 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4c870 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b  te3BtreeRollback
4c880 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
4c890 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c8a0 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74  ite3BtreeBeginSt
4c8b0 6d 74 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a  mt(Btree*,int);.
4c8c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c8d0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
4c8e0 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65  reateTable(Btree
4c8f0 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20 66 6c 61  *, int*, int fla
4c900 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  gs);.SQLITE_PRIV
4c910 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4c920 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 42 74  treeIsInTrans(Bt
4c930 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
4c940 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c950 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72  3BtreeIsInReadTr
4c960 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  ans(Btree*);.SQL
4c970 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c980 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
4c990 42 61 63 6b 75 70 28 42 74 72 65 65 2a 29 3b 0a  Backup(Btree*);.
4c9a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4c9b0 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65  oid *sqlite3Btre
4c9c0 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 2c  eSchema(Btree *,
4c9d0 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f   int, void(*)(vo
4c9e0 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  id *));.SQLITE_P
4c9f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4ca00 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f 63  e3BtreeSchemaLoc
4ca10 6b 65 64 28 42 74 72 65 65 20 2a 70 42 74 72 65  ked(Btree *pBtre
4ca20 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
4ca30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4ca40 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72  reeLockTable(Btr
4ca50 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20  ee *pBtree, int 
4ca60 69 54 61 62 2c 20 75 38 20 69 73 57 72 69 74 65  iTab, u8 isWrite
4ca70 4c 6f 63 6b 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Lock);.SQLITE_PR
4ca80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4ca90 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28  3BtreeSavepoint(
4caa0 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c 20 69 6e  Btree *, int, in
4cab0 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
4cac0 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
4cad0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46  sqlite3BtreeGetF
4cae0 69 6c 65 6e 61 6d 65 28 42 74 72 65 65 20 2a 29  ilename(Btree *)
4caf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4cb00 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
4cb10 69 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72  ite3BtreeGetJour
4cb20 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 29  nalname(Btree *)
4cb30 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4cb40 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4cb50 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65 20  eCopyFile(Btree 
4cb60 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a 0a 53 51  *, Btree *);..SQ
4cb70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4cb80 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 63   sqlite3BtreeInc
4cb90 72 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a 29  rVacuum(Btree *)
4cba0 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61 67 73 20  ;../* The flags 
4cbb0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
4cbc0 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54  ite3BtreeCreateT
4cbd0 61 62 6c 65 20 63 61 6e 20 62 65 20 74 68 65 20  able can be the 
4cbe0 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a 20 6f 66  bitwise OR.** of
4cbf0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
4cc00 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65  lags:.*/.#define
4cc10 20 42 54 52 45 45 5f 49 4e 54 4b 45 59 20 20 20   BTREE_INTKEY   
4cc20 20 20 31 20 20 20 20 2f 2a 20 54 61 62 6c 65 20    1    /* Table 
4cc30 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62 69 74 20  has only 64-bit 
4cc40 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 6b  signed integer k
4cc50 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  eys */.#define B
4cc60 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 20 20  TREE_ZERODATA   
4cc70 32 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 68 61  2    /* Table ha
4cc80 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d 20 6e 6f  s keys only - no
4cc90 20 64 61 74 61 20 2a 2f 0a 23 64 65 66 69 6e 65   data */.#define
4cca0 20 42 54 52 45 45 5f 4c 45 41 46 44 41 54 41 20   BTREE_LEAFDATA 
4ccb0 20 20 34 20 20 20 20 2f 2a 20 44 61 74 61 20 73    4    /* Data s
4ccc0 74 6f 72 65 64 20 69 6e 20 6c 65 61 76 65 73 20  tored in leaves 
4ccd0 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69 65 73 20 49  only.  Implies I
4cce0 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c 49 54 45  NTKEY */..SQLITE
4ccf0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4cd00 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61 62  ite3BtreeDropTab
4cd10 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20  le(Btree*, int, 
4cd20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  int*);.SQLITE_PR
4cd30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4cd40 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c 65  3BtreeClearTable
4cd50 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Btree*, int, in
4cd60 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
4cd70 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4cd80 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73  BtreeTripAllCurs
4cd90 6f 72 73 28 42 74 72 65 65 2a 2c 20 69 6e 74 29  ors(Btree*, int)
4cda0 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
4cdb0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
4cdc0 72 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65  reeGetMeta(Btree
4cdd0 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 69 64   *pBtree, int id
4cde0 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 65 29 3b  x, u32 *pValue);
4cdf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4ce00 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4ce10 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 65 65  UpdateMeta(Btree
4ce20 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32 20  *, int idx, u32 
4ce30 76 61 6c 75 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54  value);../*.** T
4ce40 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
4ce50 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 42 74  ter to sqlite3Bt
4ce60 72 65 65 47 65 74 4d 65 74 61 20 6f 72 20 73 71  reeGetMeta or sq
4ce70 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
4ce80 4d 65 74 61 0a 2a 2a 20 73 68 6f 75 6c 64 20 62  Meta.** should b
4ce90 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c  e one of the fol
4cea0 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e 20 54  lowing values. T
4ceb0 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  he integer value
4cec0 73 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 0a  s are assigned .
4ced0 2a 2a 20 74 6f 20 63 6f 6e 73 74 61 6e 74 73 20  ** to constants 
4cee0 73 6f 20 74 68 61 74 20 74 68 65 20 6f 66 66 73  so that the offs
4cef0 65 74 20 6f 66 20 74 68 65 20 63 6f 72 72 65 73  et of the corres
4cf00 70 6f 6e 64 69 6e 67 20 66 69 65 6c 64 20 69 6e  ponding field in
4cf10 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61   an.** SQLite da
4cf20 74 61 62 61 73 65 20 68 65 61 64 65 72 20 6d 61  tabase header ma
4cf30 79 20 62 65 20 66 6f 75 6e 64 20 75 73 69 6e 67  y be found using
4cf40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   the following f
4cf50 6f 72 6d 75 6c 61 3a 0a 2a 2a 0a 2a 2a 20 20 20  ormula:.**.**   
4cf60 6f 66 66 73 65 74 20 3d 20 33 36 20 2b 20 28 69  offset = 36 + (i
4cf70 64 78 20 2a 20 34 29 0a 2a 2a 0a 2a 2a 20 46 6f  dx * 4).**.** Fo
4cf80 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 66  r example, the f
4cf90 72 65 65 2d 70 61 67 65 2d 63 6f 75 6e 74 20 66  ree-page-count f
4cfa0 69 65 6c 64 20 69 73 20 6c 6f 63 61 74 65 64 20  ield is located 
4cfb0 61 74 20 62 79 74 65 20 6f 66 66 73 65 74 20 33  at byte offset 3
4cfc0 36 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 61  6 of.** the data
4cfd0 62 61 73 65 20 66 69 6c 65 20 68 65 61 64 65 72  base file header
4cfe0 2e 20 54 68 65 20 69 6e 63 72 2d 76 61 63 75 75  . The incr-vacuu
4cff0 6d 2d 66 6c 61 67 20 66 69 65 6c 64 20 69 73 20  m-flag field is 
4d000 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a 20 62 79  located at.** by
4d010 74 65 20 6f 66 66 73 65 74 20 36 34 20 28 3d 3d  te offset 64 (==
4d020 20 33 36 2b 34 2a 37 29 2e 0a 2a 2f 0a 23 64 65   36+4*7)..*/.#de
4d030 66 69 6e 65 20 42 54 52 45 45 5f 46 52 45 45 5f  fine BTREE_FREE_
4d040 50 41 47 45 5f 43 4f 55 4e 54 20 20 20 20 20 30  PAGE_COUNT     0
4d050 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 53  .#define BTREE_S
4d060 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 20 20 20  CHEMA_VERSION   
4d070 20 20 20 31 0a 23 64 65 66 69 6e 65 20 42 54 52     1.#define BTR
4d080 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 20  EE_FILE_FORMAT  
4d090 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65         2.#define
4d0a0 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43   BTREE_DEFAULT_C
4d0b0 41 43 48 45 5f 53 49 5a 45 20 20 33 0a 23 64 65  ACHE_SIZE  3.#de
4d0c0 66 69 6e 65 20 42 54 52 45 45 5f 4c 41 52 47 45  fine BTREE_LARGE
4d0d0 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 20 20 34  ST_ROOT_PAGE   4
4d0e0 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 54  .#define BTREE_T
4d0f0 45 58 54 5f 45 4e 43 4f 44 49 4e 47 20 20 20 20  EXT_ENCODING    
4d100 20 20 20 35 0a 23 64 65 66 69 6e 65 20 42 54 52     5.#define BTR
4d110 45 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e 20  EE_USER_VERSION 
4d120 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65         6.#define
4d130 20 42 54 52 45 45 5f 49 4e 43 52 5f 56 41 43 55   BTREE_INCR_VACU
4d140 55 4d 20 20 20 20 20 20 20 20 20 37 0a 0a 53 51  UM         7..SQ
4d150 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d160 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
4d170 73 6f 72 28 0a 20 20 42 74 72 65 65 2a 2c 20 20  sor(.  Btree*,  
4d180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d190 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
4d1a0 54 72 65 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  Tree containing 
4d1b0 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f  table to open */
4d1c0 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20  .  int iTable,  
4d1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d1e0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
4d1f0 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 20 2a 2f   of root page */
4d200 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20  .  int wrFlag,  
4d210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d220 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66 6f 72          /* 1 for
4d230 20 77 72 69 74 69 6e 67 2e 20 20 30 20 66 6f 72   writing.  0 for
4d240 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20   read-only */.  
4d250 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 2a 2c  struct KeyInfo*,
4d260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d270 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72       /* First ar
4d280 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d 70 61 72  gument to compar
4d290 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  e function */.  
4d2a0 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  BtCursor *pCurso
4d2b0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
4d2c0 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f       /* Space to
4d2d0 20 77 72 69 74 65 20 63 75 72 73 6f 72 20 73 74   write cursor st
4d2e0 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b 0a 53 51  ructure */.);.SQ
4d2f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d300 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
4d310 73 6f 72 53 69 7a 65 28 76 6f 69 64 29 3b 0a 53  sorSize(void);.S
4d320 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
4d330 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  id sqlite3BtreeC
4d340 75 72 73 6f 72 5a 65 72 6f 28 42 74 43 75 72 73  ursorZero(BtCurs
4d350 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  or*);..SQLITE_PR
4d360 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d370 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f  3BtreeCloseCurso
4d380 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51  r(BtCursor*);.SQ
4d390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4d3a0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
4d3b0 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20 42  etoUnpacked(.  B
4d3c0 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e 70 61  tCursor*,.  Unpa
4d3d0 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e 4b  ckedRecord *pUnK
4d3e0 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b 65 79  ey,.  i64 intKey
4d3f0 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a 20 20  ,.  int bias,.  
4d400 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53 51 4c  int *pRes.);.SQL
4d410 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4d420 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
4d430 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75 72  orHasMoved(BtCur
4d440 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c  sor*, int*);.SQL
4d450 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4d460 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65  sqlite3BtreeDele
4d470 74 65 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53  te(BtCursor*);.S
4d480 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d490 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  t sqlite3BtreeIn
4d4a0 73 65 72 74 28 42 74 43 75 72 73 6f 72 2a 2c 20  sert(BtCursor*, 
4d4b0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
4d4c0 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 20 20  , i64 nKey,.    
4d4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
4d4f0 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c  nst void *pData,
4d500 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20   int nData,.    
4d510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
4d530 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62 69 61  t nZero, int bia
4d540 73 2c 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c  s, int seekResul
4d550 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4d560 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4d570 72 65 65 46 69 72 73 74 28 42 74 43 75 72 73 6f  reeFirst(BtCurso
4d580 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a  r*, int *pRes);.
4d590 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4d5a0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  nt sqlite3BtreeL
4d5b0 61 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20 69  ast(BtCursor*, i
4d5c0 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49 54  nt *pRes);.SQLIT
4d5d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4d5e0 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 42  lite3BtreeNext(B
4d5f0 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70  tCursor*, int *p
4d600 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Res);.SQLITE_PRI
4d610 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4d620 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 6f  BtreeEof(BtCurso
4d630 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
4d640 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4d650 74 72 65 65 50 72 65 76 69 6f 75 73 28 42 74 43  treePrevious(BtC
4d660 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65  ursor*, int *pRe
4d670 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  s);.SQLITE_PRIVA
4d680 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4d690 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43 75 72  reeKeySize(BtCur
4d6a0 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53 69 7a 65  sor*, i64 *pSize
4d6b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4d6c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4d6d0 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72 2a 2c  eeKey(BtCursor*,
4d6e0 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32   u32 offset, u32
4d6f0 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51   amt, void*);.SQ
4d700 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
4d710 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
4d720 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42 74  BtreeKeyFetch(Bt
4d730 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 41  Cursor*, int *pA
4d740 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  mt);.SQLITE_PRIV
4d750 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ATE const void *
4d760 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
4d770 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a 2c  Fetch(BtCursor*,
4d780 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51 4c   int *pAmt);.SQL
4d790 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4d7a0 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
4d7b0 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c 20  Size(BtCursor*, 
4d7c0 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c  u32 *pSize);.SQL
4d7d0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4d7e0 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
4d7f0 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20  (BtCursor*, u32 
4d800 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c  offset, u32 amt,
4d810 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f   void*);.SQLITE_
4d820 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
4d830 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
4d840 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72  edRowid(BtCursor
4d850 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
4d860 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4d870 45 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  E sqlite3_int64 
4d880 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43  sqlite3BtreeGetC
4d890 61 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72  achedRowid(BtCur
4d8a0 73 6f 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  sor*);..SQLITE_P
4d8b0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
4d8c0 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69  ite3BtreeIntegri
4d8d0 74 79 43 68 65 63 6b 28 42 74 72 65 65 2a 2c 20  tyCheck(Btree*, 
4d8e0 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69 6e 74 20  int *aRoot, int 
4d8f0 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69 6e 74 2a  nRoot, int, int*
4d900 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4d910 45 20 73 74 72 75 63 74 20 50 61 67 65 72 20 2a  E struct Pager *
4d920 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
4d930 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53 51 4c 49  r(Btree*);..SQLI
4d940 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4d950 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 44 61  qlite3BtreePutDa
4d960 74 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33  ta(BtCursor*, u3
4d970 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d  2 offset, u32 am
4d980 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54  t, void*);.SQLIT
4d990 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
4d9a0 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65  qlite3BtreeCache
4d9b0 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73 6f  Overflow(BtCurso
4d9c0 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r *);.SQLITE_PRI
4d9d0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4d9e0 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f  3BtreeClearCurso
4d9f0 72 28 42 74 43 75 72 73 6f 72 20 2a 29 3b 0a 0a  r(BtCursor *);..
4da00 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53  #ifndef NDEBUG.S
4da10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4da20 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
4da30 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74 43 75  rsorIsValid(BtCu
4da40 72 73 6f 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  rsor*);.#endif..
4da50 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
4da60 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 53  MIT_BTREECOUNT.S
4da70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4da80 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
4da90 75 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 2c 20  unt(BtCursor *, 
4daa0 69 36 34 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  i64 *);.#endif..
4dab0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
4dac0 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
4dad0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4dae0 65 65 43 75 72 73 6f 72 49 6e 66 6f 28 42 74 43  eeCursorInfo(BtC
4daf0 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c 20 69 6e  ursor*, int*, in
4db00 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
4db10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
4db20 74 72 65 65 43 75 72 73 6f 72 4c 69 73 74 28 42  treeCursorList(B
4db30 74 72 65 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  tree*);.#endif..
4db40 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20  /*.** If we are 
4db50 6e 6f 74 20 75 73 69 6e 67 20 73 68 61 72 65 64  not using shared
4db60 20 63 61 63 68 65 2c 20 74 68 65 6e 20 74 68 65   cache, then the
4db70 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f  re is no need to
4db80 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78 65 73 20  .** use mutexes 
4db90 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 42 74  to access the Bt
4dba0 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65  Shared structure
4dbb0 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74 68 65 0a  s.  So make the.
4dbc0 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 4c 65 61  ** Enter and Lea
4dbd0 76 65 20 70 72 6f 63 65 64 75 72 65 73 20 6e 6f  ve procedures no
4dbe0 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  -ops..*/.#ifndef
4dbf0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
4dc00 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49 54 45  RED_CACHE.SQLITE
4dc10 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
4dc20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
4dc30 72 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54  r(Btree*);.SQLIT
4dc40 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
4dc50 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
4dc60 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b  erAll(sqlite3*);
4dc70 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
4dc80 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
4dc90 72 28 58 29 20 0a 23 20 64 65 66 69 6e 65 20 73  r(X) .# define s
4dca0 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
4dcb0 41 6c 6c 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23  All(X).#endif..#
4dcc0 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
4dcd0 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
4dce0 41 43 48 45 29 20 26 26 20 53 51 4c 49 54 45 5f  ACHE) && SQLITE_
4dcf0 54 48 52 45 41 44 53 41 46 45 0a 53 51 4c 49 54  THREADSAFE.SQLIT
4dd00 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
4dd10 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
4dd20 76 65 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  ve(Btree*);.SQLI
4dd30 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
4dd40 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
4dd50 74 65 72 43 75 72 73 6f 72 28 42 74 43 75 72 73  terCursor(BtCurs
4dd60 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  or*);.SQLITE_PRI
4dd70 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
4dd80 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72  te3BtreeLeaveCur
4dd90 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a  sor(BtCursor*);.
4dda0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
4ddb0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
4ddc0 65 65 4c 65 61 76 65 41 6c 6c 28 73 71 6c 69 74  eeLeaveAll(sqlit
4ddd0 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e3*);.SQLITE_PRI
4dde0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
4ddf0 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72  te3BtreeMutexArr
4de00 61 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75 74  ayEnter(BtreeMut
4de10 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54  exArray*);.SQLIT
4de20 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
4de30 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
4de40 65 78 41 72 72 61 79 4c 65 61 76 65 28 42 74 72  exArrayLeave(Btr
4de50 65 65 4d 75 74 65 78 41 72 72 61 79 2a 29 3b 0a  eeMutexArray*);.
4de60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
4de70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
4de80 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73 65  eeMutexArrayInse
4de90 72 74 28 42 74 72 65 65 4d 75 74 65 78 41 72 72  rt(BtreeMutexArr
4dea0 61 79 2a 2c 20 42 74 72 65 65 2a 29 3b 0a 23 69  ay*, Btree*);.#i
4deb0 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f  fndef NDEBUG.  /
4dec0 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
4ded0 20 61 72 65 20 75 73 65 64 20 69 6e 73 69 64 65   are used inside
4dee0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
4def0 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 53 51  ents only. */.SQ
4df00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
4df10 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  nt sqlite3BtreeH
4df20 6f 6c 64 73 4d 75 74 65 78 28 42 74 72 65 65 2a  oldsMutex(Btree*
4df30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4df40 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 42  E   int sqlite3B
4df50 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65  treeHoldsAllMute
4df60 78 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23  xes(sqlite3*);.#
4df70 65 6e 64 69 66 0a 23 65 6c 73 65 0a 0a 23 20 64  endif.#else..# d
4df80 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72  efine sqlite3Btr
4df90 65 65 4c 65 61 76 65 28 58 29 0a 23 20 64 65 66  eeLeave(X).# def
4dfa0 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
4dfb0 45 6e 74 65 72 43 75 72 73 6f 72 28 58 29 0a 23  EnterCursor(X).#
4dfc0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
4dfd0 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28  treeLeaveCursor(
4dfe0 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  X).# define sqli
4dff0 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c  te3BtreeLeaveAll
4e000 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  (X).# define sql
4e010 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
4e020 72 61 79 45 6e 74 65 72 28 58 29 0a 23 20 64 65  rayEnter(X).# de
4e030 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65  fine sqlite3Btre
4e040 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65  eMutexArrayLeave
4e050 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  (X).# define sql
4e060 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
4e070 72 61 79 49 6e 73 65 72 74 28 58 2c 59 29 0a 0a  rayInsert(X,Y)..
4e080 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
4e090 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
4e0a0 58 29 20 31 0a 23 20 64 65 66 69 6e 65 20 73 71  X) 1.# define sq
4e0b0 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
4e0c0 6c 6c 4d 75 74 65 78 65 73 28 58 29 20 31 0a 23  llMutexes(X) 1.#
4e0d0 65 6e 64 69 66 0a 0a 0a 23 65 6e 64 69 66 20 2f  endif...#endif /
4e0e0 2a 20 5f 42 54 52 45 45 5f 48 5f 20 2a 2f 0a 0a  * _BTREE_H_ */..
4e0f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
4e100 45 6e 64 20 6f 66 20 62 74 72 65 65 2e 68 20 2a  End of btree.h *
4e110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
4e140 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
4e150 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
4e160 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
4e170 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
4e180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
4e190 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
4e1a0 49 6e 63 6c 75 64 65 20 76 64 62 65 2e 68 20 69  Include vdbe.h i
4e1b0 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
4e1c0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
4e1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
4e1e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
4e1f0 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 2e  Begin file vdbe.
4e200 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4e210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
4e230 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
4e240 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
4e250 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
4e260 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
4e270 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
4e280 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
4e290 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
4e2a0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
4e2b0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
4e2c0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
4e2d0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
4e2e0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
4e2f0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
4e300 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
4e310 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
4e320 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
4e330 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
4e340 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
4e350 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
4e360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e3a0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 61 64 65 72  ******.** Header
4e3b0 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 56 69   file for the Vi
4e3c0 72 74 75 61 6c 20 44 61 74 61 42 61 73 65 20 45  rtual DataBase E
4e3d0 6e 67 69 6e 65 20 28 56 44 42 45 29 0a 2a 2a 0a  ngine (VDBE).**.
4e3e0 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 64  ** This header d
4e3f0 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
4e400 66 61 63 65 20 74 6f 20 74 68 65 20 76 69 72 74  face to the virt
4e410 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67  ual database eng
4e420 69 6e 65 0a 2a 2a 20 6f 72 20 56 44 42 45 2e 20  ine.** or VDBE. 
4e430 20 54 68 65 20 56 44 42 45 20 69 6d 70 6c 65 6d   The VDBE implem
4e440 65 6e 74 73 20 61 6e 20 61 62 73 74 72 61 63 74  ents an abstract
4e450 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 72 75   machine that ru
4e460 6e 73 20 61 0a 2a 2a 20 73 69 6d 70 6c 65 20 70  ns a.** simple p
4e470 72 6f 67 72 61 6d 20 74 6f 20 61 63 63 65 73 73  rogram to access
4e480 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65 20   and modify the 
4e490 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74 61 62  underlying datab
4e4a0 61 73 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ase..*/.#ifndef 
4e4b0 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48 5f 0a  _SQLITE_VDBE_H_.
4e4c0 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f  #define _SQLITE_
4e4d0 56 44 42 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41  VDBE_H_../*.** A
4e4e0 20 73 69 6e 67 6c 65 20 56 44 42 45 20 69 73 20   single VDBE is 
4e4f0 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75 63 74  an opaque struct
4e500 75 72 65 20 6e 61 6d 65 64 20 22 56 64 62 65 22  ure named "Vdbe"
4e510 2e 20 20 4f 6e 6c 79 20 72 6f 75 74 69 6e 65 73  .  Only routines
4e520 0a 2a 2a 20 69 6e 20 74 68 65 20 73 6f 75 72 63  .** in the sourc
4e530 65 20 66 69 6c 65 20 73 71 6c 69 74 65 56 64 62  e file sqliteVdb
4e540 65 2e 63 20 61 72 65 20 61 6c 6c 6f 77 65 64 20  e.c are allowed 
4e550 74 6f 20 73 65 65 20 74 68 65 20 69 6e 73 69 64  to see the insid
4e560 65 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74  es.** of this st
4e570 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65  ructure..*/.type
4e580 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 20  def struct Vdbe 
4e590 56 64 62 65 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  Vdbe;../*.** The
4e5a0 20 6e 61 6d 65 73 20 6f 66 20 74 68 65 20 66 6f   names of the fo
4e5b0 6c 6c 6f 77 69 6e 67 20 74 79 70 65 73 20 64 65  llowing types de
4e5c0 63 6c 61 72 65 64 20 69 6e 20 76 64 62 65 49 6e  clared in vdbeIn
4e5d0 74 2e 68 20 61 72 65 20 72 65 71 75 69 72 65 64  t.h are required
4e5e0 0a 2a 2a 20 66 6f 72 20 74 68 65 20 56 64 62 65  .** for the Vdbe
4e5f0 4f 70 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a  Op definition..*
4e600 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
4e610 20 56 64 62 65 46 75 6e 63 20 56 64 62 65 46 75   VdbeFunc VdbeFu
4e620 6e 63 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  nc;.typedef stru
4e630 63 74 20 4d 65 6d 20 4d 65 6d 3b 0a 74 79 70 65  ct Mem Mem;.type
4e640 64 65 66 20 73 74 72 75 63 74 20 53 75 62 50 72  def struct SubPr
4e650 6f 67 72 61 6d 20 53 75 62 50 72 6f 67 72 61 6d  ogram SubProgram
4e660 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c  ;../*.** A singl
4e670 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66  e instruction of
4e680 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63   the virtual mac
4e690 68 69 6e 65 20 68 61 73 20 61 6e 20 6f 70 63 6f  hine has an opco
4e6a0 64 65 0a 2a 2a 20 61 6e 64 20 61 73 20 6d 61 6e  de.** and as man
4e6b0 79 20 61 73 20 74 68 72 65 65 20 6f 70 65 72 61  y as three opera
4e6c0 6e 64 73 2e 20 20 54 68 65 20 69 6e 73 74 72 75  nds.  The instru
4e6d0 63 74 69 6f 6e 20 69 73 20 72 65 63 6f 72 64 65  ction is recorde
4e6e0 64 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 73 74 61  d.** as an insta
4e6f0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
4e700 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 3a 0a  wing structure:.
4e710 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f 70  */.struct VdbeOp
4e720 20 7b 0a 20 20 75 38 20 6f 70 63 6f 64 65 3b 20   {.  u8 opcode; 
4e730 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74           /* What
4e740 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 70 65   operation to pe
4e750 72 66 6f 72 6d 20 2a 2f 0a 20 20 73 69 67 6e 65  rform */.  signe
4e760 64 20 63 68 61 72 20 70 34 74 79 70 65 3b 20 2f  d char p4type; /
4e770 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 50 34 5f  * One of the P4_
4e780 78 78 78 20 63 6f 6e 73 74 61 6e 74 73 20 66 6f  xxx constants fo
4e790 72 20 70 34 20 2a 2f 0a 20 20 75 38 20 6f 70 66  r p4 */.  u8 opf
4e7a0 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a  lags;         /*
4e7b0 20 4d 61 73 6b 20 6f 66 20 74 68 65 20 4f 50 46   Mask of the OPF
4e7c0 4c 47 5f 2a 20 66 6c 61 67 73 20 69 6e 20 6f 70  LG_* flags in op
4e7d0 63 6f 64 65 73 2e 68 20 2a 2f 0a 20 20 75 38 20  codes.h */.  u8 
4e7e0 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p5;             
4e7f0 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65   /* Fifth parame
4e800 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e  ter is an unsign
4e810 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  ed character */.
4e820 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20    int p1;       
4e830 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f        /* First o
4e840 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
4e850 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p2;             
4e860 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65  /* Second parame
4e870 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a  ter (often the j
4e880 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29  ump destination)
4e890 20 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20   */.  int p3;   
4e8a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
4e8b0 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
4e8c0 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20   */.  union {   
4e8d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 75            /* fou
4e8e0 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f  rth parameter */
4e8f0 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20  .    int i;     
4e900 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
4e910 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20  nteger value if 
4e920 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32  p4type==P4_INT32
4e930 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b   */.    void *p;
4e940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4e950 2a 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65  * Generic pointe
4e960 72 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a  r */.    char *z
4e970 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4e980 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61  /* Pointer to da
4e990 74 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63  ta for string (c
4e9a0 68 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73  har array) types
4e9b0 20 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36   */.    i64 *pI6
4e9c0 34 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  4;             /
4e9d0 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79  * Used when p4ty
4e9e0 70 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a  pe is P4_INT64 *
4e9f0 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52  /.    double *pR
4ea00 65 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  eal;         /* 
4ea10 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65  Used when p4type
4ea20 20 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20   is P4_REAL */. 
4ea30 20 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e     FuncDef *pFun
4ea40 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65  c;        /* Use
4ea50 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
4ea60 20 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20   P4_FUNCDEF */. 
4ea70 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64     VdbeFunc *pVd
4ea80 62 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65  beFunc;   /* Use
4ea90 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
4eaa0 20 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a   P4_VDBEFUNC */.
4eab0 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
4eac0 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73  ll;        /* Us
4ead0 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4eae0 73 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a  s P4_COLLSEQ */.
4eaf0 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20      Mem *pMem;  
4eb00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
4eb10 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69  ed when p4type i
4eb20 73 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20  s P4_MEM */.    
4eb30 56 54 61 62 6c 65 20 2a 70 56 74 61 62 3b 20 20  VTable *pVtab;  
4eb40 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77         /* Used w
4eb50 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34  hen p4type is P4
4eb60 5f 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79  _VTAB */.    Key
4eb70 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
4eb80 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
4eb90 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45   p4type is P4_KE
4eba0 59 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74  YINFO */.    int
4ebb0 20 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20   *ai;           
4ebc0 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e      /* Used when
4ebd0 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e   p4type is P4_IN
4ebe0 54 41 52 52 41 59 20 2a 2f 0a 20 20 20 20 53 75  TARRAY */.    Su
4ebf0 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67 72  bProgram *pProgr
4ec00 61 6d 3b 20 20 2f 2a 20 55 73 65 64 20 77 68 65  am;  /* Used whe
4ec10 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 53  n p4type is P4_S
4ec20 55 42 50 52 4f 47 52 41 4d 20 2a 2f 0a 20 20 7d  UBPROGRAM */.  }
4ec30 20 70 34 3b 0a 23 69 66 64 65 66 20 53 51 4c 49   p4;.#ifdef SQLI
4ec40 54 45 5f 44 45 42 55 47 0a 20 20 63 68 61 72 20  TE_DEBUG.  char 
4ec50 2a 7a 43 6f 6d 6d 65 6e 74 3b 20 20 20 20 20 20  *zComment;      
4ec60 20 20 20 20 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74      /* Comment t
4ec70 6f 20 69 6d 70 72 6f 76 65 20 72 65 61 64 61 62  o improve readab
4ec80 69 6c 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 0a  ility */.#endif.
4ec90 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
4eca0 49 4c 45 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20  ILE.  int cnt;  
4ecb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4ecc0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  * Number of time
4ecd0 73 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  s this instructi
4ece0 6f 6e 20 77 61 73 20 65 78 65 63 75 74 65 64 20  on was executed 
4ecf0 2a 2f 0a 20 20 75 36 34 20 63 79 63 6c 65 73 3b  */.  u64 cycles;
4ed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
4ed10 20 54 6f 74 61 6c 20 74 69 6d 65 20 73 70 65 6e   Total time spen
4ed20 74 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73  t executing this
4ed30 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a   instruction */.
4ed40 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65  #endif.};.typede
4ed50 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 20  f struct VdbeOp 
4ed60 56 64 62 65 4f 70 3b 0a 0a 0a 2f 2a 0a 2a 2a 20  VdbeOp;.../*.** 
4ed70 41 20 73 75 62 2d 72 6f 75 74 69 6e 65 20 75 73  A sub-routine us
4ed80 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
4ed90 61 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  a trigger progra
4eda0 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 75 62  m..*/.struct Sub
4edb0 50 72 6f 67 72 61 6d 20 7b 0a 20 20 56 64 62 65  Program {.  Vdbe
4edc0 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20  Op *aOp;        
4edd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72            /* Arr
4ede0 61 79 20 6f 66 20 6f 70 63 6f 64 65 73 20 66 6f  ay of opcodes fo
4edf0 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f  r sub-program */
4ee00 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20  .  int nOp;     
4ee10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ee20 20 2f 2a 20 45 6c 65 6d 65 6e 74 73 20 69 6e 20   /* Elements in 
4ee30 61 4f 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  aOp[] */.  int n
4ee40 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
4ee50 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
4ee60 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c  er of memory cel
4ee70 6c 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  ls required */. 
4ee80 20 69 6e 74 20 6e 43 73 72 3b 20 20 20 20 20 20   int nCsr;      
4ee90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4eea0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73  * Number of curs
4eeb0 6f 72 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a  ors required */.
4eec0 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
4eed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4eee0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69  /* Number of poi
4eef0 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74  nters to this st
4ef00 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 76 6f 69  ructure */.  voi
4ef10 64 20 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20  d *token;       
4ef20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 64             /* id
4ef30 20 74 68 61 74 20 6d 61 79 20 62 65 20 75 73 65   that may be use
4ef40 64 20 74 6f 20 72 65 63 75 72 73 69 76 65 20 74  d to recursive t
4ef50 72 69 67 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f  riggers */.};../
4ef60 2a 0a 2a 2a 20 41 20 73 6d 61 6c 6c 65 72 20 76  *.** A smaller v
4ef70 65 72 73 69 6f 6e 20 6f 66 20 56 64 62 65 4f 70  ersion of VdbeOp
4ef80 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 56 64   used for the Vd
4ef90 62 65 41 64 64 4f 70 4c 69 73 74 28 29 20 66 75  beAddOpList() fu
4efa0 6e 63 74 69 6f 6e 20 62 65 63 61 75 73 65 0a 2a  nction because.*
4efb0 2a 20 69 74 20 74 61 6b 65 73 20 75 70 20 6c 65  * it takes up le
4efc0 73 73 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 72  ss space..*/.str
4efd0 75 63 74 20 56 64 62 65 4f 70 4c 69 73 74 20 7b  uct VdbeOpList {
4efe0 0a 20 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20  .  u8 opcode;   
4eff0 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f         /* What o
4f000 70 65 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66  peration to perf
4f010 6f 72 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20  orm */.  signed 
4f020 63 68 61 72 20 70 31 3b 20 20 20 20 20 2f 2a 20  char p1;     /* 
4f030 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a 2f  First operand */
4f040 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 70  .  signed char p
4f050 32 3b 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64  2;     /* Second
4f060 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74 65   parameter (ofte
4f070 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74 69  n the jump desti
4f080 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 73 69 67  nation) */.  sig
4f090 6e 65 64 20 63 68 61 72 20 70 33 3b 20 20 20 20  ned char p3;    
4f0a0 20 2f 2a 20 54 68 69 72 64 20 70 61 72 61 6d 65   /* Third parame
4f0b0 74 65 72 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65  ter */.};.typede
4f0c0 66 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 4c  f struct VdbeOpL
4f0d0 69 73 74 20 56 64 62 65 4f 70 4c 69 73 74 3b 0a  ist VdbeOpList;.
4f0e0 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
4f0f0 61 6c 75 65 73 20 6f 66 20 56 64 62 65 4f 70 2e  alues of VdbeOp.
4f100 70 34 74 79 70 65 0a 2a 2f 0a 23 64 65 66 69 6e  p4type.*/.#defin
4f110 65 20 50 34 5f 4e 4f 54 55 53 45 44 20 20 20 20  e P4_NOTUSED    
4f120 30 20 20 20 2f 2a 20 54 68 65 20 50 34 20 70 61  0   /* The P4 pa
4f130 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 75  rameter is not u
4f140 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  sed */.#define P
4f150 34 5f 44 59 4e 41 4d 49 43 20 20 28 2d 31 29 20  4_DYNAMIC  (-1) 
4f160 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
4f170 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64   string obtained
4f180 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c   from sqliteMall
4f190 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  oc() */.#define 
4f1a0 50 34 5f 53 54 41 54 49 43 20 20 20 28 2d 32 29  P4_STATIC   (-2)
4f1b0 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
4f1c0 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67 20  a static string 
4f1d0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 43 4f  */.#define P4_CO
4f1e0 4c 4c 53 45 51 20 20 28 2d 34 29 20 20 2f 2a 20  LLSEQ  (-4)  /* 
4f1f0 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
4f200 74 6f 20 61 20 43 6f 6c 6c 53 65 71 20 73 74 72  to a CollSeq str
4f210 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e  ucture */.#defin
4f220 65 20 50 34 5f 46 55 4e 43 44 45 46 20 20 28 2d  e P4_FUNCDEF  (-
4f230 35 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70  5)  /* P4 is a p
4f240 6f 69 6e 74 65 72 20 74 6f 20 61 20 46 75 6e 63  ointer to a Func
4f250 44 65 66 20 73 74 72 75 63 74 75 72 65 20 2a 2f  Def structure */
4f260 0a 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49  .#define P4_KEYI
4f270 4e 46 4f 20 20 28 2d 36 29 20 20 2f 2a 20 50 34  NFO  (-6)  /* P4
4f280 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
4f290 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63   a KeyInfo struc
4f2a0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
4f2b0 50 34 5f 56 44 42 45 46 55 4e 43 20 28 2d 37 29  P4_VDBEFUNC (-7)
4f2c0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
4f2d0 6e 74 65 72 20 74 6f 20 61 20 56 64 62 65 46 75  nter to a VdbeFu
4f2e0 6e 63 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  nc structure */.
4f2f0 23 64 65 66 69 6e 65 20 50 34 5f 4d 45 4d 20 20  #define P4_MEM  
4f300 20 20 20 20 28 2d 38 29 20 20 2f 2a 20 50 34 20      (-8)  /* P4 
4f310 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
4f320 61 20 4d 65 6d 2a 20 20 20 20 73 74 72 75 63 74  a Mem*    struct
4f330 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
4f340 34 5f 54 52 41 4e 53 49 45 4e 54 20 28 2d 39 29  4_TRANSIENT (-9)
4f350 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
4f360 74 65 72 20 74 6f 20 61 20 74 72 61 6e 73 69 65  ter to a transie
4f370 6e 74 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65  nt string */.#de
4f380 66 69 6e 65 20 50 34 5f 56 54 41 42 20 20 20 20  fine P4_VTAB    
4f390 20 28 2d 31 30 29 20 2f 2a 20 50 34 20 69 73 20   (-10) /* P4 is 
4f3a0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
4f3b0 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72  sqlite3_vtab str
4f3c0 75 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e  ucture */.#defin
4f3d0 65 20 50 34 5f 4d 50 52 49 4e 54 46 20 20 28 2d  e P4_MPRINTF  (-
4f3e0 31 31 29 20 2f 2a 20 50 34 20 69 73 20 61 20 73  11) /* P4 is a s
4f3f0 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66  tring obtained f
4f400 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  rom sqlite3_mpri
4f410 6e 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65  ntf() */.#define
4f420 20 50 34 5f 52 45 41 4c 20 20 20 20 20 28 2d 31   P4_REAL     (-1
4f430 32 29 20 2f 2a 20 50 34 20 69 73 20 61 20 36 34  2) /* P4 is a 64
4f440 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f  -bit floating po
4f450 69 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 23 64 65  int value */.#de
4f460 66 69 6e 65 20 50 34 5f 49 4e 54 36 34 20 20 20  fine P4_INT64   
4f470 20 28 2d 31 33 29 20 2f 2a 20 50 34 20 69 73 20   (-13) /* P4 is 
4f480 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  a 64-bit signed 
4f490 69 6e 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69  integer */.#defi
4f4a0 6e 65 20 50 34 5f 49 4e 54 33 32 20 20 20 20 28  ne P4_INT32    (
4f4b0 2d 31 34 29 20 2f 2a 20 50 34 20 69 73 20 61 20  -14) /* P4 is a 
4f4c0 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  32-bit signed in
4f4d0 74 65 67 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65  teger */.#define
4f4e0 20 50 34 5f 49 4e 54 41 52 52 41 59 20 28 2d 31   P4_INTARRAY (-1
4f4f0 35 29 20 2f 2a 20 50 34 20 69 73 20 61 20 76 65  5) /* P4 is a ve
4f500 63 74 6f 72 20 6f 66 20 33 32 2d 62 69 74 20 69  ctor of 32-bit i
4f510 6e 74 65 67 65 72 73 20 2a 2f 0a 23 64 65 66 69  ntegers */.#defi
4f520 6e 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d  ne P4_SUBPROGRAM
4f530 20 20 28 2d 31 38 29 20 2f 2a 20 50 34 20 69 73    (-18) /* P4 is
4f540 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
4f550 53 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63  SubProgram struc
4f560 74 75 72 65 20 2a 2f 0a 0a 2f 2a 20 57 68 65 6e  ture */../* When
4f570 20 61 64 64 69 6e 67 20 61 20 50 34 20 61 72 67   adding a P4 arg
4f580 75 6d 65 6e 74 20 75 73 69 6e 67 20 50 34 5f 4b  ument using P4_K
4f590 45 59 49 4e 46 4f 2c 20 61 20 63 6f 70 79 20 6f  EYINFO, a copy o
4f5a0 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  f the KeyInfo st
4f5b0 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6d 61  ructure.** is ma
4f5c0 64 65 2e 20 20 54 68 61 74 20 63 6f 70 79 20 69  de.  That copy i
4f5d0 73 20 66 72 65 65 64 20 77 68 65 6e 20 74 68 65  s freed when the
4f5e0 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69 7a   Vdbe is finaliz
4f5f0 65 64 2e 20 20 42 75 74 20 69 66 20 74 68 65 0a  ed.  But if the.
4f600 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 50  ** argument is P
4f610 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
4f620 46 2c 20 74 68 65 20 70 61 73 73 65 64 20 69 6e  F, the passed in
4f630 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64   pointer is used
4f640 2e 20 20 49 74 20 73 74 69 6c 6c 0a 2a 2a 20 67  .  It still.** g
4f650 65 74 73 20 66 72 65 65 64 20 77 68 65 6e 20 74  ets freed when t
4f660 68 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c  he Vdbe is final
4f670 69 7a 65 64 20 73 6f 20 69 74 20 73 74 69 6c 6c  ized so it still
4f680 20 73 68 6f 75 6c 64 20 62 65 20 6f 62 74 61 69   should be obtai
4f690 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 61 20 73 69  ned.** from a si
4f6a0 6e 67 6c 65 20 73 71 6c 69 74 65 4d 61 6c 6c 6f  ngle sqliteMallo
4f6b0 63 28 29 2e 20 20 42 75 74 20 6e 6f 20 63 6f 70  c().  But no cop
4f6c0 79 20 69 73 20 6d 61 64 65 20 61 6e 64 20 74 68  y is made and th
4f6d0 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e  e calling.** fun
4f6e0 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 2a 6e 6f  ction should *no
4f6f0 74 2a 20 74 72 79 20 74 6f 20 66 72 65 65 20 74  t* try to free t
4f700 68 65 20 4b 65 79 49 6e 66 6f 2e 0a 2a 2f 0a 23  he KeyInfo..*/.#
4f710 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e 46  define P4_KEYINF
4f720 4f 5f 48 41 4e 44 4f 46 46 20 28 2d 31 36 29 0a  O_HANDOFF (-16).
4f730 23 64 65 66 69 6e 65 20 50 34 5f 4b 45 59 49 4e  #define P4_KEYIN
4f740 46 4f 5f 53 54 41 54 49 43 20 20 28 2d 31 37 29  FO_STATIC  (-17)
4f750 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 56 64 62 65  ../*.** The Vdbe
4f760 2e 61 43 6f 6c 4e 61 6d 65 20 61 72 72 61 79 20  .aColName array 
4f770 63 6f 6e 74 61 69 6e 73 20 35 6e 20 4d 65 6d 20  contains 5n Mem 
4f780 73 74 72 75 63 74 75 72 65 73 2c 20 77 68 65 72  structures, wher
4f790 65 20 6e 20 69 73 20 74 68 65 20 0a 2a 2a 20 6e  e n is the .** n
4f7a0 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
4f7b0 20 6f 66 20 64 61 74 61 20 72 65 74 75 72 6e 65   of data returne
4f7c0 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65  d by the stateme
4f7d0 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43  nt..*/.#define C
4f7e0 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 20 20 20 20 20  OLNAME_NAME     
4f7f0 30 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d  0.#define COLNAM
4f800 45 5f 44 45 43 4c 54 59 50 45 20 31 0a 23 64 65  E_DECLTYPE 1.#de
4f810 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 41 54  fine COLNAME_DAT
4f820 41 42 41 53 45 20 32 0a 23 64 65 66 69 6e 65 20  ABASE 2.#define 
4f830 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 20 20 20  COLNAME_TABLE   
4f840 20 33 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41   3.#define COLNA
4f850 4d 45 5f 43 4f 4c 55 4d 4e 20 20 20 34 0a 23 69  ME_COLUMN   4.#i
4f860 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
4f870 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
4f880 54 41 0a 23 20 64 65 66 69 6e 65 20 43 4f 4c 4e  TA.# define COLN
4f890 41 4d 45 5f 4e 20 20 20 20 20 20 20 20 35 20 20  AME_N        5  
4f8a0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
4f8b0 20 43 4f 4c 4e 41 4d 45 5f 78 78 78 20 73 79 6d   COLNAME_xxx sym
4f8c0 62 6f 6c 73 20 2a 2f 0a 23 65 6c 73 65 0a 23 20  bols */.#else.# 
4f8d0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
4f8e0 54 5f 44 45 43 4c 54 59 50 45 0a 23 20 20 20 64  T_DECLTYPE.#   d
4f8f0 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20  efine COLNAME_N 
4f900 20 20 20 20 20 31 20 20 20 20 20 20 2f 2a 20 53       1      /* S
4f910 74 6f 72 65 20 6f 6e 6c 79 20 74 68 65 20 6e 61  tore only the na
4f920 6d 65 20 2a 2f 0a 23 20 65 6c 73 65 0a 23 20 20  me */.# else.#  
4f930 20 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f   define COLNAME_
4f940 4e 20 20 20 20 20 20 32 20 20 20 20 20 20 2f 2a  N      2      /*
4f950 20 53 74 6f 72 65 20 74 68 65 20 6e 61 6d 65 20   Store the name 
4f960 61 6e 64 20 64 65 63 6c 74 79 70 65 20 2a 2f 0a  and decltype */.
4f970 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a  # endif.#endif..
4f980 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
4f990 69 6e 67 20 6d 61 63 72 6f 20 63 6f 6e 76 65 72  ing macro conver
4f9a0 74 73 20 61 20 72 65 6c 61 74 69 76 65 20 61 64  ts a relative ad
4f9b0 64 72 65 73 73 20 69 6e 20 74 68 65 20 70 32 20  dress in the p2 
4f9c0 66 69 65 6c 64 0a 2a 2a 20 6f 66 20 61 20 56 64  field.** of a Vd
4f9d0 62 65 4f 70 20 73 74 72 75 63 74 75 72 65 20 69  beOp structure i
4f9e0 6e 74 6f 20 61 20 6e 65 67 61 74 69 76 65 20 6e  nto a negative n
4f9f0 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 0a 2a  umber so that .*
4fa00 2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  * sqlite3VdbeAdd
4fa10 4f 70 4c 69 73 74 28 29 20 6b 6e 6f 77 73 20 74  OpList() knows t
4fa20 68 61 74 20 74 68 65 20 61 64 64 72 65 73 73 20  hat the address 
4fa30 69 73 20 72 65 6c 61 74 69 76 65 2e 20 20 43 61  is relative.  Ca
4fa40 6c 6c 69 6e 67 0a 2a 2a 20 74 68 65 20 6d 61 63  lling.** the mac
4fa50 72 6f 20 61 67 61 69 6e 20 72 65 73 74 6f 72 65  ro again restore
4fa60 73 20 74 68 65 20 61 64 64 72 65 73 73 2e 0a 2a  s the address..*
4fa70 2f 0a 23 64 65 66 69 6e 65 20 41 44 44 52 28 58  /.#define ADDR(X
4fa80 29 20 20 28 2d 31 2d 28 58 29 29 0a 0a 2f 2a 0a  )  (-1-(X))../*.
4fa90 2a 2a 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20  ** The makefile 
4faa0 73 63 61 6e 73 20 74 68 65 20 76 64 62 65 2e 63  scans the vdbe.c
4fab0 20 73 6f 75 72 63 65 20 66 69 6c 65 20 61 6e 64   source file and
4fac0 20 63 72 65 61 74 65 73 20 74 68 65 20 22 6f 70   creates the "op
4fad0 63 6f 64 65 73 2e 68 22 0a 2a 2a 20 68 65 61 64  codes.h".** head
4fae0 65 72 20 66 69 6c 65 20 74 68 61 74 20 64 65 66  er file that def
4faf0 69 6e 65 73 20 61 20 6e 75 6d 62 65 72 20 66 6f  ines a number fo
4fb00 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 75 73  r each opcode us
4fb10 65 64 20 62 79 20 74 68 65 20 56 44 42 45 2e 0a  ed by the VDBE..
4fb20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4fb30 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 70 63 6f 64  ** Include opcod
4fb40 65 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  es.h in the midd
4fb50 6c 65 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a  le of vdbe.h ***
4fb60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4fb70 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4fb80 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 70  ** Begin file op
4fb90 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  codes.h ********
4fba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4fbb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4fbc0 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61 74 69 63 61  */./* Automatica
4fbd0 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 2e 20 20  lly generated.  
4fbe0 44 6f 20 6e 6f 74 20 65 64 69 74 20 2a 2f 0a 2f  Do not edit */./
4fbf0 2a 20 53 65 65 20 74 68 65 20 6d 6b 6f 70 63 6f  * See the mkopco
4fc00 64 65 68 2e 61 77 6b 20 73 63 72 69 70 74 20 66  deh.awk script f
4fc10 6f 72 20 64 65 74 61 69 6c 73 20 2a 2f 0a 23 64  or details */.#d
4fc20 65 66 69 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20  efine OP_Goto   
4fc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a                1.
4fc50 23 64 65 66 69 6e 65 20 4f 50 5f 47 6f 73 75 62  #define OP_Gosub
4fc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc80 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 74  2.#define OP_Ret
4fc90 75 72 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  urn             
4fca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcb0 20 20 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59    3.#define OP_Y
4fcc0 69 65 6c 64 20 20 20 20 20 20 20 20 20 20 20 20  ield            
4fcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fce0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 4f 50      4.#define OP
4fcf0 5f 48 61 6c 74 49 66 4e 75 6c 6c 20 20 20 20 20  _HaltIfNull     
4fd00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd10 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20        5.#define 
4fd20 4f 50 5f 48 61 6c 74 20 20 20 20 20 20 20 20 20  OP_Halt         
4fd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd40 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e          6.#defin
4fd50 65 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20 20  e OP_Integer    
4fd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd70 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66            7.#def
4fd80 69 6e 65 20 4f 50 5f 49 6e 74 36 34 20 20 20 20  ine OP_Int64    
4fd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fda0 20 20 20 20 20 20 20 20 20 20 20 20 38 0a 23 64              8.#d
4fdb0 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 20 20 20  efine OP_Real   
4fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fdd0 20 20 20 20 20 20 20 20 20 20 20 20 31 33 30 20              130 
4fde0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4fdf0 46 4c 4f 41 54 20 20 20 20 2a 2f 0a 23 64 65 66  FLOAT    */.#def
4fe00 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67 38 20 20  ine OP_String8  
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 39 34 20 20 20             94   
4fe30 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54  /* same as TK_ST
4fe40 52 49 4e 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e  RING   */.#defin
4fe50 65 20 4f 50 5f 53 74 72 69 6e 67 20 20 20 20 20  e OP_String     
4fe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe70 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66            9.#def
4fe80 69 6e 65 20 4f 50 5f 4e 75 6c 6c 20 20 20 20 20  ine OP_Null     
4fe90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fea0 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64             10.#d
4feb0 65 66 69 6e 65 20 4f 50 5f 42 6c 6f 62 20 20 20  efine OP_Blob   
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 31 31 0a               11.
4fee0 23 64 65 66 69 6e 65 20 4f 50 5f 56 61 72 69 61  #define OP_Varia
4fef0 62 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ble             
4ff00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4ff10 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76  2.#define OP_Mov
4ff20 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
4ff30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff40 20 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43   13.#define OP_C
4ff50 6f 70 79 20 20 20 20 20 20 20 20 20 20 20 20 20  opy             
4ff60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff70 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 4f 50     14.#define OP
4ff80 5f 53 43 6f 70 79 20 20 20 20 20 20 20 20 20 20  _SCopy          
4ff90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffa0 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65 20       15.#define 
4ffb0 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20 20 20 20  OP_ResultRow    
4ffc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffd0 20 20 20 20 20 20 20 31 36 0a 23 64 65 66 69 6e         16.#defin
4ffe0 65 20 4f 50 5f 43 6f 6e 63 61 74 20 20 20 20 20  e OP_Concat     
4fff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50000 20 20 20 20 20 20 20 20 20 39 31 20 20 20 2f 2a           91   /*
50010 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43   same as TK_CONC
50020 41 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  AT   */.#define 
50030 4f 50 5f 41 64 64 20 20 20 20 20 20 20 20 20 20  OP_Add          
50040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50050 20 20 20 20 20 20 20 38 36 20 20 20 2f 2a 20 73         86   /* s
50060 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 20 20  ame as TK_PLUS  
50070 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
50080 5f 53 75 62 74 72 61 63 74 20 20 20 20 20 20 20  _Subtract       
50090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500a0 20 20 20 20 20 38 37 20 20 20 2f 2a 20 73 61 6d       87   /* sam
500b0 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20  e as TK_MINUS   
500c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d   */.#define OP_M
500d0 75 6c 74 69 70 6c 79 20 20 20 20 20 20 20 20 20  ultiply         
500e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500f0 20 20 20 38 38 20 20 20 2f 2a 20 73 61 6d 65 20     88   /* same 
50100 61 73 20 54 4b 5f 53 54 41 52 20 20 20 20 20 2a  as TK_STAR     *
50110 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 69 76  /.#define OP_Div
50120 69 64 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ide             
50130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50140 20 38 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   89   /* same as
50150 20 54 4b 5f 53 4c 41 53 48 20 20 20 20 2a 2f 0a   TK_SLASH    */.
50160 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69  #define OP_Remai
50170 6e 64 65 72 20 20 20 20 20 20 20 20 20 20 20 20  nder            
50180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
50190 30 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  0   /* same as T
501a0 4b 5f 52 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64  K_REM      */.#d
501b0 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 6c 53 65 71  efine OP_CollSeq
501c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 37 0a               17.
501e0 23 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e 63 74  #define OP_Funct
501f0 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
50200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
50210 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74  8.#define OP_Bit
50220 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20  And             
50230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50240 20 38 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73   82   /* same as
50250 20 54 4b 5f 42 49 54 41 4e 44 20 20 20 2a 2f 0a   TK_BITAND   */.
50260 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4f 72  #define OP_BitOr
50270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
50290 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  3   /* same as T
502a0 4b 5f 42 49 54 4f 52 20 20 20 20 2a 2f 0a 23 64  K_BITOR    */.#d
502b0 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65  efine OP_ShiftLe
502c0 66 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ft              
502d0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 34 20               84 
502e0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
502f0 4c 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66  LSHIFT   */.#def
50300 69 6e 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68  ine OP_ShiftRigh
50310 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
50320 20 20 20 20 20 20 20 20 20 20 20 38 35 20 20 20             85   
50330 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53  /* same as TK_RS
50340 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e  HIFT   */.#defin
50350 65 20 4f 50 5f 41 64 64 49 6d 6d 20 20 20 20 20  e OP_AddImm     
50360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50370 20 20 20 20 20 20 20 20 20 32 30 0a 23 64 65 66           20.#def
50380 69 6e 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74  ine OP_MustBeInt
50390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503a0 20 20 20 20 20 20 20 20 20 20 20 32 31 0a 23 64             21.#d
503b0 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41 66 66  efine OP_RealAff
503c0 69 6e 69 74 79 20 20 20 20 20 20 20 20 20 20 20  inity           
503d0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 32 0a               22.
503e0 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 54 65 78  #define OP_ToTex
503f0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
50400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
50410 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  1   /* same as T
50420 4b 5f 54 4f 5f 54 45 58 54 20 20 2a 2f 0a 23 64  K_TO_TEXT  */.#d
50430 65 66 69 6e 65 20 4f 50 5f 54 6f 42 6c 6f 62 20  efine OP_ToBlob 
50440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50450 20 20 20 20 20 20 20 20 20 20 20 20 31 34 32 20              142 
50460 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
50470 54 4f 5f 42 4c 4f 42 20 20 2a 2f 0a 23 64 65 66  TO_BLOB  */.#def
50480 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63  ine OP_ToNumeric
50490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504a0 20 20 20 20 20 20 20 20 20 20 31 34 33 20 20 20            143   
504b0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f  /* same as TK_TO
504c0 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66 69  _NUMERIC*/.#defi
504d0 6e 65 20 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20  ne OP_ToInt     
504e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504f0 20 20 20 20 20 20 20 20 20 31 34 34 20 20 20 2f           144   /
50500 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
50510 49 4e 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  INT   */.#define
50520 20 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 20 20   OP_ToReal      
50530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50540 20 20 20 20 20 20 20 31 34 35 20 20 20 2f 2a 20         145   /* 
50550 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 52 45  same as TK_TO_RE
50560 41 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  AL  */.#define O
50570 50 5f 45 71 20 20 20 20 20 20 20 20 20 20 20 20  P_Eq            
50580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50590 20 20 20 20 20 20 37 36 20 20 20 2f 2a 20 73 61        76   /* sa
505a0 6d 65 20 61 73 20 54 4b 5f 45 51 20 20 20 20 20  me as TK_EQ     
505b0 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
505c0 4e 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  Ne              
505d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505e0 20 20 20 20 37 35 20 20 20 2f 2a 20 73 61 6d 65      75   /* same
505f0 20 61 73 20 54 4b 5f 4e 45 20 20 20 20 20 20 20   as TK_NE       
50600 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 74  */.#define OP_Lt
50610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50630 20 20 37 39 20 20 20 2f 2a 20 73 61 6d 65 20 61    79   /* same a
50640 73 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 2a 2f  s TK_LT       */
50650 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20  .#define OP_Le  
50660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50680 37 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  78   /* same as 
50690 54 4b 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0a 23  TK_LE       */.#
506a0 64 65 66 69 6e 65 20 4f 50 5f 47 74 20 20 20 20  define OP_Gt    
506b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 37                77
506d0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
506e0 5f 47 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65  _GT       */.#de
506f0 66 69 6e 65 20 4f 50 5f 47 65 20 20 20 20 20 20  fine OP_Ge      
50700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50710 20 20 20 20 20 20 20 20 20 20 20 20 38 30 20 20              80  
50720 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47   /* same as TK_G
50730 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  E       */.#defi
50740 6e 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f  ne OP_Permutatio
50750 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
50760 20 20 20 20 20 20 20 20 20 20 32 33 0a 23 64 65            23.#de
50770 66 69 6e 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20  fine OP_Compare 
50780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50790 20 20 20 20 20 20 20 20 20 20 20 20 32 34 0a 23              24.#
507a0 64 65 66 69 6e 65 20 4f 50 5f 4a 75 6d 70 20 20  define OP_Jump  
507b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 35                25
507d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20  .#define OP_And 
507e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50800 36 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  69   /* same as 
50810 54 4b 5f 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23  TK_AND      */.#
50820 64 65 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 20  define OP_Or    
50830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 38                68
50850 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50860 5f 4f 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 65  _OR       */.#de
50870 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20 20 20  fine OP_Not     
50880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50890 20 20 20 20 20 20 20 20 20 20 20 20 31 39 20 20              19  
508a0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
508b0 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  OT      */.#defi
508c0 6e 65 20 4f 50 5f 42 69 74 4e 6f 74 20 20 20 20  ne OP_BitNot    
508d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508e0 20 20 20 20 20 20 20 20 20 20 39 33 20 20 20 2f            93   /
508f0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
50900 4e 4f 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  NOT   */.#define
50910 20 4f 50 5f 49 66 20 20 20 20 20 20 20 20 20 20   OP_If          
50920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50930 20 20 20 20 20 20 20 20 32 36 0a 23 64 65 66 69          26.#defi
50940 6e 65 20 4f 50 5f 49 66 4e 6f 74 20 20 20 20 20  ne OP_IfNot     
50950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50960 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 65            27.#de
50970 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20  fine OP_IsNull  
50980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50990 20 20 20 20 20 20 20 20 20 20 20 20 37 33 20 20              73  
509a0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49   /* same as TK_I
509b0 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69  SNULL   */.#defi
509c0 6e 65 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 20 20  ne OP_NotNull   
509d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509e0 20 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f            74   /
509f0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54  * same as TK_NOT
50a00 4e 55 4c 4c 20 20 2a 2f 0a 23 64 65 66 69 6e 65  NULL  */.#define
50a10 20 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20 20 20 20   OP_Column      
50a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a30 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69          28.#defi
50a40 6e 65 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 20  ne OP_Affinity  
50a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a60 20 20 20 20 20 20 20 20 20 20 32 39 0a 23 64 65            29.#de
50a70 66 69 6e 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  fine OP_MakeReco
50a80 72 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rd              
50a90 20 20 20 20 20 20 20 20 20 20 20 20 33 30 0a 23              30.#
50aa0 64 65 66 69 6e 65 20 4f 50 5f 43 6f 75 6e 74 20  define OP_Count 
50ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31                31
50ad0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 61 76 65  .#define OP_Save
50ae0 70 6f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20  point           
50af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b00 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 75  32.#define OP_Au
50b10 74 6f 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20  toCommit        
50b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b30 20 20 33 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    33.#define OP_
50b40 54 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 20 20  Transaction     
50b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b60 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65 20 4f      34.#define O
50b70 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20  P_ReadCookie    
50b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b90 20 20 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65        35.#define
50ba0 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 20 20 20   OP_SetCookie   
50bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50bc0 20 20 20 20 20 20 20 20 33 36 0a 23 64 65 66 69          36.#defi
50bd0 6e 65 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b  ne OP_VerifyCook
50be0 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ie              
50bf0 20 20 20 20 20 20 20 20 20 20 33 37 0a 23 64 65            37.#de
50c00 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 52 65 61 64  fine OP_OpenRead
50c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c20 20 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 23              38.#
50c30 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 57 72  define OP_OpenWr
50c40 69 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ite             
50c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 39                39
50c60 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e  .#define OP_Open
50c70 45 70 68 65 6d 65 72 61 6c 20 20 20 20 20 20 20  Ephemeral       
50c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c90 34 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70  40.#define OP_Op
50ca0 65 6e 50 73 65 75 64 6f 20 20 20 20 20 20 20 20  enPseudo        
50cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cc0 20 20 34 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f    41.#define OP_
50cd0 43 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20  Close           
50ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cf0 20 20 20 20 34 32 0a 23 64 65 66 69 6e 65 20 4f      42.#define O
50d00 50 5f 53 65 65 6b 4c 74 20 20 20 20 20 20 20 20  P_SeekLt        
50d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d20 20 20 20 20 20 20 34 33 0a 23 64 65 66 69 6e 65        43.#define
50d30 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20 20   OP_SeekLe      
50d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d50 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 69          44.#defi
50d60 6e 65 20 4f 50 5f 53 65 65 6b 47 65 20 20 20 20  ne OP_SeekGe    
50d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d80 20 20 20 20 20 20 20 20 20 20 34 35 0a 23 64 65            45.#de
50d90 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 74 20 20  fine OP_SeekGt  
50da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50db0 20 20 20 20 20 20 20 20 20 20 20 20 34 36 0a 23              46.#
50dc0 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 20 20  define OP_Seek  
50dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 37                47
50df0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 46  .#define OP_NotF
50e00 6f 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20  ound            
50e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e20 34 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f  48.#define OP_Fo
50e30 75 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20  und             
50e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e50 20 20 34 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f    49.#define OP_
50e60 49 73 55 6e 69 71 75 65 20 20 20 20 20 20 20 20  IsUnique        
50e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e80 20 20 20 20 35 30 0a 23 64 65 66 69 6e 65 20 4f      50.#define O
50e90 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20 20  P_NotExists     
50ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50eb0 20 20 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65        51.#define
50ec0 20 4f 50 5f 53 65 71 75 65 6e 63 65 20 20 20 20   OP_Sequence    
50ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ee0 20 20 20 20 20 20 20 20 35 32 0a 23 64 65 66 69          52.#defi
50ef0 6e 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20 20  ne OP_NewRowid  
50f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f10 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 64 65            53.#de
50f20 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74 20 20  fine OP_Insert  
50f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f40 20 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 23              54.#
50f50 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74  define OP_Insert
50f60 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  Int             
50f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35                55
50f80 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 6c 65  .#define OP_Dele
50f90 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  te              
50fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fb0 35 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65  56.#define OP_Re
50fc0 73 65 74 43 6f 75 6e 74 20 20 20 20 20 20 20 20  setCount        
50fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fe0 20 20 35 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f    57.#define OP_
50ff0 52 6f 77 4b 65 79 20 20 20 20 20 20 20 20 20 20  RowKey          
51000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51010 20 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 4f      58.#define O
51020 50 5f 52 6f 77 44 61 74 61 20 20 20 20 20 20 20  P_RowData       
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51040 20 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65        59.#define
51050 20 4f 50 5f 52 6f 77 69 64 20 20 20 20 20 20 20   OP_Rowid       
51060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51070 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 69          60.#defi
51080 6e 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20 20  ne OP_NullRow   
51090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510a0 20 20 20 20 20 20 20 20 20 20 36 31 0a 23 64 65            61.#de
510b0 66 69 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20  fine OP_Last    
510c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
510d0 20 20 20 20 20 20 20 20 20 20 20 20 36 32 0a 23              62.#
510e0 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72 74 20 20  define OP_Sort  
510f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 33                63
51110 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 77 69  .#define OP_Rewi
51120 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nd              
51130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51140 36 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 72  64.#define OP_Pr
51150 65 76 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ev              
51160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51170 20 20 36 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f    65.#define OP_
51180 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20  Next            
51190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511a0 20 20 20 20 36 36 0a 23 64 65 66 69 6e 65 20 4f      66.#define O
511b0 50 5f 49 64 78 49 6e 73 65 72 74 20 20 20 20 20  P_IdxInsert     
511c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511d0 20 20 20 20 20 20 36 37 0a 23 64 65 66 69 6e 65        67.#define
511e0 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 20 20 20   OP_IdxDelete   
511f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51200 20 20 20 20 20 20 20 20 37 30 0a 23 64 65 66 69          70.#defi
51210 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20  ne OP_IdxRowid  
51220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51230 20 20 20 20 20 20 20 20 20 20 37 31 0a 23 64 65            71.#de
51240 66 69 6e 65 20 4f 50 5f 49 64 78 4c 54 20 20 20  fine OP_IdxLT   
51250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51260 20 20 20 20 20 20 20 20 20 20 20 20 37 32 0a 23              72.#
51270 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 47 45 20  define OP_IdxGE 
51280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 31                81
512a0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 65 73 74  .#define OP_Dest
512b0 72 6f 79 20 20 20 20 20 20 20 20 20 20 20 20 20  roy             
512c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
512d0 39 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c  92.#define OP_Cl
512e0 65 61 72 20 20 20 20 20 20 20 20 20 20 20 20 20  ear             
512f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51300 20 20 39 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f    95.#define OP_
51310 43 72 65 61 74 65 49 6e 64 65 78 20 20 20 20 20  CreateIndex     
51320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51330 20 20 20 20 39 36 0a 23 64 65 66 69 6e 65 20 4f      96.#define O
51340 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 20 20  P_CreateTable   
51350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51360 20 20 20 20 20 20 39 37 0a 23 64 65 66 69 6e 65        97.#define
51370 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20   OP_ParseSchema 
51380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51390 20 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69          98.#defi
513a0 6e 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73  ne OP_LoadAnalys
513b0 69 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  is              
513c0 20 20 20 20 20 20 20 20 20 20 39 39 0a 23 64 65            99.#de
513d0 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 61 62 6c  fine OP_DropTabl
513e0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
513f0 20 20 20 20 20 20 20 20 20 20 20 31 30 30 0a 23             100.#
51400 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70 49 6e  define OP_DropIn
51410 64 65 78 20 20 20 20 20 20 20 20 20 20 20 20 20  dex             
51420 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 31               101
51430 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f 70  .#define OP_Drop
51440 54 72 69 67 67 65 72 20 20 20 20 20 20 20 20 20  Trigger         
51450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51460 30 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e  02.#define OP_In
51470 74 65 67 72 69 74 79 43 6b 20 20 20 20 20 20 20  tegrityCk       
51480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51490 20 31 30 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f   103.#define OP_
514a0 52 6f 77 53 65 74 41 64 64 20 20 20 20 20 20 20  RowSetAdd       
514b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514c0 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 20 4f     104.#define O
514d0 50 5f 52 6f 77 53 65 74 52 65 61 64 20 20 20 20  P_RowSetRead    
514e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514f0 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e 65       105.#define
51500 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 20 20   OP_RowSetTest  
51510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51520 20 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69         106.#defi
51530 6e 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 20 20  ne OP_Program   
51540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51550 20 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65           107.#de
51560 66 69 6e 65 20 4f 50 5f 50 61 72 61 6d 20 20 20  fine OP_Param   
51570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51580 20 20 20 20 20 20 20 20 20 20 20 31 30 38 0a 23             108.#
51590 64 65 66 69 6e 65 20 4f 50 5f 46 6b 43 6f 75 6e  define OP_FkCoun
515a0 74 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20  ter             
515b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 39               109
515c0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 6b 49 66  .#define OP_FkIf
515d0 5a 65 72 6f 20 20 20 20 20 20 20 20 20 20 20 20  Zero            
515e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
515f0 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d 65  10.#define OP_Me
51600 6d 4d 61 78 20 20 20 20 20 20 20 20 20 20 20 20  mMax            
51610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51620 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f   111.#define OP_
51630 49 66 50 6f 73 20 20 20 20 20 20 20 20 20 20 20  IfPos           
51640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51650 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20 4f     112.#define O
51660 50 5f 49 66 4e 65 67 20 20 20 20 20 20 20 20 20  P_IfNeg         
51670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51680 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65       113.#define
51690 20 4f 50 5f 49 66 5a 65 72 6f 20 20 20 20 20 20   OP_IfZero      
516a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
516b0 20 20 20 20 20 20 20 31 31 34 0a 23 64 65 66 69         114.#defi
516c0 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20 20  ne OP_AggStep   
516d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
516e0 20 20 20 20 20 20 20 20 20 31 31 35 0a 23 64 65           115.#de
516f0 66 69 6e 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c  fine OP_AggFinal
51700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51710 20 20 20 20 20 20 20 20 20 20 20 31 31 36 0a 23             116.#
51720 64 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d  define OP_Vacuum
51730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51740 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 37               117
51750 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 63 72  .#define OP_Incr
51760 56 61 63 75 75 6d 20 20 20 20 20 20 20 20 20 20  Vacuum          
51770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51780 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 78  18.#define OP_Ex
51790 70 69 72 65 20 20 20 20 20 20 20 20 20 20 20 20  pire            
517a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
517b0 20 31 31 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f   119.#define OP_
517c0 54 61 62 6c 65 4c 6f 63 6b 20 20 20 20 20 20 20  TableLock       
517d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
517e0 20 20 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f     120.#define O
517f0 50 5f 56 42 65 67 69 6e 20 20 20 20 20 20 20 20  P_VBegin        
51800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51810 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65       121.#define
51820 20 4f 50 5f 56 43 72 65 61 74 65 20 20 20 20 20   OP_VCreate     
51830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51840 20 20 20 20 20 20 20 31 32 32 0a 23 64 65 66 69         122.#defi
51850 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f 79 20 20  ne OP_VDestroy  
51860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51870 20 20 20 20 20 20 20 20 20 31 32 33 0a 23 64 65           123.#de
51880 66 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20  fine OP_VOpen   
51890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
518a0 20 20 20 20 20 20 20 20 20 20 20 31 32 34 0a 23             124.#
518b0 64 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c 74 65  define OP_VFilte
518c0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
518d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 35               125
518e0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c  .#define OP_VCol
518f0 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  umn             
51900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51910 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 4e  26.#define OP_VN
51920 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20  ext             
51930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51940 20 31 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f   127.#define OP_
51950 56 52 65 6e 61 6d 65 20 20 20 20 20 20 20 20 20  VRename         
51960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51970 20 20 20 31 32 38 0a 23 64 65 66 69 6e 65 20 4f     128.#define O
51980 50 5f 56 55 70 64 61 74 65 20 20 20 20 20 20 20  P_VUpdate       
51990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
519a0 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e 65       129.#define
519b0 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20   OP_Pagecount   
519c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
519d0 20 20 20 20 20 20 20 31 33 31 0a 23 64 65 66 69         131.#defi
519e0 6e 65 20 4f 50 5f 54 72 61 63 65 20 20 20 20 20  ne OP_Trace     
519f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51a00 20 20 20 20 20 20 20 20 20 31 33 32 0a 23 64 65           132.#de
51a10 66 69 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20 20 20  fine OP_Noop    
51a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51a30 20 20 20 20 20 20 20 20 20 20 20 31 33 33 0a 23             133.#
51a40 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61 69  define OP_Explai
51a50 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
51a60 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34               134
51a70 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ../* The followi
51a80 6e 67 20 6f 70 63 6f 64 65 20 76 61 6c 75 65 73  ng opcode values
51a90 20 61 72 65 20 6e 65 76 65 72 20 75 73 65 64 20   are never used 
51aa0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  */.#define OP_No
51ab0 74 55 73 65 64 5f 31 33 35 20 20 20 20 20 20 20  tUsed_135       
51ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51ad0 20 31 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f   135.#define OP_
51ae0 4e 6f 74 55 73 65 64 5f 31 33 36 20 20 20 20 20  NotUsed_136     
51af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51b00 20 20 20 31 33 36 0a 23 64 65 66 69 6e 65 20 4f     136.#define O
51b10 50 5f 4e 6f 74 55 73 65 64 5f 31 33 37 20 20 20  P_NotUsed_137   
51b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51b30 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e 65       137.#define
51b40 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 38 20   OP_NotUsed_138 
51b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51b60 20 20 20 20 20 20 20 31 33 38 0a 23 64 65 66 69         138.#defi
51b70 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33  ne OP_NotUsed_13
51b80 39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  9               
51b90 20 20 20 20 20 20 20 20 20 31 33 39 0a 23 64 65           139.#de
51ba0 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f  fine OP_NotUsed_
51bb0 31 34 30 20 20 20 20 20 20 20 20 20 20 20 20 20  140             
51bc0 20 20 20 20 20 20 20 20 20 20 20 31 34 30 0a 0a             140..
51bd0 0a 2f 2a 20 50 72 6f 70 65 72 74 69 65 73 20 73  ./* Properties s
51be0 75 63 68 20 61 73 20 22 6f 75 74 32 22 20 6f 72  uch as "out2" or
51bf0 20 22 6a 75 6d 70 22 20 74 68 61 74 20 61 72 65   "jump" that are
51c00 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a 2a 2a   specified in.**
51c10 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c 6c 6f 77   comments follow
51c20 69 6e 67 20 74 68 65 20 22 63 61 73 65 22 20 66  ing the "case" f
51c30 6f 72 20 65 61 63 68 20 6f 70 63 6f 64 65 20 69  or each opcode i
51c40 6e 20 74 68 65 20 76 64 62 65 2e 63 0a 2a 2a 20  n the vdbe.c.** 
51c50 61 72 65 20 65 6e 63 6f 64 65 64 20 69 6e 74 6f  are encoded into
51c60 20 62 69 74 76 65 63 74 6f 72 73 20 61 73 20 66   bitvectors as f
51c70 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a 23 64 65 66 69  ollows:.*/.#defi
51c80 6e 65 20 4f 50 46 4c 47 5f 4a 55 4d 50 20 20 20  ne OPFLG_JUMP   
51c90 20 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20           0x0001 
51ca0 20 2f 2a 20 6a 75 6d 70 3a 20 20 50 32 20 68 6f   /* jump:  P2 ho
51cb0 6c 64 73 20 6a 6d 70 20 74 61 72 67 65 74 20 2a  lds jmp target *
51cc0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f  /.#define OPFLG_
51cd0 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20  OUT2_PRERELEASE 
51ce0 30 78 30 30 30 32 20 20 2f 2a 20 6f 75 74 32 2d  0x0002  /* out2-
51cf0 70 72 65 72 65 6c 65 61 73 65 3a 20 2a 2f 0a 23  prerelease: */.#
51d00 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 31  define OPFLG_IN1
51d10 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
51d20 30 30 34 20 20 2f 2a 20 69 6e 31 3a 20 20 20 50  004  /* in1:   P
51d30 31 20 69 73 20 61 6e 20 69 6e 70 75 74 20 2a 2f  1 is an input */
51d40 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49  .#define OPFLG_I
51d50 4e 32 20 20 20 20 20 20 20 20 20 20 20 20 20 30  N2             0
51d60 78 30 30 30 38 20 20 2f 2a 20 69 6e 32 3a 20 20  x0008  /* in2:  
51d70 20 50 32 20 69 73 20 61 6e 20 69 6e 70 75 74 20   P2 is an input 
51d80 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47  */.#define OPFLG
51d90 5f 49 4e 33 20 20 20 20 20 20 20 20 20 20 20 20  _IN3            
51da0 20 30 78 30 30 31 30 20 20 2f 2a 20 69 6e 33 3a   0x0010  /* in3:
51db0 20 20 20 50 33 20 69 73 20 61 6e 20 69 6e 70 75     P3 is an inpu
51dc0 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  t */.#define OPF
51dd0 4c 47 5f 4f 55 54 32 20 20 20 20 20 20 20 20 20  LG_OUT2         
51de0 20 20 20 30 78 30 30 32 30 20 20 2f 2a 20 6f 75     0x0020  /* ou
51df0 74 32 3a 20 20 50 32 20 69 73 20 61 6e 20 6f 75  t2:  P2 is an ou
51e00 74 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tput */.#define 
51e10 4f 50 46 4c 47 5f 4f 55 54 33 20 20 20 20 20 20  OPFLG_OUT3      
51e20 20 20 20 20 20 20 30 78 30 30 34 30 20 20 2f 2a        0x0040  /*
51e30 20 6f 75 74 33 3a 20 20 50 33 20 69 73 20 61 6e   out3:  P3 is an
51e40 20 6f 75 74 70 75 74 20 2a 2f 0a 23 64 65 66 69   output */.#defi
51e50 6e 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c  ne OPFLG_INITIAL
51e60 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20 20 30 20 2a  IZER {\./*   0 *
51e70 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78  / 0x00, 0x01, 0x
51e80 30 35 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 20  05, 0x04, 0x04, 
51e90 30 78 31 30 2c 20 30 78 30 30 2c 20 30 78 30 32  0x10, 0x00, 0x02
51ea0 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f 20 30 78 30  ,\./*   8 */ 0x0
51eb0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  2, 0x02, 0x02, 0
51ec0 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x02, 0x00, 0x00,
51ed0 20 30 78 32 34 2c 20 30 78 32 34 2c 5c 0a 2f 2a   0x24, 0x24,\./*
51ee0 20 20 31 36 20 2a 2f 20 30 78 30 30 2c 20 30 78    16 */ 0x00, 0x
51ef0 30 30 2c 20 30 78 30 30 2c 20 30 78 32 34 2c 20  00, 0x00, 0x24, 
51f00 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 34  0x04, 0x05, 0x04
51f10 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20 32 34 20  , 0x00,\./*  24 
51f20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30  */ 0x00, 0x01, 0
51f30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 30 30 2c  x05, 0x05, 0x00,
51f40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
51f50 32 2c 5c 0a 2f 2a 20 20 33 32 20 2a 2f 20 30 78  2,\./*  32 */ 0x
51f60 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
51f70 30 78 30 32 2c 20 30 78 31 30 2c 20 30 78 30 30  0x02, 0x10, 0x00
51f80 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a 2f  , 0x00, 0x00,\./
51f90 2a 20 20 34 30 20 2a 2f 20 30 78 30 30 2c 20 30  *  40 */ 0x00, 0
51fa0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 31 31 2c  x00, 0x00, 0x11,
51fb0 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 31   0x11, 0x11, 0x1
51fc0 31 2c 20 30 78 30 38 2c 5c 0a 2f 2a 20 20 34 38  1, 0x08,\./*  48
51fd0 20 2a 2f 20 30 78 31 31 2c 20 30 78 31 31 2c 20   */ 0x11, 0x11, 
51fe0 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78 30 32  0x11, 0x11, 0x02
51ff0 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78  , 0x02, 0x00, 0x
52000 30 30 2c 5c 0a 2f 2a 20 20 35 36 20 2a 2f 20 30  00,\./*  56 */ 0
52010 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
52020 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30   0x00, 0x02, 0x0
52030 30 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 5c 0a  0, 0x01, 0x01,\.
52040 2f 2a 20 20 36 34 20 2a 2f 20 30 78 30 31 2c 20  /*  64 */ 0x01, 
52050 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 38  0x01, 0x01, 0x08
52060 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78  , 0x4c, 0x4c, 0x
52070 30 30 2c 20 30 78 30 32 2c 5c 0a 2f 2a 20 20 37  00, 0x02,\./*  7
52080 32 20 2a 2f 20 30 78 30 31 2c 20 30 78 30 35 2c  2 */ 0x01, 0x05,
52090 20 30 78 30 35 2c 20 30 78 31 35 2c 20 30 78 31   0x05, 0x15, 0x1
520a0 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30  5, 0x15, 0x15, 0
520b0 78 31 35 2c 5c 0a 2f 2a 20 20 38 30 20 2a 2f 20  x15,\./*  80 */ 
520c0 30 78 31 35 2c 20 30 78 30 31 2c 20 30 78 34 63  0x15, 0x01, 0x4c
520d0 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78  , 0x4c, 0x4c, 0x
520e0 34 63 2c 20 30 78 34 63 2c 20 30 78 34 63 2c 5c  4c, 0x4c, 0x4c,\
520f0 0a 2f 2a 20 20 38 38 20 2a 2f 20 30 78 34 63 2c  ./*  88 */ 0x4c,
52100 20 30 78 34 63 2c 20 30 78 34 63 2c 20 30 78 34   0x4c, 0x4c, 0x4
52110 63 2c 20 30 78 30 32 2c 20 30 78 32 34 2c 20 30  c, 0x02, 0x24, 0
52120 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 20  x02, 0x00,\./*  
52130 39 36 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 32  96 */ 0x02, 0x02
52140 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
52150 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
52160 30 78 30 30 2c 5c 0a 2f 2a 20 31 30 34 20 2a 2f  0x00,\./* 104 */
52170 20 30 78 30 63 2c 20 30 78 34 35 2c 20 30 78 31   0x0c, 0x45, 0x1
52180 35 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30  5, 0x01, 0x02, 0
52190 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 38 2c  x00, 0x01, 0x08,
521a0 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20 30 78 30 35  \./* 112 */ 0x05
521b0 2c 20 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78  , 0x05, 0x05, 0x
521c0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
521d0 30 78 30 31 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20  0x01, 0x00,\./* 
521e0 31 32 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30  120 */ 0x00, 0x0
521f0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
52200 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 30 2c  x00, 0x01, 0x00,
52210 20 30 78 30 31 2c 5c 0a 2f 2a 20 31 32 38 20 2a   0x01,\./* 128 *
52220 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  / 0x00, 0x00, 0x
52230 30 32 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20  02, 0x02, 0x00, 
52240 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
52250 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f 20 30 78 30  ,\./* 136 */ 0x0
52260 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
52270 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 34 2c  x00, 0x00, 0x04,
52280 20 30 78 30 34 2c 20 30 78 30 34 2c 5c 0a 2f 2a   0x04, 0x04,\./*
52290 20 31 34 34 20 2a 2f 20 30 78 30 34 2c 20 30 78   144 */ 0x04, 0x
522a0 30 34 2c 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  04,}../*********
522b0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63  ***** End of opc
522c0 6f 64 65 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  odes.h *********
522d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
522e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
522f0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
52300 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
52310 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
52320 66 66 20 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a  ff in vdbe.h ***
52330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52340 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f  ****/../*.** Pro
52350 74 6f 74 79 70 65 73 20 66 6f 72 20 74 68 65 20  totypes for the 
52360 56 44 42 45 20 69 6e 74 65 72 66 61 63 65 2e 20  VDBE interface. 
52370 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e   See comments on
52380 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
52390 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73  ion.** for a des
523a0 63 72 69 70 74 69 6f 6e 20 6f 66 20 77 68 61 74  cription of what
523b0 20 65 61 63 68 20 6f 66 20 74 68 65 73 65 20 72   each of these r
523c0 6f 75 74 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f  outines does..*/
523d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
523e0 56 64 62 65 20 2a 73 71 6c 69 74 65 33 56 64 62  Vdbe *sqlite3Vdb
523f0 65 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 2a  eCreate(sqlite3*
52400 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
52410 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
52420 65 41 64 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e  eAddOp0(Vdbe*,in
52430 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
52440 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
52450 62 65 41 64 64 4f 70 31 28 56 64 62 65 2a 2c 69  beAddOp1(Vdbe*,i
52460 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  nt,int);.SQLITE_
52470 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
52480 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 56 64  te3VdbeAddOp2(Vd
52490 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29  be*,int,int,int)
524a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
524b0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
524c0 41 64 64 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74  AddOp3(Vdbe*,int
524d0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ,int,int,int);.S
524e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
524f0 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
52500 4f 70 34 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  Op4(Vdbe*,int,in
52510 74 2c 69 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20  t,int,int,const 
52520 63 68 61 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a  char *zP4,int);.
52530 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
52540 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
52550 64 4f 70 34 49 6e 74 28 56 64 62 65 2a 2c 69 6e  dOp4Int(Vdbe*,in
52560 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e  t,int,int,int,in
52570 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
52580 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
52590 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62 65  beAddOpList(Vdbe
525a0 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65  *, int nOp, Vdbe
525b0 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f  OpList const *aO
525c0 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  p);.SQLITE_PRIVA
525d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
525e0 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62 65  dbeChangeP1(Vdbe
525f0 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74  *, int addr, int
52600 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   P1);.SQLITE_PRI
52610 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
52620 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 56 64  3VdbeChangeP2(Vd
52630 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69  be*, int addr, i
52640 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54 45 5f 50  nt P2);.SQLITE_P
52650 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
52660 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28  te3VdbeChangeP3(
52670 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c  Vdbe*, int addr,
52680 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c 49 54 45   int P3);.SQLITE
52690 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
526a0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
526b0 35 28 56 64 62 65 2a 2c 20 75 38 20 50 35 29 3b  5(Vdbe*, u8 P5);
526c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
526d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
526e0 4a 75 6d 70 48 65 72 65 28 56 64 62 65 2a 2c 20  JumpHere(Vdbe*, 
526f0 69 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49 54  int addr);.SQLIT
52700 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52710 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
52720 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c 20 69 6e  ToNoop(Vdbe*, in
52730 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 3b 0a  t addr, int N);.
52740 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
52750 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
52760 68 61 6e 67 65 50 34 28 56 64 62 65 2a 2c 20 69  hangeP4(Vdbe*, i
52770 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63  nt addr, const c
52780 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 4e 29  har *zP4, int N)
52790 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
527a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
527b0 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65 2a  eUsesBtree(Vdbe*
527c0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
527d0 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73  RIVATE VdbeOp *s
527e0 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28  qlite3VdbeGetOp(
527f0 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  Vdbe*, int);.SQL
52800 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
52810 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
52820 61 62 65 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c  abel(Vdbe*);.SQL
52830 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
52840 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
52850 74 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  te(Vdbe*);.SQLIT
52860 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52870 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65  qlite3VdbeMakeRe
52880 61 64 79 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  ady(Vdbe*,int,in
52890 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e  t,int,int,int,in
528a0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
528b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
528c0 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65 2a  beFinalize(Vdbe*
528d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
528e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
528f0 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56  beResolveLabel(V
52900 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  dbe*, int);.SQLI
52910 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
52920 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
52930 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23 69  tAddr(Vdbe*);.#i
52940 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
52950 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  G.SQLITE_PRIVATE
52960 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64     int sqlite3Vd
52970 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74  beAssertMayAbort
52980 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53  (Vdbe *, int);.S
52990 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
529a0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
529b0 54 72 61 63 65 28 56 64 62 65 2a 2c 46 49 4c 45  Trace(Vdbe*,FILE
529c0 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  *);.#endif.SQLIT
529d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
529e0 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 53  qlite3VdbeResetS
529f0 74 65 70 52 65 73 75 6c 74 28 56 64 62 65 2a 29  tepResult(Vdbe*)
52a00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52a10 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
52a20 52 65 73 65 74 28 56 64 62 65 2a 29 3b 0a 53 51  Reset(Vdbe*);.SQ
52a30 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
52a40 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  d sqlite3VdbeSet
52a50 4e 75 6d 43 6f 6c 73 28 56 64 62 65 2a 2c 69 6e  NumCols(Vdbe*,in
52a60 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
52a70 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
52a80 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64 62  beSetColName(Vdb
52a90 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63 6f  e*, int, int, co
52aa0 6e 73 74 20 63 68 61 72 20 2a 2c 20 76 6f 69 64  nst char *, void
52ab0 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
52ac0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
52ad0 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e   sqlite3VdbeCoun
52ae0 74 43 68 61 6e 67 65 73 28 56 64 62 65 2a 29 3b  tChanges(Vdbe*);
52af0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52b00 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33  sqlite3 *sqlite3
52b10 56 64 62 65 44 62 28 56 64 62 65 2a 29 3b 0a 53  VdbeDb(Vdbe*);.S
52b20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
52b30 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  id sqlite3VdbeSe
52b40 74 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e 73  tSql(Vdbe*, cons
52b50 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  t char *z, int n
52b60 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
52b70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
52b80 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65  te3VdbeSwap(Vdbe
52b90 2a 2c 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  *,Vdbe*);.SQLITE
52ba0 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20  _PRIVATE VdbeOp 
52bb0 2a 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b 65  *sqlite3VdbeTake
52bc0 4f 70 41 72 72 61 79 28 56 64 62 65 2a 2c 20 69  OpArray(Vdbe*, i
52bd0 6e 74 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  nt*, int*);.SQLI
52be0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
52bf0 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f 67 72  sqlite3VdbeProgr
52c00 61 6d 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  amDelete(sqlite3
52c10 20 2a 2c 20 53 75 62 50 72 6f 67 72 61 6d 20 2a   *, SubProgram *
52c20 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
52c30 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 76  RIVATE sqlite3_v
52c40 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64 62  alue *sqlite3Vdb
52c50 65 47 65 74 56 61 6c 75 65 28 56 64 62 65 2a 2c  eGetValue(Vdbe*,
52c60 20 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49 54   int, u8);.SQLIT
52c70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
52c80 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 72  qlite3VdbeSetVar
52c90 6d 61 73 6b 28 56 64 62 65 2a 2c 20 69 6e 74 29  mask(Vdbe*, int)
52ca0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
52cb0 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 53 51 4c 49  _OMIT_TRACE.SQLI
52cc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 63 68 61  TE_PRIVATE   cha
52cd0 72 20 2a 73 71 6c 69 74 65 33 56 64 62 65 45 78  r *sqlite3VdbeEx
52ce0 70 61 6e 64 53 71 6c 28 56 64 62 65 2a 2c 20 63  pandSql(Vdbe*, c
52cf0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e  onst char*);.#en
52d00 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  dif..SQLITE_PRIV
52d10 41 54 45 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  ATE UnpackedReco
52d20 72 64 20 2a 73 71 6c 69 74 65 33 56 64 62 65 52  rd *sqlite3VdbeR
52d30 65 63 6f 72 64 55 6e 70 61 63 6b 28 4b 65 79 49  ecordUnpack(KeyI
52d40 6e 66 6f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  nfo*,int,const v
52d50 6f 69 64 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b  oid*,char*,int);
52d60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52d70 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
52d80 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65  DeleteUnpackedRe
52d90 63 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63  cord(UnpackedRec
52da0 6f 72 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ord*);.SQLITE_PR
52db0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
52dc0 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61  3VdbeRecordCompa
52dd0 72 65 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69  re(int,const voi
52de0 64 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  d*,UnpackedRecor
52df0 64 2a 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e  d*);...#ifndef N
52e00 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
52e10 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
52e20 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56  te3VdbeComment(V
52e30 64 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  dbe*, const char
52e40 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e  *, ...);.# defin
52e50 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29  e VdbeComment(X)
52e60 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d    sqlite3VdbeCom
52e70 6d 65 6e 74 20 58 0a 53 51 4c 49 54 45 5f 50 52  ment X.SQLITE_PR
52e80 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
52e90 69 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  ite3VdbeNoopComm
52ea0 65 6e 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74  ent(Vdbe*, const
52eb0 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20   char*, ...);.# 
52ec0 64 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43  define VdbeNoopC
52ed0 6f 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74  omment(X)  sqlit
52ee0 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e  e3VdbeNoopCommen
52ef0 74 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  t X.#else.# defi
52f00 6e 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58  ne VdbeComment(X
52f10 29 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e  ).# define VdbeN
52f20 6f 6f 70 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65  oopComment(X).#e
52f30 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ndif..#endif../*
52f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
52f50 64 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a  d of vdbe.h ****
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 2f 0a 2f 2a  ************/./*
52f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
52fa0 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
52fb0 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71  e left off in sq
52fc0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
52fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
52fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
52ff0 63 6c 75 64 65 20 70 61 67 65 72 2e 68 20 69 6e  clude pager.h in
53000 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73   the middle of s
53010 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
53020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
53030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
53040 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 68  gin file pager.h
53050 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
53060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
53080 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
53090 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
530a0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
530b0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
530c0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
530d0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
530e0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
530f0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
53100 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
53110 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
53120 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
53130 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
53140 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
53150 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
53160 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
53170 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
53180 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
53190 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
531a0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
531b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
531c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
531d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
531e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
531f0 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  ****.** This hea
53200 64 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73  der file defines
53210 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74   the interface t
53220 68 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 70  hat the sqlite p
53230 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62  age cache.** sub
53240 73 79 73 74 65 6d 2e 20 20 54 68 65 20 70 61 67  system.  The pag
53250 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65  e cache subsyste
53260 6d 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74  m reads and writ
53270 65 73 20 61 20 66 69 6c 65 20 61 20 70 61 67 65  es a file a page
53280 0a 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 61 6e  .** at a time an
53290 64 20 70 72 6f 76 69 64 65 73 20 61 20 6a 6f 75  d provides a jou
532a0 72 6e 61 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63  rnal for rollbac
532b0 6b 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f  k..*/..#ifndef _
532c0 50 41 47 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65  PAGER_H_.#define
532d0 20 5f 50 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a   _PAGER_H_../*.*
532e0 2a 20 44 65 66 61 75 6c 74 20 6d 61 78 69 6d 75  * Default maximu
532f0 6d 20 73 69 7a 65 20 66 6f 72 20 70 65 72 73 69  m size for persi
53300 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69  stent journal fi
53310 6c 65 73 2e 20 41 20 6e 65 67 61 74 69 76 65 20  les. A negative 
53320 0a 2a 2a 20 76 61 6c 75 65 20 6d 65 61 6e 73 20  .** value means 
53330 6e 6f 20 6c 69 6d 69 74 2e 20 54 68 69 73 20 76  no limit. This v
53340 61 6c 75 65 20 6d 61 79 20 62 65 20 6f 76 65 72  alue may be over
53350 72 69 64 64 65 6e 20 75 73 69 6e 67 20 74 68 65  ridden using the
53360 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65   .** sqlite3Page
53370 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69  rJournalSizeLimi
53380 74 28 29 20 41 50 49 2e 20 53 65 65 20 61 6c 73  t() API. See als
53390 6f 20 22 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61  o "PRAGMA journa
533a0 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a  l_size_limit"..*
533b0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
533c0 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c  _DEFAULT_JOURNAL
533d0 5f 53 49 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64  _SIZE_LIMIT.  #d
533e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46  efine SQLITE_DEF
533f0 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a  AULT_JOURNAL_SIZ
53400 45 5f 4c 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69  E_LIMIT -1.#endi
53410 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70  f../*.** The typ
53420 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73  e used to repres
53430 65 6e 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65  ent a page numbe
53440 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61  r.  The first pa
53450 67 65 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20  ge in a file.** 
53460 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31  is called page 1
53470 2e 20 20 30 20 69 73 20 75 73 65 64 20 74 6f 20  .  0 is used to 
53480 72 65 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61  represent "not a
53490 20 70 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64   page"..*/.typed
534a0 65 66 20 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a  ef u32 Pgno;../*
534b0 0a 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69  .** Each open fi
534c0 6c 65 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79  le is managed by
534d0 20 61 20 73 65 70 61 72 61 74 65 20 69 6e 73 74   a separate inst
534e0 61 6e 63 65 20 6f 66 20 74 68 65 20 22 50 61 67  ance of the "Pag
534f0 65 72 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  er" structure..*
53500 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
53510 20 50 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f   Pager Pager;../
53520 2a 0a 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65  *.** Handle type
53530 20 66 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74   for pages..*/.t
53540 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67  ypedef struct Pg
53550 48 64 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a  Hdr DbPage;../*.
53560 2a 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 50  ** Page number P
53570 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20  AGER_MJ_PGNO is 
53580 6e 65 76 65 72 20 75 73 65 64 20 69 6e 20 61 6e  never used in an
53590 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
535a0 20 28 69 74 20 69 73 0a 2a 2a 20 72 65 73 65 72   (it is.** reser
535b0 76 65 64 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20  ved for working 
535c0 61 72 6f 75 6e 64 20 61 20 77 69 6e 64 6f 77 73  around a windows
535d0 2f 70 6f 73 69 78 20 69 6e 63 6f 6d 70 61 74 69  /posix incompati
535e0 62 69 6c 69 74 79 29 2e 20 49 74 20 69 73 0a 2a  bility). It is.*
535f0 2a 20 75 73 65 64 20 69 6e 20 74 68 65 20 6a 6f  * used in the jo
53600 75 72 6e 61 6c 20 74 6f 20 73 69 67 6e 69 66 79  urnal to signify
53610 20 74 68 61 74 20 74 68 65 20 72 65 6d 61 69 6e   that the remain
53620 64 65 72 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  der of the journ
53630 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64  al file .** is d
53640 65 76 6f 74 65 64 20 74 6f 20 73 74 6f 72 69 6e  evoted to storin
53650 67 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  g a master journ
53660 61 6c 20 6e 61 6d 65 20 2d 20 74 68 65 72 65 20  al name - there 
53670 61 72 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65  are no more page
53680 73 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63  s to.** roll bac
53690 6b 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20  k. See comments 
536a0 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 77 72 69  for function wri
536b0 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28  teMasterJournal(
536c0 29 20 69 6e 20 70 61 67 65 72 2e 63 20 0a 2a 2a  ) in pager.c .**
536d0 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f   for details..*/
536e0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4d  .#define PAGER_M
536f0 4a 5f 50 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f  J_PGNO(x) ((Pgno
53700 29 28 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f  )((PENDING_BYTE/
53710 28 28 78 29 2d 3e 70 61 67 65 53 69 7a 65 29 29  ((x)->pageSize))
53720 2b 31 29 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  +1))../*.** Allo
53730 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74  wed values for t
53740 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74  he flags paramet
53750 65 72 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67  er to sqlite3Pag
53760 65 72 4f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20  erOpen()..**.** 
53770 4e 4f 54 45 3a 20 54 68 65 73 65 20 76 61 6c 75  NOTE: These valu
53780 65 73 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68  es must match th
53790 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
537a0 42 54 52 45 45 5f 20 76 61 6c 75 65 73 20 69 6e  BTREE_ values in
537b0 20 62 74 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65   btree.h..*/.#de
537c0 66 69 6e 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f  fine PAGER_OMIT_
537d0 4a 4f 55 52 4e 41 4c 20 20 30 78 30 30 30 31 20  JOURNAL  0x0001 
537e0 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65     /* Do not use
537f0 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72   a rollback jour
53800 6e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  nal */.#define P
53810 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b  AGER_NO_READLOCK
53820 20 20 20 30 78 30 30 30 32 20 20 20 20 2f 2a 20     0x0002    /* 
53830 4f 6d 69 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f  Omit readlocks o
53840 6e 20 72 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73  n readonly files
53850 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64   */../*.** Valid
53860 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20   values for the 
53870 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
53880 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c  to sqlite3PagerL
53890 6f 63 6b 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f  ockingMode()..*/
538a0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c  .#define PAGER_L
538b0 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59  OCKINGMODE_QUERY
538c0 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69 6e 65        -1.#define
538d0 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f   PAGER_LOCKINGMO
538e0 44 45 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30  DE_NORMAL      0
538f0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4c  .#define PAGER_L
53900 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55  OCKINGMODE_EXCLU
53910 53 49 56 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20  SIVE   1../*.** 
53920 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72  Valid values for
53930 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
53940 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 50  ment to sqlite3P
53950 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  agerJournalMode(
53960 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  )..*/.#define PA
53970 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
53980 51 55 45 52 59 20 20 20 20 20 20 2d 31 0a 23 64  QUERY      -1.#d
53990 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52  efine PAGER_JOUR
539a0 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20  NALMODE_DELETE  
539b0 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 69      0   /* Commi
539c0 74 20 62 79 20 64 65 6c 65 74 69 6e 67 20 6a 6f  t by deleting jo
539d0 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 64  urnal file */.#d
539e0 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52  efine PAGER_JOUR
539f0 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20  NALMODE_PERSIST 
53a00 20 20 20 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 69      1   /* Commi
53a10 74 20 62 79 20 7a 65 72 6f 69 6e 67 20 6a 6f 75  t by zeroing jou
53a20 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 23  rnal header */.#
53a30 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55  define PAGER_JOU
53a40 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20  RNALMODE_OFF    
53a50 20 20 20 20 20 32 20 20 20 2f 2a 20 4a 6f 75 72       2   /* Jour
53a60 6e 61 6c 20 6f 6d 69 74 74 65 64 2e 20 20 2a 2f  nal omitted.  */
53a70 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 4a  .#define PAGER_J
53a80 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43  OURNALMODE_TRUNC
53a90 41 54 45 20 20 20 20 33 20 20 20 2f 2a 20 43 6f  ATE    3   /* Co
53aa0 6d 6d 69 74 20 62 79 20 74 72 75 6e 63 61 74 69  mmit by truncati
53ab0 6e 67 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64  ng journal */.#d
53ac0 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52  efine PAGER_JOUR
53ad0 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20  NALMODE_MEMORY  
53ae0 20 20 20 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 65      4   /* In-me
53af0 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  mory journal fil
53b00 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  e */../*.** The 
53b10 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74 68 69  remainder of thi
53b20 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
53b30 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  the declarations
53b40 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
53b50 73 0a 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20 75  s.** that make u
53b60 70 20 74 68 65 20 50 61 67 65 72 20 73 75 62 2d  p the Pager sub-
53b70 73 79 73 74 65 6d 20 41 50 49 2e 20 53 65 65 20  system API. See 
53b80 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d  source code comm
53b90 65 6e 74 73 20 66 6f 72 20 0a 2a 2a 20 61 20 64  ents for .** a d
53ba0 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 74  etailed descript
53bb0 69 6f 6e 20 6f 66 20 65 61 63 68 20 72 6f 75 74  ion of each rout
53bc0 69 6e 65 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e  ine..*/../* Open
53bd0 20 61 6e 64 20 63 6c 6f 73 65 20 61 20 50 61 67   and close a Pag
53be0 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a  er connection. *
53bf0 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  / .SQLITE_PRIVAT
53c00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
53c10 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  erOpen(.  sqlite
53c20 33 5f 76 66 73 2a 2c 0a 20 20 50 61 67 65 72 20  3_vfs*,.  Pager 
53c30 2a 2a 70 70 50 61 67 65 72 2c 0a 20 20 63 6f 6e  **ppPager,.  con
53c40 73 74 20 63 68 61 72 2a 2c 0a 20 20 69 6e 74 2c  st char*,.  int,
53c50 0a 20 20 69 6e 74 2c 0a 20 20 69 6e 74 2c 0a 20  .  int,.  int,. 
53c60 20 76 6f 69 64 28 2a 29 28 44 62 50 61 67 65 2a   void(*)(DbPage*
53c70 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ).);.SQLITE_PRIV
53c80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
53c90 61 67 65 72 43 6c 6f 73 65 28 50 61 67 65 72 20  agerClose(Pager 
53ca0 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c 49 54 45  *pPager);.SQLITE
53cb0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
53cc0 69 74 65 33 50 61 67 65 72 52 65 61 64 46 69 6c  ite3PagerReadFil
53cd0 65 68 65 61 64 65 72 28 50 61 67 65 72 2a 2c 20  eheader(Pager*, 
53ce0 69 6e 74 2c 20 75 6e 73 69 67 6e 65 64 20 63 68  int, unsigned ch
53cf0 61 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69  ar*);../* Functi
53d00 6f 6e 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 66  ons used to conf
53d10 69 67 75 72 65 20 61 20 50 61 67 65 72 20 6f 62  igure a Pager ob
53d20 6a 65 63 74 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f  ject. */.SQLITE_
53d30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
53d40 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79  ite3PagerSetBusy
53d50 68 61 6e 64 6c 65 72 28 50 61 67 65 72 2a 2c 20  handler(Pager*, 
53d60 69 6e 74 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20  int(*)(void *), 
53d70 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  void *);.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 53 65 74 50 61 67 65 73  te3PagerSetPages
53da0 69 7a 65 28 50 61 67 65 72 2a 2c 20 75 31 36 2a  ize(Pager*, u16*
53db0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
53dc0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
53dd0 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f  e3PagerMaxPageCo
53de0 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e 74 29  unt(Pager*, int)
53df0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53e00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
53e10 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 50  erSetCachesize(P
53e20 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ager*, int);.SQL
53e30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
53e40 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
53e50 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65  SafetyLevel(Page
53e60 72 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  r*,int,int);.SQL
53e70 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
53e80 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b  sqlite3PagerLock
53e90 69 6e 67 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c  ingMode(Pager *,
53ea0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
53eb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
53ec0 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64  3PagerJournalMod
53ed0 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b  e(Pager *, int);
53ee0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
53ef0 69 36 34 20 73 71 6c 69 74 65 33 50 61 67 65 72  i64 sqlite3Pager
53f00 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74  JournalSizeLimit
53f10 28 50 61 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a  (Pager *, i64);.
53f20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
53f30 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a  qlite3_backup **
53f40 73 71 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b  sqlite3PagerBack
53f50 75 70 50 74 72 28 50 61 67 65 72 2a 29 3b 0a 0a  upPtr(Pager*);..
53f60 2f 2a 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65  /* Functions use
53f70 64 20 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20  d to obtain and 
53f80 72 65 6c 65 61 73 65 20 70 61 67 65 20 72 65 66  release page ref
53f90 65 72 65 6e 63 65 73 2e 20 2a 2f 20 0a 53 51 4c  erences. */ .SQL
53fa0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
53fb0 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75  sqlite3PagerAcqu
53fc0 69 72 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ire(Pager *pPage
53fd0 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62  r, Pgno pgno, Db
53fe0 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 69  Page **ppPage, i
53ff0 6e 74 20 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65  nt clrFlag);.#de
54000 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 67 65  fine sqlite3Page
54010 72 47 65 74 28 41 2c 42 2c 43 29 20 73 71 6c 69  rGet(A,B,C) sqli
54020 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28  te3PagerAcquire(
54030 41 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f  A,B,C,0).SQLITE_
54040 50 52 49 56 41 54 45 20 44 62 50 61 67 65 20 2a  PRIVATE DbPage *
54050 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b  sqlite3PagerLook
54060 75 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  up(Pager *pPager
54070 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51  , Pgno pgno);.SQ
54080 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
54090 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  d sqlite3PagerRe
540a0 66 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49  f(DbPage*);.SQLI
540b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
540c0 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
540d0 66 28 44 62 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20  f(DbPage*);../* 
540e0 4f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 70 61  Operations on pa
540f0 67 65 20 72 65 66 65 72 65 6e 63 65 73 2e 20 2a  ge references. *
54100 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
54110 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
54120 72 57 72 69 74 65 28 44 62 50 61 67 65 2a 29 3b  rWrite(DbPage*);
54130 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54140 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
54150 72 44 6f 6e 74 57 72 69 74 65 28 44 62 50 61 67  rDontWrite(DbPag
54160 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
54170 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
54180 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50 61 67  agerMovepage(Pag
54190 65 72 2a 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f  er*,DbPage*,Pgno
541a0 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
541b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
541c0 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75  3PagerPageRefcou
541d0 6e 74 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c  nt(DbPage*);.SQL
541e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
541f0 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65   *sqlite3PagerGe
54200 74 44 61 74 61 28 44 62 50 61 67 65 20 2a 29 3b  tData(DbPage *);
54210 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45   .SQLITE_PRIVATE
54220 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
54230 67 65 72 47 65 74 45 78 74 72 61 28 44 62 50 61  gerGetExtra(DbPa
54240 67 65 20 2a 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63  ge *); ../* Func
54250 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 6d 61  tions used to ma
54260 6e 61 67 65 20 70 61 67 65 72 20 74 72 61 6e 73  nage pager trans
54270 61 63 74 69 6f 6e 73 20 61 6e 64 20 73 61 76 65  actions and save
54280 70 6f 69 6e 74 73 2e 20 2a 2f 0a 53 51 4c 49 54  points. */.SQLIT
54290 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
542a0 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
542b0 75 6e 74 28 50 61 67 65 72 2a 2c 20 69 6e 74 2a  unt(Pager*, int*
542c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
542d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
542e0 65 72 42 65 67 69 6e 28 50 61 67 65 72 2a 2c 20  erBegin(Pager*, 
542f0 69 6e 74 20 65 78 46 6c 61 67 2c 20 69 6e 74 29  int exFlag, int)
54300 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
54310 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
54320 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
54330 50 61 67 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61  Pager*,const cha
54340 72 20 2a 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29  r *zMaster, int)
54350 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
54360 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
54370 72 53 79 6e 63 28 50 61 67 65 72 20 2a 70 50 61  rSync(Pager *pPa
54380 67 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ger);.SQLITE_PRI
54390 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
543a0 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
543b0 54 77 6f 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  Two(Pager*);.SQL
543c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
543d0 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c  sqlite3PagerRoll
543e0 62 61 63 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51  back(Pager*);.SQ
543f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
54400 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
54410 6e 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72  nSavepoint(Pager
54420 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e 29   *pPager, int n)
54430 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
54440 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
54450 72 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65 72  rSavepoint(Pager
54460 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6f 70   *pPager, int op
54470 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74  , int iSavepoint
54480 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
54490 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
544a0 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61 67  erSharedLock(Pag
544b0 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 0a 2f 2a  er *pPager);../*
544c0 20 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20   Functions used 
544d0 74 6f 20 71 75 65 72 79 20 70 61 67 65 72 20 73  to query pager s
544e0 74 61 74 65 20 61 6e 64 20 63 6f 6e 66 69 67 75  tate and configu
544f0 72 61 74 69 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54  ration. */.SQLIT
54500 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71 6c  E_PRIVATE u8 sql
54510 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f  ite3PagerIsreado
54520 6e 6c 79 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  nly(Pager*);.SQL
54530 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
54540 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63  sqlite3PagerRefc
54550 6f 75 6e 74 28 50 61 67 65 72 2a 29 3b 0a 53 51  ount(Pager*);.SQ
54560 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
54570 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
54580 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61  PagerFilename(Pa
54590 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
545a0 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69  IVATE const sqli
545b0 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33  te3_vfs *sqlite3
545c0 50 61 67 65 72 56 66 73 28 50 61 67 65 72 2a 29  PagerVfs(Pager*)
545d0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
545e0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73   sqlite3_file *s
545f0 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28  qlite3PagerFile(
54600 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  Pager*);.SQLITE_
54610 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
54620 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  ar *sqlite3Pager
54630 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65  Journalname(Page
54640 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
54650 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
54660 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72  agerNosync(Pager
54670 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
54680 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
54690 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 50  PagerTempSpace(P
546a0 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
546b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
546c0 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50  e3PagerIsMemdb(P
546d0 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63  ager*);../* Func
546e0 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 74 72  tions used to tr
546f0 75 6e 63 61 74 65 20 74 68 65 20 64 61 74 61 62  uncate the datab
54700 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c  ase file. */.SQL
54710 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
54720 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72 75   sqlite3PagerTru
54730 6e 63 61 74 65 49 6d 61 67 65 28 50 61 67 65 72  ncateImage(Pager
54740 2a 2c 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 46 75 6e  *,Pgno);../* Fun
54750 63 74 69 6f 6e 73 20 74 6f 20 73 75 70 70 6f 72  ctions to suppor
54760 74 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  t testing and de
54770 62 75 67 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 20  bugging. */.#if 
54780 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29  !defined(NDEBUG)
54790 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
547a0 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45 5f  TE_TEST).SQLITE_
547b0 50 52 49 56 41 54 45 20 20 20 50 67 6e 6f 20 73  PRIVATE   Pgno s
547c0 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e  qlite3PagerPagen
547d0 75 6d 62 65 72 28 44 62 50 61 67 65 2a 29 3b 0a  umber(DbPage*);.
547e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
547f0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
54800 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62 50  rIswriteable(DbP
54810 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 69  age*);.#endif.#i
54820 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
54830 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
54840 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61    int *sqlite3Pa
54850 67 65 72 53 74 61 74 73 28 50 61 67 65 72 2a 29  gerStats(Pager*)
54860 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
54870 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50     void sqlite3P
54880 61 67 65 72 52 65 66 64 75 6d 70 28 50 61 67 65  agerRefdump(Page
54890 72 2a 29 3b 0a 20 20 76 6f 69 64 20 64 69 73 61  r*);.  void disa
548a0 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
548b0 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 3b 0a 20  _errors(void);. 
548c0 20 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d   void enable_sim
548d0 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
548e0 28 76 6f 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20  (void);.#else.# 
548f0 64 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f 73  define disable_s
54900 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
54910 72 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 6e  rs().# define en
54920 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69  able_simulated_i
54930 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 69  o_errors().#endi
54940 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41  f..#endif /* _PA
54950 47 45 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  GER_H_ */../****
54960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
54970 66 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a  f pager.h ******
54980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
549a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
549b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
549c0 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
549d0 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74  eft off in sqlit
549e0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
549f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
54a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
54a10 64 65 20 70 63 61 63 68 65 2e 68 20 69 6e 20 74  de pcache.h in t
54a20 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
54a30 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
54a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
54a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
54a60 20 66 69 6c 65 20 70 63 61 63 68 65 2e 68 20 2a   file pcache.h *
54a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
54aa0 20 32 30 30 38 20 41 75 67 75 73 74 20 30 35 0a   2008 August 05.
54ab0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
54ac0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
54ad0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
54ae0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
54af0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
54b00 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
54b10 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
54b20 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
54b30 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
54b40 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
54b50 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
54b60 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
54b70 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
54b80 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
54b90 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
54ba0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
54bb0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
54bc0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
54bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
54c10 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
54c20 6c 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  le defines the i
54c30 6e 74 65 72 66 61 63 65 20 74 68 61 74 20 74 68  nterface that th
54c40 65 20 73 71 6c 69 74 65 20 70 61 67 65 20 63 61  e sqlite page ca
54c50 63 68 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d  che.** subsystem
54c60 2e 20 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f  . .*/..#ifndef _
54c70 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 65 64  PCACHE_H_..typed
54c80 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72 20  ef struct PgHdr 
54c90 50 67 48 64 72 3b 0a 74 79 70 65 64 65 66 20 73  PgHdr;.typedef s
54ca0 74 72 75 63 74 20 50 43 61 63 68 65 20 50 43 61  truct PCache PCa
54cb0 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65 72  che;../*.** Ever
54cc0 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61  y page in the ca
54cd0 63 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65  che is controlle
54ce0 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  d by an instance
54cf0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
54d00 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a  g.** structure..
54d10 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72 20  */.struct PgHdr 
54d20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61 3b  {.  void *pData;
54d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54d40 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66     /* Content of
54d50 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20   this page */.  
54d60 76 6f 69 64 20 2a 70 45 78 74 72 61 3b 20 20 20  void *pExtra;   
54d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
54d80 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 6e 74 20  * Extra content 
54d90 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72  */.  PgHdr *pDir
54da0 74 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ty;             
54db0 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 65 6e 74      /* Transient
54dc0 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70   list of dirty p
54dd0 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  ages */.  Pgno p
54de0 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  gno;            
54df0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
54e00 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69 73   number for this
54e10 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 67 65 72   page */.  Pager
54e20 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20 20   *pPager;       
54e30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
54e40 20 70 61 67 65 72 20 74 68 69 73 20 70 61 67 65   pager this page
54e50 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a 23   is part of */.#
54e60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45  ifdef SQLITE_CHE
54e70 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 32 20 70  CK_PAGES.  u32 p
54e80 61 67 65 48 61 73 68 3b 20 20 20 20 20 20 20 20  ageHash;        
54e90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
54ea0 68 20 6f 66 20 70 61 67 65 20 63 6f 6e 74 65 6e  h of page conten
54eb0 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75 31  t */.#endif.  u1
54ec0 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  6 flags;        
54ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
54ee0 50 47 48 44 52 20 66 6c 61 67 73 20 64 65 66 69  PGHDR flags defi
54ef0 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 0a 20 20  ned below */..  
54f00 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
54f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54f40 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 6c 65  *******.  ** Ele
54f50 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 72 65 20  ments above are 
54f60 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 74 68 61  public.  All tha
54f70 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 70 72 69  t follows is pri
54f80 76 61 74 65 20 74 6f 20 70 63 61 63 68 65 2e 63  vate to pcache.c
54f90 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f 75 6c 64  .  ** and should
54fa0 20 6e 6f 74 20 62 65 20 61 63 63 65 73 73 65 64   not be accessed
54fb0 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65   by other module
54fc0 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 20 6e 52  s..  */.  i16 nR
54fd0 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
54fe0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
54ff0 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20 74  er of users of t
55000 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 43  his page */.  PC
55010 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20 20  ache *pCache;   
55020 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
55030 43 61 63 68 65 20 74 68 61 74 20 6f 77 6e 73 20  Cache that owns 
55040 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 0a 20 20  this page */..  
55050 50 67 48 64 72 20 2a 70 44 69 72 74 79 4e 65 78  PgHdr *pDirtyNex
55060 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  t;             /
55070 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74 20 69  * Next element i
55080 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20  n list of dirty 
55090 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64 72  pages */.  PgHdr
550a0 20 2a 70 44 69 72 74 79 50 72 65 76 3b 20 20 20   *pDirtyPrev;   
550b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
550c0 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 20 69 6e  vious element in
550d0 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70   list of dirty p
550e0 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 42  ages */.};../* B
550f0 69 74 20 76 61 6c 75 65 73 20 66 6f 72 20 50 67  it values for Pg
55100 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a 23 64 65  Hdr.flags */.#de
55110 66 69 6e 65 20 50 47 48 44 52 5f 44 49 52 54 59  fine PGHDR_DIRTY
55120 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30               0x0
55130 30 32 20 20 2f 2a 20 50 61 67 65 20 68 61 73 20  02  /* Page has 
55140 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69  changed */.#defi
55150 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  ne PGHDR_NEED_SY
55160 4e 43 20 20 20 20 20 20 20 20 20 30 78 30 30 34  NC         0x004
55170 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 20 72    /* Fsync the r
55180 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
55190 62 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20 20  before.         
551a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
551b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
551c0 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 70 61   writing this pa
551d0 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ge to the databa
551e0 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47  se */.#define PG
551f0 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 20 20  HDR_NEED_READ   
55200 20 20 20 20 20 20 30 78 30 30 38 20 20 2f 2a 20        0x008  /* 
55210 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 65 61  Content is unrea
55220 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48  d */.#define PGH
55230 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45 4c  DR_REUSE_UNLIKEL
55240 59 20 20 20 20 30 78 30 31 30 20 20 2f 2a 20 41  Y    0x010  /* A
55250 20 68 69 6e 74 20 74 68 61 74 20 72 65 75 73 65   hint that reuse
55260 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f 0a   is unlikely */.
55270 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 4f  #define PGHDR_DO
55280 4e 54 5f 57 52 49 54 45 20 20 20 20 20 20 20 20  NT_WRITE        
55290 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 6f 74  0x020  /* Do not
552a0 20 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 74   write content t
552b0 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49 6e  o disk */../* In
552c0 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75  itialize and shu
552d0 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63  tdown the page c
552e0 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20 2a  ache subsystem *
552f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
55300 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63   int sqlite3Pcac
55310 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69  heInitialize(voi
55320 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  d);.SQLITE_PRIVA
55330 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
55340 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76 6f  cacheShutdown(vo
55350 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 63 61  id);../* Page ca
55360 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e 61 67  che buffer manag
55370 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 65 20  ement:.** These 
55380 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65  routines impleme
55390 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  nt SQLITE_CONFIG
553a0 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f 0a 53  _PAGECACHE..*/.S
553b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
553c0 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68 65  id sqlite3PCache
553d0 42 75 66 66 65 72 53 65 74 75 70 28 76 6f 69 64  BufferSetup(void
553e0 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20   *, int sz, int 
553f0 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 20 61  n);../* Create a
55400 20 6e 65 77 20 70 61 67 65 72 20 63 61 63 68 65   new pager cache
55410 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d 6f 72  ..** Under memor
55420 79 20 73 74 72 65 73 73 2c 20 69 6e 76 6f 6b 65  y stress, invoke
55430 20 78 53 74 72 65 73 73 20 74 6f 20 74 72 79 20   xStress to try 
55440 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c  to make pages cl
55450 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c 65  ean..** Only cle
55460 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 64 20  an and unpinned 
55470 70 61 67 65 73 20 63 61 6e 20 62 65 20 72 65 63  pages can be rec
55480 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  laimed..*/.SQLIT
55490 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
554a0 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e  qlite3PcacheOpen
554b0 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c 20  (.  int szPage, 
554c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
554d0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76     /* Size of ev
554e0 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  ery page */.  in
554f0 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20 20  t szExtra,      
55500 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
55510 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73 6f  Extra space asso
55520 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68  ciated with each
55530 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62   page */.  int b
55540 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20  Purgeable,      
55550 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
55560 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20 6f  e if pages are o
55570 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20  n backing store 
55580 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72 65  */.  int (*xStre
55590 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64 72  ss)(void*, PgHdr
555a0 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74  *), /* Call to t
555b0 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73  ry to make pages
555c0 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64   clean */.  void
555d0 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20 20   *pStress,      
555e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
555f0 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73  gument to xStres
55600 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70  s */.  PCache *p
55610 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20 20  ToInit          
55620 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f        /* Preallo
55630 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20  cated space for 
55640 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 3b  the PCache */.);
55650 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20  ../* Modify the 
55660 70 61 67 65 2d 73 69 7a 65 20 61 66 74 65 72 20  page-size after 
55670 74 68 65 20 63 61 63 68 65 20 68 61 73 20 62 65  the cache has be
55680 65 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a 53  en created. */.S
55690 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
556a0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
556b0 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 63  SetPageSize(PCac
556c0 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20  he *, int);../* 
556d0 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
556e0 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 43  in bytes of a PC
556f0 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 55 73  ache object.  Us
55700 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61 74  ed to preallocat
55710 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 70 61  e.** storage spa
55720 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ce..*/.SQLITE_PR
55730 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
55740 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64  3PcacheSize(void
55750 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65 61  );../* One relea
55760 73 65 20 70 65 72 20 73 75 63 63 65 73 73 66 75  se per successfu
55770 6c 20 66 65 74 63 68 2e 20 20 50 61 67 65 20 69  l fetch.  Page i
55780 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c 20 72  s pinned until r
55790 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 66 65  eleased..** Refe
557a0 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e 20 0a  rence counted. .
557b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
557c0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
557d0 63 68 65 46 65 74 63 68 28 50 43 61 63 68 65 2a  cheFetch(PCache*
557e0 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65 61  , Pgno, int crea
557f0 74 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a 29  teFlag, PgHdr**)
55800 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
55810 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
55820 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64 72  cheRelease(PgHdr
55830 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
55840 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
55850 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72  PcacheDrop(PgHdr
55860 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52  *);         /* R
55870 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f 6d 20  emove page from 
55880 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f  cache */.SQLITE_
55890 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
558a0 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44 69  ite3PcacheMakeDi
558b0 72 74 79 28 50 67 48 64 72 2a 29 3b 20 20 20 20  rty(PgHdr*);    
558c0 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 61 67  /* Make sure pag
558d0 65 20 69 73 20 6d 61 72 6b 65 64 20 64 69 72 74  e is marked dirt
558e0 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  y */.SQLITE_PRIV
558f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
55900 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28  PcacheMakeClean(
55910 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20 4d  PgHdr*);    /* M
55920 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 61 67  ark a single pag
55930 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 53 51  e as clean */.SQ
55940 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
55950 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43  d sqlite3PcacheC
55960 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 2a 29  leanAll(PCache*)
55970 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 6c 6c  ;    /* Mark all
55980 20 64 69 72 74 79 20 6c 69 73 74 20 70 61 67 65   dirty list page
55990 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a 2f  s as clean */../
559a0 2a 20 43 68 61 6e 67 65 20 61 20 70 61 67 65 20  * Change a page 
559b0 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 20 62 79  number.  Used by
559c0 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 2a 2f   incr-vacuum. */
559d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
559e0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
559f0 68 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c 20 50  heMove(PgHdr*, P
55a00 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76 65  gno);../* Remove
55a10 20 61 6c 6c 20 70 61 67 65 73 20 77 69 74 68 20   all pages with 
55a20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 20 74  pgno>x.  Reset t
55a30 68 65 20 63 61 63 68 65 20 69 66 20 78 3d 3d 30  he cache if x==0
55a40 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
55a50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
55a60 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 50 43  cacheTruncate(PC
55a70 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 3b 0a  ache*, Pgno x);.
55a80 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 20 6f  ./* Get a list o
55a90 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65  f all dirty page
55aa0 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20  s in the cache, 
55ab0 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 20 6e  sorted by page n
55ac0 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 45 5f  umber */.SQLITE_
55ad0 50 52 49 56 41 54 45 20 50 67 48 64 72 20 2a 73  PRIVATE PgHdr *s
55ae0 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74  qlite3PcacheDirt
55af0 79 4c 69 73 74 28 50 43 61 63 68 65 2a 29 3b 0a  yList(PCache*);.
55b00 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63 6c  ./* Reset and cl
55b10 6f 73 65 20 74 68 65 20 63 61 63 68 65 20 6f 62  ose the cache ob
55b20 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  ject */.SQLITE_P
55b30 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
55b40 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50  te3PcacheClose(P
55b50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c 65  Cache*);../* Cle
55b60 61 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 70 61  ar flags from pa
55b70 67 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20  ges of the page 
55b80 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f  cache */.SQLITE_
55b90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
55ba0 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53  ite3PcacheClearS
55bb0 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65 20  yncFlags(PCache 
55bc0 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 72 64 20  *);../* Discard 
55bd0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
55be0 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c  the cache */.SQL
55bf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
55c00 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c   sqlite3PcacheCl
55c10 65 61 72 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f  ear(PCache*);../
55c20 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74  * Return the tot
55c30 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74  al number of out
55c40 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65  standing page re
55c50 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 51 4c 49  ferences */.SQLI
55c60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
55c70 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
55c80 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a  ount(PCache*);..
55c90 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  /* Increment the
55ca0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
55cb0 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   of an existing 
55cc0 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  page */.SQLITE_P
55cd0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
55ce0 74 65 33 50 63 61 63 68 65 52 65 66 28 50 67 48  te3PcacheRef(PgH
55cf0 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  dr*);..SQLITE_PR
55d00 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
55d10 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f  3PcachePageRefco
55d20 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a 0a 2f 2a  unt(PgHdr*);../*
55d30 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61   Return the tota
55d40 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
55d50 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
55d60 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f  cache */.SQLITE_
55d70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
55d80 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75  te3PcachePagecou
55d90 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 23 69  nt(PCache*);..#i
55da0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
55db0 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20 7c 7c  _CHECK_PAGES) ||
55dc0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
55dd0 44 45 42 55 47 29 0a 2f 2a 20 49 74 65 72 61 74  DEBUG)./* Iterat
55de0 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 64 69  e through all di
55df0 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65 6e  rty pages curren
55e00 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 74 68  tly stored in th
55e10 65 20 63 61 63 68 65 2e 20 54 68 69 73 0a 2a 2a  e cache. This.**
55e20 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f 6e   interface is on
55e30 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20  ly available if 
55e40 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
55e50 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 77 68  ES is defined wh
55e60 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 62 72 61  en the .** libra
55e70 72 79 20 69 73 20 62 75 69 6c 74 2e 0a 2a 2f 0a  ry is built..*/.
55e80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
55e90 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
55ea0 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50 43  eIterateDirty(PC
55eb0 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76 6f  ache *pCache, vo
55ec0 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48 64  id (*xIter)(PgHd
55ed0 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f  r *));.#endif../
55ee0 2a 20 53 65 74 20 61 6e 64 20 67 65 74 20 74 68  * Set and get th
55ef0 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63 68  e suggested cach
55f00 65 2d 73 69 7a 65 20 66 6f 72 20 74 68 65 20 73  e-size for the s
55f10 70 65 63 69 66 69 65 64 20 70 61 67 65 72 2d 63  pecified pager-c
55f20 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  ache..**.** If n
55f30 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d  o global maximum
55f40 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 2c 20   is configured, 
55f50 74 68 65 6e 20 74 68 65 20 73 79 73 74 65 6d 20  then the system 
55f60 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 69 6d 69  attempts to limi
55f70 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20 6e  t.** the total n
55f80 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63  umber of pages c
55f90 61 63 68 65 64 20 62 79 20 70 75 72 67 65 61 62  ached by purgeab
55fa0 6c 65 20 70 61 67 65 72 2d 63 61 63 68 65 73 20  le pager-caches 
55fb0 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66  to the sum.** of
55fc0 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63   the suggested c
55fd0 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a 2f 0a 53  ache-sizes..*/.S
55fe0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
55ff0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
56000 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61  SetCachesize(PCa
56010 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 69 66  che *, int);.#if
56020 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
56030 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
56040 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
56050 47 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61  GetCachesize(PCa
56060 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  che *);.#endif..
56070 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
56080 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41  ABLE_MEMORY_MANA
56090 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 74 6f  GEMENT./* Try to
560a0 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 20 75   return memory u
560b0 73 65 64 20 62 79 20 74 68 65 20 70 63 61 63 68  sed by the pcach
560c0 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 65 20  e module to the 
560d0 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 61 70  main memory heap
560e0 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
560f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
56100 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72  acheReleaseMemor
56110 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 0a  y(int);.#endif..
56120 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
56130 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
56140 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
56150 61 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c 69  acheStats(int*,i
56160 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a  nt*,int*,int*);.
56170 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
56180 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
56190 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61  te3PCacheSetDefa
561a0 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 65 6e 64  ult(void);..#end
561b0 69 66 20 2f 2a 20 5f 50 43 41 43 48 45 5f 48 5f  if /* _PCACHE_H_
561c0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
561d0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61 63  **** End of pcac
561e0 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  he.h ***********
561f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56210 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
56220 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
56230 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
56240 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68  f in sqliteInt.h
56250 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
56260 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/../*********
56270 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73  ***** Include os
56280 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
56290 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
562a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562b0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
562c0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
562d0 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   os.h **********
562e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56300 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
56310 20 53 65 70 74 65 6d 62 65 72 20 31 36 0a 2a 2a   September 16.**
56320 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
56330 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
56340 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
56350 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
56360 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
56370 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
56380 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
56390 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
563a0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
563b0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
563c0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
563d0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
563e0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
563f0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
56400 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
56410 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
56420 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
56430 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
56440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56480 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  *.**.** This hea
56490 64 65 72 20 66 69 6c 65 20 28 74 6f 67 65 74 68  der file (togeth
564a0 65 72 20 77 69 74 68 20 69 73 20 63 6f 6d 70 61  er with is compa
564b0 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65 2d 63 6f  nion C source-co
564c0 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e 63  de file.** "os.c
564d0 22 29 20 61 74 74 65 6d 70 74 20 74 6f 20 61 62  ") attempt to ab
564e0 73 74 72 61 63 74 20 74 68 65 20 75 6e 64 65 72  stract the under
564f0 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20  lying operating 
56500 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a 2a  system so that.*
56510 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  * the SQLite lib
56520 72 61 72 79 20 77 69 6c 6c 20 77 6f 72 6b 20 6f  rary will work o
56530 6e 20 62 6f 74 68 20 50 4f 53 49 58 20 61 6e 64  n both POSIX and
56540 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73   windows systems
56550 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61  ..**.** This hea
56560 64 65 72 20 66 69 6c 65 20 69 73 20 23 69 6e 63  der file is #inc
56570 6c 75 64 65 2d 65 64 20 62 79 20 73 71 6c 69 74  lude-ed by sqlit
56580 65 49 6e 74 2e 68 20 61 6e 64 20 74 68 75 73 20  eInt.h and thus 
56590 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65 69 6e 67  ends up.** being
565a0 20 69 6e 63 6c 75 64 65 64 20 62 79 20 65 76 65   included by eve
565b0 72 79 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a  ry source file..
565c0 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49  */.#ifndef _SQLI
565d0 54 45 5f 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65  TE_OS_H_.#define
565e0 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a   _SQLITE_OS_H_..
565f0 2f 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74  /*.** Figure out
56600 20 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69   if we are deali
56610 6e 67 20 77 69 74 68 20 55 6e 69 78 2c 20 57 69  ng with Unix, Wi
56620 6e 64 6f 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f  ndows, or some o
56630 74 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e  ther.** operatin
56640 67 20 73 79 73 74 65 6d 2e 20 20 41 66 74 65 72  g system.  After
56650 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   the following b
56660 6c 6f 63 6b 20 6f 66 20 70 72 65 70 72 6f 63 65  lock of preproce
56670 73 73 20 6d 61 63 72 6f 73 2c 0a 2a 2a 20 61 6c  ss macros,.** al
56680 6c 20 6f 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55  l of SQLITE_OS_U
56690 4e 49 58 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 57  NIX, SQLITE_OS_W
566a0 49 4e 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53  IN, SQLITE_OS_OS
566b0 32 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 53  2, and SQLITE_OS
566c0 5f 4f 54 48 45 52 20 0a 2a 2a 20 77 69 6c 6c 20  _OTHER .** will 
566d0 64 65 66 69 6e 65 64 20 74 6f 20 65 69 74 68 65  defined to eithe
566e0 72 20 31 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f  r 1 or 0.  One o
566f0 66 20 74 68 65 20 66 6f 75 72 20 77 69 6c 6c 20  f the four will 
56700 62 65 20 31 2e 20 20 54 68 65 20 6f 74 68 65 72  be 1.  The other
56710 20 0a 2a 2a 20 74 68 72 65 65 20 77 69 6c 6c 20   .** three will 
56720 62 65 20 30 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  be 0..*/.#if def
56730 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f  ined(SQLITE_OS_O
56740 54 48 45 52 29 0a 23 20 69 66 20 53 51 4c 49 54  THER).# if SQLIT
56750 45 5f 4f 53 5f 4f 54 48 45 52 3d 3d 31 0a 23 20  E_OS_OTHER==1.# 
56760 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f    undef SQLITE_O
56770 53 5f 55 4e 49 58 0a 23 20 20 20 64 65 66 69 6e  S_UNIX.#   defin
56780 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  e SQLITE_OS_UNIX
56790 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c   0.#   undef SQL
567a0 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 64  ITE_OS_WIN.#   d
567b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
567c0 57 49 4e 20 30 0a 23 20 20 20 75 6e 64 65 66 20  WIN 0.#   undef 
567d0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20  SQLITE_OS_OS2.# 
567e0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
567f0 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a  OS_OS2 0.# else.
56800 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45  #   undef SQLITE
56810 5f 4f 53 5f 4f 54 48 45 52 0a 23 20 65 6e 64 69  _OS_OTHER.# endi
56820 66 0a 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65  f.#endif.#if !de
56830 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f  fined(SQLITE_OS_
56840 55 4e 49 58 29 20 26 26 20 21 64 65 66 69 6e 65  UNIX) && !define
56850 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45  d(SQLITE_OS_OTHE
56860 52 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  R).# define SQLI
56870 54 45 5f 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20  TE_OS_OTHER 0.# 
56880 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53  ifndef SQLITE_OS
56890 5f 57 49 4e 0a 23 20 20 20 69 66 20 64 65 66 69  _WIN.#   if defi
568a0 6e 65 64 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64  ned(_WIN32) || d
568b0 65 66 69 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c  efined(WIN32) ||
568c0 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49   defined(__CYGWI
568d0 4e 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  N__) || defined(
568e0 5f 5f 4d 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20  __MINGW32__) || 
568f0 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e  defined(__BORLAN
56900 44 43 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69  DC__).#     defi
56910 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  ne SQLITE_OS_WIN
56920 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20   1.#     define 
56930 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30  SQLITE_OS_UNIX 0
56940 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51  .#     define SQ
56950 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20  LITE_OS_OS2 0.# 
56960 20 20 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f    elif defined(_
56970 5f 45 4d 58 5f 5f 29 20 7c 7c 20 64 65 66 69 6e  _EMX__) || defin
56980 65 64 28 5f 4f 53 32 29 20 7c 7c 20 64 65 66 69  ed(_OS2) || defi
56990 6e 65 64 28 4f 53 32 29 20 7c 7c 20 64 65 66 69  ned(OS2) || defi
569a0 6e 65 64 28 5f 4f 53 32 5f 29 20 7c 7c 20 64 65  ned(_OS2_) || de
569b0 66 69 6e 65 64 28 5f 5f 4f 53 32 5f 5f 29 0a 23  fined(__OS2__).#
569c0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
569d0 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20  TE_OS_WIN 0.#   
569e0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
569f0 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20  OS_UNIX 0.#     
56a00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
56a10 5f 4f 53 32 20 31 0a 23 20 20 20 65 6c 73 65 0a  _OS2 1.#   else.
56a20 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  #     define SQL
56a30 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20  ITE_OS_WIN 0.#  
56a40 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
56a50 5f 4f 53 5f 55 4e 49 58 20 31 0a 23 20 20 20 20  _OS_UNIX 1.#    
56a60 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
56a70 53 5f 4f 53 32 20 30 0a 23 20 20 65 6e 64 69 66  S_OS2 0.#  endif
56a80 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  .# else.#  defin
56a90 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  e SQLITE_OS_UNIX
56aa0 20 30 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c   0.#  define SQL
56ab0 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65  ITE_OS_OS2 0.# e
56ac0 6e 64 69 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e  ndif.#else.# ifn
56ad0 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  def SQLITE_OS_WI
56ae0 4e 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49  N.#  define SQLI
56af0 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e  TE_OS_WIN 0.# en
56b00 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  dif.#endif../*.*
56b10 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77  * Determine if w
56b20 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
56b30 74 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77  th WindowsCE - w
56b40 68 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a  hich has a much.
56b50 2a 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a  ** reduced API..
56b60 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  */.#if defined(_
56b70 57 49 4e 33 32 5f 57 43 45 29 0a 23 20 64 65 66  WIN32_WCE).# def
56b80 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ine SQLITE_OS_WI
56b90 4e 43 45 20 31 0a 23 65 6c 73 65 0a 23 20 64 65  NCE 1.#else.# de
56ba0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
56bb0 49 4e 43 45 20 30 0a 23 65 6e 64 69 66 0a 0a 0a  INCE 0.#endif...
56bc0 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65  /*.** Define the
56bd0 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66   maximum size of
56be0 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
56bf0 65 6e 61 6d 65 0a 2a 2f 0a 23 69 66 20 53 51 4c  ename.*/.#if SQL
56c00 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 69 6e 63  ITE_OS_WIN.# inc
56c10 6c 75 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e  lude <windows.h>
56c20 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
56c30 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28  _TEMPNAME_SIZE (
56c40 4d 41 58 5f 50 41 54 48 2b 35 30 29 0a 23 65 6c  MAX_PATH+50).#el
56c50 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32  if SQLITE_OS_OS2
56c60 0a 23 20 69 66 20 28 5f 5f 47 4e 55 43 5f 5f 20  .# if (__GNUC__ 
56c70 3e 20 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20  > 3 || __GNUC__ 
56c80 3d 3d 20 33 20 26 26 20 5f 5f 47 4e 55 43 5f 4d  == 3 && __GNUC_M
56c90 49 4e 4f 52 5f 5f 20 3e 3d 20 33 29 20 26 26 20  INOR__ >= 3) && 
56ca0 64 65 66 69 6e 65 64 28 4f 53 32 5f 48 49 47 48  defined(OS2_HIGH
56cb0 5f 4d 45 4d 4f 52 59 29 0a 23 20 20 69 6e 63 6c  _MEMORY).#  incl
56cc0 75 64 65 20 3c 6f 73 32 73 61 66 65 2e 68 3e 20  ude <os2safe.h> 
56cd0 2f 2a 20 68 61 73 20 74 6f 20 62 65 20 69 6e 63  /* has to be inc
56ce0 6c 75 64 65 64 20 62 65 66 6f 72 65 20 6f 73 32  luded before os2
56cf0 2e 68 20 66 6f 72 20 6c 69 6e 6b 69 6e 67 20 74  .h for linking t
56d00 6f 20 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64 69  o work */.# endi
56d10 66 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f  f.# define INCL_
56d20 44 4f 53 44 41 54 45 54 49 4d 45 0a 23 20 64 65  DOSDATETIME.# de
56d30 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 46 49 4c  fine INCL_DOSFIL
56d40 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e  EMGR.# define IN
56d50 43 4c 5f 44 4f 53 45 52 52 4f 52 53 0a 23 20 64  CL_DOSERRORS.# d
56d60 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 49  efine INCL_DOSMI
56d70 53 43 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c  SC.# define INCL
56d80 5f 44 4f 53 50 52 4f 43 45 53 53 0a 23 20 64 65  _DOSPROCESS.# de
56d90 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44  fine INCL_DOSMOD
56da0 55 4c 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20  ULEMGR.# define 
56db0 49 4e 43 4c 5f 44 4f 53 53 45 4d 41 50 48 4f 52  INCL_DOSSEMAPHOR
56dc0 45 53 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73  ES.# include <os
56dd0 32 2e 68 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c  2.h>.# include <
56de0 75 63 6f 6e 76 2e 68 3e 0a 23 20 64 65 66 69 6e  uconv.h>.# defin
56df0 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d  e SQLITE_TEMPNAM
56e00 45 5f 53 49 5a 45 20 28 43 43 48 4d 41 58 50 41  E_SIZE (CCHMAXPA
56e10 54 48 43 4f 4d 50 29 0a 23 65 6c 73 65 0a 23 20  THCOMP).#else.# 
56e20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
56e30 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 32 30 30 0a  MPNAME_SIZE 200.
56e40 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 66 20 74 68  #endif../* If th
56e50 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d  e SET_FULLSYNC m
56e60 61 63 72 6f 20 69 73 20 6e 6f 74 20 64 65 66 69  acro is not defi
56e70 6e 65 64 20 61 62 6f 76 65 2c 20 74 68 65 6e 20  ned above, then 
56e80 6d 61 6b 65 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d  make it.** a no-
56e90 6f 70 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 45  op.*/.#ifndef SE
56ea0 54 5f 46 55 4c 4c 53 59 4e 43 0a 23 20 64 65 66  T_FULLSYNC.# def
56eb0 69 6e 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43  ine SET_FULLSYNC
56ec0 28 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  (x,y).#endif../*
56ed0 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20  .** The default 
56ee0 73 69 7a 65 20 6f 66 20 61 20 64 69 73 6b 20 73  size of a disk s
56ef0 65 63 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66  ector.*/.#ifndef
56f00 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
56f10 53 45 43 54 4f 52 5f 53 49 5a 45 0a 23 20 64 65  SECTOR_SIZE.# de
56f20 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
56f30 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20  ULT_SECTOR_SIZE 
56f40 35 31 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  512.#endif../*.*
56f50 2a 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  * Temporary file
56f60 73 20 61 72 65 20 6e 61 6d 65 64 20 73 74 61 72  s are named star
56f70 74 69 6e 67 20 77 69 74 68 20 74 68 69 73 20 70  ting with this p
56f80 72 65 66 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62  refix followed b
56f90 79 20 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61  y 16 random.** a
56fa0 6c 70 68 61 6e 75 6d 65 72 69 63 20 63 68 61 72  lphanumeric char
56fb0 61 63 74 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66  acters, and no f
56fc0 69 6c 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54  ile extension. T
56fd0 68 65 79 20 61 72 65 20 73 74 6f 72 65 64 20 69  hey are stored i
56fe0 6e 20 74 68 65 0a 2a 2a 20 4f 53 27 73 20 73 74  n the.** OS's st
56ff0 61 6e 64 61 72 64 20 74 65 6d 70 6f 72 61 72 79  andard temporary
57000 20 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2c   file directory,
57010 20 61 6e 64 20 61 72 65 20 64 65 6c 65 74 65 64   and are deleted
57020 20 70 72 69 6f 72 20 74 6f 20 65 78 69 74 2e 0a   prior to exit..
57030 2a 2a 20 49 66 20 73 71 6c 69 74 65 20 69 73 20  ** If sqlite is 
57040 62 65 69 6e 67 20 65 6d 62 65 64 64 65 64 20 69  being embedded i
57050 6e 20 61 6e 6f 74 68 65 72 20 70 72 6f 67 72 61  n another progra
57060 6d 2c 20 79 6f 75 20 6d 61 79 20 77 69 73 68 20  m, you may wish 
57070 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a  to change the.**
57080 20 70 72 65 66 69 78 20 74 6f 20 72 65 66 6c 65   prefix to refle
57090 63 74 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 27  ct your program'
570a0 73 20 6e 61 6d 65 2c 20 73 6f 20 74 68 61 74 20  s name, so that 
570b0 69 66 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20  if your program 
570c0 65 78 69 74 73 0a 2a 2a 20 70 72 65 6d 61 74 75  exits.** prematu
570d0 72 65 6c 79 2c 20 6f 6c 64 20 74 65 6d 70 6f 72  rely, old tempor
570e0 61 72 79 20 66 69 6c 65 73 20 63 61 6e 20 62 65  ary files can be
570f0 20 65 61 73 69 6c 79 20 69 64 65 6e 74 69 66 69   easily identifi
57100 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20  ed. This can be 
57110 64 6f 6e 65 0a 2a 2a 20 75 73 69 6e 67 20 2d 44  done.** using -D
57120 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45  SQLITE_TEMP_FILE
57130 5f 50 52 45 46 49 58 3d 6d 79 70 72 65 66 69 78  _PREFIX=myprefix
57140 5f 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  _ on the compile
57150 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 0a  r command line..
57160 2a 2a 0a 2a 2a 20 32 30 30 36 2d 31 30 2d 33 31  **.** 2006-10-31
57170 3a 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 70  :  The default p
57180 72 65 66 69 78 20 75 73 65 64 20 74 6f 20 62 65  refix used to be
57190 20 22 73 71 6c 69 74 65 5f 22 2e 20 20 42 75 74   "sqlite_".  But
571a0 20 74 68 65 6e 0a 2a 2a 20 4d 63 61 66 65 65 20   then.** Mcafee 
571b0 73 74 61 72 74 65 64 20 75 73 69 6e 67 20 53 51  started using SQ
571c0 4c 69 74 65 20 69 6e 20 74 68 65 69 72 20 61 6e  Lite in their an
571d0 74 69 2d 76 69 72 75 73 20 70 72 6f 64 75 63 74  ti-virus product
571e0 20 61 6e 64 20 69 74 0a 2a 2a 20 73 74 61 72 74   and it.** start
571f0 65 64 20 70 75 74 74 69 6e 67 20 66 69 6c 65 73  ed putting files
57200 20 77 69 74 68 20 74 68 65 20 22 73 71 6c 69 74   with the "sqlit
57210 65 22 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 63  e" name in the c
57220 3a 2f 74 65 6d 70 20 66 6f 6c 64 65 72 2e 0a 2a  :/temp folder..*
57230 2a 20 54 68 69 73 20 61 6e 6e 6f 79 65 64 20 6d  * This annoyed m
57240 61 6e 79 20 77 69 6e 64 6f 77 73 20 75 73 65 72  any windows user
57250 73 2e 20 20 54 68 6f 73 65 20 75 73 65 72 73 20  s.  Those users 
57260 77 6f 75 6c 64 20 74 68 65 6e 20 64 6f 20 61 20  would then do a 
57270 0a 2a 2a 20 47 6f 6f 67 6c 65 20 73 65 61 72 63  .** Google searc
57280 68 20 66 6f 72 20 22 73 71 6c 69 74 65 22 2c 20  h for "sqlite", 
57290 66 69 6e 64 20 74 68 65 20 74 65 6c 65 70 68 6f  find the telepho
572a0 6e 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 74 68  ne numbers of th
572b0 65 0a 2a 2a 20 64 65 76 65 6c 6f 70 65 72 73 20  e.** developers 
572c0 61 6e 64 20 63 61 6c 6c 20 74 6f 20 77 61 6b 65  and call to wake
572d0 20 74 68 65 6d 20 75 70 20 61 74 20 6e 69 67 68   them up at nigh
572e0 74 20 61 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e 0a  t and complain..
572f0 2a 2a 20 46 6f 72 20 74 68 69 73 20 72 65 61 73  ** For this reas
57300 6f 6e 2c 20 74 68 65 20 64 65 66 61 75 6c 74 20  on, the default 
57310 6e 61 6d 65 20 70 72 65 66 69 78 20 69 73 20 63  name prefix is c
57320 68 61 6e 67 65 64 20 74 6f 20 62 65 20 22 73 71  hanged to be "sq
57330 6c 69 74 65 22 20 0a 2a 2a 20 73 70 65 6c 6c 65  lite" .** spelle
57340 64 20 62 61 63 6b 77 61 72 64 73 2e 20 20 53 6f  d backwards.  So
57350 20 74 68 65 20 74 65 6d 70 20 66 69 6c 65 73 20   the temp files 
57360 61 72 65 20 73 74 69 6c 6c 20 69 64 65 6e 74 69  are still identi
57370 66 69 65 64 2c 20 62 75 74 0a 2a 2a 20 61 6e 79  fied, but.** any
57380 62 6f 64 79 20 73 6d 61 72 74 20 65 6e 6f 75 67  body smart enoug
57390 68 20 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20  h to figure out 
573a0 74 68 65 20 63 6f 64 65 20 69 73 20 61 6c 73 6f  the code is also
573b0 20 6c 69 6b 65 6c 79 20 73 6d 61 72 74 0a 2a 2a   likely smart.**
573c0 20 65 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20   enough to know 
573d0 74 68 61 74 20 63 61 6c 6c 69 6e 67 20 74 68 65  that calling the
573e0 20 64 65 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20   developer will 
573f0 6e 6f 74 20 68 65 6c 70 20 67 65 74 20 72 69 64  not help get rid
57400 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 2e  .** of the file.
57410 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
57420 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45  TE_TEMP_FILE_PRE
57430 46 49 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  FIX.# define SQL
57440 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52  ITE_TEMP_FILE_PR
57450 45 46 49 58 20 22 65 74 69 6c 71 73 5f 22 0a 23  EFIX "etilqs_".#
57460 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
57470 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65   following value
57480 73 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 20  s may be passed 
57490 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
574a0 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c  gument to.** sql
574b0 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68  ite3OsLock(). Th
574c0 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20  e various locks 
574d0 65 78 68 69 62 69 74 20 74 68 65 20 66 6f 6c 6c  exhibit the foll
574e0 6f 77 69 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a  owing semantics:
574f0 0a 2a 2a 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20  .**.** SHARED:  
57500 20 20 41 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20    Any number of 
57510 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f  processes may ho
57520 6c 64 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  ld a SHARED lock
57530 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e   simultaneously.
57540 0a 2a 2a 20 52 45 53 45 52 56 45 44 3a 20 20 41  .** RESERVED:  A
57550 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20   single process 
57560 6d 61 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52  may hold a RESER
57570 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69  VED lock on a fi
57580 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20  le at.**        
57590 20 20 20 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74      any time. Ot
575a0 68 65 72 20 70 72 6f 63 65 73 73 65 73 20 6d 61  her processes ma
575b0 79 20 68 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69  y hold and obtai
575c0 6e 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63  n new SHARED loc
575d0 6b 73 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20  ks..** PENDING: 
575e0 20 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65    A single proce
575f0 73 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 50 45  ss may hold a PE
57600 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20  NDING lock on a 
57610 66 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20  file at.**      
57620 20 20 20 20 20 20 61 6e 79 20 6f 6e 65 20 74 69        any one ti
57630 6d 65 2e 20 45 78 69 73 74 69 6e 67 20 53 48 41  me. Existing SHA
57640 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65  RED locks may pe
57650 72 73 69 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65  rsist, but no ne
57660 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  w.**            
57670 53 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79  SHARED locks may
57680 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20   be obtained by 
57690 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e  other processes.
576a0 0a 2a 2a 20 45 58 43 4c 55 53 49 56 45 3a 20 41  .** EXCLUSIVE: A
576b0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
576c0 20 70 72 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f   precludes all o
576d0 74 68 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a  ther locks..**.*
576e0 2a 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d  * PENDING_LOCK m
576f0 61 79 20 6e 6f 74 20 62 65 20 70 61 73 73 65 64  ay not be passed
57700 20 64 69 72 65 63 74 6c 79 20 74 6f 20 73 71 6c   directly to sql
57710 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e  ite3OsLock(). In
57720 73 74 65 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63  stead, a.** proc
57730 65 73 73 20 74 68 61 74 20 72 65 71 75 65 73 74  ess that request
57740 73 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  s an EXCLUSIVE l
57750 6f 63 6b 20 6d 61 79 20 61 63 74 75 61 6c 6c 79  ock may actually
57760 20 6f 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e   obtain a PENDIN
57770 47 0a 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20  G.** lock. This 
57780 63 61 6e 20 62 65 20 75 70 67 72 61 64 65 64 20  can be upgraded 
57790 74 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20  to an EXCLUSIVE 
577a0 6c 6f 63 6b 20 62 79 20 61 20 73 75 62 73 65 71  lock by a subseq
577b0 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  uent call to.** 
577c0 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e  sqlite3OsLock().
577d0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c  .*/.#define NO_L
577e0 4f 43 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64  OCK         0.#d
577f0 65 66 69 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43  efine SHARED_LOC
57800 4b 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20  K     1.#define 
57810 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20  RESERVED_LOCK   
57820 32 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e  2.#define PENDIN
57830 47 5f 4c 4f 43 4b 20 20 20 20 33 0a 23 64 65 66  G_LOCK    3.#def
57840 69 6e 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  ine EXCLUSIVE_LO
57850 43 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c  CK  4../*.** Fil
57860 65 20 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a  e Locking Notes:
57870 20 20 28 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20    (Mostly about 
57880 77 69 6e 64 6f 77 73 20 62 75 74 20 61 6c 73 6f  windows but also
57890 20 73 6f 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55   some info for U
578a0 6e 69 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61  nix).**.** We ca
578b0 6e 6e 6f 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c  nnot use LockFil
578c0 65 45 78 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46  eEx() or UnlockF
578d0 69 6c 65 45 78 28 29 20 6f 6e 20 57 69 6e 39 35  ileEx() on Win95
578e0 2f 39 38 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a  /98/ME because.*
578f0 2a 20 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e  * those function
57900 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61  s are not availa
57910 62 6c 65 2e 20 20 53 6f 20 77 65 20 75 73 65 20  ble.  So we use 
57920 6f 6e 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20  only LockFile() 
57930 61 6e 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c  and.** UnlockFil
57940 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46  e()..**.** LockF
57950 69 6c 65 28 29 20 70 72 65 76 65 6e 74 73 20 6e  ile() prevents n
57960 6f 74 20 6a 75 73 74 20 77 72 69 74 69 6e 67 20  ot just writing 
57970 62 75 74 20 61 6c 73 6f 20 72 65 61 64 69 6e 67  but also reading
57980 20 62 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73   by other proces
57990 73 65 73 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44  ses..** A SHARED
579a0 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65  _LOCK is obtaine
579b0 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73  d by locking a s
579c0 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63  ingle randomly-c
579d0 68 6f 73 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f  hosen .** byte o
579e0 75 74 20 6f 66 20 61 20 73 70 65 63 69 66 69 63  ut of a specific
579f0 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 2e   range of bytes.
57a00 20 54 68 65 20 6c 6f 63 6b 20 62 79 74 65 20 69   The lock byte i
57a10 73 20 6f 62 74 61 69 6e 65 64 20 61 74 20 0a 2a  s obtained at .*
57a20 2a 20 72 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20  * random so two 
57a30 73 65 70 61 72 61 74 65 20 72 65 61 64 65 72 73  separate readers
57a40 20 63 61 6e 20 70 72 6f 62 61 62 6c 79 20 61 63   can probably ac
57a50 63 65 73 73 20 74 68 65 20 66 69 6c 65 20 61 74  cess the file at
57a60 20 74 68 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69   the .** same ti
57a70 6d 65 2c 20 75 6e 6c 65 73 73 20 74 68 65 79 20  me, unless they 
57a80 61 72 65 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20  are unlucky and 
57a90 63 68 6f 6f 73 65 20 74 68 65 20 73 61 6d 65 20  choose the same 
57aa0 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e  lock byte..** An
57ab0 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20   EXCLUSIVE_LOCK 
57ac0 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c  is obtained by l
57ad0 6f 63 6b 69 6e 67 20 61 6c 6c 20 62 79 74 65 73  ocking all bytes
57ae0 20 69 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a   in the range..*
57af0 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  * There can only
57b00 20 62 65 20 6f 6e 65 20 77 72 69 74 65 72 2e 20   be one writer. 
57b10 20 41 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b   A RESERVED_LOCK
57b20 20 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20   is obtained by 
57b30 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e  locking.** a sin
57b40 67 6c 65 20 62 79 74 65 20 6f 66 20 74 68 65 20  gle byte of the 
57b50 66 69 6c 65 20 74 68 61 74 20 69 73 20 64 65 73  file that is des
57b60 69 67 6e 61 74 65 64 20 61 73 20 74 68 65 20 72  ignated as the r
57b70 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 62 79 74  eserved lock byt
57b80 65 2e 0a 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f  e..** A PENDING_
57b90 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64  LOCK is obtained
57ba0 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65   by locking a de
57bb0 73 69 67 6e 61 74 65 64 20 62 79 74 65 20 64 69  signated byte di
57bc0 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20  fferent from.** 
57bd0 74 68 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  the RESERVED_LOC
57be0 4b 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  K byte..**.** On
57bf0 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73   WinNT/2K/XP sys
57c00 74 65 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78  tems, LockFileEx
57c10 28 29 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c  () and UnlockFil
57c20 65 45 78 28 29 20 61 72 65 20 61 76 61 69 6c 61  eEx() are availa
57c30 62 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65  ble,.** which me
57c40 61 6e 73 20 77 65 20 63 61 6e 20 75 73 65 20 72  ans we can use r
57c50 65 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63  eader/writer loc
57c60 6b 73 2e 20 20 57 68 65 6e 20 72 65 61 64 65 72  ks.  When reader
57c70 2f 77 72 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a  /writer locks.**
57c80 20 61 72 65 20 75 73 65 64 2c 20 74 68 65 20 6c   are used, the l
57c90 6f 63 6b 20 69 73 20 70 6c 61 63 65 64 20 6f 6e  ock is placed on
57ca0 20 74 68 65 20 73 61 6d 65 20 72 61 6e 67 65 20   the same range 
57cb0 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 69 73  of bytes that is
57cc0 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f   used.** for pro
57cd0 62 61 62 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69  babilistic locki
57ce0 6e 67 20 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d  ng in Win95/98/M
57cf0 45 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 6c  E.  Hence, the l
57d00 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a  ocking scheme.**
57d10 20 77 69 6c 6c 20 73 75 70 70 6f 72 74 20 74 77   will support tw
57d20 6f 20 6f 72 20 6d 6f 72 65 20 57 69 6e 39 35 20  o or more Win95 
57d30 72 65 61 64 65 72 73 20 6f 72 20 74 77 6f 20 6f  readers or two o
57d40 72 20 6d 6f 72 65 20 57 69 6e 4e 54 20 72 65 61  r more WinNT rea
57d50 64 65 72 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73  ders..** But a s
57d60 69 6e 67 6c 65 20 57 69 6e 39 35 20 72 65 61 64  ingle Win95 read
57d70 65 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74  er will lock out
57d80 20 61 6c 6c 20 57 69 6e 4e 54 20 72 65 61 64 65   all WinNT reade
57d90 72 73 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a  rs and a single.
57da0 2a 2a 20 57 69 6e 4e 54 20 72 65 61 64 65 72 20  ** WinNT reader 
57db0 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c  will lock out al
57dc0 6c 20 6f 74 68 65 72 20 57 69 6e 39 35 20 72 65  l other Win95 re
57dd0 61 64 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  aders..**.** The
57de0 20 66 6f 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69   following #defi
57df0 6e 65 73 20 73 70 65 63 69 66 79 20 74 68 65 20  nes specify the 
57e00 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 75  range of bytes u
57e10 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e  sed for locking.
57e20 0a 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 20  .** SHARED_SIZE 
57e30 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
57e40 20 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65   bytes available
57e50 20 69 6e 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f   in the pool fro
57e60 6d 20 77 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e  m which.** a ran
57e70 64 6f 6d 20 62 79 74 65 20 69 73 20 73 65 6c 65  dom byte is sele
57e80 63 74 65 64 20 66 6f 72 20 61 20 73 68 61 72 65  cted for a share
57e90 64 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f  d lock.  The poo
57ea0 6c 20 6f 66 20 62 79 74 65 73 20 66 6f 72 0a 2a  l of bytes for.*
57eb0 2a 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 62  * shared locks b
57ec0 65 67 69 6e 73 20 61 74 20 53 48 41 52 45 44 5f  egins at SHARED_
57ed0 46 49 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  FIRST. .**.** Th
57ee0 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73  e same locking s
57ef0 74 72 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62  trategy and.** b
57f00 79 74 65 20 72 61 6e 67 65 73 20 61 72 65 20 75  yte ranges are u
57f10 73 65 64 20 66 6f 72 20 55 6e 69 78 2e 20 20 54  sed for Unix.  T
57f20 68 69 73 20 6c 65 61 76 65 73 20 6f 70 65 6e 20  his leaves open 
57f30 74 68 65 20 70 6f 73 73 69 62 6c 69 74 79 20 6f  the possiblity o
57f40 66 20 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65  f having.** clie
57f50 6e 74 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69  nts on win95, wi
57f60 6e 4e 54 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c  nNT, and unix al
57f70 6c 20 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65  l talking to the
57f80 20 73 61 6d 65 20 73 68 61 72 65 64 20 66 69 6c   same shared fil
57f90 65 0a 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63  e.** and all loc
57fa0 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20  king correctly. 
57fb0 20 54 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20   To do so would 
57fc0 72 65 71 75 69 72 65 20 74 68 61 74 20 73 61 6d  require that sam
57fd0 62 61 20 28 6f 72 20 77 68 61 74 65 76 65 72 0a  ba (or whatever.
57fe0 2a 2a 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67  ** tool is being
57ff0 20 75 73 65 64 20 66 6f 72 20 66 69 6c 65 20 73   used for file s
58000 68 61 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e  haring) implemen
58010 74 73 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74  ts locks correct
58020 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69  ly between.** wi
58030 6e 64 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e 20  ndows and unix. 
58040 20 49 27 6d 20 67 75 65 73 73 69 6e 67 20 74 68   I'm guessing th
58050 61 74 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20  at isn't likely 
58060 74 6f 20 68 61 70 70 65 6e 2c 20 62 75 74 20 62  to happen, but b
58070 79 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73  y.** using the s
58080 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67  ame locking rang
58090 65 20 77 65 20 61 72 65 20 61 74 20 6c 65 61 73  e we are at leas
580a0 74 20 6f 70 65 6e 20 74 6f 20 74 68 65 20 70 6f  t open to the po
580b0 73 73 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a  ssibility..**.**
580c0 20 4c 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64   Locking in wind
580d0 6f 77 73 20 69 73 20 6d 61 6e 64 69 74 6f 72 79  ows is manditory
580e0 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 73  .  For this reas
580f0 6f 6e 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73 74  on, we cannot st
58100 6f 72 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61  ore.** actual da
58110 74 61 20 69 6e 20 74 68 65 20 62 79 74 65 73 20  ta in the bytes 
58120 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67  used for locking
58130 2e 20 20 54 68 65 20 70 61 67 65 72 20 6e 65 76  .  The pager nev
58140 65 72 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20  er allocates.** 
58150 74 68 65 20 70 61 67 65 73 20 69 6e 76 6f 6c 76  the pages involv
58160 65 64 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68  ed in locking th
58170 65 72 65 66 6f 72 65 2e 20 20 53 48 41 52 45 44  erefore.  SHARED
58180 5f 53 49 5a 45 20 69 73 20 73 65 6c 65 63 74 65  _SIZE is selecte
58190 64 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c  d so.** that all
581a0 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74 20   locks will fit 
581b0 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65  on a single page
581c0 20 65 76 65 6e 20 61 74 20 74 68 65 20 6d 69 6e   even at the min
581d0 69 6d 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a  imum page size..
581e0 2a 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  ** PENDING_BYTE 
581f0 64 65 66 69 6e 65 73 20 74 68 65 20 62 65 67 69  defines the begi
58200 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63  nning of the loc
58210 6b 73 2e 20 20 42 79 20 64 65 66 61 75 6c 74 20  ks.  By default 
58220 50 45 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20  PENDING_BYTE.** 
58230 69 73 20 73 65 74 20 68 69 67 68 20 73 6f 20 74  is set high so t
58240 68 61 74 20 77 65 20 64 6f 6e 27 74 20 68 61 76  hat we don't hav
58250 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e  e to allocate an
58260 20 75 6e 75 73 65 64 20 70 61 67 65 20 65 78 63   unused page exc
58270 65 70 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20  ept.** for very 
58280 6c 61 72 67 65 20 64 61 74 61 62 61 73 65 73 2e  large databases.
58290 20 20 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64    But one should
582a0 20 74 65 73 74 20 74 68 65 20 70 61 67 65 20 73   test the page s
582b0 6b 69 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a  kipping logic .*
582c0 2a 20 62 79 20 73 65 74 74 69 6e 67 20 50 45 4e  * by setting PEN
582d0 44 49 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e  DING_BYTE low an
582e0 64 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e  d running the en
582f0 74 69 72 65 20 72 65 67 72 65 73 73 69 6f 6e 20  tire regression 
58300 73 75 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61  suite..**.** Cha
58310 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20  nging the value 
58320 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  of PENDING_BYTE 
58330 72 65 73 75 6c 74 73 20 69 6e 20 61 20 73 75 62  results in a sub
58340 74 6c 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65  tly incompatible
58350 0a 2a 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e  .** file format.
58360 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68    Depending on h
58370 6f 77 20 69 74 20 69 73 20 63 68 61 6e 67 65 64  ow it is changed
58380 2c 20 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20  , you might not 
58390 6e 6f 74 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e  notice.** the in
583a0 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 72 69  compatibility ri
583b0 67 68 74 20 61 77 61 79 2c 20 65 76 65 6e 20 72  ght away, even r
583c0 75 6e 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 65  unning a full re
583d0 67 72 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a  gression test..*
583e0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f  * The default lo
583f0 63 61 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e  cation of PENDIN
58400 47 5f 42 59 54 45 20 69 73 20 74 68 65 20 66 69  G_BYTE is the fi
58410 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74 68  rst byte past th
58420 65 0a 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 72  e.** 1GB boundar
58430 79 2e 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65  y..**.*/.#define
58440 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 20 20   PENDING_BYTE   
58450 20 20 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e     sqlite3Pendin
58460 67 42 79 74 65 0a 23 64 65 66 69 6e 65 20 52 45  gByte.#define RE
58470 53 45 52 56 45 44 5f 42 59 54 45 20 20 20 20 20  SERVED_BYTE     
58480 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29  (PENDING_BYTE+1)
58490 0a 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f  .#define SHARED_
584a0 46 49 52 53 54 20 20 20 20 20 20 28 50 45 4e 44  FIRST      (PEND
584b0 49 4e 47 5f 42 59 54 45 2b 32 29 0a 23 64 65 66  ING_BYTE+2).#def
584c0 69 6e 65 20 53 48 41 52 45 44 5f 53 49 5a 45 20  ine SHARED_SIZE 
584d0 20 20 20 20 20 20 35 31 30 0a 0a 2f 2a 0a 2a 2a        510../*.**
584e0 20 57 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20   Wrapper around 
584f0 4f 53 20 73 70 65 63 69 66 69 63 20 73 71 6c 69  OS specific sqli
58500 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 66 75  te3_os_init() fu
58510 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  nction..*/.SQLIT
58520 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
58530 6c 69 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64  lite3OsInit(void
58540 29 3b 0a 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74  );../* .** Funct
58550 69 6f 6e 73 20 66 6f 72 20 61 63 63 65 73 73 69  ions for accessi
58560 6e 67 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ng sqlite3_file 
58570 6d 65 74 68 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49  methods .*/.SQLI
58580 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58590 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71  qlite3OsClose(sq
585a0 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a 53 51  lite3_file*);.SQ
585b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
585c0 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73   sqlite3OsRead(s
585d0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 76 6f  qlite3_file*, vo
585e0 69 64 2a 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36  id*, int amt, i6
585f0 34 20 6f 66 66 73 65 74 29 3b 0a 53 51 4c 49 54  4 offset);.SQLIT
58600 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
58610 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73 71 6c  lite3OsWrite(sql
58620 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73  ite3_file*, cons
58630 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61 6d 74  t void*, int amt
58640 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b 0a 53  , i64 offset);.S
58650 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
58660 74 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  t sqlite3OsTrunc
58670 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ate(sqlite3_file
58680 2a 2c 20 69 36 34 20 73 69 7a 65 29 3b 0a 53 51  *, i64 size);.SQ
58690 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
586a0 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73   sqlite3OsSync(s
586b0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
586c0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
586d0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
586e0 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33  FileSize(sqlite3
586f0 5f 66 69 6c 65 2a 2c 20 69 36 34 20 2a 70 53 69  _file*, i64 *pSi
58700 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ze);.SQLITE_PRIV
58710 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
58720 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  sLock(sqlite3_fi
58730 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  le*, int);.SQLIT
58740 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
58750 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 73 71  lite3OsUnlock(sq
58760 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
58770 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
58780 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
58790 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
587a0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
587b0 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29  d, int *pResOut)
587c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
587d0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69   int sqlite3OsFi
587e0 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
587f0 33 5f 66 69 6c 65 2a 2c 69 6e 74 2c 76 6f 69 64  3_file*,int,void
58800 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  *);.#define SQLI
58810 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e 43 48  TE_FCNTL_DB_UNCH
58820 41 4e 47 45 44 20 30 78 63 61 30 39 33 66 61 30  ANGED 0xca093fa0
58830 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
58840 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63  int sqlite3OsSec
58850 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  torSize(sqlite3_
58860 66 69 6c 65 20 2a 69 64 29 3b 0a 53 51 4c 49 54  file *id);.SQLIT
58870 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
58880 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61  lite3OsDeviceCha
58890 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c  racteristics(sql
588a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 3b 0a  ite3_file *id);.
588b0 0a 2f 2a 20 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ./* .** Function
588c0 73 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  s for accessing 
588d0 73 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68  sqlite3_vfs meth
588e0 6f 64 73 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ods .*/.SQLITE_P
588f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58900 65 33 4f 73 4f 70 65 6e 28 73 71 6c 69 74 65 33  e3OsOpen(sqlite3
58910 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68  _vfs *, const ch
58920 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69  ar *, sqlite3_fi
58930 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29  le*, int, int *)
58940 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
58950 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44 65   int sqlite3OsDe
58960 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76 66 73  lete(sqlite3_vfs
58970 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
58980 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
58990 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
589a0 65 33 4f 73 41 63 63 65 73 73 28 73 71 6c 69 74  e3OsAccess(sqlit
589b0 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20  e3_vfs *, const 
589c0 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74  char *, int, int
589d0 20 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49   *pResOut);.SQLI
589e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
589f0 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68  qlite3OsFullPath
58a00 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73  name(sqlite3_vfs
58a10 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
58a20 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b 0a  , int, char *);.
58a30 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
58a40 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
58a50 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ON.SQLITE_PRIVAT
58a60 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f  E void *sqlite3O
58a70 73 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  sDlOpen(sqlite3_
58a80 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  vfs *, const cha
58a90 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r *);.SQLITE_PRI
58aa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
58ab0 33 4f 73 44 6c 45 72 72 6f 72 28 73 71 6c 69 74  3OsDlError(sqlit
58ac0 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74 2c 20 63  e3_vfs *, int, c
58ad0 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  har *);.SQLITE_P
58ae0 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71  RIVATE void (*sq
58af0 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71 6c  lite3OsDlSym(sql
58b00 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f 69 64  ite3_vfs *, void
58b10 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
58b20 29 29 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  ))(void);.SQLITE
58b30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
58b40 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73  lite3OsDlClose(s
58b50 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76 6f  qlite3_vfs *, vo
58b60 69 64 20 2a 29 3b 0a 23 65 6e 64 69 66 20 2f 2a  id *);.#endif /*
58b70 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
58b80 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53  D_EXTENSION */.S
58b90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
58ba0 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f  t sqlite3OsRando
58bb0 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66  mness(sqlite3_vf
58bc0 73 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a  s *, int, char *
58bd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
58be0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 53  E int sqlite3OsS
58bf0 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73  leep(sqlite3_vfs
58c00 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
58c10 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
58c20 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d  ite3OsCurrentTim
58c30 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  e(sqlite3_vfs *,
58c40 20 64 6f 75 62 6c 65 2a 29 3b 0a 0a 2f 2a 0a 2a   double*);../*.*
58c50 2a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 66 75  * Convenience fu
58c60 6e 63 74 69 6f 6e 73 20 66 6f 72 20 6f 70 65 6e  nctions for open
58c70 69 6e 67 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20  ing and closing 
58c80 66 69 6c 65 73 20 75 73 69 6e 67 20 0a 2a 2a 20  files using .** 
58c90 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
58ca0 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 63 65   to obtain space
58cb0 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2d 68 61   for the file-ha
58cc0 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a  ndle structure..
58cd0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
58ce0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f  E int sqlite3OsO
58cf0 70 65 6e 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 65  penMalloc(sqlite
58d00 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63  3_vfs *, const c
58d10 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66  har *, sqlite3_f
58d20 69 6c 65 20 2a 2a 2c 20 69 6e 74 2c 69 6e 74 2a  ile **, int,int*
58d30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
58d40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
58d50 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65 33  loseFree(sqlite3
58d60 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 65 6e 64 69  _file *);..#endi
58d70 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 4f 53 5f  f /* _SQLITE_OS_
58d80 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  H_ */../********
58d90 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
58da0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
58db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58dd0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
58de0 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
58df0 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
58e00 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  off in sqliteInt
58e10 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
58e20 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
58e30 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6d  ****** Include m
58e40 75 74 65 78 2e 68 20 69 6e 20 74 68 65 20 6d 69  utex.h in the mi
58e50 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
58e60 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
58e70 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
58e80 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
58e90 65 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a 2a  e mutex.h ******
58ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58ec0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
58ed0 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a  7 August 28.**.*
58ee0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
58ef0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
58f00 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
58f10 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
58f20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
58f30 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
58f40 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
58f50 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
58f60 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
58f70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
58f80 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
58f90 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
58fa0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
58fb0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
58fc0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
58fd0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
58fe0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
58ff0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
59000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
59040 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
59050 69 6e 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 68  ins the common h
59060 65 61 64 65 72 20 66 6f 72 20 61 6c 6c 20 6d 75  eader for all mu
59070 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
59080 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ons..** The sqli
59090 74 65 49 6e 74 2e 68 20 68 65 61 64 65 72 20 23  teInt.h header #
590a0 69 6e 63 6c 75 64 65 73 20 74 68 69 73 20 66 69  includes this fi
590b0 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  le so that it is
590c0 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f   available.** to
590d0 20 61 6c 6c 20 73 6f 75 72 63 65 20 66 69 6c 65   all source file
590e0 73 2e 20 20 57 65 20 62 72 65 61 6b 20 69 74 20  s.  We break it 
590f0 6f 75 74 20 69 6e 20 61 6e 20 65 66 66 6f 72 74  out in an effort
59100 20 74 6f 20 6b 65 65 70 20 74 68 65 20 63 6f 64   to keep the cod
59110 65 0a 2a 2a 20 62 65 74 74 65 72 20 6f 72 67 61  e.** better orga
59120 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54  nized..**.** NOT
59130 45 3a 20 20 73 6f 75 72 63 65 20 66 69 6c 65 73  E:  source files
59140 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 23 69   should *not* #i
59150 6e 63 6c 75 64 65 20 74 68 69 73 20 68 65 61 64  nclude this head
59160 65 72 20 66 69 6c 65 20 64 69 72 65 63 74 6c 79  er file directly
59170 2e 0a 2a 2a 20 53 6f 75 72 63 65 20 66 69 6c 65  ..** Source file
59180 73 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75 64  s should #includ
59190 65 20 74 68 65 20 73 71 6c 69 74 65 49 6e 74 2e  e the sqliteInt.
591a0 68 20 66 69 6c 65 20 61 6e 64 20 6c 65 74 20 74  h file and let t
591b0 68 61 74 20 66 69 6c 65 0a 2a 2a 20 69 6e 63 6c  hat file.** incl
591c0 75 64 65 20 74 68 69 73 20 6f 6e 65 20 69 6e 64  ude this one ind
591d0 69 72 65 63 74 6c 79 2e 0a 2a 2f 0a 0a 0a 2f 2a  irectly..*/.../*
591e0 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77  .** Figure out w
591f0 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  hat version of t
59200 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20  he code to use. 
59210 20 54 68 65 20 63 68 6f 69 63 65 73 20 61 72 65   The choices are
59220 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  .**.**   SQLITE_
59230 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20  MUTEX_OMIT      
59240 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69     No mutex logi
59250 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75  c.  Not even stu
59260 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20  bs.  The.**     
59270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59280 20 20 20 20 20 20 20 20 6d 75 74 65 78 65 73 20          mutexes 
59290 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e  implemention can
592a0 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64 65  not be overridde
592b0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  n.**            
592c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
592d0 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a   at start-time..
592e0 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  **.**   SQLITE_M
592f0 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20  UTEX_NOOP       
59300 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72    For single-thr
59310 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f  eaded applicatio
59320 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20  ns.  No.**      
59330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59340 20 20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 78         mutual ex
59350 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 69  clusion is provi
59360 64 65 64 2e 20 20 42 75 74 20 74 68 69 73 0a 2a  ded.  But this.*
59370 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
59380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6d                im
59390 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e  plementation can
593a0 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61   be overridden a
593b0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
593c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
593d0 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a   start-time..**.
593e0 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  **   SQLITE_MUTE
593f0 58 5f 50 54 48 52 45 41 44 53 20 20 20 20 20 46  X_PTHREADS     F
59400 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65  or multi-threade
59410 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f  d applications o
59420 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20  n Unix..**.**   
59430 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32  SQLITE_MUTEX_W32
59440 20 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75            For mu
59450 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70  lti-threaded app
59460 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e  lications on Win
59470 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  32..**.**   SQLI
59480 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20  TE_MUTEX_OS2    
59490 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d        For multi-
594a0 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
594b0 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a  tions on OS/2..*
594c0 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48  /.#if !SQLITE_TH
594d0 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e  READSAFE.# defin
594e0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  e SQLITE_MUTEX_O
594f0 4d 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  MIT.#endif.#if S
59500 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
59510 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
59520 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a  ITE_MUTEX_NOOP).
59530 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  #  if SQLITE_OS_
59540 55 4e 49 58 0a 23 20 20 20 20 64 65 66 69 6e 65  UNIX.#    define
59550 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54   SQLITE_MUTEX_PT
59560 48 52 45 41 44 53 0a 23 20 20 65 6c 69 66 20 53  HREADS.#  elif S
59570 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20  QLITE_OS_WIN.#  
59580 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
59590 4d 55 54 45 58 5f 57 33 32 0a 23 20 20 65 6c 69  MUTEX_W32.#  eli
595a0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a  f SQLITE_OS_OS2.
595b0 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  #    define SQLI
595c0 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20  TE_MUTEX_OS2.#  
595d0 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65  else.#    define
595e0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f   SQLITE_MUTEX_NO
595f0 4f 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64  OP.#  endif.#end
59600 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
59610 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a  E_MUTEX_OMIT./*.
59620 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  ** If this is a 
59630 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61  no-op implementa
59640 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20  tion, implement 
59650 65 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61  everything as ma
59660 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  cros..*/.#define
59670 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
59680 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c  lloc(X)    ((sql
59690 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23  ite3_mutex*)8).#
596a0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
596b0 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65  utex_free(X).#de
596c0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
596d0 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66  ex_enter(X).#def
596e0 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
596f0 78 5f 74 72 79 28 58 29 20 20 20 20 20 20 53 51  x_try(X)      SQ
59700 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20  LITE_OK.#define 
59710 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
59720 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73  ave(X).#define s
59730 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
59740 64 28 58 29 20 20 20 20 20 31 0a 23 64 65 66 69  d(X)     1.#defi
59750 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
59760 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23  _notheld(X)  1.#
59770 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75  define sqlite3Mu
59780 74 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 20  texAlloc(X)     
59790 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78   ((sqlite3_mutex
597a0 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c  *)8).#define sql
597b0 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 20  ite3MutexInit() 
597c0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b         SQLITE_OK
597d0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
597e0 4d 75 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 69  MutexEnd().#endi
597f0 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c  f /* defined(SQL
59800 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 20  ITE_MUTEX_OMIT) 
59810 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
59820 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78  *** End of mutex
59830 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
59840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59860 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
59870 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
59880 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
59890 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
598a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
598b0 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  **/.../*.** Each
598c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
598d0 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79  o be accessed by
598e0 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 61   the system is a
598f0 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66  n instance.** of
59900 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
59910 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65  tructure.  There
59920 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77   are normally tw
59930 6f 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63  o of these struc
59940 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20  tures.** in the 
59950 73 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 72  sqlite.aDb[] arr
59960 61 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 74  ay.  aDb[0] is t
59970 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
59980 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62   file and.** aDb
59990 5b 31 5d 20 69 73 20 74 68 65 20 64 61 74 61 62  [1] is the datab
599a0 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f  ase file used to
599b0 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20   hold temporary 
599c0 74 61 62 6c 65 73 2e 20 20 41 64 64 69 74 69 6f  tables.  Additio
599d0 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 73  nal.** databases
599e0 20 6d 61 79 20 62 65 20 61 74 74 61 63 68 65 64   may be attached
599f0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 7b  ..*/.struct Db {
59a00 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
59a10 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
59a20 6f 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65  of this database
59a30 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74   */.  Btree *pBt
59a40 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
59a50 65 20 42 2a 54 72 65 65 20 73 74 72 75 63 74 75  e B*Tree structu
59a60 72 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 61  re for this data
59a70 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75  base file */.  u
59a80 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20  8 inTrans;      
59a90 20 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72      /* 0: not wr
59aa0 69 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e  itable.  1: Tran
59ab0 73 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65  saction.  2: Che
59ac0 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20  ckpoint */.  u8 
59ad0 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20  safety_level;   
59ae0 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73    /* How aggress
59af0 69 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64  ive at syncing d
59b00 61 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20  ata to disk */. 
59b10 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
59b20 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  ;     /* Pointer
59b30 20 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68   to database sch
59b40 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68  ema (possibly sh
59b50 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ared) */.};../*.
59b60 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
59b70 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
59b80 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73  structure stores
59b90 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65   a database sche
59ba0 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ma..**.** If the
59bb0 72 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61  re are no virtua
59bc0 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75  l tables configu
59bd0 72 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65  red in this sche
59be0 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d  ma, the.** Schem
59bf0 61 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73  a.db variable is
59c00 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66   set to NULL. Af
59c10 74 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69  ter the first vi
59c20 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68  rtual table.** h
59c30 61 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69  as been added, i
59c40 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e  t is set to poin
59c50 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
59c60 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a  e connection .**
59c70 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
59c80 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  the connection. 
59c90 4f 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74  Once a virtual t
59ca0 61 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a  able has been.**
59cb0 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63   added to the Sc
59cc0 68 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61  hema structure a
59cd0 6e 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62  nd the Schema.db
59ce0 20 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61   variable popula
59cf0 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68  ted, .** only th
59d00 61 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  at database conn
59d10 65 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74  ection may use t
59d20 68 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65  he Schema to pre
59d30 70 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65  pare .** stateme
59d40 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53  nts..*/.struct S
59d50 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63  chema {.  int sc
59d60 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f  hema_cookie;   /
59d70 2a 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d  * Database schem
59d80 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72  a version number
59d90 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a   for this file *
59da0 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68  /.  Hash tblHash
59db0 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20  ;        /* All 
59dc0 74 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62  tables indexed b
59dd0 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68  y name */.  Hash
59de0 20 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20   idxHash;       
59df0 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20   /* All (named) 
59e00 69 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20  indices indexed 
59e10 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73  by name */.  Has
59e20 68 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20  h trigHash;     
59e30 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72    /* All trigger
59e40 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d  s indexed by nam
59e50 65 20 2a 2f 0a 20 20 48 61 73 68 20 66 6b 65 79  e */.  Hash fkey
59e60 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41  Hash;       /* A
59e70 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20  ll foreign keys 
59e80 62 79 20 72 65 66 65 72 65 6e 63 65 64 20 74 61  by referenced ta
59e90 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 61  ble name */.  Ta
59ea0 62 6c 65 20 2a 70 53 65 71 54 61 62 3b 20 20 20  ble *pSeqTab;   
59eb0 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65     /* The sqlite
59ec0 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
59ed0 75 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 52  used by AUTOINCR
59ee0 45 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 20 66 69  EMENT */.  u8 fi
59ef0 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20  le_format;      
59f00 2f 2a 20 53 63 68 65 6d 61 20 66 6f 72 6d 61 74  /* Schema format
59f10 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 74 68 69   version for thi
59f20 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 65  s file */.  u8 e
59f30 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
59f40 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e   /* Text encodin
59f50 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20 64  g used by this d
59f60 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 31 36  atabase */.  u16
59f70 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
59f80 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63    /* Flags assoc
59f90 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
59fa0 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20  schema */.  int 
59fb0 63 61 63 68 65 5f 73 69 7a 65 3b 20 20 20 20 20  cache_size;     
59fc0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
59fd0 67 65 73 20 74 6f 20 75 73 65 20 69 6e 20 74 68  ges to use in th
59fe0 65 20 63 61 63 68 65 20 2a 2f 0a 23 69 66 6e 64  e cache */.#ifnd
59ff0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
5a000 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71  IRTUALTABLE.  sq
5a010 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
5a020 20 20 20 2f 2a 20 22 4f 77 6e 65 72 22 20 63 6f     /* "Owner" co
5a030 6e 6e 65 63 74 69 6f 6e 2e 20 53 65 65 20 63 6f  nnection. See co
5a040 6d 6d 65 6e 74 20 61 62 6f 76 65 20 2a 2f 0a 23  mment above */.#
5a050 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
5a060 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 6e  These macros can
5a070 20 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 74   be used to test
5a080 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 20  , set, or clear 
5a090 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20  bits in the .** 
5a0a0 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a  Db.flags field..
5a0b0 2a 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73  */.#define DbHas
5a0c0 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20  Property(D,I,P) 
5a0d0 20 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49      (((D)->aDb[I
5a0e0 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73  ].pSchema->flags
5a0f0 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66  &(P))==(P)).#def
5a100 69 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f 70  ine DbHasAnyProp
5a110 65 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28 28  erty(D,I,P)  (((
5a120 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65  D)->aDb[I].pSche
5a130 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d  ma->flags&(P))!=
5a140 30 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65 74  0).#define DbSet
5a150 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20  Property(D,I,P) 
5a160 20 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e      (D)->aDb[I].
5a170 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d  pSchema->flags|=
5a180 28 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43 6c  (P).#define DbCl
5a190 65 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49 2c  earProperty(D,I,
5a1a0 50 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d  P)   (D)->aDb[I]
5a1b0 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26  .pSchema->flags&
5a1c0 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  =~(P)../*.** All
5a1d0 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20  owed values for 
5a1e0 74 68 65 20 44 42 2e 66 6c 61 67 73 20 66 69 65  the DB.flags fie
5a1f0 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 42  ld..**.** The DB
5a200 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 66 6c  _SchemaLoaded fl
5a210 61 67 20 69 73 20 73 65 74 20 61 66 74 65 72 20  ag is set after 
5a220 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
5a230 65 6d 61 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20  ema has been.** 
5a240 72 65 61 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e  read into intern
5a250 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a  al hash tables..
5a260 2a 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65 73 65 74  **.** DB_Unreset
5a270 56 69 65 77 73 20 6d 65 61 6e 73 20 74 68 61 74  Views means that
5a280 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 69 65   one or more vie
5a290 77 73 20 68 61 76 65 20 63 6f 6c 75 6d 6e 20 6e  ws have column n
5a2a0 61 6d 65 73 20 74 68 61 74 0a 2a 2a 20 68 61 76  ames that.** hav
5a2b0 65 20 62 65 65 6e 20 66 69 6c 6c 65 64 20 6f 75  e been filled ou
5a2c0 74 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 6d  t.  If the schem
5a2d0 61 20 63 68 61 6e 67 65 73 2c 20 74 68 65 73 65  a changes, these
5a2e0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 69   column names mi
5a2f0 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65 73 20 61  ght.** changes a
5a300 6e 64 20 73 6f 20 74 68 65 20 76 69 65 77 20 77  nd so the view w
5a310 69 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 72  ill need to be r
5a320 65 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  eset..*/.#define
5a330 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
5a340 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 54      0x0001  /* T
5a350 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65  he schema has be
5a360 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 65  en loaded */.#de
5a370 66 69 6e 65 20 44 42 5f 55 6e 72 65 73 65 74 56  fine DB_UnresetV
5a380 69 65 77 73 20 20 20 20 30 78 30 30 30 32 20 20  iews    0x0002  
5a390 2f 2a 20 53 6f 6d 65 20 76 69 65 77 73 20 68 61  /* Some views ha
5a3a0 76 65 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d  ve defined colum
5a3b0 6e 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69  n names */.#defi
5a3c0 6e 65 20 44 42 5f 45 6d 70 74 79 20 20 20 20 20  ne DB_Empty     
5a3d0 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a        0x0004  /*
5a3e0 20 54 68 65 20 66 69 6c 65 20 69 73 20 65 6d 70   The file is emp
5a3f0 74 79 20 28 6c 65 6e 67 74 68 20 30 20 62 79 74  ty (length 0 byt
5a400 65 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  es) */../*.** Th
5a410 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66  e number of diff
5a420 65 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74  erent kinds of t
5a430 68 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20 62  hings that can b
5a440 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 75 73 69  e limited.** usi
5a450 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c  ng the sqlite3_l
5a460 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65  imit() interface
5a470 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
5a480 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 4c  ITE_N_LIMIT (SQL
5a490 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45  ITE_LIMIT_TRIGGE
5a4a0 52 5f 44 45 50 54 48 2b 31 29 0a 0a 2f 2a 0a 2a  R_DEPTH+1)../*.*
5a4b0 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c  * Lookaside mall
5a4c0 6f 63 20 69 73 20 61 20 73 65 74 20 6f 66 20 66  oc is a set of f
5a4d0 69 78 65 64 2d 73 69 7a 65 20 62 75 66 66 65 72  ixed-size buffer
5a4e0 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73  s that can be us
5a4f0 65 64 0a 2a 2a 20 74 6f 20 73 61 74 69 73 66 79  ed.** to satisfy
5a500 20 73 6d 61 6c 6c 20 74 72 61 6e 73 69 65 6e 74   small transient
5a510 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
5a520 6f 6e 20 72 65 71 75 65 73 74 73 20 66 6f 72 20  on requests for 
5a530 6f 62 6a 65 63 74 73 0a 2a 2a 20 61 73 73 6f 63  objects.** assoc
5a540 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61 72  iated with a par
5a550 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
5a560 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68   connection.  Th
5a570 65 20 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b  e use of.** look
5a580 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 70 72 6f  aside malloc pro
5a590 76 69 64 65 73 20 61 20 73 69 67 6e 69 66 69 63  vides a signific
5a5a0 61 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ant performance 
5a5b0 65 6e 68 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28  enhancement.** (
5a5c0 61 70 70 72 6f 78 20 31 30 25 29 20 62 79 20 61  approx 10%) by a
5a5d0 76 6f 69 64 69 6e 67 20 6e 75 6d 65 72 6f 75 73  voiding numerous
5a5e0 20 6d 61 6c 6c 6f 63 2f 66 72 65 65 20 72 65 71   malloc/free req
5a5f0 75 65 73 74 73 20 77 68 69 6c 65 20 70 61 72 73  uests while pars
5a600 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65  ing.** SQL state
5a610 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ments..**.** The
5a620 20 4c 6f 6f 6b 61 73 69 64 65 20 73 74 72 75 63   Lookaside struc
5a630 74 75 72 65 20 68 6f 6c 64 73 20 63 6f 6e 66 69  ture holds confi
5a640 67 75 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61  guration informa
5a650 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a  tion about the.*
5a660 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c  * lookaside mall
5a670 6f 63 20 73 75 62 73 79 73 74 65 6d 2e 20 20 45  oc subsystem.  E
5a680 61 63 68 20 61 76 61 69 6c 61 62 6c 65 20 6d 65  ach available me
5a690 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
5a6a0 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73  in.** the lookas
5a6b0 69 64 65 20 73 75 62 73 79 73 74 65 6d 20 69 73  ide subsystem is
5a6c0 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e   stored on a lin
5a6d0 6b 65 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b  ked list of Look
5a6e0 61 73 69 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a  asideSlot.** obj
5a6f0 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b  ects..**.** Look
5a700 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  aside allocation
5a710 73 20 61 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77  s are only allow
5a720 65 64 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 74  ed for objects t
5a730 68 61 74 20 61 72 65 20 61 73 73 6f 63 69 61 74  hat are associat
5a740 65 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72  ed.** with a par
5a750 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
5a760 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 48 65   connection.  He
5a770 6e 63 65 2c 20 73 63 68 65 6d 61 20 69 6e 66 6f  nce, schema info
5a780 72 6d 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a  rmation cannot.*
5a790 2a 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 6c  * be stored in l
5a7a0 6f 6f 6b 61 73 69 64 65 20 62 65 63 61 75 73 65  ookaside because
5a7b0 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 68 65   in shared cache
5a7c0 20 6d 6f 64 65 20 74 68 65 20 73 63 68 65 6d 61   mode the schema
5a7d0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
5a7e0 69 73 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c  is shared by mul
5a7f0 74 69 70 6c 65 20 64 61 74 61 62 61 73 65 20 63  tiple database c
5a800 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68 65  onnections.  The
5a810 72 65 66 6f 72 65 2c 20 77 68 69 6c 65 20 70 61  refore, while pa
5a820 72 73 69 6e 67 0a 2a 2a 20 73 63 68 65 6d 61 20  rsing.** schema 
5a830 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65  information, the
5a840 20 4c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62   Lookaside.bEnab
5a850 6c 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61  led flag is clea
5a860 72 65 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 6c  red so that.** l
5a870 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74  ookaside allocat
5a880 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 75 73 65  ions are not use
5a890 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 74  d to construct t
5a8a0 68 65 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74  he schema object
5a8b0 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f  s..*/.struct Loo
5a8c0 6b 61 73 69 64 65 20 7b 0a 20 20 75 31 36 20 73  kaside {.  u16 s
5a8d0 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z;              
5a8e0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61     /* Size of ea
5a8f0 63 68 20 62 75 66 66 65 72 20 69 6e 20 62 79 74  ch buffer in byt
5a900 65 73 20 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62  es */.  u8 bEnab
5a910 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  led;            
5a920 2f 2a 20 46 61 6c 73 65 20 74 6f 20 64 69 73 61  /* False to disa
5a930 62 6c 65 20 6e 65 77 20 6c 6f 6f 6b 61 73 69 64  ble new lookasid
5a940 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f  e allocations */
5a950 0a 20 20 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b  .  u8 bMalloced;
5a960 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
5a970 75 65 20 69 66 20 70 53 74 61 72 74 20 6f 62 74  ue if pStart obt
5a980 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
5a990 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20  e3_malloc() */. 
5a9a0 20 69 6e 74 20 6e 4f 75 74 3b 20 20 20 20 20 20   int nOut;      
5a9b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5a9c0 65 72 20 6f 66 20 62 75 66 66 65 72 73 20 63 75  er of buffers cu
5a9d0 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20  rrently checked 
5a9e0 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f  out */.  int mxO
5a9f0 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
5aa00 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61   /* Highwater ma
5aa10 72 6b 20 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20  rk for nOut */. 
5aa20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a   LookasideSlot *
5aa30 70 46 72 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74  pFree;   /* List
5aa40 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 75   of available bu
5aa50 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20  ffers */.  void 
5aa60 2a 70 53 74 61 72 74 3b 20 20 20 20 20 20 20 20  *pStart;        
5aa70 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65     /* First byte
5aa80 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 6d 65   of available me
5aa90 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20  mory space */.  
5aaa0 76 6f 69 64 20 2a 70 45 6e 64 3b 20 20 20 20 20  void *pEnd;     
5aab0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
5aac0 20 62 79 74 65 20 70 61 73 74 20 65 6e 64 20 6f   byte past end o
5aad0 66 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63  f available spac
5aae0 65 20 2a 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c  e */.};.struct L
5aaf0 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20  ookasideSlot {. 
5ab00 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a   LookasideSlot *
5ab10 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78  pNext;    /* Nex
5ab20 74 20 62 75 66 66 65 72 20 69 6e 20 74 68 65 20  t buffer in the 
5ab30 6c 69 73 74 20 6f 66 20 66 72 65 65 20 62 75 66  list of free buf
5ab40 66 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  fers */.};../*.*
5ab50 2a 20 41 20 68 61 73 68 20 74 61 62 6c 65 20 66  * A hash table f
5ab60 6f 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69  or function defi
5ab70 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48  nitions..**.** H
5ab80 61 73 68 20 65 61 63 68 20 46 75 6e 63 44 65 66  ash each FuncDef
5ab90 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20   structure into 
5aba0 6f 6e 65 20 6f 66 20 74 68 65 20 46 75 6e 63 44  one of the FuncD
5abb0 65 66 48 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73  efHash.a[] slots
5abc0 2e 0a 2a 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20  ..** Collisions 
5abd0 61 72 65 20 6f 6e 20 74 68 65 20 46 75 6e 63 44  are on the FuncD
5abe0 65 66 2e 70 48 61 73 68 20 63 68 61 69 6e 2e 0a  ef.pHash chain..
5abf0 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65  */.struct FuncDe
5ac00 66 48 61 73 68 20 7b 0a 20 20 46 75 6e 63 44 65  fHash {.  FuncDe
5ac10 66 20 2a 61 5b 32 33 5d 3b 20 20 20 20 20 20 20  f *a[23];       
5ac20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f  /* Hash table fo
5ac30 72 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d  r functions */.}
5ac40 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61  ;../*.** Each da
5ac50 74 61 62 61 73 65 20 69 73 20 61 6e 20 69 6e 73  tabase is an ins
5ac60 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
5ac70 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
5ac80 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
5ac90 74 65 2e 6c 61 73 74 52 6f 77 69 64 20 72 65 63  te.lastRowid rec
5aca0 6f 72 64 73 20 74 68 65 20 6c 61 73 74 20 69 6e  ords the last in
5acb0 73 65 72 74 20 72 6f 77 69 64 20 67 65 6e 65 72  sert rowid gener
5acc0 61 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e  ated by an.** in
5acd0 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74 2e 20  sert statement. 
5ace0 20 49 6e 73 65 72 74 73 20 6f 6e 20 76 69 65 77   Inserts on view
5acf0 73 20 64 6f 20 6e 6f 74 20 61 66 66 65 63 74 20  s do not affect 
5ad00 69 74 73 20 76 61 6c 75 65 2e 20 20 45 61 63 68  its value.  Each
5ad10 0a 2a 2a 20 74 72 69 67 67 65 72 20 68 61 73 20  .** trigger has 
5ad20 69 74 73 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c  its own context,
5ad30 20 73 6f 20 74 68 61 74 20 6c 61 73 74 52 6f 77   so that lastRow
5ad40 69 64 20 63 61 6e 20 62 65 20 75 70 64 61 74 65  id can be update
5ad50 64 20 69 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67  d inside.** trig
5ad60 67 65 72 73 20 61 73 20 75 73 75 61 6c 2e 20 20  gers as usual.  
5ad70 54 68 65 20 70 72 65 76 69 6f 75 73 20 76 61 6c  The previous val
5ad80 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f  ue will be resto
5ad90 72 65 64 20 6f 6e 63 65 20 74 68 65 20 74 72 69  red once the tri
5ada0 67 67 65 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20  gger.** exits.  
5adb0 55 70 6f 6e 20 65 6e 74 65 72 69 6e 67 20 61 20  Upon entering a 
5adc0 62 65 66 6f 72 65 20 6f 72 20 69 6e 73 74 65 61  before or instea
5add0 64 20 6f 66 20 74 72 69 67 67 65 72 2c 20 6c 61  d of trigger, la
5ade0 73 74 52 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a  stRowid is no.**
5adf0 20 6c 6f 6e 67 65 72 20 28 73 69 6e 63 65 20 61   longer (since a
5ae00 66 74 65 72 20 76 65 72 73 69 6f 6e 20 32 2e 38  fter version 2.8
5ae10 2e 31 32 29 20 72 65 73 65 74 20 74 6f 20 2d 31  .12) reset to -1
5ae20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
5ae30 74 65 2e 6e 43 68 61 6e 67 65 20 64 6f 65 73 20  te.nChange does 
5ae40 6e 6f 74 20 63 6f 75 6e 74 20 63 68 61 6e 67 65  not count change
5ae50 73 20 77 69 74 68 69 6e 20 74 72 69 67 67 65 72  s within trigger
5ae60 73 20 61 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a  s and keeps no.*
5ae70 2a 20 63 6f 6e 74 65 78 74 2e 20 20 49 74 20 69  * context.  It i
5ae80 73 20 72 65 73 65 74 20 61 74 20 73 74 61 72 74  s reset at start
5ae90 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63   of sqlite3_exec
5aea0 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e  ..** The sqlite.
5aeb0 6c 73 43 68 61 6e 67 65 20 72 65 70 72 65 73 65  lsChange represe
5aec0 6e 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  nts the number o
5aed0 66 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62  f changes made b
5aee0 79 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e  y the last.** in
5aef0 73 65 72 74 2c 20 75 70 64 61 74 65 2c 20 6f 72  sert, update, or
5af00 20 64 65 6c 65 74 65 20 73 74 61 74 65 6d 65 6e   delete statemen
5af10 74 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 63  t.  It remains c
5af20 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f  onstant througho
5af30 75 74 20 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68  ut the.** length
5af40 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   of a statement 
5af50 61 6e 64 20 69 73 20 74 68 65 6e 20 75 70 64 61  and is then upda
5af60 74 65 64 20 62 79 20 4f 50 5f 53 65 74 43 6f 75  ted by OP_SetCou
5af70 6e 74 73 2e 20 20 49 74 20 6b 65 65 70 73 20 61  nts.  It keeps a
5af80 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 73 74 61 63  .** context stac
5af90 6b 20 6a 75 73 74 20 6c 69 6b 65 20 6c 61 73 74  k just like last
5afa0 52 6f 77 69 64 20 73 6f 20 74 68 61 74 20 74 68  Rowid so that th
5afb0 65 20 63 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67  e count of chang
5afc0 65 73 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 74  es.** within a t
5afd0 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20 73 65  rigger is not se
5afe0 65 6e 20 6f 75 74 73 69 64 65 20 74 68 65 20 74  en outside the t
5aff0 72 69 67 67 65 72 2e 20 20 43 68 61 6e 67 65 73  rigger.  Changes
5b000 20 74 6f 20 76 69 65 77 73 20 64 6f 20 6e 6f 74   to views do not
5b010 0a 2a 2a 20 61 66 66 65 63 74 20 74 68 65 20 76  .** affect the v
5b020 61 6c 75 65 20 6f 66 20 6c 73 43 68 61 6e 67 65  alue of lsChange
5b030 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e  ..** The sqlite.
5b040 63 73 43 68 61 6e 67 65 20 6b 65 65 70 73 20 74  csChange keeps t
5b050 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62  rack of the numb
5b060 65 72 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68  er of current ch
5b070 61 6e 67 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20  anges (since.** 
5b080 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65  the last stateme
5b090 6e 74 29 20 61 6e 64 20 69 73 20 75 73 65 64 20  nt) and is used 
5b0a0 74 6f 20 75 70 64 61 74 65 20 73 71 6c 69 74 65  to update sqlite
5b0b0 5f 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a  _lsChange..**.**
5b0c0 20 54 68 65 20 6d 65 6d 62 65 72 20 76 61 72 69   The member vari
5b0d0 61 62 6c 65 73 20 73 71 6c 69 74 65 2e 65 72 72  ables sqlite.err
5b0e0 43 6f 64 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72  Code, sqlite.zEr
5b0f0 72 4d 73 67 20 61 6e 64 20 73 71 6c 69 74 65 2e  rMsg and sqlite.
5b100 7a 45 72 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f  zErrMsg16.** sto
5b110 72 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  re the most rece
5b120 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e  nt error code an
5b130 64 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65  d, if applicable
5b140 2c 20 73 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a  , string. The.**
5b150 20 69 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69   internal functi
5b160 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  on sqlite3Error(
5b170 29 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74  ) is used to set
5b180 20 74 68 65 73 65 20 76 61 72 69 61 62 6c 65 73   these variables
5b190 0a 2a 2a 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79  .** consistently
5b1a0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
5b1b0 74 65 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  te3 {.  sqlite3_
5b1c0 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20  vfs *pVfs;      
5b1d0 20 20 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65        /* OS Inte
5b1e0 72 66 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  rface */.  int n
5b1f0 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
5b200 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5b210 65 72 20 6f 66 20 62 61 63 6b 65 6e 64 73 20 63  er of backends c
5b220 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20  urrently in use 
5b230 2a 2f 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20  */.  Db *aDb;   
5b240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b250 20 20 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e     /* All backen
5b260 64 73 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  ds */.  int flag
5b270 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
5b280 20 20 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c        /* Miscell
5b290 61 6e 65 6f 75 73 20 66 6c 61 67 73 2e 20 53 65  aneous flags. Se
5b2a0 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74  e below */.  int
5b2b0 20 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20   openFlags;     
5b2c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
5b2d0 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 73 71  ags passed to sq
5b2e0 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28  lite3_vfs.xOpen(
5b2f0 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f  ) */.  int errCo
5b300 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  de;             
5b310 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
5b320 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 28  ent error code (
5b330 53 51 4c 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69  SQLITE_*) */.  i
5b340 6e 74 20 65 72 72 4d 61 73 6b 3b 20 20 20 20 20  nt errMask;     
5b350 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b360 26 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77  & result codes w
5b370 69 74 68 20 74 68 69 73 20 62 65 66 6f 72 65 20  ith this before 
5b380 72 65 74 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75  returning */.  u
5b390 38 20 61 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20  8 autoCommit;   
5b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b3b0 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  The auto-commit 
5b3c0 66 6c 61 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65  flag. */.  u8 te
5b3d0 6d 70 5f 73 74 6f 72 65 3b 20 20 20 20 20 20 20  mp_store;       
5b3e0 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 66           /* 1: f
5b3f0 69 6c 65 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a  ile 2: memory 0:
5b400 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38   default */.  u8
5b410 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20   mallocFailed;  
5b420 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
5b430 72 75 65 20 69 66 20 77 65 20 68 61 76 65 20 73  rue if we have s
5b440 65 65 6e 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69  een a malloc fai
5b450 6c 75 72 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c  lure */.  u8 dfl
5b460 74 4c 6f 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20  tLockMode;      
5b470 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75          /* Defau
5b480 6c 74 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20  lt locking-mode 
5b490 66 6f 72 20 61 74 74 61 63 68 65 64 20 64 62 73  for attached dbs
5b4a0 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75   */.  u8 dfltJou
5b4b0 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20  rnalMode;       
5b4c0 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a      /* Default j
5b4d0 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20  ournal mode for 
5b4e0 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a  attached dbs */.
5b4f0 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 6e 65    signed char ne
5b500 78 74 41 75 74 6f 76 61 63 3b 20 20 20 20 20 20  xtAutovac;      
5b510 2f 2a 20 41 75 74 6f 76 61 63 20 73 65 74 74 69  /* Autovac setti
5b520 6e 67 20 61 66 74 65 72 20 56 41 43 55 55 4d 20  ng after VACUUM 
5b530 69 66 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20  if >=0 */.  int 
5b540 6e 65 78 74 50 61 67 65 73 69 7a 65 3b 20 20 20  nextPagesize;   
5b550 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
5b560 65 73 69 7a 65 20 61 66 74 65 72 20 56 41 43 55  esize after VACU
5b570 55 4d 20 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e  UM if >0 */.  in
5b580 74 20 6e 54 61 62 6c 65 3b 20 20 20 20 20 20 20  t nTable;       
5b590 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
5b5a0 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20  umber of tables 
5b5b0 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
5b5c0 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44  */.  CollSeq *pD
5b5d0 66 6c 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20  fltColl;        
5b5e0 20 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c     /* The defaul
5b5f0 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  t collating sequ
5b600 65 6e 63 65 20 28 42 49 4e 41 52 59 29 20 2a 2f  ence (BINARY) */
5b610 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64  .  i64 lastRowid
5b620 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5b630 20 2f 2a 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73   /* ROWID of mos
5b640 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74 20  t recent insert 
5b650 28 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20  (see above) */. 
5b660 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20   u32 magic;     
5b670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5b680 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66  * Magic number f
5b690 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 61 72  or detect librar
5b6a0 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e  y misuse */.  in
5b6b0 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20  t nChange;      
5b6c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
5b6d0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
5b6e0 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
5b6f0 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74  () */.  int nTot
5b700 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20  alChange;       
5b710 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72        /* Value r
5b720 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
5b730 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
5b740 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  () */.  sqlite3_
5b750 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20  mutex *mutex;   
5b760 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74        /* Connect
5b770 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69  ion mutex */.  i
5b780 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  nt aLimit[SQLITE
5b790 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20  _N_LIMIT];   /* 
5b7a0 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75  Limits */.  stru
5b7b0 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e  ct sqlite3InitIn
5b7c0 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66  fo {      /* Inf
5b7d0 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75  ormation used du
5b7e0 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
5b7f0 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ion */.    int i
5b800 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
5b810 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62         /* When b
5b820 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69  ack is being ini
5b830 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20  tialized */.    
5b840 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20  int newTnum;    
5b850 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
5b860 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65  ootpage of table
5b870 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a   being initializ
5b880 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73  ed */.    u8 bus
5b890 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
5b8a0 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66        /* TRUE if
5b8b0 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69   currently initi
5b8c0 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 20 20 75  alizing */.    u
5b8d0 38 20 6f 72 70 68 61 6e 54 72 69 67 67 65 72 3b  8 orphanTrigger;
5b8e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
5b8f0 73 74 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  st statement is 
5b900 6f 72 70 68 61 6e 65 64 20 54 45 4d 50 20 74 72  orphaned TEMP tr
5b910 69 67 67 65 72 20 2a 2f 0a 20 20 7d 20 69 6e 69  igger */.  } ini
5b920 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 6e 73  t;.  int nExtens
5b930 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ion;            
5b940 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5b950 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f 6e  loaded extension
5b960 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45  s */.  void **aE
5b970 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20  xtension;       
5b980 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
5b990 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
5b9a0 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72  handles */.  str
5b9b0 75 63 74 20 56 64 62 65 20 2a 70 56 64 62 65 3b  uct Vdbe *pVdbe;
5b9c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69             /* Li
5b9d0 73 74 20 6f 66 20 61 63 74 69 76 65 20 76 69 72  st of active vir
5b9e0 74 75 61 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f  tual machines */
5b9f0 0a 20 20 69 6e 74 20 61 63 74 69 76 65 56 64 62  .  int activeVdb
5ba00 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  eCnt;           
5ba10 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56 44   /* Number of VD
5ba20 42 45 73 20 63 75 72 72 65 6e 74 6c 79 20 65 78  BEs currently ex
5ba30 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  ecuting */.  int
5ba40 20 77 72 69 74 65 56 64 62 65 43 6e 74 3b 20 20   writeVdbeCnt;  
5ba50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
5ba60 6d 62 65 72 20 6f 66 20 61 63 74 69 76 65 20 56  mber of active V
5ba70 44 42 45 73 20 74 68 61 74 20 61 72 65 20 77 72  DBEs that are wr
5ba80 69 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20  iting */.  void 
5ba90 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c  (*xTrace)(void*,
5baa0 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 20 20  const char*);   
5bab0 20 20 20 20 20 2f 2a 20 54 72 61 63 65 20 66 75       /* Trace fu
5bac0 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
5bad0 20 2a 70 54 72 61 63 65 41 72 67 3b 20 20 20 20   *pTraceArg;    
5bae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5baf0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
5bb00 74 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 66  t to the trace f
5bb10 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  unction */.  voi
5bb20 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f  d (*xProfile)(vo
5bb30 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  id*,const char*,
5bb40 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c  u64);  /* Profil
5bb50 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ing function */.
5bb60 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65    void *pProfile
5bb70 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
5bb80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5bb90 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69  rgument to profi
5bba0 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  le function */. 
5bbb0 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72   void *pCommitAr
5bbc0 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
5bbd0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
5bbe0 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63  o xCommitCallbac
5bbf0 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20  k() */   .  int 
5bc00 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63  (*xCommitCallbac
5bc10 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a  k)(void*);    /*
5bc20 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72   Invoked at ever
5bc30 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76  y commit. */.  v
5bc40 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72  oid *pRollbackAr
5bc50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
5bc60 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
5bc70 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
5bc80 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64  k() */   .  void
5bc90 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c   (*xRollbackCall
5bca0 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a  back)(void*); /*
5bcb0 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72   Invoked at ever
5bcc0 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76  y commit. */.  v
5bcd0 6f 69 64 20 2a 70 55 70 64 61 74 65 41 72 67 3b  oid *pUpdateArg;
5bce0 0a 20 20 76 6f 69 64 20 28 2a 78 55 70 64 61 74  .  void (*xUpdat
5bcf0 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  eCallback)(void*
5bd00 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  ,int, const char
5bd10 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71  *,const char*,sq
5bd20 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76  lite_int64);.  v
5bd30 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64  oid(*xCollNeeded
5bd40 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
5bd50 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
5bd60 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f  nst char*);.  vo
5bd70 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31  id(*xCollNeeded1
5bd80 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  6)(void*,sqlite3
5bd90 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63  *,int eTextRep,c
5bda0 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76  onst void*);.  v
5bdb0 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64  oid *pCollNeeded
5bdc0 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  Arg;.  sqlite3_v
5bdd0 61 6c 75 65 20 2a 70 45 72 72 3b 20 20 20 20 20  alue *pErr;     
5bde0 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
5bdf0 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
5be00 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72  e */.  char *zEr
5be10 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20  rMsg;           
5be20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
5be30 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
5be40 65 20 28 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  e (UTF-8 encoded
5be50 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72  ) */.  char *zEr
5be60 72 4d 73 67 31 36 3b 20 20 20 20 20 20 20 20 20  rMsg16;         
5be70 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
5be80 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
5be90 65 20 28 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  e (UTF-16 encode
5bea0 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a  d) */.  union {.
5beb0 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74      volatile int
5bec0 20 69 73 49 6e 74 65 72 72 75 70 74 65 64 3b 20   isInterrupted; 
5bed0 2f 2a 20 54 72 75 65 20 69 66 20 73 71 6c 69 74  /* True if sqlit
5bee0 65 33 5f 69 6e 74 65 72 72 75 70 74 20 68 61 73  e3_interrupt has
5bef0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a   been called */.
5bf00 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73      double notUs
5bf10 65 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  ed1;            
5bf20 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d  /* Spacer */.  }
5bf30 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65   u1;.  Lookaside
5bf40 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20   lookaside;     
5bf50 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64       /* Lookasid
5bf60 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75  e malloc configu
5bf70 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65  ration */.#ifnde
5bf80 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
5bf90 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e  THORIZATION.  in
5bfa0 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a  t (*xAuth)(void*
5bfb0 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
5bfc0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
5bfd0 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
5bfe0 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 20  har*);.         
5bff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c000 20 20 20 20 20 20 20 2f 2a 20 41 63 63 65 73 73         /* Access
5c010 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66   authorization f
5c020 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  unction */.  voi
5c030 64 20 2a 70 41 75 74 68 41 72 67 3b 20 20 20 20  d *pAuthArg;    
5c040 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73             /* 1s
5c050 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
5c060 65 20 61 63 63 65 73 73 20 61 75 74 68 20 66 75  e access auth fu
5c070 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66  nction */.#endif
5c080 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
5c090 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41  OMIT_PROGRESS_CA
5c0a0 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78  LLBACK.  int (*x
5c0b0 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 20 2a  Progress)(void *
5c0c0 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72  );     /* The pr
5c0d0 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
5c0e0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 67  */.  void *pProg
5c0f0 72 65 73 73 41 72 67 3b 20 20 20 20 20 20 20 20  ressArg;        
5c100 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
5c110 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63  o the progress c
5c120 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  allback */.  int
5c130 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b 20 20   nProgressOps;  
5c140 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
5c150 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20  mber of opcodes 
5c160 66 6f 72 20 70 72 6f 67 72 65 73 73 20 63 61 6c  for progress cal
5c170 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a  lback */.#endif.
5c180 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
5c190 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
5c1a0 0a 20 20 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b  .  Hash aModule;
5c1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c1c0 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20 62 79   /* populated by
5c1d0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
5c1e0 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61  module() */.  Ta
5c1f0 62 6c 65 20 2a 70 56 54 61 62 3b 20 20 20 20 20  ble *pVTab;     
5c200 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76              /* v
5c210 74 61 62 20 77 69 74 68 20 61 63 74 69 76 65 20  tab with active 
5c220 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65 20 6d  Connect/Create m
5c230 65 74 68 6f 64 20 2a 2f 0a 20 20 56 54 61 62 6c  ethod */.  VTabl
5c240 65 20 2a 2a 61 56 54 72 61 6e 73 3b 20 20 20 20  e **aVTrans;    
5c250 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
5c260 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 20  ual tables with 
5c270 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e  open transaction
5c280 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61  s */.  int nVTra
5c290 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ns;             
5c2a0 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65       /* Allocate
5c2b0 64 20 73 69 7a 65 20 6f 66 20 61 56 54 72 61 6e  d size of aVTran
5c2c0 73 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70  s */.  VTable *p
5c2d0 44 69 73 63 6f 6e 6e 65 63 74 3b 20 20 20 20 2f  Disconnect;    /
5c2e0 2a 20 44 69 73 63 6f 6e 6e 65 63 74 20 74 68 65  * Disconnect the
5c2f0 73 65 20 69 6e 20 6e 65 78 74 20 73 71 6c 69 74  se in next sqlit
5c300 65 33 5f 70 72 65 70 61 72 65 28 29 20 2a 2f 0a  e3_prepare() */.
5c310 23 65 6e 64 69 66 0a 20 20 46 75 6e 63 44 65 66  #endif.  FuncDef
5c320 48 61 73 68 20 61 46 75 6e 63 3b 20 20 20 20 20  Hash aFunc;     
5c330 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74         /* Hash t
5c340 61 62 6c 65 20 6f 66 20 63 6f 6e 6e 65 63 74 69  able of connecti
5c350 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  on functions */.
5c360 20 20 48 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b    Hash aCollSeq;
5c370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c380 2f 2a 20 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67  /* All collating
5c390 20 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20   sequences */.  
5c3a0 42 75 73 79 48 61 6e 64 6c 65 72 20 62 75 73 79  BusyHandler busy
5c3b0 48 61 6e 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a  Handler;      /*
5c3c0 20 42 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a   Busy callback *
5c3d0 2f 0a 20 20 69 6e 74 20 62 75 73 79 54 69 6d 65  /.  int busyTime
5c3e0 6f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  out;            
5c3f0 20 20 2f 2a 20 42 75 73 79 20 68 61 6e 64 6c 65    /* Busy handle
5c400 72 20 74 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73  r timeout, in ms
5c410 65 63 20 2a 2f 0a 20 20 44 62 20 61 44 62 53 74  ec */.  Db aDbSt
5c420 61 74 69 63 5b 32 5d 3b 20 20 20 20 20 20 20 20  atic[2];        
5c430 20 20 20 20 20 20 2f 2a 20 53 74 61 74 69 63 20        /* Static 
5c440 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 32 20  space for the 2 
5c450 64 65 66 61 75 6c 74 20 62 61 63 6b 65 6e 64 73  default backends
5c460 20 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e 74 20   */.  Savepoint 
5c470 2a 70 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20  *pSavepoint;    
5c480 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61      /* List of a
5c490 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73  ctive savepoints
5c4a0 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70   */.  int nSavep
5c4b0 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  oint;           
5c4c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
5c4d0 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e   non-transaction
5c4e0 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20   savepoints */. 
5c4f0 20 69 6e 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b   int nStatement;
5c500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5c510 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74  * Number of nest
5c520 65 64 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61  ed statement-tra
5c530 6e 73 61 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20  nsactions  */.  
5c540 75 38 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e  u8 isTransaction
5c550 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a  Savepoint;    /*
5c560 20 54 72 75 65 20 69 66 20 74 68 65 20 6f 75 74   True if the out
5c570 65 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74  ermost savepoint
5c580 20 69 73 20 61 20 54 53 20 2a 2f 0a 20 20 69 36   is a TS */.  i6
5c590 34 20 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b  4 nDeferredCons;
5c5a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
5c5b0 65 74 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73  et deferred cons
5c5c0 74 72 61 69 6e 74 73 20 74 68 69 73 20 74 72 61  traints this tra
5c5d0 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 0a 23 69  nsaction. */..#i
5c5e0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
5c5f0 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59  LE_UNLOCK_NOTIFY
5c600 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
5c610 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72  ing variables ar
5c620 65 20 61 6c 6c 20 70 72 6f 74 65 63 74 65 64 20  e all protected 
5c630 62 79 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41  by the STATIC_MA
5c640 53 54 45 52 20 0a 20 20 2a 2a 20 6d 75 74 65 78  STER .  ** mutex
5c650 2c 20 6e 6f 74 20 62 79 20 73 71 6c 69 74 65 33  , not by sqlite3
5c660 2e 6d 75 74 65 78 2e 20 54 68 65 79 20 61 72 65  .mutex. They are
5c670 20 75 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e   used by code in
5c680 20 6e 6f 74 69 66 79 2e 63 2e 20 0a 20 20 2a 2a   notify.c. .  **
5c690 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 55 6e  .  ** When X.pUn
5c6a0 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  lockConnection==
5c6b0 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  Y, that means th
5c6c0 61 74 20 58 20 69 73 20 77 61 69 74 69 6e 67 20  at X is waiting 
5c6d0 66 6f 72 20 59 20 74 6f 0a 20 20 2a 2a 20 75 6e  for Y to.  ** un
5c6e0 6c 6f 63 6b 20 73 6f 20 74 68 61 74 20 69 74 20  lock so that it 
5c6f0 63 61 6e 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a  can proceed..  *
5c700 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 42  *.  ** When X.pB
5c710 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
5c720 6e 3d 3d 59 2c 20 74 68 61 74 20 6d 65 61 6e 73  n==Y, that means
5c730 20 74 68 61 74 20 73 6f 6d 65 74 68 69 6e 67 20   that something 
5c740 74 68 61 74 20 58 20 74 72 69 65 64 0a 20 20 2a  that X tried.  *
5c750 2a 20 74 72 69 65 64 20 74 6f 20 64 6f 20 72 65  * tried to do re
5c760 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 77 69  cently failed wi
5c770 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  th an SQLITE_LOC
5c780 4b 45 44 20 65 72 72 6f 72 20 64 75 65 20 74 6f  KED error due to
5c790 20 6c 6f 63 6b 73 0a 20 20 2a 2a 20 68 65 6c 64   locks.  ** held
5c7a0 20 62 79 20 59 2e 0a 20 20 2a 2f 0a 20 20 73 71   by Y..  */.  sq
5c7b0 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 69 6e 67  lite3 *pBlocking
5c7c0 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 43  Connection; /* C
5c7d0 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 63  onnection that c
5c7e0 61 75 73 65 64 20 53 51 4c 49 54 45 5f 4c 4f 43  aused SQLITE_LOC
5c7f0 4b 45 44 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  KED */.  sqlite3
5c800 20 2a 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74   *pUnlockConnect
5c810 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ion;           /
5c820 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  * Connection to 
5c830 77 61 74 63 68 20 66 6f 72 20 75 6e 6c 6f 63 6b  watch for unlock
5c840 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 6e 6c   */.  void *pUnl
5c850 6f 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20  ockArg;         
5c860 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5c870 72 67 75 6d 65 6e 74 20 74 6f 20 78 55 6e 6c 6f  rgument to xUnlo
5c880 63 6b 4e 6f 74 69 66 79 20 2a 2f 0a 20 20 76 6f  ckNotify */.  vo
5c890 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69  id (*xUnlockNoti
5c8a0 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74  fy)(void **, int
5c8b0 29 3b 20 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f  );  /* Unlock no
5c8c0 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  tify callback */
5c8d0 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 4e 65 78  .  sqlite3 *pNex
5c8e0 74 42 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20  tBlocked;       
5c8f0 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73 74   /* Next in list
5c900 20 6f 66 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20   of all blocked 
5c910 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 23  connections */.#
5c920 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
5c930 41 20 6d 61 63 72 6f 20 74 6f 20 64 69 73 63 6f  A macro to disco
5c940 76 65 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  ver the encoding
5c950 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 0a   of a database..
5c960 2a 2f 0a 23 64 65 66 69 6e 65 20 45 4e 43 28 64  */.#define ENC(d
5c970 62 29 20 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d  b) ((db)->aDb[0]
5c980 2e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a  .pSchema->enc)..
5c990 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76  /*.** Possible v
5c9a0 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 71  alues for the sq
5c9b0 6c 69 74 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f  lite.flags and o
5c9c0 72 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64  r Db.flags field
5c9d0 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69  s..**.** On sqli
5c9e0 74 65 2e 66 6c 61 67 73 2c 20 74 68 65 20 53 51  te.flags, the SQ
5c9f0 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c  LITE_InTrans val
5ca00 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  ue means that we
5ca10 20 68 61 76 65 0a 2a 2a 20 65 78 65 63 75 74 65   have.** execute
5ca20 64 20 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44  d a BEGIN.  On D
5ca30 62 2e 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f  b.flags, SQLITE_
5ca40 49 6e 54 72 61 6e 73 20 6d 65 61 6e 73 20 61 20  InTrans means a 
5ca50 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61  statement.** tra
5ca60 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
5ca70 76 65 20 6f 6e 20 74 68 61 74 20 70 61 72 74 69  ve on that parti
5ca80 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 66  cular database f
5ca90 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ile..*/.#define 
5caa0 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65  SQLITE_VdbeTrace
5cab0 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30 31        0x00000001
5cac0 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61    /* True to tra
5cad0 63 65 20 56 44 42 45 20 65 78 65 63 75 74 69 6f  ce VDBE executio
5cae0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  n */.#define SQL
5caf0 49 54 45 5f 49 6e 54 72 61 6e 73 20 20 20 20 20  ITE_InTrans     
5cb00 20 20 20 30 78 30 30 30 30 30 30 30 38 20 20 2f     0x00000008  /
5cb10 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 74  * True if in a t
5cb20 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64  ransaction */.#d
5cb30 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74  efine SQLITE_Int
5cb40 65 72 6e 43 68 61 6e 67 65 73 20 20 30 78 30 30  ernChanges  0x00
5cb50 30 30 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d  000010  /* Uncom
5cb60 6d 69 74 74 65 64 20 48 61 73 68 20 74 61 62 6c  mitted Hash tabl
5cb70 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65  e changes */.#de
5cb80 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c  fine SQLITE_Full
5cb90 43 6f 6c 4e 61 6d 65 73 20 20 20 30 78 30 30 30  ColNames   0x000
5cba0 30 30 30 32 30 20 20 2f 2a 20 53 68 6f 77 20 66  00020  /* Show f
5cbb0 75 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  ull column names
5cbc0 20 6f 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64   on SELECT */.#d
5cbd0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f  efine SQLITE_Sho
5cbe0 72 74 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30  rtColNames  0x00
5cbf0 30 30 30 30 34 30 20 20 2f 2a 20 53 68 6f 77 20  000040  /* Show 
5cc00 73 68 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61  short columns na
5cc10 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  mes */.#define S
5cc20 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
5cc30 20 20 20 20 20 30 78 30 30 30 30 30 30 38 30 20       0x00000080 
5cc40 20 2f 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20 63   /* Count rows c
5cc50 68 61 6e 67 65 64 20 62 79 20 49 4e 53 45 52 54  hanged by INSERT
5cc60 2c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  , */.           
5cc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5cc90 2a 20 20 20 44 45 4c 45 54 45 2c 20 6f 72 20 55  *   DELETE, or U
5cca0 50 44 41 54 45 20 61 6e 64 20 72 65 74 75 72 6e  PDATE and return
5ccb0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
5ccc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ccd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5cce0 20 20 20 74 68 65 20 63 6f 75 6e 74 20 75 73 69     the count usi
5ccf0 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a  ng a callback. *
5cd00 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5cd10 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20  _NullCallback   
5cd20 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 49  0x00000100  /* I
5cd30 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61  nvoke the callba
5cd40 63 6b 20 6f 6e 63 65 20 69 66 20 74 68 65 20 2a  ck once if the *
5cd50 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
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 2f 2a 20 20              /*  
5cd80 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65   result set is e
5cd90 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mpty */.#define 
5cda0 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20  SQLITE_SqlTrace 
5cdb0 20 20 20 20 20 20 30 78 30 30 30 30 30 32 30 30        0x00000200
5cdc0 20 20 2f 2a 20 44 65 62 75 67 20 70 72 69 6e 74    /* Debug print
5cdd0 20 53 51 4c 20 61 73 20 69 74 20 65 78 65 63 75   SQL as it execu
5cde0 74 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  tes */.#define S
5cdf0 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e  QLITE_VdbeListin
5ce00 67 20 20 20 20 30 78 30 30 30 30 30 34 30 30 20  g    0x00000400 
5ce10 20 2f 2a 20 44 65 62 75 67 20 6c 69 73 74 69 6e   /* Debug listin
5ce20 67 73 20 6f 66 20 56 44 42 45 20 70 72 6f 67 72  gs of VDBE progr
5ce30 61 6d 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ams */.#define S
5ce40 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d  QLITE_WriteSchem
5ce50 61 20 20 20 20 30 78 30 30 30 30 30 38 30 30 20  a    0x00000800 
5ce60 20 2f 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74 65   /* OK to update
5ce70 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a   SQLITE_MASTER *
5ce80 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5ce90 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 20  _NoReadlock     
5cea0 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 52  0x00001000  /* R
5ceb0 65 61 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69  eadlocks are omi
5cec0 74 74 65 64 20 77 68 65 6e 20 0a 20 20 20 20 20  tted when .     
5ced0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cef0 20 20 20 20 20 2a 2a 20 61 63 63 65 73 73 69 6e       ** accessin
5cf00 67 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61  g read-only data
5cf10 62 61 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  bases */.#define
5cf20 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68   SQLITE_IgnoreCh
5cf30 65 63 6b 73 20 20 20 30 78 30 30 30 30 32 30 30  ecks   0x0000200
5cf40 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66  0  /* Do not enf
5cf50 6f 72 63 65 20 63 68 65 63 6b 20 63 6f 6e 73 74  orce check const
5cf60 72 61 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e  raints */.#defin
5cf70 65 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63  e SQLITE_ReadUnc
5cf80 6f 6d 6d 69 74 74 65 64 20 30 78 30 30 30 30 34  ommitted 0x00004
5cf90 30 30 30 20 2f 2a 20 46 6f 72 20 73 68 61 72 65  000 /* For share
5cfa0 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a  d-cache mode */.
5cfb0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
5cfc0 65 67 61 63 79 46 69 6c 65 46 6d 74 20 20 30 78  egacyFileFmt  0x
5cfd0 30 30 30 30 38 30 30 30 20 20 2f 2a 20 43 72 65  00008000  /* Cre
5cfe0 61 74 65 20 6e 65 77 20 64 61 74 61 62 61 73 65  ate new database
5cff0 73 20 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f  s in format 1 */
5d000 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5d010 46 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 20 30  FullFSync      0
5d020 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 55 73  x00010000  /* Us
5d030 65 20 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20  e full fsync on 
5d040 74 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23  the backend */.#
5d050 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f  define SQLITE_Lo
5d060 61 64 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 30  adExtension  0x0
5d070 30 30 32 30 30 30 30 20 20 2f 2a 20 45 6e 61 62  0020000  /* Enab
5d080 6c 65 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  le load_extensio
5d090 6e 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51  n */..#define SQ
5d0a0 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64  LITE_RecoveryMod
5d0b0 65 20 20 20 30 78 30 30 30 34 30 30 30 30 20 20  e   0x00040000  
5d0c0 2f 2a 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61  /* Ignore schema
5d0d0 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69   errors */.#defi
5d0e0 6e 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73  ne SQLITE_Revers
5d0f0 65 4f 72 64 65 72 20 20 20 30 78 30 30 31 30 30  eOrder   0x00100
5d100 30 30 30 20 20 2f 2a 20 52 65 76 65 72 73 65 20  000  /* Reverse 
5d110 75 6e 6f 72 64 65 72 65 64 20 53 45 4c 45 43 54  unordered SELECT
5d120 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
5d130 49 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 20  ITE_RecTriggers 
5d140 20 20 20 30 78 30 30 32 30 30 30 30 30 20 20 2f     0x00200000  /
5d150 2a 20 45 6e 61 62 6c 65 20 72 65 63 75 72 73 69  * Enable recursi
5d160 76 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23  ve triggers */.#
5d170 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 6f  define SQLITE_Fo
5d180 72 65 69 67 6e 4b 65 79 73 20 20 20 20 30 78 30  reignKeys    0x0
5d190 30 34 30 30 30 30 30 20 20 2f 2a 20 45 6e 66 6f  0400000  /* Enfo
5d1a0 72 63 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  rce foreign key 
5d1b0 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 2a 2f 0a  constraints  */.
5d1c0 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20  ./*.** Possible 
5d1d0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73  values for the s
5d1e0 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 69 65 6c  qlite.magic fiel
5d1f0 64 2e 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72  d..** The number
5d200 73 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 61  s are obtained a
5d210 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76  t random and hav
5d220 65 20 6e 6f 20 73 70 65 63 69 61 6c 20 6d 65 61  e no special mea
5d230 6e 69 6e 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74  ning, other.** t
5d240 68 61 6e 20 62 65 69 6e 67 20 64 69 73 74 69 6e  han being distin
5d250 63 74 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74  ct from one anot
5d260 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  her..*/.#define 
5d270 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
5d280 4e 20 20 20 20 20 30 78 61 30 32 39 61 36 39 37  N     0xa029a697
5d290 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73    /* Database is
5d2a0 20 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65   open */.#define
5d2b0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c   SQLITE_MAGIC_CL
5d2c0 4f 53 45 44 20 20 20 30 78 39 66 33 63 32 64 33  OSED   0x9f3c2d3
5d2d0 33 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69  3  /* Database i
5d2e0 73 20 63 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66  s closed */.#def
5d2f0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  ine SQLITE_MAGIC
5d300 5f 53 49 43 4b 20 20 20 20 20 30 78 34 62 37 37  _SICK     0x4b77
5d310 31 32 39 30 20 20 2f 2a 20 45 72 72 6f 72 20 61  1290  /* Error a
5d320 6e 64 20 61 77 61 69 74 69 6e 67 20 63 6c 6f 73  nd awaiting clos
5d330 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
5d340 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 20  ITE_MAGIC_BUSY  
5d350 20 20 20 30 78 66 30 33 62 37 39 30 36 20 20 2f     0xf03b7906  /
5d360 2a 20 44 61 74 61 62 61 73 65 20 63 75 72 72 65  * Database curre
5d370 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23  ntly in use */.#
5d380 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
5d390 47 49 43 5f 45 52 52 4f 52 20 20 20 20 30 78 62  GIC_ERROR    0xb
5d3a0 35 33 35 37 39 33 30 20 20 2f 2a 20 41 6e 20 53  5357930  /* An S
5d3b0 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72  QLITE_MISUSE err
5d3c0 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a  or occurred */..
5d3d0 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 66  /*.** Each SQL f
5d3e0 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 66 69 6e  unction is defin
5d3f0 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63  ed by an instanc
5d400 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
5d410 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ng.** structure.
5d420 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74    A pointer to t
5d430 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
5d440 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73   stored in the s
5d450 71 6c 69 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68  qlite.aFunc.** h
5d460 61 73 68 20 74 61 62 6c 65 2e 20 20 57 68 65 6e  ash table.  When
5d470 20 6d 75 6c 74 69 70 6c 65 20 66 75 6e 63 74 69   multiple functi
5d480 6f 6e 73 20 68 61 76 65 20 74 68 65 20 73 61 6d  ons have the sam
5d490 65 20 6e 61 6d 65 2c 20 74 68 65 20 68 61 73 68  e name, the hash
5d4a0 20 74 61 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73   table.** points
5d4b0 20 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   to a linked lis
5d4c0 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63  t of these struc
5d4d0 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  tures..*/.struct
5d4e0 20 46 75 6e 63 44 65 66 20 7b 0a 20 20 69 31 36   FuncDef {.  i16
5d4f0 20 6e 41 72 67 3b 20 20 20 20 20 20 20 20 20 20   nArg;          
5d500 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
5d510 72 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65  rguments.  -1 me
5d520 61 6e 73 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f  ans unlimited */
5d530 0a 20 20 75 38 20 69 50 72 65 66 45 6e 63 3b 20  .  u8 iPrefEnc; 
5d540 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65          /* Prefe
5d550 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69  rred text encodi
5d560 6e 67 20 28 53 51 4c 49 54 45 5f 55 54 46 38 2c  ng (SQLITE_UTF8,
5d570 20 31 36 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a   16LE, 16BE) */.
5d580 20 20 75 38 20 66 6c 61 67 73 3b 20 20 20 20 20    u8 flags;     
5d590 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63         /* Some c
5d5a0 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 53 51  ombination of SQ
5d5b0 4c 49 54 45 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20  LITE_FUNC_* */. 
5d5c0 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74 61   void *pUserData
5d5d0 3b 20 20 20 20 20 2f 2a 20 55 73 65 72 20 64 61  ;     /* User da
5d5e0 74 61 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a  ta parameter */.
5d5f0 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 78 74    FuncDef *pNext
5d600 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 66  ;      /* Next f
5d610 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 73 61 6d  unction with sam
5d620 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64  e name */.  void
5d630 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
5d640 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
5d650 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b  qlite3_value**);
5d660 20 2f 2a 20 52 65 67 75 6c 61 72 20 66 75 6e 63   /* Regular func
5d670 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28  tion */.  void (
5d680 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f  *xStep)(sqlite3_
5d690 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
5d6a0 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f  ite3_value**); /
5d6b0 2a 20 41 67 67 72 65 67 61 74 65 20 73 74 65 70  * Aggregate step
5d6c0 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 69   */.  void (*xFi
5d6d0 6e 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f  nalize)(sqlite3_
5d6e0 63 6f 6e 74 65 78 74 2a 29 3b 20 20 20 20 20 20  context*);      
5d6f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67            /* Agg
5d700 72 65 67 61 74 65 20 66 69 6e 61 6c 69 7a 65 72  regate finalizer
5d710 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
5d720 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51  e;         /* SQ
5d730 4c 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75  L name of the fu
5d740 6e 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e  nction. */.  Fun
5d750 63 44 65 66 20 2a 70 48 61 73 68 3b 20 20 20 20  cDef *pHash;    
5d760 20 20 2f 2a 20 4e 65 78 74 20 77 69 74 68 20 61    /* Next with a
5d770 20 64 69 66 66 65 72 65 6e 74 20 6e 61 6d 65 20   different name 
5d780 62 75 74 20 74 68 65 20 73 61 6d 65 20 68 61 73  but the same has
5d790 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50  h */.};../*.** P
5d7a0 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66  ossible values f
5d7b0 6f 72 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73  or FuncDef.flags
5d7c0 0a 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 4c 49 4b 45 20 20 20 20  TE_FUNC_LIKE    
5d7e0 20 30 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61   0x01 /* Candida
5d7f0 74 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20  te for the LIKE 
5d800 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a  optimization */.
5d810 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
5d820 55 4e 43 5f 43 41 53 45 20 20 20 20 20 30 78 30  UNC_CASE     0x0
5d830 32 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74  2 /* Case-sensit
5d840 69 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75  ive LIKE-type fu
5d850 6e 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  nction */.#defin
5d860 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50  e SQLITE_FUNC_EP
5d870 48 45 4d 20 20 20 20 30 78 30 34 20 2f 2a 20 45  HEM    0x04 /* E
5d880 70 68 65 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74  phemeral.  Delet
5d890 65 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 23  e with VDBE */.#
5d8a0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
5d8b0 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38  NC_NEEDCOLL 0x08
5d8c0 20 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 46 75   /* sqlite3GetFu
5d8d0 6e 63 43 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68  ncCollSeq() migh
5d8e0 74 20 62 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 23  t be called */.#
5d8f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
5d900 4e 43 5f 50 52 49 56 41 54 45 20 20 30 78 31 30  NC_PRIVATE  0x10
5d910 20 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6f 72 20   /* Allowed for 
5d920 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c  internal use onl
5d930 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  y */.#define SQL
5d940 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 20 20  ITE_FUNC_COUNT  
5d950 20 20 30 78 32 30 20 2f 2a 20 42 75 69 6c 74 2d    0x20 /* Built-
5d960 69 6e 20 63 6f 75 6e 74 28 2a 29 20 61 67 67 72  in count(*) aggr
5d970 65 67 61 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65  egate */.#define
5d980 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41   SQLITE_FUNC_COA
5d990 4c 45 53 43 45 20 30 78 34 30 20 2f 2a 20 42 75  LESCE 0x40 /* Bu
5d9a0 69 6c 74 2d 69 6e 20 63 6f 61 6c 65 73 63 65 28  ilt-in coalesce(
5d9b0 29 20 6f 72 20 69 66 6e 75 6c 6c 28 29 20 66 75  ) or ifnull() fu
5d9c0 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  nction */../*.**
5d9d0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
5d9e0 68 72 65 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e  hree macros, FUN
5d9f0 43 54 49 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e  CTION(), LIKEFUN
5da00 43 28 29 20 61 6e 64 20 41 47 47 52 45 47 41 54  C() and AGGREGAT
5da10 45 28 29 20 61 72 65 0a 2a 2a 20 75 73 65 64 20  E() are.** used 
5da20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 69 6e  to create the in
5da30 69 74 69 61 6c 69 7a 65 72 73 20 66 6f 72 20 74  itializers for t
5da40 68 65 20 46 75 6e 63 44 65 66 20 73 74 72 75 63  he FuncDef struc
5da50 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46  tures..**.**   F
5da60 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e  UNCTION(zName, n
5da70 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20  Arg, iArg, bNC, 
5da80 78 46 75 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73  xFunc).**     Us
5da90 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73  ed to create a s
5daa0 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64  calar function d
5dab0 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66  efinition of a f
5dac0 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a  unction zName .*
5dad0 2a 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65  *     implemente
5dae0 64 20 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20  d by C function 
5daf0 78 46 75 6e 63 20 74 68 61 74 20 61 63 63 65 70  xFunc that accep
5db00 74 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74  ts nArg argument
5db10 73 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20 76 61  s. The.**     va
5db20 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 69 41  lue passed as iA
5db30 72 67 20 69 73 20 63 61 73 74 20 74 6f 20 61 20  rg is cast to a 
5db40 28 76 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65  (void*) and made
5db50 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20   available.**   
5db60 20 20 61 73 20 74 68 65 20 75 73 65 72 2d 64 61    as the user-da
5db70 74 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72  ta (sqlite3_user
5db80 5f 64 61 74 61 28 29 29 20 66 6f 72 20 74 68 65  _data()) for the
5db90 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a   function. If .*
5dba0 2a 20 20 20 20 20 61 72 67 75 6d 65 6e 74 20 62  *     argument b
5dbb0 4e 43 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  NC is true, then
5dbc0 20 74 68 65 20 53 51 4c 49 54 45 5f 46 55 4e 43   the SQLITE_FUNC
5dbd0 5f 4e 45 45 44 43 4f 4c 4c 20 66 6c 61 67 20 69  _NEEDCOLL flag i
5dbe0 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41  s set..**.**   A
5dbf0 47 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20  GGREGATE(zName, 
5dc00 6e 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c  nArg, iArg, bNC,
5dc10 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a   xStep, xFinal).
5dc20 2a 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63  **     Used to c
5dc30 72 65 61 74 65 20 61 6e 20 61 67 67 72 65 67 61  reate an aggrega
5dc40 74 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69  te function defi
5dc50 6e 69 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nition implement
5dc60 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 74 68 65  ed by.**     the
5dc70 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 78 53 74   C functions xSt
5dc80 65 70 20 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54  ep and xFinal. T
5dc90 68 65 20 66 69 72 73 74 20 66 6f 75 72 20 70 61  he first four pa
5dca0 72 61 6d 65 74 65 72 73 0a 2a 2a 20 20 20 20 20  rameters.**     
5dcb0 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20  are interpreted 
5dcc0 69 6e 20 74 68 65 20 73 61 6d 65 20 77 61 79 20  in the same way 
5dcd0 61 73 20 74 68 65 20 66 69 72 73 74 20 34 20 70  as the first 4 p
5dce0 61 72 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20  arameters to.** 
5dcf0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a      FUNCTION()..
5dd00 2a 2a 0a 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43  **.**   LIKEFUNC
5dd10 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41  (zName, nArg, pA
5dd20 72 67 2c 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20  rg, flags).**   
5dd30 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65    Used to create
5dd40 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69   a scalar functi
5dd50 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66  on definition of
5dd60 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d   a function zNam
5dd70 65 20 0a 2a 2a 20 20 20 20 20 74 68 61 74 20 61  e .**     that a
5dd80 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75  ccepts nArg argu
5dd90 6d 65 6e 74 73 20 61 6e 64 20 69 73 20 69 6d 70  ments and is imp
5dda0 6c 65 6d 65 6e 74 65 64 20 62 79 20 61 20 63 61  lemented by a ca
5ddb0 6c 6c 20 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20  ll to C .**     
5ddc0 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e  function likeFun
5ddd0 63 2e 20 41 72 67 75 6d 65 6e 74 20 70 41 72 67  c. Argument pArg
5dde0 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76   is cast to a (v
5ddf0 6f 69 64 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a  oid *) and made.
5de00 2a 2a 20 20 20 20 20 61 76 61 69 6c 61 62 6c 65  **     available
5de10 20 61 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   as the function
5de20 20 75 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69   user-data (sqli
5de30 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29  te3_user_data())
5de40 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e  . The.**     Fun
5de50 63 44 65 66 2e 66 6c 61 67 73 20 76 61 72 69 61  cDef.flags varia
5de60 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20 74 68  ble is set to th
5de70 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61  e value passed a
5de80 73 20 74 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20  s the flags.**  
5de90 20 20 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f     parameter..*/
5dea0 0a 23 64 65 66 69 6e 65 20 46 55 4e 43 54 49 4f  .#define FUNCTIO
5deb0 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69  N(zName, nArg, i
5dec0 41 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29  Arg, bNC, xFunc)
5ded0 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49   \.  {nArg, SQLI
5dee0 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c  TE_UTF8, bNC*SQL
5def0 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c  ITE_FUNC_NEEDCOL
5df00 4c 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49  L, \.   SQLITE_I
5df10 4e 54 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 2c  NT_TO_PTR(iArg),
5df20 20 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c   0, xFunc, 0, 0,
5df30 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66   #zName, 0}.#def
5df40 69 6e 65 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e  ine STR_FUNCTION
5df50 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41  (zName, nArg, pA
5df60 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20  rg, bNC, xFunc) 
5df70 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54  \.  {nArg, SQLIT
5df80 45 5f 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49  E_UTF8, bNC*SQLI
5df90 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c  TE_FUNC_NEEDCOLL
5dfa0 2c 20 5c 0a 20 20 20 70 41 72 67 2c 20 30 2c 20  , \.   pArg, 0, 
5dfb0 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e  xFunc, 0, 0, #zN
5dfc0 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20  ame, 0}.#define 
5dfd0 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20  LIKEFUNC(zName, 
5dfe0 6e 41 72 67 2c 20 61 72 67 2c 20 66 6c 61 67 73  nArg, arg, flags
5dff0 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c  ) \.  {nArg, SQL
5e000 49 54 45 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c  ITE_UTF8, flags,
5e010 20 28 76 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c   (void *)arg, 0,
5e020 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c   likeFunc, 0, 0,
5e030 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66   #zName, 0}.#def
5e040 69 6e 65 20 41 47 47 52 45 47 41 54 45 28 7a 4e  ine AGGREGATE(zN
5e050 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20  ame, nArg, arg, 
5e060 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61  nc, xStep, xFina
5e070 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51  l) \.  {nArg, SQ
5e080 4c 49 54 45 5f 55 54 46 38 2c 20 6e 63 2a 53 51  LITE_UTF8, nc*SQ
5e090 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f  LITE_FUNC_NEEDCO
5e0a0 4c 4c 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f  LL, \.   SQLITE_
5e0b0 49 4e 54 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c  INT_TO_PTR(arg),
5e0c0 20 30 2c 20 30 2c 20 78 53 74 65 70 2c 78 46 69   0, 0, xStep,xFi
5e0d0 6e 61 6c 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f  nal,#zName,0}../
5e0e0 2a 0a 2a 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74  *.** All current
5e0f0 20 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20   savepoints are 
5e100 73 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b  stored in a link
5e110 65 64 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67  ed list starting
5e120 20 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70   at.** sqlite3.p
5e130 53 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 20 66  Savepoint. The f
5e140 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20  irst element in 
5e150 74 68 65 20 6c 69 73 74 20 69 73 20 74 68 65 20  the list is the 
5e160 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a  most recently.**
5e170 20 6f 70 65 6e 65 64 20 73 61 76 65 70 6f 69 6e   opened savepoin
5e180 74 2e 20 53 61 76 65 70 6f 69 6e 74 73 20 61 72  t. Savepoints ar
5e190 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c  e added to the l
5e1a0 69 73 74 20 62 79 20 74 68 65 20 76 64 62 65 0a  ist by the vdbe.
5e1b0 2a 2a 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20  ** OP_Savepoint 
5e1c0 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  instruction..*/.
5e1d0 73 74 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74  struct Savepoint
5e1e0 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65   {.  char *zName
5e1f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5e200 20 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65           /* Save
5e210 70 6f 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d  point name (nul-
5e220 74 65 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20  terminated) */. 
5e230 20 69 36 34 20 6e 44 65 66 65 72 72 65 64 43 6f   i64 nDeferredCo
5e240 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ns;             
5e250 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
5e260 66 20 64 65 66 65 72 72 65 64 20 66 6b 20 76 69  f deferred fk vi
5e270 6f 6c 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 53 61  olations */.  Sa
5e280 76 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20  vepoint *pNext; 
5e290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e2a0 20 20 2f 2a 20 50 61 72 65 6e 74 20 73 61 76 65    /* Parent save
5e2b0 70 6f 69 6e 74 20 28 69 66 20 61 6e 79 29 20 2a  point (if any) *
5e2c0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  /.};../*.** The 
5e2d0 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75 73  following are us
5e2e0 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  ed as the second
5e2f0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
5e300 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 29  lite3Savepoint()
5e310 2c 0a 2a 2a 20 61 6e 64 20 61 73 20 74 68 65 20  ,.** and as the 
5e320 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  P1 argument to t
5e330 68 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20  he OP_Savepoint 
5e340 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  instruction..*/.
5e350 23 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e  #define SAVEPOIN
5e360 54 5f 42 45 47 49 4e 20 20 20 20 20 20 30 0a 23  T_BEGIN      0.#
5e370 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54  define SAVEPOINT
5e380 5f 52 45 4c 45 41 53 45 20 20 20 20 31 0a 23 64  _RELEASE    1.#d
5e390 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f  efine SAVEPOINT_
5e3a0 52 4f 4c 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f  ROLLBACK   2.../
5e3b0 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65  *.** Each SQLite
5e3c0 20 6d 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c   module (virtual
5e3d0 20 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f   table definitio
5e3e0 6e 29 20 69 73 20 64 65 66 69 6e 65 64 20 62 79  n) is defined by
5e3f0 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20   an.** instance 
5e400 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
5e410 20 73 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72   structure, stor
5e420 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ed in the sqlite
5e430 33 2e 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73  3.aModule.** has
5e440 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75  h table..*/.stru
5e450 63 74 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f  ct Module {.  co
5e460 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
5e470 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20  le *pModule;    
5e480 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70     /* Callback p
5e490 6f 69 6e 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e  ointers */.  con
5e4a0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  st char *zName; 
5e4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e4c0 20 20 2f 2a 20 4e 61 6d 65 20 70 61 73 73 65 64    /* Name passed
5e4d0 20 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c   to create_modul
5e4e0 65 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  e() */.  void *p
5e4f0 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Aux;            
5e500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5e510 20 70 41 75 78 20 70 61 73 73 65 64 20 74 6f 20   pAux passed to 
5e520 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20  create_module() 
5e530 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73  */.  void (*xDes
5e540 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 3b 20 20  troy)(void *);  
5e550 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64            /* Mod
5e560 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66  ule destructor f
5e570 75 6e 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f  unction */.};../
5e580 2a 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  *.** information
5e590 20 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75   about each colu
5e5a0 6d 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62  mn of an SQL tab
5e5b0 6c 65 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e  le is held in an
5e5c0 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20   instance.** of 
5e5d0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a  this structure..
5e5e0 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e  */.struct Column
5e5f0 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65   {.  char *zName
5e600 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ;     /* Name of
5e610 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   this column */.
5e620 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20    Expr *pDflt;  
5e630 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61     /* Default va
5e640 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75  lue of this colu
5e650 6d 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44  mn */.  char *zD
5e660 66 6c 74 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67  flt;     /* Orig
5e670 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65  inal text of the
5e680 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a   default value *
5e690 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b  /.  char *zType;
5e6a0 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70       /* Data typ
5e6b0 65 20 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d  e for this colum
5e6c0 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f  n */.  char *zCo
5e6d0 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61  ll;     /* Colla
5e6e0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20  ting sequence.  
5e6f0 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65  If NULL, use the
5e700 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38   default */.  u8
5e710 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f   notNull;      /
5e720 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20  * True if there 
5e730 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  is a NOT NULL co
5e740 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38  nstraint */.  u8
5e750 20 69 73 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f   isPrimKey;    /
5e760 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63  * True if this c
5e770 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66  olumn is part of
5e780 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
5e790 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e   */.  char affin
5e7a0 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66  ity;   /* One of
5e7b0 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f   the SQLITE_AFF_
5e7c0 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69  ... values */.#i
5e7d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
5e7e0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
5e7f0 20 75 38 20 69 73 48 69 64 64 65 6e 3b 20 20 20   u8 isHidden;   
5e800 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
5e810 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64  s column is 'hid
5e820 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  den' */.#endif.}
5e830 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c  ;../*.** A "Coll
5e840 61 74 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20  ating Sequence" 
5e850 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e  is defined by an
5e860 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
5e870 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74   following.** st
5e880 72 75 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74  ructure. Concept
5e890 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69  ually, a collati
5e8a0 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73  ng sequence cons
5e8b0 69 73 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61  ists of a name a
5e8c0 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73  nd.** a comparis
5e8d0 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20  on routine that 
5e8e0 64 65 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65  defines the orde
5e8f0 72 20 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e  r of that sequen
5e900 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  ce..**.** There 
5e910 6d 61 79 20 74 77 6f 20 73 65 70 61 72 61 74 65  may two separate
5e920 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
5e930 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f   of the collatio
5e940 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a  n function, one.
5e950 2a 2a 20 74 68 61 74 20 70 72 6f 63 65 73 73 65  ** that processe
5e960 73 20 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20  s text in UTF-8 
5e970 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65  encoding (CollSe
5e980 71 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74  q.xCmp) and anot
5e990 68 65 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63  her that.** proc
5e9a0 65 73 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64  esses text encod
5e9b0 65 64 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f  ed in UTF-16 (Co
5e9c0 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75  llSeq.xCmp16), u
5e9d0 73 69 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65  sing the machine
5e9e0 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20  .** native byte 
5e9f0 6f 72 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f  order. When a co
5ea00 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
5ea10 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c   is invoked, SQL
5ea20 69 74 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74  ite selects.** t
5ea30 68 65 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20  he version that 
5ea40 77 69 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65  will require the
5ea50 20 6c 65 61 73 74 20 65 78 70 65 6e 73 69 76 65   least expensive
5ea60 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61   encoding.** tra
5ea70 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e  nslations, if an
5ea80 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c  y..**.** The Col
5ea90 6c 53 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65  lSeq.pUser membe
5eaa0 72 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e  r variable is an
5eab0 20 65 78 74 72 61 20 70 61 72 61 6d 65 74 65 72   extra parameter
5eac0 20 74 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a   that passed in.
5ead0 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ** as the first 
5eae0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
5eaf0 55 54 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e  UTF-8 comparison
5eb00 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e   function, xCmp.
5eb10 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65  .** CollSeq.pUse
5eb20 72 31 36 20 69 73 20 74 68 65 20 65 71 75 69 76  r16 is the equiv
5eb30 61 6c 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54  alent for the UT
5eb40 46 2d 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20  F-16 comparison 
5eb50 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d  function,.** xCm
5eb60 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f  p16..**.** If bo
5eb70 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20  th CollSeq.xCmp 
5eb80 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70  and CollSeq.xCmp
5eb90 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20  16 are NULL, it 
5eba0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a  means that the.*
5ebb0 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * collating sequ
5ebc0 65 6e 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65  ence is undefine
5ebd0 64 2e 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c  d.  Indices buil
5ebe0 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65  t on an undefine
5ebf0 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73  d.** collating s
5ec00 65 71 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20  equence may not 
5ec10 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74  be read or writt
5ec20 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f  en..*/.struct Co
5ec30 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a  llSeq {.  char *
5ec40 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
5ec50 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
5ec60 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
5ec70 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  e, UTF-8 encoded
5ec80 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20   */.  u8 enc;   
5ec90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
5eca0 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e  ext encoding han
5ecb0 64 6c 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a  dled by xCmp() *
5ecc0 2f 0a 20 20 75 38 20 74 79 70 65 3b 20 20 20 20  /.  u8 type;    
5ecd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
5ece0 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43   of the SQLITE_C
5ecf0 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62  OLL_... values b
5ed00 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  elow */.  void *
5ed10 70 55 73 65 72 3b 20 20 20 20 20 20 20 20 20 20  pUser;          
5ed20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e  /* First argumen
5ed30 74 20 74 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20  t to xCmp() */. 
5ed40 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69   int (*xCmp)(voi
5ed50 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f  d*,int, const vo
5ed60 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  id*, int, const 
5ed70 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28  void*);.  void (
5ed80 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20  *xDel)(void*);  
5ed90 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f  /* Destructor fo
5eda0 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f  r pUser */.};../
5edb0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
5edc0 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74  ues of CollSeq.t
5edd0 79 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ype:.*/.#define 
5ede0 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41  SQLITE_COLL_BINA
5edf0 52 59 20 20 31 20 20 2f 2a 20 54 68 65 20 64 65  RY  1  /* The de
5ee00 66 61 75 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63  fault memcmp() c
5ee10 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
5ee20 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
5ee30 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20  ITE_COLL_NOCASE 
5ee40 20 32 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74   2  /* The built
5ee50 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61  -in NOCASE colla
5ee60 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f  ting sequence */
5ee70 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5ee80 43 4f 4c 4c 5f 52 45 56 45 52 53 45 20 33 20 20  COLL_REVERSE 3  
5ee90 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20  /* The built-in 
5eea0 52 45 56 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e  REVERSE collatin
5eeb0 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64  g sequence */.#d
5eec0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c  efine SQLITE_COL
5eed0 4c 5f 55 53 45 52 20 20 20 20 30 20 20 2f 2a 20  L_USER    0  /* 
5eee0 41 6e 79 20 6f 74 68 65 72 20 75 73 65 72 2d 64  Any other user-d
5eef0 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67  efined collating
5ef00 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a   sequence */../*
5ef10 0a 2a 2a 20 41 20 73 6f 72 74 20 6f 72 64 65 72  .** A sort order
5ef20 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 41   can be either A
5ef30 53 43 20 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23  SC or DESC..*/.#
5ef40 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f  define SQLITE_SO
5ef50 5f 41 53 43 20 20 20 20 20 20 20 30 20 20 2f 2a  _ASC       0  /*
5ef60 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69   Sort in ascendi
5ef70 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66  ng order */.#def
5ef80 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45  ine SQLITE_SO_DE
5ef90 53 43 20 20 20 20 20 20 31 20 20 2f 2a 20 53 6f  SC      1  /* So
5efa0 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20  rt in ascending 
5efb0 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  order */../*.** 
5efc0 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  Column affinity 
5efd0 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  types..**.** The
5efe0 73 65 20 75 73 65 64 20 74 6f 20 68 61 76 65 20  se used to have 
5eff0 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69  mnemonic name li
5f000 6b 65 20 27 69 27 20 66 6f 72 20 53 51 4c 49 54  ke 'i' for SQLIT
5f010 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 61 6e  E_AFF_INTEGER an
5f020 64 0a 2a 2a 20 27 74 27 20 66 6f 72 20 53 51 4c  d.** 't' for SQL
5f030 49 54 45 5f 41 46 46 5f 54 45 58 54 2e 20 20 42  ITE_AFF_TEXT.  B
5f040 75 74 20 77 65 20 63 61 6e 20 73 61 76 65 20 61  ut we can save a
5f050 20 6c 69 74 74 6c 65 20 73 70 61 63 65 20 61 6e   little space an
5f060 64 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65  d improve.** the
5f070 20 73 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20   speed a little 
5f080 62 79 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65  by numbering the
5f090 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74   values consecut
5f0a0 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42  ively.  .**.** B
5f0b0 75 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 73  ut rather than s
5f0c0 74 61 72 74 20 77 69 74 68 20 30 20 6f 72 20 31  tart with 0 or 1
5f0d0 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74 68 20  , we begin with 
5f0e0 27 61 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0a  'a'.  That way,.
5f0f0 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65  ** when multiple
5f100 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 20   affinity types 
5f110 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 65 64  are concatenated
5f120 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 61   into a string a
5f130 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20 74 68  nd.** used as th
5f140 65 20 50 34 20 6f 70 65 72 61 6e 64 2c 20 74 68  e P4 operand, th
5f150 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20  ey will be more 
5f160 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  readable..**.** 
5f170 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 74  Note also that t
5f180 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 73  he numeric types
5f190 20 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67   are grouped tog
5f1a0 65 74 68 65 72 20 73 6f 20 74 68 61 74 20 74 65  ether so that te
5f1b0 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e  sting.** for a n
5f1c0 75 6d 65 72 69 63 20 74 79 70 65 20 69 73 20 61  umeric type is a
5f1d0 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73   single comparis
5f1e0 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  on..*/.#define S
5f1f0 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20  QLITE_AFF_TEXT  
5f200 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65 20 53     'a'.#define S
5f210 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20  QLITE_AFF_NONE  
5f220 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65 20 53     'b'.#define S
5f230 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
5f240 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65 20 53  C  'c'.#define S
5f250 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
5f260 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65 20 53  R  'd'.#define S
5f270 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20  QLITE_AFF_REAL  
5f280 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e 65 20     'e'..#define 
5f290 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63  sqlite3IsNumeric
5f2a0 41 66 66 69 6e 69 74 79 28 58 29 20 20 28 28 58  Affinity(X)  ((X
5f2b0 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  )>=SQLITE_AFF_NU
5f2c0 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  MERIC)../*.** Th
5f2d0 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53  e SQLITE_AFF_MAS
5f2e0 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f  K values masks o
5f2f0 66 66 20 74 68 65 20 73 69 67 6e 69 66 69 63 61  ff the significa
5f300 6e 74 20 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a  nt bits of an.**
5f310 20 61 66 66 69 6e 69 74 79 20 76 61 6c 75 65 2e   affinity value.
5f320 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c   .*/.#define SQL
5f330 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20  ITE_AFF_MASK    
5f340 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64   0x67../*.** Add
5f350 69 74 69 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75  itional bit valu
5f360 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 4f  es that can be O
5f370 52 65 64 20 77 69 74 68 20 61 6e 20 61 66 66 69  Red with an affi
5f380 6e 69 74 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20  nity without.** 
5f390 63 68 61 6e 67 69 6e 67 20 74 68 65 20 61 66 66  changing the aff
5f3a0 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e  inity..*/.#defin
5f3b0 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  e SQLITE_JUMPIFN
5f3c0 55 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a 20 6a  ULL   0x08  /* j
5f3d0 75 6d 70 73 20 69 66 20 65 69 74 68 65 72 20 6f  umps if either o
5f3e0 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a  perand is NULL *
5f3f0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5f400 5f 53 54 4f 52 45 50 32 20 20 20 20 20 20 30 78  _STOREP2      0x
5f410 31 30 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73  10  /* Store res
5f420 75 6c 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72  ult in reg[P2] r
5f430 61 74 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20  ather than jump 
5f440 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
5f450 45 5f 4e 55 4c 4c 45 51 20 20 20 20 20 20 20 30  E_NULLEQ       0
5f460 78 38 30 20 20 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c  x80  /* NULL=NUL
5f470 4c 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f  L */../*.** An o
5f480 62 6a 65 63 74 20 6f 66 20 74 68 69 73 20 74 79  bject of this ty
5f490 70 65 20 69 73 20 63 72 65 61 74 65 64 20 66 6f  pe is created fo
5f4a0 72 20 65 61 63 68 20 76 69 72 74 75 61 6c 20 74  r each virtual t
5f4b0 61 62 6c 65 20 70 72 65 73 65 6e 74 20 69 6e 0a  able present in.
5f4c0 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
5f4d0 73 63 68 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 49  schema. .**.** I
5f4e0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  f the database s
5f4f0 63 68 65 6d 61 20 69 73 20 73 68 61 72 65 64 2c  chema is shared,
5f500 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f   then there is o
5f510 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ne instance of t
5f520 68 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  his.** structure
5f530 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61   for each databa
5f540 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 73  se connection (s
5f550 71 6c 69 74 65 33 2a 29 20 74 68 61 74 20 75 73  qlite3*) that us
5f560 65 73 20 74 68 65 20 73 68 61 72 65 64 0a 2a 2a  es the shared.**
5f570 20 73 63 68 65 6d 61 2e 20 54 68 69 73 20 69 73   schema. This is
5f580 20 62 65 63 61 75 73 65 20 65 61 63 68 20 64 61   because each da
5f590 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
5f5a0 6e 20 72 65 71 75 69 72 65 73 20 69 74 73 20 6f  n requires its o
5f5b0 77 6e 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 73  wn unique.** ins
5f5c0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c  tance of the sql
5f5d0 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c  ite3_vtab* handl
5f5e0 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  e used to access
5f5f0 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
5f600 6c 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  le .** implement
5f610 61 74 69 6f 6e 2e 20 73 71 6c 69 74 65 33 5f 76  ation. sqlite3_v
5f620 74 61 62 2a 20 68 61 6e 64 6c 65 73 20 63 61 6e  tab* handles can
5f630 20 6e 6f 74 20 62 65 20 73 68 61 72 65 64 20 62   not be shared b
5f640 65 74 77 65 65 6e 20 0a 2a 2a 20 64 61 74 61 62  etween .** datab
5f650 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c  ase connections,
5f660 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65 20 72   even when the r
5f670 65 73 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65  est of the in-me
5f680 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 0a 2a  mory database .*
5f690 2a 20 73 63 68 65 6d 61 20 69 73 20 73 68 61 72  * schema is shar
5f6a0 65 64 2c 20 61 73 20 74 68 65 20 69 6d 70 6c 65  ed, as the imple
5f6b0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 74 65 6e 20  mentation often 
5f6c0 73 74 6f 72 65 73 20 74 68 65 20 64 61 74 61 62  stores the datab
5f6d0 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  ase.** connectio
5f6e0 6e 20 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20  n handle passed 
5f6f0 74 6f 20 69 74 20 76 69 61 20 74 68 65 20 78 43  to it via the xC
5f700 6f 6e 6e 65 63 74 28 29 20 6f 72 20 78 43 72 65  onnect() or xCre
5f710 61 74 65 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20  ate() method.** 
5f720 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a  during initializ
5f730 61 74 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79  ation internally
5f740 2e 20 54 68 69 73 20 64 61 74 61 62 61 73 65 20  . This database 
5f750 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  connection handl
5f760 65 20 6d 61 79 0a 2a 2a 20 74 68 65 6e 20 75 73  e may.** then us
5f770 65 64 20 62 79 20 74 68 65 20 76 69 72 74 75 61  ed by the virtua
5f780 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  l table implemen
5f790 74 61 74 69 6f 6e 20 74 6f 20 61 63 63 65 73 73  tation to access
5f7a0 20 72 65 61 6c 20 74 61 62 6c 65 73 20 0a 2a 2a   real tables .**
5f7b0 20 77 69 74 68 69 6e 20 74 68 65 20 64 61 74 61   within the data
5f7c0 62 61 73 65 2e 20 53 6f 20 74 68 61 74 20 74 68  base. So that th
5f7d0 65 79 20 61 70 70 65 61 72 20 61 73 20 70 61 72  ey appear as par
5f7e0 74 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 73  t of the callers
5f7f0 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e   .** transaction
5f800 2c 20 74 68 65 73 65 20 61 63 63 65 73 73 65 73  , these accesses
5f810 20 6e 65 65 64 20 74 6f 20 62 65 20 6d 61 64 65   need to be made
5f820 20 76 69 61 20 74 68 65 20 73 61 6d 65 20 64 61   via the same da
5f830 74 61 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65  tabase .** conne
5f840 63 74 69 6f 6e 20 61 73 20 74 68 61 74 20 75 73  ction as that us
5f850 65 64 20 74 6f 20 65 78 65 63 75 74 65 20 53 51  ed to execute SQ
5f860 4c 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20  L operations on 
5f870 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
5f880 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 56 54 61  e..**.** All VTa
5f890 62 6c 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74  ble objects that
5f8a0 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 61   correspond to a
5f8b0 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e   single table in
5f8c0 20 61 20 73 68 61 72 65 64 0a 2a 2a 20 64 61 74   a shared.** dat
5f8d0 61 62 61 73 65 20 73 63 68 65 6d 61 20 61 72 65  abase schema are
5f8e0 20 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65   initially store
5f8f0 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 2d 6c 69  d in a linked-li
5f900 73 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  st pointed to by
5f910 0a 2a 2a 20 74 68 65 20 54 61 62 6c 65 2e 70 56  .** the Table.pV
5f920 54 61 62 6c 65 20 6d 65 6d 62 65 72 20 76 61 72  Table member var
5f930 69 61 62 6c 65 20 6f 66 20 74 68 65 20 63 6f 72  iable of the cor
5f940 72 65 73 70 6f 6e 64 69 6e 67 20 54 61 62 6c 65  responding Table
5f950 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 57 68 65 6e   object..** When
5f960 20 61 6e 20 73 71 6c 69 74 65 33 5f 70 72 65 70   an sqlite3_prep
5f970 61 72 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 20  are() operation 
5f980 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 61  is required to a
5f990 63 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61  ccess the virtua
5f9a0 6c 0a 2a 2a 20 74 61 62 6c 65 2c 20 69 74 20 73  l.** table, it s
5f9b0 65 61 72 63 68 65 73 20 74 68 65 20 6c 69 73 74  earches the list
5f9c0 20 66 6f 72 20 74 68 65 20 56 54 61 62 6c 65 20   for the VTable 
5f9d0 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73  that corresponds
5f9e0 20 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   to the.** datab
5f9f0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64  ase connection d
5fa00 6f 69 6e 67 20 74 68 65 20 70 72 65 70 61 72 69  oing the prepari
5fa10 6e 67 20 73 6f 20 61 73 20 74 6f 20 75 73 65 20  ng so as to use 
5fa20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 73  the correct.** s
5fa30 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e  qlite3_vtab* han
5fa40 64 6c 65 20 69 6e 20 74 68 65 20 63 6f 6d 70 69  dle in the compi
5fa50 6c 65 64 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a  led query..**.**
5fa60 20 57 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f   When an in-memo
5fa70 72 79 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20  ry Table object 
5fa80 69 73 20 64 65 6c 65 74 65 64 20 28 66 6f 72 20  is deleted (for 
5fa90 65 78 61 6d 70 6c 65 20 77 68 65 6e 20 74 68 65  example when the
5faa0 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 62 65  .** schema is be
5fab0 69 6e 67 20 72 65 6c 6f 61 64 65 64 20 66 6f 72  ing reloaded for
5fac0 20 73 6f 6d 65 20 72 65 61 73 6f 6e 29 2c 20 74   some reason), t
5fad0 68 65 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74  he VTable object
5fae0 73 20 61 72 65 20 6e 6f 74 20 0a 2a 2a 20 64 65  s are not .** de
5faf0 6c 65 74 65 64 20 61 6e 64 20 74 68 65 20 73 71  leted and the sq
5fb00 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64  lite3_vtab* hand
5fb10 6c 65 73 20 61 72 65 20 6e 6f 74 20 78 44 69 73  les are not xDis
5fb20 63 6f 6e 6e 65 63 74 28 29 65 64 20 0a 2a 2a 20  connect()ed .** 
5fb30 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 73  immediately. Ins
5fb40 74 65 61 64 2c 20 74 68 65 79 20 61 72 65 20 6d  tead, they are m
5fb50 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 54 61  oved from the Ta
5fb60 62 6c 65 2e 70 56 54 61 62 6c 65 20 6c 69 73 74  ble.pVTable list
5fb70 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 6c   to.** another l
5fb80 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65  inked list heade
5fb90 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  d by the sqlite3
5fba0 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6d 65 6d  .pDisconnect mem
5fbb0 62 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  ber of the.** co
5fbc0 72 72 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69  rresponding sqli
5fbd0 74 65 33 20 73 74 72 75 63 74 75 72 65 2e 20 54  te3 structure. T
5fbe0 68 65 79 20 61 72 65 20 74 68 65 6e 20 64 65 6c  hey are then del
5fbf0 65 74 65 64 2f 78 44 69 73 63 6f 6e 6e 65 63 74  eted/xDisconnect
5fc00 65 64 20 0a 2a 2a 20 6e 65 78 74 20 74 69 6d 65  ed .** next time
5fc10 20 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20   a statement is 
5fc20 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73  prepared using s
5fc30 61 69 64 20 73 71 6c 69 74 65 33 2a 2e 20 54 68  aid sqlite3*. Th
5fc40 69 73 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f  is is done.** to
5fc50 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 20   avoid deadlock 
5fc60 69 73 73 75 65 73 20 69 6e 76 6f 6c 76 69 6e 67  issues involving
5fc70 20 6d 75 6c 74 69 70 6c 65 20 73 71 6c 69 74 65   multiple sqlite
5fc80 33 2e 6d 75 74 65 78 20 6d 75 74 65 78 65 73 2e  3.mutex mutexes.
5fc90 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 63 6f 6d  .** Refer to com
5fca0 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63  ments above func
5fcb0 74 69 6f 6e 20 73 71 6c 69 74 65 33 56 74 61 62  tion sqlite3Vtab
5fcc0 55 6e 6c 6f 63 6b 4c 69 73 74 28 29 20 66 6f 72  UnlockList() for
5fcd0 20 61 6e 0a 2a 2a 20 65 78 70 6c 61 6e 61 74 69   an.** explanati
5fce0 6f 6e 20 61 73 20 74 6f 20 77 68 79 20 69 74 20  on as to why it 
5fcf0 69 73 20 73 61 66 65 20 74 6f 20 61 64 64 20 61  is safe to add a
5fd00 6e 20 65 6e 74 72 79 20 74 6f 20 61 6e 20 73 71  n entry to an sq
5fd10 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63  lite3.pDisconnec
5fd20 74 0a 2a 2a 20 6c 69 73 74 20 77 69 74 68 6f 75  t.** list withou
5fd30 74 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 63 6f  t holding the co
5fd40 72 72 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69  rresponding sqli
5fd50 74 65 33 2e 6d 75 74 65 78 20 6d 75 74 65 78 2e  te3.mutex mutex.
5fd60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72  .**.** The memor
5fd70 79 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 6f 66  y for objects of
5fd80 20 74 68 69 73 20 74 79 70 65 20 69 73 20 61 6c   this type is al
5fd90 77 61 79 73 20 61 6c 6c 6f 63 61 74 65 64 20 62  ways allocated b
5fda0 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d  y .** sqlite3DbM
5fdb0 61 6c 6c 6f 63 28 29 2c 20 75 73 69 6e 67 20 74  alloc(), using t
5fdc0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  he connection ha
5fdd0 6e 64 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 56  ndle stored in V
5fde0 54 61 62 6c 65 2e 64 62 20 61 73 20 0a 2a 2a 20  Table.db as .** 
5fdf0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
5fe00 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 54  nt..*/.struct VT
5fe10 61 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33  able {.  sqlite3
5fe20 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
5fe30 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
5fe40 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 73 6f 63 69  onnection associ
5fe50 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74  ated with this t
5fe60 61 62 6c 65 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65  able */.  Module
5fe70 20 2a 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 20   *pMod;         
5fe80 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
5fe90 6f 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  o module impleme
5fea0 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c  ntation */.  sql
5feb0 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
5fec0 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  ;      /* Pointe
5fed0 72 20 74 6f 20 76 74 61 62 20 69 6e 73 74 61 6e  r to vtab instan
5fee0 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66  ce */.  int nRef
5fef0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5ff00 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
5ff10 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20  ointers to this 
5ff20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 56  structure */.  V
5ff30 54 61 62 6c 65 20 2a 70 4e 65 78 74 3b 20 20 20  Table *pNext;   
5ff40 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
5ff50 20 69 6e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20   in linked list 
5ff60 28 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 7d  (see above) */.}
5ff70 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51  ;../*.** Each SQ
5ff80 4c 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 65  L table is repre
5ff90 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79  sented in memory
5ffa0 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   by an instance 
5ffb0 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77  of the.** follow
5ffc0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
5ffd0 2a 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65  *.** Table.zName
5ffe0 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
5fff0 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20  the table.  The 
60000 63 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69 67  case of the orig
60010 69 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54  inal.** CREATE T
60020 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69  ABLE statement i
60030 73 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63 61  s stored, but ca
60040 73 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 66  se is not signif
60050 69 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d  icant for.** com
60060 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  parisons..**.** 
60070 54 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68  Table.nCol is th
60080 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
60090 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c  mns in this tabl
600a0 65 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69  e.  Table.aCol i
600b0 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74  s a.** pointer t
600c0 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43 6f  o an array of Co
600d0 6c 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73 2c  lumn structures,
600e0 20 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f   one for each co
600f0 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  lumn..**.** If t
60100 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20  he table has an 
60110 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
60120 4b 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e  KEY, then Table.
60130 69 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e 64  iPKey is the ind
60140 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c  ex of.** the col
60150 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61 74  umn that is that
60160 20 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69 73   key.   Otherwis
60170 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73  e Table.iPKey is
60180 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65   negative.  Note
60190 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 74  .** that the dat
601a0 61 74 79 70 65 20 6f 66 20 74 68 65 20 50 52 49  atype of the PRI
601b0 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65  MARY KEY must be
601c0 20 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68 69   INTEGER for thi
601d0 73 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65  s field to.** be
601e0 20 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 45   set.  An INTEGE
601f0 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73  R PRIMARY KEY is
60200 20 75 73 65 64 20 61 73 20 74 68 65 20 72 6f 77   used as the row
60210 69 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20  id for each row 
60220 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e  of.** the table.
60230 20 20 49 66 20 61 20 74 61 62 6c 65 20 68 61 73    If a table has
60240 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d   no INTEGER PRIM
60250 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 20  ARY KEY, then a 
60260 72 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20  random rowid.** 
60270 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72  is generated for
60280 20 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65   each row of the
60290 20 74 61 62 6c 65 2e 20 20 54 46 5f 48 61 73 50   table.  TF_HasP
602a0 72 69 6d 61 72 79 4b 65 79 20 69 73 20 73 65 74  rimaryKey is set
602b0 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65   if.** the table
602c0 20 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52 59   has any PRIMARY
602d0 20 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f 72   KEY, INTEGER or
602e0 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a   otherwise..**.*
602f0 2a 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20  * Table.tnum is 
60300 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
60310 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54 72  for the root BTr
60320 65 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 74  ee page of the t
60330 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64  able in the.** d
60340 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49  atabase file.  I
60350 66 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20 74  f Table.iDb is t
60360 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
60370 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 62  database table b
60380 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c  ackend.** in sql
60390 69 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73  ite.aDb[].  0 is
603a0 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   for the main da
603b0 74 61 62 61 73 65 20 61 6e 64 20 31 20 69 73 20  tabase and 1 is 
603c0 66 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68 61  for the file tha
603d0 74 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f  t.** holds tempo
603e0 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20  rary tables and 
603f0 69 6e 64 69 63 65 73 2e 20 20 49 66 20 54 46 5f  indices.  If TF_
60400 45 70 68 65 6d 65 72 61 6c 20 69 73 20 73 65 74  Ephemeral is set
60410 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 61 62  .** then the tab
60420 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  le is stored in 
60430 61 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 61  a file that is a
60440 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c  utomatically del
60450 65 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65  eted.** when the
60460 20 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f 20   VDBE cursor to 
60470 74 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c 6f  the table is clo
60480 73 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63 61  sed.  In this ca
60490 73 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a 2a  se Table.tnum .*
604a0 2a 20 72 65 66 65 72 73 20 56 44 42 45 20 63 75  * refers VDBE cu
604b0 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 74  rsor number that
604c0 20 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c 65   holds the table
604d0 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68   open, not to th
604e0 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 6e  e root.** page n
604f0 75 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65 6e  umber.  Transien
60500 74 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 65  t tables are use
60510 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65  d to hold the re
60520 73 75 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73 75  sults of a.** su
60530 62 2d 71 75 65 72 79 20 74 68 61 74 20 61 70 70  b-query that app
60540 65 61 72 73 20 69 6e 73 74 65 61 64 20 6f 66 20  ears instead of 
60550 61 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61 6d  a real table nam
60560 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  e in the FROM cl
60570 61 75 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53 45  ause .** of a SE
60580 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
60590 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 20  */.struct Table 
605a0 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4d  {.  sqlite3 *dbM
605b0 65 6d 3b 20 20 20 20 20 20 2f 2a 20 44 42 20 63  em;      /* DB c
605c0 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 65 64 20 66  onnection used f
605d0 6f 72 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c  or lookaside all
605e0 6f 63 61 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 63  ocations. */.  c
605f0 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
60600 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
60610 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77  he table or view
60620 20 2a 2f 0a 20 20 69 6e 74 20 69 50 4b 65 79 3b   */.  int iPKey;
60630 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
60640 20 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 75   not negative, u
60650 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79 5d 20 61  se aCol[iPKey] a
60660 73 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65  s the primary ke
60670 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  y */.  int nCol;
60680 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
60690 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
606a0 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a   in this table *
606b0 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c  /.  Column *aCol
606c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f  ;        /* Info
606d0 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61  rmation about ea
606e0 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49  ch column */.  I
606f0 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20  ndex *pIndex;   
60700 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53      /* List of S
60710 51 4c 20 69 6e 64 65 78 65 73 20 6f 6e 20 74 68  QL indexes on th
60720 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 69  is table. */.  i
60730 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20  nt tnum;        
60740 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42 54 72 65      /* Root BTre
60750 65 20 6e 6f 64 65 20 66 6f 72 20 74 68 69 73 20  e node for this 
60760 74 61 62 6c 65 20 28 73 65 65 20 6e 6f 74 65 20  table (see note 
60770 61 62 6f 76 65 29 20 2a 2f 0a 20 20 53 65 6c 65  above) */.  Sele
60780 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20  ct *pSelect;    
60790 20 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20 74 61 62   /* NULL for tab
607a0 6c 65 73 2e 20 20 50 6f 69 6e 74 73 20 74 6f 20  les.  Points to 
607b0 64 65 66 69 6e 69 74 69 6f 6e 20 69 66 20 61 20  definition if a 
607c0 76 69 65 77 2e 20 2a 2f 0a 20 20 75 31 36 20 6e  view. */.  u16 n
607d0 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
607e0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69  /* Number of poi
607f0 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 54 61  nters to this Ta
60800 62 6c 65 20 2a 2f 0a 20 20 75 38 20 74 61 62 46  ble */.  u8 tabF
60810 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a  lags;         /*
60820 20 4d 61 73 6b 20 6f 66 20 54 46 5f 2a 20 76 61   Mask of TF_* va
60830 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20 6b 65 79  lues */.  u8 key
60840 43 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f  Conf;          /
60850 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 69 6e 20  * What to do in 
60860 63 61 73 65 20 6f 66 20 75 6e 69 71 75 65 6e 65  case of uniquene
60870 73 73 20 63 6f 6e 66 6c 69 63 74 20 6f 6e 20 69  ss conflict on i
60880 50 4b 65 79 20 2a 2f 0a 20 20 46 4b 65 79 20 2a  PKey */.  FKey *
60890 70 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20 2f  pFKey;         /
608a0 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  * Linked list of
608b0 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79   all foreign key
608c0 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20  s in this table 
608d0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41  */.  char *zColA
608e0 66 66 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 72  ff;       /* Str
608f0 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65  ing defining the
60900 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63   affinity of eac
60910 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 69 66 6e  h column */.#ifn
60920 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
60930 43 48 45 43 4b 0a 20 20 45 78 70 72 20 2a 70 43  CHECK.  Expr *pC
60940 68 65 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  heck;        /* 
60950 54 68 65 20 41 4e 44 20 6f 66 20 61 6c 6c 20 43  The AND of all C
60960 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73  HECK constraints
60970 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64   */.#endif.#ifnd
60980 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
60990 4c 54 45 52 54 41 42 4c 45 0a 20 20 69 6e 74 20  LTERTABLE.  int 
609a0 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b 20 20 20  addColOffset;   
609b0 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 43 52   /* Offset in CR
609c0 45 41 54 45 20 54 41 42 4c 45 20 73 74 6d 74 20  EATE TABLE stmt 
609d0 74 6f 20 61 64 64 20 61 20 6e 65 77 20 63 6f 6c  to add a new col
609e0 75 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  umn */.#endif.#i
609f0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
60a00 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
60a10 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65   VTable *pVTable
60a20 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ;     /* List of
60a30 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e   VTable objects.
60a40 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f 64 75 6c   */.  int nModul
60a50 65 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 4e 75  eArg;      /* Nu
60a60 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
60a70 73 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20  s to the module 
60a80 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f  */.  char **azMo
60a90 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20 54 65 78  duleArg;  /* Tex
60aa0 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20  t of all module 
60ab0 61 72 67 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64  args. [0] is mod
60ac0 75 6c 65 20 6e 61 6d 65 20 2a 2f 0a 23 65 6e 64  ule name */.#end
60ad0 69 66 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  if.  Trigger *pT
60ae0 72 69 67 67 65 72 3b 20 20 20 2f 2a 20 4c 69 73  rigger;   /* Lis
60af0 74 20 6f 66 20 74 72 69 67 67 65 72 73 20 73 74  t of triggers st
60b00 6f 72 65 64 20 69 6e 20 70 53 63 68 65 6d 61 20  ored in pSchema 
60b10 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63  */.  Schema *pSc
60b20 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68  hema;     /* Sch
60b30 65 6d 61 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ema that contain
60b40 73 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a  s this table */.
60b50 20 20 54 61 62 6c 65 20 2a 70 4e 65 78 74 5a 6f    Table *pNextZo
60b60 6d 62 69 65 3b 20 20 2f 2a 20 4e 65 78 74 20 6f  mbie;  /* Next o
60b70 6e 20 74 68 65 20 50 61 72 73 65 2e 70 5a 6f 6d  n the Parse.pZom
60b80 62 69 65 54 61 62 20 6c 69 73 74 20 2a 2f 0a 7d  bieTab list */.}
60b90 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ;../*.** Allowed
60ba0 20 76 61 6c 75 65 73 20 66 6f 72 20 54 61 62 65   values for Tabe
60bb0 2e 74 61 62 46 6c 61 67 73 2e 0a 2a 2f 0a 23 64  .tabFlags..*/.#d
60bc0 65 66 69 6e 65 20 54 46 5f 52 65 61 64 6f 6e 6c  efine TF_Readonl
60bd0 79 20 20 20 20 20 20 20 20 30 78 30 31 20 20 20  y        0x01   
60be0 20 2f 2a 20 52 65 61 64 2d 6f 6e 6c 79 20 73 79   /* Read-only sy
60bf0 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 23 64  stem table */.#d
60c00 65 66 69 6e 65 20 54 46 5f 45 70 68 65 6d 65 72  efine TF_Ephemer
60c10 61 6c 20 20 20 20 20 20 20 30 78 30 32 20 20 20  al       0x02   
60c20 20 2f 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c   /* An ephemeral
60c30 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e   table */.#defin
60c40 65 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b  e TF_HasPrimaryK
60c50 65 79 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20  ey   0x04    /* 
60c60 54 61 62 6c 65 20 68 61 73 20 61 20 70 72 69 6d  Table has a prim
60c70 61 72 79 20 6b 65 79 20 2a 2f 0a 23 64 65 66 69  ary key */.#defi
60c80 6e 65 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d  ne TF_Autoincrem
60c90 65 6e 74 20 20 20 30 78 30 38 20 20 20 20 2f 2a  ent   0x08    /*
60ca0 20 49 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79   Integer primary
60cb0 20 6b 65 79 20 69 73 20 61 75 74 6f 69 6e 63 72   key is autoincr
60cc0 65 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  ement */.#define
60cd0 20 54 46 5f 56 69 72 74 75 61 6c 20 20 20 20 20   TF_Virtual     
60ce0 20 20 20 20 30 78 31 30 20 20 20 20 2f 2a 20 49      0x10    /* I
60cf0 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  s a virtual tabl
60d00 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f  e */.#define TF_
60d10 4e 65 65 64 4d 65 74 61 64 61 74 61 20 20 20 20  NeedMetadata    
60d20 30 78 32 30 20 20 20 20 2f 2a 20 61 43 6f 6c 5b  0x20    /* aCol[
60d30 5d 2e 7a 54 79 70 65 20 61 6e 64 20 61 43 6f 6c  ].zType and aCol
60d40 5b 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73 69 6e 67  [].pColl missing
60d50 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 65 73   */..../*.** Tes
60d60 74 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72  t to see whether
60d70 20 6f 72 20 6e 6f 74 20 61 20 74 61 62 6c 65 20   or not a table 
60d80 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  is a virtual tab
60d90 6c 65 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20  le.  This is.** 
60da0 64 6f 6e 65 20 61 73 20 61 20 6d 61 63 72 6f 20  done as a macro 
60db0 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20  so that it will 
60dc0 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 6f 75 74  be optimized out
60dd0 20 77 68 65 6e 20 76 69 72 74 75 61 6c 0a 2a 2a   when virtual.**
60de0 20 74 61 62 6c 65 20 73 75 70 70 6f 72 74 20 69   table support i
60df0 73 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74  s omitted from t
60e00 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66  he build..*/.#if
60e10 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
60e20 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20  _VIRTUALTABLE.# 
60e30 20 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61   define IsVirtua
60e40 6c 28 58 29 20 20 20 20 20 20 28 28 28 58 29 2d  l(X)      (((X)-
60e50 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56  >tabFlags & TF_V
60e60 69 72 74 75 61 6c 29 21 3d 30 29 0a 23 20 20 64  irtual)!=0).#  d
60e70 65 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f  efine IsHiddenCo
60e80 6c 75 6d 6e 28 58 29 20 28 28 58 29 2d 3e 69 73  lumn(X) ((X)->is
60e90 48 69 64 64 65 6e 29 0a 23 65 6c 73 65 0a 23 20  Hidden).#else.# 
60ea0 20 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61   define IsVirtua
60eb0 6c 28 58 29 20 20 20 20 20 20 30 0a 23 20 20 64  l(X)      0.#  d
60ec0 65 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f  efine IsHiddenCo
60ed0 6c 75 6d 6e 28 58 29 20 30 0a 23 65 6e 64 69 66  lumn(X) 0.#endif
60ee0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72  ../*.** Each for
60ef0 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
60f00 69 6e 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  int is an instan
60f10 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
60f20 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
60f30 2a 0a 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b  *.** A foreign k
60f40 65 79 20 69 73 20 61 73 73 6f 63 69 61 74 65 64  ey is associated
60f50 20 77 69 74 68 20 74 77 6f 20 74 61 62 6c 65 73   with two tables
60f60 2e 20 20 54 68 65 20 22 66 72 6f 6d 22 20 74 61  .  The "from" ta
60f70 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 74 61  ble is.** the ta
60f80 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ble that contain
60f90 73 20 74 68 65 20 52 45 46 45 52 45 4e 43 45 53  s the REFERENCES
60fa0 20 63 6c 61 75 73 65 20 74 68 61 74 20 63 72 65   clause that cre
60fb0 61 74 65 73 20 74 68 65 20 66 6f 72 65 69 67 6e  ates the foreign
60fc0 0a 2a 2a 20 6b 65 79 2e 20 20 54 68 65 20 22 74  .** key.  The "t
60fd0 6f 22 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  o" table is the 
60fe0 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61  table that is na
60ff0 6d 65 64 20 69 6e 20 74 68 65 20 52 45 46 45 52  med in the REFER
61000 45 4e 43 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a  ENCES clause..**
61010 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 65   Consider this e
61020 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20  xample:.**.**   
61030 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 65    CREATE TABLE e
61040 78 31 28 0a 2a 2a 20 20 20 20 20 20 20 61 20 49  x1(.**       a I
61050 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
61060 45 59 2c 0a 2a 2a 20 20 20 20 20 20 20 62 20 49  EY,.**       b I
61070 4e 54 45 47 45 52 20 43 4f 4e 53 54 52 41 49 4e  NTEGER CONSTRAIN
61080 54 20 66 6b 31 20 52 45 46 45 52 45 4e 43 45 53  T fk1 REFERENCES
61090 20 65 78 32 28 78 29 0a 2a 2a 20 20 20 20 20 29   ex2(x).**     )
610a0 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65  ;.**.** For fore
610b0 69 67 6e 20 6b 65 79 20 22 66 6b 31 22 2c 20 74  ign key "fk1", t
610c0 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73  he from-table is
610d0 20 22 65 78 31 22 20 61 6e 64 20 74 68 65 20 74   "ex1" and the t
610e0 6f 2d 74 61 62 6c 65 20 69 73 20 22 65 78 32 22  o-table is "ex2"
610f0 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46  ..**.** Each REF
61100 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67  ERENCES clause g
61110 65 6e 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74  enerates an inst
61120 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
61130 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a  owing structure.
61140 2a 2a 20 77 68 69 63 68 20 69 73 20 61 74 74 61  ** which is atta
61150 63 68 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d  ched to the from
61160 2d 74 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d  -table.  The to-
61170 74 61 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65  table need not e
61180 78 69 73 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65  xist when.** the
61190 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63   from-table is c
611a0 72 65 61 74 65 64 2e 20 20 54 68 65 20 65 78 69  reated.  The exi
611b0 73 74 65 6e 63 65 20 6f 66 20 74 68 65 20 74 6f  stence of the to
611c0 2d 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 68  -table is not ch
611d0 65 63 6b 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74  ecked..*/.struct
611e0 20 46 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20   FKey {.  Table 
611f0 2a 70 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54  *pFrom;     /* T
61200 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
61210 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 63  the REFERENCES c
61220 6c 61 75 73 65 20 28 61 6b 61 3a 20 43 68 69 6c  lause (aka: Chil
61230 64 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e  d) */.  FKey *pN
61240 65 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78  extFrom;  /* Nex
61250 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e  t foreign key in
61260 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72   pFrom */.  char
61270 20 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a   *zTo;        /*
61280 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74   Name of table t
61290 68 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e  hat the key poin
612a0 74 73 20 74 6f 20 28 61 6b 61 3a 20 50 61 72 65  ts to (aka: Pare
612b0 6e 74 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70  nt) */.  FKey *p
612c0 4e 65 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65  NextTo;    /* Ne
612d0 78 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f  xt foreign key o
612e0 6e 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54  n table named zT
612f0 6f 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 50 72  o */.  FKey *pPr
61300 65 76 54 6f 3b 20 20 20 20 2f 2a 20 50 72 65 76  evTo;    /* Prev
61310 69 6f 75 73 20 66 6f 72 65 69 67 6e 20 6b 65 79  ious foreign key
61320 20 6f 6e 20 74 61 62 6c 65 20 6e 61 6d 65 64 20   on table named 
61330 7a 54 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  zTo */.  int nCo
61340 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  l;         /* Nu
61350 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
61360 69 6e 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20  in this key */. 
61370 20 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d   /* EV: R-30323-
61380 32 31 39 31 37 20 2a 2f 0a 20 20 75 38 20 69 73  21917 */.  u8 is
61390 44 65 66 65 72 72 65 64 3b 20 20 20 20 2f 2a 20  Deferred;    /* 
613a0 54 72 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69  True if constrai
613b0 6e 74 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64  nt checking is d
613c0 65 66 65 72 72 65 64 20 74 69 6c 6c 20 43 4f 4d  eferred till COM
613d0 4d 49 54 20 2a 2f 0a 20 20 75 38 20 61 41 63 74  MIT */.  u8 aAct
613e0 69 6f 6e 5b 32 5d 3b 20 20 20 20 20 20 20 20 20  ion[2];         
613f0 20 2f 2a 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e   /* ON DELETE an
61400 64 20 4f 4e 20 55 50 44 41 54 45 20 61 63 74 69  d ON UPDATE acti
61410 6f 6e 73 2c 20 72 65 73 70 65 63 74 69 76 65 6c  ons, respectivel
61420 79 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  y */.  Trigger *
61430 61 70 54 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f  apTrigger[2];  /
61440 2a 20 54 72 69 67 67 65 72 73 20 66 6f 72 20 61  * Triggers for a
61450 41 63 74 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73  Action[] actions
61460 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 43 6f   */.  struct sCo
61470 6c 4d 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69  lMap {  /* Mappi
61480 6e 67 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  ng of columns in
61490 20 70 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e   pFrom to column
614a0 73 20 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20  s in zTo */.    
614b0 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20  int iFrom;      
614c0 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
614d0 6f 6c 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a  olumn in pFrom *
614e0 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c  /.    char *zCol
614f0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ;        /* Name
61500 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54   of column in zT
61510 6f 2e 20 20 49 66 20 30 20 75 73 65 20 50 52 49  o.  If 0 use PRI
61520 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20  MARY KEY */.  } 
61530 61 43 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20  aCol[1];        
61540 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72  /* One entry for
61550 20 65 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f   each of nCol co
61560 6c 75 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  lumn s */.};../*
61570 0a 2a 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f  .** SQLite suppo
61580 72 74 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65  rts many differe
61590 6e 74 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c  nt ways to resol
615a0 76 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a  ve a constraint.
615b0 2a 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42  ** error.  ROLLB
615c0 41 43 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d  ACK processing m
615d0 65 61 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73  eans that a cons
615e0 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
615f0 0a 2a 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f  .** causes the o
61600 70 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63  peration in proc
61610 65 73 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20  ess to fail and 
61620 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
61630 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74  transaction.** t
61640 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  o be rolled back
61650 2e 20 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73  .  ABORT process
61660 69 6e 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70  ing means the op
61670 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65  eration in proce
61680 73 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20  ss.** fails and 
61690 61 6e 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65  any prior change
616a0 73 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20  s from that one 
616b0 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61  operation are ba
616c0 63 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74  cked out,.** but
616d0 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
616e0 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62   is not rolled b
616f0 61 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65  ack.  FAIL proce
61700 73 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74  ssing means that
61710 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f  .** the operatio
61720 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74  n in progress st
61730 6f 70 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ops and returns 
61740 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20  an error code.  
61750 42 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61  But prior.** cha
61760 6e 67 65 73 20 64 75 65 20 74 6f 20 74 68 65 20  nges due to the 
61770 73 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61  same operation a
61780 72 65 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75  re not backed ou
61790 74 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63  t and no rollbac
617a0 6b 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47  k.** occurs.  IG
617b0 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20  NORE means that 
617c0 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 72  the particular r
617d0 6f 77 20 74 68 61 74 20 63 61 75 73 65 64 20 74  ow that caused t
617e0 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a  he constraint.**
617f0 20 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e   error is not in
61800 73 65 72 74 65 64 20 6f 72 20 75 70 64 61 74 65  serted or update
61810 64 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63  d.  Processing c
61820 6f 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20  ontinues and no 
61830 65 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75  error.** is retu
61840 72 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d  rned.  REPLACE m
61850 65 61 6e 73 20 74 68 61 74 20 70 72 65 65 78 69  eans that preexi
61860 73 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 72  sting database r
61870 6f 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a  ows that caused.
61880 2a 2a 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73  ** a UNIQUE cons
61890 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
618a0 20 61 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20   are removed so 
618b0 74 68 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73  that the new ins
618c0 65 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65  ert or.** update
618d0 20 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50   can proceed.  P
618e0 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e  rocessing contin
618f0 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72  ues and no error
61900 20 69 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a   is reported..**
61910 0a 2a 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45  .** RESTRICT, SE
61920 54 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41  TNULL, and CASCA
61930 44 45 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79  DE actions apply
61940 20 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e   only to foreign
61950 20 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49   keys..** RESTRI
61960 43 54 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  CT is the same a
61970 73 20 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45  s ABORT for IMME
61980 44 49 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65  DIATE foreign ke
61990 79 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61  ys and the.** sa
619a0 6d 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66  me as ROLLBACK f
619b0 6f 72 20 44 45 46 45 52 52 45 44 20 6b 65 79 73  or DEFERRED keys
619c0 2e 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73  .  SETNULL means
619d0 20 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67   that the foreig
619e0 6e 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20  n.** key is set 
619f0 74 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44  to NULL.  CASCAD
61a00 45 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44  E means that a D
61a10 45 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20  ELETE or UPDATE 
61a20 6f 66 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65  of the.** refere
61a30 6e 63 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69  nced table row i
61a40 73 20 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74  s propagated int
61a50 6f 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 68  o the row that h
61a60 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65  olds the.** fore
61a70 69 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20  ign key..** .** 
61a80 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79  The following sy
61a90 6d 62 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72  mbolic values ar
61aa0 65 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64  e used to record
61ab0 20 77 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f   which type.** o
61ac0 66 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65  f action to take
61ad0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f  ..*/.#define OE_
61ae0 4e 6f 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20  None     0   /* 
61af0 54 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73  There is no cons
61b00 74 72 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20  traint to check 
61b10 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f  */.#define OE_Ro
61b20 6c 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61  llback 1   /* Fa
61b30 69 6c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  il the operation
61b40 20 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68   and rollback th
61b50 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  e transaction */
61b60 0a 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72  .#define OE_Abor
61b70 74 20 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b  t    2   /* Back
61b80 20 6f 75 74 20 63 68 61 6e 67 65 73 20 62 75 74   out changes but
61b90 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20   do no rollback 
61ba0 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23  transaction */.#
61bb0 64 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20  define OE_Fail  
61bc0 20 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74     3   /* Stop t
61bd0 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74  he operation but
61be0 20 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72   leave all prior
61bf0 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66   changes */.#def
61c00 69 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20  ine OE_Ignore   
61c10 34 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68  4   /* Ignore th
61c20 65 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20  e error. Do not 
61c30 64 6f 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72  do the INSERT or
61c40 20 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69   UPDATE */.#defi
61c50 6e 65 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35  ne OE_Replace  5
61c60 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69     /* Delete exi
61c70 73 74 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68  sting record, th
61c80 65 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20  en do INSERT or 
61c90 55 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69  UPDATE */..#defi
61ca0 6e 65 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36  ne OE_Restrict 6
61cb0 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66     /* OE_Abort f
61cc0 6f 72 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45  or IMMEDIATE, OE
61cd0 5f 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45  _Rollback for DE
61ce0 46 45 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e  FERRED */.#defin
61cf0 65 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20  e OE_SetNull  7 
61d00 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72    /* Set the for
61d10 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74  eign key value t
61d20 6f 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e  o NULL */.#defin
61d30 65 20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20  e OE_SetDflt  8 
61d40 20 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72    /* Set the for
61d50 65 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74  eign key value t
61d60 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f  o its default */
61d70 0a 23 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63  .#define OE_Casc
61d80 61 64 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63  ade  9   /* Casc
61d90 61 64 65 20 74 68 65 20 63 68 61 6e 67 65 73 20  ade the changes 
61da0 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44  */..#define OE_D
61db0 65 66 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44  efault  99  /* D
61dc0 6f 20 77 68 61 74 65 76 65 72 20 74 68 65 20 64  o whatever the d
61dd0 65 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73  efault action is
61de0 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69   */.../*.** An i
61df0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
61e00 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
61e10 72 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20  re is passed as 
61e20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67  the first.** arg
61e30 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33  ument to sqlite3
61e40 56 64 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61  VdbeKeyCompare a
61e50 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  nd is used to co
61e60 6e 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f  ntrol the .** co
61e70 6d 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20  mparison of the 
61e80 74 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a  two index keys..
61e90 2a 2f 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66  */.struct KeyInf
61ea0 6f 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  o {.  sqlite3 *d
61eb0 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  b;        /* The
61ec0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
61ed0 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63  tion */.  u8 enc
61ee0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
61ef0 20 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d   Text encoding -
61f00 20 6f 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54   one of the TEXT
61f10 5f 55 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a  _Utf* values */.
61f20 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20    u16 nField;   
61f30 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
61f40 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43  of entries in aC
61f50 6f 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61  oll[] */.  u8 *a
61f60 53 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f  SortOrder;     /
61f70 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20  * If defined an 
61f80 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73  aSortOrder[i] is
61f90 20 74 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43   true, sort DESC
61fa0 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61   */.  CollSeq *a
61fb0 43 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c  Coll[1];  /* Col
61fc0 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
61fd0 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66  for each term of
61fe0 20 74 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a   the key */.};..
61ff0 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*.** An instanc
62000 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
62010 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c  ng structure hol
62020 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ds information a
62030 62 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65  bout a.** single
62040 20 69 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68   index record th
62050 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  at has already b
62060 65 65 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69  een parsed out i
62070 6e 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a  nto individual.*
62080 2a 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20  * values..**.** 
62090 41 20 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f  A record is an o
620a0 62 6a 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61  bject that conta
620b0 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ins one or more 
620c0 66 69 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a  fields of data..
620d0 2a 2a 20 52 65 63 6f 72 64 73 20 61 72 65 20 75  ** Records are u
620e0 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65  sed to store the
620f0 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61   content of a ta
62100 62 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73  ble row and to s
62110 74 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20  tore.** the key 
62120 6f 66 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20  of an index.  A 
62130 62 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66  blob encoding of
62140 20 61 20 72 65 63 6f 72 64 20 69 73 20 63 72 65   a record is cre
62150 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f  ated by.** the O
62160 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63  P_MakeRecord opc
62170 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 20  ode of the VDBE 
62180 61 6e 64 20 69 73 20 64 69 73 61 73 73 65 6d 62  and is disassemb
62190 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50  led by the.** OP
621a0 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a  _Column opcode..
621b0 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63  **.** This struc
621c0 74 75 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63  ture holds a rec
621d0 6f 72 64 20 74 68 61 74 20 68 61 73 20 61 6c 72  ord that has alr
621e0 65 61 64 79 20 62 65 65 6e 20 64 69 73 61 73 73  eady been disass
621f0 65 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69  embled.** into i
62200 74 73 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66  ts constituent f
62210 69 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  ields..*/.struct
62220 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
62230 7b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  {.  KeyInfo *pKe
62240 79 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61  yInfo;  /* Colla
62250 74 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72  tion and sort-or
62260 64 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  der information 
62270 2a 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b  */.  u16 nField;
62280 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
62290 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
622a0 20 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31   apMem[] */.  u1
622b0 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  6 flags;        
622c0 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74    /* Boolean set
622d0 74 69 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44  tings.  UNPACKED
622e0 5f 2e 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20  _... below */.  
622f0 69 36 34 20 72 6f 77 69 64 3b 20 20 20 20 20 20  i64 rowid;      
62300 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 55      /* Used by U
62310 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53  NPACKED_PREFIX_S
62320 45 41 52 43 48 20 2a 2f 0a 20 20 4d 65 6d 20 2a  EARCH */.  Mem *
62330 61 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  aMem;          /
62340 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a  * Values */.};..
62350 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
62360 6c 75 65 73 20 6f 66 20 55 6e 70 61 63 6b 65 64  lues of Unpacked
62370 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a 2a 2f 0a  Record.flags.*/.
62380 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44  #define UNPACKED
62390 5f 4e 45 45 44 5f 46 52 45 45 20 20 20 20 20 30  _NEED_FREE     0
623a0 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d 6f 72 79  x0001  /* Memory
623b0 20 69 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 33   is from sqlite3
623c0 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66  Malloc() */.#def
623d0 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45  ine UNPACKED_NEE
623e0 44 5f 44 45 53 54 52 4f 59 20 20 30 78 30 30 30  D_DESTROY  0x000
623f0 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 73 20 73  2  /* apMem[]s s
62400 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20 64 65 73  hould all be des
62410 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65 66 69 6e  troyed */.#defin
62420 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52  e UNPACKED_IGNOR
62430 45 5f 52 4f 57 49 44 20 20 30 78 30 30 30 34 20  E_ROWID  0x0004 
62440 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72 61 69 6c   /* Ignore trail
62450 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20 6b 65 79  ing rowid on key
62460 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50  1 */.#define UNP
62470 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 20 20  ACKED_INCRKEY   
62480 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 4d      0x0008  /* M
62490 61 6b 65 20 74 68 69 73 20 6b 65 79 20 61 6e 20  ake this key an 
624a0 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65 72 20 2a  epsilon larger *
624b0 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b  /.#define UNPACK
624c0 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20  ED_PREFIX_MATCH 
624d0 20 30 78 30 30 31 30 20 20 2f 2a 20 41 20 70 72   0x0010  /* A pr
624e0 65 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 6f  efix match is co
624f0 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 23  nsidered OK */.#
62500 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f  define UNPACKED_
62510 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 30 78  PREFIX_SEARCH 0x
62520 30 30 32 30 20 20 2f 2a 20 41 20 70 72 65 66 69  0020  /* A prefi
62530 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69  x match is consi
62540 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a  dered OK */../*.
62550 2a 2a 20 45 61 63 68 20 53 51 4c 20 69 6e 64 65  ** Each SQL inde
62560 78 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  x is represented
62570 20 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e   in memory by an
62580 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
62590 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
625a0 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
625b0 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  he columns of th
625c0 65 20 74 61 62 6c 65 20 74 68 61 74 20 61 72 65  e table that are
625d0 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61   to be indexed a
625e0 72 65 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20  re described.** 
625f0 62 79 20 74 68 65 20 61 69 43 6f 6c 75 6d 6e 5b  by the aiColumn[
62600 5d 20 66 69 65 6c 64 20 6f 66 20 74 68 69 73 20  ] field of this 
62610 73 74 72 75 63 74 75 72 65 2e 20 20 46 6f 72 20  structure.  For 
62620 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65  example, suppose
62630 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 68 65 20  .** we have the 
62640 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20  following table 
62650 61 6e 64 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a  and index:.**.**
62660 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
62670 45 20 45 78 31 28 63 31 20 69 6e 74 2c 20 63 32  E Ex1(c1 int, c2
62680 20 69 6e 74 2c 20 63 33 20 74 65 78 74 29 3b 0a   int, c3 text);.
62690 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 49 4e  **     CREATE IN
626a0 44 45 58 20 45 78 32 20 4f 4e 20 45 78 31 28 63  DEX Ex2 ON Ex1(c
626b0 33 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20  3,c1);.**.** In 
626c0 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
626d0 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 20 45  ure describing E
626e0 78 31 2c 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61  x1, nCol==3 beca
626f0 75 73 65 20 74 68 65 72 65 20 61 72 65 0a 2a 2a  use there are.**
62700 20 74 68 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69   three columns i
62710 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e  n the table.  In
62720 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63   the Index struc
62730 74 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 0a  ture describing.
62740 2a 2a 20 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d  ** Ex2, nColumn=
62750 3d 32 20 73 69 6e 63 65 20 32 20 6f 66 20 74 68  =2 since 2 of th
62760 65 20 33 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45  e 3 columns of E
62770 78 31 20 61 72 65 20 69 6e 64 65 78 65 64 2e 0a  x1 are indexed..
62780 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
62790 61 69 43 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20  aiColumn is {2, 
627a0 30 7d 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d  0}.  aiColumn[0]
627b0 3d 3d 32 20 62 65 63 61 75 73 65 20 74 68 65 20  ==2 because the 
627c0 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e  .** first column
627d0 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28   to be indexed (
627e0 63 33 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78  c3) has an index
627f0 20 6f 66 20 32 20 69 6e 20 45 78 31 2e 61 43 6f   of 2 in Ex1.aCo
62800 6c 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f  l[]..** The seco
62810 6e 64 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20  nd column to be 
62820 69 6e 64 65 78 65 64 20 28 63 31 29 20 68 61 73  indexed (c1) has
62830 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 69   an index of 0 i
62840 6e 0a 2a 2a 20 45 78 31 2e 61 43 6f 6c 5b 5d 2c  n.** Ex1.aCol[],
62850 20 68 65 6e 63 65 20 45 78 32 2e 61 69 43 6f 6c   hence Ex2.aiCol
62860 75 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a  umn[1]==0..**.**
62870 20 54 68 65 20 49 6e 64 65 78 2e 6f 6e 45 72 72   The Index.onErr
62880 6f 72 20 66 69 65 6c 64 20 64 65 74 65 72 6d 69  or field determi
62890 6e 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e  nes whether or n
628a0 6f 74 20 74 68 65 20 69 6e 64 65 78 65 64 20 63  ot the indexed c
628b0 6f 6c 75 6d 6e 73 0a 2a 2a 20 6d 75 73 74 20 62  olumns.** must b
628c0 65 20 75 6e 69 71 75 65 20 61 6e 64 20 77 68 61  e unique and wha
628d0 74 20 74 6f 20 64 6f 20 69 66 20 74 68 65 79 20  t to do if they 
628e0 61 72 65 20 6e 6f 74 2e 20 20 57 68 65 6e 20 49  are not.  When I
628f0 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 3d 4f 45 5f  ndex.onError=OE_
62900 4e 6f 6e 65 2c 0a 2a 2a 20 69 74 20 6d 65 61 6e  None,.** it mean
62910 73 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20  s this is not a 
62920 75 6e 69 71 75 65 20 69 6e 64 65 78 2e 20 20 4f  unique index.  O
62930 74 68 65 72 77 69 73 65 20 69 74 20 69 73 20 61  therwise it is a
62940 20 75 6e 69 71 75 65 20 69 6e 64 65 78 0a 2a 2a   unique index.**
62950 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f   and the value o
62960 66 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20  f Index.onError 
62970 69 6e 64 69 63 61 74 65 20 74 68 65 20 77 68 69  indicate the whi
62980 63 68 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f  ch conflict reso
62990 6c 75 74 69 6f 6e 20 0a 2a 2a 20 61 6c 67 6f 72  lution .** algor
629a0 69 74 68 6d 20 74 6f 20 65 6d 70 6c 6f 79 20 77  ithm to employ w
629b0 68 65 6e 65 76 65 72 20 61 6e 20 61 74 74 65 6d  henever an attem
629c0 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e  pt is made to in
629d0 73 65 72 74 20 61 20 6e 6f 6e 2d 75 6e 69 71 75  sert a non-uniqu
629e0 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f  e.** element..*/
629f0 0a 73 74 72 75 63 74 20 49 6e 64 65 78 20 7b 0a  .struct Index {.
62a00 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
62a10 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
62a20 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  is index */.  in
62a30 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f  t nColumn;     /
62a40 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
62a50 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65  mns in the table
62a60 20 75 73 65 64 20 62 79 20 74 68 69 73 20 69 6e   used by this in
62a70 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69  dex */.  int *ai
62a80 43 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69  Column;   /* Whi
62a90 63 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75  ch columns are u
62aa0 73 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65  sed by this inde
62ab0 78 2e 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0a  x.  1st is 0 */.
62ac0 20 20 75 6e 73 69 67 6e 65 64 20 2a 61 69 52 6f    unsigned *aiRo
62ad0 77 45 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20  wEst; /* Result 
62ae0 6f 66 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e  of ANALYZE: Est.
62af0 20 72 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62   rows selected b
62b00 79 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f  y each column */
62b10 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65  .  Table *pTable
62b20 3b 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74  ;   /* The SQL t
62b30 61 62 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78  able being index
62b40 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d  ed */.  int tnum
62b50 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65  ;        /* Page
62b60 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74   containing root
62b70 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 69   of this index i
62b80 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
62b90 2a 2f 0a 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b  */.  u8 onError;
62ba0 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72        /* OE_Abor
62bb0 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45  t, OE_Ignore, OE
62bc0 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f  _Replace, or OE_
62bd0 4e 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 61 75 74  None */.  u8 aut
62be0 6f 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 54 72  oIndex;    /* Tr
62bf0 75 65 20 69 66 20 69 73 20 61 75 74 6f 6d 61 74  ue if is automat
62c00 69 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 28  ically created (
62c10 65 78 3a 20 62 79 20 55 4e 49 51 55 45 29 20 2a  ex: by UNIQUE) *
62c20 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66  /.  char *zColAf
62c30 66 3b 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64  f;   /* String d
62c40 65 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69  efining the affi
62c50 6e 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c  nity of each col
62c60 75 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  umn */.  Index *
62c70 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 54 68 65  pNext;    /* The
62c80 20 6e 65 78 74 20 69 6e 64 65 78 20 61 73 73 6f   next index asso
62c90 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
62ca0 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  same table */.  
62cb0 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b  Schema *pSchema;
62cc0 20 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61   /* Schema conta
62cd0 69 6e 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78  ining this index
62ce0 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f   */.  u8 *aSortO
62cf0 72 64 65 72 3b 20 20 2f 2a 20 41 72 72 61 79 20  rder;  /* Array 
62d00 6f 66 20 73 69 7a 65 20 49 6e 64 65 78 2e 6e 43  of size Index.nC
62d10 6f 6c 75 6d 6e 2e 20 54 72 75 65 3d 3d 44 45 53  olumn. True==DES
62d20 43 2c 20 46 61 6c 73 65 3d 3d 41 53 43 20 2a 2f  C, False==ASC */
62d30 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 6c  .  char **azColl
62d40 3b 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20  ;   /* Array of 
62d50 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
62d60 63 65 20 6e 61 6d 65 73 20 66 6f 72 20 69 6e 64  ce names for ind
62d70 65 78 20 2a 2f 0a 20 20 49 6e 64 65 78 53 61 6d  ex */.  IndexSam
62d80 70 6c 65 20 2a 61 53 61 6d 70 6c 65 3b 20 20 20  ple *aSample;   
62d90 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 53 51 4c   /* Array of SQL
62da0 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45  ITE_INDEX_SAMPLE
62db0 53 20 73 61 6d 70 6c 65 73 20 2a 2f 0a 7d 3b 0a  S samples */.};.
62dc0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 73 61 6d 70  ./*.** Each samp
62dd0 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  le stored in the
62de0 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61   sqlite_stat2 ta
62df0 62 6c 65 20 69 73 20 72 65 70 72 65 73 65 6e 74  ble is represent
62e00 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 0a 2a 2a  ed in memory .**
62e10 20 75 73 69 6e 67 20 61 20 73 74 72 75 63 74 75   using a structu
62e20 72 65 20 6f 66 20 74 68 69 73 20 74 79 70 65 2e  re of this type.
62e30 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 78  .*/.struct Index
62e40 53 61 6d 70 6c 65 20 7b 0a 20 20 75 6e 69 6f 6e  Sample {.  union
62e50 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20   {.    char *z; 
62e60 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
62e70 69 66 20 65 54 79 70 65 20 69 73 20 53 51 4c 49  if eType is SQLI
62e80 54 45 5f 54 45 58 54 20 6f 72 20 53 51 4c 49 54  TE_TEXT or SQLIT
62e90 45 5f 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 64 6f  E_BLOB */.    do
62ea0 75 62 6c 65 20 72 3b 20 20 20 20 20 20 20 2f 2a  uble r;       /*
62eb0 20 56 61 6c 75 65 20 69 66 20 65 54 79 70 65 20   Value if eType 
62ec0 69 73 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20  is SQLITE_FLOAT 
62ed0 6f 72 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  or SQLITE_INTEGE
62ee0 52 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 38  R */.  } u;.  u8
62ef0 20 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 20   eType;         
62f00 2f 2a 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20  /* SQLITE_NULL, 
62f10 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 2e  SQLITE_INTEGER .
62f20 2e 2e 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20  .. etc. */.  u8 
62f30 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 2f  nByte;         /
62f40 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65 20 6f  * Size in byte o
62f50 66 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2e 20  f text or blob. 
62f60 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63  */.};../*.** Eac
62f70 68 20 74 6f 6b 65 6e 20 63 6f 6d 69 6e 67 20 6f  h token coming o
62f80 75 74 20 6f 66 20 74 68 65 20 6c 65 78 65 72 20  ut of the lexer 
62f90 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
62fa0 66 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74  f.** this struct
62fb0 75 72 65 2e 20 20 54 6f 6b 65 6e 73 20 61 72 65  ure.  Tokens are
62fc0 20 61 6c 73 6f 20 75 73 65 64 20 61 73 20 70 61   also used as pa
62fd0 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73  rt of an express
62fe0 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ion..**.** Note 
62ff0 69 66 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 20 74 68  if Token.z==0 th
63000 65 6e 20 54 6f 6b 65 6e 2e 64 79 6e 20 61 6e 64  en Token.dyn and
63010 20 54 6f 6b 65 6e 2e 6e 20 61 72 65 20 75 6e 64   Token.n are und
63020 65 66 69 6e 65 64 20 61 6e 64 0a 2a 2a 20 6d 61  efined and.** ma
63030 79 20 63 6f 6e 74 61 69 6e 20 72 61 6e 64 6f 6d  y contain random
63040 20 76 61 6c 75 65 73 2e 20 20 44 6f 20 6e 6f 74   values.  Do not
63050 20 6d 61 6b 65 20 61 6e 79 20 61 73 73 75 6d 70   make any assump
63060 74 69 6f 6e 73 20 61 62 6f 75 74 20 54 6f 6b 65  tions about Toke
63070 6e 2e 64 79 6e 0a 2a 2a 20 61 6e 64 20 54 6f 6b  n.dyn.** and Tok
63080 65 6e 2e 6e 20 77 68 65 6e 20 54 6f 6b 65 6e 2e  en.n when Token.
63090 7a 3d 3d 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  z==0..*/.struct 
630a0 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20  Token {.  const 
630b0 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 2f 2a 20  char *z;     /* 
630c0 54 65 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65  Text of the toke
630d0 6e 2e 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72  n.  Not NULL-ter
630e0 6d 69 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e  minated! */.  un
630f0 73 69 67 6e 65 64 20 69 6e 74 20 6e 3b 20 20 20  signed int n;   
63100 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68   /* Number of ch
63110 61 72 61 63 74 65 72 73 20 69 6e 20 74 68 69 73  aracters in this
63120 20 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   token */.};../*
63130 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
63140 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
63150 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72  e contains infor
63160 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f  mation needed to
63170 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64   generate.** cod
63180 65 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74  e for a SELECT t
63190 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67  hat contains agg
631a0 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
631b0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e  ..**.** If Expr.
631c0 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  op==TK_AGG_COLUM
631d0 4e 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43  N or TK_AGG_FUNC
631e0 54 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70  TION then Expr.p
631f0 41 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20  AggInfo is a.** 
63200 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20  pointer to this 
63210 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
63220 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65  Expr.iColumn fie
63230 6c 64 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  ld is the index 
63240 69 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43  in.** AggInfo.aC
63250 6f 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e  ol[] or AggInfo.
63260 61 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72  aFunc[] of infor
63270 6d 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f  mation needed to
63280 20 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64   generate.** cod
63290 65 20 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e  e for that node.
632a0 0a 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70  .**.** AggInfo.p
632b0 47 72 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49  GroupBy and AggI
632c0 6e 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20  nfo.aFunc.pExpr 
632d0 70 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20  point to fields 
632e0 77 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72  within the.** or
632f0 69 67 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74  iginal Select st
63300 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73  ructure that des
63310 63 72 69 62 65 73 20 74 68 65 20 53 45 4c 45 43  cribes the SELEC
63320 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  T statement.  Th
63330 65 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f  ese.** fields do
63340 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
63350 66 72 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c  freed when deall
63360 6f 63 61 74 69 6e 67 20 74 68 65 20 41 67 67 49  ocating the AggI
63370 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  nfo structure..*
63380 2f 0a 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f  /.struct AggInfo
63390 20 7b 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f   {.  u8 directMo
633a0 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  de;          /* 
633b0 44 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67  Direct rendering
633c0 20 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65   mode means take
633d0 20 64 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20   data directly. 
633e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
633f0 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d           ** from
63400 20 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72   source tables r
63410 61 74 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20  ather than from 
63420 61 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a  accumulators */.
63430 20 20 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49    u8 useSortingI
63440 64 78 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20  dx;       /* In 
63450 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66  direct mode, ref
63460 65 72 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69  erence the sorti
63470 6e 67 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a  ng index rather.
63480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63490 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61            ** tha
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 69 6e 74 20 73 6f 72 74  le */.  int sort
634c0 69 6e 67 49 64 78 3b 20 20 20 20 20 20 20 20 20  ingIdx;         
634d0 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
634e0 20 6f 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20   of the sorting 
634f0 69 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c  index */.  ExprL
63500 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20  ist *pGroupBy;  
63510 20 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20     /* The group 
63520 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  by clause */.  i
63530 6e 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d  nt nSortingColum
63540 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  n;     /* Number
63550 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
63560 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  he sorting index
63570 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67   */.  struct Agg
63580 49 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a  Info_col {    /*
63590 20 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   For each column
635a0 20 75 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20   used in source 
635b0 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61  tables */.    Ta
635c0 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
635d0 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65         /* Source
635e0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e   table */.    in
635f0 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20  t iTable;       
63600 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
63610 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73   number of the s
63620 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20  ource table */. 
63630 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20     int iColumn; 
63640 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
63650 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74  olumn number wit
63660 68 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74  hin the source t
63670 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  able */.    int 
63680 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20  iSorterColumn;  
63690 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
636a0 75 6d 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72  umber in the sor
636b0 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20  ting index */.  
636c0 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20    int iMem;     
636d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65             /* Me
636e0 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68  mory location th
636f0 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d  at acts as accum
63700 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78  ulator */.    Ex
63710 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20  pr *pExpr;      
63720 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72         /* The or
63730 69 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f  iginal expressio
63740 6e 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a  n */.  } *aCol;.
63750 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20    int nColumn;  
63760 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
63770 62 65 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72  ber of used entr
63780 69 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f  ies in aCol[] */
63790 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c  .  int nColumnAl
637a0 6c 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  loc;       /* Nu
637b0 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c  mber of slots al
637c0 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c  located for aCol
637d0 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63  [] */.  int nAcc
637e0 75 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20  umulator;       
637f0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
63800 75 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74  umns that show t
63810 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75  hrough to the ou
63820 74 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20  tput..          
63830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63840 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f  ** Additional co
63850 6c 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f  lumns are used o
63860 6e 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72  nly as parameter
63870 73 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20  s to.           
63880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
63890 2a 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  * aggregate func
638a0 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63  tions */.  struc
638b0 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b  t AggInfo_func {
638c0 20 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61     /* For each a
638d0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
638e0 6e 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70  n */.    Expr *p
638f0 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20  Expr;           
63900 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
63910 65 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e  encoding the fun
63920 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e  ction */.    Fun
63930 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20  cDef *pFunc;    
63940 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67        /* The agg
63950 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20  regate function 
63960 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
63970 2f 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20  /.    int iMem; 
63980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
63990 2a 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  * Memory locatio
639a0 6e 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61  n that acts as a
639b0 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20  ccumulator */.  
639c0 20 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b    int iDistinct;
639d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70             /* Ep
639e0 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73  hemeral table us
639f0 65 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49  ed to enforce DI
63a00 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61  STINCT */.  } *a
63a10 46 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e  Func;.  int nFun
63a20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
63a30 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
63a40 72 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20  ries in aFunc[] 
63a50 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c  */.  int nFuncAl
63a60 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  loc;         /* 
63a70 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20  Number of slots 
63a80 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46  allocated for aF
63a90 75 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  unc[] */.};../*.
63aa0 2a 2a 20 54 68 65 20 64 61 74 61 74 79 70 65 20  ** The datatype 
63ab0 79 6e 56 61 72 20 69 73 20 61 20 73 69 67 6e 65  ynVar is a signe
63ac0 64 20 69 6e 74 65 67 65 72 2c 20 65 69 74 68 65  d integer, eithe
63ad0 72 20 31 36 2d 62 69 74 20 6f 72 20 33 32 2d 62  r 16-bit or 32-b
63ae0 69 74 2e 0a 2a 2a 20 55 73 75 61 6c 6c 79 20 69  it..** Usually i
63af0 74 20 69 73 20 31 36 2d 62 69 74 73 2e 20 20 42  t is 16-bits.  B
63b00 75 74 20 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ut if SQLITE_MAX
63b10 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
63b20 20 69 73 20 67 72 65 61 74 65 72 0a 2a 2a 20 74   is greater.** t
63b30 68 61 6e 20 33 32 37 36 37 20 77 65 20 68 61 76  han 32767 we hav
63b40 65 20 74 6f 20 6d 61 6b 65 20 69 74 20 33 32 2d  e to make it 32-
63b50 62 69 74 2e 20 20 31 36 2d 62 69 74 20 69 73 20  bit.  16-bit is 
63b60 70 72 65 66 65 72 72 65 64 20 62 65 63 61 75 73  preferred becaus
63b70 65 0a 2a 2a 20 69 74 20 75 73 65 73 20 6c 65 73  e.** it uses les
63b80 73 20 6d 65 6d 6f 72 79 20 69 6e 20 74 68 65 20  s memory in the 
63b90 45 78 70 72 20 6f 62 6a 65 63 74 2c 20 77 68 69  Expr object, whi
63ba0 63 68 20 69 73 20 61 20 62 69 67 20 6d 65 6d 6f  ch is a big memo
63bb0 72 79 20 75 73 65 72 0a 2a 2a 20 69 6e 20 73 79  ry user.** in sy
63bc0 73 74 65 6d 73 20 77 69 74 68 20 6c 6f 74 73 20  stems with lots 
63bd0 6f 66 20 70 72 65 70 61 72 65 64 20 73 74 61 74  of prepared stat
63be0 65 6d 65 6e 74 73 2e 20 20 41 6e 64 20 66 65 77  ements.  And few
63bf0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a   applications.**
63c00 20 6e 65 65 64 20 6d 6f 72 65 20 74 68 61 6e 20   need more than 
63c10 61 62 6f 75 74 20 31 30 20 6f 72 20 32 30 20 76  about 10 or 20 v
63c20 61 72 69 61 62 6c 65 73 2e 20 20 42 75 74 20 73  ariables.  But s
63c30 6f 6d 65 20 65 78 74 72 65 6d 65 20 75 73 65 72  ome extreme user
63c40 73 20 77 61 6e 74 0a 2a 2a 20 74 6f 20 68 61 76  s want.** to hav
63c50 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
63c60 6d 65 6e 74 73 20 77 69 74 68 20 6f 76 65 72 20  ments with over 
63c70 33 32 37 36 37 20 76 61 72 69 61 62 6c 65 73 2c  32767 variables,
63c80 20 61 6e 64 20 66 6f 72 20 74 68 65 6d 0a 2a 2a   and for them.**
63c90 20 74 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 61   the option is a
63ca0 76 61 69 6c 61 62 6c 65 20 28 61 74 20 63 6f 6d  vailable (at com
63cb0 70 69 6c 65 2d 74 69 6d 65 29 2e 0a 2a 2f 0a 23  pile-time)..*/.#
63cc0 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41  if SQLITE_MAX_VA
63cd0 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 3d 33  RIABLE_NUMBER<=3
63ce0 32 37 36 37 0a 74 79 70 65 64 65 66 20 69 31 36  2767.typedef i16
63cf0 20 79 6e 56 61 72 3b 0a 23 65 6c 73 65 0a 74 79   ynVar;.#else.ty
63d00 70 65 64 65 66 20 69 6e 74 20 79 6e 56 61 72 3b  pedef int ynVar;
63d10 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45  .#endif../*.** E
63d20 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e 20 65  ach node of an e
63d30 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65  xpression in the
63d40 20 70 61 72 73 65 20 74 72 65 65 20 69 73 20 61   parse tree is a
63d50 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66  n instance.** of
63d60 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
63d70 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70 20 69  .**.** Expr.op i
63d80 73 20 74 68 65 20 6f 70 63 6f 64 65 2e 20 54 68  s the opcode. Th
63d90 65 20 69 6e 74 65 67 65 72 20 70 61 72 73 65 72  e integer parser
63da0 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20 61 72 65   token codes are
63db0 20 72 65 75 73 65 64 0a 2a 2a 20 61 73 20 6f 70   reused.** as op
63dc0 63 6f 64 65 73 20 68 65 72 65 2e 20 46 6f 72 20  codes here. For 
63dd0 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 70 61 72  example, the par
63de0 73 65 72 20 64 65 66 69 6e 65 73 20 54 4b 5f 47  ser defines TK_G
63df0 45 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67  E to be an integ
63e00 65 72 0a 2a 2a 20 63 6f 64 65 20 72 65 70 72 65  er.** code repre
63e10 73 65 6e 74 69 6e 67 20 74 68 65 20 22 3e 3d 22  senting the ">="
63e20 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20   operator. This 
63e30 73 61 6d 65 20 69 6e 74 65 67 65 72 20 63 6f 64  same integer cod
63e40 65 20 69 73 20 72 65 75 73 65 64 0a 2a 2a 20 74  e is reused.** t
63e50 6f 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20  o represent the 
63e60 67 72 65 61 74 65 72 2d 74 68 61 6e 2d 6f 72 2d  greater-than-or-
63e70 65 71 75 61 6c 2d 74 6f 20 6f 70 65 72 61 74 6f  equal-to operato
63e80 72 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73  r in the express
63e90 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a 2a 2a 0a  ion.** tree..**.
63ea0 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
63eb0 73 69 6f 6e 20 69 73 20 61 6e 20 53 51 4c 20 6c  sion is an SQL l
63ec0 69 74 65 72 61 6c 20 28 54 4b 5f 49 4e 54 45 47  iteral (TK_INTEG
63ed0 45 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c 20 54 4b  ER, TK_FLOAT, TK
63ee0 5f 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72 20 54 4b  _BLOB, .** or TK
63ef0 5f 53 54 52 49 4e 47 29 2c 20 74 68 65 6e 20 45  _STRING), then E
63f00 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e 74 61 69  xpr.token contai
63f10 6e 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 74  ns the text of t
63f20 68 65 20 53 51 4c 20 6c 69 74 65 72 61 6c 2e 20  he SQL literal. 
63f30 49 66 0a 2a 2a 20 74 68 65 20 65 78 70 72 65 73  If.** the expres
63f40 73 69 6f 6e 20 69 73 20 61 20 76 61 72 69 61 62  sion is a variab
63f50 6c 65 20 28 54 4b 5f 56 41 52 49 41 42 4c 45 29  le (TK_VARIABLE)
63f60 2c 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65  , then Expr.toke
63f70 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 0a  n contains the .
63f80 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 61 6d 65  ** variable name
63f90 2e 20 46 69 6e 61 6c 6c 79 2c 20 69 66 20 74 68  . Finally, if th
63fa0 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
63fb0 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  an SQL function 
63fc0 28 54 4b 5f 46 55 4e 43 54 49 4f 4e 29 2c 0a 2a  (TK_FUNCTION),.*
63fd0 2a 20 74 68 65 6e 20 45 78 70 72 2e 74 6f 6b 65  * then Expr.toke
63fe0 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e  n contains the n
63ff0 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ame of the funct
64000 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e  ion..**.** Expr.
64010 70 52 69 67 68 74 20 61 6e 64 20 45 78 70 72 2e  pRight and Expr.
64020 70 4c 65 66 74 20 61 72 65 20 74 68 65 20 6c 65  pLeft are the le
64030 66 74 20 61 6e 64 20 72 69 67 68 74 20 73 75 62  ft and right sub
64040 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 61  expressions of a
64050 0a 2a 2a 20 62 69 6e 61 72 79 20 6f 70 65 72 61  .** binary opera
64060 74 6f 72 2e 20 45 69 74 68 65 72 20 6f 72 20 62  tor. Either or b
64070 6f 74 68 20 6d 61 79 20 62 65 20 4e 55 4c 4c 2e  oth may be NULL.
64080 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 4c  .**.** Expr.x.pL
64090 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66  ist is a list of
640a0 20 61 72 67 75 6d 65 6e 74 73 20 69 66 20 74 68   arguments if th
640b0 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
640c0 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2c  an SQL function,
640d0 0a 2a 2a 20 61 20 43 41 53 45 20 65 78 70 72 65  .** a CASE expre
640e0 73 73 69 6f 6e 20 6f 72 20 61 6e 20 49 4e 20 65  ssion or an IN e
640f0 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65  xpression of the
64100 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20 49 4e 20   form "<lhs> IN 
64110 28 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29 22 2e 0a  (<y>, <z>...)"..
64120 2a 2a 20 45 78 70 72 2e 78 2e 70 53 65 6c 65 63  ** Expr.x.pSelec
64130 74 20 69 73 20 75 73 65 64 20 69 66 20 74 68 65  t is used if the
64140 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
64150 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61   sub-select or a
64160 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 0a  n expression of.
64170 2a 2a 20 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68  ** the form "<lh
64180 73 3e 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e  s> IN (SELECT ..
64190 2e 29 22 2e 20 49 66 20 74 68 65 20 45 50 5f 78  .)". If the EP_x
641a0 49 73 53 65 6c 65 63 74 20 62 69 74 20 69 73 20  IsSelect bit is 
641b0 73 65 74 20 69 6e 20 74 68 65 0a 2a 2a 20 45 78  set in the.** Ex
641c0 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c 20 74  pr.flags mask, t
641d0 68 65 6e 20 45 78 70 72 2e 78 2e 70 53 65 6c 65  hen Expr.x.pSele
641e0 63 74 20 69 73 20 76 61 6c 69 64 2e 20 4f 74 68  ct is valid. Oth
641f0 65 72 77 69 73 65 2c 20 45 78 70 72 2e 78 2e 70  erwise, Expr.x.p
64200 4c 69 73 74 20 69 73 20 0a 2a 2a 20 76 61 6c 69  List is .** vali
64210 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72  d..**.** An expr
64220 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f  ession of the fo
64230 72 6d 20 49 44 20 6f 72 20 49 44 2e 49 44 20 72  rm ID or ID.ID r
64240 65 66 65 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d  efers to a colum
64250 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a  n in a table..**
64260 20 46 6f 72 20 73 75 63 68 20 65 78 70 72 65 73   For such expres
64270 73 69 6f 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69  sions, Expr.op i
64280 73 20 73 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55  s set to TK_COLU
64290 4d 4e 20 61 6e 64 20 45 78 70 72 2e 69 54 61 62  MN and Expr.iTab
642a0 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74  le is.** the int
642b0 65 67 65 72 20 63 75 72 73 6f 72 20 6e 75 6d 62  eger cursor numb
642c0 65 72 20 6f 66 20 61 20 56 44 42 45 20 63 75 72  er of a VDBE cur
642d0 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  sor pointing to 
642e0 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a  that table and.*
642f0 2a 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69  * Expr.iColumn i
64300 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d  s the column num
64310 62 65 72 20 66 6f 72 20 74 68 65 20 73 70 65 63  ber for the spec
64320 69 66 69 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66  ific column.  If
64330 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73 73 69   the.** expressi
64340 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 61 20  on is used as a 
64350 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 61 67 67  result in an agg
64360 72 65 67 61 74 65 20 53 45 4c 45 43 54 2c 20 74  regate SELECT, t
64370 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65  hen the.** value
64380 20 69 73 20 61 6c 73 6f 20 73 74 6f 72 65 64 20   is also stored 
64390 69 6e 20 74 68 65 20 45 78 70 72 2e 69 41 67 67  in the Expr.iAgg
643a0 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61   column in the a
643b0 67 67 72 65 67 61 74 65 20 73 6f 20 74 68 61 74  ggregate so that
643c0 0a 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 61 63  .** it can be ac
643d0 63 65 73 73 65 64 20 61 66 74 65 72 20 61 6c 6c  cessed after all
643e0 20 61 67 67 72 65 67 61 74 65 73 20 61 72 65 20   aggregates are 
643f0 63 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  computed..**.** 
64400 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
64410 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e 64 20  n is an unbound 
64420 76 61 72 69 61 62 6c 65 20 6d 61 72 6b 65 72 20  variable marker 
64430 28 61 20 71 75 65 73 74 69 6f 6e 20 6d 61 72 6b  (a question mark
64440 20 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20 27   .** character '
64450 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e  ?' in the origin
64460 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68 65  al SQL) then the
64470 20 45 78 70 72 2e 69 54 61 62 6c 65 20 68 6f 6c   Expr.iTable hol
64480 64 73 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a  ds the index .**
64490 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74   number for that
644a0 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a   variable..**.**
644b0 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
644c0 6f 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79  on is a subquery
644d0 20 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75   then Expr.iColu
644e0 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65  mn holds an inte
644f0 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  ger.** register 
64500 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e  number containin
64510 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  g the result of 
64520 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49  the subquery.  I
64530 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72  f the.** subquer
64540 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61  y gives a consta
64550 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20  nt result, then 
64560 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49  iTable is -1.  I
64570 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a  f the subquery.*
64580 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72  * gives a differ
64590 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69  ent answer at di
645a0 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75  fferent times du
645b0 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70  ring statement p
645c0 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65  rocessing.** the
645d0 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20  n iTable is the 
645e0 61 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62  address of a sub
645f0 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d  routine that com
64600 70 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65  putes the subque
64610 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ry..**.** If the
64620 20 45 78 70 72 20 69 73 20 6f 66 20 74 79 70 65   Expr is of type
64630 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20   OP_Column, and 
64640 74 68 65 20 74 61 62 6c 65 20 69 74 20 69 73 20  the table it is 
64650 73 65 6c 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a  selecting from.*
64660 2a 20 69 73 20 61 20 64 69 73 6b 20 74 61 62 6c  * is a disk tabl
64670 65 20 6f 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22  e or the "old.*"
64680 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74   pseudo-table, t
64690 68 65 6e 20 70 54 61 62 20 70 6f 69 6e 74 73 20  hen pTab points 
646a0 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73  to the.** corres
646b0 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 20 64 65  ponding table de
646c0 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  finition..**.** 
646d0 41 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f 54 45 53  ALLOCATION NOTES
646e0 3a 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20 6f 62 6a  :.**.** Expr obj
646f0 65 63 74 73 20 63 61 6e 20 75 73 65 20 61 20 6c  ects can use a l
64700 6f 74 20 6f 66 20 6d 65 6d 6f 72 79 20 73 70 61  ot of memory spa
64710 63 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 73  ce in database s
64720 63 68 65 6d 61 2e 20 20 54 6f 0a 2a 2a 20 68 65  chema.  To.** he
64730 6c 70 20 72 65 64 75 63 65 20 6d 65 6d 6f 72 79  lp reduce memory
64740 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2c 20 73   requirements, s
64750 6f 6d 65 74 69 6d 65 73 20 61 6e 20 45 78 70 72  ometimes an Expr
64760 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20 62 65 0a   object will be.
64770 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e 20 20 41  ** truncated.  A
64780 6e 64 20 74 6f 20 72 65 64 75 63 65 20 74 68 65  nd to reduce the
64790 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72   number of memor
647a0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 73  y allocations, s
647b0 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 74 77 6f 20  ometimes.** two 
647c0 6f 72 20 6d 6f 72 65 20 45 78 70 72 20 6f 62 6a  or more Expr obj
647d0 65 63 74 73 20 77 69 6c 6c 20 62 65 20 73 74 6f  ects will be sto
647e0 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c 65 20  red in a single 
647f0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
64800 6e 2c 0a 2a 2a 20 74 6f 67 65 74 68 65 72 20 77  n,.** together w
64810 69 74 68 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20  ith Expr.zToken 
64820 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49  strings..**.** I
64830 66 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64  f the EP_Reduced
64840 20 61 6e 64 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c   and EP_TokenOnl
64850 79 20 66 6c 61 67 73 20 61 72 65 20 73 65 74 20  y flags are set 
64860 77 68 65 6e 0a 2a 2a 20 61 6e 20 45 78 70 72 20  when.** an Expr 
64870 6f 62 6a 65 63 74 20 69 73 20 74 72 75 6e 63 61  object is trunca
64880 74 65 64 2e 20 20 57 68 65 6e 20 45 50 5f 52 65  ted.  When EP_Re
64890 64 75 63 65 64 20 69 73 20 73 65 74 2c 20 74 68  duced is set, th
648a0 65 6e 20 61 6c 6c 0a 2a 2a 20 74 68 65 20 63 68  en all.** the ch
648b0 69 6c 64 20 45 78 70 72 20 6f 62 6a 65 63 74 73  ild Expr objects
648c0 20 69 6e 20 74 68 65 20 45 78 70 72 2e 70 4c 65   in the Expr.pLe
648d0 66 74 20 61 6e 64 20 45 78 70 72 2e 70 52 69 67  ft and Expr.pRig
648e0 68 74 20 73 75 62 74 72 65 65 73 0a 2a 2a 20 61  ht subtrees.** a
648f0 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74  re contained wit
64900 68 69 6e 20 74 68 65 20 73 61 6d 65 20 6d 65 6d  hin the same mem
64910 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  ory allocation. 
64920 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65 72 2c 20   Note, however, 
64930 74 68 61 74 0a 2a 2a 20 74 68 65 20 73 75 62 74  that.** the subt
64940 72 65 65 73 20 69 6e 20 45 78 70 72 2e 78 2e 70  rees in Expr.x.p
64950 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70  List or Expr.x.p
64960 53 65 6c 65 63 74 20 61 72 65 20 61 6c 77 61 79  Select are alway
64970 73 20 73 65 70 61 72 61 74 65 6c 79 0a 2a 2a 20  s separately.** 
64980 61 6c 6c 6f 63 61 74 65 64 2c 20 72 65 67 61 72  allocated, regar
64990 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72  dless of whether
649a0 20 6f 72 20 6e 6f 74 20 45 50 5f 52 65 64 75 63   or not EP_Reduc
649b0 65 64 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 73 74  ed is set..*/.st
649c0 72 75 63 74 20 45 78 70 72 20 7b 0a 20 20 75 38  ruct Expr {.  u8
649d0 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   op;            
649e0 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 69 6f       /* Operatio
649f0 6e 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74  n performed by t
64a00 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 20 20 63 68  his node */.  ch
64a10 61 72 20 61 66 66 69 6e 69 74 79 3b 20 20 20 20  ar affinity;    
64a20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69       /* The affi
64a30 6e 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75  nity of the colu
64a40 6d 6e 20 6f 72 20 30 20 69 66 20 6e 6f 74 20 61  mn or 0 if not a
64a50 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 31 36   column */.  u16
64a60 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
64a70 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 66      /* Various f
64a80 6c 61 67 73 2e 20 20 45 50 5f 2a 20 53 65 65 20  lags.  EP_* See 
64a90 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 6e 69 6f 6e  below */.  union
64aa0 20 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6f   {.    char *zTo
64ab0 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ken;          /*
64ac0 20 54 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 5a 65   Token value. Ze
64ad0 72 6f 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e  ro terminated an
64ae0 64 20 64 65 71 75 6f 74 65 64 20 2a 2f 0a 20 20  d dequoted */.  
64af0 20 20 69 6e 74 20 69 56 61 6c 75 65 3b 20 20 20    int iValue;   
64b00 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65           /* Inte
64b10 67 65 72 20 76 61 6c 75 65 20 69 66 20 45 50 5f  ger value if EP_
64b20 49 6e 74 56 61 6c 75 65 20 2a 2f 0a 20 20 7d 20  IntValue */.  } 
64b30 75 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  u;..  /* If the 
64b40 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 66 6c 61  EP_TokenOnly fla
64b50 67 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20  g is set in the 
64b60 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c  Expr.flags mask,
64b70 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70   then no.  ** sp
64b80 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ace is allocated
64b90 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20   for the fields 
64ba0 62 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74  below this point
64bb0 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a  . An attempt to.
64bc0 20 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d    ** access them
64bd0 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20   will result in 
64be0 61 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61  a segfault or ma
64bf0 6c 66 75 6e 63 74 69 6f 6e 2e 20 0a 20 20 2a 2a  lfunction. .  **
64c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64c40 2a 2a 2a 2f 0a 0a 20 20 45 78 70 72 20 2a 70 4c  ***/..  Expr *pL
64c50 65 66 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  eft;           /
64c60 2a 20 4c 65 66 74 20 73 75 62 6e 6f 64 65 20 2a  * Left subnode *
64c70 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74  /.  Expr *pRight
64c80 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69  ;          /* Ri
64c90 67 68 74 20 73 75 62 6e 6f 64 65 20 2a 2f 0a 20  ght subnode */. 
64ca0 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 45 78 70   union {.    Exp
64cb0 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20  rList *pList;   
64cc0 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 61 72    /* Function ar
64cd0 67 75 6d 65 6e 74 73 20 6f 72 20 69 6e 20 22 3c  guments or in "<
64ce0 65 78 70 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d  expr> IN (<expr-
64cf0 6c 69 73 74 29 22 20 2a 2f 0a 20 20 20 20 53 65  list)" */.    Se
64d00 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20  lect *pSelect;  
64d10 20 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 73     /* Used for s
64d20 75 62 2d 73 65 6c 65 63 74 73 20 61 6e 64 20 22  ub-selects and "
64d30 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65 6c 65  <expr> IN (<sele
64d40 63 74 3e 29 22 20 2a 2f 0a 20 20 7d 20 78 3b 0a  ct>)" */.  } x;.
64d50 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
64d60 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
64d70 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f  collation type o
64d80 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20  f the column or 
64d90 30 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68  0 */..  /* If th
64da0 65 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c 61  e EP_Reduced fla
64db0 67 20 69 73 20 73 65 74 20 69 6e 20 74 68 65 20  g is set in the 
64dc0 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b 2c  Expr.flags mask,
64dd0 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a 20 73 70   then no.  ** sp
64de0 61 63 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ace is allocated
64df0 20 66 6f 72 20 74 68 65 20 66 69 65 6c 64 73 20   for the fields 
64e00 62 65 6c 6f 77 20 74 68 69 73 20 70 6f 69 6e 74  below this point
64e10 2e 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a  . An attempt to.
64e20 20 20 2a 2a 20 61 63 63 65 73 73 20 74 68 65 6d    ** access them
64e30 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20   will result in 
64e40 61 20 73 65 67 66 61 75 6c 74 20 6f 72 20 6d 61  a segfault or ma
64e50 6c 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a 2a  lfunction..  ***
64e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64ea0 2a 2a 2f 0a 0a 20 20 69 6e 74 20 69 54 61 62 6c  **/..  int iTabl
64eb0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
64ec0 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63 75 72 73   TK_COLUMN: curs
64ed0 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62  or number of tab
64ee0 6c 65 20 68 6f 6c 64 69 6e 67 20 63 6f 6c 75 6d  le holding colum
64ef0 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n.              
64f00 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b             ** TK
64f10 5f 52 45 47 49 53 54 45 52 3a 20 72 65 67 69 73  _REGISTER: regis
64f20 74 65 72 20 6e 75 6d 62 65 72 0a 20 20 20 20 20  ter number.     
64f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64f40 20 20 20 20 2a 2a 20 54 4b 5f 54 52 49 47 47 45      ** TK_TRIGGE
64f50 52 3a 20 31 20 2d 3e 20 6e 65 77 2c 20 30 20 2d  R: 1 -> new, 0 -
64f60 3e 20 6f 6c 64 20 2a 2f 0a 20 20 79 6e 56 61 72  > old */.  ynVar
64f70 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   iColumn;       
64f80 20 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20    /* TK_COLUMN: 
64f90 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 2e 20 20 2d  column index.  -
64fa0 31 20 66 6f 72 20 72 6f 77 69 64 2e 0a 20 20 20  1 for rowid..   
64fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64fc0 20 20 20 20 20 20 2a 2a 20 54 4b 5f 56 41 52 49        ** TK_VARI
64fd0 41 42 4c 45 3a 20 76 61 72 69 61 62 6c 65 20 6e  ABLE: variable n
64fe0 75 6d 62 65 72 20 28 61 6c 77 61 79 73 20 3e 3d  umber (always >=
64ff0 20 31 29 2e 20 2a 2f 0a 20 20 69 31 36 20 69 41   1). */.  i16 iA
65000 67 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gg;             
65010 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72 79 20   /* Which entry 
65020 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f  in pAggInfo->aCo
65030 6c 5b 5d 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d  l[] or ->aFunc[]
65040 20 2a 2f 0a 20 20 69 31 36 20 69 52 69 67 68 74   */.  i16 iRight
65050 4a 6f 69 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20  JoinTable;   /* 
65060 49 66 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20  If EP_FromJoin, 
65070 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
65080 6f 66 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20  of the join */. 
65090 20 75 38 20 66 6c 61 67 73 32 3b 20 20 20 20 20   u8 flags2;     
650a0 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e          /* Secon
650b0 64 20 73 65 74 20 6f 66 20 66 6c 61 67 73 2e 20  d set of flags. 
650c0 20 45 50 32 5f 2e 2e 2e 20 2a 2f 0a 20 20 75 38   EP2_... */.  u8
650d0 20 6f 70 32 3b 20 20 20 20 20 20 20 20 20 20 20   op2;           
650e0 20 20 20 20 20 2f 2a 20 49 66 20 61 20 54 4b 5f       /* If a TK_
650f0 52 45 47 49 53 54 45 52 2c 20 74 68 65 20 6f 72  REGISTER, the or
65100 69 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20  iginal value of 
65110 45 78 70 72 2e 6f 70 20 2a 2f 0a 20 20 41 67 67  Expr.op */.  Agg
65120 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b 20  Info *pAggInfo; 
65130 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 54      /* Used by T
65140 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e 64  K_AGG_COLUMN and
65150 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e   TK_AGG_FUNCTION
65160 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
65170 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
65180 54 61 62 6c 65 20 66 6f 72 20 54 4b 5f 43 4f 4c  Table for TK_COL
65190 55 4d 4e 20 65 78 70 72 65 73 73 69 6f 6e 73 2e  UMN expressions.
651a0 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d   */.#if SQLITE_M
651b0 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a  AX_EXPR_DEPTH>0.
651c0 20 20 69 6e 74 20 6e 48 65 69 67 68 74 3b 20 20    int nHeight;  
651d0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69 67           /* Heig
651e0 68 74 20 6f 66 20 74 68 65 20 74 72 65 65 20 68  ht of the tree h
651f0 65 61 64 65 64 20 62 79 20 74 68 69 73 20 6e 6f  eaded by this no
65200 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  de */.#endif.};.
65210 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
65220 77 69 6e 67 20 61 72 65 20 74 68 65 20 6d 65 61  wing are the mea
65230 6e 69 6e 67 73 20 6f 66 20 62 69 74 73 20 69 6e  nings of bits in
65240 20 74 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20   the Expr.flags 
65250 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  field..*/.#defin
65260 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 20 20  e EP_FromJoin   
65270 30 78 30 30 30 31 20 20 2f 2a 20 4f 72 69 67 69  0x0001  /* Origi
65280 6e 61 74 65 64 20 69 6e 20 4f 4e 20 6f 72 20 55  nated in ON or U
65290 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61  SING clause of a
652a0 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65   join */.#define
652b0 20 45 50 5f 41 67 67 20 20 20 20 20 20 20 20 30   EP_Agg        0
652c0 78 30 30 30 32 20 20 2f 2a 20 43 6f 6e 74 61 69  x0002  /* Contai
652d0 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 61  ns one or more a
652e0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
652f0 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50  ns */.#define EP
65300 5f 52 65 73 6f 6c 76 65 64 20 20 20 30 78 30 30  _Resolved   0x00
65310 30 34 20 20 2f 2a 20 49 44 73 20 68 61 76 65 20  04  /* IDs have 
65320 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 20 74 6f  been resolved to
65330 20 43 4f 4c 55 4d 4e 73 20 2a 2f 0a 23 64 65 66   COLUMNs */.#def
65340 69 6e 65 20 45 50 5f 45 72 72 6f 72 20 20 20 20  ine EP_Error    
65350 20 20 30 78 30 30 30 38 20 20 2f 2a 20 45 78 70    0x0008  /* Exp
65360 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73  ression contains
65370 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 65 72 72   one or more err
65380 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ors */.#define E
65390 50 5f 44 69 73 74 69 6e 63 74 20 20 20 30 78 30  P_Distinct   0x0
653a0 30 31 30 20 20 2f 2a 20 41 67 67 72 65 67 61 74  010  /* Aggregat
653b0 65 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  e function with 
653c0 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64  DISTINCT keyword
653d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 56   */.#define EP_V
653e0 61 72 53 65 6c 65 63 74 20 20 30 78 30 30 32 30  arSelect  0x0020
653f0 20 20 2f 2a 20 70 53 65 6c 65 63 74 20 69 73 20    /* pSelect is 
65400 63 6f 72 72 65 6c 61 74 65 64 2c 20 6e 6f 74 20  correlated, not 
65410 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 23 64 65 66  constant */.#def
65420 69 6e 65 20 45 50 5f 44 62 6c 51 75 6f 74 65 64  ine EP_DblQuoted
65430 20 20 30 78 30 30 34 30 20 20 2f 2a 20 74 6f 6b    0x0040  /* tok
65440 65 6e 2e 7a 20 77 61 73 20 6f 72 69 67 69 6e 61  en.z was origina
65450 6c 6c 79 20 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a  lly in "..." */.
65460 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 66 69 78  #define EP_Infix
65470 46 75 6e 63 20 20 30 78 30 30 38 30 20 20 2f 2a  Func  0x0080  /*
65480 20 54 72 75 65 20 66 6f 72 20 61 6e 20 69 6e 66   True for an inf
65490 69 78 20 66 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b  ix function: LIK
654a0 45 2c 20 47 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a  E, GLOB, etc */.
654b0 23 64 65 66 69 6e 65 20 45 50 5f 45 78 70 43 6f  #define EP_ExpCo
654c0 6c 6c 61 74 65 20 30 78 30 31 30 30 20 20 2f 2a  llate 0x0100  /*
654d0 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
654e0 6e 63 65 20 73 70 65 63 69 66 69 65 64 20 65 78  nce specified ex
654f0 70 6c 69 63 69 74 6c 79 20 2a 2f 0a 23 64 65 66  plicitly */.#def
65500 69 6e 65 20 45 50 5f 41 6e 79 41 66 66 20 20 20  ine EP_AnyAff   
65510 20 20 30 78 30 32 30 30 20 20 2f 2a 20 43 61 6e    0x0200  /* Can
65520 20 74 61 6b 65 20 61 20 63 61 63 68 65 64 20 63   take a cached c
65530 6f 6c 75 6d 6e 20 6f 66 20 61 6e 79 20 61 66 66  olumn of any aff
65540 69 6e 69 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65  inity */.#define
65550 20 45 50 5f 46 69 78 65 64 44 65 73 74 20 20 30   EP_FixedDest  0
65560 78 30 34 30 30 20 20 2f 2a 20 52 65 73 75 6c 74  x0400  /* Result
65570 20 6e 65 65 64 65 64 20 69 6e 20 61 20 73 70 65   needed in a spe
65580 63 69 66 69 63 20 72 65 67 69 73 74 65 72 20 2a  cific register *
65590 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 49 6e 74  /.#define EP_Int
655a0 56 61 6c 75 65 20 20 20 30 78 30 38 30 30 20 20  Value   0x0800  
655b0 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65  /* Integer value
655c0 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 75 2e   contained in u.
655d0 69 56 61 6c 75 65 20 2a 2f 0a 23 64 65 66 69 6e  iValue */.#defin
655e0 65 20 45 50 5f 78 49 73 53 65 6c 65 63 74 20 20  e EP_xIsSelect  
655f0 30 78 31 30 30 30 20 20 2f 2a 20 78 2e 70 53 65  0x1000  /* x.pSe
65600 6c 65 63 74 20 69 73 20 76 61 6c 69 64 20 28 6f  lect is valid (o
65610 74 68 65 72 77 69 73 65 20 78 2e 70 4c 69 73 74  therwise x.pList
65620 20 69 73 29 20 2a 2f 0a 0a 23 64 65 66 69 6e 65   is) */..#define
65630 20 45 50 5f 52 65 64 75 63 65 64 20 20 20 20 30   EP_Reduced    0
65640 78 32 30 30 30 20 20 2f 2a 20 45 78 70 72 20 73  x2000  /* Expr s
65650 74 72 75 63 74 20 69 73 20 45 58 50 52 5f 52 45  truct is EXPR_RE
65660 44 55 43 45 44 53 49 5a 45 20 62 79 74 65 73 20  DUCEDSIZE bytes 
65670 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  only */.#define 
65680 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20 20 30 78  EP_TokenOnly  0x
65690 34 30 30 30 20 20 2f 2a 20 45 78 70 72 20 73 74  4000  /* Expr st
656a0 72 75 63 74 20 69 73 20 45 58 50 52 5f 54 4f 4b  ruct is EXPR_TOK
656b0 45 4e 4f 4e 4c 59 53 49 5a 45 20 62 79 74 65 73  ENONLYSIZE bytes
656c0 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65   only */.#define
656d0 20 45 50 5f 53 74 61 74 69 63 20 20 20 20 20 30   EP_Static     0
656e0 78 38 30 30 30 20 20 2f 2a 20 48 65 6c 64 20 69  x8000  /* Held i
656f0 6e 20 6d 65 6d 6f 72 79 20 6e 6f 74 20 6f 62 74  n memory not obt
65700 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
65710 63 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  c() */../*.** Th
65720 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
65730 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20  the meanings of 
65740 62 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72  bits in the Expr
65750 2e 66 6c 61 67 73 32 20 66 69 65 6c 64 2e 0a 2a  .flags2 field..*
65760 2f 0a 23 64 65 66 69 6e 65 20 45 50 32 5f 4d 61  /.#define EP2_Ma
65770 6c 6c 6f 63 65 64 54 6f 6b 65 6e 20 20 30 78 30  llocedToken  0x0
65780 30 30 31 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20  001  /* Need to 
65790 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20  sqlite3DbFree() 
657a0 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 2a 2f 0a 23  Expr.zToken */.#
657b0 64 65 66 69 6e 65 20 45 50 32 5f 49 72 72 65 64  define EP2_Irred
657c0 75 63 69 62 6c 65 20 20 20 20 30 78 30 30 30 32  ucible    0x0002
657d0 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 45 58 50 52    /* Cannot EXPR
657e0 44 55 50 5f 52 45 44 55 43 45 20 74 68 69 73 20  DUP_REDUCE this 
657f0 45 78 70 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  Expr */../*.** T
65800 68 65 20 70 73 65 75 64 6f 2d 72 6f 75 74 69 6e  he pseudo-routin
65810 65 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74  e sqlite3ExprSet
65820 49 72 72 65 64 75 63 69 62 6c 65 20 73 65 74 73  Irreducible sets
65830 20 74 68 65 20 45 50 32 5f 49 72 72 65 64 75 63   the EP2_Irreduc
65840 69 62 6c 65 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20  ible.** flag on 
65850 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74  an expression st
65860 72 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 66  ructure.  This f
65870 6c 61 67 20 69 73 20 75 73 65 64 20 66 6f 72 20  lag is used for 
65880 56 56 26 41 20 6f 6e 6c 79 2e 20 20 54 68 65 0a  VV&A only.  The.
65890 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6d  ** routine is im
658a0 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d  plemented as a m
658b0 61 63 72 6f 20 74 68 61 74 20 6f 6e 6c 79 20 77  acro that only w
658c0 6f 72 6b 73 20 77 68 65 6e 20 69 6e 20 64 65 62  orks when in deb
658d0 75 67 67 69 6e 67 20 6d 6f 64 65 2c 0a 2a 2a 20  ugging mode,.** 
658e0 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 62 75 72  so as not to bur
658f0 64 65 6e 20 70 72 6f 64 75 63 74 69 6f 6e 20 63  den production c
65900 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ode..*/.#ifdef S
65910 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 20 64 65  QLITE_DEBUG.# de
65920 66 69 6e 65 20 45 78 70 72 53 65 74 49 72 72 65  fine ExprSetIrre
65930 64 75 63 69 62 6c 65 28 58 29 20 20 28 58 29 2d  ducible(X)  (X)-
65940 3e 66 6c 61 67 73 32 20 7c 3d 20 45 50 32 5f 49  >flags2 |= EP2_I
65950 72 72 65 64 75 63 69 62 6c 65 0a 23 65 6c 73 65  rreducible.#else
65960 0a 23 20 64 65 66 69 6e 65 20 45 78 70 72 53 65  .# define ExprSe
65970 74 49 72 72 65 64 75 63 69 62 6c 65 28 58 29 0a  tIrreducible(X).
65980 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
65990 65 73 65 20 6d 61 63 72 6f 73 20 63 61 6e 20 62  ese macros can b
659a0 65 20 75 73 65 64 20 74 6f 20 74 65 73 74 2c 20  e used to test, 
659b0 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 20 62 69  set, or clear bi
659c0 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 45 78  ts in the .** Ex
659d0 70 72 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a  pr.flags field..
659e0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 78 70 72 48  */.#define ExprH
659f0 61 73 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20  asProperty(E,P) 
65a00 20 20 20 20 28 28 28 45 29 2d 3e 66 6c 61 67 73      (((E)->flags
65a10 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66  &(P))==(P)).#def
65a20 69 6e 65 20 45 78 70 72 48 61 73 41 6e 79 50 72  ine ExprHasAnyPr
65a30 6f 70 65 72 74 79 28 45 2c 50 29 20 20 28 28 28  operty(E,P)  (((
65a40 45 29 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d  E)->flags&(P))!=
65a50 30 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72 53  0).#define ExprS
65a60 65 74 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20  etProperty(E,P) 
65a70 20 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 7c 3d      (E)->flags|=
65a80 28 50 29 0a 23 64 65 66 69 6e 65 20 45 78 70 72  (P).#define Expr
65a90 43 6c 65 61 72 50 72 6f 70 65 72 74 79 28 45 2c  ClearProperty(E,
65aa0 50 29 20 20 20 28 45 29 2d 3e 66 6c 61 67 73 26  P)   (E)->flags&
65ab0 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  =~(P)../*.** Mac
65ac0 72 6f 73 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ros to determine
65ad0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
65ae0 79 74 65 73 20 72 65 71 75 69 72 65 64 20 62 79  ytes required by
65af0 20 61 20 6e 6f 72 6d 61 6c 20 45 78 70 72 20 0a   a normal Expr .
65b00 2a 2a 20 73 74 72 75 63 74 2c 20 61 6e 20 45 78  ** struct, an Ex
65b10 70 72 20 73 74 72 75 63 74 20 77 69 74 68 20 74  pr struct with t
65b20 68 65 20 45 50 5f 52 65 64 75 63 65 64 20 66 6c  he EP_Reduced fl
65b30 61 67 20 73 65 74 20 69 6e 20 45 78 70 72 2e 66  ag set in Expr.f
65b40 6c 61 67 73 20 0a 2a 2a 20 61 6e 64 20 61 6e 20  lags .** and an 
65b50 45 78 70 72 20 73 74 72 75 63 74 20 77 69 74 68  Expr struct with
65b60 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c   the EP_TokenOnl
65b70 79 20 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23  y flag set..*/.#
65b80 64 65 66 69 6e 65 20 45 58 50 52 5f 46 55 4c 4c  define EXPR_FULL
65b90 53 49 5a 45 20 20 20 20 20 20 20 20 20 20 20 73  SIZE           s
65ba0 69 7a 65 6f 66 28 45 78 70 72 29 20 20 20 20 20  izeof(Expr)     
65bb0 20 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 73 69        /* Full si
65bc0 7a 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58  ze */.#define EX
65bd0 50 52 5f 52 45 44 55 43 45 44 53 49 5a 45 20 20  PR_REDUCEDSIZE  
65be0 20 20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45        offsetof(E
65bf0 78 70 72 2c 69 54 61 62 6c 65 29 20 20 2f 2a 20  xpr,iTable)  /* 
65c00 43 6f 6d 6d 6f 6e 20 66 65 61 74 75 72 65 73 20  Common features 
65c10 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f  */.#define EXPR_
65c20 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 20 20  TOKENONLYSIZE   
65c30 20 20 20 6f 66 66 73 65 74 6f 66 28 45 78 70 72     offsetof(Expr
65c40 2c 70 4c 65 66 74 29 20 20 20 2f 2a 20 46 65 77  ,pLeft)   /* Few
65c50 65 72 20 66 65 61 74 75 72 65 73 20 2a 2f 0a 0a  er features */..
65c60 2f 2a 0a 2a 2a 20 46 6c 61 67 73 20 70 61 73 73  /*.** Flags pass
65c70 65 64 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  ed to the sqlite
65c80 33 45 78 70 72 44 75 70 28 29 20 66 75 6e 63 74  3ExprDup() funct
65c90 69 6f 6e 2e 20 53 65 65 20 74 68 65 20 68 65 61  ion. See the hea
65ca0 64 65 72 20 63 6f 6d 6d 65 6e 74 20 0a 2a 2a 20  der comment .** 
65cb0 61 62 6f 76 65 20 73 71 6c 69 74 65 33 45 78 70  above sqlite3Exp
65cc0 72 44 75 70 28 29 20 66 6f 72 20 64 65 74 61 69  rDup() for detai
65cd0 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ls..*/.#define E
65ce0 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 20 20  XPRDUP_REDUCE   
65cf0 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a        0x0001  /*
65d00 20 55 73 65 64 20 72 65 64 75 63 65 64 2d 73 69   Used reduced-si
65d10 7a 65 20 45 78 70 72 20 6e 6f 64 65 73 20 2a 2f  ze Expr nodes */
65d20 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20 6f  ../*.** A list o
65d30 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20  f expressions.  
65d40 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20  Each expression 
65d50 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 68  may optionally h
65d60 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20 20  ave a.** name.  
65d70 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f 6d  An expr/name com
65d80 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65 20  bination can be 
65d90 75 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20  used in several 
65da0 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61 73  ways, such.** as
65db0 20 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65 78   the list of "ex
65dc0 70 72 20 41 53 20 49 44 22 20 66 69 65 6c 64 73  pr AS ID" fields
65dd0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53 45   following a "SE
65de0 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65 0a  LECT" or in the.
65df0 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20 3d  ** list of "ID =
65e00 20 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e 20   expr" items in 
65e10 61 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c 69  an UPDATE.  A li
65e20 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  st of expression
65e30 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65  s can.** also be
65e40 20 75 73 65 64 20 61 73 20 74 68 65 20 61 72 67   used as the arg
65e50 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63 74  ument to a funct
65e60 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63 61  ion, in which ca
65e70 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a 2a  se the a.zName.*
65e80 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20 75  * field is not u
65e90 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 45  sed..*/.struct E
65ea0 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20  xprList {.  int 
65eb0 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  nExpr;          
65ec0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
65ed0 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20 74  expressions on t
65ee0 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  he list */.  int
65ef0 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20   nAlloc;        
65f00 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
65f10 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74   entries allocat
65f20 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e  ed below */.  in
65f30 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20 20  t iECursor;     
65f40 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75 72       /* VDBE Cur
65f50 73 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20 77  sor associated w
65f60 69 74 68 20 74 68 69 73 20 45 78 70 72 4c 69 73  ith this ExprLis
65f70 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78  t */.  struct Ex
65f80 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20  prList_item {.  
65f90 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20    Expr *pExpr;  
65fa0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
65fb0 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69  list of expressi
65fc0 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72 20  ons */.    char 
65fd0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
65fe0 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f 63    /* Token assoc
65ff0 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
66000 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
66010 20 20 63 68 61 72 20 2a 7a 53 70 61 6e 3b 20 20    char *zSpan;  
66020 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67           /* Orig
66030 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65  inal text of the
66040 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
66050 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b     u8 sortOrder;
66060 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66            /* 1 f
66070 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72  or DESC or 0 for
66080 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64   ASC */.    u8 d
66090 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  one;            
660a0 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20     /* A flag to 
660b0 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72  indicate when pr
660c0 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69  ocessing is fini
660d0 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20  shed */.    u16 
660e0 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  iCol;           
660f0 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20     /* For ORDER 
66100 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  BY, column numbe
66110 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20  r in result set 
66120 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61  */.    u16 iAlia
66130 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s;            /*
66140 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73   Index into Pars
66150 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a  e.aAlias[] for z
66160 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20  Name */.  } *a; 
66170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
66180 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f   /* One entry fo
66190 72 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f  r each expressio
661a0 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  n */.};../*.** A
661b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
661c0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
661d0 75 73 65 64 20 62 79 20 74 68 65 20 70 61 72 73  used by the pars
661e0 65 72 20 74 6f 20 72 65 63 6f 72 64 20 62 6f 74  er to record bot
661f0 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 20 74  h.** the parse t
66200 72 65 65 20 66 6f 72 20 61 6e 20 65 78 70 72 65  ree for an expre
66210 73 73 69 6f 6e 20 61 6e 64 20 74 68 65 20 73 70  ssion and the sp
66220 61 6e 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74  an of input text
66230 20 66 6f 72 20 61 6e 0a 2a 2a 20 65 78 70 72 65   for an.** expre
66240 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ssion..*/.struct
66250 20 45 78 70 72 53 70 61 6e 20 7b 0a 20 20 45 78   ExprSpan {.  Ex
66260 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20  pr *pExpr;      
66270 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65      /* The expre
66280 73 73 69 6f 6e 20 70 61 72 73 65 20 74 72 65 65  ssion parse tree
66290 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
662a0 20 2a 7a 53 74 61 72 74 3b 20 20 20 2f 2a 20 46   *zStart;   /* F
662b0 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 6f  irst character o
662c0 66 20 69 6e 70 75 74 20 74 65 78 74 20 2a 2f 0a  f input text */.
662d0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
662e0 6e 64 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20 63  nd;     /* One c
662f0 68 61 72 61 63 74 65 72 20 70 61 73 74 20 74 68  haracter past th
66300 65 20 65 6e 64 20 6f 66 20 69 6e 70 75 74 20 74  e end of input t
66310 65 78 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ext */.};../*.**
66320 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
66330 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63  this structure c
66340 61 6e 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65  an hold a simple
66350 20 6c 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66   list of identif
66360 69 65 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73  iers,.** such as
66370 20 74 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63   the list "a,b,c
66380 22 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  " in the followi
66390 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a  ng statements:.*
663a0 2a 0a 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54  *.**      INSERT
663b0 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56   INTO t(a,b,c) V
663c0 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20  ALUES ...;.**   
663d0 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20     CREATE INDEX 
663e0 69 64 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b  idx ON t(a,b,c);
663f0 0a 2a 2a 20 20 20 20 20 20 43 52 45 41 54 45 20  .**      CREATE 
66400 54 52 49 47 47 45 52 20 74 72 69 67 20 42 45 46  TRIGGER trig BEF
66410 4f 52 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28  ORE UPDATE ON t(
66420 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a  a,b,c) ...;.**.*
66430 2a 20 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69  * The IdList.a.i
66440 64 78 20 66 69 65 6c 64 20 69 73 20 75 73 65 64  dx field is used
66450 20 77 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74   when the IdList
66460 20 72 65 70 72 65 73 65 6e 74 73 20 74 68 65 20   represents the 
66470 6c 69 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d  list of.** colum
66480 6e 20 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20  n names after a 
66490 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e  table name in an
664a0 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e   INSERT statemen
664b0 74 2e 20 20 49 6e 20 74 68 65 20 73 74 61 74 65  t.  In the state
664c0 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49  ment.**.**     I
664d0 4e 53 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62  NSERT INTO t(a,b
664e0 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66  ,c) ....**.** If
664f0 20 22 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68   "a" is the k-th
66500 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65   column of table
66510 20 22 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73   "t", then IdLis
66520 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a  t.a[0].idx==k..*
66530 2f 0a 73 74 72 75 63 74 20 49 64 4c 69 73 74 20  /.struct IdList 
66540 7b 0a 20 20 73 74 72 75 63 74 20 49 64 4c 69 73  {.  struct IdLis
66550 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61  t_item {.    cha
66560 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f  r *zName;      /
66570 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64  * Name of the id
66580 65 6e 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20  entifier */.    
66590 69 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20  int idx;        
665a0 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f    /* Index in so
665b0 6d 65 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20  me Table.aCol[] 
665c0 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  of a column name
665d0 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a  d zName */.  } *
665e0 61 3b 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20  a;.  int nId;   
665f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
66600 6f 66 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f  of identifiers o
66610 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20  n the list */.  
66620 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20  int nAlloc;     
66630 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
66640 74 72 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  tries allocated 
66650 66 6f 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f  for a[] below */
66660 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62  .};../*.** The b
66670 69 74 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20  itmask datatype 
66680 64 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73  defined below is
66690 20 75 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75   used for variou
666a0 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e  s optimizations.
666b0 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20  .**.** Changing 
666c0 74 68 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62  this from a 64-b
666d0 69 74 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74  it to a 32-bit t
666e0 79 70 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e  ype limits the n
666f0 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c  umber of.** tabl
66700 65 73 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20  es in a join to 
66710 33 32 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34  32 instead of 64
66720 2e 20 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72  .  But it also r
66730 65 64 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a  educes the size.
66740 2a 2a 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  ** of the librar
66750 79 20 62 79 20 37 33 38 20 62 79 74 65 73 20 6f  y by 738 bytes o
66760 6e 20 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64  n ix86..*/.typed
66770 65 66 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a  ef u64 Bitmask;.
66780 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65  ./*.** The numbe
66790 72 20 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42  r of bits in a B
667a0 69 74 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d  itmask.  "BMS" m
667b0 65 61 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69  eans "BitMask Si
667c0 7a 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ze"..*/.#define 
667d0 42 4d 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65  BMS  ((int)(size
667e0 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a  of(Bitmask)*8)).
667f0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
66800 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 64  wing structure d
66810 65 73 63 72 69 62 65 73 20 74 68 65 20 46 52 4f  escribes the FRO
66820 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45  M clause of a SE
66830 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
66840 2a 2a 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72  ** Each table or
66850 20 73 75 62 71 75 65 72 79 20 69 6e 20 74 68 65   subquery in the
66860 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20   FROM clause is 
66870 61 20 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65  a separate eleme
66880 6e 74 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63  nt of.** the Src
66890 4c 69 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a  List.a[] array..
668a0 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61  **.** With the a
668b0 64 64 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69  ddition of multi
668c0 70 6c 65 20 64 61 74 61 62 61 73 65 20 73 75 70  ple database sup
668d0 70 6f 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  port, the follow
668e0 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  ing structure.**
668f0 20 63 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65   can also be use
66900 64 20 74 6f 20 64 65 73 63 72 69 62 65 20 61 20  d to describe a 
66910 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65  particular table
66920 20 73 75 63 68 20 61 73 20 74 68 65 20 74 61 62   such as the tab
66930 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f  le that.** is mo
66940 64 69 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53  dified by an INS
66950 45 52 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20  ERT, DELETE, or 
66960 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
66970 2e 20 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53  .  In standard S
66980 51 4c 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61  QL,.** such a ta
66990 62 6c 65 20 6d 75 73 74 20 62 65 20 61 20 73 69  ble must be a si
669a0 6d 70 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20  mple name: ID.  
669b0 42 75 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74  But in SQLite, t
669c0 68 65 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20  he table can.** 
669d0 6e 6f 77 20 62 65 20 69 64 65 6e 74 69 66 69 65  now be identifie
669e0 64 20 62 79 20 61 20 64 61 74 61 62 61 73 65 20  d by a database 
669f0 6e 61 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65  name, a dot, the
66a00 6e 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  n the table name
66a10 3a 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54  : ID.ID..**.** T
66a20 68 65 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72  he jointype star
66a30 74 73 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74  ts out showing t
66a40 68 65 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74  he join type bet
66a50 77 65 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74  ween the current
66a60 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68   table.** and th
66a70 65 20 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20  e next table on 
66a80 74 68 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70  the list.  The p
66a90 61 72 73 65 72 20 62 75 69 6c 64 73 20 74 68 65  arser builds the
66aa0 20 6c 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a   list this way..
66ab0 2a 2a 20 42 75 74 20 73 71 6c 69 74 65 33 53 72  ** But sqlite3Sr
66ac0 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79  cListShiftJoinTy
66ad0 70 65 28 29 20 6c 61 74 65 72 20 73 68 69 66 74  pe() later shift
66ae0 73 20 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20  s the jointypes 
66af0 73 6f 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20  so that each.** 
66b00 6a 6f 69 6e 74 79 70 65 20 65 78 70 72 65 73 73  jointype express
66b10 65 73 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77  es the join betw
66b20 65 65 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e  een the table an
66b30 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 74  d the previous t
66b40 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  able..*/.struct 
66b50 53 72 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20  SrcList {.  i16 
66b60 6e 53 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20  nSrc;        /* 
66b70 4e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73  Number of tables
66b80 20 6f 72 20 73 75 62 71 75 65 72 69 65 73 20 69   or subqueries i
66b90 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
66ba0 65 20 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f  e */.  i16 nAllo
66bb0 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  c;      /* Numbe
66bc0 72 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c  r of entries all
66bd0 6f 63 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65  ocated in a[] be
66be0 6c 6f 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  low */.  struct 
66bf0 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20  SrcList_item {. 
66c00 20 20 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61     char *zDataba
66c10 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  se;  /* Name of 
66c20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67  database holding
66c30 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20   this table */. 
66c40 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20     char *zName; 
66c50 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
66c60 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  the table */.   
66c70 20 63 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20   char *zAlias;  
66c80 20 20 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61     /* The "B" pa
66c90 72 74 20 6f 66 20 61 20 22 41 20 41 53 20 42 22  rt of a "A AS B"
66ca0 20 70 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20   phrase.  zName 
66cb0 69 73 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20  is the "A" */.  
66cc0 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
66cd0 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61      /* An SQL ta
66ce0 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  ble correspondin
66cf0 67 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20  g to zName */.  
66d00 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
66d10 74 3b 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20  t;  /* A SELECT 
66d20 73 74 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69  statement used i
66d30 6e 20 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62  n place of a tab
66d40 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75  le name */.    u
66d50 38 20 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20  8 isPopulated;  
66d60 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61   /* Temporary ta
66d70 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ble associated w
66d80 69 74 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f  ith SELECT is po
66d90 70 75 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75  pulated */.    u
66da0 38 20 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20  8 jointype;     
66db0 20 2f 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e   /* Type of join
66dc0 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 61 62   between this ab
66dd0 6c 65 20 61 6e 64 20 74 68 65 20 70 72 65 76 69  le and the previ
66de0 6f 75 73 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f  ous */.    u8 no
66df0 74 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20  tIndexed;    /* 
66e00 54 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73  True if there is
66e10 20 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63   a NOT INDEXED c
66e20 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74  lause */.    int
66e30 20 69 43 75 72 73 6f 72 3b 20 20 20 20 20 20 2f   iCursor;      /
66e40 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
66e50 72 20 6e 75 6d 62 65 72 20 75 73 65 64 20 74 6f  r number used to
66e60 20 61 63 63 65 73 73 20 74 68 69 73 20 74 61 62   access this tab
66e70 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a  le */.    Expr *
66e80 70 4f 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  pOn;        /* T
66e90 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20  he ON clause of 
66ea0 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20 20 49 64  a join */.    Id
66eb0 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b 20 20 20  List *pUsing;   
66ec0 2f 2a 20 54 68 65 20 55 53 49 4e 47 20 63 6c 61  /* The USING cla
66ed0 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f  use of a join */
66ee0 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 63 6f 6c  .    Bitmask col
66ef0 55 73 65 64 3b 20 20 2f 2a 20 42 69 74 20 4e 20  Used;  /* Bit N 
66f00 28 31 3c 3c 4e 29 20 73 65 74 20 69 66 20 63 6f  (1<<N) set if co
66f10 6c 75 6d 6e 20 4e 20 6f 66 20 70 54 61 62 20 69  lumn N of pTab i
66f20 73 20 75 73 65 64 20 2a 2f 0a 20 20 20 20 63 68  s used */.    ch
66f30 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20  ar *zIndex;     
66f40 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 20 66 72  /* Identifier fr
66f50 6f 6d 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c  om "INDEXED BY <
66f60 7a 49 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20  zIndex>" clause 
66f70 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  */.    Index *pI
66f80 6e 64 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65  ndex;    /* Inde
66f90 78 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72  x structure corr
66fa0 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e  esponding to zIn
66fb0 64 65 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20  dex, if any */. 
66fc0 20 7d 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20   } a[1];        
66fd0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72       /* One entr
66fe0 79 20 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74  y for each ident
66ff0 69 66 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73  ifier on the lis
67000 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50  t */.};../*.** P
67010 65 72 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20  ermitted values 
67020 6f 66 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61  of the SrcList.a
67030 2e 6a 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a  .jointype field.
67040 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e  */.#define JT_IN
67050 4e 45 52 20 20 20 20 20 30 78 30 30 30 31 20 20  NER     0x0001  
67060 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66    /* Any kind of
67070 20 69 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20   inner or cross 
67080 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  join */.#define 
67090 4a 54 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30  JT_CROSS     0x0
670a0 30 30 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63  002    /* Explic
670b0 69 74 20 75 73 65 20 6f 66 20 74 68 65 20 43 52  it use of the CR
670c0 4f 53 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23  OSS keyword */.#
670d0 64 65 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41  define JT_NATURA
670e0 4c 20 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a  L   0x0004    /*
670f0 20 54 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74   True for a "nat
67100 75 72 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64  ural" join */.#d
67110 65 66 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20  efine JT_LEFT   
67120 20 20 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20     0x0008    /* 
67130 4c 65 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20  Left outer join 
67140 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49  */.#define JT_RI
67150 47 48 54 20 20 20 20 20 30 78 30 30 31 30 20 20  GHT     0x0010  
67160 20 20 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72    /* Right outer
67170 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65   join */.#define
67180 20 4a 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78   JT_OUTER     0x
67190 30 30 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22  0020    /* The "
671a0 4f 55 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69  OUTER" keyword i
671b0 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65  s present */.#de
671c0 66 69 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20  fine JT_ERROR   
671d0 20 20 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75    0x0040    /* u
671e0 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70  nknown or unsupp
671f0 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20  orted join type 
67200 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65  */.../*.** A Whe
67210 72 65 50 6c 61 6e 20 6f 62 6a 65 63 74 20 68 6f  rePlan object ho
67220 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  lds information 
67230 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 61  that describes a
67240 20 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 74 72 61 74   lookup.** strat
67250 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  egy..**.** This 
67260 6f 62 6a 65 63 74 20 69 73 20 69 6e 74 65 6e 64  object is intend
67270 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20  ed to be opaque 
67280 6f 75 74 73 69 64 65 20 6f 66 20 74 68 65 20 77  outside of the w
67290 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e 0a 2a  here.c module..*
672a0 2a 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65 64  * It is included
672b0 20 68 65 72 65 20 6f 6e 6c 79 20 73 6f 20 74 68   here only so th
672c0 61 74 20 74 68 61 74 20 63 6f 6d 70 69 6c 65 72  at that compiler
672d0 20 77 69 6c 6c 20 6b 6e 6f 77 20 68 6f 77 20 62   will know how b
672e0 69 67 20 69 74 0a 2a 2a 20 69 73 2e 20 20 4e 6f  ig it.** is.  No
672f0 6e 65 20 6f 66 20 74 68 65 20 66 69 65 6c 64 73  ne of the fields
67300 20 69 6e 20 74 68 69 73 20 6f 62 6a 65 63 74 20   in this object 
67310 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 6f  should be used o
67320 75 74 73 69 64 65 20 6f 66 0a 2a 2a 20 74 68 65  utside of.** the
67330 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65 2e   where.c module.
67340 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68  .**.** Within th
67350 65 20 75 6e 69 6f 6e 2c 20 70 49 64 78 20 69 73  e union, pIdx is
67360 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20   only used when 
67370 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 49 4e  wsFlags&WHERE_IN
67380 44 45 58 45 44 20 69 73 20 74 72 75 65 2e 0a 2a  DEXED is true..*
67390 2a 20 70 54 65 72 6d 20 69 73 20 6f 6e 6c 79 20  * pTerm is only 
673a0 75 73 65 64 20 77 68 65 6e 20 77 73 46 6c 61 67  used when wsFlag
673b0 73 26 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52  s&WHERE_MULTI_OR
673c0 20 69 73 20 74 72 75 65 2e 20 20 41 6e 64 20 70   is true.  And p
673d0 56 74 61 62 49 64 78 0a 2a 2a 20 69 73 20 6f 6e  VtabIdx.** is on
673e0 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46  ly used when wsF
673f0 6c 61 67 73 26 57 48 45 52 45 5f 56 49 52 54 55  lags&WHERE_VIRTU
67400 41 4c 54 41 42 4c 45 20 69 73 20 74 72 75 65 2e  ALTABLE is true.
67410 20 20 49 74 20 69 73 20 6e 65 76 65 72 20 74 68    It is never th
67420 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74 20 6d  e.** case that m
67430 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20  ore than one of 
67440 74 68 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73  these conditions
67450 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 72   is true..*/.str
67460 75 63 74 20 57 68 65 72 65 50 6c 61 6e 20 7b 0a  uct WherePlan {.
67470 20 20 75 33 32 20 77 73 46 6c 61 67 73 3b 20 20    u32 wsFlags;  
67480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67490 20 2f 2a 20 57 48 45 52 45 5f 2a 20 66 6c 61 67   /* WHERE_* flag
674a0 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20  s that describe 
674b0 74 68 65 20 73 74 72 61 74 65 67 79 20 2a 2f 0a  the strategy */.
674c0 20 20 75 33 32 20 6e 45 71 3b 20 20 20 20 20 20    u32 nEq;      
674d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
674e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d   /* Number of ==
674f0 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
67500 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 49 6e    union {.    In
67510 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20  dex *pIdx;      
67520 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67530 49 6e 64 65 78 20 77 68 65 6e 20 57 48 45 52 45  Index when WHERE
67540 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 65  _INDEXED is true
67550 20 2a 2f 0a 20 20 20 20 73 74 72 75 63 74 20 57   */.    struct W
67560 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b  hereTerm *pTerm;
67570 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20         /* WHERE 
67580 63 6c 61 75 73 65 20 74 65 72 6d 20 66 6f 72 20  clause term for 
67590 4f 52 2d 73 65 61 72 63 68 20 2a 2f 0a 20 20 20  OR-search */.   
675a0 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
675b0 6e 66 6f 20 2a 70 56 74 61 62 49 64 78 3b 20 20  nfo *pVtabIdx;  
675c0 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
675d0 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 2a 2f   index to use */
675e0 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  .  } u;.};../*.*
675f0 2a 20 46 6f 72 20 65 61 63 68 20 6e 65 73 74 65  * For each neste
67600 64 20 6c 6f 6f 70 20 69 6e 20 61 20 57 48 45 52  d loop in a WHER
67610 45 20 63 6c 61 75 73 65 20 69 6d 70 6c 65 6d 65  E clause impleme
67620 6e 74 61 74 69 6f 6e 2c 20 74 68 65 20 57 68 65  ntation, the Whe
67630 72 65 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74  reInfo.** struct
67640 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 73  ure contains a s
67650 69 6e 67 6c 65 20 69 6e 73 74 61 6e 63 65 20 6f  ingle instance o
67660 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
67670 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72  .  This structur
67680 65 0a 2a 2a 20 69 73 20 69 6e 74 65 6e 64 65 64  e.** is intended
67690 20 74 6f 20 62 65 20 70 72 69 76 61 74 65 20 74   to be private t
676a0 68 65 20 74 68 65 20 77 68 65 72 65 2e 63 20 6d  he the where.c m
676b0 6f 64 75 6c 65 20 61 6e 64 20 73 68 6f 75 6c 64  odule and should
676c0 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65 73   not be.** acces
676d0 73 20 6f 72 20 6d 6f 64 69 66 69 65 64 20 62 79  s or modified by
676e0 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 2e 0a   other modules..
676f0 2a 2a 0a 2a 2a 20 54 68 65 20 70 49 64 78 49 6e  **.** The pIdxIn
67700 66 6f 20 66 69 65 6c 64 20 69 73 20 75 73 65 64  fo field is used
67710 20 74 6f 20 68 65 6c 70 20 70 69 63 6b 20 74 68   to help pick th
67720 65 20 62 65 73 74 20 69 6e 64 65 78 20 6f 6e 20  e best index on 
67730 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62  a.** virtual tab
67740 6c 65 2e 20 20 54 68 65 20 70 49 64 78 49 6e 66  le.  The pIdxInf
67750 6f 20 70 6f 69 6e 74 65 72 20 63 6f 6e 74 61 69  o pointer contai
67760 6e 73 20 69 6e 64 65 78 69 6e 67 0a 2a 2a 20 69  ns indexing.** i
67770 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74  nformation for t
67780 68 65 20 69 2d 74 68 20 74 61 62 6c 65 20 69 6e  he i-th table in
67790 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
677a0 20 62 65 66 6f 72 65 20 72 65 6f 72 64 65 72 69   before reorderi
677b0 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 74 68 65 20 70  ng..** All the p
677c0 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 73  IdxInfo pointers
677d0 20 61 72 65 20 66 72 65 65 64 20 62 79 20 77 68   are freed by wh
677e0 65 72 65 49 6e 66 6f 46 72 65 65 28 29 20 69 6e  ereInfoFree() in
677f0 20 77 68 65 72 65 2e 63 2e 0a 2a 2a 20 41 6c 6c   where.c..** All
67800 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69   other informati
67810 6f 6e 20 69 6e 20 74 68 65 20 69 2d 74 68 20 57  on in the i-th W
67820 68 65 72 65 4c 65 76 65 6c 20 6f 62 6a 65 63 74  hereLevel object
67830 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61   for the i-th ta
67840 62 6c 65 0a 2a 2a 20 61 66 74 65 72 20 46 52 4f  ble.** after FRO
67850 4d 20 63 6c 61 75 73 65 20 6f 72 64 65 72 69 6e  M clause orderin
67860 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65  g..*/.struct Whe
67870 72 65 4c 65 76 65 6c 20 7b 0a 20 20 57 68 65 72  reLevel {.  Wher
67880 65 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20 20  ePlan plan;     
67890 20 20 2f 2a 20 71 75 65 72 79 20 70 6c 61 6e 20    /* query plan 
678a0 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74  for this element
678b0 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   of the FROM cla
678c0 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65  use */.  int iLe
678d0 66 74 4a 6f 69 6e 3b 20 20 20 20 20 20 20 20 2f  ftJoin;        /
678e0 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73  * Memory cell us
678f0 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
67900 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20  LEFT OUTER JOIN 
67910 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 43 75 72  */.  int iTabCur
67920 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
67930 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 73  e VDBE cursor us
67940 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ed to access the
67950 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
67960 69 49 64 78 43 75 72 3b 20 20 20 20 20 20 20 20  iIdxCur;        
67970 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75    /* The VDBE cu
67980 72 73 6f 72 20 75 73 65 64 20 74 6f 20 61 63 63  rsor used to acc
67990 65 73 73 20 70 49 64 78 20 2a 2f 0a 20 20 69 6e  ess pIdx */.  in
679a0 74 20 61 64 64 72 42 72 6b 3b 20 20 20 20 20 20  t addrBrk;      
679b0 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65      /* Jump here
679c0 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
679d0 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69   the loop */.  i
679e0 6e 74 20 61 64 64 72 4e 78 74 3b 20 20 20 20 20  nt addrNxt;     
679f0 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
67a00 65 20 74 6f 20 73 74 61 72 74 20 74 68 65 20 6e  e to start the n
67a10 65 78 74 20 49 4e 20 63 6f 6d 62 69 6e 61 74 69  ext IN combinati
67a20 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  on */.  int addr
67a30 43 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Cont;         /*
67a40 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 63 6f   Jump here to co
67a50 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68 65 20  ntinue with the 
67a60 6e 65 78 74 20 6c 6f 6f 70 20 63 79 63 6c 65 20  next loop cycle 
67a70 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 46 69 72  */.  int addrFir
67a80 73 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69  st;        /* Fi
67a90 72 73 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  rst instruction 
67aa0 6f 66 20 69 6e 74 65 72 69 6f 72 20 6f 66 20 74  of interior of t
67ab0 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 75 38 20  he loop */.  u8 
67ac0 69 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20  iFrom;          
67ad0 20 20 20 2f 2a 20 57 68 69 63 68 20 65 6e 74 72     /* Which entr
67ae0 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  y in the FROM cl
67af0 61 75 73 65 20 2a 2f 0a 20 20 75 38 20 6f 70 2c  ause */.  u8 op,
67b00 20 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20   p5;            
67b10 2f 2a 20 4f 70 63 6f 64 65 20 61 6e 64 20 50 35  /* Opcode and P5
67b20 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20 74   of the opcode t
67b30 68 61 74 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f  hat ends the loo
67b40 70 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20 70  p */.  int p1, p
67b50 32 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  2;           /* 
67b60 4f 70 65 72 61 6e 64 73 20 6f 66 20 74 68 65 20  Operands of the 
67b70 6f 70 63 6f 64 65 20 75 73 65 64 20 74 6f 20 65  opcode used to e
67b80 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a  nds the loop */.
67b90 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20    union {       
67ba0 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72          /* Infor
67bb0 6d 61 74 69 6f 6e 20 74 68 61 74 20 64 65 70 65  mation that depe
67bc0 6e 64 73 20 6f 6e 20 70 6c 61 6e 2e 77 73 46 6c  nds on plan.wsFl
67bd0 61 67 73 20 2a 2f 0a 20 20 20 20 73 74 72 75 63  ags */.    struc
67be0 74 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 49  t {.      int nI
67bf0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
67c00 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
67c10 72 69 65 73 20 69 6e 20 61 49 6e 4c 6f 6f 70 5b  ries in aInLoop[
67c20 5d 20 2a 2f 0a 20 20 20 20 20 20 73 74 72 75 63  ] */.      struc
67c30 74 20 49 6e 4c 6f 6f 70 20 7b 0a 20 20 20 20 20  t InLoop {.     
67c40 20 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20     int iCur;    
67c50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
67c60 20 56 44 42 45 20 63 75 72 73 6f 72 20 75 73 65   VDBE cursor use
67c70 64 20 62 79 20 74 68 69 73 20 49 4e 20 6f 70 65  d by this IN ope
67c80 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20  rator */.       
67c90 20 69 6e 74 20 61 64 64 72 49 6e 54 6f 70 3b 20   int addrInTop; 
67ca0 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f          /* Top o
67cb0 66 20 74 68 65 20 49 4e 20 6c 6f 6f 70 20 2a 2f  f the IN loop */
67cc0 0a 20 20 20 20 20 20 7d 20 2a 61 49 6e 4c 6f 6f  .      } *aInLoo
67cd0 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p;           /* 
67ce0 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
67cf0 74 20 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e  t each nested IN
67d00 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20   operator */.   
67d10 20 7d 20 69 6e 3b 20 20 20 20 20 20 20 20 20 20   } in;          
67d20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77         /* Used w
67d30 68 65 6e 20 70 6c 61 6e 2e 77 73 46 6c 61 67 73  hen plan.wsFlags
67d40 26 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 2a  &WHERE_IN_ABLE *
67d50 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 54  /.  } u;..  /* T
67d60 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69 65  he following fie
67d70 6c 64 20 69 73 20 72 65 61 6c 6c 79 20 6e 6f 74  ld is really not
67d80 20 70 61 72 74 20 6f 66 20 74 68 65 20 63 75 72   part of the cur
67d90 72 65 6e 74 20 6c 65 76 65 6c 2e 20 20 42 75 74  rent level.  But
67da0 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64 20 61 20  .  ** we need a 
67db0 70 6c 61 63 65 20 74 6f 20 63 61 63 68 65 20 76  place to cache v
67dc0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 64  irtual table ind
67dd0 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ex information f
67de0 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 76 69 72  or each.  ** vir
67df0 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 20 74 68  tual table in th
67e00 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e  e FROM clause an
67e10 64 20 74 68 65 20 57 68 65 72 65 4c 65 76 65 6c  d the WhereLevel
67e20 20 73 74 72 75 63 74 75 72 65 20 69 73 0a 20 20   structure is.  
67e30 2a 2a 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20  ** a convenient 
67e40 70 6c 61 63 65 20 73 69 6e 63 65 20 74 68 65 72  place since ther
67e50 65 20 69 73 20 6f 6e 65 20 57 68 65 72 65 4c 65  e is one WhereLe
67e60 76 65 6c 20 66 6f 72 20 65 61 63 68 20 46 52 4f  vel for each FRO
67e70 4d 20 63 6c 61 75 73 65 0a 20 20 2a 2a 20 65 6c  M clause.  ** el
67e80 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71  ement..  */.  sq
67e90 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
67ea0 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a 20   *pIdxInfo;  /* 
67eb0 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20 6e  Index info for n
67ec0 2d 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c 65  -th source table
67ed0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6c   */.};../*.** Fl
67ee0 61 67 73 20 61 70 70 72 6f 70 72 69 61 74 65 20  ags appropriate 
67ef0 66 6f 72 20 74 68 65 20 77 63 74 72 6c 46 6c 61  for the wctrlFla
67f00 67 73 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20  gs parameter of 
67f10 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
67f20 6e 28 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 57  n().** and the W
67f30 68 65 72 65 49 6e 66 6f 2e 77 63 74 72 6c 46 6c  hereInfo.wctrlFl
67f40 61 67 73 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23  ags member..*/.#
67f50 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44  define WHERE_ORD
67f60 45 52 42 59 5f 4e 4f 52 4d 41 4c 20 20 20 30 78  ERBY_NORMAL   0x
67f70 30 30 30 30 20 2f 2a 20 4e 6f 2d 6f 70 20 2a 2f  0000 /* No-op */
67f80 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f  .#define WHERE_O
67f90 52 44 45 52 42 59 5f 4d 49 4e 20 20 20 20 20 20  RDERBY_MIN      
67fa0 30 78 30 30 30 31 20 2f 2a 20 4f 52 44 45 52 20  0x0001 /* ORDER 
67fb0 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f  BY processing fo
67fc0 72 20 6d 69 6e 28 29 20 66 75 6e 63 20 2a 2f 0a  r min() func */.
67fd0 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52  #define WHERE_OR
67fe0 44 45 52 42 59 5f 4d 41 58 20 20 20 20 20 20 30  DERBY_MAX      0
67ff0 78 30 30 30 32 20 2f 2a 20 4f 52 44 45 52 20 42  x0002 /* ORDER B
68000 59 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72  Y processing for
68010 20 6d 61 78 28 29 20 66 75 6e 63 20 2a 2f 0a 23   max() func */.#
68020 64 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4e 45  define WHERE_ONE
68030 50 41 53 53 5f 44 45 53 49 52 45 44 20 20 30 78  PASS_DESIRED  0x
68040 30 30 30 34 20 2f 2a 20 57 61 6e 74 20 74 6f 20  0004 /* Want to 
68050 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50 44 41  do one-pass UPDA
68060 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 23 64 65  TE/DELETE */.#de
68070 66 69 6e 65 20 57 48 45 52 45 5f 44 55 50 4c 49  fine WHERE_DUPLI
68080 43 41 54 45 53 5f 4f 4b 20 20 20 20 30 78 30 30  CATES_OK    0x00
68090 30 38 20 2f 2a 20 4f 6b 20 74 6f 20 72 65 74 75  08 /* Ok to retu
680a0 72 6e 20 61 20 72 6f 77 20 6d 6f 72 65 20 74 68  rn a row more th
680b0 61 6e 20 6f 6e 63 65 20 2a 2f 0a 23 64 65 66 69  an once */.#defi
680c0 6e 65 20 57 48 45 52 45 5f 4f 4d 49 54 5f 4f 50  ne WHERE_OMIT_OP
680d0 45 4e 20 20 20 20 20 20 20 20 30 78 30 30 31 30  EN        0x0010
680e0 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72   /* Table cursor
680f0 20 61 72 65 20 61 6c 72 65 61 64 79 20 6f 70 65   are already ope
68100 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  n */.#define WHE
68110 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 20 20  RE_OMIT_CLOSE   
68120 20 20 20 20 30 78 30 30 32 30 20 2f 2a 20 4f 6d      0x0020 /* Om
68130 69 74 20 63 6c 6f 73 65 20 6f 66 20 74 61 62 6c  it close of tabl
68140 65 20 26 20 69 6e 64 65 78 20 63 75 72 73 6f 72  e & index cursor
68150 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  s */.#define WHE
68160 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c 45 20 20  RE_FORCE_TABLE  
68170 20 20 20 20 30 78 30 30 34 30 20 2f 2a 20 44 6f      0x0040 /* Do
68180 20 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e 64 65   not use an inde
68190 78 2d 6f 6e 6c 79 20 73 65 61 72 63 68 20 2a 2f  x-only search */
681a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52  ../*.** The WHER
681b0 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73  E clause process
681c0 69 6e 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20  ing routine has 
681d0 74 77 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65  two halves.  The
681e0 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 74 20 64  .** first part d
681f0 6f 65 73 20 74 68 65 20 73 74 61 72 74 20 6f 66  oes the start of
68200 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20   the WHERE loop 
68210 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a  and the second.*
68220 2a 20 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20  * half does the 
68230 74 61 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52  tail of the WHER
68240 45 20 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74  E loop.  An inst
68250 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20  ance of.** this 
68260 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 74  structure is ret
68270 75 72 6e 65 64 20 62 79 20 74 68 65 20 66 69 72  urned by the fir
68280 73 74 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73  st half and pass
68290 65 64 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73  ed.** into the s
682a0 65 63 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69  econd half to gi
682b0 76 65 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69  ve some continui
682c0 74 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68  ty..*/.struct Wh
682d0 65 72 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73  ereInfo {.  Pars
682e0 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
682f0 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20   /* Parsing and 
68300 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20  code generating 
68310 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 75 31 36  context */.  u16
68320 20 77 63 74 72 6c 46 6c 61 67 73 3b 20 20 20 20   wctrlFlags;    
68330 20 20 2f 2a 20 46 6c 61 67 73 20 6f 72 69 67 69    /* Flags origi
68340 6e 61 6c 6c 79 20 70 61 73 73 65 64 20 74 6f 20  nally passed to 
68350 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
68360 6e 28 29 20 2a 2f 0a 20 20 75 38 20 6f 6b 4f 6e  n() */.  u8 okOn
68370 65 50 61 73 73 3b 20 20 20 20 20 20 20 20 2f 2a  ePass;        /*
68380 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65 2d 70   Ok to use one-p
68390 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f  ass algorithm fo
683a0 72 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45  r UPDATE or DELE
683b0 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  TE */.  SrcList 
683c0 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 20 20 20  *pTabList;      
683d0 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
683e0 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  f tables in the 
683f0 6a 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54  join */.  int iT
68400 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
68410 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
68420 76 65 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f  very beginning o
68430 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70  f the WHERE loop
68440 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69   */.  int iConti
68450 6e 75 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  nue;            
68460 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
68470 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69  e to continue wi
68480 74 68 20 6e 65 78 74 20 72 65 63 6f 72 64 20 2a  th next record *
68490 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 3b 20  /.  int iBreak; 
684a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
684b0 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20     /* Jump here 
684c0 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20  to break out of 
684d0 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  the loop */.  in
684e0 74 20 6e 4c 65 76 65 6c 3b 20 20 20 20 20 20 20  t nLevel;       
684f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
68500 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64  Number of nested
68510 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 74 72 75 63   loop */.  struc
68520 74 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70  t WhereClause *p
68530 57 43 3b 20 20 20 20 20 20 20 2f 2a 20 44 65 63  WC;       /* Dec
68540 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68  omposition of th
68550 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
68560 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 61  /.  WhereLevel a
68570 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20  [1];            
68580 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
68590 6e 20 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73  n about each nes
685a0 74 20 6c 6f 6f 70 20 69 6e 20 57 48 45 52 45 20  t loop in WHERE 
685b0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e  */.};../*.** A N
685c0 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 66 69 6e  ameContext defin
685d0 65 73 20 61 20 63 6f 6e 74 65 78 74 20 69 6e 20  es a context in 
685e0 77 68 69 63 68 20 74 6f 20 72 65 73 6f 6c 76 65  which to resolve
685f0 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
68600 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54 68 65  n.** names.  The
68610 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69 73 74   context consist
68620 73 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20 74  s of a list of t
68630 61 62 6c 65 73 20 28 74 68 65 20 70 53 72 63 4c  ables (the pSrcL
68640 69 73 74 29 20 66 69 65 6c 64 20 61 6e 64 0a 2a  ist) field and.*
68650 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65  * a list of name
68660 64 20 65 78 70 72 65 73 73 69 6f 6e 20 28 70 45  d expression (pE
68670 4c 69 73 74 29 2e 20 20 54 68 65 20 6e 61 6d 65  List).  The name
68680 64 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  d expression lis
68690 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55 4c 4c  t may.** be NULL
686a0 2e 20 20 54 68 65 20 70 53 72 63 20 63 6f 72 72  .  The pSrc corr
686b0 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 46  esponds to the F
686c0 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20  ROM clause of a 
686d0 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74 6f 20  SELECT or.** to 
686e0 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
686f0 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 79 20 49  operated on by I
68700 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f  NSERT, UPDATE, o
68710 72 20 44 45 4c 45 54 45 2e 20 20 54 68 65 0a 2a  r DELETE.  The.*
68720 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 65 73 70  * pEList corresp
68730 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 65 73 75  onds to the resu
68740 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45  lt set of a SELE
68750 43 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c 20 66  CT and is NULL f
68760 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74 61 74  or.** other stat
68770 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 61  ements..**.** Na
68780 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 6e 20 62  meContexts can b
68790 65 20 6e 65 73 74 65 64 2e 20 20 57 68 65 6e 20  e nested.  When 
687a0 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 2c  resolving names,
687b0 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20   the inner-most 
687c0 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 73  .** context is s
687d0 65 61 72 63 68 65 64 20 66 69 72 73 74 2e 20 20  earched first.  
687e0 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66  If no match is f
687f0 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 74 20 6f  ound, the next o
68800 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78 74 20  uter.** context 
68810 69 73 20 63 68 65 63 6b 65 64 2e 20 20 49 66 20  is checked.  If 
68820 74 68 65 72 65 20 69 73 20 73 74 69 6c 6c 20 6e  there is still n
68830 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 6e 65 78  o match, the nex
68840 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69 73 20  t context.** is 
68850 63 68 65 63 6b 65 64 2e 20 20 54 68 69 73 20 70  checked.  This p
68860 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75 65 73  rocess continues
68870 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20 61 20   until either a 
68880 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 0a 2a  match is found.*
68890 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65 78 74  * or all context
688a0 73 20 61 72 65 20 63 68 65 63 6b 2e 20 20 57 68  s are check.  Wh
688b0 65 6e 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f  en a match is fo
688c0 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 20 6d 65  und, the nRef me
688d0 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65 20 63  mber of.** the c
688e0 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e 69 6e  ontext containin
688f0 67 20 74 68 65 20 6d 61 74 63 68 20 69 73 20 69  g the match is i
68900 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a 2a 0a  ncremented. .**.
68910 2a 2a 20 45 61 63 68 20 73 75 62 71 75 65 72 79  ** Each subquery
68920 20 67 65 74 73 20 61 20 6e 65 77 20 4e 61 6d 65   gets a new Name
68930 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 70 4e  Context.  The pN
68940 65 78 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73  ext field points
68950 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d 65 43   to the.** NameC
68960 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20 70 61  ontext in the pa
68970 72 65 6e 74 20 71 75 65 72 79 2e 20 20 54 68 75  rent query.  Thu
68980 73 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66  s the process of
68990 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a 2a 2a   scanning the.**
689a0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c 69 73   NameContext lis
689b0 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f  t corresponds to
689c0 20 73 65 61 72 63 68 69 6e 67 20 74 68 72 6f 75   searching throu
689d0 67 68 20 73 75 63 63 65 73 73 69 76 65 6c 79 20  gh successively 
689e0 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 75 65 72  outer.** subquer
689f0 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  ies looking for 
68a00 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 72 75  a match..*/.stru
68a10 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 7b  ct NameContext {
68a20 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
68a30 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  ;       /* The p
68a40 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69  arser */.  SrcLi
68a50 73 74 20 2a 70 53 72 63 4c 69 73 74 3b 20 20 20  st *pSrcList;   
68a60 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 74  /* One or more t
68a70 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 72 65  ables used to re
68a80 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f 0a 20  solve names */. 
68a90 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
68aa0 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 61  t;    /* Optiona
68ab0 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65 64 20  l list of named 
68ac0 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20  expressions */. 
68ad0 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
68ae0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
68af0 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c 76 65  of names resolve
68b00 64 20 62 79 20 74 68 69 73 20 63 6f 6e 74 65 78  d by this contex
68b10 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b  t */.  int nErr;
68b20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
68b30 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
68b40 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68 69 6c  encountered whil
68b50 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65  e resolving name
68b60 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f 77 41  s */.  u8 allowA
68b70 67 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  gg;         /* A
68b80 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
68b90 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 72 65 20  ns allowed here 
68ba0 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 67 3b 20  */.  u8 hasAgg; 
68bb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
68bc0 65 20 69 66 20 61 67 67 72 65 67 61 74 65 73 20  e if aggregates 
68bd0 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38  are seen */.  u8
68be0 20 69 73 43 68 65 63 6b 3b 20 20 20 20 20 20 20   isCheck;       
68bf0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 65     /* True if re
68c00 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e  solving names in
68c10 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61   a CHECK constra
68c20 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 65  int */.  int nDe
68c30 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  pth;          /*
68c40 20 44 65 70 74 68 20 6f 66 20 73 75 62 71 75 65   Depth of subque
68c50 72 79 20 72 65 63 75 72 73 69 6f 6e 2e 20 31 20  ry recursion. 1 
68c60 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 69 6f 6e  for no recursion
68c70 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70   */.  AggInfo *p
68c80 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20 49 6e  AggInfo;   /* In
68c90 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
68ca0 61 67 67 72 65 67 61 74 65 73 20 61 74 20 74 68  aggregates at th
68cb0 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 4e 61  is level */.  Na
68cc0 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65 78 74  meContext *pNext
68cd0 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74 65 72  ;  /* Next outer
68ce0 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20   name context.  
68cf0 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72 6d 6f  NULL for outermo
68d00 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  st */.};../*.** 
68d10 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
68d20 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
68d30 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20  ucture contains 
68d40 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  all information.
68d50 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 67 65 6e  ** needed to gen
68d60 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61  erate code for a
68d70 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 54 20 73   single SELECT s
68d80 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
68d90 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 20 74 6f  nLimit is set to
68da0 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73 20   -1 if there is 
68db0 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e  no LIMIT clause.
68dc0 20 20 6e 4f 66 66 73 65 74 20 69 73 20 73 65 74    nOffset is set
68dd0 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74 68 65   to 0..** If the
68de0 72 65 20 69 73 20 61 20 4c 49 4d 49 54 20 63 6c  re is a LIMIT cl
68df0 61 75 73 65 2c 20 74 68 65 20 70 61 72 73 65 72  ause, the parser
68e00 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 74 6f 20   sets nLimit to 
68e10 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
68e20 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20 6e 4f  .** limit and nO
68e30 66 66 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c  ffset to the val
68e40 75 65 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74  ue of the offset
68e50 20 28 6f 72 20 30 20 69 66 20 74 68 65 72 65 20   (or 0 if there 
68e60 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73 65 74  is not.** offset
68e70 29 2e 20 20 42 75 74 20 6c 61 74 65 72 20 6f 6e  ).  But later on
68e80 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e 4f 66  , nLimit and nOf
68e90 66 73 65 74 20 62 65 63 6f 6d 65 20 74 68 65 20  fset become the 
68ea0 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
68eb0 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 42 45 20  .** in the VDBE 
68ec0 74 68 61 74 20 72 65 63 6f 72 64 20 74 68 65 20  that record the 
68ed0 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73 65 74  limit and offset
68ee0 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a   counters..**.**
68ef0 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 5d 20   addrOpenEphm[] 
68f00 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 69 6e 20  entries contain 
68f10 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 4f  the address of O
68f20 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20  P_OpenEphemeral 
68f30 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68 65 73  opcodes..** Thes
68f40 65 20 61 64 64 72 65 73 73 65 73 20 6d 75 73 74  e addresses must
68f50 20 62 65 20 73 74 6f 72 65 64 20 73 6f 20 74 68   be stored so th
68f60 61 74 20 77 65 20 63 61 6e 20 67 6f 20 62 61 63  at we can go bac
68f70 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a 2a 2a  k and fill in.**
68f80 20 74 68 65 20 50 34 5f 4b 45 59 49 4e 46 4f 20   the P4_KEYINFO 
68f90 61 6e 64 20 50 32 20 70 61 72 61 6d 65 74 65 72  and P2 parameter
68fa0 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 74 68 65  s later.  Neithe
68fb0 72 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 6e 6f  r the KeyInfo no
68fc0 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  r.** the number 
68fd0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 50 32  of columns in P2
68fe0 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 74 65 64   can be computed
68ff0 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
69000 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50 5f 4f  e.** as the OP_O
69010 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 75 63 74  penEphm instruct
69020 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 62 65 63  ion is coded bec
69030 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e 6f 75  ause not.** enou
69040 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  gh information a
69050 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f 75 6e  bout the compoun
69060 64 20 71 75 65 72 79 20 69 73 20 6b 6e 6f 77 6e  d query is known
69070 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a   at that point..
69080 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66  ** The KeyInfo f
69090 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b  or addrOpenTran[
690a0 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e 74 61  0] and [1] conta
690b0 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ins collating se
690c0 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72 20 74  quences.** for t
690d0 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20  he result set.  
690e0 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20  The KeyInfo for 
690f0 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32 5d 20  addrOpenTran[2] 
69100 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69  contains collati
69110 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 20  ng.** sequences 
69120 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20 42 59  for the ORDER BY
69130 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 72 75   clause..*/.stru
69140 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 20 45 78  ct Select {.  Ex
69150 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20  prList *pEList; 
69160 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 65 6c       /* The fiel
69170 64 73 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ds of the result
69180 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20   */.  u8 op;    
69190 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
691a0 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49 4f 4e  One of: TK_UNION
691b0 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54 45 52   TK_ALL TK_INTER
691c0 53 45 43 54 20 54 4b 5f 45 58 43 45 50 54 20 2a  SECT TK_EXCEPT *
691d0 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69 74  /.  char affinit
691e0 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  y;         /* Ma
691f0 6b 65 52 65 63 6f 72 64 20 77 69 74 68 20 74 68  keRecord with th
69200 69 73 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20  is affinity for 
69210 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 75 31 36  SRT_Set */.  u16
69220 20 73 65 6c 46 6c 61 67 73 3b 20 20 20 20 20 20   selFlags;      
69230 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 53      /* Various S
69240 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20  F_* values */.  
69250 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20  SrcList *pSrc;  
69260 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 46 52         /* The FR
69270 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45  OM clause */.  E
69280 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20  xpr *pWhere;    
69290 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
692a0 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45  RE clause */.  E
692b0 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42  xprList *pGroupB
692c0 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f  y;    /* The GRO
692d0 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  UP BY clause */.
692e0 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e 67 3b    Expr *pHaving;
692f0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
69300 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20 2a 2f  HAVING clause */
69310 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
69320 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54 68 65  derBy;    /* The
69330 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
69340 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50   */.  Select *pP
69350 72 69 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  rior;        /* 
69360 50 72 69 6f 72 20 73 65 6c 65 63 74 20 69 6e 20  Prior select in 
69370 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63  a compound selec
69380 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  t statement */. 
69390 20 53 65 6c 65 63 74 20 2a 70 4e 65 78 74 3b 20   Select *pNext; 
693a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
693b0 73 65 6c 65 63 74 20 74 6f 20 74 68 65 20 6c 65  select to the le
693c0 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  ft in a compound
693d0 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 52   */.  Select *pR
693e0 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f 2a 20  ightmost;    /* 
693f0 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c 65 63  Right-most selec
69400 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20  t in a compound 
69410 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  select statement
69420 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d   */.  Expr *pLim
69430 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  it;          /* 
69440 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69 6f 6e  LIMIT expression
69450 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e 6f 74  . NULL means not
69460 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78 70 72   used. */.  Expr
69470 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 20 20 20   *pOffset;      
69480 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 65 78 70     /* OFFSET exp
69490 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65  ression. NULL me
694a0 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20 2a 2f  ans not used. */
694b0 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20 69  .  int iLimit, i
694c0 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d 65 6d  Offset;   /* Mem
694d0 6f 72 79 20 72 65 67 69 73 74 65 72 73 20 68 6f  ory registers ho
694e0 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 20 4f 46  lding LIMIT & OF
694f0 46 53 45 54 20 63 6f 75 6e 74 65 72 73 20 2a 2f  FSET counters */
69500 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 65 6e 45  .  int addrOpenE
69510 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f 50 5f  phm[3];   /* OP_
69520 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f 64 65  OpenEphem opcode
69530 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74 68 69  s related to thi
69540 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b 0a 0a  s select */.};..
69550 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61  /*.** Allowed va
69560 6c 75 65 73 20 66 6f 72 20 53 65 6c 65 63 74 2e  lues for Select.
69570 73 65 6c 46 6c 61 67 73 2e 20 20 54 68 65 20 22  selFlags.  The "
69580 53 46 22 20 70 72 65 66 69 78 20 73 74 61 6e 64  SF" prefix stand
69590 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c 65 63 74  s for.** "Select
695a0 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65 66 69   Flag"..*/.#defi
695b0 6e 65 20 53 46 5f 44 69 73 74 69 6e 63 74 20 20  ne SF_Distinct  
695c0 20 20 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a        0x0001  /*
695d0 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 62   Output should b
695e0 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a 23 64  e DISTINCT */.#d
695f0 65 66 69 6e 65 20 53 46 5f 52 65 73 6f 6c 76 65  efine SF_Resolve
69600 64 20 20 20 20 20 20 20 20 30 78 30 30 30 32 20  d        0x0002 
69610 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72 73 20   /* Identifiers 
69620 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76  have been resolv
69630 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46  ed */.#define SF
69640 5f 41 67 67 72 65 67 61 74 65 20 20 20 20 20 20  _Aggregate      
69650 20 30 78 30 30 30 34 20 20 2f 2a 20 43 6f 6e 74   0x0004  /* Cont
69660 61 69 6e 73 20 61 67 67 72 65 67 61 74 65 20 66  ains aggregate f
69670 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66  unctions */.#def
69680 69 6e 65 20 53 46 5f 55 73 65 73 45 70 68 65 6d  ine SF_UsesEphem
69690 65 72 61 6c 20 20 20 30 78 30 30 30 38 20 20 2f  eral   0x0008  /
696a0 2a 20 55 73 65 73 20 74 68 65 20 4f 70 65 6e 45  * Uses the OpenE
696b0 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64 65 20  phemeral opcode 
696c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 45 78  */.#define SF_Ex
696d0 70 61 6e 64 65 64 20 20 20 20 20 20 20 20 30 78  panded        0x
696e0 30 30 31 30 20 20 2f 2a 20 73 71 6c 69 74 65 33  0010  /* sqlite3
696f0 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 63  SelectExpand() c
69700 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20 2a 2f  alled on this */
69710 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 61 73 54  .#define SF_HasT
69720 79 70 65 49 6e 66 6f 20 20 20 20 20 30 78 30 30  ypeInfo     0x00
69730 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75 62 71  20  /* FROM subq
69740 75 65 72 69 65 73 20 68 61 76 65 20 54 61 62 6c  ueries have Tabl
69750 65 20 6d 65 74 61 64 61 74 61 20 2a 2f 0a 0a 0a  e metadata */...
69760 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  /*.** The result
69770 73 20 6f 66 20 61 20 73 65 6c 65 63 74 20 63 61  s of a select ca
69780 6e 20 62 65 20 64 69 73 74 72 69 62 75 74 65 64  n be distributed
69790 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61 79 73   in several ways
697a0 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52 54 22 20  .  The.** "SRT" 
697b0 70 72 65 66 69 78 20 6d 65 61 6e 73 20 22 53 45  prefix means "SE
697c0 4c 45 43 54 20 52 65 73 75 6c 74 20 54 79 70 65  LECT Result Type
697d0 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  "..*/.#define SR
697e0 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20 20 31  T_Union        1
697f0 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c    /* Store resul
69800 74 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20  t as keys in an 
69810 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65  index */.#define
69820 20 53 52 54 5f 45 78 63 65 70 74 20 20 20 20 20   SRT_Except     
69830 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65 20 72    2  /* Remove r
69840 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 55 4e 49  esult from a UNI
69850 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66  ON index */.#def
69860 69 6e 65 20 53 52 54 5f 45 78 69 73 74 73 20 20  ine SRT_Exists  
69870 20 20 20 20 20 33 20 20 2f 2a 20 53 74 6f 72 65       3  /* Store
69880 20 31 20 69 66 20 74 68 65 20 72 65 73 75 6c 74   1 if the result
69890 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 2a 2f   is not empty */
698a0 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 44 69 73  .#define SRT_Dis
698b0 63 61 72 64 20 20 20 20 20 20 34 20 20 2f 2a 20  card      4  /* 
698c0 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68 65 20  Do not save the 
698d0 72 65 73 75 6c 74 73 20 61 6e 79 77 68 65 72 65  results anywhere
698e0 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52 44 45   */../* The ORDE
698f0 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 69  R BY clause is i
69900 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c 20 6f  gnored for all o
69910 66 20 74 68 65 20 61 62 6f 76 65 20 2a 2f 0a 23  f the above */.#
69920 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 62 6c 65  define Ignorable
69930 4f 72 64 65 72 62 79 28 58 29 20 28 28 58 2d 3e  Orderby(X) ((X->
69940 65 44 65 73 74 29 3c 3d 53 52 54 5f 44 69 73 63  eDest)<=SRT_Disc
69950 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 20 53 52  ard)..#define SR
69960 54 5f 4f 75 74 70 75 74 20 20 20 20 20 20 20 35  T_Output       5
69970 20 20 2f 2a 20 4f 75 74 70 75 74 20 65 61 63 68    /* Output each
69980 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 20 2a   row of result *
69990 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 4d 65  /.#define SRT_Me
699a0 6d 20 20 20 20 20 20 20 20 20 20 36 20 20 2f 2a  m          6  /*
699b0 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20 69 6e   Store result in
699c0 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 2a   a memory cell *
699d0 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 53 65  /.#define SRT_Se
699e0 74 20 20 20 20 20 20 20 20 20 20 37 20 20 2f 2a  t          7  /*
699f0 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20 61   Store results a
69a00 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69 6e 64  s keys in an ind
69a10 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52  ex */.#define SR
69a20 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20 20 38  T_Table        8
69a30 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c    /* Store resul
69a40 74 20 61 73 20 64 61 74 61 20 77 69 74 68 20 61  t as data with a
69a50 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f 77 69  n automatic rowi
69a60 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  d */.#define SRT
69a70 5f 45 70 68 65 6d 54 61 62 20 20 20 20 20 39 20  _EphemTab     9 
69a80 20 2f 2a 20 43 72 65 61 74 65 20 74 72 61 6e 73   /* Create trans
69a90 69 65 6e 74 20 74 61 62 20 61 6e 64 20 73 74 6f  ient tab and sto
69aa0 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c  re like SRT_Tabl
69ab0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  e */.#define SRT
69ac0 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 31 30 20  _Coroutine   10 
69ad0 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
69ae0 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72 65 73  ingle row of res
69af0 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20  ult */../*.** A 
69b00 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20 74  structure used t
69b10 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 65 20  o customize the 
69b20 62 65 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69  behavior of sqli
69b30 74 65 33 53 65 6c 65 63 74 28 29 2e 20 53 65 65  te3Select(). See
69b40 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f  .** comments abo
69b50 76 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ve sqlite3Select
69b60 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a  () for details..
69b70 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
69b80 74 20 53 65 6c 65 63 74 44 65 73 74 20 53 65 6c  t SelectDest Sel
69b90 65 63 74 44 65 73 74 3b 0a 73 74 72 75 63 74 20  ectDest;.struct 
69ba0 53 65 6c 65 63 74 44 65 73 74 20 7b 0a 20 20 75  SelectDest {.  u
69bb0 38 20 65 44 65 73 74 3b 20 20 20 20 20 20 20 20  8 eDest;        
69bc0 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f   /* How to dispo
69bd0 73 65 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  se of the result
69be0 73 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e 69  s */.  u8 affini
69bf0 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66 69  ty;      /* Affi
69c00 6e 69 74 79 20 75 73 65 64 20 77 68 65 6e 20 65  nity used when e
69c10 44 65 73 74 3d 3d 53 52 54 5f 53 65 74 20 2a 2f  Dest==SRT_Set */
69c20 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b 20 20 20  .  int iParm;   
69c30 20 20 20 20 20 2f 2a 20 41 20 70 61 72 61 6d 65       /* A parame
69c40 74 65 72 20 75 73 65 64 20 62 79 20 74 68 65 20  ter used by the 
69c50 65 44 65 73 74 20 64 69 73 70 6f 73 61 6c 20 6d  eDest disposal m
69c60 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 69  ethod */.  int i
69c70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Mem;         /* 
69c80 42 61 73 65 20 72 65 67 69 73 74 65 72 20 77 68  Base register wh
69c90 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  ere results are 
69ca0 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74  written */.  int
69cb0 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 2f   nMem;         /
69cc0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67 69  * Number of regi
69cd0 73 74 65 72 73 20 61 6c 6c 6f 63 61 74 65 64 20  sters allocated 
69ce0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 44 75 72  */.};../*.** Dur
69cf0 69 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61 74  ing code generat
69d00 69 6f 6e 20 6f 66 20 73 74 61 74 65 6d 65 6e 74  ion of statement
69d10 73 20 74 68 61 74 20 64 6f 20 69 6e 73 65 72 74  s that do insert
69d20 73 20 69 6e 74 6f 20 41 55 54 4f 49 4e 43 52 45  s into AUTOINCRE
69d30 4d 45 4e 54 20 0a 2a 2a 20 74 61 62 6c 65 73 2c  MENT .** tables,
69d40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
69d50 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 61 74  nformation is at
69d60 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 54 61  tached to the Ta
69d70 62 6c 65 2e 75 2e 61 75 74 6f 49 6e 63 2e 70 0a  ble.u.autoInc.p.
69d80 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 65 61  ** pointer of ea
69d90 63 68 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  ch autoincrement
69da0 20 74 61 62 6c 65 20 74 6f 20 72 65 63 6f 72 64   table to record
69db0 20 73 6f 6d 65 20 73 69 64 65 20 69 6e 66 6f 72   some side infor
69dc0 6d 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 74  mation that.** t
69dd0 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f  he code generato
69de0 72 20 6e 65 65 64 73 2e 20 20 57 65 20 68 61 76  r needs.  We hav
69df0 65 20 74 6f 20 6b 65 65 70 20 70 65 72 2d 74 61  e to keep per-ta
69e00 62 6c 65 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e  ble autoincremen
69e10 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  t.** information
69e20 20 69 6e 20 63 61 73 65 20 69 6e 73 65 72 74 73   in case inserts
69e30 20 61 72 65 20 64 6f 77 6e 20 77 69 74 68 69 6e   are down within
69e40 20 74 72 69 67 67 65 72 73 2e 20 20 54 72 69 67   triggers.  Trig
69e50 67 65 72 73 20 64 6f 20 6e 6f 74 0a 2a 2a 20 6e  gers do not.** n
69e60 6f 72 6d 61 6c 6c 79 20 63 6f 6f 72 64 69 6e 61  ormally coordina
69e70 74 65 20 74 68 65 69 72 20 61 63 74 69 76 69 74  te their activit
69e80 69 65 73 2c 20 62 75 74 20 77 65 20 64 6f 20 6e  ies, but we do n
69e90 65 65 64 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74  eed to coordinat
69ea0 65 20 74 68 65 0a 2a 2a 20 6c 6f 61 64 69 6e 67  e the.** loading
69eb0 20 61 6e 64 20 73 61 76 69 6e 67 20 6f 66 20 61   and saving of a
69ec0 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 69 6e 66  utoincrement inf
69ed0 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72  ormation..*/.str
69ee0 75 63 74 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20  uct AutoincInfo 
69ef0 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20  {.  AutoincInfo 
69f00 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65 78  *pNext;   /* Nex
69f10 74 20 69 6e 66 6f 20 62 6c 6f 63 6b 20 69 6e 20  t info block in 
69f20 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d 20 61  a list of them a
69f30 6c 6c 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  ll */.  Table *p
69f40 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Tab;          /*
69f50 20 54 61 62 6c 65 20 74 68 69 73 20 69 6e 66 6f   Table this info
69f60 20 62 6c 6f 63 6b 20 72 65 66 65 72 73 20 74 6f   block refers to
69f70 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
69f80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
69f90 6e 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33 2e  ndex in sqlite3.
69fa0 61 44 62 5b 5d 20 6f 66 20 64 61 74 61 62 61 73  aDb[] of databas
69fb0 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
69fc0 2f 0a 20 20 69 6e 74 20 72 65 67 43 74 72 3b 20  /.  int regCtr; 
69fd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
69fe0 6f 72 79 20 72 65 67 69 73 74 65 72 20 68 6f 6c  ory register hol
69ff0 64 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 63  ding the rowid c
6a000 6f 75 6e 74 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ounter */.};../*
6a010 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20  .** Size of the 
6a020 63 6f 6c 75 6d 6e 20 63 61 63 68 65 0a 2a 2f 0a  column cache.*/.
6a030 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e  #ifndef SQLITE_N
6a040 5f 43 4f 4c 43 41 43 48 45 0a 23 20 64 65 66 69  _COLCACHE.# defi
6a050 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43  ne SQLITE_N_COLC
6a060 41 43 48 45 20 31 30 0a 23 65 6e 64 69 66 0a 0a  ACHE 10.#endif..
6a070 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 6f  /*.** At least o
6a080 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ne instance of t
6a090 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
6a0a0 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65  ucture is create
6a0b0 64 20 66 6f 72 20 65 61 63 68 20 0a 2a 2a 20 74  d for each .** t
6a0c0 72 69 67 67 65 72 20 74 68 61 74 20 6d 61 79 20  rigger that may 
6a0d0 62 65 20 66 69 72 65 64 20 77 68 69 6c 65 20 70  be fired while p
6a0e0 61 72 73 69 6e 67 20 61 6e 20 49 4e 53 45 52 54  arsing an INSERT
6a0f0 2c 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45  , UPDATE or DELE
6a100 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e  TE.** statement.
6a110 20 41 6c 6c 20 73 75 63 68 20 6f 62 6a 65 63 74   All such object
6a120 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  s are stored in 
6a130 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  the linked list 
6a140 68 65 61 64 65 64 20 61 74 0a 2a 2a 20 50 61 72  headed at.** Par
6a150 73 65 2e 70 54 72 69 67 67 65 72 50 72 67 20 61  se.pTriggerPrg a
6a160 6e 64 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20  nd deleted once 
6a170 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c  statement compil
6a180 61 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a  ation has been.*
6a190 2a 20 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a  * completed..**.
6a1a0 2a 2a 20 41 20 56 64 62 65 20 73 75 62 2d 70 72  ** A Vdbe sub-pr
6a1b0 6f 67 72 61 6d 20 74 68 61 74 20 69 6d 70 6c 65  ogram that imple
6a1c0 6d 65 6e 74 73 20 74 68 65 20 62 6f 64 79 20 61  ments the body a
6a1d0 6e 64 20 57 48 45 4e 20 63 6c 61 75 73 65 20 6f  nd WHEN clause o
6a1e0 66 20 74 72 69 67 67 65 72 0a 2a 2a 20 54 72 69  f trigger.** Tri
6a1f0 67 67 65 72 50 72 67 2e 70 54 72 69 67 67 65 72  ggerPrg.pTrigger
6a200 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 64 65 66  , assuming a def
6a210 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54  ault ON CONFLICT
6a220 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20 54 72   clause of.** Tr
6a230 69 67 67 65 72 50 72 67 2e 6f 72 63 6f 6e 66 2c  iggerPrg.orconf,
6a240 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68   is stored in th
6a250 65 20 54 72 69 67 67 65 72 50 72 67 2e 70 50 72  e TriggerPrg.pPr
6a260 6f 67 72 61 6d 20 76 61 72 69 61 62 6c 65 2e 0a  ogram variable..
6a270 2a 2a 20 54 68 65 20 50 61 72 73 65 2e 70 54 72  ** The Parse.pTr
6a280 69 67 67 65 72 50 72 67 20 6c 69 73 74 20 6e 65  iggerPrg list ne
6a290 76 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 77 6f  ver contains two
6a2a0 20 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68   entries with th
6a2b0 65 20 73 61 6d 65 0a 2a 2a 20 76 61 6c 75 65 73  e same.** values
6a2c0 20 66 6f 72 20 62 6f 74 68 20 70 54 72 69 67 67   for both pTrigg
6a2d0 65 72 20 61 6e 64 20 6f 72 63 6f 6e 66 2e 0a 2a  er and orconf..*
6a2e0 2a 0a 2a 2a 20 54 68 65 20 54 72 69 67 67 65 72  *.** The Trigger
6a2f0 50 72 67 2e 61 43 6f 6c 6d 61 73 6b 5b 30 5d 20  Prg.aColmask[0] 
6a300 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20  variable is set 
6a310 74 6f 20 61 20 6d 61 73 6b 20 6f 66 20 6f 6c 64  to a mask of old
6a320 2e 2a 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 61 63  .* columns.** ac
6a330 63 65 73 73 65 64 20 28 6f 72 20 73 65 74 20 74  cessed (or set t
6a340 6f 20 30 20 66 6f 72 20 74 72 69 67 67 65 72 73  o 0 for triggers
6a350 20 66 69 72 65 64 20 61 73 20 61 20 72 65 73 75   fired as a resu
6a360 6c 74 20 6f 66 20 49 4e 53 45 52 54 20 0a 2a 2a  lt of INSERT .**
6a370 20 73 74 61 74 65 6d 65 6e 74 73 29 2e 20 53 69   statements). Si
6a380 6d 69 6c 61 72 6c 79 2c 20 74 68 65 20 54 72 69  milarly, the Tri
6a390 67 67 65 72 50 72 67 2e 61 43 6f 6c 6d 61 73 6b  ggerPrg.aColmask
6a3a0 5b 31 5d 20 76 61 72 69 61 62 6c 65 20 69 73 20  [1] variable is 
6a3b0 73 65 74 20 74 6f 0a 2a 2a 20 61 20 6d 61 73 6b  set to.** a mask
6a3c0 20 6f 66 20 6e 65 77 2e 2a 20 63 6f 6c 75 6d 6e   of new.* column
6a3d0 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70 72  s used by the pr
6a3e0 6f 67 72 61 6d 2e 0a 2a 2f 0a 73 74 72 75 63 74  ogram..*/.struct
6a3f0 20 54 72 69 67 67 65 72 50 72 67 20 7b 0a 20 20   TriggerPrg {.  
6a400 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
6a410 72 3b 20 20 20 20 20 20 2f 2a 20 54 72 69 67 67  r;      /* Trigg
6a420 65 72 20 74 68 69 73 20 70 72 6f 67 72 61 6d 20  er this program 
6a430 77 61 73 20 63 6f 64 65 64 20 66 72 6f 6d 20 2a  was coded from *
6a440 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20  /.  int orconf; 
6a450 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
6a460 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49  efault ON CONFLI
6a470 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 53  CT policy */.  S
6a480 75 62 50 72 6f 67 72 61 6d 20 2a 70 50 72 6f 67  ubProgram *pProg
6a490 72 61 6d 3b 20 20 20 2f 2a 20 50 72 6f 67 72 61  ram;   /* Progra
6a4a0 6d 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 70  m implementing p
6a4b0 54 72 69 67 67 65 72 2f 6f 72 63 6f 6e 66 20 2a  Trigger/orconf *
6a4c0 2f 0a 20 20 75 33 32 20 61 43 6f 6c 6d 61 73 6b  /.  u32 aColmask
6a4d0 5b 32 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  [2];        /* M
6a4e0 61 73 6b 73 20 6f 66 20 6f 6c 64 2e 2a 2c 20 6e  asks of old.*, n
6a4f0 65 77 2e 2a 20 63 6f 6c 75 6d 6e 73 20 61 63 63  ew.* columns acc
6a500 65 73 73 65 64 20 2a 2f 0a 20 20 54 72 69 67 67  essed */.  Trigg
6a510 65 72 50 72 67 20 2a 70 4e 65 78 74 3b 20 20 20  erPrg *pNext;   
6a520 20 20 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79     /* Next entry
6a530 20 69 6e 20 50 61 72 73 65 2e 70 54 72 69 67 67   in Parse.pTrigg
6a540 65 72 50 72 67 20 6c 69 73 74 20 2a 2f 0a 7d 3b  erPrg list */.};
6a550 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 70  ../*.** An SQL p
6a560 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20  arser context.  
6a570 41 20 63 6f 70 79 20 6f 66 20 74 68 69 73 20 73  A copy of this s
6a580 74 72 75 63 74 75 72 65 20 69 73 20 70 61 73 73  tructure is pass
6a590 65 64 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68  ed through.** th
6a5a0 65 20 70 61 72 73 65 72 20 61 6e 64 20 64 6f 77  e parser and dow
6a5b0 6e 20 69 6e 74 6f 20 61 6c 6c 20 74 68 65 20 70  n into all the p
6a5c0 61 72 73 65 72 20 61 63 74 69 6f 6e 20 72 6f 75  arser action rou
6a5d0 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  tine in order to
6a5e0 0a 2a 2a 20 63 61 72 72 79 20 61 72 6f 75 6e 64  .** carry around
6a5f0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61   information tha
6a600 74 20 69 73 20 67 6c 6f 62 61 6c 20 74 6f 20 74  t is global to t
6a610 68 65 20 65 6e 74 69 72 65 20 70 61 72 73 65 2e  he entire parse.
6a620 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 75 63  .**.** The struc
6a630 74 75 72 65 20 69 73 20 64 69 76 69 64 65 64 20  ture is divided 
6a640 69 6e 74 6f 20 74 77 6f 20 70 61 72 74 73 2e 20  into two parts. 
6a650 20 57 68 65 6e 20 74 68 65 20 70 61 72 73 65 72   When the parser
6a660 20 61 6e 64 20 63 6f 64 65 0a 2a 2a 20 67 65 6e   and code.** gen
6a670 65 72 61 74 65 20 63 61 6c 6c 20 74 68 65 6d 73  erate call thems
6a680 65 6c 76 65 73 20 72 65 63 75 72 73 69 76 65 6c  elves recursivel
6a690 79 2c 20 74 68 65 20 66 69 72 73 74 20 70 61 72  y, the first par
6a6a0 74 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 75  t of the structu
6a6b0 72 65 0a 2a 2a 20 69 73 20 63 6f 6e 73 74 61 6e  re.** is constan
6a6c0 74 20 62 75 74 20 74 68 65 20 73 65 63 6f 6e 64  t but the second
6a6d0 20 70 61 72 74 20 69 73 20 72 65 73 65 74 20 61   part is reset a
6a6e0 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  t the beginning 
6a6f0 61 6e 64 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 61  and end of.** ea
6a700 63 68 20 72 65 63 75 72 73 69 6f 6e 2e 0a 2a 2a  ch recursion..**
6a710 0a 2a 2a 20 54 68 65 20 6e 54 61 62 6c 65 4c 6f  .** The nTableLo
6a720 63 6b 20 61 6e 64 20 61 54 61 62 6c 65 4c 6f 63  ck and aTableLoc
6a730 6b 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20  k variables are 
6a740 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68 65  only used if the
6a750 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 0a 2a   shared-cache .*
6a760 2a 20 66 65 61 74 75 72 65 20 69 73 20 65 6e 61  * feature is ena
6a770 62 6c 65 64 20 28 69 66 20 73 71 6c 69 74 65 33  bled (if sqlite3
6a780 54 73 64 28 29 2d 3e 75 73 65 53 68 61 72 65 64  Tsd()->useShared
6a790 44 61 74 61 20 69 73 20 74 72 75 65 29 2e 20 54  Data is true). T
6a7a0 68 65 79 20 61 72 65 0a 2a 2a 20 75 73 65 64 20  hey are.** used 
6a7b0 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73 65 74  to store the set
6a7c0 20 6f 66 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20   of table-locks 
6a7d0 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20  required by the 
6a7e0 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 0a  statement being.
6a7f0 2a 2a 20 63 6f 6d 70 69 6c 65 64 2e 20 46 75 6e  ** compiled. Fun
6a800 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 54 61 62  ction sqlite3Tab
6a810 6c 65 4c 6f 63 6b 28 29 20 69 73 20 75 73 65 64  leLock() is used
6a820 20 74 6f 20 61 64 64 20 65 6e 74 72 69 65 73 20   to add entries 
6a830 74 6f 20 74 68 65 0a 2a 2a 20 6c 69 73 74 2e 0a  to the.** list..
6a840 2a 2f 0a 73 74 72 75 63 74 20 50 61 72 73 65 20  */.struct Parse 
6a850 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
6a860 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
6a870 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74  main database st
6a880 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74  ructure */.  int
6a890 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
6a8a0 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
6a8b0 20 66 72 6f 6d 20 65 78 65 63 75 74 69 6f 6e 20   from execution 
6a8c0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d  */.  char *zErrM
6a8d0 73 67 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 20  sg;       /* An 
6a8e0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f  error message */
6a8f0 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20  .  Vdbe *pVdbe; 
6a900 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 6e          /* An en
6a910 67 69 6e 65 20 66 6f 72 20 65 78 65 63 75 74 69  gine for executi
6a920 6e 67 20 64 61 74 61 62 61 73 65 20 62 79 74 65  ng database byte
6a930 63 6f 64 65 20 2a 2f 0a 20 20 75 38 20 63 6f 6c  code */.  u8 col
6a940 4e 61 6d 65 73 53 65 74 3b 20 20 20 20 20 20 2f  NamesSet;      /
6a950 2a 20 54 52 55 45 20 61 66 74 65 72 20 4f 50 5f  * TRUE after OP_
6a960 43 6f 6c 75 6d 6e 4e 61 6d 65 20 68 61 73 20 62  ColumnName has b
6a970 65 65 6e 20 69 73 73 75 65 64 20 74 6f 20 70 56  een issued to pV
6a980 64 62 65 20 2a 2f 0a 20 20 75 38 20 6e 61 6d 65  dbe */.  u8 name
6a990 43 6c 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a  Clash;        /*
6a9a0 20 41 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62   A permanent tab
6a9b0 6c 65 20 6e 61 6d 65 20 63 6c 61 73 68 65 73 20  le name clashes 
6a9c0 77 69 74 68 20 74 65 6d 70 20 74 61 62 6c 65 20  with temp table 
6a9d0 6e 61 6d 65 20 2a 2f 0a 20 20 75 38 20 63 68 65  name */.  u8 che
6a9e0 63 6b 53 63 68 65 6d 61 3b 20 20 20 20 20 20 2f  ckSchema;      /
6a9f0 2a 20 43 61 75 73 65 73 20 73 63 68 65 6d 61 20  * Causes schema 
6aa00 63 6f 6f 6b 69 65 20 63 68 65 63 6b 20 61 66 74  cookie check aft
6aa10 65 72 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20  er an error */. 
6aa20 20 75 38 20 6e 65 73 74 65 64 3b 20 20 20 20 20   u8 nested;     
6aa30 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6aa40 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20  of nested calls 
6aa50 74 6f 20 74 68 65 20 70 61 72 73 65 72 2f 63 6f  to the parser/co
6aa60 64 65 20 67 65 6e 65 72 61 74 6f 72 20 2a 2f 0a  de generator */.
6aa70 20 20 75 38 20 70 61 72 73 65 45 72 72 6f 72 3b    u8 parseError;
6aa80 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61         /* True a
6aa90 66 74 65 72 20 61 20 70 61 72 73 69 6e 67 20 65  fter a parsing e
6aaa0 72 72 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 31  rror.  Ticket #1
6aab0 37 39 34 20 2a 2f 0a 20 20 75 38 20 6e 54 65 6d  794 */.  u8 nTem
6aac0 70 52 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  pReg;         /*
6aad0 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 6d 70 6f   Number of tempo
6aae0 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 69  rary registers i
6aaf0 6e 20 61 54 65 6d 70 52 65 67 5b 5d 20 2a 2f 0a  n aTempReg[] */.
6ab00 20 20 75 38 20 6e 54 65 6d 70 49 6e 55 73 65 3b    u8 nTempInUse;
6ab10 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6ab20 20 6f 66 20 61 54 65 6d 70 52 65 67 5b 5d 20 63   of aTempReg[] c
6ab30 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64  urrently checked
6ab40 20 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 61 54   out */.  int aT
6ab50 65 6d 70 52 65 67 5b 38 5d 3b 20 20 20 20 20 2f  empReg[8];     /
6ab60 2a 20 48 6f 6c 64 69 6e 67 20 61 72 65 61 20 66  * Holding area f
6ab70 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  or temporary reg
6ab80 69 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20  isters */.  int 
6ab90 6e 52 61 6e 67 65 52 65 67 3b 20 20 20 20 20 20  nRangeReg;      
6aba0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
6abb0 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
6abc0 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  er block */.  in
6abd0 74 20 69 52 61 6e 67 65 52 65 67 3b 20 20 20 20  t iRangeReg;    
6abe0 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69     /* First regi
6abf0 73 74 65 72 20 69 6e 20 74 65 6d 70 6f 72 61 72  ster in temporar
6ac00 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b  y register block
6ac10 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20   */.  int nErr; 
6ac20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
6ac30 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73  mber of errors s
6ac40 65 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61  een */.  int nTa
6ac50 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b;            /*
6ac60 20 4e 75 6d 62 65 72 20 6f 66 20 70 72 65 76 69   Number of previ
6ac70 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
6ac80 56 44 42 45 20 63 75 72 73 6f 72 73 20 2a 2f 0a  VDBE cursors */.
6ac90 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20    int nMem;     
6aca0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6acb0 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73   of memory cells
6acc0 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a   used so far */.
6acd0 20 20 69 6e 74 20 6e 53 65 74 3b 20 20 20 20 20    int nSet;     
6ace0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6acf0 20 6f 66 20 73 65 74 73 20 75 73 65 64 20 73 6f   of sets used so
6ad00 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 6b   far */.  int ck
6ad10 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 2f  Base;          /
6ad20 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72 20  * Base register 
6ad30 6f 66 20 64 61 74 61 20 64 75 72 69 6e 67 20 63  of data during c
6ad40 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73  heck constraints
6ad50 20 2a 2f 0a 20 20 69 6e 74 20 69 43 61 63 68 65   */.  int iCache
6ad60 4c 65 76 65 6c 3b 20 20 20 20 20 2f 2a 20 43 6f  Level;     /* Co
6ad70 6c 43 61 63 68 65 20 76 61 6c 69 64 20 77 68 65  lCache valid whe
6ad80 6e 20 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 69 4c  n aColCache[].iL
6ad90 65 76 65 6c 3c 3d 69 43 61 63 68 65 4c 65 76 65  evel<=iCacheLeve
6ada0 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 43 61 63 68  l */.  int iCach
6adb0 65 43 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 43  eCnt;       /* C
6adc0 6f 75 6e 74 65 72 20 75 73 65 64 20 74 6f 20 67  ounter used to g
6add0 65 6e 65 72 61 74 65 20 61 43 6f 6c 43 61 63 68  enerate aColCach
6ade0 65 5b 5d 2e 6c 72 75 20 76 61 6c 75 65 73 20 2a  e[].lru values *
6adf0 2f 0a 20 20 75 38 20 6e 43 6f 6c 43 61 63 68 65  /.  u8 nColCache
6ae00 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
6ae10 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  er of entries in
6ae20 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68   the column cach
6ae30 65 20 2a 2f 0a 20 20 75 38 20 69 43 6f 6c 43 61  e */.  u8 iColCa
6ae40 63 68 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  che;        /* N
6ae50 65 78 74 20 65 6e 74 72 79 20 6f 66 20 74 68 65  ext entry of the
6ae60 20 63 61 63 68 65 20 74 6f 20 72 65 70 6c 61 63   cache to replac
6ae70 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 43  e */.  struct yC
6ae80 6f 6c 43 61 63 68 65 20 7b 0a 20 20 20 20 69 6e  olCache {.    in
6ae90 74 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20  t iTable;       
6aea0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72      /* Table cur
6aeb0 73 6f 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  sor number */.  
6aec0 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
6aed0 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
6aee0 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a   column number *
6aef0 2f 0a 20 20 20 20 75 38 20 61 66 66 43 68 61 6e  /.    u8 affChan
6af00 67 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ge;         /* T
6af10 72 75 65 20 69 66 20 74 68 69 73 20 72 65 67 69  rue if this regi
6af20 73 74 65 72 20 68 61 73 20 68 61 64 20 61 6e 20  ster has had an 
6af30 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20  affinity change 
6af40 2a 2f 0a 20 20 20 20 75 38 20 74 65 6d 70 52 65  */.    u8 tempRe
6af50 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g;           /* 
6af60 69 52 65 67 20 69 73 20 61 20 74 65 6d 70 20 72  iReg is a temp r
6af70 65 67 69 73 74 65 72 20 74 68 61 74 20 6e 65 65  egister that nee
6af80 64 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a  ds to be freed *
6af90 2f 0a 20 20 20 20 69 6e 74 20 69 4c 65 76 65 6c  /.    int iLevel
6afa0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
6afb0 65 73 74 69 6e 67 20 6c 65 76 65 6c 20 2a 2f 0a  esting level */.
6afc0 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20      int iReg;   
6afd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
6afe0 20 77 69 74 68 20 76 61 6c 75 65 20 6f 66 20 74   with value of t
6aff0 68 69 73 20 63 6f 6c 75 6d 6e 2e 20 30 20 6d 65  his column. 0 me
6b000 61 6e 73 20 6e 6f 6e 65 2e 20 2a 2f 0a 20 20 20  ans none. */.   
6b010 20 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20 20   int lru;       
6b020 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 73 74 20         /* Least 
6b030 72 65 63 65 6e 74 6c 79 20 75 73 65 64 20 65 6e  recently used en
6b040 74 72 79 20 68 61 73 20 74 68 65 20 73 6d 61 6c  try has the smal
6b050 6c 65 73 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20  lest value */.  
6b060 7d 20 61 43 6f 6c 43 61 63 68 65 5b 53 51 4c 49  } aColCache[SQLI
6b070 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 5d 3b 20  TE_N_COLCACHE]; 
6b080 20 2f 2a 20 4f 6e 65 20 66 6f 72 20 65 61 63 68   /* One for each
6b090 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e   column cache en
6b0a0 74 72 79 20 2a 2f 0a 20 20 75 33 32 20 77 72 69  try */.  u32 wri
6b0b0 74 65 4d 61 73 6b 3b 20 20 20 20 20 20 20 2f 2a  teMask;       /*
6b0c0 20 53 74 61 72 74 20 61 20 77 72 69 74 65 20 74   Start a write t
6b0d0 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
6b0e0 65 73 65 20 64 61 74 61 62 61 73 65 73 20 2a 2f  ese databases */
6b0f0 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65 4d 61 73  .  u32 cookieMas
6b100 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61  k;      /* Bitma
6b110 73 6b 20 6f 66 20 73 63 68 65 6d 61 20 76 65 72  sk of schema ver
6b120 69 66 69 65 64 20 64 61 74 61 62 61 73 65 73 20  ified databases 
6b130 2a 2f 0a 20 20 75 38 20 69 73 4d 75 6c 74 69 57  */.  u8 isMultiW
6b140 72 69 74 65 3b 20 20 20 20 20 2f 2a 20 54 72 75  rite;     /* Tru
6b150 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 6d  e if statement m
6b160 61 79 20 61 66 66 65 63 74 2f 69 6e 73 65 72 74  ay affect/insert
6b170 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 2a   multiple rows *
6b180 2f 0a 20 20 75 38 20 6d 61 79 41 62 6f 72 74 3b  /.  u8 mayAbort;
6b190 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
6b1a0 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 6d 61   if statement ma
6b1b0 79 20 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54  y throw an ABORT
6b1c0 20 65 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20   exception */.  
6b1d0 69 6e 74 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b 20  int cookieGoto; 
6b1e0 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
6b1f0 6f 66 20 4f 50 5f 47 6f 74 6f 20 74 6f 20 63 6f  of OP_Goto to co
6b200 6f 6b 69 65 20 76 65 72 69 66 69 65 72 20 73 75  okie verifier su
6b210 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  broutine */.  in
6b220 74 20 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 53 51  t cookieValue[SQ
6b230 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45  LITE_MAX_ATTACHE
6b240 44 2b 32 5d 3b 20 20 2f 2a 20 56 61 6c 75 65 73  D+2];  /* Values
6b250 20 6f 66 20 63 6f 6f 6b 69 65 73 20 74 6f 20 76   of cookies to v
6b260 65 72 69 66 79 20 2a 2f 0a 23 69 66 6e 64 65 66  erify */.#ifndef
6b270 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
6b280 52 45 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20  RED_CACHE.  int 
6b290 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20 20 20 20 20  nTableLock;     
6b2a0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
6b2b0 6c 6f 63 6b 73 20 69 6e 20 61 54 61 62 6c 65 4c  locks in aTableL
6b2c0 6f 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 4c 6f  ock */.  TableLo
6b2d0 63 6b 20 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b 20  ck *aTableLock; 
6b2e0 2f 2a 20 52 65 71 75 69 72 65 64 20 74 61 62 6c  /* Required tabl
6b2f0 65 20 6c 6f 63 6b 73 20 66 6f 72 20 73 68 61 72  e locks for shar
6b300 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f  ed-cache mode */
6b310 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 72 65  .#endif.  int re
6b320 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 2f  gRowid;        /
6b330 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
6b340 6e 67 20 72 6f 77 69 64 20 6f 66 20 43 52 45 41  ng rowid of CREA
6b350 54 45 20 54 41 42 4c 45 20 65 6e 74 72 79 20 2a  TE TABLE entry *
6b360 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 6f 74 3b  /.  int regRoot;
6b370 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
6b380 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 6f  ster holding roo
6b390 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  t page number fo
6b3a0 72 20 6e 65 77 20 6f 62 6a 65 63 74 73 20 2a 2f  r new objects */
6b3b0 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a  .  AutoincInfo *
6b3c0 70 41 69 6e 63 3b 20 20 2f 2a 20 49 6e 66 6f 72  pAinc;  /* Infor
6b3d0 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 41 55 54  mation about AUT
6b3e0 4f 49 4e 43 52 45 4d 45 4e 54 20 63 6f 75 6e 74  OINCREMENT count
6b3f0 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61  ers */.  int nMa
6b400 78 41 72 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  xArg;         /*
6b410 20 4d 61 78 20 61 72 67 73 20 70 61 73 73 65 64   Max args passed
6b420 20 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f   to user functio
6b430 6e 20 62 79 20 73 75 62 2d 70 72 6f 67 72 61 6d  n by sub-program
6b440 20 2a 2f 0a 0a 20 20 2f 2a 20 49 6e 66 6f 72 6d   */..  /* Inform
6b450 61 74 69 6f 6e 20 75 73 65 64 20 77 68 69 6c 65  ation used while
6b460 20 63 6f 64 69 6e 67 20 74 72 69 67 67 65 72 20   coding trigger 
6b470 70 72 6f 67 72 61 6d 73 2e 20 2a 2f 0a 20 20 50  programs. */.  P
6b480 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 3b  arse *pToplevel;
6b490 20 20 20 20 2f 2a 20 50 61 72 73 65 20 73 74 72      /* Parse str
6b4a0 75 63 74 75 72 65 20 66 6f 72 20 6d 61 69 6e 20  ucture for main 
6b4b0 70 72 6f 67 72 61 6d 20 28 6f 72 20 4e 55 4c 4c  program (or NULL
6b4c0 29 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  ) */.  Table *pT
6b4d0 72 69 67 67 65 72 54 61 62 3b 20 20 2f 2a 20 54  riggerTab;  /* T
6b4e0 61 62 6c 65 20 74 72 69 67 67 65 72 73 20 61 72  able triggers ar
6b4f0 65 20 62 65 69 6e 67 20 63 6f 64 65 64 20 66 6f  e being coded fo
6b500 72 20 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61  r */.  u32 oldma
6b510 73 6b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  sk;         /* M
6b520 61 73 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c  ask of old.* col
6b530 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 65 64 20  umns referenced 
6b540 2a 2f 0a 20 20 75 33 32 20 6e 65 77 6d 61 73 6b  */.  u32 newmask
6b550 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73  ;         /* Mas
6b560 6b 20 6f 66 20 6e 65 77 2e 2a 20 63 6f 6c 75 6d  k of new.* colum
6b570 6e 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f  ns referenced */
6b580 0a 20 20 75 38 20 65 54 72 69 67 67 65 72 4f 70  .  u8 eTriggerOp
6b590 3b 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 55 50  ;       /* TK_UP
6b5a0 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 20  DATE, TK_INSERT 
6b5b0 6f 72 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a  or TK_DELETE */.
6b5c0 20 20 75 38 20 65 4f 72 63 6f 6e 66 3b 20 20 20    u8 eOrconf;   
6b5d0 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c         /* Defaul
6b5e0 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  t ON CONFLICT po
6b5f0 6c 69 63 79 20 66 6f 72 20 74 72 69 67 67 65 72  licy for trigger
6b600 20 73 74 65 70 73 20 2a 2f 0a 20 20 75 38 20 64   steps */.  u8 d
6b610 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 3b 20  isableTriggers; 
6b620 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69 73 61   /* True to disa
6b630 62 6c 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a  ble triggers */.
6b640 0a 20 20 2f 2a 20 41 62 6f 76 65 20 69 73 20 63  .  /* Above is c
6b650 6f 6e 73 74 61 6e 74 20 62 65 74 77 65 65 6e 20  onstant between 
6b660 72 65 63 75 72 73 69 6f 6e 73 2e 20 20 42 65 6c  recursions.  Bel
6b670 6f 77 20 69 73 20 72 65 73 65 74 20 62 65 66 6f  ow is reset befo
6b680 72 65 20 61 6e 64 20 61 66 74 65 72 0a 20 20 2a  re and after.  *
6b690 2a 20 65 61 63 68 20 72 65 63 75 72 73 69 6f 6e  * each recursion
6b6a0 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 56 61 72 3b   */..  int nVar;
6b6b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
6b6c0 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 76 61 72  umber of '?' var
6b6d0 69 61 62 6c 65 73 20 73 65 65 6e 20 69 6e 20 74  iables seen in t
6b6e0 68 65 20 53 51 4c 20 73 6f 20 66 61 72 20 2a 2f  he SQL so far */
6b6f0 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 70 72 3b  .  int nVarExpr;
6b700 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6b710 72 20 6f 66 20 75 73 65 64 20 73 6c 6f 74 73 20  r of used slots 
6b720 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a  in apVarExpr[] *
6b730 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 70 72  /.  int nVarExpr
6b740 41 6c 6c 6f 63 3b 20 20 20 2f 2a 20 4e 75 6d 62  Alloc;   /* Numb
6b750 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20  er of allocated 
6b760 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 72 45 78  slots in apVarEx
6b770 70 72 5b 5d 20 2a 2f 0a 20 20 45 78 70 72 20 2a  pr[] */.  Expr *
6b780 2a 61 70 56 61 72 45 78 70 72 3b 20 20 20 20 2f  *apVarExpr;    /
6b790 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 3a 61  * Pointers to :a
6b7a0 61 61 20 61 6e 64 20 24 61 61 61 61 20 77 69 6c  aa and $aaaa wil
6b7b0 64 63 61 72 64 20 65 78 70 72 65 73 73 69 6f 6e  dcard expression
6b7c0 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 52 65  s */.  Vdbe *pRe
6b7d0 70 72 65 70 61 72 65 3b 20 20 20 20 2f 2a 20 56  prepare;    /* V
6b7e0 4d 20 62 65 69 6e 67 20 72 65 70 72 65 70 61 72  M being reprepar
6b7f0 65 64 20 28 73 71 6c 69 74 65 33 52 65 70 72 65  ed (sqlite3Repre
6b800 70 61 72 65 28 29 29 20 2a 2f 0a 20 20 69 6e 74  pare()) */.  int
6b810 20 6e 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20   nAlias;        
6b820 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
6b830 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 73 65  liased result se
6b840 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69  t columns */.  i
6b850 6e 74 20 6e 41 6c 69 61 73 41 6c 6c 6f 63 3b 20  nt nAliasAlloc; 
6b860 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6b870 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73   allocated slots
6b880 20 66 6f 72 20 61 41 6c 69 61 73 5b 5d 20 2a 2f   for aAlias[] */
6b890 0a 20 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20  .  int *aAlias; 
6b8a0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
6b8b0 74 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  ter used to hold
6b8c0 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20   aliased result 
6b8d0 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b  */.  u8 explain;
6b8e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
6b8f0 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e  e if the EXPLAIN
6b900 20 66 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f   flag is found o
6b910 6e 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20  n the query */. 
6b920 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65   Token sNameToke
6b930 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77  n;    /* Token w
6b940 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65 64 20  ith unqualified 
6b950 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61  schema object na
6b960 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c  me */.  Token sL
6b970 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20  astToken;    /* 
6b980 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70  The last token p
6b990 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  arsed */.  const
6b9a0 20 63 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20   char *zTail;   
6b9b0 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20  /* All SQL text 
6b9c0 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 65  past the last se
6b9d0 6d 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a  micolon parsed *
6b9e0 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54  /.  Table *pNewT
6b9f0 61 62 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 61  able;    /* A ta
6ba00 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72  ble being constr
6ba10 75 63 74 65 64 20 62 79 20 43 52 45 41 54 45 20  ucted by CREATE 
6ba20 54 41 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67  TABLE */.  Trigg
6ba30 65 72 20 2a 70 4e 65 77 54 72 69 67 67 65 72 3b  er *pNewTrigger;
6ba40 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20       /* Trigger 
6ba50 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 20  under construct 
6ba60 62 79 20 61 20 43 52 45 41 54 45 20 54 52 49 47  by a CREATE TRIG
6ba70 47 45 52 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  GER */.  const c
6ba80 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78  har *zAuthContex
6ba90 74 3b 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61  t; /* The 6th pa
6baa0 72 61 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78  rameter to db->x
6bab0 41 75 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a  Auth callbacks *
6bac0 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
6bad0 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
6bae0 4c 45 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b  LE.  Token sArg;
6baf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6bb00 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74  /* Complete text
6bb10 20 6f 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67   of a module arg
6bb20 75 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65  ument */.  u8 de
6bb30 63 6c 61 72 65 56 74 61 62 3b 20 20 20 20 20 20  clareVtab;      
6bb40 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
6bb50 20 69 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f   inside sqlite3_
6bb60 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a  declare_vtab() *
6bb70 2f 0a 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63  /.  int nVtabLoc
6bb80 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k;             /
6bb90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74  * Number of virt
6bba0 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f  ual tables to lo
6bbb0 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a  ck */.  Table **
6bbc0 61 70 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20  apVtabLock;     
6bbd0 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
6bbe0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
6bbf0 6e 65 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20  needing locking 
6bc00 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20  */.#endif.  int 
6bc10 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  nHeight;        
6bc20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
6bc30 6e 20 74 72 65 65 20 68 65 69 67 68 74 20 6f 66  n tree height of
6bc40 20 63 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c   current sub-sel
6bc50 65 63 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ect */.  Table *
6bc60 70 5a 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20  pZombieTab;     
6bc70 20 2f 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c   /* List of Tabl
6bc80 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c  e objects to del
6bc90 65 74 65 20 61 66 74 65 72 20 63 6f 64 65 20 67  ete after code g
6bca0 65 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50  en */.  TriggerP
6bcb0 72 67 20 2a 70 54 72 69 67 67 65 72 50 72 67 3b  rg *pTriggerPrg;
6bcc0 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69      /* Linked li
6bcd0 73 74 20 6f 66 20 63 6f 64 65 64 20 74 72 69 67  st of coded trig
6bce0 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64  gers */.};..#ifd
6bcf0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
6bd00 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 23 64  IRTUALTABLE.  #d
6bd10 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45  efine IN_DECLARE
6bd20 5f 56 54 41 42 20 30 0a 23 65 6c 73 65 0a 20 20  _VTAB 0.#else.  
6bd30 23 64 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41  #define IN_DECLA
6bd40 52 45 5f 56 54 41 42 20 28 70 50 61 72 73 65 2d  RE_VTAB (pParse-
6bd50 3e 64 65 63 6c 61 72 65 56 74 61 62 29 0a 23 65  >declareVtab).#e
6bd60 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  ndif../*.** An i
6bd70 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
6bd80 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
6bd90 72 65 20 63 61 6e 20 62 65 20 64 65 63 6c 61 72  re can be declar
6bda0 65 64 20 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e  ed on a stack an
6bdb0 64 20 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76  d used.** to sav
6bdc0 65 20 74 68 65 20 50 61 72 73 65 2e 7a 41 75 74  e the Parse.zAut
6bdd0 68 43 6f 6e 74 65 78 74 20 76 61 6c 75 65 20 73  hContext value s
6bde0 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65  o that it can be
6bdf0 20 72 65 73 74 6f 72 65 64 20 6c 61 74 65 72 2e   restored later.
6be00 0a 2a 2f 0a 73 74 72 75 63 74 20 41 75 74 68 43  .*/.struct AuthC
6be10 6f 6e 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74  ontext {.  const
6be20 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74   char *zAuthCont
6be30 65 78 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 61  ext;   /* Put sa
6be40 76 65 64 20 50 61 72 73 65 2e 7a 41 75 74 68 43  ved Parse.zAuthC
6be50 6f 6e 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 20  ontext here */. 
6be60 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
6be70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6be80 54 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74  The Parse struct
6be90 75 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ure */.};../*.**
6bea0 20 42 69 74 66 69 65 6c 64 20 66 6c 61 67 73 20   Bitfield flags 
6beb0 66 6f 72 20 50 35 20 76 61 6c 75 65 20 69 6e 20  for P5 value in 
6bec0 4f 50 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f 50  OP_Insert and OP
6bed0 5f 44 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69  _Delete.*/.#defi
6bee0 6e 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47  ne OPFLAG_NCHANG
6bef0 45 20 20 20 20 20 20 20 30 78 30 31 20 20 20 20  E       0x01    
6bf00 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65  /* Set to update
6bf10 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a   db->nChange */.
6bf20 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c  #define OPFLAG_L
6bf30 41 53 54 52 4f 57 49 44 20 20 20 20 20 30 78 30  ASTROWID     0x0
6bf40 32 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75  2    /* Set to u
6bf50 70 64 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f  pdate db->lastRo
6bf60 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  wid */.#define O
6bf70 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20  PFLAG_ISUPDATE  
6bf80 20 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54      0x04    /* T
6bf90 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73  his OP_Insert is
6bfa0 20 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a   an sql UPDATE *
6bfb0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47  /.#define OPFLAG
6bfc0 5f 41 50 50 45 4e 44 20 20 20 20 20 20 20 20 30  _APPEND        0
6bfd0 78 30 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69  x08    /* This i
6bfe0 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61  s likely to be a
6bff0 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66  n append */.#def
6c000 69 6e 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45  ine OPFLAG_USESE
6c010 45 4b 52 45 53 55 4c 54 20 30 78 31 30 20 20 20  EKRESULT 0x10   
6c020 20 2f 2a 20 54 72 79 20 74 6f 20 61 76 6f 69 64   /* Try to avoid
6c030 20 61 20 73 65 65 6b 20 69 6e 20 42 74 72 65 65   a seek in Btree
6c040 49 6e 73 65 72 74 28 29 20 2a 2f 0a 23 64 65 66  Insert() */.#def
6c050 69 6e 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52  ine OPFLAG_CLEAR
6c060 43 41 43 48 45 20 20 20 20 30 78 32 30 20 20 20  CACHE    0x20   
6c070 20 2f 2a 20 43 6c 65 61 72 20 70 73 65 75 64 6f   /* Clear pseudo
6c080 2d 74 61 62 6c 65 20 63 61 63 68 65 20 69 6e 20  -table cache in 
6c090 4f 50 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a  OP_Column */../*
6c0a0 0a 20 2a 20 45 61 63 68 20 74 72 69 67 67 65 72  . * Each trigger
6c0b0 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20   present in the 
6c0c0 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
6c0d0 69 73 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20  is stored as an 
6c0e0 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73  instance of. * s
6c0f0 74 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 0a  truct Trigger. .
6c100 20 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20 74   *. * Pointers t
6c110 6f 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 73  o instances of s
6c120 74 72 75 63 74 20 54 72 69 67 67 65 72 20 61 72  truct Trigger ar
6c130 65 20 73 74 6f 72 65 64 20 69 6e 20 74 77 6f 20  e stored in two 
6c140 77 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74  ways.. * 1. In t
6c150 68 65 20 22 74 72 69 67 48 61 73 68 22 20 68 61  he "trigHash" ha
6c160 73 68 20 74 61 62 6c 65 20 28 70 61 72 74 20 6f  sh table (part o
6c170 66 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 74  f the sqlite3* t
6c180 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74  hat represents t
6c190 68 65 20 0a 20 2a 20 20 20 20 64 61 74 61 62 61  he . *    databa
6c1a0 73 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73  se). This allows
6c1b0 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75   Trigger structu
6c1c0 72 65 73 20 74 6f 20 62 65 20 72 65 74 72 69 65  res to be retrie
6c1d0 76 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20  ved by name.. * 
6c1e0 32 2e 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20  2. All triggers 
6c1f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
6c200 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 66  a single table f
6c210 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  orm a linked lis
6c220 74 2c 20 75 73 69 6e 67 20 74 68 65 0a 20 2a 20  t, using the. * 
6c230 20 20 20 70 4e 65 78 74 20 6d 65 6d 62 65 72 20     pNext member 
6c240 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  of struct Trigge
6c250 72 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  r. A pointer to 
6c260 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e  the first elemen
6c270 74 20 6f 66 20 74 68 65 0a 20 2a 20 20 20 20 6c  t of the. *    l
6c280 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 73 74  inked list is st
6c290 6f 72 65 64 20 61 73 20 74 68 65 20 22 70 54 72  ored as the "pTr
6c2a0 69 67 67 65 72 22 20 6d 65 6d 62 65 72 20 6f 66  igger" member of
6c2b0 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a   the associated.
6c2c0 20 2a 20 20 20 20 73 74 72 75 63 74 20 54 61 62   *    struct Tab
6c2d0 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73  le.. *. * The "s
6c2e0 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72  tep_list" member
6c2f0 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66   points to the f
6c300 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
6c310 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a  a linked list. *
6c320 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
6c330 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 73  SQL statements s
6c340 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20  pecified as the 
6c350 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e  trigger program.
6c360 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67  . */.struct Trig
6c370 67 65 72 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  ger {.  char *zN
6c380 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
6c390 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  /* The name of t
6c3a0 68 65 20 74 72 69 67 67 65 72 20 20 20 20 20 20  he trigger      
6c3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c3c0 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61 62    */.  char *tab
6c3d0 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  le;            /
6c3e0 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76  * The table or v
6c3f0 69 65 77 20 74 6f 20 77 68 69 63 68 20 74 68 65  iew to which the
6c400 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65 73   trigger applies
6c410 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20   */.  u8 op;    
6c420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6c430 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54   One of TK_DELET
6c440 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b  E, TK_UPDATE, TK
6c450 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20  _INSERT         
6c460 2a 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20  */.  u8 tr_tm;  
6c470 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6c480 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42  One of TRIGGER_B
6c490 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41  EFORE, TRIGGER_A
6c4a0 46 54 45 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a  FTER */.  Expr *
6c4b0 70 57 68 65 6e 3b 20 20 20 20 20 20 20 20 20 20  pWhen;          
6c4c0 20 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 63 6c    /* The WHEN cl
6c4d0 61 75 73 65 20 6f 66 20 74 68 65 20 65 78 70 72  ause of the expr
6c4e0 65 73 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e  ession (may be N
6c4f0 55 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74  ULL) */.  IdList
6c500 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20   *pColumns;     
6c510 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
6c520 61 6e 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f  an UPDATE OF <co
6c530 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67  lumn-list> trigg
6c540 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
6c550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c560 20 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73   the <column-lis
6c570 74 3e 20 69 73 20 73 74 6f 72 65 64 20 68 65 72  t> is stored her
6c580 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70  e */.  Schema *p
6c590 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f  Schema;        /
6c5a0 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e  * Schema contain
6c5b0 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20  ing the trigger 
6c5c0 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61  */.  Schema *pTa
6c5d0 62 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20  bSchema;     /* 
6c5e0 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e  Schema containin
6c5f0 67 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  g the table */. 
6c600 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 74   TriggerStep *st
6c610 65 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b  ep_list; /* Link
6c620 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72   list of trigger
6c630 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20   program steps  
6c640 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
6c650 54 72 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20  Trigger *pNext; 
6c660 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
6c670 74 72 69 67 67 65 72 20 61 73 73 6f 63 69 61 74  trigger associat
6c680 65 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c  ed with the tabl
6c690 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  e */.};../*.** A
6c6a0 20 74 72 69 67 67 65 72 20 69 73 20 65 69 74 68   trigger is eith
6c6b0 65 72 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61  er a BEFORE or a
6c6c0 6e 20 41 46 54 45 52 20 74 72 69 67 67 65 72 2e  n AFTER trigger.
6c6d0 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
6c6e0 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74  constants.** det
6c6f0 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a  ermine which. .*
6c700 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72  *.** If there ar
6c710 65 20 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67  e multiple trigg
6c720 65 72 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f  ers, you might o
6c730 66 20 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e  f some BEFORE an
6c740 64 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a  d some AFTER..**
6c750 20 49 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20   In that cases, 
6c760 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65  the constants be
6c770 6c 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20  low can be ORed 
6c780 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65  together..*/.#de
6c790 66 69 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46  fine TRIGGER_BEF
6c7a0 4f 52 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54  ORE  1.#define T
6c7b0 52 49 47 47 45 52 5f 41 46 54 45 52 20 20 20 32  RIGGER_AFTER   2
6c7c0 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61  ../*. * An insta
6c7d0 6e 63 65 20 6f 66 20 73 74 72 75 63 74 20 54 72  nce of struct Tr
6c7e0 69 67 67 65 72 53 74 65 70 20 69 73 20 75 73 65  iggerStep is use
6c7f0 64 20 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e  d to store a sin
6c800 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  gle SQL statemen
6c810 74 0a 20 2a 20 74 68 61 74 20 69 73 20 61 20 70  t. * that is a p
6c820 61 72 74 20 6f 66 20 61 20 74 72 69 67 67 65 72  art of a trigger
6c830 2d 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a  -program. . *. *
6c840 20 49 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74   Instances of st
6c850 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70  ruct TriggerStep
6c860 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61   are stored in a
6c870 20 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c   singly linked l
6c880 69 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75  ist (linked. * u
6c890 73 69 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22  sing the "pNext"
6c8a0 20 6d 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e   member) referen
6c8b0 63 65 64 20 62 79 20 74 68 65 20 22 73 74 65 70  ced by the "step
6c8c0 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66  _list" member of
6c8d0 20 74 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61   the . * associa
6c8e0 74 65 64 20 73 74 72 75 63 74 20 54 72 69 67 67  ted struct Trigg
6c8f0 65 72 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65  er instance. The
6c900 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
6c910 66 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  f the linked lis
6c920 74 20 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73  t is. * the firs
6c930 74 20 73 74 65 70 20 6f 66 20 74 68 65 20 74 72  t step of the tr
6c940 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20  igger-program.. 
6c950 2a 20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d  * . * The "op" m
6c960 65 6d 62 65 72 20 69 6e 64 69 63 61 74 65 73 20  ember indicates 
6c970 77 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20  whether this is 
6c980 61 20 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53  a "DELETE", "INS
6c990 45 52 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f  ERT", "UPDATE" o
6c9a0 72 0a 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74  r. * "SELECT" st
6c9b0 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61  atement. The mea
6c9c0 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68  nings of the oth
6c9d0 65 72 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65  er members is de
6c9e0 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20  termined by the 
6c9f0 0a 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70  . * value of "op
6ca00 22 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a  " as follows:. *
6ca10 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e  . * (op == TK_IN
6ca20 53 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20  SERT). * orconf 
6ca30 20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65     -> stores the
6ca40 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67   ON CONFLICT alg
6ca50 6f 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63  orithm. * pSelec
6ca60 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69  t   -> If this i
6ca70 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f  s an INSERT INTO
6ca80 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20   ... SELECT ... 
6ca90 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a  statement, then.
6caa0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
6cab0 74 68 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f  this stores a po
6cac0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c  inter to the SEL
6cad0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f  ECT statement. O
6cae0 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20  therwise NULL.. 
6caf0 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41  * target    -> A
6cb00 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74   token holding t
6cb10 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f  he quoted name o
6cb20 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 69  f the table to i
6cb30 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70  nsert into.. * p
6cb40 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 74  ExprList -> If t
6cb50 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
6cb60 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53   INTO ... VALUES
6cb70 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20   ... statement, 
6cb80 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20  then. *         
6cb90 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73       this stores
6cba0 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e   values to be in
6cbb0 73 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69 73  serted. Otherwis
6cbc0 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69  e NULL.. * pIdLi
6cbd0 73 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20  st   -> If this 
6cbe0 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54  is an INSERT INT
6cbf0 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e  O ... (<column-n
6cc00 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e  ames>) VALUES ..
6cc10 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  . . *           
6cc20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68     statement, th
6cc30 65 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20 74  en this stores t
6cc40 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20  he column-names 
6cc50 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20 20  to be. *        
6cc60 20 20 20 20 20 20 69 6e 73 65 72 74 65 64 20 69        inserted i
6cc70 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d  nto.. *. * (op =
6cc80 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20  = TK_DELETE). * 
6cc90 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74  target    -> A t
6cca0 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65  oken holding the
6ccb0 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20   quoted name of 
6ccc0 74 68 65 20 74 61 62 6c 65 20 74 6f 20 64 65 6c  the table to del
6ccd0 65 74 65 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68  ete from.. * pWh
6cce0 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57 48  ere    -> The WH
6ccf0 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ERE clause of th
6cd00 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  e DELETE stateme
6cd10 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70 65  nt if one is spe
6cd20 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 20  cified.. *      
6cd30 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 73          Otherwis
6cd40 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28  e NULL.. * . * (
6cd50 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29  op == TK_UPDATE)
6cd60 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e  . * target    ->
6cd70 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67   A token holding
6cd80 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65   the quoted name
6cd90 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
6cda0 20 75 70 64 61 74 65 20 72 6f 77 73 20 6f 66 2e   update rows of.
6cdb0 0a 20 2a 20 70 57 68 65 72 65 20 20 20 20 2d 3e  . * pWhere    ->
6cdc0 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
6cdd0 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45 20  e of the UPDATE 
6cde0 73 74 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65  statement if one
6cdf0 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 20   is specified.. 
6ce00 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  *              O
6ce10 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20  therwise NULL.. 
6ce20 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 41  * pExprList -> A
6ce30 20 6c 69 73 74 20 6f 66 20 74 68 65 20 63 6f 6c   list of the col
6ce40 75 6d 6e 73 20 74 6f 20 75 70 64 61 74 65 20 61  umns to update a
6ce50 6e 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  nd the expressio
6ce60 6e 73 20 74 6f 20 75 70 64 61 74 65 0a 20 2a 20  ns to update. * 
6ce70 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
6ce80 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c 69 74 65  m to. See sqlite
6ce90 33 55 70 64 61 74 65 28 29 20 64 6f 63 75 6d 65  3Update() docume
6cea0 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70 43 68 61  ntation of "pCha
6ceb0 6e 67 65 73 22 0a 20 2a 20 20 20 20 20 20 20 20  nges". *        
6cec0 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a        argument..
6ced0 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54   * . */.struct T
6cee0 72 69 67 67 65 72 53 74 65 70 20 7b 0a 20 20 75  riggerStep {.  u
6cef0 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  8 op;           
6cf00 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b      /* One of TK
6cf10 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41  _DELETE, TK_UPDA
6cf20 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54  TE, TK_INSERT, T
6cf30 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38  K_SELECT */.  u8
6cf40 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20   orconf;        
6cf50 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63     /* OE_Rollbac
6cf60 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67  k etc. */.  Trig
6cf70 67 65 72 20 2a 70 54 72 69 67 3b 20 20 20 20 20  ger *pTrig;     
6cf80 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20   /* The trigger 
6cf90 74 68 61 74 20 74 68 69 73 20 73 74 65 70 20 69  that this step i
6cfa0 73 20 61 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20  s a part of */. 
6cfb0 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
6cfc0 3b 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20  ;     /* SELECT 
6cfd0 73 74 61 74 6d 65 6e 74 20 6f 72 20 52 48 53 20  statment or RHS 
6cfe0 6f 66 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e  of INSERT INTO .
6cff0 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a  . SELECT ... */.
6d000 20 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20    Token target; 
6d010 20 20 20 20 20 20 20 2f 2a 20 54 61 72 67 65 74         /* Target
6d020 20 74 61 62 6c 65 20 66 6f 72 20 44 45 4c 45 54   table for DELET
6d030 45 2c 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52  E, UPDATE, INSER
6d040 54 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  T */.  Expr *pWh
6d050 65 72 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ere;        /* T
6d060 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
6d070 66 6f 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50  for DELETE or UP
6d080 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 20  DATE steps */.  
6d090 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72 4c  ExprList *pExprL
6d0a0 69 73 74 3b 20 2f 2a 20 53 45 54 20 63 6c 61 75  ist; /* SET clau
6d0b0 73 65 20 66 6f 72 20 55 50 44 41 54 45 2e 20 20  se for UPDATE.  
6d0c0 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 66 6f  VALUES clause fo
6d0d0 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 49 64  r INSERT */.  Id
6d0e0 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20  List *pIdList;  
6d0f0 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d     /* Column nam
6d100 65 73 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f  es for INSERT */
6d110 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
6d120 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20  pNext;  /* Next 
6d130 69 6e 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74  in the link-list
6d140 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65   */.  TriggerSte
6d150 70 20 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61  p *pLast;  /* La
6d160 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69  st element in li
6d170 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66  nk-list. Valid f
6d180 6f 72 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79  or 1st elem only
6d190 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
6d1a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
6d1b0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69  cture contains i
6d1c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20  nformation used 
6d1d0 62 79 20 74 68 65 20 73 71 6c 69 74 65 46 69 78  by the sqliteFix
6d1e0 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  ....** routines 
6d1f0 61 73 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65  as they walk the
6d200 20 70 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d   parse tree to m
6d210 61 6b 65 20 64 61 74 61 62 61 73 65 20 72 65 66  ake database ref
6d220 65 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69  erences.** expli
6d230 63 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65  cit.  .*/.typede
6d240 66 20 73 74 72 75 63 74 20 44 62 46 69 78 65 72  f struct DbFixer
6d250 20 44 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74   DbFixer;.struct
6d260 20 44 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72   DbFixer {.  Par
6d270 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
6d280 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
6d290 63 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20  context.  Error 
6d2a0 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e  messages written
6d2b0 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   here */.  const
6d2c0 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f   char *zDb;    /
6d2d0 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  * Make sure all 
6d2e0 6f 62 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74  objects are cont
6d2f0 61 69 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61  ained in this da
6d300 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73  tabase */.  cons
6d310 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20  t char *zType;  
6d320 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 63  /* Type of the c
6d330 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20  ontainer - used 
6d340 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67  for error messag
6d350 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f  es */.  const To
6d360 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e  ken *pName; /* N
6d370 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61  ame of the conta
6d380 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20  iner - used for 
6d390 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a  error messages *
6d3a0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f  /.};../*.** An o
6d3b0 62 6a 65 63 74 65 64 20 75 73 65 64 20 74 6f 20  bjected used to 
6d3c0 61 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74  accumulate the t
6d3d0 65 78 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20  ext of a string 
6d3e0 77 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e  where we.** do n
6d3f0 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b  ot necessarily k
6d400 6e 6f 77 20 68 6f 77 20 62 69 67 20 74 68 65 20  now how big the 
6d410 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69  string will be i
6d420 6e 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74  n the end..*/.st
6d430 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a  ruct StrAccum {.
6d440 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
6d450 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e         /* Option
6d460 61 6c 20 64 61 74 61 62 61 73 65 20 66 6f 72 20  al database for 
6d470 6c 6f 6f 6b 61 73 69 64 65 2e 20 20 43 61 6e 20  lookaside.  Can 
6d480 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61  be NULL */.  cha
6d490 72 20 2a 7a 42 61 73 65 3b 20 20 20 20 20 20 20  r *zBase;       
6d4a0 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f    /* A base allo
6d4b0 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f  cation.  Not fro
6d4c0 6d 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63  m malloc. */.  c
6d4d0 68 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20  har *zText;     
6d4e0 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e      /* The strin
6d4f0 67 20 63 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66  g collected so f
6d500 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68  ar */.  int  nCh
6d510 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ar;          /* 
6d520 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74  Length of the st
6d530 72 69 6e 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20  ring so far */. 
6d540 20 69 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20   int  nAlloc;   
6d550 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20        /* Amount 
6d560 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  of space allocat
6d570 65 64 20 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20  ed in zText */. 
6d580 20 69 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20   int  mxAlloc;  
6d590 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
6d5a0 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20   allowed string 
6d5b0 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 75 38 20 20  length */.  u8  
6d5c0 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20   mallocFailed;  
6d5d0 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65   /* Becomes true
6d5e0 20 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61   if any memory a
6d5f0 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20  llocation fails 
6d600 2a 2f 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c  */.  u8   useMal
6d610 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  loc;      /* Tru
6d620 65 20 69 66 20 7a 54 65 78 74 20 69 73 20 65 6e  e if zText is en
6d630 6c 61 72 67 65 61 62 6c 65 20 75 73 69 6e 67 20  largeable using 
6d640 72 65 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20  realloc */.  u8 
6d650 20 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20    tooBig;       
6d660 20 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75    /* Becomes tru
6d670 65 20 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65  e if string size
6d680 20 65 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20   exceeds limits 
6d690 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70  */.};../*.** A p
6d6a0 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73  ointer to this s
6d6b0 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
6d6c0 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20   to communicate 
6d6d0 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66  information.** f
6d6e0 72 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20  rom sqlite3Init 
6d6f0 61 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65  and OP_ParseSche
6d700 6d 61 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  ma into the sqli
6d710 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e  te3InitCallback.
6d720 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
6d730 63 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ct {.  sqlite3 *
6d740 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  db;        /* Th
6d750 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67  e database being
6d760 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
6d770 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
6d780 20 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d        /* 0 for m
6d790 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31  ain database.  1
6d7a0 20 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66   for TEMP, 2.. f
6d7b0 6f 72 20 41 54 54 41 43 48 65 64 20 2a 2f 0a 20  or ATTACHed */. 
6d7c0 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
6d7d0 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65  ;    /* Error me
6d7e0 73 73 61 67 65 20 73 74 6f 72 65 64 20 68 65 72  ssage stored her
6d7f0 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  e */.  int rc;  
6d800 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
6d810 73 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64  sult code stored
6d820 20 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44   here */.} InitD
6d830 61 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75  ata;../*.** Stru
6d840 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67  cture containing
6d850 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72   global configur
6d860 61 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74  ation data for t
6d870 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
6d880 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74  y..**.** This st
6d890 72 75 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e  ructure also con
6d8a0 74 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65  tains some state
6d8b0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
6d8c0 0a 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43  .struct Sqlite3C
6d8d0 6f 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d  onfig {.  int bM
6d8e0 65 6d 73 74 61 74 3b 20 20 20 20 20 20 20 20 20  emstat;         
6d8f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
6d900 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65  rue to enable me
6d910 6d 6f 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20  mory status */. 
6d920 20 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b   int bCoreMutex;
6d930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d940 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e     /* True to en
6d950 61 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69  able core mutexi
6d960 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c  ng */.  int bFul
6d970 6c 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  lMutex;         
6d980 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
6d990 65 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c  e to enable full
6d9a0 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69   mutexing */.  i
6d9b0 6e 74 20 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20  nt mxStrlen;    
6d9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d9d0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 74 72 69   /* Maximum stri
6d9e0 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69  ng length */.  i
6d9f0 6e 74 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20  nt szLookaside; 
6da00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da10 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b   /* Default look
6da20 61 73 69 64 65 20 62 75 66 66 65 72 20 73 69 7a  aside buffer siz
6da30 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b  e */.  int nLook
6da40 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20  aside;          
6da50 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
6da60 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  ult lookaside bu
6da70 66 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20  ffer count */.  
6da80 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
6da90 6f 64 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20  ods m;          
6daa0 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d    /* Low-level m
6dab0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
6dac0 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20   interface */.  
6dad0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
6dae0 74 68 6f 64 73 20 6d 75 74 65 78 3b 20 20 20 20  thods mutex;    
6daf0 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d    /* Low-level m
6db00 75 74 65 78 20 69 6e 74 65 72 66 61 63 65 20 2a  utex interface *
6db10 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63  /.  sqlite3_pcac
6db20 68 65 5f 6d 65 74 68 6f 64 73 20 70 63 61 63 68  he_methods pcach
6db30 65 3b 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76  e;    /* Low-lev
6db40 65 6c 20 70 61 67 65 2d 63 61 63 68 65 20 69 6e  el page-cache in
6db50 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69  terface */.  voi
6db60 64 20 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20  d *pHeap;       
6db70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6db80 2a 20 48 65 61 70 20 73 74 6f 72 61 67 65 20 73  * Heap storage s
6db90 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48  pace */.  int nH
6dba0 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  eap;            
6dbb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
6dbc0 69 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a  ize of pHeap[] *
6dbd0 2f 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d  /.  int mnReq, m
6dbe0 78 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20  xReq;           
6dbf0 20 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64        /* Min and
6dc00 20 6d 61 78 20 68 65 61 70 20 72 65 71 75 65 73   max heap reques
6dc10 74 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f  ts sizes */.  vo
6dc20 69 64 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20  id *pScratch;   
6dc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dc40 2f 2a 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72  /* Scratch memor
6dc50 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72  y */.  int szScr
6dc60 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20  atch;           
6dc70 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
6dc80 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68   of each scratch
6dc90 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74   buffer */.  int
6dca0 20 6e 53 63 72 61 74 63 68 3b 20 20 20 20 20 20   nScratch;      
6dcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6dcc0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61  * Number of scra
6dcd0 74 63 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20  tch buffers */. 
6dce0 20 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20   void *pPage;   
6dcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dd00 20 20 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65     /* Page cache
6dd10 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74   memory */.  int
6dd20 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20   szPage;        
6dd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6dd40 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70  * Size of each p
6dd50 61 67 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a  age in pPage[] *
6dd60 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20  /.  int nPage;  
6dd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dd80 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6dd90 6f 66 20 70 61 67 65 73 20 69 6e 20 70 50 61 67  of pages in pPag
6dda0 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50  e[] */.  int mxP
6ddb0 61 72 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20  arserStack;     
6ddc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61             /* ma
6ddd0 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74  ximum depth of t
6dde0 68 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20  he parser stack 
6ddf0 2a 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43  */.  int sharedC
6de00 61 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20  acheEnabled;    
6de10 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69         /* true i
6de20 66 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d  f shared-cache m
6de30 6f 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20  ode enabled */. 
6de40 20 2f 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69   /* The above mi
6de50 67 68 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a  ght be initializ
6de60 65 64 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  ed to non-zero. 
6de70 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e   The following n
6de80 65 65 64 20 74 6f 20 61 6c 77 61 79 73 0a 20 20  eed to always.  
6de90 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20  ** initially be 
6dea0 7a 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a  zero, however. *
6deb0 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20  /.  int isInit; 
6dec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ded0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66        /* True af
6dee0 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ter initializati
6def0 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  on has finished 
6df00 2a 2f 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72  */.  int inProgr
6df10 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ess;            
6df20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77         /* True w
6df30 68 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74  hile initializat
6df40 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20  ion in progress 
6df50 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 75 74 65 78  */.  int isMutex
6df60 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20  Init;           
6df70 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61         /* True a
6df80 66 74 65 72 20 6d 75 74 65 78 65 73 20 61 72 65  fter mutexes are
6df90 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
6dfa0 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 6e    int isMallocIn
6dfb0 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  it;             
6dfc0 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65      /* True afte
6dfd0 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74  r malloc is init
6dfe0 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74  ialized */.  int
6dff0 20 69 73 50 43 61 63 68 65 49 6e 69 74 3b 20 20   isPCacheInit;  
6e000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6e010 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c  * True after mal
6e020 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  loc is initializ
6e030 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
6e040 6d 75 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65  mutex *pInitMute
6e050 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74  x;        /* Mut
6e060 65 78 20 75 73 65 64 20 62 79 20 73 71 6c 69 74  ex used by sqlit
6e070 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
6e080 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69  */.  int nRefIni
6e090 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  tMutex;         
6e0a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6e0b0 20 6f 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e   of users of pIn
6e0c0 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f  itMutex */.};../
6e0d0 2a 0a 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69  *.** Context poi
6e0e0 6e 74 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e  nter passed down
6e0f0 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 72 65   through the tre
6e100 65 2d 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63  e-walk..*/.struc
6e110 74 20 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74  t Walker {.  int
6e120 20 28 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   (*xExprCallback
6e130 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a  )(Walker*, Expr*
6e140 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61  );     /* Callba
6e150 63 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f  ck for expressio
6e160 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  ns */.  int (*xS
6e170 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57  electCallback)(W
6e180 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b  alker*,Select*);
6e190 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f    /* Callback fo
6e1a0 72 20 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50  r SELECTs */.  P
6e1b0 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
6e1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e1d0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
6e1e0 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a  er context.  */.
6e1f0 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20    union {       
6e200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e210 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
6e220 78 74 72 61 20 64 61 74 61 20 66 6f 72 20 63 61  xtra data for ca
6e230 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61  llback */.    Na
6e240 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20  meContext *pNC; 
6e250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e260 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69           /* Nami
6e270 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
6e280 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
6e290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6e2b0 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f  Integer value */
6e2c0 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46  .  } u;.};../* F
6e2d0 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
6e2e0 6f 6e 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ons */.SQLITE_PR
6e2f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6e300 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72  3WalkExpr(Walker
6e310 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
6e320 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6e330 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
6e340 74 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c  t(Walker*, ExprL
6e350 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
6e360 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6e370 33 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b  3WalkSelect(Walk
6e380 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53  er*, Select*);.S
6e390 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6e3a0 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  t sqlite3WalkSel
6e3b0 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c  ectExpr(Walker*,
6e3c0 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54   Select*);.SQLIT
6e3d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6e3e0 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46  lite3WalkSelectF
6e3f0 72 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c  rom(Walker*, Sel
6e400 65 63 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65  ect*);../*.** Re
6e410 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74  turn code from t
6e420 68 65 20 70 61 72 73 65 2d 74 72 65 65 20 77 61  he parse-tree wa
6e430 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73  lking primitives
6e440 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61   and their.** ca
6e450 6c 6c 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66  llbacks..*/.#def
6e460 69 6e 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  ine WRC_Continue
6e470 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69      0   /* Conti
6e480 6e 75 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68  nue down into ch
6e490 69 6c 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e  ildren */.#defin
6e4a0 65 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20  e WRC_Prune     
6e4b0 20 20 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68    1   /* Omit ch
6e4c0 69 6c 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69  ildren but conti
6e4d0 6e 75 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c  nue walking sibl
6e4e0 69 6e 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ings */.#define 
6e4f0 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20  WRC_Abort       
6e500 32 20 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74  2   /* Abandon t
6e510 68 65 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a  he tree walk */.
6e520 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20  ./*.** Assuming 
6e530 7a 49 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  zIn points to th
6e540 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
6e550 61 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  a UTF-8 characte
6e560 72 2c 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49  r,.** advance zI
6e570 6e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  n to point to th
6e580 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
6e590 74 68 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63  the next UTF-8 c
6e5a0 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65  haracter..*/.#de
6e5b0 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50  fine SQLITE_SKIP
6e5c0 5f 55 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20  _UTF8(zIn) {    
6e5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e5e0 20 20 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a      \.  if( (*(z
6e5f0 49 6e 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20  In++))>=0xc0 ){ 
6e600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e610 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
6e620 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20     while( (*zIn 
6e630 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b  & 0xc0)==0x80 ){
6e640 20 7a 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20   zIn++; }       
6e650 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
6e660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
6e690 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51  .}../*.** The SQ
6e6a0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
6e6b0 54 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65  T macro can be e
6e6c0 69 74 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74  ither a constant
6e6d0 20 28 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e   (for production
6e6e0 0a 2a 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61  .** builds) or a
6e6f0 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28   function call (
6e700 66 6f 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20  for debugging). 
6e710 20 49 66 20 69 74 20 69 73 20 61 20 66 75 6e 63   If it is a func
6e720 74 69 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74  tion call,.** it
6e730 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72   allows the oper
6e740 61 74 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72  ator to set a br
6e750 65 61 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20  eakpoint at the 
6e760 73 70 6f 74 20 77 68 65 72 65 20 64 61 74 61 62  spot where datab
6e770 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f  ase.** corruptio
6e780 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63  n is first detec
6e790 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ted..*/.#ifdef S
6e7a0 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49  QLITE_DEBUG.SQLI
6e7b0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
6e7c0 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28   sqlite3Corrupt(
6e7d0 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20  void);.# define 
6e7e0 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
6e7f0 4b 50 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75  KPT sqlite3Corru
6e800 70 74 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  pt().#else.# def
6e810 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  ine SQLITE_CORRU
6e820 50 54 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43  PT_BKPT SQLITE_C
6e830 4f 52 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f  ORRUPT.#endif../
6e840 2a 0a 2a 2a 20 54 68 65 20 63 74 79 70 65 2e 68  *.** The ctype.h
6e850 20 68 65 61 64 65 72 20 69 73 20 6e 65 65 64 65   header is neede
6e860 64 20 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20  d for non-ASCII 
6e870 73 79 73 74 65 6d 73 2e 20 20 49 74 20 69 73 20  systems.  It is 
6e880 61 6c 73 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 62  also.** needed b
6e890 79 20 46 54 53 33 20 77 68 65 6e 20 46 54 53 33  y FTS3 when FTS3
6e8a0 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20   is included in 
6e8b0 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
6e8c0 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
6e8d0 64 28 53 51 4c 49 54 45 5f 41 53 43 49 49 29 20  d(SQLITE_ASCII) 
6e8e0 7c 7c 20 5c 0a 20 20 20 20 28 64 65 66 69 6e 65  || \.    (define
6e8f0 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
6e900 46 54 53 33 29 20 26 26 20 64 65 66 69 6e 65 64  FTS3) && defined
6e910 28 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41  (SQLITE_AMALGAMA
6e920 54 49 4f 4e 29 29 0a 23 20 69 6e 63 6c 75 64 65  TION)).# include
6e930 20 3c 63 74 79 70 65 2e 68 3e 0a 23 65 6e 64 69   <ctype.h>.#endi
6e940 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  f../*.** The fol
6e950 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 69  lowing macros mi
6e960 6d 69 63 20 74 68 65 20 73 74 61 6e 64 61 72 64  mic the standard
6e970 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f   library functio
6e980 6e 73 20 74 6f 75 70 70 65 72 28 29 2c 0a 2a 2a  ns toupper(),.**
6e990 20 69 73 73 70 61 63 65 28 29 2c 20 69 73 61 6c   isspace(), isal
6e9a0 6e 75 6d 28 29 2c 20 69 73 64 69 67 69 74 28 29  num(), isdigit()
6e9b0 20 61 6e 64 20 69 73 78 64 69 67 69 74 28 29 2c   and isxdigit(),
6e9c0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54   respectively. T
6e9d0 68 65 0a 2a 2a 20 73 71 6c 69 74 65 20 76 65 72  he.** sqlite ver
6e9e0 73 69 6f 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b 20  sions only work 
6e9f0 66 6f 72 20 41 53 43 49 49 20 63 68 61 72 61 63  for ASCII charac
6ea00 74 65 72 73 2c 20 72 65 67 61 72 64 6c 65 73 73  ters, regardless
6ea10 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23   of locale..*/.#
6ea20 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
6ea30 49 49 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  II.# define sqli
6ea40 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 28  te3Toupper(x)  (
6ea50 28 78 29 26 7e 28 73 71 6c 69 74 65 33 43 74 79  (x)&~(sqlite3Cty
6ea60 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20  peMap[(unsigned 
6ea70 63 68 61 72 29 28 78 29 5d 26 30 78 32 30 29 29  char)(x)]&0x20))
6ea80 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6ea90 33 49 73 73 70 61 63 65 28 78 29 20 20 20 28 73  3Isspace(x)   (s
6eaa0 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
6eab0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6eac0 29 5d 26 30 78 30 31 29 0a 23 20 64 65 66 69 6e  )]&0x01).# defin
6ead0 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d  e sqlite3Isalnum
6eae0 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74  (x)   (sqlite3Ct
6eaf0 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64  ypeMap[(unsigned
6eb00 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 36 29   char)(x)]&0x06)
6eb10 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6eb20 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 28 73  3Isalpha(x)   (s
6eb30 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
6eb40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6eb50 29 5d 26 30 78 30 32 29 0a 23 20 64 65 66 69 6e  )]&0x02).# defin
6eb60 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  e sqlite3Isdigit
6eb70 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74  (x)   (sqlite3Ct
6eb80 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64  ypeMap[(unsigned
6eb90 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 34 29   char)(x)]&0x04)
6eba0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6ebb0 33 49 73 78 64 69 67 69 74 28 78 29 20 20 28 73  3Isxdigit(x)  (s
6ebc0 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
6ebd0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6ebe0 29 5d 26 30 78 30 38 29 0a 23 20 64 65 66 69 6e  )]&0x08).# defin
6ebf0 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72  e sqlite3Tolower
6ec00 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 55 70  (x)   (sqlite3Up
6ec10 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69  perToLower[(unsi
6ec20 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 29 0a  gned char)(x)]).
6ec30 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
6ec40 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78 29  qlite3Toupper(x)
6ec50 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69     toupper((unsi
6ec60 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23  gned char)(x)).#
6ec70 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
6ec80 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73 70  sspace(x)   issp
6ec90 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68  ace((unsigned ch
6eca0 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65  ar)(x)).# define
6ecb0 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28   sqlite3Isalnum(
6ecc0 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e  x)   isalnum((un
6ecd0 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29  signed char)(x))
6ece0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6ecf0 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 69 73  3Isalpha(x)   is
6ed00 61 6c 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20  alpha((unsigned 
6ed10 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69  char)(x)).# defi
6ed20 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  ne sqlite3Isdigi
6ed30 74 28 78 29 20 20 20 69 73 64 69 67 69 74 28 28  t(x)   isdigit((
6ed40 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6ed50 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  )).# define sqli
6ed60 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20  te3Isxdigit(x)  
6ed70 69 73 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e  isxdigit((unsign
6ed80 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64  ed char)(x)).# d
6ed90 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c  efine sqlite3Tol
6eda0 6f 77 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65  ower(x)   tolowe
6edb0 72 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  r((unsigned char
6edc0 29 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  )(x)).#endif../*
6edd0 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e  .** Internal fun
6ede0 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73  ction prototypes
6edf0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
6ee00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  TE int sqlite3St
6ee10 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  rICmp(const char
6ee20 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
6ee30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6ee40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e  E int sqlite3IsN
6ee50 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72  umber(const char
6ee60 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51  *, int*, u8);.SQ
6ee70 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6ee80 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
6ee90 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23  (const char*);.#
6eea0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74  define sqlite3St
6eeb0 72 4e 49 43 6d 70 20 73 71 6c 69 74 65 33 5f 73  rNICmp sqlite3_s
6eec0 74 72 6e 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f  trnicmp..SQLITE_
6eed0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6eee0 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f  te3MallocInit(vo
6eef0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
6ef00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6ef10 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b  MallocEnd(void);
6ef20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ef30 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c  void *sqlite3Mal
6ef40 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45  loc(int);.SQLITE
6ef50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
6ef60 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
6ef70 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
6ef80 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
6ef90 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
6efa0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a  sqlite3*, int);.
6efb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6efc0 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61  oid *sqlite3DbMa
6efd0 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a  llocRaw(sqlite3*
6efe0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6eff0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
6f000 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c  ite3DbStrDup(sql
6f010 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
6f020 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f030 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
6f040 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65  DbStrNDup(sqlite
6f050 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  3*,const char*, 
6f060 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6f070 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
6f080 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c  e3Realloc(void*,
6f090 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6f0a0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
6f0b0 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72  te3DbReallocOrFr
6f0c0 65 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f  ee(sqlite3 *, vo
6f0d0 69 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  id *, int);.SQLI
6f0e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6f0f0 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f  *sqlite3DbReallo
6f100 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69  c(sqlite3 *, voi
6f110 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  d *, int);.SQLIT
6f120 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6f130 71 6c 69 74 65 33 44 62 46 72 65 65 28 73 71 6c  qlite3DbFree(sql
6f140 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53  ite3*, void*);.S
6f150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6f160 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  t sqlite3MallocS
6f170 69 7a 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  ize(void*);.SQLI
6f180 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6f190 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
6f1a0 7a 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69  ze(sqlite3*, voi
6f1b0 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d*);.SQLITE_PRIV
6f1c0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
6f1d0 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69  3ScratchMalloc(i
6f1e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6f1f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6f200 53 63 72 61 74 63 68 46 72 65 65 28 76 6f 69 64  ScratchFree(void
6f210 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6f220 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
6f230 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b  PageMalloc(int);
6f240 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f250 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
6f260 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c  Free(void*);.SQL
6f270 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6f280 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65   sqlite3MemSetDe
6f290 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c  fault(void);.SQL
6f2a0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6f2b0 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61   sqlite3BenignMa
6f2c0 6c 6c 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28  llocHooks(void (
6f2d0 2a 29 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28  *)(void), void (
6f2e0 2a 29 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54  *)(void));.SQLIT
6f2f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6f300 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d  lite3MemoryAlarm
6f310 28 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c  (void (*)(void*,
6f320 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20   sqlite3_int64, 
6f330 69 6e 74 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c  int), void*, sql
6f340 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a  ite3_int64);../*
6f350 0a 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77  .** On systems w
6f360 69 74 68 20 61 6d 70 6c 65 20 73 74 61 63 6b 20  ith ample stack 
6f370 73 70 61 63 65 20 61 6e 64 20 74 68 61 74 20 73  space and that s
6f380 75 70 70 6f 72 74 20 61 6c 6c 6f 63 61 28 29 2c  upport alloca(),
6f390 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20   make.** use of 
6f3a0 61 6c 6c 6f 63 61 28 29 20 74 6f 20 6f 62 74 61  alloca() to obta
6f3b0 69 6e 20 73 70 61 63 65 20 66 6f 72 20 6c 61 72  in space for lar
6f3c0 67 65 20 61 75 74 6f 6d 61 74 69 63 20 6f 62 6a  ge automatic obj
6f3d0 65 63 74 73 2e 20 20 42 79 20 64 65 66 61 75 6c  ects.  By defaul
6f3e0 74 2c 0a 2a 2a 20 6f 62 74 61 69 6e 20 73 70 61  t,.** obtain spa
6f3f0 63 65 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ce from malloc()
6f400 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f  ..**.** The allo
6f410 63 61 28 29 20 72 6f 75 74 69 6e 65 20 6e 65 76  ca() routine nev
6f420 65 72 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e  er returns NULL.
6f430 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73    This will caus
6f440 65 20 63 6f 64 65 20 70 61 74 68 73 0a 2a 2a 20  e code paths.** 
6f450 74 68 61 74 20 64 65 61 6c 20 77 69 74 68 20 73  that deal with s
6f460 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63  qlite3StackAlloc
6f470 28 29 20 66 61 69 6c 75 72 65 73 20 74 6f 20 62  () failures to b
6f480 65 20 75 6e 72 65 61 63 68 61 62 6c 65 2e 0a 2a  e unreachable..*
6f490 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
6f4a0 55 53 45 5f 41 4c 4c 4f 43 41 0a 23 20 64 65 66  USE_ALLOCA.# def
6f4b0 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b  ine sqlite3Stack
6f4c0 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20 20 20  AllocRaw(D,N)   
6f4d0 61 6c 6c 6f 63 61 28 4e 29 0a 23 20 64 65 66 69  alloca(N).# defi
6f4e0 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41  ne sqlite3StackA
6f4f0 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 6d  llocZero(D,N)  m
6f500 65 6d 73 65 74 28 61 6c 6c 6f 63 61 28 4e 29 2c  emset(alloca(N),
6f510 20 30 2c 20 4e 29 0a 23 20 64 65 66 69 6e 65 20   0, N).# define 
6f520 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65  sqlite3StackFree
6f530 28 44 2c 50 29 20 20 20 20 20 20 20 0a 23 65 6c  (D,P)       .#el
6f540 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  se.# define sqli
6f550 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77  te3StackAllocRaw
6f560 28 44 2c 4e 29 20 20 20 73 71 6c 69 74 65 33 44  (D,N)   sqlite3D
6f570 62 4d 61 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 0a  bMallocRaw(D,N).
6f580 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6f590 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44  StackAllocZero(D
6f5a0 2c 4e 29 20 20 73 71 6c 69 74 65 33 44 62 4d 61  ,N)  sqlite3DbMa
6f5b0 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 0a 23 20  llocZero(D,N).# 
6f5c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74  define sqlite3St
6f5d0 61 63 6b 46 72 65 65 28 44 2c 50 29 20 20 20 20  ackFree(D,P)    
6f5e0 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
6f5f0 28 44 2c 50 29 0a 23 65 6e 64 69 66 0a 0a 23 69  (D,P).#endif..#i
6f600 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
6f610 4c 45 5f 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54  LE_MEMSYS3.SQLIT
6f620 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
6f630 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
6f640 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47  ods *sqlite3MemG
6f650 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 3b  etMemsys3(void);
6f660 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
6f670 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
6f680 53 59 53 35 0a 53 51 4c 49 54 45 5f 50 52 49 56  SYS5.SQLITE_PRIV
6f690 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
6f6a0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73  3_mem_methods *s
6f6b0 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73  qlite3MemGetMems
6f6c0 79 73 35 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69  ys5(void);.#endi
6f6d0 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  f...#ifndef SQLI
6f6e0 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51  TE_MUTEX_OMIT.SQ
6f6f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73  LITE_PRIVATE   s
6f700 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
6f710 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66  hods *sqlite3Def
6f720 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 3b  aultMutex(void);
6f730 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f740 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
6f750 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  *sqlite3MutexAll
6f760 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  oc(int);.SQLITE_
6f770 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
6f780 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76  lite3MutexInit(v
6f790 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
6f7a0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
6f7b0 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29  e3MutexEnd(void)
6f7c0 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  ;.#endif..SQLITE
6f7d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6f7e0 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28  ite3StatusValue(
6f7f0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6f800 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6f810 33 53 74 61 74 75 73 41 64 64 28 69 6e 74 2c 20  3StatusAdd(int, 
6f820 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6f830 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6f840 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 2c 20  3StatusSet(int, 
6f850 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
6f860 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6f870 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a  3IsNaN(double);.
6f880 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f890 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72  void sqlite3VXPr
6f8a0 69 6e 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20  intf(StrAccum*, 
6f8b0 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  int, const char*
6f8c0 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 23 69 66 6e  , va_list);.#ifn
6f8d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
6f8e0 54 52 41 43 45 0a 53 51 4c 49 54 45 5f 50 52 49  TRACE.SQLITE_PRI
6f8f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6f900 33 58 50 72 69 6e 74 66 28 53 74 72 41 63 63 75  3XPrintf(StrAccu
6f910 6d 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  m*, const char*,
6f920 20 2e 2e 2e 29 3b 0a 23 65 6e 64 69 66 0a 53 51   ...);.#endif.SQ
6f930 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
6f940 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74  r *sqlite3MPrint
6f950 66 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  f(sqlite3*,const
6f960 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51   char*, ...);.SQ
6f970 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
6f980 72 20 2a 73 71 6c 69 74 65 33 56 4d 50 72 69 6e  r *sqlite3VMPrin
6f990 74 66 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  tf(sqlite3*,cons
6f9a0 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74  t char*, va_list
6f9b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6f9c0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d  E char *sqlite3M
6f9d0 41 70 70 65 6e 64 66 28 73 71 6c 69 74 65 33 2a  Appendf(sqlite3*
6f9e0 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61  ,char*,const cha
6f9f0 72 2a 2c 2e 2e 2e 29 3b 0a 23 69 66 20 64 65 66  r*,...);.#if def
6fa00 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
6fa10 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
6fa20 49 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54  ITE_DEBUG).SQLIT
6fa30 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
6fa40 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
6fa50 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ntf(const char*,
6fa60 20 2e 2e 2e 29 3b 0a 23 65 6e 64 69 66 0a 23 69   ...);.#endif.#i
6fa70 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
6fa80 5f 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52  _TEST).SQLITE_PR
6fa90 49 56 41 54 45 20 20 20 76 6f 69 64 20 2a 73 71  IVATE   void *sq
6faa0 6c 69 74 65 33 54 65 73 74 54 65 78 74 54 6f 50  lite3TestTextToP
6fab0 74 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  tr(const char*);
6fac0 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50  .#endif.SQLITE_P
6fad0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6fae0 74 65 33 53 65 74 53 74 72 69 6e 67 28 63 68 61  te3SetString(cha
6faf0 72 20 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 2c 20  r **, sqlite3*, 
6fb00 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e  const char*, ...
6fb10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6fb20 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72  E void sqlite3Er
6fb30 72 6f 72 4d 73 67 28 50 61 72 73 65 2a 2c 20 63  rorMsg(Parse*, c
6fb40 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
6fb50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6fb60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72   void sqlite3Err
6fb70 6f 72 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b  orClear(Parse*);
6fb80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fb90 69 6e 74 20 73 71 6c 69 74 65 33 44 65 71 75 6f  int sqlite3Dequo
6fba0 74 65 28 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  te(char*);.SQLIT
6fbb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6fbc0 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65  lite3KeywordCode
6fbd0 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
6fbe0 63 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  char*, int);.SQL
6fbf0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
6fc00 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72  sqlite3RunParser
6fc10 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63  (Parse*, const c
6fc20 68 61 72 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a  har*, char **);.
6fc30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6fc40 6f 69 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73  oid sqlite3Finis
6fc50 68 43 6f 64 69 6e 67 28 50 61 72 73 65 2a 29 3b  hCoding(Parse*);
6fc60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fc70 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65  int sqlite3GetTe
6fc80 6d 70 52 65 67 28 50 61 72 73 65 2a 29 3b 0a 53  mpReg(Parse*);.S
6fc90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6fca0 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  id sqlite3Releas
6fcb0 65 54 65 6d 70 52 65 67 28 50 61 72 73 65 2a 2c  eTempReg(Parse*,
6fcc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6fcd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
6fce0 47 65 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72  GetTempRange(Par
6fcf0 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  se*,int);.SQLITE
6fd00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
6fd10 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
6fd20 52 61 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74  Range(Parse*,int
6fd30 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
6fd40 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
6fd50 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 73 71 6c  te3ExprAlloc(sql
6fd60 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  ite3*,int,const 
6fd70 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Token*,int);.SQL
6fd80 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
6fd90 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 73 71   *sqlite3Expr(sq
6fda0 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74  lite3*,int,const
6fdb0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
6fdc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6fdd0 69 74 65 33 45 78 70 72 41 74 74 61 63 68 53 75  ite3ExprAttachSu
6fde0 62 74 72 65 65 73 28 73 71 6c 69 74 65 33 2a 2c  btrees(sqlite3*,
6fdf0 45 78 70 72 2a 2c 45 78 70 72 2a 2c 45 78 70 72  Expr*,Expr*,Expr
6fe00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6fe10 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
6fe20 50 45 78 70 72 28 50 61 72 73 65 2a 2c 20 69 6e  PExpr(Parse*, in
6fe30 74 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c  t, Expr*, Expr*,
6fe40 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a   const Token*);.
6fe50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
6fe60 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72  xpr *sqlite3Expr
6fe70 41 6e 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70  And(sqlite3*,Exp
6fe80 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  r*, Expr*);.SQLI
6fe90 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
6fea0 2a 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63  *sqlite3ExprFunc
6feb0 74 69 6f 6e 28 50 61 72 73 65 2a 2c 45 78 70 72  tion(Parse*,Expr
6fec0 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  List*, Token*);.
6fed0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6fee0 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
6fef0 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50  ssignVarNumber(P
6ff00 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  arse*, Expr*);.S
6ff10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6ff20 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  id sqlite3ExprDe
6ff30 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45  lete(sqlite3*, E
6ff40 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
6ff50 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a  IVATE ExprList *
6ff60 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
6ff70 70 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70  ppend(Parse*,Exp
6ff80 72 4c 69 73 74 2a 2c 45 78 70 72 2a 29 3b 0a 53  rList*,Expr*);.S
6ff90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6ffa0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  id sqlite3ExprLi
6ffb0 73 74 53 65 74 4e 61 6d 65 28 50 61 72 73 65 2a  stSetName(Parse*
6ffc0 2c 45 78 70 72 4c 69 73 74 2a 2c 54 6f 6b 65 6e  ,ExprList*,Token
6ffd0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
6ffe0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6fff0 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 53 70  te3ExprListSetSp
70000 61 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69  an(Parse*,ExprLi
70010 73 74 2a 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0a  st*,ExprSpan*);.
70020 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70030 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c  oid sqlite3ExprL
70040 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65  istDelete(sqlite
70050 33 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a  3*, ExprList*);.
70060 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
70070 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73  nt sqlite3Init(s
70080 71 6c 69 74 65 33 2a 2c 20 63 68 61 72 2a 2a 29  qlite3*, char**)
70090 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
700a0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74   int sqlite3Init
700b0 43 61 6c 6c 62 61 63 6b 28 76 6f 69 64 2a 2c 20  Callback(void*, 
700c0 69 6e 74 2c 20 63 68 61 72 2a 2a 2c 20 63 68 61  int, char**, cha
700d0 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  r**);.SQLITE_PRI
700e0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
700f0 33 50 72 61 67 6d 61 28 50 61 72 73 65 2a 2c 54  3Pragma(Parse*,T
70100 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b  oken*,Token*,Tok
70110 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  en*,int);.SQLITE
70120 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70130 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e  lite3ResetIntern
70140 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33  alSchema(sqlite3
70150 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.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 42 65 67 69 6e 50 61 72 73 65 28 50  ite3BeginParse(P
70180 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  arse*,int);.SQLI
70190 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
701a0 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74  sqlite3CommitInt
701b0 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 73 71 6c  ernalChanges(sql
701c0 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
701d0 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
701e0 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66  lite3ResultSetOf
701f0 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c 53 65  Select(Parse*,Se
70200 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  lect*);.SQLITE_P
70210 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70220 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62  te3OpenMasterTab
70230 6c 65 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 29  le(Parse *, int)
70240 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70250 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61   void sqlite3Sta
70260 72 74 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54  rtTable(Parse*,T
70270 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74  oken*,Token*,int
70280 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ,int,int,int);.S
70290 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
702a0 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  id sqlite3AddCol
702b0 75 6d 6e 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e  umn(Parse*,Token
702c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
702d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
702e0 64 64 4e 6f 74 4e 75 6c 6c 28 50 61 72 73 65 2a  ddNotNull(Parse*
702f0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70300 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70310 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79  te3AddPrimaryKey
70320 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73  (Parse*, ExprLis
70330 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  t*, int, int, in
70340 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
70350 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
70360 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e  ddCheckConstrain
70370 74 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29  t(Parse*, Expr*)
70380 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70390 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
703a0 43 6f 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65  ColumnType(Parse
703b0 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54  *,Token*);.SQLIT
703c0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
703d0 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74  qlite3AddDefault
703e0 56 61 6c 75 65 28 50 61 72 73 65 2a 2c 45 78 70  Value(Parse*,Exp
703f0 72 53 70 61 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  rSpan*);.SQLITE_
70400 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70410 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54 79  ite3AddCollateTy
70420 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e  pe(Parse*, Token
70430 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70440 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
70450 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 54  ndTable(Parse*,T
70460 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65 6c  oken*,Token*,Sel
70470 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  ect*);..SQLITE_P
70480 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a 73  RIVATE Bitvec *s
70490 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
704a0 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f  te(u32);.SQLITE_
704b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
704c0 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42 69  te3BitvecTest(Bi
704d0 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51 4c  tvec*, u32);.SQL
704e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
704f0 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74  sqlite3BitvecSet
70500 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a  (Bitvec*, u32);.
70510 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70520 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65  oid sqlite3Bitve
70530 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c 20  cClear(Bitvec*, 
70540 75 33 32 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c  u32, void*);.SQL
70550 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
70560 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
70570 73 74 72 6f 79 28 42 69 74 76 65 63 2a 29 3b 0a  stroy(Bitvec*);.
70580 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
70590 33 32 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  32 sqlite3Bitvec
705a0 53 69 7a 65 28 42 69 74 76 65 63 2a 29 3b 0a 53  Size(Bitvec*);.S
705b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
705c0 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42  t sqlite3BitvecB
705d0 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74 2c 69  uiltinTest(int,i
705e0 6e 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  nt*);..SQLITE_PR
705f0 49 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71  IVATE RowSet *sq
70600 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28  lite3RowSetInit(
70610 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 2c  sqlite3*, void*,
70620 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b 0a   unsigned int);.
70630 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70640 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53 65  oid sqlite3RowSe
70650 74 43 6c 65 61 72 28 52 6f 77 53 65 74 2a 29 3b  tClear(RowSet*);
70660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70670 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 77 53  void sqlite3RowS
70680 65 74 49 6e 73 65 72 74 28 52 6f 77 53 65 74 2a  etInsert(RowSet*
70690 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50  , i64);.SQLITE_P
706a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
706b0 65 33 52 6f 77 53 65 74 54 65 73 74 28 52 6f 77  e3RowSetTest(Row
706c0 53 65 74 2a 2c 20 75 38 20 69 42 61 74 63 68 2c  Set*, u8 iBatch,
706d0 20 69 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52   i64);.SQLITE_PR
706e0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
706f0 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53  3RowSetNext(RowS
70700 65 74 2a 2c 20 69 36 34 2a 29 3b 0a 0a 53 51 4c  et*, i64*);..SQL
70710 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
70720 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 56 69   sqlite3CreateVi
70730 65 77 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a  ew(Parse*,Token*
70740 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53  ,Token*,Token*,S
70750 65 6c 65 63 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b  elect*,int,int);
70760 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
70770 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
70780 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
70790 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
707a0 54 41 42 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52  TABLE).SQLITE_PR
707b0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
707c0 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
707d0 4e 61 6d 65 73 28 50 61 72 73 65 2a 2c 54 61 62  Names(Parse*,Tab
707e0 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  le*);.#else.# de
707f0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 69 65 77  fine sqlite3View
70800 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 41  GetColumnNames(A
70810 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51  ,B) 0.#endif..SQ
70820 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70830 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62  d sqlite3DropTab
70840 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  le(Parse*, 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 76  SQLITE_PRIVATE v
70870 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
70880 65 54 61 62 6c 65 28 54 61 62 6c 65 2a 29 3b 0a  eTable(Table*);.
70890 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
708a0 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
708b0 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
708c0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41     void sqlite3A
708d0 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69  utoincrementBegi
708e0 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  n(Parse *pParse)
708f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70900 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41     void sqlite3A
70910 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28  utoincrementEnd(
70920 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b 0a  Parse *pParse);.
70930 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
70940 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d  qlite3Autoincrem
70950 65 6e 74 42 65 67 69 6e 28 58 29 0a 23 20 64 65  entBegin(X).# de
70960 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f  fine sqlite3Auto
70970 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 58 29 0a  incrementEnd(X).
70980 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52  #endif.SQLITE_PR
70990 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
709a0 65 33 49 6e 73 65 72 74 28 50 61 72 73 65 2a 2c  e3Insert(Parse*,
709b0 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c   SrcList*, ExprL
709c0 69 73 74 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 49  ist*, Select*, I
709d0 64 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51  dList*, int);.SQ
709e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
709f0 64 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41  d *sqlite3ArrayA
70a00 6c 6c 6f 63 61 74 65 28 73 71 6c 69 74 65 33 2a  llocate(sqlite3*
70a10 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 69  ,void*,int,int,i
70a20 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a  nt*,int*,int*);.
70a30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
70a40 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64  dList *sqlite3Id
70a50 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74  ListAppend(sqlit
70a60 65 33 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 54 6f  e3*, IdList*, To
70a70 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
70a80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
70a90 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c  3IdListIndex(IdL
70aa0 69 73 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ist*,const char*
70ab0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70ac0 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74  E SrcList *sqlit
70ad0 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65  e3SrcListEnlarge
70ae0 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69  (sqlite3*, SrcLi
70af0 73 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  st*, int, int);.
70b00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
70b10 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
70b20 72 63 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c  rcListAppend(sql
70b30 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 2c  ite3*, SrcList*,
70b40 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29   Token*, Token*)
70b50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70b60 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
70b70 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72  3SrcListAppendFr
70b80 6f 6d 54 65 72 6d 28 50 61 72 73 65 2a 2c 20 53  omTerm(Parse*, S
70b90 72 63 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c  rcList*, Token*,
70ba0 20 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20   Token*,.       
70bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
70bd0 6f 6b 65 6e 2a 2c 20 53 65 6c 65 63 74 2a 2c 20  oken*, Select*, 
70be0 45 78 70 72 2a 2c 20 49 64 4c 69 73 74 2a 29 3b  Expr*, IdList*);
70bf0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70c00 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
70c10 69 73 74 49 6e 64 65 78 65 64 42 79 28 50 61 72  istIndexedBy(Par
70c20 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c  se *, SrcList *,
70c30 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54   Token *);.SQLIT
70c40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
70c50 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f  lite3IndexedByLo
70c60 6f 6b 75 70 28 50 61 72 73 65 20 2a 2c 20 73 74  okup(Parse *, st
70c70 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
70c80 6d 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  m *);.SQLITE_PRI
70c90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70ca0 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69  3SrcListShiftJoi
70cb0 6e 54 79 70 65 28 53 72 63 4c 69 73 74 2a 29 3b  nType(SrcList*);
70cc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70cd0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
70ce0 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73  istAssignCursors
70cf0 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
70d00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70d10 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49  TE void sqlite3I
70d20 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69  dListDelete(sqli
70d30 74 65 33 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a  te3*, IdList*);.
70d40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70d50 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69  oid sqlite3SrcLi
70d60 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  stDelete(sqlite3
70d70 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51  *, SrcList*);.SQ
70d80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64  LITE_PRIVATE Ind
70d90 65 78 20 2a 73 71 6c 69 74 65 33 43 72 65 61 74  ex *sqlite3Creat
70da0 65 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 54 6f  eIndex(Parse*,To
70db0 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c  ken*,Token*,SrcL
70dc0 69 73 74 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69  ist*,ExprList*,i
70dd0 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20  nt,Token*,.     
70de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70df0 20 20 20 54 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20     Token*, int, 
70e00 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
70e10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70e20 33 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65  3DropIndex(Parse
70e30 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74  *, SrcList*, int
70e40 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70e50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 65 6c  E int sqlite3Sel
70e60 65 63 74 28 50 61 72 73 65 2a 2c 20 53 65 6c 65  ect(Parse*, Sele
70e70 63 74 2a 2c 20 53 65 6c 65 63 74 44 65 73 74 2a  ct*, SelectDest*
70e80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70e90 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65  E Select *sqlite
70ea0 33 53 65 6c 65 63 74 4e 65 77 28 50 61 72 73 65  3SelectNew(Parse
70eb0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 53 72 63 4c  *,ExprList*,SrcL
70ec0 69 73 74 2a 2c 45 78 70 72 2a 2c 45 78 70 72 4c  ist*,Expr*,ExprL
70ed0 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20 20  ist*,.          
70ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
70ef0 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69  xpr*,ExprList*,i
70f00 6e 74 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29 3b  nt,Expr*,Expr*);
70f10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70f20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65  void sqlite3Sele
70f30 63 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33  ctDelete(sqlite3
70f40 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
70f50 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
70f60 65 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  e *sqlite3SrcLis
70f70 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65 2a 2c 20  tLookup(Parse*, 
70f80 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54  SrcList*);.SQLIT
70f90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
70fa0 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28  lite3IsReadOnly(
70fb0 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20  Parse*, Table*, 
70fc0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
70fd0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70fe0 33 4f 70 65 6e 54 61 62 6c 65 28 50 61 72 73 65  3OpenTable(Parse
70ff0 2a 2c 20 69 6e 74 20 69 43 75 72 2c 20 69 6e 74  *, int iCur, int
71000 20 69 44 62 2c 20 54 61 62 6c 65 2a 2c 20 69 6e   iDb, Table*, in
71010 74 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  t);.#if defined(
71020 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50  SQLITE_ENABLE_UP
71030 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49  DATE_DELETE_LIMI
71040 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  T) && !defined(S
71050 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
71060 45 52 59 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  ERY).SQLITE_PRIV
71070 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
71080 33 4c 69 6d 69 74 57 68 65 72 65 28 50 61 72 73  3LimitWhere(Pars
71090 65 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20  e *, SrcList *, 
710a0 45 78 70 72 20 2a 2c 20 45 78 70 72 4c 69 73 74  Expr *, ExprList
710b0 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72   *, Expr *, Expr
710c0 20 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e   *, char *);.#en
710d0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
710e0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
710f0 65 6c 65 74 65 46 72 6f 6d 28 50 61 72 73 65 2a  eleteFrom(Parse*
71100 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72  , SrcList*, Expr
71110 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71120 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
71130 70 64 61 74 65 28 50 61 72 73 65 2a 2c 20 53 72  pdate(Parse*, Sr
71140 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73 74  cList*, ExprList
71150 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a  *, Expr*, int);.
71160 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 57  SQLITE_PRIVATE W
71170 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74 65  hereInfo *sqlite
71180 33 57 68 65 72 65 42 65 67 69 6e 28 50 61 72 73  3WhereBegin(Pars
71190 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78  e*, SrcList*, Ex
711a0 70 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2a 2c  pr*, ExprList**,
711b0 20 75 31 36 29 3b 0a 53 51 4c 49 54 45 5f 50 52   u16);.SQLITE_PR
711c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
711d0 65 33 57 68 65 72 65 45 6e 64 28 57 68 65 72 65  e3WhereEnd(Where
711e0 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Info*);.SQLITE_P
711f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
71200 65 33 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c  e3ExprCodeGetCol
71210 75 6d 6e 28 50 61 72 73 65 2a 2c 20 54 61 62 6c  umn(Parse*, Tabl
71220 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  e*, int, int, in
71230 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
71240 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71250 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
71260 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Parse*, int, in
71270 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
71280 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71290 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79  ite3ExprCodeCopy
712a0 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e  (Parse*, int, in
712b0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
712c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
712d0 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f  ite3ExprCacheSto
712e0 72 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20  re(Parse*, int, 
712f0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
71300 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
71310 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
71320 75 73 68 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  ush(Parse*);.SQL
71330 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
71340 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
71350 65 50 6f 70 28 50 61 72 73 65 2a 2c 20 69 6e 74  ePop(Parse*, int
71360 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71370 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
71380 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61  prCacheRemove(Pa
71390 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  rse*, int);.SQLI
713a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
713b0 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
713c0 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53  Clear(Parse*);.S
713d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
713e0 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  id sqlite3ExprCa
713f0 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
71400 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69  e(Parse*, int, i
71410 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
71420 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71430 45 78 70 72 48 61 72 64 43 6f 70 79 28 50 61 72  ExprHardCopy(Par
71440 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  se*,int,int);.SQ
71450 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
71460 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
71470 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20  (Parse*, Expr*, 
71480 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71490 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
714a0 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72  ExprCodeTemp(Par
714b0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2a  se*, Expr*, int*
714c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
714d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
714e0 72 43 6f 64 65 54 61 72 67 65 74 28 50 61 72 73  rCodeTarget(Pars
714f0 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b  e*, Expr*, int);
71500 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71510 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
71520 6f 64 65 41 6e 64 43 61 63 68 65 28 50 61 72 73  odeAndCache(Pars
71530 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b  e*, Expr*, int);
71540 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71550 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
71560 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 50 61  CodeConstants(Pa
71570 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  rse*, Expr*);.SQ
71580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
71590 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
715a0 45 78 70 72 4c 69 73 74 28 50 61 72 73 65 2a 2c  ExprList(Parse*,
715b0 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c   ExprList*, int,
715c0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
715d0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
715e0 65 33 45 78 70 72 49 66 54 72 75 65 28 50 61 72  e3ExprIfTrue(Par
715f0 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c  se*, Expr*, int,
71600 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
71610 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
71620 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 50 61  e3ExprIfFalse(Pa
71630 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e 74  rse*, Expr*, int
71640 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
71650 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
71660 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 73  lite3FindTable(s
71670 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
71680 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  ar*, const char*
71690 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
716a0 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33  E Table *sqlite3
716b0 4c 6f 63 61 74 65 54 61 62 6c 65 28 50 61 72 73  LocateTable(Pars
716c0 65 2a 2c 69 6e 74 20 69 73 56 69 65 77 2c 63 6f  e*,int isView,co
716d0 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74  nst char*, const
716e0 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
716f0 50 52 49 56 41 54 45 20 49 6e 64 65 78 20 2a 73  PRIVATE Index *s
71700 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
71710 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
71720 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  har*, const char
71730 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71740 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
71750 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61  nlinkAndDeleteTa
71760 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  ble(sqlite3*,int
71770 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53  ,const char*);.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 55 6e 6c 69 6e 6b  id sqlite3Unlink
717a0 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 73  AndDeleteIndex(s
717b0 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73  qlite3*,int,cons
717c0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
717d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
717e0 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73  lite3Vacuum(Pars
717f0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
71800 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
71810 75 6e 56 61 63 75 75 6d 28 63 68 61 72 2a 2a 2c  unVacuum(char**,
71820 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49   sqlite3*);.SQLI
71830 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
71840 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d  *sqlite3NameFrom
71850 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33 2a 2c 20  Token(sqlite3*, 
71860 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
71870 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
71880 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 45  te3ExprCompare(E
71890 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  xpr*, Expr*);.SQ
718a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
718b0 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61  d sqlite3ExprAna
718c0 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e  lyzeAggregates(N
718d0 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70  ameContext*, Exp
718e0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
718f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71900 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69  ExprAnalyzeAggLi
71910 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c  st(NameContext*,
71920 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49  ExprList*);.SQLI
71930 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 20  TE_PRIVATE Vdbe 
71940 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28  *sqlite3GetVdbe(
71950 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
71960 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71970 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74  ite3PrngSaveStat
71980 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f  e(void);.SQLITE_
71990 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
719a0 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53  ite3PrngRestoreS
719b0 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49  tate(void);.SQLI
719c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
719d0 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74  sqlite3PrngReset
719e0 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c  State(void);.SQL
719f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
71a00 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b   sqlite3Rollback
71a10 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  All(sqlite3*);.S
71a20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71a30 69 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65  id sqlite3CodeVe
71a40 72 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 65  rifySchema(Parse
71a50 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
71a60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71a70 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63  ite3BeginTransac
71a80 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 69 6e 74  tion(Parse*, int
71a90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71aa0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
71ab0 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28  mmitTransaction(
71ac0 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Parse*);.SQLITE_
71ad0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71ae0 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e  ite3RollbackTran
71af0 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b  saction(Parse*);
71b00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71b10 76 6f 69 64 20 73 71 6c 69 74 65 33 53 61 76 65  void sqlite3Save
71b20 70 6f 69 6e 74 28 50 61 72 73 65 2a 2c 20 69 6e  point(Parse*, in
71b30 74 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  t, Token*);.SQLI
71b40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
71b50 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65  sqlite3CloseSave
71b60 70 6f 69 6e 74 73 28 73 71 6c 69 74 65 33 20 2a  points(sqlite3 *
71b70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71b80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
71b90 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70 72  rIsConstant(Expr
71ba0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71bb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
71bc0 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a  prIsConstantNotJ
71bd0 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49  oin(Expr*);.SQLI
71be0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
71bf0 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
71c00 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 45  tantOrFunction(E
71c10 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
71c20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
71c30 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45  3ExprIsInteger(E
71c40 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c  xpr*, int*);.SQL
71c50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
71c60 73 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65  sqlite3ExprCanBe
71c70 4e 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70 72 2a  Null(const Expr*
71c80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71c90 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
71ca0 70 72 43 6f 64 65 49 73 4e 75 6c 6c 4a 75 6d 70  prCodeIsNullJump
71cb0 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20 45 78  (Vdbe*, const Ex
71cc0 70 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  pr*, int, int);.
71cd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
71ce0 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 4e 65  nt sqlite3ExprNe
71cf0 65 64 73 4e 6f 41 66 66 69 6e 69 74 79 43 68 61  edsNoAffinityCha
71d00 6e 67 65 28 63 6f 6e 73 74 20 45 78 70 72 2a 2c  nge(const Expr*,
71d10 20 63 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50   char);.SQLITE_P
71d20 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
71d30 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20  e3IsRowid(const 
71d40 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
71d50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71d60 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65  te3GenerateRowDe
71d70 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54 61 62  lete(Parse*, Tab
71d80 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69  le*, int, int, i
71d90 6e 74 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69  nt, Trigger *, i
71da0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
71db0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71dc0 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78  GenerateRowIndex
71dd0 44 65 6c 65 74 65 28 50 61 72 73 65 2a 2c 20 54  Delete(Parse*, T
71de0 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2a  able*, int, int*
71df0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71e00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 6e  E int sqlite3Gen
71e10 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 50 61  erateIndexKey(Pa
71e20 72 73 65 2a 2c 20 49 6e 64 65 78 2a 2c 20 69 6e  rse*, Index*, in
71e30 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
71e40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71e50 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
71e60 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b  eConstraintCheck
71e70 73 28 50 61 72 73 65 2a 2c 54 61 62 6c 65 2a 2c  s(Parse*,Table*,
71e80 69 6e 74 2c 69 6e 74 2c 0a 20 20 20 20 20 20 20  int,int,.       
71e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
71eb0 74 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69  t*,int,int,int,i
71ec0 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  nt,int*);.SQLITE
71ed0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
71ee0 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73  lite3CompleteIns
71ef0 65 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20 54  ertion(Parse*, T
71f00 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  able*, int, int,
71f10 20 69 6e 74 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c   int*, int, int,
71f20 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
71f30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
71f40 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64  3OpenTableAndInd
71f50 69 63 65 73 28 50 61 72 73 65 2a 2c 20 54 61 62  ices(Parse*, Tab
71f60 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  le*, int, int);.
71f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
71f80 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e  oid sqlite3Begin
71f90 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50  WriteOperation(P
71fa0 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  arse*, int, int)
71fb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71fc0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 75 6c   void sqlite3Mul
71fd0 74 69 57 72 69 74 65 28 50 61 72 73 65 2a 29 3b  tiWrite(Parse*);
71fe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71ff0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 79 41  void sqlite3MayA
72000 62 6f 72 74 28 50 61 72 73 65 2a 29 3b 0a 53 51  bort(Parse*);.SQ
72010 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
72020 64 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e  d sqlite3HaltCon
72030 73 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20  straint(Parse*, 
72040 69 6e 74 2c 20 63 68 61 72 2a 2c 20 69 6e 74 29  int, char*, int)
72050 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72060 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
72070 70 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 45  prDup(sqlite3*,E
72080 78 70 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  xpr*,int);.SQLIT
72090 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69  E_PRIVATE ExprLi
720a0 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c  st *sqlite3ExprL
720b0 69 73 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  istDup(sqlite3*,
720c0 45 78 70 72 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a  ExprList*,int);.
720d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
720e0 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
720f0 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65  rcListDup(sqlite
72100 33 2a 2c 53 72 63 4c 69 73 74 2a 2c 69 6e 74 29  3*,SrcList*,int)
72110 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72120 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65 33   IdList *sqlite3
72130 49 64 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65  IdListDup(sqlite
72140 33 2a 2c 49 64 4c 69 73 74 2a 29 3b 0a 53 51 4c  3*,IdList*);.SQL
72150 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65  ITE_PRIVATE Sele
72160 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63  ct *sqlite3Selec
72170 74 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 65  tDup(sqlite3*,Se
72180 6c 65 63 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  lect*,int);.SQLI
72190 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
721a0 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e  sqlite3FuncDefIn
721b0 73 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68  sert(FuncDefHash
721c0 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51  *, FuncDef*);.SQ
721d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e  LITE_PRIVATE Fun
721e0 63 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e  cDef *sqlite3Fin
721f0 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  dFunction(sqlite
72200 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  3*,const char*,i
72210 6e 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a  nt,int,u8,int);.
72220 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
72230 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
72240 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69  terBuiltinFuncti
72250 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  ons(sqlite3*);.S
72260 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
72270 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  id sqlite3Regist
72280 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69  erDateTimeFuncti
72290 6f 6e 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ons(void);.SQLIT
722a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
722b0 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c  qlite3RegisterGl
722c0 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f  obalFunctions(vo
722d0 69 64 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  id);.#ifdef SQLI
722e0 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
722f0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
72300 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71  lite3SafetyOn(sq
72310 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
72320 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
72330 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73  lite3SafetyOff(s
72340 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a  qlite3*);.#else.
72350 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
72360 53 61 66 65 74 79 4f 6e 28 41 29 20 30 0a 23 20  SafetyOn(A) 0.# 
72370 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61  define sqlite3Sa
72380 66 65 74 79 4f 66 66 28 41 29 20 30 0a 23 65 6e  fetyOff(A) 0.#en
72390 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
723a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61  TE int sqlite3Sa
723b0 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69  fetyCheckOk(sqli
723c0 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
723d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
723e0 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
723f0 4f 72 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a  OrOk(sqlite3*);.
72400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
72410 6f 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67  oid sqlite3Chang
72420 65 43 6f 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20  eCookie(Parse*, 
72430 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69  int);..#if !defi
72440 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
72450 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65  VIEW) && !define
72460 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
72470 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52  IGGER).SQLITE_PR
72480 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
72490 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65  e3MaterializeVie
724a0 77 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  w(Parse*, Table*
724b0 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23  , Expr*, int);.#
724c0 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
724d0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
724e0 45 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ER.SQLITE_PRIVAT
724f0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
72500 42 65 67 69 6e 54 72 69 67 67 65 72 28 50 61 72  BeginTrigger(Par
72510 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65  se*, Token*,Toke
72520 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73  n*,int,int,IdLis
72530 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20  t*,SrcList*,.   
72540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72550 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 69 6e          Expr*,in
72560 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
72570 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
72580 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
72590 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67  ger(Parse*, Trig
725a0 67 65 72 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a  gerStep*, Token*
725b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
725c0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
725d0 44 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73  DropTrigger(Pars
725e0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e  e*, SrcList*, in
725f0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
72600 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
72610 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28  3DropTriggerPtr(
72620 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 2a  Parse*, Trigger*
72630 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72640 45 20 20 20 54 72 69 67 67 65 72 20 2a 73 71 6c  E   Trigger *sql
72650 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73  ite3TriggersExis
72660 74 28 50 61 72 73 65 20 2a 2c 20 54 61 62 6c 65  t(Parse *, Table
72670 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74  *, int, ExprList
72680 2a 2c 20 69 6e 74 20 2a 70 4d 61 73 6b 29 3b 0a  *, int *pMask);.
72690 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
726a0 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65   Trigger *sqlite
726b0 33 54 72 69 67 67 65 72 4c 69 73 74 28 50 61 72  3TriggerList(Par
726c0 73 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a  se *, Table *);.
726d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
726e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
726f0 65 52 6f 77 54 72 69 67 67 65 72 28 50 61 72 73  eRowTrigger(Pars
72700 65 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 69  e*, Trigger *, i
72710 6e 74 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69  nt, ExprList*, i
72720 6e 74 2c 20 54 61 62 6c 65 20 2a 2c 0a 20 20 20  nt, Table *,.   
72730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72740 20 20 20 20 20 20 20 20 20 69 6e 74 2c 20 69 6e           int, in
72750 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
72760 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
72770 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
72780 67 67 65 72 44 69 72 65 63 74 28 50 61 72 73 65  ggerDirect(Parse
72790 20 2a 2c 20 54 72 69 67 67 65 72 20 2a 2c 20 54   *, Trigger *, T
727a0 61 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74  able *, int, int
727b0 2c 20 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 73  , int);.  void s
727c0 71 6c 69 74 65 56 69 65 77 54 72 69 67 67 65 72  qliteViewTrigger
727d0 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  s(Parse*, Table*
727e0 2c 20 45 78 70 72 2a 2c 20 69 6e 74 2c 20 45 78  , Expr*, int, Ex
727f0 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  prList*);.SQLITE
72800 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
72810 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
72820 67 67 65 72 53 74 65 70 28 73 71 6c 69 74 65 33  ggerStep(sqlite3
72830 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70 2a 29  *, TriggerStep*)
72840 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72850 20 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a     TriggerStep *
72860 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65  sqlite3TriggerSe
72870 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33  lectStep(sqlite3
72880 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49  *,Select*);.SQLI
72890 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69  TE_PRIVATE   Tri
728a0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
728b0 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74  3TriggerInsertSt
728c0 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65  ep(sqlite3*,Toke
728d0 6e 2a 2c 20 49 64 4c 69 73 74 2a 2c 0a 20 20 20  n*, IdList*,.   
728e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
728f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72900 20 20 20 20 20 45 78 70 72 4c 69 73 74 2a 2c 53       ExprList*,S
72910 65 6c 65 63 74 2a 2c 75 38 29 3b 0a 53 51 4c 49  elect*,u8);.SQLI
72920 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69  TE_PRIVATE   Tri
72930 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
72940 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74  3TriggerUpdateSt
72950 65 70 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65  ep(sqlite3*,Toke
72960 6e 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 45 78  n*,ExprList*, Ex
72970 70 72 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45  pr*, u8);.SQLITE
72980 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
72990 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
729a0 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70  riggerDeleteStep
729b0 28 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a  (sqlite3*,Token*
729c0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
729d0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
729e0 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
729f0 67 67 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54  gger(sqlite3*, T
72a00 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  rigger*);.SQLITE
72a10 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
72a20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64  sqlite3UnlinkAnd
72a30 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71  DeleteTrigger(sq
72a40 6c 69 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74  lite3*,int,const
72a50 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
72a60 50 52 49 56 41 54 45 20 20 20 75 33 32 20 73 71  PRIVATE   u32 sq
72a70 6c 69 74 65 33 54 72 69 67 67 65 72 43 6f 6c 6d  lite3TriggerColm
72a80 61 73 6b 28 50 61 72 73 65 2a 2c 54 72 69 67 67  ask(Parse*,Trigg
72a90 65 72 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e  er*,ExprList*,in
72aa0 74 2c 69 6e 74 2c 54 61 62 6c 65 2a 2c 69 6e 74  t,int,Table*,int
72ab0 29 3b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  );.# define sqli
72ac0 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
72ad0 28 70 29 20 28 28 70 29 2d 3e 70 54 6f 70 6c 65  (p) ((p)->pTople
72ae0 76 65 6c 20 3f 20 28 70 29 2d 3e 70 54 6f 70 6c  vel ? (p)->pTopl
72af0 65 76 65 6c 20 3a 20 28 70 29 29 0a 23 65 6c 73  evel : (p)).#els
72b00 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  e.# define sqlit
72b10 65 33 54 72 69 67 67 65 72 73 45 78 69 73 74 28  e3TriggersExist(
72b20 42 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23 20 64  B,C,D,E,F) 0.# d
72b30 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44 65 6c  efine sqlite3Del
72b40 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 29 0a  eteTrigger(A,B).
72b50 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
72b60 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 41  DropTriggerPtr(A
72b70 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ,B).# define sql
72b80 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c  ite3UnlinkAndDel
72b90 65 74 65 54 72 69 67 67 65 72 28 41 2c 42 2c 43  eteTrigger(A,B,C
72ba0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
72bb0 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
72bc0 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47 2c 48  (A,B,C,D,E,F,G,H
72bd0 2c 49 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ,I).# define sql
72be0 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
72bf0 65 72 44 69 72 65 63 74 28 41 2c 42 2c 43 2c 44  erDirect(A,B,C,D
72c00 2c 45 2c 46 29 0a 23 20 64 65 66 69 6e 65 20 73  ,E,F).# define s
72c10 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73  qlite3TriggerLis
72c20 74 28 58 2c 20 59 29 20 30 0a 23 20 64 65 66 69  t(X, Y) 0.# defi
72c30 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 54  ne sqlite3ParseT
72c40 6f 70 6c 65 76 65 6c 28 70 29 20 70 0a 23 20 64  oplevel(p) p.# d
72c50 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69  efine sqlite3Tri
72c60 67 67 65 72 43 6f 6c 6d 61 73 6b 28 41 2c 42 2c  ggerColmask(A,B,
72c70 43 2c 44 2c 45 2c 46 2c 47 29 20 30 0a 23 65 6e  C,D,E,F,G) 0.#en
72c80 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  dif..SQLITE_PRIV
72c90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a  ATE int sqlite3J
72ca0 6f 69 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20  oinType(Parse*, 
72cb0 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20  Token*, Token*, 
72cc0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
72cd0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
72ce0 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67  ite3CreateForeig
72cf0 6e 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70  nKey(Parse*, Exp
72d00 72 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20  rList*, Token*, 
72d10 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b  ExprList*, int);
72d20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72d30 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 65  void sqlite3Defe
72d40 72 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73  rForeignKey(Pars
72d50 65 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65  e*, int);.#ifnde
72d60 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
72d70 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49  THORIZATION.SQLI
72d80 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
72d90 64 20 73 71 6c 69 74 65 33 41 75 74 68 52 65 61  d sqlite3AuthRea
72da0 64 28 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53  d(Parse*,Expr*,S
72db0 63 68 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29  chema*,SrcList*)
72dc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72dd0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75     int sqlite3Au
72de0 74 68 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69  thCheck(Parse*,i
72df0 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  nt, const char*,
72e00 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f   const char*, co
72e10 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
72e20 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
72e30 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e  d sqlite3AuthCon
72e40 74 65 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c  textPush(Parse*,
72e50 20 41 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63   AuthContext*, c
72e60 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
72e70 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
72e80 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f  id sqlite3AuthCo
72e90 6e 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e  ntextPop(AuthCon
72ea0 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  text*);.SQLITE_P
72eb0 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
72ec0 69 74 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28  ite3AuthReadCol(
72ed0 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68  Parse*, const ch
72ee0 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  ar *, const char
72ef0 20 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a   *, int);.#else.
72f00 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
72f10 41 75 74 68 52 65 61 64 28 61 2c 62 2c 63 2c 64  AuthRead(a,b,c,d
72f20 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
72f30 65 33 41 75 74 68 43 68 65 63 6b 28 61 2c 62 2c  e3AuthCheck(a,b,
72f40 63 2c 64 2c 65 29 20 20 20 20 53 51 4c 49 54 45  c,d,e)    SQLITE
72f50 5f 4f 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  _OK.# define sql
72f60 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
72f70 75 73 68 28 61 2c 62 2c 63 29 0a 23 20 64 65 66  ush(a,b,c).# def
72f80 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43  ine sqlite3AuthC
72f90 6f 6e 74 65 78 74 50 6f 70 28 61 29 20 20 28 28  ontextPop(a)  ((
72fa0 76 6f 69 64 29 28 61 29 29 0a 23 65 6e 64 69 66  void)(a)).#endif
72fb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72fc0 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74 61  void sqlite3Atta
72fd0 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a  ch(Parse*, Expr*
72fe0 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b  , Expr*, Expr*);
72ff0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73000 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 74 61  void sqlite3Deta
73010 63 68 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a  ch(Parse*, Expr*
73020 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73030 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
73040 65 65 46 61 63 74 6f 72 79 28 73 71 6c 69 74 65  eeFactory(sqlite
73050 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
73060 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20  r *zFilename,.  
73070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
73080 20 20 20 20 20 69 6e 74 20 6f 6d 69 74 4a 6f 75       int omitJou
73090 72 6e 61 6c 2c 20 69 6e 74 20 6e 43 61 63 68 65  rnal, int nCache
730a0 2c 20 69 6e 74 20 66 6c 61 67 73 2c 20 42 74 72  , int flags, Btr
730b0 65 65 20 2a 2a 70 70 42 74 72 65 65 29 3b 0a 53  ee **ppBtree);.S
730c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
730d0 74 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74  t sqlite3FixInit
730e0 28 44 62 46 69 78 65 72 2a 2c 20 50 61 72 73 65  (DbFixer*, Parse
730f0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  *, int, const ch
73100 61 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65 6e  ar*, const Token
73110 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
73120 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
73130 78 53 72 63 4c 69 73 74 28 44 62 46 69 78 65 72  xSrcList(DbFixer
73140 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51  *, SrcList*);.SQ
73150 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73160 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63   sqlite3FixSelec
73170 74 28 44 62 46 69 78 65 72 2a 2c 20 53 65 6c 65  t(DbFixer*, Sele
73180 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ct*);.SQLITE_PRI
73190 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
731a0 46 69 78 45 78 70 72 28 44 62 46 69 78 65 72 2a  FixExpr(DbFixer*
731b0 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
731c0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
731d0 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28  ite3FixExprList(
731e0 44 62 46 69 78 65 72 2a 2c 20 45 78 70 72 4c 69  DbFixer*, ExprLi
731f0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
73200 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
73210 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28 44  FixTriggerStep(D
73220 62 46 69 78 65 72 2a 2c 20 54 72 69 67 67 65 72  bFixer*, Trigger
73230 53 74 65 70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Step*);.SQLITE_P
73240 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
73250 65 33 41 74 6f 46 28 63 6f 6e 73 74 20 63 68 61  e3AtoF(const cha
73260 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a  r *z, double*);.
73270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
73280 6e 74 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74  nt sqlite3GetInt
73290 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  32(const char *,
732a0 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   int*);.SQLITE_P
732b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
732c0 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 63  e3FitsIn64Bits(c
732d0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
732e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
732f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66  E int sqlite3Utf
73300 31 36 42 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20  16ByteLen(const 
73310 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74  void *pData, int
73320 20 6e 43 68 61 72 29 3b 0a 53 51 4c 49 54 45 5f   nChar);.SQLITE_
73330 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
73340 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 63  te3Utf8CharLen(c
73350 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
73360 2c 20 69 6e 74 20 6e 42 79 74 65 29 3b 0a 53 51  , int nByte);.SQ
73370 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73380 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
73390 28 63 6f 6e 73 74 20 75 38 2a 2c 20 63 6f 6e 73  (const u8*, cons
733a0 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t u8**);../*.** 
733b0 52 6f 75 74 69 6e 65 73 20 74 6f 20 72 65 61 64  Routines to read
733c0 20 61 6e 64 20 77 72 69 74 65 20 76 61 72 69 61   and write varia
733d0 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
733e0 65 72 73 2e 20 20 54 68 65 73 65 20 75 73 65 64  ers.  These used
733f0 20 74 6f 0a 2a 2a 20 62 65 20 64 65 66 69 6e 65   to.** be define
73400 64 20 6c 6f 63 61 6c 6c 79 2c 20 62 75 74 20 6e  d locally, but n
73410 6f 77 20 77 65 20 75 73 65 20 74 68 65 20 76 61  ow we use the va
73420 72 69 6e 74 20 72 6f 75 74 69 6e 65 73 20 69 6e  rint routines in
73430 20 74 68 65 20 75 74 69 6c 2e 63 0a 2a 2a 20 66   the util.c.** f
73440 69 6c 65 2e 20 20 43 6f 64 65 20 73 68 6f 75 6c  ile.  Code shoul
73450 64 20 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20  d use the MACRO 
73460 66 6f 72 6d 73 20 62 65 6c 6f 77 2c 20 61 73 20  forms below, as 
73470 74 68 65 20 56 61 72 69 6e 74 33 32 20 76 65 72  the Varint32 ver
73480 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 63 6f 64  sions.** are cod
73490 65 64 20 74 6f 20 61 73 73 75 6d 65 20 74 68 65  ed to assume the
734a0 20 73 69 6e 67 6c 65 20 62 79 74 65 20 63 61 73   single byte cas
734b0 65 20 69 73 20 61 6c 72 65 61 64 79 20 68 61 6e  e is already han
734c0 64 6c 65 64 20 28 77 68 69 63 68 20 0a 2a 2a 20  dled (which .** 
734d0 74 68 65 20 4d 41 43 52 4f 20 66 6f 72 6d 20 64  the MACRO form d
734e0 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oes)..*/.SQLITE_
734f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
73500 74 65 33 50 75 74 56 61 72 69 6e 74 28 75 6e 73  te3PutVarint(uns
73510 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 36 34  igned char*, u64
73520 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73530 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74  E int sqlite3Put
73540 56 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65  Varint32(unsigne
73550 64 20 63 68 61 72 2a 2c 20 75 33 32 29 3b 0a 53  d char*, u32);.S
73560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 38  QLITE_PRIVATE u8
73570 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
73580 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  t(const unsigned
73590 20 63 68 61 72 20 2a 2c 20 75 36 34 20 2a 29 3b   char *, u64 *);
735a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
735b0 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61 72  u8 sqlite3GetVar
735c0 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73 69  int32(const unsi
735d0 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 33 32  gned char *, u32
735e0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
735f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
73600 61 72 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 3b  arintLen(u64 v);
73610 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61 64  ../*.** The head
73620 65 72 20 6f 66 20 61 20 72 65 63 6f 72 64 20 63  er of a record c
73630 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 65 71  onsists of a seq
73640 75 65 6e 63 65 20 76 61 72 69 61 62 6c 65 2d 6c  uence variable-l
73650 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e 0a  ength integers..
73660 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72  ** These integer
73670 73 20 61 72 65 20 61 6c 6d 6f 73 74 20 61 6c 77  s are almost alw
73680 61 79 73 20 73 6d 61 6c 6c 20 61 6e 64 20 61 72  ays small and ar
73690 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 73  e encoded as a s
736a0 69 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 54  ingle byte..** T
736b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
736c0 72 6f 73 20 74 61 6b 65 20 61 64 76 61 6e 74 61  ros take advanta
736d0 67 65 20 74 68 69 73 20 66 61 63 74 20 74 6f 20  ge this fact to 
736e0 70 72 6f 76 69 64 65 20 61 20 66 61 73 74 20 65  provide a fast e
736f0 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 64 65 63  ncode.** and dec
73700 6f 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67  ode of the integ
73710 65 72 73 20 69 6e 20 61 20 72 65 63 6f 72 64 20  ers in a record 
73720 68 65 61 64 65 72 2e 20 20 49 74 20 69 73 20 66  header.  It is f
73730 61 73 74 65 72 20 66 6f 72 20 74 68 65 20 63 6f  aster for the co
73740 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65  mmon.** case whe
73750 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 69  re the integer i
73760 73 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e  s a single byte.
73770 20 20 49 74 20 69 73 20 61 20 6c 69 74 74 6c 65    It is a little
73780 20 73 6c 6f 77 65 72 20 77 68 65 6e 20 74 68 65   slower when the
73790 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 73 20 74  .** integer is t
737a0 77 6f 20 6f 72 20 6d 6f 72 65 20 62 79 74 65 73  wo or more bytes
737b0 2e 20 20 42 75 74 20 6f 76 65 72 61 6c 6c 20 69  .  But overall i
737c0 74 20 69 73 20 66 61 73 74 65 72 2e 0a 2a 2a 0a  t is faster..**.
737d0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
737e0 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65   expressions are
737f0 20 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a   equivalent:.**.
73800 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74  **     x = sqlit
73810 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 20 41  e3GetVarint32( A
73820 2c 20 26 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78  , &B );.**     x
73830 20 3d 20 73 71 6c 69 74 65 33 50 75 74 56 61 72   = sqlite3PutVar
73840 69 6e 74 33 32 28 20 41 2c 20 42 20 29 3b 0a 2a  int32( A, B );.*
73850 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 67 65 74  *.**     x = get
73860 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29  Varint32( A, B )
73870 3b 0a 2a 2a 20 20 20 20 20 78 20 3d 20 70 75 74  ;.**     x = put
73880 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20 29  Varint32( A, B )
73890 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ;.**.*/.#define 
738a0 67 65 74 56 61 72 69 6e 74 33 32 28 41 2c 42 29  getVarint32(A,B)
738b0 20 20 28 75 38 29 28 28 2a 28 41 29 3c 28 75 38    (u8)((*(A)<(u8
738c0 29 30 78 38 30 29 20 3f 20 28 28 42 29 20 3d 20  )0x80) ? ((B) = 
738d0 28 75 33 32 29 2a 28 41 29 29 2c 31 20 3a 20 73  (u32)*(A)),1 : s
738e0 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33  qlite3GetVarint3
738f0 32 28 28 41 29 2c 20 28 75 33 32 20 2a 29 26 28  2((A), (u32 *)&(
73900 42 29 29 29 0a 23 64 65 66 69 6e 65 20 70 75 74  B))).#define put
73910 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28  Varint32(A,B)  (
73920 75 38 29 28 28 28 75 33 32 29 28 42 29 3c 28 75  u8)(((u32)(B)<(u
73930 33 32 29 30 78 38 30 29 20 3f 20 28 2a 28 41 29  32)0x80) ? (*(A)
73940 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
73950 72 29 28 42 29 29 2c 31 20 3a 20 73 71 6c 69 74  r)(B)),1 : sqlit
73960 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 28 41  e3PutVarint32((A
73970 29 2c 20 28 42 29 29 29 0a 23 64 65 66 69 6e 65  ), (B))).#define
73980 20 67 65 74 56 61 72 69 6e 74 20 20 20 20 73 71   getVarint    sq
73990 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 0a 23  lite3GetVarint.#
739a0 64 65 66 69 6e 65 20 70 75 74 56 61 72 69 6e 74  define putVarint
739b0 20 20 20 20 73 71 6c 69 74 65 33 50 75 74 56 61      sqlite3PutVa
739c0 72 69 6e 74 0a 0a 0a 53 51 4c 49 54 45 5f 50 52  rint...SQLITE_PR
739d0 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
739e0 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66   *sqlite3IndexAf
739f0 66 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a  finityStr(Vdbe *
73a00 2c 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49  , Index *);.SQLI
73a10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
73a20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69  sqlite3TableAffi
73a30 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 2c 20  nityStr(Vdbe *, 
73a40 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Table *);.SQLITE
73a50 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
73a60 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69  lite3CompareAffi
73a70 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72  nity(Expr *pExpr
73a80 2c 20 63 68 61 72 20 61 66 66 32 29 3b 0a 53 51  , char aff2);.SQ
73a90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73aa0 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66   sqlite3IndexAff
73ab0 69 6e 69 74 79 4f 6b 28 45 78 70 72 20 2a 70 45  inityOk(Expr *pE
73ac0 78 70 72 2c 20 63 68 61 72 20 69 64 78 5f 61 66  xpr, char idx_af
73ad0 66 69 6e 69 74 79 29 3b 0a 53 51 4c 49 54 45 5f  finity);.SQLITE_
73ae0 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c  PRIVATE char sql
73af0 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
73b00 28 45 78 70 72 20 2a 70 45 78 70 72 29 3b 0a 53  (Expr *pExpr);.S
73b10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
73b20 74 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28  t sqlite3Atoi64(
73b30 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 36 34  const char*, i64
73b40 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
73b50 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
73b60 72 72 6f 72 28 73 71 6c 69 74 65 33 2a 2c 20 69  rror(sqlite3*, i
73b70 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  nt, const char*,
73b80 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ...);.SQLITE_PRI
73b90 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
73ba0 65 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69  e3HexToBlob(sqli
73bb0 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  te3*, const char
73bc0 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b 0a 53 51 4c   *z, int n);.SQL
73bd0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
73be0 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
73bf0 6d 65 28 50 61 72 73 65 20 2a 2c 20 54 6f 6b 65  me(Parse *, Toke
73c00 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f  n *, Token *, To
73c10 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  ken **);.SQLITE_
73c20 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
73c30 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74  ar *sqlite3ErrSt
73c40 72 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  r(int);.SQLITE_P
73c50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
73c60 65 33 52 65 61 64 53 63 68 65 6d 61 28 50 61 72  e3ReadSchema(Par
73c70 73 65 20 2a 70 50 61 72 73 65 29 3b 0a 53 51 4c  se *pParse);.SQL
73c80 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c  ITE_PRIVATE Coll
73c90 53 65 71 20 2a 73 71 6c 69 74 65 33 46 69 6e 64  Seq *sqlite3Find
73ca0 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a  CollSeq(sqlite3*
73cb0 2c 75 38 20 65 6e 63 2c 20 63 6f 6e 73 74 20 63  ,u8 enc, const c
73cc0 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  har*,int);.SQLIT
73cd0 45 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65  E_PRIVATE CollSe
73ce0 71 20 2a 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  q *sqlite3Locate
73cf0 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70  CollSeq(Parse *p
73d00 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
73d10 72 2a 7a 4e 61 6d 65 29 3b 0a 53 51 4c 49 54 45  r*zName);.SQLITE
73d20 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71  _PRIVATE CollSeq
73d30 20 2a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c   *sqlite3ExprCol
73d40 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61 72  lSeq(Parse *pPar
73d50 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29  se, Expr *pExpr)
73d60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73d70 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45 78   Expr *sqlite3Ex
73d80 70 72 53 65 74 43 6f 6c 6c 28 50 61 72 73 65 20  prSetColl(Parse 
73d90 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 2c  *pParse, Expr *,
73da0 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54   Token *);.SQLIT
73db0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
73dc0 6c 69 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65  lite3CheckCollSe
73dd0 71 28 50 61 72 73 65 20 2a 2c 20 43 6f 6c 6c 53  q(Parse *, CollS
73de0 65 71 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  eq *);.SQLITE_PR
73df0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
73e00 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65  3CheckObjectName
73e10 28 50 61 72 73 65 20 2a 2c 20 63 6f 6e 73 74 20  (Parse *, const 
73e20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  char *);.SQLITE_
73e30 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
73e40 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67  ite3VdbeSetChang
73e50 65 73 28 73 71 6c 69 74 65 33 20 2a 2c 20 69 6e  es(sqlite3 *, in
73e60 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  t);..SQLITE_PRIV
73e70 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ATE const void *
73e80 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
73e90 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c  (sqlite3_value*,
73ea0 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   u8);.SQLITE_PRI
73eb0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
73ec0 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74  ValueBytes(sqlit
73ed0 65 33 5f 76 61 6c 75 65 2a 2c 20 75 38 29 3b 0a  e3_value*, u8);.
73ee0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73ef0 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65  oid sqlite3Value
73f00 53 65 74 53 74 72 28 73 71 6c 69 74 65 33 5f 76  SetStr(sqlite3_v
73f10 61 6c 75 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73  alue*, int, cons
73f20 74 20 76 6f 69 64 20 2a 2c 75 38 2c 20 0a 20 20  t void *,u8, .  
73f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
73f40 20 20 20 20 20 20 76 6f 69 64 28 2a 29 28 76 6f        void(*)(vo
73f50 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  id*));.SQLITE_PR
73f60 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
73f70 65 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69  e3ValueFree(sqli
73f80 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
73f90 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
73fa0 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74  te3_value *sqlit
73fb0 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69 74  e3ValueNew(sqlit
73fc0 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  e3 *);.SQLITE_PR
73fd0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
73fe0 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69  te3Utf16to8(sqli
73ff0 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  te3 *, const voi
74000 64 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 64 65 66  d*, int);.#ifdef
74010 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
74020 54 41 54 32 0a 53 51 4c 49 54 45 5f 50 52 49 56  TAT2.SQLITE_PRIV
74030 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
74040 33 55 74 66 38 74 6f 31 36 28 73 71 6c 69 74 65  3Utf8to16(sqlite
74050 33 20 2a 2c 20 75 38 2c 20 63 68 61 72 20 2a 2c  3 *, u8, char *,
74060 20 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 23 65   int, int *);.#e
74070 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
74080 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
74090 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73 71 6c  alueFromExpr(sql
740a0 69 74 65 33 20 2a 2c 20 45 78 70 72 20 2a 2c 20  ite3 *, Expr *, 
740b0 75 38 2c 20 75 38 2c 20 73 71 6c 69 74 65 33 5f  u8, u8, sqlite3_
740c0 76 61 6c 75 65 20 2a 2a 29 3b 0a 53 51 4c 49 54  value **);.SQLIT
740d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
740e0 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79  qlite3ValueApply
740f0 41 66 66 69 6e 69 74 79 28 73 71 6c 69 74 65 33  Affinity(sqlite3
74100 5f 76 61 6c 75 65 20 2a 2c 20 75 38 2c 20 75 38  _value *, u8, u8
74110 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
74120 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 53  E_AMALGAMATION.S
74130 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
74140 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
74150 72 20 73 71 6c 69 74 65 33 4f 70 63 6f 64 65 50  r sqlite3OpcodeP
74160 72 6f 70 65 72 74 79 5b 5d 3b 0a 53 51 4c 49 54  roperty[];.SQLIT
74170 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
74180 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71  unsigned char sq
74190 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
741a0 72 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r[];.SQLITE_PRIV
741b0 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ATE const unsign
741c0 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 43  ed char sqlite3C
741d0 74 79 70 65 4d 61 70 5b 5d 3b 0a 53 51 4c 49 54  typeMap[];.SQLIT
741e0 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45  E_PRIVATE SQLITE
741f0 5f 57 53 44 20 73 74 72 75 63 74 20 53 71 6c 69  _WSD struct Sqli
74200 74 65 33 43 6f 6e 66 69 67 20 73 71 6c 69 74 65  te3Config sqlite
74210 33 43 6f 6e 66 69 67 3b 0a 53 51 4c 49 54 45 5f  3Config;.SQLITE_
74220 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57  PRIVATE SQLITE_W
74230 53 44 20 46 75 6e 63 44 65 66 48 61 73 68 20 73  SD FuncDefHash s
74240 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63  qlite3GlobalFunc
74250 74 69 6f 6e 73 3b 0a 53 51 4c 49 54 45 5f 50 52  tions;.SQLITE_PR
74260 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
74270 33 50 65 6e 64 69 6e 67 42 79 74 65 3b 0a 23 65  3PendingByte;.#e
74280 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
74290 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
742a0 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28 44 62  RootPageMoved(Db
742b0 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
742c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
742d0 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78  d sqlite3Reindex
742e0 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c  (Parse*, Token*,
742f0 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
74300 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74310 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63 74 69  lite3AlterFuncti
74320 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  ons(sqlite3*);.S
74330 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74340 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52  id sqlite3AlterR
74350 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 72 73 65  enameTable(Parse
74360 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f 6b  *, SrcList*, Tok
74370 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
74380 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
74390 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75  GetToken(const u
743a0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c 20  nsigned char *, 
743b0 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  int *);.SQLITE_P
743c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
743d0 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 50  te3NestedParse(P
743e0 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  arse*, const cha
743f0 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45  r*, ...);.SQLITE
74400 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74410 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
74420 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73 71  redStatements(sq
74430 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
74440 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
74450 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74  te3CodeSubselect
74460 28 50 61 72 73 65 20 2a 2c 20 45 78 70 72 20 2a  (Parse *, Expr *
74470 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
74480 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
74490 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72   sqlite3SelectPr
744a0 65 70 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63  ep(Parse*, Selec
744b0 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a  t*, NameContext*
744c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
744d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73  E int sqlite3Res
744e0 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 4e 61  olveExprNames(Na
744f0 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45 78 70 72  meContext*, Expr
74500 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74510 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
74520 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65  esolveSelectName
74530 73 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74  s(Parse*, Select
74540 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 29  *, NameContext*)
74550 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74560 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f   int sqlite3Reso
74570 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28  lveOrderGroupBy(
74580 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c  Parse*, Select*,
74590 20 45 78 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73   ExprList*, cons
745a0 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
745b0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
745c0 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75  lite3ColumnDefau
745d0 6c 74 28 56 64 62 65 20 2a 2c 20 54 61 62 6c 65  lt(Vdbe *, Table
745e0 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53   *, int, int);.S
745f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74600 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46  id sqlite3AlterF
74610 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50  inishAddColumn(P
74620 61 72 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29  arse *, Token *)
74630 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74640 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74   void sqlite3Alt
74650 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e  erBeginAddColumn
74660 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73  (Parse *, SrcLis
74670 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  t *);.SQLITE_PRI
74680 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
74690 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28  lite3GetCollSeq(
746a0 73 71 6c 69 74 65 33 2a 2c 20 75 38 2c 20 43 6f  sqlite3*, u8, Co
746b0 6c 6c 53 65 71 20 2a 2c 20 63 6f 6e 73 74 20 63  llSeq *, const c
746c0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
746d0 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74  IVATE char sqlit
746e0 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 63  e3AffinityType(c
746f0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
74700 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
74710 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28   sqlite3Analyze(
74720 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20  Parse*, Token*, 
74730 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
74740 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
74750 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e  te3InvokeBusyHan
74760 64 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65 72  dler(BusyHandler
74770 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74780 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
74790 6e 64 44 62 28 73 71 6c 69 74 65 33 2a 2c 20 54  ndDb(sqlite3*, T
747a0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
747b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
747c0 65 33 46 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c  e3FindDbName(sql
747d0 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68  ite3 *, const ch
747e0 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ar *);.SQLITE_PR
747f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
74800 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28 73 71  3AnalysisLoad(sq
74810 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44 42 29 3b  lite3*,int iDB);
74820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74830 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
74840 74 65 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 49  teIndexSamples(I
74850 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ndex*);.SQLITE_P
74860 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74870 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74  te3DefaultRowEst
74880 28 49 6e 64 65 78 2a 29 3b 0a 53 51 4c 49 54 45  (Index*);.SQLITE
74890 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
748a0 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b  lite3RegisterLik
748b0 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74  eFunctions(sqlit
748c0 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  e3*, int);.SQLIT
748d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
748e0 6c 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74  lite3IsLikeFunct
748f0 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 45 78 70  ion(sqlite3*,Exp
74900 72 2a 2c 69 6e 74 2a 2c 63 68 61 72 2a 29 3b 0a  r*,int*,char*);.
74910 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74920 6f 69 64 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d  oid sqlite3Minim
74930 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 50 61 72  umFileFormat(Par
74940 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  se*, int, int);.
74950 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74960 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65 6d  oid sqlite3Schem
74970 61 46 72 65 65 28 76 6f 69 64 20 2a 29 3b 0a 53  aFree(void *);.S
74980 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 63  QLITE_PRIVATE Sc
74990 68 65 6d 61 20 2a 73 71 6c 69 74 65 33 53 63 68  hema *sqlite3Sch
749a0 65 6d 61 47 65 74 28 73 71 6c 69 74 65 33 20 2a  emaGet(sqlite3 *
749b0 2c 20 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49  , Btree *);.SQLI
749c0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
749d0 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
749e0 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  dex(sqlite3 *db,
749f0 20 53 63 68 65 6d 61 20 2a 29 3b 0a 53 51 4c 49   Schema *);.SQLI
74a00 54 45 5f 50 52 49 56 41 54 45 20 4b 65 79 49 6e  TE_PRIVATE KeyIn
74a10 66 6f 20 2a 73 71 6c 69 74 65 33 49 6e 64 65 78  fo *sqlite3Index
74a20 4b 65 79 69 6e 66 6f 28 50 61 72 73 65 20 2a 2c  Keyinfo(Parse *,
74a30 20 49 6e 64 65 78 20 2a 29 3b 0a 53 51 4c 49 54   Index *);.SQLIT
74a40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
74a50 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28  lite3CreateFunc(
74a60 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74  sqlite3 *, const
74a70 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 69 6e   char *, int, in
74a80 74 2c 20 76 6f 69 64 20 2a 2c 20 0a 20 20 76 6f  t, void *, .  vo
74a90 69 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63  id (*)(sqlite3_c
74aa0 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
74ab0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20  te3_value **),. 
74ac0 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74 65   void (*)(sqlite
74ad0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
74ae0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29  qlite3_value **)
74af0 2c 20 76 6f 69 64 20 28 2a 29 28 73 71 6c 69 74  , void (*)(sqlit
74b00 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 53  e3_context*));.S
74b10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
74b20 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  t sqlite3ApiExit
74b30 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
74b40 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
74b50 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70  TE int sqlite3Op
74b60 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 50  enTempDatabase(P
74b70 61 72 73 65 20 2a 29 3b 0a 0a 53 51 4c 49 54 45  arse *);..SQLITE
74b80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74b90 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
74ba0 74 28 53 74 72 41 63 63 75 6d 2a 2c 20 63 68 61  t(StrAccum*, cha
74bb0 72 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53  r*, int, int);.S
74bc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74bd0 69 64 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  id sqlite3StrAcc
74be0 75 6d 41 70 70 65 6e 64 28 53 74 72 41 63 63 75  umAppend(StrAccu
74bf0 6d 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  m*,const char*,i
74c00 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
74c10 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
74c20 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28  3StrAccumFinish(
74c30 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49  StrAccum*);.SQLI
74c40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
74c50 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52  sqlite3StrAccumR
74c60 65 73 65 74 28 53 74 72 41 63 63 75 6d 2a 29 3b  eset(StrAccum*);
74c70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74c80 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65  void sqlite3Sele
74c90 63 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65 63  ctDestInit(Selec
74ca0 74 44 65 73 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b  tDest*,int,int);
74cb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74cc0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 43 72 65  Expr *sqlite3Cre
74cd0 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72 28 73 71  ateColumnExpr(sq
74ce0 6c 69 74 65 33 20 2a 2c 20 53 72 63 4c 69 73 74  lite3 *, SrcList
74cf0 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a   *, int, int);..
74d00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74d10 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75  oid sqlite3Backu
74d20 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 33  pRestart(sqlite3
74d30 5f 62 61 63 6b 75 70 20 2a 29 3b 0a 53 51 4c 49  _backup *);.SQLI
74d40 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
74d50 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64  sqlite3BackupUpd
74d60 61 74 65 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ate(sqlite3_back
74d70 75 70 20 2a 2c 20 50 67 6e 6f 2c 20 63 6f 6e 73  up *, Pgno, cons
74d80 74 20 75 38 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  t u8 *);../*.** 
74d90 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f  The interface to
74da0 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65 6e 65 72   the LEMON-gener
74db0 61 74 65 64 20 70 61 72 73 65 72 0a 2a 2f 0a 53  ated parser.*/.S
74dc0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74dd0 69 64 20 2a 73 71 6c 69 74 65 33 50 61 72 73 65  id *sqlite3Parse
74de0 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28 2a 29 28  rAlloc(void*(*)(
74df0 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c 49 54 45  size_t));.SQLITE
74e00 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74e10 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65 28  lite3ParserFree(
74e20 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a 29 28 76  void*, void(*)(v
74e30 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50  oid*));.SQLITE_P
74e40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74e50 74 65 33 50 61 72 73 65 72 28 76 6f 69 64 2a 2c  te3Parser(void*,
74e60 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20 50 61 72   int, Token, Par
74e70 73 65 2a 29 3b 0a 23 69 66 64 65 66 20 59 59 54  se*);.#ifdef YYT
74e80 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54  RACKMAXSTACKDEPT
74e90 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  H.SQLITE_PRIVATE
74ea0 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61     int sqlite3Pa
74eb0 72 73 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f  rserStackPeak(vo
74ec0 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51  id*);.#endif..SQ
74ed0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
74ee0 64 20 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61  d sqlite3AutoLoa
74ef0 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69  dExtensions(sqli
74f00 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53  te3*);.#ifndef S
74f10 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
74f20 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45  EXTENSION.SQLITE
74f30 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
74f40 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65  sqlite3CloseExte
74f50 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29  nsions(sqlite3*)
74f60 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
74f70 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74   sqlite3CloseExt
74f80 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65 6e 64 69  ensions(X).#endi
74f90 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
74fa0 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
74fb0 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  CHE.SQLITE_PRIVA
74fc0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
74fd0 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61 72 73 65  3TableLock(Parse
74fe0 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 75 38   *, int, int, u8
74ff0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b  , const char *);
75000 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
75010 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
75020 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23 65 6e  k(v,w,x,y,z).#en
75030 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
75040 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  TE_TEST.SQLITE_P
75050 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
75060 69 74 65 33 55 74 66 38 54 6f 38 28 75 6e 73 69  ite3Utf8To8(unsi
75070 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 23 65 6e  gned char*);.#en
75080 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
75090 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
750a0 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65 20 73  ABLE.#  define s
750b0 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28  qlite3VtabClear(
750c0 59 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  Y).#  define sql
750d0 69 74 65 33 56 74 61 62 53 79 6e 63 28 58 2c 59  ite3VtabSync(X,Y
750e0 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 20 64  ) SQLITE_OK.#  d
750f0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61  efine sqlite3Vta
75100 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a 23 20 20  bRollback(X).#  
75110 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74  define sqlite3Vt
75120 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23 20 20 64  abCommit(X).#  d
75130 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61  efine sqlite3Vta
75140 62 49 6e 53 79 6e 63 28 64 62 29 20 30 0a 23 20  bInSync(db) 0.# 
75150 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
75160 74 61 62 4c 6f 63 6b 28 58 29 20 0a 23 20 20 64  tabLock(X) .#  d
75170 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61  efine sqlite3Vta
75180 62 55 6e 6c 6f 63 6b 28 58 29 0a 23 20 20 64 65  bUnlock(X).#  de
75190 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
751a0 55 6e 6c 6f 63 6b 4c 69 73 74 28 58 29 0a 23 65  UnlockList(X).#e
751b0 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  lse.SQLITE_PRIVA
751c0 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74  TE    void sqlit
751d0 65 33 56 74 61 62 43 6c 65 61 72 28 54 61 62 6c  e3VtabClear(Tabl
751e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
751f0 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74  ATE    int sqlit
75200 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69 74  e3VtabSync(sqlit
75210 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 29  e3 *db, char **)
75220 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75230 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56      int sqlite3V
75240 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69  tabRollback(sqli
75250 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45  te3 *db);.SQLITE
75260 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20  _PRIVATE    int 
75270 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69  sqlite3VtabCommi
75280 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a  t(sqlite3 *db);.
75290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
752a0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74    void sqlite3Vt
752b0 61 62 4c 6f 63 6b 28 56 54 61 62 6c 65 20 2a 29  abLock(VTable *)
752c0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
752d0 20 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33      void sqlite3
752e0 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54 61 62 6c  VtabUnlock(VTabl
752f0 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e *);.SQLITE_PRI
75300 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c  VATE    void sql
75310 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69  ite3VtabUnlockLi
75320 73 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 20  st(sqlite3*);.# 
75330 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
75340 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 28 28  tabInSync(db) ((
75350 64 62 29 2d 3e 6e 56 54 72 61 6e 73 3e 30 20 26  db)->nVTrans>0 &
75360 26 20 28 64 62 29 2d 3e 61 56 54 72 61 6e 73 3d  & (db)->aVTrans=
75370 3d 30 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  =0).#endif.SQLIT
75380 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
75390 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72  qlite3VtabMakeWr
753a0 69 74 61 62 6c 65 28 50 61 72 73 65 2a 2c 54 61  itable(Parse*,Ta
753b0 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ble*);.SQLITE_PR
753c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
753d0 65 33 56 74 61 62 42 65 67 69 6e 50 61 72 73 65  e3VtabBeginParse
753e0 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c  (Parse*, Token*,
753f0 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29   Token*, Token*)
75400 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75410 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
75420 62 46 69 6e 69 73 68 50 61 72 73 65 28 50 61 72  bFinishParse(Par
75430 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  se*, Token*);.SQ
75440 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
75450 64 20 73 71 6c 69 74 65 33 56 74 61 62 41 72 67  d sqlite3VtabArg
75460 49 6e 69 74 28 50 61 72 73 65 2a 29 3b 0a 53 51  Init(Parse*);.SQ
75470 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
75480 64 20 73 71 6c 69 74 65 33 56 74 61 62 41 72 67  d sqlite3VtabArg
75490 45 78 74 65 6e 64 28 50 61 72 73 65 2a 2c 20 54  Extend(Parse*, T
754a0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
754b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
754c0 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65  e3VtabCallCreate
754d0 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20  (sqlite3*, int, 
754e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 68  const char *, ch
754f0 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ar **);.SQLITE_P
75500 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
75510 65 33 56 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63  e3VtabCallConnec
75520 74 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  t(Parse*, Table*
75530 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
75540 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
75550 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 73 71 6c  bCallDestroy(sql
75560 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73  ite3*, int, cons
75570 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54  t char *);.SQLIT
75580 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
75590 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 73  lite3VtabBegin(s
755a0 71 6c 69 74 65 33 20 2a 2c 20 56 54 61 62 6c 65  qlite3 *, VTable
755b0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
755c0 41 54 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c  ATE FuncDef *sql
755d0 69 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64  ite3VtabOverload
755e0 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33  Function(sqlite3
755f0 20 2a 2c 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74   *,FuncDef*, int
75600 20 6e 41 72 67 2c 20 45 78 70 72 2a 29 3b 0a 53   nArg, Expr*);.S
75610 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
75620 69 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69  id sqlite3Invali
75630 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  dFunction(sqlite
75640 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
75650 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b  qlite3_value**);
75660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75670 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 50  int sqlite3VdbeP
75680 61 72 61 6d 65 74 65 72 49 6e 64 65 78 28 56 64  arameterIndex(Vd
75690 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  be*, const char*
756a0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
756b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
756c0 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e  e3TransferBindin
756d0 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  gs(sqlite3_stmt 
756e0 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  *, sqlite3_stmt 
756f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
75700 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65  TE int sqlite3Re
75710 70 72 65 70 61 72 65 28 56 64 62 65 2a 29 3b 0a  prepare(Vdbe*);.
75720 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
75730 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c  oid sqlite3ExprL
75740 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 50  istCheckLength(P
75750 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a  arse*, ExprList*
75760 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  , const char*);.
75770 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
75780 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 42  ollSeq *sqlite3B
75790 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c  inaryCompareColl
757a0 53 65 71 28 50 61 72 73 65 20 2a 2c 20 45 78 70  Seq(Parse *, Exp
757b0 72 20 2a 2c 20 45 78 70 72 20 2a 29 3b 0a 53 51  r *, Expr *);.SQ
757c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
757d0 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65   sqlite3TempInMe
757e0 6d 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69 74  mory(const sqlit
757f0 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  e3*);.SQLITE_PRI
75800 56 41 54 45 20 56 54 61 62 6c 65 20 2a 73 71 6c  VATE VTable *sql
75810 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 73 71  ite3GetVTable(sq
75820 6c 69 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29 3b  lite3*, Table*);
75830 0a 0a 2f 2a 20 44 65 63 6c 61 72 61 74 69 6f 6e  ../* Declaration
75840 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20  s for functions 
75850 69 6e 20 66 6b 65 79 2e 63 2e 20 41 6c 6c 20 6f  in fkey.c. All o
75860 66 20 74 68 65 73 65 20 61 72 65 20 72 65 70 6c  f these are repl
75870 61 63 65 64 20 62 79 0a 2a 2a 20 6e 6f 2d 6f 70  aced by.** no-op
75880 20 6d 61 63 72 6f 73 20 69 66 20 4f 4d 49 54 5f   macros if OMIT_
75890 46 4f 52 45 49 47 4e 5f 4b 45 59 20 69 73 20 64  FOREIGN_KEY is d
758a0 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73 20  efined. In this 
758b0 63 61 73 65 20 6e 6f 20 66 6f 72 65 69 67 6e 0a  case no foreign.
758c0 2a 2a 20 6b 65 79 20 66 75 6e 63 74 69 6f 6e 61  ** key functiona
758d0 6c 69 74 79 20 69 73 20 61 76 61 69 6c 61 62 6c  lity is availabl
758e0 65 2e 20 49 66 20 4f 4d 49 54 5f 54 52 49 47 47  e. If OMIT_TRIGG
758f0 45 52 20 69 73 20 64 65 66 69 6e 65 64 20 62 75  ER is defined bu
75900 74 0a 2a 2a 20 4f 4d 49 54 5f 46 4f 52 45 49 47  t.** OMIT_FOREIG
75910 4e 5f 4b 45 59 20 69 73 20 6e 6f 74 2c 20 6f 6e  N_KEY is not, on
75920 6c 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 66  ly some of the f
75930 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 2d  unctions are no-
75940 6f 70 65 64 2e 20 49 6e 0a 2a 2a 20 74 68 69 73  oped. In.** this
75950 20 63 61 73 65 20 66 6f 72 65 69 67 6e 20 6b 65   case foreign ke
75960 79 73 20 61 72 65 20 70 61 72 73 65 64 2c 20 62  ys are parsed, b
75970 75 74 20 6e 6f 20 6f 74 68 65 72 20 66 75 6e 63  ut no other func
75980 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 0a 2a 2a  tionality is .**
75990 20 70 72 6f 76 69 64 65 64 20 28 65 6e 66 6f 72   provided (enfor
759a0 63 65 6d 65 6e 74 20 6f 66 20 46 4b 20 63 6f 6e  cement of FK con
759b0 73 74 72 61 69 6e 74 73 20 72 65 71 75 69 72 65  straints require
759c0 73 20 74 68 65 20 74 72 69 67 67 65 72 73 20 73  s the triggers s
759d0 75 62 2d 73 79 73 74 65 6d 29 2e 0a 2a 2f 0a 23  ub-system)..*/.#
759e0 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
759f0 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
75a00 4b 45 59 29 20 26 26 20 21 64 65 66 69 6e 65 64  KEY) && !defined
75a10 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
75a20 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49  GGER).SQLITE_PRI
75a30 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
75a40 74 65 33 46 6b 43 68 65 63 6b 28 50 61 72 73 65  te3FkCheck(Parse
75a50 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20  *, Table*, int, 
75a60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
75a70 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
75a80 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65 28 50  te3FkDropTable(P
75a90 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 20 2a  arse*, SrcList *
75aa0 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54  , Table*);.SQLIT
75ab0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
75ac0 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e   sqlite3FkAction
75ad0 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  s(Parse*, Table*
75ae0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74  , ExprList*, int
75af0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
75b00 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 46  E   int sqlite3F
75b10 6b 52 65 71 75 69 72 65 64 28 50 61 72 73 65 2a  kRequired(Parse*
75b20 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2a 2c 20  , Table*, int*, 
75b30 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
75b40 56 41 54 45 20 20 20 75 33 32 20 73 71 6c 69 74  VATE   u32 sqlit
75b50 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 50 61 72 73  e3FkOldmask(Pars
75b60 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c  e*, Table*);.SQL
75b70 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 46 4b  ITE_PRIVATE   FK
75b80 65 79 20 2a 73 71 6c 69 74 65 33 46 6b 52 65 66  ey *sqlite3FkRef
75b90 65 72 65 6e 63 65 73 28 54 61 62 6c 65 20 2a 29  erences(Table *)
75ba0 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e  ;.#else.  #defin
75bb0 65 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f  e sqlite3FkActio
75bc0 6e 73 28 61 2c 62 2c 63 2c 64 29 0a 20 20 23 64  ns(a,b,c,d).  #d
75bd0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 43  efine sqlite3FkC
75be0 68 65 63 6b 28 61 2c 62 2c 63 2c 64 29 0a 20 20  heck(a,b,c,d).  
75bf0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46  #define sqlite3F
75c00 6b 44 72 6f 70 54 61 62 6c 65 28 61 2c 62 2c 63  kDropTable(a,b,c
75c10 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69  ).  #define sqli
75c20 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 61 2c 62  te3FkOldmask(a,b
75c30 29 20 20 20 20 20 20 30 0a 20 20 23 64 65 66 69  )      0.  #defi
75c40 6e 65 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75  ne sqlite3FkRequ
75c50 69 72 65 64 28 61 2c 62 2c 63 2c 64 29 20 30 0a  ired(a,b,c,d) 0.
75c60 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
75c70 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
75c80 47 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52  GN_KEY.SQLITE_PR
75c90 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
75ca0 69 74 65 33 46 6b 44 65 6c 65 74 65 28 54 61 62  ite3FkDelete(Tab
75cb0 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64  le*);.#else.  #d
75cc0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 44  efine sqlite3FkD
75cd0 65 6c 65 74 65 28 61 29 0a 23 65 6e 64 69 66 0a  elete(a).#endif.
75ce0 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62 6c  ../*.** Availabl
75cf0 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f 72  e fault injector
75d00 73 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e 75  s.  Should be nu
75d10 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e 67  mbered beginning
75d20 20 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65 66   with 0..*/.#def
75d30 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54  ine SQLITE_FAULT
75d40 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43 20  INJECTOR_MALLOC 
75d50 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51      0.#define SQ
75d60 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43 54  LITE_FAULTINJECT
75d70 4f 52 5f 43 4f 55 4e 54 20 20 20 20 20 20 31 0a  OR_COUNT      1.
75d80 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72  ./*.** The inter
75d90 66 61 63 65 20 74 6f 20 74 68 65 20 63 6f 64 65  face to the code
75da0 20 69 6e 20 66 61 75 6c 74 2e 63 20 75 73 65 64   in fault.c used
75db0 20 66 6f 72 20 69 64 65 6e 74 69 66 79 69 6e 67   for identifying
75dc0 20 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61 6c   "benign".** mal
75dd0 6c 6f 63 20 66 61 69 6c 75 72 65 73 2e 20 54 68  loc failures. Th
75de0 69 73 20 69 73 20 6f 6e 6c 79 20 70 72 65 73 65  is is only prese
75df0 6e 74 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49  nt if SQLITE_OMI
75e00 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 2a  T_BUILTIN_TEST.*
75e10 2a 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  * is not defined
75e20 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
75e30 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
75e40 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49  _TEST.SQLITE_PRI
75e50 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
75e60 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
75e70 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c 49  lloc(void);.SQLI
75e80 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
75e90 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  d sqlite3EndBeni
75ea0 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a  gnMalloc(void);.
75eb0 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
75ec0 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
75ed0 67 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64 65  gnMalloc().  #de
75ee0 66 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64 42  fine sqlite3EndB
75ef0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23 65  enignMalloc().#e
75f00 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 49 4e  ndif..#define IN
75f10 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 20 20 20  _INDEX_ROWID    
75f20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
75f30 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20 20   IN_INDEX_EPH   
75f40 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66            2.#def
75f50 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44  ine IN_INDEX_IND
75f60 45 58 20 20 20 20 20 20 20 20 20 20 20 33 0a 53  EX           3.S
75f70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
75f80 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49  t sqlite3FindInI
75f90 6e 64 65 78 28 50 61 72 73 65 20 2a 2c 20 45 78  ndex(Parse *, Ex
75fa0 70 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23 69  pr *, int*);..#i
75fb0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
75fc0 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a  LE_ATOMIC_WRITE.
75fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
75fe0 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72   int sqlite3Jour
75ff0 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  nalOpen(sqlite3_
76000 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61  vfs *, const cha
76010 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c  r *, sqlite3_fil
76020 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  e *, int, int);.
76030 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
76040 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72   int sqlite3Jour
76050 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33 5f  nalSize(sqlite3_
76060 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  vfs *);.SQLITE_P
76070 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c  RIVATE   int sql
76080 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74  ite3JournalCreat
76090 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
760a0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  );.#else.  #defi
760b0 6e 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  ne sqlite3Journa
760c0 6c 53 69 7a 65 28 70 56 66 73 29 20 28 28 70 56  lSize(pVfs) ((pV
760d0 66 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a 23  fs)->szOsFile).#
760e0 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
760f0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
76100 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e  e3MemJournalOpen
76110 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
76120 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76130 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 4a   int sqlite3MemJ
76140 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64 29  ournalSize(void)
76150 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76160 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d 65   int sqlite3IsMe
76170 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65 33  mJournal(sqlite3
76180 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20 53  _file *);..#if S
76190 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
761a0 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50 52  EPTH>0.SQLITE_PR
761b0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
761c0 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
761d0 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
761e0 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49 54   Expr *p);.SQLIT
761f0 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20  E_PRIVATE   int 
76200 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
76210 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a  rHeight(Select *
76220 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
76230 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 45  E   int sqlite3E
76240 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 50  xprCheckHeight(P
76250 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c  arse*, int);.#el
76260 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  se.  #define sql
76270 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
76280 74 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e 65  t(x,y).  #define
76290 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
762a0 70 72 48 65 69 67 68 74 28 78 29 20 30 0a 20 20  prHeight(x) 0.  
762b0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45  #define sqlite3E
762c0 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28 78  xprCheckHeight(x
762d0 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49  ,y).#endif..SQLI
762e0 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
762f0 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63  qlite3Get4byte(c
76300 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49 54  onst u8*);.SQLIT
76310 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
76320 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28 75  qlite3Put4byte(u
76330 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64 65  8*, u32);..#ifde
76340 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
76350 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53 51  UNLOCK_NOTIFY.SQ
76360 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
76370 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  oid sqlite3Conne
76380 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71 6c  ctionBlocked(sql
76390 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33 20  ite3 *, sqlite3 
763a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
763b0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
763c0 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63  3ConnectionUnloc
763d0 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ked(sqlite3 *db)
763e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
763f0 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43     void sqlite3C
76400 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28  onnectionClosed(
76410 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65  sqlite3 *db);.#e
76420 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
76430 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
76440 6c 6f 63 6b 65 64 28 78 2c 79 29 0a 20 20 23 64  locked(x,y).  #d
76450 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 6e  efine sqlite3Con
76460 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28  nectionUnlocked(
76470 78 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  x).  #define sql
76480 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c  ite3ConnectionCl
76490 6f 73 65 64 28 78 29 0a 23 65 6e 64 69 66 0a 0a  osed(x).#endif..
764a0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
764b0 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
764c0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
764d0 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49 4c  3ParserTrace(FIL
764e0 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65 6e  E*, char *);.#en
764f0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  dif../*.** If th
76500 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 20  e SQLITE_ENABLE 
76510 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20 74  IOTRACE exists t
76520 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 76  hen the global v
76530 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69 74  ariable.** sqlit
76540 65 33 49 6f 54 72 61 63 65 20 69 73 20 61 20 70  e3IoTrace is a p
76550 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72 69 6e  ointer to a prin
76560 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65 20  tf-like routine 
76570 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e 74  used to.** print
76580 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65 73   I/O tracing mes
76590 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64 65  sages. .*/.#ifde
765a0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
765b0 49 4f 54 52 41 43 45 0a 23 20 64 65 66 69 6e 65  IOTRACE.# define
765c0 20 49 4f 54 52 41 43 45 28 41 29 20 20 69 66 28   IOTRACE(A)  if(
765d0 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 20   sqlite3IoTrace 
765e0 29 7b 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63  ){ sqlite3IoTrac
765f0 65 20 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50 52  e A; }.SQLITE_PR
76600 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
76610 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53  ite3VdbeIOTraceS
76620 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  ql(Vdbe*);.SQLIT
76630 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 28  E_PRIVATE void (
76640 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65 29  *sqlite3IoTrace)
76650 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e  (const char*,...
76660 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
76670 65 20 49 4f 54 52 41 43 45 28 41 29 0a 23 20 64  e IOTRACE(A).# d
76680 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62  efine sqlite3Vdb
76690 65 49 4f 54 72 61 63 65 53 71 6c 28 58 29 0a 23  eIOTraceSql(X).#
766a0 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f  endif..#endif../
766b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
766c0 6e 64 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  nd of sqliteInt.
766d0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
766e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
766f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
76700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
76710 65 67 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61 6c  egin file global
76720 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
76730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
76750 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31  *.** 2008 June 1
76760 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  3.**.** The auth
76770 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
76780 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
76790 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
767a0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
767b0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
767c0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
767d0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
767e0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
767f0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
76800 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
76810 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
76820 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
76830 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
76840 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
76850 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
76860 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
76870 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
76880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
768a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
768b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
768c0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
768d0 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69   contains defini
768e0 74 69 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c 20  tions of global 
768f0 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 63 6f  variables and co
76900 6e 74 61 6e 74 73 2e 0a 2a 2f 0a 0a 2f 2a 20 41  ntants..*/../* A
76910 6e 20 61 72 72 61 79 20 74 6f 20 6d 61 70 20 61  n array to map a
76920 6c 6c 20 75 70 70 65 72 2d 63 61 73 65 20 63 68  ll upper-case ch
76930 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68  aracters into th
76940 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  eir correspondin
76950 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73 65 20  g.** lower-case 
76960 63 68 61 72 61 63 74 65 72 2e 20 0a 2a 2a 0a 2a  character. .**.*
76970 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20 63 6f  * SQLite only co
76980 6e 73 69 64 65 72 73 20 55 53 2d 41 53 43 49 49  nsiders US-ASCII
76990 20 28 6f 72 20 45 42 43 44 49 43 29 20 63 68 61   (or EBCDIC) cha
769a0 72 61 63 74 65 72 73 2e 20 20 57 65 20 64 6f 20  racters.  We do 
769b0 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 63 61  not.** handle ca
769c0 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 66  se conversions f
769d0 6f 72 20 74 68 65 20 55 54 46 20 63 68 61 72 61  or the UTF chara
769e0 63 74 65 72 20 73 65 74 20 73 69 6e 63 65 20 74  cter set since t
769f0 68 65 20 74 61 62 6c 65 73 0a 2a 2a 20 69 6e 76  he tables.** inv
76a00 6f 6c 76 65 64 20 61 72 65 20 6e 65 61 72 6c 79  olved are nearly
76a10 20 61 73 20 62 69 67 20 6f 72 20 62 69 67 67 65   as big or bigge
76a20 72 20 74 68 61 6e 20 53 51 4c 69 74 65 20 69 74  r than SQLite it
76a30 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  self..*/.SQLITE_
76a40 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e  PRIVATE const un
76a50 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69  signed char sqli
76a60 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b  te3UpperToLower[
76a70 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20 53 51 4c  ] = {.#ifdef SQL
76a80 49 54 45 5f 41 53 43 49 49 0a 20 20 20 20 20 20  ITE_ASCII.      
76a90 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20 20  0,  1,  2,  3,  
76aa0 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20 20  4,  5,  6,  7,  
76ab0 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20 31  8,  9, 10, 11, 1
76ac0 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20 31  2, 13, 14, 15, 1
76ad0 36 2c 20 31 37 2c 0a 20 20 20 20 20 31 38 2c 20  6, 17,.     18, 
76ae0 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 20  19, 20, 21, 22, 
76af0 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 20  23, 24, 25, 26, 
76b00 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 20  27, 28, 29, 30, 
76b10 33 31 2c 20 33 32 2c 20 33 33 2c 20 33 34 2c 20  31, 32, 33, 34, 
76b20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20 33 37 2c  35,.     36, 37,
76b30 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34 31 2c   38, 39, 40, 41,
76b40 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34 35 2c   42, 43, 44, 45,
76b50 20 34 36 2c 20 34 37 2c 20 34 38 2c 20 34 39 2c   46, 47, 48, 49,
76b60 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33 2c   50, 51, 52, 53,
76b70 0a 20 20 20 20 20 35 34 2c 20 35 35 2c 20 35 36  .     54, 55, 56
76b80 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 36 30  , 57, 58, 59, 60
76b90 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 36 34  , 61, 62, 63, 64
76ba0 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30  , 97, 98, 99,100
76bb0 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 0a 20 20  ,101,102,103,.  
76bc0 20 20 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 30    104,105,106,10
76bd0 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31  7,108,109,110,11
76be0 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 31 31  1,112,113,114,11
76bf0 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c 31 31  5,116,117,118,11
76c00 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20 20 20 31  9,120,121,.    1
76c10 32 32 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20  22, 91, 92, 93, 
76c20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39 37 2c 20  94, 95, 96, 97, 
76c30 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c 31  98, 99,100,101,1
76c40 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35 2c 31  02,103,104,105,1
76c50 30 36 2c 31 30 37 2c 0a 20 20 20 20 31 30 38 2c  06,107,.    108,
76c60 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32 2c  109,110,111,112,
76c70 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36 2c  113,114,115,116,
76c80 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30 2c  117,118,119,120,
76c90 31 32 31 2c 31 32 32 2c 31 32 33 2c 31 32 34 2c  121,122,123,124,
76ca0 31 32 35 2c 0a 20 20 20 20 31 32 36 2c 31 32 37  125,.    126,127
76cb0 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31  ,128,129,130,131
76cc0 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35  ,132,133,134,135
76cd0 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39  ,136,137,138,139
76ce0 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33  ,140,141,142,143
76cf0 2c 0a 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34  ,.    144,145,14
76d00 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35  6,147,148,149,15
76d10 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35  0,151,152,153,15
76d20 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35  4,155,156,157,15
76d30 38 2c 31 35 39 2c 31 36 30 2c 31 36 31 2c 0a 20  8,159,160,161,. 
76d40 20 20 20 31 36 32 2c 31 36 33 2c 31 36 34 2c 31     162,163,164,1
76d50 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31  65,166,167,168,1
76d60 36 39 2c 31 37 30 2c 31 37 31 2c 31 37 32 2c 31  69,170,171,172,1
76d70 37 33 2c 31 37 34 2c 31 37 35 2c 31 37 36 2c 31  73,174,175,176,1
76d80 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20 20 20 20  77,178,179,.    
76d90 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c  180,181,182,183,
76da0 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c  184,185,186,187,
76db0 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c  188,189,190,191,
76dc0 31 39 32 2c 31 39 33 2c 31 39 34 2c 31 39 35 2c  192,193,194,195,
76dd0 31 39 36 2c 31 39 37 2c 0a 20 20 20 20 31 39 38  196,197,.    198
76de0 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c 32 30 32  ,199,200,201,202
76df0 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36  ,203,204,205,206
76e00 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c 32 31 30  ,207,208,209,210
76e10 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c 32 31 34  ,211,212,213,214
76e20 2c 32 31 35 2c 0a 20 20 20 20 32 31 36 2c 32 31  ,215,.    216,21
76e30 37 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 32  7,218,219,220,22
76e40 31 2c 32 32 32 2c 32 32 33 2c 32 32 34 2c 32 32  1,222,223,224,22
76e50 35 2c 32 32 36 2c 32 32 37 2c 32 32 38 2c 32 32  5,226,227,228,22
76e60 39 2c 32 33 30 2c 32 33 31 2c 32 33 32 2c 32 33  9,230,231,232,23
76e70 33 2c 0a 20 20 20 20 32 33 34 2c 32 33 35 2c 32  3,.    234,235,2
76e80 33 36 2c 32 33 37 2c 32 33 38 2c 32 33 39 2c 32  36,237,238,239,2
76e90 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33 2c 32  40,241,242,243,2
76ea0 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37 2c 32  44,245,246,247,2
76eb0 34 38 2c 32 34 39 2c 32 35 30 2c 32 35 31 2c 0a  48,249,250,251,.
76ec0 20 20 20 20 32 35 32 2c 32 35 33 2c 32 35 34 2c      252,253,254,
76ed0 32 35 35 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  255.#endif.#ifde
76ee0 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a  f SQLITE_EBCDIC.
76ef0 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20 32 2c        0,  1,  2,
76f00 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20 36 2c    3,  4,  5,  6,
76f10 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31 30 2c    7,  8,  9, 10,
76f20 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31 34 2c   11, 12, 13, 14,
76f30 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0a 20 20   15, /* 0x */.  
76f40 20 20 20 31 36 2c 20 31 37 2c 20 31 38 2c 20 31     16, 17, 18, 1
76f50 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c 20 32  9, 20, 21, 22, 2
76f60 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c 20 32  3, 24, 25, 26, 2
76f70 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c 20 33  7, 28, 29, 30, 3
76f80 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 20 20 20  1, /* 1x */.    
76f90 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c   32, 33, 34, 35,
76fa0 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33 39 2c   36, 37, 38, 39,
76fb0 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34 33 2c   40, 41, 42, 43,
76fc0 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34 37 2c   44, 45, 46, 47,
76fd0 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20 20 34   /* 2x */.     4
76fe0 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c 20 35  8, 49, 50, 51, 5
76ff0 32 2c 20 35 33 2c 20 35 34 2c 20 35 35 2c 20 35  2, 53, 54, 55, 5
77000 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20 36  6, 57, 58, 59, 6
77010 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20 2f  0, 61, 62, 63, /
77020 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20 36 34 2c  * 3x */.     64,
77030 20 36 35 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c   65, 66, 67, 68,
77040 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c   69, 70, 71, 72,
77050 20 37 33 2c 20 37 34 2c 20 37 35 2c 20 37 36 2c   73, 74, 75, 76,
77060 20 37 37 2c 20 37 38 2c 20 37 39 2c 20 2f 2a 20   77, 78, 79, /* 
77070 34 78 20 2a 2f 0a 20 20 20 20 20 38 30 2c 20 38  4x */.     80, 8
77080 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38  1, 82, 83, 84, 8
77090 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38  5, 86, 87, 88, 8
770a0 39 2c 20 39 30 2c 20 39 31 2c 20 39 32 2c 20 39  9, 90, 91, 92, 9
770b0 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a 20 35 78  3, 94, 95, /* 5x
770c0 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20 39 37 2c   */.     96, 97,
770d0 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c   66, 67, 68, 69,
770e0 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c   70, 71, 72, 73,
770f0 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30 39 2c  106,107,108,109,
77100 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36 78 20 2a  110,111, /* 6x *
77110 2f 0a 20 20 20 20 31 31 32 2c 20 38 31 2c 20 38  /.    112, 81, 8
77120 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38  2, 83, 84, 85, 8
77130 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 31 32  6, 87, 88, 89,12
77140 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 31 32  2,123,124,125,12
77150 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20 2a 2f 0a  6,127, /* 7x */.
77160 20 20 20 20 31 32 38 2c 31 32 39 2c 31 33 30 2c      128,129,130,
77170 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c  131,132,133,134,
77180 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c  135,136,137,138,
77190 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c  139,140,141,142,
771a0 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 0a 20 20  143, /* 8x */.  
771b0 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34    144,145,146,14
771c0 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35  7,148,149,150,15
771d0 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35  1,152,153,154,15
771e0 35 2c 31 35 36 2c 31 35 37 2c 31 35 36 2c 31 35  5,156,157,156,15
771f0 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20  9, /* 9x */.    
77200 31 36 30 2c 31 36 31 2c 31 36 32 2c 31 36 33 2c  160,161,162,163,
77210 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c  164,165,166,167,
77220 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c  168,169,170,171,
77230 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 37 35 2c  140,141,142,175,
77240 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20 31 37   /* Ax */.    17
77250 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 31 38  6,177,178,179,18
77260 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c 31 38  0,181,182,183,18
77270 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c 31 38  4,185,186,187,18
77280 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c 20 2f  8,189,190,191, /
77290 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31 39 32 2c  * Bx */.    192,
772a0 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c  129,130,131,132,
772b0 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c  133,134,135,136,
772c0 31 33 37 2c 32 30 32 2c 32 30 33 2c 32 30 34 2c  137,202,203,204,
772d0 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20  205,206,207, /* 
772e0 43 78 20 2a 2f 0a 20 20 20 20 32 30 38 2c 31 34  Cx */.    208,14
772f0 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34  5,146,147,148,14
77300 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35  9,150,151,152,15
77310 33 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c 32 32  3,218,219,220,22
77320 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a 20 44 78  1,222,223, /* Dx
77330 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32 32 35 2c   */.    224,225,
77340 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c  162,163,164,165,
77350 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c  166,167,168,169,
77360 32 33 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c  232,203,204,205,
77370 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78 20 2a  206,207, /* Ex *
77380 2f 0a 20 20 20 20 32 33 39 2c 32 34 30 2c 32 34  /.    239,240,24
77390 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 34  1,242,243,244,24
773a0 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 34  5,246,247,248,24
773b0 39 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32  9,219,220,221,22
773c0 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a 2f 0a  2,255, /* Fx */.
773d0 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
773e0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 32   The following 2
773f0 35 36 20 62 79 74 65 20 6c 6f 6f 6b 75 70 20 74  56 byte lookup t
77400 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20  able is used to 
77410 73 75 70 70 6f 72 74 20 53 51 4c 69 74 65 73 20  support SQLites 
77420 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 71 75 69  built-in.** equi
77430 76 61 6c 65 6e 74 73 20 74 6f 20 74 68 65 20 66  valents to the f
77440 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 6e 64 61 72  ollowing standar
77450 64 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69  d library functi
77460 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 73 73  ons:.**.**   iss
77470 70 61 63 65 28 29 20 20 20 20 20 20 20 20 20 20  pace()          
77480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78                0x
77490 30 31 0a 2a 2a 20 20 20 69 73 61 6c 70 68 61 28  01.**   isalpha(
774a0 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
774b0 20 20 20 20 20 20 20 20 20 30 78 30 32 0a 2a 2a           0x02.**
774c0 20 20 20 69 73 64 69 67 69 74 28 29 20 20 20 20     isdigit()    
774d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
774e0 20 20 20 20 30 78 30 34 0a 2a 2a 20 20 20 69 73      0x04.**   is
774f0 61 6c 6e 75 6d 28 29 20 20 20 20 20 20 20 20 20  alnum()         
77500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
77510 78 30 36 0a 2a 2a 20 20 20 69 73 78 64 69 67 69  x06.**   isxdigi
77520 74 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20  t()             
77530 20 20 20 20 20 20 20 20 20 20 30 78 30 38 0a 2a            0x08.*
77540 2a 20 20 20 74 6f 75 70 70 65 72 28 29 20 20 20  *   toupper()   
77550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77560 20 20 20 20 20 30 78 32 30 0a 2a 2a 20 20 20 53       0x20.**   S
77570 51 4c 69 74 65 20 69 64 65 6e 74 69 66 69 65 72  QLite identifier
77580 20 63 68 61 72 61 63 74 65 72 20 20 20 20 20 20   character      
77590 30 78 34 30 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30  0x40.**.** Bit 0
775a0 78 32 30 20 69 73 20 73 65 74 20 69 66 20 74 68  x20 is set if th
775b0 65 20 6d 61 70 70 65 64 20 63 68 61 72 61 63 74  e mapped charact
775c0 65 72 20 72 65 71 75 69 72 65 73 20 74 72 61 6e  er requires tran
775d0 73 6c 61 74 69 6f 6e 20 74 6f 20 75 70 70 65 72  slation to upper
775e0 0a 2a 2a 20 63 61 73 65 2e 20 69 2e 65 2e 20 69  .** case. i.e. i
775f0 66 20 74 68 65 20 63 68 61 72 61 63 74 65 72 20  f the character 
77600 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 20  is a lower-case 
77610 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 2e  ASCII character.
77620 0a 2a 2a 20 49 66 20 78 20 69 73 20 61 20 6c 6f  .** If x is a lo
77630 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 63  wer-case ASCII c
77640 68 61 72 61 63 74 65 72 2c 20 74 68 65 6e 20 69  haracter, then i
77650 74 73 20 75 70 70 65 72 2d 63 61 73 65 20 65 71  ts upper-case eq
77660 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 69 73 20 28  uivalent.** is (
77670 78 20 2d 20 30 78 32 30 29 2e 20 54 68 65 72 65  x - 0x20). There
77680 66 6f 72 65 20 74 6f 75 70 70 65 72 28 29 20 63  fore toupper() c
77690 61 6e 20 62 65 20 69 6d 70 6c 65 6d 65 6e 74 65  an be implemente
776a0 64 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 78  d as:.**.**   (x
776b0 20 26 20 7e 28 6d 61 70 5b 78 5d 26 30 78 32 30   & ~(map[x]&0x20
776c0 29 29 0a 2a 2a 0a 2a 2a 20 53 74 61 6e 64 61 72  )).**.** Standar
776d0 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 6c 6f 77  d function tolow
776e0 65 72 28 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e  er() is implemen
776f0 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 73 71  ted using the sq
77700 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
77710 72 5b 5d 0a 2a 2a 20 61 72 72 61 79 2e 20 74 6f  r[].** array. to
77720 6c 6f 77 65 72 28 29 20 69 73 20 75 73 65 64 20  lower() is used 
77730 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e 20  more often than 
77740 74 6f 75 70 70 65 72 28 29 20 62 79 20 53 51 4c  toupper() by SQL
77750 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30  ite..**.** Bit 0
77760 78 34 30 20 69 73 20 73 65 74 20 69 66 20 74 68  x40 is set if th
77770 65 20 63 68 61 72 61 63 74 65 72 20 6e 6f 6e 2d  e character non-
77780 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 61 6e 64  alphanumeric and
77790 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20   can be used in 
777a0 61 6e 20 0a 2a 2a 20 53 51 4c 69 74 65 20 69 64  an .** SQLite id
777b0 65 6e 74 69 66 69 65 72 2e 20 20 49 64 65 6e 74  entifier.  Ident
777c0 69 66 69 65 72 73 20 61 72 65 20 61 6c 70 68 61  ifiers are alpha
777d0 6e 75 6d 65 72 69 63 73 2c 20 22 5f 22 2c 20 22  numerics, "_", "
777e0 24 22 2c 20 61 6e 64 20 61 6e 79 0a 2a 2a 20 6e  $", and any.** n
777f0 6f 6e 2d 41 53 43 49 49 20 55 54 46 20 63 68 61  on-ASCII UTF cha
77800 72 61 63 74 65 72 2e 20 48 65 6e 63 65 20 74 68  racter. Hence th
77810 65 20 74 65 73 74 20 66 6f 72 20 77 68 65 74 68  e test for wheth
77820 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 68 61 72  er or not a char
77830 61 63 74 65 72 20 69 73 0a 2a 2a 20 70 61 72 74  acter is.** part
77840 20 6f 66 20 61 6e 20 69 64 65 6e 74 69 66 69 65   of an identifie
77850 72 20 69 73 20 30 78 34 36 2e 0a 2a 2a 0a 2a 2a  r is 0x46..**.**
77860 20 53 51 4c 69 74 65 27 73 20 76 65 72 73 69 6f   SQLite's versio
77870 6e 73 20 61 72 65 20 69 64 65 6e 74 69 63 61 6c  ns are identical
77880 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64   to the standard
77890 20 76 65 72 73 69 6f 6e 73 20 61 73 73 75 6d 69   versions assumi
778a0 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 20 6f  ng a.** locale o
778b0 66 20 22 43 22 2e 20 54 68 65 79 20 61 72 65 20  f "C". They are 
778c0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6d  implemented as m
778d0 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 74 65 49  acros in sqliteI
778e0 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  nt.h..*/.#ifdef 
778f0 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 4c  SQLITE_ASCII.SQL
77900 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
77910 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
77920 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b  sqlite3CtypeMap[
77930 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 30 30 2c  256] = {.  0x00,
77940 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77950 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77960 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30  x00, 0x00,  /* 0
77970 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  0..07    .......
77980 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
77990 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20 30  1, 0x01, 0x01, 0
779a0 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 30 2c  x01, 0x01, 0x00,
779b0 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 2e 2e 30   0x00,  /* 08..0
779c0 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  f    ........ */
779d0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
779e0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
779f0 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77a00 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20 20  0,  /* 10..17   
77a10 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
77a20 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77a30 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77a40 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
77a50 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e 2e  /* 18..1f    ...
77a60 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 31 2c  ..... */.  0x01,
77a70 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77a80 30 2c 20 30 78 34 30 2c 20 30 78 30 30 2c 20 30  0, 0x40, 0x00, 0
77a90 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32  x00, 0x00,  /* 2
77aa0 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24 25 26  0..27     !"#$%&
77ab0 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  ' */.  0x00, 0x0
77ac0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77ad0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77ae0 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e 32   0x00,  /* 28..2
77af0 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a 2f  f    ()*+,-./ */
77b00 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30  .  0x0c, 0x0c, 0
77b10 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63 2c  x0c, 0x0c, 0x0c,
77b20 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30   0x0c, 0x0c, 0x0
77b30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20 20 20  c,  /* 30..37   
77b40 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 20 20 30   01234567 */.  0
77b50 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 30 2c  x0c, 0x0c, 0x00,
77b60 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77b70 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
77b80 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 38 39 3a  /* 38..3f    89:
77b90 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 30 30  ;<=>? */..  0x00
77ba0 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30 78  , 0x0a, 0x0a, 0x
77bb0 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20  0a, 0x0a, 0x0a, 
77bc0 30 78 30 61 2c 20 30 78 30 32 2c 20 20 2f 2a 20  0x0a, 0x02,  /* 
77bd0 34 30 2e 2e 34 37 20 20 20 20 40 41 42 43 44 45  40..47    @ABCDE
77be0 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78  FG */.  0x02, 0x
77bf0 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20  02, 0x02, 0x02, 
77c00 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
77c10 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 38 2e 2e  , 0x02,  /* 48..
77c20 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f 20 2a  4f    HIJKLMNO *
77c30 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c 20  /.  0x02, 0x02, 
77c40 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
77c50 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78  , 0x02, 0x02, 0x
77c60 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 20 20  02,  /* 50..57  
77c70 20 20 50 51 52 53 54 55 56 57 20 2a 2f 0a 20 20    PQRSTUVW */.  
77c80 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32  0x02, 0x02, 0x02
77c90 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77ca0 30 30 2c 20 30 78 30 30 2c 20 30 78 34 30 2c 20  00, 0x00, 0x40, 
77cb0 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 20 58 59   /* 58..5f    XY
77cc0 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 30 30  Z[\]^_ */.  0x00
77cd0 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30 78  , 0x2a, 0x2a, 0x
77ce0 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20  2a, 0x2a, 0x2a, 
77cf0 30 78 32 61 2c 20 30 78 32 32 2c 20 20 2f 2a 20  0x2a, 0x22,  /* 
77d00 36 30 2e 2e 36 37 20 20 20 20 60 61 62 63 64 65  60..67    `abcde
77d10 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78  fg */.  0x22, 0x
77d20 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20  22, 0x22, 0x22, 
77d30 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
77d40 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 38 2e 2e  , 0x22,  /* 68..
77d50 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f 20 2a  6f    hijklmno *
77d60 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c 20  /.  0x22, 0x22, 
77d70 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
77d80 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78  , 0x22, 0x22, 0x
77d90 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 20 20  22,  /* 70..77  
77da0 20 20 70 71 72 73 74 75 76 77 20 2a 2f 0a 20 20    pqrstuvw */.  
77db0 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32  0x22, 0x22, 0x22
77dc0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77dd0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77de0 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 20 78 79   /* 78..7f    xy
77df0 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 78 34  z{|}~. */..  0x4
77e00 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77e10 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77e20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a   0x40, 0x40,  /*
77e30 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e 2e   80..87    .....
77e40 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30  ... */.  0x40, 0
77e50 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77e60 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77e70 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 38 38 2e  0, 0x40,  /* 88.
77e80 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .8f    ........ 
77e90 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c  */.  0x40, 0x40,
77ea0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77eb0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77ec0 78 34 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37 20  x40,  /* 90..97 
77ed0 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
77ee0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77ef0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77f00 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77f10 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20 2e    /* 98..9f    .
77f20 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34  ....... */.  0x4
77f30 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77f40 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77f50 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a   0x40, 0x40,  /*
77f60 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e 2e 2e   a0..a7    .....
77f70 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30  ... */.  0x40, 0
77f80 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
77f90 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77fa0 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 61 38 2e  0, 0x40,  /* a8.
77fb0 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20  .af    ........ 
77fc0 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30 2c  */.  0x40, 0x40,
77fd0 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
77fe0 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
77ff0 78 34 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 37 20  x40,  /* b0..b7 
78000 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20     ........ */. 
78010 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34   0x40, 0x40, 0x4
78020 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30  0, 0x40, 0x40, 0
78030 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c  x40, 0x40, 0x40,
78040 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 20 20 2e    /* b8..bf    .
78050 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 30 78  ....... */..  0x
78060 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
78070 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
78080 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f  , 0x40, 0x40,  /
78090 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e 2e 2e 2e  * c0..c7    ....
780a0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20  .... */.  0x40, 
780b0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
780c0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
780d0 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 63 38  40, 0x40,  /* c8
780e0 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..cf    ........
780f0 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30   */.  0x40, 0x40
78100 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
78110 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
78120 30 78 34 30 2c 20 20 2f 2a 20 64 30 2e 2e 64 37  0x40,  /* d0..d7
78130 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
78140 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78    0x40, 0x40, 0x
78150 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
78160 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
78170 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 20 20 20  ,  /* d8..df    
78180 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78  ........ */.  0x
78190 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
781a0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
781b0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 20 2f  , 0x40, 0x40,  /
781c0 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e 2e 2e 2e  * e0..e7    ....
781d0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 34 30 2c 20  .... */.  0x40, 
781e0 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
781f0 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
78200 34 30 2c 20 30 78 34 30 2c 20 20 2f 2a 20 65 38  40, 0x40,  /* e8
78210 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..ef    ........
78220 20 2a 2f 0a 20 20 30 78 34 30 2c 20 30 78 34 30   */.  0x40, 0x40
78230 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78  , 0x40, 0x40, 0x
78240 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
78250 30 78 34 30 2c 20 20 2f 2a 20 66 30 2e 2e 66 37  0x40,  /* f0..f7
78260 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
78270 20 20 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78    0x40, 0x40, 0x
78280 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30 2c 20  40, 0x40, 0x40, 
78290 30 78 34 30 2c 20 30 78 34 30 2c 20 30 78 34 30  0x40, 0x40, 0x40
782a0 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20 20     /* f8..ff    
782b0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a 23  ........ */.};.#
782c0 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54  endif..../*.** T
782d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e  he following sin
782e0 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  gleton contains 
782f0 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69  the global confi
78300 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20  guration for.** 
78310 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
78320 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ry..*/.SQLITE_PR
78330 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53 44  IVATE SQLITE_WSD
78340 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43   struct Sqlite3C
78350 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f 6e  onfig sqlite3Con
78360 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54  fig = {.   SQLIT
78370 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41  E_DEFAULT_MEMSTA
78380 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61  TUS,  /* bMemsta
78390 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20 20  t */.   1,      
783a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
783b0 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78     /* bCoreMutex
783c0 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54 48   */.   SQLITE_TH
783d0 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20 20  READSAFE==1,    
783e0 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20    /* bFullMutex 
783f0 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66 65  */.   0x7ffffffe
78400 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
78410 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a   /* mxStrlen */.
78420 20 20 20 31 30 30 2c 20 20 20 20 20 20 20 20 20     100,         
78430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
78440 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a   szLookaside */.
78450 20 20 20 35 30 30 2c 20 20 20 20 20 20 20 20 20     500,         
78460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
78470 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20   nLookaside */. 
78480 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30    {0,0,0,0,0,0,0
78490 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ,0},         /* 
784a0 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c 30  m */.   {0,0,0,0
784b0 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 20  ,0,0,0,0,0},    
784c0 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a 20     /* mutex */. 
784d0 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30    {0,0,0,0,0,0,0
784e0 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a 20  ,0,0,0,0},   /* 
784f0 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 76 6f  pcache */.   (vo
78500 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20  id*)0,          
78510 20 20 20 20 20 20 20 20 2f 2a 20 70 48 65 61 70          /* pHeap
78520 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
78530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78540 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20 20    /* nHeap */.  
78550 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 20 20   0, 0,          
78560 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d              /* m
78570 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 20 2a 2f  nHeap, mxHeap */
78580 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20 20  .   (void*)0,   
78590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
785a0 2a 20 70 53 63 72 61 74 63 68 20 2a 2f 0a 20 20  * pScratch */.  
785b0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
785c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
785d0 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 30  zScratch */.   0
785e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
785f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53 63            /* nSc
78600 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f 69  ratch */.   (voi
78610 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20 20  d*)0,           
78620 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20         /* pPage 
78630 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20 20  */.   0,        
78640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78650 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20 20   /* szPage */.  
78660 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
78670 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e              /* n
78680 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Page */.   0,   
78690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
786a0 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 73 65        /* mxParse
786b0 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c 20  rStack */.   0, 
786c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
786d0 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72 65          /* share
786e0 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 2a 2f  dCacheEnabled */
786f0 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20 72  .   /* All the r
78700 65 73 74 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  est should alway
78710 73 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  s be initialized
78720 20 74 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20 30   to zero */.   0
78730 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
78740 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73 49            /* isI
78750 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  nit */.   0,    
78760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78770 20 20 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72 65       /* inProgre
78780 73 73 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  ss */.   0,     
78790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
787a0 20 20 20 20 2f 2a 20 69 73 4d 75 74 65 78 49 6e      /* isMutexIn
787b0 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20  it */.   0,     
787c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
787d0 20 20 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63 49      /* isMallocI
787e0 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  nit */.   0,    
787f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78800 20 20 20 20 20 2f 2a 20 69 73 50 43 61 63 68 65       /* isPCache
78810 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Init */.   0,   
78820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78830 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d 75        /* pInitMu
78840 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  tex */.   0,    
78850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78860 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69 74       /* nRefInit
78870 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a  Mutex */.};.../*
78880 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66  .** Hash table f
78890 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69  or global functi
788a0 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20  ons - functions 
788b0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a  common to all.**
788c0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
788d0 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69 6e  tions.  After in
788e0 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68  itialization, th
788f0 69 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72  is table is.** r
78900 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c  ead-only..*/.SQL
78910 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c 49  ITE_PRIVATE SQLI
78920 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48 61  TE_WSD FuncDefHa
78930 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  sh sqlite3Global
78940 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a 2a  Functions;../*.*
78950 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74  * The value of t
78960 68 65 20 22 70 65 6e 64 69 6e 67 22 20 62 79 74  he "pending" byt
78970 65 20 6d 75 73 74 20 62 65 20 30 78 34 30 30 30  e must be 0x4000
78980 30 30 30 30 20 28 31 20 62 79 74 65 20 70 61 73  0000 (1 byte pas
78990 74 20 74 68 65 0a 2a 2a 20 31 2d 67 69 62 61 62  t the.** 1-gibab
789a0 79 74 65 20 62 6f 75 6e 64 61 72 79 29 20 69 6e  yte boundary) in
789b0 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 64 61   a compatible da
789c0 74 61 62 61 73 65 2e 20 20 53 51 4c 69 74 65 20  tabase.  SQLite 
789d0 6e 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 74 68  never uses.** th
789e0 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  e database page 
789f0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
78a00 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e 20  e pending byte. 
78a10 20 49 74 20 6e 65 76 65 72 20 61 74 74 65 6d 70   It never attemp
78a20 74 73 0a 2a 2a 20 74 6f 20 72 65 61 64 20 6f 72  ts.** to read or
78a30 20 77 72 69 74 65 20 74 68 61 74 20 70 61 67 65   write that page
78a40 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20 62  .  The pending b
78a50 79 74 65 20 70 61 67 65 20 69 73 20 73 65 74 20  yte page is set 
78a60 61 73 73 69 67 6e 0a 2a 2a 20 66 6f 72 20 75 73  assign.** for us
78a70 65 20 62 79 20 74 68 65 20 56 46 53 20 6c 61 79  e by the VFS lay
78a80 65 72 73 20 61 73 20 73 70 61 63 65 20 66 6f 72  ers as space for
78a90 20 6d 61 6e 61 67 69 6e 67 20 66 69 6c 65 20 6c   managing file l
78aa0 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69  ocks..**.** Duri
78ab0 6e 67 20 74 65 73 74 69 6e 67 2c 20 69 74 20 69  ng testing, it i
78ac0 73 20 6f 66 74 65 6e 20 64 65 73 69 72 61 62 6c  s often desirabl
78ad0 65 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 70 65  e to move the pe
78ae0 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 0a 2a 2a  nding byte to.**
78af0 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 6f 73   a different pos
78b00 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c  ition in the fil
78b10 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20  e.  This allows 
78b20 63 6f 64 65 20 74 68 61 74 20 68 61 73 20 74 6f  code that has to
78b30 0a 2a 2a 20 64 65 61 6c 20 77 69 74 68 20 74 68  .** deal with th
78b40 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74  e pending byte t
78b50 6f 20 72 75 6e 20 6f 6e 20 66 69 6c 65 73 20 74  o run on files t
78b60 68 61 74 20 61 72 65 20 6d 75 63 68 20 73 6d 61  hat are much sma
78b70 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e 20 31 20 47  ller.** than 1 G
78b80 69 42 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  iB.  The sqlite3
78b90 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 20  _test_control() 
78ba0 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62 65  interface can be
78bb0 20 75 73 65 64 20 74 6f 0a 2a 2a 20 6d 6f 76 65   used to.** move
78bc0 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74   the pending byt
78bd0 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f 52 54 41  e..**.** IMPORTA
78be0 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74 68  NT:  Changing th
78bf0 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20 74  e pending byte t
78c00 6f 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65  o any value othe
78c10 72 20 74 68 61 6e 0a 2a 2a 20 30 78 34 30 30 30  r than.** 0x4000
78c20 30 30 30 30 20 72 65 73 75 6c 74 73 20 69 6e 20  0000 results in 
78c30 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20  an incompatible 
78c40 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f  database file fo
78c50 72 6d 61 74 21 0a 2a 2a 20 43 68 61 6e 67 69 6e  rmat!.** Changin
78c60 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79  g the pending by
78c70 74 65 20 64 75 72 69 6e 67 20 6f 70 65 72 61 74  te during operat
78c80 69 6e 67 20 72 65 73 75 6c 74 73 20 69 6e 20 75  ing results in u
78c90 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 61 6e 64 20  ndefined.** and 
78ca0 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61  dileterious beha
78cb0 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  vior..*/.SQLITE_
78cc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
78cd0 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 20 3d  te3PendingByte =
78ce0 20 30 78 34 30 30 30 30 30 30 30 3b 0a 0a 2f 2a   0x40000000;../*
78cf0 0a 2a 2a 20 50 72 6f 70 65 72 74 69 65 73 20 6f  .** Properties o
78d00 66 20 6f 70 63 6f 64 65 73 2e 20 20 54 68 65 20  f opcodes.  The 
78d10 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45  OPFLG_INITIALIZE
78d20 52 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20 63 72  R macro is.** cr
78d30 65 61 74 65 64 20 62 79 20 6d 6b 6f 70 63 6f 64  eated by mkopcod
78d40 65 68 2e 61 77 6b 20 64 75 72 69 6e 67 20 63 6f  eh.awk during co
78d50 6d 70 69 6c 61 74 69 6f 6e 2e 20 20 44 61 74 61  mpilation.  Data
78d60 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20   is obtained.** 
78d70 66 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65 6e 74  from the comment
78d80 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  s following the 
78d90 22 63 61 73 65 20 4f 50 5f 78 78 78 78 3a 22 20  "case OP_xxxx:" 
78da0 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a  statements in.**
78db0 20 74 68 65 20 76 64 62 65 2e 63 20 66 69 6c 65   the vdbe.c file
78dc0 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  .  .*/.SQLITE_PR
78dd0 49 56 41 54 45 20 63 6f 6e 73 74 20 75 6e 73 69  IVATE const unsi
78de0 67 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65  gned char sqlite
78df0 33 4f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b  3OpcodeProperty[
78e00 5d 20 3d 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41  ] = OPFLG_INITIA
78e10 4c 49 5a 45 52 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  LIZER;../*******
78e20 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 67  ******* End of g
78e30 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  lobal.c ********
78e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78e60 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
78e70 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
78e80 6c 65 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a 2a  le status.c ****
78e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78eb0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
78ec0 30 38 20 4a 75 6e 65 20 31 38 0a 2a 2a 0a 2a 2a  08 June 18.**.**
78ed0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
78ee0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
78ef0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
78f00 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
78f10 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
78f20 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
78f30 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
78f40 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
78f50 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
78f60 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
78f70 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
78f80 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
78f90 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
78fa0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
78fb0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
78fc0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
78fd0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
78fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79020 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
79030 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70 6c  This module impl
79040 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69 74  ements the sqlit
79050 65 33 5f 73 74 61 74 75 73 28 29 20 69 6e 74 65  e3_status() inte
79060 72 66 61 63 65 20 61 6e 64 20 72 65 6c 61 74 65  rface and relate
79070 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 61 6c 69  d.** functionali
79080 74 79 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61  ty..*/../*.** Va
79090 72 69 61 62 6c 65 73 20 69 6e 20 77 68 69 63 68  riables in which
790a0 20 74 6f 20 72 65 63 6f 72 64 20 73 74 61 74 75   to record statu
790b0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  s information..*
790c0 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
790d0 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65   sqlite3StatType
790e0 20 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65   sqlite3StatType
790f0 3b 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f  ;.static SQLITE_
79100 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74  WSD struct sqlit
79110 65 33 53 74 61 74 54 79 70 65 20 7b 0a 20 20 69  e3StatType {.  i
79120 6e 74 20 6e 6f 77 56 61 6c 75 65 5b 39 5d 3b 20  nt nowValue[9]; 
79130 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
79140 6e 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e  nt value */.  in
79150 74 20 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 20 20  t mxValue[9];   
79160 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
79170 6d 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c  m value */.} sql
79180 69 74 65 33 53 74 61 74 20 3d 20 7b 20 7b 30 2c  ite3Stat = { {0,
79190 7d 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20  }, {0,} };.../* 
791a0 54 68 65 20 22 77 73 64 53 74 61 74 22 20 6d 61  The "wsdStat" ma
791b0 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65  cro will resolve
791c0 20 74 6f 20 74 68 65 20 73 74 61 74 75 73 20 69   to the status i
791d0 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74  nformation.** st
791e0 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20  ate vector.  If 
791f0 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20  writable static 
79200 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72  data is unsuppor
79210 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65  ted on the targe
79220 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f  t,.** we have to
79230 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74   locate the stat
79240 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d  e vector at run-
79250 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f  time.  In the mo
79260 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73  re common.** cas
79270 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65  e where writable
79280 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20   static data is 
79290 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53 74  supported, wsdSt
792a0 61 74 20 63 61 6e 20 72 65 66 65 72 20 64 69 72  at can refer dir
792b0 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20  ectly.** to the 
792c0 22 73 71 6c 69 74 65 33 53 74 61 74 22 20 73 74  "sqlite3Stat" st
792d0 61 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61  ate vector decla
792e0 72 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69  red above..*/.#i
792f0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
79300 5f 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73  _WSD.# define ws
79310 64 53 74 61 74 49 6e 69 74 20 20 73 71 6c 69 74  dStatInit  sqlit
79320 65 33 53 74 61 74 54 79 70 65 20 2a 78 20 3d 20  e3StatType *x = 
79330 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 53  &GLOBAL(sqlite3S
79340 74 61 74 54 79 70 65 2c 73 71 6c 69 74 65 33 53  tatType,sqlite3S
79350 74 61 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73  tat).# define ws
79360 64 53 74 61 74 20 78 5b 30 5d 0a 23 65 6c 73 65  dStat x[0].#else
79370 0a 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61  .# define wsdSta
79380 74 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77  tInit.# define w
79390 73 64 53 74 61 74 20 73 71 6c 69 74 65 33 53 74  sdStat sqlite3St
793a0 61 74 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  at.#endif../*.**
793b0 20 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72   Return the curr
793c0 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 61 20 73  ent value of a s
793d0 74 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 2e  tatus parameter.
793e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
793f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  TE int sqlite3St
79400 61 74 75 73 56 61 6c 75 65 28 69 6e 74 20 6f 70  atusValue(int op
79410 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74  ){.  wsdStatInit
79420 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d  ;.  assert( op>=
79430 30 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a  0 && op<ArraySiz
79440 65 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c  e(wsdStat.nowVal
79450 75 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ue) );.  return 
79460 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
79470 5b 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  [op];.}../*.** A
79480 64 64 20 4e 20 74 6f 20 74 68 65 20 76 61 6c 75  dd N to the valu
79490 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 72 65  e of a status re
794a0 63 6f 72 64 2e 20 20 49 74 20 69 73 20 61 73 73  cord.  It is ass
794b0 75 6d 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a  umed that the.**
794c0 20 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20 61 70   caller holds ap
794d0 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 73 2e  propriate locks.
794e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
794f0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
79500 74 61 74 75 73 41 64 64 28 69 6e 74 20 6f 70 2c  tatusAdd(int op,
79510 20 69 6e 74 20 4e 29 7b 0a 20 20 77 73 64 53 74   int N){.  wsdSt
79520 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74  atInit;.  assert
79530 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72  ( op>=0 && op<Ar
79540 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e  raySize(wsdStat.
79550 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 77  nowValue) );.  w
79560 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b  sdStat.nowValue[
79570 6f 70 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 28 20  op] += N;.  if( 
79580 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
79590 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56  [op]>wsdStat.mxV
795a0 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20  alue[op] ){.    
795b0 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b  wsdStat.mxValue[
795c0 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f  op] = wsdStat.no
795d0 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a  wValue[op];.  }.
795e0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
795f0 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74   value of a stat
79600 75 73 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49  us to X..*/.SQLI
79610 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
79620 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74  sqlite3StatusSet
79630 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29 7b  (int op, int X){
79640 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a  .  wsdStatInit;.
79650 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20    assert( op>=0 
79660 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28  && op<ArraySize(
79670 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
79680 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74 2e 6e  ) );.  wsdStat.n
79690 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b  owValue[op] = X;
796a0 0a 20 20 69 66 28 20 77 73 64 53 74 61 74 2e 6e  .  if( wsdStat.n
796b0 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53  owValue[op]>wsdS
796c0 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20  tat.mxValue[op] 
796d0 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d  ){.    wsdStat.m
796e0 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64  xValue[op] = wsd
796f0 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70  Stat.nowValue[op
79700 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ];.  }.}../*.** 
79710 51 75 65 72 79 20 73 74 61 74 75 73 20 69 6e 66  Query status inf
79720 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  ormation..**.** 
79730 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74  This implementat
79740 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74  ion assumes that
79750 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74   reading or writ
79760 69 6e 67 20 61 6e 20 61 6c 69 67 6e 65 64 0a 2a  ing an aligned.*
79770 2a 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  * 32-bit integer
79780 20 69 73 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70   is an atomic op
79790 65 72 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 61  eration.  If tha
797a0 74 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20  t assumption is 
797b0 6e 6f 74 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65  not true,.** the
797c0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
797d0 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65  s not threadsafe
797e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
797f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74  int sqlite3_stat
79800 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a  us(int op, int *
79810 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70  pCurrent, int *p
79820 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72  Highwater, int r
79830 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 77 73 64  esetFlag){.  wsd
79840 53 74 61 74 49 6e 69 74 3b 0a 20 20 69 66 28 20  StatInit;.  if( 
79850 6f 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 72 61  op<0 || op>=Arra
79860 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f  ySize(wsdStat.no
79870 77 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 72  wValue) ){.    r
79880 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
79890 55 53 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 72  USE;.  }.  *pCur
798a0 72 65 6e 74 20 3d 20 77 73 64 53 74 61 74 2e 6e  rent = wsdStat.n
798b0 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a  owValue[op];.  *
798c0 70 48 69 67 68 77 61 74 65 72 20 3d 20 77 73 64  pHighwater = wsd
798d0 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d  Stat.mxValue[op]
798e0 3b 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c 61  ;.  if( resetFla
798f0 67 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74  g ){.    wsdStat
79900 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77  .mxValue[op] = w
79910 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b  sdStat.nowValue[
79920 6f 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  op];.  }.  retur
79930 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
79940 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74  /*.** Query stat
79950 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  us information f
79960 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61  or a single data
79970 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  base connection.
79980 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
79990 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61  t sqlite3_db_sta
799a0 74 75 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  tus(.  sqlite3 *
799b0 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  db,          /* 
799c0 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
799d0 6e 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 74  nection whose st
799e0 61 74 75 73 20 69 73 20 64 65 73 69 72 65 64 20  atus is desired 
799f0 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
79a00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
79a10 61 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 69  atus verb */.  i
79a20 6e 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 20  nt *pCurrent,   
79a30 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 75       /* Write cu
79a40 72 72 65 6e 74 20 76 61 6c 75 65 20 68 65 72 65  rrent value here
79a50 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 68   */.  int *pHigh
79a60 77 61 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 57  water,      /* W
79a70 72 69 74 65 20 68 69 67 68 2d 77 61 74 65 72 20  rite high-water 
79a80 6d 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 69  mark here */.  i
79a90 6e 74 20 72 65 73 65 74 46 6c 61 67 20 20 20 20  nt resetFlag    
79aa0 20 20 20 20 20 2f 2a 20 52 65 73 65 74 20 68 69       /* Reset hi
79ab0 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 66  gh-water mark if
79ac0 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 73 77   true */.){.  sw
79ad0 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20  itch( op ){.    
79ae0 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54  case SQLITE_DBST
79af0 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55  ATUS_LOOKASIDE_U
79b00 53 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a 70 43  SED: {.      *pC
79b10 75 72 72 65 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f  urrent = db->loo
79b20 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20  kaside.nOut;.   
79b30 20 20 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d     *pHighwater =
79b40 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d   db->lookaside.m
79b50 78 4f 75 74 3b 0a 20 20 20 20 20 20 69 66 28 20  xOut;.      if( 
79b60 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20  resetFlag ){.   
79b70 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69       db->lookasi
79b80 64 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c  de.mxOut = db->l
79b90 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20  ookaside.nOut;. 
79ba0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
79bb0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
79bc0 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72  fault: {.      r
79bd0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
79be0 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  OR;.    }.  }.  
79bf0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
79c00 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
79c10 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74 61 74  **** End of stat
79c20 75 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  us.c ***********
79c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79c50 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
79c60 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
79c70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  date.c *********
79c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79ca0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20  ***/./*.** 2003 
79cb0 4f 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a  October 31.**.**
79cc0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
79cd0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
79ce0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
79cf0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
79d00 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
79d10 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
79d20 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
79d30 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
79d40 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
79d50 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
79d60 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
79d70 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
79d80 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
79d90 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
79da0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
79db0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
79dc0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
79dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
79e20 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
79e30 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20  the C functions 
79e40 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 64  that implement d
79e50 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20  ate and time.** 
79e60 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53 51  functions for SQ
79e70 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  Lite.  .**.** Th
79e80 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20  ere is only one 
79e90 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20  exported symbol 
79ea0 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74  in this file - t
79eb0 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73  he function.** s
79ec0 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61  qlite3RegisterDa
79ed0 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28  teTimeFunctions(
79ee0 29 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62  ) found at the b
79ef0 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c  ottom of the fil
79f00 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20  e..** All other 
79f10 63 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73 63  code has file sc
79f20 6f 70 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ope..**.** SQLit
79f30 65 20 70 72 6f 63 65 73 73 65 73 20 61 6c 6c 20  e processes all 
79f40 74 69 6d 65 73 20 61 6e 64 20 64 61 74 65 73 20  times and dates 
79f50 61 73 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75  as Julian Day nu
79f60 6d 62 65 72 73 2e 20 20 54 68 65 0a 2a 2a 20 64  mbers.  The.** d
79f70 61 74 65 73 20 61 6e 64 20 74 69 6d 65 73 20 61  ates and times a
79f80 72 65 20 73 74 6f 72 65 64 20 61 73 20 74 68 65  re stored as the
79f90 20 6e 75 6d 62 65 72 20 6f 66 20 64 61 79 73 20   number of days 
79fa0 73 69 6e 63 65 20 6e 6f 6f 6e 0a 2a 2a 20 69 6e  since noon.** in
79fb0 20 47 72 65 65 6e 77 69 63 68 20 6f 6e 20 4e 6f   Greenwich on No
79fc0 76 65 6d 62 65 72 20 32 34 2c 20 34 37 31 34 20  vember 24, 4714 
79fd0 42 2e 43 2e 20 61 63 63 6f 72 64 69 6e 67 20 74  B.C. according t
79fe0 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 0a  o the Gregorian.
79ff0 2a 2a 20 63 61 6c 65 6e 64 61 72 20 73 79 73 74  ** calendar syst
7a000 65 6d 2e 20 0a 2a 2a 0a 2a 2a 20 31 39 37 30 2d  em. .**.** 1970-
7a010 30 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69  01-01 00:00:00 i
7a020 73 20 4a 44 20 32 34 34 30 35 38 37 2e 35 0a 2a  s JD 2440587.5.*
7a030 2a 20 32 30 30 30 2d 30 31 2d 30 31 20 30 30 3a  * 2000-01-01 00:
7a040 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34 35 31  00:00 is JD 2451
7a050 35 34 34 2e 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73  544.5.**.** This
7a060 20 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 72 65   implemention re
7a070 71 75 69 72 65 73 20 79 65 61 72 73 20 74 6f 20  quires years to 
7a080 62 65 20 65 78 70 72 65 73 73 65 64 20 61 73 20  be expressed as 
7a090 61 20 34 2d 64 69 67 69 74 20 6e 75 6d 62 65 72  a 4-digit number
7a0a0 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e 73 20  .** which means 
7a0b0 74 68 61 74 20 6f 6e 6c 79 20 64 61 74 65 73 20  that only dates 
7a0c0 62 65 74 77 65 65 6e 20 30 30 30 30 2d 30 31 2d  between 0000-01-
7a0d0 30 31 20 61 6e 64 20 39 39 39 39 2d 31 32 2d 33  01 and 9999-12-3
7a0e0 31 20 63 61 6e 0a 2a 2a 20 62 65 20 72 65 70 72  1 can.** be repr
7a0f0 65 73 65 6e 74 65 64 2c 20 65 76 65 6e 20 74 68  esented, even th
7a100 6f 75 67 68 20 6a 75 6c 69 61 6e 20 64 61 79 20  ough julian day 
7a110 6e 75 6d 62 65 72 73 20 61 6c 6c 6f 77 20 61 20  numbers allow a 
7a120 6d 75 63 68 20 77 69 64 65 72 0a 2a 2a 20 72 61  much wider.** ra
7a130 6e 67 65 20 6f 66 20 64 61 74 65 73 2e 0a 2a 2a  nge of dates..**
7a140 0a 2a 2a 20 54 68 65 20 47 72 65 67 6f 72 69 61  .** The Gregoria
7a150 6e 20 63 61 6c 65 6e 64 61 72 20 73 79 73 74 65  n calendar syste
7a160 6d 20 69 73 20 75 73 65 64 20 66 6f 72 20 61 6c  m is used for al
7a170 6c 20 64 61 74 65 73 20 61 6e 64 20 74 69 6d 65  l dates and time
7a180 73 2c 0a 2a 2a 20 65 76 65 6e 20 74 68 6f 73 65  s,.** even those
7a190 20 74 68 61 74 20 70 72 65 64 61 74 65 20 74 68   that predate th
7a1a0 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65  e Gregorian cale
7a1b0 6e 64 61 72 2e 20 20 48 69 73 74 6f 72 69 61 6e  ndar.  Historian
7a1c0 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 75 73 65  s usually.** use
7a1d0 20 74 68 65 20 4a 75 6c 69 61 6e 20 63 61 6c 65   the Julian cale
7a1e0 6e 64 61 72 20 66 6f 72 20 64 61 74 65 73 20 70  ndar for dates p
7a1f0 72 69 6f 72 20 74 6f 20 31 35 38 32 2d 31 30 2d  rior to 1582-10-
7a200 31 35 20 61 6e 64 20 66 6f 72 20 73 6f 6d 65 0a  15 and for some.
7a210 2a 2a 20 64 61 74 65 73 20 61 66 74 65 72 77 61  ** dates afterwa
7a220 72 64 73 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f  rds, depending o
7a230 6e 20 6c 6f 63 61 6c 65 2e 20 20 42 65 77 61 72  n locale.  Bewar
7a240 65 20 6f 66 20 74 68 69 73 20 64 69 66 66 65 72  e of this differ
7a250 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ence..**.** The 
7a260 63 6f 6e 76 65 72 73 69 6f 6e 20 61 6c 67 6f 72  conversion algor
7a270 69 74 68 6d 73 20 61 72 65 20 69 6d 70 6c 65 6d  ithms are implem
7a280 65 6e 74 65 64 20 62 61 73 65 64 20 6f 6e 20 64  ented based on d
7a290 65 73 63 72 69 70 74 69 6f 6e 73 0a 2a 2a 20 69  escriptions.** i
7a2a0 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
7a2b0 74 65 78 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  text:.**.**     
7a2c0 20 4a 65 61 6e 20 4d 65 65 75 73 0a 2a 2a 20 20   Jean Meeus.**  
7a2d0 20 20 20 20 41 73 74 72 6f 6e 6f 6d 69 63 61 6c      Astronomical
7a2e0 20 41 6c 67 6f 72 69 74 68 6d 73 2c 20 32 6e 64   Algorithms, 2nd
7a2f0 20 45 64 69 74 69 6f 6e 2c 20 31 39 39 38 0a 2a   Edition, 1998.*
7a300 2a 20 20 20 20 20 20 49 53 42 4d 20 30 2d 39 34  *      ISBM 0-94
7a310 33 33 39 36 2d 36 31 2d 31 0a 2a 2a 20 20 20 20  3396-61-1.**    
7a320 20 20 57 69 6c 6c 6d 61 6e 6e 2d 42 65 6c 6c 2c    Willmann-Bell,
7a330 20 49 6e 63 0a 2a 2a 20 20 20 20 20 20 52 69 63   Inc.**      Ric
7a340 68 6d 6f 6e 64 2c 20 56 69 72 67 69 6e 69 61 20  hmond, Virginia 
7a350 28 55 53 41 29 0a 2a 2f 0a 23 69 6e 63 6c 75 64  (USA).*/.#includ
7a360 65 20 3c 74 69 6d 65 2e 68 3e 0a 0a 23 69 66 6e  e <time.h>..#ifn
7a370 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
7a380 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 0a  DATETIME_FUNCS..
7a390 2f 2a 0a 2a 2a 20 4f 6e 20 72 65 63 65 6e 74 20  /*.** On recent 
7a3a0 57 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d  Windows platform
7a3b0 73 2c 20 74 68 65 20 6c 6f 63 61 6c 74 69 6d 65  s, the localtime
7a3c0 5f 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73  _s() function is
7a3d0 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 61 73   available.** as
7a3e0 20 70 61 72 74 20 6f 66 20 74 68 65 20 22 53 65   part of the "Se
7a3f0 63 75 72 65 20 43 52 54 22 2e 20 49 74 20 69 73  cure CRT". It is
7a400 20 65 73 73 65 6e 74 69 61 6c 6c 79 20 65 71 75   essentially equ
7a410 69 76 61 6c 65 6e 74 20 74 6f 20 0a 2a 2a 20 6c  ivalent to .** l
7a420 6f 63 61 6c 74 69 6d 65 5f 72 28 29 20 61 76 61  ocaltime_r() ava
7a430 69 6c 61 62 6c 65 20 75 6e 64 65 72 20 6d 6f 73  ilable under mos
7a440 74 20 50 4f 53 49 58 20 70 6c 61 74 66 6f 72 6d  t POSIX platform
7a450 73 2c 20 65 78 63 65 70 74 20 74 68 61 74 20 74  s, except that t
7a460 68 65 20 0a 2a 2a 20 6f 72 64 65 72 20 6f 66 20  he .** order of 
7a470 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 69  the parameters i
7a480 73 20 72 65 76 65 72 73 65 64 2e 0a 2a 2a 0a 2a  s reversed..**.*
7a490 2a 20 53 65 65 20 68 74 74 70 3a 2f 2f 6d 73 64  * See http://msd
7a4a0 6e 2e 6d 69 63 72 6f 73 6f 66 74 2e 63 6f 6d 2f  n.microsoft.com/
7a4b0 65 6e 2d 75 73 2f 6c 69 62 72 61 72 79 2f 61 34  en-us/library/a4
7a4c0 34 32 78 33 79 65 28 56 53 2e 38 30 29 2e 61 73  42x3ye(VS.80).as
7a4d0 70 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  px..**.** If the
7a4e0 20 75 73 65 72 20 68 61 73 20 6e 6f 74 20 69 6e   user has not in
7a4f0 64 69 63 61 74 65 64 20 74 6f 20 75 73 65 20 6c  dicated to use l
7a500 6f 63 61 6c 74 69 6d 65 5f 72 28 29 20 6f 72 20  ocaltime_r() or 
7a510 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 0a 2a 2a  localtime_s().**
7a520 20 61 6c 72 65 61 64 79 2c 20 63 68 65 63 6b 20   already, check 
7a530 66 6f 72 20 61 6e 20 4d 53 56 43 20 62 75 69 6c  for an MSVC buil
7a540 64 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 74 68  d environment th
7a550 61 74 20 70 72 6f 76 69 64 65 73 20 0a 2a 2a 20  at provides .** 
7a560 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 2e 0a 2a  localtime_s()..*
7a570 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 48  /.#if !defined(H
7a580 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 29  AVE_LOCALTIME_R)
7a590 20 26 26 20 21 64 65 66 69 6e 65 64 28 48 41 56   && !defined(HAV
7a5a0 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 20 26  E_LOCALTIME_S) &
7a5b0 26 20 5c 0a 20 20 20 20 20 64 65 66 69 6e 65 64  & \.     defined
7a5c0 28 5f 4d 53 43 5f 56 45 52 29 20 26 26 20 64 65  (_MSC_VER) && de
7a5d0 66 69 6e 65 64 28 5f 43 52 54 5f 49 4e 53 45 43  fined(_CRT_INSEC
7a5e0 55 52 45 5f 44 45 50 52 45 43 41 54 45 29 0a 23  URE_DEPRECATE).#
7a5f0 64 65 66 69 6e 65 20 48 41 56 45 5f 4c 4f 43 41  define HAVE_LOCA
7a600 4c 54 49 4d 45 5f 53 20 31 0a 23 65 6e 64 69 66  LTIME_S 1.#endif
7a610 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63 74  ../*.** A struct
7a620 75 72 65 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20  ure for holding 
7a630 61 20 73 69 6e 67 6c 65 20 64 61 74 65 20 61 6e  a single date an
7a640 64 20 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64  d time..*/.typed
7a650 65 66 20 73 74 72 75 63 74 20 44 61 74 65 54 69  ef struct DateTi
7a660 6d 65 20 44 61 74 65 54 69 6d 65 3b 0a 73 74 72  me DateTime;.str
7a670 75 63 74 20 44 61 74 65 54 69 6d 65 20 7b 0a 20  uct DateTime {. 
7a680 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
7a690 4a 44 3b 20 2f 2a 20 54 68 65 20 6a 75 6c 69 61  JD; /* The julia
7a6a0 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 74 69 6d  n day number tim
7a6b0 65 73 20 38 36 34 30 30 30 30 30 20 2a 2f 0a 20  es 86400000 */. 
7a6c0 20 69 6e 74 20 59 2c 20 4d 2c 20 44 3b 20 20 20   int Y, M, D;   
7a6d0 20 20 20 20 2f 2a 20 59 65 61 72 2c 20 6d 6f 6e      /* Year, mon
7a6e0 74 68 2c 20 61 6e 64 20 64 61 79 20 2a 2f 0a 20  th, and day */. 
7a6f0 20 69 6e 74 20 68 2c 20 6d 3b 20 20 20 20 20 20   int h, m;      
7a700 20 20 20 20 2f 2a 20 48 6f 75 72 20 61 6e 64 20      /* Hour and 
7a710 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 69 6e 74  minutes */.  int
7a720 20 74 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   tz;            
7a730 2f 2a 20 54 69 6d 65 7a 6f 6e 65 20 6f 66 66 73  /* Timezone offs
7a740 65 74 20 69 6e 20 6d 69 6e 75 74 65 73 20 2a 2f  et in minutes */
7a750 0a 20 20 64 6f 75 62 6c 65 20 73 3b 20 20 20 20  .  double s;    
7a760 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 73        /* Seconds
7a770 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64   */.  char valid
7a780 59 4d 44 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  YMD;     /* True
7a790 20 28 31 29 20 69 66 20 59 2c 4d 2c 44 20 61 72   (1) if Y,M,D ar
7a7a0 65 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61  e valid */.  cha
7a7b0 72 20 76 61 6c 69 64 48 4d 53 3b 20 20 20 20 20  r validHMS;     
7a7c0 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 68  /* True (1) if h
7a7d0 2c 6d 2c 73 20 61 72 65 20 76 61 6c 69 64 20 2a  ,m,s are valid *
7a7e0 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 4a 44  /.  char validJD
7a7f0 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28  ;      /* True (
7a800 31 29 20 69 66 20 69 4a 44 20 69 73 20 76 61 6c  1) if iJD is val
7a810 69 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c  id */.  char val
7a820 69 64 54 5a 3b 20 20 20 20 20 20 2f 2a 20 54 72  idTZ;      /* Tr
7a830 75 65 20 28 31 29 20 69 66 20 74 7a 20 69 73 20  ue (1) if tz is 
7a840 76 61 6c 69 64 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a  valid */.};.../*
7a850 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 7a 44 61 74  .** Convert zDat
7a860 65 20 69 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f  e into one or mo
7a870 72 65 20 69 6e 74 65 67 65 72 73 2e 20 20 41 64  re integers.  Ad
7a880 64 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e  ditional argumen
7a890 74 73 0a 2a 2a 20 63 6f 6d 65 20 69 6e 20 67 72  ts.** come in gr
7a8a0 6f 75 70 73 20 6f 66 20 35 20 61 73 20 66 6f 6c  oups of 5 as fol
7a8b0 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lows:.**.**     
7a8c0 20 20 4e 20 20 20 20 20 20 20 6e 75 6d 62 65 72    N       number
7a8d0 20 6f 66 20 64 69 67 69 74 73 20 69 6e 20 74 68   of digits in th
7a8e0 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  e integer.**    
7a8f0 20 20 20 6d 69 6e 20 20 20 20 20 6d 69 6e 69 6d     min     minim
7a900 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65  um allowed value
7a910 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a   of the integer.
7a920 2a 2a 20 20 20 20 20 20 20 6d 61 78 20 20 20 20  **       max    
7a930 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64   maximum allowed
7a940 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e   value of the in
7a950 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6e  teger.**       n
7a960 65 78 74 43 20 20 20 66 69 72 73 74 20 63 68 61  extC   first cha
7a970 72 61 63 74 65 72 20 61 66 74 65 72 20 74 68 65  racter after the
7a980 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20   integer.**     
7a990 20 20 70 56 61 6c 20 20 20 20 77 68 65 72 65 20    pVal    where 
7a9a0 74 6f 20 77 72 69 74 65 20 74 68 65 20 69 6e 74  to write the int
7a9b0 65 67 65 72 73 20 76 61 6c 75 65 2e 0a 2a 2a 0a  egers value..**.
7a9c0 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 63  ** Conversions c
7a9d0 6f 6e 74 69 6e 75 65 20 75 6e 74 69 6c 20 6f 6e  ontinue until on
7a9e0 65 20 77 69 74 68 20 6e 65 78 74 43 3d 3d 30 20  e with nextC==0 
7a9f0 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a  is encountered..
7aa00 2a 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  ** The function 
7aa10 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
7aa20 65 72 20 6f 66 20 73 75 63 63 65 73 73 66 75 6c  er of successful
7aa30 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 0a 2a 2f   conversions..*/
7aa40 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 44  .static int getD
7aa50 69 67 69 74 73 28 63 6f 6e 73 74 20 63 68 61 72  igits(const char
7aa60 20 2a 7a 44 61 74 65 2c 20 2e 2e 2e 29 7b 0a 20   *zDate, ...){. 
7aa70 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69   va_list ap;.  i
7aa80 6e 74 20 76 61 6c 3b 0a 20 20 69 6e 74 20 4e 3b  nt val;.  int N;
7aa90 0a 20 20 69 6e 74 20 6d 69 6e 3b 0a 20 20 69 6e  .  int min;.  in
7aaa0 74 20 6d 61 78 3b 0a 20 20 69 6e 74 20 6e 65 78  t max;.  int nex
7aab0 74 43 3b 0a 20 20 69 6e 74 20 2a 70 56 61 6c 3b  tC;.  int *pVal;
7aac0 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a  .  int cnt = 0;.
7aad0 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
7aae0 44 61 74 65 29 3b 0a 20 20 64 6f 7b 0a 20 20 20  Date);.  do{.   
7aaf0 20 4e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20   N = va_arg(ap, 
7ab00 69 6e 74 29 3b 0a 20 20 20 20 6d 69 6e 20 3d 20  int);.    min = 
7ab10 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
7ab20 0a 20 20 20 20 6d 61 78 20 3d 20 76 61 5f 61 72  .    max = va_ar
7ab30 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
7ab40 6e 65 78 74 43 20 3d 20 76 61 5f 61 72 67 28 61  nextC = va_arg(a
7ab50 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 70 56 61  p, int);.    pVa
7ab60 6c 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  l = va_arg(ap, i
7ab70 6e 74 2a 29 3b 0a 20 20 20 20 76 61 6c 20 3d 20  nt*);.    val = 
7ab80 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d  0;.    while( N-
7ab90 2d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  - ){.      if( !
7aba0 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
7abb0 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20  zDate) ){.      
7abc0 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69    goto end_getDi
7abd0 67 69 74 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20  gits;.      }.  
7abe0 20 20 20 20 76 61 6c 20 3d 20 76 61 6c 2a 31 30      val = val*10
7abf0 20 2b 20 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b   + *zDate - '0';
7ac00 0a 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a  .      zDate++;.
7ac10 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 76 61      }.    if( va
7ac20 6c 3c 6d 69 6e 20 7c 7c 20 76 61 6c 3e 6d 61 78  l<min || val>max
7ac30 20 7c 7c 20 28 6e 65 78 74 43 21 3d 30 20 26 26   || (nextC!=0 &&
7ac40 20 6e 65 78 74 43 21 3d 2a 7a 44 61 74 65 29 20   nextC!=*zDate) 
7ac50 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  ){.      goto en
7ac60 64 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20  d_getDigits;.   
7ac70 20 7d 0a 20 20 20 20 2a 70 56 61 6c 20 3d 20 76   }.    *pVal = v
7ac80 61 6c 3b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b  al;.    zDate++;
7ac90 0a 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 7d 77  .    cnt++;.  }w
7aca0 68 69 6c 65 28 20 6e 65 78 74 43 20 29 3b 0a 65  hile( nextC );.e
7acb0 6e 64 5f 67 65 74 44 69 67 69 74 73 3a 0a 20 20  nd_getDigits:.  
7acc0 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65  va_end(ap);.  re
7acd0 74 75 72 6e 20 63 6e 74 3b 0a 7d 0a 0a 2f 2a 0a  turn cnt;.}../*.
7ace0 2a 2a 20 52 65 61 64 20 74 65 78 74 20 66 72 6f  ** Read text fro
7acf0 6d 20 7a 5b 5d 20 61 6e 64 20 63 6f 6e 76 65 72  m z[] and conver
7ad00 74 20 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e  t into a floatin
7ad10 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 20  g point number. 
7ad20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 6e   Return.** the n
7ad30 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74 73 20  umber of digits 
7ad40 63 6f 6e 76 65 72 74 65 64 2e 0a 2a 2f 0a 23 64  converted..*/.#d
7ad50 65 66 69 6e 65 20 67 65 74 56 61 6c 75 65 20 73  efine getValue s
7ad60 71 6c 69 74 65 33 41 74 6f 46 0a 0a 2f 2a 0a 2a  qlite3AtoF../*.*
7ad70 2a 20 50 61 72 73 65 20 61 20 74 69 6d 65 7a 6f  * Parse a timezo
7ad80 6e 65 20 65 78 74 65 6e 73 69 6f 6e 20 6f 6e 20  ne extension on 
7ad90 74 68 65 20 65 6e 64 20 6f 66 20 61 20 64 61 74  the end of a dat
7ada0 65 2d 74 69 6d 65 2e 0a 2a 2a 20 54 68 65 20 65  e-time..** The e
7adb0 78 74 65 6e 73 69 6f 6e 20 69 73 20 6f 66 20 74  xtension is of t
7adc0 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20  he form:.**.**  
7add0 20 20 20 20 20 20 28 2b 2f 2d 29 48 48 3a 4d 4d        (+/-)HH:MM
7ade0 0a 2a 2a 0a 2a 2a 20 4f 72 20 74 68 65 20 22 7a  .**.** Or the "z
7adf0 75 6c 75 22 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a  ulu" notation:.*
7ae00 2a 0a 2a 2a 20 20 20 20 20 20 20 20 5a 0a 2a 2a  *.**        Z.**
7ae10 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 73 65  .** If the parse
7ae20 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
7ae30 77 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72  write the number
7ae40 20 6f 66 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 6f   of minutes.** o
7ae50 66 20 63 68 61 6e 67 65 20 69 6e 20 70 2d 3e 74  f change in p->t
7ae60 7a 20 61 6e 64 20 72 65 74 75 72 6e 20 30 2e 20  z and return 0. 
7ae70 20 49 66 20 61 20 70 61 72 73 65 72 20 65 72 72   If a parser err
7ae80 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 72 65  or occurs,.** re
7ae90 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a  turn non-zero..*
7aea0 2a 0a 2a 2a 20 41 20 6d 69 73 73 69 6e 67 20 73  *.** A missing s
7aeb0 70 65 63 69 66 69 65 72 20 69 73 20 6e 6f 74 20  pecifier is not 
7aec0 63 6f 6e 73 69 64 65 72 65 64 20 61 6e 20 65 72  considered an er
7aed0 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ror..*/.static i
7aee0 6e 74 20 70 61 72 73 65 54 69 6d 65 7a 6f 6e 65  nt parseTimezone
7aef0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61  (const char *zDa
7af00 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29  te, DateTime *p)
7af10 7b 0a 20 20 69 6e 74 20 73 67 6e 20 3d 20 30 3b  {.  int sgn = 0;
7af20 0a 20 20 69 6e 74 20 6e 48 72 2c 20 6e 4d 6e 3b  .  int nHr, nMn;
7af30 0a 20 20 69 6e 74 20 63 3b 0a 20 20 77 68 69 6c  .  int c;.  whil
7af40 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63  e( sqlite3Isspac
7af50 65 28 2a 7a 44 61 74 65 29 20 29 7b 20 7a 44 61  e(*zDate) ){ zDa
7af60 74 65 2b 2b 3b 20 7d 0a 20 20 70 2d 3e 74 7a 20  te++; }.  p->tz 
7af70 3d 20 30 3b 0a 20 20 63 20 3d 20 2a 7a 44 61 74  = 0;.  c = *zDat
7af80 65 3b 0a 20 20 69 66 28 20 63 3d 3d 27 2d 27 20  e;.  if( c=='-' 
7af90 29 7b 0a 20 20 20 20 73 67 6e 20 3d 20 2d 31 3b  ){.    sgn = -1;
7afa0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d  .  }else if( c==
7afb0 27 2b 27 20 29 7b 0a 20 20 20 20 73 67 6e 20 3d  '+' ){.    sgn =
7afc0 20 2b 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   +1;.  }else if(
7afd0 20 63 3d 3d 27 5a 27 20 7c 7c 20 63 3d 3d 27 7a   c=='Z' || c=='z
7afe0 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b  ' ){.    zDate++
7aff0 3b 0a 20 20 20 20 67 6f 74 6f 20 7a 75 6c 75 5f  ;.    goto zulu_
7b000 74 69 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  time;.  }else{. 
7b010 20 20 20 72 65 74 75 72 6e 20 63 21 3d 30 3b 0a     return c!=0;.
7b020 20 20 7d 0a 20 20 7a 44 61 74 65 2b 2b 3b 0a 20    }.  zDate++;. 
7b030 20 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a   if( getDigits(z
7b040 44 61 74 65 2c 20 32 2c 20 30 2c 20 31 34 2c 20  Date, 2, 0, 14, 
7b050 27 3a 27 2c 20 26 6e 48 72 2c 20 32 2c 20 30 2c  ':', &nHr, 2, 0,
7b060 20 35 39 2c 20 30 2c 20 26 6e 4d 6e 29 21 3d 32   59, 0, &nMn)!=2
7b070 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
7b080 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d  ;.  }.  zDate +=
7b090 20 35 3b 0a 20 20 70 2d 3e 74 7a 20 3d 20 73 67   5;.  p->tz = sg
7b0a0 6e 2a 28 6e 4d 6e 20 2b 20 6e 48 72 2a 36 30 29  n*(nMn + nHr*60)
7b0b0 3b 0a 7a 75 6c 75 5f 74 69 6d 65 3a 0a 20 20 77  ;.zulu_time:.  w
7b0c0 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73  hile( sqlite3Iss
7b0d0 70 61 63 65 28 2a 7a 44 61 74 65 29 20 29 7b 20  pace(*zDate) ){ 
7b0e0 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 72 65 74  zDate++; }.  ret
7b0f0 75 72 6e 20 2a 7a 44 61 74 65 21 3d 30 3b 0a 7d  urn *zDate!=0;.}
7b100 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 74 69  ../*.** Parse ti
7b110 6d 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  mes of the form 
7b120 48 48 3a 4d 4d 20 6f 72 20 48 48 3a 4d 4d 3a 53  HH:MM or HH:MM:S
7b130 53 20 6f 72 20 48 48 3a 4d 4d 3a 53 53 2e 46 46  S or HH:MM:SS.FF
7b140 46 46 2e 0a 2a 2a 20 54 68 65 20 48 48 2c 20 4d  FF..** The HH, M
7b150 4d 2c 20 61 6e 64 20 53 53 20 6d 75 73 74 20 65  M, and SS must e
7b160 61 63 68 20 62 65 20 65 78 61 63 74 6c 79 20 32  ach be exactly 2
7b170 20 64 69 67 69 74 73 2e 20 20 54 68 65 0a 2a 2a   digits.  The.**
7b180 20 66 72 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f   fractional seco
7b190 6e 64 73 20 46 46 46 46 20 63 61 6e 20 62 65 20  nds FFFF can be 
7b1a0 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69  one or more digi
7b1b0 74 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ts..**.** Return
7b1c0 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61   1 if there is a
7b1d0 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 61   parsing error a
7b1e0 6e 64 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2e  nd 0 on success.
7b1f0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
7b200 61 72 73 65 48 68 4d 6d 53 73 28 63 6f 6e 73 74  arseHhMmSs(const
7b210 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61   char *zDate, Da
7b220 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e  teTime *p){.  in
7b230 74 20 68 2c 20 6d 2c 20 73 3b 0a 20 20 64 6f 75  t h, m, s;.  dou
7b240 62 6c 65 20 6d 73 20 3d 20 30 2e 30 3b 0a 20 20  ble ms = 0.0;.  
7b250 69 66 28 20 67 65 74 44 69 67 69 74 73 28 7a 44  if( getDigits(zD
7b260 61 74 65 2c 20 32 2c 20 30 2c 20 32 34 2c 20 27  ate, 2, 0, 24, '
7b270 3a 27 2c 20 26 68 2c 20 32 2c 20 30 2c 20 35 39  :', &h, 2, 0, 59
7b280 2c 20 30 2c 20 26 6d 29 21 3d 32 20 29 7b 0a 20  , 0, &m)!=2 ){. 
7b290 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
7b2a0 0a 20 20 7a 44 61 74 65 20 2b 3d 20 35 3b 0a 20  .  zDate += 5;. 
7b2b0 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 3a 27   if( *zDate==':'
7b2c0 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b 3b   ){.    zDate++;
7b2d0 0a 20 20 20 20 69 66 28 20 67 65 74 44 69 67 69  .    if( getDigi
7b2e0 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20  ts(zDate, 2, 0, 
7b2f0 35 39 2c 20 30 2c 20 26 73 29 21 3d 31 20 29 7b  59, 0, &s)!=1 ){
7b300 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
7b310 0a 20 20 20 20 7d 0a 20 20 20 20 7a 44 61 74 65  .    }.    zDate
7b320 20 2b 3d 20 32 3b 0a 20 20 20 20 69 66 28 20 2a   += 2;.    if( *
7b330 7a 44 61 74 65 3d 3d 27 2e 27 20 26 26 20 73 71  zDate=='.' && sq
7b340 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 44 61  lite3Isdigit(zDa
7b350 74 65 5b 31 5d 29 20 29 7b 0a 20 20 20 20 20 20  te[1]) ){.      
7b360 64 6f 75 62 6c 65 20 72 53 63 61 6c 65 20 3d 20  double rScale = 
7b370 31 2e 30 3b 0a 20 20 20 20 20 20 7a 44 61 74 65  1.0;.      zDate
7b380 2b 2b 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28  ++;.      while(
7b390 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
7b3a0 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20  *zDate) ){.     
7b3b0 20 20 20 6d 73 20 3d 20 6d 73 2a 31 30 2e 30 20     ms = ms*10.0 
7b3c0 2b 20 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a  + *zDate - '0';.
7b3d0 20 20 20 20 20 20 20 20 72 53 63 61 6c 65 20 2a          rScale *
7b3e0 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 20  = 10.0;.        
7b3f0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20 7d  zDate++;.      }
7b400 0a 20 20 20 20 20 20 6d 73 20 2f 3d 20 72 53 63  .      ms /= rSc
7b410 61 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ale;.    }.  }el
7b420 73 65 7b 0a 20 20 20 20 73 20 3d 20 30 3b 0a 20  se{.    s = 0;. 
7b430 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20   }.  p->validJD 
7b440 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48  = 0;.  p->validH
7b450 4d 53 20 3d 20 31 3b 0a 20 20 70 2d 3e 68 20 3d  MS = 1;.  p->h =
7b460 20 68 3b 0a 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a   h;.  p->m = m;.
7b470 20 20 70 2d 3e 73 20 3d 20 73 20 2b 20 6d 73 3b    p->s = s + ms;
7b480 0a 20 20 69 66 28 20 70 61 72 73 65 54 69 6d 65  .  if( parseTime
7b490 7a 6f 6e 65 28 7a 44 61 74 65 2c 20 70 29 20 29  zone(zDate, p) )
7b4a0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 70 2d 3e   return 1;.  p->
7b4b0 76 61 6c 69 64 54 5a 20 3d 20 28 70 2d 3e 74 7a  validTZ = (p->tz
7b4c0 21 3d 30 29 3f 31 3a 30 3b 0a 20 20 72 65 74 75  !=0)?1:0;.  retu
7b4d0 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  rn 0;.}../*.** C
7b4e0 6f 6e 76 65 72 74 20 66 72 6f 6d 20 59 59 59 59  onvert from YYYY
7b4f0 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 20  -MM-DD HH:MM:SS 
7b500 74 6f 20 6a 75 6c 69 61 6e 20 64 61 79 2e 20 20  to julian day.  
7b510 57 65 20 61 6c 77 61 79 73 20 61 73 73 75 6d 65  We always assume
7b520 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 59 59 59  .** that the YYY
7b530 59 2d 4d 4d 2d 44 44 20 69 73 20 61 63 63 6f 72  Y-MM-DD is accor
7b540 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72 65 67  ding to the Greg
7b550 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 0a  orian calendar..
7b560 2a 2a 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 3a  **.** Reference:
7b570 20 20 4d 65 65 75 73 20 70 61 67 65 20 36 31 0a    Meeus page 61.
7b580 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
7b590 6f 6d 70 75 74 65 4a 44 28 44 61 74 65 54 69 6d  omputeJD(DateTim
7b5a0 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59 2c 20  e *p){.  int Y, 
7b5b0 4d 2c 20 44 2c 20 41 2c 20 42 2c 20 58 31 2c 20  M, D, A, B, X1, 
7b5c0 58 32 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 76 61  X2;..  if( p->va
7b5d0 6c 69 64 4a 44 20 29 20 72 65 74 75 72 6e 3b 0a  lidJD ) return;.
7b5e0 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d    if( p->validYM
7b5f0 44 20 29 7b 0a 20 20 20 20 59 20 3d 20 70 2d 3e  D ){.    Y = p->
7b600 59 3b 0a 20 20 20 20 4d 20 3d 20 70 2d 3e 4d 3b  Y;.    M = p->M;
7b610 0a 20 20 20 20 44 20 3d 20 70 2d 3e 44 3b 0a 20  .    D = p->D;. 
7b620 20 7d 65 6c 73 65 7b 0a 20 20 20 20 59 20 3d 20   }else{.    Y = 
7b630 32 30 30 30 3b 20 20 2f 2a 20 49 66 20 6e 6f 20  2000;  /* If no 
7b640 59 4d 44 20 73 70 65 63 69 66 69 65 64 2c 20 61  YMD specified, a
7b650 73 73 75 6d 65 20 32 30 30 30 2d 4a 61 6e 2d 30  ssume 2000-Jan-0
7b660 31 20 2a 2f 0a 20 20 20 20 4d 20 3d 20 31 3b 0a  1 */.    M = 1;.
7b670 20 20 20 20 44 20 3d 20 31 3b 0a 20 20 7d 0a 20      D = 1;.  }. 
7b680 20 69 66 28 20 4d 3c 3d 32 20 29 7b 0a 20 20 20   if( M<=2 ){.   
7b690 20 59 2d 2d 3b 0a 20 20 20 20 4d 20 2b 3d 20 31   Y--;.    M += 1
7b6a0 32 3b 0a 20 20 7d 0a 20 20 41 20 3d 20 59 2f 31  2;.  }.  A = Y/1
7b6b0 30 30 3b 0a 20 20 42 20 3d 20 32 20 2d 20 41 20  00;.  B = 2 - A 
7b6c0 2b 20 28 41 2f 34 29 3b 0a 20 20 58 31 20 3d 20  + (A/4);.  X1 = 
7b6d0 33 36 35 32 35 2a 28 59 2b 34 37 31 36 29 2f 31  36525*(Y+4716)/1
7b6e0 30 30 3b 0a 20 20 58 32 20 3d 20 33 30 36 30 30  00;.  X2 = 30600
7b6f0 31 2a 28 4d 2b 31 29 2f 31 30 30 30 30 3b 0a 20  1*(M+1)/10000;. 
7b700 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74   p->iJD = (sqlit
7b710 65 33 5f 69 6e 74 36 34 29 28 28 58 31 20 2b 20  e3_int64)((X1 + 
7b720 58 32 20 2b 20 44 20 2b 20 42 20 2d 20 31 35 32  X2 + D + B - 152
7b730 34 2e 35 20 29 20 2a 20 38 36 34 30 30 30 30 30  4.5 ) * 86400000
7b740 29 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20  );.  p->validJD 
7b750 3d 20 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61  = 1;.  if( p->va
7b760 6c 69 64 48 4d 53 20 29 7b 0a 20 20 20 20 70 2d  lidHMS ){.    p-
7b770 3e 69 4a 44 20 2b 3d 20 70 2d 3e 68 2a 33 36 30  >iJD += p->h*360
7b780 30 30 30 30 20 2b 20 70 2d 3e 6d 2a 36 30 30 30  0000 + p->m*6000
7b790 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  0 + (sqlite3_int
7b7a0 36 34 29 28 70 2d 3e 73 2a 31 30 30 30 29 3b 0a  64)(p->s*1000);.
7b7b0 20 20 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64      if( p->valid
7b7c0 54 5a 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69  TZ ){.      p->i
7b7d0 4a 44 20 2d 3d 20 70 2d 3e 74 7a 2a 36 30 30 30  JD -= p->tz*6000
7b7e0 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  0;.      p->vali
7b7f0 64 59 4d 44 20 3d 20 30 3b 0a 20 20 20 20 20 20  dYMD = 0;.      
7b800 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b  p->validHMS = 0;
7b810 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 54  .      p->validT
7b820 5a 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  Z = 0;.    }.  }
7b830 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20  .}../*.** Parse 
7b840 64 61 74 65 73 20 6f 66 20 74 68 65 20 66 6f 72  dates of the for
7b850 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 59 59 59 59  m.**.**     YYYY
7b860 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e  -MM-DD HH:MM:SS.
7b870 46 46 46 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d  FFF.**     YYYY-
7b880 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a  MM-DD HH:MM:SS.*
7b890 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44  *     YYYY-MM-DD
7b8a0 20 48 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20 59 59   HH:MM.**     YY
7b8b0 59 59 2d 4d 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57  YY-MM-DD.**.** W
7b8c0 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20  rite the result 
7b8d0 69 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d  into the DateTim
7b8e0 65 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  e structure and 
7b8f0 72 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73  return 0.** on s
7b900 75 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20  uccess and 1 if 
7b910 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67  the input string
7b920 20 69 73 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66   is not a well-f
7b930 6f 72 6d 65 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a  ormed.** date..*
7b940 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72  /.static int par
7b950 73 65 59 79 79 79 4d 6d 44 64 28 63 6f 6e 73 74  seYyyyMmDd(const
7b960 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61   char *zDate, Da
7b970 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e  teTime *p){.  in
7b980 74 20 59 2c 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a  t Y, M, D, neg;.
7b990 0a 20 20 69 66 28 20 7a 44 61 74 65 5b 30 5d 3d  .  if( zDate[0]=
7b9a0 3d 27 2d 27 20 29 7b 0a 20 20 20 20 7a 44 61 74  ='-' ){.    zDat
7b9b0 65 2b 2b 3b 0a 20 20 20 20 6e 65 67 20 3d 20 31  e++;.    neg = 1
7b9c0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
7b9d0 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  eg = 0;.  }.  if
7b9e0 28 20 67 65 74 44 69 67 69 74 73 28 7a 44 61 74  ( getDigits(zDat
7b9f0 65 2c 34 2c 30 2c 39 39 39 39 2c 27 2d 27 2c 26  e,4,0,9999,'-',&
7ba00 59 2c 32 2c 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c  Y,2,1,12,'-',&M,
7ba10 32 2c 31 2c 33 31 2c 30 2c 26 44 29 21 3d 33 20  2,1,31,0,&D)!=3 
7ba20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
7ba30 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20  .  }.  zDate += 
7ba40 31 30 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c  10;.  while( sql
7ba50 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61  ite3Isspace(*zDa
7ba60 74 65 29 20 7c 7c 20 27 54 27 3d 3d 2a 28 75 38  te) || 'T'==*(u8
7ba70 2a 29 7a 44 61 74 65 20 29 7b 20 7a 44 61 74 65  *)zDate ){ zDate
7ba80 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 70 61 72 73  ++; }.  if( pars
7ba90 65 48 68 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70  eHhMmSs(zDate, p
7baa0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57  )==0 ){.    /* W
7bab0 65 20 67 6f 74 20 74 68 65 20 74 69 6d 65 20 2a  e got the time *
7bac0 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a  /.  }else if( *z
7bad0 44 61 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 70  Date==0 ){.    p
7bae0 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a  ->validHMS = 0;.
7baf0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
7bb00 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 70 2d 3e  urn 1;.  }.  p->
7bb10 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 70  validJD = 0;.  p
7bb20 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a  ->validYMD = 1;.
7bb30 20 20 70 2d 3e 59 20 3d 20 6e 65 67 20 3f 20 2d    p->Y = neg ? -
7bb40 59 20 3a 20 59 3b 0a 20 20 70 2d 3e 4d 20 3d 20  Y : Y;.  p->M = 
7bb50 4d 3b 0a 20 20 70 2d 3e 44 20 3d 20 44 3b 0a 20  M;.  p->D = D;. 
7bb60 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20   if( p->validTZ 
7bb70 29 7b 0a 20 20 20 20 63 6f 6d 70 75 74 65 4a 44  ){.    computeJD
7bb80 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  (p);.  }.  retur
7bb90 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  n 0;.}../*.** Se
7bba0 74 20 74 68 65 20 74 69 6d 65 20 74 6f 20 74 68  t the time to th
7bbb0 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20 72  e current time r
7bbc0 65 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 56  eported by the V
7bbd0 46 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  FS.*/.static voi
7bbe0 64 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f 43  d setDateTimeToC
7bbf0 75 72 72 65 6e 74 28 73 71 6c 69 74 65 33 5f 63  urrent(sqlite3_c
7bc00 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
7bc10 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20   DateTime *p){. 
7bc20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 73 71 6c   double r;.  sql
7bc30 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
7bc40 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
7bc50 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ndle(context);. 
7bc60 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
7bc70 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20  tTime(db->pVfs, 
7bc80 26 72 29 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d 20  &r);.  p->iJD = 
7bc90 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28  (sqlite3_int64)(
7bca0 72 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30  r*86400000.0 + 0
7bcb0 2e 35 29 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 4a  .5);.  p->validJ
7bcc0 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  D = 1;.}../*.** 
7bcd0 41 74 74 65 6d 70 74 20 74 6f 20 70 61 72 73 65  Attempt to parse
7bce0 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e   the given strin
7bcf0 67 20 69 6e 74 6f 20 61 20 4a 75 6c 69 61 6e 20  g into a Julian 
7bd00 44 61 79 20 4e 75 6d 62 65 72 2e 20 20 52 65 74  Day Number.  Ret
7bd10 75 72 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  urn.** the numbe
7bd20 72 20 6f 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a  r of errors..**.
7bd30 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
7bd40 20 61 72 65 20 61 63 63 65 70 74 61 62 6c 65 20   are acceptable 
7bd50 66 6f 72 6d 73 20 66 6f 72 20 74 68 65 20 69 6e  forms for the in
7bd60 70 75 74 20 73 74 72 69 6e 67 3a 0a 2a 2a 0a 2a  put string:.**.*
7bd70 2a 20 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44  *      YYYY-MM-D
7bd80 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46 20 20  D HH:MM:SS.FFF  
7bd90 2b 2f 2d 48 48 3a 4d 4d 0a 2a 2a 20 20 20 20 20  +/-HH:MM.**     
7bda0 20 44 44 44 44 2e 44 44 20 0a 2a 2a 20 20 20 20   DDDD.DD .**    
7bdb0 20 20 6e 6f 77 0a 2a 2a 0a 2a 2a 20 49 6e 20 74    now.**.** In t
7bdc0 68 65 20 66 69 72 73 74 20 66 6f 72 6d 2c 20 74  he first form, t
7bdd0 68 65 20 2b 2f 2d 48 48 3a 4d 4d 20 69 73 20 61  he +/-HH:MM is a
7bde0 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20  lways optional. 
7bdf0 20 54 68 65 20 66 72 61 63 74 69 6f 6e 61 6c 0a   The fractional.
7be00 2a 2a 20 73 65 63 6f 6e 64 73 20 65 78 74 65 6e  ** seconds exten
7be10 73 69 6f 6e 20 28 74 68 65 20 22 2e 46 46 46 22  sion (the ".FFF"
7be20 29 20 69 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20  ) is optional.  
7be30 54 68 65 20 73 65 63 6f 6e 64 73 20 70 6f 72 74  The seconds port
7be40 69 6f 6e 0a 2a 2a 20 28 22 3a 53 53 2e 46 46 46  ion.** (":SS.FFF
7be50 22 29 20 69 73 20 6f 70 74 69 6f 6e 2e 20 20 54  ") is option.  T
7be60 68 65 20 79 65 61 72 20 61 6e 64 20 64 61 74 65  he year and date
7be70 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 20   can be omitted 
7be80 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68  as long.** as th
7be90 65 72 65 20 69 73 20 61 20 74 69 6d 65 20 73 74  ere is a time st
7bea0 72 69 6e 67 2e 20 20 54 68 65 20 74 69 6d 65 20  ring.  The time 
7beb0 73 74 72 69 6e 67 20 63 61 6e 20 62 65 20 6f 6d  string can be om
7bec0 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a 2a  itted as long.**
7bed0 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20 79   as there is a y
7bee0 65 61 72 20 61 6e 64 20 64 61 74 65 2e 0a 2a 2f  ear and date..*/
7bef0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73  .static int pars
7bf00 65 44 61 74 65 4f 72 54 69 6d 65 28 0a 20 20 73  eDateOrTime(.  s
7bf10 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7bf20 63 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73  context, .  cons
7bf30 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 0a  t char *zDate, .
7bf40 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a 29 7b    DateTime *p.){
7bf50 0a 20 20 69 6e 74 20 69 73 52 65 61 6c 4e 75 6d  .  int isRealNum
7bf60 3b 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 66  ;    /* Return f
7bf70 72 6f 6d 20 73 71 6c 69 74 65 33 49 73 4e 75 6d  rom sqlite3IsNum
7bf80 62 65 72 28 29 2e 20 20 4e 6f 74 20 75 73 65 64  ber().  Not used
7bf90 20 2a 2f 0a 20 20 69 66 28 20 70 61 72 73 65 59   */.  if( parseY
7bfa0 79 79 79 4d 6d 44 64 28 7a 44 61 74 65 2c 70 29  yyyMmDd(zDate,p)
7bfb0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
7bfc0 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  n 0;.  }else if(
7bfd0 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a 44 61   parseHhMmSs(zDa
7bfe0 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20 20 20  te, p)==0 ){.   
7bff0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
7c000 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  se if( sqlite3St
7c010 72 49 43 6d 70 28 7a 44 61 74 65 2c 22 6e 6f 77  rICmp(zDate,"now
7c020 22 29 3d 3d 30 29 7b 0a 20 20 20 20 73 65 74 44  ")==0){.    setD
7c030 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74  ateTimeToCurrent
7c040 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20  (context, p);.  
7c050 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65    return 0;.  }e
7c060 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 49  lse if( sqlite3I
7c070 73 4e 75 6d 62 65 72 28 7a 44 61 74 65 2c 20 26  sNumber(zDate, &
7c080 69 73 52 65 61 6c 4e 75 6d 2c 20 53 51 4c 49 54  isRealNum, SQLIT
7c090 45 5f 55 54 46 38 29 20 29 7b 0a 20 20 20 20 64  E_UTF8) ){.    d
7c0a0 6f 75 62 6c 65 20 72 3b 0a 20 20 20 20 67 65 74  ouble r;.    get
7c0b0 56 61 6c 75 65 28 7a 44 61 74 65 2c 20 26 72 29  Value(zDate, &r)
7c0c0 3b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28  ;.    p->iJD = (
7c0d0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72  sqlite3_int64)(r
7c0e0 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e  *86400000.0 + 0.
7c0f0 35 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64  5);.    p->valid
7c100 4a 44 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  JD = 1;.    retu
7c110 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 0;.  }.  retu
7c120 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  rn 1;.}../*.** C
7c130 6f 6d 70 75 74 65 20 74 68 65 20 59 65 61 72 2c  ompute the Year,
7c140 20 4d 6f 6e 74 68 2c 20 61 6e 64 20 44 61 79 20   Month, and Day 
7c150 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20  from the julian 
7c160 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73  day number..*/.s
7c170 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75  tatic void compu
7c180 74 65 59 4d 44 28 44 61 74 65 54 69 6d 65 20 2a  teYMD(DateTime *
7c190 70 29 7b 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20  p){.  int Z, A, 
7c1a0 42 2c 20 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a  B, C, D, E, X1;.
7c1b0 20 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d    if( p->validYM
7c1c0 44 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  D ) return;.  if
7c1d0 28 20 21 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b  ( !p->validJD ){
7c1e0 0a 20 20 20 20 70 2d 3e 59 20 3d 20 32 30 30 30  .    p->Y = 2000
7c1f0 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a  ;.    p->M = 1;.
7c200 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20      p->D = 1;.  
7c210 7d 65 6c 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28  }else{.    Z = (
7c220 69 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b 20 34  int)((p->iJD + 4
7c230 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 30  3200000)/8640000
7c240 30 29 3b 0a 20 20 20 20 41 20 3d 20 28 69 6e 74  0);.    A = (int
7c250 29 28 28 5a 20 2d 20 31 38 36 37 32 31 36 2e 32  )((Z - 1867216.2
7c260 35 29 2f 33 36 35 32 34 2e 32 35 29 3b 0a 20 20  5)/36524.25);.  
7c270 20 20 41 20 3d 20 5a 20 2b 20 31 20 2b 20 41 20    A = Z + 1 + A 
7c280 2d 20 28 41 2f 34 29 3b 0a 20 20 20 20 42 20 3d  - (A/4);.    B =
7c290 20 41 20 2b 20 31 35 32 34 3b 0a 20 20 20 20 43   A + 1524;.    C
7c2a0 20 3d 20 28 69 6e 74 29 28 28 42 20 2d 20 31 32   = (int)((B - 12
7c2b0 32 2e 31 29 2f 33 36 35 2e 32 35 29 3b 0a 20 20  2.1)/365.25);.  
7c2c0 20 20 44 20 3d 20 28 33 36 35 32 35 2a 43 29 2f    D = (36525*C)/
7c2d0 31 30 30 3b 0a 20 20 20 20 45 20 3d 20 28 69 6e  100;.    E = (in
7c2e0 74 29 28 28 42 2d 44 29 2f 33 30 2e 36 30 30 31  t)((B-D)/30.6001
7c2f0 29 3b 0a 20 20 20 20 58 31 20 3d 20 28 69 6e 74  );.    X1 = (int
7c300 29 28 33 30 2e 36 30 30 31 2a 45 29 3b 0a 20 20  )(30.6001*E);.  
7c310 20 20 70 2d 3e 44 20 3d 20 42 20 2d 20 44 20 2d    p->D = B - D -
7c320 20 58 31 3b 0a 20 20 20 20 70 2d 3e 4d 20 3d 20   X1;.    p->M = 
7c330 45 3c 31 34 20 3f 20 45 2d 31 20 3a 20 45 2d 31  E<14 ? E-1 : E-1
7c340 33 3b 0a 20 20 20 20 70 2d 3e 59 20 3d 20 70 2d  3;.    p->Y = p-
7c350 3e 4d 3e 32 20 3f 20 43 20 2d 20 34 37 31 36 20  >M>2 ? C - 4716 
7c360 3a 20 43 20 2d 20 34 37 31 35 3b 0a 20 20 7d 0a  : C - 4715;.  }.
7c370 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20    p->validYMD = 
7c380 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  1;.}../*.** Comp
7c390 75 74 65 20 74 68 65 20 48 6f 75 72 2c 20 4d 69  ute the Hour, Mi
7c3a0 6e 75 74 65 2c 20 61 6e 64 20 53 65 63 6f 6e 64  nute, and Second
7c3b0 73 20 66 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61  s from the julia
7c3c0 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f  n day number..*/
7c3d0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d  .static void com
7c3e0 70 75 74 65 48 4d 53 28 44 61 74 65 54 69 6d 65  puteHMS(DateTime
7c3f0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73 3b 0a 20   *p){.  int s;. 
7c400 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53   if( p->validHMS
7c410 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 63 6f 6d   ) return;.  com
7c420 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 73 20 3d  puteJD(p);.  s =
7c430 20 28 69 6e 74 29 28 28 70 2d 3e 69 4a 44 20 2b   (int)((p->iJD +
7c440 20 34 33 32 30 30 30 30 30 29 20 25 20 38 36 34   43200000) % 864
7c450 30 30 30 30 30 29 3b 0a 20 20 70 2d 3e 73 20 3d  00000);.  p->s =
7c460 20 73 2f 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d   s/1000.0;.  s =
7c470 20 28 69 6e 74 29 70 2d 3e 73 3b 0a 20 20 70 2d   (int)p->s;.  p-
7c480 3e 73 20 2d 3d 20 73 3b 0a 20 20 70 2d 3e 68 20  >s -= s;.  p->h 
7c490 3d 20 73 2f 33 36 30 30 3b 0a 20 20 73 20 2d 3d  = s/3600;.  s -=
7c4a0 20 70 2d 3e 68 2a 33 36 30 30 3b 0a 20 20 70 2d   p->h*3600;.  p-
7c4b0 3e 6d 20 3d 20 73 2f 36 30 3b 0a 20 20 70 2d 3e  >m = s/60;.  p->
7c4c0 73 20 2b 3d 20 73 20 2d 20 70 2d 3e 6d 2a 36 30  s += s - p->m*60
7c4d0 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20  ;.  p->validHMS 
7c4e0 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  = 1;.}../*.** Co
7c4f0 6d 70 75 74 65 20 62 6f 74 68 20 59 4d 44 20 61  mpute both YMD a
7c500 6e 64 20 48 4d 53 0a 2a 2f 0a 73 74 61 74 69 63  nd HMS.*/.static
7c510 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 59 4d 44   void computeYMD
7c520 5f 48 4d 53 28 44 61 74 65 54 69 6d 65 20 2a 70  _HMS(DateTime *p
7c530 29 7b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 28  ){.  computeYMD(
7c540 70 29 3b 0a 20 20 63 6f 6d 70 75 74 65 48 4d 53  p);.  computeHMS
7c550 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  (p);.}../*.** Cl
7c560 65 61 72 20 74 68 65 20 59 4d 44 20 61 6e 64 20  ear the YMD and 
7c570 48 4d 53 20 61 6e 64 20 74 68 65 20 54 5a 0a 2a  HMS and the TZ.*
7c580 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c  /.static void cl
7c590 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 44 61  earYMD_HMS_TZ(Da
7c5a0 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 70 2d  teTime *p){.  p-
7c5b0 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b 0a 20  >validYMD = 0;. 
7c5c0 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 30   p->validHMS = 0
7c5d0 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d  ;.  p->validTZ =
7c5e0 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53   0;.}..#ifndef S
7c5f0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c  QLITE_OMIT_LOCAL
7c600 54 49 4d 45 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75  TIME./*.** Compu
7c610 74 65 20 74 68 65 20 64 69 66 66 65 72 65 6e 63  te the differenc
7c620 65 20 28 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e  e (in millisecon
7c630 64 73 29 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6c  ds).** between l
7c640 6f 63 61 6c 74 69 6d 65 20 61 6e 64 20 55 54 43  ocaltime and UTC
7c650 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 0a 2a 2a   (a.k.a. GMT).**
7c660 20 66 6f 72 20 74 68 65 20 74 69 6d 65 20 76 61   for the time va
7c670 6c 75 65 20 70 20 77 68 65 72 65 20 70 20 69 73  lue p where p is
7c680 20 69 6e 20 55 54 43 2e 0a 2a 2f 0a 73 74 61 74   in UTC..*/.stat
7c690 69 63 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  ic sqlite3_int64
7c6a0 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74   localtimeOffset
7c6b0 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20  (DateTime *p){. 
7c6c0 20 44 61 74 65 54 69 6d 65 20 78 2c 20 79 3b 0a   DateTime x, y;.
7c6d0 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 78 20    time_t t;.  x 
7c6e0 3d 20 2a 70 3b 0a 20 20 63 6f 6d 70 75 74 65 59  = *p;.  computeY
7c6f0 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 69 66  MD_HMS(&x);.  if
7c700 28 20 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78 2e  ( x.Y<1971 || x.
7c710 59 3e 3d 32 30 33 38 20 29 7b 0a 20 20 20 20 78  Y>=2038 ){.    x
7c720 2e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 78  .Y = 2000;.    x
7c730 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 78 2e 44 20  .M = 1;.    x.D 
7c740 3d 20 31 3b 0a 20 20 20 20 78 2e 68 20 3d 20 30  = 1;.    x.h = 0
7c750 3b 0a 20 20 20 20 78 2e 6d 20 3d 20 30 3b 0a 20  ;.    x.m = 0;. 
7c760 20 20 20 78 2e 73 20 3d 20 30 2e 30 3b 0a 20 20     x.s = 0.0;.  
7c770 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e 74  } else {.    int
7c780 20 73 20 3d 20 28 69 6e 74 29 28 78 2e 73 20 2b   s = (int)(x.s +
7c790 20 30 2e 35 29 3b 0a 20 20 20 20 78 2e 73 20 3d   0.5);.    x.s =
7c7a0 20 73 3b 0a 20 20 7d 0a 20 20 78 2e 74 7a 20 3d   s;.  }.  x.tz =
7c7b0 20 30 3b 0a 20 20 78 2e 76 61 6c 69 64 4a 44 20   0;.  x.validJD 
7c7c0 3d 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44  = 0;.  computeJD
7c7d0 28 26 78 29 3b 0a 20 20 74 20 3d 20 28 74 69 6d  (&x);.  t = (tim
7c7e0 65 5f 74 29 28 78 2e 69 4a 44 2f 31 30 30 30 20  e_t)(x.iJD/1000 
7c7f0 2d 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 29  - 21086676*(i64)
7c800 31 30 30 30 30 29 3b 0a 23 69 66 64 65 66 20 48  10000);.#ifdef H
7c810 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a  AVE_LOCALTIME_R.
7c820 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74    {.    struct t
7c830 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f  m sLocal;.    lo
7c840 63 61 6c 74 69 6d 65 5f 72 28 26 74 2c 20 26 73  caltime_r(&t, &s
7c850 4c 6f 63 61 6c 29 3b 0a 20 20 20 20 79 2e 59 20  Local);.    y.Y 
7c860 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72  = sLocal.tm_year
7c870 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d   + 1900;.    y.M
7c880 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e   = sLocal.tm_mon
7c890 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20   + 1;.    y.D = 
7c8a0 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a  sLocal.tm_mday;.
7c8b0 20 20 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c      y.h = sLocal
7c8c0 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e  .tm_hour;.    y.
7c8d0 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69  m = sLocal.tm_mi
7c8e0 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f  n;.    y.s = sLo
7c8f0 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a  cal.tm_sec;.  }.
7c900 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41  #elif defined(HA
7c910 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 20  VE_LOCALTIME_S) 
7c920 26 26 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d  && HAVE_LOCALTIM
7c930 45 5f 53 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  E_S.  {.    stru
7c940 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b 0a 20 20  ct tm sLocal;.  
7c950 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 26 73    localtime_s(&s
7c960 4c 6f 63 61 6c 2c 20 26 74 29 3b 0a 20 20 20 20  Local, &t);.    
7c970 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f  y.Y = sLocal.tm_
7c980 79 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20  year + 1900;.   
7c990 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.M = sLocal.tm
7c9a0 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e  _mon + 1;.    y.
7c9b0 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 64  D = sLocal.tm_md
7c9c0 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 73 4c  ay;.    y.h = sL
7c9d0 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b 0a 20 20  ocal.tm_hour;.  
7c9e0 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.m = sLocal.t
7c9f0 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d  m_min;.    y.s =
7ca00 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65 63 3b 0a   sLocal.tm_sec;.
7ca10 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20    }.#else.  {.  
7ca20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70 54 6d    struct tm *pTm
7ca30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
7ca40 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
7ca50 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
7ca60 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
7ca70 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20 70 54  MASTER));.    pT
7ca80 6d 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 28 26 74  m = localtime(&t
7ca90 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 70 54 6d  );.    y.Y = pTm
7caa0 2d 3e 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30  ->tm_year + 1900
7cab0 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 70 54 6d 2d  ;.    y.M = pTm-
7cac0 3e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20  >tm_mon + 1;.   
7cad0 20 79 2e 44 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d   y.D = pTm->tm_m
7cae0 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d 20 70  day;.    y.h = p
7caf0 54 6d 2d 3e 74 6d 5f 68 6f 75 72 3b 0a 20 20 20  Tm->tm_hour;.   
7cb00 20 79 2e 6d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d   y.m = pTm->tm_m
7cb10 69 6e 3b 0a 20 20 20 20 79 2e 73 20 3d 20 70 54  in;.    y.s = pT
7cb20 6d 2d 3e 74 6d 5f 73 65 63 3b 0a 20 20 20 20 73  m->tm_sec;.    s
7cb30 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
7cb40 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41  ve(sqlite3MutexA
7cb50 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
7cb60 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
7cb70 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
7cb80 79 2e 76 61 6c 69 64 59 4d 44 20 3d 20 31 3b 0a  y.validYMD = 1;.
7cb90 20 20 79 2e 76 61 6c 69 64 48 4d 53 20 3d 20 31    y.validHMS = 1
7cba0 3b 0a 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d 20  ;.  y.validJD = 
7cbb0 30 3b 0a 20 20 79 2e 76 61 6c 69 64 54 5a 20 3d  0;.  y.validTZ =
7cbc0 20 30 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28   0;.  computeJD(
7cbd0 26 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 79 2e  &y);.  return y.
7cbe0 69 4a 44 20 2d 20 78 2e 69 4a 44 3b 0a 7d 0a 23  iJD - x.iJD;.}.#
7cbf0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
7cc00 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 20 2a  OMIT_LOCALTIME *
7cc10 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73  /../*.** Process
7cc20 20 61 20 6d 6f 64 69 66 69 65 72 20 74 6f 20 61   a modifier to a
7cc30 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d 70   date-time stamp
7cc40 2e 20 20 54 68 65 20 6d 6f 64 69 66 69 65 72 73  .  The modifiers
7cc50 20 61 72 65 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f   are.** as follo
7cc60 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4e 4e  ws:.**.**     NN
7cc70 4e 20 64 61 79 73 0a 2a 2a 20 20 20 20 20 4e 4e  N days.**     NN
7cc80 4e 20 68 6f 75 72 73 0a 2a 2a 20 20 20 20 20 4e  N hours.**     N
7cc90 4e 4e 20 6d 69 6e 75 74 65 73 0a 2a 2a 20 20 20  NN minutes.**   
7cca0 20 20 4e 4e 4e 2e 4e 4e 4e 4e 20 73 65 63 6f 6e    NNN.NNNN secon
7ccb0 64 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 6f  ds.**     NNN mo
7ccc0 6e 74 68 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20  nths.**     NNN 
7ccd0 79 65 61 72 73 0a 2a 2a 20 20 20 20 20 73 74 61  years.**     sta
7cce0 72 74 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20  rt of month.**  
7ccf0 20 20 20 73 74 61 72 74 20 6f 66 20 79 65 61 72     start of year
7cd00 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66  .**     start of
7cd10 20 77 65 65 6b 0a 2a 2a 20 20 20 20 20 73 74 61   week.**     sta
7cd20 72 74 20 6f 66 20 64 61 79 0a 2a 2a 20 20 20 20  rt of day.**    
7cd30 20 77 65 65 6b 64 61 79 20 4e 0a 2a 2a 20 20 20   weekday N.**   
7cd40 20 20 75 6e 69 78 65 70 6f 63 68 0a 2a 2a 20 20    unixepoch.**  
7cd50 20 20 20 6c 6f 63 61 6c 74 69 6d 65 0a 2a 2a 20     localtime.** 
7cd60 20 20 20 20 75 74 63 0a 2a 2a 0a 2a 2a 20 52 65      utc.**.** Re
7cd70 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73  turn 0 on succes
7cd80 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65  s and 1 if there
7cd90 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20   is any kind of 
7cda0 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  error..*/.static
7cdb0 20 69 6e 74 20 70 61 72 73 65 4d 6f 64 69 66 69   int parseModifi
7cdc0 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  er(const char *z
7cdd0 4d 6f 64 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  Mod, DateTime *p
7cde0 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 31 3b  ){.  int rc = 1;
7cdf0 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 64 6f 75 62  .  int n;.  doub
7ce00 6c 65 20 72 3b 0a 20 20 63 68 61 72 20 2a 7a 2c  le r;.  char *z,
7ce10 20 7a 42 75 66 5b 33 30 5d 3b 0a 20 20 7a 20 3d   zBuf[30];.  z =
7ce20 20 7a 42 75 66 3b 0a 20 20 66 6f 72 28 6e 3d 30   zBuf;.  for(n=0
7ce30 3b 20 6e 3c 41 72 72 61 79 53 69 7a 65 28 7a 42  ; n<ArraySize(zB
7ce40 75 66 29 2d 31 20 26 26 20 7a 4d 6f 64 5b 6e 5d  uf)-1 && zMod[n]
7ce50 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 7a 5b 6e 5d  ; n++){.    z[n]
7ce60 20 3d 20 28 63 68 61 72 29 73 71 6c 69 74 65 33   = (char)sqlite3
7ce70 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38  UpperToLower[(u8
7ce80 29 7a 4d 6f 64 5b 6e 5d 5d 3b 0a 20 20 7d 0a 20  )zMod[n]];.  }. 
7ce90 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 73 77 69   z[n] = 0;.  swi
7cea0 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 23 69 66  tch( z[0] ){.#if
7ceb0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
7cec0 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 63  _LOCALTIME.    c
7ced0 61 73 65 20 27 6c 27 3a 20 7b 0a 20 20 20 20 20  ase 'l': {.     
7cee0 20 2f 2a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65   /*    localtime
7cef0 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
7cf00 2a 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20  ** Assuming the 
7cf10 63 75 72 72 65 6e 74 20 74 69 6d 65 20 76 61 6c  current time val
7cf20 75 65 20 69 73 20 55 54 43 20 28 61 2e 6b 2e 61  ue is UTC (a.k.a
7cf30 2e 20 47 4d 54 29 2c 20 73 68 69 66 74 20 69 74  . GMT), shift it
7cf40 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f   to.      ** sho
7cf50 77 20 6c 6f 63 61 6c 20 74 69 6d 65 2e 0a 20 20  w local time..  
7cf60 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
7cf70 20 73 74 72 63 6d 70 28 7a 2c 20 22 6c 6f 63 61   strcmp(z, "loca
7cf80 6c 74 69 6d 65 22 29 3d 3d 30 20 29 7b 0a 20 20  ltime")==0 ){.  
7cf90 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28        computeJD(
7cfa0 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69  p);.        p->i
7cfb0 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f  JD += localtimeO
7cfc0 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20  ffset(p);.      
7cfd0 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54    clearYMD_HMS_T
7cfe0 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63  Z(p);.        rc
7cff0 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
7d000 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
7d010 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65  .#endif.    case
7d020 20 27 75 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a   'u': {.      /*
7d030 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 75 6e 69  .      **    uni
7d040 78 65 70 6f 63 68 0a 20 20 20 20 20 20 2a 2a 0a  xepoch.      **.
7d050 20 20 20 20 20 20 2a 2a 20 54 72 65 61 74 20 74        ** Treat t
7d060 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  he current value
7d070 20 6f 66 20 70 2d 3e 69 4a 44 20 61 73 20 74 68   of p->iJD as th
7d080 65 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20  e number of.    
7d090 20 20 2a 2a 20 73 65 63 6f 6e 64 73 20 73 69 6e    ** seconds sin
7d0a0 63 65 20 31 39 37 30 2e 20 20 43 6f 6e 76 65 72  ce 1970.  Conver
7d0b0 74 20 74 6f 20 61 20 72 65 61 6c 20 6a 75 6c 69  t to a real juli
7d0c0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 2e 0a 20  an day number.. 
7d0d0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
7d0e0 28 20 73 74 72 63 6d 70 28 7a 2c 20 22 75 6e 69  ( strcmp(z, "uni
7d0f0 78 65 70 6f 63 68 22 29 3d 3d 30 20 26 26 20 70  xepoch")==0 && p
7d100 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a 20 20 20  ->validJD ){.   
7d110 20 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 70       p->iJD = (p
7d120 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 29 2f 38  ->iJD + 43200)/8
7d130 36 34 30 30 20 2b 20 32 31 30 38 36 36 37 36 2a  6400 + 21086676*
7d140 28 69 36 34 29 31 30 30 30 30 30 30 30 3b 0a 20  (i64)10000000;. 
7d150 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f         clearYMD_
7d160 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20  HMS_TZ(p);.     
7d170 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
7d180 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
7d190 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45  E_OMIT_LOCALTIME
7d1a0 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28 20  .      else if( 
7d1b0 73 74 72 63 6d 70 28 7a 2c 20 22 75 74 63 22 29  strcmp(z, "utc")
7d1c0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
7d1d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 63 31 3b  qlite3_int64 c1;
7d1e0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
7d1f0 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63  JD(p);.        c
7d200 31 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66  1 = localtimeOff
7d210 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20  set(p);.        
7d220 70 2d 3e 69 4a 44 20 2d 3d 20 63 31 3b 0a 20 20  p->iJD -= c1;.  
7d230 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48        clearYMD_H
7d240 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20  MS_TZ(p);.      
7d250 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 63 31 20 2d    p->iJD += c1 -
7d260 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74   localtimeOffset
7d270 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  (p);.        rc 
7d280 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  = 0;.      }.#en
7d290 64 69 66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  dif.      break;
7d2a0 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
7d2b0 27 77 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a  'w': {.      /*.
7d2c0 20 20 20 20 20 20 2a 2a 20 20 20 20 77 65 65 6b        **    week
7d2d0 64 61 79 20 4e 0a 20 20 20 20 20 20 2a 2a 0a 20  day N.      **. 
7d2e0 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65       ** Move the
7d2f0 20 64 61 74 65 20 74 6f 20 74 68 65 20 73 61 6d   date to the sam
7d300 65 20 74 69 6d 65 20 6f 6e 20 74 68 65 20 6e 65  e time on the ne
7d310 78 74 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66  xt occurrence of
7d320 0a 20 20 20 20 20 20 2a 2a 20 77 65 65 6b 64 61  .      ** weekda
7d330 79 20 4e 20 77 68 65 72 65 20 30 3d 3d 53 75 6e  y N where 0==Sun
7d340 64 61 79 2c 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20  day, 1==Monday, 
7d350 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49  and so forth.  I
7d360 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64  f the.      ** d
7d370 61 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f  ate is already o
7d380 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
7d390 65 20 77 65 65 6b 64 61 79 2c 20 74 68 69 73 20  e weekday, this 
7d3a0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20  is a no-op..    
7d3b0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73    */.      if( s
7d3c0 74 72 6e 63 6d 70 28 7a 2c 20 22 77 65 65 6b 64  trncmp(z, "weekd
7d3d0 61 79 20 22 2c 20 38 29 3d 3d 30 20 26 26 20 67  ay ", 8)==0 && g
7d3e0 65 74 56 61 6c 75 65 28 26 7a 5b 38 5d 2c 26 72  etValue(&z[8],&r
7d3f0 29 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 20  )>0.            
7d400 20 20 20 20 20 26 26 20 28 6e 3d 28 69 6e 74 29       && (n=(int)
7d410 72 29 3d 3d 72 20 26 26 20 6e 3e 3d 30 20 26 26  r)==r && n>=0 &&
7d420 20 72 3c 37 20 29 7b 0a 20 20 20 20 20 20 20 20   r<7 ){.        
7d430 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 5a 3b  sqlite3_int64 Z;
7d440 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
7d450 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20  YMD_HMS(p);.    
7d460 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d      p->validTZ =
7d470 20 30 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76   0;.        p->v
7d480 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20  alidJD = 0;.    
7d490 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29      computeJD(p)
7d4a0 3b 0a 20 20 20 20 20 20 20 20 5a 20 3d 20 28 28  ;.        Z = ((
7d4b0 70 2d 3e 69 4a 44 20 2b 20 31 32 39 36 30 30 30  p->iJD + 1296000
7d4c0 30 30 29 2f 38 36 34 30 30 30 30 30 29 20 25 20  00)/86400000) % 
7d4d0 37 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 5a  7;.        if( Z
7d4e0 3e 6e 20 29 20 5a 20 2d 3d 20 37 3b 0a 20 20 20  >n ) Z -= 7;.   
7d4f0 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28       p->iJD += (
7d500 6e 20 2d 20 5a 29 2a 38 36 34 30 30 30 30 30 3b  n - Z)*86400000;
7d510 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d  .        clearYM
7d520 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
7d530 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
7d540 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
7d550 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
7d560 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a   's': {.      /*
7d570 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 73 74 61  .      **    sta
7d580 72 74 20 6f 66 20 54 54 54 54 54 0a 20 20 20 20  rt of TTTTT.    
7d590 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d 6f    **.      ** Mo
7d5a0 76 65 20 74 68 65 20 64 61 74 65 20 62 61 63 6b  ve the date back
7d5b0 77 61 72 64 73 20 74 6f 20 74 68 65 20 62 65 67  wards to the beg
7d5c0 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 63 75  inning of the cu
7d5d0 72 72 65 6e 74 20 64 61 79 2c 0a 20 20 20 20 20  rrent day,.     
7d5e0 20 2a 2a 20 6f 72 20 6d 6f 6e 74 68 20 6f 72 20   ** or month or 
7d5f0 79 65 61 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  year..      */. 
7d600 20 20 20 20 20 69 66 28 20 73 74 72 6e 63 6d 70       if( strncmp
7d610 28 7a 2c 20 22 73 74 61 72 74 20 6f 66 20 22 2c  (z, "start of ",
7d620 20 39 29 21 3d 30 20 29 20 62 72 65 61 6b 3b 0a   9)!=0 ) break;.
7d630 20 20 20 20 20 20 7a 20 2b 3d 20 39 3b 0a 20 20        z += 9;.  
7d640 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70      computeYMD(p
7d650 29 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  );.      p->vali
7d660 64 48 4d 53 20 3d 20 31 3b 0a 20 20 20 20 20 20  dHMS = 1;.      
7d670 70 2d 3e 68 20 3d 20 70 2d 3e 6d 20 3d 20 30 3b  p->h = p->m = 0;
7d680 0a 20 20 20 20 20 20 70 2d 3e 73 20 3d 20 30 2e  .      p->s = 0.
7d690 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  0;.      p->vali
7d6a0 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  dTZ = 0;.      p
7d6b0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  ->validJD = 0;. 
7d6c0 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28       if( strcmp(
7d6d0 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29 7b  z,"month")==0 ){
7d6e0 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20 3d 20  .        p->D = 
7d6f0 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  1;.        rc = 
7d700 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  0;.      }else i
7d710 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61  f( strcmp(z,"yea
7d720 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  r")==0 ){.      
7d730 20 20 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b    computeYMD(p);
7d740 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20 3d 20  .        p->M = 
7d750 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 44 20  1;.        p->D 
7d760 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 63 20  = 1;.        rc 
7d770 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
7d780 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 64   if( strcmp(z,"d
7d790 61 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ay")==0 ){.     
7d7a0 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20     rc = 0;.     
7d7b0 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
7d7c0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
7d7d0 2b 27 3a 0a 20 20 20 20 63 61 73 65 20 27 2d 27  +':.    case '-'
7d7e0 3a 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 0a  :.    case '0':.
7d7f0 20 20 20 20 63 61 73 65 20 27 31 27 3a 0a 20 20      case '1':.  
7d800 20 20 63 61 73 65 20 27 32 27 3a 0a 20 20 20 20    case '2':.    
7d810 63 61 73 65 20 27 33 27 3a 0a 20 20 20 20 63 61  case '3':.    ca
7d820 73 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65  se '4':.    case
7d830 20 27 35 27 3a 0a 20 20 20 20 63 61 73 65 20 27   '5':.    case '
7d840 36 27 3a 0a 20 20 20 20 63 61 73 65 20 27 37 27  6':.    case '7'
7d850 3a 0a 20 20 20 20 63 61 73 65 20 27 38 27 3a 0a  :.    case '8':.
7d860 20 20 20 20 63 61 73 65 20 27 39 27 3a 20 7b 0a      case '9': {.
7d870 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 52 6f        double rRo
7d880 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 6e 20 3d  under;.      n =
7d890 20 67 65 74 56 61 6c 75 65 28 7a 2c 20 26 72 29   getValue(z, &r)
7d8a0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
7d8b0 6e 3e 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66  n>=1 );.      if
7d8c0 28 20 7a 5b 6e 5d 3d 3d 27 3a 27 20 29 7b 0a 20  ( z[n]==':' ){. 
7d8d0 20 20 20 20 20 20 20 2f 2a 20 41 20 6d 6f 64 69         /* A modi
7d8e0 66 69 65 72 20 6f 66 20 74 68 65 20 66 6f 72 6d  fier of the form
7d8f0 20 28 2b 7c 2d 29 48 48 3a 4d 4d 3a 53 53 2e 46   (+|-)HH:MM:SS.F
7d900 46 46 20 61 64 64 73 20 28 6f 72 20 73 75 62 74  FF adds (or subt
7d910 72 61 63 74 73 29 20 74 68 65 0a 20 20 20 20 20  racts) the.     
7d920 20 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20     ** specified 
7d930 6e 75 6d 62 65 72 20 6f 66 20 68 6f 75 72 73 2c  number of hours,
7d940 20 6d 69 6e 75 74 65 73 2c 20 73 65 63 6f 6e 64   minutes, second
7d950 73 2c 20 61 6e 64 20 66 72 61 63 74 69 6f 6e 61  s, and fractiona
7d960 6c 20 73 65 63 6f 6e 64 73 0a 20 20 20 20 20 20  l seconds.      
7d970 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 69 6d 65    ** to the time
7d980 2e 20 20 54 68 65 20 22 2e 46 46 46 22 20 6d 61  .  The ".FFF" ma
7d990 79 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 54  y be omitted.  T
7d9a0 68 65 20 22 3a 53 53 2e 46 46 46 22 20 6d 61 79  he ":SS.FFF" may
7d9b0 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f   be.        ** o
7d9c0 6d 69 74 74 65 64 2e 0a 20 20 20 20 20 20 20 20  mitted..        
7d9d0 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  */.        const
7d9e0 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20   char *z2 = z;. 
7d9f0 20 20 20 20 20 20 20 44 61 74 65 54 69 6d 65 20         DateTime 
7da00 74 78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  tx;.        sqli
7da10 74 65 33 5f 69 6e 74 36 34 20 64 61 79 3b 0a 20  te3_int64 day;. 
7da20 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69         if( !sqli
7da30 74 65 33 49 73 64 69 67 69 74 28 2a 7a 32 29 20  te3Isdigit(*z2) 
7da40 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ) z2++;.        
7da50 6d 65 6d 73 65 74 28 26 74 78 2c 20 30 2c 20 73  memset(&tx, 0, s
7da60 69 7a 65 6f 66 28 74 78 29 29 3b 0a 20 20 20 20  izeof(tx));.    
7da70 20 20 20 20 69 66 28 20 70 61 72 73 65 48 68 4d      if( parseHhM
7da80 6d 53 73 28 7a 32 2c 20 26 74 78 29 20 29 20 62  mSs(z2, &tx) ) b
7da90 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 6f  reak;.        co
7daa0 6d 70 75 74 65 4a 44 28 26 74 78 29 3b 0a 20 20  mputeJD(&tx);.  
7dab0 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d 3d 20        tx.iJD -= 
7dac0 34 33 32 30 30 30 30 30 3b 0a 20 20 20 20 20 20  43200000;.      
7dad0 20 20 64 61 79 20 3d 20 74 78 2e 69 4a 44 2f 38    day = tx.iJD/8
7dae0 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20  6400000;.       
7daf0 20 74 78 2e 69 4a 44 20 2d 3d 20 64 61 79 2a 38   tx.iJD -= day*8
7db00 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20  6400000;.       
7db10 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 2d 27 20 29   if( z[0]=='-' )
7db20 20 74 78 2e 69 4a 44 20 3d 20 2d 74 78 2e 69 4a   tx.iJD = -tx.iJ
7db30 44 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  D;.        compu
7db40 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20  teJD(p);.       
7db50 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a   clearYMD_HMS_TZ
7db60 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  (p);.        p->
7db70 69 4a 44 20 2b 3d 20 74 78 2e 69 4a 44 3b 0a 20  iJD += tx.iJD;. 
7db80 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20         rc = 0;. 
7db90 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
7dba0 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 20 2b 3d      }.      z +=
7dbb0 20 6e 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   n;.      while(
7dbc0 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28   sqlite3Isspace(
7dbd0 2a 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 20  *z) ) z++;.     
7dbe0 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
7dbf0 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 20 20 69  en30(z);.      i
7dc00 66 28 20 6e 3e 31 30 20 7c 7c 20 6e 3c 33 20 29  f( n>10 || n<3 )
7dc10 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 69 66   break;.      if
7dc20 28 20 7a 5b 6e 2d 31 5d 3d 3d 27 73 27 20 29 7b  ( z[n-1]=='s' ){
7dc30 20 7a 5b 6e 2d 31 5d 20 3d 20 30 3b 20 6e 2d 2d   z[n-1] = 0; n--
7dc40 3b 20 7d 0a 20 20 20 20 20 20 63 6f 6d 70 75 74  ; }.      comput
7dc50 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 72 63  eJD(p);.      rc
7dc60 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 52 6f 75   = 0;.      rRou
7dc70 6e 64 65 72 20 3d 20 72 3c 30 20 3f 20 2d 30 2e  nder = r<0 ? -0.
7dc80 35 20 3a 20 2b 30 2e 35 3b 0a 20 20 20 20 20 20  5 : +0.5;.      
7dc90 69 66 28 20 6e 3d 3d 33 20 26 26 20 73 74 72 63  if( n==3 && strc
7dca0 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29  mp(z,"day")==0 )
7dcb0 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44  {.        p->iJD
7dcc0 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74   += (sqlite3_int
7dcd0 36 34 29 28 72 2a 38 36 34 30 30 30 30 30 2e 30  64)(r*86400000.0
7dce0 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20   + rRounder);.  
7dcf0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d      }else if( n=
7dd00 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22  =4 && strcmp(z,"
7dd10 68 6f 75 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20  hour")==0 ){.   
7dd20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28       p->iJD += (
7dd30 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72  sqlite3_int64)(r
7dd40 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 32 34 2e  *(86400000.0/24.
7dd50 30 29 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a  0) + rRounder);.
7dd60 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
7dd70 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70 28 7a  n==6 && strcmp(z
7dd80 2c 22 6d 69 6e 75 74 65 22 29 3d 3d 30 20 29 7b  ,"minute")==0 ){
7dd90 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
7dda0 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36  += (sqlite3_int6
7ddb0 34 29 28 72 2a 28 38 36 34 30 30 30 30 30 2e 30  4)(r*(86400000.0
7ddc0 2f 28 32 34 2e 30 2a 36 30 2e 30 29 29 20 2b 20  /(24.0*60.0)) + 
7ddd0 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20  rRounder);.     
7dde0 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36 20   }else if( n==6 
7ddf0 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 73 65 63  && strcmp(z,"sec
7de00 6f 6e 64 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ond")==0 ){.    
7de10 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73      p->iJD += (s
7de20 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a  qlite3_int64)(r*
7de30 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34 2e  (86400000.0/(24.
7de40 30 2a 36 30 2e 30 2a 36 30 2e 30 29 29 20 2b 20  0*60.0*60.0)) + 
7de50 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20  rRounder);.     
7de60 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 35 20   }else if( n==5 
7de70 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e  && strcmp(z,"mon
7de80 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  th")==0 ){.     
7de90 20 20 20 69 6e 74 20 78 2c 20 79 3b 0a 20 20 20     int x, y;.   
7dea0 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f       computeYMD_
7deb0 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20  HMS(p);.        
7dec0 70 2d 3e 4d 20 2b 3d 20 28 69 6e 74 29 72 3b 0a  p->M += (int)r;.
7ded0 20 20 20 20 20 20 20 20 78 20 3d 20 70 2d 3e 4d          x = p->M
7dee0 3e 30 20 3f 20 28 70 2d 3e 4d 2d 31 29 2f 31 32  >0 ? (p->M-1)/12
7def0 20 3a 20 28 70 2d 3e 4d 2d 31 32 29 2f 31 32 3b   : (p->M-12)/12;
7df00 0a 20 20 20 20 20 20 20 20 70 2d 3e 59 20 2b 3d  .        p->Y +=
7df10 20 78 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d   x;.        p->M
7df20 20 2d 3d 20 78 2a 31 32 3b 0a 20 20 20 20 20 20   -= x*12;.      
7df30 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30    p->validJD = 0
7df40 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  ;.        comput
7df50 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20  eJD(p);.        
7df60 79 20 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20  y = (int)r;.    
7df70 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b 0a      if( y!=r ){.
7df80 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44            p->iJD
7df90 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74   += (sqlite3_int
7dfa0 36 34 29 28 28 72 20 2d 20 79 29 2a 33 30 2e 30  64)((r - y)*30.0
7dfb0 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 72 52  *86400000.0 + rR
7dfc0 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 20  ounder);.       
7dfd0 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69   }.      }else i
7dfe0 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72 63 6d  f( n==4 && strcm
7dff0 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29  p(z,"year")==0 )
7e000 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 79 20  {.        int y 
7e010 3d 20 28 69 6e 74 29 72 3b 0a 20 20 20 20 20 20  = (int)r;.      
7e020 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53    computeYMD_HMS
7e030 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  (p);.        p->
7e040 59 20 2b 3d 20 79 3b 0a 20 20 20 20 20 20 20 20  Y += y;.        
7e050 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a  p->validJD = 0;.
7e060 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a          computeJ
7e070 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66  D(p);.        if
7e080 28 20 79 21 3d 72 20 29 7b 0a 20 20 20 20 20 20  ( y!=r ){.      
7e090 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73      p->iJD += (s
7e0a0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 28 72  qlite3_int64)((r
7e0b0 20 2d 20 79 29 2a 33 36 35 2e 30 2a 38 36 34 30   - y)*365.0*8640
7e0c0 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64 65  0000.0 + rRounde
7e0d0 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  r);.        }.  
7e0e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
7e0f0 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 20     rc = 1;.     
7e100 20 7d 0a 20 20 20 20 20 20 63 6c 65 61 72 59 4d   }.      clearYM
7e110 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
7e120 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
7e130 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
7e140 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
7e150 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
7e160 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63  c;.}../*.** Proc
7e170 65 73 73 20 74 69 6d 65 20 66 75 6e 63 74 69 6f  ess time functio
7e180 6e 20 61 72 67 75 6d 65 6e 74 73 2e 20 20 61 72  n arguments.  ar
7e190 67 76 5b 30 5d 20 69 73 20 61 20 64 61 74 65 2d  gv[0] is a date-
7e1a0 74 69 6d 65 20 73 74 61 6d 70 2e 0a 2a 2a 20 61  time stamp..** a
7e1b0 72 67 76 5b 31 5d 20 61 6e 64 20 66 6f 6c 6c 6f  rgv[1] and follo
7e1c0 77 69 6e 67 20 61 72 65 20 6d 6f 64 69 66 69 65  wing are modifie
7e1d0 72 73 2e 20 20 50 61 72 73 65 20 74 68 65 6d 20  rs.  Parse them 
7e1e0 61 6c 6c 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a  all and write.**
7e1f0 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 74   the resulting t
7e200 69 6d 65 20 69 6e 74 6f 20 74 68 65 20 44 61 74  ime into the Dat
7e210 65 54 69 6d 65 20 73 74 72 75 63 74 75 72 65 20  eTime structure 
7e220 70 2e 20 20 52 65 74 75 72 6e 20 30 0a 2a 2a 20  p.  Return 0.** 
7e230 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 31  on success and 1
7e240 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   if there are an
7e250 79 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  y errors..**.** 
7e260 49 66 20 74 68 65 72 65 20 61 72 65 20 7a 65 72  If there are zer
7e270 6f 20 70 61 72 61 6d 65 74 65 72 73 20 28 69 66  o parameters (if
7e280 20 65 76 65 6e 20 61 72 67 76 5b 30 5d 20 69 73   even argv[0] is
7e290 20 75 6e 64 65 66 69 6e 65 64 29 0a 2a 2a 20 74   undefined).** t
7e2a0 68 65 6e 20 61 73 73 75 6d 65 20 61 20 64 65 66  hen assume a def
7e2b0 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 22 6e  ault value of "n
7e2c0 6f 77 22 20 66 6f 72 20 61 72 67 76 5b 30 5d 2e  ow" for argv[0].
7e2d0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
7e2e0 73 44 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33  sDate(.  sqlite3
7e2f0 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
7e300 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  t, .  int argc, 
7e310 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
7e320 20 2a 2a 61 72 67 76 2c 20 0a 20 20 44 61 74 65   **argv, .  Date
7e330 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74  Time *p.){.  int
7e340 20 69 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   i;.  const unsi
7e350 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20  gned char *z;.  
7e360 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 6d 65 6d  int eType;.  mem
7e370 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66  set(p, 0, sizeof
7e380 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 61 72 67  (*p));.  if( arg
7e390 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 65 74 44  c==0 ){.    setD
7e3a0 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e 74  ateTimeToCurrent
7e3b0 28 63 6f 6e 74 65 78 74 2c 20 70 29 3b 0a 20 20  (context, p);.  
7e3c0 7d 65 6c 73 65 20 69 66 28 20 28 65 54 79 70 65  }else if( (eType
7e3d0 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
7e3e0 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 29 3d  _type(argv[0]))=
7e3f0 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 0a 20 20  =SQLITE_FLOAT.  
7e400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7e410 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
7e420 45 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20  E_INTEGER ){.   
7e430 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74   p->iJD = (sqlit
7e440 65 33 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65  e3_int64)(sqlite
7e450 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61  3_value_double(a
7e460 72 67 76 5b 30 5d 29 2a 38 36 34 30 30 30 30 30  rgv[0])*86400000
7e470 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70  .0 + 0.5);.    p
7e480 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20  ->validJD = 1;. 
7e490 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20   }else{.    z = 
7e4a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
7e4b0 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  xt(argv[0]);.   
7e4c0 20 69 66 28 20 21 7a 20 7c 7c 20 70 61 72 73 65   if( !z || parse
7e4d0 44 61 74 65 4f 72 54 69 6d 65 28 63 6f 6e 74 65  DateOrTime(conte
7e4e0 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 70 29  xt, (char*)z, p)
7e4f0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
7e500 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
7e510 66 6f 72 28 69 3d 31 3b 20 69 3c 61 72 67 63 3b  for(i=1; i<argc;
7e520 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28   i++){.    if( (
7e530 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  z = sqlite3_valu
7e540 65 5f 74 65 78 74 28 61 72 67 76 5b 69 5d 29 29  e_text(argv[i]))
7e550 3d 3d 30 20 7c 7c 20 70 61 72 73 65 4d 6f 64 69  ==0 || parseModi
7e560 66 69 65 72 28 28 63 68 61 72 2a 29 7a 2c 20 70  fier((char*)z, p
7e570 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
7e580 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
7e590 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f   return 0;.}.../
7e5a0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
7e5b0 6e 67 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c  ng routines impl
7e5c0 65 6d 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75  ement the variou
7e5d0 73 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 20  s date and time 
7e5e0 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6f 66 20  functions.** of 
7e5f0 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  SQLite..*/../*.*
7e600 2a 20 20 20 20 6a 75 6c 69 61 6e 64 61 79 28 20  *    julianday( 
7e610 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c  TIMESTRING, MOD,
7e620 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a   MOD, ...).**.**
7e630 20 52 65 74 75 72 6e 20 74 68 65 20 6a 75 6c 69   Return the juli
7e640 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 20 6f 66  an day number of
7e650 20 74 68 65 20 64 61 74 65 20 73 70 65 63 69 66   the date specif
7e660 69 65 64 20 69 6e 20 74 68 65 20 61 72 67 75 6d  ied in the argum
7e670 65 6e 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ents.*/.static v
7e680 6f 69 64 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e  oid juliandayFun
7e690 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
7e6a0 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
7e6b0 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
7e6c0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
7e6d0 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20  v.){.  DateTime 
7e6e0 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28  x;.  if( isDate(
7e6f0 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61  context, argc, a
7e700 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20  rgv, &x)==0 ){. 
7e710 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29     computeJD(&x)
7e720 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
7e730 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
7e740 65 78 74 2c 20 78 2e 69 4a 44 2f 38 36 34 30 30  ext, x.iJD/86400
7e750 30 30 30 2e 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  000.0);.  }.}../
7e760 2a 0a 2a 2a 20 20 20 20 64 61 74 65 74 69 6d 65  *.**    datetime
7e770 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f  ( TIMESTRING, MO
7e780 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a  D, MOD, ...).**.
7e790 2a 2a 20 52 65 74 75 72 6e 20 59 59 59 59 2d 4d  ** Return YYYY-M
7e7a0 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f  M-DD HH:MM:SS.*/
7e7b0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 61 74  .static void dat
7e7c0 65 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c  etimeFunc(.  sql
7e7d0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
7e7e0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
7e7f0 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
7e800 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44  ue **argv.){.  D
7e810 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28  ateTime x;.  if(
7e820 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c   isDate(context,
7e830 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29   argc, argv, &x)
7e840 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ==0 ){.    char 
7e850 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63  zBuf[100];.    c
7e860 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78  omputeYMD_HMS(&x
7e870 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  );.    sqlite3_s
7e880 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
7e890 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 34  Buf), zBuf, "%04
7e8a0 64 2d 25 30 32 64 2d 25 30 32 64 20 25 30 32 64  d-%02d-%02d %02d
7e8b0 3a 25 30 32 64 3a 25 30 32 64 22 2c 0a 20 20 20  :%02d:%02d",.   
7e8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7e8d0 20 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 2c    x.Y, x.M, x.D,
7e8e0 20 78 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29   x.h, x.m, (int)
7e8f0 28 78 2e 73 29 29 3b 0a 20 20 20 20 73 71 6c 69  (x.s));.    sqli
7e900 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
7e910 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d  context, zBuf, -
7e920 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  1, SQLITE_TRANSI
7e930 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ENT);.  }.}../*.
7e940 2a 2a 20 20 20 20 74 69 6d 65 28 20 54 49 4d 45  **    time( TIME
7e950 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
7e960 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74  , ...).**.** Ret
7e970 75 72 6e 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a  urn HH:MM:SS.*/.
7e980 73 74 61 74 69 63 20 76 6f 69 64 20 74 69 6d 65  static void time
7e990 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
7e9a0 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
7e9b0 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
7e9c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
7e9d0 61 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69  argv.){.  DateTi
7e9e0 6d 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61  me x;.  if( isDa
7e9f0 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63  te(context, argc
7ea00 2c 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29  , argv, &x)==0 )
7ea10 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b  {.    char zBuf[
7ea20 31 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74  100];.    comput
7ea30 65 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71  eHMS(&x);.    sq
7ea40 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
7ea50 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75  izeof(zBuf), zBu
7ea60 66 2c 20 22 25 30 32 64 3a 25 30 32 64 3a 25 30  f, "%02d:%02d:%0
7ea70 32 64 22 2c 20 78 2e 68 2c 20 78 2e 6d 2c 20 28  2d", x.h, x.m, (
7ea80 69 6e 74 29 78 2e 73 29 3b 0a 20 20 20 20 73 71  int)x.s);.    sq
7ea90 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
7eaa0 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
7eab0 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
7eac0 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  SIENT);.  }.}../
7ead0 2a 0a 2a 2a 20 20 20 20 64 61 74 65 28 20 54 49  *.**    date( TI
7eae0 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d  MESTRING, MOD, M
7eaf0 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52  OD, ...).**.** R
7eb00 65 74 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44  eturn YYYY-MM-DD
7eb10 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
7eb20 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69  dateFunc(.  sqli
7eb30 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
7eb40 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
7eb50 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
7eb60 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61  e **argv.){.  Da
7eb70 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20  teTime x;.  if( 
7eb80 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20  isDate(context, 
7eb90 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d  argc, argv, &x)=
7eba0 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72 20 7a  =0 ){.    char z
7ebb0 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 63 6f  Buf[100];.    co
7ebc0 6d 70 75 74 65 59 4d 44 28 26 78 29 3b 0a 20 20  mputeYMD(&x);.  
7ebd0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
7ebe0 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c  tf(sizeof(zBuf),
7ebf0 20 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32   zBuf, "%04d-%02
7ec00 64 2d 25 30 32 64 22 2c 20 78 2e 59 2c 20 78 2e  d-%02d", x.Y, x.
7ec10 4d 2c 20 78 2e 44 29 3b 0a 20 20 20 20 73 71 6c  M, x.D);.    sql
7ec20 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
7ec30 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c 20  (context, zBuf, 
7ec40 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  -1, SQLITE_TRANS
7ec50 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  IENT);.  }.}../*
7ec60 0a 2a 2a 20 20 20 20 73 74 72 66 74 69 6d 65 28  .**    strftime(
7ec70 20 46 4f 52 4d 41 54 2c 20 54 49 4d 45 53 54 52   FORMAT, TIMESTR
7ec80 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e  ING, MOD, MOD, .
7ec90 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ..).**.** Return
7eca0 20 61 20 73 74 72 69 6e 67 20 64 65 73 63 72 69   a string descri
7ecb0 62 65 64 20 62 79 20 46 4f 52 4d 41 54 2e 20 20  bed by FORMAT.  
7ecc0 43 6f 6e 76 65 72 73 69 6f 6e 73 20 61 73 20 66  Conversions as f
7ecd0 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
7ece0 25 64 20 20 64 61 79 20 6f 66 20 6d 6f 6e 74 68  %d  day of month
7ecf0 0a 2a 2a 20 20 20 25 66 20 20 2a 2a 20 66 72 61  .**   %f  ** fra
7ed00 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 20  ctional seconds 
7ed10 20 53 53 2e 53 53 53 0a 2a 2a 20 20 20 25 48 20   SS.SSS.**   %H 
7ed20 20 68 6f 75 72 20 30 30 2d 32 34 0a 2a 2a 20 20   hour 00-24.**  
7ed30 20 25 6a 20 20 64 61 79 20 6f 66 20 79 65 61 72   %j  day of year
7ed40 20 30 30 30 2d 33 36 36 0a 2a 2a 20 20 20 25 4a   000-366.**   %J
7ed50 20 20 2a 2a 20 4a 75 6c 69 61 6e 20 64 61 79 20    ** Julian day 
7ed60 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 25 6d 20 20  number.**   %m  
7ed70 6d 6f 6e 74 68 20 30 31 2d 31 32 0a 2a 2a 20 20  month 01-12.**  
7ed80 20 25 4d 20 20 6d 69 6e 75 74 65 20 30 30 2d 35   %M  minute 00-5
7ed90 39 0a 2a 2a 20 20 20 25 73 20 20 73 65 63 6f 6e  9.**   %s  secon
7eda0 64 73 20 73 69 6e 63 65 20 31 39 37 30 2d 30 31  ds since 1970-01
7edb0 2d 30 31 0a 2a 2a 20 20 20 25 53 20 20 73 65 63  -01.**   %S  sec
7edc0 6f 6e 64 73 20 30 30 2d 35 39 0a 2a 2a 20 20 20  onds 00-59.**   
7edd0 25 77 20 20 64 61 79 20 6f 66 20 77 65 65 6b 20  %w  day of week 
7ede0 30 2d 36 20 20 73 75 6e 64 61 79 3d 3d 30 0a 2a  0-6  sunday==0.*
7edf0 2a 20 20 20 25 57 20 20 77 65 65 6b 20 6f 66 20  *   %W  week of 
7ee00 79 65 61 72 20 30 30 2d 35 33 0a 2a 2a 20 20 20  year 00-53.**   
7ee10 25 59 20 20 79 65 61 72 20 30 30 30 30 2d 39 39  %Y  year 0000-99
7ee20 39 39 0a 2a 2a 20 20 20 25 25 20 20 25 0a 2a 2f  99.**   %%  %.*/
7ee30 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72  .static void str
7ee40 66 74 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c  ftimeFunc(.  sql
7ee50 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
7ee60 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
7ee70 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
7ee80 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44  ue **argv.){.  D
7ee90 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 75 36 34  ateTime x;.  u64
7eea0 20 6e 3b 0a 20 20 73 69 7a 65 5f 74 20 69 2c 6a   n;.  size_t i,j
7eeb0 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 73  ;.  char *z;.  s
7eec0 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 63 6f  qlite3 *db;.  co
7eed0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6d 74 20 3d  nst char *zFmt =
7eee0 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
7eef0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
7ef00 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 63 68 61  (argv[0]);.  cha
7ef10 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 69  r zBuf[100];.  i
7ef20 66 28 20 7a 46 6d 74 3d 3d 30 20 7c 7c 20 69 73  f( zFmt==0 || is
7ef30 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20 61 72  Date(context, ar
7ef40 67 63 2d 31 2c 20 61 72 67 76 2b 31 2c 20 26 78  gc-1, argv+1, &x
7ef50 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62  ) ) return;.  db
7ef60 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
7ef70 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
7ef80 74 65 78 74 29 3b 0a 20 20 66 6f 72 28 69 3d 30  text);.  for(i=0
7ef90 2c 20 6e 3d 31 3b 20 7a 46 6d 74 5b 69 5d 3b 20  , n=1; zFmt[i]; 
7efa0 69 2b 2b 2c 20 6e 2b 2b 29 7b 0a 20 20 20 20 69  i++, n++){.    i
7efb0 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 25 27 20  f( zFmt[i]=='%' 
7efc0 29 7b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28  ){.      switch(
7efd0 20 7a 46 6d 74 5b 69 2b 31 5d 20 29 7b 0a 20 20   zFmt[i+1] ){.  
7efe0 20 20 20 20 20 20 63 61 73 65 20 27 64 27 3a 0a        case 'd':.
7eff0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 48 27          case 'H'
7f000 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  :.        case '
7f010 6d 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73 65  m':.        case
7f020 20 27 4d 27 3a 0a 20 20 20 20 20 20 20 20 63 61   'M':.        ca
7f030 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20 20 20  se 'S':.        
7f040 63 61 73 65 20 27 57 27 3a 0a 20 20 20 20 20 20  case 'W':.      
7f050 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 20 20 20      n++;.       
7f060 20 20 20 2f 2a 20 66 61 6c 6c 20 74 68 72 75 20     /* fall thru 
7f070 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  */.        case 
7f080 27 77 27 3a 0a 20 20 20 20 20 20 20 20 63 61 73  'w':.        cas
7f090 65 20 27 25 27 3a 0a 20 20 20 20 20 20 20 20 20  e '%':.         
7f0a0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
7f0b0 63 61 73 65 20 27 66 27 3a 0a 20 20 20 20 20 20  case 'f':.      
7f0c0 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20 20      n += 8;.    
7f0d0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7f0e0 20 20 20 20 20 63 61 73 65 20 27 6a 27 3a 0a 20       case 'j':. 
7f0f0 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 33 3b           n += 3;
7f100 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
7f110 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
7f120 59 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20  Y':.          n 
7f130 2b 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20  += 8;.          
7f140 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
7f150 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20 20 20  ase 's':.       
7f160 20 63 61 73 65 20 27 4a 27 3a 0a 20 20 20 20 20   case 'J':.     
7f170 20 20 20 20 20 6e 20 2b 3d 20 35 30 3b 0a 20 20       n += 50;.  
7f180 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
7f190 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a         default:.
7f1a0 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
7f1b0 3b 20 20 2f 2a 20 45 52 52 4f 52 2e 20 20 72 65  ;  /* ERROR.  re
7f1c0 74 75 72 6e 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20  turn a NULL */. 
7f1d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 2b 2b       }.      i++
7f1e0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 74 65  ;.    }.  }.  te
7f1f0 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65 6f  stcase( n==sizeo
7f200 66 28 7a 42 75 66 29 2d 31 20 29 3b 0a 20 20 74  f(zBuf)-1 );.  t
7f210 65 73 74 63 61 73 65 28 20 6e 3d 3d 73 69 7a 65  estcase( n==size
7f220 6f 66 28 7a 42 75 66 29 20 29 3b 0a 20 20 74 65  of(zBuf) );.  te
7f230 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29  stcase( n==(u64)
7f240 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
7f250 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b  E_LIMIT_LENGTH]+
7f260 31 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  1 );.  testcase(
7f270 20 6e 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69   n==(u64)db->aLi
7f280 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
7f290 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 69 66  _LENGTH] );.  if
7f2a0 28 20 6e 3c 73 69 7a 65 6f 66 28 7a 42 75 66 29  ( n<sizeof(zBuf)
7f2b0 20 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66   ){.    z = zBuf
7f2c0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3e  ;.  }else if( n>
7f2d0 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69 74 5b  (u64)db->aLimit[
7f2e0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
7f2f0 47 54 48 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69  GTH] ){.    sqli
7f300 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
7f310 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29  _toobig(context)
7f320 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
7f330 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20 3d 20 73  }else{.    z = s
7f340 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
7f350 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a 20  w(db, (int)n);. 
7f360 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20     if( z==0 ){. 
7f370 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
7f380 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
7f390 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
7f3a0 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
7f3b0 7d 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 78  }.  computeJD(&x
7f3c0 29 3b 0a 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f  );.  computeYMD_
7f3d0 48 4d 53 28 26 78 29 3b 0a 20 20 66 6f 72 28 69  HMS(&x);.  for(i
7f3e0 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d 3b 20 69  =j=0; zFmt[i]; i
7f3f0 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d  ++){.    if( zFm
7f400 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0a 20 20 20  t[i]!='%' ){.   
7f410 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 46 6d 74     z[j++] = zFmt
7f420 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  [i];.    }else{.
7f430 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
7f440 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 5d   switch( zFmt[i]
7f450 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65   ){.        case
7f460 20 27 64 27 3a 20 20 73 71 6c 69 74 65 33 5f 73   'd':  sqlite3_s
7f470 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d  nprintf(3, &z[j]
7f480 2c 22 25 30 32 64 22 2c 78 2e 44 29 3b 20 6a 2b  ,"%02d",x.D); j+
7f490 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  =2; break;.     
7f4a0 20 20 20 63 61 73 65 20 27 66 27 3a 20 7b 0a 20     case 'f': {. 
7f4b0 20 20 20 20 20 20 20 20 20 64 6f 75 62 6c 65 20           double 
7f4c0 73 20 3d 20 78 2e 73 3b 0a 20 20 20 20 20 20 20  s = x.s;.       
7f4d0 20 20 20 69 66 28 20 73 3e 35 39 2e 39 39 39 20     if( s>59.999 
7f4e0 29 20 73 20 3d 20 35 39 2e 39 39 39 3b 0a 20 20  ) s = 59.999;.  
7f4f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
7f500 73 6e 70 72 69 6e 74 66 28 37 2c 20 26 7a 5b 6a  snprintf(7, &z[j
7f510 5d 2c 22 25 30 36 2e 33 66 22 2c 20 73 29 3b 0a  ],"%06.3f", s);.
7f520 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 73            j += s
7f530 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26  qlite3Strlen30(&
7f540 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20  z[j]);.         
7f550 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
7f560 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  }.        case '
7f570 48 27 3a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  H':  sqlite3_snp
7f580 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22  rintf(3, &z[j],"
7f590 25 30 32 64 22 2c 78 2e 68 29 3b 20 6a 2b 3d 32  %02d",x.h); j+=2
7f5a0 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
7f5b0 20 63 61 73 65 20 27 57 27 3a 20 2f 2a 20 46 61   case 'W': /* Fa
7f5c0 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20  ll thru */.     
7f5d0 20 20 20 63 61 73 65 20 27 6a 27 3a 20 7b 0a 20     case 'j': {. 
7f5e0 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 44 61           int nDa
7f5f0 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  y;             /
7f600 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 79 73  * Number of days
7f610 20 73 69 6e 63 65 20 31 73 74 20 64 61 79 20 6f   since 1st day o
7f620 66 20 79 65 61 72 20 2a 2f 0a 20 20 20 20 20 20  f year */.      
7f630 20 20 20 20 44 61 74 65 54 69 6d 65 20 79 20 3d      DateTime y =
7f640 20 78 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e   x;.          y.
7f650 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20  validJD = 0;.   
7f660 20 20 20 20 20 20 20 79 2e 4d 20 3d 20 31 3b 0a         y.M = 1;.
7f670 20 20 20 20 20 20 20 20 20 20 79 2e 44 20 3d 20            y.D = 
7f680 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6d  1;.          com
7f690 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 20 20  puteJD(&y);.    
7f6a0 20 20 20 20 20 20 6e 44 61 79 20 3d 20 28 69 6e        nDay = (in
7f6b0 74 29 28 28 78 2e 69 4a 44 2d 79 2e 69 4a 44 2b  t)((x.iJD-y.iJD+
7f6c0 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30  43200000)/864000
7f6d0 30 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  00);.          i
7f6e0 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d 27 57 27 20  f( zFmt[i]=='W' 
7f6f0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
7f700 6e 74 20 77 64 3b 20 20 20 2f 2a 20 30 3d 4d 6f  nt wd;   /* 0=Mo
7f710 6e 64 61 79 2c 20 31 3d 54 75 65 73 64 61 79 2c  nday, 1=Tuesday,
7f720 20 2e 2e 2e 20 36 3d 53 75 6e 64 61 79 20 2a 2f   ... 6=Sunday */
7f730 0a 20 20 20 20 20 20 20 20 20 20 20 20 77 64 20  .            wd 
7f740 3d 20 28 69 6e 74 29 28 28 28 78 2e 69 4a 44 2b  = (int)(((x.iJD+
7f750 34 33 32 30 30 30 30 30 29 2f 38 36 34 30 30 30  43200000)/864000
7f760 30 30 29 25 37 29 3b 0a 20 20 20 20 20 20 20 20  00)%7);.        
7f770 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
7f780 69 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25  intf(3, &z[j],"%
7f790 30 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29  02d",(nDay+7-wd)
7f7a0 2f 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  /7);.           
7f7b0 20 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20   j += 2;.       
7f7c0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
7f7d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
7f7e0 70 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c  printf(4, &z[j],
7f7f0 22 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0a  "%03d",nDay+1);.
7f800 20 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d              j +=
7f810 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   3;.          }.
7f820 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7f830 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
7f840 20 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a 20     case 'J': {. 
7f850 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
7f860 5f 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 7a  _snprintf(20, &z
7f870 5b 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 4a  [j],"%.16g",x.iJ
7f880 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20  D/86400000.0);. 
7f890 20 20 20 20 20 20 20 20 20 6a 2b 3d 73 71 6c 69           j+=sqli
7f8a0 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 5b 6a  te3Strlen30(&z[j
7f8b0 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  ]);.          br
7f8c0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
7f8d0 20 20 20 20 20 20 20 63 61 73 65 20 27 6d 27 3a         case 'm':
7f8e0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
7f8f0 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32  tf(3, &z[j],"%02
7f900 64 22 2c 78 2e 4d 29 3b 20 6a 2b 3d 32 3b 20 62  d",x.M); j+=2; b
7f910 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
7f920 73 65 20 27 4d 27 3a 20 20 73 71 6c 69 74 65 33  se 'M':  sqlite3
7f930 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b  _snprintf(3, &z[
7f940 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 6d 29 3b 20  j],"%02d",x.m); 
7f950 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20  j+=2; break;.   
7f960 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b       case 's': {
7f970 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
7f980 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 30 2c 26  e3_snprintf(30,&
7f990 7a 5b 6a 5d 2c 22 25 6c 6c 64 22 2c 0a 20 20 20  z[j],"%lld",.   
7f9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f9b0 20 20 20 20 20 20 20 20 28 69 36 34 29 28 78 2e          (i64)(x.
7f9c0 69 4a 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36  iJD/1000 - 21086
7f9d0 36 37 36 2a 28 69 36 34 29 31 30 30 30 30 29 29  676*(i64)10000))
7f9e0 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d  ;.          j +=
7f9f0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
7fa00 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20  (&z[j]);.       
7fa10 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
7fa20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65    }.        case
7fa30 20 27 53 27 3a 20 20 73 71 6c 69 74 65 33 5f 73   'S':  sqlite3_s
7fa40 6e 70 72 69 6e 74 66 28 33 2c 26 7a 5b 6a 5d 2c  nprintf(3,&z[j],
7fa50 22 25 30 32 64 22 2c 28 69 6e 74 29 78 2e 73 29  "%02d",(int)x.s)
7fa60 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20  ; j+=2; break;. 
7fa70 20 20 20 20 20 20 20 63 61 73 65 20 27 77 27 3a         case 'w':
7fa80 20 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a   {.          z[j
7fa90 2b 2b 5d 20 3d 20 28 63 68 61 72 29 28 28 28 78  ++] = (char)(((x
7faa0 2e 69 4a 44 2b 31 32 39 36 30 30 30 30 30 29 2f  .iJD+129600000)/
7fab0 38 36 34 30 30 30 30 30 29 20 25 20 37 29 20 2b  86400000) % 7) +
7fac0 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '0';.          
7fad0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
7fae0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 59  .        case 'Y
7faf0 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ': {.          s
7fb00 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
7fb10 35 2c 26 7a 5b 6a 5d 2c 22 25 30 34 64 22 2c 78  5,&z[j],"%04d",x
7fb20 2e 59 29 3b 20 6a 2b 3d 73 71 6c 69 74 65 33 53  .Y); j+=sqlite3S
7fb30 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a  trlen30(&z[j]);.
7fb40 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7fb50 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
7fb60 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 7a 5b     default:   z[
7fb70 6a 2b 2b 5d 20 3d 20 27 25 27 3b 20 62 72 65 61  j++] = '%'; brea
7fb80 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
7fb90 0a 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b  .  }.  z[j] = 0;
7fba0 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
7fbb0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
7fbc0 7a 2c 20 2d 31 2c 0a 20 20 20 20 20 20 20 20 20  z, -1,.         
7fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 3d 3d               z==
7fbe0 7a 42 75 66 20 3f 20 53 51 4c 49 54 45 5f 54 52  zBuf ? SQLITE_TR
7fbf0 41 4e 53 49 45 4e 54 20 3a 20 53 51 4c 49 54 45  ANSIENT : SQLITE
7fc00 5f 44 59 4e 41 4d 49 43 29 3b 0a 7d 0a 0a 2f 2a  _DYNAMIC);.}../*
7fc10 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69 6d 65  .** current_time
7fc20 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ().**.** This fu
7fc30 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
7fc40 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73  he same value as
7fc50 20 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f   time('now')..*/
7fc60 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 74 69  .static void cti
7fc70 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  meFunc(.  sqlite
7fc80 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
7fc90 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
7fca0 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
7fcb0 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
7fcc0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
7fcd0 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
7fce0 74 55 73 65 64 32 29 3b 0a 20 20 74 69 6d 65 46  tUsed2);.  timeF
7fcf0 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20  unc(context, 0, 
7fd00 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72  0);.}../*.** cur
7fd10 72 65 6e 74 5f 64 61 74 65 28 29 0a 2a 2a 0a 2a  rent_date().**.*
7fd20 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
7fd30 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65  returns the same
7fd40 20 76 61 6c 75 65 20 61 73 20 64 61 74 65 28 27   value as date('
7fd50 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  now')..*/.static
7fd60 20 76 6f 69 64 20 63 64 61 74 65 46 75 6e 63 28   void cdateFunc(
7fd70 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
7fd80 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
7fd90 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
7fda0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f  lite3_value **No
7fdb0 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53  tUsed2.){.  UNUS
7fdc0 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
7fdd0 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
7fde0 3b 0a 20 20 64 61 74 65 46 75 6e 63 28 63 6f 6e  ;.  dateFunc(con
7fdf0 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a  text, 0, 0);.}..
7fe00 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 74 69  /*.** current_ti
7fe10 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 0a 2a 2a 20  mestamp().**.** 
7fe20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
7fe30 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76  turns the same v
7fe40 61 6c 75 65 20 61 73 20 64 61 74 65 74 69 6d 65  alue as datetime
7fe50 28 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74  ('now')..*/.stat
7fe60 69 63 20 76 6f 69 64 20 63 74 69 6d 65 73 74 61  ic void ctimesta
7fe70 6d 70 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  mpFunc(.  sqlite
7fe80 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
7fe90 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
7fea0 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
7feb0 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
7fec0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
7fed0 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
7fee0 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 74  tUsed2);.  datet
7fef0 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c  imeFunc(context,
7ff00 20 30 2c 20 30 29 3b 0a 7d 0a 23 65 6e 64 69 66   0, 0);.}.#endif
7ff10 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
7ff20 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d  ITE_OMIT_DATETIM
7ff30 45 5f 46 55 4e 43 53 29 20 2a 2f 0a 0a 23 69 66  E_FUNCS) */..#if
7ff40 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
7ff50 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 2f  DATETIME_FUNCS./
7ff60 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 69 62 72  *.** If the libr
7ff70 61 72 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20  ary is compiled 
7ff80 74 6f 20 6f 6d 69 74 20 74 68 65 20 66 75 6c 6c  to omit the full
7ff90 2d 73 63 61 6c 65 20 64 61 74 65 20 61 6e 64 20  -scale date and 
7ffa0 74 69 6d 65 0a 2a 2a 20 68 61 6e 64 6c 69 6e 67  time.** handling
7ffb0 20 28 74 6f 20 67 65 74 20 61 20 73 6d 61 6c 6c   (to get a small
7ffc0 65 72 20 62 69 6e 61 72 79 29 2c 20 74 68 65 20  er binary), the 
7ffd0 66 6f 6c 6c 6f 77 69 6e 67 20 6d 69 6e 69 6d 61  following minima
7ffe0 6c 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20  l version.** of 
7fff0 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 63 75  the functions cu
80000 72 72 65 6e 74 5f 74 69 6d 65 28 29 2c 20 63 75  rrent_time(), cu
80010 72 72 65 6e 74 5f 64 61 74 65 28 29 20 61 6e 64  rrent_date() and
80020 20 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61   current_timesta
80030 6d 70 28 29 0a 2a 2a 20 61 72 65 20 69 6e 63 6c  mp().** are incl
80040 75 64 65 64 20 69 6e 73 74 65 61 64 2e 20 54 68  uded instead. Th
80050 69 73 20 69 73 20 74 6f 20 73 75 70 70 6f 72 74  is is to support
80060 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74   column declarat
80070 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 69 6e 63  ions that.** inc
80080 6c 75 64 65 20 22 44 45 46 41 55 4c 54 20 43 55  lude "DEFAULT CU
80090 52 52 45 4e 54 5f 54 49 4d 45 22 20 65 74 63 2e  RRENT_TIME" etc.
800a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
800b0 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20 43 2d  tion uses the C-
800c0 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f 6e  library function
800d0 73 20 74 69 6d 65 28 29 2c 20 67 6d 74 69 6d 65  s time(), gmtime
800e0 28 29 0a 2a 2a 20 61 6e 64 20 73 74 72 66 74 69  ().** and strfti
800f0 6d 65 28 29 2e 20 54 68 65 20 66 6f 72 6d 61 74  me(). The format
80100 20 73 74 72 69 6e 67 20 74 6f 20 70 61 73 73 20   string to pass 
80110 74 6f 20 73 74 72 66 74 69 6d 65 28 29 20 69 73  to strftime() is
80120 20 73 75 70 70 6c 69 65 64 0a 2a 2a 20 61 73 20   supplied.** as 
80130 74 68 65 20 75 73 65 72 2d 64 61 74 61 20 66 6f  the user-data fo
80140 72 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 0a  r the function..
80150 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
80160 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 28 0a  urrentTimeFunc(.
80170 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
80180 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
80190 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
801a0 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
801b0 7b 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20  {.  time_t t;.  
801c0 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20  char *zFormat = 
801d0 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
801e0 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
801f0 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  t);.  sqlite3 *d
80200 62 3b 0a 20 20 64 6f 75 62 6c 65 20 72 54 3b 0a  b;.  double rT;.
80210 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b    char zBuf[20];
80220 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
80230 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 55 4e  ETER(argc);.  UN
80240 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
80250 72 67 76 29 3b 0a 0a 20 20 64 62 20 3d 20 73 71  rgv);..  db = sq
80260 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
80270 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
80280 3b 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 75 72  ;.  sqlite3OsCur
80290 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56 66  rentTime(db->pVf
802a0 73 2c 20 26 72 54 29 3b 0a 23 69 66 6e 64 65 66  s, &rT);.#ifndef
802b0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
802c0 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 74 20  ATING_POINT.  t 
802d0 3d 20 38 36 34 30 30 2e 30 2a 28 72 54 20 2d 20  = 86400.0*(rT - 
802e0 32 34 34 30 35 38 37 2e 35 29 20 2b 20 30 2e 35  2440587.5) + 0.5
802f0 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 77 69 74  ;.#else.  /* wit
80300 68 6f 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f  hout floating po
80310 69 6e 74 20 73 75 70 70 6f 72 74 2c 20 72 54 20  int support, rT 
80320 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a 20 61  will have.  ** a
80330 6c 72 65 61 64 79 20 6c 6f 73 74 20 66 72 61 63  lready lost frac
80340 74 69 6f 6e 61 6c 20 64 61 79 20 70 72 65 63 69  tional day preci
80350 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 74 20 3d  sion..  */.  t =
80360 20 38 36 34 30 30 20 2a 20 28 72 54 20 2d 20 32   86400 * (rT - 2
80370 34 34 30 35 38 37 29 20 2d 20 34 33 32 30 30 3b  440587) - 43200;
80380 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 48  .#endif.#ifdef H
80390 41 56 45 5f 47 4d 54 49 4d 45 5f 52 0a 20 20 7b  AVE_GMTIME_R.  {
803a0 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 73  .    struct tm s
803b0 4e 6f 77 3b 0a 20 20 20 20 67 6d 74 69 6d 65 5f  Now;.    gmtime_
803c0 72 28 26 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20  r(&t, &sNow);.  
803d0 20 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c    strftime(zBuf,
803e0 20 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 26 73   20, zFormat, &s
803f0 4e 6f 77 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  Now);.  }.#else.
80400 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74    {.    struct t
80410 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c 69  m *pTm;.    sqli
80420 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
80430 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
80440 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
80450 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a  TATIC_MASTER));.
80460 20 20 20 20 70 54 6d 20 3d 20 67 6d 74 69 6d 65      pTm = gmtime
80470 28 26 74 29 3b 0a 20 20 20 20 73 74 72 66 74 69  (&t);.    strfti
80480 6d 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f  me(zBuf, 20, zFo
80490 72 6d 61 74 2c 20 70 54 6d 29 3b 0a 20 20 20 20  rmat, pTm);.    
804a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
804b0 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ave(sqlite3Mutex
804c0 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
804d0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
804e0 29 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ));.  }.#endif..
804f0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
80500 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
80510 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  Buf, -1, SQLITE_
80520 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 65  TRANSIENT);.}.#e
80530 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
80540 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74   function regist
80550 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 20  ered all of the 
80560 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f 6e  above C function
80570 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e 63  s as SQL.** func
80580 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68 6f  tions.  This sho
80590 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79 20  uld be the only 
805a0 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20  routine in this 
805b0 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78 74  file with.** ext
805c0 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a  ernal linkage..*
805d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
805e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
805f0 69 73 74 65 72 44 61 74 65 54 69 6d 65 46 75 6e  isterDateTimeFun
80600 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20  ctions(void){.  
80610 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
80620 44 20 46 75 6e 63 44 65 66 20 61 44 61 74 65 54  D FuncDef aDateT
80630 69 6d 65 46 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23  imeFuncs[] = {.#
80640 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
80650 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43  IT_DATETIME_FUNC
80660 53 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6a  S.    FUNCTION(j
80670 75 6c 69 61 6e 64 61 79 2c 20 20 20 20 20 20 20  ulianday,       
80680 20 2d 31 2c 20 30 2c 20 30 2c 20 6a 75 6c 69 61   -1, 0, 0, julia
80690 6e 64 61 79 46 75 6e 63 20 29 2c 0a 20 20 20 20  ndayFunc ),.    
806a0 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 2c 20 20  FUNCTION(date,  
806b0 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30             -1, 0
806c0 2c 20 30 2c 20 64 61 74 65 46 75 6e 63 20 20 20  , 0, dateFunc   
806d0 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
806e0 4f 4e 28 74 69 6d 65 2c 20 20 20 20 20 20 20 20  ON(time,        
806f0 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 74       -1, 0, 0, t
80700 69 6d 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a  imeFunc      ),.
80710 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 64 61 74      FUNCTION(dat
80720 65 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20 2d  etime,         -
80730 31 2c 20 30 2c 20 30 2c 20 64 61 74 65 74 69 6d  1, 0, 0, datetim
80740 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46 55  eFunc  ),.    FU
80750 4e 43 54 49 4f 4e 28 73 74 72 66 74 69 6d 65 2c  NCTION(strftime,
80760 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20           -1, 0, 
80770 30 2c 20 73 74 72 66 74 69 6d 65 46 75 6e 63 20  0, strftimeFunc 
80780 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
80790 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20  (current_time,  
807a0 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 74 69      0, 0, 0, cti
807b0 6d 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 20 20  meFunc     ),.  
807c0 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65    FUNCTION(curre
807d0 6e 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c  nt_timestamp, 0,
807e0 20 30 2c 20 30 2c 20 63 74 69 6d 65 73 74 61 6d   0, 0, ctimestam
807f0 70 46 75 6e 63 29 2c 0a 20 20 20 20 46 55 4e 43  pFunc),.    FUNC
80800 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64 61 74  TION(current_dat
80810 65 2c 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c  e,      0, 0, 0,
80820 20 63 64 61 74 65 46 75 6e 63 20 20 20 20 20 29   cdateFunc     )
80830 2c 0a 23 65 6c 73 65 0a 20 20 20 20 53 54 52 5f  ,.#else.    STR_
80840 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74  FUNCTION(current
80850 5f 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 22  _time,      0, "
80860 25 48 3a 25 4d 3a 25 53 22 2c 20 20 20 20 20 20  %H:%M:%S",      
80870 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54 69      0, currentTi
80880 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54 52  meFunc),.    STR
80890 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e  _FUNCTION(curren
808a0 74 5f 74 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20  t_timestamp, 0, 
808b0 22 25 59 2d 25 6d 2d 25 64 22 2c 20 20 20 20 20  "%Y-%m-%d",     
808c0 20 20 20 20 20 30 2c 20 63 75 72 72 65 6e 74 54       0, currentT
808d0 69 6d 65 46 75 6e 63 29 2c 0a 20 20 20 20 53 54  imeFunc),.    ST
808e0 52 5f 46 55 4e 43 54 49 4f 4e 28 63 75 72 72 65  R_FUNCTION(curre
808f0 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30 2c  nt_date,      0,
80900 20 22 25 59 2d 25 6d 2d 25 64 20 25 48 3a 25 4d   "%Y-%m-%d %H:%M
80910 3a 25 53 22 2c 20 30 2c 20 63 75 72 72 65 6e 74  :%S", 0, current
80920 54 69 6d 65 46 75 6e 63 29 2c 0a 23 65 6e 64 69  TimeFunc),.#endi
80930 66 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a  f.  };.  int i;.
80940 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70    FuncDefHash *p
80950 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46  Hash = &GLOBAL(F
80960 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69  uncDefHash, sqli
80970 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f  te3GlobalFunctio
80980 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a  ns);.  FuncDef *
80990 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66  aFunc = (FuncDef
809a0 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65  *)&GLOBAL(FuncDe
809b0 66 2c 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63  f, aDateTimeFunc
809c0 73 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20  s);..  for(i=0; 
809d0 69 3c 41 72 72 61 79 53 69 7a 65 28 61 44 61 74  i<ArraySize(aDat
809e0 65 54 69 6d 65 46 75 6e 63 73 29 3b 20 69 2b 2b  eTimeFuncs); i++
809f0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75  ){.    sqlite3Fu
80a00 6e 63 44 65 66 49 6e 73 65 72 74 28 70 48 61 73  ncDefInsert(pHas
80a10 68 2c 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20  h, &aFunc[i]);. 
80a20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
80a30 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 61 74  ***** End of dat
80a40 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
80a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80a70 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
80a80 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
80a90 20 6f 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   os.c **********
80aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80ac0 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35  ****/./*.** 2005
80ad0 20 4e 6f 76 65 6d 62 65 72 20 32 39 0a 2a 2a 0a   November 29.**.
80ae0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
80af0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
80b00 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
80b10 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
80b20 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
80b30 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
80b40 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
80b50 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
80b60 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
80b70 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
80b80 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
80b90 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
80ba0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
80bb0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
80bc0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
80bd0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
80be0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
80bf0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
80c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80c40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
80c50 20 63 6f 6e 74 61 69 6e 73 20 4f 53 20 69 6e 74   contains OS int
80c60 65 72 66 61 63 65 20 63 6f 64 65 20 74 68 61 74  erface code that
80c70 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c   is common to al
80c80 6c 0a 2a 2a 20 61 72 63 68 69 74 65 63 74 75 72  l.** architectur
80c90 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 5f  es..*/.#define _
80ca0 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 20 31 0a 23  SQLITE_OS_C_ 1.#
80cb0 75 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53  undef _SQLITE_OS
80cc0 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64  _C_../*.** The d
80cd0 65 66 61 75 6c 74 20 53 51 4c 69 74 65 20 73 71  efault SQLite sq
80ce0 6c 69 74 65 33 5f 76 66 73 20 69 6d 70 6c 65 6d  lite3_vfs implem
80cf0 65 6e 74 61 74 69 6f 6e 73 20 64 6f 20 6e 6f 74  entations do not
80d00 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d 65 6d   allocate.** mem
80d10 6f 72 79 20 28 61 63 74 75 61 6c 6c 79 2c 20 6f  ory (actually, o
80d20 73 5f 75 6e 69 78 2e 63 20 61 6c 6c 6f 63 61 74  s_unix.c allocat
80d30 65 73 20 61 20 73 6d 61 6c 6c 20 61 6d 6f 75 6e  es a small amoun
80d40 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 66  t of memory.** f
80d50 72 6f 6d 20 77 69 74 68 69 6e 20 4f 73 4f 70 65  rom within OsOpe
80d60 6e 28 29 29 2c 20 62 75 74 20 73 6f 6d 65 20 74  n()), but some t
80d70 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70 6c 65  hird-party imple
80d80 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 79 2e 0a  mentations may..
80d90 2a 2a 20 53 6f 20 77 65 20 74 65 73 74 20 74 68  ** So we test th
80da0 65 20 65 66 66 65 63 74 73 20 6f 66 20 61 20 6d  e effects of a m
80db0 61 6c 6c 6f 63 28 29 20 66 61 69 6c 69 6e 67 20  alloc() failing 
80dc0 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 4f  and the sqlite3O
80dd0 73 58 58 58 28 29 0a 2a 2a 20 66 75 6e 63 74 69  sXXX().** functi
80de0 6f 6e 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c  on returning SQL
80df0 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20  ITE_IOERR_NOMEM 
80e00 75 73 69 6e 67 20 74 68 65 20 44 4f 5f 4f 53 5f  using the DO_OS_
80e10 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 6d 61 63 72  MALLOC_TEST macr
80e20 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  o..**.** The fol
80e30 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73  lowing functions
80e40 20 61 72 65 20 69 6e 73 74 72 75 6d 65 6e 74 65   are instrumente
80e50 64 20 66 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 66  d for malloc() f
80e60 61 69 6c 75 72 65 20 0a 2a 2a 20 74 65 73 74 69  ailure .** testi
80e70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71  ng:.**.**     sq
80e80 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 0a 2a 2a  lite3OsOpen().**
80e90 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 52 65       sqlite3OsRe
80ea0 61 64 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  ad().**     sqli
80eb0 74 65 33 4f 73 57 72 69 74 65 28 29 0a 2a 2a 20  te3OsWrite().** 
80ec0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53 79 6e      sqlite3OsSyn
80ed0 63 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  c().**     sqlit
80ee0 65 33 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 0a 2a 2f  e3OsLock().**.*/
80ef0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
80f00 49 54 45 5f 54 45 53 54 29 20 26 26 20 28 53 51  ITE_TEST) && (SQ
80f10 4c 49 54 45 5f 4f 53 5f 57 49 4e 3d 3d 30 29 0a  LITE_OS_WIN==0).
80f20 20 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f    #define DO_OS_
80f30 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 78 29 20 69  MALLOC_TEST(x) i
80f40 66 20 28 21 78 20 7c 7c 20 21 73 71 6c 69 74 65  f (!x || !sqlite
80f50 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 78 29  3IsMemJournal(x)
80f60 29 20 7b 20 20 20 20 20 5c 0a 20 20 20 20 76 6f  ) {     \.    vo
80f70 69 64 20 2a 70 54 73 74 41 6c 6c 6f 63 20 3d 20  id *pTstAlloc = 
80f80 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 31 30  sqlite3Malloc(10
80f90 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
80fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
80fb0 0a 20 20 20 20 69 66 20 28 21 70 54 73 74 41 6c  .    if (!pTstAl
80fc0 6c 6f 63 29 20 72 65 74 75 72 6e 20 53 51 4c 49  loc) return SQLI
80fd0 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 20  TE_IOERR_NOMEM; 
80fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80ff0 20 20 20 20 20 20 5c 0a 20 20 20 20 73 71 6c 69        \.    sqli
81000 74 65 33 5f 66 72 65 65 28 70 54 73 74 41 6c 6c  te3_free(pTstAll
81010 6f 63 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  oc);            
81020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
81030 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
81040 20 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69   }.#else.  #defi
81050 6e 65 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f  ne DO_OS_MALLOC_
81060 54 45 53 54 28 78 29 0a 23 65 6e 64 69 66 0a 0a  TEST(x).#endif..
81070 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
81080 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ing routines are
81090 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61   convenience wra
810a0 70 70 65 72 73 20 61 72 6f 75 6e 64 20 6d 65 74  ppers around met
810b0 68 6f 64 73 0a 2a 2a 20 6f 66 20 74 68 65 20 73  hods.** of the s
810c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65  qlite3_file obje
810d0 63 74 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 73  ct.  This is mos
810e0 74 6c 79 20 6a 75 73 74 20 73 79 6e 74 61 63 74  tly just syntact
810f0 69 63 20 73 75 67 61 72 2e 20 41 6c 6c 0a 2a 2a  ic sugar. All.**
81100 20 6f 66 20 74 68 69 73 20 77 6f 75 6c 64 20 62   of this would b
81110 65 20 63 6f 6d 70 6c 65 74 65 6c 79 20 61 75 74  e completely aut
81120 6f 6d 61 74 69 63 20 69 66 20 53 51 4c 69 74 65  omatic if SQLite
81130 20 77 65 72 65 20 63 6f 64 65 64 20 75 73 69 6e   were coded usin
81140 67 0a 2a 2a 20 43 2b 2b 20 69 6e 73 74 65 61 64  g.** C++ instead
81150 20 6f 66 20 70 6c 61 69 6e 20 6f 6c 64 20 43 2e   of plain old C.
81160 0a 2a 2f 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 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69  Close(sqlite3_fi
81190 6c 65 20 2a 70 49 64 29 7b 0a 20 20 69 6e 74 20  le *pId){.  int 
811a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
811b0 20 20 69 66 28 20 70 49 64 2d 3e 70 4d 65 74 68    if( pId->pMeth
811c0 6f 64 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ods ){.    rc = 
811d0 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78  pId->pMethods->x
811e0 43 6c 6f 73 65 28 70 49 64 29 3b 0a 20 20 20 20  Close(pId);.    
811f0 70 49 64 2d 3e 70 4d 65 74 68 6f 64 73 20 3d 20  pId->pMethods = 
81200 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
81210 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  rc;.}.SQLITE_PRI
81220 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
81230 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f 66  OsRead(sqlite3_f
81240 69 6c 65 20 2a 69 64 2c 20 76 6f 69 64 20 2a 70  ile *id, void *p
81250 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36  Buf, int amt, i6
81260 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f  4 offset){.  DO_
81270 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69  OS_MALLOC_TEST(i
81280 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  d);.  return id-
81290 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 52 65 61 64  >pMethods->xRead
812a0 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20  (id, pBuf, amt, 
812b0 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54  offset);.}.SQLIT
812c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
812d0 6c 69 74 65 33 4f 73 57 72 69 74 65 28 73 71 6c  lite3OsWrite(sql
812e0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 63  ite3_file *id, c
812f0 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c  onst void *pBuf,
81300 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66   int amt, i64 of
81310 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d  fset){.  DO_OS_M
81320 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a  ALLOC_TEST(id);.
81330 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
81340 74 68 6f 64 73 2d 3e 78 57 72 69 74 65 28 69 64  thods->xWrite(id
81350 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66  , pBuf, amt, off
81360 73 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  set);.}.SQLITE_P
81370 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
81380 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c  e3OsTruncate(sql
81390 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
813a0 36 34 20 73 69 7a 65 29 7b 0a 20 20 72 65 74 75  64 size){.  retu
813b0 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
813c0 3e 78 54 72 75 6e 63 61 74 65 28 69 64 2c 20 73  >xTruncate(id, s
813d0 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  ize);.}.SQLITE_P
813e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
813f0 65 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33  e3OsSync(sqlite3
81400 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66  _file *id, int f
81410 6c 61 67 73 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d  lags){.  DO_OS_M
81420 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a  ALLOC_TEST(id);.
81430 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65    return id->pMe
81440 74 68 6f 64 73 2d 3e 78 53 79 6e 63 28 69 64 2c  thods->xSync(id,
81450 20 66 6c 61 67 73 29 3b 0a 7d 0a 53 51 4c 49 54   flags);.}.SQLIT
81460 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
81470 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
81480 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
81490 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20  , i64 *pSize){. 
814a0 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45   DO_OS_MALLOC_TE
814b0 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e  ST(id);.  return
814c0 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
814d0 46 69 6c 65 53 69 7a 65 28 69 64 2c 20 70 53 69  FileSize(id, pSi
814e0 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ze);.}.SQLITE_PR
814f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
81500 33 4f 73 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  3OsLock(sqlite3_
81510 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
81520 63 6b 54 79 70 65 29 7b 0a 20 20 44 4f 5f 4f 53  ckType){.  DO_OS
81530 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29  _MALLOC_TEST(id)
81540 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  ;.  return id->p
81550 4d 65 74 68 6f 64 73 2d 3e 78 4c 6f 63 6b 28 69  Methods->xLock(i
81560 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b 0a 7d 0a  d, lockType);.}.
81570 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
81580 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  nt sqlite3OsUnlo
81590 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
815a0 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 54 79 70  *id, int lockTyp
815b0 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  e){.  return id-
815c0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 55 6e 6c 6f  >pMethods->xUnlo
815d0 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 29  ck(id, lockType)
815e0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
815f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
81600 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
81610 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
81620 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
81630 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f  ){.  DO_OS_MALLO
81640 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72 65  C_TEST(id);.  re
81650 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64  turn id->pMethod
81660 73 2d 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65  s->xCheckReserve
81670 64 4c 6f 63 6b 28 69 64 2c 20 70 52 65 73 4f 75  dLock(id, pResOu
81680 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  t);.}.SQLITE_PRI
81690 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
816a0 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71  OsFileControl(sq
816b0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
816c0 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41  int op, void *pA
816d0 72 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64  rg){.  return id
816e0 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c  ->pMethods->xFil
816f0 65 43 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f 70 2c  eControl(id, op,
81700 20 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45   pArg);.}.SQLITE
81710 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
81720 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65  ite3OsSectorSize
81730 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
81740 64 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63  d){.  int (*xSec
81750 74 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33  torSize)(sqlite3
81760 5f 66 69 6c 65 2a 29 20 3d 20 69 64 2d 3e 70 4d  _file*) = id->pM
81770 65 74 68 6f 64 73 2d 3e 78 53 65 63 74 6f 72 53  ethods->xSectorS
81780 69 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 78  ize;.  return (x
81790 53 65 63 74 6f 72 53 69 7a 65 20 3f 20 78 53 65  SectorSize ? xSe
817a0 63 74 6f 72 53 69 7a 65 28 69 64 29 20 3a 20 53  ctorSize(id) : S
817b0 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45  QLITE_DEFAULT_SE
817c0 43 54 4f 52 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51  CTOR_SIZE);.}.SQ
817d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
817e0 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65   sqlite3OsDevice
817f0 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
81800 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
81810 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e  ){.  return id->
81820 70 4d 65 74 68 6f 64 73 2d 3e 78 44 65 76 69 63  pMethods->xDevic
81830 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
81840 28 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  (id);.}../*.** T
81850 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66  he next group of
81860 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f   routines are co
81870 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65  nvenience wrappe
81880 72 73 20 61 72 6f 75 6e 64 20 74 68 65 0a 2a 2a  rs around the.**
81890 20 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f   VFS methods..*/
818a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
818b0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  int sqlite3OsOpe
818c0 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  n(.  sqlite3_vfs
818d0 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74   *pVfs, .  const
818e0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20   char *zPath, . 
818f0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
81900 46 69 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61  File, .  int fla
81910 67 73 2c 20 0a 20 20 69 6e 74 20 2a 70 46 6c 61  gs, .  int *pFla
81920 67 73 4f 75 74 0a 29 7b 0a 20 20 69 6e 74 20 72  gsOut.){.  int r
81930 63 3b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f  c;.  DO_OS_MALLO
81940 43 5f 54 45 53 54 28 30 29 3b 0a 20 20 2f 2a 20  C_TEST(0);.  /* 
81950 30 78 37 66 31 66 20 69 73 20 61 20 6d 61 73 6b  0x7f1f is a mask
81960 20 6f 66 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   of SQLITE_OPEN_
81970 20 66 6c 61 67 73 20 74 68 61 74 20 61 72 65 20   flags that are 
81980 76 61 6c 69 64 20 74 6f 20 62 65 20 70 61 73 73  valid to be pass
81990 65 64 0a 20 20 2a 2a 20 64 6f 77 6e 20 69 6e 74  ed.  ** down int
819a0 6f 20 74 68 65 20 56 46 53 20 6c 61 79 65 72 2e  o the VFS layer.
819b0 20 20 53 6f 6d 65 20 53 51 4c 49 54 45 5f 4f 50    Some SQLITE_OP
819c0 45 4e 5f 20 66 6c 61 67 73 20 28 66 6f 72 20 65  EN_ flags (for e
819d0 78 61 6d 70 6c 65 2c 0a 20 20 2a 2a 20 53 51 4c  xample,.  ** SQL
819e0 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54  ITE_OPEN_FULLMUT
819f0 45 58 20 6f 72 20 53 51 4c 49 54 45 5f 4f 50 45  EX or SQLITE_OPE
81a00 4e 5f 53 48 41 52 45 44 43 41 43 48 45 29 20 61  N_SHAREDCACHE) a
81a10 72 65 20 62 6c 6f 63 6b 65 64 20 62 65 66 6f 72  re blocked befor
81a20 65 0a 20 20 2a 2a 20 72 65 61 63 68 69 6e 67 20  e.  ** reaching 
81a30 74 68 65 20 56 46 53 2e 20 2a 2f 0a 20 20 72 63  the VFS. */.  rc
81a40 20 3d 20 70 56 66 73 2d 3e 78 4f 70 65 6e 28 70   = pVfs->xOpen(p
81a50 56 66 73 2c 20 7a 50 61 74 68 2c 20 70 46 69 6c  Vfs, zPath, pFil
81a60 65 2c 20 66 6c 61 67 73 20 26 20 30 78 37 66 31  e, flags & 0x7f1
81a70 66 2c 20 70 46 6c 61 67 73 4f 75 74 29 3b 0a 20  f, pFlagsOut);. 
81a80 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
81a90 49 54 45 5f 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d  ITE_OK || pFile-
81aa0 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20 29 3b 0a  >pMethods==0 );.
81ab0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
81ac0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
81ad0 74 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74  t sqlite3OsDelet
81ae0 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  e(sqlite3_vfs *p
81af0 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Vfs, const char 
81b00 2a 7a 50 61 74 68 2c 20 69 6e 74 20 64 69 72 53  *zPath, int dirS
81b10 79 6e 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  ync){.  return p
81b20 56 66 73 2d 3e 78 44 65 6c 65 74 65 28 70 56 66  Vfs->xDelete(pVf
81b30 73 2c 20 7a 50 61 74 68 2c 20 64 69 72 53 79 6e  s, zPath, dirSyn
81b40 63 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  c);.}.SQLITE_PRI
81b50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
81b60 4f 73 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69  OsAccess(.  sqli
81b70 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a  te3_vfs *pVfs, .
81b80 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
81b90 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67  ath, .  int flag
81ba0 73 2c 20 0a 20 20 69 6e 74 20 2a 70 52 65 73 4f  s, .  int *pResO
81bb0 75 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41  ut.){.  DO_OS_MA
81bc0 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0a 20 20  LLOC_TEST(0);.  
81bd0 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 41 63  return pVfs->xAc
81be0 63 65 73 73 28 70 56 66 73 2c 20 7a 50 61 74 68  cess(pVfs, zPath
81bf0 2c 20 66 6c 61 67 73 2c 20 70 52 65 73 4f 75 74  , flags, pResOut
81c00 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
81c10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
81c20 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20  sFullPathname(. 
81c30 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
81c40 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  fs, .  const cha
81c50 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74  r *zPath, .  int
81c60 20 6e 50 61 74 68 4f 75 74 2c 20 0a 20 20 63 68   nPathOut, .  ch
81c70 61 72 20 2a 7a 50 61 74 68 4f 75 74 0a 29 7b 0a  ar *zPathOut.){.
81c80 20 20 7a 50 61 74 68 4f 75 74 5b 30 5d 20 3d 20    zPathOut[0] = 
81c90 30 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73  0;.  return pVfs
81ca0 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  ->xFullPathname(
81cb0 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e 50 61  pVfs, zPath, nPa
81cc0 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75 74 29  thOut, zPathOut)
81cd0 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
81ce0 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
81cf0 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52  ENSION.SQLITE_PR
81d00 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
81d10 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71 6c 69  te3OsDlOpen(sqli
81d20 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 63  te3_vfs *pVfs, c
81d30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
81d40 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73  ){.  return pVfs
81d50 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73 2c 20  ->xDlOpen(pVfs, 
81d60 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c 49 54 45  zPath);.}.SQLITE
81d70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
81d80 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 73  lite3OsDlError(s
81d90 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
81da0 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61  , int nByte, cha
81db0 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 70  r *zBufOut){.  p
81dc0 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28 70 56  Vfs->xDlError(pV
81dd0 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66 4f  fs, nByte, zBufO
81de0 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ut);.}.SQLITE_PR
81df0 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c  IVATE void (*sql
81e00 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71 6c 69  ite3OsDlSym(sqli
81e10 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 76  te3_vfs *pVfs, v
81e20 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 6f 6e 73  oid *pHdle, cons
81e30 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 29 28 76  t char *zSym))(v
81e40 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  oid){.  return p
81e50 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56 66 73  Vfs->xDlSym(pVfs
81e60 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d 29 3b 0a  , pHdle, zSym);.
81e70 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
81e80 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44   void sqlite3OsD
81e90 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76  lClose(sqlite3_v
81ea0 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a  fs *pVfs, void *
81eb0 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 56 66 73  pHandle){.  pVfs
81ec0 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c  ->xDlClose(pVfs,
81ed0 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6e   pHandle);.}.#en
81ee0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
81ef0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
81f00 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  N */.SQLITE_PRIV
81f10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
81f20 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  sRandomness(sqli
81f30 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69  te3_vfs *pVfs, i
81f40 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a  nt nByte, char *
81f50 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65 74 75  zBufOut){.  retu
81f60 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64 6f 6d  rn pVfs->xRandom
81f70 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79 74 65  ness(pVfs, nByte
81f80 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51  , zBufOut);.}.SQ
81f90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
81fa0 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28   sqlite3OsSleep(
81fb0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
81fc0 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29 7b 0a  s, int nMicro){.
81fd0 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d 3e 78    return pVfs->x
81fe0 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d 69 63  Sleep(pVfs, nMic
81ff0 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ro);.}.SQLITE_PR
82000 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
82010 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 73  3OsCurrentTime(s
82020 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
82030 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d 65 4f  , double *pTimeO
82040 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  ut){.  return pV
82050 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69 6d 65  fs->xCurrentTime
82060 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75 74 29  (pVfs, pTimeOut)
82070 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
82080 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
82090 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20 20 73  sOpenMalloc(.  s
820a0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
820b0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
820c0 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c 69 74  *zFile, .  sqlit
820d0 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69 6c 65  e3_file **ppFile
820e0 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a  , .  int flags,.
820f0 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73    int *pOutFlags
82100 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
82110 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 73  QLITE_NOMEM;.  s
82120 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
82130 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20 28 73  le;.  pFile = (s
82140 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71  qlite3_file *)sq
82150 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56 66 73  lite3Malloc(pVfs
82160 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20 20 69  ->szOsFile);.  i
82170 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20 20 20  f( pFile ){.    
82180 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
82190 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20  en(pVfs, zFile, 
821a0 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70 4f  pFile, flags, pO
821b0 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20 69 66  utFlags);.    if
821c0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
821d0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
821e0 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a 20 20  _free(pFile);.  
821f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a    }else{.      *
82200 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65 3b 0a  ppFile = pFile;.
82210 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
82220 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
82230 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
82240 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28 73  te3OsCloseFree(s
82250 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69  qlite3_file *pFi
82260 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  le){.  int rc = 
82270 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73  SQLITE_OK;.  ass
82280 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
82290 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43 6c  rc = sqlite3OsCl
822a0 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20 73 71  ose(pFile);.  sq
822b0 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65  lite3_free(pFile
822c0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
822d0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
822e0 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72 61 70  nction is a wrap
822f0 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20 4f  per around the O
82300 53 20 73 70 65 63 69 66 69 63 20 69 6d 70 6c 65  S specific imple
82310 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20  mentation of.** 
82320 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
82330 29 2e 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f  ). The purpose o
82340 66 20 74 68 65 20 77 72 61 70 70 65 72 20 69 73  f the wrapper is
82350 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 0a   to provide the.
82360 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69  ** ability to si
82370 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c 6f 63 20  mulate a malloc 
82380 66 61 69 6c 75 72 65 2c 20 73 6f 20 74 68 61 74  failure, so that
82390 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66   the handling of
823a0 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69 6e 20   an.** error in 
823b0 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
823c0 29 20 62 79 20 74 68 65 20 75 70 70 65 72 20 6c  ) by the upper l
823d0 61 79 65 72 73 20 63 61 6e 20 62 65 20 74 65 73  ayers can be tes
823e0 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
823f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
82400 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 29 7b 0a  e3OsInit(void){.
82410 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69    void *p = sqli
82420 74 65 33 5f 6d 61 6c 6c 6f 63 28 31 30 29 3b 0a  te3_malloc(10);.
82430 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
82440 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
82450 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
82460 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  (p);.  return sq
82470 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 3b  lite3_os_init();
82480 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6c 69  .}../*.** The li
82490 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69 73 74  st of all regist
824a0 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65 6d 65  ered VFS impleme
824b0 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61  ntations..*/.sta
824c0 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20  tic sqlite3_vfs 
824d0 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 76 66 73  * SQLITE_WSD vfs
824e0 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e  List = 0;.#defin
824f0 65 20 76 66 73 4c 69 73 74 20 47 4c 4f 42 41 4c  e vfsList GLOBAL
82500 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
82510 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a 2a 20  vfsList)../*.** 
82520 4c 6f 63 61 74 65 20 61 20 56 46 53 20 62 79 20  Locate a VFS by 
82530 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e 61 6d  name.  If no nam
82540 65 20 69 73 20 67 69 76 65 6e 2c 20 73 69 6d 70  e is given, simp
82550 6c 79 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a  ly return the.**
82560 20 66 69 72 73 74 20 56 46 53 20 6f 6e 20 74 68   first VFS on th
82570 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54  e list..*/.SQLIT
82580 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 66  E_API sqlite3_vf
82590 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73 5f 66  s *sqlite3_vfs_f
825a0 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ind(const char *
825b0 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74 65 33  zVfs){.  sqlite3
825c0 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30 3b 0a  _vfs *pVfs = 0;.
825d0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
825e0 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f  DSAFE.  sqlite3_
825f0 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a 23 65  mutex *mutex;.#e
82600 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
82610 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
82620 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  T.  int rc = sql
82630 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
82640 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
82650 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 23  turn 0;.#endif.#
82660 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
82670 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d 20 73  SAFE.  mutex = s
82680 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
82690 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
826a0 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65  ATIC_MASTER);.#e
826b0 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ndif.  sqlite3_m
826c0 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
826d0 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20 3d 20  );.  for(pVfs = 
826e0 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b 20 70  vfsList; pVfs; p
826f0 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78 74 29  Vfs=pVfs->pNext)
82700 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73 3d 3d  {.    if( zVfs==
82710 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69  0 ) break;.    i
82720 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73 2c 20  f( strcmp(zVfs, 
82730 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20  pVfs->zName)==0 
82740 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 73  ) break;.  }.  s
82750 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
82760 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  ve(mutex);.  ret
82770 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a  urn pVfs;.}../*.
82780 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46 53 20  ** Unlink a VFS 
82790 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65 64 20  from the linked 
827a0 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63 20 76  list.*/.static v
827b0 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28 73 71  oid vfsUnlink(sq
827c0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 29  lite3_vfs *pVfs)
827d0 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
827e0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73  te3_mutex_held(s
827f0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
82800 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
82810 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20 29 3b  ATIC_MASTER)) );
82820 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29  .  if( pVfs==0 )
82830 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70 20 2a  {.    /* No-op *
82840 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66  /.  }else if( vf
82850 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b 0a 20  sList==pVfs ){. 
82860 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 66     vfsList = pVf
82870 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73  s->pNext;.  }els
82880 65 20 69 66 28 20 76 66 73 4c 69 73 74 20 29 7b  e if( vfsList ){
82890 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73  .    sqlite3_vfs
828a0 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20   *p = vfsList;. 
828b0 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 4e 65     while( p->pNe
828c0 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74 21 3d  xt && p->pNext!=
828d0 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20 70 20  pVfs ){.      p 
828e0 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  = p->pNext;.    
828f0 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65  }.    if( p->pNe
82900 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20  xt==pVfs ){.    
82910 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66    p->pNext = pVf
82920 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  s->pNext;.    }.
82930 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67    }.}../*.** Reg
82940 69 73 74 65 72 20 61 20 56 46 53 20 77 69 74 68  ister a VFS with
82950 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20 49 74   the system.  It
82960 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f 20   is harmless to 
82970 72 65 67 69 73 74 65 72 20 74 68 65 20 73 61 6d  register the sam
82980 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69 70 6c  e.** VFS multipl
82990 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20 6e 65  e times.  The ne
829a0 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68  w VFS becomes th
829b0 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d 61 6b  e default if mak
829c0 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72 75 65  eDflt is.** true
829d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
829e0 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  int sqlite3_vfs_
829f0 72 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33  register(sqlite3
82a00 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
82a10 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 73 71 6c  makeDflt){.  sql
82a20 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
82a30 78 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53  x = 0;.#ifndef S
82a40 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
82a50 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  NIT.  int rc = s
82a60 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
82a70 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  e();.  if( rc ) 
82a80 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69  return rc;.#endi
82a90 66 0a 20 20 6d 75 74 65 78 20 3d 20 73 71 6c 69  f.  mutex = sqli
82aa0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
82ab0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
82ac0 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71 6c  C_MASTER);.  sql
82ad0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
82ae0 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e  (mutex);.  vfsUn
82af0 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 69 66  link(pVfs);.  if
82b00 28 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20 76 66  ( makeDflt || vf
82b10 73 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  sList==0 ){.    
82b20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 66  pVfs->pNext = vf
82b30 73 4c 69 73 74 3b 0a 20 20 20 20 76 66 73 4c 69  sList;.    vfsLi
82b40 73 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d 65 6c  st = pVfs;.  }el
82b50 73 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e  se{.    pVfs->pN
82b60 65 78 74 20 3d 20 76 66 73 4c 69 73 74 2d 3e 70  ext = vfsList->p
82b70 4e 65 78 74 3b 0a 20 20 20 20 76 66 73 4c 69 73  Next;.    vfsLis
82b80 74 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66 73 3b  t->pNext = pVfs;
82b90 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 76 66  .  }.  assert(vf
82ba0 73 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  sList);.  sqlite
82bb0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
82bc0 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  tex);.  return S
82bd0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
82be0 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61 20  ** Unregister a 
82bf0 56 46 53 20 73 6f 20 74 68 61 74 20 69 74 20 69  VFS so that it i
82c00 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63 63 65  s no longer acce
82c10 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ssible..*/.SQLIT
82c20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
82c30 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
82c40 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
82c50 66 73 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  fs){.#if SQLITE_
82c60 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c  THREADSAFE.  sql
82c70 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
82c80 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78  x = sqlite3Mutex
82c90 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
82ca0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
82cb0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  );.#endif.  sqli
82cc0 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
82cd0 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55 6e 6c  mutex);.  vfsUnl
82ce0 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73 71 6c  ink(pVfs);.  sql
82cf0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
82d00 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  (mutex);.  retur
82d10 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
82d20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
82d30 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a 2a 2a  End of os.c ****
82d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
82d70 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
82d80 42 65 67 69 6e 20 66 69 6c 65 20 66 61 75 6c 74  Begin file fault
82d90 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
82da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
82dc0 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e 20 32  /*.** 2008 Jan 2
82dd0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
82de0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
82df0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
82e00 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
82e10 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
82e20 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
82e30 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
82e40 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
82e50 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
82e60 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
82e70 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
82e80 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
82e90 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
82ea0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
82eb0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
82ec0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
82ed0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
82ee0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
82ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82f30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
82f40 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74   contains code t
82f50 6f 20 73 75 70 70 6f 72 74 20 74 68 65 20 63 6f  o support the co
82f60 6e 63 65 70 74 20 6f 66 20 22 62 65 6e 69 67 6e  ncept of "benign
82f70 22 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69  " .** malloc fai
82f80 6c 75 72 65 73 20 28 77 68 65 6e 20 74 68 65 20  lures (when the 
82f90 78 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65  xMalloc() or xRe
82fa0 61 6c 6c 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f  alloc() method o
82fb0 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  f the.** sqlite3
82fc0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72  _mem_methods str
82fd0 75 63 74 75 72 65 20 66 61 69 6c 73 20 74 6f 20  ucture fails to 
82fe0 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b  allocate a block
82ff0 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e   of memory.** an
83000 64 20 72 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a  d returns 0). .*
83010 2a 0a 2a 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63  *.** Most malloc
83020 20 66 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f   failures are no
83030 6e 2d 62 65 6e 69 67 6e 2e 20 41 66 74 65 72 20  n-benign. After 
83040 74 68 65 79 20 6f 63 63 75 72 2c 20 53 51 4c 69  they occur, SQLi
83050 74 65 0a 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74  te.** abandons t
83060 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61  he current opera
83070 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 73  tion and returns
83080 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28   an error code (
83090 75 73 75 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54  usually.** SQLIT
830a0 45 5f 4e 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20  E_NOMEM) to the 
830b0 75 73 65 72 2e 20 48 6f 77 65 76 65 72 2c 20 73  user. However, s
830c0 6f 6d 65 74 69 6d 65 73 20 61 20 66 61 75 6c 74  ometimes a fault
830d0 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72   is not necessar
830e0 69 6c 79 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f  ily.** fatal. Fo
830f0 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 61 20  r example, if a 
83100 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69  malloc fails whi
83110 6c 65 20 72 65 73 69 7a 69 6e 67 20 61 20 68 61  le resizing a ha
83120 73 68 20 74 61 62 6c 65 2c 20 74 68 69 73 20 0a  sh table, this .
83130 2a 2a 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79  ** is completely
83140 20 72 65 63 6f 76 65 72 61 62 6c 65 20 73 69 6d   recoverable sim
83150 70 6c 79 20 62 79 20 6e 6f 74 20 63 61 72 72 79  ply by not carry
83160 69 6e 67 20 6f 75 74 20 74 68 65 20 72 65 73 69  ing out the resi
83170 7a 65 2e 20 54 68 65 20 0a 2a 2a 20 68 61 73 68  ze. The .** hash
83180 20 74 61 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74   table will cont
83190 69 6e 75 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e  inue to function
831a0 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61   normally.  So a
831b0 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20   malloc failure 
831c0 0a 2a 2a 20 64 75 72 69 6e 67 20 61 20 68 61 73  .** during a has
831d0 68 20 74 61 62 6c 65 20 72 65 73 69 7a 65 20 69  h table resize i
831e0 73 20 61 20 62 65 6e 69 67 6e 20 66 61 75 6c 74  s a benign fault
831f0 2e 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53  ..*/...#ifndef S
83200 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54  QLITE_OMIT_BUILT
83210 49 4e 5f 54 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47  IN_TEST../*.** G
83220 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2e  lobal variables.
83230 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
83240 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48  ct BenignMallocH
83250 6f 6f 6b 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f  ooks BenignMallo
83260 63 48 6f 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53  cHooks;.static S
83270 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
83280 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f   BenignMallocHoo
83290 6b 73 20 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42  ks {.  void (*xB
832a0 65 6e 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64  enignBegin)(void
832b0 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e  );.  void (*xBen
832c0 69 67 6e 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d  ignEnd)(void);.}
832d0 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20   sqlite3Hooks = 
832e0 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68  { 0, 0 };../* Th
832f0 65 20 22 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63  e "wsdHooks" mac
83300 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20  ro will resolve 
83310 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  to the appropria
83320 74 65 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48  te BenignMallocH
83330 6f 6f 6b 73 0a 2a 2a 20 73 74 72 75 63 74 75 72  ooks.** structur
83340 65 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20  e.  If writable 
83350 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75  static data is u
83360 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68  nsupported on th
83370 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20  e target,.** we 
83380 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74  have to locate t
83390 68 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20  he state vector 
833a0 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e  at run-time.  In
833b0 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e   the more common
833c0 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77  .** case where w
833d0 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64  ritable static d
833e0 61 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64  ata is supported
833f0 2c 20 77 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72  , wsdHooks can r
83400 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a  efer directly.**
83410 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33   to the "sqlite3
83420 48 6f 6f 6b 73 22 20 73 74 61 74 65 20 76 65 63  Hooks" state vec
83430 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f  tor declared abo
83440 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ve..*/.#ifdef SQ
83450 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20  LITE_OMIT_WSD.# 
83460 64 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49  define wsdHooksI
83470 6e 69 74 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61  nit \.  BenignMa
83480 6c 6c 6f 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26  llocHooks *x = &
83490 47 4c 4f 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c  GLOBAL(BenignMal
834a0 6c 6f 63 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33  locHooks,sqlite3
834b0 48 6f 6f 6b 73 29 0a 23 20 64 65 66 69 6e 65 20  Hooks).# define 
834c0 77 73 64 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65  wsdHooks x[0].#e
834d0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64  lse.# define wsd
834e0 48 6f 6f 6b 73 49 6e 69 74 0a 23 20 64 65 66 69  HooksInit.# defi
834f0 6e 65 20 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69  ne wsdHooks sqli
83500 74 65 33 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a  te3Hooks.#endif.
83510 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
83520 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77   hooks to call w
83530 68 65 6e 20 73 71 6c 69 74 65 33 42 65 67 69 6e  hen sqlite3Begin
83540 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61  BenignMalloc() a
83550 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64  nd.** sqlite3End
83560 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61  BenignMalloc() a
83570 72 65 20 63 61 6c 6c 65 64 2c 20 72 65 73 70 65  re called, respe
83580 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  ctively..*/.SQLI
83590 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
835a0 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c  sqlite3BenignMal
835b0 6c 6f 63 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 64  locHooks(.  void
835c0 20 28 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29   (*xBenignBegin)
835d0 28 76 6f 69 64 29 2c 0a 20 20 76 6f 69 64 20 28  (void),.  void (
835e0 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69  *xBenignEnd)(voi
835f0 64 29 0a 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73  d).){.  wsdHooks
83600 49 6e 69 74 3b 0a 20 20 77 73 64 48 6f 6f 6b 73  Init;.  wsdHooks
83610 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20  .xBenignBegin = 
83620 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20  xBenignBegin;.  
83630 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e  wsdHooks.xBenign
83640 45 6e 64 20 3d 20 78 42 65 6e 69 67 6e 45 6e 64  End = xBenignEnd
83650 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
83660 28 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67  (sqlite3EndBenig
83670 6e 4d 61 6c 6c 6f 63 28 29 29 20 69 73 20 63 61  nMalloc()) is ca
83680 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65 20 63  lled by SQLite c
83690 6f 64 65 20 74 6f 20 69 6e 64 69 63 61 74 65 20  ode to indicate 
836a0 74 68 61 74 0a 2a 2a 20 73 75 62 73 65 71 75 65  that.** subseque
836b0 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  nt malloc failur
836c0 65 73 20 61 72 65 20 62 65 6e 69 67 6e 2e 20 41  es are benign. A
836d0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
836e0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
836f0 29 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 74  ).** indicates t
83700 68 61 74 20 73 75 62 73 65 71 75 65 6e 74 20 6d  hat subsequent m
83710 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61  alloc failures a
83720 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a  re non-benign..*
83730 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
83740 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67   void sqlite3Beg
83750 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76  inBenignMalloc(v
83760 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73  oid){.  wsdHooks
83770 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48  Init;.  if( wsdH
83780 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69  ooks.xBenignBegi
83790 6e 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b  n ){.    wsdHook
837a0 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 28 29  s.xBenignBegin()
837b0 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50  ;.  }.}.SQLITE_P
837c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
837d0 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
837e0 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48  oc(void){.  wsdH
837f0 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20  ooksInit;.  if( 
83800 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e  wsdHooks.xBenign
83810 45 6e 64 20 29 7b 0a 20 20 20 20 77 73 64 48 6f  End ){.    wsdHo
83820 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e 64 28 29  oks.xBenignEnd()
83830 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20  ;.  }.}..#endif 
83840 20 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c    /* #ifndef SQL
83850 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
83860 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  _TEST */../*****
83870 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
83880 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a   fault.c *******
83890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
838c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
838d0 66 69 6c 65 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a  file mem0.c ****
838e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
838f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83900 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
83910 32 30 30 38 20 4f 63 74 6f 62 65 72 20 32 38 0a  2008 October 28.
83920 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
83930 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
83940 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
83950 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
83960 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
83970 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
83980 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
83990 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
839a0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
839b0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
839c0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
839d0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
839e0 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
839f0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
83a00 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
83a10 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
83a20 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
83a30 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
83a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
83a80 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
83a90 6f 6e 74 61 69 6e 73 20 61 20 6e 6f 2d 6f 70 20  ontains a no-op 
83aa0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
83ab0 6e 20 64 72 69 76 65 72 73 20 66 6f 72 20 75 73  n drivers for us
83ac0 65 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45  e when.** SQLITE
83ad0 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20  _ZERO_MALLOC is 
83ae0 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 61 6c  defined.  The al
83af0 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73  location drivers
83b00 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20   implemented.** 
83b10 68 65 72 65 20 61 6c 77 61 79 73 20 66 61 69 6c  here always fail
83b20 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  .  SQLite will n
83b30 6f 74 20 6f 70 65 72 61 74 65 20 77 69 74 68 20  ot operate with 
83b40 74 68 65 73 65 20 64 72 69 76 65 72 73 2e 20 20  these drivers.  
83b50 54 68 65 73 65 0a 2a 2a 20 61 72 65 20 6d 65 72  These.** are mer
83b60 65 6c 79 20 70 6c 61 63 65 68 6f 6c 64 65 72 73  ely placeholders
83b70 2e 20 20 52 65 61 6c 20 64 72 69 76 65 72 73 20  .  Real drivers 
83b80 6d 75 73 74 20 62 65 20 73 75 62 73 74 69 74 75  must be substitu
83b90 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c  ted using.** sql
83ba0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 62 65  ite3_config() be
83bb0 66 6f 72 65 20 53 51 4c 69 74 65 20 77 69 6c 6c  fore SQLite will
83bc0 20 6f 70 65 72 61 74 65 2e 0a 2a 2f 0a 0a 2f 2a   operate..*/../*
83bd0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
83be0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
83bf0 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20  llocator is the 
83c00 64 65 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a  default.  It is.
83c10 2a 2a 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20  ** used when no 
83c20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c  other memory all
83c30 6f 63 61 74 6f 72 20 69 73 20 73 70 65 63 69 66  ocator is specif
83c40 69 65 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c  ied using compil
83c50 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73  e-time.** macros
83c60 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
83c70 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 0a 0a  TE_ZERO_MALLOC..
83c80 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 76 65 72 73  /*.** No-op vers
83c90 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 6d 65 6d 6f  ions of all memo
83ca0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f  ry allocation ro
83cb0 75 74 69 6e 65 73 0a 2a 2f 0a 73 74 61 74 69 63  utines.*/.static
83cc0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65   void *sqlite3Me
83cd0 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74  mMalloc(int nByt
83ce0 65 29 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a  e){ return 0; }.
83cf0 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
83d00 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20  te3MemFree(void 
83d10 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 72 6e  *pPrior){ return
83d20 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ; }.static void 
83d30 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c  *sqlite3MemReall
83d40 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 2c  oc(void *pPrior,
83d50 20 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 74   int nByte){ ret
83d60 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20  urn 0; }.static 
83d70 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 53 69  int sqlite3MemSi
83d80 7a 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72 29  ze(void *pPrior)
83d90 7b 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74  { return 0; }.st
83da0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
83db0 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e  MemRoundup(int n
83dc0 29 7b 20 72 65 74 75 72 6e 20 6e 3b 20 7d 0a 73  ){ return n; }.s
83dd0 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65  tatic int sqlite
83de0 33 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e  3MemInit(void *N
83df0 6f 74 55 73 65 64 29 7b 20 72 65 74 75 72 6e 20  otUsed){ return 
83e00 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61  SQLITE_OK; }.sta
83e10 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
83e20 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64  MemShutdown(void
83e30 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74 75   *NotUsed){ retu
83e40 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rn; }../*.** Thi
83e50 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65  s routine is the
83e60 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e   only routine in
83e70 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20   this file with 
83e80 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65  external linkage
83e90 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65  ..**.** Populate
83ea0 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d   the low-level m
83eb0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
83ec0 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65   function pointe
83ed0 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33  rs in.** sqlite3
83ee0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77  GlobalConfig.m w
83ef0 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ith pointers to 
83f00 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20  the routines in 
83f10 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51  this file..*/.SQ
83f20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
83f30 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44  d sqlite3MemSetD
83f40 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20  efault(void){.  
83f50 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
83f60 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
83f70 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20   defaultMethods 
83f80 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33  = {.     sqlite3
83f90 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20  MemMalloc,.     
83fa0 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a  sqlite3MemFree,.
83fb0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52       sqlite3MemR
83fc0 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c  ealloc,.     sql
83fd0 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20  ite3MemSize,.   
83fe0 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e    sqlite3MemRoun
83ff0 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  dup,.     sqlite
84000 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73  3MemInit,.     s
84010 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77  qlite3MemShutdow
84020 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20  n,.     0.  };. 
84030 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
84040 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41  SQLITE_CONFIG_MA
84050 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65  LLOC, &defaultMe
84060 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23 65 6e 64 69  thods);.}..#endi
84070 66 20 2f 2a 20 53 51 4c 49 54 45 5f 5a 45 52 4f  f /* SQLITE_ZERO
84080 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a  _MALLOC */../***
84090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
840a0 6f 66 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a  of mem0.c ******
840b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
840c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
840d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
840e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
840f0 6e 20 66 69 6c 65 20 6d 65 6d 31 2e 63 20 2a 2a  n file mem1.c **
84100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
84130 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34  * 2007 August 14
84140 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
84150 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
84160 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
84170 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
84180 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
84190 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
841a0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
841b0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
841c0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
841d0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
841e0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
841f0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
84200 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
84210 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
84220 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
84230 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
84240 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
84250 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
84260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
842a0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
842b0 63 6f 6e 74 61 69 6e 73 20 6c 6f 77 2d 6c 65 76  contains low-lev
842c0 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  el memory alloca
842d0 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f 72  tion drivers for
842e0 20 77 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20   when.** SQLite 
842f0 77 69 6c 6c 20 75 73 65 20 74 68 65 20 73 74 61  will use the sta
84300 6e 64 61 72 64 20 43 2d 6c 69 62 72 61 72 79 20  ndard C-library 
84310 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66  malloc/realloc/f
84320 72 65 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a  ree interface.**
84330 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d   to obtain the m
84340 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 0a  emory it needs..
84350 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
84360 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65  contains impleme
84370 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ntations of the 
84380 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
84390 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72   allocation.** r
843a0 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 65  outines specifie
843b0 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  d in the sqlite3
843c0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  _mem_methods obj
843d0 65 63 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ect..*/../*.** T
843e0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
843f0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
84400 74 6f 72 20 69 73 20 74 68 65 20 64 65 66 61 75  tor is the defau
84410 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73  lt.  It is.** us
84420 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72  ed when no other
84430 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
84440 72 20 69 73 20 73 70 65 63 69 66 69 65 64 20 75  r is specified u
84450 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  sing compile-tim
84460 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a  e.** macros..*/.
84470 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 59  #ifdef SQLITE_SY
84480 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a  STEM_MALLOC../*.
84490 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f 63 28 29  ** Like malloc()
844a0 2c 20 62 75 74 20 72 65 6d 65 6d 62 65 72 20 74  , but remember t
844b0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61  he size of the a
844c0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 6f 20  llocation.** so 
844d0 74 68 61 74 20 77 65 20 63 61 6e 20 66 69 6e 64  that we can find
844e0 20 69 74 20 6c 61 74 65 72 20 75 73 69 6e 67 20   it later using 
844f0 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 29  sqlite3MemSize()
84500 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73  ..**.** For this
84510 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69   low-level routi
84520 6e 65 2c 20 77 65 20 61 72 65 20 67 75 61 72 61  ne, we are guara
84530 6e 74 65 65 64 20 74 68 61 74 20 6e 42 79 74 65  nteed that nByte
84540 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a 20 63 61  >0 because.** ca
84550 73 65 73 20 6f 66 20 6e 42 79 74 65 3c 3d 30 20  ses of nByte<=0 
84560 77 69 6c 6c 20 62 65 20 69 6e 74 65 72 63 65 70  will be intercep
84570 74 65 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69  ted and dealt wi
84580 74 68 20 62 79 20 68 69 67 68 65 72 20 6c 65 76  th by higher lev
84590 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a  el.** routines..
845a0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
845b0 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
845c0 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 73  (int nByte){.  s
845d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b  qlite3_int64 *p;
845e0 0a 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65  .  assert( nByte
845f0 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20  >0 );.  nByte = 
84600 52 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20  ROUND8(nByte);. 
84610 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79   p = malloc( nBy
84620 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 20  te+8 );.  if( p 
84630 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42  ){.    p[0] = nB
84640 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20  yte;.    p++;.  
84650 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  }.  return (void
84660 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c   *)p;.}../*.** L
84670 69 6b 65 20 66 72 65 65 28 29 20 62 75 74 20 77  ike free() but w
84680 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74  orks for allocat
84690 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64 20 66 72  ions obtained fr
846a0 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c  om sqlite3MemMal
846b0 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69  loc().** or sqli
846c0 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 29 2e  te3MemRealloc().
846d0 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20  .**.** For this 
846e0 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e  low-level routin
846f0 65 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e  e, we already kn
84700 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72 21 3d  ow that pPrior!=
84710 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61 73 65 73  0 since.** cases
84720 20 77 68 65 72 65 20 70 50 72 69 6f 72 3d 3d 30   where pPrior==0
84730 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20   will have been 
84740 69 6e 74 65 63 65 70 74 65 64 20 61 6e 64 20 64  intecepted and d
84750 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20 62 79 20  ealt with.** by 
84760 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75  higher-level rou
84770 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tines..*/.static
84780 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
84790 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f  Free(void *pPrio
847a0 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  r){.  sqlite3_in
847b0 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65  t64 *p = (sqlite
847c0 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b  3_int64*)pPrior;
847d0 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f  .  assert( pPrio
847e0 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d 3b 0a 20  r!=0 );.  p--;. 
847f0 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a   free(p);.}../*.
84800 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c 6f 63 28  ** Like realloc(
84810 29 2e 20 20 52 65 73 69 7a 65 20 61 6e 20 61 6c  ).  Resize an al
84820 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69 6f 75  location previou
84830 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  sly obtained fro
84840 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 65 6d 4d  m.** sqlite3MemM
84850 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46  alloc()..**.** F
84860 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65  or this low-leve
84870 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 77 65 20  l interface, we 
84880 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f 72  know that pPrior
84890 21 3d 30 2e 20 20 43 61 73 65 73 20 77 68 65 72  !=0.  Cases wher
848a0 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d 30 20 77  e.** pPrior==0 w
848b0 68 69 6c 65 20 68 61 76 65 20 62 65 65 6e 20 69  hile have been i
848c0 6e 74 65 72 63 65 70 74 65 64 20 62 79 20 68 69  ntercepted by hi
848d0 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69  gher-level routi
848e0 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64 69 72 65  ne and.** redire
848f0 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c 6f 63 2e  cted to xMalloc.
84900 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20 77 65 20    Similarly, we 
84910 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79 74 65 3e  know that nByte>
84920 30 20 62 65 63 61 75 73 65 73 0a 2a 2a 20 63 61  0 becauses.** ca
84930 73 65 73 20 77 68 65 72 65 20 6e 42 79 74 65 3c  ses where nByte<
84940 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65  =0 will have bee
84950 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79  n intercepted by
84960 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 0a 2a 2a   higher-level.**
84970 20 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 72 65   routines and re
84980 64 69 72 65 63 74 65 64 20 74 6f 20 78 46 72 65  directed to xFre
84990 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
849a0 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61  d *sqlite3MemRea
849b0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f  lloc(void *pPrio
849c0 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  r, int nByte){. 
849d0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
849e0 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74  p = (sqlite3_int
849f0 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 61 73  64*)pPrior;.  as
84a00 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d 30 20  sert( pPrior!=0 
84a10 26 26 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20  && nByte>0 );.  
84a20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e  nByte = ROUND8(n
84a30 42 79 74 65 29 3b 0a 20 20 70 20 3d 20 28 73 71  Byte);.  p = (sq
84a40 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72  lite3_int64*)pPr
84a50 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 70 20  ior;.  p--;.  p 
84a60 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20 6e 42 79  = realloc(p, nBy
84a70 74 65 2b 38 20 29 3b 0a 20 20 69 66 28 20 70 20  te+8 );.  if( p 
84a80 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 6e 42  ){.    p[0] = nB
84a90 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b 0a 20 20  yte;.    p++;.  
84aa0 7d 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64  }.  return (void
84ab0 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  *)p;.}../*.** Re
84ac0 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f 63 61 74  port the allocat
84ad0 65 64 20 73 69 7a 65 20 6f 66 20 61 20 70 72 69  ed size of a pri
84ae0 6f 72 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 78  or return from x
84af0 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 78  Malloc().** or x
84b00 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74  Realloc()..*/.st
84b10 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
84b20 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50  MemSize(void *pP
84b30 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  rior){.  sqlite3
84b40 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20 69 66 28  _int64 *p;.  if(
84b50 20 70 50 72 69 6f 72 3d 3d 30 20 29 20 72 65 74   pPrior==0 ) ret
84b60 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 28 73 71  urn 0;.  p = (sq
84b70 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72  lite3_int64*)pPr
84b80 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20 20 72 65  ior;.  p--;.  re
84b90 74 75 72 6e 20 28 69 6e 74 29 70 5b 30 5d 3b 0a  turn (int)p[0];.
84ba0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75  }../*.** Round u
84bb0 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65  p a request size
84bc0 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c   to the next val
84bd0 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69  id allocation si
84be0 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ze..*/.static in
84bf0 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e  t sqlite3MemRoun
84c00 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65  dup(int n){.  re
84c10 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e 29 3b 0a  turn ROUND8(n);.
84c20 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  }../*.** Initial
84c30 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e  ize this module.
84c40 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
84c50 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f  qlite3MemInit(vo
84c60 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  id *NotUsed){.  
84c70 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
84c80 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74  (NotUsed);.  ret
84c90 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
84ca0 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69 61  ../*.** Deinitia
84cb0 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c 65  lize this module
84cc0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
84cd0 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64   sqlite3MemShutd
84ce0 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  own(void *NotUse
84cf0 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
84d00 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
84d10 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  .  return;.}../*
84d20 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
84d30 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75   is the only rou
84d40 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c  tine in this fil
84d50 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20  e with external 
84d60 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50  linkage..**.** P
84d70 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d  opulate the low-
84d80 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
84d90 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  ocation function
84da0 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20   pointers in.** 
84db0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
84dc0 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74  fig.m with point
84dd0 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75 74 69  ers to the routi
84de0 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  nes in this file
84df0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
84e00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
84e10 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28 76 6f  MemSetDefault(vo
84e20 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  id){.  static co
84e30 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
84e40 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c 74 4d  methods defaultM
84e50 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20  ethods = {.     
84e60 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
84e70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
84e80 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71 6c 69  mFree,.     sqli
84e90 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20  te3MemRealloc,. 
84ea0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 69      sqlite3MemSi
84eb0 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  ze,.     sqlite3
84ec0 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20  MemRoundup,.    
84ed0 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c   sqlite3MemInit,
84ee0 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
84ef0 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30  Shutdown,.     0
84f00 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f  .  };.  sqlite3_
84f10 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f  config(SQLITE_CO
84f20 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65  NFIG_MALLOC, &de
84f30 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d  faultMethods);.}
84f40 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
84f50 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43  TE_SYSTEM_MALLOC
84f60 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
84f70 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 31  **** End of mem1
84f80 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
84f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84fb0 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
84fc0 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
84fd0 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem2.c *********
84fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85000 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
85010 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a 2a 20  August 15.**.** 
85020 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
85030 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
85040 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
85050 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
85060 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
85070 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
85080 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
85090 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
850a0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
850b0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
850c0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
850d0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
850e0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
850f0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
85100 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
85110 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
85120 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
85130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85170 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
85180 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
85190 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  s low-level memo
851a0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72  ry allocation dr
851b0 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a 2a  ivers for when.*
851c0 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73  * SQLite will us
851d0 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43  e the standard C
851e0 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f  -library malloc/
851f0 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74  realloc/free int
85200 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 74  erface.** to obt
85210 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69  ain the memory i
85220 74 20 6e 65 65 64 73 20 77 68 69 6c 65 20 61 64  t needs while ad
85230 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 61 64 64  ding lots of add
85240 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 67 69 6e  itional debuggin
85250 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  g.** information
85260 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f 63 61 74   to each allocat
85270 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ion in order to 
85280 68 65 6c 70 20 64 65 74 65 63 74 20 61 6e 64 20  help detect and 
85290 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a 20 6c 65  fix memory.** le
852a0 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 79 20 75  aks and memory u
852b0 73 61 67 65 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a  sage errors..**.
852c0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
852d0 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e 74 61  tains implementa
852e0 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c 6f 77  tions of the low
852f0 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
85300 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74  location.** rout
85310 69 6e 65 73 20 73 70 65 63 69 66 69 65 64 20 69  ines specified i
85320 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 65  n the sqlite3_me
85330 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  m_methods object
85340 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ..*/../*.** This
85350 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20   version of the 
85360 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
85370 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 69 66   is used only if
85380 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d   the.** SQLITE_M
85390 45 4d 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73  EMDEBUG macro is
853a0 20 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 69 66 64   defined.*/.#ifd
853b0 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42  ef SQLITE_MEMDEB
853c0 55 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 61  UG../*.** The ba
853d0 63 6b 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e  cktrace function
853e0 61 6c 69 74 79 20 69 73 20 6f 6e 6c 79 20 61 76  ality is only av
853f0 61 69 6c 61 62 6c 65 20 77 69 74 68 20 47 4c 49  ailable with GLI
85400 42 43 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 5f 47  BC.*/.#ifdef __G
85410 4c 49 42 43 5f 5f 0a 20 20 65 78 74 65 72 6e 20  LIBC__.  extern 
85420 69 6e 74 20 62 61 63 6b 74 72 61 63 65 28 76 6f  int backtrace(vo
85430 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 65 78 74  id**,int);.  ext
85440 65 72 6e 20 76 6f 69 64 20 62 61 63 6b 74 72 61  ern void backtra
85450 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 76 6f  ce_symbols_fd(vo
85460 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c 69 6e  id*const*,int,in
85470 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  t);.#else.# defi
85480 6e 65 20 62 61 63 6b 74 72 61 63 65 28 41 2c 42  ne backtrace(A,B
85490 29 20 31 0a 23 20 64 65 66 69 6e 65 20 62 61 63  ) 1.# define bac
854a0 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66  ktrace_symbols_f
854b0 64 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66 0a  d(A,B,C).#endif.
854c0 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65 6d 6f  ./*.** Each memo
854d0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 6f  ry allocation lo
854e0 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  oks like this:.*
854f0 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *.**  ----------
85500 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85510 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85520 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
85530 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
85540 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20 62 61  *  | Title |  ba
85550 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73  cktrace pointers
85560 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20   |  MemBlockHdr 
85570 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 7c 20  |  allocation | 
85580 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a 20 20   EndGuard |.**  
85590 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
855a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
855b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
855c0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
855d0 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54  --------.**.** T
855e0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63  he application c
855f0 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 20 61 20  ode sees only a 
85600 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61  pointer to the a
85610 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65 20 68  llocation.  We h
85620 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b 20 75  ave.** to back u
85630 70 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c 6f 63  p from the alloc
85640 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 74 6f  ation pointer to
85650 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f   find the MemBlo
85660 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a 20 4d  ckHdr.  The.** M
85670 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c 6c 73  emBlockHdr tells
85680 20 75 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20   us the size of 
85690 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  the allocation a
856a0 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nd the number of
856b0 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 20 70 6f  .** backtrace po
856c0 69 6e 74 65 72 73 2e 20 20 54 68 65 72 65 20 69  inters.  There i
856d0 73 20 61 6c 73 6f 20 61 20 67 75 61 72 64 20 77  s also a guard w
856e0 6f 72 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f  ord at the end o
856f0 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63  f the.** MemBloc
85700 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  kHdr..*/.struct 
85710 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a 20 20  MemBlockHdr {.  
85720 69 36 34 20 69 53 69 7a 65 3b 20 20 20 20 20 20  i64 iSize;      
85730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
85740 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
85750 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a  his allocation *
85760 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  /.  struct MemBl
85770 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c 20 2a  ockHdr *pNext, *
85780 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e 6b 65  pPrev;  /* Linke
85790 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e  d list of all un
857a0 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a  freed memory */.
857b0 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63    char nBacktrac
857c0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
857d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
857e0 6f 66 20 62 61 63 6b 74 72 61 63 65 73 20 6f 6e  of backtraces on
857f0 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f 0a 20   this alloc */. 
85800 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61 63 65   char nBacktrace
85810 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 20 20 20  Slots;          
85820 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61 62 6c       /* Availabl
85830 65 20 62 61 63 6b 74 72 61 63 65 20 73 6c 6f 74  e backtrace slot
85840 73 20 2a 2f 0a 20 20 73 68 6f 72 74 20 6e 54 69  s */.  short nTi
85850 74 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  tle;            
85860 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
85870 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20 69 6e  tes of title; in
85880 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f 0a 20  cludes '\0' */. 
85890 20 69 6e 74 20 69 46 6f 72 65 47 75 61 72 64 3b   int iForeGuard;
858a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
858b0 20 20 20 20 20 2f 2a 20 47 75 61 72 64 20 77 6f       /* Guard wo
858c0 72 64 20 66 6f 72 20 73 61 6e 69 74 79 20 2a 2f  rd for sanity */
858d0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61 72 64  .};../*.** Guard
858e0 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66 69 6e   words.*/.#defin
858f0 65 20 46 4f 52 45 47 55 41 52 44 20 30 78 38 30  e FOREGUARD 0x80
85900 46 35 45 31 35 33 0a 23 64 65 66 69 6e 65 20 52  F5E153.#define R
85910 45 41 52 47 55 41 52 44 20 30 78 45 34 36 37 36  EARGUARD 0xE4676
85920 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65  B53../*.** Numbe
85930 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69 7a 65  r of malloc size
85940 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f 20 74   increments to t
85950 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  rack..*/.#define
85960 20 4e 43 53 49 5a 45 20 20 31 30 30 30 0a 0a 2f   NCSIZE  1000../
85970 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20  *.** All of the 
85980 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73  static variables
85990 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6d 6f   used by this mo
859a0 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65 63 74  dule are collect
859b0 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e  ed.** into a sin
859c0 67 6c 65 20 73 74 72 75 63 74 75 72 65 20 6e 61  gle structure na
859d0 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 68 69 73  med "mem".  This
859e0 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0a   is to keep the.
859f0 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69 61 62  ** static variab
85a00 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e  les organized an
85a10 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61 6d 65  d to reduce name
85a20 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a  space pollution.
85a30 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d 6f 64  ** when this mod
85a40 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 20  ule is combined 
85a50 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20 74 68  with other in th
85a60 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a  e amalgamation..
85a70 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  */.static struct
85a80 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20   {.  .  /*.  ** 
85a90 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c  Mutex to control
85aa0 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d   access to the m
85ab0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
85ac0 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f   subsystem..  */
85ad0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
85ae0 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20   *mutex;..  /*. 
85af0 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74 61 69   ** Head and tai
85b00 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69  l of a linked li
85b10 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73 74 61  st of all outsta
85b20 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e  nding allocation
85b30 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  s.  */.  struct 
85b40 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 46 69  MemBlockHdr *pFi
85b50 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20 4d 65  rst;.  struct Me
85b60 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61 73 74  mBlockHdr *pLast
85b70 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54  ;.  .  /*.  ** T
85b80 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 65 76  he number of lev
85b90 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61 63 65  els of backtrace
85ba0 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65 77 20   to save in new 
85bb0 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a  allocations..  *
85bc0 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74 72 61  /.  int nBacktra
85bd0 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78 42 61  ce;.  void (*xBa
85be0 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e  cktrace)(int, in
85bf0 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a 20 20  t, void **);..  
85c00 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20 74 65  /*.  ** Title te
85c10 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 20  xt to insert in 
85c20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20 62 6c  front of each bl
85c30 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e  ock.  */.  int n
85c40 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a  Title;        /*
85c50 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74 6c 65   Bytes of zTitle
85c60 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63 6c 75   to save.  Inclu
85c70 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70 61 64  des '\0' and pad
85c80 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 7a  ding */.  char z
85c90 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f 2a 20  Title[100];  /* 
85ca0 54 68 65 20 74 69 74 6c 65 20 74 65 78 74 20 2a  The title text *
85cb0 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 73 71  /..  /* .  ** sq
85cc0 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73 61 6c  lite3MallocDisal
85cd0 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e 74 73  low() increments
85ce0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
85cf0 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73 71 6c  ounter..  ** sql
85d00 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f 77 28  ite3MallocAllow(
85d10 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69 74 2e  ) decrements it.
85d20 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69 73 61  .  */.  int disa
85d30 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f 74 20  llow; /* Do not 
85d40 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61 6c 6c  allow memory all
85d50 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a  ocation */..  /*
85d60 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73 74 61  .  ** Gather sta
85d70 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65 20 73  tistics on the s
85d80 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61  izes of memory a
85d90 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a  llocations..  **
85da0 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20 74 68   nAlloc[i] is th
85db0 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f  e number of allo
85dc0 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20  cation attempts 
85dd0 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79 74 65  of i*8.  ** byte
85de0 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20 69 73  s.  i==NCSIZE is
85df0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61   the number of a
85e00 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70  llocation attemp
85e10 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69 7a 65  ts for.  ** size
85e20 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43 53 49  s more than NCSI
85e30 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 20 2a 2f  ZE*8 bytes..  */
85e40 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b 4e 43  .  int nAlloc[NC
85e50 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a 20 54  SIZE];      /* T
85e60 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 61  otal number of a
85e70 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20  llocations */.  
85e80 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e 43 53  int nCurrent[NCS
85e90 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75 72 72  IZE];    /* Curr
85ea0 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c  ent number of al
85eb0 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  locations */.  i
85ec0 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e 43 53  nt mxCurrent[NCS
85ed0 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67 68 77  IZE];   /* Highw
85ee0 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20 6e 43  ater mark for nC
85ef0 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d 65 6d  urrent */..} mem
85f00 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74  ;.../*.** Adjust
85f10 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 73 74   memory usage st
85f20 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74 61 74  atistics.*/.stat
85f30 69 63 20 76 6f 69 64 20 61 64 6a 75 73 74 53 74  ic void adjustSt
85f40 61 74 73 28 69 6e 74 20 69 53 69 7a 65 2c 20 69  ats(int iSize, i
85f50 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b 0a 20  nt increment){. 
85f60 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44 38 28   int i = ROUND8(
85f70 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66 28 20  iSize)/8;.  if( 
85f80 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a 20 20  i>NCSIZE-1 ){.  
85f90 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d 20 31    i = NCSIZE - 1
85fa0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e 63 72  ;.  }.  if( incr
85fb0 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20 20 6d  ement>0 ){.    m
85fc0 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b 3b 0a  em.nAlloc[i]++;.
85fd0 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74      mem.nCurrent
85fe0 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28 20 6d  [i]++;.    if( m
85ff0 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 6d  em.nCurrent[i]>m
86000 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 20  em.mxCurrent[i] 
86010 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d 78 43  ){.      mem.mxC
86020 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65 6d 2e  urrent[i] = mem.
86030 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20 20 20  nCurrent[i];.   
86040 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
86050 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2d  mem.nCurrent[i]-
86060 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  -;.    assert( m
86070 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3e 3d  em.nCurrent[i]>=
86080 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  0 );.  }.}../*.*
86090 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c 6f 63  * Given an alloc
860a0 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68 65 20  ation, find the 
860b0 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f 72 20  MemBlockHdr for 
860c0 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  that allocation.
860d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
860e0 69 6e 65 20 63 68 65 63 6b 73 20 74 68 65 20 67  ine checks the g
860f0 75 61 72 64 73 20 61 74 20 65 69 74 68 65 72 20  uards at either 
86100 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  end of the alloc
86110 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69 66 20  ation and.** if 
86120 74 68 65 79 20 61 72 65 20 69 6e 63 6f 72 72 65  they are incorre
86130 63 74 20 69 74 20 61 73 73 65 72 74 73 2e 0a 2a  ct it asserts..*
86140 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20  /.static struct 
86150 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73 71 6c  MemBlockHdr *sql
86160 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48 65 61  ite3MemsysGetHea
86170 64 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c 6f 63  der(void *pAlloc
86180 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75 63 74  ation){.  struct
86190 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 3b   MemBlockHdr *p;
861a0 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a 20 20  .  int *pInt;.  
861b0 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74 20 6e  u8 *pU8;.  int n
861c0 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 20 3d 20  Reserve;..  p = 
861d0 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b  (struct MemBlock
861e0 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e  Hdr*)pAllocation
861f0 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73 65 72  ;.  p--;.  asser
86200 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61 72 64  t( p->iForeGuard
86210 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 41 52 44  ==(int)FOREGUARD
86220 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 3d   );.  nReserve =
86230 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69 7a 65   ROUND8(p->iSize
86240 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69 6e 74  );.  pInt = (int
86250 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20  *)pAllocation;. 
86260 20 70 55 38 20 3d 20 28 75 38 2a 29 70 41 6c 6c   pU8 = (u8*)pAll
86270 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73 65 72  ocation;.  asser
86280 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 65  t( pInt[nReserve
86290 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d 3d 28  /sizeof(int)]==(
862a0 69 6e 74 29 52 45 41 52 47 55 41 52 44 20 29 3b  int)REARGUARD );
862b0 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 65 63 6b  .  /* This check
862c0 73 20 61 6e 79 20 6f 66 20 74 68 65 20 22 65 78  s any of the "ex
862d0 74 72 61 22 20 62 79 74 65 73 20 61 6c 6c 6f 63  tra" bytes alloc
862e0 61 74 65 64 20 64 75 65 0a 20 20 2a 2a 20 74 6f  ated due.  ** to
862f0 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 74 6f 20   rounding up to 
86300 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61  an 8 byte bounda
86310 72 79 20 74 6f 20 65 6e 73 75 72 65 20 0a 20 20  ry to ensure .  
86320 2a 2a 20 74 68 65 79 20 68 61 76 65 6e 27 74 20  ** they haven't 
86330 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74 65 6e  been overwritten
86340 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  ..  */.  while( 
86350 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 70 2d 3e  nReserve-- > p->
86360 69 53 69 7a 65 20 29 20 61 73 73 65 72 74 28 20  iSize ) assert( 
86370 70 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d 3d 30  pU8[nReserve]==0
86380 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  x65 );.  return 
86390 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  p;.}../*.** Retu
863a0 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
863b0 20 62 79 74 65 73 20 63 75 72 72 65 6e 74 6c 79   bytes currently
863c0 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 61 64   allocated at ad
863d0 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 61 74  dress p..*/.stat
863e0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
863f0 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a  mSize(void *p){.
86400 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63    struct MemBloc
86410 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 69 66  kHdr *pHdr;.  if
86420 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75  ( !p ){.    retu
86430 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 64 72  rn 0;.  }.  pHdr
86440 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73   = sqlite3Memsys
86450 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a 20 20  GetHeader(p);.  
86460 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 53 69  return pHdr->iSi
86470 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  ze;.}../*.** Ini
86480 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f  tialize the memo
86490 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
864a0 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74  bsystem..*/.stat
864b0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
864c0 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55  mInit(void *NotU
864d0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
864e0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
864f0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 69  );.  assert( (si
86500 7a 65 6f 66 28 73 74 72 75 63 74 20 4d 65 6d 42  zeof(struct MemB
86510 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d 20 30  lockHdr)&7) == 0
86520 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74   );.  if( !sqlit
86530 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
86540 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 2f  Memstat ){.    /
86550 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74 61 74  * If memory stat
86560 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 74  us is enabled, t
86570 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63 2e 63  hen the malloc.c
86580 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20 61 6c   wrapper will al
86590 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68 6f 6c  ready.    ** hol
865a0 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d  d the STATIC_MEM
865b0 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68 65 20   mutex when the 
865c0 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20 61 72  routines here ar
865d0 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a 20 20  e invoked. */.  
865e0 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20 73 71    mem.mutex = sq
865f0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
86600 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
86610 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20  TIC_MEM);.  }.  
86620 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
86630 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69  ;.}../*.** Deini
86640 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f  tialize the memo
86650 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
86660 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74  bsystem..*/.stat
86670 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  ic void sqlite3M
86680 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 20  emShutdown(void 
86690 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55  *NotUsed){.  UNU
866a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
866b0 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e 6d 75  tUsed);.  mem.mu
866c0 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  tex = 0;.}../*.*
866d0 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71  * Round up a req
866e0 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65  uest size to the
866f0 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f   next valid allo
86700 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a  cation size..*/.
86710 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
86720 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69 6e 74  e3MemRoundup(int
86730 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20 52 4f   n){.  return RO
86740 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  UND8(n);.}../*.*
86750 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65  * Allocate nByte
86760 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79   bytes of memory
86770 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
86780 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c   *sqlite3MemMall
86790 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  oc(int nByte){. 
867a0 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
867b0 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69  Hdr *pHdr;.  voi
867c0 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61 72 20  d **pBt;.  char 
867d0 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b  *z;.  int *pInt;
867e0 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a  .  void *p = 0;.
867f0 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65 3b    int totalSize;
86800 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76 65 3b  .  int nReserve;
86810 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
86820 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74 65 78  _enter(mem.mutex
86830 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  );.  assert( mem
86840 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29 3b 0a  .disallow==0 );.
86850 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52 4f 55    nReserve = ROU
86860 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20 74 6f  ND8(nByte);.  to
86870 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73 65 72  talSize = nReser
86880 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64  ve + sizeof(*pHd
86890 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29  r) + sizeof(int)
868a0 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   +.             
868b0 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65    mem.nBacktrace
868c0 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b  *sizeof(void*) +
868d0 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 70   mem.nTitle;.  p
868e0 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61 6c 53   = malloc(totalS
868f0 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  ize);.  if( p ){
86900 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20 20 20  .    z = p;.    
86910 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 26 7a  pBt = (void**)&z
86920 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a 20 20  [mem.nTitle];.  
86930 20 20 70 48 64 72 20 3d 20 28 73 74 72 75 63 74    pHdr = (struct
86940 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 26 70   MemBlockHdr*)&p
86950 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63  Bt[mem.nBacktrac
86960 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e  e];.    pHdr->pN
86970 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 48 64  ext = 0;.    pHd
86980 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d 2e 70  r->pPrev = mem.p
86990 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 20 6d 65  Last;.    if( me
869a0 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20 20 20  m.pLast ){.     
869b0 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e 65 78   mem.pLast->pNex
869c0 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 7d 65  t = pHdr;.    }e
869d0 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 70  lse{.      mem.p
869e0 46 69 72 73 74 20 3d 20 70 48 64 72 3b 0a 20 20  First = pHdr;.  
869f0 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73    }.    mem.pLas
86a00 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20 70 48  t = pHdr;.    pH
86a10 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64 20 3d  dr->iForeGuard =
86a20 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 20 20 20   FOREGUARD;.    
86a30 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
86a40 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42 61 63  Slots = mem.nBac
86a50 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 48 64 72  ktrace;.    pHdr
86a60 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d 2e 6e  ->nTitle = mem.n
86a70 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 28 20 6d  Title;.    if( m
86a80 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b  em.nBacktrace ){
86a90 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 61 41 64  .      void *aAd
86aa0 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20 70 48  dr[40];.      pH
86ab0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d  dr->nBacktrace =
86ac0 20 62 61 63 6b 74 72 61 63 65 28 61 41 64 64 72   backtrace(aAddr
86ad0 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65  , mem.nBacktrace
86ae0 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d 65 6d  +1)-1;.      mem
86af0 63 70 79 28 70 42 74 2c 20 26 61 41 64 64 72 5b  cpy(pBt, &aAddr[
86b00 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74  1], pHdr->nBackt
86b10 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64  race*sizeof(void
86b20 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  *));.      asser
86b30 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20 20 20  t(pBt[0]);.     
86b40 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b 74 72   if( mem.xBacktr
86b50 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  ace ){.        m
86b60 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 6e 42  em.xBacktrace(nB
86b70 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b  yte, pHdr->nBack
86b80 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64 72 5b  trace-1, &aAddr[
86b90 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  1]);.      }.   
86ba0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 48   }else{.      pH
86bb0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d  dr->nBacktrace =
86bc0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   0;.    }.    if
86bd0 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b 0a  ( mem.nTitle ){.
86be0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20        memcpy(z, 
86bf0 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d 2e  mem.zTitle, mem.
86c00 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d 0a 20  nTitle);.    }. 
86c10 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 3d     pHdr->iSize =
86c20 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 6a 75   nByte;.    adju
86c30 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c 20 2b  stStats(nByte, +
86c40 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d 20 28  1);.    pInt = (
86c50 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b 0a 20  int*)&pHdr[1];. 
86c60 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 65     pInt[nReserve
86c70 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 3d 20  /sizeof(int)] = 
86c80 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20 20 6d  REARGUARD;.    m
86c90 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 36 35  emset(pInt, 0x65
86ca0 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 20  , nReserve);.   
86cb0 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 6e 74   p = (void*)pInt
86cc0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
86cd0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e  mutex_leave(mem.
86ce0 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
86cf0 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72   p; .}../*.** Fr
86d00 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74  ee memory..*/.st
86d10 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
86d20 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70  3MemFree(void *p
86d30 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 63 74  Prior){.  struct
86d40 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48   MemBlockHdr *pH
86d50 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74  dr;.  void **pBt
86d60 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 61  ;.  char *z;.  a
86d70 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c  ssert( sqlite3Gl
86d80 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73  obalConfig.bMems
86d90 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 65 78  tat || mem.mutex
86da0 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 3d 20  !=0 );.  pHdr = 
86db0 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74  sqlite3MemsysGet
86dc0 48 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a  Header(pPrior);.
86dd0 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29    pBt = (void**)
86de0 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d 20 70  pHdr;.  pBt -= p
86df0 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53  Hdr->nBacktraceS
86e00 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f  lots;.  sqlite3_
86e10 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e  mutex_enter(mem.
86e20 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 48  mutex);.  if( pH
86e30 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20  dr->pPrev ){.   
86e40 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70   assert( pHdr->p
86e50 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 48 64  Prev->pNext==pHd
86e60 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70  r );.    pHdr->p
86e70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 48  Prev->pNext = pH
86e80 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c  dr->pNext;.  }el
86e90 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
86ea0 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 64 72  mem.pFirst==pHdr
86eb0 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 69 72   );.    mem.pFir
86ec0 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 74  st = pHdr->pNext
86ed0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 64 72  ;.  }.  if( pHdr
86ee0 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 61  ->pNext ){.    a
86ef0 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 4e 65  ssert( pHdr->pNe
86f00 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 72 20  xt->pPrev==pHdr 
86f10 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e 65  );.    pHdr->pNe
86f20 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 64 72  xt->pPrev = pHdr
86f30 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c 73 65  ->pPrev;.  }else
86f40 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65  {.    assert( me
86f50 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 29 3b  m.pLast==pHdr );
86f60 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d  .    mem.pLast =
86f70 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20   pHdr->pPrev;.  
86f80 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70  }.  z = (char*)p
86f90 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 72 2d  Bt;.  z -= pHdr-
86fa0 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a 75 73  >nTitle;.  adjus
86fb0 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69 53 69  tStats(pHdr->iSi
86fc0 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d 73 65  ze, -1);.  memse
86fd0 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a 65 6f  t(z, 0x2b, sizeo
86fe0 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d 3e 6e  f(void*)*pHdr->n
86ff0 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 2b  BacktraceSlots +
87000 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b   sizeof(*pHdr) +
87010 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
87020 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 2b     pHdr->iSize +
87030 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 20 70   sizeof(int) + p
87040 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a 20 20  Hdr->nTitle);.  
87050 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c 69 74  free(z);.  sqlit
87060 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
87070 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d 0a 0a  em.mutex);  .}..
87080 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
87090 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73   size of an exis
870a0 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ting memory allo
870b0 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f  cation..**.** Fo
870c0 72 20 74 68 69 73 20 64 65 62 75 67 67 69 6e 67  r this debugging
870d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c   implementation,
870e0 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d 61 6b   we *always* mak
870f0 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a  e a copy of the.
87100 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e  ** allocation in
87110 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 20 69  to a new place i
87120 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 74 68  n memory.  In th
87130 69 73 20 77 61 79 2c 20 69 66 20 74 68 65 20 0a  is way, if the .
87140 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65 6c 20  ** higher level 
87150 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20 70 6f  code is using po
87160 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 6c 64  inter to the old
87170 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20   allocation, it 
87180 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f 72 65  is .** much more
87190 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 61 6b   likely to break
871a0 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75 63 68   and we are much
871b0 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 6f 20   more liking to 
871c0 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 72 6f  find.** the erro
871d0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
871e0 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61  d *sqlite3MemRea
871f0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f  lloc(void *pPrio
87200 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  r, int nByte){. 
87210 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b   struct MemBlock
87220 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a 20 20  Hdr *pOldHdr;.  
87230 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 61 73  void *pNew;.  as
87240 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c  sert( mem.disall
87250 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c 64 48  ow==0 );.  pOldH
87260 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73  dr = sqlite3Mems
87270 79 73 47 65 74 48 65 61 64 65 72 28 70 50 72 69  ysGetHeader(pPri
87280 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  or);.  pNew = sq
87290 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 6e  lite3MemMalloc(n
872a0 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70 4e 65  Byte);.  if( pNe
872b0 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  w ){.    memcpy(
872c0 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 6e 42  pNew, pPrior, nB
872d0 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 53 69  yte<pOldHdr->iSi
872e0 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 4f 6c  ze ? nByte : pOl
872f0 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20  dHdr->iSize);.  
87300 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f 6c 64    if( nByte>pOld
87310 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20  Hdr->iSize ){.  
87320 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68      memset(&((ch
87330 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 48 64  ar*)pNew)[pOldHd
87340 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 62 2c  r->iSize], 0x2b,
87350 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 64 72   nByte - pOldHdr
87360 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a  ->iSize);.    }.
87370 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72      sqlite3MemFr
87380 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a  ee(pPrior);.  }.
87390 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d    return pNew;.}
873a0 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65  ../*.** Populate
873b0 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d   the low-level m
873c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
873d0 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65   function pointe
873e0 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33  rs in.** sqlite3
873f0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77  GlobalConfig.m w
87400 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  ith pointers to 
87410 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20  the routines in 
87420 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51  this file..*/.SQ
87430 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
87440 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44  d sqlite3MemSetD
87450 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20  efault(void){.  
87460 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
87470 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
87480 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20   defaultMethods 
87490 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33  = {.     sqlite3
874a0 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20  MemMalloc,.     
874b0 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a  sqlite3MemFree,.
874c0 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52       sqlite3MemR
874d0 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c  ealloc,.     sql
874e0 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20  ite3MemSize,.   
874f0 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e    sqlite3MemRoun
87500 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  dup,.     sqlite
87510 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73  3MemInit,.     s
87520 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77  qlite3MemShutdow
87530 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20  n,.     0.  };. 
87540 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
87550 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41  SQLITE_CONFIG_MA
87560 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65  LLOC, &defaultMe
87570 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  thods);.}../*.**
87580 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20   Set the number 
87590 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c 65 76  of backtrace lev
875a0 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 61 63  els kept for eac
875b0 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a  h allocation..**
875c0 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f   A value of zero
875d0 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63 6b 74   turns off backt
875e0 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e 75 6d  racing.  The num
875f0 62 65 72 20 69 73 20 61 6c 77 61 79 73 20 72 6f  ber is always ro
87600 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f 20 61  unded.** up to a
87610 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 2e 0a   multiple of 2..
87620 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
87630 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  E void sqlite3Me
87640 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 28  mdebugBacktrace(
87650 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 69 66  int depth){.  if
87660 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64 65 70  ( depth<0 ){ dep
87670 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 28 20  th = 0; }.  if( 
87680 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65 70 74  depth>20 ){ dept
87690 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 70 74  h = 20; }.  dept
876a0 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26 30 78  h = (depth+1)&0x
876b0 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b 74  fe;.  mem.nBackt
876c0 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a 7d 0a  race = depth;.}.
876d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
876e0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64  void sqlite3Memd
876f0 65 62 75 67 42 61 63 6b 74 72 61 63 65 43 61 6c  ebugBacktraceCal
87700 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42 61  lback(void (*xBa
87710 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e  cktrace)(int, in
87720 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a 20 20  t, void **)){.  
87730 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 3d  mem.xBacktrace =
87740 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a   xBacktrace;.}..
87750 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69  /*.** Set the ti
87760 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 20 73  tle string for s
87770 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61  ubsequent alloca
87780 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tions..*/.SQLITE
87790 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
877a0 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74  lite3MemdebugSet
877b0 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 61 72  title(const char
877c0 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75 6e 73   *zTitle){.  uns
877d0 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20 73 71  igned int n = sq
877e0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54  lite3Strlen30(zT
877f0 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73 71 6c  itle) + 1;.  sql
87800 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
87810 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69  (mem.mutex);.  i
87820 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d  f( n>=sizeof(mem
87830 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d 20 73  .zTitle) ) n = s
87840 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65  izeof(mem.zTitle
87850 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65  )-1;.  memcpy(me
87860 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65  m.zTitle, zTitle
87870 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74  , n);.  mem.zTit
87880 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d  le[n] = 0;.  mem
87890 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e 44 38  .nTitle = ROUND8
878a0 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  (n);.  sqlite3_m
878b0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d  utex_leave(mem.m
878c0 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45  utex);.}..SQLITE
878d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
878e0 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53 79 6e  lite3MemdebugSyn
878f0 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65  c(){.  struct Me
87900 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b  mBlockHdr *pHdr;
87910 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65 6d 2e  .  for(pHdr=mem.
87920 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20 70 48  pFirst; pHdr; pH
87930 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74 29 7b  dr=pHdr->pNext){
87940 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42 74 20  .    void **pBt 
87950 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a  = (void**)pHdr;.
87960 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d      pBt -= pHdr-
87970 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
87980 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63 6b 74  ;.    mem.xBackt
87990 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69 7a 65  race(pHdr->iSize
879a0 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61  , pHdr->nBacktra
879b0 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29 3b 0a  ce-1, &pBt[1]);.
879c0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65    }.}../*.** Ope
879d0 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64 69 63  n the file indic
879e0 61 74 65 64 20 61 6e 64 20 77 72 69 74 65 20 61  ated and write a
879f0 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e 66 72   log of all unfr
87a00 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61  eed memory .** a
87a10 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74 6f 20  llocations into 
87a20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c  that log..*/.SQL
87a30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
87a40 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67   sqlite3Memdebug
87a50 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20  Dump(const char 
87a60 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 46  *zFilename){.  F
87a70 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74 72 75  ILE *out;.  stru
87a80 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
87a90 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70  pHdr;.  void **p
87aa0 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6f  Bt;.  int i;.  o
87ab0 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65  ut = fopen(zFile
87ac0 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 69 66  name, "w");.  if
87ad0 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ( out==0 ){.    
87ae0 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
87af0 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75  "** Unable to ou
87b00 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75  tput memory debu
87b10 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73  g output log: %s
87b20 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   **\n",.        
87b30 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c              zFil
87b40 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75  ename);.    retu
87b50 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 48  rn;.  }.  for(pH
87b60 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b 20 70  dr=mem.pFirst; p
87b70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72 2d 3e  Hdr; pHdr=pHdr->
87b80 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68 61 72  pNext){.    char
87b90 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70 48 64   *z = (char*)pHd
87ba0 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48 64 72  r;.    z -= pHdr
87bb0 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74  ->nBacktraceSlot
87bc0 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 20  s*sizeof(void*) 
87bd0 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a  + pHdr->nTitle;.
87be0 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
87bf0 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79 74 65   "**** %lld byte
87c00 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25 73 20  s at %p from %s 
87c10 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20 20 20  ****\n", .      
87c20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a        pHdr->iSiz
87c30 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70 48 64  e, &pHdr[1], pHd
87c40 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20 3a 20  r->nTitle ? z : 
87c50 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66 28 20  "???");.    if( 
87c60 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
87c70 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75 73 68   ){.      fflush
87c80 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70 42 74  (out);.      pBt
87c90 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72 3b   = (void**)pHdr;
87ca0 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20 70 48  .      pBt -= pH
87cb0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c  dr->nBacktraceSl
87cc0 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63 6b 74  ots;.      backt
87cd0 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28  race_symbols_fd(
87ce0 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b  pBt, pHdr->nBack
87cf0 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28 6f 75  trace, fileno(ou
87d00 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72 69 6e  t));.      fprin
87d10 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20  tf(out, "\n");. 
87d20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72 69 6e     }.  }.  fprin
87d30 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54 53 3a  tf(out, "COUNTS:
87d40 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  \n");.  for(i=0;
87d50 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69 2b 2b   i<NCSIZE-1; i++
87d60 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e  ){.    if( mem.n
87d70 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20 20 20  Alloc[i] ){.    
87d80 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
87d90 20 20 20 25 35 64 3a 20 25 31 30 64 20 25 31 30     %5d: %10d %10
87da0 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20 20 20  d %10d\n", .    
87db0 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d 65 6d          i*8, mem
87dc0 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e  .nAlloc[i], mem.
87dd0 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d  nCurrent[i], mem
87de0 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29 3b 0a  .mxCurrent[i]);.
87df0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
87e00 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a  mem.nAlloc[NCSIZ
87e10 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70 72 69  E-1] ){.    fpri
87e20 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25 35 64  ntf(out, "   %5d
87e30 3a 20 25 31 30 64 20 25 31 30 64 20 25 31 30 64  : %10d %10d %10d
87e40 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
87e50 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20 6d 65    NCSIZE*8-8, me
87e60 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d  m.nAlloc[NCSIZE-
87e70 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  1],.            
87e80 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 4e 43   mem.nCurrent[NC
87e90 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43  SIZE-1], mem.mxC
87ea0 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d  urrent[NCSIZE-1]
87eb0 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73 65 28  );.  }.  fclose(
87ec0 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  out);.}../*.** R
87ed0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
87ee0 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69 74 65   of times sqlite
87ef0 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68 61 73  3MemMalloc() has
87f00 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f   been called..*/
87f10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
87f20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  int sqlite3Memde
87f30 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74 28 29  bugMallocCount()
87f40 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
87f50 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 66   nTotal = 0;.  f
87f60 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49 5a 45  or(i=0; i<NCSIZE
87f70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54 6f 74  ; i++){.    nTot
87f80 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63  al += mem.nAlloc
87f90 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  [i];.  }.  retur
87fa0 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65  n nTotal;.}...#e
87fb0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  ndif /* SQLITE_M
87fc0 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a  EMDEBUG */../***
87fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
87fe0 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a  of mem2.c ******
87ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
88020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
88030 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a  n file mem3.c **
88040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
88060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
88070 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31  * 2007 October 1
88080 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  4.**.** The auth
88090 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
880a0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
880b0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
880c0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
880d0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
880e0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
880f0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
88100 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
88110 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
88120 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
88130 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
88140 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
88150 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
88160 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
88170 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
88180 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
88190 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
881a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
881b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
881c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
881d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
881e0 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
881f0 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e  ntains the C fun
88200 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c  ctions that impl
88210 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a  ement a memory.*
88220 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  * allocation sub
88230 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62  system for use b
88240 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a  y SQLite. .**.**
88250 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
88260 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
88270 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d  cation subsystem
88280 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73   omits all.** us
88290 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54  e of malloc(). T
882a0 68 65 20 53 51 4c 69 74 65 20 75 73 65 72 20 73  he SQLite user s
882b0 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20  upplies a block 
882c0 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66  of memory.** bef
882d0 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69  ore calling sqli
882e0 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
882f0 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f   from which allo
88300 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d  cations.** are m
88310 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64  ade and returned
88320 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28   by the xMalloc(
88330 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29  ) and xRealloc()
88340 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74   .** implementat
88350 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74  ions. Once sqlit
88360 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
88370 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c  has been called,
88380 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f  .** the amount o
88390 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62  f memory availab
883a0 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20  le to SQLite is 
883b0 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74  fixed and cannot
883c0 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a  .** be changed..
883d0 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  **.** This versi
883e0 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
883f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
88400 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65  ystem is include
88410 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c  d.** in the buil
88420 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45  d only if SQLITE
88430 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20  _ENABLE_MEMSYS3 
88440 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 0a  is defined..*/..
88450 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  /*.** This versi
88460 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
88470 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f 6e   allocator is on
88480 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68  ly built into th
88490 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51 4c  e library.** SQL
884a0 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
884b0 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20 44  S3 is defined. D
884c0 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79 6d  efining this sym
884d0 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  bol does not.** 
884e0 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c 69  mean that the li
884f0 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20 61  brary will use a
88500 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79 20   memory-pool by 
88510 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74 68  default, just th
88520 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61 69  at.** it is avai
88530 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70 6f  lable. The mempo
88540 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ol allocator is 
88550 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61 6c  activated by cal
88560 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ling.** sqlite3_
88570 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69 66  config()..*/.#if
88580 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
88590 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a 2a  E_MEMSYS3../*.**
885a0 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28 69   Maximum size (i
885b0 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f 66  n Mem3Blocks) of
885c0 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e 6b   a "small" chunk
885d0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f  ..*/.#define MX_
885e0 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a  SMALL 10.../*.**
885f0 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 6c   Number of freel
88600 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a 2a  ist hash slots.*
88610 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53 48  /.#define N_HASH
88620 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 65    61../*.** A me
88630 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
88640 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20 22  (also called a "
88650 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74 73  chunk") consists
88660 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20 6d   of two or .** m
88670 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72 65  ore blocks where
88680 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20 38   each block is 8
88690 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69 72   bytes.  The fir
886a0 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20 0a  st 8 bytes are .
886b0 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61 74  ** a header that
886c0 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65 64   is not returned
886d0 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a 2a   to the user..**
886e0 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20 74  .** A chunk is t
886f0 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63 6b  wo or more block
88700 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65 72  s that is either
88710 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72 0a   checked out or.
88720 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66 69  ** free.  The fi
88730 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66 6f  rst block has fo
88740 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e 68  rmat u.hdr.  u.h
88750 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20 74  dr.size4x is 4 t
88760 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65  imes the.** size
88770 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   of the allocati
88780 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20  on in blocks if 
88790 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  the allocation i
887a0 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20 75  s free..** The u
887b0 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62 69  .hdr.size4x&1 bi
887c0 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65  t is true if the
887d0 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65   chunk is checke
887e0 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61 6c  d out and.** fal
887f0 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b 20  se if the chunk 
88800 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69  is on the freeli
88810 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 73  st.  The u.hdr.s
88820 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20 69  ize4x&2 bit.** i
88830 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70 72  s true if the pr
88840 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73 20  evious chunk is 
88850 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20  checked out and 
88860 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a 20  false if the.** 
88870 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69  previous chunk i
88880 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e 68  s free.  The u.h
88890 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65 6c  dr.prevSize fiel
888a0 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66  d is the size of
888b0 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73  .** the previous
888c0 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b 73   chunk in blocks
888d0 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75 73   if the previous
888e0 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68 65   chunk is on the
888f0 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49 66  .** freelist. If
88900 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68   the previous ch
88910 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f  unk is checked o
88920 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68 64  ut, then.** u.hd
88930 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20 62  r.prevSize can b
88940 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61  e part of the da
88950 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75 6e  ta for that chun
88960 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a 20  k and should.** 
88970 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20 77  not be read or w
88980 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65  ritten..**.** We
88990 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79 20   often identify 
889a0 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20 69  a chunk by its i
889b0 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f  ndex in mem3.aPo
889c0 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20 74  ol[].  When.** t
889d0 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68 65  his is done, the
889e0 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65 66   chunk index ref
889f0 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e  ers to the secon
88a00 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74 68  d block of.** th
88a10 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68 69  e chunk.  In thi
88a20 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73 74  s way, the first
88a30 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69 6e   chunk has an in
88a40 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20 63  dex of 1..** A c
88a50 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30 20  hunk index of 0 
88a60 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20 63  means "no such c
88a70 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68 65  hunk" and is the
88a80 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20 6f   equivalent.** o
88a90 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  f a NULL pointer
88aa0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ..**.** The seco
88ab0 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65 65  nd block of free
88ac0 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74 68   chunks is of th
88ad0 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20 20  e form u.list.  
88ae0 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c 64  The.** two field
88af0 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65 2d  s form a double-
88b00 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 63  linked list of c
88b10 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65 64  hunks of related
88b20 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e 74   sizes..** Point
88b30 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64 20  ers to the head 
88b40 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65 20  of the list are 
88b50 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e 61  stored in mem3.a
88b60 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f 72  iSmall[] .** for
88b70 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73 20   smaller chunks 
88b80 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b  and mem3.aiHash[
88b90 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68 75  ] for larger chu
88ba0 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  nks..**.** The s
88bb0 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 61  econd block of a
88bc0 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20 64   chunk is user d
88bd0 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e 6b  ata if the chunk
88be0 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a 20   is checked .** 
88bf0 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e 6b  out.  If a chunk
88c00 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c   is checked out,
88c10 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 6d   the user data m
88c20 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a 2a  ay extend into.*
88c30 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65 76  * the u.hdr.prev
88c40 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74 68  Size value of th
88c50 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75 6e  e following chun
88c60 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  k..*/.typedef st
88c70 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 4d  ruct Mem3Block M
88c80 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63 74  em3Block;.struct
88c90 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20 75   Mem3Block {.  u
88ca0 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75 63  nion {.    struc
88cb0 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70 72  t {.      u32 pr
88cc0 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69 7a  evSize;   /* Siz
88cd0 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63 68  e of previous ch
88ce0 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b  unk in Mem3Block
88cf0 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20   elements */.   
88d00 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20 20     u32 size4x;  
88d10 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69 7a     /* 4x the siz
88d20 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68 75  e of current chu
88d30 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20  nk in Mem3Block 
88d40 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20  elements */.    
88d50 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75 63  } hdr;.    struc
88d60 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e 65  t {.      u32 ne
88d70 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  xt;       /* Ind
88d80 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ex in mem3.aPool
88d90 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65 20  [] of next free 
88da0 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20 75  chunk */.      u
88db0 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20 2f  32 prev;       /
88dc0 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e  * Index in mem3.
88dd0 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76 69  aPool[] of previ
88de0 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20 2a  ous free chunk *
88df0 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20 20  /.    } list;.  
88e00 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  } u;.};../*.** A
88e10 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63  ll of the static
88e20 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20   variables used 
88e30 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61  by this module a
88e40 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20  re collected.** 
88e50 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74  into a single st
88e60 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d  ructure named "m
88e70 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20 74  em3".  This is t
88e80 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74  o keep the.** st
88e90 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f  atic variables o
88ea0 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20  rganized and to 
88eb0 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65  reduce namespace
88ec0 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68   pollution.** wh
88ed0 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69  en this module i
88ee0 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20  s combined with 
88ef0 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61  other in the ama
88f00 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  lgamation..*/.st
88f10 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
88f20 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62 61  struct Mem3Globa
88f30 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65  l {.  /*.  ** Me
88f40 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66  mory available f
88f50 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 6e  or allocation. n
88f60 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a 65  Pool is the size
88f70 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20 20   of the array.  
88f80 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b  ** (in Mem3Block
88f90 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  s) pointed to by
88fa0 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a 20   aPool less 2.. 
88fb0 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c 3b   */.  u32 nPool;
88fc0 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61 50  .  Mem3Block *aP
88fd0 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ool;..  /*.  ** 
88fe0 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 65  True if we are e
88ff0 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75 74  valuating an out
89000 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c 62  -of-memory callb
89010 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ack..  */.  int 
89020 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20 20  alarmBusy;.  .  
89030 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74 6f  /*.  ** Mutex to
89040 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73 20   control access 
89050 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  to the memory al
89060 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
89070 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  em..  */.  sqlit
89080 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b  e3_mutex *mutex;
89090 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  .  .  /*.  ** Th
890a0 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74  e minimum amount
890b0 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20 74   of free space t
890c0 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65 6e  hat we have seen
890d0 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e 4d  ..  */.  u32 mnM
890e0 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  aster;..  /*.  *
890f0 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68 65  * iMaster is the
89100 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61   index of the ma
89110 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f 73  ster chunk.  Mos
89120 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e  t new allocation
89130 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66 66  s.  ** occur off
89140 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e 20   of this chunk. 
89150 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65   szMaster is the
89160 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42 6c   size (in Mem3Bl
89170 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74 68  ocks).  ** of th
89180 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65 72  e current master
89190 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30 20  .  iMaster is 0 
891a0 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20  if there is not 
891b0 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20 20  master chunk..  
891c0 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63 68  ** The master ch
891d0 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65 69  unk is not in ei
891e0 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68 5b  ther the aiHash[
891f0 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e 0a  ] or aiSmall[]..
89200 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73 74    */.  u32 iMast
89210 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73 74  er;.  u32 szMast
89220 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41  er;..  /*.  ** A
89230 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f 66  rray of lists of
89240 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63 63   free blocks acc
89250 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62 6c  ording to the bl
89260 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20 66  ock size .  ** f
89270 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b  or smaller chunk
89280 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e 20  s, or a hash on 
89290 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20 66  the block size f
892a0 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 63  or larger.  ** c
892b0 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75 33  hunks..  */.  u3
892c0 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d 41  2 aiSmall[MX_SMA
892d0 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72 20  LL-1];   /* For 
892e0 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68 20  sizes 2 through 
892f0 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75 73  MX_SMALL, inclus
89300 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69 48  ive */.  u32 aiH
89310 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20 20  ash[N_HASH];    
89320 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73      /* For sizes
89330 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64 20   MX_SMALL+1 and 
89340 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d 33  larger */.} mem3
89350 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d 3b   = { 97535575 };
89360 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20 47  ..#define mem3 G
89370 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d  LOBAL(struct Mem
89380 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a 0a  3Global, mem3)..
89390 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65  /*.** Unlink the
893a0 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61   chunk at mem3.a
893b0 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73  Pool[i] from lis
893c0 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c  t it is currentl
893d0 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f 74  y.** on.  *pRoot
893e0 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68 61   is the list tha
893f0 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72 20  t i is a member 
89400 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  of..*/.static vo
89410 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b  id memsys3Unlink
89420 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c 20  FromList(u32 i, 
89430 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20 75  u32 *pRoot){.  u
89440 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e 61  32 next = mem3.a
89450 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e  Pool[i].u.list.n
89460 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76 20  ext;.  u32 prev 
89470 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e  = mem3.aPool[i].
89480 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20 61  u.list.prev;.  a
89490 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
894a0 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
894b0 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
894c0 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70  rev==0 ){.    *p
894d0 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d  Root = next;.  }
894e0 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e 61  else{.    mem3.a
894f0 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73  Pool[prev].u.lis
89500 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20  t.next = next;. 
89510 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29 7b   }.  if( next ){
89520 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
89530 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72 65  next].u.list.pre
89540 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20 20  v = prev;.  }.  
89550 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
89560 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a 20  list.next = 0;. 
89570 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75   mem3.aPool[i].u
89580 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b 0a  .list.prev = 0;.
89590 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20  }../*.** Unlink 
895a0 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e 64  the chunk at ind
895b0 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77 68  ex i from .** wh
895c0 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20 63  atever list is c
895d0 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62 65  urrently a membe
895e0 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  r of..*/.static 
895f0 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69  void memsys3Unli
89600 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32  nk(u32 i){.  u32
89610 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61   size, hash;.  a
89620 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
89630 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d  utex_held(mem3.m
89640 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
89650 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  t( (mem3.aPool[i
89660 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
89670 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61 73   & 1)==0 );.  as
89680 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20  sert( i>=1 );.  
89690 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  size = mem3.aPoo
896a0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
896b0 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28  e4x/4;.  assert(
896c0 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f   size==mem3.aPoo
896d0 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64  l[i+size-1].u.hd
896e0 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20  r.prevSize );.  
896f0 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20  assert( size>=2 
89700 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d  );.  if( size <=
89710 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20   MX_SMALL ){.   
89720 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72   memsys3UnlinkFr
89730 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e  omList(i, &mem3.
89740 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29  aiSmall[size-2])
89750 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68  ;.  }else{.    h
89760 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48  ash = size % N_H
89770 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33  ASH;.    memsys3
89780 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69  UnlinkFromList(i
89790 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68  , &mem3.aiHash[h
897a0 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ash]);.  }.}../*
897b0 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68 75  .** Link the chu
897c0 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c  nk at mem3.aPool
897d0 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20 6f  [i] so that is o
897e0 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74 65  n the list roote
897f0 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e 0a  d.** at *pRoot..
89800 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
89810 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69  emsys3LinkIntoLi
89820 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a 70  st(u32 i, u32 *p
89830 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74 28  Root){.  assert(
89840 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
89850 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
89860 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  );.  mem3.aPool[
89870 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20 3d  i].u.list.next =
89880 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33 2e   *pRoot;.  mem3.
89890 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
898a0 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28 20  prev = 0;.  if( 
898b0 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d 65  *pRoot ){.    me
898c0 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d  m3.aPool[*pRoot]
898d0 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 69  .u.list.prev = i
898e0 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20 3d  ;.  }.  *pRoot =
898f0 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e   i;.}../*.** Lin
89900 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69  k the chunk at i
89910 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74 68  ndex i into eith
89920 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  er the appropria
89930 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75 6e  te.** small chun
89940 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f 20  k list, or into 
89950 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20  the large chunk 
89960 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73  hash table..*/.s
89970 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
89980 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20  s3Link(u32 i){. 
89990 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68 3b   u32 size, hash;
899a0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
899b0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
899c0 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  m3.mutex) );.  a
899d0 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20  ssert( i>=1 );. 
899e0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61   assert( (mem3.a
899f0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
89a00 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20 29  size4x & 1)==0 )
89a10 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e  ;.  size = mem3.
89a20 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
89a30 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73  .size4x/4;.  ass
89a40 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e  ert( size==mem3.
89a50 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e  aPool[i+size-1].
89a60 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29  u.hdr.prevSize )
89a70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
89a80 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69 7a  >=2 );.  if( siz
89a90 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b  e <= MX_SMALL ){
89aa0 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b  .    memsys3Link
89ab0 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65 6d  IntoList(i, &mem
89ac0 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32  3.aiSmall[size-2
89ad0 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ]);.  }else{.   
89ae0 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e   hash = size % N
89af0 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79  _HASH;.    memsy
89b00 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 69  s3LinkIntoList(i
89b10 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68  , &mem3.aiHash[h
89b20 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ash]);.  }.}../*
89b30 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54 49  .** If the STATI
89b40 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20 6e  C_MEM mutex is n
89b50 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2c  ot already held,
89b60 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e 20   obtain it now. 
89b70 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69 6c  The mutex.** wil
89b80 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65 6c  l already be hel
89b90 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20 63  d (obtained by c
89ba0 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63 29  ode in malloc.c)
89bb0 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c   if.** sqlite3Gl
89bc0 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 53  obalConfig.bMemS
89bd0 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a  tat is true..*/.
89be0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
89bf0 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b 0a  ys3Enter(void){.
89c00 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
89c10 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
89c20 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d 75  at==0 && mem3.mu
89c30 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d 65  tex==0 ){.    me
89c40 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  m3.mutex = sqlit
89c50 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
89c60 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
89c70 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  _MEM);.  }.  sql
89c80 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
89c90 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a  (mem3.mutex);.}.
89ca0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
89cb0 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b 0a  ys3Leave(void){.
89cc0 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
89cd0 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78  leave(mem3.mutex
89ce0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  );.}../*.** Call
89cf0 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20 75  ed when we are u
89d00 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66 79  nable to satisfy
89d10 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f   an allocation o
89d20 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  f nBytes..*/.sta
89d30 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33  tic void memsys3
89d40 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74 20  OutOfMemory(int 
89d50 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21 6d  nByte){.  if( !m
89d60 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29 7b  em3.alarmBusy ){
89d70 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42  .    mem3.alarmB
89d80 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73 73  usy = 1;.    ass
89d90 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
89da0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
89db0 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  ex) );.    sqlit
89dc0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
89dd0 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em3.mutex);.    
89de0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
89df0 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a 20  memory(nByte);. 
89e00 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
89e10 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74 65  _enter(mem3.mute
89e20 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61  x);.    mem3.ala
89e30 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d 0a  rmBusy = 0;.  }.
89e40 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b 20  }.../*.** Chunk 
89e50 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75 6e  i is a free chun
89e60 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20  k that has been 
89e70 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75 73  unlinked.  Adjus
89e80 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20 70  t its .** size p
89e90 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63 68  arameters for ch
89ea0 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74 75  eck-out and retu
89eb0 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
89ec0 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f 72  the .** user por
89ed0 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75 6e  tion of the chun
89ee0 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  k..*/.static voi
89ef0 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f  d *memsys3Checko
89f00 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e 42  ut(u32 i, u32 nB
89f10 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b 0a  lock){.  u32 x;.
89f20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
89f30 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
89f40 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  3.mutex) );.  as
89f50 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20 20  sert( i>=1 );.  
89f60 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f  assert( mem3.aPo
89f70 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
89f80 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29  ze4x/4==nBlock )
89f90 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33  ;.  assert( mem3
89fa0 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d  .aPool[i+nBlock-
89fb0 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
89fc0 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 78  e==nBlock );.  x
89fd0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d   = mem3.aPool[i-
89fe0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b  1].u.hdr.size4x;
89ff0 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d  .  mem3.aPool[i-
8a000 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8a010 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20 7c  = nBlock*4 | 1 |
8a020 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e 61   (x&2);.  mem3.a
8a030 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d  Pool[i+nBlock-1]
8a040 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
8a050 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d 33  = nBlock;.  mem3
8a060 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d  .aPool[i+nBlock-
8a070 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8a080 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20 26  |= 2;.  return &
8a090 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d  mem3.aPool[i];.}
8a0a0 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61 20  ../*.** Carve a 
8a0b0 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68 65  piece off of the
8a0c0 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d 33   end of the mem3
8a0d0 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63 68  .iMaster free ch
8a0e0 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61  unk..** Return a
8a0f0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
8a100 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  new allocation. 
8a110 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73 74   Or, if the mast
8a120 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20 6e  er chunk.** is n
8a130 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 2c  ot large enough,
8a140 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74   return 0..*/.st
8a150 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79  atic void *memsy
8a160 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33 32  s3FromMaster(u32
8a170 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73 65   nBlock){.  asse
8a180 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
8a190 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
8a1a0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
8a1b0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e  mem3.szMaster>=n
8a1c0 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20 6e  Block );.  if( n
8a1d0 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d 61  Block>=mem3.szMa
8a1e0 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f 2a  ster-1 ){.    /*
8a1f0 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65 20   Use the entire 
8a200 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76 6f  master */.    vo
8a210 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33 43  id *p = memsys3C
8a220 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d 61  heckout(mem3.iMa
8a230 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61 73  ster, mem3.szMas
8a240 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 69  ter);.    mem3.i
8a250 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
8a260 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20  mem3.szMaster = 
8a270 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61  0;.    mem3.mnMa
8a280 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 65  ster = 0;.    re
8a290 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65 7b  turn p;.  }else{
8a2a0 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74 68  .    /* Split th
8a2b0 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e 20  e master block. 
8a2c0 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69 6c   Return the tail
8a2d0 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65 77  . */.    u32 new
8a2e0 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20 3d  i, x;.    newi =
8a2f0 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b 20   mem3.iMaster + 
8a300 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d 20  mem3.szMaster - 
8a310 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73 65  nBlock;.    asse
8a320 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33 2e  rt( newi > mem3.
8a330 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20 20  iMaster+1 );.   
8a340 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
8a350 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a  .iMaster+mem3.sz
8a360 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
8a370 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f 63  prevSize = nBloc
8a380 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  k;.    mem3.aPoo
8a390 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d  l[mem3.iMaster+m
8a3a0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e  em3.szMaster-1].
8a3b0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20  u.hdr.size4x |= 
8a3c0 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f  2;.    mem3.aPoo
8a3d0 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72 2e  l[newi-1].u.hdr.
8a3e0 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b 2a  size4x = nBlock*
8a3f0 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33 2e  4 + 1;.    mem3.
8a400 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c 6f  szMaster -= nBlo
8a410 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ck;.    mem3.aPo
8a420 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72  ol[newi-1].u.hdr
8a430 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33  .prevSize = mem3
8a440 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20 78  .szMaster;.    x
8a450 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65   = mem3.aPool[me
8a460 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  m3.iMaster-1].u.
8a470 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b 0a  hdr.size4x & 2;.
8a480 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d      mem3.aPool[m
8a490 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
8a4a0 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65  .hdr.size4x = me
8a4b0 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20  m3.szMaster*4 | 
8a4c0 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e  x;.    if( mem3.
8a4d0 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33 2e  szMaster < mem3.
8a4e0 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  mnMaster ){.    
8a4f0 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20    mem3.mnMaster 
8a500 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3b  = mem3.szMaster;
8a510 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
8a520 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e 61  n (void*)&mem3.a
8a530 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d 0a  Pool[newi];.  }.
8a540 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74 20  }../*.** *pRoot 
8a550 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20 61  is the head of a
8a560 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63 68   list of free ch
8a570 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d 65  unks of the same
8a580 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d 65   size.** or same
8a590 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e 20   size hash.  In 
8a5a0 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70 52  other words, *pR
8a5b0 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79 20  oot is an entry 
8a5c0 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65 6d  in either.** mem
8a5d0 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20 6d  3.aiSmall[] or m
8a5e0 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20 0a  em3.aiHash[].  .
8a5f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
8a600 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c 20  ne examines all 
8a610 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20 67  entries on the g
8a620 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74 72  iven list and tr
8a630 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65 73  ies.** to coales
8a640 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73 20  ce each entries 
8a650 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66 72  with adjacent fr
8a660 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a 0a  ee chunks.  .**.
8a670 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61 20  ** If it sees a 
8a680 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c 61  chunk that is la
8a690 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e 69  rger than mem3.i
8a6a0 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c 61  Master, it repla
8a6b0 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72 72  ces .** the curr
8a6c0 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ent mem3.iMaster
8a6d0 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c 61   with the new la
8a6e0 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e 20  rger chunk.  In 
8a6f0 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68 69  order for.** thi
8a700 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 72  s mem3.iMaster r
8a710 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77 6f  eplacement to wo
8a720 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20 63  rk, the master c
8a730 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a 20  hunk must be.** 
8a740 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65 20  linked into the 
8a750 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54 68  hash tables.  Th
8a760 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e 6f  at is not the no
8a770 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a 2a  rmal state of.**
8a780 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f 75   affairs, of cou
8a790 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  rse.  The callin
8a7a0 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 6c  g routine must l
8a7b0 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a 2a  ink the master.*
8a7c0 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20 69  * chunk before i
8a7d0 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75  nvoking this rou
8a7e0 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74 20  tine, then must 
8a7f0 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73 73  unlink the (poss
8a800 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64 29  ibly.** changed)
8a810 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f 6e   master chunk on
8a820 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ce this routine 
8a830 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a 2f  has finished..*/
8a840 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
8a850 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a 70  sys3Merge(u32 *p
8a860 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e 65  Root){.  u32 iNe
8a870 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c 20  xt, prev, size, 
8a880 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74 28  i, x;..  assert(
8a890 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
8a8a0 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
8a8b0 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f 6f  );.  for(i=*pRoo
8a8c0 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74 29  t; i>0; i=iNext)
8a8d0 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d 65  {.    iNext = me
8a8e0 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69  m3.aPool[i].u.li
8a8f0 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69 7a  st.next;.    siz
8a900 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  e = mem3.aPool[i
8a910 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8a920 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 73  ;.    assert( (s
8a930 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20 20  ize&1)==0 );.   
8a940 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d 30   if( (size&2)==0
8a950 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73   ){.      memsys
8a960 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28  3UnlinkFromList(
8a970 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20  i, pRoot);.     
8a980 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65 6d   assert( i > mem
8a990 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
8a9a0 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20  dr.prevSize );. 
8a9b0 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d 20       prev = i - 
8a9c0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
8a9d0 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a  u.hdr.prevSize;.
8a9e0 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d 3d        if( prev==
8a9f0 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20  iNext ){.       
8aa00 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50   iNext = mem3.aP
8aa10 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73 74  ool[prev].u.list
8aa20 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  .next;.      }. 
8aa30 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
8aa40 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20 20  nk(prev);.      
8aa50 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65 2f  size = i + size/
8aa60 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20 20  4 - prev;.      
8aa70 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70  x = mem3.aPool[p
8aa80 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  rev-1].u.hdr.siz
8aa90 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d  e4x & 2;.      m
8aaa0 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31  em3.aPool[prev-1
8aab0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d  ].u.hdr.size4x =
8aac0 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20 20   size*4 | x;.   
8aad0 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72     mem3.aPool[pr
8aae0 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72  ev+size-1].u.hdr
8aaf0 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65  .prevSize = size
8ab00 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 4c  ;.      memsys3L
8ab10 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20  ink(prev);.     
8ab20 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20 7d   i = prev;.    }
8ab30 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a 65  else{.      size
8ab40 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20   /= 4;.    }.   
8ab50 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e 73   if( size>mem3.s
8ab60 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20  zMaster ){.     
8ab70 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20   mem3.iMaster = 
8ab80 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a  i;.      mem3.sz
8ab90 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a 20  Master = size;. 
8aba0 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
8abb0 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63 6b  * Return a block
8abc0 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74   of memory of at
8abd0 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69 6e   least nBytes in
8abe0 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e   size..** Return
8abf0 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65 2e   NULL if unable.
8ac00 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
8ac10 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
8ac20 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20  t the necessary 
8ac30 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c  mutexes, if any,
8ac40 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20   are.** already 
8ac50 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c  held by the call
8ac60 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66  er. Hence "Unsaf
8ac70 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e"..*/.static vo
8ac80 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f  id *memsys3Mallo
8ac90 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74  cUnsafe(int nByt
8aca0 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 75  e){.  u32 i;.  u
8acb0 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33 32  32 nBlock;.  u32
8acc0 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73 65   toFree;..  asse
8acd0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
8ace0 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65  x_held(mem3.mute
8acf0 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
8ad00 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b  sizeof(Mem3Block
8ad10 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e 42  )==8 );.  if( nB
8ad20 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20 6e  yte<=12 ){.    n
8ad30 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65 6c  Block = 2;.  }el
8ad40 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20 3d  se{.    nBlock =
8ad50 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38 3b   (nByte + 11)/8;
8ad60 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e  .  }.  assert( n
8ad70 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20 2f  Block>=2 );..  /
8ad80 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20 4c  * STEP 1:.  ** L
8ad90 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72 79  ook for an entry
8ada0 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20   of the correct 
8adb0 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20 74  size in either t
8adc0 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63 68  he small.  ** ch
8add0 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e 20  unk table or in 
8ade0 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b 20  the large chunk 
8adf0 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69  hash table.  Thi
8ae00 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65 73  s is.  ** succes
8ae10 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68 65  sful most of the
8ae20 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20 74   time (about 9 t
8ae30 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29 2e  imes out of 10).
8ae40 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c 6f  .  */.  if( nBlo
8ae50 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29  ck <= MX_SMALL )
8ae60 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e 61  {.    i = mem3.a
8ae70 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d  iSmall[nBlock-2]
8ae80 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b  ;.    if( i>0 ){
8ae90 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e  .      memsys3Un
8aea0 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20  linkFromList(i, 
8aeb0 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e 42  &mem3.aiSmall[nB
8aec0 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20 20  lock-2]);.      
8aed0 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68  return memsys3Ch
8aee0 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b  eckout(i, nBlock
8aef0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
8af00 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20 3d  {.    int hash =
8af10 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53 48   nBlock % N_HASH
8af20 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d 33  ;.    for(i=mem3
8af30 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20 69  .aiHash[hash]; i
8af40 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f 6c  >0; i=mem3.aPool
8af50 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29  [i].u.list.next)
8af60 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33  {.      if( mem3
8af70 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
8af80 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f  r.size4x/4==nBlo
8af90 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  ck ){.        me
8afa0 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c  msys3UnlinkFromL
8afb0 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 48  ist(i, &mem3.aiH
8afc0 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20 20  ash[hash]);.    
8afd0 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79      return memsy
8afe0 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42  s3Checkout(i, nB
8aff0 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20  lock);.      }. 
8b000 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53     }.  }..  /* S
8b010 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79 20  TEP 2:.  ** Try 
8b020 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20 61  to satisfy the a
8b030 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61 72  llocation by car
8b040 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66 66  ving a piece off
8b050 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a   of the end.  **
8b060 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 63   of the master c
8b070 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65 70  hunk.  This step
8b080 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20 69   usually works i
8b090 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e 0a  f step 1 fails..
8b0a0 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e    */.  if( mem3.
8b0b0 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b  szMaster>=nBlock
8b0c0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d   ){.    return m
8b0d0 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72  emsys3FromMaster
8b0e0 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 0a  (nBlock);.  }...
8b0f0 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a 20    /* STEP 3:  . 
8b100 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   ** Loop through
8b110 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d 6f   the entire memo
8b120 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65 73  ry pool.  Coales
8b130 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65  ce adjacent free
8b140 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20 52  .  ** chunks.  R
8b150 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 73  ecompute the mas
8b160 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68 65  ter chunk as the
8b170 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63 68   largest free ch
8b180 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20 74  unk..  ** Then t
8b190 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74 69  ry again to sati
8b1a0 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  sfy the allocati
8b1b0 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61 20  on by carving a 
8b1c0 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20 6f  piece off.  ** o
8b1d0 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
8b1e0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20   master chunk.  
8b1f0 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65 6e  This step happen
8b200 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65  s very.  ** rare
8b210 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20 20  ly (we hope!).  
8b220 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65 3d  */.  for(toFree=
8b230 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72 65  nBlock*16; toFre
8b240 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31 36  e<(mem3.nPool*16
8b250 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29 7b  ); toFree *= 2){
8b260 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74 4f  .    memsys3OutO
8b270 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29 3b  fMemory(toFree);
8b280 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69 4d  .    if( mem3.iM
8b290 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d  aster ){.      m
8b2a0 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33 2e  emsys3Link(mem3.
8b2b0 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  iMaster);.      
8b2c0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20 30  mem3.iMaster = 0
8b2d0 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d  ;.      mem3.szM
8b2e0 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 7d  aster = 0;.    }
8b2f0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
8b300 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20  N_HASH; i++){.  
8b310 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67 65      memsys3Merge
8b320 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d  (&mem3.aiHash[i]
8b330 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  );.    }.    for
8b340 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c  (i=0; i<MX_SMALL
8b350 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  -1; i++){.      
8b360 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65  memsys3Merge(&me
8b370 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b 0a  m3.aiSmall[i]);.
8b380 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 65      }.    if( me
8b390 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20  m3.szMaster ){. 
8b3a0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
8b3b0 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29  nk(mem3.iMaster)
8b3c0 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 33  ;.      if( mem3
8b3d0 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63  .szMaster>=nBloc
8b3e0 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  k ){.        ret
8b3f0 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d 4d  urn memsys3FromM
8b400 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20  aster(nBlock);. 
8b410 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
8b420 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20 6f  ..  /* If none o
8b430 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72 6b  f the above work
8b440 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69 6c  ed, then we fail
8b450 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b  . */.  return 0;
8b460 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61  .}../*.** Free a
8b470 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d 65  n outstanding me
8b480 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  mory allocation.
8b490 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
8b4a0 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61  tion assumes tha
8b4b0 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79 20  t the necessary 
8b4c0 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79 2c  mutexes, if any,
8b4d0 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79 20   are.** already 
8b4e0 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c  held by the call
8b4f0 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61 66  er. Hence "Unsaf
8b500 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d 73  e"..*/.void mems
8b510 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76 6f  ys3FreeUnsafe(vo
8b520 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65 6d  id *pOld){.  Mem
8b530 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65 6d  3Block *p = (Mem
8b540 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20 20  3Block*)pOld;.  
8b550 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69 7a  int i;.  u32 siz
8b560 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28 20  e, x;.  assert( 
8b570 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
8b580 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29  ld(mem3.mutex) )
8b590 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d 65  ;.  assert( p>me
8b5a0 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26 6d  m3.aPool && p<&m
8b5b0 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e  em3.aPool[mem3.n
8b5c0 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20 70  Pool] );.  i = p
8b5d0 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a 20   - mem3.aPool;. 
8b5e0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e 61   assert( (mem3.a
8b5f0 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
8b600 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b 0a  size4x&1)==1 );.
8b610 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50    size = mem3.aP
8b620 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
8b630 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72  ize4x/4;.  asser
8b640 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33 2e  t( i+size<=mem3.
8b650 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65 6d  nPool+1 );.  mem
8b660 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
8b670 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31 3b  dr.size4x &= ~1;
8b680 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b  .  mem3.aPool[i+
8b690 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  size-1].u.hdr.pr
8b6a0 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20  evSize = size;. 
8b6b0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69   mem3.aPool[i+si
8b6c0 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  ze-1].u.hdr.size
8b6d0 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d 73  4x &= ~2;.  mems
8b6e0 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20 2f  ys3Link(i);..  /
8b6f0 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64 20  * Try to expand 
8b700 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e 67  the master using
8b710 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65 64   the newly freed
8b720 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28 20   chunk */.  if( 
8b730 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b 0a  mem3.iMaster ){.
8b740 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d 33      while( (mem3
8b750 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
8b760 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
8b770 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20 20  e4x&2)==0 ){.   
8b780 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61     size = mem3.a
8b790 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
8b7a0 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  r-1].u.hdr.prevS
8b7b0 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  ize;.      mem3.
8b7c0 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 65 3b  iMaster -= size;
8b7d0 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61  .      mem3.szMa
8b7e0 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20 20  ster += size;.  
8b7f0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
8b800 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b  k(mem3.iMaster);
8b810 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e  .      x = mem3.
8b820 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
8b830 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  er-1].u.hdr.size
8b840 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d 65  4x & 2;.      me
8b850 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
8b860 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73  aster-1].u.hdr.s
8b870 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d  ize4x = mem3.szM
8b880 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20 20  aster*4 | x;.   
8b890 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65     mem3.aPool[me
8b8a0 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e  m3.iMaster+mem3.
8b8b0 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  szMaster-1].u.hd
8b8c0 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d  r.prevSize = mem
8b8d0 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20  3.szMaster;.    
8b8e0 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e 61  }.    x = mem3.a
8b8f0 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
8b900 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
8b910 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 6c 65  x & 2;.    while
8b920 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65  ( (mem3.aPool[me
8b930 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e  m3.iMaster+mem3.
8b940 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64  szMaster-1].u.hd
8b950 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 20 29  r.size4x&1)==0 )
8b960 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55  {.      memsys3U
8b970 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74  nlink(mem3.iMast
8b980 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  er+mem3.szMaster
8b990 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a  );.      mem3.sz
8b9a0 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e 61  Master += mem3.a
8b9b0 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
8b9c0 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d  r+mem3.szMaster-
8b9d0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
8b9e0 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50  4;.      mem3.aP
8b9f0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
8ba00 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8ba10 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72   = mem3.szMaster
8ba20 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65  *4 | x;.      me
8ba30 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
8ba40 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73  aster+mem3.szMas
8ba50 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ter-1].u.hdr.pre
8ba60 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d  vSize = mem3.szM
8ba70 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 7d  aster;.    }.  }
8ba80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
8ba90 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20   the size of an 
8baa0 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f  outstanding allo
8bab0 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65 73  cation, in bytes
8bac0 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20 72  .  The.** size r
8bad0 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74 68  eturned omits th
8bae0 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72 20  e 8-byte header 
8baf0 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73 20  overhead.  This 
8bb00 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f  only.** works fo
8bb10 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61 72  r chunks that ar
8bb20 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63  e currently chec
8bb30 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74  ked out..*/.stat
8bb40 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 53 69  ic int memsys3Si
8bb50 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 4d  ze(void *p){.  M
8bb60 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63 6b  em3Block *pBlock
8bb70 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
8bb80 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f 63  eturn 0;.  pBloc
8bb90 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a 29  k = (Mem3Block*)
8bba0 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 42  p;.  assert( (pB
8bbb0 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73  lock[-1].u.hdr.s
8bbc0 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b 0a 20  ize4x&1)!=0 );. 
8bbd0 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b 5b   return (pBlock[
8bbe0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8bbf0 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a 2f  &~3)*2 - 4;.}../
8bc00 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20  *.** Round up a 
8bc10 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20  request size to 
8bc20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61  the next valid a
8bc30 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a  llocation size..
8bc40 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
8bc50 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 6e 74  msys3Roundup(int
8bc60 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31 32   n){.  if( n<=12
8bc70 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
8bc80 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  2;.  }else{.    
8bc90 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 26 7e  return ((n+11)&~
8bca0 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f  7) - 4;.  }.}../
8bcb0 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42  *.** Allocate nB
8bcc0 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a  ytes of memory..
8bcd0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
8bce0 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69 6e  memsys3Malloc(in
8bcf0 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71 6c  t nBytes){.  sql
8bd00 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20  ite3_int64 *p;. 
8bd10 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73 3e   assert( nBytes>
8bd20 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  0 );          /*
8bd30 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65 72   malloc.c filter
8bd40 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65 71  s out 0 byte req
8bd50 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73 79  uests */.  memsy
8bd60 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d  s3Enter();.  p =
8bd70 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e   memsys3MallocUn
8bd80 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20  safe(nBytes);.  
8bd90 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b 0a  memsys3Leave();.
8bda0 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
8bdb0 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  p; .}../*.** Fre
8bdc0 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f 69  e memory..*/.voi
8bdd0 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76 6f  d memsys3Free(vo
8bde0 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 61  id *pPrior){.  a
8bdf0 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 29 3b  ssert( pPrior );
8be00 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28  .  memsys3Enter(
8be10 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 65 65  );.  memsys3Free
8be20 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a  Unsafe(pPrior);.
8be30 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29    memsys3Leave()
8be40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67  ;.}../*.** Chang
8be50 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  e the size of an
8be60 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79   existing memory
8be70 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 76   allocation.*/.v
8be80 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 61 6c  oid *memsys3Real
8be90 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  loc(void *pPrior
8bea0 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20  , int nBytes){. 
8beb0 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f 69   int nOld;.  voi
8bec0 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72 69  d *p;.  if( pPri
8bed0 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  or==0 ){.    ret
8bee0 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  urn sqlite3_mall
8bef0 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a  oc(nBytes);.  }.
8bf00 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20    if( nBytes<=0 
8bf10 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
8bf20 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 20  ree(pPrior);.   
8bf30 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
8bf40 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33 53   nOld = memsys3S
8bf50 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 69  ize(pPrior);.  i
8bf60 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64 20  f( nBytes<=nOld 
8bf70 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64 2d  && nBytes>=nOld-
8bf80 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 75 72  128 ){.    retur
8bf90 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20  n pPrior;.  }.  
8bfa0 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a  memsys3Enter();.
8bfb0 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61 6c    p = memsys3Mal
8bfc0 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73  locUnsafe(nBytes
8bfd0 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  );.  if( p ){.  
8bfe0 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74 65    if( nOld<nByte
8bff0 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  s ){.      memcp
8c000 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f 6c  y(p, pPrior, nOl
8c010 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
8c020 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70       memcpy(p, p
8c030 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b 0a  Prior, nBytes);.
8c040 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79 73      }.    memsys
8c050 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72 69  3FreeUnsafe(pPri
8c060 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79  or);.  }.  memsy
8c070 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74  s3Leave();.  ret
8c080 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
8c090 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20  Initialize this 
8c0a0 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  module..*/.stati
8c0b0 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e 69  c int memsys3Ini
8c0c0 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
8c0d0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
8c0e0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
8c0f0 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f   if( !sqlite3Glo
8c100 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 20  balConfig.pHeap 
8c110 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
8c120 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
8c130 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70 6f  .  /* Store a po
8c140 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d  inter to the mem
8c150 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c 6f  ory block in glo
8c160 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d 65  bal structure me
8c170 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  m3. */.  assert(
8c180 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63   sizeof(Mem3Bloc
8c190 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33 2e  k)==8 );.  mem3.
8c1a0 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c 6f  aPool = (Mem3Blo
8c1b0 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62  ck *)sqlite3Glob
8c1c0 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a  alConfig.pHeap;.
8c1d0 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20 28    mem3.nPool = (
8c1e0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
8c1f0 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a 65  fig.nHeap / size
8c200 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20 2d  of(Mem3Block)) -
8c210 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61   2;..  /* Initia
8c220 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72 20  lize the master 
8c230 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d 33  block. */.  mem3
8c240 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33  .szMaster = mem3
8c250 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 6d  .nPool;.  mem3.m
8c260 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e 73  nMaster = mem3.s
8c270 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33 2e  zMaster;.  mem3.
8c280 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20 6d  iMaster = 1;.  m
8c290 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e 68  em3.aPool[0].u.h
8c2a0 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65 6d  dr.size4x = (mem
8c2b0 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20 2b  3.szMaster<<2) +
8c2c0 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c   2;.  mem3.aPool
8c2d0 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68  [mem3.nPool].u.h
8c2e0 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65  dr.prevSize = me
8c2f0 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33  m3.nPool;.  mem3
8c300 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f  .aPool[mem3.nPoo
8c310 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  l].u.hdr.size4x 
8c320 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  = 1;..  return S
8c330 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
8c340 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20  ** Deinitialize 
8c350 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  this module..*/.
8c360 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
8c370 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69 64  ys3Shutdown(void
8c380 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e   *NotUsed){.  UN
8c390 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
8c3a0 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 33 2e  otUsed);.  mem3.
8c3b0 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 72 65 74  mutex = 0;.  ret
8c3c0 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  urn;.}..../*.** 
8c3d0 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e  Open the file in
8c3e0 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74  dicated and writ
8c3f0 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75  e a log of all u
8c400 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a  nfreed memory .*
8c410 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e  * allocations in
8c420 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a  to that log..*/.
8c430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
8c440 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73 79  oid sqlite3Memsy
8c450 73 33 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61  s3Dump(const cha
8c460 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 23  r *zFilename){.#
8c470 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
8c480 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a  UG.  FILE *out;.
8c490 20 20 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75 33    u32 i, j;.  u3
8c4a0 32 20 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a 46  2 size;.  if( zF
8c4b0 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46  ilename==0 || zF
8c4c0 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b  ilename[0]==0 ){
8c4d0 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f 75  .    out = stdou
8c4e0 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
8c4f0 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69 6c  out = fopen(zFil
8c500 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20 20  ename, "w");.   
8c510 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20   if( out==0 ){. 
8c520 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
8c530 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20  err, "** Unable 
8c540 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79  to output memory
8c550 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c 6f   debug output lo
8c560 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20  g: %s **\n",.   
8c570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8c580 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20     zFilename);. 
8c590 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
8c5a0 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33   }.  }.  memsys3
8c5b0 45 6e 74 65 72 28 29 3b 0a 20 20 66 70 72 69 6e  Enter();.  fprin
8c5c0 74 66 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53 3a  tf(out, "CHUNKS:
8c5d0 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b  \n");.  for(i=1;
8c5e0 20 69 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b 20   i<=mem3.nPool; 
8c5f0 69 2b 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20 20  i+=size/4){.    
8c600 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f  size = mem3.aPoo
8c610 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
8c620 65 34 78 3b 0a 20 20 20 20 69 66 28 20 73 69 7a  e4x;.    if( siz
8c630 65 2f 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20 20  e/4<=1 ){.      
8c640 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70  fprintf(out, "%p
8c650 20 73 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c 20   size error\n", 
8c660 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b  &mem3.aPool[i]);
8c670 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30  .      assert( 0
8c680 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
8c690 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28  .    }.    if( (
8c6a0 73 69 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d 65  size&1)==0 && me
8c6b0 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f  m3.aPool[i+size/
8c6c0 34 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  4-1].u.hdr.prevS
8c6d0 69 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a 20  ize!=size/4 ){. 
8c6e0 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
8c6f0 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a 65 20  , "%p tail size 
8c700 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c 6e  does not match\n
8c710 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ", &mem3.aPool[i
8c720 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ]);.      assert
8c730 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ( 0 );.      bre
8c740 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ak;.    }.    if
8c750 28 20 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ( ((mem3.aPool[i
8c760 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64 72  +size/4-1].u.hdr
8c770 2e 73 69 7a 65 34 78 26 32 29 3e 3e 31 29 21 3d  .size4x&2)>>1)!=
8c780 28 73 69 7a 65 26 31 29 20 29 7b 0a 20 20 20 20  (size&1) ){.    
8c790 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8c7a0 25 70 20 74 61 69 6c 20 63 68 65 63 6b 6f 75 74  %p tail checkout
8c7b0 20 62 69 74 20 69 73 20 69 6e 63 6f 72 72 65 63   bit is incorrec
8c7c0 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f  t\n", &mem3.aPoo
8c7d0 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73  l[i]);.      ass
8c7e0 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20  ert( 0 );.      
8c7f0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
8c800 20 69 66 28 20 73 69 7a 65 26 31 20 29 7b 0a 20   if( size&1 ){. 
8c810 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
8c820 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73 20  , "%p %6d bytes 
8c830 63 68 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c 20  checked out\n", 
8c840 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20  &mem3.aPool[i], 
8c850 28 73 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a 20  (size/4)*8-8);. 
8c860 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
8c870 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70  fprintf(out, "%p
8c880 20 25 36 64 20 62 79 74 65 73 20 66 72 65 65 25   %6d bytes free%
8c890 73 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f  s\n", &mem3.aPoo
8c8a0 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a 38  l[i], (size/4)*8
8c8b0 2d 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  -8,.            
8c8c0 20 20 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69 4d        i==mem3.iM
8c8d0 61 73 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73 74  aster ? " **mast
8c8e0 65 72 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20 20  er**" : "");.   
8c8f0 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30   }.  }.  for(i=0
8c900 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20  ; i<MX_SMALL-1; 
8c910 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65  i++){.    if( me
8c920 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d 30  m3.aiSmall[i]==0
8c930 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
8c940 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 73   fprintf(out, "s
8c950 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29 3b  mall(%2d):", i);
8c960 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65 6d  .    for(j = mem
8c970 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a 3e  3.aiSmall[i]; j>
8c980 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  0; j=mem3.aPool[
8c990 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29 7b  j].u.list.next){
8c9a0 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f  .      fprintf(o
8c9b0 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20 26  ut, " %p(%d)", &
8c9c0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a 20  mem3.aPool[j],. 
8c9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d 65               (me
8c9e0 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75 2e  m3.aPool[j-1].u.
8c9f0 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38 2d  hdr.size4x/4)*8-
8ca00 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 70  8);.    }.    fp
8ca10 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29  rintf(out, "\n")
8ca20 3b 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  ; .  }.  for(i=0
8ca30 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29  ; i<N_HASH; i++)
8ca40 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 61  {.    if( mem3.a
8ca50 69 48 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63 6f  iHash[i]==0 ) co
8ca60 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69  ntinue;.    fpri
8ca70 6e 74 66 28 6f 75 74 2c 20 22 68 61 73 68 28 25  ntf(out, "hash(%
8ca80 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66  2d):", i);.    f
8ca90 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48 61  or(j = mem3.aiHa
8caa0 73 68 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65  sh[i]; j>0; j=me
8cab0 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69  m3.aPool[j].u.li
8cac0 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20  st.next){.      
8cad0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25  fprintf(out, " %
8cae0 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50  p(%d)", &mem3.aP
8caf0 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20  ool[j],.        
8cb00 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f        (mem3.aPoo
8cb10 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[j-1].u.hdr.siz
8cb20 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20  e4x/4)*8-8);.   
8cb30 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f   }.    fprintf(o
8cb40 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a  ut, "\n"); .  }.
8cb50 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8cb60 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d 65  master=%d\n", me
8cb70 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 66  m3.iMaster);.  f
8cb80 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f 77  printf(out, "now
8cb90 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33  Used=%d\n", mem3
8cba0 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e  .nPool*8 - mem3.
8cbb0 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 66  szMaster*8);.  f
8cbc0 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78 55  printf(out, "mxU
8cbd0 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e  sed=%d\n", mem3.
8cbe0 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e 6d  nPool*8 - mem3.m
8cbf0 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 73 71  nMaster*8);.  sq
8cc00 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
8cc10 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20  e(mem3.mutex);. 
8cc20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74   if( out==stdout
8cc30 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73   ){.    fflush(s
8cc40 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  tdout);.  }else{
8cc50 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29  .    fclose(out)
8cc60 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e  ;.  }.#else.  UN
8cc70 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a  USED_PARAMETER(z
8cc80 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69  Filename);.#endi
8cc90 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  f.}../*.** This 
8cca0 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f  routine is the o
8ccb0 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74  nly routine in t
8ccc0 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78  his file with ex
8ccd0 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61  ternal .** linka
8cce0 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c 61  ge..**.** Popula
8ccf0 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c  te the low-level
8cd00 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
8cd10 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e  on function poin
8cd20 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74  ters in.** sqlit
8cd30 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
8cd40 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
8cd50 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69  o the routines i
8cd60 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 54 68 65  n this file. The
8cd70 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 73 70  .** arguments sp
8cd80 65 63 69 66 79 20 74 68 65 20 62 6c 6f 63 6b 20  ecify the block 
8cd90 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61 6e  of memory to man
8cda0 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  age..**.** This 
8cdb0 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20  routine is only 
8cdc0 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74 65  called by sqlite
8cdd0 33 5f 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64 20  3_config(), and 
8cde0 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73 20  therefore.** is 
8cdf0 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
8ce00 62 65 20 74 68 72 65 61 64 73 61 66 65 20 28 69  be threadsafe (i
8ce10 74 20 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53 51  t is not)..*/.SQ
8ce20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
8ce30 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  st sqlite3_mem_m
8ce40 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d  ethods *sqlite3M
8ce50 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 6f 69  emGetMemsys3(voi
8ce60 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  d){.  static con
8ce70 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  st sqlite3_mem_m
8ce80 65 74 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d 65  ethods mempoolMe
8ce90 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 6d  thods = {.     m
8cea0 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20 20  emsys3Malloc,.  
8ceb0 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 2c 0a     memsys3Free,.
8cec0 20 20 20 20 20 6d 65 6d 73 79 73 33 52 65 61 6c       memsys3Real
8ced0 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73  loc,.     memsys
8cee0 33 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d 73  3Size,.     mems
8cef0 79 73 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20  ys3Roundup,.    
8cf00 20 6d 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20 20   memsys3Init,.  
8cf10 20 20 20 6d 65 6d 73 79 73 33 53 68 75 74 64 6f     memsys3Shutdo
8cf20 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a  wn,.     0.  };.
8cf30 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f 6f    return &mempoo
8cf40 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e  lMethods;.}..#en
8cf50 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e  dif /* SQLITE_EN
8cf60 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f 0a  ABLE_MEMSYS3 */.
8cf70 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
8cf80 20 45 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20 2a   End of mem3.c *
8cf90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
8cfc0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
8cfd0 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 35   Begin file mem5
8cfe0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
8cff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
8d010 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f  ./*.** 2007 Octo
8d020 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 14.**.** The
8d030 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
8d040 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
8d050 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
8d060 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
8d070 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
8d080 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
8d090 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
8d0a0 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
8d0b0 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
8d0c0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
8d0d0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
8d0e0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
8d0f0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
8d100 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
8d110 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
8d120 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
8d130 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
8d140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8d180 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
8d190 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
8d1a0 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
8d1b0 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d   implement a mem
8d1c0 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  ory.** allocatio
8d1d0 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f 72 20  n subsystem for 
8d1e0 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 20 0a  use by SQLite. .
8d1f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  **.** This versi
8d200 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
8d210 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
8d220 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a  ystem omits all.
8d230 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63  ** use of malloc
8d240 28 29 2e 20 54 68 65 20 61 70 70 6c 69 63 61 74  (). The applicat
8d250 69 6f 6e 20 67 69 76 65 73 20 53 51 4c 69 74 65  ion gives SQLite
8d260 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f   a block of memo
8d270 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61 6c  ry.** before cal
8d280 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e 69  ling sqlite3_ini
8d290 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20 77  tialize() from w
8d2a0 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  hich allocations
8d2b0 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e 64  .** are made and
8d2c0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
8d2d0 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 78   xMalloc() and x
8d2e0 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69 6d  Realloc() .** im
8d2f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20 4f  plementations. O
8d300 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  nce sqlite3_init
8d310 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65 65  ialize() has bee
8d320 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68 65  n called,.** the
8d330 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72   amount of memor
8d340 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 53  y available to S
8d350 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20 61  QLite is fixed a
8d360 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65 20  nd cannot.** be 
8d370 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  changed..**.** T
8d380 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
8d390 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
8d3a0 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 69  tion subsystem i
8d3b0 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69 6e  s included.** in
8d3c0 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79 20   the build only 
8d3d0 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
8d3e0 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69  _MEMSYS5 is defi
8d3f0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ned..**.** This 
8d400 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
8d410 20 75 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77   uses the follow
8d420 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 3a 0a 2a  ing algorithm:.*
8d430 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 6c 6c 20 6d  *.**   1.  All m
8d440 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
8d450 73 20 73 69 7a 65 73 20 61 72 65 20 72 6f 75 6e  s sizes are roun
8d460 64 65 64 20 75 70 20 74 6f 20 61 20 70 6f 77 65  ded up to a powe
8d470 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 20 20  r of 2..**.**   
8d480 32 2e 20 20 49 66 20 74 77 6f 20 61 64 6a 61 63  2.  If two adjac
8d490 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73 20  ent free blocks 
8d4a0 61 72 65 20 74 68 65 20 68 61 6c 76 65 73 20 6f  are the halves o
8d4b0 66 20 61 20 6c 61 72 67 65 72 20 62 6c 6f 63 6b  f a larger block
8d4c0 2c 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 6e 20  ,.**       then 
8d4d0 74 68 65 20 74 77 6f 20 62 6c 6f 63 6b 73 20 61  the two blocks a
8d4e0 72 65 20 63 6f 61 6c 65 73 65 64 20 69 6e 74 6f  re coalesed into
8d4f0 20 74 68 65 20 73 69 6e 67 6c 65 20 6c 61 72 67   the single larg
8d500 65 72 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20  er block..**.** 
8d510 20 20 33 2e 20 20 4e 65 77 20 6d 65 6d 6f 72 79    3.  New memory
8d520 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 72   is allocated fr
8d530 6f 6d 20 74 68 65 20 66 69 72 73 74 20 61 76 61  om the first ava
8d540 69 6c 61 62 6c 65 20 66 72 65 65 20 62 6c 6f 63  ilable free bloc
8d550 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 6c  k..**.** This al
8d560 67 6f 72 69 74 68 6d 20 69 73 20 64 65 73 63 72  gorithm is descr
8d570 69 62 65 64 20 69 6e 3a 20 4a 2e 20 4d 2e 20 52  ibed in: J. M. R
8d580 6f 62 73 6f 6e 2e 20 22 42 6f 75 6e 64 73 20 66  obson. "Bounds f
8d590 6f 72 20 53 6f 6d 65 20 46 75 6e 63 74 69 6f 6e  or Some Function
8d5a0 73 0a 2a 2a 20 43 6f 6e 63 65 72 6e 69 6e 67 20  s.** Concerning 
8d5b0 44 79 6e 61 6d 69 63 20 53 74 6f 72 61 67 65 20  Dynamic Storage 
8d5c0 41 6c 6c 6f 63 61 74 69 6f 6e 22 2e 20 4a 6f 75  Allocation". Jou
8d5d0 72 6e 61 6c 20 6f 66 20 74 68 65 20 41 73 73 6f  rnal of the Asso
8d5e0 63 69 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 43  ciation for.** C
8d5f0 6f 6d 70 75 74 69 6e 67 20 4d 61 63 68 69 6e 65  omputing Machine
8d600 72 79 2c 20 56 6f 6c 75 6d 65 20 32 31 2c 20 4e  ry, Volume 21, N
8d610 75 6d 62 65 72 20 38 2c 20 4a 75 6c 79 20 31 39  umber 8, July 19
8d620 37 34 2c 20 70 61 67 65 73 20 34 39 31 2d 34 39  74, pages 491-49
8d630 39 2e 0a 2a 2a 20 0a 2a 2a 20 4c 65 74 20 6e 20  9..** .** Let n 
8d640 62 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  be the size of t
8d650 68 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f 63  he largest alloc
8d660 61 74 69 6f 6e 20 64 69 76 69 64 65 64 20 62 79  ation divided by
8d670 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 2a 2a 20   the minimum.** 
8d680 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20  allocation size 
8d690 28 61 66 74 65 72 20 72 6f 75 6e 64 69 6e 67 20  (after rounding 
8d6a0 61 6c 6c 20 73 69 7a 65 73 20 75 70 20 74 6f 20  all sizes up to 
8d6b0 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 29 20 20  a power of 2.)  
8d6c0 4c 65 74 20 4d 0a 2a 2a 20 62 65 20 74 68 65 20  Let M.** be the 
8d6d0 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f  maximum amount o
8d6e0 66 20 6d 65 6d 6f 72 79 20 65 76 65 72 20 6f 75  f memory ever ou
8d6f0 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65  tstanding at one
8d700 20 74 69 6d 65 2e 20 20 4c 65 74 0a 2a 2a 20 4e   time.  Let.** N
8d710 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 61 6d   be the total am
8d720 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 61  ount of memory a
8d730 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c 6c  vailable for all
8d740 6f 63 61 74 69 6f 6e 2e 20 20 52 6f 62 73 6f 6e  ocation.  Robson
8d750 0a 2a 2a 20 70 72 6f 76 65 64 20 74 68 61 74 20  .** proved that 
8d760 74 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  this memory allo
8d770 63 61 74 6f 72 20 77 69 6c 6c 20 6e 65 76 65 72  cator will never
8d780 20 62 72 65 61 6b 64 6f 77 6e 20 64 75 65 20 74   breakdown due t
8d790 6f 20 0a 2a 2a 20 66 72 61 67 6d 65 6e 74 61 74  o .** fragmentat
8d7a0 69 6f 6e 20 61 73 20 6c 6f 6e 67 20 61 73 20 74  ion as long as t
8d7b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e  he following con
8d7c0 73 74 72 61 69 6e 74 20 68 6f 6c 64 73 3a 0a 2a  straint holds:.*
8d7d0 2a 0a 2a 2a 20 20 20 20 20 20 4e 20 3e 3d 20 20  *.**      N >=  
8d7e0 4d 2a 28 31 20 2b 20 6c 6f 67 32 28 6e 29 2f 32  M*(1 + log2(n)/2
8d7f0 29 20 2d 20 6e 20 2b 20 31 0a 2a 2a 0a 2a 2a 20  ) - n + 1.**.** 
8d800 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74  The sqlite3_stat
8d810 75 73 28 29 20 6c 6f 67 69 63 20 74 72 61 63 6b  us() logic track
8d820 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61  s the maximum va
8d830 6c 75 65 73 20 6f 66 20 6e 20 61 6e 64 20 4d 20  lues of n and M 
8d840 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 20 61 70  so.** that an ap
8d850 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 2c 20 61  plication can, a
8d860 74 20 61 6e 79 20 74 69 6d 65 2c 20 76 65 72 69  t any time, veri
8d870 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69  fy this constrai
8d880 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  nt..*/../*.** Th
8d890 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  is version of th
8d8a0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
8d8b0 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20  or is used only 
8d8c0 77 68 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  when .** SQLITE_
8d8d0 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69  ENABLE_MEMSYS5 i
8d8e0 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69  s defined..*/.#i
8d8f0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
8d900 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a 2f 2a 0a 2a  LE_MEMSYS5../*.*
8d910 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f  * A minimum allo
8d920 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e 73  cation is an ins
8d930 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
8d940 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
8d950 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c 6f  ..** Larger allo
8d960 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20 61  cations are an a
8d970 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73 74  rray of these st
8d980 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20 74  ructures where t
8d990 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68  he.** size of th
8d9a0 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f 77  e array is a pow
8d9b0 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 54  er of 2..**.** T
8d9c0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20  he size of this 
8d9d0 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20 61  object must be a
8d9e0 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20   power of two.  
8d9f0 54 68 61 74 20 66 61 63 74 20 69 73 0a 2a 2a 20  That fact is.** 
8da00 76 65 72 69 66 69 65 64 20 69 6e 20 6d 65 6d 73  verified in mems
8da10 79 73 35 49 6e 69 74 28 29 2e 0a 2a 2f 0a 74 79  ys5Init()..*/.ty
8da20 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d  pedef struct Mem
8da30 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b 0a  5Link Mem5Link;.
8da40 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b 20  struct Mem5Link 
8da50 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20 20  {.  int next;   
8da60 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
8da70 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b 20  next free chunk 
8da80 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b 20 20  */.  int prev;  
8da90 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
8daa0 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20 63   previous free c
8dab0 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  hunk */.};../*.*
8dac0 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f  * Maximum size o
8dad0 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  f any allocation
8dae0 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 58 29   is ((1<<LOGMAX)
8daf0 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 2e 20 53  *mem5.szAtom). S
8db00 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 73 7a 41  ince.** mem5.szA
8db10 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61 74  tom is always at
8db20 20 6c 65 61 73 74 20 38 20 61 6e 64 20 33 32 2d   least 8 and 32-
8db30 62 69 74 20 69 6e 74 65 67 65 72 73 20 61 72 65  bit integers are
8db40 20 75 73 65 64 2c 0a 2a 2a 20 69 74 20 69 73 20   used,.** it is 
8db50 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 70 6f 73  not actually pos
8db60 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68 20 74  sible to reach t
8db70 68 69 73 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64  his limit..*/.#d
8db80 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30 0a  efine LOGMAX 30.
8db90 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 65  ./*.** Masks use
8dba0 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72 6c  d for mem5.aCtrl
8dbb0 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a  [] elements..*/.
8dbc0 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f 47  #define CTRL_LOG
8dbd0 53 49 5a 45 20 20 30 78 31 66 20 20 20 20 2f 2a  SIZE  0x1f    /*
8dbe0 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 20 74 68   Log2 Size of th
8dbf0 69 73 20 62 6c 6f 63 6b 20 2a 2f 0a 23 64 65 66  is block */.#def
8dc00 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20 20  ine CTRL_FREE   
8dc10 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72 75    0x20    /* Tru
8dc20 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65 64  e if not checked
8dc30 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41   out */../*.** A
8dc40 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63  ll of the static
8dc50 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20   variables used 
8dc60 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61  by this module a
8dc70 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20  re collected.** 
8dc80 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74  into a single st
8dc90 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d  ructure named "m
8dca0 65 6d 35 22 2e 20 20 54 68 69 73 20 69 73 20 74  em5".  This is t
8dcb0 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74  o keep the.** st
8dcc0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f  atic variables o
8dcd0 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20  rganized and to 
8dce0 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65  reduce namespace
8dcf0 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68   pollution.** wh
8dd00 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69  en this module i
8dd10 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20  s combined with 
8dd20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61  other in the ama
8dd30 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  lgamation..*/.st
8dd40 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
8dd50 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61  struct Mem5Globa
8dd60 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 65  l {.  /*.  ** Me
8dd70 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 66  mory available f
8dd80 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20 20  or allocation.  
8dd90 2a 2f 0a 20 20 69 6e 74 20 73 7a 41 74 6f 6d 3b  */.  int szAtom;
8dda0 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65 73        /* Smalles
8ddb0 74 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c 6f 63  t possible alloc
8ddc0 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 20 2a  ation in bytes *
8ddd0 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b 20  /.  int nBlock; 
8dde0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
8ddf0 66 20 73 7a 41 74 6f 6d 20 73 69 7a 65 64 20 62  f szAtom sized b
8de00 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20 2a  locks in zPool *
8de10 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 20 20  /.  u8 *zPool;  
8de20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61       /* Memory a
8de30 76 61 69 6c 61 62 6c 65 20 74 6f 20 62 65 20 61  vailable to be a
8de40 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 0a 20  llocated */.  . 
8de50 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74   /*.  ** Mutex t
8de60 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
8de70 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   to the memory a
8de80 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
8de90 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  tem..  */.  sqli
8dea0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
8deb0 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 65 72  ;..  /*.  ** Per
8dec0 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73 74  formance statist
8ded0 69 63 73 0a 20 20 2a 2f 0a 20 20 75 36 34 20 6e  ics.  */.  u64 n
8dee0 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 2f  Alloc;         /
8def0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
8df00 66 20 63 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c 6f  f calls to mallo
8df10 63 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c  c */.  u64 total
8df20 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 54 6f  Alloc;     /* To
8df30 74 61 6c 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c 6f  tal of all mallo
8df40 63 20 63 61 6c 6c 73 20 2d 20 69 6e 63 6c 75 64  c calls - includ
8df50 65 73 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67  es internal frag
8df60 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c 45   */.  u64 totalE
8df70 78 63 65 73 73 3b 20 20 20 20 2f 2a 20 54 6f 74  xcess;    /* Tot
8df80 61 6c 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67  al internal frag
8df90 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75  mentation */.  u
8dfa0 33 32 20 63 75 72 72 65 6e 74 4f 75 74 3b 20 20  32 currentOut;  
8dfb0 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 63 68     /* Current ch
8dfc0 65 63 6b 6f 75 74 2c 20 69 6e 63 6c 75 64 69 6e  eckout, includin
8dfd0 67 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67 6d  g internal fragm
8dfe0 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75 33  entation */.  u3
8dff0 32 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b 20  2 currentCount; 
8e000 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d    /* Current num
8e010 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74 20  ber of distinct 
8e020 63 68 65 63 6b 6f 75 74 73 20 2a 2f 0a 20 20 75  checkouts */.  u
8e030 33 32 20 6d 61 78 4f 75 74 3b 20 20 20 20 20 20  32 maxOut;      
8e040 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e     /* Maximum in
8e050 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 72  stantaneous curr
8e060 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 75 33 32 20  entOut */.  u32 
8e070 6d 61 78 43 6f 75 6e 74 3b 20 20 20 20 20 20 20  maxCount;       
8e080 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74 61  /* Maximum insta
8e090 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e 74  ntaneous current
8e0a0 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 33 32 20 6d  Count */.  u32 m
8e0b0 61 78 52 65 71 75 65 73 74 3b 20 20 20 20 20 2f  axRequest;     /
8e0c0 2a 20 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63 61  * Largest alloca
8e0d0 74 69 6f 6e 20 28 65 78 63 6c 75 73 69 76 65 20  tion (exclusive 
8e0e0 6f 66 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67  of internal frag
8e0f0 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  ) */.  .  /*.  *
8e100 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65 20  * Lists of free 
8e110 62 6c 6f 63 6b 73 2e 20 20 61 69 46 72 65 65 6c  blocks.  aiFreel
8e120 69 73 74 5b 30 5d 20 69 73 20 61 20 6c 69 73 74  ist[0] is a list
8e130 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20   of free blocks 
8e140 6f 66 0a 20 20 2a 2a 20 73 69 7a 65 20 6d 65 6d  of.  ** size mem
8e150 35 2e 73 7a 41 74 6f 6d 2e 20 20 61 69 46 72 65  5.szAtom.  aiFre
8e160 65 6c 69 73 74 5b 31 5d 20 68 6f 6c 64 73 20 62  elist[1] holds b
8e170 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 73 7a  locks of size sz
8e180 41 74 6f 6d 2a 32 2e 0a 20 20 2a 2a 20 61 6e 64  Atom*2..  ** and
8e190 20 73 6f 20 66 6f 72 74 68 2e 0a 20 20 2a 2f 0a   so forth..  */.
8e1a0 20 20 69 6e 74 20 61 69 46 72 65 65 6c 69 73 74    int aiFreelist
8e1b0 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 20 2f  [LOGMAX+1];..  /
8e1c0 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 6f 72  *.  ** Space for
8e1d0 20 74 72 61 63 6b 69 6e 67 20 77 68 69 63 68 20   tracking which 
8e1e0 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 63 6b  blocks are check
8e1f0 65 64 20 6f 75 74 20 61 6e 64 20 74 68 65 20 73  ed out and the s
8e200 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 63 68  ize.  ** of each
8e210 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 79 74   block.  One byt
8e220 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 20 2a  e per block..  *
8e230 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b 0a 0a  /.  u8 *aCtrl;..
8e240 7d 20 6d 65 6d 35 20 3d 20 7b 20 30 20 7d 3b 0a  } mem5 = { 0 };.
8e250 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74 68  ./*.** Access th
8e260 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  e static variabl
8e270 65 20 74 68 72 6f 75 67 68 20 61 20 6d 61 63 72  e through a macr
8e280 6f 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f 4d 49  o for SQLITE_OMI
8e290 54 5f 57 53 44 0a 2a 2f 0a 23 64 65 66 69 6e 65  T_WSD.*/.#define
8e2a0 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74 72   mem5 GLOBAL(str
8e2b0 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c 20  uct Mem5Global, 
8e2c0 6d 65 6d 35 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73  mem5)../*.** Ass
8e2d0 75 6d 69 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f 6c  uming mem5.zPool
8e2e0 20 69 73 20 64 69 76 69 64 65 64 20 75 70 20 69   is divided up i
8e2f0 6e 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20  nto an array of 
8e300 4d 65 6d 35 4c 69 6e 6b 0a 2a 2a 20 73 74 72 75  Mem5Link.** stru
8e310 63 74 75 72 65 73 2c 20 72 65 74 75 72 6e 20 61  ctures, return a
8e320 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
8e330 69 64 78 2d 74 68 20 73 75 63 68 20 6c 69 6b 2e  idx-th such lik.
8e340 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 35  .*/.#define MEM5
8e350 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d 35  LINK(idx) ((Mem5
8e360 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a 50  Link *)(&mem5.zP
8e370 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e 73  ool[(idx)*mem5.s
8e380 7a 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20  zAtom]))../*.** 
8e390 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b  Unlink the chunk
8e3a0 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69   at mem5.aPool[i
8e3b0 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20 69  ] from list it i
8e3c0 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f  s currently.** o
8e3d0 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 20 62 65  n.  It should be
8e3e0 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e 61   found on mem5.a
8e3f0 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73 69  iFreelist[iLogsi
8e400 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ze]..*/.static v
8e410 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e  oid memsys5Unlin
8e420 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f  k(int i, int iLo
8e430 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 6e 65  gsize){.  int ne
8e440 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 73 73 65  xt, prev;.  asse
8e450 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d 65  rt( i>=0 && i<me
8e460 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61  m5.nBlock );.  a
8e470 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65 3e  ssert( iLogsize>
8e480 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c 3d  =0 && iLogsize<=
8e490 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73 65  LOGMAX );.  asse
8e4a0 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b  rt( (mem5.aCtrl[
8e4b0 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49 5a  i] & CTRL_LOGSIZ
8e4c0 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b 0a  E)==iLogsize );.
8e4d0 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d 35 4c 49  .  next = MEM5LI
8e4e0 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 70  NK(i)->next;.  p
8e4f0 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69  rev = MEM5LINK(i
8e500 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 28 20 70  )->prev;.  if( p
8e510 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 6d 65 6d  rev<0 ){.    mem
8e520 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
8e530 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a 20  gsize] = next;. 
8e540 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 45 4d 35   }else{.    MEM5
8e550 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78 74  LINK(prev)->next
8e560 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69   = next;.  }.  i
8e570 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20 20  f( next>=0 ){.  
8e580 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 78 74 29    MEM5LINK(next)
8e590 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a 20  ->prev = prev;. 
8e5a0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b   }.}../*.** Link
8e5b0 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d 65   the chunk at me
8e5c0 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20 74  m5.aPool[i] so t
8e5d0 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 69 4c  hat is on the iL
8e5e0 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65 20 6c  ogsize.** free l
8e5f0 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ist..*/.static v
8e600 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28  oid memsys5Link(
8e610 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67 73  int i, int iLogs
8e620 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b 0a 20  ize){.  int x;. 
8e630 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
8e640 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 35  _mutex_held(mem5
8e650 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  .mutex) );.  ass
8e660 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d  ert( i>=0 && i<m
8e670 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  em5.nBlock );.  
8e680 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65  assert( iLogsize
8e690 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c  >=0 && iLogsize<
8e6a0 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73  =LOGMAX );.  ass
8e6b0 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c  ert( (mem5.aCtrl
8e6c0 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49  [i] & CTRL_LOGSI
8e6d0 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b  ZE)==iLogsize );
8e6e0 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c 49 4e 4b  ..  x = MEM5LINK
8e6f0 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d 35  (i)->next = mem5
8e700 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67  .aiFreelist[iLog
8e710 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49 4e  size];.  MEM5LIN
8e720 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 2d 31 3b  K(i)->prev = -1;
8e730 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 7b 0a 20  .  if( x>=0 ){. 
8e740 20 20 20 61 73 73 65 72 74 28 20 78 3c 6d 65 6d     assert( x<mem
8e750 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20 20  5.nBlock );.    
8e760 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e 70 72 65  MEM5LINK(x)->pre
8e770 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 6d 65 6d  v = i;.  }.  mem
8e780 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
8e790 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a 2f  gsize] = i;.}../
8e7a0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54  *.** If the STAT
8e7b0 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20  IC_MEM mutex is 
8e7c0 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64  not already held
8e7d0 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e  , obtain it now.
8e7e0 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69   The mutex.** wi
8e7f0 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65  ll already be he
8e800 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20  ld (obtained by 
8e810 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63  code in malloc.c
8e820 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47  ) if.** sqlite3G
8e830 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
8e840 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f  Stat is true..*/
8e850 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
8e860 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29 7b  sys5Enter(void){
8e870 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
8e880 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74 65  _enter(mem5.mute
8e890 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  x);.}.static voi
8e8a0 64 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 76  d memsys5Leave(v
8e8b0 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  oid){.  sqlite3_
8e8c0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 35  mutex_leave(mem5
8e8d0 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  .mutex);.}../*.*
8e8e0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a  * Return the siz
8e8f0 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64  e of an outstand
8e900 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20  ing allocation, 
8e910 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a  in bytes.  The.*
8e920 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20  * size returned 
8e930 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74 65  omits the 8-byte
8e940 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 61 64   header overhead
8e950 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20  .  This only.** 
8e960 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73  works for chunks
8e970 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e   that are curren
8e980 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e  tly checked out.
8e990 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
8e9a0 65 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 64 20  emsys5Size(void 
8e9b0 2a 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a 65  *p){.  int iSize
8e9c0 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b   = 0;.  if( p ){
8e9d0 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28 75  .    int i = ((u
8e9e0 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c  8 *)p-mem5.zPool
8e9f0 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a 20  )/mem5.szAtom;. 
8ea00 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20     assert( i>=0 
8ea10 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  && i<mem5.nBlock
8ea20 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d 20   );.    iSize = 
8ea30 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 2a 20 28 31  mem5.szAtom * (1
8ea40 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b   << (mem5.aCtrl[
8ea50 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29  i]&CTRL_LOGSIZE)
8ea60 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
8ea70 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  iSize;.}../*.** 
8ea80 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 20 65  Find the first e
8ea90 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 65 65  ntry on the free
8eaa0 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e 20 20  list iLogsize.  
8eab0 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 65  Unlink that.** e
8eac0 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20  ntry and return 
8ead0 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73  its index. .*/.s
8eae0 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73  tatic int memsys
8eaf0 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 6e 74  5UnlinkFirst(int
8eb00 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e   iLogsize){.  in
8eb10 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72 73  t i;.  int iFirs
8eb20 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 4c  t;..  assert( iL
8eb30 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f  ogsize>=0 && iLo
8eb40 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b  gsize<=LOGMAX );
8eb50 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d 20  .  i = iFirst = 
8eb60 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
8eb70 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 73 73  iLogsize];.  ass
8eb80 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20 29  ert( iFirst>=0 )
8eb90 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29  ;.  while( i>0 )
8eba0 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 69 72  {.    if( i<iFir
8ebb0 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 69 3b  st ) iFirst = i;
8ebc0 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c 49 4e  .    i = MEM5LIN
8ebd0 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a  K(i)->next;.  }.
8ebe0 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28    memsys5Unlink(
8ebf0 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 7a 65  iFirst, iLogsize
8ec00 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 69 72  );.  return iFir
8ec10 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  st;.}../*.** Ret
8ec20 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d  urn a block of m
8ec30 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73  emory of at leas
8ec40 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65  t nBytes in size
8ec50 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c  ..** Return NULL
8ec60 20 69 66 20 75 6e 61 62 6c 65 2e 20 20 52 65 74   if unable.  Ret
8ec70 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 42 79 74  urn NULL if nByt
8ec80 65 73 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  es==0..**.** The
8ec90 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74 65   caller guarante
8eca0 65 73 20 74 68 61 74 20 6e 42 79 74 65 20 70 6f  es that nByte po
8ecb0 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  sitive..**.** Th
8ecc0 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62 74  e caller has obt
8ecd0 61 69 6e 65 64 20 61 20 6d 75 74 65 78 20 70 72  ained a mutex pr
8ece0 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20  ior to invoking 
8ecf0 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  this.** routine 
8ed00 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 76 65  so there is neve
8ed10 72 20 61 6e 79 20 63 68 61 6e 63 65 20 74 68 61  r any chance tha
8ed20 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a  t two or more.**
8ed30 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20   threads can be 
8ed40 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  in this routine 
8ed50 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
8ed60 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
8ed70 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63 55   *memsys5MallocU
8ed80 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65 29  nsafe(int nByte)
8ed90 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
8eda0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
8edb0 20 61 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 5d 20   a mem5.aPool[] 
8edc0 73 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 69 42  slot */.  int iB
8edd0 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  in;        /* In
8ede0 64 65 78 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 69  dex into mem5.ai
8edf0 46 72 65 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20 20  Freelist[] */.  
8ee00 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 20 20 20 20  int iFullSz;    
8ee10 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c 6f   /* Size of allo
8ee20 63 61 74 69 6f 6e 20 72 6f 75 6e 64 65 64 20 75  cation rounded u
8ee30 70 20 74 6f 20 70 6f 77 65 72 20 6f 66 20 32 20  p to power of 2 
8ee40 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f 67 73 69 7a  */.  int iLogsiz
8ee50 65 3b 20 20 20 20 2f 2a 20 4c 6f 67 32 20 6f 66  e;    /* Log2 of
8ee60 20 69 46 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d 49   iFullSz/POW2_MI
8ee70 4e 20 2a 2f 0a 0a 20 20 2f 2a 20 6e 42 79 74 65  N */..  /* nByte
8ee80 20 6d 75 73 74 20 62 65 20 61 20 70 6f 73 69 74   must be a posit
8ee90 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ive */.  assert(
8eea0 20 6e 42 79 74 65 3e 30 20 29 3b 0a 0a 20 20 2f   nByte>0 );..  /
8eeb0 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 20  * Keep track of 
8eec0 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f  the maximum allo
8eed0 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e 20  cation request. 
8eee0 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65   Even unfulfille
8eef0 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73 20  d.  ** requests 
8ef00 61 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a 20  are counted */. 
8ef10 20 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 3e   if( (u32)nByte>
8ef20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20  mem5.maxRequest 
8ef30 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52  ){.    mem5.maxR
8ef40 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b 0a  equest = nByte;.
8ef50 20 20 7d 0a 0a 20 20 2f 2a 20 41 62 6f 72 74 20    }..  /* Abort 
8ef60 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  if the requested
8ef70 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
8ef80 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
8ef90 74 68 65 20 6c 61 72 67 65 73 74 0a 20 20 2a 2a  the largest.  **
8efa0 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 74 68   power of two th
8efb0 61 74 20 77 65 20 63 61 6e 20 72 65 70 72 65 73  at we can repres
8efc0 65 6e 74 20 75 73 69 6e 67 20 33 32 2d 62 69 74  ent using 32-bit
8efd0 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73   signed integers
8efe0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 79  ..  */.  if( nBy
8eff0 74 65 20 3e 20 30 78 34 30 30 30 30 30 30 30 20  te > 0x40000000 
8f000 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
8f010 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64  .  }..  /* Round
8f020 20 6e 42 79 74 65 20 75 70 20 74 6f 20 74 68 65   nByte up to the
8f030 20 6e 65 78 74 20 76 61 6c 69 64 20 70 6f 77 65   next valid powe
8f040 72 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66 6f  r of two */.  fo
8f050 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 73  r(iFullSz=mem5.s
8f060 7a 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d  zAtom, iLogsize=
8f070 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 65  0; iFullSz<nByte
8f080 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20  ; iFullSz *= 2, 
8f090 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20  iLogsize++){}.. 
8f0a0 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d 65   /* Make sure me
8f0b0 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c  m5.aiFreelist[iL
8f0c0 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73  ogsize] contains
8f0d0 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66 72   at least one fr
8f0e0 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20  ee.  ** block.  
8f0f0 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c  If not, then spl
8f100 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68  it a block of th
8f110 65 20 6e 65 78 74 20 6c 61 72 67 65 72 20 70 6f  e next larger po
8f120 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f 20  wer of.  ** two 
8f130 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61  in order to crea
8f140 74 65 20 61 20 6e 65 77 20 66 72 65 65 20 62 6c  te a new free bl
8f150 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67  ock of size iLog
8f160 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  size..  */.  for
8f170 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20  (iBin=iLogsize; 
8f180 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
8f190 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e 3c  iBin]<0 && iBin<
8f1a0 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b 29  =LOGMAX; iBin++)
8f1b0 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c 4f  {}.  if( iBin>LO
8f1c0 47 4d 41 58 20 29 20 72 65 74 75 72 6e 20 30 3b  GMAX ) return 0;
8f1d0 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35 55 6e  .  i = memsys5Un
8f1e0 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e 29 3b  linkFirst(iBin);
8f1f0 0a 20 20 77 68 69 6c 65 28 20 69 42 69 6e 3e 69  .  while( iBin>i
8f200 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 20 20 69  Logsize ){.    i
8f210 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 20  nt newSize;..   
8f220 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 77   iBin--;.    new
8f230 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69 6e  Size = 1 << iBin
8f240 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c  ;.    mem5.aCtrl
8f250 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43 54  [i+newSize] = CT
8f260 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b 0a  RL_FREE | iBin;.
8f270 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28      memsys5Link(
8f280 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e 29  i+newSize, iBin)
8f290 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 74  ;.  }.  mem5.aCt
8f2a0 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a 65  rl[i] = iLogsize
8f2b0 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 61  ;..  /* Update a
8f2c0 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 6f 72 6d  llocator perform
8f2d0 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73 2e  ance statistics.
8f2e0 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f   */.  mem5.nAllo
8f2f0 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61  c++;.  mem5.tota
8f300 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c 53  lAlloc += iFullS
8f310 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 45  z;.  mem5.totalE
8f320 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53 7a  xcess += iFullSz
8f330 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d 35   - nByte;.  mem5
8f340 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b  .currentCount++;
8f350 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f  .  mem5.currentO
8f360 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20  ut += iFullSz;. 
8f370 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f 75   if( mem5.maxCou
8f380 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43  nt<mem5.currentC
8f390 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78 43  ount ) mem5.maxC
8f3a0 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72  ount = mem5.curr
8f3b0 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28 20  entCount;.  if( 
8f3c0 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d 35  mem5.maxOut<mem5
8f3d0 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 20 6d 65  .currentOut ) me
8f3e0 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d 35  m5.maxOut = mem5
8f3f0 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 20  .currentOut;..  
8f400 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  /* Return a poin
8f410 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63  ter to the alloc
8f420 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a  ated memory. */.
8f430 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
8f440 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d 65  &mem5.zPool[i*me
8f450 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f  m5.szAtom];.}../
8f460 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74  *.** Free an out
8f470 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20  standing memory 
8f480 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  allocation..*/.s
8f490 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79  tatic void memsy
8f4a0 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 6f 69  s5FreeUnsafe(voi
8f4b0 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 32 20  d *pOld){.  u32 
8f4c0 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a  size, iLogsize;.
8f4d0 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 0a 0a 20    int iBlock;.. 
8f4e0 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20 74   /* Set iBlock t
8f4f0 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  o the index of t
8f500 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65 64  he block pointed
8f510 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20 0a   to by pOld in .
8f520 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20 6f    ** the array o
8f530 66 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 62 79  f mem5.szAtom by
8f540 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74 65  te blocks pointe
8f550 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50 6f  d to by mem5.zPo
8f560 6f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f 63  ol..  */.  iBloc
8f570 6b 20 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64 2d  k = ((u8 *)pOld-
8f580 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d 35  mem5.zPool)/mem5
8f590 2e 73 7a 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20 43  .szAtom;..  /* C
8f5a0 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f  heck that the po
8f5b0 69 6e 74 65 72 20 70 4f 6c 64 20 70 6f 69 6e 74  inter pOld point
8f5c0 73 20 74 6f 20 61 20 76 61 6c 69 64 2c 20 6e 6f  s to a valid, no
8f5d0 6e 2d 66 72 65 65 20 62 6c 6f 63 6b 2e 20 2a 2f  n-free block. */
8f5e0 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63  .  assert( iBloc
8f5f0 6b 3e 3d 30 20 26 26 20 69 42 6c 6f 63 6b 3c 6d  k>=0 && iBlock<m
8f600 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  em5.nBlock );.  
8f610 61 73 73 65 72 74 28 20 28 28 75 38 20 2a 29 70  assert( ((u8 *)p
8f620 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 25  Old-mem5.zPool)%
8f630 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3d 3d 30 20 29  mem5.szAtom==0 )
8f640 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d  ;.  assert( (mem
8f650 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20  5.aCtrl[iBlock] 
8f660 26 20 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30 20  & CTRL_FREE)==0 
8f670 29 3b 0a 0a 20 20 69 4c 6f 67 73 69 7a 65 20 3d  );..  iLogsize =
8f680 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
8f690 63 6b 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49  ck] & CTRL_LOGSI
8f6a0 5a 45 3b 0a 20 20 73 69 7a 65 20 3d 20 31 3c 3c  ZE;.  size = 1<<
8f6b0 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 61 73 73 65  iLogsize;.  asse
8f6c0 72 74 28 20 69 42 6c 6f 63 6b 2b 73 69 7a 65 2d  rt( iBlock+size-
8f6d0 31 3c 28 75 33 32 29 6d 65 6d 35 2e 6e 42 6c 6f  1<(u32)mem5.nBlo
8f6e0 63 6b 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43  ck );..  mem5.aC
8f6f0 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 7c 3d 20 43  trl[iBlock] |= C
8f700 54 52 4c 5f 46 52 45 45 3b 0a 20 20 6d 65 6d 35  TRL_FREE;.  mem5
8f710 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 2b 73 69  .aCtrl[iBlock+si
8f720 7a 65 2d 31 5d 20 7c 3d 20 43 54 52 4c 5f 46 52  ze-1] |= CTRL_FR
8f730 45 45 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65  EE;.  assert( me
8f740 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 3e  m5.currentCount>
8f750 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  0 );.  assert( m
8f760 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 3d  em5.currentOut>=
8f770 28 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74 6f  (size*mem5.szAto
8f780 6d 29 20 29 3b 0a 20 20 6d 65 6d 35 2e 63 75 72  m) );.  mem5.cur
8f790 72 65 6e 74 43 6f 75 6e 74 2d 2d 3b 0a 20 20 6d  rentCount--;.  m
8f7a0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 2d  em5.currentOut -
8f7b0 3d 20 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74  = size*mem5.szAt
8f7c0 6f 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65  om;.  assert( me
8f7d0 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 30 20  m5.currentOut>0 
8f7e0 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43  || mem5.currentC
8f7f0 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73  ount==0 );.  ass
8f800 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e  ert( mem5.curren
8f810 74 43 6f 75 6e 74 3e 30 20 7c 7c 20 6d 65 6d 35  tCount>0 || mem5
8f820 2e 63 75 72 72 65 6e 74 4f 75 74 3d 3d 30 20 29  .currentOut==0 )
8f830 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b  ;..  mem5.aCtrl[
8f840 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46  iBlock] = CTRL_F
8f850 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a  REE | iLogsize;.
8f860 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28    while( ALWAYS(
8f870 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 29  iLogsize<LOGMAX)
8f880 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75 64   ){.    int iBud
8f890 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42 6c  dy;.    if( (iBl
8f8a0 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26  ock>>iLogsize) &
8f8b0 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75 64   1 ){.      iBud
8f8c0 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 69  dy = iBlock - si
8f8d0 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ze;.    }else{. 
8f8e0 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69 42       iBuddy = iB
8f8f0 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 20  lock + size;.   
8f900 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 69   }.    assert( i
8f910 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20 20  Buddy>=0 );.    
8f920 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c 3c  if( (iBuddy+(1<<
8f930 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35 2e  iLogsize))>mem5.
8f940 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b 0a  nBlock ) break;.
8f950 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43 74      if( mem5.aCt
8f960 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54 52  rl[iBuddy]!=(CTR
8f970 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a  L_FREE | iLogsiz
8f980 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  e) ) break;.    
8f990 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42  memsys5Unlink(iB
8f9a0 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b  uddy, iLogsize);
8f9b0 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b  .    iLogsize++;
8f9c0 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79 3c  .    if( iBuddy<
8f9d0 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  iBlock ){.      
8f9e0 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64  mem5.aCtrl[iBudd
8f9f0 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c  y] = CTRL_FREE |
8fa00 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20   iLogsize;.     
8fa10 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
8fa20 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  ck] = 0;.      i
8fa30 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b 0a  Block = iBuddy;.
8fa40 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
8fa50 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f   mem5.aCtrl[iBlo
8fa60 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20  ck] = CTRL_FREE 
8fa70 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20  | iLogsize;.    
8fa80 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75    mem5.aCtrl[iBu
8fa90 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  ddy] = 0;.    }.
8faa0 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a 20      size *= 2;. 
8fab0 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b   }.  memsys5Link
8fac0 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a  (iBlock, iLogsiz
8fad0 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  e);.}../*.** All
8fae0 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66 20  ocate nBytes of 
8faf0 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 63  memory.*/.static
8fb00 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61   void *memsys5Ma
8fb10 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73 29  lloc(int nBytes)
8fb20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
8fb30 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20  4 *p = 0;.  if( 
8fb40 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20 20  nBytes>0 ){.    
8fb50 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a  memsys5Enter();.
8fb60 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d      p = memsys5M
8fb70 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74  allocUnsafe(nByt
8fb80 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35  es);.    memsys5
8fb90 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20 72  Leave();.  }.  r
8fba0 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20  eturn (void*)p; 
8fbb0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d  .}../*.** Free m
8fbc0 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  emory..**.** The
8fbd0 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65 6d   outer layer mem
8fbe0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70 72  ory allocator pr
8fbf0 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75 74  events this rout
8fc00 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69 6e  ine from.** bein
8fc10 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 50  g called with pP
8fc20 72 69 6f 72 3d 3d 30 2e 0a 2a 2f 0a 73 74 61 74  rior==0..*/.stat
8fc30 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46  ic void memsys5F
8fc40 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ree(void *pPrior
8fc50 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72  ){.  assert( pPr
8fc60 69 6f 72 21 3d 30 20 29 3b 0a 20 20 6d 65 6d 73  ior!=0 );.  mems
8fc70 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6d 65  ys5Enter();.  me
8fc80 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28  msys5FreeUnsafe(
8fc90 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73 79  pPrior);.  memsy
8fca0 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d 0a 0a  s5Leave();  .}..
8fcb0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
8fcc0 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73   size of an exis
8fcd0 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ting memory allo
8fce0 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  cation..**.** Th
8fcf0 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65  e outer layer me
8fd00 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70  mory allocator p
8fd10 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75  revents this rou
8fd20 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69  tine from.** bei
8fd30 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70  ng called with p
8fd40 50 72 69 6f 72 3d 3d 30 2e 20 20 0a 2a 2a 0a 2a  Prior==0.  .**.*
8fd50 2a 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77 61  * nBytes is alwa
8fd60 79 73 20 61 20 76 61 6c 75 65 20 6f 62 74 61 69  ys a value obtai
8fd70 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72  ned from a prior
8fd80 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 6d 65 6d 73   call to.** mems
8fd90 79 73 35 52 6f 75 6e 64 28 29 2e 20 20 48 65 6e  ys5Round().  Hen
8fda0 63 65 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77  ce nBytes is alw
8fdb0 61 79 73 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69  ays a non-negati
8fdc0 76 65 20 70 6f 77 65 72 0a 2a 2a 20 6f 66 20 74  ve power.** of t
8fdd0 77 6f 2e 20 20 49 66 20 6e 42 79 74 65 73 3d 3d  wo.  If nBytes==
8fde0 30 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  0 that means tha
8fdf0 74 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 61 6c  t an oversize al
8fe00 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 28 61 6e 20  location.** (an 
8fe10 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 65  allocation large
8fe20 72 20 74 68 61 6e 20 30 78 34 30 30 30 30 30 30  r than 0x4000000
8fe30 30 29 20 77 61 73 20 72 65 71 75 65 73 74 65 64  0) was requested
8fe40 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75   and this.** rou
8fe50 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74 75  tine should retu
8fe60 72 6e 20 30 20 77 69 74 68 6f 75 74 20 66 72 65  rn 0 without fre
8fe70 65 69 6e 67 20 70 50 72 69 6f 72 2e 0a 2a 2f 0a  eing pPrior..*/.
8fe80 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d  static void *mem
8fe90 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69 64  sys5Realloc(void
8fea0 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42   *pPrior, int nB
8feb0 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c  ytes){.  int nOl
8fec0 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20  d;.  void *p;.  
8fed0 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21 3d  assert( pPrior!=
8fee0 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  0 );.  assert( (
8fef0 6e 42 79 74 65 73 26 28 6e 42 79 74 65 73 2d 31  nBytes&(nBytes-1
8ff00 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ))==0 );.  asser
8ff10 74 28 20 6e 42 79 74 65 73 3e 3d 30 20 29 3b 0a  t( nBytes>=0 );.
8ff20 20 20 69 66 28 20 6e 42 79 74 65 73 3d 3d 30 20    if( nBytes==0 
8ff30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
8ff40 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65  .  }.  nOld = me
8ff50 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f 72  msys5Size(pPrior
8ff60 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c  );.  if( nBytes<
8ff70 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65 74  =nOld ){.    ret
8ff80 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a  urn pPrior;.  }.
8ff90 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29    memsys5Enter()
8ffa0 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d  ;.  p = memsys5M
8ffb0 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74  allocUnsafe(nByt
8ffc0 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  es);.  if( p ){.
8ffd0 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70 50      memcpy(p, pP
8ffe0 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20  rior, nOld);.   
8fff0 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61   memsys5FreeUnsa
90000 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a  fe(pPrior);.  }.
90010 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 29    memsys5Leave()
90020 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
90030 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20  ./*.** Round up 
90040 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74  a request size t
90050 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64  o the next valid
90060 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
90070 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 61 6c 6c  .  If.** the all
90080 6f 63 61 74 69 6f 6e 20 69 73 20 74 6f 6f 20 6c  ocation is too l
90090 61 72 67 65 20 74 6f 20 62 65 20 68 61 6e 64 6c  arge to be handl
900a0 65 64 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63  ed by this alloc
900b0 61 74 69 6f 6e 20 73 79 73 74 65 6d 2c 0a 2a 2a  ation system,.**
900c0 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a   return 0..**.**
900d0 20 41 6c 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 73   All allocations
900e0 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72   must be a power
900f0 20 6f 66 20 74 77 6f 20 61 6e 64 20 6d 75 73 74   of two and must
90100 20 62 65 20 65 78 70 72 65 73 73 65 64 20 62 79   be expressed by
90110 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69 67   a.** 32-bit sig
90120 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 48 65  ned integer.  He
90130 6e 63 65 20 74 68 65 20 6c 61 72 67 65 73 74 20  nce the largest 
90140 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30 78  allocation is 0x
90150 34 30 30 30 30 30 30 30 0a 2a 2a 20 6f 72 20 31  40000000.** or 1
90160 30 37 33 37 34 31 38 32 34 20 62 79 74 65 73 2e  073741824 bytes.
90170 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
90180 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69 6e  emsys5Roundup(in
90190 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75 6c  t n){.  int iFul
901a0 6c 53 7a 3b 0a 20 20 69 66 28 20 6e 20 3e 20 30  lSz;.  if( n > 0
901b0 78 34 30 30 30 30 30 30 30 20 29 20 72 65 74 75  x40000000 ) retu
901c0 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 46 75 6c  rn 0;.  for(iFul
901d0 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b  lSz=mem5.szAtom;
901e0 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75 6c   iFullSz<n; iFul
901f0 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65 74  lSz *= 2);.  ret
90200 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a  urn iFullSz;.}..
90210 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
90220 20 63 65 69 6c 69 6e 67 20 6f 66 20 74 68 65 20   ceiling of the 
90230 6c 6f 67 61 72 69 74 68 6d 20 62 61 73 65 20 32  logarithm base 2
90240 20 6f 66 20 69 56 61 6c 75 65 2e 0a 2a 2a 0a 2a   of iValue..**.*
90250 2a 20 45 78 61 6d 70 6c 65 73 3a 20 20 20 6d 65  * Examples:   me
90260 6d 73 79 73 35 4c 6f 67 28 31 29 20 2d 3e 20 30  msys5Log(1) -> 0
90270 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
90280 6d 65 6d 73 79 73 35 4c 6f 67 28 32 29 20 2d 3e  memsys5Log(2) ->
90290 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   1.**           
902a0 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 34 29 20    memsys5Log(4) 
902b0 2d 3e 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 20  -> 2.**         
902c0 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 35      memsys5Log(5
902d0 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20 20  ) -> 3.**       
902e0 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67        memsys5Log
902f0 28 38 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20  (8) -> 3.**     
90300 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c          memsys5L
90310 6f 67 28 39 29 20 2d 3e 20 34 0a 2a 2f 0a 73 74  og(9) -> 4.*/.st
90320 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35  atic int memsys5
90330 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29 7b  Log(int iValue){
90340 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 66  .  int iLog;.  f
90350 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69  or(iLog=0; (1<<i
90360 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c 6f  Log)<iValue; iLo
90370 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  g++);.  return i
90380 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  Log;.}../*.** In
90390 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d  itialize the mem
903a0 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 0a 2a  ory allocator..*
903b0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
903c0 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73  e is not threads
903d0 61 66 65 2e 20 20 54 68 65 20 63 61 6c 6c 65 72  afe.  The caller
903e0 20 6d 75 73 74 20 62 65 20 68 6f 6c 64 69 6e 67   must be holding
903f0 20 61 20 6d 75 74 65 78 0a 2a 2a 20 74 6f 20 70   a mutex.** to p
90400 72 65 76 65 6e 74 20 6d 75 6c 74 69 70 6c 65 20  revent multiple 
90410 74 68 72 65 61 64 73 20 66 72 6f 6d 20 65 6e 74  threads from ent
90420 65 72 69 6e 67 20 61 74 20 74 68 65 20 73 61 6d  ering at the sam
90430 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69  e time..*/.stati
90440 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e 69  c int memsys5Ini
90450 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
90460 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20  {.  int ii;     
90470 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
90480 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
90490 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 2f  nByte;         /
904a0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
904b0 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69  s of memory avai
904c0 6c 61 62 6c 65 20 74 6f 20 74 68 69 73 20 61 6c  lable to this al
904d0 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20  locator */.  u8 
904e0 2a 7a 42 79 74 65 3b 20 20 20 20 20 20 20 20 20  *zByte;         
904f0 2f 2a 20 4d 65 6d 6f 72 79 20 75 73 61 62 6c 65  /* Memory usable
90500 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63 61 74   by this allocat
90510 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69 6e  or */.  int nMin
90520 4c 6f 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c 6f  Log;       /* Lo
90530 67 20 62 61 73 65 20 32 20 6f 66 20 6d 69 6e 69  g base 2 of mini
90540 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  mum allocation s
90550 69 7a 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ize in bytes */.
90560 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20 20    int iOffset;  
90570 20 20 20 20 20 2f 2a 20 41 6e 20 6f 66 66 73 65       /* An offse
90580 74 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 43 74 72  t into mem5.aCtr
90590 6c 5b 5d 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44  l[] */..  UNUSED
905a0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
905b0 65 64 29 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20 74  ed);..  /* For t
905c0 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74  he purposes of t
905d0 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 64 69 73  his routine, dis
905e0 61 62 6c 65 20 74 68 65 20 6d 75 74 65 78 20 2a  able the mutex *
905f0 2f 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d  /.  mem5.mutex =
90600 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69   0;..  /* The si
90610 7a 65 20 6f 66 20 61 20 4d 65 6d 35 4c 69 6e 6b  ze of a Mem5Link
90620 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20   object must be 
90630 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20  a power of two. 
90640 20 56 65 72 69 66 79 20 74 68 61 74 0a 20 20 2a   Verify that.  *
90650 2a 20 74 68 69 73 20 69 73 20 63 61 73 65 2e 0a  * this is case..
90660 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28    */.  assert( (
90670 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b 29  sizeof(Mem5Link)
90680 26 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e  &(sizeof(Mem5Lin
90690 6b 29 2d 31 29 29 3d 3d 30 20 29 3b 0a 0a 20 20  k)-1))==0 );..  
906a0 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33 47  nByte = sqlite3G
906b0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61  lobalConfig.nHea
906c0 70 3b 0a 20 20 7a 42 79 74 65 20 3d 20 28 75 38  p;.  zByte = (u8
906d0 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  *)sqlite3GlobalC
906e0 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 61  onfig.pHeap;.  a
906f0 73 73 65 72 74 28 20 7a 42 79 74 65 21 3d 30 20  ssert( zByte!=0 
90700 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63  );  /* sqlite3_c
90710 6f 6e 66 69 67 28 29 20 64 6f 65 73 20 6e 6f 74  onfig() does not
90720 20 61 6c 6c 6f 77 20 6f 74 68 65 72 77 69 73 65   allow otherwise
90730 20 2a 2f 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d   */..  nMinLog =
90740 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 6c 69   memsys5Log(sqli
90750 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
90760 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 2e 73  mnReq);.  mem5.s
90770 7a 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69 6e  zAtom = (1<<nMin
90780 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20 28  Log);.  while( (
90790 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35 4c  int)sizeof(Mem5L
907a0 69 6e 6b 29 3e 6d 65 6d 35 2e 73 7a 41 74 6f 6d  ink)>mem5.szAtom
907b0 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 73 7a 41   ){.    mem5.szA
907c0 74 6f 6d 20 3d 20 6d 65 6d 35 2e 73 7a 41 74 6f  tom = mem5.szAto
907d0 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d  m << 1;.  }..  m
907e0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42  em5.nBlock = (nB
907f0 79 74 65 20 2f 20 28 6d 65 6d 35 2e 73 7a 41 74  yte / (mem5.szAt
90800 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 29 3b  om+sizeof(u8)));
90810 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20  .  mem5.zPool = 
90820 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 43  zByte;.  mem5.aC
90830 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d  trl = (u8 *)&mem
90840 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c  5.zPool[mem5.nBl
90850 6f 63 6b 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 5d  ock*mem5.szAtom]
90860 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  ;..  for(ii=0; i
90870 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b 29  i<=LOGMAX; ii++)
90880 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72 65  {.    mem5.aiFre
90890 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a  elist[ii] = -1;.
908a0 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20 3d    }..  iOffset =
908b0 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f 47   0;.  for(ii=LOG
908c0 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d 2d  MAX; ii>=0; ii--
908d0 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f  ){.    int nAllo
908e0 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20 20  c = (1<<ii);.   
908f0 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e 41   if( (iOffset+nA
90900 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f  lloc)<=mem5.nBlo
90910 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 35  ck ){.      mem5
90920 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d 20  .aCtrl[iOffset] 
90930 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45 45  = ii | CTRL_FREE
90940 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c  ;.      memsys5L
90950 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69 29  ink(iOffset, ii)
90960 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74 20  ;.      iOffset 
90970 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d  += nAlloc;.    }
90980 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f 66  .    assert((iOf
90990 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d  fset+nAlloc)>mem
909a0 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a  5.nBlock);.  }..
909b0 20 20 2f 2a 20 49 66 20 61 20 6d 75 74 65 78 20    /* If a mutex 
909c0 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  is required for 
909d0 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f 6e  normal operation
909e0 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20 2a  , allocate one *
909f0 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  /.  if( sqlite3G
90a00 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
90a10 73 74 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20 6d  stat==0 ){.    m
90a20 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69  em5.mutex = sqli
90a30 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
90a40 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
90a50 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 0a 20 20 72  C_MEM);.  }..  r
90a60 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
90a70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74  .}../*.** Deinit
90a80 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75  ialize this modu
90a90 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
90aa0 69 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f  id memsys5Shutdo
90ab0 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  wn(void *NotUsed
90ac0 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
90ad0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
90ae0 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 30    mem5.mutex = 0
90af0 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 23  ;.  return;.}..#
90b00 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
90b10 54 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65  T./*.** Open the
90b20 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64 20   file indicated 
90b30 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67 20  and write a log 
90b40 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d  of all unfreed m
90b50 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63 61  emory .** alloca
90b60 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74 20  tions into that 
90b70 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  log..*/.SQLITE_P
90b80 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
90b90 74 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28 63  te3Memsys5Dump(c
90ba0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
90bb0 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a 6f  name){.  FILE *o
90bc0 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20  ut;.  int i, j, 
90bd0 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67  n;.  int nMinLog
90be0 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61  ;..  if( zFilena
90bf0 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61  me==0 || zFilena
90c00 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  me[0]==0 ){.    
90c10 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20  out = stdout;.  
90c20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d  }else{.    out =
90c30 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65   fopen(zFilename
90c40 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20  , "w");.    if( 
90c50 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  out==0 ){.      
90c60 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
90c70 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75  "** Unable to ou
90c80 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75  tput memory debu
90c90 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73  g output log: %s
90ca0 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   **\n",.        
90cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46                zF
90cc0 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ilename);.      
90cd0 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
90ce0 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72  }.  memsys5Enter
90cf0 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20  ();.  nMinLog = 
90d00 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35 2e  memsys5Log(mem5.
90d10 73 7a 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 69  szAtom);.  for(i
90d20 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 26  =0; i<=LOGMAX &&
90d30 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69   i+nMinLog<32; i
90d40 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d 30  ++){.    for(n=0
90d50 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65 6c  , j=mem5.aiFreel
90d60 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20  ist[i]; j>=0; j 
90d70 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e  = MEM5LINK(j)->n
90d80 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20  ext, n++){}.    
90d90 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66 72  fprintf(out, "fr
90da0 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66 20  eelist items of 
90db0 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20  size %d: %d\n", 
90dc0 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20 69  mem5.szAtom << i
90dd0 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72 69  , n);.  }.  fpri
90de0 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e  ntf(out, "mem5.n
90df0 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25 6c  Alloc       = %l
90e00 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c  lu\n", mem5.nAll
90e10 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  oc);.  fprintf(o
90e20 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 41  ut, "mem5.totalA
90e30 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e 22  lloc   = %llu\n"
90e40 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f  , mem5.totalAllo
90e50 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  c);.  fprintf(ou
90e60 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45 78  t, "mem5.totalEx
90e70 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c  cess  = %llu\n",
90e80 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73   mem5.totalExces
90e90 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  s);.  fprintf(ou
90ea0 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74  t, "mem5.current
90eb0 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d  Out   = %u\n", m
90ec0 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29 3b  em5.currentOut);
90ed0 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
90ee0 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75  "mem5.currentCou
90ef0 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35  nt = %u\n", mem5
90f00 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b 0a  .currentCount);.
90f10 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
90f20 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20 20  mem5.maxOut     
90f30 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e    = %u\n", mem5.
90f40 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69 6e  maxOut);.  fprin
90f50 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d 61  tf(out, "mem5.ma
90f60 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75 5c  xCount     = %u\
90f70 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e  n", mem5.maxCoun
90f80 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  t);.  fprintf(ou
90f90 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71 75  t, "mem5.maxRequ
90fa0 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d  est   = %u\n", m
90fb0 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29 3b  em5.maxRequest);
90fc0 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  .  memsys5Leave(
90fd0 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73 74  );.  if( out==st
90fe0 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c 75  dout ){.    fflu
90ff0 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65  sh(stdout);.  }e
91000 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28  lse{.    fclose(
91010 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  out);.  }.}.#end
91020 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  if../*.** This r
91030 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e  outine is the on
91040 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68  ly routine in th
91050 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78 74  is file with ext
91060 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61 67  ernal .** linkag
91070 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61 20  e. It returns a 
91080 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  pointer to a sta
91090 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  tic sqlite3_mem_
910a0 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75 63  methods.** struc
910b0 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68  t populated with
910c0 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65 74   the memsys5 met
910d0 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  hods..*/.SQLITE_
910e0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71  PRIVATE const sq
910f0 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
91100 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74  s *sqlite3MemGet
91110 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a 20  Memsys5(void){. 
91120 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71   static const sq
91130 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64  lite3_mem_method
91140 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64 73  s memsys5Methods
91150 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73   = {.     memsys
91160 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65  5Malloc,.     me
91170 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20 20  msys5Free,.     
91180 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c 0a  memsys5Realloc,.
91190 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a 65       memsys5Size
911a0 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52 6f  ,.     memsys5Ro
911b0 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73  undup,.     mems
911c0 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65  ys5Init,.     me
911d0 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a 20  msys5Shutdown,. 
911e0 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74      0.  };.  ret
911f0 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74 68  urn &memsys5Meth
91200 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  ods;.}..#endif /
91210 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
91220 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a 2a  MEMSYS5 */../***
91230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
91240 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a 2a  of mem5.c ******
91250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
91280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
91290 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20 2a  n file mutex.c *
912a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
912b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
912c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
912d0 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31 34  * 2007 August 14
912e0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
912f0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
91300 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
91310 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
91320 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
91330 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
91340 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
91350 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
91360 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
91370 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
91380 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
91390 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
913a0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
913b0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
913c0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
913d0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
913e0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
913f0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
91400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
91440 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
91450 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
91460 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
91470 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a  ment mutexes..**
91480 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
91490 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74  ntains code that
914a0 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f 73   is common acros
914b0 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70 6c  s all mutex impl
914c0 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  ementations..*/.
914d0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
914e0 49 54 45 5f 44 45 42 55 47 29 20 26 26 20 21 64  ITE_DEBUG) && !d
914f0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55  efined(SQLITE_MU
91500 54 45 58 5f 4f 4d 49 54 29 0a 2f 2a 0a 2a 2a 20  TEX_OMIT)./*.** 
91510 46 6f 72 20 64 65 62 75 67 67 69 6e 67 20 70 75  For debugging pu
91520 72 70 6f 73 65 73 2c 20 72 65 63 6f 72 64 20 77  rposes, record w
91530 68 65 6e 20 74 68 65 20 6d 75 74 65 78 20 73 75  hen the mutex su
91540 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69  bsystem is initi
91550 61 6c 69 7a 65 64 0a 2a 2a 20 61 6e 64 20 75 6e  alized.** and un
91560 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 6f 20 74  initialized so t
91570 68 61 74 20 77 65 20 63 61 6e 20 61 73 73 65 72  hat we can asser
91580 74 28 29 20 69 66 20 74 68 65 72 65 20 69 73 20  t() if there is 
91590 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a  an attempt to.**
915a0 20 61 6c 6c 6f 63 61 74 65 20 61 20 6d 75 74 65   allocate a mute
915b0 78 20 77 68 69 6c 65 20 74 68 65 20 73 79 73 74  x while the syst
915c0 65 6d 20 69 73 20 75 6e 69 6e 69 74 69 61 6c 69  em is uninitiali
915d0 7a 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53  zed..*/.static S
915e0 51 4c 49 54 45 5f 57 53 44 20 69 6e 74 20 6d 75  QLITE_WSD int mu
915f0 74 65 78 49 73 49 6e 69 74 20 3d 20 30 3b 0a 23  texIsInit = 0;.#
91600 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
91610 44 45 42 55 47 20 2a 2f 0a 0a 0a 23 69 66 6e 64  DEBUG */...#ifnd
91620 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
91630 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  OMIT./*.** Initi
91640 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20  alize the mutex 
91650 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  system..*/.SQLIT
91660 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
91670 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76  lite3MutexInit(v
91680 6f 69 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 20  oid){ .  int rc 
91690 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
916a0 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
916b0 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65  Config.bCoreMute
916c0 78 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71  x ){.    if( !sq
916d0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
916e0 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c  g.mutex.xMutexAl
916f0 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  loc ){.      /* 
91700 49 66 20 74 68 65 20 78 4d 75 74 65 78 41 6c 6c  If the xMutexAll
91710 6f 63 20 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f  oc method has no
91720 74 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e  t been set, then
91730 20 74 68 65 20 75 73 65 72 20 64 69 64 20 6e 6f   the user did no
91740 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 61  t.      ** insta
91750 6c 6c 20 61 20 6d 75 74 65 78 20 69 6d 70 6c 65  ll a mutex imple
91760 6d 65 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 71  mentation via sq
91770 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 70  lite3_config() p
91780 72 69 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 2a  rior to .      *
91790 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  * sqlite3_initia
917a0 6c 69 7a 65 28 29 20 62 65 69 6e 67 20 63 61 6c  lize() being cal
917b0 6c 65 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20  led. This block 
917c0 63 6f 70 69 65 73 20 70 6f 69 6e 74 65 72 73 20  copies pointers 
917d0 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  to.      ** the 
917e0 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e  default implemen
917f0 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20  tation into the 
91800 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
91810 66 69 67 20 73 74 72 75 63 74 75 72 65 2e 0a 20  fig structure.. 
91820 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
91830 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
91840 6f 64 73 20 2a 70 46 72 6f 6d 20 3d 20 73 71 6c  ods *pFrom = sql
91850 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78  ite3DefaultMutex
91860 28 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ();.      sqlite
91870 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
91880 2a 70 54 6f 20 3d 20 26 73 71 6c 69 74 65 33 47  *pTo = &sqlite3G
91890 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
918a0 78 3b 0a 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  x;..      memcpy
918b0 28 70 54 6f 2c 20 70 46 72 6f 6d 2c 20 6f 66 66  (pTo, pFrom, off
918c0 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75  setof(sqlite3_mu
918d0 74 65 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75  tex_methods, xMu
918e0 74 65 78 41 6c 6c 6f 63 29 29 3b 0a 20 20 20 20  texAlloc));.    
918f0 20 20 6d 65 6d 63 70 79 28 26 70 54 6f 2d 3e 78    memcpy(&pTo->x
91900 4d 75 74 65 78 46 72 65 65 2c 20 26 70 46 72 6f  MutexFree, &pFro
91910 6d 2d 3e 78 4d 75 74 65 78 46 72 65 65 2c 0a 20  m->xMutexFree,. 
91920 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65              size
91930 6f 66 28 2a 70 54 6f 29 20 2d 20 6f 66 66 73 65  of(*pTo) - offse
91940 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  tof(sqlite3_mute
91950 78 5f 6d 65 74 68 6f 64 73 2c 20 78 4d 75 74 65  x_methods, xMute
91960 78 46 72 65 65 29 29 3b 0a 20 20 20 20 20 20 70  xFree));.      p
91970 54 6f 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 20  To->xMutexAlloc 
91980 3d 20 70 46 72 6f 6d 2d 3e 78 4d 75 74 65 78 41  = pFrom->xMutexA
91990 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  lloc;.    }.    
919a0 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  rc = sqlite3Glob
919b0 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
919c0 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 7d  MutexInit();.  }
919d0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
919e0 44 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69  DEBUG.  GLOBAL(i
919f0 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74 29  nt, mutexIsInit)
91a00 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20   = 1;.#endif..  
91a10 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
91a20 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74 68 65  .** Shutdown the
91a30 20 6d 75 74 65 78 20 73 79 73 74 65 6d 2e 20 54   mutex system. T
91a40 68 69 73 20 63 61 6c 6c 20 66 72 65 65 73 20 72  his call frees r
91a50 65 73 6f 75 72 63 65 73 20 61 6c 6c 6f 63 61 74  esources allocat
91a60 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
91a70 4d 75 74 65 78 49 6e 69 74 28 29 2e 0a 2a 2f 0a  MutexInit()..*/.
91a80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
91a90 6e 74 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45  nt sqlite3MutexE
91aa0 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20  nd(void){.  int 
91ab0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
91ac0 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
91ad0 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
91ae0 78 4d 75 74 65 78 45 6e 64 20 29 7b 0a 20 20 20  xMutexEnd ){.   
91af0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f   rc = sqlite3Glo
91b00 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
91b10 78 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20 20 7d  xMutexEnd();.  }
91b20 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
91b30 44 45 42 55 47 0a 20 20 47 4c 4f 42 41 4c 28 69  DEBUG.  GLOBAL(i
91b40 6e 74 2c 20 6d 75 74 65 78 49 73 49 6e 69 74 29  nt, mutexIsInit)
91b50 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20   = 0;.#endif..  
91b60 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
91b70 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70  .** Retrieve a p
91b80 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
91b90 69 63 20 6d 75 74 65 78 20 6f 72 20 61 6c 6c 6f  ic mutex or allo
91ba0 63 61 74 65 20 61 20 6e 65 77 20 64 79 6e 61 6d  cate a new dynam
91bb0 69 63 20 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ic one..*/.SQLIT
91bc0 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75  E_API sqlite3_mu
91bd0 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 6d 75 74  tex *sqlite3_mut
91be0 65 78 5f 61 6c 6c 6f 63 28 69 6e 74 20 69 64 29  ex_alloc(int id)
91bf0 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
91c00 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20  _OMIT_AUTOINIT. 
91c10 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69   if( sqlite3_ini
91c20 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75  tialize() ) retu
91c30 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72  rn 0;.#endif.  r
91c40 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f  eturn sqlite3Glo
91c50 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e  balConfig.mutex.
91c60 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29 3b  xMutexAlloc(id);
91c70 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .}..SQLITE_PRIVA
91c80 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  TE sqlite3_mutex
91c90 20 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c   *sqlite3MutexAl
91ca0 6c 6f 63 28 69 6e 74 20 69 64 29 7b 0a 20 20 69  loc(int id){.  i
91cb0 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61  f( !sqlite3Globa
91cc0 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
91cd0 65 78 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ex ){.    return
91ce0 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74   0;.  }.  assert
91cf0 28 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75  ( GLOBAL(int, mu
91d00 74 65 78 49 73 49 6e 69 74 29 20 29 3b 0a 20 20  texIsInit) );.  
91d10 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c  return sqlite3Gl
91d20 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
91d30 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69 64 29  .xMutexAlloc(id)
91d40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
91d50 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 2e  a dynamic mutex.
91d60 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
91d70 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  oid sqlite3_mute
91d80 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 6d  x_free(sqlite3_m
91d90 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20  utex *p){.  if( 
91da0 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  p ){.    sqlite3
91db0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
91dc0 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28 70 29  ex.xMutexFree(p)
91dd0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ;.  }.}../*.** O
91de0 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65 78 20  btain the mutex 
91df0 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72  p. If some other
91e00 20 74 68 72 65 61 64 20 61 6c 72 65 61 64 79 20   thread already 
91e10 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c 20 62  has the mutex, b
91e20 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20 69 74  lock.** until it
91e30 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e 65 64   can be obtained
91e40 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
91e50 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75 74  void sqlite3_mut
91e60 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
91e70 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66  _mutex *p){.  if
91e80 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
91e90 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
91ea0 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74 65 72  utex.xMutexEnter
91eb0 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
91ec0 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74  * Obtain the mut
91ed0 65 78 20 70 2e 20 49 66 20 73 75 63 63 65 73 73  ex p. If success
91ee0 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ful, return SQLI
91ef0 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65  TE_OK. Otherwise
91f00 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a 2a 20  , if another.** 
91f10 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74 68 65  thread holds the
91f20 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20 63 61   mutex and it ca
91f30 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64  nnot be obtained
91f40 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
91f50 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  BUSY..*/.SQLITE_
91f60 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
91f70 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69 74 65  mutex_try(sqlite
91f80 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69  3_mutex *p){.  i
91f90 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
91fa0 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  K;.  if( p ){.  
91fb0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
91fc0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
91fd0 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70 29 3b  ex.xMutexTry(p);
91fe0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
91ff0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
92000 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
92010 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69  ve() routine exi
92020 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74 20  ts a mutex that 
92030 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  was previously.*
92040 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74 68 65  * entered by the
92050 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54   same thread.  T
92060 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75  he behavior is u
92070 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65 20  ndefined if the 
92080 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e 6f 74  mutex .** is not
92090 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
920a0 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f  ed. If a NULL po
920b0 69 6e 74 65 72 20 69 73 20 70 61 73 73 65 64 20  inter is passed 
920c0 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 0a 2a  as an argument.*
920d0 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * this function 
920e0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53  is a no-op..*/.S
920f0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
92100 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
92110 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ve(sqlite3_mutex
92120 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
92130 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
92140 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
92150 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b 0a 20  MutexLeave(p);. 
92160 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44   }.}..#ifndef ND
92170 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  EBUG./*.** The s
92180 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
92190 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  d() and sqlite3_
921a0 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20  mutex_notheld() 
921b0 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69  routine are.** i
921c0 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20  ntended for use 
921d0 69 6e 73 69 64 65 20 61 73 73 65 72 74 28 29 20  inside assert() 
921e0 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53  statements..*/.S
921f0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
92200 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
92210 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
92220 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d  p){.  return p==
92230 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62  0 || sqlite3Glob
92240 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
92250 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a 7d 0a  MutexHeld(p);.}.
92260 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
92270 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
92280 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  held(sqlite3_mut
92290 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ex *p){.  return
922a0 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33   p==0 || sqlite3
922b0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
922c0 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65 6c 64  ex.xMutexNotheld
922d0 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  (p);.}.#endif..#
922e0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
922f0 4d 55 54 45 58 5f 4f 4d 49 54 20 2a 2f 0a 0a 2f  MUTEX_OMIT */../
92300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
92310 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20 2a 2a  nd of mutex.c **
92320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
92350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
92360 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f  egin file mutex_
92370 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  noop.c *********
92380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
923a0 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65  *.** 2008 Octobe
923b0 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 07.**.** The a
923c0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
923d0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
923e0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
923f0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
92400 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
92410 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
92420 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
92430 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
92440 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
92450 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
92460 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
92470 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
92480 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
92490 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
924a0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
924b0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
924c0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
924d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
924e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
924f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
92510 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
92520 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20   contains the C 
92530 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69  functions that i
92540 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73  mplement mutexes
92550 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70  ..**.** This imp
92560 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 20 74  lementation in t
92570 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f  his file does no
92580 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20 6d 75  t provide any mu
92590 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73 69 6f  tual.** exclusio
925a0 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20 73 75  n and is thus su
925b0 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 6f  itable for use o
925c0 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61 74 69  nly in applicati
925d0 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73 65 20  ons.** that use 
925e0 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69 6e 67  SQLite in a sing
925f0 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  le thread.  The 
92600 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e 65 64  routines defined
92610 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70 6c 61  .** here are pla
92620 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41 70 70  ce-holders.  App
92630 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 73 75  lications can su
92640 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69 6e 67  bstitute working
92650 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74 69 6e  .** mutex routin
92660 65 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65  es at start-time
92670 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a 2a 2a   using the.**.**
92680 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e       sqlite3_con
92690 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49  fig(SQLITE_CONFI
926a0 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a 2a 0a  G_MUTEX,...).**.
926b0 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a  ** interface..**
926c0 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20  .** If compiled 
926d0 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55  with SQLITE_DEBU
926e0 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69 6f 6e  G, then addition
926f0 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e 73 65  al logic is inse
92700 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64 6f 65  rted.** that doe
92710 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67  s error checking
92720 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f 20 6d   on mutexes to m
92730 61 6b 65 20 73 75 72 65 20 74 68 65 79 20 61 72  ake sure they ar
92740 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c 6c 65  e being.** calle
92750 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a  d correctly..*/.
92760 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
92770 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29  LITE_MUTEX_NOOP)
92780 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
92790 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a  ITE_DEBUG)./*.**
927a0 20 53 74 75 62 20 72 6f 75 74 69 6e 65 73 20 66   Stub routines f
927b0 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 6d 65 74  or all mutex met
927c0 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  hods..**.** This
927d0 20 72 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64   routines provid
927e0 65 20 6e 6f 20 6d 75 74 75 61 6c 20 65 78 63 6c  e no mutual excl
927f0 75 73 69 6f 6e 20 6f 72 20 65 72 72 6f 72 20 63  usion or error c
92800 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  hecking..*/.stat
92810 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78  ic int noopMutex
92820 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  Held(sqlite3_mut
92830 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 31  ex *p){ return 1
92840 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  ; }.static int n
92850 6f 6f 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  oopMutexNotheld(
92860 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
92870 29 7b 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73  ){ return 1; }.s
92880 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75  tatic int noopMu
92890 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72  texInit(void){ r
928a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
928b0 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f   }.static int no
928c0 6f 70 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29  opMutexEnd(void)
928d0 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
928e0 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 73 71 6c  OK; }.static sql
928f0 69 74 65 33 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70  ite3_mutex *noop
92900 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69  MutexAlloc(int i
92910 64 29 7b 20 72 65 74 75 72 6e 20 28 73 71 6c 69  d){ return (sqli
92920 74 65 33 5f 6d 75 74 65 78 2a 29 38 3b 20 7d 0a  te3_mutex*)8; }.
92930 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70  static void noop
92940 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74 65  MutexFree(sqlite
92950 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74  3_mutex *p){ ret
92960 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f  urn; }.static vo
92970 69 64 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65  id noopMutexEnte
92980 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  r(sqlite3_mutex 
92990 2a 70 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73  *p){ return; }.s
929a0 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75  tatic int noopMu
929b0 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d  texTry(sqlite3_m
929c0 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e  utex *p){ return
929d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74   SQLITE_OK; }.st
929e0 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75  atic void noopMu
929f0 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33  texLeave(sqlite3
92a00 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75  _mutex *p){ retu
92a10 72 6e 3b 20 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  rn; }..SQLITE_PR
92a20 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75  IVATE sqlite3_mu
92a30 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c  tex_methods *sql
92a40 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78  ite3DefaultMutex
92a50 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
92a60 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
92a70 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20  ethods sMutex = 
92a80 7b 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 49  {.    noopMutexI
92a90 6e 69 74 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74  nit,.    noopMut
92aa0 65 78 45 6e 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d  exEnd,.    noopM
92ab0 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e  utexAlloc,.    n
92ac0 6f 6f 70 4d 75 74 65 78 46 72 65 65 2c 0a 20 20  oopMutexFree,.  
92ad0 20 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72    noopMutexEnter
92ae0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 54  ,.    noopMutexT
92af0 72 79 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65  ry,.    noopMute
92b00 78 4c 65 61 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f  xLeave,..    noo
92b10 70 4d 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20  pMutexHeld,.    
92b20 6e 6f 6f 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64  noopMutexNotheld
92b30 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20  .  };..  return 
92b40 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69  &sMutex;.}.#endi
92b50 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c  f /* defined(SQL
92b60 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20  ITE_MUTEX_NOOP) 
92b70 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
92b80 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 23 69  TE_DEBUG) */..#i
92b90 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
92ba0 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20  _MUTEX_NOOP) && 
92bb0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
92bc0 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74  EBUG)./*.** In t
92bd0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
92be0 6f 6e 2c 20 65 72 72 6f 72 20 63 68 65 63 6b 69  on, error checki
92bf0 6e 67 20 69 73 20 70 72 6f 76 69 64 65 64 20 66  ng is provided f
92c00 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e  or testing.** an
92c10 64 20 64 65 62 75 67 67 69 6e 67 20 70 75 72 70  d debugging purp
92c20 6f 73 65 73 2e 20 20 54 68 65 20 6d 75 74 65 78  oses.  The mutex
92c30 65 73 20 73 74 69 6c 6c 20 64 6f 20 6e 6f 74 20  es still do not 
92c40 70 72 6f 76 69 64 65 20 61 6e 79 0a 2a 2a 20 6d  provide any.** m
92c50 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 2e  utual exclusion.
92c60 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  .*/../*.** The m
92c70 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73  utex object.*/.s
92c80 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75  truct sqlite3_mu
92c90 74 65 78 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20  tex {.  int id; 
92ca0 20 20 20 20 2f 2a 20 54 68 65 20 6d 75 74 65 78      /* The mutex
92cb0 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63   type */.  int c
92cc0 6e 74 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  nt;    /* Number
92cd0 20 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74 68   of entries with
92ce0 6f 75 74 20 61 20 6d 61 74 63 68 69 6e 67 20 6c  out a matching l
92cf0 65 61 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  eave */.};../*.*
92d00 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
92d10 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73  tex_held() and s
92d20 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
92d30 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61  held() routine a
92d40 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66  re.** intended f
92d50 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20 61 73  or use inside as
92d60 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
92d70 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
92d80 20 64 65 62 75 67 4d 75 74 65 78 48 65 6c 64 28   debugMutexHeld(
92d90 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
92da0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30  ){.  return p==0
92db0 20 7c 7c 20 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a   || p->cnt>0;.}.
92dc0 73 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67  static int debug
92dd0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c  MutexNotheld(sql
92de0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
92df0 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c    return p==0 ||
92e00 20 70 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f   p->cnt==0;.}../
92e10 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
92e20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65  and deinitialize
92e30 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
92e40 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  stem..*/.static 
92e50 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 49 6e  int debugMutexIn
92e60 69 74 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  it(void){ return
92e70 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74   SQLITE_OK; }.st
92e80 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75  atic int debugMu
92e90 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65  texEnd(void){ re
92ea0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
92eb0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
92ec0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
92ed0 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63  () routine alloc
92ee0 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75  ates a new.** mu
92ef0 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20  tex and returns 
92f00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
92f10 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73 20    If it returns 
92f20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61  NULL.** that mea
92f30 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78 20  ns that a mutex 
92f40 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c  could not be all
92f50 6f 63 61 74 65 64 2e 20 0a 2a 2f 0a 73 74 61 74  ocated. .*/.stat
92f60 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
92f70 20 2a 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f   *debugMutexAllo
92f80 63 28 69 6e 74 20 69 64 29 7b 0a 20 20 73 74 61  c(int id){.  sta
92f90 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  tic sqlite3_mute
92fa0 78 20 61 53 74 61 74 69 63 5b 36 5d 3b 0a 20 20  x aStatic[6];.  
92fb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
92fc0 4e 65 77 20 3d 20 30 3b 0a 20 20 73 77 69 74 63  New = 0;.  switc
92fd0 68 28 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73  h( id ){.    cas
92fe0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  e SQLITE_MUTEX_F
92ff0 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51  AST:.    case SQ
93000 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
93010 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 4e  SIVE: {.      pN
93020 65 77 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ew = sqlite3Mall
93030 6f 63 28 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  oc(sizeof(*pNew)
93040 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65  );.      if( pNe
93050 77 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65  w ){.        pNe
93060 77 2d 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20  w->id = id;.    
93070 20 20 20 20 70 4e 65 77 2d 3e 63 6e 74 20 3d 20      pNew->cnt = 
93080 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
93090 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
930a0 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
930b0 20 20 20 61 73 73 65 72 74 28 20 69 64 2d 32 20     assert( id-2 
930c0 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73  >= 0 );.      as
930d0 73 65 72 74 28 20 69 64 2d 32 20 3c 20 28 69 6e  sert( id-2 < (in
930e0 74 29 28 73 69 7a 65 6f 66 28 61 53 74 61 74 69  t)(sizeof(aStati
930f0 63 29 2f 73 69 7a 65 6f 66 28 61 53 74 61 74 69  c)/sizeof(aStati
93100 63 5b 30 5d 29 29 20 29 3b 0a 20 20 20 20 20 20  c[0])) );.      
93110 70 4e 65 77 20 3d 20 26 61 53 74 61 74 69 63 5b  pNew = &aStatic[
93120 69 64 2d 32 5d 3b 0a 20 20 20 20 20 20 70 4e 65  id-2];.      pNe
93130 77 2d 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20  w->id = id;.    
93140 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
93150 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
93160 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
93170 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61  routine dealloca
93180 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79  tes a previously
93190 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74 65 78   allocated mutex
931a0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
931b0 20 64 65 62 75 67 4d 75 74 65 78 46 72 65 65 28   debugMutexFree(
931c0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
931d0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
931e0 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  cnt==0 );.  asse
931f0 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
93200 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c 20  E_MUTEX_FAST || 
93210 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
93220 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b  TEX_RECURSIVE );
93230 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
93240 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  p);.}../*.** The
93250 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
93260 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74  nter() and sqlit
93270 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72  e3_mutex_try() r
93280 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a  outines attempt.
93290 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75  ** to enter a mu
932a0 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72  tex.  If another
932b0 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61   thread is alrea
932c0 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75  dy within the mu
932d0 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  tex,.** sqlite3_
932e0 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69  mutex_enter() wi
932f0 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c  ll block and sql
93300 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
93310 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20   will return.** 
93320 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68  SQLITE_BUSY.  Th
93330 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
93340 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20  try() interface 
93350 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
93360 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73  K.** upon succes
93370 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74  sful entry.  Mut
93380 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69  exes created usi
93390 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ng SQLITE_MUTEX_
933a0 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a  RECURSIVE can.**
933b0 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74   be entered mult
933c0 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68  iple times by th
933d0 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20  e same thread.  
933e0 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68  In such cases th
933f0 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74  e,.** mutex must
93400 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71   be exited an eq
93410 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  ual number of ti
93420 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68  mes before anoth
93430 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e  er thread.** can
93440 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20   enter.  If the 
93450 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65  same thread trie
93460 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f  s to enter any o
93470 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74  ther kind of mut
93480 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20  ex.** more than 
93490 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69  once, the behavi
934a0 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  or is undefined.
934b0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
934c0 64 65 62 75 67 4d 75 74 65 78 45 6e 74 65 72 28  debugMutexEnter(
934d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
934e0 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
934f0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
93500 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 64 65  _RECURSIVE || de
93510 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  bugMutexNotheld(
93520 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b  p) );.  p->cnt++
93530 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64  ;.}.static int d
93540 65 62 75 67 4d 75 74 65 78 54 72 79 28 73 71 6c  ebugMutexTry(sql
93550 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
93560 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
93570 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
93580 43 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67  CURSIVE || debug
93590 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20  MutexNotheld(p) 
935a0 29 3b 0a 20 20 70 2d 3e 63 6e 74 2b 2b 3b 0a 20  );.  p->cnt++;. 
935b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
935c0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K;.}../*.** The 
935d0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
935e0 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65 78  ave() routine ex
935f0 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61 74  its a mutex that
93600 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73   was.** previous
93610 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74 68  ly entered by th
93620 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20  e same thread.  
93630 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20  The behavior.** 
93640 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20  is undefined if 
93650 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f 74  the mutex is not
93660 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65 72   currently enter
93670 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20  ed or.** is not 
93680 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61  currently alloca
93690 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69 6c  ted.  SQLite wil
936a0 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68 65  l never do eithe
936b0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
936c0 64 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61 76  d debugMutexLeav
936d0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
936e0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 64  *p){.  assert( d
936f0 65 62 75 67 4d 75 74 65 78 48 65 6c 64 28 70 29  ebugMutexHeld(p)
93700 20 29 3b 0a 20 20 70 2d 3e 63 6e 74 2d 2d 3b 0a   );.  p->cnt--;.
93710 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
93720 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45  =SQLITE_MUTEX_RE
93730 43 55 52 53 49 56 45 20 7c 7c 20 64 65 62 75 67  CURSIVE || debug
93740 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20  MutexNotheld(p) 
93750 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  );.}..SQLITE_PRI
93760 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74  VATE sqlite3_mut
93770 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69  ex_methods *sqli
93780 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28  te3DefaultMutex(
93790 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
937a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
937b0 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b  thods sMutex = {
937c0 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 49  .    debugMutexI
937d0 6e 69 74 2c 0a 20 20 20 20 64 65 62 75 67 4d 75  nit,.    debugMu
937e0 74 65 78 45 6e 64 2c 0a 20 20 20 20 64 65 62 75  texEnd,.    debu
937f0 67 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20  gMutexAlloc,.   
93800 20 64 65 62 75 67 4d 75 74 65 78 46 72 65 65 2c   debugMutexFree,
93810 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 45  .    debugMutexE
93820 6e 74 65 72 2c 0a 20 20 20 20 64 65 62 75 67 4d  nter,.    debugM
93830 75 74 65 78 54 72 79 2c 0a 20 20 20 20 64 65 62  utexTry,.    deb
93840 75 67 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20  ugMutexLeave,.. 
93850 20 20 20 64 65 62 75 67 4d 75 74 65 78 48 65 6c     debugMutexHel
93860 64 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65  d,.    debugMute
93870 78 4e 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20  xNotheld.  };.. 
93880 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b   return &sMutex;
93890 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66  .}.#endif /* def
938a0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45  ined(SQLITE_MUTE
938b0 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65 66 69 6e  X_NOOP) && defin
938c0 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
938d0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
938e0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65  **** End of mute
938f0 78 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a  x_noop.c *******
93900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93920 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
93930 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
93940 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a  mutex_os2.c ****
93950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93970 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
93980 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20  August 28.**.** 
93990 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
939a0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
939b0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
939c0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
939d0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
939e0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
939f0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
93a00 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
93a10 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
93a20 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
93a30 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
93a40 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
93a50 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
93a60 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
93a70 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
93a80 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
93a90 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
93aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93ae0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
93af0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
93b00 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74  he C functions t
93b10 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75  hat implement mu
93b20 74 65 78 65 73 20 66 6f 72 20 4f 53 2f 32 0a 2a  texes for OS/2.*
93b30 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  /../*.** The cod
93b40 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
93b50 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 53  s only used if S
93b60 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20  QLITE_MUTEX_OS2 
93b70 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 53  is defined..** S
93b80 65 65 20 74 68 65 20 6d 75 74 65 78 2e 68 20 66  ee the mutex.h f
93b90 69 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  ile for details.
93ba0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
93bb0 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 0a 2f 2a 2a  E_MUTEX_OS2../**
93bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93bd0 2a 2a 2a 2a 20 4f 53 2f 32 20 4d 75 74 65 78 20  **** OS/2 Mutex 
93be0 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  Implementation *
93bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93c00 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
93c10 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
93c20 6f 66 20 6d 75 74 65 78 65 73 20 69 73 20 62 75  of mutexes is bu
93c30 69 6c 74 20 75 73 69 6e 67 20 74 68 65 20 4f 53  ilt using the OS
93c40 2f 32 20 41 50 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  /2 API..*/../*.*
93c50 2a 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65  * The mutex obje
93c60 63 74 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72  ct.** Each recur
93c70 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e  sive mutex is an
93c80 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
93c90 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
93ca0 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ture..*/.struct 
93cb0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a  sqlite3_mutex {.
93cc0 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 20 20 20    HMTX mutex;   
93cd0 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e      /* Mutex con
93ce0 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f 63  trolling the loc
93cf0 6b 20 2a 2f 0a 20 20 69 6e 74 20 20 69 64 3b 20  k */.  int  id; 
93d00 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65           /* Mute
93d10 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20  x type */.  int 
93d20 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 2f 2a   nRef;        /*
93d30 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   Number of refer
93d40 65 6e 63 65 73 20 2a 2f 0a 20 20 54 49 44 20 20  ences */.  TID  
93d50 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 2f 2a 20  owner;       /* 
93d60 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67 20 74  Thread holding t
93d70 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d 3b 0a  his mutex */.};.
93d80 0a 23 64 65 66 69 6e 65 20 4f 53 32 5f 4d 55 54  .#define OS2_MUT
93d90 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 20  EX_INITIALIZER  
93da0 20 30 2c 30 2c 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20   0,0,0,0../*.** 
93db0 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64  Initialize and d
93dc0 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  einitialize the 
93dd0 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 2e  mutex subsystem.
93de0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
93df0 73 32 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64  s2MutexInit(void
93e00 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ){ return SQLITE
93e10 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  _OK; }.static in
93e20 74 20 6f 73 32 4d 75 74 65 78 45 6e 64 28 76 6f  t os2MutexEnd(vo
93e30 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  id){ return SQLI
93e40 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK; }../*.** 
93e50 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
93e60 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e  x_alloc() routin
93e70 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  e allocates a ne
93e80 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72  w.** mutex and r
93e90 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
93ea0 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72   to it.  If it r
93eb0 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74  eturns NULL.** t
93ec0 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61  hat means that a
93ed0 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74   mutex could not
93ee0 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a   be allocated. .
93ef0 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75  ** SQLite will u
93f00 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20  nwind its stack 
93f10 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72  and return an er
93f20 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65  ror.  The argume
93f30 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  nt.** to sqlite3
93f40 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69  _mutex_alloc() i
93f50 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69  s one of these i
93f60 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
93f70 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
93f80 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
93f90 45 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20  EX_FAST         
93fa0 20 20 20 20 20 20 30 0a 2a 2a 20 3c 6c 69 3e 20        0.** <li> 
93fb0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
93fc0 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20 20  CURSIVE         
93fd0 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49   1.** <li>  SQLI
93fe0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
93ff0 4d 41 53 54 45 52 20 20 20 20 20 20 32 0a 2a 2a  MASTER      2.**
94000 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
94010 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 20 20  TEX_STATIC_MEM  
94020 20 20 20 20 20 20 20 33 0a 2a 2a 20 3c 6c 69 3e         3.** <li>
94030 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
94040 54 41 54 49 43 5f 50 52 4e 47 20 20 20 20 20 20  TATIC_PRNG      
94050 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a    4.** </ul>.**.
94060 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f  ** The first two
94070 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65   constants cause
94080 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
94090 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65  lloc() to create
940a0 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e  .** a new mutex.
940b0 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20    The new mutex 
940c0 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65  is recursive whe
940d0 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
940e0 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75  ECURSIVE.** is u
940f0 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65  sed but not nece
94100 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20  ssarily so when 
94110 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
94120 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68  T is used..** Th
94130 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
94140 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
94150 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64  need to make a d
94160 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65  istinction.** be
94170 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54  tween SQLITE_MUT
94180 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64  EX_RECURSIVE and
94190 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
941a0 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a  ST if it does.**
941b0 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42   not want to.  B
941c0 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  ut SQLite will o
941d0 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65  nly request a re
941e0 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e  cursive mutex in
941f0 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20  .** cases where 
94200 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20  it really needs 
94210 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65  one.  If a faste
94220 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20  r non-recursive 
94230 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65  mutex.** impleme
94240 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  ntation is avail
94250 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74  able on the host
94260 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d   platform, the m
94270 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a  utex subsystem.*
94280 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73  * might return s
94290 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72  uch a mutex in r
942a0 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54  esponse to SQLIT
942b0 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a  E_MUTEX_FAST..**
942c0 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c  .** The other al
942d0 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73  lowed parameters
942e0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
942f0 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72  x_alloc() each r
94300 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
94310 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70  er to a static p
94320 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78  reexisting mutex
94330 2e 20 20 54 68 72 65 65 20 73 74 61 74 69 63 20  .  Three static 
94340 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75  mutexes are.** u
94350 73 65 64 20 62 79 20 74 68 65 20 63 75 72 72 65  sed by the curre
94360 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51  nt version of SQ
94370 4c 69 74 65 2e 20 20 46 75 74 75 72 65 20 76 65  Lite.  Future ve
94380 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
94390 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69  .** may add addi
943a0 74 69 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75  tional static mu
943b0 74 65 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d  texes.  Static m
943c0 75 74 65 78 65 73 20 61 72 65 20 66 6f 72 20 69  utexes are for i
943d0 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62  nternal.** use b
943e0 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20  y SQLite only.  
943f0 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61  Applications tha
94400 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74  t use SQLite mut
94410 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75  exes should.** u
94420 73 65 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61  se only the dyna
94430 6d 69 63 20 6d 75 74 65 78 65 73 20 72 65 74 75  mic mutexes retu
94440 72 6e 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d  rned by SQLITE_M
94450 55 54 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20  UTEX_FAST or.** 
94460 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
94470 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  URSIVE..**.** No
94480 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f  te that if one o
94490 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  f the dynamic mu
944a0 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20 28  tex parameters (
944b0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
944c0 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d  T.** or SQLITE_M
944d0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 20  UTEX_RECURSIVE) 
944e0 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c  is used then sql
944f0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
94500 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  ().** returns a 
94510 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20  different mutex 
94520 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20  on every call.  
94530 42 75 74 20 66 6f 72 20 74 68 65 20 73 74 61 74  But for the stat
94540 69 63 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65  ic.** mutex type
94550 73 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65  s, the same mute
94560 78 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  x is returned on
94570 20 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74   every call that
94580 20 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65   has.** the same
94590 20 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f   type number..*/
945a0 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
945b0 6d 75 74 65 78 20 2a 6f 73 32 4d 75 74 65 78 41  mutex *os2MutexA
945c0 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b  lloc(int iType){
945d0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
945e0 20 2a 70 20 3d 20 4e 55 4c 4c 3b 0a 20 20 73 77   *p = NULL;.  sw
945f0 69 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20  itch( iType ){. 
94600 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
94610 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63  UTEX_FAST:.    c
94620 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
94630 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20  _RECURSIVE: {.  
94640 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d      p = sqlite3M
94650 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f  allocZero( sizeo
94660 66 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69  f(*p) );.      i
94670 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  f( p ){.        
94680 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20  p->id = iType;. 
94690 20 20 20 20 20 20 20 69 66 28 20 44 6f 73 43 72         if( DosCr
946a0 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20 30 2c  eateMutexSem( 0,
946b0 20 26 70 2d 3e 6d 75 74 65 78 2c 20 30 2c 20 46   &p->mutex, 0, F
946c0 41 4c 53 45 20 29 20 21 3d 20 4e 4f 5f 45 52 52  ALSE ) != NO_ERR
946d0 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OR ){.          
946e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20  sqlite3_free( p 
946f0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d  );.          p =
94700 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 7d   NULL;.        }
94710 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
94720 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
94730 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
94740 20 73 74 61 74 69 63 20 76 6f 6c 61 74 69 6c 65   static volatile
94750 20 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b   int isInit = 0;
94760 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 73 71  .      static sq
94770 6c 69 74 65 33 5f 6d 75 74 65 78 20 73 74 61 74  lite3_mutex stat
94780 69 63 4d 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a  icMutexes[] = {.
94790 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
947a0 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
947b0 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53   },.        { OS
947c0 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  2_MUTEX_INITIALI
947d0 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20  ZER, },.        
947e0 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54  { OS2_MUTEX_INIT
947f0 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20  IALIZER, },.    
94800 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f      { OS2_MUTEX_
94810 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a  INITIALIZER, },.
94820 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55          { OS2_MU
94830 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
94840 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53   },.        { OS
94850 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49  2_MUTEX_INITIALI
94860 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 7d 3b  ZER, },.      };
94870 0a 20 20 20 20 20 20 69 66 20 28 20 21 69 73 49  .      if ( !isI
94880 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 41  nit ){.        A
94890 50 49 52 45 54 20 72 63 3b 0a 20 20 20 20 20 20  PIRET rc;.      
948a0 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20 20    PTIB ptib;.   
948b0 20 20 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a       PPIB ppib;.
948c0 20 20 20 20 20 20 20 20 48 4d 54 58 20 6d 75 74          HMTX mut
948d0 65 78 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  ex;.        char
948e0 20 6e 61 6d 65 5b 33 32 5d 3b 0a 20 20 20 20 20   name[32];.     
948f0 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f     DosGetInfoBlo
94900 63 6b 73 28 20 26 70 74 69 62 2c 20 26 70 70 69  cks( &ptib, &ppi
94910 62 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  b );.        sql
94920 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 73  ite3_snprintf( s
94930 69 7a 65 6f 66 28 6e 61 6d 65 29 2c 20 6e 61 6d  izeof(name), nam
94940 65 2c 20 22 5c 5c 53 45 4d 33 32 5c 5c 53 51 4c  e, "\\SEM32\\SQL
94950 49 54 45 25 30 34 78 22 2c 0a 20 20 20 20 20 20  ITE%04x",.      
94960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
94970 20 20 20 20 70 70 69 62 2d 3e 70 69 62 5f 75 6c      ppib->pib_ul
94980 70 69 64 20 29 3b 0a 20 20 20 20 20 20 20 20 77  pid );.        w
94990 68 69 6c 65 28 20 21 69 73 49 6e 69 74 20 29 7b  hile( !isInit ){
949a0 0a 20 20 20 20 20 20 20 20 20 20 6d 75 74 65 78  .          mutex
949b0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
949c0 72 63 20 3d 20 44 6f 73 43 72 65 61 74 65 4d 75  rc = DosCreateMu
949d0 74 65 78 53 65 6d 28 20 6e 61 6d 65 2c 20 26 6d  texSem( name, &m
949e0 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 29 3b  utex, 0, FALSE);
949f0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
94a00 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  c == NO_ERROR ){
94a10 0a 20 20 20 20 20 20 20 20 20 20 20 20 75 6e 73  .            uns
94a20 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20  igned int i;.   
94a30 20 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73           if( !is
94a40 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Init ){.        
94a50 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d 20 30        for( i = 0
94a60 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73 74 61  ; i < sizeof(sta
94a70 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69 7a 65  ticMutexes)/size
94a80 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65 73  of(staticMutexes
94a90 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20 20 20  [0]); i++ ){.   
94aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73               Dos
94ab0 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d 28 20  CreateMutexSem( 
94ac0 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65 78 65  0, &staticMutexe
94ad0 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c 20 46  s[i].mutex, 0, F
94ae0 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20  ALSE );.        
94af0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
94b00 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31        isInit = 1
94b10 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
94b20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 43              DosC
94b30 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20 6d 75  loseMutexSem( mu
94b40 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20 20 20  tex );.         
94b50 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20 3d 3d   }else if( rc ==
94b60 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41 54 45   ERROR_DUPLICATE
94b70 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20 20 20  _NAME ){.       
94b80 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28 20 31       DosSleep( 1
94b90 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65   );.          }e
94ba0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
94bb0 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 20   return p;.     
94bc0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
94bd0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
94be0 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e  ssert( iType-2 >
94bf0 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  = 0 );.      ass
94c00 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73  ert( iType-2 < s
94c10 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65  izeof(staticMute
94c20 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74  xes)/sizeof(stat
94c30 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b  icMutexes[0]) );
94c40 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74  .      p = &stat
94c50 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d  icMutexes[iType-
94c60 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20  2];.      p->id 
94c70 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62  = iType;.      b
94c80 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
94c90 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a    return p;.}...
94ca0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
94cb0 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61  ne deallocates a
94cc0 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f   previously allo
94cd0 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a 2a 20  cated mutex..** 
94ce0 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75  SQLite is carefu
94cf0 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  l to deallocate 
94d00 65 76 65 72 79 20 6d 75 74 65 78 20 74 68 61 74  every mutex that
94d10 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a   it allocates..*
94d20 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  /.static void os
94d30 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74  2MutexFree(sqlit
94d40 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
94d50 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
94d60 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  n;.  assert( p->
94d70 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61 73 73  nRef==0 );.  ass
94d80 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
94d90 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 7c 7c  TE_MUTEX_FAST ||
94da0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
94db0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29  UTEX_RECURSIVE )
94dc0 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75 74 65  ;.  DosCloseMute
94dd0 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78 20 29  xSem( p->mutex )
94de0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
94df0 28 20 70 20 29 3b 0a 7d 0a 0a 23 69 66 64 65 66  ( p );.}..#ifdef
94e00 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a   SQLITE_DEBUG./*
94e10 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
94e20 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64  mutex_held() and
94e30 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
94e40 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65  otheld() routine
94e50 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64   are.** intended
94e60 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65 20   for use inside 
94e70 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
94e80 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  nts..*/.static i
94e90 6e 74 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 28  nt os2MutexHeld(
94ea0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
94eb0 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20  ){.  TID tid;.  
94ec0 50 49 44 20 70 69 64 3b 0a 20 20 55 4c 4f 4e 47  PID pid;.  ULONG
94ed0 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 49 42   ulCount;.  PTIB
94ee0 20 70 74 69 62 3b 0a 20 20 69 66 28 20 70 21 3d   ptib;.  if( p!=
94ef0 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65  0 ) {.    DosQue
94f00 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75  ryMutexSem(p->mu
94f10 74 65 78 2c 20 26 70 69 64 2c 20 26 74 69 64 2c  tex, &pid, &tid,
94f20 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d 20   &ulCount);.  } 
94f30 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f 73 47 65  else {.    DosGe
94f40 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69  tInfoBlocks(&pti
94f50 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 74 69  b, NULL);.    ti
94f60 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 5f 70 74  d = ptib->tib_pt
94f70 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64 3b  ib2->tib2_ultid;
94f80 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3d  .  }.  return p=
94f90 3d 30 20 7c 7c 20 28 70 2d 3e 6e 52 65 66 21 3d  =0 || (p->nRef!=
94fa0 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74  0 && p->owner==t
94fb0 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  id);.}.static in
94fc0 74 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c  t os2MutexNothel
94fd0 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  d(sqlite3_mutex 
94fe0 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a  *p){.  TID tid;.
94ff0 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c 4f    PID pid;.  ULO
95000 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 54  NG ulCount;.  PT
95010 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20 70  IB ptib;.  if( p
95020 21 3d 20 30 20 29 20 7b 0a 20 20 20 20 44 6f 73  != 0 ) {.    Dos
95030 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d  QueryMutexSem(p-
95040 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74  >mutex, &pid, &t
95050 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20  id, &ulCount);. 
95060 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f   } else {.    Do
95070 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26  sGetInfoBlocks(&
95080 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20  ptib, NULL);.   
95090 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62   tid = ptib->tib
950a0 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74  _ptib2->tib2_ult
950b0 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  id;.  }.  return
950c0 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65 66   p==0 || p->nRef
950d0 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21  ==0 || p->owner!
950e0 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  =tid;.}.#endif..
950f0 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  /*.** The sqlite
95100 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20  3_mutex_enter() 
95110 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
95120 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73  x_try() routines
95130 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65   attempt.** to e
95140 6e 74 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49  nter a mutex.  I
95150 66 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  f another thread
95160 20 69 73 20 61 6c 72 65 61 64 79 20 77 69 74 68   is already with
95170 69 6e 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a  in the mutex,.**
95180 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
95190 6e 74 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63  nter() will bloc
951a0 6b 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  k and sqlite3_mu
951b0 74 65 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72  tex_try() will r
951c0 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f  eturn.** SQLITE_
951d0 42 55 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74  BUSY.  The sqlit
951e0 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69  e3_mutex_try() i
951f0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
95200 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70   SQLITE_OK.** up
95210 6f 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e  on successful en
95220 74 72 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72  try.  Mutexes cr
95230 65 61 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49  eated using SQLI
95240 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
95250 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74  VE can.** be ent
95260 65 72 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  ered multiple ti
95270 6d 65 73 20 62 79 20 74 68 65 20 73 61 6d 65 20  mes by the same 
95280 74 68 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68  thread.  In such
95290 20 63 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d   cases the,.** m
952a0 75 74 65 78 20 6d 75 73 74 20 62 65 20 65 78 69  utex must be exi
952b0 74 65 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d  ted an equal num
952c0 62 65 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66  ber of times bef
952d0 6f 72 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65  ore another thre
952e0 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e  ad.** can enter.
952f0 20 20 49 66 20 74 68 65 20 73 61 6d 65 20 74 68    If the same th
95300 72 65 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e  read tries to en
95310 74 65 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69  ter any other ki
95320 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d  nd of mutex.** m
95330 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74  ore than once, t
95340 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20 75  he behavior is u
95350 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61  ndefined..*/.sta
95360 74 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65  tic void os2Mute
95370 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d  xEnter(sqlite3_m
95380 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20  utex *p){.  TID 
95390 74 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65  tid;.  PID holde
953a0 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64  r1;.  ULONG hold
953b0 65 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  er2;.  if( p==0 
953c0 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
953d0 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
953e0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
953f0 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78 4e 6f 74  E || os2MutexNot
95400 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 44 6f 73  held(p) );.  Dos
95410 52 65 71 75 65 73 74 4d 75 74 65 78 53 65 6d 28  RequestMutexSem(
95420 70 2d 3e 6d 75 74 65 78 2c 20 53 45 4d 5f 49 4e  p->mutex, SEM_IN
95430 44 45 46 49 4e 49 54 45 5f 57 41 49 54 29 3b 0a  DEFINITE_WAIT);.
95440 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53    DosQueryMutexS
95450 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f  em(p->mutex, &ho
95460 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f  lder1, &tid, &ho
95470 6c 64 65 72 32 29 3b 0a 20 20 70 2d 3e 6f 77 6e  lder2);.  p->own
95480 65 72 20 3d 20 74 69 64 3b 0a 20 20 70 2d 3e 6e  er = tid;.  p->n
95490 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20  Ref++;.}.static 
954a0 69 6e 74 20 6f 73 32 4d 75 74 65 78 54 72 79 28  int os2MutexTry(
954b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
954c0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54  ){.  int rc;.  T
954d0 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68 6f  ID tid;.  PID ho
954e0 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20 68  lder1;.  ULONG h
954f0 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70 3d  older2;.  if( p=
95500 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
95510 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28  TE_OK;.  assert(
95520 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
95530 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c  UTEX_RECURSIVE |
95540 7c 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c  | os2MutexNothel
95550 64 28 70 29 20 29 3b 0a 20 20 69 66 28 20 44 6f  d(p) );.  if( Do
95560 73 52 65 71 75 65 73 74 4d 75 74 65 78 53 65 6d  sRequestMutexSem
95570 28 70 2d 3e 6d 75 74 65 78 2c 20 53 45 4d 5f 49  (p->mutex, SEM_I
95580 4d 4d 45 44 49 41 54 45 5f 52 45 54 55 52 4e 29  MMEDIATE_RETURN)
95590 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 7b 0a   == NO_ERROR) {.
955a0 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65      DosQueryMute
955b0 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26  xSem(p->mutex, &
955c0 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c 20 26  holder1, &tid, &
955d0 68 6f 6c 64 65 72 32 29 3b 0a 20 20 20 20 70 2d  holder2);.    p-
955e0 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a 20 20  >owner = tid;.  
955f0 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20    p->nRef++;.   
95600 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
95610 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
95620 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
95630 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
95640 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
95650 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
95660 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65  eave() routine e
95670 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61  xits a mutex tha
95680 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75  t was.** previou
95690 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74  sly entered by t
956a0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
956b0 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a   The behavior.**
956c0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66   is undefined if
956d0 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f   the mutex is no
956e0 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65  t currently ente
956f0 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74  red or.** is not
95700 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63   currently alloc
95710 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69  ated.  SQLite wi
95720 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68  ll never do eith
95730 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
95740 69 64 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65  id os2MutexLeave
95750 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
95760 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20  p){.  TID tid;. 
95770 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20   PID holder1;.  
95780 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20  ULONG holder2;. 
95790 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
957a0 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  rn;.  assert( p-
957b0 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 44 6f 73  >nRef>0 );.  Dos
957c0 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d  QueryMutexSem(p-
957d0 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72 31  >mutex, &holder1
957e0 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72 32  , &tid, &holder2
957f0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
95800 6f 77 6e 65 72 3d 3d 74 69 64 20 29 3b 0a 20 20  owner==tid );.  
95810 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73  p->nRef--;.  ass
95820 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  ert( p->nRef==0 
95830 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  || p->id==SQLITE
95840 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
95850 20 29 3b 0a 20 20 44 6f 73 52 65 6c 65 61 73 65   );.  DosRelease
95860 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65  MutexSem(p->mute
95870 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  x);.}..SQLITE_PR
95880 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75  IVATE sqlite3_mu
95890 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c  tex_methods *sql
958a0 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78  ite3DefaultMutex
958b0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
958c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
958d0 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20  ethods sMutex = 
958e0 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 49 6e  {.    os2MutexIn
958f0 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  it,.    os2Mutex
95900 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65  End,.    os2Mute
95910 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73 32 4d  xAlloc,.    os2M
95920 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 6f 73  utexFree,.    os
95930 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20  2MutexEnter,.   
95940 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a 20 20   os2MutexTry,.  
95950 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76 65 2c    os2MutexLeave,
95960 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
95970 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75 74 65  EBUG.    os2Mute
95980 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32 4d 75  xHeld,.    os2Mu
95990 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69  texNotheld.#endi
959a0 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e  f.  };..  return
959b0 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64   &sMutex;.}.#end
959c0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
959d0 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  EX_OS2 */../****
959e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
959f0 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20 2a 2a  f mutex_os2.c **
95a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
95a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
95a40 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e 69 78   file mutex_unix
95a50 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
95a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
95a80 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a   2007 August 28.
95a90 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
95aa0 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
95ab0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
95ac0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
95ad0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
95ae0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
95af0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
95b00 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
95b10 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
95b20 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
95b30 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
95b40 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
95b50 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
95b60 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
95b70 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
95b80 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
95b90 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
95ba0 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
95bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
95bf0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
95c00 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
95c10 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
95c20 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20  ent mutexes for 
95c30 70 74 68 72 65 61 64 73 0a 2a 2f 0a 0a 2f 2a 0a  pthreads.*/../*.
95c40 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
95c50 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
95c60 20 75 73 65 64 20 69 66 20 77 65 20 61 72 65 20   used if we are 
95c70 63 6f 6d 70 69 6c 69 6e 67 20 74 68 72 65 61 64  compiling thread
95c80 73 61 66 65 0a 2a 2a 20 75 6e 64 65 72 20 75 6e  safe.** under un
95c90 69 78 20 77 69 74 68 20 70 74 68 72 65 61 64 73  ix with pthreads
95ca0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
95cb0 74 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  t this implement
95cc0 61 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 61  ation requires a
95cd0 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 74 68 72   version of pthr
95ce0 65 61 64 73 20 74 68 61 74 0a 2a 2a 20 73 75 70  eads that.** sup
95cf0 70 6f 72 74 73 20 72 65 63 75 72 73 69 76 65 20  ports recursive 
95d00 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 69 66 64  mutexes..*/.#ifd
95d10 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
95d20 50 54 48 52 45 41 44 53 0a 0a 23 69 6e 63 6c 75  PTHREADS..#inclu
95d30 64 65 20 3c 70 74 68 72 65 61 64 2e 68 3e 0a 0a  de <pthread.h>..
95d40 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65 63 75  ./*.** Each recu
95d50 72 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61  rsive mutex is a
95d60 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
95d70 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
95d80 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  cture..*/.struct
95d90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b   sqlite3_mutex {
95da0 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78  .  pthread_mutex
95db0 5f 74 20 6d 75 74 65 78 3b 20 20 20 20 20 2f 2a  _t mutex;     /*
95dc0 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69   Mutex controlli
95dd0 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20  ng the lock */. 
95de0 20 69 6e 74 20 69 64 3b 20 20 20 20 20 20 20 20   int id;        
95df0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
95e00 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69  utex type */.  i
95e10 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
95e20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
95e30 62 65 72 20 6f 66 20 65 6e 74 72 61 6e 63 65 73  ber of entrances
95e40 20 2a 2f 0a 20 20 70 74 68 72 65 61 64 5f 74 20   */.  pthread_t 
95e50 6f 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20  owner;          
95e60 20 2f 2a 20 54 68 72 65 61 64 20 74 68 61 74 20   /* Thread that 
95e70 69 73 20 77 69 74 68 69 6e 20 74 68 69 73 20 6d  is within this m
95e80 75 74 65 78 20 2a 2f 0a 23 69 66 64 65 66 20 53  utex */.#ifdef S
95e90 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 69 6e  QLITE_DEBUG.  in
95ea0 74 20 74 72 61 63 65 3b 20 20 20 20 20 20 20 20  t trace;        
95eb0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
95ec0 20 74 6f 20 74 72 61 63 65 20 63 68 61 6e 67 65   to trace change
95ed0 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 23  s */.#endif.};.#
95ee0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
95ef0 55 47 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  UG.#define SQLIT
95f00 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  E3_MUTEX_INITIAL
95f10 49 5a 45 52 20 7b 20 50 54 48 52 45 41 44 5f 4d  IZER { PTHREAD_M
95f20 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
95f30 2c 20 30 2c 20 30 2c 20 28 70 74 68 72 65 61 64  , 0, 0, (pthread
95f40 5f 74 29 30 2c 20 30 20 7d 0a 23 65 6c 73 65 0a  _t)0, 0 }.#else.
95f50 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f  #define SQLITE3_
95f60 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
95f70 52 20 7b 20 50 54 48 52 45 41 44 5f 4d 55 54 45  R { PTHREAD_MUTE
95f80 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 30  X_INITIALIZER, 0
95f90 2c 20 30 2c 20 28 70 74 68 72 65 61 64 5f 74 29  , 0, (pthread_t)
95fa0 30 20 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  0 }.#endif../*.*
95fb0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
95fc0 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73  tex_held() and s
95fd0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74  qlite3_mutex_not
95fe0 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61  held() routine a
95ff0 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66  re.** intended f
96000 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69  or use only insi
96010 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
96020 65 6d 65 6e 74 73 2e 20 20 4f 6e 20 73 6f 6d 65  ements.  On some
96030 20 70 6c 61 74 66 6f 72 6d 73 2c 0a 2a 2a 20 74   platforms,.** t
96040 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 72 61  here might be ra
96050 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74 68  ce conditions th
96060 61 74 20 63 61 6e 20 63 61 75 73 65 20 74 68 65  at can cause the
96070 73 65 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a  se routines to.*
96080 2a 20 64 65 6c 69 76 65 72 20 69 6e 63 6f 72 72  * deliver incorr
96090 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20 49 6e  ect results.  In
960a0 20 70 61 72 74 69 63 75 6c 61 72 2c 20 69 66 20   particular, if 
960b0 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 29 20  pthread_equal() 
960c0 69 73 0a 2a 2a 20 6e 6f 74 20 61 6e 20 61 74 6f  is.** not an ato
960d0 6d 69 63 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74  mic operation, t
960e0 68 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e  hen these routin
960f0 65 73 20 6d 69 67 68 74 20 64 65 6c 69 76 65 72  es might deliver
96100 79 0a 2a 2a 20 69 6e 63 6f 72 72 65 63 74 20 72  y.** incorrect r
96110 65 73 75 6c 74 73 2e 20 20 4f 6e 20 6d 6f 73 74  esults.  On most
96120 20 70 6c 61 74 66 6f 72 6d 73 2c 20 70 74 68 72   platforms, pthr
96130 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 61  ead_equal() is a
96140 20 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20   .** comparison 
96150 6f 66 20 74 77 6f 20 69 6e 74 65 67 65 72 73 20  of two integers 
96160 61 6e 64 20 69 73 20 74 68 65 72 65 66 6f 72 65  and is therefore
96170 20 61 74 6f 6d 69 63 2e 20 20 42 75 74 20 77 65   atomic.  But we
96180 20 61 72 65 0a 2a 2a 20 74 6f 6c 64 20 74 68 61   are.** told tha
96190 74 20 48 50 55 58 20 69 73 20 6e 6f 74 20 73 75  t HPUX is not su
961a0 63 68 20 61 20 70 6c 61 74 66 6f 72 6d 2e 20 20  ch a platform.  
961b0 49 66 20 73 6f 2c 20 74 68 65 6e 20 74 68 65 73  If so, then thes
961c0 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 77 69  e routines.** wi
961d0 6c 6c 20 6e 6f 74 20 61 6c 77 61 79 73 20 77 6f  ll not always wo
961e0 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20  rk correctly on 
961f0 48 50 55 58 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74  HPUX..**.** On t
96200 68 6f 73 65 20 70 6c 61 74 66 6f 72 6d 73 20 77  hose platforms w
96210 68 65 72 65 20 70 74 68 72 65 61 64 5f 65 71 75  here pthread_equ
96220 61 6c 28 29 20 69 73 20 6e 6f 74 20 61 74 6f 6d  al() is not atom
96230 69 63 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 73 68  ic, SQLite.** sh
96240 6f 75 6c 64 20 62 65 20 63 6f 6d 70 69 6c 65 64  ould be compiled
96250 20 77 69 74 68 6f 75 74 20 2d 44 53 51 4c 49 54   without -DSQLIT
96260 45 5f 44 45 42 55 47 20 61 6e 64 20 77 69 74 68  E_DEBUG and with
96270 20 2d 44 4e 44 45 42 55 47 20 74 6f 0a 2a 2a 20   -DNDEBUG to.** 
96280 6d 61 6b 65 20 73 75 72 65 20 6e 6f 20 61 73 73  make sure no ass
96290 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
962a0 20 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 61   are evaluated a
962b0 6e 64 20 68 65 6e 63 65 20 74 68 65 73 65 0a 2a  nd hence these.*
962c0 2a 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 6e  * routines are n
962d0 65 76 65 72 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  ever called..*/.
962e0 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45  #if !defined(NDE
962f0 42 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  BUG) || defined(
96300 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 74  SQLITE_DEBUG).st
96310 61 74 69 63 20 69 6e 74 20 70 74 68 72 65 61 64  atic int pthread
96320 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65  MutexHeld(sqlite
96330 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72  3_mutex *p){.  r
96340 65 74 75 72 6e 20 28 70 2d 3e 6e 52 65 66 21 3d  eturn (p->nRef!=
96350 30 20 26 26 20 70 74 68 72 65 61 64 5f 65 71 75  0 && pthread_equ
96360 61 6c 28 70 2d 3e 6f 77 6e 65 72 2c 20 70 74 68  al(p->owner, pth
96370 72 65 61 64 5f 73 65 6c 66 28 29 29 29 3b 0a 7d  read_self()));.}
96380 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72  .static int pthr
96390 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28  eadMutexNotheld(
963a0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
963b0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e  ){.  return p->n
963c0 52 65 66 3d 3d 30 20 7c 7c 20 70 74 68 72 65 61  Ref==0 || pthrea
963d0 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72  d_equal(p->owner
963e0 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  , pthread_self()
963f0 29 3d 3d 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  )==0;.}.#endif..
96400 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
96410 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a   and deinitializ
96420 65 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73  e the mutex subs
96430 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63  ystem..*/.static
96440 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65   int pthreadMute
96450 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74  xInit(void){ ret
96460 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
96470 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72  .static int pthr
96480 65 61 64 4d 75 74 65 78 45 6e 64 28 76 6f 69 64  eadMutexEnd(void
96490 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ){ return SQLITE
964a0 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK; }../*.** Th
964b0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
964c0 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65 20  alloc() routine 
964d0 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a  allocates a new.
964e0 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65 74  ** mutex and ret
964f0 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
96500 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65 74  o it.  If it ret
96510 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61  urns NULL.** tha
96520 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d  t means that a m
96530 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62  utex could not b
96540 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51  e allocated.  SQ
96550 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77  Lite.** will unw
96560 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20 61 6e  ind its stack an
96570 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  d return an erro
96580 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74  r.  The argument
96590 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  .** to sqlite3_m
965a0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20  utex_alloc() is 
965b0 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74  one of these int
965c0 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a  eger constants:.
965d0 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
965e0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
965f0 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _FAST.** <li>  S
96600 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
96610 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53  RSIVE.** <li>  S
96620 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
96630 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69  IC_MASTER.** <li
96640 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
96650 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c  STATIC_MEM.** <l
96660 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
96670 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20  _STATIC_MEM2.** 
96680 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
96690 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a  EX_STATIC_PRNG.*
966a0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
966b0 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a  UTEX_STATIC_LRU.
966c0 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f  ** <li>  SQLITE_
966d0 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
966e0 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a  2.** </ul>.**.**
966f0 20 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 63   The first two c
96700 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65 20 73  onstants cause s
96710 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
96720 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65 0a 2a  oc() to create.*
96730 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e 20 20  * a new mutex.  
96740 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20 69 73  The new mutex is
96750 20 72 65 63 75 72 73 69 76 65 20 77 68 65 6e 20   recursive when 
96760 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
96770 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75 73 65  URSIVE.** is use
96780 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65 73 73  d but not necess
96790 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20 53 51  arily so when SQ
967a0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
967b0 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20  is used..** The 
967c0 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61  mutex implementa
967d0 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65  tion does not ne
967e0 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73  ed to make a dis
967f0 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77  tinction.** betw
96800 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  een SQLITE_MUTEX
96810 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64 20 53  _RECURSIVE and S
96820 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
96830 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e   if it does.** n
96840 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42 75 74  ot want to.  But
96850 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c   SQLite will onl
96860 79 20 72 65 71 75 65 73 74 20 61 20 72 65 63 75  y request a recu
96870 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a  rsive mutex in.*
96880 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 69 74  * cases where it
96890 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e   really needs on
968a0 65 2e 20 20 49 66 20 61 20 66 61 73 74 65 72 20  e.  If a faster 
968b0 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75  non-recursive mu
968c0 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  tex.** implement
968d0 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62  ation is availab
968e0 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74 20 70  le on the host p
968f0 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74  latform, the mut
96900 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20  ex subsystem.** 
96910 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73 75 63  might return suc
96920 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72 65 73  h a mutex in res
96930 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54 45 5f  ponse to SQLITE_
96940 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a  MUTEX_FAST..**.*
96950 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f  * The other allo
96960 77 65 64 20 70 61 72 61 6d 65 74 65 72 73 20 74  wed parameters t
96970 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  o sqlite3_mutex_
96980 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72 65 74  alloc() each ret
96990 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  urn.** a pointer
969a0 20 74 6f 20 61 20 73 74 61 74 69 63 20 70 72 65   to a static pre
969b0 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78 2e 20  existing mutex. 
969c0 20 53 69 78 20 73 74 61 74 69 63 20 6d 75 74 65   Six static mute
969d0 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20  xes are.** used 
969e0 62 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  by the current v
969f0 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65  ersion of SQLite
96a00 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f  .  Future versio
96a10 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20  ns of SQLite.** 
96a20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e  may add addition
96a30 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  al static mutexe
96a40 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78  s.  Static mutex
96a50 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72  es are for inter
96a60 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51  nal.** use by SQ
96a70 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c  Lite only.  Appl
96a80 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73  ications that us
96a90 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73  e SQLite mutexes
96aa0 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f   should.** use o
96ab0 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20  nly the dynamic 
96ac0 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64  mutexes returned
96ad0 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   by SQLITE_MUTEX
96ae0 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49  _FAST or.** SQLI
96af0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
96b00 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  VE..**.** Note t
96b10 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68  hat if one of th
96b20 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20  e dynamic mutex 
96b30 70 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49  parameters (SQLI
96b40 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a  TE_MUTEX_FAST.**
96b50 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58   or SQLITE_MUTEX
96b60 5f 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75  _RECURSIVE) is u
96b70 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33  sed then sqlite3
96b80 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a  _mutex_alloc().*
96b90 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66  * returns a diff
96ba0 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65  erent mutex on e
96bb0 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20  very call.  But 
96bc0 66 6f 72 20 74 68 65 20 73 74 61 74 69 63 20 0a  for the static .
96bd0 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20  ** mutex types, 
96be0 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69  the same mutex i
96bf0 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76  s returned on ev
96c00 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61  ery call that ha
96c10 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79  s.** the same ty
96c20 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74  pe number..*/.st
96c30 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74  atic sqlite3_mut
96c40 65 78 20 2a 70 74 68 72 65 61 64 4d 75 74 65 78  ex *pthreadMutex
96c50 41 6c 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29  Alloc(int iType)
96c60 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74  {.  static sqlit
96c70 65 33 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d  e3_mutex staticM
96c80 75 74 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20  utexes[] = {.   
96c90 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49   SQLITE3_MUTEX_I
96ca0 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20  NITIALIZER,.    
96cb0 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e  SQLITE3_MUTEX_IN
96cc0 49 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53  ITIALIZER,.    S
96cd0 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49  QLITE3_MUTEX_INI
96ce0 54 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51  TIALIZER,.    SQ
96cf0 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54  LITE3_MUTEX_INIT
96d00 49 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c  IALIZER,.    SQL
96d10 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49  ITE3_MUTEX_INITI
96d20 41 4c 49 5a 45 52 2c 0a 20 20 20 20 53 51 4c 49  ALIZER,.    SQLI
96d30 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  TE3_MUTEX_INITIA
96d40 4c 49 5a 45 52 0a 20 20 7d 3b 0a 20 20 73 71 6c  LIZER.  };.  sql
96d50 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 20  ite3_mutex *p;. 
96d60 20 73 77 69 74 63 68 28 20 69 54 79 70 65 20 29   switch( iType )
96d70 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
96d80 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
96d90 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d 20 73  E: {.      p = s
96da0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
96db0 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a  ( sizeof(*p) );.
96dc0 20 20 20 20 20 20 69 66 28 20 70 20 29 7b 0a 23        if( p ){.#
96dd0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f 4d  ifdef SQLITE_HOM
96de0 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56 45  EGROWN_RECURSIVE
96df0 5f 4d 55 54 45 58 0a 20 20 20 20 20 20 20 20 2f  _MUTEX.        /
96e00 2a 20 49 66 20 72 65 63 75 72 73 69 76 65 20 6d  * If recursive m
96e10 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61  utexes are not a
96e20 76 61 69 6c 61 62 6c 65 2c 20 77 65 20 77 69 6c  vailable, we wil
96e30 6c 20 68 61 76 65 20 74 6f 0a 20 20 20 20 20 20  l have to.      
96e40 20 20 2a 2a 20 62 75 69 6c 64 20 6f 75 72 20 6f    ** build our o
96e50 77 6e 2e 20 20 53 65 65 20 62 65 6c 6f 77 2e 20  wn.  See below. 
96e60 2a 2f 0a 20 20 20 20 20 20 20 20 70 74 68 72 65  */.        pthre
96e70 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70  ad_mutex_init(&p
96e80 2d 3e 6d 75 74 65 78 2c 20 30 29 3b 0a 23 65 6c  ->mutex, 0);.#el
96e90 73 65 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73  se.        /* Us
96ea0 65 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75  e a recursive mu
96eb0 74 65 78 20 69 66 20 69 74 20 69 73 20 61 76 61  tex if it is ava
96ec0 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20  ilable */.      
96ed0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61    pthread_mutexa
96ee0 74 74 72 5f 74 20 72 65 63 75 72 73 69 76 65 41  ttr_t recursiveA
96ef0 74 74 72 3b 0a 20 20 20 20 20 20 20 20 70 74 68  ttr;.        pth
96f00 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 69  read_mutexattr_i
96f10 6e 69 74 28 26 72 65 63 75 72 73 69 76 65 41 74  nit(&recursiveAt
96f20 74 72 29 3b 0a 20 20 20 20 20 20 20 20 70 74 68  tr);.        pth
96f30 72 65 61 64 5f 6d 75 74 65 78 61 74 74 72 5f 73  read_mutexattr_s
96f40 65 74 74 79 70 65 28 26 72 65 63 75 72 73 69 76  ettype(&recursiv
96f50 65 41 74 74 72 2c 20 50 54 48 52 45 41 44 5f 4d  eAttr, PTHREAD_M
96f60 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b  UTEX_RECURSIVE);
96f70 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
96f80 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70 2d 3e  _mutex_init(&p->
96f90 6d 75 74 65 78 2c 20 26 72 65 63 75 72 73 69 76  mutex, &recursiv
96fa0 65 41 74 74 72 29 3b 0a 20 20 20 20 20 20 20 20  eAttr);.        
96fb0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 61 74 74  pthread_mutexatt
96fc0 72 5f 64 65 73 74 72 6f 79 28 26 72 65 63 75 72  r_destroy(&recur
96fd0 73 69 76 65 41 74 74 72 29 3b 0a 23 65 6e 64 69  siveAttr);.#endi
96fe0 66 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20  f.        p->id 
96ff0 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 7d  = iType;.      }
97000 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
97010 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
97020 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 20  ITE_MUTEX_FAST: 
97030 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69  {.      p = sqli
97040 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
97050 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20  izeof(*p) );.   
97060 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20     if( p ){.    
97070 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70      p->id = iTyp
97080 65 3b 0a 20 20 20 20 20 20 20 20 70 74 68 72 65  e;.        pthre
97090 61 64 5f 6d 75 74 65 78 5f 69 6e 69 74 28 26 70  ad_mutex_init(&p
970a0 2d 3e 6d 75 74 65 78 2c 20 30 29 3b 0a 20 20 20  ->mutex, 0);.   
970b0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
970c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
970d0 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ult: {.      ass
970e0 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20  ert( iType-2 >= 
970f0 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
97100 74 28 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72  t( iType-2 < Arr
97110 61 79 53 69 7a 65 28 73 74 61 74 69 63 4d 75 74  aySize(staticMut
97120 65 78 65 73 29 20 29 3b 0a 20 20 20 20 20 20 70  exes) );.      p
97130 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65 78 65   = &staticMutexe
97140 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20 20  s[iType-2];.    
97150 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b    p->id = iType;
97160 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
97170 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
97180 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68   p;.}.../*.** Th
97190 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c  is routine deall
971a0 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 6f 75  ocates a previou
971b0 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  sly.** allocated
971c0 20 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65 20   mutex.  SQLite 
971d0 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64 65  is careful to de
971e0 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a 2a  allocate every.*
971f0 2a 20 6d 75 74 65 78 20 74 68 61 74 20 69 74 20  * mutex that it 
97200 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74  allocates..*/.st
97210 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61  atic void pthrea
97220 64 4d 75 74 65 78 46 72 65 65 28 73 71 6c 69 74  dMutexFree(sqlit
97230 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
97240 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d  assert( p->nRef=
97250 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
97260 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
97270 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69  TEX_FAST || p->i
97280 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
97290 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 70  RECURSIVE );.  p
972a0 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 64 65 73  thread_mutex_des
972b0 74 72 6f 79 28 26 70 2d 3e 6d 75 74 65 78 29 3b  troy(&p->mutex);
972c0 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
972d0 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  p);.}../*.** The
972e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
972f0 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69 74  nter() and sqlit
97300 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72  e3_mutex_try() r
97310 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74 0a  outines attempt.
97320 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d 75  ** to enter a mu
97330 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65 72  tex.  If another
97340 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65 61   thread is alrea
97350 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d 75  dy within the mu
97360 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  tex,.** sqlite3_
97370 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77 69  mutex_enter() wi
97380 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c  ll block and sql
97390 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
973a0 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20   will return.** 
973b0 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68  SQLITE_BUSY.  Th
973c0 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
973d0 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65 20  try() interface 
973e0 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f  returns SQLITE_O
973f0 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65 73  K.** upon succes
97400 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74  sful entry.  Mut
97410 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73 69  exes created usi
97420 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ng SQLITE_MUTEX_
97430 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a  RECURSIVE can.**
97440 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c 74   be entered mult
97450 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74 68  iple times by th
97460 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20 20  e same thread.  
97470 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74 68  In such cases th
97480 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74  e,.** mutex must
97490 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65 71   be exited an eq
974a0 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  ual number of ti
974b0 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74 68  mes before anoth
974c0 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e  er thread.** can
974d0 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20   enter.  If the 
974e0 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69 65  same thread trie
974f0 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f  s to enter any o
97500 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74  ther kind of mut
97510 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20  ex.** more than 
97520 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76 69  once, the behavi
97530 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  or is undefined.
97540 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
97550 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65  pthreadMutexEnte
97560 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  r(sqlite3_mutex 
97570 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
97580 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
97590 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20  EX_RECURSIVE || 
975a0 70 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68  pthreadMutexNoth
975b0 65 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65  eld(p) );..#ifde
975c0 66 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f  f SQLITE_HOMEGRO
975d0 57 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54  WN_RECURSIVE_MUT
975e0 45 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72  EX.  /* If recur
975f0 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65  sive mutexes are
97600 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20   not available, 
97610 74 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20  then we have to 
97620 67 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77  grow.  ** our ow
97630 6e 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  n.  This impleme
97640 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20  ntation assumes 
97650 74 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 75  that pthread_equ
97660 61 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f  al().  ** is ato
97670 6d 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 61  mic - that it ca
97680 6e 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 64  nnot be deceived
97690 20 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73   into thinking s
976a0 65 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e  elf.  ** and p->
976b0 6f 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20  owner are equal 
976c0 69 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e  if p->owner chan
976d0 67 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20  ges between two 
976e0 76 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74  values.  ** that
976f0 20 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74   are not equal t
97700 6f 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65  o self while the
97710 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74   comparison is t
97720 61 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a  aking place..  *
97730 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  * This implement
97740 61 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d  ation also assum
97750 65 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61  es a coherent ca
97760 63 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a  che - that .  **
97770 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73   separate proces
97780 73 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20  ses cannot read 
97790 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73  different values
977a0 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20   from the same. 
977b0 20 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 74   ** address at t
977c0 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49  he same time.  I
977d0 66 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73  f either of thes
977e0 65 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73  e two conditions
977f0 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65  .  ** are not me
97800 74 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65  t, then the mute
97810 78 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e  xes will fail an
97820 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20  d problems will 
97830 72 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b  result..  */.  {
97840 0a 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 73  .    pthread_t s
97850 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65  elf = pthread_se
97860 6c 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  lf();.    if( p-
97870 3e 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 65  >nRef>0 && pthre
97880 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65  ad_equal(p->owne
97890 72 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20  r, self) ){.    
978a0 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20    p->nRef++;.   
978b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 74   }else{.      pt
978c0 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f 63 6b  hread_mutex_lock
978d0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  (&p->mutex);.   
978e0 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52     assert( p->nR
978f0 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  ef==0 );.      p
97900 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c 66 3b 0a  ->owner = self;.
97910 20 20 20 20 20 20 70 2d 3e 6e 52 65 66 20 3d 20        p->nRef = 
97920 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c  1;.    }.  }.#el
97930 73 65 0a 20 20 2f 2a 20 55 73 65 20 74 68 65 20  se.  /* Use the 
97940 62 75 69 6c 74 2d 69 6e 20 72 65 63 75 72 73 69  built-in recursi
97950 76 65 20 6d 75 74 65 78 65 73 20 69 66 20 74 68  ve mutexes if th
97960 65 79 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  ey are available
97970 2e 0a 20 20 2a 2f 0a 20 20 70 74 68 72 65 61 64  ..  */.  pthread
97980 5f 6d 75 74 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e  _mutex_lock(&p->
97990 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e  mutex);.  p->own
979a0 65 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c  er = pthread_sel
979b0 66 28 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b  f();.  p->nRef++
979c0 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ;.#endif..#ifdef
979d0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
979e0 69 66 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a  if( p->trace ){.
979f0 20 20 20 20 70 72 69 6e 74 66 28 22 65 6e 74 65      printf("ente
97a00 72 20 6d 75 74 65 78 20 25 70 20 28 25 64 29 20  r mutex %p (%d) 
97a10 77 69 74 68 20 6e 52 65 66 3d 25 64 5c 6e 22 2c  with nRef=%d\n",
97a20 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c 20 70 2d   p, p->trace, p-
97a30 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23 65 6e 64  >nRef);.  }.#end
97a40 69 66 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  if.}.static int 
97a50 70 74 68 72 65 61 64 4d 75 74 65 78 54 72 79 28  pthreadMutexTry(
97a60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
97a70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  ){.  int rc;.  a
97a80 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
97a90 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
97aa0 53 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d  SIVE || pthreadM
97ab0 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29  utexNotheld(p) )
97ac0 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
97ad0 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52  _HOMEGROWN_RECUR
97ae0 53 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20  SIVE_MUTEX.  /* 
97af0 49 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  If recursive mut
97b00 65 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61  exes are not ava
97b10 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20  ilable, then we 
97b20 68 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a  have to grow.  *
97b30 2a 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73  * our own.  This
97b40 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
97b50 61 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68  assumes that pth
97b60 72 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a  read_equal().  *
97b70 2a 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68  * is atomic - th
97b80 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20  at it cannot be 
97b90 64 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68  deceived into th
97ba0 69 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a  inking self.  **
97bb0 20 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72   and p->owner ar
97bc0 65 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77  e equal if p->ow
97bd0 6e 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77  ner changes betw
97be0 65 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20  een two values. 
97bf0 20 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74   ** that are not
97c00 20 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77   equal to self w
97c10 68 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69  hile the compari
97c20 73 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c  son is taking pl
97c30 61 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69  ace..  ** This i
97c40 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c  mplementation al
97c50 73 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68  so assumes a coh
97c60 65 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68  erent cache - th
97c70 61 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74  at .  ** separat
97c80 65 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e  e processes cann
97c90 6f 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e  ot read differen
97ca0 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68  t values from th
97cb0 65 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72  e same.  ** addr
97cc0 65 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20  ess at the same 
97cd0 74 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72  time.  If either
97ce0 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f   of these two co
97cf0 6e 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72  nditions.  ** ar
97d00 65 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20  e not met, then 
97d10 74 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c  the mutexes will
97d20 20 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65   fail and proble
97d30 6d 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a  ms will result..
97d40 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68    */.  {.    pth
97d50 72 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74  read_t self = pt
97d60 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20  hread_self();.  
97d70 20 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20    if( p->nRef>0 
97d80 26 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  && pthread_equal
97d90 28 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29  (p->owner, self)
97da0 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65   ){.      p->nRe
97db0 66 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  f++;.      rc = 
97dc0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
97dd0 65 6c 73 65 20 69 66 28 20 70 74 68 72 65 61 64  else if( pthread
97de0 5f 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26  _mutex_trylock(&
97df0 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a  p->mutex)==0 ){.
97e00 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
97e10 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 20 20  >nRef==0 );.    
97e20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 73 65 6c    p->owner = sel
97e30 66 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66  f;.      p->nRef
97e40 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 1;.      rc =
97e50 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
97e60 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20  }else{.      rc 
97e70 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
97e80 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20     }.  }.#else. 
97e90 20 2f 2a 20 55 73 65 20 74 68 65 20 62 75 69 6c   /* Use the buil
97ea0 74 2d 69 6e 20 72 65 63 75 72 73 69 76 65 20 6d  t-in recursive m
97eb0 75 74 65 78 65 73 20 69 66 20 74 68 65 79 20 61  utexes if they a
97ec0 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20  re available..  
97ed0 2a 2f 0a 20 20 69 66 28 20 70 74 68 72 65 61 64  */.  if( pthread
97ee0 5f 6d 75 74 65 78 5f 74 72 79 6c 6f 63 6b 28 26  _mutex_trylock(&
97ef0 70 2d 3e 6d 75 74 65 78 29 3d 3d 30 20 29 7b 0a  p->mutex)==0 ){.
97f00 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70      p->owner = p
97f10 74 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20  thread_self();. 
97f20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20     p->nRef++;.  
97f30 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
97f40 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
97f50 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
97f60 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  .  }.#endif..#if
97f70 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
97f80 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
97f90 45 5f 4f 4b 20 26 26 20 70 2d 3e 74 72 61 63 65  E_OK && p->trace
97fa0 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22   ){.    printf("
97fb0 65 6e 74 65 72 20 6d 75 74 65 78 20 25 70 20 28  enter mutex %p (
97fc0 25 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64  %d) with nRef=%d
97fd0 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65  \n", p, p->trace
97fe0 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a  , p->nRef);.  }.
97ff0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
98000 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
98010 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
98020 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65  eave() routine e
98030 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61  xits a mutex tha
98040 74 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75  t was.** previou
98050 73 6c 79 20 65 6e 74 65 72 65 64 20 62 79 20 74  sly entered by t
98060 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
98070 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a   The behavior.**
98080 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69 66   is undefined if
98090 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e 6f   the mutex is no
980a0 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65  t currently ente
980b0 72 65 64 20 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74  red or.** is not
980c0 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63   currently alloc
980d0 61 74 65 64 2e 20 20 53 51 4c 69 74 65 20 77 69  ated.  SQLite wi
980e0 6c 6c 20 6e 65 76 65 72 20 64 6f 20 65 69 74 68  ll never do eith
980f0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
98100 69 64 20 70 74 68 72 65 61 64 4d 75 74 65 78 4c  id pthreadMutexL
98110 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
98120 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
98130 28 20 70 74 68 72 65 61 64 4d 75 74 65 78 48 65  ( pthreadMutexHe
98140 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 6e 52  ld(p) );.  p->nR
98150 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20  ef--;.  assert( 
98160 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70 2d  p->nRef==0 || p-
98170 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
98180 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 0a  X_RECURSIVE );..
98190 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 4f  #ifdef SQLITE_HO
981a0 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53 49 56  MEGROWN_RECURSIV
981b0 45 5f 4d 55 54 45 58 0a 20 20 69 66 28 20 70 2d  E_MUTEX.  if( p-
981c0 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
981d0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e  pthread_mutex_un
981e0 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b  lock(&p->mutex);
981f0 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 70 74 68  .  }.#else.  pth
98200 72 65 61 64 5f 6d 75 74 65 78 5f 75 6e 6c 6f 63  read_mutex_unloc
98210 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 23 65  k(&p->mutex);.#e
98220 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
98230 49 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20  ITE_DEBUG.  if( 
98240 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20  p->trace ){.    
98250 70 72 69 6e 74 66 28 22 6c 65 61 76 65 20 6d 75  printf("leave mu
98260 74 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68  tex %p (%d) with
98270 20 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20   nRef=%d\n", p, 
98280 70 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65  p->trace, p->nRe
98290 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  f);.  }.#endif.}
982a0 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
982b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
982c0 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44  ethods *sqlite3D
982d0 65 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64  efaultMutex(void
982e0 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
982f0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
98300 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20  s sMutex = {.   
98310 20 70 74 68 72 65 61 64 4d 75 74 65 78 49 6e 69   pthreadMutexIni
98320 74 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  t,.    pthreadMu
98330 74 65 78 45 6e 64 2c 0a 20 20 20 20 70 74 68 72  texEnd,.    pthr
98340 65 61 64 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 20  eadMutexAlloc,. 
98350 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 46     pthreadMutexF
98360 72 65 65 2c 0a 20 20 20 20 70 74 68 72 65 61 64  ree,.    pthread
98370 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20  MutexEnter,.    
98380 70 74 68 72 65 61 64 4d 75 74 65 78 54 72 79 2c  pthreadMutexTry,
98390 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74 65  .    pthreadMute
983a0 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20 53  xLeave,.#ifdef S
983b0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
983c0 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64  pthreadMutexHeld
983d0 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75 74  ,.    pthreadMut
983e0 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6c 73 65 0a  exNotheld.#else.
983f0 20 20 20 20 30 2c 0a 20 20 20 20 30 0a 23 65 6e      0,.    0.#en
98400 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75  dif.  };..  retu
98410 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 0a 23  rn &sMutex;.}..#
98420 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
98430 4d 55 54 45 58 5f 50 54 48 52 45 41 44 20 2a 2f  MUTEX_PTHREAD */
98440 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
98450 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 75  * End of mutex_u
98460 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nix.c **********
98470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98490 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
984a0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74  * Begin file mut
984b0 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ex_w32.c *******
984c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
984d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
984e0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67  /./*.** 2007 Aug
984f0 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 14.**.** The
98500 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
98510 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
98520 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
98530 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
98540 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
98550 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
98560 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
98570 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
98580 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
98590 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
985a0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
985b0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
985c0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
985d0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
985e0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
985f0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
98600 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
98610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98650 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
98660 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
98670 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
98680 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
98690 65 73 20 66 6f 72 20 77 69 6e 33 32 0a 2a 2f 0a  es for win32.*/.
986a0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  ./*.** The code 
986b0 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20  in this file is 
986c0 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 77 65 20  only used if we 
986d0 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 20 6d 75  are compiling mu
986e0 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a 20 6f  ltithreaded.** o
986f0 6e 20 61 20 77 69 6e 33 32 20 73 79 73 74 65 6d  n a win32 system
98700 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
98710 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 0a 2f 2a  TE_MUTEX_W32../*
98720 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72 73 69  .** Each recursi
98730 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e 20 69  ve mutex is an i
98740 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
98750 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
98760 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  re..*/.struct sq
98770 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20  lite3_mutex {.  
98780 43 52 49 54 49 43 41 4c 5f 53 45 43 54 49 4f 4e  CRITICAL_SECTION
98790 20 6d 75 74 65 78 3b 20 20 20 20 2f 2a 20 4d 75   mutex;    /* Mu
987a0 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e 67 20  tex controlling 
987b0 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  the lock */.  in
987c0 74 20 69 64 3b 20 20 20 20 20 20 20 20 20 20 20  t id;           
987d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74 65           /* Mute
987e0 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20  x type */.  int 
987f0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
98800 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
98810 20 6f 66 20 65 6e 74 65 72 61 6e 63 65 73 20 2a   of enterances *
98820 2f 0a 20 20 44 57 4f 52 44 20 6f 77 6e 65 72 3b  /.  DWORD owner;
98830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
98840 2a 20 54 68 72 65 61 64 20 68 6f 6c 64 69 6e 67  * Thread holding
98850 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 7d   this mutex */.}
98860 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  ;../*.** Return 
98870 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20  true (non-zero) 
98880 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e  if we are runnin
98890 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c 20 57  g under WinNT, W
988a0 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a 2a 20  in2K, WinXP,.** 
988b0 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74 75 72  or WinCE.  Retur
988c0 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29 20 66  n false (zero) f
988d0 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39 38 2c  or Win95, Win98,
988e0 20 6f 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a 2a 2a   or WinME..**.**
988f0 20 48 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65   Here is an inte
98900 72 65 73 74 69 6e 67 20 6f 62 73 65 72 76 61 74  resting observat
98910 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20 57 69 6e  ion:  Win95, Win
98920 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45 20 6c 61  98, and WinME la
98930 63 6b 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69  ck.** the LockFi
98940 6c 65 45 78 28 29 20 41 50 49 2e 20 20 42 75 74  leEx() API.  But
98950 20 77 65 20 63 61 6e 20 73 74 69 6c 6c 20 73 74   we can still st
98960 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20 61 67  atically link ag
98970 61 69 6e 73 74 20 74 68 61 74 0a 2a 2a 20 41 50  ainst that.** AP
98980 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20  I as long as we 
98990 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20 77 69  don't call it wi
989a0 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39 35 2f  n running Win95/
989b0 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c 20 74  98/ME.  A call t
989c0 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  o.** this routin
989d0 65 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74  e is used to det
989e0 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 68 6f  ermine if the ho
989f0 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38 2f 4d  st is Win95/98/M
98a00 45 20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f 32 4b  E or.** WinNT/2K
98a10 2f 58 50 20 73 6f 20 74 68 61 74 20 77 65 20 77  /XP so that we w
98a20 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74 68 65 72  ill know whether
98a30 20 6f 72 20 6e 6f 74 20 77 65 20 63 61 6e 20 73   or not we can s
98a40 61 66 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20 74 68  afely call.** th
98a50 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41  e LockFileEx() A
98a60 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d 75 74 65 78 49  PI..**.** mutexI
98a70 73 4e 54 28 29 20 69 73 20 6f 6e 6c 79 20 75 73  sNT() is only us
98a80 65 64 20 66 6f 72 20 74 68 65 20 54 72 79 45 6e  ed for the TryEn
98a90 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
98aa0 6f 6e 28 29 20 41 50 49 20 63 61 6c 6c 2c 0a 2a  on() API call,.*
98ab0 2a 20 77 68 69 63 68 20 69 73 20 6f 6e 6c 79 20  * which is only 
98ac0 61 76 61 69 6c 61 62 6c 65 20 69 66 20 79 6f 75  available if you
98ad0 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 77 61  r application wa
98ae0 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  s compiled with 
98af0 0a 2a 2a 20 5f 57 49 4e 33 32 5f 57 49 4e 4e 54  .** _WIN32_WINNT
98b00 20 64 65 66 69 6e 65 64 20 74 6f 20 61 20 76 61   defined to a va
98b10 6c 75 65 20 3e 3d 20 30 78 30 34 30 30 2e 20 20  lue >= 0x0400.  
98b20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f  Currently, the o
98b30 6e 6c 79 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 54  nly.** call to T
98b40 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53  ryEnterCriticalS
98b50 65 63 74 69 6f 6e 28 29 20 69 73 20 23 69 66 64  ection() is #ifd
98b60 65 66 27 65 64 20 6f 75 74 2c 20 73 6f 20 23 69  ef'ed out, so #i
98b70 66 64 65 66 20 0a 2a 2a 20 74 68 69 73 20 6f 75  fdef .** this ou
98b80 74 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a 23 69  t as well..*/.#i
98b90 66 20 30 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  f 0.#if SQLITE_O
98ba0 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65  S_WINCE.# define
98bb0 20 6d 75 74 65 78 49 73 4e 54 28 29 20 20 28 31   mutexIsNT()  (1
98bc0 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74 69 63  ).#else.  static
98bd0 20 69 6e 74 20 6d 75 74 65 78 49 73 4e 54 28 76   int mutexIsNT(v
98be0 6f 69 64 29 7b 0a 20 20 20 20 73 74 61 74 69 63  oid){.    static
98bf0 20 69 6e 74 20 6f 73 54 79 70 65 20 3d 20 30 3b   int osType = 0;
98c00 0a 20 20 20 20 69 66 28 20 6f 73 54 79 70 65 3d  .    if( osType=
98c10 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45  =0 ){.      OSVE
98c20 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b  RSIONINFO sInfo;
98c30 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f  .      sInfo.dwO
98c40 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65  SVersionInfoSize
98c50 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29   = sizeof(sInfo)
98c60 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69  ;.      GetVersi
98c70 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20  onEx(&sInfo);.  
98c80 20 20 20 20 6f 73 54 79 70 65 20 3d 20 73 49 6e      osType = sIn
98c90 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49 64 3d  fo.dwPlatformId=
98ca0 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f 57 49  =VER_PLATFORM_WI
98cb0 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31 3b 0a  N32_NT ? 2 : 1;.
98cc0 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
98cd0 20 6f 73 54 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a   osType==2;.  }.
98ce0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
98cf0 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 23 65 6e  _OS_WINCE */.#en
98d00 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
98d10 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54  TE_DEBUG./*.** T
98d20 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
98d30 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69  _held() and sqli
98d40 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
98d50 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a  d() routine are.
98d60 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20  ** intended for 
98d70 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64 65 20  use only inside 
98d80 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
98d90 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  nts..*/.static i
98da0 6e 74 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 28  nt winMutexHeld(
98db0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
98dc0 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e  ){.  return p->n
98dd0 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f 77 6e  Ref!=0 && p->own
98de0 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68  er==GetCurrentTh
98df0 72 65 61 64 49 64 28 29 3b 0a 7d 0a 73 74 61 74  readId();.}.stat
98e00 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 4e  ic int winMutexN
98e10 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  otheld(sqlite3_m
98e20 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
98e30 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c  rn p->nRef==0 ||
98e40 20 70 2d 3e 6f 77 6e 65 72 21 3d 47 65 74 43 75   p->owner!=GetCu
98e50 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b  rrentThreadId();
98e60 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
98e70 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64  * Initialize and
98e80 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68   deinitialize th
98e90 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
98ea0 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c  m..*/.static sql
98eb0 69 74 65 33 5f 6d 75 74 65 78 20 77 69 6e 4d 75  ite3_mutex winMu
98ec0 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65  tex_staticMutexe
98ed0 73 5b 36 5d 3b 0a 73 74 61 74 69 63 20 69 6e 74  s[6];.static int
98ee0 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74   winMutex_isInit
98ef0 20 3d 20 30 3b 0a 2f 2a 20 41 73 20 77 69 6e 4d   = 0;./* As winM
98f00 75 74 65 78 49 6e 69 74 28 29 20 61 6e 64 20 77  utexInit() and w
98f10 69 6e 4d 75 74 65 78 45 6e 64 28 29 20 61 72 65  inMutexEnd() are
98f20 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72 74 0a   called as part.
98f30 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ** of the sqlite
98f40 33 5f 69 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64  3_initialize and
98f50 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
98f60 6e 28 29 0a 2a 2a 20 70 72 6f 63 65 73 73 69 6e  n().** processin
98f70 67 2c 20 74 68 65 20 22 69 6e 74 65 72 6c 6f 63  g, the "interloc
98f80 6b 65 64 22 20 6d 61 67 69 63 20 69 73 20 70 72  ked" magic is pr
98f90 6f 62 61 62 6c 79 20 6e 6f 74 0a 2a 2a 20 73 74  obably not.** st
98fa0 72 69 63 74 6c 79 20 6e 65 63 65 73 73 61 72 79  rictly necessary
98fb0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 6c 6f 6e 67  ..*/.static long
98fc0 20 77 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 20 3d   winMutex_lock =
98fd0 20 30 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20   0;..static int 
98fe0 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 76 6f 69  winMutexInit(voi
98ff0 64 29 7b 20 0a 20 20 2f 2a 20 54 68 65 20 66 69  d){ .  /* The fi
99000 72 73 74 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74  rst to increment
99010 20 74 6f 20 31 20 64 6f 65 73 20 61 63 74 75 61   to 1 does actua
99020 6c 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  l initialization
99030 20 2a 2f 0a 20 20 69 66 28 20 49 6e 74 65 72 6c   */.  if( Interl
99040 6f 63 6b 65 64 43 6f 6d 70 61 72 65 45 78 63 68  ockedCompareExch
99050 61 6e 67 65 28 26 77 69 6e 4d 75 74 65 78 5f 6c  ange(&winMutex_l
99060 6f 63 6b 2c 20 31 2c 20 30 29 3d 3d 30 20 29 7b  ock, 1, 0)==0 ){
99070 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
99080 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
99090 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74  Size(winMutex_st
990a0 61 74 69 63 4d 75 74 65 78 65 73 29 3b 20 69 2b  aticMutexes); i+
990b0 2b 29 7b 0a 20 20 20 20 20 20 49 6e 69 74 69 61  +){.      Initia
990c0 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65 63 74  lizeCriticalSect
990d0 69 6f 6e 28 26 77 69 6e 4d 75 74 65 78 5f 73 74  ion(&winMutex_st
990e0 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d  aticMutexes[i].m
990f0 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20  utex);.    }.   
99100 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74   winMutex_isInit
99110 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 1;.  }else{. 
99120 20 20 20 2f 2a 20 53 6f 6d 65 6f 6e 65 20 65 6c     /* Someone el
99130 73 65 20 69 73 20 69 6e 20 74 68 65 20 70 72 6f  se is in the pro
99140 63 65 73 73 20 6f 66 20 69 6e 69 74 69 6e 67 20  cess of initing 
99150 74 68 65 20 73 74 61 74 69 63 20 6d 75 74 65 78  the static mutex
99160 65 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  es */.    while(
99170 20 21 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69   !winMutex_isIni
99180 74 20 29 7b 0a 20 20 20 20 20 20 53 6c 65 65 70  t ){.      Sleep
99190 28 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  (1);.    }.  }. 
991a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
991b0 4b 3b 20 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  K; .}..static in
991c0 74 20 77 69 6e 4d 75 74 65 78 45 6e 64 28 76 6f  t winMutexEnd(vo
991d0 69 64 29 7b 20 0a 20 20 2f 2a 20 54 68 65 20 66  id){ .  /* The f
991e0 69 72 73 74 20 74 6f 20 64 65 63 72 65 6d 65 6e  irst to decremen
991f0 74 20 74 6f 20 30 20 64 6f 65 73 20 61 63 74 75  t to 0 does actu
99200 61 6c 20 73 68 75 74 64 6f 77 6e 20 0a 20 20 2a  al shutdown .  *
99210 2a 20 28 77 68 69 63 68 20 73 68 6f 75 6c 64 20  * (which should 
99220 62 65 20 74 68 65 20 6c 61 73 74 20 74 6f 20 73  be the last to s
99230 68 75 74 64 6f 77 6e 2e 29 20 2a 2f 0a 20 20 69  hutdown.) */.  i
99240 66 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 43 6f  f( InterlockedCo
99250 6d 70 61 72 65 45 78 63 68 61 6e 67 65 28 26 77  mpareExchange(&w
99260 69 6e 4d 75 74 65 78 5f 6c 6f 63 6b 2c 20 30 2c  inMutex_lock, 0,
99270 20 31 29 3d 3d 31 20 29 7b 0a 20 20 20 20 69 66   1)==1 ){.    if
99280 28 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69  ( winMutex_isIni
99290 74 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 69 6e  t==1 ){.      in
992a0 74 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  t i;.      for(i
992b0 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
992c0 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d  winMutex_staticM
992d0 75 74 65 78 65 73 29 3b 20 69 2b 2b 29 7b 0a 20  utexes); i++){. 
992e0 20 20 20 20 20 20 20 44 65 6c 65 74 65 43 72 69         DeleteCri
992f0 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 77 69  ticalSection(&wi
99300 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74  nMutex_staticMut
99310 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a  exes[i].mutex);.
99320 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 69        }.      wi
99330 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 20 3d 20  nMutex_isInit = 
99340 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  0;.    }.  }.  r
99350 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
99360 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73   .}../*.** The s
99370 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
99380 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c  oc() routine all
99390 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20  ocates a new.** 
993a0 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e  mutex and return
993b0 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
993c0 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e  t.  If it return
993d0 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d  s NULL.** that m
993e0 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65  eans that a mute
993f0 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61  x could not be a
99400 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
99410 65 0a 2a 2a 20 77 69 6c 6c 20 75 6e 77 69 6e 64  e.** will unwind
99420 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72   its stack and r
99430 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20  eturn an error. 
99440 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   The argument.**
99450 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
99460 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65  x_alloc() is one
99470 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65   of these intege
99480 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a  r constants:.**.
99490 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
994a0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
994b0 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  ST.** <li>  SQLI
994c0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
994d0 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49  VE.** <li>  SQLI
994e0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
994f0 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20 20  MASTER.** <li>  
99500 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
99510 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e 20  TIC_MEM.** <li> 
99520 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
99530 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c 69  ATIC_MEM2.** <li
99540 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
99550 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20 3c  STATIC_PRNG.** <
99560 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
99570 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a 20  X_STATIC_LRU.** 
99580 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
99590 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a  EX_STATIC_LRU2.*
995a0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68  * </ul>.**.** Th
995b0 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73  e first two cons
995c0 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69  tants cause sqli
995d0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
995e0 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61  ) to create.** a
995f0 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65   new mutex.  The
99600 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65   new mutex is re
99610 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c  cursive when SQL
99620 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
99630 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62  IVE.** is used b
99640 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  ut not necessari
99650 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54  ly so when SQLIT
99660 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20  E_MUTEX_FAST is 
99670 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74  used..** The mut
99680 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
99690 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
996a0 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e  to make a distin
996b0 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e  ction.** between
996c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
996d0 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49  CURSIVE and SQLI
996e0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66  TE_MUTEX_FAST if
996f0 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20   it does.** not 
99700 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51  want to.  But SQ
99710 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72  Lite will only r
99720 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69  equest a recursi
99730 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63  ve mutex in.** c
99740 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65  ases where it re
99750 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20  ally needs one. 
99760 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e   If a faster non
99770 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78  -recursive mutex
99780 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
99790 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  on is available 
997a0 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74  on the host plat
997b0 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20  form, the mutex 
997c0 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67  subsystem.** mig
997d0 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61  ht return such a
997e0 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e   mutex in respon
997f0 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54  se to SQLITE_MUT
99800 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54  EX_FAST..**.** T
99810 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64  he other allowed
99820 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73   parameters to s
99830 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
99840 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e  oc() each return
99850 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  .** a pointer to
99860 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69   a static preexi
99870 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 53 69  sting mutex.  Si
99880 78 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73  x static mutexes
99890 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20   are.** used by 
998a0 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73  the current vers
998b0 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20  ion of SQLite.  
998c0 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  Future versions 
998d0 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79  of SQLite.** may
998e0 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   add additional 
998f0 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20  static mutexes. 
99900 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   Static mutexes 
99910 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c  are for internal
99920 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74  .** use by SQLit
99930 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61  e only.  Applica
99940 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53  tions that use S
99950 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68  QLite mutexes sh
99960 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79  ould.** use only
99970 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74   the dynamic mut
99980 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79  exes returned by
99990 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
999a0 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f  ST or.** SQLITE_
999b0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e  MUTEX_RECURSIVE.
999c0 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
999d0 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 64   if one of the d
999e0 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61 72  ynamic mutex par
999f0 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45 5f  ameters (SQLITE_
99a00 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f 72  MUTEX_FAST.** or
99a10 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
99a20 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65 64  CURSIVE) is used
99a30 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d 75   then sqlite3_mu
99a40 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20 72  tex_alloc().** r
99a50 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72 65  eturns a differe
99a60 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72  nt mutex on ever
99a70 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f 72  y call.  But for
99a80 20 74 68 65 20 73 74 61 74 69 63 20 0a 2a 2a 20   the static .** 
99a90 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
99aa0 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
99ab0 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
99ac0 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
99ad0 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
99ae0 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
99af0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
99b00 2a 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 28 69  *winMutexAlloc(i
99b10 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c  nt iType){.  sql
99b20 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 3b 0a 0a  ite3_mutex *p;..
99b30 20 20 73 77 69 74 63 68 28 20 69 54 79 70 65 20    switch( iType 
99b40 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
99b50 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 3a 0a 20  TE_MUTEX_FAST:. 
99b60 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
99b70 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 3a 20  UTEX_RECURSIVE: 
99b80 7b 0a 20 20 20 20 20 20 70 20 3d 20 73 71 6c 69  {.      p = sqli
99b90 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
99ba0 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 20  izeof(*p) );.   
99bb0 20 20 20 69 66 28 20 70 20 29 7b 20 20 0a 20 20     if( p ){  .  
99bc0 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54        p->id = iT
99bd0 79 70 65 3b 0a 20 20 20 20 20 20 20 20 49 6e 69  ype;.        Ini
99be0 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53  tializeCriticalS
99bf0 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78  ection(&p->mutex
99c00 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
99c10 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
99c20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
99c30 20 20 20 61 73 73 65 72 74 28 20 77 69 6e 4d 75     assert( winMu
99c40 74 65 78 5f 69 73 49 6e 69 74 3d 3d 31 20 29 3b  tex_isInit==1 );
99c50 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
99c60 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20  Type-2 >= 0 );. 
99c70 20 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79       assert( iTy
99c80 70 65 2d 32 20 3c 20 41 72 72 61 79 53 69 7a 65  pe-2 < ArraySize
99c90 28 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63  (winMutex_static
99ca0 4d 75 74 65 78 65 73 29 20 29 3b 0a 20 20 20 20  Mutexes) );.    
99cb0 20 20 70 20 3d 20 26 77 69 6e 4d 75 74 65 78 5f    p = &winMutex_
99cc0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54  staticMutexes[iT
99cd0 79 70 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d  ype-2];.      p-
99ce0 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20  >id = iType;.   
99cf0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
99d00 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
99d10 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  }.../*.** This r
99d20 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74  outine deallocat
99d30 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79 0a  es a previously.
99d40 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 6d 75 74  ** allocated mut
99d50 65 78 2e 20 20 53 51 4c 69 74 65 20 69 73 20 63  ex.  SQLite is c
99d60 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f  areful to deallo
99d70 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 6d 75  cate every.** mu
99d80 74 65 78 20 74 68 61 74 20 69 74 20 61 6c 6c 6f  tex that it allo
99d90 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  cates..*/.static
99da0 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 46 72   void winMutexFr
99db0 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ee(sqlite3_mutex
99dc0 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
99dd0 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p );.  assert( p
99de0 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61  ->nRef==0 );.  a
99df0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
99e00 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
99e10 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  || p->id==SQLITE
99e20 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
99e30 20 29 3b 0a 20 20 44 65 6c 65 74 65 43 72 69 74   );.  DeleteCrit
99e40 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e  icalSection(&p->
99e50 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
99e60 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a  3_free(p);.}../*
99e70 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
99e80 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e  mutex_enter() an
99e90 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
99ea0 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61  try() routines a
99eb0 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74  ttempt.** to ent
99ec0 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20  er a mutex.  If 
99ed0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69  another thread i
99ee0 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  s already within
99ef0 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73   the mutex,.** s
99f00 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
99f10 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20  er() will block 
99f20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
99f30 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74  x_try() will ret
99f40 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  urn.** SQLITE_BU
99f50 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  SY.  The sqlite3
99f60 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74  _mutex_try() int
99f70 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53  erface returns S
99f80 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e  QLITE_OK.** upon
99f90 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72   successful entr
99fa0 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61  y.  Mutexes crea
99fb0 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45  ted using SQLITE
99fc0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
99fd0 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72   can.** be enter
99fe0 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
99ff0 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68  s by the same th
9a000 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63  read.  In such c
9a010 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74  ases the,.** mut
9a020 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65  ex must be exite
9a030 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65  d an equal numbe
9a040 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72  r of times befor
9a050 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  e another thread
9a060 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20  .** can enter.  
9a070 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  If the same thre
9a080 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65  ad tries to ente
9a090 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64  r any other kind
9a0a0 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72   of mutex.** mor
9a0b0 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65  e than once, the
9a0c0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
9a0d0 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  efined..*/.stati
9a0e0 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 45  c void winMutexE
9a0f0 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74  nter(sqlite3_mut
9a100 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
9a110 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f  ( p->id==SQLITE_
9a120 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
9a130 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65  || winMutexNothe
9a140 6c 64 28 70 29 20 29 3b 0a 20 20 45 6e 74 65 72  ld(p) );.  Enter
9a150 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
9a160 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d  &p->mutex);.  p-
9a170 3e 6f 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72  >owner = GetCurr
9a180 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b 20 0a  entThreadId(); .
9a190 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73    p->nRef++;.}.s
9a1a0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74  tatic int winMut
9a1b0 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75  exTry(sqlite3_mu
9a1c0 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  tex *p){.  int r
9a1d0 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
9a1e0 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
9a1f0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
9a200 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e 4d  ECURSIVE || winM
9a210 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29  utexNotheld(p) )
9a220 3b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  ;.  /*.  ** The 
9a230 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
9a240 79 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20 76  y() routine is v
9a250 65 72 79 20 72 61 72 65 6c 79 20 75 73 65 64 2c  ery rarely used,
9a260 20 61 6e 64 20 77 68 65 6e 20 69 74 0a 20 20 2a   and when it.  *
9a270 2a 20 69 73 20 75 73 65 64 20 69 74 20 69 73 20  * is used it is 
9a280 6d 65 72 65 6c 79 20 61 6e 20 6f 70 74 69 6d 69  merely an optimi
9a290 7a 61 74 69 6f 6e 2e 20 20 53 6f 20 69 74 20 69  zation.  So it i
9a2a0 73 20 4f 4b 20 66 6f 72 20 69 74 20 74 6f 20 61  s OK for it to a
9a2b0 6c 77 61 79 73 0a 20 20 2a 2a 20 66 61 69 6c 2e  lways.  ** fail.
9a2c0 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65    .  **.  ** The
9a2d0 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61   TryEnterCritica
9a2e0 6c 53 65 63 74 69 6f 6e 28 29 20 69 6e 74 65 72  lSection() inter
9a2f0 66 61 63 65 20 69 73 20 6f 6e 6c 79 20 61 76 61  face is only ava
9a300 69 6c 61 62 6c 65 20 6f 6e 20 57 69 6e 4e 54 2e  ilable on WinNT.
9a310 0a 20 20 2a 2a 20 41 6e 64 20 73 6f 6d 65 20 77  .  ** And some w
9a320 69 6e 64 6f 77 73 20 63 6f 6d 70 69 6c 65 72 73  indows compilers
9a330 20 63 6f 6d 70 6c 61 69 6e 20 69 66 20 79 6f 75   complain if you
9a340 20 74 72 79 20 74 6f 20 75 73 65 20 69 74 20 77   try to use it w
9a350 69 74 68 6f 75 74 0a 20 20 2a 2a 20 66 69 72 73  ithout.  ** firs
9a360 74 20 64 6f 69 6e 67 20 73 6f 6d 65 20 23 64 65  t doing some #de
9a370 66 69 6e 65 73 20 74 68 61 74 20 70 72 65 76 65  fines that preve
9a380 6e 74 20 53 51 4c 69 74 65 20 66 72 6f 6d 20 62  nt SQLite from b
9a390 75 69 6c 64 69 6e 67 20 6f 6e 20 57 69 6e 39 38  uilding on Win98
9a3a0 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 61 74 20  ..  ** For that 
9a3b0 72 65 61 73 6f 6e 2c 20 77 65 20 77 69 6c 6c 20  reason, we will 
9a3c0 6f 6d 69 74 20 74 68 69 73 20 6f 70 74 69 6d 69  omit this optimi
9a3d0 7a 61 74 69 6f 6e 20 66 6f 72 20 6e 6f 77 2e 20  zation for now. 
9a3e0 20 53 65 65 0a 20 20 2a 2a 20 74 69 63 6b 65 74   See.  ** ticket
9a3f0 20 23 32 36 38 35 2e 0a 20 20 2a 2f 0a 23 69 66   #2685..  */.#if
9a400 20 30 0a 20 20 69 66 28 20 6d 75 74 65 78 49 73   0.  if( mutexIs
9a410 4e 54 28 29 20 26 26 20 54 72 79 45 6e 74 65 72  NT() && TryEnter
9a420 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
9a430 26 70 2d 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20  &p->mutex) ){.  
9a440 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65 74    p->owner = Get
9a450 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28  CurrentThreadId(
9a460 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b  );.    p->nRef++
9a470 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  ;.    rc = SQLIT
9a480 45 5f 4f 4b 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  E_OK;.  }.#else.
9a490 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
9a4a0 45 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 20 20  ER(p);.#endif.  
9a4b0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
9a4c0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
9a4d0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f  mutex_leave() ro
9a4e0 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75  utine exits a mu
9a4f0 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20  tex that was.** 
9a500 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72  previously enter
9a510 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  ed by the same t
9a520 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61  hread.  The beha
9a530 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66  vior.** is undef
9a540 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65  ined if the mute
9a550 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  x is not current
9a560 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a  ly entered or.**
9a570 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
9a580 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51  y allocated.  SQ
9a590 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  Lite will never 
9a5a0 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74  do either..*/.st
9a5b0 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75 74  atic void winMut
9a5c0 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f  exLeave(sqlite3_
9a5d0 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
9a5e0 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29  ert( p->nRef>0 )
9a5f0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6f  ;.  assert( p->o
9a600 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74  wner==GetCurrent
9a610 54 68 72 65 61 64 49 64 28 29 20 29 3b 0a 20 20  ThreadId() );.  
9a620 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73  p->nRef--;.  ass
9a630 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  ert( p->nRef==0 
9a640 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  || p->id==SQLITE
9a650 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
9a660 20 29 3b 0a 20 20 4c 65 61 76 65 43 72 69 74 69   );.  LeaveCriti
9a670 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d  calSection(&p->m
9a680 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45  utex);.}..SQLITE
9a690 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
9a6a0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a  _mutex_methods *
9a6b0 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75  sqlite3DefaultMu
9a6c0 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61  tex(void){.  sta
9a6d0 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  tic sqlite3_mute
9a6e0 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78  x_methods sMutex
9a6f0 20 3d 20 7b 0a 20 20 20 20 77 69 6e 4d 75 74 65   = {.    winMute
9a700 78 49 6e 69 74 2c 0a 20 20 20 20 77 69 6e 4d 75  xInit,.    winMu
9a710 74 65 78 45 6e 64 2c 0a 20 20 20 20 77 69 6e 4d  texEnd,.    winM
9a720 75 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 77  utexAlloc,.    w
9a730 69 6e 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20  inMutexFree,.   
9a740 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 2c 0a   winMutexEnter,.
9a750 20 20 20 20 77 69 6e 4d 75 74 65 78 54 72 79 2c      winMutexTry,
9a760 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 4c 65 61  .    winMutexLea
9a770 76 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ve,.#ifdef SQLIT
9a780 45 5f 44 45 42 55 47 0a 20 20 20 20 77 69 6e 4d  E_DEBUG.    winM
9a790 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 77 69  utexHeld,.    wi
9a7a0 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65  nMutexNotheld.#e
9a7b0 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20 20 20 30  lse.    0,.    0
9a7c0 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20  .#endif.  };..  
9a7d0 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a  return &sMutex;.
9a7e0 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
9a7f0 54 45 5f 4d 55 54 45 58 5f 57 33 32 20 2a 2f 0a  TE_MUTEX_W32 */.
9a800 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
9a810 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 5f 77 33   End of mutex_w3
9a820 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  2.c ************
9a830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
9a850 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
9a860 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 61 6c 6c   Begin file mall
9a870 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  oc.c ***********
9a880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
9a8a0 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
9a8b0 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
9a8c0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
9a8d0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
9a8e0 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
9a8f0 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
9a900 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
9a910 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
9a920 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
9a930 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
9a940 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
9a950 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
9a960 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
9a970 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
9a980 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
9a990 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
9a9a0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
9a9b0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
9a9c0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
9a9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9aa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9aa10 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 65  *******.**.** Me
9aa20 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
9aa30 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  functions used t
9aa40 68 72 6f 75 67 68 6f 75 74 20 73 71 6c 69 74 65  hroughout sqlite
9aa50 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ..*/../*.** This
9aa60 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 20 77 68   routine runs wh
9aa70 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  en the memory al
9aa80 6c 6f 63 61 74 6f 72 20 73 65 65 73 20 74 68 61  locator sees tha
9aa90 74 20 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d  t the.** total m
9aaa0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
9aab0 20 69 73 20 61 62 6f 75 74 20 74 6f 20 65 78 63   is about to exc
9aac0 65 65 64 20 74 68 65 20 73 6f 66 74 20 68 65 61  eed the soft hea
9aad0 70 0a 2a 2a 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73  p.** limit..*/.s
9aae0 74 61 74 69 63 20 76 6f 69 64 20 73 6f 66 74 48  tatic void softH
9aaf0 65 61 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72  eapLimitEnforcer
9ab00 28 0a 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65  (.  void *NotUse
9ab10 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  d, .  sqlite3_in
9ab20 74 36 34 20 4e 6f 74 55 73 65 64 32 2c 0a 20 20  t64 NotUsed2,.  
9ab30 69 6e 74 20 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b  int allocSize.){
9ab40 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
9ab50 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
9ab60 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74  tUsed2);.  sqlit
9ab70 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
9ab80 79 28 61 6c 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a  y(allocSize);.}.
9ab90 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73  ./*.** Set the s
9aba0 6f 66 74 20 68 65 61 70 2d 73 69 7a 65 20 6c 69  oft heap-size li
9abb0 6d 69 74 20 66 6f 72 20 74 68 65 20 6c 69 62 72  mit for the libr
9abc0 61 72 79 2e 20 50 61 73 73 69 6e 67 20 61 20 7a  ary. Passing a z
9abd0 65 72 6f 20 6f 72 20 0a 2a 2a 20 6e 65 67 61 74  ero or .** negat
9abe0 69 76 65 20 76 61 6c 75 65 20 69 6e 64 69 63 61  ive value indica
9abf0 74 65 73 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f  tes no limit..*/
9ac00 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
9ac10 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65   sqlite3_soft_he
9ac20 61 70 5f 6c 69 6d 69 74 28 69 6e 74 20 6e 29 7b  ap_limit(int n){
9ac30 0a 20 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36  .  sqlite3_uint6
9ac40 34 20 69 4c 69 6d 69 74 3b 0a 20 20 69 6e 74 20  4 iLimit;.  int 
9ac50 6f 76 65 72 61 67 65 3b 0a 20 20 69 66 28 20 6e  overage;.  if( n
9ac60 3c 30 20 29 7b 0a 20 20 20 20 69 4c 69 6d 69 74  <0 ){.    iLimit
9ac70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
9ac80 20 20 20 69 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20     iLimit = n;. 
9ac90 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
9aca0 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
9acb0 20 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61    sqlite3_initia
9acc0 6c 69 7a 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20  lize();.#endif. 
9acd0 20 69 66 28 20 69 4c 69 6d 69 74 3e 30 20 29 7b   if( iLimit>0 ){
9ace0 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f  .    sqlite3Memo
9acf0 72 79 41 6c 61 72 6d 28 73 6f 66 74 48 65 61 70  ryAlarm(softHeap
9ad00 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 2c 20 30  LimitEnforcer, 0
9ad10 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 7d 65 6c  , iLimit);.  }el
9ad20 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d  se{.    sqlite3M
9ad30 65 6d 6f 72 79 41 6c 61 72 6d 28 30 2c 20 30 2c  emoryAlarm(0, 0,
9ad40 20 30 29 3b 0a 20 20 7d 0a 20 20 6f 76 65 72 61   0);.  }.  overa
9ad50 67 65 20 3d 20 28 69 6e 74 29 28 73 71 6c 69 74  ge = (int)(sqlit
9ad60 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29  e3_memory_used()
9ad70 20 2d 20 28 69 36 34 29 6e 29 3b 0a 20 20 69 66   - (i64)n);.  if
9ad80 28 20 6f 76 65 72 61 67 65 3e 30 20 29 7b 0a 20  ( overage>0 ){. 
9ad90 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61     sqlite3_relea
9ada0 73 65 5f 6d 65 6d 6f 72 79 28 6f 76 65 72 61 67  se_memory(overag
9adb0 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  e);.  }.}../*.**
9adc0 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 6c 65   Attempt to rele
9add0 61 73 65 20 75 70 20 74 6f 20 6e 20 62 79 74 65  ase up to n byte
9ade0 73 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69  s of non-essenti
9adf0 61 6c 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e  al memory curren
9ae00 74 6c 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 53  tly.** held by S
9ae10 51 4c 69 74 65 2e 20 41 6e 20 65 78 61 6d 70 6c  QLite. An exampl
9ae20 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69  e of non-essenti
9ae30 61 6c 20 6d 65 6d 6f 72 79 20 69 73 20 6d 65 6d  al memory is mem
9ae40 6f 72 79 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63  ory used to.** c
9ae50 61 63 68 65 20 64 61 74 61 62 61 73 65 20 70 61  ache database pa
9ae60 67 65 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74  ges that are not
9ae70 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73   currently in us
9ae80 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
9ae90 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c   int sqlite3_rel
9aea0 65 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 20  ease_memory(int 
9aeb0 6e 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54  n){.#ifdef SQLIT
9aec0 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f  E_ENABLE_MEMORY_
9aed0 4d 41 4e 41 47 45 4d 45 4e 54 0a 20 20 69 6e 74  MANAGEMENT.  int
9aee0 20 6e 52 65 74 20 3d 20 30 3b 0a 20 20 6e 52 65   nRet = 0;.  nRe
9aef0 74 20 2b 3d 20 73 71 6c 69 74 65 33 50 63 61 63  t += sqlite3Pcac
9af00 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28  heReleaseMemory(
9af10 6e 2d 6e 52 65 74 29 3b 0a 20 20 72 65 74 75 72  n-nRet);.  retur
9af20 6e 20 6e 52 65 74 3b 0a 23 65 6c 73 65 0a 20 20  n nRet;.#else.  
9af30 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
9af40 28 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  (n);.  return SQ
9af50 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a  LITE_OK;.#endif.
9af60 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 20 69  }../*.** State i
9af70 6e 66 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c  nformation local
9af80 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   to the memory a
9af90 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
9afa0 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53  tem..*/.static S
9afb0 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74  QLITE_WSD struct
9afc0 20 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a 20 20   Mem0Global {.  
9afd0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65  /* Number of fre
9afe0 65 20 70 61 67 65 73 20 66 6f 72 20 73 63 72 61  e pages for scra
9aff0 74 63 68 20 61 6e 64 20 70 61 67 65 2d 63 61 63  tch and page-cac
9b000 68 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 75  he memory */.  u
9b010 33 32 20 6e 53 63 72 61 74 63 68 46 72 65 65 3b  32 nScratchFree;
9b020 0a 20 20 75 33 32 20 6e 50 61 67 65 46 72 65 65  .  u32 nPageFree
9b030 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
9b040 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20  ex *mutex;      
9b050 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 73     /* Mutex to s
9b060 65 72 69 61 6c 69 7a 65 20 61 63 63 65 73 73 20  erialize access 
9b070 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  */..  /*.  ** Th
9b080 65 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b  e alarm callback
9b090 20 61 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e   and its argumen
9b0a0 74 73 2e 20 20 54 68 65 20 6d 65 6d 30 2e 6d 75  ts.  The mem0.mu
9b0b0 74 65 78 20 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20  tex lock will.  
9b0c0 2a 2a 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65  ** be held while
9b0d0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73   the callback is
9b0e0 20 72 75 6e 6e 69 6e 67 2e 20 20 52 65 63 75 72   running.  Recur
9b0f0 73 69 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a  sive calls into.
9b100 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20    ** the memory 
9b110 73 75 62 73 79 73 74 65 6d 20 61 72 65 20 61 6c  subsystem are al
9b120 6c 6f 77 65 64 2c 20 62 75 74 20 6e 6f 20 6e 65  lowed, but no ne
9b130 77 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c  w callbacks will
9b140 20 62 65 0a 20 20 2a 2a 20 69 73 73 75 65 64 2e   be.  ** issued.
9b150 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  .  */.  sqlite3_
9b160 69 6e 74 36 34 20 61 6c 61 72 6d 54 68 72 65 73  int64 alarmThres
9b170 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20 28 2a 61  hold;.  void (*a
9b180 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29 28 76 6f  larmCallback)(vo
9b190 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  id*, sqlite3_int
9b1a0 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20  64,int);.  void 
9b1b0 2a 61 6c 61 72 6d 41 72 67 3b 0a 0a 20 20 2f 2a  *alarmArg;..  /*
9b1c0 0a 20 20 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74  .  ** Pointers t
9b1d0 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71 6c  o the end of sql
9b1e0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b1f0 2e 70 53 63 72 61 74 63 68 20 61 6e 64 0a 20 20  .pScratch and.  
9b200 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ** sqlite3Global
9b210 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 74 6f 20  Config.pPage to 
9b220 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72  a block of memor
9b230 79 20 74 68 61 74 20 72 65 63 6f 72 64 73 0a 20  y that records. 
9b240 20 2a 2a 20 77 68 69 63 68 20 70 61 67 65 73 20   ** which pages 
9b250 61 72 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20  are available.. 
9b260 20 2a 2f 0a 20 20 75 33 32 20 2a 61 53 63 72 61   */.  u32 *aScra
9b270 74 63 68 46 72 65 65 3b 0a 20 20 75 33 32 20 2a  tchFree;.  u32 *
9b280 61 50 61 67 65 46 72 65 65 3b 0a 7d 20 6d 65 6d  aPageFree;.} mem
9b290 30 20 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30  0 = { 0, 0, 0, 0
9b2a0 2c 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a  , 0, 0, 0, 0 };.
9b2b0 0a 23 64 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c  .#define mem0 GL
9b2c0 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 30  OBAL(struct Mem0
9b2d0 47 6c 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f  Global, mem0)../
9b2e0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
9b2f0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
9b300 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e  ation subsystem.
9b310 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9b320 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61  TE int sqlite3Ma
9b330 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29 7b 0a  llocInit(void){.
9b340 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
9b350 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c  balConfig.m.xMal
9b360 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  loc==0 ){.    sq
9b370 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75  lite3MemSetDefau
9b380 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  lt();.  }.  mems
9b390 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a  et(&mem0, 0, siz
9b3a0 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 20 20 69 66  eof(mem0));.  if
9b3b0 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9b3c0 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78  onfig.bCoreMutex
9b3d0 20 29 7b 0a 20 20 20 20 6d 65 6d 30 2e 6d 75 74   ){.    mem0.mut
9b3e0 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
9b3f0 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
9b400 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b  TEX_STATIC_MEM);
9b410 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
9b420 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
9b430 53 63 72 61 74 63 68 20 26 26 20 73 71 6c 69 74  Scratch && sqlit
9b440 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
9b450 7a 53 63 72 61 74 63 68 3e 3d 31 30 30 0a 20 20  zScratch>=100.  
9b460 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c      && sqlite3Gl
9b470 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
9b480 74 63 68 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e  tch>=0 ){.    in
9b490 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  t i;.    sqlite3
9b4a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
9b4b0 63 72 61 74 63 68 20 3d 20 52 4f 55 4e 44 44 4f  cratch = ROUNDDO
9b4c0 57 4e 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61  WN8(sqlite3Globa
9b4d0 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
9b4e0 68 2d 34 29 3b 0a 20 20 20 20 6d 65 6d 30 2e 61  h-4);.    mem0.a
9b4f0 53 63 72 61 74 63 68 46 72 65 65 20 3d 20 28 75  ScratchFree = (u
9b500 33 32 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c  32*)&((char*)sql
9b510 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b520 2e 70 53 63 72 61 74 63 68 29 0a 20 20 20 20 20  .pScratch).     
9b530 20 20 20 20 20 20 20 20 20 20 20 20 20 5b 73 71               [sq
9b540 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b550 67 2e 73 7a 53 63 72 61 74 63 68 2a 73 71 6c 69  g.szScratch*sqli
9b560 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b570 6e 53 63 72 61 74 63 68 5d 3b 0a 20 20 20 20 66  nScratch];.    f
9b580 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c 69 74 65  or(i=0; i<sqlite
9b590 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53  3GlobalConfig.nS
9b5a0 63 72 61 74 63 68 3b 20 69 2b 2b 29 7b 20 6d 65  cratch; i++){ me
9b5b0 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b  m0.aScratchFree[
9b5c0 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65  i] = i; }.    me
9b5d0 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65 20  m0.nScratchFree 
9b5e0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
9b5f0 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 3b 0a  onfig.nScratch;.
9b600 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
9b610 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b620 2e 70 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20  .pScratch = 0;. 
9b630 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
9b640 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68  Config.szScratch
9b650 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
9b660 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b670 66 69 67 2e 70 50 61 67 65 20 26 26 20 73 71 6c  fig.pPage && sql
9b680 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9b690 2e 73 7a 50 61 67 65 3e 3d 35 31 32 0a 20 20 20  .szPage>=512.   
9b6a0 20 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f     && sqlite3Glo
9b6b0 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3e  balConfig.nPage>
9b6c0 3d 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  =1 ){.    int i;
9b6d0 0a 20 20 20 20 69 6e 74 20 6f 76 65 72 68 65 61  .    int overhea
9b6e0 64 3b 0a 20 20 20 20 69 6e 74 20 73 7a 20 3d 20  d;.    int sz = 
9b6f0 52 4f 55 4e 44 44 4f 57 4e 38 28 73 71 6c 69 74  ROUNDDOWN8(sqlit
9b700 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
9b710 7a 50 61 67 65 29 3b 0a 20 20 20 20 69 6e 74 20  zPage);.    int 
9b720 6e 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  n = sqlite3Globa
9b730 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 0a 20  lConfig.nPage;. 
9b740 20 20 20 6f 76 65 72 68 65 61 64 20 3d 20 28 34     overhead = (4
9b750 2a 6e 20 2b 20 73 7a 20 2d 20 31 29 2f 73 7a 3b  *n + sz - 1)/sz;
9b760 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
9b770 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 2d  alConfig.nPage -
9b780 3d 20 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 20  = overhead;.    
9b790 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 20 3d  mem0.aPageFree =
9b7a0 20 28 75 33 32 2a 29 26 28 28 63 68 61 72 2a 29   (u32*)&((char*)
9b7b0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b7c0 66 69 67 2e 70 50 61 67 65 29 0a 20 20 20 20 20  fig.pPage).     
9b7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 5b 73 71               [sq
9b7e0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b7f0 67 2e 73 7a 50 61 67 65 2a 73 71 6c 69 74 65 33  g.szPage*sqlite3
9b800 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61  GlobalConfig.nPa
9b810 67 65 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  ge];.    for(i=0
9b820 3b 20 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61  ; i<sqlite3Globa
9b830 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 20 69  lConfig.nPage; i
9b840 2b 2b 29 7b 20 6d 65 6d 30 2e 61 50 61 67 65 46  ++){ mem0.aPageF
9b850 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20  ree[i] = i; }.  
9b860 20 20 6d 65 6d 30 2e 6e 50 61 67 65 46 72 65 65    mem0.nPageFree
9b870 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
9b880 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20  Config.nPage;.  
9b890 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
9b8a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
9b8b0 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20 73 71  Page = 0;.    sq
9b8c0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b8d0 67 2e 73 7a 50 61 67 65 20 3d 20 30 3b 0a 20 20  g.szPage = 0;.  
9b8e0 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
9b8f0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9b900 2e 78 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c  .xInit(sqlite3Gl
9b910 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70  obalConfig.m.pAp
9b920 70 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pData);.}../*.**
9b930 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68   Deinitialize th
9b940 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
9b950 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a  ion subsystem..*
9b960 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9b970 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c   void sqlite3Mal
9b980 6c 6f 63 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20  locEnd(void){.  
9b990 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9b9a0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64  lConfig.m.xShutd
9b9b0 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  own ){.    sqlit
9b9c0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9b9d0 2e 78 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74  .xShutdown(sqlit
9b9e0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9b9f0 2e 70 41 70 70 44 61 74 61 29 3b 0a 20 20 7d 0a  .pAppData);.  }.
9ba00 20 20 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20    memset(&mem0, 
9ba10 30 2c 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29  0, sizeof(mem0))
9ba20 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
9ba30 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  n the amount of 
9ba40 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79  memory currently
9ba50 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f   checked out..*/
9ba60 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
9ba70 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
9ba80 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f  3_memory_used(vo
9ba90 69 64 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78  id){.  int n, mx
9baa0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  ;.  sqlite3_int6
9bab0 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 33  4 res;.  sqlite3
9bac0 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f 53  _status(SQLITE_S
9bad0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
9bae0 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 30 29 3b 0a  D, &n, &mx, 0);.
9baf0 20 20 72 65 73 20 3d 20 28 73 71 6c 69 74 65 33    res = (sqlite3
9bb00 5f 69 6e 74 36 34 29 6e 3b 20 20 2f 2a 20 57 6f  _int64)n;  /* Wo
9bb10 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 20 69 6e  rk around bug in
9bb20 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b   Borland C. Tick
9bb30 65 74 20 23 33 32 31 36 20 2a 2f 0a 20 20 72 65  et #3216 */.  re
9bb40 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a  turn res;.}../*.
9bb50 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61  ** Return the ma
9bb60 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20  ximum amount of 
9bb70 6d 65 6d 6f 72 79 20 74 68 61 74 20 68 61 73 20  memory that has 
9bb80 65 76 65 72 20 62 65 65 6e 0a 2a 2a 20 63 68 65  ever been.** che
9bb90 63 6b 65 64 20 6f 75 74 20 73 69 6e 63 65 20 65  cked out since e
9bba0 69 74 68 65 72 20 74 68 65 20 62 65 67 69 6e 6e  ither the beginn
9bbb0 69 6e 67 20 6f 66 20 74 68 69 73 20 70 72 6f 63  ing of this proc
9bbc0 65 73 73 0a 2a 2a 20 6f 72 20 73 69 6e 63 65 20  ess.** or since 
9bbd0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
9bbe0 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  reset..*/.SQLITE
9bbf0 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74  _API sqlite3_int
9bc00 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  64 sqlite3_memor
9bc10 79 5f 68 69 67 68 77 61 74 65 72 28 69 6e 74 20  y_highwater(int 
9bc20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20 20 69 6e  resetFlag){.  in
9bc30 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 74  t n, mx;.  sqlit
9bc40 65 33 5f 69 6e 74 36 34 20 72 65 73 3b 0a 20 20  e3_int64 res;.  
9bc50 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 53  sqlite3_status(S
9bc60 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
9bc70 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d  ORY_USED, &n, &m
9bc80 78 2c 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 20  x, resetFlag);. 
9bc90 20 72 65 73 20 3d 20 28 73 71 6c 69 74 65 33 5f   res = (sqlite3_
9bca0 69 6e 74 36 34 29 6d 78 3b 20 20 2f 2a 20 57 6f  int64)mx;  /* Wo
9bcb0 72 6b 20 61 72 6f 75 6e 64 20 62 75 67 20 69 6e  rk around bug in
9bcc0 20 42 6f 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b   Borland C. Tick
9bcd0 65 74 20 23 33 32 31 36 20 2a 2f 0a 20 20 72 65  et #3216 */.  re
9bce0 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a  turn res;.}../*.
9bcf0 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 61 6c  ** Change the al
9bd00 61 72 6d 20 63 61 6c 6c 62 61 63 6b 0a 2a 2f 0a  arm callback.*/.
9bd10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
9bd20 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79  nt sqlite3Memory
9bd30 41 6c 61 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78  Alarm(.  void(*x
9bd40 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a  Callback)(void *
9bd50 70 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e  pArg, sqlite3_in
9bd60 74 36 34 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c  t64 used,int N),
9bd70 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20  .  void *pArg,. 
9bd80 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
9bd90 54 68 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 73  Threshold.){.  s
9bda0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
9bdb0 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  er(mem0.mutex);.
9bdc0 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c    mem0.alarmCall
9bdd0 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b  back = xCallback
9bde0 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72  ;.  mem0.alarmAr
9bdf0 67 20 3d 20 70 41 72 67 3b 0a 20 20 6d 65 6d 30  g = pArg;.  mem0
9be00 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20  .alarmThreshold 
9be10 3d 20 69 54 68 72 65 73 68 6f 6c 64 3b 0a 20 20  = iThreshold;.  
9be20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
9be30 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ave(mem0.mutex);
9be40 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
9be50 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  _OK;.}..#ifndef 
9be60 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52  SQLITE_OMIT_DEPR
9be70 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70  ECATED./*.** Dep
9be80 72 65 63 61 74 65 64 20 65 78 74 65 72 6e 61 6c  recated external
9be90 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 6e 74   interface.  Int
9bea0 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74  ernal/core SQLit
9beb0 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64  e code.** should
9bec0 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4d 65 6d   call sqlite3Mem
9bed0 6f 72 79 41 6c 61 72 6d 2e 0a 2a 2f 0a 53 51 4c  oryAlarm..*/.SQL
9bee0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
9bef0 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d  te3_memory_alarm
9bf00 28 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62  (.  void(*xCallb
9bf10 61 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c  ack)(void *pArg,
9bf20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75   sqlite3_int64 u
9bf30 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f  sed,int N),.  vo
9bf40 69 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69  id *pArg,.  sqli
9bf50 74 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73  te3_int64 iThres
9bf60 68 6f 6c 64 0a 29 7b 0a 20 20 72 65 74 75 72 6e  hold.){.  return
9bf70 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c   sqlite3MemoryAl
9bf80 61 72 6d 28 78 43 61 6c 6c 62 61 63 6b 2c 20 70  arm(xCallback, p
9bf90 41 72 67 2c 20 69 54 68 72 65 73 68 6f 6c 64 29  Arg, iThreshold)
9bfa0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
9bfb0 2a 20 54 72 69 67 67 65 72 20 74 68 65 20 61 6c  * Trigger the al
9bfc0 61 72 6d 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76  arm .*/.static v
9bfd0 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  oid sqlite3Mallo
9bfe0 63 41 6c 61 72 6d 28 69 6e 74 20 6e 42 79 74 65  cAlarm(int nByte
9bff0 29 7b 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c  ){.  void (*xCal
9c000 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 73 71 6c  lback)(void*,sql
9c010 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 3b  ite3_int64,int);
9c020 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
9c030 20 6e 6f 77 55 73 65 64 3b 0a 20 20 76 6f 69 64   nowUsed;.  void
9c040 20 2a 70 41 72 67 3b 0a 20 20 69 66 28 20 6d 65   *pArg;.  if( me
9c050 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b  m0.alarmCallback
9c060 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
9c070 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d 65 6d 30  xCallback = mem0
9c080 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3b 0a  .alarmCallback;.
9c090 20 20 6e 6f 77 55 73 65 64 20 3d 20 73 71 6c 69    nowUsed = sqli
9c0a0 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28 53  te3StatusValue(S
9c0b0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
9c0c0 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20 70 41 72  ORY_USED);.  pAr
9c0d0 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72  g = mem0.alarmAr
9c0e0 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43  g;.  mem0.alarmC
9c0f0 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 73  allback = 0;.  s
9c100 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
9c110 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  ve(mem0.mutex);.
9c120 20 20 78 43 61 6c 6c 62 61 63 6b 28 70 41 72 67    xCallback(pArg
9c130 2c 20 6e 6f 77 55 73 65 64 2c 20 6e 42 79 74 65  , nowUsed, nByte
9c140 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
9c150 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75  ex_enter(mem0.mu
9c160 74 65 78 29 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61  tex);.  mem0.ala
9c170 72 6d 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61  rmCallback = xCa
9c180 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65 6d 30 2e 61  llback;.  mem0.a
9c190 6c 61 72 6d 41 72 67 20 3d 20 70 41 72 67 3b 0a  larmArg = pArg;.
9c1a0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 6d 65  }../*.** Do a me
9c1b0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
9c1c0 77 69 74 68 20 73 74 61 74 69 73 74 69 63 73 20  with statistics 
9c1d0 61 6e 64 20 61 6c 61 72 6d 73 2e 20 20 41 73 73  and alarms.  Ass
9c1e0 75 6d 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20  ume the.** lock 
9c1f0 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e  is already held.
9c200 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
9c210 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 69  allocWithAlarm(i
9c220 6e 74 20 6e 2c 20 76 6f 69 64 20 2a 2a 70 70 29  nt n, void **pp)
9c230 7b 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 3b 0a 20  {.  int nFull;. 
9c240 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65   void *p;.  asse
9c250 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
9c260 78 5f 68 65 6c 64 28 6d 65 6d 30 2e 6d 75 74 65  x_held(mem0.mute
9c270 78 29 20 29 3b 0a 20 20 6e 46 75 6c 6c 20 3d 20  x) );.  nFull = 
9c280 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9c290 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e  fig.m.xRoundup(n
9c2a0 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 61 74  );.  sqlite3Stat
9c2b0 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41  usSet(SQLITE_STA
9c2c0 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c  TUS_MALLOC_SIZE,
9c2d0 20 6e 29 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e   n);.  if( mem0.
9c2e0 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 21 3d 30  alarmCallback!=0
9c2f0 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 55 73 65   ){.    int nUse
9c300 64 20 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75  d = sqlite3Statu
9c310 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54  sValue(SQLITE_ST
9c320 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
9c330 29 3b 0a 20 20 20 20 69 66 28 20 6e 55 73 65 64  );.    if( nUsed
9c340 2b 6e 46 75 6c 6c 20 3e 3d 20 6d 65 6d 30 2e 61  +nFull >= mem0.a
9c350 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 29 7b  larmThreshold ){
9c360 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61  .      sqlite3Ma
9c370 6c 6c 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29  llocAlarm(nFull)
9c380 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20  ;.    }.  }.  p 
9c390 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
9c3a0 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28  onfig.m.xMalloc(
9c3b0 6e 46 75 6c 6c 29 3b 0a 20 20 69 66 28 20 70 3d  nFull);.  if( p=
9c3c0 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d  =0 && mem0.alarm
9c3d0 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20  Callback ){.    
9c3e0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61  sqlite3MallocAla
9c3f0 72 6d 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 70  rm(nFull);.    p
9c400 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
9c410 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63  Config.m.xMalloc
9c420 28 6e 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69  (nFull);.  }.  i
9c430 66 28 20 70 20 29 7b 0a 20 20 20 20 6e 46 75 6c  f( p ){.    nFul
9c440 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  l = sqlite3Mallo
9c450 63 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 73 71  cSize(p);.    sq
9c460 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
9c470 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
9c480 4f 52 59 5f 55 53 45 44 2c 20 6e 46 75 6c 6c 29  ORY_USED, nFull)
9c490 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20 70 3b  ;.  }.  *pp = p;
9c4a0 0a 20 20 72 65 74 75 72 6e 20 6e 46 75 6c 6c 3b  .  return nFull;
9c4b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
9c4c0 74 65 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73  te memory.  This
9c4d0 20 72 6f 75 74 69 6e 65 20 69 73 20 6c 69 6b 65   routine is like
9c4e0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
9c4f0 29 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74  ) except that it
9c500 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 65 20  .** assumes the 
9c510 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d  memory subsystem
9c520 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
9c530 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a  n initialized..*
9c540 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9c550 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61   void *sqlite3Ma
9c560 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76  lloc(int n){.  v
9c570 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 6e 3c  oid *p;.  if( n<
9c580 3d 30 20 7c 7c 20 6e 3e 3d 30 78 37 66 66 66 66  =0 || n>=0x7ffff
9c590 66 30 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 20  f00 ){.    /* A 
9c5a0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
9c5b0 6e 20 6f 66 20 61 20 6e 75 6d 62 65 72 20 6f 66  n of a number of
9c5c0 20 62 79 74 65 73 20 77 68 69 63 68 20 69 73 20   bytes which is 
9c5d0 6e 65 61 72 20 74 68 65 20 6d 61 78 69 6d 75 6d  near the maximum
9c5e0 0a 20 20 20 20 2a 2a 20 73 69 67 6e 65 64 20 69  .    ** signed i
9c5f0 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6d 69 67  nteger value mig
9c600 68 74 20 63 61 75 73 65 20 61 6e 20 69 6e 74 65  ht cause an inte
9c610 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 69 6e 73  ger overflow ins
9c620 69 64 65 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  ide of the.    *
9c630 2a 20 78 4d 61 6c 6c 6f 63 28 29 2e 20 20 48 65  * xMalloc().  He
9c640 6e 63 65 20 77 65 20 6c 69 6d 69 74 20 74 68 65  nce we limit the
9c650 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 74 6f   maximum size to
9c660 20 30 78 37 66 66 66 66 66 30 30 2c 20 67 69 76   0x7fffff00, giv
9c670 69 6e 67 0a 20 20 20 20 2a 2a 20 32 35 35 20 62  ing.    ** 255 b
9c680 79 74 65 73 20 6f 66 20 6f 76 65 72 68 65 61 64  ytes of overhead
9c690 2e 20 20 53 51 4c 69 74 65 20 69 74 73 65 6c 66  .  SQLite itself
9c6a0 20 77 69 6c 6c 20 6e 65 76 65 72 20 75 73 65 20   will never use 
9c6b0 61 6e 79 74 68 69 6e 67 20 6e 65 61 72 0a 20 20  anything near.  
9c6c0 20 20 2a 2a 20 74 68 69 73 20 61 6d 6f 75 6e 74    ** this amount
9c6d0 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 20  .  The only way 
9c6e0 74 6f 20 72 65 61 63 68 20 74 68 65 20 6c 69 6d  to reach the lim
9c6f0 69 74 20 69 73 20 77 69 74 68 20 73 71 6c 69 74  it is with sqlit
9c700 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20  e3_malloc() */. 
9c710 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73     p = 0;.  }els
9c720 65 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f  e if( sqlite3Glo
9c730 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
9c740 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  at ){.    sqlite
9c750 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
9c760 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 6d  m0.mutex);.    m
9c770 61 6c 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e  allocWithAlarm(n
9c780 2c 20 26 70 29 3b 0a 20 20 20 20 73 71 6c 69 74  , &p);.    sqlit
9c790 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
9c7a0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65  em0.mutex);.  }e
9c7b0 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c  lse{.    p = sql
9c7c0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9c7d0 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20  .m.xMalloc(n);. 
9c7e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
9c7f0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ../*.** This ver
9c800 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
9c810 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  ry allocation is
9c820 20 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20   for use by the 
9c830 61 70 70 6c 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20  application..** 
9c840 46 69 72 73 74 20 6d 61 6b 65 20 73 75 72 65 20  First make sure 
9c850 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79  the memory subsy
9c860 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69  stem is initiali
9c870 7a 65 64 2c 20 74 68 65 6e 20 64 6f 20 74 68 65  zed, then do the
9c880 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a  .** allocation..
9c890 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
9c8a0 69 64 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  id *sqlite3_mall
9c8b0 6f 63 28 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64  oc(int n){.#ifnd
9c8c0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
9c8d0 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71  UTOINIT.  if( sq
9c8e0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
9c8f0 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23  () ) return 0;.#
9c900 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73  endif.  return s
9c910 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b  qlite3Malloc(n);
9c920 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74  .}../*.** Each t
9c930 68 72 65 61 64 20 6d 61 79 20 6f 6e 6c 79 20 68  hread may only h
9c940 61 76 65 20 61 20 73 69 6e 67 6c 65 20 6f 75 74  ave a single out
9c950 73 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74  standing allocat
9c960 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 78 53 63 72  ion from.** xScr
9c970 61 74 63 68 4d 61 6c 6c 6f 63 28 29 2e 20 20 57  atchMalloc().  W
9c980 65 20 76 65 72 69 66 79 20 74 68 69 73 20 63 6f  e verify this co
9c990 6e 73 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20  nstraint in the 
9c9a0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a  single-threaded.
9c9b0 2a 2a 20 63 61 73 65 20 62 79 20 73 65 74 74 69  ** case by setti
9c9c0 6e 67 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f  ng scratchAllocO
9c9d0 75 74 20 74 6f 20 31 20 77 68 65 6e 20 61 6e 20  ut to 1 when an 
9c9e0 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73  allocation.** is
9c9f0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 6c 65   outstanding cle
9ca00 61 72 69 6e 67 20 69 74 20 77 68 65 6e 20 74 68  aring it when th
9ca10 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  e allocation is 
9ca20 66 72 65 65 64 2e 0a 2a 2f 0a 23 69 66 20 53 51  freed..*/.#if SQ
9ca30 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d  LITE_THREADSAFE=
9ca40 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e  =0 && !defined(N
9ca50 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e  DEBUG).static in
9ca60 74 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75  t scratchAllocOu
9ca70 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a  t = 0;.#endif...
9ca80 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d  /*.** Allocate m
9ca90 65 6d 6f 72 79 20 74 68 61 74 20 69 73 20 74 6f  emory that is to
9caa0 20 62 65 20 75 73 65 64 20 61 6e 64 20 72 65 6c   be used and rel
9cab0 65 61 73 65 64 20 72 69 67 68 74 20 61 77 61 79  eased right away
9cac0 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
9cad0 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20  e is similar to 
9cae0 61 6c 6c 6f 63 61 28 29 20 69 6e 20 74 68 61 74  alloca() in that
9caf0 20 69 74 20 69 73 20 6e 6f 74 20 69 6e 74 65 6e   it is not inten
9cb00 64 65 64 0a 2a 2a 20 66 6f 72 20 73 69 74 75 61  ded.** for situa
9cb10 74 69 6f 6e 73 20 77 68 65 72 65 20 74 68 65 20  tions where the 
9cb20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 62 65 20  memory might be 
9cb30 68 65 6c 64 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20  held long-term. 
9cb40 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   This.** routine
9cb50 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20   is intended to 
9cb60 67 65 74 20 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c  get memory to ol
9cb70 64 20 6c 61 72 67 65 20 74 72 61 6e 73 69 65 6e  d large transien
9cb80 74 20 64 61 74 61 0a 2a 2a 20 73 74 72 75 63 74  t data.** struct
9cb90 75 72 65 73 20 74 68 61 74 20 77 6f 75 6c 64 20  ures that would 
9cba0 6e 6f 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 69 74  not normally fit
9cbb0 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 66   on the stack of
9cbc0 20 61 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20   an.** embedded 
9cbd0 70 72 6f 63 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51  processor..*/.SQ
9cbe0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
9cbf0 64 20 2a 73 71 6c 69 74 65 33 53 63 72 61 74 63  d *sqlite3Scratc
9cc00 68 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a  hMalloc(int n){.
9cc10 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73    void *p;.  ass
9cc20 65 72 74 28 20 6e 3e 30 20 29 3b 0a 0a 23 69 66  ert( n>0 );..#if
9cc30 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
9cc40 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65  FE==0 && !define
9cc50 64 28 4e 44 45 42 55 47 29 0a 20 20 2f 2a 20 56  d(NDEBUG).  /* V
9cc60 65 72 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f  erify that no mo
9cc70 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61  re than one scra
9cc80 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70  tch allocation p
9cc90 65 72 20 74 68 72 65 61 64 0a 20 20 2a 2a 20 69  er thread.  ** i
9cca0 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74  s outstanding at
9ccb0 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69   one time.  (Thi
9ccc0 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65  s is only checke
9ccd0 64 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 69  d in the.  ** si
9cce0 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61  ngle-threaded ca
9ccf0 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e  se since checkin
9cd00 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74  g in the multi-t
9cd10 68 72 65 61 64 65 64 20 63 61 73 65 0a 20 20 2a  hreaded case.  *
9cd20 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20  * would be much 
9cd30 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64  more complicated
9cd40 2e 29 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .) */.  assert( 
9cd50 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d  scratchAllocOut=
9cd60 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  =0 );.#endif..  
9cd70 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9cd80 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
9cd90 68 3c 6e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  h<n ){.    goto 
9cda0 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77  scratch_overflow
9cdb0 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20  ;.  }else{  .   
9cdc0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
9cdd0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
9cde0 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 30 2e 6e  ;.    if( mem0.n
9cdf0 53 63 72 61 74 63 68 46 72 65 65 3d 3d 30 20 29  ScratchFree==0 )
9ce00 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
9ce10 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9ce20 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 67  .mutex);.      g
9ce30 6f 74 6f 20 73 63 72 61 74 63 68 5f 6f 76 65 72  oto scratch_over
9ce40 66 6c 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  flow;.    }else{
9ce50 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
9ce60 20 20 20 20 69 20 3d 20 6d 65 6d 30 2e 61 53 63      i = mem0.aSc
9ce70 72 61 74 63 68 46 72 65 65 5b 2d 2d 6d 65 6d 30  ratchFree[--mem0
9ce80 2e 6e 53 63 72 61 74 63 68 46 72 65 65 5d 3b 0a  .nScratchFree];.
9ce90 20 20 20 20 20 20 69 20 2a 3d 20 73 71 6c 69 74        i *= sqlit
9cea0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
9ceb0 7a 53 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20  zScratch;.      
9cec0 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64  sqlite3StatusAdd
9ced0 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53  (SQLITE_STATUS_S
9cee0 43 52 41 54 43 48 5f 55 53 45 44 2c 20 31 29 3b  CRATCH_USED, 1);
9cef0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
9cf00 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53  atusSet(SQLITE_S
9cf10 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49  TATUS_SCRATCH_SI
9cf20 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71  ZE, n);.      sq
9cf30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
9cf40 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  e(mem0.mutex);. 
9cf50 20 20 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29       p = (void*)
9cf60 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  &((char*)sqlite3
9cf70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9cf80 72 61 74 63 68 29 5b 69 5d 3b 0a 20 20 20 20 20  ratch)[i];.     
9cf90 20 61 73 73 65 72 74 28 20 20 28 28 28 75 38 2a   assert(  (((u8*
9cfa0 29 70 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37  )p - (u8*)0) & 7
9cfb0 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20  )==0 );.    }.  
9cfc0 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  }.#if SQLITE_THR
9cfd0 45 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64  EADSAFE==0 && !d
9cfe0 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20  efined(NDEBUG). 
9cff0 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74   scratchAllocOut
9d000 20 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a   = p!=0;.#endif.
9d010 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a 73 63  .  return p;..sc
9d020 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a  ratch_overflow:.
9d030 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
9d040 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
9d050 61 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  at ){.    sqlite
9d060 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
9d070 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73  m0.mutex);.    s
9d080 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28  qlite3StatusSet(
9d090 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
9d0a0 52 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a  RATCH_SIZE, n);.
9d0b0 20 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69      n = mallocWi
9d0c0 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a  thAlarm(n, &p);.
9d0d0 20 20 20 20 69 66 28 20 70 20 29 20 73 71 6c 69      if( p ) sqli
9d0e0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
9d0f0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
9d100 43 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e 29 3b  CH_OVERFLOW, n);
9d110 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
9d120 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
9d130 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tex);.  }else{. 
9d140 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c     p = sqlite3Gl
9d150 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61  obalConfig.m.xMa
9d160 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 69 66  lloc(n);.  }.#if
9d170 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
9d180 46 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65  FE==0 && !define
9d190 64 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61  d(NDEBUG).  scra
9d1a0 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21  tchAllocOut = p!
9d1b0 3d 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74  =0;.#endif.  ret
9d1c0 75 72 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c  urn p;    .}.SQL
9d1d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9d1e0 20 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46   sqlite3ScratchF
9d1f0 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  ree(void *p){.  
9d200 69 66 28 20 70 20 29 7b 0a 0a 23 69 66 20 53 51  if( p ){..#if SQ
9d210 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d  LITE_THREADSAFE=
9d220 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e  =0 && !defined(N
9d230 44 45 42 55 47 29 0a 20 20 20 20 2f 2a 20 56 65  DEBUG).    /* Ve
9d240 72 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72  rify that no mor
9d250 65 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74  e than one scrat
9d260 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65  ch allocation pe
9d270 72 20 74 68 72 65 61 64 0a 20 20 20 20 2a 2a 20  r thread.    ** 
9d280 69 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61  is outstanding a
9d290 74 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68  t one time.  (Th
9d2a0 69 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b  is is only check
9d2b0 65 64 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a  ed in the.    **
9d2c0 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64   single-threaded
9d2d0 20 63 61 73 65 20 73 69 6e 63 65 20 63 68 65 63   case since chec
9d2e0 6b 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74  king in the mult
9d2f0 69 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 0a  i-threaded case.
9d300 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20      ** would be 
9d310 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69  much more compli
9d320 63 61 74 65 64 2e 29 20 2a 2f 0a 20 20 20 20 61  cated.) */.    a
9d330 73 73 65 72 74 28 20 73 63 72 61 74 63 68 41 6c  ssert( scratchAl
9d340 6c 6f 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 20 20  locOut==1 );.   
9d350 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74   scratchAllocOut
9d360 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20   = 0;.#endif..  
9d370 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
9d380 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74  balConfig.pScrat
9d390 63 68 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20  ch==0.          
9d3a0 20 7c 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f   || p<sqlite3Glo
9d3b0 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74  balConfig.pScrat
9d3c0 63 68 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c  ch.           ||
9d3d0 20 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e   p>=(void*)mem0.
9d3e0 61 53 63 72 61 74 63 68 46 72 65 65 20 29 7b 0a  aScratchFree ){.
9d3f0 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
9d400 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d  3GlobalConfig.bM
9d410 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20  emstat ){.      
9d420 20 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71    int iSize = sq
9d430 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28  lite3MallocSize(
9d440 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  p);.        sqli
9d450 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
9d460 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem0.mutex);.   
9d470 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
9d480 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
9d490 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52  TUS_SCRATCH_OVER
9d4a0 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20  FLOW, -iSize);. 
9d4b0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
9d4c0 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
9d4d0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
9d4e0 44 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20  D, -iSize);.    
9d4f0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
9d500 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28  lConfig.m.xFree(
9d510 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  p);.        sqli
9d520 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
9d530 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem0.mutex);.   
9d540 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
9d550 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
9d560 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29  onfig.m.xFree(p)
9d570 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
9d580 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  lse{.      int i
9d590 3b 0a 20 20 20 20 20 20 69 20 3d 20 28 69 6e 74  ;.      i = (int
9d5a0 29 28 28 75 38 2a 29 70 20 2d 20 28 75 38 2a 29  )((u8*)p - (u8*)
9d5b0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9d5c0 66 69 67 2e 70 53 63 72 61 74 63 68 29 3b 0a 20  fig.pScratch);. 
9d5d0 20 20 20 20 20 69 20 2f 3d 20 73 71 6c 69 74 65       i /= sqlite
9d5e0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
9d5f0 53 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 61  Scratch;.      a
9d600 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
9d610 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  <sqlite3GlobalCo
9d620 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b  nfig.nScratch );
9d630 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
9d640 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e  utex_enter(mem0.
9d650 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73  mutex);.      as
9d660 73 65 72 74 28 20 6d 65 6d 30 2e 6e 53 63 72 61  sert( mem0.nScra
9d670 74 63 68 46 72 65 65 3c 28 75 33 32 29 73 71 6c  tchFree<(u32)sql
9d680 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9d690 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20  .nScratch );.   
9d6a0 20 20 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68     mem0.aScratch
9d6b0 46 72 65 65 5b 6d 65 6d 30 2e 6e 53 63 72 61 74  Free[mem0.nScrat
9d6c0 63 68 46 72 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20  chFree++] = i;. 
9d6d0 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
9d6e0 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
9d6f0 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44  TUS_SCRATCH_USED
9d700 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  , -1);.      sql
9d710 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
9d720 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9d730 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
9d740 20 54 52 55 45 20 69 66 20 70 20 69 73 20 61 20   TRUE if p is a 
9d750 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
9d760 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d   allocation from
9d770 20 64 62 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53   db.*/.#ifndef S
9d780 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41  QLITE_OMIT_LOOKA
9d790 53 49 44 45 0a 73 74 61 74 69 63 20 69 6e 74 20  SIDE.static int 
9d7a0 69 73 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c 69  isLookaside(sqli
9d7b0 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70  te3 *db, void *p
9d7c0 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 20 26  ){.  return db &
9d7d0 26 20 70 20 26 26 20 70 3e 3d 64 62 2d 3e 6c 6f  & p && p>=db->lo
9d7e0 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74 20 26  okaside.pStart &
9d7f0 26 20 70 3c 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  & p<db->lookasid
9d800 65 2e 70 45 6e 64 3b 0a 7d 0a 23 65 6c 73 65 0a  e.pEnd;.}.#else.
9d810 23 64 65 66 69 6e 65 20 69 73 4c 6f 6f 6b 61 73  #define isLookas
9d820 69 64 65 28 41 2c 42 29 20 30 0a 23 65 6e 64 69  ide(A,B) 0.#endi
9d830 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  f../*.** Return 
9d840 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 6d 65  the size of a me
9d850 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
9d860 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69  previously obtai
9d870 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69  ned from.** sqli
9d880 74 65 33 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 73  te3Malloc() or s
9d890 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
9d8a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9d8b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61  TE int sqlite3Ma
9d8c0 6c 6c 6f 63 53 69 7a 65 28 76 6f 69 64 20 2a 70  llocSize(void *p
9d8d0 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
9d8e0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9d8f0 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 7d 0a 53 51  m.xSize(p);.}.SQ
9d900 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
9d910 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
9d920 53 69 7a 65 28 73 71 6c 69 74 65 33 20 2a 64 62  Size(sqlite3 *db
9d930 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73  , void *p){.  as
9d940 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73  sert( db==0 || s
9d950 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
9d960 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
9d970 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64    if( isLookasid
9d980 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20  e(db, p) ){.    
9d990 72 65 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61  return db->looka
9d9a0 73 69 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65  side.sz;.  }else
9d9b0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
9d9c0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9d9d0 2e 6d 2e 78 53 69 7a 65 28 70 29 3b 0a 20 20 7d  .m.xSize(p);.  }
9d9e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d  .}../*.** Free m
9d9f0 65 6d 6f 72 79 20 70 72 65 76 69 6f 75 73 6c 79  emory previously
9da00 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
9da10 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a  qlite3Malloc()..
9da20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
9da30 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  id sqlite3_free(
9da40 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20  void *p){.  if( 
9da50 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
9da60 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
9da70 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61  alConfig.bMemsta
9da80 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
9da90 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
9daa0 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71  0.mutex);.    sq
9dab0 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
9dac0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
9dad0 4f 52 59 5f 55 53 45 44 2c 20 2d 73 71 6c 69 74  ORY_USED, -sqlit
9dae0 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 29  e3MallocSize(p))
9daf0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  ;.    sqlite3Glo
9db00 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65  balConfig.m.xFre
9db10 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  e(p);.    sqlite
9db20 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9db30 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c  m0.mutex);.  }el
9db40 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47  se{.    sqlite3G
9db50 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46  lobalConfig.m.xF
9db60 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ree(p);.  }.}../
9db70 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79  *.** Free memory
9db80 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 61   that might be a
9db90 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
9dba0 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61   particular data
9dbb0 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69  base.** connecti
9dbc0 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
9dbd0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
9dbe0 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 65 33  e3DbFree(sqlite3
9dbf0 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a   *db, void *p){.
9dc00 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20    assert( db==0 
9dc10 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
9dc20 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
9dc30 20 29 3b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b   );.  if( isLook
9dc40 61 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a  aside(db, p) ){.
9dc50 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f      LookasideSlo
9dc60 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b 61  t *pBuf = (Looka
9dc70 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0a 20 20 20  sideSlot*)p;.   
9dc80 20 70 42 75 66 2d 3e 70 4e 65 78 74 20 3d 20 64   pBuf->pNext = d
9dc90 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72  b->lookaside.pFr
9dca0 65 65 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  ee;.    db->look
9dcb0 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42  aside.pFree = pB
9dcc0 75 66 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  uf;.    db->look
9dcd0 61 73 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20  aside.nOut--;.  
9dce0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
9dcf0 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a  e3_free(p);.  }.
9dd00 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
9dd10 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65  the size of an e
9dd20 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61  xisting memory a
9dd30 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c  llocation.*/.SQL
9dd40 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
9dd50 20 2a 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63   *sqlite3Realloc
9dd60 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74  (void *pOld, int
9dd70 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20   nBytes){.  int 
9dd80 6e 4f 6c 64 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f  nOld, nNew;.  vo
9dd90 69 64 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20  id *pNew;.  if( 
9dda0 70 4f 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72  pOld==0 ){.    r
9ddb0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c  eturn sqlite3Mal
9ddc0 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d  loc(nBytes);.  }
9ddd0 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30  .  if( nBytes<=0
9dde0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
9ddf0 66 72 65 65 28 70 4f 6c 64 29 3b 0a 20 20 20 20  free(pOld);.    
9de00 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
9de10 69 66 28 20 6e 42 79 74 65 73 3e 3d 30 78 37 66  if( nBytes>=0x7f
9de20 66 66 66 66 30 30 20 29 7b 0a 20 20 20 20 2f 2a  ffff00 ){.    /*
9de30 20 54 68 65 20 30 78 37 66 66 66 66 30 30 20 6c   The 0x7ffff00 l
9de40 69 6d 69 74 20 74 65 72 6d 20 69 73 20 65 78 70  imit term is exp
9de50 6c 61 69 6e 65 64 20 69 6e 20 63 6f 6d 6d 65 6e  lained in commen
9de60 74 73 20 6f 6e 20 73 71 6c 69 74 65 33 4d 61 6c  ts on sqlite3Mal
9de70 6c 6f 63 28 29 20 2a 2f 0a 20 20 20 20 72 65 74  loc() */.    ret
9de80 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c  urn 0;.  }.  nOl
9de90 64 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  d = sqlite3Mallo
9dea0 63 53 69 7a 65 28 70 4f 6c 64 29 3b 0a 20 20 6e  cSize(pOld);.  n
9deb0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f  New = sqlite3Glo
9dec0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75  balConfig.m.xRou
9ded0 6e 64 75 70 28 6e 42 79 74 65 73 29 3b 0a 20 20  ndup(nBytes);.  
9dee0 69 66 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29  if( nOld==nNew )
9def0 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 70 4f 6c  {.    pNew = pOl
9df00 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  d;.  }else if( s
9df10 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9df20 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20  ig.bMemstat ){. 
9df30 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
9df40 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65  _enter(mem0.mute
9df50 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  x);.    sqlite3S
9df60 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f  tatusSet(SQLITE_
9df70 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49  STATUS_MALLOC_SI
9df80 5a 45 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20  ZE, nBytes);.   
9df90 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 61 74   if( sqlite3Stat
9dfa0 75 73 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53  usValue(SQLITE_S
9dfb0 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45  TATUS_MEMORY_USE
9dfc0 44 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 3e 3d 20  D)+nNew-nOld >= 
9dfd0 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 30 2e  .          mem0.
9dfe0 61 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 29  alarmThreshold )
9dff0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d  {.      sqlite3M
9e000 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e 4e 65 77 2d  allocAlarm(nNew-
9e010 6e 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20  nOld);.    }.   
9e020 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47   pNew = sqlite3G
9e030 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52  lobalConfig.m.xR
9e040 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65  ealloc(pOld, nNe
9e050 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77  w);.    if( pNew
9e060 3d 3d 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72  ==0 && mem0.alar
9e070 6d 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20  mCallback ){.   
9e080 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63     sqlite3Malloc
9e090 41 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20  Alarm(nBytes);. 
9e0a0 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
9e0b0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9e0c0 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c  m.xRealloc(pOld,
9e0d0 20 6e 4e 65 77 29 3b 0a 20 20 20 20 7d 0a 20 20   nNew);.    }.  
9e0e0 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20    if( pNew ){.  
9e0f0 20 20 20 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74      nNew = sqlit
9e100 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4e 65  e3MallocSize(pNe
9e110 77 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  w);.      sqlite
9e120 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
9e130 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
9e140 55 53 45 44 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29  USED, nNew-nOld)
9e150 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
9e160 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
9e170 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d  mem0.mutex);.  }
9e180 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 20 3d  else{.    pNew =
9e190 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9e1a0 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28  nfig.m.xRealloc(
9e1b0 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 7d  pOld, nNew);.  }
9e1c0 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
9e1d0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 75 62  }../*.** The pub
9e1e0 6c 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f  lic interface to
9e1f0 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 2e   sqlite3Realloc.
9e200 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 61 74    Make sure that
9e210 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73   the memory.** s
9e220 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74  ubsystem is init
9e230 69 61 6c 69 7a 65 64 20 70 72 69 6f 72 20 74 6f  ialized prior to
9e240 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65   invoking sqlite
9e250 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c 49  Realloc..*/.SQLI
9e260 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
9e270 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69  ite3_realloc(voi
9e280 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b  d *pOld, int n){
9e290 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
9e2a0 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
9e2b0 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  if( sqlite3_init
9e2c0 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72  ialize() ) retur
9e2d0 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65  n 0;.#endif.  re
9e2e0 74 75 72 6e 20 73 71 6c 69 74 65 33 52 65 61 6c  turn sqlite3Real
9e2f0 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a 7d 0a  loc(pOld, n);.}.
9e300 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
9e310 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79   and zero memory
9e320 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/ .SQLITE_PRI
9e330 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
9e340 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74  e3MallocZero(int
9e350 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d   n){.  void *p =
9e360 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e   sqlite3Malloc(n
9e370 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  );.  if( p ){.  
9e380 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e    memset(p, 0, n
9e390 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
9e3a0 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  p;.}../*.** Allo
9e3b0 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65  cate and zero me
9e3c0 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c  mory.  If the al
9e3d0 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20  location fails, 
9e3e0 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c  make.** the mall
9e3f0 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e  ocFailed flag in
9e400 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
9e410 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
9e420 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
9e430 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  *sqlite3DbMalloc
9e440 5a 65 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 62  Zero(sqlite3 *db
9e450 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64  , int n){.  void
9e460 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d   *p = sqlite3DbM
9e470 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b  allocRaw(db, n);
9e480 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
9e490 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b  memset(p, 0, n);
9e4a0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
9e4b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
9e4c0 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f  te and zero memo
9e4d0 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f  ry.  If the allo
9e4e0 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61  cation fails, ma
9e4f0 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63  ke.** the malloc
9e500 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74  Failed flag in t
9e510 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f  he connection po
9e520 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  inter..**.** If 
9e530 64 62 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d 61  db!=0 and db->ma
9e540 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 74 72  llocFailed is tr
9e550 75 65 20 28 69 6e 64 69 63 61 74 69 6e 67 20 61  ue (indicating a
9e560 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a 2a 2a   prior malloc.**
9e570 20 66 61 69 6c 75 72 65 20 6f 6e 20 74 68 65 20   failure on the 
9e580 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
9e590 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65 6e 20 61  nnection) then a
9e5a0 6c 77 61 79 73 20 72 65 74 75 72 6e 20 30 2e 0a  lways return 0..
9e5b0 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20 61 20 70  ** Hence for a p
9e5c0 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61  articular databa
9e5d0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f  se connection, o
9e5e0 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72 74  nce malloc start
9e5f0 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 69 74  s.** failing, it
9e600 20 66 61 69 6c 73 20 63 6f 6e 73 69 73 74 65 6e   fails consisten
9e610 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c 6c 6f 63  tly until malloc
9e620 46 61 69 6c 65 64 20 69 73 20 72 65 73 65 74 2e  Failed is reset.
9e630 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69  .** This is an i
9e640 6d 70 6f 72 74 61 6e 74 20 61 73 73 75 6d 70 74  mportant assumpt
9e650 69 6f 6e 2e 20 20 54 68 65 72 65 20 61 72 65 20  ion.  There are 
9e660 6d 61 6e 79 20 70 6c 61 63 65 73 20 69 6e 20 74  many places in t
9e670 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74 20  he.** code that 
9e680 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74  do things like t
9e690 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
9e6a0 20 20 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e 74     int *a = (int
9e6b0 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
9e6c0 63 52 61 77 28 64 62 2c 20 31 30 30 29 3b 0a 2a  cRaw(db, 100);.*
9e6d0 2a 20 20 20 20 20 20 20 20 20 69 6e 74 20 2a 62  *         int *b
9e6e0 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33   = (int*)sqlite3
9e6f0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
9e700 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 20  200);.**        
9e710 20 69 66 28 20 62 20 29 20 61 5b 31 30 5d 20 3d   if( b ) a[10] =
9e720 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68   9;.**.** In oth
9e730 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61 20 73  er words, if a s
9e740 75 62 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63  ubsequent malloc
9e750 20 28 65 78 3a 20 22 62 22 29 20 77 6f 72 6b 65   (ex: "b") worke
9e760 64 2c 20 69 74 20 69 73 20 61 73 73 75 6d 65 64  d, it is assumed
9e770 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 70 72 69  .** that all pri
9e780 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65 78 3a 20  or mallocs (ex: 
9e790 22 61 22 29 20 77 6f 72 6b 65 64 20 74 6f 6f 2e  "a") worked too.
9e7a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
9e7b0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
9e7c0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69  DbMallocRaw(sqli
9e7d0 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b  te3 *db, int n){
9e7e0 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73  .  void *p;.  as
9e7f0 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73  sert( db==0 || s
9e800 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
9e810 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
9e820 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
9e830 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 20 20  MIT_LOOKASIDE.  
9e840 69 66 28 20 64 62 20 29 7b 0a 20 20 20 20 4c 6f  if( db ){.    Lo
9e850 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75  okasideSlot *pBu
9e860 66 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  f;.    if( db->m
9e870 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
9e880 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
9e890 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d     }.    if( db-
9e8a0 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
9e8b0 6c 65 64 20 26 26 20 6e 3c 3d 64 62 2d 3e 6c 6f  led && n<=db->lo
9e8c0 6f 6b 61 73 69 64 65 2e 73 7a 0a 20 20 20 20 20  okaside.sz.     
9e8d0 20 20 20 20 26 26 20 28 70 42 75 66 20 3d 20 64      && (pBuf = d
9e8e0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72  b->lookaside.pFr
9e8f0 65 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ee)!=0 ){.      
9e900 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46  db->lookaside.pF
9e910 72 65 65 20 3d 20 70 42 75 66 2d 3e 70 4e 65 78  ree = pBuf->pNex
9e920 74 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f  t;.      db->loo
9e930 6b 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b 0a 20  kaside.nOut++;. 
9e940 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f       if( db->loo
9e950 6b 61 73 69 64 65 2e 6e 4f 75 74 3e 64 62 2d 3e  kaside.nOut>db->
9e960 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20  lookaside.mxOut 
9e970 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c  ){.        db->l
9e980 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d  ookaside.mxOut =
9e990 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e   db->lookaside.n
9e9a0 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Out;.      }.   
9e9b0 20 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a     return (void*
9e9c0 29 70 42 75 66 3b 0a 20 20 20 20 7d 0a 20 20 7d  )pBuf;.    }.  }
9e9d0 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 64 62 20  .#else.  if( db 
9e9e0 26 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  && db->mallocFai
9e9f0 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72  led ){.    retur
9ea00 6e 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  n 0;.  }.#endif.
9ea10 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
9ea20 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 21 70  loc(n);.  if( !p
9ea30 20 26 26 20 64 62 20 29 7b 0a 20 20 20 20 64 62   && db ){.    db
9ea40 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
9ea50 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
9ea60 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73   p;.}../*.** Res
9ea70 69 7a 65 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66  ize the block of
9ea80 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20   memory pointed 
9ea90 74 6f 20 62 79 20 70 20 74 6f 20 6e 20 62 79 74  to by p to n byt
9eaa0 65 73 2e 20 49 66 20 74 68 65 0a 2a 2a 20 72 65  es. If the.** re
9eab0 73 69 7a 65 20 66 61 69 6c 73 2c 20 73 65 74 20  size fails, set 
9eac0 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  the mallocFailed
9ead0 20 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e   flag in the con
9eae0 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a  nection object..
9eaf0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
9eb00 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44  E void *sqlite3D
9eb10 62 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33  bRealloc(sqlite3
9eb20 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69   *db, void *p, i
9eb30 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70  nt n){.  void *p
9eb40 4e 65 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  New = 0;.  asser
9eb50 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73  t( db!=0 );.  as
9eb60 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
9eb70 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
9eb80 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62 2d  ex) );.  if( db-
9eb90 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30  >mallocFailed==0
9eba0 20 29 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30   ){.    if( p==0
9ebb0 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
9ebc0 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
9ebd0 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20  Raw(db, n);.    
9ebe0 7d 0a 20 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b  }.    if( isLook
9ebf0 61 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a  aside(db, p) ){.
9ec00 20 20 20 20 20 20 69 66 28 20 6e 3c 3d 64 62 2d        if( n<=db-
9ec10 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29 7b  >lookaside.sz ){
9ec20 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
9ec30 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  p;.      }.     
9ec40 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
9ec50 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e  bMallocRaw(db, n
9ec60 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65  );.      if( pNe
9ec70 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  w ){.        mem
9ec80 63 70 79 28 70 4e 65 77 2c 20 70 2c 20 64 62 2d  cpy(pNew, p, db-
9ec90 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 3b 0a  >lookaside.sz);.
9eca0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
9ecb0 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20  bFree(db, p);.  
9ecc0 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
9ecd0 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71  .      pNew = sq
9ece0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2c  lite3_realloc(p,
9ecf0 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21   n);.      if( !
9ed00 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20  pNew ){.        
9ed10 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
9ed20 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
9ed30 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
9ed40 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pNew;.}../*.** 
9ed50 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61 6c 6c  Attempt to reall
9ed60 6f 63 61 74 65 20 70 2e 20 20 49 66 20 74 68 65  ocate p.  If the
9ed70 20 72 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61   reallocation fa
9ed80 69 6c 73 2c 20 74 68 65 6e 20 66 72 65 65 20 70  ils, then free p
9ed90 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74 68 65 20  .** and set the 
9eda0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
9edb0 67 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  g in the databas
9edc0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
9edd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9ede0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52  void *sqlite3DbR
9edf0 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c  eallocOrFree(sql
9ee00 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a  ite3 *db, void *
9ee10 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69  p, int n){.  voi
9ee20 64 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20  d *pNew;.  pNew 
9ee30 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
9ee40 6f 63 28 64 62 2c 20 70 2c 20 6e 29 3b 0a 20 20  oc(db, p, n);.  
9ee50 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20  if( !pNew ){.   
9ee60 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
9ee70 62 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74  b, p);.  }.  ret
9ee80 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
9ee90 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f  ** Make a copy o
9eea0 66 20 61 20 73 74 72 69 6e 67 20 69 6e 20 6d 65  f a string in me
9eeb0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
9eec0 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
9eed0 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 66 75 6e  ). These .** fun
9eee0 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73 71 6c 69  ctions call sqli
9eef0 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28 29 20 64  te3MallocRaw() d
9ef00 69 72 65 63 74 6c 79 20 69 6e 73 74 65 61 64 20  irectly instead 
9ef10 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  of sqliteMalloc(
9ef20 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 62 65  ). This.** is be
9ef30 63 61 75 73 65 20 77 68 65 6e 20 6d 65 6d 6f 72  cause when memor
9ef40 79 20 64 65 62 75 67 67 69 6e 67 20 69 73 20 74  y debugging is t
9ef50 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65 73 65 20  urned on, these 
9ef60 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72  two functions ar
9ef70 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 69 61  e .** called via
9ef80 20 6d 61 63 72 6f 73 20 74 68 61 74 20 72 65 63   macros that rec
9ef90 6f 72 64 20 74 68 65 20 63 75 72 72 65 6e 74 20  ord the current 
9efa0 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65 20 6e 75  file and line nu
9efb0 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 54  mber in the.** T
9efc0 68 72 65 61 64 44 61 74 61 20 73 74 72 75 63 74  hreadData struct
9efd0 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ure..*/.SQLITE_P
9efe0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
9eff0 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c  ite3DbStrDup(sql
9f000 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
9f010 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72  char *z){.  char
9f020 20 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a 65 5f 74   *zNew;.  size_t
9f030 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29   n;.  if( z==0 )
9f040 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
9f050 20 20 7d 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65    }.  n = sqlite
9f060 33 53 74 72 6c 65 6e 33 30 28 7a 29 20 2b 20 31  3Strlen30(z) + 1
9f070 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26 30  ;.  assert( (n&0
9f080 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b  x7fffffff)==n );
9f090 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  zNew = sqlite
9f0a0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
9f0b0 20 28 69 6e 74 29 6e 29 3b 0a 20 20 69 66 28 20   (int)n);.  if( 
9f0c0 7a 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63  zNew ){.    memc
9f0d0 70 79 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a  py(zNew, z, n);.
9f0e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65    }.  return zNe
9f0f0 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  w;.}.SQLITE_PRIV
9f100 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
9f110 33 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74  3DbStrNDup(sqlit
9f120 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
9f130 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20  ar *z, int n){. 
9f140 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 69   char *zNew;.  i
9f150 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72  f( z==0 ){.    r
9f160 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61  eturn 0;.  }.  a
9f170 73 73 65 72 74 28 20 28 6e 26 30 78 37 66 66 66  ssert( (n&0x7fff
9f180 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e  ffff)==n );.  zN
9f190 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
9f1a0 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2b 31 29  llocRaw(db, n+1)
9f1b0 3b 0a 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a  ;.  if( zNew ){.
9f1c0 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c      memcpy(zNew,
9f1d0 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65 77   z, n);.    zNew
9f1e0 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  [n] = 0;.  }.  r
9f1f0 65 74 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 2f  eturn zNew;.}../
9f200 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 73 74  *.** Create a st
9f210 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a 46  ring from the zF
9f220 72 6f 6d 61 74 20 61 72 67 75 6d 65 6e 74 20 61  romat argument a
9f230 6e 64 20 74 68 65 20 76 61 5f 6c 69 73 74 20 74  nd the va_list t
9f240 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a 20  hat follows..** 
9f250 53 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67  Store the string
9f260 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69   in memory obtai
9f270 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
9f280 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65  alloc() and make
9f290 20 2a 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f   *pz.** point to
9f2a0 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2f   that string..*/
9f2b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9f2c0 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 74 53  void sqlite3SetS
9f2d0 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a 2c  tring(char **pz,
9f2e0 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f   sqlite3 *db, co
9f2f0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
9f300 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  t, ...){.  va_li
9f310 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a  st ap;.  char *z
9f320 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  ;..  va_start(ap
9f330 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20  , zFormat);.  z 
9f340 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
9f350 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61  f(db, zFormat, a
9f360 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29  p);.  va_end(ap)
9f370 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
9f380 65 28 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 2a 70  e(db, *pz);.  *p
9f390 7a 20 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  z = z;.}.../*.**
9f3a0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
9f3b0 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65  ust be called be
9f3c0 66 6f 72 65 20 65 78 69 74 69 6e 67 20 61 6e 79  fore exiting any
9f3d0 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 69   API function (i
9f3e0 2e 65 2e 20 0a 2a 2a 20 72 65 74 75 72 6e 69 6e  .e. .** returnin
9f3f0 67 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65  g control to the
9f400 20 75 73 65 72 29 20 74 68 61 74 20 68 61 73 20   user) that has 
9f410 63 61 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f 6d  called sqlite3_m
9f420 61 6c 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 6c 69  alloc or.** sqli
9f430 74 65 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a 2a 0a  te3_realloc..**.
9f440 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20  ** The returned 
9f450 76 61 6c 75 65 20 69 73 20 6e 6f 72 6d 61 6c 6c  value is normall
9f460 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  y a copy of the 
9f470 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
9f480 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74  to this.** funct
9f490 69 6f 6e 2e 20 48 6f 77 65 76 65 72 2c 20 69 66  ion. However, if
9f4a0 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
9f4b0 75 72 65 20 68 61 73 20 6f 63 63 75 72 72 65 64  ure has occurred
9f4c0 20 73 69 6e 63 65 20 74 68 65 20 70 72 65 76 69   since the previ
9f4d0 6f 75 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f  ous.** invocatio
9f4e0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69  n SQLITE_NOMEM i
9f4f0 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65  s returned inste
9f500 61 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ad. .**.** If th
9f510 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
9f520 2c 20 64 62 2c 20 69 73 20 6e 6f 74 20 4e 55 4c  , db, is not NUL
9f530 4c 20 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 28 29  L and a malloc()
9f540 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
9f550 72 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  red,.** then the
9f560 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72 6f   connection erro
9f570 72 2d 63 6f 64 65 20 28 74 68 65 20 76 61 6c 75  r-code (the valu
9f580 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
9f590 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 29  lite3_errcode())
9f5a0 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51  .** is set to SQ
9f5b0 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 53  LITE_NOMEM..*/.S
9f5c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
9f5d0 74 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  t sqlite3ApiExit
9f5e0 28 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 69 6e  (sqlite3* db, in
9f5f0 74 20 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20 74  t rc){.  /* If t
9f600 68 65 20 64 62 20 68 61 6e 64 6c 65 20 69 73 20  he db handle is 
9f610 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 77  not NULL, then w
9f620 65 20 6d 75 73 74 20 68 6f 6c 64 20 74 68 65 20  e must hold the 
9f630 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  connection handl
9f640 65 0a 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 72  e.  ** mutex her
9f650 65 2e 20 4f 74 68 65 72 77 69 73 65 20 74 68 65  e. Otherwise the
9f660 20 72 65 61 64 20 28 61 6e 64 20 70 6f 73 73 69   read (and possi
9f670 62 6c 65 20 77 72 69 74 65 29 20 6f 66 20 64 62  ble write) of db
9f680 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 0a  ->mallocFailed .
9f690 20 20 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c 20    ** is unsafe, 
9f6a0 61 73 20 69 73 20 74 68 65 20 63 61 6c 6c 20 74  as is the call t
9f6b0 6f 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29  o sqlite3Error()
9f6c0 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
9f6d0 20 21 64 62 20 7c 7c 20 73 71 6c 69 74 65 33 5f   !db || sqlite3_
9f6e0 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
9f6f0 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64  utex) );.  if( d
9f700 62 20 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f 63  b && (db->malloc
9f710 46 61 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53 51  Failed || rc==SQ
9f720 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
9f730 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
9f740 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
9f750 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20  _NOMEM, 0);.    
9f760 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
9f770 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53   = 0;.    rc = S
9f780 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
9f790 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 28  .  return rc & (
9f7a0 64 62 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 6b  db ? db->errMask
9f7b0 20 3a 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a   : 0xff);.}../**
9f7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
9f7d0 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a   of malloc.c ***
9f7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
9f810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
9f820 69 6e 20 66 69 6c 65 20 70 72 69 6e 74 66 2e 63  in file printf.c
9f830 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
9f840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
9f860 2a 2a 20 54 68 65 20 22 70 72 69 6e 74 66 22 20  ** The "printf" 
9f870 63 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77  code that follow
9f880 73 20 64 61 74 65 73 20 66 72 6f 6d 20 74 68 65  s dates from the
9f890 20 31 39 38 30 27 73 2e 20 20 49 74 20 69 73 20   1980's.  It is 
9f8a0 69 6e 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 63  in.** the public
9f8b0 20 64 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f 72   domain.  The or
9f8c0 69 67 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 20  iginal comments 
9f8d0 61 72 65 20 69 6e 63 6c 75 64 65 64 20 68 65 72  are included her
9f8e0 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74  e for.** complet
9f8f0 65 6e 65 73 73 2e 20 20 54 68 65 79 20 61 72 65  eness.  They are
9f900 20 76 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 74   very out-of-dat
9f910 65 20 62 75 74 20 6d 69 67 68 74 20 62 65 20 75  e but might be u
9f920 73 65 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 68  seful as.** an h
9f930 69 73 74 6f 72 69 63 61 6c 20 72 65 66 65 72 65  istorical refere
9f940 6e 63 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68  nce.  Most of th
9f950 65 20 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 22  e "enhancements"
9f960 20 68 61 76 65 20 62 65 65 6e 20 62 61 63 6b 65   have been backe
9f970 64 0a 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 74  d.** out so that
9f980 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69   the functionali
9f990 74 79 20 69 73 20 6e 6f 77 20 74 68 65 20 73 61  ty is now the sa
9f9a0 6d 65 20 61 73 20 73 74 61 6e 64 61 72 64 20 70  me as standard p
9f9b0 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 2a 2a  rintf()..**.****
9f9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9fa00 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  ******.**.** The
9fa10 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c   following modul
9fa20 65 73 20 69 73 20 61 6e 20 65 6e 68 61 6e 63 65  es is an enhance
9fa30 64 20 72 65 70 6c 61 63 65 6d 65 6e 74 20 66 6f  d replacement fo
9fa40 72 20 74 68 65 20 22 70 72 69 6e 74 66 22 20 73  r the "printf" s
9fa50 75 62 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 66 6f  ubroutines.** fo
9fa60 75 6e 64 20 69 6e 20 74 68 65 20 73 74 61 6e 64  und in the stand
9fa70 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 20 20  ard C library.  
9fa80 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e  The following en
9fa90 68 61 6e 63 65 6d 65 6e 74 73 20 61 72 65 0a 2a  hancements are.*
9faa0 2a 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a  * supported:.**.
9fab0 2a 2a 20 20 20 20 20 20 2b 20 20 41 64 64 69 74  **      +  Addit
9fac0 69 6f 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 73 2e  ional functions.
9fad0 20 20 54 68 65 20 73 74 61 6e 64 61 72 64 20 73    The standard s
9fae0 65 74 20 6f 66 20 22 70 72 69 6e 74 66 22 20 66  et of "printf" f
9faf0 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20  unctions.**     
9fb00 20 20 20 20 69 6e 63 6c 75 64 65 73 20 70 72 69      includes pri
9fb10 6e 74 66 2c 20 66 70 72 69 6e 74 66 2c 20 73 70  ntf, fprintf, sp
9fb20 72 69 6e 74 66 2c 20 76 70 72 69 6e 74 66 2c 20  rintf, vprintf, 
9fb30 76 66 70 72 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a  vfprintf, and.**
9fb40 20 20 20 20 20 20 20 20 20 76 73 70 72 69 6e 74           vsprint
9fb50 66 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65 20  f.  This module 
9fb60 61 64 64 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69  adds the followi
9fb70 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ng:.**.**       
9fb80 20 20 20 20 2a 20 20 73 6e 70 72 69 6e 74 66 20      *  snprintf 
9fb90 2d 2d 20 57 6f 72 6b 73 20 6c 69 6b 65 20 73 70  -- Works like sp
9fba0 72 69 6e 74 66 2c 20 62 75 74 20 68 61 73 20 61  rintf, but has a
9fbb0 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65 6e 74  n extra argument
9fbc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68 69               whi
9fbe0 63 68 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f  ch is the size o
9fbf0 66 20 74 68 65 20 62 75 66 66 65 72 20 77 72 69  f the buffer wri
9fc00 74 74 65 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20  tten to..**.**  
9fc10 20 20 20 20 20 20 20 20 20 2a 20 20 6d 70 72 69           *  mpri
9fc20 6e 74 66 20 2d 2d 20 20 53 69 6d 69 6c 61 72 20  ntf --  Similar 
9fc30 74 6f 20 73 70 72 69 6e 74 66 2e 20 20 57 72 69  to sprintf.  Wri
9fc40 74 65 73 20 6f 75 74 70 75 74 20 74 6f 20 6d 65  tes output to me
9fc50 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20  mory.**         
9fc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fc70 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d   obtained from m
9fc80 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  alloc..**.**    
9fc90 20 20 20 20 20 20 20 2a 20 20 78 70 72 69 6e 74         *  xprint
9fca0 66 20 2d 2d 20 20 43 61 6c 6c 73 20 61 20 66 75  f --  Calls a fu
9fcb0 6e 63 74 69 6f 6e 20 74 6f 20 64 69 73 70 6f 73  nction to dispos
9fcc0 65 20 6f 66 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a  e of output..**.
9fcd0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20  **           *  
9fce0 6e 70 72 69 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f  nprintf --  No o
9fcf0 75 74 70 75 74 2c 20 62 75 74 20 72 65 74 75 72  utput, but retur
9fd00 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
9fd10 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20   characters.**  
9fd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9fd30 20 20 20 20 20 20 20 20 74 68 61 74 20 77 6f 75          that wou
9fd40 6c 64 20 68 61 76 65 20 62 65 65 6e 20 6f 75 74  ld have been out
9fd50 70 75 74 20 62 79 20 70 72 69 6e 74 66 2e 0a 2a  put by printf..*
9fd60 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
9fd70 20 20 41 20 76 2d 20 76 65 72 73 69 6f 6e 20 28    A v- version (
9fd80 65 78 3a 20 76 73 6e 70 72 69 6e 74 66 29 20 6f  ex: vsnprintf) o
9fd90 66 20 65 76 65 72 79 20 66 75 6e 63 74 69 6f 6e  f every function
9fda0 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20   is also.**     
9fdb0 20 20 20 20 20 20 20 20 20 73 75 70 70 6c 69 65           supplie
9fdc0 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20  d..**.**      + 
9fdd0 20 41 20 66 65 77 20 65 78 74 65 6e 73 69 6f 6e   A few extension
9fde0 73 20 74 6f 20 74 68 65 20 66 6f 72 6d 61 74 74  s to the formatt
9fdf0 69 6e 67 20 6e 6f 74 61 74 69 6f 6e 20 61 72 65  ing notation are
9fe00 20 73 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a   supported:.**.*
9fe10 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54  *           *  T
9fe20 68 65 20 22 3d 22 20 66 6c 61 67 20 28 73 69 6d  he "=" flag (sim
9fe30 69 6c 61 72 20 74 6f 20 22 2d 22 29 20 63 61 75  ilar to "-") cau
9fe40 73 65 73 20 74 68 65 20 6f 75 74 70 75 74 20 74  ses the output t
9fe50 6f 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  o be.**         
9fe60 20 20 20 20 20 62 65 20 63 65 6e 74 65 72 65 64       be centered
9fe70 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69   in the appropri
9fe80 61 74 65 6c 79 20 73 69 7a 65 64 20 66 69 65 6c  ately sized fiel
9fe90 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  d..**.**        
9fea0 20 20 20 2a 20 20 54 68 65 20 25 62 20 66 69 65     *  The %b fie
9feb0 6c 64 20 6f 75 74 70 75 74 73 20 61 6e 20 69 6e  ld outputs an in
9fec0 74 65 67 65 72 20 69 6e 20 62 69 6e 61 72 79 20  teger in binary 
9fed0 6e 6f 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  notation..**.** 
9fee0 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 65            *  The
9fef0 20 25 63 20 66 69 65 6c 64 20 6e 6f 77 20 61 63   %c field now ac
9ff00 63 65 70 74 73 20 61 20 70 72 65 63 69 73 69 6f  cepts a precisio
9ff10 6e 2e 20 20 54 68 65 20 63 68 61 72 61 63 74 65  n.  The characte
9ff20 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20  r output.**     
9ff30 20 20 20 20 20 20 20 20 20 69 73 20 72 65 70 65           is repe
9ff40 61 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62  ated by the numb
9ff50 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 65 20  er of times the 
9ff60 70 72 65 63 69 73 69 6f 6e 20 73 70 65 63 69 66  precision specif
9ff70 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ies..**.**      
9ff80 20 20 20 20 20 2a 20 20 54 68 65 20 25 27 20 66       *  The %' f
9ff90 69 65 6c 64 20 77 6f 72 6b 73 20 6c 69 6b 65 20  ield works like 
9ffa0 25 63 2c 20 62 75 74 20 74 61 6b 65 73 20 61 73  %c, but takes as
9ffb0 20 69 74 73 20 63 68 61 72 61 63 74 65 72 20 74   its character t
9ffc0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
9ffd0 20 20 20 6e 65 78 74 20 63 68 61 72 61 63 74 65     next characte
9ffe0 72 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20  r of the format 
9fff0 73 74 72 69 6e 67 2c 20 69 6e 73 74 65 61 64 20  string, instead 
a0000 6f 66 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20  of the next.**  
a0010 20 20 20 20 20 20 20 20 20 20 20 20 61 72 67 75              argu
a0020 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78 61 6d 70  ment.  For examp
a0030 6c 65 2c 20 20 70 72 69 6e 74 66 28 22 25 2e 37  le,  printf("%.7
a0040 38 27 2d 22 29 20 20 70 72 69 6e 74 73 20 37 38  8'-")  prints 78
a0050 20 6d 69 6e 75 73 0a 2a 2a 20 20 20 20 20 20 20   minus.**       
a0060 20 20 20 20 20 20 20 73 69 67 6e 73 2c 20 74 68         signs, th
a0070 65 20 73 61 6d 65 20 61 73 20 20 70 72 69 6e 74  e same as  print
a0080 66 28 22 25 2e 37 38 63 22 2c 27 2d 27 29 2e 0a  f("%.78c",'-')..
a0090 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 57 68  **.**      +  Wh
a00a0 65 6e 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e  en compiled usin
a00b0 67 20 47 43 43 20 6f 6e 20 61 20 53 50 41 52 43  g GCC on a SPARC
a00c0 2c 20 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  , this version o
a00d0 66 20 70 72 69 6e 74 66 20 69 73 0a 2a 2a 20 20  f printf is.**  
a00e0 20 20 20 20 20 20 20 66 61 73 74 65 72 20 74 68         faster th
a00f0 61 6e 20 74 68 65 20 6c 69 62 72 61 72 79 20 70  an the library p
a0100 72 69 6e 74 66 20 66 6f 72 20 53 55 4e 20 4f 53  rintf for SUN OS
a0110 20 34 2e 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20   4.1..**.**     
a0120 20 2b 20 20 41 6c 6c 20 66 75 6e 63 74 69 6f 6e   +  All function
a0130 73 20 61 72 65 20 66 75 6c 6c 79 20 72 65 65 6e  s are fully reen
a0140 74 72 61 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a  trant..**.*/../*
a0150 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 74  .** Conversion t
a0160 79 70 65 73 20 66 61 6c 6c 20 69 6e 74 6f 20 76  ypes fall into v
a0170 61 72 69 6f 75 73 20 63 61 74 65 67 6f 72 69 65  arious categorie
a0180 73 20 61 73 20 64 65 66 69 6e 65 64 20 62 79 20  s as defined by 
a0190 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  the.** following
a01a0 20 65 6e 75 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f   enumeration..*/
a01b0 0a 23 64 65 66 69 6e 65 20 65 74 52 41 44 49 58  .#define etRADIX
a01c0 20 20 20 20 20 20 20 31 20 2f 2a 20 49 6e 74 65         1 /* Inte
a01d0 67 65 72 20 74 79 70 65 73 2e 20 20 25 64 2c 20  ger types.  %d, 
a01e0 25 78 2c 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66  %x, %o, and so f
a01f0 6f 72 74 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20  orth */.#define 
a0200 65 74 46 4c 4f 41 54 20 20 20 20 20 20 20 32 20  etFLOAT       2 
a0210 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  /* Floating poin
a0220 74 2e 20 20 25 66 20 2a 2f 0a 23 64 65 66 69 6e  t.  %f */.#defin
a0230 65 20 65 74 45 58 50 20 20 20 20 20 20 20 20 20  e etEXP         
a0240 33 20 2f 2a 20 45 78 70 6f 6e 65 6e 74 69 6f 6e  3 /* Exponention
a0250 61 6c 20 6e 6f 74 61 74 69 6f 6e 2e 20 25 65 20  al notation. %e 
a0260 61 6e 64 20 25 45 20 2a 2f 0a 23 64 65 66 69 6e  and %E */.#defin
a0270 65 20 65 74 47 45 4e 45 52 49 43 20 20 20 20 20  e etGENERIC     
a0280 34 20 2f 2a 20 46 6c 6f 61 74 69 6e 67 20 6f 72  4 /* Floating or
a0290 20 65 78 70 6f 6e 65 6e 74 69 61 6c 2c 20 64 65   exponential, de
a02a0 70 65 6e 64 69 6e 67 20 6f 6e 20 65 78 70 6f 6e  pending on expon
a02b0 65 6e 74 2e 20 25 67 20 2a 2f 0a 23 64 65 66 69  ent. %g */.#defi
a02c0 6e 65 20 65 74 53 49 5a 45 20 20 20 20 20 20 20  ne etSIZE       
a02d0 20 35 20 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d   5 /* Return num
a02e0 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
a02f0 73 20 70 72 6f 63 65 73 73 65 64 20 73 6f 20 66  s processed so f
a0300 61 72 2e 20 25 6e 20 2a 2f 0a 23 64 65 66 69 6e  ar. %n */.#defin
a0310 65 20 65 74 53 54 52 49 4e 47 20 20 20 20 20 20  e etSTRING      
a0320 36 20 2f 2a 20 53 74 72 69 6e 67 73 2e 20 25 73  6 /* Strings. %s
a0330 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 44 59   */.#define etDY
a0340 4e 53 54 52 49 4e 47 20 20 20 37 20 2f 2a 20 44  NSTRING   7 /* D
a0350 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
a0360 61 74 65 64 20 73 74 72 69 6e 67 73 2e 20 25 7a  ated strings. %z
a0370 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 50 45   */.#define etPE
a0380 52 43 45 4e 54 20 20 20 20 20 38 20 2f 2a 20 50  RCENT     8 /* P
a0390 65 72 63 65 6e 74 20 73 79 6d 62 6f 6c 2e 20 25  ercent symbol. %
a03a0 25 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 43  % */.#define etC
a03b0 48 41 52 58 20 20 20 20 20 20 20 39 20 2f 2a 20  HARX       9 /* 
a03c0 43 68 61 72 61 63 74 65 72 73 2e 20 25 63 20 2a  Characters. %c *
a03d0 2f 0a 2f 2a 20 54 68 65 20 72 65 73 74 20 61 72  /./* The rest ar
a03e0 65 20 65 78 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f  e extensions, no
a03f0 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 6f 75 6e 64  t normally found
a0400 20 69 6e 20 70 72 69 6e 74 66 28 29 20 2a 2f 0a   in printf() */.
a0410 23 64 65 66 69 6e 65 20 65 74 53 51 4c 45 53 43  #define etSQLESC
a0420 41 50 45 20 20 31 30 20 2f 2a 20 53 74 72 69 6e  APE  10 /* Strin
a0430 67 73 20 77 69 74 68 20 27 5c 27 27 20 64 6f 75  gs with '\'' dou
a0440 62 6c 65 64 2e 20 20 25 71 20 2a 2f 0a 23 64 65  bled.  %q */.#de
a0450 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45  fine etSQLESCAPE
a0460 32 20 31 31 20 2f 2a 20 53 74 72 69 6e 67 73 20  2 11 /* Strings 
a0470 77 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65  with '\'' double
a0480 64 20 61 6e 64 20 65 6e 63 6c 6f 73 65 64 20 69  d and enclosed i
a0490 6e 20 27 27 2c 0a 20 20 20 20 20 20 20 20 20 20  n '',.          
a04a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a04b0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20 72 65  NULL pointers re
a04c0 70 6c 61 63 65 64 20 62 79 20 53 51 4c 20 4e 55  placed by SQL NU
a04d0 4c 4c 2e 20 20 25 51 20 2a 2f 0a 23 64 65 66 69  LL.  %Q */.#defi
a04e0 6e 65 20 65 74 54 4f 4b 45 4e 20 20 20 20 20 20  ne etTOKEN      
a04f0 31 32 20 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20  12 /* a pointer 
a0500 74 6f 20 61 20 54 6f 6b 65 6e 20 73 74 72 75 63  to a Token struc
a0510 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
a0520 65 74 53 52 43 4c 49 53 54 20 20 20 20 31 33 20  etSRCLIST    13 
a0530 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  /* a pointer to 
a0540 61 20 53 72 63 4c 69 73 74 20 2a 2f 0a 23 64 65  a SrcList */.#de
a0550 66 69 6e 65 20 65 74 50 4f 49 4e 54 45 52 20 20  fine etPOINTER  
a0560 20 20 31 34 20 2f 2a 20 54 68 65 20 25 70 20 63    14 /* The %p c
a0570 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 23 64 65  onversion */.#de
a0580 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45  fine etSQLESCAPE
a0590 33 20 31 35 20 2f 2a 20 25 77 20 2d 3e 20 53 74  3 15 /* %w -> St
a05a0 72 69 6e 67 73 20 77 69 74 68 20 27 5c 22 27 20  rings with '\"' 
a05b0 64 6f 75 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69  doubled */.#defi
a05c0 6e 65 20 65 74 4f 52 44 49 4e 41 4c 20 20 20 20  ne etORDINAL    
a05d0 31 36 20 2f 2a 20 25 72 20 2d 3e 20 31 73 74 2c  16 /* %r -> 1st,
a05e0 20 32 6e 64 2c 20 33 72 64 2c 20 34 74 68 2c 20   2nd, 3rd, 4th, 
a05f0 65 74 63 2e 20 20 45 6e 67 6c 69 73 68 20 6f 6e  etc.  English on
a0600 6c 79 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 65  ly */..#define e
a0610 74 49 4e 56 41 4c 49 44 20 20 20 20 20 30 20 2f  tINVALID     0 /
a0620 2a 20 41 6e 79 20 75 6e 72 65 63 6f 67 6e 69 7a  * Any unrecogniz
a0630 65 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79  ed conversion ty
a0640 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e  pe */.../*.** An
a0650 20 22 65 74 42 79 74 65 22 20 69 73 20 61 6e 20   "etByte" is an 
a0660 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20 76  8-bit unsigned v
a0670 61 6c 75 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  alue..*/.typedef
a0680 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 65   unsigned char e
a0690 74 42 79 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61  tByte;../*.** Ea
a06a0 63 68 20 62 75 69 6c 74 69 6e 20 63 6f 6e 76 65  ch builtin conve
a06b0 72 73 69 6f 6e 20 63 68 61 72 61 63 74 65 72 20  rsion character 
a06c0 28 65 78 3a 20 74 68 65 20 27 64 27 20 69 6e 20  (ex: the 'd' in 
a06d0 22 25 64 22 29 20 69 73 20 64 65 73 63 72 69 62  "%d") is describ
a06e0 65 64 0a 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74  ed.** by an inst
a06f0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
a0700 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a  owing structure.
a0710 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
a0720 74 20 65 74 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a  t et_info {   /*
a0730 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
a0740 75 74 20 65 61 63 68 20 66 6f 72 6d 61 74 20 66  ut each format f
a0750 69 65 6c 64 20 2a 2f 0a 20 20 63 68 61 72 20 66  ield */.  char f
a0760 6d 74 74 79 70 65 3b 20 20 20 20 20 20 20 20 20  mttype;         
a0770 20 20 20 2f 2a 20 54 68 65 20 66 6f 72 6d 61 74     /* The format
a0780 20 66 69 65 6c 64 20 63 6f 64 65 20 6c 65 74 74   field code lett
a0790 65 72 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 62  er */.  etByte b
a07a0 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ase;            
a07b0 20 2f 2a 20 54 68 65 20 62 61 73 65 20 66 6f 72   /* The base for
a07c0 20 72 61 64 69 78 20 63 6f 6e 76 65 72 73 69 6f   radix conversio
a07d0 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  n */.  etByte fl
a07e0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
a07f0 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f  /* One or more o
a0800 66 20 46 4c 41 47 5f 20 63 6f 6e 73 74 61 6e 74  f FLAG_ constant
a0810 73 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 65 74 42  s below */.  etB
a0820 79 74 65 20 74 79 70 65 3b 20 20 20 20 20 20 20  yte type;       
a0830 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73        /* Convers
a0840 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a  ion paradigm */.
a0850 20 20 65 74 42 79 74 65 20 63 68 61 72 73 65 74    etByte charset
a0860 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66  ;          /* Of
a0870 66 73 65 74 20 69 6e 74 6f 20 61 44 69 67 69 74  fset into aDigit
a0880 73 5b 5d 20 6f 66 20 74 68 65 20 64 69 67 69 74  s[] of the digit
a0890 73 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 65 74  s string */.  et
a08a0 42 79 74 65 20 70 72 65 66 69 78 3b 20 20 20 20  Byte prefix;    
a08b0 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
a08c0 20 69 6e 74 6f 20 61 50 72 65 66 69 78 5b 5d 20   into aPrefix[] 
a08d0 6f 66 20 74 68 65 20 70 72 65 66 69 78 20 73 74  of the prefix st
a08e0 72 69 6e 67 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66  ring */.} et_inf
a08f0 6f 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65  o;../*.** Allowe
a0900 64 20 76 61 6c 75 65 73 20 66 6f 72 20 65 74 5f  d values for et_
a0910 69 6e 66 6f 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64  info.flags.*/.#d
a0920 65 66 69 6e 65 20 46 4c 41 47 5f 53 49 47 4e 45  efine FLAG_SIGNE
a0930 44 20 20 31 20 20 20 20 20 2f 2a 20 54 72 75 65  D  1     /* True
a0940 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 74 6f   if the value to
a0950 20 63 6f 6e 76 65 72 74 20 69 73 20 73 69 67 6e   convert is sign
a0960 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c  ed */.#define FL
a0970 41 47 5f 49 4e 54 45 52 4e 20 20 32 20 20 20 20  AG_INTERN  2    
a0980 20 2f 2a 20 54 72 75 65 20 69 66 20 66 6f 72 20   /* True if for 
a0990 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c  internal use onl
a09a0 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41  y */.#define FLA
a09b0 47 5f 53 54 52 49 4e 47 20 20 34 20 20 20 20 20  G_STRING  4     
a09c0 2f 2a 20 41 6c 6c 6f 77 20 69 6e 66 69 6e 69 74  /* Allow infinit
a09d0 79 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a  y precision */..
a09e0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
a09f0 77 69 6e 67 20 74 61 62 6c 65 20 69 73 20 73 65  wing table is se
a0a00 61 72 63 68 65 64 20 6c 69 6e 65 61 72 6c 79 2c  arched linearly,
a0a10 20 73 6f 20 69 74 20 69 73 20 67 6f 6f 64 20 74   so it is good t
a0a20 6f 20 70 75 74 20 74 68 65 0a 2a 2a 20 6d 6f 73  o put the.** mos
a0a30 74 20 66 72 65 71 75 65 6e 74 6c 79 20 75 73 65  t frequently use
a0a40 64 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70  d conversion typ
a0a50 65 73 20 66 69 72 73 74 2e 0a 2a 2f 0a 73 74 61  es first..*/.sta
a0a60 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61  tic const char a
a0a70 44 69 67 69 74 73 5b 5d 20 3d 20 22 30 31 32 33  Digits[] = "0123
a0a80 34 35 36 37 38 39 41 42 43 44 45 46 30 31 32 33  456789ABCDEF0123
a0a90 34 35 36 37 38 39 61 62 63 64 65 66 22 3b 0a 73  456789abcdef";.s
a0aa0 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
a0ab0 20 61 50 72 65 66 69 78 5b 5d 20 3d 20 22 2d 78   aPrefix[] = "-x
a0ac0 30 5c 30 30 30 58 30 22 3b 0a 73 74 61 74 69 63  0\000X0";.static
a0ad0 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f 20 66   const et_info f
a0ae0 6d 74 69 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b  mtinfo[] = {.  {
a0af0 20 20 27 64 27 2c 20 31 30 2c 20 31 2c 20 65 74    'd', 10, 1, et
a0b00 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20  RADIX,      0,  
a0b10 30 20 7d 2c 0a 20 20 7b 20 20 27 73 27 2c 20 20  0 },.  {  's',  
a0b20 30 2c 20 34 2c 20 65 74 53 54 52 49 4e 47 2c 20  0, 4, etSTRING, 
a0b30 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b      0,  0 },.  {
a0b40 20 20 27 67 27 2c 20 20 30 2c 20 31 2c 20 65 74    'g',  0, 1, et
a0b50 47 45 4e 45 52 49 43 2c 20 20 20 20 33 30 2c 20  GENERIC,    30, 
a0b60 30 20 7d 2c 0a 20 20 7b 20 20 27 7a 27 2c 20 20  0 },.  {  'z',  
a0b70 30 2c 20 34 2c 20 65 74 44 59 4e 53 54 52 49 4e  0, 4, etDYNSTRIN
a0b80 47 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b  G,  0,  0 },.  {
a0b90 20 20 27 71 27 2c 20 20 30 2c 20 34 2c 20 65 74    'q',  0, 4, et
a0ba0 53 51 4c 45 53 43 41 50 45 2c 20 20 30 2c 20 20  SQLESCAPE,  0,  
a0bb0 30 20 7d 2c 0a 20 20 7b 20 20 27 51 27 2c 20 20  0 },.  {  'Q',  
a0bc0 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50  0, 4, etSQLESCAP
a0bd0 45 32 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b  E2, 0,  0 },.  {
a0be0 20 20 27 77 27 2c 20 20 30 2c 20 34 2c 20 65 74    'w',  0, 4, et
a0bf0 53 51 4c 45 53 43 41 50 45 33 2c 20 30 2c 20 20  SQLESCAPE3, 0,  
a0c00 30 20 7d 2c 0a 20 20 7b 20 20 27 63 27 2c 20 20  0 },.  {  'c',  
a0c10 30 2c 20 30 2c 20 65 74 43 48 41 52 58 2c 20 20  0, 0, etCHARX,  
a0c20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b      0,  0 },.  {
a0c30 20 20 27 6f 27 2c 20 20 38 2c 20 30 2c 20 65 74    'o',  8, 0, et
a0c40 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20  RADIX,      0,  
a0c50 32 20 7d 2c 0a 20 20 7b 20 20 27 75 27 2c 20 31  2 },.  {  'u', 1
a0c60 30 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20  0, 0, etRADIX,  
a0c70 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b      0,  0 },.  {
a0c80 20 20 27 78 27 2c 20 31 36 2c 20 30 2c 20 65 74    'x', 16, 0, et
a0c90 52 41 44 49 58 2c 20 20 20 20 20 20 31 36 2c 20  RADIX,      16, 
a0ca0 31 20 7d 2c 0a 20 20 7b 20 20 27 58 27 2c 20 31  1 },.  {  'X', 1
a0cb0 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20  6, 0, etRADIX,  
a0cc0 20 20 20 20 30 2c 20 20 34 20 7d 2c 0a 23 69 66      0,  4 },.#if
a0cd0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
a0ce0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
a0cf0 20 20 7b 20 20 27 66 27 2c 20 20 30 2c 20 31 2c    {  'f',  0, 1,
a0d00 20 65 74 46 4c 4f 41 54 2c 20 20 20 20 20 20 30   etFLOAT,      0
a0d10 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 65 27  ,  0 },.  {  'e'
a0d20 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 2c 20  ,  0, 1, etEXP, 
a0d30 20 20 20 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a         30, 0 },.
a0d40 20 20 7b 20 20 27 45 27 2c 20 20 30 2c 20 31 2c    {  'E',  0, 1,
a0d50 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 20 31   etEXP,        1
a0d60 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 47 27  4, 0 },.  {  'G'
a0d70 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e 45 52  ,  0, 1, etGENER
a0d80 49 43 2c 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a  IC,    14, 0 },.
a0d90 23 65 6e 64 69 66 0a 20 20 7b 20 20 27 69 27 2c  #endif.  {  'i',
a0da0 20 31 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c   10, 1, etRADIX,
a0db0 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20        0,  0 },. 
a0dc0 20 7b 20 20 27 6e 27 2c 20 20 30 2c 20 30 2c 20   {  'n',  0, 0, 
a0dd0 65 74 53 49 5a 45 2c 20 20 20 20 20 20 20 30 2c  etSIZE,       0,
a0de0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 25 27 2c    0 },.  {  '%',
a0df0 20 20 30 2c 20 30 2c 20 65 74 50 45 52 43 45 4e    0, 0, etPERCEN
a0e00 54 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20  T,    0,  0 },. 
a0e10 20 7b 20 20 27 70 27 2c 20 31 36 2c 20 30 2c 20   {  'p', 16, 0, 
a0e20 65 74 50 4f 49 4e 54 45 52 2c 20 20 20 20 30 2c  etPOINTER,    0,
a0e30 20 20 31 20 7d 2c 0a 0a 2f 2a 20 41 6c 6c 20 74    1 },../* All t
a0e40 68 65 20 72 65 73 74 20 68 61 76 65 20 74 68 65  he rest have the
a0e50 20 46 4c 41 47 5f 49 4e 54 45 52 4e 20 62 69 74   FLAG_INTERN bit
a0e60 20 73 65 74 20 61 6e 64 20 61 72 65 20 74 68 75   set and are thu
a0e70 73 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a  s for internal.*
a0e80 2a 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20  * use only */.  
a0e90 7b 20 20 27 54 27 2c 20 20 30 2c 20 32 2c 20 65  {  'T',  0, 2, e
a0ea0 74 54 4f 4b 45 4e 2c 20 20 20 20 20 20 30 2c 20  tTOKEN,      0, 
a0eb0 20 30 20 7d 2c 0a 20 20 7b 20 20 27 53 27 2c 20   0 },.  {  'S', 
a0ec0 20 30 2c 20 32 2c 20 65 74 53 52 43 4c 49 53 54   0, 2, etSRCLIST
a0ed0 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20  ,    0,  0 },.  
a0ee0 7b 20 20 27 72 27 2c 20 31 30 2c 20 33 2c 20 65  {  'r', 10, 3, e
a0ef0 74 4f 52 44 49 4e 41 4c 2c 20 20 20 20 30 2c 20  tORDINAL,    0, 
a0f00 20 30 20 7d 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   0 },.};../*.** 
a0f10 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  If SQLITE_OMIT_F
a0f20 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 69 73  LOATING_POINT is
a0f30 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 6e   defined, then n
a0f40 6f 6e 65 20 6f 66 20 74 68 65 20 66 6c 6f 61 74  one of the float
a0f50 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 63 6f 6e  ing point.** con
a0f60 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c 20 77 6f  versions will wo
a0f70 72 6b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  rk..*/.#ifndef S
a0f80 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
a0f90 49 4e 47 5f 50 4f 49 4e 54 0a 2f 2a 0a 2a 2a 20  ING_POINT./*.** 
a0fa0 22 2a 76 61 6c 22 20 69 73 20 61 20 64 6f 75 62  "*val" is a doub
a0fb0 6c 65 20 73 75 63 68 20 74 68 61 74 20 30 2e 31  le such that 0.1
a0fc0 20 3c 3d 20 2a 76 61 6c 20 3c 20 31 30 2e 30 0a   <= *val < 10.0.
a0fd0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 73  ** Return the as
a0fe0 63 69 69 20 63 6f 64 65 20 66 6f 72 20 74 68 65  cii code for the
a0ff0 20 6c 65 61 64 69 6e 67 20 64 69 67 69 74 20 6f   leading digit o
a1000 66 20 2a 76 61 6c 2c 20 74 68 65 6e 0a 2a 2a 20  f *val, then.** 
a1010 6d 75 6c 74 69 70 6c 79 20 22 2a 76 61 6c 22 20  multiply "*val" 
a1020 62 79 20 31 30 2e 30 20 74 6f 20 72 65 6e 6f 72  by 10.0 to renor
a1030 6d 61 6c 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 45 78  malize..**.** Ex
a1040 61 6d 70 6c 65 3a 0a 2a 2a 20 20 20 20 20 69 6e  ample:.**     in
a1050 70 75 74 3a 20 20 20 20 20 2a 76 61 6c 20 3d 20  put:     *val = 
a1060 33 2e 31 34 31 35 39 0a 2a 2a 20 20 20 20 20 6f  3.14159.**     o
a1070 75 74 70 75 74 3a 20 20 20 20 2a 76 61 6c 20 3d  utput:    *val =
a1080 20 31 2e 34 31 35 39 20 20 20 20 66 75 6e 63 74   1.4159    funct
a1090 69 6f 6e 20 72 65 74 75 72 6e 20 3d 20 27 33 27  ion return = '3'
a10a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 75 6e 74  .**.** The count
a10b0 65 72 20 2a 63 6e 74 20 69 73 20 69 6e 63 72 65  er *cnt is incre
a10c0 6d 65 6e 74 65 64 20 65 61 63 68 20 74 69 6d 65  mented each time
a10d0 2e 20 20 41 66 74 65 72 20 63 6f 75 6e 74 65 72  .  After counter
a10e0 20 65 78 63 65 65 64 73 0a 2a 2a 20 31 36 20 28   exceeds.** 16 (
a10f0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69  the number of si
a1100 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73  gnificant digits
a1110 20 69 6e 20 61 20 36 34 2d 62 69 74 20 66 6c 6f   in a 64-bit flo
a1120 61 74 29 20 27 30 27 20 69 73 0a 2a 2a 20 61 6c  at) '0' is.** al
a1130 77 61 79 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  ways returned..*
a1140 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 65 74  /.static char et
a1150 5f 67 65 74 64 69 67 69 74 28 4c 4f 4e 47 44 4f  _getdigit(LONGDO
a1160 55 42 4c 45 5f 54 59 50 45 20 2a 76 61 6c 2c 20  UBLE_TYPE *val, 
a1170 69 6e 74 20 2a 63 6e 74 29 7b 0a 20 20 69 6e 74  int *cnt){.  int
a1180 20 64 69 67 69 74 3b 0a 20 20 4c 4f 4e 47 44 4f   digit;.  LONGDO
a1190 55 42 4c 45 5f 54 59 50 45 20 64 3b 0a 20 20 69  UBLE_TYPE d;.  i
a11a0 66 28 20 28 2a 63 6e 74 29 2b 2b 20 3e 3d 20 31  f( (*cnt)++ >= 1
a11b0 36 20 29 20 72 65 74 75 72 6e 20 27 30 27 3b 0a  6 ) return '0';.
a11c0 20 20 64 69 67 69 74 20 3d 20 28 69 6e 74 29 2a    digit = (int)*
a11d0 76 61 6c 3b 0a 20 20 64 20 3d 20 64 69 67 69 74  val;.  d = digit
a11e0 3b 0a 20 20 64 69 67 69 74 20 2b 3d 20 27 30 27  ;.  digit += '0'
a11f0 3b 0a 20 20 2a 76 61 6c 20 3d 20 28 2a 76 61 6c  ;.  *val = (*val
a1200 20 2d 20 64 29 2a 31 30 2e 30 3b 0a 20 20 72 65   - d)*10.0;.  re
a1210 74 75 72 6e 20 28 63 68 61 72 29 64 69 67 69 74  turn (char)digit
a1220 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
a1230 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
a1240 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a  NG_POINT */../*.
a1250 2a 2a 20 41 70 70 65 6e 64 20 4e 20 73 70 61 63  ** Append N spac
a1260 65 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20  e characters to 
a1270 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
a1280 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74   buffer..*/.stat
a1290 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64 53 70  ic void appendSp
a12a0 61 63 65 28 53 74 72 41 63 63 75 6d 20 2a 70 41  ace(StrAccum *pA
a12b0 63 63 75 6d 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  ccum, int N){.  
a12c0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
a12d0 72 20 7a 53 70 61 63 65 73 5b 5d 20 3d 20 22 20  r zSpaces[] = " 
a12e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a12f0 20 20 20 20 20 20 20 20 20 20 20 20 22 3b 0a 20              ";. 
a1300 20 77 68 69 6c 65 28 20 4e 3e 3d 28 69 6e 74 29   while( N>=(int)
a1310 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d  sizeof(zSpaces)-
a1320 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  1 ){.    sqlite3
a1330 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
a1340 41 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20  Accum, zSpaces, 
a1350 73 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d  sizeof(zSpaces)-
a1360 31 29 3b 0a 20 20 20 20 4e 20 2d 3d 20 73 69 7a  1);.    N -= siz
a1370 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 3b 0a  eof(zSpaces)-1;.
a1380 20 20 7d 0a 20 20 69 66 28 20 4e 3e 30 20 29 7b    }.  if( N>0 ){
a1390 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41  .    sqlite3StrA
a13a0 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
a13b0 6d 2c 20 7a 53 70 61 63 65 73 2c 20 4e 29 3b 0a  m, zSpaces, N);.
a13c0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20    }.}../*.** On 
a13d0 6d 61 63 68 69 6e 65 73 20 77 69 74 68 20 61 20  machines with a 
a13e0 73 6d 61 6c 6c 20 73 74 61 63 6b 20 73 69 7a 65  small stack size
a13f0 2c 20 79 6f 75 20 63 61 6e 20 72 65 64 65 66 69  , you can redefi
a1400 6e 65 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45  ne the.** SQLITE
a1410 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20  _PRINT_BUF_SIZE 
a1420 74 6f 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20  to be less than 
a1430 33 35 30 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  350..*/.#ifndef 
a1440 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46  SQLITE_PRINT_BUF
a1450 5f 53 49 5a 45 0a 23 20 69 66 20 64 65 66 69 6e  _SIZE.# if defin
a1460 65 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f  ed(SQLITE_SMALL_
a1470 53 54 41 43 4b 29 0a 23 20 20 20 64 65 66 69 6e  STACK).#   defin
a1480 65 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42  e SQLITE_PRINT_B
a1490 55 46 5f 53 49 5a 45 20 35 30 0a 23 20 65 6c 73  UF_SIZE 50.# els
a14a0 65 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c  e.#   define SQL
a14b0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49  ITE_PRINT_BUF_SI
a14c0 5a 45 20 33 35 30 0a 23 20 65 6e 64 69 66 0a 23  ZE 350.# endif.#
a14d0 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 65 74  endif.#define et
a14e0 42 55 46 53 49 5a 45 20 53 51 4c 49 54 45 5f 50  BUFSIZE SQLITE_P
a14f0 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 20 2f  RINT_BUF_SIZE  /
a1500 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 75  * Size of the ou
a1510 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 0a  tput buffer */..
a1520 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70  /*.** The root p
a1530 72 6f 67 72 61 6d 2e 20 20 41 6c 6c 20 76 61 72  rogram.  All var
a1540 69 61 74 69 6f 6e 73 20 63 61 6c 6c 20 74 68 69  iations call thi
a1550 73 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e  s core..**.** IN
a1560 50 55 54 53 3a 0a 2a 2a 20 20 20 66 75 6e 63 20  PUTS:.**   func 
a1570 20 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e    This is a poin
a1580 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
a1590 6e 20 74 61 6b 69 6e 67 20 74 68 72 65 65 20 61  n taking three a
a15a0 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20  rguments.**     
a15b0 20 20 20 20 20 20 20 31 2e 20 41 20 70 6f 69 6e         1. A poin
a15c0 74 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e  ter to anything.
a15d0 20 20 53 61 6d 65 20 61 73 20 74 68 65 20 22 61    Same as the "a
a15e0 72 67 22 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  rg" parameter..*
a15f0 2a 20 20 20 20 20 20 20 20 20 20 20 20 32 2e 20  *            2. 
a1600 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
a1610 20 6c 69 73 74 20 6f 66 20 63 68 61 72 61 63 74   list of charact
a1620 65 72 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74  ers to be output
a1630 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
a1640 20 20 28 4e 6f 74 65 2c 20 74 68 69 73 20 6c 69    (Note, this li
a1650 73 74 20 69 73 20 4e 4f 54 20 6e 75 6c 6c 20 74  st is NOT null t
a1660 65 72 6d 69 6e 61 74 65 64 2e 29 0a 2a 2a 20 20  erminated.).**  
a1670 20 20 20 20 20 20 20 20 20 20 33 2e 20 41 6e 20            3. An 
a1680 69 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f  integer number o
a1690 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20  f characters to 
a16a0 62 65 20 6f 75 74 70 75 74 2e 0a 2a 2a 20 20 20  be output..**   
a16b0 20 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74              (Not
a16c0 65 3a 20 54 68 69 73 20 6e 75 6d 62 65 72 20 6d  e: This number m
a16d0 69 67 68 74 20 62 65 20 7a 65 72 6f 2e 29 0a 2a  ight be zero.).*
a16e0 2a 0a 2a 2a 20 20 20 61 72 67 20 20 20 20 54 68  *.**   arg    Th
a16f0 69 73 20 69 73 20 74 68 65 20 70 6f 69 6e 74 65  is is the pointe
a1700 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 77 68  r to anything wh
a1710 69 63 68 20 77 69 6c 6c 20 62 65 20 70 61 73 73  ich will be pass
a1720 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 20  ed as the.**    
a1730 20 20 20 20 20 20 66 69 72 73 74 20 61 72 67 75        first argu
a1740 6d 65 6e 74 20 74 6f 20 22 66 75 6e 63 22 2e 20  ment to "func". 
a1750 20 55 73 65 20 69 74 20 66 6f 72 20 77 68 61 74   Use it for what
a1760 65 76 65 72 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a  ever you like..*
a1770 2a 0a 2a 2a 20 20 20 66 6d 74 20 20 20 20 54 68  *.**   fmt    Th
a1780 69 73 20 69 73 20 74 68 65 20 66 6f 72 6d 61 74  is is the format
a1790 20 73 74 72 69 6e 67 2c 20 61 73 20 69 6e 20 74   string, as in t
a17a0 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 2e 0a  he usual print..
a17b0 2a 2a 0a 2a 2a 20 20 20 61 70 20 20 20 20 20 54  **.**   ap     T
a17c0 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72  his is a pointer
a17d0 20 74 6f 20 61 20 6c 69 73 74 20 6f 66 20 61 72   to a list of ar
a17e0 67 75 6d 65 6e 74 73 2e 20 20 53 61 6d 65 20 61  guments.  Same a
a17f0 73 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  s in.**         
a1800 20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20   vfprint..**.** 
a1810 4f 55 54 50 55 54 53 3a 0a 2a 2a 20 20 20 20 20  OUTPUTS:.**     
a1820 20 20 20 20 20 54 68 65 20 72 65 74 75 72 6e 20       The return 
a1830 76 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74  value is the tot
a1840 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61  al number of cha
a1850 72 61 63 74 65 72 73 20 73 65 6e 74 20 74 6f 0a  racters sent to.
a1860 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
a1870 66 75 6e 63 74 69 6f 6e 20 22 66 75 6e 63 22 2e  function "func".
a1880 20 20 52 65 74 75 72 6e 73 20 2d 31 20 6f 6e 20    Returns -1 on 
a1890 61 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e  a error..**.** N
a18a0 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64  ote that the ord
a18b0 65 72 20 69 6e 20 77 68 69 63 68 20 61 75 74 6f  er in which auto
a18c0 6d 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20  matic variables 
a18d0 61 72 65 20 64 65 63 6c 61 72 65 64 20 62 65 6c  are declared bel
a18e0 6f 77 0a 2a 2a 20 73 65 65 6d 73 20 74 6f 20 6d  ow.** seems to m
a18f0 61 6b 65 20 61 20 62 69 67 20 64 69 66 66 65 72  ake a big differ
a1900 65 6e 63 65 20 69 6e 20 64 65 74 65 72 6d 69 6e  ence in determin
a1910 69 6e 67 20 68 6f 77 20 66 61 73 74 20 74 68 69  ing how fast thi
a1920 73 20 62 65 61 73 74 0a 2a 2a 20 77 69 6c 6c 20  s beast.** will 
a1930 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  run..*/.SQLITE_P
a1940 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a1950 74 65 33 56 58 50 72 69 6e 74 66 28 0a 20 20 53  te3VXPrintf(.  S
a1960 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c  trAccum *pAccum,
a1970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1980 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20    /* Accumulate 
a1990 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a  results here */.
a19a0 20 20 69 6e 74 20 75 73 65 45 78 74 65 6e 64 65    int useExtende
a19b0 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
a19c0 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78       /* Allow ex
a19d0 74 65 6e 64 65 64 20 25 2d 63 6f 6e 76 65 72 73  tended %-convers
a19e0 69 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ions */.  const 
a19f0 63 68 61 72 20 2a 66 6d 74 2c 20 20 20 20 20 20  char *fmt,      
a1a00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a1a10 46 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f  Format string */
a1a20 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 20 20 20  .  va_list ap   
a1a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1a40 20 20 20 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e        /* argumen
a1a50 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63  ts */.){.  int c
a1a60 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a1a70 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68        /* Next ch
a1a80 61 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 66  aracter in the f
a1a90 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a  ormat string */.
a1aa0 20 20 63 68 61 72 20 2a 62 75 66 70 74 3b 20 20    char *bufpt;  
a1ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a1ac0 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63  Pointer to the c
a1ad0 6f 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72  onversion buffer
a1ae0 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 63 69 73   */.  int precis
a1af0 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ion;            
a1b00 20 2f 2a 20 50 72 65 63 69 73 69 6f 6e 20 6f 66   /* Precision of
a1b10 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 65   the current fie
a1b20 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67  ld */.  int leng
a1b30 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  th;             
a1b40 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
a1b50 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69  the field */.  i
a1b60 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20  nt idx;         
a1b70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 67            /* A g
a1b80 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 6c  eneral purpose l
a1b90 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
a1ba0 20 69 6e 74 20 77 69 64 74 68 3b 20 20 20 20 20   int width;     
a1bb0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
a1bc0 69 64 74 68 20 6f 66 20 74 68 65 20 63 75 72 72  idth of the curr
a1bd0 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65  ent field */.  e
a1be0 74 42 79 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a  tByte flag_leftj
a1bf0 75 73 74 69 66 79 3b 20 20 20 2f 2a 20 54 72 75  ustify;   /* Tru
a1c00 65 20 69 66 20 22 2d 22 20 66 6c 61 67 20 69 73  e if "-" flag is
a1c10 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74   present */.  et
a1c20 42 79 74 65 20 66 6c 61 67 5f 70 6c 75 73 73 69  Byte flag_plussi
a1c30 67 6e 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  gn;      /* True
a1c40 20 69 66 20 22 2b 22 20 66 6c 61 67 20 69 73 20   if "+" flag is 
a1c50 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
a1c60 79 74 65 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69  yte flag_blanksi
a1c70 67 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20  gn;     /* True 
a1c80 69 66 20 22 20 22 20 66 6c 61 67 20 69 73 20 70  if " " flag is p
a1c90 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79  resent */.  etBy
a1ca0 74 65 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74  te flag_alternat
a1cb0 65 66 6f 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69  eform; /* True i
a1cc0 66 20 22 23 22 20 66 6c 61 67 20 69 73 20 70 72  f "#" flag is pr
a1cd0 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74  esent */.  etByt
a1ce0 65 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b  e flag_altform2;
a1cf0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a1d00 20 22 21 22 20 66 6c 61 67 20 69 73 20 70 72 65   "!" flag is pre
a1d10 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
a1d20 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20   flag_zeropad;  
a1d30 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a1d40 66 69 65 6c 64 20 77 69 64 74 68 20 63 6f 6e 73  field width cons
a1d50 74 61 6e 74 20 73 74 61 72 74 73 20 77 69 74 68  tant starts with
a1d60 20 7a 65 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74   zero */.  etByt
a1d70 65 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20  e flag_long;    
a1d80 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a1d90 20 22 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65   "l" flag is pre
a1da0 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
a1db0 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20   flag_longlong; 
a1dc0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a1dd0 74 68 65 20 22 6c 6c 22 20 66 6c 61 67 20 69 73  the "ll" flag is
a1de0 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74   present */.  et
a1df0 42 79 74 65 20 64 6f 6e 65 3b 20 20 20 20 20 20  Byte done;      
a1e00 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
a1e10 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61   termination fla
a1e20 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 75 69  g */.  sqlite_ui
a1e30 6e 74 36 34 20 6c 6f 6e 67 76 61 6c 75 65 3b 20  nt64 longvalue; 
a1e40 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 69    /* Value for i
a1e50 6e 74 65 67 65 72 20 74 79 70 65 73 20 2a 2f 0a  nteger types */.
a1e60 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50    LONGDOUBLE_TYP
a1e70 45 20 72 65 61 6c 76 61 6c 75 65 3b 20 2f 2a 20  E realvalue; /* 
a1e80 56 61 6c 75 65 20 66 6f 72 20 72 65 61 6c 20 74  Value for real t
a1e90 79 70 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ypes */.  const 
a1ea0 65 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20  et_info *infop; 
a1eb0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
a1ec0 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  to the appropria
a1ed0 74 65 20 69 6e 66 6f 20 73 74 72 75 63 74 75 72  te info structur
a1ee0 65 20 2a 2f 0a 20 20 63 68 61 72 20 62 75 66 5b  e */.  char buf[
a1ef0 65 74 42 55 46 53 49 5a 45 5d 3b 20 20 20 20 20  etBUFSIZE];     
a1f00 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20    /* Conversion 
a1f10 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 68 61 72  buffer */.  char
a1f20 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20   prefix;        
a1f30 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 69 78         /* Prefix
a1f40 20 63 68 61 72 61 63 74 65 72 2e 20 20 22 2b 22   character.  "+"
a1f50 20 6f 72 20 22 2d 22 20 6f 72 20 22 20 22 20 6f   or "-" or " " o
a1f60 72 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 65 74 42  r '\0'. */.  etB
a1f70 79 74 65 20 78 74 79 70 65 20 3d 20 30 3b 20 20  yte xtype = 0;  
a1f80 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65          /* Conve
a1f90 72 73 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a  rsion paradigm *
a1fa0 2f 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 72 61  /.  char *zExtra
a1fb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
a1fc0 2a 20 45 78 74 72 61 20 6d 65 6d 6f 72 79 20 75  * Extra memory u
a1fd0 73 65 64 20 66 6f 72 20 65 74 54 43 4c 45 53 43  sed for etTCLESC
a1fe0 41 50 45 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  APE conversions 
a1ff0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
a2000 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
a2010 50 4f 49 4e 54 0a 20 20 69 6e 74 20 20 65 78 70  POINT.  int  exp
a2020 2c 20 65 32 3b 20 20 20 20 20 20 20 20 20 20 20  , e2;           
a2030 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 6f     /* exponent o
a2040 66 20 72 65 61 6c 20 6e 75 6d 62 65 72 73 20 2a  f real numbers *
a2050 2f 0a 20 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64  /.  double round
a2060 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  er;            /
a2070 2a 20 55 73 65 64 20 66 6f 72 20 72 6f 75 6e 64  * Used for round
a2080 69 6e 67 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  ing floating poi
a2090 6e 74 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 65  nt values */.  e
a20a0 74 42 79 74 65 20 66 6c 61 67 5f 64 70 3b 20 20  tByte flag_dp;  
a20b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
a20c0 65 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f 69  e if decimal poi
a20d0 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 73 68 6f  nt should be sho
a20e0 77 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66  wn */.  etByte f
a20f0 6c 61 67 5f 72 74 7a 3b 20 20 20 20 20 20 20 20  lag_rtz;        
a2100 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 72     /* True if tr
a2110 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 73 68 6f  ailing zeros sho
a2120 75 6c 64 20 62 65 20 72 65 6d 6f 76 65 64 20 2a  uld be removed *
a2130 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f  /.  etByte flag_
a2140 65 78 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f  exp;           /
a2150 2a 20 54 72 75 65 20 74 6f 20 66 6f 72 63 65 20  * True to force 
a2160 64 69 73 70 6c 61 79 20 6f 66 20 74 68 65 20 65  display of the e
a2170 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  xponent */.  int
a2180 20 6e 73 64 3b 20 20 20 20 20 20 20 20 20 20 20   nsd;           
a2190 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
a21a0 72 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74  r of significant
a21b0 20 64 69 67 69 74 73 20 72 65 74 75 72 6e 65 64   digits returned
a21c0 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 6c 65   */.#endif..  le
a21d0 6e 67 74 68 20 3d 20 30 3b 0a 20 20 62 75 66 70  ngth = 0;.  bufp
a21e0 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 3b 20 28  t = 0;.  for(; (
a21f0 63 3d 28 2a 66 6d 74 29 29 21 3d 30 3b 20 2b 2b  c=(*fmt))!=0; ++
a2200 66 6d 74 29 7b 0a 20 20 20 20 69 66 28 20 63 21  fmt){.    if( c!
a2210 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 69 6e  ='%' ){.      in
a2220 74 20 61 6d 74 3b 0a 20 20 20 20 20 20 62 75 66  t amt;.      buf
a2230 70 74 20 3d 20 28 63 68 61 72 20 2a 29 66 6d 74  pt = (char *)fmt
a2240 3b 0a 20 20 20 20 20 20 61 6d 74 20 3d 20 31 3b  ;.      amt = 1;
a2250 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63  .      while( (c
a2260 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 25 27 20  =(*++fmt))!='%' 
a2270 26 26 20 63 21 3d 30 20 29 20 61 6d 74 2b 2b 3b  && c!=0 ) amt++;
a2280 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
a2290 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
a22a0 63 75 6d 2c 20 62 75 66 70 74 2c 20 61 6d 74 29  cum, bufpt, amt)
a22b0 3b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30  ;.      if( c==0
a22c0 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
a22d0 20 20 20 20 69 66 28 20 28 63 3d 28 2a 2b 2b 66      if( (c=(*++f
a22e0 6d 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  mt))==0 ){.     
a22f0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a2300 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 22  Append(pAccum, "
a2310 25 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72  %", 1);.      br
a2320 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  eak;.    }.    /
a2330 2a 20 46 69 6e 64 20 6f 75 74 20 77 68 61 74 20  * Find out what 
a2340 66 6c 61 67 73 20 61 72 65 20 70 72 65 73 65 6e  flags are presen
a2350 74 20 2a 2f 0a 20 20 20 20 66 6c 61 67 5f 6c 65  t */.    flag_le
a2360 66 74 6a 75 73 74 69 66 79 20 3d 20 66 6c 61 67  ftjustify = flag
a2370 5f 70 6c 75 73 73 69 67 6e 20 3d 20 66 6c 61 67  _plussign = flag
a2380 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 0a 20 20  _blanksign = .  
a2390 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74     flag_alternat
a23a0 65 66 6f 72 6d 20 3d 20 66 6c 61 67 5f 61 6c 74  eform = flag_alt
a23b0 66 6f 72 6d 32 20 3d 20 66 6c 61 67 5f 7a 65 72  form2 = flag_zer
a23c0 6f 70 61 64 20 3d 20 30 3b 0a 20 20 20 20 64 6f  opad = 0;.    do
a23d0 6e 65 20 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a  ne = 0;.    do{.
a23e0 20 20 20 20 20 20 73 77 69 74 63 68 28 20 63 20        switch( c 
a23f0 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  ){.        case 
a2400 27 2d 27 3a 20 20 20 66 6c 61 67 5f 6c 65 66 74  '-':   flag_left
a2410 6a 75 73 74 69 66 79 20 3d 20 31 3b 20 20 20 20  justify = 1;    
a2420 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
a2430 63 61 73 65 20 27 2b 27 3a 20 20 20 66 6c 61 67  case '+':   flag
a2440 5f 70 6c 75 73 73 69 67 6e 20 3d 20 31 3b 20 20  _plussign = 1;  
a2450 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a2460 20 20 20 20 20 63 61 73 65 20 27 20 27 3a 20 20       case ' ':  
a2470 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20   flag_blanksign 
a2480 3d 20 31 3b 20 20 20 20 20 20 20 62 72 65 61 6b  = 1;       break
a2490 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
a24a0 23 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72  #':   flag_alter
a24b0 6e 61 74 65 66 6f 72 6d 20 3d 20 31 3b 20 20 20  nateform = 1;   
a24c0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
a24d0 61 73 65 20 27 21 27 3a 20 20 20 66 6c 61 67 5f  ase '!':   flag_
a24e0 61 6c 74 66 6f 72 6d 32 20 3d 20 31 3b 20 20 20  altform2 = 1;   
a24f0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a2500 20 20 20 20 63 61 73 65 20 27 30 27 3a 20 20 20      case '0':   
a2510 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d 20 31  flag_zeropad = 1
a2520 3b 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;         break;
a2530 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74  .        default
a2540 3a 20 20 20 20 64 6f 6e 65 20 3d 20 31 3b 20 20  :    done = 1;  
a2550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
a2560 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
a2570 20 20 7d 77 68 69 6c 65 28 20 21 64 6f 6e 65 20    }while( !done 
a2580 26 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21  && (c=(*++fmt))!
a2590 3d 30 20 29 3b 0a 20 20 20 20 2f 2a 20 47 65 74  =0 );.    /* Get
a25a0 20 74 68 65 20 66 69 65 6c 64 20 77 69 64 74 68   the field width
a25b0 20 2a 2f 0a 20 20 20 20 77 69 64 74 68 20 3d 20   */.    width = 
a25c0 30 3b 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2a  0;.    if( c=='*
a25d0 27 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68  ' ){.      width
a25e0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   = va_arg(ap,int
a25f0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 77 69 64  );.      if( wid
a2600 74 68 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  th<0 ){.        
a2610 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
a2620 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 77 69   = 1;.        wi
a2630 64 74 68 20 3d 20 2d 77 69 64 74 68 3b 0a 20 20  dth = -width;.  
a2640 20 20 20 20 7d 0a 20 20 20 20 20 20 63 20 3d 20      }.      c = 
a2650 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d 65 6c 73  *++fmt;.    }els
a2660 65 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  e{.      while( 
a2670 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27  c>='0' && c<='9'
a2680 20 29 7b 0a 20 20 20 20 20 20 20 20 77 69 64 74   ){.        widt
a2690 68 20 3d 20 77 69 64 74 68 2a 31 30 20 2b 20 63  h = width*10 + c
a26a0 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20   - '0';.        
a26b0 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20  c = *++fmt;.    
a26c0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
a26d0 28 20 77 69 64 74 68 20 3e 20 65 74 42 55 46 53  ( width > etBUFS
a26e0 49 5a 45 2d 31 30 20 29 7b 0a 20 20 20 20 20 20  IZE-10 ){.      
a26f0 77 69 64 74 68 20 3d 20 65 74 42 55 46 53 49 5a  width = etBUFSIZ
a2700 45 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  E-10;.    }.    
a2710 2f 2a 20 47 65 74 20 74 68 65 20 70 72 65 63 69  /* Get the preci
a2720 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20  sion */.    if( 
a2730 63 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20  c=='.' ){.      
a2740 70 72 65 63 69 73 69 6f 6e 20 3d 20 30 3b 0a 20  precision = 0;. 
a2750 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b       c = *++fmt;
a2760 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 2a  .      if( c=='*
a2770 27 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 65  ' ){.        pre
a2780 63 69 73 69 6f 6e 20 3d 20 76 61 5f 61 72 67 28  cision = va_arg(
a2790 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20  ap,int);.       
a27a0 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30   if( precision<0
a27b0 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d   ) precision = -
a27c0 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20  precision;.     
a27d0 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
a27e0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a27f0 20 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30      while( c>='0
a2800 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20  ' && c<='9' ){. 
a2810 20 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69           precisi
a2820 6f 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e 2a 31  on = precision*1
a2830 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20  0 + c - '0';.   
a2840 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d         c = *++fm
a2850 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
a2860 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
a2870 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20        precision 
a2880 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  = -1;.    }.    
a2890 2f 2a 20 47 65 74 20 74 68 65 20 63 6f 6e 76 65  /* Get the conve
a28a0 72 73 69 6f 6e 20 74 79 70 65 20 6d 6f 64 69 66  rsion type modif
a28b0 69 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 63  ier */.    if( c
a28c0 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 66  =='l' ){.      f
a28d0 6c 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20  lag_long = 1;.  
a28e0 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a      c = *++fmt;.
a28f0 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27        if( c=='l'
a2900 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67   ){.        flag
a2910 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20  _longlong = 1;. 
a2920 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d         c = *++fm
a2930 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  t;.      }else{.
a2940 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e          flag_lon
a2950 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20  glong = 0;.     
a2960 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
a2970 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20      flag_long = 
a2980 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20  flag_longlong = 
a2990 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20  0;.    }.    /* 
a29a0 46 65 74 63 68 20 74 68 65 20 69 6e 66 6f 20 65  Fetch the info e
a29b0 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 65  ntry for the fie
a29c0 6c 64 20 2a 2f 0a 20 20 20 20 69 6e 66 6f 70 20  ld */.    infop 
a29d0 3d 20 26 66 6d 74 69 6e 66 6f 5b 30 5d 3b 0a 20  = &fmtinfo[0];. 
a29e0 20 20 20 78 74 79 70 65 20 3d 20 65 74 49 4e 56     xtype = etINV
a29f0 41 4c 49 44 3b 0a 20 20 20 20 66 6f 72 28 69 64  ALID;.    for(id
a2a00 78 3d 30 3b 20 69 64 78 3c 41 72 72 61 79 53 69  x=0; idx<ArraySi
a2a10 7a 65 28 66 6d 74 69 6e 66 6f 29 3b 20 69 64 78  ze(fmtinfo); idx
a2a20 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63  ++){.      if( c
a2a30 3d 3d 66 6d 74 69 6e 66 6f 5b 69 64 78 5d 2e 66  ==fmtinfo[idx].f
a2a40 6d 74 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20  mttype ){.      
a2a50 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e    infop = &fmtin
a2a60 66 6f 5b 69 64 78 5d 3b 0a 20 20 20 20 20 20 20  fo[idx];.       
a2a70 20 69 66 28 20 75 73 65 45 78 74 65 6e 64 65 64   if( useExtended
a2a80 20 7c 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67   || (infop->flag
a2a90 73 20 26 20 46 4c 41 47 5f 49 4e 54 45 52 4e 29  s & FLAG_INTERN)
a2aa0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
a2ab0 20 78 74 79 70 65 20 3d 20 69 6e 66 6f 70 2d 3e   xtype = infop->
a2ac0 74 79 70 65 3b 0a 20 20 20 20 20 20 20 20 7d 65  type;.        }e
a2ad0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72  lse{.          r
a2ae0 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d  eturn;.        }
a2af0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
a2b00 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
a2b10 20 20 7a 45 78 74 72 61 20 3d 20 30 3b 0a 0a 0a    zExtra = 0;...
a2b20 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65      /* Limit the
a2b30 20 70 72 65 63 69 73 69 6f 6e 20 74 6f 20 70 72   precision to pr
a2b40 65 76 65 6e 74 20 6f 76 65 72 66 6c 6f 77 69 6e  event overflowin
a2b50 67 20 62 75 66 5b 5d 20 64 75 72 69 6e 67 20 63  g buf[] during c
a2b60 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20  onversion */.   
a2b70 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 65   if( precision>e
a2b80 74 42 55 46 53 49 5a 45 2d 34 30 20 26 26 20 28  tBUFSIZE-40 && (
a2b90 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46  infop->flags & F
a2ba0 4c 41 47 5f 53 54 52 49 4e 47 29 3d 3d 30 20 29  LAG_STRING)==0 )
a2bb0 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 6f  {.      precisio
a2bc0 6e 20 3d 20 65 74 42 55 46 53 49 5a 45 2d 34 30  n = etBUFSIZE-40
a2bd0 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a  ;.    }..    /*.
a2be0 20 20 20 20 2a 2a 20 41 74 20 74 68 69 73 20 70      ** At this p
a2bf0 6f 69 6e 74 2c 20 76 61 72 69 61 62 6c 65 73 20  oint, variables 
a2c00 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  are initialized 
a2c10 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20 20  as follows:.    
a2c20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67  **.    **   flag
a2c30 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 20  _alternateform  
a2c40 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20          TRUE if 
a2c50 61 20 27 23 27 20 69 73 20 70 72 65 73 65 6e 74  a '#' is present
a2c60 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f  ..    **   flag_
a2c70 61 6c 74 66 6f 72 6d 32 20 20 20 20 20 20 20 20  altform2        
a2c80 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61         TRUE if a
a2c90 20 27 21 27 20 69 73 20 70 72 65 73 65 6e 74 2e   '!' is present.
a2ca0 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 70  .    **   flag_p
a2cb0 6c 75 73 73 69 67 6e 20 20 20 20 20 20 20 20 20  lussign         
a2cc0 20 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20        TRUE if a 
a2cd0 27 2b 27 20 69 73 20 70 72 65 73 65 6e 74 2e 0a  '+' is present..
a2ce0 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 65      **   flag_le
a2cf0 66 74 6a 75 73 74 69 66 79 20 20 20 20 20 20 20  ftjustify       
a2d00 20 20 20 20 20 54 52 55 45 20 69 66 20 61 20 27       TRUE if a '
a2d10 2d 27 20 69 73 20 70 72 65 73 65 6e 74 20 6f 72  -' is present or
a2d20 20 69 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 20   if the.    **  
a2d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2d40 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69 65               fie
a2d50 6c 64 20 77 69 64 74 68 20 77 61 73 20 6e 65 67  ld width was neg
a2d60 61 74 69 76 65 2e 0a 20 20 20 20 2a 2a 20 20 20  ative..    **   
a2d70 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 20 20 20  flag_zeropad    
a2d80 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45              TRUE
a2d90 20 69 66 20 74 68 65 20 77 69 64 74 68 20 62 65   if the width be
a2da0 67 61 6e 20 77 69 74 68 20 30 2e 0a 20 20 20 20  gan with 0..    
a2db0 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 20  **   flag_long  
a2dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2dd0 20 54 52 55 45 20 69 66 20 74 68 65 20 6c 65 74   TRUE if the let
a2de0 74 65 72 20 27 6c 27 20 28 65 6c 6c 29 20 70 72  ter 'l' (ell) pr
a2df0 65 66 69 78 65 64 0a 20 20 20 20 2a 2a 20 20 20  efixed.    **   
a2e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2e10 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
a2e20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 72 61  conversion chara
a2e30 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 20 20 66  cter..    **   f
a2e40 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 20 20 20  lag_longlong    
a2e50 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20             TRUE 
a2e60 69 66 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c  if the letter 'l
a2e70 6c 27 20 28 65 6c 6c 20 65 6c 6c 29 20 70 72 65  l' (ell ell) pre
a2e80 66 69 78 65 64 0a 20 20 20 20 2a 2a 20 20 20 20  fixed.    **    
a2e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2ea0 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 63             the c
a2eb0 6f 6e 76 65 72 73 69 6f 6e 20 63 68 61 72 61 63  onversion charac
a2ec0 74 65 72 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c  ter..    **   fl
a2ed0 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 20 20 20  ag_blanksign    
a2ee0 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69            TRUE i
a2ef0 66 20 61 20 27 20 27 20 69 73 20 70 72 65 73 65  f a ' ' is prese
a2f00 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 77 69 64  nt..    **   wid
a2f10 74 68 20 20 20 20 20 20 20 20 20 20 20 20 20 20  th              
a2f20 20 20 20 20 20 20 20 20 20 54 68 65 20 73 70 65           The spe
a2f30 63 69 66 69 65 64 20 66 69 65 6c 64 20 77 69 64  cified field wid
a2f40 74 68 2e 20 20 54 68 69 73 20 69 73 0a 20 20 20  th.  This is.   
a2f50 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20   **             
a2f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2f70 20 20 61 6c 77 61 79 73 20 6e 6f 6e 2d 6e 65 67    always non-neg
a2f80 61 74 69 76 65 2e 20 20 5a 65 72 6f 20 69 73 20  ative.  Zero is 
a2f90 74 68 65 20 64 65 66 61 75 6c 74 2e 0a 20 20 20  the default..   
a2fa0 20 2a 2a 20 20 20 70 72 65 63 69 73 69 6f 6e 20   **   precision 
a2fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2fc0 20 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20    The specified 
a2fd0 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 65 20  precision.  The 
a2fe0 64 65 66 61 75 6c 74 0a 20 20 20 20 2a 2a 20 20  default.    **  
a2ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3000 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20               is 
a3010 2d 31 2e 0a 20 20 20 20 2a 2a 20 20 20 78 74 79  -1..    **   xty
a3020 70 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  pe              
a3030 20 20 20 20 20 20 20 20 20 54 68 65 20 63 6c 61           The cla
a3040 73 73 20 6f 66 20 74 68 65 20 63 6f 6e 76 65 72  ss of the conver
a3050 73 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20 20 20 69  sion..    **   i
a3060 6e 66 6f 70 20 20 20 20 20 20 20 20 20 20 20 20  nfop            
a3070 20 20 20 20 20 20 20 20 20 20 20 50 6f 69 6e 74             Point
a3080 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70  er to the approp
a3090 72 69 61 74 65 20 69 6e 66 6f 20 73 74 72 75 63  riate info struc
a30a0 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 77  t..    */.    sw
a30b0 69 74 63 68 28 20 78 74 79 70 65 20 29 7b 0a 20  itch( xtype ){. 
a30c0 20 20 20 20 20 63 61 73 65 20 65 74 50 4f 49 4e       case etPOIN
a30d0 54 45 52 3a 0a 20 20 20 20 20 20 20 20 66 6c 61  TER:.        fla
a30e0 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 73 69 7a  g_longlong = siz
a30f0 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65  eof(char*)==size
a3100 6f 66 28 69 36 34 29 3b 0a 20 20 20 20 20 20 20  of(i64);.       
a3110 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 73 69 7a   flag_long = siz
a3120 65 6f 66 28 63 68 61 72 2a 29 3d 3d 73 69 7a 65  eof(char*)==size
a3130 6f 66 28 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20  of(long int);.  
a3140 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68        /* Fall th
a3150 72 6f 75 67 68 20 69 6e 74 6f 20 74 68 65 20 6e  rough into the n
a3160 65 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20  ext case */.    
a3170 20 20 63 61 73 65 20 65 74 4f 52 44 49 4e 41 4c    case etORDINAL
a3180 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 52  :.      case etR
a3190 41 44 49 58 3a 0a 20 20 20 20 20 20 20 20 69 66  ADIX:.        if
a31a0 28 20 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20 26  ( infop->flags &
a31b0 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 29 7b 0a   FLAG_SIGNED ){.
a31c0 20 20 20 20 20 20 20 20 20 20 69 36 34 20 76 3b            i64 v;
a31d0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66  .          if( f
a31e0 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b 0a  lag_longlong ){.
a31f0 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d 20              v = 
a3200 76 61 5f 61 72 67 28 61 70 2c 69 36 34 29 3b 0a  va_arg(ap,i64);.
a3210 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20            }else 
a3220 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 29 7b  if( flag_long ){
a3230 0a 20 20 20 20 20 20 20 20 20 20 20 20 76 20 3d  .            v =
a3240 20 76 61 5f 61 72 67 28 61 70 2c 6c 6f 6e 67 20   va_arg(ap,long 
a3250 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  int);.          
a3260 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a3270 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 61 70     v = va_arg(ap
a3280 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20  ,int);.         
a3290 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
a32a0 20 76 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20   v<0 ){.        
a32b0 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20      longvalue = 
a32c0 2d 76 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  -v;.            
a32d0 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20  prefix = '-';.  
a32e0 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
a32f0 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76             longv
a3300 61 6c 75 65 20 3d 20 76 3b 0a 20 20 20 20 20 20  alue = v;.      
a3310 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 70        if( flag_p
a3320 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20 20 20  lussign )       
a3330 20 70 72 65 66 69 78 20 3d 20 27 2b 27 3b 0a 20   prefix = '+';. 
a3340 20 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20             else 
a3350 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69  if( flag_blanksi
a3360 67 6e 20 29 20 20 70 72 65 66 69 78 20 3d 20 27  gn )  prefix = '
a3370 20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ';.            
a3380 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20  else            
a3390 20 20 20 20 20 20 20 20 20 20 20 70 72 65 66 69             prefi
a33a0 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  x = 0;.         
a33b0 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65   }.        }else
a33c0 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
a33d0 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 29 7b  flag_longlong ){
a33e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e  .            lon
a33f0 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28  gvalue = va_arg(
a3400 61 70 2c 75 36 34 29 3b 0a 20 20 20 20 20 20 20  ap,u64);.       
a3410 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61     }else if( fla
a3420 67 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20  g_long ){.      
a3430 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20        longvalue 
a3440 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69  = va_arg(ap,unsi
a3450 67 6e 65 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a  gned long int);.
a3460 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
a3470 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e  .            lon
a3480 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28  gvalue = va_arg(
a3490 61 70 2c 75 6e 73 69 67 6e 65 64 20 69 6e 74 29  ap,unsigned int)
a34a0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
a34b0 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
a34c0 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
a34d0 20 20 20 20 20 20 69 66 28 20 6c 6f 6e 67 76 61        if( longva
a34e0 6c 75 65 3d 3d 30 20 29 20 66 6c 61 67 5f 61 6c  lue==0 ) flag_al
a34f0 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20 30 3b  ternateform = 0;
a3500 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61  .        if( fla
a3510 67 5f 7a 65 72 6f 70 61 64 20 26 26 20 70 72 65  g_zeropad && pre
a3520 63 69 73 69 6f 6e 3c 77 69 64 74 68 2d 28 70 72  cision<width-(pr
a3530 65 66 69 78 21 3d 30 29 20 29 7b 0a 20 20 20 20  efix!=0) ){.    
a3540 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20        precision 
a3550 3d 20 77 69 64 74 68 2d 28 70 72 65 66 69 78 21  = width-(prefix!
a3560 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  =0);.        }. 
a3570 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 26         bufpt = &
a3580 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 31 5d  buf[etBUFSIZE-1]
a3590 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 74  ;.        if( xt
a35a0 79 70 65 3d 3d 65 74 4f 52 44 49 4e 41 4c 20 29  ype==etORDINAL )
a35b0 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74  {.          stat
a35c0 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4f  ic const char zO
a35d0 72 64 5b 5d 20 3d 20 22 74 68 73 74 6e 64 72 64  rd[] = "thstndrd
a35e0 22 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ";.          int
a35f0 20 78 20 3d 20 28 69 6e 74 29 28 6c 6f 6e 67 76   x = (int)(longv
a3600 61 6c 75 65 20 25 20 31 30 29 3b 0a 20 20 20 20  alue % 10);.    
a3610 20 20 20 20 20 20 69 66 28 20 78 3e 3d 34 20 7c        if( x>=4 |
a3620 7c 20 28 6c 6f 6e 67 76 61 6c 75 65 2f 31 30 29  | (longvalue/10)
a3630 25 31 30 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  %10==1 ){.      
a3640 20 20 20 20 20 20 78 20 3d 20 30 3b 0a 20 20 20        x = 0;.   
a3650 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a3660 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 5a 45     buf[etBUFSIZE
a3670 2d 33 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b  -3] = zOrd[x*2];
a3680 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 5b 65  .          buf[e
a3690 74 42 55 46 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f  tBUFSIZE-2] = zO
a36a0 72 64 5b 78 2a 32 2b 31 5d 3b 0a 20 20 20 20 20  rd[x*2+1];.     
a36b0 20 20 20 20 20 62 75 66 70 74 20 2d 3d 20 32 3b       bufpt -= 2;
a36c0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a36d0 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20 72     {.          r
a36e0 65 67 69 73 74 65 72 20 63 6f 6e 73 74 20 63 68  egister const ch
a36f0 61 72 20 2a 63 73 65 74 3b 20 20 20 20 20 20 2f  ar *cset;      /
a3700 2a 20 55 73 65 20 72 65 67 69 73 74 65 72 73 20  * Use registers 
a3710 66 6f 72 20 73 70 65 65 64 20 2a 2f 0a 20 20 20  for speed */.   
a3720 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20         register 
a3730 69 6e 74 20 62 61 73 65 3b 0a 20 20 20 20 20 20  int base;.      
a3740 20 20 20 20 63 73 65 74 20 3d 20 26 61 44 69 67      cset = &aDig
a3750 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73  its[infop->chars
a3760 65 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62  et];.          b
a3770 61 73 65 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73  ase = infop->bas
a3780 65 3b 0a 20 20 20 20 20 20 20 20 20 20 64 6f 7b  e;.          do{
a3790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a37a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a37b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
a37c0 6e 76 65 72 74 20 74 6f 20 61 73 63 69 69 20 2a  nvert to ascii *
a37d0 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28  /.            *(
a37e0 2d 2d 62 75 66 70 74 29 20 3d 20 63 73 65 74 5b  --bufpt) = cset[
a37f0 6c 6f 6e 67 76 61 6c 75 65 25 62 61 73 65 5d 3b  longvalue%base];
a3800 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e  .            lon
a3810 67 76 61 6c 75 65 20 3d 20 6c 6f 6e 67 76 61 6c  gvalue = longval
a3820 75 65 2f 62 61 73 65 3b 0a 20 20 20 20 20 20 20  ue/base;.       
a3830 20 20 20 7d 77 68 69 6c 65 28 20 6c 6f 6e 67 76     }while( longv
a3840 61 6c 75 65 3e 30 20 29 3b 0a 20 20 20 20 20 20  alue>0 );.      
a3850 20 20 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67    }.        leng
a3860 74 68 20 3d 20 28 69 6e 74 29 28 26 62 75 66 5b  th = (int)(&buf[
a3870 65 74 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66  etBUFSIZE-1]-buf
a3880 70 74 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  pt);.        for
a3890 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2d 6c  (idx=precision-l
a38a0 65 6e 67 74 68 3b 20 69 64 78 3e 30 3b 20 69 64  ength; idx>0; id
a38b0 78 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20  x--){.          
a38c0 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 27 30 27  *(--bufpt) = '0'
a38d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
a38e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a38f0 20 5a 65 72 6f 20 70 61 64 20 2a 2f 0a 20 20 20   Zero pad */.   
a3900 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
a3910 66 28 20 70 72 65 66 69 78 20 29 20 2a 28 2d 2d  f( prefix ) *(--
a3920 62 75 66 70 74 29 20 3d 20 70 72 65 66 69 78 3b  bufpt) = prefix;
a3930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a3940 2a 20 41 64 64 20 73 69 67 6e 20 2a 2f 0a 20 20  * Add sign */.  
a3950 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 61        if( flag_a
a3960 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 26 26 20  lternateform && 
a3970 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 20 29 7b  infop->prefix ){
a3980 20 20 20 20 20 20 2f 2a 20 41 64 64 20 22 30 22        /* Add "0"
a3990 20 6f 72 20 22 30 78 22 20 2a 2f 0a 20 20 20 20   or "0x" */.    
a39a0 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
a39b0 20 2a 70 72 65 3b 0a 20 20 20 20 20 20 20 20 20   *pre;.         
a39c0 20 63 68 61 72 20 78 3b 0a 20 20 20 20 20 20 20   char x;.       
a39d0 20 20 20 70 72 65 20 3d 20 26 61 50 72 65 66 69     pre = &aPrefi
a39e0 78 5b 69 6e 66 6f 70 2d 3e 70 72 65 66 69 78 5d  x[infop->prefix]
a39f0 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
a3a00 3b 20 28 78 3d 28 2a 70 72 65 29 29 21 3d 30 3b  ; (x=(*pre))!=0;
a3a10 20 70 72 65 2b 2b 29 20 2a 28 2d 2d 62 75 66 70   pre++) *(--bufp
a3a20 74 29 20 3d 20 78 3b 0a 20 20 20 20 20 20 20 20  t) = x;.        
a3a30 7d 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68  }.        length
a3a40 20 3d 20 28 69 6e 74 29 28 26 62 75 66 5b 65 74   = (int)(&buf[et
a3a50 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74  BUFSIZE-1]-bufpt
a3a60 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
a3a70 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 46  ;.      case etF
a3a80 4c 4f 41 54 3a 0a 20 20 20 20 20 20 63 61 73 65  LOAT:.      case
a3a90 20 65 74 45 58 50 3a 0a 20 20 20 20 20 20 63 61   etEXP:.      ca
a3aa0 73 65 20 65 74 47 45 4e 45 52 49 43 3a 0a 20 20  se etGENERIC:.  
a3ab0 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 20        realvalue 
a3ac0 3d 20 76 61 5f 61 72 67 28 61 70 2c 64 6f 75 62  = va_arg(ap,doub
a3ad0 6c 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  le);.#ifndef SQL
a3ae0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
a3af0 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 20 20  G_POINT.        
a3b00 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20  if( precision<0 
a3b10 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 36 3b  ) precision = 6;
a3b20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20           /* Set 
a3b30 64 65 66 61 75 6c 74 20 70 72 65 63 69 73 69 6f  default precisio
a3b40 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  n */.        if(
a3b50 20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46   precision>etBUF
a3b60 53 49 5a 45 2f 32 2d 31 30 20 29 20 70 72 65 63  SIZE/2-10 ) prec
a3b70 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a  ision = etBUFSIZ
a3b80 45 2f 32 2d 31 30 3b 0a 20 20 20 20 20 20 20 20  E/2-10;.        
a3b90 69 66 28 20 72 65 61 6c 76 61 6c 75 65 3c 30 2e  if( realvalue<0.
a3ba0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  0 ){.          r
a3bb0 65 61 6c 76 61 6c 75 65 20 3d 20 2d 72 65 61 6c  ealvalue = -real
a3bc0 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 20  value;.         
a3bd0 20 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20   prefix = '-';. 
a3be0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
a3bf0 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
a3c00 5f 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20  _plussign )     
a3c10 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b       prefix = '+
a3c20 27 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73  ';.          els
a3c30 65 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b  e if( flag_blank
a3c40 73 69 67 6e 20 29 20 20 20 20 70 72 65 66 69 78  sign )    prefix
a3c50 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20   = ' ';.        
a3c60 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20    else          
a3c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
a3c80 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20  refix = 0;.     
a3c90 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
a3ca0 20 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49   xtype==etGENERI
a3cb0 43 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3e 30  C && precision>0
a3cc0 20 29 20 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a   ) precision--;.
a3cd0 23 69 66 20 30 0a 20 20 20 20 20 20 20 20 2f 2a  #if 0.        /*
a3ce0 20 52 6f 75 6e 64 69 6e 67 20 77 6f 72 6b 73 20   Rounding works 
a3cf0 6c 69 6b 65 20 42 53 44 20 77 68 65 6e 20 74 68  like BSD when th
a3d00 65 20 63 6f 6e 73 74 61 6e 74 20 30 2e 34 39 39  e constant 0.499
a3d10 39 20 69 73 20 75 73 65 64 2e 20 20 57 69 65 72  9 is used.  Wier
a3d20 64 21 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f  d! */.        fo
a3d30 72 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c  r(idx=precision,
a3d40 20 72 6f 75 6e 64 65 72 3d 30 2e 34 39 39 39 3b   rounder=0.4999;
a3d50 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72   idx>0; idx--, r
a3d60 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 3b 0a 23 65  ounder*=0.1);.#e
a3d70 6c 73 65 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  lse.        /* I
a3d80 74 20 6d 61 6b 65 73 20 6d 6f 72 65 20 73 65 6e  t makes more sen
a3d90 73 65 20 74 6f 20 75 73 65 20 30 2e 35 20 2a 2f  se to use 0.5 */
a3da0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78  .        for(idx
a3db0 3d 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e  =precision, roun
a3dc0 64 65 72 3d 30 2e 35 3b 20 69 64 78 3e 30 3b 20  der=0.5; idx>0; 
a3dd0 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d  idx--, rounder*=
a3de0 30 2e 31 29 7b 7d 0a 23 65 6e 64 69 66 0a 20 20  0.1){}.#endif.  
a3df0 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d        if( xtype=
a3e00 3d 65 74 46 4c 4f 41 54 20 29 20 72 65 61 6c 76  =etFLOAT ) realv
a3e10 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b  alue += rounder;
a3e20 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 72 6d  .        /* Norm
a3e30 61 6c 69 7a 65 20 72 65 61 6c 76 61 6c 75 65 20  alize realvalue 
a3e40 74 6f 20 77 69 74 68 69 6e 20 31 30 2e 30 20 3e  to within 10.0 >
a3e50 20 72 65 61 6c 76 61 6c 75 65 20 3e 3d 20 31 2e   realvalue >= 1.
a3e60 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 65 78 70  0 */.        exp
a3e70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66   = 0;.        if
a3e80 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 28  ( sqlite3IsNaN((
a3e90 64 6f 75 62 6c 65 29 72 65 61 6c 76 61 6c 75 65  double)realvalue
a3ea0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  ) ){.          b
a3eb0 75 66 70 74 20 3d 20 22 4e 61 4e 22 3b 0a 20 20  ufpt = "NaN";.  
a3ec0 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a3ed0 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72   3;.          br
a3ee0 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
a3ef0 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76         if( realv
a3f00 61 6c 75 65 3e 30 2e 30 20 29 7b 0a 20 20 20 20  alue>0.0 ){.    
a3f10 20 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61        while( rea
a3f20 6c 76 61 6c 75 65 3e 3d 31 65 33 32 20 26 26 20  lvalue>=1e32 && 
a3f30 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c  exp<=350 ){ real
a3f40 76 61 6c 75 65 20 2a 3d 20 31 65 2d 33 32 3b 20  value *= 1e-32; 
a3f50 65 78 70 2b 3d 33 32 3b 20 7d 0a 20 20 20 20 20  exp+=32; }.     
a3f60 20 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c       while( real
a3f70 76 61 6c 75 65 3e 3d 31 65 38 20 26 26 20 65 78  value>=1e8 && ex
a3f80 70 3c 3d 33 35 30 20 29 7b 20 72 65 61 6c 76 61  p<=350 ){ realva
a3f90 6c 75 65 20 2a 3d 20 31 65 2d 38 3b 20 65 78 70  lue *= 1e-8; exp
a3fa0 2b 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20 20 20  +=8; }.         
a3fb0 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c 75   while( realvalu
a3fc0 65 3e 3d 31 30 2e 30 20 26 26 20 65 78 70 3c 3d  e>=10.0 && exp<=
a3fd0 33 35 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65  350 ){ realvalue
a3fe0 20 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20   *= 0.1; exp++; 
a3ff0 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  }.          whil
a4000 65 28 20 72 65 61 6c 76 61 6c 75 65 3c 31 65 2d  e( realvalue<1e-
a4010 38 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a  8 ){ realvalue *
a4020 3d 20 31 65 38 3b 20 65 78 70 2d 3d 38 3b 20 7d  = 1e8; exp-=8; }
a4030 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
a4040 28 20 72 65 61 6c 76 61 6c 75 65 3c 31 2e 30 20  ( realvalue<1.0 
a4050 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
a4060 31 30 2e 30 3b 20 65 78 70 2d 2d 3b 20 7d 0a 20  10.0; exp--; }. 
a4070 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78 70           if( exp
a4080 3e 33 35 30 20 29 7b 0a 20 20 20 20 20 20 20 20  >350 ){.        
a4090 20 20 20 20 69 66 28 20 70 72 65 66 69 78 3d 3d      if( prefix==
a40a0 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  '-' ){.         
a40b0 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 2d 49       bufpt = "-I
a40c0 6e 66 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nf";.           
a40d0 20 7d 65 6c 73 65 20 69 66 28 20 70 72 65 66 69   }else if( prefi
a40e0 78 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 20 20  x=='+' ){.      
a40f0 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
a4100 22 2b 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20  "+Inf";.        
a4110 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a4120 20 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d           bufpt =
a4130 20 22 49 6e 66 22 3b 0a 20 20 20 20 20 20 20 20   "Inf";.        
a4140 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
a4150 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c 69 74    length = sqlit
a4160 65 33 53 74 72 6c 65 6e 33 30 28 62 75 66 70 74  e3Strlen30(bufpt
a4170 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  );.            b
a4180 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
a4190 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
a41a0 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b      bufpt = buf;
a41b0 0a 20 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20  .        /*.    
a41c0 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 66 69      ** If the fi
a41d0 65 6c 64 20 74 79 70 65 20 69 73 20 65 74 47 45  eld type is etGE
a41e0 4e 45 52 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76  NERIC, then conv
a41f0 65 72 74 20 74 6f 20 65 69 74 68 65 72 20 65 74  ert to either et
a4200 45 58 50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  EXP.        ** o
a4210 72 20 65 74 46 4c 4f 41 54 2c 20 61 73 20 61 70  r etFLOAT, as ap
a4220 70 72 6f 70 72 69 61 74 65 2e 0a 20 20 20 20 20  propriate..     
a4230 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c     */.        fl
a4240 61 67 5f 65 78 70 20 3d 20 78 74 79 70 65 3d 3d  ag_exp = xtype==
a4250 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 69  etEXP;.        i
a4260 66 28 20 78 74 79 70 65 21 3d 65 74 46 4c 4f 41  f( xtype!=etFLOA
a4270 54 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  T ){.          r
a4280 65 61 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e  ealvalue += roun
a4290 64 65 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69  der;.          i
a42a0 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30  f( realvalue>=10
a42b0 2e 30 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20  .0 ){ realvalue 
a42c0 2a 3d 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d  *= 0.1; exp++; }
a42d0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a42e0 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74     if( xtype==et
a42f0 47 45 4e 45 52 49 43 20 29 7b 0a 20 20 20 20 20  GENERIC ){.     
a4300 20 20 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20       flag_rtz = 
a4310 21 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66  !flag_alternatef
a4320 6f 72 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69  orm;.          i
a4330 66 28 20 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70  f( exp<-4 || exp
a4340 3e 70 72 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20  >precision ){.  
a4350 20 20 20 20 20 20 20 20 20 20 78 74 79 70 65 20            xtype 
a4360 3d 20 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20  = etEXP;.       
a4370 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a4380 20 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20        precision 
a4390 3d 20 70 72 65 63 69 73 69 6f 6e 20 2d 20 65 78  = precision - ex
a43a0 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78  p;.            x
a43b0 74 79 70 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a  type = etFLOAT;.
a43c0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
a43d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a43e0 20 20 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20       flag_rtz = 
a43f0 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
a4400 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d       if( xtype==
a4410 65 74 45 58 50 20 29 7b 0a 20 20 20 20 20 20 20  etEXP ){.       
a4420 20 20 20 65 32 20 3d 20 30 3b 0a 20 20 20 20 20     e2 = 0;.     
a4430 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a4440 20 20 20 20 65 32 20 3d 20 65 78 70 3b 0a 20 20      e2 = exp;.  
a4450 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a4460 6e 73 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  nsd = 0;.       
a4470 20 66 6c 61 67 5f 64 70 20 3d 20 28 70 72 65 63   flag_dp = (prec
a4480 69 73 69 6f 6e 3e 30 20 3f 31 3a 30 29 20 7c 20  ision>0 ?1:0) | 
a4490 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
a44a0 72 6d 20 7c 20 66 6c 61 67 5f 61 6c 74 66 6f 72  rm | flag_altfor
a44b0 6d 32 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  m2;.        /* T
a44c0 68 65 20 73 69 67 6e 20 69 6e 20 66 72 6f 6e 74  he sign in front
a44d0 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a   of the number *
a44e0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72  /.        if( pr
a44f0 65 66 69 78 20 29 7b 0a 20 20 20 20 20 20 20 20  efix ){.        
a4500 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 70    *(bufpt++) = p
a4510 72 65 66 69 78 3b 0a 20 20 20 20 20 20 20 20 7d  refix;.        }
a4520 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 69 67 69  .        /* Digi
a4530 74 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  ts prior to the 
a4540 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f  decimal point */
a4550 0a 20 20 20 20 20 20 20 20 69 66 28 20 65 32 3c  .        if( e2<
a4560 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2a  0 ){.          *
a4570 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b  (bufpt++) = '0';
a4580 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
a4590 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20            for(; 
a45a0 65 32 3e 3d 30 3b 20 65 32 2d 2d 29 7b 0a 20 20  e2>=0; e2--){.  
a45b0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
a45c0 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67  t++) = et_getdig
a45d0 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e  it(&realvalue,&n
a45e0 73 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  sd);.          }
a45f0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a4600 20 20 20 2f 2a 20 54 68 65 20 64 65 63 69 6d 61     /* The decima
a4610 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20  l point */.     
a4620 20 20 20 69 66 28 20 66 6c 61 67 5f 64 70 20 29     if( flag_dp )
a4630 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75  {.          *(bu
a4640 66 70 74 2b 2b 29 20 3d 20 27 2e 27 3b 0a 20 20  fpt++) = '.';.  
a4650 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a4660 2f 2a 20 22 30 22 20 64 69 67 69 74 73 20 61 66  /* "0" digits af
a4670 74 65 72 20 74 68 65 20 64 65 63 69 6d 61 6c 20  ter the decimal 
a4680 70 6f 69 6e 74 20 62 75 74 20 62 65 66 6f 72 65  point but before
a4690 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20 20   the first.     
a46a0 20 20 20 2a 2a 20 73 69 67 6e 69 66 69 63 61 6e     ** significan
a46b0 74 20 64 69 67 69 74 20 6f 66 20 74 68 65 20 6e  t digit of the n
a46c0 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 20  umber */.       
a46d0 20 66 6f 72 28 65 32 2b 2b 3b 20 65 32 3c 30 3b   for(e2++; e2<0;
a46e0 20 70 72 65 63 69 73 69 6f 6e 2d 2d 2c 20 65 32   precision--, e2
a46f0 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  ++){.          a
a4700 73 73 65 72 74 28 20 70 72 65 63 69 73 69 6f 6e  ssert( precision
a4710 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  >0 );.          
a4720 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27  *(bufpt++) = '0'
a4730 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a4740 20 20 20 20 2f 2a 20 53 69 67 6e 69 66 69 63 61      /* Significa
a4750 6e 74 20 64 69 67 69 74 73 20 61 66 74 65 72 20  nt digits after 
a4760 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e  the decimal poin
a4770 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69  t */.        whi
a4780 6c 65 28 20 28 70 72 65 63 69 73 69 6f 6e 2d 2d  le( (precision--
a4790 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  )>0 ){.         
a47a0 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65 74   *(bufpt++) = et
a47b0 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 6c 76  _getdigit(&realv
a47c0 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 20 20  alue,&nsd);.    
a47d0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a      }.        /*
a47e0 20 52 65 6d 6f 76 65 20 74 72 61 69 6c 69 6e 67   Remove trailing
a47f0 20 7a 65 72 6f 73 20 61 6e 64 20 74 68 65 20 22   zeros and the "
a4800 2e 22 20 69 66 20 6e 6f 20 64 69 67 69 74 73 20  ." if no digits 
a4810 66 6f 6c 6c 6f 77 20 74 68 65 20 22 2e 22 20 2a  follow the "." *
a4820 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c  /.        if( fl
a4830 61 67 5f 72 74 7a 20 26 26 20 66 6c 61 67 5f 64  ag_rtz && flag_d
a4840 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77  p ){.          w
a4850 68 69 6c 65 28 20 62 75 66 70 74 5b 2d 31 5d 3d  hile( bufpt[-1]=
a4860 3d 27 30 27 20 29 20 2a 28 2d 2d 62 75 66 70 74  ='0' ) *(--bufpt
a4870 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  ) = 0;.         
a4880 20 61 73 73 65 72 74 28 20 62 75 66 70 74 3e 62   assert( bufpt>b
a4890 75 66 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  uf );.          
a48a0 69 66 28 20 62 75 66 70 74 5b 2d 31 5d 3d 3d 27  if( bufpt[-1]=='
a48b0 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  .' ){.          
a48c0 20 20 69 66 28 20 66 6c 61 67 5f 61 6c 74 66 6f    if( flag_altfo
a48d0 72 6d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20  rm2 ){.         
a48e0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
a48f0 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20  = '0';.         
a4900 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a4910 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70          *(--bufp
a4920 74 29 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  t) = 0;.        
a4930 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
a4940 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
a4950 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 22      /* Add the "
a4960 65 4e 4e 4e 22 20 73 75 66 66 69 78 20 2a 2f 0a  eNNN" suffix */.
a4970 20 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67          if( flag
a4980 5f 65 78 70 20 7c 7c 20 78 74 79 70 65 3d 3d 65  _exp || xtype==e
a4990 74 45 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20  tEXP ){.        
a49a0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 61    *(bufpt++) = a
a49b0 44 69 67 69 74 73 5b 69 6e 66 6f 70 2d 3e 63 68  Digits[infop->ch
a49c0 61 72 73 65 74 5d 3b 0a 20 20 20 20 20 20 20 20  arset];.        
a49d0 20 20 69 66 28 20 65 78 70 3c 30 20 29 7b 0a 20    if( exp<0 ){. 
a49e0 20 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66             *(buf
a49f0 70 74 2b 2b 29 20 3d 20 27 2d 27 3b 20 65 78 70  pt++) = '-'; exp
a4a00 20 3d 20 2d 65 78 70 3b 0a 20 20 20 20 20 20 20   = -exp;.       
a4a10 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a4a20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
a4a30 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20   = '+';.        
a4a40 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
a4a50 28 20 65 78 70 3e 3d 31 30 30 20 29 7b 0a 20 20  ( exp>=100 ){.  
a4a60 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
a4a70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29 28 28 65  t++) = (char)((e
a4a80 78 70 2f 31 30 30 29 2b 27 30 27 29 3b 20 20 20  xp/100)+'0');   
a4a90 20 20 20 20 20 2f 2a 20 31 30 30 27 73 20 64 69       /* 100's di
a4aa0 67 69 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  git */.         
a4ab0 20 20 20 65 78 70 20 25 3d 20 31 30 30 3b 0a 20     exp %= 100;. 
a4ac0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
a4ad0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
a4ae0 3d 20 28 63 68 61 72 29 28 65 78 70 2f 31 30 2b  = (char)(exp/10+
a4af0 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20  '0');           
a4b00 20 20 2f 2a 20 31 30 27 73 20 64 69 67 69 74 20    /* 10's digit 
a4b10 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62  */.          *(b
a4b20 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29  ufpt++) = (char)
a4b30 28 65 78 70 25 31 30 2b 27 30 27 29 3b 20 20 20  (exp%10+'0');   
a4b40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 27 73            /* 1's
a4b50 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20 20   digit */.      
a4b60 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 62 75 66    }.        *buf
a4b70 70 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 20  pt = 0;..       
a4b80 20 2f 2a 20 54 68 65 20 63 6f 6e 76 65 72 74 65   /* The converte
a4b90 64 20 6e 75 6d 62 65 72 20 69 73 20 69 6e 20 62  d number is in b
a4ba0 75 66 5b 5d 20 61 6e 64 20 7a 65 72 6f 20 74 65  uf[] and zero te
a4bb0 72 6d 69 6e 61 74 65 64 2e 20 4f 75 74 70 75 74  rminated. Output
a4bc0 20 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20   it..        ** 
a4bd0 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6e 75  Note that the nu
a4be0 6d 62 65 72 20 69 73 20 69 6e 20 74 68 65 20 75  mber is in the u
a4bf0 73 75 61 6c 20 6f 72 64 65 72 2c 20 6e 6f 74 20  sual order, not 
a4c00 72 65 76 65 72 73 65 64 20 61 73 20 77 69 74 68  reversed as with
a4c10 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65  .        ** inte
a4c20 67 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 73 2e  ger conversions.
a4c30 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 65 6e 67   */.        leng
a4c40 74 68 20 3d 20 28 69 6e 74 29 28 62 75 66 70 74  th = (int)(bufpt
a4c50 2d 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20 62  -buf);.        b
a4c60 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a 20 20 20  ufpt = buf;..   
a4c70 20 20 20 20 20 2f 2a 20 53 70 65 63 69 61 6c 20       /* Special 
a4c80 63 61 73 65 3a 20 20 41 64 64 20 6c 65 61 64 69  case:  Add leadi
a4c90 6e 67 20 7a 65 72 6f 73 20 69 66 20 74 68 65 20  ng zeros if the 
a4ca0 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 66 6c 61  flag_zeropad fla
a4cb0 67 20 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  g is.        ** 
a4cc0 73 65 74 20 61 6e 64 20 77 65 20 61 72 65 20 6e  set and we are n
a4cd0 6f 74 20 6c 65 66 74 20 6a 75 73 74 69 66 69 65  ot left justifie
a4ce0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28  d */.        if(
a4cf0 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26 26   flag_zeropad &&
a4d00 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69   !flag_leftjusti
a4d10 66 79 20 26 26 20 6c 65 6e 67 74 68 20 3c 20 77  fy && length < w
a4d20 69 64 74 68 29 7b 0a 20 20 20 20 20 20 20 20 20  idth){.         
a4d30 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20   int i;.        
a4d40 20 20 69 6e 74 20 6e 50 61 64 20 3d 20 77 69 64    int nPad = wid
a4d50 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a 20 20 20  th - length;.   
a4d60 20 20 20 20 20 20 20 66 6f 72 28 69 3d 77 69 64         for(i=wid
a4d70 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20 69 2d 2d  th; i>=nPad; i--
a4d80 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  ){.            b
a4d90 75 66 70 74 5b 69 5d 20 3d 20 62 75 66 70 74 5b  ufpt[i] = bufpt[
a4da0 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20 20 20 20  i-nPad];.       
a4db0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
a4dc0 20 3d 20 70 72 65 66 69 78 21 3d 30 3b 0a 20 20   = prefix!=0;.  
a4dd0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e          while( n
a4de0 50 61 64 2d 2d 20 29 20 62 75 66 70 74 5b 69 2b  Pad-- ) bufpt[i+
a4df0 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20  +] = '0';.      
a4e00 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64      length = wid
a4e10 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65  th;.        }.#e
a4e20 6e 64 69 66 0a 20 20 20 20 20 20 20 20 62 72 65  ndif.        bre
a4e30 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65  ak;.      case e
a4e40 74 53 49 5a 45 3a 0a 20 20 20 20 20 20 20 20 2a  tSIZE:.        *
a4e50 28 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 2a 29  (va_arg(ap,int*)
a4e60 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e 43 68 61  ) = pAccum->nCha
a4e70 72 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74  r;.        lengt
a4e80 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20  h = width = 0;. 
a4e90 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a4ea0 20 20 20 20 63 61 73 65 20 65 74 50 45 52 43 45      case etPERCE
a4eb0 4e 54 3a 0a 20 20 20 20 20 20 20 20 62 75 66 5b  NT:.        buf[
a4ec0 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20 20 20 20  0] = '%';.      
a4ed0 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20    bufpt = buf;. 
a4ee0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a4ef0 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  1;.        break
a4f00 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 43  ;.      case etC
a4f10 48 41 52 58 3a 0a 20 20 20 20 20 20 20 20 63 20  HARX:.        c 
a4f20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29  = va_arg(ap,int)
a4f30 3b 0a 20 20 20 20 20 20 20 20 62 75 66 5b 30 5d  ;.        buf[0]
a4f40 20 3d 20 28 63 68 61 72 29 63 3b 0a 20 20 20 20   = (char)c;.    
a4f50 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f      if( precisio
a4f60 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  n>=0 ){.        
a4f70 20 20 66 6f 72 28 69 64 78 3d 31 3b 20 69 64 78    for(idx=1; idx
a4f80 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69 64 78 2b  <precision; idx+
a4f90 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d 20 28 63  +) buf[idx] = (c
a4fa0 68 61 72 29 63 3b 0a 20 20 20 20 20 20 20 20 20  har)c;.         
a4fb0 20 6c 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73   length = precis
a4fc0 69 6f 6e 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  ion;.        }el
a4fd0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65  se{.          le
a4fe0 6e 67 74 68 20 3d 31 3b 0a 20 20 20 20 20 20 20  ngth =1;.       
a4ff0 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70 74   }.        bufpt
a5000 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20   = buf;.        
a5010 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
a5020 65 20 65 74 53 54 52 49 4e 47 3a 0a 20 20 20 20  e etSTRING:.    
a5030 20 20 63 61 73 65 20 65 74 44 59 4e 53 54 52 49    case etDYNSTRI
a5040 4e 47 3a 0a 20 20 20 20 20 20 20 20 62 75 66 70  NG:.        bufp
a5050 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 63 68  t = va_arg(ap,ch
a5060 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ar*);.        if
a5070 28 20 62 75 66 70 74 3d 3d 30 20 29 7b 0a 20 20  ( bufpt==0 ){.  
a5080 20 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20          bufpt = 
a5090 22 22 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  "";.        }els
a50a0 65 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 44  e if( xtype==etD
a50b0 59 4e 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20  YNSTRING ){.    
a50c0 20 20 20 20 20 20 7a 45 78 74 72 61 20 3d 20 62        zExtra = b
a50d0 75 66 70 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ufpt;.        }.
a50e0 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63          if( prec
a50f0 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20  ision>=0 ){.    
a5100 20 20 20 20 20 20 66 6f 72 28 6c 65 6e 67 74 68        for(length
a5110 3d 30 3b 20 6c 65 6e 67 74 68 3c 70 72 65 63 69  =0; length<preci
a5120 73 69 6f 6e 20 26 26 20 62 75 66 70 74 5b 6c 65  sion && bufpt[le
a5130 6e 67 74 68 5d 3b 20 6c 65 6e 67 74 68 2b 2b 29  ngth]; length++)
a5140 7b 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  {}.        }else
a5150 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67  {.          leng
a5160 74 68 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  th = sqlite3Strl
a5170 65 6e 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20  en30(bufpt);.   
a5180 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
a5190 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
a51a0 20 65 74 53 51 4c 45 53 43 41 50 45 3a 0a 20 20   etSQLESCAPE:.  
a51b0 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 53      case etSQLES
a51c0 43 41 50 45 32 3a 0a 20 20 20 20 20 20 63 61 73  CAPE2:.      cas
a51d0 65 20 65 74 53 51 4c 45 53 43 41 50 45 33 3a 20  e etSQLESCAPE3: 
a51e0 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 2c  {.        int i,
a51f0 20 6a 2c 20 6b 2c 20 6e 2c 20 69 73 6e 75 6c 6c   j, k, n, isnull
a5200 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 65  ;.        int ne
a5210 65 64 51 75 6f 74 65 3b 0a 20 20 20 20 20 20 20  edQuote;.       
a5220 20 63 68 61 72 20 63 68 3b 0a 20 20 20 20 20 20   char ch;.      
a5230 20 20 63 68 61 72 20 71 20 3d 20 28 28 78 74 79    char q = ((xty
a5240 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 33  pe==etSQLESCAPE3
a5250 29 3f 27 22 27 3a 27 5c 27 27 29 3b 20 20 20 2f  )?'"':'\'');   /
a5260 2a 20 51 75 6f 74 65 20 63 68 61 72 61 63 74 65  * Quote characte
a5270 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61  r */.        cha
a5280 72 20 2a 65 73 63 61 72 67 20 3d 20 76 61 5f 61  r *escarg = va_a
a5290 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20  rg(ap,char*);.  
a52a0 20 20 20 20 20 20 69 73 6e 75 6c 6c 20 3d 20 65        isnull = e
a52b0 73 63 61 72 67 3d 3d 30 3b 0a 20 20 20 20 20 20  scarg==0;.      
a52c0 20 20 69 66 28 20 69 73 6e 75 6c 6c 20 29 20 65    if( isnull ) e
a52d0 73 63 61 72 67 20 3d 20 28 78 74 79 70 65 3d 3d  scarg = (xtype==
a52e0 65 74 53 51 4c 45 53 43 41 50 45 32 20 3f 20 22  etSQLESCAPE2 ? "
a52f0 4e 55 4c 4c 22 20 3a 20 22 28 4e 55 4c 4c 29 22  NULL" : "(NULL)"
a5300 29 3b 0a 20 20 20 20 20 20 20 20 6b 20 3d 20 70  );.        k = p
a5310 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20  recision;.      
a5320 20 20 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68    for(i=n=0; (ch
a5330 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30 20 26  =escarg[i])!=0 &
a5340 26 20 6b 21 3d 30 3b 20 69 2b 2b 2c 20 6b 2d 2d  & k!=0; i++, k--
a5350 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
a5360 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a 20   ch==q )  n++;. 
a5370 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a5380 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69 73   needQuote = !is
a5390 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d 65  null && xtype==e
a53a0 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20 20  tSQLESCAPE2;.   
a53b0 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31 20       n += i + 1 
a53c0 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a 20  + needQuote*2;. 
a53d0 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74 42         if( n>etB
a53e0 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20  UFSIZE ){.      
a53f0 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78 74      bufpt = zExt
a5400 72 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ra = sqlite3Mall
a5410 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 20  oc( n );.       
a5420 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20     if( bufpt==0 
a5430 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
a5440 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69  Accum->mallocFai
a5450 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  led = 1;.       
a5460 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
a5470 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a5480 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a5490 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20    bufpt = buf;. 
a54a0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a54b0 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20   j = 0;.        
a54c0 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20  if( needQuote ) 
a54d0 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a  bufpt[j++] = q;.
a54e0 20 20 20 20 20 20 20 20 6b 20 3d 20 69 3b 0a 20          k = i;. 
a54f0 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
a5500 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  i<k; i++){.     
a5510 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20       bufpt[j++] 
a5520 3d 20 63 68 20 3d 20 65 73 63 61 72 67 5b 69 5d  = ch = escarg[i]
a5530 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
a5540 63 68 3d 3d 71 20 29 20 62 75 66 70 74 5b 6a 2b  ch==q ) bufpt[j+
a5550 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20 20  +] = ch;.       
a5560 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e   }.        if( n
a5570 65 65 64 51 75 6f 74 65 20 29 20 62 75 66 70 74  eedQuote ) bufpt
a5580 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20 20  [j++] = q;.     
a5590 20 20 20 62 75 66 70 74 5b 6a 5d 20 3d 20 30 3b     bufpt[j] = 0;
a55a0 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
a55b0 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20  = j;.        /* 
a55c0 54 68 65 20 70 72 65 63 69 73 69 6f 6e 20 69 6e  The precision in
a55d0 20 25 71 20 61 6e 64 20 25 51 20 6d 65 61 6e 73   %q and %Q means
a55e0 20 68 6f 77 20 6d 61 6e 79 20 69 6e 70 75 74 20   how many input 
a55f0 63 68 61 72 61 63 74 65 72 73 20 74 6f 0a 20 20  characters to.  
a5600 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 75 6d 65        ** consume
a5610 2c 20 6e 6f 74 20 74 68 65 20 6c 65 6e 67 74 68  , not the length
a5620 20 6f 66 20 74 68 65 20 6f 75 74 70 75 74 2e 2e   of the output..
a5630 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 28  ..        ** if(
a5640 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 26 26   precision>=0 &&
a5650 20 70 72 65 63 69 73 69 6f 6e 3c 6c 65 6e 67 74   precision<lengt
a5660 68 20 29 20 6c 65 6e 67 74 68 20 3d 20 70 72 65  h ) length = pre
a5670 63 69 73 69 6f 6e 3b 20 2a 2f 0a 20 20 20 20 20  cision; */.     
a5680 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a5690 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 54  }.      case etT
a56a0 4f 4b 45 4e 3a 20 7b 0a 20 20 20 20 20 20 20 20  OKEN: {.        
a56b0 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 3d 20  Token *pToken = 
a56c0 76 61 5f 61 72 67 28 61 70 2c 20 54 6f 6b 65 6e  va_arg(ap, Token
a56d0 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  *);.        if( 
a56e0 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20  pToken ){.      
a56f0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
a5700 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
a5710 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  , (const char*)p
a5720 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54 6f 6b 65 6e  Token->z, pToken
a5730 2d 3e 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ->n);.        }.
a5740 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d          length =
a5750 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20   width = 0;.    
a5760 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
a5770 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 65 74   }.      case et
a5780 53 52 43 4c 49 53 54 3a 20 7b 0a 20 20 20 20 20  SRCLIST: {.     
a5790 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
a57a0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 53 72   = va_arg(ap, Sr
a57b0 63 4c 69 73 74 2a 29 3b 0a 20 20 20 20 20 20 20  cList*);.       
a57c0 20 69 6e 74 20 6b 20 3d 20 76 61 5f 61 72 67 28   int k = va_arg(
a57d0 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
a57e0 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
a57f0 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26  _item *pItem = &
a5800 70 53 72 63 2d 3e 61 5b 6b 5d 3b 0a 20 20 20 20  pSrc->a[k];.    
a5810 20 20 20 20 61 73 73 65 72 74 28 20 6b 3e 3d 30      assert( k>=0
a5820 20 26 26 20 6b 3c 70 53 72 63 2d 3e 6e 53 72 63   && k<pSrc->nSrc
a5830 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
a5840 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  pItem->zDatabase
a5850 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
a5860 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
a5870 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65  end(pAccum, pIte
a5880 6d 2d 3e 7a 44 61 74 61 62 61 73 65 2c 20 2d 31  m->zDatabase, -1
a5890 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
a58a0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
a58b0 6e 64 28 70 41 63 63 75 6d 2c 20 22 2e 22 2c 20  nd(pAccum, ".", 
a58c0 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  1);.        }.  
a58d0 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
a58e0 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63  AccumAppend(pAcc
a58f0 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  um, pItem->zName
a5900 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 6c  , -1);.        l
a5910 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20  ength = width = 
a5920 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  0;.        break
a5930 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
a5940 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
a5950 20 20 20 61 73 73 65 72 74 28 20 78 74 79 70 65     assert( xtype
a5960 3d 3d 65 74 49 4e 56 41 4c 49 44 20 29 3b 0a 20  ==etINVALID );. 
a5970 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
a5980 20 20 20 20 20 7d 0a 20 20 20 20 7d 2f 2a 20 45       }.    }/* E
a5990 6e 64 20 73 77 69 74 63 68 20 6f 76 65 72 20 74  nd switch over t
a59a0 68 65 20 66 6f 72 6d 61 74 20 74 79 70 65 20 2a  he format type *
a59b0 2f 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  /.    /*.    ** 
a59c0 54 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  The text of the 
a59d0 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f  conversion is po
a59e0 69 6e 74 65 64 20 74 6f 20 62 79 20 22 62 75 66  inted to by "buf
a59f0 70 74 22 20 61 6e 64 20 69 73 0a 20 20 20 20 2a  pt" and is.    *
a5a00 2a 20 22 6c 65 6e 67 74 68 22 20 63 68 61 72 61  * "length" chara
a5a10 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65  cters long.  The
a5a20 20 66 69 65 6c 64 20 77 69 64 74 68 20 69 73 20   field width is 
a5a30 22 77 69 64 74 68 22 2e 20 20 44 6f 0a 20 20 20  "width".  Do.   
a5a40 20 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a   ** the output..
a5a50 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
a5a60 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79  flag_leftjustify
a5a70 20 29 7b 0a 20 20 20 20 20 20 72 65 67 69 73 74   ){.      regist
a5a80 65 72 20 69 6e 74 20 6e 73 70 61 63 65 3b 0a 20  er int nspace;. 
a5a90 20 20 20 20 20 6e 73 70 61 63 65 20 3d 20 77 69       nspace = wi
a5aa0 64 74 68 2d 6c 65 6e 67 74 68 3b 0a 20 20 20 20  dth-length;.    
a5ab0 20 20 69 66 28 20 6e 73 70 61 63 65 3e 30 20 29    if( nspace>0 )
a5ac0 7b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e 64  {.        append
a5ad0 53 70 61 63 65 28 70 41 63 63 75 6d 2c 20 6e 73  Space(pAccum, ns
a5ae0 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  pace);.      }. 
a5af0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 65 6e     }.    if( len
a5b00 67 74 68 3e 30 20 29 7b 0a 20 20 20 20 20 20 73  gth>0 ){.      s
a5b10 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
a5b20 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66  pend(pAccum, buf
a5b30 70 74 2c 20 6c 65 6e 67 74 68 29 3b 0a 20 20 20  pt, length);.   
a5b40 20 7d 0a 20 20 20 20 69 66 28 20 66 6c 61 67 5f   }.    if( flag_
a5b50 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20  leftjustify ){. 
a5b60 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e       register in
a5b70 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20  t nspace;.      
a5b80 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c  nspace = width-l
a5b90 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28  ength;.      if(
a5ba0 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20   nspace>0 ){.   
a5bb0 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65       appendSpace
a5bc0 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29  (pAccum, nspace)
a5bd0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
a5be0 20 20 20 20 69 66 28 20 7a 45 78 74 72 61 20 29      if( zExtra )
a5bf0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
a5c00 66 72 65 65 28 7a 45 78 74 72 61 29 3b 0a 20 20  free(zExtra);.  
a5c10 20 20 7d 0a 20 20 7d 2f 2a 20 45 6e 64 20 66 6f    }.  }/* End fo
a5c20 72 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20  r loop over the 
a5c30 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f  format string */
a5c40 0a 7d 20 2f 2a 20 45 6e 64 20 6f 66 20 66 75 6e  .} /* End of fun
a5c50 63 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ction */../*.** 
a5c60 41 70 70 65 6e 64 20 4e 20 62 79 74 65 73 20 6f  Append N bytes o
a5c70 66 20 74 65 78 74 20 66 72 6f 6d 20 7a 20 74 6f  f text from z to
a5c80 20 74 68 65 20 53 74 72 41 63 63 75 6d 20 6f 62   the StrAccum ob
a5c90 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ject..*/.SQLITE_
a5ca0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a5cb0 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
a5cc0 6e 64 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20  nd(StrAccum *p, 
a5cd0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
a5ce0 6e 74 20 4e 29 7b 0a 20 20 61 73 73 65 72 74 28  nt N){.  assert(
a5cf0 20 7a 21 3d 30 20 7c 7c 20 4e 3d 3d 30 20 29 3b   z!=0 || N==0 );
a5d00 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69 67  .  if( p->tooBig
a5d10 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   | p->mallocFail
a5d20 65 64 20 29 7b 0a 20 20 20 20 74 65 73 74 63 61  ed ){.    testca
a5d30 73 65 28 70 2d 3e 74 6f 6f 42 69 67 29 3b 0a 20  se(p->tooBig);. 
a5d40 20 20 20 74 65 73 74 63 61 73 65 28 70 2d 3e 6d     testcase(p->m
a5d50 61 6c 6c 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20  allocFailed);.  
a5d60 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
a5d70 69 66 28 20 4e 3c 30 20 29 7b 0a 20 20 20 20 4e  if( N<0 ){.    N
a5d80 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
a5d90 33 30 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28  30(z);.  }.  if(
a5da0 20 4e 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 7a   N==0 || NEVER(z
a5db0 3d 3d 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ==0) ){.    retu
a5dc0 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  rn;.  }.  if( p-
a5dd0 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e 6e  >nChar+N >= p->n
a5de0 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68 61  Alloc ){.    cha
a5df0 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66 28  r *zNew;.    if(
a5e00 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 29   !p->useMalloc )
a5e10 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69  {.      p->tooBi
a5e20 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20 3d  g = 1;.      N =
a5e30 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d 3e   p->nAlloc - p->
a5e40 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20 20  nChar - 1;.     
a5e50 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20 20   if( N<=0 ){.   
a5e60 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
a5e70 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
a5e80 20 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77 20        i64 szNew 
a5e90 3d 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20  = p->nChar;.    
a5ea0 20 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20 31    szNew += N + 1
a5eb0 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e 65  ;.      if( szNe
a5ec0 77 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20 29  w > p->mxAlloc )
a5ed0 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
a5ee0 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 70  3StrAccumReset(p
a5ef0 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 6f  );.        p->to
a5f00 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  oBig = 1;.      
a5f10 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
a5f20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
a5f30 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 28 69 6e 74 29  ->nAlloc = (int)
a5f40 73 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20  szNew;.      }. 
a5f50 20 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69       zNew = sqli
a5f60 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70  te3DbMallocRaw(p
a5f70 2d 3e 64 62 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20  ->db, p->nAlloc 
a5f80 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4e 65  );.      if( zNe
a5f90 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d  w ){.        mem
a5fa0 63 70 79 28 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65  cpy(zNew, p->zTe
a5fb0 78 74 2c 20 70 2d 3e 6e 43 68 61 72 29 3b 0a 20  xt, p->nChar);. 
a5fc0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
a5fd0 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a  rAccumReset(p);.
a5fe0 20 20 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74          p->zText
a5ff0 20 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d   = zNew;.      }
a6000 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 2d  else{.        p-
a6010 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
a6020 31 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  1;.        sqlit
a6030 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28  e3StrAccumReset(
a6040 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  p);.        retu
a6050 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
a6060 7d 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26  }.  }.  memcpy(&
a6070 70 2d 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61  p->zText[p->nCha
a6080 72 5d 2c 20 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e  r], z, N);.  p->
a6090 6e 43 68 61 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f  nChar += N;.}../
a60a0 2a 0a 2a 2a 20 46 69 6e 69 73 68 20 6f 66 66 20  *.** Finish off 
a60b0 61 20 73 74 72 69 6e 67 20 62 79 20 6d 61 6b 69  a string by maki
a60c0 6e 67 20 73 75 72 65 20 69 74 20 69 73 20 7a 65  ng sure it is ze
a60d0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a 2a  ro-terminated..*
a60e0 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
a60f0 65 72 20 74 6f 20 74 68 65 20 72 65 73 75 6c 74  er to the result
a6100 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 52 65 74  ing string.  Ret
a6110 75 72 6e 20 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f  urn a NULL.** po
a6120 69 6e 74 65 72 20 69 66 20 61 6e 79 20 6b 69 6e  inter if any kin
a6130 64 20 6f 66 20 65 72 72 6f 72 20 77 61 73 20 65  d of error was e
a6140 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53  ncountered..*/.S
a6150 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
a6160 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63  ar *sqlite3StrAc
a6170 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63  cumFinish(StrAcc
a6180 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  um *p){.  if( p-
a6190 3e 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 70 2d  >zText ){.    p-
a61a0 3e 7a 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d  >zText[p->nChar]
a61b0 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 2d   = 0;.    if( p-
a61c0 3e 75 73 65 4d 61 6c 6c 6f 63 20 26 26 20 70 2d  >useMalloc && p-
a61d0 3e 7a 54 65 78 74 3d 3d 70 2d 3e 7a 42 61 73 65  >zText==p->zBase
a61e0 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 7a 54 65   ){.      p->zTe
a61f0 78 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  xt = sqlite3DbMa
a6200 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 70  llocRaw(p->db, p
a6210 2d 3e 6e 43 68 61 72 2b 31 20 29 3b 0a 20 20 20  ->nChar+1 );.   
a6220 20 20 20 69 66 28 20 70 2d 3e 7a 54 65 78 74 20     if( p->zText 
a6230 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ){.        memcp
a6240 79 28 70 2d 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a  y(p->zText, p->z
a6250 42 61 73 65 2c 20 70 2d 3e 6e 43 68 61 72 2b 31  Base, p->nChar+1
a6260 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
a6270 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f          p->mallo
a6280 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
a6290 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
a62a0 20 72 65 74 75 72 6e 20 70 2d 3e 7a 54 65 78 74   return p->zText
a62b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74  ;.}../*.** Reset
a62c0 20 61 6e 20 53 74 72 41 63 63 75 6d 20 73 74 72   an StrAccum str
a62d0 69 6e 67 2e 20 20 52 65 63 6c 61 69 6d 20 61 6c  ing.  Reclaim al
a62e0 6c 20 6d 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72  l malloced memor
a62f0 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
a6300 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a6310 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 53  3StrAccumReset(S
a6320 74 72 41 63 63 75 6d 20 2a 70 29 7b 0a 20 20 69  trAccum *p){.  i
a6330 66 28 20 70 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e  f( p->zText!=p->
a6340 7a 42 61 73 65 20 29 7b 0a 20 20 20 20 73 71 6c  zBase ){.    sql
a6350 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62  ite3DbFree(p->db
a6360 2c 20 70 2d 3e 7a 54 65 78 74 29 3b 0a 20 20 7d  , p->zText);.  }
a6370 0a 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 30 3b  .  p->zText = 0;
a6380 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
a6390 6c 69 7a 65 20 61 20 73 74 72 69 6e 67 20 61 63  lize a string ac
a63a0 63 75 6d 75 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c  cumulator.*/.SQL
a63b0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a63c0 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a63d0 49 6e 69 74 28 53 74 72 41 63 63 75 6d 20 2a 70  Init(StrAccum *p
a63e0 2c 20 63 68 61 72 20 2a 7a 42 61 73 65 2c 20 69  , char *zBase, i
a63f0 6e 74 20 6e 2c 20 69 6e 74 20 6d 78 29 7b 0a 20  nt n, int mx){. 
a6400 20 70 2d 3e 7a 54 65 78 74 20 3d 20 70 2d 3e 7a   p->zText = p->z
a6410 42 61 73 65 20 3d 20 7a 42 61 73 65 3b 0a 20 20  Base = zBase;.  
a6420 70 2d 3e 64 62 20 3d 20 30 3b 0a 20 20 70 2d 3e  p->db = 0;.  p->
a6430 6e 43 68 61 72 20 3d 20 30 3b 0a 20 20 70 2d 3e  nChar = 0;.  p->
a6440 6e 41 6c 6c 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d  nAlloc = n;.  p-
a6450 3e 6d 78 41 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20  >mxAlloc = mx;. 
a6460 20 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20   p->useMalloc = 
a6470 31 3b 0a 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d  1;.  p->tooBig =
a6480 20 30 3b 0a 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46   0;.  p->mallocF
a6490 61 69 6c 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  ailed = 0;.}../*
a64a0 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d  .** Print into m
a64b0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
a64c0 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
a64d0 28 29 2e 20 20 55 73 65 20 74 68 65 20 69 6e 74  ().  Use the int
a64e0 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65  ernal.** %-conve
a64f0 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73  rsion extensions
a6500 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
a6510 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
a6520 33 56 4d 50 72 69 6e 74 66 28 73 71 6c 69 74 65  3VMPrintf(sqlite
a6530 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
a6540 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c  r *zFormat, va_l
a6550 69 73 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20  ist ap){.  char 
a6560 2a 7a 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65  *z;.  char zBase
a6570 5b 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55  [SQLITE_PRINT_BU
a6580 46 5f 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63  F_SIZE];.  StrAc
a6590 63 75 6d 20 61 63 63 3b 0a 20 20 61 73 73 65 72  cum acc;.  asser
a65a0 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 73 71  t( db!=0 );.  sq
a65b0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
a65c0 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73  t(&acc, zBase, s
a65d0 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 0a 20 20  izeof(zBase),.  
a65e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a65f0 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53      db->aLimit[S
a6600 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
a6610 54 48 5d 29 3b 0a 20 20 61 63 63 2e 64 62 20 3d  TH]);.  acc.db =
a6620 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56 58   db;.  sqlite3VX
a6630 50 72 69 6e 74 66 28 26 61 63 63 2c 20 31 2c 20  Printf(&acc, 1, 
a6640 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
a6650 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63  z = sqlite3StrAc
a6660 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b  cumFinish(&acc);
a6670 0a 20 20 69 66 28 20 61 63 63 2e 6d 61 6c 6c 6f  .  if( acc.mallo
a6680 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 64  cFailed ){.    d
a6690 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
a66a0 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 1;.  }.  retur
a66b0 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  n z;.}../*.** Pr
a66c0 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20  int into memory 
a66d0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
a66e0 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 55  liteMalloc().  U
a66f0 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a  se the internal.
a6700 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20  ** %-conversion 
a6710 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53  extensions..*/.S
a6720 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
a6730 61 72 20 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e  ar *sqlite3MPrin
a6740 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  tf(sqlite3 *db, 
a6750 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
a6760 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
a6770 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20  list ap;.  char 
a6780 2a 7a 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61  *z;.  va_start(a
a6790 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a  p, zFormat);.  z
a67a0 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e   = sqlite3VMPrin
a67b0 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20  tf(db, zFormat, 
a67c0 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
a67d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
a67e0 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 73 71 6c  ../*.** Like sql
a67f0 69 74 65 33 4d 50 72 69 6e 74 66 28 29 2c 20 62  ite3MPrintf(), b
a6800 75 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44  ut call sqlite3D
a6810 62 46 72 65 65 28 29 20 6f 6e 20 7a 53 74 72 20  bFree() on zStr 
a6820 61 66 74 65 72 20 66 6f 72 6d 61 74 74 69 6e 67  after formatting
a6830 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 61  .** the string a
a6840 6e 64 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  nd before return
a6850 6e 69 6e 67 2e 20 20 54 68 69 73 20 72 6f 75 74  ning.  This rout
a6860 69 6e 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ine is intended 
a6870 74 6f 20 62 65 20 75 73 65 64 0a 2a 2a 20 74 6f  to be used.** to
a6880 20 6d 6f 64 69 66 79 20 61 6e 20 65 78 69 73 74   modify an exist
a6890 69 6e 67 20 73 74 72 69 6e 67 2e 20 20 46 6f 72  ing string.  For
a68a0 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20   example:.**.** 
a68b0 20 20 20 20 20 20 78 20 3d 20 73 71 6c 69 74 65        x = sqlite
a68c0 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 78 2c 20  3MPrintf(db, x, 
a68d0 22 70 72 65 66 69 78 20 25 73 20 73 75 66 66 69  "prefix %s suffi
a68e0 78 22 2c 20 78 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51  x", x);.**.*/.SQ
a68f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
a6900 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e  r *sqlite3MAppen
a6910 64 66 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  df(sqlite3 *db, 
a6920 63 68 61 72 20 2a 7a 53 74 72 2c 20 63 6f 6e 73  char *zStr, cons
a6930 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
a6940 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
a6950 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a   ap;.  char *z;.
a6960 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
a6970 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73  Format);.  z = s
a6980 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64  qlite3VMPrintf(d
a6990 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  b, zFormat, ap);
a69a0 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20  .  va_end(ap);. 
a69b0 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
a69c0 62 2c 20 7a 53 74 72 29 3b 0a 20 20 72 65 74 75  b, zStr);.  retu
a69d0 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  rn z;.}../*.** P
a69e0 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
a69f0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
a6a00 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
a6a10 20 20 4f 6d 69 74 20 74 68 65 20 69 6e 74 65 72    Omit the inter
a6a20 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73  nal.** %-convers
a6a30 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a  ion extensions..
a6a40 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  */.SQLITE_API ch
a6a50 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 6d 70 72  ar *sqlite3_vmpr
a6a60 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20  intf(const char 
a6a70 2a 7a 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73  *zFormat, va_lis
a6a80 74 20 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a  t ap){.  char *z
a6a90 3b 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53  ;.  char zBase[S
a6aa0 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f  QLITE_PRINT_BUF_
a6ab0 53 49 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75  SIZE];.  StrAccu
a6ac0 6d 20 61 63 63 3b 0a 23 69 66 6e 64 65 66 20 53  m acc;.#ifndef S
a6ad0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
a6ae0 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  NIT.  if( sqlite
a6af0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29  3_initialize() )
a6b00 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
a6b10 66 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63  f.  sqlite3StrAc
a6b20 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42  cumInit(&acc, zB
a6b30 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a 42 61 73  ase, sizeof(zBas
a6b40 65 29 2c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  e), SQLITE_MAX_L
a6b50 45 4e 47 54 48 29 3b 0a 20 20 73 71 6c 69 74 65  ENGTH);.  sqlite
a6b60 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20  3VXPrintf(&acc, 
a6b70 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  0, zFormat, ap);
a6b80 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74  .  z = sqlite3St
a6b90 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63  rAccumFinish(&ac
a6ba0 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  c);.  return z;.
a6bb0 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69  }../*.** Print i
a6bc0 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
a6bd0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
a6be0 5f 6d 61 6c 6c 6f 63 28 29 28 29 2e 20 20 4f 6d  _malloc()().  Om
a6bf0 69 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a  it the internal.
a6c00 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20  ** %-conversion 
a6c10 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53  extensions..*/.S
a6c20 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
a6c30 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
a6c40 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
a6c50 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
a6c60 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72 20  list ap;.  char 
a6c70 2a 7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  *z;.#ifndef SQLI
a6c80 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
a6c90 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
a6ca0 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65  nitialize() ) re
a6cb0 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20  turn 0;.#endif. 
a6cc0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
a6cd0 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71  ormat);.  z = sq
a6ce0 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 7a  lite3_vmprintf(z
a6cf0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76  Format, ap);.  v
a6d00 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74  a_end(ap);.  ret
a6d10 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
a6d20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
a6d30 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 6e  () works like sn
a6d40 70 72 69 6e 74 66 28 29 20 65 78 63 65 70 74 20  printf() except 
a6d50 74 68 61 74 20 69 74 20 69 67 6e 6f 72 65 73 20  that it ignores 
a6d60 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c  the.** current l
a6d70 6f 63 61 6c 65 20 73 65 74 74 69 6e 67 73 2e 20  ocale settings. 
a6d80 20 54 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61   This is importa
a6d90 6e 74 20 66 6f 72 20 53 51 4c 69 74 65 20 62 65  nt for SQLite be
a6da0 63 61 75 73 65 20 77 65 0a 2a 2a 20 61 72 65 20  cause we.** are 
a6db0 6e 6f 74 20 61 62 6c 65 20 74 6f 20 75 73 65 20  not able to use 
a6dc0 61 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63  a "," as the dec
a6dd0 69 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 20 70 6c  imal point in pl
a6de0 61 63 65 20 6f 66 20 22 2e 22 20 61 73 0a 2a 2a  ace of "." as.**
a6df0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 73 6f   specified by so
a6e00 6d 65 20 6c 6f 63 61 6c 65 73 2e 0a 2a 2f 0a 53  me locales..*/.S
a6e10 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
a6e20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
a6e30 28 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 42  (int n, char *zB
a6e40 75 66 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  uf, const char *
a6e50 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
a6e60 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 6c   char *z;.  va_l
a6e70 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63  ist ap;.  StrAcc
a6e80 75 6d 20 61 63 63 3b 0a 0a 20 20 69 66 28 20 6e  um acc;..  if( n
a6e90 3c 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  <=0 ){.    retur
a6ea0 6e 20 7a 42 75 66 3b 0a 20 20 7d 0a 20 20 73 71  n zBuf;.  }.  sq
a6eb0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
a6ec0 74 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 6e 2c  t(&acc, zBuf, n,
a6ed0 20 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61   0);.  acc.useMa
a6ee0 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73  lloc = 0;.  va_s
a6ef0 74 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29  tart(ap,zFormat)
a6f00 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69  ;.  sqlite3VXPri
a6f10 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f  ntf(&acc, 0, zFo
a6f20 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
a6f30 65 6e 64 28 61 70 29 3b 0a 20 20 7a 20 3d 20 73  end(ap);.  z = s
a6f40 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
a6f50 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65  nish(&acc);.  re
a6f60 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 20 64  turn z;.}..#if d
a6f70 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
a6f80 42 55 47 29 0a 2f 2a 0a 2a 2a 20 41 20 76 65 72  BUG)./*.** A ver
a6f90 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66 28 29  sion of printf()
a6fa0 20 74 68 61 74 20 75 6e 64 65 72 73 74 61 6e 64   that understand
a6fb0 73 20 25 6c 6c 64 2e 20 20 55 73 65 64 20 66 6f  s %lld.  Used fo
a6fc0 72 20 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2a 20  r debugging..** 
a6fd0 54 68 65 20 70 72 69 6e 74 66 28 29 20 62 75 69  The printf() bui
a6fe0 6c 74 20 69 6e 74 6f 20 73 6f 6d 65 20 76 65 72  lt into some ver
a6ff0 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73  sions of windows
a7000 20 64 6f 65 73 20 6e 6f 74 20 75 6e 64 65 72 73   does not unders
a7010 74 61 6e 64 20 25 6c 6c 64 0a 2a 2a 20 61 6e 64  tand %lld.** and
a7020 20 73 65 67 66 61 75 6c 74 73 20 69 66 20 79 6f   segfaults if yo
a7030 75 20 67 69 76 65 20 69 74 20 61 20 6c 6f 6e 67  u give it a long
a7040 20 6c 6f 6e 67 20 69 6e 74 2e 0a 2a 2f 0a 53 51   long int..*/.SQ
a7050 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a7060 64 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  d sqlite3DebugPr
a7070 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72 20  intf(const char 
a7080 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a  *zFormat, ...){.
a7090 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
a70a0 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 20 20  StrAccum acc;.  
a70b0 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d 3b 0a  char zBuf[500];.
a70c0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
a70d0 6d 49 6e 69 74 28 26 61 63 63 2c 20 7a 42 75 66  mInit(&acc, zBuf
a70e0 2c 20 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20  , sizeof(zBuf), 
a70f0 30 29 3b 0a 20 20 61 63 63 2e 75 73 65 4d 61 6c  0);.  acc.useMal
a7100 6c 6f 63 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74  loc = 0;.  va_st
a7110 61 72 74 28 61 70 2c 7a 46 6f 72 6d 61 74 29 3b  art(ap,zFormat);
a7120 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e  .  sqlite3VXPrin
a7130 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72  tf(&acc, 0, zFor
a7140 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
a7150 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65  nd(ap);.  sqlite
a7160 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28  3StrAccumFinish(
a7170 26 61 63 63 29 3b 0a 20 20 66 70 72 69 6e 74 66  &acc);.  fprintf
a7180 28 73 74 64 6f 75 74 2c 22 25 73 22 2c 20 7a 42  (stdout,"%s", zB
a7190 75 66 29 3b 0a 20 20 66 66 6c 75 73 68 28 73 74  uf);.  fflush(st
a71a0 64 6f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  dout);.}.#endif.
a71b0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
a71c0 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a  OMIT_TRACE./*.**
a71d0 20 76 61 72 69 61 62 6c 65 2d 61 72 67 75 6d 65   variable-argume
a71e0 6e 74 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e  nt wrapper aroun
a71f0 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74  d sqlite3VXPrint
a7200 66 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  f()..*/.SQLITE_P
a7210 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a7220 74 65 33 58 50 72 69 6e 74 66 28 53 74 72 41 63  te3XPrintf(StrAc
a7230 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  cum *p, const ch
a7240 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
a7250 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
a7260 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a  .  va_start(ap,z
a7270 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74  Format);.  sqlit
a7280 65 33 56 58 50 72 69 6e 74 66 28 70 2c 20 31 2c  e3VXPrintf(p, 1,
a7290 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20   zFormat, ap);. 
a72a0 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 7d 0a 23   va_end(ap);.}.#
a72b0 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
a72c0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72  ****** End of pr
a72d0 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  intf.c *********
a72e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a72f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7300 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
a7310 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
a7320 65 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a  e random.c *****
a7330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7350 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
a7360 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
a7370 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
a7380 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
a7390 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
a73a0 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
a73b0 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
a73c0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
a73d0 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
a73e0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
a73f0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
a7400 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
a7410 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
a7420 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
a7430 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
a7440 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
a7450 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
a7460 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
a7470 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
a7480 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
a7490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a74a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a74b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a74c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
a74d0 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
a74e0 69 6e 73 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c  ins code to impl
a74f0 65 6d 65 6e 74 20 61 20 70 73 65 75 64 6f 2d 72  ement a pseudo-r
a7500 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 0a 2a 2a 20  andom number.** 
a7510 67 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29  generator (PRNG)
a7520 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a   for SQLite..**.
a7530 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  ** Random number
a7540 73 20 61 72 65 20 75 73 65 64 20 62 79 20 73 6f  s are used by so
a7550 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
a7560 73 65 20 62 61 63 6b 65 6e 64 73 20 69 6e 20 6f  se backends in o
a7570 72 64 65 72 0a 2a 2a 20 74 6f 20 67 65 6e 65 72  rder.** to gener
a7580 61 74 65 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67  ate random integ
a7590 65 72 20 6b 65 79 73 20 66 6f 72 20 74 61 62 6c  er keys for tabl
a75a0 65 73 20 6f 72 20 72 61 6e 64 6f 6d 20 66 69 6c  es or random fil
a75b0 65 6e 61 6d 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 20  enames..*/.../* 
a75c0 41 6c 6c 20 74 68 72 65 61 64 73 20 73 68 61 72  All threads shar
a75d0 65 20 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f  e a single rando
a75e0 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74  m number generat
a75f0 6f 72 2e 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  or..** This stru
a7600 63 74 75 72 65 20 69 73 20 74 68 65 20 63 75 72  cture is the cur
a7610 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20 74 68  rent state of th
a7620 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2f 0a  e generator..*/.
a7630 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53  static SQLITE_WS
a7640 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  D struct sqlite3
a7650 50 72 6e 67 54 79 70 65 20 7b 0a 20 20 75 6e 73  PrngType {.  uns
a7660 69 67 6e 65 64 20 63 68 61 72 20 69 73 49 6e 69  igned char isIni
a7670 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t;          /* T
a7680 72 75 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a  rue if initializ
a7690 65 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ed */.  unsigned
a76a0 20 63 68 61 72 20 69 2c 20 6a 3b 20 20 20 20 20   char i, j;     
a76b0 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20         /* State 
a76c0 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75  variables */.  u
a76d0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 5b 32  nsigned char s[2
a76e0 35 36 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  56];          /*
a76f0 20 53 74 61 74 65 20 76 61 72 69 61 62 6c 65 73   State variables
a7700 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 50 72 6e   */.} sqlite3Prn
a7710 67 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20  g;../*.** Get a 
a7720 73 69 6e 67 6c 65 20 38 2d 62 69 74 20 72 61 6e  single 8-bit ran
a7730 64 6f 6d 20 76 61 6c 75 65 20 66 72 6f 6d 20 74  dom value from t
a7740 68 65 20 52 43 34 20 50 52 4e 47 2e 20 20 54 68  he RC4 PRNG.  Th
a7750 65 20 4d 75 74 65 78 0a 2a 2a 20 6d 75 73 74 20  e Mutex.** must 
a7760 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 65 78  be held while ex
a7770 65 63 75 74 69 6e 67 20 74 68 69 73 20 72 6f 75  ecuting this rou
a7780 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 79 20  tine..**.** Why 
a7790 6e 6f 74 20 6a 75 73 74 20 75 73 65 20 61 20 6c  not just use a l
a77a0 69 62 72 61 72 79 20 72 61 6e 64 6f 6d 20 67 65  ibrary random ge
a77b0 6e 65 72 61 74 6f 72 20 6c 69 6b 65 20 6c 72 61  nerator like lra
a77c0 6e 64 34 38 28 29 20 66 6f 72 20 74 68 69 73 3f  nd48() for this?
a77d0 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20  .** Because the 
a77e0 4f 50 5f 4e 65 77 52 6f 77 69 64 20 6f 70 63 6f  OP_NewRowid opco
a77f0 64 65 20 69 6e 20 74 68 65 20 56 44 42 45 20 64  de in the VDBE d
a7800 65 70 65 6e 64 73 20 6f 6e 20 68 61 76 69 6e 67  epends on having
a7810 20 61 20 76 65 72 79 0a 2a 2a 20 67 6f 6f 64 20   a very.** good 
a7820 73 6f 75 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d  source of random
a7830 20 6e 75 6d 62 65 72 73 2e 20 20 54 68 65 20 6c   numbers.  The l
a7840 72 61 6e 64 34 38 28 29 20 6c 69 62 72 61 72 79  rand48() library
a7850 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 0a 2a 2a   function may.**
a7860 20 77 65 6c 6c 20 62 65 20 67 6f 6f 64 20 65 6e   well be good en
a7870 6f 75 67 68 2e 20 20 42 75 74 20 6d 61 79 62 65  ough.  But maybe
a7880 20 6e 6f 74 2e 20 20 4f 72 20 6d 61 79 62 65 20   not.  Or maybe 
a7890 6c 72 61 6e 64 34 38 28 29 20 68 61 73 20 73 6f  lrand48() has so
a78a0 6d 65 0a 2a 2a 20 73 75 62 74 6c 65 20 70 72 6f  me.** subtle pro
a78b0 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d 65 20 73 79  blems on some sy
a78c0 73 74 65 6d 73 20 74 68 61 74 20 63 6f 75 6c 64  stems that could
a78d0 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 2e   cause problems.
a78e0 20 20 49 74 20 69 73 20 68 61 72 64 0a 2a 2a 20    It is hard.** 
a78f0 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f 20 6d 69 6e  to know.  To min
a7900 69 6d 69 7a 65 20 74 68 65 20 72 69 73 6b 20 6f  imize the risk o
a7910 66 20 70 72 6f 62 6c 65 6d 73 20 64 75 65 20 74  f problems due t
a7920 6f 20 62 61 64 20 6c 72 61 6e 64 34 38 28 29 0a  o bad lrand48().
a7930 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
a7940 6e 73 2c 20 53 51 4c 69 74 65 20 75 73 65 73 20  ns, SQLite uses 
a7950 74 68 69 73 20 72 61 6e 64 6f 6d 20 6e 75 6d 62  this random numb
a7960 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62 61 73  er generator bas
a7970 65 64 0a 2a 2a 20 6f 6e 20 52 43 34 2c 20 77 68  ed.** on RC4, wh
a7980 69 63 68 20 77 65 20 6b 6e 6f 77 20 77 6f 72 6b  ich we know work
a7990 73 20 76 65 72 79 20 77 65 6c 6c 2e 0a 2a 2a 0a  s very well..**.
a79a0 2a 2a 20 28 4c 61 74 65 72 29 3a 20 20 41 63 74  ** (Later):  Act
a79b0 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65 77 52 6f 77  ually, OP_NewRow
a79c0 69 64 20 64 6f 65 73 20 6e 6f 74 20 64 65 70 65  id does not depe
a79d0 6e 64 20 6f 6e 20 61 20 67 6f 6f 64 20 73 6f 75  nd on a good sou
a79e0 72 63 65 20 6f 66 0a 2a 2a 20 72 61 6e 64 6f 6d  rce of.** random
a79f0 6e 65 73 73 20 61 6e 79 20 6d 6f 72 65 2e 20 20  ness any more.  
a7a00 42 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76  But we will leav
a7a10 65 20 74 68 69 73 20 63 6f 64 65 20 69 6e 20 61  e this code in a
a7a20 6c 6c 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2f 0a  ll the same..*/.
a7a30 73 74 61 74 69 63 20 75 38 20 72 61 6e 64 6f 6d  static u8 random
a7a40 42 79 74 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e  Byte(void){.  un
a7a50 73 69 67 6e 65 64 20 63 68 61 72 20 74 3b 0a 0a  signed char t;..
a7a60 0a 20 20 2f 2a 20 54 68 65 20 22 77 73 64 50 72  .  /* The "wsdPr
a7a70 6e 67 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72  ng" macro will r
a7a80 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 70 73  esolve to the ps
a7a90 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62  eudo-random numb
a7aa0 65 72 20 67 65 6e 65 72 61 74 6f 72 0a 20 20 2a  er generator.  *
a7ab0 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20  * state vector. 
a7ac0 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74 61   If writable sta
a7ad0 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73 75  tic data is unsu
a7ae0 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74  pported on the t
a7af0 61 72 67 65 74 2c 0a 20 20 2a 2a 20 77 65 20 68  arget,.  ** we h
a7b00 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68  ave to locate th
a7b10 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61  e state vector a
a7b20 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20  t run-time.  In 
a7b30 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a  the more common.
a7b40 20 20 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20    ** case where 
a7b50 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20  writable static 
a7b60 64 61 74 61 20 69 73 20 73 75 70 70 6f 72 74 65  data is supporte
a7b70 64 2c 20 77 73 64 50 72 6e 67 20 63 61 6e 20 72  d, wsdPrng can r
a7b80 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a 20 20  efer directly.  
a7b90 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74  ** to the "sqlit
a7ba0 65 33 50 72 6e 67 22 20 73 74 61 74 65 20 76 65  e3Prng" state ve
a7bb0 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62  ctor declared ab
a7bc0 6f 76 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66  ove..  */.#ifdef
a7bd0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
a7be0 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
a7bf0 33 50 72 6e 67 54 79 70 65 20 2a 70 20 3d 20 26  3PrngType *p = &
a7c00 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71  GLOBAL(struct sq
a7c10 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73  lite3PrngType, s
a7c20 71 6c 69 74 65 33 50 72 6e 67 29 3b 0a 23 20 64  qlite3Prng);.# d
a7c30 65 66 69 6e 65 20 77 73 64 50 72 6e 67 20 70 5b  efine wsdPrng p[
a7c40 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  0].#else.# defin
a7c50 65 20 77 73 64 50 72 6e 67 20 73 71 6c 69 74 65  e wsdPrng sqlite
a7c60 33 50 72 6e 67 0a 23 65 6e 64 69 66 0a 0a 0a 20  3Prng.#endif... 
a7c70 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
a7c80 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  he state of the 
a7c90 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65  random number ge
a7ca0 6e 65 72 61 74 6f 72 20 6f 6e 63 65 2c 0a 20 20  nerator once,.  
a7cb0 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d  ** the first tim
a7cc0 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  e this routine i
a7cd0 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 73  s called.  The s
a7ce0 65 65 64 20 76 61 6c 75 65 20 64 6f 65 73 0a 20  eed value does. 
a7cf0 20 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   ** not need to 
a7d00 63 6f 6e 74 61 69 6e 20 61 20 6c 6f 74 20 6f 66  contain a lot of
a7d10 20 72 61 6e 64 6f 6d 6e 65 73 73 20 73 69 6e 63   randomness sinc
a7d20 65 20 77 65 20 61 72 65 20 6e 6f 74 0a 20 20 2a  e we are not.  *
a7d30 2a 20 74 72 79 69 6e 67 20 74 6f 20 64 6f 20 73  * trying to do s
a7d40 65 63 75 72 65 20 65 6e 63 72 79 70 74 69 6f 6e  ecure encryption
a7d50 20 6f 72 20 61 6e 79 74 68 69 6e 67 20 6c 69 6b   or anything lik
a7d60 65 20 74 68 61 74 2e 2e 2e 0a 20 20 2a 2a 0a 20  e that....  **. 
a7d70 20 2a 2a 20 4e 6f 74 68 69 6e 67 20 69 6e 20 74   ** Nothing in t
a7d80 68 69 73 20 66 69 6c 65 20 6f 72 20 61 6e 79 77  his file or anyw
a7d90 68 65 72 65 20 65 6c 73 65 20 69 6e 20 53 51 4c  here else in SQL
a7da0 69 74 65 20 64 6f 65 73 20 61 6e 79 20 6b 69 6e  ite does any kin
a7db0 64 20 6f 66 0a 20 20 2a 2a 20 65 6e 63 72 79 70  d of.  ** encryp
a7dc0 74 69 6f 6e 2e 20 20 54 68 65 20 52 43 34 20 61  tion.  The RC4 a
a7dd0 6c 67 6f 72 69 74 68 6d 20 69 73 20 62 65 69 6e  lgorithm is bein
a7de0 67 20 75 73 65 64 20 61 73 20 61 20 50 52 4e 47  g used as a PRNG
a7df0 20 28 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 0a   (pseudo-random.
a7e00 20 20 2a 2a 20 6e 75 6d 62 65 72 20 67 65 6e 65    ** number gene
a7e10 72 61 74 6f 72 29 20 6e 6f 74 20 61 73 20 61 6e  rator) not as an
a7e20 20 65 6e 63 72 79 70 74 69 6f 6e 20 64 65 76 69   encryption devi
a7e30 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ce..  */.  if( !
a7e40 77 73 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 29  wsdPrng.isInit )
a7e50 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
a7e60 20 63 68 61 72 20 6b 5b 32 35 36 5d 3b 0a 20 20   char k[256];.  
a7e70 20 20 77 73 64 50 72 6e 67 2e 6a 20 3d 20 30 3b    wsdPrng.j = 0;
a7e80 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 69 20 3d  .    wsdPrng.i =
a7e90 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f   0;.    sqlite3O
a7ea0 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69  sRandomness(sqli
a7eb0 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 2c  te3_vfs_find(0),
a7ec0 20 32 35 36 2c 20 6b 29 3b 0a 20 20 20 20 66 6f   256, k);.    fo
a7ed0 72 28 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b  r(i=0; i<256; i+
a7ee0 2b 29 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e  +){.      wsdPrn
a7ef0 67 2e 73 5b 69 5d 20 3d 20 28 75 38 29 69 3b 0a  g.s[i] = (u8)i;.
a7f00 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
a7f10 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a  0; i<256; i++){.
a7f20 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20        wsdPrng.j 
a7f30 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20  += wsdPrng.s[i] 
a7f40 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 20 74 20  + k[i];.      t 
a7f50 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50  = wsdPrng.s[wsdP
a7f60 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 20 77 73  rng.j];.      ws
a7f70 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e  dPrng.s[wsdPrng.
a7f80 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69  j] = wsdPrng.s[i
a7f90 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67  ];.      wsdPrng
a7fa0 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 20 20 7d  .s[i] = t;.    }
a7fb0 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 69 73 49  .    wsdPrng.isI
a7fc0 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20  nit = 1;.  }..  
a7fd0 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 64 20  /* Generate and 
a7fe0 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 20 72 61  return single ra
a7ff0 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a 20  ndom byte.  */. 
a8000 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b 0a 20 20   wsdPrng.i++;.  
a8010 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73  t = wsdPrng.s[ws
a8020 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 73 64 50  dPrng.i];.  wsdP
a8030 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 77 73  rng.j += t;.  ws
a8040 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e  dPrng.s[wsdPrng.
a8050 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77  i] = wsdPrng.s[w
a8060 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 77 73 64  sdPrng.j];.  wsd
a8070 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a  Prng.s[wsdPrng.j
a8080 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20 77 73  ] = t;.  t += ws
a8090 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e  dPrng.s[wsdPrng.
a80a0 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64  i];.  return wsd
a80b0 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a 2f 2a  Prng.s[t];.}../*
a80c0 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 72 61 6e  .** Return N ran
a80d0 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 53 51  dom bytes..*/.SQ
a80e0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
a80f0 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
a8100 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 70 42  (int N, void *pB
a8110 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  uf){.  unsigned 
a8120 63 68 61 72 20 2a 7a 42 75 66 20 3d 20 70 42 75  char *zBuf = pBu
a8130 66 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  f;.#if SQLITE_TH
a8140 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74  READSAFE.  sqlit
a8150 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20  e3_mutex *mutex 
a8160 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  = sqlite3MutexAl
a8170 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
a8180 5f 53 54 41 54 49 43 5f 50 52 4e 47 29 3b 0a 23  _STATIC_PRNG);.#
a8190 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
a81a0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
a81b0 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d  x);.  while( N--
a81c0 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75 66 2b 2b   ){.    *(zBuf++
a81d0 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 65 28 29  ) = randomByte()
a81e0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
a81f0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
a8200 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  x);.}..#ifndef S
a8210 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54  QLITE_OMIT_BUILT
a8220 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 46 6f  IN_TEST./*.** Fo
a8230 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73  r testing purpos
a8240 65 73 2c 20 77 65 20 73 6f 6d 65 74 69 6d 65 73  es, we sometimes
a8250 20 77 61 6e 74 20 74 6f 20 70 72 65 73 65 72 76   want to preserv
a8260 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 0a 2a  e the state of.*
a8270 2a 20 50 52 4e 47 20 61 6e 64 20 72 65 73 74 6f  * PRNG and resto
a8280 72 65 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69  re the PRNG to i
a8290 74 73 20 73 61 76 65 64 20 73 74 61 74 65 20 61  ts saved state a
a82a0 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2c 20  t a later time, 
a82b0 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65 74 20 74  or.** to reset t
a82c0 68 65 20 50 52 4e 47 20 74 6f 20 69 74 73 20 69  he PRNG to its i
a82d0 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 20 20 54  nitial state.  T
a82e0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 63  hese routines ac
a82f0 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 68 6f 73  complish.** thos
a8300 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54  e tasks..**.** T
a8310 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  he sqlite3_test_
a8320 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66  control() interf
a8330 61 63 65 20 63 61 6c 6c 73 20 74 68 65 73 65 20  ace calls these 
a8340 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 63  routines to.** c
a8350 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52 4e 47 2e  ontrol the PRNG.
a8360 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54  .*/.static SQLIT
a8370 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71 6c  E_WSD struct sql
a8380 69 74 65 33 50 72 6e 67 54 79 70 65 20 73 71 6c  ite3PrngType sql
a8390 69 74 65 33 53 61 76 65 64 50 72 6e 67 3b 0a 53  ite3SavedPrng;.S
a83a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
a83b0 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61  id sqlite3PrngSa
a83c0 76 65 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20  veState(void){. 
a83d0 20 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c   memcpy(.    &GL
a83e0 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69  OBAL(struct sqli
a83f0 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c  te3PrngType, sql
a8400 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a  ite3SavedPrng),.
a8410 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75      &GLOBAL(stru
a8420 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79  ct sqlite3PrngTy
a8430 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29  pe, sqlite3Prng)
a8440 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c  ,.    sizeof(sql
a8450 69 74 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d  ite3Prng).  );.}
a8460 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a8470 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67  void sqlite3Prng
a8480 52 65 73 74 6f 72 65 53 74 61 74 65 28 76 6f 69  RestoreState(voi
a8490 64 29 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20  d){.  memcpy(.  
a84a0 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74    &GLOBAL(struct
a84b0 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
a84c0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2c 0a  , sqlite3Prng),.
a84d0 20 20 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75      &GLOBAL(stru
a84e0 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79  ct sqlite3PrngTy
a84f0 70 65 2c 20 73 71 6c 69 74 65 33 53 61 76 65 64  pe, sqlite3Saved
a8500 50 72 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f  Prng),.    sizeo
a8510 66 28 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20  f(sqlite3Prng). 
a8520 20 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49   );.}.SQLITE_PRI
a8530 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a8540 33 50 72 6e 67 52 65 73 65 74 53 74 61 74 65 28  3PrngResetState(
a8550 76 6f 69 64 29 7b 0a 20 20 47 4c 4f 42 41 4c 28  void){.  GLOBAL(
a8560 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72  struct sqlite3Pr
a8570 6e 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50  ngType, sqlite3P
a8580 72 6e 67 29 2e 69 73 49 6e 69 74 20 3d 20 30 3b  rng).isInit = 0;
a8590 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
a85a0 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
a85b0 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  _TEST */../*****
a85c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
a85d0 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a   random.c ******
a85e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a85f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8600 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
a8610 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
a8620 66 69 6c 65 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a  file utf.c *****
a8630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8650 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
a8660 32 30 30 34 20 41 70 72 69 6c 20 31 33 0a 2a 2a  2004 April 13.**
a8670 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
a8680 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
a8690 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
a86a0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
a86b0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
a86c0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
a86d0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
a86e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
a86f0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
a8700 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
a8710 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
a8720 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
a8730 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
a8740 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
a8750 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
a8760 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
a8770 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
a8780 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
a8790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2a 2a  ****************
a87c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
a87d0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
a87e0 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64  ns routines used
a87f0 20 74 6f 20 74 72 61 6e 73 6c 61 74 65 20 62 65   to translate be
a8800 74 77 65 65 6e 20 55 54 46 2d 38 2c 20 0a 2a 2a  tween UTF-8, .**
a8810 20 55 54 46 2d 31 36 2c 20 55 54 46 2d 31 36 42   UTF-16, UTF-16B
a8820 45 2c 20 61 6e 64 20 55 54 46 2d 31 36 4c 45 2e  E, and UTF-16LE.
a8830 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20  .**.** Notes on 
a8840 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42  UTF-8:.**.**   B
a8850 79 74 65 2d 30 20 20 20 20 42 79 74 65 2d 31 20  yte-0    Byte-1 
a8860 20 20 20 42 79 74 65 2d 32 20 20 20 20 42 79 74     Byte-2    Byt
a8870 65 2d 33 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20  e-3    Value.** 
a8880 20 30 78 78 78 78 78 78 78 20 20 20 20 20 20 20   0xxxxxxx       
a8890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a88a0 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30            000000
a88b0 30 30 20 30 30 30 30 30 30 30 30 20 30 78 78 78  00 00000000 0xxx
a88c0 78 78 78 78 0a 2a 2a 20 20 31 31 30 79 79 79 79  xxxx.**  110yyyy
a88d0 79 20 20 31 30 78 78 78 78 78 78 20 20 20 20 20  y  10xxxxxx     
a88e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a88f0 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30 30    00000000 00000
a8900 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20  yyy yyxxxxxx.** 
a8910 20 31 31 31 30 7a 7a 7a 7a 20 20 31 30 79 79 79   1110zzzz  10yyy
a8920 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20  yyy  10xxxxxx   
a8930 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30            000000
a8940 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78  00 zzzzyyyy yyxx
a8950 78 78 78 78 0a 2a 2a 20 20 31 31 31 31 30 75 75  xxxx.**  11110uu
a8960 75 20 20 31 30 75 75 7a 7a 7a 7a 20 20 31 30 79  u  10uuzzzz  10y
a8970 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20  yyyyy  10xxxxxx 
a8980 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79    000uuuuu zzzzy
a8990 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a  yyy yyxxxxxx.**.
a89a0 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55  **.** Notes on U
a89b0 54 46 2d 31 36 3a 20 20 28 77 69 74 68 20 77 77  TF-16:  (with ww
a89c0 77 77 2b 31 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a  ww+1==uuuuu).**.
a89d0 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d 30 20 20  **      Word-0  
a89e0 20 20 20 20 20 20 20 20 20 20 20 20 20 57 6f 72               Wor
a89f0 64 2d 31 20 20 20 20 20 20 20 20 20 20 56 61 6c  d-1          Val
a8a00 75 65 0a 2a 2a 20 20 31 31 30 31 31 30 77 77 20  ue.**  110110ww 
a8a10 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31 30 31 31  wwzzzzyy   11011
a8a20 31 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20  1yy yyxxxxxx    
a8a30 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79  000uuuuu zzzzyyy
a8a40 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 7a  y yyxxxxxx.**  z
a8a50 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78  zzzyyyy yyxxxxxx
a8a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8a70 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30          00000000
a8a80 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78   zzzzyyyy yyxxxx
a8a90 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20  xx.**.**.** BOM 
a8aa0 6f 72 20 42 79 74 65 20 4f 72 64 65 72 20 4d 61  or Byte Order Ma
a8ab0 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 78 66 66 20  rk:.**     0xff 
a8ac0 30 78 66 65 20 20 20 6c 69 74 74 6c 65 2d 65 6e  0xfe   little-en
a8ad0 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c  dian utf-16 foll
a8ae0 6f 77 73 0a 2a 2a 20 20 20 20 20 30 78 66 65 20  ows.**     0xfe 
a8af0 30 78 66 66 20 20 20 62 69 67 2d 65 6e 64 69 61  0xff   big-endia
a8b00 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73  n utf-16 follows
a8b10 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  .**.*/./********
a8b20 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76  ****** Include v
a8b30 64 62 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20  dbeInt.h in the 
a8b40 6d 69 64 64 6c 65 20 6f 66 20 75 74 66 2e 63 20  middle of utf.c 
a8b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8b60 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
a8b70 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
a8b80 65 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a  e vdbeInt.h ****
a8b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8bb0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
a8bc0 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a  3 September 6.**
a8bd0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
a8be0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
a8bf0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
a8c00 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
a8c10 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
a8c20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
a8c30 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
a8c40 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
a8c50 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
a8c60 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
a8c70 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
a8c80 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
a8c90 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
a8ca0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
a8cb0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
a8cc0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
a8cd0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
a8ce0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
a8cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
a8d30 54 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64  This is the head
a8d40 65 72 20 66 69 6c 65 20 66 6f 72 20 69 6e 66 6f  er file for info
a8d50 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20  rmation that is 
a8d60 70 72 69 76 61 74 65 20 74 6f 20 74 68 65 0a 2a  private to the.*
a8d70 2a 20 56 44 42 45 2e 20 20 54 68 69 73 20 69 6e  * VDBE.  This in
a8d80 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 74  formation used t
a8d90 6f 20 61 6c 6c 20 62 65 20 61 74 20 74 68 65 20  o all be at the 
a8da0 74 6f 70 20 6f 66 20 74 68 65 20 73 69 6e 67 6c  top of the singl
a8db0 65 0a 2a 2a 20 73 6f 75 72 63 65 20 63 6f 64 65  e.** source code
a8dc0 20 66 69 6c 65 20 22 76 64 62 65 2e 63 22 2e 20   file "vdbe.c". 
a8dd0 20 57 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20   When that file 
a8de0 62 65 63 61 6d 65 20 74 6f 6f 20 62 69 67 20 28  became too big (
a8df0 6f 76 65 72 0a 2a 2a 20 36 30 30 30 20 6c 69 6e  over.** 6000 lin
a8e00 65 73 20 6c 6f 6e 67 29 20 69 74 20 77 61 73 20  es long) it was 
a8e10 73 70 6c 69 74 20 75 70 20 69 6e 74 6f 20 73 65  split up into se
a8e20 76 65 72 61 6c 20 73 6d 61 6c 6c 65 72 20 66 69  veral smaller fi
a8e30 6c 65 73 20 61 6e 64 0a 2a 2a 20 74 68 69 73 20  les and.** this 
a8e40 68 65 61 64 65 72 20 69 6e 66 6f 72 6d 61 74 69  header informati
a8e50 6f 6e 20 77 61 73 20 66 61 63 74 6f 72 65 64 20  on was factored 
a8e60 6f 75 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  out..*/.#ifndef 
a8e70 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 23 64 65 66  _VDBEINT_H_.#def
a8e80 69 6e 65 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a  ine _VDBEINT_H_.
a8e90 0a 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73 20 74 72  ./*.** SQL is tr
a8ea0 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20  anslated into a 
a8eb0 73 65 71 75 65 6e 63 65 20 6f 66 20 69 6e 73 74  sequence of inst
a8ec0 72 75 63 74 69 6f 6e 73 20 74 6f 20 62 65 0a 2a  ructions to be.*
a8ed0 2a 20 65 78 65 63 75 74 65 64 20 62 79 20 61 20  * executed by a 
a8ee0 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e  virtual machine.
a8ef0 20 20 45 61 63 68 20 69 6e 73 74 72 75 63 74 69    Each instructi
a8f00 6f 6e 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  on is an instanc
a8f10 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c  e.** of the foll
a8f20 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
a8f30 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
a8f40 63 74 20 56 64 62 65 4f 70 20 4f 70 3b 0a 0a 2f  ct VdbeOp Op;../
a8f50 2a 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20 76 61 6c  *.** Boolean val
a8f60 75 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75  ues.*/.typedef u
a8f70 6e 73 69 67 6e 65 64 20 63 68 61 72 20 42 6f 6f  nsigned char Boo
a8f80 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73  l;../*.** A curs
a8f90 6f 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  or is a pointer 
a8fa0 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 42 54  into a single BT
a8fb0 72 65 65 20 77 69 74 68 69 6e 20 61 20 64 61 74  ree within a dat
a8fc0 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 54  abase file..** T
a8fd0 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 73 65  he cursor can se
a8fe0 65 6b 20 74 6f 20 61 20 42 54 72 65 65 20 65 6e  ek to a BTree en
a8ff0 74 72 79 20 77 69 74 68 20 61 20 70 61 72 74 69  try with a parti
a9000 63 75 6c 61 72 20 6b 65 79 2c 20 6f 72 0a 2a 2a  cular key, or.**
a9010 20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20 65   loop over all e
a9020 6e 74 72 69 65 73 20 6f 66 20 74 68 65 20 42 74  ntries of the Bt
a9030 72 65 65 2e 20 20 59 6f 75 20 63 61 6e 20 61 6c  ree.  You can al
a9040 73 6f 20 69 6e 73 65 72 74 20 6e 65 77 20 42 54  so insert new BT
a9050 72 65 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 6f  ree.** entries o
a9060 72 20 72 65 74 72 69 65 76 65 20 74 68 65 20 6b  r retrieve the k
a9070 65 79 20 6f 72 20 64 61 74 61 20 66 72 6f 6d 20  ey or data from 
a9080 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74  the entry that t
a9090 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20  he cursor.** is 
a90a0 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
a90b0 6e 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 45 76  ng to..** .** Ev
a90c0 65 72 79 20 63 75 72 73 6f 72 20 74 68 61 74 20  ery cursor that 
a90d0 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
a90e0 69 6e 65 20 68 61 73 20 6f 70 65 6e 20 69 73 20  ine has open is 
a90f0 72 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61  represented by a
a9100 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66  n.** instance of
a9110 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
a9120 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
a9130 49 66 20 74 68 65 20 56 64 62 65 43 75 72 73 6f  If the VdbeCurso
a9140 72 2e 69 73 54 72 69 67 67 65 72 52 6f 77 20 66  r.isTriggerRow f
a9150 6c 61 67 20 69 73 20 73 65 74 20 69 74 20 6d 65  lag is set it me
a9160 61 6e 73 20 74 68 61 74 20 74 68 69 73 20 63 75  ans that this cu
a9170 72 73 6f 72 20 69 73 0a 2a 2a 20 72 65 61 6c 6c  rsor is.** reall
a9180 79 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 74  y a single row t
a9190 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74  hat represents t
a91a0 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73  he NEW or OLD ps
a91b0 65 75 64 6f 2d 74 61 62 6c 65 20 6f 66 0a 2a 2a  eudo-table of.**
a91c0 20 61 20 72 6f 77 20 74 72 69 67 67 65 72 2e 20   a row trigger. 
a91d0 20 54 68 65 20 64 61 74 61 20 66 6f 72 20 74 68   The data for th
a91e0 65 20 72 6f 77 20 69 73 20 73 74 6f 72 65 64 20  e row is stored 
a91f0 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e 70 44  in VdbeCursor.pD
a9200 61 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72  ata and.** the r
a9210 6f 77 69 64 20 69 73 20 69 6e 20 56 64 62 65 43  owid is in VdbeC
a9220 75 72 73 6f 72 2e 69 4b 65 79 2e 0a 2a 2f 0a 73  ursor.iKey..*/.s
a9230 74 72 75 63 74 20 56 64 62 65 43 75 72 73 6f 72  truct VdbeCursor
a9240 20 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70   {.  BtCursor *p
a9250 43 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 54 68  Cursor;    /* Th
a9260 65 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75  e cursor structu
a9270 72 65 20 6f 66 20 74 68 65 20 62 61 63 6b 65 6e  re of the backen
a9280 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  d */.  int iDb; 
a9290 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a92a0 49 6e 64 65 78 20 6f 66 20 63 75 72 73 6f 72 20  Index of cursor 
a92b0 64 61 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e  database in db->
a92c0 61 44 62 5b 5d 20 28 6f 72 20 2d 31 29 20 2a 2f  aDb[] (or -1) */
a92d0 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64  .  i64 lastRowid
a92e0 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74  ;        /* Last
a92f0 20 72 6f 77 69 64 20 66 72 6f 6d 20 61 20 4e 65   rowid from a Ne
a9300 78 74 20 6f 72 20 4e 65 78 74 49 64 78 20 6f 70  xt or NextIdx op
a9310 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 42 6f 6f  eration */.  Boo
a9320 6c 20 7a 65 72 6f 65 64 3b 20 20 20 20 20 20 20  l zeroed;       
a9330 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 65     /* True if ze
a9340 72 6f 65 64 20 6f 75 74 20 61 6e 64 20 72 65 61  roed out and rea
a9350 64 79 20 66 6f 72 20 72 65 75 73 65 20 2a 2f 0a  dy for reuse */.
a9360 20 20 42 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61    Bool rowidIsVa
a9370 6c 69 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20  lid;    /* True 
a9380 69 66 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20  if lastRowid is 
a9390 76 61 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20  valid */.  Bool 
a93a0 61 74 46 69 72 73 74 3b 20 20 20 20 20 20 20 20  atFirst;        
a93b0 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e   /* True if poin
a93c0 74 69 6e 67 20 74 6f 20 66 69 72 73 74 20 65 6e  ting to first en
a93d0 74 72 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 75 73  try */.  Bool us
a93e0 65 52 61 6e 64 6f 6d 52 6f 77 69 64 3b 20 20 2f  eRandomRowid;  /
a93f0 2a 20 47 65 6e 65 72 61 74 65 20 6e 65 77 20 72  * Generate new r
a9400 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 73 65  ecord numbers se
a9410 6d 69 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f 0a 20  mi-randomly */. 
a9420 20 42 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b 20 20   Bool nullRow;  
a9430 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
a9440 66 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20  f pointing to a 
a9450 72 6f 77 20 77 69 74 68 20 6e 6f 20 64 61 74 61  row with no data
a9460 20 2a 2f 0a 20 20 42 6f 6f 6c 20 64 65 66 65 72   */.  Bool defer
a9470 72 65 64 4d 6f 76 65 74 6f 3b 20 20 2f 2a 20 41  redMoveto;  /* A
a9480 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
a9490 42 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 69 73  BtreeMoveto() is
a94a0 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 42 6f 6f   needed */.  Boo
a94b0 6c 20 69 73 54 61 62 6c 65 3b 20 20 20 20 20 20  l isTable;      
a94c0 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20     /* True if a 
a94d0 74 61 62 6c 65 20 72 65 71 75 69 72 69 6e 67 20  table requiring 
a94e0 69 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a  integer keys */.
a94f0 20 20 42 6f 6f 6c 20 69 73 49 6e 64 65 78 3b 20    Bool isIndex; 
a9500 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
a9510 69 66 20 61 6e 20 69 6e 64 65 78 20 63 6f 6e 74  if an index cont
a9520 61 69 6e 69 6e 67 20 6b 65 79 73 20 6f 6e 6c 79  aining keys only
a9530 20 2d 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20   - no data */.  
a9540 69 36 34 20 6d 6f 76 65 74 6f 54 61 72 67 65 74  i64 movetoTarget
a9550 3b 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e  ;     /* Argumen
a9560 74 20 74 6f 20 74 68 65 20 64 65 66 65 72 72 65  t to the deferre
a9570 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  d sqlite3BtreeMo
a9580 76 65 74 6f 28 29 20 2a 2f 0a 20 20 42 74 72 65  veto() */.  Btre
a9590 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20  e *pBt;         
a95a0 20 20 2f 2a 20 53 65 70 61 72 61 74 65 20 66 69    /* Separate fi
a95b0 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f  le holding tempo
a95c0 72 61 72 79 20 74 61 62 6c 65 20 2a 2f 0a 20 20  rary table */.  
a95d0 69 6e 74 20 70 73 65 75 64 6f 54 61 62 6c 65 52  int pseudoTableR
a95e0 65 67 3b 20 20 20 2f 2a 20 52 65 67 69 73 74 65  eg;   /* Registe
a95f0 72 20 68 6f 6c 64 69 6e 67 20 70 73 65 75 64 6f  r holding pseudo
a9600 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74 2e 20 2a  table content. *
a9610 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  /.  KeyInfo *pKe
a9620 79 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66  yInfo;    /* Inf
a9630 6f 20 61 62 6f 75 74 20 69 6e 64 65 78 20 6b 65  o about index ke
a9640 79 73 20 6e 65 65 64 65 64 20 62 79 20 69 6e 64  ys needed by ind
a9650 65 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20  ex cursors */.  
a9660 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20  int nField;     
a9670 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
a9680 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65  of fields in the
a9690 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34   header */.  i64
a96a0 20 73 65 71 43 6f 75 6e 74 3b 20 20 20 20 20 20   seqCount;      
a96b0 20 20 20 2f 2a 20 53 65 71 75 65 6e 63 65 20 63     /* Sequence c
a96c0 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69  ounter */.  sqli
a96d0 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
a96e0 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 20 20 2f  *pVtabCursor;  /
a96f0 2a 20 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72  * The cursor for
a9700 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
a9710 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   */.  const sqli
a9720 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
a9730 75 6c 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64 75  ule;     /* Modu
a9740 6c 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 56  le for cursor pV
a9750 74 61 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20 20  tabCursor */..  
a9760 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73  /* Result of las
a9770 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  t sqlite3BtreeMo
a9780 76 65 74 6f 28 29 20 64 6f 6e 65 20 62 79 20 61  veto() done by a
a9790 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f  n OP_NotExists o
a97a0 72 20 0a 20 20 2a 2a 20 4f 50 5f 49 73 55 6e 69  r .  ** OP_IsUni
a97b0 71 75 65 20 6f 70 63 6f 64 65 20 6f 6e 20 74 68  que opcode on th
a97c0 69 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20  is cursor. */.  
a97d0 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 0a  int seekResult;.
a97e0 0a 20 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66  .  /* Cached inf
a97f0 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
a9800 68 65 20 68 65 61 64 65 72 20 66 6f 72 20 74 68  he header for th
a9810 65 20 64 61 74 61 20 72 65 63 6f 72 64 20 74 68  e data record th
a9820 61 74 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73  at the.  ** curs
a9830 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  or is currently 
a9840 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e  pointing to.  On
a9850 6c 79 20 76 61 6c 69 64 20 69 66 20 63 61 63 68  ly valid if cach
a9860 65 53 74 61 74 75 73 20 6d 61 74 63 68 65 73 0a  eStatus matches.
a9870 20 20 2a 2a 20 56 64 62 65 2e 63 61 63 68 65 43    ** Vdbe.cacheC
a9880 74 72 2e 20 20 56 64 62 65 2e 63 61 63 68 65 43  tr.  Vdbe.cacheC
a9890 74 72 20 77 69 6c 6c 20 6e 65 76 65 72 20 74 61  tr will never ta
a98a0 6b 65 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20  ke on the value 
a98b0 6f 66 0a 20 20 2a 2a 20 43 41 43 48 45 5f 53 54  of.  ** CACHE_ST
a98c0 41 4c 45 20 61 6e 64 20 73 6f 20 73 65 74 74 69  ALE and so setti
a98d0 6e 67 20 63 61 63 68 65 53 74 61 74 75 73 3d 43  ng cacheStatus=C
a98e0 41 43 48 45 5f 53 54 41 4c 45 20 67 75 61 72 61  ACHE_STALE guara
a98f0 6e 74 65 65 73 20 74 68 61 74 0a 20 20 2a 2a 20  ntees that.  ** 
a9900 74 68 65 20 63 61 63 68 65 20 69 73 20 6f 75 74  the cache is out
a9910 20 6f 66 20 64 61 74 65 2e 0a 20 20 2a 2a 0a 20   of date..  **. 
a9920 20 2a 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70   ** aRow might p
a9930 6f 69 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72  oint to (ephemer
a9940 61 6c 29 20 64 61 74 61 20 66 6f 72 20 74 68 65  al) data for the
a9950 20 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72   current row, or
a9960 20 69 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62   it might.  ** b
a9970 65 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 75  e NULL..  */.  u
a9980 33 32 20 63 61 63 68 65 53 74 61 74 75 73 3b 20  32 cacheStatus; 
a9990 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73       /* Cache is
a99a0 20 76 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d   valid if this m
a99b0 61 74 63 68 65 73 20 56 64 62 65 2e 63 61 63 68  atches Vdbe.cach
a99c0 65 43 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61  eCtr */.  int pa
a99d0 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20  yloadSize;      
a99e0 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20  /* Total number 
a99f0 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
a9a00 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20  record */.  u32 
a9a10 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 20  *aType;         
a9a20 20 20 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73    /* Type values
a9a30 20 66 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73   for all entries
a9a40 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a   in the record *
a9a50 2f 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74  /.  u32 *aOffset
a9a60 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63  ;         /* Cac
a9a70 68 65 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74  hed offsets to t
a9a80 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68  he start of each
a9a90 20 63 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f   columns data */
a9aa0 0a 20 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20  .  u8 *aRow;    
a9ab0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
a9ac0 20 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74   for the current
a9ad0 20 72 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20   row, if all on 
a9ae0 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74  one page */.};.t
a9af0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64  ypedef struct Vd
a9b00 62 65 43 75 72 73 6f 72 20 56 64 62 65 43 75 72  beCursor VdbeCur
a9b10 73 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  sor;../*.** When
a9b20 20 61 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69   a sub-program i
a9b30 73 20 65 78 65 63 75 74 65 64 20 28 4f 50 5f 50  s executed (OP_P
a9b40 72 6f 67 72 61 6d 29 2c 20 61 20 73 74 72 75 63  rogram), a struc
a9b50 74 75 72 65 20 6f 66 20 74 68 69 73 20 74 79 70  ture of this typ
a9b60 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65  e.** is allocate
a9b70 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63  d to store the c
a9b80 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20  urrent value of 
a9b90 74 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e  the program coun
a9ba0 74 65 72 2c 20 61 73 0a 2a 2a 20 77 65 6c 6c 20  ter, as.** well 
a9bb0 61 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d  as the current m
a9bc0 65 6d 6f 72 79 20 63 65 6c 6c 20 61 72 72 61 79  emory cell array
a9bd0 20 61 6e 64 20 76 61 72 69 6f 75 73 20 6f 74 68   and various oth
a9be0 65 72 20 66 72 61 6d 65 20 73 70 65 63 69 66 69  er frame specifi
a9bf0 63 0a 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72  c.** values stor
a9c00 65 64 20 69 6e 20 74 68 65 20 56 64 62 65 20 73  ed in the Vdbe s
a9c10 74 72 75 63 74 2e 20 57 68 65 6e 20 74 68 65 20  truct. When the 
a9c20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 66  sub-program is f
a9c30 69 6e 69 73 68 65 64 2c 20 0a 2a 2a 20 74 68 65  inished, .** the
a9c40 73 65 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f  se values are co
a9c50 70 69 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65  pied back to the
a9c60 20 56 64 62 65 20 66 72 6f 6d 20 74 68 65 20 56   Vdbe from the V
a9c70 64 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 75  dbeFrame structu
a9c80 72 65 2c 0a 2a 2a 20 72 65 73 74 6f 72 69 6e 67  re,.** restoring
a9c90 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
a9ca0 65 20 56 4d 20 74 6f 20 61 73 20 69 74 20 77 61  e VM to as it wa
a9cb0 73 20 62 65 66 6f 72 65 20 74 68 65 20 73 75 62  s before the sub
a9cc0 2d 70 72 6f 67 72 61 6d 0a 2a 2a 20 62 65 67 61  -program.** bega
a9cd0 6e 20 65 78 65 63 75 74 69 6e 67 2e 0a 2a 2a 0a  n executing..**.
a9ce0 2a 2a 20 46 72 61 6d 65 73 20 61 72 65 20 73 74  ** Frames are st
a9cf0 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64  ored in a linked
a9d00 20 6c 69 73 74 20 68 65 61 64 65 64 20 61 74 20   list headed at 
a9d10 56 64 62 65 2e 70 50 61 72 65 6e 74 2e 20 56 64  Vdbe.pParent. Vd
a9d20 62 65 2e 70 50 61 72 65 6e 74 0a 2a 2a 20 69 73  be.pParent.** is
a9d30 20 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 74   the parent of t
a9d40 68 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65  he current frame
a9d50 2c 20 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65  , or zero if the
a9d60 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 0a 2a   current frame.*
a9d70 2a 20 69 73 20 74 68 65 20 6d 61 69 6e 20 56 64  * is the main Vd
a9d80 62 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 74  be program..*/.t
a9d90 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64  ypedef struct Vd
a9da0 62 65 46 72 61 6d 65 20 56 64 62 65 46 72 61 6d  beFrame VdbeFram
a9db0 65 3b 0a 73 74 72 75 63 74 20 56 64 62 65 46 72  e;.struct VdbeFr
a9dc0 61 6d 65 20 7b 0a 20 20 56 64 62 65 20 2a 76 3b  ame {.  Vdbe *v;
a9dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a9de0 2f 2a 20 56 4d 20 74 68 69 73 20 66 72 61 6d 65  /* VM this frame
a9df0 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 20   belongs to */. 
a9e00 20 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20   int pc;        
a9e10 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67           /* Prog
a9e20 72 61 6d 20 43 6f 75 6e 74 65 72 20 2a 2f 0a 20  ram Counter */. 
a9e30 20 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20   Op *aOp;       
a9e40 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67           /* Prog
a9e50 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ram instructions
a9e60 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20   */.  int nOp;  
a9e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a9e80 20 53 69 7a 65 20 6f 66 20 61 4f 70 20 61 72 72   Size of aOp arr
a9e90 61 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65  ay */.  Mem *aMe
a9ea0 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
a9eb0 2f 2a 20 41 72 72 61 79 20 6f 66 20 6d 65 6d 6f  /* Array of memo
a9ec0 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 69 6e  ry cells */.  in
a9ed0 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t nMem;         
a9ee0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
a9ef0 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 4d  of entries in aM
a9f00 65 6d 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73  em */.  VdbeCurs
a9f10 6f 72 20 2a 2a 61 70 43 73 72 3b 20 20 20 20 20  or **apCsr;     
a9f20 2f 2a 20 45 6c 65 6d 65 6e 74 20 6f 66 20 56 64  /* Element of Vd
a9f30 62 65 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20  be cursors */.  
a9f40 75 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20  u16 nCursor;    
a9f50 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
a9f60 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
a9f70 61 70 43 73 72 20 2a 2f 0a 20 20 76 6f 69 64 20  apCsr */.  void 
a9f80 2a 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  *token;         
a9f90 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 53 75     /* Copy of Su
a9fa0 62 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e 20 2a  bProgram.token *
a9fb0 2f 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 4d 65  /.  int nChildMe
a9fc0 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  m;          /* N
a9fd0 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20  umber of memory 
a9fe0 63 65 6c 6c 73 20 66 6f 72 20 63 68 69 6c 64 20  cells for child 
a9ff0 66 72 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  frame */.  int n
aa000 43 68 69 6c 64 43 73 72 3b 20 20 20 20 20 20 20  ChildCsr;       
aa010 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
aa020 63 75 72 73 6f 72 73 20 66 6f 72 20 63 68 69 6c  cursors for chil
aa030 64 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 36 34  d frame */.  i64
aa040 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20   lastRowid;     
aa050 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 69 6e 73       /* Last ins
aa060 65 72 74 20 72 6f 77 69 64 20 28 73 71 6c 69 74  ert rowid (sqlit
aa070 65 33 2e 6c 61 73 74 52 6f 77 69 64 29 20 2a 2f  e3.lastRowid) */
aa080 0a 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20  .  int nChange; 
aa090 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
aa0a0 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 73 20  atement changes 
aa0b0 28 56 64 62 65 2e 6e 43 68 61 6e 67 65 73 29 20  (Vdbe.nChanges) 
aa0c0 20 20 20 20 2a 2f 0a 20 20 56 64 62 65 46 72 61      */.  VdbeFra
aa0d0 6d 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20  me *pParent;    
aa0e0 20 2f 2a 20 50 61 72 65 6e 74 20 6f 66 20 74 68   /* Parent of th
aa0f0 69 73 20 66 72 61 6d 65 20 2a 2f 0a 7d 3b 0a 0a  is frame */.};..
aa100 23 64 65 66 69 6e 65 20 56 64 62 65 46 72 61 6d  #define VdbeFram
aa110 65 4d 65 6d 28 70 29 20 28 28 4d 65 6d 20 2a 29  eMem(p) ((Mem *)
aa120 26 28 28 75 38 20 2a 29 70 29 5b 52 4f 55 4e 44  &((u8 *)p)[ROUND
aa130 38 28 73 69 7a 65 6f 66 28 56 64 62 65 46 72 61  8(sizeof(VdbeFra
aa140 6d 65 29 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 41 20  me))])../*.** A 
aa150 76 61 6c 75 65 20 66 6f 72 20 56 64 62 65 43 75  value for VdbeCu
aa160 72 73 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20  rsor.cacheValid 
aa170 74 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63  that means the c
aa180 61 63 68 65 20 69 73 20 61 6c 77 61 79 73 20 69  ache is always i
aa190 6e 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69  nvalid..*/.#defi
aa1a0 6e 65 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30  ne CACHE_STALE 0
aa1b0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c  ../*.** Internal
aa1c0 6c 79 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e  ly, the vdbe man
aa1d0 69 70 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20  ipulates nearly 
aa1e0 61 6c 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61  all SQL values a
aa1f0 73 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75  s Mem.** structu
aa200 72 65 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74  res. Each Mem st
aa210 72 75 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d  ruct may cache m
aa220 75 6c 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e  ultiple represen
aa230 74 61 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c  tations (string,
aa240 0a 2a 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e  .** integer etc.
aa250 29 20 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61  ) of the same va
aa260 6c 75 65 2e 20 20 41 20 76 61 6c 75 65 20 28 61  lue.  A value (a
aa270 6e 64 20 74 68 65 72 65 66 6f 72 65 20 4d 65 6d  nd therefore Mem
aa280 20 73 74 72 75 63 74 75 72 65 29 0a 2a 2a 20 68   structure).** h
aa290 61 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  as the following
aa2a0 20 70 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a 0a   properties:.**.
aa2b0 2a 2a 20 45 61 63 68 20 76 61 6c 75 65 20 68 61  ** Each value ha
aa2c0 73 20 61 20 6d 61 6e 69 66 65 73 74 20 74 79 70  s a manifest typ
aa2d0 65 2e 20 54 68 65 20 6d 61 6e 69 66 65 73 74 20  e. The manifest 
aa2e0 74 79 70 65 20 6f 66 20 74 68 65 20 76 61 6c 75  type of the valu
aa2f0 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61  e stored.** in a
aa300 20 4d 65 6d 20 73 74 72 75 63 74 20 69 73 20 72   Mem struct is r
aa310 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 4d  eturned by the M
aa320 65 6d 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61 63  emType(Mem*) mac
aa330 72 6f 2e 20 54 68 65 20 74 79 70 65 20 69 73 0a  ro. The type is.
aa340 2a 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45  ** one of SQLITE
aa350 5f 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e  _NULL, SQLITE_IN
aa360 54 45 47 45 52 2c 20 53 51 4c 49 54 45 5f 52 45  TEGER, SQLITE_RE
aa370 41 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 20  AL, SQLITE_TEXT 
aa380 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c 4f  or.** SQLITE_BLO
aa390 42 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d  B..*/.struct Mem
aa3a0 20 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20   {.  union {.   
aa3b0 20 69 36 34 20 69 3b 20 20 20 20 20 20 20 20 20   i64 i;         
aa3c0 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20       /* Integer 
aa3d0 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 69 6e  value. */.    in
aa3e0 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20  t nZero;        
aa3f0 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 62    /* Used when b
aa400 69 74 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 20 73  it MEM_Zero is s
aa410 65 74 20 69 6e 20 66 6c 61 67 73 20 2a 2f 0a 20  et in flags */. 
aa420 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66     FuncDef *pDef
aa430 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 6f  ;      /* Used o
aa440 6e 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d  nly when flags==
aa450 4d 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 20 52  MEM_Agg */.    R
aa460 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74 3b 20  owSet *pRowSet; 
aa470 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20     /* Used only 
aa480 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f  when flags==MEM_
aa490 52 6f 77 53 65 74 20 2a 2f 0a 20 20 20 20 56 64  RowSet */.    Vd
aa4a0 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
aa4b0 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 66    /* Used when f
aa4c0 6c 61 67 73 3d 3d 4d 45 4d 5f 46 72 61 6d 65 20  lags==MEM_Frame 
aa4d0 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 64 6f 75 62  */.  } u;.  doub
aa4e0 6c 65 20 72 3b 20 20 20 20 20 20 20 20 20 20 20  le r;           
aa4f0 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 2a 2f  /* Real value */
aa500 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
aa510 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73         /* The as
aa520 73 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73  sociated databas
aa530 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
aa540 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20    char *z;      
aa550 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20        /* String 
aa560 6f 72 20 42 4c 4f 42 20 76 61 6c 75 65 20 2a 2f  or BLOB value */
aa570 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20  .  int n;       
aa580 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
aa590 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69   of characters i
aa5a0 6e 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2c 20  n string value, 
aa5b0 65 78 63 6c 75 64 69 6e 67 20 27 5c 30 27 20 2a  excluding '\0' *
aa5c0 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20  /.  u16 flags;  
aa5d0 20 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20          /* Some 
aa5e0 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4d  combination of M
aa5f0 45 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f 53 74 72  EM_Null, MEM_Str
aa600 2c 20 4d 45 4d 5f 44 79 6e 2c 20 65 74 63 2e 20  , MEM_Dyn, etc. 
aa610 2a 2f 0a 20 20 75 38 20 20 74 79 70 65 3b 20 20  */.  u8  type;  
aa620 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
aa630 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20  of SQLITE_NULL, 
aa640 53 51 4c 49 54 45 5f 54 45 58 54 2c 20 53 51 4c  SQLITE_TEXT, SQL
aa650 49 54 45 5f 49 4e 54 45 47 45 52 2c 20 65 74 63  ITE_INTEGER, etc
aa660 20 2a 2f 0a 20 20 75 38 20 20 65 6e 63 3b 20 20   */.  u8  enc;  
aa670 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c            /* SQL
aa680 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
aa690 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45  _UTF16BE, SQLITE
aa6a0 5f 55 54 46 31 36 4c 45 20 2a 2f 0a 20 20 76 6f  _UTF16LE */.  vo
aa6b0 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20  id (*xDel)(void 
aa6c0 2a 29 3b 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e  *);  /* If not n
aa6d0 75 6c 6c 2c 20 63 61 6c 6c 20 74 68 69 73 20 66  ull, call this f
aa6e0 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65 6c 65 74  unction to delet
aa6f0 65 20 4d 65 6d 2e 7a 20 2a 2f 0a 20 20 63 68 61  e Mem.z */.  cha
aa700 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 20 20  r *zMalloc;     
aa710 20 2f 2a 20 44 79 6e 61 6d 69 63 20 62 75 66 66   /* Dynamic buff
aa720 65 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  er allocated by 
aa730 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
aa740 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f 6e 65 20 6f   */.};../* One o
aa750 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 66 6f  r more of the fo
aa760 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 61 72  llowing flags ar
aa770 65 20 73 65 74 20 74 6f 20 69 6e 64 69 63 61 74  e set to indicat
aa780 65 20 74 68 65 20 76 61 6c 69 64 4f 4b 0a 2a 2a  e the validOK.**
aa790 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73   representations
aa7a0 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 73 74   of the value st
aa7b0 6f 72 65 64 20 69 6e 20 74 68 65 20 4d 65 6d 20  ored in the Mem 
aa7c0 73 74 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  struct..**.** If
aa7d0 20 74 68 65 20 4d 45 4d 5f 4e 75 6c 6c 20 66 6c   the MEM_Null fl
aa7e0 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  ag is set, then 
aa7f0 74 68 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20  the value is an 
aa800 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2e 0a  SQL NULL value..
aa810 2a 2a 20 4e 6f 20 6f 74 68 65 72 20 66 6c 61 67  ** No other flag
aa820 73 20 6d 61 79 20 62 65 20 73 65 74 20 69 6e 20  s may be set in 
aa830 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a  this case..**.**
aa840 20 49 66 20 74 68 65 20 4d 45 4d 5f 53 74 72 20   If the MEM_Str 
aa850 66 6c 61 67 20 69 73 20 73 65 74 20 74 68 65 6e  flag is set then
aa860 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 61 74   Mem.z points at
aa870 20 61 20 73 74 72 69 6e 67 20 72 65 70 72 65 73   a string repres
aa880 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 55 73 75  entation..** Usu
aa890 61 6c 6c 79 20 74 68 69 73 20 69 73 20 65 6e 63  ally this is enc
aa8a0 6f 64 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  oded in the same
aa8b0 20 75 6e 69 63 6f 64 65 20 65 6e 63 6f 64 69 6e   unicode encodin
aa8c0 67 20 61 73 20 74 68 65 20 6d 61 69 6e 0a 2a 2a  g as the main.**
aa8d0 20 64 61 74 61 62 61 73 65 20 28 73 65 65 20 62   database (see b
aa8e0 65 6c 6f 77 20 66 6f 72 20 65 78 63 65 70 74 69  elow for excepti
aa8f0 6f 6e 73 29 2e 20 49 66 20 74 68 65 20 4d 45 4d  ons). If the MEM
aa900 5f 54 65 72 6d 20 66 6c 61 67 20 69 73 20 61 6c  _Term flag is al
aa910 73 6f 0a 2a 2a 20 73 65 74 2c 20 74 68 65 6e 20  so.** set, then 
aa920 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 6e 75  the string is nu
aa930 6c 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 54 68  l terminated. Th
aa940 65 20 4d 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45  e MEM_Int and ME
aa950 4d 5f 52 65 61 6c 20 0a 2a 2a 20 66 6c 61 67 73  M_Real .** flags
aa960 20 6d 61 79 20 63 6f 65 78 69 73 74 20 77 69 74   may coexist wit
aa970 68 20 74 68 65 20 4d 45 4d 5f 53 74 72 20 66 6c  h the MEM_Str fl
aa980 61 67 2e 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70  ag..**.** Multip
aa990 6c 65 20 6f 66 20 74 68 65 73 65 20 76 61 6c 75  le of these valu
aa9a0 65 73 20 63 61 6e 20 61 70 70 65 61 72 20 69 6e  es can appear in
aa9b0 20 4d 65 6d 2e 66 6c 61 67 73 2e 20 20 42 75 74   Mem.flags.  But
aa9c0 20 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a 20 61 74 20   only one.** at 
aa9d0 61 20 74 69 6d 65 20 63 61 6e 20 61 70 70 65 61  a time can appea
aa9e0 72 20 69 6e 20 4d 65 6d 2e 74 79 70 65 2e 0a 2a  r in Mem.type..*
aa9f0 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 4e 75  /.#define MEM_Nu
aaa00 6c 6c 20 20 20 20 20 20 30 78 30 30 30 31 20 20  ll      0x0001  
aaa10 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 4e 55 4c   /* Value is NUL
aaa20 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  L */.#define MEM
aaa30 5f 53 74 72 20 20 20 20 20 20 20 30 78 30 30 30  _Str       0x000
aaa40 32 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  2   /* Value is 
aaa50 61 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66  a string */.#def
aaa60 69 6e 65 20 4d 45 4d 5f 49 6e 74 20 20 20 20 20  ine MEM_Int     
aaa70 20 20 30 78 30 30 30 34 20 20 20 2f 2a 20 56 61    0x0004   /* Va
aaa80 6c 75 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65  lue is an intege
aaa90 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  r */.#define MEM
aaaa0 5f 52 65 61 6c 20 20 20 20 20 20 30 78 30 30 30  _Real      0x000
aaab0 38 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  8   /* Value is 
aaac0 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 2a 2f  a real number */
aaad0 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 42 6c 6f  .#define MEM_Blo
aaae0 62 20 20 20 20 20 20 30 78 30 30 31 30 20 20 20  b      0x0010   
aaaf0 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 20 42 4c  /* Value is a BL
aab00 4f 42 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  OB */.#define ME
aab10 4d 5f 52 6f 77 53 65 74 20 20 20 20 30 78 30 30  M_RowSet    0x00
aab20 32 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73  20   /* Value is
aab30 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74   a RowSet object
aab40 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
aab50 46 72 61 6d 65 20 20 20 20 20 30 78 30 30 34 30  Frame     0x0040
aab60 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61     /* Value is a
aab70 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63   VdbeFrame objec
aab80 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  t */.#define MEM
aab90 5f 54 79 70 65 4d 61 73 6b 20 20 30 78 30 30 66  _TypeMask  0x00f
aaba0 66 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 74  f   /* Mask of t
aabb0 79 70 65 20 62 69 74 73 20 2a 2f 0a 0a 2f 2a 20  ype bits */../* 
aabc0 57 68 65 6e 65 76 65 72 20 4d 65 6d 20 63 6f 6e  Whenever Mem con
aabd0 74 61 69 6e 73 20 61 20 76 61 6c 69 64 20 73 74  tains a valid st
aabe0 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 72 65 70  ring or blob rep
aabf0 72 65 73 65 6e 74 61 74 69 6f 6e 2c 20 6f 6e 65  resentation, one
aac00 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f   of.** the follo
aac10 77 69 6e 67 20 66 6c 61 67 73 20 6d 75 73 74 20  wing flags must 
aac20 62 65 20 73 65 74 20 74 6f 20 64 65 74 65 72 6d  be set to determ
aac30 69 6e 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d  ine the memory m
aac40 61 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 70 6f 6c  anagement.** pol
aac50 69 63 79 20 66 6f 72 20 4d 65 6d 2e 7a 2e 20 20  icy for Mem.z.  
aac60 54 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61  The MEM_Term fla
aac70 67 20 74 65 6c 6c 73 20 75 73 20 77 68 65 74 68  g tells us wheth
aac80 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a 2a 2a  er or not the.**
aac90 20 73 74 72 69 6e 67 20 69 73 20 5c 30 30 30 20   string is \000 
aaca0 6f 72 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e  or \u0000 termin
aacb0 61 74 65 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ated.*/.#define 
aacc0 4d 45 4d 5f 54 65 72 6d 20 20 20 20 20 20 30 78  MEM_Term      0x
aacd0 30 32 30 30 20 20 20 2f 2a 20 53 74 72 69 6e 67  0200   /* String
aace0 20 72 65 70 20 69 73 20 6e 75 6c 20 74 65 72 6d   rep is nul term
aacf0 69 6e 61 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e  inated */.#defin
aad00 65 20 4d 45 4d 5f 44 79 6e 20 20 20 20 20 20 20  e MEM_Dyn       
aad10 30 78 30 34 30 30 20 20 20 2f 2a 20 4e 65 65 64  0x0400   /* Need
aad20 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 46   to call sqliteF
aad30 72 65 65 28 29 20 6f 6e 20 4d 65 6d 2e 7a 20 2a  ree() on Mem.z *
aad40 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74  /.#define MEM_St
aad50 61 74 69 63 20 20 20 20 30 78 30 38 30 30 20 20  atic    0x0800  
aad60 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73   /* Mem.z points
aad70 20 74 6f 20 61 20 73 74 61 74 69 63 20 73 74 72   to a static str
aad80 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ing */.#define M
aad90 45 4d 5f 45 70 68 65 6d 20 20 20 20 20 30 78 31  EM_Ephem     0x1
aada0 30 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70  000   /* Mem.z p
aadb0 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 70 68 65  oints to an ephe
aadc0 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 2a 2f 0a  meral string */.
aadd0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 41 67 67 20  #define MEM_Agg 
aade0 20 20 20 20 20 20 30 78 32 30 30 30 20 20 20 2f        0x2000   /
aadf0 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74  * Mem.z points t
aae00 6f 20 61 6e 20 61 67 67 20 66 75 6e 63 74 69 6f  o an agg functio
aae10 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 23 64 65  n context */.#de
aae20 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 20 20  fine MEM_Zero   
aae30 20 20 20 30 78 34 30 30 30 20 20 20 2f 2a 20 4d     0x4000   /* M
aae40 65 6d 2e 69 20 63 6f 6e 74 61 69 6e 73 20 63 6f  em.i contains co
aae50 75 6e 74 20 6f 66 20 30 73 20 61 70 70 65 6e 64  unt of 0s append
aae60 65 64 20 74 6f 20 62 6c 6f 62 20 2a 2f 0a 0a 23  ed to blob */..#
aae70 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
aae80 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 23 75 6e  T_INCRBLOB.  #un
aae90 64 65 66 20 4d 45 4d 5f 5a 65 72 6f 0a 20 20 23  def MEM_Zero.  #
aaea0 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20  define MEM_Zero 
aaeb0 30 78 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 0a  0x0000.#endif...
aaec0 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 79 20  /*.** Clear any 
aaed0 65 78 69 73 74 69 6e 67 20 74 79 70 65 20 66 6c  existing type fl
aaee0 61 67 73 20 66 72 6f 6d 20 61 20 4d 65 6d 20 61  ags from a Mem a
aaef0 6e 64 20 72 65 70 6c 61 63 65 20 74 68 65 6d 20  nd replace them 
aaf00 77 69 74 68 20 66 0a 2a 2f 0a 23 64 65 66 69 6e  with f.*/.#defin
aaf10 65 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67  e MemSetTypeFlag
aaf20 28 70 2c 20 66 29 20 5c 0a 20 20 20 28 28 70 29  (p, f) \.   ((p)
aaf30 2d 3e 66 6c 61 67 73 20 3d 20 28 28 70 29 2d 3e  ->flags = ((p)->
aaf40 66 6c 61 67 73 26 7e 28 4d 45 4d 5f 54 79 70 65  flags&~(MEM_Type
aaf50 4d 61 73 6b 7c 4d 45 4d 5f 5a 65 72 6f 29 29 7c  Mask|MEM_Zero))|
aaf60 66 29 0a 0a 0a 2f 2a 20 41 20 56 64 62 65 46 75  f).../* A VdbeFu
aaf70 6e 63 20 69 73 20 6a 75 73 74 20 61 20 46 75 6e  nc is just a Fun
aaf80 63 44 65 66 20 28 64 65 66 69 6e 65 64 20 69 6e  cDef (defined in
aaf90 20 73 71 6c 69 74 65 49 6e 74 2e 68 29 20 74 68   sqliteInt.h) th
aafa0 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61  at contains.** a
aafb0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
aafc0 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 75 78 69  ation about auxi
aafd0 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f  liary informatio
aafe0 6e 20 62 6f 75 6e 64 20 74 6f 20 61 72 67 75 6d  n bound to argum
aaff0 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66  ents.** of the f
ab000 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69  unction.  This i
ab010 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  s used to implem
ab020 65 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ent the sqlite3_
ab030 67 65 74 5f 61 75 78 64 61 74 61 28 29 0a 2a 2a  get_auxdata().**
ab040 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 65 74   and sqlite3_set
ab050 5f 61 75 78 64 61 74 61 28 29 20 41 50 49 73 2e  _auxdata() APIs.
ab060 20 20 54 68 65 20 22 61 75 78 64 61 74 61 22 20    The "auxdata" 
ab070 69 73 20 73 6f 6d 65 20 61 75 78 69 6c 69 61 72  is some auxiliar
ab080 79 20 64 61 74 61 0a 2a 2a 20 74 68 61 74 20 63  y data.** that c
ab090 61 6e 20 62 65 20 61 73 73 6f 63 69 61 74 65 64  an be associated
ab0a0 20 77 69 74 68 20 61 20 63 6f 6e 73 74 61 6e 74   with a constant
ab0b0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66   argument to a f
ab0c0 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a  unction.  This.*
ab0d0 2a 20 61 6c 6c 6f 77 73 20 66 75 6e 63 74 69 6f  * allows functio
ab0e0 6e 73 20 73 75 63 68 20 61 73 20 22 72 65 67 65  ns such as "rege
ab0f0 78 70 22 20 74 6f 20 63 6f 6d 70 69 6c 65 20 74  xp" to compile t
ab100 68 65 69 72 20 63 6f 6e 73 74 61 6e 74 20 72 65  heir constant re
ab110 67 75 6c 61 72 0a 2a 2a 20 65 78 70 72 65 73 73  gular.** express
ab120 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 6e 63  ion argument onc
ab130 65 20 61 6e 64 20 72 65 75 73 65 64 20 74 68 65  e and reused the
ab140 20 63 6f 6d 70 69 6c 65 64 20 63 6f 64 65 20 66   compiled code f
ab150 6f 72 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 69  or multiple.** i
ab160 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73  nvocations..*/.s
ab170 74 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 7b  truct VdbeFunc {
ab180 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e  .  FuncDef *pFun
ab190 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
ab1a0 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69   /* The definiti
ab1b0 6f 6e 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  on of the functi
ab1c0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 75 78  on */.  int nAux
ab1d0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ab1e0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
ab1f0 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63  of entries alloc
ab200 61 74 65 64 20 66 6f 72 20 61 70 41 75 78 5b 5d  ated for apAux[]
ab210 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 75 78   */.  struct Aux
ab220 44 61 74 61 20 7b 0a 20 20 20 20 76 6f 69 64 20  Data {.    void 
ab230 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20  *pAux;          
ab240 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 78 20           /* Aux 
ab250 64 61 74 61 20 66 6f 72 20 74 68 65 20 69 2d 74  data for the i-t
ab260 68 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20  h argument */.  
ab270 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65    void (*xDelete
ab280 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20  )(void *);      
ab290 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f  /* Destructor fo
ab2a0 72 20 74 68 65 20 61 75 78 20 64 61 74 61 20 2a  r the aux data *
ab2b0 2f 0a 20 20 7d 20 61 70 41 75 78 5b 31 5d 3b 20  /.  } apAux[1]; 
ab2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ab2d0 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f 74 20 66 6f    /* One slot fo
ab2e0 72 20 65 61 63 68 20 66 75 6e 63 74 69 6f 6e 20  r each function 
ab2f0 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a  argument */.};..
ab300 2f 2a 0a 2a 2a 20 54 68 65 20 22 63 6f 6e 74 65  /*.** The "conte
ab310 78 74 22 20 61 72 67 75 6d 65 6e 74 20 66 6f 72  xt" argument for
ab320 20 61 20 69 6e 73 74 61 6c 6c 61 62 6c 65 20 66   a installable f
ab330 75 6e 63 74 69 6f 6e 2e 20 20 41 20 70 6f 69 6e  unction.  A poin
ab340 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73  ter to an.** ins
ab350 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
ab360 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20 66  ructure is the f
ab370 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
ab380 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 75 73   the routines us
ab390 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  ed.** implement 
ab3a0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
ab3b0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69  s..**.** There i
ab3c0 73 20 61 20 74 79 70 65 64 65 66 20 66 6f 72 20  s a typedef for 
ab3d0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
ab3e0 6e 20 73 71 6c 69 74 65 2e 68 2e 20 20 53 6f 20  n sqlite.h.  So 
ab3f0 61 6c 6c 20 72 6f 75 74 69 6e 65 73 2c 0a 2a 2a  all routines,.**
ab400 20 65 76 65 6e 20 74 68 65 20 70 75 62 6c 69 63   even the public
ab410 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 53 51   interface to SQ
ab420 4c 69 74 65 2c 20 63 61 6e 20 75 73 65 20 61 20  Lite, can use a 
ab430 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20  pointer to this 
ab440 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 42 75  structure..** Bu
ab450 74 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 74  t this file is t
ab460 68 65 20 6f 6e 6c 79 20 70 6c 61 63 65 20 77 68  he only place wh
ab470 65 72 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ere the internal
ab480 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 69 73   details of this
ab490 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 61 72  .** structure ar
ab4a0 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54  e known..**.** T
ab4b0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
ab4c0 20 64 65 66 69 6e 65 64 20 69 6e 73 69 64 65 20   defined inside 
ab4d0 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 62 65 63  of vdbeInt.h bec
ab4e0 61 75 73 65 20 69 74 20 75 73 65 73 20 73 75 62  ause it uses sub
ab4f0 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 28 4d  structures.** (M
ab500 65 6d 29 20 77 68 69 63 68 20 61 72 65 20 6f 6e  em) which are on
ab510 6c 79 20 64 65 66 69 6e 65 64 20 74 68 65 72 65  ly defined there
ab520 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
ab530 74 65 33 5f 63 6f 6e 74 65 78 74 20 7b 0a 20 20  te3_context {.  
ab540 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b 20  FuncDef *pFunc; 
ab550 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
ab560 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 69 6e 66   to function inf
ab570 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d 55 53 54 20  ormation.  MUST 
ab580 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 56 64  BE FIRST */.  Vd
ab590 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e  beFunc *pVdbeFun
ab5a0 63 3b 20 20 2f 2a 20 41 75 78 69 6c 61 72 79 20  c;  /* Auxilary 
ab5b0 64 61 74 61 2c 20 69 66 20 63 72 65 61 74 65 64  data, if created
ab5c0 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73 3b 20 20 20  . */.  Mem s;   
ab5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ab5e0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
ab5f0 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20   is stored here 
ab600 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20  */.  Mem *pMem; 
ab610 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65             /* Me
ab620 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 74  mory cell used t
ab630 6f 20 73 74 6f 72 65 20 61 67 67 72 65 67 61 74  o store aggregat
ab640 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69  e context */.  i
ab650 6e 74 20 69 73 45 72 72 6f 72 3b 20 20 20 20 20  nt isError;     
ab660 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f       /* Error co
ab670 64 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  de returned by t
ab680 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a  he function. */.
ab690 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
ab6a0 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61  ;       /* Colla
ab6b0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f  ting sequence */
ab6c0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 53 65 74  .};../*.** A Set
ab6d0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
ab6e0 65 64 20 66 6f 72 20 71 75 69 63 6b 20 74 65 73  ed for quick tes
ab6f0 74 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20 61  ting to see if a
ab700 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 61 72   value.** is par
ab710 74 20 6f 66 20 61 20 73 6d 61 6c 6c 20 73 65 74  t of a small set
ab720 2e 20 20 53 65 74 73 20 61 72 65 20 75 73 65 64  .  Sets are used
ab730 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f   to implement co
ab740 64 65 20 6c 69 6b 65 0a 2a 2a 20 74 68 69 73 3a  de like.** this:
ab750 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78  .**            x
ab760 2e 79 20 49 4e 20 28 27 68 69 27 2c 27 68 6f 6f  .y IN ('hi','hoo
ab770 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a 74 79 70 65  ','hum').*/.type
ab780 64 65 66 20 73 74 72 75 63 74 20 53 65 74 20 53  def struct Set S
ab790 65 74 3b 0a 73 74 72 75 63 74 20 53 65 74 20 7b  et;.struct Set {
ab7a0 0a 20 20 48 61 73 68 20 68 61 73 68 3b 20 20 20  .  Hash hash;   
ab7b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
ab7c0 65 74 20 69 73 20 6a 75 73 74 20 61 20 68 61 73  et is just a has
ab7d0 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73  h table */.  Has
ab7e0 68 45 6c 65 6d 20 2a 70 72 65 76 3b 20 20 20 20  hElem *prev;    
ab7f0 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 6c      /* Previousl
ab800 79 20 61 63 63 65 73 73 65 64 20 68 61 73 68 20  y accessed hash 
ab810 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  elemen */.};../*
ab820 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
ab830 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  of the virtual m
ab840 61 63 68 69 6e 65 2e 20 20 54 68 69 73 20 73 74  achine.  This st
ab850 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
ab860 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a   the complete.**
ab870 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 76 69   state of the vi
ab880 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a  rtual machine..*
ab890 2a 0a 2a 2a 20 54 68 65 20 22 73 71 6c 69 74 65  *.** The "sqlite
ab8a0 33 5f 73 74 6d 74 22 20 73 74 72 75 63 74 75 72  3_stmt" structur
ab8b0 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69  e pointer that i
ab8c0 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  s returned by sq
ab8d0 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a  lite3_compile().
ab8e0 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70  ** is really a p
ab8f0 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
ab900 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
ab910 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
ab920 68 65 20 56 64 62 65 2e 69 6e 56 74 61 62 4d 65  he Vdbe.inVtabMe
ab930 74 68 6f 64 20 76 61 72 69 61 62 6c 65 20 69 73  thod variable is
ab940 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f   set to non-zero
ab950 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f   for the duratio
ab960 6e 20 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74  n of.** any virt
ab970 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64  ual table method
ab980 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64   invocations mad
ab990 65 20 62 79 20 74 68 65 20 76 64 62 65 20 70 72  e by the vdbe pr
ab9a0 6f 67 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20  ogram. It is.** 
ab9b0 73 65 74 20 74 6f 20 32 20 66 6f 72 20 78 44 65  set to 2 for xDe
ab9c0 73 74 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c  stroy method cal
ab9d0 6c 73 20 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c  ls and 1 for all
ab9e0 20 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2e 20   other methods. 
ab9f0 54 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65  This.** variable
aba00 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 77 6f   is used for two
aba10 20 70 75 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c   purposes: to al
aba20 6c 6f 77 20 78 44 65 73 74 72 6f 79 20 6d 65 74  low xDestroy met
aba30 68 6f 64 73 20 74 6f 20 65 78 65 63 75 74 65 0a  hods to execute.
aba40 2a 2a 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20  ** "DROP TABLE" 
aba50 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 74  statements and t
aba60 6f 20 70 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e  o prevent some n
aba70 61 73 74 79 20 73 69 64 65 20 65 66 66 65 63 74  asty side effect
aba80 73 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66  s of.** malloc f
aba90 61 69 6c 75 72 65 20 77 68 65 6e 20 53 51 4c 69  ailure when SQLi
abaa0 74 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 72 65  te is invoked re
abab0 63 75 72 73 69 76 65 6c 79 20 62 79 20 61 20 76  cursively by a v
abac0 69 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a  irtual table .**
abad0 20 6d 65 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e   method function
abae0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65  ..*/.struct Vdbe
abaf0 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
abb00 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
abb10 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
abb20 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 6f 77 6e  nection that own
abb30 73 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74  s this statement
abb40 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65   */.  Vdbe *pPre
abb50 76 2c 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a  v,*pNext;     /*
abb60 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   Linked list of 
abb70 56 44 42 45 73 20 77 69 74 68 20 74 68 65 20 73  VDBEs with the s
abb80 61 6d 65 20 56 64 62 65 2e 64 62 20 2a 2f 0a 20  ame Vdbe.db */. 
abb90 20 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20   int nOp;       
abba0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
abbb0 65 72 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f  er of instructio
abbc0 6e 73 20 69 6e 20 74 68 65 20 70 72 6f 67 72 61  ns in the progra
abbd0 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 41 6c  m */.  int nOpAl
abbe0 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 2f  loc;           /
abbf0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74  * Number of slot
abc00 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
abc10 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61  aOp[] */.  Op *a
abc20 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Op;             
abc30 20 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 68     /* Space to h
abc40 6f 6c 64 20 74 68 65 20 76 69 72 74 75 61 6c 20  old the virtual 
abc50 6d 61 63 68 69 6e 65 27 73 20 70 72 6f 67 72 61  machine's progra
abc60 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65  m */.  int nLabe
abc70 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
abc80 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 61 62 65  * Number of labe
abc90 6c 73 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74  ls used */.  int
abca0 20 6e 4c 61 62 65 6c 41 6c 6c 6f 63 3b 20 20 20   nLabelAlloc;   
abcb0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
abcc0 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65  f slots allocate
abcd0 64 20 69 6e 20 61 4c 61 62 65 6c 5b 5d 20 2a 2f  d in aLabel[] */
abce0 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 3b 20  .  int *aLabel; 
abcf0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70             /* Sp
abd00 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ace to hold the 
abd10 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20  labels */.  Mem 
abd20 2a 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20  **apArg;        
abd30 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73      /* Arguments
abd40 20 74 6f 20 63 75 72 72 65 6e 74 6c 79 20 65 78   to currently ex
abd50 65 63 75 74 69 6e 67 20 75 73 65 72 20 66 75 6e  ecuting user fun
abd60 63 74 69 6f 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a  ction */.  Mem *
abd70 61 43 6f 6c 4e 61 6d 65 3b 20 20 20 20 20 20 20  aColName;       
abd80 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d     /* Column nam
abd90 65 73 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  es to return */.
abda0 20 20 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53 65    Mem *pResultSe
abdb0 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  t;        /* Poi
abdc0 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79  nter to an array
abdd0 20 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20   of results */. 
abde0 20 75 31 36 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b   u16 nResColumn;
abdf0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
abe00 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
abe10 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20   one row of the 
abe20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
abe30 75 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20  u16 nCursor;    
abe40 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
abe50 72 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70  r of slots in ap
abe60 43 73 72 5b 5d 20 2a 2f 0a 20 20 56 64 62 65 43  Csr[] */.  VdbeC
abe70 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20 20  ursor **apCsr;  
abe80 20 20 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d 65 6e     /* One elemen
abe90 74 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 20  t of this array 
abea0 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 63 75  for each open cu
abeb0 72 73 6f 72 20 2a 2f 0a 20 20 75 38 20 65 72 72  rsor */.  u8 err
abec0 6f 72 41 63 74 69 6f 6e 3b 20 20 20 20 20 20 20  orAction;       
abed0 20 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61 63    /* Recovery ac
abee0 74 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63 61  tion to do in ca
abef0 73 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 2a  se of an error *
abf00 2f 0a 20 20 75 38 20 6f 6b 56 61 72 3b 20 20 20  /.  u8 okVar;   
abf10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
abf20 72 75 65 20 69 66 20 61 7a 56 61 72 5b 5d 20 68  rue if azVar[] h
abf30 61 73 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  as been initiali
abf40 7a 65 64 20 2a 2f 0a 20 20 79 6e 56 61 72 20 6e  zed */.  ynVar n
abf50 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Var;            
abf60 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
abf70 74 72 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20  tries in aVar[] 
abf80 2a 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20  */.  Mem *aVar; 
abf90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
abfa0 56 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 4f  Values for the O
abfb0 50 5f 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64  P_Variable opcod
abfc0 65 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  e. */.  char **a
abfd0 7a 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20  zVar;           
abfe0 2f 2a 20 4e 61 6d 65 20 6f 66 20 76 61 72 69 61  /* Name of varia
abff0 62 6c 65 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61  bles */.  u32 ma
ac000 67 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  gic;            
ac010 20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65    /* Magic numbe
ac020 72 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65  r for sanity che
ac030 63 6b 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e  cking */.  int n
ac040 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
ac050 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
ac060 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73  memory locations
ac070 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63   currently alloc
ac080 61 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61  ated */.  Mem *a
ac090 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
ac0a0 20 20 2f 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20    /* The memory 
ac0b0 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75  locations */.  u
ac0c0 33 32 20 63 61 63 68 65 43 74 72 3b 20 20 20 20  32 cacheCtr;    
ac0d0 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65 43 75         /* VdbeCu
ac0e0 72 73 6f 72 20 72 6f 77 20 63 61 63 68 65 20 67  rsor row cache g
ac0f0 65 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e 74 65  eneration counte
ac100 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20  r */.  int pc;  
ac110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ac120 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f  * The program co
ac130 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72  unter */.  int r
ac140 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
ac150 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72     /* Value to r
ac160 65 74 75 72 6e 20 2a 2f 0a 20 20 63 68 61 72 20  eturn */.  char 
ac170 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20  *zErrMsg;       
ac180 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73     /* Error mess
ac190 61 67 65 20 77 72 69 74 74 65 6e 20 68 65 72 65  age written here
ac1a0 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e   */.  u8 explain
ac1b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
ac1c0 20 54 72 75 65 20 69 66 20 45 58 50 4c 41 49 4e   True if EXPLAIN
ac1d0 20 70 72 65 73 65 6e 74 20 6f 6e 20 53 51 4c 20   present on SQL 
ac1e0 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20 75 38 20  command */.  u8 
ac1f0 63 68 61 6e 67 65 43 6e 74 4f 6e 3b 20 20 20 20  changeCntOn;    
ac200 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
ac210 75 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67  update the chang
ac220 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75  e-counter */.  u
ac230 38 20 65 78 70 69 72 65 64 3b 20 20 20 20 20 20  8 expired;      
ac240 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
ac250 66 20 74 68 65 20 56 4d 20 6e 65 65 64 73 20 74  f the VM needs t
ac260 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20  o be recompiled 
ac270 2a 2f 0a 20 20 75 38 20 6d 69 6e 57 72 69 74 65  */.  u8 minWrite
ac280 46 69 6c 65 46 6f 72 6d 61 74 3b 20 20 2f 2a 20  FileFormat;  /* 
ac290 4d 69 6e 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72  Minimum file for
ac2a0 6d 61 74 20 66 6f 72 20 77 72 69 74 61 62 6c 65  mat for writable
ac2b0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
ac2c0 2a 2f 0a 20 20 75 38 20 69 6e 56 74 61 62 4d 65  */.  u8 inVtabMe
ac2d0 74 68 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  thod;        /* 
ac2e0 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f  See comments abo
ac2f0 76 65 20 2a 2f 0a 20 20 75 38 20 75 73 65 73 53  ve */.  u8 usesS
ac300 74 6d 74 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20  tmtJournal;     
ac310 2f 2a 20 54 72 75 65 20 69 66 20 75 73 65 73 20  /* True if uses 
ac320 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72  a statement jour
ac330 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 72 65 61 64  nal */.  u8 read
ac340 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20  Only;           
ac350 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 72 65 61   /* True for rea
ac360 64 2d 6f 6e 6c 79 20 73 74 61 74 65 6d 65 6e 74  d-only statement
ac370 73 20 2a 2f 0a 20 20 75 38 20 69 73 50 72 65 70  s */.  u8 isPrep
ac380 61 72 65 56 32 3b 20 20 20 20 20 20 20 20 20 2f  areV2;         /
ac390 2a 20 54 72 75 65 20 69 66 20 70 72 65 70 61 72  * True if prepar
ac3a0 65 64 20 77 69 74 68 20 70 72 65 70 61 72 65 5f  ed with prepare_
ac3b0 76 32 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  v2() */.  int nC
ac3c0 68 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20  hange;          
ac3d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64    /* Number of d
ac3e0 62 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 73  b changes made s
ac3f0 69 6e 63 65 20 6c 61 73 74 20 72 65 73 65 74 20  ince last reset 
ac400 2a 2f 0a 20 20 69 6e 74 20 62 74 72 65 65 4d 61  */.  int btreeMa
ac410 73 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  sk;          /* 
ac420 42 69 74 6d 61 73 6b 20 6f 66 20 64 62 2d 3e 61  Bitmask of db->a
ac430 44 62 5b 5d 20 65 6e 74 72 69 65 73 20 72 65 66  Db[] entries ref
ac440 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 69 36 34  erenced */.  i64
ac450 20 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20 20   startTime;     
ac460 20 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 65       /* Time whe
ac470 6e 20 71 75 65 72 79 20 73 74 61 72 74 65 64 20  n query started 
ac480 2d 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66 69  - used for profi
ac490 6c 69 6e 67 20 2a 2f 0a 20 20 42 74 72 65 65 4d  ling */.  BtreeM
ac4a0 75 74 65 78 41 72 72 61 79 20 61 4d 75 74 65 78  utexArray aMutex
ac4b0 3b 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66  ; /* An array of
ac4c0 20 42 74 72 65 65 20 75 73 65 64 20 68 65 72 65   Btree used here
ac4d0 20 61 6e 64 20 6e 65 65 64 69 6e 67 20 6c 6f 63   and needing loc
ac4e0 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 61 43 6f 75  ks */.  int aCou
ac4f0 6e 74 65 72 5b 32 5d 3b 20 20 20 20 20 20 20 20  nter[2];        
ac500 2f 2a 20 43 6f 75 6e 74 65 72 73 20 75 73 65 64  /* Counters used
ac510 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 6d 74   by sqlite3_stmt
ac520 5f 73 74 61 74 75 73 28 29 20 2a 2f 0a 20 20 63  _status() */.  c
ac530 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20  har *zSql;      
ac540 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f         /* Text o
ac550 66 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  f the SQL statem
ac560 65 6e 74 20 74 68 61 74 20 67 65 6e 65 72 61 74  ent that generat
ac570 65 64 20 74 68 69 73 20 2a 2f 0a 20 20 76 6f 69  ed this */.  voi
ac580 64 20 2a 70 46 72 65 65 3b 20 20 20 20 20 20 20  d *pFree;       
ac590 20 20 20 20 20 2f 2a 20 46 72 65 65 20 74 68 69       /* Free thi
ac5a0 73 20 77 68 65 6e 20 64 65 6c 65 74 69 6e 67 20  s when deleting 
ac5b0 74 68 65 20 76 64 62 65 20 2a 2f 0a 20 20 69 36  the vdbe */.  i6
ac5c0 34 20 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3b  4 nFkConstraint;
ac5d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
ac5e0 6f 66 20 69 6d 6d 2e 20 46 4b 20 63 6f 6e 73 74  of imm. FK const
ac5f0 72 61 69 6e 74 73 20 74 68 69 73 20 56 4d 20 2a  raints this VM *
ac600 2f 0a 20 20 69 36 34 20 6e 53 74 6d 74 44 65 66  /.  i64 nStmtDef
ac610 43 6f 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 4e  Cons;       /* N
ac620 75 6d 62 65 72 20 6f 66 20 64 65 66 2e 20 63 6f  umber of def. co
ac630 6e 73 74 72 61 69 6e 74 73 20 77 68 65 6e 20 73  nstraints when s
ac640 74 6d 74 20 73 74 61 72 74 65 64 20 2a 2f 0a 20  tmt started */. 
ac650 20 69 6e 74 20 69 53 74 61 74 65 6d 65 6e 74 3b   int iStatement;
ac660 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
ac670 65 6d 65 6e 74 20 6e 75 6d 62 65 72 20 28 6f 72  ement number (or
ac680 20 30 20 69 66 20 68 61 73 20 6e 6f 74 20 6f 70   0 if has not op
ac690 65 6e 65 64 20 73 74 6d 74 29 20 2a 2f 0a 23 69  ened stmt) */.#i
ac6a0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
ac6b0 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 65 3b  G.  FILE *trace;
ac6c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
ac6d0 72 69 74 65 20 61 6e 20 65 78 65 63 75 74 69 6f  rite an executio
ac6e0 6e 20 74 72 61 63 65 20 68 65 72 65 2c 20 69 66  n trace here, if
ac6f0 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e   not NULL */.#en
ac700 64 69 66 0a 20 20 56 64 62 65 46 72 61 6d 65 20  dif.  VdbeFrame 
ac710 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a  *pFrame;      /*
ac720 20 50 61 72 65 6e 74 20 66 72 61 6d 65 20 2a 2f   Parent frame */
ac730 0a 20 20 69 6e 74 20 6e 46 72 61 6d 65 3b 20 20  .  int nFrame;  
ac740 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
ac750 6d 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20 69  mber of frames i
ac760 6e 20 70 46 72 61 6d 65 20 6c 69 73 74 20 2a 2f  n pFrame list */
ac770 0a 20 20 75 33 32 20 65 78 70 6d 61 73 6b 3b 20  .  u32 expmask; 
ac780 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69             /* Bi
ac790 6e 64 69 6e 67 20 74 6f 20 74 68 65 73 65 20 76  nding to these v
ac7a0 61 72 73 20 69 6e 76 61 6c 69 64 61 74 65 73 20  ars invalidates 
ac7b0 56 4d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  VM */.};../*.** 
ac7c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72  The following ar
ac7d0 65 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  e allowed values
ac7e0 20 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a   for Vdbe.magic.
ac7f0 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f  */.#define VDBE_
ac800 4d 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30  MAGIC_INIT     0
ac810 78 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20  x26bceaa5    /* 
ac820 42 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20  Building a VDBE 
ac830 70 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69  program */.#defi
ac840 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  ne VDBE_MAGIC_RU
ac850 4e 20 20 20 20 20 20 30 78 62 64 66 32 30 64 61  N      0xbdf20da
ac860 33 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20  3    /* VDBE is 
ac870 72 65 61 64 79 20 74 6f 20 65 78 65 63 75 74 65  ready to execute
ac880 20 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45   */.#define VDBE
ac890 5f 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20  _MAGIC_HALT     
ac8a0 30 78 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a  0x519c2973    /*
ac8b0 20 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65   VDBE has comple
ac8c0 74 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f  ted execution */
ac8d0 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41  .#define VDBE_MA
ac8e0 47 49 43 5f 44 45 41 44 20 20 20 20 20 30 78 62  GIC_DEAD     0xb
ac8f0 36 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68  606c3c8    /* Th
ac900 65 20 56 44 42 45 20 68 61 73 20 62 65 65 6e 20  e VDBE has been 
ac910 64 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a  deallocated */..
ac920 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70  /*.** Function p
ac930 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c  rototypes.*/.SQL
ac940 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
ac950 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65   sqlite3VdbeFree
ac960 43 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 56  Cursor(Vdbe *, V
ac970 64 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69  dbeCursor*);.voi
ac980 64 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53  d sqliteVdbePopS
ac990 74 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b  tack(Vdbe*,int);
ac9a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac9b0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
ac9c0 75 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65  ursorMoveto(Vdbe
ac9d0 43 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65  Cursor*);.#if de
ac9e0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
ac9f0 55 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56  UG) || defined(V
aca00 44 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c  DBE_PROFILE).SQL
aca10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
aca20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
aca30 74 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20  tOp(FILE*, int, 
aca40 4f 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c  Op*);.#endif.SQL
aca50 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
aca60 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
aca70 6c 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53  lTypeLen(u32);.S
aca80 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33  QLITE_PRIVATE u3
aca90 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  2 sqlite3VdbeSer
acaa0 69 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e  ialType(Mem*, in
acab0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
acac0 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64  TE u32 sqlite3Vd
acad0 62 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69  beSerialPut(unsi
acae0 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c  gned char*, int,
acaf0 20 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c   Mem*, int);.SQL
acb00 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
acb10 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
acb20 6c 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67  lGet(const unsig
acb30 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20  ned char*, u32, 
acb40 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
acb50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
acb60 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44  e3VdbeDeleteAuxD
acb70 61 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69  ata(VdbeFunc*, i
acb80 6e 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65  nt);..int sqlite
acb90 32 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65  2BtreeKeyCompare
acba0 28 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e  (BtCursor *, con
acbb0 73 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20  st void *, int, 
acbc0 69 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c  int, int *);.SQL
acbd0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
acbe0 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65  sqlite3VdbeIdxKe
acbf0 79 43 6f 6d 70 61 72 65 28 56 64 62 65 43 75 72  yCompare(VdbeCur
acc00 73 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63  sor*,UnpackedRec
acc10 6f 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49  ord*,int*);.SQLI
acc20 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
acc30 71 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77  qlite3VdbeIdxRow
acc40 69 64 28 73 71 6c 69 74 65 33 2a 2c 20 42 74 43  id(sqlite3*, BtC
acc50 75 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 3b  ursor *, i64 *);
acc60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
acc70 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f  int sqlite3MemCo
acc80 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d 2a  mpare(const Mem*
acc90 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f  , const Mem*, co
acca0 6e 73 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a 53  nst CollSeq*);.S
accb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
accc0 74 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65  t sqlite3VdbeExe
accd0 63 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45  c(Vdbe*);.SQLITE
acce0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
accf0 69 74 65 33 56 64 62 65 4c 69 73 74 28 56 64 62  ite3VdbeList(Vdb
acd00 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
acd10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
acd20 64 62 65 48 61 6c 74 28 56 64 62 65 2a 29 3b 0a  dbeHalt(Vdbe*);.
acd30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
acd40 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  nt sqlite3VdbeCh
acd50 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d  angeEncoding(Mem
acd60 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45   *, int);.SQLITE
acd70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
acd80 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69  ite3VdbeMemTooBi
acd90 67 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  g(Mem*);.SQLITE_
acda0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
acdb0 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d  te3VdbeMemCopy(M
acdc0 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 29  em*, const Mem*)
acdd0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
acde0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
acdf0 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
ace00 4d 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a  Mem*, const Mem*
ace10 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
ace20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
ace30 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d  te3VdbeMemMove(M
ace40 65 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49  em*, Mem*);.SQLI
ace50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ace60 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c  qlite3VdbeMemNul
ace70 54 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29 3b  Terminate(Mem*);
ace80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ace90 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
acea0 65 6d 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20 63  emSetStr(Mem*, c
aceb0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c  onst char*, int,
acec0 20 75 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69   u8, void(*)(voi
aced0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  d*));.SQLITE_PRI
acee0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
acef0 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34  3VdbeMemSetInt64
acf00 28 4d 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51 4c  (Mem*, i64);.SQL
acf10 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
acf20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
acf30 65 74 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20 64  etDouble(Mem*, d
acf40 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50  ouble);.SQLITE_P
acf50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
acf60 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c  te3VdbeMemSetNul
acf70 6c 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  l(Mem*);.SQLITE_
acf80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
acf90 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65  ite3VdbeMemSetZe
acfa0 72 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74 29  roBlob(Mem*,int)
acfb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
acfc0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
acfd0 65 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d 65  eMemSetRowSet(Me
acfe0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
acff0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ad000 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61  dbeMemMakeWritea
ad010 62 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  ble(Mem*);.SQLIT
ad020 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ad030 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69  lite3VdbeMemStri
ad040 6e 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e 74 29  ngify(Mem*, int)
ad050 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ad060 20 69 36 34 20 73 71 6c 69 74 65 33 56 64 62 65   i64 sqlite3Vdbe
ad070 49 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a  IntValue(Mem*);.
ad080 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ad090 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
ad0a0 6d 49 6e 74 65 67 65 72 69 66 79 28 4d 65 6d 2a  mIntegerify(Mem*
ad0b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ad0c0 45 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33  E double sqlite3
ad0d0 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 4d 65  VdbeRealValue(Me
ad0e0 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  m*);.SQLITE_PRIV
ad0f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
ad100 56 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e  VdbeIntegerAffin
ad110 69 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  ity(Mem*);.SQLIT
ad120 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ad130 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c  lite3VdbeMemReal
ad140 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  ify(Mem*);.SQLIT
ad150 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ad160 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65  lite3VdbeMemNume
ad170 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49  rify(Mem*);.SQLI
ad180 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ad190 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
ad1a0 6d 42 74 72 65 65 28 42 74 43 75 72 73 6f 72 2a  mBtree(BtCursor*
ad1b0 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65 6d  ,int,int,int,Mem
ad1c0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
ad1d0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
ad1e0 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65  dbeMemRelease(Me
ad1f0 6d 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52  m *p);.SQLITE_PR
ad200 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ad210 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
ad220 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29  External(Mem *p)
ad230 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ad240 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
ad250 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d 2a  MemFinalize(Mem*
ad260 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c  , FuncDef*);.SQL
ad270 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
ad280 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f  t char *sqlite3O
ad290 70 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b 0a  pcodeName(int);.
ad2a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ad2b0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
ad2c0 6d 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c  mGrow(Mem *pMem,
ad2d0 20 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73   int n, int pres
ad2e0 65 72 76 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  erve);.SQLITE_PR
ad2f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
ad300 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74 65 6d  3VdbeCloseStatem
ad310 65 6e 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29  ent(Vdbe *, int)
ad320 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
ad330 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
ad340 65 46 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62  eFrameDelete(Vdb
ad350 65 46 72 61 6d 65 2a 29 3b 0a 53 51 4c 49 54 45  eFrame*);.SQLITE
ad360 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
ad370 69 74 65 33 56 64 62 65 46 72 61 6d 65 52 65 73  ite3VdbeFrameRes
ad380 74 6f 72 65 28 56 64 62 65 46 72 61 6d 65 20 2a  tore(VdbeFrame *
ad390 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ad3a0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
ad3b0 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 4d  beMemStoreType(M
ad3c0 65 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66 6e  em *pMem);..#ifn
ad3d0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
ad3e0 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 53 51 4c 49  FOREIGN_KEY.SQLI
ad3f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ad400 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46  qlite3VdbeCheckF
ad410 6b 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a  k(Vdbe *, int);.
ad420 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
ad430 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46  qlite3VdbeCheckF
ad440 6b 28 70 2c 69 29 20 30 0a 23 65 6e 64 69 66 0a  k(p,i) 0.#endif.
ad450 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
ad460 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
ad470 45 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  E.SQLITE_PRIVATE
ad480 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
ad490 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72  eMutexArrayEnter
ad4a0 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 6c 73 65  (Vdbe *p);.#else
ad4b0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
ad4c0 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 45  3VdbeMutexArrayE
ad4d0 6e 74 65 72 28 70 29 0a 23 65 6e 64 69 66 0a 0a  nter(p).#endif..
ad4e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ad4f0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
ad500 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 2a 2c  mTranslate(Mem*,
ad510 20 75 38 29 3b 0a 23 69 66 64 65 66 20 53 51 4c   u8);.#ifdef SQL
ad520 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45  ITE_DEBUG.SQLITE
ad530 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
ad540 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74  sqlite3VdbePrint
ad550 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  Sql(Vdbe*);.SQLI
ad560 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
ad570 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  d sqlite3VdbeMem
ad580 50 72 65 74 74 79 50 72 69 6e 74 28 4d 65 6d 20  PrettyPrint(Mem 
ad590 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a 42 75  *pMem, char *zBu
ad5a0 66 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  f);.#endif.SQLIT
ad5b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ad5c0 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61 6e 64  lite3VdbeMemHand
ad5d0 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29  leBom(Mem *pMem)
ad5e0 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
ad5f0 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
ad600 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
ad610 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
ad620 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65  MemExpandBlob(Me
ad630 6d 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64  m *);.#else.  #d
ad640 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62  efine sqlite3Vdb
ad650 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 78  eMemExpandBlob(x
ad660 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64  ) SQLITE_OK.#end
ad670 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  if..#endif /* !d
ad680 65 66 69 6e 65 64 28 5f 56 44 42 45 49 4e 54 5f  efined(_VDBEINT_
ad690 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  H_) */../*******
ad6a0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76  ******* End of v
ad6b0 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  dbeInt.h *******
ad6c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad6e0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
ad6f0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
ad700 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
ad710 20 6f 66 66 20 69 6e 20 75 74 66 2e 63 20 2a 2a   off in utf.c **
ad720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad730 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66  ******/..#ifndef
ad740 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
ad750 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  TION./*.** The f
ad760 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61 6e  ollowing constan
ad770 74 20 76 61 6c 75 65 20 69 73 20 75 73 65 64 20  t value is used 
ad780 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f 42 49  by the SQLITE_BI
ad790 47 45 4e 44 49 41 4e 20 61 6e 64 0a 2a 2a 20 53  GENDIAN and.** S
ad7a0 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49  QLITE_LITTLEENDI
ad7b0 41 4e 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 53 51  AN macros..*/.SQ
ad7c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
ad7d0 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33 6f 6e  st int sqlite3on
ad7e0 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 2f 2a  e = 1;.#endif /*
ad7f0 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
ad800 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  TION */../*.** T
ad810 68 69 73 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65  his lookup table
ad820 20 69 73 20 75 73 65 64 20 74 6f 20 68 65 6c 70   is used to help
ad830 20 64 65 63 6f 64 65 20 74 68 65 20 66 69 72 73   decode the firs
ad840 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20 61 20 6d  t byte of.** a m
ad850 75 6c 74 69 2d 62 79 74 65 20 55 54 46 38 20 63  ulti-byte UTF8 c
ad860 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 73 74 61  haracter..*/.sta
ad870 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
ad880 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55  ed char sqlite3U
ad890 74 66 38 54 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a  tf8Trans1[] = {.
ad8a0 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78    0x00, 0x01, 0x
ad8b0 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20  02, 0x03, 0x04, 
ad8c0 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37  0x05, 0x06, 0x07
ad8d0 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20  ,.  0x08, 0x09, 
ad8e0 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63  0x0a, 0x0b, 0x0c
ad8f0 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78  , 0x0d, 0x0e, 0x
ad900 30 66 2c 0a 20 20 30 78 31 30 2c 20 30 78 31 31  0f,.  0x10, 0x11
ad910 2c 20 30 78 31 32 2c 20 30 78 31 33 2c 20 30 78  , 0x12, 0x13, 0x
ad920 31 34 2c 20 30 78 31 35 2c 20 30 78 31 36 2c 20  14, 0x15, 0x16, 
ad930 30 78 31 37 2c 0a 20 20 30 78 31 38 2c 20 30 78  0x17,.  0x18, 0x
ad940 31 39 2c 20 30 78 31 61 2c 20 30 78 31 62 2c 20  19, 0x1a, 0x1b, 
ad950 30 78 31 63 2c 20 30 78 31 64 2c 20 30 78 31 65  0x1c, 0x1d, 0x1e
ad960 2c 20 30 78 31 66 2c 0a 20 20 30 78 30 30 2c 20  , 0x1f,.  0x00, 
ad970 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33  0x01, 0x02, 0x03
ad980 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78  , 0x04, 0x05, 0x
ad990 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38  06, 0x07,.  0x08
ad9a0 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78  , 0x09, 0x0a, 0x
ad9b0 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20  0b, 0x0c, 0x0d, 
ad9c0 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78  0x0e, 0x0f,.  0x
ad9d0 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20  00, 0x01, 0x02, 
ad9e0 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35  0x03, 0x04, 0x05
ad9f0 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20  , 0x06, 0x07,.  
ada00 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32  0x00, 0x01, 0x02
ada10 2c 20 30 78 30 33 2c 20 30 78 30 30 2c 20 30 78  , 0x03, 0x00, 0x
ada20 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 0a  01, 0x00, 0x00,.
ada30 7d 3b 0a 0a 0a 23 64 65 66 69 6e 65 20 57 52 49  };...#define WRI
ada40 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29  TE_UTF8(zOut, c)
ada50 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
ada60 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ada70 69 66 28 20 63 3c 30 78 30 30 30 38 30 20 29 7b  if( c<0x00080 ){
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 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
adab0 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78 46 46  ++ = (u8)(c&0xFF
adac0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
adad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
adae0 20 20 7d 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 20 20 20                  
adb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb10 20 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20         \.  else 
adb20 69 66 28 20 63 3c 30 78 30 30 38 30 30 20 29 7b  if( c<0x00800 ){
adb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb50 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
adb60 30 78 43 30 20 2b 20 28 75 38 29 28 28 63 3e 3e  0xC0 + (u8)((c>>
adb70 36 29 26 30 78 31 46 29 3b 20 20 20 20 20 20 20  6)&0x1F);       
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 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
adba0 28 75 38 29 28 63 20 26 20 30 78 33 46 29 3b 20  (u8)(c & 0x3F); 
adbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbc0 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20    \.  }         
adbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbf0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65             \.  e
adc00 6c 73 65 20 69 66 28 20 63 3c 30 78 31 30 30 30  lse if( c<0x1000
adc10 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
adc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc30 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
adc40 2b 20 3d 20 30 78 45 30 20 2b 20 28 75 38 29 28  + = 0xE0 + (u8)(
adc50 28 63 3e 3e 31 32 29 26 30 78 30 46 29 3b 20 20  (c>>12)&0x0F);  
adc60 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
adc70 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
adc80 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 36 29 20  0 + (u8)((c>>6) 
adc90 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
adca0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
adcb0 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38  t++ = 0x80 + (u8
adcc0 29 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20  )(c & 0x3F);    
adcd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
adce0 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20  .  }else{       
adcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add10 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
add20 4f 75 74 2b 2b 20 3d 20 30 78 46 30 20 2b 20 28  Out++ = 0xF0 + (
add30 75 38 29 28 28 63 3e 3e 31 38 29 20 26 20 30 78  u8)((c>>18) & 0x
add40 30 37 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  07);            
add50 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
add60 20 30 78 38 30 20 2b 20 28 75 38 29 28 28 63 3e   0x80 + (u8)((c>
add70 3e 31 32 29 20 26 20 30 78 33 46 29 3b 20 20 20  >12) & 0x3F);   
add80 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
add90 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b  *zOut++ = 0x80 +
adda0 20 28 75 38 29 28 28 63 3e 3e 36 29 20 26 20 30   (u8)((c>>6) & 0
addb0 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x3F);           
addc0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
addd0 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63   = 0x80 + (u8)(c
adde0 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20   & 0x3F);       
addf0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ade00 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }               
ade10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade30 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e       \.}..#defin
ade40 65 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28  e WRITE_UTF16LE(
ade50 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20  zOut, c) {      
ade60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ade80 20 20 69 66 28 20 63 3c 3d 30 78 46 46 46 46 20    if( c<=0xFFFF 
ade90 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
adea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adec0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
aded0 2b 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46  + = (u8)(c&0x00F
adee0 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
adef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf00 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
adf10 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 28  *zOut++ = (u8)((
adf20 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20  c>>8)&0x00FF);  
adf30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf50 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20  \.  }else{      
adf60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf90 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
adfa0 74 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e 3e  t++ = (u8)(((c>>
adfb0 31 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 28  10)&0x003F) + ((
adfc0 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29  (c-0x10000)>>10)
adfd0 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20  &0x00C0));  \.  
adfe0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
adff0 28 30 78 30 30 44 38 20 2b 20 28 28 28 63 2d 30  (0x00D8 + (((c-0
ae000 78 31 30 30 30 30 29 3e 3e 31 38 29 26 30 78 30  x10000)>>18)&0x0
ae010 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3));            
ae020 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
ae030 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29  = (u8)(c&0x00FF)
ae040 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ae050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae060 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
ae070 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30  Out++ = (u8)(0x0
ae080 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30 78  0DC + ((c>>8)&0x
ae090 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20 20  03));           
ae0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ae0b0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20    }             
ae0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0f0 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65      \.}..#define
ae100 20 57 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a   WRITE_UTF16BE(z
ae110 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20 20  Out, c) {       
ae120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae130 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ae140 20 69 66 28 20 63 3c 3d 30 78 46 46 46 46 20 29   if( c<=0xFFFF )
ae150 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
ae160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae180 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
ae190 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26 30   = (u8)((c>>8)&0
ae1a0 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20  x00FF);         
ae1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1c0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
ae1d0 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26  zOut++ = (u8)(c&
ae1e0 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20  0x00FF);        
ae1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ae210 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20  .  }else{       
ae220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae230 20 20 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 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
ae260 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44 38  ++ = (u8)(0x00D8
ae270 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29   + (((c-0x10000)
ae280 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20  >>18)&0x03));   
ae290 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ae2a0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
ae2b0 28 28 63 3e 3e 31 30 29 26 30 78 30 30 33 46 29  ((c>>10)&0x003F)
ae2c0 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29   + (((c-0x10000)
ae2d0 3e 3e 31 30 29 26 30 78 30 30 43 30 29 29 3b 20  >>10)&0x00C0)); 
ae2e0 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
ae2f0 20 28 75 38 29 28 30 78 30 30 44 43 20 2b 20 28   (u8)(0x00DC + (
ae300 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20 20  (c>>8)&0x03));  
ae310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae320 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
ae330 75 74 2b 2b 20 3d 20 28 75 38 29 28 63 26 30 78  ut++ = (u8)(c&0x
ae340 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20 20  00FF);          
ae350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae360 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ae370 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20   }              
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 20 20 20 20 20 20 20 20                  
ae3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae3b0 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20     \.}..#define 
ae3c0 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e  READ_UTF16LE(zIn
ae3d0 2c 20 54 45 52 4d 2c 20 63 29 7b 20 20 20 20 20  , TERM, c){     
ae3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae3f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ae400 20 20 63 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20    c = (*zIn++); 
ae410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae440 20 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28        \.  c += (
ae450 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20  (*zIn++)<<8);   
ae460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae470 20 20 20 20 20 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 5c 0a                \.
ae490 20 20 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20    if( c>=0xD800 
ae4a0 26 26 20 63 3c 30 78 45 30 30 30 20 26 26 20 54  && c<0xE000 && T
ae4b0 45 52 4d 20 29 7b 20 20 20 20 20 20 20 20 20 20  ERM ){          
ae4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae4d0 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20        \.    int 
ae4e0 63 32 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20  c2 = (*zIn++);  
ae4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ae520 20 20 20 20 63 32 20 2b 3d 20 28 28 2a 7a 49 6e      c2 += ((*zIn
ae530 2b 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20  ++)<<8);        
ae540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae560 20 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20        \.    c = 
ae570 28 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28  (c2&0x03FF) + ((
ae580 63 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b  c&0x003F)<<10) +
ae590 20 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78   (((c&0x03C0)+0x
ae5a0 30 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a  0040)<<10);   \.
ae5b0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20    }             
ae5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae5f0 20 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69        \.}..#defi
ae600 6e 65 20 52 45 41 44 5f 55 54 46 31 36 42 45 28  ne READ_UTF16BE(
ae610 7a 49 6e 2c 20 54 45 52 4d 2c 20 63 29 7b 20 20  zIn, TERM, c){  
ae620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae640 20 5c 0a 20 20 63 20 3d 20 28 28 2a 7a 49 6e 2b   \.  c = ((*zIn+
ae650 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20  +)<<8);         
ae660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae680 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 2b           \.  c +
ae690 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20  = (*zIn++);     
ae6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae6d0 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 44 38   \.  if( c>=0xD8
ae6e0 30 30 20 26 26 20 63 3c 30 78 45 30 30 30 20 26  00 && c<0xE000 &
ae6f0 26 20 54 45 52 4d 20 29 7b 20 20 20 20 20 20 20  & TERM ){       
ae700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae710 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69           \.    i
ae720 6e 74 20 63 32 20 3d 20 28 28 2a 7a 49 6e 2b 2b  nt c2 = ((*zIn++
ae730 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20 20  )<<8);          
ae740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae760 20 5c 0a 20 20 20 20 63 32 20 2b 3d 20 28 2a 7a   \.    c2 += (*z
ae770 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20  In++);          
ae780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae7a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63           \.    c
ae7b0 20 3d 20 28 63 32 26 30 78 30 33 46 46 29 20 2b   = (c2&0x03FF) +
ae7c0 20 28 28 63 26 30 78 30 30 33 46 29 3c 3c 31 30   ((c&0x003F)<<10
ae7d0 29 20 2b 20 28 28 28 63 26 30 78 30 33 43 30 29  ) + (((c&0x03C0)
ae7e0 2b 30 78 30 30 34 30 29 3c 3c 31 30 29 3b 20 20  +0x0040)<<10);  
ae7f0 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
ae800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae830 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a           \.}../*
ae840 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20  .** Translate a 
ae850 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61  single UTF-8 cha
ae860 72 61 63 74 65 72 2e 20 20 52 65 74 75 72 6e 20  racter.  Return 
ae870 74 68 65 20 75 6e 69 63 6f 64 65 20 76 61 6c 75  the unicode valu
ae880 65 2e 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20  e..**.** During 
ae890 74 72 61 6e 73 6c 61 74 69 6f 6e 2c 20 61 73 73  translation, ass
ae8a0 75 6d 65 20 74 68 61 74 20 74 68 65 20 62 79 74  ume that the byt
ae8b0 65 20 74 68 61 74 20 7a 54 65 72 6d 20 70 6f 69  e that zTerm poi
ae8c0 6e 74 73 0a 2a 2a 20 69 73 20 61 20 30 78 30 30  nts.** is a 0x00
ae8d0 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20  ..**.** Write a 
ae8e0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
ae8f0 65 78 74 20 75 6e 72 65 61 64 20 62 79 74 65 20  ext unread byte 
ae900 62 61 63 6b 20 69 6e 74 6f 20 2a 70 7a 4e 65 78  back into *pzNex
ae910 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 4f  t..**.** Notes O
ae920 6e 20 49 6e 76 61 6c 69 64 20 55 54 46 2d 38 3a  n Invalid UTF-8:
ae930 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20  .**.**  *  This 
ae940 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c  routine never al
ae950 6c 6f 77 73 20 61 20 37 2d 62 69 74 20 63 68 61  lows a 7-bit cha
ae960 72 61 63 74 65 72 20 28 30 78 30 30 20 74 68 72  racter (0x00 thr
ae970 6f 75 67 68 20 30 78 37 66 29 20 74 6f 0a 2a 2a  ough 0x7f) to.**
ae980 20 20 20 20 20 62 65 20 65 6e 63 6f 64 65 64 20       be encoded 
ae990 61 73 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20  as a multi-byte 
ae9a0 63 68 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20  character.  Any 
ae9b0 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61  multi-byte chara
ae9c0 63 74 65 72 20 74 68 61 74 0a 2a 2a 20 20 20 20  cter that.**    
ae9d0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63   attempts to enc
ae9e0 6f 64 65 20 61 20 76 61 6c 75 65 20 62 65 74 77  ode a value betw
ae9f0 65 65 6e 20 30 78 30 30 20 61 6e 64 20 30 78 37  een 0x00 and 0x7
aea00 66 20 69 73 20 72 65 6e 64 65 72 65 64 20 61 73  f is rendered as
aea10 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20   0xfffd..**.**  
aea20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  *  This routine 
aea30 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 55  never allows a U
aea40 54 46 31 36 20 73 75 72 72 6f 67 61 74 65 20 76  TF16 surrogate v
aea50 61 6c 75 65 20 74 6f 20 62 65 20 65 6e 63 6f 64  alue to be encod
aea60 65 64 2e 0a 2a 2a 20 20 20 20 20 49 66 20 61 20  ed..**     If a 
aea70 6d 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61  multi-byte chara
aea80 63 74 65 72 20 61 74 74 65 6d 70 74 73 20 74 6f  cter attempts to
aea90 20 65 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20   encode a value 
aeaa0 62 65 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 30  between.**     0
aeab0 78 64 38 30 30 20 61 6e 64 20 30 78 65 30 30 30  xd800 and 0xe000
aeac0 20 74 68 65 6e 20 69 74 20 69 73 20 72 65 6e 64   then it is rend
aead0 65 72 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a  ered as 0xfffd..
aeae0 2a 2a 0a 2a 2a 20 20 2a 20 20 42 79 74 65 73 20  **.**  *  Bytes 
aeaf0 69 6e 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20  in the range of 
aeb00 30 78 38 30 20 74 68 72 6f 75 67 68 20 30 78 62  0x80 through 0xb
aeb10 66 20 77 68 69 63 68 20 6f 63 63 75 72 20 61 73  f which occur as
aeb20 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 20 20   the first.**   
aeb30 20 20 62 79 74 65 20 6f 66 20 61 20 63 68 61 72    byte of a char
aeb40 61 63 74 65 72 20 61 72 65 20 69 6e 74 65 72 70  acter are interp
aeb50 72 65 74 65 64 20 61 73 20 73 69 6e 67 6c 65 2d  reted as single-
aeb60 62 79 74 65 20 63 68 61 72 61 63 74 65 72 73 0a  byte characters.
aeb70 2a 2a 20 20 20 20 20 61 6e 64 20 72 65 6e 64 65  **     and rende
aeb80 72 65 64 20 61 73 20 74 68 65 6d 73 65 6c 76 65  red as themselve
aeb90 73 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68  s even though th
aeba0 65 79 20 61 72 65 20 74 65 63 68 6e 69 63 61 6c  ey are technical
aebb0 6c 79 0a 2a 2a 20 20 20 20 20 69 6e 76 61 6c 69  ly.**     invali
aebc0 64 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a  d characters..**
aebd0 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75  .**  *  This rou
aebe0 74 69 6e 65 20 61 63 63 65 70 74 73 20 61 6e 20  tine accepts an 
aebf0 69 6e 66 69 6e 69 74 65 20 6e 75 6d 62 65 72 20  infinite number 
aec00 6f 66 20 64 69 66 66 65 72 65 6e 74 20 55 54 46  of different UTF
aec10 38 20 65 6e 63 6f 64 69 6e 67 73 0a 2a 2a 20 20  8 encodings.**  
aec20 20 20 20 66 6f 72 20 75 6e 69 63 6f 64 65 20 76     for unicode v
aec30 61 6c 75 65 73 20 30 78 38 30 20 61 6e 64 20 67  alues 0x80 and g
aec40 72 65 61 74 65 72 2e 20 20 49 74 20 64 6f 20 6e  reater.  It do n
aec50 6f 74 20 63 68 61 6e 67 65 20 6f 76 65 72 2d 6c  ot change over-l
aec60 65 6e 67 74 68 0a 2a 2a 20 20 20 20 20 65 6e 63  ength.**     enc
aec70 6f 64 69 6e 67 73 20 74 6f 20 30 78 66 66 66 64  odings to 0xfffd
aec80 20 61 73 20 73 6f 6d 65 20 73 79 73 74 65 6d 73   as some systems
aec90 20 72 65 63 6f 6d 6d 65 6e 64 2e 0a 2a 2f 0a 23   recommend..*/.#
aeca0 64 65 66 69 6e 65 20 52 45 41 44 5f 55 54 46 38  define READ_UTF8
aecb0 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 20  (zIn, zTerm, c) 
aecc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aecd0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20            \.  c 
aece0 3d 20 2a 28 7a 49 6e 2b 2b 29 3b 20 20 20 20 20  = *(zIn++);     
aecf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aed10 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63         \.  if( c
aed20 3e 3d 30 78 63 30 20 29 7b 20 20 20 20 20 20 20  >=0xc0 ){       
aed30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aed40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aed50 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 73 71      \.    c = sq
aed60 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b  lite3Utf8Trans1[
aed70 63 2d 30 78 63 30 5d 3b 20 20 20 20 20 20 20 20  c-0xc0];        
aed80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aed90 20 5c 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49   \.    while( zI
aeda0 6e 21 3d 7a 54 65 72 6d 20 26 26 20 28 2a 7a 49  n!=zTerm && (*zI
aedb0 6e 20 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20  n & 0xc0)==0x80 
aedc0 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a  ){            \.
aedd0 20 20 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 29        c = (c<<6)
aede0 20 2b 20 28 30 78 33 66 20 26 20 2a 28 7a 49 6e   + (0x3f & *(zIn
aedf0 2b 2b 29 29 3b 20 20 20 20 20 20 20 20 20 20 20  ++));           
aee00 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
aee10 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20   }              
aee20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aee30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aee40 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66          \.    if
aee50 28 20 63 3c 30 78 38 30 20 20 20 20 20 20 20 20  ( c<0x80        
aee60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aee70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aee80 20 20 20 20 20 5c 0a 20 20 20 20 20 20 20 20 7c       \.        |
aee90 7c 20 28 63 26 30 78 46 46 46 46 46 38 30 30 29  | (c&0xFFFFF800)
aeea0 3d 3d 30 78 44 38 30 30 20 20 20 20 20 20 20 20  ==0xD800        
aeeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeec0 20 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20 28    \.        || (
aeed0 63 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30  c&0xFFFFFFFE)==0
aeee0 78 46 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78  xFFFE ){  c = 0x
aeef0 46 46 46 44 3b 20 7d 20 20 20 20 20 20 20 20 5c  FFFD; }        \
aef00 0a 20 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  .  }.SQLITE_PRIV
aef10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55  ATE int sqlite3U
aef20 74 66 38 52 65 61 64 28 0a 20 20 63 6f 6e 73 74  tf8Read(.  const
aef30 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
aef40 7a 49 6e 2c 20 20 20 20 20 20 20 2f 2a 20 46 69  zIn,       /* Fi
aef50 72 73 74 20 62 79 74 65 20 6f 66 20 55 54 46 2d  rst byte of UTF-
aef60 38 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20  8 character */. 
aef70 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
aef80 63 68 61 72 20 2a 2a 70 7a 4e 65 78 74 20 20 20  char **pzNext   
aef90 20 2f 2a 20 57 72 69 74 65 20 66 69 72 73 74 20   /* Write first 
aefa0 62 79 74 65 20 70 61 73 74 20 55 54 46 2d 38 20  byte past UTF-8 
aefb0 63 68 61 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  char here */.){.
aefc0 20 20 69 6e 74 20 63 3b 0a 0a 20 20 2f 2a 20 53    int c;..  /* S
aefd0 61 6d 65 20 61 73 20 52 45 41 44 5f 55 54 46 38  ame as READ_UTF8
aefe0 28 29 20 61 62 6f 76 65 20 62 75 74 20 77 69 74  () above but wit
aeff0 68 6f 75 74 20 74 68 65 20 7a 54 65 72 6d 20 70  hout the zTerm p
af000 61 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 46  arameter..  ** F
af010 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  or this routine,
af020 20 77 65 20 61 73 73 75 6d 65 20 74 68 65 20 55   we assume the U
af030 54 46 38 20 73 74 72 69 6e 67 20 69 73 20 61 6c  TF8 string is al
af040 77 61 79 73 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  ways zero-termin
af050 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 63 20 3d  ated..  */.  c =
af060 20 2a 28 7a 49 6e 2b 2b 29 3b 0a 20 20 69 66 28   *(zIn++);.  if(
af070 20 63 3e 3d 30 78 63 30 20 29 7b 0a 20 20 20 20   c>=0xc0 ){.    
af080 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 54  c = sqlite3Utf8T
af090 72 61 6e 73 31 5b 63 2d 30 78 63 30 5d 3b 0a 20  rans1[c-0xc0];. 
af0a0 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20     while( (*zIn 
af0b0 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b  & 0xc0)==0x80 ){
af0c0 0a 20 20 20 20 20 20 63 20 3d 20 28 63 3c 3c 36  .      c = (c<<6
af0d0 29 20 2b 20 28 30 78 33 66 20 26 20 2a 28 7a 49  ) + (0x3f & *(zI
af0e0 6e 2b 2b 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20  n++));.    }.   
af0f0 20 69 66 28 20 63 3c 30 78 38 30 0a 20 20 20 20   if( c<0x80.    
af100 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46 46 46      || (c&0xFFFF
af110 46 38 30 30 29 3d 3d 30 78 44 38 30 30 0a 20 20  F800)==0xD800.  
af120 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46        || (c&0xFF
af130 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20  FFFFFE)==0xFFFE 
af140 29 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20  ){  c = 0xFFFD; 
af150 7d 0a 20 20 7d 0a 20 20 2a 70 7a 4e 65 78 74 20  }.  }.  *pzNext 
af160 3d 20 7a 49 6e 3b 0a 20 20 72 65 74 75 72 6e 20  = zIn;.  return 
af170 63 3b 0a 7d 0a 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 49  c;.}...../*.** I
af180 66 20 74 68 65 20 54 52 41 4e 53 4c 41 54 45 5f  f the TRANSLATE_
af190 54 52 41 43 45 20 6d 61 63 72 6f 20 69 73 20 64  TRACE macro is d
af1a0 65 66 69 6e 65 64 2c 20 74 68 65 20 76 61 6c 75  efined, the valu
af1b0 65 20 6f 66 20 65 61 63 68 20 4d 65 6d 20 69 73  e of each Mem is
af1c0 0a 2a 2a 20 70 72 69 6e 74 65 64 20 6f 6e 20 73  .** printed on s
af1d0 74 64 65 72 72 20 6f 6e 20 74 68 65 20 77 61 79  tderr on the way
af1e0 20 69 6e 74 6f 20 61 6e 64 20 6f 75 74 20 6f 66   into and out of
af1f0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54   sqlite3VdbeMemT
af200 72 61 6e 73 6c 61 74 65 28 29 2e 0a 2a 2f 20 0a  ranslate()..*/ .
af210 2f 2a 20 23 64 65 66 69 6e 65 20 54 52 41 4e 53  /* #define TRANS
af220 4c 41 54 45 5f 54 52 41 43 45 20 31 20 2a 2f 0a  LATE_TRACE 1 */.
af230 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
af240 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
af250 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72   This routine tr
af260 61 6e 73 66 6f 72 6d 73 20 74 68 65 20 69 6e 74  ansforms the int
af270 65 72 6e 61 6c 20 74 65 78 74 20 65 6e 63 6f 64  ernal text encod
af280 69 6e 67 20 75 73 65 64 20 62 79 20 70 4d 65 6d  ing used by pMem
af290 20 74 6f 0a 2a 2a 20 64 65 73 69 72 65 64 45 6e   to.** desiredEn
af2a0 63 2e 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f  c. It is an erro
af2b0 72 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20  r if the string 
af2c0 69 73 20 61 6c 72 65 61 64 79 20 6f 66 20 74 68  is already of th
af2d0 65 20 64 65 73 69 72 65 64 0a 2a 2a 20 65 6e 63  e desired.** enc
af2e0 6f 64 69 6e 67 2c 20 6f 72 20 69 66 20 2a 70 4d  oding, or if *pM
af2f0 65 6d 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74  em does not cont
af300 61 69 6e 20 61 20 73 74 72 69 6e 67 20 76 61 6c  ain a string val
af310 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ue..*/.SQLITE_PR
af320 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
af330 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74  3VdbeMemTranslat
af340 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 75 38 20  e(Mem *pMem, u8 
af350 64 65 73 69 72 65 64 45 6e 63 29 7b 0a 20 20 69  desiredEnc){.  i
af360 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20  nt len;         
af370 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
af380 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
af390 6f 75 74 70 75 74 20 73 74 72 69 6e 67 20 69 6e  output string in
af3a0 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 6e 73 69   bytes */.  unsi
af3b0 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 3b  gned char *zOut;
af3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
af3d0 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66    /* Output buff
af3e0 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  er */.  unsigned
af3f0 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20   char *zIn;     
af400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
af410 20 49 6e 70 75 74 20 69 74 65 72 61 74 6f 72 20   Input iterator 
af420 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
af430 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20 20 20 20  ar *zTerm;      
af440 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
af450 64 20 6f 66 20 69 6e 70 75 74 20 2a 2f 0a 20 20  d of input */.  
af460 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
af470 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
af480 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
af490 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75 6e  iterator */.  un
af4a0 73 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20  signed int c;.. 
af4b0 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
af4c0 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
af4d0 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
af4e0 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
af4f0 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 66   assert( pMem->f
af500 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 3b 0a  lags&MEM_Str );.
af510 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
af520 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63 20  enc!=desiredEnc 
af530 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  );.  assert( pMe
af540 6d 2d 3e 65 6e 63 21 3d 30 20 29 3b 0a 20 20 61  m->enc!=0 );.  a
af550 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d  ssert( pMem->n>=
af560 30 20 29 3b 0a 0a 23 69 66 20 64 65 66 69 6e 65  0 );..#if define
af570 64 28 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43  d(TRANSLATE_TRAC
af580 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  E) && defined(SQ
af590 4c 49 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a  LITE_DEBUG).  {.
af5a0 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30      char zBuf[10
af5b0 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0];.    sqlite3V
af5c0 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e  dbeMemPrettyPrin
af5d0 74 28 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20  t(pMem, zBuf);. 
af5e0 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72     fprintf(stder
af5f0 72 2c 20 22 49 4e 50 55 54 3a 20 20 25 73 5c 6e  r, "INPUT:  %s\n
af600 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65  ", zBuf);.  }.#e
af610 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ndif..  /* If th
af620 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73  e translation is
af630 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36 20   between UTF-16 
af640 6c 69 74 74 6c 65 20 61 6e 64 20 62 69 67 20 65  little and big e
af650 6e 64 69 61 6e 2c 20 74 68 65 6e 20 0a 20 20 2a  ndian, then .  *
af660 2a 20 61 6c 6c 20 74 68 61 74 20 69 73 20 72 65  * all that is re
af670 71 75 69 72 65 64 20 69 73 20 74 6f 20 73 77 61  quired is to swa
af680 70 20 74 68 65 20 62 79 74 65 20 6f 72 64 65 72  p the byte order
af690 2e 20 54 68 69 73 20 63 61 73 65 20 69 73 20 68  . This case is h
af6a0 61 6e 64 6c 65 64 0a 20 20 2a 2a 20 64 69 66 66  andled.  ** diff
af6b0 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65  erently from the
af6c0 20 6f 74 68 65 72 73 2e 0a 20 20 2a 2f 0a 20 20   others..  */.  
af6d0 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 53  if( pMem->enc!=S
af6e0 51 4c 49 54 45 5f 55 54 46 38 20 26 26 20 64 65  QLITE_UTF8 && de
af6f0 73 69 72 65 64 45 6e 63 21 3d 53 51 4c 49 54 45  siredEnc!=SQLITE
af700 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 75 38 20  _UTF8 ){.    u8 
af710 74 65 6d 70 3b 0a 20 20 20 20 69 6e 74 20 72 63  temp;.    int rc
af720 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
af730 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69  e3VdbeMemMakeWri
af740 74 65 61 62 6c 65 28 70 4d 65 6d 29 3b 0a 20 20  teable(pMem);.  
af750 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
af760 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73  _OK ){.      ass
af770 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
af780 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 72  NOMEM );.      r
af790 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
af7a0 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 49  EM;.    }.    zI
af7b0 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a  n = (u8*)pMem->z
af7c0 3b 0a 20 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a  ;.    zTerm = &z
af7d0 49 6e 5b 70 4d 65 6d 2d 3e 6e 26 7e 31 5d 3b 0a  In[pMem->n&~1];.
af7e0 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a      while( zIn<z
af7f0 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 74 65  Term ){.      te
af800 6d 70 20 3d 20 2a 7a 49 6e 3b 0a 20 20 20 20 20  mp = *zIn;.     
af810 20 2a 7a 49 6e 20 3d 20 2a 28 7a 49 6e 2b 31 29   *zIn = *(zIn+1)
af820 3b 0a 20 20 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20  ;.      zIn++;. 
af830 20 20 20 20 20 2a 7a 49 6e 2b 2b 20 3d 20 74 65       *zIn++ = te
af840 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d  mp;.    }.    pM
af850 65 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65  em->enc = desire
af860 64 45 6e 63 3b 0a 20 20 20 20 67 6f 74 6f 20 74  dEnc;.    goto t
af870 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3b 0a 20 20  ranslate_out;.  
af880 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 6c 65 6e 20  }..  /* Set len 
af890 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  to the maximum n
af8a0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72  umber of bytes r
af8b0 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 20 6f  equired in the o
af8c0 75 74 70 75 74 20 62 75 66 66 65 72 2e 20 2a 2f  utput buffer. */
af8d0 0a 20 20 69 66 28 20 64 65 73 69 72 65 64 45 6e  .  if( desiredEn
af8e0 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c==SQLITE_UTF8 )
af8f0 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f  {.    /* When co
af900 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 55 54  nverting from UT
af910 46 2d 31 36 2c 20 74 68 65 20 6d 61 78 69 6d 75  F-16, the maximu
af920 6d 20 67 72 6f 77 74 68 20 72 65 73 75 6c 74 73  m growth results
af930 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74 72 61   from.    ** tra
af940 6e 73 6c 61 74 69 6e 67 20 61 20 32 2d 62 79 74  nslating a 2-byt
af950 65 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 61  e character to a
af960 20 34 2d 62 79 74 65 20 55 54 46 2d 38 20 63 68   4-byte UTF-8 ch
af970 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20  aracter..    ** 
af980 41 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 73  A single byte is
af990 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68   required for th
af9a0 65 20 6f 75 74 70 75 74 20 73 74 72 69 6e 67 0a  e output string.
af9b0 20 20 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69      ** nul-termi
af9c0 6e 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  nator..    */.  
af9d0 20 20 70 4d 65 6d 2d 3e 6e 20 26 3d 20 7e 31 3b    pMem->n &= ~1;
af9e0 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d  .    len = pMem-
af9f0 3e 6e 20 2a 20 32 20 2b 20 31 3b 0a 20 20 7d 65  >n * 2 + 1;.  }e
afa00 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57 68 65 6e  lse{.    /* When
afa10 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d   converting from
afa20 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 31 36   UTF-8 to UTF-16
afa30 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f   the maximum gro
afa40 77 74 68 20 69 73 20 63 61 75 73 65 64 0a 20 20  wth is caused.  
afa50 20 20 2a 2a 20 77 68 65 6e 20 61 20 31 2d 62 79    ** when a 1-by
afa60 74 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  te UTF-8 charact
afa70 65 72 20 69 73 20 74 72 61 6e 73 6c 61 74 65 64  er is translated
afa80 20 69 6e 74 6f 20 61 20 32 2d 62 79 74 65 20 55   into a 2-byte U
afa90 54 46 2d 31 36 0a 20 20 20 20 2a 2a 20 63 68 61  TF-16.    ** cha
afaa0 72 61 63 74 65 72 2e 20 54 77 6f 20 62 79 74 65  racter. Two byte
afab0 73 20 61 72 65 20 72 65 71 75 69 72 65 64 20 69  s are required i
afac0 6e 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  n the output buf
afad0 66 65 72 20 66 6f 72 20 74 68 65 0a 20 20 20 20  fer for the.    
afae0 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  ** nul-terminato
afaf0 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65  r..    */.    le
afb00 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32 20  n = pMem->n * 2 
afb10 2b 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  + 2;.  }..  /* S
afb20 65 74 20 7a 49 6e 20 74 6f 20 70 6f 69 6e 74 20  et zIn to point 
afb30 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  at the start of 
afb40 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 72  the input buffer
afb50 20 61 6e 64 20 7a 54 65 72 6d 20 74 6f 20 70 6f   and zTerm to po
afb60 69 6e 74 20 31 0a 20 20 2a 2a 20 62 79 74 65 20  int 1.  ** byte 
afb70 70 61 73 74 20 74 68 65 20 65 6e 64 2e 0a 20 20  past the end..  
afb80 2a 2a 0a 20 20 2a 2a 20 56 61 72 69 61 62 6c 65  **.  ** Variable
afb90 20 7a 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20   zOut is set to 
afba0 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6f 75 74  point at the out
afbb0 70 75 74 20 62 75 66 66 65 72 2c 20 73 70 61 63  put buffer, spac
afbc0 65 20 6f 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20  e obtained.  ** 
afbd0 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
afbe0 6c 6f 63 28 29 2e 0a 20 20 2a 2f 0a 20 20 7a 49  loc()..  */.  zI
afbf0 6e 20 3d 20 28 75 38 2a 29 70 4d 65 6d 2d 3e 7a  n = (u8*)pMem->z
afc00 3b 0a 20 20 7a 54 65 72 6d 20 3d 20 26 7a 49 6e  ;.  zTerm = &zIn
afc10 5b 70 4d 65 6d 2d 3e 6e 5d 3b 0a 20 20 7a 4f 75  [pMem->n];.  zOu
afc20 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  t = sqlite3DbMal
afc30 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e 64 62 2c  locRaw(pMem->db,
afc40 20 6c 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a 4f   len);.  if( !zO
afc50 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ut ){.    return
afc60 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
afc70 20 7d 0a 20 20 7a 20 3d 20 7a 4f 75 74 3b 0a 0a   }.  z = zOut;..
afc80 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63 3d    if( pMem->enc=
afc90 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a  =SQLITE_UTF8 ){.
afca0 20 20 20 20 69 66 28 20 64 65 73 69 72 65 64 45      if( desiredE
afcb0 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
afcc0 4c 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55  LE ){.      /* U
afcd0 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31 36 20 4c  TF-8 -> UTF-16 L
afce0 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2a 2f 0a  ittle-endian */.
afcf0 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e        while( zIn
afd00 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  <zTerm ){.      
afd10 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69 74 65 33    /* c = sqlite3
afd20 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54  Utf8Read(zIn, zT
afd30 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a  erm, (const u8**
afd40 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 20 20 20  )&zIn); */.     
afd50 20 20 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e     READ_UTF8(zIn
afd60 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 20  , zTerm, c);.   
afd70 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36       WRITE_UTF16
afd80 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20  LE(z, c);.      
afd90 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
afda0 20 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72     assert( desir
afdb0 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  edEnc==SQLITE_UT
afdc0 46 31 36 42 45 20 29 3b 0a 20 20 20 20 20 20 2f  F16BE );.      /
afdd0 2a 20 55 54 46 2d 38 20 2d 3e 20 55 54 46 2d 31  * UTF-8 -> UTF-1
afde0 36 20 42 69 67 2d 65 6e 64 69 61 6e 20 2a 2f 0a  6 Big-endian */.
afdf0 20 20 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e        while( zIn
afe00 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  <zTerm ){.      
afe10 20 20 2f 2a 20 63 20 3d 20 73 71 6c 69 74 65 33    /* c = sqlite3
afe20 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 7a 54  Utf8Read(zIn, zT
afe30 65 72 6d 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a  erm, (const u8**
afe40 29 26 7a 49 6e 29 3b 20 2a 2f 0a 20 20 20 20 20  )&zIn); */.     
afe50 20 20 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e     READ_UTF8(zIn
afe60 2c 20 7a 54 65 72 6d 2c 20 63 29 3b 0a 20 20 20  , zTerm, c);.   
afe70 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36       WRITE_UTF16
afe80 42 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20  BE(z, c);.      
afe90 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d  }.    }.    pMem
afea0 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20  ->n = (int)(z - 
afeb0 7a 4f 75 74 29 3b 0a 20 20 20 20 2a 7a 2b 2b 20  zOut);.    *z++ 
afec0 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
afed0 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65    assert( desire
afee0 64 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  dEnc==SQLITE_UTF
afef0 38 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4d 65  8 );.    if( pMe
aff00 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  m->enc==SQLITE_U
aff10 54 46 31 36 4c 45 20 29 7b 0a 20 20 20 20 20 20  TF16LE ){.      
aff20 2f 2a 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65  /* UTF-16 Little
aff30 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d 38  -endian -> UTF-8
aff40 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28   */.      while(
aff50 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20 20   zIn<zTerm ){.  
aff60 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36        READ_UTF16
aff70 4c 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54 65 72  LE(zIn, zIn<zTer
aff80 6d 2c 20 63 29 3b 20 0a 20 20 20 20 20 20 20 20  m, c); .        
aff90 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 63 29  WRITE_UTF8(z, c)
affa0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
affb0 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54  lse{.      /* UT
affc0 46 2d 31 36 20 42 69 67 2d 65 6e 64 69 61 6e 20  F-16 Big-endian 
affd0 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20 20 20  -> UTF-8 */.    
affe0 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65    while( zIn<zTe
afff0 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 52 45  rm ){.        RE
b0000 41 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20  AD_UTF16BE(zIn, 
b0010 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b 20 0a  zIn<zTerm, c); .
b0020 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55 54          WRITE_UT
b0030 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20  F8(z, c);.      
b0040 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d  }.    }.    pMem
b0050 2d 3e 6e 20 3d 20 28 69 6e 74 29 28 7a 20 2d 20  ->n = (int)(z - 
b0060 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 2a 7a 20  zOut);.  }.  *z 
b0070 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 28  = 0;.  assert( (
b0080 70 4d 65 6d 2d 3e 6e 2b 28 64 65 73 69 72 65 64  pMem->n+(desired
b0090 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38  Enc==SQLITE_UTF8
b00a0 3f 31 3a 32 29 29 3c 3d 6c 65 6e 20 29 3b 0a 0a  ?1:2))<=len );..
b00b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
b00c0 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20  Release(pMem);. 
b00d0 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 3d 20   pMem->flags &= 
b00e0 7e 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d  ~(MEM_Static|MEM
b00f0 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 3b  _Dyn|MEM_Ephem);
b0100 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 64  .  pMem->enc = d
b0110 65 73 69 72 65 64 45 6e 63 3b 0a 20 20 70 4d 65  esiredEnc;.  pMe
b0120 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 4d 45 4d  m->flags |= (MEM
b0130 5f 54 65 72 6d 7c 4d 45 4d 5f 44 79 6e 29 3b 0a  _Term|MEM_Dyn);.
b0140 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61    pMem->z = (cha
b0150 72 2a 29 7a 4f 75 74 3b 0a 20 20 70 4d 65 6d 2d  r*)zOut;.  pMem-
b0160 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d  >zMalloc = pMem-
b0170 3e 7a 3b 0a 0a 74 72 61 6e 73 6c 61 74 65 5f 6f  >z;..translate_o
b0180 75 74 3a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  ut:.#if defined(
b0190 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 29  TRANSLATE_TRACE)
b01a0 20 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49   && defined(SQLI
b01b0 54 45 5f 44 45 42 55 47 29 0a 20 20 7b 0a 20 20  TE_DEBUG).  {.  
b01c0 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d    char zBuf[100]
b01d0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
b01e0 65 4d 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28  eMemPrettyPrint(
b01f0 70 4d 65 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20  pMem, zBuf);.   
b0200 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
b0210 20 22 4f 55 54 50 55 54 3a 20 25 73 5c 6e 22 2c   "OUTPUT: %s\n",
b0220 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64   zBuf);.  }.#end
b0230 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  if.  return SQLI
b0240 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
b0250 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
b0260 63 6b 73 20 66 6f 72 20 61 20 62 79 74 65 2d 6f  cks for a byte-o
b0270 72 64 65 72 20 6d 61 72 6b 20 61 74 20 74 68 65  rder mark at the
b0280 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
b0290 65 20 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72  e .** UTF-16 str
b02a0 69 6e 67 20 73 74 6f 72 65 64 20 69 6e 20 2a 70  ing stored in *p
b02b0 4d 65 6d 2e 20 49 66 20 6f 6e 65 20 69 73 20 70  Mem. If one is p
b02c0 72 65 73 65 6e 74 2c 20 69 74 20 69 73 20 72 65  resent, it is re
b02d0 6d 6f 76 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65  moved and.** the
b02e0 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65   encoding of the
b02f0 20 4d 65 6d 20 61 64 6a 75 73 74 65 64 2e 20 54   Mem adjusted. T
b0300 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  his routine does
b0310 20 6e 6f 74 20 64 6f 20 61 6e 79 0a 2a 2a 20 62   not do any.** b
b0320 79 74 65 2d 73 77 61 70 70 69 6e 67 2c 20 69 74  yte-swapping, it
b0330 20 6a 75 73 74 20 73 65 74 73 20 4d 65 6d 2e 65   just sets Mem.e
b0340 6e 63 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79  nc appropriately
b0350 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f  ..**.** The allo
b0360 63 61 74 69 6f 6e 20 28 73 74 61 74 69 63 2c 20  cation (static, 
b0370 64 79 6e 61 6d 69 63 20 65 74 63 2e 29 20 61 6e  dynamic etc.) an
b0380 64 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68  d encoding of th
b0390 65 20 4d 65 6d 20 6d 61 79 20 62 65 0a 2a 2a 20  e Mem may be.** 
b03a0 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 20  changed by this 
b03b0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  function..*/.SQL
b03c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b03d0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48 61  sqlite3VdbeMemHa
b03e0 6e 64 6c 65 42 6f 6d 28 4d 65 6d 20 2a 70 4d 65  ndleBom(Mem *pMe
b03f0 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  m){.  int rc = S
b0400 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 62  QLITE_OK;.  u8 b
b0410 6f 6d 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  om = 0;..  asser
b0420 74 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b  t( pMem->n>=0 );
b0430 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3e 31  .  if( pMem->n>1
b0440 20 29 7b 0a 20 20 20 20 75 38 20 62 31 20 3d 20   ){.    u8 b1 = 
b0450 2a 28 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a  *(u8 *)pMem->z;.
b0460 20 20 20 20 75 38 20 62 32 20 3d 20 2a 28 28 28      u8 b2 = *(((
b0470 75 38 20 2a 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20  u8 *)pMem->z) + 
b0480 31 29 3b 0a 20 20 20 20 69 66 28 20 62 31 3d 3d  1);.    if( b1==
b0490 30 78 46 45 20 26 26 20 62 32 3d 3d 30 78 46 46  0xFE && b2==0xFF
b04a0 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20   ){.      bom = 
b04b0 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a  SQLITE_UTF16BE;.
b04c0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 62 31      }.    if( b1
b04d0 3d 3d 30 78 46 46 20 26 26 20 62 32 3d 3d 30 78  ==0xFF && b2==0x
b04e0 46 45 20 29 7b 0a 20 20 20 20 20 20 62 6f 6d 20  FE ){.      bom 
b04f0 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  = SQLITE_UTF16LE
b0500 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20  ;.    }.  }.  . 
b0510 20 69 66 28 20 62 6f 6d 20 29 7b 0a 20 20 20 20   if( bom ){.    
b0520 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
b0530 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65  MemMakeWriteable
b0540 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20  (pMem);.    if( 
b0550 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
b0560 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d  .      pMem->n -
b0570 3d 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 6d 6f  = 2;.      memmo
b0580 76 65 28 70 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65  ve(pMem->z, &pMe
b0590 6d 2d 3e 7a 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e  m->z[2], pMem->n
b05a0 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a  );.      pMem->z
b05b0 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27  [pMem->n] = '\0'
b05c0 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b  ;.      pMem->z[
b05d0 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30  pMem->n+1] = '\0
b05e0 27 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  ';.      pMem->f
b05f0 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d  lags |= MEM_Term
b0600 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e  ;.      pMem->en
b0610 63 20 3d 20 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20  c = bom;.    }. 
b0620 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
b0630 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
b0640 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
b0650 0a 0a 2f 2a 0a 2a 2a 20 70 5a 20 69 73 20 61 20  ../*.** pZ is a 
b0660 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 75 6e  UTF-8 encoded un
b0670 69 63 6f 64 65 20 73 74 72 69 6e 67 2e 20 49 66  icode string. If
b0680 20 6e 42 79 74 65 20 69 73 20 6c 65 73 73 20 74   nByte is less t
b0690 68 61 6e 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74  han zero,.** ret
b06a0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
b06b0 66 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63  f unicode charac
b06c0 74 65 72 73 20 69 6e 20 70 5a 20 75 70 20 74 6f  ters in pZ up to
b06d0 20 28 62 75 74 20 6e 6f 74 20 69 6e 63 6c 75 64   (but not includ
b06e0 69 6e 67 29 0a 2a 2a 20 74 68 65 20 66 69 72 73  ing).** the firs
b06f0 74 20 30 78 30 30 20 62 79 74 65 2e 20 49 66 20  t 0x00 byte. If 
b0700 6e 42 79 74 65 20 69 73 20 6e 6f 74 20 6c 65 73  nByte is not les
b0710 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 72 65 74  s than zero, ret
b0720 75 72 6e 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65  urn the.** numbe
b0730 72 20 6f 66 20 75 6e 69 63 6f 64 65 20 63 68 61  r of unicode cha
b0740 72 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 66  racters in the f
b0750 69 72 73 74 20 6e 42 79 74 65 20 6f 66 20 70 5a  irst nByte of pZ
b0760 20 28 6f 72 20 75 70 20 74 6f 20 0a 2a 2a 20 74   (or up to .** t
b0770 68 65 20 66 69 72 73 74 20 30 78 30 30 2c 20 77  he first 0x00, w
b0780 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66  hichever comes f
b0790 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  irst)..*/.SQLITE
b07a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b07b0 69 74 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28  ite3Utf8CharLen(
b07c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c  const char *zIn,
b07d0 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 69   int nByte){.  i
b07e0 6e 74 20 72 20 3d 20 30 3b 0a 20 20 63 6f 6e 73  nt r = 0;.  cons
b07f0 74 20 75 38 20 2a 7a 20 3d 20 28 63 6f 6e 73 74  t u8 *z = (const
b0800 20 75 38 2a 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73   u8*)zIn;.  cons
b0810 74 20 75 38 20 2a 7a 54 65 72 6d 3b 0a 20 20 69  t u8 *zTerm;.  i
b0820 66 28 20 6e 42 79 74 65 3e 3d 30 20 29 7b 0a 20  f( nByte>=0 ){. 
b0830 20 20 20 7a 54 65 72 6d 20 3d 20 26 7a 5b 6e 42     zTerm = &z[nB
b0840 79 74 65 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  yte];.  }else{. 
b0850 20 20 20 7a 54 65 72 6d 20 3d 20 28 63 6f 6e 73     zTerm = (cons
b0860 74 20 75 38 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a  t u8*)(-1);.  }.
b0870 20 20 61 73 73 65 72 74 28 20 7a 3c 3d 7a 54 65    assert( z<=zTe
b0880 72 6d 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a  rm );.  while( *
b0890 7a 21 3d 30 20 26 26 20 7a 3c 7a 54 65 72 6d 20  z!=0 && z<zTerm 
b08a0 29 7b 0a 20 20 20 20 53 51 4c 49 54 45 5f 53 4b  ){.    SQLITE_SK
b08b0 49 50 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20  IP_UTF8(z);.    
b08c0 72 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  r++;.  }.  retur
b08d0 6e 20 72 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20  n r;.}../* This 
b08e0 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 69 73  test function is
b08f0 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 75   not currently u
b0900 73 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 6d  sed by the autom
b0910 61 74 65 64 20 74 65 73 74 2d 73 75 69 74 65 2e  ated test-suite.
b0920 20 0a 2a 2a 20 48 65 6e 63 65 20 69 74 20 69 73   .** Hence it is
b0930 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
b0940 69 6e 20 64 65 62 75 67 20 62 75 69 6c 64 73 2e  in debug builds.
b0950 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
b0960 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20  SQLITE_TEST) && 
b0970 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
b0980 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e  EBUG)./*.** Tran
b0990 73 6c 61 74 65 20 55 54 46 2d 38 20 74 6f 20 55  slate UTF-8 to U
b09a0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  TF-8..**.** This
b09b0 20 68 61 73 20 74 68 65 20 65 66 66 65 63 74 20   has the effect 
b09c0 6f 66 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74  of making sure t
b09d0 68 61 74 20 74 68 65 20 73 74 72 69 6e 67 20 69  hat the string i
b09e0 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a  s well-formed.**
b09f0 20 55 54 46 2d 38 2e 20 20 4d 69 73 63 6f 64 65   UTF-8.  Miscode
b0a00 64 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  d characters are
b0a10 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20   removed..**.** 
b0a20 54 68 65 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20  The translation 
b0a30 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65  is done in-place
b0a40 20 28 73 69 6e 63 65 20 69 74 20 69 73 20 69 6d   (since it is im
b0a50 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65  possible for the
b0a60 0a 2a 2a 20 63 6f 72 72 65 63 74 20 55 54 46 2d  .** correct UTF-
b0a70 38 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65  8 encoding to be
b0a80 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 61 20 6d   longer than a m
b0a90 61 6c 66 6f 72 6d 65 64 20 65 6e 63 6f 64 69 6e  alformed encodin
b0aa0 67 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  g)..*/.SQLITE_PR
b0ab0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b0ac0 33 55 74 66 38 54 6f 38 28 75 6e 73 69 67 6e 65  3Utf8To8(unsigne
b0ad0 64 20 63 68 61 72 20 2a 7a 49 6e 29 7b 0a 20 20  d char *zIn){.  
b0ae0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
b0af0 4f 75 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73  Out = zIn;.  uns
b0b00 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74 61  igned char *zSta
b0b10 72 74 20 3d 20 7a 49 6e 3b 0a 20 20 75 33 32 20  rt = zIn;.  u32 
b0b20 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e  c;..  while( zIn
b0b30 5b 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73  [0] ){.    c = s
b0b40 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
b0b50 49 6e 2c 20 28 63 6f 6e 73 74 20 75 38 2a 2a 29  In, (const u8**)
b0b60 26 7a 49 6e 29 3b 0a 20 20 20 20 69 66 28 20 63  &zIn);.    if( c
b0b70 21 3d 30 78 66 66 66 64 20 29 7b 0a 20 20 20 20  !=0xfffd ){.    
b0b80 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 4f 75    WRITE_UTF8(zOu
b0b90 74 2c 20 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  t, c);.    }.  }
b0ba0 0a 20 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 20 20  .  *zOut = 0;.  
b0bb0 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 4f 75  return (int)(zOu
b0bc0 74 20 2d 20 7a 53 74 61 72 74 29 3b 0a 7d 0a 23  t - zStart);.}.#
b0bd0 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
b0be0 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
b0bf0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
b0c00 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69   UTF-16 string i
b0c10 6e 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63  n the native enc
b0c20 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46  oding into a UTF
b0c30 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65  -8 string..** Me
b0c40 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
b0c50 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73   UTF-8 string is
b0c60 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
b0c70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e  qlite3_malloc an
b0c80 64 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65  d must.** be fre
b0c90 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ed by the callin
b0ca0 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  g function..**.*
b0cb0 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  * NULL is return
b0cc0 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ed if there is a
b0cd0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  n allocation err
b0ce0 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  or..*/.SQLITE_PR
b0cf0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
b0d00 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69  te3Utf16to8(sqli
b0d10 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76  te3 *db, const v
b0d20 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74  oid *z, int nByt
b0d30 65 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d  e){.  Mem m;.  m
b0d40 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a  emset(&m, 0, siz
b0d50 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20  eof(m));.  m.db 
b0d60 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56  = db;.  sqlite3V
b0d70 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c  dbeMemSetStr(&m,
b0d80 20 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54   z, nByte, SQLIT
b0d90 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53  E_UTF16NATIVE, S
b0da0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
b0db0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
b0dc0 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53  geEncoding(&m, S
b0dd0 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69  QLITE_UTF8);.  i
b0de0 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
b0df0 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  led ){.    sqlit
b0e00 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
b0e10 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20  (&m);.    m.z = 
b0e20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  0;.  }.  assert(
b0e30 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   (m.flags & MEM_
b0e40 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e  Term)!=0 || db->
b0e50 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
b0e60 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61    assert( (m.fla
b0e70 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30  gs & MEM_Str)!=0
b0e80 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
b0e90 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e  iled );.  return
b0ea0 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   (m.flags & MEM_
b0eb0 44 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20  Dyn)!=0 ? m.z : 
b0ec0 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
b0ed0 64 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a  db, m.z);.}../*.
b0ee0 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46  ** Convert a UTF
b0ef0 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 74 68 65  -8 string to the
b0f00 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67   UTF-16 encoding
b0f10 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
b0f20 72 61 6d 65 74 65 72 0a 2a 2a 20 65 6e 63 2e 20  rameter.** enc. 
b0f30 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
b0f40 20 6e 65 77 20 73 74 72 69 6e 67 20 69 73 20 72   new string is r
b0f50 65 74 75 72 6e 65 64 2c 20 61 6e 64 20 74 68 65  eturned, and the
b0f60 20 76 61 6c 75 65 20 6f 66 20 2a 70 6e 4f 75 74   value of *pnOut
b0f70 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68  .** is set to th
b0f80 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
b0f90 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
b0fa0 69 6e 20 62 79 74 65 73 2e 20 54 68 65 20 63 61  in bytes. The ca
b0fb0 6c 6c 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 72 72  ll should.** arr
b0fc0 61 6e 67 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c  ange to call sql
b0fd0 69 74 65 33 44 62 46 72 65 65 28 29 20 6f 6e 20  ite3DbFree() on 
b0fe0 74 68 65 20 72 65 74 75 72 6e 65 64 20 70 6f 69  the returned poi
b0ff0 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 0a  nter when it is.
b1000 2a 2a 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 71  ** no longer req
b1010 75 69 72 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 49 66  uired..** .** If
b1020 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72   a malloc failur
b1030 65 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69  e occurs, NULL i
b1040 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
b1050 68 65 20 64 62 2e 6d 61 6c 6c 6f 63 46 61 69 6c  he db.mallocFail
b1060 65 64 0a 2a 2a 20 66 6c 61 67 20 73 65 74 2e 0a  ed.** flag set..
b1070 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
b1080 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 53 51  _ENABLE_STAT2.SQ
b1090 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
b10a0 72 20 2a 73 71 6c 69 74 65 33 55 74 66 38 74 6f  r *sqlite3Utf8to
b10b0 31 36 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  16(sqlite3 *db, 
b10c0 75 38 20 65 6e 63 2c 20 63 68 61 72 20 2a 7a 2c  u8 enc, char *z,
b10d0 20 69 6e 74 20 6e 2c 20 69 6e 74 20 2a 70 6e 4f   int n, int *pnO
b10e0 75 74 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20  ut){.  Mem m;.  
b10f0 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69  memset(&m, 0, si
b1100 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62  zeof(m));.  m.db
b1110 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33   = db;.  sqlite3
b1120 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d  VdbeMemSetStr(&m
b1130 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55  , z, n, SQLITE_U
b1140 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  TF8, SQLITE_STAT
b1150 49 43 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  IC);.  if( sqlit
b1160 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61  e3VdbeMemTransla
b1170 74 65 28 26 6d 2c 20 65 6e 63 29 20 29 7b 0a 20  te(&m, enc) ){. 
b1180 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d     assert( db->m
b1190 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
b11a0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
b11b0 0a 20 20 61 73 73 65 72 74 28 20 6d 2e 7a 3d 3d  .  assert( m.z==
b11c0 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 2a  m.zMalloc );.  *
b11d0 70 6e 4f 75 74 20 3d 20 6d 2e 6e 3b 0a 20 20 72  pnOut = m.n;.  r
b11e0 65 74 75 72 6e 20 6d 2e 7a 3b 0a 7d 0a 23 65 6e  eturn m.z;.}.#en
b11f0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 7a 49 6e 20 69  dif../*.** zIn i
b1200 73 20 61 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  s a UTF-16 encod
b1210 65 64 20 75 6e 69 63 6f 64 65 20 73 74 72 69 6e  ed unicode strin
b1220 67 20 61 74 20 6c 65 61 73 74 20 6e 43 68 61 72  g at least nChar
b1230 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67   characters long
b1240 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ..** Return the 
b1250 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
b1260 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e 43 68  in the first nCh
b1270 61 72 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61  ar unicode chara
b1280 63 74 65 72 73 0a 2a 2a 20 69 6e 20 70 5a 2e 20  cters.** in pZ. 
b1290 20 6e 43 68 61 72 20 6d 75 73 74 20 62 65 20 6e   nChar must be n
b12a0 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2f 0a  on-negative..*/.
b12b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b12c0 6e 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42  nt sqlite3Utf16B
b12d0 79 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69  yteLen(const voi
b12e0 64 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 43 68 61  d *zIn, int nCha
b12f0 72 29 7b 0a 20 20 69 6e 74 20 63 3b 0a 20 20 75  r){.  int c;.  u
b1300 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e  nsigned char con
b1310 73 74 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69  st *z = zIn;.  i
b1320 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 0a 20 20 69  nt n = 0;.  .  i
b1330 66 28 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  f( SQLITE_UTF16N
b1340 41 54 49 56 45 3d 3d 53 51 4c 49 54 45 5f 55 54  ATIVE==SQLITE_UT
b1350 46 31 36 42 45 20 29 7b 0a 20 20 20 20 77 68 69  F16BE ){.    whi
b1360 6c 65 28 20 6e 3c 6e 43 68 61 72 20 29 7b 0a 20  le( n<nChar ){. 
b1370 20 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 42       READ_UTF16B
b1380 45 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20  E(z, 1, c);.    
b1390 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d    n++;.    }.  }
b13a0 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c 65 28  else{.    while(
b13b0 20 6e 3c 6e 43 68 61 72 20 29 7b 0a 20 20 20 20   n<nChar ){.    
b13c0 20 20 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a    READ_UTF16LE(z
b13d0 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e  , 1, c);.      n
b13e0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ++;.    }.  }.  
b13f0 72 65 74 75 72 6e 20 28 69 6e 74 29 28 7a 2d 28  return (int)(z-(
b1400 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
b1410 6e 73 74 20 2a 29 7a 49 6e 29 3b 0a 7d 0a 0a 23  nst *)zIn);.}..#
b1420 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
b1430 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68  E_TEST)./*.** Th
b1440 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
b1450 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 54 43  lled from the TC
b1460 4c 20 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20  L test function 
b1470 22 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c 66 74  "translate_selft
b1480 65 73 74 22 2e 0a 2a 2a 20 49 74 20 63 68 65 63  est"..** It chec
b1490 6b 73 20 74 68 61 74 20 74 68 65 20 70 72 69 6d  ks that the prim
b14a0 69 74 69 76 65 73 20 66 6f 72 20 73 65 72 69 61  itives for seria
b14b0 6c 69 7a 69 6e 67 20 61 6e 64 20 64 65 73 65 72  lizing and deser
b14c0 69 61 6c 69 7a 69 6e 67 0a 2a 2a 20 63 68 61 72  ializing.** char
b14d0 61 63 74 65 72 73 20 69 6e 20 65 61 63 68 20 65  acters in each e
b14e0 6e 63 6f 64 69 6e 67 20 61 72 65 20 69 6e 76 65  ncoding are inve
b14f0 72 73 65 73 20 6f 66 20 65 61 63 68 20 6f 74 68  rses of each oth
b1500 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
b1510 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
b1520 65 33 55 74 66 53 65 6c 66 54 65 73 74 28 76 6f  e3UtfSelfTest(vo
b1530 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  id){.  unsigned 
b1540 69 6e 74 20 69 2c 20 74 3b 0a 20 20 75 6e 73 69  int i, t;.  unsi
b1550 67 6e 65 64 20 63 68 61 72 20 7a 42 75 66 5b 32  gned char zBuf[2
b1560 30 5d 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  0];.  unsigned c
b1570 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 6e 3b  har *z;.  int n;
b1580 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
b1590 63 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  c;..  for(i=0; i
b15a0 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b  <0x00110000; i++
b15b0 29 7b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b  ){.    z = zBuf;
b15c0 0a 20 20 20 20 57 52 49 54 45 5f 55 54 46 38 28  .    WRITE_UTF8(
b15d0 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28  z, i);.    n = (
b15e0 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20  int)(z-zBuf);.  
b15f0 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26    assert( n>0 &&
b1600 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30   n<=4 );.    z[0
b1610 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a  ] = 0;.    z = z
b1620 42 75 66 3b 0a 20 20 20 20 63 20 3d 20 73 71 6c  Buf;.    c = sql
b1630 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 2c 20  ite3Utf8Read(z, 
b1640 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 29 3b  (const u8**)&z);
b1650 0a 20 20 20 20 74 20 3d 20 69 3b 0a 20 20 20 20  .    t = i;.    
b1660 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26  if( i>=0xD800 &&
b1670 20 69 3c 3d 30 78 44 46 46 46 20 29 20 74 20 3d   i<=0xDFFF ) t =
b1680 20 30 78 46 46 46 44 3b 0a 20 20 20 20 69 66 28   0xFFFD;.    if(
b1690 20 28 69 26 30 78 46 46 46 46 46 46 46 45 29 3d   (i&0xFFFFFFFE)=
b16a0 3d 30 78 46 46 46 45 20 29 20 74 20 3d 20 30 78  =0xFFFE ) t = 0x
b16b0 46 46 46 44 3b 0a 20 20 20 20 61 73 73 65 72 74  FFFD;.    assert
b16c0 28 20 63 3d 3d 74 20 29 3b 0a 20 20 20 20 61 73  ( c==t );.    as
b16d0 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d  sert( (z-zBuf)==
b16e0 6e 20 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  n );.  }.  for(i
b16f0 3d 30 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30  =0; i<0x00110000
b1700 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
b1710 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30  i>=0xD800 && i<0
b1720 78 45 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65  xE000 ) continue
b1730 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a  ;.    z = zBuf;.
b1740 20 20 20 20 57 52 49 54 45 5f 55 54 46 31 36 4c      WRITE_UTF16L
b1750 45 28 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d  E(z, i);.    n =
b1760 20 28 69 6e 74 29 28 7a 2d 7a 42 75 66 29 3b 0a   (int)(z-zBuf);.
b1770 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20      assert( n>0 
b1780 26 26 20 6e 3c 3d 34 20 29 3b 0a 20 20 20 20 7a  && n<=4 );.    z
b1790 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 20 3d  [0] = 0;.    z =
b17a0 20 7a 42 75 66 3b 0a 20 20 20 20 52 45 41 44 5f   zBuf;.    READ_
b17b0 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63 29  UTF16LE(z, 1, c)
b17c0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d  ;.    assert( c=
b17d0 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =i );.    assert
b17e0 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b  ( (z-zBuf)==n );
b17f0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
b1800 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b  i<0x00110000; i+
b1810 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30  +){.    if( i>=0
b1820 78 44 38 30 30 20 26 26 20 69 3c 30 78 45 30 30  xD800 && i<0xE00
b1830 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
b1840 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20    z = zBuf;.    
b1850 57 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c  WRITE_UTF16BE(z,
b1860 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e   i);.    n = (in
b1870 74 29 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20  t)(z-zBuf);.    
b1880 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e  assert( n>0 && n
b1890 3c 3d 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20  <=4 );.    z[0] 
b18a0 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75  = 0;.    z = zBu
b18b0 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31  f;.    READ_UTF1
b18c0 36 42 45 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20  6BE(z, 1, c);.  
b18d0 20 20 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29    assert( c==i )
b18e0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 7a  ;.    assert( (z
b18f0 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d  -zBuf)==n );.  }
b1900 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
b1910 49 54 45 5f 54 45 53 54 20 2a 2f 0a 23 65 6e 64  ITE_TEST */.#end
b1920 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
b1930 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a  T_UTF16 */../***
b1940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
b1950 6f 66 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a  of utf.c *******
b1960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
b1990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
b19a0 6e 20 66 69 6c 65 20 75 74 69 6c 2e 63 20 2a 2a  n file util.c **
b19b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b19c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b19d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
b19e0 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
b19f0 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
b1a00 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
b1a10 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
b1a20 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
b1a30 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
b1a40 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
b1a50 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
b1a60 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
b1a70 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
b1a80 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
b1a90 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
b1aa0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
b1ab0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
b1ac0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
b1ad0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
b1ae0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
b1af0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
b1b00 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
b1b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1b50 2a 2a 0a 2a 2a 20 55 74 69 6c 69 74 79 20 66 75  **.** Utility fu
b1b60 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 68 72  nctions used thr
b1b70 6f 75 67 68 6f 75 74 20 73 71 6c 69 74 65 2e 0a  oughout sqlite..
b1b80 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
b1b90 63 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f  contains functio
b1ba0 6e 73 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6e  ns for allocatin
b1bb0 67 20 6d 65 6d 6f 72 79 2c 20 63 6f 6d 70 61 72  g memory, compar
b1bc0 69 6e 67 0a 2a 2a 20 73 74 72 69 6e 67 73 2c 20  ing.** strings, 
b1bd0 61 6e 64 20 73 74 75 66 66 20 6c 69 6b 65 20 74  and stuff like t
b1be0 68 61 74 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65  hat..**.*/.#ifde
b1bf0 66 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53  f SQLITE_HAVE_IS
b1c00 4e 41 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6d  NAN.# include <m
b1c10 61 74 68 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f  ath.h>.#endif../
b1c20 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 20 6e 65 65  *.** Routine nee
b1c30 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ded to support t
b1c40 68 65 20 74 65 73 74 63 61 73 65 28 29 20 6d 61  he testcase() ma
b1c50 63 72 6f 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  cro..*/.#ifdef S
b1c60 51 4c 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54  QLITE_COVERAGE_T
b1c70 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EST.SQLITE_PRIVA
b1c80 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
b1c90 6f 76 65 72 61 67 65 28 69 6e 74 20 78 29 7b 0a  overage(int x){.
b1ca0 20 20 73 74 61 74 69 63 20 69 6e 74 20 64 75 6d    static int dum
b1cb0 6d 79 20 3d 20 30 3b 0a 20 20 64 75 6d 6d 79 20  my = 0;.  dummy 
b1cc0 2b 3d 20 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  += x;.}.#endif..
b1cd0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
b1ce0 65 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e  e if the floatin
b1cf0 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73  g point value is
b1d00 20 4e 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e   Not a Number (N
b1d10 61 4e 29 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74  aN)..**.** Use t
b1d20 68 65 20 6d 61 74 68 20 6c 69 62 72 61 72 79 20  he math library 
b1d30 69 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e  isnan() function
b1d40 20 69 66 20 63 6f 6d 70 69 6c 65 64 20 77 69 74   if compiled wit
b1d50 68 20 53 51 4c 49 54 45 5f 48 41 56 45 5f 49 53  h SQLITE_HAVE_IS
b1d60 4e 41 4e 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  NAN..** Otherwis
b1d70 65 2c 20 77 65 20 68 61 76 65 20 6f 75 72 20 6f  e, we have our o
b1d80 77 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  wn implementatio
b1d90 6e 20 74 68 61 74 20 77 6f 72 6b 73 20 6f 6e 20  n that works on 
b1da0 6d 6f 73 74 20 73 79 73 74 65 6d 73 2e 0a 2a 2f  most systems..*/
b1db0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b1dc0 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  int sqlite3IsNaN
b1dd0 28 64 6f 75 62 6c 65 20 78 29 7b 0a 20 20 69 6e  (double x){.  in
b1de0 74 20 72 63 3b 20 20 20 2f 2a 20 54 68 65 20 76  t rc;   /* The v
b1df0 61 6c 75 65 20 72 65 74 75 72 6e 20 2a 2f 0a 23  alue return */.#
b1e00 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
b1e10 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 29 0a 20  TE_HAVE_ISNAN). 
b1e20 20 2f 2a 0a 20 20 2a 2a 20 53 79 73 74 65 6d 73   /*.  ** Systems
b1e30 20 74 68 61 74 20 73 75 70 70 6f 72 74 20 74 68   that support th
b1e40 65 20 69 73 6e 61 6e 28 29 20 6c 69 62 72 61 72  e isnan() librar
b1e50 79 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  y function shoul
b1e60 64 20 70 72 6f 62 61 62 6c 79 0a 20 20 2a 2a 20  d probably.  ** 
b1e70 6d 61 6b 65 20 75 73 65 20 6f 66 20 69 74 20 62  make use of it b
b1e80 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68  y compiling with
b1e90 20 2d 44 53 51 4c 49 54 45 5f 48 41 56 45 5f 49   -DSQLITE_HAVE_I
b1ea0 53 4e 41 4e 2e 20 20 42 75 74 20 77 65 20 68 61  SNAN.  But we ha
b1eb0 76 65 0a 20 20 2a 2a 20 66 6f 75 6e 64 20 74 68  ve.  ** found th
b1ec0 61 74 20 6d 61 6e 79 20 73 79 73 74 65 6d 73 20  at many systems 
b1ed0 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 20 77 6f  do not have a wo
b1ee0 72 6b 69 6e 67 20 69 73 6e 61 6e 28 29 20 66 75  rking isnan() fu
b1ef0 6e 63 74 69 6f 6e 20 73 6f 0a 20 20 2a 2a 20 74  nction so.  ** t
b1f00 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
b1f10 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 20 61  on is provided a
b1f20 73 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65  s an alternative
b1f30 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73  ..  **.  ** This
b1f40 20 4e 61 4e 20 74 65 73 74 20 73 6f 6d 65 74 69   NaN test someti
b1f50 6d 65 73 20 66 61 69 6c 73 20 69 66 20 63 6f 6d  mes fails if com
b1f60 70 69 6c 65 64 20 6f 6e 20 47 43 43 20 77 69 74  piled on GCC wit
b1f70 68 20 2d 66 66 61 73 74 2d 6d 61 74 68 2e 0a 20  h -ffast-math.. 
b1f80 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72   ** On the other
b1f90 20 68 61 6e 64 2c 20 74 68 65 20 75 73 65 20 6f   hand, the use o
b1fa0 66 20 2d 66 66 61 73 74 2d 6d 61 74 68 20 63 6f  f -ffast-math co
b1fb0 6d 65 73 20 77 69 74 68 20 74 68 65 20 66 6f 6c  mes with the fol
b1fc0 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 77 61 72 6e  lowing.  ** warn
b1fd0 69 6e 67 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  ing:.  **.  **  
b1fe0 20 20 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20      This option 
b1ff0 5b 2d 66 66 61 73 74 2d 6d 61 74 68 5d 20 73 68  [-ffast-math] sh
b2000 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 74 75  ould never be tu
b2010 72 6e 65 64 20 6f 6e 20 62 79 20 61 6e 79 0a 20  rned on by any. 
b2020 20 2a 2a 20 20 20 20 20 20 2d 4f 20 6f 70 74 69   **      -O opti
b2030 6f 6e 20 73 69 6e 63 65 20 69 74 20 63 61 6e 20  on since it can 
b2040 72 65 73 75 6c 74 20 69 6e 20 69 6e 63 6f 72 72  result in incorr
b2050 65 63 74 20 6f 75 74 70 75 74 20 66 6f 72 20 70  ect output for p
b2060 72 6f 67 72 61 6d 73 0a 20 20 2a 2a 20 20 20 20  rograms.  **    
b2070 20 20 77 68 69 63 68 20 64 65 70 65 6e 64 20 6f    which depend o
b2080 6e 20 61 6e 20 65 78 61 63 74 20 69 6d 70 6c 65  n an exact imple
b2090 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 49 45 45  mentation of IEE
b20a0 45 20 6f 72 20 49 53 4f 20 0a 20 20 2a 2a 20 20  E or ISO .  **  
b20b0 20 20 20 20 72 75 6c 65 73 2f 73 70 65 63 69 66      rules/specif
b20c0 69 63 61 74 69 6f 6e 73 20 66 6f 72 20 6d 61 74  ications for mat
b20d0 68 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a  h functions..  *
b20e0 2a 0a 20 20 2a 2a 20 55 6e 64 65 72 20 4d 53 56  *.  ** Under MSV
b20f0 43 2c 20 74 68 69 73 20 4e 61 4e 20 74 65 73 74  C, this NaN test
b2100 20 6d 61 79 20 66 61 69 6c 20 69 66 20 63 6f 6d   may fail if com
b2110 70 69 6c 65 64 20 77 69 74 68 20 61 20 66 6c 6f  piled with a flo
b2120 61 74 69 6e 67 2d 0a 20 20 2a 2a 20 70 6f 69 6e  ating-.  ** poin
b2130 74 20 70 72 65 63 69 73 69 6f 6e 20 6d 6f 64 65  t precision mode
b2140 20 6f 74 68 65 72 20 74 68 61 6e 20 2f 66 70 3a   other than /fp:
b2150 70 72 65 63 69 73 65 2e 20 20 46 72 6f 6d 20 74  precise.  From t
b2160 68 65 20 4d 53 44 4e 20 0a 20 20 2a 2a 20 64 6f  he MSDN .  ** do
b2170 63 75 6d 65 6e 74 61 74 69 6f 6e 3a 0a 20 20 2a  cumentation:.  *
b2180 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68 65 20  *.  **      The 
b2190 63 6f 6d 70 69 6c 65 72 20 5b 77 69 74 68 20 2f  compiler [with /
b21a0 66 70 3a 70 72 65 63 69 73 65 5d 20 77 69 6c 6c  fp:precise] will
b21b0 20 70 72 6f 70 65 72 6c 79 20 68 61 6e 64 6c 65   properly handle
b21c0 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 0a 20 20   comparisons .  
b21d0 2a 2a 20 20 20 20 20 20 69 6e 76 6f 6c 76 69 6e  **      involvin
b21e0 67 20 4e 61 4e 2e 20 46 6f 72 20 65 78 61 6d 70  g NaN. For examp
b21f0 6c 65 2c 20 78 20 21 3d 20 78 20 65 76 61 6c 75  le, x != x evalu
b2200 61 74 65 73 20 74 6f 20 74 72 75 65 20 69 66 20  ates to true if 
b2210 78 20 69 73 20 4e 61 4e 20 0a 20 20 2a 2a 20 20  x is NaN .  **  
b2220 20 20 20 20 2e 2e 2e 0a 20 20 2a 2f 0a 23 69 66      ....  */.#if
b2230 64 65 66 20 5f 5f 46 41 53 54 5f 4d 41 54 48 5f  def __FAST_MATH_
b2240 5f 0a 23 20 65 72 72 6f 72 20 53 51 4c 69 74 65  _.# error SQLite
b2250 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63   will not work c
b2260 6f 72 72 65 63 74 6c 79 20 77 69 74 68 20 74 68  orrectly with th
b2270 65 20 2d 66 66 61 73 74 2d 6d 61 74 68 20 6f 70  e -ffast-math op
b2280 74 69 6f 6e 20 6f 66 20 47 43 43 2e 0a 23 65 6e  tion of GCC..#en
b2290 64 69 66 0a 20 20 76 6f 6c 61 74 69 6c 65 20 64  dif.  volatile d
b22a0 6f 75 62 6c 65 20 79 20 3d 20 78 3b 0a 20 20 76  ouble y = x;.  v
b22b0 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20 7a  olatile double z
b22c0 20 3d 20 79 3b 0a 20 20 72 63 20 3d 20 28 79 21   = y;.  rc = (y!
b22d0 3d 7a 29 3b 0a 23 65 6c 73 65 20 20 2f 2a 20 69  =z);.#else  /* i
b22e0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
b22f0 5f 48 41 56 45 5f 49 53 4e 41 4e 29 20 2a 2f 0a  _HAVE_ISNAN) */.
b2300 20 20 72 63 20 3d 20 69 73 6e 61 6e 28 78 29 3b    rc = isnan(x);
b2310 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
b2320 45 5f 48 41 56 45 5f 49 53 4e 41 4e 20 2a 2f 0a  E_HAVE_ISNAN */.
b2330 20 20 74 65 73 74 63 61 73 65 28 20 72 63 20 29    testcase( rc )
b2340 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
b2350 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20  ../*.** Compute 
b2360 61 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68 20  a string length 
b2370 74 68 61 74 20 69 73 20 6c 69 6d 69 74 65 64 20  that is limited 
b2380 74 6f 20 77 68 61 74 20 63 61 6e 20 62 65 20 73  to what can be s
b2390 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 6c 6f 77 65  tored in.** lowe
b23a0 72 20 33 30 20 62 69 74 73 20 6f 66 20 61 20 33  r 30 bits of a 3
b23b0 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74  2-bit signed int
b23c0 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eger..**.** The 
b23d0 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 77  value returned w
b23e0 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 6e 65 67  ill never be neg
b23f0 61 74 69 76 65 2e 20 20 4e 6f 72 20 77 69 6c 6c  ative.  Nor will
b2400 20 69 74 20 65 76 65 72 20 62 65 20 67 72 65 61   it ever be grea
b2410 74 65 72 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20  ter.** than the 
b2420 61 63 74 75 61 6c 20 6c 65 6e 67 74 68 20 6f 66  actual length of
b2430 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20 46 6f   the string.  Fo
b2440 72 20 76 65 72 79 20 6c 6f 6e 67 20 73 74 72 69  r very long stri
b2450 6e 67 73 20 28 67 72 65 61 74 65 72 0a 2a 2a 20  ngs (greater.** 
b2460 74 68 61 6e 20 31 47 69 42 29 20 74 68 65 20 76  than 1GiB) the v
b2470 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 6d 69  alue returned mi
b2480 67 68 74 20 62 65 20 6c 65 73 73 20 74 68 61 6e  ght be less than
b2490 20 74 68 65 20 74 72 75 65 20 73 74 72 69 6e 67   the true string
b24a0 20 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 53 51 4c 49   length..*/.SQLI
b24b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b24c0 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 63  qlite3Strlen30(c
b24d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
b24e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20   const char *z2 
b24f0 3d 20 7a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20  = z;.  if( z==0 
b2500 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
b2510 69 6c 65 28 20 2a 7a 32 20 29 7b 20 7a 32 2b 2b  ile( *z2 ){ z2++
b2520 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 78 33  ; }.  return 0x3
b2530 66 66 66 66 66 66 66 20 26 20 28 69 6e 74 29 28  fffffff & (int)(
b2540 7a 32 20 2d 20 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  z2 - z);.}../*.*
b2550 2a 20 53 65 74 20 74 68 65 20 6d 6f 73 74 20 72  * Set the most r
b2560 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 65  ecent error code
b2570 20 61 6e 64 20 65 72 72 6f 72 20 73 74 72 69 6e   and error strin
b2580 67 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65  g for the sqlite
b2590 0a 2a 2a 20 68 61 6e 64 6c 65 20 22 64 62 22 2e  .** handle "db".
b25a0 20 54 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   The error code 
b25b0 69 73 20 73 65 74 20 74 6f 20 22 65 72 72 5f 63  is set to "err_c
b25c0 6f 64 65 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  ode"..**.** If i
b25d0 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73  t is not NULL, s
b25e0 74 72 69 6e 67 20 7a 46 6f 72 6d 61 74 20 73 70  tring zFormat sp
b25f0 65 63 69 66 69 65 73 20 74 68 65 20 66 6f 72 6d  ecifies the form
b2600 61 74 20 6f 66 20 74 68 65 0a 2a 2a 20 65 72 72  at of the.** err
b2610 6f 72 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65  or string in the
b2620 20 73 74 79 6c 65 20 6f 66 20 74 68 65 20 70 72   style of the pr
b2630 69 6e 74 66 20 66 75 6e 63 74 69 6f 6e 73 3a 20  intf functions: 
b2640 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  The following.**
b2650 20 66 6f 72 6d 61 74 20 63 68 61 72 61 63 74 65   format characte
b2660 72 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a  rs are allowed:.
b2670 2a 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 20 20  **.**      %s   
b2680 20 20 20 49 6e 73 65 72 74 20 61 20 73 74 72 69     Insert a stri
b2690 6e 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20 20  ng.**      %z   
b26a0 20 20 20 41 20 73 74 72 69 6e 67 20 74 68 61 74     A string that
b26b0 20 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64   should be freed
b26c0 20 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 20 20   after use.**   
b26d0 20 20 20 25 64 20 20 20 20 20 20 49 6e 73 65 72     %d      Inser
b26e0 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20  t an integer.** 
b26f0 20 20 20 20 20 25 54 20 20 20 20 20 20 49 6e 73       %T      Ins
b2700 65 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20  ert a token.**  
b2710 20 20 20 20 25 53 20 20 20 20 20 20 49 6e 73 65      %S      Inse
b2720 72 74 20 74 68 65 20 66 69 72 73 74 20 65 6c 65  rt the first ele
b2730 6d 65 6e 74 20 6f 66 20 61 20 53 72 63 4c 69 73  ment of a SrcLis
b2740 74 0a 2a 2a 0a 2a 2a 20 7a 46 6f 72 6d 61 74 20  t.**.** zFormat 
b2750 61 6e 64 20 61 6e 79 20 73 74 72 69 6e 67 20 74  and any string t
b2760 6f 6b 65 6e 73 20 74 68 61 74 20 66 6f 6c 6c 6f  okens that follo
b2770 77 20 69 74 20 61 72 65 20 61 73 73 75 6d 65 64  w it are assumed
b2780 20 74 6f 20 62 65 0a 2a 2a 20 65 6e 63 6f 64 65   to be.** encode
b2790 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a  d in UTF-8..**.*
b27a0 2a 20 54 6f 20 63 6c 65 61 72 20 74 68 65 20 6d  * To clear the m
b27b0 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f 72  ost recent error
b27c0 20 66 6f 72 20 73 71 6c 69 74 65 20 68 61 6e 64   for sqlite hand
b27d0 6c 65 20 22 64 62 22 2c 20 73 71 6c 69 74 65 33  le "db", sqlite3
b27e0 45 72 72 6f 72 0a 2a 2a 20 73 68 6f 75 6c 64 20  Error.** should 
b27f0 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20 65  be called with e
b2800 72 72 5f 63 6f 64 65 20 73 65 74 20 74 6f 20 53  rr_code set to S
b2810 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 7a 46 6f  QLITE_OK and zFo
b2820 72 6d 61 74 20 73 65 74 0a 2a 2a 20 74 6f 20 4e  rmat set.** to N
b2830 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ULL..*/.SQLITE_P
b2840 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
b2850 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33  te3Error(sqlite3
b2860 20 2a 64 62 2c 20 69 6e 74 20 65 72 72 5f 63 6f   *db, int err_co
b2870 64 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  de, const char *
b2880 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
b2890 20 69 66 28 20 64 62 20 26 26 20 28 64 62 2d 3e   if( db && (db->
b28a0 70 45 72 72 20 7c 7c 20 28 64 62 2d 3e 70 45 72  pErr || (db->pEr
b28b0 72 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  r = sqlite3Value
b28c0 4e 65 77 28 64 62 29 29 21 3d 30 29 20 29 7b 0a  New(db))!=0) ){.
b28d0 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20      db->errCode 
b28e0 3d 20 65 72 72 5f 63 6f 64 65 3b 0a 20 20 20 20  = err_code;.    
b28f0 69 66 28 20 7a 46 6f 72 6d 61 74 20 29 7b 0a 20  if( zFormat ){. 
b2900 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20       char *z;.  
b2910 20 20 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a      va_list ap;.
b2920 20 20 20 20 20 20 76 61 5f 73 74 61 72 74 28 61        va_start(a
b2930 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20  p, zFormat);.   
b2940 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d     z = sqlite3VM
b2950 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d  Printf(db, zForm
b2960 61 74 2c 20 61 70 29 3b 0a 20 20 20 20 20 20 76  at, ap);.      v
b2970 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 20 20 20  a_end(ap);.     
b2980 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
b2990 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31  Str(db->pErr, -1
b29a0 2c 20 7a 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  , z, SQLITE_UTF8
b29b0 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  , SQLITE_DYNAMIC
b29c0 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
b29d0 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
b29e0 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c  SetStr(db->pErr,
b29f0 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f 55 54   0, 0, SQLITE_UT
b2a00 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  F8, SQLITE_STATI
b2a10 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  C);.    }.  }.}.
b2a20 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 65 72  ./*.** Add an er
b2a30 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70  ror message to p
b2a40 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61  Parse->zErrMsg a
b2a50 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 70 50 61  nd increment pPa
b2a60 72 73 65 2d 3e 6e 45 72 72 2e 0a 2a 2a 20 54 68  rse->nErr..** Th
b2a70 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d  e following form
b2a80 61 74 74 69 6e 67 20 63 68 61 72 61 63 74 65 72  atting character
b2a90 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a  s are allowed:.*
b2aa0 2a 0a 2a 2a 20 20 20 20 20 20 25 73 20 20 20 20  *.**      %s    
b2ab0 20 20 49 6e 73 65 72 74 20 61 20 73 74 72 69 6e    Insert a strin
b2ac0 67 0a 2a 2a 20 20 20 20 20 20 25 7a 20 20 20 20  g.**      %z    
b2ad0 20 20 41 20 73 74 72 69 6e 67 20 74 68 61 74 20    A string that 
b2ae0 73 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 20  should be freed 
b2af0 61 66 74 65 72 20 75 73 65 0a 2a 2a 20 20 20 20  after use.**    
b2b00 20 20 25 64 20 20 20 20 20 20 49 6e 73 65 72 74    %d      Insert
b2b10 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20   an integer.**  
b2b20 20 20 20 20 25 54 20 20 20 20 20 20 49 6e 73 65      %T      Inse
b2b30 72 74 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20  rt a token.**   
b2b40 20 20 20 25 53 20 20 20 20 20 20 49 6e 73 65 72     %S      Inser
b2b50 74 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d  t the first elem
b2b60 65 6e 74 20 6f 66 20 61 20 53 72 63 4c 69 73 74  ent of a SrcList
b2b70 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
b2b80 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 75  tion should be u
b2b90 73 65 64 20 74 6f 20 72 65 70 6f 72 74 20 61 6e  sed to report an
b2ba0 79 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63  y error that occ
b2bb0 75 72 73 20 77 68 69 6c 73 74 0a 2a 2a 20 63 6f  urs whilst.** co
b2bc0 6d 70 69 6c 69 6e 67 20 61 6e 20 53 51 4c 20 73  mpiling an SQL s
b2bd0 74 61 74 65 6d 65 6e 74 20 28 69 2e 65 2e 20 77  tatement (i.e. w
b2be0 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 70 72  ithin sqlite3_pr
b2bf0 65 70 61 72 65 28 29 29 2e 20 54 68 65 0a 2a 2a  epare()). The.**
b2c00 20 6c 61 73 74 20 74 68 69 6e 67 20 74 68 65 20   last thing the 
b2c10 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
b2c20 29 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20  ) function does 
b2c30 69 73 20 63 6f 70 79 20 74 68 65 20 65 72 72 6f  is copy the erro
b2c40 72 0a 2a 2a 20 73 74 6f 72 65 64 20 62 79 20 74  r.** stored by t
b2c50 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74  his function int
b2c60 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  o the database h
b2c70 61 6e 64 6c 65 20 75 73 69 6e 67 20 73 71 6c 69  andle using sqli
b2c80 74 65 33 45 72 72 6f 72 28 29 2e 0a 2a 2a 20 46  te3Error()..** F
b2c90 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 45  unction sqlite3E
b2ca0 72 72 6f 72 28 29 20 73 68 6f 75 6c 64 20 62 65  rror() should be
b2cb0 20 75 73 65 64 20 64 75 72 69 6e 67 20 73 74 61   used during sta
b2cc0 74 65 6d 65 6e 74 20 65 78 65 63 75 74 69 6f 6e  tement execution
b2cd0 0a 2a 2a 20 28 73 71 6c 69 74 65 33 5f 73 74 65  .** (sqlite3_ste
b2ce0 70 28 29 20 65 74 63 2e 29 2e 0a 2a 2f 0a 53 51  p() etc.)..*/.SQ
b2cf0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b2d00 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73  d sqlite3ErrorMs
b2d10 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  g(Parse *pParse,
b2d20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
b2d30 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
b2d40 5f 6c 69 73 74 20 61 70 3b 0a 20 20 73 71 6c 69  _list ap;.  sqli
b2d50 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
b2d60 2d 3e 64 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e  ->db;.  pParse->
b2d70 6e 45 72 72 2b 2b 3b 0a 20 20 73 71 6c 69 74 65  nErr++;.  sqlite
b2d80 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72  3DbFree(db, pPar
b2d90 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  se->zErrMsg);.  
b2da0 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
b2db0 72 6d 61 74 29 3b 0a 20 20 70 50 61 72 73 65 2d  rmat);.  pParse-
b2dc0 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  >zErrMsg = sqlit
b2dd0 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a  e3VMPrintf(db, z
b2de0 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76  Format, ap);.  v
b2df0 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 70 50 61  a_end(ap);.  pPa
b2e00 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
b2e10 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _ERROR;.}../*.**
b2e20 20 43 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72   Clear the error
b2e30 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72   message in pPar
b2e40 73 65 2c 20 69 66 20 61 6e 79 0a 2a 2f 0a 53 51  se, if any.*/.SQ
b2e50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b2e60 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c  d sqlite3ErrorCl
b2e70 65 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  ear(Parse *pPars
b2e80 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  e){.  sqlite3DbF
b2e90 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ree(pParse->db, 
b2ea0 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29  pParse->zErrMsg)
b2eb0 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72  ;.  pParse->zErr
b2ec0 4d 73 67 20 3d 20 30 3b 0a 20 20 70 50 61 72 73  Msg = 0;.  pPars
b2ed0 65 2d 3e 6e 45 72 72 20 3d 20 30 3b 0a 7d 0a 0a  e->nErr = 0;.}..
b2ee0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 6e  /*.** Convert an
b2ef0 20 53 51 4c 2d 73 74 79 6c 65 20 71 75 6f 74 65   SQL-style quote
b2f00 64 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20  d string into a 
b2f10 6e 6f 72 6d 61 6c 20 73 74 72 69 6e 67 20 62 79  normal string by
b2f20 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74 68 65   removing.** the
b2f30 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72   quote character
b2f40 73 2e 20 20 54 68 65 20 63 6f 6e 76 65 72 73 69  s.  The conversi
b2f50 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d 70 6c  on is done in-pl
b2f60 61 63 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  ace.  If the.** 
b2f70 69 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74 20 62  input does not b
b2f80 65 67 69 6e 20 77 69 74 68 20 61 20 71 75 6f 74  egin with a quot
b2f90 65 20 63 68 61 72 61 63 74 65 72 2c 20 74 68 65  e character, the
b2fa0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  n this routine.*
b2fb0 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  * is a no-op..**
b2fc0 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 73 74  .** The input st
b2fd0 72 69 6e 67 20 6d 75 73 74 20 62 65 20 7a 65 72  ring must be zer
b2fe0 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 41  o-terminated.  A
b2ff0 20 6e 65 77 20 7a 65 72 6f 2d 74 65 72 6d 69 6e   new zero-termin
b3000 61 74 6f 72 0a 2a 2a 20 69 73 20 61 64 64 65 64  ator.** is added
b3010 20 74 6f 20 74 68 65 20 64 65 71 75 6f 74 65 64   to the dequoted
b3020 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54   string..**.** T
b3030 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
b3040 69 73 20 2d 31 20 69 66 20 6e 6f 20 64 65 71 75  is -1 if no dequ
b3050 6f 74 69 6e 67 20 6f 63 63 75 72 73 20 6f 72 20  oting occurs or 
b3060 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
b3070 65 0a 2a 2a 20 64 65 71 75 6f 74 65 64 20 73 74  e.** dequoted st
b3080 72 69 6e 67 2c 20 65 78 63 6c 75 73 69 76 65 20  ring, exclusive 
b3090 6f 66 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d  of the zero term
b30a0 69 6e 61 74 6f 72 2c 20 69 66 20 64 65 71 75 6f  inator, if dequo
b30b0 74 69 6e 67 20 64 6f 65 73 0a 2a 2a 20 6f 63 63  ting does.** occ
b30c0 75 72 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46  ur..**.** 2002-F
b30d0 65 62 2d 31 34 3a 20 54 68 69 73 20 72 6f 75 74  eb-14: This rout
b30e0 69 6e 65 20 69 73 20 65 78 74 65 6e 64 65 64 20  ine is extended 
b30f0 74 6f 20 72 65 6d 6f 76 65 20 4d 53 2d 41 63 63  to remove MS-Acc
b3100 65 73 73 20 73 74 79 6c 65 0a 2a 2a 20 62 72 61  ess style.** bra
b3110 63 6b 65 74 73 20 66 72 6f 6d 20 61 72 6f 75 6e  ckets from aroun
b3120 64 20 69 64 65 6e 74 69 66 65 72 73 2e 20 20 46  d identifers.  F
b3130 6f 72 20 65 78 61 6d 70 6c 65 3a 20 20 22 5b 61  or example:  "[a
b3140 2d 62 2d 63 5d 22 20 62 65 63 6f 6d 65 73 0a 2a  -b-c]" becomes.*
b3150 2a 20 22 61 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51  * "a-b-c"..*/.SQ
b3160 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b3170 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28   sqlite3Dequote(
b3180 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72  char *z){.  char
b3190 20 71 75 6f 74 65 3b 0a 20 20 69 6e 74 20 69 2c   quote;.  int i,
b31a0 20 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29   j;.  if( z==0 )
b31b0 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 71 75   return -1;.  qu
b31c0 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77  ote = z[0];.  sw
b31d0 69 74 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20  itch( quote ){. 
b31e0 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62     case '\'':  b
b31f0 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27  reak;.    case '
b3200 22 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  "':   break;.   
b3210 20 63 61 73 65 20 27 60 27 3a 20 20 20 62 72 65   case '`':   bre
b3220 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ak;             
b3230 20 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20     /* For MySQL 
b3240 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f  compatibility */
b3250 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20  .    case '[':  
b3260 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62   quote = ']';  b
b3270 72 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53  reak;  /* For MS
b3280 20 53 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61   SqlServer compa
b3290 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20  tibility */.    
b32a0 64 65 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75  default:    retu
b32b0 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 66 6f 72  rn -1;.  }.  for
b32c0 28 69 3d 31 2c 20 6a 3d 30 3b 20 41 4c 57 41 59  (i=1, j=0; ALWAY
b32d0 53 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 0a 20  S(z[i]); i++){. 
b32e0 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f     if( z[i]==quo
b32f0 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  te ){.      if( 
b3300 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b  z[i+1]==quote ){
b3310 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20  .        z[j++] 
b3320 3d 20 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20  = quote;.       
b3330 20 69 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73   i++;.      }els
b3340 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  e{.        break
b3350 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
b3360 6c 73 65 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b  lse{.      z[j++
b3370 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a  ] = z[i];.    }.
b3380 20 20 7d 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a    }.  z[j] = 0;.
b3390 20 20 72 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f    return j;.}../
b33a0 2a 20 43 6f 6e 76 65 6e 69 65 6e 74 20 73 68 6f  * Convenient sho
b33b0 72 74 2d 68 61 6e 64 20 2a 2f 0a 23 64 65 66 69  rt-hand */.#defi
b33c0 6e 65 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 20  ne UpperToLower 
b33d0 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
b33e0 77 65 72 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20  wer../*.** Some 
b33f0 73 79 73 74 65 6d 73 20 68 61 76 65 20 73 74 72  systems have str
b3400 69 63 6d 70 28 29 2e 20 20 4f 74 68 65 72 73 20  icmp().  Others 
b3410 68 61 76 65 20 73 74 72 63 61 73 65 63 6d 70 28  have strcasecmp(
b3420 29 2e 20 20 42 65 63 61 75 73 65 0a 2a 2a 20 74  ).  Because.** t
b3430 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 69  here is no consi
b3440 73 74 65 6e 63 79 2c 20 77 65 20 77 69 6c 6c 20  stency, we will 
b3450 64 65 66 69 6e 65 20 6f 75 72 20 6f 77 6e 2e 0a  define our own..
b3460 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b3470 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 72  E int sqlite3Str
b3480 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72 20  ICmp(const char 
b3490 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74 20 63 68  *zLeft, const ch
b34a0 61 72 20 2a 7a 52 69 67 68 74 29 7b 0a 20 20 72  ar *zRight){.  r
b34b0 65 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64  egister unsigned
b34c0 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20   char *a, *b;.  
b34d0 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  a = (unsigned ch
b34e0 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20  ar *)zLeft;.  b 
b34f0 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
b3500 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69   *)zRight;.  whi
b3510 6c 65 28 20 2a 61 21 3d 30 20 26 26 20 55 70 70  le( *a!=0 && Upp
b3520 65 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 3d 3d 55  erToLower[*a]==U
b3530 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 29  pperToLower[*b])
b3540 7b 20 61 2b 2b 3b 20 62 2b 2b 3b 20 7d 0a 20 20  { a++; b++; }.  
b3550 72 65 74 75 72 6e 20 55 70 70 65 72 54 6f 4c 6f  return UpperToLo
b3560 77 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54  wer[*a] - UpperT
b3570 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 53 51  oLower[*b];.}.SQ
b3580 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
b3590 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 63 6f  ite3_strnicmp(co
b35a0 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c  nst char *zLeft,
b35b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 69   const char *zRi
b35c0 67 68 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  ght, int N){.  r
b35d0 65 67 69 73 74 65 72 20 75 6e 73 69 67 6e 65 64  egister unsigned
b35e0 20 63 68 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20   char *a, *b;.  
b35f0 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  a = (unsigned ch
b3600 61 72 20 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20  ar *)zLeft;.  b 
b3610 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
b3620 20 2a 29 7a 52 69 67 68 74 3b 0a 20 20 77 68 69   *)zRight;.  whi
b3630 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26 20 2a  le( N-- > 0 && *
b3640 61 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c  a!=0 && UpperToL
b3650 6f 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54  ower[*a]==UpperT
b3660 6f 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b  oLower[*b]){ a++
b3670 3b 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72  ; b++; }.  retur
b3680 6e 20 4e 3c 30 20 3f 20 30 20 3a 20 55 70 70 65  n N<0 ? 0 : Uppe
b3690 72 54 6f 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55  rToLower[*a] - U
b36a0 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b  pperToLower[*b];
b36b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
b36c0 20 54 52 55 45 20 69 66 20 7a 20 69 73 20 61 20   TRUE if z is a 
b36d0 70 75 72 65 20 6e 75 6d 65 72 69 63 20 73 74 72  pure numeric str
b36e0 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 46 41 4c  ing.  Return FAL
b36f0 53 45 20 61 6e 64 20 6c 65 61 76 65 0a 2a 2a 20  SE and leave.** 
b3700 2a 72 65 61 6c 6e 75 6d 20 75 6e 63 68 61 6e 67  *realnum unchang
b3710 65 64 20 69 66 20 74 68 65 20 73 74 72 69 6e 67  ed if the string
b3720 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 63 68   contains any ch
b3730 61 72 61 63 74 65 72 20 77 68 69 63 68 20 69 73  aracter which is
b3740 20 6e 6f 74 0a 2a 2a 20 70 61 72 74 20 6f 66 20   not.** part of 
b3750 61 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20  a number..**.** 
b3760 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73  If the string is
b3770 20 70 75 72 65 20 6e 75 6d 65 72 69 63 2c 20 73   pure numeric, s
b3780 65 74 20 2a 72 65 61 6c 6e 75 6d 20 74 6f 20 54  et *realnum to T
b3790 52 55 45 20 69 66 20 74 68 65 20 73 74 72 69 6e  RUE if the strin
b37a0 67 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68  g.** contains th
b37b0 65 20 27 2e 27 20 63 68 61 72 61 63 74 65 72 20  e '.' character 
b37c0 6f 72 20 61 6e 20 22 45 2b 30 30 30 22 20 73 74  or an "E+000" st
b37d0 79 6c 65 20 65 78 70 6f 6e 65 6e 74 69 61 74 69  yle exponentiati
b37e0 6f 6e 20 73 75 66 66 69 78 2e 0a 2a 2a 20 4f 74  on suffix..** Ot
b37f0 68 65 72 77 69 73 65 20 73 65 74 20 2a 72 65 61  herwise set *rea
b3800 6c 6e 75 6d 20 74 6f 20 46 41 4c 53 45 2e 20 20  lnum to FALSE.  
b3810 4e 6f 74 65 20 74 68 61 74 20 6a 75 73 74 20 62  Note that just b
b3820 65 63 61 75 65 20 2a 72 65 61 6c 6e 75 6d 20 69  ecaue *realnum i
b3830 73 0a 2a 2a 20 66 61 6c 73 65 20 64 6f 65 73 20  s.** false does 
b3840 6e 6f 74 20 6d 65 61 6e 20 74 68 61 74 20 74 68  not mean that th
b3850 65 20 6e 75 6d 62 65 72 20 63 61 6e 20 62 65 20  e number can be 
b3860 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e  successfully con
b3870 76 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 61  verted into.** a
b3880 6e 20 69 6e 74 65 67 65 72 20 2d 20 69 74 20 6d  n integer - it m
b3890 69 67 68 74 20 62 65 20 74 6f 6f 20 62 69 67 2e  ight be too big.
b38a0 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 6d 70 74 79 20  .**.** An empty 
b38b0 73 74 72 69 6e 67 20 69 73 20 63 6f 6e 73 69 64  string is consid
b38c0 65 72 65 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63  ered non-numeric
b38d0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b38e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
b38f0 73 4e 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68  sNumber(const ch
b3900 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 72 65 61 6c  ar *z, int *real
b3910 6e 75 6d 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20  num, u8 enc){.  
b3920 69 6e 74 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d  int incr = (enc=
b3930 3d 53 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32  =SQLITE_UTF8?1:2
b3940 29 3b 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51  );.  if( enc==SQ
b3950 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 20 7a  LITE_UTF16BE ) z
b3960 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d  ++;.  if( *z=='-
b3970 27 20 7c 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20 7a  ' || *z=='+' ) z
b3980 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 69 66 28 20   += incr;.  if( 
b3990 21 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28  !sqlite3Isdigit(
b39a0 2a 7a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  *z) ){.    retur
b39b0 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20 2b 3d 20  n 0;.  }.  z += 
b39c0 69 6e 63 72 3b 0a 20 20 2a 72 65 61 6c 6e 75 6d  incr;.  *realnum
b39d0 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 73   = 0;.  while( s
b39e0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
b39f0 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20  ) ){ z += incr; 
b3a00 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20  }.  if( *z=='.' 
b3a10 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63 72  ){.    z += incr
b3a20 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74  ;.    if( !sqlit
b3a30 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20  e3Isdigit(*z) ) 
b3a40 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68  return 0;.    wh
b3a50 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69  ile( sqlite3Isdi
b3a60 67 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20  git(*z) ){ z += 
b3a70 69 6e 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61  incr; }.    *rea
b3a80 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  lnum = 1;.  }.  
b3a90 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a  if( *z=='e' || *
b3aa0 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a 20  z=='E' ){.    z 
b3ab0 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28  += incr;.    if(
b3ac0 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d 3d   *z=='+' || *z==
b3ad0 27 2d 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72 3b  '-' ) z += incr;
b3ae0 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
b3af0 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 20 72  3Isdigit(*z) ) r
b3b00 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69  eturn 0;.    whi
b3b10 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67  le( sqlite3Isdig
b3b20 69 74 28 2a 7a 29 20 29 7b 20 7a 20 2b 3d 20 69  it(*z) ){ z += i
b3b30 6e 63 72 3b 20 7d 0a 20 20 20 20 2a 72 65 61 6c  ncr; }.    *real
b3b40 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  num = 1;.  }.  r
b3b50 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a  eturn *z==0;.}..
b3b60 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67  /*.** The string
b3b70 20 7a 5b 5d 20 69 73 20 61 6e 20 41 53 43 49 49   z[] is an ASCII
b3b80 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
b3b90 6f 66 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72  of a real number
b3ba0 2e 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 69  ..** Convert thi
b3bb0 73 20 73 74 72 69 6e 67 20 74 6f 20 61 20 64 6f  s string to a do
b3bc0 75 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  uble..**.** This
b3bd0 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d 65 73   routine assumes
b3be0 20 74 68 61 74 20 7a 5b 5d 20 72 65 61 6c 6c 79   that z[] really
b3bf0 20 69 73 20 61 20 76 61 6c 69 64 20 6e 75 6d 62   is a valid numb
b3c00 65 72 2e 20 20 49 66 20 69 74 0a 2a 2a 20 69 73  er.  If it.** is
b3c10 20 6e 6f 74 2c 20 74 68 65 20 72 65 73 75 6c 74   not, the result
b3c20 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
b3c30 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
b3c40 65 20 69 73 20 75 73 65 64 20 69 6e 73 74 65 61  e is used instea
b3c50 64 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79  d of the library
b3c60 20 61 74 6f 66 28 29 20 66 75 6e 63 74 69 6f 6e   atof() function
b3c70 20 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20   because.** the 
b3c80 6c 69 62 72 61 72 79 20 61 74 6f 66 28 29 20 6d  library atof() m
b3c90 69 67 68 74 20 77 61 6e 74 20 74 6f 20 75 73 65  ight want to use
b3ca0 20 22 2c 22 20 61 73 20 74 68 65 20 64 65 63 69   "," as the deci
b3cb0 6d 61 6c 20 70 6f 69 6e 74 20 69 6e 73 74 65 61  mal point instea
b3cc0 64 0a 2a 2a 20 6f 66 20 22 2e 22 20 64 65 70 65  d.** of "." depe
b3cd0 6e 64 69 6e 67 20 6f 6e 20 68 6f 77 20 6c 6f 63  nding on how loc
b3ce0 61 6c 65 20 69 73 20 73 65 74 2e 20 20 42 75 74  ale is set.  But
b3cf0 20 74 68 61 74 20 77 6f 75 6c 64 20 63 61 75 73   that would caus
b3d00 65 20 70 72 6f 62 6c 65 6d 73 0a 2a 2a 20 66 6f  e problems.** fo
b3d10 72 20 53 51 4c 2e 20 20 53 6f 20 74 68 69 73 20  r SQL.  So this 
b3d20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20 75  routine always u
b3d30 73 65 73 20 22 2e 22 20 72 65 67 61 72 64 6c 65  ses "." regardle
b3d40 73 73 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f  ss of locale..*/
b3d50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b3d60 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f 46 28  int sqlite3AtoF(
b3d70 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 64  const char *z, d
b3d80 6f 75 62 6c 65 20 2a 70 52 65 73 75 6c 74 29 7b  ouble *pResult){
b3d90 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
b3da0 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
b3db0 49 4e 54 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  INT.  const char
b3dc0 20 2a 7a 42 65 67 69 6e 20 3d 20 7a 3b 0a 20 20   *zBegin = z;.  
b3dd0 2f 2a 20 73 69 67 6e 20 2a 20 73 69 67 6e 69 66  /* sign * signif
b3de0 69 63 61 6e 64 20 2a 20 28 31 30 20 5e 20 28 65  icand * (10 ^ (e
b3df0 73 69 67 6e 20 2a 20 65 78 70 6f 6e 65 6e 74 29  sign * exponent)
b3e00 29 20 2a 2f 0a 20 20 69 6e 74 20 73 69 67 6e 20  ) */.  int sign 
b3e10 3d 20 31 3b 20 20 20 2f 2a 20 73 69 67 6e 20 6f  = 1;   /* sign o
b3e20 66 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f  f significand */
b3e30 0a 20 20 69 36 34 20 73 20 3d 20 30 3b 20 20 20  .  i64 s = 0;   
b3e40 20 20 20 2f 2a 20 73 69 67 6e 69 66 69 63 61 6e     /* significan
b3e50 64 20 2a 2f 0a 20 20 69 6e 74 20 64 20 3d 20 30  d */.  int d = 0
b3e60 3b 20 20 20 20 20 20 2f 2a 20 61 64 6a 75 73 74  ;      /* adjust
b3e70 20 65 78 70 6f 6e 65 6e 74 20 66 6f 72 20 73 68   exponent for sh
b3e80 69 66 74 69 6e 67 20 64 65 63 69 6d 61 6c 20 70  ifting decimal p
b3e90 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 65 73  oint */.  int es
b3ea0 69 67 6e 20 3d 20 31 3b 20 20 2f 2a 20 73 69 67  ign = 1;  /* sig
b3eb0 6e 20 6f 66 20 65 78 70 6f 6e 65 6e 74 20 2a 2f  n of exponent */
b3ec0 0a 20 20 69 6e 74 20 65 20 3d 20 30 3b 20 20 20  .  int e = 0;   
b3ed0 20 20 20 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 2a     /* exponent *
b3ee0 2f 0a 20 20 64 6f 75 62 6c 65 20 72 65 73 75 6c  /.  double resul
b3ef0 74 3b 0a 20 20 69 6e 74 20 6e 44 69 67 69 74 73  t;.  int nDigits
b3f00 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 73 6b 69 70   = 0;..  /* skip
b3f10 20 6c 65 61 64 69 6e 67 20 73 70 61 63 65 73 20   leading spaces 
b3f20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69  */.  while( sqli
b3f30 74 65 33 49 73 73 70 61 63 65 28 2a 7a 29 20 29  te3Isspace(*z) )
b3f40 20 7a 2b 2b 3b 0a 20 20 2f 2a 20 67 65 74 20 73   z++;.  /* get s
b3f50 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69 63 61  ign of significa
b3f60 6e 64 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d  nd */.  if( *z==
b3f70 27 2d 27 20 29 7b 0a 20 20 20 20 73 69 67 6e 20  '-' ){.    sign 
b3f80 3d 20 2d 31 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  = -1;.    z++;. 
b3f90 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27   }else if( *z=='
b3fa0 2b 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  +' ){.    z++;. 
b3fb0 20 7d 0a 20 20 2f 2a 20 73 6b 69 70 20 6c 65 61   }.  /* skip lea
b3fc0 64 69 6e 67 20 7a 65 72 6f 65 73 20 2a 2f 0a 20  ding zeroes */. 
b3fd0 20 77 68 69 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30   while( z[0]=='0
b3fe0 27 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73  ' ) z++, nDigits
b3ff0 2b 2b 3b 0a 0a 20 20 2f 2a 20 63 6f 70 79 20 6d  ++;..  /* copy m
b4000 61 78 20 73 69 67 6e 69 66 69 63 61 6e 74 20 64  ax significant d
b4010 69 67 69 74 73 20 74 6f 20 73 69 67 6e 69 66 69  igits to signifi
b4020 63 61 6e 64 20 2a 2f 0a 20 20 77 68 69 6c 65 28  cand */.  while(
b4030 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
b4040 2a 7a 29 20 26 26 20 73 3c 28 28 4c 41 52 47 45  *z) && s<((LARGE
b4050 53 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30 29 20  ST_INT64-9)/10) 
b4060 29 7b 0a 20 20 20 20 73 20 3d 20 73 2a 31 30 20  ){.    s = s*10 
b4070 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20  + (*z - '0');.  
b4080 20 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b    z++, nDigits++
b4090 3b 0a 20 20 7d 0a 20 20 2f 2a 20 73 6b 69 70 20  ;.  }.  /* skip 
b40a0 6e 6f 6e 2d 73 69 67 6e 69 66 69 63 61 6e 74 20  non-significant 
b40b0 73 69 67 6e 69 66 69 63 61 6e 64 20 64 69 67 69  significand digi
b40c0 74 73 0a 20 20 2a 2a 20 28 69 6e 63 72 65 61 73  ts.  ** (increas
b40d0 65 20 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 20  e exponent by d 
b40e0 74 6f 20 73 68 69 66 74 20 64 65 63 69 6d 61 6c  to shift decimal
b40f0 20 6c 65 66 74 29 20 2a 2f 0a 20 20 77 68 69 6c   left) */.  whil
b4100 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  e( sqlite3Isdigi
b4110 74 28 2a 7a 29 20 29 20 7a 2b 2b 2c 20 6e 44 69  t(*z) ) z++, nDi
b4120 67 69 74 73 2b 2b 2c 20 64 2b 2b 3b 0a 0a 20 20  gits++, d++;..  
b4130 2f 2a 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f  /* if decimal po
b4140 69 6e 74 20 69 73 20 70 72 65 73 65 6e 74 20 2a  int is present *
b4150 2f 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2e 27 20  /.  if( *z=='.' 
b4160 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  ){.    z++;.    
b4170 2f 2a 20 63 6f 70 79 20 64 69 67 69 74 73 20 66  /* copy digits f
b4180 72 6f 6d 20 61 66 74 65 72 20 64 65 63 69 6d 61  rom after decima
b4190 6c 20 74 6f 20 73 69 67 6e 69 66 69 63 61 6e 64  l to significand
b41a0 0a 20 20 20 20 2a 2a 20 28 64 65 63 72 65 61 73  .    ** (decreas
b41b0 65 20 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 20  e exponent by d 
b41c0 74 6f 20 73 68 69 66 74 20 64 65 63 69 6d 61 6c  to shift decimal
b41d0 20 72 69 67 68 74 29 20 2a 2f 0a 20 20 20 20 77   right) */.    w
b41e0 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64  hile( sqlite3Isd
b41f0 69 67 69 74 28 2a 7a 29 20 26 26 20 73 3c 28 28  igit(*z) && s<((
b4200 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 2d 39 29  LARGEST_INT64-9)
b4210 2f 31 30 29 20 29 7b 0a 20 20 20 20 20 20 73 20  /10) ){.      s 
b4220 3d 20 73 2a 31 30 20 2b 20 28 2a 7a 20 2d 20 27  = s*10 + (*z - '
b4230 30 27 29 3b 0a 20 20 20 20 20 20 7a 2b 2b 2c 20  0');.      z++, 
b4240 6e 44 69 67 69 74 73 2b 2b 2c 20 64 2d 2d 3b 0a  nDigits++, d--;.
b4250 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 73 6b 69      }.    /* ski
b4260 70 20 6e 6f 6e 2d 73 69 67 6e 69 66 69 63 61 6e  p non-significan
b4270 74 20 64 69 67 69 74 73 20 2a 2f 0a 20 20 20 20  t digits */.    
b4280 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73  while( sqlite3Is
b4290 64 69 67 69 74 28 2a 7a 29 20 29 20 7a 2b 2b 2c  digit(*z) ) z++,
b42a0 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a   nDigits++;.  }.
b42b0 0a 20 20 2f 2a 20 69 66 20 65 78 70 6f 6e 65 6e  .  /* if exponen
b42c0 74 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  t is present */.
b42d0 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c    if( *z=='e' ||
b42e0 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20   *z=='E' ){.    
b42f0 7a 2b 2b 3b 0a 20 20 20 20 2f 2a 20 67 65 74 20  z++;.    /* get 
b4300 73 69 67 6e 20 6f 66 20 65 78 70 6f 6e 65 6e 74  sign of exponent
b4310 20 2a 2f 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d   */.    if( *z==
b4320 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 65 73 69  '-' ){.      esi
b4330 67 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7a  gn = -1;.      z
b4340 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ++;.    }else if
b4350 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20 20  ( *z=='+' ){.   
b4360 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     z++;.    }.  
b4370 20 20 2f 2a 20 63 6f 70 79 20 64 69 67 69 74 73    /* copy digits
b4380 20 74 6f 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a   to exponent */.
b4390 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74      while( sqlit
b43a0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b  e3Isdigit(*z) ){
b43b0 0a 20 20 20 20 20 20 65 20 3d 20 65 2a 31 30 20  .      e = e*10 
b43c0 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20 20  + (*z - '0');.  
b43d0 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20      z++;.    }. 
b43e0 20 7d 0a 0a 20 20 2f 2a 20 61 64 6a 75 73 74 20   }..  /* adjust 
b43f0 65 78 70 6f 6e 65 6e 74 20 62 79 20 64 2c 20 61  exponent by d, a
b4400 6e 64 20 75 70 64 61 74 65 20 73 69 67 6e 20 2a  nd update sign *
b4410 2f 0a 20 20 65 20 3d 20 28 65 2a 65 73 69 67 6e  /.  e = (e*esign
b4420 29 20 2b 20 64 3b 0a 20 20 69 66 28 20 65 3c 30  ) + d;.  if( e<0
b4430 20 29 20 7b 0a 20 20 20 20 65 73 69 67 6e 20 3d   ) {.    esign =
b4440 20 2d 31 3b 0a 20 20 20 20 65 20 2a 3d 20 2d 31   -1;.    e *= -1
b4450 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  ;.  } else {.   
b4460 20 65 73 69 67 6e 20 3d 20 31 3b 0a 20 20 7d 0a   esign = 1;.  }.
b4470 0a 20 20 2f 2a 20 69 66 20 30 20 73 69 67 6e 69  .  /* if 0 signi
b4480 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 66 28 20  ficand */.  if( 
b4490 21 73 20 29 20 7b 0a 20 20 20 20 2f 2a 20 49 6e  !s ) {.    /* In
b44a0 20 74 68 65 20 49 45 45 45 20 37 35 34 20 73 74   the IEEE 754 st
b44b0 61 6e 64 61 72 64 2c 20 7a 65 72 6f 20 69 73 20  andard, zero is 
b44c0 73 69 67 6e 65 64 2e 0a 20 20 20 20 2a 2a 20 41  signed..    ** A
b44d0 64 64 20 74 68 65 20 73 69 67 6e 20 69 66 20 77  dd the sign if w
b44e0 65 27 76 65 20 73 65 65 6e 20 61 74 20 6c 65 61  e've seen at lea
b44f0 73 74 20 6f 6e 65 20 64 69 67 69 74 20 2a 2f 0a  st one digit */.
b4500 20 20 20 20 72 65 73 75 6c 74 20 3d 20 28 73 69      result = (si
b4510 67 6e 3c 30 20 26 26 20 6e 44 69 67 69 74 73 29  gn<0 && nDigits)
b4520 20 3f 20 2d 28 64 6f 75 62 6c 65 29 30 20 3a 20   ? -(double)0 : 
b4530 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 7d 20 65  (double)0;.  } e
b4540 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 61 74 74  lse {.    /* att
b4550 65 6d 70 74 20 74 6f 20 72 65 64 75 63 65 20 65  empt to reduce e
b4560 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 69  xponent */.    i
b4570 66 28 20 65 73 69 67 6e 3e 30 20 29 7b 0a 20 20  f( esign>0 ){.  
b4580 20 20 20 20 77 68 69 6c 65 28 20 73 3c 28 4c 41      while( s<(LA
b4590 52 47 45 53 54 5f 49 4e 54 36 34 2f 31 30 29 20  RGEST_INT64/10) 
b45a0 26 26 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2a 3d  && e>0 ) e--,s*=
b45b0 31 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  10;.    }else{. 
b45c0 20 20 20 20 20 77 68 69 6c 65 28 20 21 28 73 25       while( !(s%
b45d0 31 30 29 20 26 26 20 65 3e 30 20 29 20 65 2d 2d  10) && e>0 ) e--
b45e0 2c 73 2f 3d 31 30 3b 0a 20 20 20 20 7d 0a 0a 20  ,s/=10;.    }.. 
b45f0 20 20 20 2f 2a 20 61 64 6a 75 73 74 20 74 68 65     /* adjust the
b4600 20 73 69 67 6e 20 6f 66 20 73 69 67 6e 69 66 69   sign of signifi
b4610 63 61 6e 64 20 2a 2f 0a 20 20 20 20 73 20 3d 20  cand */.    s = 
b4620 73 69 67 6e 3c 30 20 3f 20 2d 73 20 3a 20 73 3b  sign<0 ? -s : s;
b4630 0a 0a 20 20 20 20 2f 2a 20 69 66 20 65 78 70 6f  ..    /* if expo
b4640 6e 65 6e 74 2c 20 73 63 61 6c 65 20 73 69 67 6e  nent, scale sign
b4650 69 66 69 63 61 6e 64 20 61 73 20 61 70 70 72 6f  ificand as appro
b4660 70 72 69 61 74 65 0a 20 20 20 20 2a 2a 20 61 6e  priate.    ** an
b4670 64 20 73 74 6f 72 65 20 69 6e 20 72 65 73 75 6c  d store in resul
b4680 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 20  t. */.    if( e 
b4690 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  ){.      double 
b46a0 73 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20  scale = 1.0;.   
b46b0 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74 6f     /* attempt to
b46c0 20 68 61 6e 64 6c 65 20 65 78 74 72 65 6d 65 6c   handle extremel
b46d0 79 20 73 6d 61 6c 6c 2f 6c 61 72 67 65 20 6e 75  y small/large nu
b46e0 6d 62 65 72 73 20 62 65 74 74 65 72 20 2a 2f 0a  mbers better */.
b46f0 20 20 20 20 20 20 69 66 28 20 65 3e 33 30 37 20        if( e>307 
b4700 26 26 20 65 3c 33 34 32 20 29 7b 0a 20 20 20 20  && e<342 ){.    
b4710 20 20 20 20 77 68 69 6c 65 28 20 65 25 33 30 38      while( e%308
b4720 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e   ) { scale *= 1.
b4730 30 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a  0e+1; e -= 1; }.
b4740 20 20 20 20 20 20 20 20 69 66 28 20 65 73 69 67          if( esig
b4750 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  n<0 ){.         
b4760 20 72 65 73 75 6c 74 20 3d 20 73 20 2f 20 73 63   result = s / sc
b4770 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 72  ale;.          r
b4780 65 73 75 6c 74 20 2f 3d 20 31 2e 30 65 2b 33 30  esult /= 1.0e+30
b4790 38 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  8;.        }else
b47a0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75  {.          resu
b47b0 6c 74 20 3d 20 73 20 2a 20 73 63 61 6c 65 3b 0a  lt = s * scale;.
b47c0 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74            result
b47d0 20 2a 3d 20 31 2e 30 65 2b 33 30 38 3b 0a 20 20   *= 1.0e+308;.  
b47e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
b47f0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
b4800 31 2e 30 65 2b 32 32 20 69 73 20 74 68 65 20 6c  1.0e+22 is the l
b4810 61 72 67 65 73 74 20 70 6f 77 65 72 20 6f 66 20  argest power of 
b4820 31 30 20 74 68 61 6e 20 63 61 6e 20 62 65 20 0a  10 than can be .
b4830 20 20 20 20 20 20 20 20 2a 2a 20 72 65 70 72 65          ** repre
b4840 73 65 6e 74 65 64 20 65 78 61 63 74 6c 79 2e 20  sented exactly. 
b4850 2a 2f 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  */.        while
b4860 28 20 65 25 32 32 20 29 20 7b 20 73 63 61 6c 65  ( e%22 ) { scale
b4870 20 2a 3d 20 31 2e 30 65 2b 31 3b 20 65 20 2d 3d   *= 1.0e+1; e -=
b4880 20 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 77 68   1; }.        wh
b4890 69 6c 65 28 20 65 3e 30 20 29 20 7b 20 73 63 61  ile( e>0 ) { sca
b48a0 6c 65 20 2a 3d 20 31 2e 30 65 2b 32 32 3b 20 65  le *= 1.0e+22; e
b48b0 20 2d 3d 20 32 32 3b 20 7d 0a 20 20 20 20 20 20   -= 22; }.      
b48c0 20 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b    if( esign<0 ){
b48d0 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c  .          resul
b48e0 74 20 3d 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20  t = s / scale;. 
b48f0 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
b4900 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d          result =
b4910 20 73 20 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20   s * scale;.    
b4920 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
b4930 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
b4940 20 72 65 73 75 6c 74 20 3d 20 28 64 6f 75 62 6c   result = (doubl
b4950 65 29 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  e)s;.    }.  }..
b4960 20 20 2f 2a 20 73 74 6f 72 65 20 74 68 65 20 72    /* store the r
b4970 65 73 75 6c 74 20 2a 2f 0a 20 20 2a 70 52 65 73  esult */.  *pRes
b4980 75 6c 74 20 3d 20 72 65 73 75 6c 74 3b 0a 0a 20  ult = result;.. 
b4990 20 2f 2a 20 72 65 74 75 72 6e 20 6e 75 6d 62 65   /* return numbe
b49a0 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
b49b0 75 73 65 64 20 2a 2f 0a 20 20 72 65 74 75 72 6e  used */.  return
b49c0 20 28 69 6e 74 29 28 7a 20 2d 20 7a 42 65 67 69   (int)(z - zBegi
b49d0 6e 29 3b 0a 23 65 6c 73 65 0a 20 20 72 65 74 75  n);.#else.  retu
b49e0 72 6e 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34  rn sqlite3Atoi64
b49f0 28 7a 2c 20 70 52 65 73 75 6c 74 29 3b 0a 23 65  (z, pResult);.#e
b4a00 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
b4a10 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
b4a20 4e 54 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  NT */.}../*.** C
b4a30 6f 6d 70 61 72 65 20 74 68 65 20 31 39 2d 63 68  ompare the 19-ch
b4a40 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 7a  aracter string z
b4a50 4e 75 6d 20 61 67 61 69 6e 73 74 20 74 68 65 20  Num against the 
b4a60 74 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74  text representat
b4a70 69 6f 6e 0a 2a 2a 20 76 61 6c 75 65 20 32 5e 36  ion.** value 2^6
b4a80 33 3a 20 20 39 32 32 33 33 37 32 30 33 36 38 35  3:  922337203685
b4a90 34 37 37 35 38 30 38 2e 20 20 52 65 74 75 72 6e  4775808.  Return
b4aa0 20 6e 65 67 61 74 69 76 65 2c 20 7a 65 72 6f 2c   negative, zero,
b4ab0 20 6f 72 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20   or positive.** 
b4ac0 69 66 20 7a 4e 75 6d 20 69 73 20 6c 65 73 73 20  if zNum is less 
b4ad0 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c 20  than, equal to, 
b4ae0 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  or greater than 
b4af0 74 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  the string..**.*
b4b00 2a 20 55 6e 6c 69 6b 65 20 6d 65 6d 63 6d 70 28  * Unlike memcmp(
b4b10 29 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  ) this routine i
b4b20 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  s guaranteed to 
b4b30 72 65 74 75 72 6e 20 74 68 65 20 64 69 66 66 65  return the diffe
b4b40 72 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65 20  rence.** in the 
b4b50 76 61 6c 75 65 73 20 6f 66 20 74 68 65 20 6c 61  values of the la
b4b60 73 74 20 64 69 67 69 74 20 69 66 20 74 68 65 20  st digit if the 
b4b70 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63 65 20  only difference 
b4b80 69 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6c 61 73  is in the.** las
b4b90 74 20 64 69 67 69 74 2e 20 20 53 6f 2c 20 66 6f  t digit.  So, fo
b4ba0 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a  r example,.**.**
b4bb0 20 20 20 20 20 20 63 6f 6d 70 61 72 65 32 70 6f        compare2po
b4bc0 77 36 33 28 22 39 32 32 33 33 37 32 30 33 36 38  w63("92233720368
b4bd0 35 34 37 37 35 38 30 30 22 29 0a 2a 2a 0a 2a 2a  54775800").**.**
b4be0 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 2d 38 2e   will return -8.
b4bf0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
b4c00 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 63 6f 6e  ompare2pow63(con
b4c10 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d 29 7b 0a  st char *zNum){.
b4c20 20 20 69 6e 74 20 63 3b 0a 20 20 63 20 3d 20 6d    int c;.  c = m
b4c30 65 6d 63 6d 70 28 7a 4e 75 6d 2c 22 39 32 32 33  emcmp(zNum,"9223
b4c40 33 37 32 30 33 36 38 35 34 37 37 35 38 30 22 2c  37203685477580",
b4c50 31 38 29 2a 31 30 3b 0a 20 20 69 66 28 20 63 3d  18)*10;.  if( c=
b4c60 3d 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 7a 4e  =0 ){.    c = zN
b4c70 75 6d 5b 31 38 5d 20 2d 20 27 38 27 3b 0a 20 20  um[18] - '8';.  
b4c80 7d 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a  }.  return c;.}.
b4c90 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
b4ca0 52 55 45 20 69 66 20 7a 4e 75 6d 20 69 73 20 61  RUE if zNum is a
b4cb0 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
b4cc0 6e 74 65 67 65 72 20 61 6e 64 20 77 72 69 74 65  nteger and write
b4cd0 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66  .** the value of
b4ce0 20 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 74   the integer int
b4cf0 6f 20 2a 70 4e 75 6d 2e 20 20 49 66 20 7a 4e 75  o *pNum.  If zNu
b4d00 6d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65  m is not an inte
b4d10 67 65 72 0a 2a 2a 20 6f 72 20 69 73 20 61 6e 20  ger.** or is an 
b4d20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73 20  integer that is 
b4d30 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 62 65 20  too large to be 
b4d40 65 78 70 72 65 73 73 65 64 20 77 69 74 68 20 36  expressed with 6
b4d50 34 20 62 69 74 73 2c 0a 2a 2a 20 74 68 65 6e 20  4 bits,.** then 
b4d60 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a  return false..**
b4d70 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f  .** When this ro
b4d80 75 74 69 6e 65 20 77 61 73 20 6f 72 69 67 69 6e  utine was origin
b4d90 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 74 20  ally written it 
b4da0 64 65 61 6c 74 20 77 69 74 68 20 6f 6e 6c 79 0a  dealt with only.
b4db0 2a 2a 20 33 32 2d 62 69 74 20 6e 75 6d 62 65 72  ** 32-bit number
b4dc0 73 2e 20 20 41 74 20 74 68 61 74 20 74 69 6d 65  s.  At that time
b4dd0 2c 20 69 74 20 77 61 73 20 6d 75 63 68 20 66 61  , it was much fa
b4de0 73 74 65 72 20 74 68 61 6e 20 74 68 65 0a 2a 2a  ster than the.**
b4df0 20 61 74 6f 69 28 29 20 6c 69 62 72 61 72 79 20   atoi() library 
b4e00 72 6f 75 74 69 6e 65 20 69 6e 20 52 65 64 48 61  routine in RedHa
b4e10 74 20 37 2e 32 2e 0a 2a 2f 0a 53 51 4c 49 54 45  t 7.2..*/.SQLITE
b4e20 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b4e30 69 74 65 33 41 74 6f 69 36 34 28 63 6f 6e 73 74  ite3Atoi64(const
b4e40 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 36 34   char *zNum, i64
b4e50 20 2a 70 4e 75 6d 29 7b 0a 20 20 69 36 34 20 76   *pNum){.  i64 v
b4e60 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 65 67 3b   = 0;.  int neg;
b4e70 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 63  .  int i, c;.  c
b4e80 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72  onst char *zStar
b4e90 74 3b 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69  t;.  while( sqli
b4ea0 74 65 33 49 73 73 70 61 63 65 28 2a 7a 4e 75 6d  te3Isspace(*zNum
b4eb0 29 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 69 66  ) ) zNum++;.  if
b4ec0 28 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a  ( *zNum=='-' ){.
b4ed0 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20      neg = 1;.   
b4ee0 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65   zNum++;.  }else
b4ef0 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20   if( *zNum=='+' 
b4f00 29 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a  ){.    neg = 0;.
b4f10 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65      zNum++;.  }e
b4f20 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30  lse{.    neg = 0
b4f30 3b 0a 20 20 7d 0a 20 20 7a 53 74 61 72 74 20 3d  ;.  }.  zStart =
b4f40 20 7a 4e 75 6d 3b 0a 20 20 77 68 69 6c 65 28 20   zNum;.  while( 
b4f50 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 7b 20  zNum[0]=='0' ){ 
b4f60 7a 4e 75 6d 2b 2b 3b 20 7d 20 2f 2a 20 53 6b 69  zNum++; } /* Ski
b4f70 70 20 6f 76 65 72 20 6c 65 61 64 69 6e 67 20 7a  p over leading z
b4f80 65 72 6f 73 2e 20 54 69 63 6b 65 74 20 23 32 34  eros. Ticket #24
b4f90 35 34 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  54 */.  for(i=0;
b4fa0 20 28 63 3d 7a 4e 75 6d 5b 69 5d 29 3e 3d 27 30   (c=zNum[i])>='0
b4fb0 27 20 26 26 20 63 3c 3d 27 39 27 3b 20 69 2b 2b  ' && c<='9'; i++
b4fc0 29 7b 0a 20 20 20 20 76 20 3d 20 76 2a 31 30 20  ){.    v = v*10 
b4fd0 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 7d 0a 20  + c - '0';.  }. 
b4fe0 20 2a 70 4e 75 6d 20 3d 20 6e 65 67 20 3f 20 2d   *pNum = neg ? -
b4ff0 76 20 3a 20 76 3b 0a 20 20 69 66 28 20 63 21 3d  v : v;.  if( c!=
b5000 30 20 7c 7c 20 28 69 3d 3d 30 20 26 26 20 7a 53  0 || (i==0 && zS
b5010 74 61 72 74 3d 3d 7a 4e 75 6d 29 20 7c 7c 20 69  tart==zNum) || i
b5020 3e 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 7a 4e  >19 ){.    /* zN
b5030 75 6d 20 69 73 20 65 6d 70 74 79 20 6f 72 20 63  um is empty or c
b5040 6f 6e 74 61 69 6e 73 20 6e 6f 6e 2d 6e 75 6d 65  ontains non-nume
b5050 72 69 63 20 74 65 78 74 20 6f 72 20 69 73 20 6c  ric text or is l
b5060 6f 6e 67 65 72 0a 20 20 20 20 2a 2a 20 74 68 61  onger.    ** tha
b5070 6e 20 31 39 20 64 69 67 69 74 73 20 28 74 68 75  n 19 digits (thu
b5080 73 20 67 75 61 72 61 6e 74 69 6e 67 20 74 68 61  s guaranting tha
b5090 74 20 69 74 20 69 73 20 74 6f 6f 20 6c 61 72 67  t it is too larg
b50a0 65 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  e) */.    return
b50b0 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
b50c0 69 3c 31 39 20 29 7b 0a 20 20 20 20 2f 2a 20 4c  i<19 ){.    /* L
b50d0 65 73 73 20 74 68 61 6e 20 31 39 20 64 69 67 69  ess than 19 digi
b50e0 74 73 2c 20 73 6f 20 77 65 20 6b 6e 6f 77 20 74  ts, so we know t
b50f0 68 61 74 20 69 74 20 66 69 74 73 20 69 6e 20 36  hat it fits in 6
b5100 34 20 62 69 74 73 20 2a 2f 0a 20 20 20 20 72 65  4 bits */.    re
b5110 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  turn 1;.  }else{
b5120 0a 20 20 20 20 2f 2a 20 31 39 2d 64 69 67 69 74  .    /* 19-digit
b5130 20 6e 75 6d 62 65 72 73 20 6d 75 73 74 20 62 65   numbers must be
b5140 20 6e 6f 20 6c 61 72 67 65 72 20 74 68 61 6e 20   no larger than 
b5150 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
b5160 38 30 37 20 69 66 20 70 6f 73 69 74 69 76 65 0a  807 if positive.
b5170 20 20 20 20 2a 2a 20 6f 72 20 39 32 32 33 33 37      ** or 922337
b5180 32 30 33 36 38 35 34 37 37 35 38 30 38 20 69 66  2036854775808 if
b5190 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65   negative.  Note
b51a0 20 74 68 61 74 20 39 32 32 33 33 37 32 30 33 36   that 9223372036
b51b0 38 35 34 36 36 35 38 30 38 0a 20 20 20 20 2a 2a  854665808.    **
b51c0 20 69 73 20 32 5e 36 33 2e 20 2a 2f 0a 20 20 20   is 2^63. */.   
b51d0 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65 32   return compare2
b51e0 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67 3b  pow63(zNum)<neg;
b51f0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
b5200 65 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 72 65  e string zNum re
b5210 70 72 65 73 65 6e 74 73 20 61 6e 20 75 6e 73 69  presents an unsi
b5220 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 54  gned integer.  T
b5230 68 65 20 7a 4e 75 6d 20 73 74 72 69 6e 67 0a 2a  he zNum string.*
b5240 2a 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 6f 6e  * consists of on
b5250 65 20 6f 72 20 6d 6f 72 65 20 64 69 67 69 74 20  e or more digit 
b5260 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 20 69  characters and i
b5270 73 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 0a  s terminated by.
b5280 2a 2a 20 61 20 7a 65 72 6f 20 63 68 61 72 61 63  ** a zero charac
b5290 74 65 72 2e 20 20 41 6e 79 20 73 74 72 61 79 20  ter.  Any stray 
b52a0 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 4e  characters in zN
b52b0 75 6d 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64  um result in und
b52c0 65 66 69 6e 65 64 0a 2a 2a 20 62 65 68 61 76 69  efined.** behavi
b52d0 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  or..**.** If the
b52e0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
b52f0 72 20 74 68 61 74 20 7a 4e 75 6d 20 72 65 70 72  r that zNum repr
b5300 65 73 65 6e 74 73 20 77 69 6c 6c 20 66 69 74 20  esents will fit 
b5310 69 6e 20 61 0a 2a 2a 20 36 34 2d 62 69 74 20 73  in a.** 64-bit s
b5320 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2c 20 72  igned integer, r
b5330 65 74 75 72 6e 20 54 52 55 45 2e 20 20 4f 74 68  eturn TRUE.  Oth
b5340 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 46 41  erwise return FA
b5350 4c 53 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  LSE..**.** If th
b5360 65 20 6e 65 67 46 6c 61 67 20 70 61 72 61 6d 65  e negFlag parame
b5370 74 65 72 20 69 73 20 74 72 75 65 2c 20 74 68 61  ter is true, tha
b5380 74 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 4e 75  t means that zNu
b5390 6d 20 72 65 61 6c 6c 79 20 72 65 70 72 65 73 65  m really represe
b53a0 6e 74 73 0a 2a 2a 20 61 20 6e 65 67 61 74 69 76  nts.** a negativ
b53b0 65 20 6e 75 6d 62 65 72 2e 20 20 28 54 68 65 20  e number.  (The 
b53c0 6c 65 61 64 69 6e 67 20 22 2d 22 20 69 73 20 6f  leading "-" is o
b53d0 6d 69 74 74 65 64 20 66 72 6f 6d 20 7a 4e 75 6d  mitted from zNum
b53e0 2e 29 20 20 54 68 69 73 0a 2a 2a 20 70 61 72 61  .)  This.** para
b53f0 6d 65 74 65 72 20 69 73 20 6e 65 65 64 65 64 20  meter is needed 
b5400 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 61 20 62  to determine a b
b5410 6f 75 6e 64 61 72 79 20 63 61 73 65 2e 20 20 41  oundary case.  A
b5420 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 66 20 22 39   string.** of "9
b5430 32 32 33 33 37 33 30 33 36 38 35 34 37 37 35 38  2233730368547758
b5440 30 38 22 20 72 65 74 75 72 6e 73 20 66 61 6c 73  08" returns fals
b5450 65 20 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20  e if negFlag is 
b5460 66 61 6c 73 65 20 6f 72 20 74 72 75 65 0a 2a 2a  false or true.**
b5470 20 69 66 20 6e 65 67 46 6c 61 67 20 69 73 20 74   if negFlag is t
b5480 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 64 69  rue..**.** Leadi
b5490 6e 67 20 7a 65 72 6f 73 20 61 72 65 20 69 67 6e  ng zeros are ign
b54a0 6f 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ored..*/.SQLITE_
b54b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b54c0 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28  te3FitsIn64Bits(
b54d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d  const char *zNum
b54e0 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 29 7b 0a  , int negFlag){.
b54f0 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
b5500 65 67 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  eg = 0;..  asser
b5510 74 28 20 7a 4e 75 6d 5b 30 5d 3e 3d 27 30 27 20  t( zNum[0]>='0' 
b5520 26 26 20 7a 4e 75 6d 5b 30 5d 3c 3d 27 39 27 20  && zNum[0]<='9' 
b5530 29 3b 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 61 6e  ); /* zNum is an
b5540 20 75 6e 73 69 67 6e 65 64 20 6e 75 6d 62 65 72   unsigned number
b5550 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 65 67 46 6c   */..  if( negFl
b5560 61 67 20 29 20 6e 65 67 20 3d 20 31 2d 6e 65 67  ag ) neg = 1-neg
b5570 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 4e 75 6d  ;.  while( *zNum
b5580 3d 3d 27 30 27 20 29 7b 0a 20 20 20 20 7a 4e 75  =='0' ){.    zNu
b5590 6d 2b 2b 3b 20 20 20 2f 2a 20 53 6b 69 70 20 6c  m++;   /* Skip l
b55a0 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 20 54  eading zeros.  T
b55b0 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20  icket #2454 */. 
b55c0 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 4e   }.  for(i=0; zN
b55d0 75 6d 5b 69 5d 3b 20 69 2b 2b 29 7b 20 61 73 73  um[i]; i++){ ass
b55e0 65 72 74 28 20 7a 4e 75 6d 5b 69 5d 3e 3d 27 30  ert( zNum[i]>='0
b55f0 27 20 26 26 20 7a 4e 75 6d 5b 69 5d 3c 3d 27 39  ' && zNum[i]<='9
b5600 27 20 29 3b 20 7d 0a 20 20 69 66 28 20 69 3c 31  ' ); }.  if( i<1
b5610 39 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72  9 ){.    /* Guar
b5620 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 69 66  anteed to fit if
b5630 20 6c 65 73 73 20 74 68 61 6e 20 31 39 20 64 69   less than 19 di
b5640 67 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75  gits */.    retu
b5650 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 1;.  }else if
b5660 28 20 69 3e 31 39 20 29 7b 0a 20 20 20 20 2f 2a  ( i>19 ){.    /*
b5670 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62   Guaranteed to b
b5680 65 20 74 6f 6f 20 62 69 67 20 69 66 20 67 72 65  e too big if gre
b5690 61 74 65 72 20 74 68 61 6e 20 31 39 20 64 69 67  ater than 19 dig
b56a0 69 74 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  its */.    retur
b56b0 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  n 0;.  }else{.  
b56c0 20 20 2f 2a 20 43 6f 6d 70 61 72 65 20 61 67 61    /* Compare aga
b56d0 69 6e 73 74 20 32 5e 36 33 2e 20 2a 2f 0a 20 20  inst 2^63. */.  
b56e0 20 20 72 65 74 75 72 6e 20 63 6f 6d 70 61 72 65    return compare
b56f0 32 70 6f 77 36 33 28 7a 4e 75 6d 29 3c 6e 65 67  2pow63(zNum)<neg
b5700 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
b5710 66 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74  f zNum represent
b5720 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  s an integer tha
b5730 74 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 33 32  t will fit in 32
b5740 2d 62 69 74 73 2c 20 74 68 65 6e 20 73 65 74 0a  -bits, then set.
b5750 2a 2a 20 2a 70 56 61 6c 75 65 20 74 6f 20 74 68  ** *pValue to th
b5760 61 74 20 69 6e 74 65 67 65 72 20 61 6e 64 20 72  at integer and r
b5770 65 74 75 72 6e 20 74 72 75 65 2e 20 20 4f 74 68  eturn true.  Oth
b5780 65 72 77 69 73 65 20 72 65 74 75 72 6e 20 66 61  erwise return fa
b5790 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 6e  lse..**.** Any n
b57a0 6f 6e 2d 6e 75 6d 65 72 69 63 20 63 68 61 72 61  on-numeric chara
b57b0 63 74 65 72 73 20 74 68 61 74 20 66 6f 6c 6c 6f  cters that follo
b57c0 77 69 6e 67 20 7a 4e 75 6d 20 61 72 65 20 69 67  wing zNum are ig
b57d0 6e 6f 72 65 64 2e 0a 2a 2a 20 54 68 69 73 20 69  nored..** This i
b57e0 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  s different from
b57f0 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 29   sqlite3Atoi64()
b5800 20 77 68 69 63 68 20 72 65 71 75 69 72 65 73 20   which requires 
b5810 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 6e 75 6d  the.** input num
b5820 62 65 72 20 74 6f 20 62 65 20 7a 65 72 6f 2d 74  ber to be zero-t
b5830 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2f 0a 53 51  erminated..*/.SQ
b5840 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b5850 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32   sqlite3GetInt32
b5860 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75  (const char *zNu
b5870 6d 2c 20 69 6e 74 20 2a 70 56 61 6c 75 65 29 7b  m, int *pValue){
b5880 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
b5890 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c 20  v = 0;.  int i, 
b58a0 63 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30  c;.  int neg = 0
b58b0 3b 0a 20 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d  ;.  if( zNum[0]=
b58c0 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20  ='-' ){.    neg 
b58d0 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b  = 1;.    zNum++;
b58e0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 4e 75  .  }else if( zNu
b58f0 6d 5b 30 5d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20  m[0]=='+' ){.   
b5900 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 77   zNum++;.  }.  w
b5910 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27  hile( zNum[0]=='
b5920 30 27 20 29 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 66  0' ) zNum++;.  f
b5930 6f 72 28 69 3d 30 3b 20 69 3c 31 31 20 26 26 20  or(i=0; i<11 && 
b5940 28 63 20 3d 20 7a 4e 75 6d 5b 69 5d 20 2d 20 27  (c = zNum[i] - '
b5950 30 27 29 3e 3d 30 20 26 26 20 63 3c 3d 39 3b 20  0')>=0 && c<=9; 
b5960 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 76 2a  i++){.    v = v*
b5970 31 30 20 2b 20 63 3b 0a 20 20 7d 0a 0a 20 20 2f  10 + c;.  }..  /
b5980 2a 20 54 68 65 20 6c 6f 6e 67 65 73 74 20 64 65  * The longest de
b5990 63 69 6d 61 6c 20 72 65 70 72 65 73 65 6e 74 61  cimal representa
b59a0 74 69 6f 6e 20 6f 66 20 61 20 33 32 20 62 69 74  tion of a 32 bit
b59b0 20 69 6e 74 65 67 65 72 20 69 73 20 31 30 20 64   integer is 10 d
b59c0 69 67 69 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  igits:.  **.  **
b59d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 33               123
b59e0 34 35 36 37 38 39 30 0a 20 20 2a 2a 20 20 20 20  4567890.  **    
b59f0 20 32 5e 33 31 20 2d 3e 20 32 31 34 37 34 38 33   2^31 -> 2147483
b5a00 36 34 38 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  648.  */.  if( i
b5a10 3e 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  >10 ){.    retur
b5a20 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76  n 0;.  }.  if( v
b5a30 2d 6e 65 67 3e 32 31 34 37 34 38 33 36 34 37 20  -neg>2147483647 
b5a40 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
b5a50 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67 20 29  .  }.  if( neg )
b5a60 7b 0a 20 20 20 20 76 20 3d 20 2d 76 3b 0a 20 20  {.    v = -v;.  
b5a70 7d 0a 20 20 2a 70 56 61 6c 75 65 20 3d 20 28 69  }.  *pValue = (i
b5a80 6e 74 29 76 3b 0a 20 20 72 65 74 75 72 6e 20 31  nt)v;.  return 1
b5a90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76  ;.}../*.** The v
b5aa0 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
b5ab0 6e 74 65 67 65 72 20 65 6e 63 6f 64 69 6e 67 20  nteger encoding 
b5ac0 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  is as follows:.*
b5ad0 2a 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 20  *.** KEY:.**    
b5ae0 20 20 20 20 20 41 20 3d 20 30 78 78 78 78 78 78       A = 0xxxxxx
b5af0 78 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64  x    7 bits of d
b5b00 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67  ata and one flag
b5b10 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20   bit.**         
b5b20 42 20 3d 20 31 78 78 78 78 78 78 78 20 20 20 20  B = 1xxxxxxx    
b5b30 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61  7 bits of data a
b5b40 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a  nd one flag bit.
b5b50 2a 2a 20 20 20 20 20 20 20 20 20 43 20 3d 20 78  **         C = x
b5b60 78 78 78 78 78 78 78 20 20 20 20 38 20 62 69 74  xxxxxxx    8 bit
b5b70 73 20 6f 66 20 64 61 74 61 0a 2a 2a 0a 2a 2a 20  s of data.**.** 
b5b80 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 31   7 bits - A.** 1
b5b90 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 32  4 bits - BA.** 2
b5ba0 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 20  1 bits - BBA.** 
b5bb0 32 38 20 62 69 74 73 20 2d 20 42 42 42 41 0a 2a  28 bits - BBBA.*
b5bc0 2a 20 33 35 20 62 69 74 73 20 2d 20 42 42 42 42  * 35 bits - BBBB
b5bd0 41 0a 2a 2a 20 34 32 20 62 69 74 73 20 2d 20 42  A.** 42 bits - B
b5be0 42 42 42 42 41 0a 2a 2a 20 34 39 20 62 69 74 73  BBBBA.** 49 bits
b5bf0 20 2d 20 42 42 42 42 42 42 41 0a 2a 2a 20 35 36   - BBBBBBA.** 56
b5c00 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 41   bits - BBBBBBBA
b5c10 0a 2a 2a 20 36 34 20 62 69 74 73 20 2d 20 42 42  .** 64 bits - BB
b5c20 42 42 42 42 42 42 43 0a 2a 2f 0a 0a 2f 2a 0a 2a  BBBBBBC.*/../*.*
b5c30 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62 69 74  * Write a 64-bit
b5c40 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68   variable-length
b5c50 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d 6f   integer to memo
b5c60 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  ry starting at p
b5c70 5b 30 5d 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e 67  [0]..** The leng
b5c80 74 68 20 6f 66 20 64 61 74 61 20 77 72 69 74 65  th of data write
b5c90 20 77 69 6c 6c 20 62 65 20 62 65 74 77 65 65 6e   will be between
b5ca0 20 31 20 61 6e 64 20 39 20 62 79 74 65 73 2e 20   1 and 9 bytes. 
b5cb0 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f   The number.** o
b5cc0 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e 20  f bytes written 
b5cd0 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
b5ce0 2a 2a 20 41 20 76 61 72 69 61 62 6c 65 2d 6c 65  ** A variable-le
b5cf0 6e 67 74 68 20 69 6e 74 65 67 65 72 20 63 6f 6e  ngth integer con
b5d00 73 69 73 74 73 20 6f 66 20 74 68 65 20 6c 6f 77  sists of the low
b5d10 65 72 20 37 20 62 69 74 73 20 6f 66 20 65 61 63  er 7 bits of eac
b5d20 68 20 62 79 74 65 0a 2a 2a 20 66 6f 72 20 61 6c  h byte.** for al
b5d30 6c 20 62 79 74 65 73 20 74 68 61 74 20 68 61 76  l bytes that hav
b5d40 65 20 74 68 65 20 38 74 68 20 62 69 74 20 73 65  e the 8th bit se
b5d50 74 20 61 6e 64 20 6f 6e 65 20 62 79 74 65 20 77  t and one byte w
b5d60 69 74 68 20 74 68 65 20 38 74 68 0a 2a 2a 20 62  ith the 8th.** b
b5d70 69 74 20 63 6c 65 61 72 2e 20 20 45 78 63 65 70  it clear.  Excep
b5d80 74 2c 20 69 66 20 77 65 20 67 65 74 20 74 6f 20  t, if we get to 
b5d90 74 68 65 20 39 74 68 20 62 79 74 65 2c 20 69 74  the 9th byte, it
b5da0 20 73 74 6f 72 65 73 20 74 68 65 20 66 75 6c 6c   stores the full
b5db0 0a 2a 2a 20 38 20 62 69 74 73 20 61 6e 64 20 69  .** 8 bits and i
b5dc0 73 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 2e  s the last byte.
b5dd0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b5de0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75  TE int sqlite3Pu
b5df0 74 56 61 72 69 6e 74 28 75 6e 73 69 67 6e 65 64  tVarint(unsigned
b5e00 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20 76 29   char *p, u64 v)
b5e10 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b  {.  int i, j, n;
b5e20 0a 20 20 75 38 20 62 75 66 5b 31 30 5d 3b 0a 20  .  u8 buf[10];. 
b5e30 20 69 66 28 20 76 20 26 20 28 28 28 75 36 34 29   if( v & (((u64)
b5e40 30 78 66 66 30 30 30 30 30 30 29 3c 3c 33 32 29  0xff000000)<<32)
b5e50 20 29 7b 0a 20 20 20 20 70 5b 38 5d 20 3d 20 28   ){.    p[8] = (
b5e60 75 38 29 76 3b 0a 20 20 20 20 76 20 3e 3e 3d 20  u8)v;.    v >>= 
b5e70 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d 37 3b 20  8;.    for(i=7; 
b5e80 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20  i>=0; i--){.    
b5e90 20 20 70 5b 69 5d 20 3d 20 28 75 38 29 28 28 76    p[i] = (u8)((v
b5ea0 20 26 20 30 78 37 66 29 20 7c 20 30 78 38 30 29   & 0x7f) | 0x80)
b5eb0 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 37 3b  ;.      v >>= 7;
b5ec0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
b5ed0 6e 20 39 3b 0a 20 20 7d 20 20 20 20 0a 20 20 6e  n 9;.  }    .  n
b5ee0 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20   = 0;.  do{.    
b5ef0 62 75 66 5b 6e 2b 2b 5d 20 3d 20 28 75 38 29 28  buf[n++] = (u8)(
b5f00 28 76 20 26 20 30 78 37 66 29 20 7c 20 30 78 38  (v & 0x7f) | 0x8
b5f10 30 29 3b 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b  0);.    v >>= 7;
b5f20 0a 20 20 7d 77 68 69 6c 65 28 20 76 21 3d 30 20  .  }while( v!=0 
b5f30 29 3b 0a 20 20 62 75 66 5b 30 5d 20 26 3d 20 30  );.  buf[0] &= 0
b5f40 78 37 66 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  x7f;.  assert( n
b5f50 3c 3d 39 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30  <=9 );.  for(i=0
b5f60 2c 20 6a 3d 6e 2d 31 3b 20 6a 3e 3d 30 3b 20 6a  , j=n-1; j>=0; j
b5f70 2d 2d 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 70 5b  --, i++){.    p[
b5f80 69 5d 20 3d 20 62 75 66 5b 6a 5d 3b 0a 20 20 7d  i] = buf[j];.  }
b5f90 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
b5fa0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
b5fb0 6e 65 20 69 73 20 61 20 66 61 73 74 65 72 20 76  ne is a faster v
b5fc0 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  ersion of sqlite
b5fd0 33 50 75 74 56 61 72 69 6e 74 28 29 20 74 68 61  3PutVarint() tha
b5fe0 74 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20  t only.** works 
b5ff0 66 6f 72 20 33 32 2d 62 69 74 20 70 6f 73 69 74  for 32-bit posit
b6000 69 76 65 20 69 6e 74 65 67 65 72 73 20 61 6e 64  ive integers and
b6010 20 77 68 69 63 68 20 69 73 20 6f 70 74 69 6d 69   which is optimi
b6020 7a 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63  zed for.** the c
b6030 6f 6d 6d 6f 6e 20 63 61 73 65 20 6f 66 20 73 6d  ommon case of sm
b6040 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 20 20 41  all integers.  A
b6050 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20   MACRO version, 
b6060 70 75 74 56 61 72 69 6e 74 33 32 2c 0a 2a 2a 20  putVarint32,.** 
b6070 69 73 20 70 72 6f 76 69 64 65 64 20 77 68 69 63  is provided whic
b6080 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20 73 69  h inlines the si
b6090 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20  ngle-byte case. 
b60a0 20 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64   All code should
b60b0 20 75 73 65 0a 2a 2a 20 74 68 65 20 4d 41 43 52   use.** the MACR
b60c0 4f 20 76 65 72 73 69 6f 6e 20 61 73 20 74 68 69  O version as thi
b60d0 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d  s function assum
b60e0 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79  es the single-by
b60f0 74 65 20 63 61 73 65 20 68 61 73 0a 2a 2a 20 61  te case has.** a
b6100 6c 72 65 61 64 79 20 62 65 65 6e 20 68 61 6e 64  lready been hand
b6110 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  led..*/.SQLITE_P
b6120 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b6130 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e  e3PutVarint32(un
b6140 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20  signed char *p, 
b6150 75 33 32 20 76 29 7b 0a 23 69 66 6e 64 65 66 20  u32 v){.#ifndef 
b6160 70 75 74 56 61 72 69 6e 74 33 32 0a 20 20 69 66  putVarint32.  if
b6170 28 20 28 76 20 26 20 7e 30 78 37 66 29 3d 3d 30  ( (v & ~0x7f)==0
b6180 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20 76   ){.    p[0] = v
b6190 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
b61a0 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28    }.#endif.  if(
b61b0 20 28 76 20 26 20 7e 30 78 33 66 66 66 29 3d 3d   (v & ~0x3fff)==
b61c0 30 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d 20  0 ){.    p[0] = 
b61d0 28 75 38 29 28 28 76 3e 3e 37 29 20 7c 20 30 78  (u8)((v>>7) | 0x
b61e0 38 30 29 3b 0a 20 20 20 20 70 5b 31 5d 20 3d 20  80);.    p[1] = 
b61f0 28 75 38 29 28 76 20 26 20 30 78 37 66 29 3b 0a  (u8)(v & 0x7f);.
b6200 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
b6210 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
b6220 65 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20 76  e3PutVarint(p, v
b6230 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  );.}../*.** Read
b6240 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62   a 64-bit variab
b6250 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
b6260 72 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74  r from memory st
b6270 61 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a  arting at p[0]..
b6280 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
b6290 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65  mber of bytes re
b62a0 61 64 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69  ad.  The value i
b62b0 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a  s stored in *v..
b62c0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b62d0 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56  E u8 sqlite3GetV
b62e0 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69  arint(const unsi
b62f0 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75 36  gned char *p, u6
b6300 34 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c 62  4 *v){.  u32 a,b
b6310 2c 73 3b 0a 0a 20 20 61 20 3d 20 2a 70 3b 0a 20  ,s;..  a = *p;. 
b6320 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d 61 73   /* a: p0 (unmas
b6330 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
b6340 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  a&0x80)).  {.   
b6350 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74   *v = a;.    ret
b6360 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 70 2b  urn 1;.  }..  p+
b6370 2b 3b 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f  +;.  b = *p;.  /
b6380 2a 20 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65  * b: p1 (unmaske
b6390 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26  d) */.  if (!(b&
b63a0 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61  0x80)).  {.    a
b63b0 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20   &= 0x7f;.    a 
b63c0 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d  = a<<7;.    a |=
b63d0 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a   b;.    *v = a;.
b63e0 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
b63f0 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20  }..  p++;.  a = 
b6400 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70  a<<14;.  a |= *p
b6410 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34  ;.  /* a: p0<<14
b6420 20 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29   | p2 (unmasked)
b6430 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78   */.  if (!(a&0x
b6440 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26  80)).  {.    a &
b6450 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
b6460 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 78  7f);.    b &= 0x
b6470 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37  7f;.    b = b<<7
b6480 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
b6490 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65    *v = a;.    re
b64a0 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 3;.  }..  /
b64b0 2a 20 43 53 45 31 20 66 72 6f 6d 20 62 65 6c 6f  * CSE1 from belo
b64c0 77 20 2a 2f 0a 20 20 61 20 26 3d 20 28 30 78 37  w */.  a &= (0x7
b64d0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b64e0 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31   p++;.  b = b<<1
b64f0 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  b |= *p;.  
b6500 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70  /* b: p1<<14 | p
b6510 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  3 (unmasked) */.
b6520 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29    if (!(b&0x80))
b6530 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30  .  {.    b &= (0
b6540 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b6550 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53  .    /* moved CS
b6560 45 31 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20  E1 up */.    /* 
b6570 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
b6580 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61  (0x7f); */.    a
b6590 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = a<<7;.    a |
b65a0 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d 20 61 3b  = b;.    *v = a;
b65b0 0a 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a 20  .    return 4;. 
b65c0 20 7d 0a 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c   }..  /* a: p0<<
b65d0 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29  14 | p2 (masked)
b65e0 20 2a 2f 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c   */.  /* b: p1<<
b65f0 31 34 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65  14 | p3 (unmaske
b6600 64 29 20 2a 2f 0a 20 20 2f 2a 20 31 3a 73 61 76  d) */.  /* 1:sav
b6610 65 20 6f 66 66 20 70 30 3c 3c 32 31 20 7c 20 70  e off p0<<21 | p
b6620 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20  1<<14 | p2<<7 | 
b6630 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20  p3 (masked) */. 
b6640 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75   /* moved CSE1 u
b6650 70 20 2a 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28  p */.  /* a &= (
b6660 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b6670 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30 78 37  ; */.  b &= (0x7
b6680 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b6690 20 73 20 3d 20 61 3b 0a 20 20 2f 2a 20 73 3a 20   s = a;.  /* s: 
b66a0 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 6d 61 73  p0<<14 | p2 (mas
b66b0 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a  ked) */..  p++;.
b66c0 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61    a = a<<14;.  a
b66d0 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20   |= *p;.  /* a: 
b66e0 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20  p0<<28 | p2<<14 
b66f0 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 20  | p4 (unmasked) 
b6700 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  */.  if (!(a&0x8
b6710 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 77  0)).  {.    /* w
b6720 65 20 63 61 6e 20 73 6b 69 70 20 74 68 65 73 65  e can skip these
b6730 20 63 61 75 73 65 20 74 68 65 79 20 77 65 72 65   cause they were
b6740 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64   (effectively) d
b6750 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c  one above in cal
b6760 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f  c'ing s */.    /
b6770 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 38  * a &= (0x7f<<28
b6780 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  )|(0x7f<<14)|(0x
b6790 37 66 29 3b 20 2a 2f 0a 20 20 20 20 2f 2a 20 62  7f); */.    /* b
b67a0 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
b67b0 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 62 20  0x7f); */.    b 
b67c0 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d  = b<<7;.    a |=
b67d0 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31   b;.    s = s>>1
b67e0 38 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36  8;.    *v = ((u6
b67f0 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20  4)s)<<32 | a;.  
b6800 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 7d 0a    return 5;.  }.
b6810 0a 20 20 2f 2a 20 32 3a 73 61 76 65 20 6f 66 66  .  /* 2:save off
b6820 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34   p0<<21 | p1<<14
b6830 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d   | p2<<7 | p3 (m
b6840 61 73 6b 65 64 29 20 2a 2f 0a 20 20 73 20 3d 20  asked) */.  s = 
b6850 73 3c 3c 37 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a  s<<7;.  s |= b;.
b6860 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 32 31 20 7c    /* s: p0<<21 |
b6870 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c 3c 37 20   p1<<14 | p2<<7 
b6880 7c 20 70 33 20 28 6d 61 73 6b 65 64 29 20 2a 2f  | p3 (masked) */
b6890 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20 62  ..  p++;.  b = b
b68a0 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b  <<14;.  b |= *p;
b68b0 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 32 38 20  .  /* b: p1<<28 
b68c0 7c 20 70 33 3c 3c 31 34 20 7c 20 70 35 20 28 75  | p3<<14 | p5 (u
b68d0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
b68e0 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b   (!(b&0x80)).  {
b68f0 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20 73  .    /* we can s
b6900 6b 69 70 20 74 68 69 73 20 63 61 75 73 65 20 69  kip this cause i
b6910 74 20 77 61 73 20 28 65 66 66 65 63 74 69 76 65  t was (effective
b6920 6c 79 29 20 64 6f 6e 65 20 61 62 6f 76 65 20 69  ly) done above i
b6930 6e 20 63 61 6c 63 27 69 6e 67 20 73 20 2a 2f 0a  n calc'ing s */.
b6940 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30 78 37      /* b &= (0x7
b6950 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34  f<<28)|(0x7f<<14
b6960 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20  )|(0x7f); */.   
b6970 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
b6980 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d  |(0x7f);.    a =
b6990 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20   a<<7;.    a |= 
b69a0 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e 31 38  b;.    s = s>>18
b69b0 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36 34  ;.    *v = ((u64
b69c0 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20  )s)<<32 | a;.   
b69d0 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a   return 6;.  }..
b69e0 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c    p++;.  a = a<<
b69f0 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20  14;.  a |= *p;. 
b6a00 20 2f 2a 20 61 3a 20 70 32 3c 3c 32 38 20 7c 20   /* a: p2<<28 | 
b6a10 70 34 3c 3c 31 34 20 7c 20 70 36 20 28 75 6e 6d  p4<<14 | p6 (unm
b6a20 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
b6a30 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(a&0x80)).  {. 
b6a40 20 20 20 61 20 26 3d 20 28 30 78 31 66 3c 3c 32     a &= (0x1f<<2
b6a50 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
b6a60 78 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 28  x7f);.    b &= (
b6a70 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b6a80 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a  ;.    b = b<<7;.
b6a90 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20      a |= b;.    
b6aa0 73 20 3d 20 73 3e 3e 31 31 3b 0a 20 20 20 20 2a  s = s>>11;.    *
b6ab0 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32  v = ((u64)s)<<32
b6ac0 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e   | a;.    return
b6ad0 20 37 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53   7;.  }..  /* CS
b6ae0 45 32 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f  E2 from below */
b6af0 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31  .  a &= (0x7f<<1
b6b00 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b  4)|(0x7f);.  p++
b6b10 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20  ;.  b = b<<14;. 
b6b20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62   b |= *p;.  /* b
b6b30 3a 20 70 33 3c 3c 32 38 20 7c 20 70 35 3c 3c 31  : p3<<28 | p5<<1
b6b40 34 20 7c 20 70 37 20 28 75 6e 6d 61 73 6b 65 64  4 | p7 (unmasked
b6b50 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30  ) */.  if (!(b&0
b6b60 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 62 20  x80)).  {.    b 
b6b70 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30  &= (0x1f<<28)|(0
b6b80 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b6b90 0a 20 20 20 20 2f 2a 20 6d 6f 76 65 64 20 43 53  .    /* moved CS
b6ba0 45 32 20 75 70 20 2a 2f 0a 20 20 20 20 2f 2a 20  E2 up */.    /* 
b6bb0 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c  a &= (0x7f<<14)|
b6bc0 28 30 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61  (0x7f); */.    a
b6bd0 20 3d 20 61 3c 3c 37 3b 0a 20 20 20 20 61 20 7c   = a<<7;.    a |
b6be0 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e 3e  = b;.    s = s>>
b6bf0 34 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28 75 36  4;.    *v = ((u6
b6c00 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 20 20  4)s)<<32 | a;.  
b6c10 20 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 7d 0a    return 8;.  }.
b6c20 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c  .  p++;.  a = a<
b6c30 3c 31 35 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a  <15;.  a |= *p;.
b6c40 20 20 2f 2a 20 61 3a 20 70 34 3c 3c 32 39 20 7c    /* a: p4<<29 |
b6c50 20 70 36 3c 3c 31 35 20 7c 20 70 38 20 28 75 6e   p6<<15 | p8 (un
b6c60 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a  masked) */..  /*
b6c70 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20 2a   moved CSE2 up *
b6c80 2f 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37  /.  /* a &= (0x7
b6c90 66 3c 3c 32 39 29 7c 28 30 78 37 66 3c 3c 31 35  f<<29)|(0x7f<<15
b6ca0 29 7c 28 30 78 66 66 29 3b 20 2a 2f 0a 20 20 62  )|(0xff); */.  b
b6cb0 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
b6cc0 30 78 37 66 29 3b 0a 20 20 62 20 3d 20 62 3c 3c  0x7f);.  b = b<<
b6cd0 38 3b 0a 20 20 61 20 7c 3d 20 62 3b 0a 0a 20 20  8;.  a |= b;..  
b6ce0 73 20 3d 20 73 3c 3c 34 3b 0a 20 20 62 20 3d 20  s = s<<4;.  b = 
b6cf0 70 5b 2d 34 5d 3b 0a 20 20 62 20 26 3d 20 30 78  p[-4];.  b &= 0x
b6d00 37 66 3b 0a 20 20 62 20 3d 20 62 3e 3e 33 3b 0a  7f;.  b = b>>3;.
b6d10 20 20 73 20 7c 3d 20 62 3b 0a 0a 20 20 2a 76 20    s |= b;..  *v 
b6d20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c  = ((u64)s)<<32 |
b6d30 20 61 3b 0a 0a 20 20 72 65 74 75 72 6e 20 39 3b   a;..  return 9;
b6d40 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61  .}../*.** Read a
b6d50 20 33 32 2d 62 69 74 20 76 61 72 69 61 62 6c 65   32-bit variable
b6d60 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20  -length integer 
b6d70 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72  from memory star
b6d80 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a  ting at p[0]..**
b6d90 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
b6da0 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64  er of bytes read
b6db0 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20  .  The value is 
b6dc0 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2a  stored in *v..**
b6dd0 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 72 69 6e  .** If the varin
b6de0 74 20 73 74 6f 72 65 64 20 69 6e 20 70 5b 30 5d  t stored in p[0]
b6df0 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20   is larger than 
b6e00 63 61 6e 20 66 69 74 20 69 6e 20 61 20 33 32 2d  can fit in a 32-
b6e10 62 69 74 20 75 6e 73 69 67 6e 65 64 0a 2a 2a 20  bit unsigned.** 
b6e20 69 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 73 65  integer, then se
b6e30 74 20 2a 76 20 74 6f 20 30 78 66 66 66 66 66 66  t *v to 0xffffff
b6e40 66 66 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 41 43 52  ff..**.** A MACR
b6e50 4f 20 76 65 72 73 69 6f 6e 2c 20 67 65 74 56 61  O version, getVa
b6e60 72 69 6e 74 33 32 2c 20 69 73 20 70 72 6f 76 69  rint32, is provi
b6e70 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e 65  ded which inline
b6e80 73 20 74 68 65 20 0a 2a 2a 20 73 69 6e 67 6c 65  s the .** single
b6e90 2d 62 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c  -byte case.  All
b6ea0 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65   code should use
b6eb0 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69   the MACRO versi
b6ec0 6f 6e 20 61 73 20 0a 2a 2a 20 74 68 69 73 20 66  on as .** this f
b6ed0 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20  unction assumes 
b6ee0 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  the single-byte 
b6ef0 63 61 73 65 20 68 61 73 20 61 6c 72 65 61 64 79  case has already
b6f00 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a   been handled..*
b6f10 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b6f20 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56 61   u8 sqlite3GetVa
b6f30 72 69 6e 74 33 32 28 63 6f 6e 73 74 20 75 6e 73  rint32(const uns
b6f40 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c 20 75  igned char *p, u
b6f50 33 32 20 2a 76 29 7b 0a 20 20 75 33 32 20 61 2c  32 *v){.  u32 a,
b6f60 62 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 31 2d 62  b;..  /* The 1-b
b6f70 79 74 65 20 63 61 73 65 2e 20 20 4f 76 65 72 77  yte case.  Overw
b6f80 68 65 6c 6d 69 6e 67 6c 79 20 74 68 65 20 6d 6f  helmingly the mo
b6f90 73 74 20 63 6f 6d 6d 6f 6e 2e 20 20 48 61 6e 64  st common.  Hand
b6fa0 6c 65 64 20 69 6e 6c 69 6e 65 0a 20 20 2a 2a 20  led inline.  ** 
b6fb0 62 79 20 74 68 65 20 67 65 74 56 61 72 69 6e 33  by the getVarin3
b6fc0 32 28 29 20 6d 61 63 72 6f 20 2a 2f 0a 20 20 61  2() macro */.  a
b6fd0 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70   = *p;.  /* a: p
b6fe0 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  0 (unmasked) */.
b6ff0 23 69 66 6e 64 65 66 20 67 65 74 56 61 72 69 6e  #ifndef getVarin
b7000 74 33 32 0a 20 20 69 66 20 28 21 28 61 26 30 78  t32.  if (!(a&0x
b7010 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20  80)).  {.    /* 
b7020 56 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 30  Values between 0
b7030 20 61 6e 64 20 31 32 37 20 2a 2f 0a 20 20 20 20   and 127 */.    
b7040 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75  *v = a;.    retu
b7050 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  rn 1;.  }.#endif
b7060 0a 0a 20 20 2f 2a 20 54 68 65 20 32 2d 62 79 74  ..  /* The 2-byt
b7070 65 20 63 61 73 65 20 2a 2f 0a 20 20 70 2b 2b 3b  e case */.  p++;
b7080 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20  .  b = *p;.  /* 
b7090 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 29  b: p1 (unmasked)
b70a0 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78   */.  if (!(b&0x
b70b0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20  80)).  {.    /* 
b70c0 56 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31  Values between 1
b70d0 32 38 20 61 6e 64 20 31 36 33 38 33 20 2a 2f 0a  28 and 16383 */.
b70e0 20 20 20 20 61 20 26 3d 20 30 78 37 66 3b 0a 20      a &= 0x7f;. 
b70f0 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20 20     a = a<<7;.   
b7100 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20   *v = a | b;.   
b7110 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a   return 2;.  }..
b7120 20 20 2f 2a 20 54 68 65 20 33 2d 62 79 74 65 20    /* The 3-byte 
b7130 63 61 73 65 20 2a 2f 0a 20 20 70 2b 2b 3b 0a 20  case */.  p++;. 
b7140 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20   a = a<<14;.  a 
b7150 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70  |= *p;.  /* a: p
b7160 30 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d 61  0<<14 | p2 (unma
b7170 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21  sked) */.  if (!
b7180 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20  (a&0x80)).  {.  
b7190 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74 77    /* Values betw
b71a0 65 65 6e 20 31 36 33 38 34 20 61 6e 64 20 32 30  een 16384 and 20
b71b0 39 37 31 35 31 20 2a 2f 0a 20 20 20 20 61 20 26  97151 */.    a &
b71c0 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
b71d0 37 66 29 3b 0a 20 20 20 20 62 20 26 3d 20 30 78  7f);.    b &= 0x
b71e0 37 66 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37  7f;.    b = b<<7
b71f0 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62  ;.    *v = a | b
b7200 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33 3b 0a  ;.    return 3;.
b7210 20 20 7d 0a 0a 20 20 2f 2a 20 41 20 33 32 2d 62    }..  /* A 32-b
b7220 69 74 20 76 61 72 69 6e 74 20 69 73 20 75 73 65  it varint is use
b7230 64 20 74 6f 20 73 74 6f 72 65 20 73 69 7a 65 20  d to store size 
b7240 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 62  information in b
b7250 74 72 65 65 73 2e 0a 20 20 2a 2a 20 4f 62 6a 65  trees..  ** Obje
b7260 63 74 73 20 61 72 65 20 72 61 72 65 6c 79 20 6c  cts are rarely l
b7270 61 72 67 65 72 20 74 68 61 6e 20 32 4d 69 42 20  arger than 2MiB 
b7280 6c 69 6d 69 74 20 6f 66 20 61 20 33 2d 62 79 74  limit of a 3-byt
b7290 65 20 76 61 72 69 6e 74 2e 0a 20 20 2a 2a 20 41  e varint..  ** A
b72a0 20 33 2d 62 79 74 65 20 76 61 72 69 6e 74 20 69   3-byte varint i
b72b0 73 20 73 75 66 66 69 63 69 65 6e 74 2c 20 66 6f  s sufficient, fo
b72c0 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 72 65  r example, to re
b72d0 63 6f 72 64 20 74 68 65 20 73 69 7a 65 0a 20 20  cord the size.  
b72e0 2a 2a 20 6f 66 20 61 20 31 30 34 38 35 36 39 2d  ** of a 1048569-
b72f0 62 79 74 65 20 42 4c 4f 42 20 6f 72 20 73 74 72  byte BLOB or str
b7300 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  ing..  **.  ** W
b7310 65 20 6f 6e 6c 79 20 75 6e 72 6f 6c 6c 20 74 68  e only unroll th
b7320 65 20 66 69 72 73 74 20 31 2d 2c 20 32 2d 2c 20  e first 1-, 2-, 
b7330 61 6e 64 20 33 2d 20 62 79 74 65 20 63 61 73 65  and 3- byte case
b7340 73 2e 20 20 54 68 65 20 76 65 72 79 0a 20 20 2a  s.  The very.  *
b7350 2a 20 72 61 72 65 20 6c 61 72 67 65 72 20 63 61  * rare larger ca
b7360 73 65 73 20 63 61 6e 20 62 65 20 68 61 6e 64 6c  ses can be handl
b7370 65 64 20 62 79 20 74 68 65 20 73 6c 6f 77 65 72  ed by the slower
b7380 20 36 34 2d 62 69 74 20 76 61 72 69 6e 74 0a 20   64-bit varint. 
b7390 20 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a 20 20 2a   ** routine..  *
b73a0 2f 0a 23 69 66 20 31 0a 20 20 7b 0a 20 20 20 20  /.#if 1.  {.    
b73b0 75 36 34 20 76 36 34 3b 0a 20 20 20 20 75 38 20  u64 v64;.    u8 
b73c0 6e 3b 0a 0a 20 20 20 20 70 20 2d 3d 20 32 3b 0a  n;..    p -= 2;.
b73d0 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47      n = sqlite3G
b73e0 65 74 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34  etVarint(p, &v64
b73f0 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  );.    assert( n
b7400 3e 33 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20  >3 && n<=9 );.  
b7410 20 20 69 66 28 20 28 76 36 34 20 26 20 53 51 4c    if( (v64 & SQL
b7420 49 54 45 5f 4d 41 58 5f 55 33 32 29 21 3d 76 36  ITE_MAX_U32)!=v6
b7430 34 20 29 7b 0a 20 20 20 20 20 20 2a 76 20 3d 20  4 ){.      *v = 
b7440 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 20 20  0xffffffff;.    
b7450 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2a 76 20  }else{.      *v 
b7460 3d 20 28 75 33 32 29 76 36 34 3b 0a 20 20 20 20  = (u32)v64;.    
b7470 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a  }.    return n;.
b7480 20 20 7d 0a 0a 23 65 6c 73 65 0a 20 20 2f 2a 20    }..#else.  /* 
b7490 46 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  For following co
b74a0 64 65 20 28 6b 65 70 74 20 66 6f 72 20 68 69 73  de (kept for his
b74b0 74 6f 72 69 63 61 6c 20 72 65 63 6f 72 64 20 6f  torical record o
b74c0 6e 6c 79 29 20 73 68 6f 77 73 20 61 6e 0a 20 20  nly) shows an.  
b74d0 2a 2a 20 75 6e 72 6f 6c 6c 69 6e 67 20 66 6f 72  ** unrolling for
b74e0 20 74 68 65 20 33 2d 20 61 6e 64 20 34 2d 62 79   the 3- and 4-by
b74f0 74 65 20 76 61 72 69 6e 74 20 63 61 73 65 73 2e  te varint cases.
b7500 20 20 54 68 69 73 20 63 6f 64 65 20 69 73 0a 20    This code is. 
b7510 20 2a 2a 20 73 6c 69 67 68 74 6c 79 20 66 61 73   ** slightly fas
b7520 74 65 72 2c 20 62 75 74 20 69 74 20 69 73 20 61  ter, but it is a
b7530 6c 73 6f 20 6c 61 72 67 65 72 20 61 6e 64 20 6d  lso larger and m
b7540 75 63 68 20 68 61 72 64 65 72 20 74 6f 20 74 65  uch harder to te
b7550 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 2b 2b 3b 0a  st..  */.  p++;.
b7560 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62    b = b<<14;.  b
b7570 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20   |= *p;.  /* b: 
b7580 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d  p1<<14 | p3 (unm
b7590 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
b75a0 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(b&0x80)).  {. 
b75b0 20 20 20 2f 2a 20 56 61 6c 75 65 73 20 62 65 74     /* Values bet
b75c0 77 65 65 6e 20 32 30 39 37 31 35 32 20 61 6e 64  ween 2097152 and
b75d0 20 32 36 38 34 33 35 34 35 35 20 2a 2f 0a 20 20   268435455 */.  
b75e0 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34    b &= (0x7f<<14
b75f0 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20  )|(0x7f);.    a 
b7600 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  &= (0x7f<<14)|(0
b7610 78 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c  x7f);.    a = a<
b7620 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20 7c  <7;.    *v = a |
b7630 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 34   b;.    return 4
b7640 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20  ;.  }..  p++;.  
b7650 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c  a = a<<14;.  a |
b7660 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30  = *p;.  /* a: p0
b7670 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20  <<28 | p2<<14 | 
b7680 70 34 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p4 (unmasked) */
b7690 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29  .  if (!(a&0x80)
b76a0 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 57 61 6c  ).  {.    /* Wal
b76b0 75 65 73 20 20 62 65 74 77 65 65 6e 20 32 36 38  ues  between 268
b76c0 34 33 35 34 35 36 20 61 6e 64 20 33 34 33 35 39  435456 and 34359
b76d0 37 33 38 33 36 37 20 2a 2f 0a 20 20 20 20 61 20  738367 */.    a 
b76e0 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30  &= (0x1f<<28)|(0
b76f0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b7700 0a 20 20 20 20 62 20 26 3d 20 28 30 78 31 66 3c  .    b &= (0x1f<
b7710 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c  <28)|(0x7f<<14)|
b7720 28 30 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20  (0x7f);.    b = 
b7730 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61  b<<7;.    *v = a
b7740 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e   | b;.    return
b7750 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65   5;.  }..  /* We
b7760 20 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63 68 20   can only reach 
b7770 74 68 69 73 20 70 6f 69 6e 74 20 77 68 65 6e 20  this point when 
b7780 72 65 61 64 69 6e 67 20 61 20 63 6f 72 72 75 70  reading a corrup
b7790 74 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  t database.  ** 
b77a0 66 69 6c 65 2e 20 20 49 6e 20 74 68 61 74 20 63  file.  In that c
b77b0 61 73 65 20 77 65 20 61 72 65 20 6e 6f 74 20 69  ase we are not i
b77c0 6e 20 61 6e 79 20 68 75 72 72 79 2e 20 20 55 73  n any hurry.  Us
b77d0 65 20 74 68 65 20 28 72 65 6c 61 74 69 76 65 6c  e the (relativel
b77e0 79 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 67 65 6e  y.  ** slow) gen
b77f0 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 73 71 6c  eral-purpose sql
b7800 69 74 65 33 47 65 74 56 61 72 69 6e 74 28 29 20  ite3GetVarint() 
b7810 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61  routine to extra
b7820 63 74 20 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75  ct the.  ** valu
b7830 65 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 36  e. */.  {.    u6
b7840 34 20 76 36 34 3b 0a 20 20 20 20 75 38 20 6e 3b  4 v64;.    u8 n;
b7850 0a 0a 20 20 20 20 70 20 2d 3d 20 34 3b 0a 20 20  ..    p -= 4;.  
b7860 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74    n = sqlite3Get
b7870 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34 29 3b  Varint(p, &v64);
b7880 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 35  .    assert( n>5
b7890 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 20 20   && n<=9 );.    
b78a0 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a 20  *v = (u32)v64;. 
b78b0 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d     return n;.  }
b78c0 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
b78d0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
b78e0 65 72 20 6f 66 20 62 79 74 65 73 20 74 68 61 74  er of bytes that
b78f0 20 77 69 6c 6c 20 62 65 20 6e 65 65 64 65 64 20   will be needed 
b7900 74 6f 20 73 74 6f 72 65 20 74 68 65 20 67 69 76  to store the giv
b7910 65 6e 0a 2a 2a 20 36 34 2d 62 69 74 20 69 6e 74  en.** 64-bit int
b7920 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eger..*/.SQLITE_
b7930 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b7940 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34  te3VarintLen(u64
b7950 20 76 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 30   v){.  int i = 0
b7960 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 69 2b 2b 3b  ;.  do{.    i++;
b7970 0a 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20  .    v >>= 7;.  
b7980 7d 77 68 69 6c 65 28 20 76 21 3d 30 20 26 26 20  }while( v!=0 && 
b7990 41 4c 57 41 59 53 28 69 3c 39 29 20 29 3b 0a 20  ALWAYS(i<9) );. 
b79a0 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 0a 2f   return i;.}.../
b79b0 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72 69  *.** Read or wri
b79c0 74 65 20 61 20 66 6f 75 72 2d 62 79 74 65 20 62  te a four-byte b
b79d0 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
b79e0 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49  r value..*/.SQLI
b79f0 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
b7a00 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 63  qlite3Get4byte(c
b7a10 6f 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20 20 72  onst u8 *p){.  r
b7a20 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32 34 29  eturn (p[0]<<24)
b7a30 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20 7c 20   | (p[1]<<16) | 
b7a40 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b 33 5d  (p[2]<<8) | p[3]
b7a50 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
b7a60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
b7a70 75 74 34 62 79 74 65 28 75 6e 73 69 67 6e 65 64  ut4byte(unsigned
b7a80 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29   char *p, u32 v)
b7a90 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28  {.  p[0] = (u8)(
b7aa0 76 3e 3e 32 34 29 3b 0a 20 20 70 5b 31 5d 20 3d  v>>24);.  p[1] =
b7ab0 20 28 75 38 29 28 76 3e 3e 31 36 29 3b 0a 20 20   (u8)(v>>16);.  
b7ac0 70 5b 32 5d 20 3d 20 28 75 38 29 28 76 3e 3e 38  p[2] = (u8)(v>>8
b7ad0 29 3b 0a 20 20 70 5b 33 5d 20 3d 20 28 75 38 29  );.  p[3] = (u8)
b7ae0 76 3b 0a 7d 0a 0a 0a 0a 23 69 66 20 21 64 65 66  v;.}....#if !def
b7af0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
b7b00 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c  _BLOB_LITERAL) |
b7b10 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
b7b20 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a  _HAS_CODEC)./*.*
b7b30 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69  * Translate a si
b7b40 6e 67 6c 65 20 62 79 74 65 20 6f 66 20 48 65 78  ngle byte of Hex
b7b50 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72   into an integer
b7b60 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
b7b70 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20  e only works if 
b7b80 68 20 72 65 61 6c 6c 79 20 69 73 20 61 20 76 61  h really is a va
b7b90 6c 69 64 20 68 65 78 61 64 65 63 69 6d 61 6c 0a  lid hexadecimal.
b7ba0 2a 2a 20 63 68 61 72 61 63 74 65 72 3a 20 20 30  ** character:  0
b7bb0 2e 2e 39 61 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 73  ..9a..fA..F.*/.s
b7bc0 74 61 74 69 63 20 75 38 20 68 65 78 54 6f 49 6e  tatic u8 hexToIn
b7bd0 74 28 69 6e 74 20 68 29 7b 0a 20 20 61 73 73 65  t(int h){.  asse
b7be0 72 74 28 20 28 68 3e 3d 27 30 27 20 26 26 20 68  rt( (h>='0' && h
b7bf0 3c 3d 27 39 27 29 20 7c 7c 20 20 28 68 3e 3d 27  <='9') ||  (h>='
b7c00 61 27 20 26 26 20 68 3c 3d 27 66 27 29 20 7c 7c  a' && h<='f') ||
b7c10 20 20 28 68 3e 3d 27 41 27 20 26 26 20 68 3c 3d    (h>='A' && h<=
b7c20 27 46 27 29 20 29 3b 0a 23 69 66 64 65 66 20 53  'F') );.#ifdef S
b7c30 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 68 20  QLITE_ASCII.  h 
b7c40 2b 3d 20 39 2a 28 31 26 28 68 3e 3e 36 29 29 3b  += 9*(1&(h>>6));
b7c50 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
b7c60 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 20 20 68  QLITE_EBCDIC.  h
b7c70 20 2b 3d 20 39 2a 28 31 26 7e 28 68 3e 3e 34 29   += 9*(1&~(h>>4)
b7c80 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  );.#endif.  retu
b7c90 72 6e 20 28 75 38 29 28 68 20 26 20 30 78 66 29  rn (u8)(h & 0xf)
b7ca0 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  ;.}.#endif /* !S
b7cb0 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
b7cc0 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54  LITERAL || SQLIT
b7cd0 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a  E_HAS_CODEC */..
b7ce0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
b7cf0 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49  ITE_OMIT_BLOB_LI
b7d00 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69 6e 65  TERAL) || define
b7d10 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  d(SQLITE_HAS_COD
b7d20 45 43 29 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  EC)./*.** Conver
b7d30 74 20 61 20 42 4c 4f 42 20 6c 69 74 65 72 61 6c  t a BLOB literal
b7d40 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 27   of the form "x'
b7d50 68 68 68 68 68 68 27 22 20 69 6e 74 6f 20 69 74  hhhhhh'" into it
b7d60 73 20 62 69 6e 61 72 79 0a 2a 2a 20 76 61 6c 75  s binary.** valu
b7d70 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  e.  Return a poi
b7d80 6e 74 65 72 20 74 6f 20 69 74 73 20 62 69 6e 61  nter to its bina
b7d90 72 79 20 76 61 6c 75 65 2e 20 20 53 70 61 63 65  ry value.  Space
b7da0 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20   to hold the.** 
b7db0 62 69 6e 61 72 79 20 76 61 6c 75 65 20 68 61 73  binary value has
b7dc0 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 66   been obtained f
b7dd0 72 6f 6d 20 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d  rom malloc and m
b7de0 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79 0a  ust be freed by.
b7df0 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72  ** the calling r
b7e00 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54  outine..*/.SQLIT
b7e10 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
b7e20 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62  sqlite3HexToBlob
b7e30 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
b7e40 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
b7e50 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 42 6c   n){.  char *zBl
b7e60 6f 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  ob;.  int i;..  
b7e70 7a 42 6c 6f 62 20 3d 20 28 63 68 61 72 20 2a 29  zBlob = (char *)
b7e80 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
b7e90 61 77 28 64 62 2c 20 6e 2f 32 20 2b 20 31 29 3b  aw(db, n/2 + 1);
b7ea0 0a 20 20 6e 2d 2d 3b 0a 20 20 69 66 28 20 7a 42  .  n--;.  if( zB
b7eb0 6c 6f 62 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  lob ){.    for(i
b7ec0 3d 30 3b 20 69 3c 6e 3b 20 69 2b 3d 32 29 7b 0a  =0; i<n; i+=2){.
b7ed0 20 20 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d        zBlob[i/2]
b7ee0 20 3d 20 28 68 65 78 54 6f 49 6e 74 28 7a 5b 69   = (hexToInt(z[i
b7ef0 5d 29 3c 3c 34 29 20 7c 20 68 65 78 54 6f 49 6e  ])<<4) | hexToIn
b7f00 74 28 7a 5b 69 2b 31 5d 29 3b 0a 20 20 20 20 7d  t(z[i+1]);.    }
b7f10 0a 20 20 20 20 7a 42 6c 6f 62 5b 69 2f 32 5d 20  .    zBlob[i/2] 
b7f20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 0;.  }.  retur
b7f30 6e 20 7a 42 6c 6f 62 3b 0a 7d 0a 23 65 6e 64 69  n zBlob;.}.#endi
b7f40 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49  f /* !SQLITE_OMI
b7f50 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c  T_BLOB_LITERAL |
b7f60 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  | SQLITE_HAS_COD
b7f70 45 43 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68  EC */.../*.** Ch
b7f80 61 6e 67 65 20 74 68 65 20 73 71 6c 69 74 65 2e  ange the sqlite.
b7f90 6d 61 67 69 63 20 66 72 6f 6d 20 53 51 4c 49 54  magic from SQLIT
b7fa0 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 74 6f 20  E_MAGIC_OPEN to 
b7fb0 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
b7fc0 59 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20  Y..** Return an 
b7fd0 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f 29  error (non-zero)
b7fe0 20 69 66 20 74 68 65 20 6d 61 67 69 63 20 77 61   if the magic wa
b7ff0 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41 47  s not SQLITE_MAG
b8000 49 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20  IC_OPEN.** when 
b8010 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
b8020 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  called..**.** Th
b8030 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
b8040 6c 6c 65 64 20 77 68 65 6e 20 65 6e 74 65 72 69  lled when enteri
b8050 6e 67 20 61 6e 20 53 51 4c 69 74 65 20 41 50 49  ng an SQLite API
b8060 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 41  .  The SQLITE_MA
b8070 47 49 43 5f 4f 50 45 4e 0a 2a 2a 20 76 61 6c 75  GIC_OPEN.** valu
b8080 65 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  e indicates that
b8090 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
b80a0 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73 65 64 20  nnection passed 
b80b0 69 6e 74 6f 20 74 68 65 20 41 50 49 20 69 73 0a  into the API is.
b80c0 2a 2a 20 6f 70 65 6e 20 61 6e 64 20 69 73 20 6e  ** open and is n
b80d0 6f 74 20 62 65 69 6e 67 20 75 73 65 64 20 62 79  ot being used by
b80e0 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 2e   another thread.
b80f0 20 20 42 79 20 63 68 61 6e 67 69 6e 67 20 74 68    By changing th
b8100 65 20 76 61 6c 75 65 0a 2a 2a 20 74 6f 20 53 51  e value.** to SQ
b8110 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
b8120 77 65 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  we indicate that
b8130 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
b8140 69 73 20 69 6e 20 75 73 65 2e 0a 2a 2a 20 73 71  is in use..** sq
b8150 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 29  lite3SafetyOff()
b8160 20 62 65 6c 6f 77 20 77 69 6c 6c 20 63 68 61 6e   below will chan
b8170 67 65 20 74 68 65 20 76 61 6c 75 65 20 62 61 63  ge the value bac
b8180 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49  k to SQLITE_MAGI
b8190 43 5f 4f 50 45 4e 0a 2a 2a 20 77 68 65 6e 20 74  C_OPEN.** when t
b81a0 68 65 20 41 50 49 20 65 78 69 74 73 2e 20 0a 2a  he API exits. .*
b81b0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
b81c0 65 20 69 73 20 61 20 61 74 74 65 6d 70 74 20 74  e is a attempt t
b81d0 6f 20 64 65 74 65 63 74 20 69 66 20 74 77 6f 20  o detect if two 
b81e0 74 68 72 65 61 64 73 20 75 73 65 20 74 68 65 0a  threads use the.
b81f0 2a 2a 20 73 61 6d 65 20 73 71 6c 69 74 65 2a 20  ** same sqlite* 
b8200 70 6f 69 6e 74 65 72 20 61 74 20 74 68 65 20 73  pointer at the s
b8210 61 6d 65 20 74 69 6d 65 2e 20 20 54 68 65 72 65  ame time.  There
b8220 20 69 73 20 61 20 72 61 63 65 20 0a 2a 2a 20 63   is a race .** c
b8230 6f 6e 64 69 74 69 6f 6e 20 73 6f 20 69 74 20 69  ondition so it i
b8240 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20  s possible that 
b8250 74 68 65 20 65 72 72 6f 72 20 69 73 20 6e 6f 74  the error is not
b8260 20 64 65 74 65 63 74 65 64 2e 0a 2a 2a 20 42 75   detected..** Bu
b8270 74 20 75 73 75 61 6c 6c 79 20 74 68 65 20 70 72  t usually the pr
b8280 6f 62 6c 65 6d 20 77 69 6c 6c 20 62 65 20 73 65  oblem will be se
b8290 65 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20  en.  The result 
b82a0 77 69 6c 6c 20 62 65 20 61 6e 0a 2a 2a 20 65 72  will be an.** er
b82b0 72 6f 72 20 77 68 69 63 68 20 63 61 6e 20 62 65  ror which can be
b82c0 20 75 73 65 64 20 74 6f 20 64 65 62 75 67 20 74   used to debug t
b82d0 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 74  he application t
b82e0 68 61 74 20 69 73 0a 2a 2a 20 75 73 69 6e 67 20  hat is.** using 
b82f0 53 51 4c 69 74 65 20 69 6e 63 6f 72 72 65 63 74  SQLite incorrect
b8300 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74  ly..**.** Ticket
b8310 20 23 32 30 32 3a 20 20 49 66 20 64 62 2d 3e 6d   #202:  If db->m
b8320 61 67 69 63 20 69 73 20 6e 6f 74 20 61 20 76 61  agic is not a va
b8330 6c 69 64 20 6f 70 65 6e 20 76 61 6c 75 65 2c 20  lid open value, 
b8340 74 61 6b 65 20 63 61 72 65 20 6e 6f 74 0a 2a 2a  take care not.**
b8350 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64   to modify the d
b8360 62 20 73 74 72 75 63 74 75 72 65 20 61 74 20 61  b structure at a
b8370 6c 6c 2e 20 20 49 74 20 63 6f 75 6c 64 20 62 65  ll.  It could be
b8380 20 74 68 61 74 20 64 62 20 69 73 20 61 20 73 74   that db is a st
b8390 61 6c 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20  ale.** pointer. 
b83a0 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
b83b0 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61   it could be tha
b83c0 74 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e  t there has been
b83d0 20 61 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c   a prior.** call
b83e0 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73   to sqlite3_clos
b83f0 65 28 64 62 29 20 61 6e 64 20 64 62 20 68 61 73  e(db) and db has
b8400 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65   been deallocate
b8410 64 2e 20 20 41 6e 64 20 77 65 20 64 6f 0a 2a 2a  d.  And we do.**
b8420 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 77 72 69   not want to wri
b8430 74 65 20 69 6e 74 6f 20 64 65 61 6c 6c 6f 63 61  te into dealloca
b8440 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 23  ted memory..*/.#
b8450 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
b8460 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
b8470 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66  E int sqlite3Saf
b8480 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 20 2a 64  etyOn(sqlite3 *d
b8490 62 29 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  b){.  if( db->ma
b84a0 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49  gic==SQLITE_MAGI
b84b0 43 5f 4f 50 45 4e 20 29 7b 0a 20 20 20 20 64 62  C_OPEN ){.    db
b84c0 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45  ->magic = SQLITE
b84d0 5f 4d 41 47 49 43 5f 42 55 53 59 3b 0a 20 20 20  _MAGIC_BUSY;.   
b84e0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
b84f0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
b8500 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 72 65  mutex) );.    re
b8510 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 20  turn 0;.  }else 
b8520 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53  if( db->magic==S
b8530 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
b8540 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69   ){.    db->magi
b8550 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
b8560 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62 2d 3e  _ERROR;.    db->
b8570 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
b8580 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
b8590 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  rn 1;.}.#endif..
b85a0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
b85b0 20 6d 61 67 69 63 20 66 72 6f 6d 20 53 51 4c 49   magic from SQLI
b85c0 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 74 6f  TE_MAGIC_BUSY to
b85d0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
b85e0 45 4e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e  EN..** Return an
b85f0 20 65 72 72 6f 72 20 28 6e 6f 6e 2d 7a 65 72 6f   error (non-zero
b8600 29 20 69 66 20 74 68 65 20 6d 61 67 69 63 20 77  ) if the magic w
b8610 61 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4d 41  as not SQLITE_MA
b8620 47 49 43 5f 42 55 53 59 0a 2a 2a 20 77 68 65 6e  GIC_BUSY.** when
b8630 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
b8640 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 23 69 66 64   called..*/.#ifd
b8650 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
b8660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
b8670 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  nt sqlite3Safety
b8680 4f 66 66 28 73 71 6c 69 74 65 33 20 2a 64 62 29  Off(sqlite3 *db)
b8690 7b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 67 69  {.  if( db->magi
b86a0 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c==SQLITE_MAGIC_
b86b0 42 55 53 59 20 29 7b 0a 20 20 20 20 64 62 2d 3e  BUSY ){.    db->
b86c0 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d  magic = SQLITE_M
b86d0 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 20 20 20 61  AGIC_OPEN;.    a
b86e0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
b86f0 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
b8700 74 65 78 29 20 29 3b 0a 20 20 20 20 72 65 74 75  tex) );.    retu
b8710 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rn 0;.  }else{. 
b8720 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53     db->magic = S
b8730 51 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f  QLITE_MAGIC_ERRO
b8740 52 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73  R;.    db->u1.is
b8750 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b  Interrupted = 1;
b8760 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
b8770 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   }.}.#endif../*.
b8780 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65  ** Check to make
b8790 20 73 75 72 65 20 77 65 20 68 61 76 65 20 61 20   sure we have a 
b87a0 76 61 6c 69 64 20 64 62 20 70 6f 69 6e 74 65 72  valid db pointer
b87b0 2e 20 20 54 68 69 73 20 74 65 73 74 20 69 73 20  .  This test is 
b87c0 6e 6f 74 0a 2a 2a 20 66 6f 6f 6c 70 72 6f 6f 66  not.** foolproof
b87d0 20 62 75 74 20 69 74 20 64 6f 65 73 20 70 72 6f   but it does pro
b87e0 76 69 64 65 20 73 6f 6d 65 20 6d 65 61 73 75 72  vide some measur
b87f0 65 20 6f 66 20 70 72 6f 74 65 63 74 69 6f 6e 20  e of protection 
b8800 61 67 61 69 6e 73 74 0a 2a 2a 20 6d 69 73 75 73  against.** misus
b8810 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 66 61  e of the interfa
b8820 63 65 20 73 75 63 68 20 61 73 20 70 61 73 73 69  ce such as passi
b8830 6e 67 20 69 6e 20 64 62 20 70 6f 69 6e 74 65 72  ng in db pointer
b8840 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 4e 55  s that are.** NU
b8850 4c 4c 20 6f 72 20 77 68 69 63 68 20 68 61 76 65  LL or which have
b8860 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79   been previously
b8870 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69   closed.  If thi
b8880 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
b8890 73 0a 2a 2a 20 31 20 69 74 20 6d 65 61 6e 73 20  s.** 1 it means 
b88a0 74 68 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e  that the db poin
b88b0 74 65 72 20 69 73 20 76 61 6c 69 64 20 61 6e 64  ter is valid and
b88c0 20 30 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20   0 if it should 
b88d0 6e 6f 74 20 62 65 0a 2a 2a 20 64 65 72 65 66 65  not be.** derefe
b88e0 72 65 6e 63 65 64 20 66 6f 72 20 61 6e 79 20 72  renced for any r
b88f0 65 61 73 6f 6e 2e 20 20 54 68 65 20 63 61 6c 6c  eason.  The call
b8900 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  ing function sho
b8910 75 6c 64 20 69 6e 76 6f 6b 65 0a 2a 2a 20 53 51  uld invoke.** SQ
b8920 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 6d 6d 65  LITE_MISUSE imme
b8930 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 73  diately..**.** s
b8940 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
b8950 6b 4f 6b 28 29 20 72 65 71 75 69 72 65 73 20 74  kOk() requires t
b8960 68 61 74 20 74 68 65 20 64 62 20 70 6f 69 6e 74  hat the db point
b8970 65 72 20 62 65 20 76 61 6c 69 64 20 66 6f 72 0a  er be valid for.
b8980 2a 2a 20 75 73 65 2e 20 20 73 71 6c 69 74 65 33  ** use.  sqlite3
b8990 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f  SafetyCheckSickO
b89a0 72 4f 6b 28 29 20 61 6c 6c 6f 77 73 20 61 20 64  rOk() allows a d
b89b0 62 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 66  b pointer that f
b89c0 61 69 6c 65 64 20 74 6f 0a 2a 2a 20 6f 70 65 6e  ailed to.** open
b89d0 20 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 69 73   properly and is
b89e0 20 6e 6f 74 20 66 69 74 20 66 6f 72 20 67 65 6e   not fit for gen
b89f0 65 72 61 6c 20 75 73 65 20 62 75 74 20 77 68 69  eral use but whi
b8a00 63 68 20 63 61 6e 20 62 65 0a 2a 2a 20 75 73 65  ch can be.** use
b8a10 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
b8a20 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 72 72 6d   to sqlite3_errm
b8a30 73 67 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  sg() or sqlite3_
b8a40 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49  close()..*/.SQLI
b8a50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b8a60 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
b8a70 6b 4f 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29  kOk(sqlite3 *db)
b8a80 7b 0a 20 20 75 33 32 20 6d 61 67 69 63 3b 0a 20  {.  u32 magic;. 
b8a90 20 69 66 28 20 64 62 3d 3d 30 20 29 20 72 65 74   if( db==0 ) ret
b8aa0 75 72 6e 20 30 3b 0a 20 20 6d 61 67 69 63 20 3d  urn 0;.  magic =
b8ab0 20 64 62 2d 3e 6d 61 67 69 63 3b 0a 20 20 69 66   db->magic;.  if
b8ac0 28 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f  ( magic!=SQLITE_
b8ad0 4d 41 47 49 43 5f 4f 50 45 4e 20 0a 23 69 66 64  MAGIC_OPEN .#ifd
b8ae0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
b8af0 20 20 20 20 20 26 26 20 6d 61 67 69 63 21 3d 53       && magic!=S
b8b00 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
b8b10 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20  .#endif.  ){.   
b8b20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
b8b30 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  se{.    return 1
b8b40 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50  ;.  }.}.SQLITE_P
b8b50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b8b60 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63  e3SafetyCheckSic
b8b70 6b 4f 72 4f 6b 28 73 71 6c 69 74 65 33 20 2a 64  kOrOk(sqlite3 *d
b8b80 62 29 7b 0a 20 20 75 33 32 20 6d 61 67 69 63 3b  b){.  u32 magic;
b8b90 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d  .  magic = db->m
b8ba0 61 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69  agic;.  if( magi
b8bb0 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c!=SQLITE_MAGIC_
b8bc0 53 49 43 4b 20 26 26 0a 20 20 20 20 20 20 6d 61  SICK &&.      ma
b8bd0 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49  gic!=SQLITE_MAGI
b8be0 43 5f 4f 50 45 4e 20 26 26 0a 20 20 20 20 20 20  C_OPEN &&.      
b8bf0 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41  magic!=SQLITE_MA
b8c00 47 49 43 5f 42 55 53 59 20 29 20 72 65 74 75 72  GIC_BUSY ) retur
b8c10 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  n 0;.  return 1;
b8c20 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
b8c30 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 69 6c 2e  *** End of util.
b8c40 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
b8c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8c70 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
b8c80 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
b8c90 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.c **********
b8ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8cc0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
b8cd0 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a  eptember 22.**.*
b8ce0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
b8cf0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
b8d00 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
b8d10 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
b8d20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
b8d30 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
b8d40 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
b8d50 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
b8d60 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
b8d70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
b8d80 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
b8d90 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
b8da0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
b8db0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
b8dc0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
b8dd0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
b8de0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
b8df0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
b8e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
b8e40 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d  is is the implem
b8e50 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65  entation of gene
b8e60 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65 73 0a  ric hash-tables.
b8e70 2a 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74  ** used in SQLit
b8e80 65 2e 0a 2a 2f 0a 0a 2f 2a 20 54 75 72 6e 20 62  e..*/../* Turn b
b8e90 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20  ulk memory into 
b8ea0 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 62 6a  a hash table obj
b8eb0 65 63 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a  ect by initializ
b8ec0 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64  ing the.** field
b8ed0 73 20 6f 66 20 74 68 65 20 48 61 73 68 20 73 74  s of the Hash st
b8ee0 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22  ructure..**.** "
b8ef0 70 4e 65 77 22 20 69 73 20 61 20 70 6f 69 6e 74  pNew" is a point
b8f00 65 72 20 74 6f 20 74 68 65 20 68 61 73 68 20 74  er to the hash t
b8f10 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20  able that is to 
b8f20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a  be initialized..
b8f30 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b8f40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61  E void sqlite3Ha
b8f50 73 68 49 6e 69 74 28 48 61 73 68 20 2a 70 4e 65  shInit(Hash *pNe
b8f60 77 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4e  w){.  assert( pN
b8f70 65 77 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77 2d  ew!=0 );.  pNew-
b8f80 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e  >first = 0;.  pN
b8f90 65 77 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20  ew->count = 0;. 
b8fa0 20 70 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20   pNew->htsize = 
b8fb0 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20  0;.  pNew->ht = 
b8fc0 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20  0;.}../* Remove 
b8fd0 61 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d  all entries from
b8fe0 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20   a hash table.  
b8ff0 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f  Reclaim all memo
b9000 72 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73  ry..** Call this
b9010 20 72 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65   routine to dele
b9020 74 65 20 61 20 68 61 73 68 20 74 61 62 6c 65 20  te a hash table 
b9030 6f 72 20 74 6f 20 72 65 73 65 74 20 61 20 68 61  or to reset a ha
b9040 73 68 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74  sh table.** to t
b9050 68 65 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a  he empty state..
b9060 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b9070 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61  E void sqlite3Ha
b9080 73 68 43 6c 65 61 72 28 48 61 73 68 20 2a 70 48  shClear(Hash *pH
b9090 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65  ){.  HashElem *e
b90a0 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  lem;         /* 
b90b0 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
b90c0 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66   all elements of
b90d0 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20   the table */.. 
b90e0 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20 29   assert( pH!=0 )
b90f0 3b 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66  ;.  elem = pH->f
b9100 69 72 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73  irst;.  pH->firs
b9110 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  t = 0;.  sqlite3
b9120 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20  _free(pH->ht);. 
b9130 20 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70   pH->ht = 0;.  p
b9140 48 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20  H->htsize = 0;. 
b9150 20 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a   while( elem ){.
b9160 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65      HashElem *ne
b9170 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e  xt_elem = elem->
b9180 6e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  next;.    sqlite
b9190 33 5f 66 72 65 65 28 65 6c 65 6d 29 3b 0a 20 20  3_free(elem);.  
b91a0 20 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c    elem = next_el
b91b0 65 6d 3b 0a 20 20 7d 0a 20 20 70 48 2d 3e 63 6f  em;.  }.  pH->co
b91c0 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  unt = 0;.}../*.*
b91d0 2a 20 54 68 65 20 68 61 73 68 69 6e 67 20 66 75  * The hashing fu
b91e0 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
b91f0 63 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 73  c unsigned int s
b9200 74 72 48 61 73 68 28 63 6f 6e 73 74 20 63 68 61  trHash(const cha
b9210 72 20 2a 7a 2c 20 69 6e 74 20 6e 4b 65 79 29 7b  r *z, int nKey){
b9220 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20  .  int h = 0;.  
b9230 61 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20  assert( nKey>=0 
b9240 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79  );.  while( nKey
b9250 20 3e 20 30 20 20 29 7b 0a 20 20 20 20 68 20 3d   > 0  ){.    h =
b9260 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71   (h<<3) ^ h ^ sq
b9270 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
b9280 72 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  r[(unsigned char
b9290 29 2a 7a 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79  )*z++];.    nKey
b92a0 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  --;.  }.  return
b92b0 20 68 3b 0a 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b 20   h;.}.../* Link 
b92c0 70 4e 65 77 20 65 6c 65 6d 65 6e 74 20 69 6e 74  pNew element int
b92d0 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
b92e0 20 70 48 2e 20 20 49 66 20 70 45 6e 74 72 79 21   pH.  If pEntry!
b92f0 3d 30 20 74 68 65 6e 20 61 6c 73 6f 0a 2a 2a 20  =0 then also.** 
b9300 69 6e 73 65 72 74 20 70 4e 65 77 20 69 6e 74 6f  insert pNew into
b9310 20 74 68 65 20 70 45 6e 74 72 79 20 68 61 73 68   the pEntry hash
b9320 20 62 75 63 6b 65 74 2e 0a 2a 2f 0a 73 74 61 74   bucket..*/.stat
b9330 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 45 6c  ic void insertEl
b9340 65 6d 65 6e 74 28 0a 20 20 48 61 73 68 20 2a 70  ement(.  Hash *p
b9350 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H,              
b9360 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20  /* The complete 
b9370 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
b9380 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74  struct _ht *pEnt
b9390 72 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 65 6e  ry,    /* The en
b93a0 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20 70  try into which p
b93b0 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64 20  New is inserted 
b93c0 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70  */.  HashElem *p
b93d0 4e 65 77 20 20 20 20 20 20 20 20 20 2f 2a 20 54  New         /* T
b93e0 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65  he element to be
b93f0 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b 0a   inserted */.){.
b9400 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 48 65 61    HashElem *pHea
b9410 64 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73  d;       /* Firs
b9420 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64  t element alread
b9430 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20  y in pEntry */. 
b9440 20 69 66 28 20 70 45 6e 74 72 79 20 29 7b 0a 20   if( pEntry ){. 
b9450 20 20 20 70 48 65 61 64 20 3d 20 70 45 6e 74 72     pHead = pEntr
b9460 79 2d 3e 63 6f 75 6e 74 20 3f 20 70 45 6e 74 72  y->count ? pEntr
b9470 79 2d 3e 63 68 61 69 6e 20 3a 20 30 3b 0a 20 20  y->chain : 0;.  
b9480 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b    pEntry->count+
b9490 2b 3b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63  +;.    pEntry->c
b94a0 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 20 20 7d  hain = pNew;.  }
b94b0 65 6c 73 65 7b 0a 20 20 20 20 70 48 65 61 64 20  else{.    pHead 
b94c0 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  = 0;.  }.  if( p
b94d0 48 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65 77  Head ){.    pNew
b94e0 2d 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b 0a  ->next = pHead;.
b94f0 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d      pNew->prev =
b9500 20 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20   pHead->prev;.  
b9510 20 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72 65    if( pHead->pre
b9520 76 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65 76  v ){ pHead->prev
b9530 2d 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d  ->next = pNew; }
b9540 0a 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20  .    else       
b9550 20 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72 73        { pH->firs
b9560 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20  t = pNew; }.    
b9570 70 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e  pHead->prev = pN
b9580 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ew;.  }else{.   
b9590 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48   pNew->next = pH
b95a0 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66 28  ->first;.    if(
b95b0 20 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70 48   pH->first ){ pH
b95c0 2d 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d 20  ->first->prev = 
b95d0 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77  pNew; }.    pNew
b95e0 2d 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  ->prev = 0;.    
b95f0 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77  pH->first = pNew
b9600 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 52 65 73  ;.  }.}.../* Res
b9610 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62  ize the hash tab
b9620 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61  le so that it ca
b9630 6e 74 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65  ntains "new_size
b9640 22 20 62 75 63 6b 65 74 73 2e 0a 2a 2a 0a 2a 2a  " buckets..**.**
b9650 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   The hash table 
b9660 6d 69 67 68 74 20 66 61 69 6c 20 74 6f 20 72 65  might fail to re
b9670 73 69 7a 65 20 69 66 20 73 71 6c 69 74 65 33 5f  size if sqlite3_
b9680 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 6f  malloc() fails o
b9690 72 0a 2a 2a 20 69 66 20 74 68 65 20 6e 65 77 20  r.** if the new 
b96a0 73 69 7a 65 20 69 73 20 74 68 65 20 73 61 6d 65  size is the same
b96b0 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 73 69   as the prior si
b96c0 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ze..** Return TR
b96d0 55 45 20 69 66 20 74 68 65 20 72 65 73 69 7a 65  UE if the resize
b96e0 20 6f 63 63 75 72 73 20 61 6e 64 20 66 61 6c 73   occurs and fals
b96f0 65 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61  e if not..*/.sta
b9700 74 69 63 20 69 6e 74 20 72 65 68 61 73 68 28 48  tic int rehash(H
b9710 61 73 68 20 2a 70 48 2c 20 75 6e 73 69 67 6e 65  ash *pH, unsigne
b9720 64 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b  d int new_size){
b9730 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a 6e  .  struct _ht *n
b9740 65 77 5f 68 74 3b 20 20 20 20 20 20 20 20 20 20  ew_ht;          
b9750 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 68 61 73    /* The new has
b9760 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73  h table */.  Has
b9770 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65  hElem *elem, *ne
b9780 78 74 5f 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 46  xt_elem;    /* F
b9790 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
b97a0 65 78 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e 74  existing element
b97b0 73 20 2a 2f 0a 0a 23 69 66 20 53 51 4c 49 54 45  s */..#if SQLITE
b97c0 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d  _MALLOC_SOFT_LIM
b97d0 49 54 3e 30 0a 20 20 69 66 28 20 6e 65 77 5f 73  IT>0.  if( new_s
b97e0 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63  ize*sizeof(struc
b97f0 74 20 5f 68 74 29 3e 53 51 4c 49 54 45 5f 4d 41  t _ht)>SQLITE_MA
b9800 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 20  LLOC_SOFT_LIMIT 
b9810 29 7b 0a 20 20 20 20 6e 65 77 5f 73 69 7a 65 20  ){.    new_size 
b9820 3d 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f  = SQLITE_MALLOC_
b9830 53 4f 46 54 5f 4c 49 4d 49 54 2f 73 69 7a 65 6f  SOFT_LIMIT/sizeo
b9840 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0a 20  f(struct _ht);. 
b9850 20 7d 0a 20 20 69 66 28 20 6e 65 77 5f 73 69 7a   }.  if( new_siz
b9860 65 3d 3d 70 48 2d 3e 68 74 73 69 7a 65 20 29 20  e==pH->htsize ) 
b9870 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66  return 0;.#endif
b9880 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 61 62 69  ..  /* The inabi
b9890 6c 69 74 79 20 74 6f 20 61 6c 6c 6f 63 61 74 65  lity to allocate
b98a0 73 20 73 70 61 63 65 20 66 6f 72 20 61 20 6c 61  s space for a la
b98b0 72 67 65 72 20 68 61 73 68 20 74 61 62 6c 65 20  rger hash table 
b98c0 69 73 0a 20 20 2a 2a 20 61 20 70 65 72 66 6f 72  is.  ** a perfor
b98d0 6d 61 6e 63 65 20 68 69 74 20 62 75 74 20 69 74  mance hit but it
b98e0 20 69 73 20 6e 6f 74 20 61 20 66 61 74 61 6c 20   is not a fatal 
b98f0 65 72 72 6f 72 2e 20 20 53 6f 20 6d 61 72 6b 20  error.  So mark 
b9900 74 68 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  the.  ** allocat
b9910 69 6f 6e 20 61 73 20 61 20 62 65 6e 69 67 6e 2e  ion as a benign.
b9920 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42  .  */.  sqlite3B
b9930 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
b9940 28 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28  ();.  new_ht = (
b9950 73 74 72 75 63 74 20 5f 68 74 20 2a 29 73 71 6c  struct _ht *)sql
b9960 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f  ite3Malloc( new_
b9970 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75  size*sizeof(stru
b9980 63 74 20 5f 68 74 29 20 29 3b 0a 20 20 73 71 6c  ct _ht) );.  sql
b9990 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
b99a0 6c 6f 63 28 29 3b 0a 0a 20 20 69 66 28 20 6e 65  loc();..  if( ne
b99b0 77 5f 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  w_ht==0 ) return
b99c0 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   0;.  sqlite3_fr
b99d0 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48  ee(pH->ht);.  pH
b99e0 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a 20  ->ht = new_ht;. 
b99f0 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e 65   pH->htsize = ne
b9a00 77 5f 73 69 7a 65 20 3d 20 73 71 6c 69 74 65 33  w_size = sqlite3
b9a10 4d 61 6c 6c 6f 63 53 69 7a 65 28 6e 65 77 5f 68  MallocSize(new_h
b9a20 74 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74  t)/sizeof(struct
b9a30 20 5f 68 74 29 3b 0a 20 20 6d 65 6d 73 65 74 28   _ht);.  memset(
b9a40 6e 65 77 5f 68 74 2c 20 30 2c 20 6e 65 77 5f 73  new_ht, 0, new_s
b9a50 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63  ize*sizeof(struc
b9a60 74 20 5f 68 74 29 29 3b 0a 20 20 66 6f 72 28 65  t _ht));.  for(e
b9a70 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70  lem=pH->first, p
b9a80 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d  H->first=0; elem
b9a90 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c  ; elem = next_el
b9aa0 65 6d 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65  em){.    unsigne
b9ab0 64 20 69 6e 74 20 68 20 3d 20 73 74 72 48 61 73  d int h = strHas
b9ac0 68 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c  h(elem->pKey, el
b9ad0 65 6d 2d 3e 6e 4b 65 79 29 20 25 20 6e 65 77 5f  em->nKey) % new_
b9ae0 73 69 7a 65 3b 0a 20 20 20 20 6e 65 78 74 5f 65  size;.    next_e
b9af0 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  lem = elem->next
b9b00 3b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d  ;.    insertElem
b9b10 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b  ent(pH, &new_ht[
b9b20 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 20  h], elem);.  }. 
b9b30 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
b9b40 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 28   This function (
b9b50 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65  for internal use
b9b60 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20 61   only) locates a
b9b70 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e 0a  n element in an.
b9b80 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 74 68  ** hash table th
b9b90 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20 67  at matches the g
b9ba0 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65 20 68  iven key.  The h
b9bb0 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79  ash for this key
b9bc0 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20   has.** already 
b9bd0 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 61 6e  been computed an
b9be0 64 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  d is passed as t
b9bf0 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72  he 4th parameter
b9c00 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 48 61 73 68  ..*/.static Hash
b9c10 45 6c 65 6d 20 2a 66 69 6e 64 45 6c 65 6d 65 6e  Elem *findElemen
b9c20 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 63 6f  tGivenHash(.  co
b9c30 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 20 20  nst Hash *pH,   
b9c40 20 20 2f 2a 20 54 68 65 20 70 48 20 74 6f 20 62    /* The pH to b
b9c50 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20  e searched */.  
b9c60 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65 79  const char *pKey
b9c70 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 77  ,   /* The key w
b9c80 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67 20  e are searching 
b9c90 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b 65  for */.  int nKe
b9ca0 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
b9cb0 42 79 74 65 73 20 69 6e 20 6b 65 79 20 28 6e 6f  Bytes in key (no
b9cc0 74 20 63 6f 75 6e 74 69 6e 67 20 7a 65 72 6f 20  t counting zero 
b9cd0 74 65 72 6d 69 6e 61 74 6f 72 29 20 2a 2f 0a 20  terminator) */. 
b9ce0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20   unsigned int h 
b9cf0 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68       /* The hash
b9d00 20 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20 2a   for this key. *
b9d10 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  /.){.  HashElem 
b9d20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *elem;          
b9d30 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
b9d40 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65   loop thru the e
b9d50 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20  lement list */. 
b9d60 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20   int count;     
b9d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b9d80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65  /* Number of ele
b9d90 6d 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74 65  ments left to te
b9da0 73 74 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48 2d  st */..  if( pH-
b9db0 3e 68 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63  >ht ){.    struc
b9dc0 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d 20  t _ht *pEntry = 
b9dd0 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20  &pH->ht[h];.    
b9de0 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63  elem = pEntry->c
b9df0 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20  hain;.    count 
b9e00 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b  = pEntry->count;
b9e10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 65 6c  .  }else{.    el
b9e20 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a  em = pH->first;.
b9e30 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 48 2d 3e      count = pH->
b9e40 63 6f 75 6e 74 3b 0a 20 20 7d 0a 20 20 77 68 69  count;.  }.  whi
b9e50 6c 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20 41  le( count-- && A
b9e60 4c 57 41 59 53 28 65 6c 65 6d 29 20 29 7b 0a 20  LWAYS(elem) ){. 
b9e70 20 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 4b 65     if( elem->nKe
b9e80 79 3d 3d 6e 4b 65 79 20 26 26 20 73 71 6c 69 74  y==nKey && sqlit
b9e90 65 33 53 74 72 4e 49 43 6d 70 28 65 6c 65 6d 2d  e3StrNICmp(elem-
b9ea0 3e 70 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29  >pKey,pKey,nKey)
b9eb0 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20 20 72 65  ==0 ){ .      re
b9ec0 74 75 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20 7d  turn elem;.    }
b9ed0 0a 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d  .    elem = elem
b9ee0 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65  ->next;.  }.  re
b9ef0 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65  turn 0;.}../* Re
b9f00 6d 6f 76 65 20 61 20 73 69 6e 67 6c 65 20 65 6e  move a single en
b9f10 74 72 79 20 66 72 6f 6d 20 74 68 65 20 68 61 73  try from the has
b9f20 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 61 20  h table given a 
b9f30 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 0a  pointer to that.
b9f40 2a 2a 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20 61  ** element and a
b9f50 20 68 61 73 68 20 6f 6e 20 74 68 65 20 65 6c 65   hash on the ele
b9f60 6d 65 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a 73  ment's key..*/.s
b9f70 74 61 74 69 63 20 76 6f 69 64 20 72 65 6d 6f 76  tatic void remov
b9f80 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73  eElementGivenHas
b9f90 68 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20  h(.  Hash *pH,  
b9fa0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48         /* The pH
b9fb0 20 63 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65   containing "ele
b9fc0 6d 22 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d  m" */.  HashElem
b9fd0 2a 20 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68 65  * elem,   /* The
b9fe0 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 72   element to be r
b9ff0 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20  emoved from the 
ba000 70 48 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  pH */.  unsigned
ba010 20 69 6e 74 20 68 20 20 20 20 2f 2a 20 48 61 73   int h    /* Has
ba020 68 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  h value for the 
ba030 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20  element */.){.  
ba040 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74  struct _ht *pEnt
ba050 72 79 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e  ry;.  if( elem->
ba060 70 72 65 76 20 29 7b 0a 20 20 20 20 65 6c 65 6d  prev ){.    elem
ba070 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 65  ->prev->next = e
ba080 6c 65 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d 65  lem->next; .  }e
ba090 6c 73 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69 72  lse{.    pH->fir
ba0a0 73 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b  st = elem->next;
ba0b0 0a 20 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d 2d  .  }.  if( elem-
ba0c0 3e 6e 65 78 74 20 29 7b 0a 20 20 20 20 65 6c 65  >next ){.    ele
ba0d0 6d 2d 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d 20  m->next->prev = 
ba0e0 65 6c 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a  elem->prev;.  }.
ba0f0 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b 0a    if( pH->ht ){.
ba100 20 20 20 20 70 45 6e 74 72 79 20 3d 20 26 70 48      pEntry = &pH
ba110 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 69 66 28  ->ht[h];.    if(
ba120 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d 3d   pEntry->chain==
ba130 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20 20 70 45  elem ){.      pE
ba140 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c  ntry->chain = el
ba150 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a  em->next;.    }.
ba160 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e      pEntry->coun
ba170 74 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t--;.    assert(
ba180 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3e 3d   pEntry->count>=
ba190 30 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  0 );.  }.  sqlit
ba1a0 65 33 5f 66 72 65 65 28 20 65 6c 65 6d 20 29 3b  e3_free( elem );
ba1b0 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a  .  pH->count--;.
ba1c0 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3c    if( pH->count<
ba1d0 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
ba1e0 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29  ( pH->first==0 )
ba1f0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48  ;.    assert( pH
ba200 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20  ->count==0 );.  
ba210 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65    sqlite3HashCle
ba220 61 72 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ar(pH);.  }.}../
ba230 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63  * Attempt to loc
ba240 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f  ate an element o
ba250 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  f the hash table
ba260 20 70 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a   pH with a key.*
ba270 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 70  * that matches p
ba280 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72  Key,nKey.  Retur
ba290 6e 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  n the data for t
ba2a0 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69  his element if i
ba2b0 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f  t is.** found, o
ba2c0 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20  r NULL if there 
ba2d0 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a  is no match..*/.
ba2e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
ba2f0 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68  oid *sqlite3Hash
ba300 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 20  Find(const Hash 
ba310 2a 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *pH, const char 
ba320 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29  *pKey, int nKey)
ba330 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c  {.  HashElem *el
ba340 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c  em;    /* The el
ba350 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68  ement that match
ba360 65 73 20 6b 65 79 20 2a 2f 0a 20 20 75 6e 73 69  es key */.  unsi
ba370 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20 20 2f  gned int h;    /
ba380 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 20  * A hash on key 
ba390 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48  */..  assert( pH
ba3a0 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
ba3b0 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61 73   pKey!=0 );.  as
ba3c0 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29 3b  sert( nKey>=0 );
ba3d0 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b  .  if( pH->ht ){
ba3e0 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 73 68  .    h = strHash
ba3f0 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20 70  (pKey, nKey) % p
ba400 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65 6c  H->htsize;.  }el
ba410 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a 20  se{.    h = 0;. 
ba420 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64   }.  elem = find
ba430 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73 68  ElementGivenHash
ba440 28 70 48 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 2c  (pH, pKey, nKey,
ba450 20 68 29 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c   h);.  return el
ba460 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64 61 74 61 20  em ? elem->data 
ba470 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72  : 0;.}../* Inser
ba480 74 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74  t an element int
ba490 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  o the hash table
ba4a0 20 70 48 2e 20 20 54 68 65 20 6b 65 79 20 69 73   pH.  The key is
ba4b0 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e   pKey,nKey.** an
ba4c0 64 20 74 68 65 20 64 61 74 61 20 69 73 20 22 64  d the data is "d
ba4d0 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  ata"..**.** If n
ba4e0 6f 20 65 6c 65 6d 65 6e 74 20 65 78 69 73 74 73  o element exists
ba4f0 20 77 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67   with a matching
ba500 20 6b 65 79 2c 20 74 68 65 6e 20 61 20 6e 65 77   key, then a new
ba510 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 69 73 20 63  .** element is c
ba520 72 65 61 74 65 64 20 61 6e 64 20 4e 55 4c 4c 20  reated and NULL 
ba530 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
ba540 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 65 6c  ** If another el
ba550 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 65 78  ement already ex
ba560 69 73 74 73 20 77 69 74 68 20 74 68 65 20 73 61  ists with the sa
ba570 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20 74 68 65  me key, then the
ba580 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20 72 65 70  .** new data rep
ba590 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 20 64 61  laces the old da
ba5a0 74 61 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 64  ta and the old d
ba5b0 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ata is returned.
ba5c0 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20 6e  .** The key is n
ba5d0 6f 74 20 63 6f 70 69 65 64 20 69 6e 20 74 68 69  ot copied in thi
ba5e0 73 20 69 6e 73 74 61 6e 63 65 2e 20 20 49 66 20  s instance.  If 
ba5f0 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c 20  a malloc fails, 
ba600 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20  then.** the new 
ba610 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64  data is returned
ba620 20 61 6e 64 20 74 68 65 20 68 61 73 68 20 74 61   and the hash ta
ba630 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ble is unchanged
ba640 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22  ..**.** If the "
ba650 64 61 74 61 22 20 70 61 72 61 6d 65 74 65 72 20  data" parameter 
ba660 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
ba670 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74   is NULL, then t
ba680 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63 6f  he.** element co
ba690 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 22  rresponding to "
ba6a0 6b 65 79 22 20 69 73 20 72 65 6d 6f 76 65 64 20  key" is removed 
ba6b0 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61  from the hash ta
ba6c0 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
ba6d0 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
ba6e0 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 48  ite3HashInsert(H
ba6f0 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 63  ash *pH, const c
ba700 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  har *pKey, int n
ba710 4b 65 79 2c 20 76 6f 69 64 20 2a 64 61 74 61 29  Key, void *data)
ba720 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  {.  unsigned int
ba730 20 68 3b 20 20 20 20 20 20 20 2f 2a 20 74 68 65   h;       /* the
ba740 20 68 61 73 68 20 6f 66 20 74 68 65 20 6b 65 79   hash of the key
ba750 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74 61 62   modulo hash tab
ba760 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 48 61 73  le size */.  Has
ba770 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20  hElem *elem;    
ba780 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f     /* Used to lo
ba790 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d  op thru the elem
ba7a0 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 48 61  ent list */.  Ha
ba7b0 73 68 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d  shElem *new_elem
ba7c0 3b 20 20 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65  ;   /* New eleme
ba7d0 6e 74 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  nt added to the 
ba7e0 70 48 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  pH */..  assert(
ba7f0 20 70 48 21 3d 30 20 29 3b 0a 20 20 61 73 73 65   pH!=0 );.  asse
ba800 72 74 28 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20  rt( pKey!=0 );. 
ba810 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30   assert( nKey>=0
ba820 20 29 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74   );.  if( pH->ht
ba830 73 69 7a 65 20 29 7b 0a 20 20 20 20 68 20 3d 20  size ){.    h = 
ba840 73 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b  strHash(pKey, nK
ba850 65 79 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65  ey) % pH->htsize
ba860 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68  ;.  }else{.    h
ba870 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 65 6d   = 0;.  }.  elem
ba880 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47 69   = findElementGi
ba890 76 65 6e 48 61 73 68 28 70 48 2c 70 4b 65 79 2c  venHash(pH,pKey,
ba8a0 6e 4b 65 79 2c 68 29 3b 0a 20 20 69 66 28 20 65  nKey,h);.  if( e
ba8b0 6c 65 6d 20 29 7b 0a 20 20 20 20 76 6f 69 64 20  lem ){.    void 
ba8c0 2a 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65 6d  *old_data = elem
ba8d0 2d 3e 64 61 74 61 3b 0a 20 20 20 20 69 66 28 20  ->data;.    if( 
ba8e0 64 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20  data==0 ){.     
ba8f0 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69   removeElementGi
ba900 76 65 6e 48 61 73 68 28 70 48 2c 65 6c 65 6d 2c  venHash(pH,elem,
ba910 68 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  h);.    }else{. 
ba920 20 20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20       elem->data 
ba930 3d 20 64 61 74 61 3b 0a 20 20 20 20 20 20 65 6c  = data;.      el
ba940 65 6d 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b  em->pKey = pKey;
ba950 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 4b  .      assert(nK
ba960 65 79 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65 79 29 3b  ey==elem->nKey);
ba970 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
ba980 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 7d 0a  n old_data;.  }.
ba990 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29 20    if( data==0 ) 
ba9a0 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 65 77 5f  return 0;.  new_
ba9b0 65 6c 65 6d 20 3d 20 28 48 61 73 68 45 6c 65 6d  elem = (HashElem
ba9c0 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  *)sqlite3Malloc(
ba9d0 20 73 69 7a 65 6f 66 28 48 61 73 68 45 6c 65 6d   sizeof(HashElem
ba9e0 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65  ) );.  if( new_e
ba9f0 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  lem==0 ) return 
baa00 64 61 74 61 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d  data;.  new_elem
baa10 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20  ->pKey = pKey;. 
baa20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20   new_elem->nKey 
baa30 3d 20 6e 4b 65 79 3b 0a 20 20 6e 65 77 5f 65 6c  = nKey;.  new_el
baa40 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b  em->data = data;
baa50 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a  .  pH->count++;.
baa60 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3e    if( pH->count>
baa70 3d 31 30 20 26 26 20 70 48 2d 3e 63 6f 75 6e 74  =10 && pH->count
baa80 20 3e 20 32 2a 70 48 2d 3e 68 74 73 69 7a 65 20   > 2*pH->htsize 
baa90 29 7b 0a 20 20 20 20 69 66 28 20 72 65 68 61 73  ){.    if( rehas
baaa0 68 28 70 48 2c 20 70 48 2d 3e 63 6f 75 6e 74 2a  h(pH, pH->count*
baab0 32 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  2) ){.      asse
baac0 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30  rt( pH->htsize>0
baad0 20 29 3b 0a 20 20 20 20 20 20 68 20 3d 20 73 74   );.      h = st
baae0 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65 79  rHash(pKey, nKey
baaf0 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b 0a  ) % pH->htsize;.
bab00 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
bab10 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 69 6e  pH->ht ){.    in
bab20 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20  sertElement(pH, 
bab30 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f  &pH->ht[h], new_
bab40 65 6c 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  elem);.  }else{.
bab50 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e      insertElemen
bab60 74 28 70 48 2c 20 30 2c 20 6e 65 77 5f 65 6c 65  t(pH, 0, new_ele
bab70 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  m);.  }.  return
bab80 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a   0;.}../********
bab90 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 61  ****** End of ha
baba0 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.c ***********
babb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
babc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
babd0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
babe0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
babf0 65 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a 2a  e opcodes.c ****
bac00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bac20 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d 61  *****/./* Automa
bac30 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65  tically generate
bac40 64 2e 20 20 44 6f 20 6e 6f 74 20 65 64 69 74 20  d.  Do not edit 
bac50 2a 2f 0a 2f 2a 20 53 65 65 20 74 68 65 20 6d 6b  */./* See the mk
bac60 6f 70 63 6f 64 65 63 2e 61 77 6b 20 73 63 72 69  opcodec.awk scri
bac70 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20  pt for details. 
bac80 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
bac90 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
baca0 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  AIN) || !defined
bacb0 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69  (NDEBUG) || defi
bacc0 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c 45  ned(VDBE_PROFILE
bacd0 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
bace0 49 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54  ITE_DEBUG).SQLIT
bacf0 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
bad00 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70 63  char *sqlite3Opc
bad10 6f 64 65 4e 61 6d 65 28 69 6e 74 20 69 29 7b 0a  odeName(int i){.
bad20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
bad30 61 72 20 2a 63 6f 6e 73 74 20 61 7a 4e 61 6d 65  ar *const azName
bad40 5b 5d 20 3d 20 7b 20 22 3f 22 2c 0a 20 20 20 20  [] = { "?",.    
bad50 20 2f 2a 20 20 20 31 20 2a 2f 20 22 47 6f 74 6f   /*   1 */ "Goto
bad60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 32 20 2a  ",.     /*   2 *
bad70 2f 20 22 47 6f 73 75 62 22 2c 0a 20 20 20 20 20  / "Gosub",.     
bad80 2f 2a 20 20 20 33 20 2a 2f 20 22 52 65 74 75 72  /*   3 */ "Retur
bad90 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 34 20  n",.     /*   4 
bada0 2a 2f 20 22 59 69 65 6c 64 22 2c 0a 20 20 20 20  */ "Yield",.    
badb0 20 2f 2a 20 20 20 35 20 2a 2f 20 22 48 61 6c 74   /*   5 */ "Halt
badc0 49 66 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a  IfNull",.     /*
badd0 20 20 20 36 20 2a 2f 20 22 48 61 6c 74 22 2c 0a     6 */ "Halt",.
bade0 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20 22       /*   7 */ "
badf0 49 6e 74 65 67 65 72 22 2c 0a 20 20 20 20 20 2f  Integer",.     /
bae00 2a 20 20 20 38 20 2a 2f 20 22 49 6e 74 36 34 22  *   8 */ "Int64"
bae10 2c 0a 20 20 20 20 20 2f 2a 20 20 20 39 20 2a 2f  ,.     /*   9 */
bae20 20 22 53 74 72 69 6e 67 22 2c 0a 20 20 20 20 20   "String",.     
bae30 2f 2a 20 20 31 30 20 2a 2f 20 22 4e 75 6c 6c 22  /*  10 */ "Null"
bae40 2c 0a 20 20 20 20 20 2f 2a 20 20 31 31 20 2a 2f  ,.     /*  11 */
bae50 20 22 42 6c 6f 62 22 2c 0a 20 20 20 20 20 2f 2a   "Blob",.     /*
bae60 20 20 31 32 20 2a 2f 20 22 56 61 72 69 61 62 6c    12 */ "Variabl
bae70 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 33 20  e",.     /*  13 
bae80 2a 2f 20 22 4d 6f 76 65 22 2c 0a 20 20 20 20 20  */ "Move",.     
bae90 2f 2a 20 20 31 34 20 2a 2f 20 22 43 6f 70 79 22  /*  14 */ "Copy"
baea0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 35 20 2a 2f  ,.     /*  15 */
baeb0 20 22 53 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f   "SCopy",.     /
baec0 2a 20 20 31 36 20 2a 2f 20 22 52 65 73 75 6c 74  *  16 */ "Result
baed0 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31  Row",.     /*  1
baee0 37 20 2a 2f 20 22 43 6f 6c 6c 53 65 71 22 2c 0a  7 */ "CollSeq",.
baef0 20 20 20 20 20 2f 2a 20 20 31 38 20 2a 2f 20 22       /*  18 */ "
baf00 46 75 6e 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20  Function",.     
baf10 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f 74 22 2c  /*  19 */ "Not",
baf20 0a 20 20 20 20 20 2f 2a 20 20 32 30 20 2a 2f 20  .     /*  20 */ 
baf30 22 41 64 64 49 6d 6d 22 2c 0a 20 20 20 20 20 2f  "AddImm",.     /
baf40 2a 20 20 32 31 20 2a 2f 20 22 4d 75 73 74 42 65  *  21 */ "MustBe
baf50 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32  Int",.     /*  2
baf60 32 20 2a 2f 20 22 52 65 61 6c 41 66 66 69 6e 69  2 */ "RealAffini
baf70 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 33  ty",.     /*  23
baf80 20 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 6f 6e   */ "Permutation
baf90 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 34 20 2a  ",.     /*  24 *
bafa0 2f 20 22 43 6f 6d 70 61 72 65 22 2c 0a 20 20 20  / "Compare",.   
bafb0 20 20 2f 2a 20 20 32 35 20 2a 2f 20 22 4a 75 6d    /*  25 */ "Jum
bafc0 70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 20  p",.     /*  26 
bafd0 2a 2f 20 22 49 66 22 2c 0a 20 20 20 20 20 2f 2a  */ "If",.     /*
bafe0 20 20 32 37 20 2a 2f 20 22 49 66 4e 6f 74 22 2c    27 */ "IfNot",
baff0 0a 20 20 20 20 20 2f 2a 20 20 32 38 20 2a 2f 20  .     /*  28 */ 
bb000 22 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f  "Column",.     /
bb010 2a 20 20 32 39 20 2a 2f 20 22 41 66 66 69 6e 69  *  29 */ "Affini
bb020 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 30  ty",.     /*  30
bb030 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72 64 22   */ "MakeRecord"
bb040 2c 0a 20 20 20 20 20 2f 2a 20 20 33 31 20 2a 2f  ,.     /*  31 */
bb050 20 22 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20 2f   "Count",.     /
bb060 2a 20 20 33 32 20 2a 2f 20 22 53 61 76 65 70 6f  *  32 */ "Savepo
bb070 69 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  int",.     /*  3
bb080 33 20 2a 2f 20 22 41 75 74 6f 43 6f 6d 6d 69 74  3 */ "AutoCommit
bb090 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 34 20 2a  ",.     /*  34 *
bb0a0 2f 20 22 54 72 61 6e 73 61 63 74 69 6f 6e 22 2c  / "Transaction",
bb0b0 0a 20 20 20 20 20 2f 2a 20 20 33 35 20 2a 2f 20  .     /*  35 */ 
bb0c0 22 52 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20  "ReadCookie",.  
bb0d0 20 20 20 2f 2a 20 20 33 36 20 2a 2f 20 22 53 65     /*  36 */ "Se
bb0e0 74 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f  tCookie",.     /
bb0f0 2a 20 20 33 37 20 2a 2f 20 22 56 65 72 69 66 79  *  37 */ "Verify
bb100 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f 2a  Cookie",.     /*
bb110 20 20 33 38 20 2a 2f 20 22 4f 70 65 6e 52 65 61    38 */ "OpenRea
bb120 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 39 20  d",.     /*  39 
bb130 2a 2f 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a  */ "OpenWrite",.
bb140 20 20 20 20 20 2f 2a 20 20 34 30 20 2a 2f 20 22       /*  40 */ "
bb150 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 2c 0a  OpenEphemeral",.
bb160 20 20 20 20 20 2f 2a 20 20 34 31 20 2a 2f 20 22       /*  41 */ "
bb170 4f 70 65 6e 50 73 65 75 64 6f 22 2c 0a 20 20 20  OpenPseudo",.   
bb180 20 20 2f 2a 20 20 34 32 20 2a 2f 20 22 43 6c 6f    /*  42 */ "Clo
bb190 73 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 33  se",.     /*  43
bb1a0 20 2a 2f 20 22 53 65 65 6b 4c 74 22 2c 0a 20 20   */ "SeekLt",.  
bb1b0 20 20 20 2f 2a 20 20 34 34 20 2a 2f 20 22 53 65     /*  44 */ "Se
bb1c0 65 6b 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ekLe",.     /*  
bb1d0 34 35 20 2a 2f 20 22 53 65 65 6b 47 65 22 2c 0a  45 */ "SeekGe",.
bb1e0 20 20 20 20 20 2f 2a 20 20 34 36 20 2a 2f 20 22       /*  46 */ "
bb1f0 53 65 65 6b 47 74 22 2c 0a 20 20 20 20 20 2f 2a  SeekGt",.     /*
bb200 20 20 34 37 20 2a 2f 20 22 53 65 65 6b 22 2c 0a    47 */ "Seek",.
bb210 20 20 20 20 20 2f 2a 20 20 34 38 20 2a 2f 20 22       /*  48 */ "
bb220 4e 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20 20 20  NotFound",.     
bb230 2f 2a 20 20 34 39 20 2a 2f 20 22 46 6f 75 6e 64  /*  49 */ "Found
bb240 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 30 20 2a  ",.     /*  50 *
bb250 2f 20 22 49 73 55 6e 69 71 75 65 22 2c 0a 20 20  / "IsUnique",.  
bb260 20 20 20 2f 2a 20 20 35 31 20 2a 2f 20 22 4e 6f     /*  51 */ "No
bb270 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20 20 2f  tExists",.     /
bb280 2a 20 20 35 32 20 2a 2f 20 22 53 65 71 75 65 6e  *  52 */ "Sequen
bb290 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 33  ce",.     /*  53
bb2a0 20 2a 2f 20 22 4e 65 77 52 6f 77 69 64 22 2c 0a   */ "NewRowid",.
bb2b0 20 20 20 20 20 2f 2a 20 20 35 34 20 2a 2f 20 22       /*  54 */ "
bb2c0 49 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a  Insert",.     /*
bb2d0 20 20 35 35 20 2a 2f 20 22 49 6e 73 65 72 74 49    55 */ "InsertI
bb2e0 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36  nt",.     /*  56
bb2f0 20 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 20   */ "Delete",.  
bb300 20 20 20 2f 2a 20 20 35 37 20 2a 2f 20 22 52 65     /*  57 */ "Re
bb310 73 65 74 43 6f 75 6e 74 22 2c 0a 20 20 20 20 20  setCount",.     
bb320 2f 2a 20 20 35 38 20 2a 2f 20 22 52 6f 77 4b 65  /*  58 */ "RowKe
bb330 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 39 20  y",.     /*  59 
bb340 2a 2f 20 22 52 6f 77 44 61 74 61 22 2c 0a 20 20  */ "RowData",.  
bb350 20 20 20 2f 2a 20 20 36 30 20 2a 2f 20 22 52 6f     /*  60 */ "Ro
bb360 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  wid",.     /*  6
bb370 31 20 2a 2f 20 22 4e 75 6c 6c 52 6f 77 22 2c 0a  1 */ "NullRow",.
bb380 20 20 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22       /*  62 */ "
bb390 4c 61 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Last",.     /*  
bb3a0 36 33 20 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20  63 */ "Sort",.  
bb3b0 20 20 20 2f 2a 20 20 36 34 20 2a 2f 20 22 52 65     /*  64 */ "Re
bb3c0 77 69 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  wind",.     /*  
bb3d0 36 35 20 2a 2f 20 22 50 72 65 76 22 2c 0a 20 20  65 */ "Prev",.  
bb3e0 20 20 20 2f 2a 20 20 36 36 20 2a 2f 20 22 4e 65     /*  66 */ "Ne
bb3f0 78 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 37  xt",.     /*  67
bb400 20 2a 2f 20 22 49 64 78 49 6e 73 65 72 74 22 2c   */ "IdxInsert",
bb410 0a 20 20 20 20 20 2f 2a 20 20 36 38 20 2a 2f 20  .     /*  68 */ 
bb420 22 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  "Or",.     /*  6
bb430 39 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 20 20 20  9 */ "And",.    
bb440 20 2f 2a 20 20 37 30 20 2a 2f 20 22 49 64 78 44   /*  70 */ "IdxD
bb450 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a 20  elete",.     /* 
bb460 20 37 31 20 2a 2f 20 22 49 64 78 52 6f 77 69 64   71 */ "IdxRowid
bb470 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 32 20 2a  ",.     /*  72 *
bb480 2f 20 22 49 64 78 4c 54 22 2c 0a 20 20 20 20 20  / "IdxLT",.     
bb490 2f 2a 20 20 37 33 20 2a 2f 20 22 49 73 4e 75 6c  /*  73 */ "IsNul
bb4a0 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 34 20  l",.     /*  74 
bb4b0 2a 2f 20 22 4e 6f 74 4e 75 6c 6c 22 2c 0a 20 20  */ "NotNull",.  
bb4c0 20 20 20 2f 2a 20 20 37 35 20 2a 2f 20 22 4e 65     /*  75 */ "Ne
bb4d0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 36 20 2a  ",.     /*  76 *
bb4e0 2f 20 22 45 71 22 2c 0a 20 20 20 20 20 2f 2a 20  / "Eq",.     /* 
bb4f0 20 37 37 20 2a 2f 20 22 47 74 22 2c 0a 20 20 20   77 */ "Gt",.   
bb500 20 20 2f 2a 20 20 37 38 20 2a 2f 20 22 4c 65 22    /*  78 */ "Le"
bb510 2c 0a 20 20 20 20 20 2f 2a 20 20 37 39 20 2a 2f  ,.     /*  79 */
bb520 20 22 4c 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20   "Lt",.     /*  
bb530 38 30 20 2a 2f 20 22 47 65 22 2c 0a 20 20 20 20  80 */ "Ge",.    
bb540 20 2f 2a 20 20 38 31 20 2a 2f 20 22 49 64 78 47   /*  81 */ "IdxG
bb550 45 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 32 20  E",.     /*  82 
bb560 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 20 20 20  */ "BitAnd",.   
bb570 20 20 2f 2a 20 20 38 33 20 2a 2f 20 22 42 69 74    /*  83 */ "Bit
bb580 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 34  Or",.     /*  84
bb590 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 74 22 2c   */ "ShiftLeft",
bb5a0 0a 20 20 20 20 20 2f 2a 20 20 38 35 20 2a 2f 20  .     /*  85 */ 
bb5b0 22 53 68 69 66 74 52 69 67 68 74 22 2c 0a 20 20  "ShiftRight",.  
bb5c0 20 20 20 2f 2a 20 20 38 36 20 2a 2f 20 22 41 64     /*  86 */ "Ad
bb5d0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 37 20  d",.     /*  87 
bb5e0 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c 0a 20  */ "Subtract",. 
bb5f0 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f 20 22 4d      /*  88 */ "M
bb600 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 20 20 2f  ultiply",.     /
bb610 2a 20 20 38 39 20 2a 2f 20 22 44 69 76 69 64 65  *  89 */ "Divide
bb620 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30 20 2a  ",.     /*  90 *
bb630 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 2c 0a 20  / "Remainder",. 
bb640 20 20 20 20 2f 2a 20 20 39 31 20 2a 2f 20 22 43      /*  91 */ "C
bb650 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 2f 2a 20  oncat",.     /* 
bb660 20 39 32 20 2a 2f 20 22 44 65 73 74 72 6f 79 22   92 */ "Destroy"
bb670 2c 0a 20 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f  ,.     /*  93 */
bb680 20 22 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20   "BitNot",.     
bb690 2f 2a 20 20 39 34 20 2a 2f 20 22 53 74 72 69 6e  /*  94 */ "Strin
bb6a0 67 38 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 35  g8",.     /*  95
bb6b0 20 2a 2f 20 22 43 6c 65 61 72 22 2c 0a 20 20 20   */ "Clear",.   
bb6c0 20 20 2f 2a 20 20 39 36 20 2a 2f 20 22 43 72 65    /*  96 */ "Cre
bb6d0 61 74 65 49 6e 64 65 78 22 2c 0a 20 20 20 20 20  ateIndex",.     
bb6e0 2f 2a 20 20 39 37 20 2a 2f 20 22 43 72 65 61 74  /*  97 */ "Creat
bb6f0 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a  eTable",.     /*
bb700 20 20 39 38 20 2a 2f 20 22 50 61 72 73 65 53 63    98 */ "ParseSc
bb710 68 65 6d 61 22 2c 0a 20 20 20 20 20 2f 2a 20 20  hema",.     /*  
bb720 39 39 20 2a 2f 20 22 4c 6f 61 64 41 6e 61 6c 79  99 */ "LoadAnaly
bb730 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  sis",.     /* 10
bb740 30 20 2a 2f 20 22 44 72 6f 70 54 61 62 6c 65 22  0 */ "DropTable"
bb750 2c 0a 20 20 20 20 20 2f 2a 20 31 30 31 20 2a 2f  ,.     /* 101 */
bb760 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20   "DropIndex",.  
bb770 20 20 20 2f 2a 20 31 30 32 20 2a 2f 20 22 44 72     /* 102 */ "Dr
bb780 6f 70 54 72 69 67 67 65 72 22 2c 0a 20 20 20 20  opTrigger",.    
bb790 20 2f 2a 20 31 30 33 20 2a 2f 20 22 49 6e 74 65   /* 103 */ "Inte
bb7a0 67 72 69 74 79 43 6b 22 2c 0a 20 20 20 20 20 2f  grityCk",.     /
bb7b0 2a 20 31 30 34 20 2a 2f 20 22 52 6f 77 53 65 74  * 104 */ "RowSet
bb7c0 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  Add",.     /* 10
bb7d0 35 20 2a 2f 20 22 52 6f 77 53 65 74 52 65 61 64  5 */ "RowSetRead
bb7e0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 36 20 2a  ",.     /* 106 *
bb7f0 2f 20 22 52 6f 77 53 65 74 54 65 73 74 22 2c 0a  / "RowSetTest",.
bb800 20 20 20 20 20 2f 2a 20 31 30 37 20 2a 2f 20 22       /* 107 */ "
bb810 50 72 6f 67 72 61 6d 22 2c 0a 20 20 20 20 20 2f  Program",.     /
bb820 2a 20 31 30 38 20 2a 2f 20 22 50 61 72 61 6d 22  * 108 */ "Param"
bb830 2c 0a 20 20 20 20 20 2f 2a 20 31 30 39 20 2a 2f  ,.     /* 109 */
bb840 20 22 46 6b 43 6f 75 6e 74 65 72 22 2c 0a 20 20   "FkCounter",.  
bb850 20 20 20 2f 2a 20 31 31 30 20 2a 2f 20 22 46 6b     /* 110 */ "Fk
bb860 49 66 5a 65 72 6f 22 2c 0a 20 20 20 20 20 2f 2a  IfZero",.     /*
bb870 20 31 31 31 20 2a 2f 20 22 4d 65 6d 4d 61 78 22   111 */ "MemMax"
bb880 2c 0a 20 20 20 20 20 2f 2a 20 31 31 32 20 2a 2f  ,.     /* 112 */
bb890 20 22 49 66 50 6f 73 22 2c 0a 20 20 20 20 20 2f   "IfPos",.     /
bb8a0 2a 20 31 31 33 20 2a 2f 20 22 49 66 4e 65 67 22  * 113 */ "IfNeg"
bb8b0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 34 20 2a 2f  ,.     /* 114 */
bb8c0 20 22 49 66 5a 65 72 6f 22 2c 0a 20 20 20 20 20   "IfZero",.     
bb8d0 2f 2a 20 31 31 35 20 2a 2f 20 22 41 67 67 53 74  /* 115 */ "AggSt
bb8e0 65 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 36  ep",.     /* 116
bb8f0 20 2a 2f 20 22 41 67 67 46 69 6e 61 6c 22 2c 0a   */ "AggFinal",.
bb900 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f 20 22       /* 117 */ "
bb910 56 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a  Vacuum",.     /*
bb920 20 31 31 38 20 2a 2f 20 22 49 6e 63 72 56 61 63   118 */ "IncrVac
bb930 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  uum",.     /* 11
bb940 39 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a 20  9 */ "Expire",. 
bb950 20 20 20 20 2f 2a 20 31 32 30 20 2a 2f 20 22 54      /* 120 */ "T
bb960 61 62 6c 65 4c 6f 63 6b 22 2c 0a 20 20 20 20 20  ableLock",.     
bb970 2f 2a 20 31 32 31 20 2a 2f 20 22 56 42 65 67 69  /* 121 */ "VBegi
bb980 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 32 20  n",.     /* 122 
bb990 2a 2f 20 22 56 43 72 65 61 74 65 22 2c 0a 20 20  */ "VCreate",.  
bb9a0 20 20 20 2f 2a 20 31 32 33 20 2a 2f 20 22 56 44     /* 123 */ "VD
bb9b0 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a  estroy",.     /*
bb9c0 20 31 32 34 20 2a 2f 20 22 56 4f 70 65 6e 22 2c   124 */ "VOpen",
bb9d0 0a 20 20 20 20 20 2f 2a 20 31 32 35 20 2a 2f 20  .     /* 125 */ 
bb9e0 22 56 46 69 6c 74 65 72 22 2c 0a 20 20 20 20 20  "VFilter",.     
bb9f0 2f 2a 20 31 32 36 20 2a 2f 20 22 56 43 6f 6c 75  /* 126 */ "VColu
bba00 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 37  mn",.     /* 127
bba10 20 2a 2f 20 22 56 4e 65 78 74 22 2c 0a 20 20 20   */ "VNext",.   
bba20 20 20 2f 2a 20 31 32 38 20 2a 2f 20 22 56 52 65    /* 128 */ "VRe
bba30 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31  name",.     /* 1
bba40 32 39 20 2a 2f 20 22 56 55 70 64 61 74 65 22 2c  29 */ "VUpdate",
bba50 0a 20 20 20 20 20 2f 2a 20 31 33 30 20 2a 2f 20  .     /* 130 */ 
bba60 22 52 65 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20  "Real",.     /* 
bba70 31 33 31 20 2a 2f 20 22 50 61 67 65 63 6f 75 6e  131 */ "Pagecoun
bba80 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 32 20  t",.     /* 132 
bba90 2a 2f 20 22 54 72 61 63 65 22 2c 0a 20 20 20 20  */ "Trace",.    
bbaa0 20 2f 2a 20 31 33 33 20 2a 2f 20 22 4e 6f 6f 70   /* 133 */ "Noop
bbab0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a  ",.     /* 134 *
bbac0 2f 20 22 45 78 70 6c 61 69 6e 22 2c 0a 20 20 20  / "Explain",.   
bbad0 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e 6f 74    /* 135 */ "Not
bbae0 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 20 20 20  Used_135",.     
bbaf0 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74 55 73  /* 136 */ "NotUs
bbb00 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 20 2f 2a  ed_136",.     /*
bbb10 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73 65 64   137 */ "NotUsed
bbb20 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a 20 31  _137",.     /* 1
bbb30 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31  38 */ "NotUsed_1
bbb40 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 39  38",.     /* 139
bbb50 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 39   */ "NotUsed_139
bbb60 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 30 20 2a  ",.     /* 140 *
bbb70 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34 30 22 2c  / "NotUsed_140",
bbb80 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 2a 2f 20  .     /* 141 */ 
bbb90 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 20 20 2f  "ToText",.     /
bbba0 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42 6c 6f 62  * 142 */ "ToBlob
bbbb0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 33 20 2a  ",.     /* 143 *
bbbc0 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c 0a 20  / "ToNumeric",. 
bbbd0 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f 20 22 54      /* 144 */ "T
bbbe0 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31  oInt",.     /* 1
bbbf0 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c 22 2c 0a  45 */ "ToReal",.
bbc00 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 61 7a    };.  return az
bbc10 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e 64 69  Name[i];.}.#endi
bbc20 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
bbc30 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65  ** End of opcode
bbc40 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.c ************
bbc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbc70 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
bbc80 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73  ** Begin file os
bbc90 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _os2.c *********
bbca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbcc0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 46 65  */./*.** 2006 Fe
bbcd0 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  b 14.**.** The a
bbce0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
bbcf0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
bbd00 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
bbd10 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
bbd20 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
bbd30 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
bbd40 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
bbd50 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
bbd60 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
bbd70 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
bbd80 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
bbd90 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
bbda0 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
bbdb0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
bbdc0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
bbdd0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
bbde0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
bbdf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbe00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbe10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbe20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbe30 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
bbe40 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
bbe50 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73  s code that is s
bbe60 70 65 63 69 66 69 63 20 74 6f 20 4f 53 2f 32 2e  pecific to OS/2.
bbe70 0a 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45  .*/...#if SQLITE
bbe80 5f 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a 20 41  _OS_OS2../*.** A
bbe90 20 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65 6d 6f   Note About Memo
bbea0 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a 0a 2a  ry Allocation:.*
bbeb0 2a 0a 2a 2a 20 54 68 69 73 20 64 72 69 76 65 72  *.** This driver
bbec0 20 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29 2f 66   uses malloc()/f
bbed0 72 65 65 28 29 20 64 69 72 65 63 74 6c 79 20 72  ree() directly r
bbee0 61 74 68 65 72 20 74 68 61 6e 20 67 6f 69 6e 67  ather than going
bbef0 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20   through.** the 
bbf00 53 51 4c 69 74 65 2d 77 72 61 70 70 65 72 73 20  SQLite-wrappers 
bbf10 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
bbf20 2f 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 2e  /sqlite3_free().
bbf30 20 20 54 68 6f 73 65 20 77 72 61 70 70 65 72 73    Those wrappers
bbf40 0a 2a 2a 20 61 72 65 20 64 65 73 69 67 6e 65 64  .** are designed
bbf50 20 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d 62 65   for use on embe
bbf60 64 64 65 64 20 73 79 73 74 65 6d 73 20 77 68 65  dded systems whe
bbf70 72 65 20 6d 65 6d 6f 72 79 20 69 73 20 73 63 61  re memory is sca
bbf80 72 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c 6c 6f  rce and.** mallo
bbf90 63 20 66 61 69 6c 75 72 65 73 20 68 61 70 70 65  c failures happe
bbfa0 6e 20 66 72 65 71 75 65 6e 74 6c 79 2e 20 20 4f  n frequently.  O
bbfb0 53 2f 32 20 64 6f 65 73 20 6e 6f 74 20 74 79 70  S/2 does not typ
bbfc0 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a 2a 2a  ically run on.**
bbfd0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
bbfe0 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 74 20 64  s, and when it d
bbff0 6f 65 73 20 74 68 65 20 64 65 76 65 6c 6f 70 65  oes the develope
bc000 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 76 65  rs normally have
bc010 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f 62 6c   bigger.** probl
bc020 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 61 62 6f  ems to worry abo
bc030 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e 67 20  ut than running 
bc040 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20  out of memory.  
bc050 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 74 0a  So there is not.
bc060 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e 67 20  ** a compelling 
bc070 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20  need to use the 
bc080 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a 2a 20  wrappers..**.** 
bc090 42 75 74 20 74 68 65 72 65 20 69 73 20 61 20 67  But there is a g
bc0a0 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20 6e 6f  ood reason to no
bc0b0 74 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65  t use the wrappe
bc0c0 72 73 2e 20 20 49 66 20 77 65 20 75 73 65 20 74  rs.  If we use t
bc0d0 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 73 20 74  he.** wrappers t
bc0e0 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 65 74 20  hen we will get 
bc0f0 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c 6f 63  simulated malloc
bc100 28 29 20 66 61 69 6c 75 72 65 73 20 77 69 74 68  () failures with
bc110 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69 76 65  in this.** drive
bc120 72 2e 20 20 41 6e 64 20 74 68 61 74 20 63 61 75  r.  And that cau
bc130 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20 6f 66  ses all kinds of
bc140 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 75   problems for ou
bc150 72 20 74 65 73 74 73 2e 20 20 57 65 0a 2a 2a 20  r tests.  We.** 
bc160 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20 53 51  could enhance SQ
bc170 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 77 69 74  Lite to deal wit
bc180 68 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c  h simulated mall
bc190 6f 63 20 66 61 69 6c 75 72 65 73 20 77 69 74 68  oc failures with
bc1a0 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64 72 69  in.** the OS dri
bc1b0 76 65 72 2c 20 62 75 74 20 74 68 65 20 63 6f 64  ver, but the cod
bc1c0 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 74  e to deal with t
bc1d0 68 6f 73 65 20 66 61 69 6c 75 72 65 20 77 6f 75  hose failure wou
bc1e0 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65 78 65  ld not.** be exe
bc1f0 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75 78 20  rcised on Linux 
bc200 28 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20  (which does not 
bc210 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63 28 29  need to malloc()
bc220 20 69 6e 20 74 68 65 20 64 72 69 76 65 72 29 0a   in the driver).
bc230 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 77 6f 75  ** and so we wou
bc240 6c 64 20 68 61 76 65 20 64 69 66 66 69 63 75 6c  ld have difficul
bc250 74 79 20 77 72 69 74 69 6e 67 20 63 6f 76 65 72  ty writing cover
bc260 61 67 65 20 74 65 73 74 73 20 66 6f 72 20 74 68  age tests for th
bc270 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42 65 74  at.** code.  Bet
bc280 74 65 72 20 74 6f 20 6c 65 61 76 65 20 74 68 65  ter to leave the
bc290 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 20 74 68   code out, we th
bc2a0 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ink..**.** The p
bc2b0 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 64 69 73  oint of this dis
bc2c0 63 75 73 73 69 6f 6e 20 69 73 20 61 73 20 66 6f  cussion is as fo
bc2d0 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63 72 65  llows:  When cre
bc2e0 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a 20 4f  ating a new.** O
bc2f0 53 20 6c 61 79 65 72 20 66 6f 72 20 61 6e 20 65  S layer for an e
bc300 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d 2c 20  mbedded system, 
bc310 69 66 20 79 6f 75 20 75 73 65 20 74 68 69 73 20  if you use this 
bc320 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61 6d 70  file as an examp
bc330 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74 68 65  le,.** avoid the
bc340 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29   use of malloc()
bc350 2f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20  /free().  Those 
bc360 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6b  routines work ok
bc370 20 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64 65 73 6b   on OS/2.** desk
bc380 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 73 6f 20  tops but not so 
bc390 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64 65 64  well in embedded
bc3a0 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a 2f 2a   systems..*/../*
bc3b0 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65 64 20  .** Macros used 
bc3c0 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65  to determine whe
bc3d0 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 75  ther or not to u
bc3e0 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 23  se threads..*/.#
bc3f0 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
bc400 45 5f 54 48 52 45 41 44 53 41 46 45 29 20 26 26  E_THREADSAFE) &&
bc410 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
bc420 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  FE.# define SQLI
bc430 54 45 5f 4f 53 32 5f 54 48 52 45 41 44 53 20 31  TE_OS2_THREADS 1
bc440 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
bc450 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74  nclude code that
bc460 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c   is common to al
bc470 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a  l os_*.c files.*
bc480 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
bc490 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d  * Include os_com
bc4a0 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  mon.h in the mid
bc4b0 64 6c 65 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20  dle of os_os2.c 
bc4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc4d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
bc4e0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
bc4f0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
bc500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc520 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
bc530 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
bc540 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
bc550 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
bc560 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
bc570 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
bc580 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
bc590 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
bc5a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
bc5b0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
bc5c0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
bc5d0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
bc5e0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
bc5f0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
bc600 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
bc610 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
bc620 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
bc630 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
bc640 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
bc650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc690 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
bc6a0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
bc6b0 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69   macros and a li
bc6c0 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65  ttle bit of code
bc6d0 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20   that is common 
bc6e0 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65  to.** all of the
bc6f0 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66   platform-specif
bc700 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63  ic files (os_*.c
bc710 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64  ) and is #includ
bc720 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a  ed into those.**
bc730 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   files..**.** Th
bc740 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62  is file should b
bc750 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74  e #included by t
bc760 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20  he os_*.c files 
bc770 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74  only.  It is not
bc780 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75   a.** general pu
bc790 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c  rpose header fil
bc7a0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f  e..*/.#ifndef _O
bc7b0 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66  S_COMMON_H_.#def
bc7c0 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  ine _OS_COMMON_H
bc7d0 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73  _../*.** At leas
bc7e0 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20  t two bugs have 
bc7f0 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75  slipped in becau
bc800 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68  se we changed th
bc810 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a  e MEMORY_DEBUG.*
bc820 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54  * macro to SQLIT
bc830 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65  E_DEBUG and some
bc840 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73   older makefiles
bc850 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61   have not yet ma
bc860 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68  de the.** switch
bc870 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
bc880 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74   code should cat
bc890 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ch this problem 
bc8a0 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e  at compile-time.
bc8b0 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52  .*/.#ifdef MEMOR
bc8c0 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20  Y_DEBUG.# error 
bc8d0 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55  "The MEMORY_DEBU
bc8e0 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c  G macro is obsol
bc8f0 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45  ete.  Use SQLITE
bc900 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22  _DEBUG instead."
bc910 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
bc920 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
bc930 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
bc940 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d  sqlite3OSTrace =
bc950 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52   0;.#define OSTR
bc960 41 43 45 31 28 58 29 20 20 20 20 20 20 20 20 20  ACE1(X)         
bc970 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
bc980 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
bc990 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69  gPrintf(X).#defi
bc9a0 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
bc9b0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
bc9c0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
bc9d0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
bc9e0 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
bc9f0 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20  ACE3(X,Y,Z)     
bca00 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
bca10 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
bca20 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23  gPrintf(X,Y,Z).#
bca30 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28  define OSTRACE4(
bca40 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73  X,Y,Z,A)   if( s
bca50 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
bca60 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
bca70 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66  tf(X,Y,Z,A).#def
bca80 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59  ine OSTRACE5(X,Y
bca90 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69  ,Z,A,B) if( sqli
bcaa0 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c  te3OSTrace ) sql
bcab0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
bcac0 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
bcad0 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
bcae0 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69  Z,A,B,C) \.    i
bcaf0 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
bcb00 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
bcb10 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
bcb20 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
bcb30 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  E7(X,Y,Z,A,B,C,D
bcb40 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74  ) \.    if(sqlit
bcb50 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74  e3OSTrace) sqlit
bcb60 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
bcb70 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c  Y,Z,A,B,C,D).#el
bcb80 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  se.#define OSTRA
bcb90 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f  CE1(X).#define O
bcba0 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65  STRACE2(X,Y).#de
bcbb0 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c  fine OSTRACE3(X,
bcbc0 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Y,Z).#define OST
bcbd0 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23  RACE4(X,Y,Z,A).#
bcbe0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28  define OSTRACE5(
bcbf0 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69  X,Y,Z,A,B).#defi
bcc00 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c  ne OSTRACE6(X,Y,
bcc10 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
bcc20 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
bcc30 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a  A,B,C,D).#endif.
bcc40 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f  ./*.** Macros fo
bcc50 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72  r performance tr
bcc60 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79  acing.  Normally
bcc70 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e   turned off.  On
bcc80 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69  ly works.** on i
bcc90 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f  486 hardware..*/
bcca0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50  .#ifdef SQLITE_P
bccb0 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45  ERFORMANCE_TRACE
bccc0 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e  ../* .** hwtime.
bccd0 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e  h contains inlin
bcce0 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65  e assembler code
bccf0 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e   for implementin
bcd00 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f  g .** high-perfo
bcd10 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f  rmance timing ro
bcd20 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a  utines..*/./****
bcd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
bcd40 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74  de hwtime.h in t
bcd50 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f  he middle of os_
bcd60 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
bcd70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
bcd80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
bcd90 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a   file hwtime.h *
bcda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
bcdd0 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a   2008 May 27.**.
bcde0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
bcdf0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
bce00 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
bce10 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
bce20 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
bce30 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
bce40 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
bce50 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
bce60 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
bce70 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
bce80 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
bce90 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
bcea0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
bceb0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
bcec0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
bced0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
bcee0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
bcef0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
bcf00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf40 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
bcf50 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65   contains inline
bcf60 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65   asm code for re
bcf70 74 72 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70  trieving "high-p
bcf80 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63  erformance".** c
bcf90 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20  ounters for x86 
bcfa0 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2f 0a 23  class CPUs..*/.#
bcfb0 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48  ifndef _HWTIME_H
bcfc0 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d  _.#define _HWTIM
bcfd0 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  E_H_../*.** The 
bcfe0 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
bcff0 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20  e only works on 
bd000 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f  pentium-class (o
bd010 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73  r newer) process
bd020 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20  ors..** It uses 
bd030 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65  the RDTSC opcode
bd040 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63   to read the cyc
bd050 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f  le count value o
bd060 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f  ut of the.** pro
bd070 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72  cessor and retur
bd080 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20  ns that value.  
bd090 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  This can be used
bd0a0 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a   for high-res.**
bd0b0 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23   profiling..*/.#
bd0c0 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
bd0d0 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  UC__) || defined
bd0e0 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c  (_MSC_VER)) && \
bd0f0 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28  .      (defined(
bd100 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64  i386) || defined
bd110 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65  (__i386__) || de
bd120 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a  fined(_M_IX86)).
bd130 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f  .  #if defined(_
bd140 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e  _GNUC__)..  __in
bd150 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69  line__ sqlite_ui
bd160 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
bd170 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75  me(void){.     u
bd180 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20  nsigned int lo, 
bd190 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f  hi;.     __asm__
bd1a0 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22   __volatile__ ("
bd1b0 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c  rdtsc" : "=a" (l
bd1c0 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a  o), "=d" (hi));.
bd1d0 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c       return (sql
bd1e0 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c  ite_uint64)hi <<
bd1f0 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20   32 | lo;.  }.. 
bd200 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f   #elif defined(_
bd210 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65  MSC_VER)..  __de
bd220 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f  clspec(naked) __
bd230 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69  inline sqlite_ui
bd240 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c  nt64 __cdecl sql
bd250 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
bd260 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20  {.     __asm {. 
bd270 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20         rdtsc.   
bd280 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b       ret       ;
bd290 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74   return value at
bd2a0 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a   EDX:EAX.     }.
bd2b0 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23    }..  #endif..#
bd2c0 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  elif (defined(__
bd2d0 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e  GNUC__) && defin
bd2e0 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a  ed(__x86_64__)).
bd2f0 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71  .  __inline__ sq
bd300 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69  lite_uint64 sqli
bd310 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
bd320 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
bd330 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20  long val;.      
bd340 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69  __asm__ __volati
bd350 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20  le__ ("rdtsc" : 
bd360 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20  "=A" (val));.   
bd370 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20     return val;. 
bd380 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69   }. .#elif (defi
bd390 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26  ned(__GNUC__) &&
bd3a0 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f   defined(__ppc__
bd3b0 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f  ))..  __inline__
bd3c0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
bd3d0 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
bd3e0 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  d){.      unsign
bd3f0 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74  ed long long ret
bd400 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67  val;.      unsig
bd410 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20  ned long junk;. 
bd420 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
bd430 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a  olatile__ ("\n\.
bd440 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20            1:    
bd450 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a    mftbu   %1\n\.
bd460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bd470 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c    mftb    %L0\n\
bd480 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
bd490 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c     mftbu   %0\n\
bd4a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
bd4b0 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31     cmpw    %0,%1
bd4c0 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
bd4d0 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62        bne     1b
bd4e0 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
bd4f0 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76      : "=r" (retv
bd500 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29  al), "=r" (junk)
bd510 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
bd520 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c  retval;.  }..#el
bd530 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65  se..  #error Nee
bd540 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  d implementation
bd550 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d   of sqlite3Hwtim
bd560 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61  e() for your pla
bd570 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a  tform...  /*.  *
bd580 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74  * To compile wit
bd590 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e  hout implementin
bd5a0 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  g sqlite3Hwtime(
bd5b0 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66  ) for your platf
bd5c0 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61  orm,.  ** you ca
bd5d0 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f  n remove the abo
bd5e0 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73  ve #error and us
bd5f0 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  e the following.
bd600 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69    ** stub functi
bd610 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f  on.  You will lo
bd620 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72  se timing suppor
bd630 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20  t for many.  ** 
bd640 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67  of the debugging
bd650 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69   and testing uti
bd660 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73  lities, but it s
bd670 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65  hould at.  ** le
bd680 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20  ast compile and 
bd690 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45  run..  */.SQLITE
bd6a0 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74  _PRIVATE   sqlit
bd6b0 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
bd6c0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65  Hwtime(void){ re
bd6d0 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69  turn ((sqlite_ui
bd6e0 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64  nt64)0); }..#end
bd6f0 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  if..#endif /* !d
bd700 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48  efined(_HWTIME_H
bd710 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  _) */../********
bd720 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77  ****** End of hw
bd730 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  time.h *********
bd740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd760 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
bd770 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
bd780 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
bd790 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e  off in os_common
bd7a0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
bd7b0 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73  *****/..static s
bd7c0 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73  qlite_uint64 g_s
bd7d0 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c  tart;.static sql
bd7e0 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61  ite_uint64 g_ela
bd7f0 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49  psed;.#define TI
bd800 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20  MER_START       
bd810 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48  g_start=sqlite3H
bd820 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20  wtime().#define 
bd830 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20  TIMER_END       
bd840 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69    g_elapsed=sqli
bd850 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74  te3Hwtime()-g_st
bd860 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  art.#define TIME
bd870 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f  R_ELAPSED     g_
bd880 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64  elapsed.#else.#d
bd890 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52  efine TIMER_STAR
bd8a0 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  T.#define TIMER_
bd8b0 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  END.#define TIME
bd8c0 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 28  R_ELAPSED     ((
bd8d0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29  sqlite_uint64)0)
bd8e0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
bd8f0 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74  f we compile wit
bd900 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53  h the SQLITE_TES
bd910 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65  T macro set, the
bd920 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  n the following 
bd930 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65  block.** of code
bd940 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68   will give us th
bd950 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d  e ability to sim
bd960 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f  ulate a disk I/O
bd970 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a   error.  This.**
bd980 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73   is used for tes
bd990 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63  ting the I/O rec
bd9a0 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a  overy logic..*/.
bd9b0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
bd9c0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
bd9d0 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
bd9e0 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20  or_hit = 0;     
bd9f0 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20         /* Total 
bda00 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72  number of I/O Er
bda10 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  rors */.SQLITE_A
bda20 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
bda30 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20  o_error_hardhit 
bda40 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  = 0;        /* N
bda50 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e  umber of non-ben
bda60 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51  ign errors */.SQ
bda70 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
bda80 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
bda90 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20  nding = 0;      
bdaa0 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20    /* Count down 
bdab0 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72  to first I/O err
bdac0 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  or */.SQLITE_API
bdad0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
bdae0 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20  error_persist = 
bdaf0 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  0;        /* Tru
bdb00 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20  e if I/O errors 
bdb10 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54  persist */.SQLIT
bdb20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
bdb30 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67  3_io_error_benig
bdb40 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  n = 0;         /
bdb50 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 73  * True if errors
bdb60 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53   are benign */.S
bdb70 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
bdb80 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
bdb90 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49  ending = 0;.SQLI
bdba0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
bdbb0 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b  e3_diskfull = 0;
bdbc0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
bdbd0 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58  eIOErrorBenign(X
bdbe0 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  ) sqlite3_io_err
bdbf0 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64  or_benign=(X).#d
bdc00 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f  efine SimulateIO
bdc10 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20  Error(CODE)  \. 
bdc20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f   if( (sqlite3_io
bdc30 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26  _error_persist &
bdc40 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  & sqlite3_io_err
bdc50 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20  or_hit) \.      
bdc60 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65   || sqlite3_io_e
bdc70 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d  rror_pending-- =
bdc80 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20  = 1 )  \.       
bdc90 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69         { local_i
bdca0 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a  oerr(); CODE; }.
bdcb0 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61  static void loca
bdcc0 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54  l_ioerr(){.  IOT
bdcd0 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29  RACE(("IOERR\n")
bdce0 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f  );.  sqlite3_io_
bdcf0 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69  error_hit++;.  i
bdd00 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65  f( !sqlite3_io_e
bdd10 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71  rror_benign ) sq
bdd20 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
bdd30 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66  ardhit++;.}.#def
bdd40 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  ine SimulateDisk
bdd50 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20  fullError(CODE) 
bdd60 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  \.   if( sqlite3
bdd70 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
bdd80 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20  g ){ \.     if( 
bdd90 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
bdda0 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b  _pending == 1 ){
bddb0 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f   \.       local_
bddc0 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20  ioerr(); \.     
bddd0 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75    sqlite3_diskfu
bdde0 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20  ll = 1; \.      
bddf0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
bde00 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20  r_hit = 1; \.   
bde10 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20      CODE; \.    
bde20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20   }else{ \.      
bde30 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
bde40 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20  l_pending--; \. 
bde50 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c      } \.   }.#el
bde60 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  se.#define Simul
bde70 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
bde80 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  (X).#define Simu
bde90 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23  lateIOError(A).#
bdea0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44  define SimulateD
bdeb0 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a  iskfullError(A).
bdec0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68  #endif../*.** Wh
bded0 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70  en testing, keep
bdee0 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20   a count of the 
bdef0 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66  number of open f
bdf00 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  iles..*/.#ifdef 
bdf10 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
bdf20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
bdf30 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75  e3_open_file_cou
bdf40 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  nt = 0;.#define 
bdf50 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20  OpenCounter(X)  
bdf60 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c  sqlite3_open_fil
bdf70 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c  e_count+=(X).#el
bdf80 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43  se.#define OpenC
bdf90 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66  ounter(X).#endif
bdfa0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
bdfb0 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f  ined(_OS_COMMON_
bdfc0 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  H_) */../*******
bdfd0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
bdfe0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
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 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
be020 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
be030 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
be040 20 6f 66 66 20 69 6e 20 6f 73 5f 6f 73 32 2e 63   off in os_os2.c
be050 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
be060 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ******/../*.** T
be070 68 65 20 6f 73 32 46 69 6c 65 20 73 74 72 75 63  he os2File struc
be080 74 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73  ture is subclass
be090 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   of sqlite3_file
be0a0 20 73 70 65 63 69 66 69 63 20 66 6f 72 20 74 68   specific for th
be0b0 65 20 4f 53 2f 32 0a 2a 2a 20 70 72 6f 74 61 62  e OS/2.** protab
be0c0 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a  ility layer..*/.
be0d0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 6f  typedef struct o
be0e0 73 32 46 69 6c 65 20 6f 73 32 46 69 6c 65 3b 0a  s2File os2File;.
be0f0 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 7b  struct os2File {
be100 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
be110 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65  _io_methods *pMe
be120 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73  thod;  /* Always
be130 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
be140 20 2a 2f 0a 20 20 48 46 49 4c 45 20 68 3b 20 20   */.  HFILE h;  
be150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be160 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61 63  /* Handle for ac
be170 63 65 73 73 69 6e 67 20 74 68 65 20 66 69 6c 65  cessing the file
be180 20 2a 2f 0a 20 20 63 68 61 72 2a 20 70 61 74 68   */.  char* path
be190 54 6f 44 65 6c 3b 20 20 20 20 20 20 20 20 20 20  ToDel;          
be1a0 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20  /* Name of file 
be1b0 74 6f 20 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f  to delete on clo
be1c0 73 65 2c 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20  se, NULL if not 
be1d0 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
be1e0 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 2f  ar locktype;   /
be1f0 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63  * Type of lock c
be200 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e  urrently held on
be210 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 7d 3b   this file */.};
be220 0a 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 5f 54  ..#define LOCK_T
be230 49 4d 45 4f 55 54 20 31 30 4c 20 2f 2a 20 74 68  IMEOUT 10L /* th
be240 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e  e default lockin
be250 67 20 74 69 6d 65 6f 75 74 20 2a 2f 0a 0a 2f 2a  g timeout */../*
be260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
be2b0 54 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f  The next group o
be2c0 66 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65  f routines imple
be2d0 6d 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 74  ment the I/O met
be2e0 68 6f 64 73 20 73 70 65 63 69 66 69 65 64 0a 2a  hods specified.*
be2f0 2a 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33  * by the sqlite3
be300 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
be310 63 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ct..************
be320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be360 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  **/../*.** Close
be370 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74   a file..*/.stat
be380 69 63 20 69 6e 74 20 6f 73 32 43 6c 6f 73 65 28  ic int os2Close(
be390 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
be3a0 64 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63  d ){.  APIRET rc
be3b0 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f   = NO_ERROR;.  o
be3c0 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20  s2File *pFile;. 
be3d0 20 69 66 28 20 69 64 20 26 26 20 28 70 46 69 6c   if( id && (pFil
be3e0 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64  e = (os2File*)id
be3f0 29 20 21 3d 20 30 20 29 7b 0a 20 20 20 20 4f 53  ) != 0 ){.    OS
be400 54 52 41 43 45 32 28 20 22 43 4c 4f 53 45 20 25  TRACE2( "CLOSE %
be410 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29  d\n", pFile->h )
be420 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 43 6c  ;.    rc = DosCl
be430 6f 73 65 28 20 70 46 69 6c 65 2d 3e 68 20 29 3b  ose( pFile->h );
be440 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
be450 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  type = NO_LOCK;.
be460 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70      if( pFile->p
be470 61 74 68 54 6f 44 65 6c 20 21 3d 20 4e 55 4c 4c  athToDel != NULL
be480 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 44   ){.      rc = D
be490 6f 73 46 6f 72 63 65 44 65 6c 65 74 65 28 20 28  osForceDelete( (
be4a0 50 53 5a 29 70 46 69 6c 65 2d 3e 70 61 74 68 54  PSZ)pFile->pathT
be4b0 6f 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 66 72  oDel );.      fr
be4c0 65 65 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54  ee( pFile->pathT
be4d0 6f 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 46  oDel );.      pF
be4e0 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d  ile->pathToDel =
be4f0 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 20   NULL;.    }.   
be500 20 69 64 20 3d 20 30 3b 0a 20 20 20 20 4f 70 65   id = 0;.    Ope
be510 6e 43 6f 75 6e 74 65 72 28 20 2d 31 20 29 3b 0a  nCounter( -1 );.
be520 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
be530 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53   == NO_ERROR ? S
be540 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54  QLITE_OK : SQLIT
be550 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a  E_IOERR;.}../*.*
be560 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d  * Read data from
be570 20 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62   a file into a b
be580 75 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53  uffer.  Return S
be590 51 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a  QLITE_OK if all.
be5a0 2a 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65  ** bytes were re
be5b0 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  ad successfully 
be5c0 61 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  and SQLITE_IOERR
be5d0 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65   if anything goe
be5e0 73 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73  s.** wrong..*/.s
be5f0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 52 65 61  tatic int os2Rea
be600 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  d(.  sqlite3_fil
be610 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20  e *id,          
be620 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20       /* File to 
be630 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76  read from */.  v
be640 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20  oid *pBuf,      
be650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
be660 2a 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 20  * Write content 
be670 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72  into this buffer
be680 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20   */.  int amt,  
be690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be6a0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
be6b0 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64  of bytes to read
be6c0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
be6d0 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20  t64 offset      
be6e0 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72        /* Begin r
be6f0 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f  eading at this o
be700 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c  ffset */.){.  UL
be710 4f 4e 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e  ONG fileLocation
be720 20 3d 20 30 4c 3b 0a 20 20 55 4c 4f 4e 47 20 67   = 0L;.  ULONG g
be730 6f 74 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70  ot;.  os2File *p
be740 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
be750 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69  )id;.  assert( i
be760 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61  d!=0 );.  Simula
be770 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
be780 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  n SQLITE_IOERR_R
be790 45 41 44 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  EAD );.  OSTRACE
be7a0 33 28 20 22 52 45 41 44 20 25 64 20 6c 6f 63 6b  3( "READ %d lock
be7b0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
be7c0 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  , pFile->locktyp
be7d0 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 65  e );.  if( DosSe
be7e0 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e  tFilePtr(pFile->
be7f0 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f  h, offset, FILE_
be800 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61  BEGIN, &fileLoca
be810 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f  tion) != NO_ERRO
be820 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  R ){.    return 
be830 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20  SQLITE_IOERR;.  
be840 7d 0a 20 20 69 66 28 20 44 6f 73 52 65 61 64 28  }.  if( DosRead(
be850 20 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c   pFile->h, pBuf,
be860 20 61 6d 74 2c 20 26 67 6f 74 20 29 20 21 3d 20   amt, &got ) != 
be870 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
be880 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
be890 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20  ERR_READ;.  }.  
be8a0 69 66 28 20 67 6f 74 20 3d 3d 20 28 55 4c 4f 4e  if( got == (ULON
be8b0 47 29 61 6d 74 20 29 0a 20 20 20 20 72 65 74 75  G)amt ).    retu
be8c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
be8d0 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 55 6e  else {.    /* Un
be8e0 72 65 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66  read portions of
be8f0 20 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65   the input buffe
be900 72 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 66  r must be zero-f
be910 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d  illed */.    mem
be920 73 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 75  set(&((char*)pBu
be930 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d  f)[got], 0, amt-
be940 67 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  got);.    return
be950 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48   SQLITE_IOERR_SH
be960 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a  ORT_READ;.  }.}.
be970 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74  ./*.** Write dat
be980 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20  a from a buffer 
be990 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65  into a file.  Re
be9a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
be9b0 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20  n success.** or 
be9c0 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72  some other error
be9d0 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65   code on failure
be9e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
be9f0 6f 73 32 57 72 69 74 65 28 0a 20 20 73 71 6c 69  os2Write(.  sqli
bea00 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20  te3_file *id,   
bea10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
bea20 69 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74  ile to write int
bea30 6f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  o */.  const voi
bea40 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20  d *pBuf,        
bea50 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 79         /* The by
bea60 74 65 73 20 74 6f 20 62 65 20 77 72 69 74 74 65  tes to be writte
bea70 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20  n */.  int amt, 
bea80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bea90 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
beaa0 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69   of bytes to wri
beab0 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  te */.  sqlite3_
beac0 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20  int64 offset    
bead0 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
beae0 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20  t into the file 
beaf0 74 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e 67  to begin writing
beb00 20 61 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e   at */.){.  ULON
beb10 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d  G fileLocation =
beb20 20 30 4c 3b 0a 20 20 41 50 49 52 45 54 20 72 63   0L;.  APIRET rc
beb30 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55   = NO_ERROR;.  U
beb40 4c 4f 4e 47 20 77 72 6f 74 65 3b 0a 20 20 6f 73  LONG wrote;.  os
beb50 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
beb60 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61  os2File*)id;.  a
beb70 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
beb80 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
beb90 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
beba0 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 29 3b 0a  _IOERR_WRITE );.
bebb0 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75    SimulateDiskfu
bebc0 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  llError( return 
bebd0 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 20  SQLITE_FULL );. 
bebe0 20 4f 53 54 52 41 43 45 33 28 20 22 57 52 49 54   OSTRACE3( "WRIT
bebf0 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c  E %d lock=%d\n",
bec00 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65   pFile->h, pFile
bec10 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ->locktype );.  
bec20 69 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74  if( DosSetFilePt
bec30 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73  r(pFile->h, offs
bec40 65 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20  et, FILE_BEGIN, 
bec50 26 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21  &fileLocation) !
bec60 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
bec70 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
bec80 49 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 61 73 73  IOERR;.  }.  ass
bec90 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20  ert( amt>0 );.  
beca0 77 68 69 6c 65 28 20 61 6d 74 20 3e 20 30 20 26  while( amt > 0 &
becb0 26 0a 20 20 20 20 20 20 20 20 20 28 20 72 63 20  &.         ( rc 
becc0 3d 20 44 6f 73 57 72 69 74 65 28 20 70 46 69 6c  = DosWrite( pFil
becd0 65 2d 3e 68 2c 20 28 50 56 4f 49 44 29 70 42 75  e->h, (PVOID)pBu
bece0 66 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 20 29  f, amt, &wrote )
becf0 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 26   ) == NO_ERROR &
bed00 26 0a 20 20 20 20 20 20 20 20 20 77 72 6f 74 65  &.         wrote
bed10 20 3e 20 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d   > 0.  ){.    am
bed20 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20  t -= wrote;.    
bed30 70 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29  pBuf = &((char*)
bed40 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20  pBuf)[wrote];.  
bed50 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 20 72 63  }..  return ( rc
bed60 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 7c 7c 20   != NO_ERROR || 
bed70 61 6d 74 20 3e 20 28 69 6e 74 29 77 72 6f 74 65  amt > (int)wrote
bed80 20 29 20 3f 20 53 51 4c 49 54 45 5f 46 55 4c 4c   ) ? SQLITE_FULL
bed90 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a   : SQLITE_OK;.}.
beda0 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20  ./*.** Truncate 
bedb0 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20  an open file to 
bedc0 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65  a specified size
bedd0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
bede0 73 32 54 72 75 6e 63 61 74 65 28 20 73 71 6c 69  s2Truncate( sqli
bedf0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36  te3_file *id, i6
bee00 34 20 6e 42 79 74 65 20 29 7b 0a 20 20 41 50 49  4 nByte ){.  API
bee10 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f  RET rc = NO_ERRO
bee20 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46  R;.  os2File *pF
bee30 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29  ile = (os2File*)
bee40 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20  id;.  OSTRACE3( 
bee50 22 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c 6c  "TRUNCATE %d %ll
bee60 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
bee70 6e 42 79 74 65 20 29 3b 0a 20 20 53 69 6d 75 6c  nByte );.  Simul
bee80 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75  ateIOError( retu
bee90 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
beea0 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63  TRUNCATE );.  rc
beeb0 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 53 69 7a   = DosSetFileSiz
beec0 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79  e( pFile->h, nBy
beed0 74 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  te );.  return r
beee0 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20  c == NO_ERROR ? 
beef0 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49  SQLITE_OK : SQLI
bef00 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54  TE_IOERR_TRUNCAT
bef10 45 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  E;.}..#ifdef SQL
bef20 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43  ITE_TEST./*.** C
bef30 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20  ount the number 
bef40 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64  of fullsyncs and
bef50 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20   normal syncs.  
bef60 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20  This is used to 
bef70 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e  test.** that syn
bef80 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73  cs and fullsyncs
bef90 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74   are occuring at
befa0 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73   the right times
befb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
befc0 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63  int sqlite3_sync
befd0 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49  _count = 0;.SQLI
befe0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
beff0 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e  e3_fullsync_coun
bf000 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  t = 0;.#endif../
bf010 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61  *.** Make sure a
bf020 6c 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 70  ll writes to a p
bf030 61 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61  articular file a
bf040 72 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20  re committed to 
bf050 64 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  disk..*/.static 
bf060 69 6e 74 20 6f 73 32 53 79 6e 63 28 20 73 71 6c  int os2Sync( sql
bf070 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
bf080 6e 74 20 66 6c 61 67 73 20 29 7b 0a 20 20 6f 73  nt flags ){.  os
bf090 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
bf0a0 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f  os2File*)id;.  O
bf0b0 53 54 52 41 43 45 33 28 20 22 53 59 4e 43 20 25  STRACE3( "SYNC %
bf0c0 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46  d lock=%d\n", pF
bf0d0 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c  ile->h, pFile->l
bf0e0 6f 63 6b 74 79 70 65 20 29 3b 0a 23 69 66 64 65  ocktype );.#ifde
bf0f0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
bf100 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
bf110 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 29 7b 0a 20  TE_SYNC_FULL){. 
bf120 20 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73     sqlite3_fulls
bf130 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d  ync_count++;.  }
bf140 0a 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f  .  sqlite3_sync_
bf150 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a  count++;.#endif.
bf160 20 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69    /* If we compi
bf170 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c  led with the SQL
bf180 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67  ITE_NO_SYNC flag
bf190 2c 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69  , then syncing i
bf1a0 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20  s a.  ** no-op. 
bf1b0 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54   */.#ifdef SQLIT
bf1c0 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53  E_NO_SYNC.  UNUS
bf1d0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 46 69  ED_PARAMETER(pFi
bf1e0 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  le);.  return SQ
bf1f0 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20  LITE_OK;.#else. 
bf200 20 72 65 74 75 72 6e 20 44 6f 73 52 65 73 65 74   return DosReset
bf210 42 75 66 66 65 72 28 20 70 46 69 6c 65 2d 3e 68  Buffer( pFile->h
bf220 20 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f   ) == NO_ERROR ?
bf230 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c   SQLITE_OK : SQL
bf240 49 54 45 5f 49 4f 45 52 52 3b 0a 23 65 6e 64 69  ITE_IOERR;.#endi
bf250 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72  f.}../*.** Deter
bf260 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65 6e 74  mine the current
bf270 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c 65 20   size of a file 
bf280 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74 61 74  in bytes.*/.stat
bf290 69 63 20 69 6e 74 20 6f 73 32 46 69 6c 65 53 69  ic int os2FileSi
bf2a0 7a 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ze( sqlite3_file
bf2b0 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e   *id, sqlite3_in
bf2c0 74 36 34 20 2a 70 53 69 7a 65 20 29 7b 0a 20 20  t64 *pSize ){.  
bf2d0 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45  APIRET rc = NO_E
bf2e0 52 52 4f 52 3b 0a 20 20 46 49 4c 45 53 54 41 54  RROR;.  FILESTAT
bf2f0 55 53 33 20 66 73 74 73 33 46 69 6c 65 49 6e 66  US3 fsts3FileInf
bf300 6f 3b 0a 20 20 6d 65 6d 73 65 74 28 26 66 73 74  o;.  memset(&fst
bf310 73 33 46 69 6c 65 49 6e 66 6f 2c 20 30 2c 20 73  s3FileInfo, 0, s
bf320 69 7a 65 6f 66 28 66 73 74 73 33 46 69 6c 65 49  izeof(fsts3FileI
bf330 6e 66 6f 29 29 3b 0a 20 20 61 73 73 65 72 74 28  nfo));.  assert(
bf340 20 69 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75   id!=0 );.  Simu
bf350 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
bf360 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
bf370 5f 46 53 54 41 54 20 29 3b 0a 20 20 72 63 20 3d  _FSTAT );.  rc =
bf380 20 44 6f 73 51 75 65 72 79 46 69 6c 65 49 6e 66   DosQueryFileInf
bf390 6f 28 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64  o( ((os2File*)id
bf3a0 29 2d 3e 68 2c 20 46 49 4c 5f 53 54 41 4e 44 41  )->h, FIL_STANDA
bf3b0 52 44 2c 20 26 66 73 74 73 33 46 69 6c 65 49 6e  RD, &fsts3FileIn
bf3c0 66 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53  fo, sizeof(FILES
bf3d0 54 41 54 55 53 33 29 20 29 3b 0a 20 20 69 66 28  TATUS3) );.  if(
bf3e0 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc == NO_ERROR 
bf3f0 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20  ){.    *pSize = 
bf400 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2e 63 62  fsts3FileInfo.cb
bf410 46 69 6c 65 3b 0a 20 20 20 20 72 65 74 75 72 6e  File;.    return
bf420 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
bf430 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
bf440 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54  SQLITE_IOERR_FST
bf450 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  AT;.  }.}../*.**
bf460 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64 65   Acquire a reade
bf470 72 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69  r lock..*/.stati
bf480 63 20 69 6e 74 20 67 65 74 52 65 61 64 4c 6f 63  c int getReadLoc
bf490 6b 28 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c  k( os2File *pFil
bf4a0 65 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20  e ){.  FILELOCK 
bf4b0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20   LockArea,.     
bf4c0 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65         UnlockAre
bf4d0 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b  a;.  APIRET res;
bf4e0 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41  .  memset(&LockA
bf4f0 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c  rea, 0, sizeof(L
bf500 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d  ockArea));.  mem
bf510 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  set(&UnlockArea,
bf520 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63   0, sizeof(Unloc
bf530 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41  kArea));.  LockA
bf540 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48  rea.lOffset = SH
bf550 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 4c 6f  ARED_FIRST;.  Lo
bf560 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
bf570 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 55  SHARED_SIZE;.  U
bf580 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
bf590 74 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b  t = 0L;.  Unlock
bf5a0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
bf5b0 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74  ;.  res = DosSet
bf5c0 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65  FileLocks( pFile
bf5d0 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61  ->h, &UnlockArea
bf5e0 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43  , &LockArea, LOC
bf5f0 4b 5f 54 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b  K_TIMEOUT, 1L );
bf600 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 47 45  .  OSTRACE3( "GE
bf610 54 52 45 41 44 4c 4f 43 4b 20 25 64 20 72 65 73  TREADLOCK %d res
bf620 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
bf630 2c 20 72 65 73 20 29 3b 0a 20 20 72 65 74 75 72  , res );.  retur
bf640 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n res;.}../*.** 
bf650 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a  Undo a readlock.
bf660 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
bf670 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 20 6f 73  lockReadLock( os
bf680 32 46 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 46  2File *id ){.  F
bf690 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65  ILELOCK  LockAre
bf6a0 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55  a,.            U
bf6b0 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49  nlockArea;.  API
bf6c0 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65  RET res;.  memse
bf6d0 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  t(&LockArea, 0, 
bf6e0 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29  sizeof(LockArea)
bf6f0 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c  );.  memset(&Unl
bf700 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65  ockArea, 0, size
bf710 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b  of(UnlockArea));
bf720 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66  .  LockArea.lOff
bf730 73 65 74 20 3d 20 30 4c 3b 0a 20 20 4c 6f 63 6b  set = 0L;.  Lock
bf740 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
bf750 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c  ;.  UnlockArea.l
bf760 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f  Offset = SHARED_
bf770 46 49 52 53 54 3b 0a 20 20 55 6e 6c 6f 63 6b 41  FIRST;.  UnlockA
bf780 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41  rea.lRange = SHA
bf790 52 45 44 5f 53 49 5a 45 3b 0a 20 20 72 65 73 20  RED_SIZE;.  res 
bf7a0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
bf7b0 73 28 20 69 64 2d 3e 68 2c 20 26 55 6e 6c 6f 63  s( id->h, &Unloc
bf7c0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
bf7d0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
bf7e0 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33  1L );.  OSTRACE3
bf7f0 28 20 22 55 4e 4c 4f 43 4b 2d 52 45 41 44 4c 4f  ( "UNLOCK-READLO
bf800 43 4b 20 66 69 6c 65 20 68 61 6e 64 6c 65 3d 25  CK file handle=%
bf810 64 20 72 65 73 3d 25 64 3f 5c 6e 22 2c 20 69 64  d res=%d?\n", id
bf820 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 72 65  ->h, res );.  re
bf830 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a  turn res;.}../*.
bf840 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65  ** Lock the file
bf850 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73   with the lock s
bf860 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61  pecified by para
bf870 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d  meter locktype -
bf880 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66   one.** of the f
bf890 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
bf8a0 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c      (1) SHARED_L
bf8b0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52  OCK.**     (2) R
bf8c0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20  ESERVED_LOCK.** 
bf8d0 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f      (3) PENDING_
bf8e0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20  LOCK.**     (4) 
bf8f0 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a  EXCLUSIVE_LOCK.*
bf900 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  *.** Sometimes w
bf910 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f  hen requesting o
bf920 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61  ne lock state, a
bf930 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73  dditional lock s
bf940 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73  tates.** are ins
bf950 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e  erted in between
bf960 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d  .  The locking m
bf970 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65  ight fail on one
bf980 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a   of the later.**
bf990 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61   transitions lea
bf9a0 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74  ving the lock st
bf9b0 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72  ate different fr
bf9c0 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74  om what it start
bf9d0 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20  ed but.** still 
bf9e0 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61  short of its goa
bf9f0 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  l.  The followin
bfa00 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68  g chart shows th
bfa10 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61  e allowed.** tra
bfa20 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nsitions and the
bfa30 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d   inserted interm
bfa40 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a  ediate states:.*
bfa50 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44  *.**    UNLOCKED
bfa60 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20   -> SHARED.**   
bfa70 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52   SHARED -> RESER
bfa80 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  VED.**    SHARED
bfa90 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
bfaa0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
bfab0 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45   RESERVED -> (PE
bfac0 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
bfad0 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  IVE.**    PENDIN
bfae0 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  G -> EXCLUSIVE.*
bfaf0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
bfb00 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72  e will only incr
bfb10 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68  ease a lock.  Th
bfb20 65 20 6f 73 32 55 6e 6c 6f 63 6b 28 29 20 72 6f  e os2Unlock() ro
bfb30 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20  utine.** erases 
bfb40 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63  all locks at onc
bfb50 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73  e and returns us
bfb60 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20   immediately to 
bfb70 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e  locking level 0.
bfb80 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f  .** It is not po
bfb90 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20  ssible to lower 
bfba0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
bfbb0 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20  l one step at a 
bfbc0 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75  time.  You.** mu
bfbd0 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 74  st go straight t
bfbe0 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  o locking level 
bfbf0 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  0..*/.static int
bfc00 20 6f 73 32 4c 6f 63 6b 28 20 73 71 6c 69 74 65   os2Lock( sqlite
bfc10 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
bfc20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 69 6e  locktype ){.  in
bfc30 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
bfc40 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72  ;       /* Retur
bfc50 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62 72  n code from subr
bfc60 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 41 50 49  outines */.  API
bfc70 52 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52  RET res = NO_ERR
bfc80 4f 52 3b 20 20 20 20 2f 2a 20 52 65 73 75 6c 74  OR;    /* Result
bfc90 20 6f 66 20 61 6e 20 4f 53 2f 32 20 6c 6f 63 6b   of an OS/2 lock
bfca0 20 63 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e   call */.  int n
bfcb0 65 77 4c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20  ewLocktype;     
bfcc0 20 20 2f 2a 20 53 65 74 20 70 46 69 6c 65 2d 3e    /* Set pFile->
bfcd0 6c 6f 63 6b 74 79 70 65 20 74 6f 20 74 68 69 73  locktype to this
bfce0 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20 65 78   value before ex
bfcf0 69 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 67  iting */.  int g
bfd00 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20  otPendingLock = 
bfd10 30 3b 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20  0;/* True if we 
bfd20 61 63 71 75 69 72 65 64 20 61 20 50 45 4e 44 49  acquired a PENDI
bfd30 4e 47 20 6c 6f 63 6b 20 74 68 69 73 20 74 69 6d  NG lock this tim
bfd40 65 20 2a 2f 0a 20 20 46 49 4c 45 4c 4f 43 4b 20  e */.  FILELOCK 
bfd50 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20   LockArea,.     
bfd60 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65         UnlockAre
bfd70 61 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46  a;.  os2File *pF
bfd80 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29  ile = (os2File*)
bfd90 69 64 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f  id;.  memset(&Lo
bfda0 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f  ckArea, 0, sizeo
bfdb0 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20  f(LockArea));.  
bfdc0 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72  memset(&UnlockAr
bfdd0 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e  ea, 0, sizeof(Un
bfde0 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 61 73  lockArea));.  as
bfdf0 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29  sert( pFile!=0 )
bfe00 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c  ;.  OSTRACE4( "L
bfe10 4f 43 4b 20 25 64 20 25 64 20 77 61 73 20 25 64  OCK %d %d was %d
bfe20 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c  \n", pFile->h, l
bfe30 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
bfe40 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 0a 20 20 2f  locktype );..  /
bfe50 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c  * If there is al
bfe60 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20  ready a lock of 
bfe70 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72  this type or mor
bfe80 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e  e restrictive on
bfe90 20 74 68 65 0a 20 20 2a 2a 20 6f 73 32 46 69 6c   the.  ** os2Fil
bfea0 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44  e, do nothing. D
bfeb0 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64  on't use the end
bfec0 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68  _lock: exit path
bfed0 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65  , as.  ** sqlite
bfee0 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20  3_mutex_enter() 
bfef0 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c  hasn't been call
bff00 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69  ed yet..  */.  i
bff10 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
bff20 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  pe>=locktype ){.
bff30 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c      OSTRACE3( "L
bff40 4f 43 4b 20 25 64 20 25 64 20 6f 6b 20 28 61 6c  OCK %d %d ok (al
bff50 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20  ready held)\n", 
bff60 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
bff70 70 65 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  pe );.    return
bff80 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
bff90 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .  /* Make sure 
bffa0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71 75  the locking sequ
bffb0 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74 0a  ence is correct.
bffc0 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
bffd0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d  File->locktype!=
bffe0 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74  NO_LOCK || lockt
bfff0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
c0000 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f   );.  assert( lo
c0010 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47 5f  cktype!=PENDING_
c0020 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
c0030 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53 45  ( locktype!=RESE
c0040 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46 69  RVED_LOCK || pFi
c0050 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  le->locktype==SH
c0060 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20  ARED_LOCK );..  
c0070 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e 44  /* Lock the PEND
c0080 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 66  ING_LOCK byte if
c0090 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71 75   we need to acqu
c00a0 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  ire a PENDING lo
c00b0 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48 41  ck or.  ** a SHA
c00c0 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77 65  RED lock.  If we
c00d0 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 20 61   are acquiring a
c00e0 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68   SHARED lock, th
c00f0 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f 66  e acquisition of
c0100 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49 4e  .  ** the PENDIN
c0110 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20 74  G_LOCK byte is t
c0120 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a 20  emporary..  */. 
c0130 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 70   newLocktype = p
c0140 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a  File->locktype;.
c0150 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
c0160 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 0a 20  ktype==NO_LOCK. 
c0170 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70       || (locktyp
c0180 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
c0190 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  K && pFile->lock
c01a0 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c  type==RESERVED_L
c01b0 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20 4c 6f  OCK).  ){.    Lo
c01c0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c01d0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20   PENDING_BYTE;. 
c01e0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e     LockArea.lRan
c01f0 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c  ge = 1L;.    Unl
c0200 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c0210 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 0L;.    Unlock
c0220 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
c0230 3b 0a 0a 20 20 20 20 2f 2a 20 77 61 69 74 20 6c  ;..    /* wait l
c0240 6f 6e 67 65 72 20 74 68 61 6e 20 4c 4f 43 4b 5f  onger than LOCK_
c0250 54 49 4d 45 4f 55 54 20 68 65 72 65 20 6e 6f 74  TIMEOUT here not
c0260 20 74 6f 20 68 61 76 65 20 74 6f 20 74 72 79 20   to have to try 
c0270 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 2a  multiple times *
c0280 2f 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53  /.    res = DosS
c0290 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69  etFileLocks( pFi
c02a0 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72  le->h, &UnlockAr
c02b0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 31  ea, &LockArea, 1
c02c0 30 30 4c 2c 20 30 4c 20 29 3b 0a 20 20 20 20 69  00L, 0L );.    i
c02d0 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52  f( res == NO_ERR
c02e0 4f 52 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 50  OR ){.      gotP
c02f0 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 31 3b 0a  endingLock = 1;.
c0300 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20        OSTRACE3( 
c0310 22 4c 4f 43 4b 20 25 64 20 70 65 6e 64 69 6e 67  "LOCK %d pending
c0320 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 6e 20 73 65   lock boolean se
c0330 74 2e 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70  t.  res=%d\n", p
c0340 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a  File->h, res );.
c0350 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
c0360 41 63 71 75 69 72 65 20 61 20 73 68 61 72 65 64  Acquire a shared
c0370 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
c0380 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
c0390 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d  D_LOCK && res ==
c03a0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
c03b0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
c03c0 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
c03d0 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67  K );.    res = g
c03e0 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65  etReadLock(pFile
c03f0 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d  );.    if( res =
c0400 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
c0410 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
c0420 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20  = SHARED_LOCK;. 
c0430 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43 45     }.    OSTRACE
c0440 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75  3( "LOCK %d acqu
c0450 69 72 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e  ire shared lock.
c0460 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   res=%d\n", pFil
c0470 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d  e->h, res );.  }
c0480 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
c0490 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 0a 20   RESERVED lock. 
c04a0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79   */.  if( lockty
c04b0 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe==RESERVED_LOC
c04c0 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45  K && res == NO_E
c04d0 52 52 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65  RROR ){.    asse
c04e0 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  rt( pFile->lockt
c04f0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
c0500 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61   );.    LockArea
c0510 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52  .lOffset = RESER
c0520 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f  VED_BYTE;.    Lo
c0530 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c0540 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  1L;.    UnlockAr
c0550 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b  ea.lOffset = 0L;
c0560 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  .    UnlockArea.
c0570 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20  lRange = 0L;.   
c0580 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c   res = DosSetFil
c0590 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68  eLocks( pFile->h
c05a0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
c05b0 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54  LockArea, LOCK_T
c05c0 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20  IMEOUT, 0L );.  
c05d0 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f    if( res == NO_
c05e0 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e  ERROR ){.      n
c05f0 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53  ewLocktype = RES
c0600 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20  ERVED_LOCK;.    
c0610 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20  }.    OSTRACE3( 
c0620 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65  "LOCK %d acquire
c0630 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 2e 20   reserved lock. 
c0640 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  res=%d\n", pFile
c0650 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a  ->h, res );.  }.
c0660 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20  .  /* Acquire a 
c0670 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 0a 20 20 2a  PENDING lock.  *
c0680 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  /.  if( locktype
c0690 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
c06a0 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52   && res == NO_ER
c06b0 52 4f 52 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f  ROR ){.    newLo
c06c0 63 6b 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47  cktype = PENDING
c06d0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65  _LOCK;.    gotPe
c06e0 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20  ndingLock = 0;. 
c06f0 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 4c 4f     OSTRACE2( "LO
c0700 43 4b 20 25 64 20 61 63 71 75 69 72 65 20 70 65  CK %d acquire pe
c0710 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20 70 65 6e 64  nding lock. pend
c0720 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61 6e  ing lock boolean
c0730 20 75 6e 73 65 74 2e 5c 6e 22 2c 20 70 46 69 6c   unset.\n", pFil
c0740 65 2d 3e 68 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  e->h );.  }..  /
c0750 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 43  * Acquire an EXC
c0760 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f  LUSIVE lock.  */
c0770 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
c0780 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
c0790 26 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52  && res == NO_ERR
c07a0 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  OR ){.    assert
c07b0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
c07c0 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e>=SHARED_LOCK )
c07d0 3b 0a 20 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f  ;.    res = unlo
c07e0 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65  ckReadLock(pFile
c07f0 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28  );.    OSTRACE2(
c0800 20 22 75 6e 72 65 61 64 6c 6f 63 6b 20 3d 20 25   "unreadlock = %
c0810 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a 20 20 20  d\n", res );.   
c0820 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
c0830 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54  t = SHARED_FIRST
c0840 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ;.    LockArea.l
c0850 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53  Range = SHARED_S
c0860 49 5a 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41  IZE;.    UnlockA
c0870 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
c0880 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
c0890 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
c08a0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69    res = DosSetFi
c08b0 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e  leLocks( pFile->
c08c0 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  h, &UnlockArea, 
c08d0 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f  &LockArea, LOCK_
c08e0 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20  TIMEOUT, 0L );. 
c08f0 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f     if( res == NO
c0900 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20  _ERROR ){.      
c0910 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 45 58  newLocktype = EX
c0920 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b 0a 20 20  CLUSIVE_LOCK;.  
c0930 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 4f    }else{.      O
c0940 53 54 52 41 43 45 32 28 20 22 4f 53 2f 32 20 65  STRACE2( "OS/2 e
c0950 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e  rror-code = %d\n
c0960 22 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 20 20  ", res );.      
c0970 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  getReadLock(pFil
c0980 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53  e);.    }.    OS
c0990 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64  TRACE3( "LOCK %d
c09a0 20 61 63 71 75 69 72 65 20 65 78 63 6c 75 73 69   acquire exclusi
c09b0 76 65 20 6c 6f 63 6b 2e 20 20 72 65 73 3d 25 64  ve lock.  res=%d
c09c0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72  \n", pFile->h, r
c09d0 65 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  es );.  }..  /* 
c09e0 49 66 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e  If we are holdin
c09f0 67 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  g a PENDING lock
c0a00 20 74 68 61 74 20 6f 75 67 68 74 20 74 6f 20 62   that ought to b
c0a10 65 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 6e  e released, then
c0a20 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65 20 69 74  .  ** release it
c0a30 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   now..  */.  if(
c0a40 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20   gotPendingLock 
c0a50 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  && locktype==SHA
c0a60 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
c0a70 69 6e 74 20 72 3b 0a 20 20 20 20 4c 6f 63 6b 41  int r;.    LockA
c0a80 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
c0a90 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ;.    LockArea.l
c0aa0 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20  Range = 0L;.    
c0ab0 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
c0ac0 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54  et = PENDING_BYT
c0ad0 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  E;.    UnlockAre
c0ae0 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20  a.lRange = 1L;. 
c0af0 20 20 20 72 20 3d 20 44 6f 73 53 65 74 46 69 6c     r = DosSetFil
c0b00 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68  eLocks( pFile->h
c0b10 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
c0b20 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54  LockArea, LOCK_T
c0b30 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20  IMEOUT, 0L );.  
c0b40 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43    OSTRACE3( "LOC
c0b50 4b 20 25 64 20 75 6e 6c 6f 63 6b 69 6e 67 20 70  K %d unlocking p
c0b60 65 6e 64 69 6e 67 2f 69 73 20 73 68 61 72 65 64  ending/is shared
c0b70 2e 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  . r=%d\n", pFile
c0b80 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 0a 20  ->h, r );.  }.. 
c0b90 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 73   /* Update the s
c0ba0 74 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b  tate of the lock
c0bb0 20 68 61 73 20 68 65 6c 64 20 69 6e 20 74 68 65   has held in the
c0bc0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
c0bd0 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72   then.  ** retur
c0be0 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
c0bf0 65 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20  e result code.. 
c0c00 20 2a 2f 0a 20 20 69 66 28 20 72 65 73 20 3d 3d   */.  if( res ==
c0c10 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
c0c20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
c0c30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4f 53  .  }else{.    OS
c0c40 54 52 41 43 45 34 28 20 22 4c 4f 43 4b 20 46 41  TRACE4( "LOCK FA
c0c50 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20 66  ILED %d trying f
c0c60 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25 64  or %d but got %d
c0c70 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20  \n", pFile->h,. 
c0c80 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63               loc
c0c90 6b 74 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79  ktype, newLockty
c0ca0 70 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  pe );.    rc = S
c0cb0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a  QLITE_BUSY;.  }.
c0cc0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
c0cd0 65 20 3d 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b  e = newLocktype;
c0ce0 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f  .  OSTRACE3( "LO
c0cf0 43 4b 20 25 64 20 6e 6f 77 20 25 64 5c 6e 22 2c  CK %d now %d\n",
c0d00 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65   pFile->h, pFile
c0d10 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ->locktype );.  
c0d20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
c0d30 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
c0d40 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
c0d50 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
c0d60 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
c0d70 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
c0d80 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
c0d90 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
c0da0 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
c0db0 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a  s held, return.*
c0dc0 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65  * non-zero, othe
c0dd0 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73  rwise zero..*/.s
c0de0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 43 68 65  tatic int os2Che
c0df0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 20  ckReservedLock( 
c0e00 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
c0e10 2c 20 69 6e 74 20 2a 70 4f 75 74 20 29 7b 0a 20  , int *pOut ){. 
c0e20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20 20 6f 73   int r = 0;.  os
c0e30 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
c0e40 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61  os2File*)id;.  a
c0e50 73 73 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20  ssert( pFile!=0 
c0e60 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  );.  if( pFile->
c0e70 6c 6f 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56  locktype>=RESERV
c0e80 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72  ED_LOCK ){.    r
c0e90 20 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 43   = 1;.    OSTRAC
c0ea0 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43  E3( "TEST WR-LOC
c0eb0 4b 20 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c  K %d %d (local)\
c0ec0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20  n", pFile->h, r 
c0ed0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
c0ee0 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72  FILELOCK  LockAr
c0ef0 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ea,.            
c0f00 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20    UnlockArea;.  
c0f10 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f    APIRET rc = NO
c0f20 5f 45 52 52 4f 52 3b 0a 20 20 20 20 6d 65 6d 73  _ERROR;.    mems
c0f30 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c  et(&LockArea, 0,
c0f40 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61   sizeof(LockArea
c0f50 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  ));.    memset(&
c0f60 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73  UnlockArea, 0, s
c0f70 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61  izeof(UnlockArea
c0f80 29 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  ));.    LockArea
c0f90 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52  .lOffset = RESER
c0fa0 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f  VED_BYTE;.    Lo
c0fb0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c0fc0 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  1L;.    UnlockAr
c0fd0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b  ea.lOffset = 0L;
c0fe0 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  .    UnlockArea.
c0ff0 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20  lRange = 0L;.   
c1000 20 72 63 20 3d 20 44 6f 73 53 65 74 46 69 6c 65   rc = DosSetFile
c1010 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c  Locks( pFile->h,
c1020 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c   &UnlockArea, &L
c1030 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49  ockArea, LOCK_TI
c1040 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20  MEOUT, 0L );.   
c1050 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54   OSTRACE3( "TEST
c1060 20 57 52 2d 4c 4f 43 4b 20 25 64 20 6c 6f 63 6b   WR-LOCK %d lock
c1070 20 72 65 73 65 72 76 65 64 20 62 79 74 65 20 72   reserved byte r
c1080 63 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  c=%d\n", pFile->
c1090 68 2c 20 72 63 20 29 3b 0a 20 20 20 20 69 66 28  h, rc );.    if(
c10a0 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc == NO_ERROR 
c10b0 29 7b 0a 20 20 20 20 20 20 41 50 49 52 45 54 20  ){.      APIRET 
c10c0 72 63 75 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20  rcu = NO_ERROR; 
c10d0 2f 2a 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66  /* return code f
c10e0 6f 72 20 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f 0a  or unlocking */.
c10f0 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c        LockArea.l
c1100 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20  Offset = 0L;.   
c1110 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e     LockArea.lRan
c1120 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20 55  ge = 0L;.      U
c1130 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65  nlockArea.lOffse
c1140 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54  t = RESERVED_BYT
c1150 45 3b 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41  E;.      UnlockA
c1160 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b  rea.lRange = 1L;
c1170 0a 20 20 20 20 20 20 72 63 75 20 3d 20 44 6f 73  .      rcu = Dos
c1180 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46  SetFileLocks( pF
c1190 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41  ile->h, &UnlockA
c11a0 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20  rea, &LockArea, 
c11b0 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c  LOCK_TIMEOUT, 0L
c11c0 20 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43   );.      OSTRAC
c11d0 45 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43  E3( "TEST WR-LOC
c11e0 4b 20 25 64 20 75 6e 6c 6f 63 6b 20 72 65 73 65  K %d unlock rese
c11f0 72 76 65 64 20 62 79 74 65 20 72 3d 25 64 5c 6e  rved byte r=%d\n
c1200 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 75  ", pFile->h, rcu
c1210 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 20   );.    }.    r 
c1220 3d 20 21 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52  = !(rc == NO_ERR
c1230 4f 52 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  OR);.    OSTRACE
c1240 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  3( "TEST WR-LOCK
c1250 20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29 5c   %d %d (remote)\
c1260 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20  n", pFile->h, r 
c1270 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 20 3d  );.  }.  *pOut =
c1280 20 72 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   r;.  return SQL
c1290 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
c12a0 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   Lower the locki
c12b0 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65  ng level on file
c12c0 20 64 65 73 63 72 69 70 74 6f 72 20 69 64 20 74   descriptor id t
c12d0 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63  o locktype.  loc
c12e0 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65  ktype.** must be
c12f0 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20   either NO_LOCK 
c1300 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a  or SHARED_LOCK..
c1310 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63  **.** If the loc
c1320 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68  king level of th
c1330 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
c1340 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20  r is already at 
c1350 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20  or below.** the 
c1360 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e  requested lockin
c1370 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f  g level, this ro
c1380 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
c1390 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f  ..**.** It is no
c13a0 74 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 74  t possible for t
c13b0 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 66  his routine to f
c13c0 61 69 6c 20 69 66 20 74 68 65 20 73 65 63 6f 6e  ail if the secon
c13d0 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73  d argument.** is
c13e0 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68   NO_LOCK.  If th
c13f0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
c1400 74 20 69 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b  t is SHARED_LOCK
c1410 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
c1420 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75  ne.** might retu
c1430 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
c1440 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
c1450 73 32 55 6e 6c 6f 63 6b 28 20 73 71 6c 69 74 65  s2Unlock( sqlite
c1460 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
c1470 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 69 6e  locktype ){.  in
c1480 74 20 74 79 70 65 3b 0a 20 20 6f 73 32 46 69 6c  t type;.  os2Fil
c1490 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46  e *pFile = (os2F
c14a0 69 6c 65 2a 29 69 64 3b 0a 20 20 41 50 49 52 45  ile*)id;.  APIRE
c14b0 54 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  T rc = SQLITE_OK
c14c0 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 20 3d  ;.  APIRET res =
c14d0 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49 4c   NO_ERROR;.  FIL
c14e0 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c  ELOCK  LockArea,
c14f0 0a 20 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c  .            Unl
c1500 6f 63 6b 41 72 65 61 3b 0a 20 20 6d 65 6d 73 65  ockArea;.  memse
c1510 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  t(&LockArea, 0, 
c1520 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29  sizeof(LockArea)
c1530 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c  );.  memset(&Unl
c1540 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65  ockArea, 0, size
c1550 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b  of(UnlockArea));
c1560 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
c1570 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
c1580 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45   locktype<=SHARE
c1590 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52  D_LOCK );.  OSTR
c15a0 41 43 45 34 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE4( "UNLOCK %d
c15b0 20 74 6f 20 25 64 20 77 61 73 20 25 64 5c 6e 22   to %d was %d\n"
c15c0 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
c15d0 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63  type, pFile->loc
c15e0 6b 74 79 70 65 20 29 3b 0a 20 20 74 79 70 65 20  ktype );.  type 
c15f0 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  = pFile->locktyp
c1600 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 45  e;.  if( type>=E
c1610 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b  XCLUSIVE_LOCK ){
c1620 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  .    LockArea.lO
c1630 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20  ffset = 0L;.    
c1640 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
c1650 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 0L;.    Unlock
c1660 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53  Area.lOffset = S
c1670 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 20  HARED_FIRST;.   
c1680 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
c1690 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45  ge = SHARED_SIZE
c16a0 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53  ;.    res = DosS
c16b0 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69  etFileLocks( pFi
c16c0 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72  le->h, &UnlockAr
c16d0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c  ea, &LockArea, L
c16e0 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20  OCK_TIMEOUT, 0L 
c16f0 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  );.    OSTRACE3(
c1700 20 22 55 4e 4c 4f 43 4b 20 25 64 20 65 78 63 6c   "UNLOCK %d excl
c1710 75 73 69 76 65 20 6c 6f 63 6b 20 72 65 73 3d 25  usive lock res=%
c1720 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
c1730 72 65 73 20 29 3b 0a 20 20 20 20 69 66 28 20 6c  res );.    if( l
c1740 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
c1750 4c 4f 43 4b 20 26 26 20 67 65 74 52 65 61 64 4c  LOCK && getReadL
c1760 6f 63 6b 28 70 46 69 6c 65 29 20 21 3d 20 4e 4f  ock(pFile) != NO
c1770 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20  _ERROR ){.      
c1780 2f 2a 20 54 68 69 73 20 73 68 6f 75 6c 64 20 6e  /* This should n
c1790 65 76 65 72 20 68 61 70 70 65 6e 2e 20 20 57 65  ever happen.  We
c17a0 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62   should always b
c17b0 65 20 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20  e able to.      
c17c0 2a 2a 20 72 65 61 63 71 75 69 72 65 20 74 68 65  ** reacquire the
c17d0 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20   read lock */.  
c17e0 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 55      OSTRACE3( "U
c17f0 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 67  NLOCK %d to %d g
c1800 65 74 52 65 61 64 4c 6f 63 6b 28 29 20 66 61 69  etReadLock() fai
c1810 6c 65 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  led\n", pFile->h
c1820 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  , locktype );.  
c1830 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
c1840 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20  IOERR_UNLOCK;.  
c1850 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 74 79    }.  }.  if( ty
c1860 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe>=RESERVED_LOC
c1870 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  K ){.    LockAre
c1880 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a  a.lOffset = 0L;.
c1890 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
c18a0 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e  nge = 0L;.    Un
c18b0 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
c18c0 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45   = RESERVED_BYTE
c18d0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
c18e0 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20  .lRange = 1L;.  
c18f0 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69    res = DosSetFi
c1900 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e  leLocks( pFile->
c1910 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  h, &UnlockArea, 
c1920 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f  &LockArea, LOCK_
c1930 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20  TIMEOUT, 0L );. 
c1940 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e     OSTRACE3( "UN
c1950 4c 4f 43 4b 20 25 64 20 72 65 73 65 72 76 65 64  LOCK %d reserved
c1960 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   res=%d\n", pFil
c1970 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d  e->h, res );.  }
c1980 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
c1990 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65  =NO_LOCK && type
c19a0 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  >=SHARED_LOCK ){
c19b0 0a 20 20 20 20 72 65 73 20 3d 20 75 6e 6c 6f 63  .    res = unloc
c19c0 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  kReadLock(pFile)
c19d0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 35 28 20  ;.    OSTRACE5( 
c19e0 22 55 4e 4c 4f 43 4b 20 25 64 20 69 73 20 25 64  "UNLOCK %d is %d
c19f0 20 77 61 6e 74 20 25 64 20 72 65 73 3d 25 64 5c   want %d res=%d\
c1a00 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 74 79  n", pFile->h, ty
c1a10 70 65 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 72 65  pe, locktype, re
c1a20 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74  s );.  }.  if( t
c1a30 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype>=PENDING_LOC
c1a40 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  K ){.    LockAre
c1a50 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a  a.lOffset = 0L;.
c1a60 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
c1a70 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e  nge = 0L;.    Un
c1a80 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
c1a90 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b   = PENDING_BYTE;
c1aa0 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  .    UnlockArea.
c1ab0 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20  lRange = 1L;.   
c1ac0 20 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c   res = DosSetFil
c1ad0 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68  eLocks( pFile->h
c1ae0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
c1af0 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54  LockArea, LOCK_T
c1b00 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20  IMEOUT, 0L );.  
c1b10 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c    OSTRACE3( "UNL
c1b20 4f 43 4b 20 25 64 20 70 65 6e 64 69 6e 67 20 72  OCK %d pending r
c1b30 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  es=%d\n", pFile-
c1b40 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20  >h, res );.  }. 
c1b50 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c1b60 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 4f   = locktype;.  O
c1b70 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b  STRACE3( "UNLOCK
c1b80 20 25 64 20 6e 6f 77 20 25 64 5c 6e 22 2c 20 70   %d now %d\n", p
c1b90 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e  File->h, pFile->
c1ba0 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 72 65  locktype );.  re
c1bb0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
c1bc0 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e 64 20 71 75  * Control and qu
c1bd0 65 72 79 20 6f 66 20 74 68 65 20 6f 70 65 6e 20  ery of the open 
c1be0 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  file handle..*/.
c1bf0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46 69  static int os2Fi
c1c00 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  leControl(sqlite
c1c10 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
c1c20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  op, void *pArg){
c1c30 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
c1c40 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
c1c50 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45  _FCNTL_LOCKSTATE
c1c60 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a  : {.      *(int*
c1c70 29 70 41 72 67 20 3d 20 28 28 6f 73 32 46 69 6c  )pArg = ((os2Fil
c1c80 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65  e*)id)->locktype
c1c90 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33  ;.      OSTRACE3
c1ca0 28 20 22 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41  ( "FCNTL_LOCKSTA
c1cb0 54 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22  TE %d lock=%d\n"
c1cc0 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29  , ((os2File*)id)
c1cd0 2d 3e 68 2c 20 28 28 6f 73 32 46 69 6c 65 2a 29  ->h, ((os2File*)
c1ce0 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b  id)->locktype );
c1cf0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
c1d00 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
c1d10 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
c1d20 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a  TE_ERROR;.}../*.
c1d30 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 65  ** Return the se
c1d40 63 74 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74  ctor size in byt
c1d50 65 73 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c  es of the underl
c1d60 79 69 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63  ying block devic
c1d70 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65  e for.** the spe
c1d80 63 69 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69  cified file. Thi
c1d90 73 20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61  s is almost alwa
c1da0 79 73 20 35 31 32 20 62 79 74 65 73 2c 20 62 75  ys 512 bytes, bu
c1db0 74 20 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67  t may be.** larg
c1dc0 65 72 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69  er for some devi
c1dd0 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ces..**.** SQLit
c1de0 65 20 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74  e code assumes t
c1df0 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e  his function can
c1e00 6e 6f 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73  not fail. It als
c1e10 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a  o assumes that.*
c1e20 2a 20 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61  * if two files a
c1e30 72 65 20 63 72 65 61 74 65 64 20 69 6e 20 74 68  re created in th
c1e40 65 20 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74  e same file-syst
c1e50 65 6d 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e  em directory (i.
c1e60 65 2e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65  e..** a database
c1e70 20 61 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c   and its journal
c1e80 20 66 69 6c 65 29 20 74 68 61 74 20 74 68 65 20   file) that the 
c1e90 73 65 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c  sector size will
c1ea0 20 62 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   be the.** same 
c1eb0 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61  for both..*/.sta
c1ec0 74 69 63 20 69 6e 74 20 6f 73 32 53 65 63 74 6f  tic int os2Secto
c1ed0 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  rSize(sqlite3_fi
c1ee0 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72  le *id){.  retur
c1ef0 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  n SQLITE_DEFAULT
c1f00 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d 0a  _SECTOR_SIZE;.}.
c1f10 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
c1f20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63 65  vector of device
c1f30 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73   characteristics
c1f40 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c1f50 6f 73 32 44 65 76 69 63 65 43 68 61 72 61 63 74  os2DeviceCharact
c1f60 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65 33  eristics(sqlite3
c1f70 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72 65  _file *id){.  re
c1f80 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn 0;.}.../*.*
c1f90 2a 20 43 68 61 72 61 63 74 65 72 20 73 65 74 20  * Character set 
c1fa0 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63  conversion objec
c1fb0 74 73 20 75 73 65 64 20 62 79 20 63 6f 6e 76 65  ts used by conve
c1fc0 72 73 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a  rsion routines..
c1fd0 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 4f  */.static UconvO
c1fe0 62 6a 65 63 74 20 75 63 55 74 66 38 20 3d 20 4e  bject ucUtf8 = N
c1ff0 55 4c 4c 3b 20 2f 2a 20 63 6f 6e 76 65 72 74 20  ULL; /* convert 
c2000 62 65 74 77 65 65 6e 20 55 54 46 2d 38 20 61 6e  between UTF-8 an
c2010 64 20 55 43 53 2d 32 20 2a 2f 0a 73 74 61 74 69  d UCS-2 */.stati
c2020 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63  c UconvObject uc
c2030 6c 43 70 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a 20  lCp = NULL;  /* 
c2040 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20  convert between 
c2050 6c 6f 63 61 6c 20 63 6f 64 65 70 61 67 65 20 61  local codepage a
c2060 6e 64 20 55 43 53 2d 32 20 2a 2f 0a 0a 2f 2a 0a  nd UCS-2 */../*.
c2070 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69  ** Helper functi
c2080 6f 6e 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  on to initialize
c2090 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
c20a0 6f 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e 64  objects from and
c20b0 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74   to UTF-8..*/.st
c20c0 61 74 69 63 20 76 6f 69 64 20 69 6e 69 74 55 63  atic void initUc
c20d0 6f 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 64  onvObjects( void
c20e0 20 29 7b 0a 20 20 69 66 28 20 55 6e 69 43 72 65   ){.  if( UniCre
c20f0 61 74 65 55 63 6f 6e 76 4f 62 6a 65 63 74 28 20  ateUconvObject( 
c2100 55 54 46 5f 38 2c 20 26 75 63 55 74 66 38 20 29  UTF_8, &ucUtf8 )
c2110 20 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20   != ULS_SUCCESS 
c2120 29 0a 20 20 20 20 75 63 55 74 66 38 20 3d 20 4e  ).    ucUtf8 = N
c2130 55 4c 4c 3b 0a 20 20 69 66 20 28 20 55 6e 69 43  ULL;.  if ( UniC
c2140 72 65 61 74 65 55 63 6f 6e 76 4f 62 6a 65 63 74  reateUconvObject
c2150 28 20 28 55 6e 69 43 68 61 72 20 2a 29 4c 22 40  ( (UniChar *)L"@
c2160 70 61 74 68 3d 79 65 73 22 2c 20 26 75 63 6c 43  path=yes", &uclC
c2170 70 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 45  p ) != ULS_SUCCE
c2180 53 53 20 29 0a 20 20 20 20 75 63 6c 43 70 20 3d  SS ).    uclCp =
c2190 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   NULL;.}../*.** 
c21a0 48 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20  Helper function 
c21b0 74 6f 20 66 72 65 65 20 74 68 65 20 63 6f 6e 76  to free the conv
c21c0 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66  ersion objects f
c21d0 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d 38  rom and to UTF-8
c21e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
c21f0 20 66 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63 74   freeUconvObject
c2200 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66 20  s( void ){.  if 
c2210 28 20 75 63 55 74 66 38 20 29 0a 20 20 20 20 55  ( ucUtf8 ).    U
c2220 6e 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65 63  niFreeUconvObjec
c2230 74 28 20 75 63 55 74 66 38 20 29 3b 0a 20 20 69  t( ucUtf8 );.  i
c2240 66 20 28 20 75 63 6c 43 70 20 29 0a 20 20 20 20  f ( uclCp ).    
c2250 55 6e 69 46 72 65 65 55 63 6f 6e 76 4f 62 6a 65  UniFreeUconvObje
c2260 63 74 28 20 75 63 6c 43 70 20 29 3b 0a 20 20 75  ct( uclCp );.  u
c2270 63 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20  cUtf8 = NULL;.  
c2280 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a  uclCp = NULL;.}.
c2290 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75  ./*.** Helper fu
c22a0 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72  nction to conver
c22b0 74 20 55 54 46 2d 38 20 66 69 6c 65 6e 61 6d 65  t UTF-8 filename
c22c0 73 20 74 6f 20 6c 6f 63 61 6c 20 4f 53 2f 32 20  s to local OS/2 
c22d0 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 68 65  codepage..** The
c22e0 20 74 77 6f 2d 73 74 65 70 20 70 72 6f 63 65 73   two-step proces
c22f0 73 3a 20 66 69 72 73 74 20 63 6f 6e 76 65 72 74  s: first convert
c2300 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20 55 54   the incoming UT
c2310 46 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e  F-8 string.** in
c2320 74 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68 65  to UCS-2 and the
c2330 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f 20  n from UCS-2 to 
c2340 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 64 65  the current code
c2350 70 61 67 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74  page..** The ret
c2360 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 6e 74  urned char point
c2370 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 72 65  er has to be fre
c2380 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  ed..*/.static ch
c2390 61 72 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 50  ar *convertUtf8P
c23a0 61 74 68 54 6f 43 70 28 20 63 6f 6e 73 74 20 63  athToCp( const c
c23b0 68 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69  har *in ){.  Uni
c23c0 43 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 43  Char tempPath[CC
c23d0 48 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61  HMAXPATH];.  cha
c23e0 72 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a  r *out = (char *
c23f0 29 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50  )calloc( CCHMAXP
c2400 41 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28  ATH, 1 );..  if(
c2410 20 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 75   !out ).    retu
c2420 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20  rn NULL;..  if( 
c2430 21 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c 43  !ucUtf8 || !uclC
c2440 70 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e  p ).    initUcon
c2450 76 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f  vObjects();..  /
c2460 2a 20 64 65 74 65 72 6d 69 6e 65 20 73 74 72 69  * determine stri
c2470 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6e 76 65  ng for the conve
c2480 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d 38 20 77  rsion of UTF-8 w
c2490 68 69 63 68 20 69 73 20 43 50 31 32 30 38 20 2a  hich is CP1208 *
c24a0 2f 0a 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f  /.  if( UniStrTo
c24b0 55 63 73 28 20 75 63 55 74 66 38 2c 20 74 65 6d  Ucs( ucUtf8, tem
c24c0 70 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 69  pPath, (char *)i
c24d0 6e 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29 20  n, CCHMAXPATH ) 
c24e0 21 3d 20 55 4c 53 5f 53 55 43 43 45 53 53 20 29  != ULS_SUCCESS )
c24f0 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 75 74 3b  .    return out;
c2500 20 2f 2a 20 69 66 20 63 6f 6e 76 65 72 73 69 6f   /* if conversio
c2510 6e 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20  n fails, return 
c2520 74 68 65 20 65 6d 70 74 79 20 73 74 72 69 6e 67  the empty string
c2530 20 2a 2f 0a 0a 20 20 2f 2a 20 63 6f 6e 76 65 72   */..  /* conver
c2540 73 69 6f 6e 20 66 6f 72 20 63 75 72 72 65 6e 74  sion for current
c2550 20 63 6f 64 65 70 61 67 65 20 77 68 69 63 68 20   codepage which 
c2560 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20  can be used for 
c2570 70 61 74 68 73 20 2a 2f 0a 20 20 55 6e 69 53 74  paths */.  UniSt
c2580 72 46 72 6f 6d 55 63 73 28 20 75 63 6c 43 70 2c  rFromUcs( uclCp,
c2590 20 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20   out, tempPath, 
c25a0 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20  CCHMAXPATH );.. 
c25b0 20 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a   return out;.}..
c25c0 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e  /*.** Helper fun
c25d0 63 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74  ction to convert
c25e0 20 66 69 6c 65 6e 61 6d 65 73 20 66 72 6f 6d 20   filenames from 
c25f0 6c 6f 63 61 6c 20 63 6f 64 65 70 61 67 65 20 74  local codepage t
c2600 6f 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20  o UTF-8..** The 
c2610 74 77 6f 2d 73 74 65 70 20 70 72 6f 63 65 73 73  two-step process
c2620 3a 20 66 69 72 73 74 20 63 6f 6e 76 65 72 74 20  : first convert 
c2630 74 68 65 20 69 6e 63 6f 6d 69 6e 67 20 63 6f 64  the incoming cod
c2640 65 70 61 67 65 2d 73 70 65 63 69 66 69 63 0a 2a  epage-specific.*
c2650 2a 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 55 43  * string into UC
c2660 53 2d 32 20 61 6e 64 20 74 68 65 6e 20 66 72 6f  S-2 and then fro
c2670 6d 20 55 43 53 2d 32 20 74 6f 20 74 68 65 20 63  m UCS-2 to the c
c2680 6f 64 65 70 61 67 65 20 6f 66 20 55 54 46 2d 38  odepage of UTF-8
c2690 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  ..** The returne
c26a0 64 20 63 68 61 72 20 70 6f 69 6e 74 65 72 20 68  d char pointer h
c26b0 61 73 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a  as to be freed..
c26c0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
c26d0 69 6f 6e 20 69 73 20 6e 6f 6e 2d 73 74 61 74 69  ion is non-stati
c26e0 63 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20  c to be able to 
c26f0 75 73 65 20 74 68 69 73 20 69 6e 20 73 68 65 6c  use this in shel
c2700 6c 2e 63 20 61 6e 64 0a 2a 2a 20 73 69 6d 69 6c  l.c and.** simil
c2710 61 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ar applications 
c2720 74 68 61 74 20 74 61 6b 65 20 63 6f 6d 6d 61 6e  that take comman
c2730 64 20 6c 69 6e 65 20 61 72 67 75 6d 65 6e 74 73  d line arguments
c2740 2e 0a 2a 2f 0a 63 68 61 72 20 2a 63 6f 6e 76 65  ..*/.char *conve
c2750 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 20  rtCpPathToUtf8( 
c2760 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20 29  const char *in )
c2770 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d 70  {.  UniChar temp
c2780 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48 5d  Path[CCHMAXPATH]
c2790 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d 20  ;.  char *out = 
c27a0 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20  (char *)calloc( 
c27b0 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29 3b  CCHMAXPATH, 1 );
c27c0 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a 20  ..  if( !out ). 
c27d0 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a     return NULL;.
c27e0 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 20 7c  .  if( !ucUtf8 |
c27f0 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20 69  | !uclCp ).    i
c2800 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28  nitUconvObjects(
c2810 29 3b 0a 0a 20 20 2f 2a 20 63 6f 6e 76 65 72 73  );..  /* convers
c2820 69 6f 6e 20 66 6f 72 20 63 75 72 72 65 6e 74 20  ion for current 
c2830 63 6f 64 65 70 61 67 65 20 77 68 69 63 68 20 63  codepage which c
c2840 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 70  an be used for p
c2850 61 74 68 73 20 2a 2f 0a 20 20 69 66 28 20 55 6e  aths */.  if( Un
c2860 69 53 74 72 54 6f 55 63 73 28 20 75 63 6c 43 70  iStrToUcs( uclCp
c2870 2c 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68 61  , tempPath, (cha
c2880 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50 41  r *)in, CCHMAXPA
c2890 54 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43  TH ) != ULS_SUCC
c28a0 45 53 53 20 29 0a 20 20 20 20 72 65 74 75 72 6e  ESS ).    return
c28b0 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e 76   out; /* if conv
c28c0 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72 65  ersion fails, re
c28d0 74 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20 73  turn the empty s
c28e0 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 64  tring */..  /* d
c28f0 65 74 65 72 6d 69 6e 65 20 73 74 72 69 6e 67 20  etermine string 
c2900 66 6f 72 20 74 68 65 20 63 6f 6e 76 65 72 73 69  for the conversi
c2910 6f 6e 20 6f 66 20 55 54 46 2d 38 20 77 68 69 63  on of UTF-8 whic
c2920 68 20 69 73 20 43 50 31 32 30 38 20 2a 2f 0a 20  h is CP1208 */. 
c2930 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 20   UniStrFromUcs( 
c2940 75 63 55 74 66 38 2c 20 6f 75 74 2c 20 74 65 6d  ucUtf8, out, tem
c2950 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41 54  pPath, CCHMAXPAT
c2960 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f  H );..  return o
c2970 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ut;.}../*.** Thi
c2980 73 20 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73  s vector defines
c2990 20 61 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73   all the methods
c29a0 20 74 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74   that can operat
c29b0 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74  e on an.** sqlit
c29c0 65 33 5f 66 69 6c 65 20 66 6f 72 20 6f 73 32 2e  e3_file for os2.
c29d0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
c29e0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
c29f0 6f 64 73 20 6f 73 32 49 6f 4d 65 74 68 6f 64 20  ods os2IoMethod 
c2a00 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20  = {.  1,        
c2a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2a20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
c2a30 20 6f 73 32 43 6c 6f 73 65 2c 0a 20 20 6f 73 32   os2Close,.  os2
c2a40 52 65 61 64 2c 0a 20 20 6f 73 32 57 72 69 74 65  Read,.  os2Write
c2a50 2c 0a 20 20 6f 73 32 54 72 75 6e 63 61 74 65 2c  ,.  os2Truncate,
c2a60 0a 20 20 6f 73 32 53 79 6e 63 2c 0a 20 20 6f 73  .  os2Sync,.  os
c2a70 32 46 69 6c 65 53 69 7a 65 2c 0a 20 20 6f 73 32  2FileSize,.  os2
c2a80 4c 6f 63 6b 2c 0a 20 20 6f 73 32 55 6e 6c 6f 63  Lock,.  os2Unloc
c2a90 6b 2c 0a 20 20 6f 73 32 43 68 65 63 6b 52 65 73  k,.  os2CheckRes
c2aa0 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 6f 73 32  ervedLock,.  os2
c2ab0 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 6f  FileControl,.  o
c2ac0 73 32 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20  s2SectorSize,.  
c2ad0 6f 73 32 44 65 76 69 63 65 43 68 61 72 61 63 74  os2DeviceCharact
c2ae0 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a  eristics.};../**
c2af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72  *********.** Her
c2b40 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d  e ends the I/O m
c2b50 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d  ethods that form
c2b60 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f   the sqlite3_io_
c2b70 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a  methods object..
c2b80 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62  **.** The next b
c2b90 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70  lock of code imp
c2ba0 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20  lements the VFS 
c2bb0 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a  methods..*******
c2bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2c00 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 72  *****/../*.** Cr
c2c10 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79  eate a temporary
c2c20 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42   file name in zB
c2c30 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62  uf.  zBuf must b
c2c40 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 0a  e big enough to.
c2c50 2a 2a 20 68 6f 6c 64 20 61 74 20 70 56 66 73 2d  ** hold at pVfs-
c2c60 3e 6d 78 50 61 74 68 6e 61 6d 65 20 63 68 61 72  >mxPathname char
c2c70 61 63 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69  acters..*/.stati
c2c80 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61 6d  c int getTempnam
c2c90 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  e(int nBuf, char
c2ca0 20 2a 7a 42 75 66 20 29 7b 0a 20 20 73 74 61 74   *zBuf ){.  stat
c2cb0 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
c2cc0 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d 20  d char zChars[] 
c2cd0 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68 69  =.    "abcdefghi
c2ce0 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79  jklmnopqrstuvwxy
c2cf0 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47 48  z".    "ABCDEFGH
c2d00 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58  IJKLMNOPQRSTUVWX
c2d10 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35 36  YZ".    "0123456
c2d20 37 38 39 22 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  789";.  int i, j
c2d30 3b 0a 20 20 63 68 61 72 20 7a 54 65 6d 70 50 61  ;.  char zTempPa
c2d40 74 68 42 75 66 5b 33 5d 3b 0a 20 20 50 53 5a 20  thBuf[3];.  PSZ 
c2d50 7a 54 65 6d 70 50 61 74 68 20 3d 20 28 50 53 5a  zTempPath = (PSZ
c2d60 29 26 7a 54 65 6d 70 50 61 74 68 42 75 66 3b 0a  )&zTempPathBuf;.
c2d70 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65    if( sqlite3_te
c2d80 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a  mp_directory ){.
c2d90 20 20 20 20 7a 54 65 6d 70 50 61 74 68 20 3d 20      zTempPath = 
c2da0 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
c2db0 65 63 74 6f 72 79 3b 0a 20 20 7d 65 6c 73 65 7b  ectory;.  }else{
c2dc0 0a 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e  .    if( DosScan
c2dd0 45 6e 76 28 20 28 50 53 5a 29 22 54 45 4d 50 22  Env( (PSZ)"TEMP"
c2de0 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20 29  , &zTempPath ) )
c2df0 7b 0a 20 20 20 20 20 20 69 66 28 20 44 6f 73 53  {.      if( DosS
c2e00 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 22 54 4d  canEnv( (PSZ)"TM
c2e10 50 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29  P", &zTempPath )
c2e20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
c2e30 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a  DosScanEnv( (PSZ
c2e40 29 22 54 4d 50 44 49 52 22 2c 20 26 7a 54 65 6d  )"TMPDIR", &zTem
c2e50 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20  pPath ) ){.     
c2e60 20 20 20 20 20 20 55 4c 4f 4e 47 20 75 6c 44 72        ULONG ulDr
c2e70 69 76 65 4e 75 6d 20 3d 20 30 2c 20 75 6c 44 72  iveNum = 0, ulDr
c2e80 69 76 65 4d 61 70 20 3d 20 30 3b 0a 20 20 20 20  iveMap = 0;.    
c2e90 20 20 20 20 20 20 20 44 6f 73 51 75 65 72 79 43         DosQueryC
c2ea0 75 72 72 65 6e 74 44 69 73 6b 28 20 26 75 6c 44  urrentDisk( &ulD
c2eb0 72 69 76 65 4e 75 6d 2c 20 26 75 6c 44 72 69 76  riveNum, &ulDriv
c2ec0 65 4d 61 70 20 29 3b 0a 20 20 20 20 20 20 20 20  eMap );.        
c2ed0 20 20 20 73 70 72 69 6e 74 66 28 20 28 63 68 61     sprintf( (cha
c2ee0 72 2a 29 7a 54 65 6d 70 50 61 74 68 2c 20 22 25  r*)zTempPath, "%
c2ef0 63 3a 22 2c 20 28 63 68 61 72 29 28 20 27 41 27  c:", (char)( 'A'
c2f00 20 2b 20 75 6c 44 72 69 76 65 4e 75 6d 20 2d 20   + ulDriveNum - 
c2f10 31 20 29 20 29 3b 0a 20 20 20 20 20 20 20 20 7d  1 ) );.        }
c2f20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
c2f30 20 7d 0a 20 20 2f 2a 20 53 74 72 69 70 20 6f 66   }.  /* Strip of
c2f40 66 20 61 20 74 72 61 69 6c 69 6e 67 20 73 6c 61  f a trailing sla
c2f50 73 68 65 73 20 6f 72 20 62 61 63 6b 73 6c 61 73  shes or backslas
c2f60 68 65 73 2c 20 6f 74 68 65 72 77 69 73 65 20 77  hes, otherwise w
c2f70 65 20 77 6f 75 6c 64 20 67 65 74 20 2a 0a 20 20  e would get *.  
c2f80 20 2a 20 6d 75 6c 74 69 70 6c 65 20 28 62 61 63   * multiple (bac
c2f90 6b 29 73 6c 61 73 68 65 73 20 77 68 69 63 68 20  k)slashes which 
c2fa0 63 61 75 73 65 73 20 44 6f 73 4f 70 65 6e 28 29  causes DosOpen()
c2fb0 20 74 6f 20 66 61 69 6c 2e 20 20 20 20 20 20 20   to fail.       
c2fc0 20 20 20 20 20 20 20 2a 0a 20 20 20 2a 20 54 72         *.   * Tr
c2fd0 61 69 6c 69 6e 67 20 73 70 61 63 65 73 20 61 72  ailing spaces ar
c2fe0 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 2c 20 65  e not allowed, e
c2ff0 69 74 68 65 72 2e 20 20 20 20 20 20 20 20 20 20  ither.          
c3000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3010 20 20 2a 2f 0a 20 20 6a 20 3d 20 73 71 6c 69 74    */.  j = sqlit
c3020 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70  e3Strlen30(zTemp
c3030 50 61 74 68 29 3b 0a 20 20 77 68 69 6c 65 28 20  Path);.  while( 
c3040 6a 20 3e 20 30 20 26 26 20 28 20 7a 54 65 6d 70  j > 0 && ( zTemp
c3050 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 5c 5c  Path[j-1] == '\\
c3060 27 20 7c 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a  ' || zTempPath[j
c3070 2d 31 5d 20 3d 3d 20 27 2f 27 0a 20 20 20 20 20  -1] == '/'.     
c3080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
c3090 7c 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d  | zTempPath[j-1]
c30a0 20 3d 3d 20 27 20 27 20 29 20 29 7b 0a 20 20 20   == ' ' ) ){.   
c30b0 20 6a 2d 2d 3b 0a 20 20 7d 0a 20 20 7a 54 65 6d   j--;.  }.  zTem
c30c0 70 50 61 74 68 5b 6a 5d 20 3d 20 27 5c 30 27 3b  pPath[j] = '\0';
c30d0 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f  .  if( !sqlite3_
c30e0 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29  temp_directory )
c30f0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d  {.    char *zTem
c3100 70 50 61 74 68 55 54 46 20 3d 20 63 6f 6e 76 65  pPathUTF = conve
c3110 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 20  rtCpPathToUtf8( 
c3120 7a 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 20  zTempPath );.   
c3130 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
c3140 66 28 20 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66  f( nBuf-30, zBuf
c3150 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
c3160 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 53 51          "%s\\"SQ
c3170 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50  LITE_TEMP_FILE_P
c3180 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 68  REFIX, zTempPath
c3190 55 54 46 20 29 3b 0a 20 20 20 20 66 72 65 65 28  UTF );.    free(
c31a0 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b   zTempPathUTF );
c31b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
c31c0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20  lite3_snprintf( 
c31d0 6e 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20  nBuf-30, zBuf,. 
c31e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c31f0 20 20 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54       "%s\\"SQLIT
c3200 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
c3210 49 58 2c 20 7a 54 65 6d 70 50 61 74 68 20 29 3b  IX, zTempPath );
c3220 0a 20 20 7d 0a 20 20 6a 20 3d 20 73 71 6c 69 74  .  }.  j = sqlit
c3230 65 33 53 74 72 6c 65 6e 33 30 28 20 7a 42 75 66  e3Strlen30( zBuf
c3240 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61   );.  sqlite3_ra
c3250 6e 64 6f 6d 6e 65 73 73 28 20 32 30 2c 20 26 7a  ndomness( 20, &z
c3260 42 75 66 5b 6a 5d 20 29 3b 0a 20 20 66 6f 72 28  Buf[j] );.  for(
c3270 20 69 20 3d 20 30 3b 20 69 20 3c 20 32 30 3b 20   i = 0; i < 20; 
c3280 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 0a 20 20 20 20  i++, j++ ){.    
c3290 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29  zBuf[j] = (char)
c32a0 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e  zChars[ ((unsign
c32b0 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29  ed char)zBuf[j])
c32c0 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29  %(sizeof(zChars)
c32d0 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75  -1) ];.  }.  zBu
c32e0 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 52  f[j] = 0;.  OSTR
c32f0 41 43 45 32 28 20 22 54 45 4d 50 20 46 49 4c 45  ACE2( "TEMP FILE
c3300 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a 42 75  NAME: %s\n", zBu
c3310 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  f );.  return SQ
c3320 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
c3330 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69  ** Turn a relati
c3340 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f  ve pathname into
c3350 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65   a full pathname
c3360 2e 20 20 57 72 69 74 65 20 74 68 65 20 66 75 6c  .  Write the ful
c3370 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20 69 6e  l.** pathname in
c3380 74 6f 20 7a 46 75 6c 6c 5b 5d 2e 20 20 7a 46 75  to zFull[].  zFu
c3390 6c 6c 5b 5d 20 77 69 6c 6c 20 62 65 20 61 74 20  ll[] will be at 
c33a0 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d 78 50 61  least pVfs->mxPa
c33b0 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65 73 20  thname.** bytes 
c33c0 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74  in size..*/.stat
c33d0 69 63 20 69 6e 74 20 6f 73 32 46 75 6c 6c 50 61  ic int os2FullPa
c33e0 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65  thname(.  sqlite
c33f0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
c3400 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
c3410 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a   to vfs object *
c3420 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
c3430 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20 20 20  zRelative,      
c3440 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c 61  /* Possibly rela
c3450 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68 20  tive input path 
c3460 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20  */.  int nFull, 
c3470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3480 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70   /* Size of outp
c3490 75 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74  ut buffer in byt
c34a0 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46  es */.  char *zF
c34b0 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  ull             
c34c0 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75      /* Output bu
c34d0 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 63 68 61  ffer */.){.  cha
c34e0 72 20 2a 7a 52 65 6c 61 74 69 76 65 43 70 20 3d  r *zRelativeCp =
c34f0 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68   convertUtf8Path
c3500 54 6f 43 70 28 20 7a 52 65 6c 61 74 69 76 65 20  ToCp( zRelative 
c3510 29 3b 0a 20 20 63 68 61 72 20 7a 46 75 6c 6c 43  );.  char zFullC
c3520 70 5b 43 43 48 4d 41 58 50 41 54 48 5d 20 3d 20  p[CCHMAXPATH] = 
c3530 22 5c 30 22 3b 0a 20 20 63 68 61 72 20 2a 7a 46  "\0";.  char *zF
c3540 75 6c 6c 55 54 46 3b 0a 20 20 41 50 49 52 45 54  ullUTF;.  APIRET
c3550 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 61   rc = DosQueryPa
c3560 74 68 49 6e 66 6f 28 20 7a 52 65 6c 61 74 69 76  thInfo( zRelativ
c3570 65 43 70 2c 20 46 49 4c 5f 51 55 45 52 59 46 55  eCp, FIL_QUERYFU
c3580 4c 4c 4e 41 4d 45 2c 20 7a 46 75 6c 6c 43 70 2c  LLNAME, zFullCp,
c3590 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c35a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c35b0 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 20   CCHMAXPATH );. 
c35c0 20 66 72 65 65 28 20 7a 52 65 6c 61 74 69 76 65   free( zRelative
c35d0 43 70 20 29 3b 0a 20 20 7a 46 75 6c 6c 55 54 46  Cp );.  zFullUTF
c35e0 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74 68   = convertCpPath
c35f0 54 6f 55 74 66 38 28 20 7a 46 75 6c 6c 43 70 20  ToUtf8( zFullCp 
c3600 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  );.  sqlite3_snp
c3610 72 69 6e 74 66 28 20 6e 46 75 6c 6c 2c 20 7a 46  rintf( nFull, zF
c3620 75 6c 6c 2c 20 7a 46 75 6c 6c 55 54 46 20 29 3b  ull, zFullUTF );
c3630 0a 20 20 66 72 65 65 28 20 7a 46 75 6c 6c 55 54  .  free( zFullUT
c3640 46 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  F );.  return rc
c3650 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53   == NO_ERROR ? S
c3660 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54  QLITE_OK : SQLIT
c3670 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 0a 2f 2a 0a  E_IOERR;.}.../*.
c3680 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a  ** Open a file..
c3690 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c36a0 32 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  2Open(.  sqlite3
c36b0 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
c36c0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73         /* Not us
c36d0 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
c36e0 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
c36f0 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
c3700 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73   the file */.  s
c3710 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
c3720 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c3730 57 72 69 74 65 20 74 68 65 20 53 51 4c 69 74 65  Write the SQLite
c3740 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68 65 72   file handle her
c3750 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  e */.  int flags
c3760 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
c3770 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64       /* Open mod
c3780 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74  e flags */.  int
c3790 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20   *pOutFlags     
c37a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
c37b0 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61 67  atus return flag
c37c0 73 20 2a 2f 0a 29 7b 0a 20 20 48 46 49 4c 45 20  s */.){.  HFILE 
c37d0 68 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 46 69 6c  h;.  ULONG ulFil
c37e0 65 41 74 74 72 69 62 75 74 65 20 3d 20 46 49 4c  eAttribute = FIL
c37f0 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 55 4c 4f 4e  E_NORMAL;.  ULON
c3800 47 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 3d 20  G ulOpenFlags = 
c3810 30 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65  0;.  ULONG ulOpe
c3820 6e 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 6f 73 32  nMode = 0;.  os2
c3830 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f  File *pFile = (o
c3840 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 41 50  s2File*)id;.  AP
c3850 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52  IRET rc = NO_ERR
c3860 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 41 63  OR;.  ULONG ulAc
c3870 74 69 6f 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 4e  tion;.  char *zN
c3880 61 6d 65 43 70 3b 0a 20 20 63 68 61 72 20 7a 54  ameCp;.  char zT
c3890 6d 70 6e 61 6d 65 5b 43 43 48 4d 41 58 50 41 54  mpname[CCHMAXPAT
c38a0 48 2b 31 5d 3b 20 20 20 20 2f 2a 20 42 75 66 66  H+1];    /* Buff
c38b0 65 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 20  er to hold name 
c38c0 6f 66 20 74 65 6d 70 20 66 69 6c 65 20 2a 2f 0a  of temp file */.
c38d0 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 63  .  /* If the sec
c38e0 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
c38f0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
c3900 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20   NULL, generate 
c3910 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72  a .  ** temporar
c3920 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75  y file name to u
c3930 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  se .  */.  if( !
c3940 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 69 6e 74  zName ){.    int
c3950 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d   rc = getTempnam
c3960 65 28 43 43 48 4d 41 58 50 41 54 48 2b 31 2c 20  e(CCHMAXPATH+1, 
c3970 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69  zTmpname);.    i
c3980 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
c3990 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
c39a0 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a   rc;.    }.    z
c39b0 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b  Name = zTmpname;
c39c0 0a 20 20 7d 0a 0a 0a 20 20 6d 65 6d 73 65 74 28  .  }...  memset(
c39d0 20 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f   pFile, 0, sizeo
c39e0 66 28 2a 70 46 69 6c 65 29 20 29 3b 0a 0a 20 20  f(*pFile) );..  
c39f0 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e 20  OSTRACE2( "OPEN 
c3a00 77 61 6e 74 20 25 64 5c 6e 22 2c 20 66 6c 61 67  want %d\n", flag
c3a10 73 20 29 3b 0a 0a 20 20 69 66 28 20 66 6c 61 67  s );..  if( flag
c3a20 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
c3a30 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20  READWRITE ){.   
c3a40 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f   ulOpenMode |= O
c3a50 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 57  PEN_ACCESS_READW
c3a60 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41 43  RITE;.    OSTRAC
c3a70 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64 2f 77  E1( "OPEN read/w
c3a80 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c  rite\n" );.  }el
c3a90 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f  se{.    ulOpenMo
c3aa0 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45 53  de |= OPEN_ACCES
c3ab0 53 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20  S_READONLY;.    
c3ac0 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20  OSTRACE1( "OPEN 
c3ad0 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b 0a  read only\n" );.
c3ae0 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73    }..  if( flags
c3af0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43   & SQLITE_OPEN_C
c3b00 52 45 41 54 45 20 29 7b 0a 20 20 20 20 75 6c 4f  REATE ){.    ulO
c3b10 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e  penFlags |= OPEN
c3b20 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f  _ACTION_OPEN_IF_
c3b30 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f 41 43  EXISTS | OPEN_AC
c3b40 54 49 4f 4e 5f 43 52 45 41 54 45 5f 49 46 5f 4e  TION_CREATE_IF_N
c3b50 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31  EW;.    OSTRACE1
c3b60 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 6e 65 77  ( "OPEN open new
c3b70 2f 63 72 65 61 74 65 5c 6e 22 20 29 3b 0a 20 20  /create\n" );.  
c3b80 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65  }else{.    ulOpe
c3b90 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e 5f 41  nFlags |= OPEN_A
c3ba0 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f 45 58  CTION_OPEN_IF_EX
c3bb0 49 53 54 53 20 7c 20 4f 50 45 4e 5f 41 43 54 49  ISTS | OPEN_ACTI
c3bc0 4f 4e 5f 46 41 49 4c 5f 49 46 5f 4e 45 57 3b 0a  ON_FAIL_IF_NEW;.
c3bd0 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f      OSTRACE1( "O
c3be0 50 45 4e 20 6f 70 65 6e 20 65 78 69 73 74 69 6e  PEN open existin
c3bf0 67 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  g\n" );.  }..  i
c3c00 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
c3c10 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 29  E_OPEN_MAIN_DB )
c3c20 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65  {.    ulOpenMode
c3c30 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44   |= OPEN_SHARE_D
c3c40 45 4e 59 4e 4f 4e 45 3b 0a 20 20 20 20 4f 53 54  ENYNONE;.    OST
c3c50 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73 68 61  RACE1( "OPEN sha
c3c60 72 65 20 72 65 61 64 2f 77 72 69 74 65 5c 6e 22  re read/write\n"
c3c70 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
c3c80 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f   ulOpenMode |= O
c3c90 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59 57 52  PEN_SHARE_DENYWR
c3ca0 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  ITE;.    OSTRACE
c3cb0 31 28 20 22 4f 50 45 4e 20 73 68 61 72 65 20 72  1( "OPEN share r
c3cc0 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b 0a 20  ead only\n" );. 
c3cd0 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 20   }..  if( flags 
c3ce0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45  & SQLITE_OPEN_DE
c3cf0 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 20  LETEONCLOSE ){. 
c3d00 20 20 20 63 68 61 72 20 70 61 74 68 55 74 66 38     char pathUtf8
c3d10 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a 23 69  [CCHMAXPATH];.#i
c3d20 66 64 65 66 20 4e 44 45 42 55 47 20 2f 2a 20 77  fdef NDEBUG /* w
c3d30 68 65 6e 20 64 65 62 75 67 67 69 6e 67 20 77 65  hen debugging we
c3d40 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 73 75   want to make su
c3d50 72 65 20 69 74 20 69 73 20 64 65 6c 65 74 65 64  re it is deleted
c3d60 20 2a 2f 0a 20 20 20 20 75 6c 46 69 6c 65 41 74   */.    ulFileAt
c3d70 74 72 69 62 75 74 65 20 3d 20 46 49 4c 45 5f 48  tribute = FILE_H
c3d80 49 44 44 45 4e 3b 0a 23 65 6e 64 69 66 0a 20 20  IDDEN;.#endif.  
c3d90 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d    os2FullPathnam
c3da0 65 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20  e( pVfs, zName, 
c3db0 43 43 48 4d 41 58 50 41 54 48 2c 20 70 61 74 68  CCHMAXPATH, path
c3dc0 55 74 66 38 20 29 3b 0a 20 20 20 20 70 46 69 6c  Utf8 );.    pFil
c3dd0 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20 63  e->pathToDel = c
c3de0 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54 6f  onvertUtf8PathTo
c3df0 43 70 28 20 70 61 74 68 55 74 66 38 20 29 3b 0a  Cp( pathUtf8 );.
c3e00 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f      OSTRACE1( "O
c3e10 50 45 4e 20 68 69 64 64 65 6e 2f 64 65 6c 65 74  PEN hidden/delet
c3e20 65 20 6f 6e 20 63 6c 6f 73 65 20 66 69 6c 65 20  e on close file 
c3e30 61 74 74 72 69 62 75 74 65 73 5c 6e 22 20 29 3b  attributes\n" );
c3e40 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 46  .  }else{.    pF
c3e50 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d  ile->pathToDel =
c3e60 20 4e 55 4c 4c 3b 0a 20 20 20 20 4f 53 54 52 41   NULL;.    OSTRA
c3e70 43 45 31 28 20 22 4f 50 45 4e 20 6e 6f 72 6d 61  CE1( "OPEN norma
c3e80 6c 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65  l file attribute
c3e90 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  \n" );.  }..  /*
c3ea0 20 61 6c 77 61 79 73 20 6f 70 65 6e 20 69 6e 20   always open in 
c3eb0 72 61 6e 64 6f 6d 20 61 63 63 65 73 73 20 6d 6f  random access mo
c3ec0 64 65 20 66 6f 72 20 70 6f 73 73 69 62 6c 79 20  de for possibly 
c3ed0 62 65 74 74 65 72 20 73 70 65 65 64 20 2a 2f 0a  better speed */.
c3ee0 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20    ulOpenMode |= 
c3ef0 4f 50 45 4e 5f 46 4c 41 47 53 5f 52 41 4e 44 4f  OPEN_FLAGS_RANDO
c3f00 4d 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20  M;.  ulOpenMode 
c3f10 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 46 41  |= OPEN_FLAGS_FA
c3f20 49 4c 5f 4f 4e 5f 45 52 52 4f 52 3b 0a 20 20 75  IL_ON_ERROR;.  u
c3f30 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45  lOpenMode |= OPE
c3f40 4e 5f 46 4c 41 47 53 5f 4e 4f 49 4e 48 45 52 49  N_FLAGS_NOINHERI
c3f50 54 3b 0a 0a 20 20 7a 4e 61 6d 65 43 70 20 3d 20  T;..  zNameCp = 
c3f60 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68 54  convertUtf8PathT
c3f70 6f 43 70 28 20 7a 4e 61 6d 65 20 29 3b 0a 20 20  oCp( zName );.  
c3f80 72 63 20 3d 20 44 6f 73 4f 70 65 6e 28 20 28 50  rc = DosOpen( (P
c3f90 53 5a 29 7a 4e 61 6d 65 43 70 2c 0a 20 20 20 20  SZ)zNameCp,.    
c3fa0 20 20 20 20 20 20 20 20 20 20 20 20 26 68 2c 0a              &h,.
c3fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3fc0 26 75 6c 41 63 74 69 6f 6e 2c 0a 20 20 20 20 20  &ulAction,.     
c3fd0 20 20 20 20 20 20 20 20 20 20 20 30 4c 2c 0a 20             0L,. 
c3fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
c3ff0 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 2c 0a  lFileAttribute,.
c4000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4010 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 0a 20 20 20  ulOpenFlags,.   
c4020 20 20 20 20 20 20 20 20 20 20 20 20 20 75 6c 4f               ulO
c4030 70 65 6e 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20  penMode,.       
c4040 20 20 20 20 20 20 20 20 20 28 50 45 41 4f 50 32           (PEAOP2
c4050 29 4e 55 4c 4c 20 29 3b 0a 20 20 66 72 65 65 28  )NULL );.  free(
c4060 20 7a 4e 61 6d 65 43 70 20 29 3b 0a 20 20 69 66   zNameCp );.  if
c4070 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ( rc != NO_ERROR
c4080 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 37   ){.    OSTRACE7
c4090 28 20 22 4f 50 45 4e 20 49 6e 76 61 6c 69 64 20  ( "OPEN Invalid 
c40a0 68 61 6e 64 6c 65 20 72 63 3d 25 64 3a 20 7a 4e  handle rc=%d: zN
c40b0 61 6d 65 3d 25 73 2c 20 75 6c 41 63 74 69 6f 6e  ame=%s, ulAction
c40c0 3d 25 23 6c 78 2c 20 75 6c 41 74 74 72 3d 25 23  =%#lx, ulAttr=%#
c40d0 6c 78 2c 20 75 6c 46 6c 61 67 73 3d 25 23 6c 78  lx, ulFlags=%#lx
c40e0 2c 20 75 6c 4d 6f 64 65 3d 25 23 6c 78 5c 6e 22  , ulMode=%#lx\n"
c40f0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
c4100 72 63 2c 20 7a 4e 61 6d 65 2c 20 75 6c 41 63 74  rc, zName, ulAct
c4110 69 6f 6e 2c 20 75 6c 46 69 6c 65 41 74 74 72 69  ion, ulFileAttri
c4120 62 75 74 65 2c 20 75 6c 4f 70 65 6e 46 6c 61 67  bute, ulOpenFlag
c4130 73 2c 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 29 3b  s, ulOpenMode );
c4140 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  .    if( pFile->
c4150 70 61 74 68 54 6f 44 65 6c 20 29 0a 20 20 20 20  pathToDel ).    
c4160 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d 3e 70    free( pFile->p
c4170 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20 20 20  athToDel );.    
c4180 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
c4190 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 66 28   = NULL;.    if(
c41a0 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
c41b0 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29  OPEN_READWRITE )
c41c0 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32  {.      OSTRACE2
c41d0 28 20 22 4f 50 45 4e 20 25 64 20 49 6e 76 61 6c  ( "OPEN %d Inval
c41e0 69 64 20 68 61 6e 64 6c 65 5c 6e 22 2c 20 28 28  id handle\n", ((
c41f0 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f  flags | SQLITE_O
c4200 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 26 20  PEN_READONLY) & 
c4210 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  ~SQLITE_OPEN_REA
c4220 44 57 52 49 54 45 29 20 29 3b 0a 20 20 20 20 20  DWRITE) );.     
c4230 20 72 65 74 75 72 6e 20 6f 73 32 4f 70 65 6e 28   return os2Open(
c4240 20 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 69 64   pVfs, zName, id
c4250 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
c4260 20 20 20 20 20 20 20 20 28 28 66 6c 61 67 73 20          ((flags 
c4270 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  | SQLITE_OPEN_RE
c4280 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54  ADONLY) & ~SQLIT
c4290 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
c42a0 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
c42b0 20 20 20 20 20 20 20 20 20 70 4f 75 74 46 6c 61           pOutFla
c42c0 67 73 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  gs );.    }else{
c42d0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
c42e0 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20  LITE_CANTOPEN;. 
c42f0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
c4300 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20  pOutFlags ){.   
c4310 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c   *pOutFlags = fl
c4320 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
c4330 4e 5f 52 45 41 44 57 52 49 54 45 20 3f 20 53 51  N_READWRITE ? SQ
c4340 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
c4350 49 54 45 20 3a 20 53 51 4c 49 54 45 5f 4f 50 45  ITE : SQLITE_OPE
c4360 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 7d 0a  N_READONLY;.  }.
c4370 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  .  pFile->pMetho
c4380 64 20 3d 20 26 6f 73 32 49 6f 4d 65 74 68 6f 64  d = &os2IoMethod
c4390 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 68  ;.  pFile->h = h
c43a0 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28  ;.  OpenCounter(
c43b0 2b 31 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  +1);.  OSTRACE3(
c43c0 20 22 4f 50 45 4e 20 25 64 20 70 4f 75 74 46 6c   "OPEN %d pOutFl
c43d0 61 67 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  ags=%d\n", pFile
c43e0 2d 3e 68 2c 20 70 4f 75 74 46 6c 61 67 73 20 29  ->h, pOutFlags )
c43f0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
c4400 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  E_OK;.}../*.** D
c4410 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65 64 20  elete the named 
c4420 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
c4430 69 6e 74 20 6f 73 32 44 65 6c 65 74 65 28 0a 20  int os2Delete(. 
c4440 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
c4450 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  fs,             
c4460 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75          /* Not u
c4470 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20  sed on os2 */.  
c4480 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
c4490 65 6e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20  ename,          
c44a0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
c44b0 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74 65  f file to delete
c44c0 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44 69   */.  int syncDi
c44d0 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
c44e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c44f0 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20  Not used on os2 
c4500 2a 2f 0a 29 7b 0a 20 20 41 50 49 52 45 54 20 72  */.){.  APIRET r
c4510 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
c4520 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43  char *zFilenameC
c4530 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50  p = convertUtf8P
c4540 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61  athToCp( zFilena
c4550 6d 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65  me );.  Simulate
c4560 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
c4570 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c  SQLITE_IOERR_DEL
c4580 45 54 45 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f  ETE );.  rc = Do
c4590 73 44 65 6c 65 74 65 28 20 28 50 53 5a 29 7a 46  sDelete( (PSZ)zF
c45a0 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 66  ilenameCp );.  f
c45b0 72 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70  ree( zFilenameCp
c45c0 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20   );.  OSTRACE2( 
c45d0 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e  "DELETE \"%s\"\n
c45e0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a  ", zFilename );.
c45f0 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e    return rc == N
c4600 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45  O_ERROR ? SQLITE
c4610 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45  _OK : SQLITE_IOE
c4620 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a 2f 2a  RR_DELETE;.}../*
c4630 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 65 78  .** Check the ex
c4640 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74 61 74  istance and stat
c4650 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a 2a 2f  us of a file..*/
c4660 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 41  .static int os2A
c4670 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33  ccess(.  sqlite3
c4680 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
c4690 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f     /* Not used o
c46a0 6e 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74  n os2 */.  const
c46b0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
c46c0 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ,    /* Name of 
c46d0 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f  file to check */
c46e0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
c46f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c4700 54 79 70 65 20 6f 66 20 74 65 73 74 20 74 6f 20  Type of test to 
c4710 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66 69 6c  make on this fil
c4720 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74  e */.  int *pOut
c4730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4740 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75 6c 74   /* Write result
c4750 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 46  s here */.){.  F
c4760 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 73 33  ILESTATUS3 fsts3
c4770 43 6f 6e 66 69 67 49 6e 66 6f 3b 0a 20 20 41 50  ConfigInfo;.  AP
c4780 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52  IRET rc = NO_ERR
c4790 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c  OR;.  char *zFil
c47a0 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72  enameCp = conver
c47b0 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a  tUtf8PathToCp( z
c47c0 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 0a 20 20 6d  Filename );..  m
c47d0 65 6d 73 65 74 28 20 26 66 73 74 73 33 43 6f 6e  emset( &fsts3Con
c47e0 66 69 67 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65  figInfo, 0, size
c47f0 6f 66 28 66 73 74 73 33 43 6f 6e 66 69 67 49 6e  of(fsts3ConfigIn
c4800 66 6f 29 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f  fo) );.  rc = Do
c4810 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20  sQueryPathInfo( 
c4820 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43 70  (PSZ)zFilenameCp
c4830 2c 20 46 49 4c 5f 53 54 41 4e 44 41 52 44 2c 0a  , FIL_STANDARD,.
c4840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4850 20 20 20 20 20 20 20 20 20 26 66 73 74 73 33 43           &fsts3C
c4860 6f 6e 66 69 67 49 6e 66 6f 2c 20 73 69 7a 65 6f  onfigInfo, sizeo
c4870 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20 29  f(FILESTATUS3) )
c4880 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 6c 65 6e  ;.  free( zFilen
c4890 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54 52 41  ameCp );.  OSTRA
c48a0 43 45 34 28 20 22 41 43 43 45 53 53 20 66 73 74  CE4( "ACCESS fst
c48b0 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74  s3ConfigInfo.att
c48c0 72 46 69 6c 65 3d 25 64 20 66 6c 61 67 73 3d 25  rFile=%d flags=%
c48d0 64 20 72 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20  d rc=%d\n",.    
c48e0 20 20 20 20 20 20 20 20 66 73 74 73 33 43 6f 6e          fsts3Con
c48f0 66 69 67 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65  figInfo.attrFile
c4900 2c 20 66 6c 61 67 73 2c 20 72 63 20 29 3b 0a 20  , flags, rc );. 
c4910 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29   switch( flags )
c4920 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
c4930 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20  E_ACCESS_READ:. 
c4940 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41     case SQLITE_A
c4950 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20  CCESS_EXISTS:.  
c4960 20 20 20 20 72 63 20 3d 20 28 72 63 20 3d 3d 20      rc = (rc == 
c4970 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 20  NO_ERROR);.     
c4980 20 4f 53 54 52 41 43 45 33 28 20 22 41 43 43 45   OSTRACE3( "ACCE
c4990 53 53 20 25 73 20 61 63 63 65 73 73 20 6f 66 20  SS %s access of 
c49a0 72 65 61 64 20 61 6e 64 20 65 78 69 73 74 73 20  read and exists 
c49b0 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c 65   rc=%d\n", zFile
c49c0 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20 20  name, rc );.    
c49d0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
c49e0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
c49f0 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20  READWRITE:.     
c4a00 20 72 63 20 3d 20 28 72 63 20 3d 3d 20 4e 4f 5f   rc = (rc == NO_
c4a10 45 52 52 4f 52 29 20 26 26 20 28 20 28 66 73 74  ERROR) && ( (fst
c4a20 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74  s3ConfigInfo.att
c4a30 72 46 69 6c 65 20 26 20 46 49 4c 45 5f 52 45 41  rFile & FILE_REA
c4a40 44 4f 4e 4c 59 29 20 3d 3d 20 30 20 29 3b 0a 20  DONLY) == 0 );. 
c4a50 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22       OSTRACE3( "
c4a60 41 43 43 45 53 53 20 25 73 20 61 63 63 65 73 73  ACCESS %s access
c4a70 20 6f 66 20 72 65 61 64 2f 77 72 69 74 65 20 20   of read/write  
c4a80 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c 65 6e  rc=%d\n", zFilen
c4a90 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20 20 20  ame, rc );.     
c4aa0 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61   break;.    defa
c4ab0 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72  ult:.      asser
c4ac0 74 28 20 21 22 49 6e 76 61 6c 69 64 20 66 6c 61  t( !"Invalid fla
c4ad0 67 73 20 61 72 67 75 6d 65 6e 74 22 20 29 3b 0a  gs argument" );.
c4ae0 20 20 7d 0a 20 20 2a 70 4f 75 74 20 3d 20 72 63    }.  *pOut = rc
c4af0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
c4b00 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65  E_OK;.}...#ifnde
c4b10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
c4b20 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a  AD_EXTENSION./*.
c4b30 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20 66 6f  ** Interfaces fo
c4b40 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68 61 72  r opening a shar
c4b50 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69 6e 64  ed library, find
c4b60 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  ing entry points
c4b70 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65 20 73  .** within the s
c4b80 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 61  hared library, a
c4b90 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65 20 73  nd closing the s
c4ba0 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a  hared library..*
c4bb0 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63  /./*.** Interfac
c4bc0 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61  es for opening a
c4bd0 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
c4be0 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70   finding entry p
c4bf0 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20  oints.** within 
c4c00 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
c4c10 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20  ry, and closing 
c4c20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
c4c30 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ry..*/.static vo
c4c40 69 64 20 2a 6f 73 32 44 6c 4f 70 65 6e 28 73 71  id *os2DlOpen(sq
c4c50 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
c4c60 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
c4c70 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 43 48 41 52  lename){.  UCHAR
c4c80 20 6c 6f 61 64 45 72 72 5b 32 35 36 5d 3b 0a 20   loadErr[256];. 
c4c90 20 48 4d 4f 44 55 4c 45 20 68 6d 6f 64 3b 0a 20   HMODULE hmod;. 
c4ca0 20 41 50 49 52 45 54 20 72 63 3b 0a 20 20 63 68   APIRET rc;.  ch
c4cb0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20  ar *zFilenameCp 
c4cc0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74  = convertUtf8Pat
c4cd0 68 54 6f 43 70 28 7a 46 69 6c 65 6e 61 6d 65 29  hToCp(zFilename)
c4ce0 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4c 6f 61 64  ;.  rc = DosLoad
c4cf0 4d 6f 64 75 6c 65 28 28 50 53 5a 29 6c 6f 61 64  Module((PSZ)load
c4d00 45 72 72 2c 20 73 69 7a 65 6f 66 28 6c 6f 61 64  Err, sizeof(load
c4d10 45 72 72 29 2c 20 7a 46 69 6c 65 6e 61 6d 65 43  Err), zFilenameC
c4d20 70 2c 20 26 68 6d 6f 64 29 3b 0a 20 20 66 72 65  p, &hmod);.  fre
c4d30 65 28 7a 46 69 6c 65 6e 61 6d 65 43 70 29 3b 0a  e(zFilenameCp);.
c4d40 20 20 72 65 74 75 72 6e 20 72 63 20 21 3d 20 4e    return rc != N
c4d50 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20 28 76  O_ERROR ? 0 : (v
c4d60 6f 69 64 2a 29 68 6d 6f 64 3b 0a 7d 0a 2f 2a 0a  oid*)hmod;.}./*.
c4d70 2a 2a 20 41 20 6e 6f 2d 6f 70 20 73 69 6e 63 65  ** A no-op since
c4d80 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
c4d90 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 74  is returned on t
c4da0 68 65 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65  he DosLoadModule
c4db0 20 63 61 6c 6c 2e 0a 2a 2a 20 6f 73 32 44 6c 6f   call..** os2Dlo
c4dc0 70 65 6e 20 72 65 74 75 72 6e 73 20 7a 65 72 6f  pen returns zero
c4dd0 20 69 66 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c   if DosLoadModul
c4de0 65 20 69 73 20 6e 6f 74 20 73 75 63 63 65 73 73  e is not success
c4df0 66 75 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ful..*/.static v
c4e00 6f 69 64 20 6f 73 32 44 6c 45 72 72 6f 72 28 73  oid os2DlError(s
c4e10 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
c4e20 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
c4e30 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 2f 2a 20 6e   *zBufOut){./* n
c4e40 6f 2d 6f 70 20 2a 2f 0a 7d 0a 73 74 61 74 69 63  o-op */.}.static
c4e50 20 76 6f 69 64 20 2a 6f 73 32 44 6c 53 79 6d 28   void *os2DlSym(
c4e60 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
c4e70 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65  s, void *pHandle
c4e80 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
c4e90 79 6d 62 6f 6c 29 7b 0a 20 20 50 46 4e 20 70 66  ymbol){.  PFN pf
c4ea0 6e 3b 0a 20 20 41 50 49 52 45 54 20 72 63 3b 0a  n;.  APIRET rc;.
c4eb0 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50    rc = DosQueryP
c4ec0 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55 4c 45  rocAddr((HMODULE
c4ed0 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20 7a 53  )pHandle, 0L, zS
c4ee0 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20  ymbol, &pfn);.  
c4ef0 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52  if( rc != NO_ERR
c4f00 4f 52 20 29 7b 0a 20 20 20 20 2f 2a 20 69 66 20  OR ){.    /* if 
c4f10 74 68 65 20 73 79 6d 62 6f 6c 20 69 74 73 65 6c  the symbol itsel
c4f20 66 20 77 61 73 20 6e 6f 74 20 66 6f 75 6e 64 2c  f was not found,
c4f30 20 73 65 61 72 63 68 20 61 67 61 69 6e 20 66 6f   search again fo
c4f40 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20  r the same.     
c4f50 2a 20 73 79 6d 62 6f 6c 20 77 69 74 68 20 61 6e  * symbol with an
c4f60 20 65 78 74 72 61 20 75 6e 64 65 72 73 63 6f 72   extra underscor
c4f70 65 2c 20 74 68 61 74 20 6d 69 67 68 74 20 62 65  e, that might be
c4f80 20 6e 65 65 64 65 64 20 64 65 70 65 6e 64 69 6e   needed dependin
c4f90 67 0a 20 20 20 20 20 2a 20 6f 6e 20 74 68 65 20  g.     * on the 
c4fa0 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e 74 69  calling conventi
c4fb0 6f 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 5f  on */.    char _
c4fc0 7a 53 79 6d 62 6f 6c 5b 32 35 36 5d 20 3d 20 22  zSymbol[256] = "
c4fd0 5f 22 3b 0a 20 20 20 20 73 74 72 6e 63 61 74 28  _";.    strncat(
c4fe0 5f 7a 53 79 6d 62 6f 6c 2c 20 7a 53 79 6d 62 6f  _zSymbol, zSymbo
c4ff0 6c 2c 20 32 35 35 29 3b 0a 20 20 20 20 72 63 20  l, 255);.    rc 
c5000 3d 20 44 6f 73 51 75 65 72 79 50 72 6f 63 41 64  = DosQueryProcAd
c5010 64 72 28 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e  dr((HMODULE)pHan
c5020 64 6c 65 2c 20 30 4c 2c 20 5f 7a 53 79 6d 62 6f  dle, 0L, _zSymbo
c5030 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 7d 0a 20 20  l, &pfn);.  }.  
c5040 72 65 74 75 72 6e 20 72 63 20 21 3d 20 4e 4f 5f  return rc != NO_
c5050 45 52 52 4f 52 20 3f 20 30 20 3a 20 28 76 6f 69  ERROR ? 0 : (voi
c5060 64 2a 29 70 66 6e 3b 0a 7d 0a 73 74 61 74 69 63  d*)pfn;.}.static
c5070 20 76 6f 69 64 20 6f 73 32 44 6c 43 6c 6f 73 65   void os2DlClose
c5080 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
c5090 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c  fs, void *pHandl
c50a0 65 29 7b 0a 20 20 44 6f 73 46 72 65 65 4d 6f 64  e){.  DosFreeMod
c50b0 75 6c 65 28 28 48 4d 4f 44 55 4c 45 29 70 48 61  ule((HMODULE)pHa
c50c0 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f  ndle);.}.#else /
c50d0 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  * if SQLITE_OMIT
c50e0 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20  _LOAD_EXTENSION 
c50f0 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20  is defined: */. 
c5100 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 4f 70   #define os2DlOp
c5110 65 6e 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f  en 0.  #define o
c5120 73 32 44 6c 45 72 72 6f 72 20 30 0a 20 20 23 64  s2DlError 0.  #d
c5130 65 66 69 6e 65 20 6f 73 32 44 6c 53 79 6d 20 30  efine os2DlSym 0
c5140 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c  .  #define os2Dl
c5150 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a  Close 0.#endif..
c5160 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70 20  ./*.** Write up 
c5170 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66  to nBuf bytes of
c5180 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f   randomness into
c5190 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63   zBuf..*/.static
c51a0 20 69 6e 74 20 6f 73 32 52 61 6e 64 6f 6d 6e 65   int os2Randomne
c51b0 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ss(sqlite3_vfs *
c51c0 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20  pVfs, int nBuf, 
c51d0 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a 20 20  char *zBuf ){.  
c51e0 69 6e 74 20 6e 20 3d 20 30 3b 0a 23 69 66 20 64  int n = 0;.#if d
c51f0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
c5200 53 54 29 0a 20 20 6e 20 3d 20 6e 42 75 66 3b 0a  ST).  n = nBuf;.
c5210 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30    memset(zBuf, 0
c5220 2c 20 6e 42 75 66 29 3b 0a 23 65 6c 73 65 0a 20  , nBuf);.#else. 
c5230 20 69 6e 74 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   int sizeofULong
c5240 20 3d 20 73 69 7a 65 6f 66 28 55 4c 4f 4e 47 29   = sizeof(ULONG)
c5250 3b 0a 20 20 69 66 28 20 28 69 6e 74 29 73 69 7a  ;.  if( (int)siz
c5260 65 6f 66 28 44 41 54 45 54 49 4d 45 29 20 3c 3d  eof(DATETIME) <=
c5270 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20   nBuf - n ){.   
c5280 20 44 41 54 45 54 49 4d 45 20 78 3b 0a 20 20 20   DATETIME x;.   
c5290 20 44 6f 73 47 65 74 44 61 74 65 54 69 6d 65 28   DosGetDateTime(
c52a0 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  &x);.    memcpy(
c52b0 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69  &zBuf[n], &x, si
c52c0 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e 20  zeof(x));.    n 
c52d0 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20  += sizeof(x);.  
c52e0 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55  }..  if( sizeofU
c52f0 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e  Long <= nBuf - n
c5300 20 29 7b 0a 20 20 20 20 50 50 49 42 20 70 70 69   ){.    PPIB ppi
c5310 62 3b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66  b;.    DosGetInf
c5320 6f 42 6c 6f 63 6b 73 28 4e 55 4c 4c 2c 20 26 70  oBlocks(NULL, &p
c5330 70 69 62 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  pib);.    memcpy
c5340 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 70 69 62  (&zBuf[n], &ppib
c5350 2d 3e 70 69 62 5f 75 6c 70 69 64 2c 20 73 69 7a  ->pib_ulpid, siz
c5360 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e  eofULong);.    n
c5370 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b   += sizeofULong;
c5380 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 69 7a 65  .  }..  if( size
c5390 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20  ofULong <= nBuf 
c53a0 2d 20 6e 20 29 7b 0a 20 20 20 20 50 54 49 42 20  - n ){.    PTIB 
c53b0 70 74 69 62 3b 0a 20 20 20 20 44 6f 73 47 65 74  ptib;.    DosGet
c53c0 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62  InfoBlocks(&ptib
c53d0 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 6d 65 6d  , NULL);.    mem
c53e0 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70  cpy(&zBuf[n], &p
c53f0 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e  tib->tib_ptib2->
c5400 74 69 62 32 5f 75 6c 74 69 64 2c 20 73 69 7a 65  tib2_ultid, size
c5410 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20  ofULong);.    n 
c5420 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a  += sizeofULong;.
c5430 20 20 7d 0a 0a 20 20 2f 2a 20 69 66 20 77 65 20    }..  /* if we 
c5440 73 74 69 6c 6c 20 68 61 76 65 6e 27 74 20 66 69  still haven't fi
c5450 6c 6c 65 64 20 74 68 65 20 62 75 66 66 65 72 20  lled the buffer 
c5460 79 65 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  yet the followin
c5470 67 20 77 69 6c 6c 20 2a 2f 0a 20 20 2f 2a 20 67  g will */.  /* g
c5480 72 61 62 20 65 76 65 72 79 74 68 69 6e 67 20 6f  rab everything o
c5490 6e 63 65 20 69 6e 73 74 65 61 64 20 6f 66 20 6d  nce instead of m
c54a0 61 6b 69 6e 67 20 73 65 76 65 72 61 6c 20 63 61  aking several ca
c54b0 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65  lls for a single
c54c0 20 69 74 65 6d 20 2a 2f 0a 20 20 69 66 28 20 73   item */.  if( s
c54d0 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42  izeofULong <= nB
c54e0 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 55 4c  uf - n ){.    UL
c54f0 4f 4e 47 20 75 6c 53 79 73 49 6e 66 6f 5b 51 53  ONG ulSysInfo[QS
c5500 56 5f 4d 41 58 5d 3b 0a 20 20 20 20 44 6f 73 51  V_MAX];.    DosQ
c5510 75 65 72 79 53 79 73 49 6e 66 6f 28 31 4c 2c 20  uerySysInfo(1L, 
c5520 51 53 56 5f 4d 41 58 2c 20 75 6c 53 79 73 49 6e  QSV_MAX, ulSysIn
c5530 66 6f 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20  fo, sizeofULong 
c5540 2a 20 51 53 56 5f 4d 41 58 29 3b 0a 0a 20 20 20  * QSV_MAX);..   
c5550 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
c5560 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56  , &ulSysInfo[QSV
c5570 5f 4d 53 5f 43 4f 55 4e 54 20 2d 20 31 5d 2c 20  _MS_COUNT - 1], 
c5580 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20  sizeofULong);.  
c5590 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f    n += sizeofULo
c55a0 6e 67 3b 0a 0a 20 20 20 20 69 66 28 20 73 69 7a  ng;..    if( siz
c55b0 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66  eofULong <= nBuf
c55c0 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65   - n ){.      me
c55d0 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26  mcpy(&zBuf[n], &
c55e0 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49  ulSysInfo[QSV_TI
c55f0 4d 45 52 5f 49 4e 54 45 52 56 41 4c 20 2d 20 31  MER_INTERVAL - 1
c5600 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b  ], sizeofULong);
c5610 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  .      n += size
c5620 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20  ofULong;.    }. 
c5630 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f     if( sizeofULo
c5640 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29  ng <= nBuf - n )
c5650 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26  {.      memcpy(&
c5660 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49  zBuf[n], &ulSysI
c5670 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 4c 4f 57  nfo[QSV_TIME_LOW
c5680 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f   - 1], sizeofULo
c5690 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20  ng);.      n += 
c56a0 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20  sizeofULong;.   
c56b0 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f   }.    if( sizeo
c56c0 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d  fULong <= nBuf -
c56d0 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63   n ){.      memc
c56e0 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c  py(&zBuf[n], &ul
c56f0 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45  SysInfo[QSV_TIME
c5700 5f 48 49 47 48 20 2d 20 31 5d 2c 20 73 69 7a 65  _HIGH - 1], size
c5710 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20  ofULong);.      
c5720 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  n += sizeofULong
c5730 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
c5740 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e  sizeofULong <= n
c5750 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20  Buf - n ){.     
c5760 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
c5770 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56  , &ulSysInfo[QSV
c5780 5f 54 4f 54 41 56 41 49 4c 4d 45 4d 20 2d 20 31  _TOTAVAILMEM - 1
c5790 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b  ], sizeofULong);
c57a0 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  .      n += size
c57b0 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20  ofULong;.    }. 
c57c0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74   }.#endif..  ret
c57d0 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn n;.}../*.** 
c57e0 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74  Sleep for a litt
c57f0 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72  le while.  Retur
c5800 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  n the amount of 
c5810 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54  time slept..** T
c5820 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  he argument is t
c5830 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63  he number of mic
c5840 72 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e  roseconds we wan
c5850 74 20 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54  t to sleep..** T
c5860 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
c5870 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
c5880 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66   microseconds of
c5890 20 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a   sleep actually.
c58a0 2a 2a 20 72 65 71 75 65 73 74 65 64 20 66 72 6f  ** requested fro
c58b0 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  m the underlying
c58c0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
c58d0 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63  m, a number whic
c58e0 68 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72  h.** might be gr
c58f0 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
c5900 75 61 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d  ual to the argum
c5910 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73  ent, but not les
c5920 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72  s.** than the ar
c5930 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  gument..*/.stati
c5940 63 20 69 6e 74 20 6f 73 32 53 6c 65 65 70 28 20  c int os2Sleep( 
c5950 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
c5960 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 20  s, int microsec 
c5970 29 7b 0a 20 20 44 6f 73 53 6c 65 65 70 28 20 28  ){.  DosSleep( (
c5980 6d 69 63 72 6f 73 65 63 2f 31 30 30 30 29 20 29  microsec/1000) )
c5990 3b 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f  ;.  return micro
c59a0 73 65 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  sec;.}../*.** Th
c59b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
c59c0 61 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20  able, if set to 
c59d0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  a non-zero value
c59e0 2c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65  , becomes the re
c59f0 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64  sult.** returned
c5a00 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43   from sqlite3OsC
c5a10 75 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54  urrentTime().  T
c5a20 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
c5a30 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64  testing..*/.#ifd
c5a40 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
c5a50 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
c5a60 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69  lite3_current_ti
c5a70 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  me = 0;.#endif..
c5a80 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63  /*.** Find the c
c5a90 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20  urrent time (in 
c5aa0 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69  Universal Coordi
c5ab0 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72  nated Time).  Wr
c5ac0 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65  ite the.** curre
c5ad0 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65  nt time and date
c5ae0 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79   as a Julian Day
c5af0 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72   number into *pr
c5b00 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72  Now and.** retur
c5b10 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69  n 0.  Return 1 i
c5b20 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64  f the time and d
c5b30 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  ate cannot be fo
c5b40 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 6f 73 32 43  und..*/.int os2C
c5b50 75 72 72 65 6e 74 54 69 6d 65 28 20 73 71 6c 69  urrentTime( sqli
c5b60 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64  te3_vfs *pVfs, d
c5b70 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 20 29 7b 0a  ouble *prNow ){.
c5b80 20 20 64 6f 75 62 6c 65 20 6e 6f 77 3b 0a 20 20    double now;.  
c5b90 53 48 4f 52 54 20 6d 69 6e 75 74 65 3b 20 2f 2a  SHORT minute; /*
c5ba0 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 62 6c   needs to be abl
c5bb0 65 20 74 6f 20 63 6f 70 65 20 77 69 74 68 20 6e  e to cope with n
c5bc0 65 67 61 74 69 76 65 20 74 69 6d 65 7a 6f 6e 65  egative timezone
c5bd0 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 55 53 48   offset */.  USH
c5be0 4f 52 54 20 73 65 63 6f 6e 64 2c 20 68 6f 75 72  ORT second, hour
c5bf0 2c 0a 20 20 20 20 20 20 20 20 20 64 61 79 2c 20  ,.         day, 
c5c00 6d 6f 6e 74 68 2c 20 79 65 61 72 3b 0a 20 20 44  month, year;.  D
c5c10 41 54 45 54 49 4d 45 20 64 74 3b 0a 20 20 44 6f  ATETIME dt;.  Do
c5c20 73 47 65 74 44 61 74 65 54 69 6d 65 28 20 26 64  sGetDateTime( &d
c5c30 74 20 29 3b 0a 20 20 73 65 63 6f 6e 64 20 3d 20  t );.  second = 
c5c40 28 55 53 48 4f 52 54 29 64 74 2e 73 65 63 6f 6e  (USHORT)dt.secon
c5c50 64 73 3b 0a 20 20 6d 69 6e 75 74 65 20 3d 20 28  ds;.  minute = (
c5c60 53 48 4f 52 54 29 64 74 2e 6d 69 6e 75 74 65 73  SHORT)dt.minutes
c5c70 20 2b 20 64 74 2e 74 69 6d 65 7a 6f 6e 65 3b 0a   + dt.timezone;.
c5c80 20 20 68 6f 75 72 20 3d 20 28 55 53 48 4f 52 54    hour = (USHORT
c5c90 29 64 74 2e 68 6f 75 72 73 3b 0a 20 20 64 61 79  )dt.hours;.  day
c5ca0 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 64 61   = (USHORT)dt.da
c5cb0 79 3b 0a 20 20 6d 6f 6e 74 68 20 3d 20 28 55 53  y;.  month = (US
c5cc0 48 4f 52 54 29 64 74 2e 6d 6f 6e 74 68 3b 0a 20  HORT)dt.month;. 
c5cd0 20 79 65 61 72 20 3d 20 28 55 53 48 4f 52 54 29   year = (USHORT)
c5ce0 64 74 2e 79 65 61 72 3b 0a 0a 20 20 2f 2a 20 43  dt.year;..  /* C
c5cf0 61 6c 63 75 6c 61 74 69 6f 6e 73 20 66 72 6f 6d  alculations from
c5d00 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72   http://www.astr
c5d10 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72  o.keele.ac.uk/~r
c5d20 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64  no/Astronomy/hjd
c5d30 2e 68 74 6d 6c 0a 20 20 20 20 20 68 74 74 70 3a  .html.     http:
c5d40 2f 2f 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c  //www.astro.keel
c5d50 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74  e.ac.uk/~rno/Ast
c5d60 72 6f 6e 6f 6d 79 2f 68 6a 64 2d 30 2e 31 2e 63  ronomy/hjd-0.1.c
c5d70 20 2a 2f 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61   */.  /* Calcula
c5d80 74 65 20 74 68 65 20 4a 75 6c 69 61 6e 20 64 61  te the Julian da
c5d90 79 73 20 2a 2f 0a 20 20 6e 6f 77 20 3d 20 64 61  ys */.  now = da
c5da0 79 20 2d 20 33 32 30 37 36 20 2b 0a 20 20 20 20  y - 32076 +.    
c5db0 31 34 36 31 2a 28 79 65 61 72 20 2b 20 34 38 30  1461*(year + 480
c5dc0 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29  0 + (month - 14)
c5dd0 2f 31 32 29 2f 34 20 2b 0a 20 20 20 20 33 36 37  /12)/4 +.    367
c5de0 2a 28 6d 6f 6e 74 68 20 2d 20 32 20 2d 20 28 6d  *(month - 2 - (m
c5df0 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32 2a 31 32  onth - 14)/12*12
c5e00 29 2f 31 32 20 2d 0a 20 20 20 20 33 2a 28 28 79  )/12 -.    3*((y
c5e10 65 61 72 20 2b 20 34 39 30 30 20 2b 20 28 6d 6f  ear + 4900 + (mo
c5e20 6e 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 31 30  nth - 14)/12)/10
c5e30 30 29 2f 34 3b 0a 0a 20 20 2f 2a 20 41 64 64 20  0)/4;..  /* Add 
c5e40 74 68 65 20 66 72 61 63 74 69 6f 6e 61 6c 20 68  the fractional h
c5e50 6f 75 72 73 2c 20 6d 69 6e 73 20 61 6e 64 20 73  ours, mins and s
c5e60 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 6e 6f 77 20  econds */.  now 
c5e70 2b 3d 20 28 68 6f 75 72 20 2b 20 31 32 2e 30 29  += (hour + 12.0)
c5e80 2f 32 34 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20  /24.0;.  now += 
c5e90 6d 69 6e 75 74 65 2f 31 34 34 30 2e 30 3b 0a 20  minute/1440.0;. 
c5ea0 20 6e 6f 77 20 2b 3d 20 73 65 63 6f 6e 64 2f 38   now += second/8
c5eb0 36 34 30 30 2e 30 3b 0a 20 20 2a 70 72 4e 6f 77  6400.0;.  *prNow
c5ec0 20 3d 20 6e 6f 77 3b 0a 23 69 66 64 65 66 20 53   = now;.#ifdef S
c5ed0 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28  QLITE_TEST.  if(
c5ee0 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
c5ef0 5f 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72  _time ){.    *pr
c5f00 4e 6f 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 75  Now = sqlite3_cu
c5f10 72 72 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30  rrent_time/86400
c5f20 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a  .0 + 2440587.5;.
c5f30 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
c5f40 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74 61 74 69 63  urn 0;.}..static
c5f50 20 69 6e 74 20 6f 73 32 47 65 74 4c 61 73 74 45   int os2GetLastE
c5f60 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73  rror(sqlite3_vfs
c5f70 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66   *pVfs, int nBuf
c5f80 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
c5f90 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
c5fa0 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
c5fb0 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20  nd deinitialize 
c5fc0 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
c5fd0 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a  stem interface..
c5fe0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
c5ff0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  t sqlite3_os_ini
c6000 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69  t(void){.  stati
c6010 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 73  c sqlite3_vfs os
c6020 32 56 66 73 20 3d 20 7b 0a 20 20 20 20 31 2c 20  2Vfs = {.    1, 
c6030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c6040 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
c6050 20 20 20 73 69 7a 65 6f 66 28 6f 73 32 46 69 6c     sizeof(os2Fil
c6060 65 29 2c 20 20 20 2f 2a 20 73 7a 4f 73 46 69 6c  e),   /* szOsFil
c6070 65 20 2a 2f 0a 20 20 20 20 43 43 48 4d 41 58 50  e */.    CCHMAXP
c6080 41 54 48 2c 20 20 20 20 20 20 20 20 2f 2a 20 6d  ATH,        /* m
c6090 78 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20  xPathname */.   
c60a0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
c60b0 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a      /* pNext */.
c60c0 20 20 20 20 22 6f 73 32 22 2c 20 20 20 20 20 20      "os2",      
c60d0 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20         /* zName 
c60e0 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
c60f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 70            /* pAp
c6100 70 44 61 74 61 20 2a 2f 0a 0a 20 20 20 20 6f 73  pData */..    os
c6110 32 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20  2Open,          
c6120 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 0a 20 20 20   /* xOpen */.   
c6130 20 6f 73 32 44 65 6c 65 74 65 2c 20 20 20 20 20   os2Delete,     
c6140 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a      /* xDelete *
c6150 2f 0a 20 20 20 20 6f 73 32 41 63 63 65 73 73 2c  /.    os2Access,
c6160 20 20 20 20 20 20 20 20 20 2f 2a 20 78 41 63 63           /* xAcc
c6170 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 46 75  ess */.    os2Fu
c6180 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20 2f 2a  llPathname,   /*
c6190 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a   xFullPathname *
c61a0 2f 0a 20 20 20 20 6f 73 32 44 6c 4f 70 65 6e 2c  /.    os2DlOpen,
c61b0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f           /* xDlO
c61c0 70 65 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c  pen */.    os2Dl
c61d0 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 2f 2a  Error,        /*
c61e0 20 78 44 6c 45 72 72 6f 72 20 2a 2f 0a 20 20 20   xDlError */.   
c61f0 20 6f 73 32 44 6c 53 79 6d 2c 20 20 20 20 20 20   os2DlSym,      
c6200 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a 2f      /* xDlSym */
c6210 0a 20 20 20 20 6f 73 32 44 6c 43 6c 6f 73 65 2c  .    os2DlClose,
c6220 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c          /* xDlCl
c6230 6f 73 65 20 2a 2f 0a 20 20 20 20 6f 73 32 52 61  ose */.    os2Ra
c6240 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 2f 2a  ndomness,     /*
c6250 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 2a 2f 0a   xRandomness */.
c6260 20 20 20 20 6f 73 32 53 6c 65 65 70 2c 20 20 20      os2Sleep,   
c6270 20 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70         /* xSleep
c6280 20 2a 2f 0a 20 20 20 20 6f 73 32 43 75 72 72 65   */.    os2Curre
c6290 6e 74 54 69 6d 65 2c 20 20 20 20 2f 2a 20 78 43  ntTime,    /* xC
c62a0 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 0a 20 20  urrentTime */.  
c62b0 20 20 6f 73 32 47 65 74 4c 61 73 74 45 72 72 6f    os2GetLastErro
c62c0 72 20 20 20 20 2f 2a 20 78 47 65 74 4c 61 73 74  r    /* xGetLast
c62d0 45 72 72 6f 72 20 2a 2f 0a 20 20 7d 3b 0a 20 20  Error */.  };.  
c62e0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
c62f0 73 74 65 72 28 26 6f 73 32 56 66 73 2c 20 31 29  ster(&os2Vfs, 1)
c6300 3b 0a 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a  ;.  initUconvObj
c6310 65 63 74 73 28 29 3b 0a 20 20 72 65 74 75 72 6e  ects();.  return
c6320 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51   SQLITE_OK;.}.SQ
c6330 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
c6340 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64  ite3_os_end(void
c6350 29 7b 0a 20 20 66 72 65 65 55 63 6f 6e 76 4f 62  ){.  freeUconvOb
c6360 6a 65 63 74 73 28 29 3b 0a 20 20 72 65 74 75 72  jects();.  retur
c6370 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
c6380 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
c6390 5f 4f 53 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a 2a  _OS_OS2 */../***
c63a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
c63b0 6f 66 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a  of os_os2.c ****
c63c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c63d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c63e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
c63f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
c6400 6e 20 66 69 6c 65 20 6f 73 5f 75 6e 69 78 2e 63  n file os_unix.c
c6410 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c6420 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
c6440 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a  * 2004 May 22.**
c6450 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
c6460 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
c6470 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
c6480 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
c6490 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
c64a0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
c64b0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
c64c0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
c64d0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
c64e0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
c64f0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
c6500 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
c6510 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
c6520 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
c6530 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
c6540 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
c6550 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
c6560 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
c6570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c6590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c65a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c65b0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
c65c0 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 56  e contains the V
c65d0 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  FS implementatio
c65e0 6e 20 66 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 20  n for unix-like 
c65f0 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
c6600 73 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 4c 69 6e  s.** include Lin
c6610 75 78 2c 20 4d 61 63 4f 53 58 2c 20 2a 42 53 44  ux, MacOSX, *BSD
c6620 2c 20 51 4e 58 2c 20 56 78 57 6f 72 6b 73 2c 20  , QNX, VxWorks, 
c6630 41 49 58 2c 20 48 50 55 58 2c 20 61 6e 64 20 6f  AIX, HPUX, and o
c6640 74 68 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  thers..**.** The
c6650 72 65 20 61 72 65 20 61 63 74 75 61 6c 6c 79 20  re are actually 
c6660 73 65 76 65 72 61 6c 20 64 69 66 66 65 72 65 6e  several differen
c6670 74 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74 61  t VFS implementa
c6680 74 69 6f 6e 73 20 69 6e 20 74 68 69 73 20 66 69  tions in this fi
c6690 6c 65 2e 0a 2a 2a 20 54 68 65 20 64 69 66 66 65  le..** The diffe
c66a0 72 65 6e 63 65 73 20 61 72 65 20 69 6e 20 74 68  rences are in th
c66b0 65 20 77 61 79 20 74 68 61 74 20 66 69 6c 65 20  e way that file 
c66c0 6c 6f 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 2e  locking is done.
c66d0 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a    The default.**
c66e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
c66f0 75 73 65 73 20 50 6f 73 69 78 20 41 64 76 69 73  uses Posix Advis
c6700 6f 72 79 20 4c 6f 63 6b 73 2e 20 20 41 6c 74 65  ory Locks.  Alte
c6710 72 6e 61 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e  rnative implemen
c6720 74 61 74 69 6f 6e 73 0a 2a 2a 20 75 73 65 20 66  tations.** use f
c6730 6c 6f 63 6b 28 29 2c 20 64 6f 74 2d 66 69 6c 65  lock(), dot-file
c6740 73 2c 20 76 61 72 69 6f 75 73 20 70 72 6f 70 72  s, various propr
c6750 69 65 74 61 72 79 20 6c 6f 63 6b 69 6e 67 20 73  ietary locking s
c6760 63 68 65 6d 61 73 2c 20 6f 72 20 73 69 6d 70 6c  chemas, or simpl
c6770 79 0a 2a 2a 20 73 6b 69 70 20 6c 6f 63 6b 69 6e  y.** skip lockin
c6780 67 20 61 6c 6c 20 74 6f 67 65 74 68 65 72 2e 0a  g all together..
c6790 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 6f 75 72 63  **.** This sourc
c67a0 65 20 66 69 6c 65 20 69 73 20 6f 72 67 61 6e 69  e file is organi
c67b0 7a 65 64 20 69 6e 74 6f 20 64 69 76 69 73 69 6f  zed into divisio
c67c0 6e 73 20 77 68 65 72 65 20 74 68 65 20 6c 6f 67  ns where the log
c67d0 69 63 20 66 6f 72 20 76 61 72 69 6f 75 73 0a 2a  ic for various.*
c67e0 2a 20 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 69  * subfunctions i
c67f0 73 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68  s contained with
c6800 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  in the appropria
c6810 74 65 20 64 69 76 69 73 69 6f 6e 2e 20 20 50 4c  te division.  PL
c6820 45 41 53 45 0a 2a 2a 20 4b 45 45 50 20 54 48 45  EASE.** KEEP THE
c6830 20 53 54 52 55 43 54 55 52 45 20 4f 46 20 54 48   STRUCTURE OF TH
c6840 49 53 20 46 49 4c 45 20 49 4e 54 41 43 54 2e 20  IS FILE INTACT. 
c6850 20 4e 65 77 20 63 6f 64 65 20 73 68 6f 75 6c 64   New code should
c6860 20 62 65 20 70 6c 61 63 65 64 0a 2a 2a 20 69 6e   be placed.** in
c6870 20 74 68 65 20 63 6f 72 72 65 63 74 20 64 69 76   the correct div
c6880 69 73 69 6f 6e 20 61 6e 64 20 73 68 6f 75 6c 64  ision and should
c6890 20 62 65 20 63 6c 65 61 72 6c 79 20 6c 61 62 65   be clearly labe
c68a0 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c  led..**.** The l
c68b0 61 79 6f 75 74 20 6f 66 20 64 69 76 69 73 69 6f  ayout of divisio
c68c0 6e 73 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  ns is as follows
c68d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 47 65 6e  :.**.**   *  Gen
c68e0 65 72 61 6c 2d 70 75 72 70 6f 73 65 20 64 65 63  eral-purpose dec
c68f0 6c 61 72 61 74 69 6f 6e 73 20 61 6e 64 20 75 74  larations and ut
c6900 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 2e  ility functions.
c6910 0a 2a 2a 20 20 20 2a 20 20 55 6e 69 71 75 65 20  .**   *  Unique 
c6920 66 69 6c 65 20 49 44 20 6c 6f 67 69 63 20 75 73  file ID logic us
c6930 65 64 20 62 79 20 56 78 57 6f 72 6b 73 2e 0a 2a  ed by VxWorks..*
c6940 2a 20 20 20 2a 20 20 56 61 72 69 6f 75 73 20 6c  *   *  Various l
c6950 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65  ocking primitive
c6960 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
c6970 20 28 61 6c 6c 20 65 78 63 65 70 74 20 70 72 6f   (all except pro
c6980 78 79 20 6c 6f 63 6b 69 6e 67 29 3a 0a 2a 2a 20  xy locking):.** 
c6990 20 20 20 20 20 2b 20 66 6f 72 20 50 6f 73 69 78       + for Posix
c69a0 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b 73 0a   Advisory Locks.
c69b0 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 6e 6f  **      + for no
c69c0 2d 6f 70 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20 20  -op locks.**    
c69d0 20 20 2b 20 66 6f 72 20 64 6f 74 2d 66 69 6c 65    + for dot-file
c69e0 20 6c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20 2b   locks.**      +
c69f0 20 66 6f 72 20 66 6c 6f 63 6b 28 29 20 6c 6f 63   for flock() loc
c6a00 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 2b 20 66  king.**      + f
c6a10 6f 72 20 6e 61 6d 65 64 20 73 65 6d 61 70 68 6f  or named semapho
c6a20 72 65 20 6c 6f 63 6b 73 20 28 56 78 57 6f 72 6b  re locks (VxWork
c6a30 73 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 20 20 20  s only).**      
c6a40 2b 20 66 6f 72 20 41 46 50 20 66 69 6c 65 73 79  + for AFP filesy
c6a50 73 74 65 6d 20 6c 6f 63 6b 73 20 28 4d 61 63 4f  stem locks (MacO
c6a60 53 58 20 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20  SX only).**   * 
c6a70 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65   sqlite3_file me
c6a80 74 68 6f 64 73 20 6e 6f 74 20 61 73 73 6f 63 69  thods not associ
c6a90 61 74 65 64 20 77 69 74 68 20 6c 6f 63 6b 69 6e  ated with lockin
c6aa0 67 2e 0a 2a 2a 20 20 20 2a 20 20 44 65 66 69 6e  g..**   *  Defin
c6ab0 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65  itions of sqlite
c6ac0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
c6ad0 65 63 74 73 20 66 6f 72 20 61 6c 6c 20 6c 6f 63  ects for all loc
c6ae0 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74  king.**      met
c6af0 68 6f 64 73 20 70 6c 75 73 20 22 66 69 6e 64 65  hods plus "finde
c6b00 72 22 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72  r" functions for
c6b10 20 65 61 63 68 20 6c 6f 63 6b 69 6e 67 20 6d 65   each locking me
c6b20 74 68 6f 64 2e 0a 2a 2a 20 20 20 2a 20 20 73 71  thod..**   *  sq
c6b30 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64  lite3_vfs method
c6b40 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
c6b50 2e 0a 2a 2a 20 20 20 2a 20 20 4c 6f 63 6b 69 6e  ..**   *  Lockin
c6b60 67 20 70 72 69 6d 69 74 69 76 65 73 20 66 6f 72  g primitives for
c6b70 20 74 68 65 20 70 72 6f 78 79 20 75 62 65 72 2d   the proxy uber-
c6b80 6c 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 2e 20  locking-method. 
c6b90 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a  (MacOSX only).**
c6ba0 20 20 20 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e     *  Definition
c6bb0 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 76 66 73  s of sqlite3_vfs
c6bc0 20 6f 62 6a 65 63 74 73 20 66 6f 72 20 61 6c 6c   objects for all
c6bd0 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73   locking methods
c6be0 0a 2a 2a 20 20 20 20 20 20 70 6c 75 73 20 69 6d  .**      plus im
c6bf0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
c6c00 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
c6c10 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f  () and sqlite3_o
c6c20 73 5f 65 6e 64 28 29 2e 0a 2a 2f 0a 23 69 66 20  s_end()..*/.#if 
c6c30 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 20  SQLITE_OS_UNIX  
c6c40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
c6c50 68 69 73 20 66 69 6c 65 20 69 73 20 75 73 65 64  his file is used
c6c60 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f   on unix only */
c6c70 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72  ../*.** There ar
c6c80 65 20 76 61 72 69 6f 75 73 20 6d 65 74 68 6f 64  e various method
c6c90 73 20 66 6f 72 20 66 69 6c 65 20 6c 6f 63 6b 69  s for file locki
c6ca0 6e 67 20 75 73 65 64 20 66 6f 72 20 63 6f 6e 63  ng used for conc
c6cb0 75 72 72 65 6e 63 79 0a 2a 2a 20 63 6f 6e 74 72  urrency.** contr
c6cc0 6f 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 50  ol:.**.**   1. P
c6cd0 4f 53 49 58 20 6c 6f 63 6b 69 6e 67 20 28 74 68  OSIX locking (th
c6ce0 65 20 64 65 66 61 75 6c 74 29 2c 0a 2a 2a 20 20  e default),.**  
c6cf0 20 32 2e 20 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a   2. No locking,.
c6d00 2a 2a 20 20 20 33 2e 20 44 6f 74 2d 66 69 6c 65  **   3. Dot-file
c6d10 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 34   locking,.**   4
c6d20 2e 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e  . flock() lockin
c6d30 67 2c 0a 2a 2a 20 20 20 35 2e 20 41 46 50 20 6c  g,.**   5. AFP l
c6d40 6f 63 6b 69 6e 67 20 28 4f 53 58 20 6f 6e 6c 79  ocking (OSX only
c6d50 29 2c 0a 2a 2a 20 20 20 36 2e 20 4e 61 6d 65 64  ),.**   6. Named
c6d60 20 50 4f 53 49 58 20 73 65 6d 61 70 68 6f 72 65   POSIX semaphore
c6d70 73 20 28 56 58 57 6f 72 6b 73 20 6f 6e 6c 79 29  s (VXWorks only)
c6d80 2c 0a 2a 2a 20 20 20 37 2e 20 70 72 6f 78 79 20  ,.**   7. proxy 
c6d90 6c 6f 63 6b 69 6e 67 2e 20 28 4f 53 58 20 6f 6e  locking. (OSX on
c6da0 6c 79 29 0a 2a 2a 0a 2a 2a 20 53 74 79 6c 65 73  ly).**.** Styles
c6db0 20 34 2c 20 35 2c 20 61 6e 64 20 37 20 61 72 65   4, 5, and 7 are
c6dc0 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
c6dd0 6f 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  of SQLITE_ENABLE
c6de0 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2a  _LOCKING_STYLE.*
c6df0 2a 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20  * is defined to 
c6e00 31 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f 45  1.  The SQLITE_E
c6e10 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
c6e20 59 4c 45 20 61 6c 73 6f 20 65 6e 61 62 6c 65 73  YLE also enables
c6e30 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20 73 65   automatic.** se
c6e40 6c 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 61  lection of the a
c6e50 70 70 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 69  ppropriate locki
c6e60 6e 67 20 73 74 79 6c 65 20 62 61 73 65 64 20 6f  ng style based o
c6e70 6e 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d  n the filesystem
c6e80 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 64 61  .** where the da
c6e90 74 61 62 61 73 65 20 69 73 20 6c 6f 63 61 74 65  tabase is locate
c6ea0 64 2e 20 20 0a 2a 2f 0a 23 69 66 20 21 64 65 66  d.  .*/.#if !def
c6eb0 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
c6ec0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
c6ed0 29 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64 28  ).#  if defined(
c6ee0 5f 5f 41 50 50 4c 45 5f 5f 29 0a 23 20 20 20 20  __APPLE__).#    
c6ef0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e  define SQLITE_EN
c6f00 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
c6f10 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a 23 20 20  LE 1.#  else.#  
c6f20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
c6f30 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
c6f40 54 59 4c 45 20 30 0a 23 20 20 65 6e 64 69 66 0a  TYLE 0.#  endif.
c6f50 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65  #endif../*.** De
c6f60 66 69 6e 65 20 74 68 65 20 4f 53 5f 56 58 57 4f  fine the OS_VXWO
c6f70 52 4b 53 20 70 72 65 2d 70 72 6f 63 65 73 73 6f  RKS pre-processo
c6f80 72 20 6d 61 63 72 6f 20 74 6f 20 31 20 69 66 20  r macro to 1 if 
c6f90 62 75 69 6c 64 69 6e 67 20 6f 6e 20 0a 2a 2a 20  building on .** 
c6fa0 76 78 77 6f 72 6b 73 2c 20 6f 72 20 30 20 6f 74  vxworks, or 0 ot
c6fb0 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 23 69 66 6e  herwise..*/.#ifn
c6fc0 64 65 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 23  def OS_VXWORKS.#
c6fd0 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 52    if defined(__R
c6fe0 54 50 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  TP__) || defined
c6ff0 28 5f 57 52 53 5f 4b 45 52 4e 45 4c 29 0a 23 20  (_WRS_KERNEL).# 
c7000 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 56 58 57     define OS_VXW
c7010 4f 52 4b 53 20 31 0a 23 20 20 65 6c 73 65 0a 23  ORKS 1.#  else.#
c7020 20 20 20 20 64 65 66 69 6e 65 20 4f 53 5f 56 58      define OS_VX
c7030 57 4f 52 4b 53 20 30 0a 23 20 20 65 6e 64 69 66  WORKS 0.#  endif
c7040 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
c7050 68 65 73 65 20 23 64 65 66 69 6e 65 73 20 73 68  hese #defines sh
c7060 6f 75 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47 42  ould enable >2GB
c7070 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e   file support on
c7080 20 50 6f 73 69 78 20 69 66 20 74 68 65 0a 2a 2a   Posix if the.**
c7090 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72   underlying oper
c70a0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 75 70  ating system sup
c70b0 70 6f 72 74 73 20 69 74 2e 20 20 49 66 20 74 68  ports it.  If th
c70c0 65 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61  e OS lacks.** la
c70d0 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
c70e0 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62  , these should b
c70f0 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20  e no-ops..**.** 
c7100 4c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f  Large file suppo
c7110 72 74 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c  rt can be disabl
c7120 65 64 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53  ed using the -DS
c7130 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46  QLITE_DISABLE_LF
c7140 53 20 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74  S switch.** on t
c7150 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d  he compiler comm
c7160 61 6e 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20  and line.  This 
c7170 69 73 20 6e 65 63 65 73 73 61 72 79 20 69 66 20  is necessary if 
c7180 79 6f 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e  you are compilin
c7190 67 0a 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74  g.** on a recent
c71a0 20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65   machine (ex: Re
c71b0 64 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f  dHat 7.2) but yo
c71c0 75 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65  u want your code
c71d0 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61   to work.** on a
c71e0 6e 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20  n older machine 
c71f0 28 65 78 3a 20 52 65 64 48 61 74 20 36 2e 30 29  (ex: RedHat 6.0)
c7200 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c  .  If you compil
c7210 65 20 6f 6e 20 52 65 64 48 61 74 20 37 2e 32 0a  e on RedHat 7.2.
c7220 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73 20  ** without this 
c7230 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65  option, LFS is e
c7240 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20  nable.  But LFS 
c7250 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69  does not exist i
c7260 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20  n the kernel.** 
c7270 69 6e 20 52 65 64 48 61 74 20 36 2e 30 2c 20 73  in RedHat 6.0, s
c7280 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74  o the code won't
c7290 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66   work.  Hence, f
c72a0 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72  or maximum binar
c72b0 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79  y.** portability
c72c0 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74   you should omit
c72d0 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   LFS..**.** The 
c72e0 70 72 65 76 69 6f 75 73 20 70 61 72 61 67 72 61  previous paragra
c72f0 70 68 20 77 61 73 20 77 72 69 74 74 65 6e 20 69  ph was written i
c7300 6e 20 32 30 30 35 2e 20 20 28 54 68 69 73 20 70  n 2005.  (This p
c7310 61 72 61 67 72 61 70 68 20 69 73 20 77 72 69 74  aragraph is writ
c7320 74 65 6e 0a 2a 2a 20 6f 6e 20 32 30 30 38 2d 31  ten.** on 2008-1
c7330 31 2d 32 38 2e 29 20 54 68 65 73 65 20 64 61 79  1-28.) These day
c7340 73 2c 20 61 6c 6c 20 4c 69 6e 75 78 20 6b 65 72  s, all Linux ker
c7350 6e 65 6c 73 20 73 75 70 70 6f 72 74 20 6c 61 72  nels support lar
c7360 67 65 20 66 69 6c 65 73 2c 20 73 6f 0a 2a 2a 20  ge files, so.** 
c7370 79 6f 75 20 73 68 6f 75 6c 64 20 70 72 6f 62 61  you should proba
c7380 62 6c 79 20 6c 65 61 76 65 20 4c 46 53 20 65 6e  bly leave LFS en
c7390 61 62 6c 65 64 2e 20 20 42 75 74 20 73 6f 6d 65  abled.  But some
c73a0 20 65 6d 62 65 64 64 65 64 20 70 6c 61 74 66 6f   embedded platfo
c73b0 72 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 6c 61 63  rms might.** lac
c73c0 6b 20 4c 46 53 20 69 6e 20 77 68 69 63 68 20 63  k LFS in which c
c73d0 61 73 65 20 74 68 65 20 53 51 4c 49 54 45 5f 44  ase the SQLITE_D
c73e0 49 53 41 42 4c 45 5f 4c 46 53 20 6d 61 63 72 6f  ISABLE_LFS macro
c73f0 20 6d 69 67 68 74 20 73 74 69 6c 6c 20 62 65 20   might still be 
c7400 75 73 65 66 75 6c 2e 0a 2a 2f 0a 23 69 66 6e 64  useful..*/.#ifnd
c7410 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c  ef SQLITE_DISABL
c7420 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65 20 5f  E_LFS.# define _
c7430 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20 20 20  LARGE_FILE      
c7440 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46 49 4c   1.# ifndef _FIL
c7450 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a 23 20  E_OFFSET_BITS.# 
c7460 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45 5f 4f    define _FILE_O
c7470 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a 23 20  FFSET_BITS 64.# 
c7480 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 5f  endif.# define _
c7490 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52 43 45  LARGEFILE_SOURCE
c74a0 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   1.#endif../*.**
c74b0 20 73 74 61 6e 64 61 72 64 20 69 6e 63 6c 75 64   standard includ
c74c0 65 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63  e files..*/.#inc
c74d0 6c 75 64 65 20 3c 73 79 73 2f 74 79 70 65 73 2e  lude <sys/types.
c74e0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73  h>.#include <sys
c74f0 2f 73 74 61 74 2e 68 3e 0a 23 69 6e 63 6c 75 64  /stat.h>.#includ
c7500 65 20 3c 66 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63  e <fcntl.h>.#inc
c7510 6c 75 64 65 20 3c 75 6e 69 73 74 64 2e 68 3e 0a  lude <unistd.h>.
c7520 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74 69  #include <sys/ti
c7530 6d 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  me.h>.#include <
c7540 65 72 72 6e 6f 2e 68 3e 0a 0a 23 69 66 20 53 51  errno.h>..#if SQ
c7550 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
c7560 49 4e 47 5f 53 54 59 4c 45 0a 23 20 69 6e 63 6c  ING_STYLE.# incl
c7570 75 64 65 20 3c 73 79 73 2f 69 6f 63 74 6c 2e 68  ude <sys/ioctl.h
c7580 3e 0a 23 20 69 66 20 4f 53 5f 56 58 57 4f 52 4b  >.# if OS_VXWORK
c7590 53 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 65  S.#  include <se
c75a0 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20 20 69 6e  maphore.h>.#  in
c75b0 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73 2e 68 3e  clude <limits.h>
c75c0 0a 23 20 65 6c 73 65 0a 23 20 20 69 6e 63 6c 75  .# else.#  inclu
c75d0 64 65 20 3c 73 79 73 2f 66 69 6c 65 2e 68 3e 0a  de <sys/file.h>.
c75e0 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f  #  include <sys/
c75f0 70 61 72 61 6d 2e 68 3e 0a 23 20 20 69 6e 63 6c  param.h>.#  incl
c7600 75 64 65 20 3c 73 79 73 2f 6d 6f 75 6e 74 2e 68  ude <sys/mount.h
c7610 3e 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  >.# endif.#endif
c7620 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   /* SQLITE_ENABL
c7630 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
c7640 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  */../*.** If we 
c7650 61 72 65 20 74 6f 20 62 65 20 74 68 72 65 61 64  are to be thread
c7660 2d 73 61 66 65 2c 20 69 6e 63 6c 75 64 65 20 74  -safe, include t
c7670 68 65 20 70 74 68 72 65 61 64 73 20 68 65 61 64  he pthreads head
c7680 65 72 20 61 6e 64 20 64 65 66 69 6e 65 0a 2a 2a  er and define.**
c7690 20 74 68 65 20 53 51 4c 49 54 45 5f 55 4e 49 58   the SQLITE_UNIX
c76a0 5f 54 48 52 45 41 44 53 20 6d 61 63 72 6f 2e 0a  _THREADS macro..
c76b0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
c76c0 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e  READSAFE.# defin
c76d0 65 20 53 51 4c 49 54 45 5f 55 4e 49 58 5f 54 48  e SQLITE_UNIX_TH
c76e0 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a  READS 1.#endif..
c76f0 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 70 65  /*.** Default pe
c7700 72 6d 69 73 73 69 6f 6e 73 20 77 68 65 6e 20 63  rmissions when c
c7710 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 66 69  reating a new fi
c7720 6c 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  le.*/.#ifndef SQ
c7730 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c  LITE_DEFAULT_FIL
c7740 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20  E_PERMISSIONS.# 
c7750 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
c7760 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
c7770 53 53 49 4f 4e 53 20 30 36 34 34 0a 23 65 6e 64  SSIONS 0644.#end
c7780 69 66 0a 0a 2f 2a 0a 20 2a 2a 20 44 65 66 61 75  if../*. ** Defau
c7790 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 77  lt permissions w
c77a0 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61 75 74  hen creating aut
c77b0 6f 20 70 72 6f 78 79 20 64 69 72 0a 20 2a 2f 0a  o proxy dir. */.
c77c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44  #ifndef SQLITE_D
c77d0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f  EFAULT_PROXYDIR_
c77e0 50 45 52 4d 49 53 53 49 4f 4e 53 0a 23 20 64 65  PERMISSIONS.# de
c77f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
c7800 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50 45 52  ULT_PROXYDIR_PER
c7810 4d 49 53 53 49 4f 4e 53 20 30 37 35 35 0a 23 65  MISSIONS 0755.#e
c7820 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69  ndif../*.** Maxi
c7830 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20 70 61  mum supported pa
c7840 74 68 2d 6c 65 6e 67 74 68 2e 0a 2a 2f 0a 23 64  th-length..*/.#d
c7850 65 66 69 6e 65 20 4d 41 58 5f 50 41 54 48 4e 41  efine MAX_PATHNA
c7860 4d 45 20 35 31 32 0a 0a 2f 2a 0a 2a 2a 20 4f 6e  ME 512../*.** On
c7870 6c 79 20 73 65 74 20 74 68 65 20 6c 61 73 74 45  ly set the lastE
c7880 72 72 6e 6f 20 69 66 20 74 68 65 20 65 72 72 6f  rrno if the erro
c7890 72 20 63 6f 64 65 20 69 73 20 61 20 72 65 61 6c  r code is a real
c78a0 20 65 72 72 6f 72 20 61 6e 64 20 6e 6f 74 20 0a   error and not .
c78b0 2a 2a 20 61 20 6e 6f 72 6d 61 6c 20 65 78 70 65  ** a normal expe
c78c0 63 74 65 64 20 72 65 74 75 72 6e 20 63 6f 64 65  cted return code
c78d0 20 6f 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20   of SQLITE_BUSY 
c78e0 6f 72 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a  or SQLITE_OK.*/.
c78f0 23 64 65 66 69 6e 65 20 49 53 5f 4c 4f 43 4b 5f  #define IS_LOCK_
c7900 45 52 52 4f 52 28 78 29 20 20 28 28 78 20 21 3d  ERROR(x)  ((x !=
c7910 20 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 28   SQLITE_OK) && (
c7920 78 20 21 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  x != SQLITE_BUSY
c7930 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74  )).../*.** Somet
c7940 69 6d 65 73 2c 20 61 66 74 65 72 20 61 20 66 69  imes, after a fi
c7950 6c 65 20 68 61 6e 64 6c 65 20 69 73 20 63 6c 6f  le handle is clo
c7960 73 65 64 20 62 79 20 53 51 4c 69 74 65 2c 20 74  sed by SQLite, t
c7970 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
c7980 6f 72 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20  or.** cannot be 
c7990 63 6c 6f 73 65 64 20 69 6d 6d 65 64 69 61 74 65  closed immediate
c79a0 6c 79 2e 20 49 6e 20 74 68 65 73 65 20 63 61 73  ly. In these cas
c79b0 65 73 2c 20 69 6e 73 74 61 6e 63 65 73 20 6f 66  es, instances of
c79c0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   the following.*
c79d0 2a 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  * structure are 
c79e0 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  used to store th
c79f0 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
c7a00 72 20 77 68 69 6c 65 20 77 61 69 74 69 6e 67 20  r while waiting 
c7a10 66 6f 72 20 61 6e 0a 2a 2a 20 6f 70 70 6f 72 74  for an.** opport
c7a20 75 6e 69 74 79 20 74 6f 20 65 69 74 68 65 72 20  unity to either 
c7a30 63 6c 6f 73 65 20 6f 72 20 72 65 75 73 65 20 69  close or reuse i
c7a40 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  t..*/.typedef st
c7a50 72 75 63 74 20 55 6e 69 78 55 6e 75 73 65 64 46  ruct UnixUnusedF
c7a60 64 20 55 6e 69 78 55 6e 75 73 65 64 46 64 3b 0a  d UnixUnusedFd;.
c7a70 73 74 72 75 63 74 20 55 6e 69 78 55 6e 75 73 65  struct UnixUnuse
c7a80 64 46 64 20 7b 0a 20 20 69 6e 74 20 66 64 3b 20  dFd {.  int fd; 
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 74 6f 20 63 6c 6f 73 65 20 2a 2f  ptor to close */
c7ac0 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20  .  int flags;   
c7ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c7ae0 46 6c 61 67 73 20 74 68 69 73 20 66 69 6c 65 20  Flags this file 
c7af0 64 65 73 63 72 69 70 74 6f 72 20 77 61 73 20 6f  descriptor was o
c7b00 70 65 6e 65 64 20 77 69 74 68 20 2a 2f 0a 20 20  pened with */.  
c7b10 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 4e  UnixUnusedFd *pN
c7b20 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ext;      /* Nex
c7b30 74 20 75 6e 75 73 65 64 20 66 69 6c 65 20 64 65  t unused file de
c7b40 73 63 72 69 70 74 6f 72 20 6f 6e 20 73 61 6d 65  scriptor on same
c7b50 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   file */.};../*.
c7b60 2a 2a 20 54 68 65 20 75 6e 69 78 46 69 6c 65 20  ** The unixFile 
c7b70 73 74 72 75 63 74 75 72 65 20 69 73 20 73 75 62  structure is sub
c7b80 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33  class of sqlite3
c7b90 5f 66 69 6c 65 20 73 70 65 63 69 66 69 63 20 74  _file specific t
c7ba0 6f 20 74 68 65 20 75 6e 69 78 0a 2a 2a 20 56 46  o the unix.** VF
c7bb0 53 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  S implementation
c7bc0 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
c7bd0 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20 75 6e  ruct unixFile un
c7be0 69 78 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 75  ixFile;.struct u
c7bf0 6e 69 78 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69  nixFile {.  sqli
c7c00 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 63  te3_io_methods c
c7c10 6f 6e 73 74 20 2a 70 4d 65 74 68 6f 64 3b 20 20  onst *pMethod;  
c7c20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 69  /* Always the fi
c7c30 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73  rst entry */.  s
c7c40 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
c7c50 74 20 2a 70 4f 70 65 6e 3b 20 20 20 20 20 20 20  t *pOpen;       
c7c60 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 20 61 6c  /* Info about al
c7c70 6c 20 6f 70 65 6e 20 66 64 27 73 20 6f 6e 20 74  l open fd's on t
c7c80 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 73  his inode */.  s
c7c90 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e  truct unixLockIn
c7ca0 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20 20 20 20 20  fo *pLock;      
c7cb0 2f 2a 20 49 6e 66 6f 20 61 62 6f 75 74 20 6c 6f  /* Info about lo
c7cc0 63 6b 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64  cks on this inod
c7cd0 65 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20  e */.  int h;   
c7ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7cf0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
c7d00 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a  ile descriptor *
c7d10 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 3b 20 20  /.  int dirfd;  
c7d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7d30 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65 73       /* File des
c7d40 63 72 69 70 74 6f 72 20 66 6f 72 20 74 68 65 20  criptor for the 
c7d50 64 69 72 65 63 74 6f 72 79 20 2a 2f 0a 20 20 75  directory */.  u
c7d60 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63  nsigned char loc
c7d70 6b 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20  ktype;          
c7d80 2f 2a 20 54 68 65 20 74 79 70 65 20 6f 66 20 6c  /* The type of l
c7d90 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 69 73  ock held on this
c7da0 20 66 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 73   fd */.  int las
c7db0 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20 20 20  tErrno;         
c7dc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
c7dd0 20 75 6e 69 78 20 65 72 72 6e 6f 20 66 72 6f 6d   unix errno from
c7de0 20 74 68 65 20 6c 61 73 74 20 49 2f 4f 20 65 72   the last I/O er
c7df0 72 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6c  ror */.  void *l
c7e00 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20  ockingContext;  
c7e10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63            /* Loc
c7e20 6b 69 6e 67 20 73 74 79 6c 65 20 73 70 65 63 69  king style speci
c7e30 66 69 63 20 73 74 61 74 65 20 2a 2f 0a 20 20 55  fic state */.  U
c7e40 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e  nixUnusedFd *pUn
c7e50 75 73 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  used;           
c7e60 2f 2a 20 50 72 65 2d 61 6c 6c 6f 63 61 74 65 64  /* Pre-allocated
c7e70 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 2f   UnixUnusedFd */
c7e80 0a 20 20 69 6e 74 20 66 69 6c 65 46 6c 61 67 73  .  int fileFlags
c7e90 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c7ea0 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e      /* Miscellan
c7eb0 6f 75 73 20 66 6c 61 67 73 20 2a 2f 0a 23 69 66  ous flags */.#if
c7ec0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
c7ed0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 69  OCKING_STYLE.  i
c7ee0 6e 74 20 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20  nt openFlags;   
c7ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7f00 2f 2a 20 54 68 65 20 66 6c 61 67 73 20 73 70 65  /* The flags spe
c7f10 63 69 66 69 65 64 20 61 74 20 6f 70 65 6e 28 29  cified at open()
c7f20 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20 53   */.#endif.#if S
c7f30 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
c7f40 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69   && defined(__li
c7f50 6e 75 78 5f 5f 29 0a 20 20 70 74 68 72 65 61 64  nux__).  pthread
c7f60 5f 74 20 74 69 64 3b 20 20 20 20 20 20 20 20 20  _t tid;         
c7f70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
c7f80 20 74 68 72 65 61 64 20 74 68 61 74 20 22 6f 77   thread that "ow
c7f90 6e 73 22 20 74 68 69 73 20 75 6e 69 78 46 69 6c  ns" this unixFil
c7fa0 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 20  e */.#endif.#if 
c7fb0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 6e 74  OS_VXWORKS.  int
c7fc0 20 69 73 44 65 6c 65 74 65 3b 20 20 20 20 20 20   isDelete;      
c7fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c7fe0 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65   Delete on close
c7ff0 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 73 74   if true */.  st
c8000 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
c8010 49 64 20 2a 70 49 64 3b 20 20 20 20 20 20 20 2f  Id *pId;       /
c8020 2a 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44  * Unique file ID
c8030 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64   */.#endif.#ifnd
c8040 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54  ef NDEBUG.  /* T
c8050 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66  he next group of
c8060 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 75   variables are u
c8070 73 65 64 20 74 6f 20 74 72 61 63 6b 20 77 68 65  sed to track whe
c8080 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a  ther or not the.
c8090 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
c80a0 20 63 6f 75 6e 74 65 72 20 69 6e 20 62 79 74 65   counter in byte
c80b0 73 20 32 34 2d 32 37 20 6f 66 20 64 61 74 61 62  s 24-27 of datab
c80c0 61 73 65 20 66 69 6c 65 73 20 61 72 65 20 75 70  ase files are up
c80d0 64 61 74 65 64 0a 20 20 2a 2a 20 77 68 65 6e 65  dated.  ** whene
c80e0 76 65 72 20 61 6e 79 20 70 61 72 74 20 6f 66 20  ver any part of 
c80f0 74 68 65 20 64 61 74 61 62 61 73 65 20 63 68 61  the database cha
c8100 6e 67 65 73 2e 20 20 41 6e 20 61 73 73 65 72 74  nges.  An assert
c8110 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 0a 20  ion fault will. 
c8120 20 2a 2a 20 6f 63 63 75 72 20 69 66 20 61 20 66   ** occur if a f
c8130 69 6c 65 20 69 73 20 75 70 64 61 74 65 64 20 77  ile is updated w
c8140 69 74 68 6f 75 74 20 61 6c 73 6f 20 75 70 64 61  ithout also upda
c8150 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63  ting the transac
c8160 74 69 6f 6e 0a 20 20 2a 2a 20 63 6f 75 6e 74 65  tion.  ** counte
c8170 72 2e 20 20 54 68 69 73 20 74 65 73 74 20 69 73  r.  This test is
c8180 20 6d 61 64 65 20 74 6f 20 61 76 6f 69 64 20 6e   made to avoid n
c8190 65 77 20 70 72 6f 62 6c 65 6d 73 20 73 69 6d 69  ew problems simi
c81a0 6c 61 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20  lar to the.  ** 
c81b0 6f 6e 65 20 64 65 73 63 72 69 62 65 64 20 62 79  one described by
c81c0 20 74 69 63 6b 65 74 20 23 33 35 38 34 2e 20 0a   ticket #3584. .
c81d0 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20    */.  unsigned 
c81e0 63 68 61 72 20 74 72 61 6e 73 43 6e 74 72 43 68  char transCntrCh
c81f0 6e 67 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ng;   /* True if
c8200 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
c8210 20 63 6f 75 6e 74 65 72 20 63 68 61 6e 67 65 64   counter changed
c8220 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
c8230 68 61 72 20 64 62 55 70 64 61 74 65 3b 20 20 20  har dbUpdate;   
c8240 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
c8250 61 6e 79 20 70 61 72 74 20 6f 66 20 64 61 74 61  any part of data
c8260 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65  base file change
c8270 64 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  d */.  unsigned 
c8280 63 68 61 72 20 69 6e 4e 6f 72 6d 61 6c 57 72 69  char inNormalWri
c8290 74 65 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66  te;   /* True if
c82a0 20 69 6e 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69   in a normal wri
c82b0 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  te operation */.
c82c0 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
c82d0 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49  LITE_TEST.  /* I
c82e0 6e 20 74 65 73 74 20 6d 6f 64 65 2c 20 69 6e 63  n test mode, inc
c82f0 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f  rease the size o
c8300 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
c8310 20 61 20 62 69 74 20 73 6f 20 74 68 61 74 20 0a   a bit so that .
c8320 20 20 2a 2a 20 69 74 20 69 73 20 6c 61 72 67 65    ** it is large
c8330 72 20 74 68 61 6e 20 74 68 65 20 73 74 72 75 63  r than the struc
c8340 74 20 43 72 61 73 68 46 69 6c 65 20 64 65 66 69  t CrashFile defi
c8350 6e 65 64 20 69 6e 20 74 65 73 74 36 2e 63 2e 0a  ned in test6.c..
c8360 20 20 2a 2f 0a 20 20 63 68 61 72 20 61 50 61 64    */.  char aPad
c8370 64 69 6e 67 5b 33 32 5d 3b 0a 23 65 6e 64 69 66  ding[32];.#endif
c8380 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  .};../*.** The f
c8390 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20  ollowing macros 
c83a0 64 65 66 69 6e 65 20 62 69 74 73 20 69 6e 20 75  define bits in u
c83b0 6e 69 78 46 69 6c 65 2e 66 69 6c 65 46 6c 61 67  nixFile.fileFlag
c83c0 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s.*/.#define SQL
c83d0 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c  ITE_WHOLE_FILE_L
c83e0 4f 43 4b 49 4e 47 20 20 30 78 30 30 30 31 20 20  OCKING  0x0001  
c83f0 20 2f 2a 20 55 73 65 20 77 68 6f 6c 65 2d 66 69   /* Use whole-fi
c8400 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 2f  le locking */../
c8410 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64  *.** Include cod
c8420 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e  e that is common
c8430 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66   to all os_*.c f
c8440 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  iles.*/./*******
c8450 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
c8460 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74  os_common.h in t
c8470 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f  he middle of os_
c8480 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  unix.c *********
c8490 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
c84a0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
c84b0 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a  le os_common.h *
c84c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c84d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c84e0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
c84f0 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20  04 May 22.**.** 
c8500 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
c8510 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
c8520 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
c8530 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
c8540 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
c8550 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
c8560 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
c8570 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
c8580 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
c8590 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
c85a0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
c85b0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
c85c0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
c85d0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
c85e0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
c85f0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
c8600 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
c8610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
c8660 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
c8670 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e  ntains macros an
c8680 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f  d a little bit o
c8690 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63  f code that is c
c86a0 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20  ommon to.** all 
c86b0 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d  of the platform-
c86c0 73 70 65 63 69 66 69 63 20 66 69 6c 65 73 20 28  specific files (
c86d0 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23  os_*.c) and is #
c86e0 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68  included into th
c86f0 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a  ose.** files..**
c8700 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68  .** This file sh
c8710 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65  ould be #include
c8720 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20  d by the os_*.c 
c8730 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20  files only.  It 
c8740 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65  is not a.** gene
c8750 72 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64  ral purpose head
c8760 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e  er file..*/.#ifn
c8770 64 65 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  def _OS_COMMON_H
c8780 5f 0a 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f  _.#define _OS_CO
c8790 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41  MMON_H_../*.** A
c87a0 74 20 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73  t least two bugs
c87b0 20 68 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e   have slipped in
c87c0 20 62 65 63 61 75 73 65 20 77 65 20 63 68 61 6e   because we chan
c87d0 67 65 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44  ged the MEMORY_D
c87e0 45 42 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f  EBUG.** macro to
c87f0 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e   SQLITE_DEBUG an
c8800 64 20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b  d some older mak
c8810 65 66 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20  efiles have not 
c8820 79 65 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20  yet made the.** 
c8830 73 77 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c  switch.  The fol
c8840 6c 6f 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75  lowing code shou
c8850 6c 64 20 63 61 74 63 68 20 74 68 69 73 20 70 72  ld catch this pr
c8860 6f 62 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65  oblem at compile
c8870 2d 74 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  -time..*/.#ifdef
c8880 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20   MEMORY_DEBUG.# 
c8890 65 72 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52  error "The MEMOR
c88a0 59 5f 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73  Y_DEBUG macro is
c88b0 20 6f 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20   obsolete.  Use 
c88c0 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73  SQLITE_DEBUG ins
c88d0 74 65 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23  tead.".#endif..#
c88e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
c88f0 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
c8900 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54  E int sqlite3OST
c8910 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e  race = 0;.#defin
c8920 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 20  e OSTRACE1(X)   
c8930 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
c8940 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
c8950 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 29  e3DebugPrintf(X)
c8960 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
c8970 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 28  2(X,Y)       if(
c8980 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20   sqlite3OSTrace 
c8990 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
c89a0 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e  intf(X,Y).#defin
c89b0 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a  e OSTRACE3(X,Y,Z
c89c0 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  )     if( sqlite
c89d0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
c89e0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
c89f0 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Y,Z).#define OST
c8a00 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20  RACE4(X,Y,Z,A)  
c8a10 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72   if( sqlite3OSTr
c8a20 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62  ace ) sqlite3Deb
c8a30 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
c8a40 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
c8a50 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66  E5(X,Y,Z,A,B) if
c8a60 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
c8a70 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
c8a80 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29  rintf(X,Y,Z,A,B)
c8a90 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
c8aa0 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c  6(X,Y,Z,A,B,C) \
c8ab0 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f  .    if(sqlite3O
c8ac0 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44  STrace) sqlite3D
c8ad0 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
c8ae0 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20  ,A,B,C).#define 
c8af0 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41  OSTRACE7(X,Y,Z,A
c8b00 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66  ,B,C,D) \.    if
c8b10 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29  (sqlite3OSTrace)
c8b20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
c8b30 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c  ntf(X,Y,Z,A,B,C,
c8b40 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  D).#else.#define
c8b50 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 65   OSTRACE1(X).#de
c8b60 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c  fine OSTRACE2(X,
c8b70 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Y).#define OSTRA
c8b80 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69  CE3(X,Y,Z).#defi
c8b90 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c  ne OSTRACE4(X,Y,
c8ba0 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  Z,A).#define OST
c8bb0 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29  RACE5(X,Y,Z,A,B)
c8bc0 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
c8bd0 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23  6(X,Y,Z,A,B,C).#
c8be0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28  define OSTRACE7(
c8bf0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23  X,Y,Z,A,B,C,D).#
c8c00 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63  endif../*.** Mac
c8c10 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61  ros for performa
c8c20 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f  nce tracing.  No
c8c30 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66  rmally turned of
c8c40 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a  f.  Only works.*
c8c50 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61  * on i486 hardwa
c8c60 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  re..*/.#ifdef SQ
c8c70 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45  LITE_PERFORMANCE
c8c80 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68  _TRACE../* .** h
c8c90 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73  wtime.h contains
c8ca0 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65   inline assemble
c8cb0 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65  r code for imple
c8cc0 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68  menting .** high
c8cd0 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d  -performance tim
c8ce0 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f  ing routines..*/
c8cf0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c8d00 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e   Include hwtime.
c8d10 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
c8d20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a  of os_common.h *
c8d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c8d40 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c8d50 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69   Begin file hwti
c8d60 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  me.h ***********
c8d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
c8d90 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20  ./*.** 2008 May 
c8da0 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  27.**.** The aut
c8db0 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
c8dc0 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
c8dd0 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
c8de0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
c8df0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
c8e00 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
c8e10 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
c8e20 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
c8e30 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
c8e40 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
c8e50 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
c8e60 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
c8e70 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
c8e80 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
c8e90 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
c8ea0 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
c8eb0 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
c8ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8f00 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
c8f10 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
c8f20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20  inline asm code 
c8f30 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22  for retrieving "
c8f40 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65  high-performance
c8f50 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f  ".** counters fo
c8f60 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73  r x86 class CPUs
c8f70 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57  ..*/.#ifndef _HW
c8f80 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20  TIME_H_.#define 
c8f90 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a  _HWTIME_H_../*.*
c8fa0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
c8fb0 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72  routine only wor
c8fc0 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c  ks on pentium-cl
c8fd0 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70  ass (or newer) p
c8fe0 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74  rocessors..** It
c8ff0 20 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20   uses the RDTSC 
c9000 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74  opcode to read t
c9010 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76  he cycle count v
c9020 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a  alue out of the.
c9030 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64  ** processor and
c9040 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61   returns that va
c9050 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62  lue.  This can b
c9060 65 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d  e used for high-
c9070 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67  res.** profiling
c9080 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65  ..*/.#if (define
c9090 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64  d(__GNUC__) || d
c90a0 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29  efined(_MSC_VER)
c90b0 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65  ) && \.      (de
c90c0 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64  fined(i386) || d
c90d0 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29  efined(__i386__)
c90e0 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49   || defined(_M_I
c90f0 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66  X86))..  #if def
c9100 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a  ined(__GNUC__)..
c9110 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
c9120 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
c9130 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
c9140 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e       unsigned in
c9150 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f  t lo, hi;.     _
c9160 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
c9170 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22  e__ ("rdtsc" : "
c9180 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28  =a" (lo), "=d" (
c9190 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72  hi));.     retur
c91a0 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  n (sqlite_uint64
c91b0 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a  )hi << 32 | lo;.
c91c0 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66    }..  #elif def
c91d0 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a  ined(_MSC_VER)..
c91e0 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b    __declspec(nak
c91f0 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c  ed) __inline sql
c9200 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65  ite_uint64 __cde
c9210 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  cl sqlite3Hwtime
c9220 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61  (void){.     __a
c9230 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74  sm {.        rdt
c9240 73 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20  sc.        ret  
c9250 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61       ; return va
c9260 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20  lue at EDX:EAX. 
c9270 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e      }.  }..  #en
c9280 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69  dif..#elif (defi
c9290 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26  ned(__GNUC__) &&
c92a0 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36   defined(__x86_6
c92b0 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e  4__))..  __inlin
c92c0 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  e__ sqlite_uint6
c92d0 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  4 sqlite3Hwtime(
c92e0 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73  void){.      uns
c92f0 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a  igned long val;.
c9300 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f        __asm__ __
c9310 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74  volatile__ ("rdt
c9320 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29  sc" : "=A" (val)
c9330 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
c9340 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66  val;.  }. .#elif
c9350 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
c9360 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f  __) && defined(_
c9370 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e  _ppc__))..  __in
c9380 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69  line__ sqlite_ui
c9390 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
c93a0 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20  me(void){.      
c93b0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
c93c0 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20  ng retval;.     
c93d0 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a   unsigned long j
c93e0 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d  unk;.      __asm
c93f0 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20  __ __volatile__ 
c9400 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  ("\n\.          
c9410 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20  1:      mftbu   
c9420 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %1\n\.          
c9430 20 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20          mftb    
c9440 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20  %L0\n\.         
c9450 20 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20           mftbu  
c9460 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20   %0\n\.         
c9470 20 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20           cmpw   
c9480 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20   %0,%1\n\.      
c9490 20 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20              bne 
c94a0 20 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20      1b".        
c94b0 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22            : "=r"
c94c0 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20   (retval), "=r" 
c94d0 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72  (junk));.      r
c94e0 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20  eturn retval;.  
c94f0 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72  }..#else..  #err
c9500 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e  or Need implemen
c9510 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  tation of sqlite
c9520 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f  3Hwtime() for yo
c9530 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20  ur platform...  
c9540 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69  /*.  ** To compi
c9550 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65  le without imple
c9560 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48  menting sqlite3H
c9570 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72  wtime() for your
c9580 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20   platform,.  ** 
c9590 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74  you can remove t
c95a0 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20  he above #error 
c95b0 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c  and use the foll
c95c0 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20  owing.  ** stub 
c95d0 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77  function.  You w
c95e0 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20  ill lose timing 
c95f0 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79  support for many
c9600 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62  .  ** of the deb
c9610 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69  ugging and testi
c9620 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75  ng utilities, bu
c9630 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20  t it should at. 
c9640 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c   ** least compil
c9650 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a  e and run..  */.
c9660 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
c9670 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73   sqlite_uint64 s
c9680 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
c9690 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c  d){ return ((sql
c96a0 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d  ite_uint64)0); }
c96b0 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ..#endif..#endif
c96c0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57   /* !defined(_HW
c96d0 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a  TIME_H_) */../**
c96e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
c96f0 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a   of hwtime.h ***
c9700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
c9730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
c9740 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
c9750 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f   left off in os_
c9760 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
c9770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74  ***********/..st
c9780 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74  atic sqlite_uint
c9790 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74  64 g_start;.stat
c97a0 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  ic sqlite_uint64
c97b0 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66   g_elapsed;.#def
c97c0 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20  ine TIMER_START 
c97d0 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71        g_start=sq
c97e0 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64  lite3Hwtime().#d
c97f0 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20  efine TIMER_END 
c9800 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65          g_elapse
c9810 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  d=sqlite3Hwtime(
c9820 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e  )-g_start.#defin
c9830 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20  e TIMER_ELAPSED 
c9840 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65      g_elapsed.#e
c9850 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  lse.#define TIME
c9860 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20  R_START.#define 
c9870 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e  TIMER_END.#defin
c9880 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20  e TIMER_ELAPSED 
c9890 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e      ((sqlite_uin
c98a0 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f  t64)0).#endif../
c98b0 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69  *.** If we compi
c98c0 6c 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  le with the SQLI
c98d0 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65  TE_TEST macro se
c98e0 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c  t, then the foll
c98f0 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f  owing block.** o
c9900 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65  f code will give
c9910 20 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20   us the ability 
c9920 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69  to simulate a di
c9930 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54  sk I/O error.  T
c9940 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66  his.** is used f
c9950 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49  or testing the I
c9960 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69  /O recovery logi
c9970 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  c..*/.#ifdef SQL
c9980 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
c9990 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
c99a0 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30  io_error_hit = 0
c99b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
c99c0 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
c99d0 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51  I/O Errors */.SQ
c99e0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
c99f0 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61  ite3_io_error_ha
c9a00 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20  rdhit = 0;      
c9a10 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e    /* Number of n
c9a20 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73  on-benign errors
c9a30 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
c9a40 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
c9a50 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b  ror_pending = 0;
c9a60 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74          /* Count
c9a70 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49   down to first I
c9a80 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49  /O error */.SQLI
c9a90 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c9aa0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73  e3_io_error_pers
c9ab0 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ist = 0;        
c9ac0 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65  /* True if I/O e
c9ad0 72 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f  rrors persist */
c9ae0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
c9af0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c9b00 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20  _benign = 0;    
c9b10 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
c9b20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67  errors are benig
c9b30 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  n */.SQLITE_API 
c9b40 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  int sqlite3_disk
c9b50 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30  full_pending = 0
c9b60 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
c9b70 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
c9b80 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53  l = 0;.#define S
c9b90 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
c9ba0 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f  nign(X) sqlite3_
c9bb0 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d  io_error_benign=
c9bc0 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  (X).#define Simu
c9bd0 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45  lateIOError(CODE
c9be0 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69  )  \.  if( (sqli
c9bf0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72  te3_io_error_per
c9c00 73 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f  sist && sqlite3_
c9c10 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a  io_error_hit) \.
c9c20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
c9c30 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
c9c40 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20  ng-- == 1 )  \. 
c9c50 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c               { l
c9c60 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f  ocal_ioerr(); CO
c9c70 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69  DE; }.static voi
c9c80 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b  d local_ioerr(){
c9c90 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45  .  IOTRACE(("IOE
c9ca0 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74  RR\n"));.  sqlit
c9cb0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b  e3_io_error_hit+
c9cc0 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  +;.  if( !sqlite
c9cd0 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67  3_io_error_benig
c9ce0 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  n ) sqlite3_io_e
c9cf0 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a  rror_hardhit++;.
c9d00 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  }.#define Simula
c9d10 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
c9d20 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73  CODE) \.   if( s
c9d30 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
c9d40 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20  pending ){ \.   
c9d50 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69    if( sqlite3_di
c9d60 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d  skfull_pending =
c9d70 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20  = 1 ){ \.       
c9d80 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c  local_ioerr(); \
c9d90 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .       sqlite3_
c9da0 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a  diskfull = 1; \.
c9db0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
c9dc0 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b  o_error_hit = 1;
c9dd0 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20   \.       CODE; 
c9de0 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a  \.     }else{ \.
c9df0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64         sqlite3_d
c9e00 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d  iskfull_pending-
c9e10 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20  -; \.     } \.  
c9e20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65   }.#else.#define
c9e30 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
c9e40 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e  Benign(X).#defin
c9e50 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
c9e60 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d  r(A).#define Sim
c9e70 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72  ulateDiskfullErr
c9e80 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  or(A).#endif../*
c9e90 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67  .** When testing
c9ea0 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f  , keep a count o
c9eb0 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
c9ec0 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23  open files..*/.#
c9ed0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
c9ee0 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
c9ef0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69   sqlite3_open_fi
c9f00 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64  le_count = 0;.#d
c9f10 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65  efine OpenCounte
c9f20 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70  r(X)  sqlite3_op
c9f30 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28  en_file_count+=(
c9f40 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65  X).#else.#define
c9f50 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a   OpenCounter(X).
c9f60 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
c9f70 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43  * !defined(_OS_C
c9f80 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a  OMMON_H_) */../*
c9f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
c9fa0 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  d of os_common.h
c9fb0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c9fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
c9fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
c9ff0 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
ca000 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73  e left off in os
ca010 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  _unix.c ********
ca020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
ca030 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 76 61 72 69  *.** Define vari
ca040 6f 75 73 20 6d 61 63 72 6f 73 20 74 68 61 74 20  ous macros that 
ca050 61 72 65 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d  are missing from
ca060 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 0a 2a   some systems..*
ca070 2f 0a 23 69 66 6e 64 65 66 20 4f 5f 4c 41 52 47  /.#ifndef O_LARG
ca080 45 46 49 4c 45 0a 23 20 64 65 66 69 6e 65 20 4f  EFILE.# define O
ca090 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a 23 65 6e  _LARGEFILE 0.#en
ca0a0 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
ca0b0 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20  E_DISABLE_LFS.# 
ca0c0 75 6e 64 65 66 20 4f 5f 4c 41 52 47 45 46 49 4c  undef O_LARGEFIL
ca0d0 45 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4c 41 52  E.# define O_LAR
ca0e0 47 45 46 49 4c 45 20 30 0a 23 65 6e 64 69 66 0a  GEFILE 0.#endif.
ca0f0 23 69 66 6e 64 65 66 20 4f 5f 4e 4f 46 4f 4c 4c  #ifndef O_NOFOLL
ca100 4f 57 0a 23 20 64 65 66 69 6e 65 20 4f 5f 4e 4f  OW.# define O_NO
ca110 46 4f 4c 4c 4f 57 20 30 0a 23 65 6e 64 69 66 0a  FOLLOW 0.#endif.
ca120 23 69 66 6e 64 65 66 20 4f 5f 42 49 4e 41 52 59  #ifndef O_BINARY
ca130 0a 23 20 64 65 66 69 6e 65 20 4f 5f 42 49 4e 41  .# define O_BINA
ca140 52 59 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  RY 0.#endif../*.
ca150 2a 2a 20 54 68 65 20 44 4a 47 50 50 20 63 6f 6d  ** The DJGPP com
ca160 70 69 6c 65 72 20 65 6e 76 69 72 6f 6e 6d 65 6e  piler environmen
ca170 74 20 6c 6f 6f 6b 73 20 6d 6f 73 74 6c 79 20 6c  t looks mostly l
ca180 69 6b 65 20 55 6e 69 78 2c 20 62 75 74 20 69 74  ike Unix, but it
ca190 0a 2a 2a 20 6c 61 63 6b 73 20 74 68 65 20 66 63  .** lacks the fc
ca1a0 6e 74 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c  ntl() system cal
ca1b0 6c 2e 20 20 53 6f 20 72 65 64 65 66 69 6e 65 20  l.  So redefine 
ca1c0 66 63 6e 74 6c 28 29 20 74 6f 20 62 65 20 73 6f  fcntl() to be so
ca1d0 6d 65 74 68 69 6e 67 0a 2a 2a 20 74 68 61 74 20  mething.** that 
ca1e0 61 6c 77 61 79 73 20 73 75 63 63 65 65 64 73 2e  always succeeds.
ca1f0 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61    This means tha
ca200 74 20 6c 6f 63 6b 69 6e 67 20 64 6f 65 73 20 6e  t locking does n
ca210 6f 74 20 6f 63 63 75 72 20 75 6e 64 65 72 0a 2a  ot occur under.*
ca220 2a 20 44 4a 47 50 50 2e 20 20 42 75 74 20 69 74  * DJGPP.  But it
ca230 20 69 73 20 44 4f 53 20 2d 20 77 68 61 74 20 64   is DOS - what d
ca240 69 64 20 79 6f 75 20 65 78 70 65 63 74 3f 0a 2a  id you expect?.*
ca250 2f 0a 23 69 66 64 65 66 20 5f 5f 44 4a 47 50 50  /.#ifdef __DJGPP
ca260 5f 5f 0a 23 20 64 65 66 69 6e 65 20 66 63 6e 74  __.# define fcnt
ca270 6c 28 41 2c 42 2c 43 29 20 30 0a 23 65 6e 64 69  l(A,B,C) 0.#endi
ca280 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 68 72  f../*.** The thr
ca290 65 61 64 69 64 20 6d 61 63 72 6f 20 72 65 73 6f  eadid macro reso
ca2a0 6c 76 65 73 20 74 6f 20 74 68 65 20 74 68 72 65  lves to the thre
ca2b0 61 64 2d 69 64 20 6f 72 20 74 6f 20 30 2e 20 20  ad-id or to 0.  
ca2c0 55 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74  Used for.** test
ca2d0 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
ca2e0 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 20 53  g only..*/.#if S
ca2f0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
ca300 0a 23 64 65 66 69 6e 65 20 74 68 72 65 61 64 69  .#define threadi
ca310 64 20 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29  d pthread_self()
ca320 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 74  .#else.#define t
ca330 68 72 65 61 64 69 64 20 30 0a 23 65 6e 64 69 66  hreadid 0.#endif
ca340 0a 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20  .../*.** Helper 
ca350 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 6f 62 74  functions to obt
ca360 61 69 6e 20 61 6e 64 20 72 65 6c 69 6e 71 75 69  ain and relinqui
ca370 73 68 20 74 68 65 20 67 6c 6f 62 61 6c 20 6d 75  sh the global mu
ca380 74 65 78 2e 20 54 68 65 0a 2a 2a 20 67 6c 6f 62  tex. The.** glob
ca390 61 6c 20 6d 75 74 65 78 20 69 73 20 75 73 65 64  al mutex is used
ca3a0 20 74 6f 20 70 72 6f 74 65 63 74 20 74 68 65 20   to protect the 
ca3b0 75 6e 69 78 4f 70 65 6e 43 6e 74 2c 20 75 6e 69  unixOpenCnt, uni
ca3c0 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 0a 2a 2a  xLockInfo and.**
ca3d0 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f   vxworksFileId o
ca3e0 62 6a 65 63 74 73 20 75 73 65 64 20 62 79 20 74  bjects used by t
ca3f0 68 69 73 20 66 69 6c 65 2c 20 61 6c 6c 20 6f 66  his file, all of
ca400 20 77 68 69 63 68 20 6d 61 79 20 62 65 20 0a 2a   which may be .*
ca410 2a 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c 74  * shared by mult
ca420 69 70 6c 65 20 74 68 72 65 61 64 73 2e 0a 2a 2a  iple threads..**
ca430 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 75 6e 69  .** Function uni
ca440 78 4d 75 74 65 78 48 65 6c 64 28 29 20 69 73 20  xMutexHeld() is 
ca450 75 73 65 64 20 74 6f 20 61 73 73 65 72 74 28 29  used to assert()
ca460 20 74 68 61 74 20 74 68 65 20 67 6c 6f 62 61 6c   that the global
ca470 20 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 68 65   mutex .** is he
ca480 6c 64 20 77 68 65 6e 20 72 65 71 75 69 72 65 64  ld when required
ca490 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
ca4a0 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 61 73 20  is only used as 
ca4b0 70 61 72 74 20 6f 66 20 61 73 73 65 72 74 28 29  part of assert()
ca4c0 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 2e   .** statements.
ca4d0 20 65 2e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 75 6e   e.g..**.**   un
ca4e0 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 0a 2a  ixEnterMutex().*
ca4f0 2a 20 20 20 20 20 61 73 73 65 72 74 28 20 75 6e  *     assert( un
ca500 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b  ixMutexHeld() );
ca510 0a 2a 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4c  .**   unixEnterL
ca520 65 61 76 65 28 29 0a 2a 2f 0a 73 74 61 74 69 63  eave().*/.static
ca530 20 76 6f 69 64 20 75 6e 69 78 45 6e 74 65 72 4d   void unixEnterM
ca540 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71  utex(void){.  sq
ca550 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
ca560 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  r(sqlite3MutexAl
ca570 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
ca580 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
ca590 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
ca5a0 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 76  unixLeaveMutex(v
ca5b0 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  oid){.  sqlite3_
ca5c0 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69  mutex_leave(sqli
ca5d0 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
ca5e0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
ca5f0 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 23 69  C_MASTER));.}.#i
ca600 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
ca610 47 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  G.static int uni
ca620 78 4d 75 74 65 78 48 65 6c 64 28 76 6f 69 64 29  xMutexHeld(void)
ca630 20 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69   {.  return sqli
ca640 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73  te3_mutex_held(s
ca650 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
ca660 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
ca670 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d  ATIC_MASTER));.}
ca680 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 65 66  .#endif...#ifdef
ca690 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a   SQLITE_DEBUG./*
ca6a0 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74  .** Helper funct
ca6b0 69 6f 6e 20 66 6f 72 20 70 72 69 6e 74 69 6e 67  ion for printing
ca6c0 20 6f 75 74 20 74 72 61 63 65 20 69 6e 66 6f 72   out trace infor
ca6d0 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 64 65 62 75  mation from debu
ca6e0 67 67 69 6e 67 0a 2a 2a 20 62 69 6e 61 72 69 65  gging.** binarie
ca6f0 73 2e 20 54 68 69 73 20 72 65 74 75 72 6e 73 20  s. This returns 
ca700 74 68 65 20 73 74 72 69 6e 67 20 72 65 70 72 65  the string repre
ca710 73 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  setation of the 
ca720 73 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6e 74 65  supplied.** inte
ca730 67 65 72 20 6c 6f 63 6b 2d 74 79 70 65 2e 0a 2a  ger lock-type..*
ca740 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
ca750 68 61 72 20 2a 6c 6f 63 6b 74 79 70 65 4e 61 6d  har *locktypeNam
ca760 65 28 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b  e(int locktype){
ca770 0a 20 20 73 77 69 74 63 68 28 20 6c 6f 63 6b 74  .  switch( lockt
ca780 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ype ){.    case 
ca790 4e 4f 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20  NO_LOCK: return 
ca7a0 22 4e 4f 4e 45 22 3b 0a 20 20 20 20 63 61 73 65  "NONE";.    case
ca7b0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3a 20 72 65   SHARED_LOCK: re
ca7c0 74 75 72 6e 20 22 53 48 41 52 45 44 22 3b 0a 20  turn "SHARED";. 
ca7d0 20 20 20 63 61 73 65 20 52 45 53 45 52 56 45 44     case RESERVED
ca7e0 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 52  _LOCK: return "R
ca7f0 45 53 45 52 56 45 44 22 3b 0a 20 20 20 20 63 61  ESERVED";.    ca
ca800 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3a  se PENDING_LOCK:
ca810 20 72 65 74 75 72 6e 20 22 50 45 4e 44 49 4e 47   return "PENDING
ca820 22 3b 0a 20 20 20 20 63 61 73 65 20 45 58 43 4c  ";.    case EXCL
ca830 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 74 75  USIVE_LOCK: retu
ca840 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22 3b 0a  rn "EXCLUSIVE";.
ca850 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 45 52    }.  return "ER
ca860 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ROR";.}.#endif..
ca870 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4c 4f  #ifdef SQLITE_LO
ca880 43 4b 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a 20 50  CK_TRACE./*.** P
ca890 72 69 6e 74 20 6f 75 74 20 69 6e 66 6f 72 6d 61  rint out informa
ca8a0 74 69 6f 6e 20 61 62 6f 75 74 20 61 6c 6c 20 6c  tion about all l
ca8b0 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ocking operation
ca8c0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
ca8d0 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
ca8e0 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e  r troubleshootin
ca8f0 67 20 6c 6f 63 6b 73 20 6f 6e 20 6d 75 6c 74 69  g locks on multi
ca900 74 68 72 65 61 64 65 64 0a 2a 2a 20 70 6c 61 74  threaded.** plat
ca910 66 6f 72 6d 73 2e 20 20 45 6e 61 62 6c 65 20 62  forms.  Enable b
ca920 79 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69 74 68  y compiling with
ca930 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 4c 4f   the -DSQLITE_LO
ca940 43 4b 5f 54 52 41 43 45 0a 2a 2a 20 63 6f 6d 6d  CK_TRACE.** comm
ca950 61 6e 64 2d 6c 69 6e 65 20 6f 70 74 69 6f 6e 20  and-line option 
ca960 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e  on the compiler.
ca970 20 20 54 68 69 73 20 63 6f 64 65 20 69 73 20 6e    This code is n
ca980 6f 72 6d 61 6c 6c 79 0a 2a 2a 20 74 75 72 6e 65  ormally.** turne
ca990 64 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74 69 63  d off..*/.static
ca9a0 20 69 6e 74 20 6c 6f 63 6b 54 72 61 63 65 28 69   int lockTrace(i
ca9b0 6e 74 20 66 64 2c 20 69 6e 74 20 6f 70 2c 20 73  nt fd, int op, s
ca9c0 74 72 75 63 74 20 66 6c 6f 63 6b 20 2a 70 29 7b  truct flock *p){
ca9d0 0a 20 20 63 68 61 72 20 2a 7a 4f 70 4e 61 6d 65  .  char *zOpName
ca9e0 2c 20 2a 7a 54 79 70 65 3b 0a 20 20 69 6e 74 20  , *zType;.  int 
ca9f0 73 3b 0a 20 20 69 6e 74 20 73 61 76 65 64 45 72  s;.  int savedEr
caa00 72 6e 6f 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 46  rno;.  if( op==F
caa10 5f 47 45 54 4c 4b 20 29 7b 0a 20 20 20 20 7a 4f  _GETLK ){.    zO
caa20 70 4e 61 6d 65 20 3d 20 22 47 45 54 4c 4b 22 3b  pName = "GETLK";
caa30 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d  .  }else if( op=
caa40 3d 46 5f 53 45 54 4c 4b 20 29 7b 0a 20 20 20 20  =F_SETLK ){.    
caa50 7a 4f 70 4e 61 6d 65 20 3d 20 22 53 45 54 4c 4b  zOpName = "SETLK
caa60 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ";.  }else{.    
caa70 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70  s = fcntl(fd, op
caa80 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , p);.    sqlite
caa90 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 66 63  3DebugPrintf("fc
caaa0 6e 74 6c 20 75 6e 6b 6e 6f 77 6e 20 25 64 20 25  ntl unknown %d %
caab0 64 20 25 64 5c 6e 22 2c 20 66 64 2c 20 6f 70 2c  d %d\n", fd, op,
caac0 20 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   s);.    return 
caad0 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  s;.  }.  if( p->
caae0 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20  l_type==F_RDLCK 
caaf0 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22  ){.    zType = "
cab00 52 44 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20  RDLCK";.  }else 
cab10 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46  if( p->l_type==F
cab20 5f 57 52 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54  _WRLCK ){.    zT
cab30 79 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20  ype = "WRLCK";. 
cab40 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f   }else if( p->l_
cab50 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b  type==F_UNLCK ){
cab60 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e  .    zType = "UN
cab70 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  LCK";.  }else{. 
cab80 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a     assert( 0 );.
cab90 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d    }.  assert( p-
caba0 3e 6c 5f 77 68 65 6e 63 65 3d 3d 53 45 45 4b 5f  >l_whence==SEEK_
cabb0 53 45 54 20 29 3b 0a 20 20 73 20 3d 20 66 63 6e  SET );.  s = fcn
cabc0 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20  tl(fd, op, p);. 
cabd0 20 73 61 76 65 64 45 72 72 6e 6f 20 3d 20 65 72   savedErrno = er
cabe0 72 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33 44 65  rno;.  sqlite3De
cabf0 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74 6c  bugPrintf("fcntl
cac00 20 25 64 20 25 64 20 25 73 20 25 73 20 25 64 20   %d %d %s %s %d 
cac10 25 64 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20  %d %d %d\n",.   
cac20 20 20 74 68 72 65 61 64 69 64 2c 20 66 64 2c 20    threadid, fd, 
cac30 7a 4f 70 4e 61 6d 65 2c 20 7a 54 79 70 65 2c 20  zOpName, zType, 
cac40 28 69 6e 74 29 70 2d 3e 6c 5f 73 74 61 72 74 2c  (int)p->l_start,
cac50 20 28 69 6e 74 29 70 2d 3e 6c 5f 6c 65 6e 2c 0a   (int)p->l_len,.
cac60 20 20 20 20 20 28 69 6e 74 29 70 2d 3e 6c 5f 70       (int)p->l_p
cac70 69 64 2c 20 73 29 3b 0a 20 20 69 66 28 20 73 3d  id, s);.  if( s=
cac80 3d 28 2d 31 29 20 26 26 20 6f 70 3d 3d 46 5f 53  =(-1) && op==F_S
cac90 45 54 4c 4b 20 26 26 20 28 70 2d 3e 6c 5f 74 79  ETLK && (p->l_ty
caca0 70 65 3d 3d 46 5f 52 44 4c 43 4b 20 7c 7c 20 70  pe==F_RDLCK || p
cacb0 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43  ->l_type==F_WRLC
cacc0 4b 29 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  K) ){.    struct
cacd0 20 66 6c 6f 63 6b 20 6c 32 3b 0a 20 20 20 20 6c   flock l2;.    l
cace0 32 20 3d 20 2a 70 3b 0a 20 20 20 20 66 63 6e 74  2 = *p;.    fcnt
cacf0 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26  l(fd, F_GETLK, &
cad00 6c 32 29 3b 0a 20 20 20 20 69 66 28 20 6c 32 2e  l2);.    if( l2.
cad10 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43 4b 20  l_type==F_RDLCK 
cad20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d  ){.      zType =
cad30 20 22 52 44 4c 43 4b 22 3b 0a 20 20 20 20 7d 65   "RDLCK";.    }e
cad40 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70  lse if( l2.l_typ
cad50 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a 20 20  e==F_WRLCK ){.  
cad60 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c      zType = "WRL
cad70 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  CK";.    }else i
cad80 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f  f( l2.l_type==F_
cad90 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a  UNLCK ){.      z
cada0 54 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a  Type = "UNLCK";.
cadb0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
cadc0 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20   assert( 0 );.  
cadd0 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
cade0 65 62 75 67 50 72 69 6e 74 66 28 22 66 63 6e 74  ebugPrintf("fcnt
cadf0 6c 2d 66 61 69 6c 75 72 65 2d 72 65 61 73 6f 6e  l-failure-reason
cae00 3a 20 25 73 20 25 64 20 25 64 20 25 64 5c 6e 22  : %s %d %d %d\n"
cae10 2c 0a 20 20 20 20 20 20 20 7a 54 79 70 65 2c 20  ,.       zType, 
cae20 28 69 6e 74 29 6c 32 2e 6c 5f 73 74 61 72 74 2c  (int)l2.l_start,
cae30 20 28 69 6e 74 29 6c 32 2e 6c 5f 6c 65 6e 2c 20   (int)l2.l_len, 
cae40 28 69 6e 74 29 6c 32 2e 6c 5f 70 69 64 29 3b 0a  (int)l2.l_pid);.
cae50 20 20 7d 0a 20 20 65 72 72 6e 6f 20 3d 20 73 61    }.  errno = sa
cae60 76 65 64 45 72 72 6e 6f 3b 0a 20 20 72 65 74 75  vedErrno;.  retu
cae70 72 6e 20 73 3b 0a 7d 0a 23 64 65 66 69 6e 65 20  rn s;.}.#define 
cae80 66 63 6e 74 6c 20 6c 6f 63 6b 54 72 61 63 65 0a  fcntl lockTrace.
cae90 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
caea0 5f 4c 4f 43 4b 5f 54 52 41 43 45 20 2a 2f 0a 0a  _LOCK_TRACE */..
caeb0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
caec0 74 69 6e 65 20 74 72 61 6e 73 6c 61 74 65 73 20  tine translates 
caed0 61 20 73 74 61 6e 64 61 72 64 20 50 4f 53 49 58  a standard POSIX
caee0 20 65 72 72 6e 6f 20 63 6f 64 65 20 69 6e 74 6f   errno code into
caef0 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 75 73   something.** us
caf00 65 66 75 6c 20 74 6f 20 74 68 65 20 63 6c 69 65  eful to the clie
caf10 6e 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74  nts of the sqlit
caf20 65 33 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 53  e3 functions.  S
caf30 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 69 74 20  pecifically, it 
caf40 69 73 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 74  is.** intended t
caf50 6f 20 74 72 61 6e 73 6c 61 74 65 20 61 20 76 61  o translate a va
caf60 72 69 65 74 79 20 6f 66 20 22 74 72 79 20 61 67  riety of "try ag
caf70 61 69 6e 22 20 65 72 72 6f 72 73 20 69 6e 74 6f  ain" errors into
caf80 20 53 51 4c 49 54 45 5f 42 55 53 59 0a 2a 2a 20   SQLITE_BUSY.** 
caf90 61 6e 64 20 61 20 76 61 72 69 65 74 79 20 6f 66  and a variety of
cafa0 20 22 70 6c 65 61 73 65 20 63 6c 6f 73 65 20 74   "please close t
cafb0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
cafc0 6f 72 20 4e 4f 57 22 20 65 72 72 6f 72 73 20 69  or NOW" errors i
cafd0 6e 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 49  nto .** SQLITE_I
cafe0 4f 45 52 52 0a 2a 2a 20 0a 2a 2a 20 45 72 72 6f  OERR.** .** Erro
caff0 72 73 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61  rs during initia
cb000 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 6f 63 6b  lization of lock
cb010 73 2c 20 6f 72 20 66 69 6c 65 20 73 79 73 74 65  s, or file syste
cb020 6d 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6c 6f  m support for lo
cb030 63 6b 73 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 68  cks,.** should h
cb040 61 6e 64 6c 65 20 45 4e 4f 4c 43 4b 2c 20 45 4e  andle ENOLCK, EN
cb050 4f 54 53 55 50 2c 20 45 4f 50 4e 4f 54 53 55 50  OTSUP, EOPNOTSUP
cb060 50 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 2a 2f  P separately..*/
cb070 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
cb080 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
cb090 45 72 72 6f 72 28 69 6e 74 20 70 6f 73 69 78 45  Error(int posixE
cb0a0 72 72 6f 72 2c 20 69 6e 74 20 73 71 6c 69 74 65  rror, int sqlite
cb0b0 49 4f 45 72 72 29 20 7b 0a 20 20 73 77 69 74 63  IOErr) {.  switc
cb0c0 68 20 28 70 6f 73 69 78 45 72 72 6f 72 29 20 7b  h (posixError) {
cb0d0 0a 20 20 63 61 73 65 20 30 3a 20 0a 20 20 20 20  .  case 0: .    
cb0e0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
cb0f0 3b 0a 20 20 20 20 0a 20 20 63 61 73 65 20 45 41  ;.    .  case EA
cb100 47 41 49 4e 3a 0a 20 20 63 61 73 65 20 45 54 49  GAIN:.  case ETI
cb110 4d 45 44 4f 55 54 3a 0a 20 20 63 61 73 65 20 45  MEDOUT:.  case E
cb120 42 55 53 59 3a 0a 20 20 63 61 73 65 20 45 49 4e  BUSY:.  case EIN
cb130 54 52 3a 0a 20 20 63 61 73 65 20 45 4e 4f 4c 43  TR:.  case ENOLC
cb140 4b 3a 20 20 0a 20 20 20 20 2f 2a 20 72 61 6e 64  K:  .    /* rand
cb150 6f 6d 20 4e 46 53 20 72 65 74 72 79 20 65 72 72  om NFS retry err
cb160 6f 72 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e  or, unless durin
cb170 67 20 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75  g file system su
cb180 70 70 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e  pport .     * in
cb190 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20  trospection, in 
cb1a0 77 68 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c  which it actuall
cb1b0 79 20 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20  y means what it 
cb1c0 73 61 79 73 20 2a 2f 0a 20 20 20 20 72 65 74 75  says */.    retu
cb1d0 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
cb1e0 20 20 20 20 0a 20 20 63 61 73 65 20 45 41 43 43      .  case EACC
cb1f0 45 53 3a 20 0a 20 20 20 20 2f 2a 20 45 41 43 43  ES: .    /* EACC
cb200 45 53 20 69 73 20 6c 69 6b 65 20 45 41 47 41 49  ES is like EAGAI
cb210 4e 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e 67  N during locking
cb220 20 6f 70 65 72 61 74 69 6f 6e 73 2c 20 62 75 74   operations, but
cb230 20 6e 6f 74 20 61 6e 79 20 6f 74 68 65 72 20 74   not any other t
cb240 69 6d 65 2a 2f 0a 20 20 20 20 69 66 28 20 28 73  ime*/.    if( (s
cb250 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51  qliteIOErr == SQ
cb260 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29  LITE_IOERR_LOCK)
cb270 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45   || ..(sqliteIOE
cb280 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45  rr == SQLITE_IOE
cb290 52 52 5f 55 4e 4c 4f 43 4b 29 20 7c 7c 20 0a 09  RR_UNLOCK) || ..
cb2a0 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20  (sqliteIOErr == 
cb2b0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c  SQLITE_IOERR_RDL
cb2c0 4f 43 4b 29 20 7c 7c 0a 09 28 73 71 6c 69 74 65  OCK) ||..(sqlite
cb2d0 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f  IOErr == SQLITE_
cb2e0 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52  IOERR_CHECKRESER
cb2f0 56 45 44 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20  VEDLOCK) ){.    
cb300 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
cb310 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BUSY;.    }.    
cb320 2f 2a 20 65 6c 73 65 20 66 61 6c 6c 20 74 68 72  /* else fall thr
cb330 6f 75 67 68 20 2a 2f 0a 20 20 63 61 73 65 20 45  ough */.  case E
cb340 50 45 52 4d 3a 20 0a 20 20 20 20 72 65 74 75 72  PERM: .    retur
cb350 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20  n SQLITE_PERM;. 
cb360 20 20 20 0a 20 20 63 61 73 65 20 45 44 45 41 44     .  case EDEAD
cb370 4c 4b 3a 0a 20 20 20 20 72 65 74 75 72 6e 20 53  LK:.    return S
cb380 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43  QLITE_IOERR_BLOC
cb390 4b 45 44 3b 0a 20 20 20 20 0a 23 69 66 20 45 4f  KED;.    .#if EO
cb3a0 50 4e 4f 54 53 55 50 50 21 3d 45 4e 4f 54 53 55  PNOTSUPP!=ENOTSU
cb3b0 50 0a 20 20 63 61 73 65 20 45 4f 50 4e 4f 54 53  P.  case EOPNOTS
cb3c0 55 50 50 3a 20 0a 20 20 20 20 2f 2a 20 73 6f 6d  UPP: .    /* som
cb3d0 65 74 68 69 6e 67 20 77 65 6e 74 20 74 65 72 72  ething went terr
cb3e0 69 62 6c 79 20 61 77 72 79 2c 20 75 6e 6c 65 73  ibly awry, unles
cb3f0 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79  s during file sy
cb400 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20  stem support .  
cb410 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69     * introspecti
cb420 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20  on, in which it 
cb430 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77  actually means w
cb440 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 23  hat it says */.#
cb450 65 6e 64 69 66 0a 23 69 66 64 65 66 20 45 4e 4f  endif.#ifdef ENO
cb460 54 53 55 50 0a 20 20 63 61 73 65 20 45 4e 4f 54  TSUP.  case ENOT
cb470 53 55 50 3a 20 0a 20 20 20 20 2f 2a 20 69 6e 76  SUP: .    /* inv
cb480 61 6c 69 64 20 66 64 2c 20 75 6e 6c 65 73 73 20  alid fd, unless 
cb490 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73 74  during file syst
cb4a0 65 6d 20 73 75 70 70 6f 72 74 20 69 6e 74 72 6f  em support intro
cb4b0 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69  spection, in whi
cb4c0 63 68 20 0a 20 20 20 20 20 2a 20 69 74 20 61 63  ch .     * it ac
cb4d0 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68 61  tually means wha
cb4e0 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 23 65 6e  t it says */.#en
cb4f0 64 69 66 0a 20 20 63 61 73 65 20 45 49 4f 3a 0a  dif.  case EIO:.
cb500 20 20 63 61 73 65 20 45 42 41 44 46 3a 0a 20 20    case EBADF:.  
cb510 63 61 73 65 20 45 49 4e 56 41 4c 3a 0a 20 20 63  case EINVAL:.  c
cb520 61 73 65 20 45 4e 4f 54 43 4f 4e 4e 3a 0a 20 20  ase ENOTCONN:.  
cb530 63 61 73 65 20 45 4e 4f 44 45 56 3a 0a 20 20 63  case ENODEV:.  c
cb540 61 73 65 20 45 4e 58 49 4f 3a 0a 20 20 63 61 73  ase ENXIO:.  cas
cb550 65 20 45 4e 4f 45 4e 54 3a 0a 20 20 63 61 73 65  e ENOENT:.  case
cb560 20 45 53 54 41 4c 45 3a 0a 20 20 63 61 73 65 20   ESTALE:.  case 
cb570 45 4e 4f 53 59 53 3a 0a 20 20 20 20 2f 2a 20 74  ENOSYS:.    /* t
cb580 68 65 73 65 20 73 68 6f 75 6c 64 20 66 6f 72 63  hese should forc
cb590 65 20 74 68 65 20 63 6c 69 65 6e 74 20 74 6f 20  e the client to 
cb5a0 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61  close the file a
cb5b0 6e 64 20 72 65 63 6f 6e 6e 65 63 74 20 2a 2f 0a  nd reconnect */.
cb5c0 20 20 20 20 0a 20 20 64 65 66 61 75 6c 74 3a 20      .  default: 
cb5d0 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
cb5e0 74 65 49 4f 45 72 72 3b 0a 20 20 7d 0a 7d 0a 0a  teIOErr;.  }.}..
cb5f0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
cb600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb640 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
cb650 2a 2a 2a 2a 20 42 65 67 69 6e 20 55 6e 69 71 75  **** Begin Uniqu
cb660 65 20 46 69 6c 65 20 49 44 20 55 74 69 6c 69 74  e File ID Utilit
cb670 79 20 55 73 65 64 20 42 79 20 56 78 57 6f 72 6b  y Used By VxWork
cb680 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s **************
cb690 2a 0a 2a 2a 0a 2a 2a 20 4f 6e 20 6d 6f 73 74 20  *.**.** On most 
cb6a0 76 65 72 73 69 6f 6e 73 20 6f 66 20 75 6e 69 78  versions of unix
cb6b0 2c 20 77 65 20 63 61 6e 20 67 65 74 20 61 20 75  , we can get a u
cb6c0 6e 69 71 75 65 20 49 44 20 66 6f 72 20 61 20 66  nique ID for a f
cb6d0 69 6c 65 20 62 79 20 63 6f 6e 63 61 74 65 6e 61  ile by concatena
cb6e0 74 69 6e 67 0a 2a 2a 20 74 68 65 20 64 65 76 69  ting.** the devi
cb6f0 63 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68  ce number and th
cb700 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20  e inode number. 
cb710 20 42 75 74 20 74 68 69 73 20 64 6f 65 73 20 6e   But this does n
cb720 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78 57 6f 72  ot work on VxWor
cb730 6b 73 2e 0a 2a 2a 20 4f 6e 20 56 78 57 6f 72 6b  ks..** On VxWork
cb740 73 2c 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65  s, a unique file
cb750 20 69 64 20 6d 75 73 74 20 62 65 20 62 61 73 65   id must be base
cb760 64 20 6f 6e 20 74 68 65 20 63 61 6e 6f 6e 69 63  d on the canonic
cb770 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 0a  al filename..**.
cb780 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** A pointer to 
cb790 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
cb7a0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
cb7b0 75 63 74 75 72 65 20 63 61 6e 20 62 65 20 75 73  ucture can be us
cb7c0 65 64 20 61 73 20 61 0a 2a 2a 20 75 6e 69 71 75  ed as a.** uniqu
cb7d0 65 20 66 69 6c 65 20 49 44 20 69 6e 20 56 78 57  e file ID in VxW
cb7e0 6f 72 6b 73 2e 20 20 45 61 63 68 20 69 6e 73 74  orks.  Each inst
cb7f0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
cb800 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a  ucture contains.
cb810 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ** a copy of the
cb820 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e   canonical filen
cb830 61 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 61  ame.  There is a
cb840 6c 73 6f 20 61 20 72 65 66 65 72 65 6e 63 65 20  lso a reference 
cb850 63 6f 75 6e 74 2e 20 20 0a 2a 2a 20 54 68 65 20  count.  .** The 
cb860 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65 63  structure is rec
cb870 6c 61 69 6d 65 64 20 77 68 65 6e 20 74 68 65 20  laimed when the 
cb880 6e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65  number of pointe
cb890 72 73 20 74 6f 20 69 74 20 64 72 6f 70 73 20 74  rs to it drops t
cb8a0 6f 0a 2a 2a 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a  o.** zero..**.**
cb8b0 20 54 68 65 72 65 20 61 72 65 20 6e 65 76 65 72   There are never
cb8c0 20 76 65 72 79 20 6d 61 6e 79 20 66 69 6c 65 73   very many files
cb8d0 20 6f 70 65 6e 20 61 74 20 6f 6e 65 20 74 69 6d   open at one tim
cb8e0 65 20 61 6e 64 20 6c 6f 6f 6b 75 70 73 20 61 72  e and lookups ar
cb8f0 65 20 6e 6f 74 0a 2a 2a 20 61 20 70 65 72 66 6f  e not.** a perfo
cb900 72 6d 61 6e 63 65 2d 63 72 69 74 69 63 61 6c 20  rmance-critical 
cb910 70 61 74 68 2c 20 73 6f 20 69 74 20 69 73 20 73  path, so it is s
cb920 75 66 66 69 63 69 65 6e 74 20 74 6f 20 70 75 74  ufficient to put
cb930 20 74 68 65 73 65 0a 2a 2a 20 73 74 72 75 63 74   these.** struct
cb940 75 72 65 73 20 6f 6e 20 61 20 6c 69 6e 6b 65 64  ures on a linked
cb950 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63 74   list..*/.struct
cb960 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 7b   vxworksFileId {
cb970 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b  .  struct vxwork
cb980 73 46 69 6c 65 49 64 20 2a 70 4e 65 78 74 3b 20  sFileId *pNext; 
cb990 20 2f 2a 20 4e 65 78 74 20 69 6e 20 61 20 6c 69   /* Next in a li
cb9a0 73 74 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a  st of them all *
cb9b0 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20  /.  int nRef;   
cb9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cb9d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
cb9e0 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 69  eferences to thi
cb9f0 73 20 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  s one */.  int n
cba00 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
cba10 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
cba20 74 68 20 6f 66 20 74 68 65 20 7a 43 61 6e 6f 6e  th of the zCanon
cba30 69 63 61 6c 4e 61 6d 65 5b 5d 20 73 74 72 69 6e  icalName[] strin
cba40 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 61  g */.  char *zCa
cba50 6e 6f 6e 69 63 61 6c 4e 61 6d 65 3b 20 20 20 20  nonicalName;    
cba60 20 20 20 20 20 2f 2a 20 43 61 6e 6f 6e 69 63 61       /* Canonica
cba70 6c 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a 7d 3b  l filename */.};
cba80 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53  ..#if OS_VXWORKS
cba90 0a 2f 2a 20 0a 2a 2a 20 41 6c 6c 20 75 6e 69 71  ./* .** All uniq
cbaa0 75 65 20 66 69 6c 65 6e 61 6d 65 73 20 61 72 65  ue filenames are
cbab0 20 68 65 6c 64 20 6f 6e 20 61 20 6c 69 6e 6b 65   held on a linke
cbac0 64 20 6c 69 73 74 20 68 65 61 64 65 64 20 62 79  d list headed by
cbad0 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c   this.** variabl
cbae0 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72  e:.*/.static str
cbaf0 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49  uct vxworksFileI
cbb00 64 20 2a 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69  d *vxworksFileLi
cbb10 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 53  st = 0;../*.** S
cbb20 69 6d 70 6c 69 66 79 20 61 20 66 69 6c 65 6e 61  implify a filena
cbb30 6d 65 20 69 6e 74 6f 20 69 74 73 20 63 61 6e 6f  me into its cano
cbb40 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a 20 62 79  nical form.** by
cbb50 20 6d 61 6b 69 6e 67 20 74 68 65 20 66 6f 6c 6c   making the foll
cbb60 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 3a 0a 2a  owing changes:.*
cbb70 2a 0a 2a 2a 20 20 2a 20 72 65 6d 6f 76 69 6e 67  *.**  * removing
cbb80 20 61 6e 79 20 74 72 61 69 6c 69 6e 67 20 61 6e   any trailing an
cbb90 64 20 64 75 70 6c 69 63 61 74 65 20 2f 0a 2a 2a  d duplicate /.**
cbba0 20 20 2a 20 63 6f 6e 76 65 72 74 20 2f 2e 2f 20    * convert /./ 
cbbb0 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a 20 20  into just /.**  
cbbc0 2a 20 63 6f 6e 76 65 72 74 20 2f 41 2f 2e 2e 2f  * convert /A/../
cbbd0 20 77 68 65 72 65 20 41 20 69 73 20 61 6e 79 20   where A is any 
cbbe0 73 69 6d 70 6c 65 20 6e 61 6d 65 20 69 6e 74 6f  simple name into
cbbf0 20 6a 75 73 74 20 2f 0a 2a 2a 0a 2a 2a 20 43 68   just /.**.** Ch
cbc00 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 69  anges are made i
cbc10 6e 2d 70 6c 61 63 65 2e 20 20 52 65 74 75 72 6e  n-place.  Return
cbc20 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 20 6c 65   the new name le
cbc30 6e 67 74 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ngth..**.** The 
cbc40 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 6e 61 6d  original filenam
cbc50 65 20 69 73 20 69 6e 20 7a 5b 30 2e 2e 6e 2d 31  e is in z[0..n-1
cbc60 5d 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e  ].  Return the n
cbc70 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72  umber of.** char
cbc80 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 73 69  acters in the si
cbc90 6d 70 6c 69 66 69 65 64 20 6e 61 6d 65 2e 0a 2a  mplified name..*
cbca0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 78 77  /.static int vxw
cbcb0 6f 72 6b 73 53 69 6d 70 6c 69 66 79 4e 61 6d 65  orksSimplifyName
cbcc0 28 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29  (char *z, int n)
cbcd0 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  {.  int i, j;.  
cbce0 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 7a 5b  while( n>1 && z[
cbcf0 6e 2d 31 5d 3d 3d 27 2f 27 20 29 7b 20 6e 2d 2d  n-1]=='/' ){ n--
cbd00 3b 20 7d 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b  ; }.  for(i=j=0;
cbd10 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<n; i++){.    
cbd20 69 66 28 20 7a 5b 69 5d 3d 3d 27 2f 27 20 29 7b  if( z[i]=='/' ){
cbd30 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31  .      if( z[i+1
cbd40 5d 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 75  ]=='/' ) continu
cbd50 65 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69  e;.      if( z[i
cbd60 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 32 3c  +1]=='.' && i+2<
cbd70 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2f 27  n && z[i+2]=='/'
cbd80 20 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b 3d   ){.        i +=
cbd90 20 31 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74   1;.        cont
cbda0 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
cbdb0 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d      if( z[i+1]==
cbdc0 27 2e 27 20 26 26 20 69 2b 33 3c 6e 20 26 26 20  '.' && i+3<n && 
cbdd0 7a 5b 69 2b 32 5d 3d 3d 27 2e 27 20 26 26 20 7a  z[i+2]=='.' && z
cbde0 5b 69 2b 33 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20  [i+3]=='/' ){.  
cbdf0 20 20 20 20 20 20 77 68 69 6c 65 28 20 6a 3e 30        while( j>0
cbe00 20 26 26 20 7a 5b 6a 2d 31 5d 21 3d 27 2f 27 20   && z[j-1]!='/' 
cbe10 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20  ){ j--; }.      
cbe20 20 20 69 66 28 20 6a 3e 30 20 29 7b 20 6a 2d 2d    if( j>0 ){ j--
cbe30 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 20 2b 3d  ; }.        i +=
cbe40 20 32 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74   2;.        cont
cbe50 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
cbe60 20 20 7d 0a 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d    }.    z[j++] =
cbe70 20 7a 5b 69 5d 3b 0a 20 20 7d 0a 20 20 7a 5b 6a   z[i];.  }.  z[j
cbe80 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  ] = 0;.  return 
cbe90 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64  j;.}../*.** Find
cbea0 20 61 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49   a unique file I
cbeb0 44 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20  D for the given 
cbec0 61 62 73 6f 6c 75 74 65 20 70 61 74 68 6e 61 6d  absolute pathnam
cbed0 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20  e.  Return.** a 
cbee0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 76  pointer to the v
cbef0 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a  xworksFileId obj
cbf00 65 63 74 2e 20 20 54 68 69 73 20 70 6f 69 6e 74  ect.  This point
cbf10 65 72 20 69 73 20 74 68 65 20 75 6e 69 71 75 65  er is the unique
cbf20 0a 2a 2a 20 66 69 6c 65 20 49 44 2e 0a 2a 2a 0a  .** file ID..**.
cbf30 2a 2a 20 54 68 65 20 6e 52 65 66 20 66 69 65 6c  ** The nRef fiel
cbf40 64 20 6f 66 20 74 68 65 20 76 78 77 6f 72 6b 73  d of the vxworks
cbf50 46 69 6c 65 49 64 20 6f 62 6a 65 63 74 20 69 73  FileId object is
cbf60 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 65 66   incremented bef
cbf70 6f 72 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63  ore.** the objec
cbf80 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  t is returned.  
cbf90 41 20 6e 65 77 20 76 78 77 6f 72 6b 73 46 69 6c  A new vxworksFil
cbfa0 65 49 64 20 6f 62 6a 65 63 74 20 69 73 20 63 72  eId object is cr
cbfb0 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 61 64 64  eated.** and add
cbfc0 65 64 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c  ed to the global
cbfd0 20 6c 69 73 74 20 69 66 20 6e 65 63 65 73 73 61   list if necessa
cbfe0 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d  ry..**.** If a m
cbff0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
cc000 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72   error occurs, r
cc010 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73  eturn NULL..*/.s
cc020 74 61 74 69 63 20 73 74 72 75 63 74 20 76 78 77  tatic struct vxw
cc030 6f 72 6b 73 46 69 6c 65 49 64 20 2a 76 78 77 6f  orksFileId *vxwo
cc040 72 6b 73 46 69 6e 64 46 69 6c 65 49 64 28 63 6f  rksFindFileId(co
cc050 6e 73 74 20 63 68 61 72 20 2a 7a 41 62 73 6f 6c  nst char *zAbsol
cc060 75 74 65 4e 61 6d 65 29 7b 0a 20 20 73 74 72 75  uteName){.  stru
cc070 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64  ct vxworksFileId
cc080 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20   *pNew;         
cc090 2f 2a 20 73 65 61 72 63 68 20 6b 65 79 20 61 6e  /* search key an
cc0a0 64 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a 2f  d new file ID */
cc0b0 0a 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b  .  struct vxwork
cc0c0 73 46 69 6c 65 49 64 20 2a 70 43 61 6e 64 69 64  sFileId *pCandid
cc0d0 61 74 65 3b 20 20 20 2f 2a 20 46 6f 72 20 6c 6f  ate;   /* For lo
cc0e0 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73 74  oping over exist
cc0f0 69 6e 67 20 66 69 6c 65 20 49 44 73 20 2a 2f 0a  ing file IDs */.
cc100 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
cc110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cc120 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
cc130 6f 66 20 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65  of zAbsoluteName
cc140 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 61 73   string */..  as
cc150 73 65 72 74 28 20 7a 41 62 73 6f 6c 75 74 65 4e  sert( zAbsoluteN
cc160 61 6d 65 5b 30 5d 3d 3d 27 2f 27 20 29 3b 0a 20  ame[0]=='/' );. 
cc170 20 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e   n = (int)strlen
cc180 28 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 3b  (zAbsoluteName);
cc190 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
cc1a0 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  3_malloc( sizeof
cc1b0 28 2a 70 4e 65 77 29 20 2b 20 28 6e 2b 31 29 20  (*pNew) + (n+1) 
cc1c0 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  );.  if( pNew==0
cc1d0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
cc1e0 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e  New->zCanonicalN
cc1f0 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70 4e  ame = (char*)&pN
cc200 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63 70 79 28  ew[1];.  memcpy(
cc210 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c  pNew->zCanonical
cc220 4e 61 6d 65 2c 20 7a 41 62 73 6f 6c 75 74 65 4e  Name, zAbsoluteN
cc230 61 6d 65 2c 20 6e 2b 31 29 3b 0a 20 20 6e 20 3d  ame, n+1);.  n =
cc240 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69 66 79   vxworksSimplify
cc250 4e 61 6d 65 28 70 4e 65 77 2d 3e 7a 43 61 6e 6f  Name(pNew->zCano
cc260 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e 29 3b 0a 0a  nicalName, n);..
cc270 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20    /* Search for 
cc280 61 6e 20 65 78 69 73 74 69 6e 67 20 65 6e 74 72  an existing entr
cc290 79 20 74 68 61 74 20 6d 61 74 63 68 69 6e 67 20  y that matching 
cc2a0 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 6e 61  the canonical na
cc2b0 6d 65 2e 0a 20 20 2a 2a 20 49 66 20 66 6f 75 6e  me..  ** If foun
cc2c0 64 2c 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  d, increment the
cc2d0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
cc2e0 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f   and return a po
cc2f0 69 6e 74 65 72 20 74 6f 0a 20 20 2a 2a 20 74 68  inter to.  ** th
cc300 65 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20  e existing file 
cc310 49 44 2e 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45  ID..  */.  unixE
cc320 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 66  nterMutex();.  f
cc330 6f 72 28 70 43 61 6e 64 69 64 61 74 65 3d 76 78  or(pCandidate=vx
cc340 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20 70  worksFileList; p
cc350 43 61 6e 64 69 64 61 74 65 3b 20 70 43 61 6e 64  Candidate; pCand
cc360 69 64 61 74 65 3d 70 43 61 6e 64 69 64 61 74 65  idate=pCandidate
cc370 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66  ->pNext){.    if
cc380 28 20 70 43 61 6e 64 69 64 61 74 65 2d 3e 6e 4e  ( pCandidate->nN
cc390 61 6d 65 3d 3d 6e 20 0a 20 20 20 20 20 26 26 20  ame==n .     && 
cc3a0 6d 65 6d 63 6d 70 28 70 43 61 6e 64 69 64 61 74  memcmp(pCandidat
cc3b0 65 2d 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d  e->zCanonicalNam
cc3c0 65 2c 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f 6e 69  e, pNew->zCanoni
cc3d0 63 61 6c 4e 61 6d 65 2c 20 6e 29 3d 3d 30 0a 20  calName, n)==0. 
cc3e0 20 20 20 29 7b 0a 20 20 20 20 20 20 20 73 71 6c     ){.       sql
cc3f0 69 74 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b  ite3_free(pNew);
cc400 0a 20 20 20 20 20 20 20 70 43 61 6e 64 69 64 61  .       pCandida
cc410 74 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  te->nRef++;.    
cc420 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65     unixLeaveMute
cc430 78 28 29 3b 0a 20 20 20 20 20 20 20 72 65 74 75  x();.       retu
cc440 72 6e 20 70 43 61 6e 64 69 64 61 74 65 3b 0a 20  rn pCandidate;. 
cc450 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e     }.  }..  /* N
cc460 6f 20 6d 61 74 63 68 20 77 61 73 20 66 6f 75 6e  o match was foun
cc470 64 2e 20 20 57 65 20 77 69 6c 6c 20 6d 61 6b 65  d.  We will make
cc480 20 61 20 6e 65 77 20 66 69 6c 65 20 49 44 20 2a   a new file ID *
cc490 2f 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d  /.  pNew->nRef =
cc4a0 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 6e 4e 61 6d   1;.  pNew->nNam
cc4b0 65 20 3d 20 6e 3b 0a 20 20 70 4e 65 77 2d 3e 70  e = n;.  pNew->p
cc4c0 4e 65 78 74 20 3d 20 76 78 77 6f 72 6b 73 46 69  Next = vxworksFi
cc4d0 6c 65 4c 69 73 74 3b 0a 20 20 76 78 77 6f 72 6b  leList;.  vxwork
cc4e0 73 46 69 6c 65 4c 69 73 74 20 3d 20 70 4e 65 77  sFileList = pNew
cc4f0 3b 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74  ;.  unixLeaveMut
cc500 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ex();.  return p
cc510 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  New;.}../*.** De
cc520 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65  crement the refe
cc530 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 6e 20 61  rence count on a
cc540 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f   vxworksFileId o
cc550 62 6a 65 63 74 2e 20 20 46 72 65 65 0a 2a 2a 20  bject.  Free.** 
cc560 74 68 65 20 6f 62 6a 65 63 74 20 77 68 65 6e 20  the object when 
cc570 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
cc580 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f  unt reaches zero
cc590 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
cc5a0 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73 65 46   vxworksReleaseF
cc5b0 69 6c 65 49 64 28 73 74 72 75 63 74 20 76 78 77  ileId(struct vxw
cc5c0 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 29  orksFileId *pId)
cc5d0 7b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  {.  unixEnterMut
cc5e0 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ex();.  assert( 
cc5f0 70 49 64 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  pId->nRef>0 );. 
cc600 20 70 49 64 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20   pId->nRef--;.  
cc610 69 66 28 20 70 49 64 2d 3e 6e 52 65 66 3d 3d 30  if( pId->nRef==0
cc620 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 76   ){.    struct v
cc630 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 2a 70  xworksFileId **p
cc640 70 3b 0a 20 20 20 20 66 6f 72 28 70 70 3d 26 76  p;.    for(pp=&v
cc650 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 20  xworksFileList; 
cc660 2a 70 70 20 26 26 20 2a 70 70 21 3d 70 49 64 3b  *pp && *pp!=pId;
cc670 20 70 70 20 3d 20 26 28 28 2a 70 70 29 2d 3e 70   pp = &((*pp)->p
cc680 4e 65 78 74 29 29 7b 7d 0a 20 20 20 20 61 73 73  Next)){}.    ass
cc690 65 72 74 28 20 2a 70 70 3d 3d 70 49 64 20 29 3b  ert( *pp==pId );
cc6a0 0a 20 20 20 20 2a 70 70 20 3d 20 70 49 64 2d 3e  .    *pp = pId->
cc6b0 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74  pNext;.    sqlit
cc6c0 65 33 5f 66 72 65 65 28 70 49 64 29 3b 0a 20 20  e3_free(pId);.  
cc6d0 7d 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74  }.  unixLeaveMut
cc6e0 65 78 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  ex();.}.#endif /
cc6f0 2a 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a  * OS_VXWORKS */.
cc700 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
cc710 20 45 6e 64 20 6f 66 20 55 6e 69 71 75 65 20 46   End of Unique F
cc720 69 6c 65 20 49 44 20 55 74 69 6c 69 74 79 20 55  ile ID Utility U
cc730 73 65 64 20 42 79 20 56 78 57 6f 72 6b 73 20 2a  sed By VxWorks *
cc740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
cc750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
cc7a0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
cc7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc7f0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
cc800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 50 6f  ************* Po
cc810 73 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63  six Advisory Loc
cc820 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  king ***********
cc830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc840 2a 0a 2a 2a 0a 2a 2a 20 50 4f 53 49 58 20 61 64  *.**.** POSIX ad
cc850 76 69 73 6f 72 79 20 6c 6f 63 6b 73 20 61 72 65  visory locks are
cc860 20 62 72 6f 6b 65 6e 20 62 79 20 64 65 73 69 67   broken by desig
cc870 6e 2e 20 20 41 4e 53 49 20 53 54 44 20 31 30 30  n.  ANSI STD 100
cc880 33 2e 31 20 28 31 39 39 36 29 0a 2a 2a 20 73 65  3.1 (1996).** se
cc890 63 74 69 6f 6e 20 36 2e 35 2e 32 2e 32 20 6c 69  ction 6.5.2.2 li
cc8a0 6e 65 73 20 34 38 33 20 74 68 72 6f 75 67 68 20  nes 483 through 
cc8b0 34 39 30 20 73 70 65 63 69 66 79 20 74 68 61 74  490 specify that
cc8c0 20 77 68 65 6e 20 61 20 70 72 6f 63 65 73 73 0a   when a process.
cc8d0 2a 2a 20 73 65 74 73 20 6f 72 20 63 6c 65 61 72  ** sets or clear
cc8e0 73 20 61 20 6c 6f 63 6b 2c 20 74 68 61 74 20 6f  s a lock, that o
cc8f0 70 65 72 61 74 69 6f 6e 20 6f 76 65 72 72 69 64  peration overrid
cc900 65 73 20 61 6e 79 20 70 72 69 6f 72 20 6c 6f 63  es any prior loc
cc910 6b 73 20 73 65 74 0a 2a 2a 20 62 79 20 74 68 65  ks set.** by the
cc920 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20   same process.  
cc930 49 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 70 6c  It does not expl
cc940 69 63 69 74 6c 79 20 73 61 79 20 73 6f 2c 20 62  icitly say so, b
cc950 75 74 20 74 68 69 73 20 69 6d 70 6c 69 65 73 0a  ut this implies.
cc960 2a 2a 20 74 68 61 74 20 69 74 20 6f 76 65 72 72  ** that it overr
cc970 69 64 65 73 20 6c 6f 63 6b 73 20 73 65 74 20 62  ides locks set b
cc980 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  y the same proce
cc990 73 73 20 75 73 69 6e 67 20 61 20 64 69 66 66 65  ss using a diffe
cc9a0 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73  rent.** file des
cc9b0 63 72 69 70 74 6f 72 2e 20 20 43 6f 6e 73 69 64  criptor.  Consid
cc9c0 65 72 20 74 68 69 73 20 74 65 73 74 20 63 61 73  er this test cas
cc9d0 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 69  e:.**.**       i
cc9e0 6e 74 20 66 64 31 20 3d 20 6f 70 65 6e 28 22 2e  nt fd1 = open(".
cc9f0 2f 66 69 6c 65 31 22 2c 20 4f 5f 52 44 57 52 7c  /file1", O_RDWR|
cca00 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a  O_CREAT, 0644);.
cca10 2a 2a 20 20 20 20 20 20 20 69 6e 74 20 66 64 32  **       int fd2
cca20 20 3d 20 6f 70 65 6e 28 22 2e 2f 66 69 6c 65 32   = open("./file2
cca30 22 2c 20 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41  ", O_RDWR|O_CREA
cca40 54 2c 20 30 36 34 34 29 3b 0a 2a 2a 0a 2a 2a 20  T, 0644);.**.** 
cca50 53 75 70 70 6f 73 65 20 2e 2f 66 69 6c 65 31 20  Suppose ./file1 
cca60 61 6e 64 20 2e 2f 66 69 6c 65 32 20 61 72 65 20  and ./file2 are 
cca70 72 65 61 6c 6c 79 20 74 68 65 20 73 61 6d 65 20  really the same 
cca80 66 69 6c 65 20 28 62 65 63 61 75 73 65 0a 2a 2a  file (because.**
cca90 20 6f 6e 65 20 69 73 20 61 20 68 61 72 64 20 6f   one is a hard o
ccaa0 72 20 73 79 6d 62 6f 6c 69 63 20 6c 69 6e 6b 20  r symbolic link 
ccab0 74 6f 20 74 68 65 20 6f 74 68 65 72 29 20 74 68  to the other) th
ccac0 65 6e 20 69 66 20 79 6f 75 20 73 65 74 0a 2a 2a  en if you set.**
ccad0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
ccae0 63 6b 20 6f 6e 20 66 64 31 2c 20 74 68 65 6e 20  ck on fd1, then 
ccaf0 74 72 79 20 74 6f 20 67 65 74 20 61 6e 20 65 78  try to get an ex
ccb00 63 6c 75 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20  clusive lock.** 
ccb10 6f 6e 20 66 64 32 2c 20 69 74 20 77 6f 72 6b 73  on fd2, it works
ccb20 2e 20 20 49 20 77 6f 75 6c 64 20 68 61 76 65 20  .  I would have 
ccb30 65 78 70 65 63 74 65 64 20 74 68 65 20 73 65 63  expected the sec
ccb40 6f 6e 64 20 6c 6f 63 6b 20 74 6f 0a 2a 2a 20 66  ond lock to.** f
ccb50 61 69 6c 20 73 69 6e 63 65 20 74 68 65 72 65 20  ail since there 
ccb60 77 61 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f  was already a lo
ccb70 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 64  ck on the file d
ccb80 75 65 20 74 6f 20 66 64 31 2e 0a 2a 2a 20 42 75  ue to fd1..** Bu
ccb90 74 20 6e 6f 74 20 73 6f 2e 20 20 53 69 6e 63 65  t not so.  Since
ccba0 20 62 6f 74 68 20 6c 6f 63 6b 73 20 63 61 6d 65   both locks came
ccbb0 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20 70   from the same p
ccbc0 72 6f 63 65 73 73 2c 20 74 68 65 0a 2a 2a 20 73  rocess, the.** s
ccbd0 65 63 6f 6e 64 20 6f 76 65 72 72 69 64 65 73 20  econd overrides 
ccbe0 74 68 65 20 66 69 72 73 74 2c 20 65 76 65 6e 20  the first, even 
ccbf0 74 68 6f 75 67 68 20 74 68 65 79 20 77 65 72 65  though they were
ccc00 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a   on different.**
ccc10 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
ccc20 73 20 6f 70 65 6e 65 64 20 6f 6e 20 64 69 66 66  s opened on diff
ccc30 65 72 65 6e 74 20 66 69 6c 65 20 6e 61 6d 65 73  erent file names
ccc40 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 65 61  ..**.** This mea
ccc50 6e 73 20 74 68 61 74 20 77 65 20 63 61 6e 6e 6f  ns that we canno
ccc60 74 20 75 73 65 20 50 4f 53 49 58 20 6c 6f 63 6b  t use POSIX lock
ccc70 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65  s to synchronize
ccc80 20 66 69 6c 65 20 61 63 63 65 73 73 0a 2a 2a 20   file access.** 
ccc90 61 6d 6f 6e 67 20 63 6f 6d 70 65 74 69 6e 67 20  among competing 
ccca0 74 68 72 65 61 64 73 20 6f 66 20 74 68 65 20 73  threads of the s
cccb0 61 6d 65 20 70 72 6f 63 65 73 73 2e 20 20 50 4f  ame process.  PO
cccc0 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c 6c 20 77  SIX locks will w
cccd0 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74 6f 20 73  ork fine.** to s
ccce0 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63 63 65 73  ynchronize acces
cccf0 73 20 66 6f 72 20 74 68 72 65 61 64 73 20 69 6e  s for threads in
ccd00 20 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73   separate proces
ccd10 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a 2a 2a 20  ses, but not.** 
ccd20 74 68 72 65 61 64 73 20 77 69 74 68 69 6e 20 74  threads within t
ccd30 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e  he same process.
ccd40 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61  .**.** To work a
ccd50 72 6f 75 6e 64 20 74 68 65 20 70 72 6f 62 6c 65  round the proble
ccd60 6d 2c 20 53 51 4c 69 74 65 20 68 61 73 20 74 6f  m, SQLite has to
ccd70 20 6d 61 6e 61 67 65 20 66 69 6c 65 20 6c 6f 63   manage file loc
ccd80 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a  ks internally.**
ccd90 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20 20 57 68   on its own.  Wh
ccda0 65 6e 65 76 65 72 20 61 20 6e 65 77 20 64 61 74  enever a new dat
ccdb0 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 2c  abase is opened,
ccdc0 20 77 65 20 68 61 76 65 20 74 6f 20 66 69 6e 64   we have to find
ccdd0 20 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63   the.** specific
ccde0 20 69 6e 6f 64 65 20 6f 66 20 74 68 65 20 64 61   inode of the da
ccdf0 74 61 62 61 73 65 20 66 69 6c 65 20 28 74 68 65  tabase file (the
cce00 20 69 6e 6f 64 65 20 69 73 20 64 65 74 65 72 6d   inode is determ
cce10 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 73  ined by the.** s
cce20 74 5f 64 65 76 20 61 6e 64 20 73 74 5f 69 6e 6f  t_dev and st_ino
cce30 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 73   fields of the s
cce40 74 61 74 20 73 74 72 75 63 74 75 72 65 20 74 68  tat structure th
cce50 61 74 20 66 73 74 61 74 28 29 20 66 69 6c 6c 73  at fstat() fills
cce60 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63 68 65 63   in).** and chec
cce70 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61 6c 72 65  k for locks alre
cce80 61 64 79 20 65 78 69 73 74 69 6e 67 20 6f 6e 20  ady existing on 
cce90 74 68 61 74 20 69 6e 6f 64 65 2e 20 20 57 68 65  that inode.  Whe
ccea0 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 63  n locks are.** c
cceb0 72 65 61 74 65 64 20 6f 72 20 72 65 6d 6f 76 65  reated or remove
ccec0 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f  d, we have to lo
cced0 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e 20 69 6e  ok at our own in
ccee0 74 65 72 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66  ternal record of
ccef0 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73 20 74 6f   the.** locks to
ccf00 20 73 65 65 20 69 66 20 61 6e 6f 74 68 65 72 20   see if another 
ccf10 74 68 72 65 61 64 20 68 61 73 20 70 72 65 76 69  thread has previ
ccf20 6f 75 73 6c 79 20 73 65 74 20 61 20 6c 6f 63 6b  ously set a lock
ccf30 20 6f 6e 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a   on that same.**
ccf40 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 28 41   inode..**.** (A
ccf50 73 69 64 65 3a 20 54 68 65 20 75 73 65 20 6f 66  side: The use of
ccf60 20 69 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20 61   inode numbers a
ccf70 73 20 75 6e 69 71 75 65 20 49 44 73 20 64 6f 65  s unique IDs doe
ccf80 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e 20 56 78  s not work on Vx
ccf90 57 6f 72 6b 73 2e 0a 2a 2a 20 46 6f 72 20 56 78  Works..** For Vx
ccfa0 57 6f 72 6b 73 2c 20 77 65 20 68 61 76 65 20 74  Works, we have t
ccfb0 6f 20 75 73 65 20 74 68 65 20 61 6c 74 65 72 6e  o use the altern
ccfc0 61 74 69 76 65 20 75 6e 69 71 75 65 20 49 44 20  ative unique ID 
ccfd0 73 79 73 74 65 6d 20 62 61 73 65 64 20 6f 6e 0a  system based on.
ccfe0 2a 2a 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69 6c  ** canonical fil
ccff0 65 6e 61 6d 65 20 61 6e 64 20 69 6d 70 6c 65 6d  ename and implem
cd000 65 6e 74 65 64 20 69 6e 20 74 68 65 20 70 72 65  ented in the pre
cd010 76 69 6f 75 73 20 64 69 76 69 73 69 6f 6e 2e 29  vious division.)
cd020 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
cd030 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75 72  e3_file structur
cd040 65 20 66 6f 72 20 50 4f 53 49 58 20 69 73 20 6e  e for POSIX is n
cd050 6f 20 6c 6f 6e 67 65 72 20 6a 75 73 74 20 61 6e  o longer just an
cd060 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a 2a 2a   integer file.**
cd070 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20 49 74   descriptor.  It
cd080 20 69 73 20 6e 6f 77 20 61 20 73 74 72 75 63 74   is now a struct
cd090 75 72 65 20 74 68 61 74 20 68 6f 6c 64 73 20 74  ure that holds t
cd0a0 68 65 20 69 6e 74 65 67 65 72 20 66 69 6c 65 0a  he integer file.
cd0b0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 61 6e  ** descriptor an
cd0c0 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  d a pointer to a
cd0d0 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
cd0e0 64 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6e  describes the in
cd0f0 74 65 72 6e 61 6c 0a 2a 2a 20 6c 6f 63 6b 73 20  ternal.** locks 
cd100 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  on the correspon
cd110 64 69 6e 67 20 69 6e 6f 64 65 2e 20 20 54 68 65  ding inode.  The
cd120 72 65 20 69 73 20 6f 6e 65 20 6c 6f 63 6b 69 6e  re is one lockin
cd130 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70  g structure.** p
cd140 65 72 20 69 6e 6f 64 65 2c 20 73 6f 20 69 66 20  er inode, so if 
cd150 74 68 65 20 73 61 6d 65 20 69 6e 6f 64 65 20 69  the same inode i
cd160 73 20 6f 70 65 6e 65 64 20 74 77 69 63 65 2c 20  s opened twice, 
cd170 62 6f 74 68 20 75 6e 69 78 46 69 6c 65 20 73 74  both unixFile st
cd180 72 75 63 74 75 72 65 73 0a 2a 2a 20 70 6f 69 6e  ructures.** poin
cd190 74 20 74 6f 20 74 68 65 20 73 61 6d 65 20 6c 6f  t to the same lo
cd1a0 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  cking structure.
cd1b0 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 73 74    The locking st
cd1c0 72 75 63 74 75 72 65 20 6b 65 65 70 73 0a 2a 2a  ructure keeps.**
cd1d0 20 61 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75   a reference cou
cd1e0 6e 74 20 28 73 6f 20 77 65 20 77 69 6c 6c 20 6b  nt (so we will k
cd1f0 6e 6f 77 20 77 68 65 6e 20 74 6f 20 64 65 6c 65  now when to dele
cd200 74 65 20 69 74 29 20 61 6e 64 20 61 20 22 63 6e  te it) and a "cn
cd210 74 22 0a 2a 2a 20 66 69 65 6c 64 20 74 68 61 74  t".** field that
cd220 20 74 65 6c 6c 73 20 75 73 20 69 74 73 20 69 6e   tells us its in
cd230 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74  ternal lock stat
cd240 75 73 2e 20 20 63 6e 74 3d 3d 30 20 6d 65 61 6e  us.  cnt==0 mean
cd250 73 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 69 73  s the.** file is
cd260 20 75 6e 6c 6f 63 6b 65 64 2e 20 20 63 6e 74 3d   unlocked.  cnt=
cd270 3d 2d 31 20 6d 65 61 6e 73 20 74 68 65 20 66 69  =-1 means the fi
cd280 6c 65 20 68 61 73 20 61 6e 20 65 78 63 6c 75 73  le has an exclus
cd290 69 76 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 63 6e 74  ive lock..** cnt
cd2a0 3e 30 20 6d 65 61 6e 73 20 74 68 65 72 65 20 61  >0 means there a
cd2b0 72 65 20 63 6e 74 20 73 68 61 72 65 64 20 6c 6f  re cnt shared lo
cd2c0 63 6b 73 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e  cks on the file.
cd2d0 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d  .**.** Any attem
cd2e0 70 74 20 74 6f 20 6c 6f 63 6b 20 6f 72 20 75 6e  pt to lock or un
cd2f0 6c 6f 63 6b 20 61 20 66 69 6c 65 20 66 69 72 73  lock a file firs
cd300 74 20 63 68 65 63 6b 73 20 74 68 65 20 6c 6f 63  t checks the loc
cd310 6b 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72  king.** structur
cd320 65 2e 20 20 54 68 65 20 66 63 6e 74 6c 28 29 20  e.  The fcntl() 
cd330 73 79 73 74 65 6d 20 63 61 6c 6c 20 69 73 20 6f  system call is o
cd340 6e 6c 79 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73  nly invoked to s
cd350 65 74 20 61 20 0a 2a 2a 20 50 4f 53 49 58 20 6c  et a .** POSIX l
cd360 6f 63 6b 20 69 66 20 74 68 65 20 69 6e 74 65 72  ock if the inter
cd370 6e 61 6c 20 6c 6f 63 6b 20 73 74 72 75 63 74 75  nal lock structu
cd380 72 65 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 62  re transitions b
cd390 65 74 77 65 65 6e 0a 2a 2a 20 61 20 6c 6f 63 6b  etween.** a lock
cd3a0 65 64 20 61 6e 64 20 61 6e 20 75 6e 6c 6f 63 6b  ed and an unlock
cd3b0 65 64 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  ed state..**.** 
cd3c0 42 75 74 20 77 61 69 74 3a 20 20 74 68 65 72 65  But wait:  there
cd3d0 20 61 72 65 20 79 65 74 20 6d 6f 72 65 20 70 72   are yet more pr
cd3e0 6f 62 6c 65 6d 73 20 77 69 74 68 20 50 4f 53 49  oblems with POSI
cd3f0 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  X advisory locks
cd400 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 79 6f 75 20 63  ..**.** If you c
cd410 6c 6f 73 65 20 61 20 66 69 6c 65 20 64 65 73 63  lose a file desc
cd420 72 69 70 74 6f 72 20 74 68 61 74 20 70 6f 69 6e  riptor that poin
cd430 74 73 20 74 6f 20 61 20 66 69 6c 65 20 74 68 61  ts to a file tha
cd440 74 20 68 61 73 20 6c 6f 63 6b 73 2c 0a 2a 2a 20  t has locks,.** 
cd450 61 6c 6c 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61  all locks on tha
cd460 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20  t file that are 
cd470 6f 77 6e 65 64 20 62 79 20 74 68 65 20 63 75 72  owned by the cur
cd480 72 65 6e 74 20 70 72 6f 63 65 73 73 20 61 72 65  rent process are
cd490 0a 2a 2a 20 72 65 6c 65 61 73 65 64 2e 20 20 54  .** released.  T
cd4a0 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68  o work around th
cd4b0 69 73 20 70 72 6f 62 6c 65 6d 2c 20 65 61 63 68  is problem, each
cd4c0 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74   unixFile struct
cd4d0 75 72 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  ure contains.** 
cd4e0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
cd4f0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75  unixOpenCnt stru
cd500 63 74 75 72 65 2e 20 20 54 68 65 72 65 20 69 73  cture.  There is
cd510 20 6f 6e 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74   one unixOpenCnt
cd520 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 70 65   structure.** pe
cd530 72 20 6f 70 65 6e 20 69 6e 6f 64 65 2c 20 77 68  r open inode, wh
cd540 69 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6d  ich means that m
cd550 75 6c 74 69 70 6c 65 20 75 6e 69 78 46 69 6c 65  ultiple unixFile
cd560 20 63 61 6e 20 70 6f 69 6e 74 20 74 6f 20 61 20   can point to a 
cd570 73 69 6e 67 6c 65 0a 2a 2a 20 75 6e 69 78 4f 70  single.** unixOp
cd580 65 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61 6e 20  enCnt.  When an 
cd590 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
cd5a0 74 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e 69 78  to close an unix
cd5b0 46 69 6c 65 2c 20 69 66 20 74 68 65 72 65 20 61  File, if there a
cd5c0 72 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e 69 78  re.** other unix
cd5d0 46 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65  File open on the
cd5e0 20 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 61 74   same inode that
cd5f0 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c 6f 63   are holding loc
cd600 6b 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a 2a 20  ks, the call.** 
cd610 74 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 20 66  to close() the f
cd620 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
cd630 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c  s deferred until
cd640 20 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f 63 6b   all of the lock
cd650 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68 65 20  s clear..** The 
cd660 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75  unixOpenCnt stru
cd670 63 74 75 72 65 20 6b 65 65 70 73 20 61 20 6c 69  cture keeps a li
cd680 73 74 20 6f 66 20 66 69 6c 65 20 64 65 73 63 72  st of file descr
cd690 69 70 74 6f 72 73 20 74 68 61 74 20 6e 65 65 64  iptors that need
cd6a0 20 74 6f 0a 2a 2a 20 62 65 20 63 6c 6f 73 65 64   to.** be closed
cd6b0 20 61 6e 64 20 74 68 61 74 20 6c 69 73 74 20 69   and that list i
cd6c0 73 20 77 61 6c 6b 65 64 20 28 61 6e 64 20 63 6c  s walked (and cl
cd6d0 65 61 72 65 64 29 20 77 68 65 6e 20 74 68 65 20  eared) when the 
cd6e0 6c 61 73 74 20 6c 6f 63 6b 0a 2a 2a 20 63 6c 65  last lock.** cle
cd6f0 61 72 73 2e 0a 2a 2a 0a 2a 2a 20 59 65 74 20 61  ars..**.** Yet a
cd700 6e 6f 74 68 65 72 20 70 72 6f 62 6c 65 6d 3a 20  nother problem: 
cd710 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 64 6f   LinuxThreads do
cd720 20 6e 6f 74 20 70 6c 61 79 20 77 65 6c 6c 20 77   not play well w
cd730 69 74 68 20 70 6f 73 69 78 20 6c 6f 63 6b 73 2e  ith posix locks.
cd740 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20 6f 6c 64 65  .**.** Many olde
cd750 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 6c 69  r versions of li
cd760 6e 75 78 20 75 73 65 20 74 68 65 20 4c 69 6e 75  nux use the Linu
cd770 78 54 68 72 65 61 64 73 20 6c 69 62 72 61 72 79  xThreads library
cd780 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 6e 6f 74   which is.** not
cd790 20 70 6f 73 69 78 20 63 6f 6d 70 6c 69 61 6e 74   posix compliant
cd7a0 2e 20 20 55 6e 64 65 72 20 4c 69 6e 75 78 54 68  .  Under LinuxTh
cd7b0 72 65 61 64 73 2c 20 61 20 6c 6f 63 6b 20 63 72  reads, a lock cr
cd7c0 65 61 74 65 64 20 62 79 20 74 68 72 65 61 64 0a  eated by thread.
cd7d0 2a 2a 20 41 20 63 61 6e 6e 6f 74 20 62 65 20 6d  ** A cannot be m
cd7e0 6f 64 69 66 69 65 64 20 6f 72 20 6f 76 65 72 72  odified or overr
cd7f0 69 64 64 65 6e 20 62 79 20 61 20 64 69 66 66 65  idden by a diffe
cd800 72 65 6e 74 20 74 68 72 65 61 64 20 42 2e 0a 2a  rent thread B..*
cd810 2a 20 4f 6e 6c 79 20 74 68 72 65 61 64 20 41 20  * Only thread A 
cd820 63 61 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 6c  can modify the l
cd830 6f 63 6b 2e 20 20 4c 6f 63 6b 69 6e 67 20 62 65  ock.  Locking be
cd840 68 61 76 69 6f 72 20 69 73 20 63 6f 72 72 65 63  havior is correc
cd850 74 0a 2a 2a 20 69 66 20 74 68 65 20 61 70 70 6c  t.** if the appl
cd860 69 61 74 69 6f 6e 20 75 73 65 73 20 74 68 65 20  iation uses the 
cd870 6e 65 77 65 72 20 4e 61 74 69 76 65 20 50 6f 73  newer Native Pos
cd880 69 78 20 54 68 72 65 61 64 20 4c 69 62 72 61 72  ix Thread Librar
cd890 79 20 28 4e 50 54 4c 29 0a 2a 2a 20 6f 6e 20 6c  y (NPTL).** on l
cd8a0 69 6e 75 78 20 2d 20 77 69 74 68 20 4e 50 54 4c  inux - with NPTL
cd8b0 20 61 20 6c 6f 63 6b 20 63 72 65 61 74 65 64 20   a lock created 
cd8c0 62 79 20 74 68 72 65 61 64 20 41 20 63 61 6e 20  by thread A can 
cd8d0 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 0a 2a  override locks.*
cd8e0 2a 20 69 6e 20 74 68 72 65 61 64 20 42 2e 20 20  * in thread B.  
cd8f0 42 75 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20  But there is no 
cd900 77 61 79 20 74 6f 20 6b 6e 6f 77 20 61 74 20 63  way to know at c
cd910 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 69 63  ompile-time whic
cd920 68 0a 2a 2a 20 74 68 72 65 61 64 69 6e 67 20 6c  h.** threading l
cd930 69 62 72 61 72 79 20 69 73 20 62 65 69 6e 67 20  ibrary is being 
cd940 75 73 65 64 2e 20 20 53 6f 20 74 68 65 72 65 20  used.  So there 
cd950 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f  is no way to kno
cd960 77 20 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d  w at.** compile-
cd970 74 69 6d 65 20 77 68 65 74 68 65 72 20 6f 72 20  time whether or 
cd980 6e 6f 74 20 74 68 72 65 61 64 20 41 20 63 61 6e  not thread A can
cd990 20 6f 76 65 72 72 69 64 65 20 6c 6f 63 6b 73 20   override locks 
cd9a0 6f 6e 20 74 68 72 65 61 64 20 42 2e 0a 2a 2a 20  on thread B..** 
cd9b0 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 61 20  We have to do a 
cd9c0 72 75 6e 2d 74 69 6d 65 20 63 68 65 63 6b 20 74  run-time check t
cd9d0 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65 20 62  o discover the b
cd9e0 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 0a 2a  ehavior of the.*
cd9f0 2a 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73  * current proces
cda00 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 79 73 74  s..**.** On syst
cda10 65 6d 73 20 77 68 65 72 65 20 74 68 72 65 61 64  ems where thread
cda20 20 41 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20   A is unable to 
cda30 6d 6f 64 69 66 79 20 6c 6f 63 6b 73 20 63 72 65  modify locks cre
cda40 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 72 65 61  ated by.** threa
cda50 64 20 42 2c 20 77 65 20 68 61 76 65 20 74 6f 20  d B, we have to 
cda60 6b 65 65 70 20 74 72 61 63 6b 20 6f 66 20 77 68  keep track of wh
cda70 69 63 68 20 74 68 72 65 61 64 20 63 72 65 61 74  ich thread creat
cda80 65 64 20 65 61 63 68 0a 2a 2a 20 6c 6f 63 6b 2e  ed each.** lock.
cda90 20 20 48 65 6e 63 65 20 74 68 65 72 65 20 69 73    Hence there is
cdaa0 20 61 6e 20 65 78 74 72 61 20 66 69 65 6c 64 20   an extra field 
cdab0 69 6e 20 74 68 65 20 6b 65 79 20 74 6f 20 74 68  in the key to th
cdac0 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 0a 2a  e unixLockInfo.*
cdad0 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 72  * structure to r
cdae0 65 63 6f 72 64 20 74 68 69 73 20 69 6e 66 6f 72  ecord this infor
cdaf0 6d 61 74 69 6f 6e 2e 20 20 41 6e 64 20 6f 6e 20  mation.  And on 
cdb00 74 68 6f 73 65 20 73 79 73 74 65 6d 73 20 69 74  those systems it
cdb10 0a 2a 2a 20 69 73 20 69 6c 6c 65 67 61 6c 20 74  .** is illegal t
cdb20 6f 20 62 65 67 69 6e 20 61 20 74 72 61 6e 73 61  o begin a transa
cdb30 63 74 69 6f 6e 20 69 6e 20 6f 6e 65 20 74 68 72  ction in one thr
cdb40 65 61 64 20 61 6e 64 20 66 69 6e 69 73 68 20 69  ead and finish i
cdb50 74 0a 2a 2a 20 69 6e 20 61 6e 6f 74 68 65 72 2e  t.** in another.
cdb60 20 20 46 6f 72 20 74 68 69 73 20 6c 61 74 74 65    For this latte
cdb70 72 20 72 65 73 74 72 69 63 74 69 6f 6e 2c 20 74  r restriction, t
cdb80 68 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 2d  here is no work-
cdb90 61 72 6f 75 6e 64 2e 0a 2a 2a 20 49 74 20 69 73  around..** It is
cdba0 20 61 20 6c 69 6d 69 74 61 74 69 6f 6e 20 6f 66   a limitation of
cdbb0 20 4c 69 6e 75 78 54 68 72 65 61 64 73 2e 0a 2a   LinuxThreads..*
cdbc0 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20  /../*.** Set or 
cdbd0 63 68 65 63 6b 20 74 68 65 20 75 6e 69 78 46 69  check the unixFi
cdbe0 6c 65 2e 74 69 64 20 66 69 65 6c 64 2e 20 20 54  le.tid field.  T
cdbf0 68 69 73 20 66 69 65 6c 64 20 69 73 20 73 65 74  his field is set
cdc00 20 77 68 65 6e 20 61 6e 20 75 6e 69 78 46 69 6c   when an unixFil
cdc10 65 0a 2a 2a 20 69 73 20 66 69 72 73 74 20 6f 70  e.** is first op
cdc20 65 6e 65 64 2e 20 20 41 6c 6c 20 73 75 62 73 65  ened.  All subse
cdc30 71 75 65 6e 74 20 75 73 65 73 20 6f 66 20 74 68  quent uses of th
cdc40 65 20 75 6e 69 78 46 69 6c 65 20 76 65 72 69 66  e unixFile verif
cdc50 79 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73 61  y that the.** sa
cdc60 6d 65 20 74 68 72 65 61 64 20 69 73 20 6f 70 65  me thread is ope
cdc70 72 61 74 69 6e 67 20 6f 6e 20 74 68 65 20 75 6e  rating on the un
cdc80 69 78 46 69 6c 65 2e 20 20 53 6f 6d 65 20 6f 70  ixFile.  Some op
cdc90 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20  erating systems 
cdca0 64 6f 0a 2a 2a 20 6e 6f 74 20 61 6c 6c 6f 77 20  do.** not allow 
cdcb0 6c 6f 63 6b 73 20 74 6f 20 62 65 20 6f 76 65 72  locks to be over
cdcc0 72 69 64 64 65 6e 20 62 79 20 6f 74 68 65 72 20  ridden by other 
cdcd0 74 68 72 65 61 64 73 20 61 6e 64 20 74 68 61 74  threads and that
cdce0 20 72 65 73 74 72 69 63 74 69 6f 6e 0a 2a 2a 20   restriction.** 
cdcf0 6d 65 61 6e 73 20 74 68 61 74 20 73 71 6c 69 74  means that sqlit
cdd00 65 33 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e  e3* database han
cdd10 64 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 6d  dles cannot be m
cdd20 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65 20 74 68  oved from one th
cdd30 72 65 61 64 0a 2a 2a 20 74 6f 20 61 6e 6f 74 68  read.** to anoth
cdd40 65 72 20 77 68 69 6c 65 20 6c 6f 63 6b 73 20 61  er while locks a
cdd50 72 65 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 56  re held..**.** V
cdd60 65 72 73 69 6f 6e 20 33 2e 33 2e 31 20 28 32 30  ersion 3.3.1 (20
cdd70 30 36 2d 30 31 2d 31 35 29 3a 20 20 75 6e 69 78  06-01-15):  unix
cdd80 46 69 6c 65 20 63 61 6e 20 62 65 20 6d 6f 76 65  File can be move
cdd90 64 20 66 72 6f 6d 20 6f 6e 65 20 74 68 72 65 61  d from one threa
cdda0 64 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20  d to.** another 
cddb0 61 73 20 6c 6f 6e 67 20 61 73 20 77 65 20 61 72  as long as we ar
cddc0 65 20 72 75 6e 6e 69 6e 67 20 6f 6e 20 61 20 73  e running on a s
cddd0 79 73 74 65 6d 20 74 68 61 74 20 73 75 70 70 6f  ystem that suppo
cdde0 72 74 73 20 74 68 72 65 61 64 73 0a 2a 2a 20 6f  rts threads.** o
cddf0 76 65 72 72 69 64 69 6e 67 20 65 61 63 68 20 6f  verriding each o
cde00 74 68 65 72 73 20 6c 6f 63 6b 73 20 28 77 68 69  thers locks (whi
cde10 63 68 20 69 73 20 6e 6f 77 20 74 68 65 20 6d 6f  ch is now the mo
cde20 73 74 20 63 6f 6d 6d 6f 6e 20 62 65 68 61 76 69  st common behavi
cde30 6f 72 29 0a 2a 2a 20 6f 72 20 69 66 20 6e 6f 20  or).** or if no 
cde40 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 20  locks are held. 
cde50 20 42 75 74 20 74 68 65 20 75 6e 69 78 46 69 6c   But the unixFil
cde60 65 2e 70 4c 6f 63 6b 20 66 69 65 6c 64 20 6e 65  e.pLock field ne
cde70 65 64 73 20 74 6f 20 62 65 0a 2a 2a 20 72 65 63  eds to be.** rec
cde80 6f 6d 70 75 74 65 64 20 62 65 63 61 75 73 65 20  omputed because 
cde90 69 74 73 20 6b 65 79 20 69 6e 63 6c 75 64 65 73  its key includes
cdea0 20 74 68 65 20 74 68 72 65 61 64 2d 69 64 2e 20   the thread-id. 
cdeb0 20 53 65 65 20 74 68 65 20 0a 2a 2a 20 74 72 61   See the .** tra
cdec0 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28 29  nsferOwnership()
cded0 20 66 75 6e 63 74 69 6f 6e 20 62 65 6c 6f 77 20   function below 
cdee0 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
cdef0 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2f 0a 23 69  nformation.*/.#i
cdf00 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
cdf10 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  AFE && defined(_
cdf20 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69  _linux__).# defi
cdf30 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49 44 28  ne SET_THREADID(
cdf40 58 29 20 20 20 28 58 29 2d 3e 74 69 64 20 3d 20  X)   (X)->tid = 
cdf50 70 74 68 72 65 61 64 5f 73 65 6c 66 28 29 0a 23  pthread_self().#
cdf60 20 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48   define CHECK_TH
cdf70 52 45 41 44 49 44 28 58 29 20 28 74 68 72 65 61  READID(X) (threa
cdf80 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
cdf90 68 65 72 73 4c 6f 63 6b 73 3d 3d 30 20 26 26 20  hersLocks==0 && 
cdfa0 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
cdfb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 70                !p
cdfc0 74 68 72 65 61 64 5f 65 71 75 61 6c 28 28 58 29  thread_equal((X)
cdfd0 2d 3e 74 69 64 2c 20 70 74 68 72 65 61 64 5f 73  ->tid, pthread_s
cdfe0 65 6c 66 28 29 29 29 0a 23 65 6c 73 65 0a 23 20  elf())).#else.# 
cdff0 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41  define SET_THREA
ce000 44 49 44 28 58 29 0a 23 20 64 65 66 69 6e 65 20  DID(X).# define 
ce010 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58  CHECK_THREADID(X
ce020 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ) 0.#endif../*.*
ce030 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
ce040 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
ce050 74 72 75 63 74 75 72 65 20 73 65 72 76 65 73 20  tructure serves 
ce060 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 64 0a  as the key used.
ce070 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20 70  ** to locate a p
ce080 61 72 74 69 63 75 6c 61 72 20 75 6e 69 78 4f 70  articular unixOp
ce090 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 20  enCnt structure 
ce0a0 67 69 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e  given its inode.
ce0b0 20 20 54 68 69 73 0a 2a 2a 20 69 73 20 74 68 65    This.** is the
ce0c0 20 73 61 6d 65 20 61 73 20 74 68 65 20 75 6e 69   same as the uni
ce0d0 78 4c 6f 63 6b 4b 65 79 20 65 78 63 65 70 74 20  xLockKey except 
ce0e0 74 68 61 74 20 74 68 65 20 74 68 72 65 61 64 20  that the thread 
ce0f0 49 44 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a  ID is omitted..*
ce100 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c  /.struct unixFil
ce110 65 49 64 20 7b 0a 20 20 64 65 76 5f 74 20 64 65  eId {.  dev_t de
ce120 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
ce130 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 6e 75      /* Device nu
ce140 6d 62 65 72 20 2a 2f 0a 23 69 66 20 4f 53 5f 56  mber */.#if OS_V
ce150 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20  XWORKS.  struct 
ce160 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70  vxworksFileId *p
ce170 49 64 3b 20 20 2f 2a 20 55 6e 69 71 75 65 20 66  Id;  /* Unique f
ce180 69 6c 65 20 49 44 20 66 6f 72 20 76 78 77 6f 72  ile ID for vxwor
ce190 6b 73 2e 20 2a 2f 0a 23 65 6c 73 65 0a 20 20 69  ks. */.#else.  i
ce1a0 6e 6f 5f 74 20 69 6e 6f 3b 20 20 20 20 20 20 20  no_t ino;       
ce1b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
ce1c0 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 65  ode number */.#e
ce1d0 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  ndif.};../*.** A
ce1e0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
ce1f0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
ce200 63 74 75 72 65 20 73 65 72 76 65 73 20 61 73 20  cture serves as 
ce210 74 68 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20  the key used.** 
ce220 74 6f 20 6c 6f 63 61 74 65 20 61 20 70 61 72 74  to locate a part
ce230 69 63 75 6c 61 72 20 75 6e 69 78 4c 6f 63 6b 49  icular unixLockI
ce240 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 67 69  nfo structure gi
ce250 76 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 0a 2a  ven its inode..*
ce260 2a 0a 2a 2a 20 49 66 20 74 68 72 65 61 64 73 20  *.** If threads 
ce270 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65 20  cannot override 
ce280 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b  each others lock
ce290 73 20 28 4c 69 6e 75 78 54 68 72 65 61 64 73 29  s (LinuxThreads)
ce2a0 2c 20 74 68 65 6e 20 77 65 0a 2a 2a 20 73 65 74  , then we.** set
ce2b0 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79   the unixLockKey
ce2c0 2e 74 69 64 20 66 69 65 6c 64 20 74 6f 20 74 68  .tid field to th
ce2d0 65 20 74 68 72 65 61 64 20 49 44 2e 20 20 49 66  e thread ID.  If
ce2e0 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65   threads can ove
ce2f0 72 72 69 64 65 0a 2a 2a 20 65 61 63 68 20 6f 74  rride.** each ot
ce300 68 65 72 73 20 6c 6f 63 6b 73 20 28 50 6f 73 69  hers locks (Posi
ce310 78 20 61 6e 64 20 4e 50 54 4c 29 20 74 68 65 6e  x and NPTL) then
ce320 20 74 69 64 20 69 73 20 61 6c 77 61 79 73 20 73   tid is always s
ce330 65 74 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 20 74  et to zero..** t
ce340 69 64 20 69 73 20 6f 6d 69 74 74 65 64 20 69 66  id is omitted if
ce350 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68   we compile with
ce360 6f 75 74 20 74 68 72 65 61 64 69 6e 67 20 73 75  out threading su
ce370 70 70 6f 72 74 20 6f 72 20 6f 6e 20 61 6e 20 4f  pport or on an O
ce380 53 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20  S.** other than 
ce390 6c 69 6e 75 78 2e 0a 2a 2f 0a 73 74 72 75 63 74  linux..*/.struct
ce3a0 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 7b 0a 20   unixLockKey {. 
ce3b0 20 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65   struct unixFile
ce3c0 49 64 20 66 69 64 3b 20 20 2f 2a 20 55 6e 69 71  Id fid;  /* Uniq
ce3d0 75 65 20 69 64 65 6e 74 69 66 69 65 72 20 66 6f  ue identifier fo
ce3e0 72 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 23 69  r the file */.#i
ce3f0 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
ce400 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  AFE && defined(_
ce410 5f 6c 69 6e 75 78 5f 5f 29 0a 20 20 70 74 68 72  _linux__).  pthr
ce420 65 61 64 5f 74 20 74 69 64 3b 20 20 2f 2a 20 54  ead_t tid;  /* T
ce430 68 72 65 61 64 20 49 44 20 6f 66 20 6c 6f 63 6b  hread ID of lock
ce440 20 6f 77 6e 65 72 2e 20 5a 65 72 6f 20 69 66 20   owner. Zero if 
ce450 6e 6f 74 20 75 73 69 6e 67 20 4c 69 6e 75 78 54  not using LinuxT
ce460 68 72 65 61 64 73 20 2a 2f 0a 23 65 6e 64 69 66  hreads */.#endif
ce470 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  .};../*.** An in
ce480 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
ce490 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
ce4a0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  e is allocated f
ce4b0 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20  or each open.** 
ce4c0 69 6e 6f 64 65 2e 20 20 4f 72 2c 20 6f 6e 20 4c  inode.  Or, on L
ce4d0 69 6e 75 78 54 68 72 65 61 64 73 2c 20 74 68 65  inuxThreads, the
ce4e0 72 65 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65  re is one of the
ce4f0 73 65 20 73 74 72 75 63 74 75 72 65 73 20 66 6f  se structures fo
ce500 72 0a 2a 2a 20 65 61 63 68 20 69 6e 6f 64 65 20  r.** each inode 
ce510 6f 70 65 6e 65 64 20 62 79 20 65 61 63 68 20 74  opened by each t
ce520 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 73  hread..**.** A s
ce530 69 6e 67 6c 65 20 69 6e 6f 64 65 20 63 61 6e 20  ingle inode can 
ce540 68 61 76 65 20 6d 75 6c 74 69 70 6c 65 20 66 69  have multiple fi
ce550 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20  le descriptors, 
ce560 73 6f 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65  so each unixFile
ce570 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 63 6f  .** structure co
ce580 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72  ntains a pointer
ce590 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   to an instance 
ce5a0 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 61  of this object a
ce5b0 6e 64 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63  nd this.** objec
ce5c0 74 20 6b 65 65 70 73 20 61 20 63 6f 75 6e 74 20  t keeps a count 
ce5d0 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
ce5e0 20 75 6e 69 78 46 69 6c 65 20 70 6f 69 6e 74 69   unixFile pointi
ce5f0 6e 67 20 74 6f 20 69 74 2e 0a 2a 2f 0a 73 74 72  ng to it..*/.str
ce600 75 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  uct unixLockInfo
ce610 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78   {.  struct unix
ce620 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b 65 79 3b  LockKey lockKey;
ce630 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b       /* The look
ce640 75 70 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  up key */.  int 
ce650 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  cnt;            
ce660 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
ce670 75 6d 62 65 72 20 6f 66 20 53 48 41 52 45 44 20  umber of SHARED 
ce680 6c 6f 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20 20  locks held */.  
ce690 69 6e 74 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20  int locktype;   
ce6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce6b0 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 41 52 45 44  /* One of SHARED
ce6c0 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 56 45 44 5f  _LOCK, RESERVED_
ce6d0 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a 20 20 69  LOCK etc. */.  i
ce6e0 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20  nt nRef;        
ce6f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ce700 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e  * Number of poin
ce710 74 65 72 73 20 74 6f 20 74 68 69 73 20 73 74 72  ters to this str
ce720 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72 75  ucture */.  stru
ce730 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ct unixLockInfo 
ce740 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a 20 4c  *pNext;     /* L
ce750 69 73 74 20 6f 66 20 61 6c 6c 20 75 6e 69 78 4c  ist of all unixL
ce760 6f 63 6b 49 6e 66 6f 20 6f 62 6a 65 63 74 73 20  ockInfo objects 
ce770 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78  */.  struct unix
ce780 4c 6f 63 6b 49 6e 66 6f 20 2a 70 50 72 65 76 3b  LockInfo *pPrev;
ce790 20 20 20 20 20 2f 2a 20 20 20 20 2e 2e 2e 2e 20       /*    .... 
ce7a0 64 6f 75 62 6c 79 20 6c 69 6e 6b 65 64 20 2a 2f  doubly linked */
ce7b0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  .};../*.** An in
ce7c0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
ce7d0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
ce7e0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  e is allocated f
ce7f0 6f 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20  or each open.** 
ce800 69 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74 72  inode.  This str
ce810 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61  ucture keeps tra
ce820 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  ck of the number
ce830 20 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61   of locks on tha
ce840 74 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66 20  t.** inode.  If 
ce850 61 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65 6d  a close is attem
ce860 70 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e 20  pted against an 
ce870 69 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68 6f  inode that is ho
ce880 6c 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c 20  lding.** locks, 
ce890 74 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65 66  the close is def
ce8a0 65 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20  erred until all 
ce8b0 6c 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20 61  locks clear by a
ce8c0 64 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c  dding the.** fil
ce8d0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20  e descriptor to 
ce8e0 62 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68 65  be closed to the
ce8f0 20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a 2a   pending list..*
ce900 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 20 43 6f 6e 73  *.** TODO:  Cons
ce910 69 64 65 72 20 63 68 61 6e 67 69 6e 67 20 74 68  ider changing th
ce920 69 73 20 73 6f 20 74 68 61 74 20 74 68 65 72 65  is so that there
ce930 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c   is only a singl
ce940 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69  e file.** descri
ce950 70 74 6f 72 20 66 6f 72 20 65 61 63 68 20 6f 70  ptor for each op
ce960 65 6e 20 66 69 6c 65 2c 20 65 76 65 6e 20 77 68  en file, even wh
ce970 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65 64 20  en it is opened 
ce980 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 0a  multiple times..
ce990 2a 2a 20 54 68 65 20 63 6c 6f 73 65 28 29 20 73  ** The close() s
ce9a0 79 73 74 65 6d 20 63 61 6c 6c 20 77 6f 75 6c 64  ystem call would
ce9b0 20 6f 6e 6c 79 20 6f 63 63 75 72 20 77 68 65 6e   only occur when
ce9c0 20 74 68 65 20 6c 61 73 74 20 64 61 74 61 62 61   the last databa
ce9d0 73 65 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  se.** using the 
ce9e0 66 69 6c 65 20 63 6c 6f 73 65 73 2e 0a 2a 2f 0a  file closes..*/.
ce9f0 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43  struct unixOpenC
cea00 6e 74 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e  nt {.  struct un
cea10 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49 64 3b  ixFileId fileId;
cea20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 70     /* The lookup
cea30 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 52   key */.  int nR
cea40 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
cea50 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
cea60 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  of pointers to t
cea70 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
cea80 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20 20  .  int nLock;   
cea90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ceaa0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 75 74 73  * Number of outs
ceab0 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 20 2a 2f  tanding locks */
ceac0 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20  .  UnixUnusedFd 
cead0 2a 70 55 6e 75 73 65 64 3b 20 20 20 20 20 20 2f  *pUnused;      /
ceae0 2a 20 55 6e 75 73 65 64 20 66 69 6c 65 20 64 65  * Unused file de
ceaf0 73 63 72 69 70 74 6f 72 73 20 74 6f 20 63 6c 6f  scriptors to clo
ceb00 73 65 20 2a 2f 0a 23 69 66 20 4f 53 5f 56 58 57  se */.#if OS_VXW
ceb10 4f 52 4b 53 0a 20 20 73 65 6d 5f 74 20 2a 70 53  ORKS.  sem_t *pS
ceb20 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
ceb30 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 64          /* Named
ceb40 20 50 4f 53 49 58 20 73 65 6d 61 70 68 6f 72 65   POSIX semaphore
ceb50 20 2a 2f 0a 20 20 63 68 61 72 20 61 53 65 6d 4e   */.  char aSemN
ceb60 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ame[MAX_PATHNAME
ceb70 2b 32 5d 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  +2];   /* Name o
ceb80 66 20 74 68 61 74 20 73 65 6d 61 70 68 6f 72 65  f that semaphore
ceb90 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 73 74 72   */.#endif.  str
ceba0 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20  uct unixOpenCnt 
cebb0 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20  *pNext, *pPrev; 
cebc0 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c    /* List of all
cebd0 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a   unixOpenCnt obj
cebe0 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  ects */.};../*.*
cebf0 2a 20 4c 69 73 74 73 20 6f 66 20 61 6c 6c 20 75  * Lists of all u
cec00 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20  nixLockInfo and 
cec10 75 6e 69 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65  unixOpenCnt obje
cec20 63 74 73 2e 20 20 54 68 65 73 65 20 75 73 65 64  cts.  These used
cec30 20 74 6f 20 62 65 20 68 61 73 68 0a 2a 2a 20 74   to be hash.** t
cec40 61 62 6c 65 73 2e 20 20 42 75 74 20 74 68 65 20  ables.  But the 
cec50 6e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65 63 74  number of object
cec60 73 20 69 73 20 72 61 72 65 6c 79 20 6d 6f 72 65  s is rarely more
cec70 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e 20 61 6e   than a dozen an
cec80 64 0a 2a 2a 20 6e 65 76 65 72 20 65 78 63 65 65  d.** never excee
cec90 64 73 20 61 20 66 65 77 20 74 68 6f 75 73 61 6e  ds a few thousan
ceca0 64 2e 20 20 41 6e 64 20 6c 6f 6f 6b 75 70 20 69  d.  And lookup i
cecb0 73 20 6e 6f 74 20 6f 6e 20 61 20 63 72 69 74 69  s not on a criti
cecc0 63 61 6c 0a 2a 2a 20 70 61 74 68 20 73 6f 20 61  cal.** path so a
cecd0 20 73 69 6d 70 6c 65 20 6c 69 6e 6b 65 64 20 6c   simple linked l
cece0 69 73 74 20 77 69 6c 6c 20 73 75 66 66 69 63 65  ist will suffice
cecf0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
ced00 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ct unixLockInfo 
ced10 2a 6c 6f 63 6b 4c 69 73 74 20 3d 20 30 3b 0a 73  *lockList = 0;.s
ced20 74 61 74 69 63 20 73 74 72 75 63 74 20 75 6e 69  tatic struct uni
ced30 78 4f 70 65 6e 43 6e 74 20 2a 6f 70 65 6e 4c 69  xOpenCnt *openLi
ced40 73 74 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 54  st = 0;../*.** T
ced50 68 69 73 20 76 61 72 69 61 62 6c 65 20 72 65 6d  his variable rem
ced60 65 6d 62 65 72 73 20 77 68 65 74 68 65 72 20 6f  embers whether o
ced70 72 20 6e 6f 74 20 74 68 72 65 61 64 73 20 63 61  r not threads ca
ced80 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  n override each 
ced90 6f 74 68 65 72 73 0a 2a 2a 20 6c 6f 63 6b 73 2e  others.** locks.
ceda0 0a 2a 2a 0a 2a 2a 20 20 20 20 30 3a 20 20 4e 6f  .**.**    0:  No
cedb0 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e 6e 6f  .  Threads canno
cedc0 74 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  t override each 
cedd0 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28  others locks.  (
cede0 4c 69 6e 75 78 54 68 72 65 61 64 73 29 0a 2a 2a  LinuxThreads).**
cedf0 20 20 20 20 31 3a 20 20 59 65 73 2e 20 20 54 68      1:  Yes.  Th
cee00 72 65 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69  reads can overri
cee10 64 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c  de each others l
cee20 6f 63 6b 73 2e 20 20 28 50 6f 73 69 78 20 26 20  ocks.  (Posix & 
cee30 4e 4c 50 54 29 0a 2a 2a 20 20 20 2d 31 3a 20 20  NLPT).**   -1:  
cee40 57 65 20 64 6f 6e 27 74 20 6b 6e 6f 77 20 79 65  We don't know ye
cee50 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 6f 6d 65  t..**.** On some
cee60 20 73 79 73 74 65 6d 73 2c 20 77 65 20 6b 6e 6f   systems, we kno
cee70 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  w at compile-tim
cee80 65 20 69 66 20 74 68 72 65 61 64 73 20 63 61 6e  e if threads can
cee90 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 0a 2a   override each.*
ceea0 2a 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20  * others locks. 
ceeb0 20 4f 6e 20 74 68 6f 73 65 20 73 79 73 74 65 6d   On those system
ceec0 73 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 54 48  s, the SQLITE_TH
ceed0 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f  READ_OVERRIDE_LO
ceee0 43 4b 20 6d 61 63 72 6f 0a 2a 2a 20 77 69 6c 6c  CK macro.** will
ceef0 20 62 65 20 73 65 74 20 61 70 70 72 6f 70 72 69   be set appropri
cef00 61 74 65 6c 79 2e 20 20 4f 6e 20 6f 74 68 65 72  ately.  On other
cef10 20 73 79 73 74 65 6d 73 2c 20 77 65 20 68 61 76   systems, we hav
cef20 65 20 74 6f 20 63 68 65 63 6b 20 61 74 0a 2a 2a  e to check at.**
cef30 20 72 75 6e 74 69 6d 65 2e 20 20 4f 6e 20 74 68   runtime.  On th
cef40 65 73 65 20 6c 61 74 74 65 72 20 73 79 73 74 65  ese latter syste
cef50 6d 73 2c 20 53 51 4c 54 49 45 5f 54 48 52 45 41  ms, SQLTIE_THREA
cef60 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20  D_OVERRIDE_LOCK 
cef70 69 73 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64 2e  is.** undefined.
cef80 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69  .**.** This vari
cef90 61 62 6c 65 20 6e 6f 72 6d 61 6c 6c 79 20 68 61  able normally ha
cefa0 73 20 66 69 6c 65 20 73 63 6f 70 65 20 6f 6e 6c  s file scope onl
cefb0 79 2e 20 20 42 75 74 20 64 75 72 69 6e 67 20 74  y.  But during t
cefc0 65 73 74 69 6e 67 2c 20 77 65 20 6d 61 6b 65 0a  esting, we make.
cefd0 2a 2a 20 69 74 20 61 20 67 6c 6f 62 61 6c 20 73  ** it a global s
cefe0 6f 20 74 68 61 74 20 74 68 65 20 74 65 73 74 20  o that the test 
ceff0 63 6f 64 65 20 63 61 6e 20 63 68 61 6e 67 65 20  code can change 
cf000 69 74 73 20 76 61 6c 75 65 20 69 6e 20 6f 72 64  its value in ord
cf010 65 72 20 74 6f 20 76 65 72 69 66 79 0a 2a 2a 20  er to verify.** 
cf020 74 68 61 74 20 74 68 65 20 72 69 67 68 74 20 73  that the right s
cf030 74 75 66 66 20 68 61 70 70 65 6e 73 20 69 6e 20  tuff happens in 
cf040 65 69 74 68 65 72 20 63 61 73 65 2e 0a 2a 2f 0a  either case..*/.
cf050 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
cf060 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65 64  DSAFE && defined
cf070 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 23 20 20 69  (__linux__).#  i
cf080 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 48 52  fndef SQLITE_THR
cf090 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43  EAD_OVERRIDE_LOC
cf0a0 4b 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53 51  K.#    define SQ
cf0b0 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52  LITE_THREAD_OVER
cf0c0 52 49 44 45 5f 4c 4f 43 4b 20 2d 31 0a 23 20 20  RIDE_LOCK -1.#  
cf0d0 65 6e 64 69 66 0a 23 20 20 69 66 64 65 66 20 53  endif.#  ifdef S
cf0e0 51 4c 49 54 45 5f 54 45 53 54 0a 69 6e 74 20 74  QLITE_TEST.int t
cf0f0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61  hreadsOverrideEa
cf100 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d 20  chOthersLocks = 
cf110 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f 56  SQLITE_THREAD_OV
cf120 45 52 52 49 44 45 5f 4c 4f 43 4b 3b 0a 23 20 20  ERRIDE_LOCK;.#  
cf130 65 6c 73 65 0a 73 74 61 74 69 63 20 69 6e 74 20  else.static int 
cf140 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45  threadsOverrideE
cf150 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d  achOthersLocks =
cf160 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 5f 4f   SQLITE_THREAD_O
cf170 56 45 52 52 49 44 45 5f 4c 4f 43 4b 3b 0a 23 20  VERRIDE_LOCK;.# 
cf180 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f   endif.#endif../
cf190 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74  *.** This struct
cf1a0 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d  ure holds inform
cf1b0 61 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74  ation passed int
cf1c0 6f 20 69 6e 64 69 76 69 64 75 61 6c 20 74 65 73  o individual tes
cf1d0 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 62 79 20  t.** threads by 
cf1e0 74 68 65 20 74 65 73 74 54 68 72 65 61 64 4c 6f  the testThreadLo
cf1f0 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 29 20  ckingBehavior() 
cf200 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 72 75  routine..*/.stru
cf210 63 74 20 74 68 72 65 61 64 54 65 73 74 44 61 74  ct threadTestDat
cf220 61 20 7b 0a 20 20 69 6e 74 20 66 64 3b 20 20 20  a {.  int fd;   
cf230 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
cf240 46 69 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65  File to be locke
cf250 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 66 6c  d */.  struct fl
cf260 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 2f 2a  ock lock;     /*
cf270 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6f 70 65   The locking ope
cf280 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ration */.  int 
cf290 72 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20  result;         
cf2a0 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20     /* Result of 
cf2b0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72  the locking oper
cf2c0 61 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 23 69 66  ation */.};..#if
cf2d0 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
cf2e0 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  FE && defined(__
cf2f0 6c 69 6e 75 78 5f 5f 29 0a 2f 2a 0a 2a 2a 20 54  linux__)./*.** T
cf300 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
cf310 75 73 65 64 20 61 73 20 74 68 65 20 6d 61 69 6e  used as the main
cf320 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 20 74   routine for a t
cf330 68 72 65 61 64 20 6c 61 75 6e 63 68 65 64 20 62  hread launched b
cf340 79 0a 2a 2a 20 74 65 73 74 54 68 72 65 61 64 4c  y.** testThreadL
cf350 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 29  ockingBehavior()
cf360 2e 20 49 74 20 74 65 73 74 73 20 77 68 65 74 68  . It tests wheth
cf370 65 72 20 74 68 65 20 73 68 61 72 65 64 2d 6c 6f  er the shared-lo
cf380 63 6b 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 62  ck obtained.** b
cf390 79 20 74 68 65 20 6d 61 69 6e 20 74 68 72 65 61  y the main threa
cf3a0 64 20 69 6e 20 74 65 73 74 54 68 72 65 61 64 4c  d in testThreadL
cf3b0 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 29  ockingBehavior()
cf3c0 20 63 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20   conflicts with 
cf3d0 61 0a 2a 2a 20 68 79 70 6f 74 68 65 74 69 63 61  a.** hypothetica
cf3e0 6c 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 62 74  l write-lock obt
cf3f0 61 69 6e 65 64 20 62 79 20 74 68 69 73 20 74 68  ained by this th
cf400 72 65 61 64 20 6f 6e 20 74 68 65 20 73 61 6d 65  read on the same
cf410 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   file..**.** The
cf420 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73 20 6e   write-lock is n
cf430 6f 74 20 61 63 74 75 61 6c 6c 79 20 61 63 71 75  ot actually acqu
cf440 69 72 65 64 2c 20 61 73 20 74 68 69 73 20 69 73  ired, as this is
cf450 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 69 66   not possible if
cf460 20 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 69 73   .** the file is
cf470 20 6f 70 65 6e 20 69 6e 20 72 65 61 64 2d 6f 6e   open in read-on
cf480 6c 79 20 6d 6f 64 65 20 28 73 65 65 20 74 69 63  ly mode (see tic
cf490 6b 65 74 20 23 33 34 37 32 29 2e 0a 2a 2f 20 0a  ket #3472)..*/ .
cf4a0 73 74 61 74 69 63 20 76 6f 69 64 20 2a 74 68 72  static void *thr
cf4b0 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 28 76  eadLockingTest(v
cf4c0 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 74  oid *pArg){.  st
cf4d0 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44  ruct threadTestD
cf4e0 61 74 61 20 2a 70 44 61 74 61 20 3d 20 28 73 74  ata *pData = (st
cf4f0 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74 44  ruct threadTestD
cf500 61 74 61 2a 29 70 41 72 67 3b 0a 20 20 70 44 61  ata*)pArg;.  pDa
cf510 74 61 2d 3e 72 65 73 75 6c 74 20 3d 20 66 63 6e  ta->result = fcn
cf520 74 6c 28 70 44 61 74 61 2d 3e 66 64 2c 20 46 5f  tl(pData->fd, F_
cf530 47 45 54 4c 4b 2c 20 26 70 44 61 74 61 2d 3e 6c  GETLK, &pData->l
cf540 6f 63 6b 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ock);.  return p
cf550 41 72 67 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  Arg;.}.#endif /*
cf560 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
cf570 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  FE && defined(__
cf580 6c 69 6e 75 78 5f 5f 29 20 2a 2f 0a 0a 0a 23 69  linux__) */...#i
cf590 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
cf5a0 41 46 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  AFE && defined(_
cf5b0 5f 6c 69 6e 75 78 5f 5f 29 0a 2f 2a 0a 2a 2a 20  _linux__)./*.** 
cf5c0 54 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 61  This procedure a
cf5d0 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72  ttempts to deter
cf5e0 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20  mine whether or 
cf5f0 6e 6f 74 20 74 68 72 65 61 64 73 0a 2a 2a 20 63  not threads.** c
cf600 61 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68  an override each
cf610 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 74 68   others locks th
cf620 65 6e 20 73 65 74 73 20 74 68 65 20 0a 2a 2a 20  en sets the .** 
cf630 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45  threadsOverrideE
cf640 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 76  achOthersLocks v
cf650 61 72 69 61 62 6c 65 20 61 70 70 72 6f 70 72 69  ariable appropri
cf660 61 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ately..*/.static
cf670 20 76 6f 69 64 20 74 65 73 74 54 68 72 65 61 64   void testThread
cf680 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28  LockingBehavior(
cf690 69 6e 74 20 66 64 5f 6f 72 69 67 29 7b 0a 20 20  int fd_orig){.  
cf6a0 69 6e 74 20 66 64 3b 0a 20 20 69 6e 74 20 72 63  int fd;.  int rc
cf6b0 3b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 61  ;.  struct threa
cf6c0 64 54 65 73 74 44 61 74 61 20 64 3b 0a 20 20 73  dTestData d;.  s
cf6d0 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 3b 0a 20  truct flock l;. 
cf6e0 20 70 74 68 72 65 61 64 5f 74 20 74 3b 0a 0a 20   pthread_t t;.. 
cf6f0 20 66 64 20 3d 20 64 75 70 28 66 64 5f 6f 72 69   fd = dup(fd_ori
cf700 67 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29  g);.  if( fd<0 )
cf710 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65   return;.  memse
cf720 74 28 26 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28  t(&l, 0, sizeof(
cf730 6c 29 29 3b 0a 20 20 6c 2e 6c 5f 74 79 70 65 20  l));.  l.l_type 
cf740 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 6c 2e 6c  = F_RDLCK;.  l.l
cf750 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 2e 6c 5f  _len = 1;.  l.l_
cf760 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 2e 6c  start = 0;.  l.l
cf770 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53  _whence = SEEK_S
cf780 45 54 3b 0a 20 20 72 63 20 3d 20 66 63 6e 74 6c  ET;.  rc = fcntl
cf790 28 66 64 5f 6f 72 69 67 2c 20 46 5f 53 45 54 4c  (fd_orig, F_SETL
cf7a0 4b 2c 20 26 6c 29 3b 0a 20 20 69 66 28 20 72 63  K, &l);.  if( rc
cf7b0 21 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  !=0 ) return;.  
cf7c0 6d 65 6d 73 65 74 28 26 64 2c 20 30 2c 20 73 69  memset(&d, 0, si
cf7d0 7a 65 6f 66 28 64 29 29 3b 0a 20 20 64 2e 66 64  zeof(d));.  d.fd
cf7e0 20 3d 20 66 64 3b 0a 20 20 64 2e 6c 6f 63 6b 20   = fd;.  d.lock 
cf7f0 3d 20 6c 3b 0a 20 20 64 2e 6c 6f 63 6b 2e 6c 5f  = l;.  d.lock.l_
cf800 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a  type = F_WRLCK;.
cf810 20 20 69 66 28 20 70 74 68 72 65 61 64 5f 63 72    if( pthread_cr
cf820 65 61 74 65 28 26 74 2c 20 30 2c 20 74 68 72 65  eate(&t, 0, thre
cf830 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 2c 20 26  adLockingTest, &
cf840 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 74 68  d)==0 ){.    pth
cf850 72 65 61 64 5f 6a 6f 69 6e 28 74 2c 20 30 29 3b  read_join(t, 0);
cf860 0a 20 20 7d 0a 20 20 63 6c 6f 73 65 28 66 64 29  .  }.  close(fd)
cf870 3b 0a 20 20 69 66 28 20 64 2e 72 65 73 75 6c 74  ;.  if( d.result
cf880 21 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  !=0 ) return;.  
cf890 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45  threadsOverrideE
cf8a0 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d  achOthersLocks =
cf8b0 20 28 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 3d   (d.lock.l_type=
cf8c0 3d 46 5f 55 4e 4c 43 4b 29 3b 0a 7d 0a 23 65 6e  =F_UNLCK);.}.#en
cf8d0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48  dif /* SQLITE_TH
cf8e0 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69  READSAFE && defi
cf8f0 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 2a  ned(__linux__) *
cf900 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  /../*.** Release
cf910 20 61 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20   a unixLockInfo 
cf920 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69 6f  structure previo
cf930 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62  usly allocated b
cf940 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29  y findLockInfo()
cf950 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65  ..**.** The mute
cf960 78 20 65 6e 74 65 72 65 64 20 75 73 69 6e 67 20  x entered using 
cf970 74 68 65 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  the unixEnterMut
cf980 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 20 6d 75  ex() function mu
cf990 73 74 20 62 65 20 68 65 6c 64 0a 2a 2a 20 77 68  st be held.** wh
cf9a0 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
cf9b0 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73   is called..*/.s
cf9c0 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61  tatic void relea
cf9d0 73 65 4c 6f 63 6b 49 6e 66 6f 28 73 74 72 75 63  seLockInfo(struc
cf9e0 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a  t unixLockInfo *
cf9f0 70 4c 6f 63 6b 29 7b 0a 20 20 61 73 73 65 72 74  pLock){.  assert
cfa00 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64 28  ( unixMutexHeld(
cfa10 29 20 29 3b 0a 20 20 69 66 28 20 70 4c 6f 63 6b  ) );.  if( pLock
cfa20 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e   ){.    pLock->n
cfa30 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  Ref--;.    if( p
cfa40 4c 6f 63 6b 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b  Lock->nRef==0 ){
cfa50 0a 20 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b  .      if( pLock
cfa60 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 20  ->pPrev ){.     
cfa70 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
cfa80 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d  ->pPrev->pNext==
cfa90 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20 20 20  pLock );.       
cfaa0 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70   pLock->pPrev->p
cfab0 4e 65 78 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e  Next = pLock->pN
cfac0 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ext;.      }else
cfad0 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
cfae0 28 20 6c 6f 63 6b 4c 69 73 74 3d 3d 70 4c 6f 63  ( lockList==pLoc
cfaf0 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63  k );.        loc
cfb00 6b 4c 69 73 74 20 3d 20 70 4c 6f 63 6b 2d 3e 70  kList = pLock->p
cfb10 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Next;.      }.  
cfb20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70      if( pLock->p
cfb30 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Next ){.        
cfb40 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 70  assert( pLock->p
cfb50 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 4c 6f  Next->pPrev==pLo
cfb60 63 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4c  ck );.        pL
cfb70 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  ock->pNext->pPre
cfb80 76 20 3d 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76  v = pLock->pPrev
cfb90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
cfba0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 6f  sqlite3_free(pLo
cfbb0 63 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  ck);.    }.  }.}
cfbc0 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
cfbd0 61 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74  a unixOpenCnt st
cfbe0 72 75 63 74 75 72 65 20 70 72 65 76 69 6f 75 73  ructure previous
cfbf0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  ly allocated by 
cfc00 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 0a  findLockInfo()..
cfc10 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75 74 65 78 20  **.** The mutex 
cfc20 65 6e 74 65 72 65 64 20 75 73 69 6e 67 20 74 68  entered using th
cfc30 65 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78  e unixEnterMutex
cfc40 28 29 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  () function must
cfc50 20 62 65 20 68 65 6c 64 0a 2a 2a 20 77 68 65 6e   be held.** when
cfc60 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
cfc70 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  s called..*/.sta
cfc80 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65  tic void release
cfc90 4f 70 65 6e 43 6e 74 28 73 74 72 75 63 74 20 75  OpenCnt(struct u
cfca0 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65  nixOpenCnt *pOpe
cfcb0 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 75 6e  n){.  assert( un
cfcc0 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b  ixMutexHeld() );
cfcd0 0a 20 20 69 66 28 20 70 4f 70 65 6e 20 29 7b 0a  .  if( pOpen ){.
cfce0 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2d      pOpen->nRef-
cfcf0 2d 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 65 6e  -;.    if( pOpen
cfd00 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20  ->nRef==0 ){.   
cfd10 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 70 50     if( pOpen->pP
cfd20 72 65 76 20 29 7b 0a 20 20 20 20 20 20 20 20 61  rev ){.        a
cfd30 73 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 70 50  ssert( pOpen->pP
cfd40 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 4f 70 65  rev->pNext==pOpe
cfd50 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70  n );.        pOp
cfd60 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74  en->pPrev->pNext
cfd70 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b   = pOpen->pNext;
cfd80 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
cfd90 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 70        assert( op
cfda0 65 6e 4c 69 73 74 3d 3d 70 4f 70 65 6e 20 29 3b  enList==pOpen );
cfdb0 0a 20 20 20 20 20 20 20 20 6f 70 65 6e 4c 69 73  .        openLis
cfdc0 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74  t = pOpen->pNext
cfdd0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
cfde0 69 66 28 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74  if( pOpen->pNext
cfdf0 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
cfe00 72 74 28 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74  rt( pOpen->pNext
cfe10 2d 3e 70 50 72 65 76 3d 3d 70 4f 70 65 6e 20 29  ->pPrev==pOpen )
cfe20 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65 6e 2d  ;.        pOpen-
cfe30 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20  >pNext->pPrev = 
cfe40 70 4f 70 65 6e 2d 3e 70 50 72 65 76 3b 0a 20 20  pOpen->pPrev;.  
cfe50 20 20 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45      }.#if SQLITE
cfe60 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64  _THREADSAFE && d
cfe70 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f  efined(__linux__
cfe80 29 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ).      assert( 
cfe90 21 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20  !pOpen->pUnused 
cfea0 7c 7c 20 74 68 72 65 61 64 73 4f 76 65 72 72 69  || threadsOverri
cfeb0 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b  deEachOthersLock
cfec0 73 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a  s==0 );.#endif..
cfed0 20 20 20 20 20 20 2f 2a 20 49 66 20 70 4f 70 65        /* If pOpe
cfee0 6e 2d 3e 70 55 6e 75 73 65 64 20 69 73 20 6e 6f  n->pUnused is no
cfef0 74 20 6e 75 6c 6c 2c 20 74 68 65 6e 20 6d 65 6d  t null, then mem
cff00 6f 72 79 20 61 6e 64 20 66 69 6c 65 2d 64 65 73  ory and file-des
cff10 63 72 69 70 74 6f 72 73 0a 20 20 20 20 20 20 2a  criptors.      *
cff20 2a 20 61 72 65 20 6c 65 61 6b 65 64 2e 0a 20 20  * are leaked..  
cff30 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
cff40 54 68 69 73 20 77 69 6c 6c 20 6f 6e 6c 79 20 68  This will only h
cff50 61 70 70 65 6e 20 69 66 2c 20 75 6e 64 65 72 20  appen if, under 
cff60 4c 69 6e 75 78 74 68 72 65 61 64 73 2c 20 74 68  Linuxthreads, th
cff70 65 20 75 73 65 72 20 68 61 73 20 6f 70 65 6e 65  e user has opene
cff80 64 0a 20 20 20 20 20 20 2a 2a 20 61 20 74 72 61  d.      ** a tra
cff90 6e 73 61 63 74 69 6f 6e 20 69 6e 20 6f 6e 65 20  nsaction in one 
cffa0 74 68 72 65 61 64 2c 20 74 68 65 6e 20 61 74 74  thread, then att
cffb0 65 6d 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74  empts to close t
cffc0 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  he database.    
cffd0 20 20 2a 2a 20 68 61 6e 64 6c 65 20 66 72 6f 6d    ** handle from
cffe0 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20   another thread 
cfff0 28 77 69 74 68 6f 75 74 20 66 69 72 73 74 20 75  (without first u
d0000 6e 6c 6f 63 6b 69 6e 67 20 74 68 65 20 64 62 20  nlocking the db 
d0010 66 69 6c 65 29 2e 0a 20 20 20 20 20 20 2a 2a 20  file)..      ** 
d0020 54 68 69 73 20 69 73 20 61 20 6d 69 73 75 73 65  This is a misuse
d0030 2e 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  .  */.      sqli
d0040 74 65 33 5f 66 72 65 65 28 70 4f 70 65 6e 29 3b  te3_free(pOpen);
d0050 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
d0060 0a 2a 2a 20 47 69 76 65 6e 20 61 20 66 69 6c 65  .** Given a file
d0070 20 64 65 73 63 72 69 70 74 6f 72 2c 20 6c 6f 63   descriptor, loc
d0080 61 74 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f  ate unixLockInfo
d0090 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e 74   and unixOpenCnt
d00a0 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74   structures that
d00b0 0a 2a 2a 20 64 65 73 63 72 69 62 65 73 20 74 68  .** describes th
d00c0 61 74 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  at file descript
d00d0 6f 72 2e 20 20 43 72 65 61 74 65 20 6e 65 77 20  or.  Create new 
d00e0 6f 6e 65 73 20 69 66 20 6e 65 63 65 73 73 61 72  ones if necessar
d00f0 79 2e 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72  y.  The.** retur
d0100 6e 20 76 61 6c 75 65 73 20 6d 69 67 68 74 20 62  n values might b
d0110 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20  e uninitialized 
d0120 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
d0130 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 75  rs..**.** The mu
d0140 74 65 78 20 65 6e 74 65 72 65 64 20 75 73 69 6e  tex entered usin
d0150 67 20 74 68 65 20 75 6e 69 78 45 6e 74 65 72 4d  g the unixEnterM
d0160 75 74 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 20  utex() function 
d0170 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 2a 2a 20  must be held.** 
d0180 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
d0190 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  on is called..**
d01a0 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 61 70  .** Return an ap
d01b0 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20  propriate error 
d01c0 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  code..*/.static 
d01d0 69 6e 74 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f  int findLockInfo
d01e0 28 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  (.  unixFile *pF
d01f0 69 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ile,            
d0200 20 20 20 2f 2a 20 55 6e 69 78 20 66 69 6c 65 20     /* Unix file 
d0210 77 69 74 68 20 66 69 6c 65 20 64 65 73 63 20 75  with file desc u
d0220 73 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20 2a  sed in the key *
d0230 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c  /.  struct unixL
d0240 6f 63 6b 49 6e 66 6f 20 2a 2a 70 70 4c 6f 63 6b  ockInfo **ppLock
d0250 2c 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65  ,  /* Return the
d0260 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74   unixLockInfo st
d0270 72 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a  ructure here */.
d0280 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65    struct unixOpe
d0290 6e 43 6e 74 20 2a 2a 70 70 4f 70 65 6e 20 20 20  nCnt **ppOpen   
d02a0 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 75   /* Return the u
d02b0 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75 63  nixOpenCnt struc
d02c0 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ture here */.){.
d02d0 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
d02e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d02f0 20 2f 2a 20 53 79 73 74 65 6d 20 63 61 6c 6c 20   /* System call 
d0300 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  return code */. 
d0310 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20   int fd;        
d0320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d0330 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63  /* The file desc
d0340 72 69 70 74 6f 72 20 66 6f 72 20 70 46 69 6c 65  riptor for pFile
d0350 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69   */.  struct uni
d0360 78 4c 6f 63 6b 4b 65 79 20 6c 6f 63 6b 4b 65 79  xLockKey lockKey
d0370 3b 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20 6b  ;    /* Lookup k
d0380 65 79 20 66 6f 72 20 74 68 65 20 75 6e 69 78 4c  ey for the unixL
d0390 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72  ockInfo structur
d03a0 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e  e */.  struct un
d03b0 69 78 46 69 6c 65 49 64 20 66 69 6c 65 49 64 3b  ixFileId fileId;
d03c0 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 75 70 20        /* Lookup 
d03d0 6b 65 79 20 66 6f 72 20 74 68 65 20 75 6e 69 78  key for the unix
d03e0 4f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 20 2a  OpenCnt struct *
d03f0 2f 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20  /.  struct stat 
d0400 73 74 61 74 62 75 66 3b 20 20 20 20 20 20 20 20  statbuf;        
d0410 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20     /* Low-level 
d0420 66 69 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  file information
d0430 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e 69   */.  struct uni
d0440 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b  xLockInfo *pLock
d0450 20 3d 20 30 3b 2f 2a 20 43 61 6e 64 69 64 61 74   = 0;/* Candidat
d0460 65 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 6f  e unixLockInfo o
d0470 62 6a 65 63 74 20 2a 2f 0a 20 20 73 74 72 75 63  bject */.  struc
d0480 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70  t unixOpenCnt *p
d0490 4f 70 65 6e 3b 20 20 20 20 20 2f 2a 20 43 61 6e  Open;     /* Can
d04a0 64 69 64 61 74 65 20 75 6e 69 78 4f 70 65 6e 43  didate unixOpenC
d04b0 6e 74 20 6f 62 6a 65 63 74 20 2a 2f 0a 0a 20 20  nt object */..  
d04c0 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65  assert( unixMute
d04d0 78 48 65 6c 64 28 29 20 29 3b 0a 0a 20 20 2f 2a  xHeld() );..  /*
d04e0 20 47 65 74 20 6c 6f 77 2d 6c 65 76 65 6c 20 69   Get low-level i
d04f0 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
d0500 20 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 77   the file that w
d0510 65 20 63 61 6e 20 75 73 65 64 20 74 6f 0a 20 20  e can used to.  
d0520 2a 2a 20 63 72 65 61 74 65 20 61 20 75 6e 69 71  ** create a uniq
d0530 75 65 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20  ue name for the 
d0540 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 66 64 20  file..  */.  fd 
d0550 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 72 63  = pFile->h;.  rc
d0560 20 3d 20 66 73 74 61 74 28 66 64 2c 20 26 73 74   = fstat(fd, &st
d0570 61 74 62 75 66 29 3b 0a 20 20 69 66 28 20 72 63  atbuf);.  if( rc
d0580 21 3d 30 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  !=0 ){.    pFile
d0590 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
d05a0 72 6e 6f 3b 0a 23 69 66 64 65 66 20 45 4f 56 45  rno;.#ifdef EOVE
d05b0 52 46 4c 4f 57 0a 20 20 20 20 69 66 28 20 70 46  RFLOW.    if( pF
d05c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3d 3d  ile->lastErrno==
d05d0 45 4f 56 45 52 46 4c 4f 57 20 29 20 72 65 74 75  EOVERFLOW ) retu
d05e0 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 3b  rn SQLITE_NOLFS;
d05f0 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75  .#endif.    retu
d0600 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b  rn SQLITE_IOERR;
d0610 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 5f 5f 41  .  }..#ifdef __A
d0620 50 50 4c 45 5f 5f 0a 20 20 2f 2a 20 4f 6e 20 4f  PPLE__.  /* On O
d0630 53 20 58 20 6f 6e 20 61 6e 20 6d 73 64 6f 73 20  S X on an msdos 
d0640 66 69 6c 65 73 79 73 74 65 6d 2c 20 74 68 65 20  filesystem, the 
d0650 69 6e 6f 64 65 20 6e 75 6d 62 65 72 20 69 73 20  inode number is 
d0660 72 65 70 6f 72 74 65 64 0a 20 20 2a 2a 20 69 6e  reported.  ** in
d0670 63 6f 72 72 65 63 74 6c 79 20 66 6f 72 20 7a 65  correctly for ze
d0680 72 6f 2d 73 69 7a 65 20 66 69 6c 65 73 2e 20 20  ro-size files.  
d0690 53 65 65 20 74 69 63 6b 65 74 20 23 33 32 36 30  See ticket #3260
d06a0 2e 20 20 54 6f 20 77 6f 72 6b 0a 20 20 2a 2a 20  .  To work.  ** 
d06b0 61 72 6f 75 6e 64 20 74 68 69 73 20 70 72 6f 62  around this prob
d06c0 6c 65 6d 20 28 77 65 20 63 6f 6e 73 69 64 65 72  lem (we consider
d06d0 20 69 74 20 61 20 62 75 67 20 69 6e 20 4f 53 20   it a bug in OS 
d06e0 58 2c 20 6e 6f 74 20 53 51 4c 69 74 65 29 0a 20  X, not SQLite). 
d06f0 20 2a 2a 20 77 65 20 61 6c 77 61 79 73 20 69 6e   ** we always in
d0700 63 72 65 61 73 65 20 74 68 65 20 66 69 6c 65 20  crease the file 
d0710 73 69 7a 65 20 74 6f 20 31 20 62 79 20 77 72 69  size to 1 by wri
d0720 74 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 62 79  ting a single by
d0730 74 65 0a 20 20 2a 2a 20 70 72 69 6f 72 20 74 6f  te.  ** prior to
d0740 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 69   accessing the i
d0750 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 20 54 68  node number.  Th
d0760 65 20 6f 6e 65 20 62 79 74 65 20 77 72 69 74 74  e one byte writt
d0770 65 6e 20 69 73 0a 20 20 2a 2a 20 61 6e 20 41 53  en is.  ** an AS
d0780 43 49 49 20 27 53 27 20 63 68 61 72 61 63 74 65  CII 'S' characte
d0790 72 20 77 68 69 63 68 20 61 6c 73 6f 20 68 61 70  r which also hap
d07a0 70 65 6e 73 20 74 6f 20 62 65 20 74 68 65 20 66  pens to be the f
d07b0 69 72 73 74 20 62 79 74 65 0a 20 20 2a 2a 20 69  irst byte.  ** i
d07c0 6e 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 20  n the header of 
d07d0 65 76 65 72 79 20 53 51 4c 69 74 65 20 64 61 74  every SQLite dat
d07e0 61 62 61 73 65 2e 20 20 49 6e 20 74 68 69 73 20  abase.  In this 
d07f0 77 61 79 2c 20 69 66 20 74 68 65 72 65 0a 20 20  way, if there.  
d0800 2a 2a 20 69 73 20 61 20 72 61 63 65 20 63 6f 6e  ** is a race con
d0810 64 69 74 69 6f 6e 20 73 75 63 68 20 74 68 61 74  dition such that
d0820 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20   another thread 
d0830 68 61 73 20 61 6c 72 65 61 64 79 20 70 6f 70 75  has already popu
d0840 6c 61 74 65 64 0a 20 20 2a 2a 20 74 68 65 20 66  lated.  ** the f
d0850 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  irst page of the
d0860 20 64 61 74 61 62 61 73 65 2c 20 6e 6f 20 64 61   database, no da
d0870 6d 61 67 65 20 69 73 20 64 6f 6e 65 2e 0a 20 20  mage is done..  
d0880 2a 2f 0a 20 20 69 66 28 20 73 74 61 74 62 75 66  */.  if( statbuf
d0890 2e 73 74 5f 73 69 7a 65 3d 3d 30 20 29 7b 0a 20  .st_size==0 ){. 
d08a0 20 20 20 72 63 20 3d 20 77 72 69 74 65 28 66 64     rc = write(fd
d08b0 2c 20 22 53 22 2c 20 31 29 3b 0a 20 20 20 20 69  , "S", 1);.    i
d08c0 66 28 20 72 63 21 3d 31 20 29 7b 0a 20 20 20 20  f( rc!=1 ){.    
d08d0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d08e0 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20 20 20  IOERR;.    }.   
d08f0 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 2c 20   rc = fstat(fd, 
d0900 26 73 74 61 74 62 75 66 29 3b 0a 20 20 20 20 69  &statbuf);.    i
d0910 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20  f( rc!=0 ){.    
d0920 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d0930 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d0940 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d0950 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20 20 7d  IOERR;.    }.  }
d0960 0a 23 65 6e 64 69 66 0a 0a 20 20 6d 65 6d 73 65  .#endif..  memse
d0970 74 28 26 6c 6f 63 6b 4b 65 79 2c 20 30 2c 20 73  t(&lockKey, 0, s
d0980 69 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 3b  izeof(lockKey));
d0990 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 64  .  lockKey.fid.d
d09a0 65 76 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f  ev = statbuf.st_
d09b0 64 65 76 3b 0a 23 69 66 20 4f 53 5f 56 58 57 4f  dev;.#if OS_VXWO
d09c0 52 4b 53 0a 20 20 6c 6f 63 6b 4b 65 79 2e 66 69  RKS.  lockKey.fi
d09d0 64 2e 70 49 64 20 3d 20 70 46 69 6c 65 2d 3e 70  d.pId = pFile->p
d09e0 49 64 3b 0a 23 65 6c 73 65 0a 20 20 6c 6f 63 6b  Id;.#else.  lock
d09f0 4b 65 79 2e 66 69 64 2e 69 6e 6f 20 3d 20 73 74  Key.fid.ino = st
d0a00 61 74 62 75 66 2e 73 74 5f 69 6e 6f 3b 0a 23 65  atbuf.st_ino;.#e
d0a10 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
d0a20 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65  THREADSAFE && de
d0a30 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29  fined(__linux__)
d0a40 0a 20 20 69 66 28 20 74 68 72 65 61 64 73 4f 76  .  if( threadsOv
d0a50 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73  errideEachOthers
d0a60 4c 6f 63 6b 73 3c 30 20 29 7b 0a 20 20 20 20 74  Locks<0 ){.    t
d0a70 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67  estThreadLocking
d0a80 42 65 68 61 76 69 6f 72 28 66 64 29 3b 0a 20 20  Behavior(fd);.  
d0a90 7d 0a 20 20 6c 6f 63 6b 4b 65 79 2e 74 69 64 20  }.  lockKey.tid 
d0aa0 3d 20 74 68 72 65 61 64 73 4f 76 65 72 72 69 64  = threadsOverrid
d0ab0 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73  eEachOthersLocks
d0ac0 20 3f 20 30 20 3a 20 70 74 68 72 65 61 64 5f 73   ? 0 : pthread_s
d0ad0 65 6c 66 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20  elf();.#endif.  
d0ae0 66 69 6c 65 49 64 20 3d 20 6c 6f 63 6b 4b 65 79  fileId = lockKey
d0af0 2e 66 69 64 3b 0a 20 20 69 66 28 20 70 70 4c 6f  .fid;.  if( ppLo
d0b00 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 70 4c 6f  ck!=0 ){.    pLo
d0b10 63 6b 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b 0a 20  ck = lockList;. 
d0b20 20 20 20 77 68 69 6c 65 28 20 70 4c 6f 63 6b 20     while( pLock 
d0b30 26 26 20 6d 65 6d 63 6d 70 28 26 6c 6f 63 6b 4b  && memcmp(&lockK
d0b40 65 79 2c 20 26 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  ey, &pLock->lock
d0b50 4b 65 79 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 6b  Key, sizeof(lock
d0b60 4b 65 79 29 29 20 29 7b 0a 20 20 20 20 20 20 70  Key)) ){.      p
d0b70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e  Lock = pLock->pN
d0b80 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ext;.    }.    i
d0b90 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20  f( pLock==0 ){. 
d0ba0 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 73 71 6c       pLock = sql
d0bb0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a  ite3_malloc( siz
d0bc0 65 6f 66 28 2a 70 4c 6f 63 6b 29 20 29 3b 0a 20  eof(*pLock) );. 
d0bd0 20 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d       if( pLock==
d0be0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  0 ){.        rc 
d0bf0 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
d0c00 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69          goto exi
d0c10 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a  t_findlockinfo;.
d0c20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65        }.      me
d0c30 6d 63 70 79 28 26 70 4c 6f 63 6b 2d 3e 6c 6f 63  mcpy(&pLock->loc
d0c40 6b 4b 65 79 2c 26 6c 6f 63 6b 4b 65 79 2c 73 69  kKey,&lockKey,si
d0c50 7a 65 6f 66 28 6c 6f 63 6b 4b 65 79 29 29 3b 0a  zeof(lockKey));.
d0c60 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65        pLock->nRe
d0c70 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4c 6f  f = 1;.      pLo
d0c80 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20  ck->cnt = 0;.   
d0c90 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79     pLock->lockty
d0ca0 70 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c  pe = 0;.      pL
d0cb0 6f 63 6b 2d 3e 70 4e 65 78 74 20 3d 20 6c 6f 63  ock->pNext = loc
d0cc0 6b 4c 69 73 74 3b 0a 20 20 20 20 20 20 70 4c 6f  kList;.      pLo
d0cd0 63 6b 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20  ck->pPrev = 0;. 
d0ce0 20 20 20 20 20 69 66 28 20 6c 6f 63 6b 4c 69 73       if( lockLis
d0cf0 74 20 29 20 6c 6f 63 6b 4c 69 73 74 2d 3e 70 50  t ) lockList->pP
d0d00 72 65 76 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20  rev = pLock;.   
d0d10 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c     lockList = pL
d0d20 6f 63 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ock;.    }else{.
d0d30 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e 52 65        pLock->nRe
d0d40 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  f++;.    }.    *
d0d50 70 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a  ppLock = pLock;.
d0d60 20 20 7d 0a 20 20 69 66 28 20 70 70 4f 70 65 6e    }.  if( ppOpen
d0d70 21 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e  !=0 ){.    pOpen
d0d80 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20 20 20   = openList;.   
d0d90 20 77 68 69 6c 65 28 20 70 4f 70 65 6e 20 26 26   while( pOpen &&
d0da0 20 6d 65 6d 63 6d 70 28 26 66 69 6c 65 49 64 2c   memcmp(&fileId,
d0db0 20 26 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 2c   &pOpen->fileId,
d0dc0 20 73 69 7a 65 6f 66 28 66 69 6c 65 49 64 29 29   sizeof(fileId))
d0dd0 20 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20   ){.      pOpen 
d0de0 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 3b 0a  = pOpen->pNext;.
d0df0 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4f      }.    if( pO
d0e00 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  pen==0 ){.      
d0e10 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f  pOpen = sqlite3_
d0e20 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a  malloc( sizeof(*
d0e30 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20 20 20 20  pOpen) );.      
d0e40 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a  if( pOpen==0 ){.
d0e50 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 4c          releaseL
d0e60 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b 29 3b 0a  ockInfo(pLock);.
d0e70 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
d0e80 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
d0e90 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 66 69 6e     goto exit_fin
d0ea0 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20 20 20 20  dlockinfo;.     
d0eb0 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28   }.      memset(
d0ec0 70 4f 70 65 6e 2c 20 30 2c 20 73 69 7a 65 6f 66  pOpen, 0, sizeof
d0ed0 28 2a 70 4f 70 65 6e 29 29 3b 0a 20 20 20 20 20  (*pOpen));.     
d0ee0 20 70 4f 70 65 6e 2d 3e 66 69 6c 65 49 64 20 3d   pOpen->fileId =
d0ef0 20 66 69 6c 65 49 64 3b 0a 20 20 20 20 20 20 70   fileId;.      p
d0f00 4f 70 65 6e 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  Open->nRef = 1;.
d0f10 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 4e 65        pOpen->pNe
d0f20 78 74 20 3d 20 6f 70 65 6e 4c 69 73 74 3b 0a 20  xt = openList;. 
d0f30 20 20 20 20 20 69 66 28 20 6f 70 65 6e 4c 69 73       if( openLis
d0f40 74 20 29 20 6f 70 65 6e 4c 69 73 74 2d 3e 70 50  t ) openList->pP
d0f50 72 65 76 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20  rev = pOpen;.   
d0f60 20 20 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f     openList = pO
d0f70 70 65 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  pen;.    }else{.
d0f80 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65        pOpen->nRe
d0f90 66 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a  f++;.    }.    *
d0fa0 70 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a  ppOpen = pOpen;.
d0fb0 20 20 7d 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f    }..exit_findlo
d0fc0 63 6b 69 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e  ckinfo:.  return
d0fd0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66   rc;.}../*.** If
d0fe0 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c   we are currentl
d0ff0 79 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74  y in a different
d1000 20 74 68 72 65 61 64 20 74 68 61 6e 20 74 68 65   thread than the
d1010 20 74 68 72 65 61 64 20 74 68 61 74 20 74 68 65   thread that the
d1020 0a 2a 2a 20 75 6e 69 78 46 69 6c 65 20 61 72 67  .** unixFile arg
d1030 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 20 74 6f  ument belongs to
d1040 2c 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20  , then transfer 
d1050 6f 77 6e 65 72 73 68 69 70 20 6f 66 20 74 68 65  ownership of the
d1060 20 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 6f 76 65   unixFile.** ove
d1070 72 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  r to the current
d1080 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41   thread..**.** A
d1090 20 75 6e 69 78 46 69 6c 65 20 69 73 20 6f 6e 6c   unixFile is onl
d10a0 79 20 6f 77 6e 65 64 20 62 79 20 61 20 74 68 72  y owned by a thr
d10b0 65 61 64 20 6f 6e 20 73 79 73 74 65 6d 73 20 74  ead on systems t
d10c0 68 61 74 20 75 73 65 20 4c 69 6e 75 78 54 68 72  hat use LinuxThr
d10d0 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65  eads..**.** Owne
d10e0 72 73 68 69 70 20 74 72 61 6e 73 66 65 72 20 69  rship transfer i
d10f0 73 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69  s only allowed i
d1100 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69  f the unixFile i
d1110 73 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f  s currently unlo
d1120 63 6b 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20  cked..** If the 
d1130 75 6e 69 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b  unixFile is lock
d1140 65 64 20 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73  ed and an owners
d1150 68 69 70 20 69 73 20 77 72 6f 6e 67 2c 20 74 68  hip is wrong, th
d1160 65 6e 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  en return.** SQL
d1170 49 54 45 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c  ITE_MISUSE.  SQL
d1180 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
d1190 65 64 20 69 66 20 65 76 65 72 79 74 68 69 6e 67  ed if everything
d11a0 20 77 6f 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53   works..*/.#if S
d11b0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
d11c0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69   && defined(__li
d11d0 6e 75 78 5f 5f 29 0a 73 74 61 74 69 63 20 69 6e  nux__).static in
d11e0 74 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73  t transferOwners
d11f0 68 69 70 28 75 6e 69 78 46 69 6c 65 20 2a 70 46  hip(unixFile *pF
d1200 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ile){.  int rc;.
d1210 20 20 70 74 68 72 65 61 64 5f 74 20 68 53 65 6c    pthread_t hSel
d1220 66 3b 0a 20 20 69 66 28 20 74 68 72 65 61 64 73  f;.  if( threads
d1230 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
d1240 72 73 4c 6f 63 6b 73 20 29 7b 0a 20 20 20 20 2f  rsLocks ){.    /
d1250 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e  * Ownership tran
d1260 73 66 65 72 73 20 6e 6f 74 20 6e 65 65 64 65 64  sfers not needed
d1270 20 6f 6e 20 74 68 69 73 20 73 79 73 74 65 6d 20   on this system 
d1280 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  */.    return SQ
d1290 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68  LITE_OK;.  }.  h
d12a0 53 65 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73  Self = pthread_s
d12b0 65 6c 66 28 29 3b 0a 20 20 69 66 28 20 70 74 68  elf();.  if( pth
d12c0 72 65 61 64 5f 65 71 75 61 6c 28 70 46 69 6c 65  read_equal(pFile
d12d0 2d 3e 74 69 64 2c 20 68 53 65 6c 66 29 20 29 7b  ->tid, hSelf) ){
d12e0 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 73  .    /* We are s
d12f0 74 69 6c 6c 20 69 6e 20 74 68 65 20 73 61 6d 65  till in the same
d1300 20 74 68 72 65 61 64 20 2a 2f 0a 20 20 20 20 4f   thread */.    O
d1310 53 54 52 41 43 45 31 28 22 4e 6f 2d 74 72 61 6e  STRACE1("No-tran
d1320 73 66 65 72 2c 20 73 61 6d 65 20 74 68 72 65 61  sfer, same threa
d1330 64 5c 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72  d\n");.    retur
d1340 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d1350 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
d1360 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20  cktype!=NO_LOCK 
d1370 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 63 61 6e  ){.    /* We can
d1380 6e 6f 74 20 63 68 61 6e 67 65 20 6f 77 6e 65 72  not change owner
d1390 73 68 69 70 20 77 68 69 6c 65 20 77 65 20 61 72  ship while we ar
d13a0 65 20 68 6f 6c 64 69 6e 67 20 61 20 6c 6f 63 6b  e holding a lock
d13b0 21 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  ! */.    return 
d13c0 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
d13d0 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54   }.  OSTRACE4("T
d13e0 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 69  ransfer ownershi
d13f0 70 20 6f 66 20 25 64 20 66 72 6f 6d 20 25 64 20  p of %d from %d 
d1400 74 6f 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20  to %d\n",.      
d1410 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c 20        pFile->h, 
d1420 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65 6c  pFile->tid, hSel
d1430 66 29 3b 0a 20 20 70 46 69 6c 65 2d 3e 74 69 64  f);.  pFile->tid
d1440 20 3d 20 68 53 65 6c 66 3b 0a 20 20 69 66 20 28   = hSelf;.  if (
d1450 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 21 3d 20  pFile->pLock != 
d1460 4e 55 4c 4c 29 20 7b 0a 20 20 20 20 72 65 6c 65  NULL) {.    rele
d1470 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c  aseLockInfo(pFil
d1480 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72  e->pLock);.    r
d1490 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f  c = findLockInfo
d14a0 28 70 46 69 6c 65 2c 20 26 70 46 69 6c 65 2d 3e  (pFile, &pFile->
d14b0 70 4c 6f 63 6b 2c 20 30 29 3b 0a 20 20 20 20 4f  pLock, 0);.    O
d14c0 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20  STRACE5("LOCK   
d14d0 20 25 64 20 69 73 20 6e 6f 77 20 25 73 28 25 73   %d is now %s(%s
d14e0 2c 25 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ,%d)\n", pFile->
d14f0 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f  h,.           lo
d1500 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65  cktypeName(pFile
d1510 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20  ->locktype),.   
d1520 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65          locktype
d1530 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 70 4c 6f 63  Name(pFile->pLoc
d1540 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 46  k->locktype), pF
d1550 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74 29  ile->pLock->cnt)
d1560 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
d1570 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
d1580 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
d1590 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 20 20 2f  ;.  }.}.#else  /
d15a0 2a 20 69 66 20 6e 6f 74 20 53 51 4c 49 54 45 5f  * if not SQLITE_
d15b0 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 20 20  THREADSAFE */.  
d15c0 2f 2a 20 4f 6e 20 73 69 6e 67 6c 65 2d 74 68 72  /* On single-thr
d15d0 65 61 64 65 64 20 62 75 69 6c 64 73 2c 20 6f 77  eaded builds, ow
d15e0 6e 65 72 73 68 69 70 20 74 72 61 6e 73 66 65 72  nership transfer
d15f0 20 69 73 20 61 20 6e 6f 2d 6f 70 20 2a 2f 0a 23   is a no-op */.#
d1600 20 64 65 66 69 6e 65 20 74 72 61 6e 73 66 65 72   define transfer
d1610 4f 77 6e 65 72 73 68 69 70 28 58 29 20 53 51 4c  Ownership(X) SQL
d1620 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 20 2f 2a  ITE_OK.#endif /*
d1630 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41   SQLITE_THREADSA
d1640 46 45 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  FE */.../*.** Th
d1650 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b  is routine check
d1660 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20  s if there is a 
d1670 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65  RESERVED lock he
d1680 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66  ld on the specif
d1690 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74  ied.** file by t
d16a0 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72  his or any other
d16b0 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63   process. If suc
d16c0 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64  h a lock is held
d16d0 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a  , set *pResOut.*
d16e0 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  * to a non-zero 
d16f0 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20  value otherwise 
d1700 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20  *pResOut is set 
d1710 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65  to zero.  The re
d1720 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73  turn value.** is
d1730 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f   set to SQLITE_O
d1740 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20  K unless an I/O 
d1750 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
d1760 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e  ing lock checkin
d1770 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
d1780 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76   unixCheckReserv
d1790 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
d17a0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52  ile *id, int *pR
d17b0 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63  esOut){.  int rc
d17c0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
d17d0 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30  int reserved = 0
d17e0 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
d17f0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
d1800 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65  )id;..  Simulate
d1810 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
d1820 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
d1830 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20  CKRESERVEDLOCK; 
d1840 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46  );..  assert( pF
d1850 69 6c 65 20 29 3b 0a 20 20 75 6e 69 78 45 6e 74  ile );.  unixEnt
d1860 65 72 4d 75 74 65 78 28 29 3b 20 2f 2a 20 42 65  erMutex(); /* Be
d1870 63 61 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f  cause pFile->pLo
d1880 63 6b 20 69 73 20 73 68 61 72 65 64 20 61 63 72  ck is shared acr
d1890 6f 73 73 20 74 68 72 65 61 64 73 20 2a 2f 0a 0a  oss threads */..
d18a0 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 20    /* Check if a 
d18b0 74 68 72 65 61 64 20 69 6e 20 74 68 69 73 20 70  thread in this p
d18c0 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73 75 63  rocess holds suc
d18d0 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66  h a lock */.  if
d18e0 28 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e  ( pFile->pLock->
d18f0 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
d1900 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65  LOCK ){.    rese
d1910 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20  rved = 1;.  }.. 
d1920 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65   /* Otherwise se
d1930 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  e if some other 
d1940 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74  process holds it
d1950 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  ..  */.#ifndef _
d1960 5f 44 4a 47 50 50 5f 5f 0a 20 20 69 66 28 20 21  _DJGPP__.  if( !
d1970 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20  reserved ){.    
d1980 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63  struct flock loc
d1990 6b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68  k;.    lock.l_wh
d19a0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b  ence = SEEK_SET;
d19b0 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72  .    lock.l_star
d19c0 74 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54  t = RESERVED_BYT
d19d0 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65  E;.    lock.l_le
d19e0 6e 20 3d 20 31 3b 0a 20 20 20 20 6c 6f 63 6b 2e  n = 1;.    lock.
d19f0 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b  l_type = F_WRLCK
d1a00 3b 0a 20 20 20 20 69 66 20 28 2d 31 20 3d 3d 20  ;.    if (-1 == 
d1a10 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20  fcntl(pFile->h, 
d1a20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29  F_GETLK, &lock))
d1a30 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72   {.      int tEr
d1a40 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d1a50 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72     rc = sqliteEr
d1a60 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
d1a70 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45  r(tErrno, SQLITE
d1a80 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
d1a90 52 56 45 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20  RVEDLOCK);.     
d1aa0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d1ab0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d1ac0 7d 20 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 2e  } else if( lock.
d1ad0 6c 5f 74 79 70 65 21 3d 46 5f 55 4e 4c 43 4b 20  l_type!=F_UNLCK 
d1ae0 29 7b 0a 20 20 20 20 20 20 72 65 73 65 72 76 65  ){.      reserve
d1af0 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  d = 1;.    }.  }
d1b00 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20 75 6e 69  .#endif.  .  uni
d1b10 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
d1b20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53 54 20   OSTRACE4("TEST 
d1b30 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64  WR-LOCK %d %d %d
d1b40 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c   (unix)\n", pFil
d1b50 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76  e->h, rc, reserv
d1b60 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74  ed);..  *pResOut
d1b70 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72   = reserved;.  r
d1b80 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d1b90 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 66 69 6c  ** Perform a fil
d1ba0 65 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74  e locking operat
d1bb0 69 6f 6e 20 6f 6e 20 61 20 72 61 6e 67 65 20 6f  ion on a range o
d1bc0 66 20 62 79 74 65 73 20 69 6e 20 61 20 66 69 6c  f bytes in a fil
d1bd0 65 2e 0a 2a 2a 20 54 68 65 20 22 6f 70 22 20 70  e..** The "op" p
d1be0 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20  arameter should 
d1bf0 62 65 20 6f 6e 65 20 6f 66 20 46 5f 52 44 4c 43  be one of F_RDLC
d1c00 4b 2c 20 46 5f 57 52 4c 43 4b 2c 20 6f 72 20 46  K, F_WRLCK, or F
d1c10 5f 55 4e 4c 43 4b 2e 0a 2a 2a 20 52 65 74 75 72  _UNLCK..** Retur
d1c20 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 6f  n 0 on success o
d1c30 72 20 2d 31 20 66 6f 72 20 66 61 69 6c 75 72 65  r -1 for failure
d1c40 2e 20 20 4f 6e 20 66 61 69 6c 75 72 65 2c 20 77  .  On failure, w
d1c50 72 69 74 65 20 74 68 65 20 65 72 72 6f 72 0a 2a  rite the error.*
d1c60 2a 20 63 6f 64 65 20 69 6e 74 6f 20 2a 70 45 72  * code into *pEr
d1c70 72 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rcode..**.** If 
d1c80 74 68 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45  the SQLITE_WHOLE
d1c90 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 62 69  _FILE_LOCKING bi
d1ca0 74 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 6e  t is clear, then
d1cb0 20 6f 6e 6c 79 20 6c 6f 63 6b 0a 2a 2a 20 74 68   only lock.** th
d1cc0 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73  e range of bytes
d1cd0 20 6f 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20   on the locking 
d1ce0 70 61 67 65 20 62 65 74 77 65 65 6e 20 53 48 41  page between SHA
d1cf0 52 45 44 5f 46 49 52 53 54 20 61 6e 64 0a 2a 2a  RED_FIRST and.**
d1d00 20 53 48 41 52 45 44 5f 53 49 5a 45 2e 20 20 49   SHARED_SIZE.  I
d1d10 66 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46  f SQLITE_WHOLE_F
d1d20 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 73  ILE_LOCKING is s
d1d30 65 74 2c 20 74 68 65 6e 20 6c 6f 63 6b 20 61 6c  et, then lock al
d1d40 6c 0a 2a 2a 20 62 79 74 65 73 20 66 72 6f 6d 20  l.** bytes from 
d1d50 30 20 75 70 20 74 6f 20 62 75 74 20 6e 6f 74 20  0 up to but not 
d1d60 69 6e 63 6c 75 64 69 6e 67 20 50 45 4e 44 49 4e  including PENDIN
d1d70 47 5f 42 59 54 45 2c 20 61 6e 64 20 61 6c 6c 20  G_BYTE, and all 
d1d80 62 79 74 65 73 0a 2a 2a 20 74 68 61 74 20 66 6f  bytes.** that fo
d1d90 6c 6c 6f 77 20 53 48 41 52 45 44 5f 46 49 52 53  llow SHARED_FIRS
d1da0 54 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65  T..**.** In othe
d1db0 72 20 77 6f 72 64 73 2c 20 6f 66 20 53 51 4c 49  r words, of SQLI
d1dc0 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f  TE_WHOLE_FILE_LO
d1dd0 43 4b 49 4e 47 20 69 66 20 66 61 6c 73 65 20 28  CKING if false (
d1de0 74 68 65 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a  the historical.*
d1df0 2a 20 64 65 66 61 75 6c 74 20 63 61 73 65 29 20  * default case) 
d1e00 74 68 65 6e 20 6f 6e 6c 79 20 6c 6f 63 6b 20 61  then only lock a
d1e10 20 73 6d 61 6c 6c 20 72 61 6e 67 65 20 6f 66 20   small range of 
d1e20 62 79 74 65 73 20 66 72 6f 6d 20 53 48 41 52 45  bytes from SHARE
d1e30 44 5f 46 49 52 53 54 0a 2a 2a 20 74 68 72 6f 75  D_FIRST.** throu
d1e40 67 68 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b  gh SHARED_FIRST+
d1e50 53 48 41 52 45 44 5f 53 49 5a 45 2d 31 2e 20 20  SHARED_SIZE-1.  
d1e60 42 75 74 20 69 66 20 53 51 4c 49 54 45 5f 57 48  But if SQLITE_WH
d1e70 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47  OLE_FILE_LOCKING
d1e80 20 69 73 0a 2a 2a 20 74 72 75 65 20 74 68 65 6e   is.** true then
d1e90 20 6c 6f 63 6b 20 65 76 65 72 79 20 62 79 74 65   lock every byte
d1ea0 20 69 6e 20 74 68 65 20 66 69 6c 65 20 65 78 63   in the file exc
d1eb0 65 70 74 20 66 6f 72 20 50 45 4e 44 49 4e 47 5f  ept for PENDING_
d1ec0 42 59 54 45 20 61 6e 64 0a 2a 2a 20 52 45 53 45  BYTE and.** RESE
d1ed0 52 56 45 44 5f 42 59 54 45 2e 0a 2a 2a 0a 2a 2a  RVED_BYTE..**.**
d1ee0 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49   SQLITE_WHOLE_FI
d1ef0 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 20  LE_LOCKING=true 
d1f00 6f 76 65 72 6c 61 70 73 20 53 51 4c 49 54 45 5f  overlaps SQLITE_
d1f10 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49  WHOLE_FILE_LOCKI
d1f20 4e 47 3d 66 61 6c 73 65 0a 2a 2a 20 61 6e 64 20  NG=false.** and 
d1f30 73 6f 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73  so the locking s
d1f40 63 68 65 6d 65 73 20 61 72 65 20 63 6f 6d 70 61  chemes are compa
d1f50 74 69 62 6c 65 2e 20 20 4f 6e 65 20 74 79 70 65  tible.  One type
d1f60 20 6f 66 20 6c 6f 63 6b 20 77 69 6c 6c 0a 2a 2a   of lock will.**
d1f70 20 65 66 66 65 63 74 69 76 65 6c 79 20 65 78 63   effectively exc
d1f80 6c 75 64 65 20 74 68 65 20 6f 74 68 65 72 20 74  lude the other t
d1f90 79 70 65 2e 20 20 54 68 65 20 72 65 61 73 6f 6e  ype.  The reason
d1fa0 20 66 6f 72 20 75 73 69 6e 67 20 74 68 65 0a 2a   for using the.*
d1fb0 2a 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46  * SQLITE_WHOLE_F
d1fc0 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 65  ILE_LOCKING=true
d1fd0 20 69 73 20 74 68 61 74 20 62 79 20 69 6e 64 69   is that by indi
d1fe0 63 61 74 69 6e 67 20 74 68 65 20 66 75 6c 6c 20  cating the full 
d1ff0 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 62 79 74 65  range.** of byte
d2000 73 20 74 6f 20 62 65 20 72 65 61 64 20 6f 72 20  s to be read or 
d2010 77 72 69 74 74 65 6e 2c 20 77 65 20 67 69 76 65  written, we give
d2020 20 68 69 6e 74 73 20 74 6f 20 4e 46 53 20 74 6f   hints to NFS to
d2030 20 68 65 6c 70 20 69 74 0a 2a 2a 20 6d 61 69 6e   help it.** main
d2040 74 61 69 6e 20 63 61 63 68 65 20 63 6f 68 65 72  tain cache coher
d2050 65 6e 63 79 2e 20 20 4f 6e 20 74 68 65 20 6f 74  ency.  On the ot
d2060 68 65 72 20 68 61 6e 64 2c 20 77 68 6f 6c 65 20  her hand, whole 
d2070 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20  file locking.** 
d2080 69 73 20 73 6c 6f 77 65 72 2c 20 73 6f 20 77 65  is slower, so we
d2090 20 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 75   don't want to u
d20a0 73 65 20 69 74 20 65 78 63 65 70 74 20 66 6f 72  se it except for
d20b0 20 4e 46 53 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   NFS..*/.static 
d20c0 69 6e 74 20 72 61 6e 67 65 4c 6f 63 6b 28 75 6e  int rangeLock(un
d20d0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69  ixFile *pFile, i
d20e0 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 45 72 72  nt op, int *pErr
d20f0 63 6f 64 65 29 7b 0a 20 20 73 74 72 75 63 74 20  code){.  struct 
d2100 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e  flock lock;.  in
d2110 74 20 72 63 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 74  t rc;.  lock.l_t
d2120 79 70 65 20 3d 20 6f 70 3b 0a 20 20 6c 6f 63 6b  ype = op;.  lock
d2130 2e 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45  .l_start = SHARE
d2140 44 5f 46 49 52 53 54 3b 0a 20 20 6c 6f 63 6b 2e  D_FIRST;.  lock.
d2150 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
d2160 53 45 54 3b 0a 20 20 69 66 28 20 28 70 46 69 6c  SET;.  if( (pFil
d2170 65 2d 3e 66 69 6c 65 46 6c 61 67 73 20 26 20 53  e->fileFlags & S
d2180 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45  QLITE_WHOLE_FILE
d2190 5f 4c 4f 43 4b 49 4e 47 29 3d 3d 30 20 29 7b 0a  _LOCKING)==0 ){.
d21a0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
d21b0 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20   SHARED_SIZE;.  
d21c0 20 20 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 69    rc = fcntl(pFi
d21d0 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20  le->h, F_SETLK, 
d21e0 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72  &lock);.    *pEr
d21f0 72 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20  rcode = errno;. 
d2200 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b   }else{.    lock
d2210 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20  .l_len = 0;.    
d2220 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65  rc = fcntl(pFile
d2230 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c  ->h, F_SETLK, &l
d2240 6f 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72 72 63  ock);.    *pErrc
d2250 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  ode = errno;.   
d2260 20 69 66 28 20 4e 45 56 45 52 28 6f 70 3d 3d 46   if( NEVER(op==F
d2270 5f 55 4e 4c 43 4b 29 20 7c 7c 20 72 63 21 3d 28  _UNLCK) || rc!=(
d2280 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63  -1) ){.      loc
d2290 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20  k.l_start = 0;. 
d22a0 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20       lock.l_len 
d22b0 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a  = PENDING_BYTE;.
d22c0 20 20 20 20 20 20 72 63 20 3d 20 66 63 6e 74 6c        rc = fcntl
d22d0 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54  (pFile->h, F_SET
d22e0 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20  LK, &lock);.    
d22f0 20 20 69 66 28 20 41 4c 57 41 59 53 28 6f 70 21    if( ALWAYS(op!
d2300 3d 46 5f 55 4e 4c 43 4b 29 20 26 26 20 72 63 3d  =F_UNLCK) && rc=
d2310 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  =(-1) ){.       
d2320 20 2a 70 45 72 72 63 6f 64 65 20 3d 20 65 72 72   *pErrcode = err
d2330 6e 6f 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b  no;.        lock
d2340 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43  .l_type = F_UNLC
d2350 4b 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e  K;.        lock.
d2360 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44  l_start = SHARED
d2370 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20  _FIRST;.        
d2380 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a  lock.l_len = 0;.
d2390 20 20 20 20 20 20 20 20 66 63 6e 74 6c 28 70 46          fcntl(pF
d23a0 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c  ile->h, F_SETLK,
d23b0 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d   &lock);.      }
d23c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
d23d0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
d23e0 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77   Lock the file w
d23f0 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65  ith the lock spe
d2400 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65  cified by parame
d2410 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f  ter locktype - o
d2420 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ne.** of the fol
d2430 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  lowing:.**.**   
d2440 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43    (1) SHARED_LOC
d2450 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53  K.**     (2) RES
d2460 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  ERVED_LOCK.**   
d2470 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f    (3) PENDING_LO
d2480 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58  CK.**     (4) EX
d2490 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a  CLUSIVE_LOCK.**.
d24a0 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65  ** Sometimes whe
d24b0 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65  n requesting one
d24c0 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64   lock state, add
d24d0 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61  itional lock sta
d24e0 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72  tes.** are inser
d24f0 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20  ted in between. 
d2500 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67   The locking mig
d2510 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f  ht fail on one o
d2520 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74  f the later.** t
d2530 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69  ransitions leavi
d2540 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74  ng the lock stat
d2550 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
d2560 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64   what it started
d2570 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68   but.** still sh
d2580 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e  ort of its goal.
d2590 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
d25a0 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20  chart shows the 
d25b0 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73  allowed.** trans
d25c0 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69  itions and the i
d25d0 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64  nserted intermed
d25e0 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  iate states:.**.
d25f0 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d  **    UNLOCKED -
d2600 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53  > SHARED.**    S
d2610 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45  HARED -> RESERVE
d2620 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
d2630 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
d2640 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52  XCLUSIVE.**    R
d2650 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44  ESERVED -> (PEND
d2660 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
d2670 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20  E.**    PENDING 
d2680 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a  -> EXCLUSIVE.**.
d2690 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
d26a0 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61  will only increa
d26b0 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20  se a lock.  Use 
d26c0 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c  the sqlite3OsUnl
d26d0 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65  ock().** routine
d26e0 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b   to lower a lock
d26f0 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74  ing level..*/.st
d2700 61 74 69 63 20 69 6e 74 20 75 6e 69 78 4c 6f 63  atic int unixLoc
d2710 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d2720 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
d2730 29 7b 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  ){.  /* The foll
d2740 6f 77 69 6e 67 20 64 65 73 63 72 69 62 65 73 20  owing describes 
d2750 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
d2760 6f 6e 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75  on of the variou
d2770 73 20 6c 6f 63 6b 73 20 61 6e 64 0a 20 20 2a 2a  s locks and.  **
d2780 20 6c 6f 63 6b 20 74 72 61 6e 73 69 74 69 6f 6e   lock transition
d2790 73 20 69 6e 20 74 65 72 6d 73 20 6f 66 20 74 68  s in terms of th
d27a0 65 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79  e POSIX advisory
d27b0 20 73 68 61 72 65 64 20 61 6e 64 20 65 78 63 6c   shared and excl
d27c0 75 73 69 76 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20  usive.  ** lock 
d27d0 70 72 69 6d 69 74 69 76 65 73 20 28 63 61 6c 6c  primitives (call
d27e0 65 64 20 72 65 61 64 2d 6c 6f 63 6b 73 20 61 6e  ed read-locks an
d27f0 64 20 77 72 69 74 65 2d 6c 6f 63 6b 73 20 62 65  d write-locks be
d2800 6c 6f 77 2c 20 74 6f 20 61 76 6f 69 64 0a 20 20  low, to avoid.  
d2810 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74  ** confusion wit
d2820 68 20 53 51 4c 69 74 65 20 6c 6f 63 6b 20 6e 61  h SQLite lock na
d2830 6d 65 73 29 2e 20 54 68 65 20 61 6c 67 6f 72 69  mes). The algori
d2840 74 68 6d 73 20 61 72 65 20 63 6f 6d 70 6c 69 63  thms are complic
d2850 61 74 65 64 0a 20 20 2a 2a 20 73 6c 69 67 68 74  ated.  ** slight
d2860 6c 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62  ly in order to b
d2870 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74  e compatible wit
d2880 68 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d  h windows system
d2890 73 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79  s simultaneously
d28a0 0a 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20  .  ** accessing 
d28b0 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
d28c0 65 20 66 69 6c 65 2c 20 69 6e 20 63 61 73 65 20  e file, in case 
d28d0 74 68 61 74 20 69 73 20 65 76 65 72 20 72 65 71  that is ever req
d28e0 75 69 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  uired..  **.  **
d28f0 20 53 79 6d 62 6f 6c 73 20 64 65 66 69 6e 65 64   Symbols defined
d2900 20 69 6e 20 6f 73 2e 68 20 69 6e 64 65 6e 74 69   in os.h indenti
d2910 66 79 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20  fy the 'pending 
d2920 62 79 74 65 27 20 61 6e 64 20 74 68 65 20 27 72  byte' and the 'r
d2930 65 73 65 72 76 65 64 0a 20 20 2a 2a 20 62 79 74  eserved.  ** byt
d2940 65 27 2c 20 65 61 63 68 20 73 69 6e 67 6c 65 20  e', each single 
d2950 62 79 74 65 73 20 61 74 20 77 65 6c 6c 20 6b 6e  bytes at well kn
d2960 6f 77 6e 20 6f 66 66 73 65 74 73 2c 20 61 6e 64  own offsets, and
d2970 20 74 68 65 20 27 73 68 61 72 65 64 20 62 79 74   the 'shared byt
d2980 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2c 20 61  e.  ** range', a
d2990 20 72 61 6e 67 65 20 6f 66 20 35 31 30 20 62 79   range of 510 by
d29a0 74 65 73 20 61 74 20 61 20 77 65 6c 6c 20 6b 6e  tes at a well kn
d29b0 6f 77 6e 20 6f 66 66 73 65 74 2e 0a 20 20 2a 2a  own offset..  **
d29c0 0a 20 20 2a 2a 20 54 6f 20 6f 62 74 61 69 6e 20  .  ** To obtain 
d29d0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 61  a SHARED lock, a
d29e0 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62   read-lock is ob
d29f0 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 27 70  tained on the 'p
d2a00 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 62 79 74 65  ending.  ** byte
d2a10 27 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 73  '.  If this is s
d2a20 75 63 63 65 73 73 66 75 6c 2c 20 61 20 72 61 6e  uccessful, a ran
d2a30 64 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 74 68  dom byte from th
d2a40 65 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20  e 'shared byte. 
d2a50 20 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 72 65   ** range' is re
d2a60 61 64 2d 6c 6f 63 6b 65 64 20 61 6e 64 20 74 68  ad-locked and th
d2a70 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 27 70  e lock on the 'p
d2a80 65 6e 64 69 6e 67 20 62 79 74 65 27 20 72 65 6c  ending byte' rel
d2a90 65 61 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  eased..  **.  **
d2aa0 20 41 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f   A process may o
d2ab0 6e 6c 79 20 6f 62 74 61 69 6e 20 61 20 52 45 53  nly obtain a RES
d2ac0 45 52 56 45 44 20 6c 6f 63 6b 20 61 66 74 65 72  ERVED lock after
d2ad0 20 69 74 20 68 61 73 20 61 20 53 48 41 52 45 44   it has a SHARED
d2ae0 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 41 20 52 45   lock..  ** A RE
d2af0 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 69  SERVED lock is i
d2b00 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 67 72  mplemented by gr
d2b10 61 62 62 69 6e 67 20 61 20 77 72 69 74 65 2d 6c  abbing a write-l
d2b20 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  ock on the.  ** 
d2b30 27 72 65 73 65 72 76 65 64 20 62 79 74 65 27 2e  'reserved byte'.
d2b40 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72   .  **.  ** A pr
d2b50 6f 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f  ocess may only o
d2b60 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 20  btain a PENDING 
d2b70 6c 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61  lock after it ha
d2b80 73 20 6f 62 74 61 69 6e 65 64 20 61 0a 20 20 2a  s obtained a.  *
d2b90 2a 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 41  * SHARED lock. A
d2ba0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73   PENDING lock is
d2bb0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20   implemented by 
d2bc0 6f 62 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74  obtaining a writ
d2bd0 65 2d 6c 6f 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74  e-lock.  ** on t
d2be0 68 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65  he 'pending byte
d2bf0 27 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20  '. This ensures 
d2c00 74 68 61 74 20 6e 6f 20 6e 65 77 20 53 48 41 52  that no new SHAR
d2c10 45 44 20 6c 6f 63 6b 73 20 63 61 6e 20 62 65 0a  ED locks can be.
d2c20 20 20 2a 2a 20 6f 62 74 61 69 6e 65 64 2c 20 62    ** obtained, b
d2c30 75 74 20 65 78 69 73 74 69 6e 67 20 53 48 41 52  ut existing SHAR
d2c40 45 44 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c 6c  ED locks are all
d2c50 6f 77 65 64 20 74 6f 20 70 65 72 73 69 73 74 2e  owed to persist.
d2c60 20 41 20 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20   A process.  ** 
d2c70 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f  does not have to
d2c80 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56   obtain a RESERV
d2c90 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 77  ED lock on the w
d2ca0 61 79 20 74 6f 20 61 20 50 45 4e 44 49 4e 47 20  ay to a PENDING 
d2cb0 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 54 68 69 73 20  lock..  ** This 
d2cc0 70 72 6f 70 65 72 74 79 20 69 73 20 75 73 65 64  property is used
d2cd0 20 62 79 20 74 68 65 20 61 6c 67 6f 72 69 74 68   by the algorith
d2ce0 6d 20 66 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61  m for rolling ba
d2cf0 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ck a journal fil
d2d00 65 0a 20 20 2a 2a 20 61 66 74 65 72 20 61 20 63  e.  ** after a c
d2d10 72 61 73 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  rash..  **.  ** 
d2d20 41 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  An EXCLUSIVE loc
d2d30 6b 2c 20 6f 62 74 61 69 6e 65 64 20 61 66 74 65  k, obtained afte
d2d40 72 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  r a PENDING lock
d2d50 20 69 73 20 68 65 6c 64 2c 20 69 73 0a 20 20 2a   is held, is.  *
d2d60 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79  * implemented by
d2d70 20 6f 62 74 61 69 6e 69 6e 67 20 61 20 77 72 69   obtaining a wri
d2d80 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 65  te-lock on the e
d2d90 6e 74 69 72 65 20 27 73 68 61 72 65 64 20 62 79  ntire 'shared by
d2da0 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2e 20  te.  ** range'. 
d2db0 53 69 6e 63 65 20 61 6c 6c 20 6f 74 68 65 72 20  Since all other 
d2dc0 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 20 61 20  locks require a 
d2dd0 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 6f 6e 65  read-lock on one
d2de0 20 6f 66 20 74 68 65 20 62 79 74 65 73 0a 20 20   of the bytes.  
d2df0 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 72  ** within this r
d2e00 61 6e 67 65 2c 20 74 68 69 73 20 65 6e 73 75 72  ange, this ensur
d2e10 65 73 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  es that no other
d2e20 20 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20   locks are held 
d2e30 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  on the.  ** data
d2e40 62 61 73 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a  base. .  **.  **
d2e50 20 54 68 65 20 72 65 61 73 6f 6e 20 61 20 73 69   The reason a si
d2e60 6e 67 6c 65 20 62 79 74 65 20 63 61 6e 6e 6f 74  ngle byte cannot
d2e70 20 62 65 20 75 73 65 64 20 69 6e 73 74 65 61 64   be used instead
d2e80 20 6f 66 20 74 68 65 20 27 73 68 61 72 65 64 20   of the 'shared 
d2e90 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27  byte.  ** range'
d2ea0 20 69 73 20 74 68 61 74 20 73 6f 6d 65 20 76 65   is that some ve
d2eb0 72 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77  rsions of window
d2ec0 73 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74  s do not support
d2ed0 20 72 65 61 64 2d 6c 6f 63 6b 73 2e 20 42 79 0a   read-locks. By.
d2ee0 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 20 72    ** locking a r
d2ef0 61 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20  andom byte from 
d2f00 61 20 72 61 6e 67 65 2c 20 63 6f 6e 63 75 72 72  a range, concurr
d2f10 65 6e 74 20 53 48 41 52 45 44 20 6c 6f 63 6b 73  ent SHARED locks
d2f20 20 6d 61 79 20 65 78 69 73 74 0a 20 20 2a 2a 20   may exist.  ** 
d2f30 65 76 65 6e 20 69 66 20 74 68 65 20 6c 6f 63 6b  even if the lock
d2f40 69 6e 67 20 70 72 69 6d 69 74 69 76 65 20 75 73  ing primitive us
d2f50 65 64 20 69 73 20 61 6c 77 61 79 73 20 61 20 77  ed is always a w
d2f60 72 69 74 65 2d 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a  rite-lock..  */.
d2f70 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
d2f80 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65  E_OK;.  unixFile
d2f90 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
d2fa0 69 6c 65 2a 29 69 64 3b 0a 20 20 73 74 72 75 63  ile*)id;.  struc
d2fb0 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a  t unixLockInfo *
d2fc0 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70  pLock = pFile->p
d2fd0 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66  Lock;.  struct f
d2fe0 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74  lock lock;.  int
d2ff0 20 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74 45   s = 0;.  int tE
d3000 72 72 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28  rrno;..  assert(
d3010 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52   pFile );.  OSTR
d3020 41 43 45 37 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE7("LOCK    %d
d3030 20 25 73 20 77 61 73 20 25 73 28 25 73 2c 25 64   %s was %s(%s,%d
d3040 29 20 70 69 64 3d 25 64 20 28 75 6e 69 78 29 5c  ) pid=%d (unix)\
d3050 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
d3060 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65      locktypeName
d3070 28 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b  (locktype), lock
d3080 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e  typeName(pFile->
d3090 6c 6f 63 6b 74 79 70 65 29 2c 0a 20 20 20 20 20  locktype),.     
d30a0 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 70 4c   locktypeName(pL
d30b0 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 29 2c 20  ock->locktype), 
d30c0 70 4c 6f 63 6b 2d 3e 63 6e 74 20 2c 20 67 65 74  pLock->cnt , get
d30d0 70 69 64 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66  pid());..  /* If
d30e0 20 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64   there is alread
d30f0 79 20 61 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73  y a lock of this
d3100 20 74 79 70 65 20 6f 72 20 6d 6f 72 65 20 72 65   type or more re
d3110 73 74 72 69 63 74 69 76 65 20 6f 6e 20 74 68 65  strictive on the
d3120 0a 20 20 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20  .  ** unixFile, 
d3130 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27  do nothing. Don'
d3140 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f 6c 6f  t use the end_lo
d3150 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61  ck: exit path, a
d3160 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72  s.  ** unixEnter
d3170 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62  Mutex() hasn't b
d3180 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a  een called yet..
d3190 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65    */.  if( pFile
d31a0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b  ->locktype>=lock
d31b0 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52  type ){.    OSTR
d31c0 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE3("LOCK    %d
d31d0 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20   %s ok (already 
d31e0 68 65 6c 64 29 20 28 75 6e 69 78 29 5c 6e 22 2c  held) (unix)\n",
d31f0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20   pFile->h,.     
d3200 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e         locktypeN
d3210 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a  ame(locktype));.
d3220 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d3230 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
d3240 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f  Make sure the lo
d3250 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  cking sequence i
d3260 73 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2a 20  s correct..  ** 
d3270 20 28 31 29 20 57 65 20 6e 65 76 65 72 20 6d 6f   (1) We never mo
d3280 76 65 20 66 72 6f 6d 20 75 6e 6c 6f 63 6b 65 64  ve from unlocked
d3290 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 68 69 67   to anything hig
d32a0 68 65 72 20 74 68 61 6e 20 73 68 61 72 65 64 20  her than shared 
d32b0 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28 32 29 20  lock..  **  (2) 
d32c0 53 51 4c 69 74 65 20 6e 65 76 65 72 20 65 78 70  SQLite never exp
d32d0 6c 69 63 69 74 6c 79 20 72 65 71 75 65 73 74 73  licitly requests
d32e0 20 61 20 70 65 6e 64 69 67 20 6c 6f 63 6b 2e 0a   a pendig lock..
d32f0 20 20 2a 2a 20 20 28 33 29 20 41 20 73 68 61 72    **  (3) A shar
d3300 65 64 20 6c 6f 63 6b 20 69 73 20 61 6c 77 61 79  ed lock is alway
d3310 73 20 68 65 6c 64 20 77 68 65 6e 20 61 20 72 65  s held when a re
d3320 73 65 72 76 65 20 6c 6f 63 6b 20 69 73 20 72 65  serve lock is re
d3330 71 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  quested..  */.  
d3340 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
d3350 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
d3360 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   || locktype==SH
d3370 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
d3380 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
d3390 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b  =PENDING_LOCK );
d33a0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
d33b0 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype!=RESERVED_LO
d33c0 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK || pFile->loc
d33d0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
d33e0 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  CK );..  /* This
d33f0 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64   mutex is needed
d3400 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e   because pFile->
d3410 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20  pLock is shared 
d3420 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20  across threads. 
d3430 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d   */.  unixEnterM
d3440 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61  utex();..  /* Ma
d3450 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 72 72  ke sure the curr
d3460 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20  ent thread owns 
d3470 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a  the pFile..  */.
d3480 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f    rc = transferO
d3490 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b  wnership(pFile);
d34a0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
d34b0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69 78  E_OK ){.    unix
d34c0 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
d34d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
d34e0 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65  .  pLock = pFile
d34f0 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20 49  ->pLock;..  /* I
d3500 66 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75 73  f some thread us
d3510 69 6e 67 20 74 68 69 73 20 50 49 44 20 68 61 73  ing this PID has
d3520 20 61 20 6c 6f 63 6b 20 76 69 61 20 61 20 64 69   a lock via a di
d3530 66 66 65 72 65 6e 74 20 75 6e 69 78 46 69 6c 65  fferent unixFile
d3540 2a 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74 68  *.  ** handle th
d3550 61 74 20 70 72 65 63 6c 75 64 65 73 20 74 68 65  at precludes the
d3560 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2c   requested lock,
d3570 20 72 65 74 75 72 6e 20 42 55 53 59 2e 0a 20 20   return BUSY..  
d3580 2a 2f 0a 20 20 69 66 28 20 28 70 46 69 6c 65 2d  */.  if( (pFile-
d3590 3e 6c 6f 63 6b 74 79 70 65 21 3d 70 4c 6f 63 6b  >locktype!=pLock
d35a0 2d 3e 6c 6f 63 6b 74 79 70 65 20 26 26 20 0a 20  ->locktype && . 
d35b0 20 20 20 20 20 20 20 20 20 28 70 4c 6f 63 6b 2d           (pLock-
d35c0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44 49  >locktype>=PENDI
d35d0 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b 74  NG_LOCK || lockt
d35e0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 29  ype>SHARED_LOCK)
d35f0 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ).  ){.    rc = 
d3600 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
d3610 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a   goto end_lock;.
d3620 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20 53    }..  /* If a S
d3630 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72 65  HARED lock is re
d3640 71 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f 6d  quested, and som
d3650 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74  e thread using t
d3660 68 69 73 20 50 49 44 20 61 6c 72 65 61 64 79 0a  his PID already.
d3670 20 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52 45    ** has a SHARE
d3680 44 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c 6f  D or RESERVED lo
d3690 63 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d 65  ck, then increme
d36a0 6e 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  nt reference cou
d36b0 6e 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65 74  nts and.  ** ret
d36c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20  urn SQLITE_OK.. 
d36d0 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79   */.  if( lockty
d36e0 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
d36f0 26 26 20 0a 20 20 20 20 20 20 28 70 4c 6f 63 6b  && .      (pLock
d3700 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ->locktype==SHAR
d3710 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63 6b  ED_LOCK || pLock
d3720 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45  ->locktype==RESE
d3730 52 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20  RVED_LOCK) ){.  
d3740 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
d3750 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
d3760 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
d3770 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
d3780 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
d3790 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29 3b   pLock->cnt>0 );
d37a0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
d37b0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f  type = SHARED_LO
d37c0 43 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63  CK;.    pLock->c
d37d0 6e 74 2b 2b 3b 0a 20 20 20 20 70 46 69 6c 65 2d  nt++;.    pFile-
d37e0 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b  >pOpen->nLock++;
d37f0 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f  .    goto end_lo
d3800 63 6b 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 41  ck;.  }...  /* A
d3810 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73   PENDING lock is
d3820 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20 61   needed before a
d3830 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45  cquiring a SHARE
d3840 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72  D lock and befor
d3850 65 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67  e.  ** acquiring
d3860 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
d3870 63 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48 41  ck.  For the SHA
d3880 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45  RED lock, the PE
d3890 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20  NDING will.  ** 
d38a0 62 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a  be released..  *
d38b0 2f 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d  /.  lock.l_len =
d38c0 20 31 4c 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77 68   1L;.  lock.l_wh
d38d0 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b  ence = SEEK_SET;
d38e0 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
d38f0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20  =SHARED_LOCK .  
d3900 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65      || (locktype
d3910 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
d3920 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   && pFile->lockt
d3930 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  ype<PENDING_LOCK
d3940 29 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b 2e  ).  ){.    lock.
d3950 6c 5f 74 79 70 65 20 3d 20 28 6c 6f 63 6b 74 79  l_type = (lockty
d3960 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 3f  pe==SHARED_LOCK?
d3970 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b 29  F_RDLCK:F_WRLCK)
d3980 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61  ;.    lock.l_sta
d3990 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54  rt = PENDING_BYT
d39a0 45 3b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c  E;.    s = fcntl
d39b0 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54  (pFile->h, F_SET
d39c0 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20  LK, &lock);.    
d39d0 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20  if( s==(-1) ){. 
d39e0 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72       tErrno = er
d39f0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rno;.      rc = 
d3a00 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d3a10 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d3a20 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c  , SQLITE_IOERR_L
d3a30 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  OCK);.      if( 
d3a40 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
d3a50 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  ) ){.        pFi
d3a60 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d3a70 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a  tErrno;.      }.
d3a80 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c        goto end_l
d3a90 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ock;.    }.  }..
d3aa0 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c  .  /* If control
d3ab0 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f   gets to this po
d3ac0 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c  int, then actual
d3ad0 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20  ly go ahead and 
d3ae0 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74  make.  ** operat
d3af0 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73  ing system calls
d3b00 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
d3b10 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  ed lock..  */.  
d3b20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
d3b30 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
d3b40 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
d3b50 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73  cnt==0 );.    as
d3b60 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63  sert( pLock->loc
d3b70 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a 20 20 20  ktype==0 );..   
d3b80 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65 20   /* Now get the 
d3b90 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20 20  read-lock */.   
d3ba0 20 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28 70   s = rangeLock(p
d3bb0 46 69 6c 65 2c 20 46 5f 52 44 4c 43 4b 2c 20 26  File, F_RDLCK, &
d3bc0 74 45 72 72 6e 6f 29 3b 0a 0a 20 20 20 20 2f 2a  tErrno);..    /*
d3bd0 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f 72   Drop the tempor
d3be0 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  ary PENDING lock
d3bf0 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73   */.    lock.l_s
d3c00 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42  tart = PENDING_B
d3c10 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  YTE;.    lock.l_
d3c20 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 6c 6f  len = 1L;.    lo
d3c30 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e  ck.l_type = F_UN
d3c40 4c 43 4b 3b 0a 20 20 20 20 69 66 28 20 66 63 6e  LCK;.    if( fcn
d3c50 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53  tl(pFile->h, F_S
d3c60 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 30 20  ETLK, &lock)!=0 
d3c70 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 20 21  ){.      if( s !
d3c80 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  = -1 ){.        
d3c90 2f 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 68 61  /* This could ha
d3ca0 70 70 65 6e 20 77 69 74 68 20 61 20 6e 65 74 77  ppen with a netw
d3cb0 6f 72 6b 20 6d 6f 75 6e 74 20 2a 2f 0a 20 20 20  ork mount */.   
d3cc0 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72       tErrno = er
d3cd0 72 6e 6f 3b 20 0a 20 20 20 20 20 20 20 20 72 63  rno; .        rc
d3ce0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72   = sqliteErrorFr
d3cf0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72  omPosixError(tEr
d3d00 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52  rno, SQLITE_IOER
d3d10 52 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20  R_UNLOCK); .    
d3d20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
d3d30 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20  ERROR(rc) ){.   
d3d40 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
d3d50 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d3d60 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
d3d70 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63      goto end_loc
d3d80 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
d3d90 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29  .    if( s==(-1)
d3da0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
d3db0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d3dc0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d3dd0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
d3de0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49  CK);.      if( I
d3df0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d3e00 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
d3e10 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d3e20 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  Errno;.      }. 
d3e30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
d3e40 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d3e50 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20  = SHARED_LOCK;. 
d3e60 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65       pFile->pOpe
d3e70 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20  n->nLock++;.    
d3e80 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 31    pLock->cnt = 1
d3e90 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
d3ea0 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58  if( locktype==EX
d3eb0 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20  CLUSIVE_LOCK && 
d3ec0 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 31 20 29 7b 0a  pLock->cnt>1 ){.
d3ed0 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 74 72      /* We are tr
d3ee0 79 69 6e 67 20 66 6f 72 20 61 6e 20 65 78 63 6c  ying for an excl
d3ef0 75 73 69 76 65 20 6c 6f 63 6b 20 62 75 74 20 61  usive lock but a
d3f00 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 6e  nother thread in
d3f10 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 73 61 6d   this.    ** sam
d3f20 65 20 70 72 6f 63 65 73 73 20 69 73 20 73 74 69  e process is sti
d3f30 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 20 73 68 61  ll holding a sha
d3f40 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 20  red lock. */.   
d3f50 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
d3f60 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  Y;.  }else{.    
d3f70 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 20 77  /* The request w
d3f80 61 73 20 66 6f 72 20 61 20 52 45 53 45 52 56 45  as for a RESERVE
d3f90 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c  D or EXCLUSIVE l
d3fa0 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20 20  ock.  It is.    
d3fb0 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74 20  ** assumed that 
d3fc0 74 68 65 72 65 20 69 73 20 61 20 53 48 41 52 45  there is a SHARE
d3fd0 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63  D or greater loc
d3fe0 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20 20  k on the file.  
d3ff0 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20 20    ** already..  
d4000 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
d4010 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   0!=pFile->lockt
d4020 79 70 65 20 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e  ype );.    lock.
d4030 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b  l_type = F_WRLCK
d4040 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6c 6f  ;.    switch( lo
d4050 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20  cktype ){.      
d4060 63 61 73 65 20 52 45 53 45 52 56 45 44 5f 4c 4f  case RESERVED_LO
d4070 43 4b 3a 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b  CK:.        lock
d4080 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45 52  .l_start = RESER
d4090 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20 20  VED_BYTE;.      
d40a0 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c    s = fcntl(pFil
d40b0 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  e->h, F_SETLK, &
d40c0 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20 20 74  lock);.        t
d40d0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d40e0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
d40f0 20 20 20 20 63 61 73 65 20 45 58 43 4c 55 53 49      case EXCLUSI
d4100 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20 20  VE_LOCK:.       
d4110 20 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28 70   s = rangeLock(p
d4120 46 69 6c 65 2c 20 46 5f 57 52 4c 43 4b 2c 20 26  File, F_WRLCK, &
d4130 74 45 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20 20  tErrno);.       
d4140 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65   break;.      de
d4150 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 61  fault:.        a
d4160 73 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d 0a  ssert(0);.    }.
d4170 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20      if( s==(-1) 
d4180 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
d4190 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d41a0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d41b0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43  SQLITE_IOERR_LOC
d41c0 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53  K);.      if( IS
d41d0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
d41e0 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  ){.        pFile
d41f0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
d4200 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rrno;.      }.  
d4210 20 20 7d 0a 20 20 7d 0a 20 20 0a 0a 23 69 66 6e    }.  }.  ..#ifn
d4220 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20  def NDEBUG.  /* 
d4230 53 65 74 20 75 70 20 74 68 65 20 74 72 61 6e 73  Set up the trans
d4240 61 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72 20 63  action-counter c
d4250 68 61 6e 67 65 20 63 68 65 63 6b 69 6e 67 20 66  hange checking f
d4260 6c 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74  lags when.  ** t
d4270 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72 6f  ransitioning fro
d4280 6d 20 61 20 53 48 41 52 45 44 20 74 6f 20 61 20  m a SHARED to a 
d4290 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 20 20  RESERVED lock.  
d42a0 54 68 65 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20  The change.  ** 
d42b0 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20 52  from SHARED to R
d42c0 45 53 45 52 56 45 44 20 6d 61 72 6b 73 20 74 68  ESERVED marks th
d42d0 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61  e beginning of a
d42e0 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77 72 69   normal.  ** wri
d42f0 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6e 6f  te operation (no
d4300 74 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20  t a hot journal 
d4310 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f 0a  rollback)..  */.
d4320 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
d4330 5f 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c 65 2d  _OK.   && pFile-
d4340 3e 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45  >locktype<=SHARE
d4350 44 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 6c 6f 63  D_LOCK.   && loc
d4360 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f  ktype==RESERVED_
d4370 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 20 70 46  LOCK.  ){.    pF
d4380 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43 68  ile->transCntrCh
d4390 6e 67 20 3d 20 30 3b 0a 20 20 20 20 70 46 69 6c  ng = 0;.    pFil
d43a0 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b  e->dbUpdate = 0;
d43b0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f  .    pFile->inNo
d43c0 72 6d 61 6c 57 72 69 74 65 20 3d 20 31 3b 0a 20  rmalWrite = 1;. 
d43d0 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 69 66   }.#endif...  if
d43e0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
d43f0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  ){.    pFile->lo
d4400 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
d4410 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f  e;.    pLock->lo
d4420 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
d4430 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6c  e;.  }else if( l
d4440 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
d4450 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70  VE_LOCK ){.    p
d4460 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d4470 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20   PENDING_LOCK;. 
d4480 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79     pLock->lockty
d4490 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43  pe = PENDING_LOC
d44a0 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b  K;.  }..end_lock
d44b0 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74  :.  unixLeaveMut
d44c0 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34  ex();.  OSTRACE4
d44d0 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20  ("LOCK    %d %s 
d44e0 25 73 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46  %s (unix)\n", pF
d44f0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
d4500 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20  Name(locktype), 
d4510 0a 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54  .      rc==SQLIT
d4520 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66  E_OK ? "ok" : "f
d4530 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72  ailed");.  retur
d4540 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
d4550 6c 6f 73 65 20 61 6c 6c 20 66 69 6c 65 20 64 65  lose all file de
d4560 73 63 72 69 70 74 6f 72 73 20 61 63 63 75 6d 75  scriptors accumu
d4570 61 74 65 64 20 69 6e 20 74 68 65 20 75 6e 69 78  ated in the unix
d4580 4f 70 65 6e 43 6e 74 2d 3e 70 55 6e 75 73 65 64  OpenCnt->pUnused
d4590 20 6c 69 73 74 2e 0a 2a 2a 20 49 66 20 61 6c 6c   list..** If all
d45a0 20 73 75 63 68 20 66 69 6c 65 20 64 65 73 63 72   such file descr
d45b0 69 70 74 6f 72 73 20 61 72 65 20 63 6c 6f 73 65  iptors are close
d45c0 64 20 77 69 74 68 6f 75 74 20 65 72 72 6f 72 2c  d without error,
d45d0 20 74 68 65 20 6c 69 73 74 20 69 73 0a 2a 2a 20   the list is.** 
d45e0 63 6c 65 61 72 65 64 20 61 6e 64 20 53 51 4c 49  cleared and SQLI
d45f0 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a  TE_OK returned..
d4600 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
d4610 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   if an error occ
d4620 75 72 73 2c 20 74 68 65 6e 20 73 75 63 63 65 73  urs, then succes
d4630 73 66 75 6c 6c 79 20 63 6c 6f 73 65 64 20 66 69  sfully closed fi
d4640 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a  le descriptor.**
d4650 20 65 6e 74 72 69 65 73 20 61 72 65 20 72 65 6d   entries are rem
d4660 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 6c 69  oved from the li
d4670 73 74 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 49  st, and SQLITE_I
d4680 4f 45 52 52 5f 43 4c 4f 53 45 20 72 65 74 75 72  OERR_CLOSE retur
d4690 6e 65 64 2e 20 0a 2a 2a 20 6e 6f 74 20 64 65 6c  ned. .** not del
d46a0 65 74 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f  eted and SQLITE_
d46b0 49 4f 45 52 52 5f 43 4c 4f 53 45 20 72 65 74 75  IOERR_CLOSE retu
d46c0 72 6e 65 64 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  rned..*/ .static
d46d0 20 69 6e 74 20 63 6c 6f 73 65 50 65 6e 64 69 6e   int closePendin
d46e0 67 46 64 73 28 75 6e 69 78 46 69 6c 65 20 2a 70  gFds(unixFile *p
d46f0 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  File){.  int rc 
d4700 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73  = SQLITE_OK;.  s
d4710 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
d4720 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65  t *pOpen = pFile
d4730 2d 3e 70 4f 70 65 6e 3b 0a 20 20 55 6e 69 78 55  ->pOpen;.  UnixU
d4740 6e 75 73 65 64 46 64 20 2a 70 45 72 72 6f 72 20  nusedFd *pError 
d4750 3d 20 30 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65  = 0;.  UnixUnuse
d4760 64 46 64 20 2a 70 3b 0a 20 20 55 6e 69 78 55 6e  dFd *p;.  UnixUn
d4770 75 73 65 64 46 64 20 2a 70 4e 65 78 74 3b 0a 20  usedFd *pNext;. 
d4780 20 66 6f 72 28 70 3d 70 4f 70 65 6e 2d 3e 70 55   for(p=pOpen->pU
d4790 6e 75 73 65 64 3b 20 70 3b 20 70 3d 70 4e 65 78  nused; p; p=pNex
d47a0 74 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20  t){.    pNext = 
d47b0 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66  p->pNext;.    if
d47c0 28 20 63 6c 6f 73 65 28 70 2d 3e 66 64 29 20 29  ( close(p->fd) )
d47d0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
d47e0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
d47f0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
d4800 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b  ITE_IOERR_CLOSE;
d4810 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20  .      p->pNext 
d4820 3d 20 70 45 72 72 6f 72 3b 0a 20 20 20 20 20 20  = pError;.      
d4830 70 45 72 72 6f 72 20 3d 20 70 3b 0a 20 20 20 20  pError = p;.    
d4840 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
d4850 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20  ite3_free(p);.  
d4860 20 20 7d 0a 20 20 7d 0a 20 20 70 4f 70 65 6e 2d    }.  }.  pOpen-
d4870 3e 70 55 6e 75 73 65 64 20 3d 20 70 45 72 72 6f  >pUnused = pErro
d4880 72 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  r;.  return rc;.
d4890 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65  }../*.** Add the
d48a0 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
d48b0 20 75 73 65 64 20 62 79 20 66 69 6c 65 20 68 61   used by file ha
d48c0 6e 64 6c 65 20 70 46 69 6c 65 20 74 6f 20 74 68  ndle pFile to th
d48d0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a  e corresponding.
d48e0 2a 2a 20 70 55 6e 75 73 65 64 20 6c 69 73 74 2e  ** pUnused list.
d48f0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
d4900 73 65 74 50 65 6e 64 69 6e 67 46 64 28 75 6e 69  setPendingFd(uni
d4910 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20  xFile *pFile){. 
d4920 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e   struct unixOpen
d4930 43 6e 74 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69  Cnt *pOpen = pFi
d4940 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 55 6e 69  le->pOpen;.  Uni
d4950 78 55 6e 75 73 65 64 46 64 20 2a 70 20 3d 20 70  xUnusedFd *p = p
d4960 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3b 0a 20  File->pUnused;. 
d4970 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65   p->pNext = pOpe
d4980 6e 2d 3e 70 55 6e 75 73 65 64 3b 0a 20 20 70 4f  n->pUnused;.  pO
d4990 70 65 6e 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70  pen->pUnused = p
d49a0 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d  ;.  pFile->h = -
d49b0 31 3b 0a 20 20 70 46 69 6c 65 2d 3e 70 55 6e 75  1;.  pFile->pUnu
d49c0 73 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  sed = 0;.}../*.*
d49d0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b  * Lower the lock
d49e0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c  ing level on fil
d49f0 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69  e descriptor pFi
d4a00 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20  le to locktype. 
d4a10 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73   locktype.** mus
d4a20 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c  t be either NO_L
d4a30 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f  OCK or SHARED_LO
d4a40 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  CK..**.** If the
d4a50 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
d4a60 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  f the file descr
d4a70 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79  iptor is already
d4a80 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20   at or below.** 
d4a90 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f  the requested lo
d4aa0 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69  cking level, thi
d4ab0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
d4ac0 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
d4ad0 69 6e 74 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 73  int unixUnlock(s
d4ae0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d4af0 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a   int locktype){.
d4b00 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d4b10 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d4b20 64 3b 20 2f 2a 20 54 68 65 20 6f 70 65 6e 20 66  d; /* The open f
d4b30 69 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ile */.  struct 
d4b40 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c  unixLockInfo *pL
d4b50 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 53 74 72  ock;      /* Str
d4b60 75 63 74 75 72 65 20 64 65 73 63 72 69 62 69 6e  ucture describin
d4b70 67 20 63 75 72 72 65 6e 74 20 6c 6f 63 6b 20 73  g current lock s
d4b80 74 61 74 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  tate */.  struct
d4b90 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20 20 20 20   flock lock;    
d4ba0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
d4bb0 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64  formation passed
d4bc0 20 69 6e 74 6f 20 66 63 6e 74 6c 28 29 20 2a 2f   into fcntl() */
d4bd0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d4be0 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
d4bf0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
d4c00 64 65 20 66 72 6f 6d 20 74 68 69 73 20 69 6e 74  de from this int
d4c10 65 72 66 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20  erface */.  int 
d4c20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
d4c30 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
d4c40 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 66  The underlying f
d4c50 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a  ile descriptor *
d4c60 2f 0a 20 20 69 6e 74 20 74 45 72 72 6e 6f 3b 20  /.  int tErrno; 
d4c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d4c80 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f       /* Error co
d4c90 64 65 20 66 72 6f 6d 20 73 79 73 74 65 6d 20 63  de from system c
d4ca0 61 6c 6c 20 65 72 72 6f 72 73 20 2a 2f 0a 0a 20  all errors */.. 
d4cb0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d4cc0 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22 55 4e  ;.  OSTRACE7("UN
d4cd0 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20  LOCK  %d %d was 
d4ce0 25 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25 64  %d(%d,%d) pid=%d
d4cf0 20 28 75 6e 69 78 29 5c 6e 22 2c 20 70 46 69 6c   (unix)\n", pFil
d4d00 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a  e->h, locktype,.
d4d10 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
d4d20 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e 70 4c  ktype, pFile->pL
d4d30 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70  ock->locktype, p
d4d40 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 63 6e 74  File->pLock->cnt
d4d50 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20  , getpid());..  
d4d60 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
d4d70 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  <=SHARED_LOCK );
d4d80 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
d4d90 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65  cktype<=locktype
d4da0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
d4db0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
d4dc0 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44  if( CHECK_THREAD
d4dd0 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20  ID(pFile) ){.   
d4de0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
d4df0 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69  ISUSE;.  }.  uni
d4e00 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  xEnterMutex();. 
d4e10 20 68 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20   h = pFile->h;. 
d4e20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e   pLock = pFile->
d4e30 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74 28  pLock;.  assert(
d4e40 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 3d 30 20 29   pLock->cnt!=0 )
d4e50 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  ;.  if( pFile->l
d4e60 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
d4e70 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72  OCK ){.    asser
d4e80 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79  t( pLock->lockty
d4e90 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  pe==pFile->lockt
d4ea0 79 70 65 20 29 3b 0a 20 20 20 20 53 69 6d 75 6c  ype );.    Simul
d4eb0 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
d4ec0 28 31 29 3b 0a 20 20 20 20 53 69 6d 75 6c 61 74  (1);.    Simulat
d4ed0 65 49 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 29  eIOError( h=(-1)
d4ee0 20 29 0a 20 20 20 20 53 69 6d 75 6c 61 74 65 49   ).    SimulateI
d4ef0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b  OErrorBenign(0);
d4f00 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
d4f10 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 72 65 64  .    /* When red
d4f20 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20 73 75 63  ucing a lock suc
d4f30 68 20 74 68 61 74 20 6f 74 68 65 72 20 70 72 6f  h that other pro
d4f40 63 65 73 73 65 73 20 63 61 6e 20 73 74 61 72 74  cesses can start
d4f50 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20  .    ** reading 
d4f60 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
d4f70 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65 20 73 75  e again, make su
d4f80 72 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20  re that the.    
d4f90 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63  ** transaction c
d4fa0 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61 74  ounter was updat
d4fb0 65 64 20 69 66 20 61 6e 79 20 70 61 72 74 20 6f  ed if any part o
d4fc0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20  f the database. 
d4fd0 20 20 20 2a 2a 20 66 69 6c 65 20 63 68 61 6e 67     ** file chang
d4fe0 65 64 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e  ed.  If the tran
d4ff0 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20  saction counter 
d5000 69 73 20 6e 6f 74 20 75 70 64 61 74 65 64 2c 0a  is not updated,.
d5010 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 63 6f 6e      ** other con
d5020 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65 20  nections to the 
d5030 73 61 6d 65 20 66 69 6c 65 20 6d 69 67 68 74 20  same file might 
d5040 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74 68 61 74  not realize that
d5050 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 69 6c 65  .    ** the file
d5060 20 68 61 73 20 63 68 61 6e 67 65 64 20 61 6e 64   has changed and
d5070 20 68 65 6e 63 65 20 6d 69 67 68 74 20 6e 6f 74   hence might not
d5080 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73 68 20 74   know to flush t
d5090 68 65 69 72 0a 20 20 20 20 2a 2a 20 63 61 63 68  heir.    ** cach
d50a0 65 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20 61  e.  The use of a
d50b0 20 73 74 61 6c 65 20 63 61 63 68 65 20 63 61 6e   stale cache can
d50c0 20 6c 65 61 64 20 74 6f 20 64 61 74 61 62 61 73   lead to databas
d50d0 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20  e corruption..  
d50e0 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
d50f0 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c   pFile->inNormal
d5100 57 72 69 74 65 3d 3d 30 0a 20 20 20 20 20 20 20  Write==0.       
d5110 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64 62 55 70    || pFile->dbUp
d5120 64 61 74 65 3d 3d 30 0a 20 20 20 20 20 20 20 20  date==0.        
d5130 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73   || pFile->trans
d5140 43 6e 74 72 43 68 6e 67 3d 3d 31 20 29 3b 0a 20  CntrChng==1 );. 
d5150 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d     pFile->inNorm
d5160 61 6c 57 72 69 74 65 20 3d 20 30 3b 0a 23 65 6e  alWrite = 0;.#en
d5170 64 69 66 0a 0a 0a 20 20 20 20 69 66 28 20 6c 6f  dif...    if( lo
d5180 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
d5190 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28  OCK ){.      if(
d51a0 20 72 61 6e 67 65 4c 6f 63 6b 28 70 46 69 6c 65   rangeLock(pFile
d51b0 2c 20 46 5f 52 44 4c 43 4b 2c 20 26 74 45 72 72  , F_RDLCK, &tErr
d51c0 6e 6f 29 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20  no)==(-1) ){.   
d51d0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
d51e0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
d51f0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
d5200 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 29  TE_IOERR_RDLOCK)
d5210 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 53  ;.        if( IS
d5220 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20  _LOCK_ERROR(rc) 
d5230 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46 69  ){.          pFi
d5240 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d5250 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20  tErrno;.        
d5260 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  }.        goto e
d5270 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20  nd_unlock;.     
d5280 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63   }.    }.    loc
d5290 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c  k.l_type = F_UNL
d52a0 43 4b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77  CK;.    lock.l_w
d52b0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
d52c0 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61  ;.    lock.l_sta
d52d0 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54  rt = PENDING_BYT
d52e0 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65  E;.    lock.l_le
d52f0 6e 20 3d 20 32 4c 3b 20 20 61 73 73 65 72 74 28  n = 2L;  assert(
d5300 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d   PENDING_BYTE+1=
d5310 3d 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 29  =RESERVED_BYTE )
d5320 3b 0a 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28  ;.    if( fcntl(
d5330 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63  h, F_SETLK, &loc
d5340 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20  k)!=(-1) ){.    
d5350 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70    pLock->locktyp
d5360 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b  e = SHARED_LOCK;
d5370 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
d5380 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f    tErrno = errno
d5390 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
d53a0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d53b0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d53c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
d53d0 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49  CK);.      if( I
d53e0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d53f0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
d5400 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d5410 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  Errno;.      }. 
d5420 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 75 6e       goto end_un
d5430 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  lock;.    }.  }.
d5440 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
d5450 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73  NO_LOCK ){.    s
d5460 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e  truct unixOpenCn
d5470 74 20 2a 70 4f 70 65 6e 3b 0a 0a 20 20 20 20 2f  t *pOpen;..    /
d5480 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  * Decrement the 
d5490 73 68 61 72 65 64 20 6c 6f 63 6b 20 63 6f 75 6e  shared lock coun
d54a0 74 65 72 2e 20 20 52 65 6c 65 61 73 65 20 74 68  ter.  Release th
d54b0 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 61 6e 0a  e lock using an.
d54c0 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f      ** OS call o
d54d0 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20 74 68 72  nly when all thr
d54e0 65 61 64 73 20 69 6e 20 74 68 69 73 20 73 61 6d  eads in this sam
d54f0 65 20 70 72 6f 63 65 73 73 20 68 61 76 65 20 72  e process have r
d5500 65 6c 65 61 73 65 64 0a 20 20 20 20 2a 2a 20 74  eleased.    ** t
d5510 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a  he lock..    */.
d5520 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2d 2d      pLock->cnt--
d5530 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d  ;.    if( pLock-
d5540 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  >cnt==0 ){.     
d5550 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46   lock.l_type = F
d5560 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f  _UNLCK;.      lo
d5570 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45  ck.l_whence = SE
d5580 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f  EK_SET;.      lo
d5590 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 6c 6f 63  ck.l_start = loc
d55a0 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b 0a 20 20  k.l_len = 0L;.  
d55b0 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
d55c0 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20  rorBenign(1);.  
d55d0 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
d55e0 72 6f 72 28 20 68 3d 28 2d 31 29 20 29 0a 20 20  ror( h=(-1) ).  
d55f0 20 20 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72      SimulateIOEr
d5600 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b 0a 20 20  rorBenign(0);.  
d5610 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68 2c      if( fcntl(h,
d5620 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29   F_SETLK, &lock)
d5630 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  !=(-1) ){.      
d5640 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70    pLock->locktyp
d5650 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20  e = NO_LOCK;.   
d5660 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
d5670 20 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f    tErrno = errno
d5680 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
d5690 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d56a0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d56b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
d56c0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69  LOCK);.        i
d56d0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
d56e0 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (rc) ){.        
d56f0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d5700 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d5710 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
d5720 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  Lock->locktype =
d5730 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20   NO_LOCK;.      
d5740 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
d5750 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20  e = NO_LOCK;.   
d5760 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
d5770 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  /* Decrement the
d5780 20 63 6f 75 6e 74 20 6f 66 20 6c 6f 63 6b 73 20   count of locks 
d5790 61 67 61 69 6e 73 74 20 74 68 69 73 20 73 61 6d  against this sam
d57a0 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 20 74 68  e file.  When th
d57b0 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e 74 20 72  e.    ** count r
d57c0 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 63 6c 6f  eaches zero, clo
d57d0 73 65 20 61 6e 79 20 6f 74 68 65 72 20 66 69 6c  se any other fil
d57e0 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 77 68  e descriptors wh
d57f0 6f 73 65 20 63 6c 6f 73 65 0a 20 20 20 20 2a 2a  ose close.    **
d5800 20 77 61 73 20 64 65 66 65 72 72 65 64 20 62 65   was deferred be
d5810 63 61 75 73 65 20 6f 66 20 6f 75 74 73 74 61 6e  cause of outstan
d5820 64 69 6e 67 20 6c 6f 63 6b 73 2e 0a 20 20 20 20  ding locks..    
d5830 2a 2f 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 70  */.    pOpen = p
d5840 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20 20 20  File->pOpen;.   
d5850 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2d 2d 3b   pOpen->nLock--;
d5860 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
d5870 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20 29 3b 0a  en->nLock>=0 );.
d5880 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d 3e 6e      if( pOpen->n
d5890 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Lock==0 ){.     
d58a0 20 69 6e 74 20 72 63 32 20 3d 20 63 6c 6f 73 65   int rc2 = close
d58b0 50 65 6e 64 69 6e 67 46 64 73 28 70 46 69 6c 65  PendingFds(pFile
d58c0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
d58d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
d58e0 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a        rc = rc2;.
d58f0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
d5900 7d 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a 0a  }...end_unlock:.
d5910 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
d5920 28 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  ();.  if( rc==SQ
d5930 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d  LITE_OK ) pFile-
d5940 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
d5950 74 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72  type;.  return r
d5960 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
d5970 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72   function perfor
d5980 6d 73 20 74 68 65 20 70 61 72 74 73 20 6f 66 20  ms the parts of 
d5990 74 68 65 20 22 63 6c 6f 73 65 20 66 69 6c 65 22  the "close file"
d59a0 20 6f 70 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63   operation .** c
d59b0 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63  ommon to all loc
d59c0 6b 69 6e 67 20 73 63 68 65 6d 65 73 2e 20 49 74  king schemes. It
d59d0 20 63 6c 6f 73 65 73 20 74 68 65 20 64 69 72 65   closes the dire
d59e0 63 74 6f 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a  ctory and file.*
d59f0 2a 20 68 61 6e 64 6c 65 73 2c 20 69 66 20 74 68  * handles, if th
d5a00 65 79 20 61 72 65 20 76 61 6c 69 64 2c 20 61 6e  ey are valid, an
d5a10 64 20 73 65 74 73 20 61 6c 6c 20 66 69 65 6c 64  d sets all field
d5a20 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c  s of the unixFil
d5a30 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74  e.** structure t
d5a40 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  o 0..**.** It is
d5a50 20 2a 6e 6f 74 2a 20 6e 65 63 65 73 73 61 72 79   *not* necessary
d5a60 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 75 74   to hold the mut
d5a70 65 78 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  ex when this rou
d5a80 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 0a  tine is called,.
d5a90 2a 2a 20 65 76 65 6e 20 6f 6e 20 56 78 57 6f 72  ** even on VxWor
d5aa0 6b 73 2e 20 20 41 20 6d 75 74 65 78 20 77 69 6c  ks.  A mutex wil
d5ab0 6c 20 62 65 20 61 63 71 75 69 72 65 64 20 6f 6e  l be acquired on
d5ac0 20 56 78 57 6f 72 6b 73 20 62 79 20 74 68 65 0a   VxWorks by the.
d5ad0 2a 2a 20 76 78 77 6f 72 6b 73 52 65 6c 65 61 73  ** vxworksReleas
d5ae0 65 46 69 6c 65 49 64 28 29 20 72 6f 75 74 69 6e  eFileId() routin
d5af0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
d5b00 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 73   closeUnixFile(s
d5b10 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
d5b20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
d5b30 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
d5b40 29 69 64 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  )id;.  if( pFile
d5b50 20 29 7b 0a 20 20 20 20 69 66 28 20 70 46 69 6c   ){.    if( pFil
d5b60 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20  e->dirfd>=0 ){. 
d5b70 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 63       int err = c
d5b80 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69 72 66  lose(pFile->dirf
d5b90 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 72  d);.      if( er
d5ba0 72 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  r ){.        pFi
d5bb0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d5bc0 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72  errno;.        r
d5bd0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
d5be0 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20  RR_DIR_CLOSE;.  
d5bf0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d5c00 20 20 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3d     pFile->dirfd=
d5c10 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
d5c20 7d 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d  }.    if( pFile-
d5c30 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  >h>=0 ){.      i
d5c40 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70  nt err = close(p
d5c50 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 20 20  File->h);.      
d5c60 69 66 28 20 65 72 72 20 29 7b 0a 20 20 20 20 20  if( err ){.     
d5c70 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
d5c80 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d5c90 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
d5ca0 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 3b 0a  TE_IOERR_CLOSE;.
d5cb0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69        }.    }.#i
d5cc0 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20  f OS_VXWORKS.   
d5cd0 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 49 64 20   if( pFile->pId 
d5ce0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 46 69  ){.      if( pFi
d5cf0 6c 65 2d 3e 69 73 44 65 6c 65 74 65 20 29 7b 0a  le->isDelete ){.
d5d00 20 20 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70          unlink(p
d5d10 46 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f  File->pId->zCano
d5d20 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20  nicalName);.    
d5d30 20 20 7d 0a 20 20 20 20 20 20 76 78 77 6f 72 6b    }.      vxwork
d5d40 73 52 65 6c 65 61 73 65 46 69 6c 65 49 64 28 70  sReleaseFileId(p
d5d50 46 69 6c 65 2d 3e 70 49 64 29 3b 0a 20 20 20 20  File->pId);.    
d5d60 20 20 70 46 69 6c 65 2d 3e 70 49 64 20 3d 20 30    pFile->pId = 0
d5d70 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
d5d80 20 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f     OSTRACE2("CLO
d5d90 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c 20 70 46  SE   %-3d\n", pF
d5da0 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20 4f 70 65  ile->h);.    Ope
d5db0 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20 20  nCounter(-1);.  
d5dc0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
d5dd0 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 29 3b 0a  File->pUnused);.
d5de0 20 20 20 20 6d 65 6d 73 65 74 28 70 46 69 6c 65      memset(pFile
d5df0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78  , 0, sizeof(unix
d5e00 46 69 6c 65 29 29 3b 0a 20 20 7d 0a 20 20 72 65  File));.  }.  re
d5e10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d5e20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
d5e30 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
d5e40 20 69 6e 74 20 75 6e 69 78 43 6c 6f 73 65 28 73   int unixClose(s
d5e50 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
d5e60 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
d5e70 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 64  ITE_OK;.  if( id
d5e80 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65   ){.    unixFile
d5e90 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
d5ea0 69 6c 65 20 2a 29 69 64 3b 0a 20 20 20 20 75 6e  ile *)id;.    un
d5eb0 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f  ixUnlock(id, NO_
d5ec0 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e 69 78 45  LOCK);.    unixE
d5ed0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
d5ee0 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65   if( pFile->pOpe
d5ef0 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65  n && pFile->pOpe
d5f00 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  n->nLock ){.    
d5f10 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
d5f20 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f  e outstanding lo
d5f30 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75  cks, do not actu
d5f40 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68 65 20 66  ally close the f
d5f50 69 6c 65 20 6a 75 73 74 0a 20 20 20 20 20 20 2a  ile just.      *
d5f60 2a 20 79 65 74 20 62 65 63 61 75 73 65 20 74 68  * yet because th
d5f70 61 74 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 74  at would clear t
d5f80 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73  hose locks.  Ins
d5f90 74 65 61 64 2c 20 61 64 64 20 74 68 65 20 66 69  tead, add the fi
d5fa0 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63  le.      ** desc
d5fb0 72 69 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d  riptor to pOpen-
d5fc0 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e 20 20  >pUnused list.  
d5fd0 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d  It will be autom
d5fe0 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64 20  atically closed 
d5ff0 0a 20 20 20 20 20 20 2a 2a 20 77 68 65 6e 20 74  .      ** when t
d6000 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20  he last lock is 
d6010 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 20 2a  cleared..      *
d6020 2f 0a 20 20 20 20 20 20 73 65 74 50 65 6e 64 69  /.      setPendi
d6030 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 20  ngFd(pFile);.   
d6040 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 4c 6f   }.    releaseLo
d6050 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 70 4c  ckInfo(pFile->pL
d6060 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c 65 61 73  ock);.    releas
d6070 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e  eOpenCnt(pFile->
d6080 70 4f 70 65 6e 29 3b 0a 20 20 20 20 72 63 20 3d  pOpen);.    rc =
d6090 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69   closeUnixFile(i
d60a0 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76  d);.    unixLeav
d60b0 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20  eMutex();.  }.  
d60c0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
d60d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
d60e0 64 20 6f 66 20 74 68 65 20 70 6f 73 69 78 20 61  d of the posix a
d60f0 64 76 69 73 6f 72 79 20 6c 6f 63 6b 20 69 6d 70  dvisory lock imp
d6100 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a  lementation ****
d6110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
d6120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f  ************/../
d6170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
d61c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d61d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f  ************* No
d61e0 2d 6f 70 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a  -op Locking ****
d61f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
d6210 2a 0a 2a 2a 20 4f 66 20 74 68 65 20 76 61 72 69  *.** Of the vari
d6220 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c  ous locking impl
d6230 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61 76 61 69  ementations avai
d6240 6c 61 62 6c 65 2c 20 74 68 69 73 20 69 73 20 62  lable, this is b
d6250 79 20 66 61 72 20 74 68 65 0a 2a 2a 20 73 69 6d  y far the.** sim
d6260 70 6c 65 73 74 3a 20 20 6c 6f 63 6b 69 6e 67 20  plest:  locking 
d6270 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 4e 6f 20  is ignored.  No 
d6280 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
d6290 74 6f 20 6c 6f 63 6b 20 74 68 65 20 64 61 74 61  to lock the data
d62a0 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 66 6f 72  base.** file for
d62b0 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74   reading or writ
d62c0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ing..**.** This 
d62d0 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73 20  locking mode is 
d62e0 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
d62f0 75 73 65 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79  use on read-only
d6300 20 64 61 74 61 62 61 73 65 73 0a 2a 2a 20 28 65   databases.** (e
d6310 78 3a 20 64 61 74 61 62 61 73 65 73 20 74 68 61  x: databases tha
d6320 74 20 61 72 65 20 62 75 72 6e 65 64 20 69 6e 74  t are burned int
d6330 6f 20 43 44 2d 52 4f 4d 2c 20 66 6f 72 20 65 78  o CD-ROM, for ex
d6340 61 6d 70 6c 65 2e 29 20 20 49 74 20 63 61 6e 0a  ample.)  It can.
d6350 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20  ** also be used 
d6360 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  if the applicati
d6370 6f 6e 20 65 6d 70 6c 6f 79 73 20 73 6f 6d 65 20  on employs some 
d6380 65 78 74 65 72 6e 61 6c 20 6d 65 63 68 61 6e 69  external mechani
d6390 73 6d 20 74 6f 0a 2a 2a 20 70 72 65 76 65 6e 74  sm to.** prevent
d63a0 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 20 61 63   simultaneous ac
d63b0 63 65 73 73 20 6f 66 20 74 68 65 20 73 61 6d 65  cess of the same
d63c0 20 64 61 74 61 62 61 73 65 20 62 79 20 74 77 6f   database by two
d63d0 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 64 61 74 61   or more.** data
d63e0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
d63f0 2e 20 20 42 75 74 20 74 68 65 72 65 20 69 73 20  .  But there is 
d6400 61 20 73 65 72 69 6f 75 73 20 72 69 73 6b 20 6f  a serious risk o
d6410 66 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  f database.** co
d6420 72 72 75 70 74 69 6f 6e 20 69 66 20 74 68 69 73  rruption if this
d6430 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 69 73   locking mode is
d6440 20 75 73 65 64 20 69 6e 20 73 69 74 75 61 74 69   used in situati
d6450 6f 6e 73 20 77 68 65 72 65 20 6d 75 6c 74 69 70  ons where multip
d6460 6c 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  le.** database c
d6470 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 61  onnections are a
d6480 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d  ccessing the sam
d6490 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
d64a0 61 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 74  at the same.** t
d64b0 69 6d 65 20 61 6e 64 20 6f 6e 65 20 6f 72 20 6d  ime and one or m
d64c0 6f 72 65 20 6f 66 20 74 68 6f 73 65 20 63 6f 6e  ore of those con
d64d0 6e 65 63 74 69 6f 6e 73 20 61 72 65 20 77 72 69  nections are wri
d64e0 74 69 6e 67 2e 0a 2a 2f 0a 0a 73 74 61 74 69 63  ting..*/..static
d64f0 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b   int nolockCheck
d6500 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
d6510 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73  ite3_file *NotUs
d6520 65 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  ed, int *pResOut
d6530 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
d6540 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
d6550 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 30 3b 0a    *pResOut = 0;.
d6560 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d6570 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  OK;.}.static int
d6580 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69   nolockLock(sqli
d6590 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65  te3_file *NotUse
d65a0 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29  d, int NotUsed2)
d65b0 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
d65c0 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
d65d0 6f 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75  otUsed2);.  retu
d65e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
d65f0 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63  static int noloc
d6600 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  kUnlock(sqlite3_
d6610 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69  file *NotUsed, i
d6620 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20  nt NotUsed2){.  
d6630 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
d6640 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
d6650 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ed2);.  return S
d6660 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
d6670 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c  ** Close the fil
d6680 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
d6690 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c   nolockClose(sql
d66a0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
d66b0 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55  .  return closeU
d66c0 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a  nixFile(id);.}..
d66d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
d66e0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20  **** End of the 
d66f0 6e 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d 70 6c 65  no-op lock imple
d6700 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a  mentation ******
d6710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
d6720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
d6770 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
d6780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d67a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d67b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d67c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
d67d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
d67e0 20 64 6f 74 2d 66 69 6c 65 20 4c 6f 63 6b 69 6e   dot-file Lockin
d67f0 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  g **************
d6800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6810 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f 74 66 69  .**.** The dotfi
d6820 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6d 70 6c 65  le locking imple
d6830 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 74  mentation uses t
d6840 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20  he existance of 
d6850 73 65 70 61 72 61 74 65 20 6c 6f 63 6b 0a 2a 2a  separate lock.**
d6860 20 66 69 6c 65 73 20 69 6e 20 6f 72 64 65 72 20   files in order 
d6870 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73  to control acces
d6880 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  s to the databas
d6890 65 2e 20 20 54 68 69 73 20 77 6f 72 6b 73 20 6f  e.  This works o
d68a0 6e 20 6a 75 73 74 0a 2a 2a 20 61 62 6f 75 74 20  n just.** about 
d68b0 65 76 65 72 79 20 66 69 6c 65 73 79 73 74 65 6d  every filesystem
d68c0 20 69 6d 61 67 69 6e 61 62 6c 65 2e 20 20 42 75   imaginable.  Bu
d68d0 74 20 74 68 65 72 65 20 61 72 65 20 73 65 72 69  t there are seri
d68e0 6f 75 73 20 64 6f 77 6e 73 69 64 65 73 3a 0a 2a  ous downsides:.*
d68f0 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 54 68 65  *.**    (1)  The
d6900 72 65 20 69 73 20 7a 65 72 6f 20 63 6f 6e 63 75  re is zero concu
d6910 72 72 65 6e 63 79 2e 20 20 41 20 73 69 6e 67 6c  rrency.  A singl
d6920 65 20 72 65 61 64 65 72 20 62 6c 6f 63 6b 73 20  e reader blocks 
d6930 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20  all other.**    
d6940 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e 73       connections
d6950 20 66 72 6f 6d 20 72 65 61 64 69 6e 67 20 6f 72   from reading or
d6960 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 74   writing the dat
d6970 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  abase..**.**    
d6980 28 32 29 20 20 41 6e 20 61 70 70 6c 69 63 61 74  (2)  An applicat
d6990 69 6f 6e 20 63 72 61 73 68 20 6f 72 20 70 6f 77  ion crash or pow
d69a0 65 72 20 6c 6f 73 73 20 63 61 6e 20 6c 65 61 76  er loss can leav
d69b0 65 20 73 74 61 6c 65 20 6c 6f 63 6b 20 66 69 6c  e stale lock fil
d69c0 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 69  es.**         si
d69d0 74 74 69 6e 67 20 61 72 6f 75 6e 64 20 74 68 61  tting around tha
d69e0 74 20 6e 65 65 64 20 74 6f 20 62 65 20 63 6c 65  t need to be cle
d69f0 61 72 65 64 20 6d 61 6e 75 61 6c 6c 79 2e 0a 2a  ared manually..*
d6a00 2a 0a 2a 2a 20 4e 65 76 65 72 74 68 65 6c 65 73  *.** Nevertheles
d6a10 73 2c 20 61 20 64 6f 74 6c 6f 63 6b 20 69 73 20  s, a dotlock is 
d6a20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 6c  an appropriate l
d6a30 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 66 6f 72 20  ocking mode for 
d6a40 75 73 65 20 69 66 20 6e 6f 0a 2a 2a 20 6f 74 68  use if no.** oth
d6a50 65 72 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74  er locking strat
d6a60 65 67 79 20 69 73 20 61 76 61 69 6c 61 62 6c 65  egy is available
d6a70 2e 0a 2a 2a 0a 2a 2a 20 44 6f 74 66 69 6c 65 20  ..**.** Dotfile 
d6a80 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73 20 62 79  locking works by
d6a90 20 63 72 65 61 74 69 6e 67 20 61 20 66 69 6c 65   creating a file
d6aa0 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72   in the same dir
d6ab0 65 63 74 6f 72 79 20 61 73 20 74 68 65 0a 2a 2a  ectory as the.**
d6ac0 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 77 69   database and wi
d6ad0 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  th the same name
d6ae0 20 62 75 74 20 77 69 74 68 20 61 20 22 2e 6c 6f   but with a ".lo
d6af0 63 6b 22 20 65 78 74 65 6e 73 69 6f 6e 20 61 64  ck" extension ad
d6b00 64 65 64 2e 0a 2a 2a 20 54 68 65 20 65 78 69 73  ded..** The exis
d6b10 74 61 6e 63 65 20 6f 66 20 61 20 6c 6f 63 6b 20  tance of a lock 
d6b20 66 69 6c 65 20 69 6d 70 6c 69 65 73 20 61 6e 20  file implies an 
d6b30 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20  EXCLUSIVE lock. 
d6b40 20 41 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 0a   All other lock.
d6b50 2a 2a 20 74 79 70 65 73 20 28 53 48 41 52 45 44  ** types (SHARED
d6b60 2c 20 52 45 53 45 52 56 45 44 2c 20 50 45 4e 44  , RESERVED, PEND
d6b70 49 4e 47 29 20 61 72 65 20 6d 61 70 70 65 64 20  ING) are mapped 
d6b80 69 6e 74 6f 20 45 58 43 4c 55 53 49 56 45 2e 0a  into EXCLUSIVE..
d6b90 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 69  */../*.** The fi
d6ba0 6c 65 20 73 75 66 66 69 78 20 61 64 64 65 64 20  le suffix added 
d6bb0 74 6f 20 74 68 65 20 64 61 74 61 20 62 61 73 65  to the data base
d6bc0 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6f 72 64   filename in ord
d6bd0 65 72 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  er to create the
d6be0 0a 2a 2a 20 6c 6f 63 6b 20 66 69 6c 65 2e 0a 2a  .** lock file..*
d6bf0 2f 0a 23 64 65 66 69 6e 65 20 44 4f 54 4c 4f 43  /.#define DOTLOC
d6c00 4b 5f 53 55 46 46 49 58 20 22 2e 6c 6f 63 6b 22  K_SUFFIX ".lock"
d6c10 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
d6c20 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74  tine checks if t
d6c30 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56  here is a RESERV
d6c40 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  ED lock held on 
d6c50 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
d6c60 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72   file by this or
d6c70 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
d6c80 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f  ss. If such a lo
d6c90 63 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20  ck is held, set 
d6ca0 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61  *pResOut.** to a
d6cb0 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20   non-zero value 
d6cc0 6f 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f  otherwise *pResO
d6cd0 75 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72  ut is set to zer
d6ce0 6f 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76  o.  The return v
d6cf0 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74  alue.** is set t
d6d00 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65  o SQLITE_OK unle
d6d10 73 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20  ss an I/O error 
d6d20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f  occurs during lo
d6d30 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2a 0a  ck checking..**.
d6d40 2a 2a 20 49 6e 20 64 6f 74 66 69 6c 65 20 6c 6f  ** In dotfile lo
d6d50 63 6b 69 6e 67 2c 20 65 69 74 68 65 72 20 61 20  cking, either a 
d6d60 6c 6f 63 6b 20 65 78 69 73 74 73 20 6f 72 20 69  lock exists or i
d6d70 74 20 64 6f 65 73 20 6e 6f 74 2e 20 20 53 6f 20  t does not.  So 
d6d80 69 6e 20 74 68 69 73 0a 2a 2a 20 76 61 72 69 61  in this.** varia
d6d90 74 69 6f 6e 20 6f 66 20 43 68 65 63 6b 52 65 73  tion of CheckRes
d6da0 65 72 76 65 64 4c 6f 63 6b 28 29 2c 20 2a 70 52  ervedLock(), *pR
d6db0 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20  esOut is set to 
d6dc0 74 72 75 65 20 69 66 20 61 6e 79 20 6c 6f 63 6b  true if any lock
d6dd0 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f 6e 20 74  .** is held on t
d6de0 68 65 20 66 69 6c 65 20 61 6e 64 20 66 61 6c 73  he file and fals
d6df0 65 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73  e if the file is
d6e00 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f 0a 73 74   unlocked..*/.st
d6e10 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b  atic int dotlock
d6e20 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
d6e30 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
d6e40 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74  id, int *pResOut
d6e50 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ) {.  int rc = S
d6e60 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
d6e70 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20  reserved = 0;.  
d6e80 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d6e90 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d6ea0 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ..  SimulateIOEr
d6eb0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
d6ec0 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45  TE_IOERR_CHECKRE
d6ed0 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20  SERVEDLOCK; );. 
d6ee0 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c   .  assert( pFil
d6ef0 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  e );..  /* Check
d6f00 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20   if a thread in 
d6f10 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c  this process hol
d6f20 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a  ds such a lock *
d6f30 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  /.  if( pFile->l
d6f40 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f 4c  ocktype>SHARED_L
d6f50 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69  OCK ){.    /* Ei
d6f60 74 68 65 72 20 74 68 69 73 20 63 6f 6e 6e 65 63  ther this connec
d6f70 74 69 6f 6e 20 6f 72 20 73 6f 6d 65 20 6f 74 68  tion or some oth
d6f80 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e  er connection in
d6f90 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73   the same proces
d6fa0 73 0a 20 20 20 20 2a 2a 20 68 6f 6c 64 73 20 61  s.    ** holds a
d6fb0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c   lock on the fil
d6fc0 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74 6f 20 63  e.  No need to c
d6fd0 68 65 63 6b 20 66 75 72 74 68 65 72 2e 20 2a 2f  heck further. */
d6fe0 0a 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20  .    reserved = 
d6ff0 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
d7000 2f 2a 20 54 68 65 20 6c 6f 63 6b 20 69 73 20 68  /* The lock is h
d7010 65 6c 64 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20  eld if and only 
d7020 69 66 20 74 68 65 20 6c 6f 63 6b 66 69 6c 65 20  if the lockfile 
d7030 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 63 6f  exists */.    co
d7040 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46  nst char *zLockF
d7050 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ile = (const cha
d7060 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e  r*)pFile->lockin
d7070 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 72 65  gContext;.    re
d7080 73 65 72 76 65 64 20 3d 20 61 63 63 65 73 73 28  served = access(
d7090 7a 4c 6f 63 6b 46 69 6c 65 2c 20 30 29 3d 3d 30  zLockFile, 0)==0
d70a0 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34  ;.  }.  OSTRACE4
d70b0 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25  ("TEST WR-LOCK %
d70c0 64 20 25 64 20 25 64 20 28 64 6f 74 6c 6f 63 6b  d %d %d (dotlock
d70d0 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  )\n", pFile->h, 
d70e0 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a 20  rc, reserved);. 
d70f0 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65   *pResOut = rese
d7100 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72  rved;.  return r
d7110 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b  c;.}../*.** Lock
d7120 20 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74   the file with t
d7130 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65  he lock specifie
d7140 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c  d by parameter l
d7150 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a  ocktype - one.**
d7160 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
d7170 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29  g:.**.**     (1)
d7180 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20   SHARED_LOCK.** 
d7190 20 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44      (2) RESERVED
d71a0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29  _LOCK.**     (3)
d71b0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a   PENDING_LOCK.**
d71c0 20 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49       (4) EXCLUSI
d71d0 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f  VE_LOCK.**.** So
d71e0 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71  metimes when req
d71f0 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b  uesting one lock
d7200 20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e   state, addition
d7210 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a  al lock states.*
d7220 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69  * are inserted i
d7230 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20  n between.  The 
d7240 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61  locking might fa
d7250 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  il on one of the
d7260 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69   later.** transi
d7270 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68  tions leaving th
d7280 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66  e lock state dif
d7290 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74  ferent from what
d72a0 20 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a   it started but.
d72b0 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f  ** still short o
d72c0 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65  f its goal.  The
d72d0 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74   following chart
d72e0 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77   shows the allow
d72f0 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  ed.** transition
d7300 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74  s and the insert
d7310 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ed intermediate 
d7320 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  states:.**.**   
d7330 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41   UNLOCKED -> SHA
d7340 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  RED.**    SHARED
d7350 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20   -> RESERVED.** 
d7360 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45     SHARED -> (PE
d7370 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
d7380 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56  IVE.**    RESERV
d7390 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
d73a0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
d73b0 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58     PENDING -> EX
d73c0 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68  CLUSIVE.**.** Th
d73d0 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
d73e0 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20  only increase a 
d73f0 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73  lock.  Use the s
d7400 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29  qlite3OsUnlock()
d7410 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c  .** routine to l
d7420 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c  ower a locking l
d7430 65 76 65 6c 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68  evel..**.** With
d7440 20 64 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67   dotfile locking
d7450 2c 20 77 65 20 72 65 61 6c 6c 79 20 6f 6e 6c 79  , we really only
d7460 20 73 75 70 70 6f 72 74 20 73 74 61 74 65 20 28   support state (
d7470 34 29 3a 20 45 58 43 4c 55 53 49 56 45 2e 0a 2a  4): EXCLUSIVE..*
d7480 2a 20 42 75 74 20 77 65 20 74 72 61 63 6b 20 74  * But we track t
d7490 68 65 20 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67  he other locking
d74a0 20 6c 65 76 65 6c 73 20 69 6e 74 65 72 6e 61 6c   levels internal
d74b0 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ly..*/.static in
d74c0 74 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b 28 73 71  t dotlockLock(sq
d74d0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
d74e0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a  int locktype) {.
d74f0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d7500 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d7510 64 3b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20 63  d;.  int fd;.  c
d7520 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d  har *zLockFile =
d7530 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e   (char *)pFile->
d7540 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a  lockingContext;.
d7550 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
d7560 45 5f 4f 4b 3b 0a 0a 0a 20 20 2f 2a 20 49 66 20  E_OK;...  /* If 
d7570 77 65 20 68 61 76 65 20 61 6e 79 20 6c 6f 63 6b  we have any lock
d7580 2c 20 74 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20  , then the lock 
d7590 66 69 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69  file already exi
d75a0 73 74 73 2e 20 20 41 6c 6c 20 77 65 20 68 61 76  sts.  All we hav
d75b0 65 0a 20 20 2a 2a 20 74 6f 20 64 6f 20 69 73 20  e.  ** to do is 
d75c0 61 64 6a 75 73 74 20 6f 75 72 20 69 6e 74 65 72  adjust our inter
d75d0 6e 61 6c 20 72 65 63 6f 72 64 20 6f 66 20 74 68  nal record of th
d75e0 65 20 6c 6f 63 6b 20 6c 65 76 65 6c 2e 0a 20 20  e lock level..  
d75f0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
d7600 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f  locktype > NO_LO
d7610 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  CK ){.    pFile-
d7620 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
d7630 74 79 70 65 3b 0a 23 69 66 20 21 4f 53 5f 56 58  type;.#if !OS_VX
d7640 57 4f 52 4b 53 0a 20 20 20 20 2f 2a 20 41 6c 77  WORKS.    /* Alw
d7650 61 79 73 20 75 70 64 61 74 65 20 74 68 65 20 74  ays update the t
d7660 69 6d 65 73 74 61 6d 70 20 6f 6e 20 74 68 65 20  imestamp on the 
d7670 6f 6c 64 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  old file */.    
d7680 75 74 69 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65  utimes(zLockFile
d7690 2c 20 4e 55 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a  , NULL);.#endif.
d76a0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d76b0 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  E_OK;.  }.  .  /
d76c0 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73  * grab an exclus
d76d0 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 66 64  ive lock */.  fd
d76e0 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 6b 46 69 6c   = open(zLockFil
d76f0 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 43 52 45  e,O_RDONLY|O_CRE
d7700 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 30 30 29 3b  AT|O_EXCL,0600);
d7710 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20  .  if( fd<0 ){. 
d7720 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 74 6f 20     /* failed to 
d7730 6f 70 65 6e 2f 63 72 65 61 74 65 20 74 68 65 20  open/create the 
d7740 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65 20 65 6c  file, someone el
d7750 73 65 20 6d 61 79 20 68 61 76 65 20 73 74 6f 6c  se may have stol
d7760 65 6e 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20  en the lock */. 
d7770 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20     int tErrno = 
d7780 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20 45  errno;.    if( E
d7790 45 58 49 53 54 20 3d 3d 20 74 45 72 72 6e 6f 20  EXIST == tErrno 
d77a0 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
d77b0 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d  LITE_BUSY;.    }
d77c0 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 72 63   else {.      rc
d77d0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72   = sqliteErrorFr
d77e0 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72  omPosixError(tEr
d77f0 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52  rno, SQLITE_IOER
d7800 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69  R_LOCK);.      i
d7810 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
d7820 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (rc) ){.        
d7830 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d7840 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20   = tErrno;.     
d7850 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74   }.    }.    ret
d7860 75 72 6e 20 72 63 3b 0a 20 20 7d 20 0a 20 20 69  urn rc;.  } .  i
d7870 66 28 20 63 6c 6f 73 65 28 66 64 29 20 29 7b 0a  f( close(fd) ){.
d7880 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d7890 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
d78a0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
d78b0 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 7d 0a 20  ERR_CLOSE;.  }. 
d78c0 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73   .  /* got it, s
d78d0 65 74 20 74 68 65 20 74 79 70 65 20 61 6e 64 20  et the type and 
d78e0 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 70  return ok */.  p
d78f0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d7900 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74   locktype;.  ret
d7910 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
d7920 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   Lower the locki
d7930 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65  ng level on file
d7940 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c   descriptor pFil
d7950 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20  e to locktype.  
d7960 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74  locktype.** must
d7970 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
d7980 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43  CK or SHARED_LOC
d7990 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  K..**.** If the 
d79a0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66  locking level of
d79b0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
d79c0 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  ptor is already 
d79d0 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74  at or below.** t
d79e0 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
d79f0 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73  king level, this
d7a00 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
d7a10 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  -op..**.** When 
d7a20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
d7a30 6c 20 72 65 61 63 68 65 73 20 4e 4f 5f 4c 4f 43  l reaches NO_LOC
d7a40 4b 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6c 6f  K, delete the lo
d7a50 63 6b 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74  ck file..*/.stat
d7a60 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b 55 6e  ic int dotlockUn
d7a70 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
d7a80 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
d7a90 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c  ype) {.  unixFil
d7aa0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
d7ab0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 63 68 61 72  File*)id;.  char
d7ac0 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63   *zLockFile = (c
d7ad0 68 61 72 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  har *)pFile->loc
d7ae0 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20  kingContext;..  
d7af0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
d7b00 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c  .  OSTRACE5("UNL
d7b10 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25  OCK  %d %d was %
d7b20 64 20 70 69 64 3d 25 64 20 28 64 6f 74 6c 6f 63  d pid=%d (dotloc
d7b30 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  k)\n", pFile->h,
d7b40 20 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 20 70   locktype,..   p
d7b50 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20  File->locktype, 
d7b60 67 65 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73  getpid());.  ass
d7b70 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53  ert( locktype<=S
d7b80 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
d7b90 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70  .  /* no-op if p
d7ba0 6f 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28  ossible */.  if(
d7bb0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d7bc0 3d 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20  ==locktype ){.  
d7bd0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d7be0 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f  OK;.  }..  /* To
d7bf0 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 73 68   downgrade to sh
d7c00 61 72 65 64 2c 20 73 69 6d 70 6c 79 20 75 70 64  ared, simply upd
d7c10 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c  ate our internal
d7c20 20 6e 6f 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20   notion of the. 
d7c30 20 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 2e 20   ** lock state. 
d7c40 20 4e 6f 20 6e 65 65 64 20 74 6f 20 6d 65 73 73   No need to mess
d7c50 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 6f   with the file o
d7c60 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69  n disk..  */.  i
d7c70 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
d7c80 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
d7c90 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d7ca0 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20  = SHARED_LOCK;. 
d7cb0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d7cc0 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  _OK;.  }.  .  /*
d7cd0 20 54 6f 20 66 75 6c 6c 79 20 75 6e 6c 6f 63 6b   To fully unlock
d7ce0 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 64   the database, d
d7cf0 65 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b 20 66  elete the lock f
d7d00 69 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ile */.  assert(
d7d10 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f   locktype==NO_LO
d7d20 43 4b 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69  CK );.  if( unli
d7d30 6e 6b 28 7a 4c 6f 63 6b 46 69 6c 65 29 20 29 7b  nk(zLockFile) ){
d7d40 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 30 3b  .    int rc = 0;
d7d50 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20  .    int tErrno 
d7d60 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28  = errno;.    if(
d7d70 20 45 4e 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e   ENOENT != tErrn
d7d80 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  o ){.      rc = 
d7d90 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d7da0 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d7db0 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  , SQLITE_IOERR_U
d7dc0 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20  NLOCK);.    }.  
d7dd0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d7de0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
d7df0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d7e00 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d7e10 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  }.    return rc;
d7e20 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c   .  }.  pFile->l
d7e30 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43  ocktype = NO_LOC
d7e40 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  K;.  return SQLI
d7e50 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
d7e60 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 20 20 4d  Close a file.  M
d7e70 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
d7e80 6b 20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61  k has been relea
d7e90 73 65 64 20 62 65 66 6f 72 65 20 63 6c 6f 73 69  sed before closi
d7ea0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
d7eb0 74 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73  t dotlockClose(s
d7ec0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
d7ed0 20 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69   {.  int rc;.  i
d7ee0 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69  f( id ){.    uni
d7ef0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
d7f00 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
d7f10 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28    dotlockUnlock(
d7f20 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20  id, NO_LOCK);.  
d7f30 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
d7f40 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
d7f50 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20  text);.  }.  rc 
d7f60 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28  = closeUnixFile(
d7f70 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  id);.  return rc
d7f80 3b 0a 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}./***********
d7f90 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74  ******* End of t
d7fa0 68 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b  he dot-file lock
d7fb0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
d7fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7fd0 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***.************
d7fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7ff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8020 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/../**********
d8030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d8070 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
d8080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
d8090 42 65 67 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 6b  Begin flock Lock
d80a0 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ing ************
d80b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d80c0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74  ****.**.** Use t
d80d0 68 65 20 66 6c 6f 63 6b 28 29 20 73 79 73 74 65  he flock() syste
d80e0 6d 20 63 61 6c 6c 20 74 6f 20 64 6f 20 66 69 6c  m call to do fil
d80f0 65 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a  e locking..**.**
d8100 20 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67   flock() locking
d8110 20 69 73 20 6c 69 6b 65 20 64 6f 74 2d 66 69 6c   is like dot-fil
d8120 65 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 61  e locking in tha
d8130 74 20 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a  t the various.**
d8140 20 66 69 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 6b   fine-grain lock
d8150 69 6e 67 20 6c 65 76 65 6c 73 20 73 75 70 70 6f  ing levels suppo
d8160 72 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 61  rted by SQLite a
d8170 72 65 20 63 6f 6c 6c 61 70 73 65 64 20 69 6e 74  re collapsed int
d8180 6f 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 65 78  o.** a single ex
d8190 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49  clusive lock.  I
d81a0 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 53  n other words, S
d81b0 48 41 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c  HARED, RESERVED,
d81c0 20 61 6e 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 20   and.** PENDING 
d81d0 6c 6f 63 6b 73 20 61 72 65 20 74 68 65 20 73 61  locks are the sa
d81e0 6d 65 20 74 68 69 6e 67 20 61 73 20 61 6e 20 45  me thing as an E
d81f0 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20  XCLUSIVE lock.  
d8200 53 51 4c 69 74 65 0a 2a 2a 20 73 74 69 6c 6c 20  SQLite.** still 
d8210 77 6f 72 6b 73 20 77 68 65 6e 20 79 6f 75 20 64  works when you d
d8220 6f 20 74 68 69 73 2c 20 62 75 74 20 63 6f 6e 63  o this, but conc
d8230 75 72 72 65 6e 63 79 20 69 73 20 72 65 64 75 63  urrency is reduc
d8240 65 64 20 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c 79  ed since.** only
d8250 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73   a single proces
d8260 73 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67  s can be reading
d8270 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74   the database at
d8280 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f   a time..**.** O
d8290 6d 69 74 20 74 68 69 73 20 73 65 63 74 69 6f 6e  mit this section
d82a0 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
d82b0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
d82c0 69 73 20 74 75 72 6e 65 64 20 6f 66 66 20 6f 72  is turned off or
d82d0 20 69 66 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67   if.** compiling
d82e0 20 66 6f 72 20 56 58 57 4f 52 4b 53 2e 0a 2a 2f   for VXWORKS..*/
d82f0 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
d8300 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
d8310 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a   && !OS_VXWORKS.
d8320 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
d8330 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68  ine checks if th
d8340 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45  ere is a RESERVE
d8350 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74  D lock held on t
d8360 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  he specified.** 
d8370 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20  file by this or 
d8380 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
d8390 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63  s. If such a loc
d83a0 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a  k is held, set *
d83b0 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20  pResOut.** to a 
d83c0 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f  non-zero value o
d83d0 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75  therwise *pResOu
d83e0 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  t is set to zero
d83f0 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
d8400 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  lue.** is set to
d8410 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73   SQLITE_OK unles
d8420 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f  s an I/O error o
d8430 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63  ccurs during loc
d8440 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73  k checking..*/.s
d8450 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43  tatic int flockC
d8460 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
d8470 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d8480 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29  d, int *pResOut)
d8490 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
d84a0 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65  ITE_OK;.  int re
d84b0 73 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e  served = 0;.  un
d84c0 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
d84d0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
d84e0 20 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72   .  SimulateIOEr
d84f0 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
d8500 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45  TE_IOERR_CHECKRE
d8510 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20  SERVEDLOCK; );. 
d8520 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c   .  assert( pFil
d8530 65 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65  e );.  .  /* Che
d8540 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69  ck if a thread i
d8550 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68  n this process h
d8560 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b  olds such a lock
d8570 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
d8580 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44  >locktype>SHARED
d8590 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73  _LOCK ){.    res
d85a0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  erved = 1;.  }. 
d85b0 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65   .  /* Otherwise
d85c0 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68   see if some oth
d85d0 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73  er process holds
d85e0 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72   it. */.  if( !r
d85f0 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 2f  eserved ){.    /
d8600 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 67 65 74  * attempt to get
d8610 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20   the lock */.   
d8620 20 69 6e 74 20 6c 72 63 20 3d 20 66 6c 6f 63 6b   int lrc = flock
d8630 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f  (pFile->h, LOCK_
d8640 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20  EX | LOCK_NB);. 
d8650 20 20 20 69 66 28 20 21 6c 72 63 20 29 7b 0a 20     if( !lrc ){. 
d8660 20 20 20 20 20 2f 2a 20 67 6f 74 20 74 68 65 20       /* got the 
d8670 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20  lock, unlock it 
d8680 2a 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 66  */.      lrc = f
d8690 6c 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c  lock(pFile->h, L
d86a0 4f 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20 69  OCK_UN);.      i
d86b0 66 20 28 20 6c 72 63 20 29 20 7b 0a 20 20 20 20  f ( lrc ) {.    
d86c0 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d      int tErrno =
d86d0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20   errno;.        
d86e0 2f 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 64  /* unlock failed
d86f0 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a   with an error *
d8700 2f 0a 20 20 20 20 20 20 20 20 6c 72 63 20 3d 20  /.        lrc = 
d8710 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d8720 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d8730 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  , SQLITE_IOERR_U
d8740 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20  NLOCK); .       
d8750 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
d8760 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20  OR(lrc) ){.     
d8770 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d8780 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
d8790 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c            rc = l
d87a0 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
d87b0 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65      }.    } else
d87c0 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72   {.      int tEr
d87d0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
d87e0 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b     reserved = 1;
d87f0 0a 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e  .      /* someon
d8800 65 20 65 6c 73 65 20 6d 69 67 68 74 20 68 61 76  e else might hav
d8810 65 20 69 74 20 72 65 73 65 72 76 65 64 20 2a 2f  e it reserved */
d8820 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 6c  .      lrc = sql
d8830 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d8840 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d8850 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b  QLITE_IOERR_LOCK
d8860 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 49 53  ); .      if( IS
d8870 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29  _LOCK_ERROR(lrc)
d8880 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
d8890 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d88a0 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72  Errno;.        r
d88b0 63 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 7d  c = lrc;.      }
d88c0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54  .    }.  }.  OST
d88d0 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c  RACE4("TEST WR-L
d88e0 4f 43 4b 20 25 64 20 25 64 20 25 64 20 28 66 6c  OCK %d %d %d (fl
d88f0 6f 63 6b 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  ock)\n", pFile->
d8900 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29  h, rc, reserved)
d8910 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
d8920 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f  _IGNORE_FLOCK_LO
d8930 43 4b 5f 45 52 52 4f 52 53 0a 20 20 69 66 28 20  CK_ERRORS.  if( 
d8940 28 72 63 20 26 20 53 51 4c 49 54 45 5f 49 4f 45  (rc & SQLITE_IOE
d8950 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f  RR) == SQLITE_IO
d8960 45 52 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ERR ){.    rc = 
d8970 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72  SQLITE_OK;.    r
d8980 65 73 65 72 76 65 64 3d 31 3b 0a 20 20 7d 0a 23  eserved=1;.  }.#
d8990 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
d89a0 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43  IGNORE_FLOCK_LOC
d89b0 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 2a 70  K_ERRORS */.  *p
d89c0 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65  ResOut = reserve
d89d0 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  d;.  return rc;.
d89e0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
d89f0 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20  e file with the 
d8a00 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62  lock specified b
d8a10 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b  y parameter lock
d8a20 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66  type - one.** of
d8a30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
d8a40 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48  **.**     (1) SH
d8a50 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  ARED_LOCK.**    
d8a60 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f   (2) RESERVED_LO
d8a70 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45  CK.**     (3) PE
d8a80 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  NDING_LOCK.**   
d8a90 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f    (4) EXCLUSIVE_
d8aa0 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74  LOCK.**.** Somet
d8ab0 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73  imes when reques
d8ac0 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74  ting one lock st
d8ad0 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20  ate, additional 
d8ae0 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61  lock states.** a
d8af0 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62  re inserted in b
d8b00 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63  etween.  The loc
d8b10 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20  king might fail 
d8b20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61  on one of the la
d8b30 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f  ter.** transitio
d8b40 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c  ns leaving the l
d8b50 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72  ock state differ
d8b60 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74  ent from what it
d8b70 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20   started but.** 
d8b80 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69  still short of i
d8b90 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f  ts goal.  The fo
d8ba0 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68  llowing chart sh
d8bb0 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a  ows the allowed.
d8bc0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61  ** transitions a
d8bd0 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20  nd the inserted 
d8be0 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61  intermediate sta
d8bf0 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e  tes:.**.**    UN
d8c00 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44  LOCKED -> SHARED
d8c10 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
d8c20 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20   RESERVED.**    
d8c30 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49  SHARED -> (PENDI
d8c40 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
d8c50 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20  .**    RESERVED 
d8c60 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
d8c70 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
d8c80 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55  PENDING -> EXCLU
d8c90 53 49 56 45 0a 2a 2a 0a 2a 2a 20 66 6c 6f 63 6b  SIVE.**.** flock
d8ca0 28 29 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73  () only really s
d8cb0 75 70 70 6f 72 74 20 45 58 43 4c 55 53 49 56 45  upport EXCLUSIVE
d8cc0 20 6c 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 63   locks.  We trac
d8cd0 6b 20 69 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a  k intermediate.*
d8ce0 2a 20 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 6e  * lock states in
d8cf0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c   the sqlite3_fil
d8d00 65 20 73 74 72 75 63 74 75 72 65 2c 20 62 75 74  e structure, but
d8d10 20 61 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45   all locks SHARE
d8d20 44 20 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72  D or.** above ar
d8d30 65 20 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 49  e really EXCLUSI
d8d40 56 45 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63  VE locks and exc
d8d50 6c 75 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 70  lude all other p
d8d60 72 6f 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a  rocesses from.**
d8d70 20 61 63 63 65 73 73 20 74 68 65 20 66 69 6c 65   access the file
d8d80 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
d8d90 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69  tine will only i
d8da0 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20  ncrease a lock. 
d8db0 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33   Use the sqlite3
d8dc0 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f  OsUnlock().** ro
d8dd0 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61  utine to lower a
d8de0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a   locking level..
d8df0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c  */.static int fl
d8e00 6f 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  ockLock(sqlite3_
d8e10 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
d8e20 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 20  cktype) {.  int 
d8e30 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d8e40 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d8e50 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d8e60 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46  d;..  assert( pF
d8e70 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 69 66 20  ile );..  /* if 
d8e80 77 65 20 61 6c 72 65 61 64 79 20 68 61 76 65 20  we already have 
d8e90 61 20 6c 6f 63 6b 2c 20 69 74 20 69 73 20 65 78  a lock, it is ex
d8ea0 63 6c 75 73 69 76 65 2e 20 20 0a 20 20 2a 2a 20  clusive.  .  ** 
d8eb0 4a 75 73 74 20 61 64 6a 75 73 74 20 6c 65 76 65  Just adjust leve
d8ec0 6c 20 61 6e 64 20 70 75 6e 74 20 6f 6e 20 6f 75  l and punt on ou
d8ed0 74 74 61 20 68 65 72 65 2e 20 2a 2f 0a 20 20 69  tta here. */.  i
d8ee0 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f (pFile->lockty
d8ef0 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 29 20 7b 0a  pe > NO_LOCK) {.
d8f00 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
d8f10 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a  ype = locktype;.
d8f20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d8f30 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  E_OK;.  }.  .  /
d8f40 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c 75 73  * grab an exclus
d8f50 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 0a 20  ive lock */.  . 
d8f60 20 69 66 20 28 66 6c 6f 63 6b 28 70 46 69 6c 65   if (flock(pFile
d8f70 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c  ->h, LOCK_EX | L
d8f80 4f 43 4b 5f 4e 42 29 29 20 7b 0a 20 20 20 20 69  OCK_NB)) {.    i
d8f90 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
d8fa0 6f 3b 0a 20 20 20 20 2f 2a 20 64 69 64 6e 27 74  o;.    /* didn't
d8fb0 20 67 65 74 2c 20 6d 75 73 74 20 62 65 20 62 75   get, must be bu
d8fc0 73 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73  sy */.    rc = s
d8fd0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d8fe0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d8ff0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
d9000 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f  CK);.    if( IS_
d9010 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29  LOCK_ERROR(rc) )
d9020 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
d9030 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
d9040 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73  o;.    }.  } els
d9050 65 20 7b 0a 20 20 20 20 2f 2a 20 67 6f 74 20 69  e {.    /* got i
d9060 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20  t, set the type 
d9070 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f  and return ok */
d9080 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
d9090 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
d90a0 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28  .  }.  OSTRACE4(
d90b0 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25  "LOCK    %d %s %
d90c0 73 20 28 66 6c 6f 63 6b 29 5c 6e 22 2c 20 70 46  s (flock)\n", pF
d90d0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
d90e0 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20  Name(locktype), 
d90f0 0a 20 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d  .           rc==
d9100 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22  SQLITE_OK ? "ok"
d9110 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 23 69   : "failed");.#i
d9120 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f  fdef SQLITE_IGNO
d9130 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52  RE_FLOCK_LOCK_ER
d9140 52 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20 26  RORS.  if( (rc &
d9150 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d   SQLITE_IOERR) =
d9160 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29  = SQLITE_IOERR )
d9170 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
d9180 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64  E_BUSY;.  }.#end
d9190 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e  if /* SQLITE_IGN
d91a0 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45  ORE_FLOCK_LOCK_E
d91b0 52 52 4f 52 53 20 2a 2f 0a 20 20 72 65 74 75 72  RRORS */.  retur
d91c0 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
d91d0 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  Lower the lockin
d91e0 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20  g level on file 
d91f0 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65  descriptor pFile
d9200 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c   to locktype.  l
d9210 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20  ocktype.** must 
d9220 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43  be either NO_LOC
d9230 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b  K or SHARED_LOCK
d9240 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
d9250 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20  ocking level of 
d9260 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
d9270 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61  tor is already a
d9280 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68  t or below.** th
d9290 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
d92a0 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20  ing level, this 
d92b0 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
d92c0 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  op..*/.static in
d92d0 74 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71  t flockUnlock(sq
d92e0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
d92f0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a  int locktype) {.
d9300 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d9310 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d9320 64 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20  d;.  .  assert( 
d9330 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41  pFile );.  OSTRA
d9340 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20  CE5("UNLOCK  %d 
d9350 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64  %d was %d pid=%d
d9360 20 28 66 6c 6f 63 6b 29 5c 6e 22 2c 20 70 46 69   (flock)\n", pFi
d9370 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
d9380 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 69 6c  .           pFil
d9390 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74  e->locktype, get
d93a0 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74  pid());.  assert
d93b0 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
d93c0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20  ED_LOCK );.  .  
d93d0 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73  /* no-op if poss
d93e0 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46  ible */.  if( pF
d93f0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c  ile->locktype==l
d9400 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72  ocktype ){.    r
d9410 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d9420 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61  .  }.  .  /* sha
d9430 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20  red can just be 
d9440 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20 61  set because we a
d9450 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 78  lways have an ex
d9460 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20  clusive */.  if 
d9470 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  (locktype==SHARE
d9480 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46  D_LOCK) {.    pF
d9490 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
d94a0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65  locktype;.    re
d94b0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d94c0 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20    }.  .  /* no, 
d94d0 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20  really, unlock. 
d94e0 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 6c  */.  int rc = fl
d94f0 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f  ock(pFile->h, LO
d9500 43 4b 5f 55 4e 29 3b 0a 20 20 69 66 20 28 72 63  CK_UN);.  if (rc
d9510 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 2c 20 74  ) {.    int r, t
d9520 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
d9530 20 20 20 72 20 3d 20 73 71 6c 69 74 65 45 72 72     r = sqliteErr
d9540 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d9550 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d9560 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20  IOERR_UNLOCK);. 
d9570 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d9580 52 52 4f 52 28 72 29 20 29 7b 0a 20 20 20 20 20  RROR(r) ){.     
d9590 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d95a0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
d95b0 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
d95c0 49 47 4e 4f 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43  IGNORE_FLOCK_LOC
d95d0 4b 5f 45 52 52 4f 52 53 0a 20 20 20 20 69 66 28  K_ERRORS.    if(
d95e0 20 28 72 20 26 20 53 51 4c 49 54 45 5f 49 4f 45   (r & SQLITE_IOE
d95f0 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f  RR) == SQLITE_IO
d9600 45 52 52 20 29 7b 0a 20 20 20 20 20 20 72 20 3d  ERR ){.      r =
d9610 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
d9620 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
d9630 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c 4f 43  LITE_IGNORE_FLOC
d9640 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f  K_LOCK_ERRORS */
d9650 0a 20 20 20 20 0a 20 20 20 20 72 65 74 75 72 6e  .    .    return
d9660 20 72 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20   r;.  } else {. 
d9670 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d9680 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20  pe = NO_LOCK;.  
d9690 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d96a0 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  OK;.  }.}../*.**
d96b0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a   Close a file..*
d96c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 6f  /.static int flo
d96d0 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  ckClose(sqlite3_
d96e0 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66  file *id) {.  if
d96f0 28 20 69 64 20 29 7b 0a 20 20 20 20 66 6c 6f 63  ( id ){.    floc
d9700 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c  kUnlock(id, NO_L
d9710 4f 43 4b 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  OCK);.  }.  retu
d9720 72 6e 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65  rn closeUnixFile
d9730 28 69 64 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  (id);.}..#endif 
d9740 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  /* SQLITE_ENABLE
d9750 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26  _LOCKING_STYLE &
d9760 26 20 21 4f 53 5f 56 58 57 4f 52 4b 20 2a 2f 0a  & !OS_VXWORK */.
d9770 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
d9780 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65  ***** End of the
d9790 20 66 6c 6f 63 6b 20 6c 6f 63 6b 20 69 6d 70 6c   flock lock impl
d97a0 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a  ementation *****
d97b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d97c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
d97d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d97e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d97f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
d9810 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
d9820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9860 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
d9870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
d9880 20 4e 61 6d 65 64 20 53 65 6d 61 70 68 6f 72 65   Named Semaphore
d9890 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a   Locking *******
d98a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d98b0 2a 0a 2a 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65  *.**.** Named se
d98c0 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20  maphore locking 
d98d0 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65  is only supporte
d98e0 64 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a  d on VxWorks..**
d98f0 0a 2a 2a 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f  .** Semaphore lo
d9900 63 6b 69 6e 67 20 69 73 20 6c 69 6b 65 20 64 6f  cking is like do
d9910 74 2d 6c 6f 63 6b 20 61 6e 64 20 66 6c 6f 63 6b  t-lock and flock
d9920 20 69 6e 20 74 68 61 74 20 69 74 20 72 65 61 6c   in that it real
d9930 6c 79 20 6f 6e 6c 79 0a 2a 2a 20 73 75 70 70 6f  ly only.** suppo
d9940 72 74 73 20 45 58 43 4c 55 53 49 56 45 20 6c 6f  rts EXCLUSIVE lo
d9950 63 6b 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 20 73  cking.  Only a s
d9960 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 63 61  ingle process ca
d9970 6e 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 0a  n read or write.
d9980 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
d9990 66 69 6c 65 20 61 74 20 61 20 74 69 6d 65 2e 20  file at a time. 
d99a0 20 54 68 69 73 20 72 65 64 75 63 65 73 20 70 6f   This reduces po
d99b0 74 65 6e 74 69 61 6c 20 63 6f 6e 63 75 72 72 65  tential concurre
d99c0 6e 63 79 2c 20 62 75 74 0a 2a 2a 20 6d 61 6b 65  ncy, but.** make
d99d0 73 20 74 68 65 20 6c 6f 63 6b 20 69 6d 70 6c 65  s the lock imple
d99e0 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 63 68 20 65  mentation much e
d99f0 61 73 69 65 72 2e 0a 2a 2f 0a 23 69 66 20 4f 53  asier..*/.#if OS
d9a00 5f 56 58 57 4f 52 4b 53 0a 0a 2f 2a 0a 2a 2a 20  _VXWORKS../*.** 
d9a10 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
d9a20 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20  cks if there is 
d9a30 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
d9a40 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63  held on the spec
d9a50 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79  ified.** file by
d9a60 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68   this or any oth
d9a70 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73  er process. If s
d9a80 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65  uch a lock is he
d9a90 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f 75 74  ld, set *pResOut
d9aa0 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72  .** to a non-zer
d9ab0 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77 69 73  o value otherwis
d9ac0 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65  e *pResOut is se
d9ad0 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20  t to zero.  The 
d9ae0 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20  return value.** 
d9af0 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  is set to SQLITE
d9b00 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f  _OK unless an I/
d9b10 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64  O error occurs d
d9b20 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b  uring lock check
d9b30 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
d9b40 6e 74 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72  nt semCheckReser
d9b50 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
d9b60 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
d9b70 52 65 73 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20  ResOut) {.  int 
d9b80 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d9b90 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20 3d    int reserved =
d9ba0 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a   0;.  unixFile *
d9bb0 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c  pFile = (unixFil
d9bc0 65 2a 29 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61  e*)id;..  Simula
d9bd0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
d9be0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  n SQLITE_IOERR_C
d9bf0 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b  HECKRESERVEDLOCK
d9c00 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  ; );.  .  assert
d9c10 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a  ( pFile );..  /*
d9c20 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65   Check if a thre
d9c30 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65  ad in this proce
d9c40 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20  ss holds such a 
d9c50 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46  lock */.  if( pF
d9c60 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48  ile->locktype>SH
d9c70 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
d9c80 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20   reserved = 1;. 
d9c90 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72   }.  .  /* Other
d9ca0 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65  wise see if some
d9cb0 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68   other process h
d9cc0 6f 6c 64 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66  olds it. */.  if
d9cd0 28 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20  ( !reserved ){. 
d9ce0 20 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d     sem_t *pSem =
d9cf0 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70   pFile->pOpen->p
d9d00 53 65 6d 3b 0a 20 20 20 20 73 74 72 75 63 74 20  Sem;.    struct 
d9d10 73 74 61 74 20 73 74 61 74 42 75 66 3b 0a 0a 20  stat statBuf;.. 
d9d20 20 20 20 69 66 28 20 73 65 6d 5f 74 72 79 77 61     if( sem_trywa
d9d30 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a  it(pSem)==-1 ){.
d9d40 20 20 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f        int tErrno
d9d50 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d9d60 69 66 28 20 45 41 47 41 49 4e 20 21 3d 20 74 45  if( EAGAIN != tE
d9d70 72 72 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  rrno ){.        
d9d80 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
d9d90 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
d9da0 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
d9db0 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45  ERR_CHECKRESERVE
d9dc0 44 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20  DLOCK);.        
d9dd0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d9de0 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20   = tErrno;.     
d9df0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
d9e00 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73    /* someone els
d9e10 65 20 68 61 73 20 74 68 65 20 6c 6f 63 6b 20 77  e has the lock w
d9e20 68 65 6e 20 77 65 20 61 72 65 20 69 6e 20 4e 4f  hen we are in NO
d9e30 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 20 20 20  _LOCK */.       
d9e40 20 72 65 73 65 72 76 65 64 20 3d 20 28 70 46 69   reserved = (pFi
d9e50 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 53  le->locktype < S
d9e60 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20  HARED_LOCK);.   
d9e70 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
d9e80 20 20 20 20 20 20 2f 2a 20 77 65 20 63 6f 75 6c        /* we coul
d9e90 64 20 68 61 76 65 20 69 74 20 69 66 20 77 65 20  d have it if we 
d9ea0 77 61 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20 20  want it */.     
d9eb0 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3b   sem_post(pSem);
d9ec0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54  .    }.  }.  OST
d9ed0 52 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c  RACE4("TEST WR-L
d9ee0 4f 43 4b 20 25 64 20 25 64 20 25 64 20 28 73 65  OCK %d %d %d (se
d9ef0 6d 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  m)\n", pFile->h,
d9f00 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a   rc, reserved);.
d9f10 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 65  .  *pResOut = re
d9f20 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72 6e  served;.  return
d9f30 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f   rc;.}../*.** Lo
d9f40 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74 68  ck the file with
d9f50 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69 66   the lock specif
d9f60 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  ied by parameter
d9f70 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a   locktype - one.
d9f80 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** of the follow
d9f90 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28  ing:.**.**     (
d9fa0 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a  1) SHARED_LOCK.*
d9fb0 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52 56  *     (2) RESERV
d9fc0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
d9fd0 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a  3) PENDING_LOCK.
d9fe0 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c 55  **     (4) EXCLU
d9ff0 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20  SIVE_LOCK.**.** 
da000 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20 72  Sometimes when r
da010 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f  equesting one lo
da020 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74 69  ck state, additi
da030 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73  onal lock states
da040 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65 64  .** are inserted
da050 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54 68   in between.  Th
da060 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20  e locking might 
da070 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74  fail on one of t
da080 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e  he later.** tran
da090 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20  sitions leaving 
da0a0 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20 64  the lock state d
da0b0 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77 68  ifferent from wh
da0c0 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62 75  at it started bu
da0d0 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74  t.** still short
da0e0 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20 54   of its goal.  T
da0f0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61  he following cha
da100 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c 6c  rt shows the all
da110 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69  owed.** transiti
da120 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73 65  ons and the inse
da130 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61 74  rted intermediat
da140 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20  e states:.**.** 
da150 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53     UNLOCKED -> S
da160 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  HARED.**    SHAR
da170 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a  ED -> RESERVED.*
da180 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 28  *    SHARED -> (
da190 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
da1a0 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53 45  USIVE.**    RESE
da1b0 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  RVED -> (PENDING
da1c0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
da1d0 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20  *    PENDING -> 
da1e0 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20  EXCLUSIVE.**.** 
da1f0 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73 20  Semaphore locks 
da200 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70 70  only really supp
da210 6f 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c 6f  ort EXCLUSIVE lo
da220 63 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20 69  cks.  We track i
da230 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20 6c  ntermediate.** l
da240 6f 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74 68  ock states in th
da250 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73  e sqlite3_file s
da260 74 72 75 63 74 75 72 65 2c 20 62 75 74 20 61 6c  tructure, but al
da270 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20 6f  l locks SHARED o
da280 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 72  r.** above are r
da290 65 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45 20  eally EXCLUSIVE 
da2a0 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75 64  locks and exclud
da2b0 65 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f 63  e all other proc
da2c0 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 63  esses from.** ac
da2d0 63 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a  cess the file..*
da2e0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
da2f0 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72  e will only incr
da300 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73  ease a lock.  Us
da310 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55  e the sqlite3OsU
da320 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69  nlock().** routi
da330 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f  ne to lower a lo
da340 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a  cking level..*/.
da350 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 4c 6f  static int semLo
da360 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
da370 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  *id, int locktyp
da380 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  e) {.  unixFile 
da390 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
da3a0 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66 64  le*)id;.  int fd
da3b0 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20  ;.  sem_t *pSem 
da3c0 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e  = pFile->pOpen->
da3d0 70 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20 3d  pSem;.  int rc =
da3e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f   SQLITE_OK;..  /
da3f0 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20  * if we already 
da400 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20  have a lock, it 
da410 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a  is exclusive.  .
da420 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74    ** Just adjust
da430 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20   level and punt 
da440 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a  on outta here. *
da450 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c  /.  if (pFile->l
da460 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43  ocktype > NO_LOC
da470 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K) {.    pFile->
da480 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
da490 79 70 65 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  ype;.    rc = SQ
da4a0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f 74  LITE_OK;.    got
da4b0 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a  o sem_end_lock;.
da4c0 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63 6b    }.  .  /* lock
da4d0 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20 62   semaphore now b
da4e0 75 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65 6e  ut bail out when
da4f0 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64 2e   already locked.
da500 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74 72   */.  if( sem_tr
da510 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31 20  ywait(pSem)==-1 
da520 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
da530 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74  TE_BUSY;.    got
da540 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a  o sem_end_lock;.
da550 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 74    }..  /* got it
da560 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61  , set the type a
da570 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a  nd return ok */.
da580 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
da590 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20  e = locktype;.. 
da5a0 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20  sem_end_lock:.  
da5b0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
da5c0 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
da5d0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
da5e0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 70  ile descriptor p
da5f0 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65  File to locktype
da600 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d  .  locktype.** m
da610 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f  ust be either NO
da620 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f  _LOCK or SHARED_
da630 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  LOCK..**.** If t
da640 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
da650 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65 73   of the file des
da660 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61  criptor is alrea
da670 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a  dy at or below.*
da680 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  * the requested 
da690 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74  locking level, t
da6a0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
da6b0 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
da6c0 63 20 69 6e 74 20 73 65 6d 55 6e 6c 6f 63 6b 28  c int semUnlock(
da6d0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
da6e0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20  , int locktype) 
da6f0 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
da700 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
da710 29 69 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53  )id;.  sem_t *pS
da720 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65  em = pFile->pOpe
da730 6e 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 73 73 65  n->pSem;..  asse
da740 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 61  rt( pFile );.  a
da750 73 73 65 72 74 28 20 70 53 65 6d 20 29 3b 0a 20  ssert( pSem );. 
da760 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f 43   OSTRACE5("UNLOC
da770 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64 20  K  %d %d was %d 
da780 70 69 64 3d 25 64 20 28 73 65 6d 29 5c 6e 22 2c  pid=%d (sem)\n",
da790 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
da7a0 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d 3e  ype,..   pFile->
da7b0 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69 64  locktype, getpid
da7c0 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  ());.  assert( l
da7d0 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44 5f  ocktype<=SHARED_
da7e0 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20  LOCK );.  .  /* 
da7f0 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62 6c  no-op if possibl
da800 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  e */.  if( pFile
da810 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63 6b  ->locktype==lock
da820 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75  type ){.    retu
da830 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
da840 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65 64  }.  .  /* shared
da850 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65 74   can just be set
da860 20 62 65 63 61 75 73 65 20 77 65 20 61 6c 77 61   because we alwa
da870 79 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c 75  ys have an exclu
da880 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c 6f  sive */.  if (lo
da890 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
da8a0 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65  OCK) {.    pFile
da8b0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
da8c0 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72  ktype;.    retur
da8d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
da8e0 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65 61  .  .  /* no, rea
da8f0 6c 6c 79 20 75 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20  lly unlock. */. 
da900 20 69 66 20 28 20 73 65 6d 5f 70 6f 73 74 28 70   if ( sem_post(p
da910 53 65 6d 29 3d 3d 2d 31 20 29 20 7b 0a 20 20 20  Sem)==-1 ) {.   
da920 20 69 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f 20   int rc, tErrno 
da930 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20  = errno;.    rc 
da940 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
da950 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
da960 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
da970 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66  _UNLOCK);.    if
da980 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
da990 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70 46 69  rc) ){.      pFi
da9a0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
da9b0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20  tErrno;.    }.  
da9c0 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 20 20    return rc; .  
da9d0 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  }.  pFile->lockt
da9e0 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20  ype = NO_LOCK;. 
da9f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
daa00 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f  K;.}../*. ** Clo
daa10 73 65 20 61 20 66 69 6c 65 2e 0a 20 2a 2f 0a 73  se a file.. */.s
daa20 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43 6c 6f  tatic int semClo
daa30 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  se(sqlite3_file 
daa40 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20  *id) {.  if( id 
daa50 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  ){.    unixFile 
daa60 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
daa70 6c 65 2a 29 69 64 3b 0a 20 20 20 20 73 65 6d 55  le*)id;.    semU
daa80 6e 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43  nlock(id, NO_LOC
daa90 4b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  K);.    assert( 
daaa0 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 75 6e 69  pFile );.    uni
daab0 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  xEnterMutex();. 
daac0 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e     releaseLockIn
daad0 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29  fo(pFile->pLock)
daae0 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65  ;.    releaseOpe
daaf0 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65  nCnt(pFile->pOpe
dab00 6e 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76  n);.    unixLeav
dab10 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 63 6c  eMutex();.    cl
dab20 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b  oseUnixFile(id);
dab30 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
dab40 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64  LITE_OK;.}..#end
dab50 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52 4b 53  if /* OS_VXWORKS
dab60 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 64 20   */./*.** Named 
dab70 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e  semaphore lockin
dab80 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  g is only availa
dab90 62 6c 65 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a  ble on VxWorks..
daba0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
dabb0 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 6e 61  ** End of the na
dabc0 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f  med semaphore lo
dabd0 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ck implementatio
dabe0 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n **************
dabf0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
dac00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dac10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dac20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dac40 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */.../**********
dac50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dac60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dac70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dac80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dac90 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****.***********
daca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dacb0 20 42 65 67 69 6e 20 41 46 50 20 4c 6f 63 6b 69   Begin AFP Locki
dacc0 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ng *************
dacd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dace0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 46 50 20 69  ****.**.** AFP i
dacf0 73 20 74 68 65 20 41 70 70 6c 65 20 46 69 6c 69  s the Apple Fili
dad00 6e 67 20 50 72 6f 74 6f 63 6f 6c 2e 20 20 41 46  ng Protocol.  AF
dad10 50 20 69 73 20 61 20 6e 65 74 77 6f 72 6b 20 66  P is a network f
dad20 69 6c 65 73 79 73 74 65 6d 20 66 6f 75 6e 64 0a  ilesystem found.
dad30 2a 2a 20 6f 6e 20 41 70 70 6c 65 20 4d 61 63 69  ** on Apple Maci
dad40 6e 74 6f 73 68 20 63 6f 6d 70 75 74 65 72 73 20  ntosh computers 
dad50 2d 20 62 6f 74 68 20 4f 53 39 20 61 6e 64 20 4f  - both OS9 and O
dad60 53 58 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 72 64 2d  SX..**.** Third-
dad70 70 61 72 74 79 20 69 6d 70 6c 65 6d 65 6e 74 61  party implementa
dad80 74 69 6f 6e 73 20 6f 66 20 41 46 50 20 61 72 65  tions of AFP are
dad90 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 42 75 74   available.  But
dada0 20 74 68 69 73 20 63 6f 64 65 20 68 65 72 65 0a   this code here.
dadb0 2a 2a 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e  ** only works on
dadc0 20 4f 53 58 2e 0a 2a 2f 0a 0a 23 69 66 20 64 65   OSX..*/..#if de
dadd0 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
dade0 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
dadf0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
dae00 2f 2a 0a 2a 2a 20 54 68 65 20 61 66 70 4c 6f 63  /*.** The afpLoc
dae10 6b 69 6e 67 43 6f 6e 74 65 78 74 20 73 74 72 75  kingContext stru
dae20 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
dae30 6c 6c 20 61 66 70 20 6c 6f 63 6b 20 73 70 65 63  ll afp lock spec
dae40 69 66 69 63 20 73 74 61 74 65 0a 2a 2f 0a 74 79  ific state.*/.ty
dae50 70 65 64 65 66 20 73 74 72 75 63 74 20 61 66 70  pedef struct afp
dae60 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 61  LockingContext a
dae70 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
dae80 3b 0a 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b  ;.struct afpLock
dae90 69 6e 67 43 6f 6e 74 65 78 74 20 7b 0a 20 20 75  ingContext {.  u
daea0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
daeb0 67 20 73 68 61 72 65 64 42 79 74 65 3b 0a 20 20  g sharedByte;.  
daec0 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61  const char *dbPa
daed0 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  th;             
daee0 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f  /* Name of the o
daef0 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a  pen file */.};..
daf00 73 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65  struct ByteRange
daf10 4c 6f 63 6b 50 42 32 0a 7b 0a 20 20 75 6e 73 69  LockPB2.{.  unsi
daf20 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f  gned long long o
daf30 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a  ffset;        /*
daf40 20 6f 66 66 73 65 74 20 74 6f 20 66 69 72 73 74   offset to first
daf50 20 62 79 74 65 20 74 6f 20 6c 6f 63 6b 20 2a 2f   byte to lock */
daf60 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67  .  unsigned long
daf70 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 3b 20 20 20   long length;   
daf80 20 20 20 20 20 2f 2a 20 6e 62 72 20 6f 66 20 62       /* nbr of b
daf90 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a  ytes to lock */.
dafa0 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
dafb0 6c 6f 6e 67 20 72 65 74 52 61 6e 67 65 53 74 61  long retRangeSta
dafc0 72 74 3b 20 2f 2a 20 6e 62 72 20 6f 66 20 31 73  rt; /* nbr of 1s
dafd0 74 20 62 79 74 65 20 6c 6f 63 6b 65 64 20 69 66  t byte locked if
dafe0 20 73 75 63 63 65 73 73 66 75 6c 20 2a 2f 0a 20   successful */. 
daff0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 75   unsigned char u
db000 6e 4c 6f 63 6b 46 6c 61 67 3b 20 20 20 20 20 20  nLockFlag;      
db010 20 20 20 2f 2a 20 31 20 3d 20 75 6e 6c 6f 63 6b     /* 1 = unlock
db020 2c 20 30 20 3d 20 6c 6f 63 6b 20 2a 2f 0a 20 20  , 0 = lock */.  
db030 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 74  unsigned char st
db040 61 72 74 45 6e 64 46 6c 61 67 3b 20 20 20 20 20  artEndFlag;     
db050 20 20 2f 2a 20 31 3d 72 65 6c 20 74 6f 20 65 6e    /* 1=rel to en
db060 64 20 6f 66 20 66 6f 72 6b 2c 20 30 3d 72 65 6c  d of fork, 0=rel
db070 20 74 6f 20 73 74 61 72 74 20 2a 2f 0a 20 20 69   to start */.  i
db080 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20 20  nt fd;          
db090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
db0a0 20 2f 2a 20 66 69 6c 65 20 64 65 73 63 20 74 6f   /* file desc to
db0b0 20 61 73 73 6f 63 20 74 68 69 73 20 6c 6f 63 6b   assoc this lock
db0c0 20 77 69 74 68 20 2a 2f 0a 7d 3b 0a 0a 23 64 65   with */.};..#de
db0d0 66 69 6e 65 20 61 66 70 66 73 42 79 74 65 52 61  fine afpfsByteRa
db0e0 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 20 20 20  ngeLock2FSCTL   
db0f0 20 20 20 20 20 5f 49 4f 57 52 28 27 7a 27 2c 20       _IOWR('z', 
db100 32 33 2c 20 73 74 72 75 63 74 20 42 79 74 65 52  23, struct ByteR
db110 61 6e 67 65 4c 6f 63 6b 50 42 32 29 0a 0a 2f 2a  angeLockPB2)../*
db120 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 75 74  .** This is a ut
db130 69 6c 69 74 79 20 66 6f 72 20 73 65 74 74 69 6e  ility for settin
db140 67 20 6f 72 20 63 6c 65 61 72 69 6e 67 20 61 20  g or clearing a 
db150 62 69 74 2d 72 61 6e 67 65 20 6c 6f 63 6b 20 6f  bit-range lock o
db160 6e 20 61 6e 0a 2a 2a 20 41 46 50 20 66 69 6c 65  n an.** AFP file
db170 73 79 73 74 65 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52  system..** .** R
db180 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
db190 6f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49  on success, SQLI
db1a0 54 45 5f 42 55 53 59 20 6f 6e 20 66 61 69 6c 75  TE_BUSY on failu
db1b0 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  re..*/.static in
db1c0 74 20 61 66 70 53 65 74 4c 6f 63 6b 28 0a 20 20  t afpSetLock(.  
db1d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68  const char *path
db1e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
db1f0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69  * Name of the fi
db200 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20  le to be locked 
db210 6f 72 20 75 6e 6c 6f 63 6b 65 64 20 2a 2f 0a 20  or unlocked */. 
db220 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
db230 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
db240 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73  /* Open file des
db250 63 72 69 70 74 6f 72 20 6f 6e 20 70 61 74 68 20  criptor on path 
db260 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f  */.  unsigned lo
db270 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74 2c 20  ng long offset, 
db280 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
db290 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a  e to be locked *
db2a0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  /.  unsigned lon
db2b0 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c 20 20  g long length,  
db2c0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
db2d0 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f  bytes to lock */
db2e0 0a 20 20 69 6e 74 20 73 65 74 4c 6f 63 6b 46 6c  .  int setLockFl
db2f0 61 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ag              
db300 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74    /* True to set
db310 20 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 74 6f   lock.  False to
db320 20 63 6c 65 61 72 20 6c 6f 63 6b 20 2a 2f 0a 29   clear lock */.)
db330 7b 0a 20 20 73 74 72 75 63 74 20 42 79 74 65 52  {.  struct ByteR
db340 61 6e 67 65 4c 6f 63 6b 50 42 32 20 70 62 3b 0a  angeLockPB2 pb;.
db350 20 20 69 6e 74 20 65 72 72 3b 0a 20 20 0a 20 20    int err;.  .  
db360 70 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20  pb.unLockFlag = 
db370 73 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20  setLockFlag ? 0 
db380 3a 20 31 3b 0a 20 20 70 62 2e 73 74 61 72 74 45  : 1;.  pb.startE
db390 6e 64 46 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62  ndFlag = 0;.  pb
db3a0 2e 6f 66 66 73 65 74 20 3d 20 6f 66 66 73 65 74  .offset = offset
db3b0 3b 0a 20 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20  ;.  pb.length = 
db3c0 6c 65 6e 67 74 68 3b 20 0a 20 20 70 62 2e 66 64  length; .  pb.fd
db3d0 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 0a   = pFile->h;.  .
db3e0 20 20 4f 53 54 52 41 43 45 36 28 22 41 46 50 53    OSTRACE6("AFPS
db3f0 45 54 4c 4f 43 4b 20 5b 25 73 5d 20 66 6f 72 20  ETLOCK [%s] for 
db400 25 64 25 73 20 69 6e 20 72 61 6e 67 65 20 25 6c  %d%s in range %l
db410 6c 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20 20 20  lx:%llx\n", .   
db420 20 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f 22 4f   (setLockFlag?"O
db430 4e 22 3a 22 4f 46 46 22 29 2c 20 70 46 69 6c 65  N":"OFF"), pFile
db440 2d 3e 68 2c 20 28 70 62 2e 66 64 3d 3d 2d 31 3f  ->h, (pb.fd==-1?
db450 22 5b 74 65 73 74 76 61 6c 2d 31 5d 22 3a 22 22  "[testval-1]":""
db460 29 2c 0a 20 20 20 20 6f 66 66 73 65 74 2c 20 6c  ),.    offset, l
db470 65 6e 67 74 68 29 3b 0a 20 20 65 72 72 20 3d 20  ength);.  err = 
db480 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66  fsctl(path, afpf
db490 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46  sByteRangeLock2F
db4a0 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20  SCTL, &pb, 0);. 
db4b0 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20   if ( err==-1 ) 
db4c0 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
db4d0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
db4e0 72 72 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43  rrno;.    OSTRAC
db4f0 45 34 28 22 41 46 50 53 45 54 4c 4f 43 4b 20 66  E4("AFPSETLOCK f
db500 61 69 6c 65 64 20 74 6f 20 66 73 63 74 6c 28 29  ailed to fsctl()
db510 20 27 25 73 27 20 25 64 20 25 73 5c 6e 22 2c 0a   '%s' %d %s\n",.
db520 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 74               pat
db530 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72 65 72  h, tErrno, strer
db540 72 6f 72 28 74 45 72 72 6e 6f 29 29 3b 0a 23 69  ror(tErrno));.#i
db550 66 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f  fdef SQLITE_IGNO
db560 52 45 5f 41 46 50 5f 4c 4f 43 4b 5f 45 52 52 4f  RE_AFP_LOCK_ERRO
db570 52 53 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  RS.    rc = SQLI
db580 54 45 5f 42 55 53 59 3b 0a 23 65 6c 73 65 0a 20  TE_BUSY;.#else. 
db590 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72     rc = sqliteEr
db5a0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
db5b0 72 28 74 45 72 72 6e 6f 2c 0a 20 20 20 20 20 20  r(tErrno,.      
db5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 65                se
db5d0 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 53 51 4c 49  tLockFlag ? SQLI
db5e0 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 3a 20  TE_IOERR_LOCK : 
db5f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
db600 4f 43 4b 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  OCK);.#endif /* 
db610 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46  SQLITE_IGNORE_AF
db620 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20 2a 2f  P_LOCK_ERRORS */
db630 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b  .    if( IS_LOCK
db640 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
db650 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
db660 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
db670 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
db680 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20  rc;.  } else {. 
db690 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
db6a0 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  _OK;.  }.}../*.*
db6b0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
db6c0 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20 69  hecks if there i
db6d0 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  s a RESERVED loc
db6e0 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70  k held on the sp
db6f0 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20  ecified.** file 
db700 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f  by this or any o
db710 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66  ther process. If
db720 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20   such a lock is 
db730 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73 4f  held, set *pResO
db740 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d 7a  ut.** to a non-z
db750 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72 77  ero value otherw
db760 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73 20  ise *pResOut is 
db770 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68  set to zero.  Th
db780 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
db790 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49  * is set to SQLI
db7a0 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e 20  TE_OK unless an 
db7b0 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73  I/O error occurs
db7c0 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68 65   during lock che
db7d0 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  cking..*/.static
db7e0 20 69 6e 74 20 61 66 70 43 68 65 63 6b 52 65 73   int afpCheckRes
db7f0 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
db800 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
db810 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74  *pResOut){.  int
db820 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
db830 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64 20  .  int reserved 
db840 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  = 0;.  unixFile 
db850 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
db860 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69 6d  le*)id;.  .  Sim
db870 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65  ulateIOError( re
db880 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
db890 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c  R_CHECKRESERVEDL
db8a0 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73 73  OCK; );.  .  ass
db8b0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
db8c0 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
db8d0 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 66  t *context = (af
db8e0 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  pLockingContext 
db8f0 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e  *) pFile->lockin
db900 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 2f  gContext;.  .  /
db910 2a 20 43 68 65 63 6b 20 69 66 20 61 20 74 68 72  * Check if a thr
db920 65 61 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63  ead in this proc
db930 65 73 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61  ess holds such a
db940 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70   lock */.  if( p
db950 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53  File->locktype>S
db960 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
db970 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
db980 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4f 74 68 65    }.  .  /* Othe
db990 72 77 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d  rwise see if som
db9a0 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20  e other process 
db9b0 68 6f 6c 64 73 20 69 74 2e 0a 20 20 20 2a 2f 0a  holds it..   */.
db9c0 20 20 69 66 28 20 21 72 65 73 65 72 76 65 64 20    if( !reserved 
db9d0 29 7b 0a 20 20 20 20 2f 2a 20 6c 6f 63 6b 20 74  ){.    /* lock t
db9e0 68 65 20 52 45 53 45 52 56 45 44 20 62 79 74 65  he RESERVED byte
db9f0 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 72 63 20   */.    int lrc 
dba00 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e  = afpSetLock(con
dba10 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46  text->dbPath, pF
dba20 69 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59  ile, RESERVED_BY
dba30 54 45 2c 20 31 2c 31 29 3b 20 20 0a 20 20 20 20  TE, 1,1);  .    
dba40 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 6c  if( SQLITE_OK==l
dba50 72 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69  rc ){.      /* i
dba60 66 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69  f we succeeded i
dba70 6e 20 74 61 6b 69 6e 67 20 74 68 65 20 72 65 73  n taking the res
dba80 65 72 76 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c 6f  erved lock, unlo
dba90 63 6b 20 69 74 20 74 6f 20 72 65 73 74 6f 72 65  ck it to restore
dbaa0 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6f 72  .      ** the or
dbab0 69 67 69 6e 61 6c 20 73 74 61 74 65 20 2a 2f 0a  iginal state */.
dbac0 20 20 20 20 20 20 6c 72 63 20 3d 20 61 66 70 53        lrc = afpS
dbad0 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
dbae0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 52  dbPath, pFile, R
dbaf0 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31 2c  ESERVED_BYTE, 1,
dbb00 20 30 29 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20   0);.    } else 
dbb10 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65  {.      /* if we
dbb20 20 66 61 69 6c 65 64 20 74 6f 20 67 65 74 20 74   failed to get t
dbb30 68 65 20 6c 6f 63 6b 20 74 68 65 6e 20 73 6f 6d  he lock then som
dbb40 65 6f 6e 65 20 65 6c 73 65 20 6d 75 73 74 20 68  eone else must h
dbb50 61 76 65 20 69 74 20 2a 2f 0a 20 20 20 20 20 20  ave it */.      
dbb60 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20  reserved = 1;.  
dbb70 20 20 7d 0a 20 20 20 20 69 66 28 20 49 53 5f 4c    }.    if( IS_L
dbb80 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20 29  OCK_ERROR(lrc) )
dbb90 7b 0a 20 20 20 20 20 20 72 63 3d 6c 72 63 3b 0a  {.      rc=lrc;.
dbba0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 4f      }.  }.  .  O
dbbb0 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52  STRACE4("TEST WR
dbbc0 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 20 28  -LOCK %d %d %d (
dbbd0 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  afp)\n", pFile->
dbbe0 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29  h, rc, reserved)
dbbf0 3b 0a 20 20 0a 20 20 2a 70 52 65 73 4f 75 74 20  ;.  .  *pResOut 
dbc00 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65  = reserved;.  re
dbc10 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
dbc20 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20  * Lock the file 
dbc30 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70  with the lock sp
dbc40 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ecified by param
dbc50 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20  eter locktype - 
dbc60 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f  one.** of the fo
dbc70 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
dbc80 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f     (1) SHARED_LO
dbc90 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45  CK.**     (2) RE
dbca0 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20  SERVED_LOCK.**  
dbcb0 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c     (3) PENDING_L
dbcc0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45  OCK.**     (4) E
dbcd0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a  XCLUSIVE_LOCK.**
dbce0 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68  .** Sometimes wh
dbcf0 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e  en requesting on
dbd00 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64  e lock state, ad
dbd10 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74  ditional lock st
dbd20 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65  ates.** are inse
dbd30 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e  rted in between.
dbd40 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69    The locking mi
dbd50 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20  ght fail on one 
dbd60 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20  of the later.** 
dbd70 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76  transitions leav
dbd80 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61  ing the lock sta
dbd90 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  te different fro
dbda0 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74 65  m what it starte
dbdb0 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73  d but.** still s
dbdc0 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c  hort of its goal
dbdd0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
dbde0 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68 65   chart shows the
dbdf0 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e   allowed.** tran
dbe00 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20  sitions and the 
dbe10 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65  inserted interme
dbe20 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a  diate states:.**
dbe30 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20  .**    UNLOCKED 
dbe40 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20  -> SHARED.**    
dbe50 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56  SHARED -> RESERV
dbe60 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  ED.**    SHARED 
dbe70 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20  -> (PENDING) -> 
dbe80 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20  EXCLUSIVE.**    
dbe90 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e  RESERVED -> (PEN
dbea0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
dbeb0 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47  VE.**    PENDING
dbec0 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a   -> EXCLUSIVE.**
dbed0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
dbee0 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65   will only incre
dbef0 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65  ase a lock.  Use
dbf00 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e   the sqlite3OsUn
dbf10 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e  lock().** routin
dbf20 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63  e to lower a loc
dbf30 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73  king level..*/.s
dbf40 74 61 74 69 63 20 69 6e 74 20 61 66 70 4c 6f 63  tatic int afpLoc
dbf50 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  k(sqlite3_file *
dbf60 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
dbf70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
dbf80 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46  LITE_OK;.  unixF
dbf90 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
dbfa0 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66  ixFile*)id;.  af
dbfb0 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  pLockingContext 
dbfc0 2a 63 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c  *context = (afpL
dbfd0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29  ockingContext *)
dbfe0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43   pFile->lockingC
dbff0 6f 6e 74 65 78 74 3b 0a 20 20 0a 20 20 61 73 73  ontext;.  .  ass
dc000 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
dc010 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20  OSTRACE5("LOCK  
dc020 20 20 25 64 20 25 73 20 77 61 73 20 25 73 20 70    %d %s was %s p
dc030 69 64 3d 25 64 20 28 61 66 70 29 5c 6e 22 2c 20  id=%d (afp)\n", 
dc040 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  pFile->h,.      
dc050 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28     locktypeName(
dc060 6c 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74  locktype), lockt
dc070 79 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c  ypeName(pFile->l
dc080 6f 63 6b 74 79 70 65 29 2c 20 67 65 74 70 69 64  ocktype), getpid
dc090 28 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ());..  /* If th
dc0a0 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61  ere is already a
dc0b0 20 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79   lock of this ty
dc0c0 70 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72  pe or more restr
dc0d0 69 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20  ictive on the.  
dc0e0 2a 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20  ** unixFile, do 
dc0f0 6e 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75  nothing. Don't u
dc100 73 65 20 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c  se the afp_end_l
dc110 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20  ock: exit path, 
dc120 61 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65  as.  ** unixEnte
dc130 72 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20  rMutex() hasn't 
dc140 62 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e  been called yet.
dc150 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  .  */.  if( pFil
dc160 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63  e->locktype>=loc
dc170 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54  ktype ){.    OST
dc180 52 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25  RACE3("LOCK    %
dc190 64 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79  d %s ok (already
dc1a0 20 68 65 6c 64 29 20 28 61 66 70 29 5c 6e 22 2c   held) (afp)\n",
dc1b0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20   pFile->h,.     
dc1c0 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61        locktypeNa
dc1d0 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20  me(locktype));. 
dc1e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
dc1f0 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  _OK;.  }..  /* M
dc200 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
dc210 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  king sequence is
dc220 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20   correct.  */.  
dc230 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
dc240 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
dc250 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   || locktype==SH
dc260 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
dc270 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
dc280 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b  =PENDING_LOCK );
dc290 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
dc2a0 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype!=RESERVED_LO
dc2b0 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK || pFile->loc
dc2c0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
dc2d0 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68  CK );.  .  /* Th
dc2e0 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64  is mutex is need
dc2f0 65 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65  ed because pFile
dc300 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65  ->pLock is share
dc310 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73  d across threads
dc320 0a 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65  .  */.  unixEnte
dc330 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20  rMutex();..  /* 
dc340 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 75  Make sure the cu
dc350 72 72 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e  rrent thread own
dc360 73 20 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a  s the pFile..  *
dc370 2f 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65  /.  rc = transfe
dc380 72 4f 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65  rOwnership(pFile
dc390 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
dc3a0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e  ITE_OK ){.    un
dc3b0 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a  ixLeaveMutex();.
dc3c0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
dc3d0 20 7d 0a 20 20 20 20 0a 20 20 2f 2a 20 41 20 50   }.    .  /* A P
dc3e0 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 6e  ENDING lock is n
dc3f0 65 65 64 65 64 20 62 65 66 6f 72 65 20 61 63 71  eeded before acq
dc400 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20  uiring a SHARED 
dc410 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f 72 65 0a  lock and before.
dc420 20 20 2a 2a 20 61 63 71 75 69 72 69 6e 67 20 61    ** acquiring a
dc430 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
dc440 2e 20 20 46 6f 72 20 74 68 65 20 53 48 41 52 45  .  For the SHARE
dc450 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50 45 4e 44  D lock, the PEND
dc460 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65  ING will.  ** be
dc470 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a   released..  */.
dc480 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
dc490 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20 20 20  SHARED_LOCK .   
dc4a0 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d     || (locktype=
dc4b0 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
dc4c0 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  && pFile->lockty
dc4d0 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29  pe<PENDING_LOCK)
dc4e0 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 61  .  ){.    int fa
dc4f0 69 6c 65 64 3b 0a 20 20 20 20 66 61 69 6c 65 64  iled;.    failed
dc500 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f   = afpSetLock(co
dc510 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20 70  ntext->dbPath, p
dc520 46 69 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59  File, PENDING_BY
dc530 54 45 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69  TE, 1, 1);.    i
dc540 66 20 28 66 61 69 6c 65 64 29 20 7b 0a 20 20 20  f (failed) {.   
dc550 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a     rc = failed;.
dc560 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65        goto afp_e
dc570 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20  nd_lock;.    }. 
dc580 20 7d 0a 20 20 0a 20 20 2f 2a 20 49 66 20 63 6f   }.  .  /* If co
dc590 6e 74 72 6f 6c 20 67 65 74 73 20 74 6f 20 74 68  ntrol gets to th
dc5a0 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61  is point, then a
dc5b0 63 74 75 61 6c 6c 79 20 67 6f 20 61 68 65 61 64  ctually go ahead
dc5c0 20 61 6e 64 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f   and make.  ** o
dc5d0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
dc5e0 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 73 70  calls for the sp
dc5f0 65 63 69 66 69 65 64 20 6c 6f 63 6b 2e 0a 20 20  ecified lock..  
dc600 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
dc610 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
dc620 7b 0a 20 20 20 20 69 6e 74 20 6c 6b 2c 20 6c 72  {.    int lk, lr
dc630 63 31 2c 20 6c 72 63 32 3b 0a 20 20 20 20 69 6e  c1, lrc2;.    in
dc640 74 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 30 3b  t lrc1Errno = 0;
dc650 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77  .    .    /* Now
dc660 20 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f   get the read-lo
dc670 63 6b 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a  ck SHARED_LOCK *
dc680 2f 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68  /.    /* note th
dc690 61 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f  at the quality o
dc6a0 66 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73  f the randomness
dc6b0 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20   doesn't matter 
dc6c0 74 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20  that much */.   
dc6d0 20 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20   lk = random(); 
dc6e0 0a 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68  .    context->sh
dc6f0 61 72 65 64 42 79 74 65 20 3d 20 28 6c 6b 20 26  aredByte = (lk &
dc700 20 30 78 37 66 66 66 66 66 66 66 29 25 28 53 48   0x7fffffff)%(SH
dc710 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 3b 0a  ARED_SIZE - 1);.
dc720 20 20 20 20 6c 72 63 31 20 3d 20 61 66 70 53 65      lrc1 = afpSe
dc730 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64  tLock(context->d
dc740 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20  bPath, pFile, . 
dc750 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 5f           SHARED_
dc760 46 49 52 53 54 2b 63 6f 6e 74 65 78 74 2d 3e 73  FIRST+context->s
dc770 68 61 72 65 64 42 79 74 65 2c 20 31 2c 20 31 29  haredByte, 1, 1)
dc780 3b 0a 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43  ;.    if( IS_LOC
dc790 4b 5f 45 52 52 4f 52 28 6c 72 63 31 29 20 29 7b  K_ERROR(lrc1) ){
dc7a0 0a 20 20 20 20 20 20 6c 72 63 31 45 72 72 6e 6f  .      lrc1Errno
dc7b0 20 3d 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72   = pFile->lastEr
dc7c0 72 6e 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  rno;.    }.    /
dc7d0 2a 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f  * Drop the tempo
dc7e0 72 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63  rary PENDING loc
dc7f0 6b 20 2a 2f 0a 20 20 20 20 6c 72 63 32 20 3d 20  k */.    lrc2 = 
dc800 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65  afpSetLock(conte
dc810 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  xt->dbPath, pFil
dc820 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c  e, PENDING_BYTE,
dc830 20 31 2c 20 30 29 3b 0a 20 20 20 20 0a 20 20 20   1, 0);.    .   
dc840 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
dc850 4f 52 28 6c 72 63 31 29 20 29 20 7b 0a 20 20 20  OR(lrc1) ) {.   
dc860 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
dc870 72 6e 6f 20 3d 20 6c 72 63 31 45 72 72 6e 6f 3b  rno = lrc1Errno;
dc880 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31  .      rc = lrc1
dc890 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70  ;.      goto afp
dc8a0 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d  _end_lock;.    }
dc8b0 20 65 6c 73 65 20 69 66 28 20 49 53 5f 4c 4f 43   else if( IS_LOC
dc8c0 4b 5f 45 52 52 4f 52 28 6c 72 63 32 29 20 29 7b  K_ERROR(lrc2) ){
dc8d0 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 32  .      rc = lrc2
dc8e0 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70  ;.      goto afp
dc8f0 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 7d  _end_lock;.    }
dc900 20 65 6c 73 65 20 69 66 28 20 6c 72 63 31 20 21   else if( lrc1 !
dc910 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 20 7b 0a  = SQLITE_OK ) {.
dc920 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31 3b        rc = lrc1;
dc930 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  .    } else {.  
dc940 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
dc950 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43  ype = SHARED_LOC
dc960 4b 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  K;.      pFile->
dc970 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a  pOpen->nLock++;.
dc980 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
dc990 20 20 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73     /* The reques
dc9a0 74 20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45  t was for a RESE
dc9b0 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56  RVED or EXCLUSIV
dc9c0 45 20 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20  E lock.  It is. 
dc9d0 20 20 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68     ** assumed th
dc9e0 61 74 20 74 68 65 72 65 20 69 73 20 61 20 53 48  at there is a SH
dc9f0 41 52 45 44 20 6f 72 20 67 72 65 61 74 65 72 20  ARED or greater 
dca00 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65  lock on the file
dca10 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e  .    ** already.
dca20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
dca30 66 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20  failed = 0;.    
dca40 61 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c 65  assert( 0!=pFile
dca50 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ->locktype );.  
dca60 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65 20 3e    if (locktype >
dca70 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  = RESERVED_LOCK 
dca80 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  && pFile->lockty
dca90 70 65 20 3c 20 52 45 53 45 52 56 45 44 5f 4c 4f  pe < RESERVED_LO
dcaa0 43 4b 29 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a  CK) {.        /*
dcab0 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45 52   Acquire a RESER
dcac0 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  VED lock */.    
dcad0 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70      failed = afp
dcae0 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d  SetLock(context-
dcaf0 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20  >dbPath, pFile, 
dcb00 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31  RESERVED_BYTE, 1
dcb10 2c 31 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ,1);.    }.    i
dcb20 66 20 28 21 66 61 69 6c 65 64 20 26 26 20 6c 6f  f (!failed && lo
dcb30 63 6b 74 79 70 65 20 3d 3d 20 45 58 43 4c 55 53  cktype == EXCLUS
dcb40 49 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20  IVE_LOCK) {.    
dcb50 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e 20    /* Acquire an 
dcb60 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 2a  EXCLUSIVE lock *
dcb70 2f 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20  /.        .     
dcb80 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73   /* Remove the s
dcb90 68 61 72 65 64 20 6c 6f 63 6b 20 62 65 66 6f 72  hared lock befor
dcba0 65 20 74 72 79 69 6e 67 20 74 68 65 20 72 61 6e  e trying the ran
dcbb0 67 65 2e 20 20 77 65 27 6c 6c 20 6e 65 65 64 20  ge.  we'll need 
dcbc0 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 65  to .      ** ree
dcbd0 73 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61  stablish the sha
dcbe0 72 65 64 20 6c 6f 63 6b 20 69 66 20 77 65 20 63  red lock if we c
dcbf0 61 6e 27 74 20 67 65 74 20 74 68 65 20 20 61 66  an't get the  af
dcc00 70 55 6e 6c 6f 63 6b 0a 20 20 20 20 20 20 2a 2f  pUnlock.      */
dcc10 0a 20 20 20 20 20 20 69 66 28 20 21 28 66 61 69  .      if( !(fai
dcc20 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b  led = afpSetLock
dcc30 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68  (context->dbPath
dcc40 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f  , pFile, SHARED_
dcc50 46 49 52 53 54 20 2b 0a 20 20 20 20 20 20 20 20  FIRST +.        
dcc60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dcc70 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64   context->shared
dcc80 42 79 74 65 2c 20 31 2c 20 30 29 29 20 29 7b 0a  Byte, 1, 0)) ){.
dcc90 20 20 20 20 20 20 20 20 69 6e 74 20 66 61 69 6c          int fail
dcca0 65 64 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  ed2 = SQLITE_OK;
dccb0 0a 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 77 20  .        /* now 
dccc0 61 74 74 65 6d 6d 70 74 20 74 6f 20 67 65 74 20  attemmpt to get 
dccd0 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f  the exclusive lo
dcce0 63 6b 20 72 61 6e 67 65 20 2a 2f 0a 20 20 20 20  ck range */.    
dccf0 20 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70      failed = afp
dcd00 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d  SetLock(context-
dcd10 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20  >dbPath, pFile, 
dcd20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 0a 20  SHARED_FIRST, . 
dcd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dcd40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 48                SH
dcd50 41 52 45 44 5f 53 49 5a 45 2c 20 31 29 3b 0a 20  ARED_SIZE, 1);. 
dcd60 20 20 20 20 20 20 20 69 66 28 20 66 61 69 6c 65         if( faile
dcd70 64 20 26 26 20 28 66 61 69 6c 65 64 32 20 3d 20  d && (failed2 = 
dcd80 61 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65  afpSetLock(conte
dcd90 78 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c  xt->dbPath, pFil
dcda0 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
dcdb0 20 20 20 20 20 20 20 20 20 20 20 53 48 41 52 45             SHARE
dcdc0 44 5f 46 49 52 53 54 20 2b 20 63 6f 6e 74 65 78  D_FIRST + contex
dcdd0 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31  t->sharedByte, 1
dcde0 2c 20 31 29 29 20 29 7b 0a 20 20 20 20 20 20 20  , 1)) ){.       
dcdf0 20 20 20 2f 2a 20 43 61 6e 27 74 20 72 65 65 73     /* Can't rees
dce00 74 61 62 6c 69 73 68 20 74 68 65 20 73 68 61 72  tablish the shar
dce10 65 64 20 6c 6f 63 6b 2e 20 20 53 71 6c 69 74 65  ed lock.  Sqlite
dce20 20 63 61 6e 27 74 20 64 65 61 6c 2c 20 74 68 69   can't deal, thi
dce30 73 20 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a  s is.          *
dce40 2a 20 61 20 63 72 69 74 69 63 61 6c 20 49 2f 4f  * a critical I/O
dce50 20 65 72 72 6f 72 0a 20 20 20 20 20 20 20 20 20   error.         
dce60 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63   */.          rc
dce70 20 3d 20 28 28 66 61 69 6c 65 64 20 26 20 53 51   = ((failed & SQ
dce80 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53  LITE_IOERR) == S
dce90 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3f 20 66  QLITE_IOERR) ? f
dcea0 61 69 6c 65 64 32 20 3a 20 0a 20 20 20 20 20 20  ailed2 : .      
dceb0 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
dcec0 49 4f 45 52 52 5f 4c 4f 43 4b 3b 0a 20 20 20 20  IOERR_LOCK;.    
dced0 20 20 20 20 20 20 67 6f 74 6f 20 61 66 70 5f 65        goto afp_e
dcee0 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20 20 20  nd_lock;.       
dcef0 20 7d 20 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   } .      }else{
dcf00 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 61  .        rc = fa
dcf10 69 6c 65 64 3b 20 0a 20 20 20 20 20 20 7d 0a 20  iled; .      }. 
dcf20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66 61 69     }.    if( fai
dcf30 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20  led ){.      rc 
dcf40 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a  = failed;.    }.
dcf50 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 72 63 3d    }.  .  if( rc=
dcf60 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
dcf70 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
dcf80 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  e = locktype;.  
dcf90 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79  }else if( lockty
dcfa0 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
dcfb0 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  CK ){.    pFile-
dcfc0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e 44  >locktype = PEND
dcfd0 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 7d 0a 20 20  ING_LOCK;.  }.  
dcfe0 0a 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20  .afp_end_lock:. 
dcff0 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28   unixLeaveMutex(
dd000 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 22 4c  );.  OSTRACE4("L
dd010 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25 73 20  OCK    %d %s %s 
dd020 28 61 66 70 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  (afp)\n", pFile-
dd030 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65  >h, locktypeName
dd040 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20  (locktype), .   
dd050 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49 54 45        rc==SQLITE
dd060 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66 61  _OK ? "ok" : "fa
dd070 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75 72 6e  iled");.  return
dd080 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f   rc;.}../*.** Lo
dd090 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
dd0a0 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65  level on file de
dd0b0 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74  scriptor pFile t
dd0c0 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63  o locktype.  loc
dd0d0 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65  ktype.** must be
dd0e0 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20   either NO_LOCK 
dd0f0 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a  or SHARED_LOCK..
dd100 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63  **.** If the loc
dd110 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68  king level of th
dd120 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
dd130 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74 20  r is already at 
dd140 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20  or below.** the 
dd150 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e  requested lockin
dd160 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f  g level, this ro
dd170 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
dd180 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
dd190 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  afpUnlock(sqlite
dd1a0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
dd1b0 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 69 6e  locktype) {.  in
dd1c0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
dd1d0 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  ;.  unixFile *pF
dd1e0 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
dd1f0 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63 6b 69 6e  )id;.  afpLockin
dd200 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d  gContext *pCtx =
dd210 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74   (afpLockingCont
dd220 65 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f  ext *) pFile->lo
dd230 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20  ckingContext;.. 
dd240 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
dd250 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e  ;.  OSTRACE5("UN
dd260 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20  LOCK  %d %d was 
dd270 25 64 20 70 69 64 3d 25 64 20 28 61 66 70 29 5c  %d pid=%d (afp)\
dd280 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
dd290 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20  cktype,.        
dd2a0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dd2b0 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20  , getpid());..  
dd2c0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
dd2d0 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  <=SHARED_LOCK );
dd2e0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
dd2f0 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65  cktype<=locktype
dd300 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
dd310 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
dd320 69 66 28 20 43 48 45 43 4b 5f 54 48 52 45 41 44  if( CHECK_THREAD
dd330 49 44 28 70 46 69 6c 65 29 20 29 7b 0a 20 20 20  ID(pFile) ){.   
dd340 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
dd350 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69  ISUSE;.  }.  uni
dd360 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  xEnterMutex();. 
dd370 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
dd380 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
dd390 20 29 7b 0a 20 20 20 20 0a 20 20 20 20 69 66 28   ){.    .    if(
dd3a0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dd3b0 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ==EXCLUSIVE_LOCK
dd3c0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
dd3d0 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e  fpSetLock(pCtx->
dd3e0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 53  dbPath, pFile, S
dd3f0 48 41 52 45 44 5f 46 49 52 53 54 2c 20 53 48 41  HARED_FIRST, SHA
dd400 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20  RED_SIZE, 0);.  
dd410 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
dd420 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70  TE_OK && locktyp
dd430 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e==SHARED_LOCK )
dd440 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c  {.        /* onl
dd450 79 20 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74  y re-establish t
dd460 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 69  he shared lock i
dd470 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20  f necessary */. 
dd480 20 20 20 20 20 20 20 69 6e 74 20 73 68 61 72 65         int share
dd490 64 4c 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52  dLockByte = SHAR
dd4a0 45 44 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73  ED_FIRST+pCtx->s
dd4b0 68 61 72 65 64 42 79 74 65 3b 0a 20 20 20 20 20  haredByte;.     
dd4c0 20 20 20 72 63 20 3d 20 61 66 70 53 65 74 4c 6f     rc = afpSetLo
dd4d0 63 6b 28 70 43 74 78 2d 3e 64 62 50 61 74 68 2c  ck(pCtx->dbPath,
dd4e0 20 70 46 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f   pFile, sharedLo
dd4f0 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20  ckByte, 1, 1);. 
dd500 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
dd510 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
dd520 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63  OK && pFile->loc
dd530 6b 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c  ktype>=PENDING_L
dd540 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  OCK ){.      rc 
dd550 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74  = afpSetLock(pCt
dd560 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  x->dbPath, pFile
dd570 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20  , PENDING_BYTE, 
dd580 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20  1, 0);.    } .  
dd590 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
dd5a0 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f  _OK && pFile->lo
dd5b0 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44  cktype>=RESERVED
dd5c0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 72  _LOCK ){.      r
dd5d0 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70  c = afpSetLock(p
dd5e0 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69  Ctx->dbPath, pFi
dd5f0 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  le, RESERVED_BYT
dd600 45 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  E, 1, 0);.    }.
dd610 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b    }else if( lock
dd620 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b  type==NO_LOCK ){
dd630 0a 20 20 20 20 2f 2a 20 63 6c 65 61 72 20 74 68  .    /* clear th
dd640 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f  e shared lock */
dd650 0a 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c  .    int sharedL
dd660 6f 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44  ockByte = SHARED
dd670 5f 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61  _FIRST+pCtx->sha
dd680 72 65 64 42 79 74 65 3b 0a 20 20 20 20 72 63 20  redByte;.    rc 
dd690 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74  = afpSetLock(pCt
dd6a0 78 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  x->dbPath, pFile
dd6b0 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  , sharedLockByte
dd6c0 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  , 1, 0);.  }..  
dd6d0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
dd6e0 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63  K ){.    if( loc
dd6f0 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29  ktype==NO_LOCK )
dd700 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 75  {.      struct u
dd710 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65  nixOpenCnt *pOpe
dd720 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e  n = pFile->pOpen
dd730 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e  ;.      pOpen->n
dd740 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73  Lock--;.      as
dd750 73 65 72 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f  sert( pOpen->nLo
dd760 63 6b 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69  ck>=0 );.      i
dd770 66 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d  f( pOpen->nLock=
dd780 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  =0 ){.        rc
dd790 20 3d 20 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46   = closePendingF
dd7a0 64 73 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20  ds(pFile);.     
dd7b0 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75   }.    }.  }.  u
dd7c0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
dd7d0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
dd7e0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c  E_OK ){.    pFil
dd7f0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
dd800 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65  cktype;.  }.  re
dd810 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
dd820 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20 26  * Close a file &
dd830 20 63 6c 65 61 6e 75 70 20 41 46 50 20 73 70 65   cleanup AFP spe
dd840 63 69 66 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f  cific locking co
dd850 6e 74 65 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63  ntext .*/.static
dd860 20 69 6e 74 20 61 66 70 43 6c 6f 73 65 28 73 71   int afpClose(sq
dd870 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20  lite3_file *id) 
dd880 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20  {.  if( id ){.  
dd890 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
dd8a0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
dd8b0 64 3b 0a 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b  d;.    afpUnlock
dd8c0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  (id, NO_LOCK);. 
dd8d0 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65     unixEnterMute
dd8e0 78 28 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69  x();.    if( pFi
dd8f0 6c 65 2d 3e 70 4f 70 65 6e 20 26 26 20 70 46 69  le->pOpen && pFi
dd900 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b  le->pOpen->nLock
dd910 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
dd920 74 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61  there are outsta
dd930 6e 64 69 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20  nding locks, do 
dd940 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f  not actually clo
dd950 73 65 20 74 68 65 20 66 69 6c 65 20 6a 75 73 74  se the file just
dd960 0a 20 20 20 20 20 20 2a 2a 20 79 65 74 20 62 65  .      ** yet be
dd970 63 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64  cause that would
dd980 20 63 6c 65 61 72 20 74 68 6f 73 65 20 6c 6f 63   clear those loc
dd990 6b 73 2e 20 20 49 6e 73 74 65 61 64 2c 20 61 64  ks.  Instead, ad
dd9a0 64 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 20  d the file.     
dd9b0 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 74   ** descriptor t
dd9c0 6f 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e  o pOpen->aPendin
dd9d0 67 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61  g.  It will be a
dd9e0 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f  utomatically clo
dd9f0 73 65 64 20 77 68 65 6e 0a 20 20 20 20 20 20 2a  sed when.      *
dda00 2a 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20  * the last lock 
dda10 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20  is cleared..    
dda20 20 20 2a 2f 0a 20 20 20 20 20 20 73 65 74 50 65    */.      setPe
dda30 6e 64 69 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a  ndingFd(pFile);.
dda40 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73      }.    releas
dda50 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e  eOpenCnt(pFile->
dda60 70 4f 70 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69  pOpen);.    sqli
dda70 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e  te3_free(pFile->
dda80 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b  lockingContext);
dda90 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69  .    closeUnixFi
ddaa0 6c 65 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78  le(id);.    unix
ddab0 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
ddac0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
ddad0 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  E_OK;.}..#endif 
ddae0 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  /* defined(__APP
ddaf0 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f  LE__) && SQLITE_
ddb00 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
ddb10 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68  TYLE */./*.** Th
ddb20 65 20 63 6f 64 65 20 61 62 6f 76 65 20 69 73 20  e code above is 
ddb30 74 68 65 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70  the AFP lock imp
ddb40 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68  lementation.  Th
ddb50 65 20 63 6f 64 65 20 69 73 20 73 70 65 63 69 66  e code is specif
ddb60 69 63 0a 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20  ic.** to MacOSX 
ddb70 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72  and does not wor
ddb80 6b 20 6f 6e 20 6f 74 68 65 72 20 75 6e 69 78 20  k on other unix 
ddb90 70 6c 61 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61  platforms.  No a
ddba0 6c 74 65 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73  lternative.** is
ddbb0 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20   available.  If 
ddbc0 79 6f 75 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c  you don't compil
ddbd0 65 20 66 6f 72 20 61 20 6d 61 63 2c 20 74 68 65  e for a mac, the
ddbe0 6e 20 74 68 65 20 22 75 6e 69 78 2d 61 66 70 22  n the "unix-afp"
ddbf0 0a 2a 2a 20 56 46 53 20 69 73 20 6e 6f 74 20 61  .** VFS is not a
ddc00 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a  vailable..**.***
ddc10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddc20 2a 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 41 46  ** End of the AF
ddc30 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74  P lock implement
ddc40 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ation **********
ddc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
ddc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddc70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddc80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f  ***********/.../
ddcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddcc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddcd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ddcf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
ddd00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
ddd10 4e 6f 6e 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69  Non-locking sqli
ddd20 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73  te3_file methods
ddd30 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
ddd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
ddd50 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 64 69  *.** The next di
ddd60 76 69 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  vision contains 
ddd70 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
ddd80 66 6f 72 20 61 6c 6c 20 6d 65 74 68 6f 64 73 20  for all methods 
ddd90 6f 66 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  of the .** sqlit
ddda0 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 6f  e3_file object o
dddb0 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6c 6f  ther than the lo
dddc0 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73 2e 20 20  cking methods.  
dddd0 54 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 6d  The locking.** m
ddde0 65 74 68 6f 64 73 20 77 65 72 65 20 64 65 66 69  ethods were defi
dddf0 6e 65 64 20 69 6e 20 64 69 76 69 73 69 6f 6e 73  ned in divisions
dde00 20 61 62 6f 76 65 20 28 6f 6e 65 20 6c 6f 63 6b   above (one lock
dde10 69 6e 67 20 6d 65 74 68 6f 64 20 70 65 72 0a 2a  ing method per.*
dde20 2a 20 64 69 76 69 73 69 6f 6e 29 2e 20 20 54 68  * division).  Th
dde30 6f 73 65 20 6d 65 74 68 6f 64 73 20 74 68 61 74  ose methods that
dde40 20 61 72 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61   are common to a
dde50 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 73  ll locking modes
dde60 0a 2a 2a 20 61 72 65 20 67 61 74 68 65 72 20 74  .** are gather t
dde70 6f 67 65 74 68 65 72 20 69 6e 74 6f 20 74 68 69  ogether into thi
dde80 73 20 64 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a  s division..*/..
dde90 2f 2a 0a 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68  /*.** Seek to th
ddea0 65 20 6f 66 66 73 65 74 20 70 61 73 73 65 64 20  e offset passed 
ddeb0 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  as the second ar
ddec0 67 75 6d 65 6e 74 2c 20 74 68 65 6e 20 72 65 61  gument, then rea
dded0 64 20 63 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20  d cnt .** bytes 
ddee0 69 6e 74 6f 20 70 42 75 66 2e 20 52 65 74 75 72  into pBuf. Retur
ddef0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
ddf00 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 72  bytes actually r
ddf10 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20  ead..**.** NB:  
ddf20 49 66 20 79 6f 75 20 64 65 66 69 6e 65 20 55 53  If you define US
ddf30 45 5f 50 52 45 41 44 20 6f 72 20 55 53 45 5f 50  E_PREAD or USE_P
ddf40 52 45 41 44 36 34 2c 20 74 68 65 6e 20 69 74 20  READ64, then it 
ddf50 6d 69 67 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65  might also.** be
ddf60 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 64 65   necessary to de
ddf70 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52  fine _XOPEN_SOUR
ddf80 43 45 20 74 6f 20 62 65 20 35 30 30 2e 20 20 54  CE to be 500.  T
ddf90 68 69 73 20 76 61 72 69 65 73 20 66 72 6f 6d 0a  his varies from.
ddfa0 2a 2a 20 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f  ** one system to
ddfb0 20 61 6e 6f 74 68 65 72 2e 20 20 53 69 6e 63 65   another.  Since
ddfc0 20 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74   SQLite does not
ddfd0 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41   define USE_PREA
ddfe0 44 0a 2a 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72  D.** any any for
ddff0 6d 20 62 79 20 64 65 66 61 75 6c 74 2c 20 77 65  m by default, we
de000 20 77 69 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70   will not attemp
de010 74 20 74 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50  t to define _XOP
de020 45 4e 5f 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65  EN_SOURCE..** Se
de030 65 20 74 69 63 6b 65 74 73 20 23 32 37 34 31 20  e tickets #2741 
de040 61 6e 64 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a  and #2681..**.**
de050 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70 69   To avoid stompi
de060 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61 6c  ng the errno val
de070 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20 72  ue on a failed r
de080 65 61 64 20 74 68 65 20 6c 61 73 74 45 72 72 6e  ead the lastErrn
de090 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  o value.** is se
de0a0 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  t before returni
de0b0 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
de0c0 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75 6e  t seekAndRead(un
de0d0 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69  ixFile *id, sqli
de0e0 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74  te3_int64 offset
de0f0 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e  , void *pBuf, in
de100 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f  t cnt){.  int go
de110 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73  t;.  i64 newOffs
de120 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52  et;.  TIMER_STAR
de130 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55  T;.#if defined(U
de140 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20  SE_PREAD).  got 
de150 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20 70  = pread(id->h, p
de160 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74  Buf, cnt, offset
de170 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
de180 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20 29  rror( got = -1 )
de190 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28  ;.#elif defined(
de1a0 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20 67  USE_PREAD64).  g
de1b0 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64 2d  ot = pread64(id-
de1c0 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f  >h, pBuf, cnt, o
de1d0 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61  ffset);.  Simula
de1e0 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d  teIOError( got =
de1f0 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e   -1 );.#else.  n
de200 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65 6b  ewOffset = lseek
de210 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20  (id->h, offset, 
de220 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d  SEEK_SET);.  Sim
de230 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e 65  ulateIOError( ne
de240 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69  wOffset-- );.  i
de250 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66  f( newOffset!=of
de260 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28 20  fset ){.    if( 
de270 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20  newOffset == -1 
de280 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78 46  ){.      ((unixF
de290 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72  ile*)id)->lastEr
de2a0 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
de2b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 28   }else{.      ((
de2c0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
de2d0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09  astErrno = 0;...
de2e0 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
de2f0 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20  n -1;.  }.  got 
de300 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42  = read(id->h, pB
de310 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66  uf, cnt);.#endif
de320 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20  .  TIMER_END;.  
de330 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20  if( got<0 ){.   
de340 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
de350 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
de360 72 6e 6f 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41  rno;.  }.  OSTRA
de370 43 45 35 28 22 52 45 41 44 20 20 20 20 25 2d 33  CE5("READ    %-3
de380 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75  d %5d %7lld %llu
de390 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c  \n", id->h, got,
de3a0 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52 5f 45   offset, TIMER_E
de3b0 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74 75 72  LAPSED);.  retur
de3c0 6e 20 67 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n got;.}../*.** 
de3d0 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61  Read data from a
de3e0 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66   file into a buf
de3f0 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  fer.  Return SQL
de400 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a  ITE_OK if all.**
de410 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64   bytes were read
de420 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e   successfully an
de430 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69  d SQLITE_IOERR i
de440 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a  f anything goes.
de450 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61  ** wrong..*/.sta
de460 74 69 63 20 69 6e 74 20 75 6e 69 78 52 65 61 64  tic int unixRead
de470 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
de480 20 2a 69 64 2c 20 0a 20 20 76 6f 69 64 20 2a 70   *id, .  void *p
de490 42 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c  Buf, .  int amt,
de4a0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
de4b0 20 6f 66 66 73 65 74 0a 29 7b 0a 20 20 75 6e 69   offset.){.  uni
de4c0 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
de4d0 75 6e 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20  unixFile *)id;. 
de4e0 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61 73 73 65   int got;.  asse
de4f0 72 74 28 20 69 64 20 29 3b 0a 0a 20 20 2f 2a 20  rt( id );..  /* 
de500 49 66 20 74 68 69 73 20 69 73 20 61 20 64 61 74  If this is a dat
de510 61 62 61 73 65 20 66 69 6c 65 20 28 6e 6f 74 20  abase file (not 
de520 61 20 6a 6f 75 72 6e 61 6c 2c 20 6d 61 73 74 65  a journal, maste
de530 72 2d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 65 6d  r-journal or tem
de540 70 0a 20 20 2a 2a 20 66 69 6c 65 29 2c 20 74 68  p.  ** file), th
de550 65 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6c  e bytes in the l
de560 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20 73 68 6f  ocking range sho
de570 75 6c 64 20 6e 65 76 65 72 20 62 65 20 72 65 61  uld never be rea
de580 64 20 6f 72 20 77 72 69 74 74 65 6e 2e 20 2a 2f  d or written. */
de590 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
de5a0 2d 3e 70 55 6e 75 73 65 64 3d 3d 30 0a 20 20 20  ->pUnused==0.   
de5b0 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50      || offset>=P
de5c0 45 4e 44 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a  ENDING_BYTE+512.
de5d0 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73 65 74         || offset
de5e0 2b 61 6d 74 3c 3d 50 45 4e 44 49 4e 47 5f 42 59  +amt<=PENDING_BY
de5f0 54 45 20 0a 20 20 29 3b 0a 0a 20 20 67 6f 74 20  TE .  );..  got 
de600 3d 20 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46  = seekAndRead(pF
de610 69 6c 65 2c 20 6f 66 66 73 65 74 2c 20 70 42 75  ile, offset, pBu
de620 66 2c 20 61 6d 74 29 3b 0a 20 20 69 66 28 20 67  f, amt);.  if( g
de630 6f 74 3d 3d 61 6d 74 20 29 7b 0a 20 20 20 20 72  ot==amt ){.    r
de640 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
de650 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 67 6f 74  .  }else if( got
de660 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 6c 61 73  <0 ){.    /* las
de670 74 45 72 72 6e 6f 20 73 65 74 20 62 79 20 73 65  tErrno set by se
de680 65 6b 41 6e 64 52 65 61 64 20 2a 2f 0a 20 20 20  ekAndRead */.   
de690 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
de6a0 4f 45 52 52 5f 52 45 41 44 3b 0a 20 20 7d 65 6c  OERR_READ;.  }el
de6b0 73 65 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  se{.    pFile->l
de6c0 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a  astErrno = 0; /*
de6d0 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65 72   not a system er
de6e0 72 6f 72 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 6e  ror */.    /* Un
de6f0 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74 68  read parts of th
de700 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62 65  e buffer must be
de710 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a   zero-filled */.
de720 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68      memset(&((ch
de730 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20  ar*)pBuf)[got], 
de740 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20  0, amt-got);.   
de750 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
de760 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b  OERR_SHORT_READ;
de770 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  .  }.}../*.** Se
de780 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74  ek to the offset
de790 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20 74   in id->offset t
de7a0 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79 74  hen read cnt byt
de7b0 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a  es into pBuf..**
de7c0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
de7d0 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75  er of bytes actu
de7e0 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64 61  ally read.  Upda
de7f0 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a  te the offset..*
de800 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 73 74  *.** To avoid st
de810 6f 6d 70 69 6e 67 20 74 68 65 20 65 72 72 6e 6f  omping the errno
de820 20 76 61 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c   value on a fail
de830 65 64 20 77 72 69 74 65 20 74 68 65 20 6c 61 73  ed write the las
de840 74 45 72 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20  tErrno value.** 
de850 69 73 20 73 65 74 20 62 65 66 6f 72 65 20 72 65  is set before re
de860 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  turning..*/.stat
de870 69 63 20 69 6e 74 20 73 65 65 6b 41 6e 64 57 72  ic int seekAndWr
de880 69 74 65 28 75 6e 69 78 46 69 6c 65 20 2a 69 64  ite(unixFile *id
de890 2c 20 69 36 34 20 6f 66 66 73 65 74 2c 20 63 6f  , i64 offset, co
de8a0 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  nst void *pBuf, 
de8b0 69 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20  int cnt){.  int 
de8c0 67 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66  got;.  i64 newOf
de8d0 66 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54  fset;.  TIMER_ST
de8e0 41 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64  ART;.#if defined
de8f0 28 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f  (USE_PREAD).  go
de900 74 20 3d 20 70 77 72 69 74 65 28 69 64 2d 3e 68  t = pwrite(id->h
de910 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66  , pBuf, cnt, off
de920 73 65 74 29 3b 0a 23 65 6c 69 66 20 64 65 66 69  set);.#elif defi
de930 6e 65 64 28 55 53 45 5f 50 52 45 41 44 36 34 29  ned(USE_PREAD64)
de940 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74 65 36  .  got = pwrite6
de950 34 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63  4(id->h, pBuf, c
de960 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c  nt, offset);.#el
de970 73 65 0a 20 20 6e 65 77 4f 66 66 73 65 74 20 3d  se.  newOffset =
de980 20 6c 73 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66   lseek(id->h, of
de990 66 73 65 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b  fset, SEEK_SET);
de9a0 0a 20 20 69 66 28 20 6e 65 77 4f 66 66 73 65 74  .  if( newOffset
de9b0 21 3d 6f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  !=offset ){.    
de9c0 69 66 28 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d  if( newOffset ==
de9d0 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 28 28 75   -1 ){.      ((u
de9e0 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61  nixFile*)id)->la
de9f0 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
dea00 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
dea10 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64    ((unixFile*)id
dea20 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30  )->lastErrno = 0
dea30 3b 09 09 09 0a 20 20 20 20 7d 0a 20 20 20 20 72  ;....    }.    r
dea40 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20  eturn -1;.  }.  
dea50 67 6f 74 20 3d 20 77 72 69 74 65 28 69 64 2d 3e  got = write(id->
dea60 68 2c 20 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23  h, pBuf, cnt);.#
dea70 65 6e 64 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e  endif.  TIMER_EN
dea80 44 3b 0a 20 20 69 66 28 20 67 6f 74 3c 30 20 29  D;.  if( got<0 )
dea90 7b 0a 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65  {.    ((unixFile
deaa0 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f  *)id)->lastErrno
deab0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a 20   = errno;.  }.. 
deac0 20 4f 53 54 52 41 43 45 35 28 22 57 52 49 54 45   OSTRACE5("WRITE
dead0 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c 6c     %-3d %5d %7ll
deae0 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68  d %llu\n", id->h
deaf0 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54  , got, offset, T
deb00 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20  IMER_ELAPSED);. 
deb10 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a   return got;.}..
deb20 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74  ./*.** Write dat
deb30 61 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20  a from a buffer 
deb40 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65  into a file.  Re
deb50 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
deb60 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20  n success.** or 
deb70 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72  some other error
deb80 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65   code on failure
deb90 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
deba0 75 6e 69 78 57 72 69 74 65 28 0a 20 20 73 71 6c  unixWrite(.  sql
debb0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a  ite3_file *id, .
debc0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42    const void *pB
debd0 75 66 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a  uf, .  int amt,.
debe0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
debf0 6f 66 66 73 65 74 20 0a 29 7b 0a 20 20 75 6e 69  offset .){.  uni
dec00 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
dec10 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
dec20 69 6e 74 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20  int wrote = 0;. 
dec30 20 61 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20   assert( id );. 
dec40 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29   assert( amt>0 )
dec50 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  ;..  /* If this 
dec60 69 73 20 61 20 64 61 74 61 62 61 73 65 20 66 69  is a database fi
dec70 6c 65 20 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61  le (not a journa
dec80 6c 2c 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61  l, master-journa
dec90 6c 20 6f 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66  l or temp.  ** f
deca0 69 6c 65 29 2c 20 74 68 65 20 62 79 74 65 73 20  ile), the bytes 
decb0 69 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72  in the locking r
decc0 61 6e 67 65 20 73 68 6f 75 6c 64 20 6e 65 76 65  ange should neve
decd0 72 20 62 65 20 72 65 61 64 20 6f 72 20 77 72 69  r be read or wri
dece0 74 74 65 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72  tten. */.  asser
decf0 74 28 20 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65  t( pFile->pUnuse
ded00 64 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20 6f  d==0.       || o
ded10 66 66 73 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42  ffset>=PENDING_B
ded20 59 54 45 2b 35 31 32 0a 20 20 20 20 20 20 20 7c  YTE+512.       |
ded30 7c 20 6f 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45  | offset+amt<=PE
ded40 4e 44 49 4e 47 5f 42 59 54 45 20 0a 20 20 29 3b  NDING_BYTE .  );
ded50 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
ded60 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20  .  /* If we are 
ded70 64 6f 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77  doing a normal w
ded80 72 69 74 65 20 74 6f 20 61 20 64 61 74 61 62 61  rite to a databa
ded90 73 65 20 66 69 6c 65 20 28 61 73 20 6f 70 70 6f  se file (as oppo
deda0 73 65 64 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e  sed to.  ** doin
dedb0 67 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  g a hot-journal 
dedc0 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 61 20 77 72  rollback or a wr
dedd0 69 74 65 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65  ite to some file
dede0 20 6f 74 68 65 72 20 74 68 61 6e 20 61 0a 20 20   other than a.  
dedf0 2a 2a 20 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61  ** normal databa
dee00 73 65 20 66 69 6c 65 29 20 74 68 65 6e 20 72 65  se file) then re
dee10 63 6f 72 64 20 74 68 65 20 66 61 63 74 20 74 68  cord the fact th
dee20 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  at the database.
dee30 20 20 2a 2a 20 68 61 73 20 63 68 61 6e 67 65 64    ** has changed
dee40 2e 20 20 49 66 20 74 68 65 20 74 72 61 6e 73 61  .  If the transa
dee50 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73  ction counter is
dee60 20 6d 6f 64 69 66 69 65 64 2c 20 72 65 63 6f 72   modified, recor
dee70 64 20 74 68 61 74 0a 20 20 2a 2a 20 66 61 63 74  d that.  ** fact
dee80 20 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   too..  */.  if(
dee90 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c   pFile->inNormal
deea0 57 72 69 74 65 20 29 7b 0a 20 20 20 20 70 46 69  Write ){.    pFi
deeb0 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 31  le->dbUpdate = 1
deec0 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61  ;  /* The databa
deed0 73 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69  se has been modi
deee0 66 69 65 64 20 2a 2f 0a 20 20 20 20 69 66 28 20  fied */.    if( 
deef0 6f 66 66 73 65 74 3c 3d 32 34 20 26 26 20 6f 66  offset<=24 && of
def00 66 73 65 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a  fset+amt>=27 ){.
def10 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20        int rc;.  
def20 20 20 20 20 63 68 61 72 20 6f 6c 64 43 6e 74 72      char oldCntr
def30 5b 34 5d 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c  [4];.      Simul
def40 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e  ateIOErrorBenign
def50 28 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  (1);.      rc = 
def60 73 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69 6c  seekAndRead(pFil
def70 65 2c 20 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20  e, 24, oldCntr, 
def80 34 29 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61  4);.      Simula
def90 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
defa0 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
defb0 21 3d 34 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c  !=4 || memcmp(ol
defc0 64 43 6e 74 72 2c 20 26 28 28 63 68 61 72 2a 29  dCntr, &((char*)
defd0 70 42 75 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d  pBuf)[24-offset]
defe0 2c 20 34 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  , 4)!=0 ){.     
deff0 20 20 20 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43     pFile->transC
df000 6e 74 72 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a  ntrChng = 1;  /*
df010 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   The transaction
df020 20 63 6f 75 6e 74 65 72 20 68 61 73 20 63 68 61   counter has cha
df030 6e 67 65 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  nged */.      }.
df040 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
df050 0a 0a 20 20 77 68 69 6c 65 28 20 61 6d 74 3e 30  ..  while( amt>0
df060 20 26 26 20 28 77 72 6f 74 65 20 3d 20 73 65 65   && (wrote = see
df070 6b 41 6e 64 57 72 69 74 65 28 70 46 69 6c 65 2c  kAndWrite(pFile,
df080 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61   offset, pBuf, a
df090 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d  mt))>0 ){.    am
df0a0 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20  t -= wrote;.    
df0b0 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b  offset += wrote;
df0c0 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28 63  .    pBuf = &((c
df0d0 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74 65  har*)pBuf)[wrote
df0e0 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74  ];.  }.  Simulat
df0f0 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74 65  eIOError(( wrote
df100 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b  =(-1), amt=1 ));
df110 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  .  SimulateDiskf
df120 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74 65  ullError(( wrote
df130 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20  =0, amt=1 ));.  
df140 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20  if( amt>0 ){.   
df150 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b 0a   if( wrote<0 ){.
df160 20 20 20 20 20 20 2f 2a 20 6c 61 73 74 45 72 72        /* lastErr
df170 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41 6e  no set by seekAn
df180 64 57 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20  dWrite */.      
df190 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
df1a0 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d  ERR_WRITE;.    }
df1b0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 46 69 6c  else{.      pFil
df1c0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30  e->lastErrno = 0
df1d0 3b 20 2f 2a 20 6e 6f 74 20 61 20 73 79 73 74 65  ; /* not a syste
df1e0 6d 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20  m error */.     
df1f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46   return SQLITE_F
df200 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ULL;.    }.  }. 
df210 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
df220 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  K;.}..#ifdef SQL
df230 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43  ITE_TEST./*.** C
df240 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20  ount the number 
df250 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64  of fullsyncs and
df260 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20   normal syncs.  
df270 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20  This is used to 
df280 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e  test.** that syn
df290 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73  cs and fullsyncs
df2a0 20 61 72 65 20 6f 63 63 75 72 72 69 6e 67 20 61   are occurring a
df2b0 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d 65  t the right time
df2c0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
df2d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e   int sqlite3_syn
df2e0 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c  c_count = 0;.SQL
df2f0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
df300 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75  te3_fullsync_cou
df310 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
df320 2f 2a 0a 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20  /*.** We do not 
df330 74 72 75 73 74 20 73 79 73 74 65 6d 73 20 74 6f  trust systems to
df340 20 70 72 6f 76 69 64 65 20 61 20 77 6f 72 6b 69   provide a worki
df350 6e 67 20 66 64 61 74 61 73 79 6e 63 28 29 2e 20  ng fdatasync(). 
df360 20 53 6f 6d 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68   Some do..** Oth
df370 65 72 73 20 64 6f 20 6e 6f 2e 20 20 54 6f 20 62  ers do no.  To b
df380 65 20 73 61 66 65 2c 20 77 65 20 77 69 6c 6c 20  e safe, we will 
df390 73 74 69 63 6b 20 77 69 74 68 20 74 68 65 20 28  stick with the (
df3a0 73 6c 6f 77 65 72 29 20 66 73 79 6e 63 28 29 2e  slower) fsync().
df3b0 0a 2a 2a 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20  .** If you know 
df3c0 74 68 61 74 20 79 6f 75 72 20 73 79 73 74 65 6d  that your system
df3d0 20 64 6f 65 73 20 73 75 70 70 6f 72 74 20 66 64   does support fd
df3e0 61 74 61 73 79 6e 63 28 29 20 63 6f 72 72 65 63  atasync() correc
df3f0 74 6c 79 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d  tly,.** then sim
df400 70 6c 79 20 63 6f 6d 70 69 6c 65 20 77 69 74 68  ply compile with
df410 20 2d 44 66 64 61 74 61 73 79 6e 63 3d 66 64 61   -Dfdatasync=fda
df420 74 61 73 79 6e 63 0a 2a 2f 0a 23 69 66 20 21 64  tasync.*/.#if !d
df430 65 66 69 6e 65 64 28 66 64 61 74 61 73 79 6e 63  efined(fdatasync
df440 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f  ) && !defined(__
df450 6c 69 6e 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e  linux__).# defin
df460 65 20 66 64 61 74 61 73 79 6e 63 20 66 73 79 6e  e fdatasync fsyn
df470 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  c.#endif../*.** 
df480 44 65 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c  Define HAVE_FULL
df490 46 53 59 4e 43 20 74 6f 20 30 20 6f 72 20 31 20  FSYNC to 0 or 1 
df4a0 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65  depending on whe
df4b0 74 68 65 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74  ther or not.** t
df4c0 68 65 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d  he F_FULLFSYNC m
df4d0 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64 2e  acro is defined.
df4e0 20 20 46 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73    F_FULLFSYNC is
df4f0 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e   currently.** on
df500 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20  ly available on 
df510 4d 61 63 20 4f 53 20 58 2e 20 20 42 75 74 20 74  Mac OS X.  But t
df520 68 61 74 20 63 6f 75 6c 64 20 63 68 61 6e 67 65  hat could change
df530 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 46 5f 46 55  ..*/.#ifdef F_FU
df540 4c 4c 46 53 59 4e 43 0a 23 20 64 65 66 69 6e 65  LLFSYNC.# define
df550 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20   HAVE_FULLFSYNC 
df560 31 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  1.#else.# define
df570 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20   HAVE_FULLFSYNC 
df580 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  0.#endif.../*.**
df590 20 54 68 65 20 66 73 79 6e 63 28 29 20 73 79 73   The fsync() sys
df5a0 74 65 6d 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f  tem call does no
df5b0 74 20 77 6f 72 6b 20 61 73 20 61 64 76 65 72 74  t work as advert
df5c0 69 73 65 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20  ised on many.** 
df5d0 75 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 54  unix systems.  T
df5e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f  he following pro
df5f0 63 65 64 75 72 65 20 69 73 20 61 6e 20 61 74 74  cedure is an att
df600 65 6d 70 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20  empt to make.** 
df610 69 74 20 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a  it work better..
df620 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
df630 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64  _NO_SYNC macro d
df640 69 73 61 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e  isables all fsyn
df650 63 28 29 73 2e 20 20 54 68 69 73 20 69 73 20 75  c()s.  This is u
df660 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73  seful.** for tes
df670 74 69 6e 67 20 77 68 65 6e 20 77 65 20 77 61 6e  ting when we wan
df680 74 20 74 6f 20 72 75 6e 20 74 68 72 6f 75 67 68  t to run through
df690 20 74 68 65 20 74 65 73 74 20 73 75 69 74 65 20   the test suite 
df6a0 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20  quickly..** You 
df6b0 61 72 65 20 73 74 72 6f 6e 67 6c 79 20 61 64 76  are strongly adv
df6c0 69 73 65 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65  ised *not* to de
df6d0 70 6c 6f 79 20 77 69 74 68 20 53 51 4c 49 54 45  ploy with SQLITE
df6e0 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62  _NO_SYNC.** enab
df6f0 6c 65 64 2c 20 68 6f 77 65 76 65 72 2c 20 73 69  led, however, si
df700 6e 63 65 20 77 69 74 68 20 53 51 4c 49 54 45 5f  nce with SQLITE_
df710 4e 4f 5f 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c  NO_SYNC enabled,
df720 20 61 6e 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20   an OS crash.** 
df730 6f 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65  or power failure
df740 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72   will likely cor
df750 72 75 70 74 20 74 68 65 20 64 61 74 61 62 61 73  rupt the databas
df760 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51  e file..**.** SQ
df770 4c 69 74 65 20 73 65 74 73 20 74 68 65 20 64 61  Lite sets the da
df780 74 61 4f 6e 6c 79 20 66 6c 61 67 20 69 66 20 74  taOnly flag if t
df790 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  he size of the f
df7a0 69 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65 64  ile is unchanged
df7b0 2e 0a 2a 2a 20 54 68 65 20 69 64 65 61 20 62 65  ..** The idea be
df7c0 68 69 6e 64 20 64 61 74 61 4f 6e 6c 79 20 69 73  hind dataOnly is
df7d0 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20   that it should 
df7e0 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 65 20 66  only write the f
df7f0 69 6c 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74  ile content.** t
df800 6f 20 64 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20  o disk, not the 
df810 69 6e 6f 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20  inode.  We only 
df820 73 65 74 20 64 61 74 61 4f 6e 6c 79 20 69 66 20  set dataOnly if 
df830 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 69 73  the file size is
df840 20 0a 2a 2a 20 75 6e 63 68 61 6e 67 65 64 20 73   .** unchanged s
df850 69 6e 63 65 20 74 68 65 20 66 69 6c 65 20 73 69  ince the file si
df860 7a 65 20 69 73 20 70 61 72 74 20 6f 66 20 74 68  ze is part of th
df870 65 20 69 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65  e inode.  Howeve
df880 72 2c 20 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20  r, .** Ted Ts'o 
df890 74 65 6c 6c 73 20 75 73 20 74 68 61 74 20 66 64  tells us that fd
df8a0 61 74 61 73 79 6e 63 28 29 20 77 69 6c 6c 20 61  atasync() will a
df8b0 6c 73 6f 20 77 72 69 74 65 20 74 68 65 20 69 6e  lso write the in
df8c0 6f 64 65 20 69 66 20 74 68 65 0a 2a 2a 20 66 69  ode if the.** fi
df8d0 6c 65 20 73 69 7a 65 20 68 61 73 20 63 68 61 6e  le size has chan
df8e0 67 65 64 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72  ged.  The only r
df8f0 65 61 6c 20 64 69 66 66 65 72 65 6e 63 65 20 62  eal difference b
df900 65 74 77 65 65 6e 20 66 64 61 74 61 73 79 6e 63  etween fdatasync
df910 28 29 0a 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28  ().** and fsync(
df920 29 2c 20 54 65 64 20 74 65 6c 6c 73 20 75 73 2c  ), Ted tells us,
df930 20 69 73 20 74 68 61 74 20 66 64 61 74 61 73 79   is that fdatasy
df940 6e 63 28 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c  nc() will not fl
df950 75 73 68 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65  ush the.** inode
df960 20 69 66 20 74 68 65 20 6d 74 69 6d 65 20 6f 72   if the mtime or
df970 20 6f 77 6e 65 72 20 6f 72 20 6f 74 68 65 72 20   owner or other 
df980 69 6e 6f 64 65 20 61 74 74 72 69 62 75 74 65 73  inode attributes
df990 20 68 61 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a   have changed..*
df9a0 2a 20 57 65 20 6f 6e 6c 79 20 63 61 72 65 20 61  * We only care a
df9b0 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20 73 69  bout the file si
df9c0 7a 65 2c 20 6e 6f 74 20 74 68 65 20 6f 74 68 65  ze, not the othe
df9d0 72 20 66 69 6c 65 20 61 74 74 72 69 62 75 74 65  r file attribute
df9e0 73 2c 20 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20  s, so.** as far 
df9f0 61 73 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6e  as SQLite is con
dfa00 63 65 72 6e 65 64 2c 20 61 6e 20 66 64 61 74 61  cerned, an fdata
dfa10 73 79 6e 63 28 29 20 69 73 20 61 6c 77 61 79 73  sync() is always
dfa20 20 61 64 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f   adequate..** So
dfa30 2c 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20  , we always use 
dfa40 66 64 61 74 61 73 79 6e 63 28 29 20 69 66 20 69  fdatasync() if i
dfa50 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20  t is available, 
dfa60 72 65 67 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a  regardless of.**
dfa70 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
dfa80 65 20 64 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e  e dataOnly flag.
dfa90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
dfaa0 75 6c 6c 5f 66 73 79 6e 63 28 69 6e 74 20 66 64  ull_fsync(int fd
dfab0 2c 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20  , int fullSync, 
dfac0 69 6e 74 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20  int dataOnly){. 
dfad0 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54   int rc;..  /* T
dfae0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66  he following "if
dfaf0 64 65 66 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20  def/elif/else/" 
dfb00 62 6c 6f 63 6b 20 68 61 73 20 74 68 65 20 73 61  block has the sa
dfb10 6d 65 20 73 74 72 75 63 74 75 72 65 20 61 73 0a  me structure as.
dfb20 20 20 2a 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c    ** the one bel
dfb30 6f 77 2e 20 49 74 20 69 73 20 72 65 70 6c 69 63  ow. It is replic
dfb40 61 74 65 64 20 68 65 72 65 20 73 6f 6c 65 6c 79  ated here solely
dfb50 20 74 6f 20 61 76 6f 69 64 20 63 6c 75 74 74 65   to avoid clutte
dfb60 72 69 6e 67 20 0a 20 20 2a 2a 20 75 70 20 74 68  ring .  ** up th
dfb70 65 20 72 65 61 6c 20 63 6f 64 65 20 77 69 74 68  e real code with
dfb80 20 74 68 65 20 55 4e 55 53 45 44 5f 50 41 52 41   the UNUSED_PARA
dfb90 4d 45 54 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a  METER() macros..
dfba0 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49    */.#ifdef SQLI
dfbb0 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55  TE_NO_SYNC.  UNU
dfbc0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 66 64  SED_PARAMETER(fd
dfbd0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
dfbe0 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b  METER(fullSync);
dfbf0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
dfc00 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23  TER(dataOnly);.#
dfc10 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53  elif HAVE_FULLFS
dfc20 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  YNC.  UNUSED_PAR
dfc30 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29  AMETER(dataOnly)
dfc40 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44  ;.#else.  UNUSED
dfc50 5f 50 41 52 41 4d 45 54 45 52 28 66 75 6c 6c 53  _PARAMETER(fullS
dfc60 79 6e 63 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  ync);.  UNUSED_P
dfc70 41 52 41 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c  ARAMETER(dataOnl
dfc80 79 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  y);.#endif..  /*
dfc90 20 52 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62   Record the numb
dfca0 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74  er of times that
dfcb0 20 77 65 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20   we do a normal 
dfcc0 66 73 79 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a  fsync() and .  *
dfcd0 2a 20 46 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69  * FULLSYNC.  Thi
dfce0 73 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67  s is used during
dfcf0 20 74 65 73 74 69 6e 67 20 74 6f 20 76 65 72 69   testing to veri
dfd00 66 79 20 74 68 61 74 20 74 68 69 73 20 70 72 6f  fy that this pro
dfd10 63 65 64 75 72 65 0a 20 20 2a 2a 20 67 65 74 73  cedure.  ** gets
dfd20 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65   called with the
dfd30 20 63 6f 72 72 65 63 74 20 61 72 67 75 6d 65 6e   correct argumen
dfd40 74 73 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  ts..  */.#ifdef 
dfd50 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66  SQLITE_TEST.  if
dfd60 28 20 66 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c  ( fullSync ) sql
dfd70 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
dfd80 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  unt++;.  sqlite3
dfd90 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  _sync_count++;.#
dfda0 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77  endif..  /* If w
dfdb0 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20  e compiled with 
dfdc0 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59  the SQLITE_NO_SY
dfdd0 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79  NC flag, then sy
dfde0 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20  ncing is a.  ** 
dfdf0 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65  no-op.  */.#ifde
dfe00 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43  f SQLITE_NO_SYNC
dfe10 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  .  rc = SQLITE_O
dfe20 4b 3b 0a 23 65 6c 69 66 20 48 41 56 45 5f 46 55  K;.#elif HAVE_FU
dfe30 4c 4c 46 53 59 4e 43 0a 20 20 69 66 28 20 66 75  LLFSYNC.  if( fu
dfe40 6c 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 72 63  llSync ){.    rc
dfe50 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46   = fcntl(fd, F_F
dfe60 55 4c 4c 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20  ULLFSYNC, 0);.  
dfe70 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
dfe80 31 3b 0a 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74  1;.  }.  /* If t
dfe90 68 65 20 46 55 4c 4c 46 53 59 4e 43 20 66 61 69  he FULLFSYNC fai
dfea0 6c 65 64 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74  led, fall back t
dfeb0 6f 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 20  o attempting an 
dfec0 66 73 79 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74  fsync()..  ** It
dfed0 20 73 68 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f   shouldn't be po
dfee0 73 73 69 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66  ssible for fullf
dfef0 73 79 6e 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20  sync to fail on 
dff00 74 68 65 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20  the local .  ** 
dff10 66 69 6c 65 20 73 79 73 74 65 6d 20 28 6f 6e 20  file system (on 
dff20 4f 53 58 29 2c 20 73 6f 20 66 61 69 6c 75 72 65  OSX), so failure
dff30 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
dff40 46 55 4c 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69  FULLFSYNC.  ** i
dff50 73 6e 27 74 20 73 75 70 70 6f 72 74 65 64 20 66  sn't supported f
dff60 6f 72 20 74 68 69 73 20 66 69 6c 65 20 73 79 73  or this file sys
dff70 74 65 6d 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74  tem. So, attempt
dff80 20 61 6e 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20   an fsync .  ** 
dff90 61 6e 64 20 28 66 6f 72 20 6e 6f 77 29 20 69 67  and (for now) ig
dffa0 6e 6f 72 65 20 74 68 65 20 6f 76 65 72 68 65 61  nore the overhea
dffb0 64 20 6f 66 20 61 20 73 75 70 65 72 66 6c 75 6f  d of a superfluo
dffc0 75 73 20 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20  us fcntl call.  
dffd0 0a 20 20 2a 2a 20 49 74 27 64 20 62 65 20 62 65  .  ** It'd be be
dffe0 74 74 65 72 20 74 6f 20 64 65 74 65 63 74 20 66  tter to detect f
dfff0 75 6c 6c 66 73 79 6e 63 20 73 75 70 70 6f 72 74  ullfsync support
e0000 20 6f 6e 63 65 20 61 6e 64 20 61 76 6f 69 64 20   once and avoid 
e0010 0a 20 20 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20  .  ** the fcntl 
e0020 63 61 6c 6c 20 65 76 65 72 79 20 74 69 6d 65 20  call every time 
e0030 73 79 6e 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a  sync is called..
e0040 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20    */.  if( rc ) 
e0050 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a  rc = fsync(fd);.
e0060 0a 23 65 6c 73 65 20 0a 20 20 72 63 20 3d 20 66  .#else .  rc = f
e0070 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a 23 69  datasync(fd);.#i
e0080 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69  f OS_VXWORKS.  i
e0090 66 28 20 72 63 3d 3d 2d 31 20 26 26 20 65 72 72  f( rc==-1 && err
e00a0 6e 6f 3d 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20  no==ENOTSUP ){. 
e00b0 20 20 20 72 63 20 3d 20 66 73 79 6e 63 28 66 64     rc = fsync(fd
e00c0 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  );.  }.#endif /*
e00d0 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23   OS_VXWORKS */.#
e00e0 65 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53  endif /* ifdef S
e00f0 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c  QLITE_NO_SYNC el
e0100 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e  if HAVE_FULLFSYN
e0110 43 20 2a 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56  C */..  if( OS_V
e0120 58 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d  XWORKS && rc!= -
e0130 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b  1 ){.    rc = 0;
e0140 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
e0150 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  ;.}../*.** Make 
e0160 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20  sure all writes 
e0170 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  to a particular 
e0180 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74  file are committ
e0190 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a  ed to disk..**.*
e01a0 2a 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30  * If dataOnly==0
e01b0 20 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66   then both the f
e01c0 69 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69  ile itself and i
e01d0 74 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c  ts metadata (fil
e01e0 65 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73  e.** size, acces
e01f0 73 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65  s time, etc) are
e0200 20 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74   synced.  If dat
e0210 61 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e  aOnly!=0 then on
e0220 6c 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64  ly the.** file d
e0230 61 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a  ata is synced..*
e0240 2a 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c  *.** Under Unix,
e0250 20 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20   also make sure 
e0260 74 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f  that the directo
e0270 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  ry entry for the
e0280 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65   file.** has bee
e0290 6e 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79  n created by fsy
e02a0 6e 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63  nc-ing the direc
e02b0 74 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69  tory that contai
e02c0 6e 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20  ns the file..** 
e02d0 49 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20  If we do not do 
e02e0 74 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f  this and we enco
e02f0 75 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61  unter a power fa
e0300 69 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63  ilure, the direc
e0310 74 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f  tory.** entry fo
e0320 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69  r the journal mi
e0330 67 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66  ght not exist af
e0340 74 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20  ter we reboot.  
e0350 54 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69  The next.** SQLi
e0360 74 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  te to access the
e0370 20 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b   file will not k
e0380 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75  now that the jou
e0390 72 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63  rnal exists (bec
e03a0 61 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65  ause.** the dire
e03b0 63 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20  ctory entry for 
e03c0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20  the journal was 
e03d0 6e 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61  never created) a
e03e0 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  nd the transacti
e03f0 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72  on.** will not r
e0400 6f 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69  oll back - possi
e0410 62 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64  bly leading to d
e0420 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
e0430 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
e0440 74 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74  t unixSync(sqlit
e0450 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
e0460 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72   flags){.  int r
e0470 63 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  c;.  unixFile *p
e0480 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
e0490 2a 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44  *)id;..  int isD
e04a0 61 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73  ataOnly = (flags
e04b0 26 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54  &SQLITE_SYNC_DAT
e04c0 41 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73  AONLY);.  int is
e04d0 46 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67  Fullsync = (flag
e04e0 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f  s&0x0F)==SQLITE_
e04f0 53 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a  SYNC_FULL;..  /*
e0500 20 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20   Check that one 
e0510 6f 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  of SQLITE_SYNC_N
e0520 4f 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61  ORMAL or FULL wa
e0530 73 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73  s passed */.  as
e0540 73 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46  sert((flags&0x0F
e0550 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  )==SQLITE_SYNC_N
e0560 4f 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28  ORMAL.      || (
e0570 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c  flags&0x0F)==SQL
e0580 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20  ITE_SYNC_FULL.  
e0590 29 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61  );..  /* Unix ca
e05a0 6e 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73  nnot, but some s
e05b0 79 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72  ystems may retur
e05c0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72  n SQLITE_FULL fr
e05d0 6f 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20  om here. This.  
e05e0 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65  ** line is to te
e05f0 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f  st that doing so
e0600 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20   does not cause 
e0610 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20  any problems..  
e0620 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73  */.  SimulateDis
e0630 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75  kfullError( retu
e0640 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29  rn SQLITE_FULL )
e0650 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  ;..  assert( pFi
e0660 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32  le );.  OSTRACE2
e0670 28 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e  ("SYNC    %-3d\n
e0680 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20  ", pFile->h);.  
e0690 72 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28  rc = full_fsync(
e06a0 70 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c  pFile->h, isFull
e06b0 73 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79  sync, isDataOnly
e06c0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
e06d0 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20  rror( rc=1 );.  
e06e0 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 70 46  if( rc ){.    pF
e06f0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
e0700 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75   errno;.    retu
e0710 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
e0720 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28  FSYNC;.  }.  if(
e0730 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30   pFile->dirfd>=0
e0740 20 29 7b 0a 20 20 20 20 69 6e 74 20 65 72 72 3b   ){.    int err;
e0750 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 44  .    OSTRACE4("D
e0760 49 52 53 59 4e 43 20 25 2d 33 64 20 28 68 61 76  IRSYNC %-3d (hav
e0770 65 5f 66 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66  e_fullfsync=%d f
e0780 75 6c 6c 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20  ullsync=%d)\n", 
e0790 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 0a 20 20  pFile->dirfd,.  
e07a0 20 20 20 20 20 20 20 20 20 20 48 41 56 45 5f 46            HAVE_F
e07b0 55 4c 4c 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c  ULLFSYNC, isFull
e07c0 73 79 6e 63 29 3b 0a 23 69 66 6e 64 65 66 20 53  sync);.#ifndef S
e07d0 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 44 49  QLITE_DISABLE_DI
e07e0 52 53 59 4e 43 0a 20 20 20 20 2f 2a 20 54 68 65  RSYNC.    /* The
e07f0 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20   directory sync 
e0800 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65  is only attempte
e0810 64 20 69 66 20 66 75 6c 6c 5f 66 73 79 6e 63 20  d if full_fsync 
e0820 69 73 0a 20 20 20 20 2a 2a 20 74 75 72 6e 65 64  is.    ** turned
e0830 20 6f 66 66 20 6f 72 20 75 6e 61 76 61 69 6c 61   off or unavaila
e0840 62 6c 65 2e 20 20 49 66 20 61 20 66 75 6c 6c 5f  ble.  If a full_
e0850 66 73 79 6e 63 20 6f 63 63 75 72 72 65 64 20 61  fsync occurred a
e0860 62 6f 76 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65  bove,.    ** the
e0870 6e 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  n the directory 
e0880 73 79 6e 63 20 69 73 20 73 75 70 65 72 66 6c 75  sync is superflu
e0890 6f 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ous..    */.    
e08a0 69 66 28 20 28 21 48 41 56 45 5f 46 55 4c 4c 46  if( (!HAVE_FULLF
e08b0 53 59 4e 43 20 7c 7c 20 21 69 73 46 75 6c 6c 73  SYNC || !isFulls
e08c0 79 6e 63 29 20 26 26 20 66 75 6c 6c 5f 66 73 79  ync) && full_fsy
e08d0 6e 63 28 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c  nc(pFile->dirfd,
e08e0 30 2c 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f  0,0) ){.       /
e08f0 2a 0a 20 20 20 20 20 20 20 2a 2a 20 57 65 20 68  *.       ** We h
e0900 61 76 65 20 72 65 63 65 69 76 65 64 20 6d 75 6c  ave received mul
e0910 74 69 70 6c 65 20 72 65 70 6f 72 74 73 20 6f 66  tiple reports of
e0920 20 66 73 79 6e 63 28 29 20 72 65 74 75 72 6e 69   fsync() returni
e0930 6e 67 0a 20 20 20 20 20 20 20 2a 2a 20 65 72 72  ng.       ** err
e0940 6f 72 73 20 77 68 65 6e 20 61 70 70 6c 69 65 64  ors when applied
e0950 20 74 6f 20 64 69 72 65 63 74 6f 72 69 65 73 20   to directories 
e0960 6f 6e 20 63 65 72 74 61 69 6e 20 66 69 6c 65 20  on certain file 
e0970 73 79 73 74 65 6d 73 2e 0a 20 20 20 20 20 20 20  systems..       
e0980 2a 2a 20 41 20 66 61 69 6c 65 64 20 64 69 72 65  ** A failed dire
e0990 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20 6e 6f  ctory sync is no
e09a0 74 20 61 20 62 69 67 20 64 65 61 6c 2e 20 20 53  t a big deal.  S
e09b0 6f 20 69 74 20 73 65 65 6d 73 0a 20 20 20 20 20  o it seems.     
e09c0 20 20 2a 2a 20 62 65 74 74 65 72 20 74 6f 20 69    ** better to i
e09d0 67 6e 6f 72 65 20 74 68 65 20 65 72 72 6f 72 2e  gnore the error.
e09e0 20 20 54 69 63 6b 65 74 20 23 31 36 35 37 0a 20    Ticket #1657. 
e09f0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
e0a00 2f 2a 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72  /* pFile->lastEr
e0a10 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a  rno = errno; */.
e0a20 20 20 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e         /* return
e0a30 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a   SQLITE_IOERR; *
e0a40 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  /.    }.#endif. 
e0a50 20 20 20 65 72 72 20 3d 20 63 6c 6f 73 65 28 70     err = close(p
e0a60 46 69 6c 65 2d 3e 64 69 72 66 64 29 3b 20 2f 2a  File->dirfd); /*
e0a70 20 4f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 79   Only need to sy
e0a80 6e 63 20 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73  nc once, so clos
e0a90 65 20 74 68 65 20 2a 2f 0a 20 20 20 20 69 66 28  e the */.    if(
e0aa0 20 65 72 72 3d 3d 30 20 29 7b 20 20 20 20 20 20   err==0 ){      
e0ab0 20 20 20 20 20 20 20 20 2f 2a 20 64 69 72 65 63          /* direc
e0ac0 74 6f 72 79 20 77 68 65 6e 20 77 65 20 61 72 65  tory when we are
e0ad0 20 64 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 70   done */.      p
e0ae0 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31  File->dirfd = -1
e0af0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
e0b00 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
e0b10 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20  rno = errno;.   
e0b20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
e0b30 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a  OERR_DIR_CLOSE;.
e0b40 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
e0b50 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
e0b60 54 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e  Truncate an open
e0b70 20 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69   file to a speci
e0b80 66 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61  fied size.*/.sta
e0b90 74 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75 6e  tic int unixTrun
e0ba0 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  cate(sqlite3_fil
e0bb0 65 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65  e *id, i64 nByte
e0bc0 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61  ){.  int rc;.  a
e0bd0 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 53  ssert( id );.  S
e0be0 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
e0bf0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
e0c00 45 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a  ERR_TRUNCATE );.
e0c10 20 20 72 63 20 3d 20 66 74 72 75 6e 63 61 74 65    rc = ftruncate
e0c20 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29  (((unixFile*)id)
e0c30 2d 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74  ->h, (off_t)nByt
e0c40 65 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a  e);.  if( rc ){.
e0c50 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29      ((unixFile*)
e0c60 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  id)->lastErrno =
e0c70 20 65 72 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75   errno;.    retu
e0c80 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
e0c90 54 52 55 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73  TRUNCATE;.  }els
e0ca0 65 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  e{.#ifndef NDEBU
e0cb0 47 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61  G.    /* If we a
e0cc0 72 65 20 64 6f 69 6e 67 20 61 20 6e 6f 72 6d 61  re doing a norma
e0cd0 6c 20 77 72 69 74 65 20 74 6f 20 61 20 64 61 74  l write to a dat
e0ce0 61 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 6f  abase file (as o
e0cf0 70 70 6f 73 65 64 20 74 6f 0a 20 20 20 20 2a 2a  pposed to.    **
e0d00 20 64 6f 69 6e 67 20 61 20 68 6f 74 2d 6a 6f 75   doing a hot-jou
e0d10 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20 6f 72  rnal rollback or
e0d20 20 61 20 77 72 69 74 65 20 74 6f 20 73 6f 6d 65   a write to some
e0d30 20 66 69 6c 65 20 6f 74 68 65 72 20 74 68 61 6e   file other than
e0d40 20 61 0a 20 20 20 20 2a 2a 20 6e 6f 72 6d 61 6c   a.    ** normal
e0d50 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 20   database file) 
e0d60 61 6e 64 20 77 65 20 74 72 75 6e 63 61 74 65 20  and we truncate 
e0d70 74 68 65 20 66 69 6c 65 20 74 6f 20 7a 65 72 6f  the file to zero
e0d80 20 6c 65 6e 67 74 68 2c 0a 20 20 20 20 2a 2a 20   length,.    ** 
e0d90 74 68 61 74 20 65 66 66 65 63 74 69 76 65 6c 79  that effectively
e0da0 20 75 70 64 61 74 65 73 20 74 68 65 20 63 68 61   updates the cha
e0db0 6e 67 65 20 63 6f 75 6e 74 65 72 2e 20 20 54 68  nge counter.  Th
e0dc0 69 73 20 6d 69 67 68 74 20 68 61 70 70 65 6e 0a  is might happen.
e0dd0 20 20 20 20 2a 2a 20 77 68 65 6e 20 72 65 73 74      ** when rest
e0de0 6f 72 69 6e 67 20 61 20 64 61 74 61 62 61 73 65  oring a database
e0df0 20 75 73 69 6e 67 20 74 68 65 20 62 61 63 6b 75   using the backu
e0e00 70 20 41 50 49 20 66 72 6f 6d 20 61 20 7a 65 72  p API from a zer
e0e10 6f 2d 6c 65 6e 67 74 68 0a 20 20 20 20 2a 2a 20  o-length.    ** 
e0e20 73 6f 75 72 63 65 2e 0a 20 20 20 20 2a 2f 0a 20  source..    */. 
e0e30 20 20 20 69 66 28 20 28 28 75 6e 69 78 46 69 6c     if( ((unixFil
e0e40 65 2a 29 69 64 29 2d 3e 69 6e 4e 6f 72 6d 61 6c  e*)id)->inNormal
e0e50 57 72 69 74 65 20 26 26 20 6e 42 79 74 65 3d 3d  Write && nByte==
e0e60 30 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69  0 ){.      ((uni
e0e70 78 46 69 6c 65 2a 29 69 64 29 2d 3e 74 72 61 6e  xFile*)id)->tran
e0e80 73 43 6e 74 72 43 68 6e 67 20 3d 20 31 3b 0a 20  sCntrChng = 1;. 
e0e90 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
e0ea0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
e0eb0 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  K;.  }.}../*.** 
e0ec0 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75  Determine the cu
e0ed0 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20  rrent size of a 
e0ee0 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f  file in bytes.*/
e0ef0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e0f00 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33  FileSize(sqlite3
e0f10 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a  _file *id, i64 *
e0f20 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63  pSize){.  int rc
e0f30 3b 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 20  ;.  struct stat 
e0f40 62 75 66 3b 0a 20 20 61 73 73 65 72 74 28 20 69  buf;.  assert( i
e0f50 64 20 29 3b 0a 20 20 72 63 20 3d 20 66 73 74 61  d );.  rc = fsta
e0f60 74 28 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64  t(((unixFile*)id
e0f70 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a 20 20 53  )->h, &buf);.  S
e0f80 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
e0f90 72 63 3d 31 20 29 3b 0a 20 20 69 66 28 20 72 63  rc=1 );.  if( rc
e0fa0 21 3d 30 20 29 7b 0a 20 20 20 20 28 28 75 6e 69  !=0 ){.    ((uni
e0fb0 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74  xFile*)id)->last
e0fc0 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
e0fd0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e0fe0 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20  _IOERR_FSTAT;.  
e0ff0 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20 62 75 66  }.  *pSize = buf
e1000 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20  .st_size;..  /* 
e1010 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20 61 20 7a  When opening a z
e1020 65 72 6f 2d 73 69 7a 65 20 64 61 74 61 62 61 73  ero-size databas
e1030 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f 63 6b 49  e, the findLockI
e1040 6e 66 6f 28 29 20 70 72 6f 63 65 64 75 72 65 0a  nfo() procedure.
e1050 20 20 2a 2a 20 77 72 69 74 65 73 20 61 20 73 69    ** writes a si
e1060 6e 67 6c 65 20 62 79 74 65 20 69 6e 74 6f 20 74  ngle byte into t
e1070 68 61 74 20 66 69 6c 65 20 69 6e 20 6f 72 64 65  hat file in orde
e1080 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64  r to work around
e1090 20 61 20 62 75 67 0a 20 20 2a 2a 20 69 6e 20 74   a bug.  ** in t
e10a0 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73 20 66 69  he OS-X msdos fi
e10b0 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e 20 6f 72  lesystem.  In or
e10c0 64 65 72 20 74 6f 20 61 76 6f 69 64 20 70 72 6f  der to avoid pro
e10d0 62 6c 65 6d 73 20 77 69 74 68 20 75 70 70 65 72  blems with upper
e10e0 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c 20 77 65  .  ** layers, we
e10f0 20 6e 65 65 64 20 74 6f 20 72 65 70 6f 72 74 20   need to report 
e1100 74 68 69 73 20 66 69 6c 65 20 73 69 7a 65 20 61  this file size a
e1110 73 20 7a 65 72 6f 20 65 76 65 6e 20 74 68 6f 75  s zero even thou
e1120 67 68 20 69 74 20 69 73 0a 20 20 2a 2a 20 72 65  gh it is.  ** re
e1130 61 6c 6c 79 20 31 2e 20 20 20 54 69 63 6b 65 74  ally 1.   Ticket
e1140 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a 20 20 69   #3260..  */.  i
e1150 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20 29 20 2a  f( *pSize==1 ) *
e1160 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a 20 20 72  pSize = 0;...  r
e1170 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e1180 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  .}..#if SQLITE_E
e1190 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e11a0 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  YLE && defined(_
e11b0 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20  _APPLE__)./*.** 
e11c0 48 61 6e 64 6c 65 72 20 66 6f 72 20 70 72 6f 78  Handler for prox
e11d0 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c 65 2d 63  y-locking file-c
e11e0 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e 20 20 44  ontrol verbs.  D
e11f0 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 6e 20  efined below in 
e1200 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69 6e 67 20  the.** proxying 
e1210 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73 69 6f 6e  locking division
e1220 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e1230 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f 6c  proxyFileControl
e1240 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69  (sqlite3_file*,i
e1250 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69  nt,void*);.#endi
e1260 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d  f.../*.** Inform
e1270 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 6f  ation and contro
e1280 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 6c  l of an open fil
e1290 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61  e handle..*/.sta
e12a0 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65  tic int unixFile
e12b0 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f  Control(sqlite3_
e12c0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70  file *id, int op
e12d0 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20  , void *pArg){. 
e12e0 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20   switch( op ){. 
e12f0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
e1300 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20  CNTL_LOCKSTATE: 
e1310 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70  {.      *(int*)p
e1320 41 72 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65  Arg = ((unixFile
e1330 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b  *)id)->locktype;
e1340 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
e1350 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
e1360 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4c     case SQLITE_L
e1370 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a 20 20 20  AST_ERRNO: {.   
e1380 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d     *(int*)pArg =
e1390 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
e13a0 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20 20  ->lastErrno;.   
e13b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e13c0 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  _OK;.    }.#ifnd
e13d0 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a  ef NDEBUG.    /*
e13e0 20 54 68 65 20 70 61 67 65 72 20 63 61 6c 6c 73   The pager calls
e13f0 20 74 68 69 73 20 6d 65 74 68 6f 64 20 74 6f 20   this method to 
e1400 73 69 67 6e 61 6c 20 74 68 61 74 20 69 74 20 68  signal that it h
e1410 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a 2a 20 61  as done.    ** a
e1420 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 74 68   rollback and th
e1430 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  at the database 
e1440 69 73 20 74 68 65 72 65 66 6f 72 65 20 75 6e 63  is therefore unc
e1450 68 61 6e 67 65 64 20 61 6e 64 0a 20 20 20 20 2a  hanged and.    *
e1460 2a 20 69 74 20 68 65 6e 63 65 20 69 74 20 69 73  * it hence it is
e1470 20 4f 4b 20 66 6f 72 20 74 68 65 20 74 72 61 6e   OK for the tran
e1480 73 61 63 74 69 6f 6e 20 63 68 61 6e 67 65 20 63  saction change c
e1490 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a 20 20 20  ounter to be.   
e14a0 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20   ** unchanged.. 
e14b0 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
e14c0 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55  QLITE_FCNTL_DB_U
e14d0 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20 20 20 20  NCHANGED: {.    
e14e0 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64    ((unixFile*)id
e14f0 29 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30 3b  )->dbUpdate = 0;
e1500 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
e1510 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23  LITE_OK;.    }.#
e1520 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
e1530 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
e1540 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64  STYLE && defined
e1550 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20  (__APPLE__).    
e1560 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54 5f  case SQLITE_SET_
e1570 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 0a 20  LOCKPROXYFILE:. 
e1580 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 47     case SQLITE_G
e1590 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45  ET_LOCKPROXYFILE
e15a0 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  : {.      return
e15b0 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74 72 6f   proxyFileContro
e15c0 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29 3b 0a 20  l(id,op,pArg);. 
e15d0 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53     }.#endif /* S
e15e0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
e15f0 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65  KING_STYLE && de
e1600 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29  fined(__APPLE__)
e1610 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   */.  }.  return
e1620 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d   SQLITE_ERROR;.}
e1630 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
e1640 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69  he sector size i
e1650 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75  n bytes of the u
e1660 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20  nderlying block 
e1670 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68  device for.** th
e1680 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65  e specified file
e1690 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74  . This is almost
e16a0 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65   always 512 byte
e16b0 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a  s, but may be.**
e16c0 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65   larger for some
e16d0 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20   devices..**.** 
e16e0 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75  SQLite code assu
e16f0 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f  mes this functio
e1700 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49  n cannot fail. I
e1710 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74  t also assumes t
e1720 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69  hat.** if two fi
e1730 6c 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20  les are created 
e1740 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65  in the same file
e1750 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72  -system director
e1760 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74  y (i.e..** a dat
e1770 61 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f  abase and its jo
e1780 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74  urnal file) that
e1790 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   the sector size
e17a0 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20   will be the.** 
e17b0 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a  same for both..*
e17c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
e17d0 78 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69  xSectorSize(sqli
e17e0 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65  te3_file *NotUse
e17f0 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
e1800 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e1810 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
e1820 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
e1830 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  SIZE;.}../*.** R
e1840 65 74 75 72 6e 20 74 68 65 20 64 65 76 69 63 65  eturn the device
e1850 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63 73   characteristics
e1860 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2e 20 54   for the file. T
e1870 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 30 20  his is always 0 
e1880 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a 73 74 61  for unix..*/.sta
e1890 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 76 69  tic int unixDevi
e18a0 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
e18b0 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  s(sqlite3_file *
e18c0 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53  NotUsed){.  UNUS
e18d0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
e18e0 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  Used);.  return 
e18f0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 72 65  0;.}../*.** Here
e1900 20 65 6e 64 73 20 74 68 65 20 69 6d 70 6c 65 6d   ends the implem
e1910 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20  entation of all 
e1920 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6d 65 74  sqlite3_file met
e1930 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  hods..**.*******
e1940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
e1950 45 6e 64 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  End sqlite3_file
e1960 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a 2a   Methods *******
e1970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1980 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
e1990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e19a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e19b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e19c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e19d0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
e19e0 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f  This division co
e19f0 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f  ntains definitio
e1a00 6e 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6f  ns of sqlite3_io
e1a10 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73  _methods objects
e1a20 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65   that.** impleme
e1a30 6e 74 20 76 61 72 69 6f 75 73 20 66 69 6c 65 20  nt various file 
e1a40 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 69  locking strategi
e1a50 65 73 2e 20 20 49 74 20 61 6c 73 6f 20 63 6f 6e  es.  It also con
e1a60 74 61 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e  tains definition
e1a70 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64 65 72 22  s.** of "finder"
e1a80 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 41 20 66   functions.  A f
e1a90 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69  inder-function i
e1aa0 73 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65  s used to locate
e1ab0 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
e1ac0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  .** sqlite3_io_m
e1ad0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 66 6f  ethods object fo
e1ae0 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  r a particular d
e1af0 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
e1b00 68 65 20 70 41 70 70 44 61 74 61 0a 2a 2a 20 66  he pAppData.** f
e1b10 69 65 6c 64 20 6f 66 20 74 68 65 20 73 71 6c 69  ield of the sqli
e1b20 74 65 33 5f 76 66 73 20 56 46 53 20 6f 62 6a 65  te3_vfs VFS obje
e1b30 63 74 73 20 61 72 65 20 69 6e 69 74 69 61 6c 69  cts are initiali
e1b40 7a 65 64 20 74 6f 20 62 65 20 70 6f 69 6e 74 65  zed to be pointe
e1b50 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20 63 6f 72  rs to.** the cor
e1b60 72 65 63 74 20 66 69 6e 64 65 72 2d 66 75 6e 63  rect finder-func
e1b70 74 69 6f 6e 20 66 6f 72 20 74 68 61 74 20 56 46  tion for that VF
e1b80 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 66 69  S..**.** Most fi
e1b90 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 72  nder functions r
e1ba0 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
e1bb0 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69 74  to a fixed sqlit
e1bc0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a  e3_io_methods.**
e1bd0 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 6f 6e   object.  The on
e1be0 6c 79 20 69 6e 74 65 72 65 73 74 69 6e 67 20 66  ly interesting f
e1bf0 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20 69  inder-function i
e1c00 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64  s autolockIoFind
e1c10 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20 6c 6f 6f  er, which.** loo
e1c20 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65 73 79  ks at the filesy
e1c30 73 74 65 6d 20 74 79 70 65 20 61 6e 64 20 74 72  stem type and tr
e1c40 69 65 73 20 74 6f 20 67 75 65 73 73 20 74 68 65  ies to guess the
e1c50 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67 0a 2a 2a   best locking.**
e1c60 20 73 74 72 61 74 65 67 79 20 66 72 6f 6d 20 74   strategy from t
e1c70 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66  hat..**.** For f
e1c80 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e 20 46 2c  inder-funtion F,
e1c90 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 61 72 65   two objects are
e1ca0 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20   created:.**.** 
e1cb0 20 20 20 28 31 29 20 54 68 65 20 72 65 61 6c 20     (1) The real 
e1cc0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 20  finder-function 
e1cd0 6e 61 6d 65 64 20 22 46 49 6d 70 74 28 29 22 2e  named "FImpt()".
e1ce0 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 41 20  .**.**    (2) A 
e1cf0 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e 74 65 72  constant pointer
e1d00 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
e1d10 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20 22 46 22  n named just "F"
e1d20 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69  ..**.**.** A poi
e1d30 6e 74 65 72 20 74 6f 20 74 68 65 20 46 20 70 6f  nter to the F po
e1d40 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73  inter is used as
e1d50 20 74 68 65 20 70 41 70 70 44 61 74 61 20 76 61   the pAppData va
e1d60 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a 2a 20 6f  lue for VFS.** o
e1d70 62 6a 65 63 74 73 2e 20 20 57 65 20 68 61 76 65  bjects.  We have
e1d80 20 74 6f 20 64 6f 20 74 68 69 73 20 69 6e 73 74   to do this inst
e1d90 65 61 64 20 6f 66 20 6c 65 74 74 69 6e 67 20 70  ead of letting p
e1da0 41 70 70 44 61 74 61 20 70 6f 69 6e 74 0a 2a 2a  AppData point.**
e1db0 20 64 69 72 65 63 74 6c 79 20 61 74 20 74 68 65   directly at the
e1dc0 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
e1dd0 20 73 69 6e 63 65 20 43 39 30 20 72 75 6c 65 73   since C90 rules
e1de0 20 70 72 65 76 65 6e 74 20 61 20 76 6f 69 64 2a   prevent a void*
e1df0 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63 61 73 74  .** from be cast
e1e00 20 69 6e 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e   into a function
e1e10 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 0a   pointer..**.**.
e1e20 2a 2a 20 45 61 63 68 20 69 6e 73 74 61 6e 63 65  ** Each instance
e1e30 20 6f 66 20 74 68 69 73 20 6d 61 63 72 6f 20 67   of this macro g
e1e40 65 6e 65 72 61 74 65 73 20 74 77 6f 20 6f 62 6a  enerates two obj
e1e50 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  ects:.**.**   * 
e1e60 20 41 20 63 6f 6e 73 74 61 6e 74 20 73 71 6c 69   A constant sqli
e1e70 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  te3_io_methods o
e1e80 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45 54 48 4f  bject call METHO
e1e90 44 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b 69  D that has locki
e1ea0 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65 74 68 6f  ng.**      metho
e1eb0 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43 4b 2c 20  ds CLOSE, LOCK, 
e1ec0 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53 4c 4f 43  UNLOCK, CKRESLOC
e1ed0 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 20 41 6e  K..**.**   *  An
e1ee0 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64   I/O method find
e1ef0 65 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c  er function call
e1f00 65 64 20 46 49 4e 44 45 52 20 74 68 61 74 20 72  ed FINDER that r
e1f10 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
e1f20 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20  .**      to the 
e1f30 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74 20 69 6e  METHOD object in
e1f40 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62 75   the previous bu
e1f50 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  llet..*/.#define
e1f60 20 49 4f 4d 45 54 48 4f 44 53 28 46 49 4e 44 45   IOMETHODS(FINDE
e1f70 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c 4f 53 45  R, METHOD, CLOSE
e1f80 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43 4b 2c 20  , LOCK, UNLOCK, 
e1f90 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20 20 20 20  CKLOCK)         
e1fa0 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63        \.static c
e1fb0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
e1fc0 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f 44 20 3d  methods METHOD =
e1fd0 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
e1fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ff0 20 20 20 20 20 5c 0a 20 20 20 31 2c 20 20 20 20       \.   1,    
e2000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2010 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f        /* iVersio
e2020 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  n */            
e2030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2040 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53 45 2c 20      \.   CLOSE, 
e2050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2060 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a       /* xClose *
e2070 2f 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 5c 0a 20 20 20 75 6e 69 78 52 65 61 64     \.   unixRead
e20a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e20b0 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 20      /* xRead */ 
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 5c 0a 20 20 20 75 6e 69 78 57 72 69 74 65    \.   unixWrite
e20f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e2100 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f 20     /* xWrite */ 
e2110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2130 20 5c 0a 20 20 20 75 6e 69 78 54 72 75 6e 63 61   \.   unixTrunca
e2140 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  te,             
e2150 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a    /* xTruncate *
e2160 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e2170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2180 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 20  \.   unixSync,  
e2190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e21a0 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 20   /* xSync */    
e21b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e21c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e21d0 0a 20 20 20 75 6e 69 78 46 69 6c 65 53 69 7a 65  .   unixFileSize
e21e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e21f0 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 20  /* xFileSize */ 
e2200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e2220 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20     LOCK,        
e2230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e2240 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20  * xLock */      
e2250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2260 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e2270 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20 20 20 20    UNLOCK,       
e2280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e2290 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20 20 20   xUnlock */     
e22a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e22b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e22c0 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20 20 20 20   CKLOCK,        
e22d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e22e0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
e22f0 63 6b 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  ck */           
e2300 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
e2310 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c  unixFileControl,
e2320 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e2330 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 20 20  FileControl */  
e2340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2350 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75            \.   u
e2360 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 2c 20 20  nixSectorSize,  
e2370 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53             /* xS
e2380 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20 20 20 20  ectorSize */    
e2390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e23a0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 75 6e           \.   un
e23b0 69 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65  ixDeviceCharacte
e23c0 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78 44 65  ristics   /* xDe
e23d0 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65 73  viceCapabilities
e23e0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
e23f0 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20 20 20 20          \.};    
e2400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2440 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20         \.static 
e2450 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
e2460 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e 44 45 52  _methods *FINDER
e2470 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20 63 68 61  ##Impl(const cha
e2480 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c 65 20 2a  r *z, unixFile *
e2490 70 29 7b 20 20 20 5c 0a 20 20 55 4e 55 53 45 44  p){   \.  UNUSED
e24a0 5f 50 41 52 41 4d 45 54 45 52 28 7a 29 3b 20 55  _PARAMETER(z); U
e24b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e24c0 70 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p);             
e24d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e24e0 20 20 20 20 20 5c 0a 20 20 72 65 74 75 72 6e 20       \.  return 
e24f0 26 4d 45 54 48 4f 44 3b 20 20 20 20 20 20 20 20  &METHOD;        
e2500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2530 20 20 20 20 5c 0a 7d 20 20 20 20 20 20 20 20 20      \.}         
e2540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2580 20 20 20 5c 0a 73 74 61 74 69 63 20 63 6f 6e 73     \.static cons
e2590 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e25a0 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74 20 46 49  hods *(*const FI
e25b0 4e 44 45 52 29 28 63 6f 6e 73 74 20 63 68 61 72  NDER)(const char
e25c0 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70 29 20 20  *,unixFile *p)  
e25d0 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e 44 45 52    \.    = FINDER
e25e0 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 48  ##Impl;../*.** H
e25f0 65 72 65 20 61 72 65 20 61 6c 6c 20 6f 66 20 74  ere are all of t
e2600 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  he sqlite3_io_me
e2610 74 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f  thods objects fo
e2620 72 20 65 61 63 68 20 6f 66 20 74 68 65 0a 2a 2a  r each of the.**
e2630 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
e2640 69 65 73 2e 20 20 46 75 6e 63 74 69 6f 6e 73 20  ies.  Functions 
e2650 74 68 61 74 20 72 65 74 75 72 6e 20 70 6f 69 6e  that return poin
e2660 74 65 72 73 20 74 6f 20 74 68 65 73 65 20 6d 65  ters to these me
e2670 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20 61 6c 73  thods.** are als
e2680 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 49 4f  o created..*/.IO
e2690 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f 73 69 78  METHODS(.  posix
e26a0 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
e26b0 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66       /* Finder f
e26c0 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
e26d0 20 20 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73    posixIoMethods
e26e0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73  ,           /* s
e26f0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e2700 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f  s object name */
e2710 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c 20 20 20  .  unixClose,   
e2720 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2730 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f  xClose method */
e2740 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20 20 20 20  .  unixLock,    
e2750 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e2760 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  xLock method */.
e2770 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c 20 20 20    unixUnlock,   
e2780 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e2790 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  Unlock method */
e27a0 0a 20 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65  .  unixCheckRese
e27b0 72 76 65 64 4c 6f 63 6b 20 20 20 20 20 2f 2a 20  rvedLock     /* 
e27c0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
e27d0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 49  ck method */.).I
e27e0 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e 6f 6c 6f  OMETHODS(.  nolo
e27f0 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20  ckIoFinder,     
e2800 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20        /* Finder 
e2810 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  function name */
e2820 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f  .  nolockIoMetho
e2830 64 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ds,          /* 
e2840 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
e2850 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a  ds object name *
e2860 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 2c  /.  nolockClose,
e2870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e2880 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a   xClose method *
e2890 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c 20  /.  nolockLock, 
e28a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e28b0 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f   xLock method */
e28c0 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c  .  nolockUnlock,
e28d0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e28e0 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  xUnlock method *
e28f0 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52  /.  nolockCheckR
e2900 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 2f 2a  eservedLock   /*
e2910 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c   xCheckReservedL
e2920 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a  ock method */.).
e2930 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 64 6f 74  IOMETHODS(.  dot
e2940 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20  lockIoFinder,   
e2950 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72         /* Finder
e2960 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a   function name *
e2970 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74  /.  dotlockIoMet
e2980 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 2f 2a  hods,         /*
e2990 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e29a0 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20  ods object name 
e29b0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73  */.  dotlockClos
e29c0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e,             /
e29d0 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20  * xClose method 
e29e0 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c 6f 63 6b  */.  dotlockLock
e29f0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e2a00 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  * xLock method *
e2a10 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63  /.  dotlockUnloc
e2a20 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  k,            /*
e2a30 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20   xUnlock method 
e2a40 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43 68 65 63  */.  dotlockChec
e2a50 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 2f  kReservedLock  /
e2a60 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
e2a70 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29  Lock method */.)
e2a80 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
e2a90 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
e2aa0 45 20 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53  E && !OS_VXWORKS
e2ab0 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 66 6c  .IOMETHODS(.  fl
e2ac0 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  ockIoFinder,    
e2ad0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
e2ae0 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
e2af0 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d 65 74 68  */.  flockIoMeth
e2b00 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ods,           /
e2b10 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  * sqlite3_io_met
e2b20 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hods object name
e2b30 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c 6f 73 65   */.  flockClose
e2b40 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e2b50 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
e2b60 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f 63 6b 2c   */.  flockLock,
e2b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2b80 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
e2b90 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b  */.  flockUnlock
e2ba0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e2bb0 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
e2bc0 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68 65 63 6b   */.  flockCheck
e2bd0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20  ReservedLock    
e2be0 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
e2bf0 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
e2c00 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 4f 53  ).#endif..#if OS
e2c10 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45 54 48 4f  _VXWORKS.IOMETHO
e2c20 44 53 28 0a 20 20 73 65 6d 49 6f 46 69 6e 64 65  DS(.  semIoFinde
e2c30 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
e2c40 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69  /* Finder functi
e2c50 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65 6d  on name */.  sem
e2c60 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20  IoMethods,      
e2c70 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
e2c80 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  3_io_methods obj
e2c90 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 73 65  ect name */.  se
e2ca0 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20  mClose,         
e2cb0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73          /* xClos
e2cc0 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65  e method */.  se
e2cd0 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  mLock,          
e2ce0 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b          /* xLock
e2cf0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65 6d   method */.  sem
e2d00 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  Unlock,         
e2d10 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63         /* xUnloc
e2d20 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 65  k method */.  se
e2d30 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  mCheckReservedLo
e2d40 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63  ck      /* xChec
e2d50 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65  kReservedLock me
e2d60 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66  thod */.).#endif
e2d70 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f  ..#if defined(__
e2d80 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49  APPLE__) && SQLI
e2d90 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
e2da0 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54 48 4f 44  G_STYLE.IOMETHOD
e2db0 53 28 0a 20 20 61 66 70 49 6f 46 69 6e 64 65 72  S(.  afpIoFinder
e2dc0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e2dd0 2a 20 46 69 6e 64 65 72 20 66 75 6e 63 74 69 6f  * Finder functio
e2de0 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70 49  n name */.  afpI
e2df0 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20  oMethods,       
e2e00 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33        /* sqlite3
e2e10 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
e2e20 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20 61 66 70  ct name */.  afp
e2e30 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20  Close,          
e2e40 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65         /* xClose
e2e50 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70   method */.  afp
e2e60 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  Lock,           
e2e70 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20         /* xLock 
e2e80 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70 55  method */.  afpU
e2e90 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20  nlock,          
e2ea0 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
e2eb0 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61 66 70   method */.  afp
e2ec0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
e2ed0 6b 20 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b  k      /* xCheck
e2ee0 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 6d 65 74  ReservedLock met
e2ef0 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a  hod */.).#endif.
e2f00 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 57 68 6f 6c  ./*.** The "Whol
e2f10 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 22 20  e File Locking" 
e2f20 66 69 6e 64 65 72 20 72 65 74 75 72 6e 73 20 74  finder returns t
e2f30 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20 6d  he same set of m
e2f40 65 74 68 6f 64 73 20 61 73 0a 2a 2a 20 74 68 65  ethods as.** the
e2f50 20 70 6f 73 69 78 20 6c 6f 63 6b 69 6e 67 20 66   posix locking f
e2f60 69 6e 64 65 72 2e 20 20 42 75 74 20 69 74 20 61  inder.  But it a
e2f70 6c 73 6f 20 73 65 74 73 20 74 68 65 20 53 51 4c  lso sets the SQL
e2f80 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c  ITE_WHOLE_FILE_L
e2f90 4f 43 4b 49 4e 47 0a 2a 2a 20 66 6c 61 67 20 74  OCKING.** flag t
e2fa0 6f 20 66 6f 72 63 65 20 74 68 65 20 70 6f 73 69  o force the posi
e2fb0 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  x advisory locks
e2fc0 20 74 6f 20 63 6f 76 65 72 20 74 68 65 20 77 68   to cover the wh
e2fd0 6f 6c 65 20 66 69 6c 65 20 69 6e 73 74 65 61 64  ole file instead
e2fe0 0a 2a 2a 20 6f 66 20 6a 75 73 74 20 61 20 73 6d  .** of just a sm
e2ff0 61 6c 6c 20 73 70 61 6e 20 6f 66 20 62 79 74 65  all span of byte
e3000 73 20 6e 65 61 72 20 74 68 65 20 31 47 69 42 20  s near the 1GiB 
e3010 62 6f 75 6e 64 61 72 79 2e 20 20 57 68 6f 6c 65  boundary.  Whole
e3020 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67 0a 2a 2a   File Locking.**
e3030 20 69 73 20 75 73 65 66 75 6c 20 6f 6e 20 4e 46   is useful on NF
e3040 53 2d 6d 6f 75 6e 74 65 64 20 66 69 6c 65 73 20  S-mounted files 
e3050 73 69 6e 63 65 20 69 74 20 68 65 6c 70 73 20 4e  since it helps N
e3060 46 53 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 63  FS to maintain c
e3070 61 63 68 65 0a 2a 2a 20 63 6f 68 65 72 65 6e 63  ache.** coherenc
e3080 79 2e 20 20 42 75 74 20 69 74 20 69 73 20 61 20  y.  But it is a 
e3090 64 65 74 72 69 6d 65 6e 74 20 74 6f 20 6f 74 68  detriment to oth
e30a0 65 72 20 66 69 6c 65 73 79 73 74 65 6d 73 20 73  er filesystems s
e30b0 69 6e 63 65 20 69 74 20 72 75 6e 73 0a 2a 2a 20  ince it runs.** 
e30c0 73 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  slower..*/.stati
e30d0 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
e30e0 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 6f 73 69  io_methods *posi
e30f0 78 57 66 6c 49 6f 46 69 6e 64 65 72 49 6d 70 6c  xWflIoFinderImpl
e3100 28 63 6f 6e 73 74 20 63 68 61 72 2a 7a 2c 20 75  (const char*z, u
e3110 6e 69 78 46 69 6c 65 2a 70 29 7b 0a 20 20 55 4e  nixFile*p){.  UN
e3120 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a  USED_PARAMETER(z
e3130 29 3b 0a 20 20 70 2d 3e 66 69 6c 65 46 6c 61 67  );.  p->fileFlag
e3140 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45  s = SQLITE_WHOLE
e3150 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20  _FILE_LOCKING;. 
e3160 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f   return &posixIo
e3170 4d 65 74 68 6f 64 73 3b 0a 7d 0a 73 74 61 74 69  Methods;.}.stati
e3180 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
e3190 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20 20 2a 28  io_methods .  *(
e31a0 2a 63 6f 6e 73 74 20 70 6f 73 69 78 57 66 6c 49  *const posixWflI
e31b0 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73 74 20 63  oFinder)(const c
e31c0 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70  har*,unixFile *p
e31d0 29 20 3d 20 70 6f 73 69 78 57 66 6c 49 6f 46 69  ) = posixWflIoFi
e31e0 6e 64 65 72 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a 2a  nderImpl;../*.**
e31f0 20 54 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69   The proxy locki
e3200 6e 67 20 6d 65 74 68 6f 64 20 69 73 20 61 20 22  ng method is a "
e3210 73 75 70 65 72 2d 6d 65 74 68 6f 64 22 20 69 6e  super-method" in
e3220 20 74 68 65 20 73 65 6e 73 65 20 74 68 61 74 20   the sense that 
e3230 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73 65 63 6f  it.** opens seco
e3240 6e 64 61 72 79 20 66 69 6c 65 20 64 65 73 63 72  ndary file descr
e3250 69 70 74 6f 72 73 20 66 6f 72 20 74 68 65 20 63  iptors for the c
e3260 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b 20 66 69  onch and lock fi
e3270 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74 20 75 73  les and.** it us
e3280 65 73 20 70 72 6f 78 79 2c 20 64 6f 74 2d 66 69  es proxy, dot-fi
e3290 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20 66 6c 6f  le, AFP, and flo
e32a0 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20 6d 65 74  ck() locking met
e32b0 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65 0a 2a 2a  hods on those.**
e32c0 20 73 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 73   secondary files
e32d0 2e 20 20 46 6f 72 20 74 68 69 73 20 72 65 61 73  .  For this reas
e32e0 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69 6f 6e  on, the division
e32f0 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
e3300 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  .** proxy lockin
e3310 67 20 69 73 20 6c 6f 63 61 74 65 64 20 6d 75 63  g is located muc
e3320 68 20 66 75 72 74 68 65 72 20 64 6f 77 6e 20 69  h further down i
e3330 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 42 75 74  n the file.  But
e3340 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74 6f 20 67   we need.** to g
e3350 6f 20 61 68 65 61 64 20 61 6e 64 20 64 65 66 69  o ahead and defi
e3360 6e 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  ne the sqlite3_i
e3370 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64 20 66 69  o_methods and fi
e3380 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  nder function.**
e3390 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69   for proxy locki
e33a0 6e 67 20 68 65 72 65 2e 20 20 53 6f 20 77 65 20  ng here.  So we 
e33b0 66 6f 72 77 61 72 64 20 64 65 63 6c 61 72 65 20  forward declare 
e33c0 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 2e  the I/O methods.
e33d0 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
e33e0 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51  __APPLE__) && SQ
e33f0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b  LITE_ENABLE_LOCK
e3400 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 74 69 63  ING_STYLE.static
e3410 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28   int proxyClose(
e3420 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 3b 0a  sqlite3_file*);.
e3430 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
e3440 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
e3450 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 63  e*, int);.static
e3460 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b   int proxyUnlock
e3470 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20  (sqlite3_file*, 
e3480 69 6e 74 29 3b 0a 73 74 61 74 69 63 20 69 6e 74  int);.static int
e3490 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73 65 72   proxyCheckReser
e34a0 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  vedLock(sqlite3_
e34b0 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b 0a 49 4f  file*, int*);.IO
e34c0 4d 45 54 48 4f 44 53 28 0a 20 20 70 72 6f 78 79  METHODS(.  proxy
e34d0 49 6f 46 69 6e 64 65 72 2c 20 20 20 20 20 20 20  IoFinder,       
e34e0 20 20 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66       /* Finder f
e34f0 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
e3500 20 20 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73    proxyIoMethods
e3510 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73  ,           /* s
e3520 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e3530 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f  s object name */
e3540 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65 2c 20 20  .  proxyClose,  
e3550 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e3560 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f  xClose method */
e3570 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c 20 20 20  .  proxyLock,   
e3580 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e3590 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  xLock method */.
e35a0 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 2c 20 20    proxyUnlock,  
e35b0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e35c0 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  Unlock method */
e35d0 0a 20 20 70 72 6f 78 79 43 68 65 63 6b 52 65 73  .  proxyCheckRes
e35e0 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 2f 2a 20  ervedLock    /* 
e35f0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
e3600 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23  ck method */.).#
e3610 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64 65 66 69  endif...#if defi
e3620 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26  ned(__APPLE__) &
e3630 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  & SQLITE_ENABLE_
e3640 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a  LOCKING_STYLE./*
e3650 20 0a 2a 2a 20 54 68 69 73 20 22 66 69 6e 64 65   .** This "finde
e3660 72 22 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65  r" function atte
e3670 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d 69 6e  mpts to determin
e3680 65 20 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69  e the best locki
e3690 6e 67 20 73 74 72 61 74 65 67 79 20 0a 2a 2a 20  ng strategy .** 
e36a0 66 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65  for the database
e36b0 20 66 69 6c 65 20 22 66 69 6c 65 50 61 74 68 22   file "filePath"
e36c0 2e 20 20 49 74 20 74 68 65 6e 20 72 65 74 75 72  .  It then retur
e36d0 6e 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  ns the sqlite3_i
e36e0 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 62 6a  o_methods.** obj
e36f0 65 63 74 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ect that impleme
e3700 6e 74 73 20 74 68 61 74 20 73 74 72 61 74 65 67  nts that strateg
e3710 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  y..**.** This is
e3720 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f 6e 6c 79   for MacOSX only
e3730 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
e3740 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e3750 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63 6b 49 6f  hods *autolockIo
e3760 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20 20 63 6f  FinderImpl(.  co
e3770 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 50 61  nst char *filePa
e3780 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d 65 20 6f  th,    /* name o
e3790 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
e37a0 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c  ile */.  unixFil
e37b0 65 20 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20  e *pNew         
e37c0 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c 65 20 6f    /* open file o
e37d0 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 64 61  bject for the da
e37e0 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 29  tabase file */.)
e37f0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
e3800 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67 20   struct Mapping 
e3810 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
e3820 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 20   *zFilesystem;  
e3830 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
e3840 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 6e  ilesystem type n
e3850 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  ame */.    const
e3860 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e3870 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73 3b 20 20  ods *pMethods;  
e3880 20 2f 2a 20 41 70 70 72 6f 70 72 69 61 74 65 20   /* Appropriate 
e3890 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 2a  locking method *
e38a0 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20 3d 20 7b  /.  } aMap[] = {
e38b0 0a 20 20 20 20 7b 20 22 68 66 73 22 2c 20 20 20  .    { "hfs",   
e38c0 20 26 70 6f 73 69 78 49 6f 4d 65 74 68 6f 64 73   &posixIoMethods
e38d0 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66 73 22 2c   },.    { "ufs",
e38e0 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68      &posixIoMeth
e38f0 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 61 66  ods },.    { "af
e3900 70 66 73 22 2c 20 20 26 61 66 70 49 6f 4d 65 74  pfs",  &afpIoMet
e3910 68 6f 64 73 20 7d 2c 0a 23 69 66 64 65 66 20 53  hods },.#ifdef S
e3920 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46 50  QLITE_ENABLE_AFP
e3930 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20 20  _LOCKING_SMB.   
e3940 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 26 61 66   { "smbfs",  &af
e3950 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 65  pIoMethods },.#e
e3960 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d 62 66 73  lse.    { "smbfs
e3970 22 2c 20 20 26 66 6c 6f 63 6b 49 6f 4d 65 74 68  ",  &flockIoMeth
e3980 6f 64 73 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20  ods },.#endif.  
e3990 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20 26 6e    { "webdav", &n
e39a0 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 20 7d  olockIoMethods }
e39b0 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20 7d 0a 20  ,.    { 0, 0 }. 
e39c0 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73   };.  int i;.  s
e39d0 74 72 75 63 74 20 73 74 61 74 66 73 20 66 73 49  truct statfs fsI
e39e0 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20 66 6c  nfo;.  struct fl
e39f0 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20  ock lockInfo;.. 
e3a00 20 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29   if( !filePath )
e3a10 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65  {.    /* If file
e3a20 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20  Path==NULL that 
e3a30 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61  means we are dea
e3a40 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e  ling with a tran
e3a50 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a  sient file.    *
e3a60 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  * that does not 
e3a70 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65  need to be locke
e3a80 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  d. */.    return
e3a90 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64   &nolockIoMethod
e3aa0 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 74 61  s;.  }.  if( sta
e3ab0 74 66 73 28 66 69 6c 65 50 61 74 68 2c 20 26 66  tfs(filePath, &f
e3ac0 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20 29 7b 0a  sInfo) != -1 ){.
e3ad0 20 20 20 20 69 66 28 20 66 73 49 6e 66 6f 2e 66      if( fsInfo.f
e3ae0 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f 52 44 4f  _flags & MNT_RDO
e3af0 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 72 65 74  NLY ){.      ret
e3b00 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74  urn &nolockIoMet
e3b10 68 6f 64 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20  hods;.    }.    
e3b20 66 6f 72 28 69 3d 30 3b 20 61 4d 61 70 5b 69 5d  for(i=0; aMap[i]
e3b30 2e 7a 46 69 6c 65 73 79 73 74 65 6d 3b 20 69 2b  .zFilesystem; i+
e3b40 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74  +){.      if( st
e3b50 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66 5f 66 73  rcmp(fsInfo.f_fs
e3b60 74 79 70 65 6e 61 6d 65 2c 20 61 4d 61 70 5b 69  typename, aMap[i
e3b70 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 29 3d 3d  ].zFilesystem)==
e3b80 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  0 ){.        ret
e3b90 75 72 6e 20 61 4d 61 70 5b 69 5d 2e 70 4d 65 74  urn aMap[i].pMet
e3ba0 68 6f 64 73 3b 0a 20 20 20 20 20 20 7d 0a 20 20  hods;.      }.  
e3bb0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65    }.  }..  /* De
e3bc0 66 61 75 6c 74 20 63 61 73 65 2e 20 48 61 6e 64  fault case. Hand
e3bd0 6c 65 73 2c 20 61 6d 6f 6e 67 73 74 20 6f 74 68  les, amongst oth
e3be0 65 72 73 2c 20 22 6e 66 73 22 2e 0a 20 20 2a 2a  ers, "nfs"..  **
e3bf0 20 54 65 73 74 20 62 79 74 65 2d 72 61 6e 67 65   Test byte-range
e3c00 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e 74   lock using fcnt
e3c10 6c 28 29 2e 20 49 66 20 74 68 65 20 63 61 6c 6c  l(). If the call
e3c20 20 73 75 63 63 65 65 64 73 2c 20 0a 20 20 2a 2a   succeeds, .  **
e3c30 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68 65   assume that the
e3c40 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70   file-system sup
e3c50 70 6f 72 74 73 20 50 4f 53 49 58 20 73 74 79 6c  ports POSIX styl
e3c60 65 20 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20  e locks. .  */. 
e3c70 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20   lockInfo.l_len 
e3c80 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e  = 1;.  lockInfo.
e3c90 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c  l_start = 0;.  l
e3ca0 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65  ockInfo.l_whence
e3cb0 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c   = SEEK_SET;.  l
e3cc0 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d  ockInfo.l_type =
e3cd0 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20   F_RDLCK;.  if( 
e3ce0 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c 20 46  fcntl(pNew->h, F
e3cf0 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49 6e 66  _GETLK, &lockInf
e3d00 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20 20 70  o)!=-1 ) {.    p
e3d10 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d  New->fileFlags =
e3d20 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49   SQLITE_WHOLE_FI
e3d30 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20 20 20 20  LE_LOCKING;.    
e3d40 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f 4d  return &posixIoM
e3d50 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b  ethods;.  }else{
e3d60 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 6f 74  .    return &dot
e3d70 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20  lockIoMethods;. 
e3d80 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73   }.}.static cons
e3d90 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e3da0 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74  hods .  *(*const
e3db0 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65   autolockIoFinde
e3dc0 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75  r)(const char*,u
e3dd0 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f  nixFile*) = auto
e3de0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c  lockIoFinderImpl
e3df0 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66  ;..#endif /* def
e3e00 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20  ined(__APPLE__) 
e3e10 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
e3e20 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 2a  _LOCKING_STYLE *
e3e30 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b  /..#if OS_VXWORK
e3e40 53 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42  S && SQLITE_ENAB
e3e50 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e3e60 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69  ./* .** This "fi
e3e70 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61  nder" function a
e3e80 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72  ttempts to deter
e3e90 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f  mine the best lo
e3ea0 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a  cking strategy .
e3eb0 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  ** for the datab
e3ec0 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61  ase file "filePa
e3ed0 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65  th".  It then re
e3ee0 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65  turns the sqlite
e3ef0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20  3_io_methods.** 
e3f00 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c  object that impl
e3f10 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61  ements that stra
e3f20 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tegy..**.** This
e3f30 20 69 73 20 66 6f 72 20 56 58 57 6f 72 6b 73 20   is for VXWorks 
e3f40 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  only..*/.static 
e3f50 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f  const sqlite3_io
e3f60 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f  _methods *autolo
e3f70 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a  ckIoFinderImpl(.
e3f80 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69    const char *fi
e3f90 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61  lePath,    /* na
e3fa0 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
e3fb0 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69  se file */.  uni
e3fc0 78 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 20  xFile *pNew     
e3fd0 20 20 20 20 20 20 2f 2a 20 74 68 65 20 6f 70 65        /* the ope
e3fe0 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74 20 2a 2f  n file object */
e3ff0 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f  .){.  struct flo
e4000 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20  ck lockInfo;..  
e4010 69 66 28 20 21 66 69 6c 65 50 61 74 68 20 29 7b  if( !filePath ){
e4020 0a 20 20 20 20 2f 2a 20 49 66 20 66 69 6c 65 50  .    /* If fileP
e4030 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61 74 20 6d  ath==NULL that m
e4040 65 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c  eans we are deal
e4050 69 6e 67 20 77 69 74 68 20 61 20 74 72 61 6e 73  ing with a trans
e4060 69 65 6e 74 20 66 69 6c 65 0a 20 20 20 20 2a 2a  ient file.    **
e4070 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e   that does not n
e4080 65 65 64 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64  eed to be locked
e4090 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
e40a0 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73  &nolockIoMethods
e40b0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74  ;.  }..  /* Test
e40c0 20 69 66 20 66 63 6e 74 6c 28 29 20 69 73 20 73   if fcntl() is s
e40d0 75 70 70 6f 72 74 65 64 20 61 6e 64 20 75 73 65  upported and use
e40e0 20 50 4f 53 49 58 20 73 74 79 6c 65 20 6c 6f 63   POSIX style loc
e40f0 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69  ks..  ** Otherwi
e4100 73 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20  se fall back to 
e4110 74 68 65 20 6e 61 6d 65 64 20 73 65 6d 61 70 68  the named semaph
e4120 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20 20 2a 2f  ore method..  */
e4130 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65  .  lockInfo.l_le
e4140 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66  n = 1;.  lockInf
e4150 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20  o.l_start = 0;. 
e4160 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e   lockInfo.l_when
e4170 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a 20  ce = SEEK_SET;. 
e4180 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65   lockInfo.l_type
e4190 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69 66   = F_RDLCK;.  if
e41a0 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68 2c  ( fcntl(pNew->h,
e41b0 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b 49   F_GETLK, &lockI
e41c0 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20 20  nfo)!=-1 ) {.   
e41d0 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78 49 6f   return &posixIo
e41e0 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65  Methods;.  }else
e41f0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 73 65  {.    return &se
e4200 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 0a  mIoMethods;.  }.
e4210 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  }.static const s
e4220 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e4230 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74 20 61 75  s .  *(*const au
e4240 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 29 28  tolockIoFinder)(
e4250 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69 78  const char*,unix
e4260 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f 6c 6f 63  File*) = autoloc
e4270 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a  kIoFinderImpl;..
e4280 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57  #endif /* OS_VXW
e4290 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45  ORKS && SQLITE_E
e42a0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e42b0 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e  YLE */../*.** An
e42c0 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66   abstract type f
e42d0 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  or a pointer to 
e42e0 61 20 49 4f 20 6d 65 74 68 6f 64 20 66 69 6e 64  a IO method find
e42f0 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a 2a 2f 0a  er function:.*/.
e4300 74 79 70 65 64 65 66 20 63 6f 6e 73 74 20 73 71  typedef const sq
e4310 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
e4320 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79 70 65 29   *(*finder_type)
e4330 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75 6e 69  (const char*,uni
e4340 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a 2a 2a 2a  xFile*);.../****
e4350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4390 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
e43a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e43b0 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 66  ***** sqlite3_vf
e43c0 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a 2a  s methods ******
e43d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e43e0 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
e43f0 73 20 64 69 76 69 73 69 6f 6e 20 63 6f 6e 74 61  s division conta
e4400 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  ins the implemen
e4410 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74 68 6f 64  tation of method
e4420 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 71 6c 69  s on the.** sqli
e4430 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a  te3_vfs object..
e4440 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  */../*.** Initia
e4450 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  lize the content
e4460 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c  s of the unixFil
e4470 65 20 73 74 72 75 63 74 75 72 65 20 70 6f 69 6e  e structure poin
e4480 74 65 64 20 74 6f 20 62 79 20 70 49 64 2e 0a 2a  ted to by pId..*
e4490 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c  /.static int fil
e44a0 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a 20 20 73  lInUnixFile(.  s
e44b0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
e44c0 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65  ,      /* Pointe
e44d0 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74 20  r to vfs object 
e44e0 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20 20 20 20  */.  int h,     
e44f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
e4500 4f 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69  Open file descri
e4510 70 74 6f 72 20 6f 66 20 66 69 6c 65 20 62 65 69  ptor of file bei
e4520 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69  ng opened */.  i
e4530 6e 74 20 64 69 72 66 64 2c 20 20 20 20 20 20 20  nt dirfd,       
e4540 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74         /* Direct
e4550 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69 70  ory file descrip
e4560 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  tor */.  sqlite3
e4570 5f 66 69 6c 65 20 2a 70 49 64 2c 20 20 20 20 20  _file *pId,     
e4580 20 2f 2a 20 57 72 69 74 65 20 74 6f 20 74 68 65   /* Write to the
e4590 20 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74   unixFile struct
e45a0 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f  ure here */.  co
e45b0 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
e45c0 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  ame,  /* Name of
e45d0 20 74 68 65 20 66 69 6c 65 20 62 65 69 6e 67 20   the file being 
e45e0 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20  opened */.  int 
e45f0 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20  noLock,         
e4600 20 20 20 20 2f 2a 20 4f 6d 69 74 20 6c 6f 63 6b      /* Omit lock
e4610 69 6e 67 20 69 66 20 74 72 75 65 20 2a 2f 0a 20  ing if true */. 
e4620 20 69 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20   int isDelete   
e4630 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 6c 65           /* Dele
e4640 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74  te on close if t
e4650 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73  rue */.){.  cons
e4660 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e4670 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e 67 53 74  hods *pLockingSt
e4680 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  yle;.  unixFile 
e4690 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46 69 6c  *pNew = (unixFil
e46a0 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74 20 72  e *)pId;.  int r
e46b0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
e46c0 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e    assert( pNew->
e46d0 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a 20  pLock==NULL );. 
e46e0 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70   assert( pNew->p
e46f0 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20  Open==NULL );.. 
e4700 20 2f 2a 20 50 61 72 61 6d 65 74 65 72 20 69 73   /* Parameter is
e4710 44 65 6c 65 74 65 20 69 73 20 6f 6e 6c 79 20 75  Delete is only u
e4720 73 65 64 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20  sed on vxworks. 
e4730 45 78 70 72 65 73 73 20 74 68 69 73 20 65 78 70  Express this exp
e4740 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a 20 68 65  licitly .  ** he
e4750 72 65 20 74 6f 20 70 72 65 76 65 6e 74 20 63 6f  re to prevent co
e4760 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20  mpiler warnings 
e4770 61 62 6f 75 74 20 75 6e 75 73 65 64 20 70 61 72  about unused par
e4780 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 20  ameters..  */.  
e4790 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e47a0 28 69 73 44 65 6c 65 74 65 29 3b 0a 0a 20 20 4f  (isDelete);..  O
e47b0 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20 20 20  STRACE3("OPEN   
e47c0 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68 2c 20   %-3d %s\n", h, 
e47d0 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20 20 0a  zFilename);    .
e47e0 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b 0a 20    pNew->h = h;. 
e47f0 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d 20 64   pNew->dirfd = d
e4800 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48 52 45  irfd;.  SET_THRE
e4810 41 44 49 44 28 70 4e 65 77 29 3b 0a 20 20 70 4e  ADID(pNew);.  pN
e4820 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73 20 3d 20  ew->fileFlags = 
e4830 30 3b 0a 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52  0;..#if OS_VXWOR
e4840 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49 64 20 3d  KS.  pNew->pId =
e4850 20 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c 65   vxworksFindFile
e4860 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  Id(zFilename);. 
e4870 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 64 3d 3d   if( pNew->pId==
e4880 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f 63 6b 20  0 ){.    noLock 
e4890 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  = 1;.    rc = SQ
e48a0 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
e48b0 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 6f  #endif..  if( no
e48c0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63  Lock ){.    pLoc
e48d0 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26 6e 6f 6c  kingStyle = &nol
e48e0 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20  ockIoMethods;.  
e48f0 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b  }else{.    pLock
e4900 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a 2a 28 66  ingStyle = (**(f
e4910 69 6e 64 65 72 5f 74 79 70 65 2a 29 70 56 66 73  inder_type*)pVfs
e4920 2d 3e 70 41 70 70 44 61 74 61 29 28 7a 46 69 6c  ->pAppData)(zFil
e4930 65 6e 61 6d 65 2c 20 70 4e 65 77 29 3b 0a 23 69  ename, pNew);.#i
e4940 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
e4950 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20  LOCKING_STYLE.  
e4960 20 20 2f 2a 20 43 61 63 68 65 20 7a 46 69 6c 65    /* Cache zFile
e4970 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c 6f 63 6b  name in the lock
e4980 69 6e 67 20 63 6f 6e 74 65 78 74 20 28 41 46 50  ing context (AFP
e4990 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20 6f 76 65   and dotlock ove
e49a0 72 72 69 64 65 29 20 66 6f 72 0a 20 20 20 20 2a  rride) for.    *
e49b0 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61 63 74 69  * proxyLock acti
e49c0 76 61 74 69 6f 6e 20 69 73 20 70 6f 73 73 69 62  vation is possib
e49d0 6c 65 20 28 72 65 6d 6f 74 65 20 70 72 6f 78 79  le (remote proxy
e49e0 20 69 73 20 62 61 73 65 64 20 6f 6e 20 64 62 20   is based on db 
e49f0 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20 7a 46 69  name).    ** zFi
e4a00 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e 73 20 76  lename remains v
e4a10 61 6c 69 64 20 75 6e 74 69 6c 20 66 69 6c 65 20  alid until file 
e4a20 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f 20 73 75  is closed, to su
e4a30 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20 70 4e 65  pport */.    pNe
e4a40 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  w->lockingContex
e4a50 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46 69 6c 65  t = (void*)zFile
e4a60 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  name;.#endif.  }
e4a70 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67  ..  if( pLocking
e4a80 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73 69 78 49  Style == &posixI
e4a90 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
e4aa0 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
e4ab0 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
e4ac0 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70  ockInfo(pNew, &p
e4ad0 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65  New->pLock, &pNe
e4ae0 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69  w->pOpen);.    i
e4af0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
e4b00 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
e4b10 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64  an error occured
e4b20 20 69 6e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f   in findLockInfo
e4b30 28 29 2c 20 63 6c 6f 73 65 20 74 68 65 20 66 69  (), close the fi
e4b40 6c 65 20 64 65 73 63 72 69 70 74 6f 72 0a 20 20  le descriptor.  
e4b50 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65      ** immediate
e4b60 6c 79 2c 20 62 65 66 6f 72 65 20 72 65 6c 65 61  ly, before relea
e4b70 73 69 6e 67 20 74 68 65 20 6d 75 74 65 78 2e 20  sing the mutex. 
e4b80 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 20 6d  findLockInfo() m
e4b90 61 79 20 66 61 69 6c 0a 20 20 20 20 20 20 2a 2a  ay fail.      **
e4ba0 20 69 6e 20 74 77 6f 20 73 63 65 6e 61 72 69 6f   in two scenario
e4bb0 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  s:.      **.    
e4bc0 20 20 2a 2a 20 20 20 28 61 29 20 41 20 63 61 6c    **   (a) A cal
e4bd0 6c 20 74 6f 20 66 73 74 61 74 28 29 20 66 61 69  l to fstat() fai
e4be0 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 20 20  led..      **   
e4bf0 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20 66 61 69  (b) A malloc fai
e4c00 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  led..      **.  
e4c10 20 20 20 20 2a 2a 20 53 63 65 6e 61 72 69 6f 20      ** Scenario 
e4c20 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20 6f 63 63  (b) may only occ
e4c30 75 72 20 69 66 20 74 68 65 20 70 72 6f 63 65 73  ur if the proces
e4c40 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 6e 6f 20  s is holding no 
e4c50 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 66  other.      ** f
e4c60 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20  ile descriptors 
e4c70 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65  open on the same
e4c80 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20   file. If there 
e4c90 77 65 72 65 20 6f 74 68 65 72 20 66 69 6c 65 0a  were other file.
e4ca0 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69 70        ** descrip
e4cb0 74 6f 72 73 20 6f 6e 20 74 68 69 73 20 66 69 6c  tors on this fil
e4cc0 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61 6c 6c 6f  e, then no mallo
e4cd0 63 20 77 6f 75 6c 64 20 62 65 20 72 65 71 75 69  c would be requi
e4ce0 72 65 64 20 62 79 0a 20 20 20 20 20 20 2a 2a 20  red by.      ** 
e4cf0 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 29 2e 20  findLockInfo(). 
e4d00 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
e4d10 61 73 65 2c 20 69 74 20 69 73 20 71 75 69 74 65  ase, it is quite
e4d20 20 73 61 66 65 20 74 6f 20 63 6c 6f 73 65 0a 20   safe to close. 
e4d30 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 20 68       ** handle h
e4d40 20 2d 20 61 73 20 69 74 20 69 73 20 67 75 61 72   - as it is guar
e4d50 61 6e 74 65 65 64 20 74 68 61 74 20 6e 6f 20 70  anteed that no p
e4d60 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69 6c 6c 20  osix locks will 
e4d70 62 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20 20  be released.    
e4d80 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67 20 73 6f    ** by doing so
e4d90 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
e4da0 20 2a 2a 20 49 66 20 73 63 65 6e 61 72 69 6f 20   ** If scenario 
e4db0 28 61 29 20 63 61 75 73 65 64 20 74 68 65 20 65  (a) caused the e
e4dc0 72 72 6f 72 20 74 68 65 6e 20 74 68 69 6e 67 73  rror then things
e4dd0 20 61 72 65 20 6e 6f 74 20 73 6f 20 73 61 66 65   are not so safe
e4de0 2e 20 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 69  . The.      ** i
e4df0 6d 70 6c 69 63 69 74 20 61 73 73 75 6d 70 74 69  mplicit assumpti
e4e00 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61 74 20  on here is that 
e4e10 69 66 20 66 73 74 61 74 28 29 20 66 61 69 6c 73  if fstat() fails
e4e20 2c 20 74 68 69 6e 67 73 20 61 72 65 20 69 6e 0a  , things are in.
e4e30 20 20 20 20 20 20 2a 2a 20 73 75 63 68 20 62 61        ** such ba
e4e40 64 20 73 68 61 70 65 20 74 68 61 74 20 64 72 6f  d shape that dro
e4e50 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20 6f 72 20  pping a lock or 
e4e60 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d 61 74 74  two doesn't matt
e4e70 65 72 20 6d 75 63 68 2e 0a 20 20 20 20 20 20 2a  er much..      *
e4e80 2f 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 68 29  /.      close(h)
e4e90 3b 0a 20 20 20 20 20 20 68 20 3d 20 2d 31 3b 0a  ;.      h = -1;.
e4ea0 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65      }.    unixLe
e4eb0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  aveMutex();.  }.
e4ec0 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
e4ed0 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e4ee0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   && defined(__AP
e4ef0 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65 20 69 66  PLE__).  else if
e4f00 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20  ( pLockingStyle 
e4f10 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f 64 73  == &afpIoMethods
e4f20 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46 50 20 6c   ){.    /* AFP l
e4f30 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20  ocking uses the 
e4f40 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20  file path so it 
e4f50 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 63 6c  needs to be incl
e4f60 75 64 65 64 20 69 6e 0a 20 20 20 20 2a 2a 20 74  uded in.    ** t
e4f70 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  he afpLockingCon
e4f80 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  text..    */.   
e4f90 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65   afpLockingConte
e4fa0 78 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 70 4e  xt *pCtx;.    pN
e4fb0 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  ew->lockingConte
e4fc0 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c 69  xt = pCtx = sqli
e4fd0 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65  te3_malloc( size
e4fe0 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20 20  of(*pCtx) );.   
e4ff0 20 69 66 28 20 70 43 74 78 3d 3d 30 20 29 7b 0a   if( pCtx==0 ){.
e5000 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e5010 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c  E_NOMEM;.    }el
e5020 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 42 3a  se{.      /* NB:
e5030 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78 69 73 74   zFilename exist
e5040 73 20 61 6e 64 20 72 65 6d 61 69 6e 73 20 76 61  s and remains va
e5050 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 66 69  lid until the fi
e5060 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a 20 20 20  le is closed.   
e5070 20 20 20 2a 2a 20 61 63 63 6f 72 64 69 6e 67 20     ** according 
e5080 74 6f 20 72 65 71 75 69 72 65 6d 65 6e 74 20 46  to requirement F
e5090 31 31 31 34 31 2e 20 20 53 6f 20 77 65 20 64 6f  11141.  So we do
e50a0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b   not need to mak
e50b0 65 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70  e a.      ** cop
e50c0 79 20 6f 66 20 74 68 65 20 66 69 6c 65 6e 61 6d  y of the filenam
e50d0 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70 43 74 78  e. */.      pCtx
e50e0 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46 69 6c 65  ->dbPath = zFile
e50f0 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73 72 61 6e  name;.      sran
e5100 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20 20 20 20  domdev();.      
e5110 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
e5120 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69 6e  ;.      rc = fin
e5130 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20  dLockInfo(pNew, 
e5140 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e 70 4f 70  NULL, &pNew->pOp
e5150 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e 69 78 4c  en);.      unixL
e5160 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 20  eaveMutex();    
e5170 20 20 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 23      .    }.  }.#
e5180 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65 20 69 66  endif..  else if
e5190 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20  ( pLockingStyle 
e51a0 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74  == &dotlockIoMet
e51b0 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 44  hods ){.    /* D
e51c0 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 75  otfile locking u
e51d0 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61 74  ses the file pat
e51e0 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74 6f  h so it needs to
e51f0 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a   be included in.
e5200 20 20 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f      ** the dotlo
e5210 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ckLockingContext
e5220 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 68 61   .    */.    cha
e5230 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20  r *zLockFile;.  
e5240 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61 6d 65 3b    int nFilename;
e5250 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d 65 20 3d  .    nFilename =
e5260 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 46 69   (int)strlen(zFi
e5270 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20 20  lename) + 6;.   
e5280 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68   zLockFile = (ch
e5290 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ar *)sqlite3_mal
e52a0 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65 29 3b 0a  loc(nFilename);.
e52b0 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b 46 69 6c      if( zLockFil
e52c0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63  e==0 ){.      rc
e52d0 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
e52e0 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
e52f0 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
e5300 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c 20 7a 4c  tf(nFilename, zL
e5310 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22 20 44 4f  ockFile, "%s" DO
e5320 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c 20 7a 46  TLOCK_SUFFIX, zF
e5330 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a  ilename);.    }.
e5340 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e      pNew->lockin
e5350 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c 6f 63 6b  gContext = zLock
e5360 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69 66 20 4f  File;.  }..#if O
e5370 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65 6c 73 65  S_VXWORKS.  else
e5380 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79   if( pLockingSty
e5390 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d 65 74 68  le == &semIoMeth
e53a0 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 61  ods ){.    /* Na
e53b0 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f  med semaphore lo
e53c0 63 6b 69 6e 67 20 75 73 65 73 20 74 68 65 20 66  cking uses the f
e53d0 69 6c 65 20 70 61 74 68 20 73 6f 20 69 74 20 6e  ile path so it n
e53e0 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20 2a  eeds to be.    *
e53f0 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  * included in th
e5400 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43 6f 6e 74  e semLockingCont
e5410 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75  ext.    */.    u
e5420 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
e5430 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f  .    rc = findLo
e5440 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20 26 70 4e  ckInfo(pNew, &pN
e5450 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70 4e 65 77  ew->pLock, &pNew
e5460 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 69 66  ->pOpen);.    if
e5470 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  ( (rc==SQLITE_OK
e5480 29 20 26 26 20 28 70 4e 65 77 2d 3e 70 4f 70 65  ) && (pNew->pOpe
e5490 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c 29 20 29  n->pSem==NULL) )
e54a0 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 53  {.      char *zS
e54b0 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77 2d 3e 70  emName = pNew->p
e54c0 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 3b 0a  Open->aSemName;.
e54d0 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20        int n;.   
e54e0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
e54f0 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ntf(MAX_PATHNAME
e5500 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22 2f 25 73  , zSemName, "/%s
e5510 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20 20 20 20  .sem",.         
e5520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e                pN
e5530 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e 6f 6e 69  ew->pId->zCanoni
e5540 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  calName);.      
e5550 66 6f 72 28 20 6e 3d 31 3b 20 7a 53 65 6d 4e 61  for( n=1; zSemNa
e5560 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a 20 20 20  me[n]; n++ ).   
e5570 20 20 20 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d       if( zSemNam
e5580 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d  e[n]=='/' ) zSem
e5590 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20  Name[n] = '_';. 
e55a0 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e       pNew->pOpen
e55b0 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65  ->pSem = sem_ope
e55c0 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52  n(zSemName, O_CR
e55d0 45 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20  EAT, 0666, 1);. 
e55e0 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e 70       if( pNew->p
e55f0 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53 45  Open->pSem == SE
e5600 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20 20  M_FAILED ){.    
e5610 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
e5620 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 70  NOMEM;.        p
e5630 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d  New->pOpen->aSem
e5640 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a  Name[0] = '\0';.
e5650 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
e5660 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
e5670 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ();.  }.#endif. 
e5680 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73 74 45 72   .  pNew->lastEr
e5690 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20 4f 53 5f  rno = 0;.#if OS_
e56a0 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20 72 63  VXWORKS.  if( rc
e56b0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
e56c0 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e     unlink(zFilen
e56d0 61 6d 65 29 3b 0a 20 20 20 20 69 73 44 65 6c 65  ame);.    isDele
e56e0 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e  te = 0;.  }.  pN
e56f0 65 77 2d 3e 69 73 44 65 6c 65 74 65 20 3d 20 69  ew->isDelete = i
e5700 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64 69 66 0a  sDelete;.#endif.
e5710 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
e5720 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 64  _OK ){.    if( d
e5730 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f 73 65 28  irfd>=0 ) close(
e5740 64 69 72 66 64 29 3b 20 2f 2a 20 73 69 6c 65 6e  dirfd); /* silen
e5750 74 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20  t leak if fail, 
e5760 61 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72  already in error
e5770 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 3e 3d 30   */.    if( h>=0
e5780 20 29 20 63 6c 6f 73 65 28 68 29 3b 0a 20 20 7d   ) close(h);.  }
e5790 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e  else{.    pNew->
e57a0 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f 63 6b 69  pMethod = pLocki
e57b0 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20 4f 70 65  ngStyle;.    Ope
e57c0 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20  nCounter(+1);.  
e57d0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
e57e0 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66  ../*.** Open a f
e57f0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
e5800 6f 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  o the directory 
e5810 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 6c 65 20  containing file 
e5820 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a 20 49 66  zFilename..** If
e5830 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 46   successful, *pF
e5840 64 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  d is set to the 
e5850 6f 70 65 6e 65 64 20 66 69 6c 65 20 64 65 73 63  opened file desc
e5860 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a 20 53 51  riptor and.** SQ
e5870 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
e5880 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ned. If an error
e5890 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72 20   occurs, either 
e58a0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20  SQLITE_NOMEM.** 
e58b0 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  or SQLITE_CANTOP
e58c0 45 4e 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  EN is returned a
e58d0 6e 64 20 2a 70 46 64 20 69 73 20 73 65 74 20 74  nd *pFd is set t
e58e0 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65 64 0a 2a  o an undefined.*
e58f0 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 49  * value..**.** I
e5900 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  f SQLITE_OK is r
e5910 65 74 75 72 6e 65 64 2c 20 74 68 65 20 63 61 6c  eturned, the cal
e5920 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ler is responsib
e5930 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e 67 0a 2a  le for closing.*
e5940 2a 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  * the file descr
e5950 69 70 74 6f 72 20 2a 70 46 64 20 75 73 69 6e 67  iptor *pFd using
e5960 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a 73 74 61   close()..*/.sta
e5970 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 69 72 65  tic int openDire
e5980 63 74 6f 72 79 28 63 6f 6e 73 74 20 63 68 61 72  ctory(const char
e5990 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 69 6e 74   *zFilename, int
e59a0 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74 20 69 69   *pFd){.  int ii
e59b0 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20 2d 31 3b  ;.  int fd = -1;
e59c0 0a 20 20 63 68 61 72 20 7a 44 69 72 6e 61 6d 65  .  char zDirname
e59d0 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d  [MAX_PATHNAME+1]
e59e0 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  ;..  sqlite3_snp
e59f0 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e 41  rintf(MAX_PATHNA
e5a00 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c 20 22 25  ME, zDirname, "%
e5a10 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  s", zFilename);.
e5a20 20 20 66 6f 72 28 69 69 3d 28 69 6e 74 29 73 74    for(ii=(int)st
e5a30 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20  rlen(zDirname); 
e5a40 69 69 3e 31 20 26 26 20 7a 44 69 72 6e 61 6d 65  ii>1 && zDirname
e5a50 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29  [ii]!='/'; ii--)
e5a60 3b 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0a  ;.  if( ii>0 ){.
e5a70 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d      zDirname[ii]
e5a80 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 20   = '\0';.    fd 
e5a90 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c  = open(zDirname,
e5aa0 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41   O_RDONLY|O_BINA
e5ab0 52 59 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  RY, 0);.    if( 
e5ac0 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20  fd>=0 ){.#ifdef 
e5ad0 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 20  FD_CLOEXEC.     
e5ae0 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54   fcntl(fd, F_SET
e5af0 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f  FD, fcntl(fd, F_
e5b00 47 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43  GETFD, 0) | FD_C
e5b10 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a  LOEXEC);.#endif.
e5b20 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22        OSTRACE3("
e5b30 4f 50 45 4e 44 49 52 20 25 2d 33 64 20 25 73 5c  OPENDIR %-3d %s\
e5b40 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d 65  n", fd, zDirname
e5b50 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a  );.    }.  }.  *
e5b60 70 46 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 75  pFd = fd;.  retu
e5b70 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 45  rn (fd>=0?SQLITE
e5b80 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 4f  _OK:SQLITE_CANTO
e5b90 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  PEN);.}../*.** C
e5ba0 72 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72  reate a temporar
e5bb0 79 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a  y file name in z
e5bc0 42 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20  Buf.  zBuf must 
e5bd0 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20  be allocated.** 
e5be0 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70  by the calling p
e5bf0 72 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 74 20  rocess and must 
e5c00 62 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f  be big enough to
e5c10 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a 2a   hold at least.*
e5c20 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  * pVfs->mxPathna
e5c30 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  me bytes..*/.sta
e5c40 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e  tic int getTempn
e5c50 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68  ame(int nBuf, ch
e5c60 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61  ar *zBuf){.  sta
e5c70 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
e5c80 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20  azDirs[] = {.   
e5c90 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a 20 20 20    0,.     0,.   
e5ca0 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c 0a 20 20    "/var/tmp",.  
e5cb0 20 20 20 22 2f 75 73 72 2f 74 6d 70 22 2c 0a 20     "/usr/tmp",. 
e5cc0 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20 20 20 20      "/tmp",.    
e5cd0 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20 73 74 61   ".",.  };.  sta
e5ce0 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
e5cf0 65 64 20 63 68 61 72 20 7a 43 68 61 72 73 5b 5d  ed char zChars[]
e5d00 20 3d 0a 20 20 20 20 22 61 62 63 64 65 66 67 68   =.    "abcdefgh
e5d10 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78  ijklmnopqrstuvwx
e5d20 79 7a 22 0a 20 20 20 20 22 41 42 43 44 45 46 47  yz".    "ABCDEFG
e5d30 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57  HIJKLMNOPQRSTUVW
e5d40 58 59 5a 22 0a 20 20 20 20 22 30 31 32 33 34 35  XYZ".    "012345
e5d50 36 37 38 39 22 3b 0a 20 20 75 6e 73 69 67 6e 65  6789";.  unsigne
e5d60 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74  d int i, j;.  st
e5d70 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20  ruct stat buf;. 
e5d80 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69   const char *zDi
e5d90 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 49  r = ".";..  /* I
e5da0 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c  t's odd to simul
e5db0 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20  ate an io-error 
e5dc0 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79  here, but really
e5dd0 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20   this is just.  
e5de0 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d  ** using the io-
e5df0 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63  error infrastruc
e5e00 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68 61  ture to test tha
e5e10 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73  t SQLite handles
e5e20 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74   this.  ** funct
e5e30 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20  ion failing. .  
e5e40 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  */.  SimulateIOE
e5e50 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
e5e60 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20  ITE_IOERR );..  
e5e70 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c 69  azDirs[0] = sqli
e5e80 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
e5e90 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c 4c 20 3d  ry;.  if (NULL =
e5ea0 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20 7b 0a 20  = azDirs[1]) {. 
e5eb0 20 20 20 61 7a 44 69 72 73 5b 31 5d 20 3d 20 67     azDirs[1] = g
e5ec0 65 74 65 6e 76 28 22 54 4d 50 44 49 52 22 29 3b  etenv("TMPDIR");
e5ed0 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72 28 69 3d  .  }.  .  for(i=
e5ee0 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 7a 44 69  0; i<sizeof(azDi
e5ef0 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a 44 69 72  rs)/sizeof(azDir
e5f00 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  s[0]); i++){.   
e5f10 20 69 66 28 20 61 7a 44 69 72 73 5b 69 5d 3d 3d   if( azDirs[i]==
e5f20 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
e5f30 20 20 69 66 28 20 73 74 61 74 28 61 7a 44 69 72    if( stat(azDir
e5f40 73 5b 69 5d 2c 20 26 62 75 66 29 20 29 20 63 6f  s[i], &buf) ) co
e5f50 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20  ntinue;.    if( 
e5f60 21 53 5f 49 53 44 49 52 28 62 75 66 2e 73 74 5f  !S_ISDIR(buf.st_
e5f70 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69 6e 75 65  mode) ) continue
e5f80 3b 0a 20 20 20 20 69 66 28 20 61 63 63 65 73 73  ;.    if( access
e5f90 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30 37 29 20  (azDirs[i], 07) 
e5fa0 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
e5fb0 7a 44 69 72 20 3d 20 61 7a 44 69 72 73 5b 69 5d  zDir = azDirs[i]
e5fc0 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  ;.    break;.  }
e5fd0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
e5fe0 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  t the output buf
e5ff0 66 65 72 20 69 73 20 6c 61 72 67 65 20 65 6e 6f  fer is large eno
e6000 75 67 68 20 66 6f 72 20 74 68 65 20 74 65 6d 70  ugh for the temp
e6010 6f 72 61 72 79 20 66 69 6c 65 20 0a 20 20 2a 2a  orary file .  **
e6020 20 6e 61 6d 65 2e 20 49 66 20 69 74 20 69 73 20   name. If it is 
e6030 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  not, return SQLI
e6040 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a 2f 0a 20  TE_ERROR..  */. 
e6050 20 69 66 28 20 28 73 74 72 6c 65 6e 28 7a 44 69   if( (strlen(zDi
e6060 72 29 20 2b 20 73 74 72 6c 65 6e 28 53 51 4c 49  r) + strlen(SQLI
e6070 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45  TE_TEMP_FILE_PRE
e6080 46 49 58 29 20 2b 20 31 37 29 20 3e 3d 20 28 73  FIX) + 17) >= (s
e6090 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b 0a 20 20  ize_t)nBuf ){.  
e60a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e60b0 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 64 6f  ERROR;.  }..  do
e60c0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
e60d0 70 72 69 6e 74 66 28 6e 42 75 66 2d 31 37 2c 20  printf(nBuf-17, 
e60e0 7a 42 75 66 2c 20 22 25 73 2f 22 53 51 4c 49 54  zBuf, "%s/"SQLIT
e60f0 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
e6100 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20 20 20 6a  IX, zDir);.    j
e6110 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a   = (int)strlen(z
e6120 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Buf);.    sqlite
e6130 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 31 35 2c  3_randomness(15,
e6140 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 20 20   &zBuf[j]);.    
e6150 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35 3b 20 69  for(i=0; i<15; i
e6160 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ++, j++){.      
e6170 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29  zBuf[j] = (char)
e6180 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69 67 6e  zChars[ ((unsign
e6190 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29  ed char)zBuf[j])
e61a0 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72 73 29  %(sizeof(zChars)
e61b0 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  -1) ];.    }.   
e61c0 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20   zBuf[j] = 0;.  
e61d0 7d 77 68 69 6c 65 28 20 61 63 63 65 73 73 28 7a  }while( access(z
e61e0 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a 20 20 72  Buf,0)==0 );.  r
e61f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e6200 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  .}..#if SQLITE_E
e6210 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e6220 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  YLE && defined(_
e6230 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a 2a 2a 20  _APPLE__)./*.** 
e6240 52 6f 75 74 69 6e 65 20 74 6f 20 74 72 61 6e 73  Routine to trans
e6250 66 6f 72 6d 20 61 20 75 6e 69 78 46 69 6c 65 20  form a unixFile 
e6260 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d 6c 6f 63  into a proxy-loc
e6270 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65 2e 0a 2a  king unixFile..*
e6280 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
e6290 20 69 6e 20 74 68 65 20 70 72 6f 78 79 2d 6c 6f   in the proxy-lo
e62a0 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20 62 75 74  ck division, but
e62b0 20 75 73 65 64 20 62 79 20 75 6e 69 78 4f 70 65   used by unixOpe
e62c0 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c 49 54 45  n().** if SQLITE
e62d0 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f  _PREFER_PROXY_LO
e62e0 43 4b 49 4e 47 20 69 73 20 64 65 66 69 6e 65 64  CKING is defined
e62f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e6300 70 72 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e  proxyTransformUn
e6310 69 78 46 69 6c 65 28 75 6e 69 78 46 69 6c 65 2a  ixFile(unixFile*
e6320 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  , const char*);.
e6330 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65  #endif../*.** Se
e6340 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73  arch for an unus
e6350 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ed file descript
e6360 6f 72 20 74 68 61 74 20 77 61 73 20 6f 70 65 6e  or that was open
e6370 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ed on the databa
e6380 73 65 20 0a 2a 2a 20 66 69 6c 65 20 28 6e 6f 74  se .** file (not
e6390 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 6d 61   a journal or ma
e63a0 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster-journal fil
e63b0 65 29 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  e) identified by
e63c0 20 70 61 74 68 6e 61 6d 65 0a 2a 2a 20 7a 50 61   pathname.** zPa
e63d0 74 68 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f  th with SQLITE_O
e63e0 50 45 4e 5f 58 58 58 20 66 6c 61 67 73 20 6d 61  PEN_XXX flags ma
e63f0 74 63 68 69 6e 67 20 74 68 6f 73 65 20 70 61 73  tching those pas
e6400 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
e6410 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  d.** argument to
e6420 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
e6430 2a 2a 0a 2a 2a 20 53 75 63 68 20 61 20 66 69 6c  **.** Such a fil
e6440 65 20 64 65 73 63 72 69 70 74 6f 72 20 6d 61 79  e descriptor may
e6450 20 65 78 69 73 74 20 69 66 20 61 20 64 61 74 61   exist if a data
e6460 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
e6470 77 61 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 62 75  was closed.** bu
e6480 74 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  t the associated
e6490 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
e64a0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6c   could not be cl
e64b0 6f 73 65 64 20 62 65 63 61 75 73 65 20 73 6f 6d  osed because som
e64c0 65 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 20  e.** other file 
e64d0 64 65 73 63 72 69 70 74 6f 72 20 6f 70 65 6e 20  descriptor open 
e64e0 6f 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65  on the same file
e64f0 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 66 69   is holding a fi
e6500 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52 65 66 65  le-lock..** Refe
e6510 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73 20 69 6e  r to comments in
e6520 20 74 68 65 20 75 6e 69 78 43 6c 6f 73 65 28 29   the unixClose()
e6530 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64 20 74 68   function and th
e6540 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d 6d 65 6e  e lengthy commen
e6550 74 0a 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20  t.** describing 
e6560 22 50 6f 73 69 78 20 41 64 76 69 73 6f 72 79 20  "Posix Advisory 
e6570 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74 68 65 20  Locking" at the 
e6580 73 74 61 72 74 20 6f 66 20 74 68 69 73 20 66 69  start of this fi
e6590 6c 65 20 66 6f 72 20 0a 2a 2a 20 66 75 72 74 68  le for .** furth
e65a0 65 72 20 64 65 74 61 69 6c 73 2e 20 41 6c 73 6f  er details. Also
e65b0 2c 20 74 69 63 6b 65 74 20 23 34 30 31 38 2e 0a  , ticket #4018..
e65c0 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 75 69 74 61  **.** If a suita
e65d0 62 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  ble file descrip
e65e0 74 6f 72 20 69 73 20 66 6f 75 6e 64 2c 20 74 68  tor is found, th
e65f0 65 6e 20 69 74 20 69 73 20 72 65 74 75 72 6e 65  en it is returne
e6600 64 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73 75 63 68  d. If no.** such
e6610 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
e6620 20 69 73 20 6c 6f 63 61 74 65 64 2c 20 2d 31 20   is located, -1 
e6630 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
e6640 73 74 61 74 69 63 20 55 6e 69 78 55 6e 75 73 65  static UnixUnuse
e6650 64 46 64 20 2a 66 69 6e 64 52 65 75 73 61 62 6c  dFd *findReusabl
e6660 65 46 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  eFd(const char *
e6670 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c 61 67 73  zPath, int flags
e6680 29 7b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46  ){.  UnixUnusedF
e6690 64 20 2a 70 55 6e 75 73 65 64 20 3d 20 30 3b 0a  d *pUnused = 0;.
e66a0 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 65 61  .  /* Do not sea
e66b0 72 63 68 20 66 6f 72 20 61 6e 20 75 6e 75 73 65  rch for an unuse
e66c0 64 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  d file descripto
e66d0 72 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 4e 6f  r on vxworks. No
e66e0 74 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 76  t because.  ** v
e66f0 78 77 6f 72 6b 73 20 77 6f 75 6c 64 20 6e 6f 74  xworks would not
e6700 20 62 65 6e 65 66 69 74 20 66 72 6f 6d 20 74 68   benefit from th
e6710 65 20 63 68 61 6e 67 65 20 28 69 74 20 6d 69 67  e change (it mig
e6720 68 74 2c 20 77 65 27 72 65 20 6e 6f 74 20 73 75  ht, we're not su
e6730 72 65 29 2c 0a 20 20 2a 2a 20 62 75 74 20 62 65  re),.  ** but be
e6740 63 61 75 73 65 20 6e 6f 20 77 61 79 20 74 6f 20  cause no way to 
e6750 74 65 73 74 20 69 74 20 69 73 20 63 75 72 72 65  test it is curre
e6760 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c 65 2e 20  ntly available. 
e6770 49 74 20 69 73 20 62 65 74 74 65 72 20 0a 20 20  It is better .  
e6780 2a 2a 20 6e 6f 74 20 74 6f 20 72 69 73 6b 20 62  ** not to risk b
e6790 72 65 61 6b 69 6e 67 20 76 78 77 6f 72 6b 73 20  reaking vxworks 
e67a0 73 75 70 70 6f 72 74 20 66 6f 72 20 74 68 65 20  support for the 
e67b0 73 61 6b 65 20 6f 66 20 73 75 63 68 20 61 6e 20  sake of such an 
e67c0 6f 62 73 63 75 72 65 20 0a 20 20 2a 2a 20 66 65  obscure .  ** fe
e67d0 61 74 75 72 65 2e 20 20 2a 2f 0a 23 69 66 20 21  ature.  */.#if !
e67e0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72  OS_VXWORKS.  str
e67f0 75 63 74 20 73 74 61 74 20 73 53 74 61 74 3b 20  uct stat sStat; 
e6800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6810 20 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20    /* Results of 
e6820 73 74 61 74 28 29 20 63 61 6c 6c 20 2a 2f 0a 0a  stat() call */..
e6830 20 20 2f 2a 20 41 20 73 74 61 74 28 29 20 63 61    /* A stat() ca
e6840 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66 6f 72 20  ll may fail for 
e6850 76 61 72 69 6f 75 73 20 72 65 61 73 6f 6e 73 2e  various reasons.
e6860 20 49 66 20 74 68 69 73 20 68 61 70 70 65 6e 73   If this happens
e6870 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 61 6c 6d  , it is.  ** alm
e6880 6f 73 74 20 63 65 72 74 61 69 6e 20 74 68 61 74  ost certain that
e6890 20 61 6e 20 6f 70 65 6e 28 29 20 63 61 6c 6c 20   an open() call 
e68a0 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74 68  on the same path
e68b0 20 77 69 6c 6c 20 61 6c 73 6f 20 66 61 69 6c 2e   will also fail.
e68c0 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69 73 20 72  .  ** For this r
e68d0 65 61 73 6f 6e 2c 20 69 66 20 61 6e 20 65 72 72  eason, if an err
e68e0 6f 72 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65  or occurs in the
e68f0 20 73 74 61 74 28 29 20 63 61 6c 6c 20 68 65 72   stat() call her
e6900 65 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20 69 67  e, it is.  ** ig
e6910 6e 6f 72 65 64 20 61 6e 64 20 2d 31 20 69 73 20  nored and -1 is 
e6920 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 63 61  returned. The ca
e6930 6c 6c 65 72 20 77 69 6c 6c 20 74 72 79 20 74 6f  ller will try to
e6940 20 6f 70 65 6e 20 61 20 6e 65 77 20 66 69 6c 65   open a new file
e6950 0a 20 20 2a 2a 20 64 65 73 63 72 69 70 74 6f 72  .  ** descriptor
e6960 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70 61 74   on the same pat
e6970 68 2c 20 66 61 69 6c 2c 20 61 6e 64 20 72 65 74  h, fail, and ret
e6980 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 74 6f 20  urn an error to 
e6990 53 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a 20 20 2a  SQLite..  **.  *
e69a0 2a 20 45 76 65 6e 20 69 66 20 61 20 73 75 62 73  * Even if a subs
e69b0 65 71 75 65 6e 74 20 6f 70 65 6e 28 29 20 63 61  equent open() ca
e69c0 6c 6c 20 64 6f 65 73 20 73 75 63 63 65 65 64 2c  ll does succeed,
e69d0 20 74 68 65 20 63 6f 6e 73 65 71 75 65 6e 63 65   the consequence
e69e0 73 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74 20 73 65  s of.  ** not se
e69f0 61 72 63 68 69 6e 67 20 66 6f 72 20 61 20 72 65  arching for a re
e6a00 73 75 73 61 62 6c 65 20 66 69 6c 65 20 64 65 73  susable file des
e6a10 63 72 69 70 74 6f 72 20 61 72 65 20 6e 6f 74 20  criptor are not 
e6a20 64 69 72 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20  dire.  */.  if( 
e6a30 30 3d 3d 73 74 61 74 28 7a 50 61 74 68 2c 20 26  0==stat(zPath, &
e6a40 73 53 74 61 74 29 20 29 7b 0a 20 20 20 20 73 74  sStat) ){.    st
e6a50 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74  ruct unixOpenCnt
e6a60 20 2a 70 4f 3b 0a 20 20 20 20 73 74 72 75 63 74   *pO;.    struct
e6a70 20 75 6e 69 78 46 69 6c 65 49 64 20 69 64 3b 0a   unixFileId id;.
e6a80 20 20 20 20 69 64 2e 64 65 76 20 3d 20 73 53 74      id.dev = sSt
e6a90 61 74 2e 73 74 5f 64 65 76 3b 0a 20 20 20 20 69  at.st_dev;.    i
e6aa0 64 2e 69 6e 6f 20 3d 20 73 53 74 61 74 2e 73 74  d.ino = sStat.st
e6ab0 5f 69 6e 6f 3b 0a 0a 20 20 20 20 75 6e 69 78 45  _ino;..    unixE
e6ac0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
e6ad0 20 66 6f 72 28 70 4f 3d 6f 70 65 6e 4c 69 73 74   for(pO=openList
e6ae0 3b 20 70 4f 20 26 26 20 6d 65 6d 63 6d 70 28 26  ; pO && memcmp(&
e6af0 69 64 2c 20 26 70 4f 2d 3e 66 69 6c 65 49 64 2c  id, &pO->fileId,
e6b00 20 73 69 7a 65 6f 66 28 69 64 29 29 3b 20 70 4f   sizeof(id)); pO
e6b10 3d 70 4f 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 20  =pO->pNext);.   
e6b20 20 69 66 28 20 70 4f 20 29 7b 0a 20 20 20 20 20   if( pO ){.     
e6b30 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 2a   UnixUnusedFd **
e6b40 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 70  pp;.      for(pp
e6b50 3d 26 70 4f 2d 3e 70 55 6e 75 73 65 64 3b 20 2a  =&pO->pUnused; *
e6b60 70 70 20 26 26 20 28 2a 70 70 29 2d 3e 66 6c 61  pp && (*pp)->fla
e6b70 67 73 21 3d 66 6c 61 67 73 3b 20 70 70 3d 26 28  gs!=flags; pp=&(
e6b80 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29 29 3b 0a  (*pp)->pNext));.
e6b90 20 20 20 20 20 20 70 55 6e 75 73 65 64 20 3d 20        pUnused = 
e6ba0 2a 70 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70  *pp;.      if( p
e6bb0 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 20 20  Unused ){.      
e6bc0 20 20 2a 70 70 20 3d 20 70 55 6e 75 73 65 64 2d    *pp = pUnused-
e6bd0 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a  >pNext;.      }.
e6be0 20 20 20 20 7d 0a 20 20 20 20 75 6e 69 78 4c 65      }.    unixLe
e6bf0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  aveMutex();.  }.
e6c00 23 65 6e 64 69 66 20 20 20 20 2f 2a 20 69 66 20  #endif    /* if 
e6c10 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 20  !OS_VXWORKS */. 
e6c20 20 72 65 74 75 72 6e 20 70 55 6e 75 73 65 64 3b   return pUnused;
e6c30 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74  .}../*.** Open t
e6c40 68 65 20 66 69 6c 65 20 7a 50 61 74 68 2e 0a 2a  he file zPath..*
e6c50 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75 73 6c 79  * .** Previously
e6c60 2c 20 74 68 65 20 53 51 4c 69 74 65 20 4f 53 20  , the SQLite OS 
e6c70 6c 61 79 65 72 20 75 73 65 64 20 74 68 72 65 65  layer used three
e6c80 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 70 6c   functions in pl
e6c90 61 63 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f  ace of this.** o
e6ca0 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73 71  ne:.**.**     sq
e6cb0 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64 57  lite3OsOpenReadW
e6cc0 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20 20 20 73  rite();.**     s
e6cd0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65 61 64  qlite3OsOpenRead
e6ce0 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20 20 20 73  Only();.**     s
e6cf0 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c  qlite3OsOpenExcl
e6d00 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a 2a 20 54  usive();.**.** T
e6d10 68 65 73 65 20 63 61 6c 6c 73 20 63 6f 72 72 65  hese calls corre
e6d20 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 66 6f 6c  spond to the fol
e6d30 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69  lowing combinati
e6d40 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a  ons of flags:.**
e6d50 0a 2a 2a 20 20 20 20 20 52 65 61 64 57 72 69 74  .**     ReadWrit
e6d60 65 28 29 20 2d 3e 20 20 20 20 20 28 52 45 41 44  e() ->     (READ
e6d70 57 52 49 54 45 20 7c 20 43 52 45 41 54 45 29 0a  WRITE | CREATE).
e6d80 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e 6c 79 28  **     ReadOnly(
e6d90 29 20 20 2d 3e 20 20 20 20 20 28 52 45 41 44 4f  )  ->     (READO
e6da0 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20 4f 70 65  NLY) .**     Ope
e6db0 6e 45 78 63 6c 75 73 69 76 65 28 29 20 2d 3e 20  nExclusive() -> 
e6dc0 28 52 45 41 44 57 52 49 54 45 20 7c 20 43 52 45  (READWRITE | CRE
e6dd0 41 54 45 20 7c 20 45 58 43 4c 55 53 49 56 45 29  ATE | EXCLUSIVE)
e6de0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64 20 4f  .**.** The old O
e6df0 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20 61  penExclusive() a
e6e00 63 63 65 70 74 65 64 20 61 20 62 6f 6f 6c 65 61  ccepted a boolea
e6e10 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20 22 64 65  n argument - "de
e6e20 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a 20 74 72  lFlag". If.** tr
e6e30 75 65 2c 20 74 68 65 20 66 69 6c 65 20 77 61 73  ue, the file was
e6e40 20 63 6f 6e 66 69 67 75 72 65 64 20 74 6f 20 62   configured to b
e6e50 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  e automatically 
e6e60 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65  deleted when the
e6e70 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64 6c 65 20  .** file handle 
e6e80 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63 68 69 65  closed. To achie
e6e90 76 65 20 74 68 65 20 73 61 6d 65 20 65 66 66 65  ve the same effe
e6ea0 63 74 20 75 73 69 6e 67 20 74 68 69 73 20 6e 65  ct using this ne
e6eb0 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2c  w .** interface,
e6ec0 20 61 64 64 20 74 68 65 20 44 45 4c 45 54 45 4f   add the DELETEO
e6ed0 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74 6f 20 74  NCLOSE flag to t
e6ee0 68 6f 73 65 20 73 70 65 63 69 66 69 65 64 20 61  hose specified a
e6ef0 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20 4f 70 65  bove for .** Ope
e6f00 6e 45 78 63 6c 75 73 69 76 65 28 29 2e 0a 2a 2f  nExclusive()..*/
e6f10 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e6f20 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f  Open(.  sqlite3_
e6f30 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20  vfs *pVfs,      
e6f40 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20       /* The VFS 
e6f50 66 6f 72 20 77 68 69 63 68 20 74 68 69 73 20 69  for which this i
e6f60 73 20 74 68 65 20 78 4f 70 65 6e 20 6d 65 74 68  s the xOpen meth
e6f70 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  od */.  const ch
e6f80 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20 20 20  ar *zPath,      
e6f90 20 20 20 20 20 2f 2a 20 50 61 74 68 6e 61 6d 65       /* Pathname
e6fa0 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 6f   of file to be o
e6fb0 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  pened */.  sqlit
e6fc0 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20  e3_file *pFile, 
e6fd0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
e6fe0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
e6ff0 6f 20 62 65 20 66 69 6c 6c 65 64 20 69 6e 20 2a  o be filled in *
e7000 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
e7010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7020 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61 67 73 20   /* Input flags 
e7030 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6f  to control the o
e7040 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  pening */.  int 
e7050 2a 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20  *pOutFlags      
e7060 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
e7070 75 74 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65  ut flags returne
e7080 64 20 74 6f 20 53 51 4c 69 74 65 20 63 6f 72 65  d to SQLite core
e7090 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78 46 69 6c   */.){.  unixFil
e70a0 65 20 2a 70 20 3d 20 28 75 6e 69 78 46 69 6c 65  e *p = (unixFile
e70b0 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69 6e 74 20   *)pFile;.  int 
e70c0 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20  fd = -1;        
e70d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
e70e0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 72 65  le descriptor re
e70f0 74 75 72 6e 65 64 20 62 79 20 6f 70 65 6e 28 29  turned by open()
e7100 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 20   */.  int dirfd 
e7110 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20  = -1;           
e7120 20 20 20 20 20 2f 2a 20 44 69 72 65 63 74 6f 72       /* Director
e7130 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  y file descripto
e7140 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65 6e 46  r */.  int openF
e7150 6c 61 67 73 20 3d 20 30 3b 20 20 20 20 20 20 20  lags = 0;       
e7160 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74        /* Flags t
e7170 6f 20 70 61 73 73 20 74 6f 20 6f 70 65 6e 28 29  o pass to open()
e7180 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70 65 20   */.  int eType 
e7190 3d 20 66 6c 61 67 73 26 30 78 46 46 46 46 46 46  = flags&0xFFFFFF
e71a0 30 30 3b 20 20 2f 2a 20 54 79 70 65 20 6f 66 20  00;  /* Type of 
e71b0 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  file to open */.
e71c0 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b 20 20 20    int noLock;   
e71d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e71e0 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74   /* True to omit
e71f0 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74 69   locking primiti
e7200 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ves */.  int rc 
e7210 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
e7220 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 74          /* Funct
e7230 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f 64 65 20  ion Return Code 
e7240 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45 78 63 6c  */..  int isExcl
e7250 75 73 69 76 65 20 20 3d 20 28 66 6c 61 67 73 20  usive  = (flags 
e7260 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58  & SQLITE_OPEN_EX
e7270 43 4c 55 53 49 56 45 29 3b 0a 20 20 69 6e 74 20  CLUSIVE);.  int 
e7280 69 73 44 65 6c 65 74 65 20 20 20 20 20 3d 20 28  isDelete     = (
e7290 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
e72a0 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
e72b0 45 29 3b 0a 20 20 69 6e 74 20 69 73 43 72 65 61  E);.  int isCrea
e72c0 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73 20  te     = (flags 
e72d0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  & SQLITE_OPEN_CR
e72e0 45 41 54 45 29 3b 0a 20 20 69 6e 74 20 69 73 52  EATE);.  int isR
e72f0 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28 66 6c 61  eadonly   = (fla
e7300 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
e7310 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 69 6e  _READONLY);.  in
e7320 74 20 69 73 52 65 61 64 57 72 69 74 65 20 20 3d  t isReadWrite  =
e7330 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45   (flags & SQLITE
e7340 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29  _OPEN_READWRITE)
e7350 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72 65 61 74  ;..  /* If creat
e7360 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6f 72 20  ing a master or 
e7370 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75 72 6e 61  main-file journa
e7380 6c 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  l, this function
e7390 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20 2a 2a 20   will open.  ** 
e73a0 61 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f  a file-descripto
e73b0 72 20 6f 6e 20 74 68 65 20 64 69 72 65 63 74 6f  r on the directo
e73c0 72 79 20 74 6f 6f 2e 20 54 68 65 20 66 69 72 73  ry too. The firs
e73d0 74 20 74 69 6d 65 20 75 6e 69 78 53 79 6e 63 28  t time unixSync(
e73e0 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c 6c 65 64  ).  ** is called
e73f0 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 66   the directory f
e7400 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 77  ile descriptor w
e7410 69 6c 6c 20 62 65 20 66 73 79 6e 63 28 29 65 64  ill be fsync()ed
e7420 20 61 6e 64 20 63 6c 6f 73 65 28 29 64 2e 0a 20   and close()d.. 
e7430 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 70 65 6e   */.  int isOpen
e7440 44 69 72 65 63 74 6f 72 79 20 3d 20 28 69 73 43  Directory = (isC
e7450 72 65 61 74 65 20 26 26 20 0a 20 20 20 20 20 20  reate && .      
e7460 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f  (eType==SQLITE_O
e7470 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e  PEN_MASTER_JOURN
e7480 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c  AL || eType==SQL
e7490 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f  ITE_OPEN_MAIN_JO
e74a0 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a 20 20 2f  URNAL).  );..  /
e74b0 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 20 7a 50  * If argument zP
e74c0 61 74 68 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f  ath is a NULL po
e74d0 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63  inter, this func
e74e0 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64  tion is required
e74f0 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20   to open.  ** a 
e7500 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20  temporary file. 
e7510 55 73 65 20 74 68 69 73 20 62 75 66 66 65 72 20  Use this buffer 
e7520 74 6f 20 73 74 6f 72 65 20 74 68 65 20 66 69 6c  to store the fil
e7530 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20 2a 2f 0a  e name in..  */.
e7540 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b    char zTmpname[
e7550 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b  MAX_PATHNAME+1];
e7560 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
e7570 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b 0a 0a 20  Name = zPath;.. 
e7580 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20 66 6f   /* Check the fo
e7590 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e  llowing statemen
e75a0 74 73 20 61 72 65 20 74 72 75 65 3a 20 0a 20 20  ts are true: .  
e75b0 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29 20 45 78  **.  **   (a) Ex
e75c0 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20 74 68 65  actly one of the
e75d0 20 52 45 41 44 57 52 49 54 45 20 61 6e 64 20 52   READWRITE and R
e75e0 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73 20 6d 75  EADONLY flags mu
e75f0 73 74 20 62 65 20 73 65 74 2c 20 61 6e 64 20 0a  st be set, and .
e7600 20 20 2a 2a 20 20 20 28 62 29 20 69 66 20 43 52    **   (b) if CR
e7610 45 41 54 45 20 69 73 20 73 65 74 2c 20 74 68 65  EATE is set, the
e7620 6e 20 52 45 41 44 57 52 49 54 45 20 6d 75 73 74  n READWRITE must
e7630 20 61 6c 73 6f 20 62 65 20 73 65 74 2c 20 61 6e   also be set, an
e7640 64 0a 20 20 2a 2a 20 20 20 28 63 29 20 69 66 20  d.  **   (c) if 
e7650 45 58 43 4c 55 53 49 56 45 20 69 73 20 73 65 74  EXCLUSIVE is set
e7660 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20 6d 75  , then CREATE mu
e7670 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a  st also be set..
e7680 20 20 2a 2a 20 20 20 28 64 29 20 69 66 20 44 45    **   (d) if DE
e7690 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69 73 20 73  LETEONCLOSE is s
e76a0 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45 20  et, then CREATE 
e76b0 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74  must also be set
e76c0 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
e76d0 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d 30 20 7c  (isReadonly==0 |
e76e0 7c 20 69 73 52 65 61 64 57 72 69 74 65 3d 3d 30  | isReadWrite==0
e76f0 29 20 26 26 20 28 69 73 52 65 61 64 57 72 69 74  ) && (isReadWrit
e7700 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e 6c 79 29  e || isReadonly)
e7710 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 43 72  );.  assert(isCr
e7720 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73 52 65 61  eate==0 || isRea
e7730 64 57 72 69 74 65 29 3b 0a 20 20 61 73 73 65 72  dWrite);.  asser
e7740 74 28 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30  t(isExclusive==0
e7750 20 7c 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 20   || isCreate);. 
e7760 20 61 73 73 65 72 74 28 69 73 44 65 6c 65 74 65   assert(isDelete
e7770 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29  ==0 || isCreate)
e7780 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e  ;..  /* The main
e7790 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   DB, main journa
e77a0 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72 20 6a 6f  l, and master jo
e77b0 75 72 6e 61 6c 20 61 72 65 20 6e 65 76 65 72 20  urnal are never 
e77c0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 20 20  automatically.  
e77d0 2a 2a 20 64 65 6c 65 74 65 64 2e 20 4e 6f 72 20  ** deleted. Nor 
e77e0 61 72 65 20 74 68 65 79 20 65 76 65 72 20 74 65  are they ever te
e77f0 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 20 20  mporary files.  
e7800 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 21 69  */.  assert( (!i
e7810 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65  sDelete && zName
e7820 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49  ) || eType!=SQLI
e7830 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20  TE_OPEN_MAIN_DB 
e7840 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 21 69  );.  assert( (!i
e7850 73 44 65 6c 65 74 65 20 26 26 20 7a 4e 61 6d 65  sDelete && zName
e7860 29 20 7c 7c 20 65 54 79 70 65 21 3d 53 51 4c 49  ) || eType!=SQLI
e7870 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55  TE_OPEN_MAIN_JOU
e7880 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73 65 72 74  RNAL );.  assert
e7890 28 20 28 21 69 73 44 65 6c 65 74 65 20 26 26 20  ( (!isDelete && 
e78a0 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21  zName) || eType!
e78b0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  =SQLITE_OPEN_MAS
e78c0 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29 3b 0a 0a  TER_JOURNAL );..
e78d0 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74    /* Assert that
e78e0 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72   the upper layer
e78f0 20 68 61 73 20 73 65 74 20 6f 6e 65 20 6f 66 20   has set one of 
e7900 74 68 65 20 22 66 69 6c 65 2d 74 79 70 65 22 20  the "file-type" 
e7910 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61 73 73 65  flags. */.  asse
e7920 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  rt( eType==SQLIT
e7930 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20  E_OPEN_MAIN_DB  
e7940 20 20 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51      || eType==SQ
e7950 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44  LITE_OPEN_TEMP_D
e7960 42 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79  B .       || eTy
e7970 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe==SQLITE_OPEN_
e7980 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20  MAIN_JOURNAL || 
e7990 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
e79a0 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20  EN_TEMP_JOURNAL 
e79b0 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65  .       || eType
e79c0 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55  ==SQLITE_OPEN_SU
e79d0 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c 20 65 54  BJOURNAL   || eT
e79e0 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
e79f0 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20  _MASTER_JOURNAL 
e7a00 0a 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65  .       || eType
e7a10 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52  ==SQLITE_OPEN_TR
e7a20 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20 29 3b 0a  ANSIENT_DB.  );.
e7a30 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20  .  memset(p, 0, 
e7a40 73 69 7a 65 6f 66 28 75 6e 69 78 46 69 6c 65 29  sizeof(unixFile)
e7a50 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d  );..  if( eType=
e7a60 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  =SQLITE_OPEN_MAI
e7a70 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55 6e 69 78  N_DB ){.    Unix
e7a80 55 6e 75 73 65 64 46 64 20 2a 70 55 6e 75 73 65  UnusedFd *pUnuse
e7a90 64 3b 0a 20 20 20 20 70 55 6e 75 73 65 64 20 3d  d;.    pUnused =
e7aa0 20 66 69 6e 64 52 65 75 73 61 62 6c 65 46 64 28   findReusableFd(
e7ab0 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29 3b 0a 20  zName, flags);. 
e7ac0 20 20 20 69 66 28 20 70 55 6e 75 73 65 64 20 29     if( pUnused )
e7ad0 7b 0a 20 20 20 20 20 20 66 64 20 3d 20 70 55 6e  {.      fd = pUn
e7ae0 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20 20 7d 65  used->fd;.    }e
e7af0 6c 73 65 7b 0a 20 20 20 20 20 20 70 55 6e 75 73  lse{.      pUnus
e7b00 65 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ed = sqlite3_mal
e7b10 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70 55 6e 75  loc(sizeof(*pUnu
e7b20 73 65 64 29 29 3b 0a 20 20 20 20 20 20 69 66 28  sed));.      if(
e7b30 20 21 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20   !pUnused ){.   
e7b40 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
e7b50 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
e7b60 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70  }.    }.    p->p
e7b70 55 6e 75 73 65 64 20 3d 20 70 55 6e 75 73 65 64  Unused = pUnused
e7b80 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 7a  ;.  }else if( !z
e7b90 4e 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Name ){.    /* I
e7ba0 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55 4c 4c 2c  f zName is NULL,
e7bb0 20 74 68 65 20 75 70 70 65 72 20 6c 61 79 65 72   the upper layer
e7bc0 20 69 73 20 72 65 71 75 65 73 74 69 6e 67 20 61   is requesting a
e7bd0 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a 2f 0a 20   temp file. */. 
e7be0 20 20 20 61 73 73 65 72 74 28 69 73 44 65 6c 65     assert(isDele
e7bf0 74 65 20 26 26 20 21 69 73 4f 70 65 6e 44 69 72  te && !isOpenDir
e7c00 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 72 63 20  ectory);.    rc 
e7c10 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41  = getTempname(MA
e7c20 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c 20 7a 54  X_PATHNAME+1, zT
e7c30 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28  mpname);.    if(
e7c40 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
e7c50 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
e7c60 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 4e 61  c;.    }.    zNa
e7c70 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20  me = zTmpname;. 
e7c80 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69   }..  /* Determi
e7c90 6e 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ne the value of 
e7ca0 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
e7cb0 74 65 72 20 70 61 73 73 65 64 20 74 6f 20 50 4f  ter passed to PO
e7cc0 53 49 58 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a  SIX function.  *
e7cd0 2a 20 6f 70 65 6e 28 29 2e 20 54 68 65 73 65 20  * open(). These 
e7ce0 6d 75 73 74 20 62 65 20 63 61 6c 63 75 6c 61 74  must be calculat
e7cf0 65 64 20 65 76 65 6e 20 69 66 20 6f 70 65 6e 28  ed even if open(
e7d00 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2c  ) is not called,
e7d10 20 61 73 0a 20 20 2a 2a 20 74 68 65 79 20 6d 61   as.  ** they ma
e7d20 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20 70  y be stored as p
e7d30 61 72 74 20 6f 66 20 74 68 65 20 66 69 6c 65 20  art of the file 
e7d40 68 61 6e 64 6c 65 20 61 6e 64 20 75 73 65 64 20  handle and used 
e7d50 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 27 63 6f  by the .  ** 'co
e7d60 6e 63 68 20 66 69 6c 65 27 20 6c 6f 63 6b 69 6e  nch file' lockin
e7d70 67 20 66 75 6e 63 74 69 6f 6e 73 20 6c 61 74 65  g functions late
e7d80 72 20 6f 6e 2e 20 20 2a 2f 0a 20 20 69 66 28 20  r on.  */.  if( 
e7d90 69 73 52 65 61 64 6f 6e 6c 79 20 29 20 20 6f 70  isReadonly )  op
e7da0 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f  enFlags |= O_RDO
e7db0 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73 52 65 61  NLY;.  if( isRea
e7dc0 64 57 72 69 74 65 20 29 20 6f 70 65 6e 46 6c 61  dWrite ) openFla
e7dd0 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 20  gs |= O_RDWR;.  
e7de0 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20 20  if( isCreate )  
e7df0 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f    openFlags |= O
e7e00 5f 43 52 45 41 54 3b 0a 20 20 69 66 28 20 69 73  _CREAT;.  if( is
e7e10 45 78 63 6c 75 73 69 76 65 20 29 20 6f 70 65 6e  Exclusive ) open
e7e20 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45 58 43 4c  Flags |= (O_EXCL
e7e30 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b 0a 20 20  |O_NOFOLLOW);.  
e7e40 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f  openFlags |= (O_
e7e50 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42 49 4e 41  LARGEFILE|O_BINA
e7e60 52 59 29 3b 0a 0a 20 20 69 66 28 20 66 64 3c 30  RY);..  if( fd<0
e7e70 20 29 7b 0a 20 20 20 20 6d 6f 64 65 5f 74 20 6f   ){.    mode_t o
e7e80 70 65 6e 4d 6f 64 65 20 3d 20 28 69 73 44 65 6c  penMode = (isDel
e7e90 65 74 65 3f 30 36 30 30 3a 53 51 4c 49 54 45 5f  ete?0600:SQLITE_
e7ea0 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52  DEFAULT_FILE_PER
e7eb0 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20 66  MISSIONS);.    f
e7ec0 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20  d = open(zName, 
e7ed0 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70 65 6e 4d  openFlags, openM
e7ee0 6f 64 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  ode);.    OSTRAC
e7ef0 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33 64  E4("OPENX   %-3d
e7f00 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c 20   %s 0%o\n", fd, 
e7f10 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67 73  zName, openFlags
e7f20 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20  );.    if( fd<0 
e7f30 26 26 20 65 72 72 6e 6f 21 3d 45 49 53 44 49 52  && errno!=EISDIR
e7f40 20 26 26 20 69 73 52 65 61 64 57 72 69 74 65 20   && isReadWrite 
e7f50 26 26 20 21 69 73 45 78 63 6c 75 73 69 76 65 20  && !isExclusive 
e7f60 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46 61 69 6c  ){.      /* Fail
e7f70 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 66  ed to open the f
e7f80 69 6c 65 20 66 6f 72 20 72 65 61 64 2f 77 72 69  ile for read/wri
e7f90 74 65 20 61 63 63 65 73 73 2e 20 54 72 79 20 72  te access. Try r
e7fa0 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a 20 20 20  ead-only. */.   
e7fb0 20 20 20 66 6c 61 67 73 20 26 3d 20 7e 28 53 51     flags &= ~(SQ
e7fc0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
e7fd0 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ITE|SQLITE_OPEN_
e7fe0 43 52 45 41 54 45 29 3b 0a 20 20 20 20 20 20 6f  CREATE);.      o
e7ff0 70 65 6e 46 6c 61 67 73 20 26 3d 20 7e 28 4f 5f  penFlags &= ~(O_
e8000 52 44 57 52 7c 4f 5f 43 52 45 41 54 29 3b 0a 20  RDWR|O_CREAT);. 
e8010 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 51       flags |= SQ
e8020 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
e8030 4c 59 3b 0a 20 20 20 20 20 20 6f 70 65 6e 46 6c  LY;.      openFl
e8040 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e 4c 59 3b  ags |= O_RDONLY;
e8050 0a 20 20 20 20 20 20 66 64 20 3d 20 6f 70 65 6e  .      fd = open
e8060 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c 61 67  (zName, openFlag
e8070 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b 0a 20 20  s, openMode);.  
e8080 20 20 7d 0a 20 20 20 20 69 66 28 20 66 64 3c 30    }.    if( fd<0
e8090 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
e80a0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
e80b0 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f        goto open_
e80c0 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a  finished;.    }.
e80d0 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 66 64    }.  assert( fd
e80e0 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 75  >=0 );.  if( pOu
e80f0 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a 70  tFlags ){.    *p
e8100 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67 73  OutFlags = flags
e8110 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e  ;.  }..  if( p->
e8120 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20 20 70  pUnused ){.    p
e8130 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 64 20 3d 20  ->pUnused->fd = 
e8140 66 64 3b 0a 20 20 20 20 70 2d 3e 70 55 6e 75 73  fd;.    p->pUnus
e8150 65 64 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67  ed->flags = flag
e8160 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73  s;.  }..  if( is
e8170 44 65 6c 65 74 65 20 29 7b 0a 23 69 66 20 4f 53  Delete ){.#if OS
e8180 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 7a 50 61  _VXWORKS.    zPa
e8190 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23 65 6c 73  th = zName;.#els
e81a0 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 4e 61  e.    unlink(zNa
e81b0 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  me);.#endif.  }.
e81c0 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
e81d0 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a  E_LOCKING_STYLE.
e81e0 20 20 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 6f    else{.    p->o
e81f0 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70 65 6e 46  penFlags = openF
e8200 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  lags;.  }.#endif
e8210 0a 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 44 69  ..  if( isOpenDi
e8220 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 72  rectory ){.    r
e8230 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74 6f 72  c = openDirector
e8240 79 28 7a 50 61 74 68 2c 20 26 64 69 72 66 64 29  y(zPath, &dirfd)
e8250 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
e8260 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
e8270 20 2f 2a 20 49 74 20 69 73 20 73 61 66 65 20 74   /* It is safe t
e8280 6f 20 63 6c 6f 73 65 20 66 64 20 61 74 20 74 68  o close fd at th
e8290 69 73 20 70 6f 69 6e 74 2c 20 62 65 63 61 75 73  is point, becaus
e82a0 65 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65  e it is guarante
e82b0 65 64 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20  ed not.      ** 
e82c0 74 6f 20 62 65 20 6f 70 65 6e 20 6f 6e 20 61 20  to be open on a 
e82d0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
e82e0 66 20 69 74 20 77 65 72 65 20 6f 70 65 6e 20 6f  f it were open o
e82f0 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c  n a database fil
e8300 65 2c 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 77  e,.      ** it w
e8310 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61 66 65  ould not be safe
e8320 20 74 6f 20 63 6c 6f 73 65 20 61 73 20 74 68 69   to close as thi
e8330 73 20 77 6f 75 6c 64 20 72 65 6c 65 61 73 65 20  s would release 
e8340 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c 64 0a 20  any locks held. 
e8350 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 66       ** on the f
e8360 69 6c 65 20 62 79 20 74 68 69 73 20 70 72 6f 63  ile by this proc
e8370 65 73 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 61  ess.  */.      a
e8380 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 53 51  ssert( eType!=SQ
e8390 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
e83a0 42 20 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65  B );.      close
e83b0 28 66 64 29 3b 20 20 20 20 20 20 20 20 20 20 20  (fd);           
e83c0 20 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65    /* silently le
e83d0 61 6b 20 69 66 20 66 61 69 6c 2c 20 61 6c 72 65  ak if fail, alre
e83e0 61 64 79 20 69 6e 20 65 72 72 6f 72 20 2a 2f 0a  ady in error */.
e83f0 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f        goto open_
e8400 66 69 6e 69 73 68 65 64 3b 0a 20 20 20 20 7d 0a  finished;.    }.
e8410 20 20 7d 0a 0a 23 69 66 64 65 66 20 46 44 5f 43    }..#ifdef FD_C
e8420 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74 6c 28 66  LOEXEC.  fcntl(f
e8430 64 2c 20 46 5f 53 45 54 46 44 2c 20 66 63 6e 74  d, F_SETFD, fcnt
e8440 6c 28 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 30  l(fd, F_GETFD, 0
e8450 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b  ) | FD_CLOEXEC);
e8460 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f 63  .#endif..  noLoc
e8470 6b 20 3d 20 65 54 79 70 65 21 3d 53 51 4c 49 54  k = eType!=SQLIT
e8480 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a  E_OPEN_MAIN_DB;.
e8490 0a 23 69 66 20 53 51 4c 49 54 45 5f 50 52 45 46  .#if SQLITE_PREF
e84a0 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e 47  ER_PROXY_LOCKING
e84b0 0a 20 20 69 66 28 20 7a 50 61 74 68 21 3d 4e 55  .  if( zPath!=NU
e84c0 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b 20 26 26  LL && !noLock &&
e84d0 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20 29 7b 0a   pVfs->xOpen ){.
e84e0 20 20 20 20 63 68 61 72 20 2a 65 6e 76 66 6f 72      char *envfor
e84f0 63 65 20 3d 20 67 65 74 65 6e 76 28 22 53 51 4c  ce = getenv("SQL
e8500 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f  ITE_FORCE_PROXY_
e8510 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20 20 20 69  LOCKING");.    i
e8520 6e 74 20 75 73 65 50 72 6f 78 79 20 3d 20 30 3b  nt useProxy = 0;
e8530 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ..    /* SQLITE_
e8540 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b  FORCE_PROXY_LOCK
e8550 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20 66 6f 72  ING==1 means for
e8560 63 65 20 61 6c 77 61 79 73 20 75 73 65 20 70 72  ce always use pr
e8570 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20 0a 20 20  oxy, 0 means .  
e8580 20 20 2a 2a 20 6e 65 76 65 72 20 75 73 65 20 70    ** never use p
e8590 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65 61 6e 73  roxy, NULL means
e85a0 20 75 73 65 20 70 72 6f 78 79 20 66 6f 72 20 6e   use proxy for n
e85b0 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 73 20 6f  on-local files o
e85c0 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28  nly.  */.    if(
e85d0 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55 4c 4c 20   envforce!=NULL 
e85e0 29 7b 0a 20 20 20 20 20 20 75 73 65 50 72 6f 78  ){.      useProx
e85f0 79 20 3d 20 61 74 6f 69 28 65 6e 76 66 6f 72 63  y = atoi(envforc
e8600 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  e)>0;.    }else{
e8610 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 73 74  .      struct st
e8620 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a 20 20 20  atfs fsInfo;.   
e8630 20 20 20 69 66 28 20 73 74 61 74 66 73 28 7a 50     if( statfs(zP
e8640 61 74 68 2c 20 26 66 73 49 6e 66 6f 29 20 3d 3d  ath, &fsInfo) ==
e8650 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f   -1 ){.        /
e8660 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 74 68 65  * In theory, the
e8670 20 63 6c 6f 73 65 28 66 64 29 20 63 61 6c 6c 20   close(fd) call 
e8680 69 73 20 73 75 62 2d 6f 70 74 69 6d 61 6c 2e 20  is sub-optimal. 
e8690 49 66 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e  If the file open
e86a0 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 69  ed.        ** wi
e86b0 74 68 20 66 64 20 69 73 20 61 20 64 61 74 61 62  th fd is a datab
e86c0 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 20 74 68  ase file, and th
e86d0 65 72 65 20 61 72 65 20 6f 74 68 65 72 20 63 6f  ere are other co
e86e0 6e 6e 65 63 74 69 6f 6e 73 20 6f 70 65 6e 0a 20  nnections open. 
e86f0 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68 61         ** on tha
e8700 74 20 66 69 6c 65 20 74 68 61 74 20 61 72 65 20  t file that are 
e8710 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c 64 69 6e  currently holdin
e8720 67 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 73  g advisory locks
e8730 20 6f 6e 20 69 74 2c 0a 20 20 20 20 20 20 20 20   on it,.        
e8740 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 61 6c 6c  ** then the call
e8750 20 74 6f 20 63 6c 6f 73 65 28 29 20 77 69 6c 6c   to close() will
e8760 20 63 61 6e 63 65 6c 20 74 68 6f 73 65 20 6c 6f   cancel those lo
e8770 63 6b 73 2e 20 49 6e 20 70 72 61 63 74 69 63 65  cks. In practice
e8780 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 77 65 27  ,.        ** we'
e8790 72 65 20 61 73 73 75 6d 69 6e 67 20 74 68 61 74  re assuming that
e87a0 20 73 74 61 74 66 73 28 29 20 64 6f 65 73 6e 27   statfs() doesn'
e87b0 74 20 66 61 69 6c 20 76 65 72 79 20 6f 66 74 65  t fail very ofte
e87c0 6e 2e 20 41 74 20 6c 65 61 73 74 0a 20 20 20 20  n. At least.    
e87d0 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68 69 6c 65      ** not while
e87e0 20 6f 74 68 65 72 20 66 69 6c 65 20 64 65 73 63   other file desc
e87f0 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 62  riptors opened b
e8800 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  y the same proce
e8810 73 73 20 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a  ss on.        **
e8820 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 61   the same file a
e8830 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20 2a 2f 0a  re working.  */.
e8840 20 20 20 20 20 20 20 20 70 2d 3e 6c 61 73 74 45          p->lastE
e8850 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
e8860 20 20 20 20 20 20 69 66 28 20 64 69 72 66 64 3e        if( dirfd>
e8870 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
e8880 63 6c 6f 73 65 28 64 69 72 66 64 29 3b 20 2f 2a  close(dirfd); /*
e8890 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 69   silently leak i
e88a0 66 20 66 61 69 6c 2c 20 69 6e 20 65 72 72 6f 72  f fail, in error
e88b0 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   */.        }.  
e88c0 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b        close(fd);
e88d0 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61   /* silently lea
e88e0 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72  k if fail, in er
e88f0 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  ror */.        r
e8900 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
e8910 5f 41 43 43 45 53 53 3b 0a 20 20 20 20 20 20 20  _ACCESS;.       
e8920 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73   goto open_finis
e8930 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  hed;.      }.   
e8940 20 20 20 75 73 65 50 72 6f 78 79 20 3d 20 21 28     useProxy = !(
e8950 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67 73 26 4d  fsInfo.f_flags&M
e8960 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20 20 20 7d  NT_LOCAL);.    }
e8970 0a 20 20 20 20 69 66 28 20 75 73 65 50 72 6f 78  .    if( useProx
e8980 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  y ){.      rc = 
e8990 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70  fillInUnixFile(p
e89a0 56 66 73 2c 20 66 64 2c 20 64 69 72 66 64 2c 20  Vfs, fd, dirfd, 
e89b0 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f  pFile, zPath, no
e89c0 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b  Lock, isDelete);
e89d0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
e89e0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
e89f0 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 54 72      rc = proxyTr
e8a00 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65 28  ansformUnixFile(
e8a10 28 75 6e 69 78 46 69 6c 65 2a 29 70 46 69 6c 65  (unixFile*)pFile
e8a20 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a 20 20 20  , ":auto:");.   
e8a30 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20     }.      goto 
e8a40 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20  open_finished;. 
e8a50 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
e8a60 20 20 0a 20 20 72 63 20 3d 20 66 69 6c 6c 49 6e    .  rc = fillIn
e8a70 55 6e 69 78 46 69 6c 65 28 70 56 66 73 2c 20 66  UnixFile(pVfs, f
e8a80 64 2c 20 64 69 72 66 64 2c 20 70 46 69 6c 65 2c  d, dirfd, pFile,
e8a90 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20   zPath, noLock, 
e8aa0 69 73 44 65 6c 65 74 65 29 3b 0a 6f 70 65 6e 5f  isDelete);.open_
e8ab0 66 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28 20  finished:.  if( 
e8ac0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
e8ad0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
e8ae0 65 28 70 2d 3e 70 55 6e 75 73 65 64 29 3b 0a 20  e(p->pUnused);. 
e8af0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
e8b00 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  }.../*.** Delete
e8b10 20 74 68 65 20 66 69 6c 65 20 61 74 20 7a 50 61   the file at zPa
e8b20 74 68 2e 20 49 66 20 74 68 65 20 64 69 72 53 79  th. If the dirSy
e8b30 6e 63 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nc argument is t
e8b40 72 75 65 2c 20 66 73 79 6e 63 28 29 0a 2a 2a 20  rue, fsync().** 
e8b50 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 61 66  the directory af
e8b60 74 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65  ter deleting the
e8b70 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
e8b80 20 69 6e 74 20 75 6e 69 78 44 65 6c 65 74 65 28   int unixDelete(
e8b90 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
e8ba0 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 2f 2a 20  NotUsed,     /* 
e8bb0 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  VFS containing t
e8bc0 68 69 73 20 61 73 20 74 68 65 20 78 44 65 6c 65  his as the xDele
e8bd0 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 63  te method */.  c
e8be0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68  onst char *zPath
e8bf0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ,        /* Name
e8c00 20 6f 66 20 66 69 6c 65 20 74 6f 20 62 65 20 64   of file to be d
e8c10 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  eleted */.  int 
e8c20 64 69 72 53 79 6e 63 20 20 20 20 20 20 20 20 20  dirSync         
e8c30 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65        /* If true
e8c40 2c 20 66 73 79 6e 63 28 29 20 64 69 72 65 63 74  , fsync() direct
e8c50 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74 69  ory after deleti
e8c60 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20  ng file */.){.  
e8c70 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
e8c80 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  OK;.  UNUSED_PAR
e8c90 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e8ca0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
e8cb0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45  or(return SQLITE
e8cc0 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a  _IOERR_DELETE);.
e8cd0 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3b    unlink(zPath);
e8ce0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
e8cf0 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a  DISABLE_DIRSYNC.
e8d00 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29 7b    if( dirSync ){
e8d10 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 20  .    int fd;.   
e8d20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74   rc = openDirect
e8d30 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b  ory(zPath, &fd);
e8d40 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
e8d50 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 4f 53  ITE_OK ){.#if OS
e8d60 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 69  _VXWORKS.      i
e8d70 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d 31  f( fsync(fd)==-1
e8d80 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 69   ).#else.      i
e8d90 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a 23  f( fsync(fd) ).#
e8da0 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20  endif.      {.  
e8db0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e8dc0 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e  E_IOERR_DIR_FSYN
e8dd0 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  C;.      }.     
e8de0 20 69 66 28 20 63 6c 6f 73 65 28 66 64 29 26 26   if( close(fd)&&
e8df0 21 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72  !rc ){.        r
e8e00 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  c = SQLITE_IOERR
e8e10 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20  _DIR_CLOSE;.    
e8e20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65    }.    }.  }.#e
e8e30 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63  ndif.  return rc
e8e40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20  ;.}../*.** Test 
e8e50 74 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66  the existance of
e8e60 20 6f 72 20 61 63 63 65 73 73 20 70 65 72 6d 69   or access permi
e8e70 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c 65 20 7a  ssions of file z
e8e80 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20 74 65 73  Path. The.** tes
e8e90 74 20 70 65 72 66 6f 72 6d 65 64 20 64 65 70 65  t performed depe
e8ea0 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  nds on the value
e8eb0 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a   of flags:.**.**
e8ec0 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43 45       SQLITE_ACCE
e8ed0 53 53 5f 45 58 49 53 54 53 3a 20 52 65 74 75 72  SS_EXISTS: Retur
e8ee0 6e 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20  n 1 if the file 
e8ef0 65 78 69 73 74 73 0a 2a 2a 20 20 20 20 20 53 51  exists.**     SQ
e8f00 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
e8f10 57 52 49 54 45 3a 20 52 65 74 75 72 6e 20 31 20  WRITE: Return 1 
e8f20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 72  if the file is r
e8f30 65 61 64 20 61 6e 64 20 77 72 69 74 61 62 6c 65  ead and writable
e8f40 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f  ..**     SQLITE_
e8f50 41 43 43 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3a  ACCESS_READONLY:
e8f60 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
e8f70 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62 6c   file is readabl
e8f80 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  e..**.** Otherwi
e8f90 73 65 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a  se return 0..*/.
e8fa0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 41  static int unixA
e8fb0 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33  ccess(.  sqlite3
e8fc0 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20  _vfs *NotUsed,  
e8fd0 20 2f 2a 20 54 68 65 20 56 46 53 20 63 6f 6e 74   /* The VFS cont
e8fe0 61 69 6e 69 6e 67 20 74 68 69 73 20 78 41 63 63  aining this xAcc
e8ff0 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ess method */.  
e9000 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
e9010 68 2c 20 20 20 20 20 20 2f 2a 20 50 61 74 68 20  h,      /* Path 
e9020 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f 20 65  of the file to e
e9030 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  xamine */.  int 
e9040 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
e9050 20 20 20 20 2f 2a 20 57 68 61 74 20 64 6f 20 77      /* What do w
e9060 65 20 77 61 6e 74 20 74 6f 20 6c 65 61 72 6e 20  e want to learn 
e9070 61 62 6f 75 74 20 74 68 65 20 7a 50 61 74 68 20  about the zPath 
e9080 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e 74 20 2a  file? */.  int *
e9090 70 52 65 73 4f 75 74 20 20 20 20 20 20 20 20 20  pResOut         
e90a0 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75     /* Write resu
e90b0 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65 72 65 20  lt boolean here 
e90c0 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 6d 6f 64  */.){.  int amod
e90d0 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f  e = 0;.  UNUSED_
e90e0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e90f0 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  d);.  SimulateIO
e9100 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51  Error( return SQ
e9110 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53  LITE_IOERR_ACCES
e9120 53 3b 20 29 3b 0a 20 20 73 77 69 74 63 68 28 20  S; );.  switch( 
e9130 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61 73  flags ){.    cas
e9140 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
e9150 45 58 49 53 54 53 3a 0a 20 20 20 20 20 20 61 6d  EXISTS:.      am
e9160 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20 20 20 20  ode = F_OK;.    
e9170 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
e9180 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
e9190 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20 20 20  READWRITE:.     
e91a0 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b 7c 52 5f   amode = W_OK|R_
e91b0 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OK;.      break;
e91c0 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
e91d0 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20  _ACCESS_READ:.  
e91e0 20 20 20 20 61 6d 6f 64 65 20 3d 20 52 5f 4f 4b      amode = R_OK
e91f0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a  ;.      break;..
e9200 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
e9210 20 20 20 61 73 73 65 72 74 28 21 22 49 6e 76 61     assert(!"Inva
e9220 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d 65  lid flags argume
e9230 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70 52 65  nt");.  }.  *pRe
e9240 73 4f 75 74 20 3d 20 28 61 63 63 65 73 73 28 7a  sOut = (access(z
e9250 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d 3d 30 29  Path, amode)==0)
e9260 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
e9270 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  E_OK;.}.../*.** 
e9280 54 75 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20  Turn a relative 
e9290 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20  pathname into a 
e92a0 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 54  full pathname. T
e92b0 68 65 20 72 65 6c 61 74 69 76 65 20 70 61 74 68  he relative path
e92c0 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 20 61 73  .** is stored as
e92d0 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65   a nul-terminate
e92e0 64 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20  d string in the 
e92f0 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74  buffer pointed t
e9300 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68 2e 20 0a  o by.** zPath. .
e9310 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f 69 6e 74  **.** zOut point
e9320 73 20 74 6f 20 61 20 62 75 66 66 65 72 20 6f 66  s to a buffer of
e9330 20 61 74 20 6c 65 61 73 74 20 73 71 6c 69 74 65   at least sqlite
e9340 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65  3_vfs.mxPathname
e9350 20 62 79 74 65 73 20 0a 2a 2a 20 28 69 6e 20 74   bytes .** (in t
e9360 68 69 73 20 63 61 73 65 2c 20 4d 41 58 5f 50 41  his case, MAX_PA
e9370 54 48 4e 41 4d 45 20 62 79 74 65 73 29 2e 20 54  THNAME bytes). T
e9380 68 65 20 66 75 6c 6c 2d 70 61 74 68 20 69 73 20  he full-path is 
e9390 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 74 68  written to.** th
e93a0 69 73 20 62 75 66 66 65 72 20 62 65 66 6f 72 65  is buffer before
e93b0 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73   returning..*/.s
e93c0 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 75  tatic int unixFu
e93d0 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71  llPathname(.  sq
e93e0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
e93f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
e9400 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62  ointer to vfs ob
e9410 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ject */.  const 
e9420 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20 20  char *zPath,    
e9430 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69          /* Possi
e9440 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70  bly relative inp
e9450 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74  ut path */.  int
e9460 20 6e 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20   nOut,          
e9470 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
e9480 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66  ze of output buf
e9490 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  fer in bytes */.
e94a0 20 20 63 68 61 72 20 2a 7a 4f 75 74 20 20 20 20    char *zOut    
e94b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e94c0 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66 65 72  /* Output buffer
e94d0 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20 49 74 27   */.){..  /* It'
e94e0 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74  s odd to simulat
e94f0 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65  e an io-error he
e9500 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74  re, but really t
e9510 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a  his is just.  **
e9520 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72   using the io-er
e9530 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75  ror infrastructu
e9540 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20  re to test that 
e9550 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74  SQLite handles t
e9560 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  his.  ** functio
e9570 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68 69 73 20  n failing. This 
e9580 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c 64 20 66  function could f
e9590 61 69 6c 20 69 66 2c 20 66 6f 72 20 65 78 61 6d  ail if, for exam
e95a0 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a 20 63 75  ple, the.  ** cu
e95b0 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67 20 64 69  rrent working di
e95c0 72 65 63 74 6f 72 79 20 68 61 73 20 62 65 65 6e  rectory has been
e95d0 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20 2a 2f 0a   unlinked..  */.
e95e0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
e95f0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
e9600 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20 61 73 73  _ERROR );..  ass
e9610 65 72 74 28 20 70 56 66 73 2d 3e 6d 78 50 61 74  ert( pVfs->mxPat
e9620 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41 54 48 4e  hname==MAX_PATHN
e9630 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f  AME );.  UNUSED_
e9640 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b  PARAMETER(pVfs);
e9650 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74 2d 31 5d  ..  zOut[nOut-1]
e9660 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 7a   = '\0';.  if( z
e9670 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20 29 7b 0a  Path[0]=='/' ){.
e9680 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
e9690 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f 75 74 2c  intf(nOut, zOut,
e96a0 20 22 25 73 22 2c 20 7a 50 61 74 68 29 3b 0a 20   "%s", zPath);. 
e96b0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20   }else{.    int 
e96c0 6e 43 77 64 3b 0a 20 20 20 20 69 66 28 20 67 65  nCwd;.    if( ge
e96d0 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f 75 74 2d  tcwd(zOut, nOut-
e96e0 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  1)==0 ){.      r
e96f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e  eturn SQLITE_CAN
e9700 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20  TOPEN;.    }.   
e9710 20 6e 43 77 64 20 3d 20 28 69 6e 74 29 73 74 72   nCwd = (int)str
e9720 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73  len(zOut);.    s
e9730 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
e9740 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74  nOut-nCwd, &zOut
e9750 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a  [nCwd], "/%s", z
e9760 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Path);.  }.  ret
e9770 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
e9780 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
e9790 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
e97a0 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  NSION./*.** Inte
e97b0 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69  rfaces for openi
e97c0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72  ng a shared libr
e97d0 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74  ary, finding ent
e97e0 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74  ry points.** wit
e97f0 68 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c  hin the shared l
e9800 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73  ibrary, and clos
e9810 69 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c  ing the shared l
e9820 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 6e 63 6c  ibrary..*/.#incl
e9830 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e 0a 73 74  ude <dlfcn.h>.st
e9840 61 74 69 63 20 76 6f 69 64 20 2a 75 6e 69 78 44  atic void *unixD
e9850 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  lOpen(sqlite3_vf
e9860 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e 73  s *NotUsed, cons
e9870 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
e9880 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  e){.  UNUSED_PAR
e9890 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e98a0 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f 70 65 6e  .  return dlopen
e98b0 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52 54 4c 44  (zFilename, RTLD
e98c0 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47 4c 4f 42  _NOW | RTLD_GLOB
e98d0 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51  AL);.}../*.** SQ
e98e0 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20  Lite calls this 
e98f0 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65 64 69 61  function immedia
e9900 74 65 6c 79 20 61 66 74 65 72 20 61 20 63 61 6c  tely after a cal
e9910 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79 6d 28 29  l to unixDlSym()
e9920 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c 4f 70 65   or.** unixDlOpe
e9930 6e 28 29 20 66 61 69 6c 73 20 28 72 65 74 75 72  n() fails (retur
e9940 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65  ns a null pointe
e9950 72 29 2e 20 49 66 20 61 20 6d 6f 72 65 20 64 65  r). If a more de
e9960 74 61 69 6c 65 64 20 65 72 72 6f 72 0a 2a 2a 20  tailed error.** 
e9970 6d 65 73 73 61 67 65 20 69 73 20 61 76 61 69 6c  message is avail
e9980 61 62 6c 65 2c 20 69 74 20 69 73 20 77 72 69 74  able, it is writ
e9990 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75 74 2e 20  ten to zBufOut. 
e99a0 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73  If no error mess
e99b0 61 67 65 0a 2a 2a 20 69 73 20 61 76 61 69 6c 61  age.** is availa
e99c0 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20 69 73 20  ble, zBufOut is 
e99d0 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69 65 64 20  left unmodified 
e99e0 61 6e 64 20 53 51 4c 69 74 65 20 75 73 65 73 20  and SQLite uses 
e99f0 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20 65 72 72  a default.** err
e9a00 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 73  or message..*/.s
e9a10 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44  tatic void unixD
e9a20 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76  lError(sqlite3_v
e9a30 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74  fs *NotUsed, int
e9a40 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
e9a50 66 4f 75 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a  fOut){.  char *z
e9a60 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  Err;.  UNUSED_PA
e9a70 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e9a80 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  ;.  unixEnterMut
e9a90 65 78 28 29 3b 0a 20 20 7a 45 72 72 20 3d 20 64  ex();.  zErr = d
e9aa0 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69 66 28 20  lerror();.  if( 
e9ab0 7a 45 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  zErr ){.    sqli
e9ac0 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75  te3_snprintf(nBu
e9ad0 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22 25 73 22  f, zBufOut, "%s"
e9ae0 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 20 20 75  , zErr);.  }.  u
e9af0 6e 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  nixLeaveMutex();
e9b00 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 28  .}.static void (
e9b10 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c 69 74  *unixDlSym(sqlit
e9b20 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
e9b30 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e 73 74 20   void *p, const 
e9b40 63 68 61 72 2a 7a 53 79 6d 29 29 28 76 6f 69 64  char*zSym))(void
e9b50 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 47 43  ){.  /* .  ** GC
e9b60 43 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63  C with -pedantic
e9b70 2d 65 72 72 6f 72 73 20 73 61 79 73 20 74 68 61  -errors says tha
e9b80 74 20 43 39 30 20 64 6f 65 73 20 6e 6f 74 20 61  t C90 does not a
e9b90 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20 74 6f 20  llow a void* to 
e9ba0 62 65 0a 20 20 2a 2a 20 63 61 73 74 20 69 6e 74  be.  ** cast int
e9bb0 6f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  o a pointer to a
e9bc0 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 6e 64 20   function.  And 
e9bd0 79 65 74 20 74 68 65 20 6c 69 62 72 61 72 79 20  yet the library 
e9be0 64 6c 73 79 6d 28 29 20 72 6f 75 74 69 6e 65 0a  dlsym() routine.
e9bf0 20 20 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76    ** returns a v
e9c00 6f 69 64 2a 20 77 68 69 63 68 20 69 73 20 72 65  oid* which is re
e9c10 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74  ally a pointer t
e9c20 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53  o a function.  S
e9c30 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20 20 2a 2a  o how do we.  **
e9c40 20 75 73 65 20 64 6c 73 79 6d 28 29 20 77 69 74   use dlsym() wit
e9c50 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72 6f  h -pedantic-erro
e9c60 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 56 61  rs?.  **.  ** Va
e9c70 72 69 61 62 6c 65 20 78 20 62 65 6c 6f 77 20 69  riable x below i
e9c80 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62 65 20  s defined to be 
e9c90 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
e9ca0 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67 0a 20  unction taking. 
e9cb0 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 76   ** parameters v
e9cc0 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73 74 20 63  oid* and const c
e9cd0 68 61 72 2a 20 61 6e 64 20 72 65 74 75 72 6e 69  har* and returni
e9ce0 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ng a pointer to 
e9cf0 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2a  a function..  **
e9d00 20 57 65 20 69 6e 69 74 69 61 6c 69 7a 65 20 78   We initialize x
e9d10 20 62 79 20 61 73 73 69 67 6e 69 6e 67 20 69 74   by assigning it
e9d20 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
e9d30 65 20 64 6c 73 79 6d 28 29 20 66 75 6e 63 74 69  e dlsym() functi
e9d40 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61 74 20 61  on..  ** (That a
e9d50 73 73 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72  ssignment requir
e9d60 65 73 20 61 20 63 61 73 74 2e 29 20 20 54 68 65  es a cast.)  The
e9d70 6e 20 77 65 20 63 61 6c 6c 20 74 68 65 20 66 75  n we call the fu
e9d80 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20 20 2a 2a  nction that.  **
e9d90 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20 0a   x points to.  .
e9da0 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 77    **.  ** This w
e9db0 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73 20 75 6e  ork-around is un
e9dc0 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72 6b 20 63  likely to work c
e9dd0 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61 6e 79 20  orrectly on any 
e9de0 73 79 73 74 65 6d 20 77 68 65 72 65 0a 20 20 2a  system where.  *
e9df0 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20 63 61 6e  * you really can
e9e00 6e 6f 74 20 63 61 73 74 20 61 20 66 75 6e 63 74  not cast a funct
e9e10 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f  ion pointer into
e9e20 20 76 6f 69 64 2a 2e 20 20 42 75 74 20 74 68 65   void*.  But the
e9e30 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f  n, on the.  ** o
e9e40 74 68 65 72 20 68 61 6e 64 2c 20 64 6c 73 79 6d  ther hand, dlsym
e9e50 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b  () will not work
e9e60 20 6f 6e 20 73 75 63 68 20 61 20 73 79 73 74 65   on such a syste
e9e70 6d 20 65 69 74 68 65 72 2c 20 73 6f 20 77 65 20  m either, so we 
e9e80 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74 20 72 65  have.  ** not re
e9e90 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79 74 68 69  ally lost anythi
e9ea0 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f 69 64 20  ng..  */.  void 
e9eb0 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c 63 6f 6e  (*(*x)(void*,con
e9ec0 73 74 20 63 68 61 72 2a 29 29 28 76 6f 69 64 29  st char*))(void)
e9ed0 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
e9ee0 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
e9ef0 20 78 20 3d 20 28 76 6f 69 64 28 2a 28 2a 29 28   x = (void(*(*)(
e9f00 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
e9f10 2a 29 29 28 76 6f 69 64 29 29 64 6c 73 79 6d 3b  *))(void))dlsym;
e9f20 0a 20 20 72 65 74 75 72 6e 20 28 2a 78 29 28 70  .  return (*x)(p
e9f30 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74 61 74 69  , zSym);.}.stati
e9f40 63 20 76 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f  c void unixDlClo
e9f50 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  se(sqlite3_vfs *
e9f60 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64 20 2a 70  NotUsed, void *p
e9f70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45  Handle){.  UNUSE
e9f80 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
e9f90 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28  sed);.  dlclose(
e9fa0 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73  pHandle);.}.#els
e9fb0 65 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f  e /* if SQLITE_O
e9fc0 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
e9fd0 4f 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a  ON is defined: *
e9fe0 2f 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78  /.  #define unix
e9ff0 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23 64 65 66  DlOpen  0.  #def
ea000 69 6e 65 20 75 6e 69 78 44 6c 45 72 72 6f 72 20  ine unixDlError 
ea010 30 0a 20 20 23 64 65 66 69 6e 65 20 75 6e 69 78  0.  #define unix
ea020 44 6c 53 79 6d 20 20 20 30 0a 20 20 23 64 65 66  DlSym   0.  #def
ea030 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20  ine unixDlClose 
ea040 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
ea050 57 72 69 74 65 20 6e 42 75 66 20 62 79 74 65 73  Write nBuf bytes
ea060 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61 74 61 20   of random data 
ea070 74 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  to the supplied 
ea080 62 75 66 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a  buffer zBuf..*/.
ea090 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 52  static int unixR
ea0a0 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65  andomness(sqlite
ea0b0 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20  3_vfs *NotUsed, 
ea0c0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
ea0d0 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f  zBuf){.  UNUSED_
ea0e0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
ea0f0 64 29 3b 0a 20 20 61 73 73 65 72 74 28 28 73 69  d);.  assert((si
ea100 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65  ze_t)nBuf>=(size
ea110 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f  of(time_t)+sizeo
ea120 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20  f(int)));..  /* 
ea130 57 65 20 68 61 76 65 20 74 6f 20 69 6e 69 74 69  We have to initi
ea140 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f 20 70 72  alize zBuf to pr
ea150 65 76 65 6e 74 20 76 61 6c 67 72 69 6e 64 20 66  event valgrind f
ea160 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67 0a 20 20  rom reporting.  
ea170 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54 68 65 20  ** errors.  The 
ea180 72 65 70 6f 72 74 73 20 69 73 73 75 65 64 20 62  reports issued b
ea190 79 20 76 61 6c 67 72 69 6e 64 20 61 72 65 20 69  y valgrind are i
ea1a0 6e 63 6f 72 72 65 63 74 20 2d 20 77 65 20 77 6f  ncorrect - we wo
ea1b0 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66 65 72 20  uld.  ** prefer 
ea1c0 74 68 61 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e  that the randomn
ea1d0 65 73 73 20 62 65 20 69 6e 63 72 65 61 73 65 64  ess be increased
ea1e0 20 62 79 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f   by making use o
ea1f0 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69  f the.  ** unini
ea200 74 69 61 6c 69 7a 65 64 20 73 70 61 63 65 20 69  tialized space i
ea210 6e 20 7a 42 75 66 20 2d 20 62 75 74 20 76 61 6c  n zBuf - but val
ea220 67 72 69 6e 64 20 65 72 72 6f 72 73 20 74 65 6e  grind errors ten
ea230 64 20 74 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20  d to worry.  ** 
ea240 73 6f 6d 65 20 75 73 65 72 73 2e 20 20 52 61 74  some users.  Rat
ea250 68 65 72 20 74 68 61 6e 20 61 72 67 75 65 2c 20  her than argue, 
ea260 69 74 20 73 65 65 6d 73 20 65 61 73 69 65 72 20  it seems easier 
ea270 6a 75 73 74 20 74 6f 20 69 6e 69 74 69 61 6c 69  just to initiali
ea280 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c  ze.  ** the whol
ea290 65 20 61 72 72 61 79 20 61 6e 64 20 73 69 6c 65  e array and sile
ea2a0 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c 20 65 76  nce valgrind, ev
ea2b0 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61 6e 73  en if that means
ea2c0 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73   less randomness
ea2d0 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e  .  ** in the ran
ea2e0 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20  dom seed..  **. 
ea2f0 20 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67   ** When testing
ea300 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a  , initializing z
ea310 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73  Buf[] to zero is
ea320 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20 54 68 61   all we do.  Tha
ea330 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61  t means.  ** tha
ea340 74 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20  t we always use 
ea350 74 68 65 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20  the same random 
ea360 6e 75 6d 62 65 72 20 73 65 71 75 65 6e 63 65 2e  number sequence.
ea370 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65    This makes the
ea380 0a 20 20 2a 2a 20 74 65 73 74 73 20 72 65 70 65  .  ** tests repe
ea390 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d  atable..  */.  m
ea3a0 65 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e  emset(zBuf, 0, n
ea3b0 42 75 66 29 3b 0a 23 69 66 20 21 64 65 66 69 6e  Buf);.#if !defin
ea3c0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a  ed(SQLITE_TEST).
ea3d0 20 20 7b 0a 20 20 20 20 69 6e 74 20 70 69 64 2c    {.    int pid,
ea3e0 20 66 64 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70   fd;.    fd = op
ea3f0 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d  en("/dev/urandom
ea400 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20  ", O_RDONLY);.  
ea410 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20    if( fd<0 ){.  
ea420 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20      time_t t;.  
ea430 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20      time(&t);.  
ea440 20 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c      memcpy(zBuf,
ea450 20 26 74 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b   &t, sizeof(t));
ea460 0a 20 20 20 20 20 20 70 69 64 20 3d 20 67 65 74  .      pid = get
ea470 70 69 64 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d  pid();.      mem
ea480 63 70 79 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66  cpy(&zBuf[sizeof
ea490 28 74 29 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65  (t)], &pid, size
ea4a0 6f 66 28 70 69 64 29 29 3b 0a 20 20 20 20 20 20  of(pid));.      
ea4b0 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 74  assert( sizeof(t
ea4c0 29 2b 73 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28  )+sizeof(pid)<=(
ea4d0 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20  size_t)nBuf );. 
ea4e0 20 20 20 20 20 6e 42 75 66 20 3d 20 73 69 7a 65       nBuf = size
ea4f0 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f 66 28 70  of(t) + sizeof(p
ea500 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  id);.    }else{.
ea510 20 20 20 20 20 20 6e 42 75 66 20 3d 20 72 65 61        nBuf = rea
ea520 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66  d(fd, zBuf, nBuf
ea530 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73 65 28 66  );.      close(f
ea540 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  d);.    }.  }.#e
ea550 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 42  ndif.  return nB
ea560 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c  uf;.}.../*.** Sl
ea570 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65  eep for a little
ea580 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20   while.  Return 
ea590 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69  the amount of ti
ea5a0 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65  me slept..** The
ea5b0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
ea5c0 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f   number of micro
ea5d0 73 65 63 6f 6e 64 73 20 77 65 20 77 61 6e 74 20  seconds we want 
ea5e0 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65  to sleep..** The
ea5f0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
ea600 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d   the number of m
ea610 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73  icroseconds of s
ea620 6c 65 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a  leep actually.**
ea630 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20   requested from 
ea640 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f  the underlying o
ea650 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2c  perating system,
ea660 20 61 20 6e 75 6d 62 65 72 20 77 68 69 63 68 0a   a number which.
ea670 2a 2a 20 6d 69 67 68 74 20 62 65 20 67 72 65 61  ** might be grea
ea680 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
ea690 6c 20 74 6f 20 74 68 65 20 61 72 67 75 6d 65 6e  l to the argumen
ea6a0 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a  t, but not less.
ea6b0 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 72 67 75  ** than the argu
ea6c0 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ment..*/.static 
ea6d0 69 6e 74 20 75 6e 69 78 53 6c 65 65 70 28 73 71  int unixSleep(sq
ea6e0 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73  lite3_vfs *NotUs
ea6f0 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  ed, int microsec
ea700 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53 5f 56 58  onds){.#if OS_VX
ea710 57 4f 52 4b 53 0a 20 20 73 74 72 75 63 74 20 74  WORKS.  struct t
ea720 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a 20 20 73  imespec sp;..  s
ea730 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69 63 72 6f  p.tv_sec = micro
ea740 73 65 63 6f 6e 64 73 20 2f 20 31 30 30 30 30 30  seconds / 100000
ea750 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73 65 63 20  0;.  sp.tv_nsec 
ea760 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20  = (microseconds 
ea770 25 20 31 30 30 30 30 30 30 29 20 2a 20 31 30 30  % 1000000) * 100
ea780 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26  0;.  nanosleep(&
ea790 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20 55 4e 55  sp, NULL);.  UNU
ea7a0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
ea7b0 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e  tUsed);.  return
ea7c0 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23   microseconds;.#
ea7d0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56  elif defined(HAV
ea7e0 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 56  E_USLEEP) && HAV
ea7f0 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 65  E_USLEEP.  uslee
ea800 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b  p(microseconds);
ea810 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ea820 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
ea830 72 65 74 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f  return microseco
ea840 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74  nds;.#else.  int
ea850 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d 69 63 72   seconds = (micr
ea860 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39 39 39 29  oseconds+999999)
ea870 2f 31 30 30 30 30 30 30 3b 0a 20 20 73 6c 65 65  /1000000;.  slee
ea880 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20 20 55 4e  p(seconds);.  UN
ea890 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
ea8a0 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72  otUsed);.  retur
ea8b0 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30 30 30 30  n seconds*100000
ea8c0 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  0;.#endif.}../*.
ea8d0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
ea8e0 20 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65   variable, if se
ea8f0 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  t to a non-zero 
ea900 76 61 6c 75 65 2c 20 69 73 20 69 6e 74 65 72 70  value, is interp
ea910 72 65 74 65 64 20 61 73 0a 2a 2a 20 74 68 65 20  reted as.** the 
ea920 6e 75 6d 62 65 72 20 6f 66 20 73 65 63 6f 6e 64  number of second
ea930 73 20 73 69 6e 63 65 20 31 39 37 30 20 61 6e 64  s since 1970 and
ea940 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74 20   is used to set 
ea950 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a  the result of.**
ea960 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
ea970 74 54 69 6d 65 28 29 20 64 75 72 69 6e 67 20 74  tTime() during t
ea980 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65  esting..*/.#ifde
ea990 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
ea9a0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
ea9b0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d  ite3_current_tim
ea9c0 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61 6b 65 20  e = 0;  /* Fake 
ea9d0 73 79 73 74 65 6d 20 74 69 6d 65 20 69 6e 20 73  system time in s
ea9e0 65 63 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37  econds since 197
ea9f0 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a  0. */.#endif../*
eaa00 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72  .** Find the cur
eaa10 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e  rent time (in Un
eaa20 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61  iversal Coordina
eaa30 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74  ted Time).  Writ
eaa40 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74  e the.** current
eaa50 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61   time and date a
eaa60 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e  s a Julian Day n
eaa70 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f  umber into *prNo
eaa80 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20  w and.** return 
eaa90 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  0.  Return 1 if 
eaaa0 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74  the time and dat
eaab0 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  e cannot be foun
eaac0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
eaad0 20 75 6e 69 78 43 75 72 72 65 6e 74 54 69 6d 65   unixCurrentTime
eaae0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f  (sqlite3_vfs *No
eaaf0 74 55 73 65 64 2c 20 64 6f 75 62 6c 65 20 2a 70  tUsed, double *p
eab00 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65 66 69 6e  rNow){.#if defin
eab10 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ed(SQLITE_OMIT_F
eab20 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29 0a 20  LOATING_POINT). 
eab30 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 74 69 6d   time_t t;.  tim
eab40 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e 6f 77 20  e(&t);.  *prNow 
eab50 3d 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74  = (((sqlite3_int
eab60 36 34 29 74 29 2f 38 36 34 30 20 2b 20 32 34 34  64)t)/8640 + 244
eab70 30 35 38 37 35 29 2f 31 30 3b 0a 23 65 6c 69 66  05875)/10;.#elif
eab80 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54 54   defined(NO_GETT
eab90 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a  OD).  time_t t;.
eaba0 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70    time(&t);.  *p
eabb0 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e 30  rNow = t/86400.0
eabc0 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23 65   + 2440587.5;.#e
eabd0 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  lif OS_VXWORKS. 
eabe0 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65 63   struct timespec
eabf0 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f 67   sNow;.  clock_g
eac00 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45 41  ettime(CLOCK_REA
eac10 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a 20  LTIME, &sNow);. 
eac20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35 38   *prNow = 244058
eac30 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73 65  7.5 + sNow.tv_se
eac40 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f 77  c/86400.0 + sNow
eac50 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30 30  .tv_nsec/8640000
eac60 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 73 65  0000000.0;.#else
eac70 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 76 61  .  struct timeva
eac80 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74 74 69 6d  l sNow;.  gettim
eac90 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c 20 30 29  eofday(&sNow, 0)
eaca0 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34  ;.  *prNow = 244
eacb0 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76  0587.5 + sNow.tv
eacc0 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73  _sec/86400.0 + s
eacd0 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38 36 34 30  Now.tv_usec/8640
eace0 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6e 64 69  0000000.0;.#endi
eacf0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
ead00 5f 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69  _TEST.  if( sqli
ead10 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65  te3_current_time
ead20 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d   ){.    *prNow =
ead30 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74   sqlite3_current
ead40 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20  _time/86400.0 + 
ead50 32 34 34 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23  2440587.5;.  }.#
ead60 65 6e 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50  endif.  UNUSED_P
ead70 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
ead80 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  );.  return 0;.}
ead90 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64 64 65 64  ../*.** We added
eada0 20 74 68 65 20 78 47 65 74 4c 61 73 74 45 72 72   the xGetLastErr
eadb0 6f 72 28 29 20 6d 65 74 68 6f 64 20 77 69 74 68  or() method with
eadc0 20 74 68 65 20 69 6e 74 65 6e 74 69 6f 6e 20 6f   the intention o
eadd0 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a 2a 20 62  f providing.** b
eade0 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76 65 6c 20  etter low-level 
eadf0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 77  error messages w
eae00 68 65 6e 20 6f 70 65 72 61 74 69 6e 67 2d 73 79  hen operating-sy
eae10 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73 20 63 6f  stem problems co
eae20 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69 6e 67 20  me up.** during 
eae30 53 51 4c 69 74 65 20 6f 70 65 72 61 74 69 6f 6e  SQLite operation
eae40 2e 20 20 42 75 74 20 73 6f 20 66 61 72 2c 20 6e  .  But so far, n
eae50 6f 6e 65 20 6f 66 20 74 68 61 74 20 68 61 73 20  one of that has 
eae60 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64  been implemented
eae70 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72 65 2e  .** in the core.
eae80 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e    So this routin
eae90 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  e is never calle
eaea0 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20 69 74 20  d.  For now, it 
eaeb0 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20 61 20 70  is merely.** a p
eaec0 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a 2a 2f 0a  lace-holder..*/.
eaed0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 47  static int unixG
eaee0 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69  etLastError(sqli
eaef0 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
eaf00 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 2c 20  , int NotUsed2, 
eaf10 63 68 61 72 20 2a 4e 6f 74 55 73 65 64 33 29 7b  char *NotUsed3){
eaf20 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
eaf30 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
eaf40 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
eaf50 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 55 4e  (NotUsed2);.  UN
eaf60 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
eaf70 6f 74 55 73 65 64 33 29 3b 0a 20 20 72 65 74 75  otUsed3);.  retu
eaf80 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 2a 2a  rn 0;.}../*.****
eaf90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eafa0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69  **** End of sqli
eafb0 74 65 33 5f 76 66 73 20 6d 65 74 68 6f 64 73 20  te3_vfs methods 
eafc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eafd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
eafe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a  **********/../**
eb030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a  ************.***
eb080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb090 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 50 72  ******* Begin Pr
eb0a0 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a  oxy Locking ****
eb0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eb0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
eb0d0 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  ** Proxy locking
eb0e0 20 69 73 20 61 20 22 75 62 65 72 2d 6c 6f 63 6b   is a "uber-lock
eb0f0 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69 6e 20 74  ing-method" in t
eb100 68 69 73 20 73 65 6e 73 65 3a 20 20 49 74 20 75  his sense:  It u
eb110 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74 68 65 72  ses the.** other
eb120 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73   locking methods
eb130 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79 20 6c 6f   on secondary lo
eb140 63 6b 20 66 69 6c 65 73 2e 20 20 50 72 6f 78 79  ck files.  Proxy
eb150 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61 0a 2a 2a   locking is a.**
eb160 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f 76 65 72   meta-layer over
eb170 20 74 6f 70 20 6f 66 20 74 68 65 20 70 72 69 6d   top of the prim
eb180 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67 20 69 6d  itive locking im
eb190 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f 76 65 2e  plemented above.
eb1a0 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73 20 72 65    For.** this re
eb1b0 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73 69  ason, the divisi
eb1c0 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  on that implemen
eb1d0 74 73 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b  ts of proxy lock
eb1e0 69 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 0a  ing is deferred.
eb1f0 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65 20 69 6e  ** until late in
eb200 20 74 68 65 20 66 69 6c 65 20 28 68 65 72 65 29   the file (here)
eb210 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68   after all of th
eb220 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d 65 74 68  e other I/O meth
eb230 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e  ods have.** been
eb240 20 64 65 66 69 6e 65 64 20 2d 20 73 6f 20 74 68   defined - so th
eb250 61 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65  at the primitive
eb260 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f 64 73   locking methods
eb270 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a   are available.*
eb280 2a 20 61 73 20 73 65 72 76 69 63 65 73 20 74 6f  * as services to
eb290 20 68 65 6c 70 20 77 69 74 68 20 74 68 65 20 69   help with the i
eb2a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
eb2b0 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a   proxy locking..
eb2c0 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  **.****.**.** Th
eb2d0 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e  e default lockin
eb2e0 67 20 73 63 68 65 6d 65 73 20 69 6e 20 53 51 4c  g schemes in SQL
eb2f0 69 74 65 20 75 73 65 20 62 79 74 65 2d 72 61 6e  ite use byte-ran
eb300 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 0a  ge locks on the.
eb310 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
eb320 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74 65 20 73   to coordinate s
eb330 61 66 65 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20  afe, concurrent 
eb340 61 63 63 65 73 73 20 62 79 20 6d 75 6c 74 69 70  access by multip
eb350 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a 20 61 6e  le readers.** an
eb360 64 20 77 72 69 74 65 72 73 20 5b 68 74 74 70 3a  d writers [http:
eb370 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f 6c 6f 63  //sqlite.org/loc
eb380 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e 20 20 54  kingv3.html].  T
eb390 68 65 20 66 69 76 65 20 66 69 6c 65 20 6c 6f 63  he five file loc
eb3a0 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 20 28  king.** states (
eb3b0 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e 44 49 4e  UNLOCKED, PENDIN
eb3c0 47 2c 20 53 48 41 52 45 44 2c 20 52 45 53 45 52  G, SHARED, RESER
eb3d0 56 45 44 2c 20 45 58 43 4c 55 53 49 56 45 29 20  VED, EXCLUSIVE) 
eb3e0 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a  are implemented.
eb3f0 2a 2a 20 61 73 20 50 4f 53 49 58 20 72 65 61 64  ** as POSIX read
eb400 20 26 20 77 72 69 74 65 20 6c 6f 63 6b 73 20 6f   & write locks o
eb410 76 65 72 20 66 69 78 65 64 20 73 65 74 20 6f 66  ver fixed set of
eb420 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76 69 61 20   locations (via 
eb430 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e 20 41 46  fsctl),.** on AF
eb440 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c 79 20 65  P and SMB only e
eb450 78 63 6c 75 73 69 76 65 20 62 79 74 65 2d 72 61  xclusive byte-ra
eb460 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65 20 61 76  nge locks are av
eb470 61 69 6c 61 62 6c 65 20 76 69 61 20 66 73 63 74  ailable via fsct
eb480 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f 57 52 28  l.** with _IOWR(
eb490 27 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20  'z', 23, struct 
eb4a0 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
eb4b0 29 20 74 6f 20 74 72 61 63 6b 20 74 68 65 20 73  ) to track the s
eb4c0 61 6d 65 20 35 20 73 74 61 74 65 73 2e 0a 2a 2a  ame 5 states..**
eb4d0 20 54 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 46   To simulate a F
eb4e0 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65 20 73 68  _RDLCK on the sh
eb4f0 61 72 65 64 20 72 61 6e 67 65 2c 20 6f 6e 20 41  ared range, on A
eb500 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79 20 73 65  FP a randomly se
eb510 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64 72 65 73  lected.** addres
eb520 73 20 69 6e 20 74 68 65 20 73 68 61 72 65 64 20  s in the shared 
eb530 72 61 6e 67 65 20 69 73 20 74 61 6b 65 6e 20 66  range is taken f
eb540 6f 72 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  or a SHARED lock
eb550 2c 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20  , the entire.** 
eb560 73 68 61 72 65 64 20 72 61 6e 67 65 20 69 73 20  shared range is 
eb570 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20 45 58 43  taken for an EXC
eb580 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a 0a 2a 2a  LUSIVE lock):.**
eb590 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44 49 4e 47  .**      PENDING
eb5a0 5f 42 59 54 45 20 20 20 20 20 20 20 20 30 78 34  _BYTE        0x4
eb5b0 30 30 30 30 30 30 30 09 09 20 20 20 09 0a 2a 2a  0000000..   ..**
eb5c0 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f 42        RESERVED_B
eb5d0 59 54 45 20 20 20 20 20 20 20 30 78 34 30 30 30  YTE       0x4000
eb5e0 30 30 30 31 0a 2a 2a 20 20 20 20 20 20 53 48 41  0001.**      SHA
eb5f0 52 45 44 5f 52 41 4e 47 45 20 20 20 20 20 20 20  RED_RANGE       
eb600 20 30 78 34 30 30 30 30 30 30 32 20 2d 3e 20 30   0x40000002 -> 0
eb610 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a 2a 2a 20  x40000200.**.** 
eb620 54 68 69 73 20 77 6f 72 6b 73 20 77 65 6c 6c 20  This works well 
eb630 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 66 69 6c  on the local fil
eb640 65 20 73 79 73 74 65 6d 2c 20 62 75 74 20 73 68  e system, but sh
eb650 6f 77 73 20 61 20 6e 65 61 72 6c 79 20 31 30 30  ows a nearly 100
eb660 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e 20 69 6e  x.** slowdown in
eb670 20 72 65 61 64 20 70 65 72 66 6f 72 6d 61 6e 63   read performanc
eb680 65 20 6f 6e 20 41 46 50 20 62 65 63 61 75 73 65  e on AFP because
eb690 20 74 68 65 20 41 46 50 20 63 6c 69 65 6e 74 20   the AFP client 
eb6a0 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74 68 65 20  disables.** the 
eb6b0 72 65 61 64 20 63 61 63 68 65 20 77 68 65 6e 20  read cache when 
eb6c0 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73  byte-range locks
eb6d0 20 61 72 65 20 70 72 65 73 65 6e 74 2e 20 20 45   are present.  E
eb6e0 6e 61 62 6c 69 6e 67 20 74 68 65 20 72 65 61 64  nabling the read
eb6f0 0a 2a 2a 20 63 61 63 68 65 20 65 78 70 6f 73 65  .** cache expose
eb700 73 20 61 20 63 61 63 68 65 20 63 6f 68 65 72 65  s a cache cohere
eb710 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74 68 61 74  ncy problem that
eb720 20 69 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 61   is present on a
eb730 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75 70 70 6f  ll OS X.** suppo
eb740 72 74 65 64 20 6e 65 74 77 6f 72 6b 20 66 69 6c  rted network fil
eb750 65 20 73 79 73 74 65 6d 73 2e 20 20 4e 46 53 20  e systems.  NFS 
eb760 61 6e 64 20 41 46 50 20 62 6f 74 68 20 6f 62 73  and AFP both obs
eb770 65 72 76 65 20 74 68 65 0a 2a 2a 20 63 6c 6f 73  erve the.** clos
eb780 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d 61 6e 74  e-to-open semant
eb790 69 63 73 20 66 6f 72 20 65 6e 73 75 72 69 6e 67  ics for ensuring
eb7a0 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63 79   cache coherency
eb7b0 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e 66 73 2e  .** [http://nfs.
eb7c0 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e 65 74 2f  sourceforge.net/
eb7d0 23 66 61 71 5f 61 38 5d 2c 20 77 68 69 63 68 20  #faq_a8], which 
eb7e0 64 6f 65 73 20 6e 6f 74 20 65 66 66 65 63 74 69  does not effecti
eb7f0 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65 73 73 20  vely.** address 
eb800 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73  the requirements
eb810 20 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 74 20   for concurrent 
eb820 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20  database access 
eb830 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 72  by multiple.** r
eb840 65 61 64 65 72 73 20 61 6e 64 20 77 72 69 74 65  eaders and write
eb850 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 77 77  rs.** [http://ww
eb860 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f 53 51 4c  w.nabble.com/SQL
eb870 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61 63 68 65  ite-on-NFS-cache
eb880 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64 31 35 36  -coherency-td156
eb890 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a 2a 2a 0a  55701.html]..**.
eb8a0 2a 2a 20 54 6f 20 61 64 64 72 65 73 73 20 74 68  ** To address th
eb8b0 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 61 6e  e performance an
eb8c0 64 20 63 61 63 68 65 20 63 6f 68 65 72 65 6e 63  d cache coherenc
eb8d0 79 20 69 73 73 75 65 73 2c 20 70 72 6f 78 79 20  y issues, proxy 
eb8e0 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20  file locking.** 
eb8f0 63 68 61 6e 67 65 73 20 74 68 65 20 77 61 79 20  changes the way 
eb900 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20  database access 
eb910 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79  is controlled by
eb920 20 6c 69 6d 69 74 69 6e 67 20 61 63 63 65 73 73   limiting access
eb930 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20   to a.** single 
eb940 68 6f 73 74 20 61 74 20 61 20 74 69 6d 65 20 61  host at a time a
eb950 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c 65 20 6c  nd moving file l
eb960 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74 68 65 20  ocks off of the 
eb970 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a  database file.**
eb980 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70 72 6f 78   and onto a prox
eb990 79 20 66 69 6c 65 20 6f 6e 20 74 68 65 20 6c 6f  y file on the lo
eb9a0 63 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d 2e  cal file system.
eb9b0 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 69 6e    .**.**.** Usin
eb9c0 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73 0a 2a 2a  g proxy locks.**
eb9d0 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
eb9e0 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50 49 73 0a  --.**.** C APIs.
eb9f0 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66  **.**  sqlite3_f
eba00 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c 20  ile_control(db, 
eba10 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 53  dbname, SQLITE_S
eba20 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45  ET_LOCKPROXYFILE
eba30 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ,.**            
eba40 20 20 20 20 20 20 20 20 20 20 20 3c 70 72 6f 78             <prox
eba50 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61 75 74 6f  y_path> | ":auto
eba60 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33  :");.**  sqlite3
eba70 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62  _file_control(db
eba80 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54 45  , dbname, SQLITE
eba90 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _GET_LOCKPROXYFI
ebaa0 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70 61 74 68  LE, &<proxy_path
ebab0 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 53 51 4c  >);.**.**.** SQL
ebac0 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a 2a 20 20   pragmas.**.**  
ebad0 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
ebae0 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c  .]lock_proxy_fil
ebaf0 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68 3e 20 7c  e=<proxy_path> |
ebb00 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50 52 41 47   :auto:.**  PRAG
ebb10 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f  MA [database.]lo
ebb20 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65 0a 2a 2a  ck_proxy_file.**
ebb30 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e 67 20 22  .** Specifying "
ebb40 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73 20 74 68  :auto:" means th
ebb50 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 61  at if there is a
ebb60 20 63 6f 6e 63 68 20 66 69 6c 65 20 77 69 74 68   conch file with
ebb70 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 68   a matching.** h
ebb80 6f 73 74 20 49 44 20 69 6e 20 69 74 2c 20 74 68  ost ID in it, th
ebb90 65 20 70 72 6f 78 79 20 70 61 74 68 20 69 6e 20  e proxy path in 
ebba0 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 77  the conch file w
ebbb0 69 6c 6c 20 62 65 20 75 73 65 64 2c 20 6f 74 68  ill be used, oth
ebbc0 65 72 77 69 73 65 0a 2a 2a 20 61 20 70 72 6f 78  erwise.** a prox
ebbd0 79 20 70 61 74 68 20 62 61 73 65 64 20 6f 6e 20  y path based on 
ebbe0 74 68 65 20 75 73 65 72 27 73 20 74 65 6d 70 20  the user's temp 
ebbf0 64 69 72 0a 2a 2a 20 28 76 69 61 20 63 6f 6e 66  dir.** (via conf
ebc00 73 74 72 28 5f 43 53 5f 44 41 52 57 49 4e 5f 55  str(_CS_DARWIN_U
ebc10 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c 2e 2e 2e  SER_TEMP_DIR,...
ebc20 29 29 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  )) will be used 
ebc30 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63 74 75 61  and the.** actua
ebc40 6c 20 70 72 6f 78 79 20 66 69 6c 65 20 6e 61 6d  l proxy file nam
ebc50 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 66  e is generated f
ebc60 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20 61 6e 64  rom the name and
ebc70 20 70 61 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20   path of the.** 
ebc80 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
ebc90 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a  For example:.**.
ebca0 2a 2a 20 20 20 20 20 20 20 46 6f 72 20 64 61 74  **       For dat
ebcb0 61 62 61 73 65 20 70 61 74 68 20 22 2f 55 73 65  abase path "/Use
ebcc0 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22 20 0a 2a  rs/me/foo.db" .*
ebcd0 2a 20 20 20 20 20 20 20 54 68 65 20 6c 6f 63 6b  *       The lock
ebce0 20 70 61 74 68 20 77 69 6c 6c 20 62 65 20 22 3c   path will be "<
ebcf0 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74 65 70 6c  tmpdir>/sqlitepl
ebd00 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d 65 5f 66  ocks/_Users_me_f
ebd10 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29 0a 2a 2a  oo.db:auto:").**
ebd20 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f 63 6b 20  .** Once a lock 
ebd30 70 72 6f 78 79 20 69 73 20 63 6f 6e 66 69 67 75  proxy is configu
ebd40 72 65 64 20 66 6f 72 20 61 20 64 61 74 61 62 61  red for a databa
ebd50 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69  se connection, i
ebd60 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20 62 65 20  t can not.** be 
ebd70 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65 76 65 72  removed, however
ebd80 20 69 74 20 6d 61 79 20 62 65 20 73 77 69 74 63   it may be switc
ebd90 68 65 64 20 74 6f 20 61 20 64 69 66 66 65 72 65  hed to a differe
ebda0 6e 74 20 70 72 6f 78 79 20 70 61 74 68 20 76 69  nt proxy path vi
ebdb0 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76 65 20 41  a.** the above A
ebdc0 50 49 73 20 28 61 73 73 75 6d 69 6e 67 20 74 68  PIs (assuming th
ebdd0 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 69 73 20  e conch file is 
ebde0 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c 64 20 62  not being held b
ebdf0 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e  y another.** con
ebe00 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72 6f 63 65  nection or proce
ebe10 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 48  ss). .**.**.** H
ebe20 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  ow proxy locking
ebe30 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d   works.** ------
ebe40 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ebe50 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 66 69  -.**.** Proxy fi
ebe60 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65 6c 69 65  le locking relie
ebe70 73 20 70 72 69 6d 61 72 69 6c 79 20 6f 6e 20 74  s primarily on t
ebe80 77 6f 20 6e 65 77 20 73 75 70 70 6f 72 74 69 6e  wo new supportin
ebe90 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a 2a 2a 20  g files: .**.** 
ebea0 20 20 2a 20 20 63 6f 6e 63 68 20 66 69 6c 65 20    *  conch file 
ebeb0 74 6f 20 6c 69 6d 69 74 20 61 63 63 65 73 73 20  to limit access 
ebec0 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
ebed0 66 69 6c 65 20 74 6f 20 61 20 73 69 6e 67 6c 65  file to a single
ebee0 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20 20 61 74   host.**      at
ebef0 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a 20 20 20   a time.**.**   
ebf00 2a 20 20 70 72 6f 78 79 20 66 69 6c 65 20 74 6f  *  proxy file to
ebf10 20 61 63 74 20 61 73 20 61 20 70 72 6f 78 79 20   act as a proxy 
ebf20 66 6f 72 20 74 68 65 20 61 64 76 69 73 6f 72 79  for the advisory
ebf30 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c 6c 79 0a   locks normally.
ebf40 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e 20 6f 6e  **      taken on
ebf50 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
ebf60 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68 20 66 69  .** The conch fi
ebf70 6c 65 20 2d 20 74 6f 20 75 73 65 20 61 20 70 72  le - to use a pr
ebf80 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c 69 74 65  oxy file, sqlite
ebf90 20 6d 75 73 74 20 66 69 72 73 74 20 22 68 6f 6c   must first "hol
ebfa0 64 20 74 68 65 20 63 6f 6e 63 68 22 0a 2a 2a 20  d the conch".** 
ebfb0 62 79 20 74 61 6b 69 6e 67 20 61 6e 20 73 71 6c  by taking an sql
ebfc0 69 74 65 2d 73 74 79 6c 65 20 73 68 61 72 65 64  ite-style shared
ebfd0 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e   lock on the con
ebfe0 63 68 20 66 69 6c 65 2c 20 72 65 61 64 69 6e 67  ch file, reading
ebff0 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73   the.** contents
ec000 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e 67 20 74   and comparing t
ec010 68 65 20 68 6f 73 74 27 73 20 75 6e 69 71 75 65  he host's unique
ec020 20 68 6f 73 74 20 49 44 20 28 73 65 65 20 62 65   host ID (see be
ec030 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b 0a 2a 2a  low) and lock.**
ec040 20 70 72 6f 78 79 20 70 61 74 68 20 61 67 61 69   proxy path agai
ec050 6e 73 74 20 74 68 65 20 76 61 6c 75 65 73 20 73  nst the values s
ec060 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 6f 6e  tored in the con
ec070 63 68 2e 20 20 54 68 65 20 63 6f 6e 63 68 20 66  ch.  The conch f
ec080 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64  ile is.** stored
ec090 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 69 72   in the same dir
ec0a0 65 63 74 6f 72 79 20 61 73 20 74 68 65 20 64 61  ectory as the da
ec0b0 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
ec0c0 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a  the file name.**
ec0d0 20 69 73 20 70 61 74 74 65 72 6e 65 64 20 61 66   is patterned af
ec0e0 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73 65  ter the database
ec0f0 20 66 69 6c 65 20 6e 61 6d 65 20 61 73 20 22 2e   file name as ".
ec100 3c 64 61 74 61 62 61 73 65 6e 61 6d 65 3e 2d 63  <databasename>-c
ec110 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20 74 68 65  onch"..** If the
ec120 20 63 6f 6e 63 68 20 66 69 6c 65 20 64 6f 65 73   conch file does
ec130 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f 72 20 69   not exist, or i
ec140 74 27 73 20 63 6f 6e 74 65 6e 74 73 20 64 6f 20  t's contents do 
ec150 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 0a 2a 2a  not match the.**
ec160 20 68 6f 73 74 20 49 44 20 61 6e 64 2f 6f 72 20   host ID and/or 
ec170 70 72 6f 78 79 20 70 61 74 68 2c 20 74 68 65 6e  proxy path, then
ec180 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 65 73 63   the lock is esc
ec190 61 6c 61 74 65 64 20 74 6f 20 61 6e 20 65 78 63  alated to an exc
ec1a0 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63 6b 20 61  lusive.** lock a
ec1b0 6e 64 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  nd the conch fil
ec1c0 65 20 63 6f 6e 74 65 6e 74 73 20 69 73 20 75 70  e contents is up
ec1d0 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 68  dated with the h
ec1e0 6f 73 74 20 49 44 20 61 6e 64 20 70 72 6f 78 79  ost ID and proxy
ec1f0 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20 74 68 65  .** path and the
ec200 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e 67 72 61   lock is downgra
ec210 64 65 64 20 74 6f 20 61 20 73 68 61 72 65 64 20  ded to a shared 
ec220 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20 49 66 20  lock again.  If 
ec230 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20 69 73 20  the conch.** is 
ec240 68 65 6c 64 20 62 79 20 61 6e 6f 74 68 65 72 20  held by another 
ec250 70 72 6f 63 65 73 73 20 28 77 69 74 68 20 61 20  process (with a 
ec260 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c 20 74 68  shared lock), th
ec270 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  e exclusive lock
ec280 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e  .** will fail an
ec290 64 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73  d SQLITE_BUSY is
ec2a0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
ec2b0 20 54 68 65 20 70 72 6f 78 79 20 66 69 6c 65 20   The proxy file 
ec2c0 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  - a single-byte 
ec2d0 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 61 6c  file used for al
ec2e0 6c 20 61 64 76 69 73 6f 72 79 20 66 69 6c 65 20  l advisory file 
ec2f0 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c  locks.** normall
ec300 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68 65 20 64  y taken on the d
ec310 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 20  atabase file.   
ec320 54 68 69 73 20 61 6c 6c 6f 77 73 20 66 6f 72 20  This allows for 
ec330 73 61 66 65 20 73 68 61 72 69 6e 67 0a 2a 2a 20  safe sharing.** 
ec340 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
ec350 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74 69 70 6c  file for multipl
ec360 65 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 72  e readers and wr
ec370 69 74 65 72 73 20 6f 6e 20 74 68 65 20 73 61 6d  iters on the sam
ec380 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68 65 20 63  e.** host (the c
ec390 6f 6e 63 68 20 65 6e 73 75 72 65 73 20 74 68 61  onch ensures tha
ec3a0 74 20 74 68 65 79 20 61 6c 6c 20 75 73 65 20 74  t they all use t
ec3b0 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c 20 6c 6f  he same local lo
ec3c0 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a 2a 2a 20  ck file)..**.** 
ec3d0 54 68 65 72 65 20 69 73 20 61 20 74 68 69 72 64  There is a third
ec3e0 20 66 69 6c 65 20 2d 20 74 68 65 20 68 6f 73 74   file - the host
ec3f0 20 49 44 20 66 69 6c 65 20 2d 20 75 73 65 64 20   ID file - used 
ec400 61 73 20 61 20 70 65 72 73 69 73 74 65 6e 74 20  as a persistent 
ec410 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20 61 20 75  record.** of a u
ec420 6e 69 71 75 65 20 69 64 65 6e 74 69 66 69 65 72  nique identifier
ec430 20 66 6f 72 20 74 68 65 20 68 6f 73 74 2c 20 61   for the host, a
ec440 20 31 32 38 2d 62 79 74 65 20 75 6e 69 71 75 65   128-byte unique
ec450 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a 2a   host id file.**
ec460 20 69 6e 20 74 68 65 20 70 61 74 68 20 64 65 66   in the path def
ec470 69 6e 65 64 20 62 79 20 74 68 65 20 48 4f 53 54  ined by the HOST
ec480 49 44 50 41 54 48 20 6d 61 63 72 6f 20 28 64 65  IDPATH macro (de
ec490 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73 0a 2a  fault value is.*
ec4a0 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61 63 68 65  * /Library/Cache
ec4b0 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c  s/.com.apple.sql
ec4c0 69 74 65 43 6f 6e 63 68 48 6f 73 74 49 64 29 2e  iteConchHostId).
ec4d0 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73 74 69 6e  .**.** Requestin
ec4e0 67 20 74 68 65 20 6c 6f 63 6b 20 70 72 6f 78 79  g the lock proxy
ec4f0 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d 65 64 69   does not immedi
ec500 61 74 65 6c 79 20 74 61 6b 65 20 74 68 65 20 63  ately take the c
ec510 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a 2a 20 6f  onch, it is.** o
ec520 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65 6e 20 74  nly taken when t
ec530 68 65 20 66 69 72 73 74 20 72 65 71 75 65 73 74  he first request
ec540 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61 62 61 73   to lock databas
ec550 65 20 66 69 6c 65 20 69 73 20 6d 61 64 65 2e 20  e file is made. 
ec560 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74 63 68 65   .** This matche
ec570 73 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73 20  s the semantics 
ec580 6f 66 20 74 68 65 20 74 72 61 64 69 74 69 6f 6e  of the tradition
ec590 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65 68 61 76  al locking behav
ec5a0 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a 20 6f 70  ior, where.** op
ec5b0 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65 63 74 69  ening a connecti
ec5c0 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73 65  on to a database
ec5d0 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 74   file does not t
ec5e0 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e 20 69 74  ake a lock on it
ec5f0 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72 65 64 20  ..** The shared 
ec600 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f 70 65 6e  lock and an open
ec610 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
ec620 20 61 72 65 20 6d 61 69 6e 74 61 69 6e 65 64 20   are maintained 
ec630 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65 20 63 6f  until .** the co
ec640 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20  nnection to the 
ec650 64 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f 73  database is clos
ec660 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ed. .**.** The p
ec670 72 6f 78 79 20 66 69 6c 65 20 61 6e 64 20 74 68  roxy file and th
ec680 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 72 65 20  e lock file are 
ec690 6e 65 76 65 72 20 64 65 6c 65 74 65 64 20 73 6f  never deleted so
ec6a0 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65 65 64 0a   they only need.
ec6b0 2a 2a 20 74 6f 20 62 65 20 63 72 65 61 74 65 64  ** to be created
ec6c0 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   the first time 
ec6d0 74 68 65 79 20 61 72 65 20 75 73 65 64 2e 0a 2a  they are used..*
ec6e0 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69  *.** Configurati
ec6f0 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 2d 2d  on options.** --
ec700 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
ec710 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51 4c 49 54  ---.**.**  SQLIT
ec720 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c  E_PREFER_PROXY_L
ec730 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20 20 20 20  OCKING.**.**    
ec740 20 20 20 44 61 74 61 62 61 73 65 20 66 69 6c 65     Database file
ec750 73 20 61 63 63 65 73 73 65 64 20 6f 6e 20 6e 6f  s accessed on no
ec760 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73  n-local file sys
ec770 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20 20 20 20  tems are.**     
ec780 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20    automatically 
ec790 63 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 70  configured for p
ec7a0 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c 20 6c 6f  roxy locking, lo
ec7b0 63 6b 20 66 69 6c 65 73 20 61 72 65 0a 2a 2a 20  ck files are.** 
ec7c0 20 20 20 20 20 20 6e 61 6d 65 64 20 61 75 74 6f        named auto
ec7d0 6d 61 74 69 63 61 6c 6c 79 20 75 73 69 6e 67 20  matically using 
ec7e0 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 20 61  the same logic a
ec7f0 73 0a 2a 2a 20 20 20 20 20 20 20 50 52 41 47 4d  s.**       PRAGM
ec800 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c  A lock_proxy_fil
ec810 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a 20 20 20  e=":auto:".**   
ec820 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f 50 52 4f   .**  SQLITE_PRO
ec830 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a 2a 20 20  XY_DEBUG.**.**  
ec840 20 20 20 20 20 45 6e 61 62 6c 65 73 20 74 68 65       Enables the
ec850 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65 72 72 6f   logging of erro
ec860 72 20 6d 65 73 73 61 67 65 73 20 64 75 72 69 6e  r messages durin
ec870 67 20 68 6f 73 74 20 69 64 20 66 69 6c 65 0a 2a  g host id file.*
ec880 2a 20 20 20 20 20 20 20 72 65 74 72 69 65 76 61  *       retrieva
ec890 6c 20 61 6e 64 20 63 72 65 61 74 69 6f 6e 0a 2a  l and creation.*
ec8a0 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50 41 54 48  *.**  HOSTIDPATH
ec8b0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76 65  .**.**       Ove
ec8c0 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75  rrides the defau
ec8d0 6c 74 20 68 6f 73 74 20 49 44 20 66 69 6c 65 20  lt host ID file 
ec8e0 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  path location.**
ec8f0 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58 59 44 49  .**  LOCKPROXYDI
ec900 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4f 76  R.**.**       Ov
ec910 65 72 72 69 64 65 73 20 74 68 65 20 64 65 66 61  errides the defa
ec920 75 6c 74 20 64 69 72 65 63 74 6f 72 79 20 75 73  ult directory us
ec930 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70 72 6f 78  ed for lock prox
ec940 79 20 66 69 6c 65 73 20 74 68 61 74 0a 2a 2a 20  y files that.** 
ec950 20 20 20 20 20 20 61 72 65 20 6e 61 6d 65 64 20        are named 
ec960 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 76 69  automatically vi
ec970 61 20 74 68 65 20 22 3a 61 75 74 6f 3a 22 20 73  a the ":auto:" s
ec980 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20 20 53 51  etting.**.**  SQ
ec990 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f  LITE_DEFAULT_PRO
ec9a0 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e  XYDIR_PERMISSION
ec9b0 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 50 65  S.**.**       Pe
ec9c0 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20 75 73 65  rmissions to use
ec9d0 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61   when creating a
ec9e0 20 64 69 72 65 63 74 6f 72 79 20 66 6f 72 20 73   directory for s
ec9f0 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a 20 20 20  toring the.**   
eca00 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66      lock proxy f
eca10 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73 65 64 20  iles, only used 
eca20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58 59 44 49  when LOCKPROXYDI
eca30 52 20 69 73 20 6e 6f 74 20 73 65 74 2e 0a 2a 2a  R is not set..**
eca40 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a 2a 20 41      .**    .** A
eca50 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61 62 6f 76  s mentioned abov
eca60 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69 6c 65 64  e, when compiled
eca70 20 77 69 74 68 20 53 51 4c 49 54 45 5f 50 52 45   with SQLITE_PRE
eca80 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e  FER_PROXY_LOCKIN
eca90 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67 20 74 68  G,.** setting th
ecaa0 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61  e environment va
ecab0 72 69 61 62 6c 65 20 53 51 4c 49 54 45 5f 46 4f  riable SQLITE_FO
ecac0 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43 4b 49 4e  RCE_PROXY_LOCKIN
ecad0 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a 2a 20 66  G to 1 will.** f
ecae0 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69  orce proxy locki
ecaf0 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f  ng to be used fo
ecb00 72 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65  r every database
ecb10 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c 20 61 6e   file opened, an
ecb20 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66 6f 72 63  d 0.** will forc
ecb30 65 20 61 75 74 6f 6d 61 74 69 63 20 70 72 6f 78  e automatic prox
ecb40 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 62 65 20  y locking to be 
ecb50 64 69 73 61 62 6c 65 64 20 66 6f 72 20 61 6c 6c  disabled for all
ecb60 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
ecb70 65 73 20 28 65 78 70 6c 69 63 69 74 79 20 63 61  es (explicity ca
ecb80 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45  lling the SQLITE
ecb90 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49  _SET_LOCKPROXYFI
ecba0 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a 2a 2a 20  LE pragma or.** 
ecbb0 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63 6f 6e 74  sqlite_file_cont
ecbc0 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f 74 20 61  rol API is not a
ecbd0 66 66 65 63 74 65 64 20 62 79 20 53 51 4c 49 54  ffected by SQLIT
ecbe0 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f  E_FORCE_PROXY_LO
ecbf0 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  CKING)..*/../*.*
ecc00 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20  * Proxy locking 
ecc10 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c  is only availabl
ecc20 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a 2a 2f 0a  e on MacOSX .*/.
ecc30 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50  #if defined(__AP
ecc40 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
ecc50 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
ecc60 53 54 59 4c 45 0a 0a 23 69 66 64 65 66 20 53 51  STYLE..#ifdef SQ
ecc70 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 73 69 6d  LITE_TEST./* sim
ecc80 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c 65 20 68  ulate multiple h
ecc90 6f 73 74 73 20 62 79 20 63 72 65 61 74 69 6e 67  osts by creating
ecca0 20 75 6e 69 71 75 65 20 68 6f 73 74 69 64 20 66   unique hostid f
eccb0 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a 53 51 4c  ile paths */.SQL
eccc0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
eccd0 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75 6d 20 3d  te3_hostid_num =
ecce0 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a   0;.#endif../*.*
eccf0 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f 63 6b 69  * The proxyLocki
ecd00 6e 67 43 6f 6e 74 65 78 74 20 68 61 73 20 74 68  ngContext has th
ecd10 65 20 70 61 74 68 20 61 6e 64 20 66 69 6c 65 20  e path and file 
ecd20 73 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 74  structures for t
ecd30 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a 20 61 6e  he remote .** an
ecd40 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79 20 66 69  d local proxy fi
ecd50 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a 74 79 70  les in it.*/.typ
ecd60 65 64 65 66 20 73 74 72 75 63 74 20 70 72 6f 78  edef struct prox
ecd70 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
ecd80 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
ecd90 65 78 74 3b 0a 73 74 72 75 63 74 20 70 72 6f 78  ext;.struct prox
ecda0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
ecdb0 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f  {.  unixFile *co
ecdc0 6e 63 68 46 69 6c 65 3b 20 20 20 20 20 20 20 20  nchFile;        
ecdd0 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63 68 20 66   /* Open conch f
ecde0 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 63  ile */.  char *c
ecdf0 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b 20 20 20  onchFilePath;   
ece00 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
ece10 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
ece20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 6c  */.  unixFile *l
ece30 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20 20 20 20  ockProxy;       
ece40 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f 78 79 20    /* Open proxy 
ece50 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20 63  lock file */.  c
ece60 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78 79 50 61  har *lockProxyPa
ece70 74 68 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  th;         /* N
ece80 61 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 78 79  ame of the proxy
ece90 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a 20 20   lock file */.  
ecea0 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20 20  char *dbPath;   
eceb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ecec0 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e  Name of the open
eced0 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 63   file */.  int c
ecee0 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20 20 20 20  onchHeld;       
ecef0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
ecf00 69 66 20 74 68 65 20 63 6f 6e 63 68 20 69 73 20  if the conch is 
ecf10 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 2a  currently held *
ecf20 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64 4c 6f 63  /.  void *oldLoc
ecf30 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20  kingContext;    
ecf40 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6c 6f 63   /* Original loc
ecf50 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74 6f 20 72  kingcontext to r
ecf60 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f 73 65 20  estore on close 
ecf70 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f  */.  sqlite3_io_
ecf80 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70  methods const *p
ecf90 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20 20 20 2f  OldMethod;     /
ecfa0 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f 4f 20 6d  * Original I/O m
ecfb0 65 74 68 6f 64 73 20 66 6f 72 20 63 6c 6f 73 65  ethods for close
ecfc0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f 53 54 49   */.};../* HOSTI
ecfd0 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43 48 4c 45  DLEN and CONCHLE
ecfe0 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64 65 20 73  N both include s
ecff0 70 61 63 65 20 66 6f 72 20 74 68 65 20 73 74 72  pace for the str
ed000 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69 6e 61 74  ing .** terminat
ed010 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23 64 65 66  ing nul .*/.#def
ed020 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e 20 20 20  ine HOSTIDLEN   
ed030 20 20 20 20 20 20 31 32 38 0a 23 64 65 66 69 6e        128.#defin
ed040 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20 20 20 20  e CONCHLEN      
ed050 20 20 20 20 28 4d 41 58 50 41 54 48 4c 45 4e 2b      (MAXPATHLEN+
ed060 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a 23 69 66  HOSTIDLEN+1).#if
ed070 6e 64 65 66 20 48 4f 53 54 49 44 50 41 54 48 0a  ndef HOSTIDPATH.
ed080 23 20 64 65 66 69 6e 65 20 48 4f 53 54 49 44 50  # define HOSTIDP
ed090 41 54 48 20 20 20 20 20 20 20 22 2f 4c 69 62 72  ATH       "/Libr
ed0a0 61 72 79 2f 43 61 63 68 65 73 2f 2e 63 6f 6d 2e  ary/Caches/.com.
ed0b0 61 70 70 6c 65 2e 73 71 6c 69 74 65 43 6f 6e 63  apple.sqliteConc
ed0c0 68 48 6f 73 74 49 64 22 0a 23 65 6e 64 69 66 0a  hHostId".#endif.
ed0d0 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79 20 61 20  ./* basically a 
ed0e0 63 6f 70 79 20 6f 66 20 75 6e 69 78 52 61 6e 64  copy of unixRand
ed0f0 6f 6d 6e 65 73 73 20 77 69 74 68 20 64 69 66 66  omness with diff
ed100 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74 20 62 65  erent.** test be
ed110 68 61 76 69 6f 72 20 62 75 69 6c 74 20 69 6e 20  havior built in 
ed120 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  */.static int pr
ed130 6f 78 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49  oxyGenerateHostI
ed140 44 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 29  D(char *pHostID)
ed150 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20 66 64 2c  {.  int pid, fd,
ed160 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64   len;.  unsigned
ed170 20 63 68 61 72 20 2a 6b 65 79 20 3d 20 28 75 6e   char *key = (un
ed180 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 48  signed char *)pH
ed190 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d 65 6d 73  ostID;.  .  mems
ed1a0 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f 53 54 49  et(key, 0, HOSTI
ed1b0 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20 3d 20 30  DLEN);.  len = 0
ed1c0 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e 28 22 2f  ;.  fd = open("/
ed1d0 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20 4f 5f  dev/urandom", O_
ed1e0 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66 28 20 66  RDONLY);.  if( f
ed1f0 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c 65 6e 20  d>=0 ){.    len 
ed200 3d 20 72 65 61 64 28 66 64 2c 20 6b 65 79 2c 20  = read(fd, key, 
ed210 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20 20 20 20  HOSTIDLEN);.    
ed220 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20 73 69  close(fd); /* si
ed230 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68 65 20  lently leak the 
ed240 66 64 20 69 66 20 69 74 20 66 61 69 6c 73 20 2a  fd if it fails *
ed250 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c 65 6e 20  /.  }.  if( len 
ed260 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20  < HOSTIDLEN ){. 
ed270 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20     time_t t;.   
ed280 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 6d   time(&t);.    m
ed290 65 6d 63 70 79 28 6b 65 79 2c 20 26 74 2c 20 73  emcpy(key, &t, s
ed2a0 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20 20 20 70  izeof(t));.    p
ed2b0 69 64 20 3d 20 67 65 74 70 69 64 28 29 3b 0a 20  id = getpid();. 
ed2c0 20 20 20 6d 65 6d 63 70 79 28 26 6b 65 79 5b 73     memcpy(&key[s
ed2d0 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c  izeof(t)], &pid,
ed2e0 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20   sizeof(pid));. 
ed2f0 20 7d 0a 20 20 0a 23 69 66 64 65 66 20 4d 41 4b   }.  .#ifdef MAK
ed300 45 5f 50 52 45 54 54 59 5f 48 4f 53 54 49 44 0a  E_PRETTY_HOSTID.
ed310 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20    {.    int i;. 
ed320 20 20 20 2f 2a 20 66 69 6c 74 65 72 20 74 68 65     /* filter the
ed330 20 62 79 74 65 73 20 69 6e 74 6f 20 70 72 69 6e   bytes into prin
ed340 74 61 62 6c 65 20 61 73 63 69 69 20 63 68 61 72  table ascii char
ed350 61 63 74 65 72 73 20 61 6e 64 20 4e 55 4c 20 74  acters and NUL t
ed360 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20 20 20 20  erminate */.    
ed370 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45 4e 2d 31  key[(HOSTIDLEN-1
ed380 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20 20 20 66  )] = 0x00;.    f
ed390 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48 4f 53 54  or( i=0; i<(HOST
ed3a0 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b 20 29 7b  IDLEN-1); i++ ){
ed3b0 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
ed3c0 63 68 61 72 20 70 61 20 3d 20 6b 65 79 5b 69 5d  char pa = key[i]
ed3d0 26 30 78 37 46 3b 0a 20 20 20 20 20 20 69 66 28  &0x7F;.      if(
ed3e0 20 70 61 3c 30 78 32 30 20 29 7b 0a 20 20 20 20   pa<0x20 ){.    
ed3f0 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20 28 6b 65      key[i] = (ke
ed400 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20 30 78 38  y[i]&0x80 == 0x8
ed410 30 29 20 3f 20 70 61 2b 30 78 34 30 20 3a 20 70  0) ? pa+0x40 : p
ed420 61 2b 30 78 32 30 3b 0a 20 20 20 20 20 20 7d 65  a+0x20;.      }e
ed430 6c 73 65 20 69 66 28 20 70 61 3d 3d 30 78 37 46  lse if( pa==0x7F
ed440 20 29 7b 0a 20 20 20 20 20 20 20 20 6b 65 79 5b   ){.        key[
ed450 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26 30 78 38  i] = (key[i]&0x8
ed460 30 20 3d 3d 20 30 78 38 30 29 20 3f 20 70 61 3d  0 == 0x80) ? pa=
ed470 30 78 32 30 20 3a 20 70 61 2b 30 78 37 45 3b 0a  0x20 : pa+0x7E;.
ed480 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
ed490 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  }.#endif.  retur
ed4a0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
ed4b0 2f 2a 20 77 72 69 74 65 73 20 74 68 65 20 68 6f  /* writes the ho
ed4c0 73 74 20 69 64 20 70 61 74 68 20 74 6f 20 70 61  st id path to pa
ed4d0 74 68 2c 20 70 61 74 68 20 73 68 6f 75 6c 64 20  th, path should 
ed4e0 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c 6f 63 61  be an pre-alloca
ed4f0 74 65 64 20 62 75 66 66 65 72 0a 2a 2a 20 77 69  ted buffer.** wi
ed500 74 68 20 65 6e 6f 75 67 68 20 73 70 61 63 65 20  th enough space 
ed510 66 6f 72 20 61 20 70 61 74 68 20 0a 2a 2f 0a 73  for a path .*/.s
ed520 74 61 74 69 63 20 76 6f 69 64 20 70 72 6f 78 79  tatic void proxy
ed530 47 65 74 48 6f 73 74 49 44 50 61 74 68 28 63 68  GetHostIDPath(ch
ed540 61 72 20 2a 70 61 74 68 2c 20 73 69 7a 65 5f 74  ar *path, size_t
ed550 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c 63 70 79   len){.  strlcpy
ed560 28 70 61 74 68 2c 20 48 4f 53 54 49 44 50 41 54  (path, HOSTIDPAT
ed570 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64 65 66 20  H, len);.#ifdef 
ed580 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66  SQLITE_TEST.  if
ed590 28 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64  ( sqlite3_hostid
ed5a0 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20 20 63 68  _num>0 ){.    ch
ed5b0 61 72 20 73 75 66 66 69 78 5b 32 5d 20 3d 20 22  ar suffix[2] = "
ed5c0 31 22 3b 0a 20 20 20 20 73 75 66 66 69 78 5b 30  1";.    suffix[0
ed5d0 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d 20 2b 20  ] = suffix[0] + 
ed5e0 73 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e  sqlite3_hostid_n
ed5f0 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63 61 74 28  um;.    strlcat(
ed600 70 61 74 68 2c 20 73 75 66 66 69 78 2c 20 6c 65  path, suffix, le
ed610 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  n);.  }.#endif. 
ed620 20 4f 53 54 52 41 43 45 33 28 22 47 45 54 48 4f   OSTRACE3("GETHO
ed630 53 54 49 44 50 41 54 48 20 20 25 73 20 70 69 64  STIDPATH  %s pid
ed640 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c 20 67 65  =%d\n", path, ge
ed650 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f 2a 20 67  tpid());.}../* g
ed660 65 74 20 74 68 65 20 68 6f 73 74 20 49 44 20 66  et the host ID f
ed670 72 6f 6d 20 61 20 73 71 6c 69 74 65 20 68 6f 73  rom a sqlite hos
ed680 74 69 64 20 66 69 6c 65 20 73 74 6f 72 65 64 20  tid file stored 
ed690 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 2d  in the .** user-
ed6a0 73 70 65 63 69 66 69 63 20 74 6d 70 20 64 69 72  specific tmp dir
ed6b0 65 63 74 6f 72 79 2c 20 63 72 65 61 74 65 20 74  ectory, create t
ed6c0 68 65 20 49 44 20 69 66 20 69 74 27 73 20 6e 6f  he ID if it's no
ed6d0 74 20 74 68 65 72 65 20 61 6c 72 65 61 64 79 20  t there already 
ed6e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
ed6f0 72 6f 78 79 47 65 74 48 6f 73 74 49 44 28 63 68  roxyGetHostID(ch
ed700 61 72 20 2a 70 48 6f 73 74 49 44 2c 20 69 6e 74  ar *pHostID, int
ed710 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20 69 6e 74   *pError){.  int
ed720 20 66 64 3b 0a 20 20 63 68 61 72 20 70 61 74 68   fd;.  char path
ed730 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 20 0a 20  [MAXPATHLEN]; . 
ed740 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a 20 20 69   size_t len;.  i
ed750 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f 4f 4b 3b  nt rc=SQLITE_OK;
ed760 0a 0a 20 20 70 72 6f 78 79 47 65 74 48 6f 73 74  ..  proxyGetHost
ed770 49 44 50 61 74 68 28 70 61 74 68 2c 20 4d 41 58  IDPath(path, MAX
ed780 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f 2a 20 74  PATHLEN);.  /* t
ed790 72 79 20 74 6f 20 63 72 65 61 74 65 20 74 68 65  ry to create the
ed7a0 20 68 6f 73 74 20 49 44 20 66 69 6c 65 2c 20 69   host ID file, i
ed7b0 66 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69  f it already exi
ed7c0 73 74 73 20 72 65 61 64 20 74 68 65 20 63 6f 6e  sts read the con
ed7d0 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64 20 3d 20  tents */.  fd = 
ed7e0 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f 43 52 45  open(path, O_CRE
ed7f0 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f 5f 45 58  AT|O_WRONLY|O_EX
ed800 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20 69 66 28  CL, 0644);.  if(
ed810 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 69 6e 74   fd<0 ){.    int
ed820 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09 09 0a 20   err=errno;.... 
ed830 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58 49     if( err!=EEXI
ed840 53 54 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  ST ){.#ifdef SQL
ed850 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20  ITE_PROXY_DEBUG 
ed860 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74  /* set the sqlit
ed870 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
ed880 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20  instead */.     
ed890 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
ed8a0 20 22 73 71 6c 69 74 65 20 65 72 72 6f 72 20 63   "sqlite error c
ed8b0 72 65 61 74 69 6e 67 20 68 6f 73 74 20 49 44 20  reating host ID 
ed8c0 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e 22 2c 0a  file %s: %s\n",.
ed8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61                pa
ed8e0 74 68 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72  th, strerror(err
ed8f0 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ));.#endif.     
ed900 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50   return SQLITE_P
ed910 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  ERM;.    }.    /
ed920 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72 65 61 74  * couldn't creat
ed930 65 20 74 68 65 20 66 69 6c 65 2c 20 72 65 61 64  e the file, read
ed940 20 69 74 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20   it instead */. 
ed950 20 20 20 66 64 20 3d 20 6f 70 65 6e 28 70 61 74     fd = open(pat
ed960 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 45 58  h, O_RDONLY|O_EX
ed970 43 4c 29 3b 0a 20 20 20 20 69 66 28 20 66 64 3c  CL);.    if( fd<
ed980 30 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  0 ){.#ifdef SQLI
ed990 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47 20 2f  TE_PROXY_DEBUG /
ed9a0 2a 20 73 65 74 20 74 68 65 20 73 71 6c 69 74 65  * set the sqlite
ed9b0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
ed9c0 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20 20 20 20  nstead */.      
ed9d0 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b  int err = errno;
ed9e0 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73  .      fprintf(s
ed9f0 74 64 65 72 72 2c 20 22 73 71 6c 69 74 65 20 65  tderr, "sqlite e
eda00 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20 68 6f 73  rror opening hos
eda10 74 20 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73  t ID file %s: %s
eda20 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
eda30 20 20 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f     path, strerro
eda40 72 28 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a  r(err));.#endif.
eda50 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
eda60 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a  ITE_PERM;.    }.
eda70 20 20 20 20 6c 65 6e 20 3d 20 70 72 65 61 64 28      len = pread(
eda80 66 64 2c 20 70 48 6f 73 74 49 44 2c 20 48 4f 53  fd, pHostID, HOS
eda90 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20  TIDLEN, 0);.    
edaa0 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a 20 20 20  if( len<0 ){.   
edab0 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 65 72 72     *pError = err
edac0 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  no;.      rc = S
edad0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
edae0 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
edaf0 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e 20 29 7b  len<HOSTIDLEN ){
edb00 0a 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d  .      *pError =
edb10 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   0;.      rc = S
edb20 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
edb30 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d 0a 20 20  T_READ;.    }.  
edb40 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a 20    close(fd); /* 
edb50 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74 68  silently leak th
edb60 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c 73  e fd if it fails
edb70 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 33   */.    OSTRACE3
edb80 28 22 47 45 54 48 4f 53 54 49 44 20 20 72 65 61  ("GETHOSTID  rea
edb90 64 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20  d %s pid=%d\n", 
edba0 70 48 6f 73 74 49 44 2c 20 67 65 74 70 69 64 28  pHostID, getpid(
edbb0 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  ));.    return r
edbc0 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  c;.  }else{.    
edbd0 2f 2a 20 77 65 27 72 65 20 63 72 65 61 74 69 6e  /* we're creatin
edbe0 67 20 74 68 65 20 68 6f 73 74 20 49 44 20 66 69  g the host ID fi
edbf0 6c 65 20 28 75 73 65 20 61 20 72 61 6e 64 6f 6d  le (use a random
edc00 20 73 74 72 69 6e 67 20 6f 66 20 62 79 74 65 73   string of bytes
edc10 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78 79 47 65  ) */.    proxyGe
edc20 6e 65 72 61 74 65 48 6f 73 74 49 44 28 70 48 6f  nerateHostID(pHo
edc30 73 74 49 44 29 3b 0a 20 20 20 20 6c 65 6e 20 3d  stID);.    len =
edc40 20 70 77 72 69 74 65 28 66 64 2c 20 70 48 6f 73   pwrite(fd, pHos
edc50 74 49 44 2c 20 48 4f 53 54 49 44 4c 45 4e 2c 20  tID, HOSTIDLEN, 
edc60 30 29 3b 0a 20 20 20 20 69 66 28 20 6c 65 6e 3c  0);.    if( len<
edc70 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72  0 ){.      *pErr
edc80 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  or = errno;.    
edc90 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
edca0 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d  ERR_WRITE;.    }
edcb0 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c 48 4f 53  else if( len<HOS
edcc0 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20 20 20 20  TIDLEN ){.      
edcd0 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20  *pError = 0;.   
edce0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
edcf0 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20 20 20  OERR_WRITE;.    
edd00 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b  }.    close(fd);
edd10 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61   /* silently lea
edd20 6b 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66  k the fd if it f
edd30 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52  ails */.    OSTR
edd40 41 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 20  ACE3("GETHOSTID 
edd50 20 77 72 6f 74 65 20 25 73 20 70 69 64 3d 25 64   wrote %s pid=%d
edd60 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65  \n", pHostID, ge
edd70 74 70 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74  tpid());.    ret
edd80 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 73  urn rc;.  }.}..s
edd90 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 47  tatic int proxyG
edda0 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f 6e 73 74  etLockPath(const
eddb0 20 63 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63   char *dbPath, c
eddc0 68 61 72 20 2a 6c 50 61 74 68 2c 20 73 69 7a 65  har *lPath, size
eddd0 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20 20 69 6e  _t maxLen){.  in
edde0 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 64 62 4c  t len;.  int dbL
eddf0 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 23 69  en;.  int i;..#i
ede00 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58 59 44 49  fdef LOCKPROXYDI
ede10 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63 70  R.  len = strlcp
ede20 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b 50 52 4f  y(lPath, LOCKPRO
ede30 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e 29 3b 0a  XYDIR, maxLen);.
ede40 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 5f 43  #else.# ifdef _C
ede50 53 5f 44 41 52 57 49 4e 5f 55 53 45 52 5f 54 45  S_DARWIN_USER_TE
ede60 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20 20 20 63  MP_DIR.  {.    c
ede70 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49  onfstr(_CS_DARWI
ede80 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c  N_USER_TEMP_DIR,
ede90 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65 6e 29 3b   lPath, maxLen);
edea0 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74 72 6c 63  .    len = strlc
edeb0 61 74 28 6c 50 61 74 68 2c 20 22 73 71 6c 69 74  at(lPath, "sqlit
edec0 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78 4c 65 6e  eplocks", maxLen
eded0 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b 64 69 72  );.    if( mkdir
edee0 28 6c 50 61 74 68 2c 20 53 51 4c 49 54 45 5f 44  (lPath, SQLITE_D
edef0 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f  EFAULT_PROXYDIR_
edf00 50 45 52 4d 49 53 53 49 4f 4e 53 29 20 29 7b 0a  PERMISSIONS) ){.
edf10 20 20 20 20 20 20 2f 2a 20 69 66 20 6d 6b 64 69        /* if mkdi
edf20 72 20 66 61 69 6c 73 2c 20 68 61 6e 64 6c 65 20  r fails, handle 
edf30 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20 63 72 65  as lock file cre
edf40 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 2a 2f  ation failure */
edf50 0a 23 20 20 69 66 64 65 66 20 53 51 4c 49 54 45  .#  ifdef SQLITE
edf60 5f 44 45 42 55 47 0a 20 20 20 20 20 20 69 6e 74  _DEBUG.      int
edf70 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b 0a 20 20   err = errno;.  
edf80 20 20 20 20 69 66 28 20 65 72 72 21 3d 45 45 58      if( err!=EEX
edf90 49 53 54 20 29 7b 0a 20 20 20 20 20 20 20 20 66  IST ){.        f
edfa0 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
edfb0 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50 61 74 68  proxyGetLockPath
edfc0 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25 6f 29 20  : mkdir(%s,0%o) 
edfd0 65 72 72 6f 72 20 25 64 20 25 73 5c 6e 22 2c 20  error %d %s\n", 
edfe0 6c 50 61 74 68 2c 0a 20 20 20 20 20 20 20 20 20  lPath,.         
edff0 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45         SQLITE_DE
ee000 46 41 55 4c 54 5f 50 52 4f 58 59 44 49 52 5f 50  FAULT_PROXYDIR_P
ee010 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65 72 72 2c  ERMISSIONS, err,
ee020 20 73 74 72 65 72 72 6f 72 28 65 72 72 29 29 3b   strerror(err));
ee030 0a 20 20 20 20 20 20 7d 0a 23 20 20 65 6e 64 69  .      }.#  endi
ee040 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  f.    }else{.   
ee050 20 20 20 4f 53 54 52 41 43 45 33 28 22 47 45 54     OSTRACE3("GET
ee060 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64 69 72 20  LOCKPATH  mkdir 
ee070 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 6c 50  %s pid=%d\n", lP
ee080 61 74 68 2c 20 67 65 74 70 69 64 28 29 29 3b 0a  ath, getpid());.
ee090 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 7d 0a 23      }.    .  }.#
ee0a0 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d 20 73 74   else.  len = st
ee0b0 72 6c 63 70 79 28 6c 50 61 74 68 2c 20 22 2f 74  rlcpy(lPath, "/t
ee0c0 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b 0a 23  mp/", maxLen);.#
ee0d0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20   endif.#endif.. 
ee0e0 20 69 66 28 20 6c 50 61 74 68 5b 6c 65 6e 2d 31   if( lPath[len-1
ee0f0 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 6c 65  ]!='/' ){.    le
ee100 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c 50 61 74  n = strlcat(lPat
ee110 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65 6e 29 3b  h, "/", maxLen);
ee120 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 74 72 61  .  }.  .  /* tra
ee130 6e 73 66 6f 72 6d 20 74 68 65 20 64 62 20 70 61  nsform the db pa
ee140 74 68 20 74 6f 20 61 20 75 6e 69 71 75 65 20 63  th to a unique c
ee150 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 64  ache name */.  d
ee160 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73 74 72 6c  bLen = (int)strl
ee170 65 6e 28 64 62 50 61 74 68 29 3b 0a 20 20 66 6f  en(dbPath);.  fo
ee180 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c 65 6e 20  r( i=0; i<dbLen 
ee190 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c 6d 61 78  && (i+len+7)<max
ee1a0 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  Len; i++){.    c
ee1b0 68 61 72 20 63 20 3d 20 64 62 50 61 74 68 5b 69  har c = dbPath[i
ee1c0 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b 69 2b 6c  ];.    lPath[i+l
ee1d0 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27 29 3f 27  en] = (c=='/')?'
ee1e0 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c 50 61 74  _':c;.  }.  lPat
ee1f0 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27 3b 0a 20  h[i+len]='\0';. 
ee200 20 73 74 72 6c 63 61 74 28 6c 50 61 74 68 2c 20   strlcat(lPath, 
ee210 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78 4c 65 6e  ":auto:", maxLen
ee220 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
ee230 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
ee240 43 72 65 61 74 65 20 61 20 6e 65 77 20 56 46 53  Create a new VFS
ee250 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
ee260 20 28 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f   (stored in memo
ee270 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
ee280 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  .** sqlite3_mall
ee290 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20 74 68 65  oc) and open the
ee2a0 20 66 69 6c 65 20 6e 61 6d 65 64 20 22 70 61 74   file named "pat
ee2b0 68 22 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64  h" in the file d
ee2c0 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a 0a 2a 2a  escriptor..**.**
ee2d0 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72   The caller is r
ee2e0 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f 74 20 6f  esponsible not o
ee2f0 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69 6e 67 20  nly for closing 
ee300 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
ee310 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c 73 6f 20  tor.** but also 
ee320 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68 65 20  for freeing the 
ee330 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
ee340 64 20 77 69 74 68 20 74 68 65 20 66 69 6c 65 20  d with the file 
ee350 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2f 0a 73  descriptor..*/.s
ee360 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43  tatic int proxyC
ee370 72 65 61 74 65 55 6e 69 78 46 69 6c 65 28 63 6f  reateUnixFile(co
ee380 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c 20  nst char *path, 
ee390 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70 46 69 6c  unixFile **ppFil
ee3a0 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  e) {.  unixFile 
ee3b0 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 66 6c 61  *pNew;.  int fla
ee3c0 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs = SQLITE_OPEN
ee3d0 5f 4d 41 49 4e 5f 44 42 7c 53 51 4c 49 54 45 5f  _MAIN_DB|SQLITE_
ee3e0 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c 49  OPEN_CREATE|SQLI
ee3f0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
ee400 45 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  E;.  int rc = SQ
ee410 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74  LITE_OK;.  sqlit
ee420 65 33 5f 76 66 73 20 64 75 6d 6d 79 56 66 73 3b  e3_vfs dummyVfs;
ee430 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75 6e 69 78  ..  pNew = (unix
ee440 46 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d  File *)sqlite3_m
ee450 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 75 6e 69  alloc(sizeof(uni
ee460 78 46 69 6c 65 29 29 3b 0a 20 20 69 66 28 20 21  xFile));.  if( !
ee470 70 4e 65 77 20 29 7b 0a 20 20 20 20 72 65 74 75  pNew ){.    retu
ee480 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
ee490 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 4e  .  }.  memset(pN
ee4a0 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 6e  ew, 0, sizeof(un
ee4b0 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20 2f 2a 20  ixFile));..  /* 
ee4c0 43 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 28 29 20  Call unixOpen() 
ee4d0 74 6f 20 6f 70 65 6e 20 74 68 65 20 70 72 6f 78  to open the prox
ee4e0 79 20 66 69 6c 65 2e 20 54 68 65 20 66 6c 61 67  y file. The flag
ee4f0 73 20 70 61 73 73 65 64 20 74 6f 20 75 6e 69 78  s passed to unix
ee500 4f 70 65 6e 28 29 0a 20 20 2a 2a 20 73 75 67 67  Open().  ** sugg
ee510 65 73 74 20 74 68 61 74 20 74 68 65 20 66 69 6c  est that the fil
ee520 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 69  e being opened i
ee530 73 20 61 20 22 6d 61 69 6e 20 64 61 74 61 62 61  s a "main databa
ee540 73 65 22 2e 20 54 68 69 73 20 69 73 0a 20 20 2a  se". This is.  *
ee550 2a 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 6f  * necessary as o
ee560 74 68 65 72 20 66 69 6c 65 20 74 79 70 65 73 20  ther file types 
ee570 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  do not necessari
ee580 6c 79 20 73 75 70 70 6f 72 74 20 6c 6f 63 6b 69  ly support locki
ee590 6e 67 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 62  ng. It.  ** is b
ee5a0 65 74 74 65 72 20 74 6f 20 75 73 65 20 75 6e 69  etter to use uni
ee5b0 78 4f 70 65 6e 28 29 20 69 6e 73 74 65 61 64 20  xOpen() instead 
ee5c0 6f 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66  of opening the f
ee5d0 69 6c 65 20 64 69 72 65 63 74 6c 79 20 77 69 74  ile directly wit
ee5e0 68 0a 20 20 2a 2a 20 6f 70 65 6e 28 29 2c 20 61  h.  ** open(), a
ee5f0 73 20 75 6e 69 78 4f 70 65 6e 28 29 20 73 65 74  s unixOpen() set
ee600 73 20 75 70 20 74 68 65 20 76 61 72 69 6f 75 73  s up the various
ee610 20 6d 65 63 68 61 6e 69 73 6d 73 20 72 65 71 75   mechanisms requ
ee620 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 6d 61 6b  ired to.  ** mak
ee630 65 20 73 75 72 65 20 61 20 63 61 6c 6c 20 74 6f  e sure a call to
ee640 20 63 6c 6f 73 65 28 29 20 64 6f 65 73 20 6e 6f   close() does no
ee650 74 20 63 61 75 73 65 20 74 68 65 20 73 79 73 74  t cause the syst
ee660 65 6d 20 74 6f 20 64 69 73 63 61 72 64 0a 20 20  em to discard.  
ee670 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 70  ** POSIX locks p
ee680 72 65 6d 61 74 75 72 65 6c 79 2e 0a 20 20 2a 2a  rematurely..  **
ee690 0a 20 20 2a 2a 20 49 74 20 69 73 20 69 6d 70 6f  .  ** It is impo
ee6a0 72 74 61 6e 74 20 74 68 61 74 20 74 68 65 20 78  rtant that the x
ee6b0 4f 70 65 6e 20 6d 65 6d 62 65 72 20 6f 66 20 74  Open member of t
ee6c0 68 65 20 56 46 53 20 6f 62 6a 65 63 74 20 70 61  he VFS object pa
ee6d0 73 73 65 64 20 74 6f 20 0a 20 20 2a 2a 20 75 6e  ssed to .  ** un
ee6e0 69 78 4f 70 65 6e 28 29 20 69 73 20 4e 55 4c 4c  ixOpen() is NULL
ee6f0 2e 20 54 68 69 73 20 74 65 6c 6c 73 20 75 6e 69  . This tells uni
ee700 78 4f 70 65 6e 28 29 20 6d 61 79 20 74 72 79 20  xOpen() may try 
ee710 74 6f 20 6f 70 65 6e 20 61 20 70 72 6f 78 79 2d  to open a proxy-
ee720 66 69 6c 65 20 0a 20 20 2a 2a 20 66 6f 72 20 74  file .  ** for t
ee730 68 65 20 70 72 6f 78 79 2d 66 69 6c 65 20 28 63  he proxy-file (c
ee740 72 65 61 74 69 6e 67 20 61 20 70 6f 74 65 6e 74  reating a potent
ee750 69 61 6c 20 69 6e 66 69 6e 69 74 65 20 6c 6f 6f  ial infinite loo
ee760 70 29 2e 0a 20 20 2a 2f 0a 20 20 64 75 6d 6d 79  p)..  */.  dummy
ee770 56 66 73 2e 70 41 70 70 44 61 74 61 20 3d 20 28  Vfs.pAppData = (
ee780 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f 63 6b 49  void*)&autolockI
ee790 6f 46 69 6e 64 65 72 3b 0a 20 20 64 75 6d 6d 79  oFinder;.  dummy
ee7a0 56 66 73 2e 78 4f 70 65 6e 20 3d 20 30 3b 0a 20  Vfs.xOpen = 0;. 
ee7b0 20 72 63 20 3d 20 75 6e 69 78 4f 70 65 6e 28 26   rc = unixOpen(&
ee7c0 64 75 6d 6d 79 56 66 73 2c 20 70 61 74 68 2c 20  dummyVfs, path, 
ee7d0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
ee7e0 70 4e 65 77 2c 20 66 6c 61 67 73 2c 20 26 66 6c  pNew, flags, &fl
ee7f0 61 67 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ags);.  if( rc==
ee800 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 66 6c  SQLITE_OK && (fl
ee810 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ags&SQLITE_OPEN_
ee820 52 45 41 44 4f 4e 4c 59 29 20 29 7b 0a 20 20 20  READONLY) ){.   
ee830 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 2d 3e   pNew->pMethod->
ee840 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f  xClose((sqlite3_
ee850 66 69 6c 65 20 2a 29 70 4e 65 77 29 3b 0a 20 20  file *)pNew);.  
ee860 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41    rc = SQLITE_CA
ee870 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a 20 20 69  NTOPEN;.  }..  i
ee880 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
ee890 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
ee8a0 66 72 65 65 28 70 4e 65 77 29 3b 0a 20 20 20 20  free(pNew);.    
ee8b0 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  pNew = 0;.  }.. 
ee8c0 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e 65 77 3b   *ppFile = pNew;
ee8d0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
ee8e0 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65 20 63 6f  ./* takes the co
ee8f0 6e 63 68 20 62 79 20 74 61 6b 69 6e 67 20 61 20  nch by taking a 
ee900 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20  shared lock and 
ee910 72 65 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74  read the content
ee920 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a 2a 2a 20  s conch, if .** 
ee930 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e 6f 6e 2d  lockPath is non-
ee940 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73 74 20 49  NULL, the host I
ee950 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69 6c 65 20  D and lock file 
ee960 70 61 74 68 20 6d 75 73 74 20 6d 61 74 63 68 2e  path must match.
ee970 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20 6c 6f 63    A NULL .** loc
ee980 6b 50 61 74 68 20 6d 65 61 6e 73 20 74 68 61 74  kPath means that
ee990 20 74 68 65 20 6c 6f 63 6b 50 61 74 68 20 69 6e   the lockPath in
ee9a0 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
ee9b0 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 66 20  will be used if 
ee9c0 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20 49 44 73  the .** host IDs
ee9d0 20 6d 61 74 63 68 2c 20 6f 72 20 61 20 6e 65 77   match, or a new
ee9e0 20 6c 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c 20   lock path will 
ee9f0 62 65 20 67 65 6e 65 72 61 74 65 64 20 61 75 74  be generated aut
eea00 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 61  omatically .** a
eea10 6e 64 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  nd written to th
eea20 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2f  e conch file..*/
eea30 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78  .static int prox
eea40 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e 69 78 46  yTakeConch(unixF
eea50 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 70  ile *pFile){.  p
eea60 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
eea70 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78  xt *pCtx = (prox
eea80 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
eea90 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
eeaa0 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a 20 20 69  Context; .  .  i
eeab0 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65  f( pCtx->conchHe
eeac0 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75  ld>0 ){.    retu
eead0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
eeae0 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46  }else{.    unixF
eeaf0 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 20 3d  ile *conchFile =
eeb00 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65   pCtx->conchFile
eeb10 3b 0a 20 20 20 20 63 68 61 72 20 74 65 73 74 56  ;.    char testV
eeb20 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a  alue[CONCHLEN];.
eeb30 20 20 20 20 63 68 61 72 20 63 6f 6e 63 68 56 61      char conchVa
eeb40 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d 3b 0a 20  lue[CONCHLEN];. 
eeb50 20 20 20 63 68 61 72 20 6c 6f 63 6b 50 61 74 68     char lockPath
eeb60 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b 0a 20 20  [MAXPATHLEN];.  
eeb70 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b 50 61 74    char *tLockPat
eeb80 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e  h = NULL;.    in
eeb90 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
eeba0 3b 0a 20 20 20 20 69 6e 74 20 72 65 61 64 52 63  ;.    int readRc
eebb0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
eebc0 20 20 69 6e 74 20 73 79 6e 63 50 65 72 6d 73 20    int syncPerms 
eebd0 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54 52 41 43  = 0;..    OSTRAC
eebe0 45 34 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 25  E4("TAKECONCH  %
eebf0 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25 64 5c  d for %s pid=%d\
eec00 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68  n", conchFile->h
eec10 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28  ,.             (
eec20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50  pCtx->lockProxyP
eec30 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c 6f 63 6b  ath ? pCtx->lock
eec40 50 72 6f 78 79 50 61 74 68 20 3a 20 22 3a 61 75  ProxyPath : ":au
eec50 74 6f 3a 22 29 2c 20 67 65 74 70 69 64 28 29 29  to:"), getpid())
eec60 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63 6f 6e 63  ;..    rc = conc
eec70 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e  hFile->pMethod->
eec80 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66  xLock((sqlite3_f
eec90 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20  ile*)conchFile, 
eeca0 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20  SHARED_LOCK);.  
eecb0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
eecc0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  _OK ){.      int
eecd0 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20   pError = 0;.   
eece0 20 20 20 6d 65 6d 73 65 74 28 74 65 73 74 56 61     memset(testVa
eecf0 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48 4c 45 4e  lue, 0, CONCHLEN
eed00 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69 73 20 66  ); /* conch is f
eed10 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a 20 20 20  ixed size */.   
eed20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 47 65 74     rc = proxyGet
eed30 48 6f 73 74 49 44 28 74 65 73 74 56 61 6c 75 65  HostID(testValue
eed40 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20 20 20 20  , &pError);.    
eed50 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d    if( (rc&0xff)=
eed60 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b  =SQLITE_IOERR ){
eed70 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
eed80 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70 45 72 72  lastErrno = pErr
eed90 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  or;.      }.    
eeda0 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63 6b    if( pCtx->lock
eedb0 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20  ProxyPath ){.   
eedc0 20 20 20 20 20 73 74 72 6c 63 70 79 28 26 74 65       strlcpy(&te
eedd0 73 74 56 61 6c 75 65 5b 48 4f 53 54 49 44 4c 45  stValue[HOSTIDLE
eede0 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72  N], pCtx->lockPr
eedf0 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48  oxyPath, MAXPATH
eee00 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  LEN);.      }.  
eee10 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d    }.    if( rc!=
eee20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
eee30 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65     goto end_take
eee40 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a 20 20 20  conch;.    }.   
eee50 20 0a 20 20 20 20 72 65 61 64 52 63 20 3d 20 75   .    readRc = u
eee60 6e 69 78 52 65 61 64 28 28 73 71 6c 69 74 65 33  nixRead((sqlite3
eee70 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c  _file *)conchFil
eee80 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43  e, conchValue, C
eee90 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20  ONCHLEN, 0);.   
eeea0 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c   if( readRc!=SQL
eeeb0 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f  ITE_IOERR_SHORT_
eeec0 52 45 41 44 20 29 7b 0a 20 20 20 20 20 20 69 66  READ ){.      if
eeed0 28 20 72 65 61 64 52 63 21 3d 53 51 4c 49 54 45  ( readRc!=SQLITE
eeee0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69  _OK ){.        i
eeef0 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51  f( (rc&0xff)==SQ
eef00 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20  LITE_IOERR ){.  
eef10 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
eef20 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f 6e 63 68  astErrno = conch
eef30 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b  File->lastErrno;
eef40 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
eef50 20 20 20 72 63 20 3d 20 72 65 61 64 52 63 3b 0a     rc = readRc;.
eef60 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
eef70 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20  _takeconch;.    
eef80 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 69 66 20    }.      /* if 
eef90 74 68 65 20 63 6f 6e 63 68 20 68 61 73 20 64 61  the conch has da
eefa0 74 61 20 63 6f 6d 70 61 72 65 20 74 68 65 20 63  ta compare the c
eefb0 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20  ontents */.     
eefc0 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63 6b   if( !pCtx->lock
eefd0 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20 20  ProxyPath ){.   
eefe0 20 20 20 20 20 2f 2a 20 66 6f 72 20 61 75 74 6f       /* for auto
eeff0 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20 6c 6f 63  -named local loc
ef000 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20 63 68 65  k file, just che
ef010 63 6b 20 74 68 65 20 68 6f 73 74 20 49 44 20 61  ck the host ID a
ef020 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20 20 20 20  nd we'll.       
ef030 20 20 2a 2a 20 75 73 65 20 74 68 65 20 6c 6f 63    ** use the loc
ef040 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74  al lock file pat
ef050 68 20 74 68 61 74 27 73 20 61 6c 72 65 61 64 79  h that's already
ef060 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a 20 20 20   in there */.   
ef070 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d 70       if( !memcmp
ef080 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e 63  (testValue, conc
ef090 68 56 61 6c 75 65 2c 20 48 4f 53 54 49 44 4c 45  hValue, HOSTIDLE
ef0a0 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  N) ){.          
ef0b0 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28 63 68 61  tLockPath = (cha
ef0c0 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c 75 65 5b  r *)&conchValue[
ef0d0 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20 20 20 20  HOSTIDLEN];.    
ef0e0 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74        goto end_t
ef0f0 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20  akeconch;.      
ef100 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
ef110 0a 20 20 20 20 20 20 20 20 2f 2a 20 77 65 27 76  .        /* we'v
ef120 65 20 67 6f 74 20 74 68 65 20 63 6f 6e 63 68 20  e got the conch 
ef130 69 66 20 63 6f 6e 63 68 56 61 6c 75 65 20 6d 61  if conchValue ma
ef140 74 63 68 65 73 20 6f 75 72 20 70 61 74 68 20 61  tches our path a
ef150 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f 0a 20 20  nd host ID */.  
ef160 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d 63 6d        if( !memcm
ef170 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63 6f 6e  p(testValue, con
ef180 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43 48 4c 45  chValue, CONCHLE
ef190 4e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  N) ){.          
ef1a0 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65 63 6f 6e  goto end_takecon
ef1b0 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ch;.        }.  
ef1c0 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
ef1d0 0a 20 20 20 20 20 20 2f 2a 20 61 20 73 68 6f 72  .      /* a shor
ef1e0 74 20 72 65 61 64 20 6d 65 61 6e 73 20 77 65 27  t read means we'
ef1f0 72 65 20 22 63 72 65 61 74 69 6e 67 22 20 74 68  re "creating" th
ef200 65 20 63 6f 6e 63 68 20 28 65 76 65 6e 20 74 68  e conch (even th
ef210 6f 75 67 68 20 69 74 20 63 6f 75 6c 64 20 0a 20  ough it could . 
ef220 20 20 20 20 20 2a 2a 20 68 61 76 65 20 62 65 65       ** have bee
ef230 6e 20 75 73 65 72 2d 69 6e 74 65 72 76 65 6e 74  n user-intervent
ef240 69 6f 6e 29 2c 20 69 66 20 77 65 20 61 63 71 75  ion), if we acqu
ef250 69 72 65 20 74 68 65 20 65 78 63 6c 75 73 69 76  ire the exclusiv
ef260 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 2a 2a  e lock,.      **
ef270 20 77 65 27 6c 6c 20 74 72 79 20 74 6f 20 6d 61   we'll try to ma
ef280 74 63 68 20 74 68 65 20 63 75 72 72 65 6e 74 20  tch the current 
ef290 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69 73 73 69  on-disk permissi
ef2a0 6f 6e 73 20 6f 66 20 74 68 65 20 64 61 74 61 62  ons of the datab
ef2b0 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ase.      */.   
ef2c0 20 20 20 73 79 6e 63 50 65 72 6d 73 20 3d 20 31     syncPerms = 1
ef2d0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20  ;.    }.    .   
ef2e0 20 2f 2a 20 65 69 74 68 65 72 20 63 6f 6e 63 68   /* either conch
ef2f0 20 77 61 73 20 65 6d 74 70 79 20 6f 72 20 64 69   was emtpy or di
ef300 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20  dn't match */.  
ef310 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c 6f 63    if( !pCtx->loc
ef320 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20  kProxyPath ){.  
ef330 20 20 20 20 70 72 6f 78 79 47 65 74 4c 6f 63 6b      proxyGetLock
ef340 50 61 74 68 28 70 43 74 78 2d 3e 64 62 50 61 74  Path(pCtx->dbPat
ef350 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20 4d 41 58  h, lockPath, MAX
ef360 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20  PATHLEN);.      
ef370 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c 6f 63 6b  tLockPath = lock
ef380 50 61 74 68 3b 0a 20 20 20 20 20 20 73 74 72 6c  Path;.      strl
ef390 63 70 79 28 26 74 65 73 74 56 61 6c 75 65 5b 48  cpy(&testValue[H
ef3a0 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f 63 6b 50  OSTIDLEN], lockP
ef3b0 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29  ath, MAXPATHLEN)
ef3c0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20 20  ;.    }.    .   
ef3d0 20 2f 2a 20 75 70 64 61 74 65 20 63 6f 6e 63 68   /* update conch
ef3e0 20 77 69 74 68 20 68 6f 73 74 20 61 6e 64 20 70   with host and p
ef3f0 61 74 68 20 28 74 68 69 73 20 77 69 6c 6c 20 66  ath (this will f
ef400 61 69 6c 20 69 66 20 6f 74 68 65 72 20 70 72 6f  ail if other pro
ef410 63 65 73 73 0a 20 20 20 20 20 2a 2a 20 68 61 73  cess.     ** has
ef420 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61   a shared lock a
ef430 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20 20 20 72  lready) */.    r
ef440 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70  c = conchFile->p
ef450 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73  Method->xLock((s
ef460 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e  qlite3_file*)con
ef470 63 68 46 69 6c 65 2c 20 45 58 43 4c 55 53 49 56  chFile, EXCLUSIV
ef480 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28  E_LOCK);.    if(
ef490 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
ef4a0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 6e 69  {.      rc = uni
ef4b0 78 57 72 69 74 65 28 28 73 71 6c 69 74 65 33 5f  xWrite((sqlite3_
ef4c0 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46 69 6c 65  file *)conchFile
ef4d0 2c 20 74 65 73 74 56 61 6c 75 65 2c 20 43 4f 4e  , testValue, CON
ef4e0 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20 20 20 20  CHLEN, 0);.     
ef4f0 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
ef500 4f 4b 20 26 26 20 73 79 6e 63 50 65 72 6d 73 20  OK && syncPerms 
ef510 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63  ){.        struc
ef520 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 20 20  t stat buf;.    
ef530 20 20 20 20 69 6e 74 20 65 72 72 20 3d 20 66 73      int err = fs
ef540 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c 20 26 62  tat(pFile->h, &b
ef550 75 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  uf);.        if(
ef560 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20   err==0 ){.     
ef570 20 20 20 20 20 2f 2a 20 74 72 79 20 74 6f 20 6d       /* try to m
ef580 61 74 63 68 20 74 68 65 20 64 61 74 61 62 61 73  atch the databas
ef590 65 20 66 69 6c 65 20 70 65 72 6d 69 73 73 69 6f  e file permissio
ef5a0 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61 69 6c 75  ns, ignore failu
ef5b0 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  re */.#ifndef SQ
ef5c0 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55 47  LITE_PROXY_DEBUG
ef5d0 0a 20 20 20 20 20 20 20 20 20 20 66 63 68 6d 6f  .          fchmo
ef5e0 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c 20  d(conchFile->h, 
ef5f0 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 23 65  buf.st_mode);.#e
ef600 6c 73 65 0a 20 20 20 20 20 20 20 20 20 20 69 66  lse.          if
ef610 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63 68 46 69  ( fchmod(conchFi
ef620 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74 5f 6d 6f  le->h, buf.st_mo
ef630 64 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  de)!=0 ){.      
ef640 20 20 20 20 20 20 69 6e 74 20 63 6f 64 65 20 3d        int code =
ef650 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20   errno;.        
ef660 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65      fprintf(stde
ef670 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20 46  rr, "fchmod %o F
ef680 41 49 4c 45 44 20 77 69 74 68 20 25 64 20 25 73  AILED with %d %s
ef690 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
ef6a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef6b0 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65 2c 20 63    buf.st_mode, c
ef6c0 6f 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 6f  ode, strerror(co
ef6d0 64 65 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  de));.          
ef6e0 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20  } else {.       
ef6f0 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
ef700 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25 6f 20  err, "fchmod %o 
ef710 53 55 43 43 45 44 45 44 5c 6e 22 2c 62 75 66 2e  SUCCEDED\n",buf.
ef720 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20 20 20 20  st_mode);.      
ef730 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65      }.        }e
ef740 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69  lse{.          i
ef750 6e 74 20 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b  nt code = errno;
ef760 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69 6e  .          fprin
ef770 74 66 28 73 74 64 65 72 72 2c 20 22 53 54 41 54  tf(stderr, "STAT
ef780 20 46 41 49 4c 45 44 5b 25 64 5d 20 77 69 74 68   FAILED[%d] with
ef790 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20 20 20 20   %d %s\n", .    
ef7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef7b0 20 20 20 20 20 20 65 72 72 2c 20 63 6f 64 65 2c        err, code,
ef7c0 20 73 74 72 65 72 72 6f 72 28 63 6f 64 65 29 29   strerror(code))
ef7d0 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ;.#endif.       
ef7e0 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
ef7f0 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e  .    conchFile->
ef800 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b  pMethod->xUnlock
ef810 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
ef820 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48 41 52 45  conchFile, SHARE
ef830 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65 6e 64 5f  D_LOCK);.  .end_
ef840 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20 20 20 4f  takeconch:.    O
ef850 53 54 52 41 43 45 32 28 22 54 52 41 4e 53 50 52  STRACE2("TRANSPR
ef860 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25 64 5c 6e  OXY: CLOSE  %d\n
ef870 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20  ", pFile->h);.  
ef880 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
ef890 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6f 70  _OK && pFile->op
ef8a0 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 20  enFlags ){.     
ef8b0 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e 3d 30   if( pFile->h>=0
ef8c0 20 29 7b 0a 23 69 66 64 65 66 20 53 54 52 49 43   ){.#ifdef STRIC
ef8d0 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52 0a 20 20  T_CLOSE_ERROR.  
ef8e0 20 20 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28        if( close(
ef8f0 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20  pFile->h) ){.   
ef900 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
ef910 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
ef920 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
ef930 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  n SQLITE_IOERR_C
ef940 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a  LOSE;.        }.
ef950 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 63 6c  #else.        cl
ef960 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29 3b 20 2f  ose(pFile->h); /
ef970 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20  * silently leak 
ef980 66 64 20 69 66 20 66 61 69 6c 20 2a 2f 0a 23 65  fd if fail */.#e
ef990 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a 20 20 20  ndif.      }.   
ef9a0 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31     pFile->h = -1
ef9b0 3b 0a 20 20 20 20 20 20 69 6e 74 20 66 64 20 3d  ;.      int fd =
ef9c0 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64 62 50 61   open(pCtx->dbPa
ef9d0 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70 65 6e 46  th, pFile->openF
ef9e0 6c 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20  lags,.          
ef9f0 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
efa00 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45  _DEFAULT_FILE_PE
efa10 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20 20 20  RMISSIONS);.    
efa20 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e    OSTRACE2("TRAN
efa30 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20 20 25 64  SPROXY: OPEN  %d
efa40 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20 20 20 20  \n", fd);.      
efa50 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20  if( fd>=0 ){.   
efa60 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20       pFile->h = 
efa70 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  fd;.      }else{
efa80 0a 20 20 20 20 20 20 20 20 72 63 3d 53 51 4c 49  .        rc=SQLI
efa90 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20 2f 2a 20  TE_CANTOPEN; /* 
efaa0 53 51 4c 49 54 45 5f 42 55 53 59 3f 20 70 72 6f  SQLITE_BUSY? pro
efab0 78 79 54 61 6b 65 43 6f 6e 63 68 20 63 61 6c 6c  xyTakeConch call
efac0 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ed.             
efad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
efae0 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b 69 6e 67    during locking
efaf0 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   */.      }.    
efb00 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
efb10 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 43 74 78  LITE_OK && !pCtx
efb20 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20  ->lockProxy ){. 
efb30 20 20 20 20 20 63 68 61 72 20 2a 70 61 74 68 20       char *path 
efb40 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f 20 74 4c  = tLockPath ? tL
efb50 6f 63 6b 50 61 74 68 20 3a 20 70 43 74 78 2d 3e  ockPath : pCtx->
efb60 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20  lockProxyPath;. 
efb70 20 20 20 20 20 2f 2a 20 41 43 53 3a 20 4e 65 65       /* ACS: Nee
efb80 64 20 74 6f 20 6d 61 6b 65 20 61 20 63 6f 70 79  d to make a copy
efb90 20 6f 66 20 70 61 74 68 20 73 6f 6d 65 74 69 6d   of path sometim
efba0 65 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d  es */.      rc =
efbb0 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e 69 78   proxyCreateUnix
efbc0 46 69 6c 65 28 70 61 74 68 2c 20 26 70 43 74 78  File(path, &pCtx
efbd0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20  ->lockProxy);.  
efbe0 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
efbf0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
efc00 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65     pCtx->conchHe
efc10 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69  ld = 1;..      i
efc20 66 28 20 74 4c 6f 63 6b 50 61 74 68 20 29 7b 0a  f( tLockPath ){.
efc30 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f          pCtx->lo
efc40 63 6b 50 72 6f 78 79 50 61 74 68 20 3d 20 73 71  ckProxyPath = sq
efc50 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
efc60 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a 20 20 20   tLockPath);.   
efc70 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c       if( pCtx->l
efc80 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f  ockProxy->pMetho
efc90 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68 6f  d == &afpIoMetho
efca0 64 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ds ){.          
efcb0 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74  ((afpLockingCont
efcc0 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c 6f 63 6b  ext *)pCtx->lock
efcd0 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  Proxy->lockingCo
efce0 6e 74 65 78 74 29 2d 3e 64 62 50 61 74 68 20 3d  ntext)->dbPath =
efcf0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
efd00 20 20 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b        pCtx->lock
efd10 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 20 20 20  ProxyPath;.     
efd20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
efd30 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
efd40 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68  conchFile->pMeth
efd50 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c  od->xUnlock((sql
efd60 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68  ite3_file*)conch
efd70 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a  File, NO_LOCK);.
efd80 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43      }.    OSTRAC
efd90 45 33 28 22 54 41 4b 45 43 4f 4e 43 48 20 20 25  E3("TAKECONCH  %
efda0 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69  d %s\n", conchFi
efdb0 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51 4c 49 54  le->h, rc==SQLIT
efdc0 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61 69 6c 65  E_OK?"ok":"faile
efdd0 64 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  d");.    return 
efde0 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  rc;.  }.}../*.**
efdf0 20 49 66 20 70 46 69 6c 65 20 68 6f 6c 64 73 20   If pFile holds 
efe00 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63 6f 6e 63  a lock on a conc
efe10 68 20 66 69 6c 65 2c 20 74 68 65 6e 20 72 65 6c  h file, then rel
efe20 65 61 73 65 20 74 68 61 74 20 6c 6f 63 6b 2e 0a  ease that lock..
efe30 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  */.static int pr
efe40 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 28  oxyReleaseConch(
efe50 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29  unixFile *pFile)
efe60 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
efe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
efe80 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65 20 72 65  /* Subroutine re
efe90 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 70  turn code */.  p
efea0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
efeb0 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a 20 54 68  xt *pCtx;  /* Th
efec0 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78  e locking contex
efed0 74 20 66 6f 72 20 74 68 65 20 70 72 6f 78 79 20  t for the proxy 
efee0 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69 78 46 69  lock */.  unixFi
efef0 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20  le *conchFile;  
eff00 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
eff10 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
eff20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20 28 70 72  */..  pCtx = (pr
eff30 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
eff40 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69  t *)pFile->locki
eff50 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 63 6f 6e  ngContext;.  con
eff60 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63  chFile = pCtx->c
eff70 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f 53 54 52  onchFile;.  OSTR
eff80 41 43 45 34 28 22 52 45 4c 45 41 53 45 43 4f 4e  ACE4("RELEASECON
eff90 43 48 20 20 25 64 20 66 6f 72 20 25 73 20 70 69  CH  %d for %s pi
effa0 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69  d=%d\n", conchFi
effb0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
effc0 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f    (pCtx->lockPro
effd0 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c  xyPath ? pCtx->l
effe0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22  ockProxyPath : "
efff0 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20 20 20 20  :auto:"), .     
f0000 20 20 20 20 20 20 67 65 74 70 69 64 28 29 29 3b        getpid());
f0010 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65  .  pCtx->conchHe
f0020 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 63  ld = 0;.  rc = c
f0030 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  onchFile->pMetho
f0040 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73 71 6c 69  d->xUnlock((sqli
f0050 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46  te3_file*)conchF
f0060 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  ile, NO_LOCK);. 
f0070 20 4f 53 54 52 41 43 45 33 28 22 52 45 4c 45 41   OSTRACE3("RELEA
f0080 53 45 43 4f 4e 43 48 20 20 25 64 20 25 73 5c 6e  SECONCH  %d %s\n
f0090 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 68 2c  ", conchFile->h,
f00a0 0a 20 20 20 20 20 20 20 20 20 20 20 28 72 63 3d  .           (rc=
f00b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b  =SQLITE_OK ? "ok
f00c0 22 20 3a 20 22 66 61 69 6c 65 64 22 29 29 3b 0a  " : "failed"));.
f00d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
f00e0 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20  /*.** Given the 
f00f0 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61  name of a databa
f0100 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70 75 74 65  se file, compute
f0110 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 69 74 73   the name of its
f0120 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a 2a 2a 20   conch file..** 
f0130 53 74 6f 72 65 20 74 68 65 20 63 6f 6e 63 68 20  Store the conch 
f0140 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d 65 6d 6f  filename in memo
f0150 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
f0160 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
f0170 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70 43 6f 6e  )..** Make *pCon
f0180 63 68 50 61 74 68 20 70 6f 69 6e 74 20 74 6f 20  chPath point to 
f0190 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e 20 20 52  the new name.  R
f01a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20  eturn SQLITE_OK 
f01b0 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72  on success.** or
f01c0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66   SQLITE_NOMEM if
f01d0 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69   unable to obtai
f01e0 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20  n memory..**.** 
f01f0 54 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65  The caller is re
f0200 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 65 6e  sponsible for en
f0210 73 75 72 69 6e 67 20 74 68 61 74 20 74 68 65 20  suring that the 
f0220 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79  allocated memory
f0230 0a 2a 2a 20 73 70 61 63 65 20 69 73 20 65 76 65  .** space is eve
f0240 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a  ntually freed..*
f0250 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50 61 74 68  *.** *pConchPath
f0260 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20   is set to NULL 
f0270 69 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  if a memory allo
f0280 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63  cation error occ
f0290 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  urs..*/.static i
f02a0 6e 74 20 70 72 6f 78 79 43 72 65 61 74 65 43 6f  nt proxyCreateCo
f02b0 6e 63 68 50 61 74 68 6e 61 6d 65 28 63 68 61 72  nchPathname(char
f02c0 20 2a 64 62 50 61 74 68 2c 20 63 68 61 72 20 2a   *dbPath, char *
f02d0 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b 0a 20 20  *pConchPath){.  
f02e0 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
f02f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f0300 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
f0310 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 28 69 6e  .  int len = (in
f0320 74 29 73 74 72 6c 65 6e 28 64 62 50 61 74 68 29  t)strlen(dbPath)
f0330 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 64  ; /* Length of d
f0340 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
f0350 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a 20 20 63   - dbPath */.  c
f0360 68 61 72 20 2a 63 6f 6e 63 68 50 61 74 68 3b 20  har *conchPath; 
f0370 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f0380 62 75 66 66 65 72 20 69 6e 20 77 68 69 63 68 20  buffer in which 
f0390 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 63 6f 6e  to construct con
f03a0 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 2f 2a  ch name */..  /*
f03b0 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20   Allocate space 
f03c0 66 6f 72 20 74 68 65 20 63 6f 6e 63 68 20 66 69  for the conch fi
f03d0 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e 69 74 69  lename and initi
f03e0 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 20 74  alize the name t
f03f0 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20  o.  ** the name 
f0400 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  of the original 
f0410 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a  database file. *
f0420 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68 50 61 74  /  .  *pConchPat
f0430 68 20 3d 20 63 6f 6e 63 68 50 61 74 68 20 3d 20  h = conchPath = 
f0440 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
f0450 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20 38 29 3b  malloc(len + 8);
f0460 0a 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 68  .  if( conchPath
f0470 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
f0480 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
f0490 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 63 6f 6e    }.  memcpy(con
f04a0 63 68 50 61 74 68 2c 20 64 62 50 61 74 68 2c 20  chPath, dbPath, 
f04b0 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20 2f 2a 20  len+1);.  .  /* 
f04c0 6e 6f 77 20 69 6e 73 65 72 74 20 61 20 22 2e 22  now insert a "."
f04d0 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74   before the last
f04e0 20 2f 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a   / character */.
f04f0 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e 2d 31 29    for( i=(len-1)
f0500 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29 7b 0a 20  ; i>=0; i-- ){. 
f0510 20 20 20 69 66 28 20 63 6f 6e 63 68 50 61 74 68     if( conchPath
f0520 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20  [i]=='/' ){.    
f0530 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 62 72 65    i++;.      bre
f0540 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
f0550 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d 27 2e 27  conchPath[i]='.'
f0560 3b 0a 20 20 77 68 69 6c 65 20 28 20 69 3c 6c 65  ;.  while ( i<le
f0570 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63 68 50 61  n ){.    conchPa
f0580 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74 68 5b 69  th[i+1]=dbPath[i
f0590 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a  ];.    i++;.  }.
f05a0 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20 74 68 65  .  /* append the
f05b0 20 22 2d 63 6f 6e 63 68 22 20 73 75 66 66 69 78   "-conch" suffix
f05c0 20 74 6f 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a   to the file */.
f05d0 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e 63 68 50    memcpy(&conchP
f05e0 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63 6f 6e 63  ath[i+1], "-conc
f05f0 68 22 2c 20 37 29 3b 0a 20 20 61 73 73 65 72 74  h", 7);.  assert
f0600 28 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 63 6f  ( (int)strlen(co
f0610 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c 65 6e 2b  nchPath) == len+
f0620 37 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53  7 );..  return S
f0630 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
f0640 20 54 61 6b 65 73 20 61 20 66 75 6c 6c 79 20 63   Takes a fully c
f0650 6f 6e 66 69 67 75 72 65 64 20 70 72 6f 78 79 20  onfigured proxy 
f0660 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65 20 75 6e  locking-style un
f0670 69 78 20 66 69 6c 65 20 61 6e 64 20 73 77 69 74  ix file and swit
f0680 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c 6f 63 61  ches.** the loca
f0690 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70 61 74 68  l lock file path
f06a0 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20   .*/.static int 
f06b0 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50  switchLockProxyP
f06c0 61 74 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46  ath(unixFile *pF
f06d0 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ile, const char 
f06e0 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79  *path) {.  proxy
f06f0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
f0700 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63  pCtx = (proxyLoc
f0710 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69  kingContext*)pFi
f0720 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f0730 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f 6c 64 50  xt;.  char *oldP
f0740 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b  ath = pCtx->lock
f0750 50 72 6f 78 79 50 61 74 68 3b 0a 20 20 69 6e 74  ProxyPath;.  int
f0760 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
f0770 0a 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c  ..  if( pFile->l
f0780 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
f0790 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
f07a0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 20  QLITE_BUSY;.  } 
f07b0 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20   ..  /* nothing 
f07c0 74 6f 20 64 6f 20 69 66 20 74 68 65 20 70 61 74  to do if the pat
f07d0 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61 75 74 6f  h is NULL, :auto
f07e0 3a 20 6f 72 20 6d 61 74 63 68 65 73 20 74 68 65  : or matches the
f07f0 20 65 78 69 73 74 69 6e 67 20 70 61 74 68 20 2a   existing path *
f0800 2f 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c  /.  if( !path ||
f0810 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c   path[0]=='\0' |
f0820 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20  | !strcmp(path, 
f0830 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a 20 20 20  ":auto:") ||.   
f0840 20 28 6f 6c 64 50 61 74 68 20 26 26 20 21 73 74   (oldPath && !st
f0850 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68 2c 20 70  rncmp(oldPath, p
f0860 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45 4e 29  ath, MAXPATHLEN)
f0870 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
f0880 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
f0890 73 65 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65  se{.    unixFile
f08a0 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43   *lockProxy = pC
f08b0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20  tx->lockProxy;. 
f08c0 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f     pCtx->lockPro
f08d0 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20 70 43 74  xy=NULL;.    pCt
f08e0 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 3d 20 30  x->conchHeld = 0
f08f0 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 50 72  ;.    if( lockPr
f0900 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  oxy!=NULL ){.   
f0910 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f 78 79 2d     rc=lockProxy-
f0920 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65  >pMethod->xClose
f0930 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ((sqlite3_file *
f0940 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20  )lockProxy);.   
f0950 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
f0960 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c  rn rc;.      sql
f0970 69 74 65 33 5f 66 72 65 65 28 6c 6f 63 6b 50 72  ite3_free(lockPr
f0980 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  oxy);.    }.    
f0990 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6f 6c 64  sqlite3_free(old
f09a0 50 61 74 68 29 3b 0a 20 20 20 20 70 43 74 78 2d  Path);.    pCtx-
f09b0 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d  >lockProxyPath =
f09c0 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
f09d0 28 30 2c 20 70 61 74 68 29 3b 0a 20 20 7d 0a 20  (0, path);.  }. 
f09e0 20 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d   .  return rc;.}
f09f0 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65 20 69 73  ../*.** pFile is
f0a00 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61 73   a file that has
f0a10 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20   been opened by 
f0a20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e 20 63 61  a prior xOpen ca
f0a30 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a 2a 20 69  ll.  dbPath.** i
f0a40 73 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 65  s a string buffe
f0a50 72 20 61 74 20 6c 65 61 73 74 20 4d 41 58 50 41  r at least MAXPA
f0a60 54 48 4c 45 4e 2b 31 20 63 68 61 72 61 63 74 65  THLEN+1 characte
f0a70 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a  rs in size..**.*
f0a80 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66  * This routine f
f0a90 69 6e 64 20 74 68 65 20 66 69 6c 65 6e 61 6d 65  ind the filename
f0aa0 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
f0ab0 20 70 46 69 6c 65 20 61 6e 64 20 77 72 69 74 65   pFile and write
f0ac0 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64 62 50 61  s it.** int dbPa
f0ad0 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  th..*/.static in
f0ae0 74 20 70 72 6f 78 79 47 65 74 44 62 50 61 74 68  t proxyGetDbPath
f0af0 46 6f 72 55 6e 69 78 46 69 6c 65 28 75 6e 69 78  ForUnixFile(unix
f0b00 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 63 68 61  File *pFile, cha
f0b10 72 20 2a 64 62 50 61 74 68 29 7b 0a 23 69 66 20  r *dbPath){.#if 
f0b20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
f0b30 5f 29 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  _).  if( pFile->
f0b40 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61 66 70 49  pMethod == &afpI
f0b50 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  oMethods ){.    
f0b60 2f 2a 20 61 66 70 20 73 74 79 6c 65 20 6b 65 65  /* afp style kee
f0b70 70 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  ps a reference t
f0b80 6f 20 74 68 65 20 64 62 20 70 61 74 68 20 69 6e  o the db path in
f0b90 20 74 68 65 20 66 69 6c 65 50 61 74 68 20 66 69   the filePath fi
f0ba0 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20 74  eld .    ** of t
f0bb0 68 65 20 73 74 72 75 63 74 20 2a 2f 0a 20 20 20  he struct */.   
f0bc0 20 61 73 73 65 72 74 28 20 28 69 6e 74 29 73 74   assert( (int)st
f0bd0 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c  rlen((char*)pFil
f0be0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f0bf0 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29  t)<=MAXPATHLEN )
f0c00 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50  ;.    strcpy(dbP
f0c10 61 74 68 2c 20 28 28 61 66 70 4c 6f 63 6b 69 6e  ath, ((afpLockin
f0c20 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c 65  gContext *)pFile
f0c30 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
f0c40 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20 20 7d 65  )->dbPath);.  }e
f0c50 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 69 66 28  lse.#endif.  if(
f0c60 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20   pFile->pMethod 
f0c70 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74  == &dotlockIoMet
f0c80 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 64  hods ){.    /* d
f0c90 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65 20 75 73  ot lock style us
f0ca0 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63  es the locking c
f0cb0 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20  ontext to store 
f0cc0 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a 20 20 20  the dot lock.   
f0cd0 20 2a 2a 20 66 69 6c 65 20 70 61 74 68 20 2a 2f   ** file path */
f0ce0 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 73  .    int len = s
f0cf0 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70 46  trlen((char *)pF
f0d00 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
f0d10 65 78 74 29 20 2d 20 73 74 72 6c 65 6e 28 44 4f  ext) - strlen(DO
f0d20 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29 3b 0a 20  TLOCK_SUFFIX);. 
f0d30 20 20 20 6d 65 6d 63 70 79 28 64 62 50 61 74 68     memcpy(dbPath
f0d40 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d  , (char *)pFile-
f0d50 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2c  >lockingContext,
f0d60 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20 7d 65 6c   len + 1);.  }el
f0d70 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c 6c 20 6f  se{.    /* all o
f0d80 74 68 65 72 20 73 74 79 6c 65 73 20 75 73 65 20  ther styles use 
f0d90 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74  the locking cont
f0da0 65 78 74 20 74 6f 20 73 74 6f 72 65 20 74 68 65  ext to store the
f0db0 20 64 62 20 66 69 6c 65 20 70 61 74 68 20 2a 2f   db file path */
f0dc0 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 74 72  .    assert( str
f0dd0 6c 65 6e 28 28 63 68 61 72 2a 29 70 46 69 6c 65  len((char*)pFile
f0de0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
f0df0 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e 20 29 3b  )<=MAXPATHLEN );
f0e00 0a 20 20 20 20 73 74 72 63 70 79 28 64 62 50 61  .    strcpy(dbPa
f0e10 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69 6c  th, (char *)pFil
f0e20 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f0e30 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  t);.  }.  return
f0e40 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
f0e50 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e 20 61 6c  *.** Takes an al
f0e60 72 65 61 64 79 20 66 69 6c 6c 65 64 20 69 6e 20  ready filled in 
f0e70 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 61 6c  unix file and al
f0e80 74 65 72 73 20 69 74 20 73 6f 20 61 6c 6c 20 66  ters it so all f
f0e90 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20  ile locking .** 
f0ea0 77 69 6c 6c 20 62 65 20 70 65 72 66 6f 72 6d 65  will be performe
f0eb0 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70  d on the local p
f0ec0 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 2e 20  roxy lock file. 
f0ed0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66   The following f
f0ee0 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20 70 72 65  ields.** are pre
f0ef0 73 65 72 76 65 64 20 69 6e 20 74 68 65 20 6c 6f  served in the lo
f0f00 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 73 6f  cking context so
f0f10 20 74 68 61 74 20 74 68 65 79 20 63 61 6e 20 62   that they can b
f0f20 65 20 72 65 73 74 6f 72 65 64 20 61 6e 64 20 0a  e restored and .
f0f30 2a 2a 20 74 68 65 20 75 6e 69 78 20 73 74 72 75  ** the unix stru
f0f40 63 74 75 72 65 20 70 72 6f 70 65 72 6c 79 20 63  cture properly c
f0f50 6c 65 61 6e 65 64 20 75 70 20 61 74 20 63 6c 6f  leaned up at clo
f0f60 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20 2d 3e 6c  se time:.**  ->l
f0f70 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 0a 2a 2a  ockingContext.**
f0f80 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a 2f 0a 73    ->pMethod.*/.s
f0f90 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 54  tatic int proxyT
f0fa0 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c 65  ransformUnixFile
f0fb0 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65  (unixFile *pFile
f0fc0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61  , const char *pa
f0fd0 74 68 29 20 7b 0a 20 20 70 72 6f 78 79 4c 6f 63  th) {.  proxyLoc
f0fe0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74  kingContext *pCt
f0ff0 78 3b 0a 20 20 63 68 61 72 20 64 62 50 61 74 68  x;.  char dbPath
f1000 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31 5d 3b 20  [MAXPATHLEN+1]; 
f1010 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
f1020 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
f1030 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 6c 6f  le */.  char *lo
f1040 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20 69  ckPath=NULL;.  i
f1050 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
f1060 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70 46 69 6c  K;.  .  if( pFil
f1070 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f  e->locktype!=NO_
f1080 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  LOCK ){.    retu
f1090 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
f10a0 20 20 7d 0a 20 20 70 72 6f 78 79 47 65 74 44 62    }.  proxyGetDb
f10b0 50 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 28  PathForUnixFile(
f10c0 70 46 69 6c 65 2c 20 64 62 50 61 74 68 29 3b 0a  pFile, dbPath);.
f10d0 20 20 69 66 28 20 21 70 61 74 68 20 7c 7c 20 70    if( !path || p
f10e0 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20 7c 7c 20  ath[0]=='\0' || 
f10f0 21 73 74 72 63 6d 70 28 70 61 74 68 2c 20 22 3a  !strcmp(path, ":
f1100 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20 20 20 6c  auto:") ){.    l
f1110 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a 20 20  ockPath=NULL;.  
f1120 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b 50  }else{.    lockP
f1130 61 74 68 3d 28 63 68 61 72 20 2a 29 70 61 74 68  ath=(char *)path
f1140 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41  ;.  }.  .  OSTRA
f1150 43 45 34 28 22 54 52 41 4e 53 50 52 4f 58 59 20  CE4("TRANSPROXY 
f1160 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d 25   %d for %s pid=%
f1170 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  d\n", pFile->h,.
f1180 20 20 20 20 20 20 20 20 20 20 20 28 6c 6f 63 6b             (lock
f1190 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61 74 68 20  Path ? lockPath 
f11a0 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74  : ":auto:"), get
f11b0 70 69 64 28 29 29 3b 0a 0a 20 20 70 43 74 78 20  pid());..  pCtx 
f11c0 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
f11d0 28 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29 20  ( sizeof(*pCtx) 
f11e0 29 3b 0a 20 20 69 66 28 20 70 43 74 78 3d 3d 30  );.  if( pCtx==0
f11f0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
f1200 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
f1210 0a 20 20 6d 65 6d 73 65 74 28 70 43 74 78 2c 20  .  memset(pCtx, 
f1220 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43 74 78 29  0, sizeof(*pCtx)
f1230 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72 6f 78 79  );..  rc = proxy
f1240 43 72 65 61 74 65 43 6f 6e 63 68 50 61 74 68 6e  CreateConchPathn
f1250 61 6d 65 28 64 62 50 61 74 68 2c 20 26 70 43 74  ame(dbPath, &pCt
f1260 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68  x->conchFilePath
f1270 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
f1280 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
f1290 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55 6e   = proxyCreateUn
f12a0 69 78 46 69 6c 65 28 70 43 74 78 2d 3e 63 6f 6e  ixFile(pCtx->con
f12b0 63 68 46 69 6c 65 50 61 74 68 2c 20 26 70 43 74  chFilePath, &pCt
f12c0 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20  x->conchFile);. 
f12d0 20 7d 20 20 0a 20 20 69 66 28 20 72 63 3d 3d 53   }  .  if( rc==S
f12e0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c 6f 63 6b  QLITE_OK && lock
f12f0 50 61 74 68 20 29 7b 0a 20 20 20 20 70 43 74 78  Path ){.    pCtx
f1300 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
f1310 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
f1320 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68 29 3b 0a  p(0, lockPath);.
f1330 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53    }..  if( rc==S
f1340 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
f1350 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 69 73  /* all memory is
f1360 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70 72 6f 78   allocated, prox
f1370 79 73 20 61 72 65 20 63 72 65 61 74 65 64 20 61  ys are created a
f1380 6e 64 20 61 73 73 69 67 6e 65 64 2c 20 0a 20 20  nd assigned, .  
f1390 20 20 2a 2a 20 73 77 69 74 63 68 20 74 68 65 20    ** switch the 
f13a0 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20  locking context 
f13b0 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68 65 6e  and pMethod then
f13c0 20 72 65 74 75 72 6e 2e 0a 20 20 20 20 2a 2f 0a   return..    */.
f13d0 20 20 20 20 70 43 74 78 2d 3e 64 62 50 61 74 68      pCtx->dbPath
f13e0 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
f13f0 75 70 28 30 2c 20 64 62 50 61 74 68 29 3b 0a 20  up(0, dbPath);. 
f1400 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c 6f 63 6b     pCtx->oldLock
f1410 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 46 69  ingContext = pFi
f1420 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f1430 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  xt;.    pFile->l
f1440 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
f1450 70 43 74 78 3b 0a 20 20 20 20 70 43 74 78 2d 3e  pCtx;.    pCtx->
f1460 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20 70 46 69  pOldMethod = pFi
f1470 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a 20 20 20  le->pMethod;.   
f1480 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20   pFile->pMethod 
f1490 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64  = &proxyIoMethod
f14a0 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  s;.  }else{.    
f14b0 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46  if( pCtx->conchF
f14c0 69 6c 65 20 29 7b 20 0a 20 20 20 20 20 20 72 63  ile ){ .      rc
f14d0 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69   = pCtx->conchFi
f14e0 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c  le->pMethod->xCl
f14f0 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ose((sqlite3_fil
f1500 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e 63 68 46  e *)pCtx->conchF
f1510 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ile);.      if( 
f1520 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
f1530 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
f1540 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69  ee(pCtx->conchFi
f1550 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  le);.    }.    s
f1560 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78  qlite3_free(pCtx
f1570 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 29  ->conchFilePath)
f1580 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ; .    sqlite3_f
f1590 72 65 65 28 70 43 74 78 29 3b 0a 20 20 7d 0a 20  ree(pCtx);.  }. 
f15a0 20 4f 53 54 52 41 43 45 33 28 22 54 52 41 4e 53   OSTRACE3("TRANS
f15b0 50 52 4f 58 59 20 20 25 64 20 25 73 5c 6e 22 2c  PROXY  %d %s\n",
f15c0 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20   pFile->h,.     
f15d0 20 20 20 20 20 20 28 72 63 3d 3d 53 51 4c 49 54        (rc==SQLIT
f15e0 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22 66  E_OK ? "ok" : "f
f15f0 61 69 6c 65 64 22 29 29 3b 0a 20 20 72 65 74 75  ailed"));.  retu
f1600 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
f1610 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61   This routine ha
f1620 6e 64 6c 65 73 20 73 71 6c 69 74 65 33 5f 66 69  ndles sqlite3_fi
f1630 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20 63 61 6c  le_control() cal
f1640 6c 73 20 74 68 61 74 20 61 72 65 20 73 70 65 63  ls that are spec
f1650 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72 6f 78 79  ific.** to proxy
f1660 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61   locking..*/.sta
f1670 74 69 63 20 69 6e 74 20 70 72 6f 78 79 46 69 6c  tic int proxyFil
f1680 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33  eControl(sqlite3
f1690 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f  _file *id, int o
f16a0 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  p, void *pArg){.
f16b0 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
f16c0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
f16d0 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  GET_LOCKPROXYFIL
f16e0 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46  E: {.      unixF
f16f0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
f1700 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20  ixFile*)id;.    
f1710 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65    if( pFile->pMe
f1720 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78 79 49 6f  thod == &proxyIo
f1730 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20  Methods ){.     
f1740 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43     proxyLockingC
f1750 6f 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28  ontext *pCtx = (
f1760 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74  proxyLockingCont
f1770 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ext*)pFile->lock
f1780 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  ingContext;.    
f1790 20 20 20 20 70 72 6f 78 79 54 61 6b 65 43 6f 6e      proxyTakeCon
f17a0 63 68 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 20  ch(pFile);.     
f17b0 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c 6f 63     if( pCtx->loc
f17c0 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a 20 20  kProxyPath ){.  
f17d0 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73 74 20          *(const 
f17e0 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d 20 70  char **)pArg = p
f17f0 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
f1800 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  th;.        }els
f1810 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63  e{.          *(c
f1820 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72  onst char **)pAr
f1830 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28 6e 6f 74  g = ":auto: (not
f1840 20 68 65 6c 64 29 22 3b 0a 20 20 20 20 20 20 20   held)";.       
f1850 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20   }.      } else 
f1860 7b 0a 20 20 20 20 20 20 20 20 2a 28 63 6f 6e 73  {.        *(cons
f1870 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20 3d  t char **)pArg =
f1880 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20   NULL;.      }. 
f1890 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
f18a0 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20  TE_OK;.    }.   
f18b0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53 45 54   case SQLITE_SET
f18c0 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 3a 20  _LOCKPROXYFILE: 
f18d0 7b 0a 20 20 20 20 20 20 75 6e 69 78 46 69 6c 65  {.      unixFile
f18e0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
f18f0 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 20 20 69  ile*)id;.      i
f1900 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
f1910 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 50  K;.      int isP
f1920 72 6f 78 79 53 74 79 6c 65 20 3d 20 28 70 46 69  roxyStyle = (pFi
f1930 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26  le->pMethod == &
f1940 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64 73 29 3b  proxyIoMethods);
f1950 0a 20 20 20 20 20 20 69 66 28 20 70 41 72 67 3d  .      if( pArg=
f1960 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e 73 74 20  =NULL || (const 
f1970 63 68 61 72 20 2a 29 70 41 72 67 3d 3d 30 20 29  char *)pArg==0 )
f1980 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 73  {.        if( is
f1990 50 72 6f 78 79 53 74 79 6c 65 20 29 7b 0a 20 20  ProxyStyle ){.  
f19a0 20 20 20 20 20 20 20 20 2f 2a 20 74 75 72 6e 20          /* turn 
f19b0 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e  off proxy lockin
f19c0 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f 72 74 65  g - not supporte
f19d0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72  d */.          r
f19e0 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
f19f0 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43   /*SQLITE_PROTOC
f1a00 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  OL? SQLITE_MISUS
f1a10 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20 20 7d 65  E?*/;.        }e
f1a20 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
f1a30 2a 20 74 75 72 6e 20 6f 66 66 20 70 72 6f 78 79  * turn off proxy
f1a40 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c 72 65 61   locking - alrea
f1a50 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50 20 2a 2f  dy off - NOOP */
f1a60 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
f1a70 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
f1a80 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
f1a90 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  {.        const 
f1aa0 63 68 61 72 20 2a 70 72 6f 78 79 50 61 74 68 20  char *proxyPath 
f1ab0 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
f1ac0 70 41 72 67 3b 0a 20 20 20 20 20 20 20 20 69 66  pArg;.        if
f1ad0 28 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 29  ( isProxyStyle )
f1ae0 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 6f 78  {.          prox
f1af0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f1b00 2a 70 43 74 78 20 3d 20 0a 20 20 20 20 20 20 20  *pCtx = .       
f1b10 20 20 20 20 20 28 70 72 6f 78 79 4c 6f 63 6b 69       (proxyLocki
f1b20 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65  ngContext*)pFile
f1b30 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
f1b40 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
f1b50 21 73 74 72 63 6d 70 28 70 41 72 67 2c 20 22 3a  !strcmp(pArg, ":
f1b60 61 75 74 6f 3a 22 29 20 0a 20 20 20 20 20 20 20  auto:") .       
f1b70 20 20 20 20 7c 7c 20 28 70 43 74 78 2d 3e 6c 6f      || (pCtx->lo
f1b80 63 6b 50 72 6f 78 79 50 61 74 68 20 26 26 0a 20  ckProxyPath &&. 
f1b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 21 73                !s
f1ba0 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e 6c 6f 63  trncmp(pCtx->loc
f1bb0 6b 50 72 6f 78 79 50 61 74 68 2c 20 70 72 6f 78  kProxyPath, prox
f1bc0 79 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c 45  yPath, MAXPATHLE
f1bd0 4e 29 29 0a 20 20 20 20 20 20 20 20 20 20 29 7b  N)).          ){
f1be0 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
f1bf0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
f1c00 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
f1c10 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
f1c20 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78 79 50 61  witchLockProxyPa
f1c30 74 68 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50  th(pFile, proxyP
f1c40 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ath);.          
f1c50 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  }.        }else{
f1c60 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75  .          /* tu
f1c70 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66 69 6c 65  rn on proxy file
f1c80 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20   locking */.    
f1c90 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79        rc = proxy
f1ca0 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69 6c  TransformUnixFil
f1cb0 65 28 70 46 69 6c 65 2c 20 70 72 6f 78 79 50 61  e(pFile, proxyPa
f1cc0 74 68 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  th);.        }. 
f1cd0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
f1ce0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
f1cf0 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
f1d00 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 20     assert( 0 ); 
f1d10 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20 61 73 73   /* The call ass
f1d20 75 72 65 73 20 74 68 61 74 20 6f 6e 6c 79 20 76  ures that only v
f1d30 61 6c 69 64 20 6f 70 63 6f 64 65 73 20 61 72 65  alid opcodes are
f1d40 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20 7d 0a 20   sent */.    }. 
f1d50 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45   }.  /*NOTREACHE
f1d60 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  D*/.  return SQL
f1d70 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a  ITE_ERROR;.}../*
f1d80 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68 69 73 20  .** Within this 
f1d90 64 69 76 69 73 69 6f 6e 20 28 74 68 65 20 70 72  division (the pr
f1da0 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e 67 20 69  oxying locking i
f1db0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 20 74  mplementation) t
f1dc0 68 65 20 70 72 6f 63 65 64 75 72 65 73 0a 2a 2a  he procedures.**
f1dd0 20 61 62 6f 76 65 20 74 68 69 73 20 70 6f 69 6e   above this poin
f1de0 74 20 61 72 65 20 61 6c 6c 20 75 74 69 6c 69 74  t are all utilit
f1df0 69 65 73 2e 20 20 54 68 65 20 6c 6f 63 6b 2d 72  ies.  The lock-r
f1e00 65 6c 61 74 65 64 20 6d 65 74 68 6f 64 73 20 6f  elated methods o
f1e10 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 2d 6c  f the.** proxy-l
f1e20 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33 5f 69  ocking sqlite3_i
f1e30 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65 63 74 20  o_method object 
f1e40 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a  follow..*/.../*.
f1e50 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
f1e60 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65 20  checks if there 
f1e70 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  is a RESERVED lo
f1e80 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 73  ck held on the s
f1e90 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65  pecified.** file
f1ea0 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79 20   by this or any 
f1eb0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20 49  other process. I
f1ec0 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73  f such a lock is
f1ed0 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65 73   held, set *pRes
f1ee0 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e 2d  Out.** to a non-
f1ef0 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65 72  zero value other
f1f00 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69 73  wise *pResOut is
f1f10 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20 54   set to zero.  T
f1f20 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a  he return value.
f1f30 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c  ** is set to SQL
f1f40 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61 6e  ITE_OK unless an
f1f50 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72   I/O error occur
f1f60 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63 68  s during lock ch
f1f70 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  ecking..*/.stati
f1f80 63 20 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b  c int proxyCheck
f1f90 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
f1fa0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
f1fb0 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b 0a 20  nt *pResOut) {. 
f1fc0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
f1fd0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
f1fe0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f  ;.  int rc = pro
f1ff0 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c  xyTakeConch(pFil
f2000 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
f2010 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
f2020 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
f2030 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78  xt *pCtx = (prox
f2040 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f2050 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
f2060 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69  Context;.    uni
f2070 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70  xFile *proxy = p
f2080 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a  Ctx->lockProxy;.
f2090 20 20 20 20 72 65 74 75 72 6e 20 70 72 6f 78 79      return proxy
f20a0 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 68 65 63  ->pMethod->xChec
f20b0 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 28 73  kReservedLock((s
f20c0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f  qlite3_file*)pro
f20d0 78 79 2c 20 70 52 65 73 4f 75 74 29 3b 0a 20 20  xy, pResOut);.  
f20e0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
f20f0 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65  ../*.** Lock the
f2100 20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c   file with the l
f2110 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79  ock specified by
f2120 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74   parameter lockt
f2130 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20  ype - one.** of 
f2140 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
f2150 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41  *.**     (1) SHA
f2160 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  RED_LOCK.**     
f2170 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43  (2) RESERVED_LOC
f2180 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e  K.**     (3) PEN
f2190 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  DING_LOCK.**    
f21a0 20 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c   (4) EXCLUSIVE_L
f21b0 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69  OCK.**.** Someti
f21c0 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74  mes when request
f21d0 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61  ing one lock sta
f21e0 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c  te, additional l
f21f0 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72  ock states.** ar
f2200 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65  e inserted in be
f2210 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b  tween.  The lock
f2220 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f  ing might fail o
f2230 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74  n one of the lat
f2240 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e  er.** transition
f2250 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f  s leaving the lo
f2260 63 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65  ck state differe
f2270 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20  nt from what it 
f2280 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73  started but.** s
f2290 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74  till short of it
f22a0 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c  s goal.  The fol
f22b0 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f  lowing chart sho
f22c0 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a  ws the allowed.*
f22d0 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e  * transitions an
f22e0 64 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69  d the inserted i
f22f0 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74  ntermediate stat
f2300 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c  es:.**.**    UNL
f2310 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a  OCKED -> SHARED.
f2320 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
f2330 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53  RESERVED.**    S
f2340 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  HARED -> (PENDIN
f2350 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
f2360 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d  **    RESERVED -
f2370 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45  > (PENDING) -> E
f2380 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50  XCLUSIVE.**    P
f2390 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53  ENDING -> EXCLUS
f23a0 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  IVE.**.** This r
f23b0 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79  outine will only
f23c0 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b   increase a lock
f23d0 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74  .  Use the sqlit
f23e0 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20  e3OsUnlock().** 
f23f0 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72  routine to lower
f2400 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c   a locking level
f2410 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
f2420 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65  proxyLock(sqlite
f2430 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
f2440 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e  locktype) {.  un
f2450 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
f2460 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
f2470 20 69 6e 74 20 72 63 20 3d 20 70 72 6f 78 79 54   int rc = proxyT
f2480 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b  akeConch(pFile);
f2490 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
f24a0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 72 6f 78  E_OK ){.    prox
f24b0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f24c0 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f  *pCtx = (proxyLo
f24d0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70  ckingContext *)p
f24e0 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
f24f0 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69  text;.    unixFi
f2500 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70 43 74 78  le *proxy = pCtx
f2510 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20 20  ->lockProxy;.   
f2520 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e 70 4d 65   rc = proxy->pMe
f2530 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c  thod->xLock((sql
f2540 69 74 65 33 5f 66 69 6c 65 2a 29 70 72 6f 78 79  ite3_file*)proxy
f2550 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20  , locktype);.   
f2560 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
f2570 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63 6b 74 79   = proxy->lockty
f2580 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  pe;.  }.  return
f2590 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c   rc;.}.../*.** L
f25a0 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
f25b0 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
f25c0 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20  escriptor pFile 
f25d0 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
f25e0 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
f25f0 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
f2600 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
f2610 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
f2620 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
f2630 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
f2640 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
f2650 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
f2660 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
f2670 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
f2680 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
f2690 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
f26a0 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28 73 71 6c   proxyUnlock(sql
f26b0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
f26c0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
f26d0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
f26e0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
f26f0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f  ;.  int rc = pro
f2700 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c  xyTakeConch(pFil
f2710 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
f2720 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
f2730 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
f2740 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78  xt *pCtx = (prox
f2750 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f2760 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
f2770 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69  Context;.    uni
f2780 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70  xFile *proxy = p
f2790 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a  Ctx->lockProxy;.
f27a0 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e      rc = proxy->
f27b0 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b  pMethod->xUnlock
f27c0 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
f27d0 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29  proxy, locktype)
f27e0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  ;.    pFile->loc
f27f0 6b 74 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c  ktype = proxy->l
f2800 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72  ocktype;.  }.  r
f2810 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
f2820 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 20  ** Close a file 
f2830 74 68 61 74 20 75 73 65 73 20 70 72 6f 78 79 20  that uses proxy 
f2840 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  locks..*/.static
f2850 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f 73 65 28   int proxyClose(
f2860 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
f2870 29 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a  ) {.  if( id ){.
f2880 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46      unixFile *pF
f2890 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
f28a0 29 69 64 3b 0a 20 20 20 20 70 72 6f 78 79 4c 6f  )id;.    proxyLo
f28b0 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43  ckingContext *pC
f28c0 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69  tx = (proxyLocki
f28d0 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46 69 6c  ngContext *)pFil
f28e0 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  e->lockingContex
f28f0 74 3b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  t;.    unixFile 
f2900 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20 70 43 74  *lockProxy = pCt
f2910 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a 20 20  x->lockProxy;.  
f2920 20 20 75 6e 69 78 46 69 6c 65 20 2a 63 6f 6e 63    unixFile *conc
f2930 68 46 69 6c 65 20 3d 20 70 43 74 78 2d 3e 63 6f  hFile = pCtx->co
f2940 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20 69 6e 74  nchFile;.    int
f2950 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
f2960 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 6c 6f  .    .    if( lo
f2970 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20 20 20 20  ckProxy ){.     
f2980 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f 78 79 2d   rc = lockProxy-
f2990 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63  >pMethod->xUnloc
f29a0 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k((sqlite3_file*
f29b0 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e 4f 5f 4c  )lockProxy, NO_L
f29c0 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  OCK);.      if( 
f29d0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
f29e0 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50        rc = lockP
f29f0 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78  roxy->pMethod->x
f2a00 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66  Close((sqlite3_f
f2a10 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b  ile*)lockProxy);
f2a20 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20  .      if( rc ) 
f2a30 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
f2a40 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f   sqlite3_free(lo
f2a50 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 20 20  ckProxy);.      
f2a60 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20  pCtx->lockProxy 
f2a70 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
f2a80 66 28 20 63 6f 6e 63 68 46 69 6c 65 20 29 7b 0a  f( conchFile ){.
f2a90 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e        if( pCtx->
f2aa0 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a 20 20 20  conchHeld ){.   
f2ab0 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 52       rc = proxyR
f2ac0 65 6c 65 61 73 65 43 6f 6e 63 68 28 70 46 69 6c  eleaseConch(pFil
f2ad0 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
f2ae0 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
f2af0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
f2b00 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d   = conchFile->pM
f2b10 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65 28 28 73  ethod->xClose((s
f2b20 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e  qlite3_file*)con
f2b30 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69  chFile);.      i
f2b40 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
f2b50 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
f2b60 5f 66 72 65 65 28 63 6f 6e 63 68 46 69 6c 65 29  _free(conchFile)
f2b70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
f2b80 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 6c  te3_free(pCtx->l
f2b90 6f 63 6b 50 72 6f 78 79 50 61 74 68 29 3b 0a 20  ockProxyPath);. 
f2ba0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
f2bb0 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50  pCtx->conchFileP
f2bc0 61 74 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ath);.    sqlite
f2bd0 33 5f 66 72 65 65 28 70 43 74 78 2d 3e 64 62 50  3_free(pCtx->dbP
f2be0 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20 72 65 73  ath);.    /* res
f2bf0 74 6f 72 65 20 74 68 65 20 6f 72 69 67 69 6e 61  tore the origina
f2c00 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78  l locking contex
f2c10 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74 68  t and pMethod th
f2c20 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a 2f 0a 20  en close it */. 
f2c30 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e     pFile->lockin
f2c40 67 43 6f 6e 74 65 78 74 20 3d 20 70 43 74 78 2d  gContext = pCtx-
f2c50 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  >oldLockingConte
f2c60 78 74 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70  xt;.    pFile->p
f2c70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78 2d 3e 70  Method = pCtx->p
f2c80 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20 20 20 73  OldMethod;.    s
f2c90 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78  qlite3_free(pCtx
f2ca0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 46  );.    return pF
f2cb0 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43  ile->pMethod->xC
f2cc0 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d 0a 20 20  lose(id);.  }.  
f2cd0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f2ce0 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a  ;.}....#endif /*
f2cf0 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
f2d00 5f 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e  __) && SQLITE_EN
f2d10 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
f2d20 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20  LE */./*.** The 
f2d30 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 73 74  proxy locking st
f2d40 79 6c 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  yle is intended 
f2d50 66 6f 72 20 75 73 65 20 77 69 74 68 20 41 46 50  for use with AFP
f2d60 20 66 69 6c 65 73 79 73 74 65 6d 73 2e 0a 2a 2a   filesystems..**
f2d70 20 41 6e 64 20 73 69 6e 63 65 20 41 46 50 20 69   And since AFP i
f2d80 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74 65 64  s only supported
f2d90 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74 68 65 20   on MacOSX, the 
f2da0 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73  proxy locking is
f2db0 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74 72 69 63   also.** restric
f2dc0 74 65 64 20 74 6f 20 4d 61 63 4f 53 58 2e 0a 2a  ted to MacOSX..*
f2dd0 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  * .**.**********
f2de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
f2df0 20 74 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 20   the proxy lock 
f2e00 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
f2e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2e20 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
f2e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2e70 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  ****/../*.** Ini
f2e80 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72  tialize the oper
f2e90 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74  ating system int
f2ea0 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  erface..**.** Th
f2eb0 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67 69 73  is routine regis
f2ec0 74 65 72 73 20 61 6c 6c 20 56 46 53 20 69 6d 70  ters all VFS imp
f2ed0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72  lementations for
f2ee0 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61   unix-like opera
f2ef0 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65 6d 73 2e  ting.** systems.
f2f00 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 2c 20    This routine, 
f2f10 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f  and the sqlite3_
f2f20 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74 69 6e 65  os_end() routine
f2f30 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2c 0a 2a   that follows,.*
f2f40 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20  * should be the 
f2f50 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73 20 69 6e  only routines in
f2f60 20 74 68 69 73 20 66 69 6c 65 20 74 68 61 74 20   this file that 
f2f70 61 72 65 20 76 69 73 69 62 6c 65 20 66 72 6f 6d  are visible from
f2f80 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c 65 73 2e   other.** files.
f2f90 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
f2fa0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  ine is called on
f2fb0 63 65 20 64 75 72 69 6e 67 20 53 51 4c 69 74 65  ce during SQLite
f2fc0 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
f2fd0 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73 69 6e 67  and by a.** sing
f2fe0 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68 65 20  le thread.  The 
f2ff0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
f3000 6e 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62 73  n and mutex subs
f3010 79 73 74 65 6d 73 20 68 61 76 65 20 6e 6f 74 0a  ystems have not.
f3020 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c 79 20 62  ** necessarily b
f3030 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  een initialized 
f3040 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
f3050 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 61 6e 64  e is called, and
f3060 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75   so they.** shou
f3070 6c 64 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 0a  ld not be used..
f3080 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
f3090 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  t sqlite3_os_ini
f30a0 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 0a  t(void){ .  /* .
f30b0 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    ** The followi
f30c0 6e 67 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 73  ng macro defines
f30d0 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 72 20   an initializer 
f30e0 66 6f 72 20 61 6e 20 73 71 6c 69 74 65 33 5f 76  for an sqlite3_v
f30f0 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20  fs object..  ** 
f3100 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
f3110 56 46 53 20 69 73 20 4e 41 4d 45 2e 20 20 54 68  VFS is NAME.  Th
f3120 65 20 70 41 70 70 44 61 74 61 20 69 73 20 61 20  e pAppData is a 
f3130 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 6f 69  pointer to a poi
f3140 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 65  nter.  ** to the
f3150 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69   "finder" functi
f3160 6f 6e 2e 20 20 28 70 41 70 70 44 61 74 61 20 69  on.  (pAppData i
f3170 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
f3180 20 70 6f 69 6e 74 65 72 20 62 65 63 61 75 73 65   pointer because
f3190 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43 39 30 20  .  ** silly C90 
f31a0 72 75 6c 65 73 20 70 72 6f 68 69 62 69 74 20 61  rules prohibit a
f31b0 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62 65 69 6e   void* from bein
f31c0 67 20 63 61 73 74 20 74 6f 20 61 20 66 75 6e 63  g cast to a func
f31d0 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a 20 20 2a  tion pointer.  *
f31e0 2a 20 61 6e 64 20 73 6f 20 77 65 20 68 61 76 65  * and so we have
f31f0 20 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 20 74   to go through t
f3200 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  he intermediate 
f3210 70 6f 69 6e 74 65 72 20 74 6f 20 61 76 6f 69 64  pointer to avoid
f3220 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 77   problems.  ** w
f3230 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 77 69  hen compiling wi
f3240 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65 72 72  th -pedantic-err
f3250 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a 20 20 2a  ors on GCC.).  *
f3260 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49 4e 44 45  *.  ** The FINDE
f3270 52 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  R parameter to t
f3280 68 69 73 20 6d 61 63 72 6f 20 69 73 20 74 68 65  his macro is the
f3290 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 70 6f 69   name of the poi
f32a0 6e 74 65 72 20 74 6f 20 74 68 65 0a 20 20 2a 2a  nter to the.  **
f32b0 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e   finder-function
f32c0 2e 20 20 54 68 65 20 66 69 6e 64 65 72 2d 66 75  .  The finder-fu
f32d0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61  nction returns a
f32e0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a   pointer to the.
f32f0 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69 6f 5f 6d    ** sqlite_io_m
f3300 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 74 68  ethods object th
f3310 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
f3320 65 20 64 65 73 69 72 65 64 20 6c 6f 63 6b 69 6e  e desired lockin
f3330 67 0a 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 73  g.  ** behaviors
f3340 2e 20 20 53 65 65 20 74 68 65 20 64 69 76 69 73  .  See the divis
f3350 69 6f 6e 20 61 62 6f 76 65 20 74 68 61 74 20 63  ion above that c
f3360 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4f 4d 45  ontains the IOME
f3370 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61 63 72 6f  THODS.  ** macro
f3380 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 20 69 6e   for addition in
f3390 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 66 69 6e  formation on fin
f33a0 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73 2e 0a 20  der-functions.. 
f33b0 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74 20 66 69   **.  ** Most fi
f33c0 6e 64 65 72 73 20 73 69 6d 70 6c 79 20 72 65 74  nders simply ret
f33d0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
f33e0 20 61 20 66 69 78 65 64 20 73 71 6c 69 74 65 33   a fixed sqlite3
f33f0 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20 20 2a 2a  _io_methods.  **
f3400 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 74 68   object.  But th
f3410 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e  e "autolockIoFin
f3420 64 65 72 22 20 61 76 61 69 6c 61 62 6c 65 20 6f  der" available o
f3430 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73 20 61 20  n MacOSX does a 
f3440 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d 6f 72 65  little.  ** more
f3450 20 74 68 61 6e 20 74 68 61 74 3b 20 69 74 20 6c   than that; it l
f3460 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c 65  ooks at the file
f3470 73 79 73 74 65 6d 20 74 79 70 65 20 74 68 61 74  system type that
f3480 20 68 6f 73 74 73 20 74 68 65 20 0a 20 20 2a 2a   hosts the .  **
f3490 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
f34a0 6e 64 20 74 72 69 65 73 20 74 6f 20 63 68 6f 6f  nd tries to choo
f34b0 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67 20 6d 65  se an locking me
f34c0 74 68 6f 64 20 61 70 70 72 6f 70 72 69 61 74 65  thod appropriate
f34d0 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61 74 20 66   for.  ** that f
f34e0 69 6c 65 73 79 73 74 65 6d 20 74 69 6d 65 2e 0a  ilesystem time..
f34f0 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 55    */.  #define U
f3500 4e 49 58 56 46 53 28 56 46 53 4e 41 4d 45 2c 20  NIXVFS(VFSNAME, 
f3510 46 49 4e 44 45 52 29 20 7b 20 20 20 20 20 20 20  FINDER) {       
f3520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3530 20 5c 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20   \.    1,       
f3540 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f3550 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20 20 20 20  iVersion */     
f3560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
f3570 0a 20 20 20 20 73 69 7a 65 6f 66 28 75 6e 69 78  .    sizeof(unix
f3580 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a 20 73 7a  File),     /* sz
f3590 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20 20 20 20  OsFile */       
f35a0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
f35b0 20 20 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2c     MAX_PATHNAME,
f35c0 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61           /* mxPa
f35d0 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20  thname */       
f35e0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
f35f0 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
f3600 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74 20         /* pNext 
f3610 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f3620 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 56           \.    V
f3630 46 53 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20  FSNAME,         
f3640 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f       /* zName */
f3650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3660 20 20 20 20 20 20 20 5c 0a 20 20 20 20 28 76 6f         \.    (vo
f3670 69 64 2a 29 26 46 49 4e 44 45 52 2c 20 20 20 20  id*)&FINDER,    
f3680 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a     /* pAppData *
f3690 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f36a0 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 4f       \.    unixO
f36b0 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  pen,            
f36c0 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20 20 20 20   /* xOpen */    
f36d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f36e0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 65 6c     \.    unixDel
f36f0 65 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ete,           /
f3700 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20 20 20 20  * xDelete */    
f3710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3720 20 5c 0a 20 20 20 20 75 6e 69 78 41 63 63 65 73   \.    unixAcces
f3730 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
f3740 78 41 63 63 65 73 73 20 2a 2f 20 20 20 20 20 20  xAccess */      
f3750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
f3760 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c 50 61 74  .    unixFullPat
f3770 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 78 46  hname,     /* xF
f3780 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20  ullPathname */  
f3790 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
f37a0 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e 2c 20 20     unixDlOpen,  
f37b0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 4f           /* xDlO
f37c0 70 65 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20  pen */          
f37d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
f37e0 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c 20 20 20   unixDlError,   
f37f0 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72         /* xDlErr
f3800 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  or */           
f3810 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75           \.    u
f3820 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20 20 20 20  nixDlSym,       
f3830 20 20 20 20 20 2f 2a 20 78 44 6c 53 79 6d 20 2a       /* xDlSym *
f3840 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f3850 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
f3860 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  xDlClose,       
f3870 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a     /* xDlClose *
f3880 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f3890 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 52       \.    unixR
f38a0 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20 20 20 20  andomness,      
f38b0 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20   /* xRandomness 
f38c0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f38d0 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 53 6c 65     \.    unixSle
f38e0 65 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ep,            /
f38f0 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20 20 20 20  * xSleep */     
f3900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3910 20 5c 0a 20 20 20 20 75 6e 69 78 43 75 72 72 65   \.    unixCurre
f3920 6e 74 54 69 6d 65 2c 20 20 20 20 20 20 2f 2a 20  ntTime,      /* 
f3930 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a 2f 20  xCurrentTime */ 
f3940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
f3950 0a 20 20 20 20 75 6e 69 78 47 65 74 4c 61 73 74  .    unixGetLast
f3960 45 72 72 6f 72 20 20 20 20 20 20 2f 2a 20 78 47  Error      /* xG
f3970 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 20 20  etLastError */  
f3980 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
f3990 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c   }..  /*.  ** Al
f39a0 6c 20 64 65 66 61 75 6c 74 20 56 46 53 65 73 20  l default VFSes 
f39b0 66 6f 72 20 75 6e 69 78 20 61 72 65 20 63 6f 6e  for unix are con
f39c0 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 6f  tained in the fo
f39d0 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79 2e 0a 20  llowing array.. 
f39e0 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68   **.  ** Note th
f39f0 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  at the sqlite3_v
f3a00 66 73 2e 70 4e 65 78 74 20 66 69 65 6c 64 20 6f  fs.pNext field o
f3a10 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74  f the VFS object
f3a20 20 69 73 20 6d 6f 64 69 66 69 65 64 0a 20 20 2a   is modified.  *
f3a30 2a 20 62 79 20 74 68 65 20 53 51 4c 69 74 65 20  * by the SQLite 
f3a40 63 6f 72 65 20 77 68 65 6e 20 74 68 65 20 56 46  core when the VF
f3a50 53 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e  S is registered.
f3a60 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69    So the followi
f3a70 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79 20 63 61  ng.  ** array ca
f3a80 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74 2e 0a 20  nnot be const.. 
f3a90 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 73 71 6c   */.  static sql
f3aa0 69 74 65 33 5f 76 66 73 20 61 56 66 73 5b 5d 20  ite3_vfs aVfs[] 
f3ab0 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 45  = {.#if SQLITE_E
f3ac0 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
f3ad0 59 4c 45 20 26 26 20 28 4f 53 5f 56 58 57 4f 52  YLE && (OS_VXWOR
f3ae0 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f  KS || defined(__
f3af0 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20 20 55 4e  APPLE__)).    UN
f3b00 49 58 56 46 53 28 22 75 6e 69 78 22 2c 20 20 20  IXVFS("unix",   
f3b10 20 20 20 20 20 20 20 61 75 74 6f 6c 6f 63 6b 49         autolockI
f3b20 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65 6c 73 65  oFinder ),.#else
f3b30 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22 75 6e  .    UNIXVFS("un
f3b40 69 78 22 2c 20 20 20 20 20 20 20 20 20 20 70 6f  ix",          po
f3b50 73 69 78 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23  sixIoFinder ),.#
f3b60 65 6e 64 69 66 0a 20 20 20 20 55 4e 49 58 56 46  endif.    UNIXVF
f3b70 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 20  S("unix-none",  
f3b80 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65     nolockIoFinde
f3b90 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53  r ),.    UNIXVFS
f3ba0 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c 65 22 2c  ("unix-dotfile",
f3bb0 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65    dotlockIoFinde
f3bc0 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53  r ),.    UNIXVFS
f3bd0 28 22 75 6e 69 78 2d 77 66 6c 22 2c 20 20 20 20  ("unix-wfl",    
f3be0 20 20 70 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64    posixWflIoFind
f3bf0 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f 56 58 57  er ),.#if OS_VXW
f3c00 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46 53  ORKS.    UNIXVFS
f3c10 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d 22  ("unix-namedsem"
f3c20 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72 20 29 2c  , semIoFinder ),
f3c30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
f3c40 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
f3c50 47 5f 53 54 59 4c 45 0a 20 20 20 20 55 4e 49 58  G_STYLE.    UNIX
f3c60 56 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 22  VFS("unix-posix"
f3c70 2c 20 20 20 20 70 6f 73 69 78 49 6f 46 69 6e 64  ,    posixIoFind
f3c80 65 72 20 29 2c 0a 23 69 66 20 21 4f 53 5f 56 58  er ),.#if !OS_VX
f3c90 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58 56 46  WORKS.    UNIXVF
f3ca0 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b 22 2c 20  S("unix-flock", 
f3cb0 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72     flockIoFinder
f3cc0 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69   ),.#endif.#endi
f3cd0 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  f.#if SQLITE_ENA
f3ce0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
f3cf0 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 41  E && defined(__A
f3d00 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55 4e 49 58  PPLE__).    UNIX
f3d10 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22 2c 20  VFS("unix-afp", 
f3d20 20 20 20 20 20 61 66 70 49 6f 46 69 6e 64 65 72       afpIoFinder
f3d30 20 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 28   ),.    UNIXVFS(
f3d40 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c 20 20 20  "unix-proxy",   
f3d50 20 70 72 6f 78 79 49 6f 46 69 6e 64 65 72 20 29   proxyIoFinder )
f3d60 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20  ,.#endif.  };.  
f3d70 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 20  unsigned int i; 
f3d80 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
f3d90 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 2f   counter */..  /
f3da0 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20 56  * Register all V
f3db0 46 53 65 73 20 64 65 66 69 6e 65 64 20 69 6e 20  FSes defined in 
f3dc0 74 68 65 20 61 56 66 73 5b 5d 20 61 72 72 61 79  the aVfs[] array
f3dd0 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   */.  for(i=0; i
f3de0 3c 28 73 69 7a 65 6f 66 28 61 56 66 73 29 2f 73  <(sizeof(aVfs)/s
f3df0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 66  izeof(sqlite3_vf
f3e00 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  s)); i++){.    s
f3e10 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
f3e20 74 65 72 28 26 61 56 66 73 5b 69 5d 2c 20 69 3d  ter(&aVfs[i], i=
f3e30 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  =0);.  }.  retur
f3e40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
f3e50 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20  ./*.** Shutdown 
f3e60 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
f3e70 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a  stem interface..
f3e80 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70 65 72 61  **.** Some opera
f3e90 74 69 6e 67 20 73 79 73 74 65 6d 73 20 6d 69 67  ting systems mig
f3ea0 68 74 20 6e 65 65 64 20 74 6f 20 64 6f 20 73 6f  ht need to do so
f3eb0 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e 20 74 68  me cleanup in th
f3ec0 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 74  is routine,.** t
f3ed0 6f 20 72 65 6c 65 61 73 65 20 64 79 6e 61 6d 69  o release dynami
f3ee0 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  cally allocated 
f3ef0 6f 62 6a 65 63 74 73 2e 20 20 42 75 74 20 6e 6f  objects.  But no
f3f00 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a 20 54 68  t on unix..** Th
f3f10 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
f3f20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69 78 2e 0a  no-op for unix..
f3f30 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
f3f40 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64  t sqlite3_os_end
f3f50 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 72  (void){ .  retur
f3f60 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a  n SQLITE_OK; .}.
f3f70 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49   .#endif /* SQLI
f3f80 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f 0a 0a 2f  TE_OS_UNIX */../
f3f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
f3fa0 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20  nd of os_unix.c 
f3fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f3fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
f3ff0 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 77 69 6e  egin file os_win
f4000 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
f4010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f4030 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 32  *.** 2004 May 22
f4040 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
f4050 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
f4060 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
f4070 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
f4080 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
f4090 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
f40a0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
f40b0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
f40c0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
f40d0 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
f40e0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
f40f0 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
f4100 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
f4110 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
f4120 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
f4130 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
f4140 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
f4150 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
f4160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f41a0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
f41b0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f  file contains co
f41c0 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63 69  de that is speci
f41d0 66 69 63 20 74 6f 20 77 69 6e 64 6f 77 73 2e 0a  fic to windows..
f41e0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
f41f0 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 20  _WIN            
f4200 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 20     /* This file 
f4210 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e 64  is used for wind
f4220 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f 2a  ows only */.../*
f4230 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 74  .** A Note About
f4240 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69   Memory Allocati
f4250 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64  on:.**.** This d
f4260 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f  river uses mallo
f4270 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65 63  c()/free() direc
f4280 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e 20  tly rather than 
f4290 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a  going through.**
f42a0 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61 70   the SQLite-wrap
f42b0 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c  pers sqlite3_mal
f42c0 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 72  loc()/sqlite3_fr
f42d0 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72 61  ee().  Those wra
f42e0 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 73  ppers.** are des
f42f0 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f 6e  igned for use on
f4300 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
f4310 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 69  s where memory i
f4320 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20  s scarce and.** 
f4330 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20  malloc failures 
f4340 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74 6c  happen frequentl
f4350 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20 6e  y.  Win32 does n
f4360 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 6e  ot typically run
f4370 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20   on.** embedded 
f4380 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 65  systems, and whe
f4390 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 65  n it does the de
f43a0 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c 6c  velopers normall
f43b0 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a 2a  y have bigger.**
f43c0 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f 72   problems to wor
f43d0 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 75  ry about than ru
f43e0 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 6d  nning out of mem
f43f0 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 69  ory.  So there i
f4400 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 65  s not.** a compe
f4410 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 73  lling need to us
f4420 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 0a  e the wrappers..
f4430 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 20  **.** But there 
f4440 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f 6e  is a good reason
f4450 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20   to not use the 
f4460 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 65  wrappers.  If we
f4470 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 70   use the.** wrap
f4480 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 6c  pers then we wil
f4490 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 20  l get simulated 
f44a0 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
f44b0 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a 2a  s within this.**
f44c0 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 68   driver.  And th
f44d0 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b 69  at causes all ki
f44e0 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20  nds of problems 
f44f0 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 20  for our tests.  
f4500 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61  We.** could enha
f4510 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 65  nce SQLite to de
f4520 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 65  al with simulate
f4530 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  d malloc failure
f4540 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20  s within.** the 
f4550 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 74  OS driver, but t
f4560 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c 20  he code to deal 
f4570 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c 75  with those failu
f4580 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20  re would not.** 
f4590 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e 20  be exercised on 
f45a0 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f 65  Linux (which doe
f45b0 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61  s not need to ma
f45c0 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 72  lloc() in the dr
f45d0 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f 20  iver).** and so 
f45e0 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 69  we would have di
f45f0 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e 67  fficulty writing
f4600 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 20   coverage tests 
f4610 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65  for that.** code
f4620 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 61  .  Better to lea
f4630 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 2c  ve the code out,
f4640 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a   we think..**.**
f4650 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68   The point of th
f4660 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 73  is discussion is
f4670 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 68   as follows:  Wh
f4680 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65  en creating a ne
f4690 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 6f  w.** OS layer fo
f46a0 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 79  r an embedded sy
f46b0 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 65  stem, if you use
f46c0 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 6e   this file as an
f46d0 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f   example,.** avo
f46e0 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d 61  id the use of ma
f46f0 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 20  lloc()/free().  
f4700 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 77  Those routines w
f4710 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f 77  ork ok on window
f4720 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75  s.** desktops bu
f4730 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e  t not so well in
f4740 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
f4750 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20  s..*/..#include 
f4760 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 66  <winbase.h>..#if
f4770 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a 23  def __CYGWIN__.#
f4780 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63 79   include <sys/cy
f4790 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a  gwin.h>.#endif..
f47a0 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65  /*.** Macros use
f47b0 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  d to determine w
f47c0 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f  hether or not to
f47d0 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f   use threads..*/
f47e0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 52  .#if defined(THR
f47f0 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52 45  EADSAFE) && THRE
f4800 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20  ADSAFE.# define 
f4810 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 41  SQLITE_W32_THREA
f4820 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  DS 1.#endif../*.
f4830 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20  ** Include code 
f4840 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
f4850 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c  o all os_*.c fil
f4860 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.*/./*********
f4870 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73  ***** Include os
f4880 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65  _common.h in the
f4890 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77 69   middle of os_wi
f48a0 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
f48b0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
f48c0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
f48d0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
f48e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f48f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4900 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
f4910 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68   May 22.**.** Th
f4920 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
f4930 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
f4940 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
f4950 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
f4960 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
f4970 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
f4980 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
f4990 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
f49a0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
f49b0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
f49c0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
f49d0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
f49e0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
f49f0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
f4a00 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
f4a10 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
f4a20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
f4a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
f4a80 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
f4a90 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20  ains macros and 
f4aa0 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20  a little bit of 
f4ab0 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d  code that is com
f4ac0 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66  mon to.** all of
f4ad0 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70   the platform-sp
f4ae0 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73  ecific files (os
f4af0 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e  _*.c) and is #in
f4b00 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73  cluded into thos
f4b10 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a  e.** files..**.*
f4b20 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75  * This file shou
f4b30 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20  ld be #included 
f4b40 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69  by the os_*.c fi
f4b50 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73  les only.  It is
f4b60 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61   not a.** genera
f4b70 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72  l purpose header
f4b80 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65   file..*/.#ifnde
f4b90 66 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a  f _OS_COMMON_H_.
f4ba0 23 64 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d  #define _OS_COMM
f4bb0 4f 4e 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20  ON_H_../*.** At 
f4bc0 6c 65 61 73 74 20 74 77 6f 20 62 75 67 73 20 68  least two bugs h
f4bd0 61 76 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62  ave slipped in b
f4be0 65 63 61 75 73 65 20 77 65 20 63 68 61 6e 67 65  ecause we change
f4bf0 64 20 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42  d the MEMORY_DEB
f4c00 55 47 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53  UG.** macro to S
f4c10 51 4c 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20  QLITE_DEBUG and 
f4c20 73 6f 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66  some older makef
f4c30 69 6c 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65  iles have not ye
f4c40 74 20 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77  t made the.** sw
f4c50 69 74 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  itch.  The follo
f4c60 77 69 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64  wing code should
f4c70 20 63 61 74 63 68 20 74 68 69 73 20 70 72 6f 62   catch this prob
f4c80 6c 65 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lem at compile-t
f4c90 69 6d 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d  ime..*/.#ifdef M
f4ca0 45 4d 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72  EMORY_DEBUG.# er
f4cb0 72 6f 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f  ror "The MEMORY_
f4cc0 44 45 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f  DEBUG macro is o
f4cd0 62 73 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51  bsolete.  Use SQ
f4ce0 4c 49 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65  LITE_DEBUG inste
f4cf0 61 64 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ad.".#endif..#if
f4d00 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
f4d10 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
f4d20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61  int sqlite3OSTra
f4d30 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  ce = 0;.#define 
f4d40 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20  OSTRACE1(X)     
f4d50 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
f4d60 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
f4d70 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23  DebugPrintf(X).#
f4d80 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28  define OSTRACE2(
f4d90 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73  X,Y)       if( s
f4da0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
f4db0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
f4dc0 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20  tf(X,Y).#define 
f4dd0 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20  OSTRACE3(X,Y,Z) 
f4de0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
f4df0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
f4e00 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
f4e10 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Z).#define OSTRA
f4e20 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69  CE4(X,Y,Z,A)   i
f4e30 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
f4e40 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
f4e50 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a  Printf(X,Y,Z,A).
f4e60 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35  #define OSTRACE5
f4e70 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20  (X,Y,Z,A,B) if( 
f4e80 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
f4e90 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
f4ea0 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  ntf(X,Y,Z,A,B).#
f4eb0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
f4ec0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20  X,Y,Z,A,B,C) \. 
f4ed0 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54     if(sqlite3OST
f4ee0 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
f4ef0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
f4f00 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,B,C).#define OS
f4f10 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE7(X,Y,Z,A,B
f4f20 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73  ,C,D) \.    if(s
f4f30 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73  qlite3OSTrace) s
f4f40 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
f4f50 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  f(X,Y,Z,A,B,C,D)
f4f60 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
f4f70 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69  STRACE1(X).#defi
f4f80 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
f4f90 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
f4fa0 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65  3(X,Y,Z).#define
f4fb0 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c   OSTRACE4(X,Y,Z,
f4fc0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
f4fd0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  CE5(X,Y,Z,A,B).#
f4fe0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
f4ff0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65  X,Y,Z,A,B,C).#de
f5000 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c  fine OSTRACE7(X,
f5010 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e  Y,Z,A,B,C,D).#en
f5020 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  dif../*.** Macro
f5030 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63  s for performanc
f5040 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d  e tracing.  Norm
f5050 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e  ally turned off.
f5060 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20    Only works.** 
f5070 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65  on i486 hardware
f5080 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
f5090 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54  TE_PERFORMANCE_T
f50a0 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74  RACE../* .** hwt
f50b0 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69  ime.h contains i
f50c0 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20  nline assembler 
f50d0 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65  code for impleme
f50e0 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70  nting .** high-p
f50f0 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e  erformance timin
f5100 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f  g routines..*/./
f5110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
f5120 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20  nclude hwtime.h 
f5130 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
f5140 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
f5150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f5160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
f5170 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65  egin file hwtime
f5180 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
f5190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f51a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
f51b0 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37  *.** 2008 May 27
f51c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
f51d0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
f51e0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
f51f0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
f5200 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
f5210 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
f5220 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
f5230 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
f5240 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
f5250 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
f5260 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
f5270 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
f5280 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
f5290 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
f52a0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
f52b0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
f52c0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
f52d0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
f52e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f52f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5320 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
f5330 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e  file contains in
f5340 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f  line asm code fo
f5350 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69  r retrieving "hi
f5360 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a  gh-performance".
f5370 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20  ** counters for 
f5380 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a  x86 class CPUs..
f5390 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49  */.#ifndef _HWTI
f53a0 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48  ME_H_.#define _H
f53b0 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  WTIME_H_../*.** 
f53c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
f53d0 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73  utine only works
f53e0 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73   on pentium-clas
f53f0 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f  s (or newer) pro
f5400 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75  cessors..** It u
f5410 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70  ses the RDTSC op
f5420 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65  code to read the
f5430 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c   cycle count val
f5440 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a  ue out of the.**
f5450 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72   processor and r
f5460 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75  eturns that valu
f5470 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20  e.  This can be 
f5480 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65  used for high-re
f5490 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a  s.** profiling..
f54a0 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28  */.#if (defined(
f54b0 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66  __GNUC__) || def
f54c0 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20  ined(_MSC_VER)) 
f54d0 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69  && \.      (defi
f54e0 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66  ned(i386) || def
f54f0 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c  ined(__i386__) |
f5500 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38  | defined(_M_IX8
f5510 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e  6))..  #if defin
f5520 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20  ed(__GNUC__)..  
f5530 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
f5540 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
f5550 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
f5560 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
f5570 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61  lo, hi;.     __a
f5580 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
f5590 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61  _ ("rdtsc" : "=a
f55a0 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69  " (lo), "=d" (hi
f55b0 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20  ));.     return 
f55c0 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68  (sqlite_uint64)h
f55d0 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20  i << 32 | lo;.  
f55e0 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e  }..  #elif defin
f55f0 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20  ed(_MSC_VER)..  
f5600 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64  __declspec(naked
f5610 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74  ) __inline sqlit
f5620 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c  e_uint64 __cdecl
f5630 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
f5640 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d  oid){.     __asm
f5650 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63   {.        rdtsc
f5660 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20  .        ret    
f5670 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75     ; return valu
f5680 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20  e at EDX:EAX.   
f5690 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69    }.  }..  #endi
f56a0 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65  f..#elif (define
f56b0 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64  d(__GNUC__) && d
f56c0 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f  efined(__x86_64_
f56d0 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f  _))..  __inline_
f56e0 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  _ sqlite_uint64 
f56f0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
f5700 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67  id){.      unsig
f5710 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20  ned long val;.  
f5720 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f      __asm__ __vo
f5730 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63  latile__ ("rdtsc
f5740 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b  " : "=A" (val));
f5750 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61  .      return va
f5760 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28  l;.  }. .#elif (
f5770 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
f5780 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70  ) && defined(__p
f5790 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69  pc__))..  __inli
f57a0 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74  ne__ sqlite_uint
f57b0 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
f57c0 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e  (void){.      un
f57d0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
f57e0 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75   retval;.      u
f57f0 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e  nsigned long jun
f5800 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f  k;.      __asm__
f5810 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22   __volatile__ ("
f5820 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a  \n\.          1:
f5830 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31        mftbu   %1
f5840 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
f5850 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c        mftb    %L
f5860 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  0\n\.           
f5870 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25         mftbu   %
f5880 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  0\n\.           
f5890 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25         cmpw    %
f58a0 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20  0,%1\n\.        
f58b0 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20            bne   
f58c0 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20    1b".          
f58d0 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28          : "=r" (
f58e0 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a  retval), "=r" (j
f58f0 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74  unk));.      ret
f5900 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a  urn retval;.  }.
f5910 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72  .#else..  #error
f5920 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61   Need implementa
f5930 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48  tion of sqlite3H
f5940 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72  wtime() for your
f5950 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a   platform...  /*
f5960 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65  .  ** To compile
f5970 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65   without impleme
f5980 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74  nting sqlite3Hwt
f5990 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70  ime() for your p
f59a0 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f  latform,.  ** yo
f59b0 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65  u can remove the
f59c0 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e   above #error an
f59d0 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77  d use the follow
f59e0 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75  ing.  ** stub fu
f59f0 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c  nction.  You wil
f5a00 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75  l lose timing su
f5a10 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20  pport for many. 
f5a20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67   ** of the debug
f5a30 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67  ging and testing
f5a40 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20   utilities, but 
f5a50 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a  it should at.  *
f5a60 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20  * least compile 
f5a70 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51  and run..  */.SQ
f5a80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73  LITE_PRIVATE   s
f5a90 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
f5aa0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
f5ab0 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74  { return ((sqlit
f5ac0 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a  e_uint64)0); }..
f5ad0 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
f5ae0 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49  * !defined(_HWTI
f5af0 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ME_H_) */../****
f5b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
f5b10 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a  f hwtime.h *****
f5b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5b30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
f5b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
f5b60 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
f5b70 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f  eft off in os_co
f5b80 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
f5b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74  *********/..stat
f5ba0 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  ic sqlite_uint64
f5bb0 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69 63   g_start;.static
f5bc0 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67   sqlite_uint64 g
f5bd0 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69 6e  _elapsed;.#defin
f5be0 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20 20  e TIMER_START   
f5bf0 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c 69      g_start=sqli
f5c00 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65 66  te3Hwtime().#def
f5c10 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20 20  ine TIMER_END   
f5c20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 3d        g_elapsed=
f5c30 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 2d  sqlite3Hwtime()-
f5c40 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65 20  g_start.#define 
f5c50 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20  TIMER_ELAPSED   
f5c60 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73    g_elapsed.#els
f5c70 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  e.#define TIMER_
f5c80 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54 49  START.#define TI
f5c90 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20  MER_END.#define 
f5ca0 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20 20  TIMER_ELAPSED   
f5cb0 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36    ((sqlite_uint6
f5cc0 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  4)0).#endif../*.
f5cd0 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65  ** If we compile
f5ce0 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45   with the SQLITE
f5cf0 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74 2c  _TEST macro set,
f5d00 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77   then the follow
f5d10 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20  ing block.** of 
f5d20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20 75  code will give u
f5d30 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74 6f  s the ability to
f5d40 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73 6b   simulate a disk
f5d50 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68 69   I/O error.  Thi
f5d60 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f 72  s.** is used for
f5d70 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f 4f   testing the I/O
f5d80 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e   recovery logic.
f5d90 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
f5da0 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
f5db0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
f5dc0 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20  _error_hit = 0; 
f5dd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
f5de0 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f  tal number of I/
f5df0 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49  O Errors */.SQLI
f5e00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
f5e10 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64  e3_io_error_hard
f5e20 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  hit = 0;        
f5e30 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e  /* Number of non
f5e40 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a  -benign errors *
f5e50 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
f5e60 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
f5e70 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20  r_pending = 0;  
f5e80 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64        /* Count d
f5e90 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f 4f  own to first I/O
f5ea0 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45   error */.SQLITE
f5eb0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
f5ec0 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73  _io_error_persis
f5ed0 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  t = 0;        /*
f5ee0 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72 72   True if I/O err
f5ef0 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a 53  ors persist */.S
f5f00 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
f5f10 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
f5f20 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20 20  enign = 0;      
f5f30 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65 72     /* True if er
f5f40 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e 20  rors are benign 
f5f50 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
f5f60 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75  t sqlite3_diskfu
f5f70 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a  ll_pending = 0;.
f5f80 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
f5f90 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20  qlite3_diskfull 
f5fa0 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d  = 0;.#define Sim
f5fb0 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
f5fc0 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69 6f  gn(X) sqlite3_io
f5fd0 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58  _error_benign=(X
f5fe0 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
f5ff0 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29 20  teIOError(CODE) 
f6000 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74 65   \.  if( (sqlite
f6010 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69  3_io_error_persi
f6020 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69 6f  st && sqlite3_io
f6030 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20  _error_hit) \.  
f6040 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f       || sqlite3_
f6050 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67  io_error_pending
f6060 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20  -- == 1 )  \.   
f6070 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f 63             { loc
f6080 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44 45  al_ioerr(); CODE
f6090 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ; }.static void 
f60a0 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20  local_ioerr(){. 
f60b0 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52 52   IOTRACE(("IOERR
f60c0 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65 33  \n"));.  sqlite3
f60d0 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b  _io_error_hit++;
f60e0 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f  .  if( !sqlite3_
f60f0 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20  io_error_benign 
f6100 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  ) sqlite3_io_err
f6110 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a  or_hardhit++;.}.
f6120 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
f6130 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f  DiskfullError(CO
f6140 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71 6c  DE) \.   if( sql
f6150 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
f6160 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20  nding ){ \.     
f6170 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73 6b  if( sqlite3_disk
f6180 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20  full_pending == 
f6190 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f  1 ){ \.       lo
f61a0 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20  cal_ioerr(); \. 
f61b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69        sqlite3_di
f61c0 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20  skfull = 1; \.  
f61d0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f       sqlite3_io_
f61e0 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c  error_hit = 1; \
f61f0 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a  .       CODE; \.
f6200 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20       }else{ \.  
f6210 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73       sqlite3_dis
f6220 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b  kfull_pending--;
f6230 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d   \.     } \.   }
f6240 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53  .#else.#define S
f6250 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65  imulateIOErrorBe
f6260 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65 20  nign(X).#define 
f6270 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
f6280 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  A).#define Simul
f6290 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72  ateDiskfullError
f62a0 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (A).#endif../*.*
f62b0 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20  * When testing, 
f62c0 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66 20  keep a count of 
f62d0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70  the number of op
f62e0 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66  en files..*/.#if
f62f0 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
f6300 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
f6310 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65  qlite3_open_file
f6320 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66  _count = 0;.#def
f6330 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28  ine OpenCounter(
f6340 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  X)  sqlite3_open
f6350 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29  _file_count+=(X)
f6360 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
f6370 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23 65  penCounter(X).#e
f6380 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  ndif..#endif /* 
f6390 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d  !defined(_OS_COM
f63a0 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a  MON_H_) */../***
f63b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
f63c0 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a  of os_common.h *
f63d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f63e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f63f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
f6400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
f6410 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
f6420 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77  left off in os_w
f6430 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
f6440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
f6450 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66  ** Some microsof
f6460 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b  t compilers lack
f6470 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f 6e   this definition
f6480 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56  ..*/.#ifndef INV
f6490 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42  ALID_FILE_ATTRIB
f64a0 55 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49 4e  UTES.# define IN
f64b0 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49  VALID_FILE_ATTRI
f64c0 42 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d 31  BUTES ((DWORD)-1
f64d0 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  ) .#endif../*.**
f64e0 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65   Determine if we
f64f0 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
f6500 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68  h WindowsCE - wh
f6510 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a  ich has a much.*
f6520 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a  * reduced API..*
f6530 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
f6540 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 41  WINCE.# define A
f6550 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28 29  reFileApisANSI()
f6560 20 31 0a 23 20 64 65 66 69 6e 65 20 46 6f 72 6d   1.# define Form
f6570 61 74 4d 65 73 73 61 67 65 57 28 61 2c 62 2c 63  atMessageW(a,b,c
f6580 2c 64 2c 65 2c 66 2c 67 29 20 30 0a 23 65 6e 64  ,d,e,f,g) 0.#end
f6590 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 43 45 20  if../*.** WinCE 
f65a0 6c 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75 70  lacks native sup
f65b0 70 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c 6f  port for file lo
f65c0 63 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76 65  cking so we have
f65d0 20 74 6f 20 66 61 6b 65 20 69 74 0a 2a 2a 20 77   to fake it.** w
f65e0 69 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f 66  ith some code of
f65f0 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66   our own..*/.#if
f6600 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
f6610 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
f6620 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69 6e  winceLock {.  in
f6630 74 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20 20  t nReaders;     
f6640 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
f6650 65 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74 61  eader locks obta
f6660 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62  ined */.  BOOL b
f6670 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f 2a  Pending;      /*
f6680 20 49 6e 64 69 63 61 74 65 73 20 61 20 70 65 6e   Indicates a pen
f6690 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65  ding lock has be
f66a0 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20  en obtained */. 
f66b0 20 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64 3b   BOOL bReserved;
f66c0 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74 65       /* Indicate
f66d0 73 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63  s a reserved loc
f66e0 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69  k has been obtai
f66f0 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62 45  ned */.  BOOL bE
f6700 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a 20  xclusive;    /* 
f6710 49 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78 63  Indicates an exc
f6720 6c 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73 20  lusive lock has 
f6730 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f  been obtained */
f6740 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23 65  .} winceLock;.#e
f6750 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
f6760 77 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75 72  winFile structur
f6770 65 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20  e is a subclass 
f6780 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  of sqlite3_file*
f6790 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68 65   specific to the
f67a0 20 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61 62   win32.** portab
f67b0 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a  ility layer..*/.
f67c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 77  typedef struct w
f67d0 69 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b 0a  inFile winFile;.
f67e0 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20 7b  struct winFile {
f67f0 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
f6800 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65  _io_methods *pMe
f6810 74 68 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65 20  thod;/* Must be 
f6820 66 69 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44 4c  first */.  HANDL
f6830 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  E h;            
f6840 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72     /* Handle for
f6850 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 66   accessing the f
f6860 69 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ile */.  unsigne
f6870 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b  d char locktype;
f6880 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b   /* Type of lock
f6890 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64 20   currently held 
f68a0 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a  on this file */.
f68b0 20 20 73 68 6f 72 74 20 73 68 61 72 65 64 4c 6f    short sharedLo
f68c0 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20 52 61 6e  ckByte;   /* Ran
f68d0 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79 74  domly chosen byt
f68e0 65 20 75 73 65 64 20 61 73 20 61 20 73 68 61 72  e used as a shar
f68f0 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 44 57 4f  ed lock */.  DWO
f6900 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20  RD lastErrno;   
f6910 20 20 20 20 20 2f 2a 20 54 68 65 20 57 69 6e 64       /* The Wind
f6920 6f 77 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74  ows errno from t
f6930 68 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f  he last I/O erro
f6940 72 20 2a 2f 0a 20 20 44 57 4f 52 44 20 73 65 63  r */.  DWORD sec
f6950 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20 2f  torSize;       /
f6960 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 6f 66  * Sector size of
f6970 20 74 68 65 20 64 65 76 69 63 65 20 66 69 6c 65   the device file
f6980 20 69 73 20 6f 6e 20 2a 2f 0a 23 69 66 20 53 51   is on */.#if SQ
f6990 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20  LITE_OS_WINCE.  
f69a0 57 43 48 41 52 20 2a 7a 44 65 6c 65 74 65 4f 6e  WCHAR *zDeleteOn
f69b0 43 6c 6f 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20  Close;  /* Name 
f69c0 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74  of file to delet
f69d0 65 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20 2a  e when closing *
f69e0 2f 0a 20 20 48 41 4e 44 4c 45 20 68 4d 75 74 65  /.  HANDLE hMute
f69f0 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  x;          /* M
f6a00 75 74 65 78 20 75 73 65 64 20 74 6f 20 63 6f 6e  utex used to con
f6a10 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 73  trol access to s
f6a20 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 20 20 0a  hared lock */  .
f6a30 20 20 48 41 4e 44 4c 45 20 68 53 68 61 72 65 64    HANDLE hShared
f6a40 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61  ;         /* Sha
f6a50 72 65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d 65  red memory segme
f6a60 6e 74 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  nt used for lock
f6a70 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f  ing */.  winceLo
f6a80 63 6b 20 6c 6f 63 61 6c 3b 20 20 20 20 20 20 20  ck local;       
f6a90 20 2f 2a 20 4c 6f 63 6b 73 20 6f 62 74 61 69 6e   /* Locks obtain
f6aa0 65 64 20 62 79 20 74 68 69 73 20 69 6e 73 74 61  ed by this insta
f6ab0 6e 63 65 20 6f 66 20 77 69 6e 46 69 6c 65 20 2a  nce of winFile *
f6ac0 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 2a 73  /.  winceLock *s
f6ad0 68 61 72 65 64 3b 20 20 20 20 20 20 2f 2a 20 47  hared;      /* G
f6ae0 6c 6f 62 61 6c 20 73 68 61 72 65 64 20 6c 6f 63  lobal shared loc
f6af0 6b 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65  k memory for the
f6b00 20 66 69 6c 65 20 20 2a 2f 0a 23 65 6e 64 69 66   file  */.#endif
f6b10 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61  .};../*.** Forwa
f6b20 72 64 20 70 72 6f 74 6f 74 79 70 65 73 2e 0a 2a  rd prototypes..*
f6b30 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
f6b40 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20 20  SectorSize(.    
f6b50 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
f6b60 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  s,.    const cha
f6b70 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20 20  r *zRelative    
f6b80 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20 6e   /* UTF-8 file n
f6b90 61 6d 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ame */.);../*.**
f6ba0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
f6bb0 61 72 69 61 62 6c 65 20 69 73 20 28 6e 6f 72 6d  ariable is (norm
f6bc0 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63 65 20 61  ally) set once a
f6bd0 6e 64 20 6e 65 76 65 72 20 63 68 61 6e 67 65 73  nd never changes
f6be0 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e 20  .** thereafter. 
f6bf0 20 49 74 20 72 65 63 6f 72 64 73 20 77 68 65 74   It records whet
f6c00 68 65 72 20 74 68 65 20 6f 70 65 72 61 74 69 6e  her the operatin
f6c10 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e 39  g system is Win9
f6c20 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a  5.** or WinNT..*
f6c30 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61 74  *.** 0:   Operat
f6c40 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e 6f  ing system unkno
f6c50 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f 70 65 72  wn..** 1:   Oper
f6c60 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20  ating system is 
f6c70 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20 20 20 4f  Win95..** 2:   O
f6c80 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
f6c90 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20  is WinNT..**.** 
f6ca0 49 6e 20 6f 72 64 65 72 20 74 6f 20 66 61 63 69  In order to faci
f6cb0 6c 69 74 61 74 65 20 74 65 73 74 69 6e 67 20 6f  litate testing o
f6cc0 6e 20 61 20 57 69 6e 4e 54 20 73 79 73 74 65 6d  n a WinNT system
f6cd0 2c 20 74 68 65 20 74 65 73 74 20 66 69 78 74 75  , the test fixtu
f6ce0 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e 75 61 6c  re.** can manual
f6cf0 6c 79 20 73 65 74 20 74 68 69 73 20 76 61 6c 75  ly set this valu
f6d00 65 20 74 6f 20 31 20 74 6f 20 65 6d 75 6c 61 74  e to 1 to emulat
f6d10 65 20 57 69 6e 39 38 20 62 65 68 61 76 69 6f 72  e Win98 behavior
f6d20 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
f6d30 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
f6d40 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
f6d50 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6c 73  s_type = 0;.#els
f6d60 65 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  e.static int sql
f6d70 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30  ite3_os_type = 0
f6d80 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
f6d90 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e  Return true (non
f6da0 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 65  -zero) if we are
f6db0 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57   running under W
f6dc0 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e  inNT, Win2K, Win
f6dd0 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e  XP,.** or WinCE.
f6de0 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28    Return false (
f6df0 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c  zero) for Win95,
f6e00 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45   Win98, or WinME
f6e10 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20  ..**.** Here is 
f6e20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f  an interesting o
f6e30 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e  bservation:  Win
f6e40 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57  95, Win98, and W
f6e50 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65  inME lack.** the
f6e60 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50   LockFileEx() AP
f6e70 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73  I.  But we can s
f6e80 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20  till statically 
f6e90 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61  link against tha
f6ea0 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67  t.** API as long
f6eb0 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c   as we don't cal
f6ec0 6c 20 69 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e  l it when runnin
f6ed0 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20  g Win95/98/ME.  
f6ee0 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69  A call to.** thi
f6ef0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
f6f00 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69  d to determine i
f6f10 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 69  f the host is Wi
f6f20 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20  n95/98/ME or.** 
f6f30 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74  WinNT/2K/XP so t
f6f40 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77  hat we will know
f6f50 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
f6f60 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61  we can safely ca
f6f70 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69  ll.** the LockFi
f6f80 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23  leEx() API..*/.#
f6f90 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
f6fa0 43 45 0a 23 20 64 65 66 69 6e 65 20 69 73 4e 54  CE.# define isNT
f6fb0 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20  ()  (1).#else.  
f6fc0 73 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54 28  static int isNT(
f6fd0 76 6f 69 64 29 7b 0a 20 20 20 20 69 66 28 20 73  void){.    if( s
f6fe0 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d  qlite3_os_type==
f6ff0 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 52  0 ){.      OSVER
f7000 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a  SIONINFO sInfo;.
f7010 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53        sInfo.dwOS
f7020 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20  VersionInfoSize 
f7030 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b  = sizeof(sInfo);
f7040 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69 6f  .      GetVersio
f7050 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20  nEx(&sInfo);.   
f7060 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79     sqlite3_os_ty
f7070 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61  pe = sInfo.dwPla
f7080 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41  tformId==VER_PLA
f7090 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f  TFORM_WIN32_NT ?
f70a0 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20   2 : 1;.    }.  
f70b0 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
f70c0 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 7d  _os_type==2;.  }
f70d0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
f70e0 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f  E_OS_WINCE */../
f70f0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55  *.** Convert a U
f7100 54 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 6d  TF-8 string to m
f7110 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65  icrosoft unicode
f7120 20 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a 0a   (UTF-16?). .**.
f7130 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  ** Space to hold
f7140 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
f7150 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64  ring is obtained
f7160 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f   from malloc..*/
f7170 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a 75  .static WCHAR *u
f7180 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e  tf8ToUnicode(con
f7190 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
f71a0 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61 72  me){.  int nChar
f71b0 3b 0a 20 20 57 43 48 41 52 20 2a 7a 57 69 64 65  ;.  WCHAR *zWide
f71c0 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43 68  Filename;..  nCh
f71d0 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f  ar = MultiByteTo
f71e0 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38  WideChar(CP_UTF8
f71f0 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  , 0, zFilename, 
f7200 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 20  -1, NULL, 0);.  
f7210 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20  zWideFilename = 
f7220 6d 61 6c 6c 6f 63 28 20 6e 43 68 61 72 2a 73 69  malloc( nChar*si
f7230 7a 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e 61  zeof(zWideFilena
f7240 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20  me[0]) );.  if( 
f7250 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d 30  zWideFilename==0
f7260 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
f7270 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 20  ;.  }.  nChar = 
f7280 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43  MultiByteToWideC
f7290 68 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20  har(CP_UTF8, 0, 
f72a0 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a  zFilename, -1, z
f72b0 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 6e 43  WideFilename, nC
f72c0 68 61 72 29 3b 0a 20 20 69 66 28 20 6e 43 68 61  har);.  if( nCha
f72d0 72 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 65  r==0 ){.    free
f72e0 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 3b  (zWideFilename);
f72f0 0a 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e 61  .    zWideFilena
f7300 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  me = 0;.  }.  re
f7310 74 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e 61  turn zWideFilena
f7320 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  me;.}../*.** Con
f7330 76 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20 75  vert microsoft u
f7340 6e 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38 2e  nicode to UTF-8.
f7350 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
f7360 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  the returned str
f7370 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e  ing is.** obtain
f7380 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ed from malloc()
f7390 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
f73a0 20 2a 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28   *unicodeToUtf8(
f73b0 63 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 69  const WCHAR *zWi
f73c0 64 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69  deFilename){.  i
f73d0 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72  nt nByte;.  char
f73e0 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20   *zFilename;..  
f73f0 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72  nByte = WideChar
f7400 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55  ToMultiByte(CP_U
f7410 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c  TF8, 0, zWideFil
f7420 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c  ename, -1, 0, 0,
f7430 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e   0, 0);.  zFilen
f7440 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42  ame = malloc( nB
f7450 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69  yte );.  if( zFi
f7460 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  lename==0 ){.   
f7470 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
f7480 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61   nByte = WideCha
f7490 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f  rToMultiByte(CP_
f74a0 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69  UTF8, 0, zWideFi
f74b0 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c  lename, -1, zFil
f74c0 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20  ename, nByte,.  
f74d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f74e0 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30              0, 0
f74f0 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d  );.  if( nByte =
f7500 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28  = 0 ){.    free(
f7510 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20  zFilename);.    
f7520 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20  zFilename = 0;. 
f7530 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c   }.  return zFil
f7540 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ename;.}../*.** 
f7550 43 6f 6e 76 65 72 74 20 61 6e 20 61 6e 73 69 20  Convert an ansi 
f7560 73 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73  string to micros
f7570 6f 66 74 20 75 6e 69 63 6f 64 65 2c 20 62 61 73  oft unicode, bas
f7580 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72  ed on the.** cur
f7590 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 73 65  rent codepage se
f75a0 74 74 69 6e 67 73 20 66 6f 72 20 66 69 6c 65 20  ttings for file 
f75b0 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 61  apis..** .** Spa
f75c0 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
f75d0 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
f75e0 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  s obtained.** fr
f75f0 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74  om malloc..*/.st
f7600 61 74 69 63 20 57 43 48 41 52 20 2a 6d 62 63 73  atic WCHAR *mbcs
f7610 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 74 20  ToUnicode(const 
f7620 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
f7630 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  {.  int nByte;. 
f7640 20 57 43 48 41 52 20 2a 7a 4d 62 63 73 46 69 6c   WCHAR *zMbcsFil
f7650 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64  ename;.  int cod
f7660 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41  epage = AreFileA
f7670 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41  pisANSI() ? CP_A
f7680 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a  CP : CP_OEMCP;..
f7690 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42    nByte = MultiB
f76a0 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f  yteToWideChar(co
f76b0 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65  depage, 0, zFile
f76c0 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 30  name, -1, NULL,0
f76d0 29 2a 73 69 7a 65 6f 66 28 57 43 48 41 52 29 3b  )*sizeof(WCHAR);
f76e0 0a 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65  .  zMbcsFilename
f76f0 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65   = malloc( nByte
f7700 2a 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69 6c  *sizeof(zMbcsFil
f7710 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69  ename[0]) );.  i
f7720 66 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65  f( zMbcsFilename
f7730 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
f7740 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65  n 0;.  }.  nByte
f7750 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69   = MultiByteToWi
f7760 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c  deChar(codepage,
f7770 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d   0, zFilename, -
f7780 31 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65  1, zMbcsFilename
f7790 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20  , nByte);.  if( 
f77a0 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20  nByte==0 ){.    
f77b0 66 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e 61  free(zMbcsFilena
f77c0 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46 69  me);.    zMbcsFi
f77d0 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a  lename = 0;.  }.
f77e0 20 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46 69    return zMbcsFi
f77f0 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  lename;.}../*.**
f7800 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f   Convert microso
f7810 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 6d 75  ft unicode to mu
f7820 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65  ltibyte characte
f7830 72 20 73 74 72 69 6e 67 2c 20 62 61 73 65 64 20  r string, based 
f7840 6f 6e 20 74 68 65 0a 2a 2a 20 75 73 65 72 27 73  on the.** user's
f7850 20 41 6e 73 69 20 63 6f 64 65 70 61 67 65 2e 0a   Ansi codepage..
f7860 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68  **.** Space to h
f7870 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64  old the returned
f7880 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69   string is obtai
f7890 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c  ned from.** mall
f78a0 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  oc()..*/.static 
f78b0 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 4d  char *unicodeToM
f78c0 62 63 73 28 63 6f 6e 73 74 20 57 43 48 41 52 20  bcs(const WCHAR 
f78d0 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b  *zWideFilename){
f78e0 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20  .  int nByte;.  
f78f0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b  char *zFilename;
f7900 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20  .  int codepage 
f7910 3d 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53  = AreFileApisANS
f7920 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43  I() ? CP_ACP : C
f7930 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74  P_OEMCP;..  nByt
f7940 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75  e = WideCharToMu
f7950 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 65  ltiByte(codepage
f7960 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61  , 0, zWideFilena
f7970 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c  me, -1, 0, 0, 0,
f7980 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65   0);.  zFilename
f7990 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65   = malloc( nByte
f79a0 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e   );.  if( zFilen
f79b0 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ame==0 ){.    re
f79c0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42  turn 0;.  }.  nB
f79d0 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f  yte = WideCharTo
f79e0 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61  MultiByte(codepa
f79f0 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65  ge, 0, zWideFile
f7a00 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e  name, -1, zFilen
f7a10 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 20  ame, nByte,.    
f7a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f7a30 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 3b            0, 0);
f7a40 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20  .  if( nByte == 
f7a50 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 46  0 ){.    free(zF
f7a60 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 46  ilename);.    zF
f7a70 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d  ilename = 0;.  }
f7a80 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e  .  return zFilen
f7a90 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ame;.}../*.** Co
f7aa0 6e 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65 20  nvert multibyte 
f7ab0 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67  character string
f7ac0 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63   to UTF-8.  Spac
f7ad0 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a  e to hold the.**
f7ae0 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67   returned string
f7af0 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f   is obtained fro
f7b00 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53  m malloc()..*/.S
f7b10 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a  QLITE_API char *
f7b20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62  sqlite3_win32_mb
f7b30 63 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73 74  cs_to_utf8(const
f7b40 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f7b50 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  ){.  char *zFile
f7b60 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48 41  nameUtf8;.  WCHA
f7b70 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20  R *zTmpWide;..  
f7b80 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 54  zTmpWide = mbcsT
f7b90 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61  oUnicode(zFilena
f7ba0 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57  me);.  if( zTmpW
f7bb0 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ide==0 ){.    re
f7bc0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46  turn 0;.  }.  zF
f7bd0 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 6e  ilenameUtf8 = un
f7be0 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d 70  icodeToUtf8(zTmp
f7bf0 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54  Wide);.  free(zT
f7c00 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72  mpWide);.  retur
f7c10 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b  n zFilenameUtf8;
f7c20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  .}../*.** Conver
f7c30 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74 69  t UTF-8 to multi
f7c40 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73  byte character s
f7c50 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 6f  tring.  Space to
f7c60 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72 65   hold the .** re
f7c70 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73  turned string is
f7c80 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d   obtained from m
f7c90 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
f7ca0 69 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f 4d  ic char *utf8ToM
f7cb0 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  bcs(const char *
f7cc0 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68  zFilename){.  ch
f7cd0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62 63  ar *zFilenameMbc
f7ce0 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d 70  s;.  WCHAR *zTmp
f7cf0 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64  Wide;..  zTmpWid
f7d00 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64  e = utf8ToUnicod
f7d10 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
f7d20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20  if( zTmpWide==0 
f7d30 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
f7d40 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65  .  }.  zFilename
f7d50 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54 6f  Mbcs = unicodeTo
f7d60 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b 0a  Mbcs(zTmpWide);.
f7d70 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29    free(zTmpWide)
f7d80 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65  ;.  return zFile
f7d90 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69 66  nameMbcs;.}..#if
f7da0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
f7db0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
f7dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
f7e00 68 69 73 20 73 65 63 74 69 6f 6e 20 63 6f 6e 74  his section cont
f7e10 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 57 69  ains code for Wi
f7e20 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a 0a  nCE only..*/./*.
f7e30 2a 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 6f 65  ** WindowsCE doe
f7e40 73 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f 63  s not have a loc
f7e50 61 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69 6f  altime() functio
f7e60 6e 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61 0a  n.  So create a.
f7e70 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0a 2a  ** substitute..*
f7e80 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f 5f 63  /.struct tm *__c
f7e90 64 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 28 63  decl localtime(c
f7ea0 6f 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 29 0a  onst time_t *t).
f7eb0 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 63  {.  static struc
f7ec0 74 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 54 49  t tm y;.  FILETI
f7ed0 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 20 53  ME uTm, lTm;.  S
f7ee0 59 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0a 20  YSTEMTIME pTm;. 
f7ef0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74   sqlite3_int64 t
f7f00 36 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b 0a  64;.  t64 = *t;.
f7f10 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20 31    t64 = (t64 + 1
f7f20 31 36 34 34 34 37 33 36 30 30 29 2a 31 30 30 30  1644473600)*1000
f7f30 30 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c 6f  0000;.  uTm.dwLo
f7f40 77 44 61 74 65 54 69 6d 65 20 3d 20 28 44 57 4f  wDateTime = (DWO
f7f50 52 44 29 28 74 36 34 20 26 20 30 78 46 46 46 46  RD)(t64 & 0xFFFF
f7f60 46 46 46 46 29 3b 0a 20 20 75 54 6d 2e 64 77 48  FFFF);.  uTm.dwH
f7f70 69 67 68 44 61 74 65 54 69 6d 65 3d 20 28 44 57  ighDateTime= (DW
f7f80 4f 52 44 29 28 74 36 34 20 3e 3e 20 33 32 29 3b  ORD)(t64 >> 32);
f7f90 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63  .  FileTimeToLoc
f7fa0 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c  alFileTime(&uTm,
f7fb0 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d  &lTm);.  FileTim
f7fc0 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26 6c  eToSystemTime(&l
f7fd0 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d  Tm,&pTm);.  y.tm
f7fe0 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65 61  _year = pTm.wYea
f7ff0 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d  r - 1900;.  y.tm
f8000 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74  _mon = pTm.wMont
f8010 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64  h - 1;.  y.tm_wd
f8020 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57  ay = pTm.wDayOfW
f8030 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79  eek;.  y.tm_mday
f8040 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79   = pTm.wDay;.  y
f8050 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77  .tm_hour = pTm.w
f8060 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e  Hour;.  y.tm_min
f8070 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a   = pTm.wMinute;.
f8080 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d    y.tm_sec = pTm
f8090 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75  .wSecond;.  retu
f80a0 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69  rn &y;.}../* Thi
f80b0 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  s will never be 
f80c0 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 66 69  called, but defi
f80d0 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20  ned to make the 
f80e0 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a  code compile */.
f80f0 23 64 65 66 69 6e 65 20 47 65 74 54 65 6d 70 50  #define GetTempP
f8100 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 66 69  athA(a,b)..#defi
f8110 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c  ne LockFile(a,b,
f8120 63 2c 64 2c 65 29 20 20 20 20 20 20 20 77 69 6e  c,d,e)       win
f8130 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62  ceLockFile(&a, b
f8140 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69  , c, d, e).#defi
f8150 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c  ne UnlockFile(a,
f8160 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 69 6e  b,c,d,e)     win
f8170 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c  ceUnlockFile(&a,
f8180 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65   b, c, d, e).#de
f8190 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28  fine LockFileEx(
f81a0 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77  a,b,c,d,e,f)   w
f81b0 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26  inceLockFileEx(&
f81c0 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66  a, b, c, d, e, f
f81d0 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e 44 4c  )..#define HANDL
f81e0 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20  E_TO_WINFILE(a) 
f81f0 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 68 61  (winFile*)&((cha
f8200 72 2a 29 61 29 5b 2d 28 69 6e 74 29 6f 66 66 73  r*)a)[-(int)offs
f8210 65 74 6f 66 28 77 69 6e 46 69 6c 65 2c 68 29 5d  etof(winFile,h)]
f8220 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20  ../*.** Acquire 
f8230 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68 61  a lock on the ha
f8240 6e 64 6c 65 20 68 0a 2a 2f 0a 73 74 61 74 69 63  ndle h.*/.static
f8250 20 76 6f 69 64 20 77 69 6e 63 65 4d 75 74 65 78   void winceMutex
f8260 41 63 71 75 69 72 65 28 48 41 4e 44 4c 45 20 68  Acquire(HANDLE h
f8270 29 7b 0a 20 20 20 44 57 4f 52 44 20 64 77 45 72  ){.   DWORD dwEr
f8280 72 3b 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20 20  r;.   do {.     
f8290 64 77 45 72 72 20 3d 20 57 61 69 74 46 6f 72 53  dwErr = WaitForS
f82a0 69 6e 67 6c 65 4f 62 6a 65 63 74 28 68 2c 20 49  ingleObject(h, I
f82b0 4e 46 49 4e 49 54 45 29 3b 0a 20 20 20 7d 20 77  NFINITE);.   } w
f82c0 68 69 6c 65 20 28 64 77 45 72 72 20 21 3d 20 57  hile (dwErr != W
f82d0 41 49 54 5f 4f 42 4a 45 43 54 5f 30 20 26 26 20  AIT_OBJECT_0 && 
f82e0 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 41 42  dwErr != WAIT_AB
f82f0 41 4e 44 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a 2a  ANDONED);.}./*.*
f8300 2a 20 52 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b  * Release a lock
f8310 20 61 63 71 75 69 72 65 64 20 62 79 20 77 69 6e   acquired by win
f8320 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 29  ceMutexAcquire()
f8330 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 77 69 6e 63  .*/.#define winc
f8340 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 68 29  eMutexRelease(h)
f8350 20 52 65 6c 65 61 73 65 4d 75 74 65 78 28 68 29   ReleaseMutex(h)
f8360 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74  ../*.** Create t
f8370 68 65 20 6d 75 74 65 78 20 61 6e 64 20 73 68 61  he mutex and sha
f8380 72 65 64 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  red memory used 
f8390 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74  for locking in t
f83a0 68 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72  he file.** descr
f83b0 69 70 74 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a 73  iptor pFile.*/.s
f83c0 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65  tatic BOOL wince
f83d0 43 72 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73 74  CreateLock(const
f83e0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f83f0 2c 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65  , winFile *pFile
f8400 29 7b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6f 6b  ){.  WCHAR *zTok
f8410 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4e 61 6d 65  ;.  WCHAR *zName
f8420 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65   = utf8ToUnicode
f8430 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 42  (zFilename);.  B
f8440 4f 4f 4c 20 62 49 6e 69 74 20 3d 20 54 52 55 45  OOL bInit = TRUE
f8450 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  ;..  /* Initiali
f8460 7a 65 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f 63  ze the local loc
f8470 6b 64 61 74 61 20 2a 2f 0a 20 20 5a 65 72 6f 4d  kdata */.  ZeroM
f8480 65 6d 6f 72 79 28 26 70 46 69 6c 65 2d 3e 6c 6f  emory(&pFile->lo
f8490 63 61 6c 2c 20 73 69 7a 65 6f 66 28 70 46 69 6c  cal, sizeof(pFil
f84a0 65 2d 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20 2f  e->local));..  /
f84b0 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 62 61  * Replace the ba
f84c0 63 6b 73 6c 61 73 68 65 73 20 66 72 6f 6d 20 74  ckslashes from t
f84d0 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20  he filename and 
f84e0 6c 6f 77 65 72 63 61 73 65 20 69 74 0a 20 20 2a  lowercase it.  *
f84f0 2a 20 74 6f 20 64 65 72 69 76 65 20 61 20 6d 75  * to derive a mu
f8500 74 65 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 7a  tex name. */.  z
f8510 54 6f 6b 20 3d 20 43 68 61 72 4c 6f 77 65 72 57  Tok = CharLowerW
f8520 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 20 28  (zName);.  for (
f8530 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b 0a  ;*zTok;zTok++){.
f8540 20 20 20 20 69 66 20 28 2a 7a 54 6f 6b 20 3d 3d      if (*zTok ==
f8550 20 27 5c 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20 27   '\\') *zTok = '
f8560 5f 27 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72  _';.  }..  /* Cr
f8570 65 61 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e 61  eate/open the na
f8580 6d 65 64 20 6d 75 74 65 78 20 2a 2f 0a 20 20 70  med mutex */.  p
f8590 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20 43  File->hMutex = C
f85a0 72 65 61 74 65 4d 75 74 65 78 57 28 4e 55 4c 4c  reateMutexW(NULL
f85b0 2c 20 46 41 4c 53 45 2c 20 7a 4e 61 6d 65 29 3b  , FALSE, zName);
f85c0 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68  .  if (!pFile->h
f85d0 4d 75 74 65 78 29 7b 0a 20 20 20 20 70 46 69 6c  Mutex){.    pFil
f85e0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47  e->lastErrno = G
f85f0 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
f8600 20 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a     free(zName);.
f8610 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53 45      return FALSE
f8620 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75  ;.  }..  /* Acqu
f8630 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62 65  ire the mutex be
f8640 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67 20  fore continuing 
f8650 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41  */.  winceMutexA
f8660 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d  cquire(pFile->hM
f8670 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20 53  utex);.  .  /* S
f8680 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20 6f  ince the names o
f8690 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73 2c  f named mutexes,
f86a0 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69 6c   semaphores, fil
f86b0 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20 61  e mappings etc a
f86c0 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73 65  re .  ** case-se
f86d0 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61 64  nsitive, take ad
f86e0 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74 20  vantage of that 
f86f0 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20 74  by uppercasing t
f8700 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20 20  he mutex name.  
f8710 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68 61  ** and using tha
f8720 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64 20  t as the shared 
f8730 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d 65  filemapping name
f8740 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 72 55 70 70  ..  */.  CharUpp
f8750 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 46  erW(zName);.  pF
f8760 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20 43  ile->hShared = C
f8770 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e 67  reateFileMapping
f8780 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45  W(INVALID_HANDLE
f8790 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20 20  _VALUE, NULL,.  
f87a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f87b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f87c0 20 20 20 20 20 50 41 47 45 5f 52 45 41 44 57 52       PAGE_READWR
f87d0 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28 77  ITE, 0, sizeof(w
f87e0 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20 20  inceLock),.     
f87f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8810 20 20 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20 2f    zName);  ..  /
f8820 2a 20 53 65 74 20 61 20 66 6c 61 67 20 74 68 61  * Set a flag tha
f8830 74 20 69 6e 64 69 63 61 74 65 73 20 77 65 27 72  t indicates we'r
f8840 65 20 74 68 65 20 66 69 72 73 74 20 74 6f 20 63  e the first to c
f8850 72 65 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79  reate the memory
f8860 20 73 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75 73   so it .  ** mus
f8870 74 20 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69 61  t be zero-initia
f8880 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28 47  lized */.  if (G
f8890 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 3d 3d  etLastError() ==
f88a0 20 45 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f 45   ERROR_ALREADY_E
f88b0 58 49 53 54 53 29 7b 0a 20 20 20 20 62 49 6e 69  XISTS){.    bIni
f88c0 74 20 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 0a  t = FALSE;.  }..
f88d0 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a 0a    free(zName);..
f88e0 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63 65    /* If we succe
f88f0 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20 74  eded in making t
f8900 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  he shared memory
f8910 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74 2e   handle, map it.
f8920 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d   */.  if (pFile-
f8930 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20 70  >hShared){.    p
f8940 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20 28  File->shared = (
f8950 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56 69  winceLock*)MapVi
f8960 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d 3e  ewOfFile(pFile->
f8970 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20 20 20  hShared, .      
f8980 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41 50 5f         FILE_MAP_
f8990 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57 52  READ|FILE_MAP_WR
f89a0 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65 6f  ITE, 0, 0, sizeo
f89b0 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20  f(winceLock));. 
f89c0 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e 67     /* If mapping
f89d0 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20 74   failed, close t
f89e0 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79  he shared memory
f89f0 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 61 73   handle and eras
f8a00 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28  e it */.    if (
f8a10 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29 7b  !pFile->shared){
f8a20 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
f8a30 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73  stErrno = GetLas
f8a40 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 20 20  tError();.      
f8a50 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c  CloseHandle(pFil
f8a60 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 20 20 20  e->hShared);.   
f8a70 20 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72 65     pFile->hShare
f8a80 64 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a  d = NULL;.    }.
f8a90 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68 61    }..  /* If sha
f8aa0 72 65 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c 64  red memory could
f8ab0 20 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64 2c   not be created,
f8ac0 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65 20   then close the 
f8ad0 6d 75 74 65 78 20 61 6e 64 20 66 61 69 6c 20 2a  mutex and fail *
f8ae0 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68  /.  if (pFile->h
f8af0 53 68 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29 7b  Shared == NULL){
f8b00 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 52  .    winceMutexR
f8b10 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d  elease(pFile->hM
f8b20 75 74 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73 65  utex);.    Close
f8b30 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d  Handle(pFile->hM
f8b40 75 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65  utex);.    pFile
f8b50 2d 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b  ->hMutex = NULL;
f8b60 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53  .    return FALS
f8b70 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 49  E;.  }.  .  /* I
f8b80 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73 68  nitialize the sh
f8b90 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 66 20 77  ared memory if w
f8ba0 65 27 72 65 20 73 75 70 70 6f 73 65 64 20 74 6f  e're supposed to
f8bb0 20 2a 2f 0a 20 20 69 66 20 28 62 49 6e 69 74 29   */.  if (bInit)
f8bc0 20 7b 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f 72   {.    ZeroMemor
f8bd0 79 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2c  y(pFile->shared,
f8be0 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f 63   sizeof(winceLoc
f8bf0 6b 29 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e 63  k));.  }..  winc
f8c00 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70 46  eMutexRelease(pF
f8c10 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20  ile->hMutex);.  
f8c20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a 0a  return TRUE;.}..
f8c30 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74 68  /*.** Destroy th
f8c40 65 20 70 61 72 74 20 6f 66 20 77 69 6e 46 69 6c  e part of winFil
f8c50 65 20 74 68 61 74 20 64 65 61 6c 73 20 77 69 74  e that deals wit
f8c60 68 20 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a 2f  h wince locks.*/
f8c70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e  .static void win
f8c80 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77 69  ceDestroyLock(wi
f8c90 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20  nFile *pFile){. 
f8ca0 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75 74   if (pFile->hMut
f8cb0 65 78 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71 75  ex){.    /* Acqu
f8cc0 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 2a 2f  ire the mutex */
f8cd0 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78 41  .    winceMutexA
f8ce0 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d  cquire(pFile->hM
f8cf0 75 74 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 54  utex);..    /* T
f8d00 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
f8d10 63 6b 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61  cks should proba
f8d20 62 6c 79 20 61 73 73 65 72 74 20 69 6e 20 64 65  bly assert in de
f8d30 62 75 67 20 6d 6f 64 65 2c 20 62 75 74 20 74 68  bug mode, but th
f8d40 65 79 0a 20 20 20 20 20 20 20 61 72 65 20 74 6f  ey.       are to
f8d50 20 63 6c 65 61 6e 75 70 20 69 6e 20 63 61 73 65   cleanup in case
f8d60 20 61 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61 69   any locks remai
f8d70 6e 65 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20 20  ned open */.    
f8d80 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
f8d90 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20 20  .nReaders){.    
f8da0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f8db0 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20 20  >nReaders --;.  
f8dc0 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c    }.    if (pFil
f8dd0 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72 76  e->local.bReserv
f8de0 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  ed){.      pFile
f8df0 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72  ->shared->bReser
f8e00 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ved = FALSE;.   
f8e10 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65   }.    if (pFile
f8e20 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67  ->local.bPending
f8e30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
f8e40 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67  shared->bPending
f8e50 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d 0a   = FALSE;.    }.
f8e60 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c      if (pFile->l
f8e70 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 29  ocal.bExclusive)
f8e80 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73  {.      pFile->s
f8e90 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76  hared->bExclusiv
f8ea0 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d  e = FALSE;.    }
f8eb0 0a 0a 20 20 20 20 2f 2a 20 44 65 2d 72 65 66 65  ..    /* De-refe
f8ec0 72 65 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65 20  rence and close 
f8ed0 6f 75 72 20 63 6f 70 79 20 6f 66 20 74 68 65 20  our copy of the 
f8ee0 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68 61  shared memory ha
f8ef0 6e 64 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d 61  ndle */.    Unma
f8f00 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c  pViewOfFile(pFil
f8f10 65 2d 3e 73 68 61 72 65 64 29 3b 0a 20 20 20 20  e->shared);.    
f8f20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69 6c  CloseHandle(pFil
f8f30 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20 20  e->hShared);..  
f8f40 20 20 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20 74    /* Done with t
f8f50 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20  he mutex */.    
f8f60 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61 73  winceMutexReleas
f8f70 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29  e(pFile->hMutex)
f8f80 3b 20 20 20 20 0a 20 20 20 20 43 6c 6f 73 65 48  ;    .    CloseH
f8f90 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d 75  andle(pFile->hMu
f8fa0 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d  tex);.    pFile-
f8fb0 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b 0a  >hMutex = NULL;.
f8fc0 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e    }.}../* .** An
f8fd0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
f8fe0 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 28  of the LockFile(
f8ff0 29 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77 73  ) API of windows
f9000 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73 74   for wince.*/.st
f9010 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65 4c  atic BOOL winceL
f9020 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44 4c  ockFile(.  HANDL
f9030 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f  E *phFile,.  DWO
f9040 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 4c  RD dwFileOffsetL
f9050 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69  ow,.  DWORD dwFi
f9060 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20 20  leOffsetHigh,.  
f9070 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42  DWORD nNumberOfB
f9080 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20  ytesToLockLow,. 
f9090 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66   DWORD nNumberOf
f90a0 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 0a  BytesToLockHigh.
f90b0 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  ){.  winFile *pF
f90c0 69 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f  ile = HANDLE_TO_
f90d0 57 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29 3b  WINFILE(phFile);
f90e0 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e 20  .  BOOL bReturn 
f90f0 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 55 4e 55 53  = FALSE;..  UNUS
f9100 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 77 46  ED_PARAMETER(dwF
f9110 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29 3b 0a  ileOffsetHigh);.
f9120 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
f9130 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65  ER(nNumberOfByte
f9140 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20  sToLockHigh);.. 
f9150 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75   if (!pFile->hMu
f9160 74 65 78 29 20 72 65 74 75 72 6e 20 54 52 55 45  tex) return TRUE
f9170 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63  ;.  winceMutexAc
f9180 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75  quire(pFile->hMu
f9190 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 57 61 6e 74  tex);..  /* Want
f91a0 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ing an exclusive
f91b0 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 69 66 20 28   lock? */.  if (
f91c0 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
f91d0 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44  == (DWORD)SHARED
f91e0 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20 26 26  _FIRST.       &&
f91f0 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
f9200 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f  oLockLow == (DWO
f9210 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29 7b  RD)SHARED_SIZE){
f9220 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f9230 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72 73  shared->nReaders
f9240 20 3d 3d 20 30 20 26 26 20 70 46 69 6c 65 2d 3e   == 0 && pFile->
f9250 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69  shared->bExclusi
f9260 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20 20 20  ve == 0){.      
f9270 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
f9280 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52 55  bExclusive = TRU
f9290 45 3b 0a 20 20 20 20 20 20 20 70 46 69 6c 65 2d  E;.       pFile-
f92a0 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76  >local.bExclusiv
f92b0 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20  e = TRUE;.      
f92c0 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b   bReturn = TRUE;
f92d0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
f92e0 20 57 61 6e 74 20 61 20 72 65 61 64 2d 6f 6e 6c   Want a read-onl
f92f0 79 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73  y lock? */.  els
f9300 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73  e if (dwFileOffs
f9310 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29  etLow == (DWORD)
f9320 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a  SHARED_FIRST &&.
f9330 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d 62             nNumb
f9340 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c  erOfBytesToLockL
f9350 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66  ow == 1){.    if
f9360 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d   (pFile->shared-
f9370 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20 30  >bExclusive == 0
f9380 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
f9390 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b  local.nReaders +
f93a0 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46 69  +;.      if (pFi
f93b0 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65  le->local.nReade
f93c0 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20  rs == 1){.      
f93d0 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f93e0 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20  >nReaders ++;.  
f93f0 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65 74      }.      bRet
f9400 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
f9410 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74  }.  }..  /* Want
f9420 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f   a pending lock?
f9430 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64   */.  else if (d
f9440 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
f9450 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e 47  = (DWORD)PENDING
f9460 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65 72  _BYTE && nNumber
f9470 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77  OfBytesToLockLow
f9480 20 3d 3d 20 31 29 7b 0a 20 20 20 20 2f 2a 20 49   == 1){.    /* I
f9490 66 20 6e 6f 20 70 65 6e 64 69 6e 67 20 6c 6f 63  f no pending loc
f94a0 6b 20 68 61 73 20 62 65 65 6e 20 61 63 71 75 69  k has been acqui
f94b0 72 65 64 2c 20 74 68 65 6e 20 61 63 71 75 69 72  red, then acquir
f94c0 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20 28  e it */.    if (
f94d0 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
f94e0 50 65 6e 64 69 6e 67 20 3d 3d 20 30 29 20 7b 0a  Pending == 0) {.
f94f0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
f9500 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 20  red->bPending = 
f9510 54 52 55 45 3b 0a 20 20 20 20 20 20 70 46 69 6c  TRUE;.      pFil
f9520 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e  e->local.bPendin
f9530 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20  g = TRUE;.      
f9540 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a  bReturn = TRUE;.
f9550 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
f9560 57 61 6e 74 20 61 20 72 65 73 65 72 76 65 64 20  Want a reserved 
f9570 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65 20  lock? */.  else 
f9580 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74  if (dwFileOffset
f9590 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52 45  Low == (DWORD)RE
f95a0 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e  SERVED_BYTE && n
f95b0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
f95c0 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20  ockLow == 1){.  
f95d0 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61    if (pFile->sha
f95e0 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d  red->bReserved =
f95f0 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69  = 0) {.      pFi
f9600 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73  le->shared->bRes
f9610 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20  erved = TRUE;.  
f9620 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c      pFile->local
f9630 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52 55  .bReserved = TRU
f9640 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
f9650 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
f9660 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78   }..  winceMutex
f9670 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68  Release(pFile->h
f9680 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  Mutex);.  return
f9690 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a   bReturn;.}../*.
f96a0 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ** An implementa
f96b0 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c 6f  tion of the Unlo
f96c0 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77 69  ckFile API of wi
f96d0 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a  ndows for wince.
f96e0 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77  */.static BOOL w
f96f0 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 0a  inceUnlockFile(.
f9700 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65    HANDLE *phFile
f9710 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65  ,.  DWORD dwFile
f9720 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f  OffsetLow,.  DWO
f9730 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48  RD dwFileOffsetH
f9740 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75  igh,.  DWORD nNu
f9750 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c  mberOfBytesToUnl
f9760 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20  ockLow,.  DWORD 
f9770 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f9780 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20  UnlockHigh.){.  
f9790 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  winFile *pFile =
f97a0 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49   HANDLE_TO_WINFI
f97b0 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f  LE(phFile);.  BO
f97c0 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c  OL bReturn = FAL
f97d0 53 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41  SE;..  UNUSED_PA
f97e0 52 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f 66  RAMETER(dwFileOf
f97f0 66 73 65 74 48 69 67 68 29 3b 0a 20 20 55 4e 55  fsetHigh);.  UNU
f9800 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 4e  SED_PARAMETER(nN
f9810 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
f9820 6c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 69 66  lockHigh);..  if
f9830 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78   (!pFile->hMutex
f9840 29 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 20  ) return TRUE;. 
f9850 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75 69   winceMutexAcqui
f9860 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  re(pFile->hMutex
f9870 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69  );..  /* Releasi
f9880 6e 67 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b  ng a reader lock
f9890 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65   or an exclusive
f98a0 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 20 28 64   lock */.  if (d
f98b0 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d  wFileOffsetLow =
f98c0 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f  = (DWORD)SHARED_
f98d0 46 49 52 53 54 29 7b 0a 20 20 20 20 2f 2a 20 44  FIRST){.    /* D
f98e0 69 64 20 77 65 20 68 61 76 65 20 61 6e 20 65 78  id we have an ex
f98f0 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f  clusive lock? */
f9900 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f9910 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65  local.bExclusive
f9920 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
f9930 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f9940 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57  UnlockLow == (DW
f9950 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29  ORD)SHARED_SIZE)
f9960 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ;.      pFile->l
f9970 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65 20  ocal.bExclusive 
f9980 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70  = FALSE;.      p
f9990 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45  File->shared->bE
f99a0 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45  xclusive = FALSE
f99b0 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20  ;.      bReturn 
f99c0 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a 20  = TRUE;.    }.. 
f99d0 20 20 20 2f 2a 20 44 69 64 20 77 65 20 6a 75 73     /* Did we jus
f99e0 74 20 68 61 76 65 20 61 20 72 65 61 64 65 72 20  t have a reader 
f99f0 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c 73  lock? */.    els
f9a00 65 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63  e if (pFile->loc
f9a10 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20  al.nReaders){.  
f9a20 20 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d 62      assert(nNumb
f9a30 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63  erOfBytesToUnloc
f9a40 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 53  kLow == (DWORD)S
f9a50 48 41 52 45 44 5f 53 49 5a 45 20 7c 7c 20 6e 4e  HARED_SIZE || nN
f9a60 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
f9a70 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 3b 0a 20  lockLow == 1);. 
f9a80 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
f9a90 6c 2e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20  l.nReaders --;. 
f9aa0 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e       if (pFile->
f9ab0 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20 3d  local.nReaders =
f9ac0 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a 20 20 20  = 0).      {.   
f9ad0 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
f9ae0 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b  ed->nReaders --;
f9af0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
f9b00 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20  Return = TRUE;. 
f9b10 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52     }.  }..  /* R
f9b20 65 6c 65 61 73 69 6e 67 20 61 20 70 65 6e 64 69  eleasing a pendi
f9b30 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c 73  ng lock */.  els
f9b40 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73  e if (dwFileOffs
f9b50 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29  etLow == (DWORD)
f9b60 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20  PENDING_BYTE && 
f9b70 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f9b80 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b  UnlockLow == 1){
f9b90 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f9ba0 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b  local.bPending){
f9bb0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
f9bc0 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 46  cal.bPending = F
f9bd0 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c  ALSE;.      pFil
f9be0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64  e->shared->bPend
f9bf0 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ing = FALSE;.   
f9c00 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55     bReturn = TRU
f9c10 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f  E;.    }.  }.  /
f9c20 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65  * Releasing a re
f9c30 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20  served lock */. 
f9c40 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65   else if (dwFile
f9c50 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57  OffsetLow == (DW
f9c60 4f 52 44 29 52 45 53 45 52 56 45 44 5f 42 59 54  ORD)RESERVED_BYT
f9c70 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79  E && nNumberOfBy
f9c80 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d  tesToUnlockLow =
f9c90 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70 46  = 1){.    if (pF
f9ca0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65  ile->local.bRese
f9cb0 72 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70 46  rved) {.      pF
f9cc0 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65  ile->local.bRese
f9cd0 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20  rved = FALSE;.  
f9ce0 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
f9cf0 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46  d->bReserved = F
f9d00 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65 74  ALSE;.      bRet
f9d10 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  urn = TRUE;.    
f9d20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75  }.  }..  winceMu
f9d30 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65  texRelease(pFile
f9d40 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74  ->hMutex);.  ret
f9d50 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a  urn bReturn;.}..
f9d60 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65  /*.** An impleme
f9d70 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c  ntation of the L
f9d80 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 20  ockFileEx() API 
f9d90 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77  of windows for w
f9da0 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42  ince.*/.static B
f9db0 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c  OOL winceLockFil
f9dc0 65 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a 70  eEx(.  HANDLE *p
f9dd0 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20 64  hFile,.  DWORD d
f9de0 77 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44 20  wFlags,.  DWORD 
f9df0 64 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44 57  dwReserved,.  DW
f9e00 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74  ORD nNumberOfByt
f9e10 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20 44  esToLockLow,.  D
f9e20 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79  WORD nNumberOfBy
f9e30 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a 20  tesToLockHigh,. 
f9e40 20 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c 70   LPOVERLAPPED lp
f9e50 4f 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20 20  Overlapped.){.  
f9e60 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f9e70 28 64 77 52 65 73 65 72 76 65 64 29 3b 0a 20 20  (dwReserved);.  
f9e80 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
f9e90 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54  (nNumberOfBytesT
f9ea0 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 2f  oLockHigh);..  /
f9eb0 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20  * If the caller 
f9ec0 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20 72  wants a shared r
f9ed0 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61 72  ead lock, forwar
f9ee0 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a  d this call.  **
f9ef0 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c   to winceLockFil
f9f00 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76 65  e */.  if (lpOve
f9f10 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74 20  rlapped->Offset 
f9f20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44  == (DWORD)SHARED
f9f30 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20  _FIRST &&.      
f9f40 64 77 46 6c 61 67 73 20 3d 3d 20 31 20 26 26 0a  dwFlags == 1 &&.
f9f50 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66 42        nNumberOfB
f9f60 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d  ytesToLockLow ==
f9f70 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f 53   (DWORD)SHARED_S
f9f80 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  IZE){.    return
f9f90 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 70   winceLockFile(p
f9fa0 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49  hFile, SHARED_FI
f9fb0 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20  RST, 0, 1, 0);. 
f9fc0 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c 53   }.  return FALS
f9fd0 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f  E;.}./*.** End o
f9fe0 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f  f the special co
f9ff0 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a  de for wince.***
fa000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64  **********/.#end
fa050 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f  if /* SQLITE_OS_
fa060 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  WINCE */../*****
fa070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa0b0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  ********.** The 
fa0c0 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72 6f  next group of ro
fa0d0 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74  utines implement
fa0e0 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73   the I/O methods
fa0f0 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62 79   specified.** by
fa100 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f   the sqlite3_io_
fa110 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a  methods object..
fa120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fa160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
fa170 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66  ./*.** Close a f
fa180 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  ile..**.** It is
fa190 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20 61   reported that a
fa1a0 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c 6f  n attempt to clo
fa1b0 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67 68  se a handle migh
fa1c0 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 66  t sometimes.** f
fa1d0 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61 20  ail.  This is a 
fa1e0 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62 6c  very unreasonabl
fa1f0 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 77 69  e result, but wi
fa200 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69 6f  ndows is notorio
fa210 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67 20  us.** for being 
fa220 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f 20  unreasonable so 
fa230 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20 74  I do not doubt t
fa240 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 61 70  hat it might hap
fa250 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20  pen.  If.** the 
fa260 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65 20  close fails, we 
fa270 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d 69  pause for 100 mi
fa280 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74  lliseconds and t
fa290 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a 2a  ry again.  As.**
fa2a0 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f 53   many as MX_CLOS
fa2b0 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d 70  E_ATTEMPT attemp
fa2c0 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20  ts to close the 
fa2d0 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65 20  handle are made 
fa2e0 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e 67  before.** giving
fa2f0 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e   up and returnin
fa300 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23  g an error..*/.#
fa310 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45 5f  define MX_CLOSE_
fa320 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69 63  ATTEMPT 3.static
fa330 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73 71   int winClose(sq
fa340 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
fa350 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20 3d  .  int rc, cnt =
fa360 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70   0;.  winFile *p
fa370 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
fa380 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  )id;..  assert( 
fa390 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41  id!=0 );.  OSTRA
fa3a0 43 45 32 28 22 43 4c 4f 53 45 20 25 64 5c 6e 22  CE2("CLOSE %d\n"
fa3b0 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 64  , pFile->h);.  d
fa3c0 6f 7b 0a 20 20 20 20 72 63 20 3d 20 43 6c 6f 73  o{.    rc = Clos
fa3d0 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68  eHandle(pFile->h
fa3e0 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d  );.  }while( rc=
fa3f0 3d 30 20 26 26 20 2b 2b 63 6e 74 20 3c 20 4d 58  =0 && ++cnt < MX
fa400 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20 26  _CLOSE_ATTEMPT &
fa410 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31  & (Sleep(100), 1
fa420 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  ) );.#if SQLITE_
fa430 4f 53 5f 57 49 4e 43 45 0a 23 64 65 66 69 6e 65  OS_WINCE.#define
fa440 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f   WINCE_DELETION_
fa450 41 54 54 45 4d 50 54 53 20 33 0a 20 20 77 69 6e  ATTEMPTS 3.  win
fa460 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 70 46  ceDestroyLock(pF
fa470 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c  ile);.  if( pFil
fa480 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73  e->zDeleteOnClos
fa490 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74  e ){.    int cnt
fa4a0 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28   = 0;.    while(
fa4b0 0a 20 20 20 20 20 20 20 20 20 20 20 44 65 6c 65  .           Dele
fa4c0 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d 3e 7a  teFileW(pFile->z
fa4d0 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3d 3d  DeleteOnClose)==
fa4e0 30 0a 20 20 20 20 20 20 20 20 26 26 20 47 65 74  0.        && Get
fa4f0 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28  FileAttributesW(
fa500 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e  pFile->zDeleteOn
fa510 43 6c 6f 73 65 29 21 3d 30 78 66 66 66 66 66 66  Close)!=0xffffff
fa520 66 66 20 0a 20 20 20 20 20 20 20 20 26 26 20 63  ff .        && c
fa530 6e 74 2b 2b 20 3c 20 57 49 4e 43 45 5f 44 45 4c  nt++ < WINCE_DEL
fa540 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 0a 20  ETION_ATTEMPTS. 
fa550 20 20 20 29 7b 0a 20 20 20 20 20 20 20 53 6c 65     ){.       Sle
fa560 65 70 28 31 30 30 29 3b 20 20 2f 2a 20 57 61 69  ep(100);  /* Wai
fa570 74 20 61 20 6c 69 74 74 6c 65 20 62 65 66 6f 72  t a little befor
fa580 65 20 74 72 79 69 6e 67 20 61 67 61 69 6e 20 2a  e trying again *
fa590 2f 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65  /.    }.    free
fa5a0 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f  (pFile->zDeleteO
fa5b0 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 65 6e  nClose);.  }.#en
fa5c0 64 69 66 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65  dif.  OpenCounte
fa5d0 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e 20  r(-1);.  return 
fa5e0 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  rc ? SQLITE_OK :
fa5f0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 7d   SQLITE_IOERR;.}
fa600 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63  ../*.** Some mic
fa610 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72 73  rosoft compilers
fa620 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69 6e   lack this defin
fa630 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ition..*/.#ifnde
fa640 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49  f INVALID_SET_FI
fa650 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64 65 66  LE_POINTER.# def
fa660 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f  ine INVALID_SET_
fa670 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28 44  FILE_POINTER ((D
fa680 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69 66 0a  WORD)-1).#endif.
fa690 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61  ./*.** Read data
fa6a0 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74   from a file int
fa6b0 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74  o a buffer.  Ret
fa6c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  urn SQLITE_OK if
fa6d0 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65   all.** bytes we
fa6e0 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66  re read successf
fa6f0 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f  ully and SQLITE_
fa700 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e  IOERR if anythin
fa710 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e  g goes.** wrong.
fa720 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
fa730 69 6e 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65  inRead(.  sqlite
fa740 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20  3_file *id,     
fa750 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20       /* File to 
fa760 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76  read from */.  v
fa770 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20  oid *pBuf,      
fa780 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
fa790 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20  te content into 
fa7a0 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a 20  this buffer */. 
fa7b0 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20   int amt,       
fa7c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
fa7d0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
fa7e0 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69  o read */.  sqli
fa7f0 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74  te3_int64 offset
fa800 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20         /* Begin 
fa810 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20  reading at this 
fa820 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 4c  offset */.){.  L
fa830 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d 20  ONG upperBits = 
fa840 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e 3e  (LONG)((offset>>
fa850 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66 66  32) & 0x7fffffff
fa860 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42  );.  LONG lowerB
fa870 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66 66  its = (LONG)(off
fa880 73 65 74 20 26 20 30 78 66 66 66 66 66 66 66 66  set & 0xffffffff
fa890 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20  );.  DWORD rc;. 
fa8a0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
fa8b0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
fa8c0 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a 20    DWORD error;. 
fa8d0 20 44 57 4f 52 44 20 67 6f 74 3b 0a 0a 20 20 61   DWORD got;..  a
fa8e0 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
fa8f0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
fa900 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
fa910 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20 20 4f  IOERR_READ);.  O
fa920 53 54 52 41 43 45 33 28 22 52 45 41 44 20 25 64  STRACE3("READ %d
fa930 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69   lock=%d\n", pFi
fa940 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f  le->h, pFile->lo
fa950 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20  cktype);.  rc = 
fa960 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70  SetFilePointer(p
fa970 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69  File->h, lowerBi
fa980 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20  ts, &upperBits, 
fa990 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69  FILE_BEGIN);.  i
fa9a0 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53  f( rc==INVALID_S
fa9b0 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20  ET_FILE_POINTER 
fa9c0 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73  && (error=GetLas
fa9d0 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52  tError())!=NO_ER
fa9e0 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  ROR ){.    pFile
fa9f0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
faa00 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ror;.    return 
faa10 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d  SQLITE_FULL;.  }
faa20 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 6c 65  .  if( !ReadFile
faa30 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c  (pFile->h, pBuf,
faa40 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 20 29   amt, &got, 0) )
faa50 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
faa60 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
faa70 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65 74  Error();.    ret
faa80 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
faa90 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66 28  _READ;.  }.  if(
faaa0 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d 74   got==(DWORD)amt
faab0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
faac0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
faad0 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61 64  e{.    /* Unread
faae0 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 62 75   parts of the bu
faaf0 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65 72  ffer must be zer
fab00 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20 20  o-filled */.    
fab10 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a 29  memset(&((char*)
fab20 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20 61  pBuf)[got], 0, a
fab30 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65 74  mt-got);.    ret
fab40 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
fab50 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 7d  _SHORT_READ;.  }
fab60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
fab70 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66 66  data from a buff
fab80 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20  er into a file. 
fab90 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
faba0 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20  K on success.** 
fabb0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72  or some other er
fabc0 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69 6c  ror code on fail
fabd0 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
fabe0 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 20 73  nt winWrite(.  s
fabf0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fac00 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
fac10 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 2a   to write into *
fac20 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
fac30 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 2f 2a  pBuf,         /*
fac40 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62 65   The bytes to be
fac50 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e   written */.  in
fac60 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20  t amt,          
fac70 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
fac80 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72  r of bytes to wr
fac90 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ite */.  sqlite3
faca0 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20  _int64 offset   
facb0 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
facc0 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62 65  o the file to be
facd0 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20 2a  gin writing at *
face0 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65  /.){.  LONG uppe
facf0 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 28  rBits = (LONG)((
fad00 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 30 78  offset>>32) & 0x
fad10 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f 4e  7fffffff);.  LON
fad20 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28 4c  G lowerBits = (L
fad30 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30 78  ONG)(offset & 0x
fad40 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57 4f  ffffffff);.  DWO
fad50 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65  RD rc;.  winFile
fad60 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
fad70 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44 20  le*)id;.  DWORD 
fad80 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20 77  error;.  DWORD w
fad90 72 6f 74 65 20 3d 20 30 3b 0a 0a 20 20 61 73 73  rote = 0;..  ass
fada0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
fadb0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
fadc0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
fadd0 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 53 69  ERR_WRITE);.  Si
fade0 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72  mulateDiskfullEr
fadf0 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54  ror(return SQLIT
fae00 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52 41  E_FULL);.  OSTRA
fae10 43 45 33 28 22 57 52 49 54 45 20 25 64 20 6c 6f  CE3("WRITE %d lo
fae20 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  ck=%d\n", pFile-
fae30 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  >h, pFile->lockt
fae40 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65 74  ype);.  rc = Set
fae50 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69 6c  FilePointer(pFil
fae60 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73 2c  e->h, lowerBits,
fae70 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49 4c   &upperBits, FIL
fae80 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28 20  E_BEGIN);.  if( 
fae90 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54 5f  rc==INVALID_SET_
faea0 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26 20  FILE_POINTER && 
faeb0 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45 72  (error=GetLastEr
faec0 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f 52  ror())!=NO_ERROR
faed0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
faee0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f 72  astErrno = error
faef0 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
faf00 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20  ITE_FULL;.  }.  
faf10 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b  assert( amt>0 );
faf20 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 61  .  while(.     a
faf30 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28 72 63  mt>0.     && (rc
faf40 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70 46 69   = WriteFile(pFi
faf50 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d 74  le->h, pBuf, amt
faf60 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21 3d 30  , &wrote, 0))!=0
faf70 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65 3e 30  .     && wrote>0
faf80 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d 3d  .  ){.    amt -=
faf90 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75 66   wrote;.    pBuf
fafa0 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75 66   = &((char*)pBuf
fafb0 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20 20  )[wrote];.  }.  
fafc0 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74 3e 28  if( !rc || amt>(
fafd0 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20 20 20  int)wrote ){.   
fafe0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
faff0 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  o = GetLastError
fb000 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  ();.    return S
fb010 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a  QLITE_FULL;.  }.
fb020 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fb030 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  OK;.}../*.** Tru
fb040 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69  ncate an open fi
fb050 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65  le to a specifie
fb060 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63  d size.*/.static
fb070 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61 74 65   int winTruncate
fb080 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
fb090 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  d, sqlite3_int64
fb0a0 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 47 20   nByte){.  LONG 
fb0b0 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e  upperBits = (LON
fb0c0 47 29 28 28 6e 42 79 74 65 3e 3e 33 32 29 20 26  G)((nByte>>32) &
fb0d0 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20   0x7fffffff);.  
fb0e0 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d  LONG lowerBits =
fb0f0 20 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20 26 20   (LONG)(nByte & 
fb100 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44  0xffffffff);.  D
fb110 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69  WORD rc;.  winFi
fb120 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
fb130 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52  File*)id;.  DWOR
fb140 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65  D error;..  asse
fb150 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f  rt( id!=0 );.  O
fb160 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 41 54  STRACE3("TRUNCAT
fb170 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46  E %d %lld\n", pF
fb180 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b 0a  ile->h, nByte);.
fb190 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
fb1a0 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  r(return SQLITE_
fb1b0 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29 3b  IOERR_TRUNCATE);
fb1c0 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50  .  rc = SetFileP
fb1d0 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c  ointer(pFile->h,
fb1e0 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70   lowerBits, &upp
fb1f0 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47  erBits, FILE_BEG
fb200 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49  IN);.  if( rc==I
fb210 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f  NVALID_SET_FILE_
fb220 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 72 6f  POINTER && (erro
fb230 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  r=GetLastError()
fb240 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  )!=NO_ERROR ){. 
fb250 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
fb260 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20  rno = error;.   
fb270 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
fb280 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 20  OERR_TRUNCATE;. 
fb290 20 7d 0a 20 20 2f 2a 20 53 65 74 45 6e 64 4f 66   }.  /* SetEndOf
fb2a0 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 6c 20 69  File will fail i
fb2b0 66 20 6e 42 79 74 65 20 69 73 20 6e 65 67 61 74  f nByte is negat
fb2c0 69 76 65 20 2a 2f 0a 20 20 69 66 28 20 21 53 65  ive */.  if( !Se
fb2d0 74 45 6e 64 4f 66 46 69 6c 65 28 70 46 69 6c 65  tEndOfFile(pFile
fb2e0 2d 3e 68 29 20 29 7b 0a 20 20 20 20 70 46 69 6c  ->h) ){.    pFil
fb2f0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 47  e->lastErrno = G
fb300 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
fb310 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fb320 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b  _IOERR_TRUNCATE;
fb330 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
fb340 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64  LITE_OK;.}..#ifd
fb350 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f  ef SQLITE_TEST./
fb360 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e  *.** Count the n
fb370 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79 6e  umber of fullsyn
fb380 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73 79  cs and normal sy
fb390 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 75 73  ncs.  This is us
fb3a0 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68  ed to test.** th
fb3b0 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75 6c  at syncs and ful
fb3c0 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75 72  lsyncs are occur
fb3d0 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 68 74  ing at the right
fb3e0 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   times..*/.SQLIT
fb3f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
fb400 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30  3_sync_count = 0
fb410 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
fb420 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79 6e   sqlite3_fullsyn
fb430 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  c_count = 0;.#en
fb440 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  dif../*.** Make 
fb450 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20  sure all writes 
fb460 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  to a particular 
fb470 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74  file are committ
fb480 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a 73  ed to disk..*/.s
fb490 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 79 6e  tatic int winSyn
fb4a0 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  c(sqlite3_file *
fb4b0 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  id, int flags){.
fb4c0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4e  #ifndef SQLITE_N
fb4d0 4f 5f 53 59 4e 43 0a 20 20 77 69 6e 46 69 6c 65  O_SYNC.  winFile
fb4e0 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69   *pFile = (winFi
fb4f0 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72  le*)id;..  asser
fb500 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53  t( id!=0 );.  OS
fb510 54 52 41 43 45 33 28 22 53 59 4e 43 20 25 64 20  TRACE3("SYNC %d 
fb520 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c  lock=%d\n", pFil
fb530 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63  e->h, pFile->loc
fb540 6b 74 79 70 65 29 3b 0a 23 65 6c 73 65 0a 20 20  ktype);.#else.  
fb550 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
fb560 28 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  (id);.#endif.#if
fb570 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  ndef SQLITE_TEST
fb580 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
fb590 54 45 52 28 66 6c 61 67 73 29 3b 0a 23 65 6c 73  TER(flags);.#els
fb5a0 65 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  e.  if( flags & 
fb5b0 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c  SQLITE_SYNC_FULL
fb5c0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
fb5d0 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b  fullsync_count++
fb5e0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
fb5f0 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  sync_count++;.#e
fb600 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 65 20  ndif.  /* If we 
fb610 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68  compiled with th
fb620 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43  e SQLITE_NO_SYNC
fb630 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63   flag, then sync
fb640 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f  ing is a.  ** no
fb650 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20  -op.  */.#ifdef 
fb660 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20  SQLITE_NO_SYNC. 
fb670 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fb680 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28  _OK;.#else.  if(
fb690 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65 72   FlushFileBuffer
fb6a0 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20  s(pFile->h) ){. 
fb6b0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fb6c0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
fb6d0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
fb6e0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  no = GetLastErro
fb6f0 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  r();.    return 
fb700 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20  SQLITE_IOERR;.  
fb710 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
fb720 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20  * Determine the 
fb730 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20  current size of 
fb740 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73 0a  a file in bytes.
fb750 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
fb760 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65  nFileSize(sqlite
fb770 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69  3_file *id, sqli
fb780 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65  te3_int64 *pSize
fb790 29 7b 0a 20 20 44 57 4f 52 44 20 75 70 70 65 72  ){.  DWORD upper
fb7a0 42 69 74 73 3b 0a 20 20 44 57 4f 52 44 20 6c 6f  Bits;.  DWORD lo
fb7b0 77 65 72 42 69 74 73 3b 0a 20 20 77 69 6e 46 69  werBits;.  winFi
fb7c0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
fb7d0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52  File*)id;.  DWOR
fb7e0 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73 65  D error;..  asse
fb7f0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 53  rt( id!=0 );.  S
fb800 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72  imulateIOError(r
fb810 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
fb820 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c 6f 77  RR_FSTAT);.  low
fb830 65 72 42 69 74 73 20 3d 20 47 65 74 46 69 6c 65  erBits = GetFile
fb840 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c 20 26  Size(pFile->h, &
fb850 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20 69 66  upperBits);.  if
fb860 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73 20 3d  (   (lowerBits =
fb870 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 53  = INVALID_FILE_S
fb880 49 5a 45 29 0a 20 20 20 20 20 26 26 20 28 28 65  IZE).     && ((e
fb890 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72  rror = GetLastEr
fb8a0 72 6f 72 28 29 29 20 21 3d 20 4e 4f 5f 45 52 52  ror()) != NO_ERR
fb8b0 4f 52 29 20 29 0a 20 20 7b 0a 20 20 20 20 70 46  OR) ).  {.    pF
fb8c0 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
fb8d0 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75   error;.    retu
fb8e0 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
fb8f0 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70 53  FSTAT;.  }.  *pS
fb900 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74 65 33  ize = (((sqlite3
fb910 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69 74 73  _int64)upperBits
fb920 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72 42 69  )<<32) + lowerBi
fb930 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ts;.  return SQL
fb940 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
fb950 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49   LOCKFILE_FAIL_I
fb960 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 75 6e  MMEDIATELY is un
fb970 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d 65 20  defined on some 
fb980 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73 2e  Windows systems.
fb990 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f 43 4b  .*/.#ifndef LOCK
fb9a0 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44 49  FILE_FAIL_IMMEDI
fb9b0 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 20 4c  ATELY.# define L
fb9c0 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d  OCKFILE_FAIL_IMM
fb9d0 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e 64 69  EDIATELY 1.#endi
fb9e0 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65  f../*.** Acquire
fb9f0 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a   a reader lock..
fba00 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41 50 49  ** Different API
fba10 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61   routines are ca
fba20 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f  lled depending o
fba30 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  n whether or not
fba40 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69 6e 39   this.** is Win9
fba50 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f 0a 73  5 or WinNT..*/.s
fba60 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65 61  tatic int getRea
fba70 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70  dLock(winFile *p
fba80 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73  File){.  int res
fba90 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29  ;.  if( isNT() )
fbaa0 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 45 44  {.    OVERLAPPED
fbab0 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c 70 2e   ovlp;.    ovlp.
fbac0 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f  Offset = SHARED_
fbad0 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c 70 2e  FIRST;.    ovlp.
fbae0 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30 3b 0a  OffsetHigh = 0;.
fbaf0 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e 74 20      ovlp.hEvent 
fbb00 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c  = 0;.    res = L
fbb10 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c 65 2d  ockFileEx(pFile-
fbb20 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49  >h, LOCKFILE_FAI
fbb30 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c 0a 20  L_IMMEDIATELY,. 
fbb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fbb50 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f 53 49      0, SHARED_SI
fbb60 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b 0a 2f  ZE, 0, &ovlp);./
fbb70 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66  * isNT() is 1 if
fbb80 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
fbb90 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73  ==1, so this els
fbba0 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75  e is never execu
fbbb0 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c  ted. .*/.#if SQL
fbbc0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
fbbd0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74    }else{.    int
fbbe0 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   lk;.    sqlite3
fbbf0 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65  _randomness(size
fbc00 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a 20 20  of(lk), &lk);.  
fbc10 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c    pFile->sharedL
fbc20 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f 72 74  ockByte = (short
fbc30 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66 66 66  )((lk & 0x7fffff
fbc40 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a 45  ff)%(SHARED_SIZE
fbc50 20 2d 20 31 29 29 3b 0a 20 20 20 20 72 65 73 20   - 1));.    res 
fbc60 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  = LockFile(pFile
fbc70 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ->h, SHARED_FIRS
fbc80 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c  T+pFile->sharedL
fbc90 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30  ockByte, 0, 1, 0
fbca0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20  );.#endif.  }.  
fbcb0 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 7b 0a  if( res == 0 ){.
fbcc0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
fbcd0 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72  rrno = GetLastEr
fbce0 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ror();.  }.  ret
fbcf0 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn res;.}../*.*
fbd00 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f 63  * Undo a readloc
fbd10 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  k.*/.static int 
fbd20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 77  unlockReadLock(w
fbd30 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a  inFile *pFile){.
fbd40 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66 28    int res;.  if(
fbd50 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 72   isNT() ){.    r
fbd60 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  es = UnlockFile(
fbd70 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45 44  pFile->h, SHARED
fbd80 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52 45  _FIRST, 0, SHARE
fbd90 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a 20 69  D_SIZE, 0);./* i
fbda0 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51  sNT() is 1 if SQ
fbdb0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31  LITE_OS_WINCE==1
fbdc0 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69  , so this else i
fbdd0 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64  s never executed
fbde0 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  . .*/.#if SQLITE
fbdf0 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d  _OS_WINCE==0.  }
fbe00 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 3d 20  else{.    res = 
fbe10 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  UnlockFile(pFile
fbe20 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ->h, SHARED_FIRS
fbe30 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61 72 65  T + pFile->share
fbe40 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c  dLockByte, 0, 1,
fbe50 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a   0);.#endif.  }.
fbe60 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29    if( res == 0 )
fbe70 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73  {.    pFile->las
fbe80 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74  tErrno = GetLast
fbe90 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72  Error();.  }.  r
fbea0 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a  eturn res;.}../*
fbeb0 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c  .** Lock the fil
fbec0 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20  e with the lock 
fbed0 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72  specified by par
fbee0 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20  ameter locktype 
fbef0 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20  - one.** of the 
fbf00 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
fbf10 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f       (1) SHARED_
fbf20 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20  LOCK.**     (2) 
fbf30 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a  RESERVED_LOCK.**
fbf40 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47       (3) PENDING
fbf50 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29  _LOCK.**     (4)
fbf60 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a   EXCLUSIVE_LOCK.
fbf70 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20  **.** Sometimes 
fbf80 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20  when requesting 
fbf90 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20  one lock state, 
fbfa0 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20  additional lock 
fbfb0 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e  states.** are in
fbfc0 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65  serted in betwee
fbfd0 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20  n.  The locking 
fbfe0 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e  might fail on on
fbff0 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a  e of the later.*
fc000 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65  * transitions le
fc010 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73  aving the lock s
fc020 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66  tate different f
fc030 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72  rom what it star
fc040 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c  ted but.** still
fc050 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f   short of its go
fc060 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  al.  The followi
fc070 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74  ng chart shows t
fc080 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72  he allowed.** tr
fc090 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68  ansitions and th
fc0a0 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72  e inserted inter
fc0b0 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a  mediate states:.
fc0c0 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45  **.**    UNLOCKE
fc0d0 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20  D -> SHARED.**  
fc0e0 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45    SHARED -> RESE
fc0f0 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45  RVED.**    SHARE
fc100 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d  D -> (PENDING) -
fc110 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20  > EXCLUSIVE.**  
fc120 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50    RESERVED -> (P
fc130 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55  ENDING) -> EXCLU
fc140 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49  SIVE.**    PENDI
fc150 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  NG -> EXCLUSIVE.
fc160 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
fc170 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63  ne will only inc
fc180 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54  rease a lock.  T
fc190 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72  he winUnlock() r
fc1a0 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73  outine.** erases
fc1b0 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e   all locks at on
fc1c0 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75  ce and returns u
fc1d0 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f  s immediately to
fc1e0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30   locking level 0
fc1f0 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70  ..** It is not p
fc200 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72  ossible to lower
fc210 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
fc220 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61  el one step at a
fc230 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d   time.  You.** m
fc240 75 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20  ust go straight 
fc250 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  to locking level
fc260 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e   0..*/.static in
fc270 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 65  t winLock(sqlite
fc280 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
fc290 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74  locktype){.  int
fc2a0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
fc2b0 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
fc2c0 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69  de from subrouti
fc2d0 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73  nes */.  int res
fc2e0 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20   = 1;           
fc2f0 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77  /* Result of a w
fc300 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c  indows lock call
fc310 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63   */.  int newLoc
fc320 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20  ktype;       /* 
fc330 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  Set pFile->lockt
fc340 79 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75  ype to this valu
fc350 65 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67  e before exiting
fc360 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e   */.  int gotPen
fc370 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20  dingLock = 0;/* 
fc380 54 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69  True if we acqui
fc390 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f  red a PENDING lo
fc3a0 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a  ck this time */.
fc3b0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
fc3c0 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b   = (winFile*)id;
fc3d0 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20 3d  .  DWORD error =
fc3e0 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 61 73   NO_ERROR;..  as
fc3f0 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
fc400 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b 20   OSTRACE5("LOCK 
fc410 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 29  %d %d was %d(%d)
fc420 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 70  \n",.          p
fc430 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
fc440 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  e, pFile->lockty
fc450 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65  pe, pFile->share
fc460 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20 2f  dLockByte);..  /
fc470 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c  * If there is al
fc480 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66 20  ready a lock of 
fc490 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f 72  this type or mor
fc4a0 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f 6e  e restrictive on
fc4b0 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c 65   the.  ** OsFile
fc4c0 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44 6f  , do nothing. Do
fc4d0 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64 5f  n't use the end_
fc4e0 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c  lock: exit path,
fc4f0 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33   as.  ** sqlite3
fc500 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 20 68  OsEnterMutex() h
fc510 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65  asn't been calle
fc520 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  d yet..  */.  if
fc530 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
fc540 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20  e>=locktype ){. 
fc550 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
fc560 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  _OK;.  }..  /* M
fc570 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63  ake sure the loc
fc580 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73  king sequence is
fc590 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20   correct.  */.  
fc5a0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
fc5b0 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b  ocktype!=NO_LOCK
fc5c0 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48   || locktype==SH
fc5d0 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
fc5e0 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21  ssert( locktype!
fc5f0 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b  =PENDING_LOCK );
fc600 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
fc610 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype!=RESERVED_LO
fc620 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63  CK || pFile->loc
fc630 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
fc640 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b  CK );..  /* Lock
fc650 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   the PENDING_LOC
fc660 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65  K byte if we nee
fc670 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50  d to acquire a P
fc680 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20  ENDING lock or. 
fc690 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63   ** a SHARED loc
fc6a0 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63  k.  If we are ac
fc6b0 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44  quiring a SHARED
fc6c0 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69   lock, the acqui
fc6d0 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74  sition of.  ** t
fc6e0 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  he PENDING_LOCK 
fc6f0 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72  byte is temporar
fc700 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63  y..  */.  newLoc
fc710 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c  ktype = pFile->l
fc720 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 20  ocktype;.  if(  
fc730 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70   (pFile->locktyp
fc740 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20 20 20  e==NO_LOCK).    
fc750 20 7c 7c 20 28 20 20 20 28 6c 6f 63 6b 74 79 70   || (   (locktyp
fc760 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
fc770 4b 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  K).         && (
fc780 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
fc790 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29 29  =RESERVED_LOCK))
fc7a0 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e  .  ){.    int cn
fc7b0 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 6c 65  t = 3;.    while
fc7c0 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 72 65  ( cnt-->0 && (re
fc7d0 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69  s = LockFile(pFi
fc7e0 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42  le->h, PENDING_B
fc7f0 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 3d 3d  YTE, 0, 1, 0))==
fc800 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 72  0 ){.      /* Tr
fc810 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 65 74  y 3 times to get
fc820 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63   the pending loc
fc830 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20  k.  The pending 
fc840 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a 20 20  lock might be.  
fc850 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 20 61      ** held by a
fc860 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 70 72  nother reader pr
fc870 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c 20 72  ocess who will r
fc880 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 6e 74  elease it moment
fc890 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a  arily..      */.
fc8a0 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22        OSTRACE2("
fc8b0 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 61 20  could not get a 
fc8c0 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63 6e  PENDING lock. cn
fc8d0 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b 0a 20  t=%d\n", cnt);. 
fc8e0 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a 20       Sleep(1);. 
fc8f0 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 6e 64     }.    gotPend
fc900 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b 0a 20  ingLock = res;. 
fc910 20 20 20 69 66 28 20 21 72 65 73 20 29 7b 0a 20     if( !res ){. 
fc920 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74       error = Get
fc930 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20  LastError();.   
fc940 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71   }.  }..  /* Acq
fc950 75 69 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f  uire a shared lo
fc960 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  ck.  */.  if( lo
fc970 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
fc980 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20  OCK && res ){.  
fc990 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
fc9a0 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f  >locktype==NO_LO
fc9b0 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20  CK );.    res = 
fc9c0 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  getReadLock(pFil
fc9d0 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20  e);.    if( res 
fc9e0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
fc9f0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f  type = SHARED_LO
fca00 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  CK;.    }else{. 
fca10 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65 74       error = Get
fca20 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20  LastError();.   
fca30 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71   }.  }..  /* Acq
fca40 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44 20  uire a RESERVED 
fca50 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
fca60 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56  locktype==RESERV
fca70 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29  ED_LOCK && res )
fca80 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  {.    assert( pF
fca90 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53  ile->locktype==S
fcaa0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
fcab0 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65    res = LockFile
fcac0 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52  (pFile->h, RESER
fcad0 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20  VED_BYTE, 0, 1, 
fcae0 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20  0);.    if( res 
fcaf0 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b  ){.      newLock
fcb00 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f  type = RESERVED_
fcb10 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  LOCK;.    }else{
fcb20 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47  .      error = G
fcb30 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
fcb40 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41     }.  }..  /* A
fcb50 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47  cquire a PENDING
fcb60 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
fcb70 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55   locktype==EXCLU
fcb80 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73  SIVE_LOCK && res
fcb90 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b 74   ){.    newLockt
fcba0 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f  ype = PENDING_LO
fcbb0 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64 69  CK;.    gotPendi
fcbc0 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a  ngLock = 0;.  }.
fcbd0 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e  .  /* Acquire an
fcbe0 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a   EXCLUSIVE lock.
fcbf0 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
fcc00 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
fcc10 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20  OCK && res ){.  
fcc20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
fcc30 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45  >locktype>=SHARE
fcc40 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65  D_LOCK );.    re
fcc50 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f  s = unlockReadLo
fcc60 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f  ck(pFile);.    O
fcc70 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 64 6c  STRACE2("unreadl
fcc80 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73  ock = %d\n", res
fcc90 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63  );.    res = Loc
fcca0 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
fccb0 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30 2c  SHARED_FIRST, 0,
fccc0 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30 29   SHARED_SIZE, 0)
fccd0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ;.    if( res ){
fcce0 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
fccf0 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c  pe = EXCLUSIVE_L
fcd00 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  OCK;.    }else{.
fcd10 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65        error = Ge
fcd20 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
fcd30 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 65 72      OSTRACE2("er
fcd40 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22  ror-code = %d\n"
fcd50 2c 20 65 72 72 6f 72 29 3b 0a 20 20 20 20 20 20  , error);.      
fcd60 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  getReadLock(pFil
fcd70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  e);.    }.  }.. 
fcd80 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f   /* If we are ho
fcd90 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20  lding a PENDING 
fcda0 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20  lock that ought 
fcdb0 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20  to be released, 
fcdc0 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73  then.  ** releas
fcdd0 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20  e it now..  */. 
fcde0 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c   if( gotPendingL
fcdf0 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d  ock && locktype=
fce00 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
fce10 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70      UnlockFile(p
fce20 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
fce30 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
fce40 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74  .  }..  /* Updat
fce50 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74  e the state of t
fce60 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64  he lock has held
fce70 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65 73   in the file des
fce80 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a  criptor then.  *
fce90 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70 70  * return the app
fcea0 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74 20  ropriate result 
fceb0 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  code..  */.  if(
fcec0 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63 20 3d   res ){.    rc =
fced0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
fcee0 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43 45  lse{.    OSTRACE
fcef0 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25  4("LOCK FAILED %
fcf00 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20  d trying for %d 
fcf10 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70  but got %d\n", p
fcf20 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
fcf30 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65      locktype, ne
fcf40 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20 20  wLocktype);.    
fcf50 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
fcf60 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 63   = error;.    rc
fcf70 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
fcf80 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63    }.  pFile->loc
fcf90 6b 74 79 70 65 20 3d 20 28 75 38 29 6e 65 77 4c  ktype = (u8)newL
fcfa0 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72  ocktype;.  retur
fcfb0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
fcfc0 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
fcfd0 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ks if there is a
fcfe0 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68   RESERVED lock h
fcff0 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69  eld on the speci
fd000 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20  fied.** file by 
fd010 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65  this or any othe
fd020 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75  r process. If su
fd030 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c  ch a lock is hel
fd040 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e  d, return.** non
fd050 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73 65  -zero, otherwise
fd060 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63   zero..*/.static
fd070 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52 65 73   int winCheckRes
fd080 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
fd090 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
fd0a0 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e 74  *pResOut){.  int
fd0b0 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a   rc;.  winFile *
fd0c0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
fd0d0 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28  *)id;..  assert(
fd0e0 20 69 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20   id!=0 );.  if( 
fd0f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
fd100 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29  =RESERVED_LOCK )
fd110 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20  {.    rc = 1;.  
fd120 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53 54    OSTRACE3("TEST
fd130 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28   WR-LOCK %d %d (
fd140 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65  local)\n", pFile
fd150 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65 6c 73  ->h, rc);.  }els
fd160 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f 63 6b  e{.    rc = Lock
fd170 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52  File(pFile->h, R
fd180 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30 2c  ESERVED_BYTE, 0,
fd190 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   1, 0);.    if( 
fd1a0 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e 6c 6f  rc ){.      Unlo
fd1b0 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
fd1c0 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20   RESERVED_BYTE, 
fd1d0 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  0, 1, 0);.    }.
fd1e0 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a 20 20      rc = !rc;.  
fd1f0 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53 54    OSTRACE3("TEST
fd200 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28   WR-LOCK %d %d (
fd210 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c  remote)\n", pFil
fd220 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 0a 20  e->h, rc);.  }. 
fd230 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a   *pResOut = rc;.
fd240 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fd250 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77  OK;.}../*.** Low
fd260 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c  er the locking l
fd270 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73  evel on file des
fd280 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c 6f  criptor id to lo
fd290 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70  cktype.  locktyp
fd2a0 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  e.** must be eit
fd2b0 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
fd2c0 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
fd2d0 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
fd2e0 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
fd2f0 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
fd300 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
fd310 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75  elow.** the requ
fd320 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
fd330 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
fd340 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  e is a no-op..**
fd350 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f  .** It is not po
fd360 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73 20  ssible for this 
fd370 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c 20  routine to fail 
fd380 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  if the second ar
fd390 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f  gument.** is NO_
fd3a0 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 73 65  LOCK.  If the se
fd3b0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
fd3c0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68 65   SHARED_LOCK the
fd3d0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  n this routine.*
fd3e0 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 53  * might return S
fd3f0 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a  QLITE_IOERR;.*/.
fd400 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 55 6e  static int winUn
fd410 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  lock(sqlite3_fil
fd420 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
fd430 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 79 70 65  ype){.  int type
fd440 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69  ;.  winFile *pFi
fd450 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69  le = (winFile*)i
fd460 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  d;.  int rc = SQ
fd470 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72  LITE_OK;.  asser
fd480 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20  t( pFile!=0 );. 
fd490 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
fd4a0 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
fd4b0 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e  ;.  OSTRACE5("UN
fd4c0 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77 61  LOCK %d to %d wa
fd4d0 73 20 25 64 28 25 64 29 5c 6e 22 2c 20 70 46 69  s %d(%d)\n", pFi
fd4e0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
fd4f0 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65  .          pFile
fd500 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c  ->locktype, pFil
fd510 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  e->sharedLockByt
fd520 65 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46 69  e);.  type = pFi
fd530 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20  le->locktype;.  
fd540 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55 53  if( type>=EXCLUS
fd550 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  IVE_LOCK ){.    
fd560 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  UnlockFile(pFile
fd570 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52 53  ->h, SHARED_FIRS
fd580 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49 5a  T, 0, SHARED_SIZ
fd590 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c  E, 0);.    if( l
fd5a0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
fd5b0 4c 4f 43 4b 20 26 26 20 21 67 65 74 52 65 61 64  LOCK && !getRead
fd5c0 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29 7b 0a 20  Lock(pFile) ){. 
fd5d0 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f       /* This sho
fd5e0 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e  uld never happen
fd5f0 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77  .  We should alw
fd600 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20  ays be able to. 
fd610 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72       ** reacquir
fd620 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20  e the read lock 
fd630 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  */.      rc = SQ
fd640 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43  LITE_IOERR_UNLOC
fd650 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  K;.    }.  }.  i
fd660 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45  f( type>=RESERVE
fd670 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e  D_LOCK ){.    Un
fd680 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e  lockFile(pFile->
fd690 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  h, RESERVED_BYTE
fd6a0 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a  , 0, 1, 0);.  }.
fd6b0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
fd6c0 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65 3e  NO_LOCK && type>
fd6d0 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
fd6e0 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f      unlockReadLo
fd6f0 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 7d 0a 20  ck(pFile);.  }. 
fd700 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49   if( type>=PENDI
fd710 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55  NG_LOCK ){.    U
fd720 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d  nlockFile(pFile-
fd730 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  >h, PENDING_BYTE
fd740 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a  , 0, 1, 0);.  }.
fd750 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
fd760 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70 65  e = (u8)locktype
fd770 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
fd780 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20  ../*.** Control 
fd790 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68 65  and query of the
fd7a0 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c   open file handl
fd7b0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
fd7c0 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c 28   winFileControl(
fd7d0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
fd7e0 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a  , int op, void *
fd7f0 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28  pArg){.  switch(
fd800 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
fd810 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
fd820 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20  KSTATE: {.      
fd830 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28  *(int*)pArg = ((
fd840 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f  winFile*)id)->lo
fd850 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72 65  cktype;.      re
fd860 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
fd870 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53      }.    case S
fd880 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f  QLITE_LAST_ERRNO
fd890 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74 2a  : {.      *(int*
fd8a0 29 70 41 72 67 20 3d 20 28 69 6e 74 29 28 28 77  )pArg = (int)((w
fd8b0 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73  inFile*)id)->las
fd8c0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65  tErrno;.      re
fd8d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
fd8e0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
fd8f0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
fd900 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
fd910 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65   the sector size
fd920 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65   in bytes of the
fd930 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63   underlying bloc
fd940 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20  k device for.** 
fd950 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69  the specified fi
fd960 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d 6f  le. This is almo
fd970 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62 79  st always 512 by
fd980 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65 0a  tes, but may be.
fd990 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73 6f  ** larger for so
fd9a0 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a  me devices..**.*
fd9b0 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61 73  * SQLite code as
fd9c0 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63 74  sumes this funct
fd9d0 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e  ion cannot fail.
fd9e0 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65 73   It also assumes
fd9f0 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20   that.** if two 
fda00 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74 65  files are create
fda10 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66 69  d in the same fi
fda20 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63 74  le-system direct
fda30 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64  ory (i.e..** a d
fda40 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73 20  atabase and its 
fda50 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74 68  journal file) th
fda60 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73 69  at the sector si
fda70 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a  ze will be the.*
fda80 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e  * same for both.
fda90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
fdaa0 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73 71 6c  inSectorSize(sql
fdab0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a  ite3_file *id){.
fdac0 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20    assert( id!=0 
fdad0 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74  );.  return (int
fdae0 29 28 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29  )(((winFile*)id)
fdaf0 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a 7d  ->sectorSize);.}
fdb00 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
fdb10 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69 63   vector of devic
fdb20 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
fdb30 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
fdb40 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63   winDeviceCharac
fdb50 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74 65  teristics(sqlite
fdb60 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 55  3_file *id){.  U
fdb70 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
fdb80 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  id);.  return 0;
fdb90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76  .}../*.** This v
fdba0 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61 6c  ector defines al
fdbb0 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74 68  l the methods th
fdbc0 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20 6f  at can operate o
fdbd0 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  n an.** sqlite3_
fdbe0 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e 0a  file for win32..
fdbf0 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
fdc00 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
fdc10 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20 3d  ds winIoMethod =
fdc20 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20   {.  1,         
fdc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
fdc40 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20  * iVersion */.  
fdc50 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e 52  winClose,.  winR
fdc60 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 65 2c  ead,.  winWrite,
fdc70 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65 2c 0a  .  winTruncate,.
fdc80 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69 6e    winSync,.  win
fdc90 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e 4c  FileSize,.  winL
fdca0 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63 6b  ock,.  winUnlock
fdcb0 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 73 65  ,.  winCheckRese
fdcc0 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e 46  rvedLock,.  winF
fdcd0 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77 69  ileControl,.  wi
fdce0 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20 77  nSectorSize,.  w
fdcf0 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74 65  inDeviceCharacte
fdd00 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a 2a  ristics.};../***
fdd10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdd20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdd30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdd40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fdd50 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72 65  ********.** Here
fdd60 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d 65   ends the I/O me
fdd70 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d 20  thods that form 
fdd80 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  the sqlite3_io_m
fdd90 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a  ethods object..*
fdda0 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62 6c  *.** The next bl
fddb0 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70 6c  ock of code impl
fddc0 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20 6d  ements the VFS m
fddd0 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a  ethods..********
fdde0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fddf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fde00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fde10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fde20 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  ****/../*.** Con
fde30 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 69 6c  vert a UTF-8 fil
fde40 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 74 65  ename into whate
fde50 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 6e 64  ver form the und
fde60 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72 61  erlying.** opera
fde70 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 6e 74  ting system want
fde80 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e 20  s filenames in. 
fde90 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
fdea0 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20  he result.** is 
fdeb0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61  obtained from ma
fdec0 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65  lloc and must be
fded0 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61   freed by the ca
fdee0 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f  lling.** functio
fdef0 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
fdf00 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 46 69  d *convertUtf8Fi
fdf10 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68 61  lename(const cha
fdf20 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
fdf30 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65   void *zConverte
fdf40 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 73 4e  d = 0;.  if( isN
fdf50 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e 76  T() ){.    zConv
fdf60 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f 55 6e  erted = utf8ToUn
fdf70 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29  icode(zFilename)
fdf80 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31  ;./* isNT() is 1
fdf90 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49   if SQLITE_OS_WI
fdfa0 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20  NCE==1, so this 
fdfb0 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78  else is never ex
fdfc0 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20  ecuted. .*/.#if 
fdfd0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
fdfe0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
fdff0 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74 66  zConverted = utf
fe000 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61 6d  8ToMbcs(zFilenam
fe010 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20  e);.#endif.  }. 
fe020 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20   /* caller will 
fe030 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d 65  handle out of me
fe040 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75 72 6e  mory */.  return
fe050 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a 0a   zConverted;.}..
fe060 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74  /*.** Create a t
fe070 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61  emporary file na
fe080 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75  me in zBuf.  zBu
fe090 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e  f must be big en
fe0a0 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20  ough to.** hold 
fe0b0 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  at pVfs->mxPathn
fe0c0 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a  ame characters..
fe0d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
fe0e0 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42  tTempname(int nB
fe0f0 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b  uf, char *zBuf){
fe100 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 20 7a  .  static char z
fe110 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61  Chars[] =.    "a
fe120 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71  bcdefghijklmnopq
fe130 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22  rstuvwxyz".    "
fe140 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50  ABCDEFGHIJKLMNOP
fe150 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20  QRSTUVWXYZ".    
fe160 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20  "0123456789";.  
fe170 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 20 63  size_t i, j;.  c
fe180 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d 41  har zTempPath[MA
fe190 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 66 28  X_PATH+1];.  if(
fe1a0 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69   sqlite3_temp_di
fe1b0 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 73  rectory ){.    s
fe1c0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
fe1d0 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65  MAX_PATH-30, zTe
fe1e0 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 73 71  mpPath, "%s", sq
fe1f0 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
fe200 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65 20 69  tory);.  }else i
fe210 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
fe220 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a 20   char *zMulti;. 
fe230 20 20 20 57 43 48 41 52 20 7a 57 69 64 65 50 61     WCHAR zWidePa
fe240 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20  th[MAX_PATH];.  
fe250 20 20 47 65 74 54 65 6d 70 50 61 74 68 57 28 4d    GetTempPathW(M
fe260 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69 64  AX_PATH-30, zWid
fe270 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75 6c  ePath);.    zMul
fe280 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74  ti = unicodeToUt
fe290 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a 20  f8(zWidePath);. 
fe2a0 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29 7b     if( zMulti ){
fe2b0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
fe2c0 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48  nprintf(MAX_PATH
fe2d0 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c 20  -30, zTempPath, 
fe2e0 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a 20  "%s", zMulti);. 
fe2f0 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c 74 69       free(zMulti
fe300 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
fe310 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
fe320 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 2f  E_NOMEM;.    }./
fe330 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69 66  * isNT() is 1 if
fe340 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
fe350 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73  ==1, so this els
fe360 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63 75  e is never execu
fe370 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74  ted. .** Since t
fe380 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e  he ASCII version
fe390 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77   of these Window
fe3a0 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69  s API do not exi
fe3b0 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a  st for WINCE,.**
fe3c0 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20   it's important 
fe3d0 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65  to not reference
fe3e0 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20   them for WINCE 
fe3f0 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53  builds..*/.#if S
fe400 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
fe410 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  0.  }else{.    c
fe420 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20 20  har *zUtf8;.    
fe430 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 5b 4d  char zMbcsPath[M
fe440 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47 65  AX_PATH];.    Ge
fe450 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 5f 50  tTempPathA(MAX_P
fe460 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61 74  ATH-30, zMbcsPat
fe470 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d 20  h);.    zUtf8 = 
fe480 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62  sqlite3_win32_mb
fe490 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 63 73  cs_to_utf8(zMbcs
fe4a0 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a  Path);.    if( z
fe4b0 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 73 71  Utf8 ){.      sq
fe4c0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d  lite3_snprintf(M
fe4d0 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65 6d  AX_PATH-30, zTem
fe4e0 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 55 74  pPath, "%s", zUt
fe4f0 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 65 28  f8);.      free(
fe500 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c 73  zUtf8);.    }els
fe510 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
fe520 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
fe530 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
fe540 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33 53 74   for(i=sqlite3St
fe550 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 74 68  rlen30(zTempPath
fe560 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d 70 50  ); i>0 && zTempP
fe570 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b 20  ath[i-1]=='\\'; 
fe580 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50 61  i--){}.  zTempPa
fe590 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71 6c  th[i] = 0;.  sql
fe5a0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42  ite3_snprintf(nB
fe5b0 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20  uf-30, zBuf,.   
fe5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe5d0 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d  "%s\\"SQLITE_TEM
fe5e0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a  P_FILE_PREFIX, z
fe5f0 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20 3d  TempPath);.  j =
fe600 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
fe610 28 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69 74 65  (zBuf);.  sqlite
fe620 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32 30 2c  3_randomness(20,
fe630 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 66 6f   &zBuf[j]);.  fo
fe640 72 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69 2b 2b  r(i=0; i<20; i++
fe650 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 66  , j++){.    zBuf
fe660 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61  [j] = (char)zCha
fe670 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63  rs[ ((unsigned c
fe680 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69  har)zBuf[j])%(si
fe690 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20  zeof(zChars)-1) 
fe6a0 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d  ];.  }.  zBuf[j]
fe6b0 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32   = 0;.  OSTRACE2
fe6c0 28 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a  ("TEMP FILENAME:
fe6d0 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20   %s\n", zBuf);. 
fe6e0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
fe6f0 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  K; .}../*.** The
fe700 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
fe710 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67   getLastErrorMsg
fe720 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74  .** is zero if t
fe730 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
fe740 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 66   fits in the buf
fe750 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f  fer, or non-zero
fe760 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69  .** otherwise (i
fe770 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 61  f the message wa
fe780 73 20 74 72 75 6e 63 61 74 65 64 29 2e 0a 2a 2f  s truncated)..*/
fe790 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c  .static int getL
fe7a0 61 73 74 45 72 72 6f 72 4d 73 67 28 69 6e 74 20  astErrorMsg(int 
fe7b0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
fe7c0 29 7b 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65  ){.  /* FormatMe
fe7d0 73 73 61 67 65 20 72 65 74 75 72 6e 73 20 30 20  ssage returns 0 
fe7e0 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 74 68  on failure.  Oth
fe7f0 65 72 77 69 73 65 20 69 74 0a 20 20 2a 2a 20 72  erwise it.  ** r
fe800 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
fe810 72 20 6f 66 20 54 43 48 41 52 73 20 77 72 69 74  r of TCHARs writ
fe820 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70 75  ten to the outpu
fe830 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20 65  t.  ** buffer, e
fe840 78 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 72  xcluding the ter
fe850 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68  minating null ch
fe860 61 72 2e 0a 20 20 2a 2f 0a 20 20 44 57 4f 52 44  ar..  */.  DWORD
fe870 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74   error = GetLast
fe880 45 72 72 6f 72 28 29 3b 0a 20 20 44 57 4f 52 44  Error();.  DWORD
fe890 20 64 77 4c 65 6e 20 3d 20 30 3b 0a 20 20 63 68   dwLen = 0;.  ch
fe8a0 61 72 20 2a 7a 4f 75 74 20 3d 20 30 3b 0a 0a 20  ar *zOut = 0;.. 
fe8b0 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20   if( isNT() ){. 
fe8c0 20 20 20 57 43 48 41 52 20 2a 7a 54 65 6d 70 57     WCHAR *zTempW
fe8d0 69 64 65 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ide = NULL;.    
fe8e0 64 77 4c 65 6e 20 3d 20 46 6f 72 6d 61 74 4d 65  dwLen = FormatMe
fe8f0 73 73 61 67 65 57 28 46 4f 52 4d 41 54 5f 4d 45  ssageW(FORMAT_ME
fe900 53 53 41 47 45 5f 41 4c 4c 4f 43 41 54 45 5f 42  SSAGE_ALLOCATE_B
fe910 55 46 46 45 52 20 7c 20 46 4f 52 4d 41 54 5f 4d  UFFER | FORMAT_M
fe920 45 53 53 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54  ESSAGE_FROM_SYST
fe930 45 4d 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53  EM | FORMAT_MESS
fe940 41 47 45 5f 49 47 4e 4f 52 45 5f 49 4e 53 45 52  AGE_IGNORE_INSER
fe950 54 53 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  TS,.            
fe960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e                 N
fe970 55 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ULL,.           
fe980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe990 65 72 72 6f 72 2c 0a 20 20 20 20 20 20 20 20 20  error,.         
fe9a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe9b0 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20    0,.           
fe9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe9d0 28 4c 50 57 53 54 52 29 20 26 7a 54 65 6d 70 57  (LPWSTR) &zTempW
fe9e0 69 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ide,.           
fe9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fea00 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
fea10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29                0)
fea20 3b 0a 20 20 20 20 69 66 28 20 64 77 4c 65 6e 20  ;.    if( dwLen 
fea30 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  > 0 ){.      /* 
fea40 61 6c 6c 6f 63 61 74 65 20 61 20 62 75 66 66 65  allocate a buffe
fea50 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f  r and convert to
fea60 20 55 54 46 38 20 2a 2f 0a 20 20 20 20 20 20 7a   UTF8 */.      z
fea70 4f 75 74 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55  Out = unicodeToU
fea80 74 66 38 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a  tf8(zTempWide);.
fea90 20 20 20 20 20 20 2f 2a 20 66 72 65 65 20 74 68        /* free th
feaa0 65 20 73 79 73 74 65 6d 20 62 75 66 66 65 72 20  e system buffer 
feab0 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 46 6f 72  allocated by For
feac0 6d 61 74 4d 65 73 73 61 67 65 20 2a 2f 0a 20 20  matMessage */.  
fead0 20 20 20 20 4c 6f 63 61 6c 46 72 65 65 28 7a 54      LocalFree(zT
feae0 65 6d 70 57 69 64 65 29 3b 0a 20 20 20 20 7d 0a  empWide);.    }.
feaf0 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69  /* isNT() is 1 i
feb00 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
feb10 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c  E==1, so this el
feb20 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63  se is never exec
feb30 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20  uted. .** Since 
feb40 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f  the ASCII versio
feb50 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f  n of these Windo
feb60 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78  ws API do not ex
feb70 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a  ist for WINCE,.*
feb80 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74  * it's important
feb90 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63   to not referenc
feba0 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45  e them for WINCE
febb0 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20   builds..*/.#if 
febc0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
febd0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
febe0 63 68 61 72 20 2a 7a 54 65 6d 70 20 3d 20 4e 55  char *zTemp = NU
febf0 4c 4c 3b 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20  LL;.    dwLen = 
fec00 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 41 28 46  FormatMessageA(F
fec10 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 41 4c  ORMAT_MESSAGE_AL
fec20 4c 4f 43 41 54 45 5f 42 55 46 46 45 52 20 7c 20  LOCATE_BUFFER | 
fec30 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46  FORMAT_MESSAGE_F
fec40 52 4f 4d 5f 53 59 53 54 45 4d 20 7c 20 46 4f 52  ROM_SYSTEM | FOR
fec50 4d 41 54 5f 4d 45 53 53 41 47 45 5f 49 47 4e 4f  MAT_MESSAGE_IGNO
fec60 52 45 5f 49 4e 53 45 52 54 53 2c 0a 20 20 20 20  RE_INSERTS,.    
fec70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fec80 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20         NULL,.   
fec90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
feca0 20 20 20 20 20 20 20 20 65 72 72 6f 72 2c 0a 20          error,. 
fecb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fecc0 20 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20            0,.   
fecd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fece0 20 20 20 20 20 20 20 20 28 4c 50 53 54 52 29 20          (LPSTR) 
fecf0 26 7a 54 65 6d 70 2c 0a 20 20 20 20 20 20 20 20  &zTemp,.        
fed00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fed10 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20     0,.          
fed20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fed30 20 30 29 3b 0a 20 20 20 20 69 66 28 20 64 77 4c   0);.    if( dwL
fed40 65 6e 20 3e 20 30 20 29 7b 0a 20 20 20 20 20 20  en > 0 ){.      
fed50 2f 2a 20 61 6c 6c 6f 63 61 74 65 20 61 20 62 75  /* allocate a bu
fed60 66 66 65 72 20 61 6e 64 20 63 6f 6e 76 65 72 74  ffer and convert
fed70 20 74 6f 20 55 54 46 38 20 2a 2f 0a 20 20 20 20   to UTF8 */.    
fed80 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33    zOut = sqlite3
fed90 5f 77 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75  _win32_mbcs_to_u
feda0 74 66 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20  tf8(zTemp);.    
fedb0 20 20 2f 2a 20 66 72 65 65 20 74 68 65 20 73 79    /* free the sy
fedc0 73 74 65 6d 20 62 75 66 66 65 72 20 61 6c 6c 6f  stem buffer allo
fedd0 63 61 74 65 64 20 62 79 20 46 6f 72 6d 61 74 4d  cated by FormatM
fede0 65 73 73 61 67 65 20 2a 2f 0a 20 20 20 20 20 20  essage */.      
fedf0 4c 6f 63 61 6c 46 72 65 65 28 7a 54 65 6d 70 29  LocalFree(zTemp)
fee00 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
fee10 20 7d 0a 20 20 69 66 28 20 30 20 3d 3d 20 64 77   }.  if( 0 == dw
fee20 4c 65 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Len ){.    sqlit
fee30 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66  e3_snprintf(nBuf
fee40 2c 20 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f 72  , zBuf, "OsError
fee50 20 30 78 25 78 20 28 25 75 29 22 2c 20 65 72 72   0x%x (%u)", err
fee60 6f 72 2c 20 65 72 72 6f 72 29 3b 0a 20 20 7d 65  or, error);.  }e
fee70 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 63 6f 70 79  lse{.    /* copy
fee80 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 6e 42   a maximum of nB
fee90 75 66 20 63 68 61 72 73 20 74 6f 20 6f 75 74 70  uf chars to outp
feea0 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 20  ut buffer */.   
feeb0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
feec0 66 28 6e 42 75 66 2c 20 7a 42 75 66 2c 20 22 25  f(nBuf, zBuf, "%
feed0 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2f  s", zOut);.    /
feee0 2a 20 66 72 65 65 20 74 68 65 20 55 54 46 38 20  * free the UTF8 
feef0 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 66 72  buffer */.    fr
fef00 65 65 28 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20  ee(zOut);.  }.  
fef10 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
fef20 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a  ** Open a file..
fef30 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
fef40 6e 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  nOpen(.  sqlite3
fef50 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
fef60 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a     /* Not used *
fef70 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
fef80 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a  zName,        /*
fef90 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
fefa0 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73  e (UTF-8) */.  s
fefb0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
fefc0 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
fefd0 65 20 74 68 65 20 53 51 4c 69 74 65 20 66 69 6c  e the SQLite fil
fefe0 65 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f  e handle here */
feff0 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20  .  int flags,   
ff000 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ff010 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20  Open mode flags 
ff020 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c  */.  int *pOutFl
ff030 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f  ags            /
ff040 2a 20 53 74 61 74 75 73 20 72 65 74 75 72 6e 20  * Status return 
ff050 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41  flags */.){.  HA
ff060 4e 44 4c 45 20 68 3b 0a 20 20 44 57 4f 52 44 20  NDLE h;.  DWORD 
ff070 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 3b  dwDesiredAccess;
ff080 0a 20 20 44 57 4f 52 44 20 64 77 53 68 61 72 65  .  DWORD dwShare
ff090 4d 6f 64 65 3b 0a 20 20 44 57 4f 52 44 20 64 77  Mode;.  DWORD dw
ff0a0 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74  CreationDisposit
ff0b0 69 6f 6e 3b 0a 20 20 44 57 4f 52 44 20 64 77 46  ion;.  DWORD dwF
ff0c0 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
ff0d0 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54  s = 0;.#if SQLIT
ff0e0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74  E_OS_WINCE.  int
ff0f0 20 69 73 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e   isTemp = 0;.#en
ff100 64 69 66 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  dif.  winFile *p
ff110 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
ff120 29 69 64 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f  )id;.  void *zCo
ff130 6e 76 65 72 74 65 64 3b 20 20 20 20 20 20 20 20  nverted;        
ff140 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
ff150 6e 61 6d 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64  name in OS encod
ff160 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ing */.  const c
ff170 68 61 72 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d  har *zUtf8Name =
ff180 20 7a 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 46 69   zName;    /* Fi
ff190 6c 65 6e 61 6d 65 20 69 6e 20 55 54 46 2d 38 20  lename in UTF-8 
ff1a0 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 68  encoding */.  ch
ff1b0 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f  ar zTmpname[MAX_
ff1c0 50 41 54 48 2b 31 5d 3b 20 20 20 20 20 20 20 20  PATH+1];        
ff1d0 2f 2a 20 42 75 66 66 65 72 20 75 73 65 64 20 74  /* Buffer used t
ff1e0 6f 20 63 72 65 61 74 65 20 74 65 6d 70 20 66 69  o create temp fi
ff1f0 6c 65 6e 61 6d 65 20 2a 2f 0a 0a 20 20 61 73 73  lename */..  ass
ff200 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
ff210 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
ff220 28 70 56 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 66  (pVfs);..  /* If
ff230 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
ff240 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
ff250 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67  ction is NULL, g
ff260 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20  enerate a .  ** 
ff270 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e  temporary file n
ff280 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f  ame to use .  */
ff290 0a 20 20 69 66 28 20 21 7a 55 74 66 38 4e 61 6d  .  if( !zUtf8Nam
ff2a0 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  e ){.    int rc 
ff2b0 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41  = getTempname(MA
ff2c0 58 5f 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61  X_PATH+1, zTmpna
ff2d0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  me);.    if( rc!
ff2e0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
ff2f0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
ff300 20 20 20 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61     }.    zUtf8Na
ff310 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20  me = zTmpname;. 
ff320 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74   }..  /* Convert
ff330 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f   the filename to
ff340 20 74 68 65 20 73 79 73 74 65 6d 20 65 6e 63 6f   the system enco
ff350 64 69 6e 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76  ding. */.  zConv
ff360 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55  erted = convertU
ff370 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66  tf8Filename(zUtf
ff380 38 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43  8Name);.  if( zC
ff390 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20  onverted==0 ){. 
ff3a0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
ff3b0 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69  _NOMEM;.  }..  i
ff3c0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
ff3d0 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
ff3e0 20 29 7b 0a 20 20 20 20 64 77 44 65 73 69 72 65   ){.    dwDesire
ff3f0 64 41 63 63 65 73 73 20 3d 20 47 45 4e 45 52 49  dAccess = GENERI
ff400 43 5f 52 45 41 44 20 7c 20 47 45 4e 45 52 49 43  C_READ | GENERIC
ff410 5f 57 52 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b  _WRITE;.  }else{
ff420 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63  .    dwDesiredAc
ff430 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52  cess = GENERIC_R
ff440 45 41 44 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 51  EAD;.  }.  /* SQ
ff450 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
ff460 49 56 45 20 69 73 20 75 73 65 64 20 74 6f 20 6d  IVE is used to m
ff470 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 20  ake sure that a 
ff480 6e 65 77 20 66 69 6c 65 20 69 73 20 0a 20 20 2a  new file is .  *
ff490 2a 20 63 72 65 61 74 65 64 2e 20 53 51 4c 69 74  * created. SQLit
ff4a0 65 20 64 6f 65 73 6e 27 74 20 75 73 65 20 69 74  e doesn't use it
ff4b0 20 74 6f 20 69 6e 64 69 63 61 74 65 20 22 65 78   to indicate "ex
ff4c0 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 22 20  clusive access" 
ff4d0 0a 20 20 2a 2a 20 61 73 20 69 74 20 69 73 20 75  .  ** as it is u
ff4e0 73 75 61 6c 6c 79 20 75 6e 64 65 72 73 74 6f 6f  sually understoo
ff4f0 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  d..  */.  assert
ff500 28 21 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  (!(flags & SQLIT
ff510 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
ff520 29 20 7c 7c 20 28 66 6c 61 67 73 20 26 20 53 51  ) || (flags & SQ
ff530 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
ff540 29 29 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20  ));.  if( flags 
ff550 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58  & SQLITE_OPEN_EX
ff560 43 4c 55 53 49 56 45 20 29 7b 0a 20 20 20 20 2f  CLUSIVE ){.    /
ff570 2a 20 43 72 65 61 74 65 73 20 61 20 6e 65 77 20  * Creates a new 
ff580 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74  file, only if it
ff590 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64   does not alread
ff5a0 79 20 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20  y exist. */.    
ff5b0 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20 65  /* If the file e
ff5c0 78 69 73 74 73 2c 20 69 74 20 66 61 69 6c 73 2e  xists, it fails.
ff5d0 20 2a 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69   */.    dwCreati
ff5e0 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20  onDisposition = 
ff5f0 43 52 45 41 54 45 5f 4e 45 57 3b 0a 20 20 7d 65  CREATE_NEW;.  }e
ff600 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
ff610 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
ff620 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 70 65  TE ){.    /* Ope
ff630 6e 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 2c  n existing file,
ff640 20 6f 72 20 63 72 65 61 74 65 20 69 66 20 69 74   or create if it
ff650 20 64 6f 65 73 6e 27 74 20 65 78 69 73 74 20 2a   doesn't exist *
ff660 2f 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e  /.    dwCreation
ff670 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50  Disposition = OP
ff680 45 4e 5f 41 4c 57 41 59 53 3b 0a 20 20 7d 65 6c  EN_ALWAYS;.  }el
ff690 73 65 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 73  se{.    /* Opens
ff6a0 20 61 20 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66   a file, only if
ff6b0 20 69 74 20 65 78 69 73 74 73 2e 20 2a 2f 0a 20   it exists. */. 
ff6c0 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73     dwCreationDis
ff6d0 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f  position = OPEN_
ff6e0 45 58 49 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20  EXISTING;.  }.  
ff6f0 64 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49  dwShareMode = FI
ff700 4c 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20  LE_SHARE_READ | 
ff710 46 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45  FILE_SHARE_WRITE
ff720 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ;.  if( flags & 
ff730 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
ff740 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23 69 66  TEONCLOSE ){.#if
ff750 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ff760 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41  .    dwFlagsAndA
ff770 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45  ttributes = FILE
ff780 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 45  _ATTRIBUTE_HIDDE
ff790 4e 3b 0a 20 20 20 20 69 73 54 65 6d 70 20 3d 20  N;.    isTemp = 
ff7a0 31 3b 0a 23 65 6c 73 65 0a 20 20 20 20 64 77 46  1;.#else.    dwF
ff7b0 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
ff7c0 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42 55  s = FILE_ATTRIBU
ff7d0 54 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20 20 20  TE_TEMPORARY.   
ff7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff7f0 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 46 49              | FI
ff800 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44  LE_ATTRIBUTE_HID
ff810 44 45 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20  DEN.            
ff820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff830 20 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 44     | FILE_FLAG_D
ff840 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a  ELETE_ON_CLOSE;.
ff850 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a  #endif.  }else{.
ff860 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74      dwFlagsAndAt
ff870 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f  tributes = FILE_
ff880 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41 4c  ATTRIBUTE_NORMAL
ff890 3b 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 70 6f 72  ;.  }.  /* Repor
ff8a0 74 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65  ts from the inte
ff8b0 72 6e 65 74 20 61 72 65 20 74 68 61 74 20 70 65  rnet are that pe
ff8c0 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6c 77  rformance is alw
ff8d0 61 79 73 0a 20 20 2a 2a 20 62 65 74 74 65 72 20  ays.  ** better 
ff8e0 69 66 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e  if FILE_FLAG_RAN
ff8f0 44 4f 4d 5f 41 43 43 45 53 53 20 69 73 20 75 73  DOM_ACCESS is us
ff900 65 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36 39  ed.  Ticket #269
ff910 39 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  9. */.#if SQLITE
ff920 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 64 77 46 6c  _OS_WINCE.  dwFl
ff930 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73  agsAndAttributes
ff940 20 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41   |= FILE_FLAG_RA
ff950 4e 44 4f 4d 5f 41 43 43 45 53 53 3b 0a 23 65 6e  NDOM_ACCESS;.#en
ff960 64 69 66 0a 20 20 69 66 28 20 69 73 4e 54 28 29  dif.  if( isNT()
ff970 20 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65 61   ){.    h = Crea
ff980 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a 29  teFileW((WCHAR*)
ff990 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20 20  zConverted,.    
ff9a0 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65     dwDesiredAcce
ff9b0 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68 61  ss,.       dwSha
ff9c0 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e  reMode,.       N
ff9d0 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43 72  ULL,.       dwCr
ff9e0 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f  eationDispositio
ff9f0 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61 67  n,.       dwFlag
ffa00 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c 0a  sAndAttributes,.
ffa10 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20         NULL.    
ffa20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
ffa30 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
ffa40 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
ffa50 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
ffa60 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e  xecuted. .** Sin
ffa70 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72  ce the ASCII ver
ffa80 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69  sion of these Wi
ffa90 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74  ndows API do not
ffaa0 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45   exist for WINCE
ffab0 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74  ,.** it's import
ffac0 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72  ant to not refer
ffad0 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49  ence them for WI
ffae0 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23  NCE builds..*/.#
ffaf0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ffb00 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
ffb10 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69 6c     h = CreateFil
ffb20 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65  eA((char*)zConve
ffb30 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77 44  rted,.       dwD
ffb40 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20 20  esiredAccess,.  
ffb50 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64 65       dwShareMode
ffb60 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20  ,.       NULL,. 
ffb70 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e        dwCreation
ffb80 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20  Disposition,.   
ffb90 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74      dwFlagsAndAt
ffba0 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20 20  tributes,.      
ffbb0 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 6e   NULL.    );.#en
ffbc0 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 68 3d  dif.  }.  if( h=
ffbd0 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f  =INVALID_HANDLE_
ffbe0 56 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 72 65  VALUE ){.    fre
ffbf0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
ffc00 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53     if( flags & S
ffc10 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
ffc20 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72 65  RITE ){.      re
ffc30 74 75 72 6e 20 77 69 6e 4f 70 65 6e 28 70 56 66  turn winOpen(pVf
ffc40 73 2c 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20  s, zName, id, . 
ffc50 20 20 20 20 20 20 20 20 20 20 20 20 28 28 66 6c              ((fl
ffc60 61 67 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  ags|SQLITE_OPEN_
ffc70 52 45 41 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 54  READONLY)&~SQLIT
ffc80 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
ffc90 29 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20  ), pOutFlags);. 
ffca0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
ffcb0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41  return SQLITE_CA
ffcc0 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20  NTOPEN;.    }.  
ffcd0 7d 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67  }.  if( pOutFlag
ffce0 73 20 29 7b 0a 20 20 20 20 69 66 28 20 66 6c 61  s ){.    if( fla
ffcf0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
ffd00 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20  _READWRITE ){.  
ffd10 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d      *pOutFlags =
ffd20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
ffd30 44 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73  DWRITE;.    }els
ffd40 65 7b 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c  e{.      *pOutFl
ffd50 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45  ags = SQLITE_OPE
ffd60 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20  N_READONLY;.    
ffd70 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70  }.  }.  memset(p
ffd80 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  File, 0, sizeof(
ffd90 2a 70 46 69 6c 65 29 29 3b 0a 20 20 70 46 69 6c  *pFile));.  pFil
ffda0 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 77 69  e->pMethod = &wi
ffdb0 6e 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69  nIoMethod;.  pFi
ffdc0 6c 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 46 69  le->h = h;.  pFi
ffdd0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
ffde0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 70 46 69 6c  NO_ERROR;.  pFil
ffdf0 65 2d 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20  e->sectorSize = 
ffe00 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 56  getSectorSize(pV
ffe10 66 73 2c 20 7a 55 74 66 38 4e 61 6d 65 29 3b 0a  fs, zUtf8Name);.
ffe20 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
ffe30 4e 43 45 0a 20 20 69 66 28 20 28 66 6c 61 67 73  NCE.  if( (flags
ffe40 20 26 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   & (SQLITE_OPEN_
ffe50 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45  READWRITE|SQLITE
ffe60 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20  _OPEN_MAIN_DB)) 
ffe70 3d 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ==.             
ffe80 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52    (SQLITE_OPEN_R
ffe90 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f  EADWRITE|SQLITE_
ffea0 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20  OPEN_MAIN_DB).  
ffeb0 20 20 20 20 20 26 26 20 21 77 69 6e 63 65 43 72       && !winceCr
ffec0 65 61 74 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20  eateLock(zName, 
ffed0 70 46 69 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20  pFile).  ){.    
ffee0 43 6c 6f 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a  CloseHandle(h);.
ffef0 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72      free(zConver
fff00 74 65 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ted);.    return
fff10 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
fff20 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 54 65  ;.  }.  if( isTe
fff30 6d 70 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  mp ){.    pFile-
fff40 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20  >zDeleteOnClose 
fff50 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20  = zConverted;.  
fff60 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b  }else.#endif.  {
fff70 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76 65  .    free(zConve
fff80 72 74 65 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65  rted);.  }.  Ope
fff90 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20  nCounter(+1);.  
fffa0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
fffb0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
fffc0 65 20 74 68 65 20 6e 61 6d 65 64 20 66 69 6c 65  e the named file
fffd0 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
fffe0 74 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e  t windows does n
ffff0 6f 74 20 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20  ot allow a file 
10000 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 69 66  to be deleted if
10001 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70   some other.** p
10002 72 6f 63 65 73 73 20 68 61 73 20 69 74 20 6f 70  rocess has it op
10003 65 6e 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20 61  en.  Sometimes a
10004 20 76 69 72 75 73 20 73 63 61 6e 6e 65 72 20 6f   virus scanner o
10005 72 20 69 6e 64 65 78 69 6e 67 20 70 72 6f 67 72  r indexing progr
10006 61 6d 0a 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20  am.** will open 
10007 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73  a journal file s
10008 68 6f 72 74 6c 79 20 61 66 74 65 72 20 69 74 20  hortly after it 
10009 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 6f 72  is created in or
1000a 64 65 72 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61  der to do.** wha
1000b 74 65 76 65 72 20 69 74 20 64 6f 65 73 2e 20 20  tever it does.  
1000c 57 68 69 6c 65 20 74 68 69 73 20 6f 74 68 65 72  While this other
1000d 20 70 72 6f 63 65 73 73 20 69 73 20 68 6f 6c 64   process is hold
1000e 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20  ing the.** file 
1000f 6f 70 65 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65  open, we will be
10010 20 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74   unable to delet
10011 65 20 69 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61  e it.  To work a
10012 72 6f 75 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72  round this.** pr
10013 6f 62 6c 65 6d 2c 20 77 65 20 64 65 6c 61 79 20  oblem, we delay 
10014 31 30 30 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73  100 milliseconds
10015 20 61 6e 64 20 74 72 79 20 74 6f 20 64 65 6c 65   and try to dele
10016 74 65 20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a  te again.  Up.**
10017 20 74 6f 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f   to MX_DELETION_
10018 41 54 54 45 4d 50 54 73 20 64 65 6c 65 74 69 6f  ATTEMPTs deletio
10019 6e 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 72  n attempts are r
1001a 75 6e 20 62 65 66 6f 72 65 20 67 69 76 69 6e 67  un before giving
1001b 0a 2a 2a 20 75 70 20 61 6e 64 20 72 65 74 75 72  .** up and retur
1001c 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  ning an error..*
1001d 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c  /.#define MX_DEL
1001e 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35  ETION_ATTEMPTS 5
1001f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44  .static int winD
10020 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33  elete(.  sqlite3
10021 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
10022 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64       /* Not used
10023 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63   on win32 */.  c
10024 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
10025 6e 61 6d 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61  name,      /* Na
10026 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65  me of file to de
10027 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79  lete */.  int sy
10028 6e 63 44 69 72 20 20 20 20 20 20 20 20 20 20 20  ncDir           
10029 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65        /* Not use
1002a 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b  d on win32 */.){
1002b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a  .  int cnt = 0;.
1002c 20 20 44 57 4f 52 44 20 72 63 3b 0a 20 20 44 57    DWORD rc;.  DW
1002d 4f 52 44 20 65 72 72 6f 72 20 3d 20 30 3b 0a 20  ORD error = 0;. 
1002e 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65   void *zConverte
1002f 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46  d = convertUtf8F
10030 69 6c 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d  ilename(zFilenam
10031 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  e);.  UNUSED_PAR
10032 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
10033 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
10034 28 73 79 6e 63 44 69 72 29 3b 0a 20 20 69 66 28  (syncDir);.  if(
10035 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29   zConverted==0 )
10036 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
10037 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
10038 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
10039 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49  (return SQLITE_I
1003a 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20  OERR_DELETE);.  
1003b 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
1003c 20 20 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65    do{.      Dele
1003d 74 65 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74  teFileW(zConvert
1003e 65 64 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  ed);.    }while(
1003f 20 20 20 28 20 20 20 28 28 72 63 20 3d 20 47 65     (   ((rc = Ge
10040 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57  tFileAttributesW
10041 28 7a 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d  (zConverted)) !=
10042 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54   INVALID_FILE_AT
10043 54 52 49 42 55 54 45 53 29 0a 20 20 20 20 20 20  TRIBUTES).      
10044 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72           || ((er
10045 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72  ror = GetLastErr
10046 6f 72 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41  or()) == ERROR_A
10047 43 43 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20  CCESS_DENIED)). 
10048 20 20 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b            && (++
10049 63 6e 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f  cnt < MX_DELETIO
1004a 4e 5f 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20  N_ATTEMPTS).    
1004b 20 20 20 20 20 20 20 26 26 20 28 53 6c 65 65 70         && (Sleep
1004c 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20  (100), 1) );./* 
1004d 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53  isNT() is 1 if S
1004e 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
1004f 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20  1, so this else 
10050 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65  is never execute
10051 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65  d. .** Since the
10052 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f   ASCII version o
10053 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20  f these Windows 
10054 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74  API do not exist
10055 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69   for WINCE,.** i
10056 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f  t's important to
10057 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74   not reference t
10058 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75  hem for WINCE bu
10059 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ilds..*/.#if SQL
1005a 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
1005b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b    }else{.    do{
1005c 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c  .      DeleteFil
1005d 65 41 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  eA(zConverted);.
1005e 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20      }while(   ( 
1005f 20 20 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65    ((rc = GetFile
10060 41 74 74 72 69 62 75 74 65 73 41 28 7a 43 6f 6e  AttributesA(zCon
10061 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41  verted)) != INVA
10062 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55  LID_FILE_ATTRIBU
10063 54 45 53 29 0a 20 20 20 20 20 20 20 20 20 20 20  TES).           
10064 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d      || ((error =
10065 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29   GetLastError())
10066 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53   == ERROR_ACCESS
10067 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20  _DENIED)).      
10068 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c       && (++cnt <
10069 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54   MX_DELETION_ATT
1006a 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20 20 20  EMPTS).         
1006b 20 20 26 26 20 28 53 6c 65 65 70 28 31 30 30 29    && (Sleep(100)
1006c 2c 20 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20  , 1) );.#endif. 
1006d 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65   }.  free(zConve
1006e 72 74 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 45  rted);.  OSTRACE
1006f 32 28 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22  2("DELETE \"%s\"
10070 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b  \n", zFilename);
10071 0a 20 20 72 65 74 75 72 6e 20 28 20 20 20 28 72  .  return (   (r
10072 63 20 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c  c == INVALID_FIL
10073 45 5f 41 54 54 52 49 42 55 54 45 53 29 20 0a 20  E_ATTRIBUTES) . 
10074 20 20 20 20 20 20 20 20 20 26 26 20 28 65 72 72           && (err
10075 6f 72 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45  or == ERROR_FILE
10076 5f 4e 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53  _NOT_FOUND)) ? S
10077 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54  QLITE_OK : SQLIT
10078 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a  E_IOERR_DELETE;.
10079 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  }../*.** Check t
1007a 68 65 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64  he existance and
1007b 20 73 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c   status of a fil
1007c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1007d 20 77 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71   winAccess(.  sq
1007e 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
1007f 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20           /* Not 
10080 75 73 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f  used on win32 */
10081 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
10082 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a  Filename,     /*
10083 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f   Name of file to
10084 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   check */.  int 
10085 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
10086 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f         /* Type o
10087 66 20 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f  f test to make o
10088 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20  n this file */. 
10089 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20   int *pResOut   
1008a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1008b 55 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b  UT: Result */.){
1008c 0a 20 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20  .  DWORD attr;. 
1008d 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 76   int rc = 0;.  v
1008e 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20  oid *zConverted 
1008f 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c  = convertUtf8Fil
10090 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29  ename(zFilename)
10091 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
10092 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66  ETER(pVfs);.  if
10093 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20  ( zConverted==0 
10094 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
10095 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
10096 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
10097 20 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69      attr = GetFi
10098 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 28 57  leAttributesW((W
10099 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
1009a 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
1009b 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
1009c 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
1009d 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
1009e 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e  xecuted. .** Sin
1009f 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72  ce the ASCII ver
100a0 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69  sion of these Wi
100a1 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74  ndows API do not
100a2 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45   exist for WINCE
100a3 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74  ,.** it's import
100a4 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72  ant to not refer
100a5 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49  ence them for WI
100a6 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23  NCE builds..*/.#
100a7 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
100a8 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
100a9 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c     attr = GetFil
100aa 65 41 74 74 72 69 62 75 74 65 73 41 28 28 63 68  eAttributesA((ch
100ab 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ar*)zConverted);
100ac 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72  .#endif.  }.  fr
100ad 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
100ae 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20    switch( flags 
100af 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
100b0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a  TE_ACCESS_READ:.
100b1 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
100b2 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20  ACCESS_EXISTS:. 
100b3 20 20 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d       rc = attr!=
100b4 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
100b5 52 49 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62  RIBUTES;.      b
100b6 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
100b7 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
100b8 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63  DWRITE:.      rc
100b9 20 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f   = (attr & FILE_
100ba 41 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e  ATTRIBUTE_READON
100bb 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72  LY)==0;.      br
100bc 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
100bd 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21  :.      assert(!
100be 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61  "Invalid flags a
100bf 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20  rgument");.  }. 
100c0 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a   *pResOut = rc;.
100c1 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
100c2 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75  OK;.}.../*.** Tu
100c3 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61  rn a relative pa
100c4 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75  thname into a fu
100c5 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72  ll pathname.  Wr
100c6 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20  ite the full.** 
100c7 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f  pathname into zO
100c8 75 74 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69  ut[].  zOut[] wi
100c9 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70  ll be at least p
100ca 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a  Vfs->mxPathname.
100cb 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ** bytes in size
100cc 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
100cd 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  winFullPathname(
100ce 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
100cf 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20  pVfs,           
100d0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76   /* Pointer to v
100d1 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63  fs object */.  c
100d2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61  onst char *zRela
100d3 74 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  tive,        /* 
100d4 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76  Possibly relativ
100d5 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a  e input path */.
100d6 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20    int nFull,    
100d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
100d8 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75  /* Size of outpu
100d9 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65  t buffer in byte
100da 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75  s */.  char *zFu
100db 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
100dc 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62       /* Output b
100dd 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a 23  uffer */.){.  .#
100de 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47  if defined(__CYG
100df 57 49 4e 5f 5f 29 0a 20 20 55 4e 55 53 45 44 5f  WIN__).  UNUSED_
100e0 50 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29  PARAMETER(nFull)
100e1 3b 0a 20 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f  ;.  cygwin_conv_
100e2 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61  to_full_win32_pa
100e3 74 68 28 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46  th(zRelative, zF
100e4 75 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ull);.  return S
100e5 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66  QLITE_OK;.#endif
100e6 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  ..#if SQLITE_OS_
100e7 57 49 4e 43 45 0a 20 20 55 4e 55 53 45 44 5f 50  WINCE.  UNUSED_P
100e8 41 52 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b  ARAMETER(nFull);
100e9 0a 20 20 2f 2a 20 57 69 6e 43 45 20 68 61 73 20  .  /* WinCE has 
100ea 6e 6f 20 63 6f 6e 63 65 70 74 20 6f 66 20 61 20  no concept of a 
100eb 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d  relative pathnam
100ec 65 2c 20 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f  e, or so I am to
100ed 6c 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ld. */.  sqlite3
100ee 5f 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e  _snprintf(pVfs->
100ef 6d 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c  mxPathname, zFul
100f0 6c 2c 20 22 25 73 22 2c 20 7a 52 65 6c 61 74 69  l, "%s", zRelati
100f1 76 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ve);.  return SQ
100f2 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a  LITE_OK;.#endif.
100f3 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f  .#if !SQLITE_OS_
100f4 57 49 4e 43 45 20 26 26 20 21 64 65 66 69 6e 65  WINCE && !define
100f5 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20  d(__CYGWIN__).  
100f6 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69  int nByte;.  voi
100f7 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20  d *zConverted;. 
100f8 20 63 68 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 55   char *zOut;.  U
100f9 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
100fa 6e 46 75 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 65  nFull);.  zConve
100fb 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74  rted = convertUt
100fc 66 38 46 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61  f8Filename(zRela
100fd 74 69 76 65 29 3b 0a 20 20 69 66 28 20 69 73 4e  tive);.  if( isN
100fe 54 28 29 20 29 7b 0a 20 20 20 20 57 43 48 41 52  T() ){.    WCHAR
100ff 20 2a 7a 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79   *zTemp;.    nBy
10100 74 65 20 3d 20 47 65 74 46 75 6c 6c 50 61 74 68  te = GetFullPath
10101 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43  NameW((WCHAR*)zC
10102 6f 6e 76 65 72 74 65 64 2c 20 30 2c 20 30 2c 20  onverted, 0, 0, 
10103 30 29 20 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d  0) + 3;.    zTem
10104 70 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74  p = malloc( nByt
10105 65 2a 73 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30  e*sizeof(zTemp[0
10106 5d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 54  ]) );.    if( zT
10107 65 6d 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  emp==0 ){.      
10108 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29  free(zConverted)
10109 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
1010a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1010b 20 7d 0a 20 20 20 20 47 65 74 46 75 6c 6c 50 61   }.    GetFullPa
1010c 74 68 4e 61 6d 65 57 28 28 57 43 48 41 52 2a 29  thNameW((WCHAR*)
1010d 7a 43 6f 6e 76 65 72 74 65 64 2c 20 6e 42 79 74  zConverted, nByt
1010e 65 2c 20 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20  e, zTemp, 0);.  
1010f 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65    free(zConverte
10110 64 29 3b 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75  d);.    zOut = u
10111 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 65  nicodeToUtf8(zTe
10112 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 54  mp);.    free(zT
10113 65 6d 70 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20  emp);./* isNT() 
10114 69 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f  is 1 if SQLITE_O
10115 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74  S_WINCE==1, so t
10116 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65  his else is neve
10117 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20  r executed. .** 
10118 53 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20  Since the ASCII 
10119 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65  version of these
1011a 20 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20   Windows API do 
1011b 6e 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49  not exist for WI
1011c 4e 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70  NCE,.** it's imp
1011d 6f 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65  ortant to not re
1011e 66 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72  ference them for
1011f 20 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a   WINCE builds..*
10120 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
10121 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65  WINCE==0.  }else
10122 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 65 6d  {.    char *zTem
10123 70 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 47  p;.    nByte = G
10124 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28  etFullPathNameA(
10125 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65  (char*)zConverte
10126 64 2c 20 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b  d, 0, 0, 0) + 3;
10127 0a 20 20 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c  .    zTemp = mal
10128 6c 6f 63 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f  loc( nByte*sizeo
10129 66 28 7a 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20  f(zTemp[0]) );. 
1012a 20 20 20 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20     if( zTemp==0 
1012b 29 7b 0a 20 20 20 20 20 20 66 72 65 65 28 7a 43  ){.      free(zC
1012c 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20 20  onverted);.     
1012d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
1012e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
1012f 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41  GetFullPathNameA
10130 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74  ((char*)zConvert
10131 65 64 2c 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70  ed, nByte, zTemp
10132 2c 20 30 29 3b 0a 20 20 20 20 66 72 65 65 28 7a  , 0);.    free(z
10133 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
10134 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77  zOut = sqlite3_w
10135 69 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66  in32_mbcs_to_utf
10136 38 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 66 72  8(zTemp);.    fr
10137 65 65 28 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69  ee(zTemp);.#endi
10138 66 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74  f.  }.  if( zOut
10139 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1013a 73 6e 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d  snprintf(pVfs->m
1013b 78 50 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c  xPathname, zFull
1013c 2c 20 22 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20  , "%s", zOut);. 
1013d 20 20 20 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20     free(zOut);. 
1013e 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1013f 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
10140 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
10141 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69  NOMEM;.  }.#endi
10142 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74  f.}../*.** Get t
10143 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f  he sector size o
10144 66 20 74 68 65 20 64 65 76 69 63 65 20 75 73 65  f the device use
10145 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 66 69  d to store.** fi
10146 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
10147 74 20 67 65 74 53 65 63 74 6f 72 53 69 7a 65 28  t getSectorSize(
10148 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73  .    sqlite3_vfs
10149 20 2a 70 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73   *pVfs,.    cons
1014a 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76  t char *zRelativ
1014b 65 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 66  e     /* UTF-8 f
1014c 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20  ile name */.){. 
1014d 20 44 57 4f 52 44 20 62 79 74 65 73 50 65 72 53   DWORD bytesPerS
1014e 65 63 74 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44  ector = SQLITE_D
1014f 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49  EFAULT_SECTOR_SI
10150 5a 45 3b 0a 20 20 2f 2a 20 47 65 74 44 69 73 6b  ZE;.  /* GetDisk
10151 46 72 65 65 53 70 61 63 65 20 69 73 20 6e 6f 74  FreeSpace is not
10152 20 73 75 70 70 6f 72 74 65 64 20 75 6e 64 65 72   supported under
10153 20 57 49 4e 43 45 20 2a 2f 0a 23 69 66 20 53 51   WINCE */.#if SQ
10154 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20  LITE_OS_WINCE.  
10155 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
10156 28 70 56 66 73 29 3b 0a 20 20 55 4e 55 53 45 44  (pVfs);.  UNUSED
10157 5f 50 41 52 41 4d 45 54 45 52 28 7a 52 65 6c 61  _PARAMETER(zRela
10158 74 69 76 65 29 3b 0a 23 65 6c 73 65 0a 20 20 63  tive);.#else.  c
10159 68 61 72 20 7a 46 75 6c 6c 70 61 74 68 5b 4d 41  har zFullpath[MA
1015a 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 6e 74  X_PATH+1];.  int
1015b 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 64 77 52   rc;.  DWORD dwR
1015c 65 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20  et = 0;.  DWORD 
1015d 64 77 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 0a 20  dwDummy;..  /*. 
1015e 20 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 67   ** We need to g
1015f 65 74 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  et the full path
10160 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   name of the fil
10161 65 0a 20 20 2a 2a 20 74 6f 20 67 65 74 20 74 68  e.  ** to get th
10162 65 20 64 72 69 76 65 20 6c 65 74 74 65 72 20 74  e drive letter t
10163 6f 20 6c 6f 6f 6b 20 75 70 20 74 68 65 20 73 65  o look up the se
10164 63 74 6f 72 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a  ctor.  ** size..
10165 20 20 2a 2f 0a 20 20 72 63 20 3d 20 77 69 6e 46    */.  rc = winF
10166 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66 73  ullPathname(pVfs
10167 2c 20 7a 52 65 6c 61 74 69 76 65 2c 20 4d 41 58  , zRelative, MAX
10168 5f 50 41 54 48 2c 20 7a 46 75 6c 6c 70 61 74 68  _PATH, zFullpath
10169 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 53  );.  if( rc == S
1016a 51 4c 49 54 45 5f 4f 4b 20 29 0a 20 20 7b 0a 20  QLITE_OK ).  {. 
1016b 20 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72     void *zConver
1016c 74 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  ted = convertUtf
1016d 38 46 69 6c 65 6e 61 6d 65 28 7a 46 75 6c 6c 70  8Filename(zFullp
1016e 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20 7a 43  ath);.    if( zC
1016f 6f 6e 76 65 72 74 65 64 20 29 7b 0a 20 20 20 20  onverted ){.    
10170 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
10171 20 20 20 20 20 20 20 20 2f 2a 20 74 72 69 6d 20          /* trim 
10172 70 61 74 68 20 74 6f 20 6a 75 73 74 20 64 72 69  path to just dri
10173 76 65 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a  ve reference */.
10174 20 20 20 20 20 20 20 20 57 43 48 41 52 20 2a 70          WCHAR *p
10175 20 3d 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20   = zConverted;. 
10176 20 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70         for(;*p;p
10177 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
10178 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b  f( *p == '\\' ){
10179 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 20  .            *p 
1017a 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20  = '\0';.        
1017b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1017c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1017d 0a 20 20 20 20 20 20 20 20 64 77 52 65 74 20 3d  .        dwRet =
1017e 20 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 63   GetDiskFreeSpac
1017f 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76  eW((WCHAR*)zConv
10180 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20  erted,.         
10181 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10182 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d           &dwDumm
10183 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y,.             
10184 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10185 20 20 20 20 20 26 62 79 74 65 73 50 65 72 53 65       &bytesPerSe
10186 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20  ctor,.          
10187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10188 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79          &dwDummy
10189 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1018a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1018b 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20      &dwDummy);. 
1018c 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1018d 20 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74 68      /* trim path
1018e 20 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20 72   to just drive r
1018f 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20  eference */.    
10190 20 20 20 20 63 68 61 72 20 2a 70 20 3d 20 28 63      char *p = (c
10191 68 61 72 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64  har *)zConverted
10192 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 2a  ;.        for(;*
10193 70 3b 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  p;p++){.        
10194 20 20 69 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27    if( *p == '\\'
10195 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
10196 2a 70 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  *p = '\0';.     
10197 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
10198 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
10199 20 20 7d 0a 20 20 20 20 20 20 20 20 64 77 52 65    }.        dwRe
1019a 74 20 3d 20 47 65 74 44 69 73 6b 46 72 65 65 53  t = GetDiskFreeS
1019b 70 61 63 65 41 28 28 63 68 61 72 2a 29 7a 43 6f  paceA((char*)zCo
1019c 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20  nverted,.       
1019d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1019e 20 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75             &dwDu
1019f 6d 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  mmy,.           
101a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
101a1 20 20 20 20 20 20 20 26 62 79 74 65 73 50 65 72         &bytesPer
101a2 53 65 63 74 6f 72 2c 0a 20 20 20 20 20 20 20 20  Sector,.        
101a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
101a4 20 20 20 20 20 20 20 20 20 20 26 64 77 44 75 6d            &dwDum
101a5 6d 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  my,.            
101a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
101a7 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 29 3b        &dwDummy);
101a8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
101a9 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
101aa 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
101ab 64 77 52 65 74 20 29 7b 0a 20 20 20 20 20 20 62  dwRet ){.      b
101ac 79 74 65 73 50 65 72 53 65 63 74 6f 72 20 3d 20  ytesPerSector = 
101ad 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53  SQLITE_DEFAULT_S
101ae 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 20 20  ECTOR_SIZE;.    
101af 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  }.  }.#endif.  r
101b0 65 74 75 72 6e 20 28 69 6e 74 29 20 62 79 74 65  eturn (int) byte
101b1 73 50 65 72 53 65 63 74 6f 72 3b 20 0a 7d 0a 0a  sPerSector; .}..
101b2 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
101b3 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
101b4 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  ON./*.** Interfa
101b5 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20  ces for opening 
101b6 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  a shared library
101b7 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20  , finding entry 
101b8 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e  points.** within
101b9 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
101ba 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ary, and closing
101bb 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
101bc 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e  ary..*/./*.** In
101bd 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70 65  terfaces for ope
101be 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c 69  ning a shared li
101bf 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20 65  brary, finding e
101c0 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77  ntry points.** w
101c1 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65 64  ithin the shared
101c2 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63 6c   library, and cl
101c3 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65 64  osing the shared
101c4 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61   library..*/.sta
101c5 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f  tic void *winDlO
101c6 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20  pen(sqlite3_vfs 
101c7 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61  *pVfs, const cha
101c8 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
101c9 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69   HANDLE h;.  voi
101ca 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  d *zConverted = 
101cb 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e  convertUtf8Filen
101cc 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a  ame(zFilename);.
101cd 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
101ce 45 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20  ER(pVfs);.  if( 
101cf 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b  zConverted==0 ){
101d0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
101d1 20 7d 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20   }.  if( isNT() 
101d2 29 7b 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c  ){.    h = LoadL
101d3 69 62 72 61 72 79 57 28 28 57 43 48 41 52 2a 29  ibraryW((WCHAR*)
101d4 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20  zConverted);./* 
101d5 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53  isNT() is 1 if S
101d6 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
101d7 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20  1, so this else 
101d8 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65  is never execute
101d9 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65  d. .** Since the
101da 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f   ASCII version o
101db 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20  f these Windows 
101dc 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74  API do not exist
101dd 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69   for WINCE,.** i
101de 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f  t's important to
101df 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74   not reference t
101e0 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75  hem for WINCE bu
101e1 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ilds..*/.#if SQL
101e2 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
101e3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d    }else{.    h =
101e4 20 4c 6f 61 64 4c 69 62 72 61 72 79 41 28 28 63   LoadLibraryA((c
101e5 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29  har*)zConverted)
101e6 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66  ;.#endif.  }.  f
101e7 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
101e8 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a  .  return (void*
101e9 29 68 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  )h;.}.static voi
101ea 64 20 77 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c  d winDlError(sql
101eb 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
101ec 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
101ed 7a 42 75 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53  zBufOut){.  UNUS
101ee 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
101ef 73 29 3b 0a 20 20 67 65 74 4c 61 73 74 45 72 72  s);.  getLastErr
101f0 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66  orMsg(nBuf, zBuf
101f1 4f 75 74 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a 77  Out);.}.void (*w
101f2 69 6e 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f  inDlSym(sqlite3_
101f3 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20  vfs *pVfs, void 
101f4 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20  *pHandle, const 
101f5 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28  char *zSymbol))(
101f6 76 6f 69 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  void){.  UNUSED_
101f7 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b  PARAMETER(pVfs);
101f8 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
101f9 49 4e 43 45 0a 20 20 2f 2a 20 54 68 65 20 47 65  INCE.  /* The Ge
101fa 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 29 20  tProcAddressA() 
101fb 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20  routine is only 
101fc 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e  available on win
101fd 63 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ce. */.  return 
101fe 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29 47  (void(*)(void))G
101ff 65 74 50 72 6f 63 41 64 64 72 65 73 73 41 28 28  etProcAddressA((
10200 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20  HANDLE)pHandle, 
10201 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a  zSymbol);.#else.
10202 20 20 2f 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77    /* All other w
10203 69 6e 64 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73  indows platforms
10204 20 65 78 70 65 63 74 20 47 65 74 50 72 6f 63 41   expect GetProcA
10205 64 64 72 65 73 73 28 29 20 74 6f 20 74 61 6b 65  ddress() to take
10206 0a 20 20 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74  .  ** an Ansi st
10207 72 69 6e 67 20 72 65 67 61 72 64 6c 65 73 73 20  ring regardless 
10208 6f 66 20 74 68 65 20 5f 55 4e 49 43 4f 44 45 20  of the _UNICODE 
10209 73 65 74 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74  setting */.  ret
1020a 75 72 6e 20 28 76 6f 69 64 28 2a 29 28 76 6f 69  urn (void(*)(voi
1020b 64 29 29 47 65 74 50 72 6f 63 41 64 64 72 65 73  d))GetProcAddres
1020c 73 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c  s((HANDLE)pHandl
1020d 65 2c 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e  e, zSymbol);.#en
1020e 64 69 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c  dif.}.void winDl
1020f 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66  Close(sqlite3_vf
10210 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70  s *pVfs, void *p
10211 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45  Handle){.  UNUSE
10212 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
10213 29 3b 0a 20 20 46 72 65 65 4c 69 62 72 61 72 79  );.  FreeLibrary
10214 28 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65  ((HANDLE)pHandle
10215 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66  );.}.#else /* if
10216 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
10217 44 5f 45 58 54 45 4e 53 49 4f 4e 20 69 73 20 64  D_EXTENSION is d
10218 65 66 69 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65  efined: */.  #de
10219 66 69 6e 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20  fine winDlOpen  
1021a 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44  0.  #define winD
1021b 6c 45 72 72 6f 72 20 30 0a 20 20 23 64 65 66 69  lError 0.  #defi
1021c 6e 65 20 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a  ne winDlSym   0.
1021d 20 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 43    #define winDlC
1021e 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a  lose 0.#endif...
1021f 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75 70 20 74  /*.** Write up t
10220 6f 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66 20  o nBuf bytes of 
10221 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20  randomness into 
10222 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  zBuf..*/.static 
10223 69 6e 74 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73  int winRandomnes
10224 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  s(sqlite3_vfs *p
10225 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63  Vfs, int nBuf, c
10226 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e  har *zBuf){.  in
10227 74 20 6e 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45  t n = 0;.  UNUSE
10228 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66 73  D_PARAMETER(pVfs
10229 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  );.#if defined(S
1022a 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20  QLITE_TEST).  n 
1022b 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74  = nBuf;.  memset
1022c 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b  (zBuf, 0, nBuf);
1022d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 73 69 7a  .#else.  if( siz
1022e 65 6f 66 28 53 59 53 54 45 4d 54 49 4d 45 29 3c  eof(SYSTEMTIME)<
1022f 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 53  =nBuf-n ){.    S
10230 59 53 54 45 4d 54 49 4d 45 20 78 3b 0a 20 20 20  YSTEMTIME x;.   
10231 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26   GetSystemTime(&
10232 78 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  x);.    memcpy(&
10233 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a  zBuf[n], &x, siz
10234 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b  eof(x));.    n +
10235 3d 20 73 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d  = sizeof(x);.  }
10236 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44 57  .  if( sizeof(DW
10237 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a  ORD)<=nBuf-n ){.
10238 20 20 20 20 44 57 4f 52 44 20 70 69 64 20 3d 20      DWORD pid = 
10239 47 65 74 43 75 72 72 65 6e 74 50 72 6f 63 65 73  GetCurrentProces
1023a 73 49 64 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70  sId();.    memcp
1023b 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 70 69 64  y(&zBuf[n], &pid
1023c 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a  , sizeof(pid));.
1023d 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28      n += sizeof(
1023e 70 69 64 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  pid);.  }.  if( 
1023f 73 69 7a 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e  sizeof(DWORD)<=n
10240 42 75 66 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f  Buf-n ){.    DWO
10241 52 44 20 63 6e 74 20 3d 20 47 65 74 54 69 63 6b  RD cnt = GetTick
10242 43 6f 75 6e 74 28 29 3b 0a 20 20 20 20 6d 65 6d  Count();.    mem
10243 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 63  cpy(&zBuf[n], &c
10244 6e 74 2c 20 73 69 7a 65 6f 66 28 63 6e 74 29 29  nt, sizeof(cnt))
10245 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f  ;.    n += sizeo
10246 66 28 63 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66  f(cnt);.  }.  if
10247 28 20 73 69 7a 65 6f 66 28 4c 41 52 47 45 5f 49  ( sizeof(LARGE_I
10248 4e 54 45 47 45 52 29 3c 3d 6e 42 75 66 2d 6e 20  NTEGER)<=nBuf-n 
10249 29 7b 0a 20 20 20 20 4c 41 52 47 45 5f 49 4e 54  ){.    LARGE_INT
1024a 45 47 45 52 20 69 3b 0a 20 20 20 20 51 75 65 72  EGER i;.    Quer
1024b 79 50 65 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e  yPerformanceCoun
1024c 74 65 72 28 26 69 29 3b 0a 20 20 20 20 6d 65 6d  ter(&i);.    mem
1024d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69  cpy(&zBuf[n], &i
1024e 2c 20 73 69 7a 65 6f 66 28 69 29 29 3b 0a 20 20  , sizeof(i));.  
1024f 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 69 29    n += sizeof(i)
10250 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  ;.  }.#endif.  r
10251 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn n;.}.../*.
10252 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c  ** Sleep for a l
10253 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65  ittle while.  Re
10254 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  turn the amount 
10255 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a  of time slept..*
10256 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
10257 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66  Sleep(sqlite3_vf
10258 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63  s *pVfs, int mic
10259 72 6f 73 65 63 29 7b 0a 20 20 53 6c 65 65 70 28  rosec){.  Sleep(
1025a 28 6d 69 63 72 6f 73 65 63 2b 39 39 39 29 2f 31  (microsec+999)/1
1025b 30 30 30 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  000);.  UNUSED_P
1025c 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a  ARAMETER(pVfs);.
1025d 20 20 72 65 74 75 72 6e 20 28 28 6d 69 63 72 6f    return ((micro
1025e 73 65 63 2b 39 39 39 29 2f 31 30 30 30 29 2a 31  sec+999)/1000)*1
1025f 30 30 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  000;.}../*.** Th
10260 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
10261 61 62 6c 65 2c 20 69 66 20 73 65 74 20 74 6f 20  able, if set to 
10262 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  a non-zero value
10263 2c 20 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65  , becomes the re
10264 73 75 6c 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64  sult.** returned
10265 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43   from sqlite3OsC
10266 75 72 72 65 6e 74 54 69 6d 65 28 29 2e 20 20 54  urrentTime().  T
10267 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
10268 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64  testing..*/.#ifd
10269 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
1026a 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1026b 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69  lite3_current_ti
1026c 6d 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  me = 0;.#endif..
1026d 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 63  /*.** Find the c
1026e 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e 20  urrent time (in 
1026f 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64 69  Universal Coordi
10270 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57 72  nated Time).  Wr
10271 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72 65  ite the.** curre
10272 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65  nt time and date
10273 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79   as a Julian Day
10274 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72   number into *pr
10275 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72  Now and.** retur
10276 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20 69  n 0.  Return 1 i
10277 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20 64  f the time and d
10278 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  ate cannot be fo
10279 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43  und..*/.int winC
1027a 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
1027b 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 64 6f  e3_vfs *pVfs, do
1027c 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20  uble *prNow){.  
1027d 46 49 4c 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f  FILETIME ft;.  /
1027e 2a 20 46 49 4c 45 54 49 4d 45 20 73 74 72 75 63  * FILETIME struc
1027f 74 75 72 65 20 69 73 20 61 20 36 34 2d 62 69 74  ture is a 64-bit
10280 20 76 61 6c 75 65 20 72 65 70 72 65 73 65 6e 74   value represent
10281 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ing the number o
10282 66 20 0a 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f  f .     100-nano
10283 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73  second intervals
10284 20 73 69 6e 63 65 20 4a 61 6e 75 61 72 79 20 31   since January 1
10285 2c 20 31 36 30 31 20 28 3d 20 4a 44 20 32 33 30  , 1601 (= JD 230
10286 35 38 31 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20  5813.5). .  */. 
10287 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74   sqlite3_int64 t
10288 69 6d 65 57 3b 20 20 20 2f 2a 20 57 68 6f 6c 65  imeW;   /* Whole
10289 20 64 61 79 73 20 2a 2f 0a 20 20 73 71 6c 69 74   days */.  sqlit
1028a 65 33 5f 69 6e 74 36 34 20 74 69 6d 65 46 3b 20  e3_int64 timeF; 
1028b 20 20 2f 2a 20 46 72 61 63 74 69 6f 6e 61 6c 20    /* Fractional 
1028c 44 61 79 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4e 75  Days */..  /* Nu
1028d 6d 62 65 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f  mber of 100-nano
1028e 73 65 63 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73  second intervals
1028f 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 64 61 79   in a single day
10290 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
10291 73 74 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  st sqlite3_int64
10292 20 6e 74 75 50 65 72 44 61 79 20 3d 20 0a 20 20   ntuPerDay = .  
10293 20 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71      10000000*(sq
10294 6c 69 74 65 33 5f 69 6e 74 36 34 29 38 36 34 30  lite3_int64)8640
10295 30 3b 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20  0;..  /* Number 
10296 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e  of 100-nanosecon
10297 64 20 69 6e 74 65 72 76 61 6c 73 20 69 6e 20 68  d intervals in h
10298 61 6c 66 20 6f 66 20 61 20 64 61 79 20 2a 2f 0a  alf of a day */.
10299 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
1029a 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74 75  qlite3_int64 ntu
1029b 50 65 72 48 61 6c 66 44 61 79 20 3d 20 0a 20 20  PerHalfDay = .  
1029c 20 20 20 20 31 30 30 30 30 30 30 30 2a 28 73 71      10000000*(sq
1029d 6c 69 74 65 33 5f 69 6e 74 36 34 29 34 33 32 30  lite3_int64)4320
1029e 30 3b 0a 0a 20 20 2f 2a 20 32 5e 33 32 20 2d 20  0;..  /* 2^32 - 
1029f 74 6f 20 61 76 6f 69 64 20 75 73 65 20 6f 66 20  to avoid use of 
102a0 4c 4c 20 61 6e 64 20 77 61 72 6e 69 6e 67 73 20  LL and warnings 
102a1 69 6e 20 67 63 63 20 2a 2f 0a 20 20 73 74 61 74  in gcc */.  stat
102a2 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
102a3 5f 69 6e 74 36 34 20 6d 61 78 33 32 42 69 74 56  _int64 max32BitV
102a4 61 6c 75 65 20 3d 20 0a 20 20 20 20 20 20 28 73  alue = .      (s
102a5 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 32 30 30  qlite3_int64)200
102a6 30 30 30 30 30 30 30 20 2b 20 28 73 71 6c 69 74  0000000 + (sqlit
102a7 65 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30 30  e3_int64)2000000
102a8 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f 69  000 + (sqlite3_i
102a9 6e 74 36 34 29 32 39 34 39 36 37 32 39 36 3b 0a  nt64)294967296;.
102aa 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
102ab 49 4e 43 45 0a 20 20 53 59 53 54 45 4d 54 49 4d  INCE.  SYSTEMTIM
102ac 45 20 74 69 6d 65 3b 0a 20 20 47 65 74 53 79 73  E time;.  GetSys
102ad 74 65 6d 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a  temTime(&time);.
102ae 20 20 2f 2a 20 69 66 20 53 79 73 74 65 6d 54 69    /* if SystemTi
102af 6d 65 54 6f 46 69 6c 65 54 69 6d 65 28 29 20 66  meToFileTime() f
102b0 61 69 6c 73 2c 20 69 74 20 72 65 74 75 72 6e 73  ails, it returns
102b1 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28   zero. */.  if (
102b2 21 53 79 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c  !SystemTimeToFil
102b3 65 54 69 6d 65 28 26 74 69 6d 65 2c 26 66 74 29  eTime(&time,&ft)
102b4 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
102b5 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74  .  }.#else.  Get
102b6 53 79 73 74 65 6d 54 69 6d 65 41 73 46 69 6c 65  SystemTimeAsFile
102b7 54 69 6d 65 28 20 26 66 74 20 29 3b 0a 23 65 6e  Time( &ft );.#en
102b8 64 69 66 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  dif.  UNUSED_PAR
102b9 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20  AMETER(pVfs);.  
102ba 74 69 6d 65 57 20 3d 20 28 28 28 73 71 6c 69 74  timeW = (((sqlit
102bb 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 48 69  e3_int64)ft.dwHi
102bc 67 68 44 61 74 65 54 69 6d 65 29 2a 6d 61 78 33  ghDateTime)*max3
102bd 32 42 69 74 56 61 6c 75 65 29 20 2b 20 28 73 71  2BitValue) + (sq
102be 6c 69 74 65 33 5f 69 6e 74 36 34 29 66 74 2e 64  lite3_int64)ft.d
102bf 77 4c 6f 77 44 61 74 65 54 69 6d 65 3b 0a 20 20  wLowDateTime;.  
102c0 74 69 6d 65 46 20 3d 20 74 69 6d 65 57 20 25 20  timeF = timeW % 
102c1 6e 74 75 50 65 72 44 61 79 3b 20 20 20 20 20 20  ntuPerDay;      
102c2 20 20 20 20 2f 2a 20 66 72 61 63 74 69 6f 6e 61      /* fractiona
102c3 6c 20 64 61 79 73 20 28 31 30 30 2d 6e 61 6e 6f  l days (100-nano
102c4 73 65 63 6f 6e 64 73 29 20 2a 2f 0a 20 20 74 69  seconds) */.  ti
102c5 6d 65 57 20 3d 20 74 69 6d 65 57 20 2f 20 6e 74  meW = timeW / nt
102c6 75 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 20  uPerDay;        
102c7 20 20 2f 2a 20 77 68 6f 6c 65 20 64 61 79 73 20    /* whole days 
102c8 2a 2f 0a 20 20 74 69 6d 65 57 20 3d 20 74 69 6d  */.  timeW = tim
102c9 65 57 20 2b 20 32 33 30 35 38 31 33 3b 20 20 20  eW + 2305813;   
102ca 20 20 20 20 20 20 20 20 20 2f 2a 20 61 64 64 20           /* add 
102cb 77 68 6f 6c 65 20 64 61 79 73 20 28 66 72 6f 6d  whole days (from
102cc 20 32 33 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20   2305813.5) */. 
102cd 20 74 69 6d 65 46 20 3d 20 74 69 6d 65 46 20 2b   timeF = timeF +
102ce 20 6e 74 75 50 65 72 48 61 6c 66 44 61 79 3b 20   ntuPerHalfDay; 
102cf 20 20 20 20 20 2f 2a 20 61 64 64 20 68 61 6c 66       /* add half
102d0 20 61 20 64 61 79 20 28 66 72 6f 6d 20 32 33 30   a day (from 230
102d1 35 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d  5813.5) */.  tim
102d2 65 57 20 3d 20 74 69 6d 65 57 20 2b 20 28 74 69  eW = timeW + (ti
102d3 6d 65 46 2f 6e 74 75 50 65 72 44 61 79 29 3b 20  meF/ntuPerDay); 
102d4 20 2f 2a 20 61 64 64 20 77 68 6f 6c 65 20 64 61   /* add whole da
102d5 79 20 69 66 20 68 61 6c 66 20 64 61 79 20 6d 61  y if half day ma
102d6 64 65 20 6f 6e 65 20 2a 2f 0a 20 20 74 69 6d 65  de one */.  time
102d7 46 20 3d 20 74 69 6d 65 46 20 25 20 6e 74 75 50  F = timeF % ntuP
102d8 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20  erDay;          
102d9 2f 2a 20 63 6f 6d 70 75 74 65 20 6e 65 77 20 66  /* compute new f
102da 72 61 63 74 69 6f 6e 61 6c 20 64 61 79 73 20 2a  ractional days *
102db 2f 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 28 64 6f  /.  *prNow = (do
102dc 75 62 6c 65 29 74 69 6d 65 57 20 2b 20 28 28 64  uble)timeW + ((d
102dd 6f 75 62 6c 65 29 74 69 6d 65 46 20 2f 20 28 64  ouble)timeF / (d
102de 6f 75 62 6c 65 29 6e 74 75 50 65 72 44 61 79 29  ouble)ntuPerDay)
102df 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
102e0 54 45 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74  TEST.  if( sqlit
102e1 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20  e3_current_time 
102e2 29 7b 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20  ){.    *prNow = 
102e3 28 28 64 6f 75 62 6c 65 29 73 71 6c 69 74 65 33  ((double)sqlite3
102e4 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 2b 20  _current_time + 
102e5 28 64 6f 75 62 6c 65 29 34 33 32 30 30 29 20 2f  (double)43200) /
102e6 20 28 64 6f 75 62 6c 65 29 38 36 34 30 30 20 2b   (double)86400 +
102e7 20 28 64 6f 75 62 6c 65 29 32 34 34 30 35 38 37   (double)2440587
102e8 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  ;.  }.#endif.  r
102e9 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
102ea 2a 20 54 68 65 20 69 64 65 61 20 69 73 20 74 68  * The idea is th
102eb 61 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  at this function
102ec 20 77 6f 72 6b 73 20 6c 69 6b 65 20 61 20 63 6f   works like a co
102ed 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20  mbination of.** 
102ee 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 61  GetLastError() a
102ef 6e 64 20 46 6f 72 6d 61 74 4d 65 73 73 61 67 65  nd FormatMessage
102f0 28 29 20 6f 6e 20 77 69 6e 64 6f 77 73 20 28 6f  () on windows (o
102f1 72 20 65 72 72 6e 6f 20 61 6e 64 0a 2a 2a 20 73  r errno and.** s
102f2 74 72 65 72 72 6f 72 5f 72 28 29 20 6f 6e 20 75  trerror_r() on u
102f3 6e 69 78 29 2e 20 41 66 74 65 72 20 61 6e 20 65  nix). After an e
102f4 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
102f5 20 62 79 20 61 6e 20 4f 53 0a 2a 2a 20 66 75 6e   by an OS.** fun
102f6 63 74 69 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61  ction, SQLite ca
102f7 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f  lls this functio
102f8 6e 20 77 69 74 68 20 7a 42 75 66 20 70 6f 69 6e  n with zBuf poin
102f9 74 69 6e 67 20 74 6f 0a 2a 2a 20 61 20 62 75 66  ting to.** a buf
102fa 66 65 72 20 6f 66 20 6e 42 75 66 20 62 79 74 65  fer of nBuf byte
102fb 73 2e 20 54 68 65 20 4f 53 20 6c 61 79 65 72 20  s. The OS layer 
102fc 73 68 6f 75 6c 64 20 70 6f 70 75 6c 61 74 65 20  should populate 
102fd 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 77 69  the.** buffer wi
102fe 74 68 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61  th a nul-termina
102ff 74 65 64 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  ted UTF-8 encode
10300 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a  d error message.
10301 2a 2a 20 64 65 73 63 72 69 62 69 6e 67 20 74 68  ** describing th
10302 65 20 6c 61 73 74 20 49 4f 20 65 72 72 6f 72 20  e last IO error 
10303 74 6f 20 68 61 76 65 20 6f 63 63 75 72 72 65 64  to have occurred
10304 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c   within the call
10305 69 6e 67 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a  ing.** thread..*
10306 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 72 72 6f  *.** If the erro
10307 72 20 6d 65 73 73 61 67 65 20 69 73 20 74 6f 6f  r message is too
10308 20 6c 61 72 67 65 20 66 6f 72 20 74 68 65 20 73   large for the s
10309 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2c 0a  upplied buffer,.
1030a 2a 2a 20 69 74 20 73 68 6f 75 6c 64 20 62 65 20  ** it should be 
1030b 74 72 75 6e 63 61 74 65 64 2e 20 54 68 65 20 72  truncated. The r
1030c 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 78  eturn value of x
1030d 47 65 74 4c 61 73 74 45 72 72 6f 72 0a 2a 2a 20  GetLastError.** 
1030e 69 73 20 7a 65 72 6f 20 69 66 20 74 68 65 20 65  is zero if the e
1030f 72 72 6f 72 20 6d 65 73 73 61 67 65 20 66 69 74  rror message fit
10310 73 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 2c  s in the buffer,
10311 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20   or non-zero.** 
10312 6f 74 68 65 72 77 69 73 65 20 28 69 66 20 74 68  otherwise (if th
10313 65 20 6d 65 73 73 61 67 65 20 77 61 73 20 74 72  e message was tr
10314 75 6e 63 61 74 65 64 29 2e 20 49 66 20 6e 6f 6e  uncated). If non
10315 2d 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65  -zero is returne
10316 64 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73  d,.** then it is
10317 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74   not necessary t
10318 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e 75  o include the nu
10319 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 20 63 68 61  l-terminator cha
1031a 72 61 63 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65  racter.** in the
1031b 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 0a   output buffer..
1031c 2a 2a 0a 2a 2a 20 4e 6f 74 20 73 75 70 70 6c 79  **.** Not supply
1031d 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ing an error mes
1031e 73 61 67 65 20 77 69 6c 6c 20 68 61 76 65 20 6e  sage will have n
1031f 6f 20 61 64 76 65 72 73 65 20 65 66 66 65 63 74  o adverse effect
10320 0a 2a 2a 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49  .** on SQLite. I
10321 74 20 69 73 20 66 69 6e 65 20 74 6f 20 68 61 76  t is fine to hav
10322 65 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  e an implementat
10323 69 6f 6e 20 74 68 61 74 20 6e 65 76 65 72 0a 2a  ion that never.*
10324 2a 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  * returns an err
10325 6f 72 20 6d 65 73 73 61 67 65 3a 0a 2a 2a 0a 2a  or message:.**.*
10326 2a 20 20 20 69 6e 74 20 78 47 65 74 4c 61 73 74  *   int xGetLast
10327 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66  Error(sqlite3_vf
10328 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
10329 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
1032a 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 7a 42  **     assert(zB
1032b 75 66 5b 30 5d 3d 3d 27 5c 30 27 29 3b 0a 2a 2a  uf[0]=='\0');.**
1032c 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 2a       return 0;.*
1032d 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 48 6f 77 65  *   }.**.** Howe
1032e 76 65 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ver if an error 
1032f 6d 65 73 73 61 67 65 20 69 73 20 73 75 70 70 6c  message is suppl
10330 69 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20  ied, it will be 
10331 69 6e 63 6f 72 70 6f 72 61 74 65 64 0a 2a 2a 20  incorporated.** 
10332 62 79 20 73 71 6c 69 74 65 20 69 6e 74 6f 20 74  by sqlite into t
10333 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
10334 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 74 68   available to th
10335 65 20 75 73 65 72 20 75 73 69 6e 67 0a 2a 2a 20  e user using.** 
10336 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
10337 2c 20 70 6f 73 73 69 62 6c 79 20 6d 61 6b 69 6e  , possibly makin
10338 67 20 49 4f 20 65 72 72 6f 72 73 20 65 61 73 69  g IO errors easi
10339 65 72 20 74 6f 20 64 65 62 75 67 2e 0a 2a 2f 0a  er to debug..*/.
1033a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 47 65  static int winGe
1033b 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74  tLastError(sqlit
1033c 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
1033d 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
1033e 75 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  uf){.  UNUSED_PA
1033f 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20  RAMETER(pVfs);. 
10340 20 72 65 74 75 72 6e 20 67 65 74 4c 61 73 74 45   return getLastE
10341 72 72 6f 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42  rrorMsg(nBuf, zB
10342 75 66 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  uf);.}../*.** In
10343 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69  itialize and dei
10344 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70  nitialize the op
10345 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
10346 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c  nterface..*/.SQL
10347 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
10348 74 65 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64  te3_os_init(void
10349 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69  ){.  static sqli
1034a 74 65 33 5f 76 66 73 20 77 69 6e 56 66 73 20 3d  te3_vfs winVfs =
1034b 20 7b 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20   {.    1,       
1034c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65            /* iVe
1034d 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a  rsion */.    siz
1034e 65 6f 66 28 77 69 6e 46 69 6c 65 29 2c 20 20 20  eof(winFile),   
1034f 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20  /* szOsFile */. 
10350 20 20 20 4d 41 58 5f 50 41 54 48 2c 20 20 20 20     MAX_PATH,    
10351 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e        /* mxPathn
10352 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
10353 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10354 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 77   pNext */.    "w
10355 69 6e 33 32 22 2c 20 20 20 20 20 20 20 20 20 20  in32",          
10356 20 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20   /* zName */.   
10357 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
10358 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20      /* pAppData 
10359 2a 2f 0a 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e  */. .    winOpen
1035a 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  ,           /* x
1035b 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44  Open */.    winD
1035c 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20 2f  elete,         /
1035d 2a 20 78 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20  * xDelete */.   
1035e 20 77 69 6e 41 63 63 65 73 73 2c 20 20 20 20 20   winAccess,     
1035f 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a      /* xAccess *
10360 2f 0a 20 20 20 20 77 69 6e 46 75 6c 6c 50 61 74  /.    winFullPat
10361 68 6e 61 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c  hname,   /* xFul
10362 6c 50 61 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20  lPathname */.   
10363 20 77 69 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20   winDlOpen,     
10364 20 20 20 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a      /* xDlOpen *
10365 2f 0a 20 20 20 20 77 69 6e 44 6c 45 72 72 6f 72  /.    winDlError
10366 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 45  ,        /* xDlE
10367 72 72 6f 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44  rror */.    winD
10368 6c 53 79 6d 2c 20 20 20 20 20 20 20 20 20 20 2f  lSym,          /
10369 2a 20 78 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20  * xDlSym */.    
1036a 77 69 6e 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20  winDlClose,     
1036b 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a     /* xDlClose *
1036c 2f 0a 20 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e  /.    winRandomn
1036d 65 73 73 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e  ess,     /* xRan
1036e 64 6f 6d 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77  domness */.    w
1036f 69 6e 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20  inSleep,        
10370 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20    /* xSleep */. 
10371 20 20 20 77 69 6e 43 75 72 72 65 6e 74 54 69 6d     winCurrentTim
10372 65 2c 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e  e,    /* xCurren
10373 74 54 69 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e  tTime */.    win
10374 47 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20  GetLastError    
10375 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72  /* xGetLastError
10376 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 73 71 6c 69   */.  };..  sqli
10377 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72  te3_vfs_register
10378 28 26 77 69 6e 56 66 73 2c 20 31 29 3b 0a 20 20  (&winVfs, 1);.  
10379 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1037a 3b 20 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  ; .}.SQLITE_API 
1037b 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65  int sqlite3_os_e
1037c 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65 74  nd(void){ .  ret
1037d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1037e 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  ..#endif /* SQLI
1037f 54 45 5f 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a  TE_OS_WIN */../*
10380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
10381 64 20 6f 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a  d of os_win.c **
10382 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10383 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10384 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10385 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
10386 67 69 6e 20 66 69 6c 65 20 62 69 74 76 65 63 2e  gin file bitvec.
10387 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
10388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10389 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1038a 0a 2a 2a 20 32 30 30 38 20 46 65 62 72 75 61 72  .** 2008 Februar
1038b 79 20 31 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 16.**.** The a
1038c 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
1038d 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
1038e 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
1038f 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
10390 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
10391 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
10392 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
10393 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
10394 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
10395 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
10396 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
10397 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
10398 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
10399 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
1039a 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
1039b 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
1039c 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
1039d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1039e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1039f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
103a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
103a1 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
103a2 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f   implements an o
103a3 62 6a 65 63 74 20 74 68 61 74 20 72 65 70 72 65  bject that repre
103a4 73 65 6e 74 73 20 61 20 66 69 78 65 64 2d 6c 65  sents a fixed-le
103a5 6e 67 74 68 0a 2a 2a 20 62 69 74 6d 61 70 2e 20  ngth.** bitmap. 
103a6 20 42 69 74 73 20 61 72 65 20 6e 75 6d 62 65 72   Bits are number
103a7 65 64 20 73 74 61 72 74 69 6e 67 20 77 69 74 68  ed starting with
103a8 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 20 62 69 74 6d   1..**.** A bitm
103a9 61 70 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ap is used to re
103aa 63 6f 72 64 20 77 68 69 63 68 20 70 61 67 65 73  cord which pages
103ab 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66   of a database f
103ac 69 6c 65 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a  ile have been.**
103ad 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 64 75 72 69   journalled duri
103ae 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ng a transaction
103af 2c 20 6f 72 20 77 68 69 63 68 20 70 61 67 65 73  , or which pages
103b0 20 68 61 76 65 20 74 68 65 20 22 64 6f 6e 74 2d   have the "dont-
103b1 77 72 69 74 65 22 0a 2a 2a 20 70 72 6f 70 65 72  write".** proper
103b2 74 79 2e 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c  ty.  Usually onl
103b3 79 20 61 20 66 65 77 20 70 61 67 65 73 20 61 72  y a few pages ar
103b4 65 20 6d 65 65 74 20 65 69 74 68 65 72 20 63 6f  e meet either co
103b5 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 20 74  ndition..** So t
103b6 68 65 20 62 69 74 6d 61 70 20 69 73 20 75 73 75  he bitmap is usu
103b7 61 6c 6c 79 20 73 70 61 72 73 65 20 61 6e 64 20  ally sparse and 
103b8 68 61 73 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c  has low cardinal
103b9 69 74 79 2e 0a 2a 2a 20 42 75 74 20 73 6f 6d 65  ity..** But some
103ba 74 69 6d 65 73 20 28 66 6f 72 20 65 78 61 6d 70  times (for examp
103bb 6c 65 20 77 68 65 6e 20 64 75 72 69 6e 67 20 61  le when during a
103bc 20 44 52 4f 50 20 6f 66 20 61 20 6c 61 72 67 65   DROP of a large
103bd 20 74 61 62 6c 65 29 20 6d 6f 73 74 0a 2a 2a 20   table) most.** 
103be 6f 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 70 61  or all of the pa
103bf 67 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73  ges in a databas
103c0 65 20 63 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61  e can get journa
103c1 6c 6c 65 64 2e 20 20 49 6e 20 74 68 6f 73 65 20  lled.  In those 
103c2 63 61 73 65 73 2c 20 0a 2a 2a 20 74 68 65 20 62  cases, .** the b
103c3 69 74 6d 61 70 20 62 65 63 6f 6d 65 73 20 64 65  itmap becomes de
103c4 6e 73 65 20 77 69 74 68 20 68 69 67 68 20 63 61  nse with high ca
103c5 72 64 69 6e 61 6c 69 74 79 2e 20 20 54 68 65 20  rdinality.  The 
103c6 61 6c 67 6f 72 69 74 68 6d 20 6e 65 65 64 73 20  algorithm needs 
103c7 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 62 6f  .** to handle bo
103c8 74 68 20 63 61 73 65 73 20 77 65 6c 6c 2e 0a 2a  th cases well..*
103c9 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66  *.** The size of
103ca 20 74 68 65 20 62 69 74 6d 61 70 20 69 73 20 66   the bitmap is f
103cb 69 78 65 64 20 77 68 65 6e 20 74 68 65 20 6f 62  ixed when the ob
103cc 6a 65 63 74 20 69 73 20 63 72 65 61 74 65 64 2e  ject is created.
103cd 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 62 69 74 73 20  .**.** All bits 
103ce 61 72 65 20 63 6c 65 61 72 20 77 68 65 6e 20 74  are clear when t
103cf 68 65 20 62 69 74 6d 61 70 20 69 73 20 63 72 65  he bitmap is cre
103d0 61 74 65 64 2e 20 20 49 6e 64 69 76 69 64 75 61  ated.  Individua
103d1 6c 20 62 69 74 73 0a 2a 2a 20 6d 61 79 20 62 65  l bits.** may be
103d2 20 73 65 74 20 6f 72 20 63 6c 65 61 72 65 64 20   set or cleared 
103d3 6f 6e 65 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a  one at a time..*
103d4 2a 0a 2a 2a 20 54 65 73 74 20 6f 70 65 72 61 74  *.** Test operat
103d5 69 6f 6e 73 20 61 72 65 20 61 62 6f 75 74 20 31  ions are about 1
103d6 30 30 20 74 69 6d 65 73 20 6d 6f 72 65 20 63 6f  00 times more co
103d7 6d 6d 6f 6e 20 74 68 61 74 20 73 65 74 20 6f 70  mmon that set op
103d8 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 20 43 6c 65  erations..** Cle
103d9 61 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72  ar operations ar
103da 65 20 65 78 63 65 65 64 69 6e 67 6c 79 20 72 61  e exceedingly ra
103db 72 65 2e 20 20 54 68 65 72 65 20 61 72 65 20 75  re.  There are u
103dc 73 75 61 6c 6c 79 20 62 65 74 77 65 65 6e 0a 2a  sually between.*
103dd 2a 20 35 20 61 6e 64 20 35 30 30 20 73 65 74 20  * 5 and 500 set 
103de 6f 70 65 72 61 74 69 6f 6e 73 20 70 65 72 20 42  operations per B
103df 69 74 76 65 63 20 6f 62 6a 65 63 74 2c 20 74 68  itvec object, th
103e0 6f 75 67 68 20 74 68 65 20 6e 75 6d 62 65 72 20  ough the number 
103e1 6f 66 20 73 65 74 73 20 63 61 6e 0a 2a 2a 20 73  of sets can.** s
103e2 6f 6d 65 74 69 6d 65 73 20 67 72 6f 77 20 69 6e  ometimes grow in
103e3 74 6f 20 74 65 6e 73 20 6f 66 20 74 68 6f 75 73  to tens of thous
103e4 61 6e 64 73 20 6f 72 20 6c 61 72 67 65 72 2e 20  ands or larger. 
103e5 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   The size of the
103e6 0a 2a 2a 20 42 69 74 76 65 63 20 6f 62 6a 65 63  .** Bitvec objec
103e7 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
103e8 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
103e9 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 74  database file at
103ea 20 74 68 65 0a 2a 2a 20 73 74 61 72 74 20 6f 66   the.** start of
103eb 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20   a transaction, 
103ec 61 6e 64 20 69 73 20 74 68 75 73 20 75 73 75 61  and is thus usua
103ed 6c 6c 79 20 6c 65 73 73 20 74 68 61 6e 20 61 20  lly less than a 
103ee 66 65 77 20 74 68 6f 75 73 61 6e 64 2c 0a 2a 2a  few thousand,.**
103ef 20 62 75 74 20 63 61 6e 20 62 65 20 61 73 20 6c   but can be as l
103f0 61 72 67 65 20 61 73 20 32 20 62 69 6c 6c 69 6f  arge as 2 billio
103f1 6e 20 66 6f 72 20 61 20 72 65 61 6c 6c 79 20 62  n for a really b
103f2 69 67 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a  ig database..*/.
103f3 0a 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20  ./* Size of the 
103f4 42 69 74 76 65 63 20 73 74 72 75 63 74 75 72 65  Bitvec structure
103f5 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 23 64   in bytes. */.#d
103f6 65 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a 20  efine BITVEC_SZ 
103f7 20 20 20 20 20 20 20 28 73 69 7a 65 6f 66 28 76         (sizeof(v
103f8 6f 69 64 2a 29 2a 31 32 38 29 20 20 2f 2a 20 35  oid*)*128)  /* 5
103f9 31 32 20 6f 6e 20 33 32 62 69 74 2e 20 20 31 30  12 on 32bit.  10
103fa 32 34 20 6f 6e 20 36 34 62 69 74 20 2a 2f 0a 0a  24 on 64bit */..
103fb 2f 2a 20 52 6f 75 6e 64 20 74 68 65 20 75 6e 69  /* Round the uni
103fc 6f 6e 20 73 69 7a 65 20 64 6f 77 6e 20 74 6f 20  on size down to 
103fd 74 68 65 20 6e 65 61 72 65 73 74 20 70 6f 69 6e  the nearest poin
103fe 74 65 72 20 62 6f 75 6e 64 61 72 79 2c 20 73 69  ter boundary, si
103ff 6e 63 65 20 74 68 61 74 27 73 20 68 6f 77 20 0a  nce that's how .
10400 2a 2a 20 69 74 20 77 69 6c 6c 20 62 65 20 61 6c  ** it will be al
10401 69 67 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65  igned within the
10402 20 42 69 74 76 65 63 20 73 74 72 75 63 74 2e 20   Bitvec struct. 
10403 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45  */.#define BITVE
10404 43 5f 55 53 49 5a 45 20 20 20 20 20 28 28 28 42  C_USIZE     (((B
10405 49 54 56 45 43 5f 53 5a 2d 28 33 2a 73 69 7a 65  ITVEC_SZ-(3*size
10406 6f 66 28 75 33 32 29 29 29 2f 73 69 7a 65 6f 66  of(u32)))/sizeof
10407 28 42 69 74 76 65 63 2a 29 29 2a 73 69 7a 65 6f  (Bitvec*))*sizeo
10408 66 28 42 69 74 76 65 63 2a 29 29 0a 0a 2f 2a 20  f(Bitvec*))../* 
10409 54 79 70 65 20 6f 66 20 74 68 65 20 61 72 72 61  Type of the arra
1040a 79 20 22 65 6c 65 6d 65 6e 74 22 20 66 6f 72 20  y "element" for 
1040b 74 68 65 20 62 69 74 6d 61 70 20 72 65 70 72 65  the bitmap repre
1040c 73 65 6e 74 61 74 69 6f 6e 2e 20 0a 2a 2a 20 53  sentation. .** S
1040d 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 77 65 72  hould be a power
1040e 20 6f 66 20 32 2c 20 61 6e 64 20 69 64 65 61 6c   of 2, and ideal
1040f 6c 79 2c 20 65 76 65 6e 6c 79 20 64 69 76 69 64  ly, evenly divid
10410 65 20 69 6e 74 6f 20 42 49 54 56 45 43 5f 55 53  e into BITVEC_US
10411 49 5a 45 2e 20 0a 2a 2a 20 53 65 74 74 69 6e 67  IZE. .** Setting
10412 20 74 68 69 73 20 74 6f 20 74 68 65 20 22 6e 61   this to the "na
10413 74 75 72 61 6c 20 77 6f 72 64 22 20 73 69 7a 65  tural word" size
10414 20 6f 66 20 79 6f 75 72 20 43 50 55 20 6d 61 79   of your CPU may
10415 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 70 65 72 66   improve.** perf
10416 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a 23 64 65 66  ormance. */.#def
10417 69 6e 65 20 42 49 54 56 45 43 5f 54 45 4c 45 4d  ine BITVEC_TELEM
10418 20 20 20 20 20 75 38 0a 2f 2a 20 53 69 7a 65 2c       u8./* Size,
10419 20 69 6e 20 62 69 74 73 2c 20 6f 66 20 74 68 65   in bits, of the
1041a 20 62 69 74 6d 61 70 20 65 6c 65 6d 65 6e 74 2e   bitmap element.
1041b 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56   */.#define BITV
1041c 45 43 5f 53 5a 45 4c 45 4d 20 20 20 20 38 0a 2f  EC_SZELEM    8./
1041d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  * Number of elem
1041e 65 6e 74 73 20 69 6e 20 61 20 62 69 74 6d 61 70  ents in a bitmap
1041f 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65 66 69   array. */.#defi
10420 6e 65 20 42 49 54 56 45 43 5f 4e 45 4c 45 4d 20  ne BITVEC_NELEM 
10421 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a      (BITVEC_USIZ
10422 45 2f 73 69 7a 65 6f 66 28 42 49 54 56 45 43 5f  E/sizeof(BITVEC_
10423 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e 75 6d 62 65  TELEM))./* Numbe
10424 72 20 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65  r of bits in the
10425 20 62 69 74 6d 61 70 20 61 72 72 61 79 2e 20 2a   bitmap array. *
10426 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43  /.#define BITVEC
10427 5f 4e 42 49 54 20 20 20 20 20 20 28 42 49 54 56  _NBIT      (BITV
10428 45 43 5f 4e 45 4c 45 4d 2a 42 49 54 56 45 43 5f  EC_NELEM*BITVEC_
10429 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20 4e 75 6d 62  SZELEM)../* Numb
1042a 65 72 20 6f 66 20 75 33 32 20 76 61 6c 75 65 73  er of u32 values
1042b 20 69 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20   in hash table. 
1042c 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45  */.#define BITVE
1042d 43 5f 4e 49 4e 54 20 20 20 20 20 20 28 42 49 54  C_NINT      (BIT
1042e 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66  VEC_USIZE/sizeof
1042f 28 75 33 32 29 29 0a 2f 2a 20 4d 61 78 69 6d 75  (u32))./* Maximu
10430 6d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  m number of entr
10431 69 65 73 20 69 6e 20 68 61 73 68 20 74 61 62 6c  ies in hash tabl
10432 65 20 62 65 66 6f 72 65 20 0a 2a 2a 20 73 75 62  e before .** sub
10433 2d 64 69 76 69 64 69 6e 67 20 61 6e 64 20 72 65  -dividing and re
10434 2d 68 61 73 68 69 6e 67 2e 20 2a 2f 0a 23 64 65  -hashing. */.#de
10435 66 69 6e 65 20 42 49 54 56 45 43 5f 4d 58 48 41  fine BITVEC_MXHA
10436 53 48 20 20 20 20 28 42 49 54 56 45 43 5f 4e 49  SH    (BITVEC_NI
10437 4e 54 2f 32 29 0a 2f 2a 20 48 61 73 68 69 6e 67  NT/2)./* Hashing
10438 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68   function for th
10439 65 20 61 48 61 73 68 20 72 65 70 72 65 73 65 6e  e aHash represen
1043a 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45 6d 70 69 72  tation..** Empir
1043b 69 63 61 6c 20 74 65 73 74 69 6e 67 20 73 68 6f  ical testing sho
1043c 77 65 64 20 74 68 61 74 20 74 68 65 20 2a 33 37  wed that the *37
1043d 20 6d 75 6c 74 69 70 6c 69 65 72 20 0a 2a 2a 20   multiplier .** 
1043e 28 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 72  (an arbitrary pr
1043f 69 6d 65 29 69 6e 20 74 68 65 20 68 61 73 68 20  ime)in the hash 
10440 66 75 6e 63 74 69 6f 6e 20 70 72 6f 76 69 64 65  function provide
10441 64 20 0a 2a 2a 20 6e 6f 20 66 65 77 65 72 20 63  d .** no fewer c
10442 6f 6c 6c 69 73 69 6f 6e 73 20 74 68 61 6e 20 74  ollisions than t
10443 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e 20 2a 2f 0a  he no-op *1. */.
10444 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 48  #define BITVEC_H
10445 41 53 48 28 58 29 20 20 20 28 28 28 58 29 2a 31  ASH(X)   (((X)*1
10446 29 25 42 49 54 56 45 43 5f 4e 49 4e 54 29 0a 0a  )%BITVEC_NINT)..
10447 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e  #define BITVEC_N
10448 50 54 52 20 20 20 20 20 20 28 42 49 54 56 45 43  PTR      (BITVEC
10449 5f 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 69  _USIZE/sizeof(Bi
1044a 74 76 65 63 20 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a  tvec *)).../*.**
1044b 20 41 20 62 69 74 6d 61 70 20 69 73 20 61 6e 20   A bitmap is an 
1044c 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
1044d 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
1044e 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ure..**.** This 
1044f 62 69 74 6d 61 70 20 72 65 63 6f 72 64 73 20 74  bitmap records t
10450 68 65 20 65 78 69 73 74 61 6e 63 65 20 6f 66 20  he existance of 
10451 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 62 69 74  zero or more bit
10452 73 0a 2a 2a 20 77 69 74 68 20 76 61 6c 75 65 73  s.** with values
10453 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69   between 1 and i
10454 53 69 7a 65 2c 20 69 6e 63 6c 75 73 69 76 65 2e  Size, inclusive.
10455 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
10456 20 74 68 72 65 65 20 70 6f 73 73 69 62 6c 65 20   three possible 
10457 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20  representations 
10458 6f 66 20 74 68 65 20 62 69 74 6d 61 70 2e 0a 2a  of the bitmap..*
10459 2a 20 49 66 20 69 53 69 7a 65 3c 3d 42 49 54 56  * If iSize<=BITV
1045a 45 43 5f 4e 42 49 54 2c 20 74 68 65 6e 20 42 69  EC_NBIT, then Bi
1045b 74 76 65 63 2e 75 2e 61 42 69 74 6d 61 70 5b 5d  tvec.u.aBitmap[]
1045c 20 69 73 20 61 20 73 74 72 61 69 67 68 74 0a 2a   is a straight.*
1045d 2a 20 62 69 74 6d 61 70 2e 20 20 54 68 65 20 6c  * bitmap.  The l
1045e 65 61 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74  east significant
1045f 20 62 69 74 20 69 73 20 62 69 74 20 31 2e 0a 2a   bit is bit 1..*
10460 2a 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3e 42 49  *.** If iSize>BI
10461 54 56 45 43 5f 4e 42 49 54 20 61 6e 64 20 69 44  TVEC_NBIT and iD
10462 69 76 69 73 6f 72 3d 3d 30 20 74 68 65 6e 20 42  ivisor==0 then B
10463 69 74 76 65 63 2e 75 2e 61 48 61 73 68 5b 5d 20  itvec.u.aHash[] 
10464 69 73 0a 2a 2a 20 61 20 68 61 73 68 20 74 61 62  is.** a hash tab
10465 6c 65 20 74 68 61 74 20 77 69 6c 6c 20 68 6f 6c  le that will hol
10466 64 20 75 70 20 74 6f 20 42 49 54 56 45 43 5f 4d  d up to BITVEC_M
10467 58 48 41 53 48 20 64 69 73 74 69 6e 63 74 20 76  XHASH distinct v
10468 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  alues..**.** Oth
10469 65 72 77 69 73 65 2c 20 74 68 65 20 76 61 6c 75  erwise, the valu
1046a 65 20 69 20 69 73 20 72 65 64 69 72 65 63 74 65  e i is redirecte
1046b 64 20 69 6e 74 6f 20 6f 6e 65 20 6f 66 20 42 49  d into one of BI
1046c 54 56 45 43 5f 4e 50 54 52 0a 2a 2a 20 73 75 62  TVEC_NPTR.** sub
1046d 2d 62 69 74 6d 61 70 73 20 70 6f 69 6e 74 65 64  -bitmaps pointed
1046e 20 74 6f 20 62 79 20 42 69 74 76 65 63 2e 75 2e   to by Bitvec.u.
1046f 61 70 53 75 62 5b 5d 2e 20 20 45 61 63 68 20 73  apSub[].  Each s
10470 75 62 62 69 74 6d 61 70 0a 2a 2a 20 68 61 6e 64  ubbitmap.** hand
10471 6c 65 73 20 75 70 20 74 6f 20 69 44 69 76 69 73  les up to iDivis
10472 6f 72 20 73 65 70 61 72 61 74 65 20 76 61 6c 75  or separate valu
10473 65 73 20 6f 66 20 69 2e 20 20 61 70 53 75 62 5b  es of i.  apSub[
10474 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20 76 61 6c 75  0] holds.** valu
10475 65 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  es between 1 and
10476 20 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53 75   iDivisor.  apSu
10477 62 5b 31 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65  b[1] holds value
10478 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 69 44 69  s between.** iDi
10479 76 69 73 6f 72 2b 31 20 61 6e 64 20 32 2a 69 44  visor+1 and 2*iD
1047a 69 76 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 4e  ivisor.  apSub[N
1047b 5d 20 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62  ] holds values b
1047c 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a 69 44 69 76  etween.** N*iDiv
1047d 69 73 6f 72 2b 31 20 61 6e 64 20 28 4e 2b 31 29  isor+1 and (N+1)
1047e 2a 69 44 69 76 69 73 6f 72 2e 20 20 45 61 63 68  *iDivisor.  Each
1047f 20 73 75 62 62 69 74 6d 61 70 20 69 73 20 6e 6f   subbitmap is no
10480 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20 74 6f 20 68  rmalized.** to h
10481 6f 6c 64 20 64 65 61 6c 20 77 69 74 68 20 76 61  old deal with va
10482 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61  lues between 1 a
10483 6e 64 20 69 44 69 76 69 73 6f 72 2e 0a 2a 2f 0a  nd iDivisor..*/.
10484 73 74 72 75 63 74 20 42 69 74 76 65 63 20 7b 0a  struct Bitvec {.
10485 20 20 75 33 32 20 69 53 69 7a 65 3b 20 20 20 20    u32 iSize;    
10486 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 62 69 74    /* Maximum bit
10487 20 69 6e 64 65 78 2e 20 20 4d 61 78 20 69 53 69   index.  Max iSi
10488 7a 65 20 69 73 20 34 2c 32 39 34 2c 39 36 37 2c  ze is 4,294,967,
10489 32 39 36 2e 20 2a 2f 0a 20 20 75 33 32 20 6e 53  296. */.  u32 nS
1048a 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  et;       /* Num
1048b 62 65 72 20 6f 66 20 62 69 74 73 20 74 68 61 74  ber of bits that
1048c 20 61 72 65 20 73 65 74 20 2d 20 6f 6e 6c 79 20   are set - only 
1048d 76 61 6c 69 64 20 66 6f 72 20 61 48 61 73 68 0a  valid for aHash.
1048e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1048f 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 2e 20 20 4d    ** element.  M
10490 61 78 20 69 73 20 42 49 54 56 45 43 5f 4e 49 4e  ax is BITVEC_NIN
10491 54 2e 20 20 46 6f 72 20 42 49 54 56 45 43 5f 53  T.  For BITVEC_S
10492 5a 20 6f 66 20 35 31 32 2c 0a 20 20 20 20 20 20  Z of 512,.      
10493 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74              ** t
10494 68 69 73 20 77 6f 75 6c 64 20 62 65 20 31 32 35  his would be 125
10495 2e 20 2a 2f 0a 20 20 75 33 32 20 69 44 69 76 69  . */.  u32 iDivi
10496 73 6f 72 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72  sor;   /* Number
10497 20 6f 66 20 62 69 74 73 20 68 61 6e 64 6c 65 64   of bits handled
10498 20 62 79 20 65 61 63 68 20 61 70 53 75 62 5b 5d   by each apSub[]
10499 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20 20   entry. */.     
1049a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1049b 53 68 6f 75 6c 64 20 3e 3d 30 20 66 6f 72 20 61  Should >=0 for a
1049c 70 53 75 62 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f  pSub element. */
1049d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1049e 20 20 20 2f 2a 20 4d 61 78 20 69 44 69 76 69 73     /* Max iDivis
1049f 6f 72 20 69 73 20 6d 61 78 28 75 33 32 29 20 2f  or is max(u32) /
104a0 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2b 20 31   BITVEC_NPTR + 1
104a1 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  .  */.          
104a2 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61          /* For a
104a3 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35 31   BITVEC_SZ of 51
104a4 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 62 65  2, this would be
104a5 20 33 34 2c 33 35 39 2c 37 33 39 2e 20 2a 2f 0a   34,359,739. */.
104a6 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 42 49    union {.    BI
104a7 54 56 45 43 5f 54 45 4c 45 4d 20 61 42 69 74 6d  TVEC_TELEM aBitm
104a8 61 70 5b 42 49 54 56 45 43 5f 4e 45 4c 45 4d 5d  ap[BITVEC_NELEM]
104a9 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 70 20 72  ;    /* Bitmap r
104aa 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a 2f  epresentation */
104ab 0a 20 20 20 20 75 33 32 20 61 48 61 73 68 5b 42  .    u32 aHash[B
104ac 49 54 56 45 43 5f 4e 49 4e 54 5d 3b 20 20 20 20  ITVEC_NINT];    
104ad 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20    /* Hash table 
104ae 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a  representation *
104af 2f 0a 20 20 20 20 42 69 74 76 65 63 20 2a 61 70  /.    Bitvec *ap
104b0 53 75 62 5b 42 49 54 56 45 43 5f 4e 50 54 52 5d  Sub[BITVEC_NPTR]
104b1 3b 20 20 2f 2a 20 52 65 63 75 72 73 69 76 65 20  ;  /* Recursive 
104b2 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 2a  representation *
104b3 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a  /.  } u;.};../*.
104b4 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
104b5 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 20 61 62  bitmap object ab
104b6 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 62 69 74  le to handle bit
104b7 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20  s between 0 and 
104b8 69 53 69 7a 65 2c 0a 2a 2a 20 69 6e 63 6c 75 73  iSize,.** inclus
104b9 69 76 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70  ive.  Return a p
104ba 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
104bb 77 20 6f 62 6a 65 63 74 2e 20 20 52 65 74 75 72  w object.  Retur
104bc 6e 20 4e 55 4c 4c 20 69 66 20 0a 2a 2a 20 6d 61  n NULL if .** ma
104bd 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53  lloc fails..*/.S
104be 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 42 69  QLITE_PRIVATE Bi
104bf 74 76 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74  tvec *sqlite3Bit
104c0 76 65 63 43 72 65 61 74 65 28 75 33 32 20 69 53  vecCreate(u32 iS
104c1 69 7a 65 29 7b 0a 20 20 42 69 74 76 65 63 20 2a  ize){.  Bitvec *
104c2 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  p;.  assert( siz
104c3 65 6f 66 28 2a 70 29 3d 3d 42 49 54 56 45 43 5f  eof(*p)==BITVEC_
104c4 53 5a 20 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69  SZ );.  p = sqli
104c5 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
104c6 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69  izeof(*p) );.  i
104c7 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e 69  f( p ){.    p->i
104c8 53 69 7a 65 20 3d 20 69 53 69 7a 65 3b 0a 20 20  Size = iSize;.  
104c9 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
104ca 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
104cb 73 65 65 20 69 66 20 74 68 65 20 69 2d 74 68 20  see if the i-th 
104cc 62 69 74 20 69 73 20 73 65 74 2e 20 20 52 65 74  bit is set.  Ret
104cd 75 72 6e 20 74 72 75 65 20 6f 72 20 66 61 6c 73  urn true or fals
104ce 65 2e 0a 2a 2a 20 49 66 20 70 20 69 73 20 4e 55  e..** If p is NU
104cf 4c 4c 20 28 69 66 20 74 68 65 20 62 69 74 6d 61  LL (if the bitma
104d0 70 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 63  p has not been c
104d1 72 65 61 74 65 64 29 20 6f 72 20 69 66 0a 2a 2a  reated) or if.**
104d2 20 69 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e   i is out of ran
104d3 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  ge, then return 
104d4 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  false..*/.SQLITE
104d5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
104d6 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42  ite3BitvecTest(B
104d7 69 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29  itvec *p, u32 i)
104d8 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  {.  if( p==0 ) r
104d9 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 69  eturn 0;.  if( i
104da 3e 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 69 3d 3d  >p->iSize || i==
104db 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
104dc 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d  i--;.  while( p-
104dd 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20  >iDivisor ){.   
104de 20 75 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e   u32 bin = i/p->
104df 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20  iDivisor;.    i 
104e0 3d 20 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b  = i%p->iDivisor;
104e1 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70  .    p = p->u.ap
104e2 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66  Sub[bin];.    if
104e3 20 28 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65   (!p) {.      re
104e4 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
104e5 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69 7a 65  }.  if( p->iSize
104e6 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20 29 7b  <=BITVEC_NBIT ){
104e7 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 2d 3e  .    return (p->
104e8 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56  u.aBitmap[i/BITV
104e9 45 43 5f 53 5a 45 4c 45 4d 5d 20 26 20 28 31 3c  EC_SZELEM] & (1<
104ea 3c 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c  <(i&(BITVEC_SZEL
104eb 45 4d 2d 31 29 29 29 29 21 3d 30 3b 0a 20 20 7d  EM-1))))!=0;.  }
104ec 20 65 6c 73 65 7b 0a 20 20 20 20 75 33 32 20 68   else{.    u32 h
104ed 20 3d 20 42 49 54 56 45 43 5f 48 41 53 48 28 69   = BITVEC_HASH(i
104ee 2b 2b 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ++);.    while( 
104ef 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b  p->u.aHash[h] ){
104f0 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e  .      if( p->u.
104f1 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65  aHash[h]==i ) re
104f2 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 68 20  turn 1;.      h 
104f3 3d 20 28 68 2b 31 29 20 25 20 42 49 54 56 45 43  = (h+1) % BITVEC
104f4 5f 4e 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 20  _NINT;.    }.   
104f5 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d   return 0;.  }.}
104f6 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
104f7 69 2d 74 68 20 62 69 74 2e 20 20 52 65 74 75 72  i-th bit.  Retur
104f8 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61  n 0 on success a
104f9 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  nd an error code
104fa 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20   if.** anything 
104fb 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a  goes wrong..**.*
104fc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  * This routine m
104fd 69 67 68 74 20 63 61 75 73 65 20 73 75 62 2d 62  ight cause sub-b
104fe 69 74 6d 61 70 73 20 74 6f 20 62 65 20 61 6c 6c  itmaps to be all
104ff 6f 63 61 74 65 64 2e 20 20 46 61 69 6c 69 6e 67  ocated.  Failing
10500 0a 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 6d  .** to get the m
10501 65 6d 6f 72 79 20 6e 65 65 64 65 64 20 74 6f 20  emory needed to 
10502 68 6f 6c 64 20 74 68 65 20 73 75 62 2d 62 69 74  hold the sub-bit
10503 6d 61 70 20 69 73 20 74 68 65 20 6f 6e 6c 79 0a  map is the only.
10504 2a 2a 20 74 68 61 74 20 63 61 6e 20 67 6f 20 77  ** that can go w
10505 72 6f 6e 67 20 77 69 74 68 20 61 6e 20 69 6e 73  rong with an ins
10506 65 72 74 2c 20 61 73 73 75 6d 69 6e 67 20 70 20  ert, assuming p 
10507 61 6e 64 20 69 20 61 72 65 20 76 61 6c 69 64 2e  and i are valid.
10508 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69  .**.** The calli
10509 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  ng function must
1050a 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 20 69   ensure that p i
1050b 73 20 61 20 76 61 6c 69 64 20 42 69 74 76 65 63  s a valid Bitvec
1050c 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 74   object.** and t
1050d 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 66 6f  hat the value fo
1050e 72 20 22 69 22 20 69 73 20 77 69 74 68 69 6e 20  r "i" is within 
1050f 72 61 6e 67 65 20 6f 66 20 74 68 65 20 42 69 74  range of the Bit
10510 76 65 63 20 6f 62 6a 65 63 74 2e 0a 2a 2a 20 4f  vec object..** O
10511 74 68 65 72 77 69 73 65 20 74 68 65 20 62 65 68  therwise the beh
10512 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
10513 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
10514 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
10515 33 42 69 74 76 65 63 53 65 74 28 42 69 74 76 65  3BitvecSet(Bitve
10516 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20  c *p, u32 i){.  
10517 75 33 32 20 68 3b 0a 20 20 69 66 28 20 70 3d 3d  u32 h;.  if( p==
10518 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
10519 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
1051a 69 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  i>0 );.  assert(
1051b 20 69 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a   i<=p->iSize );.
1051c 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65 28 28    i--;.  while((
1051d 70 2d 3e 69 53 69 7a 65 20 3e 20 42 49 54 56 45  p->iSize > BITVE
1051e 43 5f 4e 42 49 54 29 20 26 26 20 70 2d 3e 69 44  C_NBIT) && p->iD
1051f 69 76 69 73 6f 72 29 20 7b 0a 20 20 20 20 75 33  ivisor) {.    u3
10520 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69  2 bin = i/p->iDi
10521 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69  visor;.    i = i
10522 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20  %p->iDivisor;.  
10523 20 20 69 66 28 20 70 2d 3e 75 2e 61 70 53 75 62    if( p->u.apSub
10524 5b 62 69 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20 20  [bin]==0 ){.    
10525 20 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e    p->u.apSub[bin
10526 5d 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  ] = sqlite3Bitve
10527 63 43 72 65 61 74 65 28 20 70 2d 3e 69 44 69 76  cCreate( p->iDiv
10528 69 73 6f 72 20 29 3b 0a 20 20 20 20 20 20 69 66  isor );.      if
10529 28 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e  ( p->u.apSub[bin
1052a 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  ]==0 ) return SQ
1052b 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
1052c 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61  }.    p = p->u.a
1052d 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20 7d 0a 20  pSub[bin];.  }. 
1052e 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42   if( p->iSize<=B
1052f 49 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20  ITVEC_NBIT ){.  
10530 20 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69    p->u.aBitmap[i
10531 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20  /BITVEC_SZELEM] 
10532 7c 3d 20 31 20 3c 3c 20 28 69 26 28 42 49 54 56  |= 1 << (i&(BITV
10533 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 3b 0a 20  EC_SZELEM-1));. 
10534 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
10535 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 68 20 3d 20 42  _OK;.  }.  h = B
10536 49 54 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b  ITVEC_HASH(i++);
10537 0a 20 20 2f 2a 20 69 66 20 74 68 65 72 65 20 77  .  /* if there w
10538 61 73 6e 27 74 20 61 20 68 61 73 68 20 63 6f 6c  asn't a hash col
10539 6c 69 73 69 6f 6e 2c 20 61 6e 64 20 74 68 69 73  lision, and this
1053a 20 64 6f 65 73 6e 27 74 20 2a 2f 0a 20 20 2f 2a   doesn't */.  /*
1053b 20 63 6f 6d 70 6c 65 74 65 6c 79 20 66 69 6c 6c   completely fill
1053c 20 74 68 65 20 68 61 73 68 2c 20 74 68 65 6e 20   the hash, then 
1053d 6a 75 73 74 20 61 64 64 20 69 74 20 77 69 74 68  just add it with
1053e 6f 75 74 20 2a 2f 0a 20 20 2f 2a 20 77 6f 72 72  out */.  /* worr
1053f 69 6e 67 20 61 62 6f 75 74 20 73 75 62 2d 64 69  ing about sub-di
10540 76 69 64 69 6e 67 20 61 6e 64 20 72 65 2d 68 61  viding and re-ha
10541 73 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20  shing. */.  if( 
10542 21 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 29  !p->u.aHash[h] )
10543 7b 0a 20 20 20 20 69 66 20 28 70 2d 3e 6e 53 65  {.    if (p->nSe
10544 74 3c 28 42 49 54 56 45 43 5f 4e 49 4e 54 2d 31  t<(BITVEC_NINT-1
10545 29 29 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  )) {.      goto 
10546 62 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3b 0a  bitvec_set_end;.
10547 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
10548 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73     goto bitvec_s
10549 65 74 5f 72 65 68 61 73 68 3b 0a 20 20 20 20 7d  et_rehash;.    }
1054a 0a 20 20 7d 0a 20 20 2f 2a 20 74 68 65 72 65 20  .  }.  /* there 
1054b 77 61 73 20 61 20 63 6f 6c 6c 69 73 69 6f 6e 2c  was a collision,
1054c 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   check to see if
1054d 20 69 74 27 73 20 61 6c 72 65 61 64 79 20 2a 2f   it's already */
1054e 0a 20 20 2f 2a 20 69 6e 20 68 61 73 68 2c 20 69  .  /* in hash, i
1054f 66 20 6e 6f 74 2c 20 74 72 79 20 74 6f 20 66 69  f not, try to fi
10550 6e 64 20 61 20 73 70 6f 74 20 66 6f 72 20 69 74  nd a spot for it
10551 20 2a 2f 0a 20 20 64 6f 20 7b 0a 20 20 20 20 69   */.  do {.    i
10552 66 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d  f( p->u.aHash[h]
10553 3d 3d 69 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==i ) return SQL
10554 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 68 2b 2b 3b  ITE_OK;.    h++;
10555 0a 20 20 20 20 69 66 28 20 68 3e 3d 42 49 54 56  .    if( h>=BITV
10556 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b  EC_NINT ) h = 0;
10557 0a 20 20 7d 20 77 68 69 6c 65 28 20 70 2d 3e 75  .  } while( p->u
10558 2e 61 48 61 73 68 5b 68 5d 20 29 3b 0a 20 20 2f  .aHash[h] );.  /
10559 2a 20 77 65 20 64 69 64 6e 27 74 20 66 69 6e 64  * we didn't find
1055a 20 69 74 20 69 6e 20 74 68 65 20 68 61 73 68 2e   it in the hash.
1055b 20 20 68 20 70 6f 69 6e 74 73 20 74 6f 20 74 68    h points to th
1055c 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 2f 2a 20  e first */.  /* 
1055d 61 76 61 69 6c 61 62 6c 65 20 66 72 65 65 20 73  available free s
1055e 70 6f 74 2e 20 63 68 65 63 6b 20 74 6f 20 73 65  pot. check to se
1055f 65 20 69 66 20 74 68 69 73 20 69 73 20 67 6f 69  e if this is goi
10560 6e 67 20 74 6f 20 2a 2f 0a 20 20 2f 2a 20 6d 61  ng to */.  /* ma
10561 6b 65 20 6f 75 72 20 68 61 73 68 20 74 6f 6f 20  ke our hash too 
10562 22 66 75 6c 6c 22 2e 20 20 2a 2f 0a 62 69 74 76  "full".  */.bitv
10563 65 63 5f 73 65 74 5f 72 65 68 61 73 68 3a 0a 20  ec_set_rehash:. 
10564 20 69 66 28 20 70 2d 3e 6e 53 65 74 3e 3d 42 49   if( p->nSet>=BI
10565 54 56 45 43 5f 4d 58 48 41 53 48 20 29 7b 0a 20  TVEC_MXHASH ){. 
10566 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
10567 6a 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  j;.    int rc;. 
10568 20 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73     u32 *aiValues
10569 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41   = sqlite3StackA
1056a 6c 6c 6f 63 52 61 77 28 30 2c 20 73 69 7a 65 6f  llocRaw(0, sizeo
1056b 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a  f(p->u.aHash));.
1056c 20 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73      if( aiValues
1056d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
1056e 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1056f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
10570 20 20 20 6d 65 6d 63 70 79 28 61 69 56 61 6c 75     memcpy(aiValu
10571 65 73 2c 20 70 2d 3e 75 2e 61 48 61 73 68 2c 20  es, p->u.aHash, 
10572 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 48 61 73  sizeof(p->u.aHas
10573 68 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65  h));.      memse
10574 74 28 70 2d 3e 75 2e 61 70 53 75 62 2c 20 30 2c  t(p->u.apSub, 0,
10575 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61 70 53   sizeof(p->u.apS
10576 75 62 29 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69  ub));.      p->i
10577 44 69 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53  Divisor = (p->iS
10578 69 7a 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54  ize + BITVEC_NPT
10579 52 20 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50  R - 1)/BITVEC_NP
1057a 54 52 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  TR;.      rc = s
1057b 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
1057c 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 66 6f 72  p, i);.      for
1057d 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e  (j=0; j<BITVEC_N
1057e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  INT; j++){.     
1057f 20 20 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b     if( aiValues[
10580 6a 5d 20 29 20 72 63 20 7c 3d 20 73 71 6c 69 74  j] ) rc |= sqlit
10581 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 61  e3BitvecSet(p, a
10582 69 56 61 6c 75 65 73 5b 6a 5d 29 3b 0a 20 20 20  iValues[j]);.   
10583 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
10584 65 33 53 74 61 63 6b 46 72 65 65 28 30 2c 20 61  e3StackFree(0, a
10585 69 56 61 6c 75 65 73 29 3b 0a 20 20 20 20 20 20  iValues);.      
10586 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
10587 0a 20 20 7d 0a 62 69 74 76 65 63 5f 73 65 74 5f  .  }.bitvec_set_
10588 65 6e 64 3a 0a 20 20 70 2d 3e 6e 53 65 74 2b 2b  end:.  p->nSet++
10589 3b 0a 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68  ;.  p->u.aHash[h
1058a 5d 20 3d 20 69 3b 0a 20 20 72 65 74 75 72 6e 20  ] = i;.  return 
1058b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
1058c 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 69 2d  .** Clear the i-
1058d 74 68 20 62 69 74 2e 0a 2a 2a 0a 2a 2a 20 70 42  th bit..**.** pB
1058e 75 66 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69  uf must be a poi
1058f 6e 74 65 72 20 74 6f 20 61 74 20 6c 65 61 73 74  nter to at least
10590 20 42 49 54 56 45 43 5f 53 5a 20 62 79 74 65 73   BITVEC_SZ bytes
10591 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 73 74   of temporary st
10592 6f 72 61 67 65 0a 2a 2a 20 74 68 61 74 20 42 69  orage.** that Bi
10593 74 76 65 63 43 6c 65 61 72 20 63 61 6e 20 75 73  tvecClear can us
10594 65 20 74 6f 20 72 65 62 75 69 6c 74 20 69 74 73  e to rebuilt its
10595 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a   hash table..*/.
10596 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10597 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76 65  oid sqlite3Bitve
10598 63 43 6c 65 61 72 28 42 69 74 76 65 63 20 2a 70  cClear(Bitvec *p
10599 2c 20 75 33 32 20 69 2c 20 76 6f 69 64 20 2a 70  , u32 i, void *p
1059a 42 75 66 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30  Buf){.  if( p==0
1059b 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
1059c 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 69 2d  ert( i>0 );.  i-
1059d 2d 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e 69  -;.  while( p->i
1059e 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75  Divisor ){.    u
1059f 33 32 20 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44  32 bin = i/p->iD
105a0 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20  ivisor;.    i = 
105a1 69 25 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20  i%p->iDivisor;. 
105a2 20 20 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75     p = p->u.apSu
105a3 62 5b 62 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28  b[bin];.    if (
105a4 21 70 29 20 7b 0a 20 20 20 20 20 20 72 65 74 75  !p) {.      retu
105a5 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rn;.    }.  }.  
105a6 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49  if( p->iSize<=BI
105a7 54 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20  TVEC_NBIT ){.   
105a8 20 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69 2f   p->u.aBitmap[i/
105a9 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20 26  BITVEC_SZELEM] &
105aa 3d 20 7e 28 31 20 3c 3c 20 28 69 26 28 42 49 54  = ~(1 << (i&(BIT
105ab 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 3b  VEC_SZELEM-1)));
105ac 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e  .  }else{.    un
105ad 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20  signed int j;.  
105ae 20 20 75 33 32 20 2a 61 69 56 61 6c 75 65 73 20    u32 *aiValues 
105af 3d 20 70 42 75 66 3b 0a 20 20 20 20 6d 65 6d 63  = pBuf;.    memc
105b0 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e  py(aiValues, p->
105b1 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28  u.aHash, sizeof(
105b2 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20  p->u.aHash));.  
105b3 20 20 6d 65 6d 73 65 74 28 70 2d 3e 75 2e 61 48    memset(p->u.aH
105b4 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70  ash, 0, sizeof(p
105b5 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20  ->u.aHash));.   
105b6 20 70 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20   p->nSet = 0;.  
105b7 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 42 49 54    for(j=0; j<BIT
105b8 56 45 43 5f 4e 49 4e 54 3b 20 6a 2b 2b 29 7b 0a  VEC_NINT; j++){.
105b9 20 20 20 20 20 20 69 66 28 20 61 69 56 61 6c 75        if( aiValu
105ba 65 73 5b 6a 5d 20 26 26 20 61 69 56 61 6c 75 65  es[j] && aiValue
105bb 73 5b 6a 5d 21 3d 28 69 2b 31 29 20 29 7b 0a 20  s[j]!=(i+1) ){. 
105bc 20 20 20 20 20 20 20 75 33 32 20 68 20 3d 20 42         u32 h = B
105bd 49 54 56 45 43 5f 48 41 53 48 28 61 69 56 61 6c  ITVEC_HASH(aiVal
105be 75 65 73 5b 6a 5d 2d 31 29 3b 0a 20 20 20 20 20  ues[j]-1);.     
105bf 20 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20     p->nSet++;.  
105c0 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e        while( p->
105c1 75 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20  u.aHash[h] ){.  
105c2 20 20 20 20 20 20 20 20 68 2b 2b 3b 0a 20 20 20          h++;.   
105c3 20 20 20 20 20 20 20 69 66 28 20 68 3e 3d 42 49         if( h>=BI
105c4 54 56 45 43 5f 4e 49 4e 54 20 29 20 68 20 3d 20  TVEC_NINT ) h = 
105c5 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
105c6 20 20 20 20 20 70 2d 3e 75 2e 61 48 61 73 68 5b       p->u.aHash[
105c7 68 5d 20 3d 20 61 69 56 61 6c 75 65 73 5b 6a 5d  h] = aiValues[j]
105c8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
105c9 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73    }.}../*.** Des
105ca 74 72 6f 79 20 61 20 62 69 74 6d 61 70 20 6f 62  troy a bitmap ob
105cb 6a 65 63 74 2e 20 20 52 65 63 6c 61 69 6d 20 61  ject.  Reclaim a
105cc 6c 6c 20 6d 65 6d 6f 72 79 20 75 73 65 64 2e 0a  ll memory used..
105cd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
105ce 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69  E void sqlite3Bi
105cf 74 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76  tvecDestroy(Bitv
105d0 65 63 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  ec *p){.  if( p=
105d1 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
105d2 66 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29  f( p->iDivisor )
105d3 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69  {.    unsigned i
105d4 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
105d5 30 3b 20 69 3c 42 49 54 56 45 43 5f 4e 50 54 52  0; i<BITVEC_NPTR
105d6 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
105d7 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
105d8 6f 79 28 70 2d 3e 75 2e 61 70 53 75 62 5b 69 5d  oy(p->u.apSub[i]
105d9 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
105da 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
105db 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
105dc 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
105dd 20 69 53 69 7a 65 20 70 61 72 61 6d 65 74 65 72   iSize parameter
105de 20 73 70 65 63 69 66 69 65 64 20 77 68 65 6e 20   specified when 
105df 42 69 74 76 65 63 20 2a 70 0a 2a 2a 20 77 61 73  Bitvec *p.** was
105e0 20 63 72 65 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c   created..*/.SQL
105e1 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
105e2 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 69 7a  sqlite3BitvecSiz
105e3 65 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20 20  e(Bitvec *p){.  
105e4 72 65 74 75 72 6e 20 70 2d 3e 69 53 69 7a 65 3b  return p->iSize;
105e5 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
105e6 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
105e7 54 45 53 54 0a 2f 2a 0a 2a 2a 20 4c 65 74 20 56  TEST./*.** Let V
105e8 5b 5d 20 62 65 20 61 6e 20 61 72 72 61 79 20 6f  [] be an array o
105e9 66 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61  f unsigned chara
105ea 63 74 65 72 73 20 73 75 66 66 69 63 69 65 6e 74  cters sufficient
105eb 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 75 70 20 74   to hold.** up t
105ec 6f 20 4e 20 62 69 74 73 2e 20 20 4c 65 74 20 49  o N bits.  Let I
105ed 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 62   be an integer b
105ee 65 74 77 65 65 6e 20 30 20 61 6e 64 20 4e 2e 20  etween 0 and N. 
105ef 20 30 3c 3d 49 3c 4e 2e 0a 2a 2a 20 54 68 65 6e   0<=I<N..** Then
105f0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
105f1 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65  acros can be use
105f2 64 20 74 6f 20 73 65 74 2c 20 63 6c 65 61 72 2c  d to set, clear,
105f3 20 6f 72 20 74 65 73 74 0a 2a 2a 20 69 6e 64 69   or test.** indi
105f4 76 69 64 75 61 6c 20 62 69 74 73 20 77 69 74 68  vidual bits with
105f5 69 6e 20 56 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  in V..*/.#define
105f6 20 53 45 54 42 49 54 28 56 2c 49 29 20 20 20 20   SETBIT(V,I)    
105f7 20 20 56 5b 49 3e 3e 33 5d 20 7c 3d 20 28 31 3c    V[I>>3] |= (1<
105f8 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20  <(I&7)).#define 
105f9 43 4c 45 41 52 42 49 54 28 56 2c 49 29 20 20 20  CLEARBIT(V,I)   
105fa 20 56 5b 49 3e 3e 33 5d 20 26 3d 20 7e 28 31 3c   V[I>>3] &= ~(1<
105fb 3c 28 49 26 37 29 29 0a 23 64 65 66 69 6e 65 20  <(I&7)).#define 
105fc 54 45 53 54 42 49 54 28 56 2c 49 29 20 20 20 20  TESTBIT(V,I)    
105fd 20 28 56 5b 49 3e 3e 33 5d 26 28 31 3c 3c 28 49   (V[I>>3]&(1<<(I
105fe 26 37 29 29 29 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20  &7)))!=0../*.** 
105ff 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e  This routine run
10600 73 20 61 6e 20 65 78 74 65 6e 73 69 76 65 20 74  s an extensive t
10601 65 73 74 20 6f 66 20 74 68 65 20 42 69 74 76 65  est of the Bitve
10602 63 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  c code..**.** Th
10603 65 20 69 6e 70 75 74 20 69 73 20 61 6e 20 61 72  e input is an ar
10604 72 61 79 20 6f 66 20 69 6e 74 65 67 65 72 73 20  ray of integers 
10605 74 68 61 74 20 61 63 74 73 20 61 73 20 61 20 70  that acts as a p
10606 72 6f 67 72 61 6d 0a 2a 2a 20 74 6f 20 74 65 73  rogram.** to tes
10607 74 20 74 68 65 20 42 69 74 76 65 63 2e 20 20 54  t the Bitvec.  T
10608 68 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20  he integers are 
10609 6f 70 63 6f 64 65 73 20 66 6f 6c 6c 6f 77 65 64  opcodes followed
1060a 0a 2a 2a 20 62 79 20 30 2c 20 31 2c 20 6f 72 20  .** by 0, 1, or 
1060b 33 20 6f 70 65 72 61 6e 64 73 2c 20 64 65 70 65  3 operands, depe
1060c 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f 70 63  nding on the opc
1060d 6f 64 65 2e 20 20 41 6e 6f 74 68 65 72 0a 2a 2a  ode.  Another.**
1060e 20 6f 70 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20   opcode follows 
1060f 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65  immediately afte
10610 72 20 74 68 65 20 6c 61 73 74 20 6f 70 65 72 61  r the last opera
10611 6e 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  nd..**.** There 
10612 61 72 65 20 36 20 6f 70 63 6f 64 65 73 20 6e 75  are 6 opcodes nu
10613 6d 62 65 72 65 64 20 66 72 6f 6d 20 30 20 74 68  mbered from 0 th
10614 72 6f 75 67 68 20 35 2e 20 20 30 20 69 73 20 74  rough 5.  0 is t
10615 68 65 0a 2a 2a 20 22 68 61 6c 74 22 20 6f 70 63  he.** "halt" opc
10616 6f 64 65 20 61 6e 64 20 63 61 75 73 65 73 20 74  ode and causes t
10617 68 65 20 74 65 73 74 20 74 6f 20 65 6e 64 2e 0a  he test to end..
10618 2a 2a 0a 2a 2a 20 20 20 20 30 20 20 20 20 20 20  **.**    0      
10619 20 20 20 20 48 61 6c 74 20 61 6e 64 20 72 65 74      Halt and ret
1061a 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1061b 66 20 65 72 72 6f 72 73 0a 2a 2a 20 20 20 20 31  f errors.**    1
1061c 20 4e 20 53 20 58 20 20 20 20 53 65 74 20 4e 20   N S X    Set N 
1061d 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77  bits beginning w
1061e 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d  ith S and increm
1061f 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20  enting by X.**  
10620 20 20 32 20 4e 20 53 20 58 20 20 20 20 43 6c 65    2 N S X    Cle
10621 61 72 20 4e 20 62 69 74 73 20 62 65 67 69 6e 6e  ar N bits beginn
10622 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20 69  ing with S and i
10623 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20 58  ncrementing by X
10624 0a 2a 2a 20 20 20 20 33 20 4e 20 20 20 20 20 20  .**    3 N      
10625 20 20 53 65 74 20 4e 20 72 61 6e 64 6f 6d 6c 79    Set N randomly
10626 20 63 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20   chosen bits.** 
10627 20 20 20 34 20 4e 20 20 20 20 20 20 20 20 43 6c     4 N        Cl
10628 65 61 72 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63  ear N randomly c
10629 68 6f 73 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20  hosen bits.**   
1062a 20 35 20 4e 20 53 20 58 20 20 20 20 53 65 74 20   5 N S X    Set 
1062b 4e 20 62 69 74 73 20 66 72 6f 6d 20 53 20 69 6e  N bits from S in
1062c 63 72 65 6d 65 6e 74 20 58 20 69 6e 20 61 72 72  crement X in arr
1062d 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 69 6e 20  ay only, not in 
1062e 62 69 74 76 65 63 0a 2a 2a 0a 2a 2a 20 54 68 65  bitvec.**.** The
1062f 20 6f 70 63 6f 64 65 73 20 31 20 74 68 72 6f 75   opcodes 1 throu
10630 67 68 20 34 20 70 65 72 66 6f 72 6d 20 73 65 74  gh 4 perform set
10631 20 61 6e 64 20 63 6c 65 61 72 20 6f 70 65 72 61   and clear opera
10632 74 69 6f 6e 73 20 61 72 65 20 70 65 72 66 6f 72  tions are perfor
10633 6d 65 64 0a 2a 2a 20 6f 6e 20 62 6f 74 68 20 61  med.** on both a
10634 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 61   Bitvec object a
10635 6e 64 20 6f 6e 20 61 20 6c 69 6e 65 61 72 20 61  nd on a linear a
10636 72 72 61 79 20 6f 66 20 62 69 74 73 20 6f 62 74  rray of bits obt
10637 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
10638 63 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 77  c..** Opcode 5 w
10639 6f 72 6b 73 20 6f 6e 20 74 68 65 20 6c 69 6e 65  orks on the line
1063a 61 72 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e  ar array only, n
1063b 6f 74 20 6f 6e 20 74 68 65 20 42 69 74 76 65 63  ot on the Bitvec
1063c 2e 0a 2a 2a 20 4f 70 63 6f 64 65 20 35 20 69 73  ..** Opcode 5 is
1063d 20 75 73 65 64 20 74 6f 20 64 65 6c 69 62 65 72   used to deliber
1063e 61 74 65 6c 79 20 69 6e 64 75 63 65 20 61 20 66  ately induce a f
1063f 61 75 6c 74 20 69 6e 20 6f 72 64 65 72 20 74 6f  ault in order to
10640 0a 2a 2a 20 63 6f 6e 66 69 72 6d 20 74 68 61 74  .** confirm that
10641 20 65 72 72 6f 72 20 64 65 74 65 63 74 69 6f 6e   error detection
10642 20 77 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 41 74   works..**.** At
10643 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20   the conclusion 
10644 6f 66 20 74 68 65 20 74 65 73 74 20 74 68 65 20  of the test the 
10645 6c 69 6e 65 61 72 20 61 72 72 61 79 20 69 73 20  linear array is 
10646 63 6f 6d 70 61 72 65 64 0a 2a 2a 20 61 67 61 69  compared.** agai
10647 6e 73 74 20 74 68 65 20 42 69 74 76 65 63 20 6f  nst the Bitvec o
10648 62 6a 65 63 74 2e 20 20 49 66 20 74 68 65 72 65  bject.  If there
10649 20 61 72 65 20 61 6e 79 20 64 69 66 66 65 72 65   are any differe
1064a 6e 63 65 73 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f  nces,.** an erro
1064b 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  r is returned.  
1064c 49 66 20 74 68 65 79 20 61 72 65 20 74 68 65 20  If they are the 
1064d 73 61 6d 65 2c 20 7a 65 72 6f 20 69 73 20 72 65  same, zero is re
1064e 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
1064f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
10650 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  tion error occur
10651 73 2c 20 72 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f  s, return -1..*/
10652 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10653 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76 65  int sqlite3Bitve
10654 63 42 75 69 6c 74 69 6e 54 65 73 74 28 69 6e 74  cBuiltinTest(int
10655 20 73 7a 2c 20 69 6e 74 20 2a 61 4f 70 29 7b 0a   sz, int *aOp){.
10656 20 20 42 69 74 76 65 63 20 2a 70 42 69 74 76 65    Bitvec *pBitve
10657 63 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65  c = 0;.  unsigne
10658 64 20 63 68 61 72 20 2a 70 56 20 3d 20 30 3b 0a  d char *pV = 0;.
10659 20 20 69 6e 74 20 72 63 20 3d 20 2d 31 3b 0a 20    int rc = -1;. 
1065a 20 69 6e 74 20 69 2c 20 6e 78 2c 20 70 63 2c 20   int i, nx, pc, 
1065b 6f 70 3b 0a 20 20 76 6f 69 64 20 2a 70 54 6d 70  op;.  void *pTmp
1065c 53 70 61 63 65 3b 0a 0a 20 20 2f 2a 20 41 6c 6c  Space;..  /* All
1065d 6f 63 61 74 65 20 74 68 65 20 42 69 74 76 65 63  ocate the Bitvec
1065e 20 74 6f 20 62 65 20 74 65 73 74 65 64 20 61 6e   to be tested an
1065f 64 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79  d a linear array
10660 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20 74 6f   of.  ** bits to
10661 20 61 63 74 20 61 73 20 74 68 65 20 72 65 66 65   act as the refe
10662 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42 69 74 76  rence */.  pBitv
10663 65 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  ec = sqlite3Bitv
10664 65 63 43 72 65 61 74 65 28 20 73 7a 20 29 3b 0a  ecCreate( sz );.
10665 20 20 70 56 20 3d 20 73 71 6c 69 74 65 33 5f 6d    pV = sqlite3_m
10666 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29 2f 38 20  alloc( (sz+7)/8 
10667 2b 20 31 20 29 3b 0a 20 20 70 54 6d 70 53 70 61  + 1 );.  pTmpSpa
10668 63 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ce = sqlite3_mal
10669 6c 6f 63 28 42 49 54 56 45 43 5f 53 5a 29 3b 0a  loc(BITVEC_SZ);.
1066a 20 20 69 66 28 20 70 42 69 74 76 65 63 3d 3d 30    if( pBitvec==0
1066b 20 7c 7c 20 70 56 3d 3d 30 20 7c 7c 20 70 54 6d   || pV==0 || pTm
1066c 70 53 70 61 63 65 3d 3d 30 20 20 29 20 67 6f 74  pSpace==0  ) got
1066d 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20  o bitvec_end;.  
1066e 6d 65 6d 73 65 74 28 70 56 2c 20 30 2c 20 28 73  memset(pV, 0, (s
1066f 7a 2b 37 29 2f 38 20 2b 20 31 29 3b 0a 0a 20 20  z+7)/8 + 1);..  
10670 2f 2a 20 4e 55 4c 4c 20 70 42 69 74 76 65 63 20  /* NULL pBitvec 
10671 74 65 73 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74  tests */.  sqlit
10672 65 33 42 69 74 76 65 63 53 65 74 28 30 2c 20 31  e3BitvecSet(0, 1
10673 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76  );.  sqlite3Bitv
10674 65 63 43 6c 65 61 72 28 30 2c 20 31 2c 20 70 54  ecClear(0, 1, pT
10675 6d 70 53 70 61 63 65 29 3b 0a 0a 20 20 2f 2a 20  mpSpace);..  /* 
10676 52 75 6e 20 74 68 65 20 70 72 6f 67 72 61 6d 20  Run the program 
10677 2a 2f 0a 20 20 70 63 20 3d 20 30 3b 0a 20 20 77  */.  pc = 0;.  w
10678 68 69 6c 65 28 20 28 6f 70 20 3d 20 61 4f 70 5b  hile( (op = aOp[
10679 70 63 5d 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  pc])!=0 ){.    s
1067a 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20  witch( op ){.   
1067b 20 20 20 63 61 73 65 20 31 3a 0a 20 20 20 20 20     case 1:.     
1067c 20 63 61 73 65 20 32 3a 0a 20 20 20 20 20 20 63   case 2:.      c
1067d 61 73 65 20 35 3a 20 7b 0a 20 20 20 20 20 20 20  ase 5: {.       
1067e 20 6e 78 20 3d 20 34 3b 0a 20 20 20 20 20 20 20   nx = 4;.       
1067f 20 69 20 3d 20 61 4f 70 5b 70 63 2b 32 5d 20 2d   i = aOp[pc+2] -
10680 20 31 3b 0a 20 20 20 20 20 20 20 20 61 4f 70 5b   1;.        aOp[
10681 70 63 2b 32 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b  pc+2] += aOp[pc+
10682 33 5d 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  3];.        brea
10683 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
10684 20 63 61 73 65 20 33 3a 0a 20 20 20 20 20 20 63   case 3:.      c
10685 61 73 65 20 34 3a 20 0a 20 20 20 20 20 20 64 65  ase 4: .      de
10686 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
10687 20 6e 78 20 3d 20 32 3b 0a 20 20 20 20 20 20 20   nx = 2;.       
10688 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
10689 65 73 73 28 73 69 7a 65 6f 66 28 69 29 2c 20 26  ess(sizeof(i), &
1068a 69 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  i);.        brea
1068b 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
1068c 0a 20 20 20 20 69 66 28 20 28 2d 2d 61 4f 70 5b  .    if( (--aOp[
1068d 70 63 2b 31 5d 29 20 3e 20 30 20 29 20 6e 78 20  pc+1]) > 0 ) nx 
1068e 3d 20 30 3b 0a 20 20 20 20 70 63 20 2b 3d 20 6e  = 0;.    pc += n
1068f 78 3b 0a 20 20 20 20 69 20 3d 20 28 69 20 26 20  x;.    i = (i & 
10690 30 78 37 66 66 66 66 66 66 66 29 25 73 7a 3b 0a  0x7fffffff)%sz;.
10691 20 20 20 20 69 66 28 20 28 6f 70 20 26 20 31 29      if( (op & 1)
10692 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 53 45 54  !=0 ){.      SET
10693 42 49 54 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a  BIT(pV, (i+1));.
10694 20 20 20 20 20 20 69 66 28 20 6f 70 21 3d 35 20        if( op!=5 
10695 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
10696 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
10697 70 42 69 74 76 65 63 2c 20 69 2b 31 29 20 29 20  pBitvec, i+1) ) 
10698 67 6f 74 6f 20 62 69 74 76 65 63 5f 65 6e 64 3b  goto bitvec_end;
10699 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
1069a 73 65 7b 0a 20 20 20 20 20 20 43 4c 45 41 52 42  se{.      CLEARB
1069b 49 54 28 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20  IT(pV, (i+1));. 
1069c 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
1069d 65 63 43 6c 65 61 72 28 70 42 69 74 76 65 63 2c  ecClear(pBitvec,
1069e 20 69 2b 31 2c 20 70 54 6d 70 53 70 61 63 65 29   i+1, pTmpSpace)
1069f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
106a0 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65 20 73  * Test to make s
106a1 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72 20 61  ure the linear a
106a2 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d 61 74  rray exactly mat
106a3 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20 42 69  ches the.  ** Bi
106a4 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 53 74  tvec object.  St
106a5 61 72 74 20 77 69 74 68 20 74 68 65 20 61 73 73  art with the ass
106a6 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74 68 65  umption that the
106a7 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68 20  y do.  ** match 
106a8 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67 65  (rc==0).  Change
106a9 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20   rc to non-zero 
106aa 69 66 20 61 20 64 69 73 63 72 65 70 61 6e 63 79  if a discrepancy
106ab 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e 0a  .  ** is found..
106ac 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69    */.  rc = sqli
106ad 74 65 33 42 69 74 76 65 63 54 65 73 74 28 30 2c  te3BitvecTest(0,
106ae 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76  0) + sqlite3Bitv
106af 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 20  ecTest(pBitvec, 
106b0 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20 20 20  sz+1).          
106b1 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54  + sqlite3BitvecT
106b2 65 73 74 28 70 42 69 74 76 65 63 2c 20 30 29 0a  est(pBitvec, 0).
106b3 20 20 20 20 20 20 20 20 20 20 2b 20 28 73 71 6c            + (sql
106b4 69 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70  ite3BitvecSize(p
106b5 42 69 74 76 65 63 29 20 2d 20 73 7a 29 3b 0a 20  Bitvec) - sz);. 
106b6 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 7a 3b   for(i=1; i<=sz;
106b7 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 20   i++){.    if(  
106b8 28 54 45 53 54 42 49 54 28 70 56 2c 69 29 29 21  (TESTBIT(pV,i))!
106b9 3d 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65  =sqlite3BitvecTe
106ba 73 74 28 70 42 69 74 76 65 63 2c 69 29 20 29 7b  st(pBitvec,i) ){
106bb 0a 20 20 20 20 20 20 72 63 20 3d 20 69 3b 0a 20  .      rc = i;. 
106bc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
106bd 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65  }.  }..  /* Free
106be 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72 75 63   allocated struc
106bf 74 75 72 65 20 2a 2f 0a 62 69 74 76 65 63 5f 65  ture */.bitvec_e
106c0 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  nd:.  sqlite3_fr
106c1 65 65 28 70 54 6d 70 53 70 61 63 65 29 3b 0a 20  ee(pTmpSpace);. 
106c2 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 56   sqlite3_free(pV
106c3 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76  );.  sqlite3Bitv
106c4 65 63 44 65 73 74 72 6f 79 28 70 42 69 74 76 65  ecDestroy(pBitve
106c5 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
106c6 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
106c7 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
106c8 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  _TEST */../*****
106c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
106ca 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a 2a   bitvec.c ******
106cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106cd 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
106ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
106cf 66 69 6c 65 20 70 63 61 63 68 65 2e 63 20 2a 2a  file pcache.c **
106d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106d2 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
106d3 32 30 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a  2008 August 05.*
106d4 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
106d5 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
106d6 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
106d7 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
106d8 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
106d9 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
106da 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
106db 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
106dc 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
106dd 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
106de 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
106df 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
106e0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
106e1 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
106e2 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
106e3 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
106e4 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
106e5 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
106e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
106ea 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
106eb 6d 65 6e 74 73 20 74 68 61 74 20 70 61 67 65 20  ments that page 
106ec 63 61 63 68 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  cache..*/../*.**
106ed 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65   A complete page
106ee 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73   cache is an ins
106ef 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
106f0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
106f1 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67  ct PCache {.  Pg
106f2 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44  Hdr *pDirty, *pD
106f3 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20  irtyTail;       
106f4 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72    /* List of dir
106f5 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20  ty pages in LRU 
106f6 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72  order */.  PgHdr
106f7 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20   *pSynced;      
106f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
106f9 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61  * Last synced pa
106fa 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67 65  ge in dirty page
106fb 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e   list */.  int n
106fc 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
106fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
106fe 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  * Number of refe
106ff 72 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a  renced pages */.
10700 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20    int nMax;     
10701 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10702 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75        /* Configu
10703 72 65 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a  red cache size *
10704 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20  /.  int szPage; 
10705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10706 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
10707 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 69 6e  of every page in
10708 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a 20   this cache */. 
10709 20 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20 20   int szExtra;   
1070a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1070b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1070c 65 78 74 72 61 20 73 70 61 63 65 20 66 6f 72 20  extra space for 
1070d 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69  each page */.  i
1070e 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20  nt bPurgeable;  
1070f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10710 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 61     /* True if pa
10711 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b 69  ges are on backi
10712 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69 6e  ng store */.  in
10713 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f 69  t (*xStress)(voi
10714 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20 20  d*,PgHdr*);     
10715 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79    /* Call to try
10716 20 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c 65   make a page cle
10717 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  an */.  void *pS
10718 74 72 65 73 73 3b 20 20 20 20 20 20 20 20 20 20  tress;          
10719 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
1071a 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65  rgument to xStre
1071b 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ss */.  sqlite3_
1071c 70 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20  pcache *pCache; 
1071d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1071e 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 6d  luggable cache m
1071f 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64 72  odule */.  PgHdr
10720 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 20   *pPage1;       
10721 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10722 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20 70  * Reference to p
10723 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  age 1 */.};../*.
10724 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20 61  ** Some of the a
10725 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20 69  ssert() macros i
10726 6e 20 74 68 69 73 20 63 6f 64 65 20 61 72 65 20  n this code are 
10727 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74 6f  too expensive to
10728 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75 72   run.** even dur
10729 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75 67  ing normal debug
1072a 67 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d 20  ging.  Use them 
1072b 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20 6c  only rarely on l
1072c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20 74  ong-running.** t
1072d 65 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74 68  ests.  Enable th
1072e 65 20 65 78 70 65 6e 73 69 76 65 20 61 73 73 65  e expensive asse
1072f 72 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  rts using the.**
10730 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45   -DSQLITE_ENABLE
10731 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52  _EXPENSIVE_ASSER
10732 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  T=1 compile-time
10733 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 64   option..*/.#ifd
10734 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
10735 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52  _EXPENSIVE_ASSER
10736 54 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e  T.# define expen
10737 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 20 20  sive_assert(X)  
10738 61 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65 0a  assert(X).#else.
10739 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73 69  # define expensi
1073a 76 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65 6e  ve_assert(X).#en
1073b 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
1073c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1073d 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64 20  ******** Linked 
1073e 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74 20  List Management 
1073f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10740 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69  ****/..#if !defi
10741 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 64  ned(NDEBUG) && d
10742 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
10743 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f 41  ABLE_EXPENSIVE_A
10744 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68 65  SSERT)./*.** Che
10745 63 6b 20 74 68 61 74 20 74 68 65 20 70 43 61 63  ck that the pCac
10746 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72 69  he->pSynced vari
10747 61 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72 72  able is set corr
10748 65 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a 20  ectly. If it.** 
10749 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20 66  is not, either f
1074a 61 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f 72  ail an assert or
1074b 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f 74   return zero. Ot
1074c 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 0a  herwise, return.
1074d 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68 69  ** non-zero. Thi
1074e 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  s is only used i
1074f 6e 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c  n debugging buil
10750 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ds, as follows:.
10751 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69 76  **.**   expensiv
10752 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65  e_assert( pcache
10753 43 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61 63  CheckSynced(pCac
10754 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63  he) );.*/.static
10755 20 69 6e 74 20 70 63 61 63 68 65 43 68 65 63 6b   int pcacheCheck
10756 53 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a 70  Synced(PCache *p
10757 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20  Cache){.  PgHdr 
10758 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61 63  *p;.  for(p=pCac
10759 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b 20  he->pDirtyTail; 
1075a 70 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e 63  p!=pCache->pSync
1075b 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 50  ed; p=p->pDirtyP
1075c 72 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72 74  rev){.    assert
1075d 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d  ( p->nRef || (p-
1075e 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
1075f 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a 20  D_SYNC) );.  }. 
10760 20 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c 7c   return (p==0 ||
10761 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d 3e   p->nRef || (p->
10762 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
10763 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23 65  _SYNC)==0);.}.#e
10764 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47 20  ndif /* !NDEBUG 
10765 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
10766 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45 52  _EXPENSIVE_ASSER
10767 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f  T */../*.** Remo
10768 76 65 20 70 61 67 65 20 70 50 61 67 65 20 66 72  ve page pPage fr
10769 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20 64  om the list of d
1076a 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 73  irty pages..*/.s
1076b 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
1076c 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79  eRemoveFromDirty
1076d 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50 61 67  List(PgHdr *pPag
1076e 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 20  e){.  PCache *p 
1076f 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3b  = pPage->pCache;
10770 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
10771 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c 7c  e->pDirtyNext ||
10772 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74   pPage==p->pDirt
10773 79 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65 72  yTail );.  asser
10774 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79  t( pPage->pDirty
10775 50 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d 70  Prev || pPage==p
10776 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20 2f  ->pDirty );..  /
10777 2a 20 55 70 64 61 74 65 20 74 68 65 20 50 43 61  * Update the PCa
10778 63 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61 72  che1.pSynced var
10779 69 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73 61  iable if necessa
1077a 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ry. */.  if( p->
1077b 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20 29  pSynced==pPage )
1077c 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53 79  {.    PgHdr *pSy
1077d 6e 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70 44  nced = pPage->pD
1077e 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77 68  irtyPrev;.    wh
1077f 69 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26 20  ile( pSynced && 
10780 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73 26  (pSynced->flags&
10781 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
10782 20 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63 65   ){.      pSynce
10783 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44 69  d = pSynced->pDi
10784 72 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20  rtyPrev;.    }. 
10785 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20     p->pSynced = 
10786 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20 20  pSynced;.  }..  
10787 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74  if( pPage->pDirt
10788 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50 61  yNext ){.    pPa
10789 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e  ge->pDirtyNext->
1078a 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 61  pDirtyPrev = pPa
1078b 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b 0a  ge->pDirtyPrev;.
1078c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
1078d 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e 70  ert( pPage==p->p
1078e 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20 20  DirtyTail );.   
1078f 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d   p->pDirtyTail =
10790 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72   pPage->pDirtyPr
10791 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  ev;.  }.  if( pP
10792 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20  age->pDirtyPrev 
10793 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44  ){.    pPage->pD
10794 69 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74 79  irtyPrev->pDirty
10795 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70 44  Next = pPage->pD
10796 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c 73  irtyNext;.  }els
10797 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
10798 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 20  Page==p->pDirty 
10799 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79  );.    p->pDirty
1079a 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79   = pPage->pDirty
1079b 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61 67  Next;.  }.  pPag
1079c 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20  e->pDirtyNext = 
1079d 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72  0;.  pPage->pDir
1079e 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20 65  tyPrev = 0;..  e
1079f 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74 28  xpensive_assert(
107a0 20 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e 63   pcacheCheckSync
107a1 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ed(p) );.}../*.*
107a2 2a 20 41 64 64 20 70 61 67 65 20 70 50 61 67 65  * Add page pPage
107a3 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20   to the head of 
107a4 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20 28  the dirty list (
107a5 50 43 61 63 68 65 31 2e 70 44 69 72 74 79 20 69  PCache1.pDirty i
107a6 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61 67  s set to.** pPag
107a7 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e)..*/.static vo
107a8 69 64 20 70 63 61 63 68 65 41 64 64 54 6f 44 69  id pcacheAddToDi
107a9 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70  rtyList(PgHdr *p
107aa 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20  Page){.  PCache 
107ab 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63  *p = pPage->pCac
107ac 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  he;..  assert( p
107ad 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
107ae 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70 44  ==0 && pPage->pD
107af 69 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20 70  irtyPrev==0 && p
107b0 2d 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65 20  ->pDirty!=pPage 
107b1 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44 69  );..  pPage->pDi
107b2 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44 69  rtyNext = p->pDi
107b3 72 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65  rty;.  if( pPage
107b4 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b 0a  ->pDirtyNext ){.
107b5 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
107b6 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e 70  e->pDirtyNext->p
107b7 44 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b 0a  DirtyPrev==0 );.
107b8 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72 74      pPage->pDirt
107b9 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72 65  yNext->pDirtyPre
107ba 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20  v = pPage;.  }. 
107bb 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61   p->pDirty = pPa
107bc 67 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70 44  ge;.  if( !p->pD
107bd 69 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20 20  irtyTail ){.    
107be 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d 20  p->pDirtyTail = 
107bf 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 28  pPage;.  }.  if(
107c0 20 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26 20   !p->pSynced && 
107c1 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67 73  0==(pPage->flags
107c2 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
107c3 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79 6e  ) ){.    p->pSyn
107c4 63 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d  ced = pPage;.  }
107c5 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73  .  expensive_ass
107c6 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63 6b  ert( pcacheCheck
107c7 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a  Synced(p) );.}..
107c8 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61 72  /*.** Wrapper ar
107c9 6f 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61 62  ound the pluggab
107ca 6c 65 20 63 61 63 68 65 73 20 78 55 6e 70 69 6e  le caches xUnpin
107cb 20 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65 20   method. If the 
107cc 63 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69 6e  cache is.** bein
107cd 67 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69 6e  g used for an in
107ce 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
107cf 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
107d0 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73  is a no-op..*/.s
107d1 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
107d2 65 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70 29  eUnpin(PgHdr *p)
107d3 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63  {.  PCache *pCac
107d4 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a  he = p->pCache;.
107d5 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50    if( pCache->bP
107d6 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20  urgeable ){.    
107d7 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29  if( p->pgno==1 )
107d8 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d 3e  {.      pCache->
107d9 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20  pPage1 = 0;.    
107da 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  }.    sqlite3Glo
107db 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
107dc 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d 3e  .xUnpin(pCache->
107dd 70 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a 20  pCache, p, 0);. 
107de 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
107df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
107e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72  ********** Gener
107e2 61 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a 2a  al Interfaces **
107e3 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74 69  ****.**.** Initi
107e4 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64 6f  alize and shutdo
107e5 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68  wn the page cach
107e6 65 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65 69  e subsystem. Nei
107e7 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 2a  ther of these .*
107e8 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  * functions are 
107e9 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53  threadsafe..*/.S
107ea 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
107eb 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49  t sqlite3PcacheI
107ec 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 7b  nitialize(void){
107ed 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
107ee 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
107ef 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20  e.xInit==0 ){.  
107f0 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53    sqlite3PCacheS
107f1 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 7d  etDefault();.  }
107f2 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
107f3 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
107f4 61 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69 74  ache.xInit(sqlit
107f5 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
107f6 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a 53  cache.pArg);.}.S
107f7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
107f8 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
107f9 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a  Shutdown(void){.
107fa 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
107fb 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
107fc 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20 20  .xShutdown ){.  
107fd 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
107fe 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53 68  onfig.pcache.xSh
107ff 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47 6c  utdown(sqlite3Gl
10800 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
10801 65 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a  e.pArg);.  }.}..
10802 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
10803 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f   size in bytes o
10804 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65 63  f a PCache objec
10805 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
10806 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
10807 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64 29  PcacheSize(void)
10808 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66 28  { return sizeof(
10809 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a 2a  PCache); }../*.*
1080a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 50  * Create a new P
1080b 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53 74  Cache object. St
1080c 6f 72 61 67 65 20 73 70 61 63 65 20 74 6f 20 68  orage space to h
1080d 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a 2a  old the object.*
1080e 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  * has already be
1080f 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  en allocated and
10810 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 61 73   is passed in as
10811 20 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e 20   the p pointer. 
10812 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 64  .** The caller d
10813 69 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75 63  iscovers how muc
10814 68 20 73 70 61 63 65 20 6e 65 65 64 73 20 74 6f  h space needs to
10815 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79   be allocated by
10816 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71 6c   .** calling sql
10817 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 29  ite3PcacheSize()
10818 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10819 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1081a 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69 6e  PcacheOpen(.  in
1081b 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20 20  t szPage,       
1081c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
1081d 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67 65  ze of every page
1081e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74 72   */.  int szExtr
1081f 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a,              
10820 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61 63     /* Extra spac
10821 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
10822 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20  h each page */. 
10823 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 2c   int bPurgeable,
10824 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10825 20 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61   True if pages a
10826 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74  re on backing st
10827 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  ore */.  int (*x
10828 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50 67  Stress)(void*,Pg
10829 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74 6f  Hdr*),/* Call to
1082a 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67   try to make pag
1082b 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f  es clean */.  vo
1082c 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20  id *pStress,    
1082d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
1082e 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65 73  gument to xStres
1082f 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70  s */.  PCache *p
10830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10831 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63 61      /* Prealloca
10832 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74 68  ted space for th
10833 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a 20  e PCache */.){. 
10834 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 69   memset(p, 0, si
10835 7a 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a 20  zeof(PCache));. 
10836 20 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50   p->szPage = szP
10837 61 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74 72  age;.  p->szExtr
10838 61 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20 70  a = szExtra;.  p
10839 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20 62  ->bPurgeable = b
1083a 50 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d 3e  Purgeable;.  p->
1083b 78 53 74 72 65 73 73 20 3d 20 78 53 74 72 65 73  xStress = xStres
1083c 73 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73 20  s;.  p->pStress 
1083d 3d 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d 3e  = pStress;.  p->
1083e 6e 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a 2f  nMax = 100;.}../
1083f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
10840 70 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50 43  page size for PC
10841 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68 65  ache object. The
10842 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73   caller must ens
10843 75 72 65 20 74 68 61 74 20 74 68 65 72 65 0a 2a  ure that there.*
10844 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e  * are no outstan
10845 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65  ding page refere
10846 6e 63 65 73 20 77 68 65 6e 20 74 68 69 73 20 66  nces when this f
10847 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10848 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
10849 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1084a 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69  3PcacheSetPageSi
1084b 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68  ze(PCache *pCach
1084c 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b 0a  e, int szPage){.
1084d 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68 65    assert( pCache
1084e 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43 61  ->nRef==0 && pCa
1084f 63 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20 29  che->pDirty==0 )
10850 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e  ;.  if( pCache->
10851 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71  pCache ){.    sq
10852 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
10853 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f  g.pcache.xDestro
10854 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  y(pCache->pCache
10855 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70  );.    pCache->p
10856 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a 20  Cache = 0;.  }. 
10857 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20   pCache->szPage 
10858 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a  = szPage;.}../*.
10859 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69 6e  ** Try to obtain
1085a 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65   a page from the
1085b 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54   cache..*/.SQLIT
1085c 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1085d 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68  lite3PcacheFetch
1085e 28 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63  (.  PCache *pCac
1085f 68 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62 74  he,       /* Obt
10860 61 69 6e 20 74 68 65 20 70 61 67 65 20 66 72 6f  ain the page fro
10861 6d 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a  m this cache */.
10862 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20    Pgno pgno,    
10863 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
10864 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69 6e  number to obtain
10865 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74 65   */.  int create
10866 46 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20 49  Flag,       /* I
10867 66 20 74 72 75 65 2c 20 63 72 65 61 74 65 20 70  f true, create p
10868 61 67 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e  age if it does n
10869 6f 74 20 65 78 69 73 74 20 61 6c 72 65 61 64 79  ot exist already
1086a 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70 70   */.  PgHdr **pp
1086b 50 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20 57  Page        /* W
1086c 72 69 74 65 20 74 68 65 20 70 61 67 65 20 68 65  rite the page he
1086d 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64 72  re */.){.  PgHdr
1086e 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 69   *pPage = 0;.  i
1086f 6e 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20 61  nt eCreate;..  a
10870 73 73 65 72 74 28 20 70 43 61 63 68 65 21 3d 30  ssert( pCache!=0
10871 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 72   );.  assert( cr
10872 65 61 74 65 46 6c 61 67 3d 3d 31 20 7c 7c 20 63  eateFlag==1 || c
10873 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 3b 0a  reateFlag==0 );.
10874 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e 30    assert( pgno>0
10875 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
10876 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65   pluggable cache
10877 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65   (sqlite3_pcache
10878 2a 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  *) has not been 
10879 61 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20  allocated,.  ** 
1087a 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e  allocate it now.
1087b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 61  .  */.  if( !pCa
1087c 63 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20 63  che->pCache && c
1087d 72 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20 20  reateFlag ){.   
1087e 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20   sqlite3_pcache 
1087f 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74  *p;.    int nByt
10880 65 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 70  e;.    nByte = p
10881 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20  Cache->szPage + 
10882 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61 20  pCache->szExtra 
10883 2b 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29 3b  + sizeof(PgHdr);
10884 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
10885 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
10886 63 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79 74  che.xCreate(nByt
10887 65 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67  e, pCache->bPurg
10888 65 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28 20  eable);.    if( 
10889 21 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  !p ){.      retu
1088a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1088b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1088c 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
1088d 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65  cache.xCachesize
1088e 28 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78  (p, pCache->nMax
1088f 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 70  );.    pCache->p
10890 43 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a  Cache = p;.  }..
10891 20 20 65 43 72 65 61 74 65 20 3d 20 63 72 65 61    eCreate = crea
10892 74 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28 21  teFlag * (1 + (!
10893 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
10894 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70  le || !pCache->p
10895 44 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20 70  Dirty));.  if( p
10896 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b  Cache->pCache ){
10897 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c  .    pPage = sql
10898 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
10899 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70  .pcache.xFetch(p
1089a 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70  Cache->pCache, p
1089b 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a 20  gno, eCreate);. 
1089c 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67 65   }..  if( !pPage
1089d 20 26 26 20 65 43 72 65 61 74 65 3d 3d 31 20 29   && eCreate==1 )
1089e 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50 67  {.    PgHdr *pPg
1089f 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61  ;..    /* Find a
108a0 20 64 69 72 74 79 20 70 61 67 65 20 74 6f 20 77   dirty page to w
108a1 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 65 63  rite-out and rec
108a2 79 63 6c 65 2e 20 46 69 72 73 74 20 74 72 79 20  ycle. First try 
108a3 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 20 2a  to find a .    *
108a4 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73  * page that does
108a5 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20 6a   not require a j
108a6 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e 65  ournal-sync (one
108a7 20 77 69 74 68 20 50 47 48 44 52 5f 4e 45 45 44   with PGHDR_NEED
108a8 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c 65  _SYNC.    ** cle
108a9 61 72 65 64 29 2c 20 62 75 74 20 69 66 20 74 68  ared), but if th
108aa 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  at is not possib
108ab 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 20 61 6e  le settle for an
108ac 79 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20  y other .    ** 
108ad 75 6e 72 65 66 65 72 65 6e 63 65 64 20 64 69 72  unreferenced dir
108ae 74 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a  ty page..    */.
108af 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73      expensive_as
108b0 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63  sert( pcacheChec
108b1 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65 29 20  kSynced(pCache) 
108b2 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d 70  );.    for(pPg=p
108b3 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20  Cache->pSynced; 
108b4 0a 20 20 20 20 20 20 20 20 70 50 67 20 26 26 20  .        pPg && 
108b5 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28 70  (pPg->nRef || (p
108b6 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
108b7 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20 20  NEED_SYNC)); .  
108b8 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e 70        pPg=pPg->p
108b9 44 69 72 74 79 50 72 65 76 0a 20 20 20 20 29 3b  DirtyPrev.    );
108ba 0a 20 20 20 20 69 66 28 20 21 70 50 67 20 29 7b  .    if( !pPg ){
108bb 0a 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d 70  .      for(pPg=p
108bc 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69  Cache->pDirtyTai
108bd 6c 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e 6e  l; pPg && pPg->n
108be 52 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70 44  Ref; pPg=pPg->pD
108bf 69 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20 7d  irtyPrev);.    }
108c0 0a 20 20 20 20 69 66 28 20 70 50 67 20 29 7b 0a  .    if( pPg ){.
108c1 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20        int rc;.  
108c2 20 20 20 20 72 63 20 3d 20 70 43 61 63 68 65 2d      rc = pCache-
108c3 3e 78 53 74 72 65 73 73 28 70 43 61 63 68 65 2d  >xStress(pCache-
108c4 3e 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b 0a  >pStress, pPg);.
108c5 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
108c6 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53  LITE_OK && rc!=S
108c7 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
108c8 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
108c9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
108ca 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c 69      pPage = sqli
108cb 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
108cc 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70 43  pcache.xFetch(pC
108cd 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67  ache->pCache, pg
108ce 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20 69  no, 2);.  }..  i
108cf 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20  f( pPage ){.    
108d0 69 66 28 20 21 70 50 61 67 65 2d 3e 70 44 61 74  if( !pPage->pDat
108d1 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 65  a ){.      memse
108d2 74 28 70 50 61 67 65 2c 20 30 2c 20 73 69 7a 65  t(pPage, 0, size
108d3 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43 61 63  of(PgHdr) + pCac
108d4 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20 20  he->szExtra);.  
108d5 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72      pPage->pExtr
108d6 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 61 67  a = (void*)&pPag
108d7 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 61 67  e[1];.      pPag
108d8 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69 64  e->pData = (void
108d9 20 2a 29 26 28 28 63 68 61 72 20 2a 29 70 50 61   *)&((char *)pPa
108da 67 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 64 72  ge)[sizeof(PgHdr
108db 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45 78  ) + pCache->szEx
108dc 74 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 61 67  tra];.      pPag
108dd 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63  e->pCache = pCac
108de 68 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65 2d  he;.      pPage-
108df 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20 20  >pgno = pgno;.  
108e0 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
108e1 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d 70  pPage->pCache==p
108e2 43 61 63 68 65 20 29 3b 0a 20 20 20 20 61 73 73  Cache );.    ass
108e3 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e 6f  ert( pPage->pgno
108e4 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61 73  ==pgno );.    as
108e5 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 45 78  sert( pPage->pEx
108e6 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70 50  tra==(void *)&pP
108e7 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20 69  age[1] );..    i
108e8 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52 65  f( 0==pPage->nRe
108e9 66 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68  f ){.      pCach
108ea 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d  e->nRef++;.    }
108eb 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 65 66  .    pPage->nRef
108ec 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 6e 6f  ++;.    if( pgno
108ed 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43 61  ==1 ){.      pCa
108ee 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 70 50  che->pPage1 = pP
108ef 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  age;.    }.  }. 
108f0 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67 65   *ppPage = pPage
108f1 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61 67  ;.  return (pPag
108f2 65 3d 3d 30 20 26 26 20 65 43 72 65 61 74 65 29  e==0 && eCreate)
108f3 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20   ? SQLITE_NOMEM 
108f4 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  : SQLITE_OK;.}..
108f5 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20  /*.** Decrement 
108f6 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
108f7 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20 49  unt on a page. I
108f8 66 20 74 68 65 20 70 61 67 65 20 69 73 20 63 6c  f the page is cl
108f9 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20 72  ean and the.** r
108fa 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 64  eference count d
108fb 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e 20  rops to 0, then 
108fc 69 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62 6c  it is made elibl
108fd 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67 2e  e for recycling.
108fe 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
108ff 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
10900 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48  cacheRelease(PgH
10901 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  dr *p){.  assert
10902 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
10903 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66   p->nRef--;.  if
10904 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a  ( p->nRef==0 ){.
10905 20 20 20 20 50 43 61 63 68 65 20 2a 70 43 61 63      PCache *pCac
10906 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a  he = p->pCache;.
10907 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66      pCache->nRef
10908 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 2d 3e  --;.    if( (p->
10909 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
1090a 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  Y)==0 ){.      p
1090b 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a 20  cacheUnpin(p);. 
1090c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1090d 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65  /* Move the page
1090e 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20   to the head of 
1090f 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 2e 20  the dirty list. 
10910 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 68 65 52  */.      pcacheR
10911 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69  emoveFromDirtyLi
10912 73 74 28 70 29 3b 0a 20 20 20 20 20 20 70 63 61  st(p);.      pca
10913 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73  cheAddToDirtyLis
10914 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  t(p);.    }.  }.
10915 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61 73  }../*.** Increas
10916 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  e the reference 
10917 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 70 6c  count of a suppl
10918 69 65 64 20 70 61 67 65 20 62 79 20 31 2e 0a 2a  ied page by 1..*
10919 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1091a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
1091b 63 68 65 52 65 66 28 50 67 48 64 72 20 2a 70 29  cheRef(PgHdr *p)
1091c 7b 0a 20 20 61 73 73 65 72 74 28 70 2d 3e 6e 52  {.  assert(p->nR
1091d 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65 66  ef>0);.  p->nRef
1091e 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f  ++;.}../*.** Dro
1091f 70 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  p a page from th
10920 65 20 63 61 63 68 65 2e 20 54 68 65 72 65 20 6d  e cache. There m
10921 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 6f  ust be exactly o
10922 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20  ne reference to 
10923 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68 69  the.** page. Thi
10924 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74  s function delet
10925 65 73 20 74 68 61 74 20 72 65 66 65 72 65 6e 63  es that referenc
10926 65 2c 20 73 6f 20 61 66 74 65 72 20 69 74 20 72  e, so after it r
10927 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70 61  eturns the.** pa
10928 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ge pointed to by
10929 20 70 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a 2a   p is invalid..*
1092a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1092b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
1092c 63 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a 70  cheDrop(PgHdr *p
1092d 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  ){.  PCache *pCa
1092e 63 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70  che;.  assert( p
1092f 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69  ->nRef==1 );.  i
10930 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48 44  f( p->flags&PGHD
10931 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 20 70  R_DIRTY ){.    p
10932 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44  cacheRemoveFromD
10933 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 7d  irtyList(p);.  }
10934 0a 20 20 70 43 61 63 68 65 20 3d 20 70 2d 3e 70  .  pCache = p->p
10935 43 61 63 68 65 3b 0a 20 20 70 43 61 63 68 65 2d  Cache;.  pCache-
10936 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70  >nRef--;.  if( p
10937 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20  ->pgno==1 ){.   
10938 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20   pCache->pPage1 
10939 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 0;.  }.  sqlit
1093a 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
1093b 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43 61  cache.xUnpin(pCa
1093c 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20  che->pCache, p, 
1093d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  1);.}../*.** Mak
1093e 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20  e sure the page 
1093f 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72  is marked as dir
10940 74 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20  ty. If it isn't 
10941 64 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a 2a  dirty already,.*
10942 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f  * make it so..*/
10943 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10944 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
10945 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48 64  heMakeDirty(PgHd
10946 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61 67  r *p){.  p->flag
10947 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e 54  s &= ~PGHDR_DONT
10948 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72 74  _WRITE;.  assert
10949 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
1094a 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61 67   if( 0==(p->flag
1094b 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59 29  s & PGHDR_DIRTY)
1094c 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73   ){.    p->flags
1094d 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59 3b   |= PGHDR_DIRTY;
1094e 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54 6f  .    pcacheAddTo
1094f 44 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a 20  DirtyList( p);. 
10950 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65   }.}../*.** Make
10951 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20 69   sure the page i
10952 73 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65 61  s marked as clea
10953 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20 63  n. If it isn't c
10954 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a  lean already,.**
10955 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a   make it so..*/.
10956 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10957 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
10958 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64 72  eMakeClean(PgHdr
10959 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e   *p){.  if( (p->
1095a 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44 49  flags & PGHDR_DI
1095b 52 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61 63  RTY) ){.    pcac
1095c 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
1095d 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70 2d  yList(p);.    p-
1095e 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48 44  >flags &= ~(PGHD
1095f 52 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e 45  R_DIRTY|PGHDR_NE
10960 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69 66  ED_SYNC);.    if
10961 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a  ( p->nRef==0 ){.
10962 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70 69        pcacheUnpi
10963 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n(p);.    }.  }.
10964 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76  }../*.** Make ev
10965 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20  ery page in the 
10966 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a  cache clean..*/.
10967 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10968 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
10969 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65  eCleanAll(PCache
1096a 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48   *pCache){.  PgH
1096b 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20  dr *p;.  while( 
1096c 28 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69  (p = pCache->pDi
1096d 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  rty)!=0 ){.    s
1096e 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
1096f 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a  Clean(p);.  }.}.
10970 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65  ./*.** Clear the
10971 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
10972 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64   flag from all d
10973 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53  irty pages..*/.S
10974 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
10975 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
10976 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28 50  ClearSyncFlags(P
10977 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a  Cache *pCache){.
10978 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f    PgHdr *p;.  fo
10979 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69 72  r(p=pCache->pDir
1097a 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72  ty; p; p=p->pDir
1097b 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e  tyNext){.    p->
1097c 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f  flags &= ~PGHDR_
1097d 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20  NEED_SYNC;.  }. 
1097e 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64   pCache->pSynced
1097f 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74   = pCache->pDirt
10980 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  yTail;.}../*.** 
10981 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20  Change the page 
10982 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 70  number of page p
10983 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f   to newPgno. .*/
10984 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10985 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
10986 68 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70 2c  heMove(PgHdr *p,
10987 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a   Pgno newPgno){.
10988 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65    PCache *pCache
10989 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20   = p->pCache;.  
1098a 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e  assert( p->nRef>
1098b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
1098c 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71  ewPgno>0 );.  sq
1098d 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
1098e 67 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79 28  g.pcache.xRekey(
1098f 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
10990 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50  p, p->pgno, newP
10991 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20  gno);.  p->pgno 
10992 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28  = newPgno;.  if(
10993 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52   (p->flags&PGHDR
10994 5f 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e 66  _DIRTY) && (p->f
10995 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f  lags&PGHDR_NEED_
10996 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63 61  SYNC) ){.    pca
10997 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72  cheRemoveFromDir
10998 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70  tyList(p);.    p
10999 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c  cacheAddToDirtyL
1099a 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ist(p);.  }.}../
1099b 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79 20  *.** Drop every 
1099c 63 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f 73  cache entry whos
1099d 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73  e page number is
1099e 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 22 70   greater than "p
1099f 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c  gno". The.** cal
109a0 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65 20  ler must ensure 
109a1 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6e  that there are n
109a2 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65  o outstanding re
109a3 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79 20  ferences to any 
109a4 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74  pages.** other t
109a5 68 61 6e 20 70 61 67 65 20 31 20 77 69 74 68 20  han page 1 with 
109a6 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67 72  a page number gr
109a7 65 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e  eater than pgno.
109a8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
109a9 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  is a reference t
109aa 6f 20 70 61 67 65 20 31 20 61 6e 64 20 74 68 65  o page 1 and the
109ab 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72 20   pgno parameter 
109ac 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a 2a  passed to this.*
109ad 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c  * function is 0,
109ae 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 61   then the data a
109af 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  rea associated w
109b0 69 74 68 20 70 61 67 65 20 31 20 69 73 20 7a 65  ith page 1 is ze
109b1 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65  roed, but.** the
109b2 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20   page object is 
109b3 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a  not dropped..*/.
109b4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
109b5 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
109b6 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68 65  eTruncate(PCache
109b7 20 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20 70   *pCache, Pgno p
109b8 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61 63  gno){.  if( pCac
109b9 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20 20  he->pCache ){.  
109ba 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20 20    PgHdr *p;.    
109bb 50 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20  PgHdr *pNext;.  
109bc 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e    for(p=pCache->
109bd 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65  pDirty; p; p=pNe
109be 78 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78 74  xt){.      pNext
109bf 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78 74   = p->pDirtyNext
109c0 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70  ;.      if( p->p
109c1 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20  gno>pgno ){.    
109c2 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 66      assert( p->f
109c3 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59  lags&PGHDR_DIRTY
109c4 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   );.        sqli
109c5 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65  te3PcacheMakeCle
109c6 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20  an(p);.      }. 
109c7 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67 6e     }.    if( pgn
109c8 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e  o==0 && pCache->
109c9 70 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20 20  pPage1 ){.      
109ca 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e 70  memset(pCache->p
109cb 50 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c  Page1->pData, 0,
109cc 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 29   pCache->szPage)
109cd 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20 31  ;.      pgno = 1
109ce 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
109cf 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
109d0 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65  pcache.xTruncate
109d1 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c  (pCache->pCache,
109d2 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a   pgno+1);.  }.}.
109d3 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63  ./*.** Close a c
109d4 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ache..*/.SQLITE_
109d5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
109d6 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28  ite3PcacheClose(
109d7 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
109d8 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70  .  if( pCache->p
109d9 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c  Cache ){.    sql
109da 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
109db 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79  .pcache.xDestroy
109dc 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29  (pCache->pCache)
109dd 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  ;.  }.}../* .** 
109de 44 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74  Discard the cont
109df 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63 68  ents of the cach
109e0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
109e1 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
109e2 33 50 63 61 63 68 65 43 6c 65 61 72 28 50 43 61  3PcacheClear(PCa
109e3 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20  che *pCache){.  
109e4 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75  sqlite3PcacheTru
109e5 6e 63 61 74 65 28 70 43 61 63 68 65 2c 20 30 29  ncate(pCache, 0)
109e6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65  ;.}../*.** Merge
109e7 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61   two lists of pa
109e8 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  ges connected by
109e9 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70   pDirty and in p
109ea 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f  gno order..** Do
109eb 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67   not both fixing
109ec 20 74 68 65 20 70 44 69 72 74 79 50 72 65 76 20   the pDirtyPrev 
109ed 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61  pointers..*/.sta
109ee 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68  tic PgHdr *pcach
109ef 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28  eMergeDirtyList(
109f0 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72  PgHdr *pA, PgHdr
109f1 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72   *pB){.  PgHdr r
109f2 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20  esult, *pTail;. 
109f3 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74   pTail = &result
109f4 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26  ;.  while( pA &&
109f5 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70   pB ){.    if( p
109f6 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f  A->pgno<pB->pgno
109f7 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d   ){.      pTail-
109f8 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20  >pDirty = pA;.  
109f9 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a      pTail = pA;.
109fa 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70        pA = pA->p
109fb 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65  Dirty;.    }else
109fc 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70  {.      pTail->p
109fd 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20  Dirty = pB;.    
109fe 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20    pTail = pB;.  
109ff 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69      pB = pB->pDi
10a00 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  rty;.    }.  }. 
10a01 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70   if( pA ){.    p
10a02 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70  Tail->pDirty = p
10a03 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  A;.  }else if( p
10a04 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e  B ){.    pTail->
10a05 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d  pDirty = pB;.  }
10a06 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d  else{.    pTail-
10a07 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d  >pDirty = 0;.  }
10a08 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74  .  return result
10a09 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a  .pDirty;.}../*.*
10a0a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20  * Sort the list 
10a0b 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65  of pages in acce
10a0c 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70  nding order by p
10a0d 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a  gno.  Pages are.
10a0e 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ** connected by 
10a0f 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e  pDirty pointers.
10a10 20 20 54 68 65 20 70 44 69 72 74 79 50 72 65 76    The pDirtyPrev
10a11 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a   pointers are.**
10a12 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68   corrupted by th
10a13 69 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20 53  is sort..**.** S
10a14 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e 6f  ince there canno
10a15 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 32  t be more than 2
10a16 5e 33 31 20 64 69 73 74 69 6e 63 74 20 70 61 67  ^31 distinct pag
10a17 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65  es in a database
10a18 2c 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e 6f  ,.** there canno
10a19 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 33  t be more than 3
10a1a 31 20 62 75 63 6b 65 74 73 20 72 65 71 75 69 72  1 buckets requir
10a1b 65 64 20 62 79 20 74 68 65 20 6d 65 72 67 65 20  ed by the merge 
10a1c 73 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20 65  sorter..** One e
10a1d 78 74 72 61 20 62 75 63 6b 65 74 20 69 73 20 61  xtra bucket is a
10a1e 64 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f 76  dded to catch ov
10a1f 65 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20 73  erflow in case s
10a20 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65 72  omething.** ever
10a21 20 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b 65   changes to make
10a22 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65   the previous se
10a23 6e 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63 74  ntence incorrect
10a24 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 53  ..*/.#define N_S
10a25 4f 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a 73  ORT_BUCKET  32.s
10a26 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61  tatic PgHdr *pca
10a27 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74  cheSortDirtyList
10a28 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20  (PgHdr *pIn){.  
10a29 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f  PgHdr *a[N_SORT_
10a2a 42 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20 69  BUCKET], *p;.  i
10a2b 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28 61  nt i;.  memset(a
10a2c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29 3b  , 0, sizeof(a));
10a2d 0a 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29 7b  .  while( pIn ){
10a2e 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20 20  .    p = pIn;.  
10a2f 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72 74    pIn = p->pDirt
10a30 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74 79  y;.    p->pDirty
10a31 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d   = 0;.    for(i=
10a32 30 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53 4f  0; ALWAYS(i<N_SO
10a33 52 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69 2b  RT_BUCKET-1); i+
10a34 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61 5b  +){.      if( a[
10a35 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  i]==0 ){.       
10a36 20 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20 20   a[i] = p;.     
10a37 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
10a38 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
10a39 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69   = pcacheMergeDi
10a3a 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70 29  rtyList(a[i], p)
10a3b 3b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20 3d  ;.        a[i] =
10a3c 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
10a3d 7d 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28  }.    if( NEVER(
10a3e 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  i==N_SORT_BUCKET
10a3f 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  -1) ){.      /* 
10a40 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65  To get here, the
10a41 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e  re need to be 2^
10a42 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20  (N_SORT_BUCKET) 
10a43 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20 20  elements in.    
10a44 20 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20 6c    ** the input l
10a45 69 73 74 2e 20 20 42 75 74 20 74 68 61 74 20 69  ist.  But that i
10a46 73 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20 20  s impossible..  
10a47 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b 69      */.      a[i
10a48 5d 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44  ] = pcacheMergeD
10a49 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70  irtyList(a[i], p
10a4a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  );.    }.  }.  p
10a4b 20 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69   = a[0];.  for(i
10a4c 3d 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55 43  =1; i<N_SORT_BUC
10a4d 4b 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  KET; i++){.    p
10a4e 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44 69   = pcacheMergeDi
10a4f 72 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d 29  rtyList(p, a[i])
10a50 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
10a51 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
10a52 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  n a list of all 
10a53 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20 74  dirty pages in t
10a54 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65 64  he cache, sorted
10a55 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72 2e   by page number.
10a56 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
10a57 54 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74 65  TE PgHdr *sqlite
10a58 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73 74  3PcacheDirtyList
10a59 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29  (PCache *pCache)
10a5a 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20  {.  PgHdr *p;.  
10a5b 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44  for(p=pCache->pD
10a5c 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44  irty; p; p=p->pD
10a5d 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70  irtyNext){.    p
10a5e 2d 3e 70 44 69 72 74 79 20 3d 20 70 2d 3e 70 44  ->pDirty = p->pD
10a5f 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  irtyNext;.  }.  
10a60 72 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f 72  return pcacheSor
10a61 74 44 69 72 74 79 4c 69 73 74 28 70 43 61 63 68  tDirtyList(pCach
10a62 65 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a 2f  e->pDirty);.}../
10a63 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  * .** Return the
10a64 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
10a65 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67 65   referenced page
10a66 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61  s held by the ca
10a67 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  che..*/.SQLITE_P
10a68 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
10a69 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74  e3PcacheRefCount
10a6a 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29  (PCache *pCache)
10a6b 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63 68  {.  return pCach
10a6c 65 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a  e->nRef;.}../*.*
10a6d 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
10a6e 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65  ber of reference
10a6f 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 73 75  s to the page su
10a70 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67  pplied as an arg
10a71 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ument..*/.SQLITE
10a72 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
10a73 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65  ite3PcachePageRe
10a74 66 63 6f 75 6e 74 28 50 67 48 64 72 20 2a 70 29  fcount(PgHdr *p)
10a75 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52  {.  return p->nR
10a76 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65  ef;.}../* .** Re
10a77 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20 6e  turn the total n
10a78 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
10a79 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f 0a  n the cache..*/.
10a7a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
10a7b 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
10a7c 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68 65  Pagecount(PCache
10a7d 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e 74   *pCache){.  int
10a7e 20 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 69 66   nPage = 0;.  if
10a7f 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  ( pCache->pCache
10a80 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20   ){.    nPage = 
10a81 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
10a82 66 69 67 2e 70 63 61 63 68 65 2e 78 50 61 67 65  fig.pcache.xPage
10a83 63 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70 43  count(pCache->pC
10a84 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ache);.  }.  ret
10a85 75 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23 69  urn nPage;.}..#i
10a86 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
10a87 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20 73  ./*.** Get the s
10a88 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d 73  uggested cache-s
10a89 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51  ize value..*/.SQ
10a8a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
10a8b 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47 65   sqlite3PcacheGe
10a8c 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63 68  tCachesize(PCach
10a8d 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72 65  e *pCache){.  re
10a8e 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d 61  turn pCache->nMa
10a8f 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  x;.}.#endif../*.
10a90 2a 2a 20 53 65 74 20 74 68 65 20 73 75 67 67 65  ** Set the sugge
10a91 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20  sted cache-size 
10a92 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  value..*/.SQLITE
10a93 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
10a94 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61  lite3PcacheSetCa
10a95 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a  chesize(PCache *
10a96 70 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50 61  pCache, int mxPa
10a97 67 65 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e 6e  ge){.  pCache->n
10a98 4d 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20  Max = mxPage;.  
10a99 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63  if( pCache->pCac
10a9a 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  he ){.    sqlite
10a9b 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
10a9c 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28  ache.xCachesize(
10a9d 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
10a9e 6d 78 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a  mxPage);.  }.}..
10a9f 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
10aa0 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29 20  TE_CHECK_PAGES) 
10aa1 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
10aa2 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 46  E_DEBUG)./*.** F
10aa3 6f 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67  or all dirty pag
10aa4 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  es currently in 
10aa5 74 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f 6b  the cache, invok
10aa6 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 0a  e the specified.
10aa7 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68 69  ** callback. Thi
10aa8 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  s is only used i
10aa9 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48 45  f the SQLITE_CHE
10aaa 43 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20 69  CK_PAGES macro i
10aab 73 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2f  s.** defined..*/
10aac 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10aad 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
10aae 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50  heIterateDirty(P
10aaf 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76  Cache *pCache, v
10ab0 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48  oid (*xIter)(PgH
10ab1 64 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72 20  dr *)){.  PgHdr 
10ab2 2a 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28 70  *pDirty;.  for(p
10ab3 44 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70 44  Dirty=pCache->pD
10ab4 69 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70 44  irty; pDirty; pD
10ab5 69 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44 69  irty=pDirty->pDi
10ab6 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78 49  rtyNext){.    xI
10ab7 74 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20 7d  ter(pDirty);.  }
10ab8 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  .}.#endif../****
10ab9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
10aba 66 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a  f pcache.c *****
10abb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10abc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10abd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
10abe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
10abf 20 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63 20   file pcache1.c 
10ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ac2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
10ac3 20 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20 30   2008 November 0
10ac4 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
10ac5 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
10ac6 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
10ac7 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
10ac8 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
10ac9 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
10aca 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
10acb 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
10acc 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
10acd 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
10ace 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
10acf 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
10ad0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
10ad1 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
10ad2 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
10ad3 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
10ad4 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
10ad5 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
10ad6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ad7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ad8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ad9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ada 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
10adb 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
10adc 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61 63  default page cac
10add 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
10ade 6e 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  n (the.** sqlite
10adf 33 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66 61  3_pcache interfa
10ae0 63 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e  ce). It also con
10ae1 74 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74 68  tains part of th
10ae2 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
10ae3 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49 54  .** of the SQLIT
10ae4 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
10ae5 48 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72  HE and sqlite3_r
10ae6 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20  elease_memory() 
10ae7 66 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20  features..** If 
10ae8 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65  the default page
10ae9 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
10aea 61 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69 64  ation is overrid
10aeb 65 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72  en, then neither
10aec 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f   of.** these two
10aed 20 66 65 61 74 75 72 65 73 20 61 72 65 20 61 76   features are av
10aee 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 0a 0a 74 79  ailable..*/...ty
10aef 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 61  pedef struct PCa
10af0 63 68 65 31 20 50 43 61 63 68 65 31 3b 0a 74 79  che1 PCache1;.ty
10af1 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48  pedef struct PgH
10af2 64 72 31 20 50 67 48 64 72 31 3b 0a 74 79 70 65  dr1 PgHdr1;.type
10af3 64 65 66 20 73 74 72 75 63 74 20 50 67 46 72 65  def struct PgFre
10af4 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f 74  eslot PgFreeslot
10af5 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74  ;../* Pointers t
10af6 6f 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20  o structures of 
10af7 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 61  this type are ca
10af8 73 74 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20  st and returned 
10af9 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71  as .** opaque sq
10afa 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61  lite3_pcache* ha
10afb 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 20  ndles.*/.struct 
10afc 50 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43  PCache1 {.  /* C
10afd 61 63 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69  ache configurati
10afe 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50  on parameters. P
10aff 61 67 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65  age size (szPage
10b00 29 20 61 6e 64 20 74 68 65 20 70 75 72 67 65 61  ) and the purgea
10b01 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62  ble.  ** flag (b
10b02 50 75 72 67 65 61 62 6c 65 29 20 61 72 65 20 73  Purgeable) are s
10b03 65 74 20 77 68 65 6e 20 74 68 65 20 63 61 63 68  et when the cach
10b04 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d  e is created. nM
10b05 61 78 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20  ax may be .  ** 
10b06 6d 6f 64 69 66 69 65 64 20 61 74 20 61 6e 79 20  modified at any 
10b07 74 69 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74  time by a call t
10b08 6f 20 74 68 65 20 70 63 61 63 68 65 31 43 61 63  o the pcache1Cac
10b09 68 65 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e  heSize() method.
10b0a 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c  .  ** The global
10b0b 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68   mutex must be h
10b0c 65 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73 69  eld when accessi
10b0d 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20  ng nMax..  */.  
10b0e 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20  int szPage;     
10b0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b10 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61      /* Size of a
10b11 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73 20 69  llocated pages i
10b12 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74  n bytes */.  int
10b13 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20   bPurgeable;    
10b14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b15 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 68   /* True if cach
10b16 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 20 2a  e is purgeable *
10b17 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
10b18 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 20   nMin;          
10b19 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d          /* Minim
10b1a 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  um number of pag
10b1b 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20  es reserved */. 
10b1c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d   unsigned int nM
10b1d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ax;             
10b1e 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72       /* Configur
10b1f 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 22 20  ed "cache_size" 
10b20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48  value */..  /* H
10b21 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61 6c 6c  ash table of all
10b22 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f 6c 6c   pages. The foll
10b23 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20  owing variables 
10b24 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 65  may only be acce
10b25 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74  ssed.  ** when t
10b26 68 65 20 61 63 63 65 73 73 6f 72 20 69 73 20 68  he accessor is h
10b27 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 61  olding the globa
10b28 6c 20 6d 75 74 65 78 20 28 73 65 65 20 70 63 61  l mutex (see pca
10b29 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10b2a 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 68   .  ** and pcach
10b2b 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 29 2e  e1LeaveMutex()).
10b2c 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  .  */.  unsigned
10b2d 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65   int nRecyclable
10b2e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
10b2f 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
10b30 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a  n the LRU list *
10b31 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
10b32 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20   nPage;         
10b33 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
10b34 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
10b35 20 69 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20   in apHash */.  
10b36 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61  unsigned int nHa
10b37 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sh;             
10b38 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
10b39 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68   slots in apHash
10b3a 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a  [] */.  PgHdr1 *
10b3b 2a 61 70 48 61 73 68 3b 20 20 20 20 20 20 20 20  *apHash;        
10b3c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
10b3d 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61  ash table for fa
10b3e 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79  st lookup by key
10b3f 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 20   */..  unsigned 
10b40 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 20  int iMaxKey;    
10b41 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
10b42 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e 20 73  rgest key seen s
10b43 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65 28 29  ince xTruncate()
10b44 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61   */.};../*.** Ea
10b45 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 20 69  ch cache entry i
10b46 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79  s represented by
10b47 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
10b48 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a  the following .*
10b49 2a 20 73 74 72 75 63 74 75 72 65 2e 20 41 20 62  * structure. A b
10b4a 75 66 66 65 72 20 6f 66 20 50 67 48 64 72 31 2e  uffer of PgHdr1.
10b4b 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 62  pCache->szPage b
10b4c 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65  ytes is allocate
10b4d 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 62  d .** directly b
10b4e 65 66 6f 72 65 20 74 68 69 73 20 73 74 72 75 63  efore this struc
10b4f 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28  ture in memory (
10b50 73 65 65 20 74 68 65 20 50 47 48 44 52 31 5f 54  see the PGHDR1_T
10b51 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63  O_PAGE() .** mac
10b52 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74  ro below)..*/.st
10b53 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20  ruct PgHdr1 {.  
10b54 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65  unsigned int iKe
10b55 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  y;             /
10b56 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67  * Key value (pag
10b57 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50  e number) */.  P
10b58 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20  gHdr1 *pNext;   
10b59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b5a 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20 74 61   Next in hash ta
10b5b 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50  ble chain */.  P
10b5c 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20  Cache1 *pCache; 
10b5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b5e 20 43 61 63 68 65 20 74 68 61 74 20 63 75 72 72   Cache that curr
10b5f 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20  ently owns this 
10b60 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31  page */.  PgHdr1
10b61 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20   *pLruNext;     
10b62 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
10b63 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20   in LRU list of 
10b64 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a  unpinned pages *
10b65 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75  /.  PgHdr1 *pLru
10b66 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20  Prev;           
10b67 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69     /* Previous i
10b68 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e  n LRU list of un
10b69 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a  pinned pages */.
10b6a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73  };../*.** Free s
10b6b 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f  lots in the allo
10b6c 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69  cator used to di
10b6d 76 69 64 65 20 75 70 20 74 68 65 20 62 75 66 66  vide up the buff
10b6e 65 72 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e  er provided usin
10b6f 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f  g.** the SQLITE_
10b70 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10b71 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73   mechanism..*/.s
10b72 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74  truct PgFreeslot
10b73 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20   {.  PgFreeslot 
10b74 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74  *pNext;  /* Next
10b75 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b   free slot */.};
10b76 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64  ../*.** Global d
10b77 61 74 61 20 75 73 65 64 20 62 79 20 74 68 69 73  ata used by this
10b78 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69   cache..*/.stati
10b79 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  c SQLITE_WSD str
10b7a 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c  uct PCacheGlobal
10b7b 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   {.  sqlite3_mut
10b7c 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20  ex *mutex;      
10b7d 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74           /* stat
10b7e 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53  ic mutex MUTEX_S
10b7f 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20  TATIC_LRU */..  
10b80 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20  int nMaxPage;   
10b81 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b82 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d      /* Sum of nM
10b83 61 78 50 61 67 65 20 66 6f 72 20 70 75 72 67 65  axPage for purge
10b84 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20  able caches */. 
10b85 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20   int nMinPage;  
10b86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b87 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e       /* Sum of n
10b88 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67  MinPage for purg
10b89 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a  eable caches */.
10b8a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61    int nCurrentPa
10b8b 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
10b8c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
10b8d 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67  of purgeable pag
10b8e 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  es allocated */.
10b8f 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65    PgHdr1 *pLruHe
10b90 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20  ad, *pLruTail;  
10b91 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73        /* LRU lis
10b92 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61  t of unpinned pa
10b93 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72  ges */..  /* Var
10b94 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74  iables related t
10b95 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  o SQLITE_CONFIG_
10b96 50 41 47 45 43 41 43 48 45 20 73 65 74 74 69 6e  PAGECACHE settin
10b97 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53  gs. */.  int szS
10b98 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  lot;            
10b99 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b9a 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65  Size of each fre
10b9b 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64  e slot */.  void
10b9c 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b   *pStart, *pEnd;
10b9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b9e 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67  /* Bounds of pag
10b9f 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61  ecache malloc ra
10ba0 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73  nge */.  PgFrees
10ba1 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20  lot *pFree;     
10ba2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10ba3 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73  Free page blocks
10ba4 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74   */.  int isInit
10ba5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10ba6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
10ba7 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64  e if initialized
10ba8 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b   */.} pcache1_g;
10ba9 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65  ../*.** All code
10baa 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68   in this file sh
10bab 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68 65 20  ould access the 
10bac 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65  global structure
10bad 20 61 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a   above via the.*
10bae 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 65 31  * alias "pcache1
10baf 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20  ". This ensures 
10bb0 74 68 61 74 20 74 68 65 20 57 53 44 20 65 6d 75  that the WSD emu
10bb1 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77  lation is used w
10bb2 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67  hen.** compiling
10bb3 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61   for systems tha
10bb4 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74  t do not support
10bb5 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64   real WSD..*/.#d
10bb6 65 66 69 6e 65 20 70 63 61 63 68 65 31 20 28 47  efine pcache1 (G
10bb7 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50 43 61  LOBAL(struct PCa
10bb8 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68  cheGlobal, pcach
10bb9 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68  e1_g))../*.** Wh
10bba 65 6e 20 61 20 50 67 48 64 72 31 20 73 74 72 75  en a PgHdr1 stru
10bbb 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74  cture is allocat
10bbc 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74  ed, the associat
10bbd 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67  ed PCache1.szPag
10bbe 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61  e.** bytes of da
10bbf 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64  ta are located d
10bc0 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 20 69  irectly before i
10bc1 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65  t in memory (i.e
10bc2 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73  . the total.** s
10bc3 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
10bc4 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28  ation is sizeof(
10bc5 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e  PgHdr1)+PCache1.
10bc6 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 54 68  szPage byte). Th
10bc7 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50  e.** PGHDR1_TO_P
10bc8 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65  AGE() macro take
10bc9 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
10bca 20 50 67 48 64 72 31 20 73 74 72 75 63 74 75 72   PgHdr1 structur
10bcb 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d  e as.** an argum
10bcc 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ent and returns 
10bcd 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
10bce 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63   associated bloc
10bcf 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62  k of szPage.** b
10bd0 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54  ytes. The PAGE_T
10bd1 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f  O_PGHDR1() macro
10bd2 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69   does the opposi
10bd3 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74  te: its argument
10bd4 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72   is.** a pointer
10bd5 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73   to a block of s
10bd6 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 20 64  zPage bytes of d
10bd7 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 74 75  ata and the retu
10bd8 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61  rn value is.** a
10bd9 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
10bda 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 64 72  associated PgHdr
10bdb 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  1 structure..**.
10bdc 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 47 48  **   assert( PGH
10bdd 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45  DR1_TO_PAGE(PAGE
10bde 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68  _TO_PGHDR1(pCach
10bdf 65 2c 20 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f 0a  e, X))==X );.*/.
10be0 23 64 65 66 69 6e 65 20 50 47 48 44 52 31 5f 54  #define PGHDR1_T
10be1 4f 5f 50 41 47 45 28 70 29 20 20 20 20 28 76 6f  O_PAGE(p)    (vo
10be2 69 64 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20  id*)(((char*)p) 
10be3 2d 20 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50  - p->pCache->szP
10be4 61 67 65 29 0a 23 64 65 66 69 6e 65 20 50 41 47  age).#define PAG
10be5 45 5f 54 4f 5f 50 47 48 44 52 31 28 63 2c 20 70  E_TO_PGHDR1(c, p
10be6 29 20 28 50 67 48 64 72 31 2a 29 28 28 28 63 68  ) (PgHdr1*)(((ch
10be7 61 72 2a 29 70 29 20 2b 20 63 2d 3e 73 7a 50 61  ar*)p) + c->szPa
10be8 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  ge)../*.** Macro
10be9 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c  s to enter and l
10bea 65 61 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20  eave the global 
10beb 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64  LRU mutex..*/.#d
10bec 65 66 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74  efine pcache1Ent
10bed 65 72 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65  erMutex() sqlite
10bee 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63  3_mutex_enter(pc
10bef 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65  ache1.mutex).#de
10bf0 66 69 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76  fine pcache1Leav
10bf1 65 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33  eMutex() sqlite3
10bf2 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61  _mutex_leave(pca
10bf3 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a  che1.mutex)../**
10bf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10bf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10bf9 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c  ******* Page All
10bfa 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43  ocation/SQLITE_C
10bfb 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c  ONFIG_PCACHE Rel
10bfc 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a  ated Functions *
10bfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
10bfe 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
10bff 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75  ion is called du
10c00 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
10c01 69 6f 6e 20 69 66 20 61 20 73 74 61 74 69 63 20  ion if a static 
10c02 62 75 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75  buffer is .** su
10c03 70 70 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f  pplied to use fo
10c04 72 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65  r the page-cache
10c05 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20   by passing the 
10c06 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
10c07 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20  GECACHE.** verb 
10c08 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  to sqlite3_confi
10c09 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70  g(). Parameter p
10c0a 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  Buf points to an
10c0b 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67   allocation larg
10c0c 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63  e.** enough to c
10c0d 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65  ontain 'n' buffe
10c0e 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73  rs of 'sz' bytes
10c0f 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45   each..*/.SQLITE
10c10 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
10c11 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65  lite3PCacheBuffe
10c12 72 53 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75  rSetup(void *pBu
10c13 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e  f, int sz, int n
10c14 29 7b 0a 20 20 69 66 28 20 70 63 61 63 68 65 31  ){.  if( pcache1
10c15 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 50  .isInit ){.    P
10c16 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 20  gFreeslot *p;.  
10c17 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e    sz = ROUNDDOWN
10c18 38 28 73 7a 29 3b 0a 20 20 20 20 70 63 61 63 68  8(sz);.    pcach
10c19 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a  e1.szSlot = sz;.
10c1a 20 20 20 20 70 63 61 63 68 65 31 2e 70 53 74 61      pcache1.pSta
10c1b 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 70  rt = pBuf;.    p
10c1c 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30  cache1.pFree = 0
10c1d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d  ;.    while( n--
10c1e 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 28 50   ){.      p = (P
10c1f 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b  gFreeslot*)pBuf;
10c20 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20  .      p->pNext 
10c21 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b  = pcache1.pFree;
10c22 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70  .      pcache1.p
10c23 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20  Free = p;.      
10c24 70 42 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28  pBuf = (void*)&(
10c25 28 63 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d  (char*)pBuf)[sz]
10c26 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63  ;.    }.    pcac
10c27 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b  he1.pEnd = pBuf;
10c28 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  .  }.}../*.** Ma
10c29 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73  lloc function us
10c2a 65 64 20 77 69 74 68 69 6e 20 74 68 69 73 20 66  ed within this f
10c2b 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ile to allocate 
10c2c 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62  space from the b
10c2d 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75  uffer.** configu
10c2e 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  red using sqlite
10c2f 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
10c30 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10c31 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  ) option. If no 
10c32 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 65 72 20  .** such buffer 
10c33 65 78 69 73 74 73 20 6f 72 20 74 68 65 72 65 20  exists or there 
10c34 69 73 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74  is no space left
10c35 20 69 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e   in it, this fun
10c36 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20  ction falls .** 
10c37 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d  back to sqlite3M
10c38 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
10c39 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31  ic void *pcache1
10c3a 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29  Alloc(int nByte)
10c3b 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61  {.  void *p;.  a
10c3c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
10c3d 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65  utex_held(pcache
10c3e 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  1.mutex) );.  if
10c3f 28 20 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31  ( nByte<=pcache1
10c40 2e 73 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68  .szSlot && pcach
10c41 65 31 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20  e1.pFree ){.    
10c42 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e  assert( pcache1.
10c43 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 70 20  isInit );.    p 
10c44 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 61 63  = (PgHdr1 *)pcac
10c45 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70  he1.pFree;.    p
10c46 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70  cache1.pFree = p
10c47 63 61 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e  cache1.pFree->pN
10c48 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ext;.    sqlite3
10c49 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45  StatusSet(SQLITE
10c4a 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
10c4b 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a  E_SIZE, nByte);.
10c4c 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
10c4d 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
10c4e 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45  US_PAGECACHE_USE
10c4f 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  D, 1);.  }else{.
10c50 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  .    /* Allocate
10c51 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 75 73   a new buffer us
10c52 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  ing sqlite3Mallo
10c53 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20  c. Before doing 
10c54 73 6f 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20  so, exit the.   
10c55 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68   ** global pcach
10c56 65 20 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f  e mutex and unlo
10c57 63 6b 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  ck the pager-cac
10c58 68 65 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65  he object pCache
10c59 2e 20 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a  . This is .    *
10c5a 2a 20 73 6f 20 74 68 61 74 20 69 66 20 74 68 65  * so that if the
10c5b 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f   attempt to allo
10c5c 63 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65  cate a new buffe
10c5d 72 20 63 61 75 73 65 73 20 74 68 65 20 74 68 65  r causes the the
10c5e 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75   .    ** configu
10c5f 72 65 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69  red soft-heap-li
10c60 6d 69 74 20 74 6f 20 62 65 20 62 72 65 61 63 68  mit to be breach
10c61 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70  ed, it will be p
10c62 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a  ossible to.    *
10c63 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79  * reclaim memory
10c64 20 66 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72   from this pager
10c65 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20  -cache..    */. 
10c66 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d     pcache1LeaveM
10c67 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20  utex();.    p = 
10c68 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42  sqlite3Malloc(nB
10c69 79 74 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65  yte);.    pcache
10c6a 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
10c6b 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20     if( p ){.    
10c6c 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74    int sz = sqlit
10c6d 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b  e3MallocSize(p);
10c6e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
10c6f 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
10c70 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
10c71 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20  OVERFLOW, sz);. 
10c72 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
10c73 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  n p;.}../*.** Fr
10c74 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20  ee an allocated 
10c75 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20  buffer obtained 
10c76 66 72 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f  from pcache1Allo
10c77 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  c()..*/.static v
10c78 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 28  oid pcache1Free(
10c79 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65  void *p){.  asse
10c7a 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
10c7b 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d  x_held(pcache1.m
10c7c 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
10c7d 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
10c7e 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70  if( p>=pcache1.p
10c7f 53 74 61 72 74 20 26 26 20 70 3c 70 63 61 63 68  Start && p<pcach
10c80 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50  e1.pEnd ){.    P
10c81 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74  gFreeslot *pSlot
10c82 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
10c83 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
10c84 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
10c85 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53  SED, -1);.    pS
10c86 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f  lot = (PgFreeslo
10c87 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d  t*)p;.    pSlot-
10c88 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31  >pNext = pcache1
10c89 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63  .pFree;.    pcac
10c8a 68 65 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f  he1.pFree = pSlo
10c8b 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
10c8c 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69  int iSize = sqli
10c8d 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29  te3MallocSize(p)
10c8e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
10c8f 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
10c90 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f  ATUS_PAGECACHE_O
10c91 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29  VERFLOW, -iSize)
10c92 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
10c93 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ee(p);.  }.}../*
10c94 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
10c95 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69  ew page object i
10c96 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61  nitially associa
10c97 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70  ted with cache p
10c98 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  Cache..*/.static
10c99 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 65 31   PgHdr1 *pcache1
10c9a 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65  AllocPage(PCache
10c9b 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e  1 *pCache){.  in
10c9c 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66  t nByte = sizeof
10c9d 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68  (PgHdr1) + pCach
10c9e 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 76 6f 69  e->szPage;.  voi
10c9f 64 20 2a 70 50 67 20 3d 20 70 63 61 63 68 65 31  d *pPg = pcache1
10ca0 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20  Alloc(nByte);.  
10ca1 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 66 28  PgHdr1 *p;.  if(
10ca2 20 70 50 67 20 29 7b 0a 20 20 20 20 70 20 3d 20   pPg ){.    p = 
10ca3 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70  PAGE_TO_PGHDR1(p
10ca4 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 20 20  Cache, pPg);.   
10ca5 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75   if( pCache->bPu
10ca6 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  rgeable ){.     
10ca7 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
10ca8 74 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20  tPage++;.    }. 
10ca9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20   }else{.    p = 
10caa 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
10cab 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  p;.}../*.** Free
10cac 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61   a page object a
10cad 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 63  llocated by pcac
10cae 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a  he1AllocPage()..
10caf 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65  **.** The pointe
10cb0 72 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20  r is allowed to 
10cb1 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69  be NULL, which i
10cb2 73 20 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20  s prudent.  But 
10cb3 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20  it turns out.** 
10cb4 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74  that the current
10cb5 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
10cb6 68 61 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72  happens to never
10cb7 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
10cb8 6e 65 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c  ne.** with a NUL
10cb9 4c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65  L pointer, so we
10cba 20 6d 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74   mark the NULL t
10cbb 65 73 74 20 77 69 74 68 20 41 4c 57 41 59 53 28  est with ALWAYS(
10cbc 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
10cbd 64 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67  d pcache1FreePag
10cbe 65 28 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20  e(PgHdr1 *p){.  
10cbf 69 66 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b  if( ALWAYS(p) ){
10cc0 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63  .    if( p->pCac
10cc1 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29  he->bPurgeable )
10cc2 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  {.      pcache1.
10cc3 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a  nCurrentPage--;.
10cc4 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65      }.    pcache
10cc5 31 46 72 65 65 28 50 47 48 44 52 31 5f 54 4f 5f  1Free(PGHDR1_TO_
10cc6 50 41 47 45 28 70 29 29 3b 0a 20 20 7d 0a 7d 0a  PAGE(p));.  }.}.
10cc7 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75  ./*.** Malloc fu
10cc8 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53  nction used by S
10cc9 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20  QLite to obtain 
10cca 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62  space from the b
10ccb 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64  uffer configured
10ccc 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65  .** using sqlite
10ccd 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
10cce 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10ccf 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  ) option. If no 
10cd0 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65  such buffer.** e
10cd1 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63  xists, this func
10cd2 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20  tion falls back 
10cd3 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  to sqlite3Malloc
10cd4 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
10cd5 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
10cd6 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e  te3PageMalloc(in
10cd7 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70  t sz){.  void *p
10cd8 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10cd9 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70  Mutex();.  p = p
10cda 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b  cache1Alloc(sz);
10cdb 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
10cdc 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e  utex();.  return
10cdd 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   p;.}../*.** Fre
10cde 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62  e an allocated b
10cdf 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66  uffer obtained f
10ce0 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d  rom sqlite3PageM
10ce1 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  alloc()..*/.SQLI
10ce2 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
10ce3 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
10ce4 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63  void *p){.  pcac
10ce5 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  he1EnterMutex();
10ce6 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 28 70  .  pcache1Free(p
10ce7 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  );.  pcache1Leav
10ce8 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a  eMutex();.}../**
10ce9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ceb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ced 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10cee 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20  ******* General 
10cef 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46  Implementation F
10cf0 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a  unctions *******
10cf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10cf2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
10cf3 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
10cf4 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72  ion is used to r
10cf5 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74  esize the hash t
10cf6 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65  able used by the
10cf7 20 63 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a   cache passed.**
10cf8 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
10cf9 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gument..**.** Th
10cfa 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d  e global mutex m
10cfb 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e  ust be held when
10cfc 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
10cfd 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  s called..*/.sta
10cfe 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 52  tic int pcache1R
10cff 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68 65  esizeHash(PCache
10d00 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20  1 *p){.  PgHdr1 
10d01 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67  **apNew;.  unsig
10d02 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20  ned int nNew;.  
10d03 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a  unsigned int i;.
10d04 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
10d05 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
10d06 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
10d07 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61  .  nNew = p->nHa
10d08 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77  sh*2;.  if( nNew
10d09 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77  <256 ){.    nNew
10d0a 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70   = 256;.  }..  p
10d0b 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
10d0c 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61  ();.  if( p->nHa
10d0d 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67  sh ){ sqlite3Beg
10d0e 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
10d0f 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50  ; }.  apNew = (P
10d10 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33  gHdr1 **)sqlite3
10d11 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50  _malloc(sizeof(P
10d12 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a  gHdr1 *)*nNew);.
10d13 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29    if( p->nHash )
10d14 7b 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  { sqlite3EndBeni
10d15 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20  gnMalloc(); }.  
10d16 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
10d17 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77  x();.  if( apNew
10d18 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61   ){.    memset(a
10d19 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pNew, 0, sizeof(
10d1a 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b  PgHdr1 *)*nNew);
10d1b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
10d1c 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a  p->nHash; i++){.
10d1d 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 50        PgHdr1 *pP
10d1e 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72  age;.      PgHdr
10d1f 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70  1 *pNext = p->ap
10d20 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77  Hash[i];.      w
10d21 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70  hile( (pPage = p
10d22 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20  Next)!=0 ){.    
10d23 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
10d24 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79   h = pPage->iKey
10d25 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20   % nNew;.       
10d26 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e   pNext = pPage->
10d27 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70  pNext;.        p
10d28 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70  Page->pNext = ap
10d29 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20  New[h];.        
10d2a 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65  apNew[h] = pPage
10d2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
10d2c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
10d2d 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20  (p->apHash);.   
10d2e 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e   p->apHash = apN
10d2f 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68  ew;.    p->nHash
10d30 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20   = nNew;.  }..  
10d31 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73  return (p->apHas
10d32 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  h ? SQLITE_OK : 
10d33 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d  SQLITE_NOMEM);.}
10d34 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
10d35 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e  ction is used in
10d36 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f  ternally to remo
10d37 76 65 20 74 68 65 20 70 61 67 65 20 70 50 61 67  ve the page pPag
10d38 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67  e from the .** g
10d39 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20  lobal LRU list, 
10d3a 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 74  if is part of it
10d3b 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f  . If pPage is no
10d3c 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c  t part of the gl
10d3d 6f 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74  obal.** LRU list
10d3e 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  , then this func
10d3f 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
10d40 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61  .**.** The globa
10d41 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  l mutex must be 
10d42 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
10d43 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10d44 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
10d45 64 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65  d pcache1PinPage
10d46 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b  (PgHdr1 *pPage){
10d47 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
10d48 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
10d49 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
10d4a 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28    if( pPage && (
10d4b 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
10d4c 7c 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65  || pPage==pcache
10d4d 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20  1.pLruTail) ){. 
10d4e 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c     if( pPage->pL
10d4f 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20  ruPrev ){.      
10d50 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d  pPage->pLruPrev-
10d51 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67  >pLruNext = pPag
10d52 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20  e->pLruNext;.   
10d53 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65   }.    if( pPage
10d54 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20  ->pLruNext ){.  
10d55 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e      pPage->pLruN
10d56 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20  ext->pLruPrev = 
10d57 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b  pPage->pLruPrev;
10d58 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
10d59 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d  cache1.pLruHead=
10d5a 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20  =pPage ){.      
10d5b 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64  pcache1.pLruHead
10d5c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65   = pPage->pLruNe
10d5d 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  xt;.    }.    if
10d5e 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  ( pcache1.pLruTa
10d5f 69 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20  il==pPage ){.   
10d60 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54     pcache1.pLruT
10d61 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72  ail = pPage->pLr
10d62 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20  uPrev;.    }.   
10d63 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10d64 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d   = 0;.    pPage-
10d65 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20  >pLruPrev = 0;. 
10d66 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65     pPage->pCache
10d67 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b  ->nRecyclable--;
10d68 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52  .  }.}.../*.** R
10d69 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73  emove the page s
10d6a 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72  upplied as an ar
10d6b 67 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20  gument from the 
10d6c 68 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28  hash table .** (
10d6d 50 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73  PCache1.apHash s
10d6e 74 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69  tructure) that i
10d6f 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73  t is currently s
10d70 74 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20  tored in..**.** 
10d71 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  The global mutex
10d72 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
10d73 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
10d74 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73   is called..*/.s
10d75 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10d76 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68  e1RemoveFromHash
10d77 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b  (PgHdr1 *pPage){
10d78 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
10d79 68 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  h;.  PCache1 *pC
10d7a 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43  ache = pPage->pC
10d7b 61 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a  ache;.  PgHdr1 *
10d7c 2a 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67  *pp;..  h = pPag
10d7d 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65  e->iKey % pCache
10d7e 2d 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70  ->nHash;.  for(p
10d7f 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73  p=&pCache->apHas
10d80 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61  h[h]; (*pp)!=pPa
10d81 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70  ge; pp=&(*pp)->p
10d82 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28  Next);.  *pp = (
10d83 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20  *pp)->pNext;..  
10d84 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b  pCache->nPage--;
10d85 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
10d86 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  re are currently
10d87 20 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68   more than pcach
10d88 65 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73  e.nMaxPage pages
10d89 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a   allocated, try.
10d8a 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61  ** to recycle pa
10d8b 67 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68  ges to reduce th
10d8c 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74  e number allocat
10d8d 65 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61  ed to pcache.nMa
10d8e 78 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  xPage..*/.static
10d8f 20 76 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66   void pcache1Enf
10d90 6f 72 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64  orceMaxPage(void
10d91 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
10d92 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10d93 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29  pcache1.mutex) )
10d94 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 61 63 68  ;.  while( pcach
10d95 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e  e1.nCurrentPage>
10d96 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65  pcache1.nMaxPage
10d97 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75   && pcache1.pLru
10d98 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64  Tail ){.    PgHd
10d99 72 31 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e  r1 *p = pcache1.
10d9a 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63  pLruTail;.    pc
10d9b 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b  ache1PinPage(p);
10d9c 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f  .    pcache1Remo
10d9d 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20  veFromHash(p);. 
10d9e 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61     pcache1FreePa
10d9f 67 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ge(p);.  }.}../*
10da0 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20  .** Discard all 
10da1 70 61 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65  pages from cache
10da2 20 70 43 61 63 68 65 20 77 69 74 68 20 61 20 70   pCache with a p
10da3 61 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20  age number (key 
10da4 76 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74  value) .** great
10da5 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
10da6 20 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20   to iLimit. Any 
10da7 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61  pinned pages tha
10da8 74 20 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20  t meet this .** 
10da9 63 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70  criteria are unp
10daa 69 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65  inned before the
10dab 79 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e  y are discarded.
10dac 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61  .**.** The globa
10dad 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  l mutex must be 
10dae 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
10daf 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10db0 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
10db1 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74  d pcache1Truncat
10db2 65 55 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68  eUnsafe(.  PCach
10db3 65 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75  e1 *pCache, .  u
10db4 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d  nsigned int iLim
10db5 69 74 20 0a 29 7b 0a 20 20 54 45 53 54 4f 4e 4c  it .){.  TESTONL
10db6 59 28 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  Y( unsigned int 
10db7 6e 50 61 67 65 20 3d 20 30 3b 20 29 20 20 20 20  nPage = 0; )    
10db8 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73    /* Used to ass
10db9 65 72 74 20 70 43 61 63 68 65 2d 3e 6e 50 61 67  ert pCache->nPag
10dba 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a  e is correct */.
10dbb 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
10dbc 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
10dbd 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
10dbe 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b  cache1.mutex) );
10dbf 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43  .  for(h=0; h<pC
10dc0 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b  ache->nHash; h++
10dc1 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 2a  ){.    PgHdr1 **
10dc2 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70  pp = &pCache->ap
10dc3 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67  Hash[h]; .    Pg
10dc4 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20  Hdr1 *pPage;.   
10dc5 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d   while( (pPage =
10dc6 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 20   *pp)!=0 ){.    
10dc7 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 4b 65    if( pPage->iKe
10dc8 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  y>=iLimit ){.   
10dc9 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61       pCache->nPa
10dca 67 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 2a 70  ge--;.        *p
10dcb 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74  p = pPage->pNext
10dcc 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65  ;.        pcache
10dcd 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b  1PinPage(pPage);
10dce 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31  .        pcache1
10dcf 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b  FreePage(pPage);
10dd0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
10dd1 20 20 20 20 20 20 70 70 20 3d 20 26 70 50 61 67        pp = &pPag
10dd2 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  e->pNext;.      
10dd3 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67    TESTONLY( nPag
10dd4 65 2b 2b 3b 20 29 0a 20 20 20 20 20 20 7d 0a 20  e++; ).      }. 
10dd5 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
10dd6 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65  t( pCache->nPage
10dd7 3d 3d 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a  ==nPage );.}../*
10dd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10dda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ddb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ddc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
10ddd 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33  ******** sqlite3
10dde 5f 70 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20  _pcache Methods 
10ddf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10de1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
10de2 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
10de3 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
10de4 69 74 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69  ite3_pcache.xIni
10de5 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  t method..*/.sta
10de6 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 49  tic int pcache1I
10de7 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  nit(void *NotUse
10de8 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
10de9 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
10dea 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68  .  assert( pcach
10deb 65 31 2e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a  e1.isInit==0 );.
10dec 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65    memset(&pcache
10ded 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61  1, 0, sizeof(pca
10dee 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71  che1));.  if( sq
10def 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
10df0 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a  g.bCoreMutex ){.
10df1 20 20 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65      pcache1.mute
10df2 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  x = sqlite3_mute
10df3 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  x_alloc(SQLITE_M
10df4 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29  UTEX_STATIC_LRU)
10df5 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e  ;.  }.  pcache1.
10df6 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65  isInit = 1;.  re
10df7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
10df8 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
10df9 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
10dfa 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53  qlite3_pcache.xS
10dfb 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a  hutdown method..
10dfc 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
10dfd 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 61 6c   static mutex al
10dfe 6c 6f 63 61 74 65 64 20 69 6e 20 78 49 6e 69 74  located in xInit
10dff 20 64 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 6e 65   does .** not ne
10e00 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a  ed to be freed..
10e01 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
10e02 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76  cache1Shutdown(v
10e03 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  oid *NotUsed){. 
10e04 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
10e05 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73  R(NotUsed);.  as
10e06 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73  sert( pcache1.is
10e07 49 6e 69 74 21 3d 30 20 29 3b 0a 20 20 6d 65 6d  Init!=0 );.  mem
10e08 73 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c  set(&pcache1, 0,
10e09 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29   sizeof(pcache1)
10e0a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
10e0b 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
10e0c 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
10e0d 2e 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e  .xCreate method.
10e0e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  .**.** Allocate 
10e0f 61 20 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a  a new cache..*/.
10e10 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70  static sqlite3_p
10e11 63 61 63 68 65 20 2a 70 63 61 63 68 65 31 43 72  cache *pcache1Cr
10e12 65 61 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c  eate(int szPage,
10e13 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29   int bPurgeable)
10e14 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61  {.  PCache1 *pCa
10e15 63 68 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d  che;..  pCache =
10e16 20 28 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69   (PCache1 *)sqli
10e17 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
10e18 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69  f(PCache1));.  i
10e19 66 28 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20  f( pCache ){.   
10e1a 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20   memset(pCache, 
10e1b 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65  0, sizeof(PCache
10e1c 31 29 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d  1));.    pCache-
10e1d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65  >szPage = szPage
10e1e 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50  ;.    pCache->bP
10e1f 75 72 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72  urgeable = (bPur
10e20 67 65 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b  geable ? 1 : 0);
10e21 0a 20 20 20 20 69 66 28 20 62 50 75 72 67 65 61  .    if( bPurgea
10e22 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61  ble ){.      pCa
10e23 63 68 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a  che->nMin = 10;.
10e24 20 20 20 20 20 20 70 63 61 63 68 65 31 45 6e 74        pcache1Ent
10e25 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20  erMutex();.     
10e26 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67   pcache1.nMinPag
10e27 65 20 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69  e += pCache->nMi
10e28 6e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  n;.      pcache1
10e29 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
10e2a 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
10e2b 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65   (sqlite3_pcache
10e2c 20 2a 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a   *)pCache;.}../*
10e2d 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
10e2e 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
10e2f 33 5f 70 63 61 63 68 65 2e 78 43 61 63 68 65 73  3_pcache.xCaches
10e30 69 7a 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a  ize method. .**.
10e31 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  ** Configure the
10e32 20 63 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69   cache_size limi
10e33 74 20 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a  t for a cache..*
10e34 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
10e35 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 28 73  ache1Cachesize(s
10e36 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70  qlite3_pcache *p
10e37 2c 20 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50  , int nMax){.  P
10e38 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d  Cache1 *pCache =
10e39 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20   (PCache1 *)p;. 
10e3a 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75   if( pCache->bPu
10e3b 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70  rgeable ){.    p
10e3c 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
10e3d 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e  ();.    pcache1.
10e3e 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61  nMaxPage += (nMa
10e3f 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78  x - pCache->nMax
10e40 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e  );.    pCache->n
10e41 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20  Max = nMax;.    
10e42 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61  pcache1EnforceMa
10e43 78 50 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61  xPage();.    pca
10e44 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10e45 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
10e46 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
10e47 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
10e48 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d  che.xPagecount m
10e49 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69  ethod. .*/.stati
10e4a 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 61 67  c int pcache1Pag
10e4b 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70  ecount(sqlite3_p
10e4c 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74  cache *p){.  int
10e4d 20 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74   n;.  pcache1Ent
10e4e 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d  erMutex();.  n =
10e4f 20 28 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d   ((PCache1 *)p)-
10e50 3e 6e 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65  >nPage;.  pcache
10e51 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  1LeaveMutex();. 
10e52 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a   return n;.}../*
10e53 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
10e54 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
10e55 33 5f 70 63 61 63 68 65 2e 78 46 65 74 63 68 20  3_pcache.xFetch 
10e56 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46  method. .**.** F
10e57 65 74 63 68 20 61 20 70 61 67 65 20 62 79 20 6b  etch a page by k
10e58 65 79 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  ey value..**.** 
10e59 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  Whether or not a
10e5a 20 6e 65 77 20 70 61 67 65 20 6d 61 79 20 62 65   new page may be
10e5b 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68   allocated by th
10e5c 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65  is function depe
10e5d 6e 64 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61  nds on.** the va
10e5e 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 61 74  lue of the creat
10e5f 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 20  eFlag argument. 
10e60 20 30 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20   0 means do not 
10e61 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a  allocate a new.*
10e62 2a 20 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73  * page.  1 means
10e63 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
10e64 70 61 67 65 20 69 66 20 73 70 61 63 65 20 69 73  page if space is
10e65 20 65 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c   easily availabl
10e66 65 2e 20 20 32 20 0a 2a 2a 20 6d 65 61 6e 73 20  e.  2 .** means 
10e67 74 6f 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61  to try really ha
10e68 72 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  rd to allocate a
10e69 20 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   new page..**.**
10e6a 20 46 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67 65   For a non-purge
10e6b 61 62 6c 65 20 63 61 63 68 65 20 28 61 20 63 61  able cache (a ca
10e6c 63 68 65 20 75 73 65 64 20 61 73 20 74 68 65 20  che used as the 
10e6d 73 74 6f 72 61 67 65 20 66 6f 72 20 61 6e 20 69  storage for an i
10e6e 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61  n-memory.** data
10e6f 62 61 73 65 29 20 74 68 65 72 65 20 69 73 20 72  base) there is r
10e70 65 61 6c 6c 79 20 6e 6f 20 64 69 66 66 65 72 65  eally no differe
10e71 6e 63 65 20 62 65 74 77 65 65 6e 20 63 72 65 61  nce between crea
10e72 74 65 46 6c 61 67 20 31 20 61 6e 64 20 32 2e 20  teFlag 1 and 2. 
10e73 20 53 6f 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69   So.** the calli
10e74 6e 67 20 66 75 6e 63 74 69 6f 6e 20 28 70 63 61  ng function (pca
10e75 63 68 65 2e 63 29 20 77 69 6c 6c 20 6e 65 76 65  che.c) will neve
10e76 72 20 68 61 76 65 20 61 20 63 72 65 61 74 65 46  r have a createF
10e77 6c 61 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a 20 61  lag of 1 on.** a
10e78 20 6e 6f 6e 2d 70 75 72 67 61 62 6c 65 20 63 61   non-purgable ca
10e79 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  che..**.** There
10e7a 20 61 72 65 20 74 68 72 65 65 20 64 69 66 66 65   are three diffe
10e7b 72 65 6e 74 20 61 70 70 72 6f 61 63 68 65 73 20  rent approaches 
10e7c 74 6f 20 6f 62 74 61 69 6e 69 6e 67 20 73 70 61  to obtaining spa
10e7d 63 65 20 66 6f 72 20 61 20 70 61 67 65 2c 0a 2a  ce for a page,.*
10e7e 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  * depending on t
10e7f 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61  he value of para
10e80 6d 65 74 65 72 20 63 72 65 61 74 65 46 6c 61 67  meter createFlag
10e81 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 30   (which may be 0
10e82 2c 20 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a  , 1 or 2)..**.**
10e83 20 20 20 31 2e 20 52 65 67 61 72 64 6c 65 73 73     1. Regardless
10e84 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66   of the value of
10e85 20 63 72 65 61 74 65 46 6c 61 67 2c 20 74 68 65   createFlag, the
10e86 20 63 61 63 68 65 20 69 73 20 73 65 61 72 63 68   cache is search
10e87 65 64 20 66 6f 72 20 61 20 0a 2a 2a 20 20 20 20  ed for a .**    
10e88 20 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65    copy of the re
10e89 71 75 65 73 74 65 64 20 70 61 67 65 2e 20 49 66  quested page. If
10e8a 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 69   one is found, i
10e8b 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  t is returned..*
10e8c 2a 0a 2a 2a 20 20 20 32 2e 20 49 66 20 63 72 65  *.**   2. If cre
10e8d 61 74 65 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74  ateFlag==0 and t
10e8e 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  he page is not a
10e8f 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61  lready in the ca
10e90 63 68 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20  che, NULL is.** 
10e91 20 20 20 20 20 72 65 74 75 72 6e 65 64 2e 0a 2a       returned..*
10e92 2a 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65  *.**   3. If cre
10e93 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e  ateFlag is 1, an
10e94 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  d the page is no
10e95 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  t already in the
10e96 20 63 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20   cache,.**      
10e97 61 6e 64 20 69 66 20 65 69 74 68 65 72 20 6f 66  and if either of
10e98 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
10e99 72 65 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20  re true, return 
10e9a 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  NULL:.**.**     
10e9b 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62 65 72    (a) the number
10e9c 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64   of pages pinned
10e9d 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 73   by the cache is
10e9e 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a   greater than.**
10e9f 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68             PCach
10ea0 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20  e1.nMax, or.**  
10ea1 20 20 20 20 20 28 62 29 20 74 68 65 20 6e 75 6d       (b) the num
10ea2 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e  ber of pages pin
10ea3 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65  ned by the cache
10ea4 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
10ea5 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68  .**           th
10ea6 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f  e sum of nMax fo
10ea7 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20  r all purgeable 
10ea8 63 61 63 68 65 73 2c 20 6c 65 73 73 20 74 68 65  caches, less the
10ea9 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20   sum of .**     
10eaa 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61        nMin for a
10eab 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 65 61 62  ll other purgeab
10eac 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a  le caches. .**.*
10ead 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f  *   4. If none o
10eae 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65  f the first thre
10eaf 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70  e conditions app
10eb0 6c 79 20 61 6e 64 20 74 68 65 20 63 61 63 68 65  ly and the cache
10eb1 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20   is marked.**   
10eb2 20 20 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c     as purgeable,
10eb3 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74   and if one of t
10eb4 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
10eb5 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  true:.**.**     
10eb6 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72    (a) The number
10eb7 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61   of pages alloca
10eb8 74 65 64 20 66 6f 72 20 74 68 65 20 63 61 63 68  ted for the cach
10eb9 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a  e is already .**
10eba 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68             PCach
10ebb 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a  e1.nMax, or.**.*
10ebc 2a 20 20 20 20 20 20 20 28 62 29 20 54 68 65 20  *       (b) The 
10ebd 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
10ebe 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 6c  allocated for al
10ebf 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68  l purgeable cach
10ec0 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  es is.**        
10ec1 20 20 20 61 6c 72 65 61 64 79 20 65 71 75 61 6c     already equal
10ec2 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74   to or greater t
10ec3 68 61 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 6e  han the sum of n
10ec4 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20  Max for all.**  
10ec5 20 20 20 20 20 20 20 20 20 70 75 72 67 65 61 62           purgeab
10ec6 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a  le caches,.**.**
10ec7 20 20 20 20 20 20 74 68 65 6e 20 61 74 74 65 6d        then attem
10ec8 70 74 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20  pt to recycle a 
10ec9 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 4c 52  page from the LR
10eca 55 20 6c 69 73 74 2e 20 49 66 20 69 74 20 69 73  U list. If it is
10ecb 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 20   the right.**   
10ecc 20 20 20 73 69 7a 65 2c 20 72 65 74 75 72 6e 20     size, return 
10ecd 74 68 65 20 72 65 63 79 63 6c 65 64 20 62 75 66  the recycled buf
10ece 66 65 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  fer. Otherwise, 
10ecf 66 72 65 65 20 74 68 65 20 62 75 66 66 65 72 20  free the buffer 
10ed0 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63  and.**      proc
10ed1 65 65 64 20 74 6f 20 73 74 65 70 20 35 2e 20 0a  eed to step 5. .
10ed2 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72  **.**   5. Other
10ed3 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61  wise, allocate a
10ed4 6e 64 20 72 65 74 75 72 6e 20 61 20 6e 65 77 20  nd return a new 
10ed5 70 61 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a  page buffer..*/.
10ed6 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61  static void *pca
10ed7 63 68 65 31 46 65 74 63 68 28 73 71 6c 69 74 65  che1Fetch(sqlite
10ed8 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73  3_pcache *p, uns
10ed9 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c 20  igned int iKey, 
10eda 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 7b  int createFlag){
10edb 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
10edc 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68  nPinned;.  PCach
10edd 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43  e1 *pCache = (PC
10ede 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48  ache1 *)p;.  PgH
10edf 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a  dr1 *pPage = 0;.
10ee0 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68  .  assert( pCach
10ee1 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c  e->bPurgeable ||
10ee2 20 63 72 65 61 74 65 46 6c 61 67 21 3d 31 20 29   createFlag!=1 )
10ee3 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10ee4 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63  Mutex();.  if( c
10ee5 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73  reateFlag==1 ) s
10ee6 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
10ee7 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a  nMalloc();..  /*
10ee8 20 53 65 61 72 63 68 20 74 68 65 20 68 61 73 68   Search the hash
10ee9 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78   table for an ex
10eea 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f  isting entry. */
10eeb 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e  .  if( pCache->n
10eec 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e  Hash>0 ){.    un
10eed 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69  signed int h = i
10eee 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48  Key % pCache->nH
10eef 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61  ash;.    for(pPa
10ef0 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73  ge=pCache->apHas
10ef1 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61  h[h]; pPage&&pPa
10ef2 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20  ge->iKey!=iKey; 
10ef3 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65  pPage=pPage->pNe
10ef4 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  xt);.  }..  if( 
10ef5 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46  pPage || createF
10ef6 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63  lag==0 ){.    pc
10ef7 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61  ache1PinPage(pPa
10ef8 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65  ge);.    goto fe
10ef9 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  tch_out;.  }..  
10efa 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68 65 61  /* Step 3 of hea
10efb 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a  der comment. */.
10efc 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63    nPinned = pCac
10efd 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63  he->nPage - pCac
10efe 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b  he->nRecyclable;
10eff 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61  .  if( createFla
10f00 67 3d 3d 31 20 26 26 20 28 0a 20 20 20 20 20 20  g==1 && (.      
10f01 20 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 61 63    nPinned>=(pcac
10f02 68 65 31 2e 6e 4d 61 78 50 61 67 65 2b 70 43 61  he1.nMaxPage+pCa
10f03 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 68 65  che->nMin-pcache
10f04 31 2e 6e 4d 69 6e 50 61 67 65 29 0a 20 20 20 20  1.nMinPage).    
10f05 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 43   || nPinned>=(pC
10f06 61 63 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f  ache->nMax * 9 /
10f07 20 31 30 29 0a 20 20 29 29 7b 0a 20 20 20 20 67   10).  )){.    g
10f08 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20  oto fetch_out;. 
10f09 20 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65   }..  if( pCache
10f0a 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d  ->nPage>=pCache-
10f0b 3e 6e 48 61 73 68 20 26 26 20 70 63 61 63 68 65  >nHash && pcache
10f0c 31 52 65 73 69 7a 65 48 61 73 68 28 70 43 61 63  1ResizeHash(pCac
10f0d 68 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  he) ){.    goto 
10f0e 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  fetch_out;.  }..
10f0f 20 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79    /* Step 4. Try
10f10 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61   to recycle a pa
10f11 67 65 20 62 75 66 66 65 72 20 69 66 20 61 70 70  ge buffer if app
10f12 72 6f 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69  ropriate. */.  i
10f13 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67  f( pCache->bPurg
10f14 65 61 62 6c 65 20 26 26 20 70 63 61 63 68 65 31  eable && pcache1
10f15 2e 70 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20  .pLruTail && (. 
10f16 20 20 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61      (pCache->nPa
10f17 67 65 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d  ge+1>=pCache->nM
10f18 61 78 29 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e  ax) || pcache1.n
10f19 43 75 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61  CurrentPage>=pca
10f1a 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20  che1.nMaxPage.  
10f1b 29 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20  )){.    pPage = 
10f1c 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c  pcache1.pLruTail
10f1d 3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d  ;.    pcache1Rem
10f1e 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67  oveFromHash(pPag
10f1f 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50  e);.    pcache1P
10f20 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  inPage(pPage);. 
10f21 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43     if( pPage->pC
10f22 61 63 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43  ache->szPage!=pC
10f23 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a  ache->szPage ){.
10f24 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65        pcache1Fre
10f25 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
10f26 20 20 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20      pPage = 0;. 
10f27 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
10f28 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74  pcache1.nCurrent
10f29 50 61 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e  Page -= (pPage->
10f2a 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
10f2b 6c 65 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75  le - pCache->bPu
10f2c 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a  rgeable);.    }.
10f2d 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35    }..  /* Step 5
10f2e 2e 20 49 66 20 61 20 75 73 61 62 6c 65 20 70 61  . If a usable pa
10f2f 67 65 20 62 75 66 66 65 72 20 68 61 73 20 73 74  ge buffer has st
10f30 69 6c 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75  ill not been fou
10f31 6e 64 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70  nd, .  ** attemp
10f32 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  t to allocate a 
10f33 6e 65 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20  new one. .  */. 
10f34 20 69 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20   if( !pPage ){. 
10f35 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68     pPage = pcach
10f36 65 31 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63  e1AllocPage(pCac
10f37 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  he);.  }..  if( 
10f38 70 50 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73  pPage ){.    uns
10f39 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b  igned int h = iK
10f3a 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61  ey % pCache->nHa
10f3b 73 68 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  sh;.    pCache->
10f3c 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61  nPage++;.    pPa
10f3d 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b  ge->iKey = iKey;
10f3e 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78  .    pPage->pNex
10f3f 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61  t = pCache->apHa
10f40 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65  sh[h];.    pPage
10f41 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68  ->pCache = pCach
10f42 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c  e;.    pPage->pL
10f43 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  ruPrev = 0;.    
10f44 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
10f45 3d 20 30 3b 0a 20 20 20 20 2a 28 76 6f 69 64 20  = 0;.    *(void 
10f46 2a 2a 29 28 50 47 48 44 52 31 5f 54 4f 5f 50 41  **)(PGHDR1_TO_PA
10f47 47 45 28 70 50 61 67 65 29 29 20 3d 20 30 3b 0a  GE(pPage)) = 0;.
10f48 20 20 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61      pCache->apHa
10f49 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20  sh[h] = pPage;. 
10f4a 20 7d 0a 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20   }..fetch_out:. 
10f4b 20 69 66 28 20 70 50 61 67 65 20 26 26 20 69 4b   if( pPage && iK
10f4c 65 79 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b  ey>pCache->iMaxK
10f4d 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65  ey ){.    pCache
10f4e 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79  ->iMaxKey = iKey
10f4f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 72 65 61  ;.  }.  if( crea
10f50 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69  teFlag==1 ) sqli
10f51 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
10f52 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c  oc();.  pcache1L
10f53 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
10f54 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f 20 50  eturn (pPage ? P
10f55 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50  GHDR1_TO_PAGE(pP
10f56 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f  age) : 0);.}.../
10f57 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
10f58 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
10f59 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 6e  e3_pcache.xUnpin
10f5a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d   method..**.** M
10f5b 61 72 6b 20 61 20 70 61 67 65 20 61 73 20 75 6e  ark a page as un
10f5c 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c 65  pinned (eligible
10f5d 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f 75   for asynchronou
10f5e 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f  s recycling)..*/
10f5f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10f60 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 65  che1Unpin(sqlite
10f61 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76 6f 69  3_pcache *p, voi
10f62 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 73  d *pPg, int reus
10f63 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 43  eUnlikely){.  PC
10f64 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20  ache1 *pCache = 
10f65 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20  (PCache1 *)p;.  
10f66 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20  PgHdr1 *pPage = 
10f67 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70  PAGE_TO_PGHDR1(p
10f68 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 0a 20  Cache, pPg);. . 
10f69 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
10f6a 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29  pCache==pCache )
10f6b 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10f6c 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49  Mutex();..  /* I
10f6d 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f  t is an error to
10f6e 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
10f6f 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 65 20  ion if the page 
10f70 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a  is already .  **
10f71 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f   part of the glo
10f72 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20  bal LRU list..  
10f73 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
10f74 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20  ge->pLruPrev==0 
10f75 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65  && pPage->pLruNe
10f76 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  xt==0 );.  asser
10f77 74 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48  t( pcache1.pLruH
10f78 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 70 63  ead!=pPage && pc
10f79 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d  ache1.pLruTail!=
10f7a 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20  pPage );..  if( 
10f7b 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c  reuseUnlikely ||
10f7c 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
10f7d 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d  tPage>pcache1.nM
10f7e 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63  axPage ){.    pc
10f7f 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48  ache1RemoveFromH
10f80 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20  ash(pPage);.    
10f81 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28  pcache1FreePage(
10f82 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b  pPage);.  }else{
10f83 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20  .    /* Add the 
10f84 70 61 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62  page to the glob
10f85 61 6c 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72  al LRU list. Nor
10f86 6d 61 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20  mally, the page 
10f87 69 73 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20  is added to.    
10f88 2a 2a 20 74 68 65 20 68 65 61 64 20 6f 66 20 74  ** the head of t
10f89 68 65 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61  he list (last pa
10f8a 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65  ge to be recycle
10f8b 64 29 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20  d). However, if 
10f8c 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73  the .    ** reus
10f8d 65 55 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70  eUnlikely flag p
10f8e 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75  assed to this fu
10f8f 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20  nction is true, 
10f90 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65  the page is adde
10f91 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  d.    ** to the 
10f92 74 61 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74  tail of the list
10f93 20 28 66 69 72 73 74 20 70 61 67 65 20 74 6f 20   (first page to 
10f94 62 65 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20  be recycled)..  
10f95 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61    */.    if( pca
10f96 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b  che1.pLruHead ){
10f97 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70  .      pcache1.p
10f98 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65  LruHead->pLruPre
10f99 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20  v = pPage;.     
10f9a 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10f9b 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48   = pcache1.pLruH
10f9c 65 61 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68  ead;.      pcach
10f9d 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50  e1.pLruHead = pP
10f9e 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  age;.    }else{.
10f9f 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c        pcache1.pL
10fa0 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a  ruTail = pPage;.
10fa1 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c        pcache1.pL
10fa2 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a  ruHead = pPage;.
10fa3 20 20 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65      }.    pCache
10fa4 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b  ->nRecyclable++;
10fa5 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c  .  }..  pcache1L
10fa6 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a  eaveMutex();.}..
10fa7 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
10fa8 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
10fa9 74 65 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65  te3_pcache.xReke
10faa 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74  y method. .*/.st
10fab 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
10fac 31 52 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65  1Rekey(.  sqlite
10fad 33 5f 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76  3_pcache *p,.  v
10fae 6f 69 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69  oid *pPg,.  unsi
10faf 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20  gned int iOld,. 
10fb0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e   unsigned int iN
10fb1 65 77 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20  ew.){.  PCache1 
10fb2 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68  *pCache = (PCach
10fb3 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31  e1 *)p;.  PgHdr1
10fb4 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54   *pPage = PAGE_T
10fb5 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c  O_PGHDR1(pCache,
10fb6 20 70 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20   pPg);.  PgHdr1 
10fb7 2a 2a 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64  **pp;.  unsigned
10fb8 20 69 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72   int h; .  asser
10fb9 74 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d  t( pPage->iKey==
10fba 69 4f 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74  iOld );.  assert
10fbb 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d  ( pPage->pCache=
10fbc 3d 70 43 61 63 68 65 20 29 3b 0a 0a 20 20 70 63  =pCache );..  pc
10fbd 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
10fbe 29 3b 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70  );..  h = iOld%p
10fbf 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20  Cache->nHash;.  
10fc0 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70  pp = &pCache->ap
10fc1 48 61 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65  Hash[h];.  while
10fc2 28 20 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29  ( (*pp)!=pPage )
10fc3 7b 0a 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70  {.    pp = &(*pp
10fc4 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  )->pNext;.  }.  
10fc5 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65  *pp = pPage->pNe
10fc6 78 74 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25  xt;..  h = iNew%
10fc7 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20  pCache->nHash;. 
10fc8 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69   pPage->iKey = i
10fc9 4e 65 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e  New;.  pPage->pN
10fca 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70  ext = pCache->ap
10fcb 48 61 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68  Hash[h];.  pCach
10fcc 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70  e->apHash[h] = p
10fcd 50 61 67 65 3b 0a 20 20 69 66 28 20 69 4e 65 77  Page;.  if( iNew
10fce 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79  >pCache->iMaxKey
10fcf 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e   ){.    pCache->
10fd0 69 4d 61 78 4b 65 79 20 3d 20 69 4e 65 77 3b 0a  iMaxKey = iNew;.
10fd1 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c 65    }..  pcache1Le
10fd2 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f  aveMutex();.}../
10fd3 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
10fd4 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
10fd5 65 33 5f 70 63 61 63 68 65 2e 78 54 72 75 6e 63  e3_pcache.xTrunc
10fd6 61 74 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a  ate method. .**.
10fd7 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 75  ** Discard all u
10fd8 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 69 6e  npinned pages in
10fd9 20 74 68 65 20 63 61 63 68 65 20 77 69 74 68 20   the cache with 
10fda 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 65 71  a page number eq
10fdb 75 61 6c 20 74 6f 0a 2a 2a 20 6f 72 20 67 72 65  ual to.** or gre
10fdc 61 74 65 72 20 74 68 61 6e 20 70 61 72 61 6d 65  ater than parame
10fdd 74 65 72 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20  ter iLimit. Any 
10fde 70 69 6e 6e 65 64 20 70 61 67 65 73 20 77 69 74  pinned pages wit
10fdf 68 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a  h a page number.
10fe0 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 67  ** equal to or g
10fe1 72 65 61 74 65 72 20 74 68 61 6e 20 69 4c 69 6d  reater than iLim
10fe2 69 74 20 61 72 65 20 69 6d 70 6c 69 63 69 74 6c  it are implicitl
10fe3 79 20 75 6e 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73  y unpinned..*/.s
10fe4 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10fe5 65 31 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  e1Truncate(sqlit
10fe6 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e  e3_pcache *p, un
10fe7 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69  signed int iLimi
10fe8 74 29 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70  t){.  PCache1 *p
10fe9 43 61 63 68 65 20 3d 20 28 50 43 61 63 68 65 31  Cache = (PCache1
10fea 20 2a 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45   *)p;.  pcache1E
10feb 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69  nterMutex();.  i
10fec 66 28 20 69 4c 69 6d 69 74 3c 3d 70 43 61 63 68  f( iLimit<=pCach
10fed 65 2d 3e 69 4d 61 78 4b 65 79 20 29 7b 0a 20 20  e->iMaxKey ){.  
10fee 20 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74    pcache1Truncat
10fef 65 55 6e 73 61 66 65 28 70 43 61 63 68 65 2c 20  eUnsafe(pCache, 
10ff0 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 70 43 61  iLimit);.    pCa
10ff1 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69  che->iMaxKey = i
10ff2 4c 69 6d 69 74 2d 31 3b 0a 20 20 7d 0a 20 20 70  Limit-1;.  }.  p
10ff3 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
10ff4 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70  ();.}../*.** Imp
10ff5 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
10ff6 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68  he sqlite3_pcach
10ff7 65 2e 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f  e.xDestroy metho
10ff8 64 2e 20 0a 2a 2a 0a 2a 2a 20 44 65 73 74 72 6f  d. .**.** Destro
10ff9 79 20 61 20 63 61 63 68 65 20 61 6c 6c 6f 63 61  y a cache alloca
10ffa 74 65 64 20 75 73 69 6e 67 20 70 63 61 63 68 65  ted using pcache
10ffb 31 43 72 65 61 74 65 28 29 2e 0a 2a 2f 0a 73 74  1Create()..*/.st
10ffc 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
10ffd 31 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33  1Destroy(sqlite3
10ffe 5f 70 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 50  _pcache *p){.  P
10fff 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d  Cache1 *pCache =
11000 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20   (PCache1 *)p;. 
11001 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74   pcache1EnterMut
11002 65 78 28 29 3b 0a 20 20 70 63 61 63 68 65 31 54  ex();.  pcache1T
11003 72 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43  runcateUnsafe(pC
11004 61 63 68 65 2c 20 30 29 3b 0a 20 20 70 63 61 63  ache, 0);.  pcac
11005 68 65 31 2e 6e 4d 61 78 50 61 67 65 20 2d 3d 20  he1.nMaxPage -= 
11006 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 20 20  pCache->nMax;.  
11007 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65  pcache1.nMinPage
11008 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e   -= pCache->nMin
11009 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 66 6f 72  ;.  pcache1Enfor
1100a 63 65 4d 61 78 50 61 67 65 28 29 3b 0a 20 20 70  ceMaxPage();.  p
1100b 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
1100c 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  ();.  sqlite3_fr
1100d 65 65 28 70 43 61 63 68 65 2d 3e 61 70 48 61 73  ee(pCache->apHas
1100e 68 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  h);.  sqlite3_fr
1100f 65 65 28 70 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f  ee(pCache);.}../
11010 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
11011 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72  on is called dur
11012 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ing initializati
11013 6f 6e 20 28 73 71 6c 69 74 65 33 5f 69 6e 69 74  on (sqlite3_init
11014 69 61 6c 69 7a 65 28 29 29 20 74 6f 0a 2a 2a 20  ialize()) to.** 
11015 69 6e 73 74 61 6c 6c 20 74 68 65 20 64 65 66 61  install the defa
11016 75 6c 74 20 70 6c 75 67 67 61 62 6c 65 20 63 61  ult pluggable ca
11017 63 68 65 20 6d 6f 64 75 6c 65 2c 20 61 73 73 75  che module, assu
11018 6d 69 6e 67 20 74 68 65 20 75 73 65 72 20 68 61  ming the user ha
11019 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64 79  s not.** already
1101a 20 70 72 6f 76 69 64 65 64 20 61 6e 20 61 6c 74   provided an alt
1101b 65 72 6e 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c  ernative..*/.SQL
1101c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1101d 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65   sqlite3PCacheSe
1101e 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a  tDefault(void){.
1101f 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
11020 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 20  _pcache_methods 
11021 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d  defaultMethods =
11022 20 7b 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20   {.    0,       
11023 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11024 2f 2a 20 70 41 72 67 20 2a 2f 0a 20 20 20 20 70  /* pArg */.    p
11025 63 61 63 68 65 31 49 6e 69 74 2c 20 20 20 20 20  cache1Init,     
11026 20 20 20 20 20 20 20 20 2f 2a 20 78 49 6e 69 74          /* xInit
11027 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 53   */.    pcache1S
11028 68 75 74 64 6f 77 6e 2c 20 20 20 20 20 20 20 20  hutdown,        
11029 20 2f 2a 20 78 53 68 75 74 64 6f 77 6e 20 2a 2f   /* xShutdown */
1102a 0a 20 20 20 20 70 63 61 63 68 65 31 43 72 65 61  .    pcache1Crea
1102b 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  te,           /*
1102c 20 78 43 72 65 61 74 65 20 2a 2f 0a 20 20 20 20   xCreate */.    
1102d 70 63 61 63 68 65 31 43 61 63 68 65 73 69 7a 65  pcache1Cachesize
1102e 2c 20 20 20 20 20 20 20 20 2f 2a 20 78 43 61 63  ,        /* xCac
1102f 68 65 73 69 7a 65 20 2a 2f 0a 20 20 20 20 70 63  hesize */.    pc
11030 61 63 68 65 31 50 61 67 65 63 6f 75 6e 74 2c 20  ache1Pagecount, 
11031 20 20 20 20 20 20 20 2f 2a 20 78 50 61 67 65 63         /* xPagec
11032 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 70 63 61 63  ount */.    pcac
11033 68 65 31 46 65 74 63 68 2c 20 20 20 20 20 20 20  he1Fetch,       
11034 20 20 20 20 20 2f 2a 20 78 46 65 74 63 68 20 2a       /* xFetch *
11035 2f 0a 20 20 20 20 70 63 61 63 68 65 31 55 6e 70  /.    pcache1Unp
11036 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  in,            /
11037 2a 20 78 55 6e 70 69 6e 20 2a 2f 0a 20 20 20 20  * xUnpin */.    
11038 70 63 61 63 68 65 31 52 65 6b 65 79 2c 20 20 20  pcache1Rekey,   
11039 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 6b           /* xRek
1103a 65 79 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  ey */.    pcache
1103b 31 54 72 75 6e 63 61 74 65 2c 20 20 20 20 20 20  1Truncate,      
1103c 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65 20     /* xTruncate 
1103d 2a 2f 0a 20 20 20 20 70 63 61 63 68 65 31 44 65  */.    pcache1De
1103e 73 74 72 6f 79 20 20 20 20 20 20 20 20 20 20 20  stroy           
1103f 2f 2a 20 78 44 65 73 74 72 6f 79 20 2a 2f 0a 20  /* xDestroy */. 
11040 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f   };.  sqlite3_co
11041 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46  nfig(SQLITE_CONF
11042 49 47 5f 50 43 41 43 48 45 2c 20 26 64 65 66 61  IG_PCACHE, &defa
11043 75 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a  ultMethods);.}..
11044 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
11045 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41  ABLE_MEMORY_MANA
11046 47 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 54 68 69  GEMENT./*.** Thi
11047 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
11048 6c 6c 65 64 20 74 6f 20 66 72 65 65 20 73 75 70  lled to free sup
11049 65 72 66 6c 75 6f 75 73 20 64 79 6e 61 6d 69 63  erfluous dynamic
1104a 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d  ally allocated m
1104b 65 6d 6f 72 79 0a 2a 2a 20 68 65 6c 64 20 62 79  emory.** held by
1104c 20 74 68 65 20 70 61 67 65 72 20 73 79 73 74 65   the pager syste
1104d 6d 2e 20 4d 65 6d 6f 72 79 20 69 6e 20 75 73 65  m. Memory in use
1104e 20 62 79 20 61 6e 79 20 53 51 4c 69 74 65 20 70   by any SQLite p
1104f 61 67 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a 2a  ager allocated.*
11050 2a 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74  * by the current
11051 20 74 68 72 65 61 64 20 6d 61 79 20 62 65 20 73   thread may be s
11052 71 6c 69 74 65 33 5f 66 72 65 65 28 29 65 64 2e  qlite3_free()ed.
11053 0a 2a 2a 0a 2a 2a 20 6e 52 65 71 20 69 73 20 74  .**.** nReq is t
11054 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
11055 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 71  es of memory req
11056 75 69 72 65 64 2e 20 4f 6e 63 65 20 74 68 69 73  uired. Once this
11057 20 6d 75 63 68 20 68 61 73 0a 2a 2a 20 62 65 65   much has.** bee
11058 6e 20 72 65 6c 65 61 73 65 64 2c 20 74 68 65 20  n released, the 
11059 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
1105a 2e 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c  . The return val
1105b 75 65 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20  ue is the total 
1105c 6e 75 6d 62 65 72 20 0a 2a 2a 20 6f 66 20 62 79  number .** of by
1105d 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65  tes of memory re
1105e 6c 65 61 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  leased..*/.SQLIT
1105f 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
11060 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61  lite3PcacheRelea
11061 73 65 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 52 65  seMemory(int nRe
11062 71 29 7b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20  q){.  int nFree 
11063 3d 20 30 3b 0a 20 20 69 66 28 20 70 63 61 63 68  = 0;.  if( pcach
11064 65 31 2e 70 53 74 61 72 74 3d 3d 30 20 29 7b 0a  e1.pStart==0 ){.
11065 20 20 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20      PgHdr1 *p;. 
11066 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d     pcache1EnterM
11067 75 74 65 78 28 29 3b 0a 20 20 20 20 77 68 69 6c  utex();.    whil
11068 65 28 20 28 6e 52 65 71 3c 30 20 7c 7c 20 6e 46  e( (nReq<0 || nF
11069 72 65 65 3c 6e 52 65 71 29 20 26 26 20 28 70 3d  ree<nReq) && (p=
1106a 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c  pcache1.pLruTail
1106b 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 72 65 65  ) ){.      nFree
1106c 20 2b 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f   += sqlite3Mallo
1106d 63 53 69 7a 65 28 50 47 48 44 52 31 5f 54 4f 5f  cSize(PGHDR1_TO_
1106e 50 41 47 45 28 70 29 29 3b 0a 20 20 20 20 20 20  PAGE(p));.      
1106f 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70  pcache1PinPage(p
11070 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  );.      pcache1
11071 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28 70  RemoveFromHash(p
11072 29 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  );.      pcache1
11073 46 72 65 65 50 61 67 65 28 70 29 3b 0a 20 20 20  FreePage(p);.   
11074 20 7d 0a 20 20 20 20 70 63 61 63 68 65 31 4c 65   }.    pcache1Le
11075 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a  aveMutex();.  }.
11076 20 20 72 65 74 75 72 6e 20 6e 46 72 65 65 3b 0a    return nFree;.
11077 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
11078 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59  TE_ENABLE_MEMORY
11079 5f 4d 41 4e 41 47 45 4d 45 4e 54 20 2a 2f 0a 0a  _MANAGEMENT */..
1107a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
1107b 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ST./*.** This fu
1107c 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 62  nction is used b
1107d 79 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65  y test procedure
1107e 73 20 74 6f 20 69 6e 73 70 65 63 74 20 74 68 65  s to inspect the
1107f 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 0a   internal state.
11080 2a 2a 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c  ** of the global
11081 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54   cache..*/.SQLIT
11082 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
11083 71 6c 69 74 65 33 50 63 61 63 68 65 53 74 61 74  qlite3PcacheStat
11084 73 28 0a 20 20 69 6e 74 20 2a 70 6e 43 75 72 72  s(.  int *pnCurr
11085 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20 4f 55 54  ent,      /* OUT
11086 3a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  : Total number o
11087 66 20 70 61 67 65 73 20 63 61 63 68 65 64 20 2a  f pages cached *
11088 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 61 78 2c 20  /.  int *pnMax, 
11089 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
1108a 20 47 6c 6f 62 61 6c 20 6d 61 78 69 6d 75 6d 20   Global maximum 
1108b 63 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20  cache size */.  
1108c 69 6e 74 20 2a 70 6e 4d 69 6e 2c 20 20 20 20 20  int *pnMin,     
1108d 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 75 6d       /* OUT: Sum
1108e 20 6f 66 20 50 43 61 63 68 65 31 2e 6e 4d 69 6e   of PCache1.nMin
1108f 20 66 6f 72 20 70 75 72 67 65 61 62 6c 65 20 63   for purgeable c
11090 61 63 68 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  aches */.  int *
11091 70 6e 52 65 63 79 63 6c 61 62 6c 65 20 20 20 20  pnRecyclable    
11092 2f 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75  /* OUT: Total nu
11093 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 76  mber of pages av
11094 61 69 6c 61 62 6c 65 20 66 6f 72 20 72 65 63 79  ailable for recy
11095 63 6c 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50 67  cling */.){.  Pg
11096 48 64 72 31 20 2a 70 3b 0a 20 20 69 6e 74 20 6e  Hdr1 *p;.  int n
11097 52 65 63 79 63 6c 61 62 6c 65 20 3d 20 30 3b 0a  Recyclable = 0;.
11098 20 20 66 6f 72 28 70 3d 70 63 61 63 68 65 31 2e    for(p=pcache1.
11099 70 4c 72 75 48 65 61 64 3b 20 70 3b 20 70 3d 70  pLruHead; p; p=p
1109a 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a 20 20 20  ->pLruNext){.   
1109b 20 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b 0a   nRecyclable++;.
1109c 20 20 7d 0a 20 20 2a 70 6e 43 75 72 72 65 6e 74    }.  *pnCurrent
1109d 20 3d 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72   = pcache1.nCurr
1109e 65 6e 74 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 61  entPage;.  *pnMa
1109f 78 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d 61 78  x = pcache1.nMax
110a0 50 61 67 65 3b 0a 20 20 2a 70 6e 4d 69 6e 20 3d  Page;.  *pnMin =
110a1 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67   pcache1.nMinPag
110a2 65 3b 0a 20 20 2a 70 6e 52 65 63 79 63 6c 61 62  e;.  *pnRecyclab
110a3 6c 65 20 3d 20 6e 52 65 63 79 63 6c 61 62 6c 65  le = nRecyclable
110a4 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
110a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
110a6 6f 66 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a  of pcache1.c ***
110a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
110aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
110ab 6e 20 66 69 6c 65 20 72 6f 77 73 65 74 2e 63 20  n file rowset.c 
110ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
110af 2a 20 32 30 30 38 20 44 65 63 65 6d 62 65 72 20  * 2008 December 
110b0 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  3.**.** The auth
110b1 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
110b2 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
110b3 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
110b4 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
110b5 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
110b6 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
110b7 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
110b8 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
110b9 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
110ba 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
110bb 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
110bc 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
110bd 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
110be 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
110bf 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
110c0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
110c1 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
110c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
110c6 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  .**.** This modu
110c7 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e  le implements an
110c8 20 6f 62 6a 65 63 74 20 77 65 20 63 61 6c 6c 20   object we call 
110c9 61 20 22 52 6f 77 53 65 74 22 2e 0a 2a 2a 0a 2a  a "RowSet"..**.*
110ca 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f 62 6a  * The RowSet obj
110cb 65 63 74 20 69 73 20 61 20 63 6f 6c 6c 65 63 74  ect is a collect
110cc 69 6f 6e 20 6f 66 20 72 6f 77 69 64 73 2e 20 20  ion of rowids.  
110cd 52 6f 77 69 64 73 0a 2a 2a 20 61 72 65 20 69 6e  Rowids.** are in
110ce 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20  serted into the 
110cf 52 6f 77 53 65 74 20 69 6e 20 61 6e 20 61 72 62  RowSet in an arb
110d0 69 74 72 61 72 79 20 6f 72 64 65 72 2e 20 20 49  itrary order.  I
110d1 6e 73 65 72 74 73 0a 2a 2a 20 63 61 6e 20 62 65  nserts.** can be
110d2 20 69 6e 74 65 72 6d 69 78 65 64 20 77 69 74 68   intermixed with
110d3 20 74 65 73 74 73 20 74 6f 20 73 65 65 20 69 66   tests to see if
110d4 20 61 20 67 69 76 65 6e 20 72 6f 77 69 64 20 68   a given rowid h
110d5 61 73 20 62 65 65 6e 0a 2a 2a 20 70 72 65 76 69  as been.** previ
110d6 6f 75 73 6c 79 20 69 6e 73 65 72 74 65 64 20 69  ously inserted i
110d7 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 2e 0a  nto the RowSet..
110d8 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 6c 6c 20  **.** After all 
110d9 69 6e 73 65 72 74 73 20 61 72 65 20 66 69 6e 69  inserts are fini
110da 73 68 65 64 2c 20 69 74 20 69 73 20 70 6f 73 73  shed, it is poss
110db 69 62 6c 65 20 74 6f 20 65 78 74 72 61 63 74 20  ible to extract 
110dc 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73 20  the.** elements 
110dd 6f 66 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e  of the RowSet in
110de 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20   sorted order.  
110df 4f 6e 63 65 20 74 68 69 73 20 65 78 74 72 61 63  Once this extrac
110e0 74 69 6f 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 20  tion.** process 
110e1 68 61 73 20 73 74 61 72 74 65 64 2c 20 6e 6f 20  has started, no 
110e2 6e 65 77 20 65 6c 65 6d 65 6e 74 73 20 6d 61 79  new elements may
110e3 20 62 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a   be inserted..**
110e4 0a 2a 2a 20 48 65 6e 63 65 2c 20 74 68 65 20 70  .** Hence, the p
110e5 72 69 6d 69 74 69 76 65 20 6f 70 65 72 61 74 69  rimitive operati
110e6 6f 6e 73 20 66 6f 72 20 61 20 52 6f 77 53 65 74  ons for a RowSet
110e7 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 43   are:.**.**    C
110e8 52 45 41 54 45 0a 2a 2a 20 20 20 20 49 4e 53 45  REATE.**    INSE
110e9 52 54 0a 2a 2a 20 20 20 20 54 45 53 54 0a 2a 2a  RT.**    TEST.**
110ea 20 20 20 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20      SMALLEST.** 
110eb 20 20 20 44 45 53 54 52 4f 59 0a 2a 2a 0a 2a 2a     DESTROY.**.**
110ec 20 54 68 65 20 43 52 45 41 54 45 20 61 6e 64 20   The CREATE and 
110ed 44 45 53 54 52 4f 59 20 70 72 69 6d 69 74 69 76  DESTROY primitiv
110ee 65 73 20 61 72 65 20 74 68 65 20 63 6f 6e 73 74  es are the const
110ef 72 75 63 74 6f 72 20 61 6e 64 20 64 65 73 74 72  ructor and destr
110f0 75 63 74 6f 72 2c 0a 2a 2a 20 6f 62 76 69 6f 75  uctor,.** obviou
110f1 73 6c 79 2e 20 20 54 68 65 20 49 4e 53 45 52 54  sly.  The INSERT
110f2 20 70 72 69 6d 69 74 69 76 65 20 61 64 64 73 20   primitive adds 
110f3 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f  a new element to
110f4 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20   the RowSet..** 
110f5 54 45 53 54 20 63 68 65 63 6b 73 20 74 6f 20 73  TEST checks to s
110f6 65 65 20 69 66 20 61 6e 20 65 6c 65 6d 65 6e 74  ee if an element
110f7 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
110f8 68 65 20 52 6f 77 53 65 74 2e 20 20 53 4d 41 4c  he RowSet.  SMAL
110f9 4c 45 53 54 0a 2a 2a 20 65 78 74 72 61 63 74 73  LEST.** extracts
110fa 20 74 68 65 20 6c 65 61 73 74 20 76 61 6c 75 65   the least value
110fb 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53 65 74   from the RowSet
110fc 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 4e 53 45  ..**.** The INSE
110fd 52 54 20 70 72 69 6d 69 74 69 76 65 20 6d 69 67  RT primitive mig
110fe 68 74 20 61 6c 6c 6f 63 61 74 65 20 61 64 64 69  ht allocate addi
110ff 74 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 2e 20 20  tional memory.  
11100 4d 65 6d 6f 72 79 20 69 73 0a 2a 2a 20 61 6c 6c  Memory is.** all
11101 6f 63 61 74 65 64 20 69 6e 20 63 68 75 6e 6b 73  ocated in chunks
11102 20 73 6f 20 6d 6f 73 74 20 49 4e 53 45 52 54 73   so most INSERTs
11103 20 64 6f 20 6e 6f 20 61 6c 6c 6f 63 61 74 69 6f   do no allocatio
11104 6e 2e 20 20 54 68 65 72 65 20 69 73 20 61 6e 20  n.  There is an 
11105 0a 2a 2a 20 75 70 70 65 72 20 62 6f 75 6e 64 20  .** upper bound 
11106 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  on the size of a
11107 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e  llocated memory.
11108 20 20 4e 6f 20 6d 65 6d 6f 72 79 20 69 73 20 66    No memory is f
11109 72 65 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 44 45  reed.** until DE
1110a 53 54 52 4f 59 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  STROY..**.** The
1110b 20 54 45 53 54 20 70 72 69 6d 69 74 69 76 65 20   TEST primitive 
1110c 69 6e 63 6c 75 64 65 73 20 61 20 22 62 61 74 63  includes a "batc
1110d 68 22 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20  h" number.  The 
1110e 54 45 53 54 20 70 72 69 6d 69 74 69 76 65 0a 2a  TEST primitive.*
1110f 2a 20 77 69 6c 6c 20 6f 6e 6c 79 20 73 65 65 20  * will only see 
11110 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77 65  elements that we
11111 72 65 20 69 6e 73 65 72 74 65 64 20 62 65 66 6f  re inserted befo
11112 72 65 20 74 68 65 20 6c 61 73 74 20 63 68 61 6e  re the last chan
11113 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 62 61 74  ge.** in the bat
11114 63 68 20 6e 75 6d 62 65 72 2e 20 20 49 6e 20 6f  ch number.  In o
11115 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 61  ther words, if a
11116 6e 20 49 4e 53 45 52 54 20 6f 63 63 75 72 73 20  n INSERT occurs 
11117 62 65 74 77 65 65 6e 0a 2a 2a 20 74 77 6f 20 54  between.** two T
11118 45 53 54 73 20 77 68 65 72 65 20 74 68 65 20 54  ESTs where the T
11119 45 53 54 73 20 68 61 76 65 20 74 68 65 20 73 61  ESTs have the sa
1111a 6d 65 20 62 61 74 63 68 20 6e 75 62 6d 65 72 2c  me batch nubmer,
1111b 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61 6c   then the.** val
1111c 75 65 20 61 64 64 65 64 20 62 79 20 74 68 65 20  ue added by the 
1111d 49 4e 53 45 52 54 20 77 69 6c 6c 20 6e 6f 74 20  INSERT will not 
1111e 62 65 20 76 69 73 69 62 6c 65 20 74 6f 20 74 68  be visible to th
1111f 65 20 73 65 63 6f 6e 64 20 54 45 53 54 2e 0a 2a  e second TEST..*
11120 2a 20 54 68 65 20 69 6e 69 74 69 61 6c 20 62 61  * The initial ba
11121 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 7a 65  tch number is ze
11122 72 6f 2c 20 73 6f 20 69 66 20 74 68 65 20 76 65  ro, so if the ve
11123 72 79 20 66 69 72 73 74 20 54 45 53 54 20 63 6f  ry first TEST co
11124 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 6e 6f 6e 2d  ntains.** a non-
11125 7a 65 72 6f 20 62 61 74 63 68 20 6e 75 6d 62 65  zero batch numbe
11126 72 2c 20 69 74 20 77 69 6c 6c 20 73 65 65 20 61  r, it will see a
11127 6c 6c 20 70 72 69 6f 72 20 49 4e 53 45 52 54 73  ll prior INSERTs
11128 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 49 4e 53 45 52  ..**.** No INSER
11129 54 73 20 6d 61 79 20 6f 63 63 75 72 73 20 61 66  Ts may occurs af
1112a 74 65 72 20 61 20 53 4d 41 4c 4c 45 53 54 2e 20  ter a SMALLEST. 
1112b 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 77 69   An assertion wi
1112c 6c 6c 20 66 61 69 6c 20 69 66 0a 2a 2a 20 74 68  ll fail if.** th
1112d 61 74 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e  at is attempted.
1112e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 73 74 20  .**.** The cost 
1112f 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 69 73 20  of an INSERT is 
11130 72 6f 75 67 68 6c 79 20 63 6f 6e 73 74 61 6e 74  roughly constant
11131 2e 20 20 28 53 6f 6d 65 74 69 6d 65 20 6e 65 77  .  (Sometime new
11132 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 61 73 20 74   memory.** has t
11133 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f  o be allocated o
11134 6e 20 61 6e 20 49 4e 53 45 52 54 2e 29 20 20 54  n an INSERT.)  T
11135 68 65 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53  he cost of a TES
11136 54 20 77 69 74 68 20 61 20 6e 65 77 0a 2a 2a 20  T with a new.** 
11137 62 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20  batch number is 
11138 4f 28 4e 6c 6f 67 4e 29 20 77 68 65 72 65 20 4e  O(NlogN) where N
11139 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
1113a 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  f elements in th
1113b 65 20 52 6f 77 53 65 74 2e 0a 2a 2a 20 54 68 65  e RowSet..** The
1113c 20 63 6f 73 74 20 6f 66 20 61 20 54 45 53 54 20   cost of a TEST 
1113d 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 62  using the same b
1113e 61 74 63 68 20 6e 75 6d 62 65 72 20 69 73 20 4f  atch number is O
1113f 28 6c 6f 67 4e 29 2e 20 20 54 68 65 20 63 6f 73  (logN).  The cos
11140 74 0a 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73  t.** of the firs
11141 74 20 53 4d 41 4c 4c 45 53 54 20 69 73 20 4f 28  t SMALLEST is O(
11142 4e 6c 6f 67 4e 29 2e 20 20 53 65 63 6f 6e 64 20  NlogN).  Second 
11143 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 53  and subsequent S
11144 4d 41 4c 4c 45 53 54 0a 2a 2a 20 70 72 69 6d 69  MALLEST.** primi
11145 74 69 76 65 73 20 61 72 65 20 63 6f 6e 73 74 61  tives are consta
11146 6e 74 20 74 69 6d 65 2e 20 20 54 68 65 20 63 6f  nt time.  The co
11147 73 74 20 6f 66 20 44 45 53 54 52 4f 59 20 69 73  st of DESTROY is
11148 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   O(N)..**.** The
11149 72 65 20 69 73 20 61 6e 20 61 64 64 65 64 20 63  re is an added c
1114a 6f 73 74 20 6f 66 20 4f 28 4e 29 20 77 68 65 6e  ost of O(N) when
1114b 20 73 77 69 74 63 68 69 6e 67 20 62 65 74 77 65   switching betwe
1114c 65 6e 20 54 45 53 54 20 61 6e 64 0a 2a 2a 20 53  en TEST and.** S
1114d 4d 41 4c 4c 45 53 54 20 70 72 69 6d 69 74 69 76  MALLEST primitiv
1114e 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54  es..*/.../*.** T
1114f 61 72 67 65 74 20 73 69 7a 65 20 66 6f 72 20 61  arget size for a
11150 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 73  llocation chunks
11151 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57  ..*/.#define ROW
11152 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53  SET_ALLOCATION_S
11153 49 5a 45 20 31 30 32 34 0a 0a 2f 2a 0a 2a 2a 20  IZE 1024../*.** 
11154 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  The number of ro
11155 77 73 65 74 20 65 6e 74 72 69 65 73 20 70 65 72  wset entries per
11156 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e   allocation chun
11157 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f  k..*/.#define RO
11158 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43  WSET_ENTRY_PER_C
11159 48 55 4e 4b 20 20 5c 0a 20 20 20 20 20 20 20 20  HUNK  \.        
1115a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
1115b 28 52 4f 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49  (ROWSET_ALLOCATI
1115c 4f 4e 5f 53 49 5a 45 2d 38 29 2f 73 69 7a 65 6f  ON_SIZE-8)/sizeo
1115d 66 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45  f(struct RowSetE
1115e 6e 74 72 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 61  ntry))../*.** Ea
1115f 63 68 20 65 6e 74 72 79 20 69 6e 20 61 20 52 6f  ch entry in a Ro
11160 77 53 65 74 20 69 73 20 61 6e 20 69 6e 73 74 61  wSet is an insta
11161 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
11162 77 69 6e 67 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  wing object..*/.
11163 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
11164 72 79 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  ry {            
11165 0a 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20  .  i64 v;       
11166 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11167 20 2f 2a 20 52 4f 57 49 44 20 76 61 6c 75 65 20   /* ROWID value 
11168 66 6f 72 20 74 68 69 73 20 65 6e 74 72 79 20 2a  for this entry *
11169 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  /.  struct RowSe
1116a 74 45 6e 74 72 79 20 2a 70 52 69 67 68 74 3b 20  tEntry *pRight; 
1116b 20 20 2f 2a 20 52 69 67 68 74 20 73 75 62 74 72    /* Right subtr
1116c 65 65 20 28 6c 61 72 67 65 72 20 65 6e 74 72 69  ee (larger entri
1116d 65 73 29 20 6f 72 20 6c 69 73 74 20 2a 2f 0a 20  es) or list */. 
1116e 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
1116f 74 72 79 20 2a 70 4c 65 66 74 3b 20 20 20 20 2f  try *pLeft;    /
11170 2a 20 4c 65 66 74 20 73 75 62 74 72 65 65 20 28  * Left subtree (
11171 73 6d 61 6c 6c 65 72 20 65 6e 74 72 69 65 73 29  smaller entries)
11172 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f   */.};../*.** Ro
11173 77 53 65 74 45 6e 74 72 79 20 6f 62 6a 65 63 74  wSetEntry object
11174 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20  s are allocated 
11175 69 6e 20 6c 61 72 67 65 20 63 68 75 6e 6b 73 20  in large chunks 
11176 28 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74 68  (instances of th
11177 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73  e.** following s
11178 74 72 75 63 74 75 72 65 29 20 74 6f 20 72 65 64  tructure) to red
11179 75 63 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  uce memory alloc
1117a 61 74 69 6f 6e 20 6f 76 65 72 68 65 61 64 2e 20  ation overhead. 
1117b 20 54 68 65 0a 2a 2a 20 63 68 75 6e 6b 73 20 61   The.** chunks a
1117c 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e  re kept on a lin
1117d 6b 65 64 20 6c 69 73 74 20 73 6f 20 74 68 61 74  ked list so that
1117e 20 74 68 65 79 20 63 61 6e 20 62 65 20 64 65 61   they can be dea
1117f 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 77 68 65 6e  llocated.** when
11180 20 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 64   the RowSet is d
11181 65 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a 73 74 72  estroyed..*/.str
11182 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20  uct RowSetChunk 
11183 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  {.  struct RowSe
11184 74 43 68 75 6e 6b 20 2a 70 4e 65 78 74 43 68 75  tChunk *pNextChu
11185 6e 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  nk;        /* Ne
11186 78 74 20 63 68 75 6e 6b 20 6f 6e 20 6c 69 73 74  xt chunk on list
11187 20 6f 66 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a   of them all */.
11188 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
11189 6e 74 72 79 20 61 45 6e 74 72 79 5b 52 4f 57 53  ntry aEntry[ROWS
1118a 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55  ET_ENTRY_PER_CHU
1118b 4e 4b 5d 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  NK]; /* Allocate
1118c 64 20 65 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a  d entries */.};.
1118d 0a 2f 2a 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20  ./*.** A RowSet 
1118e 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  in an instance o
1118f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
11190 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
11191 20 41 20 74 79 70 65 64 65 66 20 6f 66 20 74 68   A typedef of th
11192 69 73 20 73 74 72 75 63 74 75 72 65 20 69 66 20  is structure if 
11193 66 6f 75 6e 64 20 69 6e 20 73 71 6c 69 74 65 49  found in sqliteI
11194 6e 74 2e 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  nt.h..*/.struct 
11195 52 6f 77 53 65 74 20 7b 0a 20 20 73 74 72 75 63  RowSet {.  struc
11196 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70  t RowSetChunk *p
11197 43 68 75 6e 6b 3b 20 20 20 20 2f 2a 20 4c 69 73  Chunk;    /* Lis
11198 74 20 6f 66 20 61 6c 6c 20 63 68 75 6e 6b 20 61  t of all chunk a
11199 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20  llocations */.  
1119a 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
1119b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1119c 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
1119d 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73  onnection */.  s
1119e 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
1119f 79 20 2a 70 45 6e 74 72 79 3b 20 20 20 20 2f 2a  y *pEntry;    /*
111a0 20 4c 69 73 74 20 6f 66 20 65 6e 74 72 69 65 73   List of entries
111a1 20 75 73 69 6e 67 20 70 52 69 67 68 74 20 2a 2f   using pRight */
111a2 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
111a3 45 6e 74 72 79 20 2a 70 4c 61 73 74 3b 20 20 20  Entry *pLast;   
111a4 20 20 2f 2a 20 4c 61 73 74 20 65 6e 74 72 79 20    /* Last entry 
111a5 6f 6e 20 74 68 65 20 70 45 6e 74 72 79 20 6c 69  on the pEntry li
111a6 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  st */.  struct R
111a7 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 46 72 65  owSetEntry *pFre
111a8 73 68 3b 20 20 20 20 2f 2a 20 53 6f 75 72 63 65  sh;    /* Source
111a9 20 6f 66 20 6e 65 77 20 65 6e 74 72 79 20 6f 62   of new entry ob
111aa 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63  jects */.  struc
111ab 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
111ac 54 72 65 65 3b 20 20 20 20 20 2f 2a 20 42 69 6e  Tree;     /* Bin
111ad 61 72 79 20 74 72 65 65 20 6f 66 20 65 6e 74 72  ary tree of entr
111ae 69 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72  ies */.  u16 nFr
111af 65 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  esh;            
111b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
111b1 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 6f 6e 20  r of objects on 
111b2 70 46 72 65 73 68 20 2a 2f 0a 20 20 75 38 20 69  pFresh */.  u8 i
111b3 73 53 6f 72 74 65 64 3b 20 20 20 20 20 20 20 20  sSorted;        
111b4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
111b5 75 65 20 69 66 20 70 45 6e 74 72 79 20 69 73 20  ue if pEntry is 
111b6 73 6f 72 74 65 64 20 2a 2f 0a 20 20 75 38 20 69  sorted */.  u8 i
111b7 42 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20  Batch;          
111b8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
111b9 72 72 65 6e 74 20 69 6e 73 65 72 74 20 62 61 74  rrent insert bat
111ba 63 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ch */.};../*.** 
111bb 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79  Turn bulk memory
111bc 20 69 6e 74 6f 20 61 20 52 6f 77 53 65 74 20 6f   into a RowSet o
111bd 62 6a 65 63 74 2e 20 20 4e 20 62 79 74 65 73 20  bject.  N bytes 
111be 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65  of memory.** are
111bf 20 61 76 61 69 6c 61 62 6c 65 20 61 74 20 70 53   available at pS
111c0 70 61 63 65 2e 20 20 54 68 65 20 64 62 20 70 6f  pace.  The db po
111c1 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 61 73  inter is used as
111c2 20 61 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78   a memory contex
111c3 74 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 73 75 62  t.** for any sub
111c4 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69  sequent allocati
111c5 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  ons that need to
111c6 20 6f 63 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72   occur..** Retur
111c7 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
111c8 68 65 20 6e 65 77 20 52 6f 77 53 65 74 20 6f 62  he new RowSet ob
111c9 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d  ject..**.** It m
111ca 75 73 74 20 62 65 20 74 68 65 20 63 61 73 65 20  ust be the case 
111cb 74 68 61 74 20 4e 20 69 73 20 73 75 66 66 69 63  that N is suffic
111cc 69 65 6e 74 20 74 6f 20 6d 61 6b 65 20 61 20 52  ient to make a R
111cd 6f 77 73 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a  owset.  If not.*
111ce 2a 20 61 6e 20 61 73 73 65 72 74 69 6f 6e 20 66  * an assertion f
111cf 61 75 6c 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20  ault occurs..** 
111d0 0a 2a 2a 20 49 66 20 4e 20 69 73 20 6c 61 72 67  .** If N is larg
111d1 65 72 20 74 68 61 6e 20 74 68 65 20 6d 69 6e 69  er than the mini
111d2 6d 75 6d 2c 20 75 73 65 20 74 68 65 20 73 75 72  mum, use the sur
111d3 70 6c 75 73 20 61 73 20 61 6e 20 69 6e 69 74 69  plus as an initi
111d4 61 6c 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e  al.** allocation
111d5 20 6f 66 20 65 6e 74 72 69 65 73 20 61 76 61 69   of entries avai
111d6 6c 61 62 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c  lable to be fill
111d7 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
111d8 49 56 41 54 45 20 52 6f 77 53 65 74 20 2a 73 71  IVATE RowSet *sq
111d9 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 69 74 28  lite3RowSetInit(
111da 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69  sqlite3 *db, voi
111db 64 20 2a 70 53 70 61 63 65 2c 20 75 6e 73 69 67  d *pSpace, unsig
111dc 6e 65 64 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f  ned int N){.  Ro
111dd 77 53 65 74 20 2a 70 3b 0a 20 20 61 73 73 65 72  wSet *p;.  asser
111de 74 28 20 4e 20 3e 3d 20 52 4f 55 4e 44 38 28 73  t( N >= ROUND8(s
111df 69 7a 65 6f 66 28 2a 70 29 29 20 29 3b 0a 20 20  izeof(*p)) );.  
111e0 70 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 70 2d  p = pSpace;.  p-
111e1 3e 70 43 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70  >pChunk = 0;.  p
111e2 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70 2d 3e  ->db = db;.  p->
111e3 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d  pEntry = 0;.  p-
111e4 3e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d  >pLast = 0;.  p-
111e5 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d  >pTree = 0;.  p-
111e6 3e 70 46 72 65 73 68 20 3d 20 28 73 74 72 75 63  >pFresh = (struc
111e7 74 20 52 6f 77 53 65 74 45 6e 74 72 79 2a 29 28  t RowSetEntry*)(
111e8 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70  ROUND8(sizeof(*p
111e9 29 29 20 2b 20 28 63 68 61 72 2a 29 70 29 3b 0a  )) + (char*)p);.
111ea 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 28 75    p->nFresh = (u
111eb 31 36 29 28 28 4e 20 2d 20 52 4f 55 4e 44 38 28  16)((N - ROUND8(
111ec 73 69 7a 65 6f 66 28 2a 70 29 29 29 2f 73 69 7a  sizeof(*p)))/siz
111ed 65 6f 66 28 73 74 72 75 63 74 20 52 6f 77 53 65  eof(struct RowSe
111ee 74 45 6e 74 72 79 29 29 3b 0a 20 20 70 2d 3e 69  tEntry));.  p->i
111ef 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 20 20 70  sSorted = 1;.  p
111f0 2d 3e 69 42 61 74 63 68 20 3d 20 30 3b 0a 20 20  ->iBatch = 0;.  
111f1 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
111f2 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c  ** Deallocate al
111f3 6c 20 63 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20  l chunks from a 
111f4 52 6f 77 53 65 74 2e 20 20 54 68 69 73 20 66 72  RowSet.  This fr
111f5 65 65 73 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74  ees all memory t
111f6 68 61 74 0a 2a 2a 20 74 68 65 20 52 6f 77 53 65  hat.** the RowSe
111f7 74 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64 20  t has allocated 
111f8 6f 76 65 72 20 69 74 73 20 6c 69 66 65 74 69 6d  over its lifetim
111f9 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
111fa 20 69 73 0a 2a 2a 20 74 68 65 20 64 65 73 74 72   is.** the destr
111fb 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 52 6f  uctor for the Ro
111fc 77 53 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  wSet..*/.SQLITE_
111fd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
111fe 69 74 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28  ite3RowSetClear(
111ff 52 6f 77 53 65 74 20 2a 70 29 7b 0a 20 20 73 74  RowSet *p){.  st
11200 72 75 63 74 20 52 6f 77 53 65 74 43 68 75 6e 6b  ruct RowSetChunk
11201 20 2a 70 43 68 75 6e 6b 2c 20 2a 70 4e 65 78 74   *pChunk, *pNext
11202 43 68 75 6e 6b 3b 0a 20 20 66 6f 72 28 70 43 68  Chunk;.  for(pCh
11203 75 6e 6b 3d 70 2d 3e 70 43 68 75 6e 6b 3b 20 70  unk=p->pChunk; p
11204 43 68 75 6e 6b 3b 20 70 43 68 75 6e 6b 20 3d 20  Chunk; pChunk = 
11205 70 4e 65 78 74 43 68 75 6e 6b 29 7b 0a 20 20 20  pNextChunk){.   
11206 20 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70 43   pNextChunk = pC
11207 68 75 6e 6b 2d 3e 70 4e 65 78 74 43 68 75 6e 6b  hunk->pNextChunk
11208 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
11209 72 65 65 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e  ree(p->db, pChun
1120a 6b 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68  k);.  }.  p->pCh
1120b 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46  unk = 0;.  p->nF
1120c 72 65 73 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70  resh = 0;.  p->p
1120d 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e  Entry = 0;.  p->
1120e 70 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e  pLast = 0;.  p->
1120f 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  pTree = 0;.  p->
11210 69 73 53 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a  isSorted = 1;.}.
11211 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20  ./*.** Insert a 
11212 6e 65 77 20 76 61 6c 75 65 20 69 6e 74 6f 20 61  new value into a
11213 20 52 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54   RowSet..**.** T
11214 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
11215 66 6c 61 67 20 6f 66 20 74 68 65 20 64 61 74 61  flag of the data
11216 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
11217 69 73 20 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d  is set if a.** m
11218 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
11219 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54   fails..*/.SQLIT
1121a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1121b 71 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65  qlite3RowSetInse
1121c 72 74 28 52 6f 77 53 65 74 20 2a 70 2c 20 69 36  rt(RowSet *p, i6
1121d 34 20 72 6f 77 69 64 29 7b 0a 20 20 73 74 72 75  4 rowid){.  stru
1121e 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
1121f 70 45 6e 74 72 79 3b 20 20 2f 2a 20 54 68 65 20  pEntry;  /* The 
11220 6e 65 77 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73  new entry */.  s
11221 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
11222 79 20 2a 70 4c 61 73 74 3b 20 20 20 2f 2a 20 54  y *pLast;   /* T
11223 68 65 20 6c 61 73 74 20 70 72 69 6f 72 20 65 6e  he last prior en
11224 74 72 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  try */.  assert(
11225 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70   p!=0 );.  if( p
11226 2d 3e 6e 46 72 65 73 68 3d 3d 30 20 29 7b 0a 20  ->nFresh==0 ){. 
11227 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74     struct RowSet
11228 43 68 75 6e 6b 20 2a 70 4e 65 77 3b 0a 20 20 20  Chunk *pNew;.   
11229 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
1122a 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62  bMallocRaw(p->db
1122b 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29  , sizeof(*pNew))
1122c 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d  ;.    if( pNew==
1122d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
1122e 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65  n;.    }.    pNe
1122f 77 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20  w->pNextChunk = 
11230 70 2d 3e 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70  p->pChunk;.    p
11231 2d 3e 70 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b  ->pChunk = pNew;
11232 0a 20 20 20 20 70 2d 3e 70 46 72 65 73 68 20 3d  .    p->pFresh =
11233 20 70 4e 65 77 2d 3e 61 45 6e 74 72 79 3b 0a 20   pNew->aEntry;. 
11234 20 20 20 70 2d 3e 6e 46 72 65 73 68 20 3d 20 52     p->nFresh = R
11235 4f 57 53 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f  OWSET_ENTRY_PER_
11236 43 48 55 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 6e  CHUNK;.  }.  pEn
11237 74 72 79 20 3d 20 70 2d 3e 70 46 72 65 73 68 2b  try = p->pFresh+
11238 2b 3b 0a 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d  +;.  p->nFresh--
11239 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20  ;.  pEntry->v = 
1123a 72 6f 77 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d  rowid;.  pEntry-
1123b 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70  >pRight = 0;.  p
1123c 4c 61 73 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b  Last = p->pLast;
1123d 0a 20 20 69 66 28 20 70 4c 61 73 74 20 29 7b 0a  .  if( pLast ){.
1123e 20 20 20 20 69 66 28 20 70 2d 3e 69 73 53 6f 72      if( p->isSor
1123f 74 65 64 20 26 26 20 72 6f 77 69 64 3c 3d 70 4c  ted && rowid<=pL
11240 61 73 74 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20  ast->v ){.      
11241 70 2d 3e 69 73 53 6f 72 74 65 64 20 3d 20 30 3b  p->isSorted = 0;
11242 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 61 73 74  .    }.    pLast
11243 2d 3e 70 52 69 67 68 74 20 3d 20 70 45 6e 74 72  ->pRight = pEntr
11244 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  y;.  }else{.    
11245 61 73 73 65 72 74 28 20 70 2d 3e 70 45 6e 74 72  assert( p->pEntr
11246 79 3d 3d 30 20 29 3b 20 2f 2a 20 46 69 72 65 73  y==0 ); /* Fires
11247 20 69 66 20 49 4e 53 45 52 54 20 61 66 74 65 72   if INSERT after
11248 20 53 4d 41 4c 4c 45 53 54 20 2a 2f 0a 20 20 20   SMALLEST */.   
11249 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e   p->pEntry = pEn
1124a 74 72 79 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c  try;.  }.  p->pL
1124b 61 73 74 20 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a  ast = pEntry;.}.
1124c 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f  ./*.** Merge two
1124d 20 6c 69 73 74 73 20 6f 66 20 52 6f 77 53 65 74   lists of RowSet
1124e 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 2e 20 20  Entry objects.  
1124f 52 65 6d 6f 76 65 20 64 75 70 6c 69 63 61 74 65  Remove duplicate
11250 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70  s..**.** The inp
11251 75 74 20 6c 69 73 74 73 20 61 72 65 20 63 6f 6e  ut lists are con
11252 6e 65 63 74 65 64 20 76 69 61 20 70 52 69 67 68  nected via pRigh
11253 74 20 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 61  t pointers and a
11254 72 65 20 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74  re .** assumed t
11255 6f 20 65 61 63 68 20 61 6c 72 65 61 64 79 20 62  o each already b
11256 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  e in sorted orde
11257 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72  r..*/.static str
11258 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
11259 2a 72 6f 77 53 65 74 4d 65 72 67 65 28 0a 20 20  *rowSetMerge(.  
1125a 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
1125b 72 79 20 2a 70 41 2c 20 20 20 20 2f 2a 20 46 69  ry *pA,    /* Fi
1125c 72 73 74 20 73 6f 72 74 65 64 20 6c 69 73 74 20  rst sorted list 
1125d 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a  to be merged */.
1125e 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45    struct RowSetE
1125f 6e 74 72 79 20 2a 70 42 20 20 20 20 20 2f 2a 20  ntry *pB     /* 
11260 53 65 63 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69  Second sorted li
11261 73 74 20 74 6f 20 62 65 20 6d 65 72 67 65 64 20  st to be merged 
11262 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 52  */.){.  struct R
11263 6f 77 53 65 74 45 6e 74 72 79 20 68 65 61 64 3b  owSetEntry head;
11264 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  .  struct RowSet
11265 45 6e 74 72 79 20 2a 70 54 61 69 6c 3b 0a 0a 20  Entry *pTail;.. 
11266 20 70 54 61 69 6c 20 3d 20 26 68 65 61 64 3b 0a   pTail = &head;.
11267 20 20 77 68 69 6c 65 28 20 70 41 20 26 26 20 70    while( pA && p
11268 42 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  B ){.    assert(
11269 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c   pA->pRight==0 |
1126a 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69  | pA->v<=pA->pRi
1126b 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 61 73  ght->v );.    as
1126c 73 65 72 74 28 20 70 42 2d 3e 70 52 69 67 68 74  sert( pB->pRight
1126d 3d 3d 30 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42  ==0 || pB->v<=pB
1126e 2d 3e 70 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20  ->pRight->v );. 
1126f 20 20 20 69 66 28 20 70 41 2d 3e 76 3c 70 42 2d     if( pA->v<pB-
11270 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69  >v ){.      pTai
11271 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b 0a  l->pRight = pA;.
11272 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70        pA = pA->p
11273 52 69 67 68 74 3b 0a 20 20 20 20 20 20 70 54 61  Right;.      pTa
11274 69 6c 20 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67  il = pTail->pRig
11275 68 74 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ht;.    }else if
11276 28 20 70 42 2d 3e 76 3c 70 41 2d 3e 76 20 29 7b  ( pB->v<pA->v ){
11277 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70 52  .      pTail->pR
11278 69 67 68 74 20 3d 20 70 42 3b 0a 20 20 20 20 20  ight = pB;.     
11279 20 70 42 20 3d 20 70 42 2d 3e 70 52 69 67 68 74   pB = pB->pRight
1127a 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20 3d 20  ;.      pTail = 
1127b 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20  pTail->pRight;. 
1127c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1127d 70 41 20 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b  pA = pA->pRight;
1127e 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
1127f 20 70 41 20 29 7b 0a 20 20 20 20 61 73 73 65 72   pA ){.    asser
11280 74 28 20 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30  t( pA->pRight==0
11281 20 7c 7c 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70   || pA->v<=pA->p
11282 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20  Right->v );.    
11283 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20  pTail->pRight = 
11284 70 41 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  pA;.  }else{.   
11285 20 61 73 73 65 72 74 28 20 70 42 3d 3d 30 20 7c   assert( pB==0 |
11286 7c 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20  | pB->pRight==0 
11287 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52  || pB->v<=pB->pR
11288 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70  ight->v );.    p
11289 54 61 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70  Tail->pRight = p
1128a 42 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  B;.  }.  return 
1128b 68 65 61 64 2e 70 52 69 67 68 74 3b 0a 7d 0a 0a  head.pRight;.}..
1128c 2f 2a 0a 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65  /*.** Sort all e
1128d 6c 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20 70  lements on the p
1128e 45 6e 74 72 79 20 6c 69 73 74 20 6f 66 20 74 68  Entry list of th
1128f 65 20 52 6f 77 53 65 74 20 69 6e 74 6f 20 61 73  e RowSet into as
11290 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a  cending order..*
11291 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  / .static void r
11292 6f 77 53 65 74 53 6f 72 74 28 52 6f 77 53 65 74  owSetSort(RowSet
11293 20 2a 70 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64   *p){.  unsigned
11294 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
11295 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45   RowSetEntry *pE
11296 6e 74 72 79 3b 0a 20 20 73 74 72 75 63 74 20 52  ntry;.  struct R
11297 6f 77 53 65 74 45 6e 74 72 79 20 2a 61 42 75 63  owSetEntry *aBuc
11298 6b 65 74 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 65  ket[40];..  asse
11299 72 74 28 20 70 2d 3e 69 73 53 6f 72 74 65 64 3d  rt( p->isSorted=
1129a 3d 30 20 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61  =0 );.  memset(a
1129b 42 75 63 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f  Bucket, 0, sizeo
1129c 66 28 61 42 75 63 6b 65 74 29 29 3b 0a 20 20 77  f(aBucket));.  w
1129d 68 69 6c 65 28 20 70 2d 3e 70 45 6e 74 72 79 20  hile( p->pEntry 
1129e 29 7b 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20  ){.    pEntry = 
1129f 70 2d 3e 70 45 6e 74 72 79 3b 0a 20 20 20 20 70  p->pEntry;.    p
112a0 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72  ->pEntry = pEntr
112a1 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70  y->pRight;.    p
112a2 45 6e 74 72 79 2d 3e 70 52 69 67 68 74 20 3d 20  Entry->pRight = 
112a3 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
112a4 61 42 75 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b 29  aBucket[i]; i++)
112a5 7b 0a 20 20 20 20 20 20 70 45 6e 74 72 79 20 3d  {.      pEntry =
112a6 20 72 6f 77 53 65 74 4d 65 72 67 65 28 61 42 75   rowSetMerge(aBu
112a7 63 6b 65 74 5b 69 5d 2c 20 70 45 6e 74 72 79 29  cket[i], pEntry)
112a8 3b 0a 20 20 20 20 20 20 61 42 75 63 6b 65 74 5b  ;.      aBucket[
112a9 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  i] = 0;.    }.  
112aa 20 20 61 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70    aBucket[i] = p
112ab 45 6e 74 72 79 3b 0a 20 20 7d 0a 20 20 70 45 6e  Entry;.  }.  pEn
112ac 74 72 79 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  try = 0;.  for(i
112ad 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61 42 75  =0; i<sizeof(aBu
112ae 63 6b 65 74 29 2f 73 69 7a 65 6f 66 28 61 42 75  cket)/sizeof(aBu
112af 63 6b 65 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  cket[0]); i++){.
112b0 20 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f 77      pEntry = row
112b1 53 65 74 4d 65 72 67 65 28 70 45 6e 74 72 79 2c  SetMerge(pEntry,
112b2 20 61 42 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20   aBucket[i]);.  
112b3 7d 0a 20 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20  }.  p->pEntry = 
112b4 70 45 6e 74 72 79 3b 0a 20 20 70 2d 3e 70 4c 61  pEntry;.  p->pLa
112b5 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53  st = 0;.  p->isS
112b6 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f  orted = 1;.}.../
112b7 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 2c 20  *.** The input, 
112b8 70 49 6e 2c 20 69 73 20 61 20 62 69 6e 61 72 79  pIn, is a binary
112b9 20 74 72 65 65 20 28 6f 72 20 73 75 62 74 72 65   tree (or subtre
112ba 65 29 20 6f 66 20 52 6f 77 53 65 74 45 6e 74 72  e) of RowSetEntr
112bb 79 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 43 6f  y objects..** Co
112bc 6e 76 65 72 74 20 74 68 69 73 20 74 72 65 65 20  nvert this tree 
112bd 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69  into a linked li
112be 73 74 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  st connected by 
112bf 74 68 65 20 70 52 69 67 68 74 20 70 6f 69 6e 74  the pRight point
112c0 65 72 73 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72  ers.** and retur
112c1 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  n pointers to th
112c2 65 20 66 69 72 73 74 20 61 6e 64 20 6c 61 73 74  e first and last
112c3 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65   elements of the
112c4 20 6e 65 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74   new list..*/.st
112c5 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74  atic void rowSet
112c6 54 72 65 65 54 6f 4c 69 73 74 28 0a 20 20 73 74  TreeToList(.  st
112c7 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
112c8 20 2a 70 49 6e 2c 20 20 20 20 20 20 20 20 20 2f   *pIn,         /
112c9 2a 20 52 6f 6f 74 20 6f 66 20 74 68 65 20 69 6e  * Root of the in
112ca 70 75 74 20 74 72 65 65 20 2a 2f 0a 20 20 73 74  put tree */.  st
112cb 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
112cc 20 2a 2a 70 70 46 69 72 73 74 2c 20 20 20 20 2f   **ppFirst,    /
112cd 2a 20 57 72 69 74 65 20 68 65 61 64 20 6f 66 20  * Write head of 
112ce 74 68 65 20 6f 75 74 70 75 74 20 6c 69 73 74 20  the output list 
112cf 68 65 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74  here */.  struct
112d0 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70   RowSetEntry **p
112d1 70 4c 61 73 74 20 20 20 20 20 20 2f 2a 20 57 72  pLast      /* Wr
112d2 69 74 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20  ite tail of the 
112d3 6f 75 74 70 75 74 20 6c 69 73 74 20 68 65 72 65  output list here
112d4 20 2a 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28   */.){.  assert(
112d5 20 70 49 6e 21 3d 30 20 29 3b 0a 20 20 69 66 28   pIn!=0 );.  if(
112d6 20 70 49 6e 2d 3e 70 4c 65 66 74 20 29 7b 0a 20   pIn->pLeft ){. 
112d7 20 20 20 73 74 72 75 63 74 20 52 6f 77 53 65 74     struct RowSet
112d8 45 6e 74 72 79 20 2a 70 3b 0a 20 20 20 20 72 6f  Entry *p;.    ro
112d9 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70  wSetTreeToList(p
112da 49 6e 2d 3e 70 4c 65 66 74 2c 20 70 70 46 69 72  In->pLeft, ppFir
112db 73 74 2c 20 26 70 29 3b 0a 20 20 20 20 70 2d 3e  st, &p);.    p->
112dc 70 52 69 67 68 74 20 3d 20 70 49 6e 3b 0a 20 20  pRight = pIn;.  
112dd 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 46 69  }else{.    *ppFi
112de 72 73 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20  rst = pIn;.  }. 
112df 20 69 66 28 20 70 49 6e 2d 3e 70 52 69 67 68 74   if( pIn->pRight
112e0 20 29 7b 0a 20 20 20 20 72 6f 77 53 65 74 54 72   ){.    rowSetTr
112e1 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 52  eeToList(pIn->pR
112e2 69 67 68 74 2c 20 26 70 49 6e 2d 3e 70 52 69 67  ight, &pIn->pRig
112e3 68 74 2c 20 70 70 4c 61 73 74 29 3b 0a 20 20 7d  ht, ppLast);.  }
112e4 65 6c 73 65 7b 0a 20 20 20 20 2a 70 70 4c 61 73  else{.    *ppLas
112e5 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 61  t = pIn;.  }.  a
112e6 73 73 65 72 74 28 20 28 2a 70 70 4c 61 73 74 29  ssert( (*ppLast)
112e7 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 7d  ->pRight==0 );.}
112e8 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  .../*.** Convert
112e9 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74 20 6f   a sorted list o
112ea 66 20 65 6c 65 6d 65 6e 74 73 20 28 63 6f 6e 6e  f elements (conn
112eb 65 63 74 65 64 20 62 79 20 70 52 69 67 68 74 29  ected by pRight)
112ec 20 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 0a 2a   into a binary.*
112ed 2a 20 74 72 65 65 20 77 69 74 68 20 64 65 70 74  * tree with dept
112ee 68 20 6f 66 20 69 44 65 70 74 68 2e 20 20 41 20  h of iDepth.  A 
112ef 64 65 70 74 68 20 6f 66 20 31 20 6d 65 61 6e 73  depth of 1 means
112f0 20 74 68 65 20 74 72 65 65 20 63 6f 6e 74 61 69   the tree contai
112f1 6e 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6e  ns a single.** n
112f2 6f 64 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74  ode taken from t
112f3 68 65 20 68 65 61 64 20 6f 66 20 2a 70 70 4c 69  he head of *ppLi
112f4 73 74 2e 20 20 41 20 64 65 70 74 68 20 6f 66 20  st.  A depth of 
112f5 32 20 6d 65 61 6e 73 20 61 20 74 72 65 65 20 77  2 means a tree w
112f6 69 74 68 0a 2a 2a 20 74 68 72 65 65 20 6e 6f 64  ith.** three nod
112f7 65 73 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74  es.  And so fort
112f8 68 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 61 73 20  h..**.** Use as 
112f9 6d 61 6e 79 20 65 6e 74 72 69 65 73 20 66 72 6f  many entries fro
112fa 6d 20 74 68 65 20 69 6e 70 75 74 20 6c 69 73 74  m the input list
112fb 20 61 73 20 72 65 71 75 69 72 65 64 20 61 6e 64   as required and
112fc 20 75 70 64 61 74 65 20 74 68 65 0a 2a 2a 20 2a   update the.** *
112fd 70 70 4c 69 73 74 20 74 6f 20 70 6f 69 6e 74 20  ppList to point 
112fe 74 6f 20 74 68 65 20 75 6e 75 73 65 64 20 65 6c  to the unused el
112ff 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69  ements of the li
11300 73 74 2e 20 20 49 66 20 74 68 65 20 69 6e 70 75  st.  If the inpu
11301 74 0a 2a 2a 20 6c 69 73 74 20 63 6f 6e 74 61 69  t.** list contai
11302 6e 73 20 74 6f 6f 20 66 65 77 20 65 6c 65 6d 65  ns too few eleme
11303 6e 74 73 2c 20 74 68 65 6e 20 63 6f 6e 73 74 72  nts, then constr
11304 75 63 74 20 61 6e 20 69 6e 63 6f 6d 70 6c 65 74  uct an incomplet
11305 65 20 74 72 65 65 0a 2a 2a 20 61 6e 64 20 6c 65  e tree.** and le
11306 61 76 65 20 2a 70 70 4c 69 73 74 20 73 65 74 20  ave *ppList set 
11307 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52  to NULL..**.** R
11308 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
11309 74 6f 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  to the root of t
1130a 68 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62  he constructed b
1130b 69 6e 61 72 79 20 74 72 65 65 2e 0a 2a 2f 0a 73  inary tree..*/.s
1130c 74 61 74 69 63 20 73 74 72 75 63 74 20 52 6f 77  tatic struct Row
1130d 53 65 74 45 6e 74 72 79 20 2a 72 6f 77 53 65 74  SetEntry *rowSet
1130e 4e 44 65 65 70 54 72 65 65 28 0a 20 20 73 74 72  NDeepTree(.  str
1130f 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
11310 2a 2a 70 70 4c 69 73 74 2c 0a 20 20 69 6e 74 20  **ppList,.  int 
11311 69 44 65 70 74 68 0a 29 7b 0a 20 20 73 74 72 75  iDepth.){.  stru
11312 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
11313 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  p;         /* Ro
11314 6f 74 20 6f 66 20 74 68 65 20 6e 65 77 20 74 72  ot of the new tr
11315 65 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  ee */.  struct R
11316 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 4c 65 66  owSetEntry *pLef
11317 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 73  t;     /* Left s
11318 75 62 74 72 65 65 20 2a 2f 0a 20 20 69 66 28 20  ubtree */.  if( 
11319 2a 70 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  *ppList==0 ){.  
1131a 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1131b 20 20 69 66 28 20 69 44 65 70 74 68 3d 3d 31 20    if( iDepth==1 
1131c 29 7b 0a 20 20 20 20 70 20 3d 20 2a 70 70 4c 69  ){.    p = *ppLi
1131d 73 74 3b 0a 20 20 20 20 2a 70 70 4c 69 73 74 20  st;.    *ppList 
1131e 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  = p->pRight;.   
1131f 20 70 2d 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70   p->pLeft = p->p
11320 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20 72  Right = 0;.    r
11321 65 74 75 72 6e 20 70 3b 0a 20 20 7d 0a 20 20 70  eturn p;.  }.  p
11322 4c 65 66 74 20 3d 20 72 6f 77 53 65 74 4e 44 65  Left = rowSetNDe
11323 65 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69  epTree(ppList, i
11324 44 65 70 74 68 2d 31 29 3b 0a 20 20 70 20 3d 20  Depth-1);.  p = 
11325 2a 70 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70  *ppList;.  if( p
11326 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
11327 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 70  n pLeft;.  }.  p
11328 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b  ->pLeft = pLeft;
11329 0a 20 20 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e  .  *ppList = p->
1132a 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 52 69  pRight;.  p->pRi
1132b 67 68 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65  ght = rowSetNDee
1132c 70 54 72 65 65 28 70 70 4c 69 73 74 2c 20 69 44  pTree(ppList, iD
1132d 65 70 74 68 2d 31 29 3b 0a 20 20 72 65 74 75 72  epth-1);.  retur
1132e 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n p;.}../*.** Co
1132f 6e 76 65 72 74 20 61 20 73 6f 72 74 65 64 20 6c  nvert a sorted l
11330 69 73 74 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ist of elements 
11331 69 6e 74 6f 20 61 20 62 69 6e 61 72 79 20 74 72  into a binary tr
11332 65 65 2e 20 4d 61 6b 65 20 74 68 65 20 74 72 65  ee. Make the tre
11333 65 0a 2a 2a 20 61 73 20 64 65 65 70 20 61 73 20  e.** as deep as 
11334 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69  it needs to be i
11335 6e 20 6f 72 64 65 72 20 74 6f 20 63 6f 6e 74 61  n order to conta
11336 69 6e 20 74 68 65 20 65 6e 74 69 72 65 20 6c 69  in the entire li
11337 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74  st..*/.static st
11338 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11339 20 2a 72 6f 77 53 65 74 4c 69 73 74 54 6f 54 72   *rowSetListToTr
1133a 65 65 28 73 74 72 75 63 74 20 52 6f 77 53 65 74  ee(struct RowSet
1133b 45 6e 74 72 79 20 2a 70 4c 69 73 74 29 7b 0a 20  Entry *pList){. 
1133c 20 69 6e 74 20 69 44 65 70 74 68 3b 20 20 20 20   int iDepth;    
1133d 20 20 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20         /* Depth 
1133e 6f 66 20 74 68 65 20 74 72 65 65 20 73 6f 20 66  of the tree so f
1133f 61 72 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  ar */.  struct R
11340 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20 20  owSetEntry *p;  
11341 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
11342 74 72 65 65 20 72 6f 6f 74 20 2a 2f 0a 20 20 73  tree root */.  s
11343 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
11344 79 20 2a 70 4c 65 66 74 3b 20 20 20 2f 2a 20 4c  y *pLeft;   /* L
11345 65 66 74 20 73 75 62 74 72 65 65 20 2a 2f 0a 0a  eft subtree */..
11346 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21    assert( pList!
11347 3d 30 20 29 3b 0a 20 20 70 20 3d 20 70 4c 69 73  =0 );.  p = pLis
11348 74 3b 0a 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e  t;.  pList = p->
11349 70 52 69 67 68 74 3b 0a 20 20 70 2d 3e 70 4c 65  pRight;.  p->pLe
1134a 66 74 20 3d 20 70 2d 3e 70 52 69 67 68 74 20 3d  ft = p->pRight =
1134b 20 30 3b 0a 20 20 66 6f 72 28 69 44 65 70 74 68   0;.  for(iDepth
1134c 3d 31 3b 20 70 4c 69 73 74 3b 20 69 44 65 70 74  =1; pList; iDept
1134d 68 2b 2b 29 7b 0a 20 20 20 20 70 4c 65 66 74 20  h++){.    pLeft 
1134e 3d 20 70 3b 0a 20 20 20 20 70 20 3d 20 70 4c 69  = p;.    p = pLi
1134f 73 74 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20  st;.    pList = 
11350 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70  p->pRight;.    p
11351 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b  ->pLeft = pLeft;
11352 0a 20 20 20 20 70 2d 3e 70 52 69 67 68 74 20 3d  .    p->pRight =
11353 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72 65 65   rowSetNDeepTree
11354 28 26 70 4c 69 73 74 2c 20 69 44 65 70 74 68 29  (&pList, iDepth)
11355 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
11356 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
11357 72 74 20 74 68 65 20 6c 69 73 74 20 69 6e 20 70  rt the list in p
11358 2d 3e 70 45 6e 74 72 79 20 69 6e 74 6f 20 61 20  ->pEntry into a 
11359 73 6f 72 74 65 64 20 6c 69 73 74 20 69 66 20 69  sorted list if i
1135a 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 6f 72 74  t is not.** sort
1135b 65 64 20 61 6c 72 65 61 64 79 2e 20 20 49 66 20  ed already.  If 
1135c 74 68 65 72 65 20 69 73 20 61 20 62 69 6e 61 72  there is a binar
1135d 79 20 74 72 65 65 20 6f 6e 20 70 2d 3e 70 54 72  y tree on p->pTr
1135e 65 65 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76  ee, then.** conv
1135f 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6c 69  ert it into a li
11360 73 74 20 74 6f 6f 20 61 6e 64 20 6d 65 72 67 65  st too and merge
11361 20 69 74 20 69 6e 74 6f 20 74 68 65 20 70 2d 3e   it into the p->
11362 70 45 6e 74 72 79 20 6c 69 73 74 2e 0a 2a 2f 0a  pEntry list..*/.
11363 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53  static void rowS
11364 65 74 54 6f 4c 69 73 74 28 52 6f 77 53 65 74 20  etToList(RowSet 
11365 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e 69  *p){.  if( !p->i
11366 73 53 6f 72 74 65 64 20 29 7b 0a 20 20 20 20 72  sSorted ){.    r
11367 6f 77 53 65 74 53 6f 72 74 28 70 29 3b 0a 20 20  owSetSort(p);.  
11368 7d 0a 20 20 69 66 28 20 70 2d 3e 70 54 72 65 65  }.  if( p->pTree
11369 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 52   ){.    struct R
1136a 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 48 65 61  owSetEntry *pHea
1136b 64 2c 20 2a 70 54 61 69 6c 3b 0a 20 20 20 20 72  d, *pTail;.    r
1136c 6f 77 53 65 74 54 72 65 65 54 6f 4c 69 73 74 28  owSetTreeToList(
1136d 70 2d 3e 70 54 72 65 65 2c 20 26 70 48 65 61 64  p->pTree, &pHead
1136e 2c 20 26 70 54 61 69 6c 29 3b 0a 20 20 20 20 70  , &pTail);.    p
1136f 2d 3e 70 54 72 65 65 20 3d 20 30 3b 0a 20 20 20  ->pTree = 0;.   
11370 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77   p->pEntry = row
11371 53 65 74 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74  SetMerge(p->pEnt
11372 72 79 2c 20 70 48 65 61 64 29 3b 0a 20 20 7d 0a  ry, pHead);.  }.
11373 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74  }../*.** Extract
11374 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c   the smallest el
11375 65 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 52  ement from the R
11376 6f 77 53 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20  owSet..** Write 
11377 74 68 65 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f  the element into
11378 20 2a 70 52 6f 77 69 64 2e 20 20 52 65 74 75 72   *pRowid.  Retur
11379 6e 20 31 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  n 1 on success. 
1137a 20 52 65 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20   Return.** 0 if 
1137b 74 68 65 20 52 6f 77 53 65 74 20 69 73 20 61 6c  the RowSet is al
1137c 72 65 61 64 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a  ready empty..**.
1137d 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  ** After this ro
1137e 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63  utine has been c
1137f 61 6c 6c 65 64 2c 20 74 68 65 20 73 71 6c 69 74  alled, the sqlit
11380 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 29  e3RowSetInsert()
11381 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6d 61 79 20  .** routine may 
11382 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 61 67  not be called ag
11383 61 69 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45  ain.  .*/.SQLITE
11384 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
11385 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74 28 52  ite3RowSetNext(R
11386 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 20 2a 70  owSet *p, i64 *p
11387 52 6f 77 69 64 29 7b 0a 20 20 72 6f 77 53 65 74  Rowid){.  rowSet
11388 54 6f 4c 69 73 74 28 70 29 3b 0a 20 20 69 66 28  ToList(p);.  if(
11389 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20   p->pEntry ){.  
1138a 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 2d 3e 70    *pRowid = p->p
1138b 45 6e 74 72 79 2d 3e 76 3b 0a 20 20 20 20 70 2d  Entry->v;.    p-
1138c 3e 70 45 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e  >pEntry = p->pEn
1138d 74 72 79 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  try->pRight;.   
1138e 20 69 66 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d   if( p->pEntry==
1138f 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
11390 65 33 52 6f 77 53 65 74 43 6c 65 61 72 28 70 29  e3RowSetClear(p)
11391 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
11392 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  rn 1;.  }else{. 
11393 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
11394 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  .}../*.** Check 
11395 74 6f 20 73 65 65 20 69 66 20 65 6c 65 6d 65 6e  to see if elemen
11396 74 20 69 52 6f 77 69 64 20 77 61 73 20 69 6e 73  t iRowid was ins
11397 65 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74  erted into the t
11398 68 65 20 72 6f 77 73 65 74 20 61 73 0a 2a 2a 20  he rowset as.** 
11399 70 61 72 74 20 6f 66 20 61 6e 79 20 69 6e 73 65  part of any inse
1139a 72 74 20 62 61 74 63 68 20 70 72 69 6f 72 20 74  rt batch prior t
1139b 6f 20 69 42 61 74 63 68 2e 20 20 52 65 74 75 72  o iBatch.  Retur
1139c 6e 20 31 20 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c  n 1 or 0..*/.SQL
1139d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1139e 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73  sqlite3RowSetTes
1139f 74 28 52 6f 77 53 65 74 20 2a 70 52 6f 77 53 65  t(RowSet *pRowSe
113a0 74 2c 20 75 38 20 69 42 61 74 63 68 2c 20 73 71  t, u8 iBatch, sq
113a1 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
113a2 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f  id){.  struct Ro
113a3 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20  wSetEntry *p;.  
113a4 69 66 28 20 69 42 61 74 63 68 21 3d 70 52 6f 77  if( iBatch!=pRow
113a5 53 65 74 2d 3e 69 42 61 74 63 68 20 29 7b 0a 20  Set->iBatch ){. 
113a6 20 20 20 69 66 28 20 70 52 6f 77 53 65 74 2d 3e     if( pRowSet->
113a7 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 20 20  pEntry ){.      
113a8 72 6f 77 53 65 74 54 6f 4c 69 73 74 28 70 52 6f  rowSetToList(pRo
113a9 77 53 65 74 29 3b 0a 20 20 20 20 20 20 70 52 6f  wSet);.      pRo
113aa 77 53 65 74 2d 3e 70 54 72 65 65 20 3d 20 72 6f  wSet->pTree = ro
113ab 77 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28 70  wSetListToTree(p
113ac 52 6f 77 53 65 74 2d 3e 70 45 6e 74 72 79 29 3b  RowSet->pEntry);
113ad 0a 20 20 20 20 20 20 70 52 6f 77 53 65 74 2d 3e  .      pRowSet->
113ae 70 45 6e 74 72 79 20 3d 20 30 3b 0a 20 20 20 20  pEntry = 0;.    
113af 20 20 70 52 6f 77 53 65 74 2d 3e 70 4c 61 73 74    pRowSet->pLast
113b0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
113b1 70 52 6f 77 53 65 74 2d 3e 69 42 61 74 63 68 20  pRowSet->iBatch 
113b2 3d 20 69 42 61 74 63 68 3b 0a 20 20 7d 0a 20 20  = iBatch;.  }.  
113b3 70 20 3d 20 70 52 6f 77 53 65 74 2d 3e 70 54 72  p = pRowSet->pTr
113b4 65 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29  ee;.  while( p )
113b5 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 76 3c 69  {.    if( p->v<i
113b6 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 70  Rowid ){.      p
113b7 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20   = p->pRight;.  
113b8 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 76    }else if( p->v
113b9 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  >iRowid ){.     
113ba 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20   p = p->pLeft;. 
113bb 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
113bc 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
113bd 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
113be 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
113bf 2a 2a 20 45 6e 64 20 6f 66 20 72 6f 77 73 65 74  ** End of rowset
113c0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
113c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113c3 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
113c4 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61  ** Begin file pa
113c5 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ger.c **********
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 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
113c9 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
113ca 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
113cb 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
113cc 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
113cd 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
113ce 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
113cf 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
113d0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
113d1 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
113d2 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
113d3 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
113d4 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
113d5 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
113d6 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
113d7 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
113d8 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
113d9 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
113da 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
113db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113df 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
113e0 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  s is the impleme
113e1 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70  ntation of the p
113e2 61 67 65 20 63 61 63 68 65 20 73 75 62 73 79 73  age cache subsys
113e3 74 65 6d 20 6f 72 20 22 70 61 67 65 72 22 2e 0a  tem or "pager"..
113e4 2a 2a 20 0a 2a 2a 20 54 68 65 20 70 61 67 65 72  ** .** The pager
113e5 20 69 73 20 75 73 65 64 20 74 6f 20 61 63 63 65   is used to acce
113e6 73 73 20 61 20 64 61 74 61 62 61 73 65 20 64 69  ss a database di
113e7 73 6b 20 66 69 6c 65 2e 20 20 49 74 20 69 6d 70  sk file.  It imp
113e8 6c 65 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69  lements.** atomi
113e9 63 20 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c  c commit and rol
113ea 6c 62 61 63 6b 20 74 68 72 6f 75 67 68 20 74 68  lback through th
113eb 65 20 75 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e  e use of a journ
113ec 61 6c 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20  al file that.** 
113ed 69 73 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d  is separate from
113ee 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
113ef 6c 65 2e 20 20 54 68 65 20 70 61 67 65 72 20 61  le.  The pager a
113f0 6c 73 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66  lso implements f
113f1 69 6c 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74  ile.** locking t
113f2 6f 20 70 72 65 76 65 6e 74 20 74 77 6f 20 70 72  o prevent two pr
113f3 6f 63 65 73 73 65 73 20 66 72 6f 6d 20 77 72 69  ocesses from wri
113f4 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61  ting the same da
113f5 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73  tabase.** file s
113f6 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f  imultaneously, o
113f7 72 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 66 72  r one process fr
113f8 6f 6d 20 72 65 61 64 69 6e 67 20 74 68 65 20 64  om reading the d
113f9 61 74 61 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a  atabase while.**
113fa 20 61 6e 6f 74 68 65 72 20 69 73 20 77 72 69 74   another is writ
113fb 69 6e 67 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ing..*/.#ifndef 
113fc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b  SQLITE_OMIT_DISK
113fd 49 4f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73  IO../*.** Macros
113fe 20 66 6f 72 20 74 72 6f 75 62 6c 65 73 68 6f 6f   for troubleshoo
113ff 74 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20  ting.  Normally 
11400 74 75 72 6e 65 64 20 6f 66 66 0a 2a 2f 0a 23 69  turned off.*/.#i
11401 66 20 30 0a 69 6e 74 20 73 71 6c 69 74 65 33 50  f 0.int sqlite3P
11402 61 67 65 72 54 72 61 63 65 3d 31 3b 20 20 2f 2a  agerTrace=1;  /*
11403 20 54 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20   True to enable 
11404 74 72 61 63 69 6e 67 20 2a 2f 0a 23 64 65 66 69  tracing */.#defi
11405 6e 65 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  ne sqlite3DebugP
11406 72 69 6e 74 66 20 70 72 69 6e 74 66 0a 23 64 65  rintf printf.#de
11407 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 28  fine PAGERTRACE(
11408 58 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  X)     if( sqlit
11409 65 33 50 61 67 65 72 54 72 61 63 65 20 29 7b 20  e3PagerTrace ){ 
1140a 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
1140b 74 66 20 58 3b 20 7d 0a 23 65 6c 73 65 0a 23 64  tf X; }.#else.#d
1140c 65 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45  efine PAGERTRACE
1140d 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  (X).#endif../*.*
1140e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1140f 74 77 6f 20 6d 61 63 72 6f 73 20 61 72 65 20 75  two macros are u
11410 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 50  sed within the P
11411 41 47 45 52 54 52 41 43 45 28 29 20 6d 61 63 72  AGERTRACE() macr
11412 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 70  os above.** to p
11413 72 69 6e 74 20 6f 75 74 20 66 69 6c 65 2d 64 65  rint out file-de
11414 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a 2a 0a 2a  scriptors. .**.*
11415 2a 20 50 41 47 45 52 49 44 28 29 20 74 61 6b 65  * PAGERID() take
11416 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
11417 20 50 61 67 65 72 20 73 74 72 75 63 74 20 61 73   Pager struct as
11418 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 20 54   its argument. T
11419 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  he.** associated
1141a 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f 72   file-descriptor
1141b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 46 49   is returned. FI
1141c 4c 45 48 41 4e 44 4c 45 49 44 28 29 20 74 61 6b  LEHANDLEID() tak
1141d 65 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 66 69  es an sqlite3_fi
1141e 6c 65 0a 2a 2a 20 73 74 72 75 63 74 20 61 73 20  le.** struct as 
1141f 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  its argument..*/
11420 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 49 44  .#define PAGERID
11421 28 70 29 20 28 28 69 6e 74 29 28 70 2d 3e 66 64  (p) ((int)(p->fd
11422 29 29 0a 23 64 65 66 69 6e 65 20 46 49 4c 45 48  )).#define FILEH
11423 41 4e 44 4c 45 49 44 28 66 64 29 20 28 28 69 6e  ANDLEID(fd) ((in
11424 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  t)fd)../*.** The
11425 20 70 61 67 65 20 63 61 63 68 65 20 61 73 20 61   page cache as a
11426 20 77 68 6f 6c 65 20 69 73 20 61 6c 77 61 79 73   whole is always
11427 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66   in one of the f
11428 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 61 74  ollowing.** stat
11429 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45  es:.**.**   PAGE
1142a 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20 20  R_UNLOCK        
1142b 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69  The page cache i
1142c 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
1142d 72 65 61 64 69 6e 67 20 6f 72 20 0a 2a 2a 20 20  reading or .**  
1142e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1142f 20 20 20 20 20 77 72 69 74 69 6e 67 20 74 68 65       writing the
11430 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
11431 20 54 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20   There is no.** 
11432 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11433 20 20 20 20 20 20 64 61 74 61 20 68 65 6c 64 20        data held 
11434 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73  in memory.  This
11435 20 69 73 20 74 68 65 20 69 6e 69 74 69 61 6c 0a   is the initial.
11436 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
11437 20 20 20 20 20 20 20 20 20 73 74 61 74 65 2e 0a           state..
11438 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 48  **.**   PAGER_SH
11439 41 52 45 44 20 20 20 20 20 20 20 20 54 68 65 20  ARED        The 
1143a 70 61 67 65 20 63 61 63 68 65 20 69 73 20 72 65  page cache is re
1143b 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62 61  ading the databa
1143c 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  se..**          
1143d 20 20 20 20 20 20 20 20 20 20 20 20 20 57 72 69               Wri
1143e 74 69 6e 67 20 69 73 20 6e 6f 74 20 70 65 72 6d  ting is not perm
1143f 69 74 74 65 64 2e 20 20 54 68 65 72 65 20 63 61  itted.  There ca
11440 6e 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  n be.**         
11441 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 75                mu
11442 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 20 61  ltiple readers a
11443 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d  ccessing the sam
11444 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20  e database.**   
11445 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11446 20 20 20 20 66 69 6c 65 20 61 74 20 74 68 65 20      file at the 
11447 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a  same time..**.**
11448 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 56 45     PAGER_RESERVE
11449 44 20 20 20 20 20 20 54 68 69 73 20 70 72 6f 63  D      This proc
1144a 65 73 73 20 68 61 73 20 72 65 73 65 72 76 65 64  ess has reserved
1144b 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f   the database fo
1144c 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 20 20 20  r writing.**    
1144d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1144e 20 20 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79     but has not y
1144f 65 74 20 6d 61 64 65 20 61 6e 79 20 63 68 61 6e  et made any chan
11450 67 65 73 2e 20 20 4f 6e 6c 79 20 6f 6e 65 20 70  ges.  Only one p
11451 72 6f 63 65 73 73 0a 2a 2a 20 20 20 20 20 20 20  rocess.**       
11452 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11453 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 72 65  at a time can re
11454 73 65 72 76 65 20 74 68 65 20 64 61 74 61 62 61  serve the databa
11455 73 65 2e 20 20 54 68 65 20 6f 72 69 67 69 6e 61  se.  The origina
11456 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  l.**            
11457 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62             datab
11458 61 73 65 20 66 69 6c 65 20 68 61 73 20 6e 6f 74  ase file has not
11459 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 73   been modified s
1145a 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20 20  o other.**      
1145b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1145c 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20 73   processes may s
1145d 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67 20  till be reading 
1145e 74 68 65 20 6f 6e 2d 64 69 73 6b 0a 2a 2a 20 20  the on-disk.**  
1145f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11460 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66 69       database fi
11461 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45  le..**.**   PAGE
11462 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  R_EXCLUSIVE     
11463 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20 69  The page cache i
11464 73 20 77 72 69 74 69 6e 67 20 74 68 65 20 64 61  s writing the da
11465 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20  tabase..**      
11466 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11467 20 41 63 63 65 73 73 20 69 73 20 65 78 63 6c 75   Access is exclu
11468 73 69 76 65 2e 20 20 4e 6f 20 6f 74 68 65 72 20  sive.  No other 
11469 70 72 6f 63 65 73 73 65 73 20 6f 72 0a 2a 2a 20  processes or.** 
1146a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1146b 20 20 20 20 20 20 74 68 72 65 61 64 73 20 63 61        threads ca
1146c 6e 20 62 65 20 72 65 61 64 69 6e 67 20 6f 72 20  n be reading or 
1146d 77 72 69 74 69 6e 67 20 77 68 69 6c 65 20 6f 6e  writing while on
1146e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
1146f 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63 65             proce
11470 73 73 20 69 73 20 77 72 69 74 69 6e 67 2e 0a 2a  ss is writing..*
11471 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 59 4e  *.**   PAGER_SYN
11472 43 45 44 20 20 20 20 20 20 20 20 54 68 65 20 70  CED        The p
11473 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 74 68  ager moves to th
11474 69 73 20 73 74 61 74 65 20 66 72 6f 6d 20 50 41  is state from PA
11475 47 45 52 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a  GER_EXCLUSIVE.**
11476 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11477 20 20 20 20 20 20 20 61 66 74 65 72 20 61 6c 6c         after all
11478 20 64 69 72 74 79 20 70 61 67 65 73 20 68 61 76   dirty pages hav
11479 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  e been written t
1147a 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  o the.**        
1147b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
1147c 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
1147d 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 62 65   the file has be
1147e 65 6e 20 73 79 6e 63 65 64 20 74 6f 0a 2a 2a 20  en synced to.** 
1147f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11480 20 20 20 20 20 20 64 69 73 6b 2e 20 41 6c 6c 20        disk. All 
11481 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 6f 20  that remains to 
11482 64 6f 20 69 73 20 74 6f 20 72 65 6d 6f 76 65 20  do is to remove 
11483 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  or.**           
11484 20 20 20 20 20 20 20 20 20 20 20 20 74 72 75 6e              trun
11485 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  cate the journal
11486 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 74 72   file and the tr
11487 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 20 20  ansaction .**   
11488 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11489 20 20 20 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d      will be comm
1148a 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  itted..**.** The
1148b 20 70 61 67 65 20 63 61 63 68 65 20 63 6f 6d 65   page cache come
1148c 73 20 75 70 20 69 6e 20 50 41 47 45 52 5f 55 4e  s up in PAGER_UN
1148d 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73 74  LOCK.  The first
1148e 20 74 69 6d 65 20 61 0a 2a 2a 20 73 71 6c 69 74   time a.** sqlit
1148f 65 33 50 61 67 65 72 47 65 74 28 29 20 6f 63 63  e3PagerGet() occ
11490 75 72 73 2c 20 74 68 65 20 73 74 61 74 65 20 74  urs, the state t
11491 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 41  ransitions to PA
11492 47 45 52 5f 53 48 41 52 45 44 2e 0a 2a 2a 20 41  GER_SHARED..** A
11493 66 74 65 72 20 61 6c 6c 20 70 61 67 65 73 20 68  fter all pages h
11494 61 76 65 20 62 65 65 6e 20 72 65 6c 65 61 73 65  ave been release
11495 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 5f 70  d using sqlite_p
11496 61 67 65 5f 75 6e 72 65 66 28 29 2c 0a 2a 2a 20  age_unref(),.** 
11497 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69  the state transi
11498 74 69 6f 6e 73 20 62 61 63 6b 20 74 6f 20 50 41  tions back to PA
11499 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68 65  GER_UNLOCK.  The
1149a 20 66 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74   first time.** t
1149b 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  hat sqlite3Pager
1149c 57 72 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65  Write() is calle
1149d 64 2c 20 74 68 65 20 73 74 61 74 65 20 74 72 61  d, the state tra
1149e 6e 73 69 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 50  nsitions to.** P
1149f 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20 20  AGER_RESERVED.  
114a0 28 4e 6f 74 65 20 74 68 61 74 20 73 71 6c 69 74  (Note that sqlit
114a1 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20 63  e3PagerWrite() c
114a2 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 63 61  an only be.** ca
114a3 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f 75 74 73 74  lled on an outst
114a4 61 6e 64 69 6e 67 20 70 61 67 65 20 77 68 69 63  anding page whic
114a5 68 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  h means that the
114a6 20 70 61 67 65 72 20 6d 75 73 74 0a 2a 2a 20 62   pager must.** b
114a7 65 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45  e in PAGER_SHARE
114a8 44 20 62 65 66 6f 72 65 20 69 74 20 74 72 61 6e  D before it tran
114a9 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 47 45 52  sitions to PAGER
114aa 5f 52 45 53 45 52 56 45 44 2e 29 0a 2a 2a 20 50  _RESERVED.).** P
114ab 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 6d 65  AGER_RESERVED me
114ac 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20 69  ans that there i
114ad 73 20 61 6e 20 6f 70 65 6e 20 72 6f 6c 6c 62 61  s an open rollba
114ae 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54  ck journal..** T
114af 68 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74 6f  he transition to
114b0 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
114b1 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 20 61   occurs before a
114b2 6e 79 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72  ny changes.** ar
114b3 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 64 61  e made to the da
114b4 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 6f  tabase file, tho
114b5 75 67 68 20 77 72 69 74 65 73 20 74 6f 20 74 68  ugh writes to th
114b6 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a 6f  e rollback.** jo
114b7 75 72 6e 61 6c 20 6f 63 63 75 72 73 20 77 69 74  urnal occurs wit
114b8 68 20 6a 75 73 74 20 50 41 47 45 52 5f 52 45 53  h just PAGER_RES
114b9 45 52 56 45 44 2e 20 20 41 66 74 65 72 20 61 6e  ERVED.  After an
114ba 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c   sqlite3PagerRol
114bb 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f 72 20 73 71  lback().** or sq
114bc 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
114bd 50 68 61 73 65 54 77 6f 28 29 2c 20 74 68 65 20  PhaseTwo(), the 
114be 73 74 61 74 65 20 63 61 6e 20 67 6f 20 62 61 63  state can go bac
114bf 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45  k to PAGER_SHARE
114c0 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 63 61 6e 20  D,.** or it can 
114c1 73 74 61 79 20 61 74 20 50 41 47 45 52 5f 45 58  stay at PAGER_EX
114c2 43 4c 55 53 49 56 45 20 69 66 20 77 65 20 61 72  CLUSIVE if we ar
114c3 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 61  e in exclusive a
114c4 63 63 65 73 73 20 6d 6f 64 65 2e 0a 2a 2f 0a 23  ccess mode..*/.#
114c5 64 65 66 69 6e 65 20 50 41 47 45 52 5f 55 4e 4c  define PAGER_UNL
114c6 4f 43 4b 20 20 20 20 20 20 30 0a 23 64 65 66 69  OCK      0.#defi
114c7 6e 65 20 50 41 47 45 52 5f 53 48 41 52 45 44 20  ne PAGER_SHARED 
114c8 20 20 20 20 20 31 20 20 20 2f 2a 20 73 61 6d 65       1   /* same
114c9 20 61 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20   as SHARED_LOCK 
114ca 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
114cb 5f 52 45 53 45 52 56 45 44 20 20 20 20 32 20 20  _RESERVED    2  
114cc 20 2f 2a 20 73 61 6d 65 20 61 73 20 52 45 53 45   /* same as RESE
114cd 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65  RVED_LOCK */.#de
114ce 66 69 6e 65 20 50 41 47 45 52 5f 45 58 43 4c 55  fine PAGER_EXCLU
114cf 53 49 56 45 20 20 20 34 20 20 20 2f 2a 20 73 61  SIVE   4   /* sa
114d0 6d 65 20 61 73 20 45 58 43 4c 55 53 49 56 45 5f  me as EXCLUSIVE_
114d1 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20  LOCK */.#define 
114d2 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20 20  PAGER_SYNCED    
114d3 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63    5../*.** A mac
114d4 72 6f 20 75 73 65 64 20 66 6f 72 20 69 6e 76 6f  ro used for invo
114d5 6b 69 6e 67 20 74 68 65 20 63 6f 64 65 63 20 69  king the codec i
114d6 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 2a  f there is one.*
114d7 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
114d8 48 41 53 5f 43 4f 44 45 43 0a 23 20 64 65 66 69  HAS_CODEC.# defi
114d9 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c  ne CODEC1(P,D,N,
114da 58 2c 45 29 20 5c 0a 20 20 20 20 69 66 28 20 50  X,E) \.    if( P
114db 2d 3e 78 43 6f 64 65 63 20 26 26 20 50 2d 3e 78  ->xCodec && P->x
114dc 43 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c  Codec(P->pCodec,
114dd 44 2c 4e 2c 58 29 3d 3d 30 20 29 7b 20 45 3b 20  D,N,X)==0 ){ E; 
114de 7d 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43  }.# define CODEC
114df 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 5c  2(P,D,N,X,E,O) \
114e0 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f 64  .    if( P->xCod
114e1 65 63 3d 3d 30 20 29 7b 20 4f 3d 28 63 68 61 72  ec==0 ){ O=(char
114e2 2a 29 44 3b 20 7d 65 6c 73 65 20 5c 0a 20 20 20  *)D; }else \.   
114e3 20 69 66 28 20 28 4f 3d 28 63 68 61 72 2a 29 28   if( (O=(char*)(
114e4 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e 70 43 6f  P->xCodec(P->pCo
114e5 64 65 63 2c 44 2c 4e 2c 58 29 29 29 3d 3d 30 20  dec,D,N,X)))==0 
114e6 29 7b 20 45 3b 20 7d 0a 23 65 6c 73 65 0a 23 20  ){ E; }.#else.# 
114e7 64 65 66 69 6e 65 20 43 4f 44 45 43 31 28 50 2c  define CODEC1(P,
114e8 44 2c 4e 2c 58 2c 45 29 20 20 20 2f 2a 20 4e 4f  D,N,X,E)   /* NO
114e9 2d 4f 50 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20  -OP */.# define 
114ea 43 4f 44 45 43 32 28 50 2c 44 2c 4e 2c 58 2c 45  CODEC2(P,D,N,X,E
114eb 2c 4f 29 20 4f 3d 28 63 68 61 72 2a 29 44 0a 23  ,O) O=(char*)D.#
114ec 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
114ed 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64   maximum allowed
114ee 20 73 65 63 74 6f 72 20 73 69 7a 65 2e 20 36 34   sector size. 64
114ef 4b 69 42 2e 20 49 66 20 74 68 65 20 78 53 65 63  KiB. If the xSec
114f0 74 6f 72 73 69 7a 65 28 29 20 6d 65 74 68 6f 64  torsize() method
114f1 20 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 76   .** returns a v
114f2 61 6c 75 65 20 6c 61 72 67 65 72 20 74 68 61 6e  alue larger than
114f3 20 74 68 69 73 2c 20 74 68 65 6e 20 4d 41 58 5f   this, then MAX_
114f4 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 73 20 75  SECTOR_SIZE is u
114f5 73 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20  sed instead..** 
114f6 54 68 69 73 20 63 6f 75 6c 64 20 63 6f 6e 63 65  This could conce
114f7 69 76 61 62 6c 79 20 63 61 75 73 65 20 63 6f 72  ivably cause cor
114f8 72 75 70 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e  ruption followin
114f9 67 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  g a power failur
114fa 65 20 6f 6e 0a 2a 2a 20 73 75 63 68 20 61 20 73  e on.** such a s
114fb 79 73 74 65 6d 2e 20 54 68 69 73 20 69 73 20 63  ystem. This is c
114fc 75 72 72 65 6e 74 6c 79 20 61 6e 20 75 6e 64 6f  urrently an undo
114fd 63 75 6d 65 6e 74 65 64 20 6c 69 6d 69 74 2e 0a  cumented limit..
114fe 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f 53  */.#define MAX_S
114ff 45 43 54 4f 52 5f 53 49 5a 45 20 30 78 31 30 30  ECTOR_SIZE 0x100
11500 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  00../*.** An ins
11501 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
11502 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
11503 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f   is allocated fo
11504 72 20 65 61 63 68 20 61 63 74 69 76 65 0a 2a 2a  r each active.**
11505 20 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 73   savepoint and s
11506 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
11507 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 79 73 74  tion in the syst
11508 65 6d 2e 20 41 6c 6c 20 73 75 63 68 20 73 74 72  em. All such str
11509 75 63 74 75 72 65 73 0a 2a 2a 20 61 72 65 20 73  uctures.** are s
1150a 74 6f 72 65 64 20 69 6e 20 74 68 65 20 50 61 67  tored in the Pag
1150b 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20  er.aSavepoint[] 
1150c 61 72 72 61 79 2c 20 77 68 69 63 68 20 69 73 20  array, which is 
1150d 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a 2a  allocated and.**
1150e 20 72 65 73 69 7a 65 64 20 75 73 69 6e 67 20 73   resized using s
1150f 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 29 2e  qlite3Realloc().
11510 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 73 61  .**.** When a sa
11511 76 65 70 6f 69 6e 74 20 69 73 20 63 72 65 61 74  vepoint is creat
11512 65 64 2c 20 74 68 65 20 50 61 67 65 72 53 61 76  ed, the PagerSav
11513 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65  epoint.iHdrOffse
11514 74 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65  t field is.** se
11515 74 20 74 6f 20 30 2e 20 49 66 20 61 20 6a 6f 75  t to 0. If a jou
11516 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20 77  rnal-header is w
11517 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
11518 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 77 68 69  main journal whi
11519 6c 65 0a 2a 2a 20 74 68 65 20 73 61 76 65 70 6f  le.** the savepo
1151a 69 6e 74 20 69 73 20 61 63 74 69 76 65 2c 20 74  int is active, t
1151b 68 65 6e 20 69 48 64 72 4f 66 66 73 65 74 20 69  hen iHdrOffset i
1151c 73 20 73 65 74 20 74 6f 20 74 68 65 20 62 79 74  s set to the byt
1151d 65 20 6f 66 66 73 65 74 20 0a 2a 2a 20 69 6d 6d  e offset .** imm
1151e 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69  ediately followi
1151f 6e 67 20 74 68 65 20 6c 61 73 74 20 6a 6f 75 72  ng the last jour
11520 6e 61 6c 20 72 65 63 6f 72 64 20 77 72 69 74 74  nal record writt
11521 65 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e  en into the main
11522 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 62 65 66 6f  .** journal befo
11523 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 68  re the journal-h
11524 65 61 64 65 72 2e 20 54 68 69 73 20 69 73 20 72  eader. This is r
11525 65 71 75 69 72 65 64 20 64 75 72 69 6e 67 20 73  equired during s
11526 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 72 6f 6c 6c  avepoint.** roll
11527 62 61 63 6b 20 28 73 65 65 20 70 61 67 65 72 50  back (see pagerP
11528 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74  laybackSavepoint
11529 28 29 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ())..*/.typedef 
1152a 73 74 72 75 63 74 20 50 61 67 65 72 53 61 76 65  struct PagerSave
1152b 70 6f 69 6e 74 20 50 61 67 65 72 53 61 76 65 70  point PagerSavep
1152c 6f 69 6e 74 3b 0a 73 74 72 75 63 74 20 50 61 67  oint;.struct Pag
1152d 65 72 53 61 76 65 70 6f 69 6e 74 20 7b 0a 20 20  erSavepoint {.  
1152e 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20  i64 iOffset;    
1152f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11530 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20  Starting offset 
11531 69 6e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  in main journal 
11532 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f 66 66  */.  i64 iHdrOff
11533 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  set;            
11534 20 20 2f 2a 20 53 65 65 20 61 62 6f 76 65 20 2a    /* See above *
11535 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e 53  /.  Bitvec *pInS
11536 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20 20  avepoint;       
11537 20 2f 2a 20 53 65 74 20 6f 66 20 70 61 67 65 73   /* Set of pages
11538 20 69 6e 20 74 68 69 73 20 73 61 76 65 70 6f 69   in this savepoi
11539 6e 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 4f 72  nt */.  Pgno nOr
1153a 69 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ig;             
1153b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c       /* Original
1153c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
1153d 20 69 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67   in file */.  Pg
1153e 6e 6f 20 69 53 75 62 52 65 63 3b 20 20 20 20 20  no iSubRec;     
1153f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
11540 64 65 78 20 6f 66 20 66 69 72 73 74 20 72 65 63  dex of first rec
11541 6f 72 64 20 69 6e 20 73 75 62 2d 6a 6f 75 72 6e  ord in sub-journ
11542 61 6c 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  al */.};../*.** 
11543 41 20 6f 70 65 6e 20 70 61 67 65 20 63 61 63 68  A open page cach
11544 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
11545 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
11546 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  g structure..**.
11547 2a 2a 20 65 72 72 43 6f 64 65 0a 2a 2a 0a 2a 2a  ** errCode.**.**
11548 20 20 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65     Pager.errCode
11549 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f 20 53   may be set to S
1154a 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53 51 4c  QLITE_IOERR, SQL
1154b 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f 72 0a  ITE_CORRUPT, or.
1154c 2a 2a 20 20 20 6f 72 20 53 51 4c 49 54 45 5f 46  **   or SQLITE_F
1154d 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66  ULL. Once one of
1154e 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65   the first three
1154f 20 65 72 72 6f 72 73 20 6f 63 63 75 72 73 2c 20   errors occurs, 
11550 69 74 20 70 65 72 73 69 73 74 73 0a 2a 2a 20 20  it persists.**  
11551 20 61 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64   and is returned
11552 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f   as the result o
11553 66 20 65 76 65 72 79 20 6d 61 6a 6f 72 20 70 61  f every major pa
11554 67 65 72 20 41 50 49 20 63 61 6c 6c 2e 20 20 54  ger API call.  T
11555 68 65 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46  he.**   SQLITE_F
11556 55 4c 4c 20 72 65 74 75 72 6e 20 63 6f 64 65 20  ULL return code 
11557 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66 66  is slightly diff
11558 65 72 65 6e 74 2e 20 49 74 20 70 65 72 73 69 73  erent. It persis
11559 74 73 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68  ts only until th
1155a 65 0a 2a 2a 20 20 20 6e 65 78 74 20 73 75 63 63  e.**   next succ
1155b 65 73 73 66 75 6c 20 72 6f 6c 6c 62 61 63 6b 20  essful rollback 
1155c 69 73 20 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20  is performed on 
1155d 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e  the pager cache.
1155e 20 41 6c 73 6f 2c 0a 2a 2a 20 20 20 53 51 4c 49   Also,.**   SQLI
1155f 54 45 5f 46 55 4c 4c 20 64 6f 65 73 20 6e 6f 74  TE_FULL does not
11560 20 61 66 66 65 63 74 20 74 68 65 20 73 71 6c 69   affect the sqli
11561 74 65 33 50 61 67 65 72 47 65 74 28 29 20 61 6e  te3PagerGet() an
11562 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f  d sqlite3PagerLo
11563 6f 6b 75 70 28 29 0a 2a 2a 20 20 20 41 50 49 73  okup().**   APIs
11564 2c 20 74 68 65 79 20 6d 61 79 20 73 74 69 6c 6c  , they may still
11565 20 62 65 20 75 73 65 64 20 73 75 63 63 65 73 73   be used success
11566 66 75 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 64 62 53  fully..**.** dbS
11567 69 7a 65 56 61 6c 69 64 2c 20 64 62 53 69 7a 65  izeValid, dbSize
11568 2c 20 64 62 4f 72 69 67 53 69 7a 65 2c 20 64 62  , dbOrigSize, db
11569 46 69 6c 65 53 69 7a 65 0a 2a 2a 0a 2a 2a 20 20  FileSize.**.**  
1156a 20 4d 61 6e 61 67 69 6e 67 20 74 68 65 20 73 69   Managing the si
1156b 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
1156c 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73  se file in pages
1156d 20 69 73 20 61 20 6c 69 74 74 6c 65 20 63 6f 6d   is a little com
1156e 70 6c 69 63 61 74 65 64 2e 0a 2a 2a 20 20 20 54  plicated..**   T
1156f 68 65 20 76 61 72 69 61 62 6c 65 20 50 61 67 65  he variable Page
11570 72 2e 64 62 53 69 7a 65 20 63 6f 6e 74 61 69 6e  r.dbSize contain
11571 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
11572 70 61 67 65 73 20 74 68 61 74 20 74 68 65 20 64  pages that the d
11573 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 69 6d 61  atabase.**   ima
11574 67 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e  ge currently con
11575 74 61 69 6e 73 2e 20 41 73 20 74 68 65 20 64 61  tains. As the da
11576 74 61 62 61 73 65 20 69 6d 61 67 65 20 67 72 6f  tabase image gro
11577 77 73 20 6f 72 20 73 68 72 69 6e 6b 73 20 74 68  ws or shrinks th
11578 69 73 0a 2a 2a 20 20 20 76 61 72 69 61 62 6c 65  is.**   variable
11579 20 69 73 20 75 70 64 61 74 65 64 2e 20 54 68 65   is updated. The
1157a 20 76 61 72 69 61 62 6c 65 20 50 61 67 65 72 2e   variable Pager.
1157b 64 62 46 69 6c 65 53 69 7a 65 20 63 6f 6e 74 61  dbFileSize conta
1157c 69 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a  ins the number.*
1157d 2a 20 20 20 6f 66 20 70 61 67 65 73 20 69 6e 20  *   of pages in 
1157e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1157f 65 2e 20 54 68 69 73 20 6d 61 79 20 62 65 20 64  e. This may be d
11580 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 50 61  ifferent from Pa
11581 67 65 72 2e 64 62 53 69 7a 65 0a 2a 2a 20 20 20  ger.dbSize.**   
11582 69 66 20 73 6f 6d 65 20 70 61 67 65 73 20 68 61  if some pages ha
11583 76 65 20 62 65 65 6e 20 61 70 70 65 6e 64 65 64  ve been appended
11584 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
11585 20 69 6d 61 67 65 20 62 75 74 20 6e 6f 74 20 79   image but not y
11586 65 74 20 77 72 69 74 74 65 6e 0a 2a 2a 20 20 20  et written.**   
11587 6f 75 74 20 66 72 6f 6d 20 74 68 65 20 63 61 63  out from the cac
11588 68 65 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c  he to the actual
11589 20 66 69 6c 65 20 6f 6e 20 64 69 73 6b 2e 20 4f   file on disk. O
1158a 72 20 69 66 20 74 68 65 20 69 6d 61 67 65 20 68  r if the image h
1158b 61 73 20 62 65 65 6e 0a 2a 2a 20 20 20 74 72 75  as been.**   tru
1158c 6e 63 61 74 65 64 20 62 79 20 61 6e 20 69 6e 63  ncated by an inc
1158d 72 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 20  remental-vacuum 
1158e 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 50  operation. The P
1158f 61 67 65 72 2e 64 62 4f 72 69 67 53 69 7a 65 20  ager.dbOrigSize 
11590 76 61 72 69 61 62 6c 65 0a 2a 2a 20 20 20 63 6f  variable.**   co
11591 6e 74 61 69 6e 73 20 74 68 65 20 6e 75 6d 62 65  ntains the numbe
11592 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
11593 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  e database image
11594 20 77 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e   when the curren
11595 74 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 74 69  t.**   transacti
11596 6f 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 54  on was opened. T
11597 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61  he contents of a
11598 6c 6c 20 74 68 72 65 65 20 6f 66 20 74 68 65 73  ll three of thes
11599 65 20 76 61 72 69 61 62 6c 65 73 20 69 73 0a 2a  e variables is.*
1159a 2a 20 20 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74  *   only guarant
1159b 65 65 64 20 74 6f 20 62 65 20 63 6f 72 72 65 63  eed to be correc
1159c 74 20 69 66 20 74 68 65 20 62 6f 6f 6c 65 61 6e  t if the boolean
1159d 20 50 61 67 65 72 2e 64 62 53 69 7a 65 56 61 6c   Pager.dbSizeVal
1159e 69 64 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a 2a  id is true..**.*
1159f 2a 20 20 20 54 4f 44 4f 3a 20 55 6e 64 65 72 20  *   TODO: Under 
115a0 77 68 61 74 20 63 6f 6e 64 69 74 69 6f 6e 73 20  what conditions 
115a1 69 73 20 64 62 53 69 7a 65 56 61 6c 69 64 20 73  is dbSizeValid s
115a2 65 74 3f 20 43 6c 65 61 72 65 64 3f 0a 2a 2a 0a  et? Cleared?.**.
115a3 2a 2a 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  ** changeCountDo
115a4 6e 65 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20  ne.**.**   This 
115a5 62 6f 6f 6c 65 61 6e 20 76 61 72 69 61 62 6c 65  boolean variable
115a6 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65   is used to make
115a7 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 63   sure that the c
115a8 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 2a  hange-counter .*
115a9 2a 20 20 20 28 74 68 65 20 34 2d 62 79 74 65 20  *   (the 4-byte 
115aa 68 65 61 64 65 72 20 66 69 65 6c 64 20 61 74 20  header field at 
115ab 62 79 74 65 20 6f 66 66 73 65 74 20 32 34 20 6f  byte offset 24 o
115ac 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
115ad 69 6c 65 29 20 69 73 20 0a 2a 2a 20 20 20 6e 6f  ile) is .**   no
115ae 74 20 75 70 64 61 74 65 64 20 6d 6f 72 65 20 6f  t updated more o
115af 66 74 65 6e 20 74 68 61 6e 20 6e 65 63 65 73 73  ften than necess
115b0 61 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 49 74  ary. .**.**   It
115b1 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20   is set to true 
115b2 77 68 65 6e 20 74 68 65 20 63 68 61 6e 67 65 2d  when the change-
115b3 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20 69 73  counter field is
115b4 20 75 70 64 61 74 65 64 2c 20 77 68 69 63 68 20   updated, which 
115b5 0a 2a 2a 20 20 20 63 61 6e 20 6f 6e 6c 79 20 68  .**   can only h
115b6 61 70 70 65 6e 20 69 66 20 61 6e 20 65 78 63 6c  appen if an excl
115b7 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 68 65  usive lock is he
115b8 6c 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ld on the databa
115b9 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 49 74  se file..**   It
115ba 20 69 73 20 63 6c 65 61 72 65 64 20 28 73 65 74   is cleared (set
115bb 20 74 6f 20 66 61 6c 73 65 29 20 77 68 65 6e 65   to false) whene
115bc 76 65 72 20 61 6e 20 65 78 63 6c 75 73 69 76 65  ver an exclusive
115bd 20 6c 6f 63 6b 20 69 73 20 0a 2a 2a 20 20 20 72   lock is .**   r
115be 65 6c 69 6e 71 75 69 73 68 65 64 20 6f 6e 20 74  elinquished on t
115bf 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
115c0 2e 20 45 61 63 68 20 74 69 6d 65 20 61 20 74 72  . Each time a tr
115c1 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d  ansaction is com
115c2 6d 69 74 74 65 64 2c 0a 2a 2a 20 20 20 54 68 65  mitted,.**   The
115c3 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65   changeCountDone
115c4 20 66 6c 61 67 20 69 73 20 69 6e 73 70 65 63 74   flag is inspect
115c5 65 64 2e 20 49 66 20 69 74 20 69 73 20 74 72 75  ed. If it is tru
115c6 65 2c 20 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a  e, the work of.*
115c7 2a 20 20 20 75 70 64 61 74 69 6e 67 20 74 68 65  *   updating the
115c8 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20   change-counter 
115c9 69 73 20 6f 6d 69 74 74 65 64 20 66 6f 72 20 74  is omitted for t
115ca 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
115cb 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  action..**.**   
115cc 54 68 69 73 20 6d 65 63 68 61 6e 69 73 6d 20 6d  This mechanism m
115cd 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e 20 72  eans that when r
115ce 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73  unning in exclus
115cf 69 76 65 20 6d 6f 64 65 2c 20 61 20 63 6f 6e 6e  ive mode, a conn
115d0 65 63 74 69 6f 6e 20 0a 2a 2a 20 20 20 6e 65 65  ection .**   nee
115d1 64 20 6f 6e 6c 79 20 75 70 64 61 74 65 20 74 68  d only update th
115d2 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
115d3 20 6f 6e 63 65 2c 20 66 6f 72 20 74 68 65 20 66   once, for the f
115d4 69 72 73 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  irst transaction
115d5 0a 2a 2a 20 20 20 63 6f 6d 6d 69 74 74 65 64 2e  .**   committed.
115d6 0a 2a 2a 0a 2a 2a 20 64 62 4d 6f 64 69 66 69 65  .**.** dbModifie
115d7 64 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 64 62  d.**.**   The db
115d8 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20 69 73  Modified flag is
115d9 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 61 20   set whenever a 
115da 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73  database page is
115db 20 64 69 72 74 69 65 64 2e 0a 2a 2a 20 20 20 49   dirtied..**   I
115dc 74 20 69 73 20 63 6c 65 61 72 65 64 20 61 74 20  t is cleared at 
115dd 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20  the end of each 
115de 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
115df 2a 2a 20 20 20 49 74 20 69 73 20 75 73 65 64 20  **   It is used 
115e0 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20  when committing 
115e1 6f 72 20 6f 74 68 65 72 77 69 73 65 20 65 6e 64  or otherwise end
115e2 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ing a transactio
115e3 6e 2e 20 49 66 0a 2a 2a 20 20 20 74 68 65 20 64  n. If.**   the d
115e4 62 4d 6f 64 69 66 69 65 64 20 66 6c 61 67 20 69  bModified flag i
115e5 73 20 63 6c 65 61 72 20 74 68 65 6e 20 6c 65 73  s clear then les
115e6 73 20 77 6f 72 6b 20 68 61 73 20 74 6f 20 62 65  s work has to be
115e7 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75   done..**.** jou
115e8 72 6e 61 6c 53 74 61 72 74 65 64 0a 2a 2a 0a 2a  rnalStarted.**.*
115e9 2a 20 20 20 54 68 69 73 20 66 6c 61 67 20 69 73  *   This flag is
115ea 20 73 65 74 20 77 68 65 6e 65 76 65 72 20 74 68   set whenever th
115eb 65 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  e the main journ
115ec 61 6c 20 69 73 20 73 79 6e 63 65 64 2e 20 0a 2a  al is synced. .*
115ed 2a 0a 2a 2a 20 20 20 54 68 65 20 70 6f 69 6e 74  *.**   The point
115ee 20 6f 66 20 74 68 69 73 20 66 6c 61 67 20 69 73   of this flag is
115ef 20 74 68 61 74 20 69 74 20 6d 75 73 74 20 62 65   that it must be
115f0 20 73 65 74 20 61 66 74 65 72 20 74 68 65 20 0a   set after the .
115f1 2a 2a 20 20 20 66 69 72 73 74 20 6a 6f 75 72 6e  **   first journ
115f2 61 6c 20 68 65 61 64 65 72 20 69 6e 20 61 20 6a  al header in a j
115f3 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73 20  ournal file has 
115f4 62 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 20 64  been synced to d
115f5 69 73 6b 2e 0a 2a 2a 20 20 20 41 66 74 65 72 20  isk..**   After 
115f6 74 68 69 73 20 68 61 73 20 68 61 70 70 65 6e 65  this has happene
115f7 64 2c 20 6e 65 77 20 70 61 67 65 73 20 61 70 70  d, new pages app
115f8 65 6e 64 65 64 20 74 6f 20 74 68 65 20 64 61 74  ended to the dat
115f9 61 62 61 73 65 20 0a 2a 2a 20 20 20 64 6f 20 6e  abase .**   do n
115fa 6f 74 20 6e 65 65 64 20 74 68 65 20 50 47 48 44  ot need the PGHD
115fb 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
115fc 20 73 65 74 2c 20 61 73 20 74 68 65 79 20 64 6f   set, as they do
115fd 20 6e 6f 74 20 6e 65 65 64 0a 2a 2a 20 20 20 74   not need.**   t
115fe 6f 20 77 61 69 74 20 66 6f 72 20 61 20 6a 6f 75  o wait for a jou
115ff 72 6e 61 6c 20 73 79 6e 63 20 62 65 66 6f 72 65  rnal sync before
11600 20 74 68 65 79 20 63 61 6e 20 62 65 20 77 72 69   they can be wri
11601 74 74 65 6e 20 6f 75 74 20 74 6f 0a 2a 2a 20 20  tten out to.**  
11602 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
11603 6c 65 20 28 73 65 65 20 66 75 6e 63 74 69 6f 6e  le (see function
11604 20 70 61 67 65 72 5f 77 72 69 74 65 28 29 29 2e   pager_write()).
11605 0a 2a 2a 20 20 20 0a 2a 2a 20 73 65 74 4d 61 73  .**   .** setMas
11606 74 65 72 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73  ter.**.**   This
11607 20 76 61 72 69 61 62 6c 65 20 69 73 20 75 73 65   variable is use
11608 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74  d to ensure that
11609 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
1160a 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 0a 2a 2a  nal file name.**
1160b 20 20 20 28 69 66 20 61 6e 79 29 20 69 73 20 6f     (if any) is o
1160c 6e 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74 6f  nly written into
1160d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1160e 65 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  e once..**.**   
1160f 57 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e 67 20  When committing 
11610 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74  a transaction, t
11611 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
11612 6c 20 66 69 6c 65 20 6e 61 6d 65 20 28 69 66 20  l file name (if 
11613 61 6e 79 29 0a 2a 2a 20 20 20 6d 61 79 20 62 65  any).**   may be
11614 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
11615 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77  e journal file w
11616 68 69 6c 65 20 74 68 65 20 70 61 67 65 72 20 69  hile the pager i
11617 73 20 73 74 69 6c 6c 20 69 6e 0a 2a 2a 20 20 20  s still in.**   
11618 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73  PAGER_RESERVED s
11619 74 61 74 65 20 28 73 65 65 20 43 6f 6d 6d 69 74  tate (see Commit
1161a 50 68 61 73 65 4f 6e 65 28 29 20 66 6f 72 20 74  PhaseOne() for t
1161b 68 65 20 61 63 74 69 6f 6e 29 2e 20 49 74 0a 2a  he action). It.*
1161c 2a 20 20 20 74 68 65 6e 20 61 74 74 65 6d 70 74  *   then attempt
1161d 73 20 74 6f 20 75 70 67 72 61 64 65 20 74 6f 20  s to upgrade to 
1161e 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
1161f 6b 2e 20 49 66 20 74 68 69 73 20 61 74 74 65 6d  k. If this attem
11620 70 74 0a 2a 2a 20 20 20 66 61 69 6c 73 2c 20 74  pt.**   fails, t
11621 68 65 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20  hen SQLITE_BUSY 
11622 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65 64 20  may be returned 
11623 74 6f 20 74 68 65 20 75 73 65 72 20 61 6e 64 20  to the user and 
11624 74 68 65 20 75 73 65 72 0a 2a 2a 20 20 20 6d 61  the user.**   ma
11625 79 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d  y attempt to com
11626 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63 74  mit the transact
11627 69 6f 6e 20 61 67 61 69 6e 20 6c 61 74 65 72 20  ion again later 
11628 28 63 61 6c 6c 69 6e 67 0a 2a 2a 20 20 20 43 6f  (calling.**   Co
11629 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 61  mmitPhaseOne() a
1162a 67 61 69 6e 29 2e 20 54 68 69 73 20 66 6c 61 67  gain). This flag
1162b 20 69 73 20 75 73 65 64 20 74 6f 20 65 6e 73 75   is used to ensu
1162c 72 65 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20  re that the .** 
1162d 20 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c    master journal
1162e 20 6e 61 6d 65 20 69 73 20 6f 6e 6c 79 20 77 72   name is only wr
1162f 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75  itten to the jou
11630 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 66 69  rnal file the fi
11631 72 73 74 0a 2a 2a 20 20 20 74 69 6d 65 20 43 6f  rst.**   time Co
11632 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 69  mmitPhaseOne() i
11633 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20  s called..**.** 
11634 64 6f 4e 6f 74 53 79 6e 63 0a 2a 2a 0a 2a 2a 20  doNotSync.**.** 
11635 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20    This variable 
11636 69 73 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72  is set and clear
11637 65 64 20 62 79 20 73 71 6c 69 74 65 33 50 61 67  ed by sqlite3Pag
11638 65 72 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a  erWrite()..**.**
11639 20 6e 65 65 64 53 79 6e 63 0a 2a 2a 0a 2a 2a 20   needSync.**.** 
1163a 20 20 54 4f 44 4f 3a 20 49 74 20 6d 69 67 68 74    TODO: It might
1163b 20 62 65 20 65 61 73 69 65 72 20 74 6f 20 73 65   be easier to se
1163c 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  t this variable 
1163d 69 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48  in writeJournalH
1163e 64 72 28 29 0a 2a 2a 20 20 20 61 6e 64 20 77 72  dr().**   and wr
1163f 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  iteMasterJournal
11640 28 29 20 6f 6e 6c 79 2e 20 43 68 61 6e 67 65 20  () only. Change 
11641 69 74 73 20 6d 65 61 6e 69 6e 67 20 74 6f 20 22  its meaning to "
11642 75 6e 73 79 6e 63 65 64 20 64 61 74 61 0a 2a 2a  unsynced data.**
11643 20 20 20 68 61 73 20 62 65 65 6e 20 77 72 69 74     has been writ
11644 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
11645 61 6c 22 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 49  al"..**.** subjI
11646 6e 4d 65 6d 6f 72 79 0a 2a 2a 0a 2a 2a 20 20 20  nMemory.**.**   
11647 54 68 69 73 20 69 73 20 61 20 62 6f 6f 6c 65 61  This is a boolea
11648 6e 20 76 61 72 69 61 62 6c 65 2e 20 49 66 20 74  n variable. If t
11649 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 20 72 65  rue, then any re
1164a 71 75 69 72 65 64 20 73 75 62 2d 6a 6f 75 72 6e  quired sub-journ
1164b 61 6c 0a 2a 2a 20 20 20 69 73 20 6f 70 65 6e 65  al.**   is opene
1164c 64 20 61 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  d as an in-memor
1164d 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  y journal file. 
1164e 49 66 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 69  If false, then i
1164f 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20 73 75  n-memory.**   su
11650 62 2d 6a 6f 75 72 6e 61 6c 73 20 61 72 65 20 6f  b-journals are o
11651 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 69 6e 2d  nly used for in-
11652 6d 65 6d 6f 72 79 20 70 61 67 65 72 20 66 69 6c  memory pager fil
11653 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 61  es..*/.struct Pa
11654 67 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ger {.  sqlite3_
11655 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20  vfs *pVfs;      
11656 20 20 20 20 2f 2a 20 4f 53 20 66 75 6e 63 74 69      /* OS functi
11657 6f 6e 73 20 74 6f 20 75 73 65 20 66 6f 72 20 49  ons to use for I
11658 4f 20 2a 2f 0a 20 20 75 38 20 65 78 63 6c 75 73  O */.  u8 exclus
11659 69 76 65 4d 6f 64 65 3b 20 20 20 20 20 20 20 20  iveMode;        
1165a 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20 54     /* Boolean. T
1165b 72 75 65 20 69 66 20 6c 6f 63 6b 69 6e 67 5f 6d  rue if locking_m
1165c 6f 64 65 3d 3d 45 58 43 4c 55 53 49 56 45 20 2a  ode==EXCLUSIVE *
1165d 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4d 6f  /.  u8 journalMo
1165e 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  de;             
1165f 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 20 50 41 47  /* On of the PAG
11660 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 2a  ER_JOURNALMODE_*
11661 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20   values */.  u8 
11662 75 73 65 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20  useJournal;     
11663 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20           /* Use 
11664 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  a rollback journ
11665 61 6c 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20  al on this file 
11666 2a 2f 0a 20 20 75 38 20 6e 6f 52 65 61 64 6c 6f  */.  u8 noReadlo
11667 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ck;             
11668 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f 74 68 65   /* Do not bothe
11669 72 20 74 6f 20 6f 62 74 61 69 6e 20 72 65 61 64  r to obtain read
1166a 6c 6f 63 6b 73 20 2a 2f 0a 20 20 75 38 20 6e 6f  locks */.  u8 no
1166b 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Sync;           
1166c 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74         /* Do not
1166d 20 73 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61   sync the journa
1166e 6c 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75  l if true */.  u
1166f 38 20 66 75 6c 6c 53 79 6e 63 3b 20 20 20 20 20  8 fullSync;     
11670 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f             /* Do
11671 20 65 78 74 72 61 20 73 79 6e 63 73 20 6f 66 20   extra syncs of 
11672 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20  the journal for 
11673 72 6f 62 75 73 74 6e 65 73 73 20 2a 2f 0a 20 20  robustness */.  
11674 75 38 20 73 79 6e 63 5f 66 6c 61 67 73 3b 20 20  u8 sync_flags;  
11675 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
11676 6e 65 20 6f 66 20 53 59 4e 43 5f 4e 4f 52 4d 41  ne of SYNC_NORMA
11677 4c 20 6f 72 20 53 59 4e 43 5f 46 55 4c 4c 20 2a  L or SYNC_FULL *
11678 2f 0a 20 20 75 38 20 74 65 6d 70 46 69 6c 65 3b  /.  u8 tempFile;
11679 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1167a 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20  /* zFilename is 
1167b 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
1167c 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c   */.  u8 readOnl
1167d 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
1167e 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
1167f 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61  read-only databa
11680 73 65 20 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62  se */.  u8 memDb
11681 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11682 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 69      /* True to i
11683 6e 68 69 62 69 74 20 61 6c 6c 20 66 69 6c 65 20  nhibit all file 
11684 49 2f 4f 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  I/O */..  /* The
11685 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
11686 20 63 6f 6e 74 61 69 6e 73 20 74 68 6f 73 65 20   contains those 
11687 63 6c 61 73 73 20 6d 65 6d 62 65 72 73 20 74 68  class members th
11688 61 74 20 61 72 65 20 64 79 6e 61 6d 69 63 61 6c  at are dynamical
11689 6c 79 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 65 64  ly.  ** modified
1168a 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 6f   during normal o
1168b 70 65 72 61 74 69 6f 6e 73 2e 20 54 68 65 20 6f  perations. The o
1168c 74 68 65 72 20 76 61 72 69 61 62 6c 65 73 20 69  ther variables i
1168d 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  n this structure
1168e 0a 20 20 2a 2a 20 61 72 65 20 65 69 74 68 65 72  .  ** are either
1168f 20 63 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67   constant throug
11690 68 6f 75 74 20 74 68 65 20 6c 69 66 65 74 69 6d  hout the lifetim
11691 65 20 6f 66 20 74 68 65 20 70 61 67 65 72 2c 20  e of the pager, 
11692 6f 72 20 65 6c 73 65 0a 20 20 2a 2a 20 75 73 65  or else.  ** use
11693 64 20 74 6f 20 73 74 6f 72 65 20 63 6f 6e 66 69  d to store confi
11694 67 75 72 61 74 69 6f 6e 20 70 61 72 61 6d 65 74  guration paramet
11695 65 72 73 20 74 68 61 74 20 61 66 66 65 63 74 20  ers that affect 
11696 74 68 65 20 77 61 79 20 74 68 65 20 70 61 67 65  the way the page
11697 72 20 0a 20 20 2a 2a 20 6f 70 65 72 61 74 65 73  r .  ** operates
11698 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
11699 27 73 74 61 74 65 27 20 76 61 72 69 61 62 6c 65  'state' variable
1169a 20 69 73 20 64 65 73 63 72 69 62 65 64 20 69 6e   is described in
1169b 20 6d 6f 72 65 20 64 65 74 61 69 6c 20 61 6c 6f   more detail alo
1169c 6e 67 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a  ng with the.  **
1169d 20 64 65 73 63 72 69 70 74 69 6f 6e 73 20 6f 66   descriptions of
1169e 20 74 68 65 20 76 61 6c 75 65 73 20 69 74 20 6d   the values it m
1169f 61 79 20 74 61 6b 65 20 2d 20 50 41 47 45 52 5f  ay take - PAGER_
116a0 55 4e 4c 4f 43 4b 20 65 74 63 2e 20 4d 61 6e 79  UNLOCK etc. Many
116a1 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6f 74 68   of the.  ** oth
116a2 65 72 20 76 61 72 69 61 62 6c 65 73 20 69 6e 20  er variables in 
116a3 74 68 69 73 20 62 6c 6f 63 6b 20 61 72 65 20 64  this block are d
116a4 65 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 20  escribed in the 
116a5 63 6f 6d 6d 65 6e 74 20 64 69 72 65 63 74 6c 79  comment directly
116a6 20 0a 20 20 2a 2a 20 61 62 6f 76 65 20 74 68 69   .  ** above thi
116a7 73 20 63 6c 61 73 73 20 64 65 66 69 6e 69 74 69  s class definiti
116a8 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 75 38 20 73 74  on..  */.  u8 st
116a9 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ate;            
116aa 20 20 20 20 20 20 20 2f 2a 20 50 41 47 45 52 5f         /* PAGER_
116ab 55 4e 4c 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c  UNLOCK, _SHARED,
116ac 20 5f 52 45 53 45 52 56 45 44 2c 20 65 74 63 2e   _RESERVED, etc.
116ad 20 2a 2f 0a 20 20 75 38 20 64 62 4d 6f 64 69 66   */.  u8 dbModif
116ae 69 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ied;            
116af 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
116b0 72 65 20 61 72 65 20 61 6e 79 20 63 68 61 6e 67  re are any chang
116b1 65 73 20 74 6f 20 74 68 65 20 44 62 20 2a 2f 0a  es to the Db */.
116b2 20 20 75 38 20 6e 65 65 64 53 79 6e 63 3b 20 20    u8 needSync;  
116b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
116b4 20 54 72 75 65 20 69 66 20 61 6e 20 66 73 79 6e   True if an fsyn
116b5 63 28 29 20 69 73 20 6e 65 65 64 65 64 20 6f 6e  c() is needed on
116b6 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   the journal */.
116b7 20 20 75 38 20 6a 6f 75 72 6e 61 6c 53 74 61 72    u8 journalStar
116b8 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ted;          /*
116b9 20 54 72 75 65 20 69 66 20 68 65 61 64 65 72 20   True if header 
116ba 6f 66 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 79  of journal is sy
116bb 6e 63 65 64 20 2a 2f 0a 20 20 75 38 20 63 68 61  nced */.  u8 cha
116bc 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20  ngeCountDone;   
116bd 20 20 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74        /* Set aft
116be 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  er incrementing 
116bf 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
116c0 65 72 20 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61  er */.  u8 setMa
116c1 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  ster;           
116c2 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
116c3 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65   m-j name has be
116c4 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 72  en written to jr
116c5 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74  nl */.  u8 doNot
116c6 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Sync;           
116c7 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20      /* Boolean. 
116c8 57 68 69 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e  While true, do n
116c9 6f 74 20 73 70 69 6c 6c 20 74 68 65 20 63 61 63  ot spill the cac
116ca 68 65 20 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a  he */.  u8 dbSiz
116cb 65 56 61 6c 69 64 3b 20 20 20 20 20 20 20 20 20  eValid;         
116cc 20 20 20 20 2f 2a 20 53 65 74 20 77 68 65 6e 20      /* Set when 
116cd 64 62 53 69 7a 65 20 69 73 20 63 6f 72 72 65 63  dbSize is correc
116ce 74 20 2a 2f 0a 20 20 75 38 20 73 75 62 6a 49 6e  t */.  u8 subjIn
116cf 4d 65 6d 6f 72 79 3b 20 20 20 20 20 20 20 20 20  Memory;         
116d0 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 75 73     /* True to us
116d1 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 75 62 2d  e in-memory sub-
116d2 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a 20 20 50 67  journals */.  Pg
116d3 6e 6f 20 64 62 53 69 7a 65 3b 20 20 20 20 20 20  no dbSize;      
116d4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
116d5 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
116d6 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
116d7 20 20 50 67 6e 6f 20 64 62 4f 72 69 67 53 69 7a    Pgno dbOrigSiz
116d8 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
116d9 20 64 62 53 69 7a 65 20 62 65 66 6f 72 65 20 74   dbSize before t
116da 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  he current trans
116db 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f  action */.  Pgno
116dc 20 64 62 46 69 6c 65 53 69 7a 65 3b 20 20 20 20   dbFileSize;    
116dd 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
116de 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
116df 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
116e0 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65  */.  int errCode
116e1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
116e2 20 2f 2a 20 4f 6e 65 20 6f 66 20 73 65 76 65 72   /* One of sever
116e3 61 6c 20 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f  al kinds of erro
116e4 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 63  rs */.  int nRec
116e5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
116e6 20 20 20 20 2f 2a 20 50 61 67 65 73 20 6a 6f 75      /* Pages jou
116e7 72 6e 61 6c 6c 65 64 20 73 69 6e 63 65 20 6c 61  rnalled since la
116e8 73 74 20 6a 2d 68 65 61 64 65 72 20 77 72 69 74  st j-header writ
116e9 74 65 6e 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73  ten */.  u32 cks
116ea 75 6d 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20  umInit;         
116eb 20 20 20 20 20 2f 2a 20 51 75 61 73 69 2d 72 61       /* Quasi-ra
116ec 6e 64 6f 6d 20 76 61 6c 75 65 20 61 64 64 65 64  ndom value added
116ed 20 74 6f 20 65 76 65 72 79 20 63 68 65 63 6b 73   to every checks
116ee 75 6d 20 2a 2f 0a 20 20 75 33 32 20 6e 53 75 62  um */.  u32 nSub
116ef 52 65 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rec;            
116f0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
116f1 20 72 65 63 6f 72 64 73 20 77 72 69 74 74 65 6e   records written
116f2 20 74 6f 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20   to sub-journal 
116f3 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 49 6e  */.  Bitvec *pIn
116f4 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20  Journal;        
116f5 20 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20   /* One bit for 
116f6 65 61 63 68 20 70 61 67 65 20 69 6e 20 74 68 65  each page in the
116f7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
116f8 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
116f9 20 2a 66 64 3b 20 20 20 20 20 20 20 20 20 20 20   *fd;           
116fa 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70 74  /* File descript
116fb 6f 72 20 66 6f 72 20 64 61 74 61 62 61 73 65 20  or for database 
116fc 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
116fd 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20 20  e *jfd;         
116fe 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70   /* File descrip
116ff 74 6f 72 20 66 6f 72 20 6d 61 69 6e 20 6a 6f 75  tor for main jou
11700 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65  rnal */.  sqlite
11701 33 5f 66 69 6c 65 20 2a 73 6a 66 64 3b 20 20 20  3_file *sjfd;   
11702 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 64 65        /* File de
11703 73 63 72 69 70 74 6f 72 20 66 6f 72 20 73 75 62  scriptor for sub
11704 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36  -journal */.  i6
11705 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b 20 20 20  4 journalOff;   
11706 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
11707 72 65 6e 74 20 77 72 69 74 65 20 6f 66 66 73 65  rent write offse
11708 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t in the journal
11709 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a   file */.  i64 j
1170a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 20 20 20  ournalHdr;      
1170b 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 20 6f         /* Byte o
1170c 66 66 73 65 74 20 74 6f 20 70 72 65 76 69 6f 75  ffset to previou
1170d 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  s journal header
1170e 20 2a 2f 0a 20 20 50 61 67 65 72 53 61 76 65 70   */.  PagerSavep
1170f 6f 69 6e 74 20 2a 61 53 61 76 65 70 6f 69 6e 74  oint *aSavepoint
11710 3b 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 61 63  ; /* Array of ac
11711 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20  tive savepoints 
11712 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70 6f  */.  int nSavepo
11713 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  int;            
11714 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c   /* Number of el
11715 65 6d 65 6e 74 73 20 69 6e 20 61 53 61 76 65 70  ements in aSavep
11716 6f 69 6e 74 5b 5d 20 2a 2f 0a 20 20 63 68 61 72  oint[] */.  char
11717 20 64 62 46 69 6c 65 56 65 72 73 5b 31 36 5d 3b   dbFileVers[16];
11718 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67          /* Chang
11719 65 73 20 77 68 65 6e 65 76 65 72 20 64 61 74 61  es whenever data
1171a 62 61 73 65 20 66 69 6c 65 20 63 68 61 6e 67 65  base file change
1171b 73 20 2a 2f 0a 20 20 75 33 32 20 73 65 63 74 6f  s */.  u32 secto
1171c 72 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20  rSize;          
1171d 20 20 20 2f 2a 20 41 73 73 75 6d 65 64 20 73 65     /* Assumed se
1171e 63 74 6f 72 20 73 69 7a 65 20 64 75 72 69 6e 67  ctor size during
1171f 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20   rollback */..  
11720 75 31 36 20 6e 45 78 74 72 61 3b 20 20 20 20 20  u16 nExtra;     
11721 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
11722 64 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74  dd this many byt
11723 65 73 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65  es to each in-me
11724 6d 6f 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69  mory page */.  i
11725 31 36 20 6e 52 65 73 65 72 76 65 3b 20 20 20 20  16 nReserve;    
11726 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
11727 6d 62 65 72 20 6f 66 20 75 6e 75 73 65 64 20 62  mber of unused b
11728 79 74 65 73 20 61 74 20 65 6e 64 20 6f 66 20 65  ytes at end of e
11729 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 33  ach page */.  u3
1172a 32 20 76 66 73 46 6c 61 67 73 3b 20 20 20 20 20  2 vfsFlags;     
1172b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
1172c 67 73 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 76  gs for sqlite3_v
1172d 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20  fs.xOpen() */.  
1172e 69 6e 74 20 70 61 67 65 53 69 7a 65 3b 20 20 20  int pageSize;   
1172f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
11730 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
11731 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 50 67  n a page */.  Pg
11732 6e 6f 20 6d 78 50 67 6e 6f 3b 20 20 20 20 20 20  no mxPgno;      
11733 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
11734 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 69 7a  imum allowed siz
11735 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
11736 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69  e */.  char *zFi
11737 6c 65 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  lename;         
11738 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
11739 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1173a 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72  */.  char *zJour
1173b 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  nal;            
1173c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1173d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
1173e 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e    int (*xBusyHan
1173f 64 6c 65 72 29 28 76 6f 69 64 2a 29 3b 20 2f 2a  dler)(void*); /*
11740 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 63 61 6c   Function to cal
11741 6c 20 77 68 65 6e 20 62 75 73 79 20 2a 2f 0a 20  l when busy */. 
11742 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64   void *pBusyHand
11743 6c 65 72 41 72 67 3b 20 20 20 20 20 20 2f 2a 20  lerArg;      /* 
11744 43 6f 6e 74 65 78 74 20 61 72 67 75 6d 65 6e 74  Context argument
11745 20 66 6f 72 20 78 42 75 73 79 48 61 6e 64 6c 65   for xBusyHandle
11746 72 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  r */.#ifdef SQLI
11747 54 45 5f 54 45 53 54 0a 20 20 69 6e 74 20 6e 48  TE_TEST.  int nH
11748 69 74 2c 20 6e 4d 69 73 73 3b 20 20 20 20 20 20  it, nMiss;      
11749 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 20 68        /* Cache h
1174a 69 74 73 20 61 6e 64 20 6d 69 73 73 69 6e 67 20  its and missing 
1174b 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 61 64 2c 20  */.  int nRead, 
1174c 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20 20  nWrite;         
1174d 20 2f 2a 20 44 61 74 61 62 61 73 65 20 70 61 67   /* Database pag
1174e 65 73 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20  es read/written 
1174f 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 76 6f 69 64  */.#endif.  void
11750 20 28 2a 78 52 65 69 6e 69 74 65 72 29 28 44 62   (*xReiniter)(Db
11751 50 61 67 65 2a 29 3b 20 2f 2a 20 43 61 6c 6c 20  Page*); /* Call 
11752 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65  this routine whe
11753 6e 20 72 65 6c 6f 61 64 69 6e 67 20 70 61 67 65  n reloading page
11754 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  s */.#ifdef SQLI
11755 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 76  TE_HAS_CODEC.  v
11756 6f 69 64 20 2a 28 2a 78 43 6f 64 65 63 29 28 76  oid *(*xCodec)(v
11757 6f 69 64 2a 2c 76 6f 69 64 2a 2c 50 67 6e 6f 2c  oid*,void*,Pgno,
11758 69 6e 74 29 3b 20 2f 2a 20 52 6f 75 74 69 6e 65  int); /* Routine
11759 20 66 6f 72 20 65 6e 2f 64 65 63 6f 64 69 6e 67   for en/decoding
1175a 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64 20   data */.  void 
1175b 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67  (*xCodecSizeChng
1175c 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29  )(void*,int,int)
1175d 3b 20 2f 2a 20 4e 6f 74 69 66 79 20 6f 66 20 70  ; /* Notify of p
1175e 61 67 65 20 73 69 7a 65 20 63 68 61 6e 67 65 73  age size changes
1175f 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f   */.  void (*xCo
11760 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a 29 3b  decFree)(void*);
11761 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11762 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74  Destructor for t
11763 68 65 20 63 6f 64 65 63 20 2a 2f 0a 20 20 76 6f  he codec */.  vo
11764 69 64 20 2a 70 43 6f 64 65 63 3b 20 20 20 20 20  id *pCodec;     
11765 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
11766 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78  st argument to x
11767 43 6f 64 65 63 2e 2e 2e 20 6d 65 74 68 6f 64 73  Codec... methods
11768 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 68 61   */.#endif.  cha
11769 72 20 2a 70 54 6d 70 53 70 61 63 65 3b 20 20 20  r *pTmpSpace;   
1176a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
1176b 72 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  r.pageSize bytes
1176c 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74 6d   of space for tm
1176d 70 20 75 73 65 20 2a 2f 0a 20 20 69 36 34 20 6a  p use */.  i64 j
1176e 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b  ournalSizeLimit;
1176f 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6c         /* Size l
11770 69 6d 69 74 20 66 6f 72 20 70 65 72 73 69 73 74  imit for persist
11771 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ent journal file
11772 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a 70  s */.  PCache *p
11773 50 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 20  PCache;         
11774 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
11775 20 70 61 67 65 20 63 61 63 68 65 20 6f 62 6a 65   page cache obje
11776 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ct */.  sqlite3_
11777 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 3b  backup *pBackup;
11778 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
11779 6f 20 6c 69 73 74 20 6f 66 20 6f 6e 67 6f 69 6e  o list of ongoin
1177a 67 20 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73  g backup process
1177b 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  es */.};../*.** 
1177c 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c  The following gl
1177d 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 20 68  obal variables h
1177e 6f 6c 64 20 63 6f 75 6e 74 65 72 73 20 75 73 65  old counters use
1177f 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67  d for.** testing
11780 20 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 20   purposes only. 
11781 20 54 68 65 73 65 20 76 61 72 69 61 62 6c 65 73   These variables
11782 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 69 6e   do not exist in
11783 0a 2a 2a 20 61 20 6e 6f 6e 2d 74 65 73 74 69 6e  .** a non-testin
11784 67 20 62 75 69 6c 64 2e 20 20 54 68 65 73 65 20  g build.  These 
11785 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6e 6f  variables are no
11786 74 20 74 68 72 65 61 64 2d 73 61 66 65 2e 0a 2a  t thread-safe..*
11787 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
11788 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
11789 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65  int sqlite3_page
1178a 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 20 3d  r_readdb_count =
1178b 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72   0;    /* Number
1178c 20 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 72   of full pages r
1178d 65 61 64 20 66 72 6f 6d 20 44 42 20 2a 2f 0a 53  ead from DB */.S
1178e 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1178f 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74  lite3_pager_writ
11790 65 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20  edb_count = 0;  
11791 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 75   /* Number of fu
11792 6c 6c 20 70 61 67 65 73 20 77 72 69 74 74 65 6e  ll pages written
11793 20 74 6f 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45   to DB */.SQLITE
11794 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
11795 5f 70 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f  _pager_writej_co
11796 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e  unt = 0;    /* N
11797 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 77  umber of pages w
11798 72 69 74 74 65 6e 20 74 6f 20 6a 6f 75 72 6e 61  ritten to journa
11799 6c 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 50 41  l */.# define PA
1179a 47 45 52 5f 49 4e 43 52 28 76 29 20 20 76 2b 2b  GER_INCR(v)  v++
1179b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
1179c 50 41 47 45 52 5f 49 4e 43 52 28 76 29 0a 23 65  PAGER_INCR(v).#e
1179d 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f  ndif..../*.** Jo
1179e 75 72 6e 61 6c 20 66 69 6c 65 73 20 62 65 67 69  urnal files begi
1179f 6e 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f  n with the follo
117a0 77 69 6e 67 20 6d 61 67 69 63 20 73 74 72 69 6e  wing magic strin
117a1 67 2e 20 20 54 68 65 20 64 61 74 61 0a 2a 2a 20  g.  The data.** 
117a2 77 61 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  was obtained fro
117a3 6d 20 2f 64 65 76 2f 72 61 6e 64 6f 6d 2e 20 20  m /dev/random.  
117a4 49 74 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20  It is used only 
117a5 61 73 20 61 20 73 61 6e 69 74 79 20 63 68 65 63  as a sanity chec
117a6 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 76  k..**.** Since v
117a7 65 72 73 69 6f 6e 20 32 2e 38 2e 30 2c 20 74 68  ersion 2.8.0, th
117a8 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74  e journal format
117a9 20 63 6f 6e 74 61 69 6e 73 20 61 64 64 69 74 69   contains additi
117aa 6f 6e 61 6c 20 73 61 6e 69 74 79 0a 2a 2a 20 63  onal sanity.** c
117ab 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61 74  hecking informat
117ac 69 6f 6e 2e 20 20 49 66 20 74 68 65 20 70 6f 77  ion.  If the pow
117ad 65 72 20 66 61 69 6c 73 20 77 68 69 6c 65 20 74  er fails while t
117ae 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65  he journal is be
117af 69 6e 67 0a 2a 2a 20 77 72 69 74 74 65 6e 2c 20  ing.** written, 
117b0 73 65 6d 69 2d 72 61 6e 64 6f 6d 20 67 61 72 62  semi-random garb
117b1 61 67 65 20 64 61 74 61 20 6d 69 67 68 74 20 61  age data might a
117b2 70 70 65 61 72 20 69 6e 20 74 68 65 20 6a 6f 75  ppear in the jou
117b3 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 61 66 74  rnal.** file aft
117b4 65 72 20 70 6f 77 65 72 20 69 73 20 72 65 73 74  er power is rest
117b5 6f 72 65 64 2e 20 20 49 66 20 61 6e 20 61 74 74  ored.  If an att
117b6 65 6d 70 74 20 69 73 20 74 68 65 6e 20 6d 61 64  empt is then mad
117b7 65 0a 2a 2a 20 74 6f 20 72 6f 6c 6c 20 74 68 65  e.** to roll the
117b8 20 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2c 20 74   journal back, t
117b9 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 75 6c  he database coul
117ba 64 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20  d be corrupted. 
117bb 20 54 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 0a   The additional.
117bc 2a 2a 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69  ** sanity checki
117bd 6e 67 20 64 61 74 61 20 69 73 20 61 6e 20 61 74  ng data is an at
117be 74 65 6d 70 74 20 74 6f 20 64 69 73 63 6f 76 65  tempt to discove
117bf 72 20 74 68 65 20 67 61 72 62 61 67 65 20 69 6e  r the garbage in
117c0 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
117c1 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 2a  and ignore it..*
117c2 2a 0a 2a 2a 20 54 68 65 20 73 61 6e 69 74 79 20  *.** The sanity 
117c3 63 68 65 63 6b 69 6e 67 20 69 6e 66 6f 72 6d 61  checking informa
117c4 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6e 65 77  tion for the new
117c5 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20   journal format 
117c6 63 6f 6e 73 69 73 74 73 0a 2a 2a 20 6f 66 20 61  consists.** of a
117c7 20 33 32 2d 62 69 74 20 63 68 65 63 6b 73 75 6d   32-bit checksum
117c8 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66   on each page of
117c9 20 64 61 74 61 2e 20 20 54 68 65 20 63 68 65 63   data.  The chec
117ca 6b 73 75 6d 20 63 6f 76 65 72 73 20 62 6f 74 68  ksum covers both
117cb 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 6e 75 6d  .** the page num
117cc 62 65 72 20 61 6e 64 20 74 68 65 20 70 50 61 67  ber and the pPag
117cd 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74  er->pageSize byt
117ce 65 73 20 6f 66 20 64 61 74 61 20 66 6f 72 20 74  es of data for t
117cf 68 65 20 70 61 67 65 2e 0a 2a 2a 20 54 68 69 73  he page..** This
117d0 20 63 6b 73 75 6d 20 69 73 20 69 6e 69 74 69 61   cksum is initia
117d1 6c 69 7a 65 64 20 74 6f 20 61 20 33 32 2d 62 69  lized to a 32-bi
117d2 74 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 74  t random value t
117d3 68 61 74 20 61 70 70 65 61 72 73 20 69 6e 20 74  hat appears in t
117d4 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  he.** journal fi
117d5 6c 65 20 72 69 67 68 74 20 61 66 74 65 72 20 74  le right after t
117d6 68 65 20 68 65 61 64 65 72 2e 20 20 54 68 65 20  he header.  The 
117d7 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 69 7a  random initializ
117d8 65 72 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 2c  er is important,
117d9 0a 2a 2a 20 62 65 63 61 75 73 65 20 67 61 72 62  .** because garb
117da 61 67 65 20 64 61 74 61 20 74 68 61 74 20 61 70  age data that ap
117db 70 65 61 72 73 20 61 74 20 74 68 65 20 65 6e 64  pears at the end
117dc 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 69 73   of a journal is
117dd 20 6c 69 6b 65 6c 79 0a 2a 2a 20 64 61 74 61 20   likely.** data 
117de 74 68 61 74 20 77 61 73 20 6f 6e 63 65 20 69 6e  that was once in
117df 20 6f 74 68 65 72 20 66 69 6c 65 73 20 74 68 61   other files tha
117e0 74 20 68 61 76 65 20 6e 6f 77 20 62 65 65 6e 20  t have now been 
117e1 64 65 6c 65 74 65 64 2e 20 20 49 66 20 74 68 65  deleted.  If the
117e2 0a 2a 2a 20 67 61 72 62 61 67 65 20 64 61 74 61  .** garbage data
117e3 20 63 61 6d 65 20 66 72 6f 6d 20 61 6e 20 6f 62   came from an ob
117e4 73 6f 6c 65 74 65 20 6a 6f 75 72 6e 61 6c 20 66  solete journal f
117e5 69 6c 65 2c 20 74 68 65 20 63 68 65 63 6b 73 75  ile, the checksu
117e6 6d 73 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 63  ms might.** be c
117e7 6f 72 72 65 63 74 2e 20 20 42 75 74 20 62 79 20  orrect.  But by 
117e8 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65  initializing the
117e9 20 63 68 65 63 6b 73 75 6d 20 74 6f 20 72 61 6e   checksum to ran
117ea 64 6f 6d 20 76 61 6c 75 65 20 77 68 69 63 68 0a  dom value which.
117eb 2a 2a 20 69 73 20 64 69 66 66 65 72 65 6e 74 20  ** is different 
117ec 66 6f 72 20 65 76 65 72 79 20 6a 6f 75 72 6e 61  for every journa
117ed 6c 2c 20 77 65 20 6d 69 6e 69 6d 69 7a 65 20 74  l, we minimize t
117ee 68 61 74 20 72 69 73 6b 2e 0a 2a 2f 0a 73 74 61  hat risk..*/.sta
117ef 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
117f0 65 64 20 63 68 61 72 20 61 4a 6f 75 72 6e 61 6c  ed char aJournal
117f1 4d 61 67 69 63 5b 5d 20 3d 20 7b 0a 20 20 30 78  Magic[] = {.  0x
117f2 64 39 2c 20 30 78 64 35 2c 20 30 78 30 35 2c 20  d9, 0xd5, 0x05, 
117f3 30 78 66 39 2c 20 30 78 32 30 2c 20 30 78 61 31  0xf9, 0x20, 0xa1
117f4 2c 20 30 78 36 33 2c 20 30 78 64 37 2c 0a 7d 3b  , 0x63, 0xd7,.};
117f5 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65  ../*.** The size
117f6 20 6f 66 20 74 68 65 20 6f 66 20 65 61 63 68 20   of the of each 
117f7 70 61 67 65 20 72 65 63 6f 72 64 20 69 6e 20 74  page record in t
117f8 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 67 69  he journal is gi
117f9 76 65 6e 20 62 79 0a 2a 2a 20 74 68 65 20 66 6f  ven by.** the fo
117fa 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 2e 0a 2a  llowing macro..*
117fb 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41  /.#define JOURNA
117fc 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 20  L_PG_SZ(pPager) 
117fd 20 28 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53   ((pPager->pageS
117fe 69 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a  ize) + 8)../*.**
117ff 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   The journal hea
11800 64 65 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69  der size for thi
11801 73 20 70 61 67 65 72 2e 20 54 68 69 73 20 69 73  s pager. This is
11802 20 75 73 75 61 6c 6c 79 20 74 68 65 20 73 61 6d   usually the sam
11803 65 20 0a 2a 2a 20 73 69 7a 65 20 61 73 20 61 20  e .** size as a 
11804 73 69 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74  single disk sect
11805 6f 72 2e 20 53 65 65 20 61 6c 73 6f 20 73 65 74  or. See also set
11806 53 65 63 74 6f 72 53 69 7a 65 28 29 2e 0a 2a 2f  SectorSize()..*/
11807 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c  .#define JOURNAL
11808 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20  _HDR_SZ(pPager) 
11809 28 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53  (pPager->sectorS
1180a 69 7a 65 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ize)../*.** The 
1180b 6d 61 63 72 6f 20 4d 45 4d 44 42 20 69 73 20 74  macro MEMDB is t
1180c 72 75 65 20 69 66 20 77 65 20 61 72 65 20 64 65  rue if we are de
1180d 61 6c 69 6e 67 20 77 69 74 68 20 61 6e 20 69 6e  aling with an in
1180e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
1180f 2e 0a 2a 2a 20 57 65 20 64 6f 20 74 68 69 73 20  ..** We do this 
11810 61 73 20 61 20 6d 61 63 72 6f 20 73 6f 20 74 68  as a macro so th
11811 61 74 20 69 66 20 74 68 65 20 53 51 4c 49 54 45  at if the SQLITE
11812 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 20 6d  _OMIT_MEMORYDB m
11813 61 63 72 6f 20 69 73 20 73 65 74 2c 0a 2a 2a 20  acro is set,.** 
11814 74 68 65 20 76 61 6c 75 65 20 6f 66 20 4d 45 4d  the value of MEM
11815 44 42 20 77 69 6c 6c 20 62 65 20 61 20 63 6f 6e  DB will be a con
11816 73 74 61 6e 74 20 61 6e 64 20 74 68 65 20 63 6f  stant and the co
11817 6d 70 69 6c 65 72 20 77 69 6c 6c 20 6f 70 74 69  mpiler will opti
11818 6d 69 7a 65 0a 2a 2a 20 6f 75 74 20 63 6f 64 65  mize.** out code
11819 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 65 76 65   that would neve
1181a 72 20 65 78 65 63 75 74 65 2e 0a 2a 2f 0a 23 69  r execute..*/.#i
1181b 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1181c 5f 4d 45 4d 4f 52 59 44 42 0a 23 20 64 65 66 69  _MEMORYDB.# defi
1181d 6e 65 20 4d 45 4d 44 42 20 30 0a 23 65 6c 73 65  ne MEMDB 0.#else
1181e 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44 42 20  .# define MEMDB 
1181f 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 0a 23 65  pPager->memDb.#e
11820 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
11821 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70 61  maximum legal pa
11822 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32 5e  ge number is (2^
11823 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65 66  31 - 1)..*/.#def
11824 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50 47  ine PAGER_MAX_PG
11825 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a 23  NO 2147483647..#
11826 69 66 6e 64 65 66 20 4e 44 45 42 55 47 20 0a 2f  ifndef NDEBUG ./
11827 2a 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a 0a 2a  *.** Usage:.**.*
11828 2a 20 20 20 61 73 73 65 72 74 28 20 61 73 73 65  *   assert( asse
11829 72 74 5f 70 61 67 65 72 5f 73 74 61 74 65 28 70  rt_pager_state(p
1182a 50 61 67 65 72 29 20 29 3b 0a 2a 2f 0a 73 74 61  Pager) );.*/.sta
1182b 74 69 63 20 69 6e 74 20 61 73 73 65 72 74 5f 70  tic int assert_p
1182c 61 67 65 72 5f 73 74 61 74 65 28 50 61 67 65 72  ager_state(Pager
1182d 20 2a 70 50 61 67 65 72 29 7b 0a 0a 20 20 2f 2a   *pPager){..  /*
1182e 20 41 20 74 65 6d 70 2d 66 69 6c 65 20 69 73 20   A temp-file is 
1182f 61 6c 77 61 79 73 20 69 6e 20 50 41 47 45 52 5f  always in PAGER_
11830 45 58 43 4c 55 53 49 56 45 20 6f 72 20 50 41 47  EXCLUSIVE or PAG
11831 45 52 5f 53 59 4e 43 45 44 20 73 74 61 74 65 2e  ER_SYNCED state.
11832 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50   */.  assert( pP
11833 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d  ager->tempFile==
11834 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61  0 || pPager->sta
11835 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53  te>=PAGER_EXCLUS
11836 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  IVE );..  /* The
11837 20 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65   changeCountDone
11838 20 66 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20   flag is always 
11839 73 65 74 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c  set for temp-fil
1183a 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  es */.  assert( 
1183b 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
1183c 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d 3e 63  ==0 || pPager->c
1183d 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 29  hangeCountDone )
1183e 3b 0a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  ;..  return 1;.}
1183f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
11840 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 69 74  eturn true if it
11841 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f   is necessary to
11842 20 77 72 69 74 65 20 70 61 67 65 20 2a 70 50 67   write page *pPg
11843 20 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f   into the sub-jo
11844 75 72 6e 61 6c 2e 0a 2a 2a 20 41 20 70 61 67 65  urnal..** A page
11845 20 6e 65 65 64 73 20 74 6f 20 62 65 20 77 72 69   needs to be wri
11846 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 73 75  tten into the su
11847 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 74 68 65  b-journal if the
11848 72 65 20 65 78 69 73 74 73 20 6f 6e 65 0a 2a 2a  re exists one.**
11849 20 6f 72 20 6d 6f 72 65 20 6f 70 65 6e 20 73 61   or more open sa
1184a 76 65 70 6f 69 6e 74 73 20 66 6f 72 20 77 68 69  vepoints for whi
1184b 63 68 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68  ch:.**.**   * Th
1184c 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73  e page-number is
1184d 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
1184e 75 61 6c 20 74 6f 20 50 61 67 65 72 53 61 76 65  ual to PagerSave
1184f 70 6f 69 6e 74 2e 6e 4f 72 69 67 2c 20 61 6e 64  point.nOrig, and
11850 0a 2a 2a 20 20 20 2a 20 54 68 65 20 62 69 74 20  .**   * The bit 
11851 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
11852 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72   the page-number
11853 20 69 73 20 6e 6f 74 20 73 65 74 20 69 6e 0a 2a   is not set in.*
11854 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65 70  *     PagerSavep
11855 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e  oint.pInSavepoin
11856 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
11857 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67   subjRequiresPag
11858 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  e(PgHdr *pPg){. 
11859 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 67   Pgno pgno = pPg
1185a 2d 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20  ->pgno;.  Pager 
1185b 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
1185c 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 69 3b 0a  Pager;.  int i;.
1185d 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
1185e 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b  ger->nSavepoint;
1185f 20 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65 72   i++){.    Pager
11860 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20 26  Savepoint *p = &
11861 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69  pPager->aSavepoi
11862 6e 74 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70  nt[i];.    if( p
11863 2d 3e 6e 4f 72 69 67 3e 3d 70 67 6e 6f 20 26 26  ->nOrig>=pgno &&
11864 20 30 3d 3d 73 71 6c 69 74 65 33 42 69 74 76 65   0==sqlite3Bitve
11865 63 54 65 73 74 28 70 2d 3e 70 49 6e 53 61 76 65  cTest(p->pInSave
11866 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 20 29 7b 0a  point, pgno) ){.
11867 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
11868 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
11869 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn 0;.}../*.** R
1186a 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
1186b 65 20 70 61 67 65 20 69 73 20 61 6c 72 65 61 64  e page is alread
1186c 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  y in the journal
1186d 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1186e 20 69 6e 74 20 70 61 67 65 49 6e 4a 6f 75 72 6e   int pageInJourn
1186f 61 6c 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  al(PgHdr *pPg){.
11870 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
11871 42 69 74 76 65 63 54 65 73 74 28 70 50 67 2d 3e  BitvecTest(pPg->
11872 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
11873 61 6c 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  al, pPg->pgno);.
11874 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20  }../*.** Read a 
11875 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 66  32-bit integer f
11876 72 6f 6d 20 74 68 65 20 67 69 76 65 6e 20 66 69  rom the given fi
11877 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20  le descriptor.  
11878 53 74 6f 72 65 20 74 68 65 20 69 6e 74 65 67 65  Store the intege
11879 72 0a 2a 2a 20 74 68 61 74 20 69 73 20 72 65 61  r.** that is rea
1187a 64 20 69 6e 20 2a 70 52 65 73 2e 20 20 52 65 74  d in *pRes.  Ret
1187b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  urn SQLITE_OK if
1187c 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b   everything work
1187d 65 64 2c 20 6f 72 20 61 6e 0a 2a 2a 20 65 72 72  ed, or an.** err
1187e 6f 72 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 74  or code is somet
1187f 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
11880 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 76 61 6c 75 65  .**.** All value
11881 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20  s are stored on 
11882 64 69 73 6b 20 61 73 20 62 69 67 2d 65 6e 64 69  disk as big-endi
11883 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  an..*/.static in
11884 74 20 72 65 61 64 33 32 62 69 74 73 28 73 71 6c  t read32bits(sql
11885 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20 69  ite3_file *fd, i
11886 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20 2a  64 offset, u32 *
11887 70 52 65 73 29 7b 0a 20 20 75 6e 73 69 67 6e 65  pRes){.  unsigne
11888 64 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20 20  d char ac[4];.  
11889 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33  int rc = sqlite3
1188a 4f 73 52 65 61 64 28 66 64 2c 20 61 63 2c 20 73  OsRead(fd, ac, s
1188b 69 7a 65 6f 66 28 61 63 29 2c 20 6f 66 66 73 65  izeof(ac), offse
1188c 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  t);.  if( rc==SQ
1188d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a  LITE_OK ){.    *
1188e 70 52 65 73 20 3d 20 73 71 6c 69 74 65 33 47 65  pRes = sqlite3Ge
1188f 74 34 62 79 74 65 28 61 63 29 3b 0a 20 20 7d 0a  t4byte(ac);.  }.
11890 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
11891 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 33 32  /*.** Write a 32
11892 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69 6e 74  -bit integer int
11893 6f 20 61 20 73 74 72 69 6e 67 20 62 75 66 66 65  o a string buffe
11894 72 20 69 6e 20 62 69 67 2d 65 6e 64 69 61 6e 20  r in big-endian 
11895 62 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 23  byte order..*/.#
11896 64 65 66 69 6e 65 20 70 75 74 33 32 62 69 74 73  define put32bits
11897 28 41 2c 42 29 20 20 73 71 6c 69 74 65 33 50 75  (A,B)  sqlite3Pu
11898 74 34 62 79 74 65 28 28 75 38 2a 29 41 2c 42 29  t4byte((u8*)A,B)
11899 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20  ../*.** Write a 
1189a 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20 69  32-bit integer i
1189b 6e 74 6f 20 74 68 65 20 67 69 76 65 6e 20 66 69  nto the given fi
1189c 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20  le descriptor.  
1189d 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
1189e 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 6f  .** on success o
1189f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  r an error code 
118a0 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  is something goe
118a1 73 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74  s wrong..*/.stat
118a2 69 63 20 69 6e 74 20 77 72 69 74 65 33 32 62 69  ic int write32bi
118a3 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ts(sqlite3_file 
118a4 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c  *fd, i64 offset,
118a5 20 75 33 32 20 76 61 6c 29 7b 0a 20 20 63 68 61   u32 val){.  cha
118a6 72 20 61 63 5b 34 5d 3b 0a 20 20 70 75 74 33 32  r ac[4];.  put32
118a7 62 69 74 73 28 61 63 2c 20 76 61 6c 29 3b 0a 20  bits(ac, val);. 
118a8 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 4f   return sqlite3O
118a9 73 57 72 69 74 65 28 66 64 2c 20 61 63 2c 20 34  sWrite(fd, ac, 4
118aa 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a  , offset);.}../*
118ab 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74  .** The argument
118ac 20 74 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 69   to this macro i
118ad 73 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70  s a file descrip
118ae 74 6f 72 20 28 74 79 70 65 20 73 71 6c 69 74 65  tor (type sqlite
118af 33 5f 66 69 6c 65 2a 29 2e 0a 2a 2a 20 52 65 74  3_file*)..** Ret
118b0 75 72 6e 20 30 20 69 66 20 69 74 20 69 73 20 6e  urn 0 if it is n
118b1 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 6e 6f 6e 2d  ot open, or non-
118b2 7a 65 72 6f 20 28 62 75 74 20 6e 6f 74 20 31 29  zero (but not 1)
118b3 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a 2a 2a   if it is..**.**
118b4 20 54 68 69 73 20 69 73 20 73 6f 20 74 68 61 74   This is so that
118b5 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e   expressions can
118b6 20 62 65 20 77 72 69 74 74 65 6e 20 61 73 3a 0a   be written as:.
118b7 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 69 73 4f 70  **.**   if( isOp
118b8 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
118b9 29 7b 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73  ){ ....**.** ins
118ba 74 65 61 64 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20  tead of.**.**   
118bb 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 66 64 2d  if( pPager->jfd-
118bc 3e 70 4d 65 74 68 6f 64 73 20 29 7b 20 2e 2e 2e  >pMethods ){ ...
118bd 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 69 73 4f 70  .*/.#define isOp
118be 65 6e 28 70 46 64 29 20 28 28 70 46 64 29 2d 3e  en(pFd) ((pFd)->
118bf 70 4d 65 74 68 6f 64 73 29 0a 0a 2f 2a 0a 2a 2a  pMethods)../*.**
118c0 20 49 66 20 66 69 6c 65 20 70 46 64 20 69 73 20   If file pFd is 
118c1 6f 70 65 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74  open, call sqlit
118c2 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 20 6f 6e 20  e3OsUnlock() on 
118c3 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  it..*/.static in
118c4 74 20 6f 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  t osUnlock(sqlit
118c5 65 33 5f 66 69 6c 65 20 2a 70 46 64 2c 20 69 6e  e3_file *pFd, in
118c6 74 20 65 4c 6f 63 6b 29 7b 0a 20 20 69 66 28 20  t eLock){.  if( 
118c7 21 69 73 4f 70 65 6e 28 70 46 64 29 20 29 7b 0a  !isOpen(pFd) ){.
118c8 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
118c9 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  E_OK;.  }.  retu
118ca 72 6e 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  rn sqlite3OsUnlo
118cb 63 6b 28 70 46 64 2c 20 65 4c 6f 63 6b 29 3b 0a  ck(pFd, eLock);.
118cc 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
118cd 6e 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65  nction determine
118ce 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  s whether or not
118cf 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74   the atomic-writ
118d0 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0a 2a  e optimization.*
118d1 2a 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69  * can be used wi
118d2 74 68 20 74 68 69 73 20 70 61 67 65 72 2e 20 54  th this pager. T
118d3 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
118d4 63 61 6e 20 62 65 20 75 73 65 64 20 69 66 3a 0a  can be used if:.
118d5 2a 2a 0a 2a 2a 20 20 28 61 29 20 74 68 65 20 76  **.**  (a) the v
118d6 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
118d7 20 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74   OsDeviceCharact
118d8 65 72 69 73 74 69 63 73 28 29 20 69 6e 64 69 63  eristics() indic
118d9 61 74 65 73 20 74 68 61 74 0a 2a 2a 20 20 20 20  ates that.**    
118da 20 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67    a database pag
118db 65 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e  e may be written
118dc 20 61 74 6f 6d 69 63 61 6c 6c 79 2c 20 61 6e 64   atomically, and
118dd 0a 2a 2a 20 20 28 62 29 20 74 68 65 20 76 61 6c  .**  (b) the val
118de 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f  ue returned by O
118df 73 53 65 63 74 6f 72 53 69 7a 65 28 29 20 69 73  sSectorSize() is
118e0 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
118e1 75 61 6c 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74  ual.**      to t
118e2 68 65 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a  he page size..**
118e3 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 61  .** The optimiza
118e4 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 61 6c 77  tion is also alw
118e5 61 79 73 20 65 6e 61 62 6c 65 64 20 66 6f 72 20  ays enabled for 
118e6 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e  temporary files.
118e7 20 49 74 20 69 73 0a 2a 2a 20 61 6e 20 65 72 72   It is.** an err
118e8 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  or to call this 
118e9 66 75 6e 63 74 69 6f 6e 20 69 66 20 70 50 61 67  function if pPag
118ea 65 72 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20  er is opened on 
118eb 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20  an in-memory.** 
118ec 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
118ed 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74  If the optimizat
118ee 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 75 73  ion cannot be us
118ef 65 64 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65  ed, 0 is returne
118f0 64 2e 20 49 66 20 69 74 20 63 61 6e 20 62 65 20  d. If it can be 
118f1 75 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  used,.** then th
118f2 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
118f3 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20   is the size of 
118f4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
118f5 20 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e 74   when it.** cont
118f6 61 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 64 61  ains rollback da
118f7 74 61 20 66 6f 72 20 65 78 61 63 74 6c 79 20 6f  ta for exactly o
118f8 6e 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 64  ne page..*/.#ifd
118f9 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
118fa 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 73 74  _ATOMIC_WRITE.st
118fb 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 75 66  atic int jrnlBuf
118fc 66 65 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70  ferSize(Pager *p
118fd 50 61 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74  Pager){.  assert
118fe 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66  ( !MEMDB );.  if
118ff 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  ( !pPager->tempF
11900 69 6c 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 64  ile ){.    int d
11901 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
11902 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11903 44 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72  Device character
11904 69 73 74 69 63 73 20 2a 2f 0a 20 20 20 20 69 6e  istics */.    in
11905 74 20 6e 53 65 63 74 6f 72 3b 20 20 20 20 20 20  t nSector;      
11906 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11907 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 2a  /* Sector size *
11908 2f 0a 20 20 20 20 69 6e 74 20 73 7a 50 61 67 65  /.    int szPage
11909 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1190a 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
1190b 73 69 7a 65 20 2a 2f 0a 0a 20 20 20 20 61 73 73  size */..    ass
1190c 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67  ert( isOpen(pPag
1190d 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20 20 20 64  er->fd) );.    d
1190e 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76  c = sqlite3OsDev
1190f 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
11910 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a  cs(pPager->fd);.
11911 20 20 20 20 6e 53 65 63 74 6f 72 20 3d 20 70 50      nSector = pP
11912 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65  ager->sectorSize
11913 3b 0a 20 20 20 20 73 7a 50 61 67 65 20 3d 20 70  ;.    szPage = p
11914 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b  Pager->pageSize;
11915 0a 0a 20 20 20 20 61 73 73 65 72 74 28 53 51 4c  ..    assert(SQL
11916 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
11917 35 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a  512==(512>>8));.
11918 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54      assert(SQLIT
11919 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34  E_IOCAP_ATOMIC64
1191a 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a  K==(65536>>8));.
1191b 20 20 20 20 69 66 28 20 30 3d 3d 28 64 63 26 28      if( 0==(dc&(
1191c 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f  SQLITE_IOCAP_ATO
1191d 4d 49 43 7c 28 73 7a 50 61 67 65 3e 3e 38 29 29  MIC|(szPage>>8))
1191e 20 7c 7c 20 6e 53 65 63 74 6f 72 3e 73 7a 50 61   || nSector>szPa
1191f 67 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ge) ){.      ret
11920 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
11921 0a 0a 20 20 72 65 74 75 72 6e 20 4a 4f 55 52 4e  ..  return JOURN
11922 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
11923 29 20 2b 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53  ) + JOURNAL_PG_S
11924 5a 28 70 50 61 67 65 72 29 3b 0a 7d 0a 23 65 6e  Z(pPager);.}.#en
11925 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51  dif../*.** If SQ
11926 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
11927 20 69 73 20 64 65 66 69 6e 65 64 20 74 68 65 6e   is defined then
11928 20 77 65 20 64 6f 20 73 6f 6d 65 20 73 61 6e 69   we do some sani
11929 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 6f  ty checking.** o
1192a 6e 20 74 68 65 20 63 61 63 68 65 20 75 73 69 6e  n the cache usin
1192b 67 20 61 20 68 61 73 68 20 66 75 6e 63 74 69 6f  g a hash functio
1192c 6e 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  n.  This is used
1192d 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20   for testing.** 
1192e 61 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e  and debugging on
1192f 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ly..*/.#ifdef SQ
11930 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
11931 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
11932 33 32 2d 62 69 74 20 68 61 73 68 20 6f 66 20 74  32-bit hash of t
11933 68 65 20 70 61 67 65 20 64 61 74 61 20 66 6f 72  he page data for
11934 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69   pPage..*/.stati
11935 63 20 75 33 32 20 70 61 67 65 72 5f 64 61 74 61  c u32 pager_data
11936 68 61 73 68 28 69 6e 74 20 6e 42 79 74 65 2c 20  hash(int nByte, 
11937 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
11938 44 61 74 61 29 7b 0a 20 20 75 33 32 20 68 61 73  Data){.  u32 has
11939 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a  h = 0;.  int i;.
1193a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 79    for(i=0; i<nBy
1193b 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 68 61  te; i++){.    ha
1193c 73 68 20 3d 20 28 68 61 73 68 2a 31 30 33 39 29  sh = (hash*1039)
1193d 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a 20 20 7d   + pData[i];.  }
1193e 0a 20 20 72 65 74 75 72 6e 20 68 61 73 68 3b 0a  .  return hash;.
1193f 7d 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 67  }.static u32 pag
11940 65 72 5f 70 61 67 65 68 61 73 68 28 50 67 48 64  er_pagehash(PgHd
11941 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 72 65 74  r *pPage){.  ret
11942 75 72 6e 20 70 61 67 65 72 5f 64 61 74 61 68 61  urn pager_dataha
11943 73 68 28 70 50 61 67 65 2d 3e 70 50 61 67 65 72  sh(pPage->pPager
11944 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28 75 6e 73  ->pageSize, (uns
11945 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 50 61  igned char *)pPa
11946 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d 0a 73 74  ge->pData);.}.st
11947 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f  atic void pager_
11948 73 65 74 5f 70 61 67 65 68 61 73 68 28 50 67 48  set_pagehash(PgH
11949 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 70 50  dr *pPage){.  pP
1194a 61 67 65 2d 3e 70 61 67 65 48 61 73 68 20 3d 20  age->pageHash = 
1194b 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
1194c 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Page);.}../*.** 
1194d 54 68 65 20 43 48 45 43 4b 5f 50 41 47 45 20 6d  The CHECK_PAGE m
1194e 61 63 72 6f 20 74 61 6b 65 73 20 61 20 50 67 48  acro takes a PgH
1194f 64 72 2a 20 61 73 20 61 6e 20 61 72 67 75 6d 65  dr* as an argume
11950 6e 74 2e 20 49 66 20 53 51 4c 49 54 45 5f 43 48  nt. If SQLITE_CH
11951 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20 69 73 20  ECK_PAGES.** is 
11952 64 65 66 69 6e 65 64 2c 20 61 6e 64 20 4e 44 45  defined, and NDE
11953 42 55 47 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  BUG is not defin
11954 65 64 2c 20 61 6e 20 61 73 73 65 72 74 28 29 20  ed, an assert() 
11955 73 74 61 74 65 6d 65 6e 74 20 63 68 65 63 6b 73  statement checks
11956 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 70 61 67  .** that the pag
11957 65 20 69 73 20 65 69 74 68 65 72 20 64 69 72 74  e is either dirt
11958 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61 74 63 68  y or still match
11959 65 73 20 74 68 65 20 63 61 6c 63 75 6c 61 74 65  es the calculate
1195a 64 20 70 61 67 65 2d 68 61 73 68 2e 0a 2a 2f 0a  d page-hash..*/.
1195b 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50 41  #define CHECK_PA
1195c 47 45 28 78 29 20 63 68 65 63 6b 50 61 67 65 28  GE(x) checkPage(
1195d 78 29 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  x).static void c
1195e 68 65 63 6b 50 61 67 65 28 50 67 48 64 72 20 2a  heckPage(PgHdr *
1195f 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  pPg){.  Pager *p
11960 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
11961 67 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ger;.  assert( !
11962 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 7c 7c  pPg->pageHash ||
11963 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11964 0a 20 20 20 20 20 20 7c 7c 20 28 70 50 67 2d 3e  .      || (pPg->
11965 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
11966 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61 67 65 48  Y) || pPg->pageH
11967 61 73 68 3d 3d 70 61 67 65 72 5f 70 61 67 65 68  ash==pager_pageh
11968 61 73 68 28 70 50 67 29 20 29 3b 0a 7d 0a 0a 23  ash(pPg) );.}..#
11969 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 70 61 67  else.#define pag
1196a 65 72 5f 64 61 74 61 68 61 73 68 28 58 2c 59 29  er_datahash(X,Y)
1196b 20 20 30 0a 23 64 65 66 69 6e 65 20 70 61 67 65    0.#define page
1196c 72 5f 70 61 67 65 68 61 73 68 28 58 29 20 20 30  r_pagehash(X)  0
1196d 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f 50  .#define CHECK_P
1196e 41 47 45 28 78 29 0a 23 65 6e 64 69 66 20 20 2f  AGE(x).#endif  /
1196f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  * SQLITE_CHECK_P
11970 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57  AGES */../*.** W
11971 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c  hen this is call
11972 65 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ed the journal f
11973 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70 50  ile for pager pP
11974 61 67 65 72 20 6d 75 73 74 20 62 65 20 6f 70 65  ager must be ope
11975 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  n..** This funct
11976 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ion attempts to 
11977 72 65 61 64 20 61 20 6d 61 73 74 65 72 20 6a 6f  read a master jo
11978 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
11979 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 65 6e 64  from the .** end
1197a 20 6f 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64   of the file and
1197b 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 2c  , if successful,
1197c 20 63 6f 70 69 65 73 20 69 74 20 69 6e 74 6f 20   copies it into 
1197d 6d 65 6d 6f 72 79 20 73 75 70 70 6c 69 65 64 20  memory supplied 
1197e 0a 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 65  .** by the calle
1197f 72 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20  r. See comments 
11980 61 62 6f 76 65 20 77 72 69 74 65 4d 61 73 74 65  above writeMaste
11981 72 4a 6f 75 72 6e 61 6c 28 29 20 66 6f 72 20 74  rJournal() for t
11982 68 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 75 73 65  he format.** use
11983 64 20 74 6f 20 73 74 6f 72 65 20 61 20 6d 61 73  d to store a mas
11984 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
11985 20 6e 61 6d 65 20 61 74 20 74 68 65 20 65 6e 64   name at the end
11986 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   of a journal fi
11987 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7a 4d 61 73 74 65  le..**.** zMaste
11988 72 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20  r must point to 
11989 61 20 62 75 66 66 65 72 20 6f 66 20 61 74 20 6c  a buffer of at l
1198a 65 61 73 74 20 6e 4d 61 73 74 65 72 20 62 79 74  east nMaster byt
1198b 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a  es allocated by.
1198c 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 54  ** the caller. T
1198d 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 73 71  his should be sq
1198e 6c 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68  lite3_vfs.mxPath
1198f 6e 61 6d 65 2b 31 20 28 74 6f 20 65 6e 73 75 72  name+1 (to ensur
11990 65 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 65 6e  e there is.** en
11991 6f 75 67 68 20 73 70 61 63 65 20 74 6f 20 77 72  ough space to wr
11992 69 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ite the master j
11993 6f 75 72 6e 61 6c 20 6e 61 6d 65 29 2e 20 49 66  ournal name). If
11994 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
11995 6e 61 6c 0a 2a 2a 20 6e 61 6d 65 20 69 6e 20 74  nal.** name in t
11996 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 6f  he journal is lo
11997 6e 67 65 72 20 74 68 61 6e 20 6e 4d 61 73 74 65  nger than nMaste
11998 72 20 62 79 74 65 73 20 28 69 6e 63 6c 75 64 69  r bytes (includi
11999 6e 67 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d  ng a.** nul-term
1199a 69 6e 61 74 6f 72 29 2c 20 74 68 65 6e 20 74 68  inator), then th
1199b 69 73 20 69 73 20 68 61 6e 64 6c 65 64 20 61 73  is is handled as
1199c 20 69 66 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f   if no master jo
1199d 75 72 6e 61 6c 20 6e 61 6d 65 0a 2a 2a 20 77 65  urnal name.** we
1199e 72 65 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68  re present in th
1199f 65 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a  e journal..**.**
119a0 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   If a master jou
119a1 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69  rnal file name i
119a2 73 20 70 72 65 73 65 6e 74 20 61 74 20 74 68 65  s present at the
119a3 20 65 6e 64 20 6f 66 20 74 68 65 20 6a 6f 75 72   end of the jour
119a4 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2c 20 74 68 65  nal.** file, the
119a5 6e 20 69 74 20 69 73 20 63 6f 70 69 65 64 20 69  n it is copied i
119a6 6e 74 6f 20 74 68 65 20 62 75 66 66 65 72 20 70  nto the buffer p
119a7 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61  ointed to by zMa
119a8 73 74 65 72 2e 20 41 0a 2a 2a 20 6e 75 6c 2d 74  ster. A.** nul-t
119a9 65 72 6d 69 6e 61 74 6f 72 20 62 79 74 65 20 69  erminator byte i
119aa 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  s appended to th
119ab 65 20 62 75 66 66 65 72 20 66 6f 6c 6c 6f 77 69  e buffer followi
119ac 6e 67 20 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a  ng the master.**
119ad 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61   journal file na
119ae 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20  me..**.** If it 
119af 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68  is determined th
119b0 61 74 20 6e 6f 20 6d 61 73 74 65 72 20 6a 6f 75  at no master jou
119b1 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69  rnal file name i
119b2 73 20 70 72 65 73 65 6e 74 20 0a 2a 2a 20 7a 4d  s present .** zM
119b3 61 73 74 65 72 5b 30 5d 20 69 73 20 73 65 74 20  aster[0] is set 
119b4 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f  to 0 and SQLITE_
119b5 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  OK returned..**.
119b6 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
119b7 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 61 64  ccurs while read
119b8 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75  ing from the jou
119b9 72 6e 61 6c 20 66 69 6c 65 2c 20 61 6e 20 53 51  rnal file, an SQ
119ba 4c 69 74 65 0a 2a 2a 20 65 72 72 6f 72 20 63 6f  Lite.** error co
119bb 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
119bc 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
119bd 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28  adMasterJournal(
119be 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
119bf 72 6e 6c 2c 20 63 68 61 72 20 2a 7a 4d 61 73 74  rnl, char *zMast
119c0 65 72 2c 20 75 33 32 20 6e 4d 61 73 74 65 72 29  er, u32 nMaster)
119c1 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
119c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
119c3 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
119c4 0a 20 20 75 33 32 20 6c 65 6e 3b 20 20 20 20 20  .  u32 len;     
119c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
119c6 20 4c 65 6e 67 74 68 20 69 6e 20 62 79 74 65 73   Length in bytes
119c7 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   of master journ
119c8 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 36 34  al name */.  i64
119c9 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20   szJ;           
119ca 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
119cb 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f   size in bytes o
119cc 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 70  f journal file p
119cd 4a 72 6e 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b  Jrnl */.  u32 ck
119ce 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  sum;            
119cf 20 20 20 20 20 2f 2a 20 4d 4a 20 63 68 65 63 6b       /* MJ check
119d0 73 75 6d 20 76 61 6c 75 65 20 72 65 61 64 20 66  sum value read f
119d1 72 6f 6d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20  rom journal */. 
119d2 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20 20   u32 u;         
119d3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
119d4 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75  nsigned loop cou
119d5 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  nter */.  unsign
119d6 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38  ed char aMagic[8
119d7 5d 3b 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72  ];   /* A buffer
119d8 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 67   to hold the mag
119d9 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 7a  ic header */.  z
119da 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c 30 27  Master[0] = '\0'
119db 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  ;..  if( SQLITE_
119dc 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK!=(rc = sqlite
119dd 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a 72 6e  3OsFileSize(pJrn
119de 6c 2c 20 26 73 7a 4a 29 29 0a 20 20 20 7c 7c 20  l, &szJ)).   || 
119df 73 7a 4a 3c 31 36 0a 20 20 20 7c 7c 20 53 51 4c  szJ<16.   || SQL
119e0 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65  ITE_OK!=(rc = re
119e1 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c 20  ad32bits(pJrnl, 
119e2 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 29 0a 20  szJ-16, &len)). 
119e3 20 20 7c 7c 20 6c 65 6e 3e 3d 6e 4d 61 73 74 65    || len>=nMaste
119e4 72 20 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  r .   || SQLITE_
119e5 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
119e6 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d  bits(pJrnl, szJ-
119e7 31 32 2c 20 26 63 6b 73 75 6d 29 29 0a 20 20 20  12, &cksum)).   
119e8 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  || SQLITE_OK!=(r
119e9 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
119ea 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69 63 2c  d(pJrnl, aMagic,
119eb 20 38 2c 20 73 7a 4a 2d 38 29 29 0a 20 20 20 7c   8, szJ-8)).   |
119ec 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c  | memcmp(aMagic,
119ed 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20   aJournalMagic, 
119ee 38 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f  8).   || SQLITE_
119ef 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK!=(rc = sqlite
119f0 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20 7a  3OsRead(pJrnl, z
119f1 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a 4a  Master, len, szJ
119f2 2d 31 36 2d 6c 65 6e 29 29 0a 20 20 29 7b 0a 20  -16-len)).  ){. 
119f3 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
119f4 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 74  }..  /* See if t
119f5 68 65 20 63 68 65 63 6b 73 75 6d 20 6d 61 74 63  he checksum matc
119f6 68 65 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a  hes the master j
119f7 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20  ournal name */. 
119f8 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6c 65 6e 3b   for(u=0; u<len;
119f9 20 75 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d   u++){.    cksum
119fa 20 2d 3d 20 7a 4d 61 73 74 65 72 5b 75 5d 3b 0a   -= zMaster[u];.
119fb 20 20 7d 0a 20 20 69 66 28 20 63 6b 73 75 6d 20    }.  if( cksum 
119fc 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ){.    /* If the
119fd 20 63 68 65 63 6b 73 75 6d 20 64 6f 65 73 6e 27   checksum doesn'
119fe 74 20 61 64 64 20 75 70 2c 20 74 68 65 6e 20 6f  t add up, then o
119ff 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
11a00 65 20 64 69 73 6b 20 73 65 63 74 6f 72 73 0a 20  e disk sectors. 
11a01 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67     ** containing
11a02 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
11a03 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  nal filename is 
11a04 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73 20  corrupted. This 
11a05 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 64 65 66  means.    ** def
11a06 69 6e 69 74 65 6c 79 20 72 6f 6c 6c 20 62 61 63  initely roll bac
11a07 6b 2c 20 73 6f 20 6a 75 73 74 20 72 65 74 75 72  k, so just retur
11a08 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20  n SQLITE_OK and 
11a09 72 65 70 6f 72 74 20 61 20 28 6e 75 6c 29 0a 20  report a (nul). 
11a0a 20 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75     ** master-jou
11a0b 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 2e 0a 20  rnal filename.. 
11a0c 20 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20     */.    len = 
11a0d 30 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72  0;.  }.  zMaster
11a0e 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  [len] = '\0';.  
11a0f 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54   .  return SQLIT
11a10 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  E_OK;.}../*.** R
11a11 65 74 75 72 6e 20 74 68 65 20 6f 66 66 73 65 74  eturn the offset
11a12 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72 20 62   of the sector b
11a13 6f 75 6e 64 61 72 79 20 61 74 20 6f 72 20 69 6d  oundary at or im
11a14 6d 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 66 6f  mediately .** fo
11a15 6c 6c 6f 77 69 6e 67 20 74 68 65 20 76 61 6c 75  llowing the valu
11a16 65 20 69 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75  e in pPager->jou
11a17 72 6e 61 6c 4f 66 66 2c 20 61 73 73 75 6d 69 6e  rnalOff, assumin
11a18 67 20 61 20 73 65 63 74 6f 72 20 0a 2a 2a 20 73  g a sector .** s
11a19 69 7a 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 73  ize of pPager->s
11a1a 65 63 74 6f 72 53 69 7a 65 20 62 79 74 65 73 2e  ectorSize bytes.
11a1b 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66 6f 72 20 61  .**.** i.e for a
11a1c 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20   sector size of 
11a1d 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 61 67  512:.**.**   Pag
11a1e 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 20 20  er.journalOff   
11a1f 20 20 20 20 20 20 20 52 65 74 75 72 6e 20 76 61         Return va
11a20 6c 75 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  lue.**   -------
11a21 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a22 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
11a23 0a 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20  .**   0         
11a24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a25 30 0a 2a 2a 20 20 20 35 31 32 20 20 20 20 20 20  0.**   512      
11a26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a27 20 35 31 32 0a 2a 2a 20 20 20 31 30 30 20 20 20   512.**   100   
11a28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a29 20 20 20 20 35 31 32 0a 2a 2a 20 20 20 32 30 30      512.**   200
11a2a 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
11a2b 20 20 20 20 20 20 20 32 30 34 38 0a 2a 2a 20 0a         2048.** .
11a2c 2a 2f 0a 73 74 61 74 69 63 20 69 36 34 20 6a 6f  */.static i64 jo
11a2d 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 50  urnalHdrOffset(P
11a2e 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
11a2f 20 69 36 34 20 6f 66 66 73 65 74 20 3d 20 30 3b   i64 offset = 0;
11a30 0a 20 20 69 36 34 20 63 20 3d 20 70 50 61 67 65  .  i64 c = pPage
11a31 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20  r->journalOff;. 
11a32 20 69 66 28 20 63 20 29 7b 0a 20 20 20 20 6f 66   if( c ){.    of
11a33 66 73 65 74 20 3d 20 28 28 63 2d 31 29 2f 4a 4f  fset = ((c-1)/JO
11a34 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
11a35 67 65 72 29 20 2b 20 31 29 20 2a 20 4a 4f 55 52  ger) + 1) * JOUR
11a36 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
11a37 72 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  r);.  }.  assert
11a38 28 20 6f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c  ( offset%JOURNAL
11a39 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3d  _HDR_SZ(pPager)=
11a3a 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
11a3b 6f 66 66 73 65 74 3e 3d 63 20 29 3b 0a 20 20 61  offset>=c );.  a
11a3c 73 73 65 72 74 28 20 28 6f 66 66 73 65 74 2d 63  ssert( (offset-c
11a3d 29 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  )<JOURNAL_HDR_SZ
11a3e 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 72 65  (pPager) );.  re
11a3f 74 75 72 6e 20 6f 66 66 73 65 74 3b 0a 7d 0a 0a  turn offset;.}..
11a40 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61  /*.** The journa
11a41 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f  l file must be o
11a42 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75  pen when this fu
11a43 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
11a44 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
11a45 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
11a46 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   if the journal 
11a47 66 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65  file has not bee
11a48 6e 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20  n written to.** 
11a49 77 69 74 68 69 6e 20 74 68 65 20 63 75 72 72 65  within the curre
11a4a 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28  nt transaction (
11a4b 69 2e 65 2e 20 69 66 20 50 61 67 65 72 2e 6a 6f  i.e. if Pager.jo
11a4c 75 72 6e 61 6c 4f 66 66 3d 3d 30 29 2e 0a 2a 2a  urnalOff==0)..**
11a4d 0a 2a 2a 20 49 66 20 64 6f 54 72 75 6e 63 61 74  .** If doTruncat
11a4e 65 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72  e is non-zero or
11a4f 20 74 68 65 20 50 61 67 65 72 2e 6a 6f 75 72 6e   the Pager.journ
11a50 61 6c 53 69 7a 65 4c 69 6d 69 74 20 76 61 72 69  alSizeLimit vari
11a51 61 62 6c 65 20 69 73 0a 2a 2a 20 73 65 74 20 74  able is.** set t
11a52 6f 20 30 2c 20 74 68 65 6e 20 74 72 75 6e 63 61  o 0, then trunca
11a53 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  te the journal f
11a54 69 6c 65 20 74 6f 20 7a 65 72 6f 20 62 79 74 65  ile to zero byte
11a55 73 20 69 6e 20 73 69 7a 65 2e 20 4f 74 68 65 72  s in size. Other
11a56 77 69 73 65 2c 0a 2a 2a 20 7a 65 72 6f 20 74 68  wise,.** zero th
11a57 65 20 32 38 2d 62 79 74 65 20 68 65 61 64 65 72  e 28-byte header
11a58 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
11a59 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11a5a 65 2e 20 49 6e 20 65 69 74 68 65 72 20 63 61 73  e. In either cas
11a5b 65 2c 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 61  e, .** if the pa
11a5c 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20 6e 6f  ger is not in no
11a5d 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 73 79 6e 63  -sync mode, sync
11a5e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11a5f 65 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 0a 2a  e immediately .*
11a60 2a 20 61 66 74 65 72 20 77 72 69 74 69 6e 67 20  * after writing 
11a61 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20 69 74  or truncating it
11a62 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 61 67 65 72  ..**.** If Pager
11a63 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69  .journalSizeLimi
11a64 74 20 69 73 20 73 65 74 20 74 6f 20 61 20 70 6f  t is set to a po
11a65 73 69 74 69 76 65 2c 20 6e 6f 6e 2d 7a 65 72 6f  sitive, non-zero
11a66 20 76 61 6c 75 65 2c 20 61 6e 64 0a 2a 2a 20 66   value, and.** f
11a67 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 72 75  ollowing the tru
11a68 6e 63 61 74 69 6f 6e 20 6f 72 20 7a 65 72 6f 69  ncation or zeroi
11a69 6e 67 20 64 65 73 63 72 69 62 65 64 20 61 62 6f  ng described abo
11a6a 76 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  ve the size of t
11a6b 68 65 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  he .** journal f
11a6c 69 6c 65 20 69 6e 20 62 79 74 65 73 20 69 73 20  ile in bytes is 
11a6d 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73  larger than this
11a6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74 72 75   value, then tru
11a6f 6e 63 61 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75  ncate the.** jou
11a70 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 50 61 67  rnal file to Pag
11a71 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  er.journalSizeLi
11a72 6d 69 74 20 62 79 74 65 73 2e 20 54 68 65 20 6a  mit bytes. The j
11a73 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73  ournal file does
11a74 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20  .** not need to 
11a75 62 65 20 73 79 6e 63 65 64 20 66 6f 6c 6c 6f 77  be synced follow
11a76 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74 69  ing this operati
11a77 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  on..**.** If an 
11a78 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  IO error occurs,
11a79 20 61 62 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73   abandon process
11a7a 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ing and return t
11a7b 68 65 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  he IO error code
11a7c 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  ..** Otherwise, 
11a7d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
11a7e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11a7f 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72 28 50  zeroJournalHdr(P
11a80 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
11a81 74 20 64 6f 54 72 75 6e 63 61 74 65 29 7b 0a 20  t doTruncate){. 
11a82 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
11a83 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
11a84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a85 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
11a86 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69  e */.  assert( i
11a87 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
11a88 64 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  d) );.  if( pPag
11a89 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29  er->journalOff )
11a8a 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20  {.    const i64 
11a8b 69 4c 69 6d 69 74 20 3d 20 70 50 61 67 65 72 2d  iLimit = pPager-
11a8c 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69  >journalSizeLimi
11a8d 74 3b 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 63  t;    /* Local c
11a8e 61 63 68 65 20 6f 66 20 6a 73 6c 20 2a 2f 0a 0a  ache of jsl */..
11a8f 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 5a      IOTRACE(("JZ
11a90 45 52 4f 48 44 52 20 25 70 5c 6e 22 2c 20 70 50  EROHDR %p\n", pP
11a91 61 67 65 72 29 29 0a 20 20 20 20 69 66 28 20 64  ager)).    if( d
11a92 6f 54 72 75 6e 63 61 74 65 20 7c 7c 20 69 4c 69  oTruncate || iLi
11a93 6d 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  mit==0 ){.      
11a94 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72  rc = sqlite3OsTr
11a95 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a  uncate(pPager->j
11a96 66 64 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73  fd, 0);.    }els
11a97 65 7b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  e{.      static 
11a98 63 6f 6e 73 74 20 63 68 61 72 20 7a 65 72 6f 48  const char zeroH
11a99 64 72 5b 32 38 5d 20 3d 20 7b 30 7d 3b 0a 20 20  dr[28] = {0};.  
11a9a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
11a9b 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
11a9c 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73 69  jfd, zeroHdr, si
11a9d 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20 30  zeof(zeroHdr), 0
11a9e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
11a9f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
11aa0 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  & !pPager->noSyn
11aa1 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  c ){.      rc = 
11aa2 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 50  sqlite3OsSync(pP
11aa3 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49 54  ager->jfd, SQLIT
11aa4 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 7c  E_SYNC_DATAONLY|
11aa5 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61  pPager->sync_fla
11aa6 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  gs);.    }..    
11aa7 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
11aa8 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
11aa9 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62 75   is committed bu
11aaa 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b  t the write lock
11aab 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c   .    ** is stil
11aac 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66 69  l held on the fi
11aad 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  le. If there is 
11aae 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f 6e  a size limit con
11aaf 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 20 20  figured for .   
11ab0 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74 65   ** the persiste
11ab1 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74  nt journal and t
11ab2 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11ab3 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75 6d  currently consum
11ab4 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20 73  es more.    ** s
11ab5 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 20 6c  pace than that l
11ab6 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72 2c  imit allows for,
11ab7 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e 6f 77   truncate it now
11ab8 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65  . There is no ne
11ab9 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79 6e  ed.    ** to syn
11aba 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c 6f  c the file follo
11abb 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61 74  wing this operat
11abc 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
11abd 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
11abe 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 29 7b  K && iLimit>0 ){
11abf 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b 0a 20  .      i64 sz;. 
11ac0 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
11ac1 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67  3OsFileSize(pPag
11ac2 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a 20  er->jfd, &sz);. 
11ac3 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
11ac4 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c 69  ITE_OK && sz>iLi
11ac5 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  mit ){.        r
11ac6 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75  c = sqlite3OsTru
11ac7 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  ncate(pPager->jf
11ac8 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20  d, iLimit);.    
11ac9 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
11aca 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
11acb 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  .** The journal 
11acc 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65  file must be ope
11acd 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  n when this rout
11ace 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 41  ine is called. A
11acf 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61 64   journal.** head
11ad0 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  er (JOURNAL_HDR_
11ad1 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 72 69  SZ bytes) is wri
11ad2 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f  tten into the jo
11ad3 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 74 68  urnal file at th
11ad4 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63  e.** current loc
11ad5 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ation..**.** The
11ad6 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 65 20   format for the 
11ad7 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69  journal header i
11ad8 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
11ad9 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 67 69   - 8 bytes: Magi
11ada 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 6a 6f  c identifying jo
11adb 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a 2a  urnal format..**
11adc 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 6d 62   - 4 bytes: Numb
11add 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69 6e  er of records in
11ade 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31 20   journal, or -1 
11adf 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73 20  no-sync mode is 
11ae0 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73  on..** - 4 bytes
11ae1 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20  : Random number 
11ae2 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 68 61  used for page ha
11ae3 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73  sh..** - 4 bytes
11ae4 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 62 61  : Initial databa
11ae5 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a  se page count..*
11ae6 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 65 63  * - 4 bytes: Sec
11ae7 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 62 79  tor size used by
11ae8 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68 61   the process tha
11ae9 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a 6f 75  t wrote this jou
11aea 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79 74  rnal..** - 4 byt
11aeb 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 61 67  es: Database pag
11aec 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20 46  e size..** .** F
11aed 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55 52  ollowed by (JOUR
11aee 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38 29  NAL_HDR_SZ - 28)
11aef 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64   bytes of unused
11af0 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74 69   space..*/.stati
11af1 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72 6e  c int writeJourn
11af2 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 61  alHdr(Pager *pPa
11af3 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ger){.  int rc =
11af4 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
11af5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
11af6 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
11af7 63 68 61 72 20 2a 7a 48 65 61 64 65 72 20 3d 20  char *zHeader = 
11af8 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63  pPager->pTmpSpac
11af9 65 3b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79  e;  /* Temporary
11afa 20 73 70 61 63 65 20 75 73 65 64 20 74 6f 20 62   space used to b
11afb 75 69 6c 64 20 68 65 61 64 65 72 20 2a 2f 0a 20  uild header */. 
11afc 20 75 33 32 20 6e 48 65 61 64 65 72 20 3d 20 70   u32 nHeader = p
11afd 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b  Pager->pageSize;
11afe 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
11aff 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74  buffer pointed t
11b00 6f 20 62 79 20 7a 48 65 61 64 65 72 20 2a 2f 0a  o by zHeader */.
11b01 20 20 75 33 32 20 6e 57 72 69 74 65 3b 20 20 20    u32 nWrite;   
11b02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11b03 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f        /* Bytes o
11b04 66 20 68 65 61 64 65 72 20 73 65 63 74 6f 72 20  f header sector 
11b05 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69 6e 74  written */.  int
11b06 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20   ii;            
11b07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11b08 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
11b09 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69   */..  assert( i
11b0a 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
11b0b 64 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f  d) );      /* Jo
11b0c 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20  urnal file must 
11b0d 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 69  be open. */..  i
11b0e 66 28 20 6e 48 65 61 64 65 72 3e 4a 4f 55 52 4e  f( nHeader>JOURN
11b0f 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
11b10 29 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72  ) ){.    nHeader
11b11 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53   = JOURNAL_HDR_S
11b12 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a  Z(pPager);.  }..
11b13 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
11b14 65 20 61 63 74 69 76 65 20 73 61 76 65 70 6f 69  e active savepoi
11b15 6e 74 73 20 61 6e 64 20 61 6e 79 20 6f 66 20 74  nts and any of t
11b16 68 65 6d 20 77 65 72 65 20 63 72 65 61 74 65 64  hem were created
11b17 20 0a 20 20 2a 2a 20 73 69 6e 63 65 20 74 68 65   .  ** since the
11b18 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 6a 6f 75   most recent jou
11b19 72 6e 61 6c 20 68 65 61 64 65 72 20 77 61 73 20  rnal header was 
11b1a 77 72 69 74 74 65 6e 2c 20 75 70 64 61 74 65 20  written, update 
11b1b 74 68 65 20 0a 20 20 2a 2a 20 50 61 67 65 72 53  the .  ** PagerS
11b1c 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66  avepoint.iHdrOff
11b1d 73 65 74 20 66 69 65 6c 64 73 20 6e 6f 77 2e 0a  set fields now..
11b1e 20 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b    */.  for(ii=0;
11b1f 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76   ii<pPager->nSav
11b20 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  epoint; ii++){. 
11b21 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 61     if( pPager->a
11b22 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48  Savepoint[ii].iH
11b23 64 72 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a 20  drOffset==0 ){. 
11b24 20 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61       pPager->aSa
11b25 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72  vepoint[ii].iHdr
11b26 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d  Offset = pPager-
11b27 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20  >journalOff;.   
11b28 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72   }.  }..  pPager
11b29 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 70  ->journalHdr = p
11b2a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11b2b 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66  f = journalHdrOf
11b2c 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 0a 20  fset(pPager);.. 
11b2d 20 2f 2a 20 0a 20 20 2a 2a 20 57 72 69 74 65 20   /* .  ** Write 
11b2e 74 68 65 20 6e 52 65 63 20 46 69 65 6c 64 20 2d  the nRec Field -
11b2f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
11b30 61 67 65 20 72 65 63 6f 72 64 73 20 74 68 61 74  age records that
11b31 20 66 6f 6c 6c 6f 77 20 74 68 69 73 0a 20 20 2a   follow this.  *
11b32 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  * journal header
11b33 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 7a 65 72 6f  . Normally, zero
11b34 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74   is written to t
11b35 68 69 73 20 76 61 6c 75 65 20 61 74 20 74 68 69  his value at thi
11b36 73 20 74 69 6d 65 2e 0a 20 20 2a 2a 20 41 66 74  s time..  ** Aft
11b37 65 72 20 74 68 65 20 72 65 63 6f 72 64 73 20 61  er the records a
11b38 72 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  re added to the 
11b39 6a 6f 75 72 6e 61 6c 20 28 61 6e 64 20 74 68 65  journal (and the
11b3a 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63 65 64 2c   journal synced,
11b3b 20 0a 20 20 2a 2a 20 69 66 20 69 6e 20 66 75 6c   .  ** if in ful
11b3c 6c 2d 73 79 6e 63 20 6d 6f 64 65 29 2c 20 74 68  l-sync mode), th
11b3d 65 20 7a 65 72 6f 20 69 73 20 6f 76 65 72 77 72  e zero is overwr
11b3e 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 74  itten with the t
11b3f 72 75 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20  rue number.  ** 
11b40 6f 66 20 72 65 63 6f 72 64 73 20 28 73 65 65 20  of records (see 
11b41 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 29 29 2e 0a  syncJournal())..
11b42 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 66 61 73 74    **.  ** A fast
11b43 65 72 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69  er alternative i
11b44 73 20 74 6f 20 77 72 69 74 65 20 30 78 46 46 46  s to write 0xFFF
11b45 46 46 46 46 46 20 74 6f 20 74 68 65 20 6e 52 65  FFFFF to the nRe
11b46 63 20 66 69 65 6c 64 2e 20 57 68 65 6e 0a 20 20  c field. When.  
11b47 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20 6a  ** reading the j
11b48 6f 75 72 6e 61 6c 20 74 68 69 73 20 76 61 6c 75  ournal this valu
11b49 65 20 74 65 6c 6c 73 20 53 51 4c 69 74 65 20 74  e tells SQLite t
11b4a 6f 20 61 73 73 75 6d 65 20 74 68 61 74 20 74 68  o assume that th
11b4b 65 0a 20 20 2a 2a 20 72 65 73 74 20 6f 66 20 74  e.  ** rest of t
11b4c 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11b4d 63 6f 6e 74 61 69 6e 73 20 76 61 6c 69 64 20 70  contains valid p
11b4e 61 67 65 20 72 65 63 6f 72 64 73 2e 20 54 68 69  age records. Thi
11b4f 73 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a  s assumption.  *
11b50 2a 20 69 73 20 64 61 6e 67 65 72 6f 75 73 2c 20  * is dangerous, 
11b51 61 73 20 69 66 20 61 20 66 61 69 6c 75 72 65 20  as if a failure 
11b52 6f 63 63 75 72 72 65 64 20 77 68 69 6c 73 74 20  occurred whilst 
11b53 77 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a  writing to the j
11b54 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c 65  ournal.  ** file
11b55 20 69 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20   it may contain 
11b56 73 6f 6d 65 20 67 61 72 62 61 67 65 20 64 61 74  some garbage dat
11b57 61 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 6f  a. There are two
11b58 20 73 63 65 6e 61 72 69 6f 73 0a 20 20 2a 2a 20   scenarios.  ** 
11b59 77 68 65 72 65 20 74 68 69 73 20 72 69 73 6b 20  where this risk 
11b5a 63 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 3a 0a  can be ignored:.
11b5b 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68    **.  **   * Wh
11b5c 65 6e 20 74 68 65 20 70 61 67 65 72 20 69 73 20  en the pager is 
11b5d 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e  in no-sync mode.
11b5e 20 43 6f 72 72 75 70 74 69 6f 6e 20 63 61 6e 20   Corruption can 
11b5f 66 6f 6c 6c 6f 77 20 61 0a 20 20 2a 2a 20 20 20  follow a.  **   
11b60 20 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20    power failure 
11b61 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 6e 79  in this case any
11b62 77 61 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  way..  **.  **  
11b63 20 2a 20 57 68 65 6e 20 74 68 65 20 53 51 4c 49   * When the SQLI
11b64 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50  TE_IOCAP_SAFE_AP
11b65 50 45 4e 44 20 66 6c 61 67 20 69 73 20 73 65 74  PEND flag is set
11b66 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 65 65  . This guarantee
11b67 73 0a 20 20 2a 2a 20 20 20 20 20 74 68 61 74 20  s.  **     that 
11b68 67 61 72 62 61 67 65 20 64 61 74 61 20 69 73 20  garbage data is 
11b69 6e 65 76 65 72 20 61 70 70 65 6e 64 65 64 20 74  never appended t
11b6a 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
11b6b 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  le..  */.  asser
11b6c 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
11b6d 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d  ->fd) || pPager-
11b6e 3e 6e 6f 53 79 6e 63 20 29 3b 0a 20 20 69 66 28  >noSync );.  if(
11b6f 20 28 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63   (pPager->noSync
11b70 29 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 6a 6f  ) || (pPager->jo
11b71 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52  urnalMode==PAGER
11b72 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
11b73 4f 52 59 29 0a 20 20 20 7c 7c 20 28 73 71 6c 69  ORY).   || (sqli
11b74 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61  te3OsDeviceChara
11b75 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65  cteristics(pPage
11b76 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f  r->fd)&SQLITE_IO
11b77 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29  CAP_SAFE_APPEND)
11b78 20 0a 20 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70   .  ){.    memcp
11b79 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75 72  y(zHeader, aJour
11b7a 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66  nalMagic, sizeof
11b7b 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 29  (aJournalMagic))
11b7c 3b 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28  ;.    put32bits(
11b7d 26 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28  &zHeader[sizeof(
11b7e 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c  aJournalMagic)],
11b7f 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20   0xffffffff);.  
11b80 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 73 65  }else{.    memse
11b81 74 28 7a 48 65 61 64 65 72 2c 20 30 2c 20 73 69  t(zHeader, 0, si
11b82 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
11b83 69 63 29 2b 34 29 3b 0a 20 20 7d 0a 0a 20 20 2f  ic)+4);.  }..  /
11b84 2a 20 54 68 65 20 72 61 6e 64 6f 6d 20 63 68 65  * The random che
11b85 63 6b 2d 68 61 73 68 20 69 6e 69 74 69 61 6c 69  ck-hash initiali
11b86 73 65 72 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65  ser */ .  sqlite
11b87 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a  3_randomness(siz
11b88 65 6f 66 28 70 50 61 67 65 72 2d 3e 63 6b 73 75  eof(pPager->cksu
11b89 6d 49 6e 69 74 29 2c 20 26 70 50 61 67 65 72 2d  mInit), &pPager-
11b8a 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 70  >cksumInit);.  p
11b8b 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65  ut32bits(&zHeade
11b8c 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  r[sizeof(aJourna
11b8d 6c 4d 61 67 69 63 29 2b 34 5d 2c 20 70 50 61 67  lMagic)+4], pPag
11b8e 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 29 3b 0a  er->cksumInit);.
11b8f 20 20 2f 2a 20 54 68 65 20 69 6e 69 74 69 61 6c    /* The initial
11b90 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 2a   database size *
11b91 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a  /.  put32bits(&z
11b92 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a  Header[sizeof(aJ
11b93 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 38 5d 2c  ournalMagic)+8],
11b94 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53   pPager->dbOrigS
11b95 69 7a 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20 61  ize);.  /* The a
11b96 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20 73 69  ssumed sector si
11b97 7a 65 20 66 6f 72 20 74 68 69 73 20 70 72 6f 63  ze for this proc
11b98 65 73 73 20 2a 2f 0a 20 20 70 75 74 33 32 62 69  ess */.  put32bi
11b99 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a 65  ts(&zHeader[size
11b9a 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
11b9b 29 2b 31 32 5d 2c 20 70 50 61 67 65 72 2d 3e 73  )+12], pPager->s
11b9c 65 63 74 6f 72 53 69 7a 65 29 3b 0a 0a 20 20 2f  ectorSize);..  /
11b9d 2a 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20  * The page size 
11b9e 2a 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26  */.  put32bits(&
11b9f 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
11ba0 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 36  JournalMagic)+16
11ba1 5d 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  ], pPager->pageS
11ba2 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74  ize);..  /* Init
11ba3 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 74 61 69  ializing the tai
11ba4 6c 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20  l of the buffer 
11ba5 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79  is not necessary
11ba6 2e 20 20 45 76 65 72 79 74 68 69 6e 67 0a 20 20  .  Everything.  
11ba7 2a 2a 20 77 6f 72 6b 73 20 66 69 6e 64 20 69 66  ** works find if
11ba8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
11ba9 65 6d 73 65 74 28 29 20 69 73 20 6f 6d 69 74 74  emset() is omitt
11baa 65 64 2e 20 20 42 75 74 20 69 6e 69 74 69 61 6c  ed.  But initial
11bab 69 7a 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d  izing.  ** the m
11bac 65 6d 6f 72 79 20 70 72 65 76 65 6e 74 73 20 76  emory prevents v
11bad 61 6c 67 72 69 6e 64 20 66 72 6f 6d 20 63 6f 6d  algrind from com
11bae 70 6c 61 69 6e 69 6e 67 2c 20 73 6f 20 77 65 20  plaining, so we 
11baf 61 72 65 20 77 69 6c 6c 69 6e 67 20 74 6f 0a 20  are willing to. 
11bb0 20 2a 2a 20 74 61 6b 65 20 74 68 65 20 70 65 72   ** take the per
11bb1 66 6f 72 6d 61 6e 63 65 20 68 69 74 2e 0a 20 20  formance hit..  
11bb2 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 7a 48 65  */.  memset(&zHe
11bb3 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75  ader[sizeof(aJou
11bb4 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 5d 2c 20  rnalMagic)+20], 
11bb5 30 2c 0a 20 20 20 20 20 20 20 20 20 6e 48 65 61  0,.         nHea
11bb6 64 65 72 2d 28 73 69 7a 65 6f 66 28 61 4a 6f 75  der-(sizeof(aJou
11bb7 72 6e 61 6c 4d 61 67 69 63 29 2b 32 30 29 29 3b  rnalMagic)+20));
11bb8 0a 0a 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72 79  ..  /* In theory
11bb9 2c 20 69 74 20 69 73 20 6f 6e 6c 79 20 6e 65 63  , it is only nec
11bba 65 73 73 61 72 79 20 74 6f 20 77 72 69 74 65 20  essary to write 
11bbb 74 68 65 20 32 38 20 62 79 74 65 73 20 74 68 61  the 28 bytes tha
11bbc 74 20 74 68 65 20 0a 20 20 2a 2a 20 6a 6f 75 72  t the .  ** jour
11bbd 6e 61 6c 20 68 65 61 64 65 72 20 63 6f 6e 73 75  nal header consu
11bbe 6d 65 73 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  mes to the journ
11bbf 61 6c 20 66 69 6c 65 20 68 65 72 65 2e 20 54 68  al file here. Th
11bc0 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65  en increment the
11bc1 20 0a 20 20 2a 2a 20 50 61 67 65 72 2e 6a 6f 75   .  ** Pager.jou
11bc2 72 6e 61 6c 4f 66 66 20 76 61 72 69 61 62 6c 65  rnalOff variable
11bc3 20 62 79 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f   by JOURNAL_HDR_
11bc4 53 5a 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e  SZ so that the n
11bc5 65 78 74 20 0a 20 20 2a 2a 20 72 65 63 6f 72 64  ext .  ** record
11bc6 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74   is written to t
11bc7 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 63  he following sec
11bc8 74 6f 72 20 28 6c 65 61 76 69 6e 67 20 61 20 67  tor (leaving a g
11bc9 61 70 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 20  ap in the file. 
11bca 20 2a 2a 20 74 68 61 74 20 77 69 6c 6c 20 62 65   ** that will be
11bcb 20 69 6d 70 6c 69 63 69 74 6c 79 20 66 69 6c 6c   implicitly fill
11bcc 65 64 20 69 6e 20 62 79 20 74 68 65 20 4f 53 29  ed in by the OS)
11bcd 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 48 6f 77 65  ..  **.  ** Howe
11bce 76 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20  ver it has been 
11bcf 64 69 73 63 6f 76 65 72 65 64 20 74 68 61 74 20  discovered that 
11bd0 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20  on some systems 
11bd1 74 68 69 73 20 70 61 74 74 65 72 6e 20 63 61 6e  this pattern can
11bd2 20 0a 20 20 2a 2a 20 62 65 20 73 69 67 6e 69 66   .  ** be signif
11bd3 69 63 61 6e 74 6c 79 20 73 6c 6f 77 65 72 20 74  icantly slower t
11bd4 68 61 6e 20 63 6f 6e 74 69 67 75 6f 75 73 6c 79  han contiguously
11bd5 20 77 72 69 74 69 6e 67 20 64 61 74 61 20 74 6f   writing data to
11bd6 20 74 68 65 20 66 69 6c 65 2c 0a 20 20 2a 2a 20   the file,.  ** 
11bd7 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65 61  even if that mea
11bd8 6e 73 20 65 78 70 6c 69 63 69 74 6c 79 20 77 72  ns explicitly wr
11bd9 69 74 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68  iting data to th
11bda 65 20 62 6c 6f 63 6b 20 6f 66 20 0a 20 20 2a 2a  e block of .  **
11bdb 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a   (JOURNAL_HDR_SZ
11bdc 20 2d 20 32 38 29 20 62 79 74 65 73 20 74 68 61   - 28) bytes tha
11bdd 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 75 73  t will not be us
11bde 65 64 2e 20 53 6f 20 74 68 61 74 20 69 73 20 77  ed. So that is w
11bdf 68 61 74 0a 20 20 2a 2a 20 69 73 20 64 6f 6e 65  hat.  ** is done
11be0 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  . .  **.  ** The
11be1 20 6c 6f 6f 70 20 69 73 20 72 65 71 75 69 72 65   loop is require
11be2 64 20 68 65 72 65 20 69 6e 20 63 61 73 65 20 74  d here in case t
11be3 68 65 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 69  he sector-size i
11be4 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
11be5 65 20 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  e .  ** database
11be6 20 70 61 67 65 20 73 69 7a 65 2e 20 53 69 6e 63   page size. Sinc
11be7 65 20 74 68 65 20 7a 48 65 61 64 65 72 20 62 75  e the zHeader bu
11be8 66 66 65 72 20 69 73 20 6f 6e 6c 79 20 50 61 67  ffer is only Pag
11be9 65 72 2e 70 61 67 65 53 69 7a 65 0a 20 20 2a 2a  er.pageSize.  **
11bea 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 20   bytes in size, 
11beb 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 63 61  more than one ca
11bec 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 57  ll to sqlite3OsW
11bed 72 69 74 65 28 29 20 6d 61 79 20 62 65 20 72 65  rite() may be re
11bee 71 75 69 72 65 64 0a 20 20 2a 2a 20 74 6f 20 70  quired.  ** to p
11bef 6f 70 75 6c 61 74 65 20 74 68 65 20 65 6e 74 69  opulate the enti
11bf0 72 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  re journal heade
11bf1 72 20 73 65 63 74 6f 72 2e 0a 20 20 2a 2f 20 0a  r sector..  */ .
11bf2 20 20 66 6f 72 28 6e 57 72 69 74 65 3d 30 3b 20    for(nWrite=0; 
11bf3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 26 26 6e  rc==SQLITE_OK&&n
11bf4 57 72 69 74 65 3c 4a 4f 55 52 4e 41 4c 5f 48 44  Write<JOURNAL_HD
11bf5 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 20 6e 57  R_SZ(pPager); nW
11bf6 72 69 74 65 2b 3d 6e 48 65 61 64 65 72 29 7b 0a  rite+=nHeader){.
11bf7 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48      IOTRACE(("JH
11bf8 44 52 20 25 70 20 25 6c 6c 64 20 25 64 5c 6e 22  DR %p %lld %d\n"
11bf9 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  , pPager, pPager
11bfa 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2c 20 6e 48  ->journalHdr, nH
11bfb 65 61 64 65 72 29 29 0a 20 20 20 20 72 63 20 3d  eader)).    rc =
11bfc 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
11bfd 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 48 65  pPager->jfd, zHe
11bfe 61 64 65 72 2c 20 6e 48 65 61 64 65 72 2c 20 70  ader, nHeader, p
11bff 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
11c00 66 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  f);.    pPager->
11c01 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 6e 48  journalOff += nH
11c02 65 61 64 65 72 3b 0a 20 20 7d 0a 0a 20 20 72 65  eader;.  }..  re
11c03 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
11c04 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * The journal fi
11c05 6c 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20  le must be open 
11c06 77 68 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c  when this is cal
11c07 6c 65 64 2e 20 41 20 6a 6f 75 72 6e 61 6c 20 68  led. A journal h
11c08 65 61 64 65 72 20 66 69 6c 65 0a 2a 2a 20 28 4a  eader file.** (J
11c09 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79  OURNAL_HDR_SZ by
11c0a 74 65 73 29 20 69 73 20 72 65 61 64 20 66 72 6f  tes) is read fro
11c0b 6d 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f  m the current lo
11c0c 63 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f  cation in the jo
11c0d 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 54  urnal.** file. T
11c0e 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74  he current locat
11c0f 69 6f 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ion in the journ
11c10 61 6c 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e  al file is given
11c11 20 62 79 0a 2a 2a 20 70 50 61 67 65 72 2d 3e 6a   by.** pPager->j
11c12 6f 75 72 6e 61 6c 4f 66 66 2e 20 53 65 65 20 63  ournalOff. See c
11c13 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65 20 66 75  omments above fu
11c14 6e 63 74 69 6f 6e 20 77 72 69 74 65 4a 6f 75 72  nction writeJour
11c15 6e 61 6c 48 64 72 28 29 20 66 6f 72 0a 2a 2a 20  nalHdr() for.** 
11c16 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  a description of
11c17 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61   the journal hea
11c18 64 65 72 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a  der format..**.*
11c19 2a 20 49 66 20 74 68 65 20 68 65 61 64 65 72 20  * If the header 
11c1a 69 73 20 72 65 61 64 20 73 75 63 63 65 73 73 66  is read successf
11c1b 75 6c 6c 79 2c 20 2a 70 4e 52 65 63 20 69 73 20  ully, *pNRec is 
11c1c 73 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65  set to the numbe
11c1d 72 20 6f 66 0a 2a 2a 20 70 61 67 65 20 72 65 63  r of.** page rec
11c1e 6f 72 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  ords following t
11c1f 68 69 73 20 68 65 61 64 65 72 20 61 6e 64 20 2a  his header and *
11c20 70 44 62 53 69 7a 65 20 69 73 20 73 65 74 20 74  pDbSize is set t
11c21 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  o the size of th
11c22 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 62 65  e.** database be
11c23 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63  fore the transac
11c24 74 69 6f 6e 20 62 65 67 61 6e 2c 20 69 6e 20 70  tion began, in p
11c25 61 67 65 73 2e 20 41 6c 73 6f 2c 20 70 50 61 67  ages. Also, pPag
11c26 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 0a 2a 2a  er->cksumInit.**
11c27 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 76   is set to the v
11c28 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74  alue read from t
11c29 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
11c2a 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  r. SQLITE_OK is 
11c2b 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 6e 20 74  returned.** in t
11c2c 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
11c2d 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  If the journal h
11c2e 65 61 64 65 72 20 66 69 6c 65 20 61 70 70 65 61  eader file appea
11c2f 72 73 20 74 6f 20 62 65 20 63 6f 72 72 75 70 74  rs to be corrupt
11c30 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20  ed, SQLITE_DONE 
11c31 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 61  is.** returned a
11c32 6e 64 20 2a 70 4e 52 65 63 20 61 6e 64 20 2a 50  nd *pNRec and *P
11c33 44 62 53 69 7a 65 20 61 72 65 20 75 6e 64 65 66  DbSize are undef
11c34 69 6e 65 64 2e 20 20 49 66 20 4a 4f 55 52 4e 41  ined.  If JOURNA
11c35 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 0a 2a  L_HDR_SZ bytes.*
11c36 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 61 64  * cannot be read
11c37 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
11c38 6c 20 66 69 6c 65 20 61 6e 20 65 72 72 6f 72 20  l file an error 
11c39 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
11c3a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11c3b 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 0a  readJournalHdr(.
11c3c 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c    Pager *pPager,
11c3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11c3e 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a  * Pager object *
11c3f 2f 0a 20 20 69 6e 74 20 69 73 48 6f 74 2c 0a 20  /.  int isHot,. 
11c40 20 69 36 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65   i64 journalSize
11c41 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
11c42 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 70 65   Size of the ope
11c43 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  n journal file i
11c44 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 75 33 32  n bytes */.  u32
11c45 20 2a 70 4e 52 65 63 2c 20 20 20 20 20 20 20 20   *pNRec,        
11c46 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
11c47 3a 20 56 61 6c 75 65 20 72 65 61 64 20 66 72 6f  : Value read fro
11c48 6d 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64  m the nRec field
11c49 20 2a 2f 0a 20 20 75 33 32 20 2a 70 44 62 53 69   */.  u32 *pDbSi
11c4a 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ze              
11c4b 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c 75 65     /* OUT: Value
11c4c 20 6f 66 20 6f 72 69 67 69 6e 61 6c 20 64 61 74   of original dat
11c4d 61 62 61 73 65 20 73 69 7a 65 20 66 69 65 6c 64  abase size field
11c4e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
11c4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c50 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
11c51 63 6f 64 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  code */.  unsign
11c52 65 64 20 63 68 61 72 20 61 4d 61 67 69 63 5b 38  ed char aMagic[8
11c53 5d 3b 20 20 20 20 20 2f 2a 20 41 20 62 75 66 66  ];     /* A buff
11c54 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d  er to hold the m
11c55 61 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 20  agic header */. 
11c56 20 69 36 34 20 69 48 64 72 4f 66 66 3b 20 20 20   i64 iHdrOff;   
11c57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11c58 20 4f 66 66 73 65 74 20 6f 66 20 6a 6f 75 72 6e   Offset of journ
11c59 61 6c 20 68 65 61 64 65 72 20 62 65 69 6e 67 20  al header being 
11c5a 72 65 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72  read */..  asser
11c5b 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
11c5c 2d 3e 6a 66 64 29 20 29 3b 20 20 20 20 20 20 2f  ->jfd) );      /
11c5d 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d  * Journal file m
11c5e 75 73 74 20 62 65 20 6f 70 65 6e 2e 20 2a 2f 0a  ust be open. */.
11c5f 0a 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 50 61  .  /* Advance Pa
11c60 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 74  ger.journalOff t
11c61 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  o the start of t
11c62 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 2e 20  he next sector. 
11c63 49 66 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72  If the.  ** jour
11c64 6e 61 6c 20 66 69 6c 65 20 69 73 20 74 6f 6f 20  nal file is too 
11c65 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65 72 65 20  small for there 
11c66 74 6f 20 62 65 20 61 20 68 65 61 64 65 72 20 73  to be a header s
11c67 74 6f 72 65 64 20 61 74 20 74 68 69 73 0a 20 20  tored at this.  
11c68 2a 2a 20 70 6f 69 6e 74 2c 20 72 65 74 75 72 6e  ** point, return
11c69 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 0a 20 20   SQLITE_DONE..  
11c6a 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  */.  pPager->jou
11c6b 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72 6e 61  rnalOff = journa
11c6c 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 65  lHdrOffset(pPage
11c6d 72 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  r);.  if( pPager
11c6e 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2b 4a 4f 55  ->journalOff+JOU
11c6f 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
11c70 65 72 29 20 3e 20 6a 6f 75 72 6e 61 6c 53 69 7a  er) > journalSiz
11c71 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
11c72 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
11c73 0a 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50 61  .  iHdrOff = pPa
11c74 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
11c75 0a 0a 20 20 2f 2a 20 52 65 61 64 20 69 6e 20 74  ..  /* Read in t
11c76 68 65 20 66 69 72 73 74 20 38 20 62 79 74 65 73  he first 8 bytes
11c77 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
11c78 68 65 61 64 65 72 2e 20 49 66 20 74 68 65 79 20  header. If they 
11c79 64 6f 20 6e 6f 74 20 6d 61 74 63 68 0a 20 20 2a  do not match.  *
11c7a 2a 20 74 68 65 20 20 6d 61 67 69 63 20 73 74 72  * the  magic str
11c7b 69 6e 67 20 66 6f 75 6e 64 20 61 74 20 74 68 65  ing found at the
11c7c 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 20 6a   start of each j
11c7d 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 72  ournal header, r
11c7e 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54  eturn.  ** SQLIT
11c7f 45 5f 44 4f 4e 45 2e 20 49 66 20 61 6e 20 49 4f  E_DONE. If an IO
11c80 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72   error occurs, r
11c81 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
11c82 6f 64 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a  ode. Otherwise,.
11c83 20 20 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 20 20    ** proceed..  
11c84 2a 2f 0a 20 20 69 66 28 20 69 73 48 6f 74 20 7c  */.  if( isHot |
11c85 7c 20 69 48 64 72 4f 66 66 21 3d 70 50 61 67 65  | iHdrOff!=pPage
11c86 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 29 7b  r->journalHdr ){
11c87 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
11c88 33 4f 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e  3OsRead(pPager->
11c89 6a 66 64 2c 20 61 4d 61 67 69 63 2c 20 73 69 7a  jfd, aMagic, siz
11c8a 65 6f 66 28 61 4d 61 67 69 63 29 2c 20 69 48 64  eof(aMagic), iHd
11c8b 72 4f 66 66 29 3b 0a 20 20 20 20 69 66 28 20 72  rOff);.    if( r
11c8c 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  c ){.      retur
11c8d 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
11c8e 69 66 28 20 6d 65 6d 63 6d 70 28 61 4d 61 67 69  if( memcmp(aMagi
11c8f 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  c, aJournalMagic
11c90 2c 20 73 69 7a 65 6f 66 28 61 4d 61 67 69 63 29  , sizeof(aMagic)
11c91 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  )!=0 ){.      re
11c92 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
11c93 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
11c94 2a 20 52 65 61 64 20 74 68 65 20 66 69 72 73 74  * Read the first
11c95 20 74 68 72 65 65 20 33 32 2d 62 69 74 20 66 69   three 32-bit fi
11c96 65 6c 64 73 20 6f 66 20 74 68 65 20 6a 6f 75 72  elds of the jour
11c97 6e 61 6c 20 68 65 61 64 65 72 3a 20 54 68 65 20  nal header: The 
11c98 6e 52 65 63 0a 20 20 2a 2a 20 66 69 65 6c 64 2c  nRec.  ** field,
11c99 20 74 68 65 20 63 68 65 63 6b 73 75 6d 2d 69 6e   the checksum-in
11c9a 69 74 69 61 6c 69 7a 65 72 20 61 6e 64 20 74 68  itializer and th
11c9b 65 20 64 61 74 61 62 61 73 65 20 73 69 7a 65 20  e database size 
11c9c 61 74 20 74 68 65 20 73 74 61 72 74 0a 20 20 2a  at the start.  *
11c9d 2a 20 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63  * of the transac
11c9e 74 69 6f 6e 2e 20 52 65 74 75 72 6e 20 61 6e 20  tion. Return an 
11c9f 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e  error code if an
11ca0 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ything goes wron
11ca1 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 53 51  g..  */.  if( SQ
11ca2 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72  LITE_OK!=(rc = r
11ca3 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72  ead32bits(pPager
11ca4 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 38  ->jfd, iHdrOff+8
11ca5 2c 20 70 4e 52 65 63 29 29 0a 20 20 20 7c 7c 20  , pNRec)).   || 
11ca6 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
11ca7 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67   read32bits(pPag
11ca8 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66  er->jfd, iHdrOff
11ca9 2b 31 32 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b  +12, &pPager->ck
11caa 73 75 6d 49 6e 69 74 29 29 0a 20 20 20 7c 7c 20  sumInit)).   || 
11cab 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
11cac 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67   read32bits(pPag
11cad 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66  er->jfd, iHdrOff
11cae 2b 31 36 2c 20 70 44 62 53 69 7a 65 29 29 0a 20  +16, pDbSize)). 
11caf 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
11cb0 63 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  c;.  }..  if( pP
11cb1 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11cb2 3d 3d 30 20 29 7b 0a 20 20 20 20 75 33 32 20 69  ==0 ){.    u32 i
11cb3 50 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20  PageSize;       
11cb4 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 2d          /* Page-
11cb5 73 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 6a 6f  size field of jo
11cb6 75 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a  urnal header */.
11cb7 20 20 20 20 75 33 32 20 69 53 65 63 74 6f 72 53      u32 iSectorS
11cb8 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
11cb9 20 2f 2a 20 53 65 63 74 6f 72 2d 73 69 7a 65 20   /* Sector-size 
11cba 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c  field of journal
11cbb 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75   header */.    u
11cbc 31 36 20 69 50 61 67 65 53 69 7a 65 31 36 3b 20  16 iPageSize16; 
11cbd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
11cbe 6f 70 79 20 6f 66 20 69 50 61 67 65 53 69 7a 65  opy of iPageSize
11cbf 20 69 6e 20 31 36 2d 62 69 74 20 76 61 72 69 61   in 16-bit varia
11cc0 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 52  ble */..    /* R
11cc1 65 61 64 20 74 68 65 20 70 61 67 65 2d 73 69 7a  ead the page-siz
11cc2 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a  e and sector-siz
11cc3 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
11cc4 20 66 69 65 6c 64 73 2e 20 2a 2f 0a 20 20 20 20   fields. */.    
11cc5 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
11cc6 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
11cc7 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64  pPager->jfd, iHd
11cc8 72 4f 66 66 2b 32 30 2c 20 26 69 53 65 63 74 6f  rOff+20, &iSecto
11cc9 72 53 69 7a 65 29 29 0a 20 20 20 20 20 7c 7c 20  rSize)).     || 
11cca 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
11ccb 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67   read32bits(pPag
11ccc 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66  er->jfd, iHdrOff
11ccd 2b 32 34 2c 20 26 69 50 61 67 65 53 69 7a 65 29  +24, &iPageSize)
11cce 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  ).    ){.      r
11ccf 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
11cd0 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  .    /* Check th
11cd1 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 72 65  at the values re
11cd2 61 64 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ad from the page
11cd3 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74 6f 72  -size and sector
11cd4 2d 73 69 7a 65 20 66 69 65 6c 64 73 0a 20 20 20  -size fields.   
11cd5 20 2a 2a 20 61 72 65 20 77 69 74 68 69 6e 20 72   ** are within r
11cd6 61 6e 67 65 2e 20 54 6f 20 62 65 20 27 69 6e 20  ange. To be 'in 
11cd7 72 61 6e 67 65 27 2c 20 62 6f 74 68 20 76 61 6c  range', both val
11cd8 75 65 73 20 6e 65 65 64 20 74 6f 20 62 65 20 61  ues need to be a
11cd9 20 70 6f 77 65 72 0a 20 20 20 20 2a 2a 20 6f 66   power.    ** of
11cda 20 74 77 6f 20 67 72 65 61 74 65 72 20 74 68 61   two greater tha
11cdb 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 35 31  n or equal to 51
11cdc 32 20 6f 72 20 33 32 2c 20 61 6e 64 20 6e 6f 74  2 or 32, and not
11cdd 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
11cde 65 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 73 70  eir .    ** resp
11cdf 65 63 74 69 76 65 20 63 6f 6d 70 69 6c 65 20 74  ective compile t
11ce0 69 6d 65 20 6d 61 78 69 6d 75 6d 20 6c 69 6d 69  ime maximum limi
11ce1 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ts..    */.    i
11ce2 66 28 20 69 50 61 67 65 53 69 7a 65 3c 35 31 32  f( iPageSize<512
11ce3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ce4 20 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65    || iSectorSize
11ce5 3c 33 32 0a 20 20 20 20 20 7c 7c 20 69 50 61 67  <32.     || iPag
11ce6 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58  eSize>SQLITE_MAX
11ce7 5f 50 41 47 45 5f 53 49 5a 45 20 7c 7c 20 69 53  _PAGE_SIZE || iS
11ce8 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45  ectorSize>MAX_SE
11ce9 43 54 4f 52 5f 53 49 5a 45 0a 20 20 20 20 20 7c  CTOR_SIZE.     |
11cea 7c 20 28 28 69 50 61 67 65 53 69 7a 65 2d 31 29  | ((iPageSize-1)
11ceb 26 69 50 61 67 65 53 69 7a 65 29 21 3d 30 20 20  &iPageSize)!=0  
11cec 20 7c 7c 20 28 28 69 53 65 63 74 6f 72 53 69 7a   || ((iSectorSiz
11ced 65 2d 31 29 26 69 53 65 63 74 6f 72 53 69 7a 65  e-1)&iSectorSize
11cee 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20 20  )!=0 .    ){.   
11cef 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 69 74     /* If the eit
11cf0 68 65 72 20 74 68 65 20 70 61 67 65 2d 73 69 7a  her the page-siz
11cf1 65 20 6f 72 20 73 65 63 74 6f 72 2d 73 69 7a 65  e or sector-size
11cf2 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d   in the journal-
11cf3 68 65 61 64 65 72 20 69 73 20 0a 20 20 20 20 20  header is .     
11cf4 20 2a 2a 20 69 6e 76 61 6c 69 64 2c 20 74 68 65   ** invalid, the
11cf5 6e 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68  n the process th
11cf6 61 74 20 77 72 6f 74 65 20 74 68 65 20 6a 6f 75  at wrote the jou
11cf7 72 6e 61 6c 2d 68 65 61 64 65 72 20 6d 75 73 74  rnal-header must
11cf8 20 68 61 76 65 20 0a 20 20 20 20 20 20 2a 2a 20   have .      ** 
11cf9 63 72 61 73 68 65 64 20 62 65 66 6f 72 65 20 74  crashed before t
11cfa 68 65 20 68 65 61 64 65 72 20 77 61 73 20 73 79  he header was sy
11cfb 6e 63 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  nced. In this ca
11cfc 73 65 20 73 74 6f 70 20 72 65 61 64 69 6e 67 20  se stop reading 
11cfd 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f  .      ** the jo
11cfe 75 72 6e 61 6c 20 66 69 6c 65 20 68 65 72 65 2e  urnal file here.
11cff 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
11d00 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
11d01 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  NE;.    }..    /
11d02 2a 20 55 70 64 61 74 65 20 74 68 65 20 70 61 67  * Update the pag
11d03 65 2d 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20  e-size to match 
11d04 74 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66  the value read f
11d05 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e  rom the journal.
11d06 20 0a 20 20 20 20 2a 2a 20 55 73 65 20 61 20 74   .    ** Use a t
11d07 65 73 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20  estcase() macro 
11d08 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
11d09 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  t malloc failure
11d0a 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a 2a 20   within .    ** 
11d0b 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
11d0c 28 29 20 69 73 20 74 65 73 74 65 64 2e 0a 20 20  () is tested..  
11d0d 20 20 2a 2f 0a 20 20 20 20 69 50 61 67 65 53 69    */.    iPageSi
11d0e 7a 65 31 36 20 3d 20 28 75 31 36 29 69 50 61 67  ze16 = (u16)iPag
11d0f 65 53 69 7a 65 3b 0a 20 20 20 20 72 63 20 3d 20  eSize;.    rc = 
11d10 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50  sqlite3PagerSetP
11d11 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c 20  agesize(pPager, 
11d12 26 69 50 61 67 65 53 69 7a 65 31 36 2c 20 2d 31  &iPageSize16, -1
11d13 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
11d14 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11d15 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  ;.    assert( rc
11d16 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69  !=SQLITE_OK || i
11d17 50 61 67 65 53 69 7a 65 31 36 3d 3d 28 75 31 36  PageSize16==(u16
11d18 29 69 50 61 67 65 53 69 7a 65 20 29 3b 0a 0a 20  )iPageSize );.. 
11d19 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
11d1a 20 61 73 73 75 6d 65 64 20 73 65 63 74 6f 72 2d   assumed sector-
11d1b 73 69 7a 65 20 74 6f 20 6d 61 74 63 68 20 74 68  size to match th
11d1c 65 20 76 61 6c 75 65 20 75 73 65 64 20 62 79 20  e value used by 
11d1d 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 72 6f 63  .    ** the proc
11d1e 65 73 73 20 74 68 61 74 20 63 72 65 61 74 65 64  ess that created
11d1f 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e 20 49   this journal. I
11d20 66 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 77  f this journal w
11d21 61 73 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65  as.    ** create
11d22 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 6f  d by a process o
11d23 74 68 65 72 20 74 68 61 6e 20 74 68 69 73 20 6f  ther than this o
11d24 6e 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  ne, then this ro
11d25 75 74 69 6e 65 0a 20 20 20 20 2a 2a 20 69 73 20  utine.    ** is 
11d26 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f  being called fro
11d27 6d 20 77 69 74 68 69 6e 20 70 61 67 65 72 5f 70  m within pager_p
11d28 6c 61 79 62 61 63 6b 28 29 2e 20 54 68 65 20 6c  layback(). The l
11d29 6f 63 61 6c 20 76 61 6c 75 65 0a 20 20 20 20 2a  ocal value.    *
11d2a 2a 20 6f 66 20 50 61 67 65 72 2e 73 65 63 74 6f  * of Pager.secto
11d2b 72 53 69 7a 65 20 69 73 20 72 65 73 74 6f 72 65  rSize is restore
11d2c 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  d at the end of 
11d2d 74 68 61 74 20 72 6f 75 74 69 6e 65 2e 0a 20 20  that routine..  
11d2e 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d    */.    pPager-
11d2f 3e 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 69 53  >sectorSize = iS
11d30 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 7d 0a 0a  ectorSize;.  }..
11d31 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11d32 6c 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f  lOff += JOURNAL_
11d33 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a  HDR_SZ(pPager);.
11d34 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
11d35 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  ./*.** Write the
11d36 20 73 75 70 70 6c 69 65 64 20 6d 61 73 74 65 72   supplied master
11d37 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e   journal name in
11d38 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
11d39 69 6c 65 20 66 6f 72 20 70 61 67 65 72 0a 2a 2a  ile for pager.**
11d3a 20 70 50 61 67 65 72 20 61 74 20 74 68 65 20 63   pPager at the c
11d3b 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 2e  urrent location.
11d3c 20 54 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   The master jour
11d3d 6e 61 6c 20 6e 61 6d 65 20 6d 75 73 74 20 62 65  nal name must be
11d3e 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 74 68 69   the last.** thi
11d3f 6e 67 20 77 72 69 74 74 65 6e 20 74 6f 20 61 20  ng written to a 
11d40 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66  journal file. If
11d41 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
11d42 20 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c   full-sync mode,
11d43 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
11d44 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
11d45 69 73 20 61 64 76 61 6e 63 65 64 20 74 6f 20 74  is advanced to t
11d46 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20 62  he next sector b
11d47 6f 75 6e 64 61 72 79 20 62 65 66 6f 72 65 0a 2a  oundary before.*
11d48 2a 20 61 6e 79 74 68 69 6e 67 20 69 73 20 77 72  * anything is wr
11d49 69 74 74 65 6e 2e 20 54 68 65 20 66 6f 72 6d 61  itten. The forma
11d4a 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20  t is:.**.**   + 
11d4b 34 20 62 79 74 65 73 3a 20 50 41 47 45 52 5f 4d  4 bytes: PAGER_M
11d4c 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 20 20 2b 20 4e  J_PGNO..**   + N
11d4d 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a   bytes: Master j
11d4e 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20  ournal filename 
11d4f 69 6e 20 75 74 66 2d 38 2e 0a 2a 2a 20 20 20 2b  in utf-8..**   +
11d50 20 34 20 62 79 74 65 73 3a 20 4e 20 28 6c 65 6e   4 bytes: N (len
11d51 67 74 68 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f  gth of master jo
11d52 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 20 62 79  urnal name in by
11d53 74 65 73 2c 20 6e 6f 20 6e 75 6c 2d 74 65 72 6d  tes, no nul-term
11d54 69 6e 61 74 6f 72 29 2e 0a 2a 2a 20 20 20 2b 20  inator)..**   + 
11d55 34 20 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20  4 bytes: Master 
11d56 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65  journal name che
11d57 63 6b 73 75 6d 2e 0a 2a 2a 20 20 20 2b 20 38 20  cksum..**   + 8 
11d58 62 79 74 65 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d  bytes: aJournalM
11d59 61 67 69 63 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  agic[]..**.** Th
11d5a 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
11d5b 20 70 61 67 65 20 63 68 65 63 6b 73 75 6d 20 69   page checksum i
11d5c 73 20 74 68 65 20 73 75 6d 20 6f 66 20 74 68 65  s the sum of the
11d5d 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 6d 61   bytes in the ma
11d5e 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20  ster.** journal 
11d5f 6e 61 6d 65 2c 20 77 68 65 72 65 20 65 61 63 68  name, where each
11d60 20 62 79 74 65 20 69 73 20 69 6e 74 65 72 70 72   byte is interpr
11d61 65 74 65 64 20 61 73 20 61 20 73 69 67 6e 65 64  eted as a signed
11d62 20 38 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a   8-bit integer..
11d63 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74 65 72  **.** If zMaster
11d64 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
11d65 65 72 20 28 6f 63 63 75 72 73 20 66 6f 72 20 61  er (occurs for a
11d66 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65   single database
11d67 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20 0a   transaction), .
11d68 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20  ** this call is 
11d69 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74  a no-op..*/.stat
11d6a 69 63 20 69 6e 74 20 77 72 69 74 65 4d 61 73 74  ic int writeMast
11d6b 65 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65 72 20  erJournal(Pager 
11d6c 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63  *pPager, const c
11d6d 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20  har *zMaster){. 
11d6e 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
11d6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d70 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
11d71 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 73 74 65   */.  int nMaste
11d72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
11d73 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
11d74 20 6f 66 20 73 74 72 69 6e 67 20 7a 4d 61 73 74   of string zMast
11d75 65 72 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72  er */.  i64 iHdr
11d76 4f 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Off;            
11d77 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
11d78 65 74 20 6f 66 20 68 65 61 64 65 72 20 69 6e 20  et of header in 
11d79 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
11d7a 20 20 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 20    i64 jrnlSize; 
11d7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d7c 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6a 6f     /* Size of jo
11d7d 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 20 64 69  urnal file on di
11d7e 73 6b 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75  sk */.  u32 cksu
11d7f 6d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  m = 0;          
11d80 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63           /* Chec
11d81 6b 73 75 6d 20 6f 66 20 73 74 72 69 6e 67 20 7a  ksum of string z
11d82 4d 61 73 74 65 72 20 2a 2f 0a 0a 20 20 69 66 28  Master */..  if(
11d83 20 21 7a 4d 61 73 74 65 72 20 7c 7c 20 70 50 61   !zMaster || pPa
11d84 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 0a 20  ger->setMaster. 
11d85 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75    || pPager->jou
11d86 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
11d87 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
11d88 52 59 20 0a 20 20 20 7c 7c 20 70 50 61 67 65 72  RY .   || pPager
11d89 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
11d8a 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
11d8b 5f 4f 46 46 20 0a 20 20 29 7b 0a 20 20 20 20 72  _OFF .  ){.    r
11d8c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
11d8d 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73  .  }.  pPager->s
11d8e 65 74 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20  etMaster = 1;.  
11d8f 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
11d90 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a  Pager->jfd) );..
11d91 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 65 20 74    /* Calculate t
11d92 68 65 20 6c 65 6e 67 74 68 20 69 6e 20 62 79 74  he length in byt
11d93 65 73 20 61 6e 64 20 74 68 65 20 63 68 65 63 6b  es and the check
11d94 73 75 6d 20 6f 66 20 7a 4d 61 73 74 65 72 20 2a  sum of zMaster *
11d95 2f 0a 20 20 66 6f 72 28 6e 4d 61 73 74 65 72 3d  /.  for(nMaster=
11d96 30 3b 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73 74  0; zMaster[nMast
11d97 65 72 5d 3b 20 6e 4d 61 73 74 65 72 2b 2b 29 7b  er]; nMaster++){
11d98 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 7a 4d  .    cksum += zM
11d99 61 73 74 65 72 5b 6e 4d 61 73 74 65 72 5d 3b 0a  aster[nMaster];.
11d9a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20    }..  /* If in 
11d9b 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20  full-sync mode, 
11d9c 61 64 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e  advance to the n
11d9d 65 78 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20  ext disk sector 
11d9e 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20  before writing. 
11d9f 20 2a 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a   ** the master j
11da0 6f 75 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69  ournal name. Thi
11da1 73 20 69 73 20 69 6e 20 63 61 73 65 20 74 68 65  s is in case the
11da2 20 70 72 65 76 69 6f 75 73 20 70 61 67 65 20 77   previous page w
11da3 72 69 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74  ritten to.  ** t
11da4 68 65 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61  he journal has a
11da5 6c 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63  lready been sync
11da6 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ed..  */.  if( p
11da7 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20  Pager->fullSync 
11da8 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  ){.    pPager->j
11da9 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a 6f 75 72  ournalOff = jour
11daa 6e 61 6c 48 64 72 4f 66 66 73 65 74 28 70 50 61  nalHdrOffset(pPa
11dab 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 48 64 72  ger);.  }.  iHdr
11dac 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  Off = pPager->jo
11dad 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20  urnalOff;..  /* 
11dae 57 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72  Write the master
11daf 20 6a 6f 75 72 6e 61 6c 20 64 61 74 61 20 74 6f   journal data to
11db0 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
11db1 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66  journal file. If
11db2 0a 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f  .  ** an error o
11db3 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 74 68  ccurs, return th
11db4 65 20 65 72 72 6f 72 20 63 6f 64 65 20 74 6f 20  e error code to 
11db5 74 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 2a 2f  the caller..  */
11db6 0a 20 20 69 66 28 20 28 30 20 21 3d 20 28 72 63  .  if( (0 != (rc
11db7 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70   = write32bits(p
11db8 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72  Pager->jfd, iHdr
11db9 4f 66 66 2c 20 50 41 47 45 52 5f 4d 4a 5f 50 47  Off, PAGER_MJ_PG
11dba 4e 4f 28 70 50 61 67 65 72 29 29 29 29 0a 20 20  NO(pPager)))).  
11dbb 20 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20   || (0 != (rc = 
11dbc 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
11dbd 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73  Pager->jfd, zMas
11dbe 74 65 72 2c 20 6e 4d 61 73 74 65 72 2c 20 69 48  ter, nMaster, iH
11dbf 64 72 4f 66 66 2b 34 29 29 29 0a 20 20 20 7c 7c  drOff+4))).   ||
11dc0 20 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69   (0 != (rc = wri
11dc1 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  te32bits(pPager-
11dc2 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b  >jfd, iHdrOff+4+
11dc3 6e 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74 65 72  nMaster, nMaster
11dc4 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20  ))).   || (0 != 
11dc5 28 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74  (rc = write32bit
11dc6 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69  s(pPager->jfd, i
11dc7 48 64 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72  HdrOff+4+nMaster
11dc8 2b 34 2c 20 63 6b 73 75 6d 29 29 29 0a 20 20 20  +4, cksum))).   
11dc9 7c 7c 20 28 30 20 21 3d 20 28 72 63 20 3d 20 73  || (0 != (rc = s
11dca 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
11dcb 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4a 6f 75 72  ager->jfd, aJour
11dcc 6e 61 6c 4d 61 67 69 63 2c 20 38 2c 20 69 48 64  nalMagic, 8, iHd
11dcd 72 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2b 38  rOff+4+nMaster+8
11dce 29 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  ))).  ){.    ret
11dcf 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50  urn rc;.  }.  pP
11dd0 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11dd1 20 2b 3d 20 28 6e 4d 61 73 74 65 72 2b 32 30 29   += (nMaster+20)
11dd2 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64  ;.  pPager->need
11dd3 53 79 6e 63 20 3d 20 21 70 50 61 67 65 72 2d 3e  Sync = !pPager->
11dd4 6e 6f 53 79 6e 63 3b 0a 0a 20 20 2f 2a 20 49 66  noSync;..  /* If
11dd5 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
11dd6 20 70 65 72 69 73 74 65 6e 74 2d 6a 6f 75 72 6e   peristent-journ
11dd7 61 6c 20 6d 6f 64 65 2c 20 74 68 65 6e 20 74 68  al mode, then th
11dd8 65 20 70 68 79 73 69 63 61 6c 20 0a 20 20 2a 2a  e physical .  **
11dd9 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 6d 61   journal-file ma
11dda 79 20 65 78 74 65 6e 64 20 70 61 73 74 20 74 68  y extend past th
11ddb 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73  e end of the mas
11ddc 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter-journal name
11ddd 0a 20 20 2a 2a 20 61 6e 64 20 38 20 62 79 74 65  .  ** and 8 byte
11dde 73 20 6f 66 20 6d 61 67 69 63 20 64 61 74 61 20  s of magic data 
11ddf 6a 75 73 74 20 77 72 69 74 74 65 6e 20 74 6f 20  just written to 
11de0 74 68 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69  the file. This i
11de1 73 20 0a 20 20 2a 2a 20 64 61 6e 67 65 72 6f 75  s .  ** dangerou
11de2 73 20 62 65 63 61 75 73 65 20 74 68 65 20 63 6f  s because the co
11de3 64 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61  de to rollback a
11de4 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c   hot-journal fil
11de5 65 0a 20 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20  e.  ** will not 
11de6 62 65 20 61 62 6c 65 20 74 6f 20 66 69 6e 64 20  be able to find 
11de7 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e  the master-journ
11de8 61 6c 20 6e 61 6d 65 20 74 6f 20 64 65 74 65 72  al name to deter
11de9 6d 69 6e 65 20 0a 20 20 2a 2a 20 77 68 65 74 68  mine .  ** wheth
11dea 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f  er or not the jo
11deb 75 72 6e 61 6c 20 69 73 20 68 6f 74 2e 20 0a 20  urnal is hot. . 
11dec 20 2a 2a 0a 20 20 2a 2a 20 45 61 73 69 65 73 74   **.  ** Easiest
11ded 20 74 68 69 6e 67 20 74 6f 20 64 6f 20 69 6e 20   thing to do in 
11dee 74 68 69 73 20 73 63 65 6e 61 72 69 6f 20 69 73  this scenario is
11def 20 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65   to truncate the
11df0 20 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 66   journal .  ** f
11df1 69 6c 65 20 74 6f 20 74 68 65 20 72 65 71 75 69  ile to the requi
11df2 72 65 64 20 73 69 7a 65 2e 0a 20 20 2a 2f 20 0a  red size..  */ .
11df3 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
11df4 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73  =(rc = sqlite3Os
11df5 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d  FileSize(pPager-
11df6 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65 29  >jfd, &jrnlSize)
11df7 29 0a 20 20 20 26 26 20 6a 72 6e 6c 53 69 7a 65  ).   && jrnlSize
11df8 3e 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  >pPager->journal
11df9 4f 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63 20  Off.  ){.    rc 
11dfa 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  = sqlite3OsTrunc
11dfb 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ate(pPager->jfd,
11dfc 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11dfd 4f 66 66 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Off);.  }.  retu
11dfe 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
11dff 46 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74  Find a page in t
11e00 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69  he hash table gi
11e01 76 65 6e 20 69 74 73 20 70 61 67 65 20 6e 75 6d  ven its page num
11e02 62 65 72 2e 20 52 65 74 75 72 6e 0a 2a 2a 20 61  ber. Return.** a
11e03 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
11e04 70 61 67 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20  page or NULL if 
11e05 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  the requested pa
11e06 67 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 6c  ge is not .** al
11e07 72 65 61 64 79 20 69 6e 20 6d 65 6d 6f 72 79 2e  ready in memory.
11e08 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64 72  .*/.static PgHdr
11e09 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 50   *pager_lookup(P
11e0a 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67  ager *pPager, Pg
11e0b 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64  no pgno){.  PgHd
11e0c 72 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  r *p;           
11e0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11e0e 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f   Return value */
11e0f 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74  ..  /* It is not
11e10 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 20 61 20   possible for a 
11e11 63 61 6c 6c 20 74 6f 20 50 63 61 63 68 65 46 65  call to PcacheFe
11e12 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74  tch() with creat
11e13 65 46 6c 61 67 3d 3d 30 20 74 6f 0a 20 20 2a 2a  eFlag==0 to.  **
11e14 20 66 61 69 6c 2c 20 73 69 6e 63 65 20 6e 6f 20   fail, since no 
11e15 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63  attempt to alloc
11e16 61 74 65 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f  ate dynamic memo
11e17 72 79 20 77 69 6c 6c 20 62 65 20 6d 61 64 65 2e  ry will be made.
11e18 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64 29 73 71  .  */.  (void)sq
11e19 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63 68  lite3PcacheFetch
11e1a 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
11e1b 2c 20 70 67 6e 6f 2c 20 30 2c 20 26 70 29 3b 0a  , pgno, 0, &p);.
11e1c 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
11e1d 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 74 68 65 20  *.** Unless the 
11e1e 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f  pager is in erro
11e1f 72 2d 73 74 61 74 65 2c 20 64 69 73 63 61 72 64  r-state, discard
11e20 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20 70   all in-memory p
11e21 61 67 65 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20  ages. If.** the 
11e22 70 61 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f  pager is in erro
11e23 72 2d 73 74 61 74 65 2c 20 74 68 65 6e 20 74 68  r-state, then th
11e24 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
11e25 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20  op..**.** TODO: 
11e26 57 68 79 20 63 61 6e 20 77 65 20 6e 6f 74 20 72  Why can we not r
11e27 65 73 65 74 20 74 68 65 20 70 61 67 65 72 20 77  eset the pager w
11e28 68 69 6c 65 20 69 6e 20 65 72 72 6f 72 20 73 74  hile in error st
11e29 61 74 65 3f 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ate?.*/.static v
11e2a 6f 69 64 20 70 61 67 65 72 5f 72 65 73 65 74 28  oid pager_reset(
11e2b 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
11e2c 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
11e2d 3d 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65  =pPager->errCode
11e2e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
11e2f 61 63 6b 75 70 52 65 73 74 61 72 74 28 70 50 61  ackupRestart(pPa
11e30 67 65 72 2d 3e 70 42 61 63 6b 75 70 29 3b 0a 20  ger->pBackup);. 
11e31 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
11e32 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e 70 50  Clear(pPager->pP
11e33 43 61 63 68 65 29 3b 0a 20 20 20 20 70 50 61 67  Cache);.    pPag
11e34 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20  er->dbSizeValid 
11e35 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 0;.  }.}../*.*
11e36 2a 20 46 72 65 65 20 61 6c 6c 20 73 74 72 75 63  * Free all struc
11e37 74 75 72 65 73 20 69 6e 20 74 68 65 20 50 61 67  tures in the Pag
11e38 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 5b 5d 20  er.aSavepoint[] 
11e39 61 72 72 61 79 20 61 6e 64 20 73 65 74 20 62 6f  array and set bo
11e3a 74 68 0a 2a 2a 20 50 61 67 65 72 2e 61 53 61 76  th.** Pager.aSav
11e3b 65 70 6f 69 6e 74 20 61 6e 64 20 50 61 67 65 72  epoint and Pager
11e3c 2e 6e 53 61 76 65 70 6f 69 6e 74 20 74 6f 20 7a  .nSavepoint to z
11e3d 65 72 6f 2e 20 43 6c 6f 73 65 20 74 68 65 20 73  ero. Close the s
11e3e 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 66  ub-journal.** if
11e3f 20 69 74 20 69 73 20 6f 70 65 6e 20 61 6e 64 20   it is open and 
11e40 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74  the pager is not
11e41 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f   in exclusive mo
11e42 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  de..*/.static vo
11e43 69 64 20 72 65 6c 65 61 73 65 41 6c 6c 53 61 76  id releaseAllSav
11e44 65 70 6f 69 6e 74 73 28 50 61 67 65 72 20 2a 70  epoints(Pager *p
11e45 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 69 69  Pager){.  int ii
11e46 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11e47 2f 2a 20 49 74 65 72 61 74 6f 72 20 66 6f 72 20  /* Iterator for 
11e48 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20  looping through 
11e49 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74  Pager.aSavepoint
11e4a 20 2a 2f 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20   */.  for(ii=0; 
11e4b 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65  ii<pPager->nSave
11e4c 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20  point; ii++){.  
11e4d 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
11e4e 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 61  estroy(pPager->a
11e4f 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70 49  Savepoint[ii].pI
11e50 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 7d  nSavepoint);.  }
11e51 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  .  if( !pPager->
11e52 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c  exclusiveMode ||
11e53 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75   sqlite3IsMemJou
11e54 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 6a 66  rnal(pPager->sjf
11e55 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  d) ){.    sqlite
11e56 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
11e57 3e 73 6a 66 64 29 3b 0a 20 20 7d 0a 20 20 73 71  >sjfd);.  }.  sq
11e58 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61 67 65  lite3_free(pPage
11e59 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 29 3b 0a  r->aSavepoint);.
11e5a 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70    pPager->aSavep
11e5b 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67  oint = 0;.  pPag
11e5c 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d  er->nSavepoint =
11e5d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 53   0;.  pPager->nS
11e5e 75 62 52 65 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  ubRec = 0;.}../*
11e5f 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 69 74 20  .** Set the bit 
11e60 6e 75 6d 62 65 72 20 70 67 6e 6f 20 69 6e 20 74  number pgno in t
11e61 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  he PagerSavepoin
11e62 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 0a  t.pInSavepoint .
11e63 2a 2a 20 62 69 74 76 65 63 73 20 6f 66 20 61 6c  ** bitvecs of al
11e64 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  l open savepoint
11e65 73 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  s. Return SQLITE
11e66 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75  _OK if successfu
11e67 6c 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4e  l.** or SQLITE_N
11e68 4f 4d 45 4d 20 69 66 20 61 20 6d 61 6c 6c 6f 63  OMEM if a malloc
11e69 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e   failure occurs.
11e6a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
11e6b 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74  ddToSavepointBit
11e6c 76 65 63 73 28 50 61 67 65 72 20 2a 70 50 61 67  vecs(Pager *pPag
11e6d 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  er, Pgno pgno){.
11e6e 20 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20    int ii;       
11e6f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
11e70 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
11e71 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
11e72 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65  _OK;       /* Re
11e73 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 20 20  sult code */..  
11e74 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 50 61  for(ii=0; ii<pPa
11e75 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b  ger->nSavepoint;
11e76 20 69 69 2b 2b 29 7b 0a 20 20 20 20 50 61 67 65   ii++){.    Page
11e77 72 53 61 76 65 70 6f 69 6e 74 20 2a 70 20 3d 20  rSavepoint *p = 
11e78 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f  &pPager->aSavepo
11e79 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 69 66 28  int[ii];.    if(
11e7a 20 70 67 6e 6f 3c 3d 70 2d 3e 6e 4f 72 69 67 20   pgno<=p->nOrig 
11e7b 29 7b 0a 20 20 20 20 20 20 72 63 20 7c 3d 20 73  ){.      rc |= s
11e7c 71 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28  qlite3BitvecSet(
11e7d 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74 2c  p->pInSavepoint,
11e7e 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 74 65   pgno);.      te
11e7f 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49  stcase( rc==SQLI
11e80 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20  TE_NOMEM );.    
11e81 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
11e82 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53  LITE_OK || rc==S
11e83 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20  QLITE_NOMEM );. 
11e84 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
11e85 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55  n rc;.}../*.** U
11e86 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61  nlock the databa
11e87 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 66 75  se file. This fu
11e88 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
11e89 70 20 69 66 20 74 68 65 20 70 61 67 65 72 0a 2a  p if the pager.*
11e8a 2a 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76  * is in exclusiv
11e8b 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e mode..**.** If
11e8c 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63 75   the pager is cu
11e8d 72 72 65 6e 74 6c 79 20 69 6e 20 65 72 72 6f 72  rrently in error
11e8e 20 73 74 61 74 65 2c 20 64 69 73 63 61 72 64 20   state, discard 
11e8f 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
11e90 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 61 6e  .** the cache an
11e91 64 20 72 65 73 65 74 20 74 68 65 20 50 61 67 65  d reset the Page
11e92 72 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 65  r structure inte
11e93 72 6e 61 6c 20 73 74 61 74 65 2e 20 49 66 20 74  rnal state. If t
11e94 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 6f 70  here is.** an op
11e95 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 2c  en journal-file,
11e96 20 74 68 65 6e 20 74 68 65 20 6e 65 78 74 20 74   then the next t
11e97 69 6d 65 20 61 20 73 68 61 72 65 64 2d 6c 6f 63  ime a shared-loc
11e98 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  k is obtained.**
11e99 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 66 69   on the pager fi
11e9a 6c 65 20 28 62 79 20 74 68 69 73 20 6f 72 20 61  le (by this or a
11e9b 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ny other process
11e9c 29 2c 20 69 74 20 77 69 6c 6c 20 62 65 0a 2a 2a  ), it will be.**
11e9d 20 74 72 65 61 74 65 64 20 61 73 20 61 20 68 6f   treated as a ho
11e9e 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 72 6f  t-journal and ro
11e9f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 73 74  lled back..*/.st
11ea0 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f  atic void pager_
11ea1 75 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50  unlock(Pager *pP
11ea2 61 67 65 72 29 7b 0a 20 20 69 66 28 20 21 70 50  ager){.  if( !pP
11ea3 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d  ager->exclusiveM
11ea4 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ode ){.    int r
11ea5 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
11ea6 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
11ea7 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 20 20 2f  n code */..    /
11ea8 2a 20 41 6c 77 61 79 73 20 63 6c 6f 73 65 20 74  * Always close t
11ea9 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11eaa 77 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 68  when dropping th
11eab 65 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e  e database lock.
11eac 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73  .    ** Otherwis
11ead 65 2c 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65  e, another conne
11eae 63 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e  ction with journ
11eaf 61 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d  al_mode=delete m
11eb0 69 67 68 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65  ight.    ** dele
11eb1 74 65 20 74 68 65 20 66 69 6c 65 20 6f 75 74 20  te the file out 
11eb2 66 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20  from under us.. 
11eb3 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
11eb4 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
11eb5 3e 6a 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  >jfd);.    sqlit
11eb6 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
11eb7 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
11eb8 61 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  al);.    pPager-
11eb9 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b  >pInJournal = 0;
11eba 0a 20 20 20 20 72 65 6c 65 61 73 65 41 6c 6c 53  .    releaseAllS
11ebb 61 76 65 70 6f 69 6e 74 73 28 70 50 61 67 65 72  avepoints(pPager
11ebc 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  );..    /* If th
11ebd 65 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b  e file is unlock
11ebe 65 64 2c 20 73 6f 6d 65 62 6f 64 79 20 65 6c 73  ed, somebody els
11ebf 65 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 69  e might change i
11ec0 74 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 76 61  t. The.    ** va
11ec1 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 50  lues stored in P
11ec2 61 67 65 72 2e 64 62 53 69 7a 65 20 65 74 63 2e  ager.dbSize etc.
11ec3 20 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 69 6e   might become in
11ec4 76 61 6c 69 64 20 69 66 0a 20 20 20 20 2a 2a 20  valid if.    ** 
11ec5 74 68 69 73 20 68 61 70 70 65 6e 73 2e 20 54 4f  this happens. TO
11ec6 44 4f 3a 20 52 65 61 6c 6c 79 2c 20 74 68 69 73  DO: Really, this
11ec7 20 64 6f 65 73 6e 27 74 20 6e 65 65 64 20 74 6f   doesn't need to
11ec8 20 62 65 20 63 6c 65 61 72 65 64 0a 20 20 20 20   be cleared.    
11ec9 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 68 61  ** until the cha
11eca 6e 67 65 2d 63 6f 75 6e 74 65 72 20 63 68 65 63  nge-counter chec
11ecb 6b 20 66 61 69 6c 73 20 69 6e 20 50 61 67 65 72  k fails in Pager
11ecc 53 68 61 72 65 64 4c 6f 63 6b 28 29 2e 0a 20 20  SharedLock()..  
11ecd 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d    */.    pPager-
11ece 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20 30  >dbSizeValid = 0
11ecf 3b 0a 0a 20 20 20 20 72 63 20 3d 20 6f 73 55 6e  ;..    rc = osUn
11ed0 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c  lock(pPager->fd,
11ed1 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69   NO_LOCK);.    i
11ed2 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 70  f( rc ){.      p
11ed3 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d  Pager->errCode =
11ed4 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 49   rc;.    }.    I
11ed5 4f 54 52 41 43 45 28 28 22 55 4e 4c 4f 43 4b 20  OTRACE(("UNLOCK 
11ed6 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29 0a  %p\n", pPager)).
11ed7 0a 20 20 20 20 2f 2a 20 49 66 20 50 61 67 65 72  .    /* If Pager
11ed8 2e 65 72 72 43 6f 64 65 20 69 73 20 73 65 74 2c  .errCode is set,
11ed9 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
11eda 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65   the pager cache
11edb 20 63 61 6e 6e 6f 74 20 62 65 0a 20 20 20 20 2a   cannot be.    *
11edc 2a 20 74 72 75 73 74 65 64 2e 20 4e 6f 77 20 74  * trusted. Now t
11edd 68 61 74 20 74 68 65 20 70 61 67 65 72 20 66 69  hat the pager fi
11ede 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2c 20  le is unlocked, 
11edf 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
11ee0 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 63 68 65  the.    ** cache
11ee1 20 63 61 6e 20 62 65 20 64 69 73 63 61 72 64 65   can be discarde
11ee2 64 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20  d and the error 
11ee3 63 6f 64 65 20 73 61 66 65 6c 79 20 63 6c 65 61  code safely clea
11ee4 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  red..    */.    
11ee5 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  if( pPager->errC
11ee6 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ode ){.      if(
11ee7 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
11ee8 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72  {.        pPager
11ee9 2d 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49  ->errCode = SQLI
11eea 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20  TE_OK;.      }. 
11eeb 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74       pager_reset
11eec 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a  (pPager);.    }.
11eed 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61  .    pPager->cha
11eee 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30  ngeCountDone = 0
11eef 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  ;.    pPager->st
11ef0 61 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f  ate = PAGER_UNLO
11ef1 43 4b 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  CK;.    pPager->
11ef2 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 30 3b 0a  dbModified = 0;.
11ef3 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
11ef4 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  s function shoul
11ef5 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  d be called when
11ef6 20 61 6e 20 49 4f 45 52 52 2c 20 43 4f 52 52 55   an IOERR, CORRU
11ef7 50 54 20 6f 72 20 46 55 4c 4c 20 65 72 72 6f 72  PT or FULL error
11ef8 0a 2a 2a 20 6d 61 79 20 68 61 76 65 20 6f 63 63  .** may have occ
11ef9 75 72 72 65 64 2e 20 54 68 65 20 66 69 72 73 74  urred. The first
11efa 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70   argument is a p
11efb 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61  ointer to the pa
11efc 67 65 72 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  ger .** structur
11efd 65 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 74 68  e, the second th
11efe 65 20 65 72 72 6f 72 2d 63 6f 64 65 20 61 62 6f  e error-code abo
11eff 75 74 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  ut to be returne
11f00 64 20 62 79 20 61 20 70 61 67 65 72 20 0a 2a 2a  d by a pager .**
11f01 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 20 54   API function. T
11f02 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
11f03 64 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  d is a copy of t
11f04 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
11f05 6e 74 20 0a 2a 2a 20 74 6f 20 74 68 69 73 20 66  nt .** to this f
11f06 75 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20  unction. .**.** 
11f07 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
11f08 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49 54 45  gument is SQLITE
11f09 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43  _IOERR, SQLITE_C
11f0a 4f 52 52 55 50 54 2c 20 6f 72 20 53 51 4c 49 54  ORRUPT, or SQLIT
11f0b 45 5f 46 55 4c 4c 0a 2a 2a 20 74 68 65 20 65 72  E_FULL.** the er
11f0c 72 6f 72 20 62 65 63 6f 6d 65 73 20 70 65 72 73  ror becomes pers
11f0d 69 73 74 65 6e 74 2e 20 55 6e 74 69 6c 20 74 68  istent. Until th
11f0e 65 20 70 65 72 73 69 73 74 65 6e 20 65 72 72 6f  e persisten erro
11f0f 72 20 69 73 20 63 6c 65 61 72 65 64 2c 0a 2a 2a  r is cleared,.**
11f10 20 73 75 62 73 65 71 75 65 6e 74 20 41 50 49 20   subsequent API 
11f11 63 61 6c 6c 73 20 6f 6e 20 74 68 69 73 20 50 61  calls on this Pa
11f12 67 65 72 20 77 69 6c 6c 20 69 6d 6d 65 64 69 61  ger will immedia
11f13 74 65 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20  tely return the 
11f14 73 61 6d 65 20 0a 2a 2a 20 65 72 72 6f 72 20 63  same .** error c
11f15 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 65 72  ode..**.** A per
11f16 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 69 6e  sistent error in
11f17 64 69 63 61 74 65 73 20 74 68 61 74 20 74 68 65  dicates that the
11f18 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
11f19 20 70 61 67 65 72 2d 63 61 63 68 65 20 0a 2a 2a   pager-cache .**
11f1a 20 63 61 6e 6e 6f 74 20 62 65 20 74 72 75 73 74   cannot be trust
11f1b 65 64 2e 20 54 68 69 73 20 73 74 61 74 65 20 63  ed. This state c
11f1c 61 6e 20 62 65 20 63 6c 65 61 72 65 64 20 62 79  an be cleared by
11f1d 20 63 6f 6d 70 6c 65 74 65 6c 79 20 64 69 73 63   completely disc
11f1e 61 72 64 69 6e 67 20 0a 2a 2a 20 74 68 65 20 63  arding .** the c
11f1f 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70  ontents of the p
11f20 61 67 65 72 2d 63 61 63 68 65 2e 20 49 66 20 61  ager-cache. If a
11f21 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
11f22 20 61 63 74 69 76 65 20 77 68 65 6e 0a 2a 2a 20   active when.** 
11f23 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 65  the persistent e
11f24 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74  rror occurred, t
11f25 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  hen the rollback
11f26 20 6a 6f 75 72 6e 61 6c 20 6d 61 79 20 6e 65 65   journal may nee
11f27 64 0a 2a 2a 20 74 6f 20 62 65 20 72 65 70 6c 61  d.** to be repla
11f28 79 65 64 20 74 6f 20 72 65 73 74 6f 72 65 20 74  yed to restore t
11f29 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
11f2a 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
11f2b 20 28 61 73 20 69 66 0a 2a 2a 20 69 74 20 77 65   (as if.** it we
11f2c 72 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  re a hot-journal
11f2d 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
11f2e 20 70 61 67 65 72 5f 65 72 72 6f 72 28 50 61 67   pager_error(Pag
11f2f 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
11f30 72 63 29 7b 0a 20 20 69 6e 74 20 72 63 32 20 3d  rc){.  int rc2 =
11f31 20 72 63 20 26 20 30 78 66 66 3b 0a 20 20 61 73   rc & 0xff;.  as
11f32 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
11f33 5f 4f 4b 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b  _OK || !MEMDB );
11f34 0a 20 20 61 73 73 65 72 74 28 0a 20 20 20 20 20  .  assert(.     
11f35 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64    pPager->errCod
11f36 65 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c  e==SQLITE_FULL |
11f37 7c 0a 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  |.       pPager-
11f38 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45  >errCode==SQLITE
11f39 5f 4f 4b 20 7c 7c 0a 20 20 20 20 20 20 20 28 70  _OK ||.       (p
11f3a 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26  Pager->errCode &
11f3b 20 30 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49   0xff)==SQLITE_I
11f3c 4f 45 52 52 0a 20 20 29 3b 0a 20 20 69 66 28 20  OERR.  );.  if( 
11f3d 72 63 32 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c  rc2==SQLITE_FULL
11f3e 20 7c 7c 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f   || rc2==SQLITE_
11f3f 49 4f 45 52 52 20 29 7b 0a 20 20 20 20 70 50 61  IOERR ){.    pPa
11f40 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72  ger->errCode = r
11f41 63 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  c;.  }.  return 
11f42 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65  rc;.}../*.** Exe
11f43 63 75 74 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20  cute a rollback 
11f44 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  if a transaction
11f45 20 69 73 20 61 63 74 69 76 65 20 61 6e 64 20 75   is active and u
11f46 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a 2a 20 64 61  nlock the .** da
11f47 74 61 62 61 73 65 20 66 69 6c 65 2e 20 0a 2a 2a  tabase file. .**
11f48 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72  .** If the pager
11f49 20 68 61 73 20 61 6c 72 65 61 64 79 20 65 6e 74   has already ent
11f4a 65 72 65 64 20 74 68 65 20 65 72 72 6f 72 20 73  ered the error s
11f4b 74 61 74 65 2c 20 64 6f 20 6e 6f 74 20 61 74 74  tate, do not att
11f4c 65 6d 70 74 20 0a 2a 2a 20 74 68 65 20 72 6f 6c  empt .** the rol
11f4d 6c 62 61 63 6b 20 61 74 20 74 68 69 73 20 74 69  lback at this ti
11f4e 6d 65 2e 20 49 6e 73 74 65 61 64 2c 20 70 61 67  me. Instead, pag
11f4f 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73 20 63  er_unlock() is c
11f50 61 6c 6c 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61  alled. The.** ca
11f51 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c 6f  ll to pager_unlo
11f52 63 6b 28 29 20 77 69 6c 6c 20 64 69 73 63 61 72  ck() will discar
11f53 64 20 61 6c 6c 20 69 6e 2d 6d 65 6d 6f 72 79 20  d all in-memory 
11f54 70 61 67 65 73 2c 20 75 6e 6c 6f 63 6b 0a 2a 2a  pages, unlock.**
11f55 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
11f56 6c 65 20 61 6e 64 20 63 6c 65 61 72 20 74 68 65  le and clear the
11f57 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 49 66   error state. If
11f58 20 74 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74   this means that
11f59 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20 68  .** there is a h
11f5a 6f 74 2d 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20  ot-journal left 
11f5b 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74  in the file-syst
11f5c 65 6d 2c 20 74 68 65 20 6e 65 78 74 20 63 6f 6e  em, the next con
11f5d 6e 65 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 6f 62  nection.** to ob
11f5e 74 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f  tain a shared lo
11f5f 63 6b 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20  ck on the pager 
11f60 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 74 68  (which may be th
11f61 69 73 20 6f 6e 65 29 20 77 69 6c 6c 0a 2a 2a 20  is one) will.** 
11f62 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a  roll it back..**
11f63 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72  .** If the pager
11f64 20 68 61 73 20 6e 6f 74 20 61 6c 72 65 61 64 79   has not already
11f65 20 65 6e 74 65 72 65 64 20 74 68 65 20 65 72 72   entered the err
11f66 6f 72 20 73 74 61 74 65 2c 20 62 75 74 20 61 6e  or state, but an
11f67 20 49 4f 20 6f 72 0a 2a 2a 20 6d 61 6c 6c 6f 63   IO or.** malloc
11f68 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
11f69 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c  ring a rollback,
11f6a 20 74 68 65 6e 20 74 68 69 73 20 77 69 6c 6c 20   then this will 
11f6b 69 74 73 65 6c 66 20 63 61 75 73 65 20 0a 2a 2a  itself cause .**
11f6c 20 74 68 65 20 70 61 67 65 72 20 74 6f 20 65 6e   the pager to en
11f6d 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74  ter the error st
11f6e 61 74 65 2e 20 57 68 69 63 68 20 77 69 6c 6c 20  ate. Which will 
11f6f 62 65 20 63 6c 65 61 72 65 64 20 62 79 20 74 68  be cleared by th
11f70 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 70 61 67  e.** call to pag
11f71 65 72 5f 75 6e 6c 6f 63 6b 28 29 2c 20 61 73 20  er_unlock(), as 
11f72 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e  described above.
11f73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
11f74 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f  pagerUnlockAndRo
11f75 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50  llback(Pager *pP
11f76 61 67 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61  ager){.  if( pPa
11f77 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51  ger->errCode==SQ
11f78 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65  LITE_OK && pPage
11f79 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
11f7a 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20  RESERVED ){.    
11f7b 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
11f7c 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
11f7d 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c  sqlite3PagerRoll
11f7e 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20  back(pPager);.  
11f7f 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
11f80 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a  gnMalloc();.  }.
11f81 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70    pager_unlock(p
11f82 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Pager);.}../*.**
11f83 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e   This routine en
11f84 64 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ds a transaction
11f85 2e 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  . A transaction 
11f86 69 73 20 75 73 75 61 6c 6c 79 20 65 6e 64 65 64  is usually ended
11f87 20 62 79 20 0a 2a 2a 20 65 69 74 68 65 72 20 61   by .** either a
11f88 20 43 4f 4d 4d 49 54 20 6f 72 20 61 20 52 4f 4c   COMMIT or a ROL
11f89 4c 42 41 43 4b 20 6f 70 65 72 61 74 69 6f 6e 2e  LBACK operation.
11f8a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61   This routine ma
11f8b 79 20 62 65 20 63 61 6c 6c 65 64 20 0a 2a 2a 20  y be called .** 
11f8c 61 66 74 65 72 20 72 6f 6c 6c 62 61 63 6b 20 6f  after rollback o
11f8d 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c  f a hot-journal,
11f8e 20 6f 72 20 69 66 20 61 6e 20 65 72 72 6f 72 20   or if an error 
11f8f 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70 65  occurs while ope
11f90 6e 69 6e 67 0a 2a 2a 20 74 68 65 20 6a 6f 75 72  ning.** the jour
11f91 6e 61 6c 20 66 69 6c 65 20 6f 72 20 77 72 69 74  nal file or writ
11f92 69 6e 67 20 74 68 65 20 76 65 72 79 20 66 69 72  ing the very fir
11f93 73 74 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65  st journal-heade
11f94 72 20 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61  r of a.** databa
11f95 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  se transaction..
11f96 2a 2a 20 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  ** .** If the pa
11f97 67 65 72 20 69 73 20 69 6e 20 50 41 47 45 52 5f  ger is in PAGER_
11f98 53 48 41 52 45 44 20 6f 72 20 50 41 47 45 52 5f  SHARED or PAGER_
11f99 55 4e 4c 4f 43 4b 20 73 74 61 74 65 20 77 68 65  UNLOCK state whe
11f9a 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  n this.** routin
11f9b 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20  e is called, it 
11f9c 69 73 20 61 20 6e 6f 2d 6f 70 20 28 72 65 74 75  is a no-op (retu
11f9d 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 29 2e 0a  rns SQLITE_OK)..
11f9e 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
11f9f 20 61 6e 79 20 61 63 74 69 76 65 20 73 61 76 65   any active save
11fa0 70 6f 69 6e 74 73 20 61 72 65 20 72 65 6c 65 61  points are relea
11fa1 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  sed..**.** If th
11fa2 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
11fa3 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20  s open, then it 
11fa4 69 73 20 22 66 69 6e 61 6c 69 7a 65 64 22 2e 20  is "finalized". 
11fa5 4f 6e 63 65 20 61 20 6a 6f 75 72 6e 61 6c 20 0a  Once a journal .
11fa6 2a 2a 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e  ** file has been
11fa7 20 66 69 6e 61 6c 69 7a 65 64 20 69 74 20 69 73   finalized it is
11fa8 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
11fa9 20 75 73 65 20 69 74 20 74 6f 20 72 6f 6c 6c 20   use it to roll 
11faa 62 61 63 6b 20 61 20 0a 2a 2a 20 74 72 61 6e 73  back a .** trans
11fab 61 63 74 69 6f 6e 2e 20 4e 6f 72 20 77 69 6c 6c  action. Nor will
11fac 20 69 74 20 62 65 20 63 6f 6e 73 69 64 65 72 65   it be considere
11fad 64 20 74 6f 20 62 65 20 61 20 68 6f 74 2d 6a 6f  d to be a hot-jo
11fae 75 72 6e 61 6c 20 62 79 20 74 68 69 73 0a 2a 2a  urnal by this.**
11faf 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 64 61   or any other da
11fb0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
11fb1 6e 2e 20 45 78 61 63 74 6c 79 20 68 6f 77 20 61  n. Exactly how a
11fb2 20 6a 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61   journal is fina
11fb3 6c 69 7a 65 64 0a 2a 2a 20 64 65 70 65 6e 64 73  lized.** depends
11fb4 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e   on whether or n
11fb5 6f 74 20 74 68 65 20 70 61 67 65 72 20 69 73 20  ot the pager is 
11fb6 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75  running in exclu
11fb7 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 0a 2a 2a  sive mode and.**
11fb8 20 74 68 65 20 63 75 72 72 65 6e 74 20 6a 6f 75   the current jou
11fb9 72 6e 61 6c 2d 6d 6f 64 65 20 28 50 61 67 65 72  rnal-mode (Pager
11fba 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 76 61 6c  .journalMode val
11fbb 75 65 29 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ue), as follows:
11fbc 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c  .**.**   journal
11fbd 4d 6f 64 65 3d 3d 4d 45 4d 4f 52 59 0a 2a 2a 20  Mode==MEMORY.** 
11fbe 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65      Journal file
11fbf 20 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 73   descriptor is s
11fc0 69 6d 70 6c 79 20 63 6c 6f 73 65 64 2e 20 54 68  imply closed. Th
11fc1 69 73 20 64 65 73 74 72 6f 79 73 20 61 6e 20 0a  is destroys an .
11fc2 2a 2a 20 20 20 20 20 69 6e 2d 6d 65 6d 6f 72 79  **     in-memory
11fc3 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20   journal..**.** 
11fc4 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 54    journalMode==T
11fc5 52 55 4e 43 41 54 45 0a 2a 2a 20 20 20 20 20 4a  RUNCATE.**     J
11fc6 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 74  ournal file is t
11fc7 72 75 6e 63 61 74 65 64 20 74 6f 20 7a 65 72 6f  runcated to zero
11fc8 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a   bytes in size..
11fc9 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d  **.**   journalM
11fca 6f 64 65 3d 3d 50 45 52 53 49 53 54 0a 2a 2a 20  ode==PERSIST.** 
11fcb 20 20 20 20 54 68 65 20 66 69 72 73 74 20 32 38      The first 28
11fcc 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 6a 6f   bytes of the jo
11fcd 75 72 6e 61 6c 20 66 69 6c 65 20 61 72 65 20 7a  urnal file are z
11fce 65 72 6f 65 64 2e 20 54 68 69 73 20 69 6e 76 61  eroed. This inva
11fcf 6c 69 64 61 74 65 73 0a 2a 2a 20 20 20 20 20 74  lidates.**     t
11fd0 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c  he first journal
11fd1 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 66   header in the f
11fd2 69 6c 65 2c 20 61 6e 64 20 68 65 6e 63 65 20 74  ile, and hence t
11fd3 68 65 20 65 6e 74 69 72 65 20 6a 6f 75 72 6e 61  he entire journa
11fd4 6c 0a 2a 2a 20 20 20 20 20 66 69 6c 65 2e 20 41  l.**     file. A
11fd5 6e 20 69 6e 76 61 6c 69 64 20 6a 6f 75 72 6e 61  n invalid journa
11fd6 6c 20 66 69 6c 65 20 63 61 6e 6e 6f 74 20 62 65  l file cannot be
11fd7 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a   rolled back..**
11fd8 0a 2a 2a 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64  .**   journalMod
11fd9 65 3d 3d 44 45 4c 45 54 45 0a 2a 2a 20 20 20 20  e==DELETE.**    
11fda 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
11fdb 65 20 69 73 20 63 6c 6f 73 65 64 20 61 6e 64 20  e is closed and 
11fdc 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 73 71  deleted using sq
11fdd 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 29 2e  lite3OsDelete().
11fde 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 66 20 74 68  .**.**     If th
11fdf 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69  e pager is runni
11fe0 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20  ng in exclusive 
11fe1 6d 6f 64 65 2c 20 74 68 69 73 20 6d 65 74 68 6f  mode, this metho
11fe2 64 20 6f 66 20 66 69 6e 61 6c 69 7a 69 6e 67 0a  d of finalizing.
11fe3 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f 75 72 6e  **     the journ
11fe4 61 6c 20 66 69 6c 65 20 69 73 20 6e 65 76 65 72  al file is never
11fe5 20 75 73 65 64 2e 20 49 6e 73 74 65 61 64 2c 20   used. Instead, 
11fe6 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 4d 6f  if the journalMo
11fe7 64 65 20 69 73 0a 2a 2a 20 20 20 20 20 44 45 4c  de is.**     DEL
11fe8 45 54 45 20 61 6e 64 20 74 68 65 20 70 61 67 65  ETE and the page
11fe9 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76  r is in exclusiv
11fea 65 20 6d 6f 64 65 2c 20 74 68 65 20 6d 65 74 68  e mode, the meth
11feb 6f 64 20 64 65 73 63 72 69 62 65 64 20 75 6e 64  od described und
11fec 65 72 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61  er.**     journa
11fed 6c 4d 6f 64 65 3d 3d 50 45 52 53 49 53 54 20 69  lMode==PERSIST i
11fee 73 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e 0a  s used instead..
11fef 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20  **.** After the 
11ff0 6a 6f 75 72 6e 61 6c 20 69 73 20 66 69 6e 61 6c  journal is final
11ff1 69 7a 65 64 2c 20 69 66 20 72 75 6e 6e 69 6e 67  ized, if running
11ff2 20 69 6e 20 6e 6f 6e 2d 65 78 63 6c 75 73 69 76   in non-exclusiv
11ff3 65 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 70  e mode, the.** p
11ff4 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 50 41  ager moves to PA
11ff5 47 45 52 5f 53 48 41 52 45 44 20 73 74 61 74 65  GER_SHARED state
11ff6 20 28 61 6e 64 20 64 6f 77 6e 67 72 61 64 65 73   (and downgrades
11ff7 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65   the lock on the
11ff8 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c  .** database fil
11ff9 65 20 61 63 63 6f 72 64 69 6e 67 6c 79 29 2e 0a  e accordingly)..
11ffa 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  **.** If the pag
11ffb 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e  er is running in
11ffc 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 20   exclusive mode 
11ffd 61 6e 64 20 69 73 20 69 6e 20 50 41 47 45 52 5f  and is in PAGER_
11ffe 53 59 4e 43 45 44 20 73 74 61 74 65 2c 0a 2a 2a  SYNCED state,.**
11fff 20 69 74 20 6d 6f 76 65 73 20 74 6f 20 50 41 47   it moves to PAG
12000 45 52 5f 45 58 43 4c 55 53 49 56 45 2e 20 4e 6f  ER_EXCLUSIVE. No
12001 20 6c 6f 63 6b 73 20 61 72 65 20 64 6f 77 6e 67   locks are downg
12002 72 61 64 65 64 20 77 68 65 6e 20 72 75 6e 6e 69  raded when runni
12003 6e 67 20 69 6e 0a 2a 2a 20 65 78 63 6c 75 73 69  ng in.** exclusi
12004 76 65 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53  ve mode..**.** S
12005 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
12006 72 6e 65 64 20 69 66 20 6e 6f 20 65 72 72 6f 72  rned if no error
12007 20 6f 63 63 75 72 73 2e 20 49 66 20 61 6e 20 65   occurs. If an e
12008 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
12009 6e 67 0a 2a 2a 20 61 6e 79 20 6f 66 20 74 68 65  ng.** any of the
1200a 20 49 4f 20 6f 70 65 72 61 74 69 6f 6e 73 20 74   IO operations t
1200b 6f 20 66 69 6e 61 6c 69 7a 65 20 74 68 65 20 6a  o finalize the j
1200c 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20 75  ournal file or u
1200d 6e 6c 6f 63 6b 20 74 68 65 0a 2a 2a 20 64 61 74  nlock the.** dat
1200e 61 62 61 73 65 20 74 68 65 6e 20 74 68 65 20 49  abase then the I
1200f 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  O error code is 
12010 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
12011 75 73 65 72 2e 20 49 66 20 74 68 65 20 0a 2a 2a  user. If the .**
12012 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 66 69   operation to fi
12013 6e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e  nalize the journ
12014 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 2c 20 74  al file fails, t
12015 68 65 6e 20 74 68 65 20 63 6f 64 65 20 73 74 69  hen the code sti
12016 6c 6c 0a 2a 2a 20 74 72 69 65 73 20 74 6f 20 75  ll.** tries to u
12017 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61  nlock the databa
12018 73 65 20 66 69 6c 65 20 69 66 20 6e 6f 74 20 69  se file if not i
12019 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
1201a 2e 20 49 66 20 74 68 65 0a 2a 2a 20 75 6e 6c 6f  . If the.** unlo
1201b 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 66 61 69  ck operation fai
1201c 6c 73 20 61 73 20 77 65 6c 6c 2c 20 74 68 65 6e  ls as well, then
1201d 20 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72   the first error
1201e 20 63 6f 64 65 20 72 65 6c 61 74 65 64 0a 2a 2a   code related.**
1201f 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 72   to the first er
12020 72 6f 72 20 65 6e 63 6f 75 6e 74 65 72 65 64 20  ror encountered 
12021 28 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6e  (the journal fin
12022 61 6c 69 7a 61 74 69 6f 6e 20 6f 6e 65 29 20 69  alization one) i
12023 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a  s.** returned..*
12024 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
12025 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  er_end_transacti
12026 6f 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  on(Pager *pPager
12027 2c 20 69 6e 74 20 68 61 73 4d 61 73 74 65 72 29  , int hasMaster)
12028 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
12029 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20  ITE_OK;      /* 
1202a 45 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20  Error code from 
1202b 6a 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61  journal finaliza
1202c 74 69 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 20 2a  tion operation *
1202d 2f 0a 20 20 69 6e 74 20 72 63 32 20 3d 20 53 51  /.  int rc2 = SQ
1202e 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20  LITE_OK;     /* 
1202f 45 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20  Error code from 
12030 64 62 20 66 69 6c 65 20 75 6e 6c 6f 63 6b 20 6f  db file unlock o
12031 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69  peration */..  i
12032 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
12033 3c 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  <PAGER_RESERVED 
12034 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
12035 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72  LITE_OK;.  }.  r
12036 65 6c 65 61 73 65 41 6c 6c 53 61 76 65 70 6f 69  eleaseAllSavepoi
12037 6e 74 73 28 70 50 61 67 65 72 29 3b 0a 0a 20 20  nts(pPager);..  
12038 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
12039 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70  Pager->jfd) || p
1203a 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
1203b 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 69 73  l==0 );.  if( is
1203c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
1203d 29 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e  ) ){..    /* Fin
1203e 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61  alize the journa
1203f 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69  l file. */.    i
12040 66 28 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a  f( sqlite3IsMemJ
12041 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a  ournal(pPager->j
12042 66 64 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73  fd) ){.      ass
12043 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ert( pPager->jou
12044 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
12045 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
12046 52 59 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  RY );.      sqli
12047 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65  te3OsClose(pPage
12048 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c  r->jfd);.    }el
12049 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a  se if( pPager->j
1204a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
1204b 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52  R_JOURNALMODE_TR
1204c 55 4e 43 41 54 45 20 29 7b 0a 20 20 20 20 20 20  UNCATE ){.      
1204d 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
1204e 6e 61 6c 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20  nalOff==0 ){.   
1204f 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
12050 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  _OK;.      }else
12051 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
12052 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
12053 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30 29  (pPager->jfd, 0)
12054 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
12055 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
12056 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  ff = 0;.      pP
12057 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61  ager->journalSta
12058 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65  rted = 0;.    }e
12059 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e  lse if( pPager->
1205a 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20  exclusiveMode . 
1205b 20 20 20 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a      || pPager->j
1205c 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45  ournalMode==PAGE
1205d 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45  R_JOURNALMODE_PE
1205e 52 53 49 53 54 0a 20 20 20 20 29 7b 0a 20 20 20  RSIST.    ){.   
1205f 20 20 20 72 63 20 3d 20 7a 65 72 6f 4a 6f 75 72     rc = zeroJour
12060 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 68  nalHdr(pPager, h
12061 61 73 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  asMaster);.     
12062 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61   pager_error(pPa
12063 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20  ger, rc);.      
12064 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
12065 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  ff = 0;.      pP
12066 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61  ager->journalSta
12067 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 65  rted = 0;.    }e
12068 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  lse{.      /* Th
12069 69 73 20 62 72 61 6e 63 68 20 6d 61 79 20 62 65  is branch may be
1206a 20 65 78 65 63 75 74 65 64 20 77 69 74 68 20 50   executed with P
1206b 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ager.journalMode
1206c 3d 3d 4d 45 4d 4f 52 59 20 69 66 0a 20 20 20 20  ==MEMORY if.    
1206d 20 20 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e    ** a hot-journ
1206e 61 6c 20 77 61 73 20 6a 75 73 74 20 72 6f 6c 6c  al was just roll
1206f 65 64 20 62 61 63 6b 2e 20 49 6e 20 74 68 69 73  ed back. In this
12070 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72 6e 61   case the journa
12071 6c 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20  l.      ** file 
12072 73 68 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64  should be closed
12073 20 61 6e 64 20 64 65 6c 65 74 65 64 2e 20 49 66   and deleted. If
12074 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e   this connection
12075 20 77 72 69 74 65 73 20 74 6f 0a 20 20 20 20 20   writes to.     
12076 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
12077 20 66 69 6c 65 2c 20 69 74 20 77 69 6c 6c 20 64   file, it will d
12078 6f 20 73 6f 20 75 73 69 6e 67 20 61 6e 20 69 6e  o so using an in
12079 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 2e  -memory journal.
1207a 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72    */.      asser
1207b 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  t( pPager->journ
1207c 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
1207d 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45  URNALMODE_DELETE
1207e 20 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20   .           || 
1207f 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
12080 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
12081 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20  ALMODE_MEMORY . 
12082 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 73 71       );.      sq
12083 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
12084 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20  ger->jfd);.     
12085 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 74 65   if( !pPager->te
12086 6d 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20  mpFile ){.      
12087 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
12088 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 70  Delete(pPager->p
12089 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f  Vfs, pPager->zJo
1208a 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20  urnal, 0);.     
1208b 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 64 65 66   }.    }..#ifdef
1208c 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
1208d 47 45 53 0a 20 20 20 20 73 71 6c 69 74 65 33 50  GES.    sqlite3P
1208e 63 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74  cacheIterateDirt
1208f 79 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  y(pPager->pPCach
12090 65 2c 20 70 61 67 65 72 5f 73 65 74 5f 70 61 67  e, pager_set_pag
12091 65 68 61 73 68 29 3b 0a 23 65 6e 64 69 66 0a 0a  ehash);.#endif..
12092 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
12093 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 72  eCleanAll(pPager
12094 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 20 20  ->pPCache);.    
12095 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73  sqlite3BitvecDes
12096 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e  troy(pPager->pIn
12097 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70 50  Journal);.    pP
12098 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
12099 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
1209a 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20 7d 0a  ->nRec = 0;.  }.
1209b 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e  .  if( !pPager->
1209c 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20 29 7b  exclusiveMode ){
1209d 0a 20 20 20 20 72 63 32 20 3d 20 6f 73 55 6e 6c  .    rc2 = osUnl
1209e 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ock(pPager->fd, 
1209f 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20  SHARED_LOCK);.  
120a0 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
120a1 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44 3b 0a  = PAGER_SHARED;.
120a2 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e      pPager->chan
120a3 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 30 3b  geCountDone = 0;
120a4 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61  .  }else if( pPa
120a5 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45  ger->state==PAGE
120a6 52 5f 53 59 4e 43 45 44 20 29 7b 0a 20 20 20 20  R_SYNCED ){.    
120a7 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20  pPager->state = 
120a8 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b  PAGER_EXCLUSIVE;
120a9 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73  .  }.  pPager->s
120aa 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20  etMaster = 0;.  
120ab 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
120ac 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e   = 0;.  pPager->
120ad 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 30 3b 0a  dbModified = 0;.
120ae 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 74  .  /* TODO: Is t
120af 68 69 73 20 6f 70 74 69 6d 61 6c 3f 20 57 68 79  his optimal? Why
120b0 20 69 73 20 74 68 65 20 64 62 20 73 69 7a 65 20   is the db size 
120b1 69 6e 76 61 6c 69 64 61 74 65 64 20 68 65 72 65  invalidated here
120b2 20 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20   .  ** when the 
120b3 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
120b4 20 6e 6f 74 20 75 6e 6c 6f 63 6b 65 64 3f 20 2a   not unlocked? *
120b5 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4f 72  /.  pPager->dbOr
120b6 69 67 53 69 7a 65 20 3d 20 30 3b 0a 20 20 73 71  igSize = 0;.  sq
120b7 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e 63  lite3PcacheTrunc
120b8 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61  ate(pPager->pPCa
120b9 63 68 65 2c 20 70 50 61 67 65 72 2d 3e 64 62 53  che, pPager->dbS
120ba 69 7a 65 29 3b 0a 20 20 69 66 28 20 21 4d 45 4d  ize);.  if( !MEM
120bb 44 42 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  DB ){.    pPager
120bc 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20  ->dbSizeValid = 
120bd 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  0;.  }..  return
120be 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f   (rc==SQLITE_OK?
120bf 72 63 32 3a 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  rc2:rc);.}../*.*
120c0 2a 20 50 61 72 61 6d 65 74 65 72 20 61 44 61 74  * Parameter aDat
120c1 61 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20  a must point to 
120c2 61 20 62 75 66 66 65 72 20 6f 66 20 70 50 61 67  a buffer of pPag
120c3 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74  er->pageSize byt
120c4 65 73 0a 2a 2a 20 6f 66 20 64 61 74 61 2e 20 43  es.** of data. C
120c5 6f 6d 70 75 74 65 20 61 6e 64 20 72 65 74 75 72  ompute and retur
120c6 6e 20 61 20 63 68 65 63 6b 73 75 6d 20 62 61 73  n a checksum bas
120c7 65 64 20 6f 6e 74 20 74 68 65 20 63 6f 6e 74 65  ed ont the conte
120c8 6e 74 73 20 6f 66 20 74 68 65 20 0a 2a 2a 20 70  nts of the .** p
120c9 61 67 65 20 6f 66 20 64 61 74 61 20 61 6e 64 20  age of data and 
120ca 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
120cb 65 20 6f 66 20 70 50 61 67 65 72 2d 3e 63 6b 73  e of pPager->cks
120cc 75 6d 49 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  umInit..**.** Th
120cd 69 73 20 69 73 20 6e 6f 74 20 61 20 72 65 61 6c  is is not a real
120ce 20 63 68 65 63 6b 73 75 6d 2e 20 49 74 20 69 73   checksum. It is
120cf 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65   really just the
120d0 20 73 75 6d 20 6f 66 20 74 68 65 20 0a 2a 2a 20   sum of the .** 
120d1 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c 20 76  random initial v
120d2 61 6c 75 65 20 28 70 50 61 67 65 72 2d 3e 63 6b  alue (pPager->ck
120d3 73 75 6d 49 6e 69 74 29 20 61 6e 64 20 65 76 65  sumInit) and eve
120d4 72 79 20 32 30 30 74 68 20 62 79 74 65 0a 2a 2a  ry 200th byte.**
120d5 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 61 74   of the page dat
120d6 61 2c 20 73 74 61 72 74 69 6e 67 20 77 69 74 68  a, starting with
120d7 20 62 79 74 65 20 6f 66 66 73 65 74 20 28 70 50   byte offset (pP
120d8 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 25 32  ager->pageSize%2
120d9 30 30 29 2e 0a 2a 2a 20 45 61 63 68 20 62 79 74  00)..** Each byt
120da 65 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64  e is interpreted
120db 20 61 73 20 61 6e 20 38 2d 62 69 74 20 75 6e 73   as an 8-bit uns
120dc 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a 2a  igned integer..*
120dd 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68  *.** Changing th
120de 65 20 66 6f 72 6d 75 6c 61 20 75 73 65 64 20 74  e formula used t
120df 6f 20 63 6f 6d 70 75 74 65 20 74 68 69 73 20 63  o compute this c
120e0 68 65 63 6b 73 75 6d 20 72 65 73 75 6c 74 73 20  hecksum results 
120e1 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f 6d 70 61  in an.** incompa
120e2 74 69 62 6c 65 20 6a 6f 75 72 6e 61 6c 20 66 69  tible journal fi
120e3 6c 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a  le format..**.**
120e4 20 49 66 20 6a 6f 75 72 6e 61 6c 20 63 6f 72 72   If journal corr
120e5 75 70 74 69 6f 6e 20 6f 63 63 75 72 73 20 64 75  uption occurs du
120e6 65 20 74 6f 20 61 20 70 6f 77 65 72 20 66 61 69  e to a power fai
120e7 6c 75 72 65 2c 20 74 68 65 20 6d 6f 73 74 20 6c  lure, the most l
120e8 69 6b 65 6c 79 20 0a 2a 2a 20 73 63 65 6e 61 72  ikely .** scenar
120e9 69 6f 20 69 73 20 74 68 61 74 20 6f 6e 65 20 65  io is that one e
120ea 6e 64 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20  nd or the other 
120eb 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 77 69  of the record wi
120ec 6c 6c 20 62 65 20 63 68 61 6e 67 65 64 2e 20 0a  ll be changed. .
120ed 2a 2a 20 49 74 20 69 73 20 6d 75 63 68 20 6c 65  ** It is much le
120ee 73 73 20 6c 69 6b 65 6c 79 20 74 68 61 74 20 74  ss likely that t
120ef 68 65 20 74 77 6f 20 65 6e 64 73 20 6f 66 20 74  he two ends of t
120f0 68 65 20 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72  he journal recor
120f1 64 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 63 6f 72  d will be.** cor
120f2 72 65 63 74 20 61 6e 64 20 74 68 65 20 6d 69 64  rect and the mid
120f3 64 6c 65 20 62 65 20 63 6f 72 72 75 70 74 2e 20  dle be corrupt. 
120f4 20 54 68 75 73 2c 20 74 68 69 73 20 22 63 68 65   Thus, this "che
120f5 63 6b 73 75 6d 22 20 73 63 68 65 6d 65 2c 0a 2a  cksum" scheme,.*
120f6 2a 20 74 68 6f 75 67 68 20 66 61 73 74 20 61 6e  * though fast an
120f7 64 20 73 69 6d 70 6c 65 2c 20 63 61 74 63 68 65  d simple, catche
120f8 73 20 74 68 65 20 6d 6f 73 74 6c 79 20 6c 69 6b  s the mostly lik
120f9 65 6c 79 20 6b 69 6e 64 20 6f 66 20 63 6f 72 72  ely kind of corr
120fa 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  uption..*/.stati
120fb 63 20 75 33 32 20 70 61 67 65 72 5f 63 6b 73 75  c u32 pager_cksu
120fc 6d 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  m(Pager *pPager,
120fd 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61   const u8 *aData
120fe 29 7b 0a 20 20 75 33 32 20 63 6b 73 75 6d 20 3d  ){.  u32 cksum =
120ff 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e   pPager->cksumIn
12100 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  it;         /* C
12101 68 65 63 6b 73 75 6d 20 76 61 6c 75 65 20 74 6f  hecksum value to
12102 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74   return */.  int
12103 20 69 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67   i = pPager->pag
12104 65 53 69 7a 65 2d 32 30 30 3b 20 20 20 20 20 20  eSize-200;      
12105 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
12106 74 65 72 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  ter */.  while( 
12107 69 3e 30 20 29 7b 0a 20 20 20 20 63 6b 73 75 6d  i>0 ){.    cksum
12108 20 2b 3d 20 61 44 61 74 61 5b 69 5d 3b 0a 20 20   += aData[i];.  
12109 20 20 69 20 2d 3d 20 32 30 30 3b 0a 20 20 7d 0a    i -= 200;.  }.
1210a 20 20 72 65 74 75 72 6e 20 63 6b 73 75 6d 3b 0a    return cksum;.
1210b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20  }../*.** Read a 
1210c 73 69 6e 67 6c 65 20 70 61 67 65 20 66 72 6f 6d  single page from
1210d 20 65 69 74 68 65 72 20 74 68 65 20 6a 6f 75 72   either the jour
1210e 6e 61 6c 20 66 69 6c 65 20 28 69 66 20 69 73 4d  nal file (if isM
1210f 61 69 6e 4a 72 6e 6c 3d 3d 31 29 20 6f 72 0a 2a  ainJrnl==1) or.*
12110 2a 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a  * from the sub-j
12111 6f 75 72 6e 61 6c 20 28 69 66 20 69 73 4d 61 69  ournal (if isMai
12112 6e 4a 72 6e 6c 3d 3d 30 29 20 61 6e 64 20 70 6c  nJrnl==0) and pl
12113 61 79 62 61 63 6b 20 74 68 61 74 20 70 61 67 65  ayback that page
12114 2e 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 62 65  ..** The page be
12115 67 69 6e 73 20 61 74 20 6f 66 66 73 65 74 20 2a  gins at offset *
12116 70 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65  pOffset into the
12117 20 66 69 6c 65 2e 20 54 68 65 20 2a 70 4f 66 66   file. The *pOff
12118 73 65 74 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20  set.** value is 
12119 69 6e 63 72 65 61 73 65 64 20 74 6f 20 74 68 65  increased to the
1211a 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65   start of the ne
1211b 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6a  xt page in the j
1211c 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68  ournal..**.** Th
1211d 65 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 66 6c 61  e isMainJrnl fla
1211e 67 20 69 73 20 74 72 75 65 20 69 66 20 74 68 69  g is true if thi
1211f 73 20 69 73 20 74 68 65 20 6d 61 69 6e 20 72 6f  s is the main ro
12120 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 61  llback journal a
12121 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20  nd.** false for 
12122 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  the statement jo
12123 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61 69 6e  urnal.  The main
12124 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
12125 6c 20 75 73 65 73 0a 2a 2a 20 63 68 65 63 6b 73  l uses.** checks
12126 75 6d 73 20 2d 20 74 68 65 20 73 74 61 74 65 6d  ums - the statem
12127 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 64 6f 65 73  ent journal does
12128 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74   not..**.** If t
12129 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  he page number o
1212a 66 20 74 68 65 20 70 61 67 65 20 72 65 63 6f 72  f the page recor
1212b 64 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  d read from the 
1212c 28 73 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69  (sub-)journal fi
1212d 6c 65 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72  le.** is greater
1212e 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e   than the curren
1212f 74 20 76 61 6c 75 65 20 6f 66 20 50 61 67 65 72  t value of Pager
12130 2e 64 62 53 69 7a 65 2c 20 74 68 65 6e 20 70 6c  .dbSize, then pl
12131 61 79 62 61 63 6b 20 69 73 0a 2a 2a 20 73 6b 69  ayback is.** ski
12132 70 70 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f  pped and SQLITE_
12133 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  OK is returned..
12134 2a 2a 0a 2a 2a 20 49 66 20 70 44 6f 6e 65 20 69  **.** If pDone i
12135 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
12136 20 69 74 20 69 73 20 61 20 72 65 63 6f 72 64 20   it is a record 
12137 6f 66 20 70 61 67 65 73 20 74 68 61 74 20 68 61  of pages that ha
12138 76 65 20 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65  ve already.** be
12139 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 2e 20  en played back. 
1213a 20 49 66 20 74 68 65 20 70 61 67 65 20 61 74 20   If the page at 
1213b 2a 70 4f 66 66 73 65 74 20 68 61 73 20 61 6c 72  *pOffset has alr
1213c 65 61 64 79 20 62 65 65 6e 20 70 6c 61 79 65 64  eady been played
1213d 20 62 61 63 6b 0a 2a 2a 20 28 69 66 20 74 68 65   back.** (if the
1213e 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70   corresponding p
1213f 44 6f 6e 65 20 62 69 74 20 69 73 20 73 65 74 29  Done bit is set)
12140 20 74 68 65 6e 20 73 6b 69 70 20 74 68 65 20 70   then skip the p
12141 6c 61 79 62 61 63 6b 2e 0a 2a 2a 20 4d 61 6b 65  layback..** Make
12142 20 73 75 72 65 20 74 68 65 20 70 44 6f 6e 65 20   sure the pDone 
12143 62 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  bit correspondin
12144 67 20 74 6f 20 74 68 65 20 2a 70 4f 66 66 73 65  g to the *pOffse
12145 74 20 70 61 67 65 20 69 73 20 73 65 74 0a 2a 2a  t page is set.**
12146 20 70 72 69 6f 72 20 74 6f 20 72 65 74 75 72 6e   prior to return
12147 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ing..**.** If th
12148 65 20 70 61 67 65 20 72 65 63 6f 72 64 20 69 73  e page record is
12149 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65   successfully re
1214a 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62  ad from the (sub
1214b 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a  -)journal file.*
1214c 2a 20 61 6e 64 20 70 6c 61 79 65 64 20 62 61 63  * and played bac
1214d 6b 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f  k, then SQLITE_O
1214e 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  K is returned. I
1214f 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63  f an IO error oc
12150 63 75 72 73 0a 2a 2a 20 77 68 69 6c 65 20 72 65  curs.** while re
12151 61 64 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  ading the record
12152 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29   from the (sub-)
12153 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72 20  journal file or 
12154 77 68 69 6c 65 20 77 72 69 74 69 6e 67 0a 2a 2a  while writing.**
12155 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
12156 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20   file, then the 
12157 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  IO error code is
12158 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 64 61   returned. If da
12159 74 61 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73  ta.** is success
1215a 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f 6d 20  fully read from 
1215b 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61  the (sub-)journa
1215c 6c 20 66 69 6c 65 20 62 75 74 20 61 70 70 65 61  l file but appea
1215d 72 73 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 72 72  rs to be.** corr
1215e 75 70 74 65 64 2c 20 53 51 4c 49 54 45 5f 44 4f  upted, SQLITE_DO
1215f 4e 45 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  NE is returned. 
12160 44 61 74 61 20 69 73 20 63 6f 6e 73 69 64 65 72  Data is consider
12161 65 64 20 63 6f 72 72 75 70 74 65 64 20 69 6e 0a  ed corrupted in.
12162 2a 2a 20 74 77 6f 20 63 69 72 63 75 6d 73 74 61  ** two circumsta
12163 6e 63 65 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 2a  nces:.** .**   *
12164 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 70   If the record p
12165 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 69 6c  age-number is il
12166 6c 65 67 61 6c 20 28 30 20 6f 72 20 50 41 47 45  legal (0 or PAGE
12167 52 5f 4d 4a 5f 50 47 4e 4f 29 2c 20 6f 72 0a 2a  R_MJ_PGNO), or.*
12168 2a 20 20 20 2a 20 49 66 20 74 68 65 20 72 65 63  *   * If the rec
12169 6f 72 64 20 69 73 20 62 65 69 6e 67 20 72 6f 6c  ord is being rol
1216a 6c 65 64 20 62 61 63 6b 20 66 72 6f 6d 20 74 68  led back from th
1216b 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66  e main journal f
1216c 69 6c 65 0a 2a 2a 20 20 20 20 20 61 6e 64 20 74  ile.**     and t
1216d 68 65 20 63 68 65 63 6b 73 75 6d 20 66 69 65 6c  he checksum fiel
1216e 64 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  d does not match
1216f 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f 6e 74   the record cont
12170 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 65 69 74 68  ent..**.** Neith
12171 65 72 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20  er of these two 
12172 73 63 65 6e 61 72 69 6f 73 20 61 72 65 20 70 6f  scenarios are po
12173 73 73 69 62 6c 65 20 64 75 72 69 6e 67 20 61 20  ssible during a 
12174 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62 61  savepoint rollba
12175 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69  ck..**.** If thi
12176 73 20 69 73 20 61 20 73 61 76 65 70 6f 69 6e 74  s is a savepoint
12177 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20   rollback, then 
12178 6d 65 6d 6f 72 79 20 6d 61 79 20 68 61 76 65 20  memory may have 
12179 74 6f 20 62 65 20 64 79 6e 61 6d 69 63 61 6c 6c  to be dynamicall
1217a 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 62  y.** allocated b
1217b 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  y this function.
1217c 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
1217d 63 61 73 65 20 61 6e 64 20 61 6e 20 61 6c 6c 6f  case and an allo
1217e 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 0a 2a 2a  cation fails,.**
1217f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73   SQLITE_NOMEM is
12180 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
12181 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 70  atic int pager_p
12182 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65  layback_one_page
12183 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  (.  Pager *pPage
12184 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
12185 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 62    /* The pager b
12186 65 69 6e 67 20 70 6c 61 79 65 64 20 62 61 63 6b  eing played back
12187 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 61 69 6e   */.  int isMain
12188 4a 72 6e 6c 2c 20 20 20 20 20 20 20 20 20 20 20  Jrnl,           
12189 20 20 20 20 2f 2a 20 31 20 2d 3e 20 6d 61 69 6e      /* 1 -> main
1218a 20 6a 6f 75 72 6e 61 6c 2e 20 30 20 2d 3e 20 73   journal. 0 -> s
1218b 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20  ub-journal. */. 
1218c 20 69 6e 74 20 69 73 55 6e 73 79 6e 63 2c 20 20   int isUnsync,  
1218d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1218e 2a 20 54 72 75 65 20 69 66 20 72 65 61 64 69 6e  * True if readin
1218f 67 20 66 72 6f 6d 20 75 6e 73 79 6e 63 65 64 20  g from unsynced 
12190 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  main journal */.
12191 20 20 69 36 34 20 2a 70 4f 66 66 73 65 74 2c 20    i64 *pOffset, 
12192 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12193 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 72 65 63  /* Offset of rec
12194 6f 72 64 20 74 6f 20 70 6c 61 79 62 61 63 6b 20  ord to playback 
12195 2a 2f 0a 20 20 69 6e 74 20 69 73 53 61 76 65 70  */.  int isSavep
12196 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nt,             
12197 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61     /* True for a
12198 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62   savepoint rollb
12199 61 63 6b 20 2a 2f 0a 20 20 42 69 74 76 65 63 20  ack */.  Bitvec 
1219a 2a 70 44 6f 6e 65 20 20 20 20 20 20 20 20 20 20  *pDone          
1219b 20 20 20 20 20 20 20 2f 2a 20 42 69 74 76 65 63         /* Bitvec
1219c 20 6f 66 20 70 61 67 65 73 20 61 6c 72 65 61 64   of pages alread
1219d 79 20 70 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f  y played back */
1219e 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
1219f 50 67 48 64 72 20 2a 70 50 67 3b 20 20 20 20 20  PgHdr *pPg;     
121a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
121a1 20 41 6e 20 65 78 69 73 74 69 6e 67 20 70 61 67   An existing pag
121a2 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a  e in the cache *
121a3 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20  /.  Pgno pgno;  
121a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
121a5 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 6e 75    /* The page nu
121a6 6d 62 65 72 20 6f 66 20 61 20 70 61 67 65 20 69  mber of a page i
121a7 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75  n journal */.  u
121a8 33 32 20 63 6b 73 75 6d 3b 20 20 20 20 20 20 20  32 cksum;       
121a9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
121aa 43 68 65 63 6b 73 75 6d 20 75 73 65 64 20 66 6f  Checksum used fo
121ab 72 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  r sanity checkin
121ac 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 44 61  g */.  char *aDa
121ad 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
121ae 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72       /* Temporar
121af 79 20 73 74 6f 72 61 67 65 20 66 6f 72 20 74 68  y storage for th
121b0 65 20 70 61 67 65 20 2a 2f 0a 20 20 73 71 6c 69  e page */.  sqli
121b1 74 65 33 5f 66 69 6c 65 20 2a 6a 66 64 3b 20 20  te3_file *jfd;  
121b2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
121b3 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
121b4 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c   for the journal
121b5 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65   file */..  asse
121b6 72 74 28 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 26  rt( (isMainJrnl&
121b7 7e 31 29 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f  ~1)==0 );      /
121b8 2a 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 69 73 20  * isMainJrnl is 
121b9 30 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73 73 65  0 or 1 */.  asse
121ba 72 74 28 20 28 69 73 53 61 76 65 70 6e 74 26 7e  rt( (isSavepnt&~
121bb 31 29 3d 3d 30 20 29 3b 20 20 20 20 20 20 20 2f  1)==0 );       /
121bc 2a 20 69 73 53 61 76 65 70 6e 74 20 69 73 20 30  * isSavepnt is 0
121bd 20 6f 72 20 31 20 2a 2f 0a 20 20 61 73 73 65 72   or 1 */.  asser
121be 74 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 7c 7c  t( isMainJrnl ||
121bf 20 70 44 6f 6e 65 20 29 3b 20 20 20 20 20 2f 2a   pDone );     /*
121c0 20 70 44 6f 6e 65 20 61 6c 77 61 79 73 20 75 73   pDone always us
121c1 65 64 20 6f 6e 20 73 75 62 2d 6a 6f 75 72 6e 61  ed on sub-journa
121c2 6c 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  ls */.  assert( 
121c3 69 73 53 61 76 65 70 6e 74 20 7c 7c 20 70 44 6f  isSavepnt || pDo
121c4 6e 65 3d 3d 30 20 29 3b 20 20 20 2f 2a 20 70 44  ne==0 );   /* pD
121c5 6f 6e 65 20 6e 65 76 65 72 20 75 73 65 64 20 6f  one never used o
121c6 6e 20 6e 6f 6e 2d 73 61 76 65 70 6f 69 6e 74 20  n non-savepoint 
121c7 2a 2f 0a 0a 20 20 61 44 61 74 61 20 3d 20 70 50  */..  aData = pP
121c8 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b  ager->pTmpSpace;
121c9 0a 20 20 61 73 73 65 72 74 28 20 61 44 61 74 61  .  assert( aData
121ca 20 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54   );         /* T
121cb 65 6d 70 20 73 74 6f 72 61 67 65 20 6d 75 73 74  emp storage must
121cc 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
121cd 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  en allocated */.
121ce 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70  .  /* Read the p
121cf 61 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70  age number and p
121d0 61 67 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68  age data from th
121d1 65 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62  e journal or sub
121d2 2d 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69  -journal.  ** fi
121d3 6c 65 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72  le. Return an er
121d4 72 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20  ror code to the 
121d5 63 61 6c 6c 65 72 20 69 66 20 61 6e 20 49 4f 20  caller if an IO 
121d6 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20  error occurs..  
121d7 2a 2f 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61 69  */.  jfd = isMai
121d8 6e 4a 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e  nJrnl ? pPager->
121d9 6a 66 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a  jfd : pPager->sj
121da 66 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 33  fd;.  rc = read3
121db 32 62 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66  2bits(jfd, *pOff
121dc 73 65 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69  set, &pgno);.  i
121dd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
121de 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
121df 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
121e0 61 64 28 6a 66 64 2c 20 28 75 38 2a 29 61 44 61  ad(jfd, (u8*)aDa
121e1 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  ta, pPager->page
121e2 53 69 7a 65 2c 20 28 2a 70 4f 66 66 73 65 74 29  Size, (*pOffset)
121e3 2b 34 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  +4);.  if( rc!=S
121e4 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
121e5 6e 20 72 63 3b 0a 20 20 2a 70 4f 66 66 73 65 74  n rc;.  *pOffset
121e6 20 2b 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65   += pPager->page
121e7 53 69 7a 65 20 2b 20 34 20 2b 20 69 73 4d 61 69  Size + 4 + isMai
121e8 6e 4a 72 6e 6c 2a 34 3b 0a 0a 20 20 2f 2a 20 53  nJrnl*4;..  /* S
121e9 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f  anity checking o
121ea 6e 20 74 68 65 20 70 61 67 65 2e 20 20 54 68 69  n the page.  Thi
121eb 73 20 69 73 20 6d 6f 72 65 20 69 6d 70 6f 72 74  s is more import
121ec 61 6e 74 20 74 68 61 74 20 49 20 6f 72 69 67 69  ant that I origi
121ed 6e 61 6c 6c 79 0a 20 20 2a 2a 20 74 68 6f 75 67  nally.  ** thoug
121ee 68 74 2e 20 20 49 66 20 61 20 70 6f 77 65 72 20  ht.  If a power 
121ef 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77  failure occurs w
121f0 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  hile the journal
121f1 20 69 73 20 62 65 69 6e 67 20 77 72 69 74 74 65   is being writte
121f2 6e 2c 0a 20 20 2a 2a 20 69 74 20 63 6f 75 6c 64  n,.  ** it could
121f3 20 63 61 75 73 65 20 69 6e 76 61 6c 69 64 20 64   cause invalid d
121f4 61 74 61 20 74 6f 20 62 65 20 77 72 69 74 74 65  ata to be writte
121f5 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  n into the journ
121f6 61 6c 2e 20 20 57 65 20 6e 65 65 64 20 74 6f 0a  al.  We need to.
121f7 20 20 2a 2a 20 64 65 74 65 63 74 20 74 68 69 73    ** detect this
121f8 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 28 77   invalid data (w
121f9 69 74 68 20 68 69 67 68 20 70 72 6f 62 61 62 69  ith high probabi
121fa 6c 69 74 79 29 20 61 6e 64 20 69 67 6e 6f 72 65  lity) and ignore
121fb 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20   it..  */.  if( 
121fc 70 67 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d  pgno==0 || pgno=
121fd 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70  =PAGER_MJ_PGNO(p
121fe 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 61 73  Pager) ){.    as
121ff 73 65 72 74 28 20 21 69 73 53 61 76 65 70 6e 74  sert( !isSavepnt
12200 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   );.    return S
12201 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a  QLITE_DONE;.  }.
12202 20 20 69 66 28 20 70 67 6e 6f 3e 28 50 67 6e 6f    if( pgno>(Pgno
12203 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20  )pPager->dbSize 
12204 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76 65 63  || sqlite3Bitvec
12205 54 65 73 74 28 70 44 6f 6e 65 2c 20 70 67 6e 6f  Test(pDone, pgno
12206 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
12207 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
12208 20 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20   if( isMainJrnl 
12209 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64  ){.    rc = read
1220a 33 32 62 69 74 73 28 6a 66 64 2c 20 28 2a 70 4f  32bits(jfd, (*pO
1220b 66 66 73 65 74 29 2d 34 2c 20 26 63 6b 73 75 6d  ffset)-4, &cksum
1220c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
1220d 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 69  return rc;.    i
1220e 66 28 20 21 69 73 53 61 76 65 70 6e 74 20 26 26  f( !isSavepnt &&
1220f 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61   pager_cksum(pPa
12210 67 65 72 2c 20 28 75 38 2a 29 61 44 61 74 61 29  ger, (u8*)aData)
12211 21 3d 63 6b 73 75 6d 20 29 7b 0a 20 20 20 20 20  !=cksum ){.     
12212 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
12213 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ONE;.    }.  }..
12214 20 20 69 66 28 20 70 44 6f 6e 65 20 26 26 20 28    if( pDone && (
12215 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  rc = sqlite3Bitv
12216 65 63 53 65 74 28 70 44 6f 6e 65 2c 20 70 67 6e  ecSet(pDone, pgn
12217 6f 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  o))!=SQLITE_OK )
12218 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
12219 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
1221a 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
1221b 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 7c 7c  AGER_RESERVED ||
1221c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
1221d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20  PAGER_EXCLUSIVE 
1221e 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
1221f 70 61 67 65 72 20 69 73 20 69 6e 20 52 45 53 45  pager is in RESE
12220 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e  RVED state, then
12221 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   there must be a
12222 20 63 6f 70 79 20 6f 66 20 74 68 69 73 0a 20 20   copy of this.  
12223 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 70  ** page in the p
12224 61 67 65 72 20 63 61 63 68 65 2e 20 49 6e 20 74  ager cache. In t
12225 68 69 73 20 63 61 73 65 20 6a 75 73 74 20 75 70  his case just up
12226 64 61 74 65 20 74 68 65 20 70 61 67 65 72 20 63  date the pager c
12227 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20 74  ache,.  ** not t
12228 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12229 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 6c 65  . The page is le
1222a 66 74 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20  ft marked dirty 
1222b 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20  in this case..  
1222c 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 63 65 70  **.  ** An excep
1222d 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 62 6f 76  tion to the abov
1222e 65 20 72 75 6c 65 3a 20 49 66 20 74 68 65 20 64  e rule: If the d
1222f 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 6e 6f  atabase is in no
12230 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a 20  -sync mode.  ** 
12231 61 6e 64 20 61 20 70 61 67 65 20 69 73 20 6d 6f  and a page is mo
12232 76 65 64 20 64 75 72 69 6e 67 20 61 6e 20 69 6e  ved during an in
12233 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
12234 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d   then the page m
12235 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 69  ay.  ** not be i
12236 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68  n the pager cach
12237 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 61 20 6d  e. Later: if a m
12238 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 65 72  alloc() or IO er
12239 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20  ror occurs.  ** 
1223a 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 70 61 67  during a Movepag
1223b 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 6e 20 74  e() call, then t
1223c 68 65 20 70 61 67 65 20 6d 61 79 20 6e 6f 74 20  he page may not 
1223d 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 0a  be in the cache.
1223e 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 53 6f 20    ** either. So 
1223f 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65  the condition de
12240 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 20 61  scribed in the a
12241 62 6f 76 65 20 70 61 72 61 67 72 61 70 68 20 69  bove paragraph i
12242 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 73 65 72  s not.  ** asser
12243 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20  t()able..  **.  
12244 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c 55 53 49  ** If in EXCLUSI
12245 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e 20 77  VE state, then w
12246 65 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67  e update the pag
12247 65 72 20 63 61 63 68 65 20 69 66 20 69 74 20 65  er cache if it e
12248 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e 64 20 74  xists.  ** and t
12249 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e 20 54 68  he main file. Th
1224a 65 20 70 61 67 65 20 69 73 20 74 68 65 6e 20 6d  e page is then m
1224b 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 74 79 2e  arked not dirty.
1224c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b 65  .  **.  ** Ticke
1224d 74 20 23 31 31 37 31 3a 20 20 54 68 65 20 73 74  t #1171:  The st
1224e 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
1224f 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 70 61  might contain pa
12250 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 61 74 20  ge content that 
12251 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e  is.  ** differen
12252 74 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20  t from the page 
12253 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 65 20 73  content at the s
12254 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e  tart of the tran
12255 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68  saction..  ** Th
12256 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20 61  is occurs when a
12257 20 70 61 67 65 20 69 73 20 63 68 61 6e 67 65 64   page is changed
12258 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74   prior to the st
12259 61 72 74 20 6f 66 20 61 20 73 74 61 74 65 6d 65  art of a stateme
1225a 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 63 68 61  nt.  ** then cha
1225b 6e 67 65 64 20 61 67 61 69 6e 20 77 69 74 68 69  nged again withi
1225c 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e  n the statement.
1225d 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62    When rolling b
1225e 61 63 6b 20 73 75 63 68 20 61 0a 20 20 2a 2a 20  ack such a.  ** 
1225f 73 74 61 74 65 6d 65 6e 74 20 77 65 20 6d 75 73  statement we mus
12260 74 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74  t not write to t
12261 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61  he original data
12262 62 61 73 65 20 75 6e 6c 65 73 73 20 77 65 20 6b  base unless we k
12263 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 63 65 72  now.  ** for cer
12264 74 61 69 6e 20 74 68 61 74 20 6f 72 69 67 69 6e  tain that origin
12265 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 73  al page contents
12266 20 61 72 65 20 73 79 6e 63 65 64 20 69 6e 74 6f   are synced into
12267 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61   the main rollba
12268 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e  ck.  ** journal.
12269 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 70    Otherwise, a p
1226a 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 68 74 20  ower loss might 
1226b 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 64 20 64  leave modified d
1226c 61 74 61 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  ata in the.  ** 
1226d 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69  database file wi
1226e 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 79 20 69  thout an entry i
1226f 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  n the rollback j
12270 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 61 6e 0a  ournal that can.
12271 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 74 68 65    ** restore the
12272 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73   database to its
12273 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e 20   original form. 
12274 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20   Two conditions 
12275 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6d 65 74  must be.  ** met
12276 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20   before writing 
12277 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
12278 66 69 6c 65 73 2e 20 28 31 29 20 74 68 65 20 64  files. (1) the d
12279 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 0a  atabase must be.
1227a 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 32    ** locked.  (2
1227b 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74  ) we know that t
1227c 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65  he original page
1227d 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c 6c   content is full
1227e 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a 20 69 6e  y synced.  ** in
1227f 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
12280 6c 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65  l either because
12281 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
12282 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65 6c 73   in cache or els
12283 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20  e.  ** the page 
12284 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e 65 65  is marked as nee
12285 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a 20  dSync==0..  **. 
12286 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 34 3a 20   ** 2008-04-14: 
12287 20 57 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67   When attempting
12288 20 74 6f 20 76 61 63 75 75 6d 20 61 20 63 6f 72   to vacuum a cor
12289 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
1228a 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20 70  le, it.  ** is p
1228b 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 69 6c 20  ossible to fail 
1228c 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 61  a statement on a
1228d 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 64   database that d
1228e 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 78 69 73  oes not yet exis
1228f 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 61  t..  ** Do not a
12290 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20  ttempt to write 
12291 69 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  if database file
12292 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e 20   has never been 
12293 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70  opened..  */.  p
12294 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75  Pg = pager_looku
12295 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b  p(pPager, pgno);
12296 0a 20 20 61 73 73 65 72 74 28 20 70 50 67 20 7c  .  assert( pPg |
12297 7c 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 50 41  | !MEMDB );.  PA
12298 47 45 52 54 52 41 43 45 28 28 22 50 4c 41 59 42  GERTRACE(("PLAYB
12299 41 43 4b 20 25 64 20 70 61 67 65 20 25 64 20 68  ACK %d page %d h
1229a 61 73 68 28 25 30 38 78 29 20 25 73 5c 6e 22 2c  ash(%08x) %s\n",
1229b 0a 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45  .           PAGE
1229c 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e  RID(pPager), pgn
1229d 6f 2c 20 70 61 67 65 72 5f 64 61 74 61 68 61 73  o, pager_datahas
1229e 68 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  h(pPager->pageSi
1229f 7a 65 2c 20 28 75 38 2a 29 61 44 61 74 61 29 2c  ze, (u8*)aData),
122a0 0a 20 20 20 20 20 20 20 20 20 20 20 28 69 73 4d  .           (isM
122a1 61 69 6e 4a 72 6e 6c 3f 22 6d 61 69 6e 2d 6a 6f  ainJrnl?"main-jo
122a2 75 72 6e 61 6c 22 3a 22 73 75 62 2d 6a 6f 75 72  urnal":"sub-jour
122a3 6e 61 6c 22 29 0a 20 20 29 29 3b 0a 20 20 69 66  nal").  ));.  if
122a4 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65  ( (pPager->state
122a5 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  >=PAGER_EXCLUSIV
122a6 45 29 0a 20 20 20 26 26 20 28 70 50 67 3d 3d 30  E).   && (pPg==0
122a7 20 7c 7c 20 30 3d 3d 28 70 50 67 2d 3e 66 6c 61   || 0==(pPg->fla
122a8 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
122a9 4e 43 29 29 0a 20 20 20 26 26 20 69 73 4f 70 65  NC)).   && isOpe
122aa 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 0a 20 20  n(pPager->fd).  
122ab 20 26 26 20 21 69 73 55 6e 73 79 6e 63 0a 20 20   && !isUnsync.  
122ac 29 7b 0a 20 20 20 20 69 36 34 20 6f 66 73 74 20  ){.    i64 ofst 
122ad 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29  = (pgno-1)*(i64)
122ae 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
122af 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
122b0 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
122b1 2d 3e 66 64 2c 20 28 75 38 2a 29 61 44 61 74 61  ->fd, (u8*)aData
122b2 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
122b3 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20 20 20 69  ze, ofst);.    i
122b4 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e  f( pgno>pPager->
122b5 64 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20  dbFileSize ){.  
122b6 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69      pPager->dbFi
122b7 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f 3b 0a 20  leSize = pgno;. 
122b8 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61     }.    if( pPa
122b9 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 29 7b 0a  ger->pBackup ){.
122ba 20 20 20 20 20 20 43 4f 44 45 43 31 28 70 50 61        CODEC1(pPa
122bb 67 65 72 2c 20 61 44 61 74 61 2c 20 70 67 6e 6f  ger, aData, pgno
122bc 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54 45 5f 4e  , 3, rc=SQLITE_N
122bd 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 20 73 71 6c  OMEM);.      sql
122be 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74 65  ite3BackupUpdate
122bf 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70  (pPager->pBackup
122c0 2c 20 70 67 6e 6f 2c 20 28 75 38 2a 29 61 44 61  , pgno, (u8*)aDa
122c1 74 61 29 3b 0a 20 20 20 20 20 20 43 4f 44 45 43  ta);.      CODEC
122c2 32 28 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c  2(pPager, aData,
122c3 20 70 67 6e 6f 2c 20 37 2c 20 72 63 3d 53 51 4c   pgno, 7, rc=SQL
122c4 49 54 45 5f 4e 4f 4d 45 4d 2c 20 61 44 61 74 61  ITE_NOMEM, aData
122c5 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
122c6 20 69 66 28 20 21 69 73 4d 61 69 6e 4a 72 6e 6c   if( !isMainJrnl
122c7 20 26 26 20 70 50 67 3d 3d 30 20 29 7b 0a 20 20   && pPg==0 ){.  
122c8 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
122c9 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20  a rollback of a 
122ca 73 61 76 65 70 6f 69 6e 74 20 61 6e 64 20 64 61  savepoint and da
122cb 74 61 20 77 61 73 20 6e 6f 74 20 77 72 69 74 74  ta was not writt
122cc 65 6e 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65  en to.    ** the
122cd 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 68   database and th
122ce 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e  e page is not in
122cf 2d 6d 65 6d 6f 72 79 2c 20 74 68 65 72 65 20 69  -memory, there i
122d0 73 20 61 20 70 6f 74 65 6e 74 69 61 6c 0a 20 20  s a potential.  
122d1 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 2e 20 57 68    ** problem. Wh
122d2 65 6e 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  en the page is n
122d3 65 78 74 20 66 65 74 63 68 65 64 20 62 79 20 74  ext fetched by t
122d4 68 65 20 62 2d 74 72 65 65 20 6c 61 79 65 72 2c  he b-tree layer,
122d5 20 69 74 20 0a 20 20 20 20 2a 2a 20 77 69 6c 6c   it .    ** will
122d6 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68   be read from th
122d7 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
122d8 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20 6d 61   which may or ma
122d9 79 20 6e 6f 74 20 62 65 20 0a 20 20 20 20 2a 2a  y not be .    **
122da 20 63 75 72 72 65 6e 74 2e 20 0a 20 20 20 20 2a   current. .    *
122db 2a 0a 20 20 20 20 2a 2a 20 54 68 65 72 65 20 61  *.    ** There a
122dc 72 65 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64  re a couple of d
122dd 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 74 68  ifferent ways th
122de 69 73 20 63 61 6e 20 68 61 70 70 65 6e 2e 20 41  is can happen. A
122df 6c 6c 20 61 72 65 20 71 75 69 74 65 0a 20 20 20  ll are quite.   
122e0 20 2a 2a 20 6f 62 73 63 75 72 65 2e 20 57 68 65   ** obscure. Whe
122e1 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 79 6e  n running in syn
122e2 63 68 72 6f 6e 6f 75 73 20 6d 6f 64 65 2c 20 74  chronous mode, t
122e3 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70  his can only hap
122e4 70 65 6e 20 0a 20 20 20 20 2a 2a 20 69 66 20 74  pen .    ** if t
122e5 68 65 20 70 61 67 65 20 69 73 20 6f 6e 20 74 68  he page is on th
122e6 65 20 66 72 65 65 2d 6c 69 73 74 20 61 74 20 74  e free-list at t
122e7 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
122e8 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
122e9 6e 0a 20 20 20 20 2a 2a 20 70 6f 70 75 6c 61 74  n.    ** populat
122ea 65 64 2c 20 74 68 65 6e 20 6d 6f 76 65 64 20 75  ed, then moved u
122eb 73 69 6e 67 20 73 71 6c 69 74 65 33 50 61 67 65  sing sqlite3Page
122ec 72 4d 6f 76 65 70 61 67 65 28 29 2e 0a 20 20 20  rMovepage()..   
122ed 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73   **.    ** The s
122ee 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20 61 64  olution is to ad
122ef 64 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70  d an in-memory p
122f0 61 67 65 20 74 6f 20 74 68 65 20 63 61 63 68 65  age to the cache
122f1 20 63 6f 6e 74 61 69 6e 69 6e 67 0a 20 20 20 20   containing.    
122f2 2a 2a 20 74 68 65 20 64 61 74 61 20 6a 75 73 74  ** the data just
122f3 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 73   read from the s
122f4 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 4d 61 72 6b  ub-journal. Mark
122f5 20 74 68 65 20 70 61 67 65 20 61 73 20 64 69 72   the page as dir
122f6 74 79 20 0a 20 20 20 20 2a 2a 20 61 6e 64 20 69  ty .    ** and i
122f7 66 20 74 68 65 20 70 61 67 65 72 20 72 65 71 75  f the pager requ
122f8 69 72 65 73 20 61 20 6a 6f 75 72 6e 61 6c 2d 73  ires a journal-s
122f9 79 6e 63 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74  ync, then mark t
122fa 68 65 20 70 61 67 65 20 61 73 20 0a 20 20 20 20  he page as .    
122fb 2a 2a 20 72 65 71 75 69 72 69 6e 67 20 61 20 6a  ** requiring a j
122fc 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 62 65 66 6f  ournal-sync befo
122fd 72 65 20 69 74 20 69 73 20 77 72 69 74 74 65 6e  re it is written
122fe 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
122ff 65 72 74 28 20 69 73 53 61 76 65 70 6e 74 20 29  ert( isSavepnt )
12300 3b 0a 20 20 20 20 69 66 28 20 28 72 63 20 3d 20  ;.    if( (rc = 
12301 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75  sqlite3PagerAcqu
12302 69 72 65 28 70 50 61 67 65 72 2c 20 70 67 6e 6f  ire(pPager, pgno
12303 2c 20 26 70 50 67 2c 20 31 29 29 21 3d 53 51 4c  , &pPg, 1))!=SQL
12304 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12305 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
12306 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20  .    pPg->flags 
12307 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 52  &= ~PGHDR_NEED_R
12308 45 41 44 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  EAD;.    sqlite3
12309 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28  PcacheMakeDirty(
1230a 70 50 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  pPg);.  }.  if( 
1230b 70 50 67 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f  pPg ){.    /* No
1230c 20 70 61 67 65 20 73 68 6f 75 6c 64 20 65 76 65   page should eve
1230d 72 20 62 65 20 65 78 70 6c 69 63 69 74 6c 79 20  r be explicitly 
1230e 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68 61 74  rolled back that
1230f 20 69 73 20 69 6e 20 75 73 65 2c 20 65 78 63 65   is in use, exce
12310 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72 20 70 61  pt.    ** for pa
12311 67 65 20 31 20 77 68 69 63 68 20 69 73 20 68 65  ge 1 which is he
12312 6c 64 20 69 6e 20 75 73 65 20 69 6e 20 6f 72 64  ld in use in ord
12313 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 6c  er to keep the l
12314 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a  ock on the.    *
12315 2a 20 64 61 74 61 62 61 73 65 20 61 63 74 69 76  * database activ
12316 65 2e 20 48 6f 77 65 76 65 72 20 73 75 63 68 20  e. However such 
12317 61 20 70 61 67 65 20 6d 61 79 20 62 65 20 72 6f  a page may be ro
12318 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20 72  lled back as a r
12319 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 6f 66 20  esult.    ** of 
1231a 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65 72 72 6f  an internal erro
1231b 72 20 72 65 73 75 6c 74 69 6e 67 20 69 6e 20 61  r resulting in a
1231c 6e 20 61 75 74 6f 6d 61 74 69 63 20 63 61 6c 6c  n automatic call
1231d 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   to.    ** sqlit
1231e 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28  e3PagerRollback(
1231f 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 76 6f  )..    */.    vo
12320 69 64 20 2a 70 44 61 74 61 3b 0a 20 20 20 20 70  id *pData;.    p
12321 44 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74  Data = pPg->pDat
12322 61 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 44  a;.    memcpy(pD
12323 61 74 61 2c 20 28 75 38 2a 29 61 44 61 74 61 2c  ata, (u8*)aData,
12324 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
12325 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  e);.    pPager->
12326 78 52 65 69 6e 69 74 65 72 28 70 50 67 29 3b 0a  xReiniter(pPg);.
12327 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e 4a 72      if( isMainJr
12328 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65 70 6e  nl && (!isSavepn
12329 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c 3d 70  t || *pOffset<=p
1232a 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
1232b 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  r) ){.      /* I
1232c 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  f the contents o
1232d 66 20 74 68 69 73 20 70 61 67 65 20 77 65 72 65  f this page were
1232e 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64 20 66   just restored f
1232f 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a 20 20  rom the main .  
12330 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66      ** journal f
12331 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20 63 6f  ile, then its co
12332 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20 61 73  ntent must be as
12333 20 74 68 65 79 20 77 65 72 65 20 77 68 65 6e 20   they were when 
12334 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 72  the .      ** tr
12335 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 66 69  ansaction was fi
12336 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e 20 74  rst opened. In t
12337 68 69 73 20 63 61 73 65 20 77 65 20 63 61 6e 20  his case we can 
12338 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a 20 20  mark the page.  
12339 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61 6e 2c      ** as clean,
1233a 20 73 69 6e 63 65 20 74 68 65 72 65 20 77 69 6c   since there wil
1233b 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74 6f 20  l be no need to 
1233c 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f 20  write it out to 
1233d 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  the..      **.  
1233e 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20      ** There is 
1233f 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 20 74 6f  one exception to
12340 20 74 68 69 73 20 72 75 6c 65 2e 20 49 66 20 74   this rule. If t
12341 68 65 20 70 61 67 65 20 69 73 20 62 65 69 6e 67  he page is being
12342 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 20 2a 2a   rolled.      **
12343 20 62 61 63 6b 20 61 73 20 70 61 72 74 20 6f 66   back as part of
12344 20 61 20 73 61 76 65 70 6f 69 6e 74 20 28 6f 72   a savepoint (or
12345 20 73 74 61 74 65 6d 65 6e 74 29 20 72 6f 6c 6c   statement) roll
12346 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 0a 20 20  back from an .  
12347 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 65 64 20      ** unsynced 
12348 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d  portion of the m
12349 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ain journal file
1234a 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f 74  , then it is not
1234b 20 73 61 66 65 0a 20 20 20 20 20 20 2a 2a 20 74   safe.      ** t
1234c 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20  o mark the page 
1234d 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 73 20 69  as clean. This i
1234e 73 20 62 65 63 61 75 73 65 20 6d 61 72 6b 69 6e  s because markin
1234f 67 20 74 68 65 20 70 61 67 65 20 61 73 0a 20 20  g the page as.  
12350 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 77 69 6c      ** clean wil
12351 6c 20 63 6c 65 61 72 20 74 68 65 20 50 47 48 44  l clear the PGHD
12352 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67  R_NEED_SYNC flag
12353 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 65  . Since the page
12354 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72   is.      ** alr
12355 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f 75 72  eady in the jour
12356 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 6f 72 64  nal file (record
12357 65 64 20 69 6e 20 50 61 67 65 72 2e 70 49 6e 4a  ed in Pager.pInJ
12358 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 20 20 20  ournal) and.    
12359 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e    ** the PGHDR_N
1235a 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 69 73  EED_SYNC flag is
1235b 20 63 6c 65 61 72 65 64 2c 20 69 66 20 74 68 65   cleared, if the
1235c 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e   page is written
1235d 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 61 67 61   to.      ** aga
1235e 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73 20 74  in within this t
1235f 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74 20 77  ransaction, it w
12360 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 20 61 73  ill be marked as
12361 20 64 69 72 74 79 20 62 75 74 0a 20 20 20 20 20   dirty but.     
12362 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f 4e 45   ** the PGHDR_NE
12363 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 77 69 6c  ED_SYNC flag wil
12364 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e 20 49 74  l not be set. It
12365 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 6f 74 65   could then pote
12366 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a  ntially.      **
12367 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20   be written out 
12368 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
12369 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20 69 74  e file before it
1236a 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 20  s journal file. 
1236b 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20       ** segment 
1236c 69 73 20 73 79 6e 63 65 64 2e 20 49 66 20 61 20  is synced. If a 
1236d 63 72 61 73 68 20 6f 63 63 75 72 73 20 64 75 72  crash occurs dur
1236e 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 69 6e 67  ing or following
1236f 20 74 68 69 73 2c 0a 20 20 20 20 20 20 2a 2a 20   this,.      ** 
12370 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
12371 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 2e 0a 20  ion may ensue.. 
12372 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
12373 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43  lite3PcacheMakeC
12374 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20 20 7d  lean(pPg);.    }
12375 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
12376 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70  HECK_PAGES.    p
12377 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70  Pg->pageHash = p
12378 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
12379 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f  g);.#endif.    /
1237a 2a 20 49 66 20 74 68 69 73 20 77 61 73 20 70 61  * If this was pa
1237b 67 65 20 31 2c 20 74 68 65 6e 20 72 65 73 74 6f  ge 1, then resto
1237c 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  re the value of 
1237d 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73  Pager.dbFileVers
1237e 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73  ..    ** Do this
1237f 20 62 65 66 6f 72 65 20 61 6e 79 20 64 65 63 6f   before any deco
12380 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28  ding. */.    if(
12381 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20   pgno==1 ){.    
12382 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72    memcpy(&pPager
12383 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28  ->dbFileVers, &(
12384 28 75 38 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c  (u8*)pData)[24],
12385 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64  sizeof(pPager->d
12386 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20  bFileVers));.   
12387 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64   }..    /* Decod
12388 65 20 74 68 65 20 70 61 67 65 20 6a 75 73 74 20  e the page just 
12389 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a  read from disk *
1238a 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 70 50 61  /.    CODEC1(pPa
1238b 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d  ger, pData, pPg-
1238c 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c  >pgno, 3, rc=SQL
1238d 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20  ITE_NOMEM);.    
1238e 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c  sqlite3PcacheRel
1238f 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20  ease(pPg);.  }. 
12390 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
12391 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a  *.** Parameter z
12392 4d 61 73 74 65 72 20 69 73 20 74 68 65 20 6e 61  Master is the na
12393 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a  me of a master j
12394 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73  ournal file. A s
12395 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  ingle journal.**
12396 20 66 69 6c 65 20 74 68 61 74 20 72 65 66 65 72   file that refer
12397 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73 74 65  red to the maste
12398 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  r journal file h
12399 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c  as just been rol
1239a 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69  led back..** Thi
1239b 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
1239c 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69 62   if it is possib
1239d 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65  le to delete the
1239e 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1239f 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65  file,.** and doe
123a0 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a  s so if it is..*
123a1 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d  *.** Argument zM
123a2 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 20  aster may point 
123a3 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61  to Pager.pTmpSpa
123a4 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75 66 66  ce. So that buff
123a5 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76  er is not .** av
123a6 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20  ailable for use 
123a7 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63  within this func
123a8 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  tion..**.** When
123a9 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
123aa 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61 74 65  l file is create
123ab 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c 61 74  d, it is populat
123ac 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65  ed with the name
123ad 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20  s .** of all of 
123ae 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  its child journa
123af 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20 61 6e  ls, one after an
123b0 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74 65 64  other, formatted
123b1 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20 65 6e   as utf-8 .** en
123b2 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68 65 20  coded text. The 
123b3 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68 69 6c  end of each chil
123b4 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  d journal file i
123b5 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20 61 20  s marked with a 
123b6 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  .** nul-terminat
123b7 6f 72 20 62 79 74 65 20 28 30 78 30 30 29 2e 20  or byte (0x00). 
123b8 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72 65 20  i.e. the entire 
123b9 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 6d 61  contents of a ma
123ba 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  ster journal.** 
123bb 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61 6e 73  file for a trans
123bc 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69 6e 67  action involving
123bd 20 74 77 6f 20 64 61 74 61 62 61 73 65 73 20 6d   two databases m
123be 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a 20 20  ight be:.**.**  
123bf 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61 2e 64   "/home/bill/a.d
123c0 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f 68 6f  b-journal\x00/ho
123c1 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a 6f 75  me/bill/b.db-jou
123c2 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a 2a 20  rnal\x00".**.** 
123c3 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  A master journal
123c4 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79 20 62   file may only b
123c5 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65 20 61  e deleted once a
123c6 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c 64 20  ll of its child 
123c7 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68 61 76  .** journals hav
123c8 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61  e been rolled ba
123c9 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ck..**.** This f
123ca 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20 74 68  unction reads th
123cb 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
123cc 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c  e master-journal
123cd 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a 20 6d   file into .** m
123ce 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70 73 20  emory and loops 
123cf 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f 66 20  through each of 
123d0 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  the child journa
123d1 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a 2a 20  l names. For.** 
123d2 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e  each child journ
123d3 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20 69 66  al, it checks if
123d4 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66 20 74  :.**.**   * if t
123d5 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c  he child journal
123d6 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69 66 20   exists, and if 
123d7 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74 68 65  so.**   * if the
123d8 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 63   child journal c
123d9 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65 72 65  ontains a refere
123da 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20 6a 6f  nce to master jo
123db 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20 66 69  urnal .**     fi
123dc 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a 2a 2a  le zMaster.**.**
123dd 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f 75 72   If a child jour
123de 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64  nal can be found
123df 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 62 6f   that matches bo
123e0 74 68 20 6f 66 20 74 68 65 20 63 72 69 74 65 72  th of the criter
123e1 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74 68 69  ia.** above, thi
123e2 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
123e3 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67  ns without doing
123e4 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68 65 72   anything. Other
123e5 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f 20 73  wise, if.** no s
123e6 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61  uch child journa
123e7 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20  l can be found, 
123e8 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69 73 20  file zMaster is 
123e9 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a 2a 20  deleted from.** 
123ea 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  the file-system 
123eb 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 44  using sqlite3OsD
123ec 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49  elete()..**.** I
123ed 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 77 69  f an IO error wi
123ee 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69  thin this functi
123ef 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  on, an error cod
123f0 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54  e is returned. T
123f1 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
123f2 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f 72 79  allocates memory
123f3 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69   by calling sqli
123f4 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49 66 20  te3Malloc(). If 
123f5 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  an allocation.**
123f6 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e   fails, SQLITE_N
123f7 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64  OMEM is returned
123f8 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
123f9 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f 63 20  no IO or malloc 
123fa 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63 75 72  errors .** occur
123fb 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
123fc 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
123fd 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63 74 69  ODO: This functi
123fe 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 73  on allocates a s
123ff 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66 20 6d  ingle block of m
12400 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a 2a 2a  emory to load.**
12401 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f 6e 74   the entire cont
12402 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61 73 74  ents of the mast
12403 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  er journal file.
12404 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65 0a 2a   This could be.*
12405 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 6b 69  * a couple of ki
12406 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20 2d 20  lobytes or so - 
12407 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61 72 67  potentially larg
12408 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 65  er than the page
12409 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74   .** size..*/.st
1240a 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 64  atic int pager_d
1240b 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72 20 2a  elmaster(Pager *
1240c 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 63 68  pPager, const ch
1240d 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a 20 20  ar *zMaster){.  
1240e 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
1240f 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73  s = pPager->pVfs
12410 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  ;.  int rc;     
12411 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12412 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
12413 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
12414 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a 20 4d  pMaster;    /* M
12415 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72 2d 6a  alloc'd master-j
12416 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63  ournal file desc
12417 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69  riptor */.  sqli
12418 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75 72 6e  te3_file *pJourn
12419 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27  al;   /* Malloc'
1241a 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61 6c 20  d child-journal 
1241b 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
1241c 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  */.  char *zMast
1241d 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 20 2f  erJournal = 0; /
1241e 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20 6d 61  * Contents of ma
1241f 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
12420 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61 73 74  e */.  i64 nMast
12421 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20  erJournal;      
12422 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61 73 74   /* Size of mast
12423 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  er journal file 
12424 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  */..  /* Allocat
12425 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f 74 68  e space for both
12426 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20 61 6e   the pJournal an
12427 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65 20 64  d pMaster file d
12428 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20 2a 2a  escriptors..  **
12429 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
1242a 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20  open the master 
1242b 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
1242c 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20   reading..  */. 
1242d 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71 6c 69   pMaster = (sqli
1242e 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c 69 74  te3_file *)sqlit
1242f 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 56 66  e3MallocZero(pVf
12430 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29  s->szOsFile * 2)
12431 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28  ;.  pJournal = (
12432 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28  sqlite3_file *)(
12433 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20  ((u8 *)pMaster) 
12434 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  + pVfs->szOsFile
12435 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65  );.  if( !pMaste
12436 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  r ){.    rc = SQ
12437 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
12438 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69  lse{.    const i
12439 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51 4c 49  nt flags = (SQLI
1243a 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
1243b 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53  |SQLITE_OPEN_MAS
1243c 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20  TER_JOURNAL);.  
1243d 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1243e 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61 73 74  Open(pVfs, zMast
1243f 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66 6c 61  er, pMaster, fla
12440 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66  gs, 0);.  }.  if
12441 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
12442 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72  ) goto delmaster
12443 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 73 71  _out;..  rc = sq
12444 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28  lite3OsFileSize(
12445 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73 74 65  pMaster, &nMaste
12446 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28  rJournal);.  if(
12447 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
12448 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
12449 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73  out;..  if( nMas
1244a 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a  terJournal>0 ){.
1244b 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e      char *zJourn
1244c 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d  al;.    char *zM
1244d 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a 20 20  asterPtr = 0;.  
1244e 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50 74 72    int nMasterPtr
1244f 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e   = pVfs->mxPathn
12450 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a 20 4c  ame+1;..    /* L
12451 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65 20 6d  oad the entire m
12452 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
12453 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62  le into space ob
12454 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20 20 20  tained from.    
12455 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
12456 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65 64 20  c() and pointed 
12457 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a 6f 75  to by zMasterJou
12458 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a 20 20  rnal. .    */.  
12459 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c    zMasterJournal
1245a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
1245b 28 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75  ((int)nMasterJou
1245c 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65 72 50 74  rnal + nMasterPt
1245d 72 20 2b 20 31 29 3b 0a 20 20 20 20 69 66 28 20  r + 1);.    if( 
1245e 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20  !zMasterJournal 
1245f 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
12460 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
12461 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72    goto delmaster
12462 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
12463 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26 7a 4d  zMasterPtr = &zM
12464 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61  asterJournal[nMa
12465 73 74 65 72 4a 6f 75 72 6e 61 6c 2b 31 5d 3b 0a  sterJournal+1];.
12466 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12467 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72 2c 20  OsRead(pMaster, 
12468 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2c 20  zMasterJournal, 
12469 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f 75 72  (int)nMasterJour
1246a 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  nal, 0);.    if(
1246b 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1246c 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f   goto delmaster_
1246d 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 73 74 65 72  out;.    zMaster
1246e 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65 72 4a  Journal[nMasterJ
1246f 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b 0a 0a 20 20  ournal] = 0;..  
12470 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4d 61    zJournal = zMa
12471 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20  sterJournal;.   
12472 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72 6e 61   while( (zJourna
12473 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  l-zMasterJournal
12474 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  )<nMasterJournal
12475 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 78   ){.      int ex
12476 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63 20 3d  ists;.      rc =
12477 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73   sqlite3OsAccess
12478 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c  (pVfs, zJournal,
12479 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45   SQLITE_ACCESS_E
1247a 58 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b  XISTS, &exists);
1247b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1247c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1247d 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74      goto delmast
1247e 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  er_out;.      }.
1247f 20 20 20 20 20 20 69 66 28 20 65 78 69 73 74 73        if( exists
12480 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f   ){.        /* O
12481 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ne of the journa
12482 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ls pointed to by
12483 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
12484 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20 20 20  nal exists..    
12485 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74 20 61      ** Open it a
12486 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74 20 70  nd check if it p
12487 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d 61 73  oints at the mas
12488 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a  ter journal. If.
12489 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 72          ** so, r
1248a 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64 65  eturn without de
1248b 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73 74 65  leting the maste
1248c 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a  r journal file..
1248d 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1248e 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20     int c;.      
1248f 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53    int flags = (S
12490 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
12491 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  NLY|SQLITE_OPEN_
12492 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20  MAIN_JOURNAL);. 
12493 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
12494 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
12495 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75 72 6e  zJournal, pJourn
12496 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20  al, flags, 0);. 
12497 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
12498 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
12499 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
1249a 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20  ster_out;.      
1249b 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72 63 20    }..        rc 
1249c 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72  = readMasterJour
1249d 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20 7a 4d  nal(pJournal, zM
1249e 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73 74 65  asterPtr, nMaste
1249f 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20 20 73  rPtr);.        s
124a0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 4a  qlite3OsClose(pJ
124a1 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20 20 20  ournal);.       
124a2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
124a3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
124a4 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f  goto delmaster_o
124a5 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  ut;.        }.. 
124a6 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61 73 74         c = zMast
124a7 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26 20 73  erPtr[0]!=0 && s
124a8 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50 74 72  trcmp(zMasterPtr
124a9 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b 0a 20  , zMaster)==0;. 
124aa 20 20 20 20 20 20 20 69 66 28 20 63 20 29 7b 0a         if( c ){.
124ab 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20            /* We 
124ac 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20 44 6f  have a match. Do
124ad 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20   not delete the 
124ae 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
124af 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  ile. */.        
124b0 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72    goto delmaster
124b1 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _out;.        }.
124b2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4a        }.      zJ
124b3 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c 69 74  ournal += (sqlit
124b4 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f 75 72  e3Strlen30(zJour
124b5 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d 0a 20  nal)+1);.    }. 
124b6 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73 71 6c   }.  .  rc = sql
124b7 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
124b8 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a  s, zMaster, 0);.
124b9 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3a 0a  .delmaster_out:.
124ba 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a 6f 75    if( zMasterJou
124bb 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69  rnal ){.    sqli
124bc 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74 65 72  te3_free(zMaster
124bd 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a  Journal);.  }  .
124be 20 20 69 66 28 20 70 4d 61 73 74 65 72 20 29 7b    if( pMaster ){
124bf 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c  .    sqlite3OsCl
124c0 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ose(pMaster);.  
124c1 20 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65    assert( !isOpe
124c2 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b 0a 20  n(pJournal) );. 
124c3 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
124c4 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65  e(pMaster);.  re
124c5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
124c6 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
124c7 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e   is used to chan
124c8 67 65 20 74 68 65 20 61 63 74 75 61 6c 20 73 69  ge the actual si
124c9 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ze of the databa
124ca 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e 20 74  se .** file in t
124cb 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 20  he file-system. 
124cc 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e  This only happen
124cd 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74 69 6e  s when committin
124ce 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  g a transaction,
124cf 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62  .** or rolling b
124d0 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ack a transactio
124d1 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72 6f 6c  n (including rol
124d2 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f 74 2d  ling back a hot-
124d3 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a 2a 20  journal)..**.** 
124d4 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  If the main data
124d5 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74  base file is not
124d6 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65 78 63   open, or an exc
124d7 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73 20 6e  lusive lock is n
124d8 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68 69 73  ot.** held, this
124d9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
124da 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73 65 2c  o-op. Otherwise,
124db 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
124dc 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68 61 6e   file is.** chan
124dd 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70 61 67  ged to nPage pag
124de 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67 65 72  es (nPage*pPager
124df 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74 65 73  ->pageSize bytes
124e0 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65 0a 2a  ). If the file.*
124e1 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63 75 72  * on disk is cur
124e2 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68  rently larger th
124e3 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73 2c 20  an nPage pages, 
124e4 74 68 65 6e 20 75 73 65 20 74 68 65 20 56 46 53  then use the VFS
124e5 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28 29 20  .** xTruncate() 
124e6 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e 63 61  method to trunca
124e7 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c  te it..**.** Or,
124e8 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68 74 20   it might might 
124e9 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
124ea 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64 69 73   the file on dis
124eb 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61  k is smaller tha
124ec 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61 67 65  n .** nPage page
124ed 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e  s. Some operatin
124ee 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65 6d 65  g system impleme
124ef 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67 65 74  ntations can get
124f0 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a 2a 2a   confused if .**
124f1 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72 75 6e   you try to trun
124f2 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f 20 73  cate a file to s
124f3 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20 69 73  ome size that is
124f4 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69 74 20   larger than it 
124f5 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 69 73  .** currently is
124f6 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68 69 73  , so detect this
124f7 20 63 61 73 65 20 61 6e 64 20 77 72 69 74 65 20   case and write 
124f8 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20 62 79  a single zero by
124f9 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20 65 6e  te to .** the en
124fa 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66 69 6c  d of the new fil
124fb 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a  e instead..**.**
124fc 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
124fd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
124fe 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  . If an IO error
124ff 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6d 6f   occurs while mo
12500 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65 20 64  difying.** the d
12501 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 72 65  atabase file, re
12502 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63  turn the error c
12503 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ode to the calle
12504 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
12505 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28   pager_truncate(
12506 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
12507 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e  gno nPage){.  in
12508 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
12509 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
1250a 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43  state>=PAGER_EXC
1250b 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70 65 6e  LUSIVE && isOpen
1250c 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a  (pPager->fd) ){.
1250d 20 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53      i64 currentS
1250e 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20  ize, newSize;.  
1250f 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74    /* TODO: Is it
12510 20 73 61 66 65 20 74 6f 20 75 73 65 20 50 61 67   safe to use Pag
12511 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20 68 65  er.dbFileSize he
12512 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  re? */.    rc = 
12513 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
12514 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 63  e(pPager->fd, &c
12515 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20 20 20  urrentSize);.   
12516 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61 67 65   newSize = pPage
12517 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69 36 34  r->pageSize*(i64
12518 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66 28 20  )nPage;.    if( 
12519 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1251a 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d 6e 65   currentSize!=ne
1251b 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 69  wSize ){.      i
1251c 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65 3e 6e  f( currentSize>n
1251d 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  ewSize ){.      
1251e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1251f 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d  Truncate(pPager-
12520 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b 0a 20  >fd, newSize);. 
12521 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
12522 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12523 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e  OsWrite(pPager->
12524 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77 53 69  fd, "", 1, newSi
12525 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ze-1);.      }. 
12526 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
12527 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12528 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65    pPager->dbFile
12529 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20  Size = nPage;.  
1252a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1252b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1252c 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 76 61  /*.** Set the va
1252d 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67 65 72  lue of the Pager
1252e 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61 72 69  .sectorSize vari
1252f 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67 69 76  able for the giv
12530 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61 73 65  en.** pager base
12531 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 72  d on the value r
12532 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 78  eturned by the x
12533 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74 68 6f  SectorSize metho
12534 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70 65 6e  d.** of the open
12535 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
12536 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  The sector size 
12537 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75 73 65  will be used use
12538 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d 69  d .** to determi
12539 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e 64 20  ne the size and 
1253a 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a 6f 75  alignment of jou
1253b 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e 64 20  rnal header and 
1253c 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  .** master journ
1253d 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68  al pointers with
1253e 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75 72 6e  in created journ
1253f 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  al files..**.** 
12540 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66 69  For temporary fi
12541 6c 65 73 20 74 68 65 20 65 66 66 65 63 74 69 76  les the effectiv
12542 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73  e sector size is
12543 20 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65   always 512 byte
12544 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  s..**.** Otherwi
12545 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65 6d 70  se, for non-temp
12546 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74 68 65  orary files, the
12547 20 65 66 66 65 63 74 69 76 65 20 73 65 63 74 6f   effective secto
12548 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74 68 65  r size is.** the
12549 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
1254a 62 79 20 74 68 65 20 78 53 65 63 74 6f 72 53 69  by the xSectorSi
1254b 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f 75 6e  ze() method roun
1254c 64 65 64 20 75 70 20 74 6f 20 33 32 20 69 66 0a  ded up to 32 if.
1254d 2a 2a 20 69 74 20 69 73 20 6c 65 73 73 20 74 68  ** it is less th
1254e 61 6e 20 33 32 2c 20 6f 72 20 72 6f 75 6e 64 65  an 32, or rounde
1254f 64 20 64 6f 77 6e 20 74 6f 20 4d 41 58 5f 53 45  d down to MAX_SE
12550 43 54 4f 52 5f 53 49 5a 45 20 69 66 20 69 74 0a  CTOR_SIZE if it.
12551 2a 2a 20 69 73 20 67 72 65 61 74 65 72 20 74 68  ** is greater th
12552 61 6e 20 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49  an MAX_SECTOR_SI
12553 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ZE..*/.static vo
12554 69 64 20 73 65 74 53 65 63 74 6f 72 53 69 7a 65  id setSectorSize
12555 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
12556 0a 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65  .  assert( isOpe
12557 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c  n(pPager->fd) ||
12558 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
12559 65 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 50 61  e );..  if( !pPa
1255a 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b  ger->tempFile ){
1255b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72 20 73  .    /* Sector s
1255c 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61 74 74  ize doesn't matt
1255d 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79  er for temporary
1255e 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20 74 68   files. Also, th
1255f 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 6d 61  e file.    ** ma
12560 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65 6e 20  y not have been 
12561 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e 20 77  opened yet, in w
12562 68 69 63 68 20 63 61 73 65 20 74 68 65 20 4f 73  hich case the Os
12563 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20 20 20  SectorSize().   
12564 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20 73 65   ** call will se
12565 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20  gfault..    */. 
12566 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f     pPager->secto
12567 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 4f  rSize = sqlite3O
12568 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67  sSectorSize(pPag
12569 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20 20 69  er->fd);.  }.  i
1256a 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f  f( pPager->secto
1256b 72 53 69 7a 65 3c 33 32 20 29 7b 0a 20 20 20 20  rSize<32 ){.    
1256c 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69  pPager->sectorSi
1256d 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d 0a 20 20  ze = 512;.  }.  
1256e 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74  if( pPager->sect
1256f 6f 72 53 69 7a 65 3e 4d 41 58 5f 53 45 43 54 4f  orSize>MAX_SECTO
12570 52 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 61 73  R_SIZE ){.    as
12571 73 65 72 74 28 20 4d 41 58 5f 53 45 43 54 4f 52  sert( MAX_SECTOR
12572 5f 53 49 5a 45 3e 3d 35 31 32 20 29 3b 0a 20 20  _SIZE>=512 );.  
12573 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72    pPager->sector
12574 53 69 7a 65 20 3d 20 4d 41 58 5f 53 45 43 54 4f  Size = MAX_SECTO
12575 52 5f 53 49 5a 45 3b 0a 20 20 7d 0a 7d 0a 0a 2f  R_SIZE;.  }.}../
12576 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 74 68  *.** Playback th
12577 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68  e journal and th
12578 75 73 20 72 65 73 74 6f 72 65 20 74 68 65 20 64  us restore the d
12579 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 0a  atabase file to.
1257a 2a 2a 20 74 68 65 20 73 74 61 74 65 20 69 74 20  ** the state it 
1257b 77 61 73 20 69 6e 20 62 65 66 6f 72 65 20 77 65  was in before we
1257c 20 73 74 61 72 74 65 64 20 6d 61 6b 69 6e 67 20   started making 
1257d 63 68 61 6e 67 65 73 2e 20 20 0a 2a 2a 0a 2a 2a  changes.  .**.**
1257e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
1257f 65 20 66 6f 72 6d 61 74 20 69 73 20 61 73 20 66  e format is as f
12580 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a 0a 2a 2a 20 20  ollows: .**.**  
12581 28 31 29 20 20 38 20 62 79 74 65 20 70 72 65 66  (1)  8 byte pref
12582 69 78 2e 20 20 41 20 63 6f 70 79 20 6f 66 20 61  ix.  A copy of a
12583 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a  JournalMagic[]..
12584 2a 2a 20 20 28 32 29 20 20 34 20 62 79 74 65 20  **  (2)  4 byte 
12585 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67  big-endian integ
12586 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20  er which is the 
12587 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 69 64 20  number of valid 
12588 70 61 67 65 20 72 65 63 6f 72 64 73 0a 2a 2a 20  page records.** 
12589 20 20 20 20 20 20 69 6e 20 74 68 65 20 6a 6f 75        in the jou
1258a 72 6e 61 6c 2e 20 20 49 66 20 74 68 69 73 20 76  rnal.  If this v
1258b 61 6c 75 65 20 69 73 20 30 78 66 66 66 66 66 66  alue is 0xffffff
1258c 66 66 2c 20 74 68 65 6e 20 63 6f 6d 70 75 74 65  ff, then compute
1258d 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 6e 75   the.**       nu
1258e 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72 65 63  mber of page rec
1258f 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20 6a 6f  ords from the jo
12590 75 72 6e 61 6c 20 73 69 7a 65 2e 0a 2a 2a 20 20  urnal size..**  
12591 28 33 29 20 20 34 20 62 79 74 65 20 62 69 67 2d  (3)  4 byte big-
12592 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77  endian integer w
12593 68 69 63 68 20 69 73 20 74 68 65 20 69 6e 69 74  hich is the init
12594 69 61 6c 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ial value for th
12595 65 20 0a 2a 2a 20 20 20 20 20 20 20 73 61 6e 69  e .**       sani
12596 74 79 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20  ty checksum..** 
12597 20 28 34 29 20 20 34 20 62 79 74 65 20 69 6e 74   (4)  4 byte int
12598 65 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68  eger which is th
12599 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
1259a 73 20 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68  s to truncate th
1259b 65 0a 2a 2a 20 20 20 20 20 20 20 64 61 74 61 62  e.**       datab
1259c 61 73 65 20 74 6f 20 64 75 72 69 6e 67 20 61 20  ase to during a 
1259d 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 20 28 35  rollback..**  (5
1259e 29 20 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e  )  4 byte big-en
1259f 64 69 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  dian integer whi
125a0 63 68 20 69 73 20 74 68 65 20 73 65 63 74 6f 72  ch is the sector
125a1 20 73 69 7a 65 2e 20 20 54 68 65 20 68 65 61 64   size.  The head
125a2 65 72 0a 2a 2a 20 20 20 20 20 20 20 69 73 20 74  er.**       is t
125a3 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 69  his many bytes i
125a4 6e 20 73 69 7a 65 2e 0a 2a 2a 20 20 28 36 29 20  n size..**  (6) 
125a5 20 34 20 62 79 74 65 20 62 69 67 2d 65 6e 64 69   4 byte big-endi
125a6 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68  an integer which
125a7 20 69 73 20 74 68 65 20 70 61 67 65 20 73 69 7a   is the page siz
125a8 65 2e 0a 2a 2a 20 20 28 37 29 20 20 7a 65 72 6f  e..**  (7)  zero
125a9 20 70 61 64 64 69 6e 67 20 6f 75 74 20 74 6f 20   padding out to 
125aa 74 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72 20  the next sector 
125ab 73 69 7a 65 2e 0a 2a 2a 20 20 28 38 29 20 20 5a  size..**  (8)  Z
125ac 65 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 65  ero or more page
125ad 73 20 69 6e 73 74 61 6e 63 65 73 2c 20 65 61 63  s instances, eac
125ae 68 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  h as follows:.**
125af 20 20 20 20 20 20 20 20 2b 20 20 34 20 62 79 74          +  4 byt
125b0 65 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a  e page number..*
125b1 2a 20 20 20 20 20 20 20 20 2b 20 20 70 50 61 67  *        +  pPag
125b2 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74  er->pageSize byt
125b3 65 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 20  es of data..**  
125b4 20 20 20 20 20 20 2b 20 20 34 20 62 79 74 65 20        +  4 byte 
125b5 63 68 65 63 6b 73 75 6d 0a 2a 2a 0a 2a 2a 20 57  checksum.**.** W
125b6 68 65 6e 20 77 65 20 73 70 65 61 6b 20 6f 66 20  hen we speak of 
125b7 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  the journal head
125b8 65 72 2c 20 77 65 20 6d 65 61 6e 20 74 68 65 20  er, we mean the 
125b9 66 69 72 73 74 20 37 20 69 74 65 6d 73 20 61 62  first 7 items ab
125ba 6f 76 65 2e 0a 2a 2a 20 45 61 63 68 20 65 6e 74  ove..** Each ent
125bb 72 79 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  ry in the journa
125bc 6c 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  l is an instance
125bd 20 6f 66 20 74 68 65 20 38 74 68 20 69 74 65 6d   of the 8th item
125be 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65  ..**.** Call the
125bf 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20   value from the 
125c0 73 65 63 6f 6e 64 20 62 75 6c 6c 65 74 20 22 6e  second bullet "n
125c1 52 65 63 22 2e 20 20 6e 52 65 63 20 69 73 20 74  Rec".  nRec is t
125c2 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
125c3 76 61 6c 69 64 20 70 61 67 65 20 65 6e 74 72 69  valid page entri
125c4 65 73 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  es in the journa
125c5 6c 2e 20 20 49 6e 20 6d 6f 73 74 20 63 61 73 65  l.  In most case
125c6 73 2c 20 79 6f 75 20 63 61 6e 20 63 6f 6d 70 75  s, you can compu
125c7 74 65 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20  te the.** value 
125c8 6f 66 20 6e 52 65 63 20 66 72 6f 6d 20 74 68 65  of nRec from the
125c9 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75   size of the jou
125ca 72 6e 61 6c 20 66 69 6c 65 2e 20 20 42 75 74 20  rnal file.  But 
125cb 69 66 20 61 20 70 6f 77 65 72 0a 2a 2a 20 66 61  if a power.** fa
125cc 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 20 77  ilure occurred w
125cd 68 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  hile the journal
125ce 20 77 61 73 20 62 65 69 6e 67 20 77 72 69 74 74   was being writt
125cf 65 6e 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20  en, it could be 
125d0 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61 74  the.** case that
125d1 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
125d2 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61   journal file ha
125d3 64 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69  d already been i
125d4 6e 63 72 65 61 73 65 64 20 62 75 74 0a 2a 2a 20  ncreased but.** 
125d5 74 68 65 20 65 78 74 72 61 20 65 6e 74 72 69 65  the extra entrie
125d6 73 20 68 61 64 20 6e 6f 74 20 79 65 74 20 6d 61  s had not yet ma
125d7 64 65 20 69 74 20 73 61 66 65 6c 79 20 74 6f 20  de it safely to 
125d8 64 69 73 6b 2e 20 20 49 6e 20 73 75 63 68 20 61  disk.  In such a
125d9 20 63 61 73 65 2c 0a 2a 2a 20 74 68 65 20 76 61   case,.** the va
125da 6c 75 65 20 6f 66 20 6e 52 65 63 20 63 6f 6d 70  lue of nRec comp
125db 75 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 69  uted from the fi
125dc 6c 65 20 73 69 7a 65 20 77 6f 75 6c 64 20 62 65  le size would be
125dd 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 46 6f 72   too large.  For
125de 0a 2a 2a 20 74 68 61 74 20 72 65 61 73 6f 6e 2c  .** that reason,
125df 20 77 65 20 61 6c 77 61 79 73 20 75 73 65 20 74   we always use t
125e0 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 6e  he nRec value in
125e1 20 74 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a   the header..**.
125e2 2a 2a 20 49 66 20 74 68 65 20 6e 52 65 63 20 76  ** If the nRec v
125e3 61 6c 75 65 20 69 73 20 30 78 66 66 66 66 66 66  alue is 0xffffff
125e4 66 66 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  ff it means that
125e5 20 6e 52 65 63 20 73 68 6f 75 6c 64 20 62 65 20   nRec should be 
125e6 63 6f 6d 70 75 74 65 64 0a 2a 2a 20 66 72 6f 6d  computed.** from
125e7 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2e 20   the file size. 
125e8 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 75   This value is u
125e9 73 65 64 20 77 68 65 6e 20 74 68 65 20 75 73 65  sed when the use
125ea 72 20 73 65 6c 65 63 74 73 20 74 68 65 0a 2a 2a  r selects the.**
125eb 20 6e 6f 2d 73 79 6e 63 20 6f 70 74 69 6f 6e 20   no-sync option 
125ec 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e  for the journal.
125ed 20 20 41 20 70 6f 77 65 72 20 66 61 69 6c 75 72    A power failur
125ee 65 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f 20  e could lead to 
125ef 63 6f 72 72 75 70 74 69 6f 6e 0a 2a 2a 20 69 6e  corruption.** in
125f0 20 74 68 69 73 20 63 61 73 65 2e 20 20 42 75 74   this case.  But
125f1 20 66 6f 72 20 74 68 69 6e 67 73 20 6c 69 6b 65   for things like
125f2 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
125f3 20 28 77 68 69 63 68 20 77 69 6c 6c 20 62 65 0a   (which will be.
125f4 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20  ** deleted when 
125f5 74 68 65 20 70 6f 77 65 72 20 69 73 20 72 65 73  the power is res
125f6 74 6f 72 65 64 29 20 77 65 20 64 6f 6e 27 74 20  tored) we don't 
125f7 63 61 72 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66  care.  .**.** If
125f8 20 74 68 65 20 66 69 6c 65 20 6f 70 65 6e 65 64   the file opened
125f9 20 61 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   as the journal 
125fa 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 20 77 65  file is not a we
125fb 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 6a 6f 75  ll-formed.** jou
125fc 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 6e 20 61  rnal file then a
125fd 6c 6c 20 70 61 67 65 73 20 75 70 20 74 6f 20 74  ll pages up to t
125fe 68 65 20 66 69 72 73 74 20 63 6f 72 72 75 70 74  he first corrupt
125ff 65 64 20 70 61 67 65 20 61 72 65 20 72 6f 6c 6c  ed page are roll
12600 65 64 0a 2a 2a 20 62 61 63 6b 20 28 6f 72 20 6e  ed.** back (or n
12601 6f 20 70 61 67 65 73 20 69 66 20 74 68 65 20 6a  o pages if the j
12602 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 69 73  ournal header is
12603 20 63 6f 72 72 75 70 74 65 64 29 2e 20 54 68 65   corrupted). The
12604 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a   journal file.**
12605 20 69 73 20 74 68 65 6e 20 64 65 6c 65 74 65 64   is then deleted
12606 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72   and SQLITE_OK r
12607 65 74 75 72 6e 65 64 2c 20 6a 75 73 74 20 61 73  eturned, just as
12608 20 69 66 20 6e 6f 20 63 6f 72 72 75 70 74 69 6f   if no corruptio
12609 6e 20 68 61 64 0a 2a 2a 20 62 65 65 6e 20 65 6e  n had.** been en
1260a 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a  countered..**.**
1260b 20 49 66 20 61 6e 20 49 2f 4f 20 6f 72 20 6d 61   If an I/O or ma
1260c 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f 63 63  lloc() error occ
1260d 75 72 73 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c  urs, the journal
1260e 2d 66 69 6c 65 20 69 73 20 6e 6f 74 20 64 65 6c  -file is not del
1260f 65 74 65 64 0a 2a 2a 20 61 6e 64 20 61 6e 20 65  eted.** and an e
12610 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
12611 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urned..**.** The
12612 20 69 73 48 6f 74 20 70 61 72 61 6d 65 74 65 72   isHot parameter
12613 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
12614 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f  we are trying to
12615 20 72 6f 6c 6c 62 61 63 6b 20 61 20 6a 6f 75 72   rollback a jour
12616 6e 61 6c 0a 2a 2a 20 74 68 61 74 20 6d 69 67 68  nal.** that migh
12617 74 20 62 65 20 61 20 68 6f 74 20 6a 6f 75 72 6e  t be a hot journ
12618 61 6c 2e 20 20 4f 72 2c 20 69 74 20 63 6f 75 6c  al.  Or, it coul
12619 64 20 62 65 20 74 68 61 74 20 74 68 65 20 6a 6f  d be that the jo
1261a 75 72 6e 61 6c 20 69 73 20 0a 2a 2a 20 70 72 65  urnal is .** pre
1261b 73 65 72 76 65 64 20 62 65 63 61 75 73 65 20 6f  served because o
1261c 66 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45  f JOURNALMODE_PE
1261d 52 53 49 53 54 20 6f 72 20 4a 4f 55 52 4e 41 4c  RSIST or JOURNAL
1261e 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 2e 0a 2a  MODE_TRUNCATE..*
1261f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  * If the journal
12620 20 72 65 61 6c 6c 79 20 69 73 20 68 6f 74 2c 20   really is hot, 
12621 72 65 73 65 74 20 74 68 65 20 70 61 67 65 72 20  reset the pager 
12622 63 61 63 68 65 20 70 72 69 6f 72 20 72 6f 6c 6c  cache prior roll
12623 69 6e 67 0a 2a 2a 20 62 61 63 6b 20 61 6e 79 20  ing.** back any 
12624 63 6f 6e 74 65 6e 74 2e 20 20 49 66 20 74 68 65  content.  If the
12625 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6d 65 72 65   journal is mere
12626 6c 79 20 70 65 72 73 69 73 74 65 6e 74 2c 20 6e  ly persistent, n
12627 6f 20 72 65 73 65 74 20 69 73 0a 2a 2a 20 6e 65  o reset is.** ne
12628 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eded..*/.static 
12629 69 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61  int pager_playba
1262a 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ck(Pager *pPager
1262b 2c 20 69 6e 74 20 69 73 48 6f 74 29 7b 0a 20 20  , int isHot){.  
1262c 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
1262d 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73  s = pPager->pVfs
1262e 3b 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20  ;.  i64 szJ;    
1262f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12630 53 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72  Size of the jour
12631 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74 65  nal file in byte
12632 73 20 2a 2f 0a 20 20 75 33 32 20 6e 52 65 63 3b  s */.  u32 nRec;
12633 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12634 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 52 65 63  /* Number of Rec
12635 6f 72 64 73 20 69 6e 20 74 68 65 20 6a 6f 75 72  ords in the jour
12636 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 75 3b 20  nal */.  u32 u; 
12637 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12638 20 20 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f    /* Unsigned lo
12639 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
1263a 50 67 6e 6f 20 6d 78 50 67 20 3d 20 30 3b 20 20  Pgno mxPg = 0;  
1263b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
1263c 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
1263d 20 66 69 6c 65 20 69 6e 20 70 61 67 65 73 20 2a   file in pages *
1263e 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
1263f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12640 52 65 73 75 6c 74 20 63 6f 64 65 20 6f 66 20 61  Result code of a
12641 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20   subroutine */. 
12642 20 69 6e 74 20 72 65 73 20 3d 20 31 3b 20 20 20   int res = 1;   
12643 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
12644 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
12645 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 29  qlite3OsAccess()
12646 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 73   */.  char *zMas
12647 74 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  ter = 0;       /
12648 2a 20 4e 61 6d 65 20 6f 66 20 6d 61 73 74 65 72  * Name of master
12649 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 66   journal file if
1264a 20 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 65   any */.  int ne
1264b 65 64 50 61 67 65 72 52 65 73 65 74 3b 20 20 20  edPagerReset;   
1264c 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 72 65     /* True to re
1264d 73 65 74 20 70 61 67 65 20 70 72 69 6f 72 20 74  set page prior t
1264e 6f 20 66 69 72 73 74 20 70 61 67 65 20 72 6f 6c  o first page rol
1264f 6c 62 61 63 6b 20 2a 2f 0a 0a 20 20 2f 2a 20 46  lback */..  /* F
12650 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61  igure out how ma
12651 6e 79 20 72 65 63 6f 72 64 73 20 61 72 65 20 69  ny records are i
12652 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20  n the journal.  
12653 41 62 6f 72 74 20 65 61 72 6c 79 20 69 66 0a 20  Abort early if. 
12654 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   ** the journal 
12655 69 73 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20  is empty..  */. 
12656 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
12657 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a  pPager->jfd) );.
12658 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
12659 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d  FileSize(pPager-
1265a 3e 6a 66 64 2c 20 26 73 7a 4a 29 3b 0a 20 20 69  >jfd, &szJ);.  i
1265b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1265c 20 7c 7c 20 73 7a 4a 3d 3d 30 20 29 7b 0a 20 20   || szJ==0 ){.  
1265d 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62    goto end_playb
1265e 61 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  ack;.  }..  /* R
1265f 65 61 64 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ead the master j
12660 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 66 72 6f 6d  ournal name from
12661 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 66   the journal, if
12662 20 69 74 20 69 73 20 70 72 65 73 65 6e 74 2e 0a   it is present..
12663 20 20 2a 2a 20 49 66 20 61 20 6d 61 73 74 65 72    ** If a master
12664 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61   journal file na
12665 6d 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2c  me is specified,
12666 20 62 75 74 20 74 68 65 20 66 69 6c 65 20 69 73   but the file is
12667 20 6e 6f 74 0a 20 20 2a 2a 20 70 72 65 73 65 6e   not.  ** presen
12668 74 20 6f 6e 20 64 69 73 6b 2c 20 74 68 65 6e 20  t on disk, then 
12669 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e  the journal is n
1266a 6f 74 20 68 6f 74 20 61 6e 64 20 64 6f 65 73 20  ot hot and does 
1266b 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 0a 20  not need to be. 
1266c 20 2a 2a 20 70 6c 61 79 65 64 20 62 61 63 6b 2e   ** played back.
1266d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 4f 44 4f 3a  .  **.  ** TODO:
1266e 20 54 65 63 68 6e 69 63 61 6c 6c 79 20 74 68 65   Technically the
1266f 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 6e   following is an
12670 20 65 72 72 6f 72 20 62 65 63 61 75 73 65 20 69   error because i
12671 74 20 61 73 73 75 6d 65 73 20 74 68 61 74 0a 20  t assumes that. 
12672 20 2a 2a 20 62 75 66 66 65 72 20 50 61 67 65 72   ** buffer Pager
12673 2e 70 54 6d 70 53 70 61 63 65 20 69 73 20 28 6d  .pTmpSpace is (m
12674 78 50 61 74 68 6e 61 6d 65 2b 31 29 20 62 79 74  xPathname+1) byt
12675 65 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 69 2e  es or larger. i.
12676 65 2e 20 74 68 61 74 0a 20 20 2a 2a 20 28 70 50  e. that.  ** (pP
12677 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3e  ager->pageSize >
12678 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e  = pPager->pVfs->
12679 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 2e 20 55  mxPathname+1). U
1267a 73 69 6e 67 20 6f 73 5f 75 6e 69 78 2e 63 2c 0a  sing os_unix.c,.
1267b 20 20 2a 2a 20 20 6d 78 50 61 74 68 6e 61 6d 65    **  mxPathname
1267c 20 69 73 20 35 31 32 2c 20 77 68 69 63 68 20 69   is 512, which i
1267d 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  s the same as th
1267e 65 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 61  e minimum allowa
1267f 62 6c 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 66  ble value.  ** f
12680 6f 72 20 70 61 67 65 53 69 7a 65 2e 0a 20 20 2a  or pageSize..  *
12681 2f 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20 70 50  /.  zMaster = pP
12682 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b  ager->pTmpSpace;
12683 0a 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74  .  rc = readMast
12684 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  erJournal(pPager
12685 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20  ->jfd, zMaster, 
12686 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78  pPager->pVfs->mx
12687 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 69  Pathname+1);.  i
12688 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12689 20 26 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 29   && zMaster[0] )
1268a 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1268b 65 33 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c  e3OsAccess(pVfs,
1268c 20 7a 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45   zMaster, SQLITE
1268d 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20  _ACCESS_EXISTS, 
1268e 26 72 65 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d 61  &res);.  }.  zMa
1268f 73 74 65 72 20 3d 20 30 3b 0a 20 20 69 66 28 20  ster = 0;.  if( 
12690 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
12691 20 21 72 65 73 20 29 7b 0a 20 20 20 20 67 6f 74   !res ){.    got
12692 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
12693 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f    }.  pPager->jo
12694 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20  urnalOff = 0;.  
12695 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20 3d  needPagerReset =
12696 20 69 73 48 6f 74 3b 0a 0a 20 20 2f 2a 20 54 68   isHot;..  /* Th
12697 69 73 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74  is loop terminat
12698 65 73 20 65 69 74 68 65 72 20 77 68 65 6e 20 61  es either when a
12699 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28   readJournalHdr(
1269a 29 20 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 72  ) or .  ** pager
1269b 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61  _playback_one_pa
1269c 67 65 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e  ge() call return
1269d 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72  s SQLITE_DONE or
1269e 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 0a 20 20   an IO error .  
1269f 2a 2a 20 6f 63 63 75 72 73 2e 20 0a 20 20 2a 2f  ** occurs. .  */
126a0 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20  .  while( 1 ){. 
126a1 20 20 20 69 6e 74 20 69 73 55 6e 73 79 6e 63 20     int isUnsync 
126a2 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 61  = 0;..    /* Rea
126a3 64 20 74 68 65 20 6e 65 78 74 20 6a 6f 75 72 6e  d the next journ
126a4 61 6c 20 68 65 61 64 65 72 20 66 72 6f 6d 20 74  al header from t
126a5 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
126a6 20 20 49 66 20 74 68 65 72 65 20 61 72 65 0a 20    If there are. 
126a7 20 20 20 2a 2a 20 6e 6f 74 20 65 6e 6f 75 67 68     ** not enough
126a8 20 62 79 74 65 73 20 6c 65 66 74 20 69 6e 20 74   bytes left in t
126a9 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
126aa 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 20 68  for a complete h
126ab 65 61 64 65 72 2c 20 6f 72 0a 20 20 20 20 2a 2a  eader, or.    **
126ac 20 69 74 20 69 73 20 63 6f 72 72 75 70 74 65 64   it is corrupted
126ad 2c 20 74 68 65 6e 20 61 20 70 72 6f 63 65 73 73  , then a process
126ae 20 6d 75 73 74 20 6f 66 20 66 61 69 6c 65 64 20   must of failed 
126af 77 68 69 6c 65 20 77 72 69 74 69 6e 67 20 69 74  while writing it
126b0 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 6e  ..    ** This in
126b1 64 69 63 61 74 65 73 20 6e 6f 74 68 69 6e 67 20  dicates nothing 
126b2 6d 6f 72 65 20 6e 65 65 64 73 20 74 6f 20 62 65  more needs to be
126b3 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 20 20   rolled back..  
126b4 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 72 65    */.    rc = re
126b5 61 64 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61  adJournalHdr(pPa
126b6 67 65 72 2c 20 69 73 48 6f 74 2c 20 73 7a 4a 2c  ger, isHot, szJ,
126b7 20 26 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b 0a   &nRec, &mxPg);.
126b8 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
126b9 54 45 5f 4f 4b 20 29 7b 20 0a 20 20 20 20 20 20  TE_OK ){ .      
126ba 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
126bb 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ONE ){.        r
126bc 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
126bd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74       }.      got
126be 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a  o end_playback;.
126bf 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
126c0 20 6e 52 65 63 20 69 73 20 30 78 66 66 66 66 66   nRec is 0xfffff
126c1 66 66 66 2c 20 74 68 65 6e 20 74 68 69 73 20 6a  fff, then this j
126c2 6f 75 72 6e 61 6c 20 77 61 73 20 63 72 65 61 74  ournal was creat
126c3 65 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 0a  ed by a process.
126c4 20 20 20 20 2a 2a 20 77 6f 72 6b 69 6e 67 20 69      ** working i
126c5 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e 20  n no-sync mode. 
126c6 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20  This means that 
126c7 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20  the rest of the 
126c8 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66  journal.    ** f
126c9 69 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  ile consists of 
126ca 70 61 67 65 73 2c 20 74 68 65 72 65 20 61 72 65  pages, there are
126cb 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75 72 6e 61 6c   no more journal
126cc 20 68 65 61 64 65 72 73 2e 20 43 6f 6d 70 75 74   headers. Comput
126cd 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 76 61 6c  e.    ** the val
126ce 75 65 20 6f 66 20 6e 52 65 63 20 62 61 73 65 64  ue of nRec based
126cf 20 6f 6e 20 74 68 69 73 20 61 73 73 75 6d 70 74   on this assumpt
126d0 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
126d1 69 66 28 20 6e 52 65 63 3d 3d 30 78 66 66 66 66  if( nRec==0xffff
126d2 66 66 66 66 20 29 7b 0a 20 20 20 20 20 20 61 73  ffff ){.      as
126d3 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f  sert( pPager->jo
126d4 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41  urnalOff==JOURNA
126d5 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
126d6 20 29 3b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d   );.      nRec =
126d7 20 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 4a 4f   (int)((szJ - JO
126d8 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
126d9 67 65 72 29 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47  ger))/JOURNAL_PG
126da 5f 53 5a 28 70 50 61 67 65 72 29 29 3b 0a 20 20  _SZ(pPager));.  
126db 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e    }..    /* If n
126dc 52 65 63 20 69 73 20 30 20 61 6e 64 20 74 68 69  Rec is 0 and thi
126dd 73 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6f 66  s rollback is of
126de 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63   a transaction c
126df 72 65 61 74 65 64 20 62 79 20 74 68 69 73 0a 20  reated by this. 
126e0 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 61 6e     ** process an
126e1 64 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65  d if this is the
126e2 20 66 69 6e 61 6c 20 68 65 61 64 65 72 20 69 6e   final header in
126e3 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68   the journal, th
126e4 65 6e 20 69 74 20 6d 65 61 6e 73 0a 20 20 20 20  en it means.    
126e5 2a 2a 20 74 68 61 74 20 74 68 69 73 20 70 61 72  ** that this par
126e6 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t of the journal
126e7 20 77 61 73 20 62 65 69 6e 67 20 66 69 6c 6c 65   was being fille
126e8 64 20 62 75 74 20 68 61 73 20 6e 6f 74 20 79 65  d but has not ye
126e9 74 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 73 79  t been.    ** sy
126ea 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 20 20 43  nced to disk.  C
126eb 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65  ompute the numbe
126ec 72 20 6f 66 20 70 61 67 65 73 20 62 61 73 65 64  r of pages based
126ed 20 6f 6e 20 74 68 65 20 72 65 6d 61 69 6e 69 6e   on the remainin
126ee 67 0a 20 20 20 20 2a 2a 20 73 69 7a 65 20 6f 66  g.    ** size of
126ef 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a   the file..    *
126f0 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 74 68 69  *.    ** The thi
126f1 72 64 20 74 65 72 6d 20 6f 66 20 74 68 65 20 74  rd term of the t
126f2 65 73 74 20 77 61 73 20 61 64 64 65 64 20 74 6f  est was added to
126f3 20 66 69 78 20 74 69 63 6b 65 74 20 23 32 35 36   fix ticket #256
126f4 35 2e 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 72  5..    ** When r
126f5 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f  olling back a ho
126f6 74 20 6a 6f 75 72 6e 61 6c 2c 20 6e 52 65 63 3d  t journal, nRec=
126f7 3d 30 20 61 6c 77 61 79 73 20 6d 65 61 6e 73 20  =0 always means 
126f8 74 68 61 74 20 74 68 65 20 6e 65 78 74 0a 20 20  that the next.  
126f9 20 20 2a 2a 20 63 68 75 6e 6b 20 6f 66 20 74 68    ** chunk of th
126fa 65 20 6a 6f 75 72 6e 61 6c 20 63 6f 6e 74 61 69  e journal contai
126fb 6e 73 20 7a 65 72 6f 20 70 61 67 65 73 20 74 6f  ns zero pages to
126fc 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   be rolled back.
126fd 20 20 42 75 74 0a 20 20 20 20 2a 2a 20 77 68 65    But.    ** whe
126fe 6e 20 64 6f 69 6e 67 20 61 20 52 4f 4c 4c 42 41  n doing a ROLLBA
126ff 43 4b 20 61 6e 64 20 74 68 65 20 6e 52 65 63 3d  CK and the nRec=
12700 3d 30 20 63 68 75 6e 6b 20 69 73 20 74 68 65 20  =0 chunk is the 
12701 6c 61 73 74 20 63 68 75 6e 6b 20 69 6e 0a 20 20  last chunk in.  
12702 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c    ** the journal
12703 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
12704 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68  the journal migh
12705 74 20 63 6f 6e 74 61 69 6e 20 61 64 64 69 74 69  t contain additi
12706 6f 6e 61 6c 0a 20 20 20 20 2a 2a 20 70 61 67 65  onal.    ** page
12707 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62  s that need to b
12708 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e  e rolled back an
12709 64 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65  d that the numbe
1270a 72 20 6f 66 20 70 61 67 65 73 20 0a 20 20 20 20  r of pages .    
1270b 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6d  ** should be com
1270c 70 75 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74  puted based on t
1270d 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1270e 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  size..    */.   
1270f 20 69 66 28 20 6e 52 65 63 3d 3d 30 20 26 26 20   if( nRec==0 && 
12710 21 69 73 48 6f 74 20 26 26 0a 20 20 20 20 20 20  !isHot &&.      
12711 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
12712 6c 48 64 72 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52  lHdr+JOURNAL_HDR
12713 5f 53 5a 28 70 50 61 67 65 72 29 3d 3d 70 50 61  _SZ(pPager)==pPa
12714 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
12715 29 7b 0a 20 20 20 20 20 20 6e 52 65 63 20 3d 20  ){.      nRec = 
12716 28 69 6e 74 29 28 28 73 7a 4a 20 2d 20 70 50 61  (int)((szJ - pPa
12717 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29  ger->journalOff)
12718 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a   / JOURNAL_PG_SZ
12719 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20  (pPager));.     
1271a 20 69 73 55 6e 73 79 6e 63 20 3d 20 31 3b 0a 20   isUnsync = 1;. 
1271b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
1271c 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72 73  this is the firs
1271d 74 20 68 65 61 64 65 72 20 72 65 61 64 20 66 72  t header read fr
1271e 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2c 20  om the journal, 
1271f 74 72 75 6e 63 61 74 65 20 74 68 65 0a 20 20 20  truncate the.   
12720 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
12721 65 20 62 61 63 6b 20 74 6f 20 69 74 73 20 6f 72  e back to its or
12722 69 67 69 6e 61 6c 20 73 69 7a 65 2e 0a 20 20 20  iginal size..   
12723 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67   */.    if( pPag
12724 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d  er->journalOff==
12725 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70  JOURNAL_HDR_SZ(p
12726 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20  Pager) ){.      
12727 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63  rc = pager_trunc
12728 61 74 65 28 70 50 61 67 65 72 2c 20 6d 78 50 67  ate(pPager, mxPg
12729 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1272a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1272b 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70        goto end_p
1272c 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 7d  layback;.      }
1272d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64  .      pPager->d
1272e 62 53 69 7a 65 20 3d 20 6d 78 50 67 3b 0a 20 20  bSize = mxPg;.  
1272f 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79    }..    /* Copy
12730 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20   original pages 
12731 6f 75 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  out of the journ
12732 61 6c 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f  al and back into
12733 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 64 61 74   the .    ** dat
12734 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 2f 6f  abase file and/o
12735 72 20 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20  r page cache..  
12736 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 75 3d 30    */.    for(u=0
12737 3b 20 75 3c 6e 52 65 63 3b 20 75 2b 2b 29 7b 0a  ; u<nRec; u++){.
12738 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 50 61        if( needPa
12739 67 65 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20  gerReset ){.    
1273a 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28      pager_reset(
1273b 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20  pPager);.       
1273c 20 6e 65 65 64 50 61 67 65 72 52 65 73 65 74 20   needPagerReset 
1273d 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
1273e 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c     rc = pager_pl
1273f 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28  ayback_one_page(
12740 70 50 61 67 65 72 2c 31 2c 69 73 55 6e 73 79 6e  pPager,1,isUnsyn
12741 63 2c 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  c,&pPager->journ
12742 61 6c 4f 66 66 2c 30 2c 30 29 3b 0a 20 20 20 20  alOff,0,0);.    
12743 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12744 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69  _OK ){.        i
12745 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
12746 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  NE ){.          
12747 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
12748 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
12749 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 73  ->journalOff = s
1274a 7a 4a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  zJ;.          br
1274b 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  eak;.        }el
1274c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  se{.          /*
1274d 20 49 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c   If we are unabl
1274e 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 71  e to rollback, q
1274f 75 69 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74  uit and return t
12750 68 65 20 65 72 72 6f 72 0a 20 20 20 20 20 20 20  he error.       
12751 20 20 20 2a 2a 20 63 6f 64 65 2e 20 20 54 68 69     ** code.  Thi
12752 73 20 77 69 6c 6c 20 63 61 75 73 65 20 74 68 65  s will cause the
12753 20 70 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20   pager to enter 
12754 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 0a  the error state.
12755 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 20            ** so 
12756 74 68 61 74 20 6e 6f 20 66 75 72 74 68 65 72 20  that no further 
12757 68 61 72 6d 20 77 69 6c 6c 20 62 65 20 64 6f 6e  harm will be don
12758 65 2e 20 20 50 65 72 68 61 70 73 20 74 68 65 20  e.  Perhaps the 
12759 6e 65 78 74 0a 20 20 20 20 20 20 20 20 20 20 2a  next.          *
1275a 2a 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 6d  * process to com
1275b 65 20 61 6c 6f 6e 67 20 77 69 6c 6c 20 62 65 20  e along will be 
1275c 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  able to rollback
1275d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20   the database.. 
1275e 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
1275f 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70        goto end_p
12760 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 20 20 20  layback;.       
12761 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
12762 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43  .  }.  /*NOTREAC
12763 48 45 44 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  HED*/.  assert( 
12764 30 20 29 3b 0a 0a 65 6e 64 5f 70 6c 61 79 62 61  0 );..end_playba
12765 63 6b 3a 0a 20 20 2f 2a 20 46 6f 6c 6c 6f 77 69  ck:.  /* Followi
12766 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 74  ng a rollback, t
12767 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
12768 20 73 68 6f 75 6c 64 20 62 65 20 62 61 63 6b 20   should be back 
12769 69 6e 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 0a  in its original.
1276a 20 20 2a 2a 20 73 74 61 74 65 20 70 72 69 6f 72    ** state prior
1276b 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
1276c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1276d 2c 20 73 6f 20 69 6e 76 6f 6b 65 20 74 68 65 0a  , so invoke the.
1276e 20 20 2a 2a 20 53 51 4c 49 54 45 5f 46 43 4e 54    ** SQLITE_FCNT
1276f 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 66  L_DB_UNCHANGED f
12770 69 6c 65 2d 63 6f 6e 74 72 6f 6c 20 6d 65 74 68  ile-control meth
12771 6f 64 20 74 6f 20 64 69 73 61 62 6c 65 20 74 68  od to disable th
12772 65 0a 20 20 2a 2a 20 61 73 73 65 72 74 69 6f 6e  e.  ** assertion
12773 20 74 68 61 74 20 74 68 65 20 74 72 61 6e 73 61   that the transa
12774 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 77 61  ction counter wa
12775 73 20 6d 6f 64 69 66 69 65 64 2e 0a 20 20 2a 2f  s modified..  */
12776 0a 20 20 61 73 73 65 72 74 28 0a 20 20 20 20 70  .  assert(.    p
12777 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68  Pager->fd->pMeth
12778 6f 64 73 3d 3d 30 20 7c 7c 0a 20 20 20 20 73 71  ods==0 ||.    sq
12779 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
1277a 6f 6c 28 70 50 61 67 65 72 2d 3e 66 64 2c 53 51  ol(pPager->fd,SQ
1277b 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e  LITE_FCNTL_DB_UN
1277c 43 48 41 4e 47 45 44 2c 30 29 3e 3d 53 51 4c 49  CHANGED,0)>=SQLI
1277d 54 45 5f 4f 4b 0a 20 20 29 3b 0a 0a 20 20 2f 2a  TE_OK.  );..  /*
1277e 20 49 66 20 74 68 69 73 20 70 6c 61 79 62 61 63   If this playbac
1277f 6b 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 61  k is happening a
12780 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 61 73 20  utomatically as 
12781 61 20 72 65 73 75 6c 74 20 6f 66 20 61 6e 20 49  a result of an I
12782 4f 20 6f 72 20 0a 20 20 2a 2a 20 6d 61 6c 6c 6f  O or .  ** mallo
12783 63 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63  c error that occ
12784 75 72 72 65 64 20 61 66 74 65 72 20 74 68 65 20  urred after the 
12785 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 77  change-counter w
12786 61 73 20 75 70 64 61 74 65 64 20 62 75 74 20 0a  as updated but .
12787 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20    ** before the 
12788 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20  transaction was 
12789 63 6f 6d 6d 69 74 74 65 64 2c 20 74 68 65 6e 20  committed, then 
1278a 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
1278b 65 72 20 0a 20 20 2a 2a 20 6d 6f 64 69 66 69 63  er .  ** modific
1278c 61 74 69 6f 6e 20 6d 61 79 20 6a 75 73 74 20 68  ation may just h
1278d 61 76 65 20 62 65 65 6e 20 72 65 76 65 72 74 65  ave been reverte
1278e 64 2e 20 49 66 20 74 68 69 73 20 68 61 70 70 65  d. If this happe
1278f 6e 73 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20  ns in exclusive 
12790 0a 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e  .  ** mode, then
12791 20 73 75 62 73 65 71 75 65 6e 74 20 74 72 61 6e   subsequent tran
12792 73 61 63 74 69 6f 6e 73 20 70 65 72 66 6f 72 6d  sactions perform
12793 65 64 20 62 79 20 74 68 65 20 63 6f 6e 6e 65 63  ed by the connec
12794 74 69 6f 6e 20 77 69 6c 6c 20 6e 6f 74 0a 20 20  tion will not.  
12795 2a 2a 20 75 70 64 61 74 65 20 74 68 65 20 63 68  ** update the ch
12796 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 61 74 20  ange-counter at 
12797 61 6c 6c 2e 20 54 68 69 73 20 6d 61 79 20 6c 65  all. This may le
12798 61 64 20 74 6f 20 63 61 63 68 65 20 69 6e 63 6f  ad to cache inco
12799 6e 73 69 73 74 65 6e 63 79 0a 20 20 2a 2a 20 70  nsistency.  ** p
1279a 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 74 68 65  roblems for othe
1279b 72 20 70 72 6f 63 65 73 73 65 73 20 61 74 20 73  r processes at s
1279c 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65  ome point in the
1279d 20 66 75 74 75 72 65 2e 20 53 6f 2c 20 6a 75 73   future. So, jus
1279e 74 0a 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74  t.  ** in case t
1279f 68 69 73 20 68 61 73 20 68 61 70 70 65 6e 65 64  his has happened
127a0 2c 20 63 6c 65 61 72 20 74 68 65 20 63 68 61 6e  , clear the chan
127a1 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67  geCountDone flag
127a2 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 70 50 61   now..  */.  pPa
127a3 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74  ger->changeCount
127a4 44 6f 6e 65 20 3d 20 70 50 61 67 65 72 2d 3e 74  Done = pPager->t
127a5 65 6d 70 46 69 6c 65 3b 0a 0a 20 20 69 66 28 20  empFile;..  if( 
127a6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
127a7 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 70  .    zMaster = p
127a8 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
127a9 3b 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4d  ;.    rc = readM
127aa 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61  asterJournal(pPa
127ab 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65  ger->jfd, zMaste
127ac 72 2c 20 70 50 61 67 65 72 2d 3e 70 56 66 73 2d  r, pPager->pVfs-
127ad 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a  >mxPathname+1);.
127ae 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63      testcase( rc
127af 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
127b0 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
127b1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63  ITE_OK ){.    rc
127b2 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61   = pager_end_tra
127b3 6e 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c  nsaction(pPager,
127b4 20 7a 4d 61 73 74 65 72 5b 30 5d 21 3d 27 5c 30   zMaster[0]!='\0
127b5 27 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  ');.    testcase
127b6 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
127b7 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
127b8 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d  =SQLITE_OK && zM
127b9 61 73 74 65 72 5b 30 5d 20 26 26 20 72 65 73 20  aster[0] && res 
127ba 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ){.    /* If the
127bb 72 65 20 77 61 73 20 61 20 6d 61 73 74 65 72 20  re was a master 
127bc 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68 69 73  journal and this
127bd 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65   routine will re
127be 74 75 72 6e 20 73 75 63 63 65 73 73 2c 0a 20 20  turn success,.  
127bf 20 20 2a 2a 20 73 65 65 20 69 66 20 69 74 20 69    ** see if it i
127c0 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65  s possible to de
127c1 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20  lete the master 
127c2 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20 20 2a 2f 0a  journal..    */.
127c3 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 64      rc = pager_d
127c4 65 6c 6d 61 73 74 65 72 28 70 50 61 67 65 72 2c  elmaster(pPager,
127c5 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 74   zMaster);.    t
127c6 65 73 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c  estcase( rc!=SQL
127c7 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20  ITE_OK );.  }.. 
127c8 20 2f 2a 20 54 68 65 20 50 61 67 65 72 2e 73 65   /* The Pager.se
127c9 63 74 6f 72 53 69 7a 65 20 76 61 72 69 61 62 6c  ctorSize variabl
127ca 65 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20  e may have been 
127cb 75 70 64 61 74 65 64 20 77 68 69 6c 65 20 72 6f  updated while ro
127cc 6c 6c 69 6e 67 0a 20 20 2a 2a 20 62 61 63 6b 20  lling.  ** back 
127cd 61 20 6a 6f 75 72 6e 61 6c 20 63 72 65 61 74 65  a journal create
127ce 64 20 62 79 20 61 20 70 72 6f 63 65 73 73 20 77  d by a process w
127cf 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74 20  ith a different 
127d0 73 65 63 74 6f 72 20 73 69 7a 65 0a 20 20 2a 2a  sector size.  **
127d1 20 76 61 6c 75 65 2e 20 52 65 73 65 74 20 69 74   value. Reset it
127d2 20 74 6f 20 74 68 65 20 63 6f 72 72 65 63 74 20   to the correct 
127d3 76 61 6c 75 65 20 66 6f 72 20 74 68 69 73 20 70  value for this p
127d4 72 6f 63 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73  rocess..  */.  s
127d5 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61  etSectorSize(pPa
127d6 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ger);.  return r
127d7 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79  c;.}../*.** Play
127d8 62 61 63 6b 20 73 61 76 65 70 6f 69 6e 74 20 70  back savepoint p
127d9 53 61 76 65 70 6f 69 6e 74 2e 20 4f 72 2c 20 69  Savepoint. Or, i
127da 66 20 70 53 61 76 65 70 6f 69 6e 74 3d 3d 4e 55  f pSavepoint==NU
127db 4c 4c 2c 20 74 68 65 6e 20 70 6c 61 79 62 61 63  LL, then playbac
127dc 6b 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20  k.** the entire 
127dd 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
127de 69 6c 65 2e 20 54 68 65 20 63 61 73 65 20 70 53  ile. The case pS
127df 61 76 65 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 20 6f  avepoint==NULL o
127e0 63 63 75 72 73 20 77 68 65 6e 20 0a 2a 2a 20 61  ccurs when .** a
127e1 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 63 6f 6d   ROLLBACK TO com
127e2 6d 61 6e 64 20 69 73 20 69 6e 76 6f 6b 65 64 20  mand is invoked 
127e3 6f 6e 20 61 20 53 41 56 45 50 4f 49 4e 54 20 74  on a SAVEPOINT t
127e4 68 61 74 20 69 73 20 61 20 74 72 61 6e 73 61 63  hat is a transac
127e5 74 69 6f 6e 20 0a 2a 2a 20 73 61 76 65 70 6f 69  tion .** savepoi
127e6 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70  nt..**.** When p
127e7 53 61 76 65 70 6f 69 6e 74 20 69 73 20 6e 6f 74  Savepoint is not
127e8 20 4e 55 4c 4c 20 28 6d 65 61 6e 69 6e 67 20 61   NULL (meaning a
127e9 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e   non-transaction
127ea 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20 0a 2a   savepoint is .*
127eb 2a 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62  * being rolled b
127ec 61 63 6b 29 2c 20 74 68 65 6e 20 74 68 65 20 72  ack), then the r
127ed 6f 6c 6c 62 61 63 6b 20 63 6f 6e 73 69 73 74 73  ollback consists
127ee 20 6f 66 20 75 70 20 74 6f 20 74 68 72 65 65 20   of up to three 
127ef 73 74 61 67 65 73 2c 0a 2a 2a 20 70 65 72 66 6f  stages,.** perfo
127f0 72 6d 65 64 20 69 6e 20 74 68 65 20 6f 72 64 65  rmed in the orde
127f1 72 20 73 70 65 63 69 66 69 65 64 3a 0a 2a 2a 0a  r specified:.**.
127f2 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 61 72 65  **   * Pages are
127f3 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f   played back fro
127f4 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  m the main journ
127f5 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 20 62  al starting at b
127f6 79 74 65 0a 2a 2a 20 20 20 20 20 6f 66 66 73 65  yte.**     offse
127f7 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  t PagerSavepoint
127f8 2e 69 4f 66 66 73 65 74 20 61 6e 64 20 63 6f 6e  .iOffset and con
127f9 74 69 6e 75 69 6e 67 20 74 6f 20 0a 2a 2a 20 20  tinuing to .**  
127fa 20 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e     PagerSavepoin
127fb 74 2e 69 48 64 72 4f 66 66 73 65 74 2c 20 6f 72  t.iHdrOffset, or
127fc 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
127fd 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 0a  he main journal.
127fe 2a 2a 20 20 20 20 20 66 69 6c 65 20 69 66 20 50  **     file if P
127ff 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48  agerSavepoint.iH
12800 64 72 4f 66 66 73 65 74 20 69 73 20 7a 65 72 6f  drOffset is zero
12801 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 50  ..**.**   * If P
12802 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 48  agerSavepoint.iH
12803 64 72 4f 66 66 73 65 74 20 69 73 20 6e 6f 74 20  drOffset is not 
12804 7a 65 72 6f 2c 20 74 68 65 6e 20 70 61 67 65 73  zero, then pages
12805 20 61 72 65 20 70 6c 61 79 65 64 0a 2a 2a 20 20   are played.**  
12806 20 20 20 62 61 63 6b 20 73 74 61 72 74 69 6e 67     back starting
12807 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
12808 6c 20 68 65 61 64 65 72 20 69 6d 6d 65 64 69 61  l header immedia
12809 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a  tely following .
1280a 2a 2a 20 20 20 20 20 50 61 67 65 72 53 61 76 65  **     PagerSave
1280b 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74  point.iHdrOffset
1280c 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
1280d 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  he main journal 
1280e 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  file..**.**   * 
1280f 50 61 67 65 73 20 61 72 65 20 74 68 65 6e 20 70  Pages are then p
12810 6c 61 79 65 64 20 62 61 63 6b 20 66 72 6f 6d 20  layed back from 
12811 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20  the sub-journal 
12812 66 69 6c 65 2c 20 73 74 61 72 74 69 6e 67 0a 2a  file, starting.*
12813 2a 20 20 20 20 20 77 69 74 68 20 74 68 65 20 50  *     with the P
12814 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69 53  agerSavepoint.iS
12815 75 62 52 65 63 20 61 6e 64 20 63 6f 6e 74 69 6e  ubRec and contin
12816 75 69 6e 67 20 74 6f 20 74 68 65 20 65 6e 64 20  uing to the end 
12817 6f 66 0a 2a 2a 20 20 20 20 20 74 68 65 20 6a 6f  of.**     the jo
12818 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  urnal file..**.*
12819 2a 20 54 68 72 6f 75 67 68 6f 75 74 20 74 68 65  * Throughout the
1281a 20 72 6f 6c 6c 62 61 63 6b 20 70 72 6f 63 65 73   rollback proces
1281b 73 2c 20 65 61 63 68 20 74 69 6d 65 20 61 20 70  s, each time a p
1281c 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 20 62 61  age is rolled ba
1281d 63 6b 2c 20 74 68 65 0a 2a 2a 20 63 6f 72 72 65  ck, the.** corre
1281e 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 20  sponding bit is 
1281f 73 65 74 20 69 6e 20 61 20 62 69 74 76 65 63 20  set in a bitvec 
12820 73 74 72 75 63 74 75 72 65 20 28 76 61 72 69 61  structure (varia
12821 62 6c 65 20 70 44 6f 6e 65 20 69 6e 20 74 68 65  ble pDone in the
12822 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
12823 6f 6e 20 62 65 6c 6f 77 29 2e 20 54 68 69 73 20  on below). This 
12824 69 73 20 75 73 65 64 20 74 6f 20 65 6e 73 75 72  is used to ensur
12825 65 20 74 68 61 74 20 61 20 70 61 67 65 20 69 73  e that a page is
12826 20 6f 6e 6c 79 0a 2a 2a 20 72 6f 6c 6c 65 64 20   only.** rolled 
12827 62 61 63 6b 20 74 68 65 20 66 69 72 73 74 20 74  back the first t
12828 69 6d 65 20 69 74 20 69 73 20 65 6e 63 6f 75 6e  ime it is encoun
12829 74 65 72 65 64 20 69 6e 20 65 69 74 68 65 72 20  tered in either 
1282a 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49  journal..**.** I
1282b 66 20 70 53 61 76 65 70 6f 69 6e 74 20 69 73 20  f pSavepoint is 
1282c 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 61 67 65 73  NULL, then pages
1282d 20 61 72 65 20 6f 6e 6c 79 20 70 6c 61 79 65 64   are only played
1282e 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d   back from the m
1282f 61 69 6e 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  ain.** journal f
12830 69 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e 6f  ile. There is no
12831 20 6e 65 65 64 20 66 6f 72 20 61 20 62 69 74 76   need for a bitv
12832 65 63 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ec in this case.
12833 0a 2a 2a 0a 2a 2a 20 49 6e 20 65 69 74 68 65 72  .**.** In either
12834 20 63 61 73 65 2c 20 62 65 66 6f 72 65 20 70 6c   case, before pl
12835 61 79 62 61 63 6b 20 63 6f 6d 6d 65 6e 63 65 73  ayback commences
12836 20 74 68 65 20 50 61 67 65 72 2e 64 62 53 69 7a   the Pager.dbSiz
12837 65 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73  e variable.** is
12838 20 72 65 73 65 74 20 74 6f 20 74 68 65 20 76 61   reset to the va
12839 6c 75 65 20 74 68 61 74 20 69 74 20 68 65 6c 64  lue that it held
1283a 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
1283b 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 0a   the savepoint .
1283c 2a 2a 20 28 6f 72 20 74 72 61 6e 73 61 63 74 69  ** (or transacti
1283d 6f 6e 29 2e 20 4e 6f 20 70 61 67 65 20 77 69 74  on). No page wit
1283e 68 20 61 20 70 61 67 65 2d 6e 75 6d 62 65 72 20  h a page-number 
1283f 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 69  greater than thi
12840 73 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 6c  s value.** is pl
12841 61 79 65 64 20 62 61 63 6b 2e 20 49 66 20 6f 6e  ayed back. If on
12842 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  e is encountered
12843 20 69 74 20 69 73 20 73 69 6d 70 6c 79 20 73 6b   it is simply sk
12844 69 70 70 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ipped..*/.static
12845 20 69 6e 74 20 70 61 67 65 72 50 6c 61 79 62 61   int pagerPlayba
12846 63 6b 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65  ckSavepoint(Page
12847 72 20 2a 70 50 61 67 65 72 2c 20 50 61 67 65 72  r *pPager, Pager
12848 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65  Savepoint *pSave
12849 70 6f 69 6e 74 29 7b 0a 20 20 69 36 34 20 73 7a  point){.  i64 sz
1284a 4a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  J;              
1284b 20 20 20 2f 2a 20 45 66 66 65 63 74 69 76 65 20     /* Effective 
1284c 73 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 69 6e  size of the main
1284d 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36   journal */.  i6
1284e 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20  4 iHdrOff;      
1284f 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66         /* End of
12850 20 66 69 72 73 74 20 73 65 67 6d 65 6e 74 20 6f   first segment o
12851 66 20 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 20 72  f main-journal r
12852 65 63 6f 72 64 73 20 2a 2f 0a 20 20 69 6e 74 20  ecords */.  int 
12853 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
12854 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
12855 6f 64 65 20 2a 2f 0a 20 20 42 69 74 76 65 63 20  ode */.  Bitvec 
12856 2a 70 44 6f 6e 65 20 3d 20 30 3b 20 20 20 20 20  *pDone = 0;     
12857 20 20 2f 2a 20 42 69 74 76 65 63 20 74 6f 20 65    /* Bitvec to e
12858 6e 73 75 72 65 20 70 61 67 65 73 20 70 6c 61 79  nsure pages play
12859 65 64 20 62 61 63 6b 20 6f 6e 6c 79 20 6f 6e 63  ed back only onc
1285a 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
1285b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
1285c 41 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a  AGER_SHARED );..
1285d 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20    /* Allocate a 
1285e 62 69 74 76 65 63 20 74 6f 20 75 73 65 20 74 6f  bitvec to use to
1285f 20 73 74 6f 72 65 20 74 68 65 20 73 65 74 20 6f   store the set o
12860 66 20 70 61 67 65 73 20 72 6f 6c 6c 65 64 20 62  f pages rolled b
12861 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 53 61  ack */.  if( pSa
12862 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 70  vepoint ){.    p
12863 44 6f 6e 65 20 3d 20 73 71 6c 69 74 65 33 42 69  Done = sqlite3Bi
12864 74 76 65 63 43 72 65 61 74 65 28 70 53 61 76 65  tvecCreate(pSave
12865 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67 29 3b 0a 20  point->nOrig);. 
12866 20 20 20 69 66 28 20 21 70 44 6f 6e 65 20 29 7b     if( !pDone ){
12867 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
12868 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
12869 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  }.  }..  /* Set 
1286a 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a  the database siz
1286b 65 20 62 61 63 6b 20 74 6f 20 74 68 65 20 76 61  e back to the va
1286c 6c 75 65 20 69 74 20 77 61 73 20 62 65 66 6f 72  lue it was befor
1286d 65 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20  e the savepoint 
1286e 0a 20 20 2a 2a 20 62 65 69 6e 67 20 72 65 76 65  .  ** being reve
1286f 72 74 65 64 20 77 61 73 20 6f 70 65 6e 65 64 2e  rted was opened.
12870 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e  .  */.  pPager->
12871 64 62 53 69 7a 65 20 3d 20 70 53 61 76 65 70 6f  dbSize = pSavepo
12872 69 6e 74 20 3f 20 70 53 61 76 65 70 6f 69 6e 74  int ? pSavepoint
12873 2d 3e 6e 4f 72 69 67 20 3a 20 70 50 61 67 65 72  ->nOrig : pPager
12874 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 0a 20  ->dbOrigSize;.. 
12875 20 2f 2a 20 55 73 65 20 70 50 61 67 65 72 2d 3e   /* Use pPager->
12876 6a 6f 75 72 6e 61 6c 4f 66 66 20 61 73 20 74 68  journalOff as th
12877 65 20 65 66 66 65 63 74 69 76 65 20 73 69 7a 65  e effective size
12878 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c   of the main rol
12879 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e  lback.  ** journ
1287a 61 6c 2e 20 20 54 68 65 20 61 63 74 75 61 6c 20  al.  The actual 
1287b 66 69 6c 65 20 6d 69 67 68 74 20 62 65 20 6c 61  file might be la
1287c 72 67 65 72 20 74 68 61 6e 20 74 68 69 73 20 69  rger than this i
1287d 6e 0a 20 20 2a 2a 20 50 41 47 45 52 5f 4a 4f 55  n.  ** PAGER_JOU
1287e 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54  RNALMODE_TRUNCAT
1287f 45 20 6f 72 20 50 41 47 45 52 5f 4a 4f 55 52 4e  E or PAGER_JOURN
12880 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 2e 20  ALMODE_PERSIST. 
12881 20 42 75 74 20 61 6e 79 74 68 69 6e 67 0a 20 20   But anything.  
12882 2a 2a 20 70 61 73 74 20 70 50 61 67 65 72 2d 3e  ** past pPager->
12883 6a 6f 75 72 6e 61 6c 4f 66 66 20 69 73 20 6f 66  journalOff is of
12884 66 2d 6c 69 6d 69 74 73 20 74 6f 20 75 73 2e 0a  f-limits to us..
12885 20 20 2a 2f 0a 20 20 73 7a 4a 20 3d 20 70 50 61    */.  szJ = pPa
12886 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
12887 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 62 79 20  ..  /* Begin by 
12888 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 72 65 63  rolling back rec
12889 6f 72 64 73 20 66 72 6f 6d 20 74 68 65 20 6d 61  ords from the ma
1288a 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74  in journal start
1288b 69 6e 67 20 61 74 0a 20 20 2a 2a 20 50 61 67 65  ing at.  ** Page
1288c 72 53 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73  rSavepoint.iOffs
1288d 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e  et and continuin
1288e 67 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6a 6f  g to the next jo
1288f 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 0a 20 20  urnal header..  
12890 2a 2a 20 54 68 65 72 65 20 6d 69 67 68 74 20 62  ** There might b
12891 65 20 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65  e records in the
12892 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 74 68   main journal th
12893 61 74 20 68 61 76 65 20 61 20 70 61 67 65 20 6e  at have a page n
12894 75 6d 62 65 72 0a 20 20 2a 2a 20 67 72 65 61 74  umber.  ** great
12895 65 72 20 74 68 61 6e 20 74 68 65 20 63 75 72 72  er than the curr
12896 65 6e 74 20 64 61 74 61 62 61 73 65 20 73 69 7a  ent database siz
12897 65 20 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  e (pPager->dbSiz
12898 65 29 20 62 75 74 20 74 68 6f 73 65 0a 20 20 2a  e) but those.  *
12899 2a 20 77 69 6c 6c 20 62 65 20 73 6b 69 70 70 65  * will be skippe
1289a 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e  d automatically.
1289b 20 20 50 61 67 65 73 20 61 72 65 20 61 64 64 65    Pages are adde
1289c 64 20 74 6f 20 70 44 6f 6e 65 20 61 73 20 74 68  d to pDone as th
1289d 65 79 0a 20 20 2a 2a 20 61 72 65 20 70 6c 61 79  ey.  ** are play
1289e 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20  ed back..  */.  
1289f 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29  if( pSavepoint )
128a0 7b 0a 20 20 20 20 69 48 64 72 4f 66 66 20 3d 20  {.    iHdrOff = 
128a1 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48 64 72  pSavepoint->iHdr
128a2 4f 66 66 73 65 74 20 3f 20 70 53 61 76 65 70 6f  Offset ? pSavepo
128a3 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20  int->iHdrOffset 
128a4 3a 20 73 7a 4a 3b 0a 20 20 20 20 70 50 61 67 65  : szJ;.    pPage
128a5 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
128a6 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 4f 66 66  pSavepoint->iOff
128a7 73 65 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  set;.    while( 
128a8 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
128a9 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
128aa 4f 66 66 3c 69 48 64 72 4f 66 66 20 29 7b 0a 20  Off<iHdrOff ){. 
128ab 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
128ac 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67  playback_one_pag
128ad 65 28 70 50 61 67 65 72 2c 20 31 2c 20 30 2c 20  e(pPager, 1, 0, 
128ae 26 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  &pPager->journal
128af 4f 66 66 2c 20 31 2c 20 70 44 6f 6e 65 29 3b 0a  Off, 1, pDone);.
128b0 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
128b1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
128b2 45 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  E );.  }else{.  
128b3 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
128b4 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20  lOff = 0;.  }.. 
128b5 20 2f 2a 20 43 6f 6e 74 69 6e 75 65 20 72 6f 6c   /* Continue rol
128b6 6c 69 6e 67 20 62 61 63 6b 20 72 65 63 6f 72 64  ling back record
128b7 73 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 69  s out of the mai
128b8 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72 74 69  n journal starti
128b9 6e 67 20 61 74 0a 20 20 2a 2a 20 74 68 65 20 66  ng at.  ** the f
128ba 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61  irst journal hea
128bb 64 65 72 20 73 65 65 6e 20 61 6e 64 20 63 6f 6e  der seen and con
128bc 74 69 6e 75 69 6e 67 20 75 6e 74 69 6c 20 74 68  tinuing until th
128bd 65 20 65 66 66 65 63 74 69 76 65 20 65 6e 64 0a  e effective end.
128be 20 20 2a 2a 20 6f 66 20 74 68 65 20 6d 61 69 6e    ** of the main
128bf 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20   journal file.  
128c0 43 6f 6e 74 69 6e 75 65 20 74 6f 20 73 6b 69 70  Continue to skip
128c1 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 70 61   out-of-range pa
128c2 67 65 73 20 61 6e 64 0a 20 20 2a 2a 20 63 6f 6e  ges and.  ** con
128c3 74 69 6e 75 65 20 61 64 64 69 6e 67 20 70 61 67  tinue adding pag
128c4 65 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74  es rolled back t
128c5 6f 20 70 44 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20  o pDone..  */.  
128c6 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
128c7 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e  E_OK && pPager->
128c8 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a 4a 20 29  journalOff<szJ )
128c9 7b 0a 20 20 20 20 75 33 32 20 69 69 3b 20 20 20  {.    u32 ii;   
128ca 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
128cb 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
128cc 75 33 32 20 6e 4a 52 65 63 20 3d 20 30 3b 20 20  u32 nJRec = 0;  
128cd 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
128ce 4a 6f 75 72 6e 61 6c 20 52 65 63 6f 72 64 73 20  Journal Records 
128cf 2a 2f 0a 20 20 20 20 75 33 32 20 64 75 6d 6d 79  */.    u32 dummy
128d0 3b 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a  ;.    rc = readJ
128d1 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72  ournalHdr(pPager
128d2 2c 20 30 2c 20 73 7a 4a 2c 20 26 6e 4a 52 65 63  , 0, szJ, &nJRec
128d3 2c 20 26 64 75 6d 6d 79 29 3b 0a 20 20 20 20 61  , &dummy);.    a
128d4 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
128d5 45 5f 44 4f 4e 45 20 29 3b 0a 0a 20 20 20 20 2f  E_DONE );..    /
128d6 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 22 70 50  *.    ** The "pP
128d7 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
128d8 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  +JOURNAL_HDR_SZ(
128d9 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d  pPager)==pPager-
128da 3e 6a 6f 75 72 6e 61 6c 4f 66 66 22 0a 20 20 20  >journalOff".   
128db 20 2a 2a 20 74 65 73 74 20 69 73 20 72 65 6c 61   ** test is rela
128dc 74 65 64 20 74 6f 20 74 69 63 6b 65 74 20 23 32  ted to ticket #2
128dd 35 36 35 2e 20 20 53 65 65 20 74 68 65 20 64 69  565.  See the di
128de 73 63 75 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a  scussion in the.
128df 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61      ** pager_pla
128e0 79 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f 6e  yback() function
128e1 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
128e2 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 20  information..   
128e3 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 4a 52 65   */.    if( nJRe
128e4 63 3d 3d 30 20 0a 20 20 20 20 20 26 26 20 70 50  c==0 .     && pP
128e5 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72  ager->journalHdr
128e6 2b 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  +JOURNAL_HDR_SZ(
128e7 70 50 61 67 65 72 29 3d 3d 70 50 61 67 65 72 2d  pPager)==pPager-
128e8 3e 6a 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 20 20  >journalOff.    
128e9 29 7b 0a 20 20 20 20 20 20 6e 4a 52 65 63 20 3d  ){.      nJRec =
128ea 20 28 75 33 32 29 28 28 73 7a 4a 20 2d 20 70 50   (u32)((szJ - pP
128eb 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
128ec 29 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28  )/JOURNAL_PG_SZ(
128ed 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a  pPager));.    }.
128ee 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63      for(ii=0; rc
128ef 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
128f0 69 3c 6e 4a 52 65 63 20 26 26 20 70 50 61 67 65  i<nJRec && pPage
128f1 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 73 7a  r->journalOff<sz
128f2 4a 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20  J; ii++){.      
128f3 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62  rc = pager_playb
128f4 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61  ack_one_page(pPa
128f5 67 65 72 2c 20 31 2c 20 30 2c 20 26 70 50 61 67  ger, 1, 0, &pPag
128f6 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20  er->journalOff, 
128f7 31 2c 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d  1, pDone);.    }
128f8 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21  .    assert( rc!
128f9 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a  =SQLITE_DONE );.
128fa 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 72 63    }.  assert( rc
128fb 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  !=SQLITE_OK || p
128fc 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
128fd 66 3d 3d 73 7a 4a 20 29 3b 0a 0a 20 20 2f 2a 20  f==szJ );..  /* 
128fe 46 69 6e 61 6c 6c 79 2c 20 20 72 6f 6c 6c 62 61  Finally,  rollba
128ff 63 6b 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68  ck pages from th
12900 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e 20 20  e sub-journal.  
12901 50 61 67 65 20 74 68 61 74 20 77 65 72 65 0a 20  Page that were. 
12902 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72   ** previously r
12903 6f 6c 6c 65 64 20 62 61 63 6b 20 6f 75 74 20 6f  olled back out o
12904 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  f the main journ
12905 61 6c 20 28 61 6e 64 20 61 72 65 20 68 65 6e 63  al (and are henc
12906 65 20 69 6e 20 70 44 6f 6e 65 29 0a 20 20 2a 2a  e in pDone).  **
12907 20 77 69 6c 6c 20 62 65 20 73 6b 69 70 70 65 64   will be skipped
12908 2e 20 20 4f 75 74 2d 6f 66 2d 72 61 6e 67 65 20  .  Out-of-range 
12909 70 61 67 65 73 20 61 72 65 20 61 6c 73 6f 20 73  pages are also s
1290a 6b 69 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  kipped..  */.  i
1290b 66 28 20 70 53 61 76 65 70 6f 69 6e 74 20 29 7b  f( pSavepoint ){
1290c 0a 20 20 20 20 75 33 32 20 69 69 3b 20 20 20 20  .    u32 ii;    
1290d 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1290e 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69  counter */.    i
1290f 36 34 20 6f 66 66 73 65 74 20 3d 20 70 53 61 76  64 offset = pSav
12910 65 70 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 2a  epoint->iSubRec*
12911 28 34 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53  (4+pPager->pageS
12912 69 7a 65 29 3b 0a 20 20 20 20 66 6f 72 28 69 69  ize);.    for(ii
12913 3d 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 53 75  =pSavepoint->iSu
12914 62 52 65 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45  bRec; rc==SQLITE
12915 5f 4f 4b 20 26 26 20 69 69 3c 70 50 61 67 65 72  _OK && ii<pPager
12916 2d 3e 6e 53 75 62 52 65 63 3b 20 69 69 2b 2b 29  ->nSubRec; ii++)
12917 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
12918 6f 66 66 73 65 74 3d 3d 69 69 2a 28 34 2b 70 50  offset==ii*(4+pP
12919 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20  ager->pageSize) 
1291a 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  );.      rc = pa
1291b 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65  ger_playback_one
1291c 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 30 2c  _page(pPager, 0,
1291d 20 30 2c 20 26 6f 66 66 73 65 74 2c 20 31 2c 20   0, &offset, 1, 
1291e 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pDone);.    }.  
1291f 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
12920 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d  LITE_DONE );.  }
12921 0a 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65  ..  sqlite3Bitve
12922 63 44 65 73 74 72 6f 79 28 70 44 6f 6e 65 29 3b  cDestroy(pDone);
12923 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
12924 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67  E_OK ){.    pPag
12925 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d  er->journalOff =
12926 20 73 7a 4a 3b 0a 20 20 7d 0a 20 20 72 65 74 75   szJ;.  }.  retu
12927 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
12928 43 68 61 6e 67 65 20 74 68 65 20 6d 61 78 69 6d  Change the maxim
12929 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d  um number of in-
1292a 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 68 61  memory pages tha
1292b 74 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a  t are allowed..*
1292c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1292d 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
1292e 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 50  erSetCachesize(P
1292f 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
12930 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 73 71 6c  t mxPage){.  sql
12931 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63  ite3PcacheSetCac
12932 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70  hesize(pPager->p
12933 50 43 61 63 68 65 2c 20 6d 78 50 61 67 65 29 3b  PCache, mxPage);
12934 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74  .}../*.** Adjust
12935 20 74 68 65 20 72 6f 62 75 73 74 6e 65 73 73 20   the robustness 
12936 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
12937 74 6f 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f  to damage due to
12938 20 4f 53 20 63 72 61 73 68 65 73 0a 2a 2a 20 6f   OS crashes.** o
12939 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 73  r power failures
1293a 20 62 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65   by changing the
1293b 20 6e 75 6d 62 65 72 20 6f 66 20 73 79 6e 63 73   number of syncs
1293c 28 29 73 20 77 68 65 6e 20 77 72 69 74 69 6e 67  ()s when writing
1293d 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  .** the rollback
1293e 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 72 65   journal.  There
1293f 20 61 72 65 20 74 68 72 65 65 20 6c 65 76 65 6c   are three level
12940 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4f 46 46 20  s:.**.**    OFF 
12941 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53        sqlite3OsS
12942 79 6e 63 28 29 20 69 73 20 6e 65 76 65 72 20 63  ync() is never c
12943 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 73 20  alled.  This is 
12944 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20  the default.**  
12945 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 20              for 
12946 74 65 6d 70 6f 72 61 72 79 20 61 6e 64 20 74 72  temporary and tr
12947 61 6e 73 69 65 6e 74 20 66 69 6c 65 73 2e 0a 2a  ansient files..*
12948 2a 0a 2a 2a 20 20 20 20 4e 4f 52 4d 41 4c 20 20  *.**    NORMAL  
12949 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73    The journal is
1294a 20 73 79 6e 63 65 64 20 6f 6e 63 65 20 62 65 66   synced once bef
1294b 6f 72 65 20 77 72 69 74 65 73 20 62 65 67 69 6e  ore writes begin
1294c 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   on the.**      
1294d 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65          database
1294e 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61  .  This is norma
1294f 6c 6c 79 20 61 64 65 71 75 61 74 65 20 70 72 6f  lly adequate pro
12950 74 65 63 74 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20  tection, but.** 
12951 20 20 20 20 20 20 20 20 20 20 20 20 20 69 74 20               it 
12952 69 73 20 74 68 65 6f 72 65 74 69 63 61 6c 6c 79  is theoretically
12953 20 70 6f 73 73 69 62 6c 65 2c 20 74 68 6f 75 67   possible, thoug
12954 68 20 76 65 72 79 20 75 6e 6c 69 6b 65 6c 79 2c  h very unlikely,
12955 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
12956 20 74 68 61 74 20 61 6e 20 69 6e 6f 70 65 72 74   that an inopert
12957 75 6e 65 20 70 6f 77 65 72 20 66 61 69 6c 75 72  une power failur
12958 65 20 63 6f 75 6c 64 20 6c 65 61 76 65 20 74 68  e could leave th
12959 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20  e journal.**    
1295a 20 20 20 20 20 20 20 20 20 20 69 6e 20 61 20 73            in a s
1295b 74 61 74 65 20 77 68 69 63 68 20 77 6f 75 6c 64  tate which would
1295c 20 63 61 75 73 65 20 64 61 6d 61 67 65 20 74 6f   cause damage to
1295d 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
1295e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68                wh
1295f 65 6e 20 69 74 20 69 73 20 72 6f 6c 6c 65 64 20  en it is rolled 
12960 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46  back..**.**    F
12961 55 4c 4c 20 20 20 20 20 20 54 68 65 20 6a 6f 75  ULL      The jou
12962 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 74  rnal is synced t
12963 77 69 63 65 20 62 65 66 6f 72 65 20 77 72 69 74  wice before writ
12964 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a  es begin on the.
12965 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
12966 64 61 74 61 62 61 73 65 20 28 77 69 74 68 20 73  database (with s
12967 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  ome additional i
12968 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 74 68 65  nformation - the
12969 20 6e 52 65 63 20 66 69 65 6c 64 0a 2a 2a 20 20   nRec field.**  
1296a 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
1296b 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
1296c 72 20 2d 20 62 65 69 6e 67 20 77 72 69 74 74 65  r - being writte
1296d 6e 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65  n in between the
1296e 20 74 77 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20   two.**         
1296f 20 20 20 20 20 73 79 6e 63 73 29 2e 20 20 49 66       syncs).  If
12970 20 77 65 20 61 73 73 75 6d 65 20 74 68 61 74 20   we assume that 
12971 77 72 69 74 69 6e 67 20 61 0a 2a 2a 20 20 20 20  writing a.**    
12972 20 20 20 20 20 20 20 20 20 20 73 69 6e 67 6c 65            single
12973 20 64 69 73 6b 20 73 65 63 74 6f 72 20 69 73 20   disk sector is 
12974 61 74 6f 6d 69 63 2c 20 74 68 65 6e 20 74 68 69  atomic, then thi
12975 73 20 6d 6f 64 65 20 70 72 6f 76 69 64 65 73 0a  s mode provides.
12976 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
12977 61 73 73 75 72 61 6e 63 65 20 74 68 61 74 20 74  assurance that t
12978 68 65 20 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20  he journal will 
12979 6e 6f 74 20 62 65 20 63 6f 72 72 75 70 74 65 64  not be corrupted
1297a 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   to the.**      
1297b 20 20 20 20 20 20 20 20 70 6f 69 6e 74 20 6f 66          point of
1297c 20 63 61 75 73 69 6e 67 20 64 61 6d 61 67 65 20   causing damage 
1297d 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
1297e 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e  during rollback.
1297f 0a 2a 2a 0a 2a 2a 20 4e 75 6d 65 72 69 63 20 76  .**.** Numeric v
12980 61 6c 75 65 73 20 61 73 73 6f 63 69 61 74 65 64  alues associated
12981 20 77 69 74 68 20 74 68 65 73 65 20 73 74 61 74   with these stat
12982 65 73 20 61 72 65 20 4f 46 46 3d 3d 31 2c 20 4e  es are OFF==1, N
12983 4f 52 4d 41 4c 3d 32 2c 0a 2a 2a 20 61 6e 64 20  ORMAL=2,.** and 
12984 46 55 4c 4c 3d 33 2e 0a 2a 2f 0a 23 69 66 6e 64  FULL=3..*/.#ifnd
12985 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
12986 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c  AGER_PRAGMAS.SQL
12987 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
12988 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
12989 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65  SafetyLevel(Page
1298a 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c  r *pPager, int l
1298b 65 76 65 6c 2c 20 69 6e 74 20 62 46 75 6c 6c 46  evel, int bFullF
1298c 73 79 6e 63 29 7b 0a 20 20 70 50 61 67 65 72 2d  sync){.  pPager-
1298d 3e 6e 6f 53 79 6e 63 20 3d 20 20 28 6c 65 76 65  >noSync =  (leve
1298e 6c 3d 3d 31 20 7c 7c 20 70 50 61 67 65 72 2d 3e  l==1 || pPager->
1298f 74 65 6d 70 46 69 6c 65 29 20 3f 31 3a 30 3b 0a  tempFile) ?1:0;.
12990 20 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79    pPager->fullSy
12991 6e 63 20 3d 20 28 6c 65 76 65 6c 3d 3d 33 20 26  nc = (level==3 &
12992 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  & !pPager->tempF
12993 69 6c 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61  ile) ?1:0;.  pPa
12994 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20  ger->sync_flags 
12995 3d 20 28 62 46 75 6c 6c 46 73 79 6e 63 3f 53 51  = (bFullFsync?SQ
12996 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 3a 53  LITE_SYNC_FULL:S
12997 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41  QLITE_SYNC_NORMA
12998 4c 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  L);.  if( pPager
12999 2d 3e 6e 6f 53 79 6e 63 20 29 20 70 50 61 67 65  ->noSync ) pPage
1299a 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  r->needSync = 0;
1299b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1299c 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67   The following g
1299d 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69  lobal variable i
1299e 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68  s incremented wh
1299f 65 6e 65 76 65 72 20 74 68 65 20 6c 69 62 72 61  enever the libra
129a0 72 79 0a 2a 2a 20 61 74 74 65 6d 70 74 73 20 74  ry.** attempts t
129a1 6f 20 6f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61  o open a tempora
129a2 72 79 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69  ry file.  This i
129a3 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75 73  nformation is us
129a4 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e  ed for.** testin
129a5 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f  g and analysis o
129a6 6e 6c 79 2e 20 20 0a 2a 2f 0a 23 69 66 64 65 66  nly.  .*/.#ifdef
129a7 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
129a8 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
129a9 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75  te3_opentemp_cou
129aa 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
129ab 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 74 65 6d  /*.** Open a tem
129ac 70 6f 72 61 72 79 20 66 69 6c 65 2e 0a 2a 2a 0a  porary file..**.
129ad 2a 2a 20 57 72 69 74 65 20 74 68 65 20 66 69 6c  ** Write the fil
129ae 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 6e 74  e descriptor int
129af 6f 20 2a 70 46 69 6c 65 2e 20 52 65 74 75 72 6e  o *pFile. Return
129b0 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
129b1 63 63 65 73 73 20 0a 2a 2a 20 6f 72 20 73 6f 6d  ccess .** or som
129b2 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f  e other error co
129b3 64 65 20 69 66 20 77 65 20 66 61 69 6c 2e 20 54  de if we fail. T
129b4 68 65 20 4f 53 20 77 69 6c 6c 20 61 75 74 6f 6d  he OS will autom
129b5 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20 64 65 6c  atically .** del
129b6 65 74 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72  ete the temporar
129b7 79 20 66 69 6c 65 20 77 68 65 6e 20 69 74 20 69  y file when it i
129b8 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  s closed..**.** 
129b9 54 68 65 20 66 6c 61 67 73 20 70 61 73 73 65 64  The flags passed
129ba 20 74 6f 20 74 68 65 20 56 46 53 20 6c 61 79 65   to the VFS laye
129bb 72 20 78 4f 70 65 6e 28 29 20 63 61 6c 6c 20 61  r xOpen() call a
129bc 72 65 20 74 68 6f 73 65 20 73 70 65 63 69 66 69  re those specifi
129bd 65 64 0a 2a 2a 20 62 79 20 70 61 72 61 6d 65 74  ed.** by paramet
129be 65 72 20 76 66 73 46 6c 61 67 73 20 4f 52 65 64  er vfsFlags ORed
129bf 20 77 69 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77   with the follow
129c0 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  ing:.**.**     S
129c1 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
129c2 52 49 54 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49  RITE.**     SQLI
129c3 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 0a 2a  TE_OPEN_CREATE.*
129c4 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45  *     SQLITE_OPE
129c5 4e 5f 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20  N_EXCLUSIVE.**  
129c6 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44     SQLITE_OPEN_D
129c7 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 0a 2a 2f 0a  ELETEONCLOSE.*/.
129c8 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
129c9 4f 70 65 6e 74 65 6d 70 28 0a 20 20 50 61 67 65  Opentemp(.  Page
129ca 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20 20  r *pPager,      
129cb 20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 6f    /* The pager o
129cc 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74  bject */.  sqlit
129cd 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20  e3_file *pFile, 
129ce 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 66 69   /* Write the fi
129cf 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 68 65  le descriptor he
129d0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46  re */.  int vfsF
129d1 6c 61 67 73 20 20 20 20 20 20 20 20 20 20 2f 2a  lags          /*
129d2 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68   Flags passed th
129d3 72 6f 75 67 68 20 74 6f 20 74 68 65 20 56 46 53  rough to the VFS
129d4 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
129d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
129d6 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
129d7 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
129d8 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 6f  TEST.  sqlite3_o
129d9 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74 2b 2b 3b  pentemp_count++;
129da 20 20 2f 2a 20 55 73 65 64 20 66 6f 72 20 74 65    /* Used for te
129db 73 74 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73  sting and analys
129dc 69 73 20 6f 6e 6c 79 20 2a 2f 0a 23 65 6e 64 69  is only */.#endi
129dd 66 0a 0a 20 20 76 66 73 46 6c 61 67 73 20 7c 3d  f..  vfsFlags |=
129de 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45    SQLITE_OPEN_RE
129df 41 44 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45  ADWRITE | SQLITE
129e0 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20  _OPEN_CREATE |. 
129e1 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
129e2 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
129e3 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44   | SQLITE_OPEN_D
129e4 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 3b 0a 20 20  ELETEONCLOSE;.  
129e5 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
129e6 65 6e 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c  en(pPager->pVfs,
129e7 20 30 2c 20 70 46 69 6c 65 2c 20 76 66 73 46 6c   0, pFile, vfsFl
129e8 61 67 73 2c 20 30 29 3b 0a 20 20 61 73 73 65 72  ags, 0);.  asser
129e9 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc!=SQLITE_OK
129ea 20 7c 7c 20 69 73 4f 70 65 6e 28 70 46 69 6c 65   || isOpen(pFile
129eb 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ) );.  return rc
129ec 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
129ed 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  he busy handler 
129ee 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
129ef 54 68 65 20 70 61 67 65 72 20 69 6e 76 6f 6b 65  The pager invoke
129f0 73 20 74 68 65 20 62 75 73 79 2d 68 61 6e 64 6c  s the busy-handl
129f1 65 72 20 69 66 20 73 71 6c 69 74 65 33 4f 73 4c  er if sqlite3OsL
129f2 6f 63 6b 28 29 20 72 65 74 75 72 6e 73 20 0a 2a  ock() returns .*
129f3 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 77 68  * SQLITE_BUSY wh
129f4 65 6e 20 74 72 79 69 6e 67 20 74 6f 20 75 70 67  en trying to upg
129f5 72 61 64 65 20 66 72 6f 6d 20 6e 6f 2d 6c 6f 63  rade from no-loc
129f6 6b 20 74 6f 20 61 20 53 48 41 52 45 44 20 6c 6f  k to a SHARED lo
129f7 63 6b 2c 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74  ck,.** or when t
129f8 72 79 69 6e 67 20 74 6f 20 75 70 67 72 61 64 65  rying to upgrade
129f9 20 66 72 6f 6d 20 61 20 52 45 53 45 52 56 45 44   from a RESERVED
129fa 20 6c 6f 63 6b 20 74 6f 20 61 6e 20 45 58 43 4c   lock to an EXCL
129fb 55 53 49 56 45 20 0a 2a 2a 20 6c 6f 63 6b 2e 20  USIVE .** lock. 
129fc 49 74 20 64 6f 65 73 20 2a 6e 6f 74 2a 20 69 6e  It does *not* in
129fd 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61  voke the busy ha
129fe 6e 64 6c 65 72 20 77 68 65 6e 20 75 70 67 72 61  ndler when upgra
129ff 64 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 53 48 41  ding from.** SHA
12a00 52 45 44 20 74 6f 20 52 45 53 45 52 56 45 44 2c  RED to RESERVED,
12a01 20 6f 72 20 77 68 65 6e 20 75 70 67 72 61 64 69   or when upgradi
12a02 6e 67 20 66 72 6f 6d 20 53 48 41 52 45 44 20 74  ng from SHARED t
12a03 6f 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 28  o EXCLUSIVE.** (
12a04 77 68 69 63 68 20 6f 63 63 75 72 73 20 64 75 72  which occurs dur
12a05 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  ing hot-journal 
12a06 72 6f 6c 6c 62 61 63 6b 29 2e 20 53 75 6d 6d 61  rollback). Summa
12a07 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 54 72 61 6e  ry:.**.**   Tran
12a08 73 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20  sition          
12a09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20                | 
12a0a 49 6e 76 6f 6b 65 73 20 78 42 75 73 79 48 61 6e  Invokes xBusyHan
12a0b 64 6c 65 72 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  dler.**   ------
12a0c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12a0d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12a0e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12a0f 2d 2d 0a 2a 2a 20 20 20 4e 4f 5f 4c 4f 43 4b 20  --.**   NO_LOCK 
12a10 20 20 20 20 20 20 2d 3e 20 53 48 41 52 45 44 5f        -> SHARED_
12a11 4c 4f 43 4b 20 20 20 20 20 20 7c 20 59 65 73 0a  LOCK      | Yes.
12a12 2a 2a 20 20 20 53 48 41 52 45 44 5f 4c 4f 43 4b  **   SHARED_LOCK
12a13 20 20 20 2d 3e 20 52 45 53 45 52 56 45 44 5f 4c     -> RESERVED_L
12a14 4f 43 4b 20 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20  OCK    | No.**  
12a15 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d   SHARED_LOCK   -
12a16 3e 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  > EXCLUSIVE_LOCK
12a17 20 20 20 7c 20 4e 6f 0a 2a 2a 20 20 20 52 45 53     | No.**   RES
12a18 45 52 56 45 44 5f 4c 4f 43 4b 20 2d 3e 20 45 58  ERVED_LOCK -> EX
12a19 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c  CLUSIVE_LOCK   |
12a1a 20 59 65 73 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68   Yes.**.** If th
12a1b 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63  e busy-handler c
12a1c 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
12a1d 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6c 6f  non-zero, the lo
12a1e 63 6b 20 69 73 20 0a 2a 2a 20 72 65 74 72 69 65  ck is .** retrie
12a1f 64 2e 20 49 66 20 69 74 20 72 65 74 75 72 6e 73  d. If it returns
12a20 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20   zero, then the 
12a21 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72 72 6f  SQLITE_BUSY erro
12a22 72 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  r is.** returned
12a23 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 6f   to the caller o
12a24 66 20 74 68 65 20 70 61 67 65 72 20 41 50 49 20  f the pager API 
12a25 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  function..*/.SQL
12a26 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
12a27 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
12a28 42 75 73 79 68 61 6e 64 6c 65 72 28 0a 20 20 50  Busyhandler(.  P
12a29 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20  ager *pPager,   
12a2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12a2b 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a      /* Pager obj
12a2c 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  ect */.  int (*x
12a2d 42 75 73 79 48 61 6e 64 6c 65 72 29 28 76 6f 69  BusyHandler)(voi
12a2e 64 20 2a 29 2c 20 20 20 20 20 20 20 20 20 2f 2a  d *),         /*
12a2f 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75 73 79   Pointer to busy
12a30 2d 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f  -handler functio
12a31 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75  n */.  void *pBu
12a32 73 79 48 61 6e 64 6c 65 72 41 72 67 20 20 20 20  syHandlerArg    
12a33 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
12a34 72 67 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20  rgument to pass 
12a35 74 6f 20 78 42 75 73 79 48 61 6e 64 6c 65 72 20  to xBusyHandler 
12a36 2a 2f 0a 29 7b 20 20 0a 20 20 70 50 61 67 65 72  */.){  .  pPager
12a37 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d  ->xBusyHandler =
12a38 20 78 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a 20   xBusyHandler;. 
12a39 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79 48 61   pPager->pBusyHa
12a3a 6e 64 6c 65 72 41 72 67 20 3d 20 70 42 75 73 79  ndlerArg = pBusy
12a3b 48 61 6e 64 6c 65 72 41 72 67 3b 0a 7d 0a 0a 2f  HandlerArg;.}../
12a3c 2a 0a 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 20  *.** Report the 
12a3d 63 75 72 72 65 6e 74 20 70 61 67 65 20 73 69 7a  current page siz
12a3e 65 20 61 6e 64 20 6e 75 6d 62 65 72 20 6f 66 20  e and number of 
12a3f 72 65 73 65 72 76 65 64 20 62 79 74 65 73 20 62  reserved bytes b
12a40 61 63 6b 0a 2a 2a 20 74 6f 20 74 68 65 20 63 6f  ack.** to the co
12a41 64 65 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  dec..*/.#ifdef S
12a42 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a  QLITE_HAS_CODEC.
12a43 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
12a44 72 52 65 70 6f 72 74 53 69 7a 65 28 50 61 67 65  rReportSize(Page
12a45 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66  r *pPager){.  if
12a46 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63  ( pPager->xCodec
12a47 53 69 7a 65 43 68 6e 67 20 29 7b 0a 20 20 20 20  SizeChng ){.    
12a48 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63 53 69  pPager->xCodecSi
12a49 7a 65 43 68 6e 67 28 70 50 61 67 65 72 2d 3e 70  zeChng(pPager->p
12a4a 43 6f 64 65 63 2c 20 70 50 61 67 65 72 2d 3e 70  Codec, pPager->p
12a4b 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20  ageSize,.       
12a4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12a4d 20 20 20 20 28 69 6e 74 29 70 50 61 67 65 72 2d      (int)pPager-
12a4e 3e 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 7d 0a  >nReserve);.  }.
12a4f 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
12a50 20 70 61 67 65 72 52 65 70 6f 72 74 53 69 7a 65   pagerReportSize
12a51 28 58 29 20 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70  (X)     /* No-op
12a52 20 69 66 20 77 65 20 64 6f 20 6e 6f 74 20 73 75   if we do not su
12a53 70 70 6f 72 74 20 61 20 63 6f 64 65 63 20 2a 2f  pport a codec */
12a54 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43  .#endif../*.** C
12a55 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73  hange the page s
12a56 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20  ize used by the 
12a57 50 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 54 68  Pager object. Th
12a58 65 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20  e new page size 
12a59 0a 2a 2a 20 69 73 20 70 61 73 73 65 64 20 69 6e  .** is passed in
12a5a 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a 0a   *pPageSize..**.
12a5b 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20  ** If the pager 
12a5c 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20  is in the error 
12a5d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20  state when this 
12a5e 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
12a5f 65 64 2c 20 69 74 0a 2a 2a 20 69 73 20 61 20 6e  ed, it.** is a n
12a60 6f 2d 6f 70 2e 20 54 68 65 20 76 61 6c 75 65 20  o-op. The value 
12a61 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 20  returned is the 
12a62 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f  error state erro
12a63 72 20 63 6f 64 65 20 28 69 2e 65 2e 20 0a 2a 2a  r code (i.e. .**
12a64 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 49   one of SQLITE_I
12a65 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43 4f 52  OERR, SQLITE_COR
12a66 52 55 50 54 20 6f 72 20 53 51 4c 49 54 45 5f 46  RUPT or SQLITE_F
12a67 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  ULL)..**.** Othe
12a68 72 77 69 73 65 2c 20 69 66 20 61 6c 6c 20 6f 66  rwise, if all of
12a69 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
12a6a 72 65 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20  re true:.**.**  
12a6b 20 2a 20 74 68 65 20 6e 65 77 20 70 61 67 65 20   * the new page 
12a6c 73 69 7a 65 20 28 76 61 6c 75 65 20 6f 66 20 2a  size (value of *
12a6d 70 50 61 67 65 53 69 7a 65 29 20 69 73 20 76 61  pPageSize) is va
12a6e 6c 69 64 20 28 61 20 70 6f 77 65 72 20 0a 2a 2a  lid (a power .**
12a6f 20 20 20 20 20 6f 66 20 74 77 6f 20 62 65 74 77       of two betw
12a70 65 65 6e 20 35 31 32 20 61 6e 64 20 53 51 4c 49  een 512 and SQLI
12a71 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
12a72 2c 20 69 6e 63 6c 75 73 69 76 65 29 2c 20 61 6e  , inclusive), an
12a73 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 72  d.**.**   * ther
12a74 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e  e are no outstan
12a75 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72 65  ding page refere
12a76 6e 63 65 73 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20  nces, and.**.** 
12a77 20 20 2a 20 74 68 65 20 64 61 74 61 62 61 73 65    * the database
12a78 20 69 73 20 65 69 74 68 65 72 20 6e 6f 74 20 61   is either not a
12a79 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
12a7a 62 61 73 65 20 6f 72 20 69 74 20 69 73 0a 2a 2a  base or it is.**
12a7b 20 20 20 20 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72       an in-memor
12a7c 79 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  y database that 
12a7d 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 73  currently consis
12a7e 74 73 20 6f 66 20 7a 65 72 6f 20 70 61 67 65 73  ts of zero pages
12a7f 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ..**.** then the
12a80 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 70 61   pager object pa
12a81 67 65 20 73 69 7a 65 20 69 73 20 73 65 74 20 74  ge size is set t
12a82 6f 20 2a 70 50 61 67 65 53 69 7a 65 2e 0a 2a 2a  o *pPageSize..**
12a83 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20  .** If the page 
12a84 73 69 7a 65 20 69 73 20 63 68 61 6e 67 65 64 2c  size is changed,
12a85 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
12a86 69 6f 6e 20 75 73 65 73 20 73 71 6c 69 74 65 33  ion uses sqlite3
12a87 50 61 67 65 72 4d 61 6c 6c 6f 63 28 29 20 0a 2a  PagerMalloc() .*
12a88 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 6e 65  * to obtain a ne
12a89 77 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63  w Pager.pTmpSpac
12a8a 65 20 62 75 66 66 65 72 2e 20 49 66 20 74 68 69  e buffer. If thi
12a8b 73 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74  s allocation att
12a8c 65 6d 70 74 20 0a 2a 2a 20 66 61 69 6c 73 2c 20  empt .** fails, 
12a8d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20  SQLITE_NOMEM is 
12a8e 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
12a8f 20 70 61 67 65 20 73 69 7a 65 20 72 65 6d 61 69   page size remai
12a90 6e 73 20 75 6e 63 68 61 6e 67 65 64 2e 20 0a 2a  ns unchanged. .*
12a91 2a 20 49 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63  * In all other c
12a92 61 73 65 73 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  ases, SQLITE_OK 
12a93 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
12a94 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 73  ** If the page s
12a95 69 7a 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  ize is not chang
12a96 65 64 2c 20 65 69 74 68 65 72 20 62 65 63 61 75  ed, either becau
12a97 73 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 65 6e  se one of the en
12a98 75 6d 65 72 61 74 65 64 0a 2a 2a 20 63 6f 6e 64  umerated.** cond
12a99 69 74 69 6f 6e 73 20 61 62 6f 76 65 20 69 73 20  itions above is 
12a9a 6e 6f 74 20 74 72 75 65 2c 20 74 68 65 20 70 61  not true, the pa
12a9b 67 65 72 20 77 61 73 20 69 6e 20 65 72 72 6f 72  ger was in error
12a9c 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73   state when this
12a9d 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 77 61 73  .** function was
12a9e 20 63 61 6c 6c 65 64 2c 20 6f 72 20 62 65 63 61   called, or beca
12a9f 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  use the memory a
12aa0 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70  llocation attemp
12aa1 74 20 66 61 69 6c 65 64 2c 20 0a 2a 2a 20 74 68  t failed, .** th
12aa2 65 6e 20 2a 70 50 61 67 65 53 69 7a 65 20 69 73  en *pPageSize is
12aa3 20 73 65 74 20 74 6f 20 74 68 65 20 6f 6c 64 2c   set to the old,
12aa4 20 72 65 74 61 69 6e 65 64 20 70 61 67 65 20 73   retained page s
12aa5 69 7a 65 20 62 65 66 6f 72 65 20 72 65 74 75 72  ize before retur
12aa6 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ning..*/.SQLITE_
12aa7 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
12aa8 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73  te3PagerSetPages
12aa9 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  ize(Pager *pPage
12aaa 72 2c 20 75 31 36 20 2a 70 50 61 67 65 53 69 7a  r, u16 *pPageSiz
12aab 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 29  e, int nReserve)
12aac 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 50 61  {.  int rc = pPa
12aad 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20  ger->errCode;.. 
12aae 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
12aaf 4f 4b 20 29 7b 0a 20 20 20 20 75 31 36 20 70 61  OK ){.    u16 pa
12ab0 67 65 53 69 7a 65 20 3d 20 2a 70 50 61 67 65 53  geSize = *pPageS
12ab1 69 7a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ize;.    assert(
12ab2 20 70 61 67 65 53 69 7a 65 3d 3d 30 20 7c 7c 20   pageSize==0 || 
12ab3 28 70 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 26  (pageSize>=512 &
12ab4 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51 4c 49  & pageSize<=SQLI
12ab5 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
12ab6 29 20 29 3b 0a 20 20 20 20 69 66 28 20 28 70 50  ) );.    if( (pP
12ab7 61 67 65 72 2d 3e 6d 65 6d 44 62 3d 3d 30 20 7c  ager->memDb==0 |
12ab8 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  | pPager->dbSize
12ab9 3d 3d 30 29 0a 20 20 20 20 20 26 26 20 73 71 6c  ==0).     && sql
12aba 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75  ite3PcacheRefCou
12abb 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  nt(pPager->pPCac
12abc 68 65 29 3d 3d 30 20 0a 20 20 20 20 20 26 26 20  he)==0 .     && 
12abd 70 61 67 65 53 69 7a 65 20 26 26 20 70 61 67 65  pageSize && page
12abe 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 70 61  Size!=pPager->pa
12abf 67 65 53 69 7a 65 20 0a 20 20 20 20 29 7b 0a 20  geSize .    ){. 
12ac0 20 20 20 20 20 63 68 61 72 20 2a 70 4e 65 77 20       char *pNew 
12ac1 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
12ac2 33 50 61 67 65 4d 61 6c 6c 6f 63 28 70 61 67 65  3PageMalloc(page
12ac3 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Size);.      if(
12ac4 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20   !pNew ){.      
12ac5 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
12ac6 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  MEM;.      }else
12ac7 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65 72 5f  {.        pager_
12ac8 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20  reset(pPager);. 
12ac9 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70         pPager->p
12aca 61 67 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69  ageSize = pageSi
12acb 7a 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ze;.        sqli
12acc 74 65 33 50 61 67 65 46 72 65 65 28 70 50 61 67  te3PageFree(pPag
12acd 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a  er->pTmpSpace);.
12ace 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
12acf 70 54 6d 70 53 70 61 63 65 20 3d 20 70 4e 65 77  pTmpSpace = pNew
12ad0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
12ad1 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53 69  3PcacheSetPageSi
12ad2 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ze(pPager->pPCac
12ad3 68 65 2c 20 70 61 67 65 53 69 7a 65 29 3b 0a 20  he, pageSize);. 
12ad4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
12ad5 20 2a 70 50 61 67 65 53 69 7a 65 20 3d 20 28 75   *pPageSize = (u
12ad6 31 36 29 70 50 61 67 65 72 2d 3e 70 61 67 65 53  16)pPager->pageS
12ad7 69 7a 65 3b 0a 20 20 20 20 69 66 28 20 6e 52 65  ize;.    if( nRe
12ad8 73 65 72 76 65 3c 30 20 29 20 6e 52 65 73 65 72  serve<0 ) nReser
12ad9 76 65 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65  ve = pPager->nRe
12ada 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73 65 72  serve;.    asser
12adb 74 28 20 6e 52 65 73 65 72 76 65 3e 3d 30 20 26  t( nReserve>=0 &
12adc 26 20 6e 52 65 73 65 72 76 65 3c 31 30 30 30 20  & nReserve<1000 
12add 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e  );.    pPager->n
12ade 52 65 73 65 72 76 65 20 3d 20 28 69 31 36 29 6e  Reserve = (i16)n
12adf 52 65 73 65 72 76 65 3b 0a 20 20 20 20 70 61 67  Reserve;.    pag
12ae0 65 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61  erReportSize(pPa
12ae1 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ger);.  }.  retu
12ae2 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
12ae3 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
12ae4 20 74 6f 20 74 68 65 20 22 74 65 6d 70 6f 72 61   to the "tempora
12ae5 72 79 20 70 61 67 65 22 20 62 75 66 66 65 72 20  ry page" buffer 
12ae6 68 65 6c 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a  held internally.
12ae7 2a 2a 20 62 79 20 74 68 65 20 70 61 67 65 72 2e  ** by the pager.
12ae8 20 20 54 68 69 73 20 69 73 20 61 20 62 75 66 66    This is a buff
12ae9 65 72 20 74 68 61 74 20 69 73 20 62 69 67 20 65  er that is big e
12aea 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68  nough to hold th
12aeb 65 0a 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74  e.** entire cont
12aec 65 6e 74 20 6f 66 20 61 20 64 61 74 61 62 61 73  ent of a databas
12aed 65 20 70 61 67 65 2e 20 20 54 68 69 73 20 62 75  e page.  This bu
12aee 66 66 65 72 20 69 73 20 75 73 65 64 20 69 6e 74  ffer is used int
12aef 65 72 6e 61 6c 6c 79 0a 2a 2a 20 64 75 72 69 6e  ernally.** durin
12af0 67 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64 20 77  g rollback and w
12af1 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74  ill be overwritt
12af2 65 6e 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f  en whenever a ro
12af3 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 63 63 75 72 73  llback.** occurs
12af4 2e 20 20 42 75 74 20 6f 74 68 65 72 20 6d 6f 64  .  But other mod
12af5 75 6c 65 73 20 61 72 65 20 66 72 65 65 20 74 6f  ules are free to
12af6 20 75 73 65 20 69 74 20 74 6f 6f 2c 20 61 73 20   use it too, as 
12af7 6c 6f 6e 67 20 61 73 0a 2a 2a 20 6e 6f 20 72 6f  long as.** no ro
12af8 6c 6c 62 61 63 6b 73 20 61 72 65 20 68 61 70 70  llbacks are happ
12af9 65 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ening..*/.SQLITE
12afa 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
12afb 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53  qlite3PagerTempS
12afc 70 61 63 65 28 50 61 67 65 72 20 2a 70 50 61 67  pace(Pager *pPag
12afd 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
12afe 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b  ager->pTmpSpace;
12aff 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
12b00 74 20 74 6f 20 73 65 74 20 74 68 65 20 6d 61 78  t to set the max
12b01 69 6d 75 6d 20 64 61 74 61 62 61 73 65 20 70 61  imum database pa
12b02 67 65 20 63 6f 75 6e 74 20 69 66 20 6d 78 50 61  ge count if mxPa
12b03 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e 20  ge is positive. 
12b04 0a 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63 68 61 6e  .** Make no chan
12b05 67 65 73 20 69 66 20 6d 78 50 61 67 65 20 69 73  ges if mxPage is
12b06 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61 74 69 76   zero or negativ
12b07 65 2e 20 20 41 6e 64 20 6e 65 76 65 72 20 72 65  e.  And never re
12b08 64 75 63 65 20 74 68 65 0a 2a 2a 20 6d 61 78 69  duce the.** maxi
12b09 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 20 62  mum page count b
12b0a 65 6c 6f 77 20 74 68 65 20 63 75 72 72 65 6e 74  elow the current
12b0b 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
12b0c 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 67  abase..**.** Reg
12b0d 61 72 64 6c 65 73 73 20 6f 66 20 6d 78 50 61 67  ardless of mxPag
12b0e 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 63 75  e, return the cu
12b0f 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20 70 61  rrent maximum pa
12b10 67 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c  ge count..*/.SQL
12b11 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
12b12 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61 78 50  sqlite3PagerMaxP
12b13 61 67 65 43 6f 75 6e 74 28 50 61 67 65 72 20 2a  ageCount(Pager *
12b14 70 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61  pPager, int mxPa
12b15 67 65 29 7b 0a 20 20 69 66 28 20 6d 78 50 61 67  ge){.  if( mxPag
12b16 65 3e 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65  e>0 ){.    pPage
12b17 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 6d 78 50 61  r->mxPgno = mxPa
12b18 67 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ge;.  }.  sqlite
12b19 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
12b1a 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 72 65  pPager, 0);.  re
12b1b 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6d 78 50  turn pPager->mxP
12b1c 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  gno;.}../*.** Th
12b1d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 65 74 20  e following set 
12b1e 6f 66 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  of routines are 
12b1f 75 73 65 64 20 74 6f 20 64 69 73 61 62 6c 65 20  used to disable 
12b20 74 68 65 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a  the simulated.**
12b21 20 49 2f 4f 20 65 72 72 6f 72 20 6d 65 63 68 61   I/O error mecha
12b22 6e 69 73 6d 2e 20 20 54 68 65 73 65 20 72 6f 75  nism.  These rou
12b23 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74  tines are used t
12b24 6f 20 61 76 6f 69 64 20 73 69 6d 75 6c 61 74 65  o avoid simulate
12b25 64 0a 2a 2a 20 65 72 72 6f 72 73 20 69 6e 20 70  d.** errors in p
12b26 6c 61 63 65 73 20 77 68 65 72 65 20 77 65 20 64  laces where we d
12b27 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74  o not care about
12b28 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 55   errors..**.** U
12b29 6e 6c 65 73 73 20 2d 44 53 51 4c 49 54 45 5f 54  nless -DSQLITE_T
12b2a 45 53 54 3d 31 20 69 73 20 75 73 65 64 2c 20 74  EST=1 is used, t
12b2b 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
12b2c 65 20 61 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2a 20  e all no-ops.** 
12b2d 61 6e 64 20 67 65 6e 65 72 61 74 65 20 6e 6f 20  and generate no 
12b2e 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  code..*/.#ifdef 
12b2f 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
12b30 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 69 6e  TE_API extern in
12b31 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
12b32 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 53 51 4c 49  or_pending;.SQLI
12b33 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20 69 6e  TE_API extern in
12b34 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
12b35 6f 72 5f 68 69 74 3b 0a 73 74 61 74 69 63 20 69  or_hit;.static i
12b36 6e 74 20 73 61 76 65 64 5f 63 6e 74 3b 0a 76 6f  nt saved_cnt;.vo
12b37 69 64 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c  id disable_simul
12b38 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76  ated_io_errors(v
12b39 6f 69 64 29 7b 0a 20 20 73 61 76 65 64 5f 63 6e  oid){.  saved_cn
12b3a 74 20 3d 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  t = sqlite3_io_e
12b3b 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20  rror_pending;.  
12b3c 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
12b3d 5f 70 65 6e 64 69 6e 67 20 3d 20 2d 31 3b 0a 7d  _pending = -1;.}
12b3e 0a 76 6f 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d  .void enable_sim
12b3f 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
12b40 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  (void){.  sqlite
12b41 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69  3_io_error_pendi
12b42 6e 67 20 3d 20 73 61 76 65 64 5f 63 6e 74 3b 0a  ng = saved_cnt;.
12b43 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
12b44 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74   disable_simulat
12b45 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23  ed_io_errors().#
12b46 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f 73   define enable_s
12b47 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
12b48 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  rs().#endif../*.
12b49 2a 2a 20 52 65 61 64 20 74 68 65 20 66 69 72 73  ** Read the firs
12b4a 74 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74  t N bytes from t
12b4b 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
12b4c 74 68 65 20 66 69 6c 65 20 69 6e 74 6f 20 6d 65  the file into me
12b4d 6d 6f 72 79 0a 2a 2a 20 74 68 61 74 20 70 44 65  mory.** that pDe
12b4e 73 74 20 70 6f 69 6e 74 73 20 74 6f 2e 20 0a 2a  st points to. .*
12b4f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  *.** If the page
12b50 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20  r was opened on 
12b51 61 20 74 72 61 6e 73 69 65 6e 74 20 66 69 6c 65  a transient file
12b52 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d 22 22 29   (zFilename=="")
12b53 2c 20 6f 72 0a 2a 2a 20 6f 70 65 6e 65 64 20 6f  , or.** opened o
12b54 6e 20 61 20 66 69 6c 65 20 6c 65 73 73 20 74 68  n a file less th
12b55 61 6e 20 4e 20 62 79 74 65 73 20 69 6e 20 73 69  an N bytes in si
12b56 7a 65 2c 20 74 68 65 20 6f 75 74 70 75 74 20 62  ze, the output b
12b57 75 66 66 65 72 20 69 73 0a 2a 2a 20 7a 65 72 6f  uffer is.** zero
12b58 65 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b  ed and SQLITE_OK
12b59 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65 20 72   returned. The r
12b5a 61 74 69 6f 6e 61 6c 65 20 66 6f 72 20 74 68 69  ationale for thi
12b5b 73 20 69 73 20 74 68 61 74 20 74 68 69 73 20 0a  s is that this .
12b5c 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  ** function is u
12b5d 73 65 64 20 74 6f 20 72 65 61 64 20 64 61 74 61  sed to read data
12b5e 62 61 73 65 20 68 65 61 64 65 72 73 2c 20 61 6e  base headers, an
12b5f 64 20 61 20 6e 65 77 20 74 72 61 6e 73 69 65 6e  d a new transien
12b60 74 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 73 69 7a  t or.** zero siz
12b61 65 64 20 64 61 74 61 62 61 73 65 20 68 61 73 20  ed database has 
12b62 61 20 68 65 61 64 65 72 20 74 68 61 6e 20 63 6f  a header than co
12b63 6e 73 69 73 74 73 20 65 6e 74 69 72 65 6c 79 20  nsists entirely 
12b64 6f 66 20 7a 65 72 6f 65 73 2e 0a 2a 2a 0a 2a 2a  of zeroes..**.**
12b65 20 49 66 20 61 6e 79 20 49 4f 20 65 72 72 6f 72   If any IO error
12b66 20 61 70 61 72 74 20 66 72 6f 6d 20 53 51 4c 49   apart from SQLI
12b67 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52  TE_IOERR_SHORT_R
12b68 45 41 44 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  EAD is encounter
12b69 65 64 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72  ed,.** the error
12b6a 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
12b6b 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20  d to the caller 
12b6c 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  and the contents
12b6d 20 6f 66 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75   of the.** outpu
12b6e 74 20 62 75 66 66 65 72 20 75 6e 64 65 66 69 6e  t buffer undefin
12b6f 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
12b70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
12b71 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65  3PagerReadFilehe
12b72 61 64 65 72 28 50 61 67 65 72 20 2a 70 50 61 67  ader(Pager *pPag
12b73 65 72 2c 20 69 6e 74 20 4e 2c 20 75 6e 73 69 67  er, int N, unsig
12b74 6e 65 64 20 63 68 61 72 20 2a 70 44 65 73 74 29  ned char *pDest)
12b75 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
12b76 49 54 45 5f 4f 4b 3b 0a 20 20 6d 65 6d 73 65 74  ITE_OK;.  memset
12b77 28 70 44 65 73 74 2c 20 30 2c 20 4e 29 3b 0a 20  (pDest, 0, N);. 
12b78 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28   assert( isOpen(
12b79 70 50 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 70  pPager->fd) || p
12b7a 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
12b7b 29 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28  );.  if( isOpen(
12b7c 70 50 61 67 65 72 2d 3e 66 64 29 20 29 7b 0a 20  pPager->fd) ){. 
12b7d 20 20 20 49 4f 54 52 41 43 45 28 28 22 44 42 48     IOTRACE(("DBH
12b7e 44 52 20 25 70 20 30 20 25 64 5c 6e 22 2c 20 70  DR %p 0 %d\n", p
12b7f 50 61 67 65 72 2c 20 4e 29 29 0a 20 20 20 20 72  Pager, N)).    r
12b80 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
12b81 64 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44  d(pPager->fd, pD
12b82 65 73 74 2c 20 4e 2c 20 30 29 3b 0a 20 20 20 20  est, N, 0);.    
12b83 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49  if( rc==SQLITE_I
12b84 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20  OERR_SHORT_READ 
12b85 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
12b86 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
12b87 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
12b88 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
12b89 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
12b8a 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
12b8b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
12b8c 73 73 6f 63 69 61 74 65 64 20 0a 2a 2a 20 77 69  ssociated .** wi
12b8d 74 68 20 70 50 61 67 65 72 2e 20 4e 6f 72 6d 61  th pPager. Norma
12b8e 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c  lly, this is cal
12b8f 63 75 6c 61 74 65 64 20 61 73 20 28 3c 64 62 20  culated as (<db 
12b90 66 69 6c 65 20 73 69 7a 65 3e 2f 3c 70 61 67 65  file size>/<page
12b91 2d 73 69 7a 65 3e 29 2e 0a 2a 2a 20 48 6f 77 65  -size>)..** Howe
12b92 76 65 72 2c 20 69 66 20 74 68 65 20 66 69 6c 65  ver, if the file
12b93 20 69 73 20 62 65 74 77 65 65 6e 20 31 20 61 6e   is between 1 an
12b94 64 20 3c 70 61 67 65 2d 73 69 7a 65 3e 20 62 79  d <page-size> by
12b95 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65  tes in size, the
12b96 6e 20 0a 2a 2a 20 74 68 69 73 20 69 73 20 63 6f  n .** this is co
12b97 6e 73 69 64 65 72 65 64 20 61 20 31 20 70 61 67  nsidered a 1 pag
12b98 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e file..**.** If
12b99 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
12b9a 20 65 72 72 6f 72 20 73 74 61 74 65 20 77 68 65   error state whe
12b9b 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
12b9c 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20  is called, then 
12b9d 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 61  the.** error sta
12b9e 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  te error code is
12b9f 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70   returned and *p
12ba0 6e 50 61 67 65 20 6c 65 66 74 20 75 6e 63 68 61  nPage left uncha
12ba1 6e 67 65 64 2e 20 4f 72 2c 0a 2a 2a 20 69 66 20  nged. Or,.** if 
12ba2 74 68 65 20 66 69 6c 65 20 73 79 73 74 65 6d 20  the file system 
12ba3 68 61 73 20 74 6f 20 62 65 20 71 75 65 72 69 65  has to be querie
12ba4 64 20 66 6f 72 20 74 68 65 20 73 69 7a 65 20 6f  d for the size o
12ba5 66 20 74 68 65 20 66 69 6c 65 20 61 6e 64 0a 2a  f the file and.*
12ba6 2a 20 74 68 65 20 71 75 65 72 79 20 61 74 74 65  * the query atte
12ba7 6d 70 74 20 72 65 74 75 72 6e 73 20 61 6e 20 49  mpt returns an I
12ba8 4f 20 65 72 72 6f 72 2c 20 74 68 65 20 49 4f 20  O error, the IO 
12ba9 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
12baa 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 2a 70  turned.** and *p
12bab 6e 50 61 67 65 20 69 73 20 6c 65 66 74 20 75 6e  nPage is left un
12bac 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f  changed..**.** O
12bad 74 68 65 72 77 69 73 65 2c 20 69 66 20 65 76 65  therwise, if eve
12bae 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65  rything is succe
12baf 73 73 66 75 6c 2c 20 74 68 65 6e 20 53 51 4c 49  ssful, then SQLI
12bb0 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
12bb1 64 0a 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65  d.** and *pnPage
12bb2 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e   is set to the n
12bb3 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
12bb4 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  n the database..
12bb5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12bb6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
12bb7 65 72 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65  erPagecount(Page
12bb8 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 2a  r *pPager, int *
12bb9 70 6e 50 61 67 65 29 7b 0a 20 20 50 67 6e 6f 20  pnPage){.  Pgno 
12bba 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  nPage;          
12bbb 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
12bbc 20 72 65 74 75 72 6e 20 76 69 61 20 2a 70 6e 50   return via *pnP
12bbd 61 67 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  age */..  /* If 
12bbe 74 68 65 20 70 61 67 65 72 20 69 73 20 61 6c 72  the pager is alr
12bbf 65 61 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f  eady in the erro
12bc0 72 20 73 74 61 74 65 2c 20 72 65 74 75 72 6e 20  r state, return 
12bc1 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20  the error code. 
12bc2 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  */.  if( pPager-
12bc3 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20  >errCode ){.    
12bc4 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65  return pPager->e
12bc5 72 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 2f  rrCode;.  }..  /
12bc6 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20  * Determine the 
12bc7 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
12bc8 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 53 74 6f  in the file. Sto
12bc9 72 65 20 74 68 69 73 20 69 6e 20 6e 50 61 67 65  re this in nPage
12bca 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65  . */.  if( pPage
12bcb 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29  r->dbSizeValid )
12bcc 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d 20 70 50  {.    nPage = pP
12bcd 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20  ager->dbSize;.  
12bce 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72  }else{.    int r
12bcf 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
12bd0 20 20 20 2f 2a 20 45 72 72 6f 72 20 72 65 74 75     /* Error retu
12bd1 72 6e 65 64 20 62 79 20 4f 73 46 69 6c 65 53 69  rned by OsFileSi
12bd2 7a 65 28 29 20 2a 2f 0a 20 20 20 20 69 36 34 20  ze() */.    i64 
12bd3 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  n = 0;          
12bd4 20 20 20 20 2f 2a 20 46 69 6c 65 20 73 69 7a 65      /* File size
12bd5 20 69 6e 20 62 79 74 65 73 20 72 65 74 75 72 6e   in bytes return
12bd6 65 64 20 62 79 20 4f 73 46 69 6c 65 53 69 7a 65  ed by OsFileSize
12bd7 28 29 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72  () */..    asser
12bd8 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
12bd9 2d 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d  ->fd) || pPager-
12bda 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 20  >tempFile );.   
12bdb 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
12bdc 65 72 2d 3e 66 64 29 20 26 26 20 28 30 20 21 3d  er->fd) && (0 !=
12bdd 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73   (rc = sqlite3Os
12bde 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d  FileSize(pPager-
12bdf 3e 66 64 2c 20 26 6e 29 29 29 20 29 7b 0a 20 20  >fd, &n))) ){.  
12be0 20 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28      pager_error(
12be1 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20  pPager, rc);.   
12be2 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
12be3 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 3e 30 20    }.    if( n>0 
12be4 26 26 20 6e 3c 70 50 61 67 65 72 2d 3e 70 61 67  && n<pPager->pag
12be5 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6e  eSize ){.      n
12be6 50 61 67 65 20 3d 20 31 3b 0a 20 20 20 20 7d 65  Page = 1;.    }e
12be7 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 67 65  lse{.      nPage
12be8 20 3d 20 28 50 67 6e 6f 29 28 6e 20 2f 20 70 50   = (Pgno)(n / pP
12be9 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
12bea 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
12beb 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41  Pager->state!=PA
12bec 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20  GER_UNLOCK ){.  
12bed 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
12bee 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20 20 20  ze = nPage;.    
12bef 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65    pPager->dbFile
12bf0 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20  Size = nPage;.  
12bf1 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
12bf2 7a 65 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20  zeValid = 1;.   
12bf3 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
12bf4 74 68 65 20 63 75 72 72 65 6e 74 20 6e 75 6d 62  the current numb
12bf5 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
12bf6 68 65 20 66 69 6c 65 20 69 73 20 67 72 65 61 74  he file is great
12bf7 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20 2a  er than the .  *
12bf8 2a 20 63 6f 6e 66 69 67 75 72 65 64 20 6d 61 78  * configured max
12bf9 69 6d 75 6d 20 70 61 67 65 72 20 6e 75 6d 62 65  imum pager numbe
12bfa 72 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  r, increase the 
12bfb 61 6c 6c 6f 77 65 64 20 6c 69 6d 69 74 20 73 6f  allowed limit so
12bfc 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 66  .  ** that the f
12bfd 69 6c 65 20 63 61 6e 20 62 65 20 72 65 61 64 2e  ile can be read.
12bfe 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 50 61 67  .  */.  if( nPag
12bff 65 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f  e>pPager->mxPgno
12c00 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
12c01 6d 78 50 67 6e 6f 20 3d 20 28 50 67 6e 6f 29 6e  mxPgno = (Pgno)n
12c02 50 61 67 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Page;.  }..  /* 
12c03 53 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 76  Set the output v
12c04 61 72 69 61 62 6c 65 20 61 6e 64 20 72 65 74 75  ariable and retu
12c05 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 2a 2f 0a  rn SQLITE_OK */.
12c06 20 20 69 66 28 20 70 6e 50 61 67 65 20 29 7b 0a    if( pnPage ){.
12c07 20 20 20 20 2a 70 6e 50 61 67 65 20 3d 20 6e 50      *pnPage = nP
12c08 61 67 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  age;.  }.  retur
12c09 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
12c0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62  ./*.** Try to ob
12c0b 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 66 20 74  tain a lock of t
12c0c 79 70 65 20 6c 6f 63 6b 74 79 70 65 20 6f 6e 20  ype locktype on 
12c0d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12c0e 65 2e 20 49 66 0a 2a 2a 20 61 20 73 69 6d 69 6c  e. If.** a simil
12c0f 61 72 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f  ar or greater lo
12c10 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65  ck is already he
12c11 6c 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ld, this functio
12c12 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 0a 2a 2a 20  n is a no-op.** 
12c13 28 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49 54  (returning SQLIT
12c14 45 5f 4f 4b 20 69 6d 6d 65 64 69 61 74 65 6c 79  E_OK immediately
12c15 29 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69  )..**.** Otherwi
12c16 73 65 2c 20 61 74 74 65 6d 70 74 20 74 6f 20 6f  se, attempt to o
12c17 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b 20 75  btain the lock u
12c18 73 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 4c 6f  sing sqlite3OsLo
12c19 63 6b 28 29 2e 20 49 6e 76 6f 6b 65 20 0a 2a 2a  ck(). Invoke .**
12c1a 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62 61   the busy callba
12c1b 63 6b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69  ck if the lock i
12c1c 73 20 63 75 72 72 65 6e 74 6c 79 20 6e 6f 74 20  s currently not 
12c1d 61 76 61 69 6c 61 62 6c 65 2e 20 52 65 70 65 61  available. Repea
12c1e 74 20 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20  t .** until the 
12c1f 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 72 65  busy callback re
12c20 74 75 72 6e 73 20 66 61 6c 73 65 20 6f 72 20 75  turns false or u
12c21 6e 74 69 6c 20 74 68 65 20 61 74 74 65 6d 70 74  ntil the attempt
12c22 20 74 6f 20 0a 2a 2a 20 6f 62 74 61 69 6e 20 74   to .** obtain t
12c23 68 65 20 6c 6f 63 6b 20 73 75 63 63 65 65 64 73  he lock succeeds
12c24 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
12c25 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
12c26 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  ess and an error
12c27 20 63 6f 64 65 20 69 66 20 77 65 20 63 61 6e 6e   code if we cann
12c28 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65  ot obtain.** the
12c29 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 6c 6f   lock. If the lo
12c2a 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 73  ck is obtained s
12c2b 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 73 65 74  uccessfully, set
12c2c 20 74 68 65 20 50 61 67 65 72 2e 73 74 61 74 65   the Pager.state
12c2d 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 74 6f   .** variable to
12c2e 20 6c 6f 63 6b 74 79 70 65 20 62 65 66 6f 72 65   locktype before
12c2f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73   returning..*/.s
12c30 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f  tatic int pager_
12c31 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 50 61 67  wait_on_lock(Pag
12c32 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
12c33 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74  locktype){.  int
12c34 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
12c35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12c36 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
12c37 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 4f 53   */..  /* The OS
12c38 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20 6d 75 73   lock values mus
12c39 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73  t be the same as
12c3a 20 74 68 65 20 50 61 67 65 72 20 6c 6f 63 6b 20   the Pager lock 
12c3b 76 61 6c 75 65 73 20 2a 2f 0a 20 20 61 73 73 65  values */.  asse
12c3c 72 74 28 20 50 41 47 45 52 5f 53 48 41 52 45 44  rt( PAGER_SHARED
12c3d 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  ==SHARED_LOCK );
12c3e 0a 20 20 61 73 73 65 72 74 28 20 50 41 47 45 52  .  assert( PAGER
12c3f 5f 52 45 53 45 52 56 45 44 3d 3d 52 45 53 45 52  _RESERVED==RESER
12c40 56 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  VED_LOCK );.  as
12c41 73 65 72 74 28 20 50 41 47 45 52 5f 45 58 43 4c  sert( PAGER_EXCL
12c42 55 53 49 56 45 3d 3d 45 58 43 4c 55 53 49 56 45  USIVE==EXCLUSIVE
12c43 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49  _LOCK );..  /* I
12c44 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 75  f the file is cu
12c45 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65 64  rrently unlocked
12c46 20 74 68 65 6e 20 74 68 65 20 73 69 7a 65 20 6d   then the size m
12c47 75 73 74 20 62 65 20 75 6e 6b 6e 6f 77 6e 2e 20  ust be unknown. 
12c48 49 74 0a 20 20 2a 2a 20 6d 75 73 74 20 6e 6f 74  It.  ** must not
12c49 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66   have been modif
12c4a 69 65 64 20 61 74 20 74 68 69 73 20 70 6f 69 6e  ied at this poin
12c4b 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  t..  */.  assert
12c4c 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
12c4d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c 7c  =PAGER_SHARED ||
12c4e 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56   pPager->dbSizeV
12c4f 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 61 73 73  alid==0 );.  ass
12c50 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
12c51 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44  te>=PAGER_SHARED
12c52 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f   || pPager->dbMo
12c53 64 69 66 69 65 64 3d 3d 30 20 29 3b 0a 0a 20 20  dified==0 );..  
12c54 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
12c55 69 73 20 69 73 20 65 69 74 68 65 72 20 61 20 6e  is is either a n
12c56 6f 2d 6f 70 20 28 62 65 63 61 75 73 65 20 74 68  o-op (because th
12c57 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
12c58 20 69 73 20 0a 20 20 2a 2a 20 61 6c 72 65 61 64   is .  ** alread
12c59 79 20 68 65 6c 64 2c 20 6f 72 20 6f 6e 65 20 6f  y held, or one o
12c5a 66 20 74 68 65 20 74 72 61 6e 73 69 73 74 69 6f  f the transistio
12c5b 6e 73 20 74 68 61 74 20 74 68 65 20 62 75 73 79  ns that the busy
12c5c 2d 68 61 6e 64 6c 65 72 0a 20 20 2a 2a 20 6d 61  -handler.  ** ma
12c5d 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 64 75 72  y be invoked dur
12c5e 69 6e 67 2c 20 61 63 63 6f 72 64 69 6e 67 20 74  ing, according t
12c5f 6f 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 62  o the comment ab
12c60 6f 76 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  ove.  ** sqlite3
12c61 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64  PagerSetBusyhand
12c62 6c 65 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73  ler()..  */.  as
12c63 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e 73  sert( (pPager->s
12c64 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 29 0a  tate>=locktype).
12c65 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67 65         || (pPage
12c66 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f  r->state==PAGER_
12c67 55 4e 4c 4f 43 4b 20 26 26 20 6c 6f 63 6b 74 79  UNLOCK && lockty
12c68 70 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44  pe==PAGER_SHARED
12c69 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61  ).       || (pPa
12c6a 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45  ger->state==PAGE
12c6b 52 5f 52 45 53 45 52 56 45 44 20 26 26 20 6c 6f  R_RESERVED && lo
12c6c 63 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 45 58  cktype==PAGER_EX
12c6d 43 4c 55 53 49 56 45 29 0a 20 20 29 3b 0a 0a 20  CLUSIVE).  );.. 
12c6e 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
12c6f 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  te>=locktype ){.
12c70 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
12c71 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
12c72 20 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20 3d   do {.      rc =
12c73 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70   sqlite3OsLock(p
12c74 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b 74  Pager->fd, lockt
12c75 79 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65  ype);.    }while
12c76 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53  ( rc==SQLITE_BUS
12c77 59 20 26 26 20 70 50 61 67 65 72 2d 3e 78 42 75  Y && pPager->xBu
12c78 73 79 48 61 6e 64 6c 65 72 28 70 50 61 67 65 72  syHandler(pPager
12c79 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72  ->pBusyHandlerAr
12c7a 67 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63  g) );.    if( rc
12c7b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
12c7c 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61       pPager->sta
12c7d 74 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70  te = (u8)locktyp
12c7e 65 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45  e;.      IOTRACE
12c7f 28 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22  (("LOCK %p %d\n"
12c80 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79  , pPager, lockty
12c81 70 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  pe)).    }.  }. 
12c82 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
12c83 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 61 73  *.** Function as
12c84 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73  sertTruncateCons
12c85 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 20 63  traint(pPager) c
12c86 68 65 63 6b 73 20 74 68 61 74 20 6f 6e 65 20 6f  hecks that one o
12c87 66 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f 77  f the .** follow
12c88 69 6e 67 20 69 73 20 74 72 75 65 20 66 6f 72 20  ing is true for 
12c89 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20  all dirty pages 
12c8a 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65  currently in the
12c8b 20 70 61 67 65 2d 63 61 63 68 65 3a 0a 2a 2a 0a   page-cache:.**.
12c8c 2a 2a 20 20 20 61 29 20 54 68 65 20 70 61 67 65  **   a) The page
12c8d 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73 20   number is less 
12c8e 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
12c8f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
12c90 20 0a 2a 2a 20 20 20 20 20 20 63 75 72 72 65 6e   .**      curren
12c91 74 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  t database image
12c92 2c 20 69 6e 20 70 61 67 65 73 2c 20 4f 52 0a 2a  , in pages, OR.*
12c93 2a 0a 2a 2a 20 20 20 62 29 20 69 66 20 74 68 65  *.**   b) if the
12c94 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 77 65   page content we
12c95 72 65 20 77 72 69 74 74 65 6e 20 61 74 20 74 68  re written at th
12c96 69 73 20 74 69 6d 65 2c 20 69 74 20 77 6f 75 6c  is time, it woul
12c97 64 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 62 65  d not.**      be
12c98 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77 72   necessary to wr
12c99 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ite the current 
12c9a 63 6f 6e 74 65 6e 74 20 6f 75 74 20 74 6f 20 74  content out to t
12c9b 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a  he sub-journal.*
12c9c 2a 20 20 20 20 20 20 28 61 73 20 64 65 74 65 72  *      (as deter
12c9d 6d 69 6e 65 64 20 62 79 20 66 75 6e 63 74 69 6f  mined by functio
12c9e 6e 20 73 75 62 6a 52 65 71 75 69 72 65 73 50 61  n subjRequiresPa
12c9f 67 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ge())..**.** If 
12ca0 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 61 73  the condition as
12ca1 73 65 72 74 65 64 20 62 79 20 74 68 69 73 20 66  serted by this f
12ca2 75 6e 63 74 69 6f 6e 20 77 65 72 65 20 6e 6f 74  unction were not
12ca3 20 74 72 75 65 2c 20 61 6e 64 20 74 68 65 0a 2a   true, and the.*
12ca4 2a 20 64 69 72 74 79 20 70 61 67 65 20 77 65 72  * dirty page wer
12ca5 65 20 74 6f 20 62 65 20 64 69 73 63 61 72 64 65  e to be discarde
12ca6 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65  d from the cache
12ca7 20 76 69 61 20 74 68 65 20 70 61 67 65 72 53 74   via the pagerSt
12ca8 72 65 73 73 28 29 0a 2a 2a 20 72 6f 75 74 69 6e  ress().** routin
12ca9 65 2c 20 70 61 67 65 72 53 74 72 65 73 73 28 29  e, pagerStress()
12caa 20 77 6f 75 6c 64 20 6e 6f 74 20 77 72 69 74 65   would not write
12cab 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 67   the current pag
12cac 65 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a 20  e content to.** 
12cad 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12cae 65 2e 20 49 66 20 61 20 73 61 76 65 70 6f 69 6e  e. If a savepoin
12caf 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 65  t transaction we
12cb0 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  re rolled back a
12cb1 66 74 65 72 0a 2a 2a 20 74 68 69 73 20 68 61 70  fter.** this hap
12cb2 70 65 6e 65 64 2c 20 74 68 65 20 63 6f 72 72 65  pened, the corre
12cb3 63 74 20 62 65 68 61 76 69 6f 75 72 20 77 6f 75  ct behaviour wou
12cb4 6c 64 20 62 65 20 74 6f 20 72 65 73 74 6f 72 65  ld be to restore
12cb5 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
12cb6 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70  content of the p
12cb7 61 67 65 2e 20 48 6f 77 65 76 65 72 2c 20 73 69  age. However, si
12cb8 6e 63 65 20 74 68 69 73 20 63 6f 6e 74 65 6e 74  nce this content
12cb9 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20   is not present 
12cba 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 74 68 65  in either.** the
12cbb 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f   database file o
12cbc 72 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 6f 66  r the portion of
12cbd 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   the rollback jo
12cbe 75 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a 20 73 75  urnal and .** su
12cbf 62 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 65 64  b-journal rolled
12cc0 20 62 61 63 6b 20 74 68 65 20 63 6f 6e 74 65 6e   back the conten
12cc1 74 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72  t could not be r
12cc2 65 73 74 6f 72 65 64 20 61 6e 64 20 74 68 65 0a  estored and the.
12cc3 2a 2a 20 64 61 74 61 62 61 73 65 20 69 6d 61 67  ** database imag
12cc4 65 20 77 6f 75 6c 64 20 62 65 63 6f 6d 65 20 63  e would become c
12cc5 6f 72 72 75 70 74 2e 20 49 74 20 69 73 20 74 68  orrupt. It is th
12cc6 65 72 65 66 6f 72 65 20 66 6f 72 74 75 6e 61 74  erefore fortunat
12cc7 65 20 74 68 61 74 20 0a 2a 2a 20 74 68 69 73 20  e that .** this 
12cc8 63 69 72 63 75 6d 73 74 61 6e 63 65 20 63 61 6e  circumstance can
12cc9 6e 6f 74 20 61 72 69 73 65 2e 0a 2a 2f 0a 23 69  not arise..*/.#i
12cca 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
12ccb 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63 20 76  _DEBUG).static v
12ccc 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61  oid assertTrunca
12ccd 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 28 50  teConstraintCb(P
12cce 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 61 73  gHdr *pPg){.  as
12ccf 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73  sert( pPg->flags
12cd0 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a  &PGHDR_DIRTY );.
12cd1 20 20 61 73 73 65 72 74 28 20 21 73 75 62 6a 52    assert( !subjR
12cd2 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29  equiresPage(pPg)
12cd3 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70   || pPg->pgno<=p
12cd4 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62 53 69  Pg->pPager->dbSi
12cd5 7a 65 20 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76  ze );.}.static v
12cd6 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e 63 61  oid assertTrunca
12cd7 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 67  teConstraint(Pag
12cd8 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73  er *pPager){.  s
12cd9 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65 72  qlite3PcacheIter
12cda 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72 2d  ateDirty(pPager-
12cdb 3e 70 50 43 61 63 68 65 2c 20 61 73 73 65 72 74  >pPCache, assert
12cdc 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69  TruncateConstrai
12cdd 6e 74 43 62 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23  ntCb);.}.#else.#
12cde 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 54 72   define assertTr
12cdf 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74  uncateConstraint
12ce0 28 70 50 61 67 65 72 29 0a 23 65 6e 64 69 66 0a  (pPager).#endif.
12ce1 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20  ./*.** Truncate 
12ce2 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61  the in-memory da
12ce3 74 61 62 61 73 65 20 66 69 6c 65 20 69 6d 61 67  tabase file imag
12ce4 65 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65 73  e to nPage pages
12ce5 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74  . This .** funct
12ce6 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 63 74  ion does not act
12ce7 75 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 68 65  ually modify the
12ce8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f   database file o
12ce9 6e 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a 20 6a  n disk. It .** j
12cea 75 73 74 20 73 65 74 73 20 74 68 65 20 69 6e 74  ust sets the int
12ceb 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74  ernal state of t
12cec 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20  he pager object 
12ced 73 6f 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20  so that the .** 
12cee 74 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c 6c 20  truncation will 
12cef 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 68 65  be done when the
12cf0 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
12cf1 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65  tion is committe
12cf2 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
12cf3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
12cf4 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d  3PagerTruncateIm
12cf5 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67 65  age(Pager *pPage
12cf6 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a  r, Pgno nPage){.
12cf7 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
12cf8 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b  ->dbSizeValid );
12cf9 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
12cfa 72 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 67 65  r->dbSize>=nPage
12cfb 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
12cfc 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
12cfd 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20  ER_RESERVED );. 
12cfe 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20   pPager->dbSize 
12cff 3d 20 6e 50 61 67 65 3b 0a 20 20 61 73 73 65 72  = nPage;.  asser
12d00 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61  tTruncateConstra
12d01 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 7d 0a 0a  int(pPager);.}..
12d02 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20 74  /*.** Shutdown t
12d03 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 20  he page cache.  
12d04 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  Free all memory 
12d05 61 6e 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66 69  and close all fi
12d06 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  les..**.** If a 
12d07 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20  transaction was 
12d08 69 6e 20 70 72 6f 67 72 65 73 73 20 77 68 65 6e  in progress when
12d09 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
12d0a 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a 2a   called, that.**
12d0b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
12d0c 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41 6c  rolled back.  Al
12d0d 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61  l outstanding pa
12d0e 67 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64 61  ges are invalida
12d0f 74 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69 72  ted.** and their
12d10 20 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 65 64   memory is freed
12d11 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74  .  Any attempt t
12d12 6f 20 75 73 65 20 61 20 70 61 67 65 20 61 73 73  o use a page ass
12d13 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20  ociated.** with 
12d14 74 68 69 73 20 70 61 67 65 20 63 61 63 68 65 20  this page cache 
12d15 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74  after this funct
12d16 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 6c 6c  ion returns will
12d17 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75 6c   likely.** resul
12d18 74 20 69 6e 20 61 20 63 6f 72 65 64 75 6d 70 2e  t in a coredump.
12d19 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
12d1a 74 69 6f 6e 20 61 6c 77 61 79 73 20 73 75 63 63  tion always succ
12d1b 65 65 64 73 2e 20 49 66 20 61 20 74 72 61 6e 73  eeds. If a trans
12d1c 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
12d1d 20 61 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20 69   an attempt.** i
12d1e 73 20 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20 69  s made to roll i
12d1f 74 20 62 61 63 6b 2e 20 49 66 20 61 6e 20 65 72  t back. If an er
12d20 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
12d21 67 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 0a  g the rollback .
12d22 2a 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c  ** a hot journal
12d23 20 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e 20   may be left in 
12d24 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20 62  the filesystem b
12d25 75 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20 72  ut no error is r
12d26 65 74 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74 68  eturned.** to th
12d27 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51 4c  e caller..*/.SQL
12d28 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
12d29 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73  sqlite3PagerClos
12d2a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  e(Pager *pPager)
12d2b 7b 0a 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75  {.  disable_simu
12d2c 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
12d2d 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69  );.  sqlite3Begi
12d2e 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
12d2f 0a 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  .  pPager->errCo
12d30 64 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  de = 0;.  pPager
12d31 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 20  ->exclusiveMode 
12d32 3d 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 65 73  = 0;.  pager_res
12d33 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69 66  et(pPager);.  if
12d34 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 70  ( MEMDB ){.    p
12d35 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67  ager_unlock(pPag
12d36 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  er);.  }else{.  
12d37 20 20 2f 2a 20 53 65 74 20 50 61 67 65 72 2e 6a    /* Set Pager.j
12d38 6f 75 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31 20  ournalHdr to -1 
12d39 66 6f 72 20 74 68 65 20 62 65 6e 65 66 69 74 20  for the benefit 
12d3a 6f 66 20 74 68 65 20 70 61 67 65 72 5f 70 6c 61  of the pager_pla
12d3b 79 62 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a 20  yback() .    ** 
12d3c 63 61 6c 6c 20 77 68 69 63 68 20 6d 61 79 20 62  call which may b
12d3d 65 20 6d 61 64 65 20 66 72 6f 6d 20 77 69 74 68  e made from with
12d3e 69 6e 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e  in pagerUnlockAn
12d3f 64 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66 20  dRollback(). If 
12d40 69 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74  it.    ** is not
12d41 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 75 6e   -1, then the un
12d42 73 79 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20 6f  synced portion o
12d43 66 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61  f an open journa
12d44 6c 20 66 69 6c 65 20 6d 61 79 0a 20 20 20 20 2a  l file may.    *
12d45 2a 20 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b  * be played back
12d46 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
12d47 73 65 2e 20 49 66 20 61 20 70 6f 77 65 72 20 66  se. If a power f
12d48 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77 68  ailure occurs wh
12d49 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20  ile.    ** this 
12d4a 69 73 20 68 61 70 70 65 6e 69 6e 67 2c 20 74 68  is happening, th
12d4b 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20 62  e database may b
12d4c 65 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a 20  ecome corrupt.. 
12d4d 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72     */.    pPager
12d4e 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 2d  ->journalHdr = -
12d4f 31 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f  1;.    pagerUnlo
12d50 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70 50  ckAndRollback(pP
12d51 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  ager);.  }.  sql
12d52 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
12d53 6c 6f 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65 5f  loc();.  enable_
12d54 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
12d55 6f 72 73 28 29 3b 0a 20 20 50 41 47 45 52 54 52  ors();.  PAGERTR
12d56 41 43 45 28 28 22 43 4c 4f 53 45 20 25 64 5c 6e  ACE(("CLOSE %d\n
12d57 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  ", PAGERID(pPage
12d58 72 29 29 29 3b 0a 20 20 49 4f 54 52 41 43 45 28  r)));.  IOTRACE(
12d59 28 22 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20 70  ("CLOSE %p\n", p
12d5a 50 61 67 65 72 29 29 0a 20 20 73 71 6c 69 74 65  Pager)).  sqlite
12d5b 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
12d5c 3e 66 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 50  >fd);.  sqlite3P
12d5d 61 67 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e  ageFree(pPager->
12d5e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71  pTmpSpace);.  sq
12d5f 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65  lite3PcacheClose
12d60 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
12d61 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
12d62 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66  E_HAS_CODEC.  if
12d63 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63  ( pPager->xCodec
12d64 46 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e 78  Free ) pPager->x
12d65 43 6f 64 65 63 46 72 65 65 28 70 50 61 67 65 72  CodecFree(pPager
12d66 2d 3e 70 43 6f 64 65 63 29 3b 0a 23 65 6e 64 69  ->pCodec);.#endi
12d67 66 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50  f..  assert( !pP
12d68 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74  ager->aSavepoint
12d69 20 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49 6e   && !pPager->pIn
12d6a 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73  Journal );.  ass
12d6b 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61  ert( !isOpen(pPa
12d6c 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69 73  ger->jfd) && !is
12d6d 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66  Open(pPager->sjf
12d6e 64 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  d) );..  sqlite3
12d6f 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20  _free(pPager);. 
12d70 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
12d71 4b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e  K;.}..#if !defin
12d72 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65  ed(NDEBUG) || de
12d73 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
12d74 54 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  T)./*.** Return 
12d75 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
12d76 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f  for page pPg..*/
12d77 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12d78 50 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67 65  Pgno sqlite3Page
12d79 72 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50 61  rPagenumber(DbPa
12d7a 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74 75  ge *pPg){.  retu
12d7b 72 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d 0a  rn pPg->pgno;.}.
12d7c 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e  #endif../*.** In
12d7d 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65  crement the refe
12d7e 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20  rence count for 
12d7f 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51 4c  page pPg..*/.SQL
12d80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
12d81 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
12d82 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20  (DbPage *pPg){. 
12d83 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
12d84 66 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  f(pPg);.}../*.**
12d85 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61   Sync the journa
12d86 6c 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  l. In other word
12d87 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c  s, make sure all
12d88 20 74 68 65 20 70 61 67 65 73 20 74 68 61 74 20   the pages that 
12d89 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69  have.** been wri
12d8a 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72  tten to the jour
12d8b 6e 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c  nal have actuall
12d8c 79 20 72 65 61 63 68 65 64 20 74 68 65 20 73 75  y reached the su
12d8d 72 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20  rface of the.** 
12d8e 64 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 65 20  disk and can be 
12d8f 72 65 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  restored in the 
12d90 65 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 2d 6a  event of a hot-j
12d91 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2e  ournal rollback.
12d92 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 61  .**.** If the Pa
12d93 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61  ger.needSync fla
12d94 67 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 74 68  g is not set, th
12d95 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
12d96 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20   is a.** no-op. 
12d97 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 61  Otherwise, the a
12d98 63 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20  ctions required 
12d99 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a 6f  depend on the jo
12d9a 75 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 61 6e  urnal-mode.** an
12d9b 64 20 74 68 65 20 64 65 76 69 63 65 20 63 68 61  d the device cha
12d9c 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66 20  racteristics of 
12d9d 74 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79 73  the the file-sys
12d9e 74 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  tem, as follows:
12d9f 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68  .**.**   * If th
12da0 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
12da1 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a  s an in-memory j
12da2 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f 20  ournal file, no 
12da3 61 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20 20  action need.**  
12da4 20 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a 0a     be taken..**.
12da5 2a 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73 65  **   * Otherwise
12da6 2c 20 69 66 20 74 68 65 20 64 65 76 69 63 65 20  , if the device 
12da7 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74  does not support
12da8 20 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e 44   the SAFE_APPEND
12da9 20 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20 20   property,.**   
12daa 20 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20    then the nRec 
12dab 66 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f 73  field of the mos
12dac 74 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74 74  t recently writt
12dad 65 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  en journal heade
12dae 72 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64 61  r.**     is upda
12daf 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74  ted to contain t
12db0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f 75  he number of jou
12db1 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68 61  rnal records tha
12db2 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62 65  t have.**     be
12db3 65 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c 6f  en written follo
12db4 77 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65 20  wing it. If the 
12db5 70 61 67 65 72 20 69 73 20 6f 70 65 72 61 74 69  pager is operati
12db6 6e 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 0a  ng in full-sync.
12db7 2a 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68 65  **     mode, the
12db8 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
12db9 6c 65 20 69 73 20 73 79 6e 63 65 64 20 62 65 66  le is synced bef
12dba 6f 72 65 20 74 68 69 73 20 66 69 65 6c 64 20 69  ore this field i
12dbb 73 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  s updated..**.**
12dbc 20 20 20 2a 20 49 66 20 74 68 65 20 64 65 76 69     * If the devi
12dbd 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70  ce does not supp
12dbe 6f 72 74 20 74 68 65 20 53 45 51 55 45 4e 54 49  ort the SEQUENTI
12dbf 41 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68 65  AL property, the
12dc0 6e 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e 61  n .**     journa
12dc1 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65 64  l file is synced
12dc2 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20 70  ..**.** Or, in p
12dc3 73 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a 2a  seudo-code:.**.*
12dc4 2a 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e 2d  *   if( NOT <in-
12dc5 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e 20  memory journal> 
12dc6 29 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f  ){.**     if( NO
12dc7 54 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29 7b  T SAFE_APPEND ){
12dc8 0a 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c 66  .**       if( <f
12dc9 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20 29  ull-sync mode> )
12dca 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c 20   xSync(<journal 
12dcb 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20 20  file>);.**      
12dcc 20 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66 69   <update nRec fi
12dcd 65 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a 2a  eld>.**     } .*
12dce 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53 45  *     if( NOT SE
12dcf 51 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e 63  QUENTIAL ) xSync
12dd0 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e 29  (<journal file>)
12dd1 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 54  ;.**   }.**.** T
12dd2 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e  he Pager.needSyn
12dd3 63 20 66 6c 61 67 20 69 73 20 6e 65 76 65 72 20  c flag is never 
12dd4 62 65 20 73 65 74 20 66 6f 72 20 74 65 6d 70 6f  be set for tempo
12dd5 72 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 20 61  rary files, or a
12dd6 6e 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 72 61  ny.** file opera
12dd7 74 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63 20  ting in no-sync 
12dd8 6d 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f 53 79  mode (Pager.noSy
12dd9 6e 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65  nc set to non-ze
12dda 72 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  ro)..**.** If su
12ddb 63 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20 72  ccessful, this r
12ddc 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74 68  outine clears th
12ddd 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  e PGHDR_NEED_SYN
12dde 43 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79 20  C flag of every 
12ddf 0a 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e 74  .** page current
12de0 6c 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72  ly held in memor
12de1 79 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  y before returni
12de2 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66  ng SQLITE_OK. If
12de3 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72 20   an IO.** error 
12de4 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20  is encountered, 
12de5 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f  then the IO erro
12de6 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
12de7 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ed to the caller
12de8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
12de9 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65  syncJournal(Page
12dea 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66  r *pPager){.  if
12deb 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79  ( pPager->needSy
12dec 6e 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  nc ){.    assert
12ded 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  ( !pPager->tempF
12dee 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ile );.    if( p
12def 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
12df0 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de!=PAGER_JOURNA
12df1 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a  LMODE_MEMORY ){.
12df2 20 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20        int rc;   
12df3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12df4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
12df5 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 20  turn code */.   
12df6 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 63     const int iDc
12df7 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76 69   = sqlite3OsDevi
12df8 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
12df9 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20  s(pPager->fd);. 
12dfa 20 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f       assert( isO
12dfb 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
12dfc 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 30   );..      if( 0
12dfd 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49 4f  ==(iDc&SQLITE_IO
12dfe 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29  CAP_SAFE_APPEND)
12dff 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
12e00 68 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20  his block deals 
12e01 77 69 74 68 20 61 6e 20 6f 62 73 63 75 72 65 20  with an obscure 
12e02 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 65 20  problem. If the 
12e03 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  last connection.
12e04 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20          ** that 
12e05 77 72 6f 74 65 20 74 6f 20 74 68 69 73 20 64 61  wrote to this da
12e06 74 61 62 61 73 65 20 77 61 73 20 6f 70 65 72 61  tabase was opera
12e07 74 69 6e 67 20 69 6e 20 70 65 72 73 69 73 74 65  ting in persiste
12e08 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 20  nt-journal.     
12e09 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65 6e     ** mode, then
12e0a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
12e0b 65 20 6d 61 79 20 61 74 20 74 68 69 73 20 70 6f  e may at this po
12e0c 69 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20  int actually be 
12e0d 6c 61 72 67 65 72 0a 20 20 20 20 20 20 20 20 2a  larger.        *
12e0e 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 6a 6f 75  * than Pager.jou
12e0f 72 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e 20 49  rnalOff bytes. I
12e10 66 20 74 68 65 20 6e 65 78 74 20 74 68 69 6e 67  f the next thing
12e11 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a   in the journal.
12e12 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65 20          ** file 
12e13 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 61 20  happens to be a 
12e14 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 28  journal-header (
12e15 77 72 69 74 74 65 6e 20 61 73 20 70 61 72 74 20  written as part 
12e16 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  of the.        *
12e17 2a 20 70 72 65 76 69 6f 75 73 20 63 6f 6e 6e 65  * previous conne
12e18 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74 69  ctions transacti
12e19 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72 61 73 68  on), and a crash
12e1a 20 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c 75 72   or power-failur
12e1b 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 63  e .        ** oc
12e1c 63 75 72 73 20 61 66 74 65 72 20 6e 52 65 63 20  curs after nRec 
12e1d 69 73 20 75 70 64 61 74 65 64 20 62 75 74 20 62  is updated but b
12e1e 65 66 6f 72 65 20 74 68 69 73 20 63 6f 6e 6e 65  efore this conne
12e1f 63 74 69 6f 6e 20 77 72 69 74 65 73 20 0a 20 20  ction writes .  
12e20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 69 6e        ** anythin
12e21 67 20 65 6c 73 65 20 74 6f 20 74 68 65 20 6a 6f  g else to the jo
12e22 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 63  urnal file (or c
12e23 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 61 63  ommits/rolls bac
12e24 6b 20 69 74 73 20 0a 20 20 20 20 20 20 20 20 2a  k its .        *
12e25 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c 20  * transaction), 
12e26 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 79 20  then SQLite may 
12e27 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 64 20  become confused 
12e28 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 20 0a  when doing the .
12e29 20 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d 6a          ** hot-j
12e2a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 20  ournal rollback 
12e2b 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f 76 65  following recove
12e2c 72 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c 6c 20  ry. It may roll 
12e2d 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20 20 20  back all.       
12e2e 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6e 6e   ** of this conn
12e2f 65 63 74 69 6f 6e 73 20 64 61 74 61 2c 20 74 68  ections data, th
12e30 65 6e 20 70 72 6f 63 65 65 64 20 74 6f 20 72 6f  en proceed to ro
12e31 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 65 20 6f  lling back the o
12e32 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  ld,.        ** o
12e33 75 74 2d 6f 66 2d 64 61 74 65 20 64 61 74 61 20  ut-of-date data 
12e34 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 74 2e  that follows it.
12e35 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   Database corrup
12e36 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2a  tion..        **
12e37 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 6f 20 77  .        ** To w
12e38 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 2c  ork around this,
12e39 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   if the journal 
12e3a 66 69 6c 65 20 64 6f 65 73 20 61 70 70 65 61 72  file does appear
12e3b 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20 20   to contain.    
12e3c 20 20 20 20 2a 2a 20 61 20 76 61 6c 69 64 20 68      ** a valid h
12e3d 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20  eader following 
12e3e 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66  Pager.journalOff
12e3f 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 30  , then write a 0
12e40 78 30 30 0a 20 20 20 20 20 20 20 20 2a 2a 20 62  x00.        ** b
12e41 79 74 65 20 74 6f 20 74 68 65 20 73 74 61 72 74  yte to the start
12e42 20 6f 66 20 69 74 20 74 6f 20 70 72 65 76 65 6e   of it to preven
12e43 74 20 69 74 20 66 72 6f 6d 20 62 65 69 6e 67 20  t it from being 
12e44 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20 20 20  recognized..    
12e45 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
12e46 2a 20 56 61 72 69 61 62 6c 65 20 69 4e 65 78 74  * Variable iNext
12e47 48 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74  HdrOffset is set
12e48 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 61   to the offset a
12e49 74 20 77 68 69 63 68 20 74 68 69 73 0a 20 20 20  t which this.   
12e4a 20 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 61       ** problema
12e4b 74 69 63 20 68 65 61 64 65 72 20 77 69 6c 6c 20  tic header will 
12e4c 6f 63 63 75 72 2c 20 69 66 20 69 74 20 65 78 69  occur, if it exi
12e4d 73 74 73 2e 20 61 4d 61 67 69 63 20 69 73 20 75  sts. aMagic is u
12e4e 73 65 64 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  sed .        ** 
12e4f 61 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20 62  as a temporary b
12e50 75 66 66 65 72 20 74 6f 20 69 6e 73 70 65 63 74  uffer to inspect
12e51 20 74 68 65 20 66 69 72 73 74 20 63 6f 75 70 6c   the first coupl
12e52 65 20 6f 66 20 62 79 74 65 73 20 6f 66 0a 20 20  e of bytes of.  
12e53 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 74        ** the pot
12e54 65 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 6c 20 68  ential journal h
12e55 65 61 64 65 72 2e 0a 20 20 20 20 20 20 20 20 2a  eader..        *
12e56 2f 0a 20 20 20 20 20 20 20 20 69 36 34 20 69 4e  /.        i64 iN
12e57 65 78 74 48 64 72 4f 66 66 73 65 74 3b 0a 20 20  extHdrOffset;.  
12e58 20 20 20 20 20 20 75 38 20 61 4d 61 67 69 63 5b        u8 aMagic[
12e59 38 5d 3b 0a 09 75 38 20 7a 48 65 61 64 65 72 5b  8];..u8 zHeader[
12e5a 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d  sizeof(aJournalM
12e5b 61 67 69 63 29 2b 34 5d 3b 0a 0a 09 6d 65 6d 63  agic)+4];...memc
12e5c 70 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f 75  py(zHeader, aJou
12e5d 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f  rnalMagic, sizeo
12e5e 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
12e5f 29 3b 0a 09 70 75 74 33 32 62 69 74 73 28 26 7a  );..put32bits(&z
12e60 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a  Header[sizeof(aJ
12e61 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 70  ournalMagic)], p
12e62 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20  Pager->nRec);.. 
12e63 20 20 20 20 20 20 20 69 4e 65 78 74 48 64 72 4f         iNextHdrO
12e64 66 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c 48  ffset = journalH
12e65 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72 29  drOffset(pPager)
12e66 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
12e67 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50 61  qlite3OsRead(pPa
12e68 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69 63  ger->jfd, aMagic
12e69 2c 20 38 2c 20 69 4e 65 78 74 48 64 72 4f 66 66  , 8, iNextHdrOff
12e6a 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66  set);.        if
12e6b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
12e6c 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d 61  && 0==memcmp(aMa
12e6d 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67  gic, aJournalMag
12e6e 69 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20 20  ic, 8) ){.      
12e6f 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
12e70 20 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20 30   u8 zerobyte = 0
12e71 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ;.          rc =
12e72 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
12e73 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a 65  pPager->jfd, &ze
12e74 72 6f 62 79 74 65 2c 20 31 2c 20 69 4e 65 78 74  robyte, 1, iNext
12e75 48 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  HdrOffset);.    
12e76 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
12e77 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
12e78 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f  && rc!=SQLITE_IO
12e79 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29  ERR_SHORT_READ )
12e7a 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
12e7b 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn rc;.        }
12e7c 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69  ..        /* Wri
12e7d 74 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c 75  te the nRec valu
12e7e 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  e into the journ
12e7f 61 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e 20  al file header. 
12e80 49 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a  If in.        **
12e81 20 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75   full-synchronou
12e82 73 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65  s mode, sync the
12e83 20 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e 20   journal first. 
12e84 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61  This ensures tha
12e85 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c  t.        ** all
12e86 20 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c 79   data has really
12e87 20 68 69 74 20 74 68 65 20 64 69 73 6b 20 62 65   hit the disk be
12e88 66 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70 64  fore nRec is upd
12e89 61 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20  ated to mark.   
12e8a 20 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61 20       ** it as a 
12e8b 63 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72 6f  candidate for ro
12e8c 6c 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20 20  llback..        
12e8d 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68  **.        ** Th
12e8e 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72  is is not requir
12e8f 65 64 20 69 66 20 74 68 65 20 70 65 72 73 69 73  ed if the persis
12e90 74 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70 6f  tent media suppo
12e91 72 74 73 20 74 68 65 0a 20 20 20 20 20 20 20 20  rts the.        
12e92 2a 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20 70  ** SAFE_APPEND p
12e93 72 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73 65  roperty. Because
12e94 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74   in this case it
12e95 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
12e96 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72   .        ** for
12e97 20 67 61 72 62 61 67 65 20 64 61 74 61 20 74 6f   garbage data to
12e98 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20   be appended to 
12e99 74 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e 52  the file, the nR
12e9a 65 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20 20  ec field.       
12e9b 20 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65 64   ** is populated
12e9c 20 77 69 74 68 20 30 78 46 46 46 46 46 46 46 46   with 0xFFFFFFFF
12e9d 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61   when the journa
12e9e 6c 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74  l header is writ
12e9f 74 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  ten.        ** a
12ea0 6e 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74  nd never needs t
12ea1 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20  o be updated..  
12ea2 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
12ea3 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c   if( pPager->ful
12ea4 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44 63  lSync && 0==(iDc
12ea5 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45  &SQLITE_IOCAP_SE
12ea6 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20 20  QUENTIAL) ){.   
12ea7 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
12ea8 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c  E(("SYNC journal
12ea9 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52   of %d\n", PAGER
12eaa 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20  ID(pPager)));.  
12eab 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28          IOTRACE(
12eac 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70  ("JSYNC %p\n", p
12ead 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20  Pager)).        
12eae 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
12eaf 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66 64  Sync(pPager->jfd
12eb0 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66  , pPager->sync_f
12eb1 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20 20  lags);.         
12eb2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
12eb3 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
12eb4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
12eb5 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44 52    IOTRACE(("JHDR
12eb6 20 25 70 20 25 6c 6c 64 5c 6e 22 2c 20 70 50 61   %p %lld\n", pPa
12eb7 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ger, pPager->jou
12eb8 72 6e 61 6c 48 64 72 29 29 3b 0a 20 20 20 20 20  rnalHdr));.     
12eb9 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
12eba 73 57 72 69 74 65 28 0a 20 20 20 20 20 20 20 20  sWrite(.        
12ebb 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c      pPager->jfd,
12ebc 20 7a 48 65 61 64 65 72 2c 20 73 69 7a 65 6f 66   zHeader, sizeof
12ebd 28 7a 48 65 61 64 65 72 29 2c 20 70 50 61 67 65  (zHeader), pPage
12ebe 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 0a 09 29  r->journalHdr..)
12ebf 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
12ec0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
12ec1 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
12ec2 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69  .      if( 0==(i
12ec3 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  Dc&SQLITE_IOCAP_
12ec4 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20  SEQUENTIAL) ){. 
12ec5 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
12ec6 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c  E(("SYNC journal
12ec7 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52   of %d\n", PAGER
12ec8 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20  ID(pPager)));.  
12ec9 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
12eca 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61  JSYNC %p\n", pPa
12ecb 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 72 63  ger)).        rc
12ecc 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
12ecd 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50  (pPager->jfd, pP
12ece 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73  ager->sync_flags
12ecf 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50  | .          (pP
12ed0 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73  ager->sync_flags
12ed1 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55  ==SQLITE_SYNC_FU
12ed2 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44  LL?SQLITE_SYNC_D
12ed3 41 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20  ATAONLY:0).     
12ed4 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66     );.        if
12ed5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
12ed6 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
12ed7 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
12ed8 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
12ed9 69 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75 63  ile was just suc
12eda 63 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65 64  cessfully synced
12edb 2e 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65 64  . Set Pager.need
12edc 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f 20  Sync .    ** to 
12edd 7a 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20 74  zero and clear t
12ede 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
12edf 4e 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20 70  NC flag on all p
12ee0 61 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  agess..    */.  
12ee1 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79    pPager->needSy
12ee2 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  nc = 0;.    pPag
12ee3 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74  er->journalStart
12ee4 65 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69  ed = 1;.    sqli
12ee5 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53 79  te3PcacheClearSy
12ee6 6e 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d 3e  ncFlags(pPager->
12ee7 70 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20  pPCache);.  }.. 
12ee8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
12ee9 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  K;.}../*.** The 
12eea 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
12eeb 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b 65  first in a linke
12eec 64 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20  d list of dirty 
12eed 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 0a  pages connected.
12eee 2a 2a 20 62 79 20 74 68 65 20 50 67 48 64 72 2e  ** by the PgHdr.
12eef 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e 20  pDirty pointer. 
12ef0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72  This function wr
12ef1 69 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f 66  ites each one of
12ef2 20 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f 72   the.** in-memor
12ef3 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c  y pages in the l
12ef4 69 73 74 20 74 6f 20 74 68 65 20 64 61 74 61 62  ist to the datab
12ef5 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61 72  ase file. The ar
12ef6 67 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62 65  gument may.** be
12ef7 20 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e 74   NULL, represent
12ef8 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69 73  ing an empty lis
12ef9 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  t. In this case 
12efa 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
12efb 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a  .** a no-op..**.
12efc 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75 73  ** The pager mus
12efd 74 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 20  t hold at least 
12efe 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
12eff 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
12f00 6f 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e  on.** is called.
12f01 20 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20   Before writing 
12f02 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65 20  anything to the 
12f03 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
12f04 68 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 75  his lock.** is u
12f05 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45 58  pgraded to an EX
12f06 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66  CLUSIVE lock. If
12f07 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74   the lock cannot
12f08 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a 2a   be obtained,.**
12f09 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20   SQLITE_BUSY is 
12f0a 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20  returned and no 
12f0b 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20  data is written 
12f0c 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
12f0d 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20  file..** .** If 
12f0e 74 68 65 20 70 61 67 65 72 20 69 73 20 61 20 74  the pager is a t
12f0f 65 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20 61  emp-file pager a
12f10 6e 64 20 74 68 65 20 61 63 74 75 61 6c 20 66 69  nd the actual fi
12f11 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a 2a  le-system file.*
12f12 2a 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70 65  * is not yet ope
12f13 6e 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64  n, it is created
12f14 20 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66 6f   and opened befo
12f15 72 65 20 61 6e 79 20 64 61 74 61 20 69 73 20 0a  re any data is .
12f16 2a 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e 0a  ** written out..
12f17 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20 6c  **.** Once the l
12f18 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70 67  ock has been upg
12f19 72 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e 65  raded and, if ne
12f1a 63 65 73 73 61 72 79 2c 20 74 68 65 20 66 69 6c  cessary, the fil
12f1b 65 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68 65  e opened,.** the
12f1c 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74 74   pages are writt
12f1d 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61  en out to the da
12f1e 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 6c  tabase file in l
12f1f 69 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74 69  ist order. Writi
12f20 6e 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73 20  ng.** a page is 
12f21 73 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d 65  skipped if it me
12f22 65 74 73 20 65 69 74 68 65 72 20 6f 66 20 74 68  ets either of th
12f23 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69 74  e following crit
12f24 65 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  eria:.**.**   * 
12f25 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  The page number 
12f26 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
12f27 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f 72  Pager.dbSize, or
12f28 0a 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48 44  .**   * The PGHD
12f29 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c 61  R_DONT_WRITE fla
12f2a 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65 20  g is set on the 
12f2b 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77  page..**.** If w
12f2c 72 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61 67  riting out a pag
12f2d 65 20 63 61 75 73 65 73 20 74 68 65 20 64 61 74  e causes the dat
12f2e 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67 72  abase file to gr
12f2f 6f 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c 65  ow, Pager.dbFile
12f30 53 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61 74  Size.** is updat
12f31 65 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 20  ed accordingly. 
12f32 49 66 20 70 61 67 65 20 31 20 69 73 20 77 72 69  If page 1 is wri
12f33 74 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20 74  tten out, then t
12f34 68 65 20 76 61 6c 75 65 20 63 61 63 68 65 64 0a  he value cached.
12f35 2a 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46 69  ** in Pager.dbFi
12f36 6c 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64 61  leVers[] is upda
12f37 74 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68 65  ted to match the
12f38 20 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72 65   new value store
12f39 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61  d in.** the data
12f3a 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  base file..**.**
12f3b 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 69   If everything i
12f3c 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51  s successful, SQ
12f3d 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
12f3e 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65 72  ned. If an IO er
12f3f 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c 20  ror .** occurs, 
12f40 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  an IO error code
12f41 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 72   is returned. Or
12f42 2c 20 69 66 20 74 68 65 20 45 58 43 4c 55 53 49  , if the EXCLUSI
12f43 56 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a 2a  VE lock cannot.*
12f44 2a 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20 53  * be obtained, S
12f45 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72 65  QLITE_BUSY is re
12f46 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
12f47 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69 74  c int pager_writ
12f48 65 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64 72  e_pagelist(PgHdr
12f49 20 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67 65   *pList){.  Page
12f4a 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20  r *pPager;      
12f4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f4c 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74   /* Pager object
12f4d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
12f4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f4f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
12f50 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20  turn code */..  
12f51 69 66 28 20 4e 45 56 45 52 28 70 4c 69 73 74 3d  if( NEVER(pList=
12f52 3d 30 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c  =0) ) return SQL
12f53 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72  ITE_OK;.  pPager
12f54 20 3d 20 70 4c 69 73 74 2d 3e 70 50 61 67 65 72   = pList->pPager
12f55 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20  ;..  /* At this 
12f56 70 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20  point there may 
12f57 62 65 20 65 69 74 68 65 72 20 61 20 52 45 53 45  be either a RESE
12f58 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56  RVED or EXCLUSIV
12f59 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20  E lock on the.  
12f5a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
12f5b 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6c  . If there is al
12f5c 72 65 61 64 79 20 61 6e 20 45 58 43 4c 55 53 49  ready an EXCLUSI
12f5d 56 45 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f 6c  VE lock, the fol
12f5e 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c  lowing.  ** call
12f5f 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a   is a no-op..  *
12f60 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20 74 68  *.  ** Moving th
12f61 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45 53 45  e lock from RESE
12f62 52 56 45 44 20 74 6f 20 45 58 43 4c 55 53 49 56  RVED to EXCLUSIV
12f63 45 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 6f 6c  E actually invol
12f64 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a 20 74  ves going.  ** t
12f65 68 72 6f 75 67 68 20 61 6e 20 69 6e 74 65 72 6d  hrough an interm
12f66 65 64 69 61 74 65 20 73 74 61 74 65 20 50 45 4e  ediate state PEN
12f67 44 49 4e 47 2e 20 20 20 41 20 50 45 4e 44 49 4e  DING.   A PENDIN
12f68 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74 73 20  G lock prevents 
12f69 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64 65 72 73  new.  ** readers
12f6a 20 66 72 6f 6d 20 61 74 74 61 63 68 69 6e 67 20   from attaching 
12f6b 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
12f6c 62 75 74 20 69 73 20 75 6e 73 75 66 66 69 63 69  but is unsuffici
12f6d 65 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a 20 20  ent for us to.  
12f6e 2a 2a 20 77 72 69 74 65 2e 20 20 54 68 65 20 69  ** write.  The i
12f6f 64 65 61 20 6f 66 20 61 20 50 45 4e 44 49 4e 47  dea of a PENDING
12f70 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72 65 76   lock is to prev
12f71 65 6e 74 20 6e 65 77 20 72 65 61 64 65 72 73 20  ent new readers 
12f72 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69 6e 67  from.  ** coming
12f73 20 69 6e 20 77 68 69 6c 65 20 77 65 20 77 61 69   in while we wai
12f74 74 20 66 6f 72 20 65 78 69 73 74 69 6e 67 20 72  t for existing r
12f75 65 61 64 65 72 73 20 74 6f 20 63 6c 65 61 72 2e  eaders to clear.
12f76 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69 6c 65  .  **.  ** While
12f77 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
12f78 20 74 68 65 20 52 45 53 45 52 56 45 44 20 73 74   the RESERVED st
12f79 61 74 65 2c 20 74 68 65 20 6f 72 69 67 69 6e 61  ate, the origina
12f7a 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a  l database file.
12f7b 20 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e 67 65    ** is unchange
12f7c 64 20 61 6e 64 20 77 65 20 63 61 6e 20 72 6f 6c  d and we can rol
12f7d 6c 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68 61  lback without ha
12f7e 76 69 6e 67 20 74 6f 20 70 6c 61 79 62 61 63 6b  ving to playback
12f7f 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61   the.  ** journa
12f80 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67 69  l into the origi
12f81 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  nal database fil
12f82 65 2e 20 20 4f 6e 63 65 20 77 65 20 74 72 61 6e  e.  Once we tran
12f83 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a 20 45  sition to.  ** E
12f84 58 43 4c 55 53 49 56 45 2c 20 69 74 20 6d 65 61  XCLUSIVE, it mea
12f85 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ns the database 
12f86 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63 68  file has been ch
12f87 61 6e 67 65 64 20 61 6e 64 20 61 6e 79 20 72 6f  anged and any ro
12f88 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69 6c 6c  llback.  ** will
12f89 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e   require a journ
12f8a 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20 20 2a  al playback..  *
12f8b 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  /.  assert( pPag
12f8c 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52  er->state>=PAGER
12f8d 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20 72  _RESERVED );.  r
12f8e 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f  c = pager_wait_o
12f8f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45  n_lock(pPager, E
12f90 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a  XCLUSIVE_LOCK);.
12f91 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c  .  /* If the fil
12f92 65 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65  e is a temp-file
12f93 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65   has not yet bee
12f94 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20 69  n opened, open i
12f95 74 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a 20 69  t now. It.  ** i
12f96 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66  s not possible f
12f97 6f 72 20 72 63 20 74 6f 20 62 65 20 6f 74 68 65  or rc to be othe
12f98 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b  r than SQLITE_OK
12f99 20 69 66 20 74 68 69 73 20 62 72 61 6e 63 68 0a   if this branch.
12f9a 20 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c 20 61    ** is taken, a
12f9b 73 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f  s pager_wait_on_
12f9c 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f  lock() is a no-o
12f9d 70 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c 65 73  p for temp-files
12f9e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69 73  ..  */.  if( !is
12f9f 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
12fa0 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
12fa1 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
12fa2 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
12fa3 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61  K );.    rc = pa
12fa4 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61 67  gerOpentemp(pPag
12fa5 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c 20  er, pPager->fd, 
12fa6 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67 73  pPager->vfsFlags
12fa7 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28  );.  }..  while(
12fa8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
12fa9 26 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 50  & pList ){.    P
12faa 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69 73 74  gno pgno = pList
12fab 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 2f 2a 20  ->pgno;..    /* 
12fac 49 66 20 74 68 65 72 65 20 61 72 65 20 64 69 72  If there are dir
12fad 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  ty pages in the 
12fae 70 61 67 65 20 63 61 63 68 65 20 77 69 74 68 20  page cache with 
12faf 70 61 67 65 20 6e 75 6d 62 65 72 73 20 67 72 65  page numbers gre
12fb0 61 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 61 6e  ater.    ** than
12fb1 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 74   Pager.dbSize, t
12fb2 68 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 74 65  his means sqlite
12fb3 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d  3PagerTruncateIm
12fb4 61 67 65 28 29 20 77 61 73 20 63 61 6c 6c 65 64  age() was called
12fb5 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b 65 20   to.    ** make 
12fb6 74 68 65 20 66 69 6c 65 20 73 6d 61 6c 6c 65 72  the file smaller
12fb7 20 28 70 72 65 73 75 6d 61 62 6c 79 20 62 79 20   (presumably by 
12fb8 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 6f 64 65  auto-vacuum code
12fb9 29 2e 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 0a  ). Do not write.
12fba 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 63 68 20      ** any such 
12fbb 70 61 67 65 73 20 74 6f 20 74 68 65 20 66 69 6c  pages to the fil
12fbc 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e..    **.    **
12fbd 20 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 20 77 72   Also, do not wr
12fbe 69 74 65 20 6f 75 74 20 61 6e 79 20 70 61 67 65  ite out any page
12fbf 20 74 68 61 74 20 68 61 73 20 74 68 65 20 50 47   that has the PG
12fc0 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66  HDR_DONT_WRITE f
12fc1 6c 61 67 0a 20 20 20 20 2a 2a 20 73 65 74 20 28  lag.    ** set (
12fc2 73 65 74 20 62 79 20 73 71 6c 69 74 65 33 50 61  set by sqlite3Pa
12fc3 67 65 72 44 6f 6e 74 57 72 69 74 65 28 29 29 2e  gerDontWrite()).
12fc4 20 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 63    Note that if c
12fc5 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 20 20 20  ompiled with.   
12fc6 20 2a 2a 20 53 51 4c 49 54 45 5f 53 45 43 55 52   ** SQLITE_SECUR
12fc7 45 5f 44 45 4c 45 54 45 20 74 68 65 20 50 47 48  E_DELETE the PGH
12fc8 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 62 69  DR_DONT_WRITE bi
12fc9 74 20 69 73 20 6e 65 76 65 72 20 73 65 74 20 61  t is never set a
12fca 6e 64 20 73 6f 0a 20 20 20 20 2a 2a 20 74 68 65  nd so.    ** the
12fcb 20 73 65 63 6f 6e 64 20 74 65 73 74 20 69 73 20   second test is 
12fcc 61 6c 77 61 79 73 20 74 72 75 65 2e 0a 20 20 20  always true..   
12fcd 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e 6f   */.    if( pgno
12fce 3c 3d 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  <=pPager->dbSize
12fcf 20 26 26 20 30 3d 3d 28 70 4c 69 73 74 2d 3e 66   && 0==(pList->f
12fd0 6c 61 67 73 26 50 47 48 44 52 5f 44 4f 4e 54 5f  lags&PGHDR_DONT_
12fd1 57 52 49 54 45 29 20 29 7b 0a 20 20 20 20 20 20  WRITE) ){.      
12fd2 69 36 34 20 6f 66 66 73 65 74 20 3d 20 28 70 67  i64 offset = (pg
12fd3 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65  no-1)*(i64)pPage
12fd4 72 2d 3e 70 61 67 65 53 69 7a 65 3b 20 20 20 2f  r->pageSize;   /
12fd5 2a 20 4f 66 66 73 65 74 20 74 6f 20 77 72 69 74  * Offset to writ
12fd6 65 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20  e */.      char 
12fd7 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  *pData;         
12fd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12fd9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
12fda 61 20 74 6f 20 77 72 69 74 65 20 2a 2f 20 20 20  a to write */   
12fdb 20 0a 0a 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f   ..      /* Enco
12fdc 64 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  de the database 
12fdd 2a 2f 0a 20 20 20 20 20 20 43 4f 44 45 43 32 28  */.      CODEC2(
12fde 70 50 61 67 65 72 2c 20 70 4c 69 73 74 2d 3e 70  pPager, pList->p
12fdf 44 61 74 61 2c 20 70 67 6e 6f 2c 20 36 2c 20 72  Data, pgno, 6, r
12fe0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
12fe1 45 4d 2c 20 70 44 61 74 61 29 3b 0a 0a 20 20 20  EM, pData);..   
12fe2 20 20 20 2f 2a 20 57 72 69 74 65 20 6f 75 74 20     /* Write out 
12fe3 74 68 65 20 70 61 67 65 20 64 61 74 61 2e 20 2a  the page data. *
12fe4 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  /.      rc = sql
12fe5 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
12fe6 65 72 2d 3e 66 64 2c 20 70 44 61 74 61 2c 20 70  er->fd, pData, p
12fe7 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
12fe8 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 20   offset);..     
12fe9 20 2f 2a 20 49 66 20 70 61 67 65 20 31 20 77 61   /* If page 1 wa
12fea 73 20 6a 75 73 74 20 77 72 69 74 74 65 6e 2c 20  s just written, 
12feb 75 70 64 61 74 65 20 50 61 67 65 72 2e 64 62 46  update Pager.dbF
12fec 69 6c 65 56 65 72 73 20 74 6f 20 6d 61 74 63 68  ileVers to match
12fed 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61  .      ** the va
12fee 6c 75 65 20 6e 6f 77 20 73 74 6f 72 65 64 20 69  lue now stored i
12fef 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
12ff0 69 6c 65 2e 20 49 66 20 77 72 69 74 69 6e 67 20  ile. If writing 
12ff1 74 68 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 70  this .      ** p
12ff2 61 67 65 20 63 61 75 73 65 64 20 74 68 65 20 64  age caused the d
12ff3 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20  atabase file to 
12ff4 67 72 6f 77 2c 20 75 70 64 61 74 65 20 64 62 46  grow, update dbF
12ff5 69 6c 65 53 69 7a 65 2e 20 0a 20 20 20 20 20 20  ileSize. .      
12ff6 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e  */.      if( pgn
12ff7 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  o==1 ){.        
12ff8 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e  memcpy(&pPager->
12ff9 64 62 46 69 6c 65 56 65 72 73 2c 20 26 70 44 61  dbFileVers, &pDa
12ffa 74 61 5b 32 34 5d 2c 20 73 69 7a 65 6f 66 28 70  ta[24], sizeof(p
12ffb 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72  Pager->dbFileVer
12ffc 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  s));.      }.   
12ffd 20 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67     if( pgno>pPag
12ffe 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29  er->dbFileSize )
12fff 7b 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72  {.        pPager
13000 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20 70  ->dbFileSize = p
13001 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  gno;.      }..  
13002 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 61 6e      /* Update an
13003 79 20 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 73  y backup objects
13004 20 63 6f 70 79 69 6e 67 20 74 68 65 20 63 6f 6e   copying the con
13005 74 65 6e 74 73 20 6f 66 20 74 68 69 73 20 70 61  tents of this pa
13006 67 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71  ger. */.      sq
13007 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64 61 74  lite3BackupUpdat
13008 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75  e(pPager->pBacku
13009 70 2c 20 70 67 6e 6f 2c 20 28 75 38 2a 29 70 4c  p, pgno, (u8*)pL
1300a 69 73 74 2d 3e 70 44 61 74 61 29 3b 0a 0a 20 20  ist->pData);..  
1300b 20 20 20 20 50 41 47 45 52 54 52 41 43 45 28 28      PAGERTRACE((
1300c 22 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 25  "STORE %d page %
1300d 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22 2c  d hash(%08x)\n",
1300e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1300f 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67      PAGERID(pPag
13010 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72  er), pgno, pager
13011 5f 70 61 67 65 68 61 73 68 28 70 4c 69 73 74 29  _pagehash(pList)
13012 29 29 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43  ));.      IOTRAC
13013 45 28 28 22 50 47 4f 55 54 20 25 70 20 25 64 5c  E(("PGOUT %p %d\
13014 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f  n", pPager, pgno
13015 29 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f  ));.      PAGER_
13016 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67  INCR(sqlite3_pag
13017 65 72 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74  er_writedb_count
13018 29 3b 0a 20 20 20 20 20 20 50 41 47 45 52 5f 49  );.      PAGER_I
13019 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 57 72 69  NCR(pPager->nWri
1301a 74 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  te);.    }else{.
1301b 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
1301c 28 28 22 4e 4f 53 54 4f 52 45 20 25 64 20 70 61  (("NOSTORE %d pa
1301d 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  ge %d\n", PAGERI
1301e 44 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 29  D(pPager), pgno)
1301f 29 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20  );.    }.#ifdef 
13020 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
13021 45 53 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61  ES.    pList->pa
13022 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70  geHash = pager_p
13023 61 67 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a  agehash(pList);.
13024 23 65 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74  #endif.    pList
13025 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79   = pList->pDirty
13026 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
13027 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70  rc;.}../*.** App
13028 65 6e 64 20 61 20 72 65 63 6f 72 64 20 6f 66 20  end a record of 
13029 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74  the current stat
1302a 65 20 6f 66 20 70 61 67 65 20 70 50 67 20 74 6f  e of page pPg to
1302b 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
1302c 2e 20 0a 2a 2a 20 49 74 20 69 73 20 74 68 65 20  . .** It is the 
1302d 63 61 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69  callers responsi
1302e 62 69 6c 69 74 79 20 74 6f 20 75 73 65 20 73 75  bility to use su
1302f 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 29  bjRequiresPage()
13030 20 74 6f 20 63 68 65 63 6b 20 0a 2a 2a 20 74 68   to check .** th
13031 61 74 20 69 74 20 69 73 20 72 65 61 6c 6c 79 20  at it is really 
13032 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20  required before 
13033 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e  calling this fun
13034 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ction..**.** If 
13035 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65 74 20  successful, set 
13036 74 68 65 20 62 69 74 20 63 6f 72 72 65 73 70 6f  the bit correspo
13037 6e 64 69 6e 67 20 74 6f 20 70 50 67 2d 3e 70 67  nding to pPg->pg
13038 6e 6f 20 69 6e 20 74 68 65 20 62 69 74 76 65 63  no in the bitvec
13039 73 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 6f 70 65  s.** for all ope
1303a 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 62 65 66  n savepoints bef
1303b 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
1303c 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1303d 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  on returns SQLIT
1303e 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69  E_OK if everythi
1303f 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75 6c  ng is successful
13040 2c 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72  , an IO.** error
13041 20 63 6f 64 65 20 69 66 20 74 68 65 20 61 74 74   code if the att
13042 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 74 6f  empt to write to
13043 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
13044 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 53   fails, or .** S
13045 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61  QLITE_NOMEM if a
13046 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68   malloc fails wh
13047 69 6c 65 20 73 65 74 74 69 6e 67 20 61 20 62 69  ile setting a bi
13048 74 20 69 6e 20 61 20 73 61 76 65 70 6f 69 6e 74  t in a savepoint
13049 0a 2a 2a 20 62 69 74 76 65 63 2e 0a 2a 2f 0a 73  .** bitvec..*/.s
1304a 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a 6f 75  tatic int subjou
1304b 72 6e 61 6c 50 61 67 65 28 50 67 48 64 72 20 2a  rnalPage(PgHdr *
1304c 70 50 67 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pPg){.  int rc =
1304d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61   SQLITE_OK;.  Pa
1304e 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
1304f 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28  g->pPager;.  if(
13050 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
13051 73 6a 66 64 29 20 29 7b 0a 20 20 20 20 76 6f 69  sjfd) ){.    voi
13052 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e  d *pData = pPg->
13053 70 44 61 74 61 3b 0a 20 20 20 20 69 36 34 20 6f  pData;.    i64 o
13054 66 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e  ffset = pPager->
13055 6e 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67 65  nSubRec*(4+pPage
13056 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  r->pageSize);.  
13057 20 20 63 68 61 72 20 2a 70 44 61 74 61 32 3b 0a    char *pData2;.
13058 0a 20 20 20 20 43 4f 44 45 43 32 28 70 50 61 67  .    CODEC2(pPag
13059 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e  er, pData, pPg->
1305a 70 67 6e 6f 2c 20 37 2c 20 72 65 74 75 72 6e 20  pgno, 7, return 
1305b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 70 44  SQLITE_NOMEM, pD
1305c 61 74 61 32 29 3b 0a 20 20 20 20 50 41 47 45 52  ata2);.    PAGER
1305d 54 52 41 43 45 28 28 22 53 54 4d 54 2d 4a 4f 55  TRACE(("STMT-JOU
1305e 52 4e 41 4c 20 25 64 20 70 61 67 65 20 25 64 5c  RNAL %d page %d\
1305f 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
13060 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29  er), pPg->pgno))
13061 3b 0a 20 20 0a 20 20 20 20 61 73 73 65 72 74 28  ;.  .    assert(
13062 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70   pageInJournal(p
13063 50 67 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f  Pg) || pPg->pgno
13064 3e 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53  >pPager->dbOrigS
13065 69 7a 65 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ize );.    rc = 
13066 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
13067 65 72 2d 3e 73 6a 66 64 2c 20 6f 66 66 73 65 74  er->sjfd, offset
13068 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20  , pPg->pgno);.  
13069 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1306a 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
1306b 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
1306c 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 70  (pPager->sjfd, p
1306d 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70  Data2, pPager->p
1306e 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 2b  ageSize, offset+
1306f 34 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  4);.    }.  }.  
13070 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
13071 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  K ){.    pPager-
13072 3e 6e 53 75 62 52 65 63 2b 2b 3b 0a 20 20 20 20  >nSubRec++;.    
13073 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
13074 6e 53 61 76 65 70 6f 69 6e 74 3e 30 20 29 3b 0a  nSavepoint>0 );.
13075 20 20 20 20 72 63 20 3d 20 61 64 64 54 6f 53 61      rc = addToSa
13076 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28 70  vepointBitvecs(p
13077 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
13078 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
13079 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  rc;.}.../*.** Th
1307a 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
1307b 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 63 61  alled by the pca
1307c 63 68 65 20 6c 61 79 65 72 20 77 68 65 6e 20 69  che layer when i
1307d 74 20 68 61 73 20 72 65 61 63 68 65 64 20 73 6f  t has reached so
1307e 6d 65 0a 2a 2a 20 73 6f 66 74 20 6d 65 6d 6f 72  me.** soft memor
1307f 79 20 6c 69 6d 69 74 2e 20 54 68 65 20 66 69 72  y limit. The fir
13080 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  st argument is a
13081 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61   pointer to a Pa
13082 67 65 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 28 63  ger object.** (c
13083 61 73 74 20 61 73 20 61 20 76 6f 69 64 2a 29 2e  ast as a void*).
13084 20 54 68 65 20 70 61 67 65 72 20 69 73 20 61 6c   The pager is al
13085 77 61 79 73 20 27 70 75 72 67 65 61 62 6c 65 27  ways 'purgeable'
13086 20 28 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65 6d 6f   (not an in-memo
13087 72 79 0a 2a 2a 20 64 61 74 61 62 61 73 65 29 2e  ry.** database).
13088 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
13089 6d 65 6e 74 20 69 73 20 61 20 72 65 66 65 72 65  ment is a refere
1308a 6e 63 65 20 74 6f 20 61 20 70 61 67 65 20 74 68  nce to a page th
1308b 61 74 20 69 73 20 0a 2a 2a 20 63 75 72 72 65 6e  at is .** curren
1308c 74 6c 79 20 64 69 72 74 79 20 62 75 74 20 68 61  tly dirty but ha
1308d 73 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67  s no outstanding
1308e 20 72 65 66 65 72 65 6e 63 65 73 2e 20 54 68 65   references. The
1308f 20 70 61 67 65 0a 2a 2a 20 69 73 20 61 6c 77 61   page.** is alwa
13090 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ys associated wi
13091 74 68 20 74 68 65 20 50 61 67 65 72 20 6f 62 6a  th the Pager obj
13092 65 63 74 20 70 61 73 73 65 64 20 61 73 20 74 68  ect passed as th
13093 65 20 66 69 72 73 74 20 0a 2a 2a 20 61 72 67 75  e first .** argu
13094 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
13095 6a 6f 62 20 6f 66 20 74 68 69 73 20 66 75 6e 63  job of this func
13096 74 69 6f 6e 20 69 73 20 74 6f 20 6d 61 6b 65 20  tion is to make 
13097 70 50 67 20 63 6c 65 61 6e 20 62 79 20 77 72 69  pPg clean by wri
13098 74 69 6e 67 20 69 74 73 20 63 6f 6e 74 65 6e 74  ting its content
13099 73 0a 2a 2a 20 6f 75 74 20 74 6f 20 74 68 65 20  s.** out to the 
1309a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 69  database file, i
1309b 66 20 70 6f 73 73 69 62 6c 65 2e 20 54 68 69 73  f possible. This
1309c 20 6d 61 79 20 69 6e 76 6f 6c 76 65 20 73 79 6e   may involve syn
1309d 63 69 6e 67 20 74 68 65 0a 2a 2a 20 6a 6f 75 72  cing the.** jour
1309e 6e 61 6c 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a  nal file. .**.**
1309f 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
130a0 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
130a1 65 43 6c 65 61 6e 28 29 20 69 73 20 63 61 6c 6c  eClean() is call
130a2 65 64 20 6f 6e 20 74 68 65 20 70 61 67 65 20 61  ed on the page a
130a3 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  nd.** SQLITE_OK 
130a4 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20  returned. If an 
130a5 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  IO error occurs 
130a6 77 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20  while trying to 
130a7 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 70 61 67 65  make the.** page
130a8 20 63 6c 65 61 6e 2c 20 74 68 65 20 49 4f 20 65   clean, the IO e
130a9 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
130aa 75 72 6e 65 64 2e 20 49 66 20 74 68 65 20 70 61  urned. If the pa
130ab 67 65 20 63 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20  ge cannot be.** 
130ac 6d 61 64 65 20 63 6c 65 61 6e 20 66 6f 72 20 73  made clean for s
130ad 6f 6d 65 20 6f 74 68 65 72 20 72 65 61 73 6f 6e  ome other reason
130ae 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f  , but no error o
130af 63 63 75 72 73 2c 20 74 68 65 6e 20 53 51 4c 49  ccurs, then SQLI
130b0 54 45 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75  TE_OK.** is retu
130b1 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 50  rned by sqlite3P
130b2 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29  cacheMakeClean()
130b3 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 2e 0a   is not called..
130b4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
130b5 67 65 72 53 74 72 65 73 73 28 76 6f 69 64 20 2a  gerStress(void *
130b6 70 2c 20 50 67 48 64 72 20 2a 70 50 67 29 7b 0a  p, PgHdr *pPg){.
130b7 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
130b8 3d 20 28 50 61 67 65 72 20 2a 29 70 3b 0a 20 20  = (Pager *)p;.  
130b9 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
130ba 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  OK;..  assert( p
130bb 50 67 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67  Pg->pPager==pPag
130bc 65 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  er );.  assert( 
130bd 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
130be 5f 44 49 52 54 59 20 29 3b 0a 0a 20 20 2f 2a 20  _DIRTY );..  /* 
130bf 54 68 65 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c  The doNotSync fl
130c0 61 67 20 69 73 20 73 65 74 20 62 79 20 74 68 65  ag is set by the
130c1 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
130c2 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 68  te() function wh
130c3 69 6c 65 20 69 74 0a 20 20 2a 2a 20 69 73 20 6a  ile it.  ** is j
130c4 6f 75 72 6e 61 6c 6c 69 6e 67 20 61 20 73 65 74  ournalling a set
130c5 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   of two or more 
130c6 64 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74  database pages t
130c7 68 61 74 20 61 72 65 20 73 74 6f 72 65 64 0a 20  hat are stored. 
130c8 20 2a 2a 20 6f 6e 20 74 68 65 20 73 61 6d 65 20   ** on the same 
130c9 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 53 79 6e  disk sector. Syn
130ca 63 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c  cing the journal
130cb 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20   is not allowed 
130cc 77 68 69 6c 65 0a 20 20 2a 2a 20 74 68 69 73 20  while.  ** this 
130cd 69 73 20 68 61 70 70 65 6e 69 6e 67 20 61 73 20  is happening as 
130ce 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  it is important 
130cf 74 68 61 74 20 61 6c 6c 20 6d 65 6d 62 65 72 73  that all members
130d0 20 6f 66 20 73 75 63 68 20 61 0a 20 20 2a 2a 20   of such a.  ** 
130d1 73 65 74 20 6f 66 20 70 61 67 65 73 20 61 72 65  set of pages are
130d2 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20   synced to disk 
130d3 74 6f 67 65 74 68 65 72 2e 20 53 6f 2c 20 69 66  together. So, if
130d4 20 74 68 65 20 70 61 67 65 20 74 68 69 73 20 66   the page this f
130d5 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20  unction.  ** is 
130d6 74 72 79 69 6e 67 20 74 6f 20 6d 61 6b 65 20 63  trying to make c
130d7 6c 65 61 6e 20 77 69 6c 6c 20 72 65 71 75 69 72  lean will requir
130d8 65 20 61 20 6a 6f 75 72 6e 61 6c 20 73 79 6e 63  e a journal sync
130d9 20 61 6e 64 20 74 68 65 20 64 6f 4e 6f 74 53 79   and the doNotSy
130da 6e 63 0a 20 20 2a 2a 20 66 6c 61 67 20 69 73 20  nc.  ** flag is 
130db 73 65 74 2c 20 72 65 74 75 72 6e 20 77 69 74 68  set, return with
130dc 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68 69  out doing anythi
130dd 6e 67 2e 20 54 68 65 20 70 63 61 63 68 65 20 6c  ng. The pcache l
130de 61 79 65 72 20 77 69 6c 6c 0a 20 20 2a 2a 20 6a  ayer will.  ** j
130df 75 73 74 20 68 61 76 65 20 74 6f 20 67 6f 20 61  ust have to go a
130e0 68 65 61 64 20 61 6e 64 20 61 6c 6c 6f 63 61 74  head and allocat
130e1 65 20 61 20 6e 65 77 20 70 61 67 65 20 62 75 66  e a new page buf
130e2 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 0a 20  fer instead of. 
130e3 20 2a 2a 20 72 65 75 73 69 6e 67 20 70 50 67 2e   ** reusing pPg.
130e4 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53 69 6d 69 6c  .  **.  ** Simil
130e5 61 72 6c 79 2c 20 69 66 20 74 68 65 20 70 61 67  arly, if the pag
130e6 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 65  er has already e
130e7 6e 74 65 72 65 64 20 74 68 65 20 65 72 72 6f 72  ntered the error
130e8 20 73 74 61 74 65 2c 20 64 6f 20 6e 6f 74 0a 20   state, do not. 
130e9 20 2a 2a 20 74 72 79 20 74 6f 20 77 72 69 74 65   ** try to write
130ea 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
130eb 20 70 50 67 20 74 6f 20 64 69 73 6b 2e 0a 20 20   pPg to disk..  
130ec 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  */.  if( NEVER(p
130ed 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 0a  Pager->errCode).
130ee 20 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 64     || (pPager->d
130ef 6f 4e 6f 74 53 79 6e 63 20 26 26 20 70 50 67 2d  oNotSync && pPg-
130f0 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
130f1 44 5f 53 59 4e 43 29 0a 20 20 29 7b 0a 20 20 20  D_SYNC).  ){.   
130f2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
130f3 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 79 6e  K;.  }..  /* Syn
130f4 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  c the journal fi
130f5 6c 65 20 69 66 20 72 65 71 75 69 72 65 64 2e 20  le if required. 
130f6 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d 3e 66 6c  */.  if( pPg->fl
130f7 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
130f8 59 4e 43 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  YNC ){.    rc = 
130f9 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67  syncJournal(pPag
130fa 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  er);.    if( rc=
130fb 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50  =SQLITE_OK && pP
130fc 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 26  ager->fullSync &
130fd 26 20 0a 20 20 20 20 20 20 21 28 70 50 61 67 65  & .      !(pPage
130fe 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
130ff 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
13100 45 5f 4d 45 4d 4f 52 59 29 20 26 26 0a 20 20 20  E_MEMORY) &&.   
13101 20 20 20 21 28 73 71 6c 69 74 65 33 4f 73 44 65     !(sqlite3OsDe
13102 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
13103 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 26  ics(pPager->fd)&
13104 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 41 46  SQLITE_IOCAP_SAF
13105 45 5f 41 50 50 45 4e 44 29 0a 20 20 20 20 29 7b  E_APPEND).    ){
13106 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e  .      pPager->n
13107 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 72  Rec = 0;.      r
13108 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c  c = writeJournal
13109 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 20  Hdr(pPager);.   
1310a 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
1310b 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
1310c 6f 66 20 74 68 69 73 20 70 61 67 65 20 69 73 20  of this page is 
1310d 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
1310e 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 0a  current size of.
1310f 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73    ** the databas
13110 65 20 69 6d 61 67 65 2c 20 69 74 20 6d 61 79 20  e image, it may 
13111 6e 65 65 64 20 74 6f 20 62 65 20 77 72 69 74 74  need to be writt
13112 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f  en to the sub-jo
13113 75 72 6e 61 6c 2e 0a 20 20 2a 2a 20 54 68 69 73  urnal..  ** This
13114 20 69 73 20 62 65 63 61 75 73 65 20 74 68 65 20   is because the 
13115 63 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 77 72  call to pager_wr
13116 69 74 65 5f 70 61 67 65 6c 69 73 74 28 29 20 62  ite_pagelist() b
13117 65 6c 6f 77 20 77 69 6c 6c 20 6e 6f 74 0a 20 20  elow will not.  
13118 2a 2a 20 61 63 74 75 61 6c 6c 79 20 77 72 69 74  ** actually writ
13119 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69  e data to the fi
1311a 6c 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  le in this case.
1311b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f 6e 73 69  .  **.  ** Consi
1311c 64 65 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  der the followin
1311d 67 20 73 65 71 75 65 6e 63 65 20 6f 66 20 65 76  g sequence of ev
1311e 65 6e 74 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ents:.  **.  ** 
1311f 20 20 42 45 47 49 4e 3b 0a 20 20 2a 2a 20 20 20    BEGIN;.  **   
13120 20 20 3c 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20    <journal page 
13121 58 3e 0a 20 20 2a 2a 20 20 20 20 20 3c 6d 6f 64  X>.  **     <mod
13122 69 66 79 20 70 61 67 65 20 58 3e 0a 20 20 2a 2a  ify page X>.  **
13123 20 20 20 20 20 53 41 56 45 50 4f 49 4e 54 20 73       SAVEPOINT s
13124 70 3b 0a 20 20 2a 2a 20 20 20 20 20 20 20 3c 73  p;.  **       <s
13125 68 72 69 6e 6b 20 64 61 74 61 62 61 73 65 20 66  hrink database f
13126 69 6c 65 20 74 6f 20 59 20 70 61 67 65 73 3e 0a  ile to Y pages>.
13127 20 20 2a 2a 20 20 20 20 20 20 20 70 61 67 65 72    **       pager
13128 53 74 72 65 73 73 28 70 61 67 65 20 58 29 0a 20  Stress(page X). 
13129 20 2a 2a 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b   **     ROLLBACK
1312a 20 54 4f 20 73 70 3b 0a 20 20 2a 2a 0a 20 20 2a   TO sp;.  **.  *
1312b 2a 20 49 66 20 28 58 3e 59 29 2c 20 74 68 65 6e  * If (X>Y), then
1312c 20 77 68 65 6e 20 70 61 67 65 72 53 74 72 65 73   when pagerStres
1312d 73 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65  s is called page
1312e 20 58 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 77   X will not be w
1312f 72 69 74 74 65 6e 0a 20 20 2a 2a 20 6f 75 74 20  ritten.  ** out 
13130 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
13131 66 69 6c 65 2c 20 62 75 74 20 77 69 6c 6c 20 62  file, but will b
13132 65 20 64 72 6f 70 70 65 64 20 66 72 6f 6d 20 74  e dropped from t
13133 68 65 20 63 61 63 68 65 2e 20 54 68 65 6e 2c 0a  he cache. Then,.
13134 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74    ** following t
13135 68 65 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20  he "ROLLBACK TO 
13136 73 70 22 20 73 74 61 74 65 6d 65 6e 74 2c 20 72  sp" statement, r
13137 65 61 64 69 6e 67 20 70 61 67 65 20 58 20 77 69  eading page X wi
13138 6c 6c 20 72 65 61 64 0a 20 20 2a 2a 20 64 61 74  ll read.  ** dat
13139 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  a from the datab
1313a 61 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 77  ase file. This w
1313b 69 6c 6c 20 62 65 20 74 68 65 20 63 6f 70 79 20  ill be the copy 
1313c 6f 66 20 70 61 67 65 20 58 20 61 73 20 69 74 0a  of page X as it.
1313d 20 20 2a 2a 20 77 61 73 20 77 68 65 6e 20 74 68    ** was when th
1313e 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74  e transaction st
1313f 61 72 74 65 64 2c 20 6e 6f 74 20 61 73 20 69 74  arted, not as it
13140 20 77 61 73 20 77 68 65 6e 20 22 53 41 56 45 50   was when "SAVEP
13141 4f 49 4e 54 20 73 70 22 0a 20 20 2a 2a 20 77 61  OINT sp".  ** wa
13142 73 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a 2a  s executed..  **
13143 0a 20 20 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69  .  ** The soluti
13144 6f 6e 20 69 73 20 74 6f 20 77 72 69 74 65 20 74  on is to write t
13145 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61 20  he current data 
13146 66 6f 72 20 70 61 67 65 20 58 20 69 6e 74 6f 20  for page X into 
13147 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d 6a 6f  the .  ** sub-jo
13148 75 72 6e 61 6c 20 66 69 6c 65 20 6e 6f 77 20 28  urnal file now (
13149 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72  if it is not alr
1314a 65 61 64 79 20 74 68 65 72 65 29 2c 20 73 6f 20  eady there), so 
1314b 74 68 61 74 20 69 74 20 77 69 6c 6c 0a 20 20 2a  that it will.  *
1314c 2a 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f  * be restored to
1314d 20 69 74 73 20 63 75 72 72 65 6e 74 20 76 61 6c   its current val
1314e 75 65 20 77 68 65 6e 20 74 68 65 20 22 52 4f 4c  ue when the "ROL
1314f 4c 42 41 43 4b 20 54 4f 20 73 70 22 20 69 73 20  LBACK TO sp" is 
13150 0a 20 20 2a 2a 20 65 78 65 63 75 74 65 64 2e 0a  .  ** executed..
13151 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52    */.  if( NEVER
13152 28 0a 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49  (.      rc==SQLI
13153 54 45 5f 4f 4b 20 26 26 20 70 50 67 2d 3e 70 67  TE_OK && pPg->pg
13154 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  no>pPager->dbSiz
13155 65 20 26 26 20 73 75 62 6a 52 65 71 75 69 72 65  e && subjRequire
13156 73 50 61 67 65 28 70 50 67 29 0a 20 20 29 20 29  sPage(pPg).  ) )
13157 7b 0a 20 20 20 20 72 63 20 3d 20 73 75 62 6a 6f  {.    rc = subjo
13158 75 72 6e 61 6c 50 61 67 65 28 70 50 67 29 3b 0a  urnalPage(pPg);.
13159 20 20 7d 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20    }..  /* Write 
1315a 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
1315b 74 68 65 20 70 61 67 65 20 6f 75 74 20 74 6f 20  the page out to 
1315c 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1315d 65 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  e. */.  if( rc==
1315e 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1315f 20 70 50 67 2d 3e 70 44 69 72 74 79 20 3d 20 30   pPg->pDirty = 0
13160 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72  ;.    rc = pager
13161 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28  _write_pagelist(
13162 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  pPg);.  }..  /* 
13163 4d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61 73  Mark the page as
13164 20 63 6c 65 61 6e 2e 20 2a 2f 0a 20 20 69 66 28   clean. */.  if(
13165 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
13166 7b 0a 20 20 20 20 50 41 47 45 52 54 52 41 43 45  {.    PAGERTRACE
13167 28 28 22 53 54 52 45 53 53 20 25 64 20 70 61 67  (("STRESS %d pag
13168 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  e %d\n", PAGERID
13169 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70  (pPager), pPg->p
1316a 67 6e 6f 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  gno));.    sqlit
1316b 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61  e3PcacheMakeClea
1316c 6e 28 70 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 72  n(pPg);.  }..  r
1316d 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f  eturn pager_erro
1316e 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 7d  r(pPager, rc);.}
1316f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  .../*.** Allocat
13170 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  e and initialize
13171 20 61 20 6e 65 77 20 50 61 67 65 72 20 6f 62 6a   a new Pager obj
13172 65 63 74 20 61 6e 64 20 70 75 74 20 61 20 70 6f  ect and put a po
13173 69 6e 74 65 72 20 74 6f 20 69 74 0a 2a 2a 20 69  inter to it.** i
13174 6e 20 2a 70 70 50 61 67 65 72 2e 20 54 68 65 20  n *ppPager. The 
13175 70 61 67 65 72 20 73 68 6f 75 6c 64 20 65 76 65  pager should eve
13176 6e 74 75 61 6c 6c 79 20 62 65 20 66 72 65 65 64  ntually be freed
13177 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 0a 2a   by passing it.*
13178 2a 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65  * to sqlite3Page
13179 72 43 6c 6f 73 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  rClose()..**.** 
1317a 54 68 65 20 7a 46 69 6c 65 6e 61 6d 65 20 61 72  The zFilename ar
1317b 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 61  gument is the pa
1317c 74 68 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  th to the databa
1317d 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 2e  se file to open.
1317e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65  .** If zFilename
1317f 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 20   is NULL then a 
13180 72 61 6e 64 6f 6d 6c 79 2d 6e 61 6d 65 64 20 74  randomly-named t
13181 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73  emporary file is
13182 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20   created.** and 
13183 75 73 65 64 20 61 73 20 74 68 65 20 66 69 6c 65  used as the file
13184 20 74 6f 20 62 65 20 63 61 63 68 65 64 2e 20 54   to be cached. T
13185 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20 61  emporary files a
13186 72 65 20 62 65 20 64 65 6c 65 74 65 64 0a 2a 2a  re be deleted.**
13187 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77   automatically w
13188 68 65 6e 20 74 68 65 79 20 61 72 65 20 63 6c 6f  hen they are clo
13189 73 65 64 2e 20 49 66 20 7a 46 69 6c 65 6e 61 6d  sed. If zFilenam
1318a 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 20  e is ":memory:" 
1318b 74 68 65 6e 20 0a 2a 2a 20 61 6c 6c 20 69 6e 66  then .** all inf
1318c 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68 65 6c 64  ormation is held
1318d 20 69 6e 20 63 61 63 68 65 2e 20 49 74 20 69 73   in cache. It is
1318e 20 6e 65 76 65 72 20 77 72 69 74 74 65 6e 20 74   never written t
1318f 6f 20 64 69 73 6b 2e 20 0a 2a 2a 20 54 68 69 73  o disk. .** This
13190 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
13191 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d  implement an in-
13192 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2e  memory database.
13193 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 45 78 74 72  .**.** The nExtr
13194 61 20 70 61 72 61 6d 65 74 65 72 20 73 70 65 63  a parameter spec
13195 69 66 69 65 73 20 74 68 65 20 6e 75 6d 62 65 72  ifies the number
13196 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61   of bytes of spa
13197 63 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20  ce allocated.** 
13198 61 6c 6f 6e 67 20 77 69 74 68 20 65 61 63 68 20  along with each 
13199 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20  page reference. 
1319a 54 68 69 73 20 73 70 61 63 65 20 69 73 20 61 76  This space is av
1319b 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 75  ailable to the u
1319c 73 65 72 0a 2a 2a 20 76 69 61 20 74 68 65 20 73  ser.** via the s
1319d 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78  qlite3PagerGetEx
1319e 74 72 61 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a  tra() API..**.**
1319f 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d   The flags argum
131a0 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 73  ent is used to s
131a1 70 65 63 69 66 79 20 70 72 6f 70 65 72 74 69 65  pecify propertie
131a2 73 20 74 68 61 74 20 61 66 66 65 63 74 20 74 68  s that affect th
131a3 65 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6f  e.** operation o
131a4 66 20 74 68 65 20 70 61 67 65 72 2e 20 49 74 20  f the pager. It 
131a5 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
131a6 20 73 6f 6d 65 20 62 69 74 77 69 73 65 20 63 6f   some bitwise co
131a7 6d 62 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20  mbination.** of 
131a8 74 68 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a  the PAGER_OMIT_J
131a9 4f 55 52 4e 41 4c 20 61 6e 64 20 50 41 47 45 52  OURNAL and PAGER
131aa 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 66 6c 61  _NO_READLOCK fla
131ab 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 66  gs..**.** The vf
131ac 73 46 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72  sFlags parameter
131ad 20 69 73 20 61 20 62 69 74 6d 61 73 6b 20 74 6f   is a bitmask to
131ae 20 70 61 73 73 20 74 6f 20 74 68 65 20 66 6c 61   pass to the fla
131af 67 73 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  gs parameter.** 
131b0 6f 66 20 74 68 65 20 78 4f 70 65 6e 28 29 20 6d  of the xOpen() m
131b1 65 74 68 6f 64 20 6f 66 20 74 68 65 20 73 75 70  ethod of the sup
131b2 70 6c 69 65 64 20 56 46 53 20 77 68 65 6e 20 6f  plied VFS when o
131b3 70 65 6e 69 6e 67 20 66 69 6c 65 73 2e 20 0a 2a  pening files. .*
131b4 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  *.** If the page
131b5 72 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f  r object is allo
131b6 63 61 74 65 64 20 61 6e 64 20 74 68 65 20 73 70  cated and the sp
131b7 65 63 69 66 69 65 64 20 66 69 6c 65 20 6f 70 65  ecified file ope
131b8 6e 65 64 20 0a 2a 2a 20 73 75 63 63 65 73 73 66  ned .** successf
131b9 75 6c 6c 79 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  ully, SQLITE_OK 
131ba 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
131bb 2a 70 70 50 61 67 65 72 20 73 65 74 20 74 6f 20  *ppPager set to 
131bc 70 6f 69 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20  point to.** the 
131bd 6e 65 77 20 70 61 67 65 72 20 6f 62 6a 65 63 74  new pager object
131be 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
131bf 63 75 72 73 2c 20 2a 70 70 50 61 67 65 72 20 69  curs, *ppPager i
131c0 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 0a 2a 2a  s set to NULL.**
131c1 20 61 6e 64 20 65 72 72 6f 72 20 63 6f 64 65 20   and error code 
131c2 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73 20 66  returned. This f
131c3 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75  unction may retu
131c4 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a  rn SQLITE_NOMEM.
131c5 2a 2a 20 28 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  ** (sqlite3Mallo
131c6 63 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61  c() is used to a
131c7 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 29 2c  llocate memory),
131c8 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e   SQLITE_CANTOPEN
131c9 20 6f 72 20 0a 2a 2a 20 76 61 72 69 6f 75 73 20   or .** various 
131ca 53 51 4c 49 54 45 5f 49 4f 5f 58 58 58 20 65 72  SQLITE_IO_XXX er
131cb 72 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rors..*/.SQLITE_
131cc 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
131cd 74 65 33 50 61 67 65 72 4f 70 65 6e 28 0a 20 20  te3PagerOpen(.  
131ce 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
131cf 73 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  s,       /* The 
131d0 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73  virtual file sys
131d1 74 65 6d 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20  tem to use */.  
131d2 50 61 67 65 72 20 2a 2a 70 70 50 61 67 65 72 2c  Pager **ppPager,
131d3 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
131d4 20 52 65 74 75 72 6e 20 74 68 65 20 50 61 67 65   Return the Page
131d5 72 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65  r structure here
131d6 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
131d7 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f   *zFilename,   /
131d8 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * Name of the da
131d9 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 6f  tabase file to o
131da 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 78  pen */.  int nEx
131db 74 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20  tra,            
131dc 20 20 2f 2a 20 45 78 74 72 61 20 62 79 74 65 73    /* Extra bytes
131dd 20 61 70 70 65 6e 64 20 74 6f 20 65 61 63 68 20   append to each 
131de 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a  in-memory page *
131df 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
131e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
131e1 66 6c 61 67 73 20 63 6f 6e 74 72 6f 6c 6c 69 6e  flags controllin
131e2 67 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20  g this file */. 
131e3 20 69 6e 74 20 76 66 73 46 6c 61 67 73 2c 20 20   int vfsFlags,  
131e4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61            /* fla
131e5 67 73 20 70 61 73 73 65 64 20 74 68 72 6f 75 67  gs passed throug
131e6 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 66 73  h to sqlite3_vfs
131e7 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 76 6f  .xOpen() */.  vo
131e8 69 64 20 28 2a 78 52 65 69 6e 69 74 29 28 44 62  id (*xReinit)(Db
131e9 50 61 67 65 2a 29 20 2f 2a 20 46 75 6e 63 74 69  Page*) /* Functi
131ea 6f 6e 20 74 6f 20 72 65 69 6e 69 74 69 61 6c 69  on to reinitiali
131eb 7a 65 20 70 61 67 65 73 20 2a 2f 0a 29 7b 0a 20  ze pages */.){. 
131ec 20 75 38 20 2a 70 50 74 72 3b 0a 20 20 50 61 67   u8 *pPtr;.  Pag
131ed 65 72 20 2a 70 50 61 67 65 72 20 3d 20 30 3b 20  er *pPager = 0; 
131ee 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 6f        /* Pager o
131ef 62 6a 65 63 74 20 74 6f 20 61 6c 6c 6f 63 61 74  bject to allocat
131f0 65 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a  e and return */.
131f1 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
131f2 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a 20 52 65  E_OK;      /* Re
131f3 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69  turn code */.  i
131f4 6e 74 20 74 65 6d 70 46 69 6c 65 20 3d 20 30 3b  nt tempFile = 0;
131f5 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
131f6 66 6f 72 20 74 65 6d 70 20 66 69 6c 65 73 20 28  for temp files (
131f7 69 6e 63 6c 2e 20 69 6e 2d 6d 65 6d 6f 72 79 20  incl. in-memory 
131f8 66 69 6c 65 73 29 20 2a 2f 0a 20 20 69 6e 74 20  files) */.  int 
131f9 6d 65 6d 44 62 20 3d 20 30 3b 20 20 20 20 20 20  memDb = 0;      
131fa 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
131fb 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65  this is an in-me
131fc 6d 6f 72 79 20 66 69 6c 65 20 2a 2f 0a 20 20 69  mory file */.  i
131fd 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b  nt readOnly = 0;
131fe 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
131ff 69 66 20 74 68 69 73 20 69 73 20 61 20 72 65 61  if this is a rea
13200 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 2a 2f 0a 20  d-only file */. 
13201 20 69 6e 74 20 6a 6f 75 72 6e 61 6c 46 69 6c 65   int journalFile
13202 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 42 79 74  Size;     /* Byt
13203 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 66  es to allocate f
13204 6f 72 20 65 61 63 68 20 6a 6f 75 72 6e 61 6c 20  or each journal 
13205 66 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 50  fd */.  char *zP
13206 61 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20  athname = 0;    
13207 20 2f 2a 20 46 75 6c 6c 20 70 61 74 68 20 74 6f   /* Full path to
13208 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
13209 2f 0a 20 20 69 6e 74 20 6e 50 61 74 68 6e 61 6d  /.  int nPathnam
1320a 65 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  e = 0;       /* 
1320b 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1320c 69 6e 20 7a 50 61 74 68 6e 61 6d 65 20 2a 2f 0a  in zPathname */.
1320d 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 6e 61 6c    int useJournal
1320e 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47 45   = (flags & PAGE
1320f 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 29 3d  R_OMIT_JOURNAL)=
13210 3d 30 3b 20 2f 2a 20 46 61 6c 73 65 20 74 6f 20  =0; /* False to 
13211 6f 6d 69 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  omit journal */.
13212 20 20 69 6e 74 20 6e 6f 52 65 61 64 6c 6f 63 6b    int noReadlock
13213 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47 45   = (flags & PAGE
13214 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 29 21 3d  R_NO_READLOCK)!=
13215 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f  0;  /* True to o
13216 6d 69 74 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f  mit read-lock */
13217 0a 20 20 69 6e 74 20 70 63 61 63 68 65 53 69 7a  .  int pcacheSiz
13218 65 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68  e = sqlite3Pcach
13219 65 53 69 7a 65 28 29 3b 20 20 20 20 20 20 20 2f  eSize();       /
1321a 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63  * Bytes to alloc
1321b 61 74 65 20 66 6f 72 20 50 43 61 63 68 65 20 2a  ate for PCache *
1321c 2f 0a 20 20 75 31 36 20 73 7a 50 61 67 65 44 66  /.  u16 szPageDf
1321d 6c 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  lt = SQLITE_DEFA
1321e 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20 20  ULT_PAGE_SIZE;  
1321f 2f 2a 20 44 65 66 61 75 6c 74 20 70 61 67 65 20  /* Default page 
13220 73 69 7a 65 20 2a 2f 0a 0a 20 20 2f 2a 20 46 69  size */..  /* Fi
13221 67 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 75 63  gure out how muc
13222 68 20 73 70 61 63 65 20 69 73 20 72 65 71 75 69  h space is requi
13223 72 65 64 20 66 6f 72 20 65 61 63 68 20 6a 6f 75  red for each jou
13224 72 6e 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c 65  rnal file-handle
13225 0a 20 20 2a 2a 20 28 74 68 65 72 65 20 61 72 65  .  ** (there are
13226 20 74 77 6f 20 6f 66 20 74 68 65 6d 2c 20 74 68   two of them, th
13227 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 61  e main journal a
13228 6e 64 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  nd the sub-journ
13229 61 6c 29 2e 20 54 68 69 73 0a 20 20 2a 2a 20 69  al). This.  ** i
1322a 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 70  s the maximum sp
1322b 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72  ace required for
1322c 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f   an in-memory jo
1322d 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c  urnal file handl
1322e 65 20 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65  e .  ** and a re
1322f 67 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 20 66 69  gular journal fi
13230 6c 65 2d 68 61 6e 64 6c 65 2e 20 4e 6f 74 65 20  le-handle. Note 
13231 74 68 61 74 20 61 20 22 72 65 67 75 6c 61 72 20  that a "regular 
13232 6a 6f 75 72 6e 61 6c 2d 68 61 6e 64 6c 65 22 0a  journal-handle".
13233 20 20 2a 2a 20 6d 61 79 20 62 65 20 61 20 77 72    ** may be a wr
13234 61 70 70 65 72 20 63 61 70 61 62 6c 65 20 6f 66  apper capable of
13235 20 63 61 63 68 69 6e 67 20 74 68 65 20 66 69 72   caching the fir
13236 73 74 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68  st portion of th
13237 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66  e journal.  ** f
13238 69 6c 65 20 69 6e 20 6d 65 6d 6f 72 79 20 74 6f  ile in memory to
13239 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 61   implement the a
1323a 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69  tomic-write opti
1323b 6d 69 7a 61 74 69 6f 6e 20 28 73 65 65 20 0a 20  mization (see . 
1323c 20 2a 2a 20 73 6f 75 72 63 65 20 66 69 6c 65 20   ** source file 
1323d 6a 6f 75 72 6e 61 6c 2e 63 29 2e 0a 20 20 2a 2f  journal.c)..  */
1323e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4a 6f  .  if( sqlite3Jo
1323f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 3e  urnalSize(pVfs)>
13240 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
13241 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20 20 20 6a  lSize() ){.    j
13242 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d  ournalFileSize =
13243 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 65 33 4a   ROUND8(sqlite3J
13244 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29  ournalSize(pVfs)
13245 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
13246 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20  journalFileSize 
13247 3d 20 52 4f 55 4e 44 38 28 73 71 6c 69 74 65 33  = ROUND8(sqlite3
13248 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 29  MemJournalSize()
13249 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74  );.  }..  /* Set
1324a 20 74 68 65 20 6f 75 74 70 75 74 20 76 61 72 69   the output vari
1324b 61 62 6c 65 20 74 6f 20 4e 55 4c 4c 20 69 6e 20  able to NULL in 
1324c 63 61 73 65 20 61 6e 20 65 72 72 6f 72 20 6f 63  case an error oc
1324d 63 75 72 73 2e 20 2a 2f 0a 20 20 2a 70 70 50 61  curs. */.  *ppPa
1324e 67 65 72 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43  ger = 0;..  /* C
1324f 6f 6d 70 75 74 65 20 61 6e 64 20 73 74 6f 72 65  ompute and store
13250 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61   the full pathna
13251 6d 65 20 69 6e 20 61 6e 20 61 6c 6c 6f 63 61 74  me in an allocat
13252 65 64 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  ed buffer pointe
13253 64 0a 20 20 2a 2a 20 74 6f 20 62 79 20 7a 50 61  d.  ** to by zPa
13254 74 68 6e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e  thname, length n
13255 50 61 74 68 6e 61 6d 65 2e 20 4f 72 2c 20 69 66  Pathname. Or, if
13256 20 74 68 69 73 20 69 73 20 61 20 74 65 6d 70 6f   this is a tempo
13257 72 61 72 79 20 66 69 6c 65 2c 0a 20 20 2a 2a 20  rary file,.  ** 
13258 6c 65 61 76 65 20 62 6f 74 68 20 6e 50 61 74 68  leave both nPath
13259 6e 61 6d 65 20 61 6e 64 20 7a 50 61 74 68 6e 61  name and zPathna
1325a 6d 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 2a  me set to 0..  *
1325b 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d  /.  if( zFilenam
1325c 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30  e && zFilename[0
1325d 5d 20 29 7b 0a 20 20 20 20 6e 50 61 74 68 6e 61  ] ){.    nPathna
1325e 6d 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74  me = pVfs->mxPat
1325f 68 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 7a 50 61  hname+1;.    zPa
13260 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  thname = sqlite3
13261 4d 61 6c 6c 6f 63 28 6e 50 61 74 68 6e 61 6d 65  Malloc(nPathname
13262 2a 32 29 3b 0a 20 20 20 20 69 66 28 20 7a 50 61  *2);.    if( zPa
13263 74 68 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20  thname==0 ){.   
13264 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
13265 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 23 69  _NOMEM;.    }.#i
13266 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
13267 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 69  T_MEMORYDB.    i
13268 66 28 20 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e  f( strcmp(zFilen
13269 61 6d 65 2c 22 3a 6d 65 6d 6f 72 79 3a 22 29 3d  ame,":memory:")=
1326a 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 44  =0 ){.      memD
1326b 62 20 3d 20 31 3b 0a 20 20 20 20 20 20 7a 50 61  b = 1;.      zPa
1326c 74 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 0a 20  thname[0] = 0;. 
1326d 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a     }else.#endif.
1326e 20 20 20 20 7b 0a 20 20 20 20 20 20 7a 50 61 74      {.      zPat
1326f 68 6e 61 6d 65 5b 30 5d 20 3d 20 30 3b 20 2f 2a  hname[0] = 0; /*
13270 20 4d 61 6b 65 20 73 75 72 65 20 69 6e 69 74 69   Make sure initi
13271 61 6c 69 7a 65 64 20 65 76 65 6e 20 69 66 20 46  alized even if F
13272 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 20 66 61  ullPathname() fa
13273 69 6c 73 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  ils */.      rc 
13274 3d 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50  = sqlite3OsFullP
13275 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46  athname(pVfs, zF
13276 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61  ilename, nPathna
13277 6d 65 2c 20 7a 50 61 74 68 6e 61 6d 65 29 3b 0a  me, zPathname);.
13278 20 20 20 20 7d 0a 0a 20 20 20 20 6e 50 61 74 68      }..    nPath
13279 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  name = sqlite3St
1327a 72 6c 65 6e 33 30 28 7a 50 61 74 68 6e 61 6d 65  rlen30(zPathname
1327b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
1327c 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 50 61 74  QLITE_OK && nPat
1327d 68 6e 61 6d 65 2b 38 3e 70 56 66 73 2d 3e 6d 78  hname+8>pVfs->mx
1327e 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20  Pathname ){.    
1327f 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63 68    /* This branch
13280 20 69 73 20 74 61 6b 65 6e 20 77 68 65 6e 20 74   is taken when t
13281 68 65 20 6a 6f 75 72 6e 61 6c 20 70 61 74 68 20  he journal path 
13282 72 65 71 75 69 72 65 64 20 62 79 0a 20 20 20 20  required by.    
13283 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73    ** the databas
13284 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 77  e being opened w
13285 69 6c 6c 20 62 65 20 6d 6f 72 65 20 74 68 61 6e  ill be more than
13286 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
13287 65 0a 20 20 20 20 20 20 2a 2a 20 62 79 74 65 73  e.      ** bytes
13288 20 69 6e 20 6c 65 6e 67 74 68 2e 20 54 68 69 73   in length. This
13289 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61 62   means the datab
1328a 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70  ase cannot be op
1328b 65 6e 65 64 2c 0a 20 20 20 20 20 20 2a 2a 20 61  ened,.      ** a
1328c 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65  s it will not be
1328d 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f 70 65   possible to ope
1328e 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
1328f 6c 65 20 6f 72 20 65 76 65 6e 0a 20 20 20 20 20  le or even.     
13290 20 2a 2a 20 63 68 65 63 6b 20 66 6f 72 20 61 20   ** check for a 
13291 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 65 66 6f  hot-journal befo
13292 72 65 20 72 65 61 64 69 6e 67 2e 0a 20 20 20 20  re reading..    
13293 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20    */.      rc = 
13294 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b  SQLITE_CANTOPEN;
13295 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
13296 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
13297 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
13298 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20  ee(zPathname);. 
13299 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1329a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
1329b 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  Allocate memory 
1329c 66 6f 72 20 74 68 65 20 50 61 67 65 72 20 73 74  for the Pager st
1329d 72 75 63 74 75 72 65 2c 20 50 43 61 63 68 65 20  ructure, PCache 
1329e 6f 62 6a 65 63 74 2c 20 74 68 65 0a 20 20 2a 2a  object, the.  **
1329f 20 74 68 72 65 65 20 66 69 6c 65 20 64 65 73 63   three file desc
132a0 72 69 70 74 6f 72 73 2c 20 74 68 65 20 64 61 74  riptors, the dat
132a1 61 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20  abase file name 
132a2 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  and the journal 
132a3 0a 20 20 2a 2a 20 66 69 6c 65 20 6e 61 6d 65 2e  .  ** file name.
132a4 20 54 68 65 20 6c 61 79 6f 75 74 20 69 6e 20 6d   The layout in m
132a5 65 6d 6f 72 79 20 69 73 20 61 73 20 66 6f 6c 6c  emory is as foll
132a6 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  ows:.  **.  **  
132a7 20 20 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20     Pager object 
132a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132a9 20 20 20 28 73 69 7a 65 6f 66 28 50 61 67 65 72     (sizeof(Pager
132aa 29 20 62 79 74 65 73 29 0a 20 20 2a 2a 20 20 20  ) bytes).  **   
132ab 20 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 20    PCache object 
132ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132ad 20 20 28 73 71 6c 69 74 65 33 50 63 61 63 68 65    (sqlite3Pcache
132ae 53 69 7a 65 28 29 20 62 79 74 65 73 29 0a 20 20  Size() bytes).  
132af 2a 2a 20 20 20 20 20 44 61 74 61 62 61 73 65 20  **     Database 
132b0 66 69 6c 65 20 68 61 6e 64 6c 65 20 20 20 20 20  file handle     
132b1 20 20 20 20 20 20 20 28 70 56 66 73 2d 3e 73 7a         (pVfs->sz
132b2 4f 73 46 69 6c 65 20 62 79 74 65 73 29 0a 20 20  OsFile bytes).  
132b3 2a 2a 20 20 20 20 20 53 75 62 2d 6a 6f 75 72 6e  **     Sub-journ
132b4 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 20  al file handle  
132b5 20 20 20 20 20 20 20 28 6a 6f 75 72 6e 61 6c 46         (journalF
132b6 69 6c 65 53 69 7a 65 20 62 79 74 65 73 29 0a 20  ileSize bytes). 
132b7 20 2a 2a 20 20 20 20 20 4d 61 69 6e 20 6a 6f 75   **     Main jou
132b8 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e 64 6c 65  rnal file handle
132b9 20 20 20 20 20 20 20 20 28 6a 6f 75 72 6e 61 6c          (journal
132ba 46 69 6c 65 53 69 7a 65 20 62 79 74 65 73 29 0a  FileSize bytes).
132bb 20 20 2a 2a 20 20 20 20 20 44 61 74 61 62 61 73    **     Databas
132bc 65 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 20 20  e file name     
132bd 20 20 20 20 20 20 20 20 20 28 6e 50 61 74 68 6e           (nPathn
132be 61 6d 65 2b 31 20 62 79 74 65 73 29 0a 20 20 2a  ame+1 bytes).  *
132bf 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c 20 66 69  *     Journal fi
132c0 6c 65 20 6e 61 6d 65 20 20 20 20 20 20 20 20 20  le name         
132c1 20 20 20 20 20 20 28 6e 50 61 74 68 6e 61 6d 65        (nPathname
132c2 2b 38 2b 31 20 62 79 74 65 73 29 0a 20 20 2a 2f  +8+1 bytes).  */
132c3 0a 20 20 70 50 74 72 20 3d 20 28 75 38 20 2a 29  .  pPtr = (u8 *)
132c4 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
132c5 6f 28 0a 20 20 20 20 52 4f 55 4e 44 38 28 73 69  o(.    ROUND8(si
132c6 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 29 20 2b  zeof(*pPager)) +
132c7 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 73        /* Pager s
132c8 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20  tructure */.    
132c9 52 4f 55 4e 44 38 28 70 63 61 63 68 65 53 69 7a  ROUND8(pcacheSiz
132ca 65 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f  e) +           /
132cb 2a 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 20  * PCache object 
132cc 2a 2f 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 56  */.    ROUND8(pV
132cd 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 20 2b 20  fs->szOsFile) + 
132ce 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69        /* The mai
132cf 6e 20 64 62 20 66 69 6c 65 20 2a 2f 0a 20 20 20  n db file */.   
132d0 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65   journalFileSize
132d1 20 2a 20 32 20 2b 20 20 20 20 20 20 20 20 20 20   * 2 +          
132d2 2f 2a 20 54 68 65 20 74 77 6f 20 6a 6f 75 72 6e  /* The two journ
132d3 61 6c 20 66 69 6c 65 73 20 2a 2f 20 0a 20 20 20  al files */ .   
132d4 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31 20 2b   nPathname + 1 +
132d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
132d6 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 2a 2f 0a  /* zFilename */.
132d7 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 2b 20      nPathname + 
132d8 38 20 2b 20 31 20 20 20 20 20 20 20 20 20 20 20  8 + 1           
132d9 20 20 20 2f 2a 20 7a 4a 6f 75 72 6e 61 6c 20 2a     /* zJournal *
132da 2f 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28  /.  );.  assert(
132db 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
132dc 4e 4d 45 4e 54 28 53 51 4c 49 54 45 5f 49 4e 54  NMENT(SQLITE_INT
132dd 5f 54 4f 5f 50 54 52 28 6a 6f 75 72 6e 61 6c 46  _TO_PTR(journalF
132de 69 6c 65 53 69 7a 65 29 29 20 29 3b 0a 20 20 69  ileSize)) );.  i
132df 66 28 20 21 70 50 74 72 20 29 7b 0a 20 20 20 20  f( !pPtr ){.    
132e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61  sqlite3_free(zPa
132e1 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  thname);.    ret
132e2 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
132e3 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 20 3d  ;.  }.  pPager =
132e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 50                (P
132e5 61 67 65 72 2a 29 28 70 50 74 72 29 3b 0a 20 20  ager*)(pPtr);.  
132e6 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 20  pPager->pPCache 
132e7 3d 20 20 20 20 28 50 43 61 63 68 65 2a 29 28 70  =    (PCache*)(p
132e8 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 28 73 69  Ptr += ROUND8(si
132e9 7a 65 6f 66 28 2a 70 50 61 67 65 72 29 29 29 3b  zeof(*pPager)));
132ea 0a 20 20 70 50 61 67 65 72 2d 3e 66 64 20 3d 20  .  pPager->fd = 
132eb 20 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a    (sqlite3_file*
132ec 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38  )(pPtr += ROUND8
132ed 28 70 63 61 63 68 65 53 69 7a 65 29 29 3b 0a 20  (pcacheSize));. 
132ee 20 70 50 61 67 65 72 2d 3e 73 6a 66 64 20 3d 20   pPager->sjfd = 
132ef 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 28  (sqlite3_file*)(
132f0 70 50 74 72 20 2b 3d 20 52 4f 55 4e 44 38 28 70  pPtr += ROUND8(p
132f1 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 29 3b  Vfs->szOsFile));
132f2 0a 20 20 70 50 61 67 65 72 2d 3e 6a 66 64 20 3d  .  pPager->jfd =
132f3 20 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a    (sqlite3_file*
132f4 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61  )(pPtr += journa
132f5 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 20 70 50  lFileSize);.  pP
132f6 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 20  ager->zFilename 
132f7 3d 20 20 20 20 28 63 68 61 72 2a 29 28 70 50 74  =    (char*)(pPt
132f8 72 20 2b 3d 20 6a 6f 75 72 6e 61 6c 46 69 6c 65  r += journalFile
132f9 53 69 7a 65 29 3b 0a 20 20 61 73 73 65 72 74 28  Size);.  assert(
132fa 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
132fb 4e 4d 45 4e 54 28 70 50 61 67 65 72 2d 3e 6a 66  NMENT(pPager->jf
132fc 64 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c  d) );..  /* Fill
132fd 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e 7a 46   in the Pager.zF
132fe 69 6c 65 6e 61 6d 65 20 61 6e 64 20 50 61 67 65  ilename and Page
132ff 72 2e 7a 4a 6f 75 72 6e 61 6c 20 62 75 66 66 65  r.zJournal buffe
13300 72 73 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e  rs, if required.
13301 20 2a 2f 0a 20 20 69 66 28 20 7a 50 61 74 68 6e   */.  if( zPathn
13302 61 6d 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65  ame ){.    pPage
13303 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d 20 20 20  r->zJournal =   
13304 28 63 68 61 72 2a 29 28 70 50 74 72 20 2b 3d 20  (char*)(pPtr += 
13305 6e 50 61 74 68 6e 61 6d 65 20 2b 20 31 29 3b 0a  nPathname + 1);.
13306 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65      memcpy(pPage
13307 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 50  r->zFilename, zP
13308 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68 6e 61  athname, nPathna
13309 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  me);.    memcpy(
1330a 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
1330b 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61  , zPathname, nPa
1330c 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d  thname);.    mem
1330d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a 6f  cpy(&pPager->zJo
1330e 75 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d  urnal[nPathname]
1330f 2c 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 20 38 29  , "-journal", 8)
13310 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  ;.    if( pPager
13311 2d 3e 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d  ->zFilename[0]==
13312 30 20 29 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  0 ) pPager->zJou
13313 72 6e 61 6c 5b 30 5d 20 3d 20 30 3b 0a 20 20 20  rnal[0] = 0;.   
13314 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50   sqlite3_free(zP
13315 61 74 68 6e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20  athname);.  }.  
13316 70 50 61 67 65 72 2d 3e 70 56 66 73 20 3d 20 70  pPager->pVfs = p
13317 56 66 73 3b 0a 20 20 70 50 61 67 65 72 2d 3e 76  Vfs;.  pPager->v
13318 66 73 46 6c 61 67 73 20 3d 20 76 66 73 46 6c 61  fsFlags = vfsFla
13319 67 73 3b 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74  gs;..  /* Open t
1331a 68 65 20 70 61 67 65 72 20 66 69 6c 65 2e 0a 20  he pager file.. 
1331b 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e   */.  if( zFilen
1331c 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65  ame && zFilename
1331d 5b 30 5d 20 26 26 20 21 6d 65 6d 44 62 20 29 7b  [0] && !memDb ){
1331e 0a 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20  .    int fout = 
1331f 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
13320 20 20 20 20 20 20 2f 2a 20 56 46 53 20 66 6c 61        /* VFS fla
13321 67 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 78  gs returned by x
13322 4f 70 65 6e 28 29 20 2a 2f 0a 20 20 20 20 72 63  Open() */.    rc
13323 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e   = sqlite3OsOpen
13324 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a  (pVfs, pPager->z
13325 46 69 6c 65 6e 61 6d 65 2c 20 70 50 61 67 65 72  Filename, pPager
13326 2d 3e 66 64 2c 20 76 66 73 46 6c 61 67 73 2c 20  ->fd, vfsFlags, 
13327 26 66 6f 75 74 29 3b 0a 20 20 20 20 72 65 61 64  &fout);.    read
13328 4f 6e 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c  Only = (fout&SQL
13329 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
1332a 59 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  Y);..    /* If t
1332b 68 65 20 66 69 6c 65 20 77 61 73 20 73 75 63 63  he file was succ
1332c 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64 20  essfully opened 
1332d 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20 61  for read/write a
1332e 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 63 68  ccess,.    ** ch
1332f 6f 6f 73 65 20 61 20 64 65 66 61 75 6c 74 20 70  oose a default p
13330 61 67 65 20 73 69 7a 65 20 69 6e 20 63 61 73 65  age size in case
13331 20 77 65 20 68 61 76 65 20 74 6f 20 63 72 65 61   we have to crea
13332 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61  te the.    ** da
13333 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65  tabase file. The
13334 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 73 69   default page si
13335 7a 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75  ze is the maximu
13336 6d 20 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  m of:.    **.   
13337 20 2a 2a 20 20 20 20 2b 20 53 51 4c 49 54 45 5f   **    + SQLITE_
13338 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
13339 45 2c 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54  E,.    **    + T
1333a 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
1333b 64 20 62 79 20 73 71 6c 69 74 65 33 4f 73 53 65  d by sqlite3OsSe
1333c 63 74 6f 72 53 69 7a 65 28 29 0a 20 20 20 20 2a  ctorSize().    *
1333d 2a 20 20 20 20 2b 20 54 68 65 20 6c 61 72 67 65  *    + The large
1333e 73 74 20 70 61 67 65 20 73 69 7a 65 20 74 68 61  st page size tha
1333f 74 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e  t can be written
13340 20 61 74 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20   atomically..   
13341 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
13342 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72 65  SQLITE_OK && !re
13343 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20  adOnly ){.      
13344 73 65 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50  setSectorSize(pP
13345 61 67 65 72 29 3b 0a 20 20 20 20 20 20 61 73 73  ager);.      ass
13346 65 72 74 28 53 51 4c 49 54 45 5f 44 45 46 41 55  ert(SQLITE_DEFAU
13347 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 53 51  LT_PAGE_SIZE<=SQ
13348 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
13349 5f 50 41 47 45 5f 53 49 5a 45 29 3b 0a 20 20 20  _PAGE_SIZE);.   
1334a 20 20 20 69 66 28 20 73 7a 50 61 67 65 44 66 6c     if( szPageDfl
1334b 74 3c 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72  t<pPager->sector
1334c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Size ){.        
1334d 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63 74  if( pPager->sect
1334e 6f 72 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41  orSize>SQLITE_MA
1334f 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  X_DEFAULT_PAGE_S
13350 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20  IZE ){.         
13351 20 73 7a 50 61 67 65 44 66 6c 74 20 3d 20 53 51   szPageDflt = SQ
13352 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
13353 5f 50 41 47 45 5f 53 49 5a 45 3b 0a 20 20 20 20  _PAGE_SIZE;.    
13354 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13355 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20       szPageDflt 
13356 3d 20 28 75 31 36 29 70 50 61 67 65 72 2d 3e 73  = (u16)pPager->s
13357 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20 20 20  ectorSize;.     
13358 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 69 66     }.      }.#if
13359 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
1335a 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20  E_ATOMIC_WRITE. 
1335b 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69       {.        i
1335c 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65 33  nt iDc = sqlite3
1335d 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65  OsDeviceCharacte
1335e 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e  ristics(pPager->
1335f 66 64 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  fd);.        int
13360 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61 73 73   ii;.        ass
13361 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43 41 50  ert(SQLITE_IOCAP
13362 5f 41 54 4f 4d 49 43 35 31 32 3d 3d 28 35 31 32  _ATOMIC512==(512
13363 3e 3e 38 29 29 3b 0a 20 20 20 20 20 20 20 20 61  >>8));.        a
13364 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43  ssert(SQLITE_IOC
13365 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36  AP_ATOMIC64K==(6
13366 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 20  5536>>8));.     
13367 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45     assert(SQLITE
13368 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
13369 45 5f 53 49 5a 45 3c 3d 36 35 35 33 36 29 3b 0a  E_SIZE<=65536);.
1336a 20 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 73          for(ii=s
1336b 7a 50 61 67 65 44 66 6c 74 3b 20 69 69 3c 3d 53  zPageDflt; ii<=S
1336c 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c  QLITE_MAX_DEFAUL
1336d 54 5f 50 41 47 45 5f 53 49 5a 45 3b 20 69 69 3d  T_PAGE_SIZE; ii=
1336e 69 69 2a 32 29 7b 0a 20 20 20 20 20 20 20 20 20  ii*2){.         
1336f 20 69 66 28 20 69 44 63 26 28 53 51 4c 49 54 45   if( iDc&(SQLITE
13370 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69  _IOCAP_ATOMIC|(i
13371 69 3e 3e 38 29 29 20 29 7b 0a 20 20 20 20 20 20  i>>8)) ){.      
13372 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74        szPageDflt
13373 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20   = ii;.         
13374 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
13375 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
13376 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  }.  }else{.    /
13377 2a 20 49 66 20 61 20 74 65 6d 70 6f 72 61 72 79  * If a temporary
13378 20 66 69 6c 65 20 69 73 20 72 65 71 75 65 73 74   file is request
13379 65 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 6f 70  ed, it is not op
1337a 65 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  ened immediately
1337b 2e 0a 20 20 20 20 2a 2a 20 49 6e 20 74 68 69 73  ..    ** In this
1337c 20 63 61 73 65 20 77 65 20 61 63 63 65 70 74 20   case we accept 
1337d 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65  the default page
1337e 20 73 69 7a 65 20 61 6e 64 20 64 65 6c 61 79 20   size and delay 
1337f 61 63 74 75 61 6c 6c 79 0a 20 20 20 20 2a 2a 20  actually.    ** 
13380 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c 65  opening the file
13381 20 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74   until the first
13382 20 63 61 6c 6c 20 74 6f 20 4f 73 57 72 69 74 65   call to OsWrite
13383 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ()..    **.    *
13384 2a 20 54 68 69 73 20 62 72 61 6e 63 68 20 69 73  * This branch is
13385 20 61 6c 73 6f 20 72 75 6e 20 66 6f 72 20 61 6e   also run for an
13386 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
13387 61 73 65 2e 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72  ase. An in-memor
13388 79 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73  y.    ** databas
13389 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  e is the same as
1338a 20 61 20 74 65 6d 70 2d 66 69 6c 65 20 74 68 61   a temp-file tha
1338b 74 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74  t is never writt
1338c 65 6e 20 6f 75 74 20 74 6f 0a 20 20 20 20 2a 2a  en out to.    **
1338d 20 64 69 73 6b 20 61 6e 64 20 75 73 65 73 20 61   disk and uses a
1338e 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c  n in-memory roll
1338f 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20  back journal..  
13390 20 20 2a 2f 20 0a 20 20 20 20 74 65 6d 70 46 69    */ .    tempFi
13391 6c 65 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67  le = 1;.    pPag
13392 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
13393 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 20  R_EXCLUSIVE;.   
13394 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 76 66 73   readOnly = (vfs
13395 46 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50 45  Flags&SQLITE_OPE
13396 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20 20 7d  N_READONLY);.  }
13397 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f  ..  /* The follo
13398 77 69 6e 67 20 63 61 6c 6c 20 74 6f 20 50 61 67  wing call to Pag
13399 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 29 20  erSetPagesize() 
1339a 73 65 72 76 65 73 20 74 6f 20 73 65 74 20 74 68  serves to set th
1339b 65 20 76 61 6c 75 65 20 6f 66 20 0a 20 20 2a 2a  e value of .  **
1339c 20 50 61 67 65 72 2e 70 61 67 65 53 69 7a 65 20   Pager.pageSize 
1339d 61 6e 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  and to allocate 
1339e 74 68 65 20 50 61 67 65 72 2e 70 54 6d 70 53 70  the Pager.pTmpSp
1339f 61 63 65 20 62 75 66 66 65 72 2e 0a 20 20 2a 2f  ace buffer..  */
133a0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
133a1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  E_OK ){.    asse
133a2 72 74 28 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44  rt( pPager->memD
133a3 62 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d  b==0 );.    rc =
133a4 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
133a5 50 61 67 65 73 69 7a 65 28 70 50 61 67 65 72 2c  Pagesize(pPager,
133a6 20 26 73 7a 50 61 67 65 44 66 6c 74 2c 20 2d 31   &szPageDflt, -1
133a7 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
133a8 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
133a9 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61  ;.  }..  /* If a
133aa 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  n error occurred
133ab 20 69 6e 20 65 69 74 68 65 72 20 6f 66 20 74 68   in either of th
133ac 65 20 62 6c 6f 63 6b 73 20 61 62 6f 76 65 2c 20  e blocks above, 
133ad 66 72 65 65 20 74 68 65 20 0a 20 20 2a 2a 20 50  free the .  ** P
133ae 61 67 65 72 20 73 74 72 75 63 74 75 72 65 20 61  ager structure a
133af 6e 64 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c  nd close the fil
133b0 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  e..  */.  if( rc
133b1 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
133b2 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67     assert( !pPag
133b3 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 29 3b  er->pTmpSpace );
133b4 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c  .    sqlite3OsCl
133b5 6f 73 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b  ose(pPager->fd);
133b6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
133b7 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 72  e(pPager);.    r
133b8 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
133b9 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
133ba 68 65 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74  he PCache object
133bb 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e  . */.  assert( n
133bc 45 78 74 72 61 3c 31 30 30 30 20 29 3b 0a 20 20  Extra<1000 );.  
133bd 6e 45 78 74 72 61 20 3d 20 52 4f 55 4e 44 38 28  nExtra = ROUND8(
133be 6e 45 78 74 72 61 29 3b 0a 20 20 73 71 6c 69 74  nExtra);.  sqlit
133bf 65 33 50 63 61 63 68 65 4f 70 65 6e 28 73 7a 50  e3PcacheOpen(szP
133c0 61 67 65 44 66 6c 74 2c 20 6e 45 78 74 72 61 2c  ageDflt, nExtra,
133c1 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20 20 20 20   !memDb,.       
133c2 20 20 20 20 20 20 20 20 20 20 20 20 20 21 6d 65               !me
133c3 6d 44 62 3f 70 61 67 65 72 53 74 72 65 73 73 3a  mDb?pagerStress:
133c4 30 2c 20 28 76 6f 69 64 20 2a 29 70 50 61 67 65  0, (void *)pPage
133c5 72 2c 20 70 50 61 67 65 72 2d 3e 70 50 43 61 63  r, pPager->pPCac
133c6 68 65 29 3b 0a 0a 20 20 50 41 47 45 52 54 52 41  he);..  PAGERTRA
133c7 43 45 28 28 22 4f 50 45 4e 20 25 64 20 25 73 5c  CE(("OPEN %d %s\
133c8 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c 45 49 44  n", FILEHANDLEID
133c9 28 70 50 61 67 65 72 2d 3e 66 64 29 2c 20 70 50  (pPager->fd), pP
133ca 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29  ager->zFilename)
133cb 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4f  );.  IOTRACE(("O
133cc 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20 70 50  PEN %p %s\n", pP
133cd 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 7a 46  ager, pPager->zF
133ce 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 70 50 61  ilename))..  pPa
133cf 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
133d0 3d 20 28 75 38 29 75 73 65 4a 6f 75 72 6e 61 6c  = (u8)useJournal
133d1 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65  ;.  pPager->noRe
133d2 61 64 6c 6f 63 6b 20 3d 20 28 6e 6f 52 65 61 64  adlock = (noRead
133d3 6c 6f 63 6b 20 26 26 20 72 65 61 64 4f 6e 6c 79  lock && readOnly
133d4 29 20 3f 31 3a 30 3b 0a 20 20 2f 2a 20 70 50 61  ) ?1:0;.  /* pPa
133d5 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20  ger->stmtOpen = 
133d6 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65  0; */.  /* pPage
133d7 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 3d 20 30  r->stmtInUse = 0
133d8 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72  ; */.  /* pPager
133d9 2d 3e 6e 52 65 66 20 3d 20 30 3b 20 2a 2f 0a 20  ->nRef = 0; */. 
133da 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56   pPager->dbSizeV
133db 61 6c 69 64 20 3d 20 28 75 38 29 6d 65 6d 44 62  alid = (u8)memDb
133dc 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73  ;.  /* pPager->s
133dd 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a  tmtSize = 0; */.
133de 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d    /* pPager->stm
133df 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a 20  tJSize = 0; */. 
133e0 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61 67   /* pPager->nPag
133e1 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  e = 0; */.  pPag
133e2 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d 20 53 51 4c  er->mxPgno = SQL
133e3 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55  ITE_MAX_PAGE_COU
133e4 4e 54 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  NT;.  /* pPager-
133e5 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 55  >state = PAGER_U
133e6 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20 20 61 73 73 65  NLOCK; */.  asse
133e7 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
133e8 65 20 3d 3d 20 28 74 65 6d 70 46 69 6c 65 20 3f  e == (tempFile ?
133e9 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
133ea 20 3a 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 29   : PAGER_UNLOCK)
133eb 20 29 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d   );.  /* pPager-
133ec 3e 65 72 72 4d 61 73 6b 20 3d 20 30 3b 20 2a 2f  >errMask = 0; */
133ed 0a 20 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  .  pPager->tempF
133ee 69 6c 65 20 3d 20 28 75 38 29 74 65 6d 70 46 69  ile = (u8)tempFi
133ef 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 74 65  le;.  assert( te
133f0 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c 4f  mpFile==PAGER_LO
133f1 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c  CKINGMODE_NORMAL
133f2 20 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 74   .          || t
133f3 65 6d 70 46 69 6c 65 3d 3d 50 41 47 45 52 5f 4c  empFile==PAGER_L
133f4 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55  OCKINGMODE_EXCLU
133f5 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74  SIVE );.  assert
133f6 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ( PAGER_LOCKINGM
133f7 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 3d 3d 31  ODE_EXCLUSIVE==1
133f8 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 78   );.  pPager->ex
133f9 63 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75  clusiveMode = (u
133fa 38 29 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20 70  8)tempFile; .  p
133fb 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75  Pager->changeCou
133fc 6e 74 44 6f 6e 65 20 3d 20 70 50 61 67 65 72 2d  ntDone = pPager-
133fd 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61  >tempFile;.  pPa
133fe 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20 28 75 38  ger->memDb = (u8
133ff 29 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65 72  )memDb;.  pPager
13400 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 28 75 38  ->readOnly = (u8
13401 29 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a 20  )readOnly;.  /* 
13402 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
13403 20 3d 20 30 3b 20 2a 2f 0a 20 20 61 73 73 65 72   = 0; */.  asser
13404 74 28 20 75 73 65 4a 6f 75 72 6e 61 6c 20 7c 7c  t( useJournal ||
13405 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
13406 65 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e  e );.  pPager->n
13407 6f 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d 3e  oSync = pPager->
13408 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61 67  tempFile;.  pPag
13409 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 70  er->fullSync = p
1340a 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 3f 30  Pager->noSync ?0
1340b 3a 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79  :1;.  pPager->sy
1340c 6e 63 5f 66 6c 61 67 73 20 3d 20 53 51 4c 49 54  nc_flags = SQLIT
1340d 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 3b 0a 20  E_SYNC_NORMAL;. 
1340e 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 72   /* pPager->pFir
1340f 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20  st = 0; */.  /* 
13410 70 50 61 67 65 72 2d 3e 70 46 69 72 73 74 53 79  pPager->pFirstSy
13411 6e 63 65 64 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f  nced = 0; */.  /
13412 2a 20 70 50 61 67 65 72 2d 3e 70 4c 61 73 74 20  * pPager->pLast 
13413 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72  = 0; */.  pPager
13414 2d 3e 6e 45 78 74 72 61 20 3d 20 28 75 31 36 29  ->nExtra = (u16)
13415 6e 45 78 74 72 61 3b 0a 20 20 70 50 61 67 65 72  nExtra;.  pPager
13416 2d 3e 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d  ->journalSizeLim
13417 69 74 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  it = SQLITE_DEFA
13418 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45  ULT_JOURNAL_SIZE
13419 5f 4c 49 4d 49 54 3b 0a 20 20 61 73 73 65 72 74  _LIMIT;.  assert
1341a 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
1341b 3e 66 64 29 20 7c 7c 20 74 65 6d 70 46 69 6c 65  >fd) || tempFile
1341c 20 29 3b 0a 20 20 73 65 74 53 65 63 74 6f 72 53   );.  setSectorS
1341d 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 69  ize(pPager);.  i
1341e 66 28 20 21 75 73 65 4a 6f 75 72 6e 61 6c 20 29  f( !useJournal )
1341f 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
13420 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41 47 45  urnalMode = PAGE
13421 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
13422 46 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6d  F;.  }else if( m
13423 65 6d 44 62 20 29 7b 0a 20 20 20 20 70 50 61 67  emDb ){.    pPag
13424 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20  er->journalMode 
13425 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  = PAGER_JOURNALM
13426 4f 44 45 5f 4d 45 4d 4f 52 59 3b 0a 20 20 7d 0a  ODE_MEMORY;.  }.
13427 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 78 42 75    /* pPager->xBu
13428 73 79 48 61 6e 64 6c 65 72 20 3d 20 30 3b 20 2a  syHandler = 0; *
13429 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70  /.  /* pPager->p
1342a 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 3d  BusyHandlerArg =
1342b 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d   0; */.  pPager-
1342c 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 78 52 65  >xReiniter = xRe
1342d 69 6e 69 74 3b 0a 20 20 2f 2a 20 6d 65 6d 73 65  init;.  /* memse
1342e 74 28 70 50 61 67 65 72 2d 3e 61 48 61 73 68 2c  t(pPager->aHash,
1342f 20 30 2c 20 73 69 7a 65 6f 66 28 70 50 61 67 65   0, sizeof(pPage
13430 72 2d 3e 61 48 61 73 68 29 29 3b 20 2a 2f 0a 20  r->aHash)); */. 
13431 20 2a 70 70 50 61 67 65 72 20 3d 20 70 50 61 67   *ppPager = pPag
13432 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  er;.  return SQL
13433 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a  ITE_OK;.}..../*.
13434 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
13435 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72   is called after
13436 20 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66   transitioning f
13437 72 6f 6d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  rom PAGER_UNLOCK
13438 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f 53 48 41   to.** PAGER_SHA
13439 52 45 44 20 73 74 61 74 65 2e 20 49 74 20 74 65  RED state. It te
1343a 73 74 73 20 69 66 20 74 68 65 72 65 20 69 73 20  sts if there is 
1343b 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 70 72  a hot journal pr
1343c 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 74 68 65 20  esent in.** the 
1343d 66 69 6c 65 2d 73 79 73 74 65 6d 20 66 6f 72 20  file-system for 
1343e 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 72 2e  the given pager.
1343f 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69   A hot journal i
13440 73 20 6f 6e 65 20 74 68 61 74 20 0a 2a 2a 20 6e  s one that .** n
13441 65 65 64 73 20 74 6f 20 62 65 20 70 6c 61 79 65  eeds to be playe
13442 64 20 62 61 63 6b 2e 20 41 63 63 6f 72 64 69 6e  d back. Accordin
13443 67 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  g to this functi
13444 6f 6e 2c 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61  on, a hot-journa
13445 6c 0a 2a 2a 20 66 69 6c 65 20 65 78 69 73 74 73  l.** file exists
13446 20 69 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   if the followin
13447 67 20 63 72 69 74 65 72 69 61 20 61 72 65 20 6d  g criteria are m
13448 65 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68  et:.**.**   * Th
13449 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65  e journal file e
1344a 78 69 73 74 73 20 69 6e 20 74 68 65 20 66 69 6c  xists in the fil
1344b 65 20 73 79 73 74 65 6d 2c 20 61 6e 64 0a 2a 2a  e system, and.**
1344c 20 20 20 2a 20 4e 6f 20 70 72 6f 63 65 73 73 20     * No process 
1344d 68 6f 6c 64 73 20 61 20 52 45 53 45 52 56 45 44  holds a RESERVED
1344e 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b   or greater lock
1344f 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
13450 20 66 69 6c 65 2c 20 61 6e 64 0a 2a 2a 20 20 20   file, and.**   
13451 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66  * The database f
13452 69 6c 65 20 69 74 73 65 6c 66 20 69 73 20 67 72  ile itself is gr
13453 65 61 74 65 72 20 74 68 61 6e 20 30 20 62 79 74  eater than 0 byt
13454 65 73 20 69 6e 20 73 69 7a 65 2c 20 61 6e 64 0a  es in size, and.
13455 2a 2a 20 20 20 2a 20 54 68 65 20 66 69 72 73 74  **   * The first
13456 20 62 79 74 65 20 6f 66 20 74 68 65 20 6a 6f 75   byte of the jou
13457 72 6e 61 6c 20 66 69 6c 65 20 65 78 69 73 74 73  rnal file exists
13458 20 61 6e 64 20 69 73 20 6e 6f 74 20 30 78 30 30   and is not 0x00
13459 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63  ..**.** If the c
1345a 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74  urrent size of t
1345b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1345c 20 69 73 20 30 20 62 75 74 20 61 20 6a 6f 75 72   is 0 but a jour
1345d 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 65 78 69 73  nal file.** exis
1345e 74 73 2c 20 74 68 61 74 20 69 73 20 70 72 6f 62  ts, that is prob
1345f 61 62 6c 79 20 61 6e 20 6f 6c 64 20 6a 6f 75 72  ably an old jour
13460 6e 61 6c 20 6c 65 66 74 20 6f 76 65 72 20 66 72  nal left over fr
13461 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 64 61  om a prior.** da
13462 74 61 62 61 73 65 20 77 69 74 68 20 74 68 65 20  tabase with the 
13463 73 61 6d 65 20 6e 61 6d 65 2e 20 49 6e 20 74 68  same name. In th
13464 69 73 20 63 61 73 65 20 74 68 65 20 6a 6f 75 72  is case the jour
13465 6e 61 6c 20 66 69 6c 65 20 69 73 0a 2a 2a 20 6a  nal file is.** j
13466 75 73 74 20 64 65 6c 65 74 65 64 20 75 73 69 6e  ust deleted usin
13467 67 20 4f 73 44 65 6c 65 74 65 2c 20 2a 70 45 78  g OsDelete, *pEx
13468 69 73 74 73 20 69 73 20 73 65 74 20 74 6f 20 30  ists is set to 0
13469 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a   and SQLITE_OK.*
1346a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  * is returned..*
1346b 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1346c 65 20 64 6f 65 73 20 6e 6f 74 20 63 68 65 63 6b  e does not check
1346d 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 6d   if there is a m
1346e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1346f 6c 65 6e 61 6d 65 0a 2a 2a 20 61 74 20 74 68 65  lename.** at the
13470 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65   end of the file
13471 2e 20 49 66 20 74 68 65 72 65 20 69 73 2c 20 61  . If there is, a
13472 6e 64 20 74 68 61 74 20 6d 61 73 74 65 72 20 6a  nd that master j
13473 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 64  ournal file.** d
13474 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 74  oes not exist, t
13475 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hen the journal 
13476 66 69 6c 65 20 69 73 20 6e 6f 74 20 72 65 61 6c  file is not real
13477 6c 79 20 68 6f 74 2e 20 49 6e 20 74 68 69 73 0a  ly hot. In this.
13478 2a 2a 20 63 61 73 65 20 74 68 69 73 20 72 6f 75  ** case this rou
13479 74 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e  tine will return
1347a 20 61 20 66 61 6c 73 65 2d 70 6f 73 69 74 69 76   a false-positiv
1347b 65 2e 20 54 68 65 20 70 61 67 65 72 5f 70 6c 61  e. The pager_pla
1347c 79 62 61 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69  yback().** routi
1347d 6e 65 20 77 69 6c 6c 20 64 69 73 63 6f 76 65 72  ne will discover
1347e 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61   that the journa
1347f 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 72 65  l file is not re
13480 61 6c 6c 79 20 68 6f 74 20 61 6e 64 20 0a 2a 2a  ally hot and .**
13481 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c 20 69   will not roll i
13482 74 20 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 49  t back. .**.** I
13483 66 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  f a hot-journal 
13484 66 69 6c 65 20 69 73 20 66 6f 75 6e 64 20 74 6f  file is found to
13485 20 65 78 69 73 74 2c 20 2a 70 45 78 69 73 74 73   exist, *pExists
13486 20 69 73 20 73 65 74 20 74 6f 20 31 20 61 6e 64   is set to 1 and
13487 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 72   .** SQLITE_OK r
13488 65 74 75 72 6e 65 64 2e 20 49 66 20 6e 6f 20 68  eturned. If no h
13489 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ot-journal file 
1348a 69 73 20 70 72 65 73 65 6e 74 2c 20 2a 70 45 78  is present, *pEx
1348b 69 73 74 73 20 69 73 0a 2a 2a 20 73 65 74 20 74  ists is.** set t
1348c 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f  o 0 and SQLITE_O
1348d 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61  K returned. If a
1348e 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
1348f 73 20 77 68 69 6c 65 20 74 72 79 69 6e 67 0a 2a  s while trying.*
13490 2a 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  * to determine w
13491 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20  hether or not a 
13492 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  hot-journal file
13493 20 65 78 69 73 74 73 2c 20 74 68 65 20 49 4f 20   exists, the IO 
13494 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73  error.** code is
13495 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
13496 65 20 76 61 6c 75 65 20 6f 66 20 2a 70 45 78 69  e value of *pExi
13497 73 74 73 20 69 73 20 75 6e 64 65 66 69 6e 65 64  sts is undefined
13498 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
13499 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 50 61  hasHotJournal(Pa
1349a 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
1349b 20 2a 70 45 78 69 73 74 73 29 7b 0a 20 20 73 71   *pExists){.  sq
1349c 6c 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73  lite3_vfs * cons
1349d 74 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d  t pVfs = pPager-
1349e 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b  >pVfs;.  int rc;
1349f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
134a0 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
134a1 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 65   code */.  int e
134a2 78 69 73 74 73 3b 20 20 20 20 20 20 20 20 20 20  xists;          
134a3 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
134a4 20 69 66 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   if a journal fi
134a5 6c 65 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  le is present */
134a6 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
134a7 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  er!=0 );.  asser
134a8 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f  t( pPager->useJo
134a9 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65 72  urnal );.  asser
134aa 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
134ab 2d 3e 66 64 29 20 29 3b 0a 20 20 61 73 73 65 72  ->fd) );.  asser
134ac 74 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65  t( !isOpen(pPage
134ad 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20 61 73 73  r->jfd) );.  ass
134ae 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
134af 74 65 20 3c 3d 20 50 41 47 45 52 5f 53 48 41 52  te <= PAGER_SHAR
134b0 45 44 20 29 3b 0a 0a 20 20 2a 70 45 78 69 73 74  ED );..  *pExist
134b1 73 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71  s = 0;.  rc = sq
134b2 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56  lite3OsAccess(pV
134b3 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  fs, pPager->zJou
134b4 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43  rnal, SQLITE_ACC
134b5 45 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69  ESS_EXISTS, &exi
134b6 73 74 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  sts);.  if( rc==
134b7 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 78 69  SQLITE_OK && exi
134b8 73 74 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6c  sts ){.    int l
134b9 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20 20  ocked;          
134ba 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
134bb 66 20 73 6f 6d 65 20 70 72 6f 63 65 73 73 20 68  f some process h
134bc 6f 6c 64 73 20 61 20 52 45 53 45 52 56 45 44 20  olds a RESERVED 
134bd 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  lock */..    /* 
134be 52 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 68  Race condition h
134bf 65 72 65 3a 20 20 41 6e 6f 74 68 65 72 20 70 72  ere:  Another pr
134c0 6f 63 65 73 73 20 6d 69 67 68 74 20 68 61 76 65  ocess might have
134c1 20 62 65 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68   been holding th
134c2 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 52 45 53  e.    ** the RES
134c3 45 52 56 45 44 20 6c 6f 63 6b 20 61 6e 64 20 68  ERVED lock and h
134c4 61 76 65 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 70  ave a journal op
134c5 65 6e 20 61 74 20 74 68 65 20 73 71 6c 69 74 65  en at the sqlite
134c6 33 4f 73 41 63 63 65 73 73 28 29 20 0a 20 20 20  3OsAccess() .   
134c7 20 2a 2a 20 63 61 6c 6c 20 61 62 6f 76 65 2c 20   ** call above, 
134c8 62 75 74 20 74 68 65 6e 20 64 65 6c 65 74 65 20  but then delete 
134c9 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  the journal and 
134ca 64 72 6f 70 20 74 68 65 20 6c 6f 63 6b 20 62 65  drop the lock be
134cb 66 6f 72 65 0a 20 20 20 20 2a 2a 20 77 65 20 67  fore.    ** we g
134cc 65 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  et to the follow
134cd 69 6e 67 20 73 71 6c 69 74 65 33 4f 73 43 68 65  ing sqlite3OsChe
134ce 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29  ckReservedLock()
134cf 20 63 61 6c 6c 2e 20 20 49 66 20 74 68 61 74 0a   call.  If that.
134d0 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61      ** is the ca
134d1 73 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  se, this routine
134d2 20 6d 69 67 68 74 20 74 68 69 6e 6b 20 74 68 65   might think the
134d3 72 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 72  re is a hot jour
134d4 6e 61 6c 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20  nal when.    ** 
134d5 69 6e 20 66 61 63 74 20 74 68 65 72 65 20 69 73  in fact there is
134d6 20 6e 6f 6e 65 2e 20 20 54 68 69 73 20 72 65 73   none.  This res
134d7 75 6c 74 73 20 69 6e 20 61 20 66 61 6c 73 65 2d  ults in a false-
134d8 70 6f 73 69 74 69 76 65 20 77 68 69 63 68 20 77  positive which w
134d9 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 20 64 65  ill.    ** be de
134da 61 6c 74 20 77 69 74 68 20 62 79 20 74 68 65 20  alt with by the 
134db 70 6c 61 79 62 61 63 6b 20 72 6f 75 74 69 6e 65  playback routine
134dc 2e 20 20 54 69 63 6b 65 74 20 23 33 38 38 33 2e  .  Ticket #3883.
134dd 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
134de 20 73 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52   sqlite3OsCheckR
134df 65 73 65 72 76 65 64 4c 6f 63 6b 28 70 50 61 67  eservedLock(pPag
134e0 65 72 2d 3e 66 64 2c 20 26 6c 6f 63 6b 65 64 29  er->fd, &locked)
134e1 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
134e2 4c 49 54 45 5f 4f 4b 20 26 26 20 21 6c 6f 63 6b  LITE_OK && !lock
134e3 65 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ed ){.      int 
134e4 6e 50 61 67 65 3b 0a 0a 20 20 20 20 20 20 2f 2a  nPage;..      /*
134e5 20 43 68 65 63 6b 20 74 68 65 20 73 69 7a 65 20   Check the size 
134e6 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
134e7 66 69 6c 65 2e 20 49 66 20 69 74 20 63 6f 6e 73  file. If it cons
134e8 69 73 74 73 20 6f 66 20 30 20 70 61 67 65 73 2c  ists of 0 pages,
134e9 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 64  .      ** then d
134ea 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  elete the journa
134eb 6c 20 66 69 6c 65 2e 20 53 65 65 20 74 68 65 20  l file. See the 
134ec 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 61  header comment a
134ed 62 6f 76 65 20 66 6f 72 20 0a 20 20 20 20 20 20  bove for .      
134ee 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 69 6e 67  ** the reasoning
134ef 20 68 65 72 65 2e 20 20 44 65 6c 65 74 65 20 74   here.  Delete t
134f0 68 65 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72  he obsolete jour
134f1 6e 61 6c 20 66 69 6c 65 20 75 6e 64 65 72 0a 20  nal file under. 
134f2 20 20 20 20 20 2a 2a 20 61 20 52 45 53 45 52 56       ** a RESERV
134f3 45 44 20 6c 6f 63 6b 20 74 6f 20 61 76 6f 69 64  ED lock to avoid
134f4 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73   race conditions
134f5 20 61 6e 64 20 74 6f 20 61 76 6f 69 64 20 76 69   and to avoid vi
134f6 6f 6c 61 74 69 6e 67 0a 20 20 20 20 20 20 2a 2a  olating.      **
134f7 20 5b 48 33 33 30 32 30 5d 2e 0a 20 20 20 20 20   [H33020]..     
134f8 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73   */.      rc = s
134f9 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
134fa 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26 6e 50  ount(pPager, &nP
134fb 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  age);.      if( 
134fc 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
134fd 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 50 61  .        if( nPa
134fe 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ge==0 ){.       
134ff 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42     sqlite3BeginB
13500 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
13501 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
13502 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65  ite3OsLock(pPage
13503 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f  r->fd, RESERVED_
13504 4c 4f 43 4b 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b  LOCK)==SQLITE_OK
13505 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
13506 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
13507 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a  pVfs, pPager->zJ
13508 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20  ournal, 0);.    
13509 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
1350a 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65 72 2d 3e  sUnlock(pPager->
1350b 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29  fd, SHARED_LOCK)
1350c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1350d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1350e 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
1350f 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
13510 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
13511 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65  e journal file e
13512 78 69 73 74 73 20 61 6e 64 20 6e 6f 20 6f 74 68  xists and no oth
13513 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  er connection ha
13514 73 20 61 20 72 65 73 65 72 76 65 64 0a 20 20 20  s a reserved.   
13515 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 67 72 65         ** or gre
13516 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ater lock on the
13517 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
13518 4e 6f 77 20 63 68 65 63 6b 20 74 68 61 74 20 74  Now check that t
13519 68 65 72 65 20 69 73 0a 20 20 20 20 20 20 20 20  here is.        
1351a 20 20 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e    ** at least on
1351b 65 20 6e 6f 6e 2d 7a 65 72 6f 20 62 79 74 65 73  e non-zero bytes
1351c 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
1351d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1351e 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  e..          ** 
1351f 49 66 20 74 68 65 72 65 20 69 73 2c 20 74 68 65  If there is, the
13520 6e 20 77 65 20 63 6f 6e 73 69 64 65 72 20 74 68  n we consider th
13521 69 73 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65  is journal to be
13522 20 68 6f 74 2e 20 49 66 20 6e 6f 74 2c 20 0a 20   hot. If not, . 
13523 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 63           ** it c
13524 61 6e 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 20  an be ignored.. 
13525 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
13526 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20 53 51        int f = SQ
13527 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
13528 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  LY|SQLITE_OPEN_M
13529 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20  AIN_JOURNAL;.   
1352a 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1352b 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
1352c 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
1352d 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66  , pPager->jfd, f
1352e 2c 20 26 66 29 3b 0a 20 20 20 20 20 20 20 20 20  , &f);.         
1352f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13530 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
13531 20 20 75 38 20 66 69 72 73 74 20 3d 20 30 3b 0a    u8 first = 0;.
13532 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
13533 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70   sqlite3OsRead(p
13534 50 61 67 65 72 2d 3e 6a 66 64 2c 20 28 76 6f 69  Pager->jfd, (voi
13535 64 20 2a 29 26 66 69 72 73 74 2c 20 31 2c 20 30  d *)&first, 1, 0
13536 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
13537 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  f( rc==SQLITE_IO
13538 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 29  ERR_SHORT_READ )
13539 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1353a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1353b 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
1353c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1353d 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
1353e 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20  >jfd);.         
1353f 20 20 20 2a 70 45 78 69 73 74 73 20 3d 20 28 66     *pExists = (f
13540 69 72 73 74 21 3d 30 29 3b 0a 20 20 20 20 20 20  irst!=0);.      
13541 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63      }else if( rc
13542 3d 3d 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  ==SQLITE_CANTOPE
13543 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  N ){.           
13544 20 2f 2a 20 49 66 20 77 65 20 63 61 6e 6e 6f 74   /* If we cannot
13545 20 6f 70 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61   open the rollba
13546 63 6b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ck journal file 
13547 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 65 65 20  in order to see 
13548 69 66 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  if.            *
13549 2a 20 69 74 73 20 68 61 73 20 61 20 7a 65 72 6f  * its has a zero
1354a 20 68 65 61 64 65 72 2c 20 74 68 61 74 20 6d 69   header, that mi
1354b 67 68 74 20 62 65 20 64 75 65 20 74 6f 20 61 6e  ght be due to an
1354c 20 49 2f 4f 20 65 72 72 6f 72 2c 20 6f 72 0a 20   I/O error, or. 
1354d 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74             ** it
1354e 20 6d 69 67 68 74 20 62 65 20 64 75 65 20 74 6f   might be due to
1354f 20 74 68 65 20 72 61 63 65 20 63 6f 6e 64 69 74   the race condit
13550 69 6f 6e 20 64 65 73 63 72 69 62 65 64 20 61 62  ion described ab
13551 6f 76 65 20 61 6e 64 20 69 6e 0a 20 20 20 20 20  ove and in.     
13552 20 20 20 20 20 20 20 2a 2a 20 74 69 63 6b 65 74         ** ticket
13553 20 23 33 38 38 33 2e 20 20 45 69 74 68 65 72 20   #3883.  Either 
13554 77 61 79 2c 20 61 73 73 75 6d 65 20 74 68 61 74  way, assume that
13555 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   the journal is 
13556 68 6f 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20  hot..           
13557 20 2a 2a 20 54 68 69 73 20 6d 69 67 68 74 20 62   ** This might b
13558 65 20 61 20 66 61 6c 73 65 20 70 6f 73 69 74 69  e a false positi
13559 76 65 2e 20 20 42 75 74 20 69 66 20 69 74 20 69  ve.  But if it i
1355a 73 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20  s, then the.    
1355b 20 20 20 20 20 20 20 20 2a 2a 20 61 75 74 6f 6d          ** autom
1355c 61 74 69 63 20 6a 6f 75 72 6e 61 6c 20 70 6c 61  atic journal pla
1355d 79 62 61 63 6b 20 61 6e 64 20 72 65 63 6f 76 65  yback and recove
1355e 72 79 20 6d 65 63 68 61 6e 69 73 6d 20 77 69 6c  ry mechanism wil
1355f 6c 20 64 65 61 6c 0a 20 20 20 20 20 20 20 20 20  l deal.         
13560 20 20 20 2a 2a 20 77 69 74 68 20 69 74 20 75 6e     ** with it un
13561 64 65 72 20 61 6e 20 45 58 43 4c 55 53 49 56 45  der an EXCLUSIVE
13562 20 6c 6f 63 6b 20 77 68 65 72 65 20 77 65 20 64   lock where we d
13563 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 0a 20 20  o not need to.  
13564 20 20 20 20 20 20 20 20 20 20 2a 2a 20 77 6f 72            ** wor
13565 72 79 20 73 6f 20 6d 75 63 68 20 77 69 74 68 20  ry so much with 
13566 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 73 2e  race conditions.
13567 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  .            */.
13568 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 45 78              *pEx
13569 69 73 74 73 20 3d 20 31 3b 0a 20 20 20 20 20 20  ists = 1;.      
1356a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1356b 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20  E_OK;.          
1356c 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
1356d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
1356e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1356f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 63 6f  *.** Read the co
13570 6e 74 65 6e 74 20 66 6f 72 20 70 61 67 65 20 70  ntent for page p
13571 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64 61  Pg out of the da
13572 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
13573 69 6e 74 6f 20 0a 2a 2a 20 70 50 67 2d 3e 70 44  into .** pPg->pD
13574 61 74 61 2e 20 41 20 73 68 61 72 65 64 20 6c 6f  ata. A shared lo
13575 63 6b 20 6f 72 20 67 72 65 61 74 65 72 20 6d 75  ck or greater mu
13576 73 74 20 62 65 20 68 65 6c 64 20 6f 6e 20 74 68  st be held on th
13577 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  e database.** fi
13578 6c 65 20 62 65 66 6f 72 65 20 74 68 69 73 20 66  le before this f
13579 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
1357a 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 67 65  d..**.** If page
1357b 20 31 20 69 73 20 72 65 61 64 2c 20 74 68 65 6e   1 is read, then
1357c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 61   the value of Pa
1357d 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 5b 5d  ger.dbFileVers[]
1357e 20 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68   is set to.** th
1357f 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72 6f  e value read fro
13580 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  m the database f
13581 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ile..**.** If an
13582 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73   IO error occurs
13583 2c 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72  , then the IO er
13584 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20  ror is returned 
13585 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a  to the caller..*
13586 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c  * Otherwise, SQL
13587 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
13588 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ed..*/.static in
13589 74 20 72 65 61 64 44 62 50 61 67 65 28 50 67 48  t readDbPage(PgH
1358a 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65  dr *pPg){.  Page
1358b 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d  r *pPager = pPg-
1358c 3e 70 50 61 67 65 72 3b 20 2f 2a 20 50 61 67 65  >pPager; /* Page
1358d 72 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61  r object associa
1358e 74 65 64 20 77 69 74 68 20 70 61 67 65 20 70 50  ted with page pP
1358f 67 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  g */.  Pgno pgno
13590 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 20 20 20   = pPg->pgno;   
13591 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
13592 65 72 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20  er to read */.  
13593 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
13594 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13595 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
13596 20 69 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20   i64 iOffset;   
13597 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13598 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20   Byte offset of 
13599 66 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f  file to read fro
1359a 6d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  m */..  assert( 
1359b 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
1359c 41 47 45 52 5f 53 48 41 52 45 44 20 26 26 20 21  AGER_SHARED && !
1359d 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73 65 72  MEMDB );.  asser
1359e 74 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  t( isOpen(pPager
1359f 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 69 66 28 20  ->fd) );..  if( 
135a0 4e 45 56 45 52 28 21 69 73 4f 70 65 6e 28 70 50  NEVER(!isOpen(pP
135a1 61 67 65 72 2d 3e 66 64 29 29 20 29 7b 0a 20 20  ager->fd)) ){.  
135a2 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
135a3 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20  ->tempFile );.  
135a4 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44    memset(pPg->pD
135a5 61 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e  ata, 0, pPager->
135a6 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 72  pageSize);.    r
135a7 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
135a8 0a 20 20 7d 0a 20 20 69 4f 66 66 73 65 74 20 3d  .  }.  iOffset =
135a9 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70   (pgno-1)*(i64)p
135aa 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b  Pager->pageSize;
135ab 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
135ac 73 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64  sRead(pPager->fd
135ad 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 50  , pPg->pData, pP
135ae 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
135af 69 4f 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20  iOffset);.  if( 
135b0 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
135b1 5f 53 48 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20  _SHORT_READ ){. 
135b2 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
135b3 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 67 6e  K;.  }.  if( pgn
135b4 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 75 38 20 2a  o==1 ){.    u8 *
135b5 64 62 46 69 6c 65 56 65 72 73 20 3d 20 26 28 28  dbFileVers = &((
135b6 75 38 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 5b  u8*)pPg->pData)[
135b7 32 34 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  24];.    memcpy(
135b8 26 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56  &pPager->dbFileV
135b9 65 72 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c  ers, dbFileVers,
135ba 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e   sizeof(pPager->
135bb 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20  dbFileVers));.  
135bc 7d 0a 20 20 43 4f 44 45 43 31 28 70 50 61 67 65  }.  CODEC1(pPage
135bd 72 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70  r, pPg->pData, p
135be 67 6e 6f 2c 20 33 2c 20 72 63 20 3d 20 53 51 4c  gno, 3, rc = SQL
135bf 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 0a 20 20 50  ITE_NOMEM);..  P
135c0 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65  AGER_INCR(sqlite
135c1 33 5f 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63  3_pager_readdb_c
135c2 6f 75 6e 74 29 3b 0a 20 20 50 41 47 45 52 5f 49  ount);.  PAGER_I
135c3 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 52 65 61  NCR(pPager->nRea
135c4 64 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22  d);.  IOTRACE(("
135c5 50 47 49 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70  PGIN %p %d\n", p
135c6 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20  Pager, pgno));. 
135c7 20 50 41 47 45 52 54 52 41 43 45 28 28 22 46 45   PAGERTRACE(("FE
135c8 54 43 48 20 25 64 20 70 61 67 65 20 25 64 20 68  TCH %d page %d h
135c9 61 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20  ash(%08x)\n",.  
135ca 20 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47               PAG
135cb 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 67  ERID(pPager), pg
135cc 6e 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61  no, pager_pageha
135cd 73 68 28 70 50 67 29 29 29 3b 0a 0a 20 20 72 65  sh(pPg)));..  re
135ce 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
135cf 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
135d0 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 6f 62 74  is called to obt
135d1 61 69 6e 20 61 20 73 68 61 72 65 64 20 6c 6f 63  ain a shared loc
135d2 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
135d3 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 74 20 69 73  e file..** It is
135d4 20 69 6c 6c 65 67 61 6c 20 74 6f 20 63 61 6c 6c   illegal to call
135d5 20 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71   sqlite3PagerAcq
135d6 75 69 72 65 28 29 20 75 6e 74 69 6c 20 61 66 74  uire() until aft
135d7 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  er this function
135d8 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 73 75 63  .** has been suc
135d9 63 65 73 73 66 75 6c 6c 79 20 63 61 6c 6c 65 64  cessfully called
135da 2e 20 49 66 20 61 20 73 68 61 72 65 64 2d 6c 6f  . If a shared-lo
135db 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20 68 65  ck is already he
135dc 6c 64 20 77 68 65 6e 0a 2a 2a 20 74 68 69 73 20  ld when.** this 
135dd 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
135de 65 64 2c 20 69 74 20 69 73 20 61 20 6e 6f 2d 6f  ed, it is a no-o
135df 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  p..**.** The fol
135e0 6c 6f 77 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  lowing operation
135e1 73 20 61 72 65 20 61 6c 73 6f 20 70 65 72 66 6f  s are also perfo
135e2 72 6d 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  rmed by this fun
135e3 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 31  ction..**.**   1
135e4 29 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  ) If the pager i
135e5 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 50  s currently in P
135e6 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61 74  AGER_UNLOCK stat
135e7 65 20 28 6e 6f 20 6c 6f 63 6b 20 68 65 6c 64 0a  e (no lock held.
135e8 2a 2a 20 20 20 20 20 20 6f 6e 20 74 68 65 20 64  **      on the d
135e9 61 74 61 62 61 73 65 20 66 69 6c 65 29 2c 20 74  atabase file), t
135ea 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69  hen an attempt i
135eb 73 20 6d 61 64 65 20 74 6f 20 6f 62 74 61 69 6e  s made to obtain
135ec 20 61 0a 2a 2a 20 20 20 20 20 20 53 48 41 52 45   a.**      SHARE
135ed 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  D lock on the da
135ee 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 6d 6d  tabase file. Imm
135ef 65 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 6f  ediately after o
135f0 62 74 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20  btaining.**     
135f1 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b   the SHARED lock
135f2 2c 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65  , the file-syste
135f3 6d 20 69 73 20 63 68 65 63 6b 65 64 20 66 6f 72  m is checked for
135f4 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2c 0a   a hot-journal,.
135f5 2a 2a 20 20 20 20 20 20 77 68 69 63 68 20 69 73  **      which is
135f6 20 70 6c 61 79 65 64 20 62 61 63 6b 20 69 66 20   played back if 
135f7 70 72 65 73 65 6e 74 2e 20 46 6f 6c 6c 6f 77 69  present. Followi
135f8 6e 67 20 61 6e 79 20 68 6f 74 2d 6a 6f 75 72 6e  ng any hot-journ
135f9 61 6c 20 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c  al .**      roll
135fa 62 61 63 6b 2c 20 74 68 65 20 63 6f 6e 74 65 6e  back, the conten
135fb 74 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 20  ts of the cache 
135fc 61 72 65 20 76 61 6c 69 64 61 74 65 64 20 62 79  are validated by
135fd 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 20 20 20   checking.**    
135fe 20 20 74 68 65 20 27 63 68 61 6e 67 65 2d 63 6f    the 'change-co
135ff 75 6e 74 65 72 27 20 66 69 65 6c 64 20 6f 66 20  unter' field of 
13600 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13601 65 20 68 65 61 64 65 72 20 61 6e 64 0a 2a 2a 20  e header and.** 
13602 20 20 20 20 20 64 69 73 63 61 72 64 65 64 20 69       discarded i
13603 66 20 74 68 65 79 20 61 72 65 20 66 6f 75 6e 64  f they are found
13604 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 2e 0a   to be invalid..
13605 2a 2a 0a 2a 2a 20 20 20 32 29 20 49 66 20 74 68  **.**   2) If th
13606 65 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69  e pager is runni
13607 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d  ng in exclusive-
13608 6d 6f 64 65 2c 20 61 6e 64 20 74 68 65 72 65 20  mode, and there 
13609 61 72 65 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a  are currently.**
1360a 20 20 20 20 20 20 6e 6f 20 6f 75 74 73 74 61 6e        no outstan
1360b 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20  ding references 
1360c 74 6f 20 61 6e 79 20 70 61 67 65 73 2c 20 61 6e  to any pages, an
1360d 64 20 69 73 20 69 6e 20 74 68 65 20 65 72 72 6f  d is in the erro
1360e 72 20 73 74 61 74 65 2c 0a 2a 2a 20 20 20 20 20  r state,.**     
1360f 20 74 68 65 6e 20 61 6e 20 61 74 74 65 6d 70 74   then an attempt
13610 20 69 73 20 6d 61 64 65 20 74 6f 20 63 6c 65 61   is made to clea
13611 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  r the error stat
13612 65 20 62 79 20 64 69 73 63 61 72 64 69 6e 67 0a  e by discarding.
13613 2a 2a 20 20 20 20 20 20 74 68 65 20 63 6f 6e 74  **      the cont
13614 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65  ents of the page
13615 20 63 61 63 68 65 20 61 6e 64 20 72 6f 6c 6c 69   cache and rolli
13616 6e 67 20 62 61 63 6b 20 61 6e 79 20 6f 70 65 6e  ng back any open
13617 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20   journal.**     
13618 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   file..**.** If 
13619 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 64 65  the operation de
1361a 73 63 72 69 62 65 64 20 62 79 20 28 32 29 20 61  scribed by (2) a
1361b 62 6f 76 65 20 69 73 20 6e 6f 74 20 61 74 74 65  bove is not atte
1361c 6d 70 74 65 64 2c 20 61 6e 64 20 69 66 20 74 68  mpted, and if th
1361d 65 0a 2a 2a 20 70 61 67 65 72 20 69 73 20 69 6e  e.** pager is in
1361e 20 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20   an error state 
1361f 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54  other than SQLIT
13620 45 5f 46 55 4c 4c 20 77 68 65 6e 20 74 68 69 73  E_FULL when this
13621 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74   is called,.** t
13622 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 65  he error state e
13623 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
13624 75 72 6e 65 64 2e 20 49 74 20 69 73 20 70 65 72  urned. It is per
13625 6d 69 74 74 65 64 20 74 6f 20 72 65 61 64 20 74  mitted to read t
13626 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77  he.** database w
13627 68 65 6e 20 69 6e 20 53 51 4c 49 54 45 5f 46 55  hen in SQLITE_FU
13628 4c 4c 20 65 72 72 6f 72 20 73 74 61 74 65 2e 0a  LL error state..
13629 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  **.** Otherwise,
1362a 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 69   if everything i
1362b 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51  s successful, SQ
1362c 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
1362d 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a 20 49 4f  ned. If an.** IO
1362e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68   error occurs wh
1362f 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 74 68 65 20  ile locking the 
13630 64 61 74 61 62 61 73 65 2c 20 63 68 65 63 6b 69  database, checki
13631 6e 67 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75  ng for a hot-jou
13632 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 6f 72 20  rnal.** file or 
13633 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a  rolling back a j
13634 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65  ournal file, the
13635 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
13636 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  s returned..*/.S
13637 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
13638 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68  t sqlite3PagerSh
13639 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20 2a  aredLock(Pager *
1363a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72  pPager){.  int r
1363b 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
1363c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1363d 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
1363e 20 20 69 6e 74 20 69 73 45 72 72 6f 72 52 65 73    int isErrorRes
1363f 65 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  et = 0;         
13640 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
13641 72 65 63 6f 76 65 72 69 6e 67 20 66 72 6f 6d 20  recovering from 
13642 65 72 72 6f 72 20 73 74 61 74 65 20 2a 2f 0a 0a  error state */..
13643 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
13644 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64  e is only called
13645 20 66 72 6f 6d 20 62 2d 74 72 65 65 20 61 6e 64   from b-tree and
13646 20 6f 6e 6c 79 20 77 68 65 6e 20 74 68 65 72 65   only when there
13647 20 61 72 65 20 6e 6f 0a 20 20 2a 2a 20 6f 75 74   are no.  ** out
13648 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20 2a  standing pages *
13649 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  /.  assert( sqli
1364a 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e  te3PcacheRefCoun
1364b 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
1364c 65 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e  e)==0 );.  if( N
1364d 45 56 45 52 28 4d 45 4d 44 42 20 26 26 20 70 50  EVER(MEMDB && pP
1364e 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29 20 29  ager->errCode) )
1364f 7b 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d  { return pPager-
13650 3e 65 72 72 43 6f 64 65 3b 20 7d 0a 0a 20 20 2f  >errCode; }..  /
13651 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62 61  * If this databa
13652 73 65 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f  se is in an erro
13653 72 2d 73 74 61 74 65 2c 20 6e 6f 77 20 69 73 20  r-state, now is 
13654 61 20 63 68 61 6e 63 65 20 74 6f 20 63 6c 65 61  a chance to clea
13655 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72  r.  ** the error
13656 2e 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f  . Discard the co
13657 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
13658 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20 72 6f  ger-cache and ro
13659 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 61 6e 79 20  llback.  ** any 
1365a 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 69 6e 20 74  hot journal in t
1365b 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 2e 0a  he file-system..
1365c 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65    */.  if( pPage
1365d 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20  r->errCode ){.  
1365e 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
1365f 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50 61  ger->jfd) || pPa
13660 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 29 7b  ger->zJournal ){
13661 0a 20 20 20 20 20 20 69 73 45 72 72 6f 72 52 65  .      isErrorRe
13662 73 65 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  set = 1;.    }. 
13663 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f     pPager->errCo
13664 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  de = SQLITE_OK;.
13665 20 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28      pager_reset(
13666 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20  pPager);.  }..  
13667 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
13668 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20  e==PAGER_UNLOCK 
13669 7c 7c 20 69 73 45 72 72 6f 72 52 65 73 65 74 20  || isErrorReset 
1366a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
1366b 66 73 20 2a 20 63 6f 6e 73 74 20 70 56 66 73 20  fs * const pVfs 
1366c 3d 20 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a  = pPager->pVfs;.
1366d 20 20 20 20 69 6e 74 20 69 73 48 6f 74 4a 6f 75      int isHotJou
1366e 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 61 73  rnal = 0;.    as
1366f 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a  sert( !MEMDB );.
13670 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
13671 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e  te3PcacheRefCoun
13672 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
13673 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  e)==0 );.    if(
13674 20 70 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c   pPager->noReadl
13675 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ock ){.      ass
13676 65 72 74 28 20 70 50 61 67 65 72 2d 3e 72 65 61  ert( pPager->rea
13677 64 4f 6e 6c 79 20 29 3b 0a 20 20 20 20 20 20 70  dOnly );.      p
13678 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50  Pager->state = P
13679 41 47 45 52 5f 53 48 41 52 45 44 3b 0a 20 20 20  AGER_SHARED;.   
1367a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
1367b 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e   = pager_wait_on
1367c 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 53 48  _lock(pPager, SH
1367d 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  ARED_LOCK);.    
1367e 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1367f 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61  _OK ){.        a
13680 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
13681 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
13682 43 4b 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65  CK );.        re
13683 74 75 72 6e 20 70 61 67 65 72 5f 65 72 72 6f 72  turn pager_error
13684 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20  (pPager, rc);.  
13685 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
13686 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
13687 73 74 61 74 65 3e 3d 53 48 41 52 45 44 5f 4c 4f  state>=SHARED_LO
13688 43 4b 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  CK );..    /* If
13689 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20   a journal file 
1368a 65 78 69 73 74 73 2c 20 61 6e 64 20 74 68 65 72  exists, and ther
1368b 65 20 69 73 20 6e 6f 20 52 45 53 45 52 56 45 44  e is no RESERVED
1368c 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 20   lock on the.   
1368d 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
1368e 65 2c 20 74 68 65 6e 20 69 74 20 65 69 74 68 65  e, then it eithe
1368f 72 20 6e 65 65 64 73 20 74 6f 20 62 65 20 70 6c  r needs to be pl
13690 61 79 65 64 20 62 61 63 6b 20 6f 72 20 64 65 6c  ayed back or del
13691 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  eted..    */.   
13692 20 69 66 28 20 21 69 73 45 72 72 6f 72 52 65 73   if( !isErrorRes
13693 65 74 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  et ){.      asse
13694 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
13695 65 20 3c 3d 20 50 41 47 45 52 5f 53 48 41 52 45  e <= PAGER_SHARE
13696 44 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  D );.      rc = 
13697 68 61 73 48 6f 74 4a 6f 75 72 6e 61 6c 28 70 50  hasHotJournal(pP
13698 61 67 65 72 2c 20 26 69 73 48 6f 74 4a 6f 75 72  ager, &isHotJour
13699 6e 61 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20  nal);.      if( 
1369a 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1369b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61  .        goto fa
1369c 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20  iled;.      }.  
1369d 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73 45 72    }.    if( isEr
1369e 72 6f 72 52 65 73 65 74 20 7c 7c 20 69 73 48 6f  rorReset || isHo
1369f 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  tJournal ){.    
136a0 20 20 2f 2a 20 47 65 74 20 61 6e 20 45 58 43 4c    /* Get an EXCL
136a1 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68  USIVE lock on th
136a2 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
136a3 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74 20 69   At this point i
136a4 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 69 6d  t is.      ** im
136a5 70 6f 72 74 61 6e 74 20 74 68 61 74 20 61 20 52  portant that a R
136a6 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20  ESERVED lock is 
136a7 6e 6f 74 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20  not obtained on 
136a8 74 68 65 20 77 61 79 20 74 6f 20 74 68 65 0a 20  the way to the. 
136a9 20 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56       ** EXCLUSIV
136aa 45 20 6c 6f 63 6b 2e 20 49 66 20 69 74 20 77 65  E lock. If it we
136ab 72 65 2c 20 61 6e 6f 74 68 65 72 20 70 72 6f 63  re, another proc
136ac 65 73 73 20 6d 69 67 68 74 20 6f 70 65 6e 20 74  ess might open t
136ad 68 65 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61  he.      ** data
136ae 62 61 73 65 20 66 69 6c 65 2c 20 64 65 74 65 63  base file, detec
136af 74 20 74 68 65 20 52 45 53 45 52 56 45 44 20 6c  t the RESERVED l
136b0 6f 63 6b 2c 20 61 6e 64 20 63 6f 6e 63 6c 75 64  ock, and conclud
136b1 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 20  e that the.     
136b2 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69 73 20   ** database is 
136b3 73 61 66 65 20 74 6f 20 72 65 61 64 20 77 68 69  safe to read whi
136b4 6c 65 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  le this process 
136b5 69 73 20 73 74 69 6c 6c 20 72 6f 6c 6c 69 6e 67  is still rolling
136b6 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 68   the .      ** h
136b7 6f 74 2d 6a 6f 75 72 6e 61 6c 20 62 61 63 6b 2e  ot-journal back.
136b8 0a 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20  .      ** .     
136b9 20 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 20   ** Because the 
136ba 69 6e 74 65 72 6d 65 64 69 61 74 65 20 52 45 53  intermediate RES
136bb 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 6e 6f  ERVED lock is no
136bc 74 20 72 65 71 75 65 73 74 65 64 2c 20 61 6e 79  t requested, any
136bd 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20  .      ** other 
136be 70 72 6f 63 65 73 73 20 61 74 74 65 6d 70 74 69  process attempti
136bf 6e 67 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  ng to access the
136c0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
136c1 69 6c 6c 20 67 65 74 20 74 6f 20 0a 20 20 20 20  ill get to .    
136c2 20 20 2a 2a 20 74 68 69 73 20 70 6f 69 6e 74 20    ** this point 
136c3 69 6e 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20  in the code and 
136c4 66 61 69 6c 20 74 6f 20 6f 62 74 61 69 6e 20 69  fail to obtain i
136c5 74 73 20 6f 77 6e 20 45 58 43 4c 55 53 49 56 45  ts own EXCLUSIVE
136c6 20 6c 6f 63 6b 20 0a 20 20 20 20 20 20 2a 2a 20   lock .      ** 
136c7 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
136c8 66 69 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  file..      */. 
136c9 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d       if( pPager-
136ca 3e 73 74 61 74 65 3c 45 58 43 4c 55 53 49 56 45  >state<EXCLUSIVE
136cb 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 20  _LOCK ){.       
136cc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c   rc = sqlite3OsL
136cd 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ock(pPager->fd, 
136ce 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b  EXCLUSIVE_LOCK);
136cf 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
136d0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
136d1 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67          rc = pag
136d2 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
136d3 20 72 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20   rc);.          
136d4 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20  goto failed;.   
136d5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
136d6 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50  Pager->state = P
136d7 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3b 0a  AGER_EXCLUSIVE;.
136d8 20 20 20 20 20 20 7d 0a 20 0a 20 20 20 20 20 20        }. .      
136d9 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6a 6f 75 72  /* Open the jour
136da 6e 61 6c 20 66 6f 72 20 72 65 61 64 2f 77 72 69  nal for read/wri
136db 74 65 20 61 63 63 65 73 73 2e 20 54 68 69 73 20  te access. This 
136dc 69 73 20 62 65 63 61 75 73 65 20 69 6e 20 0a 20  is because in . 
136dd 20 20 20 20 20 2a 2a 20 65 78 63 6c 75 73 69 76       ** exclusiv
136de 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 74 68  e-access mode th
136df 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
136e0 72 20 77 69 6c 6c 20 62 65 20 6b 65 70 74 20 6f  r will be kept o
136e1 70 65 6e 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a  pen and.      **
136e2 20 70 6f 73 73 69 62 6c 79 20 75 73 65 64 20 66   possibly used f
136e3 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  or a transaction
136e4 20 6c 61 74 65 72 20 6f 6e 2e 20 4f 6e 20 73 6f   later on. On so
136e5 6d 65 20 73 79 73 74 65 6d 73 2c 20 74 68 65 0a  me systems, the.
136e6 20 20 20 20 20 20 2a 2a 20 4f 73 54 72 75 6e 63        ** OsTrunc
136e7 61 74 65 28 29 20 63 61 6c 6c 20 75 73 65 64 20  ate() call used 
136e8 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63  in exclusive-acc
136e9 65 73 73 20 6d 6f 64 65 20 61 6c 73 6f 20 72 65  ess mode also re
136ea 71 75 69 72 65 73 0a 20 20 20 20 20 20 2a 2a 20  quires.      ** 
136eb 61 20 72 65 61 64 2f 77 72 69 74 65 20 66 69 6c  a read/write fil
136ec 65 20 68 61 6e 64 6c 65 2e 0a 20 20 20 20 20 20  e handle..      
136ed 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 69 73  */.      if( !is
136ee 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
136ef 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ) ){.        int
136f0 20 72 65 73 3b 0a 20 20 20 20 20 20 20 20 72 63   res;.        rc
136f1 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65   = sqlite3OsAcce
136f2 73 73 28 70 56 66 73 2c 70 50 61 67 65 72 2d 3e  ss(pVfs,pPager->
136f3 7a 4a 6f 75 72 6e 61 6c 2c 53 51 4c 49 54 45 5f  zJournal,SQLITE_
136f4 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 26 72  ACCESS_EXISTS,&r
136f5 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  es);.        if(
136f6 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
136f7 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
136f8 72 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20  res ){.         
136f9 20 20 20 69 6e 74 20 66 6f 75 74 20 3d 20 30 3b     int fout = 0;
136fa 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
136fb 20 66 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e   f = SQLITE_OPEN
136fc 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54  _READWRITE|SQLIT
136fd 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
136fe 4e 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 20  NAL;.           
136ff 20 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72   assert( !pPager
13700 2d 3e 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20  ->tempFile );.  
13701 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
13702 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
13703 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  s, pPager->zJour
13704 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66 64  nal, pPager->jfd
13705 2c 20 66 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20  , f, &fout);.   
13706 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
13707 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
13708 7c 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  | isOpen(pPager-
13709 3e 6a 66 64 29 20 29 3b 0a 20 20 20 20 20 20 20  >jfd) );.       
1370a 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1370b 49 54 45 5f 4f 4b 20 26 26 20 66 6f 75 74 26 53  ITE_OK && fout&S
1370c 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f  QLITE_OPEN_READO
1370d 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20  NLY ){.         
1370e 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1370f 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 20  _CANTOPEN;.     
13710 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
13711 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e  OsClose(pPager->
13712 6a 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  jfd);.          
13713 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 65    }.          }e
13714 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
13715 20 2f 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e   /* If the journ
13716 61 6c 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  al does not exis
13717 74 2c 20 69 74 20 75 73 75 61 6c 6c 79 20 6d 65  t, it usually me
13718 61 6e 73 20 74 68 61 74 20 73 6f 6d 65 20 0a 20  ans that some . 
13719 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 74             ** ot
1371a 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d  her connection m
1371b 61 6e 61 67 65 64 20 74 6f 20 67 65 74 20 69 6e  anaged to get in
1371c 20 61 6e 64 20 72 6f 6c 6c 20 69 74 20 62 61 63   and roll it bac
1371d 6b 20 62 65 66 6f 72 65 20 0a 20 20 20 20 20 20  k before .      
1371e 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 63 6f        ** this co
1371f 6e 6e 65 63 74 69 6f 6e 20 6f 62 74 61 69 6e 65  nnection obtaine
13720 64 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20  d the exclusive 
13721 6c 6f 63 6b 20 61 62 6f 76 65 2e 20 4f 72 2c 20  lock above. Or, 
13722 69 74 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  it .            
13723 2a 2a 20 6d 61 79 20 6d 65 61 6e 20 74 68 61 74  ** may mean that
13724 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 69   the pager was i
13725 6e 20 74 68 65 20 65 72 72 6f 72 2d 73 74 61 74  n the error-stat
13726 65 20 77 68 65 6e 20 74 68 69 73 0a 20 20 20 20  e when this.    
13727 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74          ** funct
13728 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64 20 61  ion was called a
13729 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  nd the journal f
1372a 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ile does not exi
1372b 73 74 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  st.  */.        
1372c 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65      rc = pager_e
1372d 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70  nd_transaction(p
1372e 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20  Pager, 0);.     
1372f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
13730 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
13731 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13732 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
13733 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d   failed;.      }
13734 0a 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 3a  ..      /* TODO:
13735 20 57 68 79 20 61 72 65 20 74 68 65 73 65 20 63   Why are these c
13736 6c 65 61 72 65 64 20 68 65 72 65 3f 20 49 73 20  leared here? Is 
13737 69 74 20 6e 65 63 65 73 73 61 72 79 3f 20 2a 2f  it necessary? */
13738 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a  .      pPager->j
13739 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20  ournalStarted = 
1373a 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  0;.      pPager-
1373b 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b  >journalOff = 0;
1373c 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73  .      pPager->s
1373d 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20  etMaster = 0;.  
1373e 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
1373f 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 0a 20 20  nalHdr = 0;. .  
13740 20 20 20 20 2f 2a 20 50 6c 61 79 62 61 63 6b 20      /* Playback 
13741 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65 20 6a  and delete the j
13742 6f 75 72 6e 61 6c 2e 20 20 44 72 6f 70 20 74 68  ournal.  Drop th
13743 65 20 64 61 74 61 62 61 73 65 20 77 72 69 74 65  e database write
13744 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 63 6b 20 61  .      ** lock a
13745 6e 64 20 72 65 61 63 71 75 69 72 65 20 74 68 65  nd reacquire the
13746 20 72 65 61 64 20 6c 6f 63 6b 2e 20 50 75 72 67   read lock. Purg
13747 65 20 74 68 65 20 63 61 63 68 65 20 62 65 66 6f  e the cache befo
13748 72 65 0a 20 20 20 20 20 20 2a 2a 20 70 6c 61 79  re.      ** play
13749 69 6e 67 20 62 61 63 6b 20 74 68 65 20 68 6f 74  ing back the hot
1374a 2d 6a 6f 75 72 6e 61 6c 20 73 6f 20 74 68 61 74  -journal so that
1374b 20 77 65 20 64 6f 6e 27 74 20 65 6e 64 20 75 70   we don't end up
1374c 20 77 69 74 68 0a 20 20 20 20 20 20 2a 2a 20 61   with.      ** a
1374d 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 63  n inconsistent c
1374e 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ache..      */. 
1374f 20 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28       if( isOpen(
13750 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a  pPager->jfd) ){.
13751 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67          rc = pag
13752 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67  er_playback(pPag
13753 65 72 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  er, 1);.        
13754 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
13755 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
13756 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28  c = pager_error(
13757 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20  pPager, rc);.   
13758 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c         goto fail
13759 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ed;.        }.  
1375a 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
1375b 72 74 28 20 28 70 50 61 67 65 72 2d 3e 73 74 61  rt( (pPager->sta
1375c 74 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44  te==PAGER_SHARED
1375d 29 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  ).           || 
1375e 28 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69  (pPager->exclusi
1375f 76 65 4d 6f 64 65 20 26 26 20 70 50 61 67 65 72  veMode && pPager
13760 2d 3e 73 74 61 74 65 3e 50 41 47 45 52 5f 53 48  ->state>PAGER_SH
13761 41 52 45 44 29 0a 20 20 20 20 20 20 29 3b 0a 20  ARED).      );. 
13762 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 50     }..    if( pP
13763 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20 7c 7c  ager->pBackup ||
13764 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61   sqlite3PcachePa
13765 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e  gecount(pPager->
13766 70 50 43 61 63 68 65 29 3e 30 20 29 7b 0a 20 20  pPCache)>0 ){.  
13767 20 20 20 20 2f 2a 20 54 68 65 20 73 68 61 72 65      /* The share
13768 64 2d 6c 6f 63 6b 20 68 61 73 20 6a 75 73 74 20  d-lock has just 
13769 62 65 65 6e 20 61 63 71 75 69 72 65 64 20 6f 6e  been acquired on
1376a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1376b 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  le.      ** and 
1376c 74 68 65 72 65 20 61 72 65 20 61 6c 72 65 61 64  there are alread
1376d 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63  y pages in the c
1376e 61 63 68 65 20 28 66 72 6f 6d 20 61 20 70 72 65  ache (from a pre
1376f 76 69 6f 75 73 0a 20 20 20 20 20 20 2a 2a 20 72  vious.      ** r
13770 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61  ead or write tra
13771 6e 73 61 63 74 69 6f 6e 29 2e 20 20 43 68 65 63  nsaction).  Chec
13772 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
13773 64 61 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a  database.      *
13774 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66  * has been modif
13775 69 65 64 2e 20 20 49 66 20 74 68 65 20 64 61 74  ied.  If the dat
13776 61 62 61 73 65 20 68 61 73 20 63 68 61 6e 67 65  abase has change
13777 64 2c 20 66 6c 75 73 68 20 74 68 65 0a 20 20 20  d, flush the.   
13778 20 20 20 2a 2a 20 63 61 63 68 65 2e 0a 20 20 20     ** cache..   
13779 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 44     **.      ** D
1377a 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 20  atabase changes 
1377b 69 73 20 64 65 74 65 63 74 65 64 20 62 79 20 6c  is detected by l
1377c 6f 6f 6b 69 6e 67 20 61 74 20 31 35 20 62 79 74  ooking at 15 byt
1377d 65 73 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20  es beginning.   
1377e 20 20 20 2a 2a 20 61 74 20 6f 66 66 73 65 74 20     ** at offset 
1377f 32 34 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65  24 into the file
13780 2e 20 20 54 68 65 20 66 69 72 73 74 20 34 20 6f  .  The first 4 o
13781 66 20 74 68 65 73 65 20 31 36 20 62 79 74 65 73  f these 16 bytes
13782 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 20   are.      ** a 
13783 33 32 2d 62 69 74 20 63 6f 75 6e 74 65 72 20 74  32-bit counter t
13784 68 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  hat is increment
13785 65 64 20 77 69 74 68 20 65 61 63 68 20 63 68 61  ed with each cha
13786 6e 67 65 2e 20 20 54 68 65 0a 20 20 20 20 20 20  nge.  The.      
13787 2a 2a 20 6f 74 68 65 72 20 62 79 74 65 73 20 63  ** other bytes c
13788 68 61 6e 67 65 20 72 61 6e 64 6f 6d 6c 79 20 77  hange randomly w
13789 69 74 68 20 65 61 63 68 20 66 69 6c 65 20 63 68  ith each file ch
1378a 61 6e 67 65 20 77 68 65 6e 0a 20 20 20 20 20 20  ange when.      
1378b 2a 2a 20 61 20 63 6f 64 65 63 20 69 73 20 69 6e  ** a codec is in
1378c 20 75 73 65 2e 0a 20 20 20 20 20 20 2a 2a 20 0a   use..      ** .
1378d 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69        ** There i
1378e 73 20 61 20 76 61 6e 69 73 68 69 6e 67 6c 79 20  s a vanishingly 
1378f 73 6d 61 6c 6c 20 63 68 61 6e 63 65 20 74 68 61  small chance tha
13790 74 20 61 20 63 68 61 6e 67 65 20 77 69 6c 6c 20  t a change will 
13791 6e 6f 74 20 62 65 20 0a 20 20 20 20 20 20 2a 2a  not be .      **
13792 20 64 65 74 65 63 74 65 64 2e 20 20 54 68 65 20   detected.  The 
13793 63 68 61 6e 63 65 20 6f 66 20 61 6e 20 75 6e 64  chance of an und
13794 65 74 65 63 74 65 64 20 63 68 61 6e 67 65 20 69  etected change i
13795 73 20 73 6f 20 73 6d 61 6c 6c 20 74 68 61 74 0a  s so small that.
13796 20 20 20 20 20 20 2a 2a 20 69 74 20 63 61 6e 20        ** it can 
13797 62 65 20 6e 65 67 6c 65 63 74 65 64 2e 0a 20 20  be neglected..  
13798 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 61      */.      cha
13799 72 20 64 62 46 69 6c 65 56 65 72 73 5b 73 69 7a  r dbFileVers[siz
1379a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69  eof(pPager->dbFi
1379b 6c 65 56 65 72 73 29 5d 3b 0a 20 20 20 20 20 20  leVers)];.      
1379c 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
1379d 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29  count(pPager, 0)
1379e 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  ;..      if( pPa
1379f 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a  ger->errCode ){.
137a0 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 50 61          rc = pPa
137a1 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20  ger->errCode;.  
137a2 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65        goto faile
137a3 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  d;.      }..    
137a4 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
137a5 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29 3b  ->dbSizeValid );
137a6 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65  .      if( pPage
137a7 72 2d 3e 64 62 53 69 7a 65 3e 30 20 29 7b 0a 20  r->dbSize>0 ){. 
137a8 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28         IOTRACE((
137a9 22 43 4b 56 45 52 53 20 25 70 20 25 64 5c 6e 22  "CKVERS %p %d\n"
137aa 2c 20 70 50 61 67 65 72 2c 20 73 69 7a 65 6f 66  , pPager, sizeof
137ab 28 64 62 46 69 6c 65 56 65 72 73 29 29 29 3b 0a  (dbFileVers)));.
137ac 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
137ad 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65  ite3OsRead(pPage
137ae 72 2d 3e 66 64 2c 20 26 64 62 46 69 6c 65 56 65  r->fd, &dbFileVe
137af 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c  rs, sizeof(dbFil
137b0 65 56 65 72 73 29 2c 20 32 34 29 3b 0a 20 20 20  eVers), 24);.   
137b1 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
137b2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
137b3 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b      goto failed;
137b4 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
137b5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
137b6 6d 65 6d 73 65 74 28 64 62 46 69 6c 65 56 65 72  memset(dbFileVer
137b7 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28 64 62 46  s, 0, sizeof(dbF
137b8 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 20  ileVers));.     
137b9 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 6d 65   }..      if( me
137ba 6d 63 6d 70 28 70 50 61 67 65 72 2d 3e 64 62 46  mcmp(pPager->dbF
137bb 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65 56  ileVers, dbFileV
137bc 65 72 73 2c 20 73 69 7a 65 6f 66 28 64 62 46 69  ers, sizeof(dbFi
137bd 6c 65 56 65 72 73 29 29 21 3d 30 20 29 7b 0a 20  leVers))!=0 ){. 
137be 20 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73         pager_res
137bf 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  et(pPager);.    
137c0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
137c1 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 65 78  sert( pPager->ex
137c2 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70  clusiveMode || p
137c3 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41  Pager->state==PA
137c4 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20  GER_SHARED );.  
137c5 7d 0a 0a 20 66 61 69 6c 65 64 3a 0a 20 20 69 66  }.. failed:.  if
137c6 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
137c7 29 7b 0a 20 20 20 20 2f 2a 20 70 61 67 65 72 5f  ){.    /* pager_
137c8 75 6e 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f  unlock() is a no
137c9 2d 6f 70 20 66 6f 72 20 65 78 63 6c 75 73 69 76  -op for exclusiv
137ca 65 20 6d 6f 64 65 20 61 6e 64 20 69 6e 2d 6d 65  e mode and in-me
137cb 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73 2e 20  mory databases. 
137cc 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f 75 6e 6c  */.    pager_unl
137cd 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  ock(pPager);.  }
137ce 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
137cf 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  ./*.** If the re
137d0 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 68 61  ference count ha
137d1 73 20 72 65 61 63 68 65 64 20 7a 65 72 6f 2c 20  s reached zero, 
137d2 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74  rollback any act
137d3 69 76 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69  ive.** transacti
137d4 6f 6e 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68  on and unlock th
137d5 65 20 70 61 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 45  e pager..**.** E
137d6 78 63 65 70 74 2c 20 69 6e 20 6c 6f 63 6b 69 6e  xcept, in lockin
137d7 67 5f 6d 6f 64 65 3d 45 58 43 4c 55 53 49 56 45  g_mode=EXCLUSIVE
137d8 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 6e   when there is n
137d9 6f 74 68 69 6e 67 20 74 6f 20 69 6e 0a 2a 2a 20  othing to in.** 
137da 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
137db 72 6e 61 6c 2c 20 74 68 65 20 75 6e 6c 6f 63 6b  rnal, the unlock
137dc 20 69 73 20 6e 6f 74 20 70 65 72 66 6f 72 6d 65   is not performe
137dd 64 20 61 6e 64 20 74 68 65 72 65 20 69 73 0a 2a  d and there is.*
137de 2a 20 6e 6f 74 68 69 6e 67 20 74 6f 20 72 6f 6c  * nothing to rol
137df 6c 62 61 63 6b 2c 20 73 6f 20 74 68 69 73 20 72  lback, so this r
137e0 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
137e1 70 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f  p..*/ .static vo
137e2 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66  id pagerUnlockIf
137e3 55 6e 75 73 65 64 28 50 61 67 65 72 20 2a 70 50  Unused(Pager *pP
137e4 61 67 65 72 29 7b 0a 20 20 69 66 28 20 28 73 71  ager){.  if( (sq
137e5 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
137e6 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  unt(pPager->pPCa
137e7 63 68 65 29 3d 3d 30 29 0a 20 20 20 26 26 20 28  che)==0).   && (
137e8 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69  !pPager->exclusi
137e9 76 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72  veMode || pPager
137ea 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20  ->journalOff>0) 
137eb 0a 20 20 29 7b 0a 20 20 20 20 70 61 67 65 72 55  .  ){.    pagerU
137ec 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b  nlockAndRollback
137ed 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a  (pPager);.  }.}.
137ee 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61  ./*.** Acquire a
137ef 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61   reference to pa
137f0 67 65 20 6e 75 6d 62 65 72 20 70 67 6e 6f 20 69  ge number pgno i
137f1 6e 20 70 61 67 65 72 20 70 50 61 67 65 72 20 28  n pager pPager (
137f2 61 20 70 61 67 65 0a 2a 2a 20 72 65 66 65 72 65  a page.** refere
137f3 6e 63 65 20 68 61 73 20 74 79 70 65 20 44 62 50  nce has type DbP
137f4 61 67 65 2a 29 2e 20 49 66 20 74 68 65 20 72 65  age*). If the re
137f5 71 75 65 73 74 65 64 20 72 65 66 65 72 65 6e 63  quested referenc
137f6 65 20 69 73 20 0a 2a 2a 20 73 75 63 63 65 73 73  e is .** success
137f7 66 75 6c 6c 79 20 6f 62 74 61 69 6e 65 64 2c 20  fully obtained, 
137f8 69 74 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20  it is copied to 
137f9 2a 70 70 50 61 67 65 20 61 6e 64 20 53 51 4c 49  *ppPage and SQLI
137fa 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 0a  TE_OK returned..
137fb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 71  **.** If the req
137fc 75 65 73 74 65 64 20 70 61 67 65 20 69 73 20 61  uested page is a
137fd 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61  lready in the ca
137fe 63 68 65 2c 20 69 74 20 69 73 20 72 65 74 75 72  che, it is retur
137ff 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69  ned. .** Otherwi
13800 73 65 2c 20 61 20 6e 65 77 20 70 61 67 65 20 6f  se, a new page o
13801 62 6a 65 63 74 20 69 73 20 61 6c 6c 6f 63 61 74  bject is allocat
13802 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64  ed and populated
13803 20 77 69 74 68 20 64 61 74 61 0a 2a 2a 20 72 65   with data.** re
13804 61 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ad from the data
13805 62 61 73 65 20 66 69 6c 65 2e 20 49 6e 20 73 6f  base file. In so
13806 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 70 63  me cases, the pc
13807 61 63 68 65 20 6d 6f 64 75 6c 65 20 6d 61 79 0a  ache module may.
13808 2a 2a 20 63 68 6f 6f 73 65 20 6e 6f 74 20 74 6f  ** choose not to
13809 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
1380a 70 61 67 65 20 6f 62 6a 65 63 74 20 61 6e 64 20  page object and 
1380b 6d 61 79 20 72 65 75 73 65 20 61 6e 20 65 78 69  may reuse an exi
1380c 73 74 69 6e 67 0a 2a 2a 20 6f 62 6a 65 63 74 20  sting.** object 
1380d 77 69 74 68 20 6e 6f 20 6f 75 74 73 74 61 6e 64  with no outstand
1380e 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 2e 0a  ing references..
1380f 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 74 72 61 20  **.** The extra 
13810 64 61 74 61 20 61 70 70 65 6e 64 65 64 20 74 6f  data appended to
13811 20 61 20 70 61 67 65 20 69 73 20 61 6c 77 61 79   a page is alway
13812 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f  s initialized to
13813 20 7a 65 72 6f 73 20 74 68 65 20 0a 2a 2a 20 66   zeros the .** f
13814 69 72 73 74 20 74 69 6d 65 20 61 20 70 61 67 65  irst time a page
13815 20 69 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20   is loaded into 
13816 6d 65 6d 6f 72 79 2e 20 49 66 20 74 68 65 20 70  memory. If the p
13817 61 67 65 20 72 65 71 75 65 73 74 65 64 20 69 73  age requested is
13818 20 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e 20   .** already in 
13819 74 68 65 20 63 61 63 68 65 20 77 68 65 6e 20 74  the cache when t
1381a 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
1381b 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 74 68 65  called, then the
1381c 20 65 78 74 72 61 0a 2a 2a 20 64 61 74 61 20 69   extra.** data i
1381d 73 20 6c 65 66 74 20 61 73 20 69 74 20 77 61 73  s left as it was
1381e 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 20 6f   when the page o
1381f 62 6a 65 63 74 20 77 61 73 20 6c 61 73 74 20 75  bject was last u
13820 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  sed..**.** If th
13821 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67 65  e database image
13822 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e   is smaller than
13823 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70   the requested p
13824 61 67 65 20 6f 72 20 69 66 20 61 20 0a 2a 2a 20  age or if a .** 
13825 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 69  non-zero value i
13826 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
13827 6e 6f 43 6f 6e 74 65 6e 74 20 70 61 72 61 6d 65  noContent parame
13828 74 65 72 20 61 6e 64 20 74 68 65 20 0a 2a 2a 20  ter and the .** 
13829 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69  requested page i
1382a 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 73 74  s not already st
1382b 6f 72 65 64 20 69 6e 20 74 68 65 20 63 61 63 68  ored in the cach
1382c 65 2c 20 74 68 65 6e 20 6e 6f 20 0a 2a 2a 20 61  e, then no .** a
1382d 63 74 75 61 6c 20 64 69 73 6b 20 72 65 61 64 20  ctual disk read 
1382e 6f 63 63 75 72 73 2e 20 49 6e 20 74 68 69 73 20  occurs. In this 
1382f 63 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  case the memory 
13830 69 6d 61 67 65 20 6f 66 20 74 68 65 20 0a 2a 2a  image of the .**
13831 20 70 61 67 65 20 69 73 20 69 6e 69 74 69 61 6c   page is initial
13832 69 7a 65 64 20 74 6f 20 61 6c 6c 20 7a 65 72 6f  ized to all zero
13833 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 43  s. .**.** If noC
13834 6f 6e 74 65 6e 74 20 69 73 20 74 72 75 65 2c 20  ontent is true, 
13835 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  it means that we
13836 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f   do not care abo
13837 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a  ut the contents.
13838 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2e 20  ** of the page. 
13839 54 68 69 73 20 6f 63 63 75 72 73 20 69 6e 20 74  This occurs in t
1383a 77 6f 20 73 65 70 65 72 61 74 65 20 73 63 65 6e  wo seperate scen
1383b 61 72 69 6f 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61  arios:.**.**   a
1383c 29 20 57 68 65 6e 20 72 65 61 64 69 6e 67 20 61  ) When reading a
1383d 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20   free-list leaf 
1383e 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64 61  page from the da
1383f 74 61 62 61 73 65 2c 20 61 6e 64 0a 2a 2a 0a 2a  tabase, and.**.*
13840 2a 20 20 20 62 29 20 57 68 65 6e 20 61 20 73 61  *   b) When a sa
13841 76 65 70 6f 69 6e 74 20 69 73 20 62 65 69 6e 67  vepoint is being
13842 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61 6e 64   rolled back and
13843 20 77 65 20 6e 65 65 64 20 74 6f 20 6c 6f 61 64   we need to load
13844 0a 2a 2a 20 20 20 20 20 20 61 20 6e 65 77 20 70  .**      a new p
13845 61 67 65 20 69 6e 74 6f 20 74 68 65 20 63 61 63  age into the cac
13846 68 65 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 77  he to populate w
13847 69 74 68 20 74 68 65 20 64 61 74 61 20 72 65 61  ith the data rea
13848 64 0a 2a 2a 20 20 20 20 20 20 66 72 6f 6d 20 74  d.**      from t
13849 68 65 20 73 61 76 65 70 6f 69 6e 74 20 6a 6f 75  he savepoint jou
1384a 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  rnal..**.** If n
1384b 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74 72 75 65  oContent is true
1384c 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20  , then the data 
1384d 72 65 74 75 72 6e 65 64 20 69 73 20 7a 65 72 6f  returned is zero
1384e 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a  ed instead of.**
1384f 20 62 65 69 6e 67 20 72 65 61 64 20 66 72 6f 6d   being read from
13850 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 41   the database. A
13851 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 74 68 65  dditionally, the
13852 20 62 69 74 73 20 63 6f 72 72 65 73 70 6f 6e 64   bits correspond
13853 69 6e 67 0a 2a 2a 20 74 6f 20 70 67 6e 6f 20 69  ing.** to pgno i
13854 6e 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e  n Pager.pInJourn
13855 61 6c 20 28 62 69 74 76 65 63 20 6f 66 20 70 61  al (bitvec of pa
13856 67 65 73 20 61 6c 72 65 61 64 79 20 77 72 69 74  ges already writ
13857 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a 20 6a 6f  ten to the.** jo
13858 75 72 6e 61 6c 20 66 69 6c 65 29 20 61 6e 64 20  urnal file) and 
13859 74 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69  the PagerSavepoi
1385a 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20  nt.pInSavepoint 
1385b 62 69 74 76 65 63 73 20 6f 66 20 61 6e 79 20 6f  bitvecs of any o
1385c 70 65 6e 0a 2a 2a 20 73 61 76 65 70 6f 69 6e 74  pen.** savepoint
1385d 73 20 61 72 65 20 73 65 74 2e 20 54 68 69 73 20  s are set. This 
1385e 6d 65 61 6e 73 20 69 66 20 74 68 65 20 70 61 67  means if the pag
1385f 65 20 69 73 20 6d 61 64 65 20 77 72 69 74 61 62  e is made writab
13860 6c 65 20 61 74 20 61 6e 79 0a 2a 2a 20 70 6f 69  le at any.** poi
13861 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65  nt in the future
13862 2c 20 75 73 69 6e 67 20 61 20 63 61 6c 6c 20 74  , using a call t
13863 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  o sqlite3PagerWr
13864 69 74 65 28 29 2c 20 69 74 73 20 63 6f 6e 74 65  ite(), its conte
13865 6e 74 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20  nts.** will not 
13866 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 54 68  be journaled. Th
13867 69 73 20 73 61 76 65 73 20 49 4f 2e 0a 2a 2a 0a  is saves IO..**.
13868 2a 2a 20 54 68 65 20 61 63 71 75 69 73 69 74 69  ** The acquisiti
13869 6f 6e 20 6d 69 67 68 74 20 66 61 69 6c 20 66 6f  on might fail fo
1386a 72 20 73 65 76 65 72 61 6c 20 72 65 61 73 6f 6e  r several reason
1386b 73 2e 20 20 49 6e 20 61 6c 6c 20 63 61 73 65 73  s.  In all cases
1386c 2c 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69  ,.** an appropri
1386d 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ate error code i
1386e 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a  s returned and *
1386f 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f  ppPage is set to
13870 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65   NULL..**.** See
13871 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 67   also sqlite3Pag
13872 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20 42 6f 74  erLookup().  Bot
13873 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  h this routine a
13874 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61 74 74 65  nd Lookup() atte
13875 6d 70 74 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 61  mpt.** to find a
13876 20 70 61 67 65 20 69 6e 20 74 68 65 20 69 6e 2d   page in the in-
13877 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 66 69 72  memory cache fir
13878 73 74 2e 20 20 49 66 20 74 68 65 20 70 61 67 65  st.  If the page
13879 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a   is not already.
1387a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 2c 20 74 68  ** in memory, th
1387b 69 73 20 72 6f 75 74 69 6e 65 20 67 6f 65 73 20  is routine goes 
1387c 74 6f 20 64 69 73 6b 20 74 6f 20 72 65 61 64 20  to disk to read 
1387d 69 74 20 69 6e 20 77 68 65 72 65 61 73 20 4c 6f  it in whereas Lo
1387e 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75 73 74 20 72  okup().** just r
1387f 65 74 75 72 6e 73 20 30 2e 20 20 54 68 69 73 20  eturns 0.  This 
13880 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 73  routine acquires
13881 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 74 68 65   a read-lock the
13882 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 0a 2a   first time it.*
13883 2a 20 68 61 73 20 74 6f 20 67 6f 20 74 6f 20 64  * has to go to d
13884 69 73 6b 2c 20 61 6e 64 20 63 6f 75 6c 64 20 61  isk, and could a
13885 6c 73 6f 20 70 6c 61 79 62 61 63 6b 20 61 6e 20  lso playback an 
13886 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 69 66 20 6e  old journal if n
13887 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 53 69 6e  ecessary..** Sin
13888 63 65 20 4c 6f 6f 6b 75 70 28 29 20 6e 65 76 65  ce Lookup() neve
13889 72 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 2c 20  r goes to disk, 
1388a 69 74 20 6e 65 76 65 72 20 68 61 73 20 74 6f 20  it never has to 
1388b 64 65 61 6c 20 77 69 74 68 20 6c 6f 63 6b 73 0a  deal with locks.
1388c 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69  ** or journal fi
1388d 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  les..*/.SQLITE_P
1388e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1388f 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 0a  e3PagerAcquire(.
13890 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c    Pager *pPager,
13891 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
13892 65 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 64  er open on the d
13893 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
13894 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20    Pgno pgno,    
13895 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
13896 6d 62 65 72 20 74 6f 20 66 65 74 63 68 20 2a 2f  mber to fetch */
13897 0a 20 20 44 62 50 61 67 65 20 2a 2a 70 70 50 61  .  DbPage **ppPa
13898 67 65 2c 20 20 20 20 2f 2a 20 57 72 69 74 65 20  ge,    /* Write 
13899 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1389a 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a 20 20   page here */.  
1389b 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 20  int noContent   
1389c 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 62 6f      /* Do not bo
1389d 74 68 65 72 20 72 65 61 64 69 6e 67 20 63 6f 6e  ther reading con
1389e 74 65 6e 74 20 66 72 6f 6d 20 64 69 73 6b 20 69  tent from disk i
1389f 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 69  f true */.){.  i
138a0 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a  nt rc;.  PgHdr *
138a1 70 50 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  pPg;..  assert( 
138a2 61 73 73 65 72 74 5f 70 61 67 65 72 5f 73 74 61  assert_pager_sta
138a3 74 65 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  te(pPager) );.  
138a4 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
138a5 73 74 61 74 65 3e 50 41 47 45 52 5f 55 4e 4c 4f  state>PAGER_UNLO
138a6 43 4b 20 29 3b 0a 0a 20 20 69 66 28 20 70 67 6e  CK );..  if( pgn
138a7 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  o==0 ){.    retu
138a8 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
138a9 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f  T_BKPT;.  }..  /
138aa 2a 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69  * If the pager i
138ab 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20 73  s in the error s
138ac 74 61 74 65 2c 20 72 65 74 75 72 6e 20 61 6e 20  tate, return an 
138ad 65 72 72 6f 72 20 69 6d 6d 65 64 69 61 74 65 6c  error immediatel
138ae 79 2e 20 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69  y. .  ** Otherwi
138af 73 65 2c 20 72 65 71 75 65 73 74 20 74 68 65 20  se, request the 
138b0 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 50 43  page from the PC
138b1 61 63 68 65 20 6c 61 79 65 72 2e 20 2a 2f 0a 20  ache layer. */. 
138b2 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
138b3 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  Code!=SQLITE_OK 
138b4 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  && pPager->errCo
138b5 64 65 21 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20  de!=SQLITE_FULL 
138b6 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 50 61 67  ){.    rc = pPag
138b7 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d  er->errCode;.  }
138b8 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73  else{.    rc = s
138b9 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63  qlite3PcacheFetc
138ba 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  h(pPager->pPCach
138bb 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 70 70 50 61  e, pgno, 1, ppPa
138bc 67 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  ge);.  }..  if( 
138bd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
138be 0a 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74  .    /* Either t
138bf 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
138c0 65 33 50 63 61 63 68 65 46 65 74 63 68 28 29 20  e3PcacheFetch() 
138c1 72 65 74 75 72 6e 65 64 20 61 6e 20 65 72 72 6f  returned an erro
138c2 72 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20  r or the.    ** 
138c3 70 61 67 65 72 20 77 61 73 20 61 6c 72 65 61 64  pager was alread
138c4 79 20 69 6e 20 74 68 65 20 65 72 72 6f 72 2d 73  y in the error-s
138c5 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 66  tate when this f
138c6 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c  unction was call
138c7 65 64 2e 0a 20 20 20 20 2a 2a 20 53 65 74 20 70  ed..    ** Set p
138c8 50 67 20 74 6f 20 30 20 61 6e 64 20 6a 75 6d 70  Pg to 0 and jump
138c9 20 74 6f 20 74 68 65 20 65 78 63 65 70 74 69 6f   to the exceptio
138ca 6e 20 68 61 6e 64 6c 65 72 2e 20 20 2a 2f 0a 20  n handler.  */. 
138cb 20 20 20 70 50 67 20 3d 20 30 3b 0a 20 20 20 20     pPg = 0;.    
138cc 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69  goto pager_acqui
138cd 72 65 5f 65 72 72 3b 0a 20 20 7d 0a 20 20 61 73  re_err;.  }.  as
138ce 73 65 72 74 28 20 28 2a 70 70 50 61 67 65 29 2d  sert( (*ppPage)-
138cf 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20  >pgno==pgno );. 
138d0 20 61 73 73 65 72 74 28 20 28 2a 70 70 50 61 67   assert( (*ppPag
138d1 65 29 2d 3e 70 50 61 67 65 72 3d 3d 70 50 61 67  e)->pPager==pPag
138d2 65 72 20 7c 7c 20 28 2a 70 70 50 61 67 65 29 2d  er || (*ppPage)-
138d3 3e 70 50 61 67 65 72 3d 3d 30 20 29 3b 0a 0a 20  >pPager==0 );.. 
138d4 20 69 66 28 20 28 2a 70 70 50 61 67 65 29 2d 3e   if( (*ppPage)->
138d5 70 50 61 67 65 72 20 29 7b 0a 20 20 20 20 2f 2a  pPager ){.    /*
138d6 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
138d7 65 20 70 63 61 63 68 65 20 61 6c 72 65 61 64 79  e pcache already
138d8 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6e 69   contains an ini
138d9 74 69 61 6c 69 7a 65 64 20 63 6f 70 79 20 6f 66  tialized copy of
138da 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 61 67 65  .    ** the page
138db 2e 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74  . Return without
138dc 20 66 75 72 74 68 65 72 20 61 64 6f 2e 20 20 2a   further ado.  *
138dd 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67  /.    assert( pg
138de 6e 6f 3c 3d 50 41 47 45 52 5f 4d 41 58 5f 50 47  no<=PAGER_MAX_PG
138df 4e 4f 20 26 26 20 70 67 6e 6f 21 3d 50 41 47 45  NO && pgno!=PAGE
138e0 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72  R_MJ_PGNO(pPager
138e1 29 20 29 3b 0a 20 20 20 20 50 41 47 45 52 5f 49  ) );.    PAGER_I
138e2 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 48 69 74  NCR(pPager->nHit
138e3 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
138e4 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 7d 65 6c 73  LITE_OK;..  }els
138e5 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61  e{.    /* The pa
138e6 67 65 72 20 63 61 63 68 65 20 68 61 73 20 63 72  ger cache has cr
138e7 65 61 74 65 64 20 61 20 6e 65 77 20 70 61 67 65  eated a new page
138e8 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74 20 6e 65  . Its content ne
138e9 65 64 73 20 74 6f 20 0a 20 20 20 20 2a 2a 20 62  eds to .    ** b
138ea 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20  e initialized.  
138eb 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4d 61 78 3b  */.    int nMax;
138ec 0a 0a 20 20 20 20 50 41 47 45 52 5f 49 4e 43 52  ..    PAGER_INCR
138ed 28 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73 29 3b  (pPager->nMiss);
138ee 0a 20 20 20 20 70 50 67 20 3d 20 2a 70 70 50 61  .    pPg = *ppPa
138ef 67 65 3b 0a 20 20 20 20 70 50 67 2d 3e 70 50 61  ge;.    pPg->pPa
138f0 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a 0a 20  ger = pPager;.. 
138f1 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75     /* The maximu
138f2 6d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73  m page number is
138f3 20 32 5e 33 31 2e 20 52 65 74 75 72 6e 20 53 51   2^31. Return SQ
138f4 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 20  LITE_CORRUPT if 
138f5 61 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 6e 75  a page.    ** nu
138f6 6d 62 65 72 20 67 72 65 61 74 65 72 20 74 68 61  mber greater tha
138f7 6e 20 74 68 69 73 2c 20 6f 72 20 74 68 65 20 75  n this, or the u
138f8 6e 75 73 65 64 20 6c 6f 63 6b 69 6e 67 2d 70 61  nused locking-pa
138f9 67 65 2c 20 69 73 20 72 65 71 75 65 73 74 65 64  ge, is requested
138fa 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 67 6e  . */.    if( pgn
138fb 6f 3e 50 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f  o>PAGER_MAX_PGNO
138fc 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f   || pgno==PAGER_
138fd 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20  MJ_PGNO(pPager) 
138fe 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
138ff 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
13900 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 61  T;.      goto pa
13901 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72 3b  ger_acquire_err;
13902 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d  .    }..    rc =
13903 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
13904 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 26  ecount(pPager, &
13905 6e 4d 61 78 29 3b 0a 20 20 20 20 69 66 28 20 72  nMax);.    if( r
13906 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
13907 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72        goto pager
13908 5f 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20  _acquire_err;.  
13909 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 4d 45 4d    }..    if( MEM
1390a 44 42 20 7c 7c 20 6e 4d 61 78 3c 28 69 6e 74 29  DB || nMax<(int)
1390b 70 67 6e 6f 20 7c 7c 20 6e 6f 43 6f 6e 74 65 6e  pgno || noConten
1390c 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  t ){.      if( p
1390d 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 6d 78 50 67  gno>pPager->mxPg
1390e 6e 6f 20 29 7b 0a 09 72 63 20 3d 20 53 51 4c 49  no ){..rc = SQLI
1390f 54 45 5f 46 55 4c 4c 3b 0a 09 67 6f 74 6f 20 70  TE_FULL;..goto p
13910 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72 72  ager_acquire_err
13911 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
13912 69 66 28 20 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b  if( noContent ){
13913 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 61 69 6c  .        /* Fail
13914 75 72 65 20 74 6f 20 73 65 74 20 74 68 65 20 62  ure to set the b
13915 69 74 73 20 69 6e 20 74 68 65 20 49 6e 4a 6f 75  its in the InJou
13916 72 6e 61 6c 20 62 69 74 2d 76 65 63 74 6f 72 73  rnal bit-vectors
13917 20 69 73 20 62 65 6e 69 67 6e 2e 0a 20 20 20 20   is benign..    
13918 20 20 20 20 2a 2a 20 49 74 20 6d 65 72 65 6c 79      ** It merely
13919 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 6d   means that we m
1391a 69 67 68 74 20 64 6f 20 73 6f 6d 65 20 65 78 74  ight do some ext
1391b 72 61 20 77 6f 72 6b 20 74 6f 20 6a 6f 75 72 6e  ra work to journ
1391c 61 6c 20 61 20 0a 20 20 20 20 20 20 20 20 2a 2a  al a .        **
1391d 20 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20   page that does 
1391e 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6a  not need to be j
1391f 6f 75 72 6e 61 6c 65 64 2e 20 20 4e 65 76 65 72  ournaled.  Never
13920 74 68 65 6c 65 73 73 2c 20 62 65 20 73 75 72 65  theless, be sure
13921 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20   .        ** to 
13922 74 65 73 74 20 74 68 65 20 63 61 73 65 20 77 68  test the case wh
13923 65 72 65 20 61 20 6d 61 6c 6c 6f 63 20 65 72 72  ere a malloc err
13924 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
13925 74 72 79 69 6e 67 20 74 6f 20 73 65 74 20 0a 20  trying to set . 
13926 20 20 20 20 20 20 20 2a 2a 20 61 20 62 69 74 20         ** a bit 
13927 69 6e 20 61 20 62 69 74 20 76 65 63 74 6f 72 2e  in a bit vector.
13928 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
13929 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
1392a 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
1392b 20 20 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f          if( pgno
1392c 3c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67  <=pPager->dbOrig
1392d 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Size ){.        
1392e 20 20 54 45 53 54 4f 4e 4c 59 28 20 72 63 20 3d    TESTONLY( rc =
1392f 20 29 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   ) sqlite3Bitvec
13930 53 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a  Set(pPager->pInJ
13931 6f 75 72 6e 61 6c 2c 20 70 67 6e 6f 29 3b 0a 20  ournal, pgno);. 
13932 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73           testcas
13933 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  e( rc==SQLITE_NO
13934 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 7d  MEM );.        }
13935 0a 20 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c  .        TESTONL
13936 59 28 20 72 63 20 3d 20 29 20 61 64 64 54 6f 53  Y( rc = ) addToS
13937 61 76 65 70 6f 69 6e 74 42 69 74 76 65 63 73 28  avepointBitvecs(
13938 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20  pPager, pgno);. 
13939 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
1393a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
1393b 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  M );.        sql
1393c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
1393d 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 7d 65 6c  loc();.      }el
1393e 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  se{.        mems
1393f 65 74 28 70 50 67 2d 3e 70 44 61 74 61 2c 20 30  et(pPg->pData, 0
13940 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  , pPager->pageSi
13941 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ze);.      }.   
13942 20 20 20 49 4f 54 52 41 43 45 28 28 22 5a 45 52     IOTRACE(("ZER
13943 4f 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  O %p %d\n", pPag
13944 65 72 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20  er, pgno));.    
13945 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
13946 65 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72  ert( pPg->pPager
13947 3d 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 20 20  ==pPager );.    
13948 20 20 72 63 20 3d 20 72 65 61 64 44 62 50 61 67    rc = readDbPag
13949 65 28 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66  e(pPg);.      if
1394a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1394b 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
1394c 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72  pager_acquire_er
1394d 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  r;.      }.    }
1394e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43  .#ifdef SQLITE_C
1394f 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70  HECK_PAGES.    p
13950 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70  Pg->pageHash = p
13951 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
13952 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a  g);.#endif.  }..
13953 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
13954 4f 4b 3b 0a 0a 70 61 67 65 72 5f 61 63 71 75 69  OK;..pager_acqui
13955 72 65 5f 65 72 72 3a 0a 20 20 61 73 73 65 72 74  re_err:.  assert
13956 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13957 29 3b 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a  );.  if( pPg ){.
13958 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
13959 65 44 72 6f 70 28 70 50 67 29 3b 0a 20 20 7d 0a  eDrop(pPg);.  }.
1395a 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55    pagerUnlockIfU
1395b 6e 75 73 65 64 28 70 50 61 67 65 72 29 3b 0a 0a  nused(pPager);..
1395c 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 20    *ppPage = 0;. 
1395d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1395e 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 70  *.** Acquire a p
1395f 61 67 65 20 69 66 20 69 74 20 69 73 20 61 6c 72  age if it is alr
13960 65 61 64 79 20 69 6e 20 74 68 65 20 69 6e 2d 6d  eady in the in-m
13961 65 6d 6f 72 79 20 63 61 63 68 65 2e 20 20 44 6f  emory cache.  Do
13962 0a 2a 2a 20 6e 6f 74 20 72 65 61 64 20 74 68 65  .** not read the
13963 20 70 61 67 65 20 66 72 6f 6d 20 64 69 73 6b 2e   page from disk.
13964 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
13965 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 2c 0a  er to the page,.
13966 2a 2a 20 6f 72 20 30 20 69 66 20 74 68 65 20 70  ** or 0 if the p
13967 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61  age is not in ca
13968 63 68 65 2e 20 41 6c 73 6f 2c 20 72 65 74 75 72  che. Also, retur
13969 6e 20 30 20 69 66 20 74 68 65 20 0a 2a 2a 20 70  n 0 if the .** p
1396a 61 67 65 72 20 69 73 20 69 6e 20 50 41 47 45 52  ager is in PAGER
1396b 5f 55 4e 4c 4f 43 4b 20 73 74 61 74 65 20 77 68  _UNLOCK state wh
1396c 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
1396d 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 6f   is called,.** o
1396e 72 20 69 66 20 74 68 65 20 70 61 67 65 72 20 69  r if the pager i
1396f 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74  s in an error st
13970 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53  ate other than S
13971 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a 2a 0a 2a  QLITE_FULL..**.*
13972 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74  * See also sqlit
13973 65 33 50 61 67 65 72 47 65 74 28 29 2e 20 20 54  e3PagerGet().  T
13974 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65  he difference be
13975 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74 69  tween this routi
13976 6e 65 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65  ne.** and sqlite
13977 33 50 61 67 65 72 47 65 74 28 29 20 69 73 20 74  3PagerGet() is t
13978 68 61 74 20 5f 67 65 74 28 29 20 77 69 6c 6c 20  hat _get() will 
13979 67 6f 20 74 6f 20 74 68 65 20 64 69 73 6b 20 61  go to the disk a
1397a 6e 64 20 72 65 61 64 0a 2a 2a 20 69 6e 20 74 68  nd read.** in th
1397b 65 20 70 61 67 65 20 69 66 20 74 68 65 20 70 61  e page if the pa
1397c 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  ge is not alread
1397d 79 20 69 6e 20 63 61 63 68 65 2e 20 20 54 68 69  y in cache.  Thi
1397e 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74  s routine.** ret
1397f 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 74 68 65  urns NULL if the
13980 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e 20   page is not in 
13981 63 61 63 68 65 20 6f 72 20 69 66 20 61 20 64 69  cache or if a di
13982 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 0a 2a 2a  sk I/O error .**
13983 20 68 61 73 20 65 76 65 72 20 68 61 70 70 65 6e   has ever happen
13984 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
13985 49 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71  IVATE DbPage *sq
13986 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70  lite3PagerLookup
13987 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
13988 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67  Pgno pgno){.  Pg
13989 48 64 72 20 2a 70 50 67 20 3d 20 30 3b 0a 20 20  Hdr *pPg = 0;.  
1398a 61 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d  assert( pPager!=
1398b 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1398c 67 6e 6f 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  gno!=0 );.  asse
1398d 72 74 28 20 70 50 61 67 65 72 2d 3e 70 50 43 61  rt( pPager->pPCa
1398e 63 68 65 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  che!=0 );.  asse
1398f 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
13990 65 20 3e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b  e > PAGER_UNLOCK
13991 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 63 61   );.  sqlite3Pca
13992 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72 2d  cheFetch(pPager-
13993 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20  >pPCache, pgno, 
13994 30 2c 20 26 70 50 67 29 3b 0a 20 20 72 65 74 75  0, &pPg);.  retu
13995 72 6e 20 70 50 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn pPg;.}../*.**
13996 20 52 65 6c 65 61 73 65 20 61 20 70 61 67 65 20   Release a page 
13997 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a  reference..**.**
13998 20 49 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   If the number o
13999 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  f references to 
1399a 74 68 65 20 70 61 67 65 20 64 72 6f 70 20 74 6f  the page drop to
1399b 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 0a   zero, then the.
1399c 2a 2a 20 70 61 67 65 20 69 73 20 61 64 64 65 64  ** page is added
1399d 20 74 6f 20 74 68 65 20 4c 52 55 20 6c 69 73 74   to the LRU list
1399e 2e 20 20 57 68 65 6e 20 61 6c 6c 20 72 65 66 65  .  When all refe
1399f 72 65 6e 63 65 73 20 74 6f 20 61 6c 6c 20 70 61  rences to all pa
139a0 67 65 73 0a 2a 2a 20 61 72 65 20 72 65 6c 65 61  ges.** are relea
139a1 73 65 64 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 20  sed, a rollback 
139a2 6f 63 63 75 72 73 20 61 6e 64 20 74 68 65 20 6c  occurs and the l
139a3 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
139a4 61 73 65 20 69 73 0a 2a 2a 20 72 65 6d 6f 76 65  ase is.** remove
139a5 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
139a6 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
139a7 33 50 61 67 65 72 55 6e 72 65 66 28 44 62 50 61  3PagerUnref(DbPa
139a8 67 65 20 2a 70 50 67 29 7b 0a 20 20 69 66 28 20  ge *pPg){.  if( 
139a9 70 50 67 20 29 7b 0a 20 20 20 20 50 61 67 65 72  pPg ){.    Pager
139aa 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
139ab 70 50 61 67 65 72 3b 0a 20 20 20 20 73 71 6c 69  pPager;.    sqli
139ac 74 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65  te3PcacheRelease
139ad 28 70 50 67 29 3b 0a 20 20 20 20 70 61 67 65 72  (pPg);.    pager
139ae 55 6e 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 70  UnlockIfUnused(p
139af 50 61 67 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Pager);.  }.}../
139b0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 61 69 6e  *.** If the main
139b1 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61   journal file ha
139b2 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6f  s already been o
139b3 70 65 6e 65 64 2c 20 65 6e 73 75 72 65 20 74 68  pened, ensure th
139b4 61 74 20 74 68 65 0a 2a 2a 20 73 75 62 2d 6a 6f  at the.** sub-jo
139b5 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70  urnal file is op
139b6 65 6e 20 74 6f 6f 2e 20 49 66 20 74 68 65 20 6d  en too. If the m
139b7 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6e  ain journal is n
139b8 6f 74 20 6f 70 65 6e 2c 0a 2a 2a 20 74 68 69 73  ot open,.** this
139b9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
139ba 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  o-op..**.** SQLI
139bb 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
139bc 64 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20  d if everything 
139bd 67 6f 65 73 20 61 63 63 6f 72 64 69 6e 67 20 74  goes according t
139be 6f 20 70 6c 61 6e 2e 20 0a 2a 2a 20 41 6e 20 53  o plan. .** An S
139bf 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 20  QLITE_IOERR_XXX 
139c0 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
139c1 74 75 72 6e 65 64 20 69 66 20 61 20 63 61 6c 6c  turned if a call
139c2 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33 4f   to .** sqlite3O
139c3 73 4f 70 65 6e 28 29 20 66 61 69 6c 73 2e 0a 2a  sOpen() fails..*
139c4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65  /.static int ope
139c5 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 50 61 67 65  nSubJournal(Page
139c6 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e  r *pPager){.  in
139c7 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
139c8 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70  ;.  if( isOpen(p
139c9 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 21  Pager->jfd) && !
139ca 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73  isOpen(pPager->s
139cb 6a 66 64 29 20 29 7b 0a 20 20 20 20 69 66 28 20  jfd) ){.    if( 
139cc 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
139cd 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
139ce 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 7c 7c  ALMODE_MEMORY ||
139cf 20 70 50 61 67 65 72 2d 3e 73 75 62 6a 49 6e 4d   pPager->subjInM
139d0 65 6d 6f 72 79 20 29 7b 0a 20 20 20 20 20 20 73  emory ){.      s
139d1 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c  qlite3MemJournal
139d2 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66  Open(pPager->sjf
139d3 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
139d4 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 4f       rc = pagerO
139d5 70 65 6e 74 65 6d 70 28 70 50 61 67 65 72 2c 20  pentemp(pPager, 
139d6 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 53 51  pPager->sjfd, SQ
139d7 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55  LITE_OPEN_SUBJOU
139d8 52 4e 41 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  RNAL);.    }.  }
139d9 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
139da 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
139db 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61  tion is called a
139dc 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65  t the start of e
139dd 76 65 72 79 20 77 72 69 74 65 20 74 72 61 6e 73  very write trans
139de 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 72 65  action..** There
139df 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 62 65   must already be
139e0 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 45   a RESERVED or E
139e1 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e  XCLUSIVE lock on
139e2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a   the database .*
139e3 2a 20 66 69 6c 65 20 77 68 65 6e 20 74 68 69 73  * file when this
139e4 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
139e5 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 74  ed..**.** Open t
139e6 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
139e7 66 6f 72 20 70 61 67 65 72 20 70 50 61 67 65 72  for pager pPager
139e8 20 61 6e 64 20 77 72 69 74 65 20 61 20 6a 6f 75   and write a jou
139e9 72 6e 61 6c 20 68 65 61 64 65 72 0a 2a 2a 20 74  rnal header.** t
139ea 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 69  o the start of i
139eb 74 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20  t. If there are 
139ec 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74  active savepoint
139ed 73 2c 20 6f 70 65 6e 20 74 68 65 20 73 75 62 2d  s, open the sub-
139ee 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 61 73 20 77 65  journal.** as we
139ef 6c 6c 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ll. This functio
139f0 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77  n is only used w
139f1 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hen the journal 
139f2 66 69 6c 65 20 69 73 20 62 65 69 6e 67 20 0a 2a  file is being .*
139f3 2a 20 6f 70 65 6e 65 64 20 74 6f 20 77 72 69 74  * opened to writ
139f4 65 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6c 6f 67  e a rollback log
139f5 20 66 6f 72 20 61 20 74 72 61 6e 73 61 63 74 69   for a transacti
139f6 6f 6e 2e 20 49 74 20 69 73 20 6e 6f 74 20 75 73  on. It is not us
139f7 65 64 20 0a 2a 2a 20 77 68 65 6e 20 6f 70 65 6e  ed .** when open
139f8 69 6e 67 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61  ing a hot journa
139f9 6c 20 66 69 6c 65 20 74 6f 20 72 6f 6c 6c 20 69  l file to roll i
139fa 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  t back..**.** If
139fb 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
139fc 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f 70 65  e is already ope
139fd 6e 20 28 61 73 20 69 74 20 6d 61 79 20 62 65 20  n (as it may be 
139fe 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  in exclusive mod
139ff 65 29 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73  e),.** then this
13a00 20 66 75 6e 63 74 69 6f 6e 20 6a 75 73 74 20 77   function just w
13a01 72 69 74 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20  rites a journal 
13a02 68 65 61 64 65 72 20 74 6f 20 74 68 65 20 73 74  header to the st
13a03 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61 6c  art of the.** al
13a04 72 65 61 64 79 20 6f 70 65 6e 20 66 69 6c 65 2e  ready open file.
13a05 20 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20   .**.** Whether 
13a06 6f 72 20 6e 6f 74 20 74 68 65 20 6a 6f 75 72 6e  or not the journ
13a07 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65  al file is opene
13a08 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
13a09 6f 6e 2c 20 74 68 65 0a 2a 2a 20 50 61 67 65 72  on, the.** Pager
13a0a 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 76  .pInJournal bitv
13a0b 65 63 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ec structure is 
13a0c 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a  allocated..**.**
13a0d 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
13a0e 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67 20  K if everything 
13a0f 69 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20 4f  is successful. O
13a10 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e  therwise, return
13a11 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45   .** SQLITE_NOME
13a12 4d 20 69 66 20 74 68 65 20 61 74 74 65 6d 70 74  M if the attempt
13a13 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 50 61 67   to allocate Pag
13a14 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c 20 66 61  er.pInJournal fa
13a15 69 6c 73 2c 20 6f 72 20 0a 2a 2a 20 61 6e 20 49  ils, or .** an I
13a16 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  O error code if 
13a17 6f 70 65 6e 69 6e 67 20 6f 72 20 77 72 69 74 69  opening or writi
13a18 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  ng the journal f
13a19 69 6c 65 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74  ile fails..*/.st
13a1a 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 6f  atic int pager_o
13a1b 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 50 61 67 65  pen_journal(Page
13a1c 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e  r *pPager){.  in
13a1d 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
13a1e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
13a1f 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
13a20 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c  rn code */.  sql
13a21 69 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74  ite3_vfs * const
13a22 20 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e   pVfs = pPager->
13a23 70 56 66 73 3b 20 20 20 2f 2a 20 4c 6f 63 61 6c  pVfs;   /* Local
13a24 20 63 61 63 68 65 20 6f 66 20 76 66 73 20 70 6f   cache of vfs po
13a25 69 6e 74 65 72 20 2a 2f 0a 0a 20 20 61 73 73 65  inter */..  asse
13a26 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
13a27 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45  e>=PAGER_RESERVE
13a28 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  D );.  assert( p
13a29 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61  Pager->useJourna
13a2a 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  l );.  assert( p
13a2b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
13a2c 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de!=PAGER_JOURNA
13a2d 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 61  LMODE_OFF );.  a
13a2e 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
13a2f 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a  InJournal==0 );.
13a30 20 20 0a 20 20 2f 2a 20 49 66 20 61 6c 72 65 61    .  /* If alrea
13a31 64 79 20 69 6e 20 74 68 65 20 65 72 72 6f 72 20  dy in the error 
13a32 73 74 61 74 65 2c 20 74 68 69 73 20 66 75 6e 63  state, this func
13a33 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
13a34 20 20 42 75 74 20 6f 6e 0a 20 20 2a 2a 20 74 68    But on.  ** th
13a35 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 74 68  e other hand, th
13a36 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65  is routine is ne
13a37 76 65 72 20 63 61 6c 6c 65 64 20 69 66 20 77 65  ver called if we
13a38 20 61 72 65 20 61 6c 72 65 61 64 79 20 69 6e 0a   are already in.
13a39 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 73 74    ** an error st
13a3a 61 74 65 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45  ate. */.  if( NE
13a3b 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43  VER(pPager->errC
13a3c 6f 64 65 29 20 29 20 72 65 74 75 72 6e 20 70 50  ode) ) return pP
13a3d 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a  ager->errCode;..
13a3e 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20 69 74    /* TODO: Is it
13a3f 20 72 65 61 6c 6c 79 20 70 6f 73 73 69 62 6c 65   really possible
13a40 20 74 6f 20 67 65 74 20 68 65 72 65 20 77 69 74   to get here wit
13a41 68 20 64 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30  h dbSizeValid==0
13a42 3f 20 49 66 20 6e 6f 74 2c 0a 20 20 2a 2a 20 74  ? If not,.  ** t
13a43 68 65 20 63 61 6c 6c 20 74 6f 20 50 61 67 65 72  he call to Pager
13a44 50 61 67 65 63 6f 75 6e 74 28 29 20 63 61 6e 20  Pagecount() can 
13a45 62 65 20 72 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f  be removed..  */
13a46 0a 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61  .  testcase( pPa
13a47 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
13a48 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ==0 );.  sqlite3
13a49 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  PagerPagecount(p
13a4a 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 20 70 50  Pager, 0);..  pP
13a4b 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
13a4c 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   = sqlite3Bitvec
13a4d 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e 64  Create(pPager->d
13a4e 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 50  bSize);.  if( pP
13a4f 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
13a50 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
13a51 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
13a52 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74    }..  /* Open t
13a53 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
13a54 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72  if it is not alr
13a55 65 61 64 79 20 6f 70 65 6e 2e 20 2a 2f 0a 20 20  eady open. */.  
13a56 69 66 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67  if( !isOpen(pPag
13a57 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20  er->jfd) ){.    
13a58 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  if( pPager->jour
13a59 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a  nalMode==PAGER_J
13a5a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52  OURNALMODE_MEMOR
13a5b 59 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  Y ){.      sqlit
13a5c 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e  e3MemJournalOpen
13a5d 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20  (pPager->jfd);. 
13a5e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
13a5f 63 6f 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20  const int flags 
13a60 3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  =               
13a61 20 20 20 20 2f 2a 20 56 46 53 20 66 6c 61 67 73      /* VFS flags
13a62 20 74 6f 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c   to open journal
13a63 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20   file */.       
13a64 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
13a65 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
13a66 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20  EN_CREATE|.     
13a67 20 20 20 28 70 50 61 67 65 72 2d 3e 74 65 6d 70     (pPager->temp
13a68 46 69 6c 65 20 3f 20 0a 20 20 20 20 20 20 20 20  File ? .        
13a69 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44    (SQLITE_OPEN_D
13a6a 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 7c 53 51 4c  ELETEONCLOSE|SQL
13a6b 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f  ITE_OPEN_TEMP_JO
13a6c 55 52 4e 41 4c 29 3a 0a 20 20 20 20 20 20 20 20  URNAL):.        
13a6d 20 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d    (SQLITE_OPEN_M
13a6e 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 0a 20 20 20  AIN_JOURNAL).   
13a6f 20 20 20 20 20 29 3b 0a 23 69 66 64 65 66 20 53       );.#ifdef S
13a70 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
13a71 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 20 20  MIC_WRITE.      
13a72 72 63 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75 72  rc = sqlite3Jour
13a73 6e 61 6c 4f 70 65 6e 28 0a 20 20 20 20 20 20 20  nalOpen(.       
13a74 20 20 20 70 56 66 73 2c 20 70 50 61 67 65 72 2d     pVfs, pPager-
13a75 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65  >zJournal, pPage
13a76 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 6a  r->jfd, flags, j
13a77 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 28 70 50  rnlBufferSize(pP
13a78 61 67 65 72 29 0a 20 20 20 20 20 20 29 3b 0a 23  ager).      );.#
13a79 65 6c 73 65 0a 20 20 20 20 20 20 72 63 20 3d 20  else.      rc = 
13a7a 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56  sqlite3OsOpen(pV
13a7b 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  fs, pPager->zJou
13a7c 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a 66  rnal, pPager->jf
13a7d 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 23 65  d, flags, 0);.#e
13a7e 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 61  ndif.    }.    a
13a7f 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
13a80 45 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70  E_OK || isOpen(p
13a81 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20  Pager->jfd) );. 
13a82 20 7d 0a 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20   }...  /* Write 
13a83 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61  the first journa
13a84 6c 20 68 65 61 64 65 72 20 74 6f 20 74 68 65 20  l header to the 
13a85 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 6e 64  journal file and
13a86 20 6f 70 65 6e 20 0a 20 20 2a 2a 20 74 68 65 20   open .  ** the 
13a87 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 66 20 6e  sub-journal if n
13a88 65 63 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20  ecessary..  */. 
13a89 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13a8a 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 54 4f 44  OK ){.    /* TOD
13a8b 4f 3a 20 43 68 65 63 6b 20 69 66 20 61 6c 6c 20  O: Check if all 
13a8c 6f 66 20 74 68 65 73 65 20 61 72 65 20 72 65 61  of these are rea
13a8d 6c 6c 79 20 72 65 71 75 69 72 65 64 2e 20 2a 2f  lly required. */
13a8e 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 4f  .    pPager->dbO
13a8f 72 69 67 53 69 7a 65 20 3d 20 70 50 61 67 65 72  rigSize = pPager
13a90 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 70 50  ->dbSize;.    pP
13a91 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61  ager->journalSta
13a92 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20 70 50  rted = 0;.    pP
13a93 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d  ager->needSync =
13a94 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   0;.    pPager->
13a95 6e 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 70 50  nRec = 0;.    pP
13a96 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
13a97 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
13a98 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b  ->setMaster = 0;
13a99 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
13a9a 72 6e 61 6c 48 64 72 20 3d 20 30 3b 0a 20 20 20  rnalHdr = 0;.   
13a9b 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e   rc = writeJourn
13a9c 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20  alHdr(pPager);. 
13a9d 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
13a9e 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72  ITE_OK && pPager
13a9f 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a  ->nSavepoint ){.
13aa0 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 53 75 62      rc = openSub
13aa1 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b  Journal(pPager);
13aa2 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 21 3d  .  }..  if( rc!=
13aa3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13aa4 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
13aa5 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49  stroy(pPager->pI
13aa6 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70  nJournal);.    p
13aa7 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
13aa8 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  l = 0;.  }.  ret
13aa9 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
13aaa 20 42 65 67 69 6e 20 61 20 77 72 69 74 65 2d 74   Begin a write-t
13aab 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
13aac 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65  e specified page
13aad 72 20 6f 62 6a 65 63 74 2e 20 49 66 20 61 20 0a  r object. If a .
13aae 2a 2a 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  ** write-transac
13aaf 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79  tion has already
13ab0 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c 20 74 68   been opened, th
13ab1 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
13ab2 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66   no-op..**.** If
13ab3 20 74 68 65 20 65 78 46 6c 61 67 20 61 72 67 75   the exFlag argu
13ab4 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 2c 20 74  ment is false, t
13ab5 68 65 6e 20 61 63 71 75 69 72 65 20 61 74 20 6c  hen acquire at l
13ab6 65 61 73 74 20 61 20 52 45 53 45 52 56 45 44 0a  east a RESERVED.
13ab7 2a 2a 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  ** lock on the d
13ab8 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66  atabase file. If
13ab9 20 65 78 46 6c 61 67 20 69 73 20 74 72 75 65 2c   exFlag is true,
13aba 20 74 68 65 6e 20 61 63 71 75 69 72 65 20 61 74   then acquire at
13abb 20 6c 65 61 73 74 0a 2a 2a 20 61 6e 20 45 58 43   least.** an EXC
13abc 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20  LUSIVE lock. If 
13abd 73 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 61  such a lock is a
13abe 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 6e 6f 20  lready held, no 
13abf 6c 6f 63 6b 69 6e 67 20 0a 2a 2a 20 66 75 6e 63  locking .** func
13ac0 74 69 6f 6e 73 20 6e 65 65 64 20 62 65 20 63 61  tions need be ca
13ac1 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  lled..**.** If t
13ac2 68 69 73 20 69 73 20 6e 6f 74 20 61 20 74 65 6d  his is not a tem
13ac3 70 6f 72 61 72 79 20 6f 72 20 69 6e 2d 6d 65 6d  porary or in-mem
13ac4 6f 72 79 20 66 69 6c 65 20 61 6e 64 2c 20 74 68  ory file and, th
13ac5 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  e journal file i
13ac6 73 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 69 66 20  s .** opened if 
13ac7 69 74 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  it has not been 
13ac8 61 6c 72 65 61 64 79 2e 20 46 6f 72 20 61 20 74  already. For a t
13ac9 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2c 20 74  emporary file, t
13aca 68 65 20 6f 70 65 6e 69 6e 67 20 0a 2a 2a 20 6f  he opening .** o
13acb 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
13acc 6c 65 20 69 73 20 64 65 66 65 72 72 65 64 20 75  le is deferred u
13acd 6e 74 69 6c 20 74 68 65 72 65 20 69 73 20 61 6e  ntil there is an
13ace 20 61 63 74 75 61 6c 20 6e 65 65 64 20 74 6f 20   actual need to 
13acf 0a 2a 2a 20 77 72 69 74 65 20 74 6f 20 74 68 65  .** write to the
13ad0 20 6a 6f 75 72 6e 61 6c 2e 20 54 4f 44 4f 3a 20   journal. TODO: 
13ad1 57 68 79 20 68 61 6e 64 6c 65 20 74 65 6d 70 6f  Why handle tempo
13ad2 72 61 72 79 20 66 69 6c 65 73 20 64 69 66 66 65  rary files diffe
13ad3 72 65 6e 74 6c 79 3f 0a 2a 2a 0a 2a 2a 20 49 66  rently?.**.** If
13ad4 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
13ad5 65 20 69 73 20 6f 70 65 6e 65 64 20 28 6f 72 20  e is opened (or 
13ad6 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64 79  if it is already
13ad7 20 6f 70 65 6e 29 2c 20 74 68 65 6e 20 61 0a 2a   open), then a.*
13ad8 2a 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72  * journal-header
13ad9 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 74   is written to t
13ada 68 65 20 73 74 61 72 74 20 6f 66 20 69 74 2e 0a  he start of it..
13adb 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 75 62  **.** If the sub
13adc 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65  jInMemory argume
13add 6e 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  nt is non-zero, 
13ade 74 68 65 6e 20 61 6e 79 20 73 75 62 2d 6a 6f 75  then any sub-jou
13adf 72 6e 61 6c 20 6f 70 65 6e 65 64 0a 2a 2a 20 77  rnal opened.** w
13ae0 69 74 68 69 6e 20 74 68 69 73 20 74 72 61 6e 73  ithin this trans
13ae1 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 6f  action will be o
13ae2 70 65 6e 65 64 20 61 73 20 61 6e 20 69 6e 2d 6d  pened as an in-m
13ae3 65 6d 6f 72 79 20 66 69 6c 65 2e 20 54 68 69 73  emory file. This
13ae4 0a 2a 2a 20 68 61 73 20 6e 6f 20 65 66 66 65 63  .** has no effec
13ae5 74 20 69 66 20 74 68 65 20 73 75 62 2d 6a 6f 75  t if the sub-jou
13ae6 72 6e 61 6c 20 69 73 20 61 6c 72 65 61 64 79 20  rnal is already 
13ae7 6f 70 65 6e 65 64 20 28 61 73 20 69 74 20 6d 61  opened (as it ma
13ae8 79 20 62 65 20 77 68 65 6e 0a 2a 2a 20 72 75 6e  y be when.** run
13ae9 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75 73 69 76  ning in exclusiv
13aea 65 20 6d 6f 64 65 29 20 6f 72 20 69 66 20 74 68  e mode) or if th
13aeb 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 64 6f  e transaction do
13aec 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61  es not require a
13aed 0a 2a 2a 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2e  .** sub-journal.
13aee 20 49 66 20 74 68 65 20 73 75 62 6a 49 6e 4d 65   If the subjInMe
13aef 6d 6f 72 79 20 61 72 67 75 6d 65 6e 74 20 69 73  mory argument is
13af0 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61 6e 79 20   zero, then any 
13af1 72 65 71 75 69 72 65 64 0a 2a 2a 20 73 75 62 2d  required.** sub-
13af2 6a 6f 75 72 6e 61 6c 20 69 73 20 69 6d 70 6c 65  journal is imple
13af3 6d 65 6e 74 65 64 20 69 6e 2d 6d 65 6d 6f 72 79  mented in-memory
13af4 20 69 66 20 70 50 61 67 65 72 20 69 73 20 61 6e   if pPager is an
13af5 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
13af6 61 73 65 2c 20 0a 2a 2a 20 6f 72 20 75 73 69 6e  ase, .** or usin
13af7 67 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  g a temporary fi
13af8 6c 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f  le otherwise..*/
13af9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13afa 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
13afb 42 65 67 69 6e 28 50 61 67 65 72 20 2a 70 50 61  Begin(Pager *pPa
13afc 67 65 72 2c 20 69 6e 74 20 65 78 46 6c 61 67 2c  ger, int exFlag,
13afd 20 69 6e 74 20 73 75 62 6a 49 6e 4d 65 6d 6f 72   int subjInMemor
13afe 79 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  y){.  int rc = S
13aff 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65  QLITE_OK;.  asse
13b00 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
13b01 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20  e!=PAGER_UNLOCK 
13b02 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 75 62  );.  pPager->sub
13b03 6a 49 6e 4d 65 6d 6f 72 79 20 3d 20 28 75 38 29  jInMemory = (u8)
13b04 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 3b 0a 20 20  subjInMemory;.  
13b05 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  if( pPager->stat
13b06 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20  e==PAGER_SHARED 
13b07 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
13b08 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
13b09 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  l==0 );.    asse
13b0a 72 74 28 20 21 4d 45 4d 44 42 20 26 26 20 21 70  rt( !MEMDB && !p
13b0b 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
13b0c 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 74 61 69  );..    /* Obtai
13b0d 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  n a RESERVED loc
13b0e 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
13b0f 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20 65  e file. If the e
13b10 78 46 6c 61 67 20 70 61 72 61 6d 65 74 65 72 0a  xFlag parameter.
13b11 20 20 20 20 2a 2a 20 69 73 20 74 72 75 65 2c 20      ** is true, 
13b12 74 68 65 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79  then immediately
13b13 20 75 70 67 72 61 64 65 20 74 68 69 73 20 74 6f   upgrade this to
13b14 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
13b15 63 6b 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 62  ck. The.    ** b
13b16 75 73 79 2d 68 61 6e 64 6c 65 72 20 63 61 6c 6c  usy-handler call
13b17 62 61 63 6b 20 63 61 6e 20 62 65 20 75 73 65 64  back can be used
13b18 20 77 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20   when upgrading 
13b19 74 6f 20 74 68 65 20 45 58 43 4c 55 53 49 56 45  to the EXCLUSIVE
13b1a 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 2c 20 62 75  .    ** lock, bu
13b1b 74 20 6e 6f 74 20 77 68 65 6e 20 6f 62 74 61 69  t not when obtai
13b1c 6e 69 6e 67 20 74 68 65 20 52 45 53 45 52 56 45  ning the RESERVE
13b1d 44 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20  D lock..    */. 
13b1e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
13b1f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64  sLock(pPager->fd
13b20 2c 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29  , RESERVED_LOCK)
13b21 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
13b22 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13b23 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d   pPager->state =
13b24 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 3b   PAGER_RESERVED;
13b25 0a 20 20 20 20 20 20 69 66 28 20 65 78 46 6c 61  .      if( exFla
13b26 67 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  g ){.        rc 
13b27 3d 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f  = pager_wait_on_
13b28 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58 43  lock(pPager, EXC
13b29 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20  LUSIVE_LOCK);.  
13b2a 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
13b2b 20 2f 2a 20 49 66 20 74 68 65 20 72 65 71 75 69   /* If the requi
13b2c 72 65 64 20 6c 6f 63 6b 73 20 77 65 72 65 20 73  red locks were s
13b2d 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74 61  uccessfully obta
13b2e 69 6e 65 64 2c 20 6f 70 65 6e 20 74 68 65 20 6a  ined, open the j
13b2f 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69  ournal.    ** fi
13b30 6c 65 20 61 6e 64 20 77 72 69 74 65 20 74 68 65  le and write the
13b31 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 2d 68   first journal-h
13b32 65 61 64 65 72 20 74 6f 20 69 74 2e 0a 20 20 20  eader to it..   
13b33 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
13b34 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61  SQLITE_OK && pPa
13b35 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
13b36 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  !=PAGER_JOURNALM
13b37 4f 44 45 5f 4f 46 46 20 29 7b 0a 20 20 20 20 20  ODE_OFF ){.     
13b38 20 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e   rc = pager_open
13b39 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29  _journal(pPager)
13b3a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
13b3b 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65  if( isOpen(pPage
13b3c 72 2d 3e 6a 66 64 29 20 26 26 20 70 50 61 67 65  r->jfd) && pPage
13b3d 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30  r->journalOff==0
13b3e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
13b3f 68 61 70 70 65 6e 73 20 77 68 65 6e 20 74 68 65  happens when the
13b40 20 70 61 67 65 72 20 77 61 73 20 69 6e 20 65 78   pager was in ex
13b41 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d  clusive-access m
13b42 6f 64 65 20 74 68 65 20 6c 61 73 74 0a 20 20 20  ode the last.   
13b43 20 2a 2a 20 74 69 6d 65 20 61 20 28 72 65 61 64   ** time a (read
13b44 20 6f 72 20 77 72 69 74 65 29 20 74 72 61 6e 73   or write) trans
13b45 61 63 74 69 6f 6e 20 77 61 73 20 73 75 63 63 65  action was succe
13b46 73 73 66 75 6c 6c 79 20 63 6f 6e 63 6c 75 64 65  ssfully conclude
13b47 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 68 69 73  d.    ** by this
13b48 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49 6e 73   connection. Ins
13b49 74 65 61 64 20 6f 66 20 64 65 6c 65 74 69 6e 67  tead of deleting
13b4a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
13b4b 65 20 69 74 20 77 61 73 20 0a 20 20 20 20 2a 2a  e it was .    **
13b4c 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 20 65   kept open and e
13b4d 69 74 68 65 72 20 77 61 73 20 74 72 75 6e 63 61  ither was trunca
13b4e 74 65 64 20 74 6f 20 30 20 62 79 74 65 73 20 6f  ted to 0 bytes o
13b4f 72 20 69 74 73 20 68 65 61 64 65 72 20 77 61 73  r its header was
13b50 0a 20 20 20 20 2a 2a 20 6f 76 65 72 77 72 69 74  .    ** overwrit
13b51 74 65 6e 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a  ten with zeros..
13b52 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
13b53 74 28 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 3d  t( pPager->nRec=
13b54 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
13b55 28 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67  ( pPager->dbOrig
13b56 53 69 7a 65 3d 3d 30 20 29 3b 0a 20 20 20 20 61  Size==0 );.    a
13b57 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
13b58 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a  InJournal==0 );.
13b59 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 6f      rc = pager_o
13b5a 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61 67  pen_journal(pPag
13b5b 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45  er);.  }..  PAGE
13b5c 52 54 52 41 43 45 28 28 22 54 52 41 4e 53 41 43  RTRACE(("TRANSAC
13b5d 54 49 4f 4e 20 25 64 5c 6e 22 2c 20 50 41 47 45  TION %d\n", PAGE
13b5e 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20  RID(pPager)));. 
13b5f 20 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e   assert( !isOpen
13b60 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c  (pPager->jfd) ||
13b61 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13b62 4f 66 66 3e 30 20 7c 7c 20 72 63 21 3d 53 51 4c  Off>0 || rc!=SQL
13b63 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20  ITE_OK );.  if( 
13b64 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
13b65 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50  .    assert( !pP
13b66 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64  ager->dbModified
13b67 20 29 3b 0a 20 20 20 20 2f 2a 20 49 67 6e 6f 72   );.    /* Ignor
13b68 65 20 61 6e 79 20 49 4f 20 65 72 72 6f 72 20 74  e any IO error t
13b69 68 61 74 20 6f 63 63 75 72 73 20 77 69 74 68 69  hat occurs withi
13b6a 6e 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e  n pager_end_tran
13b6b 73 61 63 74 69 6f 6e 28 29 2e 20 54 68 65 0a 20  saction(). The. 
13b6c 20 20 20 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66     ** purpose of
13b6d 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 74 6f   this call is to
13b6e 20 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72   reset the inter
13b6f 6e 61 6c 20 73 74 61 74 65 20 6f 66 20 74 68 65  nal state of the
13b70 20 70 61 67 65 72 0a 20 20 20 20 2a 2a 20 73 75   pager.    ** su
13b71 62 2d 73 79 73 74 65 6d 2e 20 49 74 20 64 6f 65  b-system. It doe
13b72 73 6e 27 74 20 6d 61 74 74 65 72 20 69 66 20 74  sn't matter if t
13b73 68 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20  he journal-file 
13b74 69 73 20 6e 6f 74 20 70 72 6f 70 65 72 6c 79 0a  is not properly.
13b75 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69 7a 65 64      ** finalized
13b76 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 28   at this point (
13b77 73 69 6e 63 65 20 69 74 20 69 73 20 6e 6f 74 20  since it is not 
13b78 61 20 76 61 6c 69 64 20 6a 6f 75 72 6e 61 6c 20  a valid journal 
13b79 66 69 6c 65 20 61 6e 79 77 61 79 29 2e 0a 20 20  file anyway)..  
13b7a 20 20 2a 2f 0a 20 20 20 20 70 61 67 65 72 5f 65    */.    pager_e
13b7b 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70  nd_transaction(p
13b7c 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 7d 0a 20  Pager, 0);.  }. 
13b7d 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
13b7e 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 73 69 6e 67  *.** Mark a sing
13b7f 6c 65 20 64 61 74 61 20 70 61 67 65 20 61 73 20  le data page as 
13b80 77 72 69 74 65 61 62 6c 65 2e 20 54 68 65 20 70  writeable. The p
13b81 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69  age is written i
13b82 6e 74 6f 20 74 68 65 20 0a 2a 2a 20 6d 61 69 6e  nto the .** main
13b83 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d   journal or sub-
13b84 6a 6f 75 72 6e 61 6c 20 61 73 20 72 65 71 75 69  journal as requi
13b85 72 65 64 2e 20 49 66 20 74 68 65 20 70 61 67 65  red. If the page
13b86 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
13b87 0a 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 6a  .** one of the j
13b88 6f 75 72 6e 61 6c 73 2c 20 74 68 65 20 63 6f 72  ournals, the cor
13b89 72 65 73 70 6f 6e 64 69 6e 67 20 62 69 74 20 69  responding bit i
13b8a 73 20 73 65 74 20 69 6e 20 74 68 65 20 0a 2a 2a  s set in the .**
13b8b 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61   Pager.pInJourna
13b8c 6c 20 62 69 74 76 65 63 20 61 6e 64 20 74 68 65  l bitvec and the
13b8d 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e   PagerSavepoint.
13b8e 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 62 69 74  pInSavepoint bit
13b8f 76 65 63 73 0a 2a 2a 20 6f 66 20 61 6e 79 20 6f  vecs.** of any o
13b90 70 65 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 61  pen savepoints a
13b91 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a  s appropriate..*
13b92 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
13b93 65 72 5f 77 72 69 74 65 28 50 67 48 64 72 20 2a  er_write(PgHdr *
13b94 70 50 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70 44  pPg){.  void *pD
13b95 61 74 61 20 3d 20 70 50 67 2d 3e 70 44 61 74 61  ata = pPg->pData
13b96 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  ;.  Pager *pPage
13b97 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b  r = pPg->pPager;
13b98 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
13b99 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 54 68 69  TE_OK;..  /* Thi
13b9a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74  s routine is not
13b9b 20 63 61 6c 6c 65 64 20 75 6e 6c 65 73 73 20 61   called unless a
13b9c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73   transaction has
13b9d 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 20 20   already been.  
13b9e 2a 2a 20 73 74 61 72 74 65 64 2e 0a 20 20 2a 2f  ** started..  */
13b9f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13ba0 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
13ba1 52 45 53 45 52 56 45 44 20 29 3b 0a 0a 20 20 2f  RESERVED );..  /
13ba2 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 68 61  * If an error ha
13ba3 73 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c  s been previousl
13ba4 79 20 64 65 74 65 63 74 65 64 2c 20 77 65 20 73  y detected, we s
13ba5 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 20 20 2a  hould not be.  *
13ba6 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  * calling this r
13ba7 6f 75 74 69 6e 65 2e 20 20 52 65 70 65 61 74 20  outine.  Repeat 
13ba8 74 68 65 20 65 72 72 6f 72 20 66 6f 72 20 72 6f  the error for ro
13ba9 62 75 73 74 6e 65 73 73 2e 0a 20 20 2a 2f 0a 20  bustness..  */. 
13baa 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65   if( NEVER(pPage
13bab 72 2d 3e 65 72 72 43 6f 64 65 29 20 29 20 20 72  r->errCode) )  r
13bac 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72  eturn pPager->er
13bad 72 43 6f 64 65 3b 0a 0a 20 20 2f 2a 20 48 69 67  rCode;..  /* Hig
13bae 68 65 72 2d 6c 65 76 65 6c 20 72 6f 75 74 69 6e  her-level routin
13baf 65 73 20 6e 65 76 65 72 20 63 61 6c 6c 20 74 68  es never call th
13bb0 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 64  is function if d
13bb1 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 0a 20  atabase is not. 
13bb2 20 2a 2a 20 77 72 69 74 61 62 6c 65 2e 20 20 42   ** writable.  B
13bb3 75 74 20 63 68 65 63 6b 20 61 6e 79 77 61 79 2c  ut check anyway,
13bb4 20 6a 75 73 74 20 66 6f 72 20 72 6f 62 75 73 74   just for robust
13bb5 6e 65 73 73 2e 20 2a 2f 0a 20 20 69 66 28 20 4e  ness. */.  if( N
13bb6 45 56 45 52 28 70 50 61 67 65 72 2d 3e 72 65 61  EVER(pPager->rea
13bb7 64 4f 6e 6c 79 29 20 29 20 72 65 74 75 72 6e 20  dOnly) ) return 
13bb8 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 0a 20 20  SQLITE_PERM;..  
13bb9 61 73 73 65 72 74 28 20 21 70 50 61 67 65 72 2d  assert( !pPager-
13bba 3e 73 65 74 4d 61 73 74 65 72 20 29 3b 0a 0a 20  >setMaster );.. 
13bbb 20 43 48 45 43 4b 5f 50 41 47 45 28 70 50 67 29   CHECK_PAGE(pPg)
13bbc 3b 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65  ;..  /* Mark the
13bbd 20 70 61 67 65 20 61 73 20 64 69 72 74 79 2e 20   page as dirty. 
13bbe 20 49 66 20 74 68 65 20 70 61 67 65 20 68 61 73   If the page has
13bbf 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72   already been wr
13bc0 69 74 74 65 6e 0a 20 20 2a 2a 20 74 6f 20 74 68  itten.  ** to th
13bc1 65 20 6a 6f 75 72 6e 61 6c 20 74 68 65 6e 20 77  e journal then w
13bc2 65 20 63 61 6e 20 72 65 74 75 72 6e 20 72 69 67  e can return rig
13bc3 68 74 20 61 77 61 79 2e 0a 20 20 2a 2f 0a 20 20  ht away..  */.  
13bc4 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
13bc5 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 69  eDirty(pPg);.  i
13bc6 66 28 20 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c  f( pageInJournal
13bc7 28 70 50 67 29 20 26 26 20 21 73 75 62 6a 52 65  (pPg) && !subjRe
13bc8 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 20  quiresPage(pPg) 
13bc9 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64  ){.    pPager->d
13bca 62 4d 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a 20  bModified = 1;. 
13bcb 20 7d 65 6c 73 65 7b 0a 0a 20 20 20 20 2f 2a 20   }else{..    /* 
13bcc 49 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66  If we get this f
13bcd 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ar, it means tha
13bce 74 20 74 68 65 20 70 61 67 65 20 6e 65 65 64 73  t the page needs
13bcf 20 74 6f 20 62 65 0a 20 20 20 20 2a 2a 20 77 72   to be.    ** wr
13bd0 69 74 74 65 6e 20 74 6f 20 74 68 65 20 74 72 61  itten to the tra
13bd1 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c  nsaction journal
13bd2 20 6f 72 20 74 68 65 20 63 6b 65 63 6b 70 6f 69   or the ckeckpoi
13bd3 6e 74 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a  nt journal.    *
13bd4 2a 20 6f 72 20 62 6f 74 68 2e 0a 20 20 20 20 2a  * or both..    *
13bd5 2a 0a 20 20 20 20 2a 2a 20 48 69 67 68 65 72 20  *.    ** Higher 
13bd6 6c 65 76 65 6c 20 72 6f 75 74 69 6e 65 73 20 73  level routines s
13bd7 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61  hould have alrea
13bd8 64 79 20 73 74 61 72 74 65 64 20 61 20 74 72 61  dy started a tra
13bd9 6e 73 61 63 74 69 6f 6e 2c 0a 20 20 20 20 2a 2a  nsaction,.    **
13bda 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68 65   which means the
13bdb 79 20 68 61 76 65 20 61 63 71 75 69 72 65 64 20  y have acquired 
13bdc 74 68 65 20 6e 65 63 65 73 73 61 72 79 20 6c 6f  the necessary lo
13bdd 63 6b 73 20 61 6e 64 20 6f 70 65 6e 65 64 0a 20  cks and opened. 
13bde 20 20 20 2a 2a 20 61 20 72 6f 6c 6c 62 61 63 6b     ** a rollback
13bdf 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 6f 75 62 6c   journal.  Doubl
13be0 65 2d 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 73  e-check to makes
13be1 20 73 75 72 65 20 74 68 69 73 20 69 73 20 74 68   sure this is th
13be2 65 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20  e case..    */. 
13be3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
13be4 61 67 65 72 42 65 67 69 6e 28 70 50 61 67 65 72  agerBegin(pPager
13be5 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 73 75 62  , 0, pPager->sub
13be6 6a 49 6e 4d 65 6d 6f 72 79 29 3b 0a 20 20 20 20  jInMemory);.    
13be7 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51  if( NEVER(rc!=SQ
13be8 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20  LITE_OK) ){.    
13be9 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
13bea 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73 4f 70   }.    if( !isOp
13beb 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20  en(pPager->jfd) 
13bec 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
13bed 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f  alMode!=PAGER_JO
13bee 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b  URNALMODE_OFF ){
13bef 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
13bf0 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61  Pager->useJourna
13bf1 6c 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  l );.      rc = 
13bf2 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e  pager_open_journ
13bf3 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  al(pPager);.    
13bf4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13bf5 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
13bf6 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
13bf7 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d 20  r->dbModified = 
13bf8 31 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 54 68 65  1;.  .    /* The
13bf9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75   transaction jou
13bfa 72 6e 61 6c 20 6e 6f 77 20 65 78 69 73 74 73 20  rnal now exists 
13bfb 61 6e 64 20 77 65 20 68 61 76 65 20 61 20 52 45  and we have a RE
13bfc 53 45 52 56 45 44 20 6f 72 20 61 6e 0a 20 20 20  SERVED or an.   
13bfd 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   ** EXCLUSIVE lo
13bfe 63 6b 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64  ck on the main d
13bff 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 57  atabase file.  W
13c00 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74  rite the current
13c01 20 70 61 67 65 20 74 6f 0a 20 20 20 20 2a 2a 20   page to.    ** 
13c02 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
13c03 6a 6f 75 72 6e 61 6c 20 69 66 20 69 74 20 69 73  journal if it is
13c04 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61   not there alrea
13c05 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  dy..    */.    i
13c06 66 28 20 21 70 61 67 65 49 6e 4a 6f 75 72 6e 61  f( !pageInJourna
13c07 6c 28 70 50 67 29 20 26 26 20 69 73 4f 70 65 6e  l(pPg) && isOpen
13c08 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
13c09 0a 20 20 20 20 20 20 69 66 28 20 70 50 67 2d 3e  .      if( pPg->
13c0a 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64 62  pgno<=pPager->db
13c0b 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20 20  OrigSize ){.    
13c0c 20 20 20 20 75 33 32 20 63 6b 73 75 6d 3b 0a 20      u32 cksum;. 
13c0d 20 20 20 20 20 20 20 63 68 61 72 20 2a 70 44 61         char *pDa
13c0e 74 61 32 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  ta2;..        /*
13c0f 20 57 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72   We should never
13c10 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 6a 6f   write to the jo
13c11 75 72 6e 61 6c 20 66 69 6c 65 20 74 68 65 20 70  urnal file the p
13c12 61 67 65 20 74 68 61 74 0a 20 20 20 20 20 20 20  age that.       
13c13 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68 65   ** contains the
13c14 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 73 2e   database locks.
13c15 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
13c16 61 73 73 65 72 74 20 76 65 72 69 66 69 65 73 0a  assert verifies.
13c17 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20          ** that 
13c18 77 65 20 64 6f 20 6e 6f 74 2e 20 2a 2f 0a 20 20  we do not. */.  
13c19 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
13c1a 67 2d 3e 70 67 6e 6f 21 3d 50 41 47 45 52 5f 4d  g->pgno!=PAGER_M
13c1b 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29  J_PGNO(pPager) )
13c1c 3b 0a 20 20 20 20 20 20 20 20 43 4f 44 45 43 32  ;.        CODEC2
13c1d 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20  (pPager, pData, 
13c1e 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65  pPg->pgno, 7, re
13c1f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
13c20 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20 20 20 20  M, pData2);.    
13c21 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 61 67 65      cksum = page
13c22 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20  r_cksum(pPager, 
13c23 28 75 38 2a 29 70 44 61 74 61 32 29 3b 0a 20 20  (u8*)pData2);.  
13c24 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
13c25 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
13c26 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  fd, pPager->jour
13c27 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70 67 6e  nalOff, pPg->pgn
13c28 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  o);.        if( 
13c29 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
13c2a 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
13c2b 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
13c2c 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44 61 74  Pager->jfd, pDat
13c2d 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  a2, pPager->page
13c2e 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Size,.          
13c2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13c30 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
13c31 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20 20 20  nalOff + 4);.   
13c32 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
13c33 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70 50 61  ournalOff += pPa
13c34 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b 34 3b  ger->pageSize+4;
13c35 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
13c36 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
13c37 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
13c38 20 20 72 63 20 3d 20 77 72 69 74 65 33 32 62 69    rc = write32bi
13c39 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ts(pPager->jfd, 
13c3a 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
13c3b 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 20 20  ff, cksum);.    
13c3c 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
13c3d 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b 0a 20  urnalOff += 4;. 
13c3e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
13c3f 20 49 4f 54 52 41 43 45 28 28 22 4a 4f 55 54 20   IOTRACE(("JOUT 
13c40 25 70 20 25 64 20 25 6c 6c 64 20 25 64 5c 6e 22  %p %d %lld %d\n"
13c41 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70  , pPager, pPg->p
13c42 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20 20 20  gno, .          
13c43 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a         pPager->j
13c44 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61 67 65  ournalOff, pPage
13c45 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b 0a 20  r->pageSize));. 
13c46 20 20 20 20 20 20 20 50 41 47 45 52 5f 49 4e 43         PAGER_INC
13c47 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65 72 5f  R(sqlite3_pager_
13c48 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b 0a 20  writej_count);. 
13c49 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43         PAGERTRAC
13c4a 45 28 28 22 4a 4f 55 52 4e 41 4c 20 25 64 20 70  E(("JOURNAL %d p
13c4b 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e 63 3d  age %d needSync=
13c4c 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22  %d hash(%08x)\n"
13c4d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 50  ,.             P
13c4e 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
13c4f 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20  pPg->pgno, .    
13c50 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e           ((pPg->
13c51 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
13c52 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70 61 67  _SYNC)?1:0), pag
13c53 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29  er_pagehash(pPg)
13c54 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  ));..        /* 
13c55 45 76 65 6e 20 69 66 20 61 6e 20 49 4f 20 6f 72  Even if an IO or
13c56 20 64 69 73 6b 66 75 6c 6c 20 65 72 72 6f 72 20   diskfull error 
13c57 6f 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 6a  occurred while j
13c58 6f 75 72 6e 61 6c 6c 69 6e 67 20 74 68 65 0a 20  ournalling the. 
13c59 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69         ** page i
13c5a 6e 20 74 68 65 20 62 6c 6f 63 6b 20 61 62 6f 76  n the block abov
13c5b 65 2c 20 73 65 74 20 74 68 65 20 6e 65 65 64 2d  e, set the need-
13c5c 73 79 6e 63 20 66 6c 61 67 20 66 6f 72 20 74 68  sync flag for th
13c5d 65 20 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20  e page..        
13c5e 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 68  ** Otherwise, wh
13c5f 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  en the transacti
13c60 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63  on is rolled bac
13c61 6b 2c 20 74 68 65 20 6c 6f 67 69 63 20 69 6e 0a  k, the logic in.
13c62 20 20 20 20 20 20 20 20 2a 2a 20 70 6c 61 79 62          ** playb
13c63 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 77  ack_one_page() w
13c64 69 6c 6c 20 74 68 69 6e 6b 20 74 68 61 74 20 74  ill think that t
13c65 68 65 20 70 61 67 65 20 6e 65 65 64 73 20 74 6f  he page needs to
13c66 20 62 65 20 72 65 73 74 6f 72 65 64 0a 20 20 20   be restored.   
13c67 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 64       ** in the d
13c68 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 41 6e  atabase file. An
13c69 64 20 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72  d if an IO error
13c6a 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f   occurs while do
13c6b 69 6e 67 20 73 6f 2c 0a 20 20 20 20 20 20 20 20  ing so,.        
13c6c 2a 2a 20 74 68 65 6e 20 63 6f 72 72 75 70 74 69  ** then corrupti
13c6d 6f 6e 20 6d 61 79 20 66 6f 6c 6c 6f 77 2e 0a 20  on may follow.. 
13c6e 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
13c6f 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e    if( !pPager->n
13c70 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20  oSync ){.       
13c71 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d     pPg->flags |=
13c72 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
13c73 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67  ;.          pPag
13c74 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31  er->needSync = 1
13c75 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20  ;.        }..   
13c76 20 20 20 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72       /* An error
13c77 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 77 72   has occurred wr
13c78 69 74 69 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75  iting to the jou
13c79 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 65 20 0a  rnal file. The .
13c7a 20 20 20 20 20 20 20 20 2a 2a 20 74 72 61 6e 73          ** trans
13c7b 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 72  action will be r
13c7c 6f 6c 6c 65 64 20 62 61 63 6b 20 62 79 20 74 68  olled back by th
13c7d 65 20 6c 61 79 65 72 20 61 62 6f 76 65 2e 0a 20  e layer above.. 
13c7e 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
13c7f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13c80 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
13c81 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
13c82 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 70      }..        p
13c83 50 61 67 65 72 2d 3e 6e 52 65 63 2b 2b 3b 0a 20  Pager->nRec++;. 
13c84 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
13c85 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
13c86 6c 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  l!=0 );.        
13c87 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  rc = sqlite3Bitv
13c88 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70 49  ecSet(pPager->pI
13c89 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70  nJournal, pPg->p
13c8a 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65  gno);.        te
13c8b 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49  stcase( rc==SQLI
13c8c 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20  TE_NOMEM );.    
13c8d 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
13c8e 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d  SQLITE_OK || rc=
13c8f 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
13c90 0a 20 20 20 20 20 20 20 20 72 63 20 7c 3d 20 61  .        rc |= a
13c91 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74  ddToSavepointBit
13c92 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 67  vecs(pPager, pPg
13c93 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  ->pgno);.       
13c94 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13c95 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
13c96 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
13c97 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20  TE_NOMEM );.    
13c98 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
13c99 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
13c9a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
13c9b 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75  if( !pPager->jou
13c9c 72 6e 61 6c 53 74 61 72 74 65 64 20 26 26 20 21  rnalStarted && !
13c9d 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29  pPager->noSync )
13c9e 7b 0a 20 20 20 20 20 20 20 20 20 20 70 50 67 2d  {.          pPg-
13c9f 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f  >flags |= PGHDR_
13ca0 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 20  NEED_SYNC;.     
13ca1 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65       pPager->nee
13ca2 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20  dSync = 1;.     
13ca3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 50 41 47     }.        PAG
13ca4 45 52 54 52 41 43 45 28 28 22 41 50 50 45 4e 44  ERTRACE(("APPEND
13ca5 20 25 64 20 70 61 67 65 20 25 64 20 6e 65 65 64   %d page %d need
13ca6 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20  Sync=%d\n",.    
13ca7 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45              PAGE
13ca8 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67  RID(pPager), pPg
13ca9 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20  ->pgno,.        
13caa 20 20 20 20 20 20 20 28 28 70 50 67 2d 3e 66 6c         ((pPg->fl
13cab 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
13cac 59 4e 43 29 3f 31 3a 30 29 29 29 3b 0a 20 20 20  YNC)?1:0)));.   
13cad 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
13cae 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74    /* If the stat
13caf 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 73  ement journal is
13cb0 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61   open and the pa
13cb1 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 69 74 2c  ge is not in it,
13cb2 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 77 72 69  .    ** then wri
13cb3 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  te the current p
13cb4 61 67 65 20 74 6f 20 74 68 65 20 73 74 61 74 65  age to the state
13cb5 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e  ment journal.  N
13cb6 6f 74 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  ote that.    ** 
13cb7 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  the statement jo
13cb8 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64 69 66  urnal format dif
13cb9 66 65 72 73 20 66 72 6f 6d 20 74 68 65 20 73 74  fers from the st
13cba 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c 20 66  andard journal f
13cbb 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69 6e 20  ormat.    ** in 
13cbc 74 68 61 74 20 69 74 20 6f 6d 69 74 73 20 74 68  that it omits th
13cbd 65 20 63 68 65 63 6b 73 75 6d 73 20 61 6e 64 20  e checksums and 
13cbe 74 68 65 20 68 65 61 64 65 72 2e 0a 20 20 20 20  the header..    
13cbf 2a 2f 0a 20 20 20 20 69 66 28 20 73 75 62 6a 52  */.    if( subjR
13cc0 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29  equiresPage(pPg)
13cc1 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
13cc2 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70 50  ubjournalPage(pP
13cc3 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  g);.    }.  }.. 
13cc4 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64   /* Update the d
13cc5 61 74 61 62 61 73 65 20 73 69 7a 65 20 61 6e 64  atabase size and
13cc6 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20   return..  */.  
13cc7 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
13cc8 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41  state>=PAGER_SHA
13cc9 52 45 44 20 29 3b 0a 20 20 69 66 28 20 70 50 61  RED );.  if( pPa
13cca 67 65 72 2d 3e 64 62 53 69 7a 65 3c 70 50 67 2d  ger->dbSize<pPg-
13ccb 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 70 50 61  >pgno ){.    pPa
13ccc 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50  ger->dbSize = pP
13ccd 67 2d 3e 70 67 6e 6f 3b 0a 20 20 7d 0a 20 20 72  g->pgno;.  }.  r
13cce 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
13ccf 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74 61 20 70  ** Mark a data p
13cd0 61 67 65 20 61 73 20 77 72 69 74 65 61 62 6c 65  age as writeable
13cd1 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  . This routine m
13cd2 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65  ust be called be
13cd3 66 6f 72 65 20 0a 2a 2a 20 6d 61 6b 69 6e 67 20  fore .** making 
13cd4 63 68 61 6e 67 65 73 20 74 6f 20 61 20 70 61 67  changes to a pag
13cd5 65 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75  e. The caller mu
13cd6 73 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 74  st check the ret
13cd7 75 72 6e 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66  urn value .** of
13cd8 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   this function a
13cd9 6e 64 20 62 65 20 63 61 72 65 66 75 6c 20 6e 6f  nd be careful no
13cda 74 20 74 6f 20 63 68 61 6e 67 65 20 61 6e 79 20  t to change any 
13cdb 70 61 67 65 20 64 61 74 61 20 75 6e 6c 65 73 73  page data unless
13cdc 20 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e   .** this routin
13cdd 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  e returns SQLITE
13cde 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  _OK..**.** The d
13cdf 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
13ce0 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
13ce1 61 6e 64 20 70 61 67 65 72 5f 77 72 69 74 65 28  and pager_write(
13ce2 29 20 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a  ) is that this.*
13ce3 2a 20 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f 20  * function also 
13ce4 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 20 73  deals with the s
13ce5 70 65 63 69 61 6c 20 63 61 73 65 20 77 68 65 72  pecial case wher
13ce6 65 20 32 20 6f 72 20 6d 6f 72 65 20 70 61 67 65  e 2 or more page
13ce7 73 0a 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73 69  s.** fit on a si
13ce8 6e 67 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72  ngle disk sector
13ce9 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61  . In this case a
13cea 6c 6c 20 63 6f 2d 72 65 73 69 64 65 6e 74 20 70  ll co-resident p
13ceb 61 67 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61 76  ages.** must hav
13cec 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  e been written t
13ced 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
13cee 6c 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  le before return
13cef 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ing..**.** If an
13cf0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 53   error occurs, S
13cf1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f 72 20 61  QLITE_NOMEM or a
13cf2 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  n IO error code 
13cf3 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61  is returned.** a
13cf4 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 4f  s appropriate. O
13cf5 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45  therwise, SQLITE
13cf6 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  _OK..*/.SQLITE_P
13cf7 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13cf8 65 33 50 61 67 65 72 57 72 69 74 65 28 44 62 50  e3PagerWrite(DbP
13cf9 61 67 65 20 2a 70 44 62 50 61 67 65 29 7b 0a 20  age *pDbPage){. 
13cfa 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
13cfb 5f 4f 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a 70  _OK;..  PgHdr *p
13cfc 50 67 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20  Pg = pDbPage;.  
13cfd 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
13cfe 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 50  pPg->pPager;.  P
13cff 67 6e 6f 20 6e 50 61 67 65 50 65 72 53 65 63 74  gno nPagePerSect
13d00 6f 72 20 3d 20 28 70 50 61 67 65 72 2d 3e 73 65  or = (pPager->se
13d01 63 74 6f 72 53 69 7a 65 2f 70 50 61 67 65 72 2d  ctorSize/pPager-
13d02 3e 70 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 69  >pageSize);..  i
13d03 66 28 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f  f( nPagePerSecto
13d04 72 3e 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20  r>1 ){.    Pgno 
13d05 6e 50 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 20  nPageCount;     
13d06 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
13d07 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
13d08 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
13d09 2f 0a 20 20 20 20 50 67 6e 6f 20 70 67 31 3b 20  /.    Pgno pg1; 
13d0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13d0b 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66  /* First page of
13d0c 20 74 68 65 20 73 65 63 74 6f 72 20 70 50 67 20   the sector pPg 
13d0d 69 73 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 2a  is located on. *
13d0e 2f 0a 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b  /.    int nPage;
13d0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13d10 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
13d11 65 73 20 73 74 61 72 74 69 6e 67 20 61 74 20 70  es starting at p
13d12 67 31 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f  g1 to journal */
13d13 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20 20  .    int ii;    
13d14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13d15 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
13d16 2f 0a 20 20 20 20 69 6e 74 20 6e 65 65 64 53 79  /.    int needSy
13d17 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  nc = 0;         
13d18 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 70  /* True if any p
13d19 61 67 65 20 68 61 73 20 50 47 48 44 52 5f 4e 45  age has PGHDR_NE
13d1a 45 44 5f 53 59 4e 43 20 2a 2f 0a 0a 20 20 20 20  ED_SYNC */..    
13d1b 2f 2a 20 53 65 74 20 74 68 65 20 64 6f 4e 6f 74  /* Set the doNot
13d1c 53 79 6e 63 20 66 6c 61 67 20 74 6f 20 31 2e 20  Sync flag to 1. 
13d1d 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20  This is because 
13d1e 77 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77 20  we cannot allow 
13d1f 61 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a  a journal.    **
13d20 20 68 65 61 64 65 72 20 74 6f 20 62 65 20 77 72   header to be wr
13d21 69 74 74 65 6e 20 62 65 74 77 65 65 6e 20 74 68  itten between th
13d22 65 20 70 61 67 65 73 20 6a 6f 75 72 6e 61 6c 65  e pages journale
13d23 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
13d24 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  on..    */.    a
13d25 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 29 3b  ssert( !MEMDB );
13d26 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
13d27 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 3d 3d  ger->doNotSync==
13d28 30 20 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  0 );.    pPager-
13d29 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20 31 3b 0a  >doNotSync = 1;.
13d2a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 74 72 69  .    /* This tri
13d2b 63 6b 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  ck assumes that 
13d2c 62 6f 74 68 20 74 68 65 20 70 61 67 65 2d 73 69  both the page-si
13d2d 7a 65 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69  ze and sector-si
13d2e 7a 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 61 6e  ze are.    ** an
13d2f 20 69 6e 74 65 67 65 72 20 70 6f 77 65 72 20 6f   integer power o
13d30 66 20 32 2e 20 49 74 20 73 65 74 73 20 76 61 72  f 2. It sets var
13d31 69 61 62 6c 65 20 70 67 31 20 74 6f 20 74 68 65  iable pg1 to the
13d32 20 69 64 65 6e 74 69 66 69 65 72 0a 20 20 20 20   identifier.    
13d33 2a 2a 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  ** of the first 
13d34 70 61 67 65 20 6f 66 20 74 68 65 20 73 65 63 74  page of the sect
13d35 6f 72 20 70 50 67 20 69 73 20 6c 6f 63 61 74 65  or pPg is locate
13d36 64 20 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  d on..    */.   
13d37 20 70 67 31 20 3d 20 28 28 70 50 67 2d 3e 70 67   pg1 = ((pPg->pg
13d38 6e 6f 2d 31 29 20 26 20 7e 28 6e 50 61 67 65 50  no-1) & ~(nPageP
13d39 65 72 53 65 63 74 6f 72 2d 31 29 29 20 2b 20 31  erSector-1)) + 1
13d3a 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  ;..    sqlite3Pa
13d3b 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61  gerPagecount(pPa
13d3c 67 65 72 2c 20 28 69 6e 74 20 2a 29 26 6e 50 61  ger, (int *)&nPa
13d3d 67 65 43 6f 75 6e 74 29 3b 0a 20 20 20 20 69 66  geCount);.    if
13d3e 28 20 70 50 67 2d 3e 70 67 6e 6f 3e 6e 50 61 67  ( pPg->pgno>nPag
13d3f 65 43 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20  eCount ){.      
13d40 6e 50 61 67 65 20 3d 20 28 70 50 67 2d 3e 70 67  nPage = (pPg->pg
13d41 6e 6f 20 2d 20 70 67 31 29 2b 31 3b 0a 20 20 20  no - pg1)+1;.   
13d42 20 7d 65 6c 73 65 20 69 66 28 20 28 70 67 31 2b   }else if( (pg1+
13d43 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d 31  nPagePerSector-1
13d44 29 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a  )>nPageCount ){.
13d45 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50        nPage = nP
13d46 61 67 65 43 6f 75 6e 74 2b 31 2d 70 67 31 3b 0a  ageCount+1-pg1;.
13d47 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13d48 20 6e 50 61 67 65 20 3d 20 6e 50 61 67 65 50 65   nPage = nPagePe
13d49 72 53 65 63 74 6f 72 3b 0a 20 20 20 20 7d 0a 20  rSector;.    }. 
13d4a 20 20 20 61 73 73 65 72 74 28 6e 50 61 67 65 3e     assert(nPage>
13d4b 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 70  0);.    assert(p
13d4c 67 31 3c 3d 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  g1<=pPg->pgno);.
13d4d 20 20 20 20 61 73 73 65 72 74 28 28 70 67 31 2b      assert((pg1+
13d4e 6e 50 61 67 65 29 3e 70 50 67 2d 3e 70 67 6e 6f  nPage)>pPg->pgno
13d4f 29 3b 0a 0a 20 20 20 20 66 6f 72 28 69 69 3d 30  );..    for(ii=0
13d50 3b 20 69 69 3c 6e 50 61 67 65 20 26 26 20 72 63  ; ii<nPage && rc
13d51 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69 69 2b  ==SQLITE_OK; ii+
13d52 2b 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 70  +){.      Pgno p
13d53 67 20 3d 20 70 67 31 2b 69 69 3b 0a 20 20 20 20  g = pg1+ii;.    
13d54 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b 0a    PgHdr *pPage;.
13d55 20 20 20 20 20 20 69 66 28 20 70 67 3d 3d 70 50        if( pg==pP
13d56 67 2d 3e 70 67 6e 6f 20 7c 7c 20 21 73 71 6c 69  g->pgno || !sqli
13d57 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 50  te3BitvecTest(pP
13d58 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c  ager->pInJournal
13d59 2c 20 70 67 29 20 29 7b 0a 20 20 20 20 20 20 20  , pg) ){.       
13d5a 20 69 66 28 20 70 67 21 3d 50 41 47 45 52 5f 4d   if( pg!=PAGER_M
13d5b 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29  J_PGNO(pPager) )
13d5c 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
13d5d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
13d5e 28 70 50 61 67 65 72 2c 20 70 67 2c 20 26 70 50  (pPager, pg, &pP
13d5f 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
13d60 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
13d61 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
13d62 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74   rc = pager_writ
13d63 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
13d64 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d        if( pPage-
13d65 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
13d66 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20 20  D_SYNC ){.      
13d67 20 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63          needSync
13d68 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
13d69 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65      assert(pPage
13d6a 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 20  r->needSync);.  
13d6b 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
13d6c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
13d6d 61 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29  agerUnref(pPage)
13d6e 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
13d6f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
13d70 6c 73 65 20 69 66 28 20 28 70 50 61 67 65 20 3d  lse if( (pPage =
13d71 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50   pager_lookup(pP
13d72 61 67 65 72 2c 20 70 67 29 29 21 3d 30 20 29 7b  ager, pg))!=0 ){
13d73 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
13d74 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  ge->flags&PGHDR_
13d75 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20  NEED_SYNC ){.   
13d76 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20         needSync 
13d77 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
13d78 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
13d79 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b  gerUnref(pPage);
13d7a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
13d7b 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 47      /* If the PG
13d7c 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c  HDR_NEED_SYNC fl
13d7d 61 67 20 69 73 20 73 65 74 20 66 6f 72 20 61 6e  ag is set for an
13d7e 79 20 6f 66 20 74 68 65 20 6e 50 61 67 65 20 70  y of the nPage p
13d7f 61 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 74 61  ages .    ** sta
13d80 72 74 69 6e 67 20 61 74 20 70 67 31 2c 20 74 68  rting at pg1, th
13d81 65 6e 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62  en it needs to b
13d82 65 20 73 65 74 20 66 6f 72 20 61 6c 6c 20 6f 66  e set for all of
13d83 20 74 68 65 6d 2e 20 42 65 63 61 75 73 65 0a 20   them. Because. 
13d84 20 20 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f     ** writing to
13d85 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20 6e 50   any of these nP
13d86 61 67 65 20 70 61 67 65 73 20 6d 61 79 20 64 61  age pages may da
13d87 6d 61 67 65 20 74 68 65 20 6f 74 68 65 72 73 2c  mage the others,
13d88 20 74 68 65 0a 20 20 20 20 2a 2a 20 6a 6f 75 72   the.    ** jour
13d89 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 63 6f  nal file must co
13d8a 6e 74 61 69 6e 20 73 79 6e 63 28 29 65 64 20 63  ntain sync()ed c
13d8b 6f 70 69 65 73 20 6f 66 20 61 6c 6c 20 6f 66 20  opies of all of 
13d8c 74 68 65 6d 0a 20 20 20 20 2a 2a 20 62 65 66 6f  them.    ** befo
13d8d 72 65 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 63  re any of them c
13d8e 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75  an be written ou
13d8f 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
13d90 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20  e file..    */. 
13d91 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
13d92 45 5f 4f 4b 20 26 26 20 6e 65 65 64 53 79 6e 63  E_OK && needSync
13d93 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
13d94 28 20 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67  ( !MEMDB && pPag
13d95 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 29 3b  er->noSync==0 );
13d96 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b  .      for(ii=0;
13d97 20 69 69 3c 6e 50 61 67 65 3b 20 69 69 2b 2b 29   ii<nPage; ii++)
13d98 7b 0a 20 20 20 20 20 20 20 20 50 67 48 64 72 20  {.        PgHdr 
13d99 2a 70 50 61 67 65 20 3d 20 70 61 67 65 72 5f 6c  *pPage = pager_l
13d9a 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67  ookup(pPager, pg
13d9b 31 2b 69 69 29 3b 0a 20 20 20 20 20 20 20 20 69  1+ii);.        i
13d9c 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20 20  f( pPage ){.    
13d9d 20 20 20 20 20 20 70 50 61 67 65 2d 3e 66 6c 61        pPage->fla
13d9e 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44  gs |= PGHDR_NEED
13d9f 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20  _SYNC;.         
13da0 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
13da1 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ef(pPage);.     
13da2 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
13da3 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65 72     assert(pPager
13da4 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 20 20  ->needSync);.   
13da5 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20   }..    assert( 
13da6 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e  pPager->doNotSyn
13da7 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 50 61 67  c==1 );.    pPag
13da8 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d 20  er->doNotSync = 
13da9 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
13daa 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65  rc = pager_write
13dab 28 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 20  (pDbPage);.  }. 
13dac 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
13dad 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
13dae 20 69 66 20 74 68 65 20 70 61 67 65 20 67 69 76   if the page giv
13daf 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65  en in the argume
13db0 6e 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c  nt was previousl
13db1 79 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20 73  y passed.** to s
13db2 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
13db3 28 29 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  ().  In other wo
13db4 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 45  rds, return TRUE
13db5 20 69 66 20 69 74 20 69 73 20 6f 6b 0a 2a 2a 20   if it is ok.** 
13db6 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 63 6f  to change the co
13db7 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67  ntent of the pag
13db8 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44  e..*/.#ifndef ND
13db9 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
13dba 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
13dbb 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
13dbc 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20  DbPage *pPg){.  
13dbd 72 65 74 75 72 6e 20 70 50 67 2d 3e 66 6c 61 67  return pPg->flag
13dbe 73 26 50 47 48 44 52 5f 44 49 52 54 59 3b 0a 7d  s&PGHDR_DIRTY;.}
13dbf 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
13dc0 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44   SQLITE_SECURE_D
13dc1 45 4c 45 54 45 0a 2f 2a 0a 2a 2a 20 41 20 63 61  ELETE./*.** A ca
13dc2 6c 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ll to this routi
13dc3 6e 65 20 74 65 6c 6c 73 20 74 68 65 20 70 61 67  ne tells the pag
13dc4 65 72 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f  er that it is no
13dc5 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 0a 2a  t necessary to.*
13dc6 2a 20 77 72 69 74 65 20 74 68 65 20 69 6e 66 6f  * write the info
13dc7 72 6d 61 74 69 6f 6e 20 6f 6e 20 70 61 67 65 20  rmation on page 
13dc8 70 50 67 20 62 61 63 6b 20 74 6f 20 74 68 65 20  pPg back to the 
13dc9 64 69 73 6b 2c 20 65 76 65 6e 20 74 68 6f 75 67  disk, even thoug
13dca 68 0a 2a 2a 20 74 68 61 74 20 70 61 67 65 20 6d  h.** that page m
13dcb 69 67 68 74 20 62 65 20 6d 61 72 6b 65 64 20 61  ight be marked a
13dcc 73 20 64 69 72 74 79 2e 20 20 54 68 69 73 20 68  s dirty.  This h
13dcd 61 70 70 65 6e 73 2c 20 66 6f 72 20 65 78 61 6d  appens, for exam
13dce 70 6c 65 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65  ple, when.** the
13dcf 20 70 61 67 65 20 68 61 73 20 62 65 65 6e 20 61   page has been a
13dd0 64 64 65 64 20 61 73 20 61 20 6c 65 61 66 20 6f  dded as a leaf o
13dd1 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 61  f the freelist a
13dd2 6e 64 20 73 6f 20 69 74 73 0a 2a 2a 20 63 6f 6e  nd so its.** con
13dd3 74 65 6e 74 20 6e 6f 20 6c 6f 6e 67 65 72 20 6d  tent no longer m
13dd4 61 74 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  atters..**.** Th
13dd5 65 20 6f 76 65 72 6c 79 69 6e 67 20 73 6f 66 74  e overlying soft
13dd6 77 61 72 65 20 6c 61 79 65 72 20 63 61 6c 6c 73  ware layer calls
13dd7 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68   this routine wh
13dd8 65 6e 20 61 6c 6c 20 6f 66 20 74 68 65 20 64 61  en all of the da
13dd9 74 61 0a 2a 2a 20 6f 6e 20 74 68 65 20 67 69 76  ta.** on the giv
13dda 65 6e 20 70 61 67 65 20 69 73 20 75 6e 75 73 65  en page is unuse
13ddb 64 2e 20 54 68 65 20 70 61 67 65 72 20 6d 61 72  d. The pager mar
13ddc 6b 73 20 74 68 65 20 70 61 67 65 20 61 73 20 63  ks the page as c
13ddd 6c 65 61 6e 20 73 6f 0a 2a 2a 20 74 68 61 74 20  lean so.** that 
13dde 69 74 20 64 6f 65 73 20 6e 6f 74 20 67 65 74 20  it does not get 
13ddf 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 2e  written to disk.
13de0 0a 2a 2a 0a 2a 2a 20 54 65 73 74 73 20 73 68 6f  .**.** Tests sho
13de1 77 20 74 68 61 74 20 74 68 69 73 20 6f 70 74 69  w that this opti
13de2 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 71 75 61  mization can qua
13de3 64 72 75 70 6c 65 20 74 68 65 20 73 70 65 65 64  druple the speed
13de4 20 6f 66 20 6c 61 72 67 65 20 0a 2a 2a 20 44 45   of large .** DE
13de5 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 73 2e  LETE operations.
13de6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13de7 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
13de8 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 50 67  agerDontWrite(Pg
13de9 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67  Hdr *pPg){.  Pag
13dea 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67  er *pPager = pPg
13deb 2d 3e 70 50 61 67 65 72 3b 0a 20 20 69 66 28 20  ->pPager;.  if( 
13dec 28 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44  (pPg->flags&PGHD
13ded 52 5f 44 49 52 54 59 29 20 26 26 20 70 50 61 67  R_DIRTY) && pPag
13dee 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3d 3d  er->nSavepoint==
13def 30 20 29 7b 0a 20 20 20 20 50 41 47 45 52 54 52  0 ){.    PAGERTR
13df0 41 43 45 28 28 22 44 4f 4e 54 5f 57 52 49 54 45  ACE(("DONT_WRITE
13df1 20 70 61 67 65 20 25 64 20 6f 66 20 25 64 5c 6e   page %d of %d\n
13df2 22 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41  ", pPg->pgno, PA
13df3 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b  GERID(pPager)));
13df4 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 43  .    IOTRACE(("C
13df5 4c 45 41 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70  LEAN %p %d\n", p
13df6 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
13df7 29 29 0a 20 20 20 20 70 50 67 2d 3e 66 6c 61 67  )).    pPg->flag
13df8 73 20 7c 3d 20 50 47 48 44 52 5f 44 4f 4e 54 5f  s |= PGHDR_DONT_
13df9 57 52 49 54 45 3b 0a 23 69 66 64 65 66 20 53 51  WRITE;.#ifdef SQ
13dfa 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53  LITE_CHECK_PAGES
13dfb 0a 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61  .    pPg->pageHa
13dfc 73 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68  sh = pager_pageh
13dfd 61 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66  ash(pPg);.#endif
13dfe 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
13dff 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
13e00 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 29 20  _SECURE_DELETE) 
13e01 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  */../*.** This r
13e02 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
13e03 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68   to increment th
13e04 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64  e value of the d
13e05 61 74 61 62 61 73 65 20 66 69 6c 65 20 0a 2a 2a  atabase file .**
13e06 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2c   change-counter,
13e07 20 73 74 6f 72 65 64 20 61 73 20 61 20 34 2d 62   stored as a 4-b
13e08 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20 69  yte big-endian i
13e09 6e 74 65 67 65 72 20 73 74 61 72 74 69 6e 67 20  nteger starting 
13e0a 61 74 20 0a 2a 2a 20 62 79 74 65 20 6f 66 66 73  at .** byte offs
13e0b 65 74 20 32 34 20 6f 66 20 74 68 65 20 70 61 67  et 24 of the pag
13e0c 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49  er file..**.** I
13e0d 66 20 74 68 65 20 69 73 44 69 72 65 63 74 4d 6f  f the isDirectMo
13e0e 64 65 20 66 6c 61 67 20 69 73 20 7a 65 72 6f 2c  de flag is zero,
13e0f 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 64 6f   then this is do
13e10 6e 65 20 62 79 20 63 61 6c 6c 69 6e 67 20 0a 2a  ne by calling .*
13e11 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  * sqlite3PagerWr
13e12 69 74 65 28 29 20 6f 6e 20 70 61 67 65 20 31 2c  ite() on page 1,
13e13 20 74 68 65 6e 20 6d 6f 64 69 66 79 69 6e 67 20   then modifying 
13e14 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
13e15 74 68 65 0a 2a 2a 20 70 61 67 65 20 64 61 74 61  the.** page data
13e16 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
13e17 68 65 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20  he file will be 
13e18 75 70 64 61 74 65 64 20 77 68 65 6e 20 74 68 65  updated when the
13e19 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e   current.** tran
13e1a 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69  saction is commi
13e1b 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tted..**.** The 
13e1c 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66 6c 61  isDirectMode fla
13e1d 67 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6e 6f  g may only be no
13e1e 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20 6c 69  n-zero if the li
13e1f 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70 69 6c  brary was compil
13e20 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 53  ed.** with the S
13e21 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
13e22 4d 49 43 5f 57 52 49 54 45 20 6d 61 63 72 6f 20  MIC_WRITE macro 
13e23 64 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73  defined. In this
13e24 20 63 61 73 65 2c 0a 2a 2a 20 69 66 20 69 73 44   case,.** if isD
13e25 69 72 65 63 74 20 69 73 20 6e 6f 6e 2d 7a 65 72  irect is non-zer
13e26 6f 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61  o, then the data
13e27 62 61 73 65 20 66 69 6c 65 20 69 73 20 75 70 64  base file is upd
13e28 61 74 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a  ated directly.**
13e29 20 62 79 20 77 72 69 74 69 6e 67 20 61 6e 20 75   by writing an u
13e2a 70 64 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f  pdated version o
13e2b 66 20 70 61 67 65 20 31 20 75 73 69 6e 67 20 61  f page 1 using a
13e2c 20 63 61 6c 6c 20 74 6f 20 74 68 65 20 0a 2a 2a   call to the .**
13e2d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
13e2e 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  ) function..*/.s
13e2f 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f  tatic int pager_
13e30 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74  incr_changecount
13e31 65 72 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  er(Pager *pPager
13e32 2c 20 69 6e 74 20 69 73 44 69 72 65 63 74 4d 6f  , int isDirectMo
13e33 64 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  de){.  int rc = 
13e34 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a  SQLITE_OK;..  /*
13e35 20 44 65 63 6c 61 72 65 20 61 6e 64 20 69 6e 69   Declare and ini
13e36 74 69 61 6c 69 7a 65 20 63 6f 6e 73 74 61 6e 74  tialize constant
13e37 20 69 6e 74 65 67 65 72 20 27 69 73 44 69 72 65   integer 'isDire
13e38 63 74 27 2e 20 49 66 20 74 68 65 0a 20 20 2a 2a  ct'. If the.  **
13e39 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70   atomic-write op
13e3a 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 65 6e  timization is en
13e3b 61 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75  abled in this bu
13e3c 69 6c 64 2c 20 74 68 65 6e 20 69 73 44 69 72 65  ild, then isDire
13e3d 63 74 0a 20 20 2a 2a 20 69 73 20 69 6e 69 74 69  ct.  ** is initi
13e3e 61 6c 69 7a 65 64 20 74 6f 20 74 68 65 20 76 61  alized to the va
13e3f 6c 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68  lue passed as th
13e40 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 70  e isDirectMode p
13e41 61 72 61 6d 65 74 65 72 0a 20 20 2a 2a 20 74 6f  arameter.  ** to
13e42 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20   this function. 
13e43 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 69 73  Otherwise, it is
13e44 20 61 6c 77 61 79 73 20 73 65 74 20 74 6f 20 7a   always set to z
13e45 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ero..  **.  ** T
13e46 68 65 20 69 64 65 61 20 69 73 20 74 68 61 74 20  he idea is that 
13e47 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72  if the atomic-wr
13e48 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ite optimization
13e49 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 65 6e 61   is not.  ** ena
13e4a 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 20  bled at compile 
13e4b 74 69 6d 65 2c 20 74 68 65 20 63 6f 6d 70 69 6c  time, the compil
13e4c 65 72 20 63 61 6e 20 6f 6d 69 74 20 74 68 65 20  er can omit the 
13e4d 74 65 73 74 73 20 6f 66 0a 20 20 2a 2a 20 27 69  tests of.  ** 'i
13e4e 73 44 69 72 65 63 74 27 20 62 65 6c 6f 77 2c 20  sDirect' below, 
13e4f 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 62  as well as the b
13e50 6c 6f 63 6b 20 65 6e 63 6c 6f 73 65 64 20 69 6e  lock enclosed in
13e51 20 74 68 65 0a 20 20 2a 2a 20 22 69 66 28 20 69   the.  ** "if( i
13e52 73 44 69 72 65 63 74 20 29 22 20 63 6f 6e 64 69  sDirect )" condi
13e53 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  tion..  */.#ifnd
13e54 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
13e55 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 23 20  _ATOMIC_WRITE.# 
13e56 64 65 66 69 6e 65 20 44 49 52 45 43 54 5f 4d 4f  define DIRECT_MO
13e57 44 45 20 30 0a 20 20 61 73 73 65 72 74 28 20 69  DE 0.  assert( i
13e58 73 44 69 72 65 63 74 4d 6f 64 65 3d 3d 30 20 29  sDirectMode==0 )
13e59 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
13e5a 45 54 45 52 28 69 73 44 69 72 65 63 74 4d 6f 64  ETER(isDirectMod
13e5b 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  e);.#else.# defi
13e5c 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 69  ne DIRECT_MODE i
13e5d 73 44 69 72 65 63 74 4d 6f 64 65 0a 23 65 6e 64  sDirectMode.#end
13e5e 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  if..  assert( pP
13e5f 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47  ager->state>=PAG
13e60 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20  ER_RESERVED );. 
13e61 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 63 68   if( !pPager->ch
13e62 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 26 26  angeCountDone &&
13e63 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e   pPager->dbSize>
13e64 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a  0 ){.    PgHdr *
13e65 70 50 67 48 64 72 3b 20 20 20 20 20 20 20 20 20  pPgHdr;         
13e66 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 72 65         /* Refere
13e67 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20 2a 2f  nce to page 1 */
13e68 0a 20 20 20 20 75 33 32 20 63 68 61 6e 67 65 5f  .    u32 change_
13e69 63 6f 75 6e 74 65 72 3b 20 20 20 20 20 20 20 20  counter;        
13e6a 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 76 61     /* Initial va
13e6b 6c 75 65 20 6f 66 20 63 68 61 6e 67 65 2d 63 6f  lue of change-co
13e6c 75 6e 74 65 72 20 66 69 65 6c 64 20 2a 2f 0a 0a  unter field */..
13e6d 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
13e6e 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 26 26  ger->tempFile &&
13e6f 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e   isOpen(pPager->
13e70 66 64 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 4f  fd) );..    /* O
13e71 70 65 6e 20 70 61 67 65 20 31 20 6f 66 20 74 68  pen page 1 of th
13e72 65 20 66 69 6c 65 20 66 6f 72 20 77 72 69 74 69  e file for writi
13e73 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ng. */.    rc = 
13e74 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
13e75 70 50 61 67 65 72 2c 20 31 2c 20 26 70 50 67 48  pPager, 1, &pPgH
13e76 64 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  dr);.    assert(
13e77 20 70 50 67 48 64 72 3d 3d 30 20 7c 7c 20 72 63   pPgHdr==0 || rc
13e78 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 0a  ==SQLITE_OK );..
13e79 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65 20 6f      /* If page o
13e7a 6e 65 20 77 61 73 20 66 65 74 63 68 65 64 20 73  ne was fetched s
13e7b 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 61 6e 64  uccessfully, and
13e7c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13e7d 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 6f 70 65  s not.    ** ope
13e7e 72 61 74 69 6e 67 20 69 6e 20 64 69 72 65 63 74  rating in direct
13e7f 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20 70 61 67 65  -mode, make page
13e80 20 31 20 77 72 69 74 61 62 6c 65 2e 20 20 57 68   1 writable.  Wh
13e81 65 6e 20 6e 6f 74 20 69 6e 20 0a 20 20 20 20 2a  en not in .    *
13e82 2a 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 70  * direct mode, p
13e83 61 67 65 20 31 20 69 73 20 61 6c 77 61 79 73 20  age 1 is always 
13e84 68 65 6c 64 20 69 6e 20 63 61 63 68 65 20 61 6e  held in cache an
13e85 64 20 68 65 6e 63 65 20 74 68 65 20 50 61 67 65  d hence the Page
13e86 72 47 65 74 28 29 0a 20 20 20 20 2a 2a 20 61 62  rGet().    ** ab
13e87 6f 76 65 20 69 73 20 61 6c 77 61 79 73 20 73 75  ove is always su
13e88 63 63 65 73 73 66 75 6c 20 2d 20 68 65 6e 63 65  ccessful - hence
13e89 20 74 68 65 20 41 4c 57 41 59 53 20 6f 6e 20 72   the ALWAYS on r
13e8a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20  c==SQLITE_OK..  
13e8b 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 44 49    */.    if( !DI
13e8c 52 45 43 54 5f 4d 4f 44 45 20 26 26 20 41 4c 57  RECT_MODE && ALW
13e8d 41 59 53 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  AYS(rc==SQLITE_O
13e8e 4b 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  K) ){.      rc =
13e8f 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
13e90 74 65 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20  te(pPgHdr);.    
13e91 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  }..    if( rc==S
13e92 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13e93 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74    /* Increment t
13e94 68 65 20 76 61 6c 75 65 20 6a 75 73 74 20 72 65  he value just re
13e95 61 64 20 61 6e 64 20 77 72 69 74 65 20 69 74 20  ad and write it 
13e96 62 61 63 6b 20 74 6f 20 62 79 74 65 20 32 34 2e  back to byte 24.
13e97 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 6e 67 65   */.      change
13e98 5f 63 6f 75 6e 74 65 72 20 3d 20 73 71 6c 69 74  _counter = sqlit
13e99 65 33 47 65 74 34 62 79 74 65 28 28 75 38 2a 29  e3Get4byte((u8*)
13e9a 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65  pPager->dbFileVe
13e9b 72 73 29 3b 0a 20 20 20 20 20 20 63 68 61 6e 67  rs);.      chang
13e9c 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b 0a 20 20 20  e_counter++;.   
13e9d 20 20 20 70 75 74 33 32 62 69 74 73 28 28 28 63     put32bits(((c
13e9e 68 61 72 2a 29 70 50 67 48 64 72 2d 3e 70 44 61  har*)pPgHdr->pDa
13e9f 74 61 29 2b 32 34 2c 20 63 68 61 6e 67 65 5f 63  ta)+24, change_c
13ea0 6f 75 6e 74 65 72 29 3b 0a 0a 20 20 20 20 20 20  ounter);..      
13ea1 2f 2a 20 49 66 20 72 75 6e 6e 69 6e 67 20 69 6e  /* If running in
13ea2 20 64 69 72 65 63 74 20 6d 6f 64 65 2c 20 77 72   direct mode, wr
13ea3 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ite the contents
13ea4 20 6f 66 20 70 61 67 65 20 31 20 74 6f 20 74 68   of page 1 to th
13ea5 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20  e file. */.     
13ea6 20 69 66 28 20 44 49 52 45 43 54 5f 4d 4f 44 45   if( DIRECT_MODE
13ea7 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   ){.        cons
13ea8 74 20 76 6f 69 64 20 2a 7a 42 75 66 20 3d 20 70  t void *zBuf = p
13ea9 50 67 48 64 72 2d 3e 70 44 61 74 61 3b 0a 20 20  PgHdr->pData;.  
13eaa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
13eab 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65  ager->dbFileSize
13eac 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63  >0 );.        rc
13ead 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
13eae 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 7a 42  e(pPager->fd, zB
13eaf 75 66 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  uf, pPager->page
13eb0 53 69 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Size, 0);.      
13eb1 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13eb2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
13eb3 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43   pPager->changeC
13eb4 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20  ountDone = 1;.  
13eb5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
13eb6 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 50 61  lse{.        pPa
13eb7 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74  ger->changeCount
13eb8 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  Done = 1;.      
13eb9 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
13eba 52 65 6c 65 61 73 65 20 74 68 65 20 70 61 67 65  Release the page
13ebb 20 72 65 66 65 72 65 6e 63 65 2e 20 2a 2f 0a 20   reference. */. 
13ebc 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
13ebd 6e 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 20  nref(pPgHdr);.  
13ebe 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
13ebf 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65  ../*.** Sync the
13ec0 20 70 61 67 65 72 20 66 69 6c 65 20 74 6f 20 64   pager file to d
13ec1 69 73 6b 2e 20 54 68 69 73 20 69 73 20 61 20 6e  isk. This is a n
13ec2 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f  o-op for in-memo
13ec3 72 79 20 66 69 6c 65 73 0a 2a 2a 20 6f 72 20 70  ry files.** or p
13ec4 61 67 65 73 20 77 69 74 68 20 74 68 65 20 50 61  ages with the Pa
13ec5 67 65 72 2e 6e 6f 53 79 6e 63 20 66 6c 61 67 20  ger.noSync flag 
13ec6 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  set..**.** If su
13ec7 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 63 61 6c  ccessful, or cal
13ec8 6c 65 64 20 6f 6e 20 61 20 70 61 67 65 72 20 66  led on a pager f
13ec9 6f 72 20 77 68 69 63 68 20 69 74 20 69 73 20 61  or which it is a
13eca 20 6e 6f 2d 6f 70 2c 20 74 68 69 73 0a 2a 2a 20   no-op, this.** 
13ecb 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
13ecc 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65   SQLITE_OK. Othe
13ecd 72 77 69 73 65 2c 20 61 6e 20 49 4f 20 65 72 72  rwise, an IO err
13ece 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
13ecf 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ned..*/.SQLITE_P
13ed0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13ed1 65 33 50 61 67 65 72 53 79 6e 63 28 50 61 67 65  e3PagerSync(Page
13ed2 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 6e  r *pPager){.  in
13ed3 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
13ed4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13ed5 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
13ed6 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21  e */.  assert( !
13ed7 4d 45 4d 44 42 20 29 3b 0a 20 20 69 66 28 20 70  MEMDB );.  if( p
13ed8 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b  Pager->noSync ){
13ed9 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
13eda 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
13edb 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
13edc 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c  Sync(pPager->fd,
13edd 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c   pPager->sync_fl
13ede 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ags);.  }.  retu
13edf 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
13ee0 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73  Sync the databas
13ee1 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 70  e file for the p
13ee2 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a 4d 61  ager pPager. zMa
13ee3 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74  ster points to t
13ee4 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61 20  he name.** of a 
13ee5 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
13ee6 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ile that should 
13ee7 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  be written into 
13ee8 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a  the individual.*
13ee9 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  * journal file. 
13eea 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65 20 4e  zMaster may be N
13eeb 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 69 6e  ULL, which is in
13eec 74 65 72 70 72 65 74 65 64 20 61 73 20 6e 6f 20  terpreted as no 
13eed 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61  master.** journa
13eee 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61 74 61  l (a single data
13eef 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  base transaction
13ef0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  )..**.** This ro
13ef1 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68  utine ensures th
13ef2 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 54 68  at:.**.**   * Th
13ef3 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
13ef4 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69  change-counter i
13ef5 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a 20 20 20  s updated,.**   
13ef6 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73  * the journal is
13ef7 20 73 79 6e 63 65 64 20 28 75 6e 6c 65 73 73 20   synced (unless 
13ef8 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65  the atomic-write
13ef9 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73   optimization is
13efa 20 75 73 65 64 29 2c 0a 2a 2a 20 20 20 2a 20 61   used),.**   * a
13efb 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 61  ll dirty pages a
13efc 72 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  re written to th
13efd 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
13efe 20 0a 2a 2a 20 20 20 2a 20 74 68 65 20 64 61 74   .**   * the dat
13eff 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 74 72  abase file is tr
13f00 75 6e 63 61 74 65 64 20 28 69 66 20 72 65 71 75  uncated (if requ
13f01 69 72 65 64 29 2c 20 61 6e 64 0a 2a 2a 20 20 20  ired), and.**   
13f02 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  * the database f
13f03 69 6c 65 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a  ile synced. .**.
13f04 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e  ** The only thin
13f05 67 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74  g that remains t
13f06 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 74 72 61  o commit the tra
13f07 6e 73 61 63 74 69 6f 6e 20 69 73 20 74 6f 20 66  nsaction is to f
13f08 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20 28 64 65 6c  inalize .** (del
13f09 65 74 65 2c 20 74 72 75 6e 63 61 74 65 20 6f 72  ete, truncate or
13f0a 20 7a 65 72 6f 20 74 68 65 20 66 69 72 73 74 20   zero the first 
13f0b 70 61 72 74 20 6f 66 29 20 74 68 65 20 6a 6f 75  part of) the jou
13f0c 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20 0a 2a  rnal file (or .*
13f0d 2a 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73  * delete the mas
13f0e 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
13f0f 20 69 66 20 73 70 65 63 69 66 69 65 64 29 2e 0a   if specified)..
13f10 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
13f11 69 66 20 7a 4d 61 73 74 65 72 3d 3d 4e 55 4c 4c  if zMaster==NULL
13f12 2c 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 20  , this does not 
13f13 6f 76 65 72 77 72 69 74 65 20 61 20 70 72 65 76  overwrite a prev
13f14 69 6f 75 73 20 76 61 6c 75 65 0a 2a 2a 20 70 61  ious value.** pa
13f15 73 73 65 64 20 74 6f 20 61 6e 20 73 71 6c 69 74  ssed to an sqlit
13f16 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61  e3PagerCommitPha
13f17 73 65 4f 6e 65 28 29 20 63 61 6c 6c 2e 0a 2a 2a  seOne() call..**
13f18 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6e 61 6c  .** If the final
13f19 20 70 61 72 61 6d 65 74 65 72 20 2d 20 6e 6f 53   parameter - noS
13f1a 79 6e 63 20 2d 20 69 73 20 74 72 75 65 2c 20 74  ync - is true, t
13f1b 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
13f1c 20 66 69 6c 65 20 69 74 73 65 6c 66 0a 2a 2a 20   file itself.** 
13f1d 69 73 20 6e 6f 74 20 73 79 6e 63 65 64 2e 20 54  is not synced. T
13f1e 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63  he caller must c
13f1f 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72  all sqlite3Pager
13f20 53 79 6e 63 28 29 20 64 69 72 65 63 74 6c 79 20  Sync() directly 
13f21 74 6f 0a 2a 2a 20 73 79 6e 63 20 74 68 65 20 64  to.** sync the d
13f22 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65 66  atabase file bef
13f23 6f 72 65 20 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d  ore calling Comm
13f24 69 74 50 68 61 73 65 54 77 6f 28 29 20 74 6f 20  itPhaseTwo() to 
13f25 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 6a 6f  delete the.** jo
13f26 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 74 68  urnal file in th
13f27 69 73 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49  is case..*/.SQLI
13f28 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
13f29 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69  qlite3PagerCommi
13f2a 74 50 68 61 73 65 4f 6e 65 28 0a 20 20 50 61 67  tPhaseOne(.  Pag
13f2b 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20  er *pPager,     
13f2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13f2d 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  Pager object */.
13f2e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d    const char *zM
13f2f 61 73 74 65 72 2c 20 20 20 20 20 20 20 20 20 20  aster,          
13f30 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55 4c 4c    /* If not NULL
13f31 2c 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  , the master jou
13f32 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 69  rnal name */.  i
13f33 6e 74 20 6e 6f 53 79 6e 63 20 20 20 20 20 20 20  nt noSync       
13f34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
13f35 2a 20 54 72 75 65 20 74 6f 20 6f 6d 69 74 20 74  * True to omit t
13f36 68 65 20 78 53 79 6e 63 20 6f 6e 20 74 68 65 20  he xSync on the 
13f37 64 62 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20  db file */.){.  
13f38 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
13f39 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
13f3a 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
13f3b 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 64 62 4f 72  /..  /* The dbOr
13f3c 69 67 53 69 7a 65 20 69 73 20 6e 65 76 65 72 20  igSize is never 
13f3d 73 65 74 20 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d  set if journal_m
13f3e 6f 64 65 3d 4f 46 46 20 2a 2f 0a 20 20 61 73 73  ode=OFF */.  ass
13f3f 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ert( pPager->jou
13f40 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f  rnalMode!=PAGER_
13f41 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20  JOURNALMODE_OFF 
13f42 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69  || pPager->dbOri
13f43 67 53 69 7a 65 3d 3d 30 20 29 3b 0a 0a 20 20 2f  gSize==0 );..  /
13f44 2a 20 49 66 20 61 20 70 72 69 6f 72 20 65 72 72  * If a prior err
13f45 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 74 68 69  or occurred, thi
13f46 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64  s routine should
13f47 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 2e 20   not be called. 
13f48 20 52 4f 4c 4c 42 41 43 4b 0a 20 20 2a 2a 20 69   ROLLBACK.  ** i
13f49 73 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  s the appropriat
13f4a 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 6e  e response to an
13f4b 20 65 72 72 6f 72 2c 20 6e 6f 74 20 43 4f 4d 4d   error, not COMM
13f4c 49 54 2e 20 20 47 75 61 72 64 20 61 67 61 69 6e  IT.  Guard again
13f4d 73 74 0a 20 20 2a 2a 20 63 6f 64 69 6e 67 20 65  st.  ** coding e
13f4e 72 72 6f 72 73 20 62 79 20 72 65 70 65 61 74 69  rrors by repeati
13f4f 6e 67 20 74 68 65 20 70 72 69 6f 72 20 65 72 72  ng the prior err
13f50 6f 72 2e 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56  or. */.  if( NEV
13f51 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  ER(pPager->errCo
13f52 64 65 29 20 29 20 72 65 74 75 72 6e 20 70 50 61  de) ) return pPa
13f53 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20  ger->errCode;.. 
13f54 20 50 41 47 45 52 54 52 41 43 45 28 28 22 44 41   PAGERTRACE(("DA
13f55 54 41 42 41 53 45 20 53 59 4e 43 3a 20 46 69 6c  TABASE SYNC: Fil
13f56 65 3d 25 73 20 7a 4d 61 73 74 65 72 3d 25 73 20  e=%s zMaster=%s 
13f57 6e 53 69 7a 65 3d 25 64 5c 6e 22 2c 20 0a 20 20  nSize=%d\n", .  
13f58 20 20 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c      pPager->zFil
13f59 65 6e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c 20  ename, zMaster, 
13f5a 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 29  pPager->dbSize))
13f5b 3b 0a 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 26  ;..  if( MEMDB &
13f5c 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69  & pPager->dbModi
13f5d 66 69 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 49  fied ){.    /* I
13f5e 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 2d  f this is an in-
13f5f 6d 65 6d 6f 72 79 20 64 62 2c 20 6f 72 20 6e 6f  memory db, or no
13f60 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65 6e   pages have been
13f61 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 6f 72 20   written to, or 
13f62 74 68 69 73 0a 20 20 20 20 2a 2a 20 66 75 6e 63  this.    ** func
13f63 74 69 6f 6e 20 68 61 73 20 61 6c 72 65 61 64 79  tion has already
13f64 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 69 74   been called, it
13f65 20 69 73 20 6d 6f 73 74 6c 79 20 61 20 6e 6f 2d   is mostly a no-
13f66 6f 70 2e 20 20 48 6f 77 65 76 65 72 2c 20 61 6e  op.  However, an
13f67 79 0a 20 20 20 20 2a 2a 20 62 61 63 6b 75 70 20  y.    ** backup 
13f68 69 6e 20 70 72 6f 67 72 65 73 73 20 6e 65 65 64  in progress need
13f69 73 20 74 6f 20 62 65 20 72 65 73 74 61 72 74 65  s to be restarte
13f6a 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  d..    */.    sq
13f6b 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 61  lite3BackupResta
13f6c 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b  rt(pPager->pBack
13f6d 75 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  up);.  }else if(
13f6e 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d   pPager->state!=
13f6f 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26 26 20  PAGER_SYNCED && 
13f70 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
13f71 65 64 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 54 68  ed ){..    /* Th
13f72 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
13f73 6b 20 75 70 64 61 74 65 73 20 74 68 65 20 63 68  k updates the ch
13f74 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e 20 45 78  ange-counter. Ex
13f75 61 63 74 6c 79 20 68 6f 77 20 69 74 0a 20 20 20  actly how it.   
13f76 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 64 65   ** does this de
13f77 70 65 6e 64 73 20 6f 6e 20 77 68 65 74 68 65 72  pends on whether
13f78 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 74 6f 6d   or not the atom
13f79 69 63 2d 75 70 64 61 74 65 20 6f 70 74 69 6d 69  ic-update optimi
13f7a 7a 61 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 77 61  zation.    ** wa
13f7b 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d  s enabled at com
13f7c 70 69 6c 65 20 74 69 6d 65 2c 20 61 6e 64 20 69  pile time, and i
13f7d 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  f this transacti
13f7e 6f 6e 20 6d 65 65 74 73 20 74 68 65 20 0a 20 20  on meets the .  
13f7f 20 20 2a 2a 20 72 75 6e 74 69 6d 65 20 63 72 69    ** runtime cri
13f80 74 65 72 69 61 20 74 6f 20 75 73 65 20 74 68 65  teria to use the
13f81 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 20 20 20   operation: .   
13f82 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20   **.    **    * 
13f83 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  The file-system 
13f84 73 75 70 70 6f 72 74 73 20 74 68 65 20 61 74 6f  supports the ato
13f85 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70 65 72  mic-write proper
13f86 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20 20 20  ty for.    **   
13f87 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a     blocks of siz
13f88 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 6e 64  e page-size, and
13f89 20 0a 20 20 20 20 2a 2a 20 20 20 20 2a 20 54 68   .    **    * Th
13f8a 69 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e 6f 74  is commit is not
13f8b 20 70 61 72 74 20 6f 66 20 61 20 6d 75 6c 74 69   part of a multi
13f8c 2d 66 69 6c 65 20 74 72 61 6e 73 61 63 74 69 6f  -file transactio
13f8d 6e 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20 20 20  n, and.    **   
13f8e 20 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 70   * Exactly one p
13f8f 61 67 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64  age has been mod
13f90 69 66 69 65 64 20 61 6e 64 20 73 74 6f 72 65 20  ified and store 
13f91 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  in the journal f
13f92 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ile..    **.    
13f93 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69  ** If the optimi
13f94 7a 61 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 65  zation was not e
13f95 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c  nabled at compil
13f96 65 20 74 69 6d 65 2c 20 74 68 65 6e 20 74 68 65  e time, then the
13f97 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 69 6e  .    ** pager_in
13f98 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72  cr_changecounter
13f99 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  () function is c
13f9a 61 6c 6c 65 64 20 74 6f 20 75 70 64 61 74 65 20  alled to update 
13f9b 74 68 65 20 63 68 61 6e 67 65 0a 20 20 20 20 2a  the change.    *
13f9c 2a 20 63 6f 75 6e 74 65 72 20 69 6e 20 27 69 6e  * counter in 'in
13f9d 64 69 72 65 63 74 2d 6d 6f 64 65 27 2e 20 49 66  direct-mode'. If
13f9e 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f   the optimizatio
13f9f 6e 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 69 6e  n is compiled in
13fa0 20 62 75 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e   but.    ** is n
13fa1 6f 74 20 61 70 70 6c 69 63 61 62 6c 65 20 74 6f  ot applicable to
13fa2 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
13fa3 6e 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4a  n, call sqlite3J
13fa4 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 29 0a 20  ournalCreate(). 
13fa5 20 20 20 2a 2a 20 74 6f 20 6d 61 6b 65 20 73 75     ** to make su
13fa6 72 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  re the journal f
13fa7 69 6c 65 20 68 61 73 20 61 63 74 75 61 6c 6c 79  ile has actually
13fa8 20 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 74   been created, t
13fa9 68 65 6e 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20  hen call.    ** 
13faa 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67  pager_incr_chang
13fab 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75 70  ecounter() to up
13fac 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d  date the change-
13fad 63 6f 75 6e 74 65 72 20 69 6e 20 69 6e 64 69 72  counter in indir
13fae 65 63 74 0a 20 20 20 20 2a 2a 20 6d 6f 64 65 2e  ect.    ** mode.
13faf 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20   .    **.    ** 
13fb0 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68  Otherwise, if th
13fb1 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69  e optimization i
13fb2 73 20 62 6f 74 68 20 65 6e 61 62 6c 65 64 20 61  s both enabled a
13fb3 6e 64 20 61 70 70 6c 69 63 61 62 6c 65 2c 0a 20  nd applicable,. 
13fb4 20 20 20 2a 2a 20 74 68 65 6e 20 63 61 6c 6c 20     ** then call 
13fb5 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67  pager_incr_chang
13fb6 65 63 6f 75 6e 74 65 72 28 29 20 74 6f 20 75 70  ecounter() to up
13fb7 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d  date the change-
13fb8 63 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 69  counter.    ** i
13fb9 6e 20 27 64 69 72 65 63 74 27 20 6d 6f 64 65 2e  n 'direct' mode.
13fba 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68   In this case th
13fbb 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77  e journal file w
13fbc 69 6c 6c 20 6e 65 76 65 72 20 62 65 0a 20 20 20  ill never be.   
13fbd 20 2a 2a 20 63 72 65 61 74 65 64 20 66 6f 72 20   ** created for 
13fbe 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e  this transaction
13fbf 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20  ..    */.#ifdef 
13fc0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54  SQLITE_ENABLE_AT
13fc1 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20 50  OMIC_WRITE.    P
13fc2 67 48 64 72 20 2a 70 50 67 3b 0a 20 20 20 20 61  gHdr *pPg;.    a
13fc3 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
13fc4 61 67 65 72 2d 3e 6a 66 64 29 20 7c 7c 20 70 50  ager->jfd) || pP
13fc5 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
13fc6 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
13fc7 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a 20 20 20 20  MODE_OFF );.    
13fc8 69 66 28 20 21 7a 4d 61 73 74 65 72 20 26 26 20  if( !zMaster && 
13fc9 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a  isOpen(pPager->j
13fca 66 64 29 20 0a 20 20 20 20 20 26 26 20 70 50 61  fd) .     && pPa
13fcb 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
13fcc 3d 6a 72 6e 6c 42 75 66 66 65 72 53 69 7a 65 28  =jrnlBufferSize(
13fcd 70 50 61 67 65 72 29 20 0a 20 20 20 20 20 26 26  pPager) .     &&
13fce 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e   pPager->dbSize>
13fcf 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53  =pPager->dbFileS
13fd0 69 7a 65 0a 20 20 20 20 20 26 26 20 28 30 3d 3d  ize.     && (0==
13fd1 28 70 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63  (pPg = sqlite3Pc
13fd2 61 63 68 65 44 69 72 74 79 4c 69 73 74 28 70 50  acheDirtyList(pP
13fd3 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 29 20  ager->pPCache)) 
13fd4 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70 44 69 72 74  || 0==pPg->pDirt
13fd5 79 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  y).    ){.      
13fd6 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 64 62  /* Update the db
13fd7 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f 75   file change cou
13fd8 6e 74 65 72 20 76 69 61 20 74 68 65 20 64 69 72  nter via the dir
13fd9 65 63 74 2d 77 72 69 74 65 20 6d 65 74 68 6f 64  ect-write method
13fda 2e 20 54 68 65 20 0a 20 20 20 20 20 20 2a 2a 20  . The .      ** 
13fdb 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 77  following call w
13fdc 69 6c 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 69  ill modify the i
13fdd 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65 73 65  n-memory represe
13fde 6e 74 61 74 69 6f 6e 20 6f 66 20 70 61 67 65 20  ntation of page 
13fdf 31 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 69  1 .      ** to i
13fe0 6e 63 6c 75 64 65 20 74 68 65 20 75 70 64 61 74  nclude the updat
13fe1 65 64 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  ed change counte
13fe2 72 20 61 6e 64 20 74 68 65 6e 20 77 72 69 74 65  r and then write
13fe3 20 70 61 67 65 20 31 20 0a 20 20 20 20 20 20 2a   page 1 .      *
13fe4 2a 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68  * directly to th
13fe5 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
13fe6 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20   Because of the 
13fe7 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 0a 20 20  atomic-write .  
13fe8 20 20 20 20 2a 2a 20 70 72 6f 70 65 72 74 79 20      ** property 
13fe9 6f 66 20 74 68 65 20 68 6f 73 74 20 66 69 6c 65  of the host file
13fea 2d 73 79 73 74 65 6d 2c 20 74 68 69 73 20 69 73  -system, this is
13feb 20 73 61 66 65 2e 0a 20 20 20 20 20 20 2a 2f 0a   safe..      */.
13fec 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
13fed 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e  _incr_changecoun
13fee 74 65 72 28 70 50 61 67 65 72 2c 20 31 29 3b 0a  ter(pPager, 1);.
13fef 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
13ff0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4a 6f 75   rc = sqlite3Jou
13ff1 72 6e 61 6c 43 72 65 61 74 65 28 70 50 61 67 65  rnalCreate(pPage
13ff2 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 69  r->jfd);.      i
13ff3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
13ff4 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
13ff5 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e   pager_incr_chan
13ff6 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72  gecounter(pPager
13ff7 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
13ff8 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 72 63    }.#else.    rc
13ff9 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63 68   = pager_incr_ch
13ffa 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61 67  angecounter(pPag
13ffb 65 72 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20  er, 0);.#endif. 
13ffc 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
13ffd 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d  E_OK ) goto comm
13ffe 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69  it_phase_one_exi
13fff 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  t;..    /* If th
14000 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68  is transaction h
14001 61 73 20 6d 61 64 65 20 74 68 65 20 64 61 74 61  as made the data
14002 62 61 73 65 20 73 6d 61 6c 6c 65 72 2c 20 74 68  base smaller, th
14003 65 6e 20 61 6c 6c 20 70 61 67 65 73 0a 20 20 20  en all pages.   
14004 20 2a 2a 20 62 65 69 6e 67 20 64 69 73 63 61 72   ** being discar
14005 64 65 64 20 62 79 20 74 68 65 20 74 72 75 6e 63  ded by the trunc
14006 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 77 72  ation must be wr
14007 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75  itten to the jou
14008 72 6e 61 6c 0a 20 20 20 20 2a 2a 20 66 69 6c 65  rnal.    ** file
14009 2e 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20  . This can only 
1400a 68 61 70 70 65 6e 20 69 6e 20 61 75 74 6f 2d 76  happen in auto-v
1400b 61 63 75 75 6d 20 6d 6f 64 65 2e 0a 20 20 20 20  acuum mode..    
1400c 2a 2a 0a 20 20 20 20 2a 2a 20 42 65 66 6f 72 65  **.    ** Before
1400d 20 72 65 61 64 69 6e 67 20 74 68 65 20 70 61 67   reading the pag
1400e 65 73 20 77 69 74 68 20 70 61 67 65 20 6e 75 6d  es with page num
1400f 62 65 72 73 20 6c 61 72 67 65 72 20 74 68 61 6e  bers larger than
14010 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 75 72   the .    ** cur
14011 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 50 61  rent value of Pa
14012 67 65 72 2e 64 62 53 69 7a 65 2c 20 73 65 74 20  ger.dbSize, set 
14013 64 62 53 69 7a 65 20 62 61 63 6b 20 74 6f 20 74  dbSize back to t
14014 68 65 20 76 61 6c 75 65 0a 20 20 20 20 2a 2a 20  he value.    ** 
14015 74 68 61 74 20 69 74 20 74 6f 6f 6b 20 61 74 20  that it took at 
14016 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
14017 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 4f 74   transaction. Ot
14018 68 65 72 77 69 73 65 2c 20 74 68 65 0a 20 20 20  herwise, the.   
14019 20 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c   ** calls to sql
1401a 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20 72  ite3PagerGet() r
1401b 65 74 75 72 6e 20 7a 65 72 6f 65 64 20 70 61 67  eturn zeroed pag
1401c 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20 0a 20  es instead of . 
1401d 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 64 61     ** reading da
1401e 74 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ta from the data
1401f 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a  base file..    *
14020 2a 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 6a 6f  *.    ** When jo
14021 75 72 6e 61 6c 5f 6d 6f 64 65 3d 3d 4f 46 46 20  urnal_mode==OFF 
14022 74 68 65 20 64 62 4f 72 69 67 53 69 7a 65 20 69  the dbOrigSize i
14023 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2c 20 73  s always zero, s
14024 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 62 6c  o this.    ** bl
14025 6f 63 6b 20 6e 65 76 65 72 20 72 75 6e 73 20 69  ock never runs i
14026 66 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 4f  f journal_mode=O
14027 46 46 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64  FF..    */.#ifnd
14028 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
14029 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
1402a 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
1402b 3c 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53  <pPager->dbOrigS
1402c 69 7a 65 20 0a 20 20 20 20 20 26 26 20 41 4c 57  ize .     && ALW
1402d 41 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72  AYS(pPager->jour
1402e 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a  nalMode!=PAGER_J
1402f 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a  OURNALMODE_OFF).
14030 20 20 20 20 29 7b 0a 20 20 20 20 20 20 50 67 6e      ){.      Pgn
14031 6f 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  o i;            
14032 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14033 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74         /* Iterat
14034 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20  or variable */. 
14035 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20       const Pgno 
14036 69 53 6b 69 70 20 3d 20 50 41 47 45 52 5f 4d 4a  iSkip = PAGER_MJ
14037 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b 20 2f  _PGNO(pPager); /
14038 2a 20 50 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 70  * Pending lock p
14039 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e  age */.      con
1403a 73 74 20 50 67 6e 6f 20 64 62 53 69 7a 65 20 3d  st Pgno dbSize =
1403b 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b   pPager->dbSize;
1403c 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
1403d 73 65 20 69 6d 61 67 65 20 73 69 7a 65 20 2a 2f  se image size */
1403e 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e   .      pPager->
1403f 64 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d  dbSize = pPager-
14040 3e 64 62 4f 72 69 67 53 69 7a 65 3b 0a 20 20 20  >dbOrigSize;.   
14041 20 20 20 66 6f 72 28 20 69 3d 64 62 53 69 7a 65     for( i=dbSize
14042 2b 31 3b 20 69 3c 3d 70 50 61 67 65 72 2d 3e 64  +1; i<=pPager->d
14043 62 4f 72 69 67 53 69 7a 65 3b 20 69 2b 2b 20 29  bOrigSize; i++ )
14044 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73  {.        if( !s
14045 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
14046 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72  (pPager->pInJour
14047 6e 61 6c 2c 20 69 29 20 26 26 20 69 21 3d 69 53  nal, i) && i!=iS
14048 6b 69 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20  kip ){.         
14049 20 50 67 48 64 72 20 2a 70 50 61 67 65 3b 20 20   PgHdr *pPage;  
1404a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
1404b 67 65 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f  ge to journal */
1404c 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
1404d 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
1404e 70 50 61 67 65 72 2c 20 69 2c 20 26 70 50 61 67  pPager, i, &pPag
1404f 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  e);.          if
14050 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
14051 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68  ) goto commit_ph
14052 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20  ase_one_exit;.  
14053 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
14054 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
14055 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
14056 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
14057 65 66 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ef(pPage);.     
14058 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
14059 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f  ITE_OK ) goto co
1405a 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65  mmit_phase_one_e
1405b 78 69 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  xit;.        }. 
1405c 20 20 20 20 20 7d 20 0a 20 20 20 20 20 20 70 50       } .      pP
1405d 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 64  ager->dbSize = d
1405e 62 53 69 7a 65 3b 0a 20 20 20 20 7d 0a 23 65 6e  bSize;.    }.#en
1405f 64 69 66 0a 0a 20 20 20 20 2f 2a 20 57 72 69 74  dif..    /* Writ
14060 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
14061 72 6e 61 6c 20 6e 61 6d 65 20 69 6e 74 6f 20 74  rnal name into t
14062 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  he journal file.
14063 20 49 66 20 61 20 6d 61 73 74 65 72 20 0a 20 20   If a master .  
14064 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c    ** journal fil
14065 65 20 6e 61 6d 65 20 68 61 73 20 61 6c 72 65 61  e name has alrea
14066 64 79 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  dy been written 
14067 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
14068 69 6c 65 2c 20 0a 20 20 20 20 2a 2a 20 6f 72 20  ile, .    ** or 
14069 69 66 20 7a 4d 61 73 74 65 72 20 69 73 20 4e 55  if zMaster is NU
1406a 4c 4c 20 28 6e 6f 20 6d 61 73 74 65 72 20 6a 6f  LL (no master jo
1406b 75 72 6e 61 6c 29 2c 20 74 68 65 6e 20 74 68 69  urnal), then thi
1406c 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f  s call is a no-o
1406d 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  p..    */.    rc
1406e 20 3d 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f   = writeMasterJo
1406f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20 7a 4d  urnal(pPager, zM
14070 61 73 74 65 72 29 3b 0a 20 20 20 20 69 66 28 20  aster);.    if( 
14071 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
14072 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73  goto commit_phas
14073 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20  e_one_exit;..   
14074 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75   /* Sync the jou
14075 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68  rnal file. If th
14076 65 20 61 74 6f 6d 69 63 2d 75 70 64 61 74 65 20  e atomic-update 
14077 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20  optimization is 
14078 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 75 73 65  being.    ** use
14079 64 2c 20 74 68 69 73 20 63 61 6c 6c 20 77 69 6c  d, this call wil
1407a 6c 20 6e 6f 74 20 63 72 65 61 74 65 20 74 68 65  l not create the
1407b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 72   journal file or
1407c 20 70 65 72 66 6f 72 6d 20 61 6e 79 0a 20 20 20   perform any.   
1407d 20 2a 2a 20 72 65 61 6c 20 49 4f 2e 0a 20 20 20   ** real IO..   
1407e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 79 6e   */.    rc = syn
1407f 63 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29  cJournal(pPager)
14080 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
14081 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63  LITE_OK ) goto c
14082 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f  ommit_phase_one_
14083 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 57 72  exit;..    /* Wr
14084 69 74 65 20 61 6c 6c 20 64 69 72 74 79 20 70 61  ite all dirty pa
14085 67 65 73 20 74 6f 20 74 68 65 20 64 61 74 61 62  ges to the datab
14086 61 73 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20  ase file. */.   
14087 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74   rc = pager_writ
14088 65 5f 70 61 67 65 6c 69 73 74 28 73 71 6c 69 74  e_pagelist(sqlit
14089 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73  e3PcacheDirtyLis
1408a 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
1408b 65 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  e));.    if( rc!
1408c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1408d 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d      assert( rc!=
1408e 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f  SQLITE_IOERR_BLO
1408f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 20 67 6f  CKED );.      go
14090 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f  to commit_phase_
14091 6f 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a  one_exit;.    }.
14092 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68      sqlite3Pcach
14093 65 43 6c 65 61 6e 41 6c 6c 28 70 50 61 67 65 72  eCleanAll(pPager
14094 2d 3e 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 20  ->pPCache);..   
14095 20 2f 2a 20 49 66 20 74 68 65 20 66 69 6c 65 20   /* If the file 
14096 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 20 74  on disk is not t
14097 68 65 20 73 61 6d 65 20 73 69 7a 65 20 61 73 20  he same size as 
14098 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61  the database ima
14099 67 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20  ge,.    ** then 
1409a 75 73 65 20 70 61 67 65 72 5f 74 72 75 6e 63 61  use pager_trunca
1409b 74 65 20 74 6f 20 67 72 6f 77 20 6f 72 20 73 68  te to grow or sh
1409c 72 69 6e 6b 20 74 68 65 20 66 69 6c 65 20 68 65  rink the file he
1409d 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  re..    */.    i
1409e 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  f( pPager->dbSiz
1409f 65 21 3d 70 50 61 67 65 72 2d 3e 64 62 46 69 6c  e!=pPager->dbFil
140a0 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 50  eSize ){.      P
140a1 67 6e 6f 20 6e 4e 65 77 20 3d 20 70 50 61 67 65  gno nNew = pPage
140a2 72 2d 3e 64 62 53 69 7a 65 20 2d 20 28 70 50 61  r->dbSize - (pPa
140a3 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 50 41 47  ger->dbSize==PAG
140a4 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
140a5 72 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  r));.      asser
140a6 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
140a7 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  >=PAGER_EXCLUSIV
140a8 45 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  E );.      rc = 
140a9 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70  pager_truncate(p
140aa 50 61 67 65 72 2c 20 6e 4e 65 77 29 3b 0a 20 20  Pager, nNew);.  
140ab 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
140ac 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d  TE_OK ) goto com
140ad 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78  mit_phase_one_ex
140ae 69 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  it;.    }..    /
140af 2a 20 46 69 6e 61 6c 6c 79 2c 20 73 79 6e 63 20  * Finally, sync 
140b0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
140b1 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70  e. */.    if( !p
140b2 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 26 26  Pager->noSync &&
140b3 20 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20   !noSync ){.    
140b4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
140b5 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c  Sync(pPager->fd,
140b6 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c   pPager->sync_fl
140b7 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ags);.    }.    
140b8 49 4f 54 52 41 43 45 28 28 22 44 42 53 59 4e 43  IOTRACE(("DBSYNC
140b9 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29   %p\n", pPager))
140ba 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  ..    pPager->st
140bb 61 74 65 20 3d 20 50 41 47 45 52 5f 53 59 4e 43  ate = PAGER_SYNC
140bc 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d 6d 69 74 5f  ED;.  }..commit_
140bd 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3a 0a  phase_one_exit:.
140be 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
140bf 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  ./*.** When this
140c0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
140c1 6c 65 64 2c 20 74 68 65 20 64 61 74 61 62 61 73  led, the databas
140c2 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20  e file has been 
140c3 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20 75 70  completely.** up
140c4 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65 63 74  dated to reflect
140c5 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d 61 64   the changes mad
140c6 65 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74  e by the current
140c7 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
140c8 0a 2a 2a 20 73 79 6e 63 65 64 20 74 6f 20 64 69  .** synced to di
140c9 73 6b 2e 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20  sk. The journal 
140ca 66 69 6c 65 20 73 74 69 6c 6c 20 65 78 69 73 74  file still exist
140cb 73 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79  s in the file-sy
140cc 73 74 65 6d 20 0a 2a 2a 20 74 68 6f 75 67 68 2c  stem .** though,
140cd 20 61 6e 64 20 69 66 20 61 20 66 61 69 6c 75 72   and if a failur
140ce 65 20 6f 63 63 75 72 73 20 61 74 20 74 68 69 73  e occurs at this
140cf 20 70 6f 69 6e 74 20 69 74 20 77 69 6c 6c 20 65   point it will e
140d0 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20  ventually.** be 
140d1 75 73 65 64 20 61 73 20 61 20 68 6f 74 2d 6a 6f  used as a hot-jo
140d2 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 63 75  urnal and the cu
140d3 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  rrent transactio
140d4 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  n rolled back..*
140d5 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
140d6 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65  on finalizes the
140d7 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 65   journal file, e
140d8 69 74 68 65 72 20 62 79 20 64 65 6c 65 74 69 6e  ither by deletin
140d9 67 2c 20 0a 2a 2a 20 74 72 75 6e 63 61 74 69 6e  g, .** truncatin
140da 67 20 6f 72 20 70 61 72 74 69 61 6c 6c 79 20 7a  g or partially z
140db 65 72 6f 69 6e 67 20 69 74 2c 20 73 6f 20 74 68  eroing it, so th
140dc 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20  at it cannot be 
140dd 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20 68 6f 74  used .** for hot
140de 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63  -journal rollbac
140df 6b 2e 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20  k. Once this is 
140e0 64 6f 6e 65 20 74 68 65 20 74 72 61 6e 73 61 63  done the transac
140e1 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 72 72 65 76  tion is.** irrev
140e2 6f 63 61 62 6c 79 20 63 6f 6d 6d 69 74 74 65 64  ocably committed
140e3 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72  ..**.** If an er
140e4 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 49  ror occurs, an I
140e5 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  O error code is 
140e6 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
140e7 20 70 61 67 65 72 0a 2a 2a 20 6d 6f 76 65 73 20   pager.** moves 
140e8 69 6e 74 6f 20 74 68 65 20 65 72 72 6f 72 20 73  into the error s
140e9 74 61 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  tate. Otherwise,
140ea 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
140eb 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
140ec 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
140ed 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74  lite3PagerCommit
140ee 50 68 61 73 65 54 77 6f 28 50 61 67 65 72 20 2a  PhaseTwo(Pager *
140ef 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72  pPager){.  int r
140f0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  c = SQLITE_OK;  
140f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140f2 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
140f3 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75  /..  /* This rou
140f4 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  tine should not 
140f5 62 65 20 63 61 6c 6c 65 64 20 69 66 20 61 20 70  be called if a p
140f6 72 69 6f 72 20 65 72 72 6f 72 20 68 61 73 20 6f  rior error has o
140f7 63 63 75 72 72 65 64 2e 0a 20 20 2a 2a 20 42 75  ccurred..  ** Bu
140f8 74 20 69 66 20 28 64 75 65 20 74 6f 20 61 20 63  t if (due to a c
140f9 6f 64 69 6e 67 20 65 72 72 6f 72 20 65 6c 73 65  oding error else
140fa 77 68 65 72 65 20 69 6e 20 74 68 65 20 73 79 73  where in the sys
140fb 74 65 6d 29 20 69 74 20 64 6f 65 73 20 67 65 74  tem) it does get
140fc 0a 20 20 2a 2a 20 63 61 6c 6c 65 64 2c 20 6a 75  .  ** called, ju
140fd 73 74 20 72 65 74 75 72 6e 20 74 68 65 20 73 61  st return the sa
140fe 6d 65 20 65 72 72 6f 72 20 63 6f 64 65 20 77 69  me error code wi
140ff 74 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74  thout doing anyt
14100 68 69 6e 67 2e 20 2a 2f 0a 20 20 69 66 28 20 4e  hing. */.  if( N
14101 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72  EVER(pPager->err
14102 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e 20 70  Code) ) return p
14103 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a  Pager->errCode;.
14104 0a 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74  .  /* This funct
14105 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  ion should not b
14106 65 20 63 61 6c 6c 65 64 20 69 66 20 74 68 65 20  e called if the 
14107 70 61 67 65 72 20 69 73 20 6e 6f 74 20 69 6e 20  pager is not in 
14108 61 74 20 6c 65 61 73 74 0a 20 20 2a 2a 20 50 41  at least.  ** PA
14109 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 74 61  GER_RESERVED sta
1410a 74 65 2e 20 41 6e 64 20 69 6e 64 65 65 64 20 53  te. And indeed S
1410b 51 4c 69 74 65 20 6e 65 76 65 72 20 64 6f 65 73  QLite never does
1410c 20 74 68 69 73 2e 20 42 75 74 20 69 74 20 69 73   this. But it is
1410d 0a 20 20 2a 2a 20 6e 69 63 65 20 74 6f 20 68 61  .  ** nice to ha
1410e 76 65 20 74 68 69 73 20 64 65 66 65 6e 73 69 76  ve this defensiv
1410f 65 20 74 65 73 74 20 68 65 72 65 20 61 6e 79 77  e test here anyw
14110 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e  ay..  */.  if( N
14111 45 56 45 52 28 70 50 61 67 65 72 2d 3e 73 74 61  EVER(pPager->sta
14112 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52 56 45  te<PAGER_RESERVE
14113 44 29 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  D) ) return SQLI
14114 54 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20  TE_ERROR;..  /* 
14115 41 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e  An optimization.
14116 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
14117 20 77 61 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c   was not actuall
14118 79 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69 6e  y modified durin
14119 67 0a 20 20 2a 2a 20 74 68 69 73 20 74 72 61 6e  g.  ** this tran
1411a 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 70 61 67  saction, the pag
1411b 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e  er is running in
1411c 20 65 78 63 6c 75 73 69 76 65 2d 6d 6f 64 65 20   exclusive-mode 
1411d 61 6e 64 20 69 73 0a 20 20 2a 2a 20 75 73 69 6e  and is.  ** usin
1411e 67 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75  g persistent jou
1411f 72 6e 61 6c 73 2c 20 74 68 65 6e 20 74 68 69 73  rnals, then this
14120 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
14121 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  o-op..  **.  ** 
14122 54 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  The start of the
14123 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 63 75   journal file cu
14124 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73  rrently contains
14125 20 61 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61   a single journa
14126 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72 20 77  l .  ** header w
14127 69 74 68 20 74 68 65 20 6e 52 65 63 20 66 69 65  ith the nRec fie
14128 6c 64 20 73 65 74 20 74 6f 20 30 2e 20 49 66 20  ld set to 0. If 
14129 73 75 63 68 20 61 20 6a 6f 75 72 6e 61 6c 20 69  such a journal i
1412a 73 20 75 73 65 64 20 61 73 0a 20 20 2a 2a 20 61  s used as.  ** a
1412b 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 64 75 72   hot-journal dur
1412c 69 6e 67 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  ing hot-journal 
1412d 72 6f 6c 6c 62 61 63 6b 2c 20 30 20 63 68 61 6e  rollback, 0 chan
1412e 67 65 73 20 77 69 6c 6c 20 62 65 20 6d 61 64 65  ges will be made
1412f 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 64 61 74  .  ** to the dat
14130 61 62 61 73 65 20 66 69 6c 65 2e 20 53 6f 20 74  abase file. So t
14131 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20  here is no need 
14132 74 6f 20 7a 65 72 6f 20 74 68 65 20 6a 6f 75 72  to zero the jour
14133 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64 65 72  nal .  ** header
14134 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61 67 65  . Since the page
14135 72 20 69 73 20 69 6e 20 65 78 63 6c 75 73 69 76  r is in exclusiv
14136 65 20 6d 6f 64 65 2c 20 74 68 65 72 65 20 69 73  e mode, there is
14137 20 6e 6f 20 6e 65 65 64 0a 20 20 2a 2a 20 74 6f   no need.  ** to
14138 20 64 72 6f 70 20 61 6e 79 20 6c 6f 63 6b 73 20   drop any locks 
14139 65 69 74 68 65 72 2e 0a 20 20 2a 2f 0a 20 20 69  either..  */.  i
1413a 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64  f( pPager->dbMod
1413b 69 66 69 65 64 3d 3d 30 20 26 26 20 70 50 61 67  ified==0 && pPag
1413c 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
1413d 65 20 0a 20 20 20 26 26 20 70 50 61 67 65 72 2d  e .   && pPager-
1413e 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
1413f 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
14140 50 45 52 53 49 53 54 0a 20 20 29 7b 0a 20 20 20  PERSIST.  ){.   
14141 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
14142 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55  >journalOff==JOU
14143 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
14144 65 72 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72  er) );.    retur
14145 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
14146 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28  ..  PAGERTRACE((
14147 22 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c 20 50  "COMMIT %d\n", P
14148 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 29  AGERID(pPager)))
14149 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
1414a 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
1414b 5f 53 59 4e 43 45 44 20 7c 7c 20 4d 45 4d 44 42  _SYNCED || MEMDB
1414c 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 64 62 4d   || !pPager->dbM
1414d 6f 64 69 66 69 65 64 20 29 3b 0a 20 20 72 63 20  odified );.  rc 
1414e 3d 20 70 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e  = pager_end_tran
1414f 73 61 63 74 69 6f 6e 28 70 50 61 67 65 72 2c 20  saction(pPager, 
14150 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65  pPager->setMaste
14151 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 61 67  r);.  return pag
14152 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
14153 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   rc);.}../*.** R
14154 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61 6e  ollback all chan
14155 67 65 73 2e 20 54 68 65 20 64 61 74 61 62 61 73  ges. The databas
14156 65 20 66 61 6c 6c 73 20 62 61 63 6b 20 74 6f 20  e falls back to 
14157 50 41 47 45 52 5f 53 48 41 52 45 44 20 6d 6f 64  PAGER_SHARED mod
14158 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  e..**.** This fu
14159 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20  nction performs 
1415a 74 77 6f 20 74 61 73 6b 73 3a 0a 2a 2a 0a 2a 2a  two tasks:.**.**
1415b 20 20 20 31 29 20 49 74 20 72 6f 6c 6c 73 20 62     1) It rolls b
1415c 61 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ack the journal 
1415d 66 69 6c 65 2c 20 72 65 73 74 6f 72 69 6e 67 20  file, restoring 
1415e 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  all database fil
1415f 65 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 69  e and .**      i
14160 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 70  n-memory cache p
14161 61 67 65 73 20 74 6f 20 74 68 65 20 73 74 61 74  ages to the stat
14162 65 20 74 68 65 79 20 77 65 72 65 20 69 6e 20 77  e they were in w
14163 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74  hen the transact
14164 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 77 61 73 20  ion.**      was 
14165 6f 70 65 6e 65 64 2c 20 61 6e 64 0a 2a 2a 20 20  opened, and.**  
14166 20 32 29 20 49 74 20 66 69 6e 61 6c 69 7a 65 73   2) It finalizes
14167 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
14168 65 2c 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  e, so that it is
14169 20 6e 6f 74 20 75 73 65 64 20 66 6f 72 20 68 6f   not used for ho
1416a 74 0a 2a 2a 20 20 20 20 20 20 72 6f 6c 6c 62 61  t.**      rollba
1416b 63 6b 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20  ck at any point 
1416c 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 2a  in the future..*
1416d 2a 0a 2a 2a 20 73 75 62 6a 65 63 74 20 74 6f 20  *.** subject to 
1416e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75  the following qu
1416f 61 6c 69 66 69 63 61 74 69 6f 6e 73 3a 0a 2a 2a  alifications:.**
14170 0a 2a 2a 20 2a 20 49 66 20 74 68 65 20 6a 6f 75  .** * If the jou
14171 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74  rnal file is not
14172 20 79 65 74 20 6f 70 65 6e 20 77 68 65 6e 20 74   yet open when t
14173 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
14174 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20 20 74 68 65  called,.**   the
14175 6e 20 6f 6e 6c 79 20 28 32 29 20 69 73 20 70 65  n only (2) is pe
14176 72 66 6f 72 6d 65 64 2e 20 49 6e 20 74 68 69 73  rformed. In this
14177 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 6e   case there is n
14178 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a  o journal file.*
14179 2a 20 20 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b  *   to roll back
1417a 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 69 6e 20  ..**.** * If in 
1417b 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20 6f  an error state o
1417c 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ther than SQLITE
1417d 5f 46 55 4c 4c 2c 20 74 68 65 6e 20 74 61 73 6b  _FULL, then task
1417e 20 28 31 29 20 69 73 20 0a 2a 2a 20 20 20 70 65   (1) is .**   pe
1417f 72 66 6f 72 6d 65 64 2e 20 49 66 20 73 75 63 63  rformed. If succ
14180 65 73 73 66 75 6c 2c 20 74 61 73 6b 20 28 32 29  essful, task (2)
14181 2e 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20  . Regardless of 
14182 74 68 65 20 6f 75 74 63 6f 6d 65 0a 2a 2a 20 20  the outcome.**  
14183 20 6f 66 20 65 69 74 68 65 72 2c 20 74 68 65 20   of either, the 
14184 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f  error state erro
14185 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
14186 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ed to the caller
14187 0a 2a 2a 20 20 20 28 69 2e 65 2e 20 65 69 74 68  .**   (i.e. eith
14188 65 72 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  er SQLITE_IOERR 
14189 6f 72 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  or SQLITE_CORRUP
1418a 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74  T)..**.** * If t
1418b 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 50  he pager is in P
1418c 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 73 74  AGER_RESERVED st
1418d 61 74 65 2c 20 74 68 65 6e 20 61 74 74 65 6d 70  ate, then attemp
1418e 74 20 28 31 29 2e 20 57 68 65 74 68 65 72 0a 2a  t (1). Whether.*
1418f 2a 20 20 20 6f 72 20 6e 6f 74 20 28 31 29 20 69  *   or not (1) i
14190 73 20 73 75 63 63 75 73 73 66 75 6c 2c 20 61 6c  s succussful, al
14191 73 6f 20 61 74 74 65 6d 70 74 20 28 32 29 2e 20  so attempt (2). 
14192 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  If successful, r
14193 65 74 75 72 6e 0a 2a 2a 20 20 20 53 51 4c 49 54  eturn.**   SQLIT
14194 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c  E_OK. Otherwise,
14195 20 65 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72   enter the error
14196 20 73 74 61 74 65 20 61 6e 64 20 72 65 74 75 72   state and retur
14197 6e 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a 20  n the first .** 
14198 20 20 65 72 72 6f 72 20 63 6f 64 65 20 65 6e 63    error code enc
14199 6f 75 6e 74 65 72 65 64 2e 20 0a 2a 2a 0a 2a 2a  ountered. .**.**
1419a 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20     In this case 
1419b 74 68 65 72 65 20 69 73 20 6e 6f 20 63 68 61 6e  there is no chan
1419c 63 65 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ce that the data
1419d 62 61 73 65 20 77 61 73 20 77 72 69 74 74 65 6e  base was written
1419e 20 74 6f 2e 20 0a 2a 2a 20 20 20 53 6f 20 69 73   to. .**   So is
1419f 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a   safe to finaliz
141a0 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
141a1 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 70  le even if the p
141a2 6c 61 79 62 61 63 6b 20 0a 2a 2a 20 20 20 28 6f  layback .**   (o
141a3 70 65 72 61 74 69 6f 6e 20 31 29 20 66 61 69 6c  peration 1) fail
141a4 65 64 2e 20 48 6f 77 65 76 65 72 20 74 68 65 20  ed. However the 
141a5 70 61 67 65 72 20 6d 75 73 74 20 65 6e 74 65 72  pager must enter
141a6 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
141a7 0a 2a 2a 20 20 20 61 73 20 74 68 65 20 63 6f 6e  .**   as the con
141a8 74 65 6e 74 73 20 6f 66 20 74 68 65 20 69 6e 2d  tents of the in-
141a9 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 61 72 65  memory cache are
141aa 20 6e 6f 77 20 73 75 73 70 65 63 74 2e 0a 2a 2a   now suspect..**
141ab 0a 2a 2a 20 2a 20 46 69 6e 61 6c 6c 79 2c 20 69  .** * Finally, i
141ac 66 20 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55  f in PAGER_EXCLU
141ad 53 49 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e  SIVE state, then
141ae 20 61 74 74 65 6d 70 74 20 28 31 29 2e 20 4f 6e   attempt (1). On
141af 6c 79 0a 2a 2a 20 20 20 61 74 74 65 6d 70 74 20  ly.**   attempt 
141b0 28 32 29 20 69 66 20 28 31 29 20 69 73 20 73 75  (2) if (1) is su
141b1 63 63 65 73 73 66 75 6c 2e 20 52 65 74 75 72 6e  ccessful. Return
141b2 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75   SQLITE_OK if su
141b3 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 20 20 6f  ccessful,.**   o
141b4 74 68 65 72 77 69 73 65 20 65 6e 74 65 72 20 74  therwise enter t
141b5 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 61  he error state a
141b6 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  nd return the er
141b7 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 74 68  ror code from th
141b8 65 20 0a 2a 2a 20 20 20 66 61 69 6c 69 6e 67 20  e .**   failing 
141b9 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  operation..**.**
141ba 20 20 20 49 6e 20 74 68 69 73 20 63 61 73 65 20     In this case 
141bb 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
141bc 65 20 6d 61 79 20 68 61 76 65 20 62 65 65 6e 20  e may have been 
141bd 77 72 69 74 74 65 6e 20 74 6f 2e 20 53 6f 20 69  written to. So i
141be 66 20 74 68 65 0a 2a 2a 20 20 20 70 6c 61 79 62  f the.**   playb
141bf 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 20 64 69  ack operation di
141c0 64 20 6e 6f 74 20 73 75 63 63 65 65 64 20 69 74  d not succeed it
141c1 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 61   would not be sa
141c2 66 65 20 74 6f 20 66 69 6e 61 6c 69 7a 65 0a 2a  fe to finalize.*
141c3 2a 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  *   the journal 
141c4 66 69 6c 65 2e 20 49 74 20 6e 65 65 64 73 20 74  file. It needs t
141c5 6f 20 62 65 20 6c 65 66 74 20 69 6e 20 74 68 65  o be left in the
141c6 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 73 6f 20   file-system so 
141c7 74 68 61 74 0a 2a 2a 20 20 20 73 6f 6d 65 20 6f  that.**   some o
141c8 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 61 6e  ther process can
141c9 20 75 73 65 20 69 74 20 74 6f 20 72 65 73 74 6f   use it to resto
141ca 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  re the database 
141cb 73 74 61 74 65 20 28 62 79 0a 2a 2a 20 20 20 68  state (by.**   h
141cc 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62  ot-journal rollb
141cd 61 63 6b 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ack)..*/.SQLITE_
141ce 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
141cf 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
141d0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
141d1 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
141d2 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
141d3 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
141d4 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 50 41 47 45  n code */.  PAGE
141d5 52 54 52 41 43 45 28 28 22 52 4f 4c 4c 42 41 43  RTRACE(("ROLLBAC
141d6 4b 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  K %d\n", PAGERID
141d7 28 70 50 61 67 65 72 29 29 29 3b 0a 20 20 69 66  (pPager)));.  if
141d8 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64  ( !pPager->dbMod
141d9 69 66 69 65 64 20 7c 7c 20 21 69 73 4f 70 65 6e  ified || !isOpen
141da 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
141db 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f  .    rc = pager_
141dc 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28  end_transaction(
141dd 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e  pPager, pPager->
141de 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 7d 65  setMaster);.  }e
141df 6c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e  lse if( pPager->
141e0 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65  errCode && pPage
141e1 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49  r->errCode!=SQLI
141e2 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 69  TE_FULL ){.    i
141e3 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  f( pPager->state
141e4 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  >=PAGER_EXCLUSIV
141e5 45 20 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72  E ){.      pager
141e6 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72  _playback(pPager
141e7 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
141e8 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72  rc = pPager->err
141e9 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  Code;.  }else{. 
141ea 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73     if( pPager->s
141eb 74 61 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45  tate==PAGER_RESE
141ec 52 56 45 44 20 29 7b 0a 20 20 20 20 20 20 69 6e  RVED ){.      in
141ed 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20  t rc2;.      rc 
141ee 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  = pager_playback
141ef 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20  (pPager, 0);.   
141f0 20 20 20 72 63 32 20 3d 20 70 61 67 65 72 5f 65     rc2 = pager_e
141f1 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70  nd_transaction(p
141f2 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73  Pager, pPager->s
141f3 65 74 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  etMaster);.     
141f4 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
141f5 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
141f6 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a   = rc2;.      }.
141f7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
141f8 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79   rc = pager_play
141f9 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b  back(pPager, 0);
141fa 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
141fb 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20  !MEMDB ){.      
141fc 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61  pPager->dbSizeVa
141fd 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a  lid = 0;.    }..
141fe 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72      /* If an err
141ff 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
14200 20 61 20 52 4f 4c 4c 42 41 43 4b 2c 20 77 65 20   a ROLLBACK, we 
14201 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72  can no longer tr
14202 75 73 74 20 74 68 65 20 70 61 67 65 72 0a 20 20  ust the pager.  
14203 20 20 2a 2a 20 63 61 63 68 65 2e 20 53 6f 20 63    ** cache. So c
14204 61 6c 6c 20 70 61 67 65 72 5f 65 72 72 6f 72 28  all pager_error(
14205 29 20 6f 6e 20 74 68 65 20 77 61 79 20 6f 75 74  ) on the way out
14206 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20 65 72 72   to make any err
14207 6f 72 20 0a 20 20 20 20 2a 2a 20 70 65 72 73 69  or .    ** persi
14208 73 74 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  stent..    */.  
14209 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72    rc = pager_err
1420a 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a  or(pPager, rc);.
1420b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1420c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1420d 20 54 52 55 45 20 69 66 20 74 68 65 20 64 61 74   TRUE if the dat
1420e 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70  abase file is op
1420f 65 6e 65 64 20 72 65 61 64 2d 6f 6e 6c 79 2e 20  ened read-only. 
14210 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a   Return FALSE.**
14211 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
14212 20 69 73 20 28 69 6e 20 74 68 65 6f 72 79 29 20   is (in theory) 
14213 77 72 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  writable..*/.SQL
14214 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73  ITE_PRIVATE u8 s
14215 71 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61  qlite3PagerIsrea
14216 64 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70 50 61  donly(Pager *pPa
14217 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  ger){.  return p
14218 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b  Pager->readOnly;
14219 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1421a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
1421b 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65  eferences to the
1421c 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54   pager..*/.SQLIT
1421d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1421e 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
1421f 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nt(Pager *pPager
14220 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
14221 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e  te3PcacheRefCoun
14222 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
14223 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  e);.}../*.** Ret
14224 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
14225 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  f references to 
14226 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61  the specified pa
14227 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ge..*/.SQLITE_PR
14228 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
14229 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75  3PagerPageRefcou
1422a 6e 74 28 44 62 50 61 67 65 20 2a 70 50 61 67 65  nt(DbPage *pPage
1422b 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
1422c 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66  te3PcachePageRef
1422d 63 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a 7d 0a  count(pPage);.}.
1422e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
1422f 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  EST./*.** This r
14230 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66  outine is used f
14231 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61  or testing and a
14232 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f  nalysis only..*/
14233 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14234 69 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65  int *sqlite3Page
14235 72 53 74 61 74 73 28 50 61 67 65 72 20 2a 70 50  rStats(Pager *pP
14236 61 67 65 72 29 7b 0a 20 20 73 74 61 74 69 63 20  ager){.  static 
14237 69 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61 5b 30  int a[11];.  a[0
14238 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68  ] = sqlite3Pcach
14239 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72  eRefCount(pPager
1423a 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b  ->pPCache);.  a[
1423b 31 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63  1] = sqlite3Pcac
1423c 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  hePagecount(pPag
1423d 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20  er->pPCache);.  
1423e 61 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33 50 63  a[2] = sqlite3Pc
1423f 61 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65  acheGetCachesize
14240 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
14241 29 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50 61 67  );.  a[3] = pPag
14242 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20  er->dbSizeValid 
14243 3f 20 28 69 6e 74 29 20 70 50 61 67 65 72 2d 3e  ? (int) pPager->
14244 64 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20 20 61  dbSize : -1;.  a
14245 5b 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e 73 74  [4] = pPager->st
14246 61 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20 70 50  ate;.  a[5] = pP
14247 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20  ager->errCode;. 
14248 20 61 5b 36 5d 20 3d 20 70 50 61 67 65 72 2d 3e   a[6] = pPager->
14249 6e 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d 20 70  nHit;.  a[7] = p
1424a 50 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a 20 20  Pager->nMiss;.  
1424b 61 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20 55 73  a[8] = 0;  /* Us
1424c 65 64 20 74 6f 20 62 65 20 70 50 61 67 65 72 2d  ed to be pPager-
1424d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b 39 5d  >nOvfl */.  a[9]
1424e 20 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 61 64   = pPager->nRead
1424f 3b 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50 61 67  ;.  a[10] = pPag
14250 65 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20 72 65  er->nWrite;.  re
14251 74 75 72 6e 20 61 3b 0a 7d 0a 23 65 6e 64 69 66  turn a;.}.#endif
14252 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
14253 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 61  rue if this is a
14254 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65  n in-memory page
14255 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
14256 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
14257 50 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67  PagerIsMemdb(Pag
14258 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
14259 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a 7d 0a 0a  eturn MEMDB;.}..
1425a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74  /*.** Check that
1425b 20 74 68 65 72 65 20 61 72 65 20 61 74 20 6c 65   there are at le
1425c 61 73 74 20 6e 53 61 76 65 70 6f 69 6e 74 20 73  ast nSavepoint s
1425d 61 76 65 70 6f 69 6e 74 73 20 6f 70 65 6e 2e 20  avepoints open. 
1425e 49 66 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20  If there are.** 
1425f 63 75 72 72 65 6e 74 6c 79 20 6c 65 73 73 20 74  currently less t
14260 68 61 6e 20 6e 53 61 76 65 70 6f 69 6e 74 73 20  han nSavepoints 
14261 6f 70 65 6e 2c 20 74 68 65 6e 20 6f 70 65 6e 20  open, then open 
14262 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 61 76 65  one or more save
14263 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 6d 61 6b  points.** to mak
14264 65 20 75 70 20 74 68 65 20 64 69 66 66 65 72 65  e up the differe
14265 6e 63 65 2e 20 49 66 20 74 68 65 20 6e 75 6d 62  nce. If the numb
14266 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 73  er of savepoints
14267 20 69 73 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65   is already.** e
14268 71 75 61 6c 20 74 6f 20 6e 53 61 76 65 70 6f 69  qual to nSavepoi
14269 6e 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75  nt, then this fu
1426a 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f  nction is a no-o
1426b 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65  p..**.** If a me
1426c 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
1426d 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f 4e 4f  fails, SQLITE_NO
1426e 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2e  MEM is returned.
1426f 20 49 66 20 61 6e 20 65 72 72 6f 72 20 0a 2a 2a   If an error .**
14270 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 6f 70   occurs while op
14271 65 6e 69 6e 67 20 74 68 65 20 73 75 62 2d 6a 6f  ening the sub-jo
14272 75 72 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 6e  urnal file, then
14273 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64   an IO error cod
14274 65 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  e is.** returned
14275 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c  . Otherwise, SQL
14276 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54  ITE_OK..*/.SQLIT
14277 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14278 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61  lite3PagerOpenSa
14279 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
1427a 50 61 67 65 72 2c 20 69 6e 74 20 6e 53 61 76 65  Pager, int nSave
1427b 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63  point){.  int rc
1427c 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20   = SQLITE_OK;   
1427d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1427e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
1427f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72  de */.  int nCur
14280 72 65 6e 74 20 3d 20 70 50 61 67 65 72 2d 3e 6e  rent = pPager->n
14281 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 20 20  Savepoint;      
14282 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d    /* Current num
14283 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69 6e 74  ber of savepoint
14284 73 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 53 61 76  s */..  if( nSav
14285 65 70 6f 69 6e 74 3e 6e 43 75 72 72 65 6e 74 20  epoint>nCurrent 
14286 26 26 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f  && pPager->useJo
14287 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74  urnal ){.    int
14288 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20 20   ii;            
14289 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1428a 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
1428b 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20   variable */.   
1428c 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20   PagerSavepoint 
1428d 2a 61 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20  *aNew;          
1428e 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20           /* New 
1428f 50 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74  Pager.aSavepoint
14290 20 61 72 72 61 79 20 2a 2f 0a 0a 20 20 20 20 2f   array */..    /
14291 2a 20 45 69 74 68 65 72 20 74 68 65 72 65 20 69  * Either there i
14292 73 20 6e 6f 20 61 63 74 69 76 65 20 6a 6f 75 72  s no active jour
14293 6e 61 6c 20 6f 72 20 74 68 65 20 73 75 62 2d 6a  nal or the sub-j
14294 6f 75 72 6e 61 6c 20 69 73 20 6f 70 65 6e 20 6f  ournal is open o
14295 72 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f  r .    ** the jo
14296 75 72 6e 61 6c 20 69 73 20 61 6c 77 61 79 73 20  urnal is always 
14297 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
14298 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
14299 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69  pPager->nSavepoi
1429a 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f 70 65 6e 28  nt==0 || isOpen(
1429b 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20 7c 7c  pPager->sjfd) ||
1429c 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61  .            pPa
1429d 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
1429e 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
1429f 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 0a 20  ODE_MEMORY );.. 
142a0 20 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 20 50     /* Grow the P
142a1 61 67 65 72 2e 61 53 61 76 65 70 6f 69 6e 74 20  ager.aSavepoint 
142a2 61 72 72 61 79 20 75 73 69 6e 67 20 72 65 61 6c  array using real
142a3 6c 6f 63 28 29 2e 20 52 65 74 75 72 6e 20 53 51  loc(). Return SQ
142a4 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 20 20 2a  LITE_NOMEM.    *
142a5 2a 20 69 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  * if the allocat
142a6 69 6f 6e 20 66 61 69 6c 73 2e 20 4f 74 68 65 72  ion fails. Other
142a7 77 69 73 65 2c 20 7a 65 72 6f 20 74 68 65 20 6e  wise, zero the n
142a8 65 77 20 70 6f 72 74 69 6f 6e 20 69 6e 20 63 61  ew portion in ca
142a9 73 65 20 61 20 0a 20 20 20 20 2a 2a 20 6d 61 6c  se a .    ** mal
142aa 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75  loc failure occu
142ab 72 73 20 77 68 69 6c 65 20 70 6f 70 75 6c 61 74  rs while populat
142ac 69 6e 67 20 69 74 20 69 6e 20 74 68 65 20 66 6f  ing it in the fo
142ad 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20 62 65 6c 6f  r(...) loop belo
142ae 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 4e  w..    */.    aN
142af 65 77 20 3d 20 28 50 61 67 65 72 53 61 76 65 70  ew = (PagerSavep
142b0 6f 69 6e 74 20 2a 29 73 71 6c 69 74 65 33 52 65  oint *)sqlite3Re
142b1 61 6c 6c 6f 63 28 0a 20 20 20 20 20 20 20 20 70  alloc(.        p
142b2 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e  Pager->aSavepoin
142b3 74 2c 20 73 69 7a 65 6f 66 28 50 61 67 65 72 53  t, sizeof(PagerS
142b4 61 76 65 70 6f 69 6e 74 29 2a 6e 53 61 76 65 70  avepoint)*nSavep
142b5 6f 69 6e 74 0a 20 20 20 20 29 3b 0a 20 20 20 20  oint.    );.    
142b6 69 66 28 20 21 61 4e 65 77 20 29 7b 0a 20 20 20  if( !aNew ){.   
142b7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
142b8 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
142b9 20 20 6d 65 6d 73 65 74 28 26 61 4e 65 77 5b 6e    memset(&aNew[n
142ba 43 75 72 72 65 6e 74 5d 2c 20 30 2c 20 28 6e 53  Current], 0, (nS
142bb 61 76 65 70 6f 69 6e 74 2d 6e 43 75 72 72 65 6e  avepoint-nCurren
142bc 74 29 20 2a 20 73 69 7a 65 6f 66 28 50 61 67 65  t) * sizeof(Page
142bd 72 53 61 76 65 70 6f 69 6e 74 29 29 3b 0a 20 20  rSavepoint));.  
142be 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70    pPager->aSavep
142bf 6f 69 6e 74 20 3d 20 61 4e 65 77 3b 0a 20 20 20  oint = aNew;.   
142c0 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f   pPager->nSavepo
142c1 69 6e 74 20 3d 20 6e 53 61 76 65 70 6f 69 6e 74  int = nSavepoint
142c2 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61  ;..    /* Popula
142c3 74 65 20 74 68 65 20 50 61 67 65 72 53 61 76 65  te the PagerSave
142c4 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72 65 73  point structures
142c5 20 6a 75 73 74 20 61 6c 6c 6f 63 61 74 65 64 2e   just allocated.
142c6 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e   */.    for(ii=n
142c7 43 75 72 72 65 6e 74 3b 20 69 69 3c 6e 53 61 76  Current; ii<nSav
142c8 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  epoint; ii++){. 
142c9 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
142ca 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
142cb 20 29 3b 0a 20 20 20 20 20 20 61 4e 65 77 5b 69   );.      aNew[i
142cc 69 5d 2e 6e 4f 72 69 67 20 3d 20 70 50 61 67 65  i].nOrig = pPage
142cd 72 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 20  r->dbSize;.     
142ce 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61 67   if( isOpen(pPag
142cf 65 72 2d 3e 6a 66 64 29 20 26 26 20 41 4c 57 41  er->jfd) && ALWA
142d0 59 53 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  YS(pPager->journ
142d1 61 6c 4f 66 66 3e 30 29 20 29 7b 0a 20 20 20 20  alOff>0) ){.    
142d2 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66      aNew[ii].iOf
142d3 66 73 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a  fset = pPager->j
142d4 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 20  ournalOff;.     
142d5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
142d6 61 4e 65 77 5b 69 69 5d 2e 69 4f 66 66 73 65 74  aNew[ii].iOffset
142d7 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53   = JOURNAL_HDR_S
142d8 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  Z(pPager);.     
142d9 20 7d 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69   }.      aNew[ii
142da 5d 2e 69 53 75 62 52 65 63 20 3d 20 70 50 61 67  ].iSubRec = pPag
142db 65 72 2d 3e 6e 53 75 62 52 65 63 3b 0a 20 20 20  er->nSubRec;.   
142dc 20 20 20 61 4e 65 77 5b 69 69 5d 2e 70 49 6e 53     aNew[ii].pInS
142dd 61 76 65 70 6f 69 6e 74 20 3d 20 73 71 6c 69 74  avepoint = sqlit
142de 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 70  e3BitvecCreate(p
142df 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a  Pager->dbSize);.
142e0 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 77 5b        if( !aNew[
142e1 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74  ii].pInSavepoint
142e2 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
142e3 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
142e4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
142e5 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20      /* Open the 
142e6 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c 20 69 66 20  sub-journal, if 
142e7 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  it is not alread
142e8 79 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 20  y opened. */.   
142e9 20 72 63 20 3d 20 6f 70 65 6e 53 75 62 4a 6f 75   rc = openSubJou
142ea 72 6e 61 6c 28 70 50 61 67 65 72 29 3b 0a 20 20  rnal(pPager);.  
142eb 20 20 61 73 73 65 72 74 54 72 75 6e 63 61 74 65    assertTruncate
142ec 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 67 65  Constraint(pPage
142ed 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  r);.  }..  retur
142ee 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
142ef 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
142f0 63 61 6c 6c 65 64 20 74 6f 20 72 6f 6c 6c 62 61  called to rollba
142f1 63 6b 20 6f 72 20 72 65 6c 65 61 73 65 20 28 63  ck or release (c
142f2 6f 6d 6d 69 74 29 20 61 20 73 61 76 65 70 6f 69  ommit) a savepoi
142f3 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 61 76 65 70  nt..** The savep
142f4 6f 69 6e 74 20 74 6f 20 72 65 6c 65 61 73 65 20  oint to release 
142f5 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64  or rollback need
142f6 20 6e 6f 74 20 62 65 20 74 68 65 20 6d 6f 73 74   not be the most
142f7 20 72 65 63 65 6e 74 6c 79 20 0a 2a 2a 20 63 72   recently .** cr
142f8 65 61 74 65 64 20 73 61 76 65 70 6f 69 6e 74 2e  eated savepoint.
142f9 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  .**.** Parameter
142fa 20 6f 70 20 69 73 20 61 6c 77 61 79 73 20 65 69   op is always ei
142fb 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54 5f 52  ther SAVEPOINT_R
142fc 4f 4c 4c 42 41 43 4b 20 6f 72 20 53 41 56 45 50  OLLBACK or SAVEP
142fd 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 0a 2a 2a  OINT_RELEASE..**
142fe 20 49 66 20 69 74 20 69 73 20 53 41 56 45 50 4f   If it is SAVEPO
142ff 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74 68 65  INT_RELEASE, the
14300 6e 20 72 65 6c 65 61 73 65 20 61 6e 64 20 64 65  n release and de
14301 73 74 72 6f 79 20 74 68 65 20 73 61 76 65 70 6f  stroy the savepo
14302 69 6e 74 20 77 69 74 68 0a 2a 2a 20 69 6e 64 65  int with.** inde
14303 78 20 69 53 61 76 65 70 6f 69 6e 74 2e 20 49 66  x iSavepoint. If
14304 20 69 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54   it is SAVEPOINT
14305 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20  _ROLLBACK, then 
14306 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20 63 68 61  rollback all cha
14307 6e 67 65 73 0a 2a 2a 20 74 68 61 74 20 68 61 76  nges.** that hav
14308 65 20 6f 63 63 75 72 72 65 64 20 73 69 6e 63 65  e occurred since
14309 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 73   the specified s
1430a 61 76 65 70 6f 69 6e 74 20 77 61 73 20 63 72 65  avepoint was cre
1430b 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ated..**.** The 
1430c 73 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 6f 6c  savepoint to rol
1430d 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73 65  lback or release
1430e 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62   is identified b
1430f 79 20 70 61 72 61 6d 65 74 65 72 20 0a 2a 2a 20  y parameter .** 
14310 69 53 61 76 65 70 6f 69 6e 74 2e 20 41 20 76 61  iSavepoint. A va
14311 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 20 74  lue of 0 means t
14312 6f 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 68 65  o operate on the
14313 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70   outermost savep
14314 6f 69 6e 74 0a 2a 2a 20 28 74 68 65 20 66 69 72  oint.** (the fir
14315 73 74 20 63 72 65 61 74 65 64 29 2e 20 41 20 76  st created). A v
14316 61 6c 75 65 20 6f 66 20 28 50 61 67 65 72 2e 6e  alue of (Pager.n
14317 53 61 76 65 70 6f 69 6e 74 2d 31 29 20 6d 65 61  Savepoint-1) mea
14318 6e 73 20 6f 70 65 72 61 74 65 0a 2a 2a 20 6f 6e  ns operate.** on
14319 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
1431a 6c 79 20 63 72 65 61 74 65 64 20 73 61 76 65 70  ly created savep
1431b 6f 69 6e 74 2e 20 49 66 20 69 53 61 76 65 70 6f  oint. If iSavepo
1431c 69 6e 74 20 69 73 20 67 72 65 61 74 65 72 20 74  int is greater t
1431d 68 61 6e 0a 2a 2a 20 28 50 61 67 65 72 2e 6e 53  han.** (Pager.nS
1431e 61 76 65 70 6f 69 6e 74 2d 31 29 2c 20 74 68 65  avepoint-1), the
1431f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
14320 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
14321 2a 20 49 66 20 61 20 6e 65 67 61 74 69 76 65 20  * If a negative 
14322 76 61 6c 75 65 20 69 73 20 70 61 73 73 65 64 20  value is passed 
14323 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
14324 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 72 65  , then the curre
14325 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  nt.** transactio
14326 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n is rolled back
14327 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 65 72  . This is differ
14328 65 6e 74 20 74 6f 20 63 61 6c 6c 69 6e 67 20 0a  ent to calling .
14329 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  ** sqlite3PagerR
1432a 6f 6c 6c 62 61 63 6b 28 29 20 62 65 63 61 75 73  ollback() becaus
1432b 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  e this function 
1432c 64 6f 65 73 20 6e 6f 74 20 74 65 72 6d 69 6e 61  does not termina
1432d 74 65 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61  te.** the transa
1432e 63 74 69 6f 6e 20 6f 72 20 75 6e 6c 6f 63 6b 20  ction or unlock 
1432f 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 69 74  the database, it
14330 20 6a 75 73 74 20 72 65 73 74 6f 72 65 73 20 74   just restores t
14331 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20  he .** contents 
14332 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
14333 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20  to its original 
14334 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 6e  state. .**.** In
14335 20 61 6e 79 20 63 61 73 65 2c 20 61 6c 6c 20 73   any case, all s
14336 61 76 65 70 6f 69 6e 74 73 20 77 69 74 68 20 61  avepoints with a
14337 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 72 20  n index greater 
14338 74 68 61 6e 20 69 53 61 76 65 70 6f 69 6e 74 20  than iSavepoint 
14339 0a 2a 2a 20 61 72 65 20 64 65 73 74 72 6f 79 65  .** are destroye
1433a 64 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 20  d. If this is a 
1433b 72 65 6c 65 61 73 65 20 6f 70 65 72 61 74 69 6f  release operatio
1433c 6e 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54  n (op==SAVEPOINT
1433d 5f 52 45 4c 45 41 53 45 29 2c 0a 2a 2a 20 74 68  _RELEASE),.** th
1433e 65 6e 20 73 61 76 65 70 6f 69 6e 74 20 69 53 61  en savepoint iSa
1433f 76 65 70 6f 69 6e 74 20 69 73 20 61 6c 73 6f 20  vepoint is also 
14340 64 65 73 74 72 6f 79 65 64 2e 0a 2a 2a 0a 2a 2a  destroyed..**.**
14341 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
14342 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ay return SQLITE
14343 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65 6d 6f  _NOMEM if a memo
14344 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
14345 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61 6e 20 49 4f  ils,.** or an IO
14346 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61   error code if a
14347 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72  n IO error occur
14348 73 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 20  s while rolling 
14349 62 61 63 6b 20 61 20 0a 2a 2a 20 73 61 76 65 70  back a .** savep
1434a 6f 69 6e 74 2e 20 49 66 20 6e 6f 20 65 72 72 6f  oint. If no erro
1434b 72 73 20 6f 63 63 75 72 2c 20 53 51 4c 49 54 45  rs occur, SQLITE
1434c 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
1434d 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56  .*/ .SQLITE_PRIV
1434e 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
1434f 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 50 61  agerSavepoint(Pa
14350 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
14351 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65 70 6f   op, int iSavepo
14352 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  int){.  int rc =
14353 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61   SQLITE_OK;..  a
14354 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 45 50  ssert( op==SAVEP
14355 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20  OINT_RELEASE || 
14356 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  op==SAVEPOINT_RO
14357 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 61 73 73 65  LLBACK );.  asse
14358 72 74 28 20 69 53 61 76 65 70 6f 69 6e 74 3e 3d  rt( iSavepoint>=
14359 30 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49  0 || op==SAVEPOI
1435a 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 0a  NT_ROLLBACK );..
1435b 20 20 69 66 28 20 69 53 61 76 65 70 6f 69 6e 74    if( iSavepoint
1435c 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f  <pPager->nSavepo
1435d 69 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  int ){.    int i
1435e 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  i;            /*
1435f 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
14360 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 4e  le */.    int nN
14361 65 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ew;          /* 
14362 4e 75 6d 62 65 72 20 6f 66 20 72 65 6d 61 69 6e  Number of remain
14363 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 73 20 61  ing savepoints a
14364 66 74 65 72 20 74 68 69 73 20 6f 70 2e 20 2a 2f  fter this op. */
14365 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20  ..    /* Figure 
14366 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 73 61 76  out how many sav
14367 65 70 6f 69 6e 74 73 20 77 69 6c 6c 20 73 74 69  epoints will sti
14368 6c 6c 20 62 65 20 61 63 74 69 76 65 20 61 66 74  ll be active aft
14369 65 72 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 6f  er this.    ** o
1436a 70 65 72 61 74 69 6f 6e 2e 20 53 74 6f 72 65 20  peration. Store 
1436b 74 68 69 73 20 76 61 6c 75 65 20 69 6e 20 6e 4e  this value in nN
1436c 65 77 2e 20 54 68 65 6e 20 66 72 65 65 20 72 65  ew. Then free re
1436d 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61 74  sources associat
1436e 65 64 20 0a 20 20 20 20 2a 2a 20 77 69 74 68 20  ed .    ** with 
1436f 61 6e 79 20 73 61 76 65 70 6f 69 6e 74 73 20 74  any savepoints t
14370 68 61 74 20 61 72 65 20 64 65 73 74 72 6f 79 65  hat are destroye
14371 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74  d by this operat
14372 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ion..    */.    
14373 6e 4e 65 77 20 3d 20 69 53 61 76 65 70 6f 69 6e  nNew = iSavepoin
14374 74 20 2b 20 28 6f 70 3d 3d 53 41 56 45 50 4f 49  t + (op==SAVEPOI
14375 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29 3b 0a 20 20  NT_ROLLBACK);.  
14376 20 20 66 6f 72 28 69 69 3d 6e 4e 65 77 3b 20 69    for(ii=nNew; i
14377 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70  i<pPager->nSavep
14378 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  oint; ii++){.   
14379 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63     sqlite3Bitvec
1437a 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e  Destroy(pPager->
1437b 61 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 2e 70  aSavepoint[ii].p
1437c 49 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20  InSavepoint);.  
1437d 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e    }.    pPager->
1437e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 6e 4e 65  nSavepoint = nNe
1437f 77 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  w;..    /* If th
14380 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 63 6b  is is a rollback
14381 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 6c 61 79   operation, play
14382 62 61 63 6b 20 74 68 65 20 73 70 65 63 69 66 69  back the specifi
14383 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 0a 20 20  ed savepoint..  
14384 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20    ** If this is 
14385 61 20 74 65 6d 70 2d 66 69 6c 65 2c 20 69 74 20  a temp-file, it 
14386 69 73 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74  is possible that
14387 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
14388 65 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e 6f 74  e has.    ** not
14389 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64   yet been opened
1438a 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
1438b 68 65 72 65 20 68 61 76 65 20 62 65 65 6e 20 6e  here have been n
1438c 6f 20 63 68 61 6e 67 65 73 20 74 6f 0a 20 20 20  o changes to.   
1438d 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
1438e 20 66 69 6c 65 2c 20 73 6f 20 74 68 65 20 70 6c   file, so the pl
1438f 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e  ayback operation
14390 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64 2e   can be skipped.
14391 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
14392 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  op==SAVEPOINT_RO
14393 4c 4c 42 41 43 4b 20 26 26 20 69 73 4f 70 65 6e  LLBACK && isOpen
14394 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
14395 0a 20 20 20 20 20 20 50 61 67 65 72 53 61 76 65  .      PagerSave
14396 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e  point *pSavepoin
14397 74 20 3d 20 28 6e 4e 65 77 3d 3d 30 29 3f 30 3a  t = (nNew==0)?0:
14398 26 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f  &pPager->aSavepo
14399 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b 0a 20 20 20  int[nNew-1];.   
1439a 20 20 20 72 63 20 3d 20 70 61 67 65 72 50 6c 61     rc = pagerPla
1439b 79 62 61 63 6b 53 61 76 65 70 6f 69 6e 74 28 70  ybackSavepoint(p
1439c 50 61 67 65 72 2c 20 70 53 61 76 65 70 6f 69 6e  Pager, pSavepoin
1439d 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t);.      assert
1439e 28 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  (rc!=SQLITE_DONE
1439f 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  );.    }.  .    
143a0 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
143a1 72 65 6c 65 61 73 65 20 6f 66 20 74 68 65 20 6f  release of the o
143a2 75 74 65 72 6d 6f 73 74 20 73 61 76 65 70 6f 69  utermost savepoi
143a3 6e 74 2c 20 74 72 75 6e 63 61 74 65 20 0a 20 20  nt, truncate .  
143a4 20 20 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75    ** the sub-jou
143a5 72 6e 61 6c 20 74 6f 20 7a 65 72 6f 20 62 79 74  rnal to zero byt
143a6 65 73 20 69 6e 20 73 69 7a 65 2e 20 2a 2f 0a 20  es in size. */. 
143a7 20 20 20 69 66 28 20 6e 4e 65 77 3d 3d 30 20 26     if( nNew==0 &
143a8 26 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  & op==SAVEPOINT_
143a9 52 45 4c 45 41 53 45 20 26 26 20 69 73 4f 70 65  RELEASE && isOpe
143aa 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 29 20  n(pPager->sjfd) 
143ab 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
143ac 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
143ad 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
143ae 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
143af 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 30 29 3b  Pager->sjfd, 0);
143b0 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e  .      pPager->n
143b1 53 75 62 52 65 63 20 3d 20 30 3b 0a 20 20 20 20  SubRec = 0;.    
143b2 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
143b3 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
143b4 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  rn the full path
143b5 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  name of the data
143b6 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51  base file..*/.SQ
143b7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
143b8 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
143b9 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61  PagerFilename(Pa
143ba 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
143bb 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a  return pPager->z
143bc 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  Filename;.}../*.
143bd 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 56 46  ** Return the VF
143be 53 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  S structure for 
143bf 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53 51  the pager..*/.SQ
143c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
143c1 73 74 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  st sqlite3_vfs *
143c2 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 73 28  sqlite3PagerVfs(
143c3 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
143c4 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
143c5 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  >pVfs;.}../*.** 
143c6 52 65 74 75 72 6e 20 74 68 65 20 66 69 6c 65 20  Return the file 
143c7 68 61 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 64  handle for the d
143c8 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 73 73  atabase file ass
143c9 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20  ociated.** with 
143ca 74 68 65 20 70 61 67 65 72 2e 20 20 54 68 69 73  the pager.  This
143cb 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 4e 55   might return NU
143cc 4c 4c 20 69 66 20 74 68 65 20 66 69 6c 65 20 68  LL if the file h
143cd 61 73 0a 2a 2a 20 6e 6f 74 20 79 65 74 20 62 65  as.** not yet be
143ce 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51  en opened..*/.SQ
143cf 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
143d0 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69 74  ite3_file *sqlit
143d1 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67 65  e3PagerFile(Page
143d2 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65  r *pPager){.  re
143d3 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 66 64 3b  turn pPager->fd;
143d4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
143d5 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68 6e 61   the full pathna
143d6 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  me of the journa
143d7 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  l file..*/.SQLIT
143d8 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
143d9 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67  char *sqlite3Pag
143da 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 50 61  erJournalname(Pa
143db 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
143dc 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a  return pPager->z
143dd 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a  Journal;.}../*.*
143de 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
143df 20 66 73 79 6e 63 28 29 20 63 61 6c 6c 73 20 61   fsync() calls a
143e0 72 65 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20  re disabled for 
143e1 74 68 69 73 20 70 61 67 65 72 2e 20 20 52 65 74  this pager.  Ret
143e2 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69 66 20  urn FALSE.** if 
143e3 66 73 79 6e 63 28 29 73 20 61 72 65 20 65 78 65  fsync()s are exe
143e4 63 75 74 65 64 20 6e 6f 72 6d 61 6c 6c 79 2e 0a  cuted normally..
143e5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
143e6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
143e7 65 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 20 2a  erNosync(Pager *
143e8 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72  pPager){.  retur
143e9 6e 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63  n pPager->noSync
143ea 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
143eb 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 2f 2a 0a  TE_HAS_CODEC./*.
143ec 2a 2a 20 53 65 74 20 6f 72 20 72 65 74 72 69 65  ** Set or retrie
143ed 76 65 20 74 68 65 20 63 6f 64 65 63 20 66 6f 72  ve the codec for
143ee 20 74 68 69 73 20 70 61 67 65 72 0a 2a 2f 0a 73   this pager.*/.s
143ef 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
143f0 65 33 50 61 67 65 72 53 65 74 43 6f 64 65 63 28  e3PagerSetCodec(
143f1 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
143f2 2c 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f 64  ,.  void *(*xCod
143f3 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a 2c  ec)(void*,void*,
143f4 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20 20 76 6f 69  Pgno,int),.  voi
143f5 64 20 28 2a 78 43 6f 64 65 63 53 69 7a 65 43 68  d (*xCodecSizeCh
143f6 6e 67 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e  ng)(void*,int,in
143f7 74 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f  t),.  void (*xCo
143f8 64 65 63 46 72 65 65 29 28 76 6f 69 64 2a 29 2c  decFree)(void*),
143f9 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 0a  .  void *pCodec.
143fa 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  ){.  if( pPager-
143fb 3e 78 43 6f 64 65 63 46 72 65 65 20 29 20 70 50  >xCodecFree ) pP
143fc 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65  ager->xCodecFree
143fd 28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 29  (pPager->pCodec)
143fe 3b 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64  ;.  pPager->xCod
143ff 65 63 20 3d 20 70 50 61 67 65 72 2d 3e 6d 65 6d  ec = pPager->mem
14400 44 62 20 3f 20 30 20 3a 20 78 43 6f 64 65 63 3b  Db ? 0 : xCodec;
14401 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65  .  pPager->xCode
14402 63 53 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64  cSizeChng = xCod
14403 65 63 53 69 7a 65 43 68 6e 67 3b 0a 20 20 70 50  ecSizeChng;.  pP
14404 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65  ager->xCodecFree
14405 20 3d 20 78 43 6f 64 65 63 46 72 65 65 3b 0a 20   = xCodecFree;. 
14406 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 20   pPager->pCodec 
14407 3d 20 70 43 6f 64 65 63 3b 0a 20 20 70 61 67 65  = pCodec;.  page
14408 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67  rReportSize(pPag
14409 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  er);.}.static vo
1440a 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
1440b 47 65 74 43 6f 64 65 63 28 50 61 67 65 72 20 2a  GetCodec(Pager *
1440c 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72  pPager){.  retur
1440d 6e 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63  n pPager->pCodec
1440e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ;.}.#endif..#ifn
1440f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
14410 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a  AUTOVACUUM./*.**
14411 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70   Move the page p
14412 50 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70  Pg to location p
14413 67 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e  gno in the file.
14414 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  .**.** There mus
14415 74 20 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63  t be no referenc
14416 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 70  es to the page p
14417 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65  reviously locate
14418 64 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68  d at.** pgno (wh
14419 69 63 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f  ich we call pPgO
1441a 6c 64 29 20 74 68 6f 75 67 68 20 74 68 61 74 20  ld) though that 
1441b 70 61 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20  page is allowed 
1441c 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68  to be.** in cach
1441d 65 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20  e.  If the page 
1441e 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74  previously locat
1441f 65 64 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f  ed at pgno is no
14420 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20  t already.** in 
14421 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
14422 72 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20  rnal, it is not 
14423 70 75 74 20 74 68 65 72 65 20 62 79 20 62 79 20  put there by by 
14424 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a  this routine..**
14425 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74  .** References t
14426 6f 20 74 68 65 20 70 61 67 65 20 70 50 67 20 72  o the page pPg r
14427 65 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64  emain valid. Upd
14428 61 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74  ating any.** met
14429 61 2d 64 61 74 61 20 61 73 73 6f 63 69 61 74 65  a-data associate
1442a 64 20 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e  d with pPg (i.e.
1442b 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20   data stored in 
1442c 74 68 65 20 6e 45 78 74 72 61 20 62 79 74 65 73  the nExtra bytes
1442d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c  .** allocated al
1442e 6f 6e 67 20 77 69 74 68 20 74 68 65 20 70 61 67  ong with the pag
1442f 65 29 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e  e) is the respon
14430 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20  sibility of the 
14431 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20  caller..**.** A 
14432 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74  transaction must
14433 20 62 65 20 61 63 74 69 76 65 20 77 68 65 6e 20   be active when 
14434 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
14435 63 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20  called. It used 
14436 74 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65  to be.** require
14437 64 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65  d that a stateme
14438 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  nt transaction w
14439 61 73 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62  as not active, b
1443a 75 74 20 74 68 69 73 20 72 65 73 74 72 69 63 74  ut this restrict
1443b 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ion.** has been 
1443c 72 65 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20  removed (CREATE 
1443d 49 4e 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d  INDEX needs to m
1443e 6f 76 65 20 61 20 70 61 67 65 20 77 68 65 6e 20  ove a page when 
1443f 61 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74  a statement.** t
14440 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
14441 74 69 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  tive)..**.** If 
14442 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d  the fourth argum
14443 65 6e 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69  ent, isCommit, i
14444 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
14445 20 74 68 69 73 20 70 61 67 65 20 69 73 20 62 65   this page is be
14446 69 6e 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20  ing.** moved as 
14447 70 61 72 74 20 6f 66 20 61 20 64 61 74 61 62 61  part of a databa
14448 73 65 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f  se reorganizatio
14449 6e 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68  n just before th
1444a 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a  e transaction .*
1444b 2a 20 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69  * is being commi
1444c 74 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  tted. In this ca
1444d 73 65 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e  se, it is guaran
1444e 74 65 65 64 20 74 68 61 74 20 74 68 65 20 64 61  teed that the da
1444f 74 61 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20  tabase page .** 
14450 70 50 67 20 72 65 66 65 72 73 20 74 6f 20 77 69  pPg refers to wi
14451 6c 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65  ll not be writte
14452 6e 20 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69  n to again withi
14453 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  n this transacti
14454 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  on..**.** This f
14455 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75  unction may retu
14456 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  rn SQLITE_NOMEM 
14457 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63  or an IO error c
14458 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a  ode if an error.
14459 2a 2a 20 6f 63 63 75 72 73 2e 20 4f 74 68 65 72  ** occurs. Other
1445a 77 69 73 65 2c 20 69 74 20 72 65 74 75 72 6e 73  wise, it returns
1445b 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53   SQLITE_OK..*/.S
1445c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1445d 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f  t sqlite3PagerMo
1445e 76 65 70 61 67 65 28 50 61 67 65 72 20 2a 70 50  vepage(Pager *pP
1445f 61 67 65 72 2c 20 44 62 50 61 67 65 20 2a 70 50  ager, DbPage *pP
14460 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e  g, Pgno pgno, in
14461 74 20 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50  t isCommit){.  P
14462 67 48 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20  gHdr *pPgOld;   
14463 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
14464 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76  he page being ov
14465 65 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20  erwritten. */.  
14466 50 67 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e  Pgno needSyncPgn
14467 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  o = 0;       /* 
14468 4f 6c 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67  Old value of pPg
14469 2d 3e 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20  ->pgno, if sync 
1446a 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  is required */. 
1446b 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
1446c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1446d 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
1446e 20 20 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b    Pgno origPgno;
1446f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
14470 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  * The original p
14471 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20  age number */.. 
14472 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52   assert( pPg->nR
14473 65 66 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e  ef>0 );..  /* In
14474 20 6f 72 64 65 72 20 74 6f 20 62 65 20 61 62 6c   order to be abl
14475 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 61  e to rollback, a
14476 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
14477 62 61 73 65 20 6d 75 73 74 20 6a 6f 75 72 6e 61  base must journa
14478 6c 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20  l.  ** the page 
14479 77 65 20 61 72 65 20 6d 6f 76 69 6e 67 20 66 72  we are moving fr
1447a 6f 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4d  om..  */.  if( M
1447b 45 4d 44 42 20 29 7b 0a 20 20 20 20 72 63 20 3d  EMDB ){.    rc =
1447c 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1447d 74 65 28 70 50 67 29 3b 0a 20 20 20 20 69 66 28  te(pPg);.    if(
1447e 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
1447f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
14480 65 20 70 61 67 65 20 62 65 69 6e 67 20 6d 6f 76  e page being mov
14481 65 64 20 69 73 20 64 69 72 74 79 20 61 6e 64 20  ed is dirty and 
14482 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 73 61 76  has not been sav
14483 65 64 20 62 79 20 74 68 65 20 6c 61 74 65 73 74  ed by the latest
14484 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 2c  .  ** savepoint,
14485 20 74 68 65 6e 20 73 61 76 65 20 74 68 65 20 63   then save the c
14486 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20  urrent contents 
14487 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f  of the page into
14488 20 74 68 65 20 0a 20 20 2a 2a 20 73 75 62 2d 6a   the .  ** sub-j
14489 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54 68 69 73  ournal now. This
1448a 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20   is required to 
1448b 68 61 6e 64 6c 65 20 74 68 65 20 66 6f 6c 6c 6f  handle the follo
1448c 77 69 6e 67 20 73 63 65 6e 61 72 69 6f 3a 0a 20  wing scenario:. 
1448d 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45 47 49 4e   **.  **   BEGIN
1448e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a 6f 75 72  ;.  **     <jour
1448f 6e 61 6c 20 70 61 67 65 20 58 2c 20 74 68 65 6e  nal page X, then
14490 20 6d 6f 64 69 66 79 20 69 74 20 69 6e 20 6d 65   modify it in me
14491 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20 20 20 53  mory>.  **     S
14492 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b 0a 20 20  AVEPOINT one;.  
14493 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76 65 20 70  **       <Move p
14494 61 67 65 20 58 20 74 6f 20 6c 6f 63 61 74 69 6f  age X to locatio
14495 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20 20 52 4f  n Y>.  **     RO
14496 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 3b 0a 20  LLBACK TO one;. 
14497 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 61 67 65   **.  ** If page
14498 20 58 20 77 65 72 65 20 6e 6f 74 20 77 72 69 74   X were not writ
14499 74 65 6e 20 74 6f 20 74 68 65 20 73 75 62 2d 6a  ten to the sub-j
1449a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20 69 74 20  ournal here, it 
1449b 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a 2a 20 62  would not.  ** b
1449c 65 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65  e possible to re
1449d 73 74 6f 72 65 20 69 74 73 20 63 6f 6e 74 65 6e  store its conten
1449e 74 73 20 77 68 65 6e 20 74 68 65 20 22 52 4f 4c  ts when the "ROL
1449f 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22 0a 20 20  LBACK TO one".  
144a0 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77 65 72  ** statement wer
144a1 65 20 69 73 20 70 72 6f 63 65 73 73 65 64 2e 0a  e is processed..
144a2 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62 6a 6f 75    **.  ** subjou
144a3 72 6e 61 6c 50 61 67 65 28 29 20 6d 61 79 20 6e  rnalPage() may n
144a4 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  eed to allocate 
144a5 73 70 61 63 65 20 74 6f 20 73 74 6f 72 65 20 70  space to store p
144a6 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f 0a 20 20  Pg->pgno into.  
144a7 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73  ** one or more s
144a8 61 76 65 70 6f 69 6e 74 20 62 69 74 76 65 63 73  avepoint bitvecs
144a9 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72 65  . This is the re
144aa 61 73 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 69  ason this functi
144ab 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72 65 74 75  on.  ** may retu
144ac 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e  rn SQLITE_NOMEM.
144ad 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 67 2d  .  */.  if( pPg-
144ae 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
144af 54 59 0a 20 20 20 26 26 20 73 75 62 6a 52 65 71  TY.   && subjReq
144b0 75 69 72 65 73 50 61 67 65 28 70 50 67 29 0a 20  uiresPage(pPg). 
144b1 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    && SQLITE_OK!=
144b2 28 72 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c  (rc = subjournal
144b3 50 61 67 65 28 70 50 67 29 29 0a 20 20 29 7b 0a  Page(pPg)).  ){.
144b4 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
144b5 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45   }..  PAGERTRACE
144b6 28 28 22 4d 4f 56 45 20 25 64 20 70 61 67 65 20  (("MOVE %d page 
144b7 25 64 20 28 6e 65 65 64 53 79 6e 63 3d 25 64 29  %d (needSync=%d)
144b8 20 6d 6f 76 65 73 20 74 6f 20 25 64 5c 6e 22 2c   moves to %d\n",
144b9 20 0a 20 20 20 20 20 20 50 41 47 45 52 49 44 28   .      PAGERID(
144ba 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70 67  pPager), pPg->pg
144bb 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26  no, (pPg->flags&
144bc 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
144bd 3f 31 3a 30 2c 20 70 67 6e 6f 29 29 3b 0a 20 20  ?1:0, pgno));.  
144be 49 4f 54 52 41 43 45 28 28 22 4d 4f 56 45 20 25  IOTRACE(("MOVE %
144bf 70 20 25 64 20 25 64 5c 6e 22 2c 20 70 50 61 67  p %d %d\n", pPag
144c0 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 70  er, pPg->pgno, p
144c1 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20 49 66 20 74  gno))..  /* If t
144c2 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73  he journal needs
144c3 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20   to be sync()ed 
144c4 62 65 66 6f 72 65 20 70 61 67 65 20 70 50 67 2d  before page pPg-
144c5 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a 2a 20 62  >pgno can.  ** b
144c6 65 20 77 72 69 74 74 65 6e 20 74 6f 2c 20 73 74  e written to, st
144c7 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e  ore pPg->pgno in
144c8 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20   local variable 
144c9 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a 20 20  needSyncPgno..  
144ca 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 69  **.  ** If the i
144cb 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20  sCommit flag is 
144cc 73 65 74 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  set, there is no
144cd 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65   need to remembe
144ce 72 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 20  r that.  ** the 
144cf 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f  journal needs to
144d0 20 62 65 20 73 79 6e 63 28 29 65 64 20 62 65 66   be sync()ed bef
144d1 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61 67  ore database pag
144d2 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a 20 20 2a  e pPg->pgno .  *
144d3 2a 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e  * can be written
144d4 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 20   to. The caller 
144d5 68 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f 6d  has already prom
144d6 69 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 74  ised not to writ
144d7 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f 0a 20 20  e to it..  */.  
144d8 69 66 28 20 28 70 50 67 2d 3e 66 6c 61 67 73 26  if( (pPg->flags&
144d9 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
144da 20 26 26 20 21 69 73 43 6f 6d 6d 69 74 20 29 7b   && !isCommit ){
144db 0a 20 20 20 20 6e 65 65 64 53 79 6e 63 50 67 6e  .    needSyncPgn
144dc 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20  o = pPg->pgno;. 
144dd 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 49     assert( pageI
144de 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c  nJournal(pPg) ||
144df 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65   pPg->pgno>pPage
144e0 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 3b  r->dbOrigSize );
144e1 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 67  .    assert( pPg
144e2 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
144e3 52 54 59 20 29 3b 0a 20 20 20 20 61 73 73 65 72  RTY );.    asser
144e4 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  t( pPager->needS
144e5 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ync );.  }..  /*
144e6 20 49 66 20 74 68 65 20 63 61 63 68 65 20 63 6f   If the cache co
144e7 6e 74 61 69 6e 73 20 61 20 70 61 67 65 20 77 69  ntains a page wi
144e8 74 68 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 70  th page-number p
144e9 67 6e 6f 2c 20 72 65 6d 6f 76 65 20 69 74 0a 20  gno, remove it. 
144ea 20 2a 2a 20 66 72 6f 6d 20 69 74 73 20 68 61 73   ** from its has
144eb 68 20 63 68 61 69 6e 2e 20 41 6c 73 6f 2c 20 69  h chain. Also, i
144ec 66 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64  f the PgHdr.need
144ed 53 79 6e 63 20 77 61 73 20 73 65 74 20 66 6f 72  Sync was set for
144ee 20 0a 20 20 2a 2a 20 70 61 67 65 20 70 67 6e 6f   .  ** page pgno
144ef 20 62 65 66 6f 72 65 20 74 68 65 20 27 6d 6f 76   before the 'mov
144f0 65 27 20 6f 70 65 72 61 74 69 6f 6e 2c 20 69 74  e' operation, it
144f1 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 74   needs to be ret
144f2 61 69 6e 65 64 20 0a 20 20 2a 2a 20 66 6f 72 20  ained .  ** for 
144f3 74 68 65 20 70 61 67 65 20 6d 6f 76 65 64 20 74  the page moved t
144f4 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 70 50 67  here..  */.  pPg
144f5 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44  ->flags &= ~PGHD
144f6 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 70  R_NEED_SYNC;.  p
144f7 50 67 4f 6c 64 20 3d 20 70 61 67 65 72 5f 6c 6f  PgOld = pager_lo
144f8 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e  okup(pPager, pgn
144f9 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70  o);.  assert( !p
144fa 50 67 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c 64 2d  PgOld || pPgOld-
144fb 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 66  >nRef==1 );.  if
144fc 28 20 70 50 67 4f 6c 64 20 29 7b 0a 20 20 20 20  ( pPgOld ){.    
144fd 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70  pPg->flags |= (p
144fe 50 67 4f 6c 64 2d 3e 66 6c 61 67 73 26 50 47 48  PgOld->flags&PGH
144ff 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20  DR_NEED_SYNC);. 
14500 20 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a     if( MEMDB ){.
14501 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20        /* Do not 
14502 64 69 73 63 61 72 64 20 70 61 67 65 73 20 66 72  discard pages fr
14503 6f 6d 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  om an in-memory 
14504 64 61 74 61 62 61 73 65 20 73 69 6e 63 65 20 77  database since w
14505 65 20 6d 69 67 68 74 0a 20 20 20 20 20 20 2a 2a  e might.      **
14506 20 6e 65 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63   need to rollbac
14507 6b 20 6c 61 74 65 72 2e 20 20 4a 75 73 74 20 6d  k later.  Just m
14508 6f 76 65 20 74 68 65 20 70 61 67 65 20 6f 75 74  ove the page out
14509 20 6f 66 20 74 68 65 20 77 61 79 2e 20 2a 2f 0a   of the way. */.
1450a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
1450b 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69  ager->dbSizeVali
1450c 64 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  d );.      sqlit
1450d 65 33 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67  e3PcacheMove(pPg
1450e 4f 6c 64 2c 20 70 50 61 67 65 72 2d 3e 64 62 53  Old, pPager->dbS
1450f 69 7a 65 2b 31 29 3b 0a 20 20 20 20 7d 65 6c 73  ize+1);.    }els
14510 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
14511 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 4f 6c  PcacheDrop(pPgOl
14512 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  d);.    }.  }.. 
14513 20 6f 72 69 67 50 67 6e 6f 20 3d 20 70 50 67 2d   origPgno = pPg-
14514 3e 70 67 6e 6f 3b 0a 20 20 73 71 6c 69 74 65 33  >pgno;.  sqlite3
14515 50 63 61 63 68 65 4d 6f 76 65 28 70 50 67 2c 20  PcacheMove(pPg, 
14516 70 67 6e 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33  pgno);.  sqlite3
14517 50 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28  PcacheMakeDirty(
14518 70 50 67 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  pPg);.  pPager->
14519 64 62 4d 6f 64 69 66 69 65 64 20 3d 20 31 3b 0a  dbModified = 1;.
1451a 0a 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 50  .  if( needSyncP
1451b 67 6e 6f 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  gno ){.    /* If
1451c 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 69 73   needSyncPgno is
1451d 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20   non-zero, then 
1451e 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1451f 20 6e 65 65 64 73 20 74 6f 20 62 65 20 0a 20 20   needs to be .  
14520 20 20 2a 2a 20 73 79 6e 63 28 29 65 64 20 62 65    ** sync()ed be
14521 66 6f 72 65 20 61 6e 79 20 64 61 74 61 20 69 73  fore any data is
14522 20 77 72 69 74 74 65 6e 20 74 6f 20 64 61 74 61   written to data
14523 62 61 73 65 20 66 69 6c 65 20 70 61 67 65 20 6e  base file page n
14524 65 65 64 53 79 6e 63 50 67 6e 6f 2e 0a 20 20 20  eedSyncPgno..   
14525 20 2a 2a 20 43 75 72 72 65 6e 74 6c 79 2c 20 6e   ** Currently, n
14526 6f 20 73 75 63 68 20 70 61 67 65 20 65 78 69 73  o such page exis
14527 74 73 20 69 6e 20 74 68 65 20 70 61 67 65 2d 63  ts in the page-c
14528 61 63 68 65 20 61 6e 64 20 74 68 65 20 0a 20 20  ache and the .  
14529 20 20 2a 2a 20 22 69 73 20 6a 6f 75 72 6e 61 6c    ** "is journal
1452a 65 64 22 20 62 69 74 76 65 63 20 66 6c 61 67 20  ed" bitvec flag 
1452b 68 61 73 20 62 65 65 6e 20 73 65 74 2e 20 54 68  has been set. Th
1452c 69 73 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  is needs to be r
1452d 65 6d 65 64 69 65 64 20 62 79 0a 20 20 20 20 2a  emedied by.    *
1452e 2a 20 6c 6f 61 64 69 6e 67 20 74 68 65 20 70 61  * loading the pa
1452f 67 65 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65  ge into the page
14530 72 2d 63 61 63 68 65 20 61 6e 64 20 73 65 74 74  r-cache and sett
14531 69 6e 67 20 74 68 65 20 50 67 48 64 72 2e 6e 65  ing the PgHdr.ne
14532 65 64 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 66  edSync .    ** f
14533 6c 61 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  lag..    **.    
14534 2a 2a 20 49 66 20 74 68 65 20 61 74 74 65 6d 70  ** If the attemp
14535 74 20 74 6f 20 6c 6f 61 64 20 74 68 65 20 70 61  t to load the pa
14536 67 65 20 69 6e 74 6f 20 74 68 65 20 70 61 67 65  ge into the page
14537 2d 63 61 63 68 65 20 66 61 69 6c 73 2c 20 28 64  -cache fails, (d
14538 75 65 0a 20 20 20 20 2a 2a 20 74 6f 20 61 20 6d  ue.    ** to a m
14539 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 66 61  alloc() or IO fa
1453a 69 6c 75 72 65 29 2c 20 63 6c 65 61 72 20 74 68  ilure), clear th
1453b 65 20 62 69 74 20 69 6e 20 74 68 65 20 70 49 6e  e bit in the pIn
1453c 4a 6f 75 72 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a  Journal[].    **
1453d 20 61 72 72 61 79 2e 20 4f 74 68 65 72 77 69 73   array. Otherwis
1453e 65 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 69  e, if the page i
1453f 73 20 6c 6f 61 64 65 64 20 61 6e 64 20 77 72 69  s loaded and wri
14540 74 74 65 6e 20 61 67 61 69 6e 20 69 6e 0a 20 20  tten again in.  
14541 20 20 2a 2a 20 74 68 69 73 20 74 72 61 6e 73 61    ** this transa
14542 63 74 69 6f 6e 2c 20 69 74 20 6d 61 79 20 62 65  ction, it may be
14543 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
14544 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 65  database file be
14545 66 6f 72 65 0a 20 20 20 20 2a 2a 20 69 74 20 69  fore.    ** it i
14546 73 20 73 79 6e 63 65 64 20 69 6e 74 6f 20 74 68  s synced into th
14547 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  e journal file. 
14548 54 68 69 73 20 77 61 79 2c 20 69 74 20 6d 61 79  This way, it may
14549 20 65 6e 64 20 75 70 20 69 6e 0a 20 20 20 20 2a   end up in.    *
1454a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * the journal fi
1454b 6c 65 20 74 77 69 63 65 2c 20 62 75 74 20 74 68  le twice, but th
1454c 61 74 20 69 73 20 6e 6f 74 20 61 20 70 72 6f 62  at is not a prob
1454d 6c 65 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  lem..    **.    
1454e 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 50 61  ** The sqlite3Pa
1454f 67 65 72 47 65 74 28 29 20 63 61 6c 6c 20 6d 61  gerGet() call ma
14550 79 20 63 61 75 73 65 20 74 68 65 20 6a 6f 75 72  y cause the jour
14551 6e 61 6c 20 74 6f 20 73 79 6e 63 2e 20 53 6f 20  nal to sync. So 
14552 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65  make.    ** sure
14553 20 74 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53   the Pager.needS
14554 79 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74 20  ync flag is set 
14555 74 6f 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  too..    */.    
14556 50 67 48 64 72 20 2a 70 50 67 48 64 72 3b 0a 20  PgHdr *pPgHdr;. 
14557 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
14558 72 2d 3e 6e 65 65 64 53 79 6e 63 20 29 3b 0a 20  r->needSync );. 
14559 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1455a 61 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20  agerGet(pPager, 
1455b 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2c 20 26 70  needSyncPgno, &p
1455c 50 67 48 64 72 29 3b 0a 20 20 20 20 69 66 28 20  PgHdr);.    if( 
1455d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1455e 0a 20 20 20 20 20 20 69 66 28 20 6e 65 65 64 53  .      if( needS
1455f 79 6e 63 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d  yncPgno<=pPager-
14560 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20  >dbOrigSize ){. 
14561 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
14562 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
14563 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73  !=0 );.        s
14564 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61  qlite3BitvecClea
14565 72 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75  r(pPager->pInJou
14566 72 6e 61 6c 2c 20 6e 65 65 64 53 79 6e 63 50 67  rnal, needSyncPg
14567 6e 6f 2c 20 70 50 61 67 65 72 2d 3e 70 54 6d 70  no, pPager->pTmp
14568 53 70 61 63 65 29 3b 0a 20 20 20 20 20 20 7d 0a  Space);.      }.
14569 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1456a 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
1456b 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b  r->needSync = 1;
1456c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
1456d 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d 30 20 26  ger->noSync==0 &
1456e 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 20 20  & !MEMDB );.    
1456f 70 50 67 48 64 72 2d 3e 66 6c 61 67 73 20 7c 3d  pPgHdr->flags |=
14570 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43   PGHDR_NEED_SYNC
14571 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  ;.    sqlite3Pca
14572 63 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67  cheMakeDirty(pPg
14573 48 64 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Hdr);.    sqlite
14574 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67 48  3PagerUnref(pPgH
14575 64 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20  dr);.  }..  /*. 
14576 20 2a 2a 20 46 6f 72 20 61 6e 20 69 6e 2d 6d 65   ** For an in-me
14577 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20 6d  mory database, m
14578 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 72 69  ake sure the ori
14579 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e 74 69  ginal page conti
1457a 6e 75 65 73 0a 20 20 2a 2a 20 74 6f 20 65 78 69  nues.  ** to exi
1457b 73 74 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20  st, in case the 
1457c 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64  transaction need
1457d 73 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 2e 20  s to roll back. 
1457e 20 55 73 65 20 70 50 67 4f 6c 64 0a 20 20 2a 2a   Use pPgOld.  **
1457f 20 61 73 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   as the original
14580 20 70 61 67 65 20 73 69 6e 63 65 20 69 74 20 68   page since it h
14581 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
14582 61 6c 6c 6f 63 61 74 65 64 2e 0a 20 20 2a 2f 0a  allocated..  */.
14583 20 20 69 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20    if( MEMDB ){. 
14584 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
14585 4d 6f 76 65 28 70 50 67 4f 6c 64 2c 20 6f 72 69  Move(pPgOld, ori
14586 67 50 67 6e 6f 29 3b 0a 20 20 20 20 73 71 6c 69  gPgno);.    sqli
14587 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
14588 67 4f 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  gOld);.  }..  re
14589 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1458a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1458b 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
1458c 20 74 6f 20 74 68 65 20 64 61 74 61 20 66 6f 72   to the data for
1458d 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70   the specified p
1458e 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  age..*/.SQLITE_P
1458f 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
14590 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
14591 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20  (DbPage *pPg){. 
14592 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52   assert( pPg->nR
14593 65 66 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50 61  ef>0 || pPg->pPa
14594 67 65 72 2d 3e 6d 65 6d 44 62 20 29 3b 0a 20 20  ger->memDb );.  
14595 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 44 61 74  return pPg->pDat
14596 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  a;.}../*.** Retu
14597 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
14598 74 68 65 20 50 61 67 65 72 2e 6e 45 78 74 72 61  the Pager.nExtra
14599 20 62 79 74 65 73 20 6f 66 20 22 65 78 74 72 61   bytes of "extra
1459a 22 20 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f  " space .** allo
1459b 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68  cated along with
1459c 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 70   the specified p
1459d 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  age..*/.SQLITE_P
1459e 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c  RIVATE void *sql
1459f 69 74 65 33 50 61 67 65 72 47 65 74 45 78 74 72  ite3PagerGetExtr
145a0 61 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a  a(DbPage *pPg){.
145a1 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 45    return pPg->pE
145a2 78 74 72 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  xtra;.}../*.** G
145a3 65 74 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b 69  et/set the locki
145a4 6e 67 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73  ng-mode for this
145a5 20 70 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65   pager. Paramete
145a6 72 20 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20  r eMode must be 
145a7 6f 6e 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52 5f  one.** of PAGER_
145a8 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52  LOCKINGMODE_QUER
145a9 59 2c 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  Y, PAGER_LOCKING
145aa 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a  MODE_NORMAL or .
145ab 2a 2a 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  ** PAGER_LOCKING
145ac 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 2e 20  MODE_EXCLUSIVE. 
145ad 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  If the parameter
145ae 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20   is not _QUERY, 
145af 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b  then.** the lock
145b0 69 6e 67 2d 6d 6f 64 65 20 69 73 20 73 65 74 20  ing-mode is set 
145b1 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73 70 65  to the value spe
145b2 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  cified..**.** Th
145b3 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65  e returned value
145b4 20 69 73 20 65 69 74 68 65 72 20 50 41 47 45 52   is either PAGER
145b5 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52  _LOCKINGMODE_NOR
145b6 4d 41 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f  MAL or.** PAGER_
145b7 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
145b8 55 53 49 56 45 2c 20 69 6e 64 69 63 61 74 69 6e  USIVE, indicatin
145b9 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 28 70  g the current (p
145ba 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65 64 29  ossibly updated)
145bb 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65  .** locking-mode
145bc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
145bd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
145be 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28  agerLockingMode(
145bf 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
145c0 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73  nt eMode){.  ass
145c1 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45  ert( eMode==PAGE
145c2 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55  R_LOCKINGMODE_QU
145c3 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20 20  ERY.            
145c4 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
145c5 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d  LOCKINGMODE_NORM
145c6 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c  AL.            |
145c7 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c  | eMode==PAGER_L
145c8 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55  OCKINGMODE_EXCLU
145c9 53 49 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74  SIVE );.  assert
145ca 28 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ( PAGER_LOCKINGM
145cb 4f 44 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20  ODE_QUERY<0 );. 
145cc 20 61 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c   assert( PAGER_L
145cd 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41  OCKINGMODE_NORMA
145ce 4c 3e 3d 30 20 26 26 20 50 41 47 45 52 5f 4c 4f  L>=0 && PAGER_LO
145cf 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53  CKINGMODE_EXCLUS
145d0 49 56 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20  IVE>=0 );.  if( 
145d1 65 4d 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 61  eMode>=0 && !pPa
145d2 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b  ger->tempFile ){
145d3 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63  .    pPager->exc
145d4 6c 75 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 38  lusiveMode = (u8
145d5 29 65 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65  )eMode;.  }.  re
145d6 74 75 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72  turn (int)pPager
145d7 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b  ->exclusiveMode;
145d8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65  .}../*.** Get/se
145d9 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f  t the journal-mo
145da 64 65 20 66 6f 72 20 74 68 69 73 20 70 61 67 65  de for this page
145db 72 2e 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f  r. Parameter eMo
145dc 64 65 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f  de must be one o
145dd 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 45  f:.**.**    PAGE
145de 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55  R_JOURNALMODE_QU
145df 45 52 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  ERY.**    PAGER_
145e0 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45  JOURNALMODE_DELE
145e1 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a  TE.**    PAGER_J
145e2 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43  OURNALMODE_TRUNC
145e3 41 54 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  ATE.**    PAGER_
145e4 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53  JOURNALMODE_PERS
145e5 49 53 54 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  IST.**    PAGER_
145e6 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a  JOURNALMODE_OFF.
145e7 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
145e8 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a  NALMODE_MEMORY.*
145e9 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61  *.** If the para
145ea 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55  meter is not _QU
145eb 45 52 59 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f  ERY, then the jo
145ec 75 72 6e 61 6c 5f 6d 6f 64 65 20 69 73 20 73 65  urnal_mode is se
145ed 74 20 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75  t to the.** valu
145ee 65 20 73 70 65 63 69 66 69 65 64 20 69 66 20 74  e specified if t
145ef 68 65 20 63 68 61 6e 67 65 20 69 73 20 61 6c 6c  he change is all
145f0 6f 77 65 64 2e 20 20 54 68 65 20 63 68 61 6e 67  owed.  The chang
145f1 65 20 69 73 20 64 69 73 61 6c 6c 6f 77 65 64 0a  e is disallowed.
145f2 2a 2a 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f  ** for the follo
145f3 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a  wing reasons:.**
145f4 0a 2a 2a 20 20 20 2a 20 20 41 6e 20 69 6e 2d 6d  .**   *  An in-m
145f5 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 63  emory database c
145f6 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 69 74 73  an only have its
145f7 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 73 65   journal_mode se
145f8 74 20 74 6f 20 5f 4f 46 46 0a 2a 2a 20 20 20 20  t to _OFF.**    
145f9 20 20 6f 72 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a    or _MEMORY..**
145fa 0a 2a 2a 20 20 20 2a 20 20 54 68 65 20 6a 6f 75  .**   *  The jou
145fb 72 6e 61 6c 20 6d 6f 64 65 20 6d 61 79 20 6e 6f  rnal mode may no
145fc 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68 69  t be changed whi
145fd 6c 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  le a transaction
145fe 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a   is active..**.*
145ff 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 69  * The returned i
14600 6e 64 69 63 61 74 65 20 74 68 65 20 63 75 72 72  ndicate the curr
14601 65 6e 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70  ent (possibly up
14602 64 61 74 65 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d  dated) journal-m
14603 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ode..*/.SQLITE_P
14604 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
14605 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f  e3PagerJournalMo
14606 64 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  de(Pager *pPager
14607 2c 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20  , int eMode){.  
14608 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50  assert( eMode==P
14609 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
1460a 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20 20  _QUERY.         
1460b 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
1460c 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44  ER_JOURNALMODE_D
1460d 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 20 20  ELETE.          
1460e 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45    || eMode==PAGE
1460f 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52  R_JOURNALMODE_TR
14610 55 4e 43 41 54 45 0a 20 20 20 20 20 20 20 20 20  UNCATE.         
14611 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
14612 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50  ER_JOURNALMODE_P
14613 45 52 53 49 53 54 0a 20 20 20 20 20 20 20 20 20  ERSIST.         
14614 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47     || eMode==PAG
14615 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
14616 46 46 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  FF .            
14617 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
14618 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
14619 52 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  RY );.  assert( 
1461a 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
1461b 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 69  E_QUERY<0 );.  i
1461c 66 28 20 65 4d 6f 64 65 3e 3d 30 0a 20 20 20 26  f( eMode>=0.   &
1461d 26 20 28 21 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f  & (!MEMDB || eMo
1461e 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
1461f 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20  LMODE_MEMORY .  
14620 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
14621 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
14622 4e 41 4c 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20  NALMODE_OFF).   
14623 26 26 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f  && !pPager->dbMo
14624 64 69 66 69 65 64 0a 20 20 20 26 26 20 28 21 69  dified.   && (!i
14625 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
14626 64 29 20 7c 7c 20 30 3d 3d 70 50 61 67 65 72 2d  d) || 0==pPager-
14627 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 0a 20 20 29  >journalOff).  )
14628 7b 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e  {.    if( isOpen
14629 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
1462a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
1462b 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66  Close(pPager->jf
1462c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  d);.    }.    pP
1462d 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
1462e 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20  e = (u8)eMode;. 
1462f 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74   }.  return (int
14630 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  )pPager->journal
14631 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  Mode;.}../*.** G
14632 65 74 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d  et/set the size-
14633 6c 69 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70  limit used for p
14634 65 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61  ersistent journa
14635 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53  l files..**.** S
14636 65 74 74 69 6e 67 20 74 68 65 20 73 69 7a 65 20  etting the size 
14637 6c 69 6d 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e  limit to -1 mean
14638 73 20 6e 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e  s no limit is en
14639 66 6f 72 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74  forced..** An at
1463a 74 65 6d 70 74 20 74 6f 20 73 65 74 20 61 20 6c  tempt to set a l
1463b 69 6d 69 74 20 73 6d 61 6c 6c 65 72 20 74 68 61  imit smaller tha
1463c 6e 20 2d 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  n -1 is a no-op.
1463d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1463e 54 45 20 69 36 34 20 73 71 6c 69 74 65 33 50 61  TE i64 sqlite3Pa
1463f 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69  gerJournalSizeLi
14640 6d 69 74 28 50 61 67 65 72 20 2a 70 50 61 67 65  mit(Pager *pPage
14641 72 2c 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a  r, i64 iLimit){.
14642 20 20 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31    if( iLimit>=-1
14643 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
14644 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74  journalSizeLimit
14645 20 3d 20 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20   = iLimit;.  }. 
14646 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e   return pPager->
14647 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74  journalSizeLimit
14648 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
14649 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
1464a 68 65 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b  he pPager->pBack
1464b 75 70 20 76 61 72 69 61 62 6c 65 2e 20 54 68 65  up variable. The
1464c 20 62 61 63 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a   backup module.*
1464d 2a 20 69 6e 20 62 61 63 6b 75 70 2e 63 20 6d 61  * in backup.c ma
1464e 69 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74  intains the cont
1464f 65 6e 74 20 6f 66 20 74 68 69 73 20 76 61 72 69  ent of this vari
14650 61 62 6c 65 2e 20 54 68 69 73 20 6d 6f 64 75 6c  able. This modul
14651 65 0a 2a 2a 20 75 73 65 73 20 69 74 20 6f 70 61  e.** uses it opa
14652 71 75 65 6c 79 20 61 73 20 61 6e 20 61 72 67 75  quely as an argu
14653 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 42  ment to sqlite3B
14654 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 61  ackupRestart() a
14655 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33 42 61 63  nd.** sqlite3Bac
14656 6b 75 70 55 70 64 61 74 65 28 29 20 6f 6e 6c 79  kupUpdate() only
14657 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14658 41 54 45 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ATE sqlite3_back
14659 75 70 20 2a 2a 73 71 6c 69 74 65 33 50 61 67 65  up **sqlite3Page
1465a 72 42 61 63 6b 75 70 50 74 72 28 50 61 67 65 72  rBackupPtr(Pager
1465b 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74   *pPager){.  ret
1465c 75 72 6e 20 26 70 50 61 67 65 72 2d 3e 70 42 61  urn &pPager->pBa
1465d 63 6b 75 70 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  ckup;.}..#endif 
1465e 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  /* SQLITE_OMIT_D
1465f 49 53 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  ISKIO */../*****
14660 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
14661 20 70 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a   pager.c *******
14662 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14663 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14664 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
14665 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
14666 66 69 6c 65 20 62 74 6d 75 74 65 78 2e 63 20 2a  file btmutex.c *
14667 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14668 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14669 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
1466a 32 30 30 37 20 41 75 67 75 73 74 20 32 37 0a 2a  2007 August 27.*
1466b 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
1466c 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
1466d 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
1466e 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
1466f 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
14670 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
14671 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
14672 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
14673 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
14674 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
14675 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
14676 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
14677 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
14678 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
14679 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1467a 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1467b 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1467c 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1467d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1467e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1467f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
14681 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
14682 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64  ntains code used
14683 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75   to implement mu
14684 74 65 78 65 73 20 6f 6e 20 42 74 72 65 65 20 6f  texes on Btree o
14685 62 6a 65 63 74 73 2e 0a 2a 2a 20 54 68 69 73 20  bjects..** This 
14686 63 6f 64 65 20 72 65 61 6c 6c 79 20 62 65 6c 6f  code really belo
14687 6e 67 73 20 69 6e 20 62 74 72 65 65 2e 63 2e 20  ngs in btree.c. 
14688 20 42 75 74 20 62 74 72 65 65 2e 63 20 69 73 20   But btree.c is 
14689 67 65 74 74 69 6e 67 20 74 6f 6f 0a 2a 2a 20 62  getting too.** b
1468a 69 67 20 61 6e 64 20 77 65 20 77 61 6e 74 20 74  ig and we want t
1468b 6f 20 62 72 65 61 6b 20 69 74 20 64 6f 77 6e 20  o break it down 
1468c 73 6f 6d 65 2e 20 20 54 68 69 73 20 70 61 63 6b  some.  This pack
1468d 61 67 65 64 20 73 65 65 6d 65 64 20 6c 69 6b 65  aged seemed like
1468e 0a 2a 2a 20 61 20 67 6f 6f 64 20 62 72 65 61 6b  .** a good break
1468f 6f 75 74 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  out..*/./*******
14690 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
14691 62 74 72 65 65 49 6e 74 2e 68 20 69 6e 20 74 68  btreeInt.h in th
14692 65 20 6d 69 64 64 6c 65 20 6f 66 20 62 74 6d 75  e middle of btmu
14693 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tex.c **********
14694 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
14695 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
14696 6c 65 20 62 74 72 65 65 49 6e 74 2e 68 20 2a 2a  le btreeInt.h **
14697 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14698 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14699 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
1469a 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a  04 April 6.**.**
1469b 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1469c 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1469d 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1469e 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1469f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
146a0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
146a1 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
146a2 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
146a3 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
146a4 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
146a5 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
146a6 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
146a7 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
146a8 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
146a9 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
146aa 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
146ab 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
146ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
146ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
146ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
146af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
146b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
146b1 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
146b2 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69  s a external (di
146b3 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61  sk-based) databa
146b4 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e  se using BTrees.
146b5 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74 61 69 6c  .** For a detail
146b6 65 64 20 64 69 73 63 75 73 73 69 6f 6e 20 6f 66  ed discussion of
146b7 20 42 54 72 65 65 73 2c 20 72 65 66 65 72 20 74   BTrees, refer t
146b8 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 6f 6e 61  o.**.**     Dona
146b9 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20 54 48 45  ld E. Knuth, THE
146ba 20 41 52 54 20 4f 46 20 43 4f 4d 50 55 54 45 52   ART OF COMPUTER
146bb 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c 20 56 6f   PROGRAMMING, Vo
146bc 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20 20 20 22  lume 3:.**     "
146bd 53 6f 72 74 69 6e 67 20 41 6e 64 20 53 65 61 72  Sorting And Sear
146be 63 68 69 6e 67 22 2c 20 70 61 67 65 73 20 34 37  ching", pages 47
146bf 33 2d 34 38 30 2e 20 41 64 64 69 73 6f 6e 2d 57  3-480. Addison-W
146c0 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20 50 75 62  esley.**     Pub
146c1 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61 6e 79 2c  lishing Company,
146c2 20 52 65 61 64 69 6e 67 2c 20 4d 61 73 73 61 63   Reading, Massac
146c3 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  husetts..**.** T
146c4 68 65 20 62 61 73 69 63 20 69 64 65 61 20 69 73  he basic idea is
146c5 20 74 68 61 74 20 65 61 63 68 20 70 61 67 65 20   that each page 
146c6 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74  of the file cont
146c7 61 69 6e 73 20 4e 20 64 61 74 61 62 61 73 65 0a  ains N database.
146c8 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e 64 20 4e  ** entries and N
146c9 2b 31 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 73  +1 pointers to s
146ca 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  ubpages..**.**  
146cb 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
146cc 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146cd 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146ce 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146cf 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72 28 30 29  -.**   |  Ptr(0)
146d0 20 7c 20 4b 65 79 28 30 29 20 7c 20 50 74 72 28   | Key(0) | Ptr(
146d1 31 29 20 7c 20 4b 65 79 28 31 29 20 7c 20 2e 2e  1) | Key(1) | ..
146d2 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20 7c 20 50  . | Key(N-1) | P
146d3 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d  tr(N) |.**   ---
146d4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146d5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146d6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
146d7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
146d8 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b  .** All of the k
146d9 65 79 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20  eys on the page 
146da 74 68 61 74 20 50 74 72 28 30 29 20 70 6f 69 6e  that Ptr(0) poin
146db 74 73 20 74 6f 20 68 61 76 65 20 76 61 6c 75 65  ts to have value
146dc 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 4b  s less.** than K
146dd 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f 66 20 74  ey(0).  All of t
146de 68 65 20 6b 65 79 73 20 6f 6e 20 70 61 67 65 20  he keys on page 
146df 50 74 72 28 31 29 20 61 6e 64 20 69 74 73 20 73  Ptr(1) and its s
146e0 75 62 70 61 67 65 73 20 68 61 76 65 0a 2a 2a 20  ubpages have.** 
146e1 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20 74  values greater t
146e2 68 61 6e 20 4b 65 79 28 30 29 20 61 6e 64 20 6c  han Key(0) and l
146e3 65 73 73 20 74 68 61 6e 20 4b 65 79 28 31 29 2e  ess than Key(1).
146e4 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79    All of the key
146e5 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e 29 20 61  s.** on Ptr(N) a
146e6 6e 64 20 69 74 73 20 73 75 62 70 61 67 65 73 20  nd its subpages 
146e7 68 61 76 65 20 76 61 6c 75 65 73 20 67 72 65 61  have values grea
146e8 74 65 72 20 74 68 61 6e 20 4b 65 79 28 4e 2d 31  ter than Key(N-1
146e9 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f  ).  And.** so fo
146ea 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69  rth..**.** Findi
146eb 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ng a particular 
146ec 6b 65 79 20 72 65 71 75 69 72 65 73 20 72 65 61  key requires rea
146ed 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29 29 20 70  ding O(log(M)) p
146ee 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20 0a 2a  ages from the .*
146ef 2a 20 64 69 73 6b 20 77 68 65 72 65 20 4d 20 69  * disk where M i
146f0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
146f1 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 74  entries in the t
146f2 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  ree..**.** In th
146f3 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  is implementatio
146f4 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66 69 6c 65  n, a single file
146f5 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65 20 6f 72   can hold one or
146f6 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20 0a   more separate .
146f7 2a 2a 20 42 54 72 65 65 73 2e 20 20 45 61 63 68  ** BTrees.  Each
146f8 20 42 54 72 65 65 20 69 73 20 69 64 65 6e 74 69   BTree is identi
146f9 66 69 65 64 20 62 79 20 74 68 65 20 69 6e 64 65  fied by the inde
146fa 78 20 6f 66 20 69 74 73 20 72 6f 6f 74 20 70 61  x of its root pa
146fb 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b 65 79 20  ge.  The.** key 
146fc 61 6e 64 20 64 61 74 61 20 66 6f 72 20 61 6e 79  and data for any
146fd 20 65 6e 74 72 79 20 61 72 65 20 63 6f 6d 62 69   entry are combi
146fe 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65 20  ned to form the 
146ff 22 70 61 79 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a  "payload".  A.**
14700 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66   fixed amount of
14701 20 70 61 79 6c 6f 61 64 20 63 61 6e 20 62 65 20   payload can be 
14702 63 61 72 72 69 65 64 20 64 69 72 65 63 74 6c 79  carried directly
14703 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
14704 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66 20 74 68  .** page.  If th
14705 65 20 70 61 79 6c 6f 61 64 20 69 73 20 6c 61 72  e payload is lar
14706 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65  ger than the pre
14707 73 65 74 20 61 6d 6f 75 6e 74 20 74 68 65 6e 20  set amount then 
14708 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79 74 65 73  surplus.** bytes
14709 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 6f   are stored on o
1470a 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 20  verflow pages.  
1470b 54 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72 20  The payload for 
1470c 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61 6e 64 20  an entry.** and 
1470d 74 68 65 20 70 72 65 63 65 64 69 6e 67 20 70 6f  the preceding po
1470e 69 6e 74 65 72 20 61 72 65 20 63 6f 6d 62 69 6e  inter are combin
1470f 65 64 20 74 6f 20 66 6f 72 6d 20 61 20 22 43 65  ed to form a "Ce
14710 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a 2a 20 70  ll".  Each .** p
14711 61 67 65 20 68 61 73 20 61 20 73 6d 61 6c 6c 20  age has a small 
14712 68 65 61 64 65 72 20 77 68 69 63 68 20 63 6f 6e  header which con
14713 74 61 69 6e 73 20 74 68 65 20 50 74 72 28 4e 29  tains the Ptr(N)
14714 20 70 6f 69 6e 74 65 72 20 61 6e 64 20 6f 74 68   pointer and oth
14715 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  er.** informatio
14716 6e 20 73 75 63 68 20 61 73 20 74 68 65 20 73 69  n such as the si
14717 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 61  ze of key and da
14718 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54  ta..**.** FORMAT
14719 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54   DETAILS.**.** T
1471a 68 65 20 66 69 6c 65 20 69 73 20 64 69 76 69 64  he file is divid
1471b 65 64 20 69 6e 74 6f 20 70 61 67 65 73 2e 20 20  ed into pages.  
1471c 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20 69  The first page i
1471d 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2c  s called page 1,
1471e 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 69  .** the second i
1471f 73 20 70 61 67 65 20 32 2c 20 61 6e 64 20 73 6f  s page 2, and so
14720 20 66 6f 72 74 68 2e 20 20 41 20 70 61 67 65 20   forth.  A page 
14721 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72 6f 20 69  number of zero i
14722 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22 6e 6f 20  ndicates.** "no 
14723 73 75 63 68 20 70 61 67 65 22 2e 20 20 54 68 65  such page".  The
14724 20 70 61 67 65 20 73 69 7a 65 20 63 61 6e 20 62   page size can b
14725 65 20 61 6e 79 20 70 6f 77 65 72 20 6f 66 20 32  e any power of 2
14726 20 62 65 74 77 65 65 6e 20 35 31 32 20 61 6e 64   between 512 and
14727 20 33 32 37 36 38 2e 0a 2a 2a 20 45 61 63 68 20   32768..** Each 
14728 70 61 67 65 20 63 61 6e 20 62 65 20 65 69 74 68  page can be eith
14729 65 72 20 61 20 62 74 72 65 65 20 70 61 67 65 2c  er a btree page,
1472a 20 61 20 66 72 65 65 6c 69 73 74 20 70 61 67 65   a freelist page
1472b 2c 20 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a  , an overflow.**
1472c 20 70 61 67 65 2c 20 6f 72 20 61 20 70 6f 69 6e   page, or a poin
1472d 74 65 72 2d 6d 61 70 20 70 61 67 65 2e 0a 2a 2a  ter-map page..**
1472e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
1472f 67 65 20 69 73 20 61 6c 77 61 79 73 20 61 20 62  ge is always a b
14730 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65 20  tree page.  The 
14731 66 69 72 73 74 20 31 30 30 20 62 79 74 65 73 20  first 100 bytes 
14732 6f 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  of the first.** 
14733 70 61 67 65 20 63 6f 6e 74 61 69 6e 20 61 20 73  page contain a s
14734 70 65 63 69 61 6c 20 68 65 61 64 65 72 20 28 74  pecial header (t
14735 68 65 20 22 66 69 6c 65 20 68 65 61 64 65 72 22  he "file header"
14736 29 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73  ) that describes
14737 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 68   the file..** Th
14738 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20  e format of the 
14739 66 69 6c 65 20 68 65 61 64 65 72 20 69 73 20 61  file header is a
1473a 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
1473b 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a 45     OFFSET   SIZE
1473c 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a      DESCRIPTION.
1473d 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20 31  **      0      1
1473e 36 20 20 20 20 20 48 65 61 64 65 72 20 73 74 72  6     Header str
1473f 69 6e 67 3a 20 22 53 51 4c 69 74 65 20 66 6f 72  ing: "SQLite for
14740 6d 61 74 20 33 5c 30 30 30 22 0a 2a 2a 20 20 20  mat 3\000".**   
14741 20 20 31 36 20 20 20 20 20 20 20 32 20 20 20 20    16       2    
14742 20 50 61 67 65 20 73 69 7a 65 20 69 6e 20 62 79   Page size in by
14743 74 65 73 2e 20 20 0a 2a 2a 20 20 20 20 20 31 38  tes.  .**     18
14744 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69 6c         1     Fil
14745 65 20 66 6f 72 6d 61 74 20 77 72 69 74 65 20 76  e format write v
14746 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 31 39  ersion.**     19
14747 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69 6c         1     Fil
14748 65 20 66 6f 72 6d 61 74 20 72 65 61 64 20 76 65  e format read ve
14749 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 30 20  rsion.**     20 
1474a 20 20 20 20 20 20 31 20 20 20 20 20 42 79 74 65        1     Byte
1474b 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63  s of unused spac
1474c 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20  e at the end of 
1474d 65 61 63 68 20 70 61 67 65 0a 2a 2a 20 20 20 20  each page.**    
1474e 20 32 31 20 20 20 20 20 20 20 31 20 20 20 20 20   21       1     
1474f 4d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79  Max embedded pay
14750 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a  load fraction.**
14751 20 20 20 20 20 32 32 20 20 20 20 20 20 20 31 20       22       1 
14752 20 20 20 20 4d 69 6e 20 65 6d 62 65 64 64 65 64      Min embedded
14753 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
14754 6e 0a 2a 2a 20 20 20 20 20 32 33 20 20 20 20 20  n.**     23     
14755 20 20 31 20 20 20 20 20 4d 69 6e 20 6c 65 61 66    1     Min leaf
14756 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
14757 6e 0a 2a 2a 20 20 20 20 20 32 34 20 20 20 20 20  n.**     24     
14758 20 20 34 20 20 20 20 20 46 69 6c 65 20 63 68 61    4     File cha
14759 6e 67 65 20 63 6f 75 6e 74 65 72 0a 2a 2a 20 20  nge counter.**  
1475a 20 20 20 32 38 20 20 20 20 20 20 20 34 20 20 20     28       4   
1475b 20 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66    Reserved for f
1475c 75 74 75 72 65 20 75 73 65 0a 2a 2a 20 20 20 20  uture use.**    
1475d 20 33 32 20 20 20 20 20 20 20 34 20 20 20 20 20   32       4     
1475e 46 69 72 73 74 20 66 72 65 65 6c 69 73 74 20 70  First freelist p
1475f 61 67 65 0a 2a 2a 20 20 20 20 20 33 36 20 20 20  age.**     36   
14760 20 20 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72      4     Number
14761 20 6f 66 20 66 72 65 65 6c 69 73 74 20 70 61 67   of freelist pag
14762 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a  es in the file.*
14763 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 36 30  *     40      60
14764 20 20 20 20 20 31 35 20 34 2d 62 79 74 65 20 6d       15 4-byte m
14765 65 74 61 20 76 61 6c 75 65 73 20 70 61 73 73 65  eta values passe
14766 64 20 74 6f 20 68 69 67 68 65 72 20 6c 61 79 65  d to higher laye
14767 72 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20 34 30 20  rs.**.**     40 
14768 20 20 20 20 20 20 34 20 20 20 20 20 53 63 68 65        4     Sche
14769 6d 61 20 63 6f 6f 6b 69 65 0a 2a 2a 20 20 20 20  ma cookie.**    
1476a 20 34 34 20 20 20 20 20 20 20 34 20 20 20 20 20   44       4     
1476b 46 69 6c 65 20 66 6f 72 6d 61 74 20 6f 66 20 73  File format of s
1476c 63 68 65 6d 61 20 6c 61 79 65 72 0a 2a 2a 20 20  chema layer.**  
1476d 20 20 20 34 38 20 20 20 20 20 20 20 34 20 20 20     48       4   
1476e 20 20 53 69 7a 65 20 6f 66 20 70 61 67 65 20 63    Size of page c
1476f 61 63 68 65 0a 2a 2a 20 20 20 20 20 35 32 20 20  ache.**     52  
14770 20 20 20 20 20 34 20 20 20 20 20 4c 61 72 67 65       4     Large
14771 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 28 61 75  st root-page (au
14772 74 6f 2f 69 6e 63 72 5f 76 61 63 75 75 6d 29 0a  to/incr_vacuum).
14773 2a 2a 20 20 20 20 20 35 36 20 20 20 20 20 20 20  **     56       
14774 34 20 20 20 20 20 31 3d 55 54 46 2d 38 20 32 3d  4     1=UTF-8 2=
14775 55 54 46 31 36 6c 65 20 33 3d 55 54 46 31 36 62  UTF16le 3=UTF16b
14776 65 0a 2a 2a 20 20 20 20 20 36 30 20 20 20 20 20  e.**     60     
14777 20 20 34 20 20 20 20 20 55 73 65 72 20 76 65 72    4     User ver
14778 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 36 34 20 20  sion.**     64  
14779 20 20 20 20 20 34 20 20 20 20 20 49 6e 63 72 65       4     Incre
1477a 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 6d 6f  mental vacuum mo
1477b 64 65 0a 2a 2a 20 20 20 20 20 36 38 20 20 20 20  de.**     68    
1477c 20 20 20 34 20 20 20 20 20 75 6e 75 73 65 64 0a     4     unused.
1477d 2a 2a 20 20 20 20 20 37 32 20 20 20 20 20 20 20  **     72       
1477e 34 20 20 20 20 20 75 6e 75 73 65 64 0a 2a 2a 20  4     unused.** 
1477f 20 20 20 20 37 36 20 20 20 20 20 20 20 34 20 20      76       4  
14780 20 20 20 75 6e 75 73 65 64 0a 2a 2a 0a 2a 2a 20     unused.**.** 
14781 41 6c 6c 20 6f 66 20 74 68 65 20 69 6e 74 65 67  All of the integ
14782 65 72 20 76 61 6c 75 65 73 20 61 72 65 20 62 69  er values are bi
14783 67 2d 65 6e 64 69 61 6e 20 28 6d 6f 73 74 20 73  g-endian (most s
14784 69 67 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20  ignificant byte 
14785 66 69 72 73 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  first)..**.** Th
14786 65 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63 6f  e file change co
14787 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65  unter is increme
14788 6e 74 65 64 20 77 68 65 6e 20 74 68 65 20 64 61  nted when the da
14789 74 61 62 61 73 65 20 69 73 20 63 68 61 6e 67 65  tabase is change
1478a 64 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6e 74 65  d.** This counte
1478b 72 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70  r allows other p
1478c 72 6f 63 65 73 73 65 73 20 74 6f 20 6b 6e 6f 77  rocesses to know
1478d 20 77 68 65 6e 20 74 68 65 20 66 69 6c 65 20 68   when the file h
1478e 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e  as changed.** an
1478f 64 20 74 68 75 73 20 77 68 65 6e 20 74 68 65 79  d thus when they
14790 20 6e 65 65 64 20 74 6f 20 66 6c 75 73 68 20 74   need to flush t
14791 68 65 69 72 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a  heir cache..**.*
14792 2a 20 54 68 65 20 6d 61 78 20 65 6d 62 65 64 64  * The max embedd
14793 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
14794 69 6f 6e 20 69 73 20 74 68 65 20 61 6d 6f 75 6e  ion is the amoun
14795 74 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 75  t of the total u
14796 73 61 62 6c 65 0a 2a 2a 20 73 70 61 63 65 20 69  sable.** space i
14797 6e 20 61 20 70 61 67 65 20 74 68 61 74 20 63 61  n a page that ca
14798 6e 20 62 65 20 63 6f 6e 73 75 6d 65 64 20 62 79  n be consumed by
14799 20 61 20 73 69 6e 67 6c 65 20 63 65 6c 6c 20 66   a single cell f
1479a 6f 72 20 73 74 61 6e 64 61 72 64 0a 2a 2a 20 42  or standard.** B
1479b 2d 74 72 65 65 20 28 6e 6f 6e 2d 4c 45 41 46 44  -tree (non-LEAFD
1479c 41 54 41 29 20 74 61 62 6c 65 73 2e 20 20 41 20  ATA) tables.  A 
1479d 76 61 6c 75 65 20 6f 66 20 32 35 35 20 6d 65 61  value of 255 mea
1479e 6e 73 20 31 30 30 25 2e 20 20 54 68 65 20 64 65  ns 100%.  The de
1479f 66 61 75 6c 74 0a 2a 2a 20 69 73 20 74 6f 20 6c  fault.** is to l
147a0 69 6d 69 74 20 74 68 65 20 6d 61 78 69 6d 75 6d  imit the maximum
147a1 20 63 65 6c 6c 20 73 69 7a 65 20 73 6f 20 74 68   cell size so th
147a2 61 74 20 61 74 20 6c 65 61 73 74 20 34 20 63 65  at at least 4 ce
147a3 6c 6c 73 20 77 69 6c 6c 20 66 69 74 0a 2a 2a 20  lls will fit.** 
147a4 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 54 68  on one page.  Th
147a5 75 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d  us the default m
147a6 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c  ax embedded payl
147a7 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73 20  oad fraction is 
147a8 36 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  64..**.** If the
147a9 20 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 20 63   payload for a c
147aa 65 6c 6c 20 69 73 20 6c 61 72 67 65 72 20 74 68  ell is larger th
147ab 61 6e 20 74 68 65 20 6d 61 78 20 70 61 79 6c 6f  an the max paylo
147ac 61 64 2c 20 74 68 65 6e 20 65 78 74 72 61 0a 2a  ad, then extra.*
147ad 2a 20 70 61 79 6c 6f 61 64 20 69 73 20 73 70 69  * payload is spi
147ae 6c 6c 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77  lled to overflow
147af 20 70 61 67 65 73 2e 20 20 4f 6e 63 65 20 61 6e   pages.  Once an
147b0 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
147b1 73 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20  s allocated,.** 
147b2 61 73 20 6d 61 6e 79 20 62 79 74 65 73 20 61 73  as many bytes as
147b3 20 70 6f 73 73 69 62 6c 65 20 61 72 65 20 6d 6f   possible are mo
147b4 76 65 64 20 69 6e 74 6f 20 74 68 65 20 6f 76 65  ved into the ove
147b5 72 66 6c 6f 77 20 70 61 67 65 73 20 77 69 74 68  rflow pages with
147b6 6f 75 74 20 6c 65 74 74 69 6e 67 0a 2a 2a 20 74  out letting.** t
147b7 68 65 20 63 65 6c 6c 20 73 69 7a 65 20 64 72 6f  he cell size dro
147b8 70 20 62 65 6c 6f 77 20 74 68 65 20 6d 69 6e 20  p below the min 
147b9 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64  embedded payload
147ba 20 66 72 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   fraction..**.**
147bb 20 54 68 65 20 6d 69 6e 20 6c 65 61 66 20 70 61   The min leaf pa
147bc 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69  yload fraction i
147bd 73 20 6c 69 6b 65 20 74 68 65 20 6d 69 6e 20 65  s like the min e
147be 6d 62 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20  mbedded payload 
147bf 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 65 78 63 65  fraction.** exce
147c0 70 74 20 74 68 61 74 20 69 74 20 61 70 70 6c 69  pt that it appli
147c1 65 73 20 74 6f 20 6c 65 61 66 20 6e 6f 64 65 73  es to leaf nodes
147c2 20 69 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74   in a LEAFDATA t
147c3 72 65 65 2e 20 20 54 68 65 20 6d 61 78 69 6d 75  ree.  The maximu
147c4 6d 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 66 72 61  m.** payload fra
147c5 63 74 69 6f 6e 20 66 6f 72 20 61 20 4c 45 41 46  ction for a LEAF
147c6 44 41 54 41 20 74 72 65 65 20 69 73 20 61 6c 77  DATA tree is alw
147c7 61 79 73 20 31 30 30 25 20 28 6f 72 20 32 35 35  ays 100% (or 255
147c8 29 20 61 6e 64 20 69 74 0a 2a 2a 20 6e 6f 74 20  ) and it.** not 
147c9 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
147ca 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 45   header..**.** E
147cb 61 63 68 20 62 74 72 65 65 20 70 61 67 65 73 20  ach btree pages 
147cc 69 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f 20  is divided into 
147cd 74 68 72 65 65 20 73 65 63 74 69 6f 6e 73 3a 20  three sections: 
147ce 20 54 68 65 20 68 65 61 64 65 72 2c 20 74 68 65   The header, the
147cf 0a 2a 2a 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72  .** cell pointer
147d0 20 61 72 72 61 79 2c 20 61 6e 64 20 74 68 65 20   array, and the 
147d1 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65  cell content are
147d2 61 2e 20 20 50 61 67 65 20 31 20 61 6c 73 6f 20  a.  Page 1 also 
147d3 68 61 73 20 61 20 31 30 30 2d 62 79 74 65 0a 2a  has a 100-byte.*
147d4 2a 20 66 69 6c 65 20 68 65 61 64 65 72 20 74 68  * file header th
147d5 61 74 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65  at occurs before
147d6 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
147d7 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d  ..**.**      |--
147d8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a  --------------|.
147d9 2a 2a 20 20 20 20 20 20 7c 20 66 69 6c 65 20 68  **      | file h
147da 65 61 64 65 72 20 20 20 20 7c 20 20 20 31 30 30  eader    |   100
147db 20 62 79 74 65 73 2e 20 20 50 61 67 65 20 31 20   bytes.  Page 1 
147dc 6f 6e 6c 79 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d  only..**      |-
147dd 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c  ---------------|
147de 0a 2a 2a 20 20 20 20 20 20 7c 20 70 61 67 65 20  .**      | page 
147df 68 65 61 64 65 72 20 20 20 20 7c 20 20 20 38 20  header    |   8 
147e0 62 79 74 65 73 20 66 6f 72 20 6c 65 61 76 65 73  bytes for leaves
147e1 2e 20 20 31 32 20 62 79 74 65 73 20 66 6f 72 20  .  12 bytes for 
147e2 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 0a 2a  interior nodes.*
147e3 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
147e4 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20  --------|.**    
147e5 20 20 7c 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72    | cell pointer
147e6 20 20 20 7c 20 20 20 7c 20 20 32 20 62 79 74 65     |   |  2 byte
147e7 73 20 70 65 72 20 63 65 6c 6c 2e 20 20 53 6f 72  s per cell.  Sor
147e8 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 20  ted order..**   
147e9 20 20 20 7c 20 61 72 72 61 79 20 20 20 20 20 20     | array      
147ea 20 20 20 20 7c 20 20 20 7c 20 20 47 72 6f 77 73      |   |  Grows
147eb 20 64 6f 77 6e 77 61 72 64 0a 2a 2a 20 20 20 20   downward.**    
147ec 20 20 7c 20 20 20 20 20 20 20 20 20 20 20 20 20    |             
147ed 20 20 20 7c 20 20 20 76 0a 2a 2a 20 20 20 20 20     |   v.**     
147ee 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   |--------------
147ef 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 75 6e  --|.**      | un
147f0 61 6c 6c 6f 63 61 74 65 64 20 20 20 20 7c 0a 2a  allocated    |.*
147f1 2a 20 20 20 20 20 20 7c 20 73 70 61 63 65 20 20  *      | space  
147f2 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20          |.**    
147f3 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
147f4 2d 2d 2d 7c 20 20 20 5e 20 20 47 72 6f 77 73 20  ---|   ^  Grows 
147f5 75 70 77 61 72 64 73 0a 2a 2a 20 20 20 20 20 20  upwards.**      
147f6 7c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 20  | cell content  
147f7 20 7c 20 20 20 7c 20 20 41 72 62 69 74 72 61 72   |   |  Arbitrar
147f8 79 20 6f 72 64 65 72 20 69 6e 74 65 72 73 70 65  y order interspe
147f9 72 73 65 64 20 77 69 74 68 20 66 72 65 65 62 6c  rsed with freebl
147fa 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 20  ocks..**      | 
147fb 61 72 65 61 20 20 20 20 20 20 20 20 20 20 20 7c  area           |
147fc 20 20 20 7c 20 20 61 6e 64 20 66 72 65 65 20 73     |  and free s
147fd 70 61 63 65 20 66 72 61 67 6d 65 6e 74 73 2e 0a  pace fragments..
147fe 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d  **      |-------
147ff 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a  ---------|.**.**
14800 20 54 68 65 20 70 61 67 65 20 68 65 61 64 65 72   The page header
14801 73 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69  s looks like thi
14802 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46 46 53 45  s:.**.**   OFFSE
14803 54 20 20 20 53 49 5a 45 20 20 20 20 20 44 45 53  T   SIZE     DES
14804 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
14805 20 30 20 20 20 20 20 20 20 31 20 20 20 20 20 20   0       1      
14806 46 6c 61 67 73 2e 20 31 3a 20 69 6e 74 6b 65 79  Flags. 1: intkey
14807 2c 20 32 3a 20 7a 65 72 6f 64 61 74 61 2c 20 34  , 2: zerodata, 4
14808 3a 20 6c 65 61 66 64 61 74 61 2c 20 38 3a 20 6c  : leafdata, 8: l
14809 65 61 66 0a 2a 2a 20 20 20 20 20 20 31 20 20 20  eaf.**      1   
1480a 20 20 20 20 32 20 20 20 20 20 20 62 79 74 65 20      2      byte 
1480b 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 66 69  offset to the fi
1480c 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a  rst freeblock.**
1480d 20 20 20 20 20 20 33 20 20 20 20 20 20 20 32 20        3       2 
1480e 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 63       number of c
1480f 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70 61 67  ells on this pag
14810 65 0a 2a 2a 20 20 20 20 20 20 35 20 20 20 20 20  e.**      5     
14811 20 20 32 20 20 20 20 20 20 66 69 72 73 74 20 62    2      first b
14812 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  yte of the cell 
14813 63 6f 6e 74 65 6e 74 20 61 72 65 61 0a 2a 2a 20  content area.** 
14814 20 20 20 20 20 37 20 20 20 20 20 20 20 31 20 20       7       1  
14815 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 66 72      number of fr
14816 61 67 6d 65 6e 74 65 64 20 66 72 65 65 20 62 79  agmented free by
14817 74 65 73 0a 2a 2a 20 20 20 20 20 20 38 20 20 20  tes.**      8   
14818 20 20 20 20 34 20 20 20 20 20 20 52 69 67 68 74      4      Right
14819 20 63 68 69 6c 64 20 28 74 68 65 20 50 74 72 28   child (the Ptr(
1481a 4e 29 20 76 61 6c 75 65 29 2e 20 20 4f 6d 69 74  N) value).  Omit
1481b 74 65 64 20 6f 6e 20 6c 65 61 76 65 73 2e 0a 2a  ted on leaves..*
1481c 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 64  *.** The flags d
1481d 65 66 69 6e 65 20 74 68 65 20 66 6f 72 6d 61 74  efine the format
1481e 20 6f 66 20 74 68 69 73 20 62 74 72 65 65 20 70   of this btree p
1481f 61 67 65 2e 20 20 54 68 65 20 6c 65 61 66 20 66  age.  The leaf f
14820 6c 61 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a  lag means that.*
14821 2a 20 74 68 69 73 20 70 61 67 65 20 68 61 73 20  * this page has 
14822 6e 6f 20 63 68 69 6c 64 72 65 6e 2e 20 20 54 68  no children.  Th
14823 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67 20  e zerodata flag 
14824 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69 73 20  means that this 
14825 70 61 67 65 20 63 61 72 72 69 65 73 0a 2a 2a 20  page carries.** 
14826 6f 6e 6c 79 20 6b 65 79 73 20 61 6e 64 20 6e 6f  only keys and no
14827 20 64 61 74 61 2e 20 20 54 68 65 20 69 6e 74 6b   data.  The intk
14828 65 79 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68  ey flag means th
14829 61 74 20 74 68 65 20 6b 65 79 20 69 73 20 61 20  at the key is a 
1482a 69 6e 74 65 67 65 72 0a 2a 2a 20 77 68 69 63 68  integer.** which
1482b 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68   is stored in th
1482c 65 20 6b 65 79 20 73 69 7a 65 20 65 6e 74 72 79  e key size entry
1482d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 68 65 61   of the cell hea
1482e 64 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20  der rather than 
1482f 69 6e 0a 2a 2a 20 74 68 65 20 70 61 79 6c 6f 61  in.** the payloa
14830 64 20 61 72 65 61 2e 0a 2a 2a 0a 2a 2a 20 54 68  d area..**.** Th
14831 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  e cell pointer a
14832 72 72 61 79 20 62 65 67 69 6e 73 20 6f 6e 20 74  rray begins on t
14833 68 65 20 66 69 72 73 74 20 62 79 74 65 20 61 66  he first byte af
14834 74 65 72 20 74 68 65 20 70 61 67 65 20 68 65 61  ter the page hea
14835 64 65 72 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c  der..** The cell
14836 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 63   pointer array c
14837 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20 6f 72 20  ontains zero or 
14838 6d 6f 72 65 20 32 2d 62 79 74 65 20 6e 75 6d 62  more 2-byte numb
14839 65 72 73 20 77 68 69 63 68 20 61 72 65 0a 2a 2a  ers which are.**
1483a 20 6f 66 66 73 65 74 73 20 66 72 6f 6d 20 74 68   offsets from th
1483b 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
1483c 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20 63  he page to the c
1483d 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e 20 74  ell content in t
1483e 68 65 20 63 65 6c 6c 0a 2a 2a 20 63 6f 6e 74 65  he cell.** conte
1483f 6e 74 20 61 72 65 61 2e 20 20 54 68 65 20 63 65  nt area.  The ce
14840 6c 6c 20 70 6f 69 6e 74 65 72 73 20 6f 63 63 75  ll pointers occu
14841 72 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  r in sorted orde
14842 72 2e 20 20 54 68 65 20 73 79 73 74 65 6d 20 73  r.  The system s
14843 74 72 69 76 65 73 0a 2a 2a 20 74 6f 20 6b 65 65  trives.** to kee
14844 70 20 66 72 65 65 20 73 70 61 63 65 20 61 66 74  p free space aft
14845 65 72 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c  er the last cell
14846 20 70 6f 69 6e 74 65 72 20 73 6f 20 74 68 61 74   pointer so that
14847 20 6e 65 77 20 63 65 6c 6c 73 20 63 61 6e 0a 2a   new cells can.*
14848 2a 20 62 65 20 65 61 73 69 6c 79 20 61 64 64 65  * be easily adde
14849 64 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67  d without having
1484a 20 74 6f 20 64 65 66 72 61 67 6d 65 6e 74 20 74   to defragment t
1484b 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 43  he page..**.** C
1484c 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 73  ell content is s
1484d 74 6f 72 65 64 20 61 74 20 74 68 65 20 76 65 72  tored at the ver
1484e 79 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67  y end of the pag
1484f 65 20 61 6e 64 20 67 72 6f 77 73 20 74 6f 77 61  e and grows towa
14850 72 64 20 74 68 65 0a 2a 2a 20 62 65 67 69 6e 6e  rd the.** beginn
14851 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67 65 2e  ing of the page.
14852 0a 2a 2a 0a 2a 2a 20 55 6e 75 73 65 64 20 73 70  .**.** Unused sp
14853 61 63 65 20 77 69 74 68 69 6e 20 74 68 65 20 63  ace within the c
14854 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
14855 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e   is collected in
14856 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74  to a linked list
14857 20 6f 66 0a 2a 2a 20 66 72 65 65 62 6c 6f 63 6b   of.** freeblock
14858 73 2e 20 20 45 61 63 68 20 66 72 65 65 62 6c 6f  s.  Each freeblo
14859 63 6b 20 69 73 20 61 74 20 6c 65 61 73 74 20 34  ck is at least 4
1485a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20   bytes in size. 
1485b 20 54 68 65 20 62 79 74 65 20 6f 66 66 73 65 74   The byte offset
1485c 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69 72 73 74  .** to the first
1485d 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20 67 69   freeblock is gi
1485e 76 65 6e 20 69 6e 20 74 68 65 20 68 65 61 64 65  ven in the heade
1485f 72 2e 20 20 46 72 65 65 62 6c 6f 63 6b 73 20 6f  r.  Freeblocks o
14860 63 63 75 72 20 69 6e 0a 2a 2a 20 69 6e 63 72 65  ccur in.** incre
14861 61 73 69 6e 67 20 6f 72 64 65 72 2e 20 20 42 65  asing order.  Be
14862 63 61 75 73 65 20 61 20 66 72 65 65 62 6c 6f 63  cause a freebloc
14863 6b 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61  k must be at lea
14864 73 74 20 34 20 62 79 74 65 73 20 69 6e 20 73 69  st 4 bytes in si
14865 7a 65 2c 0a 2a 2a 20 61 6e 79 20 67 72 6f 75 70  ze,.** any group
14866 20 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 75   of 3 or fewer u
14867 6e 75 73 65 64 20 62 79 74 65 73 20 69 6e 20 74  nused bytes in t
14868 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
14869 61 72 65 61 20 63 61 6e 6e 6f 74 0a 2a 2a 20 65  area cannot.** e
1486a 78 69 73 74 20 6f 6e 20 74 68 65 20 66 72 65 65  xist on the free
1486b 62 6c 6f 63 6b 20 63 68 61 69 6e 2e 20 20 41 20  block chain.  A 
1486c 67 72 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65  group of 3 or fe
1486d 77 65 72 20 66 72 65 65 20 62 79 74 65 73 20 69  wer free bytes i
1486e 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61 20 66 72  s called.** a fr
1486f 61 67 6d 65 6e 74 2e 20 20 54 68 65 20 74 6f 74  agment.  The tot
14870 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  al number of byt
14871 65 73 20 69 6e 20 61 6c 6c 20 66 72 61 67 6d 65  es in all fragme
14872 6e 74 73 20 69 73 20 72 65 63 6f 72 64 65 64 2e  nts is recorded.
14873 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 67 65 20  .** in the page 
14874 68 65 61 64 65 72 20 61 74 20 6f 66 66 73 65 74  header at offset
14875 20 37 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a   7..**.**    SIZ
14876 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e  E    DESCRIPTION
14877 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42  .**      2     B
14878 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 68  yte offset of th
14879 65 20 6e 65 78 74 20 66 72 65 65 62 6c 6f 63 6b  e next freeblock
1487a 0a 2a 2a 20 20 20 20 20 20 32 20 20 20 20 20 42  .**      2     B
1487b 79 74 65 73 20 69 6e 20 74 68 69 73 20 66 72 65  ytes in this fre
1487c 65 62 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20 43 65 6c  eblock.**.** Cel
1487d 6c 73 20 61 72 65 20 6f 66 20 76 61 72 69 61 62  ls are of variab
1487e 6c 65 20 6c 65 6e 67 74 68 2e 20 20 43 65 6c 6c  le length.  Cell
1487f 73 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20  s are stored in 
14880 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
14881 20 61 72 65 61 20 61 74 0a 2a 2a 20 74 68 65 20   area at.** the 
14882 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 2e  end of the page.
14883 20 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 74 68    Pointers to th
14884 65 20 63 65 6c 6c 73 20 61 72 65 20 69 6e 20 74  e cells are in t
14885 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  he cell pointer 
14886 61 72 72 61 79 0a 2a 2a 20 74 68 61 74 20 69 6d  array.** that im
14887 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77  mediately follow
14888 73 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65  s the page heade
14889 72 2e 20 20 43 65 6c 6c 73 20 69 73 20 6e 6f 74  r.  Cells is not
1488a 20 6e 65 63 65 73 73 61 72 69 6c 79 0a 2a 2a 20   necessarily.** 
1488b 63 6f 6e 74 69 67 75 6f 75 73 20 6f 72 20 69 6e  contiguous or in
1488c 20 6f 72 64 65 72 2c 20 62 75 74 20 63 65 6c 6c   order, but cell
1488d 20 70 6f 69 6e 74 65 72 73 20 61 72 65 20 63 6f   pointers are co
1488e 6e 74 69 67 75 6f 75 73 20 61 6e 64 20 69 6e 20  ntiguous and in 
1488f 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c  order..**.** Cel
14890 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 6b 65 73 20  l content makes 
14891 75 73 65 20 6f 66 20 76 61 72 69 61 62 6c 65 20  use of variable 
14892 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73 2e  length integers.
14893 20 20 41 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20    A variable.** 
14894 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 69  length integer i
14895 73 20 31 20 74 6f 20 39 20 62 79 74 65 73 20 77  s 1 to 9 bytes w
14896 68 65 72 65 20 74 68 65 20 6c 6f 77 65 72 20 37  here the lower 7
14897 20 62 69 74 73 20 6f 66 20 65 61 63 68 20 0a 2a   bits of each .*
14898 2a 20 62 79 74 65 20 61 72 65 20 75 73 65 64 2e  * byte are used.
14899 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 63 6f    The integer co
1489a 6e 73 69 73 74 73 20 6f 66 20 61 6c 6c 20 62 79  nsists of all by
1489b 74 65 73 20 74 68 61 74 20 68 61 76 65 20 62 69  tes that have bi
1489c 74 20 38 20 73 65 74 20 61 6e 64 0a 2a 2a 20 74  t 8 set and.** t
1489d 68 65 20 66 69 72 73 74 20 62 79 74 65 20 77 69  he first byte wi
1489e 74 68 20 62 69 74 20 38 20 63 6c 65 61 72 2e 20  th bit 8 clear. 
1489f 20 54 68 65 20 6d 6f 73 74 20 73 69 67 6e 69 66   The most signif
148a0 69 63 61 6e 74 20 62 79 74 65 20 6f 66 20 74 68  icant byte of th
148a1 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 61 70 70  e integer.** app
148a2 65 61 72 73 20 66 69 72 73 74 2e 20 20 41 20 76  ears first.  A v
148a3 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
148a4 6e 74 65 67 65 72 20 6d 61 79 20 6e 6f 74 20 62  nteger may not b
148a5 65 20 6d 6f 72 65 20 74 68 61 6e 20 39 20 62 79  e more than 9 by
148a6 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 41 73 20  tes long..** As 
148a7 61 20 73 70 65 63 69 61 6c 20 63 61 73 65 2c 20  a special case, 
148a8 61 6c 6c 20 38 20 62 79 74 65 73 20 6f 66 20 74  all 8 bytes of t
148a9 68 65 20 39 74 68 20 62 79 74 65 20 61 72 65 20  he 9th byte are 
148aa 75 73 65 64 20 61 73 20 64 61 74 61 2e 20 20 54  used as data.  T
148ab 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73 20 61 20  his.** allows a 
148ac 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 74  64-bit integer t
148ad 6f 20 62 65 20 65 6e 63 6f 64 65 64 20 69 6e 20  o be encoded in 
148ae 39 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  9 bytes..**.**  
148af 20 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20    0x00          
148b0 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f              beco
148b1 6d 65 73 20 20 30 78 30 30 30 30 30 30 30 30 0a  mes  0x00000000.
148b2 2a 2a 20 20 20 20 30 78 37 66 20 20 20 20 20 20  **    0x7f      
148b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
148b4 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30  becomes  0x00000
148b5 30 37 66 0a 2a 2a 20 20 20 20 30 78 38 31 20 30  07f.**    0x81 0
148b6 78 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20  x00             
148b7 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30      becomes  0x0
148b8 30 30 30 30 30 38 30 0a 2a 2a 20 20 20 20 30 78  0000080.**    0x
148b9 38 32 20 30 78 30 30 20 20 20 20 20 20 20 20 20  82 0x00         
148ba 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20          becomes 
148bb 20 30 78 30 30 30 30 30 31 30 30 0a 2a 2a 20 20   0x00000100.**  
148bc 20 20 30 78 38 30 20 30 78 37 66 20 20 20 20 20    0x80 0x7f     
148bd 20 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f              beco
148be 6d 65 73 20 20 30 78 30 30 30 30 30 30 37 66 0a  mes  0x0000007f.
148bf 2a 2a 20 20 20 20 30 78 38 61 20 30 78 39 31 20  **    0x8a 0x91 
148c0 30 78 64 31 20 30 78 61 63 20 30 78 37 38 20 20  0xd1 0xac 0x78  
148c1 62 65 63 6f 6d 65 73 20 20 30 78 31 32 33 34 35  becomes  0x12345
148c2 36 37 38 0a 2a 2a 20 20 20 20 30 78 38 31 20 30  678.**    0x81 0
148c3 78 38 31 20 30 78 38 31 20 30 78 38 31 20 30 78  x81 0x81 0x81 0x
148c4 30 31 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31  01  becomes  0x1
148c5 30 32 30 34 30 38 31 0a 2a 2a 0a 2a 2a 20 56 61  0204081.**.** Va
148c6 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e  riable length in
148c7 74 65 67 65 72 73 20 61 72 65 20 75 73 65 64 20  tegers are used 
148c8 66 6f 72 20 72 6f 77 69 64 73 20 61 6e 64 20 74  for rowids and t
148c9 6f 20 68 6f 6c 64 20 74 68 65 20 6e 75 6d 62 65  o hold the numbe
148ca 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 6f 66  r of.** bytes of
148cb 20 6b 65 79 20 61 6e 64 20 64 61 74 61 20 69 6e   key and data in
148cc 20 61 20 62 74 72 65 65 20 63 65 6c 6c 2e 0a 2a   a btree cell..*
148cd 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74  *.** The content
148ce 20 6f 66 20 61 20 63 65 6c 6c 20 6c 6f 6f 6b 73   of a cell looks
148cf 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
148d0 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44 45 53  *    SIZE    DES
148d1 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  CRIPTION.**     
148d2 20 34 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62   4     Page numb
148d3 65 72 20 6f 66 20 74 68 65 20 6c 65 66 74 20 63  er of the left c
148d4 68 69 6c 64 2e 20 4f 6d 69 74 74 65 64 20 69 66  hild. Omitted if
148d5 20 6c 65 61 66 20 66 6c 61 67 20 69 73 20 73 65   leaf flag is se
148d6 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20  t..**     var   
148d7 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
148d8 20 6f 66 20 64 61 74 61 2e 20 4f 6d 69 74 74 65   of data. Omitte
148d9 64 20 69 66 20 74 68 65 20 7a 65 72 6f 64 61 74  d if the zerodat
148da 61 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a  a flag is set..*
148db 2a 20 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d  *     var    Num
148dc 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
148dd 6b 65 79 2e 20 4f 72 20 74 68 65 20 6b 65 79 20  key. Or the key 
148de 69 74 73 65 6c 66 20 69 66 20 69 6e 74 6b 65 79  itself if intkey
148df 20 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a   flag is set..**
148e0 20 20 20 20 20 20 2a 20 20 20 20 20 50 61 79 6c        *     Payl
148e1 6f 61 64 0a 2a 2a 20 20 20 20 20 20 34 20 20 20  oad.**      4   
148e2 20 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20    First page of 
148e3 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 68 61  the overflow cha
148e4 69 6e 2e 20 20 4f 6d 69 74 74 65 64 20 69 66 20  in.  Omitted if 
148e5 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 0a 2a  no overflow.**.*
148e6 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  * Overflow pages
148e7 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c   form a linked l
148e8 69 73 74 2e 20 20 45 61 63 68 20 70 61 67 65 20  ist.  Each page 
148e9 65 78 63 65 70 74 20 74 68 65 20 6c 61 73 74 20  except the last 
148ea 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a  is completely.**
148eb 20 66 69 6c 6c 65 64 20 77 69 74 68 20 64 61 74   filled with dat
148ec 61 20 28 70 61 67 65 73 69 7a 65 20 2d 20 34 20  a (pagesize - 4 
148ed 62 79 74 65 73 29 2e 20 20 54 68 65 20 6c 61 73  bytes).  The las
148ee 74 20 70 61 67 65 20 63 61 6e 20 68 61 76 65 20  t page can have 
148ef 61 73 20 6c 69 74 74 6c 65 0a 2a 2a 20 61 73 20  as little.** as 
148f0 31 20 62 79 74 65 20 6f 66 20 64 61 74 61 2e 0a  1 byte of data..
148f1 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20  **.**    SIZE   
148f2 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20   DESCRIPTION.** 
148f3 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20       4     Page 
148f4 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 6f  number of next o
148f5 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 2a 2a 20  verflow page.** 
148f6 20 20 20 20 20 2a 20 20 20 20 20 44 61 74 61 0a       *     Data.
148f7 2a 2a 0a 2a 2a 20 46 72 65 65 6c 69 73 74 20 70  **.** Freelist p
148f8 61 67 65 73 20 63 6f 6d 65 20 69 6e 20 74 77 6f  ages come in two
148f9 20 73 75 62 74 79 70 65 73 3a 20 74 72 75 6e 6b   subtypes: trunk
148fa 20 70 61 67 65 73 20 61 6e 64 20 6c 65 61 66 20   pages and leaf 
148fb 70 61 67 65 73 2e 20 20 54 68 65 0a 2a 2a 20 66  pages.  The.** f
148fc 69 6c 65 20 68 65 61 64 65 72 20 70 6f 69 6e 74  ile header point
148fd 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 69  s to the first i
148fe 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  n a linked list 
148ff 6f 66 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 20  of trunk page.  
14900 45 61 63 68 20 74 72 75 6e 6b 0a 2a 2a 20 70 61  Each trunk.** pa
14901 67 65 20 70 6f 69 6e 74 73 20 74 6f 20 6d 75 6c  ge points to mul
14902 74 69 70 6c 65 20 6c 65 61 66 20 70 61 67 65 73  tiple leaf pages
14903 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f  .  The content o
14904 66 20 61 20 6c 65 61 66 20 70 61 67 65 20 69 73  f a leaf page is
14905 0a 2a 2a 20 75 6e 73 70 65 63 69 66 69 65 64 2e  .** unspecified.
14906 20 20 41 20 74 72 75 6e 6b 20 70 61 67 65 20 6c    A trunk page l
14907 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a  ooks like this:.
14908 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20  **.**    SIZE   
14909 20 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20   DESCRIPTION.** 
1490a 20 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20       4     Page 
1490b 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78 74 20 74  number of next t
1490c 72 75 6e 6b 20 70 61 67 65 0a 2a 2a 20 20 20 20  runk page.**    
1490d 20 20 34 20 20 20 20 20 4e 75 6d 62 65 72 20 6f    4     Number o
1490e 66 20 6c 65 61 66 20 70 6f 69 6e 74 65 72 73 20  f leaf pointers 
1490f 6f 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20  on this page.** 
14910 20 20 20 20 20 2a 20 20 20 20 20 7a 65 72 6f 20       *     zero 
14911 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 20 6e 75  or more pages nu
14912 6d 62 65 72 73 20 6f 66 20 6c 65 61 76 65 73 0a  mbers of leaves.
14913 2a 2f 0a 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c  */.../* The foll
14914 6f 77 69 6e 67 20 76 61 6c 75 65 20 69 73 20 74  owing value is t
14915 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20  he maximum cell 
14916 73 69 7a 65 20 61 73 73 75 6d 69 6e 67 20 61 20  size assuming a 
14917 6d 61 78 69 6d 75 6d 20 70 61 67 65 0a 2a 2a 20  maximum page.** 
14918 73 69 7a 65 20 67 69 76 65 20 61 62 6f 76 65 2e  size give above.
14919 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f 43  .*/.#define MX_C
1491a 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 20 28  ELL_SIZE(pBt)  (
1491b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 38 29  pBt->pageSize-8)
1491c 0a 0a 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d  ../* The maximum
1491d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   number of cells
1491e 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67   on a single pag
1491f 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
14920 65 2e 20 20 54 68 69 73 0a 2a 2a 20 61 73 73 75  e.  This.** assu
14921 6d 65 73 20 61 20 6d 69 6e 69 6d 75 6d 20 63 65  mes a minimum ce
14922 6c 6c 20 73 69 7a 65 20 6f 66 20 36 20 62 79 74  ll size of 6 byt
14923 65 73 20 20 28 34 20 62 79 74 65 73 20 66 6f 72  es  (4 bytes for
14924 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66   the cell itself
14925 0a 2a 2a 20 70 6c 75 73 20 32 20 62 79 74 65 73  .** plus 2 bytes
14926 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 20 74   for the index t
14927 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 74 68  o the cell in th
14928 65 20 70 61 67 65 20 68 65 61 64 65 72 29 2e 20  e page header). 
14929 20 53 75 63 68 0a 2a 2a 20 73 6d 61 6c 6c 20 63   Such.** small c
1492a 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20 72 61 72  ells will be rar
1492b 65 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 20  e, but they are 
1492c 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a 23 64 65  possible..*/.#de
1492d 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28 70 42 74  fine MX_CELL(pBt
1492e 29 20 28 28 70 42 74 2d 3e 70 61 67 65 53 69 7a  ) ((pBt->pageSiz
1492f 65 2d 38 29 2f 36 29 0a 0a 2f 2a 20 46 6f 72 77  e-8)/6)../* Forw
14930 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
14931 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75   */.typedef stru
14932 63 74 20 4d 65 6d 50 61 67 65 20 4d 65 6d 50 61  ct MemPage MemPa
14933 67 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ge;.typedef stru
14934 63 74 20 42 74 4c 6f 63 6b 20 42 74 4c 6f 63 6b  ct BtLock BtLock
14935 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  ;../*.** This is
14936 20 61 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20   a magic string 
14937 74 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20  that appears at 
14938 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
14939 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65   every.** SQLite
1493a 20 64 61 74 61 62 61 73 65 20 69 6e 20 6f 72 64   database in ord
1493b 65 72 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74  er to identify t
1493c 68 65 20 66 69 6c 65 20 61 73 20 61 20 72 65 61  he file as a rea
1493d 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  l database..**.*
1493e 2a 20 59 6f 75 20 63 61 6e 20 63 68 61 6e 67 65  * You can change
1493f 20 74 68 69 73 20 76 61 6c 75 65 20 61 74 20 63   this value at c
14940 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62 79 20 73  ompile-time by s
14941 70 65 63 69 66 79 69 6e 67 20 61 0a 2a 2a 20 2d  pecifying a.** -
14942 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41  DSQLITE_FILE_HEA
14943 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20 74 68 65  DER="..." on the
14944 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e   compiler comman
14945 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20  d-line.  The.** 
14946 68 65 61 64 65 72 20 6d 75 73 74 20 62 65 20 65  header must be e
14947 78 61 63 74 6c 79 20 31 36 20 62 79 74 65 73 20  xactly 16 bytes 
14948 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 7a 65  including the ze
14949 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20 73 6f  ro-terminator so
1494a 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69  .** the string i
1494b 74 73 65 6c 66 20 73 68 6f 75 6c 64 20 62 65 20  tself should be 
1494c 31 35 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f  15 characters lo
1494d 6e 67 2e 20 20 49 66 20 79 6f 75 20 63 68 61 6e  ng.  If you chan
1494e 67 65 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72  ge.** the header
1494f 2c 20 74 68 65 6e 20 79 6f 75 72 20 63 75 73 74  , then your cust
14950 6f 6d 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20  om library will 
14951 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 72  not be able to r
14952 65 61 64 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  ead .** database
14953 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 74  s generated by t
14954 68 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c  he standard tool
14955 73 20 61 6e 64 20 74 68 65 20 73 74 61 6e 64 61  s and the standa
14956 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77 69 6c 6c  rd tools.** will
14957 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20   not be able to 
14958 72 65 61 64 20 64 61 74 61 62 61 73 65 73 20 63  read databases c
14959 72 65 61 74 65 64 20 62 79 20 79 6f 75 72 20 63  reated by your c
1495a 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 2e 0a 2a  ustom library..*
1495b 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1495c 5f 46 49 4c 45 5f 48 45 41 44 45 52 20 2f 2a 20  _FILE_HEADER /* 
1495d 31 32 33 34 35 36 37 38 39 20 31 32 33 34 35 36  123456789 123456
1495e 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65 20 53 51   */.#  define SQ
1495f 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52  LITE_FILE_HEADER
14960 20 22 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20   "SQLite format 
14961 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  3".#endif../*.**
14962 20 50 61 67 65 20 74 79 70 65 20 66 6c 61 67 73   Page type flags
14963 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f 6d 62 69  .  An ORed combi
14964 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  nation of these 
14965 66 6c 61 67 73 20 61 70 70 65 61 72 20 61 73 20  flags appear as 
14966 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 62 79 74  the.** first byt
14967 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20 69 6d 61  e of on-disk ima
14968 67 65 20 6f 66 20 65 76 65 72 79 20 42 54 72 65  ge of every BTre
14969 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64 65 66 69  e page..*/.#defi
1496a 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59 20 20 20  ne PTF_INTKEY   
1496b 20 30 78 30 31 0a 23 64 65 66 69 6e 65 20 50 54   0x01.#define PT
1496c 46 5f 5a 45 52 4f 44 41 54 41 20 20 30 78 30 32  F_ZERODATA  0x02
1496d 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41  .#define PTF_LEA
1496e 46 44 41 54 41 20 20 30 78 30 34 0a 23 64 65 66  FDATA  0x04.#def
1496f 69 6e 65 20 50 54 46 5f 4c 45 41 46 20 20 20 20  ine PTF_LEAF    
14970 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a 20 41 73    0x08../*.** As
14971 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74 68   each page of th
14972 65 20 66 69 6c 65 20 69 73 20 6c 6f 61 64 65 64  e file is loaded
14973 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c 20 61 6e   into memory, an
14974 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
14975 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74   following.** st
14976 72 75 63 74 75 72 65 20 69 73 20 61 70 70 65 6e  ructure is appen
14977 64 65 64 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ded and initiali
14978 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20 20 54 68  zed to zero.  Th
14979 69 73 20 73 74 72 75 63 74 75 72 65 20 73 74 6f  is structure sto
1497a 72 65 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  res.** informati
1497b 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 70 61 67  on about the pag
1497c 65 20 74 68 61 74 20 69 73 20 64 65 63 6f 64 65  e that is decode
1497d 64 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 66  d from the raw f
1497e 69 6c 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  ile page..**.** 
1497f 54 68 65 20 70 50 61 72 65 6e 74 20 66 69 65 6c  The pParent fiel
14980 64 20 70 6f 69 6e 74 73 20 62 61 63 6b 20 74 6f  d points back to
14981 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
14982 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 75  .  This allows u
14983 73 20 74 6f 0a 2a 2a 20 77 61 6c 6b 20 75 70 20  s to.** walk up 
14984 74 68 65 20 42 54 72 65 65 20 66 72 6f 6d 20 61  the BTree from a
14985 6e 79 20 6c 65 61 66 20 74 6f 20 74 68 65 20 72  ny leaf to the r
14986 6f 6f 74 2e 20 20 43 61 72 65 20 6d 75 73 74 20  oot.  Care must 
14987 62 65 20 74 61 6b 65 6e 20 74 6f 0a 2a 2a 20 75  be taken to.** u
14988 6e 72 65 66 28 29 20 74 68 65 20 70 61 72 65 6e  nref() the paren
14989 74 20 70 61 67 65 20 70 6f 69 6e 74 65 72 20 77  t page pointer w
1498a 68 65 6e 20 74 68 69 73 20 70 61 67 65 20 69 73  hen this page is
1498b 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 66 65 72   no longer refer
1498c 65 6e 63 65 64 2e 0a 2a 2a 20 54 68 65 20 70 61  enced..** The pa
1498d 67 65 44 65 73 74 72 75 63 74 6f 72 28 29 20 72  geDestructor() r
1498e 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65 73 20 74  outine handles t
1498f 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2a 0a 2a 2a  hat chore..**.**
14990 20 41 63 63 65 73 73 20 74 6f 20 61 6c 6c 20 66   Access to all f
14991 69 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74  ields of this st
14992 72 75 63 74 75 72 65 20 69 73 20 63 6f 6e 74 72  ructure is contr
14993 6f 6c 6c 65 64 20 62 79 20 74 68 65 20 6d 75 74  olled by the mut
14994 65 78 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  ex.** stored in 
14995 4d 65 6d 50 61 67 65 2e 70 42 74 2d 3e 6d 75 74  MemPage.pBt->mut
14996 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65  ex..*/.struct Me
14997 6d 50 61 67 65 20 7b 0a 20 20 75 38 20 69 73 49  mPage {.  u8 isI
14998 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  nit;           /
14999 2a 20 54 72 75 65 20 69 66 20 70 72 65 76 69 6f  * True if previo
1499a 75 73 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64  usly initialized
1499b 2e 20 4d 55 53 54 20 42 45 20 46 49 52 53 54 21  . MUST BE FIRST!
1499c 20 2a 2f 0a 20 20 75 38 20 6e 4f 76 65 72 66 6c   */.  u8 nOverfl
1499d 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ow;        /* Nu
1499e 6d 62 65 72 20 6f 66 20 6f 76 65 72 66 6c 6f 77  mber of overflow
1499f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 69 6e 20   cell bodies in 
149a0 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20  aCell[] */.  u8 
149a1 69 6e 74 4b 65 79 3b 20 20 20 20 20 20 20 20 20  intKey;         
149a2 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74    /* True if int
149a3 6b 65 79 20 66 6c 61 67 20 69 73 20 73 65 74 20  key flag is set 
149a4 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b 20 20 20  */.  u8 leaf;   
149a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
149a6 65 20 69 66 20 6c 65 61 66 20 66 6c 61 67 20 69  e if leaf flag i
149a7 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20 68 61  s set */.  u8 ha
149a8 73 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  sData;          
149a9 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
149aa 70 61 67 65 20 73 74 6f 72 65 73 20 64 61 74 61  page stores data
149ab 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f 66 66 73   */.  u8 hdrOffs
149ac 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 31 30  et;        /* 10
149ad 30 20 66 6f 72 20 70 61 67 65 20 31 2e 20 20 30  0 for page 1.  0
149ae 20 6f 74 68 65 72 77 69 73 65 20 2a 2f 0a 20 20   otherwise */.  
149af 75 38 20 63 68 69 6c 64 50 74 72 53 69 7a 65 3b  u8 childPtrSize;
149b0 20 20 20 20 20 2f 2a 20 30 20 69 66 20 6c 65 61       /* 0 if lea
149b1 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c 65 61 66  f==1.  4 if leaf
149b2 3d 3d 30 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78  ==0 */.  u16 max
149b3 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a  Local;        /*
149b4 20 43 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65   Copy of BtShare
149b5 64 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72 20 42 74  d.maxLocal or Bt
149b6 53 68 61 72 65 64 2e 6d 61 78 4c 65 61 66 20 2a  Shared.maxLeaf *
149b7 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f 63 61 6c  /.  u16 minLocal
149b8 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79  ;        /* Copy
149b9 20 6f 66 20 42 74 53 68 61 72 65 64 2e 6d 69 6e   of BtShared.min
149ba 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68 61 72 65  Local or BtShare
149bb 64 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a 20 20 75  d.minLeaf */.  u
149bc 31 36 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20  16 cellOffset;  
149bd 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
149be 61 44 61 74 61 20 6f 66 20 66 69 72 73 74 20 63  aData of first c
149bf 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ell pointer */. 
149c0 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20   u16 nFree;     
149c1 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
149c2 6f 66 20 66 72 65 65 20 62 79 74 65 73 20 6f 6e  of free bytes on
149c3 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75   the page */.  u
149c4 31 36 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20  16 nCell;       
149c5 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
149c6 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20 70   cells on this p
149c7 61 67 65 2c 20 6c 6f 63 61 6c 20 61 6e 64 20 6f  age, local and o
149c8 76 66 6c 20 2a 2f 0a 20 20 75 31 36 20 6d 61 73  vfl */.  u16 mas
149c9 6b 50 61 67 65 3b 20 20 20 20 20 20 20 20 2f 2a  kPage;        /*
149ca 20 4d 61 73 6b 20 66 6f 72 20 70 61 67 65 20 6f   Mask for page o
149cb 66 66 73 65 74 20 2a 2f 0a 20 20 73 74 72 75 63  ffset */.  struc
149cc 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 7b 20 20 20  t _OvflCell {   
149cd 2f 2a 20 43 65 6c 6c 73 20 74 68 61 74 20 77 69  /* Cells that wi
149ce 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 44  ll not fit on aD
149cf 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20  ata[] */.    u8 
149d0 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20  *pCell;         
149d1 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20   /* Pointers to 
149d2 74 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20  the body of the 
149d3 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f  overflow cell */
149d4 0a 20 20 20 20 75 31 36 20 69 64 78 3b 20 20 20  .    u16 idx;   
149d5 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65           /* Inse
149d6 72 74 20 74 68 69 73 20 63 65 6c 6c 20 62 65 66  rt this cell bef
149d7 6f 72 65 20 69 64 78 2d 74 68 20 6e 6f 6e 2d 6f  ore idx-th non-o
149d8 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 2a 2f 0a  verflow cell */.
149d9 20 20 7d 20 61 4f 76 66 6c 5b 35 5d 3b 0a 20 20    } aOvfl[5];.  
149da 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20  BtShared *pBt;  
149db 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
149dc 74 6f 20 42 74 53 68 61 72 65 64 20 74 68 61 74  to BtShared that
149dd 20 74 68 69 73 20 70 61 67 65 20 69 73 20 70 61   this page is pa
149de 72 74 20 6f 66 20 2a 2f 0a 20 20 75 38 20 2a 61  rt of */.  u8 *a
149df 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
149e0 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 69  /* Pointer to di
149e1 73 6b 20 69 6d 61 67 65 20 6f 66 20 74 68 65 20  sk image of the 
149e2 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 44  page data */.  D
149e3 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20  bPage *pDbPage; 
149e4 20 20 20 20 2f 2a 20 50 61 67 65 72 20 70 61 67      /* Pager pag
149e5 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50 67  e handle */.  Pg
149e6 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20  no pgno;        
149e7 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65     /* Page numbe
149e8 72 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 20  r for this page 
149e9 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
149ea 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d 61 67 65   in-memory image
149eb 20 6f 66 20 61 20 64 69 73 6b 20 70 61 67 65 20   of a disk page 
149ec 68 61 73 20 74 68 65 20 61 75 78 69 6c 69 61 72  has the auxiliar
149ed 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70  y information ap
149ee 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65  pended.** to the
149ef 20 65 6e 64 2e 20 20 45 58 54 52 41 5f 53 49 5a   end.  EXTRA_SIZ
149f0 45 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  E is the number 
149f1 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  of bytes of spac
149f2 65 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64  e needed to hold
149f3 0a 2a 2a 20 74 68 61 74 20 65 78 74 72 61 20 69  .** that extra i
149f4 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23  nformation..*/.#
149f5 64 65 66 69 6e 65 20 45 58 54 52 41 5f 53 49 5a  define EXTRA_SIZ
149f6 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65  E sizeof(MemPage
149f7 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b 65  )../*.** A linke
149f8 64 20 6c 69 73 74 20 6f 66 20 74 68 65 20 66 6f  d list of the fo
149f9 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
149fa 65 73 20 69 73 20 73 74 6f 72 65 64 20 61 74 20  es is stored at 
149fb 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 2e 0a  BtShared.pLock..
149fc 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20 61 64 64  ** Locks are add
149fd 65 64 20 28 6f 72 20 75 70 67 72 61 64 65 64 20  ed (or upgraded 
149fe 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43 4b 20 74  from READ_LOCK t
149ff 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 77 68  o WRITE_LOCK) wh
14a00 65 6e 20 61 20 63 75 72 73 6f 72 20 0a 2a 2a 20  en a cursor .** 
14a01 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65  is opened on the
14a02 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74   table with root
14a03 20 70 61 67 65 20 42 74 53 68 61 72 65 64 2e 69   page BtShared.i
14a04 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20 61 72 65  Table. Locks are
14a05 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f 6d   removed.** from
14a06 20 74 68 69 73 20 6c 69 73 74 20 77 68 65 6e 20   this list when 
14a07 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
14a08 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f   committed or ro
14a09 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72 20 77 68  lled back, or wh
14a0a 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65 20 68 61  en.** a btree ha
14a0b 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e 0a  ndle is closed..
14a0c 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c 6f 63 6b  */.struct BtLock
14a0d 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72   {.  Btree *pBtr
14a0e 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 74  ee;        /* Bt
14a0f 72 65 65 20 68 61 6e 64 6c 65 20 68 6f 6c 64 69  ree handle holdi
14a10 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20 2a 2f 0a  ng this lock */.
14a11 20 20 50 67 6e 6f 20 69 54 61 62 6c 65 3b 20 20    Pgno iTable;  
14a12 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20          /* Root 
14a13 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 2a 2f  page of table */
14a14 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20 20  .  u8 eLock;    
14a15 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41 44           /* READ
14a16 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c  _LOCK or WRITE_L
14a17 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20  OCK */.  BtLock 
14a18 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 2f  *pNext;        /
14a19 2a 20 4e 65 78 74 20 69 6e 20 42 74 53 68 61 72  * Next in BtShar
14a1a 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 20 2a 2f  ed.pLock list */
14a1b 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69 64 61 74  .};../* Candidat
14a1c 65 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74 4c  e values for BtL
14a1d 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64 65  ock.eLock */.#de
14a1e 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 4b 20 20  fine READ_LOCK  
14a1f 20 20 20 31 0a 23 64 65 66 69 6e 65 20 57 52 49     1.#define WRI
14a20 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f 2a  TE_LOCK    2../*
14a21 20 41 20 42 74 72 65 65 20 68 61 6e 64 6c 65 0a   A Btree handle.
14a22 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62 61 73 65  **.** A database
14a23 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 74   connection cont
14a24 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ains a pointer t
14a25 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  o an instance of
14a26 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65 63 74 20  .** this object 
14a27 66 6f 72 20 65 76 65 72 79 20 64 61 74 61 62 61  for every databa
14a28 73 65 20 66 69 6c 65 20 74 68 61 74 20 69 74 20  se file that it 
14a29 68 61 73 20 6f 70 65 6e 2e 20 20 54 68 69 73 20  has open.  This 
14a2a 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20  structure.** is 
14a2b 6f 70 61 71 75 65 20 74 6f 20 74 68 65 20 64 61  opaque to the da
14a2c 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14a2d 6e 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65  n.  The database
14a2e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 6e   connection cann
14a2f 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65 20 69 6e  ot.** see the in
14a30 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73 20  ternals of this 
14a31 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 6f 6e  structure and on
14a32 6c 79 20 64 65 61 6c 73 20 77 69 74 68 20 70 6f  ly deals with po
14a33 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69  inters to.** thi
14a34 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  s structure..**.
14a35 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64 61 74 61  ** For some data
14a36 62 61 73 65 20 66 69 6c 65 73 2c 20 74 68 65 20  base files, the 
14a37 73 61 6d 65 20 75 6e 64 65 72 6c 79 69 6e 67 20  same underlying 
14a38 64 61 74 61 62 61 73 65 20 63 61 63 68 65 20 6d  database cache m
14a39 69 67 68 74 20 62 65 20 0a 2a 2a 20 73 68 61 72  ight be .** shar
14a3a 65 64 20 62 65 74 77 65 65 6e 20 6d 75 6c 74 69  ed between multi
14a3b 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ple connections.
14a3c 20 20 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20    In that case, 
14a3d 65 61 63 68 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  each connection.
14a3e 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e 20 69 6e  ** has it own in
14a3f 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
14a40 62 6a 65 63 74 2e 20 20 42 75 74 20 65 61 63 68  bject.  But each
14a41 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
14a42 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e  s object.** poin
14a43 74 73 20 74 6f 20 74 68 65 20 73 61 6d 65 20 42  ts to the same B
14a44 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 20  tShared object. 
14a45 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 61   The database ca
14a46 63 68 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73  che and the.** s
14a47 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64  chema associated
14a48 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
14a49 73 65 20 66 69 6c 65 20 61 72 65 20 61 6c 6c 20  se file are all 
14a4a 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e  contained within
14a4b 0a 2a 2a 20 74 68 65 20 42 74 53 68 61 72 65 64  .** the BtShared
14a4c 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41   object..**.** A
14a4d 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20 74 68 69  ll fields in thi
14a4e 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
14a4f 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 73  accessed under s
14a50 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a  qlite3.mutex..**
14a51 20 54 68 65 20 70 42 74 20 70 6f 69 6e 74 65 72   The pBt pointer
14a52 20 69 74 73 65 6c 66 20 6d 61 79 20 6e 6f 74 20   itself may not 
14a53 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65  be changed while
14a54 20 74 68 65 72 65 20 65 78 69 73 74 73 20 63 75   there exists cu
14a55 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20 74 68 65  rsors .** in the
14a56 20 72 65 66 65 72 65 6e 63 65 64 20 42 74 53 68   referenced BtSh
14a57 61 72 65 64 20 74 68 61 74 20 70 6f 69 6e 74 20  ared that point 
14a58 62 61 63 6b 20 74 6f 20 74 68 69 73 20 42 74 72  back to this Btr
14a59 65 65 20 73 69 6e 63 65 20 74 68 6f 73 65 0a 2a  ee since those.*
14a5a 2a 20 63 75 72 73 6f 72 73 20 68 61 76 65 20 74  * cursors have t
14a5b 6f 20 64 6f 20 67 6f 20 74 68 72 6f 75 67 68 20  o do go through 
14a5c 74 68 69 73 20 42 74 72 65 65 20 74 6f 20 66 69  this Btree to fi
14a5d 6e 64 20 74 68 65 69 72 20 42 74 53 68 61 72 65  nd their BtShare
14a5e 64 20 61 6e 64 0a 2a 2a 20 74 68 65 79 20 6f 66  d and.** they of
14a5f 74 65 6e 20 64 6f 20 73 6f 20 77 69 74 68 6f 75  ten do so withou
14a60 74 20 68 6f 6c 64 69 6e 67 20 73 71 6c 69 74 65  t holding sqlite
14a61 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75  3.mutex..*/.stru
14a62 63 74 20 42 74 72 65 65 20 7b 0a 20 20 73 71 6c  ct Btree {.  sql
14a63 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
14a64 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
14a65 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 69  connection holdi
14a66 6e 67 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f  ng this btree */
14a67 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
14a68 3b 20 20 20 20 20 2f 2a 20 53 68 61 72 61 62 6c  ;     /* Sharabl
14a69 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69  e content of thi
14a6a 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 75 38 20  s btree */.  u8 
14a6b 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20  inTrans;        
14a6c 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54  /* TRANS_NONE, T
14a6d 52 41 4e 53 5f 52 45 41 44 20 6f 72 20 54 52 41  RANS_READ or TRA
14a6e 4e 53 5f 57 52 49 54 45 20 2a 2f 0a 20 20 75 38  NS_WRITE */.  u8
14a6f 20 73 68 61 72 61 62 6c 65 3b 20 20 20 20 20 20   sharable;      
14a70 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 63   /* True if we c
14a71 61 6e 20 73 68 61 72 65 20 70 42 74 20 77 69 74  an share pBt wit
14a72 68 20 61 6e 6f 74 68 65 72 20 64 62 20 2a 2f 0a  h another db */.
14a73 20 20 75 38 20 6c 6f 63 6b 65 64 3b 20 20 20 20    u8 locked;    
14a74 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
14a75 64 62 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73  db currently has
14a76 20 70 42 74 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20   pBt locked */. 
14a77 20 69 6e 74 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b   int wantToLock;
14a78 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
14a79 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 74 6f   nested calls to
14a7a 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
14a7b 65 72 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  er() */.  int nB
14a7c 61 63 6b 75 70 3b 20 20 20 20 20 20 20 2f 2a 20  ackup;       /* 
14a7d 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b 75 70  Number of backup
14a7e 20 6f 70 65 72 61 74 69 6f 6e 73 20 72 65 61 64   operations read
14a7f 69 6e 67 20 74 68 69 73 20 62 74 72 65 65 20 2a  ing this btree *
14a80 2f 0a 20 20 42 74 72 65 65 20 2a 70 4e 65 78 74  /.  Btree *pNext
14a81 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f  ;      /* List o
14a82 66 20 6f 74 68 65 72 20 73 68 61 72 61 62 6c 65  f other sharable
14a83 20 42 74 72 65 65 73 20 66 72 6f 6d 20 74 68 65   Btrees from the
14a84 20 73 61 6d 65 20 64 62 20 2a 2f 0a 20 20 42 74   same db */.  Bt
14a85 72 65 65 20 2a 70 50 72 65 76 3b 20 20 20 20 20  ree *pPrev;     
14a86 20 2f 2a 20 42 61 63 6b 20 70 6f 69 6e 74 65 72   /* Back pointer
14a87 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6c 69 73   of the same lis
14a88 74 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  t */.#ifndef SQL
14a89 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
14a8a 43 41 43 48 45 0a 20 20 42 74 4c 6f 63 6b 20 6c  CACHE.  BtLock l
14a8b 6f 63 6b 3b 20 20 20 20 20 20 20 2f 2a 20 4f 62  ock;       /* Ob
14a8c 6a 65 63 74 20 75 73 65 64 20 74 6f 20 6c 6f 63  ject used to loc
14a8d 6b 20 70 61 67 65 20 31 20 2a 2f 0a 23 65 6e 64  k page 1 */.#end
14a8e 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 42 74 72  if.};../*.** Btr
14a8f 65 65 2e 69 6e 54 72 61 6e 73 20 6d 61 79 20 74  ee.inTrans may t
14a90 61 6b 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 66  ake one of the f
14a91 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73 2e  ollowing values.
14a92 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 68  .**.** If the sh
14a93 61 72 65 64 2d 64 61 74 61 20 65 78 74 65 6e 73  ared-data extens
14a94 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  ion is enabled, 
14a95 74 68 65 72 65 20 6d 61 79 20 62 65 20 6d 75 6c  there may be mul
14a96 74 69 70 6c 65 20 75 73 65 72 73 0a 2a 2a 20 6f  tiple users.** o
14a97 66 20 74 68 65 20 42 74 72 65 65 20 73 74 72 75  f the Btree stru
14a98 63 74 75 72 65 2e 20 41 74 20 6d 6f 73 74 20 6f  cture. At most o
14a99 6e 65 20 6f 66 20 74 68 65 73 65 20 6d 61 79 20  ne of these may 
14a9a 6f 70 65 6e 20 61 20 77 72 69 74 65 20 74 72 61  open a write tra
14a9b 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74  nsaction,.** but
14a9c 20 61 6e 79 20 6e 75 6d 62 65 72 20 6d 61 79 20   any number may 
14a9d 68 61 76 65 20 61 63 74 69 76 65 20 72 65 61 64  have active read
14a9e 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 2a   transactions..*
14a9f 2f 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f  /.#define TRANS_
14aa0 4e 4f 4e 45 20 20 30 0a 23 64 65 66 69 6e 65 20  NONE  0.#define 
14aa1 54 52 41 4e 53 5f 52 45 41 44 20 20 31 0a 23 64  TRANS_READ  1.#d
14aa2 65 66 69 6e 65 20 54 52 41 4e 53 5f 57 52 49 54  efine TRANS_WRIT
14aa3 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  E 2../*.** An in
14aa4 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f  stance of this o
14aa5 62 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 73  bject represents
14aa6 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   a single databa
14aa7 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20  se file..** .** 
14aa8 41 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  A single databas
14aa9 65 20 66 69 6c 65 20 63 61 6e 20 62 65 20 69 6e  e file can be in
14aaa 20 75 73 65 20 61 73 20 74 68 65 20 73 61 6d 65   use as the same
14aab 20 74 69 6d 65 20 62 79 20 74 77 6f 0a 2a 2a 20   time by two.** 
14aac 6f 72 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65  or more database
14aad 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 57   connections.  W
14aae 68 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20  hen two or more 
14aaf 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 0a  connections are.
14ab0 2a 2a 20 73 68 61 72 69 6e 67 20 74 68 65 20 73  ** sharing the s
14ab1 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  ame database fil
14ab2 65 2c 20 65 61 63 68 20 63 6f 6e 6e 65 63 74 69  e, each connecti
14ab3 6f 6e 20 68 61 73 20 69 74 20 6f 77 6e 0a 2a 2a  on has it own.**
14ab4 20 70 72 69 76 61 74 65 20 42 74 72 65 65 20 6f   private Btree o
14ab5 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20 66 69  bject for the fi
14ab6 6c 65 20 61 6e 64 20 65 61 63 68 20 6f 66 20 74  le and each of t
14ab7 68 6f 73 65 20 42 74 72 65 65 73 20 70 6f 69 6e  hose Btrees poin
14ab8 74 73 0a 2a 2a 20 74 6f 20 74 68 69 73 20 6f 6e  ts.** to this on
14ab9 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63  e BtShared objec
14aba 74 2e 20 20 42 74 53 68 61 72 65 64 2e 6e 52 65  t.  BtShared.nRe
14abb 66 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  f is the number 
14abc 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  of.** connection
14abd 73 20 63 75 72 72 65 6e 74 6c 79 20 73 68 61 72  s currently shar
14abe 69 6e 67 20 74 68 69 73 20 64 61 74 61 62 61 73  ing this databas
14abf 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 46 69  e file..**.** Fi
14ac0 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72  elds in this str
14ac1 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73  ucture are acces
14ac2 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74  sed under the Bt
14ac3 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20  Shared.mutex.** 
14ac4 6d 75 74 65 78 2c 20 65 78 63 65 70 74 20 66 6f  mutex, except fo
14ac5 72 20 6e 52 65 66 20 61 6e 64 20 70 4e 65 78 74  r nRef and pNext
14ac6 20 77 68 69 63 68 20 61 72 65 20 61 63 63 65 73   which are acces
14ac7 73 65 64 20 75 6e 64 65 72 20 74 68 65 0a 2a 2a  sed under the.**
14ac8 20 67 6c 6f 62 61 6c 20 53 51 4c 49 54 45 5f 4d   global SQLITE_M
14ac9 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
14aca 45 52 20 6d 75 74 65 78 2e 20 20 54 68 65 20 70  ER mutex.  The p
14acb 50 61 67 65 72 20 66 69 65 6c 64 0a 2a 2a 20 6d  Pager field.** m
14acc 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69  ay not be modifi
14acd 65 64 20 6f 6e 63 65 20 69 74 20 69 73 20 69 6e  ed once it is in
14ace 69 74 69 61 6c 6c 79 20 73 65 74 20 61 73 20 6c  itially set as l
14acf 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a  ong as nRef>0..*
14ad0 2a 20 54 68 65 20 70 53 63 68 65 6d 61 20 66 69  * The pSchema fi
14ad1 65 6c 64 20 6d 61 79 20 62 65 20 73 65 74 20 6f  eld may be set o
14ad2 6e 63 65 20 75 6e 64 65 72 20 42 74 53 68 61 72  nce under BtShar
14ad3 65 64 2e 6d 75 74 65 78 20 61 6e 64 0a 2a 2a 20  ed.mutex and.** 
14ad4 74 68 65 72 65 61 66 74 65 72 20 69 73 20 75 6e  thereafter is un
14ad5 63 68 61 6e 67 65 64 20 61 73 20 6c 6f 6e 67 20  changed as long 
14ad6 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a 0a 2a 2a  as nRef>0..**.**
14ad7 20 69 73 50 65 6e 64 69 6e 67 3a 0a 2a 2a 0a 2a   isPending:.**.*
14ad8 2a 20 20 20 49 66 20 61 20 42 74 53 68 61 72 65  *   If a BtShare
14ad9 64 20 63 6c 69 65 6e 74 20 66 61 69 6c 73 20 74  d client fails t
14ada 6f 20 6f 62 74 61 69 6e 20 61 20 77 72 69 74 65  o obtain a write
14adb 2d 6c 6f 63 6b 20 6f 6e 20 61 20 64 61 74 61 62  -lock on a datab
14adc 61 73 65 0a 2a 2a 20 20 20 74 61 62 6c 65 20 28  ase.**   table (
14add 62 65 63 61 75 73 65 20 74 68 65 72 65 20 65 78  because there ex
14ade 69 73 74 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  ists one or more
14adf 20 72 65 61 64 2d 6c 6f 63 6b 73 20 6f 6e 20 74   read-locks on t
14ae0 68 65 20 74 61 62 6c 65 29 2c 0a 2a 2a 20 20 20  he table),.**   
14ae1 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68 65  the shared-cache
14ae2 20 65 6e 74 65 72 73 20 27 70 65 6e 64 69 6e 67   enters 'pending
14ae3 2d 6c 6f 63 6b 27 20 73 74 61 74 65 20 61 6e 64  -lock' state and
14ae4 20 69 73 50 65 6e 64 69 6e 67 20 69 73 0a 2a 2a   isPending is.**
14ae5 20 20 20 73 65 74 20 74 6f 20 74 72 75 65 2e 0a     set to true..
14ae6 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 73 68 61 72  **.**   The shar
14ae7 65 64 2d 63 61 63 68 65 20 6c 65 61 76 65 73 20  ed-cache leaves 
14ae8 74 68 65 20 27 70 65 6e 64 69 6e 67 20 6c 6f 63  the 'pending loc
14ae9 6b 27 20 73 74 61 74 65 20 77 68 65 6e 20 65 69  k' state when ei
14aea 74 68 65 72 20 6f 66 0a 2a 2a 20 20 20 74 68 65  ther of.**   the
14aeb 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 63 63 75 72   following occur
14aec 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 31 29 20 54  :.**.**     1) T
14aed 68 65 20 63 75 72 72 65 6e 74 20 77 72 69 74 65  he current write
14aee 72 20 28 42 74 53 68 61 72 65 64 2e 70 57 72 69  r (BtShared.pWri
14aef 74 65 72 29 20 63 6f 6e 63 6c 75 64 65 73 20 69  ter) concludes i
14af0 74 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  ts transaction, 
14af1 4f 52 0a 2a 2a 20 20 20 20 20 32 29 20 54 68 65  OR.**     2) The
14af2 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73   number of locks
14af3 20 68 65 6c 64 20 62 79 20 6f 74 68 65 72 20 63   held by other c
14af4 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 72 6f 70 73  onnections drops
14af5 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20   to zero..**.** 
14af6 20 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 27    while in the '
14af7 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27 20 73 74  pending-lock' st
14af8 61 74 65 2c 20 6e 6f 20 63 6f 6e 6e 65 63 74 69  ate, no connecti
14af9 6f 6e 20 6d 61 79 20 73 74 61 72 74 20 61 20 6e  on may start a n
14afa 65 77 0a 2a 2a 20 20 20 74 72 61 6e 73 61 63 74  ew.**   transact
14afb 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69  ion..**.**   Thi
14afc 73 20 66 65 61 74 75 72 65 20 69 73 20 69 6e 63  s feature is inc
14afd 6c 75 64 65 64 20 74 6f 20 68 65 6c 70 20 70 72  luded to help pr
14afe 65 76 65 6e 74 20 77 72 69 74 65 72 2d 73 74 61  event writer-sta
14aff 72 76 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75  rvation..*/.stru
14b00 63 74 20 42 74 53 68 61 72 65 64 20 7b 0a 20 20  ct BtShared {.  
14b01 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20  Pager *pPager;  
14b02 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
14b03 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 73 71 6c  e cache */.  sql
14b04 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
14b05 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
14b06 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65 6e  onnection curren
14b07 74 6c 79 20 75 73 69 6e 67 20 74 68 69 73 20 42  tly using this B
14b08 74 72 65 65 20 2a 2f 0a 20 20 42 74 43 75 72 73  tree */.  BtCurs
14b09 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20  or *pCursor;    
14b0a 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 61 6c 6c  /* A list of all
14b0b 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 2a 2f   open cursors */
14b0c 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
14b0d 65 31 3b 20 20 20 20 20 20 2f 2a 20 46 69 72 73  e1;      /* Firs
14b0e 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 64 61  t page of the da
14b0f 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 72  tabase */.  u8 r
14b10 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20  eadOnly;        
14b11 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
14b12 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c 65   underlying file
14b13 20 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f 0a   is readonly */.
14b14 20 20 75 38 20 70 61 67 65 53 69 7a 65 46 69 78    u8 pageSizeFix
14b15 65 64 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ed;     /* True 
14b16 69 66 20 74 68 65 20 70 61 67 65 20 73 69 7a 65  if the page size
14b17 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 62   can no longer b
14b18 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 69 66  e changed */.#if
14b19 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
14b1a 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 75 38  _AUTOVACUUM.  u8
14b1b 20 61 75 74 6f 56 61 63 75 75 6d 3b 20 20 20 20   autoVacuum;    
14b1c 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
14b1d 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 65 6e  uto-vacuum is en
14b1e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 38 20 69 6e  abled */.  u8 in
14b1f 63 72 56 61 63 75 75 6d 3b 20 20 20 20 20 20 20  crVacuum;       
14b20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 63 72   /* True if incr
14b21 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c  -vacuum is enabl
14b22 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75  ed */.#endif.  u
14b23 31 36 20 70 61 67 65 53 69 7a 65 3b 20 20 20 20  16 pageSize;    
14b24 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
14b25 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 6e  mber of bytes on
14b26 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36   a page */.  u16
14b27 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20   usableSize;    
14b28 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
14b29 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f 6e 20  usable bytes on 
14b2a 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 75  each page */.  u
14b2b 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20  16 maxLocal;    
14b2c 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
14b2d 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e  local payload in
14b2e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61   non-LEAFDATA ta
14b2f 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 69  bles */.  u16 mi
14b30 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20 20 20  nLocal;         
14b31 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63 61 6c  /* Minimum local
14b32 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f 6e 2d   payload in non-
14b33 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 73 20  LEAFDATA tables 
14b34 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c 65 61 66  */.  u16 maxLeaf
14b35 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  ;          /* Ma
14b36 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c  ximum local payl
14b37 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44 41 54  oad in a LEAFDAT
14b38 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 75 31 36  A table */.  u16
14b39 20 6d 69 6e 4c 65 61 66 3b 20 20 20 20 20 20 20   minLeaf;       
14b3a 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f     /* Minimum lo
14b3b 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 61  cal payload in a
14b3c 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65 20   LEAFDATA table 
14b3d 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e 73 61  */.  u8 inTransa
14b3e 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 54 72  ction;     /* Tr
14b3f 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20  ansaction state 
14b40 2a 2f 0a 20 20 69 6e 74 20 6e 54 72 61 6e 73 61  */.  int nTransa
14b41 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20 4e 75  ction;     /* Nu
14b42 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 74 72 61  mber of open tra
14b43 6e 73 61 63 74 69 6f 6e 73 20 28 72 65 61 64 20  nsactions (read 
14b44 2b 20 77 72 69 74 65 29 20 2a 2f 0a 20 20 76 6f  + write) */.  vo
14b45 69 64 20 2a 70 53 63 68 65 6d 61 3b 20 20 20 20  id *pSchema;    
14b46 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
14b47 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  o space allocate
14b48 64 20 62 79 20 73 71 6c 69 74 65 33 42 74 72 65  d by sqlite3Btre
14b49 65 53 63 68 65 6d 61 28 29 20 2a 2f 0a 20 20 76  eSchema() */.  v
14b4a 6f 69 64 20 28 2a 78 46 72 65 65 53 63 68 65 6d  oid (*xFreeSchem
14b4b 61 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a 20 44  a)(void*);  /* D
14b4c 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 42 74  estructor for Bt
14b4d 53 68 61 72 65 64 2e 70 53 63 68 65 6d 61 20 2a  Shared.pSchema *
14b4e 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  /.  sqlite3_mute
14b4f 78 20 2a 6d 75 74 65 78 3b 20 2f 2a 20 4e 6f 6e  x *mutex; /* Non
14b50 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78  -recursive mutex
14b51 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 63 63   required to acc
14b52 65 73 73 20 74 68 69 73 20 73 74 72 75 63 74 20  ess this struct 
14b53 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 48 61  */.  Bitvec *pHa
14b54 73 43 6f 6e 74 65 6e 74 3b 20 20 2f 2a 20 53 65  sContent;  /* Se
14b55 74 20 6f 66 20 70 61 67 65 73 20 6d 6f 76 65 64  t of pages moved
14b56 20 74 6f 20 66 72 65 65 2d 6c 69 73 74 20 74 68   to free-list th
14b57 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  is transaction *
14b58 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
14b59 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
14b5a 48 45 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20  HE.  int nRef;  
14b5b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
14b5c 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63  mber of referenc
14b5d 65 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63  es to this struc
14b5e 74 75 72 65 20 2a 2f 0a 20 20 42 74 53 68 61 72  ture */.  BtShar
14b5f 65 64 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  ed *pNext;      
14b60 2f 2a 20 4e 65 78 74 20 6f 6e 20 61 20 6c 69 73  /* Next on a lis
14b61 74 20 6f 66 20 73 68 61 72 61 62 6c 65 20 42 74  t of sharable Bt
14b62 53 68 61 72 65 64 20 73 74 72 75 63 74 73 20 2a  Shared structs *
14b63 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63  /.  BtLock *pLoc
14b64 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73  k;        /* Lis
14b65 74 20 6f 66 20 6c 6f 63 6b 73 20 68 65 6c 64 20  t of locks held 
14b66 6f 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d 62  on this shared-b
14b67 74 72 65 65 20 73 74 72 75 63 74 20 2a 2f 0a 20  tree struct */. 
14b68 20 42 74 72 65 65 20 2a 70 57 72 69 74 65 72 3b   Btree *pWriter;
14b69 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20         /* Btree 
14b6a 77 69 74 68 20 63 75 72 72 65 6e 74 6c 79 20 6f  with currently o
14b6b 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61  pen write transa
14b6c 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 69 73  ction */.  u8 is
14b6d 45 78 63 6c 75 73 69 76 65 3b 20 20 20 20 20 20  Exclusive;      
14b6e 20 2f 2a 20 54 72 75 65 20 69 66 20 70 57 72 69   /* True if pWri
14b6f 74 65 72 20 68 61 73 20 61 6e 20 45 58 43 4c 55  ter has an EXCLU
14b70 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
14b71 20 64 62 20 2a 2f 0a 20 20 75 38 20 69 73 50 65   db */.  u8 isPe
14b72 6e 64 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f  nding;         /
14b73 2a 20 49 66 20 77 61 69 74 69 6e 67 20 66 6f 72  * If waiting for
14b74 20 72 65 61 64 2d 6c 6f 63 6b 73 20 74 6f 20 63   read-locks to c
14b75 6c 65 61 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  lear */.#endif. 
14b76 20 75 38 20 2a 70 54 6d 70 53 70 61 63 65 3b 20   u8 *pTmpSpace; 
14b77 20 20 20 20 20 20 20 2f 2a 20 42 74 53 68 61 72         /* BtShar
14b78 65 64 2e 70 61 67 65 53 69 7a 65 20 62 79 74 65  ed.pageSize byte
14b79 73 20 6f 66 20 73 70 61 63 65 20 66 6f 72 20 74  s of space for t
14b7a 6d 70 20 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  mp use */.};../*
14b7b 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
14b7c 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
14b7d 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
14b7e 65 64 20 74 6f 20 68 6f 6c 64 20 69 6e 66 6f 72  ed to hold infor
14b7f 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20  mation.** about 
14b80 61 20 63 65 6c 6c 2e 20 20 54 68 65 20 70 61 72  a cell.  The par
14b81 73 65 43 65 6c 6c 50 74 72 28 29 20 66 75 6e 63  seCellPtr() func
14b82 74 69 6f 6e 20 66 69 6c 6c 73 20 69 6e 20 74 68  tion fills in th
14b83 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  is structure.** 
14b84 62 61 73 65 64 20 6f 6e 20 69 6e 66 6f 72 6d 61  based on informa
14b85 74 69 6f 6e 20 65 78 74 72 61 63 74 20 66 72 6f  tion extract fro
14b86 6d 20 74 68 65 20 72 61 77 20 64 69 73 6b 20 70  m the raw disk p
14b87 61 67 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  age..*/.typedef 
14b88 73 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20  struct CellInfo 
14b89 43 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72 75 63 74  CellInfo;.struct
14b8a 20 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20 20 75 38   CellInfo {.  u8
14b8b 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 2f 2a 20   *pCell;     /* 
14b8c 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73  Pointer to the s
14b8d 74 61 72 74 20 6f 66 20 63 65 6c 6c 20 63 6f 6e  tart of cell con
14b8e 74 65 6e 74 20 2a 2f 0a 20 20 69 36 34 20 6e 4b  tent */.  i64 nK
14b8f 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ey;      /* The 
14b90 6b 65 79 20 66 6f 72 20 49 4e 54 4b 45 59 20 74  key for INTKEY t
14b91 61 62 6c 65 73 2c 20 6f 72 20 6e 75 6d 62 65 72  ables, or number
14b92 20 6f 66 20 62 79 74 65 73 20 69 6e 20 6b 65 79   of bytes in key
14b93 20 2a 2f 0a 20 20 75 33 32 20 6e 44 61 74 61 3b   */.  u32 nData;
14b94 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
14b95 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  f bytes of data 
14b96 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79 6c 6f 61  */.  u32 nPayloa
14b97 64 3b 20 20 2f 2a 20 54 6f 74 61 6c 20 61 6d 6f  d;  /* Total amo
14b98 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 2a  unt of payload *
14b99 2f 0a 20 20 75 31 36 20 6e 48 65 61 64 65 72 3b  /.  u16 nHeader;
14b9a 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
14b9b 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68  e cell content h
14b9c 65 61 64 65 72 20 69 6e 20 62 79 74 65 73 20 2a  eader in bytes *
14b9d 2f 0a 20 20 75 31 36 20 6e 4c 6f 63 61 6c 3b 20  /.  u16 nLocal; 
14b9e 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20     /* Amount of 
14b9f 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c 6f 63  payload held loc
14ba0 61 6c 6c 79 20 2a 2f 0a 20 20 75 31 36 20 69 4f  ally */.  u16 iO
14ba1 76 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f 66 66 73  verflow; /* Offs
14ba2 65 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70  et to overflow p
14ba3 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 5a 65 72  age number.  Zer
14ba4 6f 20 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77  o if no overflow
14ba5 20 2a 2f 0a 20 20 75 31 36 20 6e 53 69 7a 65 3b   */.  u16 nSize;
14ba6 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
14ba7 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74  the cell content
14ba8 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62 2d 74   on the main b-t
14ba9 72 65 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a  ree page */.};..
14baa 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 64 65  /*.** Maximum de
14bab 70 74 68 20 6f 66 20 61 6e 20 53 51 4c 69 74 65  pth of an SQLite
14bac 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72   B-Tree structur
14bad 65 2e 20 41 6e 79 20 42 2d 54 72 65 65 20 64 65  e. Any B-Tree de
14bae 65 70 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 69  eper than.** thi
14baf 73 20 77 69 6c 6c 20 62 65 20 64 65 63 6c 61 72  s will be declar
14bb0 65 64 20 63 6f 72 72 75 70 74 2e 20 54 68 69 73  ed corrupt. This
14bb1 20 76 61 6c 75 65 20 69 73 20 63 61 6c 63 75 6c   value is calcul
14bb2 61 74 65 64 20 62 61 73 65 64 20 6f 6e 20 61 0a  ated based on a.
14bb3 2a 2a 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62  ** maximum datab
14bb4 61 73 65 20 73 69 7a 65 20 6f 66 20 32 5e 33 31  ase size of 2^31
14bb5 20 70 61 67 65 73 20 61 20 6d 69 6e 69 6d 75 6d   pages a minimum
14bb6 20 66 61 6e 6f 75 74 20 6f 66 20 32 20 66 6f 72   fanout of 2 for
14bb7 20 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f 64 65 20   a.** root-node 
14bb8 61 6e 64 20 33 20 66 6f 72 20 61 6c 6c 20 6f 74  and 3 for all ot
14bb9 68 65 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64  her internal nod
14bba 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74  es..**.** If a t
14bbb 72 65 65 20 74 68 61 74 20 61 70 70 65 61 72 73  ree that appears
14bbc 20 74 6f 20 62 65 20 74 61 6c 6c 65 72 20 74 68   to be taller th
14bbd 61 6e 20 74 68 69 73 20 69 73 20 65 6e 63 6f 75  an this is encou
14bbe 6e 74 65 72 65 64 2c 20 69 74 20 69 73 0a 2a 2a  ntered, it is.**
14bbf 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
14bc0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f  e database is co
14bc1 72 72 75 70 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  rrupt..*/.#defin
14bc2 65 20 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44  e BTCURSOR_MAX_D
14bc3 45 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a 2a 20 41  EPTH 20../*.** A
14bc4 20 63 75 72 73 6f 72 20 69 73 20 61 20 70 6f 69   cursor is a poi
14bc5 6e 74 65 72 20 74 6f 20 61 20 70 61 72 74 69 63  nter to a partic
14bc6 75 6c 61 72 20 65 6e 74 72 79 20 77 69 74 68 69  ular entry withi
14bc7 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a  n a particular.*
14bc8 2a 20 62 2d 74 72 65 65 20 77 69 74 68 69 6e 20  * b-tree within 
14bc9 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  a database file.
14bca 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e 74 72 79  .**.** The entry
14bcb 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62   is identified b
14bcc 79 20 69 74 73 20 4d 65 6d 50 61 67 65 20 61 6e  y its MemPage an
14bcd 64 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a 2a  d the index in.*
14bce 2a 20 4d 65 6d 50 61 67 65 2e 61 43 65 6c 6c 5b  * MemPage.aCell[
14bcf 5d 20 6f 66 20 74 68 65 20 65 6e 74 72 79 2e 0a  ] of the entry..
14bd0 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64  **.** A single d
14bd1 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e  atabase file can
14bd2 20 73 68 61 72 65 64 20 62 79 20 74 77 6f 20 6d   shared by two m
14bd3 6f 72 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ore database con
14bd4 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20 62 75 74  nections,.** but
14bd5 20 63 75 72 73 6f 72 73 20 63 61 6e 6e 6f 74 20   cursors cannot 
14bd6 62 65 20 73 68 61 72 65 64 2e 20 20 45 61 63 68  be shared.  Each
14bd7 20 63 75 72 73 6f 72 20 69 73 20 61 73 73 6f 63   cursor is assoc
14bd8 69 61 74 65 64 20 77 69 74 68 20 61 0a 2a 2a 20  iated with a.** 
14bd9 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62  particular datab
14bda 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
14bdb 64 65 6e 74 69 66 69 65 64 20 42 74 43 75 72 73  dentified BtCurs
14bdc 6f 72 2e 70 42 74 72 65 65 2e 64 62 2e 0a 2a 2a  or.pBtree.db..**
14bdd 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68  .** Fields in th
14bde 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65  is structure are
14bdf 20 61 63 63 65 73 73 65 64 20 75 6e 64 65 72 20   accessed under 
14be0 74 68 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74  the BtShared.mut
14be1 65 78 0a 2a 2a 20 66 6f 75 6e 64 20 61 74 20 73  ex.** found at s
14be2 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 2e  elf->pBt->mutex.
14be3 20 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 43 75   .*/.struct BtCu
14be4 72 73 6f 72 20 7b 0a 20 20 42 74 72 65 65 20 2a  rsor {.  Btree *
14be5 70 42 74 72 65 65 3b 20 20 20 20 20 20 20 20 20  pBtree;         
14be6 20 20 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20     /* The Btree 
14be7 74 6f 20 77 68 69 63 68 20 74 68 69 73 20 63 75  to which this cu
14be8 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 2a 2f 0a  rsor belongs */.
14be9 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b    BtShared *pBt;
14bea 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
14beb 68 65 20 42 74 53 68 61 72 65 64 20 74 68 69 73  he BtShared this
14bec 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74   cursor points t
14bed 6f 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20  o */.  BtCursor 
14bee 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20  *pNext, *pPrev; 
14bef 20 2f 2a 20 46 6f 72 6d 73 20 61 20 6c 69 6e 6b   /* Forms a link
14bf0 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63  ed list of all c
14bf1 75 72 73 6f 72 73 20 2a 2f 0a 20 20 73 74 72 75  ursors */.  stru
14bf2 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  ct KeyInfo *pKey
14bf3 49 6e 66 6f 3b 20 2f 2a 20 41 72 67 75 6d 65 6e  Info; /* Argumen
14bf4 74 20 70 61 73 73 65 64 20 74 6f 20 63 6f 6d 70  t passed to comp
14bf5 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  arison function 
14bf6 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52 6f  */.  Pgno pgnoRo
14bf7 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ot;            /
14bf8 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20  * The root page 
14bf9 6f 66 20 74 68 69 73 20 74 72 65 65 20 2a 2f 0a  of this tree */.
14bfa 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
14bfb 63 61 63 68 65 64 52 6f 77 69 64 3b 20 2f 2a 20  cachedRowid; /* 
14bfc 4e 65 78 74 20 72 6f 77 69 64 20 63 61 63 68 65  Next rowid cache
14bfd 2e 20 20 30 20 6d 65 61 6e 73 20 6e 6f 74 20 76  .  0 means not v
14bfe 61 6c 69 64 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e  alid */.  CellIn
14bff 66 6f 20 69 6e 66 6f 3b 20 20 20 20 20 20 20 20  fo info;        
14c00 20 20 20 20 2f 2a 20 41 20 70 61 72 73 65 20 6f      /* A parse o
14c01 66 20 74 68 65 20 63 65 6c 6c 20 77 65 20 61 72  f the cell we ar
14c02 65 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f  e pointing at */
14c03 0a 20 20 75 38 20 77 72 46 6c 61 67 3b 20 20 20  .  u8 wrFlag;   
14c04 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14c05 54 72 75 65 20 69 66 20 77 72 69 74 61 62 6c 65  True if writable
14c06 20 2a 2f 0a 20 20 75 38 20 61 74 4c 61 73 74 3b   */.  u8 atLast;
14c07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14c08 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69  /* Cursor pointi
14c09 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65  ng to the last e
14c0a 6e 74 72 79 20 2a 2f 0a 20 20 75 38 20 76 61 6c  ntry */.  u8 val
14c0b 69 64 4e 4b 65 79 3b 20 20 20 20 20 20 20 20 20  idNKey;         
14c0c 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
14c0d 6e 66 6f 2e 6e 4b 65 79 20 69 73 20 76 61 6c 69  nfo.nKey is vali
14c0e 64 20 2a 2f 0a 20 20 75 38 20 65 53 74 61 74 65  d */.  u8 eState
14c0f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
14c10 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43   /* One of the C
14c11 55 52 53 4f 52 5f 58 58 58 20 63 6f 6e 73 74 61  URSOR_XXX consta
14c12 6e 74 73 20 28 73 65 65 20 62 65 6c 6f 77 29 20  nts (see below) 
14c13 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b  */.  void *pKey;
14c14 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20 6b        /* Saved k
14c15 65 79 20 74 68 61 74 20 77 61 73 20 63 75 72 73  ey that was curs
14c16 6f 72 27 73 20 6c 61 73 74 20 6b 6e 6f 77 6e 20  or's last known 
14c17 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20 20 69 36  position */.  i6
14c18 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 20 20 2f  4 nKey;        /
14c19 2a 20 53 69 7a 65 20 6f 66 20 70 4b 65 79 2c 20  * Size of pKey, 
14c1a 6f 72 20 6c 61 73 74 20 69 6e 74 65 67 65 72 20  or last integer 
14c1b 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 73 6b 69  key */.  int ski
14c1c 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 50 72 65  pNext;    /* Pre
14c1d 76 28 29 20 69 73 20 6e 6f 6f 70 20 69 66 20 6e  v() is noop if n
14c1e 65 67 61 74 69 76 65 2e 20 4e 65 78 74 28 29 20  egative. Next() 
14c1f 69 73 20 6e 6f 6f 70 20 69 66 20 70 6f 73 69 74  is noop if posit
14c20 69 76 65 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ive */.#ifndef S
14c21 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
14c22 4c 4f 42 0a 20 20 75 38 20 69 73 49 6e 63 72 62  LOB.  u8 isIncrb
14c23 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20 20 20 20  lobHandle;      
14c24 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
14c25 63 75 72 73 6f 72 20 69 73 20 61 6e 20 69 6e 63  cursor is an inc
14c26 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20 2a 2f 0a  r. io handle */.
14c27 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 66 6c 6f    Pgno *aOverflo
14c28 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  w;          /* C
14c29 61 63 68 65 20 6f 66 20 6f 76 65 72 66 6c 6f 77  ache of overflow
14c2a 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 73 20   page locations 
14c2b 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 31 36 20  */.#endif.  i16 
14c2c 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  iPage;          
14c2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14c2e 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75    /* Index of cu
14c2f 72 72 65 6e 74 20 70 61 67 65 20 69 6e 20 61 70  rrent page in ap
14c30 50 61 67 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  Page */.  MemPag
14c31 65 20 2a 61 70 50 61 67 65 5b 42 54 43 55 52 53  e *apPage[BTCURS
14c32 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20  OR_MAX_DEPTH];  
14c33 2f 2a 20 50 61 67 65 73 20 66 72 6f 6d 20 72 6f  /* Pages from ro
14c34 6f 74 20 74 6f 20 63 75 72 72 65 6e 74 20 70 61  ot to current pa
14c35 67 65 20 2a 2f 0a 20 20 75 31 36 20 61 69 49 64  ge */.  u16 aiId
14c36 78 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44  x[BTCURSOR_MAX_D
14c37 45 50 54 48 5d 3b 20 20 20 20 20 20 20 20 2f 2a  EPTH];        /*
14c38 20 43 75 72 72 65 6e 74 20 69 6e 64 65 78 20 69   Current index i
14c39 6e 20 61 70 50 61 67 65 5b 69 5d 20 2a 2f 0a 7d  n apPage[i] */.}
14c3a 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 74 65 6e 74 69  ;../*.** Potenti
14c3b 61 6c 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74  al values for Bt
14c3c 43 75 72 73 6f 72 2e 65 53 74 61 74 65 2e 0a 2a  Cursor.eState..*
14c3d 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 56 41 4c 49  *.** CURSOR_VALI
14c3e 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 70  D:.**   Cursor p
14c3f 6f 69 6e 74 73 20 74 6f 20 61 20 76 61 6c 69 64  oints to a valid
14c40 20 65 6e 74 72 79 2e 20 67 65 74 50 61 79 6c 6f   entry. getPaylo
14c41 61 64 28 29 20 65 74 63 2e 20 6d 61 79 20 62 65  ad() etc. may be
14c42 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43   called..**.** C
14c43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3a 0a 2a  URSOR_INVALID:.*
14c44 2a 20 20 20 43 75 72 73 6f 72 20 64 6f 65 73 20  *   Cursor does 
14c45 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 76  not point to a v
14c46 61 6c 69 64 20 65 6e 74 72 79 2e 20 54 68 69 73  alid entry. This
14c47 20 63 61 6e 20 68 61 70 70 65 6e 20 28 66 6f 72   can happen (for
14c48 20 65 78 61 6d 70 6c 65 29 20 0a 2a 2a 20 20 20   example) .**   
14c49 62 65 63 61 75 73 65 20 74 68 65 20 74 61 62 6c  because the tabl
14c4a 65 20 69 73 20 65 6d 70 74 79 20 6f 72 20 62 65  e is empty or be
14c4b 63 61 75 73 65 20 42 74 72 65 65 43 75 72 73 6f  cause BtreeCurso
14c4c 72 46 69 72 73 74 28 29 20 68 61 73 20 6e 6f 74  rFirst() has not
14c4d 20 62 65 65 6e 0a 2a 2a 20 20 20 63 61 6c 6c 65   been.**   calle
14c4e 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f  d..**.** CURSOR_
14c4f 52 45 51 55 49 52 45 53 45 45 4b 3a 0a 2a 2a 20  REQUIRESEEK:.** 
14c50 20 20 54 68 65 20 74 61 62 6c 65 20 74 68 61 74    The table that
14c51 20 74 68 69 73 20 63 75 72 73 6f 72 20 77 61 73   this cursor was
14c52 20 6f 70 65 6e 65 64 20 6f 6e 20 73 74 69 6c 6c   opened on still
14c53 20 65 78 69 73 74 73 2c 20 62 75 74 20 68 61 73   exists, but has
14c54 20 62 65 65 6e 20 0a 2a 2a 20 20 20 6d 6f 64 69   been .**   modi
14c55 66 69 65 64 20 73 69 6e 63 65 20 74 68 65 20 63  fied since the c
14c56 75 72 73 6f 72 20 77 61 73 20 6c 61 73 74 20 75  ursor was last u
14c57 73 65 64 2e 20 54 68 65 20 63 75 72 73 6f 72 20  sed. The cursor 
14c58 70 6f 73 69 74 69 6f 6e 20 69 73 20 73 61 76 65  position is save
14c59 64 0a 2a 2a 20 20 20 69 6e 20 76 61 72 69 61 62  d.**   in variab
14c5a 6c 65 73 20 42 74 43 75 72 73 6f 72 2e 70 4b 65  les BtCursor.pKe
14c5b 79 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 6e  y and BtCursor.n
14c5c 4b 65 79 2e 20 57 68 65 6e 20 61 20 63 75 72 73  Key. When a curs
14c5d 6f 72 20 69 73 20 69 6e 20 0a 2a 2a 20 20 20 74  or is in .**   t
14c5e 68 69 73 20 73 74 61 74 65 2c 20 72 65 73 74 6f  his state, resto
14c5f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
14c60 28 29 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64  () can be called
14c61 20 74 6f 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a   to attempt to.*
14c62 2a 20 20 20 73 65 65 6b 20 74 68 65 20 63 75 72  *   seek the cur
14c63 73 6f 72 20 74 6f 20 74 68 65 20 73 61 76 65 64  sor to the saved
14c64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   position..**.**
14c65 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3a 0a 2a   CURSOR_FAULT:.*
14c66 2a 20 20 20 41 20 75 6e 72 65 63 6f 76 65 72 61  *   A unrecovera
14c67 62 6c 65 20 65 72 72 6f 72 20 28 61 6e 20 49 2f  ble error (an I/
14c68 4f 20 65 72 72 6f 72 20 6f 72 20 61 20 6d 61 6c  O error or a mal
14c69 6c 6f 63 20 66 61 69 6c 75 72 65 29 20 68 61 73  loc failure) has
14c6a 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 20 20 6f   occurred.**   o
14c6b 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 63 6f  n a different co
14c6c 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 73 68  nnection that sh
14c6d 61 72 65 73 20 74 68 65 20 42 74 53 68 61 72 65  ares the BtShare
14c6e 64 20 63 61 63 68 65 20 77 69 74 68 20 74 68 69  d cache with thi
14c6f 73 0a 2a 2a 20 20 20 63 75 72 73 6f 72 2e 20 20  s.**   cursor.  
14c70 54 68 65 20 65 72 72 6f 72 20 68 61 73 20 6c 65  The error has le
14c71 66 74 20 74 68 65 20 63 61 63 68 65 20 69 6e 20  ft the cache in 
14c72 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20  an inconsistent 
14c73 73 74 61 74 65 2e 0a 2a 2a 20 20 20 44 6f 20 6e  state..**   Do n
14c74 6f 74 68 69 6e 67 20 65 6c 73 65 20 77 69 74 68  othing else with
14c75 20 74 68 69 73 20 63 75 72 73 6f 72 2e 20 20 41   this cursor.  A
14c76 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ny attempt to us
14c77 65 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  e the cursor.** 
14c78 20 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20    should return 
14c79 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73  the error code s
14c7a 74 6f 72 65 64 20 69 6e 20 42 74 43 75 72 73 6f  tored in BtCurso
14c7b 72 2e 73 6b 69 70 0a 2a 2f 0a 23 64 65 66 69 6e  r.skip.*/.#defin
14c7c 65 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  e CURSOR_INVALID
14c7d 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 64 65             0.#de
14c7e 66 69 6e 65 20 43 55 52 53 4f 52 5f 56 41 4c 49  fine CURSOR_VALI
14c7f 44 20 20 20 20 20 20 20 20 20 20 20 20 20 31 0a  D             1.
14c80 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 52  #define CURSOR_R
14c81 45 51 55 49 52 45 53 45 45 4b 20 20 20 20 20 20  EQUIRESEEK      
14c82 20 32 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f   2.#define CURSO
14c83 52 5f 46 41 55 4c 54 20 20 20 20 20 20 20 20 20  R_FAULT         
14c84 20 20 20 20 33 0a 0a 2f 2a 20 0a 2a 2a 20 54 68      3../* .** Th
14c85 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  e database page 
14c86 74 68 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  the PENDING_BYTE
14c87 20 6f 63 63 75 70 69 65 73 2e 20 54 68 69 73 20   occupies. This 
14c88 70 61 67 65 20 69 73 20 6e 65 76 65 72 20 75 73  page is never us
14c89 65 64 2e 0a 2a 2f 0a 23 20 64 65 66 69 6e 65 20  ed..*/.# define 
14c8a 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
14c8b 45 28 70 42 74 29 20 50 41 47 45 52 5f 4d 4a 5f  E(pBt) PAGER_MJ_
14c8c 50 47 4e 4f 28 70 42 74 29 0a 0a 2f 2a 0a 2a 2a  PGNO(pBt)../*.**
14c8d 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 64 65   These macros de
14c8e 66 69 6e 65 20 74 68 65 20 6c 6f 63 61 74 69 6f  fine the locatio
14c8f 6e 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72  n of the pointer
14c90 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 61  -map entry for a
14c91 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61   .** database pa
14c92 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 61 72  ge. The first ar
14c93 67 75 6d 65 6e 74 20 74 6f 20 65 61 63 68 20 69  gument to each i
14c94 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
14c95 75 73 61 62 6c 65 0a 2a 2a 20 62 79 74 65 73 20  usable.** bytes 
14c96 6f 6e 20 65 61 63 68 20 70 61 67 65 20 6f 66 20  on each page of 
14c97 74 68 65 20 64 61 74 61 62 61 73 65 20 28 6f 66  the database (of
14c98 74 65 6e 20 31 30 32 34 29 2e 20 54 68 65 20 73  ten 1024). The s
14c99 65 63 6f 6e 64 20 69 73 20 74 68 65 0a 2a 2a 20  econd is the.** 
14c9a 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 6c  page number to l
14c9b 6f 6f 6b 20 75 70 20 69 6e 20 74 68 65 20 70 6f  ook up in the po
14c9c 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a  inter map..**.**
14c9d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 20 72   PTRMAP_PAGENO r
14c9e 65 74 75 72 6e 73 20 74 68 65 20 64 61 74 61 62  eturns the datab
14c9f 61 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ase page number 
14ca0 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  of the pointer-m
14ca1 61 70 0a 2a 2a 20 70 61 67 65 20 74 68 61 74 20  ap.** page that 
14ca2 73 74 6f 72 65 73 20 74 68 65 20 72 65 71 75 69  stores the requi
14ca3 72 65 64 20 70 6f 69 6e 74 65 72 2e 20 50 54 52  red pointer. PTR
14ca4 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 20 72 65  MAP_PTROFFSET re
14ca5 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6f 66 66  turns.** the off
14ca6 73 65 74 20 6f 66 20 74 68 65 20 72 65 71 75 65  set of the reque
14ca7 73 74 65 64 20 6d 61 70 20 65 6e 74 72 79 2e 0a  sted map entry..
14ca8 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 67 6e  **.** If the pgn
14ca9 6f 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65  o argument passe
14caa 64 20 74 6f 20 50 54 52 4d 41 50 5f 50 41 47 45  d to PTRMAP_PAGE
14cab 4e 4f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 2d  NO is a pointer-
14cac 6d 61 70 20 70 61 67 65 2c 0a 2a 2a 20 74 68 65  map page,.** the
14cad 6e 20 70 67 6e 6f 20 69 73 20 72 65 74 75 72 6e  n pgno is return
14cae 65 64 2e 20 53 6f 20 28 70 67 6e 6f 3d 3d 50 54  ed. So (pgno==PT
14caf 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 67 73 7a  RMAP_PAGENO(pgsz
14cb0 2c 20 70 67 6e 6f 29 29 20 63 61 6e 20 62 65 0a  , pgno)) can be.
14cb1 2a 2a 20 75 73 65 64 20 74 6f 20 74 65 73 74 20  ** used to test 
14cb2 69 66 20 70 67 6e 6f 20 69 73 20 61 20 70 6f 69  if pgno is a poi
14cb3 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2e 20 50  nter-map page. P
14cb4 54 52 4d 41 50 5f 49 53 50 41 47 45 20 69 6d 70  TRMAP_ISPAGE imp
14cb5 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 69 73 20  lements.** this 
14cb6 74 65 73 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  test..*/.#define
14cb7 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70   PTRMAP_PAGENO(p
14cb8 42 74 2c 20 70 67 6e 6f 29 20 70 74 72 6d 61 70  Bt, pgno) ptrmap
14cb9 50 61 67 65 6e 6f 28 70 42 74 2c 20 70 67 6e 6f  Pageno(pBt, pgno
14cba 29 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50  ).#define PTRMAP
14cbb 5f 50 54 52 4f 46 46 53 45 54 28 70 67 70 74 72  _PTROFFSET(pgptr
14cbc 6d 61 70 2c 20 70 67 6e 6f 29 20 28 35 2a 28 70  map, pgno) (5*(p
14cbd 67 6e 6f 2d 70 67 70 74 72 6d 61 70 2d 31 29 29  gno-pgptrmap-1))
14cbe 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f  .#define PTRMAP_
14cbf 49 53 50 41 47 45 28 70 42 74 2c 20 70 67 6e 6f  ISPAGE(pBt, pgno
14cc0 29 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f  ) (PTRMAP_PAGENO
14cc1 28 28 70 42 74 29 2c 28 70 67 6e 6f 29 29 3d 3d  ((pBt),(pgno))==
14cc2 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a 2a 20 54  (pgno))../*.** T
14cc3 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69  he pointer map i
14cc4 73 20 61 20 6c 6f 6f 6b 75 70 20 74 61 62 6c 65  s a lookup table
14cc5 20 74 68 61 74 20 69 64 65 6e 74 69 66 69 65 73   that identifies
14cc6 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
14cc7 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 63 68 69   for.** each chi
14cc8 6c 64 20 70 61 67 65 20 69 6e 20 74 68 65 20 64  ld page in the d
14cc9 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
14cca 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 69  he parent page i
14ccb 73 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a  s the page that.
14ccc 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f  ** contains a po
14ccd 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 68 69  inter to the chi
14cce 6c 64 2e 20 20 45 76 65 72 79 20 70 61 67 65 20  ld.  Every page 
14ccf 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
14cd0 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 30 20 6f 72  contains.** 0 or
14cd1 20 31 20 70 61 72 65 6e 74 20 70 61 67 65 73 2e   1 parent pages.
14cd2 20 20 28 49 6e 20 74 68 69 73 20 63 6f 6e 74 65    (In this conte
14cd3 78 74 20 27 64 61 74 61 62 61 73 65 20 70 61 67  xt 'database pag
14cd4 65 27 20 72 65 66 65 72 73 0a 2a 2a 20 74 6f 20  e' refers.** to 
14cd5 61 6e 79 20 70 61 67 65 20 74 68 61 74 20 69 73  any page that is
14cd6 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65   not part of the
14cd7 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 69 74 73   pointer map its
14cd8 65 6c 66 2e 29 20 20 45 61 63 68 20 70 6f 69 6e  elf.)  Each poin
14cd9 74 65 72 20 6d 61 70 0a 2a 2a 20 65 6e 74 72 79  ter map.** entry
14cda 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73   consists of a s
14cdb 69 6e 67 6c 65 20 62 79 74 65 20 27 74 79 70 65  ingle byte 'type
14cdc 27 20 61 6e 64 20 61 20 34 20 62 79 74 65 20 70  ' and a 4 byte p
14cdd 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65  arent page numbe
14cde 72 2e 0a 2a 2a 20 54 68 65 20 50 54 52 4d 41 50  r..** The PTRMAP
14cdf 5f 58 58 58 20 69 64 65 6e 74 69 66 69 65 72 73  _XXX identifiers
14ce0 20 62 65 6c 6f 77 20 61 72 65 20 74 68 65 20 76   below are the v
14ce1 61 6c 69 64 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a  alid types..**.*
14ce2 2a 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66  * The purpose of
14ce3 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
14ce4 20 69 73 20 74 6f 20 66 61 63 69 6c 69 74 79 20   is to facility 
14ce5 6d 6f 76 69 6e 67 20 70 61 67 65 73 20 66 72 6f  moving pages fro
14ce6 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73 69 74 69 6f  m one.** positio
14ce7 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 20 74 6f  n in the file to
14ce8 20 61 6e 6f 74 68 65 72 20 61 73 20 70 61 72 74   another as part
14ce9 20 6f 66 20 61 75 74 6f 76 61 63 75 75 6d 2e 20   of autovacuum. 
14cea 20 57 68 65 6e 20 61 20 70 61 67 65 0a 2a 2a 20   When a page.** 
14ceb 69 73 20 6d 6f 76 65 64 2c 20 74 68 65 20 70 6f  is moved, the po
14cec 69 6e 74 65 72 20 69 6e 20 69 74 73 20 70 61 72  inter in its par
14ced 65 6e 74 20 6d 75 73 74 20 62 65 20 75 70 64 61  ent must be upda
14cee 74 65 64 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ted to point to 
14cef 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 6f 63 61 74  the.** new locat
14cf0 69 6f 6e 2e 20 20 54 68 65 20 70 6f 69 6e 74 65  ion.  The pointe
14cf1 72 20 6d 61 70 20 69 73 20 75 73 65 64 20 74 6f  r map is used to
14cf2 20 6c 6f 63 61 74 65 20 74 68 65 20 70 61 72 65   locate the pare
14cf3 6e 74 20 70 61 67 65 20 71 75 69 63 6b 6c 79 2e  nt page quickly.
14cf4 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 52 4f  .**.** PTRMAP_RO
14cf5 4f 54 50 41 47 45 3a 20 54 68 65 20 64 61 74 61  OTPAGE: The data
14cf6 62 61 73 65 20 70 61 67 65 20 69 73 20 61 20 72  base page is a r
14cf7 6f 6f 74 2d 70 61 67 65 2e 20 54 68 65 20 70 61  oot-page. The pa
14cf8 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74  ge-number is not
14cf9 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
14cfa 20 20 20 20 20 75 73 65 64 20 69 6e 20 74 68 69       used in thi
14cfb 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 50 54  s case..**.** PT
14cfc 52 4d 41 50 5f 46 52 45 45 50 41 47 45 3a 20 54  RMAP_FREEPAGE: T
14cfd 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
14cfe 20 69 73 20 61 6e 20 75 6e 75 73 65 64 20 28 66   is an unused (f
14cff 72 65 65 29 20 70 61 67 65 2e 20 54 68 65 20 70  ree) page. The p
14d00 61 67 65 2d 6e 75 6d 62 65 72 20 0a 2a 2a 20 20  age-number .**  
14d01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14d02 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e 20 74  is not used in t
14d03 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
14d04 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31  PTRMAP_OVERFLOW1
14d05 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
14d06 61 67 65 20 69 73 20 74 68 65 20 66 69 72 73 74  age is the first
14d07 20 70 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20   page in a list 
14d08 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  of .**          
14d09 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f           overflo
14d0a 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 61 67  w pages. The pag
14d0b 65 20 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66  e number identif
14d0c 69 65 73 20 74 68 65 20 70 61 67 65 20 74 68 61  ies the page tha
14d0d 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
14d0e 20 20 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20         contains 
14d0f 74 68 65 20 63 65 6c 6c 20 77 69 74 68 20 61 20  the cell with a 
14d10 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20  pointer to this 
14d11 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a  overflow page..*
14d12 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52  *.** PTRMAP_OVER
14d13 46 4c 4f 57 32 3a 20 54 68 65 20 64 61 74 61 62  FLOW2: The datab
14d14 61 73 65 20 70 61 67 65 20 69 73 20 74 68 65 20  ase page is the 
14d15 73 65 63 6f 6e 64 20 6f 72 20 6c 61 74 65 72 20  second or later 
14d16 70 61 67 65 20 69 6e 20 61 20 6c 69 73 74 20 6f  page in a list o
14d17 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  f.**            
14d18 20 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20         overflow 
14d19 70 61 67 65 73 2e 20 54 68 65 20 70 61 67 65 2d  pages. The page-
14d1a 6e 75 6d 62 65 72 20 69 64 65 6e 74 69 66 69 65  number identifie
14d1b 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a  s the previous.*
14d1c 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
14d1d 20 20 20 20 70 61 67 65 20 69 6e 20 74 68 65 20      page in the 
14d1e 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69  overflow page li
14d1f 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  st..**.** PTRMAP
14d20 5f 42 54 52 45 45 3a 20 54 68 65 20 64 61 74 61  _BTREE: The data
14d21 62 61 73 65 20 70 61 67 65 20 69 73 20 61 20 6e  base page is a n
14d22 6f 6e 2d 72 6f 6f 74 20 62 74 72 65 65 20 70 61  on-root btree pa
14d23 67 65 2e 20 54 68 65 20 70 61 67 65 20 6e 75 6d  ge. The page num
14d24 62 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ber.**          
14d25 20 20 20 20 20 69 64 65 6e 74 69 66 69 65 73 20       identifies 
14d26 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
14d27 69 6e 20 74 68 65 20 62 74 72 65 65 2e 0a 2a 2f  in the btree..*/
14d28 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f  .#define PTRMAP_
14d29 52 4f 4f 54 50 41 47 45 20 31 0a 23 64 65 66 69  ROOTPAGE 1.#defi
14d2a 6e 65 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41  ne PTRMAP_FREEPA
14d2b 47 45 20 32 0a 23 64 65 66 69 6e 65 20 50 54 52  GE 2.#define PTR
14d2c 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 33 0a  MAP_OVERFLOW1 3.
14d2d 23 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 4f  #define PTRMAP_O
14d2e 56 45 52 46 4c 4f 57 32 20 34 0a 23 64 65 66 69  VERFLOW2 4.#defi
14d2f 6e 65 20 50 54 52 4d 41 50 5f 42 54 52 45 45 20  ne PTRMAP_BTREE 
14d30 35 0a 0a 2f 2a 20 41 20 62 75 6e 63 68 20 6f 66  5../* A bunch of
14d31 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
14d32 65 6e 74 73 20 74 6f 20 63 68 65 63 6b 20 74 68  ents to check th
14d33 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74  e transaction st
14d34 61 74 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a  ate variables.**
14d35 20 6f 66 20 68 61 6e 64 6c 65 20 70 20 28 74 79   of handle p (ty
14d36 70 65 20 42 74 72 65 65 2a 29 20 61 72 65 20 69  pe Btree*) are i
14d37 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e 73 69 73  nternally consis
14d38 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tent..*/.#define
14d39 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28   btreeIntegrity(
14d3a 70 29 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70  p) \.  assert( p
14d3b 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ->pBt->inTransac
14d3c 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45  tion!=TRANS_NONE
14d3d 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 6e 54 72 61   || p->pBt->nTra
14d3e 6e 73 61 63 74 69 6f 6e 3d 3d 30 20 29 3b 20 5c  nsaction==0 ); \
14d3f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42  .  assert( p->pB
14d40 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
14d41 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73 20 29 3b 20  >=p->inTrans ); 
14d42 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 49 53 41  .../*.** The ISA
14d43 55 54 4f 56 41 43 55 55 4d 20 6d 61 63 72 6f 20  UTOVACUUM macro 
14d44 69 73 20 75 73 65 64 20 77 69 74 68 69 6e 20 62  is used within b
14d45 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
14d46 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a   to determine.**
14d47 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
14d48 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76   supports auto-v
14d49 61 63 75 75 6d 20 6f 72 20 6e 6f 74 2e 20 42 65  acuum or not. Be
14d4a 63 61 75 73 65 20 69 74 20 69 73 20 75 73 65 64  cause it is used
14d4b 0a 2a 2a 20 77 69 74 68 69 6e 20 61 6e 20 65 78  .** within an ex
14d4c 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73  pression that is
14d4d 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   an argument to 
14d4e 61 6e 6f 74 68 65 72 20 6d 61 63 72 6f 20 0a 2a  another macro .*
14d4f 2a 20 28 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 52  * (sqliteMallocR
14d50 61 77 29 2c 20 69 74 20 69 73 20 6e 6f 74 20 70  aw), it is not p
14d51 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 63  ossible to use c
14d52 6f 6e 64 69 74 69 6f 6e 61 6c 20 63 6f 6d 70 69  onditional compi
14d53 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 2c 20 74  lation..** So, t
14d54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 64 65 66  his macro is def
14d55 69 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2f  ined instead..*/
14d56 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
14d57 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
14d58 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41  #define ISAUTOVA
14d59 43 55 55 4d 20 28 70 42 74 2d 3e 61 75 74 6f 56  CUUM (pBt->autoV
14d5a 61 63 75 75 6d 29 0a 23 65 6c 73 65 0a 23 64 65  acuum).#else.#de
14d5b 66 69 6e 65 20 49 53 41 55 54 4f 56 41 43 55 55  fine ISAUTOVACUU
14d5c 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  M 0.#endif.../*.
14d5d 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
14d5e 65 20 69 73 20 70 61 73 73 65 64 20 61 72 6f 75  e is passed arou
14d5f 6e 64 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74  nd through all t
14d60 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69  he sanity checki
14d61 6e 67 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69  ng routines.** i
14d62 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
14d63 74 72 61 63 6b 20 6f 66 20 73 6f 6d 65 20 67 6c  track of some gl
14d64 6f 62 61 6c 20 73 74 61 74 65 20 69 6e 66 6f 72  obal state infor
14d65 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64  mation..*/.typed
14d66 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 67 72  ef struct Integr
14d67 69 74 79 43 6b 20 49 6e 74 65 67 72 69 74 79 43  ityCk IntegrityC
14d68 6b 3b 0a 73 74 72 75 63 74 20 49 6e 74 65 67 72  k;.struct Integr
14d69 69 74 79 43 6b 20 7b 0a 20 20 42 74 53 68 61 72  ityCk {.  BtShar
14d6a 65 64 20 2a 70 42 74 3b 20 20 20 20 2f 2a 20 54  ed *pBt;    /* T
14d6b 68 65 20 74 72 65 65 20 62 65 69 6e 67 20 63 68  he tree being ch
14d6c 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a 20 20 50  ecked out */.  P
14d6d 61 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20  ager *pPager;   
14d6e 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74   /* The associat
14d6f 65 64 20 70 61 67 65 72 2e 20 20 41 6c 73 6f 20  ed pager.  Also 
14d70 61 63 63 65 73 73 69 62 6c 65 20 62 79 20 70 42  accessible by pB
14d71 74 2d 3e 70 50 61 67 65 72 20 2a 2f 0a 20 20 50  t->pPager */.  P
14d72 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20 20 20 20  gno nPage;      
14d73 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
14d74 67 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62  ges in the datab
14d75 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 6e  ase */.  int *an
14d76 52 65 66 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  Ref;       /* Nu
14d77 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 65 61  mber of times ea
14d78 63 68 20 70 61 67 65 20 69 73 20 72 65 66 65 72  ch page is refer
14d79 65 6e 63 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6d  enced */.  int m
14d7a 78 45 72 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  xErr;        /* 
14d7b 53 74 6f 70 20 61 63 63 75 6d 75 6c 61 74 69 6e  Stop accumulatin
14d7c 67 20 65 72 72 6f 72 73 20 77 68 65 6e 20 74 68  g errors when th
14d7d 69 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20  is reaches zero 
14d7e 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20  */.  int nErr;  
14d7f 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
14d80 20 6f 66 20 6d 65 73 73 61 67 65 73 20 77 72 69   of messages wri
14d81 74 74 65 6e 20 74 6f 20 7a 45 72 72 4d 73 67 20  tten to zErrMsg 
14d82 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20  so far */.  int 
14d83 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 2f 2a  mallocFailed; /*
14d84 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   A memory alloca
14d85 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20 6f  tion error has o
14d86 63 63 75 72 72 65 64 20 2a 2f 0a 20 20 53 74 72  ccurred */.  Str
14d87 41 63 63 75 6d 20 65 72 72 4d 73 67 3b 20 20 2f  Accum errMsg;  /
14d88 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 74 68 65  * Accumulate the
14d89 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
14d8a 65 78 74 20 68 65 72 65 20 2a 2f 0a 7d 3b 0a 0a  ext here */.};..
14d8b 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72  /*.** Read or wr
14d8c 69 74 65 20 61 20 74 77 6f 2d 20 61 6e 64 20 66  ite a two- and f
14d8d 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64  our-byte big-end
14d8e 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ian integer valu
14d8f 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67  es..*/.#define g
14d90 65 74 32 62 79 74 65 28 78 29 20 20 20 28 28 78  et2byte(x)   ((x
14d91 29 5b 30 5d 3c 3c 38 20 7c 20 28 78 29 5b 31 5d  )[0]<<8 | (x)[1]
14d92 29 0a 23 64 65 66 69 6e 65 20 70 75 74 32 62 79  ).#define put2by
14d93 74 65 28 70 2c 76 29 20 28 28 70 29 5b 30 5d 20  te(p,v) ((p)[0] 
14d94 3d 20 28 75 38 29 28 28 76 29 3e 3e 38 29 2c 20  = (u8)((v)>>8), 
14d95 28 70 29 5b 31 5d 20 3d 20 28 75 38 29 28 76 29  (p)[1] = (u8)(v)
14d96 29 0a 23 64 65 66 69 6e 65 20 67 65 74 34 62 79  ).#define get4by
14d97 74 65 20 73 71 6c 69 74 65 33 47 65 74 34 62 79  te sqlite3Get4by
14d98 74 65 0a 23 64 65 66 69 6e 65 20 70 75 74 34 62  te.#define put4b
14d99 79 74 65 20 73 71 6c 69 74 65 33 50 75 74 34 62  yte sqlite3Put4b
14d9a 79 74 65 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  yte../**********
14d9b 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65  **** End of btre
14d9c 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
14d9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14d9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14d9f 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
14da0 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
14da1 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
14da2 66 20 69 6e 20 62 74 6d 75 74 65 78 2e 63 20 2a  f in btmutex.c *
14da3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14da4 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ***/.#ifndef SQL
14da5 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
14da6 43 41 43 48 45 0a 23 69 66 20 53 51 4c 49 54 45  CACHE.#if SQLITE
14da7 5f 54 48 52 45 41 44 53 41 46 45 0a 0a 2f 2a 0a  _THREADSAFE../*.
14da8 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 42 74  ** Obtain the Bt
14da9 53 68 61 72 65 64 20 6d 75 74 65 78 20 61 73 73  Shared mutex ass
14daa 6f 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54  ociated with B-T
14dab 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 41 6c  ree handle p. Al
14dac 73 6f 2c 0a 2a 2a 20 73 65 74 20 42 74 53 68 61  so,.** set BtSha
14dad 72 65 64 2e 64 62 20 74 6f 20 74 68 65 20 64 61  red.db to the da
14dae 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 61 73  tabase handle as
14daf 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 20  sociated with p 
14db0 61 6e 64 20 74 68 65 0a 2a 2a 20 70 2d 3e 6c 6f  and the.** p->lo
14db1 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 20 74 6f 20  cked boolean to 
14db2 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  true..*/.static 
14db3 76 6f 69 64 20 6c 6f 63 6b 42 74 72 65 65 4d 75  void lockBtreeMu
14db4 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  tex(Btree *p){. 
14db5 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b   assert( p->lock
14db6 65 64 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ed==0 );.  asser
14db7 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14db8 5f 6e 6f 74 68 65 6c 64 28 70 2d 3e 70 42 74 2d  _notheld(p->pBt-
14db9 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
14dba 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14dbb 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
14dbc 75 74 65 78 29 20 29 3b 0a 0a 20 20 73 71 6c 69  utex) );..  sqli
14dbd 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
14dbe 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a  p->pBt->mutex);.
14dbf 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70    p->pBt->db = p
14dc0 2d 3e 64 62 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 65  ->db;.  p->locke
14dc1 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  d = 1;.}../*.** 
14dc2 52 65 6c 65 61 73 65 20 74 68 65 20 42 74 53 68  Release the BtSh
14dc3 61 72 65 64 20 6d 75 74 65 78 20 61 73 73 6f 63  ared mutex assoc
14dc4 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72 65  iated with B-Tre
14dc5 65 20 68 61 6e 64 6c 65 20 70 20 61 6e 64 0a 2a  e handle p and.*
14dc6 2a 20 63 6c 65 61 72 20 74 68 65 20 70 2d 3e 6c  * clear the p->l
14dc7 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 2e 0a 2a  ocked boolean..*
14dc8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e  /.static void un
14dc9 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 42  lockBtreeMutex(B
14dca 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65  tree *p){.  asse
14dcb 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 31  rt( p->locked==1
14dcc 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
14dcd 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14dce 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  (p->pBt->mutex) 
14dcf 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
14dd0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14dd1 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
14dd2 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62  .  assert( p->db
14dd3 3d 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a  ==p->pBt->db );.
14dd4 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
14dd5 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74 2d 3e 6d  _leave(p->pBt->m
14dd6 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f 63 6b  utex);.  p->lock
14dd7 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ed = 0;.}../*.**
14dd8 20 45 6e 74 65 72 20 61 20 6d 75 74 65 78 20 6f   Enter a mutex o
14dd9 6e 20 74 68 65 20 67 69 76 65 6e 20 42 54 72 65  n the given BTre
14dda 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20  e object..**.** 
14ddb 49 66 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73  If the object is
14ddc 20 6e 6f 74 20 73 68 61 72 61 62 6c 65 2c 20 74   not sharable, t
14ddd 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20 69 73 20  hen no mutex is 
14dde 65 76 65 72 20 72 65 71 75 69 72 65 64 0a 2a 2a  ever required.**
14ddf 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
14de0 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 54  e is a no-op.  T
14de1 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 75  he underlying mu
14de2 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65 63 75 72  tex is non-recur
14de3 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20 77 65 20  sive..** But we 
14de4 6b 65 65 70 20 61 20 72 65 66 65 72 65 6e 63 65  keep a reference
14de5 20 63 6f 75 6e 74 20 69 6e 20 42 74 72 65 65 2e   count in Btree.
14de6 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f 20 74 68  wantToLock so th
14de7 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 6f 66  e behavior.** of
14de8 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   this interface 
14de9 69 73 20 72 65 63 75 72 73 69 76 65 2e 0a 2a 2a  is recursive..**
14dea 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20 64 65 61  .** To avoid dea
14deb 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69 70 6c 65  dlocks, multiple
14dec 20 42 74 72 65 65 73 20 61 72 65 20 6c 6f 63 6b   Btrees are lock
14ded 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f  ed in the same o
14dee 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c 6c 20 64  rder.** by all d
14def 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
14df0 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e 70 4e 65  ons.  The p->pNe
14df1 78 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20  xt is a list of 
14df2 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65 65 73 20  other.** Btrees 
14df3 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 74 68 65  belonging to the
14df4 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63   same database c
14df5 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20 74 68 65  onnection as the
14df6 20 70 20 42 74 72 65 65 0a 2a 2a 20 77 68 69 63   p Btree.** whic
14df7 68 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63  h need to be loc
14df8 6b 65 64 20 61 66 74 65 72 20 70 2e 20 20 49 66  ked after p.  If
14df9 20 77 65 20 63 61 6e 6e 6f 74 20 67 65 74 20 61   we cannot get a
14dfa 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70 2c 20 74   lock on.** p, t
14dfb 68 65 6e 20 66 69 72 73 74 20 75 6e 6c 6f 63 6b  hen first unlock
14dfc 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f 74 68 65   all of the othe
14dfd 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78 74 2c 20  rs on p->pNext, 
14dfe 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20 66 6f 72  then wait.** for
14dff 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20 62 65 63   the lock to bec
14e00 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e  ome available on
14e01 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f 63 6b 20   p, then relock 
14e02 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20 73 75  all of the.** su
14e03 62 73 65 71 75 65 6e 74 20 42 74 72 65 65 73 20  bsequent Btrees 
14e04 74 68 61 74 20 64 65 73 69 72 65 20 61 20 6c 6f  that desire a lo
14e05 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ck..*/.SQLITE_PR
14e06 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
14e07 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74 72  e3BtreeEnter(Btr
14e08 65 65 20 2a 70 29 7b 0a 20 20 42 74 72 65 65 20  ee *p){.  Btree 
14e09 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f 2a 20 53  *pLater;..  /* S
14e0a 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79  ome basic sanity
14e0b 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65   checking on the
14e0c 20 42 74 72 65 65 2e 20 20 54 68 65 20 6c 69 73   Btree.  The lis
14e0d 74 20 6f 66 20 42 74 72 65 65 73 0a 20 20 2a 2a  t of Btrees.  **
14e0e 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 70 4e   connected by pN
14e0f 65 78 74 20 61 6e 64 20 70 50 72 65 76 20 73 68  ext and pPrev sh
14e10 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f 72 74 65  ould be in sorte
14e11 64 20 6f 72 64 65 72 20 62 79 0a 20 20 2a 2a 20  d order by.  ** 
14e12 42 74 72 65 65 2e 70 42 74 20 76 61 6c 75 65 2e  Btree.pBt value.
14e13 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66   All elements of
14e14 20 74 68 65 20 6c 69 73 74 20 73 68 6f 75 6c 64   the list should
14e15 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20 2a 2a 20   belong to.  ** 
14e16 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74  the same connect
14e17 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61 72 65 64  ion. Only shared
14e18 20 42 74 72 65 65 73 20 61 72 65 20 6f 6e 20 74   Btrees are on t
14e19 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 61 73  he list. */.  as
14e1a 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74 3d 3d  sert( p->pNext==
14e1b 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d 3e 70  0 || p->pNext->p
14e1c 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a 20 20 61  Bt>p->pBt );.  a
14e1d 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76 3d  ssert( p->pPrev=
14e1e 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d 3e  =0 || p->pPrev->
14e1f 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20  pBt<p->pBt );.  
14e20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78 74  assert( p->pNext
14e21 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74 2d  ==0 || p->pNext-
14e22 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20  >db==p->db );.  
14e23 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 65 76  assert( p->pPrev
14e24 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65 76 2d  ==0 || p->pPrev-
14e25 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20  >db==p->db );.  
14e26 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61  assert( p->shara
14e27 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e 65 78 74  ble || (p->pNext
14e28 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72 65 76 3d  ==0 && p->pPrev=
14e29 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65  =0) );..  /* Che
14e2a 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 63  ck for locking c
14e2b 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f 0a 20 20  onsistency */.  
14e2c 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63 6b  assert( !p->lock
14e2d 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c  ed || p->wantToL
14e2e 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ock>0 );.  asser
14e2f 74 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c  t( p->sharable |
14e30 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  | p->wantToLock=
14e31 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 73  =0 );..  /* We s
14e32 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f  hould already ho
14e33 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ld a lock on the
14e34 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
14e35 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72 74  tion */.  assert
14e36 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
14e37 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  held(p->db->mute
14e38 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65  x) );..  /* Unle
14e39 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ss the database 
14e3a 69 73 20 73 68 61 72 61 62 6c 65 20 61 6e 64 20  is sharable and 
14e3b 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65 6e 20 42  unlocked, then B
14e3c 74 53 68 61 72 65 64 2e 64 62 0a 20 20 2a 2a 20  tShared.db.  ** 
14e3d 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 62  should already b
14e3e 65 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e  e set correctly.
14e3f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70   */.  assert( (p
14e40 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26 26 20 70  ->locked==0 && p
14e41 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c 7c 20 70  ->sharable) || p
14e42 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62  ->pBt->db==p->db
14e43 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 2d 3e 73   );..  if( !p->s
14e44 68 61 72 61 62 6c 65 20 29 20 72 65 74 75 72 6e  harable ) return
14e45 3b 0a 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  ;.  p->wantToLoc
14e46 6b 2b 2b 3b 0a 20 20 69 66 28 20 70 2d 3e 6c 6f  k++;.  if( p->lo
14e47 63 6b 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 0a  cked ) return;..
14e48 20 20 2f 2a 20 49 6e 20 6d 6f 73 74 20 63 61 73    /* In most cas
14e49 65 73 2c 20 77 65 20 73 68 6f 75 6c 64 20 62 65  es, we should be
14e4a 20 61 62 6c 65 20 74 6f 20 61 63 71 75 69 72 65   able to acquire
14e4b 20 74 68 65 20 6c 6f 63 6b 20 77 65 0a 20 20 2a   the lock we.  *
14e4c 2a 20 77 61 6e 74 20 77 69 74 68 6f 75 74 20 68  * want without h
14e4d 61 76 69 6e 67 20 74 6f 20 67 6f 20 74 68 72 6f  aving to go thro
14e4e 75 67 68 74 20 74 68 65 20 61 73 63 65 6e 64 69  ught the ascendi
14e4f 6e 67 20 6c 6f 63 6b 0a 20 20 2a 2a 20 70 72 6f  ng lock.  ** pro
14e50 63 65 64 75 72 65 20 74 68 61 74 20 66 6f 6c 6c  cedure that foll
14e51 6f 77 73 2e 20 20 4a 75 73 74 20 62 65 20 73 75  ows.  Just be su
14e52 72 65 20 6e 6f 74 20 74 6f 20 62 6c 6f 63 6b 2e  re not to block.
14e53 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
14e54 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 70 2d  te3_mutex_try(p-
14e55 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3d 3d 53 51  >pBt->mutex)==SQ
14e56 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
14e57 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64  ->pBt->db = p->d
14e58 62 3b 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b 65 64  b;.    p->locked
14e59 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
14e5a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20 61  ;.  }..  /* To a
14e5b 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2c 20 66  void deadlock, f
14e5c 69 72 73 74 20 72 65 6c 65 61 73 65 20 61 6c 6c  irst release all
14e5d 20 6c 6f 63 6b 73 20 77 69 74 68 20 61 20 6c 61   locks with a la
14e5e 72 67 65 72 0a 20 20 2a 2a 20 42 74 53 68 61 72  rger.  ** BtShar
14e5f 65 64 20 61 64 64 72 65 73 73 2e 20 20 54 68 65  ed address.  The
14e60 6e 20 61 63 71 75 69 72 65 20 6f 75 72 20 6c 6f  n acquire our lo
14e61 63 6b 2e 20 20 54 68 65 6e 20 72 65 61 63 71 75  ck.  Then reacqu
14e62 69 72 65 0a 20 20 2a 2a 20 74 68 65 20 6f 74 68  ire.  ** the oth
14e63 65 72 20 42 74 53 68 61 72 65 64 20 6c 6f 63 6b  er BtShared lock
14e64 73 20 74 68 61 74 20 77 65 20 75 73 65 64 20 74  s that we used t
14e65 6f 20 68 6f 6c 64 20 69 6e 20 61 73 63 65 6e 64  o hold in ascend
14e66 69 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 72 2e 0a  ing.  ** order..
14e67 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 61 74 65    */.  for(pLate
14e68 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74  r=p->pNext; pLat
14e69 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61 74 65  er; pLater=pLate
14e6a 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 61  r->pNext){.    a
14e6b 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 73  ssert( pLater->s
14e6c 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 20 20 61  harable );.    a
14e6d 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d 3e 70  ssert( pLater->p
14e6e 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 4c 61 74 65  Next==0 || pLate
14e6f 72 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e 70 4c  r->pNext->pBt>pL
14e70 61 74 65 72 2d 3e 70 42 74 20 29 3b 0a 20 20 20  ater->pBt );.   
14e71 20 61 73 73 65 72 74 28 20 21 70 4c 61 74 65 72   assert( !pLater
14e72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c 61 74  ->locked || pLat
14e73 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30  er->wantToLock>0
14e74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 61 74   );.    if( pLat
14e75 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20  er->locked ){.  
14e76 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d      unlockBtreeM
14e77 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20  utex(pLater);.  
14e78 20 20 7d 0a 20 20 7d 0a 20 20 6c 6f 63 6b 42 74    }.  }.  lockBt
14e79 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 66  reeMutex(p);.  f
14e7a 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65  or(pLater=p->pNe
14e7b 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74  xt; pLater; pLat
14e7c 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74  er=pLater->pNext
14e7d 29 7b 0a 20 20 20 20 69 66 28 20 70 4c 61 74 65  ){.    if( pLate
14e7e 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b  r->wantToLock ){
14e7f 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74 72 65 65  .      lockBtree
14e80 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20  Mutex(pLater);. 
14e81 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
14e82 2a 20 45 78 69 74 20 74 68 65 20 72 65 63 75 72  * Exit the recur
14e83 73 69 76 65 20 6d 75 74 65 78 20 6f 6e 20 61 20  sive mutex on a 
14e84 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Btree..*/.SQLITE
14e85 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
14e86 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
14e87 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 66 28  Btree *p){.  if(
14e88 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a   p->sharable ){.
14e89 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77      assert( p->w
14e8a 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20  antToLock>0 );. 
14e8b 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b     p->wantToLock
14e8c 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 77  --;.    if( p->w
14e8d 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 7b 0a  antToLock==0 ){.
14e8e 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65        unlockBtre
14e8f 65 4d 75 74 65 78 28 70 29 3b 0a 20 20 20 20 7d  eMutex(p);.    }
14e90 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  .  }.}..#ifndef 
14e91 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74  NDEBUG./*.** Ret
14e92 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
14e93 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 69  BtShared mutex i
14e94 73 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 62 74  s held on the bt
14e95 72 65 65 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a  ree, or if the.*
14e96 2a 20 42 2d 54 72 65 65 20 69 73 20 6e 6f 74 20  * B-Tree is not 
14e97 6d 61 72 6b 65 64 20 61 73 20 73 68 61 72 61 62  marked as sharab
14e98 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  le..**.** This r
14e99 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 6f  outine is used o
14e9a 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  nly from within 
14e9b 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
14e9c 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nts..*/.SQLITE_P
14e9d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
14e9e 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
14e9f 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61  x(Btree *p){.  a
14ea0 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62  ssert( p->sharab
14ea1 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b  le==0 || p->lock
14ea2 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77 61 6e 74  ed==0 || p->want
14ea3 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73  ToLock>0 );.  as
14ea4 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c  sert( p->sharabl
14ea5 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b 65  e==0 || p->locke
14ea6 64 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 3d 3d 70  d==0 || p->db==p
14ea7 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61  ->pBt->db );.  a
14ea8 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62  ssert( p->sharab
14ea9 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63 6b  le==0 || p->lock
14eaa 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  ed==0 || sqlite3
14eab 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70  _mutex_held(p->p
14eac 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
14ead 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61  assert( p->shara
14eae 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f 63  ble==0 || p->loc
14eaf 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  ked==0 || sqlite
14eb0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
14eb1 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
14eb2 20 72 65 74 75 72 6e 20 28 70 2d 3e 73 68 61 72   return (p->shar
14eb3 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f  able==0 || p->lo
14eb4 63 6b 65 64 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  cked);.}.#endif.
14eb5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
14eb6 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f  _OMIT_INCRBLOB./
14eb7 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20 6c  *.** Enter and l
14eb8 65 61 76 65 20 61 20 6d 75 74 65 78 20 6f 6e 20  eave a mutex on 
14eb9 61 20 42 74 72 65 65 20 67 69 76 65 6e 20 61 20  a Btree given a 
14eba 63 75 72 73 6f 72 20 6f 77 6e 65 64 20 62 79 20  cursor owned by 
14ebb 74 68 61 74 0a 2a 2a 20 42 74 72 65 65 2e 20 20  that.** Btree.  
14ebc 54 68 65 73 65 20 65 6e 74 72 79 20 70 6f 69 6e  These entry poin
14ebd 74 73 20 61 72 65 20 75 73 65 64 20 62 79 20 69  ts are used by i
14ebe 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f 4f 20 61  ncremental I/O a
14ebf 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20 6f 6d 69  nd can be.** omi
14ec0 74 74 65 64 20 69 66 20 74 68 61 74 20 6d 6f 64  tted if that mod
14ec1 75 6c 65 20 69 73 20 6e 6f 74 20 75 73 65 64 2e  ule is not used.
14ec2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
14ec3 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
14ec4 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72 28  treeEnterCursor(
14ec5 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
14ec6 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
14ec7 6e 74 65 72 28 70 43 75 72 2d 3e 70 42 74 72 65  nter(pCur->pBtre
14ec8 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  e);.}.SQLITE_PRI
14ec9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14eca 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f  3BtreeLeaveCurso
14ecb 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  r(BtCursor *pCur
14ecc 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  ){.  sqlite3Btre
14ecd 65 4c 65 61 76 65 28 70 43 75 72 2d 3e 70 42 74  eLeave(pCur->pBt
14ece 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  ree);.}.#endif /
14ecf 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  * SQLITE_OMIT_IN
14ed0 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  CRBLOB */.../*.*
14ed1 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65  * Enter the mute
14ed2 78 20 6f 6e 20 65 76 65 72 79 20 42 74 72 65 65  x on every Btree
14ed3 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
14ed4 20 61 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63   a database.** c
14ed5 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69 73  onnection.  This
14ed6 20 69 73 20 6e 65 65 64 65 64 20 28 66 6f 72 20   is needed (for 
14ed7 65 78 61 6d 70 6c 65 29 20 70 72 69 6f 72 20 74  example) prior t
14ed8 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20 61 20 73  o parsing.** a s
14ed9 74 61 74 65 6d 65 6e 74 20 73 69 6e 63 65 20 77  tatement since w
14eda 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70 61 72  e will be compar
14edb 69 6e 67 20 74 61 62 6c 65 20 61 6e 64 20 63 6f  ing table and co
14edc 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a 20 61 67  lumn names.** ag
14edd 61 69 6e 73 74 20 61 6c 6c 20 73 63 68 65 6d 61  ainst all schema
14ede 73 20 61 6e 64 20 77 65 20 64 6f 20 6e 6f 74 20  s and we do not 
14edf 77 61 6e 74 20 74 68 6f 73 65 20 73 63 68 65 6d  want those schem
14ee0 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72 65 73 65  as being.** rese
14ee1 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72  t out from under
14ee2 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65   us..**.** There
14ee3 20 69 73 20 61 20 63 6f 72 72 65 73 70 6f 6e 64   is a correspond
14ee4 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c 20 70 72  ing leave-all pr
14ee5 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20  ocedures..**.** 
14ee6 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 65  Enter the mutexe
14ee7 73 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f  s in accending o
14ee8 72 64 65 72 20 62 79 20 42 74 53 68 61 72 65 64  rder by BtShared
14ee9 20 70 6f 69 6e 74 65 72 20 61 64 64 72 65 73 73   pointer address
14eea 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20 74 68 65  .** to avoid the
14eeb 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20   possibility of 
14eec 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e 20 74 77  deadlock when tw
14eed 6f 20 74 68 72 65 61 64 73 20 77 69 74 68 0a 2a  o threads with.*
14eee 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 74  * two or more bt
14eef 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f 6e 20 62  rees in common b
14ef0 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f 63 6b 20  oth try to lock 
14ef1 61 6c 6c 20 74 68 65 69 72 20 62 74 72 65 65 73  all their btrees
14ef2 0a 2a 2a 20 61 74 20 74 68 65 20 73 61 6d 65 20  .** at the same 
14ef3 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  instant..*/.SQLI
14ef4 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
14ef5 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
14ef6 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62  rAll(sqlite3 *db
14ef7 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74  ){.  int i;.  Bt
14ef8 72 65 65 20 2a 70 2c 20 2a 70 4c 61 74 65 72 3b  ree *p, *pLater;
14ef9 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
14efa 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
14efb 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f  ->mutex) );.  fo
14efc 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
14efd 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20  ; i++){.    p = 
14efe 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
14eff 20 20 20 20 61 73 73 65 72 74 28 20 21 70 20 7c      assert( !p |
14f00 7c 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20  | (p->locked==0 
14f01 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 20  && p->sharable) 
14f02 7c 7c 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70  || p->pBt->db==p
14f03 2d 3e 64 62 20 29 3b 0a 20 20 20 20 69 66 28 20  ->db );.    if( 
14f04 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65  p && p->sharable
14f05 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e   ){.      p->wan
14f06 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20  tToLock++;.     
14f07 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20   if( !p->locked 
14f08 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
14f09 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  t( p->wantToLock
14f0a 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 77  ==1 );.        w
14f0b 68 69 6c 65 28 20 70 2d 3e 70 50 72 65 76 20 29  hile( p->pPrev )
14f0c 20 70 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20   p = p->pPrev;. 
14f0d 20 20 20 20 20 20 20 2f 2a 20 52 65 61 73 6f 6e         /* Reason
14f0e 20 66 6f 72 20 41 4c 57 41 59 53 3a 20 20 54 68   for ALWAYS:  Th
14f0f 65 72 65 20 6d 75 73 74 20 62 65 20 61 74 20 6c  ere must be at l
14f10 65 61 73 74 20 6f 6e 20 75 6e 6c 6f 63 6b 65 64  east on unlocked
14f11 20 42 74 72 65 65 20 69 6e 0a 20 20 20 20 20 20   Btree in.      
14f12 20 20 2a 2a 20 74 68 65 20 63 68 61 69 6e 2e 20    ** the chain. 
14f13 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 21   Otherwise the !
14f14 70 2d 3e 6c 6f 63 6b 65 64 20 74 65 73 74 20 61  p->locked test a
14f15 62 6f 76 65 20 77 6f 75 6c 64 20 68 61 76 65 20  bove would have 
14f16 66 61 69 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20  failed */.      
14f17 20 20 77 68 69 6c 65 28 20 70 2d 3e 6c 6f 63 6b    while( p->lock
14f18 65 64 20 26 26 20 41 4c 57 41 59 53 28 70 2d 3e  ed && ALWAYS(p->
14f19 70 4e 65 78 74 29 20 29 20 70 20 3d 20 70 2d 3e  pNext) ) p = p->
14f1a 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 66  pNext;.        f
14f1b 6f 72 28 70 4c 61 74 65 72 20 3d 20 70 2d 3e 70  or(pLater = p->p
14f1c 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70 4c  Next; pLater; pL
14f1d 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e 65  ater=pLater->pNe
14f1e 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  xt){.          i
14f1f 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65  f( pLater->locke
14f20 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  d ){.           
14f21 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65   unlockBtreeMute
14f22 78 28 70 4c 61 74 65 72 29 3b 0a 20 20 20 20 20  x(pLater);.     
14f23 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
14f24 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
14f25 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c  p ){.          l
14f26 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29  ockBtreeMutex(p)
14f27 3b 0a 20 20 20 20 20 20 20 20 20 20 70 20 3d 20  ;.          p = 
14f28 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  p->pNext;.      
14f29 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
14f2a 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50  }.  }.}.SQLITE_P
14f2b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
14f2c 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c  te3BtreeLeaveAll
14f2d 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
14f2e 20 69 6e 74 20 69 3b 0a 20 20 42 74 72 65 65 20   int i;.  Btree 
14f2f 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  *p;.  assert( sq
14f30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14f31 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
14f32 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
14f33 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  nDb; i++){.    p
14f34 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
14f35 74 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20  t;.    if( p && 
14f36 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20  p->sharable ){. 
14f37 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
14f38 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a  wantToLock>0 );.
14f39 20 20 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c        p->wantToL
14f3a 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28  ock--;.      if(
14f3b 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d   p->wantToLock==
14f3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 6c  0 ){.        unl
14f3d 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29  ockBtreeMutex(p)
14f3e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
14f3f 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e    }.}..#ifndef N
14f40 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75  DEBUG./*.** Retu
14f41 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 63  rn true if the c
14f42 75 72 72 65 6e 74 20 74 68 72 65 61 64 20 68 6f  urrent thread ho
14f43 6c 64 73 20 74 68 65 20 64 61 74 61 62 61 73 65  lds the database
14f44 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6d   connection.** m
14f45 75 74 65 78 20 61 6e 64 20 61 6c 6c 20 72 65 71  utex and all req
14f46 75 69 72 65 64 20 42 74 53 68 61 72 65 64 20 6d  uired BtShared m
14f47 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  utexes..**.** Th
14f48 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
14f49 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  ed inside assert
14f4a 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e  () statements on
14f4b 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
14f4c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
14f4d 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
14f4e 74 65 78 65 73 28 73 71 6c 69 74 65 33 20 2a 64  texes(sqlite3 *d
14f4f 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  b){.  int i;.  i
14f50 66 28 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65  f( !sqlite3_mute
14f51 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
14f52 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
14f53 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  0;.  }.  for(i=0
14f54 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
14f55 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 3b  ){.    Btree *p;
14f56 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62  .    p = db->aDb
14f57 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
14f58 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c   p && p->sharabl
14f59 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 28 70  e &&.         (p
14f5a 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20  ->wantToLock==0 
14f5b 7c 7c 20 21 73 71 6c 69 74 65 33 5f 6d 75 74 65  || !sqlite3_mute
14f5c 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d  x_held(p->pBt->m
14f5d 75 74 65 78 29 29 20 29 7b 0a 20 20 20 20 20 20  utex)) ){.      
14f5e 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a  return 0;.    }.
14f5f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
14f60 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  }.#endif /* NDEB
14f61 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  UG */../*.** Add
14f62 20 61 20 6e 65 77 20 42 74 72 65 65 20 70 6f 69   a new Btree poi
14f63 6e 74 65 72 20 74 6f 20 61 20 42 74 72 65 65 4d  nter to a BtreeM
14f64 75 74 65 78 41 72 72 61 79 2e 20 0a 2a 2a 20 69  utexArray. .** i
14f65 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20 63 61  f the pointer ca
14f66 6e 20 70 6f 73 73 69 62 6c 79 20 62 65 20 73 68  n possibly be sh
14f67 61 72 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 6f  ared with.** ano
14f68 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f  ther database co
14f69 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nnection..**.** 
14f6a 54 68 65 20 70 6f 69 6e 74 65 72 73 20 61 72 65  The pointers are
14f6b 20 6b 65 70 74 20 69 6e 20 73 6f 72 74 65 64 20   kept in sorted 
14f6c 6f 72 64 65 72 20 62 79 20 70 42 74 72 65 65 2d  order by pBtree-
14f6d 3e 70 42 74 2e 20 20 54 68 61 74 0a 2a 2a 20 77  >pBt.  That.** w
14f6e 61 79 20 77 68 65 6e 20 77 65 20 67 6f 20 74 6f  ay when we go to
14f6f 20 65 6e 74 65 72 20 61 6c 6c 20 74 68 65 20 6d   enter all the m
14f70 75 74 65 78 65 73 2c 20 77 65 20 63 61 6e 20 65  utexes, we can e
14f71 6e 74 65 72 20 74 68 65 6d 0a 2a 2a 20 69 6e 20  nter them.** in 
14f72 6f 72 64 65 72 20 77 69 74 68 6f 75 74 20 65 76  order without ev
14f73 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 62 61  ery having to ba
14f74 63 6b 75 70 20 61 6e 64 20 72 65 74 72 79 20 61  ckup and retry a
14f75 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a 20 77 6f  nd without.** wo
14f76 72 72 79 69 6e 67 20 61 62 6f 75 74 20 64 65 61  rrying about dea
14f77 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  dlock..**.** The
14f78 20 6e 75 6d 62 65 72 20 6f 66 20 73 68 61 72 65   number of share
14f79 64 20 62 74 72 65 65 73 20 77 69 6c 6c 20 61 6c  d btrees will al
14f7a 77 61 79 73 20 62 65 20 73 6d 61 6c 6c 20 28 75  ways be small (u
14f7b 73 75 61 6c 6c 79 20 30 20 6f 72 20 31 29 0a 2a  sually 0 or 1).*
14f7c 2a 20 73 6f 20 61 6e 20 69 6e 73 65 72 74 69 6f  * so an insertio
14f7d 6e 20 73 6f 72 74 20 69 73 20 61 6e 20 61 64 65  n sort is an ade
14f7e 71 75 61 74 65 20 61 6c 67 6f 72 69 74 68 6d 20  quate algorithm 
14f7f 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  here..*/.SQLITE_
14f80 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
14f81 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
14f82 72 61 79 49 6e 73 65 72 74 28 42 74 72 65 65 4d  rayInsert(BtreeM
14f83 75 74 65 78 41 72 72 61 79 20 2a 70 41 72 72 61  utexArray *pArra
14f84 79 2c 20 42 74 72 65 65 20 2a 70 42 74 72 65 65  y, Btree *pBtree
14f85 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  ){.  int i, j;. 
14f86 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 0a   BtShared *pBt;.
14f87 20 20 69 66 28 20 70 42 74 72 65 65 3d 3d 30 20    if( pBtree==0 
14f88 7c 7c 20 70 42 74 72 65 65 2d 3e 73 68 61 72 61  || pBtree->shara
14f89 62 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ble==0 ) return;
14f8a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
14f8b 20 20 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    {.    for(i=0;
14f8c 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65   i<pArray->nMute
14f8d 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61  x; i++){.      a
14f8e 73 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e 61  ssert( pArray->a
14f8f 42 74 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 65  Btree[i]!=pBtree
14f90 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65   );.    }.  }.#e
14f91 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70  ndif.  assert( p
14f92 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3e 3d 30  Array->nMutex>=0
14f93 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 41   );.  assert( pA
14f94 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3c 41 72 72  rray->nMutex<Arr
14f95 61 79 53 69 7a 65 28 70 41 72 72 61 79 2d 3e 61  aySize(pArray->a
14f96 42 74 72 65 65 29 2d 31 20 29 3b 0a 20 20 70 42  Btree)-1 );.  pB
14f97 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b  t = pBtree->pBt;
14f98 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41  .  for(i=0; i<pA
14f99 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b  rray->nMutex; i+
14f9a 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  +){.    assert( 
14f9b 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69  pArray->aBtree[i
14f9c 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a 20 20 20  ]!=pBtree );.   
14f9d 20 69 66 28 20 70 41 72 72 61 79 2d 3e 61 42 74   if( pArray->aBt
14f9e 72 65 65 5b 69 5d 2d 3e 70 42 74 3e 70 42 74 20  ree[i]->pBt>pBt 
14f9f 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 70  ){.      for(j=p
14fa0 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 6a  Array->nMutex; j
14fa1 3e 69 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20  >i; j--){.      
14fa2 20 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65    pArray->aBtree
14fa3 5b 6a 5d 20 3d 20 70 41 72 72 61 79 2d 3e 61 42  [j] = pArray->aB
14fa4 74 72 65 65 5b 6a 2d 31 5d 3b 0a 20 20 20 20 20  tree[j-1];.     
14fa5 20 7d 0a 20 20 20 20 20 20 70 41 72 72 61 79 2d   }.      pArray-
14fa6 3e 61 42 74 72 65 65 5b 69 5d 20 3d 20 70 42 74  >aBtree[i] = pBt
14fa7 72 65 65 3b 0a 20 20 20 20 20 20 70 41 72 72 61  ree;.      pArra
14fa8 79 2d 3e 6e 4d 75 74 65 78 2b 2b 3b 0a 20 20 20  y->nMutex++;.   
14fa9 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
14faa 0a 20 20 7d 0a 20 20 70 41 72 72 61 79 2d 3e 61  .  }.  pArray->a
14fab 42 74 72 65 65 5b 70 41 72 72 61 79 2d 3e 6e 4d  Btree[pArray->nM
14fac 75 74 65 78 2b 2b 5d 20 3d 20 70 42 74 72 65 65  utex++] = pBtree
14fad 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72  ;.}../*.** Enter
14fae 20 74 68 65 20 6d 75 74 65 78 20 6f 66 20 65 76   the mutex of ev
14faf 65 72 79 20 62 74 72 65 65 20 69 6e 20 74 68 65  ery btree in the
14fb0 20 61 72 72 61 79 2e 20 20 54 68 69 73 20 72 6f   array.  This ro
14fb1 75 74 69 6e 65 20 69 73 0a 2a 2a 20 63 61 6c 6c  utine is.** call
14fb2 65 64 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e  ed at the beginn
14fb3 69 6e 67 20 6f 66 20 73 71 6c 69 74 65 33 56 64  ing of sqlite3Vd
14fb4 62 65 45 78 65 63 28 29 2e 20 20 54 68 65 20 6d  beExec().  The m
14fb5 75 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 65 78  utexes are.** ex
14fb6 69 74 65 64 20 61 74 20 74 68 65 20 65 6e 64 20  ited at the end 
14fb7 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63  of the same func
14fb8 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
14fb9 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
14fba 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
14fbb 72 61 79 45 6e 74 65 72 28 42 74 72 65 65 4d 75  rayEnter(BtreeMu
14fbc 74 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79  texArray *pArray
14fbd 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
14fbe 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d  r(i=0; i<pArray-
14fbf 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20  >nMutex; i++){. 
14fc0 20 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41     Btree *p = pA
14fc1 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b  rray->aBtree[i];
14fc2 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73  .    /* Some bas
14fc3 69 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69  ic sanity checki
14fc4 6e 67 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  ng */.    assert
14fc5 28 20 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79  ( i==0 || pArray
14fc6 2d 3e 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70  ->aBtree[i-1]->p
14fc7 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20  Bt<p->pBt );.   
14fc8 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c 6f 63   assert( !p->loc
14fc9 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f  ked || p->wantTo
14fca 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20 20 20 2f  Lock>0 );..    /
14fcb 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 72 65  * We should alre
14fcc 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20  ady hold a lock 
14fcd 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
14fce 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
14fcf 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
14fd0 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
14fd1 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
14fd2 20 20 20 2f 2a 20 54 68 65 20 42 74 72 65 65 20     /* The Btree 
14fd3 69 73 20 73 68 61 72 61 62 6c 65 20 62 65 63 61  is sharable beca
14fd4 75 73 65 20 6f 6e 6c 79 20 73 68 61 72 61 62 6c  use only sharabl
14fd5 65 20 42 74 72 65 65 73 20 61 72 65 20 65 6e 74  e Btrees are ent
14fd6 65 72 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f  ered.    ** into
14fd7 20 74 68 65 20 61 72 72 61 79 20 69 6e 20 74 68   the array in th
14fd8 65 20 66 69 72 73 74 20 70 6c 61 63 65 2e 20 2a  e first place. *
14fd9 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  /.    assert( p-
14fda 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 0a 20 20  >sharable );..  
14fdb 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b    p->wantToLock+
14fdc 2b 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 6c  +;.    if( !p->l
14fdd 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 6c  ocked ){.      l
14fde 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29  ockBtreeMutex(p)
14fdf 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
14fe0 2a 0a 2a 2a 20 4c 65 61 76 65 20 74 68 65 20 6d  *.** Leave the m
14fe1 75 74 65 78 20 6f 66 20 65 76 65 72 79 20 62 74  utex of every bt
14fe2 72 65 65 20 69 6e 20 74 68 65 20 67 72 6f 75 70  ree in the group
14fe3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
14fe4 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
14fe5 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c  BtreeMutexArrayL
14fe6 65 61 76 65 28 42 74 72 65 65 4d 75 74 65 78 41  eave(BtreeMutexA
14fe7 72 72 61 79 20 2a 70 41 72 72 61 79 29 7b 0a 20  rray *pArray){. 
14fe8 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
14fe9 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75  0; i<pArray->nMu
14fea 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42  tex; i++){.    B
14feb 74 72 65 65 20 2a 70 20 3d 20 70 41 72 72 61 79  tree *p = pArray
14fec 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a 20 20 20  ->aBtree[i];.   
14fed 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73   /* Some basic s
14fee 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a  anity checking *
14fef 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3d  /.    assert( i=
14ff0 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d 3e 61 42  =0 || pArray->aB
14ff1 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70  tree[i-1]->pBt<p
14ff2 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73  ->pBt );.    ass
14ff3 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29  ert( p->locked )
14ff4 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ;.    assert( p-
14ff5 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b  >wantToLock>0 );
14ff6 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75  ..    /* We shou
14ff7 6c 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20  ld already hold 
14ff8 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  a lock on the da
14ff9 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14ffa 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  n */.    assert(
14ffb 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14ffc 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
14ffd 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e  ) );..    p->wan
14ffe 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69  tToLock--;.    i
14fff 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  f( p->wantToLock
15000 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 75 6e 6c  ==0 ){.      unl
15001 6f 63 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29  ockBtreeMutex(p)
15002 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23  ;.    }.  }.}..#
15003 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56  else.SQLITE_PRIV
15004 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
15005 42 74 72 65 65 45 6e 74 65 72 28 42 74 72 65 65  BtreeEnter(Btree
15006 20 2a 70 29 7b 0a 20 20 70 2d 3e 70 42 74 2d 3e   *p){.  p->pBt->
15007 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 7d 0a 53 51  db = p->db;.}.SQ
15008 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
15009 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  d sqlite3BtreeEn
1500a 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a  terAll(sqlite3 *
1500b 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  db){.  int i;.  
1500c 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
1500d 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74  Db; i++){.    Bt
1500e 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62  ree *p = db->aDb
1500f 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
15010 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70   p ){.      p->p
15011 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
15012 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64      }.  }.}.#end
15013 69 66 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f  if /* if SQLITE_
15014 54 48 52 45 41 44 53 41 46 45 20 2a 2f 0a 23 65  THREADSAFE */.#e
15015 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53  ndif /* ifndef S
15016 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
15017 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a  D_CACHE */../***
15018 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
15019 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a  of btmutex.c ***
1501a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1501b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1501c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1501d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1501e 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 63 20 2a  n file btree.c *
1501f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15021 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
15022 2a 20 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a  * 2004 April 6.*
15023 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
15024 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
15025 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
15026 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
15027 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
15028 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
15029 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1502a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1502b 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1502c 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1502d 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1502e 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1502f 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
15030 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
15031 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
15032 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
15033 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
15034 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
15035 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15036 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15037 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
15038 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
15039 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   This file imple
1503a 6d 65 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c  ments a external
1503b 20 28 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61   (disk-based) da
1503c 74 61 62 61 73 65 20 75 73 69 6e 67 20 42 54 72  tabase using BTr
1503d 65 65 73 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20  ees..** See the 
1503e 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f  header comment o
1503f 6e 20 22 62 74 72 65 65 49 6e 74 2e 68 22 20 66  n "btreeInt.h" f
15040 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
15041 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e  formation..** In
15042 63 6c 75 64 69 6e 67 20 61 20 64 65 73 63 72 69  cluding a descri
15043 70 74 69 6f 6e 20 6f 66 20 66 69 6c 65 20 66 6f  ption of file fo
15044 72 6d 61 74 20 61 6e 64 20 61 6e 20 6f 76 65 72  rmat and an over
15045 76 69 65 77 20 6f 66 20 6f 70 65 72 61 74 69 6f  view of operatio
15046 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  n..*/../*.** The
15047 20 68 65 61 64 65 72 20 73 74 72 69 6e 67 20 74   header string t
15048 68 61 74 20 61 70 70 65 61 72 73 20 61 74 20 74  hat appears at t
15049 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
1504a 65 76 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20  every.** SQLite 
1504b 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61  database..*/.sta
1504c 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a  tic const char z
1504d 4d 61 67 69 63 48 65 61 64 65 72 5b 5d 20 3d 20  MagicHeader[] = 
1504e 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44  SQLITE_FILE_HEAD
1504f 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ER;../*.** Set t
15050 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  his global varia
15051 62 6c 65 20 74 6f 20 31 20 74 6f 20 65 6e 61 62  ble to 1 to enab
15052 6c 65 20 74 72 61 63 69 6e 67 20 75 73 69 6e 67  le tracing using
15053 20 74 68 65 20 54 52 41 43 45 0a 2a 2a 20 6d 61   the TRACE.** ma
15054 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e  cro..*/.#if 0.in
15055 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72  t sqlite3BtreeTr
15056 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20  ace=1;  /* True 
15057 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e  to enable tracin
15058 67 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 54 52  g */.# define TR
15059 41 43 45 28 58 29 20 20 69 66 28 73 71 6c 69 74  ACE(X)  if(sqlit
1505a 65 33 42 74 72 65 65 54 72 61 63 65 29 7b 70 72  e3BtreeTrace){pr
1505b 69 6e 74 66 20 58 3b 66 66 6c 75 73 68 28 73 74  intf X;fflush(st
1505c 64 6f 75 74 29 3b 7d 0a 23 65 6c 73 65 0a 23 20  dout);}.#else.# 
1505d 64 65 66 69 6e 65 20 54 52 41 43 45 28 58 29 0a  define TRACE(X).
1505e 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 66 6e 64 65  #endif....#ifnde
1505f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
15060 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
15061 20 41 20 6c 69 73 74 20 6f 66 20 42 74 53 68 61   A list of BtSha
15062 72 65 64 20 6f 62 6a 65 63 74 73 20 74 68 61 74  red objects that
15063 20 61 72 65 20 65 6c 69 67 69 62 6c 65 20 66 6f   are eligible fo
15064 72 20 70 61 72 74 69 63 69 70 61 74 69 6f 6e 0a  r participation.
15065 2a 2a 20 69 6e 20 73 68 61 72 65 64 20 63 61 63  ** in shared cac
15066 68 65 2e 20 20 54 68 69 73 20 76 61 72 69 61 62  he.  This variab
15067 6c 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70  le has file scop
15068 65 20 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20  e during normal 
15069 62 75 69 6c 64 73 2c 0a 2a 2a 20 62 75 74 20 74  builds,.** but t
1506a 68 65 20 74 65 73 74 20 68 61 72 6e 65 73 73 20  he test harness 
1506b 6e 65 65 64 73 20 74 6f 20 61 63 63 65 73 73 20  needs to access 
1506c 69 74 20 73 6f 20 77 65 20 6d 61 6b 65 20 69 74  it so we make it
1506d 20 67 6c 6f 62 61 6c 20 66 6f 72 20 0a 2a 2a 20   global for .** 
1506e 74 65 73 74 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a  test builds..**.
1506f 2a 2a 20 41 63 63 65 73 73 20 74 6f 20 74 68 69  ** Access to thi
15070 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 70 72  s variable is pr
15071 6f 74 65 63 74 65 64 20 62 79 20 53 51 4c 49 54  otected by SQLIT
15072 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
15073 41 53 54 45 52 2e 0a 2a 2f 0a 23 69 66 64 65 66  ASTER..*/.#ifdef
15074 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
15075 49 54 45 5f 50 52 49 56 41 54 45 20 42 74 53 68  ITE_PRIVATE BtSh
15076 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44  ared *SQLITE_WSD
15077 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61   sqlite3SharedCa
15078 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6c  cheList = 0;.#el
15079 73 65 0a 73 74 61 74 69 63 20 42 74 53 68 61 72  se.static BtShar
1507a 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73  ed *SQLITE_WSD s
1507b 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68  qlite3SharedCach
1507c 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6e 64 69  eList = 0;.#endi
1507d 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  f.#endif /* SQLI
1507e 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
1507f 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ACHE */..#ifndef
15080 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
15081 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20  RED_CACHE./*.** 
15082 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c  Enable or disabl
15083 65 20 74 68 65 20 73 68 61 72 65 64 20 70 61 67  e the shared pag
15084 65 72 20 61 6e 64 20 73 63 68 65 6d 61 20 66 65  er and schema fe
15085 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  atures..**.** Th
15086 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 6e  is routine has n
15087 6f 20 65 66 66 65 63 74 20 6f 6e 20 65 78 69 73  o effect on exis
15088 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f  ting database co
15089 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68  nnections..** Th
1508a 65 20 73 68 61 72 65 64 20 63 61 63 68 65 20 73  e shared cache s
1508b 65 74 74 69 6e 67 20 65 66 66 65 63 74 73 20 6f  etting effects o
1508c 6e 6c 79 20 66 75 74 75 72 65 20 63 61 6c 6c 73  nly future calls
1508d 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f   to.** sqlite3_o
1508e 70 65 6e 28 29 2c 20 73 71 6c 69 74 65 33 5f 6f  pen(), sqlite3_o
1508f 70 65 6e 31 36 28 29 2c 20 6f 72 20 73 71 6c 69  pen16(), or sqli
15090 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a  te3_open_v2()..*
15091 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
15092 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
15093 73 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74  shared_cache(int
15094 20 65 6e 61 62 6c 65 29 7b 0a 20 20 73 71 6c 69   enable){.  sqli
15095 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
15096 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c  sharedCacheEnabl
15097 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20 72  ed = enable;.  r
15098 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
15099 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 66  .}.#endif....#if
1509a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1509b 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 2f  SHARED_CACHE.  /
1509c 2a 0a 20 20 2a 2a 20 54 68 65 20 66 75 6e 63 74  *.  ** The funct
1509d 69 6f 6e 73 20 71 75 65 72 79 53 68 61 72 65 64  ions queryShared
1509e 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29  CacheTableLock()
1509f 2c 20 73 65 74 53 68 61 72 65 64 43 61 63 68 65  , setSharedCache
150a0 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 0a 20 20 2a  TableLock(),.  *
150a1 2a 20 61 6e 64 20 63 6c 65 61 72 41 6c 6c 53 68  * and clearAllSh
150a2 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
150a3 63 6b 73 28 29 0a 20 20 2a 2a 20 6d 61 6e 69 70  cks().  ** manip
150a4 75 6c 61 74 65 20 65 6e 74 72 69 65 73 20 69 6e  ulate entries in
150a5 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 4c   the BtShared.pL
150a6 6f 63 6b 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  ock linked list 
150a7 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 20 20  used to store.  
150a8 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  ** shared-cache 
150a9 74 61 62 6c 65 20 6c 65 76 65 6c 20 6c 6f 63 6b  table level lock
150aa 73 2e 20 49 66 20 74 68 65 20 6c 69 62 72 61 72  s. If the librar
150ab 79 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69  y is compiled wi
150ac 74 68 20 74 68 65 0a 20 20 2a 2a 20 73 68 61 72  th the.  ** shar
150ad 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65  ed-cache feature
150ae 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20   disabled, then 
150af 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 65 76  there is only ev
150b0 65 72 20 6f 6e 65 20 75 73 65 72 0a 20 20 2a 2a  er one user.  **
150b1 20 6f 66 20 65 61 63 68 20 42 74 53 68 61 72 65   of each BtShare
150b2 64 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  d structure and 
150b3 73 6f 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20  so this locking 
150b4 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79  is not necessary
150b5 2e 20 0a 20 20 2a 2a 20 53 6f 20 64 65 66 69 6e  . .  ** So defin
150b6 65 20 74 68 65 20 6c 6f 63 6b 20 72 65 6c 61 74  e the lock relat
150b7 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20  ed functions as 
150b8 6e 6f 2d 6f 70 73 2e 0a 20 20 2a 2f 0a 20 20 23  no-ops..  */.  #
150b9 64 65 66 69 6e 65 20 71 75 65 72 79 53 68 61 72  define queryShar
150ba 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
150bb 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f  (a,b,c) SQLITE_O
150bc 4b 0a 20 20 23 64 65 66 69 6e 65 20 73 65 74 53  K.  #define setS
150bd 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
150be 6f 63 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54  ock(a,b,c) SQLIT
150bf 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 63  E_OK.  #define c
150c0 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63  learAllSharedCac
150c1 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 61 29 0a  heTableLocks(a).
150c2 20 20 23 64 65 66 69 6e 65 20 64 6f 77 6e 67 72    #define downgr
150c3 61 64 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68  adeAllSharedCach
150c4 65 54 61 62 6c 65 4c 6f 63 6b 73 28 61 29 0a 20  eTableLocks(a). 
150c5 20 23 64 65 66 69 6e 65 20 68 61 73 53 68 61 72   #define hasShar
150c6 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
150c7 28 61 2c 62 2c 63 2c 64 29 20 31 0a 20 20 23 64  (a,b,c,d) 1.  #d
150c8 65 66 69 6e 65 20 68 61 73 52 65 61 64 43 6f 6e  efine hasReadCon
150c9 66 6c 69 63 74 73 28 61 2c 20 62 29 20 30 0a 23  flicts(a, b) 0.#
150ca 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
150cb 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
150cc 44 5f 43 41 43 48 45 0a 0a 23 69 66 64 65 66 20  D_CACHE..#ifdef 
150cd 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
150ce 2a 2a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  **** This functi
150cf 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  on is only used 
150d0 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73  as part of an as
150d1 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
150d2 2e 20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63  . ***.**.** Chec
150d3 6b 20 74 6f 20 73 65 65 20 69 66 20 70 42 74 72  k to see if pBtr
150d4 65 65 20 68 6f 6c 64 73 20 74 68 65 20 72 65 71  ee holds the req
150d5 75 69 72 65 64 20 6c 6f 63 6b 73 20 74 6f 20 72  uired locks to r
150d6 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 6f 20  ead or write to 
150d7 74 68 65 20 0a 2a 2a 20 74 61 62 6c 65 20 77 69  the .** table wi
150d8 74 68 20 72 6f 6f 74 20 70 61 67 65 20 69 52 6f  th root page iRo
150d9 6f 74 2e 20 20 20 52 65 74 75 72 6e 20 31 20 69  ot.   Return 1 i
150da 66 20 69 74 20 64 6f 65 73 20 61 6e 64 20 30 20  f it does and 0 
150db 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f  if not..**.** Fo
150dc 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20  r example, when 
150dd 77 72 69 74 69 6e 67 20 74 6f 20 61 20 74 61 62  writing to a tab
150de 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67  le with root-pag
150df 65 20 69 52 6f 6f 74 20 76 69 61 20 0a 2a 2a 20  e iRoot via .** 
150e0 42 74 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  Btree connection
150e1 20 70 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20   pBtree:.**.**  
150e2 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61    assert( hasSha
150e3 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
150e4 6b 28 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 2c  k(pBtree, iRoot,
150e5 20 30 2c 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20   0, WRITE_LOCK) 
150e6 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 72  );.**.** When wr
150e7 69 74 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65  iting to an inde
150e8 78 20 74 68 61 74 20 72 65 73 69 64 65 73 20 69  x that resides i
150e9 6e 20 61 20 73 68 61 72 61 62 6c 65 20 64 61 74  n a sharable dat
150ea 61 62 61 73 65 2c 20 74 68 65 20 0a 2a 2a 20 63  abase, the .** c
150eb 61 6c 6c 65 72 20 73 68 6f 75 6c 64 20 68 61 76  aller should hav
150ec 65 20 66 69 72 73 74 20 6f 62 74 61 69 6e 65 64  e first obtained
150ed 20 61 20 6c 6f 63 6b 20 73 70 65 63 69 66 79 69   a lock specifyi
150ee 6e 67 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  ng the root page
150ef 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65   of.** the corre
150f0 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 2e 20  sponding table. 
150f1 54 68 69 73 20 6d 61 6b 65 73 20 74 68 69 6e 67  This makes thing
150f2 73 20 61 20 62 69 74 20 6d 6f 72 65 20 63 6f 6d  s a bit more com
150f3 70 6c 69 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20  plicated,.** as 
150f4 74 68 69 73 20 6d 6f 64 75 6c 65 20 74 72 65 61  this module trea
150f5 74 73 20 65 61 63 68 20 74 61 62 6c 65 20 61 73  ts each table as
150f6 20 61 20 73 65 70 61 72 61 74 65 20 73 74 72 75   a separate stru
150f7 63 74 75 72 65 2e 20 54 6f 20 64 65 74 65 72 6d  cture. To determ
150f8 69 6e 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65  ine.** the table
150f9 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
150fa 6f 20 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e  o the index bein
150fb 67 20 77 72 69 74 74 65 6e 2c 20 74 68 69 73 0a  g written, this.
150fc 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20  ** function has 
150fd 74 6f 20 73 65 61 72 63 68 20 74 68 72 6f 75 67  to search throug
150fe 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  h the database s
150ff 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73  chema..**.** Ins
15100 74 65 61 64 20 6f 66 20 61 20 6c 6f 63 6b 20 6f  tead of a lock o
15101 6e 20 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65  n the table/inde
15102 78 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65  x rooted at page
15103 20 69 52 6f 6f 74 2c 20 74 68 65 20 63 61 6c 6c   iRoot, the call
15104 65 72 20 6d 61 79 0a 2a 2a 20 68 6f 6c 64 20 61  er may.** hold a
15105 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74   write-lock on t
15106 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 20  he schema table 
15107 28 72 6f 6f 74 20 70 61 67 65 20 31 29 2e 20 54  (root page 1). T
15108 68 69 73 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 61  his is also.** a
15109 63 63 65 70 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  cceptable..*/.st
1510a 61 74 69 63 20 69 6e 74 20 68 61 73 53 68 61 72  atic int hasShar
1510b 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
1510c 28 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65  (.  Btree *pBtre
1510d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61  e,         /* Ha
1510e 6e 64 6c 65 20 74 68 61 74 20 6d 75 73 74 20 68  ndle that must h
1510f 6f 6c 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67  old lock */.  Pg
15110 6e 6f 20 69 52 6f 6f 74 2c 20 20 20 20 20 20 20  no iRoot,       
15111 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
15112 65 20 6f 66 20 62 2d 74 72 65 65 20 2a 2f 0a 20  e of b-tree */. 
15113 20 69 6e 74 20 69 73 49 6e 64 65 78 2c 20 20 20   int isIndex,   
15114 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
15115 69 66 20 69 52 6f 6f 74 20 69 73 20 74 68 65 20  if iRoot is the 
15116 72 6f 6f 74 20 6f 66 20 61 6e 20 69 6e 64 65 78  root of an index
15117 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74   b-tree */.  int
15118 20 65 4c 6f 63 6b 54 79 70 65 20 20 20 20 20 20   eLockType      
15119 20 20 20 20 2f 2a 20 52 65 71 75 69 72 65 64 20      /* Required 
1511a 6c 6f 63 6b 20 74 79 70 65 20 28 52 45 41 44 5f  lock type (READ_
1511b 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f  LOCK or WRITE_LO
1511c 43 4b 29 20 2a 2f 0a 29 7b 0a 20 20 53 63 68 65  CK) */.){.  Sche
1511d 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53  ma *pSchema = (S
1511e 63 68 65 6d 61 20 2a 29 70 42 74 72 65 65 2d 3e  chema *)pBtree->
1511f 70 42 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20  pBt->pSchema;.  
15120 50 67 6e 6f 20 69 54 61 62 20 3d 20 30 3b 0a 20  Pgno iTab = 0;. 
15121 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a   BtLock *pLock;.
15122 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61  .  /* If this da
15123 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 73 68  tabase is not sh
15124 61 72 65 61 62 6c 65 2c 20 6f 72 20 69 66 20 74  areable, or if t
15125 68 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61  he client is rea
15126 64 69 6e 67 0a 20 20 2a 2a 20 61 6e 64 20 68 61  ding.  ** and ha
15127 73 20 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d  s the read-uncom
15128 6d 69 74 74 65 64 20 66 6c 61 67 20 73 65 74 2c  mitted flag set,
15129 20 74 68 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73   then no lock is
1512a 20 72 65 71 75 69 72 65 64 2e 20 0a 20 20 2a 2a   required. .  **
1512b 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 6d 6d   Return true imm
1512c 65 64 69 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20  ediately..  */. 
1512d 20 69 66 28 20 28 70 42 74 72 65 65 2d 3e 73 68   if( (pBtree->sh
1512e 61 72 61 62 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c  arable==0).   ||
1512f 20 28 65 4c 6f 63 6b 54 79 70 65 3d 3d 52 45 41   (eLockType==REA
15130 44 5f 4c 4f 43 4b 20 26 26 20 28 70 42 74 72 65  D_LOCK && (pBtre
15131 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  e->db->flags & S
15132 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d  QLITE_ReadUncomm
15133 69 74 74 65 64 29 29 0a 20 20 29 7b 0a 20 20 20  itted)).  ){.   
15134 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a   return 1;.  }..
15135 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6c 69 65    /* If the clie
15136 6e 74 20 69 73 20 72 65 61 64 69 6e 67 20 20 6f  nt is reading  o
15137 72 20 77 72 69 74 69 6e 67 20 61 6e 20 69 6e 64  r writing an ind
15138 65 78 20 61 6e 64 20 74 68 65 20 73 63 68 65 6d  ex and the schem
15139 61 20 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 6c 6f  a is.  ** not lo
1513a 61 64 65 64 2c 20 74 68 65 6e 20 69 74 20 69 73  aded, then it is
1513b 20 74 6f 6f 20 64 69 66 66 69 63 75 6c 74 20 74   too difficult t
1513c 6f 20 61 63 74 75 61 6c 6c 79 20 63 68 65 63 6b  o actually check
1513d 20 74 6f 20 73 65 65 20 69 66 0a 20 20 2a 2a 20   to see if.  ** 
1513e 74 68 65 20 63 6f 72 72 65 63 74 20 6c 6f 63 6b  the correct lock
1513f 73 20 61 72 65 20 68 65 6c 64 2e 20 20 53 6f 20  s are held.  So 
15140 64 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 2d 20  do not bother - 
15141 6a 75 73 74 20 72 65 74 75 72 6e 20 74 72 75 65  just return true
15142 2e 0a 20 20 2a 2a 20 54 68 69 73 20 63 61 73 65  ..  ** This case
15143 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6d 65 20 75   does not come u
15144 70 20 76 65 72 79 20 6f 66 74 65 6e 20 61 6e 79  p very often any
15145 68 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  how..  */.  if( 
15146 69 73 49 6e 64 65 78 20 26 26 20 28 21 70 53 63  isIndex && (!pSc
15147 68 65 6d 61 20 7c 7c 20 28 70 53 63 68 65 6d 61  hema || (pSchema
15148 2d 3e 66 6c 61 67 73 26 44 42 5f 53 63 68 65 6d  ->flags&DB_Schem
15149 61 4c 6f 61 64 65 64 29 3d 3d 30 29 20 29 7b 0a  aLoaded)==0) ){.
1514a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1514b 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  }..  /* Figure o
1514c 75 74 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65  ut the root-page
1514d 20 74 68 61 74 20 74 68 65 20 6c 6f 63 6b 20 73   that the lock s
1514e 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 6f 6e  hould be held on
1514f 2e 20 46 6f 72 20 74 61 62 6c 65 0a 20 20 2a 2a  . For table.  **
15150 20 62 2d 74 72 65 65 73 2c 20 74 68 69 73 20 69   b-trees, this i
15151 73 20 6a 75 73 74 20 74 68 65 20 72 6f 6f 74 20  s just the root 
15152 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72  page of the b-tr
15153 65 65 20 62 65 69 6e 67 20 72 65 61 64 20 6f 72  ee being read or
15154 0a 20 20 2a 2a 20 77 72 69 74 74 65 6e 2e 20 46  .  ** written. F
15155 6f 72 20 69 6e 64 65 78 20 62 2d 74 72 65 65 73  or index b-trees
15156 2c 20 69 74 20 69 73 20 74 68 65 20 72 6f 6f 74  , it is the root
15157 20 70 61 67 65 20 6f 66 20 74 68 65 20 61 73 73   page of the ass
15158 6f 63 69 61 74 65 64 0a 20 20 2a 2a 20 74 61 62  ociated.  ** tab
15159 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  le.  */.  if( is
1515a 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 48 61 73  Index ){.    Has
1515b 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 66 6f  hElem *p;.    fo
1515c 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(p=sqliteHashFi
1515d 72 73 74 28 26 70 53 63 68 65 6d 61 2d 3e 69 64  rst(&pSchema->id
1515e 78 48 61 73 68 29 3b 20 70 3b 20 70 3d 73 71 6c  xHash); p; p=sql
1515f 69 74 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b  iteHashNext(p)){
15160 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49  .      Index *pI
15161 64 78 20 3d 20 28 49 6e 64 65 78 20 2a 29 73 71  dx = (Index *)sq
15162 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29 3b  liteHashData(p);
15163 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d  .      if( pIdx-
15164 3e 74 6e 75 6d 3d 3d 28 69 6e 74 29 69 52 6f 6f  >tnum==(int)iRoo
15165 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 54 61  t ){.        iTa
15166 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  b = pIdx->pTable
15167 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20 7d 0a  ->tnum;.      }.
15168 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
15169 20 20 20 69 54 61 62 20 3d 20 69 52 6f 6f 74 3b     iTab = iRoot;
1516a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 61 72 63  .  }..  /* Searc
1516b 68 20 66 6f 72 20 74 68 65 20 72 65 71 75 69 72  h for the requir
1516c 65 64 20 6c 6f 63 6b 2e 20 45 69 74 68 65 72 20  ed lock. Either 
1516d 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
1516e 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2c 20  root-page iTab, 
1516f 61 20 0a 20 20 2a 2a 20 77 72 69 74 65 2d 6c 6f  a .  ** write-lo
15170 63 6b 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61  ck on the schema
15171 20 74 61 62 6c 65 2c 20 6f 72 20 28 69 66 20 74   table, or (if t
15172 68 65 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61  he client is rea
15173 64 69 6e 67 29 20 61 0a 20 20 2a 2a 20 72 65 61  ding) a.  ** rea
15174 64 2d 6c 6f 63 6b 20 6f 6e 20 69 54 61 62 20 77  d-lock on iTab w
15175 69 6c 6c 20 73 75 66 66 69 63 65 2e 20 52 65 74  ill suffice. Ret
15176 75 72 6e 20 31 20 69 66 20 61 6e 79 20 6f 66 20  urn 1 if any of 
15177 74 68 65 73 65 20 61 72 65 20 66 6f 75 6e 64 2e  these are found.
15178 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 6f 63 6b    */.  for(pLock
15179 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 4c  =pBtree->pBt->pL
1517a 6f 63 6b 3b 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63  ock; pLock; pLoc
1517b 6b 3d 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b  k=pLock->pNext){
1517c 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e  .    if( pLock->
1517d 70 42 74 72 65 65 3d 3d 70 42 74 72 65 65 20 0a  pBtree==pBtree .
1517e 20 20 20 20 20 26 26 20 28 70 4c 6f 63 6b 2d 3e       && (pLock->
1517f 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 7c 7c 20  iTable==iTab || 
15180 28 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d 3d 57  (pLock->eLock==W
15181 52 49 54 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f  RITE_LOCK && pLo
15182 63 6b 2d 3e 69 54 61 62 6c 65 3d 3d 31 29 29 0a  ck->iTable==1)).
15183 20 20 20 20 20 26 26 20 70 4c 6f 63 6b 2d 3e 65       && pLock->e
15184 4c 6f 63 6b 3e 3d 65 4c 6f 63 6b 54 79 70 65 20  Lock>=eLockType 
15185 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65  .    ){.      re
15186 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
15187 7d 0a 0a 20 20 2f 2a 20 46 61 69 6c 65 64 20 74  }..  /* Failed t
15188 6f 20 66 69 6e 64 20 74 68 65 20 72 65 71 75 69  o find the requi
15189 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 72  red lock. */.  r
1518a 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69  eturn 0;.}.#endi
1518b 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55  f /* SQLITE_DEBU
1518c 47 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c  G */..#ifdef SQL
1518d 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a  ITE_DEBUG./*.***
1518e 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1518f 6d 61 79 20 62 65 20 75 73 65 64 20 61 73 20 70  may be used as p
15190 61 72 74 20 6f 66 20 61 73 73 65 72 74 28 29 20  art of assert() 
15191 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e  statements only.
15192 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52 65 74 75   ****.**.** Retu
15193 72 6e 20 74 72 75 65 20 69 66 20 69 74 20 77 6f  rn true if it wo
15194 75 6c 64 20 62 65 20 69 6c 6c 65 67 61 6c 20 66  uld be illegal f
15195 6f 72 20 70 42 74 72 65 65 20 74 6f 20 77 72 69  or pBtree to wri
15196 74 65 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 74  te into the.** t
15197 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 72 6f  able or index ro
15198 6f 74 65 64 20 61 74 20 69 52 6f 6f 74 20 62 65  oted at iRoot be
15199 63 61 75 73 65 20 6f 74 68 65 72 20 73 68 61 72  cause other shar
1519a 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61  ed connections a
1519b 72 65 0a 2a 2a 20 73 69 6d 75 6c 74 61 6e 65 6f  re.** simultaneo
1519c 75 73 6c 79 20 72 65 61 64 69 6e 67 20 74 68 61  usly reading tha
1519d 74 20 73 61 6d 65 20 74 61 62 6c 65 20 6f 72 20  t same table or 
1519e 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  index..**.** It 
1519f 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70  is illegal for p
151a0 42 74 72 65 65 20 74 6f 20 77 72 69 74 65 20 69  Btree to write i
151a1 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 42 74 72  f some other Btr
151a2 65 65 20 6f 62 6a 65 63 74 20 74 68 61 74 0a 2a  ee object that.*
151a3 2a 20 73 68 61 72 65 73 20 74 68 65 20 73 61 6d  * shares the sam
151a4 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63  e BtShared objec
151a5 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72  t is currently r
151a6 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e  eading or writin
151a7 67 0a 2a 2a 20 74 68 65 20 69 52 6f 6f 74 20 74  g.** the iRoot t
151a8 61 62 6c 65 2e 20 20 45 78 63 65 70 74 2c 20 69  able.  Except, i
151a9 66 20 74 68 65 20 6f 74 68 65 72 20 42 74 72 65  f the other Btre
151aa 65 20 6f 62 6a 65 63 74 20 68 61 73 20 74 68 65  e object has the
151ab 0a 2a 2a 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69  .** read-uncommi
151ac 74 74 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74  tted flag set, t
151ad 68 65 6e 20 69 74 20 69 73 20 4f 4b 20 66 6f 72  hen it is OK for
151ae 20 74 68 65 20 6f 74 68 65 72 20 6f 62 6a 65 63   the other objec
151af 74 20 74 6f 0a 2a 2a 20 68 61 76 65 20 61 20 72  t to.** have a r
151b0 65 61 64 20 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a  ead cursor..**.*
151b1 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 62  * For example, b
151b2 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f  efore writing to
151b3 20 61 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65   any part of the
151b4 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 0a   table or index.
151b5 2a 2a 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67  ** rooted at pag
151b6 65 20 69 52 6f 6f 74 2c 20 6f 6e 65 20 73 68 6f  e iRoot, one sho
151b7 75 6c 64 20 63 61 6c 6c 3a 0a 2a 2a 0a 2a 2a 20  uld call:.**.** 
151b8 20 20 20 61 73 73 65 72 74 28 20 21 68 61 73 52     assert( !hasR
151b9 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 42 74  eadConflicts(pBt
151ba 72 65 65 2c 20 69 52 6f 6f 74 29 20 29 3b 0a 2a  ree, iRoot) );.*
151bb 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73  /.static int has
151bc 52 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 42 74  ReadConflicts(Bt
151bd 72 65 65 20 2a 70 42 74 72 65 65 2c 20 50 67 6e  ree *pBtree, Pgn
151be 6f 20 69 52 6f 6f 74 29 7b 0a 20 20 42 74 43 75  o iRoot){.  BtCu
151bf 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70  rsor *p;.  for(p
151c0 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43  =pBtree->pBt->pC
151c1 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70  ursor; p; p=p->p
151c2 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70  Next){.    if( p
151c3 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f  ->pgnoRoot==iRoo
151c4 74 20 0a 20 20 20 20 20 26 26 20 70 2d 3e 70 42  t .     && p->pB
151c5 74 72 65 65 21 3d 70 42 74 72 65 65 0a 20 20 20  tree!=pBtree.   
151c6 20 20 26 26 20 30 3d 3d 28 70 2d 3e 70 42 74 72    && 0==(p->pBtr
151c7 65 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  ee->db->flags & 
151c8 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d  SQLITE_ReadUncom
151c9 6d 69 74 74 65 64 29 0a 20 20 20 20 29 7b 0a 20  mitted).    ){. 
151ca 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
151cb 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
151cc 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 20  n 0;.}.#endif   
151cd 20 2f 2a 20 23 69 66 64 65 66 20 53 51 4c 49 54   /* #ifdef SQLIT
151ce 45 5f 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a  E_DEBUG */../*.*
151cf 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65 20 69  * Query to see i
151d0 66 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70  f Btree handle p
151d1 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f   may obtain a lo
151d2 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b  ck of type eLock
151d3 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20   .** (READ_LOCK 
151d4 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f  or WRITE_LOCK) o
151d5 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68  n the table with
151d6 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e   root-page iTab.
151d7 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54   Return.** SQLIT
151d8 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b  E_OK if the lock
151d9 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64   may be obtained
151da 20 28 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20   (by calling.** 
151db 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61  setSharedCacheTa
151dc 62 6c 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 20 53  bleLock()), or S
151dd 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20  QLITE_LOCKED if 
151de 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  not..*/.static i
151df 6e 74 20 71 75 65 72 79 53 68 61 72 65 64 43 61  nt querySharedCa
151e0 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72  cheTableLock(Btr
151e1 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62  ee *p, Pgno iTab
151e2 2c 20 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42  , u8 eLock){.  B
151e3 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
151e4 2d 3e 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20  ->pBt;.  BtLock 
151e5 2a 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72  *pIter;..  asser
151e6 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
151e7 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a  oldsMutex(p) );.
151e8 20 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d    assert( eLock=
151e9 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c  =READ_LOCK || eL
151ea 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20  ock==WRITE_LOCK 
151eb 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
151ec 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  db!=0 );.  asser
151ed 74 28 20 21 28 70 2d 3e 64 62 2d 3e 66 6c 61 67  t( !(p->db->flag
151ee 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63  s&SQLITE_ReadUnc
151ef 6f 6d 6d 69 74 74 65 64 29 7c 7c 65 4c 6f 63 6b  ommitted)||eLock
151f0 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 7c 7c 69 54  ==WRITE_LOCK||iT
151f1 61 62 3d 3d 31 20 29 3b 0a 20 20 0a 20 20 2f 2a  ab==1 );.  .  /*
151f2 20 49 66 20 72 65 71 75 65 73 74 69 6e 67 20 61   If requesting a
151f3 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 68 65   write-lock, the
151f4 6e 20 74 68 65 20 42 74 72 65 65 20 6d 75 73 74  n the Btree must
151f5 20 68 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72   have an open wr
151f6 69 74 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  ite.  ** transac
151f7 74 69 6f 6e 20 6f 6e 20 74 68 69 73 20 66 69 6c  tion on this fil
151f8 65 2e 20 41 6e 64 2c 20 6f 62 76 69 6f 75 73 6c  e. And, obviousl
151f9 79 2c 20 66 6f 72 20 74 68 69 73 20 74 6f 20 62  y, for this to b
151fa 65 20 73 6f 20 74 68 65 72 65 20 0a 20 20 2a 2a  e so there .  **
151fb 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e   must be an open
151fc 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
151fd 6f 6e 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 69  on on the file i
151fe 74 73 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 61 73  tself..  */.  as
151ff 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41  sert( eLock==REA
15200 44 5f 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d 70 42  D_LOCK || (p==pB
15201 74 2d 3e 70 57 72 69 74 65 72 20 26 26 20 70 2d  t->pWriter && p-
15202 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f  >inTrans==TRANS_
15203 57 52 49 54 45 29 20 29 3b 0a 20 20 61 73 73 65  WRITE) );.  asse
15204 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f  rt( eLock==READ_
15205 4c 4f 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 6e 54  LOCK || pBt->inT
15206 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e  ransaction==TRAN
15207 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 0a 20 20  S_WRITE );.  .  
15208 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  /* This routine 
15209 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68  is a no-op if th
1520a 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 69  e shared-cache i
1520b 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f  s not enabled */
1520c 0a 20 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61  .  if( !p->shara
1520d 62 6c 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ble ){.    retur
1520e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1520f 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f  ..  /* If some o
15210 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ther connection 
15211 69 73 20 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78  is holding an ex
15212 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68  clusive lock, th
15213 65 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64  e.  ** requested
15214 20 6c 6f 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65   lock may not be
15215 20 6f 62 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a   obtained..  */.
15216 20 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74    if( pBt->pWrit
15217 65 72 21 3d 70 20 26 26 20 70 42 74 2d 3e 69 73  er!=p && pBt->is
15218 45 78 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20  Exclusive ){.   
15219 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
1521a 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c  onBlocked(p->db,
1521b 20 70 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64   pBt->pWriter->d
1521c 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  b);.    return S
1521d 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41  QLITE_LOCKED_SHA
1521e 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a 0a 20  REDCACHE;.  }.. 
1521f 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e   for(pIter=pBt->
15220 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49  pLock; pIter; pI
15221 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74  ter=pIter->pNext
15222 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f  ){.    /* The co
15223 6e 64 69 74 69 6f 6e 20 28 70 49 74 65 72 2d 3e  ndition (pIter->
15224 65 4c 6f 63 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e  eLock!=eLock) in
15225 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
15226 66 28 2e 2e 2e 29 20 0a 20 20 20 20 2a 2a 20 73  f(...) .    ** s
15227 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69  tatement is a si
15228 6d 70 6c 69 66 69 63 61 74 69 6f 6e 20 6f 66 3a  mplification of:
15229 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
1522a 20 28 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c   (eLock==WRITE_L
1522b 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c  OCK || pIter->eL
1522c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 29  ock==WRITE_LOCK)
1522d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 73  .    **.    ** s
1522e 69 6e 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61  ince we know tha
1522f 74 20 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54  t if eLock==WRIT
15230 45 5f 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20  E_LOCK, then no 
15231 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  other connection
15232 0a 20 20 20 20 2a 2a 20 6d 61 79 20 68 6f 6c 64  .    ** may hold
15233 20 61 20 57 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e   a WRITE_LOCK on
15234 20 61 6e 79 20 74 61 62 6c 65 20 69 6e 20 74 68   any table in th
15235 69 73 20 66 69 6c 65 20 28 73 69 6e 63 65 20 74  is file (since t
15236 68 65 72 65 20 63 61 6e 0a 20 20 20 20 2a 2a 20  here can.    ** 
15237 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65  only be a single
15238 20 77 72 69 74 65 72 29 2e 0a 20 20 20 20 2a 2f   writer)..    */
15239 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 74  .    assert( pIt
1523a 65 72 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f  er->eLock==READ_
1523b 4c 4f 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65  LOCK || pIter->e
1523c 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b  Lock==WRITE_LOCK
1523d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1523e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b  eLock==READ_LOCK
1523f 20 7c 7c 20 70 49 74 65 72 2d 3e 70 42 74 72 65   || pIter->pBtre
15240 65 3d 3d 70 20 7c 7c 20 70 49 74 65 72 2d 3e 65  e==p || pIter->e
15241 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29  Lock==READ_LOCK)
15242 3b 0a 20 20 20 20 69 66 28 20 70 49 74 65 72 2d  ;.    if( pIter-
15243 3e 70 42 74 72 65 65 21 3d 70 20 26 26 20 70 49  >pBtree!=p && pI
15244 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61  ter->iTable==iTa
15245 62 20 26 26 20 70 49 74 65 72 2d 3e 65 4c 6f 63  b && pIter->eLoc
15246 6b 21 3d 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20  k!=eLock ){.    
15247 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74    sqlite3Connect
15248 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62  ionBlocked(p->db
15249 2c 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d  , pIter->pBtree-
1524a 3e 64 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20  >db);.      if( 
1524b 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43  eLock==WRITE_LOC
1524c 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  K ){.        ass
1524d 65 72 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72  ert( p==pBt->pWr
1524e 69 74 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20  iter );.        
1524f 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d  pBt->isPending =
15250 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
15251 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15252 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43  LOCKED_SHAREDCAC
15253 48 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  HE;.    }.  }.  
15254 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
15255 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53  ;.}.#endif /* !S
15256 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
15257 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e  D_CACHE */..#ifn
15258 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
15259 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a  SHARED_CACHE./*.
1525a 2a 2a 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e  ** Add a lock on
1525b 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20   the table with 
1525c 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65  root-page iTable
1525d 20 74 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62   to the shared-b
1525e 74 72 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20  tree used.** by 
1525f 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20  Btree handle p. 
15260 50 61 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20  Parameter eLock 
15261 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 52  must be either R
15262 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20  EAD_LOCK or .** 
15263 57 52 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  WRITE_LOCK..**.*
15264 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
15265 61 73 73 75 6d 65 73 20 74 68 65 20 66 6f 6c 6c  assumes the foll
15266 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 28  owing:.**.**   (
15267 61 29 20 54 68 65 20 73 70 65 63 69 66 69 65 64  a) The specified
15268 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 70 20   Btree object p 
15269 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20  is connected to 
1526a 61 20 73 68 61 72 61 62 6c 65 0a 2a 2a 20 20 20  a sharable.**   
1526b 20 20 20 20 64 61 74 61 62 61 73 65 20 28 6f 6e      database (on
1526c 65 20 77 69 74 68 20 74 68 65 20 42 74 53 68 61  e with the BtSha
1526d 72 65 64 2e 73 68 61 72 61 62 6c 65 20 66 6c 61  red.sharable fla
1526e 67 20 73 65 74 29 2c 20 61 6e 64 0a 2a 2a 0a 2a  g set), and.**.*
1526f 2a 20 20 20 28 62 29 20 4e 6f 20 6f 74 68 65 72  *   (b) No other
15270 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 20 68   Btree objects h
15271 6f 6c 64 20 61 20 6c 6f 63 6b 20 74 68 61 74 20  old a lock that 
15272 63 6f 6e 66 6c 69 63 74 73 0a 2a 2a 20 20 20 20  conflicts.**    
15273 20 20 20 77 69 74 68 20 74 68 65 20 72 65 71 75     with the requ
15274 65 73 74 65 64 20 6c 6f 63 6b 20 28 69 2e 65 2e  ested lock (i.e.
15275 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68   querySharedCach
15276 65 54 61 62 6c 65 4c 6f 63 6b 28 29 20 68 61 73  eTableLock() has
15277 0a 2a 2a 20 20 20 20 20 20 20 61 6c 72 65 61 64  .**       alread
15278 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 61 6e  y been called an
15279 64 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54  d returned SQLIT
1527a 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  E_OK)..**.** SQL
1527b 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
1527c 65 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69  ed if the lock i
1527d 73 20 61 64 64 65 64 20 73 75 63 63 65 73 73 66  s added successf
1527e 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 4e 4f 4d  ully. SQLITE_NOM
1527f 45 4d 20 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  EM .** is return
15280 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 61  ed if a malloc a
15281 74 74 65 6d 70 74 20 66 61 69 6c 73 2e 0a 2a 2f  ttempt fails..*/
15282 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 53  .static int setS
15283 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
15284 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67  ock(Btree *p, Pg
15285 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65 4c  no iTable, u8 eL
15286 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64  ock){.  BtShared
15287 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
15288 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20    BtLock *pLock 
15289 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70  = 0;.  BtLock *p
1528a 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  Iter;..  assert(
1528b 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
1528c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20  dsMutex(p) );.  
1528d 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52  assert( eLock==R
1528e 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63  EAD_LOCK || eLoc
1528f 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b  k==WRITE_LOCK );
15290 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62  .  assert( p->db
15291 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 63  !=0 );..  /* A c
15292 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 74  onnection with t
15293 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74  he read-uncommit
15294 74 65 64 20 66 6c 61 67 20 73 65 74 20 77 69 6c  ted flag set wil
15295 6c 20 6e 65 76 65 72 20 74 72 79 20 74 6f 0a 20  l never try to. 
15296 20 2a 2a 20 6f 62 74 61 69 6e 20 61 20 72 65 61   ** obtain a rea
15297 64 2d 6c 6f 63 6b 20 75 73 69 6e 67 20 74 68 69  d-lock using thi
15298 73 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20  s function. The 
15299 6f 6e 6c 79 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  only read-lock o
1529a 62 74 61 69 6e 65 64 0a 20 20 2a 2a 20 62 79 20  btained.  ** by 
1529b 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20  a connection in 
1529c 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64  read-uncommitted
1529d 20 6d 6f 64 65 20 69 73 20 6f 6e 20 74 68 65 20   mode is on the 
1529e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 0a 20  sqlite_master . 
1529f 20 2a 2a 20 74 61 62 6c 65 2c 20 61 6e 64 20 74   ** table, and t
152a0 68 61 74 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61  hat lock is obta
152a1 69 6e 65 64 20 69 6e 20 42 74 72 65 65 42 65 67  ined in BtreeBeg
152a2 69 6e 54 72 61 6e 73 28 29 2e 20 20 2a 2f 0a 20  inTrans().  */. 
152a3 20 61 73 73 65 72 74 28 20 30 3d 3d 28 70 2d 3e   assert( 0==(p->
152a4 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
152a5 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64  _ReadUncommitted
152a6 29 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54  ) || eLock==WRIT
152a7 45 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20  E_LOCK );..  /* 
152a8 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68  This function sh
152a9 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c  ould only be cal
152aa 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 61 62 6c  led on a sharabl
152ab 65 20 62 2d 74 72 65 65 20 61 66 74 65 72 20 69  e b-tree after i
152ac 74 20 0a 20 20 2a 2a 20 68 61 73 20 62 65 65 6e  t .  ** has been
152ad 20 64 65 74 65 72 6d 69 6e 65 64 20 74 68 61 74   determined that
152ae 20 6e 6f 20 6f 74 68 65 72 20 62 2d 74 72 65 65   no other b-tree
152af 20 68 6f 6c 64 73 20 61 20 63 6f 6e 66 6c 69 63   holds a conflic
152b0 74 69 6e 67 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20  ting lock.  */. 
152b1 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72   assert( p->shar
152b2 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74  able );.  assert
152b3 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65  ( SQLITE_OK==que
152b4 72 79 53 68 61 72 65 64 43 61 63 68 65 54 61 62  rySharedCacheTab
152b5 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65  leLock(p, iTable
152b6 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f  , eLock) );..  /
152b7 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 20 74  * First search t
152b8 68 65 20 6c 69 73 74 20 66 6f 72 20 61 6e 20 65  he list for an e
152b9 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20 6f 6e 20  xisting lock on 
152ba 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20  this table. */. 
152bb 20 66 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e   for(pIter=pBt->
152bc 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49  pLock; pIter; pI
152bd 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74  ter=pIter->pNext
152be 29 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 72  ){.    if( pIter
152bf 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65  ->iTable==iTable
152c0 20 26 26 20 70 49 74 65 72 2d 3e 70 42 74 72 65   && pIter->pBtre
152c1 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 4c  e==p ){.      pL
152c2 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a 20 20 20  ock = pIter;.   
152c3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
152c4 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
152c5 20 61 62 6f 76 65 20 73 65 61 72 63 68 20 64 69   above search di
152c6 64 20 6e 6f 74 20 66 69 6e 64 20 61 20 42 74 4c  d not find a BtL
152c7 6f 63 6b 20 73 74 72 75 63 74 20 61 73 73 6f 63  ock struct assoc
152c8 69 61 74 69 6e 67 20 42 74 72 65 65 20 70 0a 20  iating Btree p. 
152c9 20 2a 2a 20 77 69 74 68 20 74 61 62 6c 65 20 69   ** with table i
152ca 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 65 20  Table, allocate 
152cb 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20  one and link it 
152cc 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 2e 0a 20  into the list.. 
152cd 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c 6f 63 6b   */.  if( !pLock
152ce 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20   ){.    pLock = 
152cf 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c 69 74 65  (BtLock *)sqlite
152d0 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65  3MallocZero(size
152d1 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 20 20  of(BtLock));.   
152d2 20 69 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20   if( !pLock ){. 
152d3 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
152d4 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
152d5 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c      pLock->iTabl
152d6 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 20  e = iTable;.    
152d7 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20 3d 20  pLock->pBtree = 
152d8 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e  p;.    pLock->pN
152d9 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b  ext = pBt->pLock
152da 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b  ;.    pBt->pLock
152db 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20   = pLock;.  }.. 
152dc 20 2f 2a 20 53 65 74 20 74 68 65 20 42 74 4c 6f   /* Set the BtLo
152dd 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c  ck.eLock variabl
152de 65 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d  e to the maximum
152df 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
152e0 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64 20 74 68  lock.  ** and th
152e1 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
152e2 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20  . This means if 
152e3 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 61 73  a write-lock was
152e4 20 61 6c 72 65 61 64 79 20 68 65 6c 64 0a 20 20   already held.  
152e5 2a 2a 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f  ** and a read-lo
152e6 63 6b 20 72 65 71 75 65 73 74 65 64 2c 20 77 65  ck requested, we
152e7 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72 65 63 74   don't incorrect
152e8 6c 79 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65  ly downgrade the
152e9 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73   lock..  */.  as
152ea 73 65 72 74 28 20 57 52 49 54 45 5f 4c 4f 43 4b  sert( WRITE_LOCK
152eb 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  >READ_LOCK );.  
152ec 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 6b 2d  if( eLock>pLock-
152ed 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c  >eLock ){.    pL
152ee 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65 4c 6f  ock->eLock = eLo
152ef 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  ck;.  }..  retur
152f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
152f1 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45  endif /* !SQLITE
152f2 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
152f3 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  HE */..#ifndef S
152f4 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
152f5 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 52 65  D_CACHE./*.** Re
152f6 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 74 61  lease all the ta
152f7 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f 63 6b 73  ble locks (locks
152f8 20 6f 62 74 61 69 6e 65 64 20 76 69 61 20 63 61   obtained via ca
152f9 6c 6c 73 20 74 6f 0a 2a 2a 20 74 68 65 20 73 65  lls to.** the se
152fa 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  tSharedCacheTabl
152fb 65 4c 6f 63 6b 28 29 20 70 72 6f 63 65 64 75 72  eLock() procedur
152fc 65 29 20 68 65 6c 64 20 62 79 20 42 74 72 65 65  e) held by Btree
152fd 20 6f 62 6a 65 63 74 20 70 2e 0a 2a 2a 0a 2a 2a   object p..**.**
152fe 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
152ff 73 73 75 6d 65 73 20 74 68 61 74 20 42 74 72 65  ssumes that Btre
15300 65 20 70 20 68 61 73 20 61 6e 20 6f 70 65 6e 20  e p has an open 
15301 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 0a 2a  read or write .*
15302 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49  * transaction. I
15303 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2c 20 74  f it does not, t
15304 68 65 6e 20 74 68 65 20 42 74 53 68 61 72 65 64  hen the BtShared
15305 2e 69 73 50 65 6e 64 69 6e 67 20 76 61 72 69 61  .isPending varia
15306 62 6c 65 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e  ble.** may be in
15307 63 6f 72 72 65 63 74 6c 79 20 63 6c 65 61 72 65  correctly cleare
15308 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
15309 64 20 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64  d clearAllShared
1530a 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28  CacheTableLocks(
1530b 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53  Btree *p){.  BtS
1530c 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
1530d 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a  pBt;.  BtLock **
1530e 70 70 49 74 65 72 20 3d 20 26 70 42 74 2d 3e 70  ppIter = &pBt->p
1530f 4c 6f 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28  Lock;..  assert(
15310 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
15311 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20  dsMutex(p) );.  
15312 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61  assert( p->shara
15313 62 6c 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65  ble || 0==*ppIte
15314 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
15315 2d 3e 69 6e 54 72 61 6e 73 3e 30 20 29 3b 0a 0a  ->inTrans>0 );..
15316 20 20 77 68 69 6c 65 28 20 2a 70 70 49 74 65 72    while( *ppIter
15317 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a   ){.    BtLock *
15318 70 4c 6f 63 6b 20 3d 20 2a 70 70 49 74 65 72 3b  pLock = *ppIter;
15319 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74  .    assert( pBt
1531a 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30  ->isExclusive==0
1531b 20 7c 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72   || pBt->pWriter
1531c 3d 3d 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20  ==pLock->pBtree 
1531d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1531e 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 2d 3e 69 6e  Lock->pBtree->in
1531f 54 72 61 6e 73 3e 3d 70 4c 6f 63 6b 2d 3e 65 4c  Trans>=pLock->eL
15320 6f 63 6b 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ock );.    if( p
15321 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20  Lock->pBtree==p 
15322 29 7b 0a 20 20 20 20 20 20 2a 70 70 49 74 65 72  ){.      *ppIter
15323 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b   = pLock->pNext;
15324 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
15325 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20  Lock->iTable!=1 
15326 7c 7c 20 70 4c 6f 63 6b 3d 3d 26 70 2d 3e 6c 6f  || pLock==&p->lo
15327 63 6b 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ck );.      if( 
15328 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 21 3d 31  pLock->iTable!=1
15329 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1532a 74 65 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b  te3_free(pLock);
1532b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
1532c 73 65 7b 0a 20 20 20 20 20 20 70 70 49 74 65 72  se{.      ppIter
1532d 20 3d 20 26 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74   = &pLock->pNext
1532e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61  ;.    }.  }..  a
1532f 73 73 65 72 74 28 20 70 42 74 2d 3e 69 73 50 65  ssert( pBt->isPe
15330 6e 64 69 6e 67 3d 3d 30 20 7c 7c 20 70 42 74 2d  nding==0 || pBt-
15331 3e 70 57 72 69 74 65 72 20 29 3b 0a 20 20 69 66  >pWriter );.  if
15332 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d  ( pBt->pWriter==
15333 70 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 57  p ){.    pBt->pW
15334 72 69 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70  riter = 0;.    p
15335 42 74 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20  Bt->isExclusive 
15336 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73  = 0;.    pBt->is
15337 50 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 7d  Pending = 0;.  }
15338 65 6c 73 65 20 69 66 28 20 70 42 74 2d 3e 6e 54  else if( pBt->nT
15339 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 32 20 29 7b  ransaction==2 ){
1533a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 66 75 6e  .    /* This fun
1533b 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
1533c 77 68 65 6e 20 42 74 72 65 65 20 70 20 69 73 20  when Btree p is 
1533d 63 6f 6e 63 6c 75 64 69 6e 67 20 69 74 73 20 0a  concluding its .
1533e 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69      ** transacti
1533f 6f 6e 2e 20 49 66 20 74 68 65 72 65 20 63 75 72  on. If there cur
15340 72 65 6e 74 6c 79 20 65 78 69 73 74 73 20 61 20  rently exists a 
15341 77 72 69 74 65 72 2c 20 61 6e 64 20 70 20 69 73  writer, and p is
15342 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74   not.    ** that
15343 20 77 72 69 74 65 72 2c 20 74 68 65 6e 20 74 68   writer, then th
15344 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b  e number of lock
15345 73 20 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63  s held by connec
15346 74 69 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20 20  tions other.    
15347 2a 2a 20 74 68 61 6e 20 74 68 65 20 77 72 69 74  ** than the writ
15348 65 72 20 6d 75 73 74 20 62 65 20 61 62 6f 75 74  er must be about
15349 20 74 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f   to drop to zero
1534a 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20  . In this case. 
1534b 20 20 20 2a 2a 20 73 65 74 20 74 68 65 20 69 73     ** set the is
1534c 50 65 6e 64 69 6e 67 20 66 6c 61 67 20 74 6f 20  Pending flag to 
1534d 30 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  0..    **.    **
1534e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74   If there is not
1534f 20 63 75 72 72 65 6e 74 6c 79 20 61 20 77 72 69   currently a wri
15350 74 65 72 2c 20 74 68 65 6e 20 42 74 53 68 61 72  ter, then BtShar
15351 65 64 2e 69 73 50 65 6e 64 69 6e 67 20 6d 75 73  ed.isPending mus
15352 74 0a 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f  t.    ** be zero
15353 20 61 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 69   already. So thi
15354 73 20 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 68  s next line is h
15355 61 72 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 20  armless in that 
15356 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  case..    */.   
15357 20 70 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20   pBt->isPending 
15358 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 0;.  }.}../*.*
15359 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1535a 63 68 61 6e 67 65 73 20 61 6c 6c 20 77 72 69 74  changes all writ
1535b 65 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20  e-locks held by 
1535c 42 74 72 65 65 20 70 20 69 6e 74 6f 20 72 65 61  Btree p into rea
1535d 64 2d 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74  d-locks..*/.stat
1535e 69 63 20 76 6f 69 64 20 64 6f 77 6e 67 72 61 64  ic void downgrad
1535f 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54  eAllSharedCacheT
15360 61 62 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20  ableLocks(Btree 
15361 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  *p){.  BtShared 
15362 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
15363 20 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65   if( pBt->pWrite
15364 72 3d 3d 70 20 29 7b 0a 20 20 20 20 42 74 4c 6f  r==p ){.    BtLo
15365 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 20 20 20 20 70  ck *pLock;.    p
15366 42 74 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b  Bt->pWriter = 0;
15367 0a 20 20 20 20 70 42 74 2d 3e 69 73 45 78 63 6c  .    pBt->isExcl
15368 75 73 69 76 65 20 3d 20 30 3b 0a 20 20 20 20 70  usive = 0;.    p
15369 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20  Bt->isPending = 
1536a 30 3b 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b  0;.    for(pLock
1536b 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f  =pBt->pLock; pLo
1536c 63 6b 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d  ck; pLock=pLock-
1536d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61  >pNext){.      a
1536e 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c  ssert( pLock->eL
1536f 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c  ock==READ_LOCK |
15370 7c 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d  | pLock->pBtree=
15371 3d 70 20 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63  =p );.      pLoc
15372 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f  k->eLock = READ_
15373 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  LOCK;.    }.  }.
15374 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
15375 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
15376 43 41 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63  CACHE */..static
15377 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67   void releasePag
15378 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  e(MemPage *pPage
15379 29 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72  );  /* Forward r
1537a 65 66 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a  eference */../*.
1537b 2a 2a 2a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  ***** This routi
1537c 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 69 64  ne is used insid
1537d 65 20 6f 66 20 61 73 73 65 72 74 28 29 20 6f 6e  e of assert() on
1537e 6c 79 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65  ly ****.**.** Ve
1537f 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63 75  rify that the cu
15380 72 73 6f 72 20 68 6f 6c 64 73 20 74 68 65 20 6d  rsor holds the m
15381 75 74 65 78 20 6f 6e 20 69 74 73 20 42 74 53 68  utex on its BtSh
15382 61 72 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ared.*/.#ifdef S
15383 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74  QLITE_DEBUG.stat
15384 69 63 20 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c  ic int cursorHol
15385 64 73 4d 75 74 65 78 28 42 74 43 75 72 73 6f 72  dsMutex(BtCursor
15386 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73   *p){.  return s
15387 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15388 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  d(p->pBt->mutex)
15389 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66  ;.}.#endif...#if
1538a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1538b 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20  _INCRBLOB./*.** 
1538c 49 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f  Invalidate the o
1538d 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
1538e 74 20 63 61 63 68 65 20 66 6f 72 20 63 75 72 73  t cache for curs
1538f 6f 72 20 70 43 75 72 2c 20 69 66 20 61 6e 79 2e  or pCur, if any.
15390 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
15391 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c  invalidateOverfl
15392 6f 77 43 61 63 68 65 28 42 74 43 75 72 73 6f 72  owCache(BtCursor
15393 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72   *pCur){.  asser
15394 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
15395 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73  tex(pCur) );.  s
15396 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 72  qlite3_free(pCur
15397 2d 3e 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20  ->aOverflow);.  
15398 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
15399 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  = 0;.}../*.** In
1539a 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65  validate the ove
1539b 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20  rflow page-list 
1539c 63 61 63 68 65 20 66 6f 72 20 61 6c 6c 20 63 75  cache for all cu
1539d 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20  rsors opened.** 
1539e 6f 6e 20 74 68 65 20 73 68 61 72 65 64 20 62 74  on the shared bt
1539f 72 65 65 20 73 74 72 75 63 74 75 72 65 20 70 42  ree structure pB
153a0 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
153a1 64 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f  d invalidateAllO
153a2 76 65 72 66 6c 6f 77 43 61 63 68 65 28 42 74 53  verflowCache(BtS
153a3 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42  hared *pBt){.  B
153a4 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73  tCursor *p;.  as
153a5 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
153a6 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
153a7 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d  tex) );.  for(p=
153a8 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b  pBt->pCursor; p;
153a9 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
153aa 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72    invalidateOver
153ab 66 6c 6f 77 43 61 63 68 65 28 70 29 3b 0a 20 20  flowCache(p);.  
153ac 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
153ad 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
153ae 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79  ed before modify
153af 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ing the contents
153b0 20 6f 66 20 61 20 74 61 62 6c 65 0a 2a 2a 20 74   of a table.** t
153b1 6f 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79  o invalidate any
153b2 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72   incrblob cursor
153b3 73 20 74 68 61 74 20 61 72 65 20 6f 70 65 6e 20  s that are open 
153b4 6f 6e 20 74 68 65 0a 2a 2a 20 72 6f 77 20 6f 72  on the.** row or
153b5 20 6f 6e 65 20 6f 66 20 74 68 65 20 72 6f 77 73   one of the rows
153b6 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e   being modified.
153b7 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65  .**.** If argume
153b8 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 20  nt isClearTable 
153b9 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
153ba 65 20 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74  e entire content
153bb 73 20 6f 66 20 74 68 65 0a 2a 2a 20 74 61 62 6c  s of the.** tabl
153bc 65 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65  e is about to be
153bd 20 64 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 69   deleted. In thi
153be 73 20 63 61 73 65 20 69 6e 76 61 6c 69 64 61 74  s case invalidat
153bf 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f 62 0a 2a  e all incrblob.*
153c0 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f  * cursors open o
153c1 6e 20 61 6e 79 20 72 6f 77 20 77 69 74 68 69 6e  n any row within
153c2 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20   the table with 
153c3 72 6f 6f 74 2d 70 61 67 65 20 70 67 6e 6f 52 6f  root-page pgnoRo
153c4 6f 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77  ot..**.** Otherw
153c5 69 73 65 2c 20 69 66 20 61 72 67 75 6d 65 6e 74  ise, if argument
153c6 20 69 73 43 6c 65 61 72 54 61 62 6c 65 20 69 73   isClearTable is
153c7 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 65   false, then the
153c8 20 72 6f 77 20 77 69 74 68 0a 2a 2a 20 72 6f 77   row with.** row
153c9 69 64 20 69 52 6f 77 20 69 73 20 62 65 69 6e 67  id iRow is being
153ca 20 72 65 70 6c 61 63 65 64 20 6f 72 20 64 65 6c   replaced or del
153cb 65 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  eted. In this ca
153cc 73 65 20 69 6e 76 61 6c 69 64 61 74 65 0a 2a 2a  se invalidate.**
153cd 20 6f 6e 6c 79 20 74 68 6f 73 65 20 69 6e 63 72   only those incr
153ce 62 6c 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65  blob cursors ope
153cf 6e 20 6f 6e 20 74 68 61 74 20 73 70 65 63 69 66  n on that specif
153d0 69 63 20 72 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69  ic row..*/.stati
153d1 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74  c void invalidat
153d2 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73  eIncrblobCursors
153d3 28 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65  (.  Btree *pBtre
153d4 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e,          /* T
153d5 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
153d6 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69   to check */.  i
153d7 36 34 20 69 52 6f 77 2c 20 20 20 20 20 20 20 20  64 iRow,        
153d8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f         /* The ro
153d9 77 69 64 20 74 68 61 74 20 6d 69 67 68 74 20 62  wid that might b
153da 65 20 63 68 61 6e 67 69 6e 67 20 2a 2f 0a 20 20  e changing */.  
153db 69 6e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65  int isClearTable
153dc 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
153dd 69 66 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 20  if all rows are 
153de 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20 2a 2f  being deleted */
153df 0a 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  .){.  BtCursor *
153e0 70 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  p;.  BtShared *p
153e1 42 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74  Bt = pBtree->pBt
153e2 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
153e3 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
153e4 65 78 28 70 42 74 72 65 65 29 20 29 3b 0a 20 20  ex(pBtree) );.  
153e5 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73  for(p=pBt->pCurs
153e6 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  or; p; p=p->pNex
153e7 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69  t){.    if( p->i
153e8 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20  sIncrblobHandle 
153e9 26 26 20 28 69 73 43 6c 65 61 72 54 61 62 6c 65  && (isClearTable
153ea 20 7c 7c 20 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79   || p->info.nKey
153eb 3d 3d 69 52 6f 77 29 20 29 7b 0a 20 20 20 20 20  ==iRow) ){.     
153ec 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52   p->eState = CUR
153ed 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20  SOR_INVALID;.   
153ee 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a   }.  }.}..#else.
153ef 20 20 2f 2a 20 53 74 75 62 20 66 75 6e 63 74 69    /* Stub functi
153f0 6f 6e 73 20 77 68 65 6e 20 49 4e 43 52 42 4c 4f  ons when INCRBLO
153f1 42 20 69 73 20 6f 6d 69 74 74 65 64 20 2a 2f 0a  B is omitted */.
153f2 20 20 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69    #define invali
153f3 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68  dateOverflowCach
153f4 65 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 69  e(x).  #define i
153f5 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72  nvalidateAllOver
153f6 66 6c 6f 77 43 61 63 68 65 28 78 29 0a 20 20 23  flowCache(x).  #
153f7 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74  define invalidat
153f8 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73  eIncrblobCursors
153f9 28 78 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 20 2f  (x,y,z).#endif /
153fa 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  * SQLITE_OMIT_IN
153fb 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  CRBLOB */../*.**
153fc 20 53 65 74 20 62 69 74 20 70 67 6e 6f 20 6f 66   Set bit pgno of
153fd 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 70 48   the BtShared.pH
153fe 61 73 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63  asContent bitvec
153ff 2e 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64  . This is called
15400 20 0a 2a 2a 20 77 68 65 6e 20 61 20 70 61 67 65   .** when a page
15401 20 74 68 61 74 20 70 72 65 76 69 6f 75 73 6c 79   that previously
15402 20 63 6f 6e 74 61 69 6e 65 64 20 64 61 74 61 20   contained data 
15403 62 65 63 6f 6d 65 73 20 61 20 66 72 65 65 2d 6c  becomes a free-l
15404 69 73 74 20 6c 65 61 66 20 0a 2a 2a 20 70 61 67  ist leaf .** pag
15405 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 74 53  e..**.** The BtS
15406 68 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e  hared.pHasConten
15407 74 20 62 69 74 76 65 63 20 65 78 69 73 74 73 20  t bitvec exists 
15408 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 61  to work around a
15409 6e 20 6f 62 73 63 75 72 65 0a 2a 2a 20 62 75 67  n obscure.** bug
1540a 20 63 61 75 73 65 64 20 62 79 20 74 68 65 20 69   caused by the i
1540b 6e 74 65 72 61 63 74 69 6f 6e 20 6f 66 20 74 77  nteraction of tw
1540c 6f 20 75 73 65 66 75 6c 20 49 4f 20 6f 70 74 69  o useful IO opti
1540d 6d 69 7a 61 74 69 6f 6e 73 20 73 75 72 72 6f 75  mizations surrou
1540e 6e 64 69 6e 67 0a 2a 2a 20 66 72 65 65 2d 6c 69  nding.** free-li
1540f 73 74 20 6c 65 61 66 20 70 61 67 65 73 3a 0a 2a  st leaf pages:.*
15410 2a 0a 2a 2a 20 20 20 31 29 20 57 68 65 6e 20 61  *.**   1) When a
15411 6c 6c 20 64 61 74 61 20 69 73 20 64 65 6c 65 74  ll data is delet
15412 65 64 20 66 72 6f 6d 20 61 20 70 61 67 65 20 61  ed from a page a
15413 6e 64 20 74 68 65 20 70 61 67 65 20 62 65 63 6f  nd the page beco
15414 6d 65 73 0a 2a 2a 20 20 20 20 20 20 61 20 66 72  mes.**      a fr
15415 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67  ee-list leaf pag
15416 65 2c 20 74 68 65 20 70 61 67 65 20 69 73 20 6e  e, the page is n
15417 6f 74 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  ot written to th
15418 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20  e database.**   
15419 20 20 20 28 61 73 20 66 72 65 65 2d 6c 69 73 74     (as free-list
1541a 20 6c 65 61 66 20 70 61 67 65 73 20 63 6f 6e 74   leaf pages cont
1541b 61 69 6e 20 6e 6f 20 6d 65 61 6e 69 6e 67 66 75  ain no meaningfu
1541c 6c 20 64 61 74 61 29 2e 20 53 6f 6d 65 74 69 6d  l data). Sometim
1541d 65 73 0a 2a 2a 20 20 20 20 20 20 73 75 63 68 20  es.**      such 
1541e 61 20 70 61 67 65 20 69 73 20 6e 6f 74 20 65 76  a page is not ev
1541f 65 6e 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 28 61  en journalled (a
15420 73 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65  s it will not be
15421 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 20   modified,.**   
15422 20 20 20 77 68 79 20 62 6f 74 68 65 72 20 6a 6f     why bother jo
15423 75 72 6e 61 6c 6c 69 6e 67 20 69 74 3f 29 2e 0a  urnalling it?)..
15424 2a 2a 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20  **.**   2) When 
15425 61 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66  a free-list leaf
15426 20 70 61 67 65 20 69 73 20 72 65 75 73 65 64 2c   page is reused,
15427 20 69 74 73 20 63 6f 6e 74 65 6e 74 20 69 73 20   its content is 
15428 6e 6f 74 20 72 65 61 64 0a 2a 2a 20 20 20 20 20  not read.**     
15429 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
1542a 73 65 20 6f 72 20 77 72 69 74 74 65 6e 20 74 6f  se or written to
1542b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
1542c 65 20 28 77 68 79 20 73 68 6f 75 6c 64 20 69 74  e (why should it
1542d 0a 2a 2a 20 20 20 20 20 20 62 65 2c 20 69 66 20  .**      be, if 
1542e 69 74 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c  it is not at all
1542f 20 6d 65 61 6e 69 6e 67 66 75 6c 3f 29 2e 0a 2a   meaningful?)..*
15430 2a 0a 2a 2a 20 42 79 20 74 68 65 6d 73 65 6c 76  *.** By themselv
15431 65 73 2c 20 74 68 65 73 65 20 6f 70 74 69 6d 69  es, these optimi
15432 7a 61 74 69 6f 6e 73 20 77 6f 72 6b 20 66 69 6e  zations work fin
15433 65 20 61 6e 64 20 70 72 6f 76 69 64 65 20 61 20  e and provide a 
15434 68 61 6e 64 79 0a 2a 2a 20 70 65 72 66 6f 72 6d  handy.** perform
15435 61 6e 63 65 20 62 6f 6f 73 74 20 74 6f 20 62 75  ance boost to bu
15436 6c 6b 20 64 65 6c 65 74 65 20 6f 72 20 69 6e 73  lk delete or ins
15437 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20  ert operations. 
15438 48 6f 77 65 76 65 72 2c 20 69 66 0a 2a 2a 20 61  However, if.** a
15439 20 70 61 67 65 20 69 73 20 6d 6f 76 65 64 20 74   page is moved t
1543a 6f 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  o the free-list 
1543b 61 6e 64 20 74 68 65 6e 20 72 65 75 73 65 64 20  and then reused 
1543c 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a  within the same.
1543d 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20  ** transaction, 
1543e 61 20 70 72 6f 62 6c 65 6d 20 63 6f 6d 65 73 20  a problem comes 
1543f 75 70 2e 20 49 66 20 74 68 65 20 70 61 67 65 20  up. If the page 
15440 69 73 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65  is not journalle
15441 64 20 77 68 65 6e 0a 2a 2a 20 69 74 20 69 73 20  d when.** it is 
15442 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72 65  moved to the fre
15443 65 2d 6c 69 73 74 20 61 6e 64 20 69 74 20 69 73  e-list and it is
15444 20 61 6c 73 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61   also not journa
15445 6c 6c 65 64 20 77 68 65 6e 20 69 74 0a 2a 2a 20  lled when it.** 
15446 69 73 20 65 78 74 72 61 63 74 65 64 20 66 72 6f  is extracted fro
15447 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  m the free-list 
15448 61 6e 64 20 72 65 75 73 65 64 2c 20 74 68 65 6e  and reused, then
15449 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61   the original da
1544a 74 61 0a 2a 2a 20 6d 61 79 20 62 65 20 6c 6f 73  ta.** may be los
1544b 74 2e 20 49 6e 20 74 68 65 20 65 76 65 6e 74 20  t. In the event 
1544c 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 69  of a rollback, i
1544d 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 70 6f 73  t may not be pos
1544e 73 69 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 73 74  sible.** to rest
1544f 6f 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ore the database
15450 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c   to its original
15451 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e 0a   configuration..
15452 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69  **.** The soluti
15453 6f 6e 20 69 73 20 74 68 65 20 42 74 53 68 61 72  on is the BtShar
15454 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62  ed.pHasContent b
15455 69 74 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20  itvec. Whenever 
15456 61 20 70 61 67 65 20 69 73 20 0a 2a 2a 20 6d 6f  a page is .** mo
15457 76 65 64 20 74 6f 20 62 65 63 6f 6d 65 20 61 20  ved to become a 
15458 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70  free-list leaf p
15459 61 67 65 2c 20 74 68 65 20 63 6f 72 72 65 73 70  age, the corresp
1545a 6f 6e 64 69 6e 67 20 62 69 74 20 69 73 0a 2a 2a  onding bit is.**
1545b 20 73 65 74 20 69 6e 20 74 68 65 20 62 69 74 76   set in the bitv
1545c 65 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20 6c  ec. Whenever a l
1545d 65 61 66 20 70 61 67 65 20 69 73 20 65 78 74 72  eaf page is extr
1545e 61 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66  acted from the f
1545f 72 65 65 2d 6c 69 73 74 2c 0a 2a 2a 20 6f 70 74  ree-list,.** opt
15460 69 6d 69 7a 61 74 69 6f 6e 20 32 20 61 62 6f 76  imization 2 abov
15461 65 20 69 73 20 6f 6d 69 74 74 65 64 20 69 66 20  e is omitted if 
15462 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
15463 67 20 62 69 74 20 69 73 20 61 6c 72 65 61 64 79  g bit is already
15464 0a 2a 2a 20 73 65 74 20 69 6e 20 42 74 53 68 61  .** set in BtSha
15465 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 2e  red.pHasContent.
15466 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   The contents of
15467 20 74 68 65 20 62 69 74 76 65 63 20 61 72 65 20   the bitvec are 
15468 63 6c 65 61 72 65 64 0a 2a 2a 20 61 74 20 74 68  cleared.** at th
15469 65 20 65 6e 64 20 6f 66 20 65 76 65 72 79 20 74  e end of every t
1546a 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  ransaction..*/.s
1546b 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 53  tatic int btreeS
1546c 65 74 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53  etHasContent(BtS
1546d 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f  hared *pBt, Pgno
1546e 20 70 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63   pgno){.  int rc
1546f 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
15470 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f  if( !pBt->pHasCo
15471 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74  ntent ){.    int
15472 20 6e 50 61 67 65 20 3d 20 31 30 30 3b 0a 20 20   nPage = 100;.  
15473 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61    sqlite3PagerPa
15474 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61  gecount(pBt->pPa
15475 67 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20  ger, &nPage);.  
15476 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 50    /* If sqlite3P
15477 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20  agerPagecount() 
15478 66 61 69 6c 73 20 74 68 65 72 65 20 69 73 20 6e  fails there is n
15479 6f 20 68 61 72 6d 20 62 65 63 61 75 73 65 20 74  o harm because t
1547a 68 65 0a 20 20 20 20 2a 2a 20 6e 50 61 67 65 20  he.    ** nPage 
1547b 76 61 72 69 61 62 6c 65 20 69 73 20 75 6e 63 68  variable is unch
1547c 61 6e 67 65 64 20 66 72 6f 6d 20 69 74 73 20 64  anged from its d
1547d 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20  efault value of 
1547e 31 30 30 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e  100 */.    pBt->
1547f 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d 20 73 71  pHasContent = sq
15480 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61 74  lite3BitvecCreat
15481 65 28 28 75 33 32 29 6e 50 61 67 65 29 3b 0a 20  e((u32)nPage);. 
15482 20 20 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61     if( !pBt->pHa
15483 73 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20  sContent ){.    
15484 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
15485 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  MEM;.    }.  }. 
15486 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
15487 4f 4b 20 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69  OK && pgno<=sqli
15488 74 65 33 42 69 74 76 65 63 53 69 7a 65 28 70 42  te3BitvecSize(pB
15489 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 20  t->pHasContent) 
1548a 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1548b 74 65 33 42 69 74 76 65 63 53 65 74 28 70 42 74  te3BitvecSet(pBt
1548c 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70  ->pHasContent, p
1548d 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  gno);.  }.  retu
1548e 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1548f 51 75 65 72 79 20 74 68 65 20 42 74 53 68 61 72  Query the BtShar
15490 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 76  ed.pHasContent v
15491 65 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ector..**.** Thi
15492 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
15493 6c 6c 65 64 20 77 68 65 6e 20 61 20 66 72 65 65  lled when a free
15494 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 20  -list leaf page 
15495 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20  is removed from 
15496 74 68 65 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74  the.** free-list
15497 20 66 6f 72 20 72 65 75 73 65 2e 20 49 74 20 72   for reuse. It r
15498 65 74 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20  eturns false if 
15499 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 72 65  it is safe to re
1549a 74 72 69 65 76 65 20 74 68 65 0a 2a 2a 20 70 61  trieve the.** pa
1549b 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ge from the page
1549c 72 20 6c 61 79 65 72 20 77 69 74 68 20 74 68 65  r layer with the
1549d 20 27 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c   'no-content' fl
1549e 61 67 20 73 65 74 2e 20 54 72 75 65 20 6f 74 68  ag set. True oth
1549f 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  erwise..*/.stati
154a0 63 20 69 6e 74 20 62 74 72 65 65 47 65 74 48 61  c int btreeGetHa
154a1 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65  sContent(BtShare
154a2 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e  d *pBt, Pgno pgn
154a3 6f 29 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 20  o){.  Bitvec *p 
154a4 3d 20 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65  = pBt->pHasConte
154a5 6e 74 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20  nt;.  return (p 
154a6 26 26 20 28 70 67 6e 6f 3e 73 71 6c 69 74 65 33  && (pgno>sqlite3
154a7 42 69 74 76 65 63 53 69 7a 65 28 70 29 20 7c 7c  BitvecSize(p) ||
154a8 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65   sqlite3BitvecTe
154a9 73 74 28 70 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d  st(p, pgno)));.}
154aa 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 28 64  ../*.** Clear (d
154ab 65 73 74 72 6f 79 29 20 74 68 65 20 42 74 53 68  estroy) the BtSh
154ac 61 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74  ared.pHasContent
154ad 20 62 69 74 76 65 63 2e 20 54 68 69 73 20 73 68   bitvec. This sh
154ae 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b  ould be.** invok
154af 65 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75  ed at the conclu
154b0 73 69 6f 6e 20 6f 66 20 65 61 63 68 20 77 72 69  sion of each wri
154b1 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  te-transaction..
154b2 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62  */.static void b
154b3 74 72 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74  treeClearHasCont
154b4 65 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42  ent(BtShared *pB
154b5 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 69 74  t){.  sqlite3Bit
154b6 76 65 63 44 65 73 74 72 6f 79 28 70 42 74 2d 3e  vecDestroy(pBt->
154b7 70 48 61 73 43 6f 6e 74 65 6e 74 29 3b 0a 20 20  pHasContent);.  
154b8 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74  pBt->pHasContent
154b9 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53   = 0;.}../*.** S
154ba 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ave the current 
154bb 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20  cursor position 
154bc 69 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65 73  in the variables
154bd 20 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a   BtCursor.nKey .
154be 2a 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e  ** and BtCursor.
154bf 70 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72  pKey. The cursor
154c0 27 73 20 73 74 61 74 65 20 69 73 20 73 65 74 20  's state is set 
154c1 74 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52  to CURSOR_REQUIR
154c2 45 53 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ESEEK..**.** The
154c3 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73   caller must ens
154c4 75 72 65 20 74 68 61 74 20 74 68 65 20 63 75 72  ure that the cur
154c5 73 6f 72 20 69 73 20 76 61 6c 69 64 20 28 68 61  sor is valid (ha
154c6 73 20 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  s eState==CURSOR
154c7 5f 56 41 4c 49 44 29 0a 2a 2a 20 70 72 69 6f 72  _VALID).** prior
154c8 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73   to calling this
154c9 20 72 6f 75 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73   routine.  .*/.s
154ca 74 61 74 69 63 20 69 6e 74 20 73 61 76 65 43 75  tatic int saveCu
154cb 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43  rsorPosition(BtC
154cc 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
154cd 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
154ce 74 28 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d  t( CURSOR_VALID=
154cf 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20 29 3b  =pCur->eState );
154d0 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 70 43  .  assert( 0==pC
154d1 75 72 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73  ur->pKey );.  as
154d2 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
154d3 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
154d4 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
154d5 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72  treeKeySize(pCur
154d6 2c 20 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a  , &pCur->nKey);.
154d7 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
154d8 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b  LITE_OK );  /* K
154d9 65 79 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20  eySize() cannot 
154da 66 61 69 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  fail */..  /* If
154db 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 74 4b   this is an intK
154dc 65 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74  ey table, then t
154dd 68 65 20 61 62 6f 76 65 20 63 61 6c 6c 20 74 6f  he above call to
154de 20 42 74 72 65 65 4b 65 79 53 69 7a 65 28 29 0a   BtreeKeySize().
154df 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20    ** stores the 
154e0 69 6e 74 65 67 65 72 20 6b 65 79 20 69 6e 20 70  integer key in p
154e1 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 68  Cur->nKey. In th
154e2 69 73 20 63 61 73 65 20 74 68 69 73 20 76 61 6c  is case this val
154e3 75 65 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 74  ue is.  ** all t
154e4 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64 2e  hat is required.
154e5 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 70   Otherwise, if p
154e6 43 75 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20  Cur is not open 
154e7 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20 2a  on an intKey.  *
154e8 2a 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61  * table, then ma
154e9 6c 6c 6f 63 20 73 70 61 63 65 20 66 6f 72 20 61  lloc space for a
154ea 6e 64 20 73 74 6f 72 65 20 74 68 65 20 70 43 75  nd store the pCu
154eb 72 2d 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f 66  r->nKey bytes of
154ec 20 6b 65 79 20 0a 20 20 2a 2a 20 64 61 74 61 2e   key .  ** data.
154ed 0a 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 70  .  */.  if( 0==p
154ee 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e  Cur->apPage[0]->
154ef 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 76 6f  intKey ){.    vo
154f0 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74  id *pKey = sqlit
154f1 65 33 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 70  e3Malloc( (int)p
154f2 43 75 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20 20 20  Cur->nKey );.   
154f3 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20   if( pKey ){.   
154f4 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
154f5 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c  treeKey(pCur, 0,
154f6 20 28 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79   (int)pCur->nKey
154f7 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 69  , pKey);.      i
154f8 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
154f9 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72   ){.        pCur
154fa 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20  ->pKey = pKey;. 
154fb 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
154fc 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
154fd 28 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a  (pKey);.      }.
154fe 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
154ff 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
15500 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  EM;.    }.  }.  
15501 61 73 73 65 72 74 28 20 21 70 43 75 72 2d 3e 61  assert( !pCur->a
15502 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79  pPage[0]->intKey
15503 20 7c 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20   || !pCur->pKey 
15504 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  );..  if( rc==SQ
15505 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
15506 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
15507 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67  0; i<=pCur->iPag
15508 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72  e; i++){.      r
15509 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d  eleasePage(pCur-
1550a 3e 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20  >apPage[i]);.   
1550b 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b     pCur->apPage[
1550c 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  i] = 0;.    }.  
1550d 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20    pCur->iPage = 
1550e 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53  -1;.    pCur->eS
1550f 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 52 45  tate = CURSOR_RE
15510 51 55 49 52 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a  QUIRESEEK;.  }..
15511 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72    invalidateOver
15512 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b  flowCache(pCur);
15513 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
15514 0a 2f 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20  ./*.** Save the 
15515 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c  positions of all
15516 20 63 75 72 73 6f 72 73 20 28 65 78 63 65 70 74   cursors (except
15517 20 70 45 78 63 65 70 74 29 20 74 68 61 74 20 61   pExcept) that a
15518 72 65 20 6f 70 65 6e 20 6f 6e 0a 2a 2a 20 74 68  re open on.** th
15519 65 20 74 61 62 6c 65 20 20 77 69 74 68 20 72 6f  e table  with ro
1551a 6f 74 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55  ot-page iRoot. U
1551b 73 75 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20  sually, this is 
1551c 63 61 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f  called just befo
1551d 72 65 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78  re cursor.** pEx
1551e 63 65 70 74 20 69 73 20 75 73 65 64 20 74 6f 20  cept is used to 
1551f 6d 6f 64 69 66 79 20 74 68 65 20 74 61 62 6c 65  modify the table
15520 20 28 42 74 72 65 65 44 65 6c 65 74 65 28 29 20   (BtreeDelete() 
15521 6f 72 20 42 74 72 65 65 49 6e 73 65 72 74 28 29  or BtreeInsert()
15522 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
15523 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28   saveAllCursors(
15524 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50  BtShared *pBt, P
15525 67 6e 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72  gno iRoot, BtCur
15526 73 6f 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20  sor *pExcept){. 
15527 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20   BtCursor *p;.  
15528 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
15529 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
1552a 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
1552b 72 74 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c  rt( pExcept==0 |
1552c 7c 20 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d  | pExcept->pBt==
1552d 70 42 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70  pBt );.  for(p=p
1552e 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20  Bt->pCursor; p; 
1552f 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
15530 20 69 66 28 20 70 21 3d 70 45 78 63 65 70 74 20   if( p!=pExcept 
15531 26 26 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20  && (0==iRoot || 
15532 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f  p->pgnoRoot==iRo
15533 6f 74 29 20 26 26 20 0a 20 20 20 20 20 20 20 20  ot) && .        
15534 70 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  p->eState==CURSO
15535 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20  R_VALID ){.     
15536 20 69 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75   int rc = saveCu
15537 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b  rsorPosition(p);
15538 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
15539 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20  E_OK!=rc ){.    
1553a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1553b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1553c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1553d 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
1553e 65 61 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  ear the current 
1553f 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e  cursor position.
15540 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15541 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
15542 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28  treeClearCursor(
15543 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
15544 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
15545 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
15546 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  ) );.  sqlite3_f
15547 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b  ree(pCur->pKey);
15548 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20  .  pCur->pKey = 
15549 30 3b 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74  0;.  pCur->eStat
1554a 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  e = CURSOR_INVAL
1554b 49 44 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20  ID;.}../*.** In 
1554c 74 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  this version of 
1554d 42 74 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65  BtreeMoveto, pKe
1554e 79 20 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e  y is a packed in
1554f 64 65 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75  dex record.** su
15550 63 68 20 61 73 20 69 73 20 67 65 6e 65 72 61 74  ch as is generat
15551 65 64 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b  ed by the OP_Mak
15552 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20  eRecord opcode. 
15553 20 55 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72   Unpack the.** r
15554 65 63 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63  ecord and then c
15555 61 6c 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55  all BtreeMovetoU
15556 6e 70 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20  npacked() to do 
15557 74 68 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61  the work..*/.sta
15558 74 69 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76  tic int btreeMov
15559 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20  eto(.  BtCursor 
1555a 2a 70 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75  *pCur,     /* Cu
1555b 72 73 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65  rsor open on the
1555c 20 62 74 72 65 65 20 74 6f 20 62 65 20 73 65 61   btree to be sea
1555d 72 63 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  rched */.  const
1555e 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f   void *pKey,   /
1555f 2a 20 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20  * Packed key if 
15560 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6e 20  the btree is an 
15561 69 6e 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e  index */.  i64 n
15562 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Key,           /
15563 2a 20 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f  * Integer key fo
15564 72 20 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20  r tables.  Size 
15565 6f 66 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69  of pKey for indi
15566 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61  ces */.  int bia
15567 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
15568 42 69 61 73 20 73 65 61 72 63 68 20 74 6f 20 74  Bias search to t
15569 68 65 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20  he high end */. 
1556a 20 69 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20   int *pRes      
1556b 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65       /* Write se
1556c 61 72 63 68 20 72 65 73 75 6c 74 73 20 68 65 72  arch results her
1556d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
1556e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1556f 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63       /* Status c
15570 6f 64 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65  ode */.  Unpacke
15571 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79  dRecord *pIdxKey
15572 3b 20 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20  ;   /* Unpacked 
15573 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63  index key */.  c
15574 68 61 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b  har aSpace[150];
15575 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
15576 70 20 73 70 61 63 65 20 66 6f 72 20 70 49 64 78  p space for pIdx
15577 4b 65 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61  Key - to avoid a
15578 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66   malloc */..  if
15579 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73  ( pKey ){.    as
1557a 73 65 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34  sert( nKey==(i64
1557b 29 28 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20  )(int)nKey );.  
1557c 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69    pIdxKey = sqli
1557d 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70  te3VdbeRecordUnp
1557e 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e  ack(pCur->pKeyIn
1557f 66 6f 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70  fo, (int)nKey, p
15580 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Key,.           
15581 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15582 20 20 20 20 20 20 20 20 20 20 20 61 53 70 61 63             aSpac
15583 65 2c 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65  e, sizeof(aSpace
15584 29 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78  ));.    if( pIdx
15585 4b 65 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Key==0 ) return 
15586 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
15587 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b  }else{.    pIdxK
15588 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63  ey = 0;.  }.  rc
15589 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
1558a 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43  ovetoUnpacked(pC
1558b 75 72 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65  ur, pIdxKey, nKe
1558c 79 2c 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a  y, bias, pRes);.
1558d 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20    if( pKey ){.  
1558e 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
1558f 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
15590 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a  d(pIdxKey);.  }.
15591 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
15592 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68  /*.** Restore th
15593 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20  e cursor to the 
15594 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20  position it was 
15595 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20  in (or as close 
15596 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a  to as possible).
15597 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73  ** when saveCurs
15598 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73  orPosition() was
15599 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68   called. Note th
1559a 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c  at this call del
1559b 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76  etes the .** sav
1559c 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f  ed position info
1559d 20 73 74 6f 72 65 64 20 62 79 20 73 61 76 65 43   stored by saveC
1559e 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c  ursorPosition(),
1559f 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65   so there can be
155a0 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20  .** at most one 
155a1 65 66 66 65 63 74 69 76 65 20 72 65 73 74 6f 72  effective restor
155a2 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
155a3 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63  ) call after eac
155a4 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72  h .** saveCursor
155a5 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73  Position()..*/.s
155a6 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 52  tatic int btreeR
155a7 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
155a8 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70  tion(BtCursor *p
155a9 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Cur){.  int rc;.
155aa 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
155ab 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
155ac 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
155ad 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53  ur->eState>=CURS
155ae 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
155af 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53  ;.  if( pCur->eS
155b0 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55  tate==CURSOR_FAU
155b1 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  LT ){.    return
155b2 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b   pCur->skipNext;
155b3 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74  .  }.  pCur->eSt
155b4 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56  ate = CURSOR_INV
155b5 41 4c 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72  ALID;.  rc = btr
155b6 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70  eeMoveto(pCur, p
155b7 43 75 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d  Cur->pKey, pCur-
155b8 3e 6e 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d  >nKey, 0, &pCur-
155b9 3e 73 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66  >skipNext);.  if
155ba 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
155bb 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
155bc 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b  ree(pCur->pKey);
155bd 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20  .    pCur->pKey 
155be 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28  = 0;.    assert(
155bf 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
155c0 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70  URSOR_VALID || p
155c1 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
155c2 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20  SOR_INVALID );. 
155c3 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
155c4 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f  }..#define resto
155c5 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
155c6 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61  (p) \.  (p->eSta
155c7 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49  te>=CURSOR_REQUI
155c8 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20  RESEEK ? \.     
155c9 20 20 20 20 62 74 72 65 65 52 65 73 74 6f 72 65      btreeRestore
155ca 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
155cb 29 20 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53  ) : \.         S
155cc 51 4c 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a  QLITE_OK)../*.**
155cd 20 44 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   Determine wheth
155ce 65 72 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73  er or not a curs
155cf 6f 72 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f  or has moved fro
155d0 6d 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69  m the position i
155d1 74 0a 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6c  t.** was last pl
155d2 61 63 65 64 20 61 74 2e 20 20 43 75 72 73 6f 72  aced at.  Cursor
155d3 73 20 63 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20  s can move when 
155d4 74 68 65 20 72 6f 77 20 74 68 65 79 20 61 72 65  the row they are
155d5 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 74 20   pointing.** at 
155d6 69 73 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66  is deleted out f
155d7 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0a  rom under them..
155d8 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
155d9 6e 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72  ne returns an er
155da 72 6f 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65  ror code if some
155db 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
155dc 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65  .  The.** intege
155dd 72 20 2a 70 48 61 73 4d 6f 76 65 64 20 69 73 20  r *pHasMoved is 
155de 73 65 74 20 74 6f 20 6f 6e 65 20 69 66 20 74 68  set to one if th
155df 65 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76  e cursor has mov
155e0 65 64 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e  ed and 0 if not.
155e1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
155e2 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
155e3 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65  reeCursorHasMove
155e4 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  d(BtCursor *pCur
155e5 2c 20 69 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64  , int *pHasMoved
155e6 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
155e7 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73  rc = restoreCurs
155e8 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29  orPosition(pCur)
155e9 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
155ea 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31    *pHasMoved = 1
155eb 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
155ec 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d  .  }.  if( pCur-
155ed 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f  >eState!=CURSOR_
155ee 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 73  VALID || pCur->s
155ef 6b 69 70 4e 65 78 74 21 3d 30 20 29 7b 0a 20 20  kipNext!=0 ){.  
155f0 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31    *pHasMoved = 1
155f1 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a  ;.  }else{.    *
155f2 70 48 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20  pHasMoved = 0;. 
155f3 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
155f4 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  TE_OK;.}..#ifnde
155f5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
155f6 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47  TOVACUUM./*.** G
155f7 69 76 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62  iven a page numb
155f8 65 72 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20  er of a regular 
155f9 64 61 74 61 62 61 73 65 20 70 61 67 65 2c 20 72  database page, r
155fa 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a  eturn the page.*
155fb 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  * number for the
155fc 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
155fd 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
155fe 74 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68  the entry for th
155ff 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20  e.** input page 
15600 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
15601 63 20 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67  c Pgno ptrmapPag
15602 65 6e 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42  eno(BtShared *pB
15603 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20  t, Pgno pgno){. 
15604 20 69 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61   int nPagesPerMa
15605 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50  pPage;.  Pgno iP
15606 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73  trMap, ret;.  as
15607 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15608 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
15609 74 65 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 73  tex) );.  nPages
1560a 50 65 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42  PerMapPage = (pB
1560b 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29  t->usableSize/5)
1560c 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20  +1;.  iPtrMap = 
1560d 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50  (pgno-2)/nPagesP
1560e 65 72 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74  erMapPage;.  ret
1560f 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67   = (iPtrMap*nPag
15610 65 73 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20  esPerMapPage) + 
15611 32 3b 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50  2; .  if( ret==P
15612 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
15613 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74  (pBt) ){.    ret
15614 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ++;.  }.  return
15615 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57   ret;.}../*.** W
15616 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e  rite an entry in
15617 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  to the pointer m
15618 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ap..**.** This r
15619 6f 75 74 69 6e 65 20 75 70 64 61 74 65 73 20 74  outine updates t
1561a 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65  he pointer map e
1561b 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75  ntry for page nu
1561c 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f  mber 'key'.** so
1561d 20 74 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f   that it maps to
1561e 20 74 79 70 65 20 27 65 54 79 70 65 27 20 61 6e   type 'eType' an
1561f 64 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75  d parent page nu
15620 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a  mber 'pgno'..**.
15621 2a 2a 20 49 66 20 2a 70 52 43 20 69 73 20 69 6e  ** If *pRC is in
15622 69 74 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f  itially non-zero
15623 20 28 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29   (non-SQLITE_OK)
15624 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
15625 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70  ne is.** a no-op
15626 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  .  If an error o
15627 63 63 75 72 73 2c 20 74 68 65 20 61 70 70 72 6f  ccurs, the appro
15628 70 72 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64  priate error cod
15629 65 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  e is written.** 
1562a 69 6e 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74  into *pRC..*/.st
1562b 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70  atic void ptrmap
1562c 50 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 42  Put(BtShared *pB
1562d 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20  t, Pgno key, u8 
1562e 65 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65  eType, Pgno pare
1562f 6e 74 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20  nt, int *pRC){. 
15630 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65   DbPage *pDbPage
15631 3b 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65  ;  /* The pointe
15632 72 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20  r map page */.  
15633 75 38 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20  u8 *pPtrmap;    
15634 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72    /* The pointer
15635 20 6d 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 50   map data */.  P
15636 67 6e 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 20  gno iPtrmap;    
15637 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20   /* The pointer 
15638 6d 61 70 20 70 61 67 65 20 6e 75 6d 62 65 72 20  map page number 
15639 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b  */.  int offset;
1563a 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
1563b 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20   in pointer map 
1563c 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63  page */.  int rc
1563d 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  ;           /* R
1563e 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
1563f 73 75 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  subfunctions */.
15640 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65  .  if( *pRC ) re
15641 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28  turn;..  assert(
15642 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15643 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
15644 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74  );.  /* The mast
15645 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20  er-journal page 
15646 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65  number must neve
15647 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20 70  r be used as a p
15648 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20  ointer map page 
15649 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d  */.  assert( 0==
1564a 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42  PTRMAP_ISPAGE(pB
1564b 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  t, PENDING_BYTE_
1564c 50 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20  PAGE(pBt)) );.. 
1564d 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75   assert( pBt->au
1564e 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66  toVacuum );.  if
1564f 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20  ( key==0 ){.    
15650 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f  *pRC = SQLITE_CO
15651 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
15652 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 50  return;.  }.  iP
15653 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50  trmap = PTRMAP_P
15654 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b  AGENO(pBt, key);
15655 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
15656 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61  agerGet(pBt->pPa
15657 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70  ger, iPtrmap, &p
15658 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  DbPage);.  if( r
15659 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1565a 20 20 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20      *pRC = rc;. 
1565b 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
1565c 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50   offset = PTRMAP
1565d 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d  _PTROFFSET(iPtrm
1565e 61 70 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20  ap, key);.  if( 
1565f 6f 66 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20  offset<0 ){.    
15660 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f  *pRC = SQLITE_CO
15661 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
15662 67 6f 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 74  goto ptrmap_exit
15663 3b 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20  ;.  }.  pPtrmap 
15664 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50  = (u8 *)sqlite3P
15665 61 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50  agerGetData(pDbP
15666 61 67 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79  age);..  if( eTy
15667 70 65 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73  pe!=pPtrmap[offs
15668 65 74 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28  et] || get4byte(
15669 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b  &pPtrmap[offset+
1566a 31 5d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20  1])!=parent ){. 
1566b 20 20 20 54 52 41 43 45 28 28 22 50 54 52 4d 41     TRACE(("PTRMA
1566c 50 5f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25  P_UPDATE: %d->(%
1566d 64 2c 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65  d,%d)\n", key, e
1566e 54 79 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a  Type, parent));.
1566f 20 20 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73      *pRC= rc = s
15670 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
15671 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  (pDbPage);.    i
15672 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
15673 20 29 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61   ){.      pPtrma
15674 70 5b 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70  p[offset] = eTyp
15675 65 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74  e;.      put4byt
15676 65 28 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65  e(&pPtrmap[offse
15677 74 2b 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20  t+1], parent);. 
15678 20 20 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70     }.  }..ptrmap
15679 5f 65 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33  _exit:.  sqlite3
1567a 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50 61  PagerUnref(pDbPa
1567b 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ge);.}../*.** Re
1567c 61 64 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d  ad an entry from
1567d 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
1567e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1567f 74 69 6e 65 20 72 65 74 72 69 65 76 65 73 20 74  tine retrieves t
15680 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65  he pointer map e
15681 6e 74 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b  ntry for page 'k
15682 65 79 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20  ey', writing.** 
15683 74 68 65 20 74 79 70 65 20 61 6e 64 20 70 61 72  the type and par
15684 65 6e 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  ent page number 
15685 74 6f 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a  to *pEType and *
15686 70 50 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65  pPgno respective
15687 6c 79 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20  ly..** An error 
15688 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
15689 20 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f   if something go
1568a 65 73 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77  es wrong, otherw
1568b 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a  ise SQLITE_OK..*
1568c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 72  /.static int ptr
1568d 6d 61 70 47 65 74 28 42 74 53 68 61 72 65 64 20  mapGet(BtShared 
1568e 2a 70 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20  *pBt, Pgno key, 
1568f 75 38 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f  u8 *pEType, Pgno
15690 20 2a 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61   *pPgno){.  DbPa
15691 67 65 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f  ge *pDbPage;   /
15692 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  * The pointer ma
15693 70 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  p page */.  int 
15694 69 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f  iPtrmap;       /
15695 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  * Pointer map pa
15696 67 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38  ge index */.  u8
15697 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20   *pPtrmap;      
15698 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20   /* Pointer map 
15699 70 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69  page data */.  i
1569a 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20  nt offset;      
1569b 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65    /* Offset of e
1569c 6e 74 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20  ntry in pointer 
1569d 6d 61 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  map */.  int rc;
1569e 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1569f 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
156a0 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  Bt->mutex) );.. 
156a1 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41   iPtrmap = PTRMA
156a2 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65  P_PAGENO(pBt, ke
156a3 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  y);.  rc = sqlit
156a4 65 33 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e  e3PagerGet(pBt->
156a5 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c  pPager, iPtrmap,
156a6 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66   &pDbPage);.  if
156a7 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72  ( rc!=0 ){.    r
156a8 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
156a9 70 50 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29  pPtrmap = (u8 *)
156aa 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
156ab 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20  ata(pDbPage);.. 
156ac 20 6f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50   offset = PTRMAP
156ad 5f 50 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d  _PTROFFSET(iPtrm
156ae 61 70 2c 20 6b 65 79 29 3b 0a 20 20 61 73 73 65  ap, key);.  asse
156af 72 74 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b  rt( pEType!=0 );
156b0 0a 20 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74  .  *pEType = pPt
156b1 72 6d 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20  rmap[offset];.  
156b2 69 66 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67  if( pPgno ) *pPg
156b3 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
156b4 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d  Ptrmap[offset+1]
156b5 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67  );..  sqlite3Pag
156b6 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65 29  erUnref(pDbPage)
156b7 3b 0a 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c  ;.  if( *pEType<
156b8 31 20 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29  1 || *pEType>5 )
156b9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
156ba 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72  ORRUPT_BKPT;.  r
156bb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
156bc 0a 7d 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20  .}..#else /* if 
156bd 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f  defined SQLITE_O
156be 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a  MIT_AUTOVACUUM *
156bf 2f 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d  /.  #define ptrm
156c0 61 70 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63  apPut(w,x,y,z,rc
156c1 29 0a 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d  ).  #define ptrm
156c2 61 70 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53  apGet(w,x,y,z) S
156c3 51 4c 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69  QLITE_OK.  #defi
156c4 6e 65 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c  ne ptrmapPutOvfl
156c5 50 74 72 28 78 2c 20 79 2c 20 72 63 29 0a 23 65  Ptr(x, y, rc).#e
156c6 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  ndif../*.** Give
156c7 6e 20 61 20 62 74 72 65 65 20 70 61 67 65 20 61  n a btree page a
156c8 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20  nd a cell index 
156c9 28 30 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72  (0 means the fir
156ca 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68  st cell on.** th
156cb 65 20 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20  e page, 1 means 
156cc 74 68 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c  the second cell,
156cd 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72   and so forth) r
156ce 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
156cf 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63  ** to the cell c
156d0 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ontent..**.** Th
156d1 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73  is routine works
156d2 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20   only for pages 
156d3 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74  that do not cont
156d4 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  ain overflow cel
156d5 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66  ls..*/.#define f
156d6 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20  indCell(P,I) \. 
156d7 20 28 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28   ((P)->aData + (
156d8 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20  (P)->maskPage & 
156d9 67 65 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61  get2byte(&(P)->a
156da 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66  Data[(P)->cellOf
156db 66 73 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a  fset+2*(I)])))..
156dc 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72  /*.** This a mor
156dd 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f  e complex versio
156de 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20  n of findCell() 
156df 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a  that works for.*
156e0 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20  * pages that do 
156e1 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77  contain overflow
156e2 20 63 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69   cells..*/.stati
156e3 63 20 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c  c u8 *findOverfl
156e4 6f 77 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a  owCell(MemPage *
156e5 70 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c  pPage, int iCell
156e6 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73  ){.  int i;.  as
156e7 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
156e8 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
156e9 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
156ea 20 66 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f   for(i=pPage->nO
156eb 76 65 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b  verflow-1; i>=0;
156ec 20 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b   i--){.    int k
156ed 3b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76  ;.    struct _Ov
156ee 66 6c 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20  flCell *pOvfl;. 
156ef 20 20 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67     pOvfl = &pPag
156f0 65 2d 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20  e->aOvfl[i];.   
156f1 20 6b 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b   k = pOvfl->idx;
156f2 0a 20 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c  .    if( k<=iCel
156f3 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b  l ){.      if( k
156f4 3d 3d 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20  ==iCell ){.     
156f5 20 20 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d     return pOvfl-
156f6 3e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a  >pCell;.      }.
156f7 20 20 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20        iCell--;. 
156f8 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
156f9 6e 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  n findCell(pPage
156fa 2c 20 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a  , iCell);.}../*.
156fb 2a 2a 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20  ** Parse a cell 
156fc 63 6f 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e  content block an
156fd 64 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65  d fill in the Ce
156fe 6c 6c 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  llInfo structure
156ff 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20  .  There.** are 
15700 74 77 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  two versions of 
15701 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20  this function.  
15702 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29  btreeParseCell()
15703 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c   takes a .** cel
15704 6c 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 73  l index as the s
15705 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61  econd argument a
15706 6e 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c  nd btreeParseCel
15707 6c 50 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73  lPtr() .** takes
15708 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
15709 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65  e body of the ce
1570a 6c 6c 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64  ll as its second
1570b 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
1570c 20 57 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c   Within this fil
1570d 65 2c 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c  e, the parseCell
1570e 28 29 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20  () macro can be 
1570f 63 61 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f  called instead o
15710 66 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43  f.** btreeParseC
15711 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20  ellPtr(). Using 
15712 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20  some compilers, 
15713 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73  this will be fas
15714 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ter..*/.static v
15715 6f 69 64 20 62 74 72 65 65 50 61 72 73 65 43 65  oid btreeParseCe
15716 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65  llPtr(.  MemPage
15717 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20   *pPage,        
15718 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e   /* Page contain
15719 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a  ing the cell */.
1571a 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20    u8 *pCell,    
1571b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
1571c 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c  nter to the cell
1571d 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c   text. */.  Cell
1571e 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20  Info *pInfo     
1571f 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74      /* Fill in t
15720 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
15721 0a 29 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20  .){.  u16 n;    
15722 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15723 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e   Number bytes in
15724 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65   cell content he
15725 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50  ader */.  u32 nP
15726 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20  ayload;         
15727 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
15728 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79  ytes of cell pay
15729 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72  load */..  asser
1572a 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1572b 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
1572c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70  ->mutex) );..  p
1572d 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43  Info->pCell = pC
1572e 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ell;.  assert( p
1572f 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c  Page->leaf==0 ||
15730 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20   pPage->leaf==1 
15731 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e  );.  n = pPage->
15732 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20  childPtrSize;.  
15733 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70  assert( n==4-4*p
15734 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  Page->leaf );.  
15735 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
15736 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  y ){.    if( pPa
15737 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20  ge->hasData ){. 
15738 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72       n += getVar
15739 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c  int32(&pCell[n],
1573a 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20   nPayload);.    
1573b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61  }else{.      nPa
1573c 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d  yload = 0;.    }
1573d 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72  .    n += getVar
1573e 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28  int(&pCell[n], (
1573f 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65  u64*)&pInfo->nKe
15740 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e  y);.    pInfo->n
15741 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b  Data = nPayload;
15742 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49  .  }else{.    pI
15743 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a  nfo->nData = 0;.
15744 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69      n += getVari
15745 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20  nt32(&pCell[n], 
15746 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70  nPayload);.    p
15747 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61  Info->nKey = nPa
15748 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e  yload;.  }.  pIn
15749 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e  fo->nPayload = n
1574a 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f  Payload;.  pInfo
1574b 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20  ->nHeader = n;. 
1574c 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c   testcase( nPayl
1574d 6f 61 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c  oad==pPage->maxL
1574e 6f 63 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61  ocal );.  testca
1574f 73 65 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50  se( nPayload==pP
15750 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20  age->maxLocal+1 
15751 29 3b 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28  );.  if( likely(
15752 6e 50 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d  nPayload<=pPage-
15753 3e 6d 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20  >maxLocal) ){.  
15754 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
15755 20 28 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63   (easy) common c
15756 61 73 65 20 77 68 65 72 65 20 74 68 65 20 65 6e  ase where the en
15757 74 69 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74  tire payload fit
15758 73 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20  s.    ** on the 
15759 6c 6f 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20  local page.  No 
1575a 6f 76 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75  overflow is requ
1575b 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ired..    */.   
1575c 20 69 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 20   int nSize;     
1575d 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69       /* Total si
1575e 7a 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65  ze of cell conte
1575f 6e 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  nt in bytes */. 
15760 20 20 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c     nSize = nPayl
15761 6f 61 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 6e  oad + n;.    pIn
15762 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31  fo->nLocal = (u1
15763 36 29 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20  6)nPayload;.    
15764 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77  pInfo->iOverflow
15765 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e   = 0;.    if( (n
15766 53 69 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b  Size & ~3)==0 ){
15767 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34  .      nSize = 4
15768 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69  ;        /* Mini
15769 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73  mum cell size is
1576a 20 34 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20   4 */.    }.    
1576b 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28  pInfo->nSize = (
1576c 75 31 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c  u16)nSize;.  }el
1576d 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  se{.    /* If th
1576e 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e  e payload will n
1576f 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c  ot fit completel
15770 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70  y on the local p
15771 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20  age, we have.   
15772 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f   ** to decide ho
15773 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20  w much to store 
15774 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20  locally and how 
15775 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e  much to spill on
15776 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c  to.    ** overfl
15777 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20 73  ow pages.  The s
15778 74 72 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69  trategy is to mi
15779 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e  nimize the amoun
1577a 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20  t of unused.    
1577b 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72  ** space on over
1577c 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65  flow pages while
1577d 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f   keeping the amo
1577e 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f  unt of local sto
1577f 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62  rage.    ** in b
15780 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20  etween minLocal 
15781 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20  and maxLocal..  
15782 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e    **.    ** Warn
15783 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74  ing:  changing t
15784 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20  he way overflow 
15785 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72  payload is distr
15786 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20  ibuted in any.  
15787 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65    ** way will re
15788 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d  sult in an incom
15789 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72  patible file for
1578a 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  mat..    */.    
1578b 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f  int minLocal;  /
1578c 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74  * Minimum amount
1578d 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64   of payload held
1578e 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20   locally */.    
1578f 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f  int maxLocal;  /
15790 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74  * Maximum amount
15791 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64   of payload held
15792 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20   locally */.    
15793 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f  int surplus;   /
15794 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f  * Overflow paylo
15795 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  ad available for
15796 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a   local storage *
15797 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20  /..    minLocal 
15798 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61  = pPage->minLoca
15799 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20  l;.    maxLocal 
1579a 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  = pPage->maxLoca
1579b 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d  l;.    surplus =
1579c 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61   minLocal + (nPa
1579d 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c  yload - minLocal
1579e 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  )%(pPage->pBt->u
1579f 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a  sableSize - 4);.
157a0 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73 75      testcase( su
157a1 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20  rplus==maxLocal 
157a2 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
157a3 20 73 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63   surplus==maxLoc
157a4 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20  al+1 );.    if( 
157a5 73 75 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f  surplus <= maxLo
157a6 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  cal ){.      pIn
157a7 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31  fo->nLocal = (u1
157a8 36 29 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d  6)surplus;.    }
157a9 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66  else{.      pInf
157aa 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36  o->nLocal = (u16
157ab 29 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d  )minLocal;.    }
157ac 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  .    pInfo->iOve
157ad 72 66 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49  rflow = (u16)(pI
157ae 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29  nfo->nLocal + n)
157af 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69  ;.    pInfo->nSi
157b0 7a 65 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  ze = pInfo->iOve
157b1 72 66 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d  rflow + 4;.  }.}
157b2 0a 23 64 65 66 69 6e 65 20 70 61 72 73 65 43 65  .#define parseCe
157b3 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c  ll(pPage, iCell,
157b4 20 70 49 6e 66 6f 29 20 5c 0a 20 20 62 74 72 65   pInfo) \.  btre
157b5 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70  eParseCellPtr((p
157b6 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28  Page), findCell(
157b7 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29  (pPage), (iCell)
157b8 29 2c 20 28 70 49 6e 66 6f 29 29 0a 73 74 61 74  ), (pInfo)).stat
157b9 69 63 20 76 6f 69 64 20 62 74 72 65 65 50 61 72  ic void btreePar
157ba 73 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67  seCell(.  MemPag
157bb 65 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20  e *pPage,       
157bc 20 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69    /* Page contai
157bd 6e 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f  ning the cell */
157be 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20  .  int iCell,   
157bf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
157c0 65 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46  e cell index.  F
157c1 69 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a  irst cell is 0 *
157c2 2f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49  /.  CellInfo *pI
157c3 6e 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46  nfo         /* F
157c4 69 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75  ill in this stru
157c5 63 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61  cture */.){.  pa
157c6 72 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  rseCell(pPage, i
157c7 43 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a  Cell, pInfo);.}.
157c8 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74  ./*.** Compute t
157c9 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20  he total number 
157ca 6f 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20  of bytes that a 
157cb 43 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68  Cell needs in th
157cc 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61  e cell.** data a
157cd 72 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65  rea of the btree
157ce 2d 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75  -page.  The retu
157cf 72 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64  rn number includ
157d0 65 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64  es the cell.** d
157d1 61 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74  ata header and t
157d2 68 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  he local payload
157d3 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76  , but not any ov
157d4 65 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a  erflow page or.*
157d5 2a 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64  * the space used
157d6 20 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69   by the cell poi
157d7 6e 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  nter..*/.static 
157d8 75 31 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  u16 cellSizePtr(
157d9 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
157da 75 38 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38  u8 *pCell){.  u8
157db 20 2a 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c   *pIter = &pCell
157dc 5b 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72  [pPage->childPtr
157dd 53 69 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69  Size];.  u32 nSi
157de 7a 65 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ze;..#ifdef SQLI
157df 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68  TE_DEBUG.  /* Th
157e0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
157e1 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
157e2 6e 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  n should always 
157e3 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20  be the same as. 
157e4 20 2a 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66   ** the (CellInf
157e5 6f 2e 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66  o.nSize) value f
157e6 6f 75 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20  ound by doing a 
157e7 66 75 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68  full parse of th
157e8 65 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20  e.  ** cell. If 
157e9 53 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20  SQLITE_DEBUG is 
157ea 64 65 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65  defined, an asse
157eb 72 74 28 29 20 61 74 20 74 68 65 20 62 6f 74 74  rt() at the bott
157ec 6f 6d 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20  om of.  ** this 
157ed 66 75 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65  function verifie
157ee 73 20 74 68 61 74 20 74 68 69 73 20 69 6e 76 61  s that this inva
157ef 72 69 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f  riant is not vio
157f0 6c 61 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c  lated. */.  Cell
157f1 49 6e 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a  Info debuginfo;.
157f2 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
157f3 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
157f4 2c 20 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a 23  , &debuginfo);.#
157f5 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70 50 61  endif..  if( pPa
157f6 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20  ge->intKey ){.  
157f7 20 20 75 38 20 2a 70 45 6e 64 3b 0a 20 20 20 20    u8 *pEnd;.    
157f8 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61  if( pPage->hasDa
157f9 74 61 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65  ta ){.      pIte
157fa 72 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32  r += getVarint32
157fb 28 70 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a  (pIter, nSize);.
157fc 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
157fd 20 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20   nSize = 0;.    
157fe 7d 0a 0a 20 20 20 20 2f 2a 20 70 49 74 65 72 20  }..    /* pIter 
157ff 6e 6f 77 20 70 6f 69 6e 74 73 20 61 74 20 74 68  now points at th
15800 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  e 64-bit integer
15801 20 6b 65 79 20 76 61 6c 75 65 2c 20 61 20 76 61   key value, a va
15802 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20  riable length . 
15803 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54     ** integer. T
15804 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f  he following blo
15805 63 6b 20 6d 6f 76 65 73 20 70 49 74 65 72 20 74  ck moves pIter t
15806 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 66  o point at the f
15807 69 72 73 74 20 62 79 74 65 0a 20 20 20 20 2a 2a  irst byte.    **
15808 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
15809 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20   the key value. 
1580a 2a 2f 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 70  */.    pEnd = &p
1580b 49 74 65 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69  Iter[9];.    whi
1580c 6c 65 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 30  le( (*pIter++)&0
1580d 78 38 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e  x80 && pIter<pEn
1580e 64 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  d );.  }else{.  
1580f 20 20 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61    pIter += getVa
15810 72 69 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53  rint32(pIter, nS
15811 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 74 65 73  ize);.  }..  tes
15812 74 63 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50  tcase( nSize==pP
15813 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b  age->maxLocal );
15814 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69  .  testcase( nSi
15815 7a 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f  ze==pPage->maxLo
15816 63 61 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e  cal+1 );.  if( n
15817 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c  Size>pPage->maxL
15818 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20  ocal ){.    int 
15819 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65  minLocal = pPage
1581a 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20  ->minLocal;.    
1581b 6e 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c  nSize = minLocal
1581c 20 2b 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c   + (nSize - minL
1581d 6f 63 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e  ocal) % (pPage->
1581e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
1581f 2d 20 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61  - 4);.    testca
15820 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65  se( nSize==pPage
15821 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20  ->maxLocal );.  
15822 20 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a    testcase( nSiz
15823 65 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63  e==pPage->maxLoc
15824 61 6c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20  al+1 );.    if( 
15825 6e 53 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78  nSize>pPage->max
15826 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e  Local ){.      n
15827 53 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b  Size = minLocal;
15828 0a 20 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65  .    }.    nSize
15829 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 6e 53 69   += 4;.  }.  nSi
1582a 7a 65 20 2b 3d 20 28 75 33 32 29 28 70 49 74 65  ze += (u32)(pIte
1582b 72 20 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f  r - pCell);..  /
1582c 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69  * The minimum si
1582d 7a 65 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69  ze of any cell i
1582e 73 20 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20  s 4 bytes. */.  
1582f 69 66 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20  if( nSize<4 ){. 
15830 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20     nSize = 4;.  
15831 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69  }..  assert( nSi
15832 7a 65 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53  ze==debuginfo.nS
15833 69 7a 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ize );.  return 
15834 28 75 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 0a 23  (u16)nSize;.}..#
15835 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
15836 55 47 0a 2f 2a 20 54 68 69 73 20 76 61 72 69 61  UG./* This varia
15837 74 69 6f 6e 20 6f 6e 20 63 65 6c 6c 53 69 7a 65  tion on cellSize
15838 50 74 72 28 29 20 69 73 20 75 73 65 64 20 69 6e  Ptr() is used in
15839 73 69 64 65 20 6f 66 20 61 73 73 65 72 74 28 29  side of assert()
1583a 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 6f   statements.** o
1583b 6e 6c 79 2e 20 2a 2f 0a 73 74 61 74 69 63 20 75  nly. */.static u
1583c 31 36 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50  16 cellSize(MemP
1583d 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20  age *pPage, int 
1583e 69 43 65 6c 6c 29 7b 0a 20 20 72 65 74 75 72 6e  iCell){.  return
1583f 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61   cellSizePtr(pPa
15840 67 65 2c 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  ge, findCell(pPa
15841 67 65 2c 20 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23  ge, iCell));.}.#
15842 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
15843 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
15844 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74  ACUUM./*.** If t
15845 68 65 20 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 70  he cell pCell, p
15846 61 72 74 20 6f 66 20 70 61 67 65 20 70 50 61 67  art of page pPag
15847 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69  e contains a poi
15848 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76  nter.** to an ov
15849 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73  erflow page, ins
1584a 65 72 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 74  ert an entry int
1584b 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  o the pointer-ma
1584c 70 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65  p.** for the ove
1584d 72 66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73  rflow page..*/.s
1584e 74 61 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61  tatic void ptrma
1584f 70 50 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50  pPutOvflPtr(MemP
15850 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a  age *pPage, u8 *
15851 70 43 65 6c 6c 2c 20 69 6e 74 20 2a 70 52 43 29  pCell, int *pRC)
15852 7b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66  {.  CellInfo inf
15853 6f 3b 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20  o;.  if( *pRC ) 
15854 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
15855 28 20 70 43 65 6c 6c 21 3d 30 20 29 3b 0a 20 20  ( pCell!=0 );.  
15856 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
15857 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20  r(pPage, pCell, 
15858 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74  &info);.  assert
15859 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70  ( (info.nData+(p
1585a 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69  Page->intKey?0:i
1585b 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 6f  nfo.nKey))==info
1585c 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 69  .nPayload );.  i
1585d 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  f( info.iOverflo
1585e 77 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76  w ){.    Pgno ov
1585f 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  fl = get4byte(&p
15860 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66  Cell[info.iOverf
15861 6c 6f 77 5d 29 3b 0a 20 20 20 20 70 74 72 6d 61  low]);.    ptrma
15862 70 50 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c  pPut(pPage->pBt,
15863 20 6f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56   ovfl, PTRMAP_OV
15864 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e  ERFLOW1, pPage->
15865 70 67 6e 6f 2c 20 70 52 43 29 3b 0a 20 20 7d 0a  pgno, pRC);.  }.
15866 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  }.#endif.../*.**
15867 20 44 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20   Defragment the 
15868 70 61 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c  page given.  All
15869 20 43 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64   Cells are moved
1586a 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f   to the.** end o
1586b 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20 61  f the page and a
1586c 6c 6c 20 66 72 65 65 20 73 70 61 63 65 20 69 73  ll free space is
1586d 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20   collected into 
1586e 6f 6e 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42  one.** big FreeB
1586f 6c 6b 20 74 68 61 74 20 6f 63 63 75 72 73 20 69  lk that occurs i
15870 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 68 65  n between the he
15871 61 64 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a  ader and cell.**
15872 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61   pointer array a
15873 6e 64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  nd the cell cont
15874 65 6e 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61  ent area..*/.sta
15875 74 69 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65  tic int defragme
15876 6e 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a  ntPage(MemPage *
15877 70 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b  pPage){.  int i;
15878 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15879 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
1587a 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63  nter */.  int pc
1587b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1587c 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
1587d 6f 66 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a  of a i-th cell *
1587e 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20  /.  int hdr;    
1587f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15880 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20  * Offset to the 
15881 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20  page header */. 
15882 20 69 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20   int size;      
15883 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
15884 69 7a 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f  ize of a cell */
15885 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a  .  int usableSiz
15886 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
15887 20 4e 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c   Number of usabl
15888 65 20 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67  e bytes on a pag
15889 65 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f  e */.  int cellO
1588a 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
1588b 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74    /* Offset to t
1588c 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  he cell pointer 
1588d 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63  array */.  int c
1588e 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  brk;            
1588f 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
15890 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  to the cell cont
15891 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e  ent area */.  in
15892 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20  t nCell;        
15893 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
15894 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74  er of cells on t
15895 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73  he page */.  uns
15896 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61  igned char *data
15897 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  ;       /* The p
15898 61 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e  age data */.  un
15899 73 69 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d  signed char *tem
1589a 70 3b 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70  p;       /* Temp
1589b 20 61 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63   area for cell c
1589c 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  ontent */.  int 
1589d 69 43 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 20  iCellFirst;     
1589e 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
1589f 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 69  allowable cell i
158a0 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ndex */.  int iC
158a1 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 20 20 20  ellLast;        
158a2 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73       /* Last pos
158a3 73 69 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78  sible cell index
158a4 20 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20   */...  assert( 
158a5 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
158a6 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
158a7 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73  DbPage) );.  ass
158a8 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21  ert( pPage->pBt!
158a9 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
158aa 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
158ab 6c 65 53 69 7a 65 20 3c 3d 20 53 51 4c 49 54 45  leSize <= SQLITE
158ac 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 29  _MAX_PAGE_SIZE )
158ad 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
158ae 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20  e->nOverflow==0 
158af 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
158b0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
158b1 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65  pPage->pBt->mute
158b2 78 29 20 29 3b 0a 20 20 74 65 6d 70 20 3d 20 73  x) );.  temp = s
158b3 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70 53  qlite3PagerTempS
158b4 70 61 63 65 28 70 50 61 67 65 2d 3e 70 42 74 2d  pace(pPage->pBt-
158b5 3e 70 50 61 67 65 72 29 3b 0a 20 20 64 61 74 61  >pPager);.  data
158b6 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
158b7 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  .  hdr = pPage->
158b8 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 63 65 6c  hdrOffset;.  cel
158b9 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d  lOffset = pPage-
158ba 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 6e  >cellOffset;.  n
158bb 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43  Cell = pPage->nC
158bc 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  ell;.  assert( n
158bd 43 65 6c 6c 3d 3d 67 65 74 32 62 79 74 65 28 26  Cell==get2byte(&
158be 64 61 74 61 5b 68 64 72 2b 33 5d 29 20 29 3b 0a  data[hdr+3]) );.
158bf 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70    usableSize = p
158c0 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
158c1 65 53 69 7a 65 3b 0a 20 20 63 62 72 6b 20 3d 20  eSize;.  cbrk = 
158c2 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
158c3 64 72 2b 35 5d 29 3b 0a 20 20 6d 65 6d 63 70 79  dr+5]);.  memcpy
158c4 28 26 74 65 6d 70 5b 63 62 72 6b 5d 2c 20 26 64  (&temp[cbrk], &d
158c5 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73 61 62 6c  ata[cbrk], usabl
158c6 65 53 69 7a 65 20 2d 20 63 62 72 6b 29 3b 0a 20  eSize - cbrk);. 
158c7 20 63 62 72 6b 20 3d 20 75 73 61 62 6c 65 53 69   cbrk = usableSi
158c8 7a 65 3b 0a 20 20 69 43 65 6c 6c 46 69 72 73 74  ze;.  iCellFirst
158c9 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20   = cellOffset + 
158ca 32 2a 6e 43 65 6c 6c 3b 0a 20 20 69 43 65 6c 6c  2*nCell;.  iCell
158cb 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 53 69 7a  Last = usableSiz
158cc 65 20 2d 20 34 3b 0a 20 20 66 6f 72 28 69 3d 30  e - 4;.  for(i=0
158cd 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b  ; i<nCell; i++){
158ce 0a 20 20 20 20 75 38 20 2a 70 41 64 64 72 3b 20  .    u8 *pAddr; 
158cf 20 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20      /* The i-th 
158d0 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  cell pointer */.
158d1 20 20 20 20 70 41 64 64 72 20 3d 20 26 64 61 74      pAddr = &dat
158d2 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 69  a[cellOffset + i
158d3 2a 32 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 65  *2];.    pc = ge
158d4 74 32 62 79 74 65 28 70 41 64 64 72 29 3b 0a 20  t2byte(pAddr);. 
158d5 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d     testcase( pc=
158d6 3d 69 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20  =iCellFirst );. 
158d7 20 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d     testcase( pc=
158d8 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 23 69  =iCellLast );.#i
158d9 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
158da 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a  E_ENABLE_OVERSIZ
158db 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20  E_CELL_CHECK).  
158dc 20 20 2f 2a 20 54 68 65 73 65 20 63 6f 6e 64 69    /* These condi
158dd 74 69 6f 6e 73 20 68 61 76 65 20 61 6c 72 65 61  tions have alrea
158de 64 79 20 62 65 65 6e 20 76 65 72 69 66 69 65 64  dy been verified
158df 20 69 6e 20 62 74 72 65 65 49 6e 69 74 50 61 67   in btreeInitPag
158e0 65 28 29 0a 20 20 20 20 2a 2a 20 69 66 20 53 51  e().    ** if SQ
158e1 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52  LITE_ENABLE_OVER
158e2 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 20  SIZE_CELL_CHECK 
158e3 69 73 20 64 65 66 69 6e 65 64 20 0a 20 20 20 20  is defined .    
158e4 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 3c 69 43  */.    if( pc<iC
158e5 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69  ellFirst || pc>i
158e6 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20  CellLast ){.    
158e7 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
158e8 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
158e9 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 61    }.#endif.    a
158ea 73 73 65 72 74 28 20 70 63 3e 3d 69 43 65 6c 6c  ssert( pc>=iCell
158eb 46 69 72 73 74 20 26 26 20 70 63 3c 3d 69 43 65  First && pc<=iCe
158ec 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20 73 69  llLast );.    si
158ed 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72  ze = cellSizePtr
158ee 28 70 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63  (pPage, &temp[pc
158ef 5d 29 3b 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20  ]);.    cbrk -= 
158f0 73 69 7a 65 3b 0a 23 69 66 20 64 65 66 69 6e 65  size;.#if define
158f1 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
158f2 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48  OVERSIZE_CELL_CH
158f3 45 43 4b 29 0a 20 20 20 20 69 66 28 20 63 62 72  ECK).    if( cbr
158f4 6b 3c 69 43 65 6c 6c 46 69 72 73 74 20 29 7b 0a  k<iCellFirst ){.
158f5 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
158f6 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
158f7 3b 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20  ;.    }.#else.  
158f8 20 20 69 66 28 20 63 62 72 6b 3c 69 43 65 6c 6c    if( cbrk<iCell
158f9 46 69 72 73 74 20 7c 7c 20 70 63 2b 73 69 7a 65  First || pc+size
158fa 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20  >usableSize ){. 
158fb 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
158fc 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
158fd 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
158fe 20 20 61 73 73 65 72 74 28 20 63 62 72 6b 2b 73    assert( cbrk+s
158ff 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20  ize<=usableSize 
15900 26 26 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69  && cbrk>=iCellFi
15901 72 73 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63  rst );.    testc
15902 61 73 65 28 20 63 62 72 6b 2b 73 69 7a 65 3d 3d  ase( cbrk+size==
15903 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20  usableSize );.  
15904 20 20 74 65 73 74 63 61 73 65 28 20 70 63 2b 73    testcase( pc+s
15905 69 7a 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20  ize==usableSize 
15906 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64  );.    memcpy(&d
15907 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74 65 6d 70  ata[cbrk], &temp
15908 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20  [pc], size);.   
15909 20 70 75 74 32 62 79 74 65 28 70 41 64 64 72 2c   put2byte(pAddr,
1590a 20 63 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73   cbrk);.  }.  as
1590b 73 65 72 74 28 20 63 62 72 6b 3e 3d 69 43 65 6c  sert( cbrk>=iCel
1590c 6c 46 69 72 73 74 20 29 3b 0a 20 20 70 75 74 32  lFirst );.  put2
1590d 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
1590e 5d 2c 20 63 62 72 6b 29 3b 0a 20 20 64 61 74 61  ], cbrk);.  data
1590f 5b 68 64 72 2b 31 5d 20 3d 20 30 3b 0a 20 20 64  [hdr+1] = 0;.  d
15910 61 74 61 5b 68 64 72 2b 32 5d 20 3d 20 30 3b 0a  ata[hdr+2] = 0;.
15911 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20    data[hdr+7] = 
15912 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74  0;.  memset(&dat
15913 61 5b 69 43 65 6c 6c 46 69 72 73 74 5d 2c 20 30  a[iCellFirst], 0
15914 2c 20 63 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73  , cbrk-iCellFirs
15915 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  t);.  assert( sq
15916 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
15917 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
15918 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20 63  Page) );.  if( c
15919 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73 74 21 3d  brk-iCellFirst!=
1591a 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a  pPage->nFree ){.
1591b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1591c 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
1591d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
1591e 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
1591f 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20   Allocate nByte 
15920 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66  bytes of space f
15921 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 42  rom within the B
15922 2d 54 72 65 65 20 70 61 67 65 20 70 61 73 73 65  -Tree page passe
15923 64 0a 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73  d.** as the firs
15924 74 20 61 72 67 75 6d 65 6e 74 2e 20 57 72 69 74  t argument. Writ
15925 65 20 69 6e 74 6f 20 2a 70 49 64 78 20 74 68 65  e into *pIdx the
15926 20 69 6e 64 65 78 20 69 6e 74 6f 20 70 50 61 67   index into pPag
15927 65 2d 3e 61 44 61 74 61 5b 5d 0a 2a 2a 20 6f 66  e->aData[].** of
15928 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
15929 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61  of allocated spa
1592a 63 65 2e 20 52 65 74 75 72 6e 20 65 69 74 68 65  ce. Return eithe
1592b 72 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 0a 2a  r SQLITE_OK or.*
1592c 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  * an error code 
1592d 28 75 73 75 61 6c 6c 79 20 53 51 4c 49 54 45 5f  (usually SQLITE_
1592e 43 4f 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20  CORRUPT)..**.** 
1592f 54 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61  The caller guara
15930 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 72 65  ntees that there
15931 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20 73   is sufficient s
15932 70 61 63 65 20 74 6f 20 6d 61 6b 65 20 74 68 65  pace to make the
15933 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  .** allocation. 
15934 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69   This routine mi
15935 67 68 74 20 6e 65 65 64 20 74 6f 20 64 65 66 72  ght need to defr
15936 61 67 6d 65 6e 74 20 69 6e 20 6f 72 64 65 72 20  agment in order 
15937 74 6f 20 62 72 69 6e 67 0a 2a 2a 20 61 6c 6c 20  to bring.** all 
15938 74 68 65 20 73 70 61 63 65 20 74 6f 67 65 74 68  the space togeth
15939 65 72 2c 20 68 6f 77 65 76 65 72 2e 20 20 54 68  er, however.  Th
1593a 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
1593b 61 76 6f 69 64 20 75 73 69 6e 67 0a 2a 2a 20 74  avoid using.** t
1593c 68 65 20 66 69 72 73 74 20 74 77 6f 20 62 79 74  he first two byt
1593d 65 73 20 70 61 73 74 20 74 68 65 20 63 65 6c 6c  es past the cell
1593e 20 70 6f 69 6e 74 65 72 20 61 72 65 61 20 73 69   pointer area si
1593f 6e 63 65 20 70 72 65 73 75 6d 61 62 6c 79 20 74  nce presumably t
15940 68 69 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f  his.** allocatio
15941 6e 20 69 73 20 62 65 69 6e 67 20 6d 61 64 65 20  n is being made 
15942 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 73 65  in order to inse
15943 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 2c 20 73  rt a new cell, s
15944 6f 20 77 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73  o we will.** als
15945 6f 20 65 6e 64 20 75 70 20 6e 65 65 64 69 6e 67  o end up needing
15946 20 61 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e   a new cell poin
15947 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ter..*/.static i
15948 6e 74 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65  nt allocateSpace
15949 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
1594a 20 69 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 20   int nByte, int 
1594b 2a 70 49 64 78 29 7b 0a 20 20 63 6f 6e 73 74 20  *pIdx){.  const 
1594c 69 6e 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d  int hdr = pPage-
1594d 3e 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 2f  >hdrOffset;    /
1594e 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66  * Local cache of
1594f 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
15950 74 20 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73  t */.  u8 * cons
15951 74 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e  t data = pPage->
15952 61 44 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 4c  aData;      /* L
15953 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50  ocal cache of pP
15954 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20  age->aData */.  
15955 69 6e 74 20 6e 46 72 61 67 3b 20 20 20 20 20 20  int nFrag;      
15956 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15957 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
15958 66 20 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74  f fragmented byt
15959 65 73 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20  es on pPage */. 
1595a 20 69 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20   int top;       
1595b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1595c 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62        /* First b
1595d 79 74 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74  yte of cell cont
1595e 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e  ent area */.  in
1595f 74 20 67 61 70 3b 20 20 20 20 20 20 20 20 2f 2a  t gap;        /*
15960 20 46 69 72 73 74 20 62 79 74 65 20 6f 66 20 67   First byte of g
15961 61 70 20 62 65 74 77 65 65 6e 20 63 65 6c 6c 20  ap between cell 
15962 70 6f 69 6e 74 65 72 73 20 61 6e 64 20 63 65 6c  pointers and cel
15963 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69  l content */.  i
15964 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f  nt rc;         /
15965 2a 20 49 6e 74 65 67 65 72 20 72 65 74 75 72 6e  * Integer return
15966 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 75   code */.  int u
15967 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20 55 73  sableSize; /* Us
15968 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 74 68 65  able size of the
15969 20 70 61 67 65 20 2a 2f 0a 20 20 0a 20 20 61 73   page */.  .  as
1596a 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
1596b 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
1596c 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
1596d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
1596e 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72  ->pBt );.  asser
1596f 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
15970 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
15971 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
15972 73 65 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 29  sert( nByte>=0 )
15973 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65  ;  /* Minimum ce
15974 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a  ll size is 4 */.
15975 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15976 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b  >nFree>=nByte );
15977 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
15978 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29  ->nOverflow==0 )
15979 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d  ;.  usableSize =
1597a 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61   pPage->pBt->usa
1597b 62 6c 65 53 69 7a 65 3b 0a 20 20 61 73 73 65 72  bleSize;.  asser
1597c 74 28 20 6e 42 79 74 65 20 3c 20 75 73 61 62 6c  t( nByte < usabl
1597d 65 53 69 7a 65 2d 38 20 29 3b 0a 0a 20 20 6e 46  eSize-8 );..  nF
1597e 72 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b 37  rag = data[hdr+7
1597f 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ];.  assert( pPa
15980 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d  ge->cellOffset =
15981 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70  = hdr + 12 - 4*p
15982 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  Page->leaf );.  
15983 67 61 70 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c  gap = pPage->cel
15984 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67  lOffset + 2*pPag
15985 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 74 6f 70 20  e->nCell;.  top 
15986 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
15987 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 69 66 28 20  [hdr+5]);.  if( 
15988 67 61 70 3e 74 6f 70 20 29 20 72 65 74 75 72 6e  gap>top ) return
15989 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1598a 42 4b 50 54 3b 0a 20 20 74 65 73 74 63 61 73 65  BKPT;.  testcase
1598b 28 20 67 61 70 2b 32 3d 3d 74 6f 70 20 29 3b 0a  ( gap+2==top );.
1598c 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70 2b    testcase( gap+
1598d 31 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73 74  1==top );.  test
1598e 63 61 73 65 28 20 67 61 70 3d 3d 74 6f 70 20 29  case( gap==top )
1598f 3b 0a 0a 20 20 69 66 28 20 6e 46 72 61 67 3e 3d  ;..  if( nFrag>=
15990 36 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 77  60 ){.    /* Alw
15991 61 79 73 20 64 65 66 72 61 67 6d 65 6e 74 20 68  ays defragment h
15992 69 67 68 6c 79 20 66 72 61 67 6d 65 6e 74 65 64  ighly fragmented
15993 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 72 63   pages */.    rc
15994 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67   = defragmentPag
15995 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66  e(pPage);.    if
15996 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
15997 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32  ;.    top = get2
15998 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
15999 5d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ]);.  }else if( 
1599a 67 61 70 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20 20  gap+2<=top ){.  
1599b 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20    /* Search the 
1599c 66 72 65 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e 67  freelist looking
1599d 20 66 6f 72 20 61 20 66 72 65 65 20 73 6c 6f 74   for a free slot
1599e 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 73   big enough to s
1599f 61 74 69 73 66 79 20 0a 20 20 20 20 2a 2a 20 74  atisfy .    ** t
159a0 68 65 20 72 65 71 75 65 73 74 2e 20 54 68 65 20  he request. The 
159a1 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 6d 61  allocation is ma
159a2 64 65 20 66 72 6f 6d 20 74 68 65 20 66 69 72 73  de from the firs
159a3 74 20 66 72 65 65 20 73 6c 6f 74 20 69 6e 20 0a  t free slot in .
159a4 20 20 20 20 2a 2a 20 74 68 65 20 6c 69 73 74 20      ** the list 
159a5 74 68 61 74 20 69 73 20 6c 61 72 67 65 20 65 6e  that is large en
159a6 6f 75 67 68 20 74 6f 20 61 63 63 6f 6d 61 64 61  ough to accomada
159a7 74 65 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  te it..    */.  
159a8 20 20 69 6e 74 20 70 63 2c 20 61 64 64 72 3b 0a    int pc, addr;.
159a9 20 20 20 20 66 6f 72 28 61 64 64 72 3d 68 64 72      for(addr=hdr
159aa 2b 31 3b 20 28 70 63 20 3d 20 67 65 74 32 62 79  +1; (pc = get2by
159ab 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29  te(&data[addr]))
159ac 3e 30 3b 20 61 64 64 72 3d 70 63 29 7b 0a 20 20  >0; addr=pc){.  
159ad 20 20 20 20 69 6e 74 20 73 69 7a 65 3b 20 20 20      int size;   
159ae 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
159af 20 6f 66 20 74 68 65 20 66 72 65 65 20 73 6c 6f   of the free slo
159b0 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70  t */.      if( p
159b1 63 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 7c  c>usableSize-4 |
159b2 7c 20 70 63 3c 61 64 64 72 2b 34 20 29 7b 0a 20  | pc<addr+4 ){. 
159b3 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
159b4 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
159b5 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  T;.      }.     
159b6 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65   size = get2byte
159b7 28 26 64 61 74 61 5b 70 63 2b 32 5d 29 3b 0a 20  (&data[pc+2]);. 
159b8 20 20 20 20 20 69 66 28 20 73 69 7a 65 3e 3d 6e       if( size>=n
159b9 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Byte ){.        
159ba 69 6e 74 20 78 20 3d 20 73 69 7a 65 20 2d 20 6e  int x = size - n
159bb 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 74 65  Byte;.        te
159bc 73 74 63 61 73 65 28 20 78 3d 3d 34 20 29 3b 0a  stcase( x==4 );.
159bd 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
159be 28 20 78 3d 3d 33 20 29 3b 0a 20 20 20 20 20 20  ( x==3 );.      
159bf 20 20 69 66 28 20 78 3c 34 20 29 7b 0a 20 20 20    if( x<4 ){.   
159c0 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65         /* Remove
159c1 20 74 68 65 20 73 6c 6f 74 20 66 72 6f 6d 20 74   the slot from t
159c2 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 55 70  he free-list. Up
159c3 64 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  date the number 
159c4 6f 66 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  of.          ** 
159c5 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73  fragmented bytes
159c6 20 77 69 74 68 69 6e 20 74 68 65 20 70 61 67 65   within the page
159c7 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 6d  . */.          m
159c8 65 6d 63 70 79 28 26 64 61 74 61 5b 61 64 64 72  emcpy(&data[addr
159c9 5d 2c 20 26 64 61 74 61 5b 70 63 5d 2c 20 32 29  ], &data[pc], 2)
159ca 3b 0a 20 20 20 20 20 20 20 20 20 20 64 61 74 61  ;.          data
159cb 5b 68 64 72 2b 37 5d 20 3d 20 28 75 38 29 28 6e  [hdr+7] = (u8)(n
159cc 46 72 61 67 20 2b 20 78 29 3b 0a 20 20 20 20 20  Frag + x);.     
159cd 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 69 7a     }else if( siz
159ce 65 2b 70 63 20 3e 20 75 73 61 62 6c 65 53 69 7a  e+pc > usableSiz
159cf 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  e ){.          r
159d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
159d1 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
159d2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
159d3 20 20 20 20 2f 2a 20 54 68 65 20 73 6c 6f 74 20      /* The slot 
159d4 72 65 6d 61 69 6e 73 20 6f 6e 20 74 68 65 20 66  remains on the f
159d5 72 65 65 2d 6c 69 73 74 2e 20 52 65 64 75 63 65  ree-list. Reduce
159d6 20 69 74 73 20 73 69 7a 65 20 74 6f 20 61 63 63   its size to acc
159d7 6f 75 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a  ount.          *
159d8 2a 20 66 6f 72 20 74 68 65 20 70 6f 72 74 69 6f  * for the portio
159d9 6e 20 75 73 65 64 20 62 79 20 74 68 65 20 6e 65  n used by the ne
159da 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 2a 2f  w allocation. */
159db 0a 20 20 20 20 20 20 20 20 20 20 70 75 74 32 62  .          put2b
159dc 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 2c  yte(&data[pc+2],
159dd 20 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   x);.        }. 
159de 20 20 20 20 20 20 20 2a 70 49 64 78 20 3d 20 70         *pIdx = p
159df 63 20 2b 20 78 3b 0a 20 20 20 20 20 20 20 20 72  c + x;.        r
159e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
159e1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
159e2 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74   }..  /* Check t
159e3 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 72  o make sure ther
159e4 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61 63  e is enough spac
159e5 65 20 69 6e 20 74 68 65 20 67 61 70 20 74 6f 20  e in the gap to 
159e6 73 61 74 69 73 66 79 0a 20 20 2a 2a 20 74 68 65  satisfy.  ** the
159e7 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 49 66   allocation.  If
159e8 20 6e 6f 74 2c 20 64 65 66 72 61 67 6d 65 6e 74   not, defragment
159e9 2e 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73  ..  */.  testcas
159ea 65 28 20 67 61 70 2b 32 2b 6e 42 79 74 65 3d 3d  e( gap+2+nByte==
159eb 74 6f 70 20 29 3b 0a 20 20 69 66 28 20 67 61 70  top );.  if( gap
159ec 2b 32 2b 6e 42 79 74 65 3e 74 6f 70 20 29 7b 0a  +2+nByte>top ){.
159ed 20 20 20 20 72 63 20 3d 20 64 65 66 72 61 67 6d      rc = defragm
159ee 65 6e 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a  entPage(pPage);.
159ef 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
159f0 75 72 6e 20 72 63 3b 0a 20 20 20 20 74 6f 70 20  urn rc;.    top 
159f1 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
159f2 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 61 73  [hdr+5]);.    as
159f3 73 65 72 74 28 20 67 61 70 2b 6e 42 79 74 65 3c  sert( gap+nByte<
159f4 3d 74 6f 70 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20  =top );.  }...  
159f5 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  /* Allocate memo
159f6 72 79 20 66 72 6f 6d 20 74 68 65 20 67 61 70 20  ry from the gap 
159f7 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65 20 63  in between the c
159f8 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61  ell pointer arra
159f9 79 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 63  y.  ** and the c
159fa 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61  ell content area
159fb 2e 20 20 54 68 65 20 62 74 72 65 65 49 6e 69 74  .  The btreeInit
159fc 50 61 67 65 28 29 20 63 61 6c 6c 20 68 61 73 20  Page() call has 
159fd 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 76 61 6c  already.  ** val
159fe 69 64 61 74 65 64 20 74 68 65 20 66 72 65 65 6c  idated the freel
159ff 69 73 74 2e 20 20 47 69 76 65 6e 20 74 68 61 74  ist.  Given that
15a00 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 73   the freelist is
15a01 20 76 61 6c 69 64 2c 20 74 68 65 72 65 0a 20 20   valid, there.  
15a02 2a 2a 20 69 73 20 6e 6f 20 77 61 79 20 74 68 61  ** is no way tha
15a03 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  t the allocation
15a04 20 63 61 6e 20 65 78 74 65 6e 64 20 6f 66 66 20   can extend off 
15a05 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70  the end of the p
15a06 61 67 65 2e 0a 20 20 2a 2a 20 54 68 65 20 61 73  age..  ** The as
15a07 73 65 72 74 28 29 20 62 65 6c 6f 77 20 76 65 72  sert() below ver
15a08 69 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f  ifies the previo
15a09 75 73 20 73 65 6e 74 65 6e 63 65 2e 0a 20 20 2a  us sentence..  *
15a0a 2f 0a 20 20 74 6f 70 20 2d 3d 20 6e 42 79 74 65  /.  top -= nByte
15a0b 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
15a0c 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 29 3b  ta[hdr+5], top);
15a0d 0a 20 20 61 73 73 65 72 74 28 20 74 6f 70 2b 6e  .  assert( top+n
15a0e 42 79 74 65 20 3c 3d 20 70 50 61 67 65 2d 3e 70  Byte <= pPage->p
15a0f 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29  Bt->usableSize )
15a10 3b 0a 20 20 2a 70 49 64 78 20 3d 20 74 6f 70 3b  ;.  *pIdx = top;
15a11 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
15a12 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  _OK;.}../*.** Re
15a13 74 75 72 6e 20 61 20 73 65 63 74 69 6f 6e 20 6f  turn a section o
15a14 66 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44 61  f the pPage->aDa
15a15 74 61 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69  ta to the freeli
15a16 73 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  st..** The first
15a17 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 77   byte of the new
15a18 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 70   free block is p
15a19 50 61 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61 72  Page->aDisk[star
15a1a 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 69  t].** and the si
15a1b 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b 20  ze of the block 
15a1c 69 73 20 22 73 69 7a 65 22 20 62 79 74 65 73 2e  is "size" bytes.
15a1d 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74  .**.** Most of t
15a1e 68 65 20 65 66 66 6f 72 74 20 68 65 72 65 20 69  he effort here i
15a1f 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63 6f  s involved in co
15a20 61 6c 65 73 69 6e 67 20 61 64 6a 61 63 65 6e 74  alesing adjacent
15a21 0a 2a 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73 20  .** free blocks 
15a22 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 62 69  into a single bi
15a23 67 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a 2f  g free block..*/
15a24 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65  .static int free
15a25 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a 70  Space(MemPage *p
15a26 50 61 67 65 2c 20 69 6e 74 20 73 74 61 72 74 2c  Page, int start,
15a27 20 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69 6e   int size){.  in
15a28 74 20 61 64 64 72 2c 20 70 62 65 67 69 6e 2c 20  t addr, pbegin, 
15a29 68 64 72 3b 0a 20 20 69 6e 74 20 69 4c 61 73 74  hdr;.  int iLast
15a2a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15a2b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67           /* Larg
15a2c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 66 72 65  est possible fre
15a2d 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74 20 2a 2f  eblock offset */
15a2e 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
15a2f 20 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e   *data = pPage->
15a30 61 44 61 74 61 3b 0a 0a 20 20 61 73 73 65 72 74  aData;..  assert
15a31 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20  ( pPage->pBt!=0 
15a32 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
15a33 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
15a34 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50  able(pPage->pDbP
15a35 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74  age) );.  assert
15a36 28 20 73 74 61 72 74 3e 3d 70 50 61 67 65 2d 3e  ( start>=pPage->
15a37 68 64 72 4f 66 66 73 65 74 2b 36 2b 70 50 61 67  hdrOffset+6+pPag
15a38 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20  e->childPtrSize 
15a39 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 73 74  );.  assert( (st
15a3a 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d 70 50 61  art + size)<=pPa
15a3b 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
15a3c 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ize );.  assert(
15a3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15a3e 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
15a3f 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
15a40 72 74 28 20 73 69 7a 65 3e 3d 30 20 29 3b 20 20  rt( size>=0 );  
15a41 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c   /* Minimum cell
15a42 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 0a 23   size is 4 */..#
15a43 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43  ifdef SQLITE_SEC
15a44 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20  URE_DELETE.  /* 
15a45 4f 76 65 72 77 72 69 74 65 20 64 65 6c 65 74 65  Overwrite delete
15a46 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69  d information wi
15a47 74 68 20 7a 65 72 6f 73 20 77 68 65 6e 20 74 68  th zeros when th
15a48 65 20 53 45 43 55 52 45 5f 44 45 4c 45 54 45 20  e SECURE_DELETE 
15a49 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20  .  ** option is 
15a4a 65 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69  enabled at compi
15a4b 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20 6d 65 6d  le-time */.  mem
15a4c 73 65 74 28 26 64 61 74 61 5b 73 74 61 72 74 5d  set(&data[start]
15a4d 2c 20 30 2c 20 73 69 7a 65 29 3b 0a 23 65 6e 64  , 0, size);.#end
15a4e 69 66 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65  if..  /* Add the
15a4f 20 73 70 61 63 65 20 62 61 63 6b 20 69 6e 74 6f   space back into
15a50 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
15a51 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20   of freeblocks. 
15a52 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a 2a 20   Note that.  ** 
15a53 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 20  even though the 
15a54 66 72 65 65 62 6c 6f 63 6b 20 6c 69 73 74 20 77  freeblock list w
15a55 61 73 20 63 68 65 63 6b 65 64 20 62 79 20 62 74  as checked by bt
15a56 72 65 65 49 6e 69 74 50 61 67 65 28 29 2c 0a 20  reeInitPage(),. 
15a57 20 2a 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67   ** btreeInitPag
15a58 65 28 29 20 64 69 64 20 6e 6f 74 20 64 65 74 65  e() did not dete
15a59 63 74 20 6f 76 65 72 6c 61 70 70 69 6e 67 20 63  ct overlapping c
15a5a 65 6c 6c 73 20 6f 72 0a 20 20 2a 2a 20 66 72 65  ells or.  ** fre
15a5b 65 62 6c 6f 63 6b 73 20 74 68 61 74 20 6f 76 65  eblocks that ove
15a5c 72 6c 61 70 70 65 64 20 63 65 6c 6c 73 2e 20 20  rlapped cells.  
15a5d 20 4e 6f 72 20 64 6f 65 73 20 69 74 20 64 65 74   Nor does it det
15a5e 65 63 74 20 77 68 65 6e 20 74 68 65 0a 20 20 2a  ect when the.  *
15a5f 2a 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  * cell content a
15a60 72 65 61 20 65 78 63 65 65 64 73 20 74 68 65 20  rea exceeds the 
15a61 76 61 6c 75 65 20 69 6e 20 74 68 65 20 70 61 67  value in the pag
15a62 65 20 68 65 61 64 65 72 2e 20 20 49 66 20 74 68  e header.  If th
15a63 65 73 65 0a 20 20 2a 2a 20 73 69 74 75 61 74 69  ese.  ** situati
15a64 6f 6e 73 20 61 72 69 73 65 2c 20 74 68 65 6e 20  ons arise, then 
15a65 73 75 62 73 65 71 75 65 6e 74 20 69 6e 73 65 72  subsequent inser
15a66 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 6d 69 67  t operations mig
15a67 68 74 20 63 6f 72 72 75 70 74 0a 20 20 2a 2a 20  ht corrupt.  ** 
15a68 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20 53  the freelist.  S
15a69 6f 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20  o we do need to 
15a6a 63 68 65 63 6b 20 66 6f 72 20 63 6f 72 72 75 70  check for corrup
15a6b 74 69 6f 6e 20 77 68 69 6c 65 20 73 63 61 6e 6e  tion while scann
15a6c 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 66 72 65  ing.  ** the fre
15a6d 65 6c 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 68 64  elist..  */.  hd
15a6e 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
15a6f 66 73 65 74 3b 0a 20 20 61 64 64 72 20 3d 20 68  fset;.  addr = h
15a70 64 72 20 2b 20 31 3b 0a 20 20 69 4c 61 73 74 20  dr + 1;.  iLast 
15a71 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  = pPage->pBt->us
15a72 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20  ableSize - 4;.  
15a73 61 73 73 65 72 74 28 20 73 74 61 72 74 3c 3d 69  assert( start<=i
15a74 4c 61 73 74 20 29 3b 0a 20 20 77 68 69 6c 65 28  Last );.  while(
15a75 20 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62   (pbegin = get2b
15a76 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29  yte(&data[addr])
15a77 29 3c 73 74 61 72 74 20 26 26 20 70 62 65 67 69  )<start && pbegi
15a78 6e 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70  n>0 ){.    if( p
15a79 62 65 67 69 6e 3c 61 64 64 72 2b 34 20 29 7b 0a  begin<addr+4 ){.
15a7a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15a7b 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15a7c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72  ;.    }.    addr
15a7d 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20 7d 0a 20   = pbegin;.  }. 
15a7e 20 69 66 28 20 70 62 65 67 69 6e 3e 69 4c 61 73   if( pbegin>iLas
15a7f 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
15a80 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
15a81 4b 50 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  KPT;.  }.  asser
15a82 74 28 20 70 62 65 67 69 6e 3e 61 64 64 72 20 7c  t( pbegin>addr |
15a83 7c 20 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a 20  | pbegin==0 );. 
15a84 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
15a85 61 64 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a 20  addr], start);. 
15a86 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
15a87 73 74 61 72 74 5d 2c 20 70 62 65 67 69 6e 29 3b  start], pbegin);
15a88 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
15a89 61 5b 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a 65  a[start+2], size
15a8a 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  );.  pPage->nFre
15a8b 65 20 3d 20 70 50 61 67 65 2d 3e 6e 46 72 65 65  e = pPage->nFree
15a8c 20 2b 20 28 75 31 36 29 73 69 7a 65 3b 0a 0a 20   + (u16)size;.. 
15a8d 20 2f 2a 20 43 6f 61 6c 65 73 63 65 20 61 64 6a   /* Coalesce adj
15a8e 61 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b  acent free block
15a8f 73 20 2a 2f 0a 20 20 61 64 64 72 20 3d 20 68 64  s */.  addr = hd
15a90 72 20 2b 20 31 3b 0a 20 20 77 68 69 6c 65 28 20  r + 1;.  while( 
15a91 28 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79  (pbegin = get2by
15a92 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29 29  te(&data[addr]))
15a93 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 6e  >0 ){.    int pn
15a94 65 78 74 2c 20 70 73 69 7a 65 2c 20 78 3b 0a 20  ext, psize, x;. 
15a95 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69     assert( pbegi
15a96 6e 3e 61 64 64 72 20 29 3b 0a 20 20 20 20 61 73  n>addr );.    as
15a97 73 65 72 74 28 20 70 62 65 67 69 6e 3c 3d 70 50  sert( pbegin<=pP
15a98 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
15a99 53 69 7a 65 2d 34 20 29 3b 0a 20 20 20 20 70 6e  Size-4 );.    pn
15a9a 65 78 74 20 3d 20 67 65 74 32 62 79 74 65 28 26  ext = get2byte(&
15a9b 64 61 74 61 5b 70 62 65 67 69 6e 5d 29 3b 0a 20  data[pbegin]);. 
15a9c 20 20 20 70 73 69 7a 65 20 3d 20 67 65 74 32 62     psize = get2b
15a9d 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
15a9e 2b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 62  +2]);.    if( pb
15a9f 65 67 69 6e 20 2b 20 70 73 69 7a 65 20 2b 20 33  egin + psize + 3
15aa0 20 3e 3d 20 70 6e 65 78 74 20 26 26 20 70 6e 65   >= pnext && pne
15aa1 78 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  xt>0 ){.      in
15aa2 74 20 66 72 61 67 20 3d 20 70 6e 65 78 74 20 2d  t frag = pnext -
15aa3 20 28 70 62 65 67 69 6e 2b 70 73 69 7a 65 29 3b   (pbegin+psize);
15aa4 0a 20 20 20 20 20 20 69 66 28 20 28 66 72 61 67  .      if( (frag
15aa5 3c 30 29 20 7c 7c 20 28 66 72 61 67 3e 28 69 6e  <0) || (frag>(in
15aa6 74 29 64 61 74 61 5b 68 64 72 2b 37 5d 29 20 29  t)data[hdr+7]) )
15aa7 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
15aa8 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15aa9 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20  BKPT;.      }.  
15aaa 20 20 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20      data[hdr+7] 
15aab 2d 3d 20 28 75 38 29 66 72 61 67 3b 0a 20 20 20  -= (u8)frag;.   
15aac 20 20 20 78 20 3d 20 67 65 74 32 62 79 74 65 28     x = get2byte(
15aad 26 64 61 74 61 5b 70 6e 65 78 74 5d 29 3b 0a 20  &data[pnext]);. 
15aae 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64       put2byte(&d
15aaf 61 74 61 5b 70 62 65 67 69 6e 5d 2c 20 78 29 3b  ata[pbegin], x);
15ab0 0a 20 20 20 20 20 20 78 20 3d 20 70 6e 65 78 74  .      x = pnext
15ab1 20 2b 20 67 65 74 32 62 79 74 65 28 26 64 61 74   + get2byte(&dat
15ab2 61 5b 70 6e 65 78 74 2b 32 5d 29 20 2d 20 70 62  a[pnext+2]) - pb
15ab3 65 67 69 6e 3b 0a 20 20 20 20 20 20 70 75 74 32  egin;.      put2
15ab4 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69  byte(&data[pbegi
15ab5 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 7d 65  n+2], x);.    }e
15ab6 6c 73 65 7b 0a 20 20 20 20 20 20 61 64 64 72 20  lse{.      addr 
15ab7 3d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 7d 0a  = pbegin;.    }.
15ab8 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
15ab9 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72   cell content ar
15aba 65 61 20 62 65 67 69 6e 73 20 77 69 74 68 20 61  ea begins with a
15abb 20 66 72 65 65 62 6c 6f 63 6b 2c 20 72 65 6d 6f   freeblock, remo
15abc 76 65 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20  ve it. */.  if( 
15abd 64 61 74 61 5b 68 64 72 2b 31 5d 3d 3d 64 61 74  data[hdr+1]==dat
15abe 61 5b 68 64 72 2b 35 5d 20 26 26 20 64 61 74 61  a[hdr+5] && data
15abf 5b 68 64 72 2b 32 5d 3d 3d 64 61 74 61 5b 68 64  [hdr+2]==data[hd
15ac0 72 2b 36 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20  r+6] ){.    int 
15ac1 74 6f 70 3b 0a 20 20 20 20 70 62 65 67 69 6e 20  top;.    pbegin 
15ac2 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
15ac3 5b 68 64 72 2b 31 5d 29 3b 0a 20 20 20 20 6d 65  [hdr+1]);.    me
15ac4 6d 63 70 79 28 26 64 61 74 61 5b 68 64 72 2b 31  mcpy(&data[hdr+1
15ac5 5d 2c 20 26 64 61 74 61 5b 70 62 65 67 69 6e 5d  ], &data[pbegin]
15ac6 2c 20 32 29 3b 0a 20 20 20 20 74 6f 70 20 3d 20  , 2);.    top = 
15ac7 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
15ac8 64 72 2b 35 5d 29 20 2b 20 67 65 74 32 62 79 74  dr+5]) + get2byt
15ac9 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 2b 32  e(&data[pbegin+2
15aca 5d 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65  ]);.    put2byte
15acb 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74  (&data[hdr+5], t
15acc 6f 70 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  op);.  }.  asser
15acd 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
15ace 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65  swriteable(pPage
15acf 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
15ad0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
15ad1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6f 64  ;.}../*.** Decod
15ad2 65 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65  e the flags byte
15ad3 20 28 74 68 65 20 66 69 72 73 74 20 62 79 74 65   (the first byte
15ad4 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 29 20   of the header) 
15ad5 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a 20 61 6e  for a page.** an
15ad6 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 66 69 65  d initialize fie
15ad7 6c 64 73 20 6f 66 20 74 68 65 20 4d 65 6d 50 61  lds of the MemPa
15ad8 67 65 20 73 74 72 75 63 74 75 72 65 20 61 63 63  ge structure acc
15ad9 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a 2a 20  ordingly..**.** 
15ada 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69  Only the followi
15adb 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20  ng combinations 
15adc 61 72 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20  are supported.  
15add 41 6e 79 74 68 69 6e 67 20 64 69 66 66 65 72 65  Anything differe
15ade 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20  nt.** indicates 
15adf 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61  a corrupt databa
15ae0 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20  se files:.**.** 
15ae1 20 20 20 20 20 20 20 20 50 54 46 5f 5a 45 52 4f          PTF_ZERO
15ae2 44 41 54 41 0a 2a 2a 20 20 20 20 20 20 20 20 20  DATA.**         
15ae3 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 7c 20 50  PTF_ZERODATA | P
15ae4 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20 20 20 20  TF_LEAF.**      
15ae5 20 20 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20     PTF_LEAFDATA 
15ae6 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a 2a 2a 20  | PTF_INTKEY.** 
15ae7 20 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46          PTF_LEAF
15ae8 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45  DATA | PTF_INTKE
15ae9 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2f 0a  Y | PTF_LEAF.*/.
15aea 73 74 61 74 69 63 20 69 6e 74 20 64 65 63 6f 64  static int decod
15aeb 65 46 6c 61 67 73 28 4d 65 6d 50 61 67 65 20 2a  eFlags(MemPage *
15aec 70 50 61 67 65 2c 20 69 6e 74 20 66 6c 61 67 42  pPage, int flagB
15aed 79 74 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64  yte){.  BtShared
15aee 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a 20 41 20   *pBt;     /* A 
15aef 63 6f 70 79 20 6f 66 20 70 50 61 67 65 2d 3e 70  copy of pPage->p
15af0 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  Bt */..  assert(
15af1 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
15af2 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67 6e 6f 3d  t==(pPage->pgno=
15af3 3d 31 20 3f 20 31 30 30 20 3a 20 30 29 20 29 3b  =1 ? 100 : 0) );
15af4 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15af5 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
15af6 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
15af7 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6c 65 61   );.  pPage->lea
15af8 66 20 3d 20 28 75 38 29 28 66 6c 61 67 42 79 74  f = (u8)(flagByt
15af9 65 3e 3e 33 29 3b 20 20 61 73 73 65 72 74 28 20  e>>3);  assert( 
15afa 50 54 46 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33  PTF_LEAF == 1<<3
15afb 20 29 3b 0a 20 20 66 6c 61 67 42 79 74 65 20 26   );.  flagByte &
15afc 3d 20 7e 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70  = ~PTF_LEAF;.  p
15afd 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
15afe 7a 65 20 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e  ze = 4-4*pPage->
15aff 6c 65 61 66 3b 0a 20 20 70 42 74 20 3d 20 70 50  leaf;.  pBt = pP
15b00 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20  age->pBt;.  if( 
15b01 66 6c 61 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c  flagByte==(PTF_L
15b02 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e  EAFDATA | PTF_IN
15b03 54 4b 45 59 29 20 29 7b 0a 20 20 20 20 70 50 61  TKEY) ){.    pPa
15b04 67 65 2d 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a  ge->intKey = 1;.
15b05 20 20 20 20 70 50 61 67 65 2d 3e 68 61 73 44 61      pPage->hasDa
15b06 74 61 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66  ta = pPage->leaf
15b07 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61 78  ;.    pPage->max
15b08 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78  Local = pBt->max
15b09 4c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d  Leaf;.    pPage-
15b0a 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d  >minLocal = pBt-
15b0b 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73  >minLeaf;.  }els
15b0c 65 20 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d  e if( flagByte==
15b0d 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a  PTF_ZERODATA ){.
15b0e 20 20 20 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65      pPage->intKe
15b0f 79 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65  y = 0;.    pPage
15b10 2d 3e 68 61 73 44 61 74 61 20 3d 20 30 3b 0a 20  ->hasData = 0;. 
15b11 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63     pPage->maxLoc
15b12 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63  al = pBt->maxLoc
15b13 61 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d  al;.    pPage->m
15b14 69 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d  inLocal = pBt->m
15b15 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65  inLocal;.  }else
15b16 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
15b17 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15b18 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
15b19 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
15b1a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  ** Initialize th
15b1b 65 20 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f  e auxiliary info
15b1c 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 64 69  rmation for a di
15b1d 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20  sk block..**.** 
15b1e 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
15b1f 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66   on success.  If
15b20 20 77 65 20 73 65 65 20 74 68 61 74 20 74 68 65   we see that the
15b21 20 70 61 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f   page does.** no
15b22 74 20 63 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c  t contain a well
15b23 2d 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65  -formed database
15b24 20 70 61 67 65 2c 20 74 68 65 6e 20 72 65 74 75   page, then retu
15b25 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f  rn .** SQLITE_CO
15b26 52 52 55 50 54 2e 20 20 4e 6f 74 65 20 74 68 61  RRUPT.  Note tha
15b27 74 20 61 20 72 65 74 75 72 6e 20 6f 66 20 53 51  t a return of SQ
15b28 4c 49 54 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74  LITE_OK does not
15b29 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20 74 68  .** guarantee th
15b2a 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20 77  at the page is w
15b2b 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20  ell-formed.  It 
15b2c 6f 6e 6c 79 20 73 68 6f 77 73 20 74 68 61 74 0a  only shows that.
15b2d 2a 2a 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20  ** we failed to 
15b2e 64 65 74 65 63 74 20 61 6e 79 20 63 6f 72 72 75  detect any corru
15b2f 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ption..*/.static
15b30 20 69 6e 74 20 62 74 72 65 65 49 6e 69 74 50 61   int btreeInitPa
15b31 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ge(MemPage *pPag
15b32 65 29 7b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  e){..  assert( p
15b33 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b 0a  Page->pBt!=0 );.
15b34 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15b35 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
15b36 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
15b37 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
15b38 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71 6c 69 74 65  ge->pgno==sqlite
15b39 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72  3PagerPagenumber
15b3a 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
15b3b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
15b3c 61 67 65 20 3d 3d 20 73 71 6c 69 74 65 33 50 61  age == sqlite3Pa
15b3d 67 65 72 47 65 74 45 78 74 72 61 28 70 50 61 67  gerGetExtra(pPag
15b3e 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
15b3f 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15b40 61 44 61 74 61 20 3d 3d 20 73 71 6c 69 74 65 33  aData == sqlite3
15b41 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61  PagerGetData(pPa
15b42 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
15b43 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69  .  if( !pPage->i
15b44 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 75 31 36  sInit ){.    u16
15b45 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   pc;            
15b46 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 20  /* Address of a 
15b47 66 72 65 65 62 6c 6f 63 6b 20 77 69 74 68 69 6e  freeblock within
15b48 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 5d 20   pPage->aData[] 
15b49 2a 2f 0a 20 20 20 20 75 38 20 68 64 72 3b 20 20  */.    u8 hdr;  
15b4a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
15b4b 73 65 74 20 74 6f 20 62 65 67 69 6e 6e 69 6e 67  set to beginning
15b4c 20 6f 66 20 70 61 67 65 20 68 65 61 64 65 72 20   of page header 
15b4d 2a 2f 0a 20 20 20 20 75 38 20 2a 64 61 74 61 3b  */.    u8 *data;
15b4e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 71 75            /* Equ
15b4f 61 6c 20 74 6f 20 70 50 61 67 65 2d 3e 61 44 61  al to pPage->aDa
15b50 74 61 20 2a 2f 0a 20 20 20 20 42 74 53 68 61 72  ta */.    BtShar
15b51 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20  ed *pBt;        
15b52 2f 2a 20 54 68 65 20 6d 61 69 6e 20 62 74 72 65  /* The main btre
15b53 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
15b54 20 20 20 75 31 36 20 75 73 61 62 6c 65 53 69 7a     u16 usableSiz
15b55 65 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20  e;    /* Amount 
15b56 6f 66 20 75 73 61 62 6c 65 20 73 70 61 63 65 20  of usable space 
15b57 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  on each page */.
15b58 20 20 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73      u16 cellOffs
15b59 65 74 3b 20 20 20 20 2f 2a 20 4f 66 66 73 65 74  et;    /* Offset
15b5a 20 66 72 6f 6d 20 73 74 61 72 74 20 6f 66 20 70   from start of p
15b5b 61 67 65 20 74 6f 20 66 69 72 73 74 20 63 65 6c  age to first cel
15b5c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20  l pointer */.   
15b5d 20 75 31 36 20 6e 46 72 65 65 3b 20 20 20 20 20   u16 nFree;     
15b5e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
15b5f 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 6f 6e   unused bytes on
15b60 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20   the page */.   
15b61 20 75 31 36 20 74 6f 70 3b 20 20 20 20 20 20 20   u16 top;       
15b62 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
15b63 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f  e of the cell co
15b64 6e 74 65 6e 74 20 61 72 65 61 20 2a 2f 0a 20 20  ntent area */.  
15b65 20 20 69 6e 74 20 69 43 65 6c 6c 46 69 72 73 74    int iCellFirst
15b66 3b 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c  ;    /* First al
15b67 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 6f 72 20  lowable cell or 
15b68 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65 74  freeblock offset
15b69 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c   */.    int iCel
15b6a 6c 4c 61 73 74 3b 20 20 20 20 20 2f 2a 20 4c 61  lLast;     /* La
15b6b 73 74 20 70 6f 73 73 69 62 6c 65 20 63 65 6c 6c  st possible cell
15b6c 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66   or freeblock of
15b6d 66 73 65 74 20 2a 2f 0a 0a 20 20 20 20 70 42 74  fset */..    pBt
15b6e 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 0a   = pPage->pBt;..
15b6f 20 20 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d      hdr = pPage-
15b70 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 20 20  >hdrOffset;.    
15b71 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
15b72 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64 65 63  ata;.    if( dec
15b73 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20  odeFlags(pPage, 
15b74 64 61 74 61 5b 68 64 72 5d 29 20 29 20 72 65 74  data[hdr]) ) ret
15b75 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
15b76 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61 73 73  PT_BKPT;.    ass
15b77 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69  ert( pBt->pageSi
15b78 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e  ze>=512 && pBt->
15b79 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20  pageSize<=32768 
15b7a 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61  );.    pPage->ma
15b7b 73 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70 61  skPage = pBt->pa
15b7c 67 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 20 20  geSize - 1;.    
15b7d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
15b7e 20 3d 20 30 3b 0a 20 20 20 20 75 73 61 62 6c 65   = 0;.    usable
15b7f 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62  Size = pBt->usab
15b80 6c 65 53 69 7a 65 3b 0a 20 20 20 20 70 50 61 67  leSize;.    pPag
15b81 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20  e->cellOffset = 
15b82 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68 64 72  cellOffset = hdr
15b83 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d   + 12 - 4*pPage-
15b84 3e 6c 65 61 66 3b 0a 20 20 20 20 74 6f 70 20 3d  >leaf;.    top =
15b85 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
15b86 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70 50 61  hdr+5]);.    pPa
15b87 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65 74 32  ge->nCell = get2
15b88 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33  byte(&data[hdr+3
15b89 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67  ]);.    if( pPag
15b8a 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45 4c 4c  e->nCell>MX_CELL
15b8b 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 2f  (pBt) ){.      /
15b8c 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c 73 20  * To many cells 
15b8d 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70 61 67  for a single pag
15b8e 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d 75 73  e.  The page mus
15b8f 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a 2f 0a  t be corrupt */.
15b90 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15b91 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15b92 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74  ;.    }.    test
15b93 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 43 65  case( pPage->nCe
15b94 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c 28 70 42 74 29  ll==MX_CELL(pBt)
15b95 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 6d 61   );..    /* A ma
15b96 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65  lformed database
15b97 20 70 61 67 65 20 6d 69 67 68 74 20 63 61 75 73   page might caus
15b98 65 20 75 73 20 74 6f 20 72 65 61 64 20 70 61 73  e us to read pas
15b99 74 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a  t the end.    **
15b9a 20 6f 66 20 70 61 67 65 20 77 68 65 6e 20 70 61   of page when pa
15b9b 72 73 69 6e 67 20 61 20 63 65 6c 6c 2e 20 20 0a  rsing a cell.  .
15b9c 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
15b9d 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
15b9e 6b 20 6f 66 20 63 6f 64 65 20 63 68 65 63 6b 73  k of code checks
15b9f 20 65 61 72 6c 79 20 74 6f 20 73 65 65 20 69 66   early to see if
15ba0 20 61 20 63 65 6c 6c 20 65 78 74 65 6e 64 73 0a   a cell extends.
15ba1 20 20 20 20 2a 2a 20 70 61 73 74 20 74 68 65 20      ** past the 
15ba2 65 6e 64 20 6f 66 20 61 20 70 61 67 65 20 62 6f  end of a page bo
15ba3 75 6e 64 61 72 79 20 61 6e 64 20 63 61 75 73 65  undary and cause
15ba4 73 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  s SQLITE_CORRUPT
15ba5 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 72   to be .    ** r
15ba6 65 74 75 72 6e 65 64 20 69 66 20 69 74 20 64 6f  eturned if it do
15ba7 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  es..    */.    i
15ba8 43 65 6c 6c 46 69 72 73 74 20 3d 20 63 65 6c 6c  CellFirst = cell
15ba9 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67 65  Offset + 2*pPage
15baa 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 43 65  ->nCell;.    iCe
15bab 6c 6c 4c 61 73 74 20 3d 20 75 73 61 62 6c 65 53  llLast = usableS
15bac 69 7a 65 20 2d 20 34 3b 0a 23 69 66 20 64 65 66  ize - 4;.#if def
15bad 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
15bae 4c 45 5f 4f 56 45 52 53 49 5a 45 5f 43 45 4c 4c  LE_OVERSIZE_CELL
15baf 5f 43 48 45 43 4b 29 0a 20 20 20 20 7b 0a 20 20  _CHECK).    {.  
15bb0 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20      int i;      
15bb1 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
15bb2 6e 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f 69  nto the cell poi
15bb3 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20 20  nter array */.  
15bb4 20 20 20 20 69 6e 74 20 73 7a 3b 20 20 20 20 20      int sz;     
15bb5 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
15bb6 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a 20 20 20 20   a cell */..    
15bb7 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65    if( !pPage->le
15bb8 61 66 20 29 20 69 43 65 6c 6c 4c 61 73 74 2d 2d  af ) iCellLast--
15bb9 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
15bba 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b   i<pPage->nCell;
15bbb 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70   i++){.        p
15bbc 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  c = get2byte(&da
15bbd 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 2b 69 2a  ta[cellOffset+i*
15bbe 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  2]);.        tes
15bbf 74 63 61 73 65 28 20 70 63 3d 3d 69 43 65 6c 6c  tcase( pc==iCell
15bc0 46 69 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20  First );.       
15bc1 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69   testcase( pc==i
15bc2 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 20 20 20 20  CellLast );.    
15bc3 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c 6c      if( pc<iCell
15bc4 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65 6c  First || pc>iCel
15bc5 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20 20  lLast ){.       
15bc6 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15bc7 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
15bc8 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
15bc9 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74   sz = cellSizePt
15bca 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70  r(pPage, &data[p
15bcb 63 5d 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  c]);.        tes
15bcc 74 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 75 73  tcase( pc+sz==us
15bcd 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20  ableSize );.    
15bce 20 20 20 20 69 66 28 20 70 63 2b 73 7a 3e 75 73      if( pc+sz>us
15bcf 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20  ableSize ){.    
15bd0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
15bd1 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
15bd2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
15bd3 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70    }.      if( !p
15bd4 50 61 67 65 2d 3e 6c 65 61 66 20 29 20 69 43 65  Page->leaf ) iCe
15bd5 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20 20 20 7d 20  llLast++;.    } 
15bd6 20 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a   .#endif..    /*
15bd7 20 43 6f 6d 70 75 74 65 20 74 68 65 20 74 6f 74   Compute the tot
15bd8 61 6c 20 66 72 65 65 20 73 70 61 63 65 20 6f 6e  al free space on
15bd9 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20 20 20   the page */.   
15bda 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26   pc = get2byte(&
15bdb 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20  data[hdr+1]);.  
15bdc 20 20 6e 46 72 65 65 20 3d 20 64 61 74 61 5b 68    nFree = data[h
15bdd 64 72 2b 37 5d 20 2b 20 74 6f 70 3b 0a 20 20 20  dr+7] + top;.   
15bde 20 77 68 69 6c 65 28 20 70 63 3e 30 20 29 7b 0a   while( pc>0 ){.
15bdf 20 20 20 20 20 20 75 31 36 20 6e 65 78 74 2c 20        u16 next, 
15be0 73 69 7a 65 3b 0a 20 20 20 20 20 20 69 66 28 20  size;.      if( 
15be1 70 63 3c 69 43 65 6c 6c 46 69 72 73 74 20 7c 7c  pc<iCellFirst ||
15be2 20 70 63 3e 69 43 65 6c 6c 4c 61 73 74 20 29 7b   pc>iCellLast ){
15be3 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72  .        /* Star
15be4 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 20  t of free block 
15be5 69 73 20 6f 66 66 20 74 68 65 20 70 61 67 65 20  is off the page 
15be6 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
15be7 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
15be8 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a  _BKPT; .      }.
15be9 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74        next = get
15bea 32 62 79 74 65 28 26 64 61 74 61 5b 70 63 5d 29  2byte(&data[pc])
15beb 3b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67  ;.      size = g
15bec 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63  et2byte(&data[pc
15bed 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +2]);.      if( 
15bee 28 6e 65 78 74 3e 30 20 26 26 20 6e 65 78 74 3c  (next>0 && next<
15bef 3d 70 63 2b 73 69 7a 65 2b 33 29 20 7c 7c 20 70  =pc+size+3) || p
15bf0 63 2b 73 69 7a 65 3e 75 73 61 62 6c 65 53 69 7a  c+size>usableSiz
15bf1 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  e ){.        /* 
15bf2 46 72 65 65 20 62 6c 6f 63 6b 73 20 6d 75 73 74  Free blocks must
15bf3 20 62 65 20 69 6e 20 61 73 63 65 6e 64 69 6e 67   be in ascending
15bf4 20 6f 72 64 65 72 2e 20 41 6e 64 20 74 68 65 20   order. And the 
15bf5 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 09 2a 2a  last byte of..**
15bf6 20 74 68 65 20 66 72 65 65 2d 62 6c 6f 63 6b 20   the free-block 
15bf7 6d 75 73 74 20 6c 69 65 20 6f 6e 20 74 68 65 20  must lie on the 
15bf8 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 20 20  database page.  
15bf9 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
15bfa 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
15bfb 5f 42 4b 50 54 3b 20 0a 20 20 20 20 20 20 7d 0a  _BKPT; .      }.
15bfc 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 6e 46        nFree = nF
15bfd 72 65 65 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20  ree + size;.    
15bfe 20 20 70 63 20 3d 20 6e 65 78 74 3b 0a 20 20 20    pc = next;.   
15bff 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74 20 74 68   }..    /* At th
15c00 69 73 20 70 6f 69 6e 74 2c 20 6e 46 72 65 65 20  is point, nFree 
15c01 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 75 6d  contains the sum
15c02 20 6f 66 20 74 68 65 20 6f 66 66 73 65 74 20 74   of the offset t
15c03 6f 20 74 68 65 20 73 74 61 72 74 0a 20 20 20 20  o the start.    
15c04 2a 2a 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63  ** of the cell-c
15c05 6f 6e 74 65 6e 74 20 61 72 65 61 20 70 6c 75 73  ontent area plus
15c06 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66   the number of f
15c07 72 65 65 20 62 79 74 65 73 20 77 69 74 68 69 6e  ree bytes within
15c08 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c  .    ** the cell
15c09 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 49  -content area. I
15c0a 66 20 74 68 69 73 20 69 73 20 67 72 65 61 74 65  f this is greate
15c0b 72 20 74 68 61 6e 20 74 68 65 20 75 73 61 62 6c  r than the usabl
15c0c 65 2d 73 69 7a 65 0a 20 20 20 20 2a 2a 20 6f 66  e-size.    ** of
15c0d 20 74 68 65 20 70 61 67 65 2c 20 74 68 65 6e 20   the page, then 
15c0e 74 68 65 20 70 61 67 65 20 6d 75 73 74 20 62 65  the page must be
15c0f 20 63 6f 72 72 75 70 74 65 64 2e 20 54 68 69 73   corrupted. This
15c10 20 63 68 65 63 6b 20 61 6c 73 6f 0a 20 20 20 20   check also.    
15c11 2a 2a 20 73 65 72 76 65 73 20 74 6f 20 76 65 72  ** serves to ver
15c12 69 66 79 20 74 68 61 74 20 74 68 65 20 6f 66 66  ify that the off
15c13 73 65 74 20 74 6f 20 74 68 65 20 73 74 61 72 74  set to the start
15c14 20 6f 66 20 74 68 65 20 63 65 6c 6c 2d 63 6f 6e   of the cell-con
15c15 74 65 6e 74 0a 20 20 20 20 2a 2a 20 61 72 65 61  tent.    ** area
15c16 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  , according to t
15c17 68 65 20 70 61 67 65 20 68 65 61 64 65 72 2c 20  he page header, 
15c18 6c 69 65 73 20 77 69 74 68 69 6e 20 74 68 65 20  lies within the 
15c19 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  page..    */.   
15c1a 20 69 66 28 20 6e 46 72 65 65 3e 75 73 61 62 6c   if( nFree>usabl
15c1b 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 72  eSize ){.      r
15c1c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15c1d 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20  RUPT_BKPT; .    
15c1e 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 46 72  }.    pPage->nFr
15c1f 65 65 20 3d 20 28 75 31 36 29 28 6e 46 72 65 65  ee = (u16)(nFree
15c20 20 2d 20 69 43 65 6c 6c 46 69 72 73 74 29 3b 0a   - iCellFirst);.
15c21 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69      pPage->isIni
15c22 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  t = 1;.  }.  ret
15c23 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
15c24 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20 61  ../*.** Set up a
15c25 20 72 61 77 20 70 61 67 65 20 73 6f 20 74 68 61   raw page so tha
15c26 74 20 69 74 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  t it looks like 
15c27 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20  a database page 
15c28 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e 6f 20 65 6e  holding.** no en
15c29 74 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  tries..*/.static
15c2a 20 76 6f 69 64 20 7a 65 72 6f 50 61 67 65 28 4d   void zeroPage(M
15c2b 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
15c2c 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 75 6e 73  nt flags){.  uns
15c2d 69 67 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61  igned char *data
15c2e 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
15c2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
15c30 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20   = pPage->pBt;. 
15c31 20 75 38 20 68 64 72 20 3d 20 70 50 61 67 65 2d   u8 hdr = pPage-
15c32 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 75 31  >hdrOffset;.  u1
15c33 36 20 66 69 72 73 74 3b 0a 0a 20 20 61 73 73 65  6 first;..  asse
15c34 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
15c35 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61 67 65  Pagenumber(pPage
15c36 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50 61 67  ->pDbPage)==pPag
15c37 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20 20 61 73 73  e->pgno );.  ass
15c38 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
15c39 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d  rGetExtra(pPage-
15c3a 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f  >pDbPage) == (vo
15c3b 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 61  id*)pPage );.  a
15c3c 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
15c3d 67 65 72 47 65 74 44 61 74 61 28 70 50 61 67 65  gerGetData(pPage
15c3e 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 64 61  ->pDbPage) == da
15c3f 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ta );.  assert( 
15c40 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
15c41 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70  iteable(pPage->p
15c42 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73  DbPage) );.  ass
15c43 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15c44 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
15c45 65 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51  ex) );.#ifdef SQ
15c46 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c 45  LITE_SECURE_DELE
15c47 54 45 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74  TE.  memset(&dat
15c48 61 5b 68 64 72 5d 2c 20 30 2c 20 70 42 74 2d 3e  a[hdr], 0, pBt->
15c49 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 68 64 72  usableSize - hdr
15c4a 29 3b 0a 23 65 6e 64 69 66 0a 20 20 64 61 74 61  );.#endif.  data
15c4b 5b 68 64 72 5d 20 3d 20 28 63 68 61 72 29 66 6c  [hdr] = (char)fl
15c4c 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20 68  ags;.  first = h
15c4d 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c 61  dr + 8 + 4*((fla
15c4e 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20  gs&PTF_LEAF)==0 
15c4f 3f 31 3a 30 29 3b 0a 20 20 6d 65 6d 73 65 74 28  ?1:0);.  memset(
15c50 26 64 61 74 61 5b 68 64 72 2b 31 5d 2c 20 30 2c  &data[hdr+1], 0,
15c51 20 34 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b   4);.  data[hdr+
15c52 37 5d 20 3d 20 30 3b 0a 20 20 70 75 74 32 62 79  7] = 0;.  put2by
15c53 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c  te(&data[hdr+5],
15c54 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
15c55 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65  );.  pPage->nFre
15c56 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e = pBt->usableS
15c57 69 7a 65 20 2d 20 66 69 72 73 74 3b 0a 20 20 64  ize - first;.  d
15c58 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65  ecodeFlags(pPage
15c59 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 50 61 67  , flags);.  pPag
15c5a 65 2d 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 68  e->hdrOffset = h
15c5b 64 72 3b 0a 20 20 70 50 61 67 65 2d 3e 63 65 6c  dr;.  pPage->cel
15c5c 6c 4f 66 66 73 65 74 20 3d 20 66 69 72 73 74 3b  lOffset = first;
15c5d 0a 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  .  pPage->nOverf
15c5e 6c 6f 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  low = 0;.  asser
15c5f 74 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  t( pBt->pageSize
15c60 3e 3d 35 31 32 20 26 26 20 70 42 74 2d 3e 70 61  >=512 && pBt->pa
15c61 67 65 53 69 7a 65 3c 3d 33 32 37 36 38 20 29 3b  geSize<=32768 );
15c62 0a 20 20 70 50 61 67 65 2d 3e 6d 61 73 6b 50 61  .  pPage->maskPa
15c63 67 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69  ge = pBt->pageSi
15c64 7a 65 20 2d 20 31 3b 0a 20 20 70 50 61 67 65 2d  ze - 1;.  pPage-
15c65 3e 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20 20 70 50  >nCell = 0;.  pP
15c66 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 31 3b  age->isInit = 1;
15c67 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  .}.../*.** Conve
15c68 72 74 20 61 20 44 62 50 61 67 65 20 6f 62 74 61  rt a DbPage obta
15c69 69 6e 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61  ined from the pa
15c6a 67 65 72 20 69 6e 74 6f 20 61 20 4d 65 6d 50 61  ger into a MemPa
15c6b 67 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 74 68  ge used by.** th
15c6c 65 20 62 74 72 65 65 20 6c 61 79 65 72 2e 0a 2a  e btree layer..*
15c6d 2f 0a 73 74 61 74 69 63 20 4d 65 6d 50 61 67 65  /.static MemPage
15c6e 20 2a 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44   *btreePageFromD
15c6f 62 50 61 67 65 28 44 62 50 61 67 65 20 2a 70 44  bPage(DbPage *pD
15c70 62 50 61 67 65 2c 20 50 67 6e 6f 20 70 67 6e 6f  bPage, Pgno pgno
15c71 2c 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  , BtShared *pBt)
15c72 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
15c73 67 65 20 3d 20 28 4d 65 6d 50 61 67 65 2a 29 73  ge = (MemPage*)s
15c74 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78  qlite3PagerGetEx
15c75 74 72 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20  tra(pDbPage);.  
15c76 70 50 61 67 65 2d 3e 61 44 61 74 61 20 3d 20 73  pPage->aData = s
15c77 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
15c78 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20 20 70  ta(pDbPage);.  p
15c79 50 61 67 65 2d 3e 70 44 62 50 61 67 65 20 3d 20  Page->pDbPage = 
15c7a 70 44 62 50 61 67 65 3b 0a 20 20 70 50 61 67 65  pDbPage;.  pPage
15c7b 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 70  ->pBt = pBt;.  p
15c7c 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e  Page->pgno = pgn
15c7d 6f 3b 0a 20 20 70 50 61 67 65 2d 3e 68 64 72 4f  o;.  pPage->hdrO
15c7e 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 70  ffset = pPage->p
15c7f 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30  gno==1 ? 100 : 0
15c80 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  ;.  return pPage
15c81 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20  ; .}../*.** Get 
15c82 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
15c83 70 61 67 65 72 2e 20 20 49 6e 69 74 69 61 6c 69  pager.  Initiali
15c84 7a 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70  ze the MemPage.p
15c85 42 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67  Bt and.** MemPag
15c86 65 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73  e.aData elements
15c87 20 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a   if needed..**.*
15c88 2a 20 49 66 20 74 68 65 20 6e 6f 43 6f 6e 74 65  * If the noConte
15c89 6e 74 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  nt flag is set, 
15c8a 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  it means that we
15c8b 20 64 6f 20 6e 6f 74 20 63 61 72 65 20 61 62 6f   do not care abo
15c8c 75 74 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  ut.** the conten
15c8d 74 20 6f 66 20 74 68 65 20 70 61 67 65 20 61 74  t of the page at
15c8e 20 74 68 69 73 20 74 69 6d 65 2e 20 20 53 6f 20   this time.  So 
15c8f 64 6f 20 6e 6f 74 20 67 6f 20 74 6f 20 74 68 65  do not go to the
15c90 20 64 69 73 6b 0a 2a 2a 20 74 6f 20 66 65 74 63   disk.** to fetc
15c91 68 20 74 68 65 20 63 6f 6e 74 65 6e 74 2e 20 20  h the content.  
15c92 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74 68 65  Just fill in the
15c93 20 63 6f 6e 74 65 6e 74 20 77 69 74 68 20 7a 65   content with ze
15c94 72 6f 73 20 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20  ros for now..** 
15c95 49 66 20 69 6e 20 74 68 65 20 66 75 74 75 72 65  If in the future
15c96 20 77 65 20 63 61 6c 6c 20 73 71 6c 69 74 65 33   we call sqlite3
15c97 50 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20  PagerWrite() on 
15c98 74 68 69 73 20 70 61 67 65 2c 20 74 68 61 74 0a  this page, that.
15c99 2a 2a 20 6d 65 61 6e 73 20 77 65 20 68 61 76 65  ** means we have
15c9a 20 73 74 61 72 74 65 64 20 74 6f 20 62 65 20 63   started to be c
15c9b 6f 6e 63 65 72 6e 65 64 20 61 62 6f 75 74 20 63  oncerned about c
15c9c 6f 6e 74 65 6e 74 20 61 6e 64 20 74 68 65 20 64  ontent and the d
15c9d 69 73 6b 0a 2a 2a 20 72 65 61 64 20 73 68 6f 75  isk.** read shou
15c9e 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74  ld occur at that
15c9f 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69   point..*/.stati
15ca0 63 20 69 6e 74 20 62 74 72 65 65 47 65 74 50 61  c int btreeGetPa
15ca1 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ge(.  BtShared *
15ca2 70 42 74 2c 20 20 20 20 20 20 20 2f 2a 20 54 68  pBt,       /* Th
15ca3 65 20 62 74 72 65 65 20 2a 2f 0a 20 20 50 67 6e  e btree */.  Pgn
15ca4 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20  o pgno,         
15ca5 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
15ca6 68 65 20 70 61 67 65 20 74 6f 20 66 65 74 63 68  he page to fetch
15ca7 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a   */.  MemPage **
15ca8 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 52 65  ppPage,    /* Re
15ca9 74 75 72 6e 20 74 68 65 20 70 61 67 65 20 69 6e  turn the page in
15caa 20 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20   this parameter 
15cab 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65  */.  int noConte
15cac 6e 74 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20  nt        /* Do 
15cad 6e 6f 74 20 6c 6f 61 64 20 70 61 67 65 20 63 6f  not load page co
15cae 6e 74 65 6e 74 20 69 66 20 74 72 75 65 20 2a 2f  ntent if true */
15caf 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
15cb0 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b  DbPage *pDbPage;
15cb1 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
15cb2 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
15cb3 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
15cb4 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
15cb5 72 41 63 71 75 69 72 65 28 70 42 74 2d 3e 70 50  rAcquire(pBt->pP
15cb6 61 67 65 72 2c 20 70 67 6e 6f 2c 20 28 44 62 50  ager, pgno, (DbP
15cb7 61 67 65 2a 2a 29 26 70 44 62 50 61 67 65 2c 20  age**)&pDbPage, 
15cb8 6e 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66  noContent);.  if
15cb9 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
15cba 3b 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 62 74  ;.  *ppPage = bt
15cbb 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67  reePageFromDbPag
15cbc 65 28 70 44 62 50 61 67 65 2c 20 70 67 6e 6f 2c  e(pDbPage, pgno,
15cbd 20 70 42 74 29 3b 0a 20 20 72 65 74 75 72 6e 20   pBt);.  return 
15cbe 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
15cbf 0a 2a 2a 20 52 65 74 72 69 65 76 65 20 61 20 70  .** Retrieve a p
15cc0 61 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67  age from the pag
15cc1 65 72 20 63 61 63 68 65 2e 20 49 66 20 74 68 65  er cache. If the
15cc2 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20   requested page 
15cc3 69 73 20 6e 6f 74 0a 2a 2a 20 61 6c 72 65 61 64  is not.** alread
15cc4 79 20 69 6e 20 74 68 65 20 70 61 67 65 72 20 63  y in the pager c
15cc5 61 63 68 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c  ache return NULL
15cc6 2e 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  . Initialize the
15cc7 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61 6e 64   MemPage.pBt and
15cc8 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44 61 74  .** MemPage.aDat
15cc9 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20 6e 65  a elements if ne
15cca 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eded..*/.static 
15ccb 4d 65 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61  MemPage *btreePa
15ccc 67 65 4c 6f 6f 6b 75 70 28 42 74 53 68 61 72 65  geLookup(BtShare
15ccd 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e  d *pBt, Pgno pgn
15cce 6f 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44  o){.  DbPage *pD
15ccf 62 50 61 67 65 3b 0a 20 20 61 73 73 65 72 74 28  bPage;.  assert(
15cd0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15cd1 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
15cd2 29 3b 0a 20 20 70 44 62 50 61 67 65 20 3d 20 73  );.  pDbPage = s
15cd3 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
15cd4 70 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 70  p(pBt->pPager, p
15cd5 67 6e 6f 29 3b 0a 20 20 69 66 28 20 70 44 62 50  gno);.  if( pDbP
15cd6 61 67 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  age ){.    retur
15cd7 6e 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d 44  n btreePageFromD
15cd8 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20 70  bPage(pDbPage, p
15cd9 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 7d 0a 20  gno, pBt);.  }. 
15cda 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
15cdb 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
15cdc 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
15cdd 61 73 65 20 66 69 6c 65 20 69 6e 20 70 61 67 65  ase file in page
15cde 73 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61  s. If there is a
15cdf 6e 79 20 6b 69 6e 64 20 6f 66 0a 2a 2a 20 65 72  ny kind of.** er
15ce0 72 6f 72 2c 20 72 65 74 75 72 6e 20 28 28 75 6e  ror, return ((un
15ce1 73 69 67 6e 65 64 20 69 6e 74 29 2d 31 29 2e 0a  signed int)-1)..
15ce2 2a 2f 0a 73 74 61 74 69 63 20 50 67 6e 6f 20 70  */.static Pgno p
15ce3 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 42 74  agerPagecount(Bt
15ce4 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
15ce5 69 6e 74 20 6e 50 61 67 65 20 3d 20 2d 31 3b 0a  int nPage = -1;.
15ce6 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
15ce7 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 20  rt( pBt->pPage1 
15ce8 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
15ce9 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
15cea 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50  pBt->pPager, &nP
15ceb 61 67 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20  age);.  assert( 
15cec 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
15ced 20 6e 50 61 67 65 3d 3d 2d 31 20 29 3b 0a 20 20   nPage==-1 );.  
15cee 72 65 74 75 72 6e 20 28 50 67 6e 6f 29 6e 50 61  return (Pgno)nPa
15cef 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  ge;.}../*.** Get
15cf0 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65   a page from the
15cf1 20 70 61 67 65 72 20 61 6e 64 20 69 6e 69 74 69   pager and initi
15cf2 61 6c 69 7a 65 20 69 74 2e 20 20 54 68 69 73 20  alize it.  This 
15cf3 72 6f 75 74 69 6e 65 20 69 73 20 6a 75 73 74 20  routine is just 
15cf4 61 0a 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65  a.** convenience
15cf5 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20   wrapper around 
15cf6 73 65 70 61 72 61 74 65 20 63 61 6c 6c 73 20 74  separate calls t
15cf7 6f 20 62 74 72 65 65 47 65 74 50 61 67 65 28 29  o btreeGetPage()
15cf8 20 61 6e 64 20 0a 2a 2a 20 62 74 72 65 65 49 6e   and .** btreeIn
15cf9 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  itPage()..**.** 
15cfa 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
15cfb 72 73 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  rs, then the val
15cfc 75 65 20 2a 70 70 50 61 67 65 20 69 73 20 73 65  ue *ppPage is se
15cfd 74 20 74 6f 20 69 73 20 75 6e 64 65 66 69 6e 65  t to is undefine
15cfe 64 2e 20 49 74 0a 2a 2a 20 6d 61 79 20 72 65 6d  d. It.** may rem
15cff 61 69 6e 20 75 6e 63 68 61 6e 67 65 64 2c 20 6f  ain unchanged, o
15d00 72 20 69 74 20 6d 61 79 20 62 65 20 73 65 74 20  r it may be set 
15d01 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20 76 61  to an invalid va
15d02 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lue..*/.static i
15d03 6e 74 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67  nt getAndInitPag
15d04 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
15d05 42 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Bt,          /* 
15d06 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
15d07 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
15d08 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
15d09 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67  umber of the pag
15d0a 65 20 74 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65  e to get */.  Me
15d0b 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 20 20  mPage **ppPage  
15d0c 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
15d0d 70 61 67 65 20 70 6f 69 6e 74 65 72 20 68 65 72  page pointer her
15d0e 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
15d0f 3b 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 50 67  ;.  TESTONLY( Pg
15d10 6e 6f 20 69 4c 61 73 74 50 67 20 3d 20 70 61 67  no iLastPg = pag
15d11 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
15d12 3b 20 29 0a 20 20 61 73 73 65 72 74 28 20 73 71  ; ).  assert( sq
15d13 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
15d14 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
15d15 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74  .  rc = btreeGet
15d16 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20  Page(pBt, pgno, 
15d17 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20 69 66  ppPage, 0);.  if
15d18 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
15d19 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65  ){.    rc = btre
15d1a 65 49 6e 69 74 50 61 67 65 28 2a 70 70 50 61 67  eInitPage(*ppPag
15d1b 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  e);.    if( rc!=
15d1c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15d1d 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a     releasePage(*
15d1e 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  ppPage);.    }. 
15d1f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
15d20 72 65 71 75 65 73 74 65 64 20 70 61 67 65 20 6e  requested page n
15d21 75 6d 62 65 72 20 77 61 73 20 65 69 74 68 65 72  umber was either
15d22 20 30 20 6f 72 20 67 72 65 61 74 65 72 20 74 68   0 or greater th
15d23 61 6e 20 74 68 65 20 70 61 67 65 0a 20 20 2a 2a  an the page.  **
15d24 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c   number of the l
15d25 61 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ast page in the 
15d26 64 61 74 61 62 61 73 65 2c 20 74 68 69 73 20 66  database, this f
15d27 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72  unction should r
15d28 65 74 75 72 6e 0a 20 20 2a 2a 20 53 51 4c 49 54  eturn.  ** SQLIT
15d29 45 5f 43 4f 52 52 55 50 54 20 6f 72 20 73 6f 6d  E_CORRUPT or som
15d2a 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 28 69  e other error (i
15d2b 2e 65 2e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29  .e. SQLITE_FULL)
15d2c 2e 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 69  . Check that thi
15d2d 73 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 63 61  s.  ** is the ca
15d2e 73 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  se.  */.  assert
15d2f 28 20 28 70 67 6e 6f 3e 30 20 26 26 20 70 67 6e  ( (pgno>0 && pgn
15d30 6f 3c 3d 69 4c 61 73 74 50 67 29 20 7c 7c 20 72  o<=iLastPg) || r
15d31 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c!=SQLITE_OK );.
15d32 20 20 74 65 73 74 63 61 73 65 28 20 70 67 6e 6f    testcase( pgno
15d33 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==0 );.  testcas
15d34 65 28 20 70 67 6e 6f 3d 3d 69 4c 61 73 74 50 67  e( pgno==iLastPg
15d35 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 63   );..  return rc
15d36 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61  ;.}../*.** Relea
15d37 73 65 20 61 20 4d 65 6d 50 61 67 65 2e 20 20 54  se a MemPage.  T
15d38 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 63 61  his should be ca
15d39 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61  lled once for ea
15d3a 63 68 20 70 72 69 6f 72 0a 2a 2a 20 63 61 6c 6c  ch prior.** call
15d3b 20 74 6f 20 62 74 72 65 65 47 65 74 50 61 67 65   to btreeGetPage
15d3c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
15d3d 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d   releasePage(Mem
15d3e 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20  Page *pPage){.  
15d3f 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20  if( pPage ){.   
15d40 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15d41 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73  aData );.    ass
15d42 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74 20  ert( pPage->pBt 
15d43 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
15d44 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45 78  qlite3PagerGetEx
15d45 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50 61  tra(pPage->pDbPa
15d46 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70 50  ge) == (void*)pP
15d47 61 67 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72  age );.    asser
15d48 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  t( sqlite3PagerG
15d49 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70 44  etData(pPage->pD
15d4a 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e 61  bPage)==pPage->a
15d4b 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73 65  Data );.    asse
15d4c 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15d4d 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
15d4e 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20  t->mutex) );.   
15d4f 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
15d50 65 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  ef(pPage->pDbPag
15d51 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  e);.  }.}../*.**
15d52 20 44 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61   During a rollba
15d53 63 6b 2c 20 77 68 65 6e 20 74 68 65 20 70 61 67  ck, when the pag
15d54 65 72 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f 72  er reloads infor
15d55 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20  mation into the 
15d56 63 61 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61 74  cache.** so that
15d57 20 74 68 65 20 63 61 63 68 65 20 69 73 20 72 65   the cache is re
15d58 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72  stored to its or
15d59 69 67 69 6e 61 6c 20 73 74 61 74 65 20 61 74 20  iginal state at 
15d5a 74 68 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a 20  the start of.** 
15d5b 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  the transaction,
15d5c 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 72   for each page r
15d5d 65 73 74 6f 72 65 64 20 74 68 69 73 20 72 6f 75  estored this rou
15d5e 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
15d5f 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
15d60 6e 65 20 6e 65 65 64 73 20 74 6f 20 72 65 73 65  ne needs to rese
15d61 74 20 74 68 65 20 65 78 74 72 61 20 64 61 74 61  t the extra data
15d62 20 73 65 63 74 69 6f 6e 20 61 74 20 74 68 65 20   section at the 
15d63 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61  end of the.** pa
15d64 67 65 20 74 6f 20 61 67 72 65 65 20 77 69 74 68  ge to agree with
15d65 20 74 68 65 20 72 65 73 74 6f 72 65 64 20 64 61   the restored da
15d66 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ta..*/.static vo
15d67 69 64 20 70 61 67 65 52 65 69 6e 69 74 28 44 62  id pageReinit(Db
15d68 50 61 67 65 20 2a 70 44 61 74 61 29 7b 0a 20 20  Page *pData){.  
15d69 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a  MemPage *pPage;.
15d6a 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61    pPage = (MemPa
15d6b 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  ge *)sqlite3Page
15d6c 72 47 65 74 45 78 74 72 61 28 70 44 61 74 61 29  rGetExtra(pData)
15d6d 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
15d6e 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63  te3PagerPageRefc
15d6f 6f 75 6e 74 28 70 44 61 74 61 29 3e 30 20 29 3b  ount(pData)>0 );
15d70 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 73  .  if( pPage->is
15d71 49 6e 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65  Init ){.    asse
15d72 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15d73 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
15d74 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 20  t->mutex) );.   
15d75 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d   pPage->isInit =
15d76 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   0;.    if( sqli
15d77 74 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63  te3PagerPageRefc
15d78 6f 75 6e 74 28 70 44 61 74 61 29 3e 31 20 29 7b  ount(pData)>1 ){
15d79 0a 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20  .      /* pPage 
15d7a 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 61 20 62  might not be a b
15d7b 74 72 65 65 20 70 61 67 65 3b 20 20 69 74 20 6d  tree page;  it m
15d7c 69 67 68 74 20 62 65 20 61 6e 20 6f 76 65 72 66  ight be an overf
15d7d 6c 6f 77 20 70 61 67 65 0a 20 20 20 20 20 20 2a  low page.      *
15d7e 2a 20 6f 72 20 70 74 72 6d 61 70 20 70 61 67 65  * or ptrmap page
15d7f 20 6f 72 20 61 20 66 72 65 65 20 70 61 67 65 2e   or a free page.
15d80 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73    In those cases
15d81 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  , the following.
15d82 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f        ** call to
15d83 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29   btreeInitPage()
15d84 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 74   will likely ret
15d85 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
15d86 50 54 2e 0a 20 20 20 20 20 20 2a 2a 20 42 75 74  PT..      ** But
15d87 20 6e 6f 20 68 61 72 6d 20 69 73 20 64 6f 6e 65   no harm is done
15d88 20 62 79 20 74 68 69 73 2e 20 20 41 6e 64 20 69   by this.  And i
15d89 74 20 69 73 20 76 65 72 79 20 69 6d 70 6f 72 74  t is very import
15d8a 61 6e 74 20 74 68 61 74 0a 20 20 20 20 20 20 2a  ant that.      *
15d8b 2a 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  * btreeInitPage(
15d8c 29 20 62 65 20 63 61 6c 6c 65 64 20 6f 6e 20 65  ) be called on e
15d8d 76 65 72 79 20 62 74 72 65 65 20 70 61 67 65 20  very btree page 
15d8e 73 6f 20 77 65 20 6d 61 6b 65 0a 20 20 20 20 20  so we make.     
15d8f 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 66 6f 72   ** the call for
15d90 20 65 76 65 72 79 20 70 61 67 65 20 74 68 61 74   every page that
15d91 20 63 6f 6d 65 73 20 69 6e 20 66 6f 72 20 72 65   comes in for re
15d92 2d 69 6e 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20  -initing. */.   
15d93 20 20 20 62 74 72 65 65 49 6e 69 74 50 61 67 65     btreeInitPage
15d94 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20  (pPage);.    }. 
15d95 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f   }.}../*.** Invo
15d96 6b 65 20 74 68 65 20 62 75 73 79 20 68 61 6e 64  ke the busy hand
15d97 6c 65 72 20 66 6f 72 20 61 20 62 74 72 65 65 2e  ler for a btree.
15d98 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
15d99 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61  treeInvokeBusyHa
15d9a 6e 64 6c 65 72 28 76 6f 69 64 20 2a 70 41 72 67  ndler(void *pArg
15d9b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
15d9c 42 74 20 3d 20 28 42 74 53 68 61 72 65 64 2a 29  Bt = (BtShared*)
15d9d 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20  pArg;.  assert( 
15d9e 70 42 74 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73  pBt->db );.  ass
15d9f 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
15da0 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d  ex_held(pBt->db-
15da1 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74  >mutex) );.  ret
15da2 75 72 6e 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b  urn sqlite3Invok
15da3 65 42 75 73 79 48 61 6e 64 6c 65 72 28 26 70 42  eBusyHandler(&pB
15da4 74 2d 3e 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c  t->db->busyHandl
15da5 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70  er);.}../*.** Op
15da6 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69  en a database fi
15da7 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65  le..** .** zFile
15da8 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  name is the name
15da9 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
15daa 20 66 69 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65   file.  If zFile
15dab 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20  name is NULL.** 
15dac 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 77  a new database w
15dad 69 74 68 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d  ith a random nam
15dae 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 20 54  e is created.  T
15daf 68 69 73 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d  his randomly nam
15db0 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  ed.** database f
15db1 69 6c 65 20 77 69 6c 6c 20 62 65 20 64 65 6c 65  ile will be dele
15db2 74 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65 33  ted when sqlite3
15db3 42 74 72 65 65 43 6c 6f 73 65 28 29 20 69 73 20  BtreeClose() is 
15db4 63 61 6c 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46  called..** If zF
15db5 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d  ilename is ":mem
15db6 6f 72 79 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e  ory:" then an in
15db7 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
15db8 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 74   is created.** t
15db9 68 61 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63  hat is automatic
15dba 61 6c 6c 79 20 64 65 73 74 72 6f 79 65 64 20 77  ally destroyed w
15dbb 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65 64  hen it is closed
15dbc 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 64  ..**.** If the d
15dbd 61 74 61 62 61 73 65 20 69 73 20 61 6c 72 65 61  atabase is alrea
15dbe 64 79 20 6f 70 65 6e 65 64 20 69 6e 20 74 68 65  dy opened in the
15dbf 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63   same database c
15dc0 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64  onnection.** and
15dc1 20 77 65 20 61 72 65 20 69 6e 20 73 68 61 72 65   we are in share
15dc2 64 20 63 61 63 68 65 20 6d 6f 64 65 2c 20 74 68  d cache mode, th
15dc3 65 6e 20 74 68 65 20 6f 70 65 6e 20 77 69 6c 6c  en the open will
15dc4 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a 2a   fail with an.**
15dc5 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
15dc6 4e 54 20 65 72 72 6f 72 2e 20 20 57 65 20 63 61  NT error.  We ca
15dc7 6e 6e 6f 74 20 61 6c 6c 6f 77 20 74 77 6f 20 6f  nnot allow two o
15dc8 72 20 6d 6f 72 65 20 42 74 53 68 61 72 65 64 0a  r more BtShared.
15dc9 2a 2a 20 6f 62 6a 65 63 74 73 20 69 6e 20 74 68  ** objects in th
15dca 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
15dcb 63 6f 6e 6e 65 63 74 69 6f 6e 20 73 69 6e 63 65  connection since
15dcc 20 64 6f 69 6e 67 20 73 6f 20 77 69 6c 6c 20 6c   doing so will l
15dcd 65 61 64 0a 2a 2a 20 74 6f 20 70 72 6f 62 6c 65  ead.** to proble
15dce 6d 73 20 77 69 74 68 20 6c 6f 63 6b 69 6e 67 2e  ms with locking.
15dcf 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15dd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
15dd1 72 65 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74  reeOpen(.  const
15dd2 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
15dd3 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  ,  /* Name of th
15dd4 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e  e file containin
15dd5 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74 61  g the BTree data
15dd6 62 61 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  base */.  sqlite
15dd7 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
15dd8 20 20 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20    /* Associated 
15dd9 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
15dda 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42  */.  Btree **ppB
15ddb 74 72 65 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  tree,        /* 
15ddc 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 42  Pointer to new B
15ddd 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72 69 74  tree object writ
15dde 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  ten here */.  in
15ddf 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
15de0 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73        /* Options
15de1 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61   */.  int vfsFla
15de2 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gs            /*
15de3 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68   Flags passed th
15de4 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33  rough to sqlite3
15de5 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a  _vfs.xOpen() */.
15de6 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  ){.  sqlite3_vfs
15de7 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20   *pVfs;         
15de8 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74      /* The VFS t
15de9 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 62  o use for this b
15dea 74 72 65 65 20 2a 2f 0a 20 20 42 74 53 68 61 72  tree */.  BtShar
15deb 65 64 20 2a 70 42 74 20 3d 20 30 3b 20 20 20 20  ed *pBt = 0;    
15dec 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 61 72           /* Shar
15ded 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65 65  ed part of btree
15dee 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
15def 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20 20  Btree *p;       
15df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
15df1 2a 20 48 61 6e 64 6c 65 20 74 6f 20 72 65 74 75  * Handle to retu
15df2 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  rn */.  sqlite3_
15df3 6d 75 74 65 78 20 2a 6d 75 74 65 78 4f 70 65 6e  mutex *mutexOpen
15df4 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76 65 6e   = 0;  /* Preven
15df5 74 73 20 61 20 72 61 63 65 20 63 6f 6e 64 69 74  ts a race condit
15df6 69 6f 6e 2e 20 54 69 63 6b 65 74 20 23 33 35 33  ion. Ticket #353
15df7 37 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  7 */.  int rc = 
15df8 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
15df9 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
15dfa 63 6f 64 65 20 66 72 6f 6d 20 74 68 69 73 20 66  code from this f
15dfb 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20  unction */.  u8 
15dfc 6e 52 65 73 65 72 76 65 3b 20 20 20 20 20 20 20  nReserve;       
15dfd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
15dfe 79 74 65 20 6f 66 20 75 6e 75 73 65 64 20 73 70  yte of unused sp
15dff 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65  ace on each page
15e00 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
15e01 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31 30  har zDbHeader[10
15e02 30 5d 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65  0];  /* Database
15e03 20 68 65 61 64 65 72 20 63 6f 6e 74 65 6e 74 20   header content 
15e04 2a 2f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65  */..  /* Set the
15e05 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65 6d 64   variable isMemd
15e06 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20 61 6e  b to true for an
15e07 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
15e08 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20 66 61  ase, or .  ** fa
15e09 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65 2d 62  lse for a file-b
15e0a 61 73 65 64 20 64 61 74 61 62 61 73 65 2e 20 54  ased database. T
15e0b 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 20 6f 6e  his symbol is on
15e0c 6c 79 20 72 65 71 75 69 72 65 64 20 69 66 0a 20  ly required if. 
15e0d 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 20 74 68   ** either of th
15e0e 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 6f 72  e shared-data or
15e0f 20 61 75 74 6f 76 61 63 75 75 6d 20 66 65 61 74   autovacuum feat
15e10 75 72 65 73 20 61 72 65 20 63 6f 6d 70 69 6c 65  ures are compile
15e11 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65  d .  ** into the
15e12 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a 2f 0a 23   library..  */.#
15e13 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
15e14 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
15e15 41 43 48 45 29 20 7c 7c 20 21 64 65 66 69 6e 65  ACHE) || !define
15e16 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  d(SQLITE_OMIT_AU
15e17 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 69 66 64  TOVACUUM).  #ifd
15e18 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  ef SQLITE_OMIT_M
15e19 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 6f 6e 73  EMORYDB.    cons
15e1a 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20 3d 20  t int isMemdb = 
15e1b 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 63  0;.  #else.    c
15e1c 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62  onst int isMemdb
15e1d 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20   = zFilename && 
15e1e 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d  !strcmp(zFilenam
15e1f 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29 3b 0a  e, ":memory:");.
15e20 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a    #endif.#endif.
15e21 0a 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30  .  assert( db!=0
15e22 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
15e23 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
15e24 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  (db->mutex) );..
15e25 20 20 70 56 66 73 20 3d 20 64 62 2d 3e 70 56 66    pVfs = db->pVf
15e26 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  s;.  p = sqlite3
15e27 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f  MallocZero(sizeo
15e28 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69 66 28  f(Btree));.  if(
15e29 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72   !p ){.    retur
15e2a 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
15e2b 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61 6e 73    }.  p->inTrans
15e2c 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20   = TRANS_NONE;. 
15e2d 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 23 69 66   p->db = db;.#if
15e2e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15e2f 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
15e30 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 20 3d  p->lock.pBtree =
15e31 20 70 3b 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 69 54   p;.  p->lock.iT
15e32 61 62 6c 65 20 3d 20 31 3b 0a 23 65 6e 64 69 66  able = 1;.#endif
15e33 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
15e34 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
15e35 44 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66  D_CACHE) && !def
15e36 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
15e37 5f 44 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20  _DISKIO).  /*.  
15e38 2a 2a 20 49 66 20 74 68 69 73 20 42 74 72 65 65  ** If this Btree
15e39 20 69 73 20 61 20 63 61 6e 64 69 64 61 74 65 20   is a candidate 
15e3a 66 6f 72 20 73 68 61 72 65 64 20 63 61 63 68 65  for shared cache
15e3b 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 6e  , try to find an
15e3c 0a 20 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 42  .  ** existing B
15e3d 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74  tShared object t
15e3e 68 61 74 20 77 65 20 63 61 6e 20 73 68 61 72 65  hat we can share
15e3f 20 77 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28   with.  */.  if(
15e40 20 69 73 4d 65 6d 64 62 3d 3d 30 20 26 26 20 7a   isMemdb==0 && z
15e41 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c  Filename && zFil
15e42 65 6e 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20  ename[0] ){.    
15e43 69 66 28 20 76 66 73 46 6c 61 67 73 20 26 20 53  if( vfsFlags & S
15e44 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45  QLITE_OPEN_SHARE
15e45 44 43 41 43 48 45 20 29 7b 0a 20 20 20 20 20 20  DCACHE ){.      
15e46 69 6e 74 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d  int nFullPathnam
15e47 65 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68  e = pVfs->mxPath
15e48 6e 61 6d 65 2b 31 3b 0a 20 20 20 20 20 20 63 68  name+1;.      ch
15e49 61 72 20 2a 7a 46 75 6c 6c 50 61 74 68 6e 61 6d  ar *zFullPathnam
15e4a 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  e = sqlite3Mallo
15e4b 63 28 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  c(nFullPathname)
15e4c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
15e4d 6d 75 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72  mutex *mutexShar
15e4e 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 73 68 61  ed;.      p->sha
15e4f 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 20  rable = 1;.     
15e50 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 68 6e   if( !zFullPathn
15e51 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ame ){.        s
15e52 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
15e53 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
15e54 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
15e55 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
15e56 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65  e3OsFullPathname
15e57 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d 65  (pVfs, zFilename
15e58 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c  , nFullPathname,
15e59 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b   zFullPathname);
15e5a 0a 20 20 20 20 20 20 6d 75 74 65 78 4f 70 65 6e  .      mutexOpen
15e5b 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
15e5c 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
15e5d 58 5f 53 54 41 54 49 43 5f 4f 50 45 4e 29 3b 0a  X_STATIC_OPEN);.
15e5e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
15e5f 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 4f  tex_enter(mutexO
15e60 70 65 6e 29 3b 0a 20 20 20 20 20 20 6d 75 74 65  pen);.      mute
15e61 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74 65  xShared = sqlite
15e62 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
15e63 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
15e64 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20 73  MASTER);.      s
15e65 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
15e66 65 72 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b  er(mutexShared);
15e67 0a 20 20 20 20 20 20 66 6f 72 28 70 42 74 3d 47  .      for(pBt=G
15e68 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a 2c  LOBAL(BtShared*,
15e69 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63  sqlite3SharedCac
15e6a 68 65 4c 69 73 74 29 3b 20 70 42 74 3b 20 70 42  heList); pBt; pB
15e6b 74 3d 70 42 74 2d 3e 70 4e 65 78 74 29 7b 0a 20  t=pBt->pNext){. 
15e6c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
15e6d 42 74 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20  Bt->nRef>0 );.  
15e6e 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 74 72        if( 0==str
15e6f 63 6d 70 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d  cmp(zFullPathnam
15e70 65 2c 20 73 71 6c 69 74 65 33 50 61 67 65 72 46  e, sqlite3PagerF
15e71 69 6c 65 6e 61 6d 65 28 70 42 74 2d 3e 70 50 61  ilename(pBt->pPa
15e72 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 20 20  ger)).          
15e73 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
15e74 33 50 61 67 65 72 56 66 73 28 70 42 74 2d 3e 70  3PagerVfs(pBt->p
15e75 50 61 67 65 72 29 3d 3d 70 56 66 73 20 29 7b 0a  Pager)==pVfs ){.
15e76 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 44            int iD
15e77 62 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  b;.          for
15e78 28 69 44 62 3d 64 62 2d 3e 6e 44 62 2d 31 3b 20  (iDb=db->nDb-1; 
15e79 69 44 62 3e 3d 30 3b 20 69 44 62 2d 2d 29 7b 0a  iDb>=0; iDb--){.
15e7a 20 20 20 20 20 20 20 20 20 20 20 20 42 74 72 65              Btre
15e7b 65 20 2a 70 45 78 69 73 74 69 6e 67 20 3d 20 64  e *pExisting = d
15e7c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b  b->aDb[iDb].pBt;
15e7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
15e7e 20 70 45 78 69 73 74 69 6e 67 20 26 26 20 70 45   pExisting && pE
15e7f 78 69 73 74 69 6e 67 2d 3e 70 42 74 3d 3d 70 42  xisting->pBt==pB
15e80 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
15e81 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
15e82 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72  _leave(mutexShar
15e83 65 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ed);.           
15e84 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
15e85 5f 6c 65 61 76 65 28 6d 75 74 65 78 4f 70 65 6e  _leave(mutexOpen
15e86 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
15e87 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46   sqlite3_free(zF
15e88 75 6c 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20  ullPathname);.  
15e89 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
15e8a 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20  te3_free(p);.   
15e8b 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
15e8c 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  n SQLITE_CONSTRA
15e8d 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20  INT;.           
15e8e 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
15e8f 20 20 20 20 20 20 20 20 20 70 2d 3e 70 42 74 20           p->pBt 
15e90 3d 20 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20  = pBt;.         
15e91 20 70 42 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20   pBt->nRef++;.  
15e92 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
15e93 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
15e94 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
15e95 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
15e96 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 73  Shared);.      s
15e97 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 46 75 6c  qlite3_free(zFul
15e98 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  lPathname);.    
15e99 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
15e9a 44 45 42 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a  DEBUG.    else{.
15e9b 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 65 62 75        /* In debu
15e9c 67 20 6d 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20  g mode, we mark 
15e9d 61 6c 6c 20 70 65 72 73 69 73 74 65 6e 74 20 64  all persistent d
15e9e 61 74 61 62 61 73 65 73 20 61 73 20 73 68 61 72  atabases as shar
15e9f 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 65 76  able.      ** ev
15ea0 65 6e 20 77 68 65 6e 20 74 68 65 79 20 61 72 65  en when they are
15ea1 20 6e 6f 74 2e 20 20 54 68 69 73 20 65 78 65 72   not.  This exer
15ea2 63 69 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e  cises the lockin
15ea3 67 20 63 6f 64 65 20 61 6e 64 0a 20 20 20 20 20  g code and.     
15ea4 20 2a 2a 20 67 69 76 65 73 20 6d 6f 72 65 20 6f   ** gives more o
15ea5 70 70 6f 72 74 75 6e 69 74 79 20 66 6f 72 20 61  pportunity for a
15ea6 73 73 65 72 74 73 28 73 71 6c 69 74 65 33 5f 6d  sserts(sqlite3_m
15ea7 75 74 65 78 5f 68 65 6c 64 28 29 29 0a 20 20 20  utex_held()).   
15ea8 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73     ** statements
15ea9 20 74 6f 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67   to find locking
15eaa 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20   problems..     
15eab 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 73 68 61   */.      p->sha
15eac 72 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d  rable = 1;.    }
15ead 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64  .#endif.  }.#end
15eae 69 66 0a 20 20 69 66 28 20 70 42 74 3d 3d 30 20  if.  if( pBt==0 
15eaf 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a  ){.    /*.    **
15eb0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
15eb1 73 73 65 72 74 73 20 6d 61 6b 65 20 73 75 72 65  sserts make sure
15eb2 20 74 68 61 74 20 73 74 72 75 63 74 75 72 65 73   that structures
15eb3 20 75 73 65 64 20 62 79 20 74 68 65 20 62 74 72   used by the btr
15eb4 65 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 74 68  ee are.    ** th
15eb5 65 20 72 69 67 68 74 20 73 69 7a 65 2e 20 20 54  e right size.  T
15eb6 68 69 73 20 69 73 20 74 6f 20 67 75 61 72 64 20  his is to guard 
15eb7 61 67 61 69 6e 73 74 20 73 69 7a 65 20 63 68 61  against size cha
15eb8 6e 67 65 73 20 74 68 61 74 20 72 65 73 75 6c 74  nges that result
15eb9 0a 20 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d  .    ** when com
15eba 70 69 6c 69 6e 67 20 6f 6e 20 61 20 64 69 66 66  piling on a diff
15ebb 65 72 65 6e 74 20 61 72 63 68 69 74 65 63 74 75  erent architectu
15ebc 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  re..    */.    a
15ebd 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 69 36  ssert( sizeof(i6
15ebe 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28  4)==8 || sizeof(
15ebf 69 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61  i64)==4 );.    a
15ec0 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 36  ssert( sizeof(u6
15ec1 34 29 3d 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28  4)==8 || sizeof(
15ec2 75 36 34 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61  u64)==4 );.    a
15ec3 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 75 33  ssert( sizeof(u3
15ec4 32 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73  2)==4 );.    ass
15ec5 65 72 74 28 20 73 69 7a 65 6f 66 28 75 31 36 29  ert( sizeof(u16)
15ec6 3d 3d 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==2 );.    asser
15ec7 74 28 20 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d  t( sizeof(Pgno)=
15ec8 3d 34 20 29 3b 0a 20 20 0a 20 20 20 20 70 42 74  =4 );.  .    pBt
15ec9 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
15eca 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42  Zero( sizeof(*pB
15ecb 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 42  t) );.    if( pB
15ecc 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63  t==0 ){.      rc
15ecd 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
15ece 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 74 72 65  .      goto btre
15ecf 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  e_open_out;.    
15ed0 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
15ed1 65 33 50 61 67 65 72 4f 70 65 6e 28 70 56 66 73  e3PagerOpen(pVfs
15ed2 2c 20 26 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  , &pBt->pPager, 
15ed3 7a 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20  zFilename,.     
15ed4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15ed5 20 20 20 20 20 45 58 54 52 41 5f 53 49 5a 45 2c       EXTRA_SIZE,
15ed6 20 66 6c 61 67 73 2c 20 76 66 73 46 6c 61 67 73   flags, vfsFlags
15ed7 2c 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a 20  , pageReinit);. 
15ed8 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
15ed9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
15eda 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52   = sqlite3PagerR
15edb 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 70 42  eadFileheader(pB
15edc 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a 65 6f 66  t->pPager,sizeof
15edd 28 7a 44 62 48 65 61 64 65 72 29 2c 7a 44 62 48  (zDbHeader),zDbH
15ede 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  eader);.    }.  
15edf 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
15ee0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  _OK ){.      got
15ee1 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74  o btree_open_out
15ee2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d  ;.    }.    pBt-
15ee3 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 73 71  >db = db;.    sq
15ee4 6c 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73  lite3PagerSetBus
15ee5 79 68 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50  yhandler(pBt->pP
15ee6 61 67 65 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b  ager, btreeInvok
15ee7 65 42 75 73 79 48 61 6e 64 6c 65 72 2c 20 70 42  eBusyHandler, pB
15ee8 74 29 3b 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d  t);.    p->pBt =
15ee9 20 70 42 74 3b 0a 20 20 0a 20 20 20 20 70 42 74   pBt;.  .    pBt
15eea 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20  ->pCursor = 0;. 
15eeb 20 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d     pBt->pPage1 =
15eec 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 72 65 61   0;.    pBt->rea
15eed 64 4f 6e 6c 79 20 3d 20 73 71 6c 69 74 65 33 50  dOnly = sqlite3P
15eee 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 70  agerIsreadonly(p
15eef 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
15ef0 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d   pBt->pageSize =
15ef1 20 67 65 74 32 62 79 74 65 28 26 7a 44 62 48 65   get2byte(&zDbHe
15ef2 61 64 65 72 5b 31 36 5d 29 3b 0a 20 20 20 20 69  ader[16]);.    i
15ef3 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  f( pBt->pageSize
15ef4 3c 35 31 32 20 7c 7c 20 70 42 74 2d 3e 70 61 67  <512 || pBt->pag
15ef5 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41 58  eSize>SQLITE_MAX
15ef6 5f 50 41 47 45 5f 53 49 5a 45 0a 20 20 20 20 20  _PAGE_SIZE.     
15ef7 20 20 20 20 7c 7c 20 28 28 70 42 74 2d 3e 70 61      || ((pBt->pa
15ef8 67 65 53 69 7a 65 2d 31 29 26 70 42 74 2d 3e 70  geSize-1)&pBt->p
15ef9 61 67 65 53 69 7a 65 29 21 3d 30 20 29 7b 0a 20  ageSize)!=0 ){. 
15efa 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53 69       pBt->pageSi
15efb 7a 65 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20  ze = 0;.#ifndef 
15efc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
15efd 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a 20  VACUUM.      /* 
15efe 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e 61 6d  If the magic nam
15eff 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 69 6c  e ":memory:" wil
15f00 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e 2d 6d  l create an in-m
15f01 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c 20  emory database, 
15f02 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6c 65  then.      ** le
15f03 61 76 65 20 74 68 65 20 61 75 74 6f 56 61 63 75  ave the autoVacu
15f04 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 64 6f  um mode at 0 (do
15f05 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 75 6d   not auto-vacuum
15f06 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20 20  ), even if.     
15f07 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41 55   ** SQLITE_DEFAU
15f08 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69 73  LT_AUTOVACUUM is
15f09 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 6f 74   true. On the ot
15f0a 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 20 20  her hand, if.   
15f0b 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49     ** SQLITE_OMI
15f0c 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 20 62  T_MEMORYDB has b
15f0d 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 68 65  een defined, the
15f0e 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 73 20  n ":memory:" is 
15f0f 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a 2a 20  just a.      ** 
15f10 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e 61 6d  regular file-nam
15f11 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  e. In this case 
15f12 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20  the auto-vacuum 
15f13 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 20 6e  applies as per n
15f14 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a  ormal..      */.
15f15 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 6e        if( zFilen
15f16 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 62 20  ame && !isMemdb 
15f17 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e  ){.        pBt->
15f18 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 53 51  autoVacuum = (SQ
15f19 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54  LITE_DEFAULT_AUT
15f1a 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 30 29  OVACUUM ? 1 : 0)
15f1b 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 69  ;.        pBt->i
15f1c 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53 51 4c  ncrVacuum = (SQL
15f1d 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f  ITE_DEFAULT_AUTO
15f1e 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 3a 20  VACUUM==2 ? 1 : 
15f1f 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  0);.      }.#end
15f20 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76  if.      nReserv
15f21 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  e = 0;.    }else
15f22 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76 65  {.      nReserve
15f23 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 30 5d   = zDbHeader[20]
15f24 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67  ;.      pBt->pag
15f25 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b 0a  eSizeFixed = 1;.
15f26 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
15f27 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
15f28 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61       pBt->autoVa
15f29 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65  cuum = (get4byte
15f2a 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b  (&zDbHeader[36 +
15f2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20 20   4*4])?1:0);.   
15f2c 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75     pBt->incrVacu
15f2d 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26  um = (get4byte(&
15f2e 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20 37  zDbHeader[36 + 7
15f2f 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64 69  *4])?1:0);.#endi
15f30 66 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  f.    }.    rc =
15f31 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
15f32 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50  Pagesize(pBt->pP
15f33 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65  ager, &pBt->page
15f34 53 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b  Size, nReserve);
15f35 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f  .    if( rc ) go
15f36 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75  to btree_open_ou
15f37 74 3b 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62  t;.    pBt->usab
15f38 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61  leSize = pBt->pa
15f39 67 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76  geSize - nReserv
15f3a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  e;.    assert( (
15f3b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 20  pBt->pageSize & 
15f3c 37 29 3d 3d 30 20 29 3b 20 20 2f 2a 20 38 2d 62  7)==0 );  /* 8-b
15f3d 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66  yte alignment of
15f3e 20 70 61 67 65 53 69 7a 65 20 2a 2f 0a 20 20 20   pageSize */.   
15f3f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
15f40 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
15f41 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69  _CACHE) && !defi
15f42 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
15f43 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41  DISKIO).    /* A
15f44 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68 61  dd the new BtSha
15f45 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 68  red object to th
15f46 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 68  e linked list sh
15f47 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 73  arable BtShareds
15f48 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
15f49 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a   p->sharable ){.
15f4a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
15f4b 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64  tex *mutexShared
15f4c 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65  ;.      pBt->nRe
15f4d 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 6d 75 74  f = 1;.      mut
15f4e 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74  exShared = sqlit
15f4f 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
15f50 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
15f51 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20  _MASTER);.      
15f52 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 41  if( SQLITE_THREA
15f53 44 53 41 46 45 20 26 26 20 73 71 6c 69 74 65 33  DSAFE && sqlite3
15f54 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f  GlobalConfig.bCo
15f55 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 20  reMutex ){.     
15f56 20 20 20 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20     pBt->mutex = 
15f57 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
15f58 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  c(SQLITE_MUTEX_F
15f59 41 53 54 29 3b 0a 20 20 20 20 20 20 20 20 69 66  AST);.        if
15f5a 28 20 70 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20  ( pBt->mutex==0 
15f5b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
15f5c 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
15f5d 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61            db->ma
15f5e 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
15f5f 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62            goto b
15f60 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  tree_open_out;. 
15f61 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
15f62 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
15f63 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
15f64 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70  Shared);.      p
15f65 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42  Bt->pNext = GLOB
15f66 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c  AL(BtShared*,sql
15f67 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
15f68 69 73 74 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42  ist);.      GLOB
15f69 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c  AL(BtShared*,sql
15f6a 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
15f6b 69 73 74 29 20 3d 20 70 42 74 3b 0a 20 20 20 20  ist) = pBt;.    
15f6c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
15f6d 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65  leave(mutexShare
15f6e 64 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  d);.    }.#endif
15f6f 0a 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e  .  }..#if !defin
15f70 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
15f71 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20  HARED_CACHE) && 
15f72 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
15f73 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f  OMIT_DISKIO).  /
15f74 2a 20 49 66 20 74 68 65 20 6e 65 77 20 42 74 72  * If the new Btr
15f75 65 65 20 75 73 65 73 20 61 20 73 68 61 72 61 62  ee uses a sharab
15f76 6c 65 20 70 42 74 53 68 61 72 65 64 2c 20 74 68  le pBtShared, th
15f77 65 6e 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a  en link the new.
15f78 20 20 2a 2a 20 42 74 72 65 65 20 69 6e 74 6f 20    ** Btree into 
15f79 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  the list of all 
15f7a 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20  sharable Btrees 
15f7b 66 6f 72 20 74 68 65 20 73 61 6d 65 20 63 6f 6e  for the same con
15f7c 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68  nection..  ** Th
15f7d 65 20 6c 69 73 74 20 69 73 20 6b 65 70 74 20 69  e list is kept i
15f7e 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
15f7f 72 20 62 79 20 70 42 74 20 61 64 64 72 65 73 73  r by pBt address
15f80 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
15f81 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
15f82 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 72 65 65  int i;.    Btree
15f83 20 2a 70 53 69 62 3b 0a 20 20 20 20 66 6f 72 28   *pSib;.    for(
15f84 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
15f85 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
15f86 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b  (pSib = db->aDb[
15f87 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70 53  i].pBt)!=0 && pS
15f88 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a  ib->sharable ){.
15f89 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
15f8a 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70 53  Sib->pPrev ){ pS
15f8b 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65 76  ib = pSib->pPrev
15f8c 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
15f8d 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70 42 74  p->pBt<pSib->pBt
15f8e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d   ){.          p-
15f8f 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 3b 0a 20  >pNext = pSib;. 
15f90 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 65           p->pPre
15f91 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  v = 0;.         
15f92 20 70 53 69 62 2d 3e 70 50 72 65 76 20 3d 20 70   pSib->pPrev = p
15f93 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
15f94 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
15f95 28 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 26 26  ( pSib->pNext &&
15f96 20 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42   pSib->pNext->pB
15f97 74 3c 70 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20  t<p->pBt ){.    
15f98 20 20 20 20 20 20 20 20 70 53 69 62 20 3d 20 70          pSib = p
15f99 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Sib->pNext;.    
15f9a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
15f9b 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69    p->pNext = pSi
15f9c 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  b->pNext;.      
15f9d 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 70      p->pPrev = p
15f9e 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 69  Sib;.          i
15f9f 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  f( p->pNext ){. 
15fa0 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e             p->pN
15fa1 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a  ext->pPrev = p;.
15fa2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
15fa3 20 20 20 20 20 20 70 53 69 62 2d 3e 70 4e 65 78        pSib->pNex
15fa4 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 7d  t = p;.        }
15fa5 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
15fa6 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
15fa7 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70 42 74  }.#endif.  *ppBt
15fa8 72 65 65 20 3d 20 70 3b 0a 0a 62 74 72 65 65 5f  ree = p;..btree_
15fa9 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20  open_out:.  if( 
15faa 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
15fab 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 26 20  .    if( pBt && 
15fac 70 42 74 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20  pBt->pPager ){. 
15fad 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
15fae 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 67  rClose(pBt->pPag
15faf 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  er);.    }.    s
15fb0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29  qlite3_free(pBt)
15fb1 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
15fb2 65 65 28 70 29 3b 0a 20 20 20 20 2a 70 70 42 74  ee(p);.    *ppBt
15fb3 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  ree = 0;.  }.  i
15fb4 66 28 20 6d 75 74 65 78 4f 70 65 6e 20 29 7b 0a  f( mutexOpen ){.
15fb5 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
15fb6 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
15fb7 75 74 65 78 4f 70 65 6e 29 20 29 3b 0a 20 20 20  utexOpen) );.   
15fb8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
15fb9 65 61 76 65 28 6d 75 74 65 78 4f 70 65 6e 29 3b  eave(mutexOpen);
15fba 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
15fbb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65  ;.}../*.** Decre
15fbc 6d 65 6e 74 20 74 68 65 20 42 74 53 68 61 72 65  ment the BtShare
15fbd 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65 72 2e 20  d.nRef counter. 
15fbe 20 57 68 65 6e 20 69 74 20 72 65 61 63 68 65 73   When it reaches
15fbf 20 7a 65 72 6f 2c 0a 2a 2a 20 72 65 6d 6f 76 65   zero,.** remove
15fc0 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73 74   the BtShared st
15fc1 72 75 63 74 75 72 65 20 66 72 6f 6d 20 74 68 65  ructure from the
15fc2 20 73 68 61 72 69 6e 67 20 6c 69 73 74 2e 20 20   sharing list.  
15fc3 52 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69  Return.** true i
15fc4 66 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 6e  f the BtShared.n
15fc5 52 65 66 20 63 6f 75 6e 74 65 72 20 72 65 61 63  Ref counter reac
15fc6 68 65 73 20 7a 65 72 6f 20 61 6e 64 20 72 65 74  hes zero and ret
15fc7 75 72 6e 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20  urn.** false if 
15fc8 69 74 20 69 73 20 73 74 69 6c 6c 20 70 6f 73 69  it is still posi
15fc9 74 69 76 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tive..*/.static 
15fca 69 6e 74 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68  int removeFromSh
15fcb 61 72 69 6e 67 4c 69 73 74 28 42 74 53 68 61 72  aringList(BtShar
15fcc 65 64 20 2a 70 42 74 29 7b 0a 23 69 66 6e 64 65  ed *pBt){.#ifnde
15fcd 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
15fce 41 52 45 44 5f 43 41 43 48 45 0a 20 20 73 71 6c  ARED_CACHE.  sql
15fcf 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73  ite3_mutex *pMas
15fd0 74 65 72 3b 0a 20 20 42 74 53 68 61 72 65 64 20  ter;.  BtShared 
15fd1 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 72 65  *pList;.  int re
15fd2 6d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 61 73  moved = 0;..  as
15fd3 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15fd4 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 42 74 2d  tex_notheld(pBt-
15fd5 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 61  >mutex) );.  pMa
15fd6 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33 4d 75  ster = sqlite3Mu
15fd7 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
15fd8 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
15fd9 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  TER);.  sqlite3_
15fda 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73  mutex_enter(pMas
15fdb 74 65 72 29 3b 0a 20 20 70 42 74 2d 3e 6e 52 65  ter);.  pBt->nRe
15fdc 66 2d 2d 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e  f--;.  if( pBt->
15fdd 6e 52 65 66 3c 3d 30 20 29 7b 0a 20 20 20 20 69  nRef<=0 ){.    i
15fde 66 28 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72  f( GLOBAL(BtShar
15fdf 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65  ed*,sqlite3Share
15fe0 64 43 61 63 68 65 4c 69 73 74 29 3d 3d 70 42 74  dCacheList)==pBt
15fe1 20 29 7b 0a 20 20 20 20 20 20 47 4c 4f 42 41 4c   ){.      GLOBAL
15fe2 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74  (BtShared*,sqlit
15fe3 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73  e3SharedCacheLis
15fe4 74 29 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b  t) = pBt->pNext;
15fe5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
15fe6 20 20 70 4c 69 73 74 20 3d 20 47 4c 4f 42 41 4c    pList = GLOBAL
15fe7 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74  (BtShared*,sqlit
15fe8 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73  e3SharedCacheLis
15fe9 74 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28  t);.      while(
15fea 20 41 4c 57 41 59 53 28 70 4c 69 73 74 29 20 26   ALWAYS(pList) &
15feb 26 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74 21 3d  & pList->pNext!=
15fec 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70  pBt ){.        p
15fed 4c 69 73 74 3d 70 4c 69 73 74 2d 3e 70 4e 65 78  List=pList->pNex
15fee 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
15fef 20 69 66 28 20 41 4c 57 41 59 53 28 70 4c 69 73   if( ALWAYS(pLis
15ff0 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c  t) ){.        pL
15ff1 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 42 74  ist->pNext = pBt
15ff2 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
15ff3 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 53  .    }.    if( S
15ff4 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
15ff5 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
15ff6 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 70 42 74  3_mutex_free(pBt
15ff7 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a  ->mutex);.    }.
15ff8 20 20 20 20 72 65 6d 6f 76 65 64 20 3d 20 31 3b      removed = 1;
15ff9 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
15ffa 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74  utex_leave(pMast
15ffb 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65  er);.  return re
15ffc 6d 6f 76 65 64 3b 0a 23 65 6c 73 65 0a 20 20 72  moved;.#else.  r
15ffd 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a  eturn 1;.#endif.
15ffe 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
15fff 72 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63  re pBt->pTmpSpac
16000 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61  e points to an a
16001 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 0a 2a 2a  llocation of .**
16002 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42   MX_CELL_SIZE(pB
16003 74 29 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61  t) bytes..*/.sta
16004 74 69 63 20 76 6f 69 64 20 61 6c 6c 6f 63 61 74  tic void allocat
16005 65 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61  eTempSpace(BtSha
16006 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 66 28  red *pBt){.  if(
16007 20 21 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65   !pBt->pTmpSpace
16008 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 54 6d   ){.    pBt->pTm
16009 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74 65 33  pSpace = sqlite3
1600a 50 61 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d  PageMalloc( pBt-
1600b 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 7d  >pageSize );.  }
1600c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74  .}../*.** Free t
1600d 68 65 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63  he pBt->pTmpSpac
1600e 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a  e allocation.*/.
1600f 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65  static void free
16010 54 65 6d 70 53 70 61 63 65 28 42 74 53 68 61 72  TempSpace(BtShar
16011 65 64 20 2a 70 42 74 29 7b 0a 20 20 73 71 6c 69  ed *pBt){.  sqli
16012 74 65 33 50 61 67 65 46 72 65 65 28 20 70 42 74  te3PageFree( pBt
16013 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20  ->pTmpSpace);.  
16014 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d  pBt->pTmpSpace =
16015 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f   0;.}../*.** Clo
16016 73 65 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62  se an open datab
16017 61 73 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 61  ase and invalida
16018 74 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 2e 0a  te all cursors..
16019 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1601a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1601b 65 65 43 6c 6f 73 65 28 42 74 72 65 65 20 2a 70  eeClose(Btree *p
1601c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
1601d 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 42  Bt = p->pBt;.  B
1601e 74 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 0a  tCursor *pCur;..
1601f 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 63    /* Close all c
16020 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 20 76 69  ursors opened vi
16021 61 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 20  a this handle.  
16022 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  */.  assert( sql
16023 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
16024 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
16025 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
16026 6e 74 65 72 28 70 29 3b 0a 20 20 70 43 75 72 20  nter(p);.  pCur 
16027 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a  = pBt->pCursor;.
16028 20 20 77 68 69 6c 65 28 20 70 43 75 72 20 29 7b    while( pCur ){
16029 0a 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70  .    BtCursor *p
1602a 54 6d 70 20 3d 20 70 43 75 72 3b 0a 20 20 20 20  Tmp = pCur;.    
1602b 70 43 75 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65  pCur = pCur->pNe
1602c 78 74 3b 0a 20 20 20 20 69 66 28 20 70 54 6d 70  xt;.    if( pTmp
1602d 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b 0a 20  ->pBtree==p ){. 
1602e 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1602f 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 54 6d  eCloseCursor(pTm
16030 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  p);.    }.  }.. 
16031 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6e 79   /* Rollback any
16032 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63 74   active transact
16033 69 6f 6e 20 61 6e 64 20 66 72 65 65 20 74 68 65  ion and free the
16034 20 68 61 6e 64 6c 65 20 73 74 72 75 63 74 75 72   handle structur
16035 65 2e 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c  e..  ** The call
16036 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
16037 52 6f 6c 6c 62 61 63 6b 28 29 20 64 72 6f 70 73  Rollback() drops
16038 20 61 6e 79 20 74 61 62 6c 65 2d 6c 6f 63 6b 73   any table-locks
16039 20 68 65 6c 64 20 62 79 0a 20 20 2a 2a 20 74 68   held by.  ** th
1603a 69 73 20 68 61 6e 64 6c 65 2e 0a 20 20 2a 2f 0a  is handle..  */.
1603b 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f    sqlite3BtreeRo
1603c 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 73 71 6c  llback(p);.  sql
1603d 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1603e 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  );..  /* If ther
1603f 65 20 61 72 65 20 73 74 69 6c 6c 20 6f 74 68 65  e are still othe
16040 72 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72 65  r outstanding re
16041 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
16042 73 68 61 72 65 64 2d 62 74 72 65 65 0a 20 20 2a  shared-btree.  *
16043 2a 20 73 74 72 75 63 74 75 72 65 2c 20 72 65 74  * structure, ret
16044 75 72 6e 20 6e 6f 77 2e 20 54 68 65 20 72 65 6d  urn now. The rem
16045 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 70  ainder of this p
16046 72 6f 63 65 64 75 72 65 20 63 6c 65 61 6e 73 20  rocedure cleans 
16047 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 73 68 61  .  ** up the sha
16048 72 65 64 2d 62 74 72 65 65 2e 0a 20 20 2a 2f 0a  red-btree..  */.
16049 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e    assert( p->wan
1604a 74 54 6f 4c 6f 63 6b 3d 3d 30 20 26 26 20 70 2d  tToLock==0 && p-
1604b 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20  >locked==0 );.  
1604c 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65  if( !p->sharable
1604d 20 7c 7c 20 72 65 6d 6f 76 65 46 72 6f 6d 53 68   || removeFromSh
1604e 61 72 69 6e 67 4c 69 73 74 28 70 42 74 29 20 29  aringList(pBt) )
1604f 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 42 74  {.    /* The pBt
16050 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6f 6e   is no longer on
16051 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c 69 73   the sharing lis
16052 74 2c 20 73 6f 20 77 65 20 63 61 6e 20 61 63 63  t, so we can acc
16053 65 73 73 0a 20 20 20 20 2a 2a 20 69 74 20 77 69  ess.    ** it wi
16054 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20  thout having to 
16055 68 6f 6c 64 20 74 68 65 20 6d 75 74 65 78 2e 0a  hold the mutex..
16056 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c      **.    ** Cl
16057 65 61 6e 20 6f 75 74 20 61 6e 64 20 64 65 6c 65  ean out and dele
16058 74 65 20 74 68 65 20 42 74 53 68 61 72 65 64 20  te the BtShared 
16059 6f 62 6a 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20  object..    */. 
1605a 20 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d     assert( !pBt-
1605b 3e 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20  >pCursor );.    
1605c 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f 73  sqlite3PagerClos
1605d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a  e(pBt->pPager);.
1605e 20 20 20 20 69 66 28 20 70 42 74 2d 3e 78 46 72      if( pBt->xFr
1605f 65 65 53 63 68 65 6d 61 20 26 26 20 70 42 74 2d  eeSchema && pBt-
16060 3e 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20  >pSchema ){.    
16061 20 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68 65    pBt->xFreeSche
16062 6d 61 28 70 42 74 2d 3e 70 53 63 68 65 6d 61 29  ma(pBt->pSchema)
16063 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
16064 74 65 33 5f 66 72 65 65 28 70 42 74 2d 3e 70 53  te3_free(pBt->pS
16065 63 68 65 6d 61 29 3b 0a 20 20 20 20 66 72 65 65  chema);.    free
16066 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a  TempSpace(pBt);.
16067 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
16068 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e  (pBt);.  }..#ifn
16069 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1606a 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 61  SHARED_CACHE.  a
1606b 73 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f  ssert( p->wantTo
1606c 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Lock==0 );.  ass
1606d 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d  ert( p->locked==
1606e 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 50  0 );.  if( p->pP
1606f 72 65 76 20 29 20 70 2d 3e 70 50 72 65 76 2d 3e  rev ) p->pPrev->
16070 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74  pNext = p->pNext
16071 3b 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74  ;.  if( p->pNext
16072 20 29 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72   ) p->pNext->pPr
16073 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 23  ev = p->pPrev;.#
16074 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
16075 5f 66 72 65 65 28 70 29 3b 0a 20 20 72 65 74 75  _free(p);.  retu
16076 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
16077 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
16078 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65 20 6e  e limit on the n
16079 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 61  umber of pages a
1607a 6c 6c 6f 77 65 64 20 69 6e 20 74 68 65 20 63 61  llowed in the ca
1607b 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  che..**.** The m
1607c 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
1607d 20 63 61 63 68 65 20 70 61 67 65 73 20 69 73 20   cache pages is 
1607e 73 65 74 20 74 6f 20 74 68 65 20 61 62 73 6f 6c  set to the absol
1607f 75 74 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20  ute.** value of 
16080 6d 78 50 61 67 65 2e 20 20 49 66 20 6d 78 50 61  mxPage.  If mxPa
16081 67 65 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20  ge is negative, 
16082 74 68 65 20 70 61 67 65 72 20 77 69 6c 6c 0a 2a  the pager will.*
16083 2a 20 6f 70 65 72 61 74 65 20 61 73 79 6e 63 68  * operate asynch
16084 72 6f 6e 6f 75 73 6c 79 20 2d 20 69 74 20 77 69  ronously - it wi
16085 6c 6c 20 6e 6f 74 20 73 74 6f 70 20 74 6f 20 64  ll not stop to d
16086 6f 20 66 73 79 6e 63 28 29 73 0a 2a 2a 20 74 6f  o fsync()s.** to
16087 20 69 6e 73 75 72 65 20 64 61 74 61 20 69 73 20   insure data is 
16088 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64  written to the d
16089 69 73 6b 20 73 75 72 66 61 63 65 20 62 65 66 6f  isk surface befo
1608a 72 65 0a 2a 2a 20 63 6f 6e 74 69 6e 75 69 6e 67  re.** continuing
1608b 2e 20 20 54 72 61 6e 73 61 63 74 69 6f 6e 73 20  .  Transactions 
1608c 73 74 69 6c 6c 20 77 6f 72 6b 20 69 66 20 73 79  still work if sy
1608d 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66  nchronous is off
1608e 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61 74  ,.** and the dat
1608f 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62 65 20  abase cannot be 
16090 63 6f 72 72 75 70 74 65 64 20 69 66 20 74 68 69  corrupted if thi
16091 73 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 63 72 61  s program.** cra
16092 73 68 65 73 2e 20 20 42 75 74 20 69 66 20 74 68  shes.  But if th
16093 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
16094 65 6d 20 63 72 61 73 68 65 73 20 6f 72 20 74 68  em crashes or th
16095 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 61 62 72  ere is.** an abr
16096 75 70 74 20 70 6f 77 65 72 20 66 61 69 6c 75 72  upt power failur
16097 65 20 77 68 65 6e 20 73 79 6e 63 68 72 6f 6e 6f  e when synchrono
16098 75 73 20 69 73 20 6f 66 66 2c 20 74 68 65 20 64  us is off, the d
16099 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 75 6c 64  atabase.** could
1609a 20 62 65 20 6c 65 66 74 20 69 6e 20 61 6e 20 69   be left in an i
1609b 6e 63 6f 6e 73 69 73 74 65 6e 74 20 61 6e 64 20  nconsistent and 
1609c 75 6e 72 65 63 6f 76 65 72 61 62 6c 65 20 73 74  unrecoverable st
1609d 61 74 65 2e 0a 2a 2a 20 53 79 6e 63 68 72 6f 6e  ate..** Synchron
1609e 6f 75 73 20 69 73 20 6f 6e 20 62 79 20 64 65 66  ous is on by def
1609f 61 75 6c 74 20 73 6f 20 64 61 74 61 62 61 73 65  ault so database
160a0 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 6e   corruption is n
160a1 6f 74 0a 2a 2a 20 6e 6f 72 6d 61 6c 6c 79 20 61  ot.** normally a
160a2 20 77 6f 72 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54   worry..*/.SQLIT
160a3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
160a4 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
160a5 68 65 53 69 7a 65 28 42 74 72 65 65 20 2a 70 2c  heSize(Btree *p,
160a6 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20   int mxPage){.  
160a7 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
160a8 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74  p->pBt;.  assert
160a9 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
160aa 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  held(p->db->mute
160ab 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  x) );.  sqlite3B
160ac 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
160ad 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
160ae 61 63 68 65 73 69 7a 65 28 70 42 74 2d 3e 70 50  achesize(pBt->pP
160af 61 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20  ager, mxPage);. 
160b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
160b1 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
160b2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
160b3 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 77  .** Change the w
160b4 61 79 20 64 61 74 61 20 69 73 20 73 79 6e 63 65  ay data is synce
160b5 64 20 74 6f 20 64 69 73 6b 20 69 6e 20 6f 72 64  d to disk in ord
160b6 65 72 20 74 6f 20 69 6e 63 72 65 61 73 65 20 6f  er to increase o
160b7 72 20 64 65 63 72 65 61 73 65 0a 2a 2a 20 68 6f  r decrease.** ho
160b8 77 20 77 65 6c 6c 20 74 68 65 20 64 61 74 61 62  w well the datab
160b9 61 73 65 20 72 65 73 69 73 74 73 20 64 61 6d 61  ase resists dama
160ba 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61  ge due to OS cra
160bb 73 68 65 73 20 61 6e 64 20 70 6f 77 65 72 0a 2a  shes and power.*
160bc 2a 20 66 61 69 6c 75 72 65 73 2e 20 20 4c 65 76  * failures.  Lev
160bd 65 6c 20 31 20 69 73 20 74 68 65 20 73 61 6d 65  el 1 is the same
160be 20 61 73 20 61 73 79 6e 63 68 72 6f 6e 6f 75 73   as asynchronous
160bf 20 28 6e 6f 20 73 79 6e 63 73 28 29 20 6f 63 63   (no syncs() occ
160c0 75 72 20 61 6e 64 0a 2a 2a 20 74 68 65 72 65 20  ur and.** there 
160c1 69 73 20 61 20 68 69 67 68 20 70 72 6f 62 61 62  is a high probab
160c2 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 29  ility of damage)
160c3 20 20 4c 65 76 65 6c 20 32 20 69 73 20 74 68 65    Level 2 is the
160c4 20 64 65 66 61 75 6c 74 2e 20 20 54 68 65 72 65   default.  There
160c5 0a 2a 2a 20 69 73 20 61 20 76 65 72 79 20 6c 6f  .** is a very lo
160c6 77 20 62 75 74 20 6e 6f 6e 2d 7a 65 72 6f 20 70  w but non-zero p
160c7 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61  robability of da
160c8 6d 61 67 65 2e 20 20 4c 65 76 65 6c 20 33 20 72  mage.  Level 3 r
160c9 65 64 75 63 65 73 20 74 68 65 0a 2a 2a 20 70 72  educes the.** pr
160ca 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64 61 6d  obability of dam
160cb 61 67 65 20 74 6f 20 6e 65 61 72 20 7a 65 72 6f  age to near zero
160cc 20 62 75 74 20 77 69 74 68 20 61 20 77 72 69 74   but with a writ
160cd 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 72 65  e performance re
160ce 64 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e  duction..*/.#ifn
160cf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
160d0 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53 51  PAGER_PRAGMAS.SQ
160d1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
160d2 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
160d3 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72 65  SafetyLevel(Btre
160d4 65 20 2a 70 2c 20 69 6e 74 20 6c 65 76 65 6c 2c  e *p, int level,
160d5 20 69 6e 74 20 66 75 6c 6c 53 79 6e 63 29 7b 0a   int fullSync){.
160d6 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
160d7 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65  = p->pBt;.  asse
160d8 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
160d9 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
160da 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
160db 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
160dc 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65    sqlite3PagerSe
160dd 74 53 61 66 65 74 79 4c 65 76 65 6c 28 70 42 74  tSafetyLevel(pBt
160de 2d 3e 70 50 61 67 65 72 2c 20 6c 65 76 65 6c 2c  ->pPager, level,
160df 20 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 73 71   fullSync);.  sq
160e0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
160e1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  p);.  return SQL
160e2 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
160e3 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
160e4 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65 6e  RUE if the given
160e5 20 62 74 72 65 65 20 69 73 20 73 65 74 20 74 6f   btree is set to
160e6 20 73 61 66 65 74 79 20 6c 65 76 65 6c 20 31 2e   safety level 1.
160e7 20 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f    In other.** wo
160e8 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55 45  rds, return TRUE
160e9 20 69 66 20 6e 6f 20 73 79 6e 63 28 29 20 6f 63   if no sync() oc
160ea 63 75 72 73 20 6f 6e 20 74 68 65 20 64 69 73 6b  curs on the disk
160eb 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   files..*/.SQLIT
160ec 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
160ed 6c 69 74 65 33 42 74 72 65 65 53 79 6e 63 44 69  lite3BtreeSyncDi
160ee 73 61 62 6c 65 64 28 42 74 72 65 65 20 2a 70 29  sabled(Btree *p)
160ef 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
160f0 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69 6e  t = p->pBt;.  in
160f1 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
160f2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
160f3 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
160f4 20 29 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 42   );  .  sqlite3B
160f5 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
160f6 61 73 73 65 72 74 28 20 70 42 74 20 26 26 20 70  assert( pBt && p
160f7 42 74 2d 3e 70 50 61 67 65 72 20 29 3b 0a 20 20  Bt->pPager );.  
160f8 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
160f9 72 4e 6f 73 79 6e 63 28 70 42 74 2d 3e 70 50 61  rNosync(pBt->pPa
160fa 67 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  ger);.  sqlite3B
160fb 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
160fc 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69  return rc;.}..#i
160fd 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
160fe 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
160ff 47 4d 41 53 29 20 7c 7c 20 21 64 65 66 69 6e 65  GMAS) || !define
16100 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  d(SQLITE_OMIT_VA
16101 43 55 55 4d 29 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  CUUM)./*.** Chan
16102 67 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 70  ge the default p
16103 61 67 65 73 20 73 69 7a 65 20 61 6e 64 20 74 68  ages size and th
16104 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 65  e number of rese
16105 72 76 65 64 20 62 79 74 65 73 20 70 65 72 20 70  rved bytes per p
16106 61 67 65 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 74  age..** Or, if t
16107 68 65 20 70 61 67 65 20 73 69 7a 65 20 68 61 73  he page size has
16108 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 66 69   already been fi
16109 78 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  xed, return SQLI
1610a 54 45 5f 52 45 41 44 4f 4e 4c 59 20 0a 2a 2a 20  TE_READONLY .** 
1610b 77 69 74 68 6f 75 74 20 63 68 61 6e 67 69 6e 67  without changing
1610c 20 61 6e 79 74 68 69 6e 67 2e 0a 2a 2a 0a 2a 2a   anything..**.**
1610d 20 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 6d   The page size m
1610e 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f  ust be a power o
1610f 66 20 32 20 62 65 74 77 65 65 6e 20 35 31 32 20  f 2 between 512 
16110 61 6e 64 20 36 35 35 33 36 2e 20 20 49 66 20 74  and 65536.  If t
16111 68 65 20 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20  he page.** size 
16112 73 75 70 70 6c 69 65 64 20 64 6f 65 73 20 6e 6f  supplied does no
16113 74 20 6d 65 65 74 20 74 68 69 73 20 63 6f 6e 73  t meet this cons
16114 74 72 61 69 6e 74 20 74 68 65 6e 20 74 68 65 20  traint then the 
16115 70 61 67 65 20 73 69 7a 65 20 69 73 20 6e 6f 74  page size is not
16116 0a 2a 2a 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a  .** changed..**.
16117 2a 2a 20 50 61 67 65 20 73 69 7a 65 73 20 61 72  ** Page sizes ar
16118 65 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 74 6f  e constrained to
16119 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74   be a power of t
1611a 77 6f 20 73 6f 20 74 68 61 74 20 74 68 65 20 72  wo so that the r
1611b 65 67 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20  egion.** of the 
1611c 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73  database file us
1611d 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 28  ed for locking (
1611e 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 50 45 4e  beginning at PEN
1611f 44 49 4e 47 5f 42 59 54 45 2c 0a 2a 2a 20 74 68  DING_BYTE,.** th
16120 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73  e first byte pas
16121 74 20 74 68 65 20 31 47 42 20 62 6f 75 6e 64 61  t the 1GB bounda
16122 72 79 2c 20 30 78 34 30 30 30 30 30 30 30 29 20  ry, 0x40000000) 
16123 6e 65 65 64 73 20 74 6f 20 6f 63 63 75 72 0a 2a  needs to occur.*
16124 2a 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69  * at the beginni
16125 6e 67 20 6f 66 20 61 20 70 61 67 65 2e 0a 2a 2a  ng of a page..**
16126 0a 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72  .** If parameter
16127 20 6e 52 65 73 65 72 76 65 20 69 73 20 6c 65 73   nReserve is les
16128 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65  s than zero, the
16129 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1612a 72 65 73 65 72 76 65 64 0a 2a 2a 20 62 79 74 65  reserved.** byte
1612b 73 20 70 65 72 20 70 61 67 65 20 69 73 20 6c 65  s per page is le
1612c 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a  ft unchanged..**
1612d 0a 2a 2a 20 49 66 20 74 68 65 20 69 46 69 78 21  .** If the iFix!
1612e 3d 30 20 74 68 65 6e 20 74 68 65 20 70 61 67 65  =0 then the page
1612f 53 69 7a 65 46 69 78 65 64 20 66 6c 61 67 20 69  SizeFixed flag i
16130 73 20 73 65 74 20 73 6f 20 74 68 61 74 20 74 68  s set so that th
16131 65 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 61  e page size.** a
16132 6e 64 20 61 75 74 6f 76 61 63 75 75 6d 20 6d 6f  nd autovacuum mo
16133 64 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72  de can no longer
16134 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a   be changed..*/.
16135 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
16136 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
16137 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65  etPageSize(Btree
16138 20 2a 70 2c 20 69 6e 74 20 70 61 67 65 53 69 7a   *p, int pageSiz
16139 65 2c 20 69 6e 74 20 6e 52 65 73 65 72 76 65 2c  e, int nReserve,
1613a 20 69 6e 74 20 69 46 69 78 29 7b 0a 20 20 69 6e   int iFix){.  in
1613b 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
1613c 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
1613d 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61 73  t = p->pBt;.  as
1613e 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e 3d  sert( nReserve>=
1613f 2d 31 20 26 26 20 6e 52 65 73 65 72 76 65 3c 3d  -1 && nReserve<=
16140 32 35 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  255 );.  sqlite3
16141 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
16142 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53 69   if( pBt->pageSi
16143 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20 20 73  zeFixed ){.    s
16144 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
16145 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  (p);.    return 
16146 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
16147 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73 65  .  }.  if( nRese
16148 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52 65  rve<0 ){.    nRe
16149 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61 67  serve = pBt->pag
1614a 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61  eSize - pBt->usa
1614b 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 61  bleSize;.  }.  a
1614c 73 73 65 72 74 28 20 6e 52 65 73 65 72 76 65 3e  ssert( nReserve>
1614d 3d 30 20 26 26 20 6e 52 65 73 65 72 76 65 3c 3d  =0 && nReserve<=
1614e 32 35 35 20 29 3b 0a 20 20 69 66 28 20 70 61 67  255 );.  if( pag
1614f 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 61  eSize>=512 && pa
16150 67 65 53 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d  geSize<=SQLITE_M
16151 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 26 26 0a  AX_PAGE_SIZE &&.
16152 20 20 20 20 20 20 20 20 28 28 70 61 67 65 53 69          ((pageSi
16153 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29 3d  ze-1)&pageSize)=
16154 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
16155 28 20 28 70 61 67 65 53 69 7a 65 20 26 20 37 29  ( (pageSize & 7)
16156 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
16157 74 28 20 21 70 42 74 2d 3e 70 50 61 67 65 31 20  t( !pBt->pPage1 
16158 26 26 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72  && !pBt->pCursor
16159 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 67   );.    pBt->pag
1615a 65 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 67  eSize = (u16)pag
1615b 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65 65 54  eSize;.    freeT
1615c 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20  empSpace(pBt);. 
1615d 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
1615e 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a  3PagerSetPagesiz
1615f 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26  e(pBt->pPager, &
16160 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6e  pBt->pageSize, n
16161 52 65 73 65 72 76 65 29 3b 0a 20 20 70 42 74 2d  Reserve);.  pBt-
16162 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 42  >usableSize = pB
16163 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 28 75  t->pageSize - (u
16164 31 36 29 6e 52 65 73 65 72 76 65 3b 0a 20 20 69  16)nReserve;.  i
16165 66 28 20 69 46 69 78 20 29 20 70 42 74 2d 3e 70  f( iFix ) pBt->p
16166 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31  ageSizeFixed = 1
16167 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
16168 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
16169 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1616a 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65  Return the curre
1616b 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 67  ntly defined pag
1616c 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45  e size.*/.SQLITE
1616d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1616e 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
1616f 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0a  Size(Btree *p){.
16170 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d    return p->pBt-
16171 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a  >pageSize;.}../*
16172 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
16173 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
16174 66 20 73 70 61 63 65 20 61 74 20 74 68 65 20 65  f space at the e
16175 6e 64 20 6f 66 20 65 76 65 72 79 20 70 61 67 65  nd of every page
16176 20 74 68 61 74 0a 2a 2a 20 61 72 65 20 69 6e 74   that.** are int
16177 65 6e 74 75 61 6c 6c 79 20 6c 65 66 74 20 75 6e  entually left un
16178 75 73 65 64 2e 20 20 54 68 69 73 20 69 73 20 74  used.  This is t
16179 68 65 20 22 72 65 73 65 72 76 65 64 22 20 73 70  he "reserved" sp
1617a 61 63 65 20 74 68 61 74 20 69 73 0a 2a 2a 20 73  ace that is.** s
1617b 6f 6d 65 74 69 6d 65 73 20 75 73 65 64 20 62 79  ometimes used by
1617c 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a   extensions..*/.
1617d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1617e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  nt sqlite3BtreeG
1617f 65 74 52 65 73 65 72 76 65 28 42 74 72 65 65 20  etReserve(Btree 
16180 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  *p){.  int n;.  
16181 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
16182 72 28 70 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70  r(p);.  n = p->p
16183 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70  Bt->pageSize - p
16184 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
16185 65 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  e;.  sqlite3Btre
16186 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
16187 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn n;.}../*.** 
16188 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  Set the maximum 
16189 70 61 67 65 20 63 6f 75 6e 74 20 66 6f 72 20 61  page count for a
1618a 20 64 61 74 61 62 61 73 65 20 69 66 20 6d 78 50   database if mxP
1618b 61 67 65 20 69 73 20 70 6f 73 69 74 69 76 65 2e  age is positive.
1618c 0a 2a 2a 20 4e 6f 20 63 68 61 6e 67 65 73 20 61  .** No changes a
1618d 72 65 20 6d 61 64 65 20 69 66 20 6d 78 50 61 67  re made if mxPag
1618e 65 20 69 73 20 30 20 6f 72 20 6e 65 67 61 74 69  e is 0 or negati
1618f 76 65 2e 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73  ve..** Regardles
16190 73 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f  s of the value o
16191 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e  f mxPage, return
16192 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67   the maximum pag
16193 65 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  e count..*/.SQLI
16194 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16195 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61  qlite3BtreeMaxPa
16196 67 65 43 6f 75 6e 74 28 42 74 72 65 65 20 2a 70  geCount(Btree *p
16197 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20  , int mxPage){. 
16198 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65   int n;.  sqlite
16199 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
1619a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 50 61 67    n = sqlite3Pag
1619b 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 70  erMaxPageCount(p
1619c 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d  ->pBt->pPager, m
1619d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69 74 65  xPage);.  sqlite
1619e 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1619f 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65    return n;.}.#e
161a0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
161a1 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47  (SQLITE_OMIT_PAG
161a2 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21  ER_PRAGMAS) || !
161a3 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
161a4 4d 49 54 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a  MIT_VACUUM) */..
161a5 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
161a6 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70   'auto-vacuum' p
161a7 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20 64  roperty of the d
161a8 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20  atabase. If the 
161a9 27 61 75 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20  'autoVacuum'.** 
161aa 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e  parameter is non
161ab 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f  -zero, then auto
161ac 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 69 73 20  -vacuum mode is 
161ad 65 6e 61 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f  enabled. If zero
161ae 2c 20 69 74 0a 2a 2a 20 69 73 20 64 69 73 61 62  , it.** is disab
161af 6c 65 64 2e 20 54 68 65 20 64 65 66 61 75 6c 74  led. The default
161b0 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 61   value for the a
161b1 75 74 6f 2d 76 61 63 75 75 6d 20 70 72 6f 70 65  uto-vacuum prope
161b2 72 74 79 20 69 73 20 0a 2a 2a 20 64 65 74 65 72  rty is .** deter
161b3 6d 69 6e 65 64 20 62 79 20 74 68 65 20 53 51 4c  mined by the SQL
161b4 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f  ITE_DEFAULT_AUTO
161b5 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f  VACUUM macro..*/
161b6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
161b7 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
161b8 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74  SetAutoVacuum(Bt
161b9 72 65 65 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f  ree *p, int auto
161ba 56 61 63 75 75 6d 29 7b 0a 23 69 66 64 65 66 20  Vacuum){.#ifdef 
161bb 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
161bc 56 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20  VACUUM.  return 
161bd 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
161be 0a 23 65 6c 73 65 0a 20 20 42 74 53 68 61 72 65  .#else.  BtShare
161bf 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
161c0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
161c1 54 45 5f 4f 4b 3b 0a 20 20 75 38 20 61 76 20 3d  TE_OK;.  u8 av =
161c2 20 28 75 38 29 61 75 74 6f 56 61 63 75 75 6d 3b   (u8)autoVacuum;
161c3 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
161c4 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20  Enter(p);.  if( 
161c5 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78  pBt->pageSizeFix
161c6 65 64 20 26 26 20 28 61 76 20 3f 31 3a 30 29 21  ed && (av ?1:0)!
161c7 3d 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d  =pBt->autoVacuum
161c8 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
161c9 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ITE_READONLY;.  
161ca 7d 65 6c 73 65 7b 0a 20 20 20 20 70 42 74 2d 3e  }else{.    pBt->
161cb 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 61 76 20  autoVacuum = av 
161cc 3f 31 3a 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69  ?1:0;.    pBt->i
161cd 6e 63 72 56 61 63 75 75 6d 20 3d 20 61 76 3d 3d  ncrVacuum = av==
161ce 32 20 3f 31 3a 30 3b 0a 20 20 7d 0a 20 20 73 71  2 ?1:0;.  }.  sq
161cf 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
161d0 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
161d1 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
161d2 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75   Return the valu
161d3 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f 2d 76  e of the 'auto-v
161d4 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74 79 2e  acuum' property.
161d5 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   If auto-vacuum 
161d6 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64 20 31  is .** enabled 1
161d7 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
161d8 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a 53 51  herwise 0..*/.SQ
161d9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
161da 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
161db 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65  AutoVacuum(Btree
161dc 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53 51 4c   *p){.#ifdef SQL
161dd 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
161de 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42 54 52  UUM.  return BTR
161df 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f  EE_AUTOVACUUM_NO
161e0 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20  NE;.#else.  int 
161e1 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  rc;.  sqlite3Btr
161e2 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63  eeEnter(p);.  rc
161e3 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e 70 42   = (.    (!p->pB
161e4 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3f 42  t->autoVacuum)?B
161e5 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
161e6 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d 3e 70  NONE:.    (!p->p
161e7 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3f  Bt->incrVacuum)?
161e8 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
161e9 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52 45 45  _FULL:.    BTREE
161ea 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e 43 52  _AUTOVACUUM_INCR
161eb 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  .  );.  sqlite3B
161ec 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
161ed 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69  return rc;.#endi
161ee 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20  f.}.../*.** Get 
161ef 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70  a reference to p
161f0 50 61 67 65 31 20 6f 66 20 74 68 65 20 64 61 74  Page1 of the dat
161f1 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 69  abase file.  Thi
161f2 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20 61  s will.** also a
161f3 63 71 75 69 72 65 20 61 20 72 65 61 64 6c 6f 63  cquire a readloc
161f4 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65 2e 0a  k on that file..
161f5 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  **.** SQLITE_OK 
161f6 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 73  is returned on s
161f7 75 63 63 65 73 73 2e 20 20 49 66 20 74 68 65 20  uccess.  If the 
161f8 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a 2a 2a  file is not a.**
161f9 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74   well-formed dat
161fa 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e  abase file, then
161fb 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
161fc 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
161fd 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
161fe 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 64  eturned if the d
161ff 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63 6b 65  atabase is locke
16200 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  d.  SQLITE_NOMEM
16201 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20  .** is returned 
16202 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66  if we run out of
16203 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73 74 61   memory. .*/.sta
16204 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74 72 65  tic int lockBtre
16205 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  e(BtShared *pBt)
16206 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65  {.  int rc;.  Me
16207 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 20  mPage *pPage1;. 
16208 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 61   int nPage;..  a
16209 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1620a 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
1620b 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
1620c 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 3d 3d  t( pBt->pPage1==
1620d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  0 );.  rc = sqli
1620e 74 65 33 50 61 67 65 72 53 68 61 72 65 64 4c 6f  te3PagerSharedLo
1620f 63 6b 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  ck(pBt->pPager);
16210 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
16211 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
16212 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65  ;.  rc = btreeGe
16213 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70  tPage(pBt, 1, &p
16214 50 61 67 65 31 2c 20 30 29 3b 0a 20 20 69 66 28  Page1, 0);.  if(
16215 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
16216 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
16217 2a 20 44 6f 20 73 6f 6d 65 20 63 68 65 63 6b 69  * Do some checki
16218 6e 67 20 74 6f 20 68 65 6c 70 20 69 6e 73 75 72  ng to help insur
16219 65 20 74 68 65 20 66 69 6c 65 20 77 65 20 6f 70  e the file we op
1621a 65 6e 65 64 20 72 65 61 6c 6c 79 20 69 73 0a 20  ened really is. 
1621b 20 2a 2a 20 61 20 76 61 6c 69 64 20 64 61 74 61   ** a valid data
1621c 62 61 73 65 20 66 69 6c 65 2e 20 0a 20 20 2a 2f  base file. .  */
1621d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50  .  rc = sqlite3P
1621e 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
1621f 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50 61 67  t->pPager, &nPag
16220 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  e);.  if( rc!=SQ
16221 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67  LITE_OK ){.    g
16222 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66  oto page1_init_f
16223 61 69 6c 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69  ailed;.  }else i
16224 66 28 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20 20  f( nPage>0 ){.  
16225 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65 3b 0a    int pageSize;.
16226 20 20 20 20 69 6e 74 20 75 73 61 62 6c 65 53 69      int usableSi
16227 7a 65 3b 0a 20 20 20 20 75 38 20 2a 70 61 67 65  ze;.    u8 *page
16228 31 20 3d 20 70 50 61 67 65 31 2d 3e 61 44 61 74  1 = pPage1->aDat
16229 61 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  a;.    rc = SQLI
1622a 54 45 5f 4e 4f 54 41 44 42 3b 0a 20 20 20 20 69  TE_NOTADB;.    i
1622b 66 28 20 6d 65 6d 63 6d 70 28 70 61 67 65 31 2c  f( memcmp(page1,
1622c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20 31   zMagicHeader, 1
1622d 36 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67  6)!=0 ){.      g
1622e 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66  oto page1_init_f
1622f 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20  ailed;.    }.   
16230 20 69 66 28 20 70 61 67 65 31 5b 31 38 5d 3e 31   if( page1[18]>1
16231 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 72   ){.      pBt->r
16232 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20 20  eadOnly = 1;.   
16233 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31   }.    if( page1
16234 5b 31 39 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20  [19]>1 ){.      
16235 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
16236 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 0a 20  failed;.    }.. 
16237 20 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75     /* The maximu
16238 6d 20 65 6d 62 65 64 64 65 64 20 66 72 61 63 74  m embedded fract
16239 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 78 61 63  ion must be exac
1623a 74 6c 79 20 32 35 25 2e 20 20 41 6e 64 20 74 68  tly 25%.  And th
1623b 65 20 6d 69 6e 69 6d 75 6d 0a 20 20 20 20 2a 2a  e minimum.    **
1623c 20 65 6d 62 65 64 64 65 64 20 66 72 61 63 74 69   embedded fracti
1623d 6f 6e 20 6d 75 73 74 20 62 65 20 31 32 2e 35 25  on must be 12.5%
1623e 20 66 6f 72 20 62 6f 74 68 20 6c 65 61 66 2d 64   for both leaf-d
1623f 61 74 61 20 61 6e 64 20 6e 6f 6e 2d 6c 65 61 66  ata and non-leaf
16240 2d 64 61 74 61 2e 0a 20 20 20 20 2a 2a 20 54 68  -data..    ** Th
16241 65 20 6f 72 69 67 69 6e 61 6c 20 64 65 73 69 67  e original desig
16242 6e 20 61 6c 6c 6f 77 65 64 20 74 68 65 73 65 20  n allowed these 
16243 61 6d 6f 75 6e 74 73 20 74 6f 20 76 61 72 79 2c  amounts to vary,
16244 20 62 75 74 20 61 73 20 6f 66 0a 20 20 20 20 2a   but as of.    *
16245 2a 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e 30 2c  * version 3.6.0,
16246 20 77 65 20 72 65 71 75 69 72 65 20 74 68 65 6d   we require them
16247 20 74 6f 20 62 65 20 66 69 78 65 64 2e 0a 20 20   to be fixed..  
16248 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d 65 6d    */.    if( mem
16249 63 6d 70 28 26 70 61 67 65 31 5b 32 31 5d 2c 20  cmp(&page1[21], 
1624a 22 5c 31 30 30 5c 30 34 30 5c 30 34 30 22 2c 33  "\100\040\040",3
1624b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  )!=0 ){.      go
1624c 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f 66 61  to page1_init_fa
1624d 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  iled;.    }.    
1624e 70 61 67 65 53 69 7a 65 20 3d 20 67 65 74 32 62  pageSize = get2b
1624f 79 74 65 28 26 70 61 67 65 31 5b 31 36 5d 29 3b  yte(&page1[16]);
16250 0a 20 20 20 20 69 66 28 20 28 28 70 61 67 65 53  .    if( ((pageS
16251 69 7a 65 2d 31 29 26 70 61 67 65 53 69 7a 65 29  ize-1)&pageSize)
16252 21 3d 30 20 7c 7c 20 70 61 67 65 53 69 7a 65 3c  !=0 || pageSize<
16253 35 31 32 20 7c 7c 0a 20 20 20 20 20 20 20 20 28  512 ||.        (
16254 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
16255 53 49 5a 45 3c 33 32 37 36 38 20 26 26 20 70 61  SIZE<32768 && pa
16256 67 65 53 69 7a 65 3e 53 51 4c 49 54 45 5f 4d 41  geSize>SQLITE_MA
16257 58 5f 50 41 47 45 5f 53 49 5a 45 29 0a 20 20 20  X_PAGE_SIZE).   
16258 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70   ){.      goto p
16259 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
1625a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
1625b 72 74 28 20 28 70 61 67 65 53 69 7a 65 20 26 20  rt( (pageSize & 
1625c 37 29 3d 3d 30 20 29 3b 0a 20 20 20 20 75 73 61  7)==0 );.    usa
1625d 62 6c 65 53 69 7a 65 20 3d 20 70 61 67 65 53 69  bleSize = pageSi
1625e 7a 65 20 2d 20 70 61 67 65 31 5b 32 30 5d 3b 0a  ze - page1[20];.
1625f 20 20 20 20 69 66 28 20 70 61 67 65 53 69 7a 65      if( pageSize
16260 21 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  !=pBt->pageSize 
16261 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 66 74 65  ){.      /* Afte
16262 72 20 72 65 61 64 69 6e 67 20 74 68 65 20 66 69  r reading the fi
16263 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  rst page of the 
16264 64 61 74 61 62 61 73 65 20 61 73 73 75 6d 69 6e  database assumin
16265 67 20 61 20 70 61 67 65 20 73 69 7a 65 0a 20 20  g a page size.  
16266 20 20 20 20 2a 2a 20 6f 66 20 42 74 53 68 61 72      ** of BtShar
16267 65 64 2e 70 61 67 65 53 69 7a 65 2c 20 77 65 20  ed.pageSize, we 
16268 68 61 76 65 20 64 69 73 63 6f 76 65 72 65 64 20  have discovered 
16269 74 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69  that the page-si
1626a 7a 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61  ze is.      ** a
1626b 63 74 75 61 6c 6c 79 20 70 61 67 65 53 69 7a 65  ctually pageSize
1626c 2e 20 55 6e 6c 6f 63 6b 20 74 68 65 20 64 61 74  . Unlock the dat
1626d 61 62 61 73 65 2c 20 6c 65 61 76 65 20 70 42 74  abase, leave pBt
1626e 2d 3e 70 50 61 67 65 31 20 61 74 0a 20 20 20 20  ->pPage1 at.    
1626f 20 20 2a 2a 20 7a 65 72 6f 20 61 6e 64 20 72 65    ** zero and re
16270 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20  turn SQLITE_OK. 
16271 54 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20  The caller will 
16272 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69  call this functi
16273 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 61 67 61 69  on.      ** agai
16274 6e 20 77 69 74 68 20 74 68 65 20 63 6f 72 72 65  n with the corre
16275 63 74 20 70 61 67 65 2d 73 69 7a 65 2e 0a 20 20  ct page-size..  
16276 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 6c      */.      rel
16277 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31 29  easePage(pPage1)
16278 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 75 73 61  ;.      pBt->usa
16279 62 6c 65 53 69 7a 65 20 3d 20 28 75 31 36 29 75  bleSize = (u16)u
1627a 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 20  sableSize;.     
1627b 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d   pBt->pageSize =
1627c 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b 0a   (u16)pageSize;.
1627d 20 20 20 20 20 20 66 72 65 65 54 65 6d 70 53 70        freeTempSp
1627e 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20  ace(pBt);.      
1627f 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
16280 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 42 74  rSetPagesize(pBt
16281 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e  ->pPager, &pBt->
16282 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20  pageSize,.      
16283 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16284 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
16285 65 53 69 7a 65 2d 75 73 61 62 6c 65 53 69 7a 65  eSize-usableSize
16286 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
16287 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rc;.    }.    if
16288 28 20 75 73 61 62 6c 65 53 69 7a 65 3c 34 38 30  ( usableSize<480
16289 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 70   ){.      goto p
1628a 61 67 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64  age1_init_failed
1628b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74 2d  ;.    }.    pBt-
1628c 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75 31 36  >pageSize = (u16
1628d 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 70  )pageSize;.    p
1628e 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d  Bt->usableSize =
1628f 20 28 75 31 36 29 75 73 61 62 6c 65 53 69 7a 65   (u16)usableSize
16290 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
16291 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
16292 0a 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61  .    pBt->autoVa
16293 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65  cuum = (get4byte
16294 28 26 70 61 67 65 31 5b 33 36 20 2b 20 34 2a 34  (&page1[36 + 4*4
16295 5d 29 3f 31 3a 30 29 3b 0a 20 20 20 20 70 42 74  ])?1:0);.    pBt
16296 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28  ->incrVacuum = (
16297 67 65 74 34 62 79 74 65 28 26 70 61 67 65 31 5b  get4byte(&page1[
16298 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b  36 + 7*4])?1:0);
16299 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f  .#endif.  }..  /
1629a 2a 20 6d 61 78 4c 6f 63 61 6c 20 69 73 20 74 68  * maxLocal is th
1629b 65 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74  e maximum amount
1629c 20 6f 66 20 70 61 79 6c 6f 61 64 20 74 6f 20 73   of payload to s
1629d 74 6f 72 65 20 6c 6f 63 61 6c 6c 79 20 66 6f 72  tore locally for
1629e 0a 20 20 2a 2a 20 61 20 63 65 6c 6c 2e 20 20 4d  .  ** a cell.  M
1629f 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 73  ake sure it is s
162a0 6d 61 6c 6c 20 65 6e 6f 75 67 68 20 73 6f 20 74  mall enough so t
162a1 68 61 74 20 61 74 20 6c 65 61 73 74 20 6d 69 6e  hat at least min
162a2 46 61 6e 6f 75 74 0a 20 20 2a 2a 20 63 65 6c 6c  Fanout.  ** cell
162a3 73 20 63 61 6e 20 77 69 6c 6c 20 66 69 74 20 6f  s can will fit o
162a4 6e 20 6f 6e 65 20 70 61 67 65 2e 20 20 57 65 20  n one page.  We 
162a5 61 73 73 75 6d 65 20 61 20 31 30 2d 62 79 74 65  assume a 10-byte
162a6 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 20 20   page header..  
162a7 2a 2a 20 42 65 73 69 64 65 73 20 74 68 65 20 70  ** Besides the p
162a8 61 79 6c 6f 61 64 2c 20 74 68 65 20 63 65 6c 6c  ayload, the cell
162a9 20 6d 75 73 74 20 73 74 6f 72 65 3a 0a 20 20 2a   must store:.  *
162aa 2a 20 20 20 20 20 32 2d 62 79 74 65 20 70 6f 69  *     2-byte poi
162ab 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c  nter to the cell
162ac 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65  .  **     4-byte
162ad 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 0a 20   child pointer. 
162ae 20 2a 2a 20 20 20 20 20 39 2d 62 79 74 65 20 6e   **     9-byte n
162af 4b 65 79 20 76 61 6c 75 65 0a 20 20 2a 2a 20 20  Key value.  **  
162b0 20 20 20 34 2d 62 79 74 65 20 6e 44 61 74 61 20     4-byte nData 
162b1 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34  value.  **     4
162b2 2d 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 20 70  -byte overflow p
162b3 61 67 65 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a  age pointer.  **
162b4 20 53 6f 20 61 20 63 65 6c 6c 20 63 6f 6e 73 69   So a cell consi
162b5 73 74 73 20 6f 66 20 61 20 32 2d 62 79 74 65 20  sts of a 2-byte 
162b6 70 6f 69 6e 65 72 2c 20 61 20 68 65 61 64 65 72  poiner, a header
162b7 20 77 68 69 63 68 20 69 73 20 61 73 20 6d 75 63   which is as muc
162b8 68 20 61 73 0a 20 20 2a 2a 20 31 37 20 62 79 74  h as.  ** 17 byt
162b9 65 73 20 6c 6f 6e 67 2c 20 30 20 74 6f 20 4e 20  es long, 0 to N 
162ba 62 79 74 65 73 20 6f 66 20 70 61 79 6c 6f 61 64  bytes of payload
162bb 2c 20 61 6e 64 20 61 6e 20 6f 70 74 69 6f 6e 61  , and an optiona
162bc 6c 20 34 20 62 79 74 65 20 6f 76 65 72 66 6c 6f  l 4 byte overflo
162bd 77 0a 20 20 2a 2a 20 70 61 67 65 20 70 6f 69 6e  w.  ** page poin
162be 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 42 74 2d  ter..  */.  pBt-
162bf 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 28 70 42 74  >maxLocal = (pBt
162c0 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29  ->usableSize-12)
162c1 2a 36 34 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20  *64/255 - 23;.  
162c2 70 42 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20  pBt->minLocal = 
162c3 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
162c4 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33  -12)*32/255 - 23
162c5 3b 0a 20 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66  ;.  pBt->maxLeaf
162c6 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   = pBt->usableSi
162c7 7a 65 20 2d 20 33 35 3b 0a 20 20 70 42 74 2d 3e  ze - 35;.  pBt->
162c8 6d 69 6e 4c 65 61 66 20 3d 20 28 70 42 74 2d 3e  minLeaf = (pBt->
162c9 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 33  usableSize-12)*3
162ca 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20 61 73  2/255 - 23;.  as
162cb 73 65 72 74 28 20 70 42 74 2d 3e 6d 61 78 4c 65  sert( pBt->maxLe
162cc 61 66 20 2b 20 32 33 20 3c 3d 20 4d 58 5f 43 45  af + 23 <= MX_CE
162cd 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20 29 3b 0a  LL_SIZE(pBt) );.
162ce 20 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20    pBt->pPage1 = 
162cf 70 50 61 67 65 31 3b 0a 20 20 72 65 74 75 72 6e  pPage1;.  return
162d0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67   SQLITE_OK;..pag
162d1 65 31 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3a 0a  e1_init_failed:.
162d2 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
162d3 61 67 65 31 29 3b 0a 20 20 70 42 74 2d 3e 70 50  age1);.  pBt->pP
162d4 61 67 65 31 20 3d 20 30 3b 0a 20 20 72 65 74 75  age1 = 0;.  retu
162d5 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
162d6 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
162d7 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 73  outstanding curs
162d8 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20 6e  ors and we are n
162d9 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  ot in the middle
162da 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61 63  .** of a transac
162db 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20 69  tion but there i
162dc 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e  s a read lock on
162dd 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
162de 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  hen.** this rout
162df 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20 66  ine unrefs the f
162e0 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  irst page of the
162e1 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
162e2 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68 65  hich .** has the
162e3 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65 61   effect of relea
162e4 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c 6f  sing the read lo
162e5 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ck..**.** If the
162e6 72 65 20 69 73 20 61 20 74 72 61 6e 73 61 63 74  re is a transact
162e7 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2c  ion in progress,
162e8 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
162e9 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61   a no-op..*/.sta
162ea 74 69 63 20 76 6f 69 64 20 75 6e 6c 6f 63 6b 42  tic void unlockB
162eb 74 72 65 65 49 66 55 6e 75 73 65 64 28 42 74 53  treeIfUnused(BtS
162ec 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 61  hared *pBt){.  a
162ed 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
162ee 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
162ef 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
162f0 74 28 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3d  t( pBt->pCursor=
162f1 3d 30 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61  =0 || pBt->inTra
162f2 6e 73 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e  nsaction>TRANS_N
162f3 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20 70 42 74  ONE );.  if( pBt
162f4 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
162f5 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70  =TRANS_NONE && p
162f6 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29 7b  Bt->pPage1!=0 ){
162f7 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74  .    assert( pBt
162f8 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20  ->pPage1->aData 
162f9 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
162fa 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
162fb 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29  unt(pBt->pPager)
162fc 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==1 );.    asser
162fd 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  t( pBt->pPage1->
162fe 61 44 61 74 61 20 29 3b 0a 20 20 20 20 72 65 6c  aData );.    rel
162ff 65 61 73 65 50 61 67 65 28 70 42 74 2d 3e 70 50  easePage(pBt->pP
16300 61 67 65 31 29 3b 0a 20 20 20 20 70 42 74 2d 3e  age1);.    pBt->
16301 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 7d 0a  pPage1 = 0;.  }.
16302 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 42 74 20  }../*.** If pBt 
16303 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65 6d 70  points to an emp
16304 74 79 20 66 69 6c 65 20 74 68 65 6e 20 63 6f 6e  ty file then con
16305 76 65 72 74 20 74 68 61 74 20 65 6d 70 74 79 20  vert that empty 
16306 66 69 6c 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e  file.** into a n
16307 65 77 20 65 6d 70 74 79 20 64 61 74 61 62 61 73  ew empty databas
16308 65 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e  e by initializin
16309 67 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65  g the first page
1630a 20 6f 66 0a 2a 2a 20 74 68 65 20 64 61 74 61 62   of.** the datab
1630b 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ase..*/.static i
1630c 6e 74 20 6e 65 77 44 61 74 61 62 61 73 65 28 42  nt newDatabase(B
1630d 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20  tShared *pBt){. 
1630e 20 4d 65 6d 50 61 67 65 20 2a 70 50 31 3b 0a 20   MemPage *pP1;. 
1630f 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
16310 64 61 74 61 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  data;.  int rc;.
16311 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20    int nPage;..  
16312 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
16313 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
16314 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
16315 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
16316 65 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67  ecount(pBt->pPag
16317 65 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 69  er, &nPage);.  i
16318 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
16319 20 7c 7c 20 6e 50 61 67 65 3e 30 20 29 7b 0a 20   || nPage>0 ){. 
1631a 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1631b 7d 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70  }.  pP1 = pBt->p
1631c 50 61 67 65 31 3b 0a 20 20 61 73 73 65 72 74 28  Page1;.  assert(
1631d 20 70 50 31 21 3d 30 20 29 3b 0a 20 20 64 61 74   pP1!=0 );.  dat
1631e 61 20 3d 20 70 50 31 2d 3e 61 44 61 74 61 3b 0a  a = pP1->aData;.
1631f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
16320 67 65 72 57 72 69 74 65 28 70 50 31 2d 3e 70 44  gerWrite(pP1->pD
16321 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  bPage);.  if( rc
16322 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
16323 6d 65 6d 63 70 79 28 64 61 74 61 2c 20 7a 4d 61  memcpy(data, zMa
16324 67 69 63 48 65 61 64 65 72 2c 20 73 69 7a 65 6f  gicHeader, sizeo
16325 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29 29  f(zMagicHeader))
16326 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
16327 6f 66 28 7a 4d 61 67 69 63 48 65 61 64 65 72 29  of(zMagicHeader)
16328 3d 3d 31 36 20 29 3b 0a 20 20 70 75 74 32 62 79  ==16 );.  put2by
16329 74 65 28 26 64 61 74 61 5b 31 36 5d 2c 20 70 42  te(&data[16], pB
1632a 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  t->pageSize);.  
1632b 64 61 74 61 5b 31 38 5d 20 3d 20 31 3b 0a 20 20  data[18] = 1;.  
1632c 64 61 74 61 5b 31 39 5d 20 3d 20 31 3b 0a 20 20  data[19] = 1;.  
1632d 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61  assert( pBt->usa
1632e 62 6c 65 53 69 7a 65 3c 3d 70 42 74 2d 3e 70 61  bleSize<=pBt->pa
1632f 67 65 53 69 7a 65 20 26 26 20 70 42 74 2d 3e 75  geSize && pBt->u
16330 73 61 62 6c 65 53 69 7a 65 2b 32 35 35 3e 3d 70  sableSize+255>=p
16331 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  Bt->pageSize);. 
16332 20 64 61 74 61 5b 32 30 5d 20 3d 20 28 75 38 29   data[20] = (u8)
16333 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d  (pBt->pageSize -
16334 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
16335 29 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20 3d 20  );.  data[21] = 
16336 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d 20 3d  64;.  data[22] =
16337 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33 5d 20   32;.  data[23] 
16338 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74 28 26  = 32;.  memset(&
16339 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31 30 30  data[24], 0, 100
1633a 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65  -24);.  zeroPage
1633b 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b 45 59  (pP1, PTF_INTKEY
1633c 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f 4c 45  |PTF_LEAF|PTF_LE
1633d 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42 74 2d  AFDATA );.  pBt-
1633e 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d  >pageSizeFixed =
1633f 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   1;.#ifndef SQLI
16340 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
16341 55 4d 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  UM.  assert( pBt
16342 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d 31 20  ->autoVacuum==1 
16343 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  || pBt->autoVacu
16344 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  um==0 );.  asser
16345 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75  t( pBt->incrVacu
16346 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 69 6e  um==1 || pBt->in
16347 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20  crVacuum==0 );. 
16348 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b   put4byte(&data[
16349 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74 2d 3e  36 + 4*4], pBt->
1634a 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 70  autoVacuum);.  p
1634b 75 74 34 62 79 74 65 28 26 64 61 74 61 5b 33 36  ut4byte(&data[36
1634c 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e 69 6e   + 7*4], pBt->in
1634d 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 6e 64 69  crVacuum);.#endi
1634e 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  f.  return SQLIT
1634f 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
16350 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72 74 20  ttempt to start 
16351 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f  a new transactio
16352 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73  n. A write-trans
16353 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 74 61  action.** is sta
16354 72 74 65 64 20 69 66 20 74 68 65 20 73 65 63 6f  rted if the seco
16355 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  nd argument is n
16356 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73  onzero, otherwis
16357 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74 72 61  e a read-.** tra
16358 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 74 68  nsaction.  If th
16359 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
1635a 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65 20 61  t is 2 or more a
1635b 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20  nd exclusive.** 
1635c 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73  transaction is s
1635d 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e 67 20  tarted, meaning 
1635e 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 70 72  that no other pr
1635f 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65 64  ocess is allowed
16360 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20 74 68  .** to access th
16361 65 20 64 61 74 61 62 61 73 65 2e 20 20 41 20 70  e database.  A p
16362 72 65 65 78 69 73 74 69 6e 67 20 74 72 61 6e 73  reexisting trans
16363 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74 20 62  action may not b
16364 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20 74 6f  e.** upgraded to
16365 20 65 78 63 6c 75 73 69 76 65 20 62 79 20 63 61   exclusive by ca
16366 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  lling this routi
16367 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69 6d 65  ne a second time
16368 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c 75 73   - the.** exclus
16369 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c 79 20  ivity flag only 
1636a 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65 77 20  works for a new 
1636b 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a  transaction..**.
1636c 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73  ** A write-trans
1636d 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73  action must be s
1636e 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 74  tarted before at
1636f 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a 2a 2a  tempting any .**
16370 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20   changes to the 
16371 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20  database.  None 
16372 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
16373 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20 77 69   routines .** wi
16374 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73 20 61  ll work unless a
16375 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
16376 73 74 61 72 74 65 64 20 66 69 72 73 74 3a 0a 2a  started first:.*
16377 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  *.**      sqlite
16378 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c  3BtreeCreateTabl
16379 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  e().**      sqli
1637a 74 65 33 42 74 72 65 65 43 72 65 61 74 65 49 6e  te3BtreeCreateIn
1637b 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20 73 71  dex().**      sq
1637c 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54  lite3BtreeClearT
1637d 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73  able().**      s
1637e 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54  qlite3BtreeDropT
1637f 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73  able().**      s
16380 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72  qlite3BtreeInser
16381 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  t().**      sqli
16382 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 29  te3BtreeDelete()
16383 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65 33  .**      sqlite3
16384 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28  BtreeUpdateMeta(
16385 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69 6e  ).**.** If an in
16386 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20 74 6f  itial attempt to
16387 20 61 63 71 75 69 72 65 20 74 68 65 20 6c 6f 63   acquire the loc
16388 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73 65 20  k fails because 
16389 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69  of lock contenti
1638a 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 64 61  on.** and the da
1638b 74 61 62 61 73 65 20 77 61 73 20 70 72 65 76 69  tabase was previ
1638c 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64 2c 20  ously unlocked, 
1638d 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68 65 20  then invoke the 
1638e 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a 2a 20  busy handler.** 
1638f 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e  if there is one.
16390 20 20 42 75 74 20 69 66 20 74 68 65 72 65 20 77    But if there w
16391 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 61 20  as previously a 
16392 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20 6e 6f  read-lock, do no
16393 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20  t.** invoke the 
16394 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d 20 6a  busy handler - j
16395 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ust return SQLIT
16396 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54 45 5f  E_BUSY.  SQLITE_
16397 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65 74 75  BUSY is .** retu
16398 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72 65 20  rned when there 
16399 69 73 20 61 6c 72 65 61 64 79 20 61 20 72 65 61  is already a rea
1639a 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65 72 20  d-lock in order 
1639b 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61 64 6c  to avoid a deadl
1639c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f  ock..**.** Suppo
1639d 73 65 20 74 68 65 72 65 20 61 72 65 20 74 77 6f  se there are two
1639e 20 70 72 6f 63 65 73 73 65 73 20 41 20 61 6e 64   processes A and
1639f 20 42 2e 20 20 41 20 68 61 73 20 61 20 72 65 61   B.  A has a rea
163a0 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68 61 73  d lock and B has
163a1 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64 20 6c  .** a reserved l
163a2 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20 74 6f  ock.  B tries to
163a3 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78 63 6c   promote to excl
163a4 75 73 69 76 65 20 62 75 74 20 69 73 20 62 6c 6f  usive but is blo
163a5 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a 2a 20  cked because.** 
163a6 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f 63 6b  of A's read lock
163a7 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20 70 72  .  A tries to pr
163a8 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72 76 65  omote to reserve
163a9 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b 65 64  d but is blocked
163aa 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20 6f 72   by B..** One or
163ab 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68   the other of th
163ac 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20  e two processes 
163ad 6d 75 73 74 20 67 69 76 65 20 77 61 79 20 6f 72  must give way or
163ae 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a 2a   there can be.**
163af 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20 20 42   no progress.  B
163b0 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c 49  y returning SQLI
163b1 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f 74 20  TE_BUSY and not 
163b2 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73  invoking the bus
163b3 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 77 68  y callback.** wh
163b4 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68 61 73  en A already has
163b5 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20 77 65   a read lock, we
163b6 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74 6f 20   encourage A to 
163b7 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65 74 20  give up and let 
163b8 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a 2a 2f  B.** proceed..*/
163b9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
163ba 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
163bb 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72 65 65  BeginTrans(Btree
163bc 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61 67 29   *p, int wrflag)
163bd 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 42 6c  {.  sqlite3 *pBl
163be 6f 63 6b 20 3d 20 30 3b 0a 20 20 42 74 53 68 61  ock = 0;.  BtSha
163bf 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
163c0 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  t;.  int rc = SQ
163c1 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69  LITE_OK;..  sqli
163c2 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
163c3 3b 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69  ;.  btreeIntegri
163c4 74 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  ty(p);..  /* If 
163c5 74 68 65 20 62 74 72 65 65 20 69 73 20 61 6c 72  the btree is alr
163c6 65 61 64 79 20 69 6e 20 61 20 77 72 69 74 65 2d  eady in a write-
163c7 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20  transaction, or 
163c8 69 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61  it.  ** is alrea
163c9 64 79 20 69 6e 20 61 20 72 65 61 64 2d 74 72 61  dy in a read-tra
163ca 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 72  nsaction and a r
163cb 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a  ead-transaction.
163cc 20 20 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65    ** is requeste
163cd 64 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d  d, this is a no-
163ce 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  op..  */.  if( p
163cf 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
163d0 5f 57 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e  _WRITE || (p->in
163d1 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41  Trans==TRANS_REA
163d2 44 20 26 26 20 21 77 72 66 6c 61 67 29 20 29 7b  D && !wrflag) ){
163d3 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f  .    goto trans_
163d4 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  begun;.  }..  /*
163d5 20 57 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   Write transacti
163d6 6f 6e 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 73  ons are not poss
163d7 69 62 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f  ible on a read-o
163d8 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  nly database */.
163d9 20 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f    if( pBt->readO
163da 6e 6c 79 20 26 26 20 77 72 66 6c 61 67 20 29 7b  nly && wrflag ){
163db 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
163dc 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67  _READONLY;.    g
163dd 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b  oto trans_begun;
163de 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  .  }..#ifndef SQ
163df 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
163e0 5f 43 41 43 48 45 0a 20 20 2f 2a 20 49 66 20 61  _CACHE.  /* If a
163e1 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20  nother database 
163e2 68 61 6e 64 6c 65 20 68 61 73 20 61 6c 72 65 61  handle has alrea
163e3 64 79 20 6f 70 65 6e 65 64 20 61 20 77 72 69 74  dy opened a writ
163e4 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20  e transaction . 
163e5 20 2a 2a 20 6f 6e 20 74 68 69 73 20 73 68 61 72   ** on this shar
163e6 65 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 75  ed-btree structu
163e7 72 65 20 61 6e 64 20 61 20 73 65 63 6f 6e 64 20  re and a second 
163e8 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
163e9 6e 20 69 73 0a 20 20 2a 2a 20 72 65 71 75 65 73  n is.  ** reques
163ea 74 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  ted, return SQLI
163eb 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20 2a 2f 0a  TE_LOCKED..  */.
163ec 20 20 69 66 28 20 28 77 72 66 6c 61 67 20 26 26    if( (wrflag &&
163ed 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
163ee 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
163ef 29 20 7c 7c 20 70 42 74 2d 3e 69 73 50 65 6e 64  ) || pBt->isPend
163f0 69 6e 67 20 29 7b 0a 20 20 20 20 70 42 6c 6f 63  ing ){.    pBloc
163f1 6b 20 3d 20 70 42 74 2d 3e 70 57 72 69 74 65 72  k = pBt->pWriter
163f2 2d 3e 64 62 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ->db;.  }else if
163f3 28 20 77 72 66 6c 61 67 3e 31 20 29 7b 0a 20 20  ( wrflag>1 ){.  
163f4 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b    BtLock *pIter;
163f5 0a 20 20 20 20 66 6f 72 28 70 49 74 65 72 3d 70  .    for(pIter=p
163f6 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72  Bt->pLock; pIter
163f7 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70  ; pIter=pIter->p
163f8 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28  Next){.      if(
163f9 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 21 3d   pIter->pBtree!=
163fa 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 6c  p ){.        pBl
163fb 6f 63 6b 20 3d 20 70 49 74 65 72 2d 3e 70 42 74  ock = pIter->pBt
163fc 72 65 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20  ree->db;.       
163fd 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
163fe 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
163ff 70 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 73 71  pBlock ){.    sq
16400 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42  lite3ConnectionB
16401 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42  locked(p->db, pB
16402 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20 3d 20  lock);.    rc = 
16403 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48  SQLITE_LOCKED_SH
16404 41 52 45 44 43 41 43 48 45 3b 0a 20 20 20 20 67  AREDCACHE;.    g
16405 6f 74 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b  oto trans_begun;
16406 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
16407 2a 20 41 6e 79 20 72 65 61 64 2d 6f 6e 6c 79 20  * Any read-only 
16408 6f 72 20 72 65 61 64 2d 77 72 69 74 65 20 74 72  or read-write tr
16409 61 6e 73 61 63 74 69 6f 6e 20 69 6d 70 6c 69 65  ansaction implie
1640a 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e  s a read-lock on
1640b 20 0a 20 20 2a 2a 20 70 61 67 65 20 31 2e 20 53   .  ** page 1. S
1640c 6f 20 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  o if some other 
1640d 73 68 61 72 65 64 2d 63 61 63 68 65 20 63 6c 69  shared-cache cli
1640e 65 6e 74 20 61 6c 72 65 61 64 79 20 68 61 73 20  ent already has 
1640f 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 0a 20 20  a write-lock .  
16410 2a 2a 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68  ** on page 1, th
16411 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61  e transaction ca
16412 6e 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2e 20  nnot be opened. 
16413 2a 2f 0a 20 20 72 63 20 3d 20 71 75 65 72 79 53  */.  rc = queryS
16414 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
16415 6f 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f  ock(p, MASTER_RO
16416 4f 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a  OT, READ_LOCK);.
16417 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
16418 3d 72 63 20 29 20 67 6f 74 6f 20 74 72 61 6e 73  =rc ) goto trans
16419 5f 62 65 67 75 6e 3b 0a 0a 20 20 64 6f 20 7b 0a  _begun;..  do {.
1641a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 6c 6f 63 6b      /* Call lock
1641b 42 74 72 65 65 28 29 20 75 6e 74 69 6c 20 65 69  Btree() until ei
1641c 74 68 65 72 20 70 42 74 2d 3e 70 50 61 67 65 31  ther pBt->pPage1
1641d 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 6f 72   is populated or
1641e 0a 20 20 20 20 2a 2a 20 6c 6f 63 6b 42 74 72 65  .    ** lockBtre
1641f 65 28 29 20 72 65 74 75 72 6e 73 20 73 6f 6d 65  e() returns some
16420 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  thing other than
16421 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 6c 6f 63 6b   SQLITE_OK. lock
16422 42 74 72 65 65 28 29 0a 20 20 20 20 2a 2a 20 6d  Btree().    ** m
16423 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ay return SQLITE
16424 5f 4f 4b 20 62 75 74 20 6c 65 61 76 65 20 70 42  _OK but leave pB
16425 74 2d 3e 70 50 61 67 65 31 20 73 65 74 20 74 6f  t->pPage1 set to
16426 20 30 20 69 66 20 61 66 74 65 72 0a 20 20 20 20   0 if after.    
16427 2a 2a 20 72 65 61 64 69 6e 67 20 70 61 67 65 20  ** reading page 
16428 31 20 69 74 20 64 69 73 63 6f 76 65 72 73 20 74  1 it discovers t
16429 68 61 74 20 74 68 65 20 70 61 67 65 2d 73 69 7a  hat the page-siz
1642a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
1642b 65 20 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 69  e .    ** file i
1642c 73 20 6e 6f 74 20 70 42 74 2d 3e 70 61 67 65 53  s not pBt->pageS
1642d 69 7a 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ize. In this cas
1642e 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 77 69  e lockBtree() wi
1642f 6c 6c 20 75 70 64 61 74 65 0a 20 20 20 20 2a 2a  ll update.    **
16430 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 74   pBt->pageSize t
16431 6f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20  o the page-size 
16432 6f 66 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64  of the file on d
16433 69 73 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  isk..    */.    
16434 77 68 69 6c 65 28 20 70 42 74 2d 3e 70 50 61 67  while( pBt->pPag
16435 65 31 3d 3d 30 20 26 26 20 53 51 4c 49 54 45 5f  e1==0 && SQLITE_
16436 4f 4b 3d 3d 28 72 63 20 3d 20 6c 6f 63 6b 42 74  OK==(rc = lockBt
16437 72 65 65 28 70 42 74 29 29 20 29 3b 0a 0a 20 20  ree(pBt)) );..  
16438 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
16439 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b  _OK && wrflag ){
1643a 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e  .      if( pBt->
1643b 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20  readOnly ){.    
1643c 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1643d 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20  READONLY;.      
1643e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72  }else{.        r
1643f 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
16440 42 65 67 69 6e 28 70 42 74 2d 3e 70 50 61 67 65  Begin(pBt->pPage
16441 72 2c 77 72 66 6c 61 67 3e 31 2c 73 71 6c 69 74  r,wrflag>1,sqlit
16442 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 70  e3TempInMemory(p
16443 2d 3e 64 62 29 29 3b 0a 20 20 20 20 20 20 20 20  ->db));.        
16444 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16445 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
16446 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65 28  c = newDatabase(
16447 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  pBt);.        }.
16448 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
16449 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1644a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1644b 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75  unlockBtreeIfUnu
1644c 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a  sed(pBt);.    }.
1644d 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51    }while( rc==SQ
1644e 4c 49 54 45 5f 42 55 53 59 20 26 26 20 70 42 74  LITE_BUSY && pBt
1644f 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
16450 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 0a 20  =TRANS_NONE &&. 
16451 20 20 20 20 20 20 20 20 20 62 74 72 65 65 49 6e           btreeIn
16452 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c 65 72 28  vokeBusyHandler(
16453 70 42 74 29 20 29 3b 0a 0a 20 20 69 66 28 20 72  pBt) );..  if( r
16454 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
16455 20 20 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61      if( p->inTra
16456 6e 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29  ns==TRANS_NONE )
16457 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72  {.      pBt->nTr
16458 61 6e 73 61 63 74 69 6f 6e 2b 2b 3b 0a 23 69 66  ansaction++;.#if
16459 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1645a 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
1645b 20 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61      if( p->shara
1645c 62 6c 65 20 29 7b 0a 09 61 73 73 65 72 74 28 20  ble ){..assert( 
1645d 70 2d 3e 6c 6f 63 6b 2e 70 42 74 72 65 65 3d 3d  p->lock.pBtree==
1645e 70 20 26 26 20 70 2d 3e 6c 6f 63 6b 2e 69 54 61  p && p->lock.iTa
1645f 62 6c 65 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  ble==1 );.      
16460 20 20 70 2d 3e 6c 6f 63 6b 2e 65 4c 6f 63 6b 20    p->lock.eLock 
16461 3d 20 52 45 41 44 5f 4c 4f 43 4b 3b 0a 20 20 20  = READ_LOCK;.   
16462 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 70 4e 65       p->lock.pNe
16463 78 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b  xt = pBt->pLock;
16464 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 70 4c  .        pBt->pL
16465 6f 63 6b 20 3d 20 26 70 2d 3e 6c 6f 63 6b 3b 0a  ock = &p->lock;.
16466 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
16467 20 20 20 7d 0a 20 20 20 20 70 2d 3e 69 6e 54 72     }.    p->inTr
16468 61 6e 73 20 3d 20 28 77 72 66 6c 61 67 3f 54 52  ans = (wrflag?TR
16469 41 4e 53 5f 57 52 49 54 45 3a 54 52 41 4e 53 5f  ANS_WRITE:TRANS_
1646a 52 45 41 44 29 3b 0a 20 20 20 20 69 66 28 20 70  READ);.    if( p
1646b 2d 3e 69 6e 54 72 61 6e 73 3e 70 42 74 2d 3e 69  ->inTrans>pBt->i
1646c 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a  nTransaction ){.
1646d 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61        pBt->inTra
1646e 6e 73 61 63 74 69 6f 6e 20 3d 20 70 2d 3e 69 6e  nsaction = p->in
1646f 54 72 61 6e 73 3b 0a 20 20 20 20 7d 0a 23 69 66  Trans;.    }.#if
16470 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16471 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20  _SHARED_CACHE.  
16472 20 20 69 66 28 20 77 72 66 6c 61 67 20 29 7b 0a    if( wrflag ){.
16473 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
16474 42 74 2d 3e 70 57 72 69 74 65 72 20 29 3b 0a 20  Bt->pWriter );. 
16475 20 20 20 20 20 70 42 74 2d 3e 70 57 72 69 74 65       pBt->pWrite
16476 72 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 42 74  r = p;.      pBt
16477 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20  ->isExclusive = 
16478 28 75 38 29 28 77 72 66 6c 61 67 3e 31 29 3b 0a  (u8)(wrflag>1);.
16479 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
1647a 0a 0a 0a 74 72 61 6e 73 5f 62 65 67 75 6e 3a 0a  ...trans_begun:.
1647b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1647c 5f 4f 4b 20 26 26 20 77 72 66 6c 61 67 20 29 7b  _OK && wrflag ){
1647d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61 6c  .    /* This cal
1647e 6c 20 6d 61 6b 65 73 20 73 75 72 65 20 74 68 61  l makes sure tha
1647f 74 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20  t the pager has 
16480 74 68 65 20 63 6f 72 72 65 63 74 20 6e 75 6d 62  the correct numb
16481 65 72 20 6f 66 0a 20 20 20 20 2a 2a 20 6f 70 65  er of.    ** ope
16482 6e 20 73 61 76 65 70 6f 69 6e 74 73 2e 20 49 66  n savepoints. If
16483 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
16484 6d 65 74 65 72 20 69 73 20 67 72 65 61 74 65 72  meter is greater
16485 20 74 68 61 6e 20 30 20 61 6e 64 0a 20 20 20 20   than 0 and.    
16486 2a 2a 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e  ** the sub-journ
16487 61 6c 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  al is not alread
16488 79 20 6f 70 65 6e 2c 20 74 68 65 6e 20 69 74 20  y open, then it 
16489 77 69 6c 6c 20 62 65 20 6f 70 65 6e 65 64 20 68  will be opened h
1648a 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ere..    */.    
1648b 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1648c 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70  rOpenSavepoint(p
1648d 42 74 2d 3e 70 50 61 67 65 72 2c 20 70 2d 3e 64  Bt->pPager, p->d
1648e 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 3b 0a  b->nSavepoint);.
1648f 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74 65    }..  btreeInte
16490 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69  grity(p);.  sqli
16491 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
16492 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
16493 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
16494 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
16495 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
16496 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72  pointer-map entr
16497 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c  ies for all chil
16498 64 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61  dren of page pPa
16499 67 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20  ge. Also, if.** 
1649a 70 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63  pPage contains c
1649b 65 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 20  ells that point 
1649c 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  to overflow page
1649d 73 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e 74  s, set the point
1649e 65 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65  er.** map entrie
1649f 73 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c  s for the overfl
164a0 6f 77 20 70 61 67 65 73 20 61 73 20 77 65 6c 6c  ow pages as well
164a1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
164a2 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28  setChildPtrmaps(
164a3 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b  MemPage *pPage){
164a4 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
164a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
164a6 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72        /* Counter
164a7 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69   variable */.  i
164a8 6e 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20  nt nCell;       
164a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
164aa 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
164ab 65 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50 61  ells in page pPa
164ac 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  ge */.  int rc; 
164ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
164ae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
164af 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42  turn code */.  B
164b0 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
164b1 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 75 38 20  Page->pBt;.  u8 
164b2 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61  isInitOrig = pPa
164b3 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67  ge->isInit;.  Pg
164b4 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d  no pgno = pPage-
164b5 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74  >pgno;..  assert
164b6 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
164b7 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
164b8 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20  >mutex) );.  rc 
164b9 3d 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  = btreeInitPage(
164ba 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63  pPage);.  if( rc
164bb 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
164bc 20 20 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c     goto set_chil
164bd 64 5f 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20  d_ptrmaps_out;. 
164be 20 7d 0a 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61   }.  nCell = pPa
164bf 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 66 6f  ge->nCell;..  fo
164c0 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  r(i=0; i<nCell; 
164c1 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 43  i++){.    u8 *pC
164c2 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
164c3 50 61 67 65 2c 20 69 29 3b 0a 0a 20 20 20 20 70  Page, i);..    p
164c4 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74 72 28  trmapPutOvflPtr(
164c5 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 72  pPage, pCell, &r
164c6 63 29 3b 0a 0a 20 20 20 20 69 66 28 20 21 70 50  c);..    if( !pP
164c7 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
164c8 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e     Pgno childPgn
164c9 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70 43 65  o = get4byte(pCe
164ca 6c 6c 29 3b 0a 20 20 20 20 20 20 70 74 72 6d 61  ll);.      ptrma
164cb 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50  pPut(pBt, childP
164cc 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
164cd 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20  E, pgno, &rc);. 
164ce 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
164cf 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
164d0 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67      Pgno childPg
164d1 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
164d2 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
164d3 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
164d4 3b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28  ;.    ptrmapPut(
164d5 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f 2c 20  pBt, childPgno, 
164d6 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 67  PTRMAP_BTREE, pg
164d7 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 7d 0a 0a 73  no, &rc);.  }..s
164d8 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73  et_child_ptrmaps
164d9 5f 6f 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69  _out:.  pPage->i
164da 73 49 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72  sInit = isInitOr
164db 69 67 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ig;.  return rc;
164dc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68  .}../*.** Somewh
164dd 65 72 65 20 6f 6e 20 70 50 61 67 65 20 69 73 20  ere on pPage is 
164de 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 67  a pointer to pag
164df 65 20 69 46 72 6f 6d 2e 20 20 4d 6f 64 69 66 79  e iFrom.  Modify
164e0 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f   this pointer so
164e1 0a 2a 2a 20 74 68 61 74 20 69 74 20 70 6f 69 6e  .** that it poin
164e2 74 73 20 74 6f 20 69 54 6f 2e 20 50 61 72 61 6d  ts to iTo. Param
164e3 65 74 65 72 20 65 54 79 70 65 20 64 65 73 63 72  eter eType descr
164e4 69 62 65 73 20 74 68 65 20 74 79 70 65 20 6f 66  ibes the type of
164e5 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 62   pointer to.** b
164e6 65 20 6d 6f 64 69 66 69 65 64 2c 20 61 73 20 20  e modified, as  
164e7 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50  follows:.**.** P
164e8 54 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20  TRMAP_BTREE:    
164e9 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65   pPage is a btre
164ea 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e  e-page. The poin
164eb 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20  ter points at a 
164ec 63 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20  child .**       
164ed 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65              page
164ee 20 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a   of pPage..**.**
164ef 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
164f0 31 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 74  1: pPage is a bt
164f1 72 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f  ree-page. The po
164f2 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20  inter points at 
164f3 61 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20  an overflow.**  
164f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
164f5 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f   page pointed to
164f6 20 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63   by one of the c
164f7 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a  ells on pPage..*
164f8 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52  *.** PTRMAP_OVER
164f9 46 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20  FLOW2: pPage is 
164fa 61 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65  an overflow-page
164fb 2e 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f  . The pointer po
164fc 69 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78 74  ints at the next
164fd 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
164fe 20 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70        overflow p
164ff 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e  age in the list.
16500 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
16501 6f 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72  odifyPagePointer
16502 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c  (MemPage *pPage,
16503 20 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e   Pgno iFrom, Pgn
16504 6f 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29  o iTo, u8 eType)
16505 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
16506 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
16507 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  Page->pBt->mutex
16508 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
16509 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1650a 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44  teable(pPage->pD
1650b 62 50 61 67 65 29 20 29 3b 0a 20 20 69 66 28 20  bPage) );.  if( 
1650c 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56  eType==PTRMAP_OV
1650d 45 52 46 4c 4f 57 32 20 29 7b 0a 20 20 20 20 2f  ERFLOW2 ){.    /
1650e 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73  * The pointer is
1650f 20 61 6c 77 61 79 73 20 74 68 65 20 66 69 72 73   always the firs
16510 74 20 34 20 62 79 74 65 73 20 6f 66 20 74 68 65  t 4 bytes of the
16511 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 61   page in this ca
16512 73 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  se.  */.    if( 
16513 67 65 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e  get4byte(pPage->
16514 61 44 61 74 61 29 21 3d 69 46 72 6f 6d 20 29 7b  aData)!=iFrom ){
16515 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
16516 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
16517 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75 74  T;.    }.    put
16518 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61  4byte(pPage->aDa
16519 74 61 2c 20 69 54 6f 29 3b 0a 20 20 7d 65 6c 73  ta, iTo);.  }els
1651a 65 7b 0a 20 20 20 20 75 38 20 69 73 49 6e 69 74  e{.    u8 isInit
1651b 4f 72 69 67 20 3d 20 70 50 61 67 65 2d 3e 69 73  Orig = pPage->is
1651c 49 6e 69 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b  Init;.    int i;
1651d 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a  .    int nCell;.
1651e 0a 20 20 20 20 62 74 72 65 65 49 6e 69 74 50 61  .    btreeInitPa
1651f 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 6e  ge(pPage);.    n
16520 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43  Cell = pPage->nC
16521 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d  ell;..    for(i=
16522 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
16523 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c  {.      u8 *pCel
16524 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61  l = findCell(pPa
16525 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66  ge, i);.      if
16526 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  ( eType==PTRMAP_
16527 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20 20 20  OVERFLOW1 ){.   
16528 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e       CellInfo in
16529 66 6f 3b 0a 20 20 20 20 20 20 20 20 62 74 72 65  fo;.        btre
1652a 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
1652b 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
1652c 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  o);.        if( 
1652d 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29  info.iOverflow )
1652e 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
1652f 69 46 72 6f 6d 3d 3d 67 65 74 34 62 79 74 65 28  iFrom==get4byte(
16530 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65  &pCell[info.iOve
16531 72 66 6c 6f 77 5d 29 20 29 7b 0a 20 20 20 20 20  rflow]) ){.     
16532 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28         put4byte(
16533 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65  &pCell[info.iOve
16534 72 66 6c 6f 77 5d 2c 20 69 54 6f 29 3b 0a 20 20  rflow], iTo);.  
16535 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
16536 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
16537 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
16538 73 65 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  se{.        if( 
16539 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 3d  get4byte(pCell)=
1653a 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20  =iFrom ){.      
1653b 20 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65      put4byte(pCe
1653c 6c 6c 2c 20 69 54 6f 29 3b 0a 20 20 20 20 20 20  ll, iTo);.      
1653d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1653e 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1653f 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 69 3d   }.  .    if( i=
16540 3d 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20  =nCell ){.      
16541 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41  if( eType!=PTRMA
16542 50 5f 42 54 52 45 45 20 7c 7c 20 0a 20 20 20 20  P_BTREE || .    
16543 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28 26        get4byte(&
16544 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61  pPage->aData[pPa
16545 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  ge->hdrOffset+8]
16546 29 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20  )!=iFrom ){.    
16547 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
16548 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
16549 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 75        }.      pu
1654a 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
1654b 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
1654c 66 66 73 65 74 2b 38 5d 2c 20 69 54 6f 29 3b 0a  ffset+8], iTo);.
1654d 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65      }..    pPage
1654e 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49 6e 69  ->isInit = isIni
1654f 74 4f 72 69 67 3b 0a 20 20 7d 0a 20 20 72 65 74  tOrig;.  }.  ret
16550 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
16551 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68  .../*.** Move th
16552 65 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20  e open database 
16553 70 61 67 65 20 70 44 62 50 61 67 65 20 74 6f 20  page pDbPage to 
16554 6c 6f 63 61 74 69 6f 6e 20 69 46 72 65 65 50 61  location iFreePa
16555 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 64 61  ge in the .** da
16556 74 61 62 61 73 65 2e 20 54 68 65 20 70 44 62 50  tabase. The pDbP
16557 61 67 65 20 72 65 66 65 72 65 6e 63 65 20 72 65  age reference re
16558 6d 61 69 6e 73 20 76 61 6c 69 64 2e 0a 2a 2a 0a  mains valid..**.
16559 2a 2a 20 54 68 65 20 69 73 43 6f 6d 6d 69 74 20  ** The isCommit 
1655a 66 6c 61 67 20 69 6e 64 69 63 61 74 65 73 20 74  flag indicates t
1655b 68 61 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20  hat there is no 
1655c 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72  need to remember
1655d 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6a 6f 75   that.** the jou
1655e 72 6e 61 6c 20 6e 65 65 64 73 20 74 6f 20 62 65  rnal needs to be
1655f 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65   sync()ed before
16560 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70   database page p
16561 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20 0a 2a 2a  DbPage->pgno .**
16562 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20   can be written 
16563 74 6f 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 68  to. The caller h
16564 61 73 20 61 6c 72 65 61 64 79 20 70 72 6f 6d 69  as already promi
16565 73 65 64 20 6e 6f 74 20 74 6f 20 77 72 69 74 65  sed not to write
16566 20 74 6f 20 74 68 61 74 0a 2a 2a 20 70 61 67 65   to that.** page
16567 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
16568 72 65 6c 6f 63 61 74 65 50 61 67 65 28 0a 20 20  relocatePage(.  
16569 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20 20  BtShared *pBt,  
1656a 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74 72 65           /* Btre
1656b 65 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  e */.  MemPage *
1656c 70 44 62 50 61 67 65 2c 20 20 20 20 20 20 20 20  pDbPage,        
1656d 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 74 6f 20  /* Open page to 
1656e 6d 6f 76 65 20 2a 2f 0a 20 20 75 38 20 65 54 79  move */.  u8 eTy
1656f 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pe,             
16570 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 6d 61     /* Pointer ma
16571 70 20 27 74 79 70 65 27 20 65 6e 74 72 79 20 66  p 'type' entry f
16572 6f 72 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20  or pDbPage */.  
16573 50 67 6e 6f 20 69 50 74 72 50 61 67 65 2c 20 20  Pgno iPtrPage,  
16574 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
16575 74 65 72 20 6d 61 70 20 27 70 61 67 65 2d 6e 6f  ter map 'page-no
16576 27 20 65 6e 74 72 79 20 66 6f 72 20 70 44 62 50  ' entry for pDbP
16577 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 46  age */.  Pgno iF
16578 72 65 65 50 61 67 65 2c 20 20 20 20 20 20 20 20  reePage,        
16579 20 20 2f 2a 20 54 68 65 20 6c 6f 63 61 74 69 6f    /* The locatio
1657a 6e 20 74 6f 20 6d 6f 76 65 20 70 44 62 50 61 67  n to move pDbPag
1657b 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 73  e to */.  int is
1657c 43 6f 6d 6d 69 74 20 20 20 20 20 20 20 20 20 20  Commit          
1657d 20 20 20 2f 2a 20 69 73 43 6f 6d 6d 69 74 20 66     /* isCommit f
1657e 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 73 71  lag passed to sq
1657f 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61  lite3PagerMovepa
16580 67 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61  ge */.){.  MemPa
16581 67 65 20 2a 70 50 74 72 50 61 67 65 3b 20 20 20  ge *pPtrPage;   
16582 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74  /* The page that
16583 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e   contains a poin
16584 74 65 72 20 74 6f 20 70 44 62 50 61 67 65 20 2a  ter to pDbPage *
16585 2f 0a 20 20 50 67 6e 6f 20 69 44 62 50 61 67 65  /.  Pgno iDbPage
16586 20 3d 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f   = pDbPage->pgno
16587 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65  ;.  Pager *pPage
16588 72 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b  r = pBt->pPager;
16589 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
1658a 73 65 72 74 28 20 65 54 79 70 65 3d 3d 50 54 52  sert( eType==PTR
1658b 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 7c 7c  MAP_OVERFLOW2 ||
1658c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f   eType==PTRMAP_O
1658d 56 45 52 46 4c 4f 57 31 20 7c 7c 20 0a 20 20 20  VERFLOW1 || .   
1658e 20 20 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50     eType==PTRMAP
1658f 5f 42 54 52 45 45 20 7c 7c 20 65 54 79 70 65 3d  _BTREE || eType=
16590 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45  =PTRMAP_ROOTPAGE
16591 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
16592 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
16593 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
16594 20 20 61 73 73 65 72 74 28 20 70 44 62 50 61 67    assert( pDbPag
16595 65 2d 3e 70 42 74 3d 3d 70 42 74 20 29 3b 0a 0a  e->pBt==pBt );..
16596 20 20 2f 2a 20 4d 6f 76 65 20 70 61 67 65 20 69    /* Move page i
16597 44 62 50 61 67 65 20 66 72 6f 6d 20 69 74 73 20  DbPage from its 
16598 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e  current location
16599 20 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20   to page number 
1659a 69 46 72 65 65 50 61 67 65 20 2a 2f 0a 20 20 54  iFreePage */.  T
1659b 52 41 43 45 28 28 22 41 55 54 4f 56 41 43 55 55  RACE(("AUTOVACUU
1659c 4d 3a 20 4d 6f 76 69 6e 67 20 25 64 20 74 6f 20  M: Moving %d to 
1659d 66 72 65 65 20 70 61 67 65 20 25 64 20 28 70 74  free page %d (pt
1659e 72 20 70 61 67 65 20 25 64 20 74 79 70 65 20 25  r page %d type %
1659f 64 29 5c 6e 22 2c 20 0a 20 20 20 20 20 20 69 44  d)\n", .      iD
165a0 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67 65  bPage, iFreePage
165a1 2c 20 69 50 74 72 50 61 67 65 2c 20 65 54 79 70  , iPtrPage, eTyp
165a2 65 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  e));.  rc = sqli
165a3 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65  te3PagerMovepage
165a4 28 70 50 61 67 65 72 2c 20 70 44 62 50 61 67 65  (pPager, pDbPage
165a5 2d 3e 70 44 62 50 61 67 65 2c 20 69 46 72 65 65  ->pDbPage, iFree
165a6 50 61 67 65 2c 20 69 73 43 6f 6d 6d 69 74 29 3b  Page, isCommit);
165a7 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
165a8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
165a9 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 44 62  rn rc;.  }.  pDb
165aa 50 61 67 65 2d 3e 70 67 6e 6f 20 3d 20 69 46 72  Page->pgno = iFr
165ab 65 65 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 49 66  eePage;..  /* If
165ac 20 70 44 62 50 61 67 65 20 77 61 73 20 61 20 62   pDbPage was a b
165ad 74 72 65 65 2d 70 61 67 65 2c 20 74 68 65 6e 20  tree-page, then 
165ae 69 74 20 6d 61 79 20 68 61 76 65 20 63 68 69 6c  it may have chil
165af 64 20 70 61 67 65 73 20 61 6e 64 2f 6f 72 20 63  d pages and/or c
165b0 65 6c 6c 73 0a 20 20 2a 2a 20 74 68 61 74 20 70  ells.  ** that p
165b1 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77  oint to overflow
165b2 20 70 61 67 65 73 2e 20 54 68 65 20 70 6f 69 6e   pages. The poin
165b3 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20  ter map entries 
165b4 66 6f 72 20 61 6c 6c 20 74 68 65 73 65 0a 20 20  for all these.  
165b5 2a 2a 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f  ** pages need to
165b6 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a   be changed..  *
165b7 2a 0a 20 20 2a 2a 20 49 66 20 70 44 62 50 61 67  *.  ** If pDbPag
165b8 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77  e is an overflow
165b9 20 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20   page, then the 
165ba 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6d 61  first 4 bytes ma
165bb 79 20 73 74 6f 72 65 20 61 0a 20 20 2a 2a 20 70  y store a.  ** p
165bc 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 75 62 73  ointer to a subs
165bd 65 71 75 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20  equent overflow 
165be 70 61 67 65 2e 20 49 66 20 74 68 69 73 20 69 73  page. If this is
165bf 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 0a   the case, then.
165c0 20 20 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72    ** the pointer
165c1 20 6d 61 70 20 6e 65 65 64 73 20 74 6f 20 62 65   map needs to be
165c2 20 75 70 64 61 74 65 64 20 66 6f 72 20 74 68 65   updated for the
165c3 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72   subsequent over
165c4 66 6c 6f 77 20 70 61 67 65 2e 0a 20 20 2a 2f 0a  flow page..  */.
165c5 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52    if( eType==PTR
165c6 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65 54 79  MAP_BTREE || eTy
165c7 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50  pe==PTRMAP_ROOTP
165c8 41 47 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  AGE ){.    rc = 
165c9 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28  setChildPtrmaps(
165ca 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
165cb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
165cc 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
165cd 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  rc;.    }.  }els
165ce 65 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 65 78 74  e{.    Pgno next
165cf 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28  Ovfl = get4byte(
165d0 70 44 62 50 61 67 65 2d 3e 61 44 61 74 61 29 3b  pDbPage->aData);
165d1 0a 20 20 20 20 69 66 28 20 6e 65 78 74 4f 76 66  .    if( nextOvf
165d2 6c 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 74  l!=0 ){.      pt
165d3 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6e 65 78  rmapPut(pBt, nex
165d4 74 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56  tOvfl, PTRMAP_OV
165d5 45 52 46 4c 4f 57 32 2c 20 69 46 72 65 65 50 61  ERFLOW2, iFreePa
165d6 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20  ge, &rc);.      
165d7 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
165d8 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  K ){.        ret
165d9 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
165da 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
165db 46 69 78 20 74 68 65 20 64 61 74 61 62 61 73 65  Fix the database
165dc 20 70 6f 69 6e 74 65 72 20 6f 6e 20 70 61 67 65   pointer on page
165dd 20 69 50 74 72 50 61 67 65 20 74 68 61 74 20 70   iPtrPage that p
165de 6f 69 6e 74 65 64 20 61 74 20 69 44 62 50 61 67  ointed at iDbPag
165df 65 20 73 6f 0a 20 20 2a 2a 20 74 68 61 74 20 69  e so.  ** that i
165e0 74 20 70 6f 69 6e 74 73 20 61 74 20 69 46 72 65  t points at iFre
165e1 65 50 61 67 65 2e 20 41 6c 73 6f 20 66 69 78 20  ePage. Also fix 
165e2 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  the pointer map 
165e3 65 6e 74 72 79 20 66 6f 72 0a 20 20 2a 2a 20 69  entry for.  ** i
165e4 50 74 72 50 61 67 65 2e 0a 20 20 2a 2f 0a 20 20  PtrPage..  */.  
165e5 69 66 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41  if( eType!=PTRMA
165e6 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20  P_ROOTPAGE ){.  
165e7 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50    rc = btreeGetP
165e8 61 67 65 28 70 42 74 2c 20 69 50 74 72 50 61 67  age(pBt, iPtrPag
165e9 65 2c 20 26 70 50 74 72 50 61 67 65 2c 20 30 29  e, &pPtrPage, 0)
165ea 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
165eb 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
165ec 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
165ed 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
165ee 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 74  e3PagerWrite(pPt
165ef 72 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  rPage->pDbPage);
165f0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
165f1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
165f2 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72  releasePage(pPtr
165f3 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72 65 74  Page);.      ret
165f4 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
165f5 20 20 72 63 20 3d 20 6d 6f 64 69 66 79 50 61 67    rc = modifyPag
165f6 65 50 6f 69 6e 74 65 72 28 70 50 74 72 50 61 67  ePointer(pPtrPag
165f7 65 2c 20 69 44 62 50 61 67 65 2c 20 69 46 72 65  e, iDbPage, iFre
165f8 65 50 61 67 65 2c 20 65 54 79 70 65 29 3b 0a 20  ePage, eType);. 
165f9 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
165fa 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66  PtrPage);.    if
165fb 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
165fc 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50  ){.      ptrmapP
165fd 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61 67  ut(pBt, iFreePag
165fe 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61  e, eType, iPtrPa
165ff 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 7d 0a  ge, &rc);.    }.
16600 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
16601 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  .}../* Forward d
16602 65 63 6c 61 72 61 74 69 6f 6e 20 72 65 71 75 69  eclaration requi
16603 72 65 64 20 62 79 20 69 6e 63 72 56 61 63 75 75  red by incrVacuu
16604 6d 53 74 65 70 28 29 2e 20 2a 2f 0a 73 74 61 74  mStep(). */.stat
16605 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42  ic int allocateB
16606 74 72 65 65 50 61 67 65 28 42 74 53 68 61 72 65  treePage(BtShare
16607 64 20 2a 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 2c  d *, MemPage **,
16608 20 50 67 6e 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75   Pgno *, Pgno, u
16609 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f  8);../*.** Perfo
1660a 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70  rm a single step
1660b 20 6f 66 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74   of an increment
1660c 61 6c 2d 76 61 63 75 75 6d 2e 20 49 66 20 73 75  al-vacuum. If su
1660d 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 72 65 74  ccessful,.** ret
1660e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49  urn SQLITE_OK. I
1660f 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 77 6f  f there is no wo
16610 72 6b 20 74 6f 20 64 6f 20 28 61 6e 64 20 74 68  rk to do (and th
16611 65 72 65 66 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f  erefore no.** po
16612 69 6e 74 20 69 6e 20 63 61 6c 6c 69 6e 67 20 74  int in calling t
16613 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 67 61  his function aga
16614 69 6e 29 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  in), return SQLI
16615 54 45 5f 44 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d  TE_DONE..**.** M
16616 6f 72 65 20 73 70 65 63 69 66 69 63 6c 79 2c 20  ore specificly, 
16617 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 74  this function at
16618 74 65 6d 70 74 73 20 74 6f 20 72 65 2d 6f 72 67  tempts to re-org
16619 61 6e 69 7a 65 20 74 68 65 20 0a 2a 2a 20 64 61  anize the .** da
1661a 74 61 62 61 73 65 20 73 6f 20 74 68 61 74 20 74  tabase so that t
1661b 68 65 20 6c 61 73 74 20 70 61 67 65 20 6f 66 20  he last page of 
1661c 74 68 65 20 66 69 6c 65 20 63 75 72 72 65 6e 74  the file current
1661d 6c 79 20 69 6e 20 75 73 65 0a 2a 2a 20 69 73 20  ly in use.** is 
1661e 6e 6f 20 6c 6f 6e 67 65 72 20 69 6e 20 75 73 65  no longer in use
1661f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e  ..**.** If the n
16620 46 69 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73  Fin parameter is
16621 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 69 73 20   non-zero, this 
16622 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65 73  function assumes
16623 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61 6c  .** that the cal
16624 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63 61  ler will keep ca
16625 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75 6d  lling incrVacuum
16626 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a 20  Step() until.** 
16627 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  it returns SQLIT
16628 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72 72  E_DONE or an err
16629 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46 69  or, and that nFi
1662a 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62  n is the.** numb
1662b 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 65 20  er of pages the 
1662c 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69  database file wi
1662d 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65 72  ll contain after
1662e 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65 73   this .** proces
1662f 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 20 20  s is complete.  
16630 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c  If nFin is zero,
16631 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   it is assumed t
16632 68 61 74 0a 2a 2a 20 69 6e 63 72 56 61 63 75 75  hat.** incrVacuu
16633 6d 53 74 65 70 28 29 20 77 69 6c 6c 20 62 65 20  mStep() will be 
16634 63 61 6c 6c 65 64 20 61 20 66 69 6e 69 74 65 20  called a finite 
16635 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 73 0a  amount of times.
16636 2a 2a 20 77 68 69 63 68 20 6d 61 79 20 6f 72 20  ** which may or 
16637 6d 61 79 20 6e 6f 74 20 65 6d 70 74 79 20 74 68  may not empty th
16638 65 20 66 72 65 65 6c 69 73 74 2e 20 20 41 20 66  e freelist.  A f
16639 75 6c 6c 20 61 75 74 6f 76 61 63 75 75 6d 0a 2a  ull autovacuum.*
1663a 2a 20 68 61 73 20 6e 46 69 6e 3e 30 2e 20 20 41  * has nFin>0.  A
1663b 20 22 50 52 41 47 4d 41 20 69 6e 63 72 65 6d 65   "PRAGMA increme
1663c 6e 74 61 6c 5f 76 61 63 75 75 6d 22 20 68 61 73  ntal_vacuum" has
1663d 20 6e 46 69 6e 3d 3d 30 2e 0a 2a 2f 0a 73 74 61   nFin==0..*/.sta
1663e 74 69 63 20 69 6e 74 20 69 6e 63 72 56 61 63 75  tic int incrVacu
1663f 75 6d 53 74 65 70 28 42 74 53 68 61 72 65 64 20  umStep(BtShared 
16640 2a 70 42 74 2c 20 50 67 6e 6f 20 6e 46 69 6e 2c  *pBt, Pgno nFin,
16641 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 29 7b 0a   Pgno iLastPg){.
16642 20 20 50 67 6e 6f 20 6e 46 72 65 65 4c 69 73 74    Pgno nFreeList
16643 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
16644 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73  umber of pages s
16645 74 69 6c 6c 20 6f 6e 20 74 68 65 20 66 72 65 65  till on the free
16646 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65  -list */..  asse
16647 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
16648 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
16649 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
1664a 69 4c 61 73 74 50 67 3e 6e 46 69 6e 20 29 3b 0a  iLastPg>nFin );.
1664b 0a 20 20 69 66 28 20 21 50 54 52 4d 41 50 5f 49  .  if( !PTRMAP_I
1664c 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74  SPAGE(pBt, iLast
1664d 50 67 29 20 26 26 20 69 4c 61 73 74 50 67 21 3d  Pg) && iLastPg!=
1664e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
1664f 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 69 6e  E(pBt) ){.    in
16650 74 20 72 63 3b 0a 20 20 20 20 75 38 20 65 54 79  t rc;.    u8 eTy
16651 70 65 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50 74  pe;.    Pgno iPt
16652 72 50 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72 65  rPage;..    nFre
16653 65 4c 69 73 74 20 3d 20 67 65 74 34 62 79 74 65  eList = get4byte
16654 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61  (&pBt->pPage1->a
16655 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 69  Data[36]);.    i
16656 66 28 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 20  f( nFreeList==0 
16657 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
16658 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
16659 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72   }..    rc = ptr
1665a 6d 61 70 47 65 74 28 70 42 74 2c 20 69 4c 61 73  mapGet(pBt, iLas
1665b 74 50 67 2c 20 26 65 54 79 70 65 2c 20 26 69 50  tPg, &eType, &iP
1665c 74 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  trPage);.    if(
1665d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1665e 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
1665f 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  c;.    }.    if(
16660 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52   eType==PTRMAP_R
16661 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 20  OOTPAGE ){.     
16662 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
16663 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
16664 20 7d 0a 0a 20 20 20 20 69 66 28 20 65 54 79 70   }..    if( eTyp
16665 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41  e==PTRMAP_FREEPA
16666 47 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  GE ){.      if( 
16667 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  nFin==0 ){.     
16668 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65     /* Remove the
16669 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 66   page from the f
1666a 69 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 2e 20  iles free-list. 
1666b 54 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75  This is not requ
1666c 69 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  ired.        ** 
1666d 69 66 20 6e 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a  if nFin is non-z
1666e 65 72 6f 2e 20 49 6e 20 74 68 61 74 20 63 61 73  ero. In that cas
1666f 65 2c 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  e, the free-list
16670 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20   will be.       
16671 20 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 74 6f   ** truncated to
16672 20 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69 73   zero after this
16673 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
16674 73 2c 20 73 6f 20 69 74 20 64 6f 65 73 6e 27 74  s, so it doesn't
16675 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 74   .        ** mat
16676 74 65 72 20 69 66 20 69 74 20 73 74 69 6c 6c 20  ter if it still 
16677 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 67 61  contains some ga
16678 72 62 61 67 65 20 65 6e 74 72 69 65 73 2e 0a 20  rbage entries.. 
16679 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
1667a 20 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 0a    Pgno iFreePg;.
1667b 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20          MemPage 
1667c 2a 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20  *pFreePg;.      
1667d 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42    rc = allocateB
1667e 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70  treePage(pBt, &p
1667f 46 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 67  FreePg, &iFreePg
16680 2c 20 69 4c 61 73 74 50 67 2c 20 31 29 3b 0a 20  , iLastPg, 1);. 
16681 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
16682 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16683 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
16684 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
16685 20 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65     assert( iFree
16686 50 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a 20  Pg==iLastPg );. 
16687 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
16688 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20  ge(pFreePg);.   
16689 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20     }.    } else 
1668a 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72  {.      Pgno iFr
1668b 65 65 50 67 3b 20 20 20 20 20 20 20 20 20 20 20  eePg;           
1668c 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 72    /* Index of fr
1668d 65 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20  ee page to move 
1668e 70 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20 20  pLastPg to */.  
1668f 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 61      MemPage *pLa
16690 73 74 50 67 3b 0a 0a 20 20 20 20 20 20 72 63 20  stPg;..      rc 
16691 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
16692 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 4c  Bt, iLastPg, &pL
16693 61 73 74 50 67 2c 20 30 29 3b 0a 20 20 20 20 20  astPg, 0);.     
16694 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
16695 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
16696 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
16697 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 46  ..      /* If nF
16698 69 6e 20 69 73 20 7a 65 72 6f 2c 20 74 68 69 73  in is zero, this
16699 20 6c 6f 6f 70 20 72 75 6e 73 20 65 78 61 63 74   loop runs exact
1669a 6c 79 20 6f 6e 63 65 20 61 6e 64 20 70 61 67 65  ly once and page
1669b 20 70 4c 61 73 74 50 67 0a 20 20 20 20 20 20 2a   pLastPg.      *
1669c 2a 20 69 73 20 73 77 61 70 70 65 64 20 77 69 74  * is swapped wit
1669d 68 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65  h the first free
1669e 20 70 61 67 65 20 70 75 6c 6c 65 64 20 6f 66 66   page pulled off
1669f 20 74 68 65 20 66 72 65 65 20 6c 69 73 74 2e 0a   the free list..
166a0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
166a1 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68  * On the other h
166a2 61 6e 64 2c 20 69 66 20 6e 46 69 6e 20 69 73 20  and, if nFin is 
166a3 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72  greater than zer
166a4 6f 2c 20 74 68 65 6e 20 6b 65 65 70 0a 20 20 20  o, then keep.   
166a5 20 20 20 2a 2a 20 6c 6f 6f 70 69 6e 67 20 75 6e     ** looping un
166a6 74 69 6c 20 61 20 66 72 65 65 2d 70 61 67 65 20  til a free-page 
166a7 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e 20 74  located within t
166a8 68 65 20 66 69 72 73 74 20 6e 46 69 6e 20 70 61  he first nFin pa
166a9 67 65 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  ges.      ** of 
166aa 74 68 65 20 66 69 6c 65 20 69 73 20 66 6f 75 6e  the file is foun
166ab 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
166ac 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 4d    do {.        M
166ad 65 6d 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b  emPage *pFreePg;
166ae 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c  .        rc = al
166af 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28  locateBtreePage(
166b0 70 42 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26  pBt, &pFreePg, &
166b1 69 46 72 65 65 50 67 2c 20 30 2c 20 30 29 3b 0a  iFreePg, 0, 0);.
166b2 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
166b3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
166b4 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
166b5 67 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20  ge(pLastPg);.   
166b6 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
166b7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
166b8 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
166b9 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20 20 20  pFreePg);.      
166ba 7d 77 68 69 6c 65 28 20 6e 46 69 6e 21 3d 30 20  }while( nFin!=0 
166bb 26 26 20 69 46 72 65 65 50 67 3e 6e 46 69 6e 20  && iFreePg>nFin 
166bc 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
166bd 20 69 46 72 65 65 50 67 3c 69 4c 61 73 74 50 67   iFreePg<iLastPg
166be 20 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 20   );.      .     
166bf 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
166c0 65 72 57 72 69 74 65 28 70 4c 61 73 74 50 67 2d  erWrite(pLastPg-
166c1 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
166c2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
166c3 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
166c4 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28   = relocatePage(
166c5 70 42 74 2c 20 70 4c 61 73 74 50 67 2c 20 65 54  pBt, pLastPg, eT
166c6 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c 20 69  ype, iPtrPage, i
166c7 46 72 65 65 50 67 2c 20 6e 46 69 6e 21 3d 30 29  FreePg, nFin!=0)
166c8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
166c9 72 65 6c 65 61 73 65 50 61 67 65 28 70 4c 61 73  releasePage(pLas
166ca 74 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  tPg);.      if( 
166cb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
166cc 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
166cd 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
166ce 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 46 69  }.  }..  if( nFi
166cf 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 73  n==0 ){.    iLas
166d0 74 50 67 2d 2d 3b 0a 20 20 20 20 77 68 69 6c 65  tPg--;.    while
166d1 28 20 69 4c 61 73 74 50 67 3d 3d 50 45 4e 44 49  ( iLastPg==PENDI
166d2 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
166d3 29 7c 7c 50 54 52 4d 41 50 5f 49 53 50 41 47 45  )||PTRMAP_ISPAGE
166d4 28 70 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29  (pBt, iLastPg) )
166d5 7b 0a 20 20 20 20 20 20 69 66 28 20 50 54 52 4d  {.      if( PTRM
166d6 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 69  AP_ISPAGE(pBt, i
166d7 4c 61 73 74 50 67 29 20 29 7b 0a 20 20 20 20 20  LastPg) ){.     
166d8 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 67 3b     MemPage *pPg;
166d9 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20  .        int rc 
166da 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
166db 42 74 2c 20 69 4c 61 73 74 50 67 2c 20 26 70 50  Bt, iLastPg, &pP
166dc 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  g, 0);.        i
166dd 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
166de 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
166df 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
166e0 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20   }.        rc = 
166e1 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
166e2 65 28 70 50 67 2d 3e 70 44 62 50 61 67 65 29 3b  e(pPg->pDbPage);
166e3 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
166e4 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20  Page(pPg);.     
166e5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
166e6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
166e7 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
166e8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
166e9 20 20 20 20 20 69 4c 61 73 74 50 67 2d 2d 3b 0a       iLastPg--;.
166ea 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
166eb 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d  3PagerTruncateIm
166ec 61 67 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  age(pBt->pPager,
166ed 20 69 4c 61 73 74 50 67 29 3b 0a 20 20 7d 0a 20   iLastPg);.  }. 
166ee 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
166ef 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 77 72  K;.}../*.** A wr
166f0 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
166f1 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20 62  must be opened b
166f2 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68  efore calling th
166f3 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20  is function..** 
166f4 49 74 20 70 65 72 66 6f 72 6d 73 20 61 20 73 69  It performs a si
166f5 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20 77 6f 72  ngle unit of wor
166f6 6b 20 74 6f 77 61 72 64 73 20 61 6e 20 69 6e 63  k towards an inc
166f7 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 2e  remental vacuum.
166f8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e  .**.** If the in
166f9 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
166fa 20 69 73 20 66 69 6e 69 73 68 65 64 20 61 66 74   is finished aft
166fb 65 72 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  er this function
166fc 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20 53 51 4c   has run,.** SQL
166fd 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65 74 75  ITE_DONE is retu
166fe 72 6e 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e  rned. If it is n
166ff 6f 74 20 66 69 6e 69 73 68 65 64 2c 20 62 75 74  ot finished, but
16700 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 72   no error occurr
16701 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  ed,.** SQLITE_OK
16702 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74   is returned. Ot
16703 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c 69 74  herwise an SQLit
16704 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 0a 2a  e error code. .*
16705 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16706 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
16707 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65  eIncrVacuum(Btre
16708 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  e *p){.  int rc;
16709 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1670a 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71   = p->pBt;..  sq
1670b 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
1670c 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42  p);.  assert( pB
1670d 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
1670e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26  ==TRANS_WRITE &&
1670f 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41   p->inTrans==TRA
16710 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 69 66  NS_WRITE );.  if
16711 28 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  ( !pBt->autoVacu
16712 75 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  um ){.    rc = S
16713 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65  QLITE_DONE;.  }e
16714 6c 73 65 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64  lse{.    invalid
16715 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61  ateAllOverflowCa
16716 63 68 65 28 70 42 74 29 3b 0a 20 20 20 20 72 63  che(pBt);.    rc
16717 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65   = incrVacuumSte
16718 70 28 70 42 74 2c 20 30 2c 20 70 61 67 65 72 50  p(pBt, 0, pagerP
16719 61 67 65 63 6f 75 6e 74 28 70 42 74 29 29 3b 0a  agecount(pBt));.
1671a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
1671b 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
1671c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1671d 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1671e 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74  s called prior t
1671f 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  o sqlite3PagerCo
16720 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72 61 6e  mmit when a tran
16721 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f  saction.** is co
16722 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e 20 61 75  mmited for an au
16723 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61  to-vacuum databa
16724 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c  se..**.** If SQL
16725 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
16726 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 6e  ed, then *pnTrun
16727 63 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  c is set to the 
16728 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 0a  number of pages.
16729 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
1672a 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 74  file should be t
1672b 72 75 6e 63 61 74 65 64 20 74 6f 20 64 75 72 69  runcated to duri
1672c 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 72  ng the commit pr
1672d 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20  ocess. .** i.e. 
1672e 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73  the database has
1672f 20 62 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a 65   been reorganize
16730 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 74  d so that only t
16731 68 65 20 66 69 72 73 74 20 2a 70 6e 54 72 75 6e  he first *pnTrun
16732 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 65 20 69  c.** pages are i
16733 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  n use..*/.static
16734 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d 43   int autoVacuumC
16735 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 64 20 2a  ommit(BtShared *
16736 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  pBt){.  int rc =
16737 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61   SQLITE_OK;.  Pa
16738 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42  ger *pPager = pB
16739 74 2d 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 41  t->pPager;.  VVA
1673a 5f 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 20  _ONLY( int nRef 
1673b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  = sqlite3PagerRe
1673c 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29  fcount(pPager) )
1673d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
1673e 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1673f 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
16740 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76   invalidateAllOv
16741 65 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29  erflowCache(pBt)
16742 3b 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d 3e  ;.  assert(pBt->
16743 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 69  autoVacuum);.  i
16744 66 28 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 63  f( !pBt->incrVac
16745 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20  uum ){.    Pgno 
16746 6e 46 69 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  nFin;         /*
16747 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
16748 20 69 6e 20 64 61 74 61 62 61 73 65 20 61 66 74   in database aft
16749 65 72 20 61 75 74 6f 76 61 63 75 75 6d 69 6e 67  er autovacuuming
1674a 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 46 72   */.    Pgno nFr
1674b 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ee;        /* Nu
1674c 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e  mber of pages on
1674d 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e   the freelist in
1674e 69 74 69 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 50  itially */.    P
1674f 67 6e 6f 20 6e 50 74 72 6d 61 70 3b 20 20 20 20  gno nPtrmap;    
16750 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 50    /* Number of P
16751 74 72 4d 61 70 20 70 61 67 65 73 20 74 6f 20 62  trMap pages to b
16752 65 20 66 72 65 65 64 20 2a 2f 0a 20 20 20 20 50  e freed */.    P
16753 67 6e 6f 20 69 46 72 65 65 3b 20 20 20 20 20 20  gno iFree;      
16754 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 70 61    /* The next pa
16755 67 65 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a  ge to be freed *
16756 2f 0a 20 20 20 20 69 6e 74 20 6e 45 6e 74 72 79  /.    int nEntry
16757 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
16758 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 6f 6e  er of entries on
16759 20 6f 6e 65 20 70 74 72 6d 61 70 20 70 61 67 65   one ptrmap page
1675a 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 6e 4f 72   */.    Pgno nOr
1675b 69 67 3b 20 20 20 20 20 20 20 20 2f 2a 20 44 61  ig;        /* Da
1675c 74 61 62 61 73 65 20 73 69 7a 65 20 62 65 66 6f  tabase size befo
1675d 72 65 20 66 72 65 65 69 6e 67 20 2a 2f 0a 0a 20  re freeing */.. 
1675e 20 20 20 6e 4f 72 69 67 20 3d 20 70 61 67 65 72     nOrig = pager
1675f 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a  Pagecount(pBt);.
16760 20 20 20 20 69 66 28 20 50 54 52 4d 41 50 5f 49      if( PTRMAP_I
16761 53 50 41 47 45 28 70 42 74 2c 20 6e 4f 72 69 67  SPAGE(pBt, nOrig
16762 29 20 7c 7c 20 6e 4f 72 69 67 3d 3d 50 45 4e 44  ) || nOrig==PEND
16763 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
16764 74 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  t) ){.      /* I
16765 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
16766 65 20 74 6f 20 63 72 65 61 74 65 20 61 20 64 61  e to create a da
16767 74 61 62 61 73 65 20 66 6f 72 20 77 68 69 63 68  tabase for which
16768 20 74 68 65 20 66 69 6e 61 6c 20 70 61 67 65 0a   the final page.
16769 20 20 20 20 20 20 2a 2a 20 69 73 20 65 69 74 68        ** is eith
1676a 65 72 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70  er a pointer-map
1676b 20 70 61 67 65 20 6f 72 20 74 68 65 20 70 65 6e   page or the pen
1676c 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65 2e 20  ding-byte page. 
1676d 49 66 20 6f 6e 65 0a 20 20 20 20 20 20 2a 2a 20  If one.      ** 
1676e 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20  is encountered, 
1676f 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20 63  this indicates c
16770 6f 72 72 75 70 74 69 6f 6e 2e 0a 20 20 20 20 20  orruption..     
16771 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
16772 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
16773 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  BKPT;.    }..   
16774 20 6e 46 72 65 65 20 3d 20 67 65 74 34 62 79 74   nFree = get4byt
16775 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  e(&pBt->pPage1->
16776 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20  aData[36]);.    
16777 6e 45 6e 74 72 79 20 3d 20 70 42 74 2d 3e 75 73  nEntry = pBt->us
16778 61 62 6c 65 53 69 7a 65 2f 35 3b 0a 20 20 20 20  ableSize/5;.    
16779 6e 50 74 72 6d 61 70 20 3d 20 28 6e 46 72 65 65  nPtrmap = (nFree
1677a 2d 6e 4f 72 69 67 2b 50 54 52 4d 41 50 5f 50 41  -nOrig+PTRMAP_PA
1677b 47 45 4e 4f 28 70 42 74 2c 20 6e 4f 72 69 67 29  GENO(pBt, nOrig)
1677c 2b 6e 45 6e 74 72 79 29 2f 6e 45 6e 74 72 79 3b  +nEntry)/nEntry;
1677d 0a 20 20 20 20 6e 46 69 6e 20 3d 20 6e 4f 72 69  .    nFin = nOri
1677e 67 20 2d 20 6e 46 72 65 65 20 2d 20 6e 50 74 72  g - nFree - nPtr
1677f 6d 61 70 3b 0a 20 20 20 20 69 66 28 20 6e 4f 72  map;.    if( nOr
16780 69 67 3e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  ig>PENDING_BYTE_
16781 50 41 47 45 28 70 42 74 29 20 26 26 20 6e 46 69  PAGE(pBt) && nFi
16782 6e 3c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  n<PENDING_BYTE_P
16783 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
16784 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a    nFin--;.    }.
16785 20 20 20 20 77 68 69 6c 65 28 20 50 54 52 4d 41      while( PTRMA
16786 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 6e 46  P_ISPAGE(pBt, nF
16787 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d 3d 50 45 4e  in) || nFin==PEN
16788 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
16789 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 46 69  Bt) ){.      nFi
1678a 6e 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n--;.    }.    i
1678b 66 28 20 6e 46 69 6e 3e 6e 4f 72 69 67 20 29 20  f( nFin>nOrig ) 
1678c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1678d 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 20  RRUPT_BKPT;..   
1678e 20 66 6f 72 28 69 46 72 65 65 3d 6e 4f 72 69 67   for(iFree=nOrig
1678f 3b 20 69 46 72 65 65 3e 6e 46 69 6e 20 26 26 20  ; iFree>nFin && 
16790 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 69  rc==SQLITE_OK; i
16791 46 72 65 65 2d 2d 29 7b 0a 20 20 20 20 20 20 72  Free--){.      r
16792 63 20 3d 20 69 6e 63 72 56 61 63 75 75 6d 53 74  c = incrVacuumSt
16793 65 70 28 70 42 74 2c 20 6e 46 69 6e 2c 20 69 46  ep(pBt, nFin, iF
16794 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ree);.    }.    
16795 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  if( (rc==SQLITE_
16796 44 4f 4e 45 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  DONE || rc==SQLI
16797 54 45 5f 4f 4b 29 20 26 26 20 6e 46 72 65 65 3e  TE_OK) && nFree>
16798 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
16799 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
1679a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1679b 65 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61  erWrite(pBt->pPa
1679c 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ge1->pDbPage);. 
1679d 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
1679e 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
1679f 61 5b 33 32 5d 2c 20 30 29 3b 0a 20 20 20 20 20  a[32], 0);.     
167a0 20 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e   put4byte(&pBt->
167a1 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
167a2 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  ], 0);.      sql
167a3 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
167a4 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50 61 67  eImage(pBt->pPag
167a5 65 72 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20 7d  er, nFin);.    }
167a6 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
167a7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
167a8 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c  sqlite3PagerRoll
167a9 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20  back(pPager);.  
167aa 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72    }.  }..  asser
167ab 74 28 20 6e 52 65 66 3d 3d 73 71 6c 69 74 65 33  t( nRef==sqlite3
167ac 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50  PagerRefcount(pP
167ad 61 67 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ager) );.  retur
167ae 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6c 73 65 20 2f  n rc;.}..#else /
167af 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  * ifndef SQLITE_
167b0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20  OMIT_AUTOVACUUM 
167b1 2a 2f 0a 23 20 64 65 66 69 6e 65 20 73 65 74 43  */.# define setC
167b2 68 69 6c 64 50 74 72 6d 61 70 73 28 78 29 20 53  hildPtrmaps(x) S
167b3 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a  QLITE_OK.#endif.
167b4 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
167b5 69 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72  ine does the fir
167b6 73 74 20 70 68 61 73 65 20 6f 66 20 61 20 74 77  st phase of a tw
167b7 6f 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20  o-phase commit. 
167b8 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   This routine.**
167b9 20 63 61 75 73 65 73 20 61 20 72 6f 6c 6c 62 61   causes a rollba
167ba 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65  ck journal to be
167bb 20 63 72 65 61 74 65 64 20 28 69 66 20 69 74 20   created (if it 
167bc 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  does not already
167bd 20 65 78 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70   exist).** and p
167be 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20 65 6e  opulated with en
167bf 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ough information
167c0 20 73 6f 20 74 68 61 74 20 69 66 20 61 20 70 6f   so that if a po
167c1 77 65 72 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a  wer loss occurs.
167c2 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
167c3 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20  can be restored 
167c4 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20  to its original 
167c5 73 74 61 74 65 20 62 79 20 70 6c 61 79 69 6e 67  state by playing
167c6 20 62 61 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75   back.** the jou
167c7 72 6e 61 6c 2e 20 20 54 68 65 6e 20 74 68 65 20  rnal.  Then the 
167c8 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
167c9 6a 6f 75 72 6e 61 6c 20 61 72 65 20 66 6c 75 73  journal are flus
167ca 68 65 64 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68  hed out to.** th
167cb 65 20 64 69 73 6b 2e 20 20 41 66 74 65 72 20 74  e disk.  After t
167cc 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61  he journal is sa
167cd 66 65 6c 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74  fely on oxide, t
167ce 68 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68  he changes to th
167cf 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 72  e.** database ar
167d0 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
167d1 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
167d2 20 61 6e 64 20 66 6c 75 73 68 65 64 20 74 6f 20   and flushed to 
167d3 6f 78 69 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65  oxide..** At the
167d4 20 65 6e 64 20 6f 66 20 74 68 69 73 20 63 61 6c   end of this cal
167d5 6c 2c 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  l, the rollback 
167d6 6a 6f 75 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78  journal still ex
167d7 69 73 74 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64  ists on the.** d
167d8 69 73 6b 20 61 6e 64 20 77 65 20 61 72 65 20 73  isk and we are s
167d9 74 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c  till holding all
167da 20 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65 20 74   locks, so the t
167db 72 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 6e  ransaction has n
167dc 6f 74 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e  ot.** committed.
167dd 20 20 53 65 65 20 73 71 6c 69 74 65 33 42 74 72    See sqlite3Btr
167de 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
167df 28 29 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  () for the secon
167e0 64 20 70 68 61 73 65 20 6f 66 20 74 68 65 0a 2a  d phase of the.*
167e1 2a 20 63 6f 6d 6d 69 74 20 70 72 6f 63 65 73 73  * commit process
167e2 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c  ..**.** This cal
167e3 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  l is a no-op if 
167e4 6e 6f 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63  no write-transac
167e5 74 69 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c  tion is currentl
167e6 79 20 61 63 74 69 76 65 20 6f 6e 20 70 42 74 2e  y active on pBt.
167e7 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  .**.** Otherwise
167e8 2c 20 73 79 6e 63 20 74 68 65 20 64 61 74 61 62  , sync the datab
167e9 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65  ase file for the
167ea 20 62 74 72 65 65 20 70 42 74 2e 20 7a 4d 61 73   btree pBt. zMas
167eb 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a  ter points to.**
167ec 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d   the name of a m
167ed 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
167ee 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  le that should b
167ef 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
167f0 68 65 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c  he.** individual
167f1 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6f   journal file, o
167f2 72 20 69 73 20 4e 55 4c 4c 2c 20 69 6e 64 69 63  r is NULL, indic
167f3 61 74 69 6e 67 20 6e 6f 20 6d 61 73 74 65 72 20  ating no master 
167f4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a  journal file .**
167f5 20 28 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73   (single databas
167f6 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 0a  e transaction)..
167f7 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  **.** When this 
167f8 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 6d  is called, the m
167f9 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 73 68  aster journal sh
167fa 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 61 76  ould already hav
167fb 65 20 62 65 65 6e 0a 2a 2a 20 63 72 65 61 74 65  e been.** create
167fc 64 2c 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  d, populated wit
167fd 68 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20 70  h this journal p
167fe 6f 69 6e 74 65 72 20 61 6e 64 20 73 79 6e 63 65  ointer and synce
167ff 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a  d to disk..**.**
16800 20 4f 6e 63 65 20 74 68 69 73 20 69 73 20 72 6f   Once this is ro
16801 75 74 69 6e 65 20 68 61 73 20 72 65 74 75 72 6e  utine has return
16802 65 64 2c 20 74 68 65 20 6f 6e 6c 79 20 74 68 69  ed, the only thi
16803 6e 67 20 72 65 71 75 69 72 65 64 20 74 6f 20 63  ng required to c
16804 6f 6d 6d 69 74 0a 2a 2a 20 74 68 65 20 77 72 69  ommit.** the wri
16805 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 66  te-transaction f
16806 6f 72 20 74 68 69 73 20 64 61 74 61 62 61 73 65  or this database
16807 20 66 69 6c 65 20 69 73 20 74 6f 20 64 65 6c 65   file is to dele
16808 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 0a  te the journal..
16809 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1680a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1680b 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  eeCommitPhaseOne
1680c 28 42 74 72 65 65 20 2a 70 2c 20 63 6f 6e 73 74  (Btree *p, const
1680d 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b   char *zMaster){
1680e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1680f 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e  TE_OK;.  if( p->
16810 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
16811 52 49 54 45 20 29 7b 0a 20 20 20 20 42 74 53 68  RITE ){.    BtSh
16812 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
16813 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  Bt;.    sqlite3B
16814 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 23 69  treeEnter(p);.#i
16815 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
16816 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
16817 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
16818 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 63  cuum ){.      rc
16819 20 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d   = autoVacuumCom
1681a 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 20 20  mit(pBt);.      
1681b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1681c 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  K ){.        sql
1681d 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1681e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1681f 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
16820 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 72    }.#endif.    r
16821 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
16822 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70  CommitPhaseOne(p
16823 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 4d 61 73  Bt->pPager, zMas
16824 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  ter, 0);.    sql
16825 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
16826 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
16827 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
16828 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
16829 6c 6c 65 64 20 66 72 6f 6d 20 62 6f 74 68 20 42  lled from both B
1682a 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54  treeCommitPhaseT
1682b 77 6f 28 29 20 61 6e 64 20 42 74 72 65 65 52 6f  wo() and BtreeRo
1682c 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 61 74 20 74  llback().** at t
1682d 68 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66  he conclusion of
1682e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a   a transaction..
1682f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62  */.static void b
16830 74 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69  treeEndTransacti
16831 6f 6e 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  on(Btree *p){.  
16832 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
16833 70 2d 3e 70 42 74 3b 0a 20 20 61 73 73 65 72 74  p->pBt;.  assert
16834 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
16835 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 0a  ldsMutex(p) );..
16836 20 20 62 74 72 65 65 43 6c 65 61 72 48 61 73 43    btreeClearHasC
16837 6f 6e 74 65 6e 74 28 70 42 74 29 3b 0a 20 20 69  ontent(pBt);.  i
16838 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 54 52  f( p->inTrans>TR
16839 41 4e 53 5f 4e 4f 4e 45 20 26 26 20 70 2d 3e 64  ANS_NONE && p->d
1683a 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
1683b 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  >1 ){.    /* If 
1683c 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72 20  there are other 
1683d 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74  active statement
1683e 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 20 74 6f  s that belong to
1683f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 0a 20   this database. 
16840 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2c 20 64 6f     ** handle, do
16841 77 6e 67 72 61 64 65 20 74 6f 20 61 20 72 65 61  wngrade to a rea
16842 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63 74 69  d-only transacti
16843 6f 6e 2e 20 54 68 65 20 6f 74 68 65 72 20 73 74  on. The other st
16844 61 74 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a 20  atements.    ** 
16845 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 72 65 61  may still be rea
16846 64 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 64 61  ding from the da
16847 74 61 62 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20  tabase.  */.    
16848 64 6f 77 6e 67 72 61 64 65 41 6c 6c 53 68 61 72  downgradeAllShar
16849 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
1684a 73 28 70 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 54  s(p);.    p->inT
1684b 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 52 45 41  rans = TRANS_REA
1684c 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  D;.  }else{.    
1684d 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65  /* If the handle
1684e 20 68 61 64 20 61 6e 79 20 6b 69 6e 64 20 6f 66   had any kind of
1684f 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
16850 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65  n, decrement the
16851 20 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63   .    ** transac
16852 74 69 6f 6e 20 63 6f 75 6e 74 20 6f 66 20 74 68  tion count of th
16853 65 20 73 68 61 72 65 64 20 62 74 72 65 65 2e 20  e shared btree. 
16854 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  If the transacti
16855 6f 6e 20 63 6f 75 6e 74 20 0a 20 20 20 20 2a 2a  on count .    **
16856 20 72 65 61 63 68 65 73 20 30 2c 20 73 65 74 20   reaches 0, set 
16857 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74 65  the shared state
16858 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 2e 20   to TRANS_NONE. 
16859 54 68 65 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49  The unlockBtreeI
1685a 66 55 6e 75 73 65 64 28 29 0a 20 20 20 20 2a 2a  fUnused().    **
1685b 20 63 61 6c 6c 20 62 65 6c 6f 77 20 77 69 6c 6c   call below will
1685c 20 75 6e 6c 6f 63 6b 20 74 68 65 20 70 61 67 65   unlock the page
1685d 72 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  r.  */.    if( p
1685e 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53  ->inTrans!=TRANS
1685f 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 63  _NONE ){.      c
16860 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63  learAllSharedCac
16861 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 29 3b  heTableLocks(p);
16862 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 61  .      pBt->nTra
16863 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20  nsaction--;.    
16864 20 20 69 66 28 20 30 3d 3d 70 42 74 2d 3e 6e 54    if( 0==pBt->nT
16865 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20  ransaction ){.  
16866 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61        pBt->inTra
16867 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53  nsaction = TRANS
16868 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20  _NONE;.      }. 
16869 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 65 74     }..    /* Set
1686a 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
1686b 6e 73 61 63 74 69 6f 6e 20 73 74 61 74 65 20 74  nsaction state t
1686c 6f 20 54 52 41 4e 53 5f 4e 4f 4e 45 20 61 6e 64  o TRANS_NONE and
1686d 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 20 20 20   unlock the .   
1686e 20 2a 2a 20 70 61 67 65 72 20 69 66 20 74 68 69   ** pager if thi
1686f 73 20 63 61 6c 6c 20 63 6c 6f 73 65 64 20 74 68  s call closed th
16870 65 20 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 77  e only read or w
16871 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
16872 2e 20 20 2a 2f 0a 20 20 20 20 70 2d 3e 69 6e 54  .  */.    p->inT
16873 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e  rans = TRANS_NON
16874 45 3b 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72  E;.    unlockBtr
16875 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b  eeIfUnused(pBt);
16876 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 49 6e 74  .  }..  btreeInt
16877 65 67 72 69 74 79 28 70 29 3b 0a 7d 0a 0a 2f 2a  egrity(p);.}../*
16878 0a 2a 2a 20 43 6f 6d 6d 69 74 20 74 68 65 20 74  .** Commit the t
16879 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 65  ransaction curre
1687a 6e 74 6c 79 20 69 6e 20 70 72 6f 67 72 65 73 73  ntly in progress
1687b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1687c 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  tine implements 
1687d 74 68 65 20 73 65 63 6f 6e 64 20 70 68 61 73 65  the second phase
1687e 20 6f 66 20 61 20 32 2d 70 68 61 73 65 20 63 6f   of a 2-phase co
1687f 6d 6d 69 74 2e 20 20 54 68 65 0a 2a 2a 20 73 71  mmit.  The.** sq
16880 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
16881 50 68 61 73 65 4f 6e 65 28 29 20 72 6f 75 74 69  PhaseOne() routi
16882 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73  ne does the firs
16883 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75  t phase and shou
16884 6c 64 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b 65 64  ld.** be invoked
16885 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e   prior to callin
16886 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  g this routine. 
16887 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 65   The sqlite3Btre
16888 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  eCommitPhaseOne(
16889 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 64 69 64  ).** routine did
1688a 20 61 6c 6c 20 74 68 65 20 77 6f 72 6b 20 6f 66   all the work of
1688b 20 77 72 69 74 69 6e 67 20 69 6e 66 6f 72 6d 61   writing informa
1688c 74 69 6f 6e 20 6f 75 74 20 74 6f 20 64 69 73 6b  tion out to disk
1688d 20 61 6e 64 20 66 6c 75 73 68 69 6e 67 20 74 68   and flushing th
1688e 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 73 6f  e.** contents so
1688f 20 74 68 61 74 20 74 68 65 79 20 61 72 65 20 77   that they are w
16890 72 69 74 74 65 6e 20 6f 6e 74 6f 20 74 68 65 20  ritten onto the 
16891 64 69 73 6b 20 70 6c 61 74 74 65 72 2e 20 20 41  disk platter.  A
16892 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  ll this.** routi
16893 6e 65 20 68 61 73 20 74 6f 20 64 6f 20 69 73 20  ne has to do is 
16894 64 65 6c 65 74 65 20 6f 72 20 74 72 75 6e 63 61  delete or trunca
16895 74 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 68  te or zero the h
16896 65 61 64 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20  eader in the.** 
16897 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
16898 72 6e 61 6c 20 28 77 68 69 63 68 20 63 61 75 73  rnal (which caus
16899 65 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  es the transacti
1689a 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 29 20 61 6e  on to commit) an
1689b 64 0a 2a 2a 20 64 72 6f 70 20 6c 6f 63 6b 73 2e  d.** drop locks.
1689c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c  .**.** This will
1689d 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 69   release the wri
1689e 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  te lock on the d
1689f 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49  atabase file.  I
168a0 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e  f there.** are n
168a1 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72 73  o active cursors
168a2 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 73  , it also releas
168a3 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b  es the read lock
168a4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
168a5 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
168a6 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54  treeCommitPhaseT
168a7 77 6f 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  wo(Btree *p){.  
168a8 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
168a9 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74  p->pBt;..  sqlit
168aa 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
168ab 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74  .  btreeIntegrit
168ac 79 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  y(p);..  /* If t
168ad 68 65 20 68 61 6e 64 6c 65 20 68 61 73 20 61 20  he handle has a 
168ae 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
168af 6e 20 6f 70 65 6e 2c 20 63 6f 6d 6d 69 74 20 74  n open, commit t
168b0 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 73  he shared-btrees
168b1 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69   .  ** transacti
168b2 6f 6e 20 61 6e 64 20 73 65 74 20 74 68 65 20 73  on and set the s
168b3 68 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54  hared state to T
168b4 52 41 4e 53 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a  RANS_READ..  */.
168b5 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73    if( p->inTrans
168b6 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b  ==TRANS_WRITE ){
168b7 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
168b8 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e   assert( pBt->in
168b9 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
168ba 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20  NS_WRITE );.    
168bb 61 73 73 65 72 74 28 20 70 42 74 2d 3e 6e 54 72  assert( pBt->nTr
168bc 61 6e 73 61 63 74 69 6f 6e 3e 30 20 29 3b 0a 20  ansaction>0 );. 
168bd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
168be 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54  agerCommitPhaseT
168bf 77 6f 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  wo(pBt->pPager);
168c0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
168c1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
168c2 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
168c3 65 28 70 29 3b 0a 20 20 20 20 20 20 72 65 74 75  e(p);.      retu
168c4 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
168c5 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
168c6 69 6f 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 44  ion = TRANS_READ
168c7 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65 65 45 6e  ;.  }..  btreeEn
168c8 64 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 29 3b  dTransaction(p);
168c9 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
168ca 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
168cb 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
168cc 2f 2a 0a 2a 2a 20 44 6f 20 62 6f 74 68 20 70 68  /*.** Do both ph
168cd 61 73 65 73 20 6f 66 20 61 20 63 6f 6d 6d 69 74  ases of a commit
168ce 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
168cf 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
168d0 74 72 65 65 43 6f 6d 6d 69 74 28 42 74 72 65 65  treeCommit(Btree
168d1 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a   *p){.  int rc;.
168d2 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
168d3 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 73  ter(p);.  rc = s
168d4 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
168d5 74 50 68 61 73 65 4f 6e 65 28 70 2c 20 30 29 3b  tPhaseOne(p, 0);
168d6 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
168d7 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
168d8 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
168d9 6d 69 74 50 68 61 73 65 54 77 6f 28 70 29 3b 0a  mitPhaseTwo(p);.
168da 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
168db 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
168dc 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e  turn rc;.}..#ifn
168dd 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a  def NDEBUG./*.**
168de 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
168df 65 72 20 6f 66 20 77 72 69 74 65 2d 63 75 72 73  er of write-curs
168e0 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69 73  ors open on this
168e1 20 68 61 6e 64 6c 65 2e 20 54 68 69 73 20 69 73   handle. This is
168e2 20 66 6f 72 20 75 73 65 0a 2a 2a 20 69 6e 20 61   for use.** in a
168e3 73 73 65 72 74 28 29 20 65 78 70 72 65 73 73 69  ssert() expressi
168e4 6f 6e 73 2c 20 73 6f 20 69 74 20 69 73 20 6f 6e  ons, so it is on
168e5 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 20 4e  ly compiled if N
168e6 44 45 42 55 47 20 69 73 20 6e 6f 74 0a 2a 2a 20  DEBUG is not.** 
168e7 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 46  defined..**.** F
168e8 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20  or the purposes 
168e9 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  of this routine,
168ea 20 61 20 77 72 69 74 65 2d 63 75 72 73 6f 72 20   a write-cursor 
168eb 69 73 20 61 6e 79 20 63 75 72 73 6f 72 20 74 68  is any cursor th
168ec 61 74 0a 2a 2a 20 69 73 20 63 61 70 61 62 6c 65  at.** is capable
168ed 20 6f 66 20 77 72 69 74 69 6e 67 20 74 6f 20 74   of writing to t
168ee 68 65 20 64 61 74 61 62 73 65 2e 20 20 54 68 61  he databse.  Tha
168ef 74 20 6d 65 61 6e 73 20 74 68 65 20 63 75 72 73  t means the curs
168f0 6f 72 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e  or was.** origin
168f1 61 6c 6c 79 20 6f 70 65 6e 65 64 20 66 6f 72 20  ally opened for 
168f2 77 72 69 74 69 6e 67 20 61 6e 64 20 74 68 65 20  writing and the 
168f3 63 75 72 73 6f 72 20 68 61 73 20 6e 6f 74 20 62  cursor has not b
168f4 65 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 62 79  e disabled.** by
168f5 20 68 61 76 69 6e 67 20 69 74 73 20 73 74 61 74   having its stat
168f6 65 20 63 68 61 6e 67 65 64 20 74 6f 20 43 55 52  e changed to CUR
168f7 53 4f 52 5f 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74  SOR_FAULT..*/.st
168f8 61 74 69 63 20 69 6e 74 20 63 6f 75 6e 74 57 72  atic int countWr
168f9 69 74 65 43 75 72 73 6f 72 73 28 42 74 53 68 61  iteCursors(BtSha
168fa 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43  red *pBt){.  BtC
168fb 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69  ursor *pCur;.  i
168fc 6e 74 20 72 20 3d 20 30 3b 0a 20 20 66 6f 72 28  nt r = 0;.  for(
168fd 70 43 75 72 3d 70 42 74 2d 3e 70 43 75 72 73 6f  pCur=pBt->pCurso
168fe 72 3b 20 70 43 75 72 3b 20 70 43 75 72 3d 70 43  r; pCur; pCur=pC
168ff 75 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  ur->pNext){.    
16900 69 66 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67  if( pCur->wrFlag
16901 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 65   && pCur->eState
16902 21 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29  !=CURSOR_FAULT )
16903 20 72 2b 2b 3b 20 0a 20 20 7d 0a 20 20 72 65 74   r++; .  }.  ret
16904 75 72 6e 20 72 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn r;.}.#endif.
16905 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
16906 69 6e 65 20 73 65 74 73 20 74 68 65 20 73 74 61  ine sets the sta
16907 74 65 20 74 6f 20 43 55 52 53 4f 52 5f 46 41 55  te to CURSOR_FAU
16908 4c 54 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72  LT and the error
16909 0a 2a 2a 20 63 6f 64 65 20 74 6f 20 65 72 72 43  .** code to errC
1690a 6f 64 65 20 66 6f 72 20 65 76 65 72 79 20 63 75  ode for every cu
1690b 72 73 6f 72 20 6f 6e 20 42 74 53 68 61 72 65 64  rsor on BtShared
1690c 20 74 68 61 74 20 70 42 74 72 65 65 0a 2a 2a 20   that pBtree.** 
1690d 72 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2a 0a 2a  references..**.*
1690e 2a 20 45 76 65 72 79 20 63 75 72 73 6f 72 20 69  * Every cursor i
1690f 73 20 74 72 69 70 70 65 64 2c 20 69 6e 63 6c 75  s tripped, inclu
16910 64 69 6e 67 20 63 75 72 73 6f 72 73 20 74 68 61  ding cursors tha
16911 74 20 62 65 6c 6f 6e 67 0a 2a 2a 20 74 6f 20 6f  t belong.** to o
16912 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63 6f  ther database co
16913 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20 68  nnections that h
16914 61 70 70 65 6e 20 74 6f 20 62 65 20 73 68 61 72  appen to be shar
16915 69 6e 67 0a 2a 2a 20 74 68 65 20 63 61 63 68 65  ing.** the cache
16916 20 77 69 74 68 20 70 42 74 72 65 65 2e 0a 2a 2a   with pBtree..**
16917 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
16918 20 67 65 74 73 20 63 61 6c 6c 65 64 20 77 68 65   gets called whe
16919 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63  n a rollback occ
1691a 75 72 73 2e 0a 2a 2a 20 41 6c 6c 20 63 75 72 73  urs..** All curs
1691b 6f 72 73 20 75 73 69 6e 67 20 74 68 65 20 73 61  ors using the sa
1691c 6d 65 20 63 61 63 68 65 20 6d 75 73 74 20 62 65  me cache must be
1691d 20 74 72 69 70 70 65 64 0a 2a 2a 20 74 6f 20 70   tripped.** to p
1691e 72 65 76 65 6e 74 20 74 68 65 6d 20 66 72 6f 6d  revent them from
1691f 20 74 72 79 69 6e 67 20 74 6f 20 75 73 65 20 74   trying to use t
16920 68 65 20 62 74 72 65 65 20 61 66 74 65 72 0a 2a  he btree after.*
16921 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 20  * the rollback. 
16922 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d 61   The rollback ma
16923 79 20 68 61 76 65 20 64 65 6c 65 74 65 64 20 74  y have deleted t
16924 61 62 6c 65 73 0a 2a 2a 20 6f 72 20 6d 6f 76 65  ables.** or move
16925 64 20 72 6f 6f 74 20 70 61 67 65 73 2c 20 73 6f  d root pages, so
16926 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66 66 69   it is not suffi
16927 63 69 65 6e 74 20 74 6f 0a 2a 2a 20 73 61 76 65  cient to.** save
16928 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
16929 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63  e cursor.  The c
1692a 75 72 73 6f 72 20 6d 75 73 74 20 62 65 0a 2a 2a  ursor must be.**
1692b 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f   invalidated..*/
1692c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1692d 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
1692e 65 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28  eTripAllCursors(
1692f 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69  Btree *pBtree, i
16930 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20 42  nt errCode){.  B
16931 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 73 71  tCursor *p;.  sq
16932 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
16933 70 42 74 72 65 65 29 3b 0a 20 20 66 6f 72 28 70  pBtree);.  for(p
16934 3d 70 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43  =pBtree->pBt->pC
16935 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70  ursor; p; p=p->p
16936 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 74 20 69  Next){.    int i
16937 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
16938 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 29  eeClearCursor(p)
16939 3b 0a 20 20 20 20 70 2d 3e 65 53 74 61 74 65 20  ;.    p->eState 
1693a 3d 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 3b 0a  = CURSOR_FAULT;.
1693b 20 20 20 20 70 2d 3e 73 6b 69 70 4e 65 78 74 20      p->skipNext 
1693c 3d 20 65 72 72 43 6f 64 65 3b 0a 20 20 20 20 66  = errCode;.    f
1693d 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 2d 3e 69 50  or(i=0; i<=p->iP
1693e 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  age; i++){.     
1693f 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 2d 3e   releasePage(p->
16940 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20  apPage[i]);.    
16941 20 20 70 2d 3e 61 70 50 61 67 65 5b 69 5d 20 3d    p->apPage[i] =
16942 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
16943 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
16944 65 28 70 42 74 72 65 65 29 3b 0a 7d 0a 0a 2f 2a  e(pBtree);.}../*
16945 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 74 68 65  .** Rollback the
16946 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20   transaction in 
16947 70 72 6f 67 72 65 73 73 2e 20 20 41 6c 6c 20 63  progress.  All c
16948 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 0a 2a  ursors will be.*
16949 2a 20 69 6e 76 61 6c 69 64 65 64 20 62 79 20 74  * invalided by t
1694a 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  his operation.  
1694b 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75  Any attempt to u
1694c 73 65 20 61 20 63 75 72 73 6f 72 0a 2a 2a 20 74  se a cursor.** t
1694d 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 74 20  hat was open at 
1694e 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
1694f 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
16950 77 69 6c 6c 20 72 65 73 75 6c 74 0a 2a 2a 20 69  will result.** i
16951 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a  n an error..**.*
16952 2a 20 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65  * This will rele
16953 61 73 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f  ase the write lo
16954 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
16955 73 65 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65  se file.  If the
16956 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74  re.** are no act
16957 69 76 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20  ive cursors, it 
16958 61 6c 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68  also releases th
16959 65 20 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a  e read lock..*/.
1695a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1695b 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52  nt sqlite3BtreeR
1695c 6f 6c 6c 62 61 63 6b 28 42 74 72 65 65 20 2a 70  ollback(Btree *p
1695d 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  ){.  int rc;.  B
1695e 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
1695f 2d 3e 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65  ->pBt;.  MemPage
16960 20 2a 70 50 61 67 65 31 3b 0a 0a 20 20 73 71 6c   *pPage1;..  sql
16961 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
16962 29 3b 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c  );.  rc = saveAl
16963 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 30 2c  lCursors(pBt, 0,
16964 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c   0);.#ifndef SQL
16965 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
16966 43 41 43 48 45 0a 20 20 69 66 28 20 72 63 21 3d  CACHE.  if( rc!=
16967 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16968 20 2f 2a 20 54 68 69 73 20 69 73 20 61 20 68 6f   /* This is a ho
16969 72 72 69 62 6c 65 20 73 69 74 75 61 74 69 6f 6e  rrible situation
1696a 2e 20 41 6e 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f  . An IO or mallo
1696b 63 28 29 20 65 72 72 6f 72 20 6f 63 63 75 72 72  c() error occurr
1696c 65 64 20 77 68 69 6c 73 74 0a 20 20 20 20 2a 2a  ed whilst.    **
1696d 20 74 72 79 69 6e 67 20 74 6f 20 73 61 76 65 20   trying to save 
1696e 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 73  cursor positions
1696f 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  . If this is an 
16970 61 75 74 6f 6d 61 74 69 63 20 72 6f 6c 6c 62 61  automatic rollba
16971 63 6b 20 28 61 73 0a 20 20 20 20 2a 2a 20 74 68  ck (as.    ** th
16972 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 63 6f  e result of a co
16973 6e 73 74 72 61 69 6e 74 2c 20 6d 61 6c 6c 6f 63  nstraint, malloc
16974 28 29 20 66 61 69 6c 75 72 65 20 6f 72 20 49 4f  () failure or IO
16975 20 65 72 72 6f 72 29 20 74 68 65 6e 20 0a 20 20   error) then .  
16976 20 20 2a 2a 20 74 68 65 20 63 61 63 68 65 20 6d    ** the cache m
16977 61 79 20 62 65 20 69 6e 74 65 72 6e 61 6c 6c 79  ay be internally
16978 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 28 6e   inconsistent (n
16979 6f 74 20 63 6f 6e 74 61 69 6e 20 76 61 6c 69 64  ot contain valid
1697a 20 74 72 65 65 73 29 20 73 6f 0a 20 20 20 20 2a   trees) so.    *
1697b 2a 20 77 65 20 63 61 6e 6e 6f 74 20 73 69 6d 70  * we cannot simp
1697c 6c 79 20 72 65 74 75 72 6e 20 74 68 65 20 65 72  ly return the er
1697d 72 6f 72 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ror to the calle
1697e 72 2e 20 49 6e 73 74 65 61 64 2c 20 61 62 6f 72  r. Instead, abor
1697f 74 20 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 71 75  t .    ** all qu
16980 65 72 69 65 73 20 74 68 61 74 20 6d 61 79 20 62  eries that may b
16981 65 20 75 73 69 6e 67 20 61 6e 79 20 6f 66 20 74  e using any of t
16982 68 65 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  he cursors that 
16983 66 61 69 6c 65 64 20 74 6f 20 73 61 76 65 2e 0a  failed to save..
16984 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
16985 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75  e3BtreeTripAllCu
16986 72 73 6f 72 73 28 70 2c 20 72 63 29 3b 0a 20 20  rsors(p, rc);.  
16987 7d 0a 23 65 6e 64 69 66 0a 20 20 62 74 72 65 65  }.#endif.  btree
16988 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20  Integrity(p);.. 
16989 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d   if( p->inTrans=
1698a 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a  =TRANS_WRITE ){.
1698b 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 20 20      int rc2;..  
1698c 20 20 61 73 73 65 72 74 28 20 54 52 41 4e 53 5f    assert( TRANS_
1698d 57 52 49 54 45 3d 3d 70 42 74 2d 3e 69 6e 54 72  WRITE==pBt->inTr
1698e 61 6e 73 61 63 74 69 6f 6e 20 29 3b 0a 20 20 20  ansaction );.   
1698f 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 50 61   rc2 = sqlite3Pa
16990 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d  gerRollback(pBt-
16991 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66  >pPager);.    if
16992 28 20 72 63 32 21 3d 53 51 4c 49 54 45 5f 4f 4b  ( rc2!=SQLITE_OK
16993 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72   ){.      rc = r
16994 63 32 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  c2;.    }..    /
16995 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6d  * The rollback m
16996 61 79 20 68 61 76 65 20 64 65 73 74 72 6f 79 65  ay have destroye
16997 64 20 74 68 65 20 70 50 61 67 65 31 2d 3e 61 44  d the pPage1->aD
16998 61 74 61 20 76 61 6c 75 65 2e 20 20 53 6f 0a 20  ata value.  So. 
16999 20 20 20 2a 2a 20 63 61 6c 6c 20 62 74 72 65 65     ** call btree
1699a 47 65 74 50 61 67 65 28 29 20 6f 6e 20 70 61 67  GetPage() on pag
1699b 65 20 31 20 61 67 61 69 6e 20 74 6f 20 6d 61 6b  e 1 again to mak
1699c 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 70 50  e.    ** sure pP
1699d 61 67 65 31 2d 3e 61 44 61 74 61 20 69 73 20 73  age1->aData is s
1699e 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20 2a 2f  et correctly. */
1699f 0a 20 20 20 20 69 66 28 20 62 74 72 65 65 47 65  .    if( btreeGe
169a0 74 50 61 67 65 28 70 42 74 2c 20 31 2c 20 26 70  tPage(pBt, 1, &p
169a1 50 61 67 65 31 2c 20 30 29 3d 3d 53 51 4c 49 54  Page1, 0)==SQLIT
169a2 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
169a3 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31  leasePage(pPage1
169a4 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  );.    }.    ass
169a5 65 72 74 28 20 63 6f 75 6e 74 57 72 69 74 65 43  ert( countWriteC
169a6 75 72 73 6f 72 73 28 70 42 74 29 3d 3d 30 20 29  ursors(pBt)==0 )
169a7 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61  ;.    pBt->inTra
169a8 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53  nsaction = TRANS
169a9 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74  _READ;.  }..  bt
169aa 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 6f  reeEndTransactio
169ab 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  n(p);.  sqlite3B
169ac 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
169ad 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
169ae 0a 2a 2a 20 53 74 61 72 74 20 61 20 73 74 61 74  .** Start a stat
169af 65 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63  ement subtransac
169b0 74 69 6f 6e 2e 20 54 68 65 20 73 75 62 74 72 61  tion. The subtra
169b1 6e 73 61 63 74 69 6f 6e 20 63 61 6e 20 63 61 6e  nsaction can can
169b2 20 62 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61   be rolled.** ba
169b3 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79  ck independently
169b4 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61   of the main tra
169b5 6e 73 61 63 74 69 6f 6e 2e 20 59 6f 75 20 6d 75  nsaction. You mu
169b6 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e 73  st start a trans
169b7 61 63 74 69 6f 6e 20 0a 2a 2a 20 62 65 66 6f 72  action .** befor
169b8 65 20 73 74 61 72 74 69 6e 67 20 61 20 73 75 62  e starting a sub
169b9 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65  transaction. The
169ba 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20   subtransaction 
169bb 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61 74  is ended automat
169bc 69 63 61 6c 6c 79 20 0a 2a 2a 20 69 66 20 74 68  ically .** if th
169bd 65 20 6d 61 69 6e 20 74 72 61 6e 73 61 63 74 69  e main transacti
169be 6f 6e 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f  on commits or ro
169bf 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  lls back..**.** 
169c0 53 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61  Statement subtra
169c1 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20 75 73  nsactions are us
169c2 65 64 20 61 72 6f 75 6e 64 20 69 6e 64 69 76 69  ed around indivi
169c3 64 75 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65  dual SQL stateme
169c4 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  nts.** that are 
169c5 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e  contained within
169c6 20 61 20 42 45 47 49 4e 2e 2e 2e 43 4f 4d 4d 49   a BEGIN...COMMI
169c7 54 20 62 6c 6f 63 6b 2e 20 20 49 66 20 61 20 63  T block.  If a c
169c8 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72  onstraint.** err
169c9 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  or occurs within
169ca 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20   the statement, 
169cb 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 74 68  the effect of th
169cc 61 74 20 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74  at one statement
169cd 0a 2a 2a 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65  .** can be rolle
169ce 64 20 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68  d back without h
169cf 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61 63  aving to rollbac
169d0 6b 20 74 68 65 20 65 6e 74 69 72 65 20 74 72 61  k the entire tra
169d1 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nsaction..**.** 
169d2 41 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 2d  A statement sub-
169d3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 69  transaction is i
169d4 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 6e  mplemented as an
169d5 20 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70   anonymous savep
169d6 6f 69 6e 74 2e 20 54 68 65 0a 2a 2a 20 76 61 6c  oint. The.** val
169d7 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ue passed as the
169d8 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
169d9 72 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e  r is the total n
169da 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f 69  umber of savepoi
169db 6e 74 73 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e  nts,.** includin
169dc 67 20 74 68 65 20 6e 65 77 20 61 6e 6f 6e 79 6d  g the new anonym
169dd 6f 75 73 20 73 61 76 65 70 6f 69 6e 74 2c 20 6f  ous savepoint, o
169de 70 65 6e 20 6f 6e 20 74 68 65 20 42 2d 54 72 65  pen on the B-Tre
169df 65 2e 20 69 2e 65 2e 20 69 66 20 74 68 65 72 65  e. i.e. if there
169e0 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76  .** are no activ
169e1 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61 6e 64  e savepoints and
169e2 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74 65 6d   no other statem
169e3 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73  ent-transactions
169e4 20 6f 70 65 6e 2c 0a 2a 2a 20 69 53 74 61 74 65   open,.** iState
169e5 6d 65 6e 74 20 69 73 20 31 2e 20 54 68 69 73 20  ment is 1. This 
169e6 61 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f  anonymous savepo
169e7 69 6e 74 20 63 61 6e 20 62 65 20 72 65 6c 65 61  int can be relea
169e8 73 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61  sed or rolled ba
169e9 63 6b 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  ck.** using the 
169ea 73 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65  sqlite3BtreeSave
169eb 70 6f 69 6e 74 28 29 20 66 75 6e 63 74 69 6f 6e  point() function
169ec 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
169ed 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
169ee 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 42 74  treeBeginStmt(Bt
169ef 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 53 74 61  ree *p, int iSta
169f0 74 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 72  tement){.  int r
169f1 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  c;.  BtShared *p
169f2 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73  Bt = p->pBt;.  s
169f3 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
169f4 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  (p);.  assert( p
169f5 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
169f6 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65  _WRITE );.  asse
169f7 72 74 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c  rt( pBt->readOnl
169f8 79 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  y==0 );.  assert
169f9 28 20 69 53 74 61 74 65 6d 65 6e 74 3e 30 20 29  ( iStatement>0 )
169fa 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 61  ;.  assert( iSta
169fb 74 65 6d 65 6e 74 3e 70 2d 3e 64 62 2d 3e 6e 53  tement>p->db->nS
169fc 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20 69 66  avepoint );.  if
169fd 28 20 4e 45 56 45 52 28 70 2d 3e 69 6e 54 72 61  ( NEVER(p->inTra
169fe 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns!=TRANS_WRITE 
169ff 7c 7c 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79  || pBt->readOnly
16a00 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  ) ){.    rc = SQ
16a01 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 3b 0a 20  LITE_INTERNAL;. 
16a02 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
16a03 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73  rt( pBt->inTrans
16a04 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
16a05 49 54 45 20 29 3b 0a 20 20 20 20 2f 2a 20 41 74  ITE );.    /* At
16a06 20 74 68 65 20 70 61 67 65 72 20 6c 65 76 65 6c   the pager level
16a07 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72  , a statement tr
16a08 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 20 73  ansaction is a s
16a09 61 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 20 20  avepoint with.  
16a0a 20 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 67 72    ** an index gr
16a0b 65 61 74 65 72 20 74 68 61 6e 20 61 6c 6c 20 73  eater than all s
16a0c 61 76 65 70 6f 69 6e 74 73 20 63 72 65 61 74 65  avepoints create
16a0d 64 20 65 78 70 6c 69 63 69 74 6c 79 20 75 73 69  d explicitly usi
16a0e 6e 67 0a 20 20 20 20 2a 2a 20 53 51 4c 20 73 74  ng.    ** SQL st
16a0f 61 74 65 6d 65 6e 74 73 2e 20 49 74 20 69 73 20  atements. It is 
16a10 69 6c 6c 65 67 61 6c 20 74 6f 20 6f 70 65 6e 2c  illegal to open,
16a11 20 72 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c   release or roll
16a12 62 61 63 6b 20 61 6e 79 0a 20 20 20 20 2a 2a 20  back any.    ** 
16a13 73 75 63 68 20 73 61 76 65 70 6f 69 6e 74 73 20  such savepoints 
16a14 77 68 69 6c 65 20 74 68 65 20 73 74 61 74 65 6d  while the statem
16a15 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
16a16 73 61 76 65 70 6f 69 6e 74 20 69 73 20 61 63 74  savepoint is act
16a17 69 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ive..    */.    
16a18 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
16a19 72 4f 70 65 6e 53 61 76 65 70 6f 69 6e 74 28 70  rOpenSavepoint(p
16a1a 42 74 2d 3e 70 50 61 67 65 72 2c 20 69 53 74 61  Bt->pPager, iSta
16a1b 74 65 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 73  tement);.  }.  s
16a1c 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
16a1d 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  (p);.  return rc
16a1e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
16a1f 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
16a20 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c  o this function,
16a21 20 6f 70 2c 20 69 73 20 61 6c 77 61 79 73 20 53   op, is always S
16a22 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
16a23 4b 0a 2a 2a 20 6f 72 20 53 41 56 45 50 4f 49 4e  K.** or SAVEPOIN
16a24 54 5f 52 45 4c 45 41 53 45 2e 20 54 68 69 73 20  T_RELEASE. This 
16a25 66 75 6e 63 74 69 6f 6e 20 65 69 74 68 65 72 20  function either 
16a26 72 65 6c 65 61 73 65 73 20 6f 72 20 72 6f 6c 6c  releases or roll
16a27 73 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 73 61  s back the.** sa
16a28 76 65 70 6f 69 6e 74 20 69 64 65 6e 74 69 66 69  vepoint identifi
16a29 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ed by parameter 
16a2a 69 53 61 76 65 70 6f 69 6e 74 2c 20 64 65 70 65  iSavepoint, depe
16a2b 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c  nding on the val
16a2c 75 65 20 0a 2a 2a 20 6f 66 20 6f 70 2e 0a 2a 2a  ue .** of op..**
16a2d 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 53  .** Normally, iS
16a2e 61 76 65 70 6f 69 6e 74 20 69 73 20 67 72 65 61  avepoint is grea
16a2f 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ter than or equa
16a30 6c 20 74 6f 20 7a 65 72 6f 2e 20 48 6f 77 65 76  l to zero. Howev
16a31 65 72 2c 20 69 66 20 6f 70 20 69 73 0a 2a 2a 20  er, if op is.** 
16a32 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
16a33 43 4b 2c 20 74 68 65 6e 20 69 53 61 76 65 70 6f  CK, then iSavepo
16a34 69 6e 74 20 6d 61 79 20 61 6c 73 6f 20 62 65 20  int may also be 
16a35 2d 31 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  -1. In this case
16a36 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65 6e 74   the .** content
16a37 73 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20  s of the entire 
16a38 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 72 65 20  transaction are 
16a39 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 69  rolled back. Thi
16a3a 73 20 69 73 20 64 69 66 66 65 72 65 6e 74 0a 2a  s is different.*
16a3b 2a 20 66 72 6f 6d 20 61 20 6e 6f 72 6d 61 6c 20  * from a normal 
16a3c 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c  transaction roll
16a3d 62 61 63 6b 2c 20 61 73 20 6e 6f 20 6c 6f 63 6b  back, as no lock
16a3e 73 20 61 72 65 20 72 65 6c 65 61 73 65 64 20 61  s are released a
16a3f 6e 64 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61  nd the.** transa
16a40 63 74 69 6f 6e 20 72 65 6d 61 69 6e 73 20 6f 70  ction remains op
16a41 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  en..*/.SQLITE_PR
16a42 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
16a43 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74 28  3BtreeSavepoint(
16a44 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 6f 70  Btree *p, int op
16a45 2c 20 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74  , int iSavepoint
16a46 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
16a47 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70  LITE_OK;.  if( p
16a48 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d   && p->inTrans==
16a49 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20  TRANS_WRITE ){. 
16a4a 20 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74     BtShared *pBt
16a4b 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 61   = p->pBt;.    a
16a4c 73 73 65 72 74 28 20 6f 70 3d 3d 53 41 56 45 50  ssert( op==SAVEP
16a4d 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 7c 7c 20  OINT_RELEASE || 
16a4e 6f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  op==SAVEPOINT_RO
16a4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20 20 20 61 73  LLBACK );.    as
16a50 73 65 72 74 28 20 69 53 61 76 65 70 6f 69 6e 74  sert( iSavepoint
16a51 3e 3d 30 20 7c 7c 20 28 69 53 61 76 65 70 6f 69  >=0 || (iSavepoi
16a52 6e 74 3d 3d 2d 31 20 26 26 20 6f 70 3d 3d 53 41  nt==-1 && op==SA
16a53 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
16a54 29 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) );.    sqlite3
16a55 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
16a56 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
16a57 61 67 65 72 53 61 76 65 70 6f 69 6e 74 28 70 42  agerSavepoint(pB
16a58 74 2d 3e 70 50 61 67 65 72 2c 20 6f 70 2c 20 69  t->pPager, op, i
16a59 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20  Savepoint);.    
16a5a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16a5b 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
16a5c 6e 65 77 44 61 74 61 62 61 73 65 28 70 42 74 29  newDatabase(pBt)
16a5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
16a5e 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
16a5f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
16a60 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  c;.}../*.** Crea
16a61 74 65 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20  te a new cursor 
16a62 66 6f 72 20 74 68 65 20 42 54 72 65 65 20 77 68  for the BTree wh
16a63 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f 6e 20 74  ose root is on t
16a64 68 65 20 70 61 67 65 0a 2a 2a 20 69 54 61 62 6c  he page.** iTabl
16a65 65 2e 20 49 66 20 61 20 72 65 61 64 2d 6f 6e 6c  e. If a read-onl
16a66 79 20 63 75 72 73 6f 72 20 69 73 20 72 65 71 75  y cursor is requ
16a67 65 73 74 65 64 2c 20 69 74 20 69 73 20 61 73 73  ested, it is ass
16a68 75 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 65  umed that.** the
16a69 20 63 61 6c 6c 65 72 20 61 6c 72 65 61 64 79 20   caller already 
16a6a 68 61 73 20 61 74 20 6c 65 61 73 74 20 61 20 72  has at least a r
16a6b 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e 73 61 63  ead-only transac
16a6c 74 69 6f 6e 20 6f 70 65 6e 0a 2a 2a 20 6f 6e 20  tion open.** on 
16a6d 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6c 72  the database alr
16a6e 65 61 64 79 2e 20 49 66 20 61 20 77 72 69 74 65  eady. If a write
16a6f 2d 63 75 72 73 6f 72 20 69 73 20 72 65 71 75 65  -cursor is reque
16a70 73 74 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  sted, then.** th
16a71 65 20 63 61 6c 6c 65 72 20 69 73 20 61 73 73 75  e caller is assu
16a72 6d 65 64 20 74 6f 20 68 61 76 65 20 61 6e 20 6f  med to have an o
16a73 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73 61  pen write transa
16a74 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ction..**.** If 
16a75 77 72 46 6c 61 67 3d 3d 30 2c 20 74 68 65 6e 20  wrFlag==0, then 
16a76 74 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 6f  the cursor can o
16a77 6e 6c 79 20 62 65 20 75 73 65 64 20 66 6f 72 20  nly be used for 
16a78 72 65 61 64 69 6e 67 2e 0a 2a 2a 20 49 66 20 77  reading..** If w
16a79 72 46 6c 61 67 3d 3d 31 2c 20 74 68 65 6e 20 74  rFlag==1, then t
16a7a 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65  he cursor can be
16a7b 20 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e   used for readin
16a7c 67 20 6f 72 20 66 6f 72 0a 2a 2a 20 77 72 69 74  g or for.** writ
16a7d 69 6e 67 20 69 66 20 6f 74 68 65 72 20 63 6f 6e  ing if other con
16a7e 64 69 74 69 6f 6e 73 20 66 6f 72 20 77 72 69 74  ditions for writ
16a7f 69 6e 67 20 61 72 65 20 61 6c 73 6f 20 6d 65 74  ing are also met
16a80 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 20  .  These.** are 
16a81 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 74  the conditions t
16a82 68 61 74 20 6d 75 73 74 20 62 65 20 6d 65 74 20  hat must be met 
16a83 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 77 72 69  in order for wri
16a84 74 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 61 6c  ting to.** be al
16a85 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20  lowed:.**.** 1: 
16a86 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74   The cursor must
16a87 20 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65   have been opene
16a88 64 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d 31  d with wrFlag==1
16a89 0a 2a 2a 0a 2a 2a 20 32 3a 20 20 4f 74 68 65 72  .**.** 2:  Other
16a8a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
16a8b 74 69 6f 6e 73 20 74 68 61 74 20 73 68 61 72 65  tions that share
16a8c 20 74 68 65 20 73 61 6d 65 20 70 61 67 65 72 20   the same pager 
16a8d 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 62 75 74  cache.**     but
16a8e 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 69   which are not i
16a8f 6e 20 74 68 65 20 52 45 41 44 5f 55 4e 43 4f 4d  n the READ_UNCOM
16a90 4d 49 54 54 45 44 20 73 74 61 74 65 20 6d 61 79  MITTED state may
16a91 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20 20   not have.**    
16a92 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 77 69   cursors open wi
16a93 74 68 20 77 72 46 6c 61 67 3d 3d 30 20 6f 6e 20  th wrFlag==0 on 
16a94 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20  the same table. 
16a95 20 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 20 20   Otherwise.**   
16a96 20 20 74 68 65 20 63 68 61 6e 67 65 73 20 6d 61    the changes ma
16a97 64 65 20 62 79 20 74 68 69 73 20 77 72 69 74 65  de by this write
16a98 20 63 75 72 73 6f 72 20 77 6f 75 6c 64 20 62 65   cursor would be
16a99 20 76 69 73 69 62 6c 65 20 74 6f 0a 2a 2a 20 20   visible to.**  
16a9a 20 20 20 74 68 65 20 72 65 61 64 20 63 75 72 73     the read curs
16a9b 6f 72 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72  ors in the other
16a9c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
16a9d 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 33 3a 20 20  tion..**.** 3:  
16a9e 54 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73  The database mus
16a9f 74 20 62 65 20 77 72 69 74 61 62 6c 65 20 28 6e  t be writable (n
16aa0 6f 74 20 6f 6e 20 72 65 61 64 2d 6f 6e 6c 79 20  ot on read-only 
16aa1 6d 65 64 69 61 29 0a 2a 2a 0a 2a 2a 20 34 3a 20  media).**.** 4: 
16aa2 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   There must be a
16aa3 6e 20 61 63 74 69 76 65 20 74 72 61 6e 73 61 63  n active transac
16aa4 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20 63  tion..**.** No c
16aa5 68 65 63 6b 69 6e 67 20 69 73 20 64 6f 6e 65 20  hecking is done 
16aa6 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
16aa7 74 20 70 61 67 65 20 69 54 61 62 6c 65 20 72 65  t page iTable re
16aa8 61 6c 6c 79 20 69 73 20 74 68 65 0a 2a 2a 20 72  ally is the.** r
16aa9 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62 2d  oot page of a b-
16aaa 74 72 65 65 2e 20 20 49 66 20 69 74 20 69 73 20  tree.  If it is 
16aab 6e 6f 74 2c 20 74 68 65 6e 20 74 68 65 20 63 75  not, then the cu
16aac 72 73 6f 72 20 61 63 71 75 69 72 65 64 0a 2a 2a  rsor acquired.**
16aad 20 77 69 6c 6c 20 6e 6f 74 20 77 6f 72 6b 20 63   will not work c
16aae 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20  orrectly..**.** 
16aaf 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68  It is assumed th
16ab0 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 42 74  at the sqlite3Bt
16ab1 72 65 65 43 75 72 73 6f 72 5a 65 72 6f 28 29 20  reeCursorZero() 
16ab2 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 0a  has been called.
16ab3 2a 2a 20 6f 6e 20 70 43 75 72 20 74 6f 20 69 6e  ** on pCur to in
16ab4 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d  itialize the mem
16ab5 6f 72 79 20 73 70 61 63 65 20 70 72 69 6f 72 20  ory space prior 
16ab6 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73  to invoking this
16ab7 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61   routine..*/.sta
16ab8 74 69 63 20 69 6e 74 20 62 74 72 65 65 43 75 72  tic int btreeCur
16ab9 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c  sor(.  Btree *p,
16aba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16abb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16abc 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a 20 20   The btree */.  
16abd 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20 20 20  int iTable,     
16abe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16abf 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70         /* Root p
16ac0 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74 6f 20  age of table to 
16ac1 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 77 72  open */.  int wr
16ac2 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 20  Flag,           
16ac3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ac4 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20   /* 1 to write. 
16ac5 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20  0 read-only */. 
16ac6 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20   struct KeyInfo 
16ac7 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20  *pKeyInfo,      
16ac8 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
16ac9 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 72 69 73   arg to comparis
16aca 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  on function */. 
16acb 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20   BtCursor *pCur 
16acc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16acd 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
16ace 20 66 6f 72 20 6e 65 77 20 63 75 72 73 6f 72 20   for new cursor 
16acf 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72 65 64  */.){.  BtShared
16ad0 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 20   *pBt = p->pBt; 
16ad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16ad2 2a 20 53 68 61 72 65 64 20 62 2d 74 72 65 65 20  * Shared b-tree 
16ad3 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 61 73 73  handle */..  ass
16ad4 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
16ad5 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29 20 29  eHoldsMutex(p) )
16ad6 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c  ;.  assert( wrFl
16ad7 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c 61 67 3d  ag==0 || wrFlag=
16ad8 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  =1 );..  /* The 
16ad9 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
16ada 20 73 74 61 74 65 6d 65 6e 74 73 20 76 65 72 69   statements veri
16adb 66 79 20 74 68 61 74 20 69 66 20 74 68 69 73 20  fy that if this 
16adc 69 73 20 61 20 73 68 61 72 61 62 6c 65 20 0a 20  is a sharable . 
16add 20 2a 2a 20 62 2d 74 72 65 65 20 64 61 74 61 62   ** b-tree datab
16ade 61 73 65 2c 20 74 68 65 20 63 6f 6e 6e 65 63 74  ase, the connect
16adf 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67 20 74  ion is holding t
16ae0 68 65 20 72 65 71 75 69 72 65 64 20 74 61 62 6c  he required tabl
16ae1 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a 2a 20 61  e locks, .  ** a
16ae2 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  nd that no other
16ae3 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 73 20   connection has 
16ae4 61 6e 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 20  any open cursor 
16ae5 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73 20 77  that conflicts w
16ae6 69 74 68 20 0a 20 20 2a 2a 20 74 68 69 73 20 6c  ith .  ** this l
16ae7 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72  ock.  */.  asser
16ae8 74 28 20 68 61 73 53 68 61 72 65 64 43 61 63 68  t( hasSharedCach
16ae9 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54  eTableLock(p, iT
16aea 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66 6f 21 3d  able, pKeyInfo!=
16aeb 30 2c 20 77 72 46 6c 61 67 2b 31 29 20 29 3b 0a  0, wrFlag+1) );.
16aec 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67    assert( wrFlag
16aed 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65 61 64 43  ==0 || !hasReadC
16aee 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 54 61 62  onflicts(p, iTab
16aef 6c 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73  le) );..  /* Ass
16af0 65 72 74 20 74 68 61 74 20 74 68 65 20 63 61 6c  ert that the cal
16af1 6c 65 72 20 68 61 73 20 6f 70 65 6e 65 64 20 74  ler has opened t
16af2 68 65 20 72 65 71 75 69 72 65 64 20 74 72 61 6e  he required tran
16af3 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 61 73  saction. */.  as
16af4 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73  sert( p->inTrans
16af5 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20  >TRANS_NONE );. 
16af6 20 61 73 73 65 72 74 28 20 77 72 46 6c 61 67 3d   assert( wrFlag=
16af7 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 61 6e 73  =0 || p->inTrans
16af8 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
16af9 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
16afa 70 50 61 67 65 31 20 26 26 20 70 42 74 2d 3e 70  pPage1 && pBt->p
16afb 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a  Page1->aData );.
16afc 0a 20 20 69 66 28 20 4e 45 56 45 52 28 77 72 46  .  if( NEVER(wrF
16afd 6c 61 67 20 26 26 20 70 42 74 2d 3e 72 65 61 64  lag && pBt->read
16afe 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 65 74  Only) ){.    ret
16aff 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
16b00 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  NLY;.  }.  if( i
16b01 54 61 62 6c 65 3d 3d 31 20 26 26 20 70 61 67 65  Table==1 && page
16b02 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3d  rPagecount(pBt)=
16b03 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
16b04 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b 0a 20   SQLITE_EMPTY;. 
16b05 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74 68 61   }..  /* Now tha
16b06 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 72 6f 72  t no other error
16b07 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 66 69 6e  s can occur, fin
16b08 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74  ish filling in t
16b09 68 65 20 42 74 43 75 72 73 6f 72 0a 20 20 2a 2a  he BtCursor.  **
16b0a 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 6c   variables and l
16b0b 69 6e 6b 20 74 68 65 20 63 75 72 73 6f 72 20 69  ink the cursor i
16b0c 6e 74 6f 20 74 68 65 20 42 74 53 68 61 72 65 64  nto the BtShared
16b0d 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20 70 43 75   list.  */.  pCu
16b0e 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 50  r->pgnoRoot = (P
16b0f 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 20 70 43  gno)iTable;.  pC
16b10 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31 3b 0a  ur->iPage = -1;.
16b11 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f    pCur->pKeyInfo
16b12 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70   = pKeyInfo;.  p
16b13 43 75 72 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b  Cur->pBtree = p;
16b14 0a 20 20 70 43 75 72 2d 3e 70 42 74 20 3d 20 70  .  pCur->pBt = p
16b15 42 74 3b 0a 20 20 70 43 75 72 2d 3e 77 72 46 6c  Bt;.  pCur->wrFl
16b16 61 67 20 3d 20 28 75 38 29 77 72 46 6c 61 67 3b  ag = (u8)wrFlag;
16b17 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 3d  .  pCur->pNext =
16b18 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 0a 20   pBt->pCursor;. 
16b19 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65 78 74   if( pCur->pNext
16b1a 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70 4e   ){.    pCur->pN
16b1b 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75  ext->pPrev = pCu
16b1c 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e 70 43  r;.  }.  pBt->pC
16b1d 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a 20 20  ursor = pCur;.  
16b1e 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43  pCur->eState = C
16b1f 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20  URSOR_INVALID;. 
16b20 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 6f 77   pCur->cachedRow
16b21 69 64 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  id = 0;.  return
16b22 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51   SQLITE_OK;.}.SQ
16b23 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
16b24 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
16b25 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a 70 2c  sor(.  Btree *p,
16b26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b28 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20     /* The btree 
16b29 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c  */.  int iTable,
16b2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b2c 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66   /* Root page of
16b2d 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a   table to open *
16b2e 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 2c 20  /.  int wrFlag, 
16b2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b31 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e 20 30  /* 1 to write. 0
16b32 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20   read-only */.  
16b33 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a  struct KeyInfo *
16b34 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 20  pKeyInfo,       
16b35 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
16b36 69 72 73 74 20 61 72 67 20 74 6f 20 78 43 6f 6d  irst arg to xCom
16b37 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74 43 75  pare() */.  BtCu
16b38 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20 20 20  rsor *pCur      
16b39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16b3a 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
16b3b 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65 72 65   new cursor here
16b3c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
16b3d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
16b3e 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20  nter(p);.  rc = 
16b3f 62 74 72 65 65 43 75 72 73 6f 72 28 70 2c 20 69  btreeCursor(p, i
16b40 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c 20 70  Table, wrFlag, p
16b41 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 29 3b 0a  KeyInfo, pCur);.
16b42 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
16b43 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
16b44 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
16b45 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f 66  turn the size of
16b46 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65   a BtCursor obje
16b47 63 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2a 0a  ct in bytes..**.
16b48 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63  ** This interfac
16b49 65 73 20 69 73 20 6e 65 65 64 65 64 20 73 6f 20  es is needed so 
16b4a 74 68 61 74 20 75 73 65 72 73 20 6f 66 20 63 75  that users of cu
16b4b 72 73 6f 72 73 20 63 61 6e 20 70 72 65 61 6c 6c  rsors can preall
16b4c 6f 63 61 74 65 0a 2a 2a 20 73 75 66 66 69 63 69  ocate.** suffici
16b4d 65 6e 74 20 73 74 6f 72 61 67 65 20 74 6f 20 68  ent storage to h
16b4e 6f 6c 64 20 61 20 63 75 72 73 6f 72 2e 20 20 54  old a cursor.  T
16b4f 68 65 20 42 74 43 75 72 73 6f 72 20 6f 62 6a 65  he BtCursor obje
16b50 63 74 20 69 73 20 6f 70 61 71 75 65 0a 2a 2a 20  ct is opaque.** 
16b51 74 6f 20 75 73 65 72 73 20 73 6f 20 74 68 65 79  to users so they
16b52 20 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65 20 73   cannot do the s
16b53 69 7a 65 6f 66 28 29 20 74 68 65 6d 73 65 6c 76  izeof() themselv
16b54 65 73 20 2d 20 74 68 65 79 20 6d 75 73 74 20 63  es - they must c
16b55 61 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  all.** this rout
16b56 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ine..*/.SQLITE_P
16b57 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16b58 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a  e3BtreeCursorSiz
16b59 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72  e(void){.  retur
16b5a 6e 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28  n ROUND8(sizeof(
16b5b 42 74 43 75 72 73 6f 72 29 29 3b 0a 7d 0a 0a 2f  BtCursor));.}../
16b5c 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
16b5d 6d 65 6d 6f 72 79 20 74 68 61 74 20 77 69 6c 6c  memory that will
16b5e 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e   be converted in
16b5f 74 6f 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62  to a BtCursor ob
16b60 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ject..**.** The 
16b61 73 69 6d 70 6c 65 20 61 70 70 72 6f 61 63 68 20  simple approach 
16b62 68 65 72 65 20 77 6f 75 6c 64 20 62 65 20 74 6f  here would be to
16b63 20 6d 65 6d 73 65 74 28 29 20 74 68 65 20 65 6e   memset() the en
16b64 74 69 72 65 20 6f 62 6a 65 63 74 0a 2a 2a 20 74  tire object.** t
16b65 6f 20 7a 65 72 6f 2e 20 20 42 75 74 20 69 74 20  o zero.  But it 
16b66 74 75 72 6e 73 20 6f 75 74 20 74 68 61 74 20 74  turns out that t
16b67 68 65 20 61 70 50 61 67 65 5b 5d 20 61 6e 64 20  he apPage[] and 
16b68 61 69 49 64 78 5b 5d 20 61 72 72 61 79 73 0a 2a  aiIdx[] arrays.*
16b69 2a 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  * do not need to
16b6a 20 62 65 20 7a 65 72 6f 65 64 20 61 6e 64 20 74   be zeroed and t
16b6b 68 65 79 20 61 72 65 20 6c 61 72 67 65 2c 20 73  hey are large, s
16b6c 6f 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 20  o we can save a 
16b6d 6c 6f 74 0a 2a 2a 20 6f 66 20 72 75 6e 2d 74 69  lot.** of run-ti
16b6e 6d 65 20 62 79 20 73 6b 69 70 70 69 6e 67 20 74  me by skipping t
16b6f 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  he initializatio
16b70 6e 20 6f 66 20 74 68 6f 73 65 20 65 6c 65 6d 65  n of those eleme
16b71 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nts..*/.SQLITE_P
16b72 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
16b73 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a 65  te3BtreeCursorZe
16b74 72 6f 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b  ro(BtCursor *p){
16b75 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20  .  memset(p, 0, 
16b76 6f 66 66 73 65 74 6f 66 28 42 74 43 75 72 73 6f  offsetof(BtCurso
16b77 72 2c 20 69 50 61 67 65 29 29 3b 0a 7d 0a 0a 2f  r, iPage));.}../
16b78 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 61 63  *.** Set the cac
16b79 68 65 64 20 72 6f 77 69 64 20 76 61 6c 75 65 20  hed rowid value 
16b7a 6f 66 20 65 76 65 72 79 20 63 75 72 73 6f 72 20  of every cursor 
16b7b 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  in the same data
16b7c 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 61 73 20  base file.** as 
16b7d 70 43 75 72 20 61 6e 64 20 68 61 76 69 6e 67 20  pCur and having 
16b7e 74 68 65 20 73 61 6d 65 20 72 6f 6f 74 20 70 61  the same root pa
16b7f 67 65 20 6e 75 6d 62 65 72 20 61 73 20 70 43 75  ge number as pCu
16b80 72 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73  r.  The value is
16b81 0a 2a 2a 20 73 65 74 20 74 6f 20 69 52 6f 77 69  .** set to iRowi
16b82 64 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 70 6f  d..**.** Only po
16b83 73 69 74 69 76 65 20 72 6f 77 69 64 20 76 61 6c  sitive rowid val
16b84 75 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72  ues are consider
16b85 65 64 20 76 61 6c 69 64 20 66 6f 72 20 74 68 69  ed valid for thi
16b86 73 20 63 61 63 68 65 2e 0a 2a 2a 20 54 68 65 20  s cache..** The 
16b87 63 61 63 68 65 20 69 73 20 69 6e 69 74 69 61 6c  cache is initial
16b88 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2c 20 69 6e  ized to zero, in
16b89 64 69 63 61 74 69 6e 67 20 61 6e 20 69 6e 76 61  dicating an inva
16b8a 6c 69 64 20 63 61 63 68 65 2e 0a 2a 2a 20 41 20  lid cache..** A 
16b8b 62 74 72 65 65 20 77 69 6c 6c 20 77 6f 72 6b 20  btree will work 
16b8c 66 69 6e 65 20 77 69 74 68 20 7a 65 72 6f 20 6f  fine with zero o
16b8d 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77 69 64  r negative rowid
16b8e 73 2e 20 20 57 65 20 6a 75 73 74 20 63 61 6e 6e  s.  We just cann
16b8f 6f 74 0a 2a 2a 20 63 61 63 68 65 20 7a 65 72 6f  ot.** cache zero
16b90 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77   or negative row
16b91 69 64 73 2c 20 77 68 69 63 68 20 6d 65 61 6e 73  ids, which means
16b92 20 74 61 62 6c 65 73 20 74 68 61 74 20 75 73 65   tables that use
16b93 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61   zero or.** nega
16b94 74 69 76 65 20 72 6f 77 69 64 73 20 6d 69 67 68  tive rowids migh
16b95 74 20 72 75 6e 20 61 20 6c 69 74 74 6c 65 20 73  t run a little s
16b96 6c 6f 77 65 72 2e 20 20 42 75 74 20 69 6e 20 70  lower.  But in p
16b97 72 61 63 74 69 63 65 2c 20 7a 65 72 6f 0a 2a 2a  ractice, zero.**
16b98 20 6f 72 20 6e 65 67 61 74 69 76 65 20 72 6f 77   or negative row
16b99 69 64 73 20 61 72 65 20 76 65 72 79 20 75 6e 63  ids are very unc
16b9a 6f 6d 6d 6f 6e 20 73 6f 20 74 68 69 73 20 73 68  ommon so this sh
16b9b 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 20 70 72  ould not be a pr
16b9c 6f 62 6c 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  oblem..*/.SQLITE
16b9d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
16b9e 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
16b9f 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f  hedRowid(BtCurso
16ba0 72 20 2a 70 43 75 72 2c 20 73 71 6c 69 74 65 33  r *pCur, sqlite3
16ba1 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29 7b 0a  _int64 iRowid){.
16ba2 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20    BtCursor *p;. 
16ba3 20 66 6f 72 28 70 3d 70 43 75 72 2d 3e 70 42 74   for(p=pCur->pBt
16ba4 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d  ->pCursor; p; p=
16ba5 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  p->pNext){.    i
16ba6 66 28 20 70 2d 3e 70 67 6e 6f 52 6f 6f 74 3d 3d  f( p->pgnoRoot==
16ba7 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29  pCur->pgnoRoot )
16ba8 20 70 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 20   p->cachedRowid 
16ba9 3d 20 69 52 6f 77 69 64 3b 0a 20 20 7d 0a 20 20  = iRowid;.  }.  
16baa 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 63 61  assert( pCur->ca
16bab 63 68 65 64 52 6f 77 69 64 3d 3d 69 52 6f 77 69  chedRowid==iRowi
16bac 64 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  d );.}../*.** Re
16bad 74 75 72 6e 20 74 68 65 20 63 61 63 68 65 64 20  turn the cached 
16bae 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20 67 69  rowid for the gi
16baf 76 65 6e 20 63 75 72 73 6f 72 2e 20 20 41 20 6e  ven cursor.  A n
16bb0 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 0a  egative or zero.
16bb1 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  ** return value 
16bb2 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74  indicates that t
16bb3 68 65 20 72 6f 77 69 64 20 63 61 63 68 65 20 69  he rowid cache i
16bb4 73 20 69 6e 76 61 6c 69 64 20 61 6e 64 20 73 68  s invalid and sh
16bb5 6f 75 6c 64 20 62 65 0a 2a 2a 20 69 67 6e 6f 72  ould be.** ignor
16bb6 65 64 2e 20 20 49 66 20 74 68 65 20 72 6f 77 69  ed.  If the rowi
16bb7 64 20 63 61 63 68 65 20 68 61 73 20 6e 65 76 65  d cache has neve
16bb8 72 20 62 65 66 6f 72 65 20 62 65 65 6e 20 73 65  r before been se
16bb9 74 2c 20 74 68 65 6e 20 61 0a 2a 2a 20 7a 65 72  t, then a.** zer
16bba 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  o is returned..*
16bbb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16bbc 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73   sqlite3_int64 s
16bbd 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 43 61  qlite3BtreeGetCa
16bbe 63 68 65 64 52 6f 77 69 64 28 42 74 43 75 72 73  chedRowid(BtCurs
16bbf 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72 65 74  or *pCur){.  ret
16bc0 75 72 6e 20 70 43 75 72 2d 3e 63 61 63 68 65 64  urn pCur->cached
16bc1 52 6f 77 69 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Rowid;.}../*.** 
16bc2 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 2e 20  Close a cursor. 
16bc3 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 6f   The read lock o
16bc4 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
16bc5 69 6c 65 20 69 73 20 72 65 6c 65 61 73 65 64 0a  ile is released.
16bc6 2a 2a 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74  ** when the last
16bc7 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73 65   cursor is close
16bc8 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
16bc9 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16bca 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72  BtreeCloseCursor
16bcb 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
16bcc 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65  {.  Btree *pBtre
16bcd 65 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65  e = pCur->pBtree
16bce 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 20 29  ;.  if( pBtree )
16bcf 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
16bd0 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
16bd1 20 70 43 75 72 2d 3e 70 42 74 3b 0a 20 20 20 20   pCur->pBt;.    
16bd2 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
16bd3 72 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20 73  r(pBtree);.    s
16bd4 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72  qlite3BtreeClear
16bd5 43 75 72 73 6f 72 28 70 43 75 72 29 3b 0a 20 20  Cursor(pCur);.  
16bd6 20 20 69 66 28 20 70 43 75 72 2d 3e 70 50 72 65    if( pCur->pPre
16bd7 76 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d  v ){.      pCur-
16bd8 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
16bd9 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  pCur->pNext;.   
16bda 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 42   }else{.      pB
16bdb 74 2d 3e 70 43 75 72 73 6f 72 20 3d 20 70 43 75  t->pCursor = pCu
16bdc 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  r->pNext;.    }.
16bdd 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e      if( pCur->pN
16bde 65 78 74 20 29 7b 0a 20 20 20 20 20 20 70 43 75  ext ){.      pCu
16bdf 72 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20  r->pNext->pPrev 
16be0 3d 20 70 43 75 72 2d 3e 70 50 72 65 76 3b 0a 20  = pCur->pPrev;. 
16be1 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30     }.    for(i=0
16be2 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65  ; i<=pCur->iPage
16be3 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65  ; i++){.      re
16be4 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e  leasePage(pCur->
16be5 61 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20  apPage[i]);.    
16be6 7d 0a 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65  }.    unlockBtre
16be7 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a  eIfUnused(pBt);.
16be8 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 4f 76      invalidateOv
16be9 65 72 66 6c 6f 77 43 61 63 68 65 28 70 43 75 72  erflowCache(pCur
16bea 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65  );.    /* sqlite
16beb 33 5f 66 72 65 65 28 70 43 75 72 29 3b 20 2a 2f  3_free(pCur); */
16bec 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
16bed 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a  eLeave(pBtree);.
16bee 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
16bef 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
16bf0 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 42   Make sure the B
16bf1 74 43 75 72 73 6f 72 2a 20 67 69 76 65 6e 20 69  tCursor* given i
16bf2 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 68  n the argument h
16bf3 61 73 20 61 20 76 61 6c 69 64 0a 2a 2a 20 42 74  as a valid.** Bt
16bf4 43 75 72 73 6f 72 2e 69 6e 66 6f 20 73 74 72 75  Cursor.info stru
16bf5 63 74 75 72 65 2e 20 20 49 66 20 69 74 20 69 73  cture.  If it is
16bf6 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 76 61 6c   not already val
16bf7 69 64 2c 20 63 61 6c 6c 0a 2a 2a 20 62 74 72 65  id, call.** btre
16bf8 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74 6f 20  eParseCell() to 
16bf9 66 69 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a  fill it in..**.*
16bfa 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20  * BtCursor.info 
16bfb 69 73 20 61 20 63 61 63 68 65 20 6f 66 20 74 68  is a cache of th
16bfc 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  e information in
16bfd 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 65 6c   the current cel
16bfe 6c 2e 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73  l..** Using this
16bff 20 63 61 63 68 65 20 72 65 64 75 63 65 73 20 74   cache reduces t
16c00 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c  he number of cal
16c01 6c 73 20 74 6f 20 62 74 72 65 65 50 61 72 73 65  ls to btreeParse
16c02 43 65 6c 6c 28 29 2e 0a 2a 2a 0a 2a 2a 20 32 30  Cell()..**.** 20
16c03 30 37 2d 30 36 2d 32 35 3a 20 20 54 68 65 72 65  07-06-25:  There
16c04 20 69 73 20 61 20 62 75 67 20 69 6e 20 73 6f 6d   is a bug in som
16c05 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 4d 53  e versions of MS
16c06 56 43 20 74 68 61 74 20 63 61 75 73 65 20 74 68  VC that cause th
16c07 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 74 6f  e.** compiler to
16c08 20 63 72 61 73 68 20 77 68 65 6e 20 67 65 74 43   crash when getC
16c09 65 6c 6c 49 6e 66 6f 28 29 20 69 73 20 69 6d 70  ellInfo() is imp
16c0a 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20 6d 61  lemented as a ma
16c0b 63 72 6f 2e 0a 2a 2a 20 42 75 74 20 74 68 65 72  cro..** But ther
16c0c 65 20 69 73 20 61 20 6d 65 61 73 75 72 65 61 62  e is a measureab
16c0d 6c 65 20 73 70 65 65 64 20 61 64 76 61 6e 74 61  le speed advanta
16c0e 67 65 20 74 6f 20 75 73 69 6e 67 20 74 68 65 20  ge to using the 
16c0f 6d 61 63 72 6f 20 6f 6e 20 67 63 63 0a 2a 2a 20  macro on gcc.** 
16c10 28 77 68 65 6e 20 6c 65 73 73 20 63 6f 6d 70 69  (when less compi
16c11 6c 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ler optimization
16c12 73 20 6c 69 6b 65 20 2d 4f 73 20 6f 72 20 2d 4f  s like -Os or -O
16c13 30 20 61 72 65 20 75 73 65 64 20 61 6e 64 20 74  0 are used and t
16c14 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 69  he.** compiler i
16c15 73 20 6e 6f 74 20 64 6f 69 6e 67 20 61 67 72 65  s not doing agre
16c16 73 73 69 76 65 20 69 6e 6c 69 6e 69 6e 67 2e 29  ssive inlining.)
16c17 20 20 53 6f 20 77 65 20 75 73 65 20 61 20 72 65    So we use a re
16c18 61 6c 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66  al function.** f
16c19 6f 72 20 4d 53 56 43 20 61 6e 64 20 61 20 6d 61  or MSVC and a ma
16c1a 63 72 6f 20 66 6f 72 20 65 76 65 72 79 74 68 69  cro for everythi
16c1b 6e 67 20 65 6c 73 65 2e 20 20 54 69 63 6b 65 74  ng else.  Ticket
16c1c 20 23 32 34 35 37 2e 0a 2a 2f 0a 23 69 66 6e 64   #2457..*/.#ifnd
16c1d 65 66 20 4e 44 45 42 55 47 0a 20 20 73 74 61 74  ef NDEBUG.  stat
16c1e 69 63 20 76 6f 69 64 20 61 73 73 65 72 74 43 65  ic void assertCe
16c1f 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20  llInfo(BtCursor 
16c20 2a 70 43 75 72 29 7b 0a 20 20 20 20 43 65 6c 6c  *pCur){.    Cell
16c21 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 69  Info info;.    i
16c22 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d  nt iPage = pCur-
16c23 3e 69 50 61 67 65 3b 0a 20 20 20 20 6d 65 6d 73  >iPage;.    mems
16c24 65 74 28 26 69 6e 66 6f 2c 20 30 2c 20 73 69 7a  et(&info, 0, siz
16c25 65 6f 66 28 69 6e 66 6f 29 29 3b 0a 20 20 20 20  eof(info));.    
16c26 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70  btreeParseCell(p
16c27 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67  Cur->apPage[iPag
16c28 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  e], pCur->aiIdx[
16c29 69 50 61 67 65 5d 2c 20 26 69 6e 66 6f 29 3b 0a  iPage], &info);.
16c2a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 63      assert( memc
16c2b 6d 70 28 26 69 6e 66 6f 2c 20 26 70 43 75 72 2d  mp(&info, &pCur-
16c2c 3e 69 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 69 6e  >info, sizeof(in
16c2d 66 6f 29 29 3d 3d 30 20 29 3b 0a 20 20 7d 0a 23  fo))==0 );.  }.#
16c2e 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 61  else.  #define a
16c2f 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28 78 29  ssertCellInfo(x)
16c30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 5f  .#endif.#ifdef _
16c31 4d 53 43 5f 56 45 52 0a 20 20 2f 2a 20 55 73 65  MSC_VER.  /* Use
16c32 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e   a real function
16c33 20 69 6e 20 4d 53 56 43 20 74 6f 20 77 6f 72 6b   in MSVC to work
16c34 20 61 72 6f 75 6e 64 20 62 75 67 73 20 69 6e 20   around bugs in 
16c35 74 68 61 74 20 63 6f 6d 70 69 6c 65 72 2e 20 2a  that compiler. *
16c36 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20  /.  static void 
16c37 67 65 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75  getCellInfo(BtCu
16c38 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20  rsor *pCur){.   
16c39 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e   if( pCur->info.
16c3a 6e 53 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20  nSize==0 ){.    
16c3b 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43    int iPage = pC
16c3c 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20 20 20  ur->iPage;.     
16c3d 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28   btreeParseCell(
16c3e 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61  pCur->apPage[iPa
16c3f 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b  ge],pCur->aiIdx[
16c40 69 50 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e  iPage],&pCur->in
16c41 66 6f 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d  fo);.      pCur-
16c42 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a  >validNKey = 1;.
16c43 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
16c44 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f 28   assertCellInfo(
16c45 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  pCur);.    }.  }
16c46 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 6e 6f 74  .#else /* if not
16c47 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 20 20 2f   _MSC_VER */.  /
16c48 2a 20 55 73 65 20 61 20 6d 61 63 72 6f 20 69 6e  * Use a macro in
16c49 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f 6d 70 69   all other compi
16c4a 6c 65 72 73 20 73 6f 20 74 68 61 74 20 74 68 65  lers so that the
16c4b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 6c   function is inl
16c4c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ined */.#define 
16c4d 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  getCellInfo(pCur
16c4e 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
16c4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c51 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 70         \.  if( p
16c52 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d  Cur->info.nSize=
16c53 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  =0 ){           
16c54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c56 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 69 6e          \.    in
16c57 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e  t iPage = pCur->
16c58 69 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  iPage;          
16c59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c5b 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 62           \.    b
16c5c 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 70 43  treeParseCell(pC
16c5d 75 72 2d 3e 61 70 50 61 67 65 5b 69 50 61 67 65  ur->apPage[iPage
16c5e 5d 2c 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50  ],pCur->aiIdx[iP
16c5f 61 67 65 5d 2c 26 70 43 75 72 2d 3e 69 6e 66 6f  age],&pCur->info
16c60 29 3b 20 5c 0a 20 20 20 20 70 43 75 72 2d 3e 76  ); \.    pCur->v
16c61 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 20 20 20  alidNKey = 1;   
16c62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c65 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65 7b 20 20      \.  }else{  
16c66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c6a 20 20 20 20 20 5c 0a 20 20 20 20 61 73 73 65 72       \.    asser
16c6b 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b  tCellInfo(pCur);
16c6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c6f 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6e 64        \.  }.#end
16c70 69 66 20 2f 2a 20 5f 4d 53 43 5f 56 45 52 20 2a  if /* _MSC_VER *
16c71 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  /..#ifndef NDEBU
16c72 47 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 72  G  /* The next r
16c73 6f 75 74 69 6e 65 20 75 73 65 64 20 6f 6e 6c 79  outine used only
16c74 20 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29   within assert()
16c75 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 2f   statements */./
16c76 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65  *.** Return true
16c77 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 42 74   if the given Bt
16c78 43 75 72 73 6f 72 20 69 73 20 76 61 6c 69 64 2e  Cursor is valid.
16c79 20 20 41 20 76 61 6c 69 64 20 63 75 72 73 6f 72    A valid cursor
16c7a 20 69 73 20 6f 6e 65 0a 2a 2a 20 74 68 61 74 20   is one.** that 
16c7b 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
16c7c 6e 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 69  nting to a row i
16c7d 6e 20 61 20 28 6e 6f 6e 2d 65 6d 70 74 79 29 20  n a (non-empty) 
16c7e 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 69 73 20 69  table..** This i
16c7f 73 20 61 20 76 65 72 69 66 69 63 61 74 69 6f 6e  s a verification
16c80 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
16c81 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 61 73 73   only within ass
16c82 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
16c83 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
16c84 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
16c85 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69  treeCursorIsVali
16c86 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  d(BtCursor *pCur
16c87 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 75 72  ){.  return pCur
16c88 20 26 26 20 70 43 75 72 2d 3e 65 53 74 61 74 65   && pCur->eState
16c89 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a  ==CURSOR_VALID;.
16c8a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  }.#endif /* NDEB
16c8b 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  UG */../*.** Set
16c8c 20 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 73   *pSize to the s
16c8d 69 7a 65 20 6f 66 20 74 68 65 20 62 75 66 66 65  ize of the buffe
16c8e 72 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64  r needed to hold
16c8f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a   the value of.**
16c90 20 74 68 65 20 6b 65 79 20 66 6f 72 20 74 68 65   the key for the
16c91 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 2e 20   current entry. 
16c92 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69   If the cursor i
16c93 73 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 0a 2a  s not pointing.*
16c94 2a 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74  * to a valid ent
16c95 72 79 2c 20 2a 70 53 69 7a 65 20 69 73 20 73 65  ry, *pSize is se
16c96 74 20 74 6f 20 30 2e 20 0a 2a 2a 0a 2a 2a 20 46  t to 0. .**.** F
16c97 6f 72 20 61 20 74 61 62 6c 65 20 77 69 74 68 20  or a table with 
16c98 74 68 65 20 49 4e 54 4b 45 59 20 66 6c 61 67 20  the INTKEY flag 
16c99 73 65 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  set, this routin
16c9a 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6b 65  e returns the ke
16c9b 79 0a 2a 2a 20 69 74 73 65 6c 66 2c 20 6e 6f 74  y.** itself, not
16c9c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
16c9d 79 74 65 73 20 69 6e 20 74 68 65 20 6b 65 79 2e  ytes in the key.
16c9e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65  .**.** The calle
16c9f 72 20 6d 75 73 74 20 70 6f 73 69 74 69 6f 6e 20  r must position 
16ca0 74 68 65 20 63 75 72 73 6f 72 20 70 72 69 6f 72  the cursor prior
16ca1 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 74 68 69   to invoking thi
16ca2 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 0a 2a  s routine..** .*
16ca3 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
16ca4 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20 49 74 20  annot fail.  It 
16ca5 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20 53  always returns S
16ca6 51 4c 49 54 45 5f 4f 4b 2e 20 20 0a 2a 2f 0a 53  QLITE_OK.  .*/.S
16ca7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16ca8 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  t sqlite3BtreeKe
16ca9 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 20 2a  ySize(BtCursor *
16caa 70 43 75 72 2c 20 69 36 34 20 2a 70 53 69 7a 65  pCur, i64 *pSize
16cab 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ){.  assert( cur
16cac 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
16cad 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
16cae 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
16caf 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 7c 7c  URSOR_INVALID ||
16cb0 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
16cb1 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
16cb2 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74   if( pCur->eStat
16cb3 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20  e!=CURSOR_VALID 
16cb4 29 7b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20  ){.    *pSize = 
16cb5 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
16cb6 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72  getCellInfo(pCur
16cb7 29 3b 0a 20 20 20 20 2a 70 53 69 7a 65 20 3d 20  );.    *pSize = 
16cb8 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b  pCur->info.nKey;
16cb9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
16cba 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
16cbb 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20  * Set *pSize to 
16cbc 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
16cbd 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 74  tes of data in t
16cbe 68 65 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20  he entry the.** 
16cbf 63 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79  cursor currently
16cc0 20 70 6f 69 6e 74 73 20 74 6f 2e 0a 2a 2a 0a 2a   points to..**.*
16cc1 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  * The caller mus
16cc2 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74  t guarantee that
16cc3 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 70   the cursor is p
16cc4 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 6e 6f 6e  ointing to a non
16cc5 2d 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 69 64 20 65  -NULL.** valid e
16cc6 6e 74 72 79 2e 20 20 49 6e 20 6f 74 68 65 72 20  ntry.  In other 
16cc7 77 6f 72 64 73 2c 20 74 68 65 20 63 61 6c 6c 69  words, the calli
16cc8 6e 67 20 70 72 6f 63 65 64 75 72 65 20 6d 75 73  ng procedure mus
16cc9 74 20 67 75 61 72 61 6e 74 65 65 0a 2a 2a 20 74  t guarantee.** t
16cca 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20 68  hat the cursor h
16ccb 61 73 20 43 75 72 73 6f 72 2e 65 53 74 61 74 65  as Cursor.eState
16ccc 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 2e 0a  ==CURSOR_VALID..
16ccd 2a 2a 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69 73  **.** Failure is
16cce 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20 20   not possible.  
16ccf 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  This function al
16cd0 77 61 79 73 20 72 65 74 75 72 6e 73 20 53 51 4c  ways returns SQL
16cd1 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 49 74 20 6d 69  ITE_OK..** It mi
16cd2 67 68 74 20 6a 75 73 74 20 61 73 20 77 65 6c 6c  ght just as well
16cd3 20 62 65 20 61 20 70 72 6f 63 65 64 75 72 65 20   be a procedure 
16cd4 28 72 65 74 75 72 6e 69 6e 67 20 76 6f 69 64 29  (returning void)
16cd5 20 62 75 74 20 77 65 20 63 6f 6e 74 69 6e 75 65   but we continue
16cd6 0a 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20 61 6e  .** to return an
16cd7 20 69 6e 74 65 67 65 72 20 72 65 73 75 6c 74 20   integer result 
16cd8 63 6f 64 65 20 66 6f 72 20 68 69 73 74 6f 72 69  code for histori
16cd9 63 61 6c 20 72 65 61 73 6f 6e 73 2e 0a 2a 2f 0a  cal reasons..*/.
16cda 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
16cdb 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  nt sqlite3BtreeD
16cdc 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72  ataSize(BtCursor
16cdd 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53 69   *pCur, u32 *pSi
16cde 7a 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63  ze){.  assert( c
16cdf 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
16ce0 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
16ce1 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
16ce2 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
16ce3 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70  .  getCellInfo(p
16ce4 43 75 72 29 3b 0a 20 20 2a 70 53 69 7a 65 20 3d  Cur);.  *pSize =
16ce5 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74   pCur->info.nDat
16ce6 61 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  a;.  return SQLI
16ce7 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
16ce8 47 69 76 65 6e 20 74 68 65 20 70 61 67 65 20 6e  Given the page n
16ce9 75 6d 62 65 72 20 6f 66 20 61 6e 20 6f 76 65 72  umber of an over
16cea 66 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65  flow page in the
16ceb 20 64 61 74 61 62 61 73 65 20 28 70 61 72 61 6d   database (param
16cec 65 74 65 72 0a 2a 2a 20 6f 76 66 6c 29 2c 20 74  eter.** ovfl), t
16ced 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69 6e  his function fin
16cee 64 73 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62  ds the page numb
16cef 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70  er of the next p
16cf0 61 67 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 6c  age in the .** l
16cf1 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 6f 76  inked list of ov
16cf2 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 49 66  erflow pages. If
16cf3 20 70 6f 73 73 69 62 6c 65 2c 20 69 74 20 75 73   possible, it us
16cf4 65 73 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75  es the auto-vacu
16cf5 75 6d 0a 2a 2a 20 70 6f 69 6e 74 65 72 2d 6d 61  um.** pointer-ma
16cf6 70 20 64 61 74 61 20 69 6e 73 74 65 61 64 20 6f  p data instead o
16cf7 66 20 72 65 61 64 69 6e 67 20 74 68 65 20 63 6f  f reading the co
16cf8 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20 6f 76  ntent of page ov
16cf9 66 6c 20 74 6f 20 64 6f 20 73 6f 2e 20 0a 2a 2a  fl to do so. .**
16cfa 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
16cfb 6f 63 63 75 72 73 20 61 6e 20 53 51 4c 69 74 65  occurs an SQLite
16cfc 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
16cfd 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69  eturned. Otherwi
16cfe 73 65 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61  se:.**.** The pa
16cff 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
16d00 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70   next overflow p
16d01 61 67 65 20 69 6e 20 74 68 65 20 6c 69 6e 6b 65  age in the linke
16d02 64 20 6c 69 73 74 20 69 73 20 0a 2a 2a 20 77 72  d list is .** wr
16d03 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f 4e  itten to *pPgnoN
16d04 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 66  ext. If page ovf
16d05 6c 20 69 73 20 74 68 65 20 6c 61 73 74 20 70 61  l is the last pa
16d06 67 65 20 69 6e 20 69 74 73 20 6c 69 6e 6b 65 64  ge in its linked
16d07 20 0a 2a 2a 20 6c 69 73 74 2c 20 2a 70 50 67 6e   .** list, *pPgn
16d08 6f 4e 65 78 74 20 69 73 20 73 65 74 20 74 6f 20  oNext is set to 
16d09 7a 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  zero. .**.** If 
16d0a 70 70 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55  ppPage is not NU
16d0b 4c 4c 2c 20 61 6e 64 20 61 20 72 65 66 65 72 65  LL, and a refere
16d0c 6e 63 65 20 74 6f 20 74 68 65 20 4d 65 6d 50 61  nce to the MemPa
16d0d 67 65 20 6f 62 6a 65 63 74 20 63 6f 72 72 65 73  ge object corres
16d0e 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70 61  ponding.** to pa
16d0f 67 65 20 6e 75 6d 62 65 72 20 70 4f 76 66 6c 20  ge number pOvfl 
16d10 77 61 73 20 6f 62 74 61 69 6e 65 64 2c 20 74 68  was obtained, th
16d11 65 6e 20 2a 70 70 50 61 67 65 20 69 73 20 73 65  en *ppPage is se
16d12 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
16d13 61 74 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 2e  at.** reference.
16d14 20 49 74 20 69 73 20 74 68 65 20 72 65 73 70 6f   It is the respo
16d15 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65  nsibility of the
16d16 20 63 61 6c 6c 65 72 20 74 6f 20 63 61 6c 6c 20   caller to call 
16d17 72 65 6c 65 61 73 65 50 61 67 65 28 29 0a 2a 2a  releasePage().**
16d18 20 6f 6e 20 2a 70 70 50 61 67 65 20 74 6f 20 66   on *ppPage to f
16d19 72 65 65 20 74 68 65 20 72 65 66 65 72 65 6e 63  ree the referenc
16d1a 65 2e 20 49 6e 20 6e 6f 20 72 65 66 65 72 65 6e  e. In no referen
16d1b 63 65 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20  ce was obtained 
16d1c 28 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 65 20  (because.** the 
16d1d 70 6f 69 6e 74 65 72 2d 6d 61 70 20 77 61 73 20  pointer-map was 
16d1e 75 73 65 64 20 74 6f 20 6f 62 74 61 69 6e 20 74  used to obtain t
16d1f 68 65 20 76 61 6c 75 65 20 66 6f 72 20 2a 70 50  he value for *pP
16d20 67 6e 6f 4e 65 78 74 29 2c 20 74 68 65 6e 0a 2a  gnoNext), then.*
16d21 2a 20 2a 70 70 50 61 67 65 20 69 73 20 73 65 74  * *ppPage is set
16d22 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61   to zero..*/.sta
16d23 74 69 63 20 69 6e 74 20 67 65 74 4f 76 65 72 66  tic int getOverf
16d24 6c 6f 77 50 61 67 65 28 0a 20 20 42 74 53 68 61  lowPage(.  BtSha
16d25 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20  red *pBt,       
16d26 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
16d27 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
16d28 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20 20    Pgno ovfl,    
16d29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
16d2a 2a 20 43 75 72 72 65 6e 74 20 6f 76 65 72 66 6c  * Current overfl
16d2b 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a  ow page number *
16d2c 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70  /.  MemPage **pp
16d2d 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  Page,           
16d2e 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61 67 65   /* OUT: MemPage
16d2f 20 68 61 6e 64 6c 65 20 28 6d 61 79 20 62 65 20   handle (may be 
16d30 4e 55 4c 4c 29 20 2a 2f 0a 20 20 50 67 6e 6f 20  NULL) */.  Pgno 
16d31 2a 70 50 67 6e 6f 4e 65 78 74 20 20 20 20 20 20  *pPgnoNext      
16d32 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
16d33 4e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 70 61  Next overflow pa
16d34 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 29 7b 0a  ge number */.){.
16d35 20 20 50 67 6e 6f 20 6e 65 78 74 20 3d 20 30 3b    Pgno next = 0;
16d36 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
16d37 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20  e = 0;.  int rc 
16d38 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
16d39 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
16d3a 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
16d3b 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
16d3c 72 74 28 70 50 67 6e 6f 4e 65 78 74 29 3b 0a 0a  rt(pPgnoNext);..
16d3d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
16d3e 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
16d3f 20 2f 2a 20 54 72 79 20 74 6f 20 66 69 6e 64 20   /* Try to find 
16d40 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e  the next page in
16d41 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c 69   the overflow li
16d42 73 74 20 75 73 69 6e 67 20 74 68 65 0a 20 20 2a  st using the.  *
16d43 2a 20 61 75 74 6f 76 61 63 75 75 6d 20 70 6f 69  * autovacuum poi
16d44 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 73 2e 20  nter-map pages. 
16d45 47 75 65 73 73 20 74 68 61 74 20 74 68 65 20 6e  Guess that the n
16d46 65 78 74 20 70 61 67 65 20 69 6e 20 0a 20 20 2a  ext page in .  *
16d47 2a 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 6c  * the overflow l
16d48 69 73 74 20 69 73 20 70 61 67 65 20 6e 75 6d 62  ist is page numb
16d49 65 72 20 28 6f 76 66 6c 2b 31 29 2e 20 49 66 20  er (ovfl+1). If 
16d4a 74 68 61 74 20 67 75 65 73 73 20 74 75 72 6e 73  that guess turns
16d4b 20 0a 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65   .  ** out to be
16d4c 20 77 72 6f 6e 67 2c 20 66 61 6c 6c 20 62 61 63   wrong, fall bac
16d4d 6b 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65  k to loading the
16d4e 20 64 61 74 61 20 6f 66 20 70 61 67 65 20 0a 20   data of page . 
16d4f 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 76 66 6c 20   ** number ovfl 
16d50 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
16d51 20 6e 65 78 74 20 70 61 67 65 20 6e 75 6d 62 65   next page numbe
16d52 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42  r..  */.  if( pB
16d53 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
16d54 0a 20 20 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a  .    Pgno pgno;.
16d55 20 20 20 20 50 67 6e 6f 20 69 47 75 65 73 73 20      Pgno iGuess 
16d56 3d 20 6f 76 66 6c 2b 31 3b 0a 20 20 20 20 75 38  = ovfl+1;.    u8
16d57 20 65 54 79 70 65 3b 0a 0a 20 20 20 20 77 68 69   eType;..    whi
16d58 6c 65 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47  le( PTRMAP_ISPAG
16d59 45 28 70 42 74 2c 20 69 47 75 65 73 73 29 20 7c  E(pBt, iGuess) |
16d5a 7c 20 69 47 75 65 73 73 3d 3d 50 45 4e 44 49 4e  | iGuess==PENDIN
16d5b 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
16d5c 20 29 7b 0a 20 20 20 20 20 20 69 47 75 65 73 73   ){.      iGuess
16d5d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ++;.    }..    i
16d5e 66 28 20 69 47 75 65 73 73 3c 3d 70 61 67 65 72  f( iGuess<=pager
16d5f 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29  Pagecount(pBt) )
16d60 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 74 72  {.      rc = ptr
16d61 6d 61 70 47 65 74 28 70 42 74 2c 20 69 47 75 65  mapGet(pBt, iGue
16d62 73 73 2c 20 26 65 54 79 70 65 2c 20 26 70 67 6e  ss, &eType, &pgn
16d63 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  o);.      if( rc
16d64 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65  ==SQLITE_OK && e
16d65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45  Type==PTRMAP_OVE
16d66 52 46 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d  RFLOW2 && pgno==
16d67 6f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  ovfl ){.        
16d68 6e 65 78 74 20 3d 20 69 47 75 65 73 73 3b 0a 20  next = iGuess;. 
16d69 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
16d6a 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d  TE_DONE;.      }
16d6b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
16d6c 66 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 65 78  f..  assert( nex
16d6d 74 3d 3d 30 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  t==0 || rc==SQLI
16d6e 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66 28  TE_DONE );.  if(
16d6f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
16d70 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65 65  {.    rc = btree
16d71 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66  GetPage(pBt, ovf
16d72 6c 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a 20  l, &pPage, 0);. 
16d73 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
16d74 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67  QLITE_OK || pPag
16d75 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  e==0 );.    if( 
16d76 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
16d77 0a 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65  .      next = ge
16d78 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44  t4byte(pPage->aD
16d79 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ata);.    }.  }.
16d7a 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74 20 3d 20  .  *pPgnoNext = 
16d7b 6e 65 78 74 3b 0a 20 20 69 66 28 20 70 70 50 61  next;.  if( ppPa
16d7c 67 65 20 29 7b 0a 20 20 20 20 2a 70 70 50 61 67  ge ){.    *ppPag
16d7d 65 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 65 6c  e = pPage;.  }el
16d7e 73 65 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50  se{.    releaseP
16d7f 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 0a  age(pPage);.  }.
16d80 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51    return (rc==SQ
16d81 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49  LITE_DONE ? SQLI
16d82 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 7d 0a 0a  TE_OK : rc);.}..
16d83 2f 2a 0a 2a 2a 20 43 6f 70 79 20 64 61 74 61 20  /*.** Copy data 
16d84 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 74 6f  from a buffer to
16d85 20 61 20 70 61 67 65 2c 20 6f 72 20 66 72 6f 6d   a page, or from
16d86 20 61 20 70 61 67 65 20 74 6f 20 61 20 62 75 66   a page to a buf
16d87 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 50 61 79 6c  fer..**.** pPayl
16d88 6f 61 64 20 69 73 20 61 20 70 6f 69 6e 74 65 72  oad is a pointer
16d89 20 74 6f 20 64 61 74 61 20 73 74 6f 72 65 64 20   to data stored 
16d8a 6f 6e 20 64 61 74 61 62 61 73 65 20 70 61 67 65  on database page
16d8b 20 70 44 62 50 61 67 65 2e 0a 2a 2a 20 49 66 20   pDbPage..** If 
16d8c 61 72 67 75 6d 65 6e 74 20 65 4f 70 20 69 73 20  argument eOp is 
16d8d 66 61 6c 73 65 2c 20 74 68 65 6e 20 6e 42 79 74  false, then nByt
16d8e 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  e bytes of data 
16d8f 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66 72  are copied.** fr
16d90 6f 6d 20 70 50 61 79 6c 6f 61 64 20 74 6f 20 74  om pPayload to t
16d91 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  he buffer pointe
16d92 64 20 61 74 20 62 79 20 70 42 75 66 2e 20 49 66  d at by pBuf. If
16d93 20 65 4f 70 20 69 73 20 74 72 75 65 2c 0a 2a 2a   eOp is true,.**
16d94 20 74 68 65 6e 20 73 71 6c 69 74 65 33 50 61 67   then sqlite3Pag
16d95 65 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c  erWrite() is cal
16d96 6c 65 64 20 6f 6e 20 70 44 62 50 61 67 65 20 61  led on pDbPage a
16d97 6e 64 20 6e 42 79 74 65 20 62 79 74 65 73 0a 2a  nd nByte bytes.*
16d98 2a 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f  * of data are co
16d99 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62 75  pied from the bu
16d9a 66 66 65 72 20 70 42 75 66 20 74 6f 20 70 50 61  ffer pBuf to pPa
16d9b 79 6c 6f 61 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  yload..**.** SQL
16d9c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
16d9d 65 64 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 6f  ed on success, o
16d9e 74 68 65 72 77 69 73 65 20 61 6e 20 65 72 72 6f  therwise an erro
16d9f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  r code..*/.stati
16da0 63 20 69 6e 74 20 63 6f 70 79 50 61 79 6c 6f 61  c int copyPayloa
16da1 64 28 0a 20 20 76 6f 69 64 20 2a 70 50 61 79 6c  d(.  void *pPayl
16da2 6f 61 64 2c 20 20 20 20 20 20 20 20 20 20 20 2f  oad,           /
16da3 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67  * Pointer to pag
16da4 65 20 64 61 74 61 20 2a 2f 0a 20 20 76 6f 69 64  e data */.  void
16da5 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20   *pBuf,         
16da6 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
16da7 20 74 6f 20 62 75 66 66 65 72 20 2a 2f 0a 20 20   to buffer */.  
16da8 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20  int nByte,      
16da9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
16daa 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
16dab 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 65 4f  copy */.  int eO
16dac 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
16dad 20 20 20 20 2f 2a 20 30 20 2d 3e 20 63 6f 70 79      /* 0 -> copy
16dae 20 66 72 6f 6d 20 70 61 67 65 2c 20 31 20 2d 3e   from page, 1 ->
16daf 20 63 6f 70 79 20 74 6f 20 70 61 67 65 20 2a 2f   copy to page */
16db0 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61  .  DbPage *pDbPa
16db1 67 65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ge           /* 
16db2 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  Page containing 
16db3 70 50 61 79 6c 6f 61 64 20 2a 2f 0a 29 7b 0a 20  pPayload */.){. 
16db4 20 69 66 28 20 65 4f 70 20 29 7b 0a 20 20 20 20   if( eOp ){.    
16db5 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72 6f  /* Copy data fro
16db6 6d 20 62 75 66 66 65 72 20 74 6f 20 70 61 67 65  m buffer to page
16db7 20 28 61 20 77 72 69 74 65 20 6f 70 65 72 61 74   (a write operat
16db8 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ion) */.    int 
16db9 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
16dba 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b  rWrite(pDbPage);
16dbb 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
16dbc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
16dbd 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
16dbe 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61 79  .    memcpy(pPay
16dbf 6c 6f 61 64 2c 20 70 42 75 66 2c 20 6e 42 79 74  load, pBuf, nByt
16dc0 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
16dc1 20 2f 2a 20 43 6f 70 79 20 64 61 74 61 20 66 72   /* Copy data fr
16dc2 6f 6d 20 70 61 67 65 20 74 6f 20 62 75 66 66 65  om page to buffe
16dc3 72 20 28 61 20 72 65 61 64 20 6f 70 65 72 61 74  r (a read operat
16dc4 69 6f 6e 29 20 2a 2f 0a 20 20 20 20 6d 65 6d 63  ion) */.    memc
16dc5 70 79 28 70 42 75 66 2c 20 70 50 61 79 6c 6f 61  py(pBuf, pPayloa
16dc6 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 7d 0a 20  d, nByte);.  }. 
16dc7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
16dc8 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  K;.}../*.** This
16dc9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
16dca 64 20 74 6f 20 72 65 61 64 20 6f 72 20 6f 76 65  d to read or ove
16dcb 72 77 72 69 74 65 20 70 61 79 6c 6f 61 64 20 69  rwrite payload i
16dcc 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66 6f  nformation.** fo
16dcd 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  r the entry that
16dce 20 74 68 65 20 70 43 75 72 20 63 75 72 73 6f 72   the pCur cursor
16dcf 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e   is pointing to.
16dd0 20 49 66 20 74 68 65 20 65 4f 70 0a 2a 2a 20 70   If the eOp.** p
16dd1 61 72 61 6d 65 74 65 72 20 69 73 20 30 2c 20 74  arameter is 0, t
16dd2 68 69 73 20 69 73 20 61 20 72 65 61 64 20 6f 70  his is a read op
16dd3 65 72 61 74 69 6f 6e 20 28 64 61 74 61 20 63 6f  eration (data co
16dd4 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 62 75 66  pied into.** buf
16dd5 66 65 72 20 70 42 75 66 29 2e 20 49 66 20 69 74  fer pBuf). If it
16dd6 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 20   is non-zero, a 
16dd7 77 72 69 74 65 20 28 64 61 74 61 20 63 6f 70 69  write (data copi
16dd8 65 64 20 66 72 6f 6d 0a 2a 2a 20 62 75 66 66 65  ed from.** buffe
16dd9 72 20 70 42 75 66 29 2e 0a 2a 2a 0a 2a 2a 20 41  r pBuf)..**.** A
16dda 20 74 6f 74 61 6c 20 6f 66 20 22 61 6d 74 22 20   total of "amt" 
16ddb 62 79 74 65 73 20 61 72 65 20 72 65 61 64 20 6f  bytes are read o
16ddc 72 20 77 72 69 74 74 65 6e 20 62 65 67 69 6e 6e  r written beginn
16ddd 69 6e 67 20 61 74 20 22 6f 66 66 73 65 74 22 2e  ing at "offset".
16dde 0a 2a 2a 20 44 61 74 61 20 69 73 20 72 65 61 64  .** Data is read
16ddf 20 74 6f 20 6f 72 20 66 72 6f 6d 20 74 68 65 20   to or from the 
16de0 62 75 66 66 65 72 20 70 42 75 66 2e 0a 2a 2a 0a  buffer pBuf..**.
16de1 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 62  ** The content b
16de2 65 69 6e 67 20 72 65 61 64 20 6f 72 20 77 72 69  eing read or wri
16de3 74 74 65 6e 20 6d 69 67 68 74 20 61 70 70 65 61  tten might appea
16de4 72 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 70 61  r on the main pa
16de5 67 65 0a 2a 2a 20 6f 72 20 62 65 20 73 63 61 74  ge.** or be scat
16de6 74 65 72 65 64 20 6f 75 74 20 6f 6e 20 6d 75 6c  tered out on mul
16de7 74 69 70 6c 65 20 6f 76 65 72 66 6c 6f 77 20 70  tiple overflow p
16de8 61 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ages..**.** If t
16de9 68 65 20 42 74 43 75 72 73 6f 72 2e 69 73 49 6e  he BtCursor.isIn
16dea 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 66 6c 61  crblobHandle fla
16deb 67 20 69 73 20 73 65 74 2c 20 61 6e 64 20 74 68  g is set, and th
16dec 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 63 75 72  e current.** cur
16ded 73 6f 72 20 65 6e 74 72 79 20 75 73 65 73 20 6f  sor entry uses o
16dee 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65 72 66  ne or more overf
16def 6c 6f 77 20 70 61 67 65 73 2c 20 74 68 69 73 20  low pages, this 
16df0 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 6c 6c 6f  function.** allo
16df1 63 61 74 65 73 20 73 70 61 63 65 20 66 6f 72 20  cates space for 
16df2 61 6e 64 20 6c 61 7a 69 6c 79 20 70 6f 70 6c 75  and lazily poplu
16df3 61 74 65 73 20 74 68 65 20 6f 76 65 72 66 6c 6f  ates the overflo
16df4 77 20 70 61 67 65 2d 6c 69 73 74 20 0a 2a 2a 20  w page-list .** 
16df5 63 61 63 68 65 20 61 72 72 61 79 20 28 42 74 43  cache array (BtC
16df6 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 29  ursor.aOverflow)
16df7 2e 20 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c  . Subsequent cal
16df8 6c 73 20 75 73 65 20 74 68 69 73 0a 2a 2a 20 63  ls use this.** c
16df9 61 63 68 65 20 74 6f 20 6d 61 6b 65 20 73 65 65  ache to make see
16dfa 6b 69 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70  king to the supp
16dfb 6c 69 65 64 20 6f 66 66 73 65 74 20 6d 6f 72 65  lied offset more
16dfc 20 65 66 66 69 63 69 65 6e 74 2e 0a 2a 2a 0a 2a   efficient..**.*
16dfd 2a 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66 6c  * Once an overfl
16dfe 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63  ow page-list cac
16dff 68 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f  he has been allo
16e00 63 61 74 65 64 2c 20 69 74 20 6d 61 79 20 62 65  cated, it may be
16e01 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 64 20  .** invalidated 
16e02 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 63 75  if some other cu
16e03 72 73 6f 72 20 77 72 69 74 65 73 20 74 6f 20 74  rsor writes to t
16e04 68 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 6f  he same table, o
16e05 72 20 69 66 0a 2a 2a 20 74 68 65 20 63 75 72 73  r if.** the curs
16e06 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f 20 61  or is moved to a
16e07 20 64 69 66 66 65 72 65 6e 74 20 72 6f 77 2e 20   different row. 
16e08 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 69 6e  Additionally, in
16e09 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20   auto-vacuum.** 
16e0a 6d 6f 64 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  mode, the follow
16e0b 69 6e 67 20 65 76 65 6e 74 73 20 6d 61 79 20 69  ing events may i
16e0c 6e 76 61 6c 69 64 61 74 65 20 61 6e 20 6f 76 65  nvalidate an ove
16e0d 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20  rflow page-list 
16e0e 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a  cache..**.**   *
16e0f 20 41 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20   An incremental 
16e10 76 61 63 75 75 6d 2c 0a 2a 2a 20 20 20 2a 20 41  vacuum,.**   * A
16e11 20 63 6f 6d 6d 69 74 20 69 6e 20 61 75 74 6f 5f   commit in auto_
16e12 76 61 63 75 75 6d 3d 22 66 75 6c 6c 22 20 6d 6f  vacuum="full" mo
16e13 64 65 2c 0a 2a 2a 20 20 20 2a 20 43 72 65 61 74  de,.**   * Creat
16e14 69 6e 67 20 61 20 74 61 62 6c 65 20 28 6d 61 79  ing a table (may
16e15 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 20   require moving 
16e16 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  an overflow page
16e17 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
16e18 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 0a   accessPayload(.
16e19 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
16e1a 2c 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72  ,      /* Cursor
16e1b 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74   pointing to ent
16e1c 72 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20  ry to read from 
16e1d 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73 65 74 2c  */.  u32 offset,
16e1e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67            /* Beg
16e1f 69 6e 20 72 65 61 64 69 6e 67 20 74 68 69 73 20  in reading this 
16e20 66 61 72 20 69 6e 74 6f 20 70 61 79 6c 6f 61 64  far into payload
16e21 20 2a 2f 0a 20 20 75 33 32 20 61 6d 74 2c 20 20   */.  u32 amt,  
16e22 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
16e23 61 64 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74  ad this many byt
16e24 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  es */.  unsigned
16e25 20 63 68 61 72 20 2a 70 42 75 66 2c 20 2f 2a 20   char *pBuf, /* 
16e26 57 72 69 74 65 20 74 68 65 20 62 79 74 65 73 20  Write the bytes 
16e27 69 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72  into this buffer
16e28 20 2a 2f 20 0a 20 20 69 6e 74 20 65 4f 70 20 20   */ .  int eOp  
16e29 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a              /* z
16e2a 65 72 6f 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e  ero to read. non
16e2b 2d 7a 65 72 6f 20 74 6f 20 77 72 69 74 65 2e 20  -zero to write. 
16e2c 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  */.){.  unsigned
16e2d 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b   char *aPayload;
16e2e 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
16e2f 54 45 5f 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b 65  TE_OK;.  u32 nKe
16e30 79 3b 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20  y;.  int iIdx = 
16e31 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  0;.  MemPage *pP
16e32 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
16e33 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  ge[pCur->iPage];
16e34 20 2f 2a 20 42 74 72 65 65 20 70 61 67 65 20 6f   /* Btree page o
16e35 66 20 63 75 72 72 65 6e 74 20 65 6e 74 72 79 20  f current entry 
16e36 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  */.  BtShared *p
16e37 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 20  Bt = pCur->pBt; 
16e38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16e39 20 2f 2a 20 42 74 72 65 65 20 74 68 69 73 20 63   /* Btree this c
16e3a 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f  ursor belongs to
16e3b 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
16e3c 50 61 67 65 20 29 3b 0a 20 20 61 73 73 65 72 74  Page );.  assert
16e3d 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
16e3e 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
16e3f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16e40 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
16e41 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  e]<pPage->nCell 
16e42 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  );.  assert( cur
16e43 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
16e44 75 72 29 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c  ur) );..  getCel
16e45 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 61  lInfo(pCur);.  a
16e46 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e  Payload = pCur->
16e47 69 6e 66 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75  info.pCell + pCu
16e48 72 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b  r->info.nHeader;
16e49 0a 20 20 6e 4b 65 79 20 3d 20 28 70 50 61 67 65  .  nKey = (pPage
16e4a 2d 3e 69 6e 74 4b 65 79 20 3f 20 30 20 3a 20 28  ->intKey ? 0 : (
16e4b 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  int)pCur->info.n
16e4c 4b 65 79 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56  Key);..  if( NEV
16e4d 45 52 28 6f 66 66 73 65 74 2b 61 6d 74 20 3e 20  ER(offset+amt > 
16e4e 6e 4b 65 79 2b 70 43 75 72 2d 3e 69 6e 66 6f 2e  nKey+pCur->info.
16e4f 6e 44 61 74 61 29 20 0a 20 20 20 7c 7c 20 26 61  nData) .   || &a
16e50 50 61 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e  Payload[pCur->in
16e51 66 6f 2e 6e 4c 6f 63 61 6c 5d 20 3e 20 26 70 50  fo.nLocal] > &pP
16e52 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e  age->aData[pBt->
16e53 75 73 61 62 6c 65 53 69 7a 65 5d 0a 20 20 29 7b  usableSize].  ){
16e54 0a 20 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 74  .    /* Trying t
16e55 6f 20 72 65 61 64 20 6f 72 20 77 72 69 74 65 20  o read or write 
16e56 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
16e57 74 68 65 20 64 61 74 61 20 69 73 20 61 6e 20 65  the data is an e
16e58 72 72 6f 72 20 2a 2f 0a 20 20 20 20 72 65 74 75  rror */.    retu
16e59 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
16e5a 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f  T_BKPT;.  }..  /
16e5b 2a 20 43 68 65 63 6b 20 69 66 20 64 61 74 61 20  * Check if data 
16e5c 6d 75 73 74 20 62 65 20 72 65 61 64 2f 77 72 69  must be read/wri
16e5d 74 74 65 6e 20 74 6f 2f 66 72 6f 6d 20 74 68 65  tten to/from the
16e5e 20 62 74 72 65 65 20 70 61 67 65 20 69 74 73 65   btree page itse
16e5f 6c 66 2e 20 2a 2f 0a 20 20 69 66 28 20 6f 66 66  lf. */.  if( off
16e60 73 65 74 3c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  set<pCur->info.n
16e61 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74  Local ){.    int
16e62 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 69 66   a = amt;.    if
16e63 28 20 61 2b 6f 66 66 73 65 74 3e 70 43 75 72 2d  ( a+offset>pCur-
16e64 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a  >info.nLocal ){.
16e65 20 20 20 20 20 20 61 20 3d 20 70 43 75 72 2d 3e        a = pCur->
16e66 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6f 66  info.nLocal - of
16e67 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  fset;.    }.    
16e68 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f 61 64  rc = copyPayload
16e69 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66 73 65  (&aPayload[offse
16e6a 74 5d 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70  t], pBuf, a, eOp
16e6b 2c 20 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  , pPage->pDbPage
16e6c 29 3b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20  );.    offset = 
16e6d 30 3b 0a 20 20 20 20 70 42 75 66 20 2b 3d 20 61  0;.    pBuf += a
16e6e 3b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a  ;.    amt -= a;.
16e6f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 66 66    }else{.    off
16e70 73 65 74 20 2d 3d 20 70 43 75 72 2d 3e 69 6e 66  set -= pCur->inf
16e71 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 7d 0a 0a 20  o.nLocal;.  }.. 
16e72 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
16e73 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20  OK && amt>0 ){. 
16e74 20 20 20 63 6f 6e 73 74 20 75 33 32 20 6f 76 66     const u32 ovf
16e75 6c 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61  lSize = pBt->usa
16e76 62 6c 65 53 69 7a 65 20 2d 20 34 3b 20 20 2f 2a  bleSize - 4;  /*
16e77 20 42 79 74 65 73 20 63 6f 6e 74 65 6e 74 20 70   Bytes content p
16e78 65 72 20 6f 76 66 6c 20 70 61 67 65 20 2a 2f 0a  er ovfl page */.
16e79 20 20 20 20 50 67 6e 6f 20 6e 65 78 74 50 61 67      Pgno nextPag
16e7a 65 3b 0a 0a 20 20 20 20 6e 65 78 74 50 61 67 65  e;..    nextPage
16e7b 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 50 61   = get4byte(&aPa
16e7c 79 6c 6f 61 64 5b 70 43 75 72 2d 3e 69 6e 66 6f  yload[pCur->info
16e7d 2e 6e 4c 6f 63 61 6c 5d 29 3b 0a 0a 23 69 66 6e  .nLocal]);..#ifn
16e7e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
16e7f 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 2f 2a 20  INCRBLOB.    /* 
16e80 49 66 20 74 68 65 20 69 73 49 6e 63 72 62 6c 6f  If the isIncrblo
16e81 62 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20  bHandle flag is 
16e82 73 65 74 20 61 6e 64 20 74 68 65 20 42 74 43 75  set and the BtCu
16e83 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f 77 5b 5d  rsor.aOverflow[]
16e84 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20  .    ** has not 
16e85 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c 20  been allocated, 
16e86 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e  allocate it now.
16e87 20 54 68 65 20 61 72 72 61 79 20 69 73 20 73 69   The array is si
16e88 7a 65 64 20 61 74 0a 20 20 20 20 2a 2a 20 6f 6e  zed at.    ** on
16e89 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68  e entry for each
16e8a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
16e8b 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63  n the overflow c
16e8c 68 61 69 6e 2e 20 54 68 65 0a 20 20 20 20 2a 2a  hain. The.    **
16e8d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
16e8e 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c  the first overfl
16e8f 6f 77 20 70 61 67 65 20 69 73 20 73 74 6f 72 65  ow page is store
16e90 64 20 69 6e 20 61 4f 76 65 72 66 6c 6f 77 5b 30  d in aOverflow[0
16e91 5d 2c 0a 20 20 20 20 2a 2a 20 65 74 63 2e 20 41  ],.    ** etc. A
16e92 20 76 61 6c 75 65 20 6f 66 20 30 20 69 6e 20 74   value of 0 in t
16e93 68 65 20 61 4f 76 65 72 66 6c 6f 77 5b 5d 20 61  he aOverflow[] a
16e94 72 72 61 79 20 6d 65 61 6e 73 20 22 6e 6f 74 20  rray means "not 
16e95 79 65 74 20 6b 6e 6f 77 6e 22 0a 20 20 20 20 2a  yet known".    *
16e96 2a 20 28 74 68 65 20 63 61 63 68 65 20 69 73 20  * (the cache is 
16e97 6c 61 7a 69 6c 79 20 70 6f 70 75 6c 61 74 65 64  lazily populated
16e98 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  )..    */.    if
16e99 28 20 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c  ( pCur->isIncrbl
16e9a 6f 62 48 61 6e 64 6c 65 20 26 26 20 21 70 43 75  obHandle && !pCu
16e9b 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a  r->aOverflow ){.
16e9c 20 20 20 20 20 20 69 6e 74 20 6e 4f 76 66 6c 20        int nOvfl 
16e9d 3d 20 28 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 50  = (pCur->info.nP
16e9e 61 79 6c 6f 61 64 2d 70 43 75 72 2d 3e 69 6e 66  ayload-pCur->inf
16e9f 6f 2e 6e 4c 6f 63 61 6c 2b 6f 76 66 6c 53 69 7a  o.nLocal+ovflSiz
16ea0 65 2d 31 29 2f 6f 76 66 6c 53 69 7a 65 3b 0a 20  e-1)/ovflSize;. 
16ea1 20 20 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72       pCur->aOver
16ea2 66 6c 6f 77 20 3d 20 28 50 67 6e 6f 20 2a 29 73  flow = (Pgno *)s
16ea3 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
16ea4 28 73 69 7a 65 6f 66 28 50 67 6e 6f 29 2a 6e 4f  (sizeof(Pgno)*nO
16ea5 76 66 6c 29 3b 0a 20 20 20 20 20 20 2f 2a 20 6e  vfl);.      /* n
16ea6 4f 76 66 6c 20 69 73 20 61 6c 77 61 79 73 20 70  Ovfl is always p
16ea7 6f 73 69 74 69 76 65 2e 20 20 49 66 20 69 74 20  ositive.  If it 
16ea8 77 65 72 65 20 7a 65 72 6f 2c 20 66 65 74 63 68  were zero, fetch
16ea9 50 61 79 6c 6f 61 64 20 77 6f 75 6c 64 20 68 61  Payload would ha
16eaa 76 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 65 6e  ve.      ** been
16eab 20 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66   used instead of
16eac 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 2a   this routine. *
16ead 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41  /.      if( ALWA
16eae 59 53 28 6e 4f 76 66 6c 29 20 26 26 20 21 70 43  YS(nOvfl) && !pC
16eaf 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b  ur->aOverflow ){
16eb0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
16eb1 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
16eb2 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
16eb3 2a 20 49 66 20 74 68 65 20 6f 76 65 72 66 6c 6f  * If the overflo
16eb4 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68  w page-list cach
16eb5 65 20 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63  e has been alloc
16eb6 61 74 65 64 20 61 6e 64 20 74 68 65 0a 20 20 20  ated and the.   
16eb7 20 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74 68   ** entry for th
16eb8 65 20 66 69 72 73 74 20 72 65 71 75 69 72 65 64  e first required
16eb9 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
16eba 73 20 76 61 6c 69 64 2c 20 73 6b 69 70 0a 20 20  s valid, skip.  
16ebb 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74 6f    ** directly to
16ebc 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   it..    */.    
16ebd 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66  if( pCur->aOverf
16ebe 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76  low && pCur->aOv
16ebf 65 72 66 6c 6f 77 5b 6f 66 66 73 65 74 2f 6f 76  erflow[offset/ov
16ec0 66 6c 53 69 7a 65 5d 20 29 7b 0a 20 20 20 20 20  flSize] ){.     
16ec1 20 69 49 64 78 20 3d 20 28 6f 66 66 73 65 74 2f   iIdx = (offset/
16ec2 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 20  ovflSize);.     
16ec3 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75 72   nextPage = pCur
16ec4 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78  ->aOverflow[iIdx
16ec5 5d 3b 0a 20 20 20 20 20 20 6f 66 66 73 65 74 20  ];.      offset 
16ec6 3d 20 28 6f 66 66 73 65 74 25 6f 76 66 6c 53 69  = (offset%ovflSi
16ec7 7a 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ze);.    }.#endi
16ec8 66 0a 0a 20 20 20 20 66 6f 72 28 20 3b 20 72 63  f..    for( ; rc
16ec9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61  ==SQLITE_OK && a
16eca 6d 74 3e 30 20 26 26 20 6e 65 78 74 50 61 67 65  mt>0 && nextPage
16ecb 3b 20 69 49 64 78 2b 2b 29 7b 0a 0a 23 69 66 6e  ; iIdx++){..#ifn
16ecc 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
16ecd 49 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 2f  INCRBLOB.      /
16ece 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 70  * If required, p
16ecf 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 76 65 72  opulate the over
16ed0 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63  flow page-list c
16ed1 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20 20 20 69  ache. */.      i
16ed2 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  f( pCur->aOverfl
16ed3 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  ow ){.        as
16ed4 73 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65  sert(!pCur->aOve
16ed5 72 66 6c 6f 77 5b 69 49 64 78 5d 20 7c 7c 20 70  rflow[iIdx] || p
16ed6 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
16ed7 49 64 78 5d 3d 3d 6e 65 78 74 50 61 67 65 29 3b  Idx]==nextPage);
16ed8 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 61  .        pCur->a
16ed9 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 20 3d  Overflow[iIdx] =
16eda 20 6e 65 78 74 50 61 67 65 3b 0a 20 20 20 20 20   nextPage;.     
16edb 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20   }.#endif..     
16edc 20 69 66 28 20 6f 66 66 73 65 74 3e 3d 6f 76 66   if( offset>=ovf
16edd 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  lSize ){.       
16ede 20 2f 2a 20 54 68 65 20 6f 6e 6c 79 20 72 65 61   /* The only rea
16edf 73 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 69 73  son to read this
16ee0 20 70 61 67 65 20 69 73 20 74 6f 20 6f 62 74 61   page is to obta
16ee1 69 6e 20 74 68 65 20 70 61 67 65 0a 20 20 20 20  in the page.    
16ee2 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f      ** number fo
16ee3 72 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 20  r the next page 
16ee4 69 6e 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  in the overflow 
16ee5 63 68 61 69 6e 2e 20 54 68 65 20 70 61 67 65 0a  chain. The page.
16ee6 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74 61 20          ** data 
16ee7 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e  is not required.
16ee8 20 53 6f 20 66 69 72 73 74 20 74 72 79 20 74 6f   So first try to
16ee9 20 6c 6f 6f 6b 75 70 20 74 68 65 20 6f 76 65 72   lookup the over
16eea 66 6c 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a 20  flow.        ** 
16eeb 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2c  page-list cache,
16eec 20 69 66 20 61 6e 79 2c 20 74 68 65 6e 20 66 61   if any, then fa
16eed 6c 6c 20 62 61 63 6b 20 74 6f 20 74 68 65 20 67  ll back to the g
16eee 65 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 29  etOverflowPage()
16eef 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63  .        ** func
16ef0 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a 2f  tion..        */
16ef1 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
16ef2 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20 20  OMIT_INCRBLOB.  
16ef3 20 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e        if( pCur->
16ef4 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 75  aOverflow && pCu
16ef5 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64  r->aOverflow[iId
16ef6 78 2b 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  x+1] ){.        
16ef7 20 20 6e 65 78 74 50 61 67 65 20 3d 20 70 43 75    nextPage = pCu
16ef8 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64  r->aOverflow[iId
16ef9 78 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20 7d 20  x+1];.        } 
16efa 65 6c 73 65 20 0a 23 65 6e 64 69 66 0a 20 20 20  else .#endif.   
16efb 20 20 20 20 20 20 20 72 63 20 3d 20 67 65 74 4f         rc = getO
16efc 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42 74 2c  verflowPage(pBt,
16efd 20 6e 65 78 74 50 61 67 65 2c 20 30 2c 20 26 6e   nextPage, 0, &n
16efe 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20 20  extPage);.      
16eff 20 20 6f 66 66 73 65 74 20 2d 3d 20 6f 76 66 6c    offset -= ovfl
16f00 53 69 7a 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Size;.      }els
16f01 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  e{.        /* Ne
16f02 65 64 20 74 6f 20 72 65 61 64 20 74 68 69 73 20  ed to read this 
16f03 70 61 67 65 20 70 72 6f 70 65 72 6c 79 2e 20 49  page properly. I
16f04 74 20 63 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20  t contains some 
16f05 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  of the.        *
16f06 2a 20 72 61 6e 67 65 20 6f 66 20 64 61 74 61 20  * range of data 
16f07 74 68 61 74 20 69 73 20 62 65 69 6e 67 20 72 65  that is being re
16f08 61 64 20 28 65 4f 70 3d 3d 30 29 20 6f 72 20 77  ad (eOp==0) or w
16f09 72 69 74 74 65 6e 20 28 65 4f 70 21 3d 30 29 2e  ritten (eOp!=0).
16f0a 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
16f0b 20 20 20 20 44 62 50 61 67 65 20 2a 70 44 62 50      DbPage *pDbP
16f0c 61 67 65 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  age;.        int
16f0d 20 61 20 3d 20 61 6d 74 3b 0a 20 20 20 20 20 20   a = amt;.      
16f0e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
16f0f 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67  gerGet(pBt->pPag
16f10 65 72 2c 20 6e 65 78 74 50 61 67 65 2c 20 26 70  er, nextPage, &p
16f11 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
16f12 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
16f13 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
16f14 61 50 61 79 6c 6f 61 64 20 3d 20 73 71 6c 69 74  aPayload = sqlit
16f15 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
16f16 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
16f17 20 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65     nextPage = ge
16f18 74 34 62 79 74 65 28 61 50 61 79 6c 6f 61 64 29  t4byte(aPayload)
16f19 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
16f1a 61 20 2b 20 6f 66 66 73 65 74 20 3e 20 6f 76 66  a + offset > ovf
16f1b 6c 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  lSize ){.       
16f1c 20 20 20 20 20 61 20 3d 20 6f 76 66 6c 53 69 7a       a = ovflSiz
16f1d 65 20 2d 20 6f 66 66 73 65 74 3b 0a 20 20 20 20  e - offset;.    
16f1e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
16f1f 20 20 72 63 20 3d 20 63 6f 70 79 50 61 79 6c 6f    rc = copyPaylo
16f20 61 64 28 26 61 50 61 79 6c 6f 61 64 5b 6f 66 66  ad(&aPayload[off
16f21 73 65 74 2b 34 5d 2c 20 70 42 75 66 2c 20 61 2c  set+4], pBuf, a,
16f22 20 65 4f 70 2c 20 70 44 62 50 61 67 65 29 3b 0a   eOp, pDbPage);.
16f23 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
16f24 33 50 61 67 65 72 55 6e 72 65 66 28 70 44 62 50  3PagerUnref(pDbP
16f25 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
16f26 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20 20  offset = 0;.    
16f27 20 20 20 20 20 20 61 6d 74 20 2d 3d 20 61 3b 0a        amt -= a;.
16f28 20 20 20 20 20 20 20 20 20 20 70 42 75 66 20 2b            pBuf +
16f29 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = a;.        }. 
16f2a 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
16f2b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
16f2c 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 29  TE_OK && amt>0 )
16f2d 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
16f2e 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
16f2f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
16f30 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  c;.}../*.** Read
16f31 20 70 61 72 74 20 6f 66 20 74 68 65 20 6b 65 79   part of the key
16f32 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
16f33 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45   cursor pCur.  E
16f34 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20  xactly.** "amt" 
16f35 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72  bytes will be tr
16f36 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42  ansfered into pB
16f37 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73  uf[].  The trans
16f38 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74  fer.** begins at
16f39 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a   "offset"..**.**
16f3a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   The caller must
16f3b 20 65 6e 73 75 72 65 20 74 68 61 74 20 70 43 75   ensure that pCu
16f3c 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
16f3d 20 61 20 76 61 6c 69 64 20 72 6f 77 0a 2a 2a 20   a valid row.** 
16f3e 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a  in the table..**
16f3f 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
16f40 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20  E_OK on success 
16f41 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  or an error code
16f42 20 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65   if anything goe
16f43 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20  s.** wrong.  An 
16f44 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
16f45 64 20 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74  d if "offset+amt
16f46 22 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  " is larger than
16f47 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c  .** the availabl
16f48 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51  e payload..*/.SQ
16f49 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
16f4a 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79   sqlite3BtreeKey
16f4b 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
16f4c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32   u32 offset, u32
16f4d 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66   amt, void *pBuf
16f4e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  ){.  assert( cur
16f4f 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
16f50 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
16f51 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
16f52 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
16f53 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
16f54 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d  Page>=0 && pCur-
16f55 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
16f56 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74  age] );.  assert
16f57 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  ( pCur->aiIdx[pC
16f58 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d  ur->iPage]<pCur-
16f59 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
16f5a 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  age]->nCell );. 
16f5b 20 72 65 74 75 72 6e 20 61 63 63 65 73 73 50 61   return accessPa
16f5c 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73  yload(pCur, offs
16f5d 65 74 2c 20 61 6d 74 2c 20 28 75 6e 73 69 67 6e  et, amt, (unsign
16f5e 65 64 20 63 68 61 72 2a 29 70 42 75 66 2c 20 30  ed char*)pBuf, 0
16f5f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  );.}../*.** Read
16f60 20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74   part of the dat
16f61 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
16f62 68 20 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20  h cursor pCur.  
16f63 45 78 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22  Exactly.** "amt"
16f64 20 62 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74   bytes will be t
16f65 72 61 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70  ransfered into p
16f66 42 75 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e  Buf[].  The tran
16f67 73 66 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61  sfer.** begins a
16f68 74 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a  t "offset"..**.*
16f69 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * Return SQLITE_
16f6a 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72  OK on success or
16f6b 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
16f6c 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a  f anything goes.
16f6d 2a 2a 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72  ** wrong.  An er
16f6e 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20  ror is returned 
16f6f 69 66 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20  if "offset+amt" 
16f70 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a  is larger than.*
16f71 2a 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20  * the available 
16f72 70 61 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49  payload..*/.SQLI
16f73 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16f74 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
16f75 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
16f76 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20  u32 offset, u32 
16f77 61 6d 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29  amt, void *pBuf)
16f78 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66  {.  int rc;..#if
16f79 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16f7a 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28  _INCRBLOB.  if (
16f7b 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
16f7c 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
16f7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
16f7e 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65  TE_ABORT;.  }.#e
16f7f 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20  ndif..  assert( 
16f80 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
16f81 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d  (pCur) );.  rc =
16f82 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f   restoreCursorPo
16f83 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20  sition(pCur);.  
16f84 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16f85 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  K ){.    assert(
16f86 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
16f87 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20  URSOR_VALID );. 
16f88 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
16f89 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75  >iPage>=0 && pCu
16f8a 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
16f8b 69 50 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73  iPage] );.    as
16f8c 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64  sert( pCur->aiId
16f8d 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70  x[pCur->iPage]<p
16f8e 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
16f8f 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20  ->iPage]->nCell 
16f90 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65  );.    rc = acce
16f91 73 73 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20  ssPayload(pCur, 
16f92 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75  offset, amt, pBu
16f93 66 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74  f, 0);.  }.  ret
16f94 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
16f95 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
16f96 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e 66  r to payload inf
16f97 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68  ormation from th
16f98 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68 65  e entry that the
16f99 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f 72   .** pCur cursor
16f9a 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e   is pointing to.
16f9b 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69 73    The pointer is
16f9c 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   to the beginnin
16f9d 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20  g of.** the key 
16f9e 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61 6e  if skipKey==0 an
16f9f 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74  d it points to t
16fa0 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
16fa1 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70 4b  data if.** skipK
16fa2 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d 62  ey==1.  The numb
16fa3 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 61  er of bytes of a
16fa4 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61 74  vailable key/dat
16fa5 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  a is written.** 
16fa6 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66 20  into *pAmt.  If 
16fa7 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20 74  *pAmt==0, then t
16fa8 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
16fa9 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a 2a  d will not be.**
16faa 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65 72   a valid pointer
16fab 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
16fac 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69 6d  tine is an optim
16fad 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73 20  ization.  It is 
16fae 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20 65  common for the e
16faf 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e 64  ntire key.** and
16fb0 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e 20   data to fit on 
16fb1 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20 61  the local page a
16fb2 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20  nd for there to 
16fb3 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a  be no overflow.*
16fb4 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20 74  * pages.  When t
16fb5 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73 20  hat is so, this 
16fb6 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20 75  routine can be u
16fb7 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74 68  sed to access th
16fb8 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61 74  e.** key and dat
16fb9 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e 67  a without making
16fba 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68 65   a copy.  If the
16fbb 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74 61   key and/or data
16fbc 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f 20   spills.** onto 
16fbd 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c 20  overflow pages, 
16fbe 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c 6f  then accessPaylo
16fbf 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 73 65  ad() must be use
16fc0 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 65 0a  d to reassemble.
16fc1 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61 20  ** the key/data 
16fc2 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74 6f  and copy it into
16fc3 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 20   a preallocated 
16fc4 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  buffer..**.** Th
16fc5 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e  e pointer return
16fc6 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
16fc7 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74 6c  ne looks directl
16fc8 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68 65  y into the cache
16fc9 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68 65  d.** page of the
16fca 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20   database.  The 
16fcb 64 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e 67  data might chang
16fcc 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e 65  e or move the ne
16fcd 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20 62  xt time.** any b
16fce 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73 20  tree routine is 
16fcf 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  called..*/.stati
16fd0 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
16fd1 20 63 68 61 72 20 2a 66 65 74 63 68 50 61 79 6c   char *fetchPayl
16fd2 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72 20  oad(.  BtCursor 
16fd3 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20 43  *pCur,      /* C
16fd4 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74  ursor pointing t
16fd5 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64 20  o entry to read 
16fd6 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  from */.  int *p
16fd7 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Amt,           /
16fd8 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62  * Write the numb
16fd9 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20  er of available 
16fda 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20 20  bytes here */.  
16fdb 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20 20  int skipKey     
16fdc 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65 67       /* read beg
16fdd 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20 69  inning at data i
16fde 66 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a  f this is true *
16fdf 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  /.){.  unsigned 
16fe0 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a  char *aPayload;.
16fe1 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
16fe2 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20  ;.  u32 nKey;.  
16fe3 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20 61  u32 nLocal;..  a
16fe4 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 20 26  ssert( pCur!=0 &
16fe5 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d 30  & pCur->iPage>=0
16fe6 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67 65   && pCur->apPage
16fe7 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a  [pCur->iPage]);.
16fe8 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
16fe9 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
16fea 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74  ALID );.  assert
16feb 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
16fec 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70 50  ex(pCur) );.  pP
16fed 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
16fee 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  ge[pCur->iPage];
16fef 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
16ff0 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
16ff1 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ge]<pPage->nCell
16ff2 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28   );.  if( NEVER(
16ff3 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
16ff4 3d 3d 30 29 20 29 7b 0a 20 20 20 20 62 74 72 65  ==0) ){.    btre
16ff5 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d  eParseCell(pCur-
16ff6 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
16ff7 61 67 65 5d 2c 20 70 43 75 72 2d 3e 61 69 49 64  age], pCur->aiId
16ff8 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c 0a  x[pCur->iPage],.
16ff9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ffa 20 20 20 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b     &pCur->info);
16ffb 0a 20 20 7d 0a 20 20 61 50 61 79 6c 6f 61 64 20  .  }.  aPayload 
16ffc 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70 43 65  = pCur->info.pCe
16ffd 6c 6c 3b 0a 20 20 61 50 61 79 6c 6f 61 64 20 2b  ll;.  aPayload +
16ffe 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 48 65  = pCur->info.nHe
16fff 61 64 65 72 3b 0a 20 20 69 66 28 20 70 50 61 67  ader;.  if( pPag
17000 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  e->intKey ){.   
17001 20 6e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 65 6c   nKey = 0;.  }el
17002 73 65 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 28  se{.    nKey = (
17003 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  int)pCur->info.n
17004 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  Key;.  }.  if( s
17005 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 50  kipKey ){.    aP
17006 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a  ayload += nKey;.
17007 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75      nLocal = pCu
17008 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d  r->info.nLocal -
17009 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a   nKey;.  }else{.
1700a 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75      nLocal = pCu
1700b 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a  r->info.nLocal;.
1700c 20 20 20 20 61 73 73 65 72 74 28 20 6e 4c 6f 63      assert( nLoc
1700d 61 6c 3c 3d 6e 4b 65 79 20 29 3b 0a 20 20 7d 0a  al<=nKey );.  }.
1700e 20 20 2a 70 41 6d 74 20 3d 20 6e 4c 6f 63 61 6c    *pAmt = nLocal
1700f 3b 0a 20 20 72 65 74 75 72 6e 20 61 50 61 79 6c  ;.  return aPayl
17010 6f 61 64 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46  oad;.}.../*.** F
17011 6f 72 20 74 68 65 20 65 6e 74 72 79 20 74 68 61  or the entry tha
17012 74 20 63 75 72 73 6f 72 20 70 43 75 72 20 69 73  t cursor pCur is
17013 20 70 6f 69 6e 74 20 74 6f 2c 20 72 65 74 75 72   point to, retur
17014 6e 20 61 73 0a 2a 2a 20 6d 61 6e 79 20 62 79 74  n as.** many byt
17015 65 73 20 6f 66 20 74 68 65 20 6b 65 79 20 6f 72  es of the key or
17016 20 64 61 74 61 20 61 73 20 61 72 65 20 61 76 61   data as are ava
17017 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f  ilable on the lo
17018 63 61 6c 0a 2a 2a 20 62 2d 74 72 65 65 20 70 61  cal.** b-tree pa
17019 67 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 6e  ge.  Write the n
1701a 75 6d 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62  umber of availab
1701b 6c 65 20 62 79 74 65 73 20 69 6e 74 6f 20 2a 70  le bytes into *p
1701c 41 6d 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  Amt..**.** The p
1701d 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64 20  ointer returned 
1701e 69 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 54  is ephemeral.  T
1701f 68 65 20 6b 65 79 2f 64 61 74 61 20 6d 61 79 20  he key/data may 
17020 6d 6f 76 65 0a 2a 2a 20 6f 72 20 62 65 20 64 65  move.** or be de
17021 73 74 72 6f 79 65 64 20 6f 6e 20 74 68 65 20 6e  stroyed on the n
17022 65 78 74 20 63 61 6c 6c 20 74 6f 20 61 6e 79 20  ext call to any 
17023 42 74 72 65 65 20 72 6f 75 74 69 6e 65 2c 0a 2a  Btree routine,.*
17024 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c  * including call
17025 73 20 66 72 6f 6d 20 6f 74 68 65 72 20 74 68 72  s from other thr
17026 65 61 64 73 20 61 67 61 69 6e 73 74 20 74 68 65  eads against the
17027 20 73 61 6d 65 20 63 61 63 68 65 2e 0a 2a 2a 20   same cache..** 
17028 48 65 6e 63 65 2c 20 61 20 6d 75 74 65 78 20 6f  Hence, a mutex o
17029 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20 73  n the BtShared s
1702a 68 6f 75 6c 64 20 62 65 20 68 65 6c 64 20 70 72  hould be held pr
1702b 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a  ior to calling.*
1702c 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a  * this routine..
1702d 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
1702e 69 6e 65 73 20 69 73 20 75 73 65 64 20 74 6f 20  ines is used to 
1702f 67 65 74 20 71 75 69 63 6b 20 61 63 63 65 73 73  get quick access
17030 20 74 6f 20 6b 65 79 20 61 6e 64 20 64 61 74 61   to key and data
17031 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f  .** in the commo
17032 6e 20 63 61 73 65 20 77 68 65 72 65 20 6e 6f 20  n case where no 
17033 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61  overflow pages a
17034 72 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49  re used..*/.SQLI
17035 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
17036 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74   void *sqlite3Bt
17037 72 65 65 4b 65 79 46 65 74 63 68 28 42 74 43 75  reeKeyFetch(BtCu
17038 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
17039 2a 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20  *pAmt){.  const 
1703a 76 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61  void *p = 0;.  a
1703b 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1703c 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e  utex_held(pCur->
1703d 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65  pBtree->db->mute
1703e 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
1703f 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
17040 28 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20  (pCur) );.  if( 
17041 41 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74  ALWAYS(pCur->eSt
17042 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
17043 44 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63  D) ){.    p = (c
17044 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68  onst void*)fetch
17045 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41  Payload(pCur, pA
17046 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65  mt, 0);.  }.  re
17047 74 75 72 6e 20 70 3b 0a 7d 0a 53 51 4c 49 54 45  turn p;.}.SQLITE
17048 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76  _PRIVATE const v
17049 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65  oid *sqlite3Btre
1704a 65 44 61 74 61 46 65 74 63 68 28 42 74 43 75 72  eDataFetch(BtCur
1704b 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
1704c 70 41 6d 74 29 7b 0a 20 20 63 6f 6e 73 74 20 76  pAmt){.  const v
1704d 6f 69 64 20 2a 70 20 3d 20 30 3b 0a 20 20 61 73  oid *p = 0;.  as
1704e 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1704f 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70  tex_held(pCur->p
17050 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78  Btree->db->mutex
17051 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63  ) );.  assert( c
17052 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
17053 70 43 75 72 29 20 29 3b 0a 20 20 69 66 28 20 41  pCur) );.  if( A
17054 4c 57 41 59 53 28 70 43 75 72 2d 3e 65 53 74 61  LWAYS(pCur->eSta
17055 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
17056 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 28 63 6f  ) ){.    p = (co
17057 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50  nst void*)fetchP
17058 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d  ayload(pCur, pAm
17059 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74  t, 1);.  }.  ret
1705a 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn p;.}.../*.**
1705b 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72   Move the cursor
1705c 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 63   down to a new c
1705d 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 20  hild page.  The 
1705e 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e 74  newPgno argument
1705f 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20   is the.** page 
17060 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63 68  number of the ch
17061 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 65  ild page to move
17062 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20   to..**.** This 
17063 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
17064 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
17065 69 66 20 74 68 65 20 70 61 67 65 2d 68 65 61 64  if the page-head
17066 65 72 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f  er flags field o
17067 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 63 68 69  f.** the new chi
17068 6c 64 20 70 61 67 65 20 64 6f 65 73 20 6e 6f 74  ld page does not
17069 20 6d 61 74 63 68 20 74 68 65 20 66 6c 61 67 73   match the flags
1706a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 70 61   field of the pa
1706b 72 65 6e 74 20 28 69 2e 65 2e 0a 2a 2a 20 69 66  rent (i.e..** if
1706c 20 61 6e 20 69 6e 74 6b 65 79 20 70 61 67 65 20   an intkey page 
1706d 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 74 68  appears to be th
1706e 65 20 70 61 72 65 6e 74 20 6f 66 20 61 20 6e 6f  e parent of a no
1706f 6e 2d 69 6e 74 6b 65 79 20 70 61 67 65 2c 20 6f  n-intkey page, o
17070 72 0a 2a 2a 20 76 69 63 65 2d 76 65 72 73 61 29  r.** vice-versa)
17071 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
17072 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42 74 43 75  moveToChild(BtCu
17073 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20  rsor *pCur, u32 
17074 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69 6e 74 20  newPgno){.  int 
17075 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d 20 70 43  rc;.  int i = pC
17076 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 4d 65 6d  ur->iPage;.  Mem
17077 50 61 67 65 20 2a 70 4e 65 77 50 61 67 65 3b 0a  Page *pNewPage;.
17078 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
17079 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a 0a 20 20  = pCur->pBt;..  
1707a 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
1707b 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
1707c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
1707d 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
1707e 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65  _VALID );.  asse
1707f 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3c  rt( pCur->iPage<
17080 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50  BTCURSOR_MAX_DEP
17081 54 48 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72  TH );.  if( pCur
17082 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43 55 52 53  ->iPage>=(BTCURS
17083 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d 31 29 20  OR_MAX_DEPTH-1) 
17084 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
17085 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
17086 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67 65  T;.  }.  rc = ge
17087 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74  tAndInitPage(pBt
17088 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70 4e 65 77  , newPgno, &pNew
17089 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20  Page);.  if( rc 
1708a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 70  ) return rc;.  p
1708b 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 2b 31 5d  Cur->apPage[i+1]
1708c 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a 20 20 70   = pNewPage;.  p
1708d 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b 31 5d 20  Cur->aiIdx[i+1] 
1708e 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61  = 0;.  pCur->iPa
1708f 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72 2d 3e 69  ge++;..  pCur->i
17090 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20  nfo.nSize = 0;. 
17091 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79   pCur->validNKey
17092 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4e 65 77   = 0;.  if( pNew
17093 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31 20 7c 7c  Page->nCell<1 ||
17094 20 70 4e 65 77 50 61 67 65 2d 3e 69 6e 74 4b 65   pNewPage->intKe
17095 79 21 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  y!=pCur->apPage[
17096 69 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20  i]->intKey ){.  
17097 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
17098 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
17099 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
1709a 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  E_OK;.}..#ifndef
1709b 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 61   NDEBUG./*.** Pa
1709c 67 65 20 70 50 61 72 65 6e 74 20 69 73 20 61 6e  ge pParent is an
1709d 20 69 6e 74 65 72 6e 61 6c 20 28 6e 6f 6e 2d 6c   internal (non-l
1709e 65 61 66 29 20 74 72 65 65 20 70 61 67 65 2e 20  eaf) tree page. 
1709f 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 0a 2a  This function .*
170a0 2a 20 61 73 73 65 72 74 73 20 74 68 61 74 20 70  * asserts that p
170a1 61 67 65 20 6e 75 6d 62 65 72 20 69 43 68 69 6c  age number iChil
170a2 64 20 69 73 20 74 68 65 20 6c 65 66 74 2d 63 68  d is the left-ch
170a3 69 6c 64 20 69 66 20 74 68 65 20 69 49 64 78 27  ild if the iIdx'
170a4 74 68 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 70 61  th.** cell in pa
170a5 67 65 20 70 50 61 72 65 6e 74 2e 20 4f 72 2c 20  ge pParent. Or, 
170a6 69 66 20 69 49 64 78 20 69 73 20 65 71 75 61 6c  if iIdx is equal
170a7 20 74 6f 20 74 68 65 20 74 6f 74 61 6c 20 6e 75   to the total nu
170a8 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73  mber of.** cells
170a9 20 69 6e 20 70 50 61 72 65 6e 74 2c 20 74 68 61   in pParent, tha
170aa 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 43  t page number iC
170ab 68 69 6c 64 20 69 73 20 74 68 65 20 72 69 67 68  hild is the righ
170ac 74 2d 63 68 69 6c 64 20 6f 66 0a 2a 2a 20 74 68  t-child of.** th
170ad 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69  e page..*/.stati
170ae 63 20 76 6f 69 64 20 61 73 73 65 72 74 50 61 72  c void assertPar
170af 65 6e 74 49 6e 64 65 78 28 4d 65 6d 50 61 67 65  entIndex(MemPage
170b0 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 20 69   *pParent, int i
170b1 49 64 78 2c 20 50 67 6e 6f 20 69 43 68 69 6c 64  Idx, Pgno iChild
170b2 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69 49 64  ){.  assert( iId
170b3 78 3c 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c  x<=pParent->nCel
170b4 6c 20 29 3b 0a 20 20 69 66 28 20 69 49 64 78 3d  l );.  if( iIdx=
170b5 3d 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20  =pParent->nCell 
170b6 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67  ){.    assert( g
170b7 65 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74  et4byte(&pParent
170b8 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d  ->aData[pParent-
170b9 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3d 3d  >hdrOffset+8])==
170ba 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 65 6c 73  iChild );.  }els
170bb 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 67  e{.    assert( g
170bc 65 74 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c  et4byte(findCell
170bd 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78 29 29  (pParent, iIdx))
170be 3d 3d 69 43 68 69 6c 64 20 29 3b 0a 20 20 7d 0a  ==iChild );.  }.
170bf 7d 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  }.#else.#  defin
170c0 65 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e  e assertParentIn
170c1 64 65 78 28 78 2c 79 2c 7a 29 20 0a 23 65 6e 64  dex(x,y,z) .#end
170c2 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74  if../*.** Move t
170c3 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 6f 20  he cursor up to 
170c4 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e  the parent page.
170c5 0a 2a 2a 0a 2a 2a 20 70 43 75 72 2d 3e 69 64 78  .**.** pCur->idx
170c6 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 63   is set to the c
170c7 65 6c 6c 20 69 6e 64 65 78 20 74 68 61 74 20 63  ell index that c
170c8 6f 6e 74 61 69 6e 73 20 74 68 65 20 70 6f 69 6e  ontains the poin
170c9 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61  ter.** to the pa
170ca 67 65 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67  ge we are coming
170cb 20 66 72 6f 6d 2e 20 20 49 66 20 77 65 20 61 72   from.  If we ar
170cc 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 74 68  e coming from th
170cd 65 0a 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20  e.** right-most 
170ce 63 68 69 6c 64 20 70 61 67 65 20 74 68 65 6e 20  child page then 
170cf 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65 74  pCur->idx is set
170d0 20 74 6f 20 6f 6e 65 20 6d 6f 72 65 20 74 68 61   to one more tha
170d1 6e 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74  n.** the largest
170d2 20 63 65 6c 6c 20 69 6e 64 65 78 2e 0a 2a 2f 0a   cell index..*/.
170d3 73 74 61 74 69 63 20 76 6f 69 64 20 6d 6f 76 65  static void move
170d4 54 6f 50 61 72 65 6e 74 28 42 74 43 75 72 73 6f  ToParent(BtCurso
170d5 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73 65  r *pCur){.  asse
170d6 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
170d7 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
170d8 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53  assert( pCur->eS
170d9 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
170da 49 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ID );.  assert( 
170db 70 43 75 72 2d 3e 69 50 61 67 65 3e 30 20 29 3b  pCur->iPage>0 );
170dc 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
170dd 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
170de 61 67 65 5d 20 29 3b 0a 20 20 61 73 73 65 72 74  age] );.  assert
170df 50 61 72 65 6e 74 49 6e 64 65 78 28 0a 20 20 20  ParentIndex(.   
170e0 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
170e1 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a 20  ur->iPage-1], . 
170e2 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70     pCur->aiIdx[p
170e3 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 2c 20 0a  Cur->iPage-1], .
170e4 20 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65      pCur->apPage
170e5 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 70  [pCur->iPage]->p
170e6 67 6e 6f 0a 20 20 29 3b 0a 20 20 72 65 6c 65 61  gno.  );.  relea
170e7 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50  sePage(pCur->apP
170e8 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
170e9 29 3b 0a 20 20 70 43 75 72 2d 3e 69 50 61 67 65  );.  pCur->iPage
170ea 2d 2d 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f  --;.  pCur->info
170eb 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43  .nSize = 0;.  pC
170ec 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20  ur->validNKey = 
170ed 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65  0;.}../*.** Move
170ee 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 70   the cursor to p
170ef 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 6f 6f 74  oint to the root
170f0 20 70 61 67 65 20 6f 66 20 69 74 73 20 62 2d 74   page of its b-t
170f1 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ree structure..*
170f2 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c  *.** If the tabl
170f3 65 20 68 61 73 20 61 20 76 69 72 74 75 61 6c 20  e has a virtual 
170f4 72 6f 6f 74 20 70 61 67 65 2c 20 74 68 65 6e 20  root page, then 
170f5 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 6d 6f  the cursor is mo
170f6 76 65 64 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20  ved to point.** 
170f7 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 72  to the virtual r
170f8 6f 6f 74 20 70 61 67 65 20 69 6e 73 74 65 61 64  oot page instead
170f9 20 6f 66 20 74 68 65 20 61 63 74 75 61 6c 20 72   of the actual r
170fa 6f 6f 74 20 70 61 67 65 2e 20 41 20 74 61 62 6c  oot page. A tabl
170fb 65 20 68 61 73 20 61 0a 2a 2a 20 76 69 72 74 75  e has a.** virtu
170fc 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 77 68 65  al root page whe
170fd 6e 20 74 68 65 20 61 63 74 75 61 6c 20 72 6f 6f  n the actual roo
170fe 74 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 73 20  t page contains 
170ff 6e 6f 20 63 65 6c 6c 73 20 61 6e 64 20 61 20 0a  no cells and a .
17100 2a 2a 20 73 69 6e 67 6c 65 20 63 68 69 6c 64 20  ** single child 
17101 70 61 67 65 2e 20 54 68 69 73 20 63 61 6e 20 6f  page. This can o
17102 6e 6c 79 20 68 61 70 70 65 6e 20 77 69 74 68 20  nly happen with 
17103 74 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64  the table rooted
17104 20 61 74 20 70 61 67 65 20 31 2e 0a 2a 2a 0a 2a   at page 1..**.*
17105 2a 20 49 66 20 74 68 65 20 62 2d 74 72 65 65 20  * If the b-tree 
17106 73 74 72 75 63 74 75 72 65 20 69 73 20 65 6d 70  structure is emp
17107 74 79 2c 20 74 68 65 20 63 75 72 73 6f 72 20 73  ty, the cursor s
17108 74 61 74 65 20 69 73 20 73 65 74 20 74 6f 20 0a  tate is set to .
17109 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  ** CURSOR_INVALI
1710a 44 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68  D. Otherwise, th
1710b 65 20 63 75 72 73 6f 72 20 69 73 20 73 65 74 20  e cursor is set 
1710c 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
1710d 66 69 72 73 74 0a 2a 2a 20 63 65 6c 6c 20 6c 6f  first.** cell lo
1710e 63 61 74 65 64 20 6f 6e 20 74 68 65 20 72 6f 6f  cated on the roo
1710f 74 20 28 6f 72 20 76 69 72 74 75 61 6c 20 72 6f  t (or virtual ro
17110 6f 74 29 20 70 61 67 65 20 61 6e 64 20 74 68 65  ot) page and the
17111 20 63 75 72 73 6f 72 20 73 74 61 74 65 0a 2a 2a   cursor state.**
17112 20 69 73 20 73 65 74 20 74 6f 20 43 55 52 53 4f   is set to CURSO
17113 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20 49  R_VALID..**.** I
17114 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  f this function 
17115 72 65 74 75 72 6e 73 20 73 75 63 63 65 73 73 66  returns successf
17116 75 6c 6c 79 2c 20 69 74 20 6d 61 79 20 62 65 20  ully, it may be 
17117 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65  assumed that the
17118 0a 2a 2a 20 70 61 67 65 2d 68 65 61 64 65 72 20  .** page-header 
17119 66 6c 61 67 73 20 69 6e 64 69 63 61 74 65 20 74  flags indicate t
1711a 68 61 74 20 74 68 65 20 5b 76 69 72 74 75 61 6c  hat the [virtual
1711b 5d 20 72 6f 6f 74 2d 70 61 67 65 20 69 73 20 74  ] root-page is t
1711c 68 65 20 65 78 70 65 63 74 65 64 20 0a 2a 2a 20  he expected .** 
1711d 6b 69 6e 64 20 6f 66 20 62 2d 74 72 65 65 20 70  kind of b-tree p
1711e 61 67 65 20 28 69 2e 65 2e 20 69 66 20 77 68 65  age (i.e. if whe
1711f 6e 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 63 75  n opening the cu
17120 72 73 6f 72 20 74 68 65 20 63 61 6c 6c 65 72 20  rsor the caller 
17121 64 69 64 20 6e 6f 74 0a 2a 2a 20 73 70 65 63 69  did not.** speci
17122 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72  fy a KeyInfo str
17123 75 63 74 75 72 65 20 74 68 65 20 66 6c 61 67 73  ucture the flags
17124 20 62 79 74 65 20 69 73 20 73 65 74 20 74 6f 20   byte is set to 
17125 30 78 30 35 20 6f 72 20 30 78 30 44 2c 0a 2a 2a  0x05 or 0x0D,.**
17126 20 69 6e 64 69 63 61 74 69 6e 67 20 61 20 74 61   indicating a ta
17127 62 6c 65 20 62 2d 74 72 65 65 2c 20 6f 72 20 69  ble b-tree, or i
17128 66 20 74 68 65 20 63 61 6c 6c 65 72 20 64 69 64  f the caller did
17129 20 73 70 65 63 69 66 79 20 61 20 4b 65 79 49 6e   specify a KeyIn
1712a 66 6f 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  fo .** structure
1712b 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20   the flags byte 
1712c 69 73 20 73 65 74 20 74 6f 20 30 78 30 32 20 6f  is set to 0x02 o
1712d 72 20 30 78 30 41 2c 20 69 6e 64 69 63 61 74 69  r 0x0A, indicati
1712e 6e 67 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 62  ng an index.** b
1712f 2d 74 72 65 65 29 2e 0a 2a 2f 0a 73 74 61 74 69  -tree)..*/.stati
17130 63 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f 74  c int moveToRoot
17131 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
17132 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f  {.  MemPage *pRo
17133 6f 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ot;.  int rc = S
17134 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72 65  QLITE_OK;.  Btre
17135 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74  e *p = pCur->pBt
17136 72 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20  ree;.  BtShared 
17137 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a  *pBt = p->pBt;..
17138 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
17139 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
1713a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55   );.  assert( CU
1713b 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20 43  RSOR_INVALID < C
1713c 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
1713d 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43  K );.  assert( C
1713e 55 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c 20  URSOR_VALID   < 
1713f 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
17140 45 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  EK );.  assert( 
17141 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 3e  CURSOR_FAULT   >
17142 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
17143 45 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75  EEK );.  if( pCu
17144 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f  r->eState>=CURSO
17145 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 7b  R_REQUIRESEEK ){
17146 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65  .    if( pCur->e
17147 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41  State==CURSOR_FA
17148 55 4c 54 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ULT ){.      ass
17149 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e  ert( pCur->skipN
1714a 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  ext!=SQLITE_OK )
1714b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  ;.      return p
1714c 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20  Cur->skipNext;. 
1714d 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1714e 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72  BtreeClearCursor
1714f 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69  (pCur);.  }..  i
17150 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d  f( pCur->iPage>=
17151 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  0 ){.    int i;.
17152 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d      for(i=1; i<=
17153 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b  pCur->iPage; i++
17154 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ){.      release
17155 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67  Page(pCur->apPag
17156 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20  e[i]);.    }.   
17157 20 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 30   pCur->iPage = 0
17158 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
17159 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74 50 61  c = getAndInitPa
1715a 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67  ge(pBt, pCur->pg
1715b 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61  noRoot, &pCur->a
1715c 70 50 61 67 65 5b 30 5d 29 3b 0a 20 20 20 20 69  pPage[0]);.    i
1715d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1715e 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e   ){.      pCur->
1715f 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
17160 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 72  INVALID;.      r
17161 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
17162 20 20 20 20 70 43 75 72 2d 3e 69 50 61 67 65 20      pCur->iPage 
17163 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  = 0;..    /* If 
17164 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 69  pCur->pKeyInfo i
17165 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
17166 20 74 68 65 20 63 61 6c 6c 65 72 20 74 68 61 74   the caller that
17167 20 6f 70 65 6e 65 64 20 74 68 69 73 20 63 75 72   opened this cur
17168 73 6f 72 0a 20 20 20 20 2a 2a 20 65 78 70 65 63  sor.    ** expec
17169 74 65 64 20 74 6f 20 6f 70 65 6e 20 69 74 20 6f  ted to open it o
1716a 6e 20 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65  n an index b-tre
1716b 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  e. Otherwise, if
1716c 20 70 4b 65 79 49 6e 66 6f 20 69 73 0a 20 20 20   pKeyInfo is.   
1716d 20 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 63 61   ** NULL, the ca
1716e 6c 6c 65 72 20 65 78 70 65 63 74 73 20 61 20 74  ller expects a t
1716f 61 62 6c 65 20 62 2d 74 72 65 65 2e 20 49 66 20  able b-tree. If 
17170 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68 65 20  this is not the 
17171 63 61 73 65 2c 0a 20 20 20 20 2a 2a 20 72 65 74  case,.    ** ret
17172 75 72 6e 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f  urn an SQLITE_CO
17173 52 52 55 50 54 20 65 72 72 6f 72 2e 20 20 2a 2f  RRUPT error.  */
17174 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
17175 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e  r->apPage[0]->in
17176 74 4b 65 79 3d 3d 31 20 7c 7c 20 70 43 75 72 2d  tKey==1 || pCur-
17177 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b  >apPage[0]->intK
17178 65 79 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  ey==0 );.    if(
17179 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f   (pCur->pKeyInfo
1717a 3d 3d 30 29 21 3d 70 43 75 72 2d 3e 61 70 50 61  ==0)!=pCur->apPa
1717b 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 29 7b  ge[0]->intKey ){
1717c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
1717d 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1717e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  T;.    }.  }..  
1717f 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74 20 74  /* Assert that t
17180 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20  he root page is 
17181 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74 20 74  of the correct t
17182 79 70 65 2e 20 54 68 69 73 20 6d 75 73 74 20 62  ype. This must b
17183 65 20 74 68 65 0a 20 20 2a 2a 20 63 61 73 65 20  e the.  ** case 
17184 61 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 74  as the call to t
17185 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  his function tha
17186 74 20 6c 6f 61 64 65 64 20 74 68 65 20 72 6f 6f  t loaded the roo
17187 74 2d 70 61 67 65 20 28 65 69 74 68 65 72 0a 20  t-page (either. 
17188 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 6f 72   ** this call or
17189 20 61 20 70 72 65 76 69 6f 75 73 20 69 6e 76 6f   a previous invo
1718a 63 61 74 69 6f 6e 29 20 77 6f 75 6c 64 20 68 61  cation) would ha
1718b 76 65 20 64 65 74 65 63 74 65 64 20 63 6f 72 72  ve detected corr
1718c 75 70 74 69 6f 6e 20 0a 20 20 2a 2a 20 69 66 20  uption .  ** if 
1718d 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e 20 77  the assumption w
1718e 65 72 65 20 6e 6f 74 20 74 72 75 65 2c 20 61 6e  ere not true, an
1718f 64 20 69 74 20 69 73 20 6e 6f 74 20 70 6f 73 73  d it is not poss
17190 69 62 6c 65 20 66 6f 72 20 74 68 65 20 66 6c 61  ible for the fla
17191 67 73 20 0a 20 20 2a 2a 20 62 79 74 65 20 74 6f  gs .  ** byte to
17192 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66   have been modif
17193 69 65 64 20 77 68 69 6c 65 20 74 68 69 73 20 63  ied while this c
17194 75 72 73 6f 72 20 69 73 20 68 6f 6c 64 69 6e 67  ursor is holding
17195 20 61 20 72 65 66 65 72 65 6e 63 65 0a 20 20 2a   a reference.  *
17196 2a 20 74 6f 20 74 68 65 20 70 61 67 65 2e 20 20  * to the page.  
17197 2a 2f 0a 20 20 70 52 6f 6f 74 20 3d 20 70 43 75  */.  pRoot = pCu
17198 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a 20 20  r->apPage[0];.  
17199 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 70  assert( pRoot->p
1719a 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e 6f 52  gno==pCur->pgnoR
1719b 6f 6f 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  oot );.  assert(
1719c 20 70 52 6f 6f 74 2d 3e 69 73 49 6e 69 74 20 26   pRoot->isInit &
1719d 26 20 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66  & (pCur->pKeyInf
1719e 6f 3d 3d 30 29 3d 3d 70 52 6f 6f 74 2d 3e 69 6e  o==0)==pRoot->in
1719f 74 4b 65 79 20 29 3b 0a 0a 20 20 70 43 75 72 2d  tKey );..  pCur-
171a0 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30 3b 0a 20  >aiIdx[0] = 0;. 
171a1 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
171a2 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 61  e = 0;.  pCur->a
171a3 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20 70 43 75  tLast = 0;.  pCu
171a4 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30  r->validNKey = 0
171a5 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e  ;..  if( pRoot->
171a6 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21 70 52 6f  nCell==0 && !pRo
171a7 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ot->leaf ){.    
171a8 50 67 6e 6f 20 73 75 62 70 61 67 65 3b 0a 20 20  Pgno subpage;.  
171a9 20 20 69 66 28 20 70 52 6f 6f 74 2d 3e 70 67 6e    if( pRoot->pgn
171aa 6f 21 3d 31 20 29 20 72 65 74 75 72 6e 20 53 51  o!=1 ) return SQ
171ab 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
171ac 54 3b 0a 20 20 20 20 73 75 62 70 61 67 65 20 3d  T;.    subpage =
171ad 20 67 65 74 34 62 79 74 65 28 26 70 52 6f 6f 74   get4byte(&pRoot
171ae 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68  ->aData[pRoot->h
171af 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
171b0 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
171b1 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3b 0a 20   CURSOR_VALID;. 
171b2 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
171b3 69 6c 64 28 70 43 75 72 2c 20 73 75 62 70 61 67  ild(pCur, subpag
171b4 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
171b5 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
171b6 28 28 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3e 30  ((pRoot->nCell>0
171b7 29 3f 43 55 52 53 4f 52 5f 56 41 4c 49 44 3a 43  )?CURSOR_VALID:C
171b8 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 29 3b 0a  URSOR_INVALID);.
171b9 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
171ba 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74  .}../*.** Move t
171bb 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74  he cursor down t
171bc 6f 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20  o the left-most 
171bd 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61  leaf entry benea
171be 74 68 20 74 68 65 0a 2a 2a 20 65 6e 74 72 79 20  th the.** entry 
171bf 74 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63  to which it is c
171c0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
171c1 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 66  g..**.** The lef
171c2 74 2d 6d 6f 73 74 20 6c 65 61 66 20 69 73 20 74  t-most leaf is t
171c3 68 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20  he one with the 
171c4 73 6d 61 6c 6c 65 73 74 20 6b 65 79 20 2d 20 74  smallest key - t
171c5 68 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 20 61  he first.** in a
171c6 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e 0a  scending order..
171c7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f  */.static int mo
171c8 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 42 74 43  veToLeftmost(BtC
171c9 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
171ca 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74  Pgno pgno;.  int
171cb 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
171cc 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
171cd 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  e;..  assert( cu
171ce 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
171cf 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
171d0 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
171d1 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
171d2 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c    while( rc==SQL
171d3 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61 67  ITE_OK && !(pPag
171d4 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
171d5 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d 3e  [pCur->iPage])->
171d6 6c 65 61 66 20 29 7b 0a 20 20 20 20 61 73 73 65  leaf ){.    asse
171d7 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
171d8 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61  pCur->iPage]<pPa
171d9 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20  ge->nCell );.   
171da 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65   pgno = get4byte
171db 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c  (findCell(pPage,
171dc 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
171dd 72 2d 3e 69 50 61 67 65 5d 29 29 3b 0a 20 20 20  r->iPage]));.   
171de 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c   rc = moveToChil
171df 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a 20  d(pCur, pgno);. 
171e0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
171e1 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68  }../*.** Move th
171e2 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20 74 6f  e cursor down to
171e3 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   the right-most 
171e4 6c 65 61 66 20 65 6e 74 72 79 20 62 65 6e 65 61  leaf entry benea
171e5 74 68 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 74  th the.** page t
171e6 6f 20 77 68 69 63 68 20 69 74 20 69 73 20 63 75  o which it is cu
171e7 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
171e8 2e 20 20 4e 6f 74 69 63 65 20 74 68 65 20 64 69  .  Notice the di
171e9 66 66 65 72 65 6e 63 65 0a 2a 2a 20 62 65 74 77  fference.** betw
171ea 65 65 6e 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f  een moveToLeftmo
171eb 73 74 28 29 20 61 6e 64 20 6d 6f 76 65 54 6f 52  st() and moveToR
171ec 69 67 68 74 6d 6f 73 74 28 29 2e 20 20 6d 6f 76  ightmost().  mov
171ed 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 0a 2a 2a  eToLeftmost().**
171ee 20 66 69 6e 64 73 20 74 68 65 20 6c 65 66 74 2d   finds the left-
171ef 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65 61  most entry benea
171f0 74 68 20 74 68 65 20 2a 65 6e 74 72 79 2a 20 77  th the *entry* w
171f1 68 65 72 65 61 73 20 6d 6f 76 65 54 6f 52 69 67  hereas moveToRig
171f2 68 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64  htmost().** find
171f3 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  s the right-most
171f4 20 65 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74   entry beneath t
171f5 68 65 20 2a 70 61 67 65 2a 2e 0a 2a 2a 0a 2a 2a  he *page*..**.**
171f6 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   The right-most 
171f7 65 6e 74 72 79 20 69 73 20 74 68 65 20 6f 6e 65  entry is the one
171f8 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 65 73   with the larges
171f9 74 20 6b 65 79 20 2d 20 74 68 65 20 6c 61 73 74  t key - the last
171fa 0a 2a 2a 20 6b 65 79 20 69 6e 20 61 73 63 65 6e  .** key in ascen
171fb 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73  ding order..*/.s
171fc 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f  tatic int moveTo
171fd 52 69 67 68 74 6d 6f 73 74 28 42 74 43 75 72 73  Rightmost(BtCurs
171fe 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e  or *pCur){.  Pgn
171ff 6f 20 70 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63  o pgno;.  int rc
17200 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
17201 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d  MemPage *pPage =
17202 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63   0;..  assert( c
17203 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
17204 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
17205 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
17206 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
17207 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51  .  while( rc==SQ
17208 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50 61  LITE_OK && !(pPa
17209 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
1720a 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 2d  e[pCur->iPage])-
1720b 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67 6e  >leaf ){.    pgn
1720c 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50  o = get4byte(&pP
1720d 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65  age->aData[pPage
1720e 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b  ->hdrOffset+8]);
1720f 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78  .    pCur->aiIdx
17210 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20  [pCur->iPage] = 
17211 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20  pPage->nCell;.  
17212 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68 69    rc = moveToChi
17213 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b 0a  ld(pCur, pgno);.
17214 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
17215 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
17216 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
17217 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d  >iPage] = pPage-
17218 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70 43  >nCell-1;.    pC
17219 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d  ur->info.nSize =
1721a 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61   0;.    pCur->va
1721b 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 7d  lidNKey = 0;.  }
1721c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1721d 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72  ./* Move the cur
1721e 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73 74  sor to the first
1721f 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61   entry in the ta
17220 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  ble.  Return SQL
17221 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63  ITE_OK.** on suc
17222 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73  cess.  Set *pRes
17223 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 72   to 0 if the cur
17224 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69  sor actually poi
17225 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67  nts to something
17226 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73  .** or set *pRes
17227 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 62   to 1 if the tab
17228 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a  le is empty..*/.
17229 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1722a 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
1722b 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70  irst(BtCursor *p
1722c 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b  Cur, int *pRes){
1722d 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
1722e 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
1722f 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
17230 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17231 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75  3_mutex_held(pCu
17232 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d  r->pBtree->db->m
17233 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20  utex) );.  rc = 
17234 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72 29  moveToRoot(pCur)
17235 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
17236 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
17237 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
17238 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
17239 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1723a 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
1723b 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d  ->iPage]->nCell=
1723c 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65  =0 );.      *pRe
1723d 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20  s = 1;.      rc 
1723e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
1723f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
17240 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61  sert( pCur->apPa
17241 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
17242 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20 20  >nCell>0 );.    
17243 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20    *pRes = 0;.   
17244 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65     rc = moveToLe
17245 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20 20  ftmost(pCur);.  
17246 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
17247 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20   rc;.}../* Move 
17248 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
17249 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20  e last entry in 
1724a 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75  the table.  Retu
1724b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20  rn SQLITE_OK.** 
1724c 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65 74  on success.  Set
1724d 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20 74   *pRes to 0 if t
1724e 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61 6c  he cursor actual
1724f 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f 6d  ly points to som
17250 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65 74  ething.** or set
17251 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20 74   *pRes to 1 if t
17252 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74  he table is empt
17253 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
17254 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
17255 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72 73  BtreeLast(BtCurs
17256 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  or *pCur, int *p
17257 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Res){.  int rc;.
17258 20 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73   .  assert( curs
17259 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
1725a 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
1725b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1725c 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d  ld(pCur->pBtree-
1725d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a  >db->mutex) );..
1725e 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73    /* If the curs
1725f 6f 72 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74  or already point
17260 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e  s to the last en
17261 74 72 79 2c 20 74 68 69 73 20 69 73 20 61 20 6e  try, this is a n
17262 6f 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 43  o-op. */.  if( C
17263 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75  URSOR_VALID==pCu
17264 72 2d 3e 65 53 74 61 74 65 20 26 26 20 70 43 75  r->eState && pCu
17265 72 2d 3e 61 74 4c 61 73 74 20 29 7b 0a 23 69 66  r->atLast ){.#if
17266 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
17267 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c 6f  .    /* This blo
17268 63 6b 20 73 65 72 76 65 73 20 74 6f 20 61 73 73  ck serves to ass
17269 65 72 74 28 29 20 74 68 61 74 20 74 68 65 20 63  ert() that the c
1726a 75 72 73 6f 72 20 72 65 61 6c 6c 79 20 64 6f 65  ursor really doe
1726b 73 20 70 6f 69 6e 74 20 0a 20 20 20 20 2a 2a 20  s point .    ** 
1726c 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72  to the last entr
1726d 79 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 2e  y in the b-tree.
1726e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a   */.    int ii;.
1726f 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
17270 3c 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 69  <pCur->iPage; ii
17271 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  ++){.      asser
17272 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69  t( pCur->aiIdx[i
17273 69 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65  i]==pCur->apPage
17274 5b 69 69 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  [ii]->nCell );. 
17275 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
17276 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
17277 72 2d 3e 69 50 61 67 65 5d 3d 3d 70 43 75 72 2d  r->iPage]==pCur-
17278 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
17279 61 67 65 5d 2d 3e 6e 43 65 6c 6c 2d 31 20 29 3b  age]->nCell-1 );
1727a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
1727b 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
1727c 69 50 61 67 65 5d 2d 3e 6c 65 61 66 20 29 3b 0a  iPage]->leaf );.
1727d 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74 75 72  #endif.    retur
1727e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1727f 0a 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52  ..  rc = moveToR
17280 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28  oot(pCur);.  if(
17281 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
17282 7b 0a 20 20 20 20 69 66 28 20 43 55 52 53 4f 52  {.    if( CURSOR
17283 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e  _INVALID==pCur->
17284 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20  eState ){.      
17285 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
17286 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
17287 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20  ]->nCell==0 );. 
17288 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a       *pRes = 1;.
17289 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1728a 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65   assert( pCur->e
1728b 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41  State==CURSOR_VA
1728c 4c 49 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52  LID );.      *pR
1728d 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63  es = 0;.      rc
1728e 20 3d 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f   = moveToRightmo
1728f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20  st(pCur);.      
17290 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 72  pCur->atLast = r
17291 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 31 3a  c==SQLITE_OK ?1:
17292 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  0;.    }.  }.  r
17293 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
17294 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
17295 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74  so that it point
17296 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 6e 65  s to an entry ne
17297 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a 20 73  ar the key .** s
17298 70 65 63 69 66 69 65 64 20 62 79 20 70 49 64 78  pecified by pIdx
17299 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e 20 20  Key or intKey.  
1729a 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 65 73   Return a succes
1729b 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f  s code..**.** Fo
1729c 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 73 2c  r INTKEY tables,
1729d 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61 72 61   the intKey para
1729e 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e 20 20  meter is used.  
1729f 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75 73 74  pIdxKey .** must
172a0 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72 20 69   be NULL.  For i
172a1 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70 49 64  ndex tables, pId
172a2 78 4b 65 79 20 69 73 20 75 73 65 64 20 61 6e 64  xKey is used and
172a3 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20 69 67   intKey.** is ig
172a4 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  nored..**.** If 
172a5 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68 20 69  an exact match i
172a6 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74 68 65  s not found, the
172a7 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  n the cursor is 
172a8 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74 20 70  always.** left p
172a9 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c 65 61  ointing at a lea
172aa 66 20 70 61 67 65 20 77 68 69 63 68 20 77 6f 75  f page which wou
172ab 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 72  ld hold the entr
172ac 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72 65 20  y if it.** were 
172ad 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20 63 75  present.  The cu
172ae 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69 6e 74  rsor might point
172af 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74 68 61   to an entry tha
172b0 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66 6f 72  t comes.** befor
172b1 65 20 6f 72 20 61 66 74 65 72 20 74 68 65 20 6b  e or after the k
172b2 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74  ey..**.** An int
172b3 65 67 65 72 20 69 73 20 77 72 69 74 74 65 6e 20  eger is written 
172b4 69 6e 74 6f 20 2a 70 52 65 73 20 77 68 69 63 68  into *pRes which
172b5 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f   is the result o
172b6 66 0a 2a 2a 20 63 6f 6d 70 61 72 69 6e 67 20 74  f.** comparing t
172b7 68 65 20 6b 65 79 20 77 69 74 68 20 74 68 65 20  he key with the 
172b8 65 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74  entry to which t
172b9 68 65 20 63 75 72 73 6f 72 20 69 73 20 0a 2a 2a  he cursor is .**
172ba 20 70 6f 69 6e 74 69 6e 67 2e 20 20 54 68 65 20   pointing.  The 
172bb 6d 65 61 6e 69 6e 67 20 6f 66 20 74 68 65 20 69  meaning of the i
172bc 6e 74 65 67 65 72 20 77 72 69 74 74 65 6e 20 69  nteger written i
172bd 6e 74 6f 0a 2a 2a 20 2a 70 52 65 73 20 69 73 20  nto.** *pRes is 
172be 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
172bf 2a 20 20 20 20 20 2a 70 52 65 73 3c 30 20 20 20  *     *pRes<0   
172c0 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73     The cursor is
172c1 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
172c2 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a  t an entry that.
172c3 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
172c4 20 20 20 20 69 73 20 73 6d 61 6c 6c 65 72 20 74      is smaller t
172c5 68 61 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b  han intKey/pIdxK
172c6 65 79 20 6f 72 20 69 66 20 74 68 65 20 74 61 62  ey or if the tab
172c7 6c 65 20 69 73 20 65 6d 70 74 79 0a 2a 2a 20 20  le is empty.**  
172c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
172c9 61 6e 64 20 74 68 65 20 63 75 72 73 6f 72 20 69  and the cursor i
172ca 73 20 74 68 65 72 65 66 6f 72 65 20 6c 65 66 74  s therefore left
172cb 20 70 6f 69 6e 74 20 74 6f 20 6e 6f 74 68 69 6e   point to nothin
172cc 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52  g..**.**     *pR
172cd 65 73 3d 3d 30 20 20 20 20 20 54 68 65 20 63 75  es==0     The cu
172ce 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69  rsor is left poi
172cf 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72  nting at an entr
172d0 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20  y that.**       
172d1 20 20 20 20 20 20 20 20 20 20 20 65 78 61 63 74             exact
172d2 6c 79 20 6d 61 74 63 68 65 73 20 69 6e 74 4b 65  ly matches intKe
172d3 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a 2a  y/pIdxKey..**.**
172d4 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20 20 20       *pRes>0    
172d5 20 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20    The cursor is 
172d6 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
172d7 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74 0a 2a   an entry that.*
172d8 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
172d9 20 20 20 69 73 20 6c 61 72 67 65 72 20 74 68 61     is larger tha
172da 6e 20 69 6e 74 4b 65 79 2f 70 49 64 78 4b 65 79  n intKey/pIdxKey
172db 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ..**.*/.SQLITE_P
172dc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
172dd 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
172de 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72 73 6f  acked(.  BtCurso
172df 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20  r *pCur,        
172e0 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20    /* The cursor 
172e1 74 6f 20 62 65 20 6d 6f 76 65 64 20 2a 2f 0a 20  to be moved */. 
172e2 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
172e3 2a 70 49 64 78 4b 65 79 2c 20 2f 2a 20 55 6e 70  *pIdxKey, /* Unp
172e4 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20  acked index key 
172e5 2a 2f 0a 20 20 69 36 34 20 69 6e 74 4b 65 79 2c  */.  i64 intKey,
172e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
172e7 20 54 68 65 20 74 61 62 6c 65 20 6b 65 79 20 2a   The table key *
172e8 2f 0a 20 20 69 6e 74 20 62 69 61 73 52 69 67 68  /.  int biasRigh
172e9 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t,           /* 
172ea 49 66 20 74 72 75 65 2c 20 62 69 61 73 20 74 68  If true, bias th
172eb 65 20 73 65 61 72 63 68 20 74 6f 20 74 68 65 20  e search to the 
172ec 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69 6e  high end */.  in
172ed 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20 20  t *pRes         
172ee 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
172ef 73 65 61 72 63 68 20 72 65 73 75 6c 74 73 20 68  search results h
172f0 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
172f1 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63  rc;..  assert( c
172f2 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
172f3 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72  pCur) );.  asser
172f4 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
172f5 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72  _held(pCur->pBtr
172f6 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ee->db->mutex) )
172f7 3b 0a 20 20 61 73 73 65 72 74 28 20 70 52 65 73  ;.  assert( pRes
172f8 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
172f9 49 64 78 4b 65 79 3d 3d 30 29 3d 3d 28 70 43 75  IdxKey==0)==(pCu
172fa 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 20  r->pKeyInfo==0) 
172fb 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
172fc 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64  cursor is alread
172fd 79 20 70 6f 73 69 74 69 6f 6e 65 64 20 61 74 20  y positioned at 
172fe 74 68 65 20 70 6f 69 6e 74 20 77 65 20 61 72 65  the point we are
172ff 20 74 72 79 69 6e 67 0a 20 20 2a 2a 20 74 6f 20   trying.  ** to 
17300 6d 6f 76 65 20 74 6f 2c 20 74 68 65 6e 20 6a 75  move to, then ju
17301 73 74 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75  st return withou
17302 74 20 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b  t doing any work
17303 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e   */.  if( pCur->
17304 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
17305 41 4c 49 44 20 26 26 20 70 43 75 72 2d 3e 76 61  ALID && pCur->va
17306 6c 69 64 4e 4b 65 79 20 0a 20 20 20 26 26 20 70  lidNKey .   && p
17307 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e  Cur->apPage[0]->
17308 69 6e 74 4b 65 79 20 0a 20 20 29 7b 0a 20 20 20  intKey .  ){.   
17309 20 69 66 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e   if( pCur->info.
1730a 6e 4b 65 79 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a  nKey==intKey ){.
1730b 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b        *pRes = 0;
1730c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
1730d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
1730e 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61 74 4c     if( pCur->atL
1730f 61 73 74 20 26 26 20 70 43 75 72 2d 3e 69 6e 66  ast && pCur->inf
17310 6f 2e 6e 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b  o.nKey<intKey ){
17311 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 2d  .      *pRes = -
17312 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  1;.      return 
17313 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
17314 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 6d 6f 76  .  }..  rc = mov
17315 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20  eToRoot(pCur);. 
17316 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72   if( rc ){.    r
17317 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
17318 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
17319 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1731a 5d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ] );.  assert( p
1731b 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
1731c 2d 3e 69 50 61 67 65 5d 2d 3e 69 73 49 6e 69 74  ->iPage]->isInit
1731d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
1731e 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1731f 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3e 30  >iPage]->nCell>0
17320 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65   || pCur->eState
17321 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ==CURSOR_INVALID
17322 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e   );.  if( pCur->
17323 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49  eState==CURSOR_I
17324 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 2a 70  NVALID ){.    *p
17325 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 61 73  Res = -1;.    as
17326 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61  sert( pCur->apPa
17327 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
17328 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  >nCell==0 );.   
17329 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1732a 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  K;.  }.  assert(
1732b 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d   pCur->apPage[0]
1732c 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 70 49 64 78  ->intKey || pIdx
1732d 4b 65 79 20 29 3b 0a 20 20 66 6f 72 28 3b 3b 29  Key );.  for(;;)
1732e 7b 0a 20 20 20 20 69 6e 74 20 6c 77 72 2c 20 75  {.    int lwr, u
1732f 70 72 3b 0a 20 20 20 20 50 67 6e 6f 20 63 68 6c  pr;.    Pgno chl
17330 64 50 67 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65  dPg;.    MemPage
17331 20 2a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e   *pPage = pCur->
17332 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
17333 67 65 5d 3b 0a 20 20 20 20 69 6e 74 20 63 3b 0a  ge];.    int c;.
17334 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e 6e  .    /* pPage->n
17335 43 65 6c 6c 20 6d 75 73 74 20 62 65 20 67 72 65  Cell must be gre
17336 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2e 20  ater than zero. 
17337 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 72  If this is the r
17338 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a 20  oot-page.    ** 
17339 74 68 65 20 63 75 72 73 6f 72 20 77 6f 75 6c 64  the cursor would
1733a 20 68 61 76 65 20 62 65 65 6e 20 49 4e 56 41 4c   have been INVAL
1733b 49 44 20 61 62 6f 76 65 20 61 6e 64 20 74 68 69  ID above and thi
1733c 73 20 66 6f 72 28 3b 3b 29 20 6c 6f 6f 70 0a 20  s for(;;) loop. 
1733d 20 20 20 2a 2a 20 6e 6f 74 20 72 75 6e 2e 20 49     ** not run. I
1733e 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 74 68  f this is not th
1733f 65 20 72 6f 6f 74 2d 70 61 67 65 2c 20 74 68 65  e root-page, the
17340 6e 20 74 68 65 20 6d 6f 76 65 54 6f 43 68 69 6c  n the moveToChil
17341 64 28 29 20 72 6f 75 74 69 6e 65 0a 20 20 20 20  d() routine.    
17342 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 61 6c  ** would have al
17343 72 65 61 64 79 20 64 65 74 65 63 74 65 64 20 64  ready detected d
17344 62 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 53 69  b corruption. Si
17345 6d 69 6c 61 72 6c 79 2c 20 70 50 61 67 65 20 6d  milarly, pPage m
17346 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 74 68  ust.    ** be th
17347 65 20 72 69 67 68 74 20 6b 69 6e 64 20 28 69 6e  e right kind (in
17348 64 65 78 20 6f 72 20 74 61 62 6c 65 29 20 6f 66  dex or table) of
17349 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 4f 74   b-tree page. Ot
1734a 68 65 72 77 69 73 65 0a 20 20 20 20 2a 2a 20 61  herwise.    ** a
1734b 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20 6f   moveToChild() o
1734c 72 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 29 20 63  r moveToRoot() c
1734d 61 6c 6c 20 77 6f 75 6c 64 20 68 61 76 65 20 64  all would have d
1734e 65 74 65 63 74 65 64 20 63 6f 72 72 75 70 74 69  etected corrupti
1734f 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  on.  */.    asse
17350 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  rt( pPage->nCell
17351 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  >0 );.    assert
17352 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3d  ( pPage->intKey=
17353 3d 28 70 49 64 78 4b 65 79 3d 3d 30 29 20 29 3b  =(pIdxKey==0) );
17354 0a 20 20 20 20 6c 77 72 20 3d 20 30 3b 0a 20 20  .    lwr = 0;.  
17355 20 20 75 70 72 20 3d 20 70 50 61 67 65 2d 3e 6e    upr = pPage->n
17356 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20  Cell-1;.    if( 
17357 62 69 61 73 52 69 67 68 74 20 29 7b 0a 20 20 20  biasRight ){.   
17358 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70     pCur->aiIdx[p
17359 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75  Cur->iPage] = (u
1735a 31 36 29 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73  16)upr;.    }els
1735b 65 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61  e{.      pCur->a
1735c 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
1735d 5d 20 3d 20 28 75 31 36 29 28 28 75 70 72 2b 6c  ] = (u16)((upr+l
1735e 77 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20  wr)/2);.    }.  
1735f 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20    for(;;){.     
17360 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d   int idx = pCur-
17361 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
17362 67 65 5d 3b 20 2f 2a 20 49 6e 64 65 78 20 6f 66  ge]; /* Index of
17363 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e   current cell in
17364 20 70 50 61 67 65 20 2a 2f 0a 20 20 20 20 20 20   pPage */.      
17365 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20  u8 *pCell;      
17366 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17367 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
17368 6f 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69  o current cell i
17369 6e 20 70 50 61 67 65 20 2a 2f 0a 0a 20 20 20 20  n pPage */..    
1736a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
1736b 7a 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 43  ze = 0;.      pC
1736c 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
1736d 50 61 67 65 2c 20 69 64 78 29 20 2b 20 70 50 61  Page, idx) + pPa
1736e 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65  ge->childPtrSize
1736f 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67  ;.      if( pPag
17370 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20  e->intKey ){.   
17371 20 20 20 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65       i64 nCellKe
17372 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  y;.        if( p
17373 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b  Page->hasData ){
17374 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 20 64  .          u32 d
17375 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20 20 20  ummy;.          
17376 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56 61 72 69  pCell += getVari
17377 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64 75 6d 6d  nt32(pCell, dumm
17378 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  y);.        }.  
17379 20 20 20 20 20 20 67 65 74 56 61 72 69 6e 74 28        getVarint(
1737a 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29 26 6e 43  pCell, (u64*)&nC
1737b 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20  ellKey);.       
1737c 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3d 3d 69   if( nCellKey==i
1737d 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  ntKey ){.       
1737e 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 20 20     c = 0;.      
1737f 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 43 65 6c    }else if( nCel
17380 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29 7b 0a 20  lKey<intKey ){. 
17381 20 20 20 20 20 20 20 20 20 63 20 3d 20 2d 31 3b           c = -1;
17382 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
17383 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
17384 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e 74 4b 65  ( nCellKey>intKe
17385 79 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 63  y );.          c
17386 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20 20 20 7d   = +1;.        }
17387 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 76  .        pCur->v
17388 61 6c 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20  alidNKey = 1;.  
17389 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f        pCur->info
1738a 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 79  .nKey = nCellKey
1738b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1738c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61         /* The ma
1738d 78 69 6d 75 6d 20 73 75 70 70 6f 72 74 65 64 20  ximum supported 
1738e 70 61 67 65 2d 73 69 7a 65 20 69 73 20 33 32 37  page-size is 327
1738f 36 38 20 62 79 74 65 73 2e 20 54 68 69 73 20 6d  68 bytes. This m
17390 65 61 6e 73 20 74 68 61 74 0a 20 20 20 20 20 20  eans that.      
17391 20 20 2a 2a 20 74 68 65 20 6d 61 78 69 6d 75 6d    ** the maximum
17392 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f 72   number of recor
17393 64 20 62 79 74 65 73 20 73 74 6f 72 65 64 20 6f  d bytes stored o
17394 6e 20 61 6e 20 69 6e 64 65 78 20 42 2d 54 72 65  n an index B-Tre
17395 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 61 67  e.        ** pag
17396 65 20 69 73 20 61 74 20 6d 6f 73 74 20 38 31 39  e is at most 819
17397 38 20 62 79 74 65 73 2c 20 77 68 69 63 68 20 6d  8 bytes, which m
17398 61 79 20 62 65 20 73 74 6f 72 65 64 20 61 73 20  ay be stored as 
17399 61 20 32 2d 62 79 74 65 0a 20 20 20 20 20 20 20  a 2-byte.       
1739a 20 2a 2a 20 76 61 72 69 6e 74 2e 20 54 68 69 73   ** varint. This
1739b 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
1739c 75 73 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20  used to attempt 
1739d 74 6f 20 61 76 6f 69 64 20 70 61 72 73 69 6e 67  to avoid parsing
1739e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65   .        ** the
1739f 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 62 79 20   entire cell by 
173a0 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 74 68 65  checking for the
173a1 20 63 61 73 65 73 20 77 68 65 72 65 20 74 68 65   cases where the
173a2 20 72 65 63 6f 72 64 20 69 73 20 0a 20 20 20 20   record is .    
173a3 20 20 20 20 2a 2a 20 73 74 6f 72 65 64 20 65 6e      ** stored en
173a4 74 69 72 65 6c 79 20 77 69 74 68 69 6e 20 74 68  tirely within th
173a5 65 20 62 2d 74 72 65 65 20 70 61 67 65 20 62 79  e b-tree page by
173a6 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65 20   inspecting the 
173a7 66 69 72 73 74 20 0a 20 20 20 20 20 20 20 20 2a  first .        *
173a8 2a 20 32 20 62 79 74 65 73 20 6f 66 20 74 68 65  * 2 bytes of the
173a9 20 63 65 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a   cell..        *
173aa 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 43  /.        int nC
173ab 65 6c 6c 20 3d 20 70 43 65 6c 6c 5b 30 5d 3b 0a  ell = pCell[0];.
173ac 20 20 20 20 20 20 20 20 69 66 28 20 21 28 6e 43          if( !(nC
173ad 65 6c 6c 20 26 20 30 78 38 30 29 20 26 26 20 6e  ell & 0x80) && n
173ae 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e 6d 61 78  Cell<=pPage->max
173af 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20  Local ){.       
173b0 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61 6e 63     /* This branc
173b1 68 20 72 75 6e 73 20 69 66 20 74 68 65 20 72 65  h runs if the re
173b2 63 6f 72 64 2d 73 69 7a 65 20 66 69 65 6c 64 20  cord-size field 
173b3 6f 66 20 74 68 65 20 63 65 6c 6c 20 69 73 20 61  of the cell is a
173b4 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 69  .          ** si
173b5 6e 67 6c 65 20 62 79 74 65 20 76 61 72 69 6e 74  ngle byte varint
173b6 20 61 6e 64 20 74 68 65 20 72 65 63 6f 72 64 20   and the record 
173b7 66 69 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 6e  fits entirely on
173b8 20 74 68 65 20 6d 61 69 6e 0a 20 20 20 20 20 20   the main.      
173b9 20 20 20 20 2a 2a 20 62 2d 74 72 65 65 20 70 61      ** b-tree pa
173ba 67 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  ge.  */.        
173bb 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62    c = sqlite3Vdb
173bc 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e  eRecordCompare(n
173bd 43 65 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43  Cell, (void*)&pC
173be 65 6c 6c 5b 31 5d 2c 20 70 49 64 78 4b 65 79 29  ell[1], pIdxKey)
173bf 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
173c0 69 66 28 20 21 28 70 43 65 6c 6c 5b 31 5d 20 26  if( !(pCell[1] &
173c1 20 30 78 38 30 29 20 0a 20 20 20 20 20 20 20 20   0x80) .        
173c2 20 20 26 26 20 28 6e 43 65 6c 6c 20 3d 20 28 28    && (nCell = ((
173c3 6e 43 65 6c 6c 26 30 78 37 66 29 3c 3c 37 29 20  nCell&0x7f)<<7) 
173c4 2b 20 70 43 65 6c 6c 5b 31 5d 29 3c 3d 70 50 61  + pCell[1])<=pPa
173c5 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 0a 20 20 20  ge->maxLocal.   
173c6 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
173c7 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64 2d    /* The record-
173c8 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 20  size field is a 
173c9 32 20 62 79 74 65 20 76 61 72 69 6e 74 20 61 6e  2 byte varint an
173ca 64 20 74 68 65 20 72 65 63 6f 72 64 20 0a 20 20  d the record .  
173cb 20 20 20 20 20 20 20 20 2a 2a 20 66 69 74 73 20          ** fits 
173cc 65 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 65 20  entirely on the 
173cd 6d 61 69 6e 20 62 2d 74 72 65 65 20 70 61 67 65  main b-tree page
173ce 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  .  */.          
173cf 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52  c = sqlite3VdbeR
173d0 65 63 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65  ecordCompare(nCe
173d1 6c 6c 2c 20 28 76 6f 69 64 2a 29 26 70 43 65 6c  ll, (void*)&pCel
173d2 6c 5b 32 5d 2c 20 70 49 64 78 4b 65 79 29 3b 0a  l[2], pIdxKey);.
173d3 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
173d4 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
173d5 72 65 63 6f 72 64 20 66 6c 6f 77 73 20 6f 76 65  record flows ove
173d6 72 20 6f 6e 74 6f 20 6f 6e 65 20 6f 72 20 6d 6f  r onto one or mo
173d7 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  re overflow page
173d8 73 2e 20 49 6e 0a 20 20 20 20 20 20 20 20 20 20  s. In.          
173d9 2a 2a 20 74 68 69 73 20 63 61 73 65 20 74 68 65  ** this case the
173da 20 77 68 6f 6c 65 20 63 65 6c 6c 20 6e 65 65 64   whole cell need
173db 73 20 74 6f 20 62 65 20 70 61 72 73 65 64 2c 20  s to be parsed, 
173dc 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74  a buffer allocat
173dd 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ed.          ** 
173de 61 6e 64 20 61 63 63 65 73 73 50 61 79 6c 6f 61  and accessPayloa
173df 64 28 29 20 75 73 65 64 20 74 6f 20 72 65 74 72  d() used to retr
173e0 69 65 76 65 20 74 68 65 20 72 65 63 6f 72 64 20  ieve the record 
173e1 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20 20  into the.       
173e2 20 20 20 2a 2a 20 62 75 66 66 65 72 20 62 65 66     ** buffer bef
173e3 6f 72 65 20 56 64 62 65 52 65 63 6f 72 64 43 6f  ore VdbeRecordCo
173e4 6d 70 61 72 65 28 29 20 63 61 6e 20 62 65 20 63  mpare() can be c
173e5 61 6c 6c 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20  alled. */.      
173e6 20 20 20 20 76 6f 69 64 20 2a 70 43 65 6c 6c 4b      void *pCellK
173e7 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 75 38  ey;.          u8
173e8 20 2a 20 63 6f 6e 73 74 20 70 43 65 6c 6c 42 6f   * const pCellBo
173e9 64 79 20 3d 20 70 43 65 6c 6c 20 2d 20 70 50 61  dy = pCell - pPa
173ea 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65  ge->childPtrSize
173eb 3b 0a 20 20 20 20 20 20 20 20 20 20 62 74 72 65  ;.          btre
173ec 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
173ed 61 67 65 2c 20 70 43 65 6c 6c 42 6f 64 79 2c 20  age, pCellBody, 
173ee 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20  &pCur->info);.  
173ef 20 20 20 20 20 20 20 20 6e 43 65 6c 6c 20 3d 20          nCell = 
173f0 28 69 6e 74 29 70 43 75 72 2d 3e 69 6e 66 6f 2e  (int)pCur->info.
173f1 6e 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20  nKey;.          
173f2 70 43 65 6c 6c 4b 65 79 20 3d 20 73 71 6c 69 74  pCellKey = sqlit
173f3 65 33 4d 61 6c 6c 6f 63 28 20 6e 43 65 6c 6c 20  e3Malloc( nCell 
173f4 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
173f5 20 70 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a   pCellKey==0 ){.
173f6 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
173f7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
173f8 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
173f9 6d 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20  moveto_finish;. 
173fa 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
173fb 20 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73       rc = access
173fc 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 30 2c  Payload(pCur, 0,
173fd 20 6e 43 65 6c 6c 2c 20 28 75 6e 73 69 67 6e 65   nCell, (unsigne
173fe 64 20 63 68 61 72 2a 29 70 43 65 6c 6c 4b 65 79  d char*)pCellKey
173ff 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
17400 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
17401 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
17402 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20  ee(pCellKey);.  
17403 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d            goto m
17404 6f 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20  oveto_finish;.  
17405 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
17406 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56      c = sqlite3V
17407 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65  dbeRecordCompare
17408 28 6e 43 65 6c 6c 2c 20 70 43 65 6c 6c 4b 65 79  (nCell, pCellKey
17409 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20  , pIdxKey);.    
1740a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
1740b 65 65 28 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20  ee(pCellKey);.  
1740c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1740d 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29        if( c==0 )
1740e 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50  {.        if( pP
1740f 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21  age->intKey && !
17410 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
17411 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d 20 69           lwr = i
17412 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20 75 70  dx;.          up
17413 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20 20 20  r = lwr - 1;.   
17414 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
17415 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
17416 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30         *pRes = 0
17417 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ;.          rc =
17418 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
17419 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74        goto movet
1741a 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20  o_finish;.      
1741b 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1741c 20 20 69 66 28 20 63 3c 30 20 29 7b 0a 20 20 20    if( c<0 ){.   
1741d 20 20 20 20 20 6c 77 72 20 3d 20 69 64 78 2b 31       lwr = idx+1
1741e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1741f 20 20 20 20 20 20 20 75 70 72 20 3d 20 69 64 78         upr = idx
17420 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
17421 20 20 69 66 28 20 6c 77 72 3e 75 70 72 20 29 7b    if( lwr>upr ){
17422 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
17423 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43        }.      pC
17424 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
17425 69 50 61 67 65 5d 20 3d 20 28 75 31 36 29 28 28  iPage] = (u16)((
17426 6c 77 72 2b 75 70 72 29 2f 32 29 3b 0a 20 20 20  lwr+upr)/2);.   
17427 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c   }.    assert( l
17428 77 72 3d 3d 75 70 72 2b 31 20 29 3b 0a 20 20 20  wr==upr+1 );.   
17429 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1742a 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 69 66  isInit );.    if
1742b 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b  ( pPage->leaf ){
1742c 0a 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20  .      chldPg = 
1742d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  0;.    }else if(
1742e 20 6c 77 72 3e 3d 70 50 61 67 65 2d 3e 6e 43 65   lwr>=pPage->nCe
1742f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 63 68 6c 64  ll ){.      chld
17430 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  Pg = get4byte(&p
17431 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
17432 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
17433 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
17434 20 20 20 63 68 6c 64 50 67 20 3d 20 67 65 74 34     chldPg = get4
17435 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50  byte(findCell(pP
17436 61 67 65 2c 20 6c 77 72 29 29 3b 0a 20 20 20 20  age, lwr));.    
17437 7d 0a 20 20 20 20 69 66 28 20 63 68 6c 64 50 67  }.    if( chldPg
17438 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ==0 ){.      ass
17439 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
1743a 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43  [pCur->iPage]<pC
1743b 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1743c 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29  >iPage]->nCell )
1743d 3b 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20  ;.      *pRes = 
1743e 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  c;.      rc = SQ
1743f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 67  LITE_OK;.      g
17440 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69 73  oto moveto_finis
17441 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75  h;.    }.    pCu
17442 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
17443 50 61 67 65 5d 20 3d 20 28 75 31 36 29 6c 77 72  Page] = (u16)lwr
17444 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f  ;.    pCur->info
17445 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20  .nSize = 0;.    
17446 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
17447 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f  = 0;.    rc = mo
17448 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
17449 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 66 28  chldPg);.    if(
1744a 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 65 74   rc ) goto movet
1744b 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 6d 6f  o_finish;.  }.mo
1744c 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 72  veto_finish:.  r
1744d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
1744e 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
1744f 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  if the cursor is
17450 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 74   not pointing at
17451 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65   an entry of the
17452 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52   table..**.** TR
17453 55 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  UE will be retur
17454 6e 65 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c  ned after a call
17455 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
17456 4e 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a 20  Next() moves.** 
17457 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 6e  past the last en
17458 74 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65  try in the table
17459 20 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65   or sqlite3Btree
1745a 50 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 73  Prev() moves pas
1745b 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 65  t.** the first e
1745c 6e 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 61  ntry.  TRUE is a
1745d 6c 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20  lso returned if 
1745e 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
1745f 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ty..*/.SQLITE_PR
17460 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
17461 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73  3BtreeEof(BtCurs
17462 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20  or *pCur){.  /* 
17463 54 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 68  TODO: What if th
17464 65 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 43  e cursor is in C
17465 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
17466 4b 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 20  K but all table 
17467 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 76  entries.  ** hav
17468 65 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f 20  e been deleted? 
17469 54 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e 65  This API will ne
1746a 65 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f 20  ed to change to 
1746b 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
1746c 63 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 6c  code.  ** as wel
1746d 6c 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 6e  l as the boolean
1746e 20 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a 20   result value.. 
1746f 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 55   */.  return (CU
17470 52 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 72  RSOR_VALID!=pCur
17471 2d 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a  ->eState);.}../*
17472 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68 65 20  .** Advance the 
17473 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 6e 65  cursor to the ne
17474 78 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  xt entry in the 
17475 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a  database.  If.**
17476 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e   successful then
17477 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49   set *pRes=0.  I
17478 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  f the cursor.** 
17479 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e  was already poin
1747a 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74  ting to the last
1747b 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64 61   entry in the da
1747c 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a 2a  tabase before.**
1747d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   this routine wa
1747e 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20 73  s called, then s
1747f 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a 53  et *pRes=1..*/.S
17480 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
17481 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  t sqlite3BtreeNe
17482 78 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  xt(BtCursor *pCu
17483 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20  r, int *pRes){. 
17484 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69   int rc;.  int i
17485 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  dx;.  MemPage *p
17486 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Page;..  assert(
17487 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
17488 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63 20  x(pCur) );.  rc 
17489 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50  = restoreCursorP
1748a 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20  osition(pCur);. 
1748b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1748c 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
1748d 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72   rc;.  }.  asser
1748e 74 28 20 70 52 65 73 21 3d 30 20 29 3b 0a 20 20  t( pRes!=0 );.  
1748f 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  if( CURSOR_INVAL
17490 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65  ID==pCur->eState
17491 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20   ){.    *pRes = 
17492 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  1;.    return SQ
17493 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
17494 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78  f( pCur->skipNex
17495 74 3e 30 20 29 7b 0a 20 20 20 20 70 43 75 72 2d  t>0 ){.    pCur-
17496 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 20  >skipNext = 0;. 
17497 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
17498 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
17499 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e  OK;.  }.  pCur->
1749a 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20  skipNext = 0;.. 
1749b 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
1749c 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
1749d 65 5d 3b 0a 20 20 69 64 78 20 3d 20 2b 2b 70 43  e];.  idx = ++pC
1749e 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
1749f 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74  iPage];.  assert
174a0 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20  ( pPage->isInit 
174a1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 78  );.  assert( idx
174a2 3c 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  <=pPage->nCell )
174a3 3b 0a 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e  ;..  pCur->info.
174a4 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75  nSize = 0;.  pCu
174a5 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30  r->validNKey = 0
174a6 3b 0a 20 20 69 66 28 20 69 64 78 3e 3d 70 50 61  ;.  if( idx>=pPa
174a7 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20  ge->nCell ){.   
174a8 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
174a9 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  f ){.      rc = 
174aa 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
174ab 2c 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  , get4byte(&pPag
174ac 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e  e->aData[pPage->
174ad 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a  hdrOffset+8]));.
174ae 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
174af 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
174b0 72 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d  rc = moveToLeftm
174b1 6f 73 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20  ost(pCur);.     
174b2 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20   *pRes = 0;.    
174b3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
174b4 20 7d 0a 20 20 20 20 64 6f 7b 0a 20 20 20 20 20   }.    do{.     
174b5 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65   if( pCur->iPage
174b6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  ==0 ){.        *
174b7 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20  pRes = 1;.      
174b8 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
174b9 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b   CURSOR_INVALID;
174ba 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
174bb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
174bc 20 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50   }.      moveToP
174bd 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20  arent(pCur);.   
174be 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d     pPage = pCur-
174bf 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
174c0 61 67 65 5d 3b 0a 20 20 20 20 7d 77 68 69 6c 65  age];.    }while
174c1 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  ( pCur->aiIdx[pC
174c2 75 72 2d 3e 69 50 61 67 65 5d 3e 3d 70 50 61 67  ur->iPage]>=pPag
174c3 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20  e->nCell );.    
174c4 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 69  *pRes = 0;.    i
174c5 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  f( pPage->intKey
174c6 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
174c7 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28  qlite3BtreeNext(
174c8 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20  pCur, pRes);.   
174c9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
174ca 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
174cb 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72    }.    return r
174cc 63 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d  c;.  }.  *pRes =
174cd 20 30 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d   0;.  if( pPage-
174ce 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 72 65 74  >leaf ){.    ret
174cf 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
174d0 20 7d 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f   }.  rc = moveTo
174d1 4c 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a  Leftmost(pCur);.
174d2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
174d3 0a 2f 2a 0a 2a 2a 20 53 74 65 70 20 74 68 65 20  ./*.** Step the 
174d4 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 62 61  cursor to the ba
174d5 63 6b 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f  ck to the previo
174d6 75 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  us entry in the 
174d7 64 61 74 61 62 61 73 65 2e 20 20 49 66 0a 2a 2a  database.  If.**
174d8 20 73 75 63 63 65 73 73 66 75 6c 20 74 68 65 6e   successful then
174d9 20 73 65 74 20 2a 70 52 65 73 3d 30 2e 20 20 49   set *pRes=0.  I
174da 66 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  f the cursor.** 
174db 77 61 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e  was already poin
174dc 74 69 6e 67 20 74 6f 20 74 68 65 20 66 69 72 73  ting to the firs
174dd 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 64  t entry in the d
174de 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a 2a  atabase before.*
174df 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * this routine w
174e0 61 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20  as called, then 
174e1 73 65 74 20 2a 70 52 65 73 3d 31 2e 0a 2a 2f 0a  set *pRes=1..*/.
174e2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
174e3 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  nt sqlite3BtreeP
174e4 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f 72  revious(BtCursor
174e5 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65   *pCur, int *pRe
174e6 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  s){.  int rc;.  
174e7 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a  MemPage *pPage;.
174e8 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
174e9 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
174ea 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74  ) );.  rc = rest
174eb 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
174ec 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  n(pCur);.  if( r
174ed 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
174ee 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
174ef 20 7d 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 73   }.  pCur->atLas
174f0 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 43 55 52  t = 0;.  if( CUR
174f1 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75  SOR_INVALID==pCu
174f2 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20  r->eState ){.   
174f3 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20   *pRes = 1;.    
174f4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
174f5 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72  ;.  }.  if( pCur
174f6 2d 3e 73 6b 69 70 4e 65 78 74 3c 30 20 29 7b 0a  ->skipNext<0 ){.
174f7 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65      pCur->skipNe
174f8 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52 65  xt = 0;.    *pRe
174f9 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72  s = 0;.    retur
174fa 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
174fb 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78  .  pCur->skipNex
174fc 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65 20  t = 0;..  pPage 
174fd 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
174fe 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61  Cur->iPage];.  a
174ff 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73  ssert( pPage->is
17500 49 6e 69 74 20 29 3b 0a 20 20 69 66 28 20 21 70  Init );.  if( !p
17501 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
17502 20 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72    int idx = pCur
17503 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
17504 61 67 65 5d 3b 0a 20 20 20 20 72 63 20 3d 20 6d  age];.    rc = m
17505 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c  oveToChild(pCur,
17506 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65   get4byte(findCe
17507 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 29 29  ll(pPage, idx)))
17508 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
17509 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1750a 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
1750b 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28  moveToRightmost(
1750c 70 43 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  pCur);.  }else{.
1750d 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 72 2d      while( pCur-
1750e 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
1750f 67 65 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ge]==0 ){.      
17510 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3d  if( pCur->iPage=
17511 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  =0 ){.        pC
17512 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52  ur->eState = CUR
17513 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20  SOR_INVALID;.   
17514 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a       *pRes = 1;.
17515 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
17516 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
17517 7d 0a 20 20 20 20 20 20 6d 6f 76 65 54 6f 50 61  }.      moveToPa
17518 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20 20  rent(pCur);.    
17519 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f  }.    pCur->info
1751a 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20  .nSize = 0;.    
1751b 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
1751c 3d 20 30 3b 0a 0a 20 20 20 20 70 43 75 72 2d 3e  = 0;..    pCur->
1751d 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
1751e 65 5d 2d 2d 3b 0a 20 20 20 20 70 50 61 67 65 20  e]--;.    pPage 
1751f 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
17520 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20  Cur->iPage];.   
17521 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b   if( pPage->intK
17522 65 79 20 26 26 20 21 70 50 61 67 65 2d 3e 6c 65  ey && !pPage->le
17523 61 66 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  af ){.      rc =
17524 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 72 65   sqlite3BtreePre
17525 76 69 6f 75 73 28 70 43 75 72 2c 20 70 52 65 73  vious(pCur, pRes
17526 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
17527 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
17528 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  OK;.    }.  }.  
17529 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 72 65 74  *pRes = 0;.  ret
1752a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1752b 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
1752c 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 64 61  page from the da
1752d 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
1752e 2a 2a 20 54 68 65 20 6e 65 77 20 70 61 67 65 20  ** The new page 
1752f 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72  is marked as dir
17530 74 79 2e 20 20 28 49 6e 20 6f 74 68 65 72 20 77  ty.  (In other w
17531 6f 72 64 73 2c 20 73 71 6c 69 74 65 33 50 61 67  ords, sqlite3Pag
17532 65 72 57 72 69 74 65 28 29 0a 2a 2a 20 68 61 73  erWrite().** has
17533 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 61   already been ca
17534 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6e 65 77 20  lled on the new 
17535 70 61 67 65 2e 29 20 20 54 68 65 20 6e 65 77 20  page.)  The new 
17536 70 61 67 65 20 68 61 73 20 61 6c 73 6f 0a 2a 2a  page has also.**
17537 20 62 65 65 6e 20 72 65 66 65 72 65 6e 63 65 64   been referenced
17538 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e 67   and the calling
17539 20 72 6f 75 74 69 6e 65 20 69 73 20 72 65 73 70   routine is resp
1753a 6f 6e 73 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c  onsible for call
1753b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61  ing.** sqlite3Pa
1753c 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 74 68  gerUnref() on th
1753d 65 20 6e 65 77 20 70 61 67 65 20 77 68 65 6e 20  e new page when 
1753e 69 74 20 69 73 20 64 6f 6e 65 2e 0a 2a 2a 0a 2a  it is done..**.*
1753f 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
17540 65 74 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65  eturned on succe
17541 73 73 2e 20 20 41 6e 79 20 6f 74 68 65 72 20 72  ss.  Any other r
17542 65 74 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69  eturn value indi
17543 63 61 74 65 73 0a 2a 2a 20 61 6e 20 65 72 72 6f  cates.** an erro
17544 72 2e 20 20 2a 70 70 50 61 67 65 20 61 6e 64 20  r.  *ppPage and 
17545 2a 70 50 67 6e 6f 20 61 72 65 20 75 6e 64 65 66  *pPgno are undef
17546 69 6e 65 64 20 69 6e 20 74 68 65 20 65 76 65 6e  ined in the even
17547 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 2e 0a 2a  t of an error..*
17548 2a 20 44 6f 20 6e 6f 74 20 69 6e 76 6f 6b 65 20  * Do not invoke 
17549 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
1754a 66 28 29 20 6f 6e 20 2a 70 70 50 61 67 65 20 69  f() on *ppPage i
1754b 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65  f an error is re
1754c 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
1754d 20 74 68 65 20 22 6e 65 61 72 62 79 22 20 70 61   the "nearby" pa
1754e 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30  rameter is not 0
1754f 2c 20 74 68 65 6e 20 61 20 28 66 65 65 62 6c 65  , then a (feeble
17550 29 20 65 66 66 6f 72 74 20 69 73 20 6d 61 64 65  ) effort is made
17551 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 74 65 20 61   to .** locate a
17552 20 70 61 67 65 20 63 6c 6f 73 65 20 74 6f 20 74   page close to t
17553 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 22  he page number "
17554 6e 65 61 72 62 79 22 2e 20 20 54 68 69 73 20 63  nearby".  This c
17555 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61 6e  an be used in an
17556 0a 2a 2a 20 61 74 74 65 6d 70 74 20 74 6f 20 6b  .** attempt to k
17557 65 65 70 20 72 65 6c 61 74 65 64 20 70 61 67 65  eep related page
17558 73 20 63 6c 6f 73 65 20 74 6f 20 65 61 63 68 20  s close to each 
17559 6f 74 68 65 72 20 69 6e 20 74 68 65 20 64 61 74  other in the dat
1755a 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 77  abase file,.** w
1755b 68 69 63 68 20 69 6e 20 74 75 72 6e 20 63 61 6e  hich in turn can
1755c 20 6d 61 6b 65 20 64 61 74 61 62 61 73 65 20 61   make database a
1755d 63 63 65 73 73 20 66 61 73 74 65 72 2e 0a 2a 2a  ccess faster..**
1755e 0a 2a 2a 20 49 66 20 74 68 65 20 22 65 78 61 63  .** If the "exac
1755f 74 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  t" parameter is 
17560 6e 6f 74 20 30 2c 20 61 6e 64 20 74 68 65 20 70  not 0, and the p
17561 61 67 65 2d 6e 75 6d 62 65 72 20 6e 65 61 72 62  age-number nearb
17562 79 20 65 78 69 73 74 73 20 0a 2a 2a 20 61 6e 79  y exists .** any
17563 77 68 65 72 65 20 6f 6e 20 74 68 65 20 66 72 65  where on the fre
17564 65 2d 6c 69 73 74 2c 20 74 68 65 6e 20 69 74 20  e-list, then it 
17565 69 73 20 67 75 61 72 65 6e 74 65 65 64 20 74 6f  is guarenteed to
17566 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68   be returned. Th
17567 69 73 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 75 73  is.** is only us
17568 65 64 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75  ed by auto-vacuu
17569 6d 20 64 61 74 61 62 61 73 65 73 20 77 68 65 6e  m databases when
1756a 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e 65   allocating a ne
1756b 77 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  w table..*/.stat
1756c 69 63 20 69 6e 74 20 61 6c 6c 6f 63 61 74 65 42  ic int allocateB
1756d 74 72 65 65 50 61 67 65 28 0a 20 20 42 74 53 68  treePage(.  BtSh
1756e 61 72 65 64 20 2a 70 42 74 2c 20 0a 20 20 4d 65  ared *pBt, .  Me
1756f 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20  mPage **ppPage, 
17570 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 2c 20  .  Pgno *pPgno, 
17571 0a 20 20 50 67 6e 6f 20 6e 65 61 72 62 79 2c 0a  .  Pgno nearby,.
17572 20 20 75 38 20 65 78 61 63 74 0a 29 7b 0a 20 20    u8 exact.){.  
17573 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b  MemPage *pPage1;
17574 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 33 32  .  int rc;.  u32
17575 20 6e 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65   n;     /* Numbe
17576 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68  r of pages on th
17577 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20 20  e freelist */.  
17578 75 33 32 20 6b 3b 20 20 20 20 20 2f 2a 20 4e 75  u32 k;     /* Nu
17579 6d 62 65 72 20 6f 66 20 6c 65 61 76 65 73 20 6f  mber of leaves o
1757a 6e 20 74 68 65 20 74 72 75 6e 6b 20 6f 66 20 74  n the trunk of t
1757b 68 65 20 66 72 65 65 6c 69 73 74 20 2a 2f 0a 20  he freelist */. 
1757c 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75 6e 6b   MemPage *pTrunk
1757d 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67 65 20   = 0;.  MemPage 
1757e 2a 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30 3b  *pPrevTrunk = 0;
1757f 0a 20 20 50 67 6e 6f 20 6d 78 50 61 67 65 3b 20  .  Pgno mxPage; 
17580 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a      /* Total siz
17581 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
17582 65 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 61 73 73  e file */..  ass
17583 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
17584 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
17585 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 31 20  ex) );.  pPage1 
17586 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20  = pBt->pPage1;. 
17587 20 6d 78 50 61 67 65 20 3d 20 70 61 67 65 72 50   mxPage = pagerP
17588 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20  agecount(pBt);. 
17589 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26 70   n = get4byte(&p
1758a 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d  Page1->aData[36]
1758b 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
1758c 3d 3d 6d 78 50 61 67 65 2d 31 20 29 3b 0a 20 20  ==mxPage-1 );.  
1758d 69 66 28 20 6e 3e 3d 6d 78 50 61 67 65 20 29 7b  if( n>=mxPage ){
1758e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1758f 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
17590 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 30 20 29  .  }.  if( n>0 )
17591 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61  {.    /* There a
17592 72 65 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20  re pages on the 
17593 66 72 65 65 6c 69 73 74 2e 20 20 52 65 75 73 65  freelist.  Reuse
17594 20 6f 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61   one of those pa
17595 67 65 73 2e 20 2a 2f 0a 20 20 20 20 50 67 6e 6f  ges. */.    Pgno
17596 20 69 54 72 75 6e 6b 3b 0a 20 20 20 20 75 38 20   iTrunk;.    u8 
17597 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 20  searchList = 0; 
17598 2f 2a 20 49 66 20 74 68 65 20 66 72 65 65 2d 6c  /* If the free-l
17599 69 73 74 20 6d 75 73 74 20 62 65 20 73 65 61 72  ist must be sear
1759a 63 68 65 64 20 66 6f 72 20 27 6e 65 61 72 62 79  ched for 'nearby
1759b 27 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 2f 2a  ' */.    .    /*
1759c 20 49 66 20 74 68 65 20 27 65 78 61 63 74 27 20   If the 'exact' 
1759d 70 61 72 61 6d 65 74 65 72 20 77 61 73 20 74 72  parameter was tr
1759e 75 65 20 61 6e 64 20 61 20 71 75 65 72 79 20 6f  ue and a query o
1759f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  f the pointer-ma
175a0 70 0a 20 20 20 20 2a 2a 20 73 68 6f 77 73 20 74  p.    ** shows t
175a1 68 61 74 20 74 68 65 20 70 61 67 65 20 27 6e 65  hat the page 'ne
175a2 61 72 62 79 27 20 69 73 20 73 6f 6d 65 77 68 65  arby' is somewhe
175a3 72 65 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c  re on the free-l
175a4 69 73 74 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a  ist, then.    **
175a5 20 74 68 65 20 65 6e 74 69 72 65 2d 6c 69 73 74   the entire-list
175a6 20 77 69 6c 6c 20 62 65 20 73 65 61 72 63 68 65   will be searche
175a7 64 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 2e  d for that page.
175a8 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  .    */.#ifndef 
175a9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
175aa 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 65  VACUUM.    if( e
175ab 78 61 63 74 20 26 26 20 6e 65 61 72 62 79 3c 3d  xact && nearby<=
175ac 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20  mxPage ){.      
175ad 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20  u8 eType;.      
175ae 61 73 73 65 72 74 28 20 6e 65 61 72 62 79 3e 30  assert( nearby>0
175af 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
175b0 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
175b1 6d 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  m );.      rc = 
175b2 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 6e  ptrmapGet(pBt, n
175b3 65 61 72 62 79 2c 20 26 65 54 79 70 65 2c 20 30  earby, &eType, 0
175b4 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
175b5 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
175b6 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54     if( eType==PT
175b7 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29 7b  RMAP_FREEPAGE ){
175b8 0a 20 20 20 20 20 20 20 20 73 65 61 72 63 68 4c  .        searchL
175b9 69 73 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  ist = 1;.      }
175ba 0a 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20  .      *pPgno = 
175bb 6e 65 61 72 62 79 3b 0a 20 20 20 20 7d 0a 23 65  nearby;.    }.#e
175bc 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44 65 63  ndif..    /* Dec
175bd 72 65 6d 65 6e 74 20 74 68 65 20 66 72 65 65 2d  rement the free-
175be 6c 69 73 74 20 63 6f 75 6e 74 20 62 79 20 31 2e  list count by 1.
175bf 20 53 65 74 20 69 54 72 75 6e 6b 20 74 6f 20 74   Set iTrunk to t
175c0 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a  he index of the.
175c1 20 20 20 20 2a 2a 20 66 69 72 73 74 20 66 72 65      ** first fre
175c2 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70 61 67  e-list trunk pag
175c3 65 2e 20 69 50 72 65 76 54 72 75 6e 6b 20 69 73  e. iPrevTrunk is
175c4 20 69 6e 69 74 69 61 6c 6c 79 20 31 2e 0a 20 20   initially 1..  
175c5 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71    */.    rc = sq
175c6 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
175c7 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
175c8 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
175c9 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 70 75  eturn rc;.    pu
175ca 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e  t4byte(&pPage1->
175cb 61 44 61 74 61 5b 33 36 5d 2c 20 6e 2d 31 29 3b  aData[36], n-1);
175cc 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 64  ..    /* The cod
175cd 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 6c 6f  e within this lo
175ce 6f 70 20 69 73 20 72 75 6e 20 6f 6e 6c 79 20 6f  op is run only o
175cf 6e 63 65 20 69 66 20 74 68 65 20 27 73 65 61 72  nce if the 'sear
175d0 63 68 4c 69 73 74 27 20 76 61 72 69 61 62 6c 65  chList' variable
175d1 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 74  .    ** is not t
175d2 72 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  rue. Otherwise, 
175d3 69 74 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72  it runs once for
175d4 20 65 61 63 68 20 74 72 75 6e 6b 2d 70 61 67 65   each trunk-page
175d5 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 66   on the.    ** f
175d6 72 65 65 2d 6c 69 73 74 20 75 6e 74 69 6c 20 74  ree-list until t
175d7 68 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27  he page 'nearby'
175d8 20 69 73 20 6c 6f 63 61 74 65 64 2e 0a 20 20 20   is located..   
175d9 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20   */.    do {.   
175da 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d 20     pPrevTrunk = 
175db 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 69 66  pTrunk;.      if
175dc 28 20 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a  ( pPrevTrunk ){.
175dd 20 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d          iTrunk =
175de 20 67 65 74 34 62 79 74 65 28 26 70 50 72 65 76   get4byte(&pPrev
175df 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 29  Trunk->aData[0])
175e0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
175e1 20 20 20 20 20 20 20 69 54 72 75 6e 6b 20 3d 20         iTrunk = 
175e2 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31  get4byte(&pPage1
175e3 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a 20 20  ->aData[32]);.  
175e4 20 20 20 20 7d 0a 20 20 20 20 20 20 74 65 73 74      }.      test
175e5 63 61 73 65 28 20 69 54 72 75 6e 6b 3d 3d 6d 78  case( iTrunk==mx
175e6 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 69 66  Page );.      if
175e7 28 20 69 54 72 75 6e 6b 3e 6d 78 50 61 67 65 20  ( iTrunk>mxPage 
175e8 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
175e9 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
175ea 4b 50 54 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  KPT;.      }else
175eb 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62  {.        rc = b
175ec 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
175ed 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75 6e 6b   iTrunk, &pTrunk
175ee 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 0);.      }.  
175ef 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
175f0 20 20 20 20 20 20 70 54 72 75 6e 6b 20 3d 20 30        pTrunk = 0
175f1 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65  ;.        goto e
175f2 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
175f3 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
175f4 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26 70   k = get4byte(&p
175f5 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29  Trunk->aData[4])
175f6 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30  ;.      if( k==0
175f7 20 26 26 20 21 73 65 61 72 63 68 4c 69 73 74 20   && !searchList 
175f8 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
175f9 65 20 74 72 75 6e 6b 20 68 61 73 20 6e 6f 20 6c  e trunk has no l
175fa 65 61 76 65 73 20 61 6e 64 20 74 68 65 20 6c 69  eaves and the li
175fb 73 74 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20  st is not being 
175fc 73 65 61 72 63 68 65 64 2e 20 0a 20 20 20 20 20  searched. .     
175fd 20 20 20 2a 2a 20 53 6f 20 65 78 74 72 61 63 74     ** So extract
175fe 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20   the trunk page 
175ff 69 74 73 65 6c 66 20 61 6e 64 20 75 73 65 20 69  itself and use i
17600 74 20 61 73 20 74 68 65 20 6e 65 77 6c 79 20 0a  t as the newly .
17601 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 6f 63          ** alloc
17602 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20 20 20  ated page */.   
17603 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72       assert( pPr
17604 65 76 54 72 75 6e 6b 3d 3d 30 20 29 3b 0a 20 20  evTrunk==0 );.  
17605 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
17606 65 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72  e3PagerWrite(pTr
17607 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  unk->pDbPage);. 
17608 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b         if( rc ){
17609 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1760a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
1760b 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
1760c 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 69 54       *pPgno = iT
1760d 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 6d 65  runk;.        me
1760e 6d 63 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44  mcpy(&pPage1->aD
1760f 61 74 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b  ata[32], &pTrunk
17610 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a  ->aData[0], 4);.
17611 20 20 20 20 20 20 20 20 2a 70 70 50 61 67 65 20          *ppPage 
17612 3d 20 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20  = pTrunk;.      
17613 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20    pTrunk = 0;.  
17614 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c        TRACE(("AL
17615 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b  LOCATE: %d trunk
17616 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73   - %d free pages
17617 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f   left\n", *pPgno
17618 2c 20 6e 2d 31 29 29 3b 0a 20 20 20 20 20 20 7d  , n-1));.      }
17619 65 6c 73 65 20 69 66 28 20 6b 3e 28 75 33 32 29  else if( k>(u32)
1761a 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
1761b 2f 34 20 2d 20 32 29 20 29 7b 0a 20 20 20 20 20  /4 - 2) ){.     
1761c 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 6b     /* Value of k
1761d 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
1761e 2e 20 20 44 61 74 61 62 61 73 65 20 63 6f 72 72  .  Database corr
1761f 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  uption */.      
17620 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
17621 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
17622 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
17623 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23 69 66 6e  ocate_page;.#ifn
17624 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
17625 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
17626 20 7d 65 6c 73 65 20 69 66 28 20 73 65 61 72 63   }else if( searc
17627 68 4c 69 73 74 20 26 26 20 6e 65 61 72 62 79 3d  hList && nearby=
17628 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20  =iTrunk ){.     
17629 20 20 20 2f 2a 20 54 68 65 20 6c 69 73 74 20 69     /* The list i
1762a 73 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 64  s being searched
1762b 20 61 6e 64 20 74 68 69 73 20 74 72 75 6e 6b 20   and this trunk 
1762c 70 61 67 65 20 69 73 20 74 68 65 20 70 61 67 65  page is the page
1762d 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61  .        ** to a
1762e 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61 72 64 6c  llocate, regardl
1762f 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 69  ess of whether i
17630 74 20 68 61 73 20 6c 65 61 76 65 73 2e 0a 20 20  t has leaves..  
17631 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
17632 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 3d   assert( *pPgno=
17633 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20 20 20 20  =iTrunk );.     
17634 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72     *ppPage = pTr
17635 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 73 65 61  unk;.        sea
17636 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20  rchList = 0;.   
17637 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
17638 33 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75  3PagerWrite(pTru
17639 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  nk->pDbPage);.  
1763a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
1763b 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
1763c 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65  nd_allocate_page
1763d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1763e 20 20 20 20 69 66 28 20 6b 3d 3d 30 20 29 7b 0a      if( k==0 ){.
1763f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70            if( !p
17640 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20  PrevTrunk ){.   
17641 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
17642 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
17643 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61  2], &pTrunk->aDa
17644 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20  ta[0], 4);.     
17645 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
17646 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
17647 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44 61 74  pPrevTrunk->aDat
17648 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61  a[0], &pTrunk->a
17649 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20  Data[0], 4);.   
1764a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1764b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1764c 20 20 2f 2a 20 54 68 65 20 74 72 75 6e 6b 20 70    /* The trunk p
1764d 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64 20  age is required 
1764e 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 62 75  by the caller bu
1764f 74 20 69 74 20 63 6f 6e 74 61 69 6e 73 20 0a 20  t it contains . 
17650 20 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e           ** poin
17651 74 65 72 73 20 74 6f 20 66 72 65 65 2d 6c 69 73  ters to free-lis
17652 74 20 6c 65 61 76 65 73 2e 20 54 68 65 20 66 69  t leaves. The fi
17653 72 73 74 20 6c 65 61 66 20 62 65 63 6f 6d 65 73  rst leaf becomes
17654 20 61 20 74 72 75 6e 6b 0a 20 20 20 20 20 20 20   a trunk.       
17655 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68     ** page in th
17656 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20  is case..       
17657 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
17658 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 54 72 75  MemPage *pNewTru
17659 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 50 67  nk;.          Pg
1765a 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20 3d 20 67  no iNewTrunk = g
1765b 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d  et4byte(&pTrunk-
1765c 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20 20 20 20  >aData[8]);.    
1765d 20 20 20 20 20 20 69 66 28 20 69 4e 65 77 54 72        if( iNewTr
1765e 75 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 20 0a 20  unk>mxPage ){ . 
1765f 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
17660 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
17661 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 20  KPT;.           
17662 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
17663 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20  te_page;.       
17664 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 74     }.          t
17665 65 73 74 63 61 73 65 28 20 69 4e 65 77 54 72 75  estcase( iNewTru
17666 6e 6b 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20  nk==mxPage );.  
17667 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 72          rc = btr
17668 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69  eeGetPage(pBt, i
17669 4e 65 77 54 72 75 6e 6b 2c 20 26 70 4e 65 77 54  NewTrunk, &pNewT
1766a 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20  runk, 0);.      
1766b 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1766c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1766d 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c       goto end_al
1766e 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20  locate_page;.   
1766f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
17670 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
17671 61 67 65 72 57 72 69 74 65 28 70 4e 65 77 54 72  agerWrite(pNewTr
17672 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  unk->pDbPage);. 
17673 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
17674 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17675 20 20 20 20 20 20 20 20 20 20 72 65 6c 65 61 73            releas
17676 65 50 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29  ePage(pNewTrunk)
17677 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f  ;.            go
17678 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  to end_allocate_
17679 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20  page;.          
1767a 7d 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63  }.          memc
1767b 70 79 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61  py(&pNewTrunk->a
1767c 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b  Data[0], &pTrunk
1767d 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a  ->aData[0], 4);.
1767e 20 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79            put4by
1767f 74 65 28 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61  te(&pNewTrunk->a
17680 44 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20  Data[4], k-1);. 
17681 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
17682 26 70 4e 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74  &pNewTrunk->aDat
17683 61 5b 38 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61  a[8], &pTrunk->a
17684 44 61 74 61 5b 31 32 5d 2c 20 28 6b 2d 31 29 2a  Data[12], (k-1)*
17685 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  4);.          re
17686 6c 65 61 73 65 50 61 67 65 28 70 4e 65 77 54 72  leasePage(pNewTr
17687 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20  unk);.          
17688 69 66 28 20 21 70 50 72 65 76 54 72 75 6e 6b 20  if( !pPrevTrunk 
17689 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
1768a 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
1768b 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
1768c 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29 20  Page1->pDbPage) 
1768d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  );.            p
1768e 75 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d  ut4byte(&pPage1-
1768f 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 4e 65 77  >aData[32], iNew
17690 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20 20 20  Trunk);.        
17691 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
17692 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
17693 33 50 61 67 65 72 57 72 69 74 65 28 70 50 72 65  3PagerWrite(pPre
17694 76 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29  vTrunk->pDbPage)
17695 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
17696 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
17697 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
17698 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20  llocate_page;.  
17699 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1769a 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65          put4byte
1769b 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61 44  (&pPrevTrunk->aD
1769c 61 74 61 5b 30 5d 2c 20 69 4e 65 77 54 72 75 6e  ata[0], iNewTrun
1769d 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  k);.          }.
1769e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1769f 20 20 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20    pTrunk = 0;.  
176a0 20 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c        TRACE(("AL
176a1 4c 4f 43 41 54 45 3a 20 25 64 20 74 72 75 6e 6b  LOCATE: %d trunk
176a2 20 2d 20 25 64 20 66 72 65 65 20 70 61 67 65 73   - %d free pages
176a3 20 6c 65 66 74 5c 6e 22 2c 20 2a 70 50 67 6e 6f   left\n", *pPgno
176a4 2c 20 6e 2d 31 29 29 3b 0a 23 65 6e 64 69 66 0a  , n-1));.#endif.
176a5 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
176a6 6b 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  k>0 ){.        /
176a7 2a 20 45 78 74 72 61 63 74 20 61 20 6c 65 61 66  * Extract a leaf
176a8 20 66 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b 20   from the trunk 
176a9 2a 2f 0a 20 20 20 20 20 20 20 20 75 33 32 20 63  */.        u32 c
176aa 6c 6f 73 65 73 74 3b 0a 20 20 20 20 20 20 20 20  losest;.        
176ab 50 67 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20 20  Pgno iPage;.    
176ac 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
176ad 72 20 2a 61 44 61 74 61 20 3d 20 70 54 72 75 6e  r *aData = pTrun
176ae 6b 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20  k->aData;.      
176af 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
176b0 67 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d  gerWrite(pTrunk-
176b1 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
176b2 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
176b3 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
176b4 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
176b5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
176b6 20 69 66 28 20 6e 65 61 72 62 79 3e 30 20 29 7b   if( nearby>0 ){
176b7 0a 20 20 20 20 20 20 20 20 20 20 75 33 32 20 69  .          u32 i
176b8 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  ;.          int 
176b9 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20  dist;.          
176ba 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20  closest = 0;.   
176bb 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 67 65         dist = ge
176bc 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 5d  t4byte(&aData[8]
176bd 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20  ) - nearby;.    
176be 20 20 20 20 20 20 69 66 28 20 64 69 73 74 3c 30        if( dist<0
176bf 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73 74 3b   ) dist = -dist;
176c0 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69  .          for(i
176c1 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20  =1; i<k; i++){. 
176c2 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 64             int d
176c3 32 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44  2 = get4byte(&aD
176c4 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e 65  ata[8+i*4]) - ne
176c5 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20  arby;.          
176c6 20 20 69 66 28 20 64 32 3c 30 20 29 20 64 32 20    if( d2<0 ) d2 
176c7 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20 20 20  = -d2;.         
176c8 20 20 20 69 66 28 20 64 32 3c 64 69 73 74 20 29     if( d2<dist )
176c9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
176ca 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 20  closest = i;.   
176cb 20 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20             dist 
176cc 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20  = d2;.          
176cd 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
176ce 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
176cf 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74           closest
176d0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
176d1 0a 20 20 20 20 20 20 20 20 69 50 61 67 65 20 3d  .        iPage =
176d2 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61   get4byte(&aData
176d3 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 3b 0a  [8+closest*4]);.
176d4 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
176d5 28 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20  ( iPage==mxPage 
176d6 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
176d7 50 61 67 65 3e 6d 78 50 61 67 65 20 29 7b 0a 20  Page>mxPage ){. 
176d8 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
176d9 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
176da 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  T;.          got
176db 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
176dc 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  age;.        }. 
176dd 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
176de 20 69 50 61 67 65 3d 3d 6d 78 50 61 67 65 20 29   iPage==mxPage )
176df 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 73  ;.        if( !s
176e0 65 61 72 63 68 4c 69 73 74 20 7c 7c 20 69 50 61  earchList || iPa
176e1 67 65 3d 3d 6e 65 61 72 62 79 20 29 7b 0a 20 20  ge==nearby ){.  
176e2 20 20 20 20 20 20 20 20 69 6e 74 20 6e 6f 43 6f          int noCo
176e3 6e 74 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 20  ntent;.         
176e4 20 2a 70 50 67 6e 6f 20 3d 20 69 50 61 67 65 3b   *pPgno = iPage;
176e5 0a 20 20 20 20 20 20 20 20 20 20 54 52 41 43 45  .          TRACE
176e6 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20  (("ALLOCATE: %d 
176e7 77 61 73 20 6c 65 61 66 20 25 64 20 6f 66 20 25  was leaf %d of %
176e8 64 20 6f 6e 20 74 72 75 6e 6b 20 25 64 22 0a 20  d on trunk %d". 
176e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
176ea 22 3a 20 25 64 20 6d 6f 72 65 20 66 72 65 65 20  ": %d more free 
176eb 70 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20  pages\n",.      
176ec 20 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e             *pPgn
176ed 6f 2c 20 63 6c 6f 73 65 73 74 2b 31 2c 20 6b 2c  o, closest+1, k,
176ee 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 2c 20 6e   pTrunk->pgno, n
176ef 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  -1));.          
176f0 69 66 28 20 63 6c 6f 73 65 73 74 3c 6b 2d 31 20  if( closest<k-1 
176f1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d  ){.            m
176f2 65 6d 63 70 79 28 26 61 44 61 74 61 5b 38 2b 63  emcpy(&aData[8+c
176f3 6c 6f 73 65 73 74 2a 34 5d 2c 20 26 61 44 61 74  losest*4], &aDat
176f4 61 5b 34 2b 6b 2a 34 5d 2c 20 34 29 3b 0a 20 20  a[4+k*4], 4);.  
176f5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
176f6 20 20 20 20 70 75 74 34 62 79 74 65 28 26 61 44      put4byte(&aD
176f7 61 74 61 5b 34 5d 2c 20 6b 2d 31 29 3b 0a 20 20  ata[4], k-1);.  
176f8 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
176f9 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
176fa 69 74 65 61 62 6c 65 28 70 54 72 75 6e 6b 2d 3e  iteable(pTrunk->
176fb 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20  pDbPage) );.    
176fc 20 20 20 20 20 20 6e 6f 43 6f 6e 74 65 6e 74 20        noContent 
176fd 3d 20 21 62 74 72 65 65 47 65 74 48 61 73 43 6f  = !btreeGetHasCo
176fe 6e 74 65 6e 74 28 70 42 74 2c 20 2a 70 50 67 6e  ntent(pBt, *pPgn
176ff 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63  o);.          rc
17700 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
17701 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70 50  pBt, *pPgno, ppP
17702 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b  age, noContent);
17703 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72  .          if( r
17704 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
17705 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d              rc =
17706 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
17707 74 65 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44  te((*ppPage)->pD
17708 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
17709 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1770a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1770b 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
1770c 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20  ge(*ppPage);.   
1770d 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1770e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1770f 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b   searchList = 0;
17710 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
17711 20 7d 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65   }.      release
17712 50 61 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29  Page(pPrevTrunk)
17713 3b 0a 20 20 20 20 20 20 70 50 72 65 76 54 72 75  ;.      pPrevTru
17714 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69  nk = 0;.    }whi
17715 6c 65 28 20 73 65 61 72 63 68 4c 69 73 74 20 29  le( searchList )
17716 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
17717 2a 20 54 68 65 72 65 20 61 72 65 20 6e 6f 20 70  * There are no p
17718 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65  ages on the free
17719 6c 69 73 74 2c 20 73 6f 20 63 72 65 61 74 65 20  list, so create 
1771a 61 20 6e 65 77 20 70 61 67 65 20 61 74 20 74 68  a new page at th
1771b 65 0a 20 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20  e.    ** end of 
1771c 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20  the file */.    
1771d 69 6e 74 20 6e 50 61 67 65 20 3d 20 70 61 67 65  int nPage = page
1771e 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 3b  rPagecount(pBt);
1771f 0a 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50  .    *pPgno = nP
17720 61 67 65 20 2b 20 31 3b 0a 0a 20 20 20 20 69 66  age + 1;..    if
17721 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e  ( *pPgno==PENDIN
17722 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
17723 20 29 7b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e   ){.      (*pPgn
17724 6f 29 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  o)++;.    }..#if
17725 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17726 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
17727 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
17728 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53  uum && PTRMAP_IS
17729 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f  PAGE(pBt, *pPgno
1772a 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  ) ){.      /* If
1772b 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74   *pPgno refers t
1772c 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  o a pointer-map 
1772d 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74  page, allocate t
1772e 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20  wo new pages.   
1772f 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64     ** at the end
17730 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73   of the file ins
17731 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65  tead of one. The
17732 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64   first allocated
17733 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62   page.      ** b
17734 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69  ecomes a new poi
17735 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74  nter-map page, t
17736 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65  he second is use
17737 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e  d by the caller.
17738 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
17739 4d 65 6d 50 61 67 65 20 2a 70 50 67 20 3d 20 30  MemPage *pPg = 0
1773a 3b 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22  ;.      TRACE(("
1773b 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20 66 72 6f  ALLOCATE: %d fro
1773c 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65 20 28 70  m end of file (p
1773d 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 29  ointer-map page)
1773e 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20  \n", *pPgno));. 
1773f 20 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50       assert( *pP
17740 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54  gno!=PENDING_BYT
17741 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a 20  E_PAGE(pBt) );. 
17742 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47       rc = btreeG
17743 65 74 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67  etPage(pBt, *pPg
17744 6e 6f 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 20  no, &pPg, 0);.  
17745 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
17746 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
17747 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
17748 65 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 62  erWrite(pPg->pDb
17749 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72  Page);.        r
1774a 65 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 3b  eleasePage(pPg);
1774b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
1774c 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1774d 63 3b 0a 20 20 20 20 20 20 28 2a 70 50 67 6e 6f  c;.      (*pPgno
1774e 29 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 2a  )++;.      if( *
1774f 70 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42  pPgno==PENDING_B
17750 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
17751 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 20 7d 0a 20   (*pPgno)++; }. 
17752 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
17753 20 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21   assert( *pPgno!
17754 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
17755 47 45 28 70 42 74 29 20 29 3b 0a 20 20 20 20 72  GE(pBt) );.    r
17756 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
17757 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 70 70  (pBt, *pPgno, pp
17758 50 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66  Page, 0);.    if
17759 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
1775a 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1775b 65 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70  e3PagerWrite((*p
1775c 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29  pPage)->pDbPage)
1775d 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
1775e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1775f 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70   releasePage(*pp
17760 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Page);.    }.   
17761 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54   TRACE(("ALLOCAT
17762 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f  E: %d from end o
17763 66 20 66 69 6c 65 5c 6e 22 2c 20 2a 70 50 67 6e  f file\n", *pPgn
17764 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  o));.  }..  asse
17765 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44  rt( *pPgno!=PEND
17766 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42  ING_BYTE_PAGE(pB
17767 74 29 20 29 3b 0a 0a 65 6e 64 5f 61 6c 6c 6f 63  t) );..end_alloc
17768 61 74 65 5f 70 61 67 65 3a 0a 20 20 72 65 6c 65  ate_page:.  rele
17769 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b  asePage(pTrunk);
1776a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70  .  releasePage(p
1776b 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 69 66  PrevTrunk);.  if
1776c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1776d 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
1776e 65 33 50 61 67 65 72 50 61 67 65 52 65 66 63 6f  e3PagerPageRefco
1776f 75 6e 74 28 28 2a 70 70 50 61 67 65 29 2d 3e 70  unt((*ppPage)->p
17770 44 62 50 61 67 65 29 3e 31 20 29 7b 0a 20 20 20  DbPage)>1 ){.   
17771 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a     releasePage(*
17772 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 72  ppPage);.      r
17773 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
17774 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
17775 0a 20 20 20 20 28 2a 70 70 50 61 67 65 29 2d 3e  .    (*ppPage)->
17776 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 65  isInit = 0;.  }e
17777 6c 73 65 7b 0a 20 20 20 20 2a 70 70 50 61 67 65  lse{.    *ppPage
17778 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
17779 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1777a 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
1777b 20 75 73 65 64 20 74 6f 20 61 64 64 20 70 61 67   used to add pag
1777c 65 20 69 50 61 67 65 20 74 6f 20 74 68 65 20 64  e iPage to the d
1777d 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 72 65  atabase file fre
1777e 65 2d 6c 69 73 74 2e 20 0a 2a 2a 20 49 74 20 69  e-list. .** It i
1777f 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74  s assumed that t
17780 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  he page is not a
17781 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 6f 66  lready a part of
17782 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 0a   the free-list..
17783 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  **.** The value 
17784 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
17785 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
17786 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
17787 73 20 6f 70 74 69 6f 6e 61 6c 2e 0a 2a 2a 20 49  s optional..** I
17788 66 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 70  f the caller hap
17789 70 65 6e 73 20 74 6f 20 68 61 76 65 20 61 20 70  pens to have a p
1778a 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65  ointer to the Me
1778b 6d 50 61 67 65 20 6f 62 6a 65 63 74 20 0a 2a 2a  mPage object .**
1778c 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
1778d 6f 20 70 61 67 65 20 69 50 61 67 65 20 68 61 6e  o page iPage han
1778e 64 79 2c 20 69 74 20 6d 61 79 20 70 61 73 73 20  dy, it may pass 
1778f 69 74 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  it as the second
17790 20 76 61 6c 75 65 2e 20 0a 2a 2a 20 4f 74 68 65   value. .** Othe
17791 72 77 69 73 65 2c 20 69 74 20 6d 61 79 20 70 61  rwise, it may pa
17792 73 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49  ss NULL..**.** I
17793 66 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  f a pointer to a
17794 20 4d 65 6d 50 61 67 65 20 6f 62 6a 65 63 74 20   MemPage object 
17795 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65  is passed as the
17796 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
17797 2c 0a 2a 2a 20 69 74 73 20 72 65 66 65 72 65 6e  ,.** its referen
17798 63 65 20 63 6f 75 6e 74 20 69 73 20 6e 6f 74 20  ce count is not 
17799 61 6c 74 65 72 65 64 20 62 79 20 74 68 69 73 20  altered by this 
1779a 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
1779b 74 69 63 20 69 6e 74 20 66 72 65 65 50 61 67 65  tic int freePage
1779c 32 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c  2(BtShared *pBt,
1779d 20 4d 65 6d 50 61 67 65 20 2a 70 4d 65 6d 50 61   MemPage *pMemPa
1779e 67 65 2c 20 50 67 6e 6f 20 69 50 61 67 65 29 7b  ge, Pgno iPage){
1779f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 72 75  .  MemPage *pTru
177a0 6e 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  nk = 0;         
177a1 20 20 20 20 20 20 20 2f 2a 20 46 72 65 65 2d 6c         /* Free-l
177a2 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a  ist trunk page *
177a3 2f 0a 20 20 50 67 6e 6f 20 69 54 72 75 6e 6b 20  /.  Pgno iTrunk 
177a4 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
177a5 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
177a6 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 2d 6c  number of free-l
177a7 69 73 74 20 74 72 75 6e 6b 20 70 61 67 65 20 2a  ist trunk page *
177a8 2f 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  / .  MemPage *pP
177a9 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61 67  age1 = pBt->pPag
177aa 65 31 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61  e1;      /* Loca
177ab 6c 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70  l reference to p
177ac 61 67 65 20 31 20 2a 2f 0a 20 20 4d 65 6d 50 61  age 1 */.  MemPa
177ad 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20  ge *pPage;      
177ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
177af 2a 20 50 61 67 65 20 62 65 69 6e 67 20 66 72 65  * Page being fre
177b0 65 64 2e 20 4d 61 79 20 62 65 20 4e 55 4c 4c 2e  ed. May be NULL.
177b1 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
177b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
177b3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
177b4 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e  urn Code */.  in
177b5 74 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20  t nFree;        
177b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
177b7 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75 6d    /* Initial num
177b8 62 65 72 20 6f 66 20 70 61 67 65 73 20 6f 6e 20  ber of pages on 
177b9 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20  free-list */..  
177ba 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
177bb 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
177bc 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
177bd 72 74 28 20 69 50 61 67 65 3e 31 20 29 3b 0a 20  rt( iPage>1 );. 
177be 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 50 61   assert( !pMemPa
177bf 67 65 20 7c 7c 20 70 4d 65 6d 50 61 67 65 2d 3e  ge || pMemPage->
177c0 70 67 6e 6f 3d 3d 69 50 61 67 65 20 29 3b 0a 0a  pgno==iPage );..
177c1 20 20 69 66 28 20 70 4d 65 6d 50 61 67 65 20 29    if( pMemPage )
177c2 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 4d  {.    pPage = pM
177c3 65 6d 50 61 67 65 3b 0a 20 20 20 20 73 71 6c 69  emPage;.    sqli
177c4 74 65 33 50 61 67 65 72 52 65 66 28 70 50 61 67  te3PagerRef(pPag
177c5 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d  e->pDbPage);.  }
177c6 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65 20  else{.    pPage 
177c7 3d 20 62 74 72 65 65 50 61 67 65 4c 6f 6f 6b 75  = btreePageLooku
177c8 70 28 70 42 74 2c 20 69 50 61 67 65 29 3b 0a 20  p(pBt, iPage);. 
177c9 20 7d 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d 65   }..  /* Increme
177ca 6e 74 20 74 68 65 20 66 72 65 65 20 70 61 67 65  nt the free page
177cb 20 63 6f 75 6e 74 20 6f 6e 20 70 50 61 67 65 31   count on pPage1
177cc 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
177cd 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
177ce 67 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  ge1->pDbPage);. 
177cf 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 66   if( rc ) goto f
177d0 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 6e  reepage_out;.  n
177d1 46 72 65 65 20 3d 20 67 65 74 34 62 79 74 65 28  Free = get4byte(
177d2 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
177d3 36 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28  6]);.  put4byte(
177d4 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
177d5 36 5d 2c 20 6e 46 72 65 65 2b 31 29 3b 0a 0a 23  6], nFree+1);..#
177d6 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 45 43  ifdef SQLITE_SEC
177d7 55 52 45 5f 44 45 4c 45 54 45 0a 20 20 2f 2a 20  URE_DELETE.  /* 
177d8 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53 45  If the SQLITE_SE
177d9 43 55 52 45 5f 44 45 4c 45 54 45 20 63 6f 6d 70  CURE_DELETE comp
177da 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20  ile-time option 
177db 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e  is enabled, then
177dc 0a 20 20 2a 2a 20 61 6c 77 61 79 73 20 66 75 6c  .  ** always ful
177dd 6c 79 20 6f 76 65 72 77 72 69 74 65 20 64 65 6c  ly overwrite del
177de 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  eted information
177df 20 77 69 74 68 20 7a 65 72 6f 73 2e 0a 20 20 2a   with zeros..  *
177e0 2f 0a 20 20 69 66 28 20 28 21 70 50 61 67 65 20  /.  if( (!pPage 
177e1 26 26 20 28 72 63 20 3d 20 62 74 72 65 65 47 65  && (rc = btreeGe
177e2 74 50 61 67 65 28 70 42 74 2c 20 69 50 61 67 65  tPage(pBt, iPage
177e3 2c 20 26 70 50 61 67 65 2c 20 30 29 29 29 0a 20  , &pPage, 0))). 
177e4 20 20 7c 7c 20 20 20 20 20 20 20 20 20 20 20 20    ||            
177e5 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67  (rc = sqlite3Pag
177e6 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70  erWrite(pPage->p
177e7 44 62 50 61 67 65 29 29 0a 20 20 29 7b 0a 20 20  DbPage)).  ){.  
177e8 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f    goto freepage_
177e9 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  out;.  }.  memse
177ea 74 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20  t(pPage->aData, 
177eb 30 2c 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70  0, pPage->pBt->p
177ec 61 67 65 53 69 7a 65 29 3b 0a 23 65 6e 64 69 66  ageSize);.#endif
177ed 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 61  ..  /* If the da
177ee 74 61 62 61 73 65 20 73 75 70 70 6f 72 74 73 20  tabase supports 
177ef 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20 77 72 69  auto-vacuum, wri
177f0 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74  te an entry in t
177f1 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20  he pointer-map. 
177f2 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20   ** to indicate 
177f3 74 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73  that the page is
177f4 20 66 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   free..  */.  if
177f5 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29  ( ISAUTOVACUUM )
177f6 7b 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28  {.    ptrmapPut(
177f7 70 42 74 2c 20 69 50 61 67 65 2c 20 50 54 52 4d  pBt, iPage, PTRM
177f8 41 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20  AP_FREEPAGE, 0, 
177f9 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63  &rc);.    if( rc
177fa 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65   ) goto freepage
177fb 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  _out;.  }..  /* 
177fc 4e 6f 77 20 6d 61 6e 69 70 75 6c 61 74 65 20 74  Now manipulate t
177fd 68 65 20 61 63 74 75 61 6c 20 64 61 74 61 62 61  he actual databa
177fe 73 65 20 66 72 65 65 2d 6c 69 73 74 20 73 74 72  se free-list str
177ff 75 63 74 75 72 65 2e 20 54 68 65 72 65 20 61 72  ucture. There ar
17800 65 20 74 77 6f 0a 20 20 2a 2a 20 70 6f 73 73 69  e two.  ** possi
17801 62 69 6c 69 74 69 65 73 2e 20 49 66 20 74 68 65  bilities. If the
17802 20 66 72 65 65 2d 6c 69 73 74 20 69 73 20 63 75   free-list is cu
17803 72 72 65 6e 74 6c 79 20 65 6d 70 74 79 2c 20 6f  rrently empty, o
17804 72 20 69 66 20 74 68 65 20 66 69 72 73 74 0a 20  r if the first. 
17805 20 2a 2a 20 74 72 75 6e 6b 20 70 61 67 65 20 69   ** trunk page i
17806 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  n the free-list 
17807 69 73 20 66 75 6c 6c 2c 20 74 68 65 6e 20 74 68  is full, then th
17808 69 73 20 70 61 67 65 20 77 69 6c 6c 20 62 65 63  is page will bec
17809 6f 6d 65 20 61 0a 20 20 2a 2a 20 6e 65 77 20 66  ome a.  ** new f
1780a 72 65 65 2d 6c 69 73 74 20 74 72 75 6e 6b 20 70  ree-list trunk p
1780b 61 67 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  age. Otherwise, 
1780c 69 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61  it will become a
1780d 20 6c 65 61 66 20 6f 66 20 74 68 65 0a 20 20 2a   leaf of the.  *
1780e 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61  * first trunk pa
1780f 67 65 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e  ge in the curren
17810 74 20 66 72 65 65 2d 6c 69 73 74 2e 20 54 68 69  t free-list. Thi
17811 73 20 62 6c 6f 63 6b 20 74 65 73 74 73 20 69 66  s block tests if
17812 20 69 74 0a 20 20 2a 2a 20 69 73 20 70 6f 73 73   it.  ** is poss
17813 69 62 6c 65 20 74 6f 20 61 64 64 20 74 68 65 20  ible to add the 
17814 70 61 67 65 20 61 73 20 61 20 6e 65 77 20 66 72  page as a new fr
17815 65 65 2d 6c 69 73 74 20 6c 65 61 66 2e 0a 20 20  ee-list leaf..  
17816 2a 2f 0a 20 20 69 66 28 20 6e 46 72 65 65 21 3d  */.  if( nFree!=
17817 30 20 29 7b 0a 20 20 20 20 75 33 32 20 6e 4c 65  0 ){.    u32 nLe
17818 61 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  af;             
17819 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 6e 75     /* Initial nu
1781a 6d 62 65 72 20 6f 66 20 6c 65 61 66 20 63 65 6c  mber of leaf cel
1781b 6c 73 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65  ls on trunk page
1781c 20 2a 2f 0a 0a 20 20 20 20 69 54 72 75 6e 6b 20   */..    iTrunk 
1781d 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67  = get4byte(&pPag
1781e 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 3b 0a  e1->aData[32]);.
1781f 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65      rc = btreeGe
17820 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e  tPage(pBt, iTrun
17821 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a  k, &pTrunk, 0);.
17822 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
17823 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67  TE_OK ){.      g
17824 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74  oto freepage_out
17825 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c 65  ;.    }..    nLe
17826 61 66 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  af = get4byte(&p
17827 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 29  Trunk->aData[4])
17828 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
17829 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3e 33 32  t->usableSize>32
1782a 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 4c 65 61   );.    if( nLea
1782b 66 20 3e 20 28 75 33 32 29 70 42 74 2d 3e 75 73  f > (u32)pBt->us
1782c 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20 29  ableSize/4 - 2 )
1782d 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1782e 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
1782f 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65  ;.      goto fre
17830 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d  epage_out;.    }
17831 0a 20 20 20 20 69 66 28 20 6e 4c 65 61 66 20 3c  .    if( nLeaf <
17832 20 28 75 33 32 29 70 42 74 2d 3e 75 73 61 62 6c   (u32)pBt->usabl
17833 65 53 69 7a 65 2f 34 20 2d 20 38 20 29 7b 0a 20  eSize/4 - 8 ){. 
17834 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20       /* In this 
17835 63 61 73 65 20 74 68 65 72 65 20 69 73 20 72 6f  case there is ro
17836 6f 6d 20 6f 6e 20 74 68 65 20 74 72 75 6e 6b 20  om on the trunk 
17837 70 61 67 65 20 74 6f 20 69 6e 73 65 72 74 20 74  page to insert t
17838 68 65 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a  he page.      **
17839 20 62 65 69 6e 67 20 66 72 65 65 64 20 61 73 20   being freed as 
1783a 61 20 6e 65 77 20 6c 65 61 66 2e 0a 20 20 20 20  a new leaf..    
1783b 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f    **.      ** No
1783c 74 65 20 74 68 61 74 20 74 68 65 20 74 72 75 6e  te that the trun
1783d 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 72 65  k page is not re
1783e 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c 20  ally full until 
1783f 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20  it contains.    
17840 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65 2f    ** usableSize/
17841 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c 20 6e  4 - 2 entries, n
17842 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20  ot usableSize/4 
17843 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73 20 77  - 8 entries as w
17844 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a 20  e have.      ** 
17845 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75 65 20  coded.  But due 
17846 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f  to a coding erro
17847 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f 66  r in versions of
17848 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74 6f   SQLite prior to
17849 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30 2c  .      ** 3.6.0,
1784a 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68 20   databases with 
1784b 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20 70  freelist trunk p
1784c 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f 72  ages holding mor
1784d 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a 20  e than.      ** 
1784e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38  usableSize/4 - 8
1784f 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62 65   entries will be
17850 20 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f 72   reported as cor
17851 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72 0a  rupt.  In order.
17852 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69 6e        ** to main
17853 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20 63  tain backwards c
17854 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69 74  ompatibility wit
17855 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73  h older versions
17856 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 20 20   of SQLite,.    
17857 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f 6e    ** we will con
17858 74 69 6e 75 65 20 74 6f 20 72 65 73 74 72 69 63  tinue to restric
17859 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
1785a 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62 6c  entries to usabl
1785b 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 20 20  eSize/4 - 8.    
1785c 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 41    ** for now.  A
1785d 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20  t some point in 
1785e 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e 63 65  the future (once
1785f 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20 75 70   everyone has up
17860 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a 2a 20  graded.      ** 
17861 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 74 65  to 3.6.0 or late
17862 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e  r) we should con
17863 73 69 64 65 72 20 66 69 78 69 6e 67 20 74 68 65  sider fixing the
17864 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 62 6f   conditional abo
17865 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 72  ve.      ** to r
17866 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a 65 2f  ead "usableSize/
17867 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f 66 20  4-2" instead of 
17868 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38 22  "usableSize/4-8"
17869 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1786a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1786b 65 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e  erWrite(pTrunk->
1786c 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
1786d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1786e 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74  K ){.        put
1786f 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61  4byte(&pTrunk->a
17870 44 61 74 61 5b 34 5d 2c 20 6e 4c 65 61 66 2b 31  Data[4], nLeaf+1
17871 29 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62  );.        put4b
17872 79 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61  yte(&pTrunk->aDa
17873 74 61 5b 38 2b 6e 4c 65 61 66 2a 34 5d 2c 20 69  ta[8+nLeaf*4], i
17874 50 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 53  Page);.#ifndef S
17875 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c  QLITE_SECURE_DEL
17876 45 54 45 0a 20 20 20 20 20 20 20 20 69 66 28 20  ETE.        if( 
17877 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20  pPage ){.       
17878 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 44     sqlite3PagerD
17879 6f 6e 74 57 72 69 74 65 28 70 50 61 67 65 2d 3e  ontWrite(pPage->
1787a 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
1787b 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
1787c 20 20 20 72 63 20 3d 20 62 74 72 65 65 53 65 74     rc = btreeSet
1787d 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 2c 20  HasContent(pBt, 
1787e 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a  iPage);.      }.
1787f 20 20 20 20 20 20 54 52 41 43 45 28 28 22 46 52        TRACE(("FR
17880 45 45 2d 50 41 47 45 3a 20 25 64 20 6c 65 61 66  EE-PAGE: %d leaf
17881 20 6f 6e 20 74 72 75 6e 6b 20 70 61 67 65 20 25   on trunk page %
17882 64 5c 6e 22 2c 70 50 61 67 65 2d 3e 70 67 6e 6f  d\n",pPage->pgno
17883 2c 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29 29 3b  ,pTrunk->pgno));
17884 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72 65 65  .      goto free
17885 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  page_out;.    }.
17886 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e    }..  /* If con
17887 74 72 6f 6c 20 66 6c 6f 77 73 20 74 6f 20 74 68  trol flows to th
17888 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 69  is point, then i
17889 74 20 77 61 73 20 6e 6f 74 20 70 6f 73 73 69 62  t was not possib
1788a 6c 65 20 74 6f 20 61 64 64 20 74 68 65 0a 20 20  le to add the.  
1788b 2a 2a 20 74 68 65 20 70 61 67 65 20 62 65 69 6e  ** the page bein
1788c 67 20 66 72 65 65 64 20 61 73 20 61 20 6c 65 61  g freed as a lea
1788d 66 20 70 61 67 65 20 6f 66 20 74 68 65 20 66 69  f page of the fi
1788e 72 73 74 20 74 72 75 6e 6b 20 69 6e 20 74 68 65  rst trunk in the
1788f 20 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2a   free-list..  **
17890 20 50 6f 73 73 69 62 6c 79 20 62 65 63 61 75 73   Possibly becaus
17891 65 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  e the free-list 
17892 69 73 20 65 6d 70 74 79 2c 20 6f 72 20 70 6f 73  is empty, or pos
17893 73 69 62 6c 79 20 62 65 63 61 75 73 65 20 74 68  sibly because th
17894 65 20 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 72  e .  ** first tr
17895 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 2d  unk in the free-
17896 6c 69 73 74 20 69 73 20 66 75 6c 6c 2e 20 45 69  list is full. Ei
17897 74 68 65 72 20 77 61 79 2c 20 74 68 65 20 70 61  ther way, the pa
17898 67 65 20 62 65 69 6e 67 20 66 72 65 65 64 0a 20  ge being freed. 
17899 20 2a 2a 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20   ** will become 
1789a 74 68 65 20 6e 65 77 20 66 69 72 73 74 20 74 72  the new first tr
1789b 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20  unk page in the 
1789c 66 72 65 65 2d 6c 69 73 74 2e 0a 20 20 2a 2f 0a  free-list..  */.
1789d 20 20 69 66 28 20 70 50 61 67 65 3d 3d 30 20 26    if( pPage==0 &
1789e 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  & SQLITE_OK!=(rc
1789f 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
178a0 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61  pBt, iPage, &pPa
178a1 67 65 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 67  ge, 0)) ){.    g
178a2 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74  oto freepage_out
178a3 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
178a4 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
178a5 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a  Page->pDbPage);.
178a6 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
178a7 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  _OK ){.    goto 
178a8 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20  freepage_out;.  
178a9 7d 0a 20 20 70 75 74 34 62 79 74 65 28 70 50 61  }.  put4byte(pPa
178aa 67 65 2d 3e 61 44 61 74 61 2c 20 69 54 72 75 6e  ge->aData, iTrun
178ab 6b 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26  k);.  put4byte(&
178ac 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 34 5d 2c  pPage->aData[4],
178ad 20 30 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28   0);.  put4byte(
178ae 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
178af 32 5d 2c 20 69 50 61 67 65 29 3b 0a 20 20 54 52  2], iPage);.  TR
178b0 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a  ACE(("FREE-PAGE:
178b1 20 25 64 20 6e 65 77 20 74 72 75 6e 6b 20 70 61   %d new trunk pa
178b2 67 65 20 72 65 70 6c 61 63 69 6e 67 20 25 64 5c  ge replacing %d\
178b3 6e 22 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c  n", pPage->pgno,
178b4 20 69 54 72 75 6e 6b 29 29 3b 0a 0a 66 72 65 65   iTrunk));..free
178b5 70 61 67 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20  page_out:.  if( 
178b6 70 50 61 67 65 20 29 7b 0a 20 20 20 20 70 50 61  pPage ){.    pPa
178b7 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a  ge->isInit = 0;.
178b8 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 50 61 67    }.  releasePag
178b9 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 6c 65  e(pPage);.  rele
178ba 61 73 65 50 61 67 65 28 70 54 72 75 6e 6b 29 3b  asePage(pTrunk);
178bb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
178bc 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65  static void free
178bd 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Page(MemPage *pP
178be 61 67 65 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a  age, int *pRC){.
178bf 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 51    if( (*pRC)==SQ
178c0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a  LITE_OK ){.    *
178c1 70 52 43 20 3d 20 66 72 65 65 50 61 67 65 32 28  pRC = freePage2(
178c2 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 50 61 67  pPage->pBt, pPag
178c3 65 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 3b  e, pPage->pgno);
178c4 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  .  }.}../*.** Fr
178c5 65 65 20 61 6e 79 20 6f 76 65 72 66 6c 6f 77 20  ee any overflow 
178c6 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65 64  pages associated
178c7 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20   with the given 
178c8 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Cell..*/.static 
178c9 69 6e 74 20 63 6c 65 61 72 43 65 6c 6c 28 4d 65  int clearCell(Me
178ca 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 6e  mPage *pPage, un
178cb 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65  signed char *pCe
178cc 6c 6c 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ll){.  BtShared 
178cd 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42  *pBt = pPage->pB
178ce 74 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e  t;.  CellInfo in
178cf 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 50  fo;.  Pgno ovflP
178d0 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  gno;.  int rc;. 
178d1 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a 20 20 75 31   int nOvfl;.  u1
178d2 36 20 6f 76 66 6c 50 61 67 65 53 69 7a 65 3b 0a  6 ovflPageSize;.
178d3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
178d4 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
178d5 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
178d6 20 29 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65   );.  btreeParse
178d7 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
178d8 43 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20  Cell, &info);.  
178d9 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c  if( info.iOverfl
178da 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ow==0 ){.    ret
178db 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20  urn SQLITE_OK;  
178dc 2f 2a 20 4e 6f 20 6f 76 65 72 66 6c 6f 77 20 70  /* No overflow p
178dd 61 67 65 73 2e 20 52 65 74 75 72 6e 20 77 69 74  ages. Return wit
178de 68 6f 75 74 20 64 6f 69 6e 67 20 61 6e 79 74 68  hout doing anyth
178df 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20 20 6f 76 66  ing */.  }.  ovf
178e0 6c 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65  lPgno = get4byte
178e1 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76  (&pCell[info.iOv
178e2 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 61 73 73 65  erflow]);.  asse
178e3 72 74 28 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  rt( pBt->usableS
178e4 69 7a 65 20 3e 20 34 20 29 3b 0a 20 20 6f 76 66  ize > 4 );.  ovf
178e5 6c 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74 2d  lPageSize = pBt-
178e6 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b  >usableSize - 4;
178e7 0a 20 20 6e 4f 76 66 6c 20 3d 20 28 69 6e 66 6f  .  nOvfl = (info
178e8 2e 6e 50 61 79 6c 6f 61 64 20 2d 20 69 6e 66 6f  .nPayload - info
178e9 2e 6e 4c 6f 63 61 6c 20 2b 20 6f 76 66 6c 50 61  .nLocal + ovflPa
178ea 67 65 53 69 7a 65 20 2d 20 31 29 2f 6f 76 66 6c  geSize - 1)/ovfl
178eb 50 61 67 65 53 69 7a 65 3b 0a 20 20 61 73 73 65  PageSize;.  asse
178ec 72 74 28 20 6f 76 66 6c 50 67 6e 6f 3d 3d 30 20  rt( ovflPgno==0 
178ed 7c 7c 20 6e 4f 76 66 6c 3e 30 20 29 3b 0a 20 20  || nOvfl>0 );.  
178ee 77 68 69 6c 65 28 20 6e 4f 76 66 6c 2d 2d 20 29  while( nOvfl-- )
178ef 7b 0a 20 20 20 20 50 67 6e 6f 20 69 4e 65 78 74  {.    Pgno iNext
178f0 20 3d 20 30 3b 0a 20 20 20 20 4d 65 6d 50 61 67   = 0;.    MemPag
178f1 65 20 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20  e *pOvfl = 0;.  
178f2 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f 3c 32    if( ovflPgno<2
178f3 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e 70 61 67   || ovflPgno>pag
178f4 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29  erPagecount(pBt)
178f5 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 30 20 69   ){.      /* 0 i
178f6 73 20 6e 6f 74 20 61 20 6c 65 67 61 6c 20 70 61  s not a legal pa
178f7 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61  ge number and pa
178f8 67 65 20 31 20 63 61 6e 6e 6f 74 20 62 65 20 61  ge 1 cannot be a
178f9 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 76 65 72  n .      ** over
178fa 66 6c 6f 77 20 70 61 67 65 2e 20 54 68 65 72 65  flow page. There
178fb 66 6f 72 65 20 69 66 20 6f 76 66 6c 50 67 6e 6f  fore if ovflPgno
178fc 3c 32 20 6f 72 20 70 61 73 74 20 74 68 65 20 65  <2 or past the e
178fd 6e 64 20 6f 66 20 74 68 65 20 0a 20 20 20 20 20  nd of the .     
178fe 20 2a 2a 20 66 69 6c 65 20 74 68 65 20 64 61 74   ** file the dat
178ff 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 63 6f  abase must be co
17900 72 72 75 70 74 2e 20 2a 2f 0a 20 20 20 20 20 20  rrupt. */.      
17901 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
17902 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
17903 7d 0a 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20  }.    if( nOvfl 
17904 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 67 65  ){.      rc = ge
17905 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 70 42  tOverflowPage(pB
17906 74 2c 20 6f 76 66 6c 50 67 6e 6f 2c 20 26 70 4f  t, ovflPgno, &pO
17907 76 66 6c 2c 20 26 69 4e 65 78 74 29 3b 0a 20 20  vfl, &iNext);.  
17908 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
17909 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
1790a 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 32    rc = freePage2
1790b 28 70 42 74 2c 20 70 4f 76 66 6c 2c 20 6f 76 66  (pBt, pOvfl, ovf
1790c 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20  lPgno);.    if( 
1790d 70 4f 76 66 6c 20 29 7b 0a 20 20 20 20 20 20 73  pOvfl ){.      s
1790e 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
1790f 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29  (pOvfl->pDbPage)
17910 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
17911 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
17912 20 20 20 20 6f 76 66 6c 50 67 6e 6f 20 3d 20 69      ovflPgno = i
17913 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Next;.  }.  retu
17914 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
17915 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68  ./*.** Create th
17916 65 20 62 79 74 65 20 73 65 71 75 65 6e 63 65 20  e byte sequence 
17917 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e  used to represen
17918 74 20 61 20 63 65 6c 6c 20 6f 6e 20 70 61 67 65  t a cell on page
17919 20 70 50 61 67 65 0a 2a 2a 20 61 6e 64 20 77 72   pPage.** and wr
1791a 69 74 65 20 74 68 61 74 20 62 79 74 65 20 73 65  ite that byte se
1791b 71 75 65 6e 63 65 20 69 6e 74 6f 20 70 43 65 6c  quence into pCel
1791c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c 6f 77 20 70  l[].  Overflow p
1791d 61 67 65 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f  ages are.** allo
1791e 63 61 74 65 64 20 61 6e 64 20 66 69 6c 6c 65 64  cated and filled
1791f 20 69 6e 20 61 73 20 6e 65 63 65 73 73 61 72 79   in as necessary
17920 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 70  .  The calling p
17921 72 6f 63 65 64 75 72 65 0a 2a 2a 20 69 73 20 72  rocedure.** is r
17922 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d  esponsible for m
17923 61 6b 69 6e 67 20 73 75 72 65 20 73 75 66 66 69  aking sure suffi
17924 63 69 65 6e 74 20 73 70 61 63 65 20 68 61 73 20  cient space has 
17925 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 0a 2a  been allocated.*
17926 2a 20 66 6f 72 20 70 43 65 6c 6c 5b 5d 2e 0a 2a  * for pCell[]..*
17927 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 70  *.** Note that p
17928 43 65 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 6e 65  Cell does not ne
17929 63 65 73 73 61 72 79 20 6e 65 65 64 20 74 6f 20  cessary need to 
1792a 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 70 50 61  point to the pPa
1792b 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 61 72 65  ge->aData.** are
1792c 61 2e 20 20 70 43 65 6c 6c 20 6d 69 67 68 74 20  a.  pCell might 
1792d 70 6f 69 6e 74 20 74 6f 20 73 6f 6d 65 20 74 65  point to some te
1792e 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 2e  mporary storage.
1792f 20 20 54 68 65 20 63 65 6c 6c 20 77 69 6c 6c 0a    The cell will.
17930 2a 2a 20 62 65 20 63 6f 6e 73 74 72 75 63 74 65  ** be constructe
17931 64 20 69 6e 20 74 68 69 73 20 74 65 6d 70 6f 72  d in this tempor
17932 61 72 79 20 61 72 65 61 20 74 68 65 6e 20 63 6f  ary area then co
17933 70 69 65 64 20 69 6e 74 6f 20 70 50 61 67 65 2d  pied into pPage-
17934 3e 61 44 61 74 61 0a 2a 2a 20 6c 61 74 65 72 2e  >aData.** later.
17935 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
17936 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20 20 4d 65 6d  illInCell(.  Mem
17937 50 61 67 65 20 2a 70 50 61 67 65 2c 20 20 20 20  Page *pPage,    
17938 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
17939 68 65 20 70 61 67 65 20 74 68 61 74 20 63 6f 6e  he page that con
1793a 74 61 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 2a  tains the cell *
1793b 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
1793c 72 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20 20  r *pCell,       
1793d 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74     /* Complete t
1793e 65 78 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  ext of the cell 
1793f 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
17940 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c  *pKey, i64 nKey,
17941 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a      /* The key *
17942 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
17943 70 44 61 74 61 2c 69 6e 74 20 6e 44 61 74 61 2c  pData,int nData,
17944 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 2a     /* The data *
17945 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20 20  /.  int nZero,  
17946 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17947 20 20 20 2f 2a 20 45 78 74 72 61 20 7a 65 72 6f     /* Extra zero
17948 20 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64   bytes to append
17949 20 74 6f 20 70 44 61 74 61 20 2a 2f 0a 20 20 69   to pData */.  i
1794a 6e 74 20 2a 70 6e 53 69 7a 65 20 20 20 20 20 20  nt *pnSize      
1794b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1794c 20 57 72 69 74 65 20 63 65 6c 6c 20 73 69 7a 65   Write cell size
1794d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e   here */.){.  in
1794e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 63 6f  t nPayload;.  co
1794f 6e 73 74 20 75 38 20 2a 70 53 72 63 3b 0a 20 20  nst u8 *pSrc;.  
17950 69 6e 74 20 6e 53 72 63 2c 20 6e 2c 20 72 63 3b  int nSrc, n, rc;
17951 0a 20 20 69 6e 74 20 73 70 61 63 65 4c 65 66 74  .  int spaceLeft
17952 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76  ;.  MemPage *pOv
17953 66 6c 20 3d 20 30 3b 0a 20 20 4d 65 6d 50 61 67  fl = 0;.  MemPag
17954 65 20 2a 70 54 6f 52 65 6c 65 61 73 65 20 3d 20  e *pToRelease = 
17955 30 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  0;.  unsigned ch
17956 61 72 20 2a 70 50 72 69 6f 72 3b 0a 20 20 75 6e  ar *pPrior;.  un
17957 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50 61  signed char *pPa
17958 79 6c 6f 61 64 3b 0a 20 20 42 74 53 68 61 72 65  yload;.  BtShare
17959 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  d *pBt = pPage->
1795a 70 42 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  pBt;.  Pgno pgno
1795b 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20  Ovfl = 0;.  int 
1795c 6e 48 65 61 64 65 72 3b 0a 20 20 43 65 6c 6c 49  nHeader;.  CellI
1795d 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 61 73 73  nfo info;..  ass
1795e 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1795f 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
17960 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  Bt->mutex) );.. 
17961 20 2f 2a 20 70 50 61 67 65 20 69 73 20 6e 6f 74   /* pPage is not
17962 20 6e 65 63 65 73 73 61 72 69 6c 79 20 77 72 69   necessarily wri
17963 74 65 61 62 6c 65 20 73 69 6e 63 65 20 70 43 65  teable since pCe
17964 6c 6c 20 6d 69 67 68 74 20 62 65 20 61 75 78 69  ll might be auxi
17965 6c 69 61 72 79 0a 20 20 2a 2a 20 62 75 66 66 65  liary.  ** buffe
17966 72 20 73 70 61 63 65 20 74 68 61 74 20 69 73 20  r space that is 
17967 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68  separate from th
17968 65 20 70 50 61 67 65 20 62 75 66 66 65 72 20 61  e pPage buffer a
17969 72 65 61 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  rea */.  assert(
1796a 20 70 43 65 6c 6c 3c 70 50 61 67 65 2d 3e 61 44   pCell<pPage->aD
1796b 61 74 61 20 7c 7c 20 70 43 65 6c 6c 3e 3d 26 70  ata || pCell>=&p
1796c 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d  Page->aData[pBt-
1796d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20 20  >pageSize].     
1796e 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65         || sqlite
1796f 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
17970 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
17971 29 20 29 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20  ) );..  /* Fill 
17972 69 6e 20 74 68 65 20 68 65 61 64 65 72 2e 20 2a  in the header. *
17973 2f 0a 20 20 6e 48 65 61 64 65 72 20 3d 20 30 3b  /.  nHeader = 0;
17974 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c  .  if( !pPage->l
17975 65 61 66 20 29 7b 0a 20 20 20 20 6e 48 65 61 64  eaf ){.    nHead
17976 65 72 20 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69  er += 4;.  }.  i
17977 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74  f( pPage->hasDat
17978 61 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72  a ){.    nHeader
17979 20 2b 3d 20 70 75 74 56 61 72 69 6e 74 28 26 70   += putVarint(&p
1797a 43 65 6c 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e  Cell[nHeader], n
1797b 44 61 74 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d  Data+nZero);.  }
1797c 65 6c 73 65 7b 0a 20 20 20 20 6e 44 61 74 61 20  else{.    nData 
1797d 3d 20 6e 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d  = nZero = 0;.  }
1797e 0a 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75  .  nHeader += pu
1797f 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e  tVarint(&pCell[n
17980 48 65 61 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29  Header], *(u64*)
17981 26 6e 4b 65 79 29 3b 0a 20 20 62 74 72 65 65 50  &nKey);.  btreeP
17982 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50 61 67  arseCellPtr(pPag
17983 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66 6f 29  e, pCell, &info)
17984 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f  ;.  assert( info
17985 2e 6e 48 65 61 64 65 72 3d 3d 6e 48 65 61 64 65  .nHeader==nHeade
17986 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  r );.  assert( i
17987 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b 65 79 20 29  nfo.nKey==nKey )
17988 3b 0a 20 20 61 73 73 65 72 74 28 20 69 6e 66 6f  ;.  assert( info
17989 2e 6e 44 61 74 61 3d 3d 28 75 33 32 29 28 6e 44  .nData==(u32)(nD
1798a 61 74 61 2b 6e 5a 65 72 6f 29 20 29 3b 0a 20 20  ata+nZero) );.  
1798b 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68  .  /* Fill in th
1798c 65 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 6e  e payload */.  n
1798d 50 61 79 6c 6f 61 64 20 3d 20 6e 44 61 74 61 20  Payload = nData 
1798e 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20 70  + nZero;.  if( p
1798f 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a  Page->intKey ){.
17990 20 20 20 20 70 53 72 63 20 3d 20 70 44 61 74 61      pSrc = pData
17991 3b 0a 20 20 20 20 6e 53 72 63 20 3d 20 6e 44 61  ;.    nSrc = nDa
17992 74 61 3b 0a 20 20 20 20 6e 44 61 74 61 20 3d 20  ta;.    nData = 
17993 30 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a 20 20 20  0;.  }else{ .   
17994 20 69 66 28 20 4e 45 56 45 52 28 6e 4b 65 79 3e   if( NEVER(nKey>
17995 30 78 37 66 66 66 66 66 66 66 20 7c 7c 20 70 4b  0x7fffffff || pK
17996 65 79 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20  ey==0) ){.      
17997 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
17998 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
17999 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2b  }.    nPayload +
1799a 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20 20  = (int)nKey;.   
1799b 20 70 53 72 63 20 3d 20 70 4b 65 79 3b 0a 20 20   pSrc = pKey;.  
1799c 20 20 6e 53 72 63 20 3d 20 28 69 6e 74 29 6e 4b    nSrc = (int)nK
1799d 65 79 3b 0a 20 20 7d 0a 20 20 2a 70 6e 53 69 7a  ey;.  }.  *pnSiz
1799e 65 20 3d 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a  e = info.nSize;.
1799f 20 20 73 70 61 63 65 4c 65 66 74 20 3d 20 69 6e    spaceLeft = in
179a0 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 70 50 61  fo.nLocal;.  pPa
179a1 79 6c 6f 61 64 20 3d 20 26 70 43 65 6c 6c 5b 6e  yload = &pCell[n
179a2 48 65 61 64 65 72 5d 3b 0a 20 20 70 50 72 69 6f  Header];.  pPrio
179a3 72 20 3d 20 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e  r = &pCell[info.
179a4 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a 0a 20 20 77  iOverflow];..  w
179a5 68 69 6c 65 28 20 6e 50 61 79 6c 6f 61 64 3e 30  hile( nPayload>0
179a6 20 29 7b 0a 20 20 20 20 69 66 28 20 73 70 61 63   ){.    if( spac
179a7 65 4c 65 66 74 3d 3d 30 20 29 7b 0a 23 69 66 6e  eLeft==0 ){.#ifn
179a8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
179a9 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
179aa 20 50 67 6e 6f 20 70 67 6e 6f 50 74 72 6d 61 70   Pgno pgnoPtrmap
179ab 20 3d 20 70 67 6e 6f 4f 76 66 6c 3b 20 2f 2a 20   = pgnoOvfl; /* 
179ac 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 70 6f  Overflow page po
179ad 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20  inter-map entry 
179ae 70 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 69 66  page */.      if
179af 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
179b0 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 7b  m ){.        do{
179b1 0a 20 20 20 20 20 20 20 20 20 20 70 67 6e 6f 4f  .          pgnoO
179b2 76 66 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  vfl++;.        }
179b3 20 77 68 69 6c 65 28 20 0a 20 20 20 20 20 20 20   while( .       
179b4 20 20 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45     PTRMAP_ISPAGE
179b5 28 70 42 74 2c 20 70 67 6e 6f 4f 76 66 6c 29 20  (pBt, pgnoOvfl) 
179b6 7c 7c 20 70 67 6e 6f 4f 76 66 6c 3d 3d 50 45 4e  || pgnoOvfl==PEN
179b7 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
179b8 42 74 29 20 0a 20 20 20 20 20 20 20 20 29 3b 0a  Bt) .        );.
179b9 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
179ba 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61       rc = alloca
179bb 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
179bc 20 26 70 4f 76 66 6c 2c 20 26 70 67 6e 6f 4f 76   &pOvfl, &pgnoOv
179bd 66 6c 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 30 29  fl, pgnoOvfl, 0)
179be 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
179bf 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
179c0 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
179c1 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72   database suppor
179c2 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c 20  ts auto-vacuum, 
179c3 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6f  and the second o
179c4 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20 20 20  r subsequent.   
179c5 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70     ** overflow p
179c6 61 67 65 20 69 73 20 62 65 69 6e 67 20 61 6c 6c  age is being all
179c7 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e 20 65  ocated, add an e
179c8 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f 69 6e  ntry to the poin
179c9 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20 2a 2a  ter-map.      **
179ca 20 66 6f 72 20 74 68 61 74 20 70 61 67 65 20 6e   for that page n
179cb 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ow. .      **.  
179cc 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69      ** If this i
179cd 73 20 74 68 65 20 66 69 72 73 74 20 6f 76 65 72  s the first over
179ce 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e 20  flow page, then 
179cf 77 72 69 74 65 20 61 20 70 61 72 74 69 61 6c 20  write a partial 
179d0 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a 2a 20  entry .      ** 
179d1 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  to the pointer-m
179d2 61 70 2e 20 49 66 20 77 65 20 77 72 69 74 65 20  ap. If we write 
179d3 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69 73 20  nothing to this 
179d4 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c 6f 74  pointer-map slot
179d5 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20  ,.      ** then 
179d6 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63 20 6f  the optimistic o
179d7 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20 70 72  verflow chain pr
179d8 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c 65 61  ocessing in clea
179d9 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20 2a 2a  rCell().      **
179da 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70 72 65   may misinterpre
179db 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61 6c 69  t the uninitiali
179dc 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64 20 64  sed values and d
179dd 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20 20  elete the.      
179de 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73 20 66  ** wrong pages f
179df 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
179e0 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
179e1 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
179e2 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51 4c 49  cuum && rc==SQLI
179e3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
179e4 20 75 38 20 65 54 79 70 65 20 3d 20 28 70 67 6e   u8 eType = (pgn
179e5 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50 5f 4f  oPtrmap?PTRMAP_O
179e6 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41 50 5f  VERFLOW2:PTRMAP_
179e7 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20 20 20  OVERFLOW1);.    
179e8 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42      ptrmapPut(pB
179e9 74 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 65 54 79  t, pgnoOvfl, eTy
179ea 70 65 2c 20 70 67 6e 6f 50 74 72 6d 61 70 2c 20  pe, pgnoPtrmap, 
179eb 26 72 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66  &rc);.        if
179ec 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
179ed 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4f    releasePage(pO
179ee 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  vfl);.        }.
179ef 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
179f0 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
179f1 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61         releasePa
179f2 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a  ge(pToRelease);.
179f3 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
179f4 63 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  c;.      }..    
179f5 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61    /* If pToRelea
179f6 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74  se is not zero t
179f7 68 61 6e 20 70 50 72 69 6f 72 20 70 6f 69 6e 74  han pPrior point
179f8 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20  s into the data 
179f9 61 72 65 61 0a 20 20 20 20 20 20 2a 2a 20 6f 66  area.      ** of
179fa 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61   pToRelease.  Ma
179fb 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61  ke sure pToRelea
179fc 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74  se is still writ
179fd 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20  eable. */.      
179fe 61 73 73 65 72 74 28 20 70 54 6f 52 65 6c 65 61  assert( pToRelea
179ff 73 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  se==0 || sqlite3
17a00 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
17a01 28 70 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62  (pToRelease->pDb
17a02 50 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20  Page) );..      
17a03 2f 2a 20 49 66 20 70 50 72 69 6f 72 20 69 73 20  /* If pPrior is 
17a04 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  part of the data
17a05 20 61 72 65 61 20 6f 66 20 70 50 61 67 65 2c 20   area of pPage, 
17a06 74 68 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 70  then make sure p
17a07 50 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73  Page.      ** is
17a08 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65   still writeable
17a09 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
17a0a 28 20 70 50 72 69 6f 72 3c 70 50 61 67 65 2d 3e  ( pPrior<pPage->
17a0b 61 44 61 74 61 20 7c 7c 20 70 50 72 69 6f 72 3e  aData || pPrior>
17a0c 3d 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70  =&pPage->aData[p
17a0d 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20  Bt->pageSize].  
17a0e 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c            || sql
17a0f 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
17a10 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50  able(pPage->pDbP
17a11 61 67 65 29 20 29 3b 0a 0a 20 20 20 20 20 20 70  age) );..      p
17a12 75 74 34 62 79 74 65 28 70 50 72 69 6f 72 2c 20  ut4byte(pPrior, 
17a13 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20 20 20 20 20  pgnoOvfl);.     
17a14 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f   releasePage(pTo
17a15 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20  Release);.      
17a16 70 54 6f 52 65 6c 65 61 73 65 20 3d 20 70 4f 76  pToRelease = pOv
17a17 66 6c 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72  fl;.      pPrior
17a18 20 3d 20 70 4f 76 66 6c 2d 3e 61 44 61 74 61 3b   = pOvfl->aData;
17a19 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28  .      put4byte(
17a1a 70 50 72 69 6f 72 2c 20 30 29 3b 0a 20 20 20 20  pPrior, 0);.    
17a1b 20 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 4f    pPayload = &pO
17a1c 76 66 6c 2d 3e 61 44 61 74 61 5b 34 5d 3b 0a 20  vfl->aData[4];. 
17a1d 20 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 3d       spaceLeft =
17a1e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
17a1f 20 2d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20   - 4;.    }.    
17a20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20 20  n = nPayload;.  
17a21 20 20 69 66 28 20 6e 3e 73 70 61 63 65 4c 65 66    if( n>spaceLef
17a22 74 20 29 20 6e 20 3d 20 73 70 61 63 65 4c 65 66  t ) n = spaceLef
17a23 74 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 54  t;..    /* If pT
17a24 6f 52 65 6c 65 61 73 65 20 69 73 20 6e 6f 74 20  oRelease is not 
17a25 7a 65 72 6f 20 74 68 61 6e 20 70 50 61 79 6c 6f  zero than pPaylo
17a26 61 64 20 70 6f 69 6e 74 73 20 69 6e 74 6f 20 74  ad points into t
17a27 68 65 20 64 61 74 61 20 61 72 65 61 0a 20 20 20  he data area.   
17a28 20 2a 2a 20 6f 66 20 70 54 6f 52 65 6c 65 61 73   ** of pToReleas
17a29 65 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 70 54  e.  Make sure pT
17a2a 6f 52 65 6c 65 61 73 65 20 69 73 20 73 74 69 6c  oRelease is stil
17a2b 6c 20 77 72 69 74 65 61 62 6c 65 2e 20 2a 2f 0a  l writeable. */.
17a2c 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 52      assert( pToR
17a2d 65 6c 65 61 73 65 3d 3d 30 20 7c 7c 20 73 71 6c  elease==0 || sql
17a2e 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
17a2f 61 62 6c 65 28 70 54 6f 52 65 6c 65 61 73 65 2d  able(pToRelease-
17a30 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20  >pDbPage) );..  
17a31 20 20 2f 2a 20 49 66 20 70 50 61 79 6c 6f 61 64    /* If pPayload
17a32 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   is part of the 
17a33 64 61 74 61 20 61 72 65 61 20 6f 66 20 70 50 61  data area of pPa
17a34 67 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73 75  ge, then make su
17a35 72 65 20 70 50 61 67 65 0a 20 20 20 20 2a 2a 20  re pPage.    ** 
17a36 69 73 20 73 74 69 6c 6c 20 77 72 69 74 65 61 62  is still writeab
17a37 6c 65 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  le */.    assert
17a38 28 20 70 50 61 79 6c 6f 61 64 3c 70 50 61 67 65  ( pPayload<pPage
17a39 2d 3e 61 44 61 74 61 20 7c 7c 20 70 50 61 79 6c  ->aData || pPayl
17a3a 6f 61 64 3e 3d 26 70 50 61 67 65 2d 3e 61 44 61  oad>=&pPage->aDa
17a3b 74 61 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  ta[pBt->pageSize
17a3c 5d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  ].            ||
17a3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
17a3e 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
17a3f 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20  pDbPage) );..   
17a40 20 69 66 28 20 6e 53 72 63 3e 30 20 29 7b 0a 20   if( nSrc>0 ){. 
17a41 20 20 20 20 20 69 66 28 20 6e 3e 6e 53 72 63 20       if( n>nSrc 
17a42 29 20 6e 20 3d 20 6e 53 72 63 3b 0a 20 20 20 20  ) n = nSrc;.    
17a43 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20 29    assert( pSrc )
17a44 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  ;.      memcpy(p
17a45 50 61 79 6c 6f 61 64 2c 20 70 53 72 63 2c 20 6e  Payload, pSrc, n
17a46 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
17a47 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61 79 6c      memset(pPayl
17a48 6f 61 64 2c 20 30 2c 20 6e 29 3b 0a 20 20 20 20  oad, 0, n);.    
17a49 7d 0a 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2d  }.    nPayload -
17a4a 3d 20 6e 3b 0a 20 20 20 20 70 50 61 79 6c 6f 61  = n;.    pPayloa
17a4b 64 20 2b 3d 20 6e 3b 0a 20 20 20 20 70 53 72 63  d += n;.    pSrc
17a4c 20 2b 3d 20 6e 3b 0a 20 20 20 20 6e 53 72 63 20   += n;.    nSrc 
17a4d 2d 3d 20 6e 3b 0a 20 20 20 20 73 70 61 63 65 4c  -= n;.    spaceL
17a4e 65 66 74 20 2d 3d 20 6e 3b 0a 20 20 20 20 69 66  eft -= n;.    if
17a4f 28 20 6e 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20  ( nSrc==0 ){.   
17a50 20 20 20 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b     nSrc = nData;
17a51 0a 20 20 20 20 20 20 70 53 72 63 20 3d 20 70 44  .      pSrc = pD
17a52 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ata;.    }.  }. 
17a53 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f   releasePage(pTo
17a54 52 65 6c 65 61 73 65 29 3b 0a 20 20 72 65 74 75  Release);.  retu
17a55 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
17a56 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68  ./*.** Remove th
17a57 65 20 69 2d 74 68 20 63 65 6c 6c 20 66 72 6f 6d  e i-th cell from
17a58 20 70 50 61 67 65 2e 20 20 54 68 69 73 20 72 6f   pPage.  This ro
17a59 75 74 69 6e 65 20 65 66 66 65 63 74 73 20 70 50  utine effects pP
17a5a 61 67 65 20 6f 6e 6c 79 2e 0a 2a 2a 20 54 68 65  age only..** The
17a5b 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 73   cell content is
17a5c 20 6e 6f 74 20 66 72 65 65 64 20 6f 72 20 64 65   not freed or de
17a5d 61 6c 6c 6f 63 61 74 65 64 2e 20 20 49 74 20 69  allocated.  It i
17a5e 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a  s assumed that.*
17a5f 2a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  * the cell conte
17a60 6e 74 20 68 61 73 20 62 65 65 6e 20 63 6f 70 69  nt has been copi
17a61 65 64 20 73 6f 6d 65 70 6c 61 63 65 20 65 6c 73  ed someplace els
17a62 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
17a63 20 6a 75 73 74 0a 2a 2a 20 72 65 6d 6f 76 65 73   just.** removes
17a64 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74   the reference t
17a65 6f 20 74 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20  o the cell from 
17a66 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 22 73 7a  pPage..**.** "sz
17a67 22 20 6d 75 73 74 20 62 65 20 74 68 65 20 6e 75  " must be the nu
17a68 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
17a69 20 74 68 65 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74   the cell..*/.st
17a6a 61 74 69 63 20 76 6f 69 64 20 64 72 6f 70 43 65  atic void dropCe
17a6b 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ll(MemPage *pPag
17a6c 65 2c 20 69 6e 74 20 69 64 78 2c 20 69 6e 74 20  e, int idx, int 
17a6d 73 7a 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20  sz, int *pRC){. 
17a6e 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
17a6f 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
17a70 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20   */.  int pc;   
17a71 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
17a72 74 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  to cell content 
17a73 6f 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65  of cell being de
17a74 6c 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 2a 64  leted */.  u8 *d
17a75 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 70 50  ata;       /* pP
17a76 61 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20  age->aData */.  
17a77 75 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20  u8 *ptr;        
17a78 2f 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 65 20  /* Used to move 
17a79 62 79 74 65 73 20 61 72 6f 75 6e 64 20 77 69 74  bytes around wit
17a7a 68 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20  hin data[] */.  
17a7b 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
17a7c 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f  /* The return co
17a7d 64 65 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72 3b  de */.  int hdr;
17a7e 20 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e          /* Begin
17a7f 6e 69 6e 67 20 6f 66 20 74 68 65 20 68 65 61 64  ning of the head
17a80 65 72 2e 20 20 30 20 6d 6f 73 74 20 70 61 67 65  er.  0 most page
17a81 73 2e 20 20 31 30 30 20 70 61 67 65 20 31 20 2a  s.  100 page 1 *
17a82 2f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20  /..  if( *pRC ) 
17a83 72 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72  return;..  asser
17a84 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64 78  t( idx>=0 && idx
17a85 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b  <pPage->nCell );
17a86 0a 20 20 61 73 73 65 72 74 28 20 73 7a 3d 3d 63  .  assert( sz==c
17a87 65 6c 6c 53 69 7a 65 28 70 50 61 67 65 2c 20 69  ellSize(pPage, i
17a88 64 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  dx) );.  assert(
17a89 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
17a8a 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
17a8b 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73  pDbPage) );.  as
17a8c 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
17a8d 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
17a8e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
17a8f 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61   data = pPage->a
17a90 44 61 74 61 3b 0a 20 20 70 74 72 20 3d 20 26 64  Data;.  ptr = &d
17a91 61 74 61 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f  ata[pPage->cellO
17a92 66 66 73 65 74 20 2b 20 32 2a 69 64 78 5d 3b 0a  ffset + 2*idx];.
17a93 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28    pc = get2byte(
17a94 70 74 72 29 3b 0a 20 20 68 64 72 20 3d 20 70 50  ptr);.  hdr = pP
17a95 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a  age->hdrOffset;.
17a96 20 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d    testcase( pc==
17a97 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
17a98 64 72 2b 35 5d 29 20 29 3b 0a 20 20 74 65 73 74  dr+5]) );.  test
17a99 63 61 73 65 28 20 70 63 2b 73 7a 3d 3d 70 50 61  case( pc+sz==pPa
17a9a 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
17a9b 69 7a 65 20 29 3b 0a 20 20 69 66 28 20 70 63 20  ize );.  if( pc 
17a9c 3c 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  < get2byte(&data
17a9d 5b 68 64 72 2b 35 5d 29 20 7c 7c 20 70 63 2b 73  [hdr+5]) || pc+s
17a9e 7a 20 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e  z > pPage->pBt->
17a9f 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20  usableSize ){.  
17aa0 20 20 2a 70 52 43 20 3d 20 53 51 4c 49 54 45 5f    *pRC = SQLITE_
17aa1 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
17aa2 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
17aa3 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 70  rc = freeSpace(p
17aa4 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a 20  Page, pc, sz);. 
17aa5 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 2a   if( rc ){.    *
17aa6 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 72 65  pRC = rc;.    re
17aa7 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
17aa8 69 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65  i=idx+1; i<pPage
17aa9 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74  ->nCell; i++, pt
17aaa 72 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30  r+=2){.    ptr[0
17aab 5d 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20  ] = ptr[2];.    
17aac 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b  ptr[1] = ptr[3];
17aad 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43  .  }.  pPage->nC
17aae 65 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74  ell--;.  put2byt
17aaf 65 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20  e(&data[hdr+3], 
17ab0 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20  pPage->nCell);. 
17ab1 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2b 3d   pPage->nFree +=
17ab2 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73   2;.}../*.** Ins
17ab3 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20 6f  ert a new cell o
17ab4 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c 20  n pPage at cell 
17ab5 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65 6c  index "i".  pCel
17ab6 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a  l points to the.
17ab7 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  ** content of th
17ab8 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66  e cell..**.** If
17ab9 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
17aba 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68  t will fit on th
17abb 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75 74  e page, then put
17abc 20 69 74 20 74 68 65 72 65 2e 20 20 49 66 20 69   it there.  If i
17abd 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 69  t.** will not fi
17abe 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20 63  t, then make a c
17abf 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  opy of the cell 
17ac0 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54 65  content into pTe
17ac1 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20 69  mp if.** pTemp i
17ac2 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 67  s not null.  Reg
17ac3 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d 70  ardless of pTemp
17ac4 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  , allocate a new
17ac5 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50 61   entry.** in pPa
17ac6 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 20  ge->aOvfl[] and 
17ac7 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 6f  make it point to
17ac8 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
17ac9 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e 20  t (either.** in 
17aca 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 69  pTemp or the ori
17acb 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e 64  ginal pCell) and
17acc 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 73   also record its
17acd 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f   index. .** Allo
17ace 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e 74  cating a new ent
17acf 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43 65  ry in pPage->aCe
17ad0 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68 61  ll[] implies tha
17ad1 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f 76  t .** pPage->nOv
17ad2 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65 6d  erflow is increm
17ad3 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ented..**.** If 
17ad4 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72  nSkip is non-zer
17ad5 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63  o, then do not c
17ad6 6f 70 79 20 74 68 65 20 66 69 72 73 74 20 6e 53  opy the first nS
17ad7 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74 68 65  kip bytes of the
17ad8 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 61  .** cell. The ca
17ad9 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77 72  ller will overwr
17ada 69 74 65 20 74 68 65 6d 20 61 66 74 65 72 20 74  ite them after t
17adb 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
17adc 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69  urns. If.** nSki
17add 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  p is non-zero, t
17ade 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e 6f  hen pCell may no
17adf 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69 6e  t point to an in
17ae0 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f 63  valid memory loc
17ae1 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20 70  ation .** (but p
17ae2 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61 6c  Cell+nSkip is al
17ae3 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a  ways valid)..*/.
17ae4 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 73 65  static void inse
17ae5 72 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67  rtCell(.  MemPag
17ae6 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50  e *pPage,   /* P
17ae7 61 67 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  age into which w
17ae8 65 20 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f  e are copying */
17ae9 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20  .  int i,       
17aea 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c       /* New cell
17aeb 20 62 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74   becomes the i-t
17aec 68 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61  h cell of the pa
17aed 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c  ge */.  u8 *pCel
17aee 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  l,        /* Con
17aef 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20  tent of the new 
17af0 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  cell */.  int sz
17af1 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  ,           /* B
17af2 79 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20  ytes of content 
17af3 69 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38  in pCell */.  u8
17af4 20 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20   *pTemp,        
17af5 2f 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20  /* Temp storage 
17af6 73 70 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c  space for pCell,
17af7 20 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20   if needed */.  
17af8 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20  Pgno iChild,    
17af9 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f    /* If non-zero
17afa 2c 20 72 65 70 6c 61 63 65 20 66 69 72 73 74 20  , replace first 
17afb 34 20 62 79 74 65 73 20 77 69 74 68 20 74 68 69  4 bytes with thi
17afc 73 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74  s value */.  int
17afd 20 2a 70 52 43 20 20 20 20 20 20 20 20 20 20 2f   *pRC          /
17afe 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74 65  * Read and write
17aff 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   return code fro
17b00 6d 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  m here */.){.  i
17b01 6e 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20  nt idx;         
17b02 20 2f 2a 20 57 68 65 72 65 20 74 6f 20 77 72 69   /* Where to wri
17b03 74 65 20 6e 65 77 20 63 65 6c 6c 20 63 6f 6e 74  te new cell cont
17b04 65 6e 74 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f  ent in data[] */
17b05 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20  .  int j;       
17b06 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
17b07 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 65 6e  nter */.  int en
17b08 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  d;          /* F
17b09 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20 74  irst byte past t
17b0a 68 65 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69  he last cell poi
17b0b 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a  nter in data[] *
17b0c 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20 20 20 20  /.  int ins;    
17b0d 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69        /* Index i
17b0e 6e 20 64 61 74 61 5b 5d 20 77 68 65 72 65 20 6e  n data[] where n
17b0f 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  ew cell pointer 
17b10 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20  is inserted */. 
17b11 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b   int cellOffset;
17b12 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
17b13 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e   first cell poin
17b14 74 65 72 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f  ter in data[] */
17b15 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20  .  u8 *data;    
17b16 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 74       /* The cont
17b17 65 6e 74 20 6f 66 20 74 68 65 20 77 68 6f 6c 65  ent of the whole
17b18 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 70   page */.  u8 *p
17b19 74 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  tr;          /* 
17b1a 55 73 65 64 20 66 6f 72 20 6d 6f 76 69 6e 67 20  Used for moving 
17b1b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 6f 75  information arou
17b1c 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a  nd in data[] */.
17b1d 0a 20 20 69 6e 74 20 6e 53 6b 69 70 20 3d 20 28  .  int nSkip = (
17b1e 69 43 68 69 6c 64 20 3f 20 34 20 3a 20 30 29 3b  iChild ? 4 : 0);
17b1f 0a 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72  ..  if( *pRC ) r
17b20 65 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74  eturn;..  assert
17b21 28 20 69 3e 3d 30 20 26 26 20 69 3c 3d 70 50 61  ( i>=0 && i<=pPa
17b22 67 65 2d 3e 6e 43 65 6c 6c 2b 70 50 61 67 65 2d  ge->nCell+pPage-
17b23 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 3b 0a 20 20  >nOverflow );.  
17b24 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
17b25 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 50  Cell<=MX_CELL(pP
17b26 61 67 65 2d 3e 70 42 74 29 20 26 26 20 4d 58 5f  age->pBt) && MX_
17b27 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29  CELL(pPage->pBt)
17b28 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61 73 73 65  <=5460 );.  asse
17b29 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  rt( pPage->nOver
17b2a 66 6c 6f 77 3c 3d 41 72 72 61 79 53 69 7a 65 28  flow<=ArraySize(
17b2b 70 50 61 67 65 2d 3e 61 4f 76 66 6c 29 20 29 3b  pPage->aOvfl) );
17b2c 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
17b2d 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
17b2e 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
17b2f 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 63 65 6c   );.  /* The cel
17b30 6c 20 73 68 6f 75 6c 64 20 6e 6f 72 6d 61 6c 6c  l should normall
17b31 79 20 62 65 20 73 69 7a 65 64 20 63 6f 72 72 65  y be sized corre
17b32 63 74 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c 20  ctly.  However, 
17b33 77 68 65 6e 20 6d 6f 76 69 6e 67 20 61 0a 20 20  when moving a.  
17b34 2a 2a 20 6d 61 6c 66 6f 72 6d 65 64 20 63 65 6c  ** malformed cel
17b35 6c 20 66 72 6f 6d 20 61 20 6c 65 61 66 20 70 61  l from a leaf pa
17b36 67 65 20 74 6f 20 61 6e 20 69 6e 74 65 72 69 6f  ge to an interio
17b37 72 20 70 61 67 65 2c 20 69 66 20 74 68 65 20 63  r page, if the c
17b38 65 6c 6c 20 73 69 7a 65 0a 20 20 2a 2a 20 77 61  ell size.  ** wa
17b39 6e 74 65 64 20 74 6f 20 62 65 20 6c 65 73 73 20  nted to be less 
17b3a 74 68 61 6e 20 34 20 62 75 74 20 67 6f 74 20 72  than 4 but got r
17b3b 6f 75 6e 64 65 64 20 75 70 20 74 6f 20 34 20 6f  ounded up to 4 o
17b3c 6e 20 74 68 65 20 6c 65 61 66 2c 20 74 68 65 6e  n the leaf, then
17b3d 20 73 69 7a 65 0a 20 20 2a 2a 20 6d 69 67 68 74   size.  ** might
17b3e 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 38 20   be less than 8 
17b3f 28 6c 65 61 66 2d 73 69 7a 65 20 2b 20 70 6f 69  (leaf-size + poi
17b40 6e 74 65 72 29 20 6f 6e 20 74 68 65 20 69 6e 74  nter) on the int
17b41 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 48 65 6e  erior node.  Hen
17b42 63 65 0a 20 20 2a 2a 20 74 68 65 20 74 65 72 6d  ce.  ** the term
17b43 20 61 66 74 65 72 20 74 68 65 20 7c 7c 20 69 6e   after the || in
17b44 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
17b45 73 73 65 72 74 28 29 2e 20 2a 2f 0a 20 20 61 73  ssert(). */.  as
17b46 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69  sert( sz==cellSi
17b47 7a 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  zePtr(pPage, pCe
17b48 6c 6c 29 20 7c 7c 20 28 73 7a 3d 3d 38 20 26 26  ll) || (sz==8 &&
17b49 20 69 43 68 69 6c 64 3e 30 29 20 29 3b 0a 20 20   iChild>0) );.  
17b4a 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  if( pPage->nOver
17b4b 66 6c 6f 77 20 7c 7c 20 73 7a 2b 32 3e 70 50 61  flow || sz+2>pPa
17b4c 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20 20  ge->nFree ){.   
17b4d 20 69 66 28 20 70 54 65 6d 70 20 29 7b 0a 20 20   if( pTemp ){.  
17b4e 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d 70      memcpy(pTemp
17b4f 2b 6e 53 6b 69 70 2c 20 70 43 65 6c 6c 2b 6e 53  +nSkip, pCell+nS
17b50 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a  kip, sz-nSkip);.
17b51 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 54        pCell = pT
17b52 65 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  emp;.    }.    i
17b53 66 28 20 69 43 68 69 6c 64 20 29 7b 0a 20 20 20  f( iChild ){.   
17b54 20 20 20 70 75 74 34 62 79 74 65 28 70 43 65 6c     put4byte(pCel
17b55 6c 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20  l, iChild);.    
17b56 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61 67 65 2d  }.    j = pPage-
17b57 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b 0a 20 20  >nOverflow++;.  
17b58 20 20 61 73 73 65 72 74 28 20 6a 3c 28 69 6e 74    assert( j<(int
17b59 29 28 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e  )(sizeof(pPage->
17b5a 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66 28 70 50  aOvfl)/sizeof(pP
17b5b 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 29 29 20  age->aOvfl[0])) 
17b5c 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f  );.    pPage->aO
17b5d 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20 3d 20 70  vfl[j].pCell = p
17b5e 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61 67 65 2d  Cell;.    pPage-
17b5f 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78 20 3d 20  >aOvfl[j].idx = 
17b60 28 75 31 36 29 69 3b 0a 20 20 7d 65 6c 73 65 7b  (u16)i;.  }else{
17b61 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71  .    int rc = sq
17b62 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
17b63 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
17b64 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
17b65 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
17b66 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20 20 20  *pRC = rc;.     
17b67 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
17b68 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
17b69 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
17b6a 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
17b6b 65 29 20 29 3b 0a 20 20 20 20 64 61 74 61 20 3d  e) );.    data =
17b6c 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20   pPage->aData;. 
17b6d 20 20 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20     cellOffset = 
17b6e 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65  pPage->cellOffse
17b6f 74 3b 0a 20 20 20 20 65 6e 64 20 3d 20 63 65 6c  t;.    end = cel
17b70 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61 67  lOffset + 2*pPag
17b71 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 69 6e  e->nCell;.    in
17b72 73 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b  s = cellOffset +
17b73 20 32 2a 69 3b 0a 20 20 20 20 72 63 20 3d 20 61   2*i;.    rc = a
17b74 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 70 50 61  llocateSpace(pPa
17b75 67 65 2c 20 73 7a 2c 20 26 69 64 78 29 3b 0a 20  ge, sz, &idx);. 
17b76 20 20 20 69 66 28 20 72 63 20 29 7b 20 2a 70 52     if( rc ){ *pR
17b77 43 20 3d 20 72 63 3b 20 72 65 74 75 72 6e 3b 20  C = rc; return; 
17b78 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 6c 6c  }.    /* The all
17b79 6f 63 61 74 65 53 70 61 63 65 28 29 20 72 6f 75  ocateSpace() rou
17b7a 74 69 6e 65 20 67 75 61 72 61 6e 74 65 65 73 20  tine guarantees 
17b7b 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  the following tw
17b7c 6f 20 70 72 6f 70 65 72 74 69 65 73 0a 20 20 20  o properties.   
17b7d 20 2a 2a 20 69 66 20 69 74 20 72 65 74 75 72 6e   ** if it return
17b7e 73 20 73 75 63 63 65 73 73 20 2a 2f 0a 20 20 20  s success */.   
17b7f 20 61 73 73 65 72 74 28 20 69 64 78 20 3e 3d 20   assert( idx >= 
17b80 65 6e 64 2b 32 20 29 3b 0a 20 20 20 20 61 73 73  end+2 );.    ass
17b81 65 72 74 28 20 69 64 78 2b 73 7a 20 3c 3d 20 70  ert( idx+sz <= p
17b82 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
17b83 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 70 50 61  eSize );.    pPa
17b84 67 65 2d 3e 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20  ge->nCell++;.   
17b85 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 2d 3d   pPage->nFree -=
17b86 20 28 75 31 36 29 28 32 20 2b 20 73 7a 29 3b 0a   (u16)(2 + sz);.
17b87 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61      memcpy(&data
17b88 5b 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65  [idx+nSkip], pCe
17b89 6c 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b  ll+nSkip, sz-nSk
17b8a 69 70 29 3b 0a 20 20 20 20 69 66 28 20 69 43 68  ip);.    if( iCh
17b8b 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 75 74  ild ){.      put
17b8c 34 62 79 74 65 28 26 64 61 74 61 5b 69 64 78 5d  4byte(&data[idx]
17b8d 2c 20 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 7d  , iChild);.    }
17b8e 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e 64 2c 20  .    for(j=end, 
17b8f 70 74 72 3d 26 64 61 74 61 5b 6a 5d 3b 20 6a 3e  ptr=&data[j]; j>
17b90 69 6e 73 3b 20 6a 2d 3d 32 2c 20 70 74 72 2d 3d  ins; j-=2, ptr-=
17b91 32 29 7b 0a 20 20 20 20 20 20 70 74 72 5b 30 5d  2){.      ptr[0]
17b92 20 3d 20 70 74 72 5b 2d 32 5d 3b 0a 20 20 20 20   = ptr[-2];.    
17b93 20 20 70 74 72 5b 31 5d 20 3d 20 70 74 72 5b 2d    ptr[1] = ptr[-
17b94 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 75  1];.    }.    pu
17b95 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 6e 73  t2byte(&data[ins
17b96 5d 2c 20 69 64 78 29 3b 0a 20 20 20 20 70 75 74  ], idx);.    put
17b97 32 62 79 74 65 28 26 64 61 74 61 5b 70 50 61 67  2byte(&data[pPag
17b98 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 33 5d 2c  e->hdrOffset+3],
17b99 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a   pPage->nCell);.
17b9a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
17b9b 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
17b9c 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 42     if( pPage->pB
17b9d 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b  t->autoVacuum ){
17b9e 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 65  .      /* The ce
17b9f 6c 6c 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61  ll may contain a
17ba0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f   pointer to an o
17ba1 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66  verflow page. If
17ba2 20 73 6f 2c 20 77 72 69 74 65 0a 20 20 20 20 20   so, write.     
17ba3 20 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 66 6f   ** the entry fo
17ba4 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  r the overflow p
17ba5 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 6f 69  age into the poi
17ba6 6e 74 65 72 20 6d 61 70 2e 0a 20 20 20 20 20 20  nter map..      
17ba7 2a 2f 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50  */.      ptrmapP
17ba8 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67 65 2c  utOvflPtr(pPage,
17ba9 20 70 43 65 6c 6c 2c 20 70 52 43 29 3b 0a 20 20   pCell, pRC);.  
17baa 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d    }.#endif.  }.}
17bab 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69  ../*.** Add a li
17bac 73 74 20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61  st of cells to a
17bad 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65   page.  The page
17bae 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 69 74 69   should be initi
17baf 61 6c 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54  ally empty..** T
17bb0 68 65 20 63 65 6c 6c 73 20 61 72 65 20 67 75 61  he cells are gua
17bb1 72 61 6e 74 65 65 64 20 74 6f 20 66 69 74 20 6f  ranteed to fit o
17bb2 6e 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73  n the page..*/.s
17bb3 74 61 74 69 63 20 76 6f 69 64 20 61 73 73 65 6d  tatic void assem
17bb4 62 6c 65 50 61 67 65 28 0a 20 20 4d 65 6d 50 61  blePage(.  MemPa
17bb5 67 65 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20  ge *pPage,   /* 
17bb6 54 68 65 20 70 61 67 65 20 74 6f 20 62 65 20 61  The page to be a
17bb7 73 73 65 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69  ssemblied */.  i
17bb8 6e 74 20 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20  nt nCell,       
17bb9 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f   /* The number o
17bba 66 20 63 65 6c 6c 73 20 74 6f 20 61 64 64 20 74  f cells to add t
17bbb 6f 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20  o this page */. 
17bbc 20 75 38 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20   u8 **apCell,   
17bbd 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74     /* Pointers t
17bbe 6f 20 63 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f  o cell bodies */
17bbf 0a 20 20 75 31 36 20 2a 61 53 69 7a 65 20 20 20  .  u16 *aSize   
17bc0 20 20 20 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66       /* Sizes of
17bc1 20 74 68 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b   the cells */.){
17bc2 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
17bc3 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
17bc4 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 70 43  nter */.  u8 *pC
17bc5 65 6c 6c 70 74 72 3b 20 20 20 20 20 2f 2a 20 41  ellptr;     /* A
17bc6 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63  ddress of next c
17bc7 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20  ell pointer */. 
17bc8 20 69 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20   int cellbody;  
17bc9 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
17bca 20 6e 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20   next cell body 
17bcb 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20  */.  u8 * const 
17bcc 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
17bcd 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20  ata;            
17bce 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64   /* Pointer to d
17bcf 61 74 61 20 66 6f 72 20 70 50 61 67 65 20 2a 2f  ata for pPage */
17bd0 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 68 64 72  .  const int hdr
17bd1 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66   = pPage->hdrOff
17bd2 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  set;           /
17bd3 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64  * Offset of head
17bd4 65 72 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20  er on pPage */. 
17bd5 20 63 6f 6e 73 74 20 69 6e 74 20 6e 55 73 61 62   const int nUsab
17bd6 6c 65 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 2d  le = pPage->pBt-
17bd7 3e 75 73 61 62 6c 65 53 69 7a 65 3b 20 2f 2a 20  >usableSize; /* 
17bd8 55 73 61 62 6c 65 20 73 69 7a 65 20 6f 66 20 70  Usable size of p
17bd9 61 67 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  age */..  assert
17bda 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c  ( pPage->nOverfl
17bdb 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ow==0 );.  asser
17bdc 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
17bdd 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
17bde 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
17bdf 73 65 72 74 28 20 6e 43 65 6c 6c 3e 3d 30 20 26  sert( nCell>=0 &
17be0 26 20 6e 43 65 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c  & nCell<=MX_CELL
17be1 28 70 50 61 67 65 2d 3e 70 42 74 29 20 26 26 20  (pPage->pBt) && 
17be2 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70  MX_CELL(pPage->p
17be3 42 74 29 3c 3d 35 34 36 30 20 29 3b 0a 20 20 61  Bt)<=5460 );.  a
17be4 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
17be5 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
17be6 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
17be7 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  ;..  /* Check th
17be8 61 74 20 74 68 65 20 70 61 67 65 20 68 61 73 20  at the page has 
17be9 6a 75 73 74 20 62 65 65 6e 20 7a 65 72 6f 65 64  just been zeroed
17bea 20 62 79 20 7a 65 72 6f 50 61 67 65 28 29 20 2a   by zeroPage() *
17beb 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  /.  assert( pPag
17bec 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20  e->nCell==0 );. 
17bed 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79 74   assert( get2byt
17bee 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3d  e(&data[hdr+5])=
17bef 3d 6e 55 73 61 62 6c 65 20 29 3b 0a 0a 20 20 70  =nUsable );..  p
17bf0 43 65 6c 6c 70 74 72 20 3d 20 26 64 61 74 61 5b  Cellptr = &data[
17bf1 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65  pPage->cellOffse
17bf2 74 20 2b 20 6e 43 65 6c 6c 2a 32 5d 3b 0a 20 20  t + nCell*2];.  
17bf3 63 65 6c 6c 62 6f 64 79 20 3d 20 6e 55 73 61 62  cellbody = nUsab
17bf4 6c 65 3b 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c  le;.  for(i=nCel
17bf5 6c 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b  l-1; i>=0; i--){
17bf6 0a 20 20 20 20 70 43 65 6c 6c 70 74 72 20 2d 3d  .    pCellptr -=
17bf7 20 32 3b 0a 20 20 20 20 63 65 6c 6c 62 6f 64 79   2;.    cellbody
17bf8 20 2d 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20   -= aSize[i];.  
17bf9 20 20 70 75 74 32 62 79 74 65 28 70 43 65 6c 6c    put2byte(pCell
17bfa 70 74 72 2c 20 63 65 6c 6c 62 6f 64 79 29 3b 0a  ptr, cellbody);.
17bfb 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61      memcpy(&data
17bfc 5b 63 65 6c 6c 62 6f 64 79 5d 2c 20 61 70 43 65  [cellbody], apCe
17bfd 6c 6c 5b 69 5d 2c 20 61 53 69 7a 65 5b 69 5d 29  ll[i], aSize[i])
17bfe 3b 0a 20 20 7d 0a 20 20 70 75 74 32 62 79 74 65  ;.  }.  put2byte
17bff 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e  (&data[hdr+3], n
17c00 43 65 6c 6c 29 3b 0a 20 20 70 75 74 32 62 79 74  Cell);.  put2byt
17c01 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20  e(&data[hdr+5], 
17c02 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61  cellbody);.  pPa
17c03 67 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 6e 43  ge->nFree -= (nC
17c04 65 6c 6c 2a 32 20 2b 20 6e 55 73 61 62 6c 65 20  ell*2 + nUsable 
17c05 2d 20 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 70  - cellbody);.  p
17c06 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 28 75  Page->nCell = (u
17c07 31 36 29 6e 43 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a  16)nCell;.}../*.
17c08 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
17c09 20 70 61 72 61 6d 65 74 65 72 73 20 64 65 74 65   parameters dete
17c0a 72 6d 69 6e 65 20 68 6f 77 20 6d 61 6e 79 20 61  rmine how many a
17c0b 64 6a 61 63 65 6e 74 20 70 61 67 65 73 20 67 65  djacent pages ge
17c0c 74 20 69 6e 76 6f 6c 76 65 64 0a 2a 2a 20 69 6e  t involved.** in
17c0d 20 61 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65   a balancing ope
17c0e 72 61 74 69 6f 6e 2e 20 20 4e 4e 20 69 73 20 74  ration.  NN is t
17c0f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 69  he number of nei
17c10 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72  ghbors on either
17c11 20 73 69 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20   side.** of the 
17c12 70 61 67 65 20 74 68 61 74 20 70 61 72 74 69 63  page that partic
17c13 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c  ipate in the bal
17c14 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ancing operation
17c15 2e 20 20 4e 42 20 69 73 20 74 68 65 0a 2a 2a 20  .  NB is the.** 
17c16 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
17c17 70 61 67 65 73 20 74 68 61 74 20 70 61 72 74 69  pages that parti
17c18 63 69 70 61 74 65 2c 20 69 6e 63 6c 75 64 69 6e  cipate, includin
17c19 67 20 74 68 65 20 74 61 72 67 65 74 20 70 61 67  g the target pag
17c1a 65 20 61 6e 64 0a 2a 2a 20 4e 4e 20 6e 65 69 67  e and.** NN neig
17c1b 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65 72 20  hbors on either 
17c1c 73 69 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  side..**.** The 
17c1d 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  minimum value of
17c1e 20 4e 4e 20 69 73 20 31 20 28 6f 66 20 63 6f 75   NN is 1 (of cou
17c1f 72 73 65 29 2e 20 20 49 6e 63 72 65 61 73 69 6e  rse).  Increasin
17c20 67 20 4e 4e 20 61 62 6f 76 65 20 31 0a 2a 2a 20  g NN above 1.** 
17c21 28 74 6f 20 32 20 6f 72 20 33 29 20 67 69 76 65  (to 2 or 3) give
17c22 73 20 61 20 6d 6f 64 65 73 74 20 69 6d 70 72 6f  s a modest impro
17c23 76 65 6d 65 6e 74 20 69 6e 20 53 45 4c 45 43 54  vement in SELECT
17c24 20 61 6e 64 20 44 45 4c 45 54 45 20 70 65 72 66   and DELETE perf
17c25 6f 72 6d 61 6e 63 65 0a 2a 2a 20 69 6e 20 65 78  ormance.** in ex
17c26 63 68 61 6e 67 65 20 66 6f 72 20 61 20 6c 61 72  change for a lar
17c27 67 65 72 20 64 65 67 72 61 64 61 74 69 6f 6e 20  ger degradation 
17c28 69 6e 20 49 4e 53 45 52 54 20 61 6e 64 20 55 50  in INSERT and UP
17c29 44 41 54 45 20 70 65 72 66 6f 72 6d 61 6e 63 65  DATE performance
17c2a 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f  ..** The value o
17c2b 66 20 4e 4e 20 61 70 70 65 61 72 73 20 74 6f 20  f NN appears to 
17c2c 67 69 76 65 20 74 68 65 20 62 65 73 74 20 72 65  give the best re
17c2d 73 75 6c 74 73 20 6f 76 65 72 61 6c 6c 2e 0a 2a  sults overall..*
17c2e 2f 0a 23 64 65 66 69 6e 65 20 4e 4e 20 31 20 20  /.#define NN 1  
17c2f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
17c30 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72  mber of neighbor
17c31 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65  s on either side
17c32 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 23 64 65   of pPage */.#de
17c33 66 69 6e 65 20 4e 42 20 28 4e 4e 2a 32 2b 31 29  fine NB (NN*2+1)
17c34 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 70        /* Total p
17c35 61 67 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e  ages involved in
17c36 20 74 68 65 20 62 61 6c 61 6e 63 65 20 2a 2f 0a   the balance */.
17c37 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
17c38 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c 41 4e  _OMIT_QUICKBALAN
17c39 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65  CE./*.** This ve
17c3a 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e 63 65  rsion of balance
17c3b 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65 20 63  () handles the c
17c3c 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20 63 61  ommon special ca
17c3d 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20 6e 65  se where.** a ne
17c3e 77 20 65 6e 74 72 79 20 69 73 20 62 65 69 6e 67  w entry is being
17c3f 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74 68 65   inserted on the
17c40 20 65 78 74 72 65 6d 65 20 72 69 67 68 74 2d 65   extreme right-e
17c41 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74 72 65  nd of the.** tre
17c42 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f 72 64  e, in other word
17c43 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65 77 20  s, when the new 
17c44 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63 6f 6d  entry will becom
17c45 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a  e the largest.**
17c46 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72   entry in the tr
17c47 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65 61  ee..**.** Instea
17c48 64 20 6f 66 20 74 72 79 69 6e 67 20 74 6f 20 62  d of trying to b
17c49 61 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 67  alance the 3 rig
17c4a 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 67  ht-most leaf pag
17c4b 65 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a 20  es, just add.** 
17c4c 61 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68  a new page to th
17c4d 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
17c4e 65 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f 6e  e and put the on
17c4f 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a 2a  e new entry in.*
17c50 2a 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68  * that page.  Th
17c51 69 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 69  is leaves the ri
17c52 67 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 20  ght side of the 
17c53 74 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a 2a  tree somewhat.**
17c54 20 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75   unbalanced.  Bu
17c55 74 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 20  t odds are that 
17c56 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72  we will be inser
17c57 74 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 73  ting new entries
17c58 0a 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73  .** at the end s
17c59 6f 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 73  oon afterwards s
17c5a 6f 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70  o the nearly emp
17c5b 74 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69  ty page will qui
17c5c 63 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e  ckly.** fill up.
17c5d 20 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a 2a    On average..**
17c5e 0a 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 65  .** pPage is the
17c5f 20 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68   leaf page which
17c60 20 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f   is the right-mo
17c61 73 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 74  st page in the t
17c62 72 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 20  ree..** pParent 
17c63 69 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20  is its parent.  
17c64 70 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 20  pPage must have 
17c65 61 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c 6f  a single overflo
17c66 77 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 68  w entry.** which
17c67 20 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 67   is also the rig
17c68 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f 6e  ht-most entry on
17c69 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
17c6a 20 54 68 65 20 70 53 70 61 63 65 20 62 75 66 66   The pSpace buff
17c6b 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  er is used to st
17c6c 6f 72 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20  ore a temporary 
17c6d 63 6f 70 79 20 6f 66 20 74 68 65 20 64 69 76 69  copy of the divi
17c6e 64 65 72 0a 2a 2a 20 63 65 6c 6c 20 74 68 61 74  der.** cell that
17c6f 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 65   will be inserte
17c70 64 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20  d into pParent. 
17c71 53 75 63 68 20 61 20 63 65 6c 6c 20 63 6f 6e 73  Such a cell cons
17c72 69 73 74 73 20 6f 66 20 61 20 34 0a 2a 2a 20 62  ists of a 4.** b
17c73 79 74 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  yte page number 
17c74 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 76 61  followed by a va
17c75 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20 69 6e  riable length in
17c76 74 65 67 65 72 2e 20 49 6e 20 6f 74 68 65 72 0a  teger. In other.
17c77 2a 2a 20 77 6f 72 64 73 2c 20 61 74 20 6d 6f 73  ** words, at mos
17c78 74 20 31 33 20 62 79 74 65 73 2e 20 48 65 6e 63  t 13 bytes. Henc
17c79 65 20 74 68 65 20 70 53 70 61 63 65 20 62 75 66  e the pSpace buf
17c7a 66 65 72 20 6d 75 73 74 20 62 65 20 61 74 0a 2a  fer must be at.*
17c7b 2a 20 6c 65 61 73 74 20 31 33 20 62 79 74 65 73  * least 13 bytes
17c7c 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61   in size..*/.sta
17c7d 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f  tic int balance_
17c7e 71 75 69 63 6b 28 4d 65 6d 50 61 67 65 20 2a 70  quick(MemPage *p
17c7f 50 61 72 65 6e 74 2c 20 4d 65 6d 50 61 67 65 20  Parent, MemPage 
17c80 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 53 70 61  *pPage, u8 *pSpa
17c81 63 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  ce){.  BtShared 
17c82 2a 63 6f 6e 73 74 20 70 42 74 20 3d 20 70 50 61  *const pBt = pPa
17c83 67 65 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 42  ge->pBt;    /* B
17c84 2d 54 72 65 65 20 44 61 74 61 62 61 73 65 20 2a  -Tree Database *
17c85 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65  /.  MemPage *pNe
17c86 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
17c87 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 6c           /* Newl
17c88 79 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65  y allocated page
17c89 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
17c8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c8b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
17c8c 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 50  turn Code */.  P
17c8d 67 6e 6f 20 70 67 6e 6f 4e 65 77 3b 20 20 20 20  gno pgnoNew;    
17c8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c8f 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
17c90 65 72 20 6f 66 20 70 4e 65 77 20 2a 2f 0a 0a 20  er of pNew */.. 
17c91 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17c92 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
17c93 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
17c94 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
17c95 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
17c96 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62  ble(pParent->pDb
17c97 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72  Page) );.  asser
17c98 74 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  t( pPage->nOverf
17c99 6c 6f 77 3d 3d 31 20 29 3b 0a 0a 20 20 69 66 28  low==1 );..  if(
17c9a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d 30   pPage->nCell<=0
17c9b 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
17c9c 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 0a  _CORRUPT_BKPT;..
17c9d 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20    /* Allocate a 
17c9e 6e 65 77 20 70 61 67 65 2e 20 54 68 69 73 20 70  new page. This p
17c9f 61 67 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20  age will become 
17ca0 74 68 65 20 72 69 67 68 74 2d 73 69 62 6c 69 6e  the right-siblin
17ca1 67 20 6f 66 20 0a 20 20 2a 2a 20 70 50 61 67 65  g of .  ** pPage
17ca2 2e 20 4d 61 6b 65 20 74 68 65 20 70 61 72 65 6e  . Make the paren
17ca3 74 20 70 61 67 65 20 77 72 69 74 61 62 6c 65 2c  t page writable,
17ca4 20 73 6f 20 74 68 61 74 20 74 68 65 20 6e 65 77   so that the new
17ca5 20 64 69 76 69 64 65 72 20 63 65 6c 6c 0a 20 20   divider cell.  
17ca6 2a 2a 20 6d 61 79 20 62 65 20 69 6e 73 65 72 74  ** may be insert
17ca7 65 64 2e 20 49 66 20 62 6f 74 68 20 74 68 65 73  ed. If both thes
17ca8 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65  e operations are
17ca9 20 73 75 63 63 65 73 73 66 75 6c 2c 20 70 72 6f   successful, pro
17caa 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  ceed..  */.  rc 
17cab 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  = allocateBtreeP
17cac 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20  age(pBt, &pNew, 
17cad 26 70 67 6e 6f 4e 65 77 2c 20 30 2c 20 30 29 3b  &pgnoNew, 0, 0);
17cae 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
17caf 54 45 5f 4f 4b 20 29 7b 0a 0a 20 20 20 20 75 38  TE_OK ){..    u8
17cb0 20 2a 70 4f 75 74 20 3d 20 26 70 53 70 61 63 65   *pOut = &pSpace
17cb1 5b 34 5d 3b 0a 20 20 20 20 75 38 20 2a 70 43 65  [4];.    u8 *pCe
17cb2 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 61 4f 76 66  ll = pPage->aOvf
17cb3 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 20  l[0].pCell;.    
17cb4 75 31 36 20 73 7a 43 65 6c 6c 20 3d 20 63 65 6c  u16 szCell = cel
17cb5 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20  lSizePtr(pPage, 
17cb6 70 43 65 6c 6c 29 3b 0a 20 20 20 20 75 38 20 2a  pCell);.    u8 *
17cb7 70 53 74 6f 70 3b 0a 0a 20 20 20 20 61 73 73 65  pStop;..    asse
17cb8 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
17cb9 49 73 77 72 69 74 65 61 62 6c 65 28 70 4e 65 77  Iswriteable(pNew
17cba 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
17cbb 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
17cbc 3e 61 44 61 74 61 5b 30 5d 3d 3d 28 50 54 46 5f  >aData[0]==(PTF_
17cbd 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 44  INTKEY|PTF_LEAFD
17cbe 41 54 41 7c 50 54 46 5f 4c 45 41 46 29 20 29 3b  ATA|PTF_LEAF) );
17cbf 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 4e  .    zeroPage(pN
17cc0 65 77 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50  ew, PTF_INTKEY|P
17cc1 54 46 5f 4c 45 41 46 44 41 54 41 7c 50 54 46 5f  TF_LEAFDATA|PTF_
17cc2 4c 45 41 46 29 3b 0a 20 20 20 20 61 73 73 65 6d  LEAF);.    assem
17cc3 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 31 2c  blePage(pNew, 1,
17cc4 20 26 70 43 65 6c 6c 2c 20 26 73 7a 43 65 6c 6c   &pCell, &szCell
17cc5 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  );..    /* If th
17cc6 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76 61  is is an auto-va
17cc7 63 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 75  cuum database, u
17cc8 70 64 61 74 65 20 74 68 65 20 70 6f 69 6e 74 65  pdate the pointe
17cc9 72 20 6d 61 70 0a 20 20 20 20 2a 2a 20 77 69 74  r map.    ** wit
17cca 68 20 65 6e 74 72 69 65 73 20 66 6f 72 20 74 68  h entries for th
17ccb 65 20 6e 65 77 20 70 61 67 65 2c 20 61 6e 64 20  e new page, and 
17ccc 61 6e 79 20 70 6f 69 6e 74 65 72 20 66 72 6f 6d  any pointer from
17ccd 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63 65 6c   the .    ** cel
17cce 6c 20 6f 6e 20 74 68 65 20 70 61 67 65 20 74 6f  l on the page to
17ccf 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
17cd0 65 2e 20 49 66 20 65 69 74 68 65 72 20 6f 66 20  e. If either of 
17cd1 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 6f 70 65  these.    ** ope
17cd2 72 61 74 69 6f 6e 73 20 66 61 69 6c 73 2c 20 74  rations fails, t
17cd3 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 69  he return code i
17cd4 73 20 73 65 74 2c 20 62 75 74 20 74 68 65 20 63  s set, but the c
17cd5 6f 6e 74 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f  ontents.    ** o
17cd6 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  f the parent pag
17cd7 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61 6e 69  e are still mani
17cd8 70 75 6c 61 74 65 64 20 62 79 20 74 68 68 20 63  pulated by thh c
17cd9 6f 64 65 20 62 65 6c 6f 77 2e 0a 20 20 20 20 2a  ode below..    *
17cda 2a 20 54 68 61 74 20 69 73 20 4f 6b 2c 20 61 74  * That is Ok, at
17cdb 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20   this point the 
17cdc 70 61 72 65 6e 74 20 70 61 67 65 20 69 73 20 67  parent page is g
17cdd 75 61 72 61 6e 74 65 65 64 20 74 6f 0a 20 20 20  uaranteed to.   
17cde 20 2a 2a 20 62 65 20 6d 61 72 6b 65 64 20 61 73   ** be marked as
17cdf 20 64 69 72 74 79 2e 20 52 65 74 75 72 6e 69 6e   dirty. Returnin
17ce0 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  g an error code 
17ce1 77 69 6c 6c 20 63 61 75 73 65 20 61 0a 20 20 20  will cause a.   
17ce2 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c 20 75 6e   ** rollback, un
17ce3 64 6f 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 65  doing any change
17ce4 73 20 6d 61 64 65 20 74 6f 20 74 68 65 20 70 61  s made to the pa
17ce5 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 20 20 2a  rent page..    *
17ce6 2f 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f  /.    if( ISAUTO
17ce7 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20  VACUUM ){.      
17ce8 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70  ptrmapPut(pBt, p
17ce9 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f 42  gnoNew, PTRMAP_B
17cea 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70  TREE, pParent->p
17ceb 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20  gno, &rc);.     
17cec 20 69 66 28 20 73 7a 43 65 6c 6c 3e 70 4e 65 77   if( szCell>pNew
17ced 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20  ->minLocal ){.  
17cee 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f        ptrmapPutO
17cef 76 66 6c 50 74 72 28 70 4e 65 77 2c 20 70 43 65  vflPtr(pNew, pCe
17cf0 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20  ll, &rc);.      
17cf1 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
17cf2 2a 20 43 72 65 61 74 65 20 61 20 64 69 76 69 64  * Create a divid
17cf3 65 72 20 63 65 6c 6c 20 74 6f 20 69 6e 73 65 72  er cell to inser
17cf4 74 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20  t into pParent. 
17cf5 54 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c  The divider cell
17cf6 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 73 74 73  .    ** consists
17cf7 20 6f 66 20 61 20 34 2d 62 79 74 65 20 70 61 67   of a 4-byte pag
17cf8 65 20 6e 75 6d 62 65 72 20 28 74 68 65 20 70 61  e number (the pa
17cf9 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 50 61  ge number of pPa
17cfa 67 65 29 20 61 6e 64 0a 20 20 20 20 2a 2a 20 61  ge) and.    ** a
17cfb 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   variable length
17cfc 20 6b 65 79 20 76 61 6c 75 65 20 28 77 68 69 63   key value (whic
17cfd 68 20 6d 75 73 74 20 62 65 20 74 68 65 20 73 61  h must be the sa
17cfe 6d 65 20 76 61 6c 75 65 20 61 73 20 74 68 65 0a  me value as the.
17cff 20 20 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 6b      ** largest k
17d00 65 79 20 6f 6e 20 70 50 61 67 65 29 2e 0a 20 20  ey on pPage)..  
17d01 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 66    **.    ** To f
17d02 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74 20  ind the largest 
17d03 6b 65 79 20 76 61 6c 75 65 20 6f 6e 20 70 50 61  key value on pPa
17d04 67 65 2c 20 66 69 72 73 74 20 66 69 6e 64 20 74  ge, first find t
17d05 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 0a 20  he right-most . 
17d06 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20 70 50     ** cell on pP
17d07 61 67 65 2e 20 54 68 65 20 66 69 72 73 74 20 74  age. The first t
17d08 77 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69  wo fields of thi
17d09 73 20 63 65 6c 6c 20 61 72 65 20 74 68 65 20 0a  s cell are the .
17d0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64 2d 6c 65      ** record-le
17d0b 6e 67 74 68 20 28 61 20 76 61 72 69 61 62 6c 65  ngth (a variable
17d0c 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20   length integer 
17d0d 61 74 20 6d 6f 73 74 20 33 32 2d 62 69 74 73 20  at most 32-bits 
17d0e 69 6e 20 73 69 7a 65 29 0a 20 20 20 20 2a 2a 20  in size).    ** 
17d0f 61 6e 64 20 74 68 65 20 6b 65 79 20 76 61 6c 75  and the key valu
17d10 65 20 28 61 20 76 61 72 69 61 62 6c 65 20 6c 65  e (a variable le
17d11 6e 67 74 68 20 69 6e 74 65 67 65 72 2c 20 6d 61  ngth integer, ma
17d12 79 20 68 61 76 65 20 61 6e 79 20 76 61 6c 75 65  y have any value
17d13 29 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69  )..    ** The fi
17d14 72 73 74 20 6f 66 20 74 68 65 20 77 68 69 6c 65  rst of the while
17d15 28 2e 2e 2e 29 20 6c 6f 6f 70 73 20 62 65 6c 6f  (...) loops belo
17d16 77 20 73 6b 69 70 73 20 6f 76 65 72 20 74 68 65  w skips over the
17d17 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68 0a 20   record-length. 
17d18 20 20 20 2a 2a 20 66 69 65 6c 64 2e 20 54 68 65     ** field. The
17d19 20 73 65 63 6f 6e 64 20 77 68 69 6c 65 28 2e 2e   second while(..
17d1a 2e 29 20 6c 6f 6f 70 20 63 6f 70 69 65 73 20 74  .) loop copies t
17d1b 68 65 20 6b 65 79 20 76 61 6c 75 65 20 66 72 6f  he key value fro
17d1c 6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 65 6c  m the.    ** cel
17d1d 6c 20 6f 6e 20 70 50 61 67 65 20 69 6e 74 6f 20  l on pPage into 
17d1e 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65  the pSpace buffe
17d1f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 43  r..    */.    pC
17d20 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
17d21 50 61 67 65 2c 20 70 50 61 67 65 2d 3e 6e 43 65  Page, pPage->nCe
17d22 6c 6c 2d 31 29 3b 0a 20 20 20 20 70 53 74 6f 70  ll-1);.    pStop
17d23 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20 20   = &pCell[9];.  
17d24 20 20 77 68 69 6c 65 28 20 28 2a 28 70 43 65 6c    while( (*(pCel
17d25 6c 2b 2b 29 26 30 78 38 30 29 20 26 26 20 70 43  l++)&0x80) && pC
17d26 65 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a 20 20 20  ell<pStop );.   
17d27 20 70 53 74 6f 70 20 3d 20 26 70 43 65 6c 6c 5b   pStop = &pCell[
17d28 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28  9];.    while( (
17d29 28 2a 28 70 4f 75 74 2b 2b 29 20 3d 20 2a 28 70  (*(pOut++) = *(p
17d2a 43 65 6c 6c 2b 2b 29 29 26 30 78 38 30 29 20 26  Cell++))&0x80) &
17d2b 26 20 70 43 65 6c 6c 3c 70 53 74 6f 70 20 29 3b  & pCell<pStop );
17d2c 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20  ..    /* Insert 
17d2d 74 68 65 20 6e 65 77 20 64 69 76 69 64 65 72 20  the new divider 
17d2e 63 65 6c 6c 20 69 6e 74 6f 20 70 50 61 72 65 6e  cell into pParen
17d2f 74 2e 20 2a 2f 0a 20 20 20 20 69 6e 73 65 72 74  t. */.    insert
17d30 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 50  Cell(pParent, pP
17d31 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2c 20 70 53  arent->nCell, pS
17d32 70 61 63 65 2c 20 28 69 6e 74 29 28 70 4f 75 74  pace, (int)(pOut
17d33 2d 70 53 70 61 63 65 29 2c 0a 20 20 20 20 20 20  -pSpace),.      
17d34 20 20 20 20 20 20 20 20 20 30 2c 20 70 50 61 67           0, pPag
17d35 65 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 0a  e->pgno, &rc);..
17d36 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 72      /* Set the r
17d37 69 67 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e 74  ight-child point
17d38 65 72 20 6f 66 20 70 50 61 72 65 6e 74 20 74 6f  er of pParent to
17d39 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65   point to the ne
17d3a 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 70  w page. */.    p
17d3b 75 74 34 62 79 74 65 28 26 70 50 61 72 65 6e 74  ut4byte(&pParent
17d3c 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d  ->aData[pParent-
17d3d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70  >hdrOffset+8], p
17d3e 67 6e 6f 4e 65 77 29 3b 0a 20 20 0a 20 20 20 20  gnoNew);.  .    
17d3f 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 72  /* Release the r
17d40 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
17d41 6e 65 77 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20  new page. */.   
17d42 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e 65   releasePage(pNe
17d43 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  w);.  }..  retur
17d44 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  n rc;.}.#endif /
17d45 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55  * SQLITE_OMIT_QU
17d46 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 23  ICKBALANCE */..#
17d47 69 66 20 30 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  if 0./*.** This 
17d48 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  function does no
17d49 74 20 63 6f 6e 74 72 69 62 75 74 65 20 61 6e 79  t contribute any
17d4a 74 68 69 6e 67 20 74 6f 20 74 68 65 20 6f 70 65  thing to the ope
17d4b 72 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65  ration of SQLite
17d4c 2e 0a 2a 2a 20 69 74 20 69 73 20 73 6f 6d 65 74  ..** it is somet
17d4d 69 6d 65 73 20 61 63 74 69 76 61 74 65 64 20 74  imes activated t
17d4e 65 6d 70 6f 72 61 72 69 6c 79 20 77 68 69 6c 65  emporarily while
17d4f 20 64 65 62 75 67 67 69 6e 67 20 63 6f 64 65 20   debugging code 
17d50 72 65 73 70 6f 6e 73 69 62 6c 65 20 0a 2a 2a 20  responsible .** 
17d51 66 6f 72 20 73 65 74 74 69 6e 67 20 70 6f 69 6e  for setting poin
17d52 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 2e  ter-map entries.
17d53 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
17d54 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28  trmapCheckPages(
17d55 4d 65 6d 50 61 67 65 20 2a 2a 61 70 50 61 67 65  MemPage **apPage
17d56 2c 20 69 6e 74 20 6e 50 61 67 65 29 7b 0a 20 20  , int nPage){.  
17d57 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 66 6f 72 28  int i, j;.  for(
17d58 69 3d 30 3b 20 69 3c 6e 50 61 67 65 3b 20 69 2b  i=0; i<nPage; i+
17d59 2b 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 3b 0a  +){.    Pgno n;.
17d5a 20 20 20 20 75 38 20 65 3b 0a 20 20 20 20 4d 65      u8 e;.    Me
17d5b 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 61  mPage *pPage = a
17d5c 70 50 61 67 65 5b 69 5d 3b 0a 20 20 20 20 42 74  pPage[i];.    Bt
17d5d 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50  Shared *pBt = pP
17d5e 61 67 65 2d 3e 70 42 74 3b 0a 20 20 20 20 61 73  age->pBt;.    as
17d5f 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49  sert( pPage->isI
17d60 6e 69 74 20 29 3b 0a 0a 20 20 20 20 66 6f 72 28  nit );..    for(
17d61 6a 3d 30 3b 20 6a 3c 70 50 61 67 65 2d 3e 6e 43  j=0; j<pPage->nC
17d62 65 6c 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  ell; j++){.     
17d63 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
17d64 20 20 20 20 20 20 75 38 20 2a 7a 3b 0a 20 20 20        u8 *z;.   
17d65 20 20 0a 20 20 20 20 20 20 7a 20 3d 20 66 69 6e    .      z = fin
17d66 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6a 29 3b  dCell(pPage, j);
17d67 0a 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73  .      btreePars
17d68 65 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20  eCellPtr(pPage, 
17d69 7a 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20  z, &info);.     
17d6a 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66   if( info.iOverf
17d6b 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 50  low ){.        P
17d6c 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34 62  gno ovfl = get4b
17d6d 79 74 65 28 26 7a 5b 69 6e 66 6f 2e 69 4f 76 65  yte(&z[info.iOve
17d6e 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20 20  rflow]);.       
17d6f 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20   ptrmapGet(pBt, 
17d70 6f 76 66 6c 2c 20 26 65 2c 20 26 6e 29 3b 0a 20  ovfl, &e, &n);. 
17d71 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e         assert( n
17d72 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26  ==pPage->pgno &&
17d73 20 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46   e==PTRMAP_OVERF
17d74 4c 4f 57 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a  LOW1 );.      }.
17d75 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65        if( !pPage
17d76 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
17d77 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d 20 67    Pgno child = g
17d78 65 74 34 62 79 74 65 28 7a 29 3b 0a 20 20 20 20  et4byte(z);.    
17d79 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42      ptrmapGet(pB
17d7a 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e  t, child, &e, &n
17d7b 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
17d7c 74 28 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e  t( n==pPage->pgn
17d7d 6f 20 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42  o && e==PTRMAP_B
17d7e 54 52 45 45 20 29 3b 0a 20 20 20 20 20 20 7d 0a  TREE );.      }.
17d7f 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70      }.    if( !p
17d80 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
17d81 20 20 20 20 50 67 6e 6f 20 63 68 69 6c 64 20 3d      Pgno child =
17d82 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
17d83 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68  ->aData[pPage->h
17d84 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20  drOffset+8]);.  
17d85 20 20 20 20 70 74 72 6d 61 70 47 65 74 28 70 42      ptrmapGet(pB
17d86 74 2c 20 63 68 69 6c 64 2c 20 26 65 2c 20 26 6e  t, child, &e, &n
17d87 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
17d88 20 6e 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20   n==pPage->pgno 
17d89 26 26 20 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52  && e==PTRMAP_BTR
17d8a 45 45 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  EE );.    }.  }.
17d8b 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
17d8c 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
17d8d 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
17d8e 64 20 74 6f 20 63 6f 70 79 20 74 68 65 20 63 6f  d to copy the co
17d8f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 2d  ntents of the b-
17d90 74 72 65 65 20 6e 6f 64 65 20 73 74 6f 72 65 64  tree node stored
17d91 20 0a 2a 2a 20 6f 6e 20 70 61 67 65 20 70 46 72   .** on page pFr
17d92 6f 6d 20 74 6f 20 70 61 67 65 20 70 54 6f 2e 20  om to page pTo. 
17d93 49 66 20 70 61 67 65 20 70 46 72 6f 6d 20 77 61  If page pFrom wa
17d94 73 20 6e 6f 74 20 61 20 6c 65 61 66 20 70 61 67  s not a leaf pag
17d95 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 70  e, then.** the p
17d96 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69  ointer-map entri
17d97 65 73 20 66 6f 72 20 65 61 63 68 20 63 68 69 6c  es for each chil
17d98 64 20 70 61 67 65 20 61 72 65 20 75 70 64 61 74  d page are updat
17d99 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a  ed so that the.*
17d9a 2a 20 70 61 72 65 6e 74 20 70 61 67 65 20 73 74  * parent page st
17d9b 6f 72 65 64 20 69 6e 20 74 68 65 20 70 6f 69 6e  ored in the poin
17d9c 74 65 72 20 6d 61 70 20 69 73 20 70 61 67 65 20  ter map is page 
17d9d 70 54 6f 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f  pTo. If pFrom co
17d9e 6e 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 79 20 63  ntained.** any c
17d9f 65 6c 6c 73 20 77 69 74 68 20 6f 76 65 72 66 6c  ells with overfl
17da0 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 73  ow page pointers
17da1 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 72 72 65  , then the corre
17da2 73 70 6f 6e 64 69 6e 67 20 70 6f 69 6e 74 65 72  sponding pointer
17da3 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73 20  .** map entries 
17da4 61 72 65 20 61 6c 73 6f 20 75 70 64 61 74 65 64  are also updated
17da5 20 73 6f 20 74 68 61 74 20 74 68 65 20 70 61 72   so that the par
17da6 65 6e 74 20 70 61 67 65 20 69 73 20 70 61 67 65  ent page is page
17da7 20 70 54 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70   pTo..**.** If p
17da8 46 72 6f 6d 20 69 73 20 63 75 72 72 65 6e 74 6c  From is currentl
17da9 79 20 63 61 72 72 79 69 6e 67 20 61 6e 79 20 6f  y carrying any o
17daa 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 28 65  verflow cells (e
17dab 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a  ntries in the.**
17dac 20 4d 65 6d 50 61 67 65 2e 61 4f 76 66 6c 5b 5d   MemPage.aOvfl[]
17dad 20 61 72 72 61 79 29 2c 20 74 68 65 79 20 61 72   array), they ar
17dae 65 20 6e 6f 74 20 63 6f 70 69 65 64 20 74 6f 20  e not copied to 
17daf 70 54 6f 2e 20 0a 2a 2a 0a 2a 2a 20 42 65 66 6f  pTo. .**.** Befo
17db0 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 70 61  re returning, pa
17db1 67 65 20 70 54 6f 20 69 73 20 72 65 69 6e 69 74  ge pTo is reinit
17db2 69 61 6c 69 7a 65 64 20 75 73 69 6e 67 20 62 74  ialized using bt
17db3 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a  reeInitPage()..*
17db4 2a 0a 2a 2a 20 54 68 65 20 70 65 72 66 6f 72 6d  *.** The perform
17db5 61 6e 63 65 20 6f 66 20 74 68 69 73 20 66 75 6e  ance of this fun
17db6 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63 72 69  ction is not cri
17db7 74 69 63 61 6c 2e 20 49 74 20 69 73 20 6f 6e 6c  tical. It is onl
17db8 79 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 74 68  y used by .** th
17db9 65 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f  e balance_shallo
17dba 77 65 72 28 29 20 61 6e 64 20 62 61 6c 61 6e 63  wer() and balanc
17dbb 65 5f 64 65 65 70 65 72 28 29 20 70 72 6f 63 65  e_deeper() proce
17dbc 64 75 72 65 73 2c 20 6e 65 69 74 68 65 72 20 6f  dures, neither o
17dbd 66 0a 2a 2a 20 77 68 69 63 68 20 61 72 65 20 63  f.** which are c
17dbe 61 6c 6c 65 64 20 6f 66 74 65 6e 20 75 6e 64 65  alled often unde
17dbf 72 20 6e 6f 72 6d 61 6c 20 63 69 72 63 75 6d 73  r normal circums
17dc0 74 61 6e 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  tances..*/.stati
17dc1 63 20 76 6f 69 64 20 63 6f 70 79 4e 6f 64 65 43  c void copyNodeC
17dc2 6f 6e 74 65 6e 74 28 4d 65 6d 50 61 67 65 20 2a  ontent(MemPage *
17dc3 70 46 72 6f 6d 2c 20 4d 65 6d 50 61 67 65 20 2a  pFrom, MemPage *
17dc4 70 54 6f 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a  pTo, int *pRC){.
17dc5 20 20 69 66 28 20 28 2a 70 52 43 29 3d 3d 53 51    if( (*pRC)==SQ
17dc6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 42  LITE_OK ){.    B
17dc7 74 53 68 61 72 65 64 20 2a 20 63 6f 6e 73 74 20  tShared * const 
17dc8 70 42 74 20 3d 20 70 46 72 6f 6d 2d 3e 70 42 74  pBt = pFrom->pBt
17dc9 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74  ;.    u8 * const
17dca 20 61 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 2d 3e   aFrom = pFrom->
17dcb 61 44 61 74 61 3b 0a 20 20 20 20 75 38 20 2a 20  aData;.    u8 * 
17dcc 63 6f 6e 73 74 20 61 54 6f 20 3d 20 70 54 6f 2d  const aTo = pTo-
17dcd 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 6e 74 20  >aData;.    int 
17dce 63 6f 6e 73 74 20 69 46 72 6f 6d 48 64 72 20 3d  const iFromHdr =
17dcf 20 70 46 72 6f 6d 2d 3e 68 64 72 4f 66 66 73 65   pFrom->hdrOffse
17dd0 74 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6e 73 74  t;.    int const
17dd1 20 69 54 6f 48 64 72 20 3d 20 28 28 70 54 6f 2d   iToHdr = ((pTo-
17dd2 3e 70 67 6e 6f 3d 3d 31 29 20 3f 20 31 30 30 20  >pgno==1) ? 100 
17dd3 3a 20 30 29 3b 0a 20 20 20 20 69 6e 74 20 72 63  : 0);.    int rc
17dd4 3b 0a 20 20 20 20 69 6e 74 20 69 44 61 74 61 3b  ;.    int iData;
17dd5 0a 20 20 0a 20 20 0a 20 20 20 20 61 73 73 65 72  .  .  .    asser
17dd6 74 28 20 70 46 72 6f 6d 2d 3e 69 73 49 6e 69 74  t( pFrom->isInit
17dd7 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
17dd8 70 46 72 6f 6d 2d 3e 6e 46 72 65 65 3e 3d 69 54  pFrom->nFree>=iT
17dd9 6f 48 64 72 20 29 3b 0a 20 20 20 20 61 73 73 65  oHdr );.    asse
17dda 72 74 28 20 67 65 74 32 62 79 74 65 28 26 61 46  rt( get2byte(&aF
17ddb 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 2b 35 5d 29  rom[iFromHdr+5])
17ddc 3c 3d 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  <=pBt->usableSiz
17ddd 65 20 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 43  e );.  .    /* C
17dde 6f 70 79 20 74 68 65 20 62 2d 74 72 65 65 20 6e  opy the b-tree n
17ddf 6f 64 65 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d  ode content from
17de0 20 70 61 67 65 20 70 46 72 6f 6d 20 74 6f 20 70   page pFrom to p
17de1 61 67 65 20 70 54 6f 2e 20 2a 2f 0a 20 20 20 20  age pTo. */.    
17de2 69 44 61 74 61 20 3d 20 67 65 74 32 62 79 74 65  iData = get2byte
17de3 28 26 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72  (&aFrom[iFromHdr
17de4 2b 35 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  +5]);.    memcpy
17de5 28 26 61 54 6f 5b 69 44 61 74 61 5d 2c 20 26 61  (&aTo[iData], &a
17de6 46 72 6f 6d 5b 69 44 61 74 61 5d 2c 20 70 42 74  From[iData], pBt
17de7 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 69 44 61  ->usableSize-iDa
17de8 74 61 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  ta);.    memcpy(
17de9 26 61 54 6f 5b 69 54 6f 48 64 72 5d 2c 20 26 61  &aTo[iToHdr], &a
17dea 46 72 6f 6d 5b 69 46 72 6f 6d 48 64 72 5d 2c 20  From[iFromHdr], 
17deb 70 46 72 6f 6d 2d 3e 63 65 6c 6c 4f 66 66 73 65  pFrom->cellOffse
17dec 74 20 2b 20 32 2a 70 46 72 6f 6d 2d 3e 6e 43 65  t + 2*pFrom->nCe
17ded 6c 6c 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52  ll);.  .    /* R
17dee 65 69 6e 69 74 69 61 6c 69 7a 65 20 70 61 67 65  einitialize page
17def 20 70 54 6f 20 73 6f 20 74 68 61 74 20 74 68 65   pTo so that the
17df0 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
17df1 20 4d 65 6d 50 61 67 65 20 73 74 72 75 63 74 75   MemPage structu
17df2 72 65 0a 20 20 20 20 2a 2a 20 6d 61 74 63 68 20  re.    ** match 
17df3 74 68 65 20 6e 65 77 20 64 61 74 61 2e 20 54 68  the new data. Th
17df4 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  e initialization
17df5 20 6f 66 20 70 54 6f 20 63 61 6e 20 61 63 74 75   of pTo can actu
17df6 61 6c 6c 79 20 66 61 69 6c 20 75 6e 64 65 72 0a  ally fail under.
17df7 20 20 20 20 2a 2a 20 66 61 69 72 6c 79 20 6f 62      ** fairly ob
17df8 73 63 75 72 65 20 63 69 72 63 75 6d 73 74 61 6e  scure circumstan
17df9 63 65 73 2c 20 65 76 65 6e 20 74 68 6f 75 67 68  ces, even though
17dfa 20 69 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66   it is a copy of
17dfb 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 0a 20 20   initialized .  
17dfc 20 20 2a 2a 20 70 61 67 65 20 70 46 72 6f 6d 2e    ** page pFrom.
17dfd 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 6f 2d  .    */.    pTo-
17dfe 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20  >isInit = 0;.   
17dff 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69 74 50   rc = btreeInitP
17e00 61 67 65 28 70 54 6f 29 3b 0a 20 20 20 20 69 66  age(pTo);.    if
17e01 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
17e02 29 7b 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20  ){.      *pRC = 
17e03 72 63 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  rc;.      return
17e04 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
17e05 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20  * If this is an 
17e06 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
17e07 62 61 73 65 2c 20 75 70 64 61 74 65 20 74 68 65  base, update the
17e08 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74   pointer-map ent
17e09 72 69 65 73 0a 20 20 20 20 2a 2a 20 66 6f 72 20  ries.    ** for 
17e0a 61 6e 79 20 62 2d 74 72 65 65 20 6f 72 20 6f 76  any b-tree or ov
17e0b 65 72 66 6c 6f 77 20 70 61 67 65 73 20 74 68 61  erflow pages tha
17e0c 74 20 70 54 6f 20 6e 6f 77 20 63 6f 6e 74 61 69  t pTo now contai
17e0d 6e 73 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20  ns the pointers 
17e0e 74 6f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  to..    */.    i
17e0f 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20  f( ISAUTOVACUUM 
17e10 29 7b 0a 20 20 20 20 20 20 2a 70 52 43 20 3d 20  ){.      *pRC = 
17e11 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28  setChildPtrmaps(
17e12 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  pTo);.    }.  }.
17e13 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
17e14 75 74 69 6e 65 20 72 65 64 69 73 74 72 69 62 75  utine redistribu
17e15 74 65 73 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65  tes cells on the
17e16 20 69 50 61 72 65 6e 74 49 64 78 27 74 68 20 63   iParentIdx'th c
17e17 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 0a  hild of pParent.
17e18 2a 2a 20 28 68 65 72 65 61 66 74 65 72 20 22 74  ** (hereafter "t
17e19 68 65 20 70 61 67 65 22 29 20 61 6e 64 20 75 70  he page") and up
17e1a 20 74 6f 20 32 20 73 69 62 6c 69 6e 67 73 20 73   to 2 siblings s
17e1b 6f 20 74 68 61 74 20 61 6c 6c 20 70 61 67 65 73  o that all pages
17e1c 20 68 61 76 65 20 61 62 6f 75 74 20 74 68 65 0a   have about the.
17e1d 2a 2a 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f  ** same amount o
17e1e 66 20 66 72 65 65 20 73 70 61 63 65 2e 20 55 73  f free space. Us
17e1f 75 61 6c 6c 79 20 61 20 73 69 6e 67 6c 65 20 73  ually a single s
17e20 69 62 6c 69 6e 67 20 6f 6e 20 65 69 74 68 65 72  ibling on either
17e21 20 73 69 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20   side of the.** 
17e22 70 61 67 65 20 61 72 65 20 75 73 65 64 20 69 6e  page are used in
17e23 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20   the balancing, 
17e24 74 68 6f 75 67 68 20 62 6f 74 68 20 73 69 62 6c  though both sibl
17e25 69 6e 67 73 20 6d 69 67 68 74 20 63 6f 6d 65 20  ings might come 
17e26 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 73 69 64 65  from one.** side
17e27 20 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20   if the page is 
17e28 74 68 65 20 66 69 72 73 74 20 6f 72 20 6c 61 73  the first or las
17e29 74 20 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70  t child of its p
17e2a 61 72 65 6e 74 2e 20 49 66 20 74 68 65 20 70 61  arent. If the pa
17e2b 67 65 20 0a 2a 2a 20 68 61 73 20 66 65 77 65 72  ge .** has fewer
17e2c 20 74 68 61 6e 20 32 20 73 69 62 6c 69 6e 67 73   than 2 siblings
17e2d 20 28 73 6f 6d 65 74 68 69 6e 67 20 77 68 69 63   (something whic
17e2e 68 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65  h can only happe
17e2f 6e 20 69 66 20 74 68 65 20 70 61 67 65 0a 2a 2a  n if the page.**
17e30 20 69 73 20 61 20 72 6f 6f 74 20 70 61 67 65 20   is a root page 
17e31 6f 72 20 61 20 63 68 69 6c 64 20 6f 66 20 61 20  or a child of a 
17e32 72 6f 6f 74 20 70 61 67 65 29 20 74 68 65 6e 20  root page) then 
17e33 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20 73 69  all available si
17e34 62 6c 69 6e 67 73 0a 2a 2a 20 70 61 72 74 69 63  blings.** partic
17e35 69 70 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c  ipate in the bal
17e36 61 6e 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  ancing..**.** Th
17e37 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c  e number of sibl
17e38 69 6e 67 73 20 6f 66 20 74 68 65 20 70 61 67 65  ings of the page
17e39 20 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 61   might be increa
17e3a 73 65 64 20 6f 72 20 64 65 63 72 65 61 73 65 64  sed or decreased
17e3b 20 62 79 20 0a 2a 2a 20 6f 6e 65 20 6f 72 20 74   by .** one or t
17e3c 77 6f 20 69 6e 20 61 6e 20 65 66 66 6f 72 74 20  wo in an effort 
17e3d 74 6f 20 6b 65 65 70 20 70 61 67 65 73 20 6e 65  to keep pages ne
17e3e 61 72 6c 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f  arly full but no
17e3f 74 20 6f 76 65 72 20 66 75 6c 6c 2e 20 0a 2a 2a  t over full. .**
17e40 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68  .** Note that wh
17e41 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
17e42 69 73 20 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20  is called, some 
17e43 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20  of the cells on 
17e44 74 68 65 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68  the page.** migh
17e45 74 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 62  t not actually b
17e46 65 20 73 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50  e stored in MemP
17e47 61 67 65 2e 61 44 61 74 61 5b 5d 2e 20 54 68 69  age.aData[]. Thi
17e48 73 20 63 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20  s can happen.** 
17e49 69 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6f  if the page is o
17e4a 76 65 72 66 75 6c 6c 2e 20 54 68 69 73 20 72 6f  verfull. This ro
17e4b 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74 68  utine ensures th
17e4c 61 74 20 61 6c 6c 20 63 65 6c 6c 73 20 61 6c 6c  at all cells all
17e4d 6f 63 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65  ocated.** to the
17e4e 20 70 61 67 65 20 61 6e 64 20 69 74 73 20 73 69   page and its si
17e4f 62 6c 69 6e 67 73 20 66 69 74 20 69 6e 74 6f 20  blings fit into 
17e50 4d 65 6d 50 61 67 65 2e 61 44 61 74 61 5b 5d 20  MemPage.aData[] 
17e51 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
17e52 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63  ..**.** In the c
17e53 6f 75 72 73 65 20 6f 66 20 62 61 6c 61 6e 63 69  ourse of balanci
17e54 6e 67 20 74 68 65 20 70 61 67 65 20 61 6e 64 20  ng the page and 
17e55 69 74 73 20 73 69 62 6c 69 6e 67 73 2c 20 63 65  its siblings, ce
17e56 6c 6c 73 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e  lls may be.** in
17e57 73 65 72 74 65 64 20 69 6e 74 6f 20 6f 72 20 72  serted into or r
17e58 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20  emoved from the 
17e59 70 61 72 65 6e 74 20 70 61 67 65 20 28 70 50 61  parent page (pPa
17e5a 72 65 6e 74 29 2e 20 44 6f 69 6e 67 20 73 6f 0a  rent). Doing so.
17e5b 2a 2a 20 6d 61 79 20 63 61 75 73 65 20 74 68 65  ** may cause the
17e5c 20 70 61 72 65 6e 74 20 70 61 67 65 20 74 6f 20   parent page to 
17e5d 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20  become overfull 
17e5e 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20 49 66  or underfull. If
17e5f 20 74 68 69 73 0a 2a 2a 20 68 61 70 70 65 6e 73   this.** happens
17e60 2c 20 69 74 20 69 73 20 74 68 65 20 72 65 73 70  , it is the resp
17e61 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68  onsibility of th
17e62 65 20 63 61 6c 6c 65 72 20 74 6f 20 69 6e 76 6f  e caller to invo
17e63 6b 65 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a  ke the correct.*
17e64 2a 20 62 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74  * balancing rout
17e65 69 6e 65 20 74 6f 20 66 69 78 20 74 68 69 73 20  ine to fix this 
17e66 70 72 6f 62 6c 65 6d 20 28 73 65 65 20 74 68 65  problem (see the
17e67 20 62 61 6c 61 6e 63 65 28 29 20 72 6f 75 74 69   balance() routi
17e68 6e 65 29 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ne). .**.** If t
17e69 68 69 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c  his routine fail
17e6a 73 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e  s for any reason
17e6b 2c 20 69 74 20 6d 69 67 68 74 20 6c 65 61 76 65  , it might leave
17e6c 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
17e6d 20 69 6e 20 61 20 63 6f 72 72 75 70 74 65 64 20   in a corrupted 
17e6e 73 74 61 74 65 2e 20 53 6f 20 69 66 20 74 68 69  state. So if thi
17e6f 73 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 2c  s routine fails,
17e70 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 68   the database sh
17e71 6f 75 6c 64 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65  ould.** be rolle
17e72 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  d back..**.** Th
17e73 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  e third argument
17e74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
17e75 6e 2c 20 61 4f 76 66 6c 53 70 61 63 65 2c 20 69  n, aOvflSpace, i
17e76 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
17e77 0a 2a 2a 20 62 75 66 66 65 72 20 62 69 67 20 65  .** buffer big e
17e78 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 6f 6e  nough to hold on
17e79 65 20 70 61 67 65 2e 20 49 66 20 77 68 69 6c 65  e page. If while
17e7a 20 69 6e 73 65 72 74 69 6e 67 20 63 65 6c 6c 73   inserting cells
17e7b 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74   into the parent
17e7c 0a 2a 2a 20 70 61 67 65 20 28 70 50 61 72 65 6e  .** page (pParen
17e7d 74 29 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  t) the parent pa
17e7e 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 65 72 66  ge becomes overf
17e7f 75 6c 6c 2c 20 74 68 69 73 20 62 75 66 66 65 72  ull, this buffer
17e80 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73   is.** used to s
17e81 74 6f 72 65 20 74 68 65 20 70 61 72 65 6e 74 27  tore the parent'
17e82 73 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73  s overflow cells
17e83 2e 20 42 65 63 61 75 73 65 20 74 68 69 73 20 66  . Because this f
17e84 75 6e 63 74 69 6f 6e 20 69 6e 73 65 72 74 73 0a  unction inserts.
17e85 2a 2a 20 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20  ** a maximum of 
17e86 66 6f 75 72 20 64 69 76 69 64 65 72 20 63 65 6c  four divider cel
17e87 6c 73 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65  ls into the pare
17e88 6e 74 20 70 61 67 65 2c 20 61 6e 64 20 74 68 65  nt page, and the
17e89 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 73 69 7a 65   maximum.** size
17e8a 20 6f 66 20 61 20 63 65 6c 6c 20 73 74 6f 72 65   of a cell store
17e8b 64 20 77 69 74 68 69 6e 20 61 6e 20 69 6e 74 65  d within an inte
17e8c 72 6e 61 6c 20 6e 6f 64 65 20 69 73 20 61 6c 77  rnal node is alw
17e8d 61 79 73 20 6c 65 73 73 20 74 68 61 6e 20 31 2f  ays less than 1/
17e8e 34 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65  4.** of the page
17e8f 2d 73 69 7a 65 2c 20 74 68 65 20 61 4f 76 66 6c  -size, the aOvfl
17e90 53 70 61 63 65 5b 5d 20 62 75 66 66 65 72 20 69  Space[] buffer i
17e91 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  s guaranteed to 
17e92 62 65 20 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75  be large.** enou
17e93 67 68 20 66 6f 72 20 61 6c 6c 20 6f 76 65 72 66  gh for all overf
17e94 6c 6f 77 20 63 65 6c 6c 73 2e 0a 2a 2a 0a 2a 2a  low cells..**.**
17e95 20 49 66 20 61 4f 76 66 6c 53 70 61 63 65 20 69   If aOvflSpace i
17e96 73 20 73 65 74 20 74 6f 20 61 20 6e 75 6c 6c 20  s set to a null 
17e97 70 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66 75  pointer, this fu
17e98 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a  nction returns .
17e99 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e  ** SQLITE_NOMEM.
17e9a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
17e9b 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 0a  alance_nonroot(.
17e9c 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65    MemPage *pPare
17e9d 6e 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nt,             
17e9e 20 20 2f 2a 20 50 61 72 65 6e 74 20 70 61 67 65    /* Parent page
17e9f 20 6f 66 20 73 69 62 6c 69 6e 67 73 20 62 65 69   of siblings bei
17ea0 6e 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20  ng balanced */. 
17ea1 20 69 6e 74 20 69 50 61 72 65 6e 74 49 64 78 2c   int iParentIdx,
17ea2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17ea3 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 22 74 68   /* Index of "th
17ea4 65 20 70 61 67 65 22 20 69 6e 20 70 50 61 72 65  e page" in pPare
17ea5 6e 74 20 2a 2f 0a 20 20 75 38 20 2a 61 4f 76 66  nt */.  u8 *aOvf
17ea6 6c 53 70 61 63 65 2c 20 20 20 20 20 20 20 20 20  lSpace,         
17ea7 20 20 20 20 20 20 20 20 2f 2a 20 70 61 67 65 2d          /* page-
17ea8 73 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70  size bytes of sp
17ea9 61 63 65 20 66 6f 72 20 70 61 72 65 6e 74 20 6f  ace for parent o
17eaa 76 66 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52  vfl */.  int isR
17eab 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  oot             
17eac 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
17ead 20 69 66 20 70 50 61 72 65 6e 74 20 69 73 20 61   if pParent is a
17eae 20 72 6f 6f 74 2d 70 61 67 65 20 2a 2f 0a 29 7b   root-page */.){
17eaf 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
17eb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
17eb1 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 61 74  /* The whole dat
17eb2 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  abase */.  int n
17eb3 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20  Cell = 0;       
17eb4 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
17eb5 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 70  r of cells in ap
17eb6 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  Cell[] */.  int 
17eb7 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b 20 20  nMaxCells = 0;  
17eb8 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f           /* Allo
17eb9 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 70  cated size of ap
17eba 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61 46  Cell, szCell, aF
17ebb 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e  rom. */.  int nN
17ebc 65 77 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ew = 0;         
17ebd 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
17ebe 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 4e   of pages in apN
17ebf 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f  ew[] */.  int nO
17ec0 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ld;             
17ec1 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
17ec2 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70 4f   of pages in apO
17ec3 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c  ld[] */.  int i,
17ec4 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20 20   j, k;          
17ec5 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
17ec6 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74  ounters */.  int
17ec7 20 6e 78 44 69 76 3b 20 20 20 20 20 20 20 20 20   nxDiv;         
17ec8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78            /* Nex
17ec9 74 20 64 69 76 69 64 65 72 20 73 6c 6f 74 20 69  t divider slot i
17eca 6e 20 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c  n pParent->aCell
17ecb 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  [] */.  int rc =
17ecc 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20   SQLITE_OK;     
17ecd 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75       /* The retu
17ece 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36  rn code */.  u16
17ecf 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b   leafCorrection;
17ed0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 34 20 69            /* 4 i
17ed1 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65 61  f pPage is a lea
17ed2 66 2e 20 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a  f.  0 if not */.
17ed3 20 20 69 6e 74 20 6c 65 61 66 44 61 74 61 3b 20    int leafData; 
17ed4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17ed5 2a 20 54 72 75 65 20 69 66 20 70 50 61 67 65 20  * True if pPage 
17ed6 69 73 20 61 20 6c 65 61 66 20 6f 66 20 61 20 4c  is a leaf of a L
17ed7 45 41 46 44 41 54 41 20 74 72 65 65 20 2a 2f 0a  EAFDATA tree */.
17ed8 20 20 69 6e 74 20 75 73 61 62 6c 65 53 70 61 63    int usableSpac
17ed9 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
17eda 2a 20 42 79 74 65 73 20 69 6e 20 70 50 61 67 65  * Bytes in pPage
17edb 20 62 65 79 6f 6e 64 20 74 68 65 20 68 65 61 64   beyond the head
17edc 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65  er */.  int page
17edd 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
17ede 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66       /* Value of
17edf 20 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d   pPage->aData[0]
17ee0 20 2a 2f 0a 20 20 69 6e 74 20 73 75 62 74 6f 74   */.  int subtot
17ee1 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  al;             
17ee2 20 20 20 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f     /* Subtotal o
17ee3 66 20 62 79 74 65 73 20 69 6e 20 63 65 6c 6c 73  f bytes in cells
17ee4 20 6f 6e 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a   on one page */.
17ee5 20 20 69 6e 74 20 69 53 70 61 63 65 31 20 3d 20    int iSpace1 = 
17ee6 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
17ee7 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20 62  * First unused b
17ee8 79 74 65 20 6f 66 20 61 53 70 61 63 65 31 5b 5d  yte of aSpace1[]
17ee9 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 76 66 6c 53   */.  int iOvflS
17eea 70 61 63 65 20 3d 20 30 3b 20 20 20 20 20 20 20  pace = 0;       
17eeb 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75 73     /* First unus
17eec 65 64 20 62 79 74 65 20 6f 66 20 61 4f 76 66 6c  ed byte of aOvfl
17eed 53 70 61 63 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74  Space[] */.  int
17eee 20 73 7a 53 63 72 61 74 63 68 3b 20 20 20 20 20   szScratch;     
17eef 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
17ef0 65 20 6f 66 20 73 63 72 61 74 63 68 20 6d 65 6d  e of scratch mem
17ef1 6f 72 79 20 72 65 71 75 65 73 74 65 64 20 2a 2f  ory requested */
17ef2 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c  .  MemPage *apOl
17ef3 64 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20  d[NB];          
17ef4 2f 2a 20 70 50 61 67 65 20 61 6e 64 20 75 70 20  /* pPage and up 
17ef5 74 6f 20 74 77 6f 20 73 69 62 6c 69 6e 67 73 20  to two siblings 
17ef6 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61 70  */.  MemPage *ap
17ef7 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20  Copy[NB];       
17ef8 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63 6f 70    /* Private cop
17ef9 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 70  ies of apOld[] p
17efa 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67  ages */.  MemPag
17efb 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b 20  e *apNew[NB+2]; 
17efc 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20         /* pPage 
17efd 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73 69 62  and up to NB sib
17efe 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61 6c 61  lings after bala
17eff 6e 63 69 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 70  ncing */.  u8 *p
17f00 52 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20  Right;          
17f01 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 63 61 74          /* Locat
17f02 69 6f 6e 20 69 6e 20 70 61 72 65 6e 74 20 6f 66  ion in parent of
17f03 20 72 69 67 68 74 2d 73 69 62 6c 69 6e 67 20 70   right-sibling p
17f04 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a  ointer */.  u8 *
17f05 61 70 44 69 76 5b 4e 42 2d 31 5d 3b 20 20 20 20  apDiv[NB-1];    
17f06 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 76 69           /* Divi
17f07 64 65 72 20 63 65 6c 6c 73 20 69 6e 20 70 50 61  der cells in pPa
17f08 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e  rent */.  int cn
17f09 74 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20  tNew[NB+2];     
17f0a 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
17f0b 69 6e 20 61 43 65 6c 6c 5b 5d 20 6f 66 20 63 65  in aCell[] of ce
17f0c 6c 6c 20 61 66 74 65 72 20 69 2d 74 68 20 70 61  ll after i-th pa
17f0d 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65  ge */.  int szNe
17f0e 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20  w[NB+2];        
17f0f 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64       /* Combined
17f10 20 73 69 7a 65 20 6f 66 20 63 65 6c 6c 73 20 70   size of cells p
17f11 6c 61 63 65 20 6f 6e 20 69 2d 74 68 20 70 61 67  lace on i-th pag
17f12 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65  e */.  u8 **apCe
17f13 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ll = 0;         
17f14 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73      /* All cells
17f15 20 62 65 67 69 6e 20 62 61 6c 61 6e 63 65 64 20   begin balanced 
17f16 2a 2f 0a 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c  */.  u16 *szCell
17f17 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
17f18 20 20 2f 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20    /* Local size 
17f19 6f 66 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20  of all cells in 
17f1a 61 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38  apCell[] */.  u8
17f1b 20 2a 61 53 70 61 63 65 31 3b 20 20 20 20 20 20   *aSpace1;      
17f1c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70             /* Sp
17f1d 61 63 65 20 66 6f 72 20 63 6f 70 69 65 73 20 6f  ace for copies o
17f1e 66 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73  f dividers cells
17f1f 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b   */.  Pgno pgno;
17f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f21 20 20 20 2f 2a 20 54 65 6d 70 20 76 61 72 20 74     /* Temp var t
17f22 6f 20 73 74 6f 72 65 20 61 20 70 61 67 65 20 6e  o store a page n
17f23 75 6d 62 65 72 20 69 6e 20 2a 2f 0a 0a 20 20 70  umber in */..  p
17f24 42 74 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70 42  Bt = pParent->pB
17f25 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  t;.  assert( sql
17f26 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
17f27 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
17f28 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
17f29 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
17f2a 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67  (pParent->pDbPag
17f2b 65 29 20 29 3b 0a 0a 23 69 66 20 30 0a 20 20 54  e) );..#if 0.  T
17f2c 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20  RACE(("BALANCE: 
17f2d 62 65 67 69 6e 20 70 61 67 65 20 25 64 20 63 68  begin page %d ch
17f2e 69 6c 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50  ild of %d\n", pP
17f2f 61 67 65 2d 3e 70 67 6e 6f 2c 20 70 50 61 72 65  age->pgno, pPare
17f30 6e 74 2d 3e 70 67 6e 6f 29 29 3b 0a 23 65 6e 64  nt->pgno));.#end
17f31 69 66 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73  if..  /* At this
17f32 20 70 6f 69 6e 74 20 70 50 61 72 65 6e 74 20 6d   point pParent m
17f33 61 79 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20  ay have at most 
17f34 6f 6e 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  one overflow cel
17f35 6c 2e 20 41 6e 64 20 69 66 0a 20 20 2a 2a 20 74  l. And if.  ** t
17f36 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  his overflow cel
17f37 6c 20 69 73 20 70 72 65 73 65 6e 74 2c 20 69 74  l is present, it
17f38 20 6d 75 73 74 20 62 65 20 74 68 65 20 63 65 6c   must be the cel
17f39 6c 20 77 69 74 68 20 0a 20 20 2a 2a 20 69 6e 64  l with .  ** ind
17f3a 65 78 20 69 50 61 72 65 6e 74 49 64 78 2e 20 54  ex iParentIdx. T
17f3b 68 69 73 20 73 63 65 6e 61 72 69 6f 20 63 6f 6d  his scenario com
17f3c 65 73 20 61 62 6f 75 74 20 77 68 65 6e 20 74 68  es about when th
17f3d 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a  is function.  **
17f3e 20 69 73 20 63 61 6c 6c 65 64 20 28 69 6e 64 69   is called (indi
17f3f 72 65 63 74 6c 79 29 20 66 72 6f 6d 20 73 71 6c  rectly) from sql
17f40 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28  ite3BtreeDelete(
17f41 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  )..  */.  assert
17f42 28 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72  ( pParent->nOver
17f43 66 6c 6f 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65  flow==0 || pPare
17f44 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31  nt->nOverflow==1
17f45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
17f46 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77  arent->nOverflow
17f47 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e 74 2d 3e  ==0 || pParent->
17f48 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 69 50  aOvfl[0].idx==iP
17f49 61 72 65 6e 74 49 64 78 20 29 3b 0a 0a 20 20 69  arentIdx );..  i
17f4a 66 28 20 21 61 4f 76 66 6c 53 70 61 63 65 20 29  f( !aOvflSpace )
17f4b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
17f4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a  ITE_NOMEM;.  }..
17f4d 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 73 69    /* Find the si
17f4e 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f 20 62  bling pages to b
17f4f 61 6c 61 6e 63 65 2e 20 41 6c 73 6f 20 6c 6f 63  alance. Also loc
17f50 61 74 65 20 74 68 65 20 63 65 6c 6c 73 20 69 6e  ate the cells in
17f51 20 70 50 61 72 65 6e 74 20 0a 20 20 2a 2a 20 74   pParent .  ** t
17f52 68 61 74 20 64 69 76 69 64 65 20 74 68 65 20 73  hat divide the s
17f53 69 62 6c 69 6e 67 73 2e 20 41 6e 20 61 74 74 65  iblings. An atte
17f54 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 66  mpt is made to f
17f55 69 6e 64 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20  ind NN siblings 
17f56 6f 6e 20 0a 20 20 2a 2a 20 65 69 74 68 65 72 20  on .  ** either 
17f57 73 69 64 65 20 6f 66 20 70 50 61 67 65 2e 20 4d  side of pPage. M
17f58 6f 72 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65  ore siblings are
17f59 20 74 61 6b 65 6e 20 66 72 6f 6d 20 6f 6e 65 20   taken from one 
17f5a 73 69 64 65 2c 20 68 6f 77 65 76 65 72 2c 20 0a  side, however, .
17f5b 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72    ** if there ar
17f5c 65 20 66 65 77 65 72 20 74 68 61 6e 20 4e 4e 20  e fewer than NN 
17f5d 73 69 62 6c 69 6e 67 73 20 6f 6e 20 74 68 65 20  siblings on the 
17f5e 6f 74 68 65 72 20 73 69 64 65 2e 20 49 66 20 70  other side. If p
17f5f 50 61 72 65 6e 74 0a 20 20 2a 2a 20 68 61 73 20  Parent.  ** has 
17f60 4e 42 20 6f 72 20 66 65 77 65 72 20 63 68 69 6c  NB or fewer chil
17f61 64 72 65 6e 20 74 68 65 6e 20 61 6c 6c 20 63 68  dren then all ch
17f62 69 6c 64 72 65 6e 20 6f 66 20 70 50 61 72 65 6e  ildren of pParen
17f63 74 20 61 72 65 20 74 61 6b 65 6e 2e 20 20 0a 20  t are taken.  . 
17f64 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f   **.  ** This lo
17f65 6f 70 20 61 6c 73 6f 20 64 72 6f 70 73 20 74 68  op also drops th
17f66 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20  e divider cells 
17f67 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20  from the parent 
17f68 70 61 67 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20  page. This.  ** 
17f69 77 61 79 2c 20 74 68 65 20 72 65 6d 61 69 6e 64  way, the remaind
17f6a 65 72 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  er of the functi
17f6b 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  on does not have
17f6c 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 61 6e   to deal with an
17f6d 79 0a 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20  y.  ** overflow 
17f6e 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61 72  cells in the par
17f6f 65 6e 74 20 70 61 67 65 2c 20 73 69 6e 63 65 20  ent page, since 
17f70 69 66 20 61 6e 79 20 65 78 69 73 74 65 64 20 74  if any existed t
17f71 68 65 79 20 77 69 6c 6c 0a 20 20 2a 2a 20 68 61  hey will.  ** ha
17f72 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ve already been 
17f73 72 65 6d 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20  removed..  */.  
17f74 69 20 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76  i = pParent->nOv
17f75 65 72 66 6c 6f 77 20 2b 20 70 50 61 72 65 6e 74  erflow + pParent
17f76 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 69 66 28 20 69  ->nCell;.  if( i
17f77 3c 32 20 29 7b 0a 20 20 20 20 6e 78 44 69 76 20  <2 ){.    nxDiv 
17f78 3d 20 30 3b 0a 20 20 20 20 6e 4f 6c 64 20 3d 20  = 0;.    nOld = 
17f79 69 2b 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  i+1;.  }else{.  
17f7a 20 20 6e 4f 6c 64 20 3d 20 33 3b 0a 20 20 20 20    nOld = 3;.    
17f7b 69 66 28 20 69 50 61 72 65 6e 74 49 64 78 3d 3d  if( iParentIdx==
17f7c 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
17f7d 20 20 20 20 20 0a 20 20 20 20 20 20 6e 78 44 69       .      nxDi
17f7e 76 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  v = 0;.    }else
17f7f 20 69 66 28 20 69 50 61 72 65 6e 74 49 64 78 3d   if( iParentIdx=
17f80 3d 69 20 29 7b 0a 20 20 20 20 20 20 6e 78 44 69  =i ){.      nxDi
17f81 76 20 3d 20 69 2d 32 3b 0a 20 20 20 20 7d 65 6c  v = i-2;.    }el
17f82 73 65 7b 0a 20 20 20 20 20 20 6e 78 44 69 76 20  se{.      nxDiv 
17f83 3d 20 69 50 61 72 65 6e 74 49 64 78 2d 31 3b 0a  = iParentIdx-1;.
17f84 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 32 3b      }.    i = 2;
17f85 0a 20 20 7d 0a 20 20 69 66 28 20 28 69 2b 6e 78  .  }.  if( (i+nx
17f86 44 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76  Div-pParent->nOv
17f87 65 72 66 6c 6f 77 29 3d 3d 70 50 61 72 65 6e 74  erflow)==pParent
17f88 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 70  ->nCell ){.    p
17f89 52 69 67 68 74 20 3d 20 26 70 50 61 72 65 6e 74  Right = &pParent
17f8a 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e 74 2d  ->aData[pParent-
17f8b 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 3b 0a 20  >hdrOffset+8];. 
17f8c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 52 69 67   }else{.    pRig
17f8d 68 74 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ht = findCell(pP
17f8e 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70  arent, i+nxDiv-p
17f8f 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f  Parent->nOverflo
17f90 77 29 3b 0a 20 20 7d 0a 20 20 70 67 6e 6f 20 3d  w);.  }.  pgno =
17f91 20 67 65 74 34 62 79 74 65 28 70 52 69 67 68 74   get4byte(pRight
17f92 29 3b 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b  );.  while( 1 ){
17f93 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64  .    rc = getAnd
17f94 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 67  InitPage(pBt, pg
17f95 6e 6f 2c 20 26 61 70 4f 6c 64 5b 69 5d 29 3b 0a  no, &apOld[i]);.
17f96 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
17f97 20 20 20 20 6d 65 6d 73 65 74 28 61 70 4f 6c 64      memset(apOld
17f98 2c 20 30 2c 20 28 69 2b 31 29 2a 73 69 7a 65 6f  , 0, (i+1)*sizeo
17f99 66 28 4d 65 6d 50 61 67 65 2a 29 29 3b 0a 20 20  f(MemPage*));.  
17f9a 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65      goto balance
17f9b 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
17f9c 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20 2b 3d      nMaxCells +=
17f9d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 65   1+apOld[i]->nCe
17f9e 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f 76  ll+apOld[i]->nOv
17f9f 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20  erflow;.    if( 
17fa0 28 69 2d 2d 29 3d 3d 30 20 29 20 62 72 65 61 6b  (i--)==0 ) break
17fa1 3b 0a 0a 20 20 20 20 69 66 28 20 69 2b 6e 78 44  ;..    if( i+nxD
17fa2 69 76 3d 3d 70 50 61 72 65 6e 74 2d 3e 61 4f 76  iv==pParent->aOv
17fa3 66 6c 5b 30 5d 2e 69 64 78 20 26 26 20 70 50 61  fl[0].idx && pPa
17fa4 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  rent->nOverflow 
17fa5 29 7b 0a 20 20 20 20 20 20 61 70 44 69 76 5b 69  ){.      apDiv[i
17fa6 5d 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61 4f 76  ] = pParent->aOv
17fa7 66 6c 5b 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20  fl[0].pCell;.   
17fa8 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62 79     pgno = get4by
17fa9 74 65 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20  te(apDiv[i]);.  
17faa 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63      szNew[i] = c
17fab 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 72 65  ellSizePtr(pPare
17fac 6e 74 2c 20 61 70 44 69 76 5b 69 5d 29 3b 0a 20  nt, apDiv[i]);. 
17fad 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 6e 4f       pParent->nO
17fae 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20  verflow = 0;.   
17faf 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 70   }else{.      ap
17fb0 44 69 76 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c  Div[i] = findCel
17fb1 6c 28 70 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44  l(pParent, i+nxD
17fb2 69 76 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65  iv-pParent->nOve
17fb3 72 66 6c 6f 77 29 3b 0a 20 20 20 20 20 20 70 67  rflow);.      pg
17fb4 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 61 70  no = get4byte(ap
17fb5 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 73  Div[i]);.      s
17fb6 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c 6c 53 69  zNew[i] = cellSi
17fb7 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61  zePtr(pParent, a
17fb8 70 44 69 76 5b 69 5d 29 3b 0a 0a 20 20 20 20 20  pDiv[i]);..     
17fb9 20 2f 2a 20 44 72 6f 70 20 74 68 65 20 63 65 6c   /* Drop the cel
17fba 6c 20 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e  l from the paren
17fbb 74 20 70 61 67 65 2e 20 61 70 44 69 76 5b 69 5d  t page. apDiv[i]
17fbc 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73 20 74 6f   still points to
17fbd 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 65  .      ** the ce
17fbe 6c 6c 20 77 69 74 68 69 6e 20 74 68 65 20 70 61  ll within the pa
17fbf 72 65 6e 74 2c 20 65 76 65 6e 20 74 68 6f 75 67  rent, even thoug
17fc0 68 20 69 74 20 68 61 73 20 62 65 65 6e 20 64 72  h it has been dr
17fc1 6f 70 70 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20  opped..      ** 
17fc2 54 68 69 73 20 69 73 20 73 61 66 65 20 62 65 63  This is safe bec
17fc3 61 75 73 65 20 64 72 6f 70 70 69 6e 67 20 61 20  ause dropping a 
17fc4 63 65 6c 6c 20 6f 6e 6c 79 20 6f 76 65 72 77 72  cell only overwr
17fc5 69 74 65 73 20 74 68 65 20 66 69 72 73 74 0a 20  ites the first. 
17fc6 20 20 20 20 20 2a 2a 20 66 6f 75 72 20 62 79 74       ** four byt
17fc7 65 73 20 6f 66 20 69 74 2c 20 61 6e 64 20 74 68  es of it, and th
17fc8 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  is function does
17fc9 20 6e 6f 74 20 6e 65 65 64 20 74 68 65 20 66 69   not need the fi
17fca 72 73 74 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75  rst.      ** fou
17fcb 72 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 64  r bytes of the d
17fcc 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 53 6f 20  ivider cell. So 
17fcd 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 73  the pointer is s
17fce 61 66 65 20 74 6f 20 75 73 65 0a 20 20 20 20 20  afe to use.     
17fcf 20 2a 2a 20 6c 61 74 65 72 20 6f 6e 2e 20 20 0a   ** later on.  .
17fd0 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
17fd1 2a 20 55 6e 6c 65 73 73 20 53 51 4c 69 74 65 20  * Unless SQLite 
17fd2 69 73 20 63 6f 6d 70 69 6c 65 64 20 69 6e 20 73  is compiled in s
17fd3 65 63 75 72 65 2d 64 65 6c 65 74 65 20 6d 6f 64  ecure-delete mod
17fd4 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  e. In this case,
17fd5 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 72  .      ** the dr
17fd6 6f 70 43 65 6c 6c 28 29 20 72 6f 75 74 69 6e 65  opCell() routine
17fd7 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20   will overwrite 
17fd8 74 68 65 20 65 6e 74 69 72 65 20 63 65 6c 6c 20  the entire cell 
17fd9 77 69 74 68 20 7a 65 72 6f 65 73 2e 0a 20 20 20  with zeroes..   
17fda 20 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61     ** In this ca
17fdb 73 65 2c 20 74 65 6d 70 6f 72 61 72 69 6c 79 20  se, temporarily 
17fdc 63 6f 70 79 20 74 68 65 20 63 65 6c 6c 20 69 6e  copy the cell in
17fdd 74 6f 20 74 68 65 20 61 4f 76 66 6c 53 70 61 63  to the aOvflSpac
17fde 65 5b 5d 0a 20 20 20 20 20 20 2a 2a 20 62 75 66  e[].      ** buf
17fdf 66 65 72 2e 20 49 74 20 77 69 6c 6c 20 62 65 20  fer. It will be 
17fe0 63 6f 70 69 65 64 20 6f 75 74 20 61 67 61 69 6e  copied out again
17fe1 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20   as soon as the 
17fe2 61 53 70 61 63 65 5b 5d 20 62 75 66 66 65 72 0a  aSpace[] buffer.
17fe3 20 20 20 20 20 20 2a 2a 20 69 73 20 61 6c 6c 6f        ** is allo
17fe4 63 61 74 65 64 2e 20 20 2a 2f 0a 23 69 66 64 65  cated.  */.#ifde
17fe5 66 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f  f SQLITE_SECURE_
17fe6 44 45 4c 45 54 45 0a 20 20 20 20 20 20 6d 65 6d  DELETE.      mem
17fe7 63 70 79 28 26 61 4f 76 66 6c 53 70 61 63 65 5b  cpy(&aOvflSpace[
17fe8 61 70 44 69 76 5b 69 5d 2d 70 50 61 72 65 6e 74  apDiv[i]-pParent
17fe9 2d 3e 61 44 61 74 61 5d 2c 20 61 70 44 69 76 5b  ->aData], apDiv[
17fea 69 5d 2c 20 73 7a 4e 65 77 5b 69 5d 29 3b 0a 20  i], szNew[i]);. 
17feb 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d 20       apDiv[i] = 
17fec 26 61 4f 76 66 6c 53 70 61 63 65 5b 61 70 44 69  &aOvflSpace[apDi
17fed 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d 3e 61 44  v[i]-pParent->aD
17fee 61 74 61 5d 3b 0a 23 65 6e 64 69 66 0a 20 20 20  ata];.#endif.   
17fef 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 72     dropCell(pPar
17ff0 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61  ent, i+nxDiv-pPa
17ff1 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2c  rent->nOverflow,
17ff2 20 73 7a 4e 65 77 5b 69 5d 2c 20 26 72 63 29 3b   szNew[i], &rc);
17ff3 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
17ff4 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c 6c 73 20   Make nMaxCells 
17ff5 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 34 20  a multiple of 4 
17ff6 69 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73  in order to pres
17ff7 65 72 76 65 20 38 2d 62 79 74 65 0a 20 20 2a 2a  erve 8-byte.  **
17ff8 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20 20   alignment */.  
17ff9 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28 6e 4d 61  nMaxCells = (nMa
17ffa 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e 33 3b 0a  xCells + 3)&~3;.
17ffb 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63  .  /*.  ** Alloc
17ffc 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65  ate space for me
17ffd 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 0a  mory structures.
17ffe 20 20 2a 2f 0a 20 20 6b 20 3d 20 70 42 74 2d 3e    */.  k = pBt->
17fff 70 61 67 65 53 69 7a 65 20 2b 20 52 4f 55 4e 44  pageSize + ROUND
18000 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65  8(sizeof(MemPage
18001 29 29 3b 0a 20 20 73 7a 53 63 72 61 74 63 68 20  ));.  szScratch 
18002 3d 0a 20 20 20 20 20 20 20 6e 4d 61 78 43 65 6c  =.       nMaxCel
18003 6c 73 2a 73 69 7a 65 6f 66 28 75 38 2a 29 20 20  ls*sizeof(u8*)  
18004 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18005 20 20 20 20 20 2f 2a 20 61 70 43 65 6c 6c 20 2a       /* apCell *
18006 2f 0a 20 20 20 20 20 2b 20 6e 4d 61 78 43 65 6c  /.     + nMaxCel
18007 6c 73 2a 73 69 7a 65 6f 66 28 75 31 36 29 20 20  ls*sizeof(u16)  
18008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18009 20 20 20 20 20 2f 2a 20 73 7a 43 65 6c 6c 20 2a       /* szCell *
1800a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d 3e 70 61  /.     + pBt->pa
1800b 67 65 53 69 7a 65 20 20 20 20 20 20 20 20 20 20  geSize          
1800c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1800d 20 20 20 20 20 2f 2a 20 61 53 70 61 63 65 31 20       /* aSpace1 
1800e 2a 2f 0a 20 20 20 20 20 2b 20 6b 2a 6e 4f 6c 64  */.     + k*nOld
1800f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
18010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18011 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63 6f        /* Page co
18012 70 69 65 73 20 28 61 70 43 6f 70 79 29 20 2a 2f  pies (apCopy) */
18013 0a 20 20 61 70 43 65 6c 6c 20 3d 20 73 71 6c 69  .  apCell = sqli
18014 74 65 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63  te3ScratchMalloc
18015 28 20 73 7a 53 63 72 61 74 63 68 20 29 3b 20 0a  ( szScratch ); .
18016 20 20 69 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20    if( apCell==0 
18017 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
18018 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f  TE_NOMEM;.    go
18019 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
1801a 75 70 3b 0a 20 20 7d 0a 20 20 73 7a 43 65 6c 6c  up;.  }.  szCell
1801b 20 3d 20 28 75 31 36 2a 29 26 61 70 43 65 6c 6c   = (u16*)&apCell
1801c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61  [nMaxCells];.  a
1801d 53 70 61 63 65 31 20 3d 20 28 75 38 2a 29 26 73  Space1 = (u8*)&s
1801e 7a 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d  zCell[nMaxCells]
1801f 3b 0a 20 20 61 73 73 65 72 74 28 20 45 49 47 48  ;.  assert( EIGH
18020 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54  T_BYTE_ALIGNMENT
18021 28 61 53 70 61 63 65 31 29 20 29 3b 0a 0a 20 20  (aSpace1) );..  
18022 2f 2a 0a 20 20 2a 2a 20 4c 6f 61 64 20 70 6f 69  /*.  ** Load poi
18023 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 63 65 6c  nters to all cel
18024 6c 73 20 6f 6e 20 73 69 62 6c 69 6e 67 20 70 61  ls on sibling pa
18025 67 65 73 20 61 6e 64 20 74 68 65 20 64 69 76 69  ges and the divi
18026 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69  der cells.  ** i
18027 6e 74 6f 20 74 68 65 20 6c 6f 63 61 6c 20 61 70  nto the local ap
18028 43 65 6c 6c 5b 5d 20 61 72 72 61 79 2e 20 20 4d  Cell[] array.  M
18029 61 6b 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68  ake copies of th
1802a 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 0a  e divider cells.
1802b 20 20 2a 2a 20 69 6e 74 6f 20 73 70 61 63 65 20    ** into space 
1802c 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 53  obtained from aS
1802d 70 61 63 65 31 5b 5d 20 61 6e 64 20 72 65 6d 6f  pace1[] and remo
1802e 76 65 20 74 68 65 20 74 68 65 20 64 69 76 69 64  ve the the divid
1802f 65 72 20 43 65 6c 6c 73 0a 20 20 2a 2a 20 66 72  er Cells.  ** fr
18030 6f 6d 20 70 50 61 72 65 6e 74 2e 0a 20 20 2a 2a  om pParent..  **
18031 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 73 69 62  .  ** If the sib
18032 6c 69 6e 67 73 20 61 72 65 20 6f 6e 20 6c 65 61  lings are on lea
18033 66 20 70 61 67 65 73 2c 20 74 68 65 6e 20 74 68  f pages, then th
18034 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73  e child pointers
18035 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 64 69 76   of the.  ** div
18036 69 64 65 72 20 63 65 6c 6c 73 20 61 72 65 20 73  ider cells are s
18037 74 72 69 70 70 65 64 20 66 72 6f 6d 20 74 68 65  tripped from the
18038 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 74 68   cells before th
18039 65 79 20 61 72 65 20 63 6f 70 69 65 64 0a 20 20  ey are copied.  
1803a 2a 2a 20 69 6e 74 6f 20 61 53 70 61 63 65 31 5b  ** into aSpace1[
1803b 5d 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79 2c  ].  In this way,
1803c 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70   all cells in ap
1803d 43 65 6c 6c 5b 5d 20 61 72 65 20 77 69 74 68 6f  Cell[] are witho
1803e 75 74 0a 20 20 2a 2a 20 63 68 69 6c 64 20 70 6f  ut.  ** child po
1803f 69 6e 74 65 72 73 2e 20 20 49 66 20 73 69 62 6c  inters.  If sibl
18040 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c 65 61  ings are not lea
18041 76 65 73 2c 20 74 68 65 6e 20 61 6c 6c 20 63 65  ves, then all ce
18042 6c 6c 20 69 6e 0a 20 20 2a 2a 20 61 70 43 65 6c  ll in.  ** apCel
18043 6c 5b 5d 20 69 6e 63 6c 75 64 65 20 63 68 69 6c  l[] include chil
18044 64 20 70 6f 69 6e 74 65 72 73 2e 20 20 45 69 74  d pointers.  Eit
18045 68 65 72 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c  her way, all cel
18046 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 0a 20  ls in apCell[]. 
18047 20 2a 2a 20 61 72 65 20 61 6c 69 6b 65 2e 0a 20   ** are alike.. 
18048 20 2a 2a 0a 20 20 2a 2a 20 6c 65 61 66 43 6f 72   **.  ** leafCor
18049 72 65 63 74 69 6f 6e 3a 20 20 34 20 69 66 20 70  rection:  4 if p
1804a 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20  Page is a leaf. 
1804b 20 30 20 69 66 20 70 50 61 67 65 20 69 73 20 6e   0 if pPage is n
1804c 6f 74 20 61 20 6c 65 61 66 2e 0a 20 20 2a 2a 20  ot a leaf..  ** 
1804d 20 20 20 20 20 20 6c 65 61 66 44 61 74 61 3a 20        leafData: 
1804e 20 31 20 69 66 20 70 50 61 67 65 20 68 6f 6c 64   1 if pPage hold
1804f 73 20 6b 65 79 2b 64 61 74 61 20 61 6e 64 20 70  s key+data and p
18050 50 61 72 65 6e 74 20 68 6f 6c 64 73 20 6f 6e 6c  Parent holds onl
18051 79 20 6b 65 79 73 2e 0a 20 20 2a 2f 0a 20 20 6c  y keys..  */.  l
18052 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 20 3d 20  eafCorrection = 
18053 61 70 4f 6c 64 5b 30 5d 2d 3e 6c 65 61 66 2a 34  apOld[0]->leaf*4
18054 3b 0a 20 20 6c 65 61 66 44 61 74 61 20 3d 20 61  ;.  leafData = a
18055 70 4f 6c 64 5b 30 5d 2d 3e 68 61 73 44 61 74 61  pOld[0]->hasData
18056 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
18057 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  Old; i++){.    i
18058 6e 74 20 6c 69 6d 69 74 3b 0a 20 20 20 20 0a 20  nt limit;.    . 
18059 20 20 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69     /* Before doi
1805a 6e 67 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65  ng anything else
1805b 2c 20 74 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  , take a copy of
1805c 20 74 68 65 20 69 27 74 68 20 6f 72 69 67 69 6e   the i'th origin
1805d 61 6c 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 2a  al sibling.    *
1805e 2a 20 54 68 65 20 72 65 73 74 20 6f 66 20 74 68  * The rest of th
1805f 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c  is function will
18060 20 75 73 65 20 64 61 74 61 20 66 72 6f 6d 20 74   use data from t
18061 68 65 20 63 6f 70 69 65 73 20 72 61 74 68 65 72  he copies rather
18062 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65  .    ** that the
18063 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20   original pages 
18064 73 69 6e 63 65 20 74 68 65 20 6f 72 69 67 69 6e  since the origin
18065 61 6c 20 70 61 67 65 73 20 77 69 6c 6c 20 62 65  al pages will be
18066 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 70   in the.    ** p
18067 72 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20  rocess of being 
18068 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 2a 2f  overwritten.  */
18069 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f  .    MemPage *pO
1806a 6c 64 20 3d 20 61 70 43 6f 70 79 5b 69 5d 20 3d  ld = apCopy[i] =
1806b 20 28 4d 65 6d 50 61 67 65 2a 29 26 61 53 70 61   (MemPage*)&aSpa
1806c 63 65 31 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a  ce1[pBt->pageSiz
1806d 65 20 2b 20 6b 2a 69 5d 3b 0a 20 20 20 20 6d 65  e + k*i];.    me
1806e 6d 63 70 79 28 70 4f 6c 64 2c 20 61 70 4f 6c 64  mcpy(pOld, apOld
1806f 5b 69 5d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 50  [i], sizeof(MemP
18070 61 67 65 29 29 3b 0a 20 20 20 20 70 4f 6c 64 2d  age));.    pOld-
18071 3e 61 44 61 74 61 20 3d 20 28 76 6f 69 64 2a 29  >aData = (void*)
18072 26 70 4f 6c 64 5b 31 5d 3b 0a 20 20 20 20 6d 65  &pOld[1];.    me
18073 6d 63 70 79 28 70 4f 6c 64 2d 3e 61 44 61 74 61  mcpy(pOld->aData
18074 2c 20 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74  , apOld[i]->aDat
18075 61 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  a, pBt->pageSize
18076 29 3b 0a 0a 20 20 20 20 6c 69 6d 69 74 20 3d 20  );..    limit = 
18077 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 2b 70 4f 6c 64  pOld->nCell+pOld
18078 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20  ->nOverflow;.   
18079 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69   for(j=0; j<limi
1807a 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 61  t; j++){.      a
1807b 73 73 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61  ssert( nCell<nMa
1807c 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20  xCells );.      
1807d 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20  apCell[nCell] = 
1807e 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c  findOverflowCell
1807f 28 70 4f 6c 64 2c 20 6a 29 3b 0a 20 20 20 20 20  (pOld, j);.     
18080 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d   szCell[nCell] =
18081 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c   cellSizePtr(pOl
18082 64 2c 20 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d  d, apCell[nCell]
18083 29 3b 0a 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b  );.      nCell++
18084 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
18085 69 3c 6e 4f 6c 64 2d 31 20 26 26 20 21 6c 65 61  i<nOld-1 && !lea
18086 66 44 61 74 61 29 7b 0a 20 20 20 20 20 20 75 31  fData){.      u1
18087 36 20 73 7a 20 3d 20 28 75 31 36 29 73 7a 4e 65  6 sz = (u16)szNe
18088 77 5b 69 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a  w[i];.      u8 *
18089 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 61 73 73  pTemp;.      ass
1808a 65 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43  ert( nCell<nMaxC
1808b 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 73 7a  ells );.      sz
1808c 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a  Cell[nCell] = sz
1808d 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20  ;.      pTemp = 
1808e 26 61 53 70 61 63 65 31 5b 69 53 70 61 63 65 31  &aSpace1[iSpace1
1808f 5d 3b 0a 20 20 20 20 20 20 69 53 70 61 63 65 31  ];.      iSpace1
18090 20 2b 3d 20 73 7a 3b 0a 20 20 20 20 20 20 61 73   += sz;.      as
18091 73 65 72 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70  sert( sz<=pBt->p
18092 61 67 65 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20  ageSize/4 );.   
18093 20 20 20 61 73 73 65 72 74 28 20 69 53 70 61 63     assert( iSpac
18094 65 31 3c 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a  e1<=pBt->pageSiz
18095 65 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  e );.      memcp
18096 79 28 70 54 65 6d 70 2c 20 61 70 44 69 76 5b 69  y(pTemp, apDiv[i
18097 5d 2c 20 73 7a 29 3b 0a 20 20 20 20 20 20 61 70  ], sz);.      ap
18098 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54  Cell[nCell] = pT
18099 65 6d 70 2b 6c 65 61 66 43 6f 72 72 65 63 74 69  emp+leafCorrecti
1809a 6f 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  on;.      assert
1809b 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  ( leafCorrection
1809c 3d 3d 30 20 7c 7c 20 6c 65 61 66 43 6f 72 72 65  ==0 || leafCorre
1809d 63 74 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20  ction==4 );.    
1809e 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20    szCell[nCell] 
1809f 3d 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20  = szCell[nCell] 
180a0 2d 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  - leafCorrection
180a1 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4f 6c  ;.      if( !pOl
180a2 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  d->leaf ){.     
180a3 20 20 20 61 73 73 65 72 74 28 20 6c 65 61 66 43     assert( leafC
180a4 6f 72 72 65 63 74 69 6f 6e 3d 3d 30 20 29 3b 0a  orrection==0 );.
180a5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
180a6 70 4f 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74 3d  pOld->hdrOffset=
180a7 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 );.        /*
180a8 20 54 68 65 20 72 69 67 68 74 20 70 6f 69 6e 74   The right point
180a9 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20  er of the child 
180aa 70 61 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d 65  page pOld become
180ab 73 20 74 68 65 20 6c 65 66 74 0a 20 20 20 20 20  s the left.     
180ac 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66     ** pointer of
180ad 20 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c   the divider cel
180ae 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 6d 65 6d  l */.        mem
180af 63 70 79 28 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c  cpy(apCell[nCell
180b0 5d 2c 20 26 70 4f 6c 64 2d 3e 61 44 61 74 61 5b  ], &pOld->aData[
180b1 38 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 7d 65  8], 4);.      }e
180b2 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
180b3 65 72 74 28 20 6c 65 61 66 43 6f 72 72 65 63 74  ert( leafCorrect
180b4 69 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20  ion==4 );.      
180b5 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6e 43 65    if( szCell[nCe
180b6 6c 6c 5d 3c 34 20 29 7b 0a 20 20 20 20 20 20 20  ll]<4 ){.       
180b7 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c     /* Do not all
180b8 6f 77 20 61 6e 79 20 63 65 6c 6c 73 20 73 6d 61  ow any cells sma
180b9 6c 6c 65 72 20 74 68 61 6e 20 34 20 62 79 74 65  ller than 4 byte
180ba 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  s. */.          
180bb 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20  szCell[nCell] = 
180bc 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  4;.        }.   
180bd 20 20 20 7d 0a 20 20 20 20 20 20 6e 43 65 6c 6c     }.      nCell
180be 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ++;.    }.  }.. 
180bf 20 2f 2a 0a 20 20 2a 2a 20 46 69 67 75 72 65 20   /*.  ** Figure 
180c0 6f 75 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  out the number o
180c1 66 20 70 61 67 65 73 20 6e 65 65 64 65 64 20 74  f pages needed t
180c2 6f 20 68 6f 6c 64 20 61 6c 6c 20 6e 43 65 6c 6c  o hold all nCell
180c3 20 63 65 6c 6c 73 2e 0a 20 20 2a 2a 20 53 74 6f   cells..  ** Sto
180c4 72 65 20 74 68 69 73 20 6e 75 6d 62 65 72 20 69  re this number i
180c5 6e 20 22 6b 22 2e 20 20 41 6c 73 6f 20 63 6f 6d  n "k".  Also com
180c6 70 75 74 65 20 73 7a 4e 65 77 5b 5d 20 77 68 69  pute szNew[] whi
180c7 63 68 20 69 73 20 74 68 65 20 74 6f 74 61 6c 0a  ch is the total.
180c8 20 20 2a 2a 20 73 69 7a 65 20 6f 66 20 61 6c 6c    ** size of all
180c9 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 69 2d   cells on the i-
180ca 74 68 20 70 61 67 65 20 61 6e 64 20 63 6e 74 4e  th page and cntN
180cb 65 77 5b 5d 20 77 68 69 63 68 20 69 73 20 74 68  ew[] which is th
180cc 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20  e index.  ** in 
180cd 61 70 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20  apCell[] of the 
180ce 63 65 6c 6c 20 74 68 61 74 20 64 69 76 69 64 65  cell that divide
180cf 73 20 70 61 67 65 20 69 20 66 72 6f 6d 20 70 61  s page i from pa
180d0 67 65 20 69 2b 31 2e 20 20 0a 20 20 2a 2a 20 63  ge i+1.  .  ** c
180d1 6e 74 4e 65 77 5b 6b 5d 20 73 68 6f 75 6c 64 20  ntNew[k] should 
180d2 65 71 75 61 6c 20 6e 43 65 6c 6c 2e 0a 20 20 2a  equal nCell..  *
180d3 2a 0a 20 20 2a 2a 20 56 61 6c 75 65 73 20 63 6f  *.  ** Values co
180d4 6d 70 75 74 65 64 20 62 79 20 74 68 69 73 20 62  mputed by this b
180d5 6c 6f 63 6b 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  lock:.  **.  ** 
180d6 20 20 20 20 20 20 20 20 20 20 6b 3a 20 54 68 65            k: The
180d7 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
180d8 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 0a 20   sibling pages. 
180d9 20 2a 2a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 3a   **    szNew[i]:
180da 20 53 70 61 63 65 64 20 75 73 65 64 20 6f 6e 20   Spaced used on 
180db 74 68 65 20 69 2d 74 68 20 73 69 62 6c 69 6e 67  the i-th sibling
180dc 20 70 61 67 65 2e 0a 20 20 2a 2a 20 20 20 63 6e   page..  **   cn
180dd 74 4e 65 77 5b 69 5d 3a 20 49 6e 64 65 78 20 69  tNew[i]: Index i
180de 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 6e 64 20 73  n apCell[] and s
180df 7a 43 65 6c 6c 5b 5d 20 66 6f 72 20 74 68 65 20  zCell[] for the 
180e0 66 69 72 73 74 20 63 65 6c 6c 20 74 6f 0a 20 20  first cell to.  
180e1 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
180e2 74 68 65 20 72 69 67 68 74 20 6f 66 20 74 68 65  the right of the
180e3 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61   i-th sibling pa
180e4 67 65 2e 0a 20 20 2a 2a 20 75 73 61 62 6c 65 53  ge..  ** usableS
180e5 70 61 63 65 3a 20 4e 75 6d 62 65 72 20 6f 66 20  pace: Number of 
180e6 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61  bytes of space a
180e7 76 61 69 6c 61 62 6c 65 20 6f 6e 20 65 61 63 68  vailable on each
180e8 20 73 69 62 6c 69 6e 67 2e 0a 20 20 2a 2a 20 0a   sibling..  ** .
180e9 20 20 2a 2f 0a 20 20 75 73 61 62 6c 65 53 70 61    */.  usableSpa
180ea 63 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  ce = pBt->usable
180eb 53 69 7a 65 20 2d 20 31 32 20 2b 20 6c 65 61 66  Size - 12 + leaf
180ec 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 66 6f  Correction;.  fo
180ed 72 28 73 75 62 74 6f 74 61 6c 3d 6b 3d 69 3d 30  r(subtotal=k=i=0
180ee 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b  ; i<nCell; i++){
180ef 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 3c 6e  .    assert( i<n
180f0 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20  MaxCells );.    
180f1 73 75 62 74 6f 74 61 6c 20 2b 3d 20 73 7a 43 65  subtotal += szCe
180f2 6c 6c 5b 69 5d 20 2b 20 32 3b 0a 20 20 20 20 69  ll[i] + 2;.    i
180f3 66 28 20 73 75 62 74 6f 74 61 6c 20 3e 20 75 73  f( subtotal > us
180f4 61 62 6c 65 53 70 61 63 65 20 29 7b 0a 20 20 20  ableSpace ){.   
180f5 20 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75     szNew[k] = su
180f6 62 74 6f 74 61 6c 20 2d 20 73 7a 43 65 6c 6c 5b  btotal - szCell[
180f7 69 5d 3b 0a 20 20 20 20 20 20 63 6e 74 4e 65 77  i];.      cntNew
180f8 5b 6b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 69  [k] = i;.      i
180f9 66 28 20 6c 65 61 66 44 61 74 61 20 29 7b 20 69  f( leafData ){ i
180fa 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 73 75 62 74  --; }.      subt
180fb 6f 74 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  otal = 0;.      
180fc 6b 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 6b  k++;.      if( k
180fd 3e 4e 42 2b 31 20 29 7b 20 72 63 20 3d 20 53 51  >NB+1 ){ rc = SQ
180fe 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 20 67 6f  LITE_CORRUPT; go
180ff 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
18100 75 70 3b 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a  up; }.    }.  }.
18101 20 20 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62    szNew[k] = sub
18102 74 6f 74 61 6c 3b 0a 20 20 63 6e 74 4e 65 77 5b  total;.  cntNew[
18103 6b 5d 20 3d 20 6e 43 65 6c 6c 3b 0a 20 20 6b 2b  k] = nCell;.  k+
18104 2b 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  +;..  /*.  ** Th
18105 65 20 70 61 63 6b 69 6e 67 20 63 6f 6d 70 75 74  e packing comput
18106 65 64 20 62 79 20 74 68 65 20 70 72 65 76 69 6f  ed by the previo
18107 75 73 20 62 6c 6f 63 6b 20 69 73 20 62 69 61 73  us block is bias
18108 65 64 20 74 6f 77 61 72 64 20 74 68 65 20 73 69  ed toward the si
18109 62 6c 69 6e 67 73 0a 20 20 2a 2a 20 6f 6e 20 74  blings.  ** on t
1810a 68 65 20 6c 65 66 74 20 73 69 64 65 2e 20 20 54  he left side.  T
1810b 68 65 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 73  he left siblings
1810c 20 61 72 65 20 61 6c 77 61 79 73 20 6e 65 61 72   are always near
1810d 6c 79 20 66 75 6c 6c 2c 20 77 68 69 6c 65 20 74  ly full, while t
1810e 68 65 0a 20 20 2a 2a 20 72 69 67 68 74 2d 6d 6f  he.  ** right-mo
1810f 73 74 20 73 69 62 6c 69 6e 67 20 6d 69 67 68 74  st sibling might
18110 20 62 65 20 6e 65 61 72 6c 79 20 65 6d 70 74 79   be nearly empty
18111 2e 20 20 54 68 69 73 20 62 6c 6f 63 6b 20 6f 66  .  This block of
18112 20 63 6f 64 65 20 61 74 74 65 6d 70 74 73 0a 20   code attempts. 
18113 20 2a 2a 20 74 6f 20 61 64 6a 75 73 74 20 74 68   ** to adjust th
18114 65 20 70 61 63 6b 69 6e 67 20 6f 66 20 73 69 62  e packing of sib
18115 6c 69 6e 67 73 20 74 6f 20 67 65 74 20 61 20 62  lings to get a b
18116 65 74 74 65 72 20 62 61 6c 61 6e 63 65 2e 0a 20  etter balance.. 
18117 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 61 64   **.  ** This ad
18118 6a 75 73 74 6d 65 6e 74 20 69 73 20 6d 6f 72 65  justment is more
18119 20 74 68 61 6e 20 61 6e 20 6f 70 74 69 6d 69 7a   than an optimiz
1811a 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 61 63 6b  ation.  The pack
1811b 69 6e 67 20 61 62 6f 76 65 20 6d 69 67 68 74 0a  ing above might.
1811c 20 20 2a 2a 20 62 65 20 73 6f 20 6f 75 74 20 6f    ** be so out o
1811d 66 20 62 61 6c 61 6e 63 65 20 61 73 20 74 6f 20  f balance as to 
1811e 62 65 20 69 6c 6c 65 67 61 6c 2e 20 20 46 6f 72  be illegal.  For
1811f 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 72 69   example, the ri
18120 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a 20 73 69  ght-most.  ** si
18121 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20 63  bling might be c
18122 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e  ompletely empty.
18123 20 20 54 68 69 73 20 61 64 6a 75 73 74 6d 65 6e    This adjustmen
18124 74 20 69 73 20 6e 6f 74 20 6f 70 74 69 6f 6e 61  t is not optiona
18125 6c 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  l..  */.  for(i=
18126 6b 2d 31 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a  k-1; i>0; i--){.
18127 20 20 20 20 69 6e 74 20 73 7a 52 69 67 68 74 20      int szRight 
18128 3d 20 73 7a 4e 65 77 5b 69 5d 3b 20 20 2f 2a 20  = szNew[i];  /* 
18129 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 6e 67 20  Size of sibling 
1812a 6f 6e 20 74 68 65 20 72 69 67 68 74 20 2a 2f 0a  on the right */.
1812b 20 20 20 20 69 6e 74 20 73 7a 4c 65 66 74 20 3d      int szLeft =
1812c 20 73 7a 4e 65 77 5b 69 2d 31 5d 3b 20 2f 2a 20   szNew[i-1]; /* 
1812d 53 69 7a 65 20 6f 66 20 73 69 62 6c 69 6e 67 20  Size of sibling 
1812e 6f 6e 20 74 68 65 20 6c 65 66 74 20 2a 2f 0a 20  on the left */. 
1812f 20 20 20 69 6e 74 20 72 3b 20 20 20 20 20 20 20     int r;       
18130 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
18131 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 65  of right-most ce
18132 6c 6c 20 69 6e 20 6c 65 66 74 20 73 69 62 6c 69  ll in left sibli
18133 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20 64 3b  ng */.    int d;
18134 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18135 20 49 6e 64 65 78 20 6f 66 20 66 69 72 73 74 20   Index of first 
18136 63 65 6c 6c 20 74 6f 20 74 68 65 20 6c 65 66 74  cell to the left
18137 20 6f 66 20 72 69 67 68 74 20 73 69 62 6c 69 6e   of right siblin
18138 67 20 2a 2f 0a 0a 20 20 20 20 72 20 3d 20 63 6e  g */..    r = cn
18139 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20  tNew[i-1] - 1;. 
1813a 20 20 20 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c     d = r + 1 - l
1813b 65 61 66 44 61 74 61 3b 0a 20 20 20 20 61 73 73  eafData;.    ass
1813c 65 72 74 28 20 64 3c 6e 4d 61 78 43 65 6c 6c 73  ert( d<nMaxCells
1813d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1813e 72 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20  r<nMaxCells );. 
1813f 20 20 20 77 68 69 6c 65 28 20 73 7a 52 69 67 68     while( szRigh
18140 74 3d 3d 30 20 7c 7c 20 73 7a 52 69 67 68 74 2b  t==0 || szRight+
18141 73 7a 43 65 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c  szCell[d]+2<=szL
18142 65 66 74 2d 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32  eft-(szCell[r]+2
18143 29 20 29 7b 0a 20 20 20 20 20 20 73 7a 52 69 67  ) ){.      szRig
18144 68 74 20 2b 3d 20 73 7a 43 65 6c 6c 5b 64 5d 20  ht += szCell[d] 
18145 2b 20 32 3b 0a 20 20 20 20 20 20 73 7a 4c 65 66  + 2;.      szLef
18146 74 20 2d 3d 20 73 7a 43 65 6c 6c 5b 72 5d 20 2b  t -= szCell[r] +
18147 20 32 3b 0a 20 20 20 20 20 20 63 6e 74 4e 65 77   2;.      cntNew
18148 5b 69 2d 31 5d 2d 2d 3b 0a 20 20 20 20 20 20 72  [i-1]--;.      r
18149 20 3d 20 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d   = cntNew[i-1] -
1814a 20 31 3b 0a 20 20 20 20 20 20 64 20 3d 20 72 20   1;.      d = r 
1814b 2b 20 31 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a  + 1 - leafData;.
1814c 20 20 20 20 7d 0a 20 20 20 20 73 7a 4e 65 77 5b      }.    szNew[
1814d 69 5d 20 3d 20 73 7a 52 69 67 68 74 3b 0a 20 20  i] = szRight;.  
1814e 20 20 73 7a 4e 65 77 5b 69 2d 31 5d 20 3d 20 73    szNew[i-1] = s
1814f 7a 4c 65 66 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  zLeft;.  }..  /*
18150 20 45 69 74 68 65 72 20 77 65 20 66 6f 75 6e 64   Either we found
18151 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63 65 6c   one or more cel
18152 6c 73 20 28 63 6e 74 6e 65 77 5b 30 5d 29 3e 30  ls (cntnew[0])>0
18153 29 20 6f 72 20 70 50 61 67 65 20 69 73 0a 20 20  ) or pPage is.  
18154 2a 2a 20 61 20 76 69 72 74 75 61 6c 20 72 6f 6f  ** a virtual roo
18155 74 20 70 61 67 65 2e 20 20 41 20 76 69 72 74 75  t page.  A virtu
18156 61 6c 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20  al root page is 
18157 77 68 65 6e 20 74 68 65 20 72 65 61 6c 20 72 6f  when the real ro
18158 6f 74 0a 20 20 2a 2a 20 70 61 67 65 20 69 73 20  ot.  ** page is 
18159 70 61 67 65 20 31 20 61 6e 64 20 77 65 20 61 72  page 1 and we ar
1815a 65 20 74 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64  e the only child
1815b 20 6f 66 20 74 68 61 74 20 70 61 67 65 2e 0a 20   of that page.. 
1815c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 63 6e   */.  assert( cn
1815d 74 4e 65 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50  tNew[0]>0 || (pP
1815e 61 72 65 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26  arent->pgno==1 &
1815f 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  & pParent->nCell
18160 3d 3d 30 29 20 29 3b 0a 0a 20 20 54 52 41 43 45  ==0) );..  TRACE
18161 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f 6c 64 3a  (("BALANCE: old:
18162 20 25 64 20 25 64 20 25 64 20 20 22 2c 0a 20 20   %d %d %d  ",.  
18163 20 20 61 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f    apOld[0]->pgno
18164 2c 20 0a 20 20 20 20 6e 4f 6c 64 3e 3d 32 20 3f  , .    nOld>=2 ?
18165 20 61 70 4f 6c 64 5b 31 5d 2d 3e 70 67 6e 6f 20   apOld[1]->pgno 
18166 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c 64 3e 3d 33  : 0,.    nOld>=3
18167 20 3f 20 61 70 4f 6c 64 5b 32 5d 2d 3e 70 67 6e   ? apOld[2]->pgn
18168 6f 20 3a 20 30 0a 20 20 29 29 3b 0a 0a 20 20 2f  o : 0.  ));..  /
18169 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  *.  ** Allocate 
1816a 6b 20 6e 65 77 20 70 61 67 65 73 2e 20 20 52 65  k new pages.  Re
1816b 75 73 65 20 6f 6c 64 20 70 61 67 65 73 20 77 68  use old pages wh
1816c 65 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20  ere possible..  
1816d 2a 2f 0a 20 20 69 66 28 20 61 70 4f 6c 64 5b 30  */.  if( apOld[0
1816e 5d 2d 3e 70 67 6e 6f 3c 3d 31 20 29 7b 0a 20 20  ]->pgno<=1 ){.  
1816f 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
18170 52 52 55 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20  RRUPT;.    goto 
18171 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b  balance_cleanup;
18172 0a 20 20 7d 0a 20 20 70 61 67 65 46 6c 61 67 73  .  }.  pageFlags
18173 20 3d 20 61 70 4f 6c 64 5b 30 5d 2d 3e 61 44 61   = apOld[0]->aDa
18174 74 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30  ta[0];.  for(i=0
18175 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20  ; i<k; i++){.   
18176 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a   MemPage *pNew;.
18177 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29      if( i<nOld )
18178 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61  {.      pNew = a
18179 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b  pNew[i] = apOld[
1817a 69 5d 3b 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b  i];.      apOld[
1817b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63  i] = 0;.      rc
1817c 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
1817d 72 69 74 65 28 70 4e 65 77 2d 3e 70 44 62 50 61  rite(pNew->pDbPa
1817e 67 65 29 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b  ge);.      nNew+
1817f 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  +;.      if( rc 
18180 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63  ) goto balance_c
18181 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73  leanup;.    }els
18182 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e{.      assert(
18183 20 69 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 63   i>0 );.      rc
18184 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
18185 50 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c  Page(pBt, &pNew,
18186 20 26 70 67 6e 6f 2c 20 70 67 6e 6f 2c 20 30 29   &pgno, pgno, 0)
18187 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
18188 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
18189 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 61 70 4e  eanup;.      apN
1818a 65 77 5b 69 5d 20 3d 20 70 4e 65 77 3b 0a 20 20  ew[i] = pNew;.  
1818b 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a 0a 20 20 20      nNew++;..   
1818c 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 70 6f     /* Set the po
1818d 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20  inter-map entry 
1818e 66 6f 72 20 74 68 65 20 6e 65 77 20 73 69 62 6c  for the new sibl
1818f 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20  ing page. */.   
18190 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
18191 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 70  UUM ){.        p
18192 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 4e  trmapPut(pBt, pN
18193 65 77 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50  ew->pgno, PTRMAP
18194 5f 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d  _BTREE, pParent-
18195 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20  >pgno, &rc);.   
18196 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
18197 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18198 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65      goto balance
18199 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20  _cleanup;.      
1819a 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1819b 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65  }.  }..  /* Free
1819c 20 61 6e 79 20 6f 6c 64 20 70 61 67 65 73 20 74   any old pages t
1819d 68 61 74 20 77 65 72 65 20 6e 6f 74 20 72 65 75  hat were not reu
1819e 73 65 64 20 61 73 20 6e 65 77 20 70 61 67 65 73  sed as new pages
1819f 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  ..  */.  while( 
181a0 69 3c 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 66 72  i<nOld ){.    fr
181a1 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 2c  eePage(apOld[i],
181a2 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72   &rc);.    if( r
181a3 63 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65  c ) goto balance
181a4 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 72 65  _cleanup;.    re
181a5 6c 65 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b  leasePage(apOld[
181a6 69 5d 29 3b 0a 20 20 20 20 61 70 4f 6c 64 5b 69  i]);.    apOld[i
181a7 5d 20 3d 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a  ] = 0;.    i++;.
181a8 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50    }..  /*.  ** P
181a9 75 74 20 74 68 65 20 6e 65 77 20 70 61 67 65 73  ut the new pages
181aa 20 69 6e 20 61 63 63 65 6e 64 69 6e 67 20 6f 72   in accending or
181ab 64 65 72 2e 20 20 54 68 69 73 20 68 65 6c 70 73  der.  This helps
181ac 20 74 6f 0a 20 20 2a 2a 20 6b 65 65 70 20 65 6e   to.  ** keep en
181ad 74 72 69 65 73 20 69 6e 20 74 68 65 20 64 69 73  tries in the dis
181ae 6b 20 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20  k file in order 
181af 73 6f 20 74 68 61 74 20 61 20 73 63 61 6e 0a 20  so that a scan. 
181b0 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65   ** of the table
181b1 20 69 73 20 61 20 6c 69 6e 65 61 72 20 73 63 61   is a linear sca
181b2 6e 20 74 68 72 6f 75 67 68 20 74 68 65 20 66 69  n through the fi
181b3 6c 65 2e 20 20 54 68 61 74 0a 20 20 2a 2a 20 69  le.  That.  ** i
181b4 6e 20 74 75 72 6e 20 68 65 6c 70 73 20 74 68 65  n turn helps the
181b5 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
181b6 6d 20 74 6f 20 64 65 6c 69 76 65 72 20 70 61 67  m to deliver pag
181b7 65 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65  es.  ** from the
181b8 20 64 69 73 6b 20 6d 6f 72 65 20 72 61 70 69 64   disk more rapid
181b9 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e  ly..  **.  ** An
181ba 20 4f 28 6e 5e 32 29 20 69 6e 73 65 72 74 69 6f   O(n^2) insertio
181bb 6e 20 73 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d  n sort algorithm
181bc 20 69 73 20 75 73 65 64 2c 20 62 75 74 20 73 69   is used, but si
181bd 6e 63 65 0a 20 20 2a 2a 20 6e 20 69 73 20 6e 65  nce.  ** n is ne
181be 76 65 72 20 6d 6f 72 65 20 74 68 61 6e 20 4e 42  ver more than NB
181bf 20 28 61 20 73 6d 61 6c 6c 20 63 6f 6e 73 74 61   (a small consta
181c0 6e 74 29 2c 20 74 68 61 74 20 73 68 6f 75 6c 64  nt), that should
181c1 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 61 20 70  .  ** not be a p
181c2 72 6f 62 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a  roblem..  **.  *
181c3 2a 20 57 68 65 6e 20 4e 42 3d 3d 33 2c 20 74 68  * When NB==3, th
181c4 69 73 20 6f 6e 65 20 6f 70 74 69 6d 69 7a 61 74  is one optimizat
181c5 69 6f 6e 20 6d 61 6b 65 73 20 74 68 65 20 64 61  ion makes the da
181c6 74 61 62 61 73 65 0a 20 20 2a 2a 20 61 62 6f 75  tabase.  ** abou
181c7 74 20 32 35 25 20 66 61 73 74 65 72 20 66 6f 72  t 25% faster for
181c8 20 6c 61 72 67 65 20 69 6e 73 65 72 74 69 6f 6e   large insertion
181c9 73 20 61 6e 64 20 64 65 6c 65 74 69 6f 6e 73 2e  s and deletions.
181ca 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  .  */.  for(i=0;
181cb 20 69 3c 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20   i<k-1; i++){.  
181cc 20 20 69 6e 74 20 6d 69 6e 56 20 3d 20 61 70 4e    int minV = apN
181cd 65 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20  ew[i]->pgno;.   
181ce 20 69 6e 74 20 6d 69 6e 49 20 3d 20 69 3b 0a 20   int minI = i;. 
181cf 20 20 20 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a 3c     for(j=i+1; j<
181d0 6b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69  k; j++){.      i
181d1 66 28 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e  f( apNew[j]->pgn
181d2 6f 3c 28 75 6e 73 69 67 6e 65 64 29 6d 69 6e 56  o<(unsigned)minV
181d3 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 69 6e 49   ){.        minI
181d4 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 6d 69   = j;.        mi
181d5 6e 56 20 3d 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70  nV = apNew[j]->p
181d6 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  gno;.      }.   
181d7 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e 49 3e   }.    if( minI>
181d8 69 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 74  i ){.      int t
181d9 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20  ;.      MemPage 
181da 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 3d 20 61  *pT;.      t = a
181db 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0a 20  pNew[i]->pgno;. 
181dc 20 20 20 20 20 70 54 20 3d 20 61 70 4e 65 77 5b       pT = apNew[
181dd 69 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b  i];.      apNew[
181de 69 5d 20 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d  i] = apNew[minI]
181df 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 6d 69  ;.      apNew[mi
181e0 6e 49 5d 20 3d 20 70 54 3b 0a 20 20 20 20 7d 0a  nI] = pT;.    }.
181e1 20 20 7d 0a 20 20 54 52 41 43 45 28 28 22 6e 65    }.  TRACE(("ne
181e2 77 3a 20 25 64 28 25 64 29 20 25 64 28 25 64 29  w: %d(%d) %d(%d)
181e3 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25   %d(%d) %d(%d) %
181e4 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 61 70  d(%d)\n",.    ap
181e5 4e 65 77 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 73 7a  New[0]->pgno, sz
181e6 4e 65 77 5b 30 5d 2c 0a 20 20 20 20 6e 4e 65 77  New[0],.    nNew
181e7 3e 3d 32 20 3f 20 61 70 4e 65 77 5b 31 5d 2d 3e  >=2 ? apNew[1]->
181e8 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d  pgno : 0, nNew>=
181e9 32 20 3f 20 73 7a 4e 65 77 5b 31 5d 20 3a 20 30  2 ? szNew[1] : 0
181ea 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 33 20 3f 20  ,.    nNew>=3 ? 
181eb 61 70 4e 65 77 5b 32 5d 2d 3e 70 67 6e 6f 20 3a  apNew[2]->pgno :
181ec 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 7a   0, nNew>=3 ? sz
181ed 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 20  New[2] : 0,.    
181ee 6e 4e 65 77 3e 3d 34 20 3f 20 61 70 4e 65 77 5b  nNew>=4 ? apNew[
181ef 33 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e  3]->pgno : 0, nN
181f0 65 77 3e 3d 34 20 3f 20 73 7a 4e 65 77 5b 33 5d  ew>=4 ? szNew[3]
181f1 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d   : 0,.    nNew>=
181f2 35 20 3f 20 61 70 4e 65 77 5b 34 5d 2d 3e 70 67  5 ? apNew[4]->pg
181f3 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 35 20  no : 0, nNew>=5 
181f4 3f 20 73 7a 4e 65 77 5b 34 5d 20 3a 20 30 29 29  ? szNew[4] : 0))
181f5 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
181f6 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
181f7 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e 70 44  able(pParent->pD
181f8 62 50 61 67 65 29 20 29 3b 0a 20 20 70 75 74 34  bPage) );.  put4
181f9 62 79 74 65 28 70 52 69 67 68 74 2c 20 61 70 4e  byte(pRight, apN
181fa 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 70 67 6e 6f  ew[nNew-1]->pgno
181fb 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 45 76  );..  /*.  ** Ev
181fc 65 6e 6c 79 20 64 69 73 74 72 69 62 75 74 65 20  enly distribute 
181fd 74 68 65 20 64 61 74 61 20 69 6e 20 61 70 43 65  the data in apCe
181fe 6c 6c 5b 5d 20 61 63 72 6f 73 73 20 74 68 65 20  ll[] across the 
181ff 6e 65 77 20 70 61 67 65 73 2e 0a 20 20 2a 2a 20  new pages..  ** 
18200 49 6e 73 65 72 74 20 64 69 76 69 64 65 72 20 63  Insert divider c
18201 65 6c 6c 73 20 69 6e 74 6f 20 70 50 61 72 65 6e  ells into pParen
18202 74 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a  t as necessary..
18203 20 20 2a 2f 0a 20 20 6a 20 3d 20 30 3b 0a 20 20    */.  j = 0;.  
18204 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b  for(i=0; i<nNew;
18205 20 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 41 73   i++){.    /* As
18206 73 65 6d 62 6c 65 20 74 68 65 20 6e 65 77 20 73  semble the new s
18207 69 62 6c 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a  ibling page. */.
18208 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65      MemPage *pNe
18209 77 20 3d 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20  w = apNew[i];.  
1820a 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78    assert( j<nMax
1820b 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 7a 65 72  Cells );.    zer
1820c 6f 50 61 67 65 28 70 4e 65 77 2c 20 70 61 67 65  oPage(pNew, page
1820d 46 6c 61 67 73 29 3b 0a 20 20 20 20 61 73 73 65  Flags);.    asse
1820e 6d 62 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 63  mblePage(pNew, c
1820f 6e 74 4e 65 77 5b 69 5d 2d 6a 2c 20 26 61 70 43  ntNew[i]-j, &apC
18210 65 6c 6c 5b 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b  ell[j], &szCell[
18211 6a 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  j]);.    assert(
18212 20 70 4e 65 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c   pNew->nCell>0 |
18213 7c 20 28 6e 4e 65 77 3d 3d 31 20 26 26 20 63 6e  | (nNew==1 && cn
18214 74 4e 65 77 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20  tNew[0]==0) );. 
18215 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d     assert( pNew-
18216 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b  >nOverflow==0 );
18217 0a 0a 20 20 20 20 6a 20 3d 20 63 6e 74 4e 65 77  ..    j = cntNew
18218 5b 69 5d 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  [i];..    /* If 
18219 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65  the sibling page
1821a 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f 76 65   assembled above
1821b 20 77 61 73 20 6e 6f 74 20 74 68 65 20 72 69 67   was not the rig
1821c 68 74 2d 6d 6f 73 74 20 73 69 62 6c 69 6e 67 2c  ht-most sibling,
1821d 0a 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 20 61  .    ** insert a
1821e 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e   divider cell in
1821f 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  to the parent pa
18220 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  ge..    */.    a
18221 73 73 65 72 74 28 20 69 3c 6e 4e 65 77 2d 31 20  ssert( i<nNew-1 
18222 7c 7c 20 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20  || j==nCell );. 
18223 20 20 20 69 66 28 20 6a 3c 6e 43 65 6c 6c 20 29     if( j<nCell )
18224 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43 65 6c  {.      u8 *pCel
18225 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 54 65  l;.      u8 *pTe
18226 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a  mp;.      int sz
18227 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
18228 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a   j<nMaxCells );.
18229 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20 61 70        pCell = ap
1822a 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 73  Cell[j];.      s
1822b 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d 20 2b 20  z = szCell[j] + 
1822c 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a  leafCorrection;.
1822d 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 26 61        pTemp = &a
1822e 4f 76 66 6c 53 70 61 63 65 5b 69 4f 76 66 6c 53  OvflSpace[iOvflS
1822f 70 61 63 65 5d 3b 0a 20 20 20 20 20 20 69 66 28  pace];.      if(
18230 20 21 70 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a   !pNew->leaf ){.
18231 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
18232 70 4e 65 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20  pNew->aData[8], 
18233 70 43 65 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20  pCell, 4);.     
18234 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 61 66 44   }else if( leafD
18235 61 74 61 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ata ){.        /
18236 2a 20 49 66 20 74 68 65 20 74 72 65 65 20 69 73  * If the tree is
18237 20 61 20 6c 65 61 66 2d 64 61 74 61 20 74 72 65   a leaf-data tre
18238 65 2c 20 61 6e 64 20 74 68 65 20 73 69 62 6c 69  e, and the sibli
18239 6e 67 73 20 61 72 65 20 6c 65 61 76 65 73 2c 20  ngs are leaves, 
1823a 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  .        ** then
1823b 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69 76   there is no div
1823c 69 64 65 72 20 63 65 6c 6c 20 69 6e 20 61 70 43  ider cell in apC
1823d 65 6c 6c 5b 5d 2e 20 49 6e 73 74 65 61 64 2c 20  ell[]. Instead, 
1823e 74 68 65 20 64 69 76 69 64 65 72 20 0a 20 20 20  the divider .   
1823f 20 20 20 20 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e       ** cell con
18240 73 69 73 74 73 20 6f 66 20 74 68 65 20 69 6e 74  sists of the int
18241 65 67 65 72 20 6b 65 79 20 66 6f 72 20 74 68 65  eger key for the
18242 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c   right-most cell
18243 20 6f 66 20 0a 20 20 20 20 20 20 20 20 2a 2a 20   of .        ** 
18244 74 68 65 20 73 69 62 6c 69 6e 67 2d 70 61 67 65  the sibling-page
18245 20 61 73 73 65 6d 62 6c 65 64 20 61 62 6f 76 65   assembled above
18246 20 6f 6e 6c 79 2e 0a 20 20 20 20 20 20 20 20 2a   only..        *
18247 2f 0a 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e  /.        CellIn
18248 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20  fo info;.       
18249 20 6a 2d 2d 3b 0a 20 20 20 20 20 20 20 20 62 74   j--;.        bt
1824a 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
1824b 70 4e 65 77 2c 20 61 70 43 65 6c 6c 5b 6a 5d 2c  pNew, apCell[j],
1824c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20   &info);.       
1824d 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a   pCell = pTemp;.
1824e 20 20 20 20 20 20 20 20 73 7a 20 3d 20 34 20 2b          sz = 4 +
1824f 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c   putVarint(&pCel
18250 6c 5b 34 5d 2c 20 69 6e 66 6f 2e 6e 4b 65 79 29  l[4], info.nKey)
18251 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70 20  ;.        pTemp 
18252 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
18253 7b 0a 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20  {.        pCell 
18254 2d 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 2f 2a  -= 4;.        /*
18255 20 4f 62 73 63 75 72 65 20 63 61 73 65 20 66 6f   Obscure case fo
18256 72 20 6e 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 20  r non-leaf-data 
18257 74 72 65 65 73 3a 20 49 66 20 74 68 65 20 63 65  trees: If the ce
18258 6c 6c 20 61 74 20 70 43 65 6c 6c 20 77 61 73 0a  ll at pCell was.
18259 20 20 20 20 20 20 20 20 2a 2a 20 70 72 65 76 69          ** previ
1825a 6f 75 73 6c 79 20 73 74 6f 72 65 64 20 6f 6e 20  ously stored on 
1825b 61 20 6c 65 61 66 20 6e 6f 64 65 2c 20 61 6e 64  a leaf node, and
1825c 20 69 74 73 20 72 65 70 6f 72 74 65 64 20 73 69   its reported si
1825d 7a 65 20 77 61 73 20 34 0a 20 20 20 20 20 20 20  ze was 4.       
1825e 20 2a 2a 20 62 79 74 65 73 2c 20 74 68 65 6e 20   ** bytes, then 
1825f 69 74 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20  it may actually 
18260 62 65 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  be smaller than 
18261 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 2a 2a  this .        **
18262 20 28 73 65 65 20 62 74 72 65 65 50 61 72 73 65   (see btreeParse
18263 43 65 6c 6c 50 74 72 28 29 2c 20 34 20 62 79 74  CellPtr(), 4 byt
18264 65 73 20 69 73 20 74 68 65 20 6d 69 6e 69 6d 75  es is the minimu
18265 6d 20 73 69 7a 65 20 6f 66 0a 20 20 20 20 20 20  m size of.      
18266 20 20 2a 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20    ** any cell). 
18267 42 75 74 20 69 74 20 69 73 20 69 6d 70 6f 72 74  But it is import
18268 61 6e 74 20 74 6f 20 70 61 73 73 20 74 68 65 20  ant to pass the 
18269 63 6f 72 72 65 63 74 20 73 69 7a 65 20 74 6f 20  correct size to 
1826a 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 65  .        ** inse
1826b 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20 72 65 70  rtCell(), so rep
1826c 61 72 73 65 20 74 68 65 20 63 65 6c 6c 20 6e 6f  arse the cell no
1826d 77 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  w..        **.  
1826e 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68        ** Note th
1826f 61 74 20 74 68 69 73 20 63 61 6e 20 6e 65 76 65  at this can neve
18270 72 20 68 61 70 70 65 6e 20 69 6e 20 61 6e 20 53  r happen in an S
18271 51 4c 69 74 65 20 64 61 74 61 20 66 69 6c 65 2c  QLite data file,
18272 20 61 73 20 61 6c 6c 0a 20 20 20 20 20 20 20 20   as all.        
18273 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 61 74 20  ** cells are at 
18274 6c 65 61 73 74 20 34 20 62 79 74 65 73 2e 20 49  least 4 bytes. I
18275 74 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69  t only happens i
18276 6e 20 62 2d 74 72 65 65 73 20 75 73 65 64 0a 20  n b-trees used. 
18277 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 65 76 61         ** to eva
18278 6c 75 61 74 65 20 22 49 4e 20 28 53 45 4c 45 43  luate "IN (SELEC
18279 54 20 2e 2e 2e 29 22 20 61 6e 64 20 73 69 6d 69  T ...)" and simi
1827a 6c 61 72 20 63 6c 61 75 73 65 73 2e 0a 20 20 20  lar clauses..   
1827b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1827c 69 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34  if( szCell[j]==4
1827d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73   ){.          as
1827e 73 65 72 74 28 6c 65 61 66 43 6f 72 72 65 63 74  sert(leafCorrect
1827f 69 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20 20 20 20  ion==4);.       
18280 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65     sz = cellSize
18281 50 74 72 28 70 50 61 72 65 6e 74 2c 20 70 43 65  Ptr(pParent, pCe
18282 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ll);.        }. 
18283 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 4f 76       }.      iOv
18284 66 6c 53 70 61 63 65 20 2b 3d 20 73 7a 3b 0a 20  flSpace += sz;. 
18285 20 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c       assert( sz<
18286 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34  =pBt->pageSize/4
18287 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
18288 28 20 69 4f 76 66 6c 53 70 61 63 65 3c 3d 70 42  ( iOvflSpace<=pB
18289 74 2d 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20  t->pageSize );. 
1828a 20 20 20 20 20 69 6e 73 65 72 74 43 65 6c 6c 28       insertCell(
1828b 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20  pParent, nxDiv, 
1828c 70 43 65 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70  pCell, sz, pTemp
1828d 2c 20 70 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 72  , pNew->pgno, &r
1828e 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  c);.      if( rc
1828f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
18290 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
18291 75 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  up;.      assert
18292 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
18293 77 72 69 74 65 61 62 6c 65 28 70 50 61 72 65 6e  writeable(pParen
18294 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a  t->pDbPage) );..
18295 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20        j++;.     
18296 20 6e 78 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a   nxDiv++;.    }.
18297 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6a 3d    }.  assert( j=
18298 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65  =nCell );.  asse
18299 72 74 28 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20 20  rt( nOld>0 );.  
1829a 61 73 73 65 72 74 28 20 6e 4e 65 77 3e 30 20 29  assert( nNew>0 )
1829b 3b 0a 20 20 69 66 28 20 28 70 61 67 65 46 6c 61  ;.  if( (pageFla
1829c 67 73 20 26 20 50 54 46 5f 4c 45 41 46 29 3d 3d  gs & PTF_LEAF)==
1829d 30 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 68  0 ){.    u8 *zCh
1829e 69 6c 64 20 3d 20 26 61 70 43 6f 70 79 5b 6e 4f  ild = &apCopy[nO
1829f 6c 64 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 3b  ld-1]->aData[8];
182a0 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 61 70 4e  .    memcpy(&apN
182a1 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61 74  ew[nNew-1]->aDat
182a2 61 5b 38 5d 2c 20 7a 43 68 69 6c 64 2c 20 34 29  a[8], zChild, 4)
182a3 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 52  ;.  }..  if( isR
182a4 6f 6f 74 20 26 26 20 70 50 61 72 65 6e 74 2d 3e  oot && pParent->
182a5 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 70 50 61 72  nCell==0 && pPar
182a6 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 3c 3d  ent->hdrOffset<=
182a7 61 70 4e 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20  apNew[0]->nFree 
182a8 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f  ){.    /* The ro
182a9 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62  ot page of the b
182aa 2d 74 72 65 65 20 6e 6f 77 20 63 6f 6e 74 61 69  -tree now contai
182ab 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 54 68 65  ns no cells. The
182ac 20 6f 6e 6c 79 20 73 69 62 6c 69 6e 67 0a 20 20   only sibling.  
182ad 20 20 2a 2a 20 70 61 67 65 20 69 73 20 74 68 65    ** page is the
182ae 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66 20   right-child of 
182af 74 68 65 20 70 61 72 65 6e 74 2e 20 43 6f 70 79  the parent. Copy
182b0 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
182b1 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 68 69 6c   the.    ** chil
182b2 64 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65 20  d page into the 
182b3 70 61 72 65 6e 74 2c 20 64 65 63 72 65 61 73 69  parent, decreasi
182b4 6e 67 20 74 68 65 20 6f 76 65 72 61 6c 6c 20 68  ng the overall h
182b5 65 69 67 68 74 20 6f 66 20 74 68 65 0a 20 20 20  eight of the.   
182b6 20 2a 2a 20 62 2d 74 72 65 65 20 73 74 72 75 63   ** b-tree struc
182b7 74 75 72 65 20 62 79 20 6f 6e 65 2e 20 54 68 69  ture by one. Thi
182b8 73 20 69 73 20 64 65 73 63 72 69 62 65 64 20 61  s is described a
182b9 73 20 74 68 65 20 22 62 61 6c 61 6e 63 65 2d 73  s the "balance-s
182ba 68 61 6c 6c 6f 77 65 72 22 0a 20 20 20 20 2a 2a  hallower".    **
182bb 20 73 75 62 2d 61 6c 67 6f 72 69 74 68 6d 20 69   sub-algorithm i
182bc 6e 20 73 6f 6d 65 20 64 6f 63 75 6d 65 6e 74 61  n some documenta
182bd 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  tion..    **.   
182be 20 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   ** If this is a
182bf 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61  n auto-vacuum da
182c0 74 61 62 61 73 65 2c 20 74 68 65 20 63 61 6c 6c  tabase, the call
182c1 20 74 6f 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74   to copyNodeCont
182c2 65 6e 74 28 29 20 0a 20 20 20 20 2a 2a 20 73 65  ent() .    ** se
182c3 74 73 20 61 6c 6c 20 70 6f 69 6e 74 65 72 2d 6d  ts all pointer-m
182c4 61 70 20 65 6e 74 72 69 65 73 20 63 6f 72 72 65  ap entries corre
182c5 73 70 6f 6e 64 69 6e 67 20 74 6f 20 64 61 74 61  sponding to data
182c6 62 61 73 65 20 69 6d 61 67 65 20 70 61 67 65 73  base image pages
182c7 20 0a 20 20 20 20 2a 2a 20 66 6f 72 20 77 68 69   .    ** for whi
182c8 63 68 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69  ch the pointer i
182c9 73 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20  s stored within 
182ca 74 68 65 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e  the content bein
182cb 67 20 63 6f 70 69 65 64 2e 0a 20 20 20 20 2a 2a  g copied..    **
182cc 0a 20 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f  .    ** The seco
182cd 6e 64 20 61 73 73 65 72 74 20 62 65 6c 6f 77 20  nd assert below 
182ce 76 65 72 69 66 69 65 73 20 74 68 61 74 20 74 68  verifies that th
182cf 65 20 63 68 69 6c 64 20 70 61 67 65 20 69 73 20  e child page is 
182d0 64 65 66 72 61 67 6d 65 6e 74 65 64 0a 20 20 20  defragmented.   
182d1 20 2a 2a 20 28 69 74 20 6d 75 73 74 20 62 65 2c   ** (it must be,
182d2 20 61 73 20 69 74 20 77 61 73 20 6a 75 73 74 20   as it was just 
182d3 72 65 63 6f 6e 73 74 72 75 63 74 65 64 20 75 73  reconstructed us
182d4 69 6e 67 20 61 73 73 65 6d 62 6c 65 50 61 67 65  ing assemblePage
182d5 28 29 29 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a  ()). This.    **
182d6 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 69 66   is important if
182d7 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
182d8 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 70   happens to be p
182d9 61 67 65 20 31 20 6f 66 20 74 68 65 20 64 61 74  age 1 of the dat
182da 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 69 6d 61  abase.    ** ima
182db 67 65 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  ge.  */.    asse
182dc 72 74 28 20 6e 4e 65 77 3d 3d 31 20 29 3b 0a 20  rt( nNew==1 );. 
182dd 20 20 20 61 73 73 65 72 74 28 20 61 70 4e 65 77     assert( apNew
182de 5b 30 5d 2d 3e 6e 46 72 65 65 20 3d 3d 20 0a 20  [0]->nFree == . 
182df 20 20 20 20 20 20 20 28 67 65 74 32 62 79 74 65         (get2byte
182e0 28 26 61 70 4e 65 77 5b 30 5d 2d 3e 61 44 61 74  (&apNew[0]->aDat
182e1 61 5b 35 5d 29 2d 61 70 4e 65 77 5b 30 5d 2d 3e  a[5])-apNew[0]->
182e2 63 65 6c 6c 4f 66 66 73 65 74 2d 61 70 4e 65 77  cellOffset-apNew
182e3 5b 30 5d 2d 3e 6e 43 65 6c 6c 2a 32 29 20 0a 20  [0]->nCell*2) . 
182e4 20 20 20 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f     );.    copyNo
182e5 64 65 43 6f 6e 74 65 6e 74 28 61 70 4e 65 77 5b  deContent(apNew[
182e6 30 5d 2c 20 70 50 61 72 65 6e 74 2c 20 26 72 63  0], pParent, &rc
182e7 29 3b 0a 20 20 20 20 66 72 65 65 50 61 67 65 28  );.    freePage(
182e8 61 70 4e 65 77 5b 30 5d 2c 20 26 72 63 29 3b 0a  apNew[0], &rc);.
182e9 20 20 7d 65 6c 73 65 20 69 66 28 20 49 53 41 55    }else if( ISAU
182ea 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20  TOVACUUM ){.    
182eb 2f 2a 20 46 69 78 20 74 68 65 20 70 6f 69 6e 74  /* Fix the point
182ec 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66  er-map entries f
182ed 6f 72 20 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73  or all the cells
182ee 20 74 68 61 74 20 77 65 72 65 20 73 68 69 66 74   that were shift
182ef 65 64 20 61 72 6f 75 6e 64 2e 20 0a 20 20 20 20  ed around. .    
182f0 2a 2a 20 54 68 65 72 65 20 61 72 65 20 73 65 76  ** There are sev
182f1 65 72 61 6c 20 64 69 66 66 65 72 65 6e 74 20 74  eral different t
182f2 79 70 65 73 20 6f 66 20 70 6f 69 6e 74 65 72 2d  ypes of pointer-
182f3 6d 61 70 20 65 6e 74 72 69 65 73 20 74 68 61 74  map entries that
182f4 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20   need to.    ** 
182f5 62 65 20 64 65 61 6c 74 20 77 69 74 68 20 62 79  be dealt with by
182f6 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 53   this routine. S
182f7 6f 6d 65 20 6f 66 20 74 68 65 73 65 20 68 61 76  ome of these hav
182f8 65 20 62 65 65 6e 20 73 65 74 20 61 6c 72 65 61  e been set alrea
182f9 64 79 2c 20 62 75 74 0a 20 20 20 20 2a 2a 20 6d  dy, but.    ** m
182fa 61 6e 79 20 68 61 76 65 20 6e 6f 74 2e 20 54 68  any have not. Th
182fb 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61  e following is a
182fc 20 73 75 6d 6d 61 72 79 3a 0a 20 20 20 20 2a 2a   summary:.    **
182fd 0a 20 20 20 20 2a 2a 20 20 20 31 29 20 54 68 65  .    **   1) The
182fe 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61   entries associa
182ff 74 65 64 20 77 69 74 68 20 6e 65 77 20 73 69 62  ted with new sib
18300 6c 69 6e 67 20 70 61 67 65 73 20 74 68 61 74 20  ling pages that 
18301 77 65 72 65 20 6e 6f 74 0a 20 20 20 20 2a 2a 20  were not.    ** 
18302 20 20 20 20 20 73 69 62 6c 69 6e 67 73 20 77 68       siblings wh
18303 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
18304 20 77 61 73 20 63 61 6c 6c 65 64 2e 20 54 68 65   was called. The
18305 73 65 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a  se have already.
18306 20 20 20 20 2a 2a 20 20 20 20 20 20 62 65 65 6e      **      been
18307 20 73 65 74 2e 20 57 65 20 64 6f 6e 27 74 20 6e   set. We don't n
18308 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f  eed to worry abo
18309 75 74 20 6f 6c 64 20 73 69 62 6c 69 6e 67 73 20  ut old siblings 
1830a 74 68 61 74 20 77 65 72 65 0a 20 20 20 20 2a 2a  that were.    **
1830b 20 20 20 20 20 20 6d 6f 76 65 64 20 74 6f 20 74        moved to t
1830c 68 65 20 66 72 65 65 2d 6c 69 73 74 20 2d 20 74  he free-list - t
1830d 68 65 20 66 72 65 65 50 61 67 65 28 29 20 63 6f  he freePage() co
1830e 64 65 20 68 61 73 20 74 61 6b 65 6e 20 63 61 72  de has taken car
1830f 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6f 66  e.    **      of
18310 20 74 68 6f 73 65 2e 0a 20 20 20 20 2a 2a 0a 20   those..    **. 
18311 20 20 20 2a 2a 20 20 20 32 29 20 54 68 65 20 70     **   2) The p
18312 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69  ointer-map entri
18313 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
18314 74 68 20 74 68 65 20 66 69 72 73 74 20 6f 76 65  th the first ove
18315 72 66 6c 6f 77 0a 20 20 20 20 2a 2a 20 20 20 20  rflow.    **    
18316 20 20 70 61 67 65 20 69 6e 20 61 6e 79 20 6f 76    page in any ov
18317 65 72 66 6c 6f 77 20 63 68 61 69 6e 73 20 75 73  erflow chains us
18318 65 64 20 62 79 20 6e 65 77 20 64 69 76 69 64 65  ed by new divide
18319 72 20 63 65 6c 6c 73 2e 20 54 68 65 73 65 20 0a  r cells. These .
1831a 20 20 20 20 2a 2a 20 20 20 20 20 20 68 61 76 65      **      have
1831b 20 61 6c 73 6f 20 61 6c 72 65 61 64 79 20 62 65   also already be
1831c 65 6e 20 74 61 6b 65 6e 20 63 61 72 65 20 6f 66  en taken care of
1831d 20 62 79 20 74 68 65 20 69 6e 73 65 72 74 43 65   by the insertCe
1831e 6c 6c 28 29 20 63 6f 64 65 2e 0a 20 20 20 20 2a  ll() code..    *
1831f 2a 0a 20 20 20 20 2a 2a 20 20 20 33 29 20 49 66  *.    **   3) If
18320 20 74 68 65 20 73 69 62 6c 69 6e 67 20 70 61 67   the sibling pag
18321 65 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65  es are not leave
18322 73 2c 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c  s, then the chil
18323 64 20 70 61 67 65 73 20 6f 66 0a 20 20 20 20 2a  d pages of.    *
18324 2a 20 20 20 20 20 20 63 65 6c 6c 73 20 73 74 6f  *      cells sto
18325 72 65 64 20 6f 6e 20 74 68 65 20 73 69 62 6c 69  red on the sibli
18326 6e 67 20 70 61 67 65 73 20 6d 61 79 20 6e 65 65  ng pages may nee
18327 64 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e  d to be updated.
18328 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
18329 20 34 29 20 49 66 20 74 68 65 20 73 69 62 6c 69   4) If the sibli
1832a 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f 74  ng pages are not
1832b 20 69 6e 74 65 72 6e 61 6c 20 69 6e 74 6b 65 79   internal intkey
1832c 20 6e 6f 64 65 73 2c 20 74 68 65 6e 20 61 6e 79   nodes, then any
1832d 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6f 76 65  .    **      ove
1832e 72 66 6c 6f 77 20 70 61 67 65 73 20 75 73 65 64  rflow pages used
1832f 20 62 79 20 74 68 65 73 65 20 63 65 6c 6c 73 20   by these cells 
18330 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20 75  may need to be u
18331 70 64 61 74 65 64 0a 20 20 20 20 2a 2a 20 20 20  pdated.    **   
18332 20 20 20 28 69 6e 74 65 72 6e 61 6c 20 69 6e 74     (internal int
18333 6b 65 79 20 6e 6f 64 65 73 20 6e 65 76 65 72 20  key nodes never 
18334 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74 65 72 73  contain pointers
18335 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   to overflow pag
18336 65 73 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  es)..    **.    
18337 2a 2a 20 20 20 35 29 20 49 66 20 74 68 65 20 73  **   5) If the s
18338 69 62 6c 69 6e 67 20 70 61 67 65 73 20 61 72 65  ibling pages are
18339 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65   not leaves, the
1833a 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  n the pointer-ma
1833b 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 65 6e  p.    **      en
1833c 74 72 69 65 73 20 66 6f 72 20 74 68 65 20 72 69  tries for the ri
1833d 67 68 74 2d 63 68 69 6c 64 20 70 61 67 65 73 20  ght-child pages 
1833e 6f 66 20 65 61 63 68 20 73 69 62 6c 69 6e 67 20  of each sibling 
1833f 6d 61 79 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20  may need.    ** 
18340 20 20 20 20 20 74 6f 20 62 65 20 75 70 64 61 74       to be updat
18341 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ed..    **.    *
18342 2a 20 43 61 73 65 73 20 31 20 61 6e 64 20 32 20  * Cases 1 and 2 
18343 61 72 65 20 64 65 61 6c 74 20 77 69 74 68 20 61  are dealt with a
18344 62 6f 76 65 20 62 79 20 6f 74 68 65 72 20 63 6f  bove by other co
18345 64 65 2e 20 54 68 65 20 6e 65 78 74 0a 20 20 20  de. The next.   
18346 20 2a 2a 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20   ** block deals 
18347 77 69 74 68 20 63 61 73 65 73 20 33 20 61 6e 64  with cases 3 and
18348 20 34 20 61 6e 64 20 74 68 65 20 6f 6e 65 20 61   4 and the one a
18349 66 74 65 72 20 74 68 61 74 2c 20 63 61 73 65 20  fter that, case 
1834a 35 2e 20 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20  5. Since.    ** 
1834b 73 65 74 74 69 6e 67 20 61 20 70 6f 69 6e 74 65  setting a pointe
1834c 72 20 6d 61 70 20 65 6e 74 72 79 20 69 73 20 61  r map entry is a
1834d 20 72 65 6c 61 74 69 76 65 6c 79 20 65 78 70 65   relatively expe
1834e 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e 2c  nsive operation,
1834f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 63 6f 64   this.    ** cod
18350 65 20 6f 6e 6c 79 20 73 65 74 73 20 70 6f 69 6e  e only sets poin
18351 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65 73 20  ter map entries 
18352 66 6f 72 20 63 68 69 6c 64 20 6f 72 20 6f 76 65  for child or ove
18353 72 66 6c 6f 77 20 70 61 67 65 73 20 74 68 61 74  rflow pages that
18354 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 61 63 74   have.    ** act
18355 75 61 6c 6c 79 20 6d 6f 76 65 64 20 62 65 74 77  ually moved betw
18356 65 65 6e 20 70 61 67 65 73 2e 20 20 2a 2f 0a 20  een pages.  */. 
18357 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77     MemPage *pNew
18358 20 3d 20 61 70 4e 65 77 5b 30 5d 3b 0a 20 20 20   = apNew[0];.   
18359 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d   MemPage *pOld =
1835a 20 61 70 43 6f 70 79 5b 30 5d 3b 0a 20 20 20 20   apCopy[0];.    
1835b 69 6e 74 20 6e 4f 76 65 72 66 6c 6f 77 20 3d 20  int nOverflow = 
1835c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b  pOld->nOverflow;
1835d 0a 20 20 20 20 69 6e 74 20 69 4e 65 78 74 4f 6c  .    int iNextOl
1835e 64 20 3d 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 20  d = pOld->nCell 
1835f 2b 20 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20  + nOverflow;.   
18360 20 69 6e 74 20 69 4f 76 65 72 66 6c 6f 77 20 3d   int iOverflow =
18361 20 28 6e 4f 76 65 72 66 6c 6f 77 20 3f 20 70 4f   (nOverflow ? pO
18362 6c 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78  ld->aOvfl[0].idx
18363 20 3a 20 2d 31 29 3b 0a 20 20 20 20 6a 20 3d 20   : -1);.    j = 
18364 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
18365 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18366 2a 20 43 75 72 72 65 6e 74 20 27 6f 6c 64 27 20  * Current 'old' 
18367 73 69 62 6c 69 6e 67 20 70 61 67 65 20 2a 2f 0a  sibling page */.
18368 20 20 20 20 6b 20 3d 20 30 3b 20 20 20 20 20 20      k = 0;      
18369 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1836a 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
1836b 74 20 27 6e 65 77 27 20 73 69 62 6c 69 6e 67 20  t 'new' sibling 
1836c 70 61 67 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28  page */.    for(
1836d 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b  i=0; i<nCell; i+
1836e 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73  +){.      int is
1836f 44 69 76 69 64 65 72 20 3d 20 30 3b 0a 20 20 20  Divider = 0;.   
18370 20 20 20 77 68 69 6c 65 28 20 69 3d 3d 69 4e 65     while( i==iNe
18371 78 74 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 20  xtOld ){.       
18372 20 2f 2a 20 43 65 6c 6c 20 69 20 69 73 20 74 68   /* Cell i is th
18373 65 20 63 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65  e cell immediate
18374 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65  ly following the
18375 20 6c 61 73 74 20 63 65 6c 6c 20 6f 6e 20 6f 6c   last cell on ol
18376 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69 62  d.        ** sib
18377 6c 69 6e 67 20 70 61 67 65 20 6a 2e 20 49 66 20  ling page j. If 
18378 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72 65  the siblings are
18379 20 6e 6f 74 20 6c 65 61 66 20 70 61 67 65 73 20   not leaf pages 
1837a 6f 66 20 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a  of an.        **
1837b 20 69 6e 74 6b 65 79 20 62 2d 74 72 65 65 2c 20   intkey b-tree, 
1837c 74 68 65 6e 20 63 65 6c 6c 20 69 20 77 61 73 20  then cell i was 
1837d 61 20 64 69 76 69 64 65 72 20 63 65 6c 6c 2e 20  a divider cell. 
1837e 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f 6c 64 20  */.        pOld 
1837f 3d 20 61 70 43 6f 70 79 5b 2b 2b 6a 5d 3b 0a 20  = apCopy[++j];. 
18380 20 20 20 20 20 20 20 69 4e 65 78 74 4f 6c 64 20         iNextOld 
18381 3d 20 69 20 2b 20 21 6c 65 61 66 44 61 74 61 20  = i + !leafData 
18382 2b 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 20 2b 20  + pOld->nCell + 
18383 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b  pOld->nOverflow;
18384 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f 6c  .        if( pOl
18385 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a  d->nOverflow ){.
18386 20 20 20 20 20 20 20 20 20 20 6e 4f 76 65 72 66            nOverf
18387 6c 6f 77 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65  low = pOld->nOve
18388 72 66 6c 6f 77 3b 0a 20 20 20 20 20 20 20 20 20  rflow;.         
18389 20 69 4f 76 65 72 66 6c 6f 77 20 3d 20 69 20 2b   iOverflow = i +
1838a 20 21 6c 65 61 66 44 61 74 61 20 2b 20 70 4f 6c   !leafData + pOl
1838b 64 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3b  d->aOvfl[0].idx;
1838c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1838d 20 20 20 69 73 44 69 76 69 64 65 72 20 3d 20 21     isDivider = !
1838e 6c 65 61 66 44 61 74 61 3b 20 20 0a 20 20 20 20  leafData;  .    
1838f 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72    }..      asser
18390 74 28 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c  t(nOverflow>0 ||
18391 20 69 4f 76 65 72 66 6c 6f 77 3c 69 20 29 3b 0a   iOverflow<i );.
18392 20 20 20 20 20 20 61 73 73 65 72 74 28 6e 4f 76        assert(nOv
18393 65 72 66 6c 6f 77 3c 32 20 7c 7c 20 70 4f 6c 64  erflow<2 || pOld
18394 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d  ->aOvfl[0].idx==
18395 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31 5d 2e 69  pOld->aOvfl[1].i
18396 64 78 2d 31 29 3b 0a 20 20 20 20 20 20 61 73 73  dx-1);.      ass
18397 65 72 74 28 6e 4f 76 65 72 66 6c 6f 77 3c 33 20  ert(nOverflow<3 
18398 7c 7c 20 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31  || pOld->aOvfl[1
18399 5d 2e 69 64 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76  ].idx==pOld->aOv
1839a 66 6c 5b 32 5d 2e 69 64 78 2d 31 29 3b 0a 20 20  fl[2].idx-1);.  
1839b 20 20 20 20 69 66 28 20 69 3d 3d 69 4f 76 65 72      if( i==iOver
1839c 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20  flow ){.        
1839d 69 73 44 69 76 69 64 65 72 20 3d 20 31 3b 0a 20  isDivider = 1;. 
1839e 20 20 20 20 20 20 20 69 66 28 20 28 2d 2d 6e 4f         if( (--nO
1839f 76 65 72 66 6c 6f 77 29 3e 30 20 29 7b 0a 20 20  verflow)>0 ){.  
183a0 20 20 20 20 20 20 20 20 69 4f 76 65 72 66 6c 6f          iOverflo
183a1 77 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  w++;.        }. 
183a2 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66       }..      if
183a3 28 20 69 3d 3d 63 6e 74 4e 65 77 5b 6b 5d 20 29  ( i==cntNew[k] )
183a4 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 65 6c  {.        /* Cel
183a5 6c 20 69 20 69 73 20 74 68 65 20 63 65 6c 6c 20  l i is the cell 
183a6 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c  immediately foll
183a7 6f 77 69 6e 67 20 74 68 65 20 6c 61 73 74 20 63  owing the last c
183a8 65 6c 6c 20 6f 6e 20 6e 65 77 0a 20 20 20 20 20  ell on new.     
183a9 20 20 20 2a 2a 20 73 69 62 6c 69 6e 67 20 70 61     ** sibling pa
183aa 67 65 20 6b 2e 20 49 66 20 74 68 65 20 73 69 62  ge k. If the sib
183ab 6c 69 6e 67 73 20 61 72 65 20 6e 6f 74 20 6c 65  lings are not le
183ac 61 66 20 70 61 67 65 73 20 6f 66 20 61 6e 0a 20  af pages of an. 
183ad 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6b 65 79         ** intkey
183ae 20 62 2d 74 72 65 65 2c 20 74 68 65 6e 20 63 65   b-tree, then ce
183af 6c 6c 20 69 20 69 73 20 61 20 64 69 76 69 64 65  ll i is a divide
183b0 72 20 63 65 6c 6c 2e 20 20 2a 2f 0a 20 20 20 20  r cell.  */.    
183b1 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e 65 77      pNew = apNew
183b2 5b 2b 2b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 69  [++k];.        i
183b3 66 28 20 21 6c 65 61 66 44 61 74 61 20 29 20 63  f( !leafData ) c
183b4 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d  ontinue;.      }
183b5 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a  .      assert( j
183b6 3c 6e 4f 6c 64 20 29 3b 0a 20 20 20 20 20 20 61  <nOld );.      a
183b7 73 73 65 72 74 28 20 6b 3c 6e 4e 65 77 20 29 3b  ssert( k<nNew );
183b8 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  ..      /* If th
183b9 65 20 63 65 6c 6c 20 77 61 73 20 6f 72 69 67 69  e cell was origi
183ba 6e 61 6c 6c 79 20 64 69 76 69 64 65 72 20 63 65  nally divider ce
183bb 6c 6c 20 28 61 6e 64 20 69 73 20 6e 6f 74 20 6e  ll (and is not n
183bc 6f 77 29 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20  ow) or.      ** 
183bd 61 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  an overflow cell
183be 2c 20 6f 72 20 69 66 20 74 68 65 20 63 65 6c 6c  , or if the cell
183bf 20 77 61 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20   was located on 
183c0 61 20 64 69 66 66 65 72 65 6e 74 20 73 69 62 6c  a different sibl
183c1 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 70 61 67  ing.      ** pag
183c2 65 20 62 65 66 6f 72 65 20 74 68 65 20 62 61 6c  e before the bal
183c3 61 6e 63 69 6e 67 2c 20 74 68 65 6e 20 74 68 65  ancing, then the
183c4 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74   pointer map ent
183c5 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a  ries associated.
183c6 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 61 6e        ** with an
183c7 79 20 63 68 69 6c 64 20 6f 72 20 6f 76 65 72 66  y child or overf
183c8 6c 6f 77 20 70 61 67 65 73 20 6e 65 65 64 20 74  low pages need t
183c9 6f 20 62 65 20 75 70 64 61 74 65 64 2e 20 20 2a  o be updated.  *
183ca 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73 44 69  /.      if( isDi
183cb 76 69 64 65 72 20 7c 7c 20 70 4f 6c 64 2d 3e 70  vider || pOld->p
183cc 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70 67 6e 6f 20  gno!=pNew->pgno 
183cd 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ){.        if( !
183ce 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 20 29  leafCorrection )
183cf 7b 0a 20 20 20 20 20 20 20 20 20 20 70 74 72 6d  {.          ptrm
183d0 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 62  apPut(pBt, get4b
183d1 79 74 65 28 61 70 43 65 6c 6c 5b 69 5d 29 2c 20  yte(apCell[i]), 
183d2 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e  PTRMAP_BTREE, pN
183d3 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a  ew->pgno, &rc);.
183d4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
183d5 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 69 5d 3e    if( szCell[i]>
183d6 70 4e 65 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29  pNew->minLocal )
183d7 7b 0a 20 20 20 20 20 20 20 20 20 20 70 74 72 6d  {.          ptrm
183d8 61 70 50 75 74 4f 76 66 6c 50 74 72 28 70 4e 65  apPutOvflPtr(pNe
183d9 77 2c 20 61 70 43 65 6c 6c 5b 69 5d 2c 20 26 72  w, apCell[i], &r
183da 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  c);.        }.  
183db 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
183dc 20 69 66 28 20 21 6c 65 61 66 43 6f 72 72 65 63   if( !leafCorrec
183dd 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 66 6f  tion ){.      fo
183de 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69  r(i=0; i<nNew; i
183df 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 75 33 32  ++){.        u32
183e0 20 6b 65 79 20 3d 20 67 65 74 34 62 79 74 65 28   key = get4byte(
183e1 26 61 70 4e 65 77 5b 69 5d 2d 3e 61 44 61 74 61  &apNew[i]->aData
183e2 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20 20 70 74  [8]);.        pt
183e3 72 6d 61 70 50 75 74 28 70 42 74 2c 20 6b 65 79  rmapPut(pBt, key
183e4 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20  , PTRMAP_BTREE, 
183e5 61 70 4e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 2c 20  apNew[i]->pgno, 
183e6 26 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  &rc);.      }.  
183e7 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f    }..#if 0.    /
183e8 2a 20 54 68 65 20 70 74 72 6d 61 70 43 68 65 63  * The ptrmapChec
183e9 6b 50 61 67 65 73 28 29 20 63 6f 6e 74 61 69 6e  kPages() contain
183ea 73 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  s assert() state
183eb 6d 65 6e 74 73 20 74 68 61 74 20 76 65 72 69 66  ments that verif
183ec 79 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 61 6c  y that.    ** al
183ed 6c 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  l pointer map pa
183ee 67 65 73 20 61 72 65 20 73 65 74 20 63 6f 72 72  ges are set corr
183ef 65 63 74 6c 79 2e 20 54 68 69 73 20 69 73 20 68  ectly. This is h
183f0 65 6c 70 66 75 6c 20 77 68 69 6c 65 20 0a 20 20  elpful while .  
183f1 20 20 2a 2a 20 64 65 62 75 67 67 69 6e 67 2e 20    ** debugging. 
183f2 54 68 69 73 20 69 73 20 75 73 75 61 6c 6c 79 20  This is usually 
183f3 64 69 73 61 62 6c 65 64 20 62 65 63 61 75 73 65  disabled because
183f4 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62   a corrupt datab
183f5 61 73 65 20 6d 61 79 0a 20 20 20 20 2a 2a 20 63  ase may.    ** c
183f6 61 75 73 65 20 61 6e 20 61 73 73 65 72 74 28 29  ause an assert()
183f7 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 66 61   statement to fa
183f8 69 6c 2e 20 20 2a 2f 0a 20 20 20 20 70 74 72 6d  il.  */.    ptrm
183f9 61 70 43 68 65 63 6b 50 61 67 65 73 28 61 70 4e  apCheckPages(apN
183fa 65 77 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 70  ew, nNew);.    p
183fb 74 72 6d 61 70 43 68 65 63 6b 50 61 67 65 73 28  trmapCheckPages(
183fc 26 70 50 61 72 65 6e 74 2c 20 31 29 3b 0a 23 65  &pParent, 1);.#e
183fd 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 61 73 73 65  ndif.  }..  asse
183fe 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 69 73 49  rt( pParent->isI
183ff 6e 69 74 20 29 3b 0a 20 20 54 52 41 43 45 28 28  nit );.  TRACE((
18400 22 42 41 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68  "BALANCE: finish
18401 65 64 3a 20 6f 6c 64 3d 25 64 20 6e 65 77 3d 25  ed: old=%d new=%
18402 64 20 63 65 6c 6c 73 3d 25 64 5c 6e 22 2c 0a 20  d cells=%d\n",. 
18403 20 20 20 20 20 20 20 20 20 6e 4f 6c 64 2c 20 6e           nOld, n
18404 4e 65 77 2c 20 6e 43 65 6c 6c 29 29 3b 0a 0a 20  New, nCell));.. 
18405 20 2f 2a 0a 20 20 2a 2a 20 43 6c 65 61 6e 75 70   /*.  ** Cleanup
18406 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
18407 67 2e 0a 20 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f  g..  */.balance_
18408 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74  cleanup:.  sqlit
18409 65 33 53 63 72 61 74 63 68 46 72 65 65 28 61 70  e3ScratchFree(ap
1840a 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30  Cell);.  for(i=0
1840b 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a  ; i<nOld; i++){.
1840c 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1840d 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 7d 0a 20  apOld[i]);.  }. 
1840e 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77   for(i=0; i<nNew
1840f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65  ; i++){.    rele
18410 61 73 65 50 61 67 65 28 61 70 4e 65 77 5b 69 5d  asePage(apNew[i]
18411 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
18412 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54   rc;.}.../*.** T
18413 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
18414 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20  called when the 
18415 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62  root page of a b
18416 2d 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  -tree structure 
18417 69 73 0a 2a 2a 20 6f 76 65 72 66 75 6c 6c 20 28  is.** overfull (
18418 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  has one or more 
18419 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29 2e  overflow pages).
1841a 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77 20 63 68 69  .**.** A new chi
1841b 6c 64 20 70 61 67 65 20 69 73 20 61 6c 6c 6f 63  ld page is alloc
1841c 61 74 65 64 20 61 6e 64 20 74 68 65 20 63 6f 6e  ated and the con
1841d 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 72  tents of the cur
1841e 72 65 6e 74 20 72 6f 6f 74 0a 2a 2a 20 70 61 67  rent root.** pag
1841f 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 6f 76 65  e, including ove
18420 72 66 6c 6f 77 20 63 65 6c 6c 73 2c 20 61 72 65  rflow cells, are
18421 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
18422 20 63 68 69 6c 64 2e 20 54 68 65 20 72 6f 6f 74   child. The root
18423 0a 2a 2a 20 70 61 67 65 20 69 73 20 74 68 65 6e  .** page is then
18424 20 6f 76 65 72 77 72 69 74 74 65 6e 20 74 6f 20   overwritten to 
18425 6d 61 6b 65 20 69 74 20 61 6e 20 65 6d 70 74 79  make it an empty
18426 20 70 61 67 65 20 77 69 74 68 20 74 68 65 20 72   page with the r
18427 69 67 68 74 2d 63 68 69 6c 64 20 0a 2a 2a 20 70  ight-child .** p
18428 6f 69 6e 74 65 72 20 70 6f 69 6e 74 69 6e 67 20  ointer pointing 
18429 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e  to the new page.
1842a 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65  .**.** Before re
1842b 74 75 72 6e 69 6e 67 2c 20 61 6c 6c 20 70 6f 69  turning, all poi
1842c 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73  nter-map entries
1842d 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
1842e 6f 20 70 61 67 65 73 20 0a 2a 2a 20 74 68 61 74  o pages .** that
1842f 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64 2d 70   the new child-p
18430 61 67 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73  age now contains
18431 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 61 72 65   pointers to are
18432 20 75 70 64 61 74 65 64 2e 20 54 68 65 0a 2a 2a   updated. The.**
18433 20 65 6e 74 72 79 20 63 6f 72 72 65 73 70 6f 6e   entry correspon
18434 64 69 6e 67 20 74 6f 20 74 68 65 20 6e 65 77 20  ding to the new 
18435 72 69 67 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e  right-child poin
18436 74 65 72 20 6f 66 20 74 68 65 20 72 6f 6f 74 0a  ter of the root.
18437 2a 2a 20 70 61 67 65 20 69 73 20 61 6c 73 6f 20  ** page is also 
18438 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  updated..**.** I
18439 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 2a 70  f successful, *p
1843a 70 43 68 69 6c 64 20 69 73 20 73 65 74 20 74 6f  pChild is set to
1843b 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 66 65 72   contain a refer
1843c 65 6e 63 65 20 74 6f 20 74 68 65 20 63 68 69 6c  ence to the chil
1843d 64 20 0a 2a 2a 20 70 61 67 65 20 61 6e 64 20 53  d .** page and S
1843e 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
1843f 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  rned. In this ca
18440 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73  se the caller is
18441 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 74 6f 20   required.** to 
18442 63 61 6c 6c 20 72 65 6c 65 61 73 65 50 61 67 65  call releasePage
18443 28 29 20 6f 6e 20 2a 70 70 43 68 69 6c 64 20 65  () on *ppChild e
18444 78 61 63 74 6c 79 20 6f 6e 63 65 2e 20 49 66 20  xactly once. If 
18445 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
18446 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  .** an error cod
18447 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  e is returned an
18448 64 20 2a 70 70 43 68 69 6c 64 20 69 73 20 73 65  d *ppChild is se
18449 74 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74 69  t to 0..*/.stati
1844a 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65  c int balance_de
1844b 65 70 65 72 28 4d 65 6d 50 61 67 65 20 2a 70 52  eper(MemPage *pR
1844c 6f 6f 74 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 70  oot, MemPage **p
1844d 70 43 68 69 6c 64 29 7b 0a 20 20 69 6e 74 20 72  pChild){.  int r
1844e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
1844f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
18450 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 73  urn value from s
18451 75 62 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a  ubprocedures */.
18452 20 20 4d 65 6d 50 61 67 65 20 2a 70 43 68 69 6c    MemPage *pChil
18453 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  d = 0;          
18454 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
18455 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20   new child page 
18456 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68  */.  Pgno pgnoCh
18457 69 6c 64 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ild = 0;        
18458 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62      /* Page numb
18459 65 72 20 6f 66 20 74 68 65 20 6e 65 77 20 63 68  er of the new ch
1845a 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20 42 74  ild page */.  Bt
1845b 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 52  Shared *pBt = pR
1845c 6f 6f 74 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20  oot->pBt;    /* 
1845d 54 68 65 20 42 54 72 65 65 20 2a 2f 0a 0a 20 20  The BTree */..  
1845e 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d 3e 6e  assert( pRoot->n
1845f 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a 20 20  Overflow>0 );.  
18460 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
18461 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
18462 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20  mutex) );..  /* 
18463 4d 61 6b 65 20 70 52 6f 6f 74 2c 20 74 68 65 20  Make pRoot, the 
18464 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65  root page of the
18465 20 62 2d 74 72 65 65 2c 20 77 72 69 74 61 62 6c   b-tree, writabl
18466 65 2e 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65  e. Allocate a ne
18467 77 20 0a 20 20 2a 2a 20 70 61 67 65 20 74 68 61  w .  ** page tha
18468 74 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68  t will become th
18469 65 20 6e 65 77 20 72 69 67 68 74 2d 63 68 69 6c  e new right-chil
1846a 64 20 6f 66 20 70 50 61 67 65 2e 20 43 6f 70 79  d of pPage. Copy
1846b 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20   the contents.  
1846c 2a 2a 20 6f 66 20 74 68 65 20 6e 6f 64 65 20 73  ** of the node s
1846d 74 6f 72 65 64 20 6f 6e 20 70 52 6f 6f 74 20 69  tored on pRoot i
1846e 6e 74 6f 20 74 68 65 20 6e 65 77 20 63 68 69 6c  nto the new chil
1846f 64 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 72  d page..  */.  r
18470 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
18471 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62  Write(pRoot->pDb
18472 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  Page);.  if( rc=
18473 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
18474 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42    rc = allocateB
18475 74 72 65 65 50 61 67 65 28 70 42 74 2c 26 70 43  treePage(pBt,&pC
18476 68 69 6c 64 2c 26 70 67 6e 6f 43 68 69 6c 64 2c  hild,&pgnoChild,
18477 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 30 29 3b 0a  pRoot->pgno,0);.
18478 20 20 20 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74      copyNodeCont
18479 65 6e 74 28 70 52 6f 6f 74 2c 20 70 43 68 69 6c  ent(pRoot, pChil
1847a 64 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28  d, &rc);.    if(
1847b 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b   ISAUTOVACUUM ){
1847c 0a 20 20 20 20 20 20 70 74 72 6d 61 70 50 75 74  .      ptrmapPut
1847d 28 70 42 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c  (pBt, pgnoChild,
1847e 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70   PTRMAP_BTREE, p
1847f 52 6f 6f 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29  Root->pgno, &rc)
18480 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
18481 28 20 72 63 20 29 7b 0a 20 20 20 20 2a 70 70 43  ( rc ){.    *ppC
18482 68 69 6c 64 20 3d 20 30 3b 0a 20 20 20 20 72 65  hild = 0;.    re
18483 6c 65 61 73 65 50 61 67 65 28 70 43 68 69 6c 64  leasePage(pChild
18484 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  );.    return rc
18485 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
18486 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72  sqlite3PagerIswr
18487 69 74 65 61 62 6c 65 28 70 43 68 69 6c 64 2d 3e  iteable(pChild->
18488 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73  pDbPage) );.  as
18489 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
1848a 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 52  erIswriteable(pR
1848b 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  oot->pDbPage) );
1848c 0a 20 20 61 73 73 65 72 74 28 20 70 43 68 69 6c  .  assert( pChil
1848d 64 2d 3e 6e 43 65 6c 6c 3d 3d 70 52 6f 6f 74 2d  d->nCell==pRoot-
1848e 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 54 52 41  >nCell );..  TRA
1848f 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 6f  CE(("BALANCE: co
18490 70 79 20 72 6f 6f 74 20 25 64 20 69 6e 74 6f 20  py root %d into 
18491 25 64 5c 6e 22 2c 20 70 52 6f 6f 74 2d 3e 70 67  %d\n", pRoot->pg
18492 6e 6f 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f  no, pChild->pgno
18493 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74  ));..  /* Copy t
18494 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  he overflow cell
18495 73 20 66 72 6f 6d 20 70 52 6f 6f 74 20 74 6f 20  s from pRoot to 
18496 70 43 68 69 6c 64 20 2a 2f 0a 20 20 6d 65 6d 63  pChild */.  memc
18497 70 79 28 70 43 68 69 6c 64 2d 3e 61 4f 76 66 6c  py(pChild->aOvfl
18498 2c 20 70 52 6f 6f 74 2d 3e 61 4f 76 66 6c 2c 20  , pRoot->aOvfl, 
18499 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pRoot->nOverflow
1849a 2a 73 69 7a 65 6f 66 28 70 52 6f 6f 74 2d 3e 61  *sizeof(pRoot->a
1849b 4f 76 66 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 68  Ovfl[0]));.  pCh
1849c 69 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d  ild->nOverflow =
1849d 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f   pRoot->nOverflo
1849e 77 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68  w;..  /* Zero th
1849f 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 52  e contents of pR
184a0 6f 6f 74 2e 20 54 68 65 6e 20 69 6e 73 74 61 6c  oot. Then instal
184a1 6c 20 70 43 68 69 6c 64 20 61 73 20 74 68 65 20  l pChild as the 
184a2 72 69 67 68 74 2d 63 68 69 6c 64 2e 20 2a 2f 0a  right-child. */.
184a3 20 20 7a 65 72 6f 50 61 67 65 28 70 52 6f 6f 74    zeroPage(pRoot
184a4 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b  , pChild->aData[
184a5 30 5d 20 26 20 7e 50 54 46 5f 4c 45 41 46 29 3b  0] & ~PTF_LEAF);
184a6 0a 20 20 70 75 74 34 62 79 74 65 28 26 70 52 6f  .  put4byte(&pRo
184a7 6f 74 2d 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d  ot->aData[pRoot-
184a8 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70  >hdrOffset+8], p
184a9 67 6e 6f 43 68 69 6c 64 29 3b 0a 0a 20 20 2a 70  gnoChild);..  *p
184aa 70 43 68 69 6c 64 20 3d 20 70 43 68 69 6c 64 3b  pChild = pChild;
184ab 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
184ac 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
184ad 65 20 70 61 67 65 20 74 68 61 74 20 70 43 75 72  e page that pCur
184ae 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
184af 73 20 74 6f 20 68 61 73 20 6a 75 73 74 20 62 65  s to has just be
184b0 65 6e 20 6d 6f 64 69 66 69 65 64 20 69 6e 0a 2a  en modified in.*
184b1 2a 20 73 6f 6d 65 20 77 61 79 2e 20 54 68 69 73  * some way. This
184b2 20 66 75 6e 63 74 69 6f 6e 20 66 69 67 75 72 65   function figure
184b3 73 20 6f 75 74 20 69 66 20 74 68 69 73 20 6d 6f  s out if this mo
184b4 64 69 66 69 63 61 74 69 6f 6e 20 6d 65 61 6e 73  dification means
184b5 20 74 68 65 0a 2a 2a 20 74 72 65 65 20 6e 65 65   the.** tree nee
184b6 64 73 20 74 6f 20 62 65 20 62 61 6c 61 6e 63 65  ds to be balance
184b7 64 2c 20 61 6e 64 20 69 66 20 73 6f 20 63 61 6c  d, and if so cal
184b8 6c 73 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  ls the appropria
184b9 74 65 20 62 61 6c 61 6e 63 69 6e 67 20 0a 2a 2a  te balancing .**
184ba 20 72 6f 75 74 69 6e 65 2e 20 42 61 6c 61 6e 63   routine. Balanc
184bb 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ing routines are
184bc 3a 0a 2a 2a 0a 2a 2a 20 20 20 62 61 6c 61 6e 63  :.**.**   balanc
184bd 65 5f 71 75 69 63 6b 28 29 0a 2a 2a 20 20 20 62  e_quick().**   b
184be 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28 29 0a  alance_deeper().
184bf 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e  **   balance_non
184c0 72 6f 6f 74 28 29 0a 2a 2f 0a 73 74 61 74 69 63  root().*/.static
184c1 20 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 74 43   int balance(BtC
184c2 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
184c3 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
184c4 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  OK;.  const int 
184c5 6e 4d 69 6e 20 3d 20 70 43 75 72 2d 3e 70 42 74  nMin = pCur->pBt
184c6 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2a 20 32  ->usableSize * 2
184c7 20 2f 20 33 3b 0a 20 20 75 38 20 61 42 61 6c 61   / 3;.  u8 aBala
184c8 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b 31 33  nceQuickSpace[13
184c9 5d 3b 0a 20 20 75 38 20 2a 70 46 72 65 65 20 3d  ];.  u8 *pFree =
184ca 20 30 3b 0a 0a 20 20 54 45 53 54 4f 4e 4c 59 28   0;..  TESTONLY(
184cb 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f 71 75 69   int balance_qui
184cc 63 6b 5f 63 61 6c 6c 65 64 20 3d 20 30 20 29 3b  ck_called = 0 );
184cd 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74  .  TESTONLY( int
184ce 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 5f   balance_deeper_
184cf 63 61 6c 6c 65 64 20 3d 20 30 20 29 3b 0a 0a 20  called = 0 );.. 
184d0 20 64 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69 50   do {.    int iP
184d1 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67  age = pCur->iPag
184d2 65 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  e;.    MemPage *
184d3 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
184d4 50 61 67 65 5b 69 50 61 67 65 5d 3b 0a 0a 20 20  Page[iPage];..  
184d5 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29    if( iPage==0 )
184d6 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 67  {.      if( pPag
184d7 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a  e->nOverflow ){.
184d8 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
184d9 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  oot page of the 
184da 62 2d 74 72 65 65 20 69 73 20 6f 76 65 72 66 75  b-tree is overfu
184db 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ll. In this case
184dc 20 63 61 6c 6c 20 74 68 65 0a 20 20 20 20 20 20   call the.      
184dd 20 20 2a 2a 20 62 61 6c 61 6e 63 65 5f 64 65 65    ** balance_dee
184de 70 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 74  per() function t
184df 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 63  o create a new c
184e0 68 69 6c 64 20 66 6f 72 20 74 68 65 20 72 6f 6f  hild for the roo
184e1 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a  t-page.        *
184e2 2a 20 61 6e 64 20 63 6f 70 79 20 74 68 65 20 63  * and copy the c
184e3 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20  urrent contents 
184e4 6f 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65  of the root-page
184e5 20 74 6f 20 69 74 2e 20 54 68 65 0a 20 20 20 20   to it. The.    
184e6 20 20 20 20 2a 2a 20 6e 65 78 74 20 69 74 65 72      ** next iter
184e7 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d  ation of the do-
184e8 6c 6f 6f 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63  loop will balanc
184e9 65 20 74 68 65 20 63 68 69 6c 64 20 70 61 67 65  e the child page
184ea 2e 0a 20 20 20 20 20 20 20 20 2a 2f 20 0a 20 20  ..        */ .  
184eb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 62        assert( (b
184ec 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 5f 63 61  alance_deeper_ca
184ed 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b 0a 20 20  lled++)==0 );.  
184ee 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e        rc = balan
184ef 63 65 5f 64 65 65 70 65 72 28 70 50 61 67 65 2c  ce_deeper(pPage,
184f0 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 31   &pCur->apPage[1
184f1 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]);.        if( 
184f2 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
184f3 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72 2d  .          pCur-
184f4 3e 69 50 61 67 65 20 3d 20 31 3b 0a 20 20 20 20  >iPage = 1;.    
184f5 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64        pCur->aiId
184f6 78 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  x[0] = 0;.      
184f7 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b      pCur->aiIdx[
184f8 31 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  1] = 0;.        
184f9 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
184fa 61 70 50 61 67 65 5b 31 5d 2d 3e 6e 4f 76 65 72  apPage[1]->nOver
184fb 66 6c 6f 77 20 29 3b 0a 20 20 20 20 20 20 20 20  flow );.        
184fc 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
184fd 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
184fe 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20      }.    }else 
184ff 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  if( pPage->nOver
18500 66 6c 6f 77 3d 3d 30 20 26 26 20 70 50 61 67 65  flow==0 && pPage
18501 2d 3e 6e 46 72 65 65 3c 3d 6e 4d 69 6e 20 29 7b  ->nFree<=nMin ){
18502 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
18503 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 4d    }else{.      M
18504 65 6d 50 61 67 65 20 2a 20 63 6f 6e 73 74 20 70  emPage * const p
18505 50 61 72 65 6e 74 20 3d 20 70 43 75 72 2d 3e 61  Parent = pCur->a
18506 70 50 61 67 65 5b 69 50 61 67 65 2d 31 5d 3b 0a  pPage[iPage-1];.
18507 20 20 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 20        int const 
18508 69 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49  iIdx = pCur->aiI
18509 64 78 5b 69 50 61 67 65 2d 31 5d 3b 0a 0a 20 20  dx[iPage-1];..  
1850a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1850b 50 61 67 65 72 57 72 69 74 65 28 70 50 61 72 65  PagerWrite(pPare
1850c 6e 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  nt->pDbPage);.  
1850d 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1850e 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 6e 64 65 66  TE_OK ){.#ifndef
1850f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49   SQLITE_OMIT_QUI
18510 43 4b 42 41 4c 41 4e 43 45 0a 20 20 20 20 20 20  CKBALANCE.      
18511 20 20 69 66 28 20 70 50 61 67 65 2d 3e 68 61 73    if( pPage->has
18512 44 61 74 61 0a 20 20 20 20 20 20 20 20 20 26 26  Data.         &&
18513 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
18514 77 3d 3d 31 0a 20 20 20 20 20 20 20 20 20 26 26  w==1.         &&
18515 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d   pPage->aOvfl[0]
18516 2e 69 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65  .idx==pPage->nCe
18517 6c 6c 0a 20 20 20 20 20 20 20 20 20 26 26 20 70  ll.         && p
18518 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 21 3d 31 0a  Parent->pgno!=1.
18519 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 72           && pPar
1851a 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 69 49 64 78  ent->nCell==iIdx
1851b 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20  .        ){.    
1851c 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 62 61        /* Call ba
1851d 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 74 6f  lance_quick() to
1851e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 73 69   create a new si
1851f 62 6c 69 6e 67 20 6f 66 20 70 50 61 67 65 20 6f  bling of pPage o
18520 6e 20 77 68 69 63 68 0a 20 20 20 20 20 20 20 20  n which.        
18521 20 20 2a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68    ** to store th
18522 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2e  e overflow cell.
18523 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29   balance_quick()
18524 20 69 6e 73 65 72 74 73 20 61 20 6e 65 77 20 63   inserts a new c
18525 65 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ell.          **
18526 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 2c 20 77   into pParent, w
18527 68 69 63 68 20 6d 61 79 20 63 61 75 73 65 20 70  hich may cause p
18528 50 61 72 65 6e 74 20 6f 76 65 72 66 6c 6f 77 2e  Parent overflow.
18529 20 49 66 20 74 68 69 73 0a 20 20 20 20 20 20 20   If this.       
1852a 20 20 20 2a 2a 20 68 61 70 70 65 6e 73 2c 20 74     ** happens, t
1852b 68 65 20 6e 65 78 74 20 69 6e 74 65 72 61 74 69  he next interati
1852c 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f  on of the do-loo
1852d 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63 65 20 70  p will balance p
1852e 50 61 72 65 6e 74 20 0a 20 20 20 20 20 20 20 20  Parent .        
1852f 20 20 2a 2a 20 75 73 65 20 65 69 74 68 65 72 20    ** use either 
18530 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28  balance_nonroot(
18531 29 20 6f 72 20 62 61 6c 61 6e 63 65 5f 64 65 65  ) or balance_dee
18532 70 65 72 28 29 2e 20 55 6e 74 69 6c 20 74 68 69  per(). Until thi
18533 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68  s.          ** h
18534 61 70 70 65 6e 73 2c 20 74 68 65 20 6f 76 65 72  appens, the over
18535 66 6c 6f 77 20 63 65 6c 6c 20 69 73 20 73 74 6f  flow cell is sto
18536 72 65 64 20 69 6e 20 74 68 65 20 61 42 61 6c 61  red in the aBala
18537 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b 5d 0a  nceQuickSpace[].
18538 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 75 66            ** buf
18539 66 65 72 2e 20 0a 20 20 20 20 20 20 20 20 20 20  fer. .          
1853a 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  **.          ** 
1853b 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74  The purpose of t
1853c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73  he following ass
1853d 65 72 74 28 29 20 69 73 20 74 6f 20 63 68 65 63  ert() is to chec
1853e 6b 20 74 68 61 74 20 6f 6e 6c 79 20 61 0a 20 20  k that only a.  
1853f 20 20 20 20 20 20 20 20 2a 2a 20 73 69 6e 67 6c          ** singl
18540 65 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63  e call to balanc
18541 65 5f 71 75 69 63 6b 28 29 20 69 73 20 6d 61 64  e_quick() is mad
18542 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c 20  e for each call 
18543 74 6f 20 74 68 69 73 0a 20 20 20 20 20 20 20 20  to this.        
18544 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49    ** function. I
18545 66 20 74 68 69 73 20 77 65 72 65 20 6e 6f 74 20  f this were not 
18546 76 65 72 69 66 69 65 64 2c 20 61 20 73 75 62 74  verified, a subt
18547 6c 65 20 62 75 67 20 69 6e 76 6f 6c 76 69 6e 67  le bug involving
18548 20 72 65 75 73 65 0a 20 20 20 20 20 20 20 20 20   reuse.         
18549 20 2a 2a 20 6f 66 20 74 68 65 20 61 42 61 6c 61   ** of the aBala
1854a 6e 63 65 51 75 69 63 6b 53 70 61 63 65 5b 5d 20  nceQuickSpace[] 
1854b 6d 69 67 68 74 20 73 6e 65 61 6b 20 69 6e 2e 0a  might sneak in..
1854c 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
1854d 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28         assert( (
1854e 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 5f 63 61  balance_quick_ca
1854f 6c 6c 65 64 2b 2b 29 3d 3d 30 20 29 3b 0a 20 20  lled++)==0 );.  
18550 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 61 6c          rc = bal
18551 61 6e 63 65 5f 71 75 69 63 6b 28 70 50 61 72 65  ance_quick(pPare
18552 6e 74 2c 20 70 50 61 67 65 2c 20 61 42 61 6c 61  nt, pPage, aBala
18553 6e 63 65 51 75 69 63 6b 53 70 61 63 65 29 3b 0a  nceQuickSpace);.
18554 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65          }else.#e
18555 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7b 0a 20  ndif.        {. 
18556 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 74           /* In t
18557 68 69 73 20 63 61 73 65 2c 20 63 61 6c 6c 20 62  his case, call b
18558 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
18559 20 74 6f 20 72 65 64 69 73 74 72 69 62 75 74 65   to redistribute
1855a 20 63 65 6c 6c 73 0a 20 20 20 20 20 20 20 20 20   cells.         
1855b 20 2a 2a 20 62 65 74 77 65 65 6e 20 70 50 61 67   ** between pPag
1855c 65 20 61 6e 64 20 75 70 20 74 6f 20 32 20 6f 66  e and up to 2 of
1855d 20 69 74 73 20 73 69 62 6c 69 6e 67 20 70 61 67   its sibling pag
1855e 65 73 2e 20 54 68 69 73 20 69 6e 76 6f 6c 76 65  es. This involve
1855f 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6d  s.          ** m
18560 6f 64 69 66 79 69 6e 67 20 74 68 65 20 63 6f 6e  odifying the con
18561 74 65 6e 74 73 20 6f 66 20 70 50 61 72 65 6e 74  tents of pParent
18562 2c 20 77 68 69 63 68 20 6d 61 79 20 63 61 75 73  , which may caus
18563 65 20 70 50 61 72 65 6e 74 20 74 6f 0a 20 20 20  e pParent to.   
18564 20 20 20 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65         ** become
18565 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20 75 6e 64   overfull or und
18566 65 72 66 75 6c 6c 2e 20 54 68 65 20 6e 65 78 74  erfull. The next
18567 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   iteration of th
18568 65 20 64 6f 2d 6c 6f 6f 70 0a 20 20 20 20 20 20  e do-loop.      
18569 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 61 6c 61      ** will bala
1856a 6e 63 65 20 74 68 65 20 70 61 72 65 6e 74 20 70  nce the parent p
1856b 61 67 65 20 74 6f 20 63 6f 72 72 65 63 74 20 74  age to correct t
1856c 68 69 73 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  his..          *
1856d 2a 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  * .          ** 
1856e 49 66 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  If the parent pa
1856f 67 65 20 62 65 63 6f 6d 65 73 20 6f 76 65 72 66  ge becomes overf
18570 75 6c 6c 2c 20 74 68 65 20 6f 76 65 72 66 6c 6f  ull, the overflo
18571 77 20 63 65 6c 6c 20 6f 72 20 63 65 6c 6c 73 0a  w cell or cells.
18572 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 72 65            ** are
18573 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 70   stored in the p
18574 53 70 61 63 65 20 62 75 66 66 65 72 20 61 6c 6c  Space buffer all
18575 6f 63 61 74 65 64 20 69 6d 6d 65 64 69 61 74 65  ocated immediate
18576 6c 79 20 62 65 6c 6f 77 2e 20 0a 20 20 20 20 20  ly below. .     
18577 20 20 20 20 20 2a 2a 20 41 20 73 75 62 73 65 71       ** A subseq
18578 75 65 6e 74 20 69 74 65 72 61 74 69 6f 6e 20 6f  uent iteration o
18579 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 69  f the do-loop wi
1857a 6c 6c 20 64 65 61 6c 20 77 69 74 68 20 74 68 69  ll deal with thi
1857b 73 20 62 79 0a 20 20 20 20 20 20 20 20 20 20 2a  s by.          *
1857c 2a 20 63 61 6c 6c 69 6e 67 20 62 61 6c 61 6e 63  * calling balanc
1857d 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 28 62 61 6c  e_nonroot() (bal
1857e 61 6e 63 65 5f 64 65 65 70 65 72 28 29 20 6d 61  ance_deeper() ma
1857f 79 20 62 65 20 63 61 6c 6c 65 64 20 66 69 72 73  y be called firs
18580 74 2c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  t,.          ** 
18581 62 75 74 20 69 74 20 64 6f 65 73 6e 27 74 20 64  but it doesn't d
18582 65 61 6c 20 77 69 74 68 20 6f 76 65 72 66 6c 6f  eal with overflo
18583 77 20 63 65 6c 6c 73 20 2d 20 6a 75 73 74 20 6d  w cells - just m
18584 6f 76 65 73 20 74 68 65 6d 20 74 6f 20 61 0a 20  oves them to a. 
18585 20 20 20 20 20 20 20 20 20 2a 2a 20 64 69 66 66           ** diff
18586 65 72 65 6e 74 20 70 61 67 65 29 2e 20 4f 6e 63  erent page). Onc
18587 65 20 74 68 69 73 20 73 75 62 73 65 71 75 65 6e  e this subsequen
18588 74 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63  t call to balanc
18589 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 0a 20 20 20  e_nonroot() .   
1858a 20 20 20 20 20 20 20 2a 2a 20 68 61 73 20 63 6f         ** has co
1858b 6d 70 6c 65 74 65 64 2c 20 69 74 20 69 73 20 73  mpleted, it is s
1858c 61 66 65 20 74 6f 20 72 65 6c 65 61 73 65 20 74  afe to release t
1858d 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65 72  he pSpace buffer
1858e 20 75 73 65 64 20 62 79 0a 20 20 20 20 20 20 20   used by.       
1858f 20 20 20 2a 2a 20 74 68 65 20 70 72 65 76 69 6f     ** the previo
18590 75 73 20 63 61 6c 6c 2c 20 61 73 20 74 68 65 20  us call, as the 
18591 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 64 61  overflow cell da
18592 74 61 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65  ta will have bee
18593 6e 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  n .          ** 
18594 63 6f 70 69 65 64 20 65 69 74 68 65 72 20 69 6e  copied either in
18595 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61  to the body of a
18596 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 6f   database page o
18597 72 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20  r into the new. 
18598 20 20 20 20 20 20 20 20 20 2a 2a 20 70 53 70 61           ** pSpa
18599 63 65 20 62 75 66 66 65 72 20 70 61 73 73 65 64  ce buffer passed
1859a 20 74 6f 20 74 68 65 20 6c 61 74 74 65 72 20 63   to the latter c
1859b 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 5f 6e  all to balance_n
1859c 6f 6e 72 6f 6f 74 28 29 2e 0a 20 20 20 20 20 20  onroot()..      
1859d 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
1859e 20 75 38 20 2a 70 53 70 61 63 65 20 3d 20 73 71   u8 *pSpace = sq
1859f 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28  lite3PageMalloc(
185a0 70 43 75 72 2d 3e 70 42 74 2d 3e 70 61 67 65 53  pCur->pBt->pageS
185a1 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ize);.          
185a2 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e  rc = balance_non
185a3 72 6f 6f 74 28 70 50 61 72 65 6e 74 2c 20 69 49  root(pParent, iI
185a4 64 78 2c 20 70 53 70 61 63 65 2c 20 69 50 61 67  dx, pSpace, iPag
185a5 65 3d 3d 31 29 3b 0a 20 20 20 20 20 20 20 20 20  e==1);.         
185a6 20 69 66 28 20 70 46 72 65 65 20 29 7b 0a 20 20   if( pFree ){.  
185a7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
185a8 70 46 72 65 65 20 69 73 20 6e 6f 74 20 4e 55 4c  pFree is not NUL
185a9 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  L, it points to 
185aa 74 68 65 20 70 53 70 61 63 65 20 62 75 66 66 65  the pSpace buffe
185ab 72 20 75 73 65 64 20 0a 20 20 20 20 20 20 20 20  r used .        
185ac 20 20 20 20 2a 2a 20 62 79 20 61 20 70 72 65 76      ** by a prev
185ad 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 62 61 6c  ious call to bal
185ae 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 20  ance_nonroot(). 
185af 49 74 73 20 63 6f 6e 74 65 6e 74 73 20 61 72 65  Its contents are
185b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
185b1 6e 6f 77 20 73 74 6f 72 65 64 20 65 69 74 68 65  now stored eithe
185b2 72 20 6f 6e 20 72 65 61 6c 20 64 61 74 61 62 61  r on real databa
185b3 73 65 20 70 61 67 65 73 20 6f 72 20 77 69 74 68  se pages or with
185b4 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 20 20  in the .        
185b5 20 20 20 20 2a 2a 20 6e 65 77 20 70 53 70 61 63      ** new pSpac
185b6 65 20 62 75 66 66 65 72 2c 20 73 6f 20 69 74 20  e buffer, so it 
185b7 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20 66 72  may be safely fr
185b8 65 65 64 20 68 65 72 65 2e 20 2a 2f 0a 20 20 20  eed here. */.   
185b9 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
185ba 50 61 67 65 46 72 65 65 28 70 46 72 65 65 29 3b  PageFree(pFree);
185bb 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  .          }..  
185bc 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
185bd 53 70 61 63 65 20 62 75 66 66 65 72 20 77 69 6c  Space buffer wil
185be 6c 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72  l be freed after
185bf 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74   the next call t
185c0 6f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62  o.          ** b
185c1 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29  alance_nonroot()
185c2 2c 20 6f 72 20 6a 75 73 74 20 62 65 66 6f 72 65  , or just before
185c3 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
185c4 65 74 75 72 6e 73 2c 20 77 68 69 63 68 65 76 65  eturns, whicheve
185c5 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  r.          ** c
185c6 6f 6d 65 73 20 66 69 72 73 74 2e 20 2a 2f 0a 20  omes first. */. 
185c7 20 20 20 20 20 20 20 20 20 70 46 72 65 65 20 3d           pFree =
185c8 20 70 53 70 61 63 65 3b 0a 20 20 20 20 20 20 20   pSpace;.       
185c9 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20   }.      }..    
185ca 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c    pPage->nOverfl
185cb 6f 77 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f  ow = 0;..      /
185cc 2a 20 54 68 65 20 6e 65 78 74 20 69 74 65 72 61  * The next itera
185cd 74 69 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c  tion of the do-l
185ce 6f 6f 70 20 62 61 6c 61 6e 63 65 73 20 74 68 65  oop balances the
185cf 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20 2a 2f   parent page. */
185d0 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
185d1 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ge(pPage);.     
185d2 20 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a   pCur->iPage--;.
185d3 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20      }.  }while( 
185d4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
185d5 0a 0a 20 20 69 66 28 20 70 46 72 65 65 20 29 7b  ..  if( pFree ){
185d6 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65  .    sqlite3Page
185d7 46 72 65 65 28 70 46 72 65 65 29 3b 0a 20 20 7d  Free(pFree);.  }
185d8 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
185d9 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61  ../*.** Insert a
185da 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e 74 6f   new record into
185db 20 74 68 65 20 42 54 72 65 65 2e 20 20 54 68 65   the BTree.  The
185dc 20 6b 65 79 20 69 73 20 67 69 76 65 6e 20 62 79   key is given by
185dd 20 28 70 4b 65 79 2c 6e 4b 65 79 29 0a 2a 2a 20   (pKey,nKey).** 
185de 61 6e 64 20 74 68 65 20 64 61 74 61 20 69 73 20  and the data is 
185df 67 69 76 65 6e 20 62 79 20 28 70 44 61 74 61 2c  given by (pData,
185e0 6e 44 61 74 61 29 2e 20 20 54 68 65 20 63 75 72  nData).  The cur
185e1 73 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79  sor is used only
185e2 20 74 6f 0a 2a 2a 20 64 65 66 69 6e 65 20 77 68   to.** define wh
185e3 61 74 20 74 61 62 6c 65 20 74 68 65 20 72 65 63  at table the rec
185e4 6f 72 64 20 73 68 6f 75 6c 64 20 62 65 20 69 6e  ord should be in
185e5 73 65 72 74 65 64 20 69 6e 74 6f 2e 20 20 54 68  serted into.  Th
185e6 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c  e cursor.** is l
185e7 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  eft pointing at 
185e8 61 20 72 61 6e 64 6f 6d 20 6c 6f 63 61 74 69 6f  a random locatio
185e9 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20  n..**.** For an 
185ea 49 4e 54 4b 45 59 20 74 61 62 6c 65 2c 20 6f 6e  INTKEY table, on
185eb 6c 79 20 74 68 65 20 6e 4b 65 79 20 76 61 6c 75  ly the nKey valu
185ec 65 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73 20  e of the key is 
185ed 75 73 65 64 2e 20 20 70 4b 65 79 20 69 73 0a 2a  used.  pKey is.*
185ee 2a 20 69 67 6e 6f 72 65 64 2e 20 20 46 6f 72 20  * ignored.  For 
185ef 61 20 5a 45 52 4f 44 41 54 41 20 74 61 62 6c 65  a ZERODATA table
185f0 2c 20 74 68 65 20 70 44 61 74 61 20 61 6e 64 20  , the pData and 
185f1 6e 44 61 74 61 20 61 72 65 20 62 6f 74 68 20 69  nData are both i
185f2 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  gnored..**.** If
185f3 20 74 68 65 20 73 65 65 6b 52 65 73 75 6c 74 20   the seekResult 
185f4 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e  parameter is non
185f5 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20 73 75  -zero, then a su
185f6 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
185f7 0a 2a 2a 20 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  .** MovetoUnpack
185f8 65 64 28 29 20 74 6f 20 73 65 65 6b 20 63 75 72  ed() to seek cur
185f9 73 6f 72 20 70 43 75 72 20 74 6f 20 28 70 4b 65  sor pCur to (pKe
185fa 79 2c 20 6e 4b 65 79 29 20 68 61 73 20 61 6c 72  y, nKey) has alr
185fb 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70 65 72  eady.** been per
185fc 66 6f 72 6d 65 64 2e 20 73 65 65 6b 52 65 73 75  formed. seekResu
185fd 6c 74 20 69 73 20 74 68 65 20 73 65 61 72 63 68  lt is the search
185fe 20 72 65 73 75 6c 74 20 72 65 74 75 72 6e 65 64   result returned
185ff 20 28 61 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20   (a negative.** 
18600 6e 75 6d 62 65 72 20 69 66 20 70 43 75 72 20 70  number if pCur p
18601 6f 69 6e 74 73 20 61 74 20 61 6e 20 65 6e 74 72  oints at an entr
18602 79 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 65  y that is smalle
18603 72 20 74 68 61 6e 20 28 70 4b 65 79 2c 20 6e 4b  r than (pKey, nK
18604 65 79 29 2c 20 6f 72 0a 2a 2a 20 61 20 70 6f 73  ey), or.** a pos
18605 69 74 69 76 65 20 76 61 6c 75 65 20 69 66 20 70  itive value if p
18606 43 75 72 20 70 6f 69 6e 74 73 20 61 74 20 61 6e  Cur points at an
18607 20 65 74 72 79 20 74 68 61 74 20 69 73 20 6c 61   etry that is la
18608 72 67 65 72 20 74 68 61 6e 20 0a 2a 2a 20 28 70  rger than .** (p
18609 4b 65 79 2c 20 6e 4b 65 79 29 29 2e 20 0a 2a 2a  Key, nKey)). .**
1860a 0a 2a 2a 20 49 66 20 74 68 65 20 73 65 65 6b 52  .** If the seekR
1860b 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20  esult parameter 
1860c 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
1860d 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 67 75 61  n the caller gua
1860e 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a 2a 20  rantees that.** 
1860f 63 75 72 73 6f 72 20 70 43 75 72 20 69 73 20 70  cursor pCur is p
18610 6f 69 6e 74 69 6e 67 20 61 74 20 74 68 65 20 65  ointing at the e
18611 78 69 73 74 69 6e 67 20 63 6f 70 79 20 6f 66 20  xisting copy of 
18612 61 20 72 6f 77 20 74 68 61 74 20 69 73 20 74 6f  a row that is to
18613 20 62 65 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74   be.** overwritt
18614 65 6e 2e 20 20 49 66 20 74 68 65 20 73 65 65 6b  en.  If the seek
18615 52 65 73 75 6c 74 20 70 61 72 61 6d 65 74 65 72  Result parameter
18616 20 69 73 20 30 2c 20 74 68 65 6e 20 63 75 72 73   is 0, then curs
18617 6f 72 20 70 43 75 72 20 6d 61 79 0a 2a 2a 20 70  or pCur may.** p
18618 6f 69 6e 74 20 74 6f 20 61 6e 79 20 65 6e 74 72  oint to any entr
18619 79 20 6f 72 20 74 6f 20 6e 6f 20 65 6e 74 72 79  y or to no entry
1861a 20 61 74 20 61 6c 6c 20 61 6e 64 20 73 6f 20 74   at all and so t
1861b 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 68 61 73  his function has
1861c 20 74 6f 20 73 65 65 6b 0a 2a 2a 20 74 68 65 20   to seek.** the 
1861d 63 75 72 73 6f 72 20 62 65 66 6f 72 65 20 74 68  cursor before th
1861e 65 20 6e 65 77 20 6b 65 79 20 63 61 6e 20 62 65  e new key can be
1861f 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51   inserted..*/.SQ
18620 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
18621 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73   sqlite3BtreeIns
18622 65 72 74 28 0a 20 20 42 74 43 75 72 73 6f 72 20  ert(.  BtCursor 
18623 2a 70 43 75 72 2c 20 20 20 20 20 20 20 20 20 20  *pCur,          
18624 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20        /* Insert 
18625 64 61 74 61 20 69 6e 74 6f 20 74 68 65 20 74 61  data into the ta
18626 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75 72 73  ble of this curs
18627 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  or */.  const vo
18628 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b  id *pKey, i64 nK
18629 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b 65  ey,    /* The ke
1862a 79 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63  y of the new rec
1862b 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ord */.  const v
1862c 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  oid *pData, int 
1862d 6e 44 61 74 61 2c 20 20 2f 2a 20 54 68 65 20 64  nData,  /* The d
1862e 61 74 61 20 6f 66 20 74 68 65 20 6e 65 77 20 72  ata of the new r
1862f 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
18630 5a 65 72 6f 2c 20 20 20 20 20 20 20 20 20 20 20  Zero,           
18631 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
18632 62 65 72 20 6f 66 20 65 78 74 72 61 20 30 20 62  ber of extra 0 b
18633 79 74 65 73 20 74 6f 20 61 70 70 65 6e 64 20 74  ytes to append t
18634 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20  o data */.  int 
18635 61 70 70 65 6e 64 42 69 61 73 2c 20 20 20 20 20  appendBias,     
18636 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
18637 75 65 20 69 66 20 74 68 69 73 20 69 73 20 6c 69  ue if this is li
18638 6b 65 6c 79 20 61 6e 20 61 70 70 65 6e 64 20 2a  kely an append *
18639 2f 0a 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75  /.  int seekResu
1863a 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  lt              
1863b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20     /* Result of 
1863c 70 72 69 6f 72 20 4d 6f 76 65 74 6f 55 6e 70 61  prior MovetoUnpa
1863d 63 6b 65 64 28 29 20 63 61 6c 6c 20 2a 2f 0a 29  cked() call */.)
1863e 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e  {.  int rc;.  in
1863f 74 20 6c 6f 63 20 3d 20 73 65 65 6b 52 65 73 75  t loc = seekResu
18640 6c 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  lt;          /* 
18641 2d 31 3a 20 62 65 66 6f 72 65 20 64 65 73 69 72  -1: before desir
18642 65 64 20 6c 6f 63 61 74 69 6f 6e 20 20 2b 31 3a  ed location  +1:
18643 20 61 66 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20   after */.  int 
18644 73 7a 4e 65 77 20 3d 20 30 3b 0a 20 20 69 6e 74  szNew = 0;.  int
18645 20 69 64 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20   idx;.  MemPage 
18646 2a 70 50 61 67 65 3b 0a 20 20 42 74 72 65 65 20  *pPage;.  Btree 
18647 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65  *p = pCur->pBtre
18648 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e;.  BtShared *p
18649 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 75  Bt = p->pBt;.  u
1864a 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6f 6c  nsigned char *ol
1864b 64 43 65 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e 65  dCell;.  unsigne
1864c 64 20 63 68 61 72 20 2a 6e 65 77 43 65 6c 6c 20  d char *newCell 
1864d 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 43 75 72  = 0;..  if( pCur
1864e 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
1864f 5f 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 61 73  _FAULT ){.    as
18650 73 65 72 74 28 20 70 43 75 72 2d 3e 73 6b 69 70  sert( pCur->skip
18651 4e 65 78 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  Next!=SQLITE_OK 
18652 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 43  );.    return pC
18653 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20  ur->skipNext;.  
18654 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  }..  assert( cur
18655 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
18656 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
18657 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26   pCur->wrFlag &&
18658 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
18659 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
1865a 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e   && !pBt->readOn
1865b 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ly );.  assert( 
1865c 68 61 73 53 68 61 72 65 64 43 61 63 68 65 54 61  hasSharedCacheTa
1865d 62 6c 65 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d  bleLock(p, pCur-
1865e 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d  >pgnoRoot, pCur-
1865f 3e 70 4b 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29  >pKeyInfo!=0, 2)
18660 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74   );..  /* Assert
18661 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72   that the caller
18662 20 68 61 73 20 62 65 65 6e 20 63 6f 6e 73 69 73   has been consis
18663 74 65 6e 74 2e 20 49 66 20 74 68 69 73 20 63 75  tent. If this cu
18664 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 0a  rsor was opened.
18665 20 20 2a 2a 20 65 78 70 65 63 74 69 6e 67 20 61    ** expecting a
18666 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2c 20  n index b-tree, 
18667 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20  then the caller 
18668 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72 74  should be insert
18669 69 6e 67 20 62 6c 6f 62 0a 20 20 2a 2a 20 6b 65  ing blob.  ** ke
1866a 79 73 20 77 69 74 68 20 6e 6f 20 61 73 73 6f 63  ys with no assoc
1866b 69 61 74 65 64 20 64 61 74 61 2e 20 49 66 20 74  iated data. If t
1866c 68 65 20 63 75 72 73 6f 72 20 77 61 73 20 6f 70  he cursor was op
1866d 65 6e 65 64 20 65 78 70 65 63 74 69 6e 67 20 61  ened expecting a
1866e 6e 0a 20 20 2a 2a 20 69 6e 74 6b 65 79 20 74 61  n.  ** intkey ta
1866f 62 6c 65 2c 20 74 68 65 20 63 61 6c 6c 65 72 20  ble, the caller 
18670 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72 74  should be insert
18671 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 73  ing integer keys
18672 20 77 69 74 68 20 61 0a 20 20 2a 2a 20 62 6c 6f   with a.  ** blo
18673 62 20 6f 66 20 61 73 73 6f 63 69 61 74 65 64 20  b of associated 
18674 64 61 74 61 2e 20 20 2a 2f 0a 20 20 61 73 73 65  data.  */.  asse
18675 72 74 28 20 28 70 4b 65 79 3d 3d 30 29 3d 3d 28  rt( (pKey==0)==(
18676 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d  pCur->pKeyInfo==
18677 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  0) );..  /* If t
18678 68 69 73 20 69 73 20 61 6e 20 69 6e 73 65 72 74  his is an insert
18679 20 69 6e 74 6f 20 61 20 74 61 62 6c 65 20 62 2d   into a table b-
1867a 74 72 65 65 2c 20 69 6e 76 61 6c 69 64 61 74 65  tree, invalidate
1867b 20 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 0a 20   any incrblob . 
1867c 20 2a 2a 20 63 75 72 73 6f 72 73 20 6f 70 65 6e   ** cursors open
1867d 20 6f 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e   on the row bein
1867e 67 20 72 65 70 6c 61 63 65 64 20 28 61 73 73 75  g replaced (assu
1867f 6d 69 6e 67 20 74 68 69 73 20 69 73 20 61 20 72  ming this is a r
18680 65 70 6c 61 63 65 0a 20 20 2a 2a 20 6f 70 65 72  eplace.  ** oper
18681 61 74 69 6f 6e 20 2d 20 69 66 20 69 74 20 69 73  ation - if it is
18682 20 6e 6f 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77   not, the follow
18683 69 6e 67 20 69 73 20 61 20 6e 6f 2d 6f 70 29 2e  ing is a no-op).
18684 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d    */.  if( pCur-
18685 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b 0a  >pKeyInfo==0 ){.
18686 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e      invalidateIn
18687 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c  crblobCursors(p,
18688 20 6e 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a   nKey, 0);.  }..
18689 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70 6f    /* Save the po
1868a 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f  sitions of any o
1868b 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70 65  ther cursors ope
1868c 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e  n on this table.
1868d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 73 6f  .  **.  ** In so
1868e 6d 65 20 63 61 73 65 73 2c 20 74 68 65 20 63 61  me cases, the ca
1868f 6c 6c 20 74 6f 20 62 74 72 65 65 4d 6f 76 65 74  ll to btreeMovet
18690 6f 28 29 20 62 65 6c 6f 77 20 69 73 20 61 20 6e  o() below is a n
18691 6f 2d 6f 70 2e 20 46 6f 72 0a 20 20 2a 2a 20 65  o-op. For.  ** e
18692 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 69 6e 73  xample, when ins
18693 65 72 74 69 6e 67 20 64 61 74 61 20 69 6e 74 6f  erting data into
18694 20 61 20 74 61 62 6c 65 20 77 69 74 68 20 61 75   a table with au
18695 74 6f 2d 67 65 6e 65 72 61 74 65 64 20 69 6e 74  to-generated int
18696 65 67 65 72 0a 20 20 2a 2a 20 6b 65 79 73 2c 20  eger.  ** keys, 
18697 74 68 65 20 56 44 42 45 20 6c 61 79 65 72 20 69  the VDBE layer i
18698 6e 76 6f 6b 65 73 20 73 71 6c 69 74 65 33 42 74  nvokes sqlite3Bt
18699 72 65 65 4c 61 73 74 28 29 20 74 6f 20 66 69 67  reeLast() to fig
1869a 75 72 65 20 6f 75 74 20 74 68 65 20 0a 20 20 2a  ure out the .  *
1869b 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20 74 6f  * integer key to
1869c 20 75 73 65 2e 20 49 74 20 74 68 65 6e 20 63 61   use. It then ca
1869d 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f  lls this functio
1869e 6e 20 74 6f 20 61 63 74 75 61 6c 6c 79 20 69 6e  n to actually in
1869f 73 65 72 74 20 74 68 65 20 0a 20 20 2a 2a 20 64  sert the .  ** d
186a0 61 74 61 20 69 6e 74 6f 20 74 68 65 20 69 6e 74  ata into the int
186a1 6b 65 79 20 42 2d 54 72 65 65 2e 20 49 6e 20 74  key B-Tree. In t
186a2 68 69 73 20 63 61 73 65 20 62 74 72 65 65 4d 6f  his case btreeMo
186a3 76 65 74 6f 28 29 20 72 65 63 6f 67 6e 69 7a 65  veto() recognize
186a4 73 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20  s.  ** that the 
186a5 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64  cursor is alread
186a6 79 20 77 68 65 72 65 20 69 74 20 6e 65 65 64 73  y where it needs
186a7 20 74 6f 20 62 65 20 61 6e 64 20 72 65 74 75 72   to be and retur
186a8 6e 73 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20  ns without.  ** 
186a9 64 6f 69 6e 67 20 61 6e 79 20 77 6f 72 6b 2e 20  doing any work. 
186aa 54 6f 20 61 76 6f 69 64 20 74 68 77 61 72 74 69  To avoid thwarti
186ab 6e 67 20 74 68 65 73 65 20 6f 70 74 69 6d 69 7a  ng these optimiz
186ac 61 74 69 6f 6e 73 2c 20 69 74 20 69 73 20 69 6d  ations, it is im
186ad 70 6f 72 74 61 6e 74 0a 20 20 2a 2a 20 6e 6f 74  portant.  ** not
186ae 20 74 6f 20 63 6c 65 61 72 20 74 68 65 20 63 75   to clear the cu
186af 72 73 6f 72 20 68 65 72 65 2e 0a 20 20 2a 2f 0a  rsor here..  */.
186b0 20 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75    rc = saveAllCu
186b1 72 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d  rsors(pBt, pCur-
186b2 3e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29  >pgnoRoot, pCur)
186b3 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
186b4 75 72 6e 20 72 63 3b 0a 20 20 69 66 28 20 21 6c  urn rc;.  if( !l
186b5 6f 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62  oc ){.    rc = b
186b6 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c  treeMoveto(pCur,
186b7 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 61 70 70   pKey, nKey, app
186b8 65 6e 64 42 69 61 73 2c 20 26 6c 6f 63 29 3b 0a  endBias, &loc);.
186b9 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
186ba 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73  urn rc;.  }.  as
186bb 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
186bc 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
186bd 20 7c 7c 20 28 70 43 75 72 2d 3e 65 53 74 61 74   || (pCur->eStat
186be 65 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49  e==CURSOR_INVALI
186bf 44 20 26 26 20 6c 6f 63 29 20 29 3b 0a 0a 20 20  D && loc) );..  
186c0 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
186c1 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
186c2 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ];.  assert( pPa
186c3 67 65 2d 3e 69 6e 74 4b 65 79 20 7c 7c 20 6e 4b  ge->intKey || nK
186c4 65 79 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ey>=0 );.  asser
186c5 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c  t( pPage->leaf |
186c6 7c 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  | !pPage->intKey
186c7 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 22 49   );..  TRACE(("I
186c8 4e 53 45 52 54 3a 20 74 61 62 6c 65 3d 25 64 20  NSERT: table=%d 
186c9 6e 6b 65 79 3d 25 6c 6c 64 20 6e 64 61 74 61 3d  nkey=%lld ndata=
186ca 25 64 20 70 61 67 65 3d 25 64 20 25 73 5c 6e 22  %d page=%d %s\n"
186cb 2c 0a 20 20 20 20 20 20 20 20 20 20 70 43 75 72  ,.          pCur
186cc 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20 6e 4b 65 79  ->pgnoRoot, nKey
186cd 2c 20 6e 44 61 74 61 2c 20 70 50 61 67 65 2d 3e  , nData, pPage->
186ce 70 67 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20  pgno,.          
186cf 6c 6f 63 3d 3d 30 20 3f 20 22 6f 76 65 72 77 72  loc==0 ? "overwr
186d0 69 74 65 22 20 3a 20 22 6e 65 77 20 65 6e 74 72  ite" : "new entr
186d1 79 22 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  y"));.  assert( 
186d2 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b  pPage->isInit );
186d3 0a 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53  .  allocateTempS
186d4 70 61 63 65 28 70 42 74 29 3b 0a 20 20 6e 65 77  pace(pBt);.  new
186d5 43 65 6c 6c 20 3d 20 70 42 74 2d 3e 70 54 6d 70  Cell = pBt->pTmp
186d6 53 70 61 63 65 3b 0a 20 20 69 66 28 20 6e 65 77  Space;.  if( new
186d7 43 65 6c 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e  Cell==0 ) return
186d8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
186d9 20 72 63 20 3d 20 66 69 6c 6c 49 6e 43 65 6c 6c   rc = fillInCell
186da 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c 2c  (pPage, newCell,
186db 20 70 4b 65 79 2c 20 6e 4b 65 79 2c 20 70 44 61   pKey, nKey, pDa
186dc 74 61 2c 20 6e 44 61 74 61 2c 20 6e 5a 65 72 6f  ta, nData, nZero
186dd 2c 20 26 73 7a 4e 65 77 29 3b 0a 20 20 69 66 28  , &szNew);.  if(
186de 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69   rc ) goto end_i
186df 6e 73 65 72 74 3b 0a 20 20 61 73 73 65 72 74 28  nsert;.  assert(
186e0 20 73 7a 4e 65 77 3d 3d 63 65 6c 6c 53 69 7a 65   szNew==cellSize
186e1 50 74 72 28 70 50 61 67 65 2c 20 6e 65 77 43 65  Ptr(pPage, newCe
186e2 6c 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ll) );.  assert(
186e3 20 73 7a 4e 65 77 3c 3d 4d 58 5f 43 45 4c 4c 5f   szNew<=MX_CELL_
186e4 53 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 69  SIZE(pBt) );.  i
186e5 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78  dx = pCur->aiIdx
186e6 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
186e7 20 69 66 28 20 6c 6f 63 3d 3d 30 20 29 7b 0a 20   if( loc==0 ){. 
186e8 20 20 20 75 31 36 20 73 7a 4f 6c 64 3b 0a 20 20     u16 szOld;.  
186e9 20 20 61 73 73 65 72 74 28 20 69 64 78 3c 70 50    assert( idx<pP
186ea 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
186eb 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
186ec 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e  gerWrite(pPage->
186ed 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66  pDbPage);.    if
186ee 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 67 6f  ( rc ){.      go
186ef 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20  to end_insert;. 
186f0 20 20 20 7d 0a 20 20 20 20 6f 6c 64 43 65 6c 6c     }.    oldCell
186f1 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67   = findCell(pPag
186f2 65 2c 20 69 64 78 29 3b 0a 20 20 20 20 69 66 28  e, idx);.    if(
186f3 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
186f4 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 6e 65  .      memcpy(ne
186f5 77 43 65 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c 20  wCell, oldCell, 
186f6 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a  4);.    }.    sz
186f7 4f 6c 64 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  Old = cellSizePt
186f8 72 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c  r(pPage, oldCell
186f9 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61  );.    rc = clea
186fa 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 6f 6c 64  rCell(pPage, old
186fb 43 65 6c 6c 29 3b 0a 20 20 20 20 64 72 6f 70 43  Cell);.    dropC
186fc 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20  ell(pPage, idx, 
186fd 73 7a 4f 6c 64 2c 20 26 72 63 29 3b 0a 20 20 20  szOld, &rc);.   
186fe 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65   if( rc ) goto e
186ff 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 7d 65 6c  nd_insert;.  }el
18700 73 65 20 69 66 28 20 6c 6f 63 3c 30 20 26 26 20  se if( loc<0 && 
18701 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29  pPage->nCell>0 )
18702 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
18703 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 20  age->leaf );.   
18704 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61   idx = ++pCur->a
18705 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
18706 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
18707 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c  assert( pPage->l
18708 65 61 66 20 29 3b 0a 20 20 7d 0a 20 20 69 6e 73  eaf );.  }.  ins
18709 65 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69  ertCell(pPage, i
1870a 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20 73 7a 4e  dx, newCell, szN
1870b 65 77 2c 20 30 2c 20 30 2c 20 26 72 63 29 3b 0a  ew, 0, 0, &rc);.
1870c 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
1870d 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65  LITE_OK || pPage
1870e 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70 50 61  ->nCell>0 || pPa
1870f 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20  ge->nOverflow>0 
18710 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 65  );..  /* If no e
18711 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64  rror has occured
18712 20 61 6e 64 20 70 50 61 67 65 20 68 61 73 20 61   and pPage has a
18713 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2c  n overflow cell,
18714 20 63 61 6c 6c 20 62 61 6c 61 6e 63 65 28 29 20   call balance() 
18715 0a 20 20 2a 2a 20 74 6f 20 72 65 64 69 73 74 72  .  ** to redistr
18716 69 62 75 74 65 20 74 68 65 20 63 65 6c 6c 73 20  ibute the cells 
18717 77 69 74 68 69 6e 20 74 68 65 20 74 72 65 65 2e  within the tree.
18718 20 53 69 6e 63 65 20 62 61 6c 61 6e 63 65 28 29   Since balance()
18719 20 6d 61 79 20 6d 6f 76 65 0a 20 20 2a 2a 20 74   may move.  ** t
1871a 68 65 20 63 75 72 73 6f 72 2c 20 7a 65 72 6f 20  he cursor, zero 
1871b 74 68 65 20 42 74 43 75 72 73 6f 72 2e 69 6e 66  the BtCursor.inf
1871c 6f 2e 6e 53 69 7a 65 20 61 6e 64 20 42 74 43 75  o.nSize and BtCu
1871d 72 73 6f 72 2e 76 61 6c 69 64 4e 4b 65 79 0a 20  rsor.validNKey. 
1871e 20 2a 2a 20 76 61 72 69 61 62 6c 65 73 2e 0a 20   ** variables.. 
1871f 20 2a 2a 0a 20 20 2a 2a 20 50 72 65 76 69 6f 75   **.  ** Previou
18720 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  s versions of SQ
18721 4c 69 74 65 20 63 61 6c 6c 65 64 20 6d 6f 76 65  Lite called move
18722 54 6f 52 6f 6f 74 28 29 20 74 6f 20 6d 6f 76 65  ToRoot() to move
18723 20 74 68 65 20 63 75 72 73 6f 72 0a 20 20 2a 2a   the cursor.  **
18724 20 62 61 63 6b 20 74 6f 20 74 68 65 20 72 6f 6f   back to the roo
18725 74 20 70 61 67 65 20 61 73 20 62 61 6c 61 6e 63  t page as balanc
18726 65 28 29 20 75 73 65 64 20 74 6f 20 69 6e 76 61  e() used to inva
18727 6c 69 64 61 74 65 20 74 68 65 20 63 6f 6e 74 65  lidate the conte
18728 6e 74 73 0a 20 20 2a 2a 20 6f 66 20 42 74 43 75  nts.  ** of BtCu
18729 72 73 6f 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e  rsor.apPage[] an
1872a 64 20 42 74 43 75 72 73 6f 72 2e 61 69 49 64 78  d BtCursor.aiIdx
1872b 5b 5d 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 64  []. Instead of d
1872c 6f 69 6e 67 20 74 68 61 74 2c 0a 20 20 2a 2a 20  oing that,.  ** 
1872d 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20 73  set the cursor s
1872e 74 61 74 65 20 74 6f 20 22 69 6e 76 61 6c 69 64  tate to "invalid
1872f 22 2e 20 54 68 69 73 20 6d 61 6b 65 73 20 63 6f  ". This makes co
18730 6d 6d 6f 6e 20 69 6e 73 65 72 74 20 6f 70 65 72  mmon insert oper
18731 61 74 69 6f 6e 73 0a 20 20 2a 2a 20 73 6c 69 67  ations.  ** slig
18732 68 74 6c 79 20 66 61 73 74 65 72 2e 0a 20 20 2a  htly faster..  *
18733 2a 0a 20 20 2a 2a 20 54 68 65 72 65 20 69 73 20  *.  ** There is 
18734 61 20 73 75 62 74 6c 65 20 62 75 74 20 69 6d 70  a subtle but imp
18735 6f 72 74 61 6e 74 20 6f 70 74 69 6d 69 7a 61 74  ortant optimizat
18736 69 6f 6e 20 68 65 72 65 20 74 6f 6f 2e 20 57 68  ion here too. Wh
18737 65 6e 20 69 6e 73 65 72 74 69 6e 67 0a 20 20 2a  en inserting.  *
18738 2a 20 6d 75 6c 74 69 70 6c 65 20 72 65 63 6f 72  * multiple recor
18739 64 73 20 69 6e 74 6f 20 61 6e 20 69 6e 74 6b 65  ds into an intke
1873a 79 20 62 2d 74 72 65 65 20 75 73 69 6e 67 20 61  y b-tree using a
1873b 20 73 69 6e 67 6c 65 20 63 75 72 73 6f 72 20 28   single cursor (
1873c 61 73 20 63 61 6e 0a 20 20 2a 2a 20 68 61 70 70  as can.  ** happ
1873d 65 6e 20 77 68 69 6c 65 20 70 72 6f 63 65 73 73  en while process
1873e 69 6e 67 20 61 6e 20 22 49 4e 53 45 52 54 20 49  ing an "INSERT I
1873f 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 22 20  NTO ... SELECT" 
18740 73 74 61 74 65 6d 65 6e 74 29 2c 20 69 74 0a 20  statement), it. 
18741 20 2a 2a 20 69 73 20 61 64 76 61 6e 74 61 67 65   ** is advantage
18742 6f 75 73 20 74 6f 20 6c 65 61 76 65 20 74 68 65  ous to leave the
18743 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   cursor pointing
18744 20 74 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74   to the last ent
18745 72 79 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 62  ry in.  ** the b
18746 2d 74 72 65 65 20 69 66 20 70 6f 73 73 69 62 6c  -tree if possibl
18747 65 2e 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  e. If the cursor
18748 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
18749 67 20 74 6f 20 74 68 65 20 6c 61 73 74 0a 20 20  g to the last.  
1874a 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  ** entry in the 
1874b 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 65 20 6e  table, and the n
1874c 65 78 74 20 72 6f 77 20 69 6e 73 65 72 74 65 64  ext row inserted
1874d 20 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20   has an integer 
1874e 6b 65 79 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20  key.  ** larger 
1874f 74 68 61 6e 20 74 68 65 20 6c 61 72 67 65 73 74  than the largest
18750 20 65 78 69 73 74 69 6e 67 20 6b 65 79 2c 20 69   existing key, i
18751 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f  t is possible to
18752 20 69 6e 73 65 72 74 20 74 68 65 0a 20 20 2a 2a   insert the.  **
18753 20 72 6f 77 20 77 69 74 68 6f 75 74 20 73 65 65   row without see
18754 6b 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 2e  king the cursor.
18755 20 54 68 69 73 20 63 61 6e 20 62 65 20 61 20 62   This can be a b
18756 69 67 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 62  ig performance b
18757 6f 6f 73 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 75  oost..  */.  pCu
18758 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20  r->info.nSize = 
18759 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64  0;.  pCur->valid
1875a 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20  NKey = 0;.  if( 
1875b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1875c 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
1875d 77 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 61  w ){.    rc = ba
1875e 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a 0a 20 20  lance(pCur);..  
1875f 20 20 2f 2a 20 4d 75 73 74 20 6d 61 6b 65 20 73    /* Must make s
18760 75 72 65 20 6e 4f 76 65 72 66 6c 6f 77 20 69 73  ure nOverflow is
18761 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 65   reset to zero e
18762 76 65 6e 20 69 66 20 74 68 65 20 62 61 6c 61 6e  ven if the balan
18763 63 65 28 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c  ce().    ** fail
18764 73 2e 20 49 6e 74 65 72 6e 61 6c 20 64 61 74 61  s. Internal data
18765 20 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 75   structure corru
18766 70 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c  ption will resul
18767 74 20 6f 74 68 65 72 77 69 73 65 2e 20 0a 20 20  t otherwise. .  
18768 20 20 2a 2a 20 41 6c 73 6f 2c 20 73 65 74 20 74    ** Also, set t
18769 68 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 20  he cursor state 
1876a 74 6f 20 69 6e 76 61 6c 69 64 2e 20 54 68 69 73  to invalid. This
1876b 20 73 74 6f 70 73 20 73 61 76 65 43 75 72 73 6f   stops saveCurso
1876c 72 50 6f 73 69 74 69 6f 6e 28 29 0a 20 20 20 20  rPosition().    
1876d 2a 2a 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 74  ** from trying t
1876e 6f 20 73 61 76 65 20 74 68 65 20 63 75 72 72 65  o save the curre
1876f 6e 74 20 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74  nt position of t
18770 68 65 20 63 75 72 73 6f 72 2e 20 20 2a 2f 0a 20  he cursor.  */. 
18771 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b     pCur->apPage[
18772 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f  pCur->iPage]->nO
18773 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20  verflow = 0;.   
18774 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20   pCur->eState = 
18775 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a  CURSOR_INVALID;.
18776 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43    }.  assert( pC
18777 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
18778 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 6c  >iPage]->nOverfl
18779 6f 77 3d 3d 30 20 29 3b 0a 0a 65 6e 64 5f 69 6e  ow==0 );..end_in
1877a 73 65 72 74 3a 0a 20 20 72 65 74 75 72 6e 20 72  sert:.  return r
1877b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  c;.}../*.** Dele
1877c 74 65 20 74 68 65 20 65 6e 74 72 79 20 74 68 61  te the entry tha
1877d 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  t the cursor is 
1877e 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68  pointing to.  Th
1877f 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c  e cursor.** is l
18780 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  eft pointing at 
18781 61 20 61 72 62 69 74 72 61 72 79 20 6c 6f 63 61  a arbitrary loca
18782 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
18783 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
18784 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 42  te3BtreeDelete(B
18785 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
18786 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75    Btree *p = pCu
18787 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53  r->pBtree;.  BtS
18788 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
18789 70 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  pBt;            
1878a 20 20 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20    .  int rc;    
1878b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1878c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
1878d 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 4d 65  urn code */.  Me
1878e 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20  mPage *pPage;   
1878f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18790 20 20 20 2f 2a 20 50 61 67 65 20 74 6f 20 64 65     /* Page to de
18791 6c 65 74 65 20 63 65 6c 6c 20 66 72 6f 6d 20 2a  lete cell from *
18792 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
18793 72 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20 20  r *pCell;       
18794 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
18795 74 65 72 20 74 6f 20 63 65 6c 6c 20 74 6f 20 64  ter to cell to d
18796 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74 20 69  elete */.  int i
18797 43 65 6c 6c 49 64 78 3b 20 20 20 20 20 20 20 20  CellIdx;        
18798 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18799 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 65 6c 6c  /* Index of cell
1879a 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
1879b 69 6e 74 20 69 43 65 6c 6c 44 65 70 74 68 3b 20  int iCellDepth; 
1879c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1879d 20 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f 66       /* Depth of
1879e 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 69 6e 67   node containing
1879f 20 70 43 65 6c 6c 20 2a 2f 20 0a 0a 20 20 61 73   pCell */ ..  as
187a0 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
187a1 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
187a2 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69    assert( pBt->i
187a3 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52  nTransaction==TR
187a4 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 61  ANS_WRITE );.  a
187a5 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72 65 61  ssert( !pBt->rea
187a6 64 4f 6e 6c 79 20 29 3b 0a 20 20 61 73 73 65 72  dOnly );.  asser
187a7 74 28 20 70 43 75 72 2d 3e 77 72 46 6c 61 67 20  t( pCur->wrFlag 
187a8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61 73  );.  assert( has
187a9 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
187aa 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d 3e 70 67  Lock(p, pCur->pg
187ab 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d 3e 70 4b  noRoot, pCur->pK
187ac 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 20 29 3b  eyInfo!=0, 2) );
187ad 0a 20 20 61 73 73 65 72 74 28 20 21 68 61 73 52  .  assert( !hasR
187ae 65 61 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20  eadConflicts(p, 
187af 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 29 20  pCur->pgnoRoot) 
187b0 29 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28  );..  if( NEVER(
187b1 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
187b2 2d 3e 69 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e  ->iPage]>=pCur->
187b3 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
187b4 67 65 5d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 20 20  ge]->nCell) .   
187b5 7c 7c 20 4e 45 56 45 52 28 70 43 75 72 2d 3e 65  || NEVER(pCur->e
187b6 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41  State!=CURSOR_VA
187b7 4c 49 44 29 0a 20 20 29 7b 0a 20 20 20 20 72 65  LID).  ){.    re
187b8 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
187b9 52 3b 20 20 2f 2a 20 53 6f 6d 65 74 68 69 6e 67  R;  /* Something
187ba 20 68 61 73 20 67 6f 6e 65 20 61 77 72 79 2e 20   has gone awry. 
187bb 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  */.  }..  /* If 
187bc 74 68 69 73 20 69 73 20 61 20 64 65 6c 65 74 65  this is a delete
187bd 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 72 65   operation to re
187be 6d 6f 76 65 20 61 20 72 6f 77 20 66 72 6f 6d 20  move a row from 
187bf 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 2c 0a  a table b-tree,.
187c0 20 20 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65 20    ** invalidate 
187c1 61 6e 79 20 69 6e 63 72 62 6c 6f 62 20 63 75 72  any incrblob cur
187c2 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65  sors open on the
187c3 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74   row being delet
187c4 65 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70 43  ed.  */.  if( pC
187c5 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20  ur->pKeyInfo==0 
187c6 29 7b 0a 20 20 20 20 69 6e 76 61 6c 69 64 61 74  ){.    invalidat
187c7 65 49 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73  eIncrblobCursors
187c8 28 70 2c 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  (p, pCur->info.n
187c9 4b 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  Key, 0);.  }..  
187ca 69 43 65 6c 6c 44 65 70 74 68 20 3d 20 70 43 75  iCellDepth = pCu
187cb 72 2d 3e 69 50 61 67 65 3b 0a 20 20 69 43 65 6c  r->iPage;.  iCel
187cc 6c 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49  lIdx = pCur->aiI
187cd 64 78 5b 69 43 65 6c 6c 44 65 70 74 68 5d 3b 0a  dx[iCellDepth];.
187ce 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e    pPage = pCur->
187cf 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 70 74  apPage[iCellDept
187d0 68 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69  h];.  pCell = fi
187d1 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  ndCell(pPage, iC
187d2 65 6c 6c 49 64 78 29 3b 0a 0a 20 20 2f 2a 20 49  ellIdx);..  /* I
187d3 66 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 61  f the page conta
187d4 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 79 20  ining the entry 
187d5 74 6f 20 64 65 6c 65 74 65 20 69 73 20 6e 6f 74  to delete is not
187d6 20 61 20 6c 65 61 66 20 70 61 67 65 2c 20 6d 6f   a leaf page, mo
187d7 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 73  ve.  ** the curs
187d8 6f 72 20 74 6f 20 74 68 65 20 6c 61 72 67 65 73  or to the larges
187d9 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74  t entry in the t
187da 72 65 65 20 74 68 61 74 20 69 73 20 73 6d 61 6c  ree that is smal
187db 6c 65 72 20 74 68 61 6e 0a 20 20 2a 2a 20 74 68  ler than.  ** th
187dc 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20 64 65  e entry being de
187dd 6c 65 74 65 64 2e 20 54 68 69 73 20 63 65 6c 6c  leted. This cell
187de 20 77 69 6c 6c 20 72 65 70 6c 61 63 65 20 74 68   will replace th
187df 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c  e cell being del
187e0 65 74 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74  eted.  ** from t
187e1 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65  he internal node
187e2 2e 20 54 68 65 20 27 70 72 65 76 69 6f 75 73 27  . The 'previous'
187e3 20 65 6e 74 72 79 20 69 73 20 75 73 65 64 20 66   entry is used f
187e4 6f 72 20 74 68 69 73 20 69 6e 73 74 65 61 64 0a  or this instead.
187e5 20 20 2a 2a 20 6f 66 20 74 68 65 20 27 6e 65 78    ** of the 'nex
187e6 74 27 20 65 6e 74 72 79 2c 20 61 73 20 74 68 65  t' entry, as the
187e7 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20   previous entry 
187e8 69 73 20 61 6c 77 61 79 73 20 61 20 70 61 72 74  is always a part
187e9 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 75 62   of the.  ** sub
187ea 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20  -tree headed by 
187eb 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 20 6f  the child page o
187ec 66 20 74 68 65 20 63 65 6c 6c 20 62 65 69 6e 67  f the cell being
187ed 20 64 65 6c 65 74 65 64 2e 20 54 68 69 73 20 6d   deleted. This m
187ee 61 6b 65 73 0a 20 20 2a 2a 20 62 61 6c 61 6e 63  akes.  ** balanc
187ef 69 6e 67 20 74 68 65 20 74 72 65 65 20 66 6f 6c  ing the tree fol
187f0 6c 6f 77 69 6e 67 20 74 68 65 20 64 65 6c 65 74  lowing the delet
187f1 65 20 6f 70 65 72 61 74 69 6f 6e 20 65 61 73 69  e operation easi
187f2 65 72 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21 70  er.  */.  if( !p
187f3 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
187f4 20 20 69 6e 74 20 6e 6f 74 55 73 65 64 3b 0a 20    int notUsed;. 
187f5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
187f6 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 75  treePrevious(pCu
187f7 72 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20  r, &notUsed);.  
187f8 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
187f9 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
187fa 53 61 76 65 20 74 68 65 20 70 6f 73 69 74 69 6f  Save the positio
187fb 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20  ns of any other 
187fc 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20  cursors open on 
187fd 74 68 69 73 20 74 61 62 6c 65 20 62 65 66 6f 72  this table befor
187fe 65 0a 20 20 2a 2a 20 6d 61 6b 69 6e 67 20 61 6e  e.  ** making an
187ff 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 2e  y modifications.
18800 20 4d 61 6b 65 20 74 68 65 20 70 61 67 65 20 63   Make the page c
18801 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 65 6e  ontaining the en
18802 74 72 79 20 74 6f 20 62 65 20 0a 20 20 2a 2a 20  try to be .  ** 
18803 64 65 6c 65 74 65 64 20 77 72 69 74 61 62 6c 65  deleted writable
18804 2e 20 54 68 65 6e 20 66 72 65 65 20 61 6e 79 20  . Then free any 
18805 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61  overflow pages a
18806 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
18807 68 65 20 0a 20 20 2a 2a 20 65 6e 74 72 79 20 61  he .  ** entry a
18808 6e 64 20 66 69 6e 61 6c 6c 79 20 72 65 6d 6f 76  nd finally remov
18809 65 20 74 68 65 20 63 65 6c 6c 20 69 74 73 65 6c  e the cell itsel
1880a 66 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68  f from within th
1880b 65 20 70 61 67 65 2e 20 20 0a 20 20 2a 2f 0a 20  e page.  .  */. 
1880c 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72   rc = saveAllCur
1880d 73 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e  sors(pBt, pCur->
1880e 70 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 3b  pgnoRoot, pCur);
1880f 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
18810 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71  rn rc;.  rc = sq
18811 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
18812 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b  pPage->pDbPage);
18813 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
18814 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 63 6c  rn rc;.  rc = cl
18815 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c 20 70  earCell(pPage, p
18816 43 65 6c 6c 29 3b 0a 20 20 64 72 6f 70 43 65 6c  Cell);.  dropCel
18817 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 49 64  l(pPage, iCellId
18818 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70  x, cellSizePtr(p
18819 50 61 67 65 2c 20 70 43 65 6c 6c 29 2c 20 26 72  Page, pCell), &r
1881a 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  c);.  if( rc ) r
1881b 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
1881c 49 66 20 74 68 65 20 63 65 6c 6c 20 64 65 6c 65  If the cell dele
1881d 74 65 64 20 77 61 73 20 6e 6f 74 20 6c 6f 63 61  ted was not loca
1881e 74 65 64 20 6f 6e 20 61 20 6c 65 61 66 20 70 61  ted on a leaf pa
1881f 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  ge, then the cur
18820 73 6f 72 0a 20 20 2a 2a 20 69 73 20 63 75 72 72  sor.  ** is curr
18821 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74  ently pointing t
18822 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e  o the largest en
18823 74 72 79 20 69 6e 20 74 68 65 20 73 75 62 2d 74  try in the sub-t
18824 72 65 65 20 68 65 61 64 65 64 0a 20 20 2a 2a 20  ree headed.  ** 
18825 62 79 20 74 68 65 20 63 68 69 6c 64 2d 70 61 67  by the child-pag
18826 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 74 68  e of the cell th
18827 61 74 20 77 61 73 20 6a 75 73 74 20 64 65 6c 65  at was just dele
18828 74 65 64 20 66 72 6f 6d 20 61 6e 20 69 6e 74 65  ted from an inte
18829 72 6e 61 6c 0a 20 20 2a 2a 20 6e 6f 64 65 2e 20  rnal.  ** node. 
1882a 54 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68  The cell from th
1882b 65 20 6c 65 61 66 20 6e 6f 64 65 20 6e 65 65 64  e leaf node need
1882c 73 20 74 6f 20 62 65 20 6d 6f 76 65 64 20 74 6f  s to be moved to
1882d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 20 20   the internal.  
1882e 2a 2a 20 6e 6f 64 65 20 74 6f 20 72 65 70 6c 61  ** node to repla
1882f 63 65 20 74 68 65 20 64 65 6c 65 74 65 64 20 63  ce the deleted c
18830 65 6c 6c 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21  ell.  */.  if( !
18831 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
18832 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 65 61     MemPage *pLea
18833 66 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  f = pCur->apPage
18834 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
18835 20 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20     int nCell;.  
18836 20 20 50 67 6e 6f 20 6e 20 3d 20 70 43 75 72 2d    Pgno n = pCur-
18837 3e 61 70 50 61 67 65 5b 69 43 65 6c 6c 44 65 70  >apPage[iCellDep
18838 74 68 2b 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20  th+1]->pgno;.   
18839 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
1883a 70 54 6d 70 3b 0a 0a 20 20 20 20 70 43 65 6c 6c  pTmp;..    pCell
1883b 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 4c 65 61   = findCell(pLea
1883c 66 2c 20 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d  f, pLeaf->nCell-
1883d 31 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20  1);.    nCell = 
1883e 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 4c 65 61  cellSizePtr(pLea
1883f 66 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 61  f, pCell);.    a
18840 73 73 65 72 74 28 20 4d 58 5f 43 45 4c 4c 5f 53  ssert( MX_CELL_S
18841 49 5a 45 28 70 42 74 29 3e 3d 6e 43 65 6c 6c 20  IZE(pBt)>=nCell 
18842 29 3b 0a 0a 20 20 20 20 61 6c 6c 6f 63 61 74 65  );..    allocate
18843 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a  TempSpace(pBt);.
18844 20 20 20 20 70 54 6d 70 20 3d 20 70 42 74 2d 3e      pTmp = pBt->
18845 70 54 6d 70 53 70 61 63 65 3b 0a 0a 20 20 20 20  pTmpSpace;..    
18846 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
18847 72 57 72 69 74 65 28 70 4c 65 61 66 2d 3e 70 44  rWrite(pLeaf->pD
18848 62 50 61 67 65 29 3b 0a 20 20 20 20 69 6e 73 65  bPage);.    inse
18849 72 74 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  rtCell(pPage, iC
1884a 65 6c 6c 49 64 78 2c 20 70 43 65 6c 6c 2d 34 2c  ellIdx, pCell-4,
1884b 20 6e 43 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c 20   nCell+4, pTmp, 
1884c 6e 2c 20 26 72 63 29 3b 0a 20 20 20 20 64 72 6f  n, &rc);.    dro
1884d 70 43 65 6c 6c 28 70 4c 65 61 66 2c 20 70 4c 65  pCell(pLeaf, pLe
1884e 61 66 2d 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43 65  af->nCell-1, nCe
1884f 6c 6c 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66  ll, &rc);.    if
18850 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
18851 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 61 6c 61  ;.  }..  /* Bala
18852 6e 63 65 20 74 68 65 20 74 72 65 65 2e 20 49 66  nce the tree. If
18853 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c 65 74   the entry delet
18854 65 64 20 77 61 73 20 6c 6f 63 61 74 65 64 20 6f  ed was located o
18855 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c 0a 20  n a leaf page,. 
18856 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 75 72   ** then the cur
18857 73 6f 72 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73  sor still points
18858 20 74 6f 20 74 68 61 74 20 70 61 67 65 2e 20 49   to that page. I
18859 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20  n this case the 
1885a 66 69 72 73 74 0a 20 20 2a 2a 20 63 61 6c 6c 20  first.  ** call 
1885b 74 6f 20 62 61 6c 61 6e 63 65 28 29 20 72 65 70  to balance() rep
1885c 61 69 72 73 20 74 68 65 20 74 72 65 65 2c 20 61  airs the tree, a
1885d 6e 64 20 74 68 65 20 69 66 28 2e 2e 2e 29 20 63  nd the if(...) c
1885e 6f 6e 64 69 74 69 6f 6e 20 69 73 0a 20 20 2a 2a  ondition is.  **
1885f 20 6e 65 76 65 72 20 74 72 75 65 2e 0a 20 20 2a   never true..  *
18860 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65  *.  ** Otherwise
18861 2c 20 69 66 20 74 68 65 20 65 6e 74 72 79 20 64  , if the entry d
18862 65 6c 65 74 65 64 20 77 61 73 20 6f 6e 20 61 6e  eleted was on an
18863 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 70   internal node p
18864 61 67 65 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 70  age, then.  ** p
18865 43 75 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20  Cur is pointing 
18866 74 6f 20 74 68 65 20 6c 65 61 66 20 70 61 67 65  to the leaf page
18867 20 66 72 6f 6d 20 77 68 69 63 68 20 61 20 63 65   from which a ce
18868 6c 6c 20 77 61 73 20 72 65 6d 6f 76 65 64 20 74  ll was removed t
18869 6f 0a 20 20 2a 2a 20 72 65 70 6c 61 63 65 20 74  o.  ** replace t
1886a 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 64 20  he cell deleted 
1886b 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 61  from the interna
1886c 6c 20 6e 6f 64 65 2e 20 54 68 69 73 20 69 73 20  l node. This is 
1886d 73 6c 69 67 68 74 6c 79 0a 20 20 2a 2a 20 74 72  slightly.  ** tr
1886e 69 63 6b 79 20 61 73 20 74 68 65 20 6c 65 61 66  icky as the leaf
1886f 20 6e 6f 64 65 20 6d 61 79 20 62 65 20 75 6e 64   node may be und
18870 65 72 66 75 6c 6c 2c 20 61 6e 64 20 74 68 65 20  erfull, and the 
18871 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 20 6d 61  internal node ma
18872 79 0a 20 20 2a 2a 20 62 65 20 65 69 74 68 65 72  y.  ** be either
18873 20 75 6e 64 65 72 20 6f 72 20 6f 76 65 72 66 75   under or overfu
18874 6c 6c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ll. In this case
18875 20 72 75 6e 20 74 68 65 20 62 61 6c 61 6e 63 69   run the balanci
18876 6e 67 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20 2a  ng algorithm.  *
18877 2a 20 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f  * on the leaf no
18878 64 65 20 66 69 72 73 74 2e 20 49 66 20 74 68 65  de first. If the
18879 20 62 61 6c 61 6e 63 65 20 70 72 6f 63 65 65 64   balance proceed
1887a 73 20 66 61 72 20 65 6e 6f 75 67 68 20 75 70 20  s far enough up 
1887b 74 68 65 0a 20 20 2a 2a 20 74 72 65 65 20 74 68  the.  ** tree th
1887c 61 74 20 77 65 20 63 61 6e 20 62 65 20 73 75 72  at we can be sur
1887d 65 20 74 68 61 74 20 61 6e 79 20 70 72 6f 62 6c  e that any probl
1887e 65 6d 20 69 6e 20 74 68 65 20 69 6e 74 65 72 6e  em in the intern
1887f 61 6c 20 6e 6f 64 65 20 68 61 73 0a 20 20 2a 2a  al node has.  **
18880 20 62 65 65 6e 20 63 6f 72 72 65 63 74 65 64 2c   been corrected,
18881 20 73 6f 20 62 65 20 69 74 2e 20 4f 74 68 65 72   so be it. Other
18882 77 69 73 65 2c 20 61 66 74 65 72 20 62 61 6c 61  wise, after bala
18883 6e 63 69 6e 67 20 74 68 65 20 6c 65 61 66 20 6e  ncing the leaf n
18884 6f 64 65 2c 0a 20 20 2a 2a 20 77 61 6c 6b 20 74  ode,.  ** walk t
18885 68 65 20 63 75 72 73 6f 72 20 75 70 20 74 68 65  he cursor up the
18886 20 74 72 65 65 20 74 6f 20 74 68 65 20 69 6e 74   tree to the int
18887 65 72 6e 61 6c 20 6e 6f 64 65 20 61 6e 64 20 62  ernal node and b
18888 61 6c 61 6e 63 65 20 69 74 20 61 73 20 0a 20 20  alance it as .  
18889 2a 2a 20 77 65 6c 6c 2e 20 20 2a 2f 0a 20 20 72  ** well.  */.  r
1888a 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72  c = balance(pCur
1888b 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1888c 49 54 45 5f 4f 4b 20 26 26 20 70 43 75 72 2d 3e  ITE_OK && pCur->
1888d 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70 74 68  iPage>iCellDepth
1888e 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 70   ){.    while( p
1888f 43 75 72 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c  Cur->iPage>iCell
18890 44 65 70 74 68 20 29 7b 0a 20 20 20 20 20 20 72  Depth ){.      r
18891 65 6c 65 61 73 65 50 61 67 65 28 70 43 75 72 2d  eleasePage(pCur-
18892 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
18893 61 67 65 2d 2d 5d 29 3b 0a 20 20 20 20 7d 0a 20  age--]);.    }. 
18894 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28     rc = balance(
18895 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  pCur);.  }..  if
18896 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
18897 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f  ){.    moveToRoo
18898 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 72  t(pCur);.  }.  r
18899 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
1889a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
1889b 42 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 72  BTree table.  Wr
1889c 69 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 6c  ite into *piTabl
1889d 65 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75  e the page.** nu
1889e 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f  mber for the roo
1889f 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65  t page of the ne
188a0 77 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  w table..**.** T
188a1 68 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 20  he type of type 
188a2 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
188a3 20 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d   the flags param
188a4 65 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 0a  eter.  Only the.
188a5 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c  ** following val
188a6 75 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 65  ues of flags are
188a7 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73   currently in us
188a8 65 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 73  e.  Other values
188a9 20 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d 69   for.** flags mi
188aa 67 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a 2a  ght not work:.**
188ab 0a 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 4e  .**     BTREE_IN
188ac 54 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 44  TKEY|BTREE_LEAFD
188ad 41 54 41 20 20 20 20 20 55 73 65 64 20 66 6f 72  ATA     Used for
188ae 20 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 68   SQL tables with
188af 20 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 20   rowid keys.**  
188b0 20 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54     BTREE_ZERODAT
188b1 41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  A               
188b2 20 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c 20     Used for SQL 
188b3 69 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 69  indices.*/.stati
188b4 63 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 74  c int btreeCreat
188b5 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c  eTable(Btree *p,
188b6 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 69   int *piTable, i
188b7 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 53  nt flags){.  BtS
188b8 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
188b9 70 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  pBt;.  MemPage *
188ba 70 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 67  pRoot;.  Pgno pg
188bb 6e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63  noRoot;.  int rc
188bc 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
188bd 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
188be 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65  tex(p) );.  asse
188bf 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73  rt( pBt->inTrans
188c0 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52  action==TRANS_WR
188c1 49 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ITE );.  assert(
188c2 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20   !pBt->readOnly 
188c3 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
188c4 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
188c5 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74  M.  rc = allocat
188c6 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
188c7 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f  &pRoot, &pgnoRoo
188c8 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 20  t, 1, 0);.  if( 
188c9 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  rc ){.    return
188ca 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20   rc;.  }.#else. 
188cb 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
188cc 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f  cuum ){.    Pgno
188cd 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 20   pgnoMove;      
188ce 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 68  /* Move a page h
188cf 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d  ere to make room
188d0 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61   for the root-pa
188d1 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67  ge */.    MemPag
188d2 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f 2a  e *pPageMove; /*
188d3 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f 76   The page to mov
188d4 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a  e to. */..    /*
188d5 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20   Creating a new 
188d6 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 62  table may probab
188d7 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e  ly require movin
188d8 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 61  g an existing da
188d9 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 6f  tabase.    ** to
188da 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74   make room for t
188db 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 6f  he new tables ro
188dc 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 65  ot page. In case
188dd 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e 73   this page turns
188de 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62  .    ** out to b
188df 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61  e an overflow pa
188e0 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f  ge, delete all o
188e1 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70  verflow page-map
188e2 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 68   caches.    ** h
188e3 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 73  eld by open curs
188e4 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ors..    */.    
188e5 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65  invalidateAllOve
188e6 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b  rflowCache(pBt);
188e7 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68  ..    /* Read th
188e8 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b  e value of meta[
188e9 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  3] from the data
188ea 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 6e  base to determin
188eb 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 20  e where the.    
188ec 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  ** root page of 
188ed 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 68  the new table sh
188ee 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 5d  ould go. meta[3]
188ef 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 20   is the largest 
188f0 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a  root-page.    **
188f1 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 2c   created so far,
188f2 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74   so the new root
188f3 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b 33  -page is (meta[3
188f4 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ]+1)..    */.   
188f5 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
188f6 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c 41  Meta(p, BTREE_LA
188f7 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 2c  RGEST_ROOT_PAGE,
188f8 20 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20   &pgnoRoot);.   
188f9 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20   pgnoRoot++;..  
188fa 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f    /* The new roo
188fb 74 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62  t-page may not b
188fc 65 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61  e allocated on a
188fd 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
188fe 65 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a  e, or the.    **
188ff 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 61   PENDING_BYTE pa
18900 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77  ge..    */.    w
18901 68 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d  hile( pgnoRoot==
18902 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42  PTRMAP_PAGENO(pB
18903 74 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a  t, pgnoRoot) ||.
18904 20 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74          pgnoRoot
18905 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  ==PENDING_BYTE_P
18906 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
18907 20 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20    pgnoRoot++;.  
18908 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
18909 70 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a  pgnoRoot>=3 );..
1890a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20      /* Allocate 
1890b 61 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65  a page. The page
1890c 20 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20   that currently 
1890d 72 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f 52  resides at pgnoR
1890e 6f 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20  oot will.    ** 
1890f 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20  be moved to the 
18910 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 28  allocated page (
18911 75 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f 63  unless the alloc
18912 61 74 65 64 20 70 61 67 65 20 68 61 70 70 65 6e  ated page happen
18913 73 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 69  s.    ** to resi
18914 64 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e  de at pgnoRoot).
18915 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
18916 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
18917 67 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d 6f  ge(pBt, &pPageMo
18918 76 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70  ve, &pgnoMove, p
18919 67 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20  gnoRoot, 1);.   
1891a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1891b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
1891c 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20  rn rc;.    }..  
1891d 20 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d    if( pgnoMove!=
1891e 70 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20  pgnoRoot ){.    
1891f 20 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73    /* pgnoRoot is
18920 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 77   the page that w
18921 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20  ill be used for 
18922 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66  the root-page of
18923 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65  .      ** the ne
18924 77 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69 6e  w table (assumin
18925 67 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6e  g an error did n
18926 6f 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20 77  ot occur). But w
18927 65 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a 20  e were.      ** 
18928 61 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f  allocated pgnoMo
18929 76 65 2e 20 49 66 20 72 65 71 75 69 72 65 64 20  ve. If required 
1892a 28 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20  (i.e. if it was 
1892b 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20  not allocated.  
1892c 20 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64      ** by extend
1892d 69 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74  ing the file), t
1892e 68 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20  he current page 
1892f 61 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f  at position pgno
18930 4d 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 73  Move.      ** is
18931 20 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c   already journal
18932 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
18933 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 30 3b     u8 eType = 0;
18934 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 50 74 72  .      Pgno iPtr
18935 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 20  Page = 0;..     
18936 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
18937 67 65 4d 6f 76 65 29 3b 0a 0a 20 20 20 20 20 20  geMove);..      
18938 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65  /* Move the page
18939 20 63 75 72 72 65 6e 74 6c 79 20 61 74 20 70 67   currently at pg
1893a 6e 6f 52 6f 6f 74 20 74 6f 20 70 67 6e 6f 4d 6f  noRoot to pgnoMo
1893b 76 65 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  ve. */.      rc 
1893c 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70  = btreeGetPage(p
1893d 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70  Bt, pgnoRoot, &p
1893e 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  Root, 0);.      
1893f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
18940 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  K ){.        ret
18941 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
18942 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
18943 70 47 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f  pGet(pBt, pgnoRo
18944 6f 74 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74  ot, &eType, &iPt
18945 72 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66  rPage);.      if
18946 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  ( eType==PTRMAP_
18947 52 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 70  ROOTPAGE || eTyp
18948 65 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41  e==PTRMAP_FREEPA
18949 47 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  GE ){.        rc
1894a 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
1894b 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a  T_BKPT;.      }.
1894c 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1894d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1894e 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1894f 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 72  Root);.        r
18950 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
18951 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
18952 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f  eType!=PTRMAP_RO
18953 4f 54 50 41 47 45 20 29 3b 0a 20 20 20 20 20 20  OTPAGE );.      
18954 61 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 50  assert( eType!=P
18955 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20 29  TRMAP_FREEPAGE )
18956 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 6c  ;.      rc = rel
18957 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20 70  ocatePage(pBt, p
18958 52 6f 6f 74 2c 20 65 54 79 70 65 2c 20 69 50 74  Root, eType, iPt
18959 72 50 61 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 2c  rPage, pgnoMove,
1895a 20 30 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61   0);.      relea
1895b 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 0a  sePage(pRoot);..
1895c 20 20 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20        /* Obtain 
1895d 74 68 65 20 70 61 67 65 20 61 74 20 70 67 6e 6f  the page at pgno
1895e 52 6f 6f 74 20 2a 2f 0a 20 20 20 20 20 20 69 66  Root */.      if
1895f 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
18960 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
18961 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
18962 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65      rc = btreeGe
18963 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 52  tPage(pBt, pgnoR
18964 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b  oot, &pRoot, 0);
18965 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
18966 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
18967 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
18968 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20       }.      rc 
18969 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1896a 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61  ite(pRoot->pDbPa
1896b 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ge);.      if( r
1896c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1896d 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
1896e 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20  age(pRoot);.    
1896f 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
18970 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
18971 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d 20  {.      pRoot = 
18972 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20 7d  pPageMove;.    }
18973 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65   ..    /* Update
18974 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
18975 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20 77   and meta-data w
18976 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f 74  ith the new root
18977 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a 2f  -page number. */
18978 0a 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70  .    ptrmapPut(p
18979 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 50 54  Bt, pgnoRoot, PT
1897a 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30  RMAP_ROOTPAGE, 0
1897b 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20  , &rc);.    if( 
1897c 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  rc ){.      rele
1897d 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a  asePage(pRoot);.
1897e 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1897f 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
18980 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64 61  sqlite3BtreeUpda
18981 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67 6e  teMeta(p, 4, pgn
18982 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28 20  oRoot);.    if( 
18983 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  rc ){.      rele
18984 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a  asePage(pRoot);.
18985 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
18986 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 7b  .    }..  }else{
18987 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61  .    rc = alloca
18988 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c  teBtreePage(pBt,
18989 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f   &pRoot, &pgnoRo
1898a 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69  ot, 1, 0);.    i
1898b 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1898c 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  c;.  }.#endif.  
1898d 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
1898e 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
1898f 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29 20  pRoot->pDbPage) 
18990 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70 52  );.  zeroPage(pR
18991 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54 46  oot, flags | PTF
18992 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74 65  _LEAF);.  sqlite
18993 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f 6f  3PagerUnref(pRoo
18994 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 2a  t->pDbPage);.  *
18995 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29 70  piTable = (int)p
18996 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75 72  gnoRoot;.  retur
18997 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53  n SQLITE_OK;.}.S
18998 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
18999 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72  t sqlite3BtreeCr
1899a 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65 20  eateTable(Btree 
1899b 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c 65  *p, int *piTable
1899c 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20  , int flags){.  
1899d 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
1899e 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
1899f 20 20 72 63 20 3d 20 62 74 72 65 65 43 72 65 61    rc = btreeCrea
189a0 74 65 54 61 62 6c 65 28 70 2c 20 70 69 54 61 62  teTable(p, piTab
189a1 6c 65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 73 71  le, flags);.  sq
189a2 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
189a3 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
189a4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20  .}../*.** Erase 
189a5 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62 61  the given databa
189a6 73 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c 20  se page and all 
189a7 69 74 73 20 63 68 69 6c 64 72 65 6e 2e 20 20 52  its children.  R
189a8 65 74 75 72 6e 0a 2a 2a 20 74 68 65 20 70 61 67  eturn.** the pag
189a9 65 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73  e to the freelis
189aa 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
189ab 20 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61   clearDatabasePa
189ac 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ge(.  BtShared *
189ad 70 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f  pBt,           /
189ae 2a 20 54 68 65 20 42 54 72 65 65 20 74 68 61 74  * The BTree that
189af 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 61   contains the ta
189b0 62 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67  ble */.  Pgno pg
189b1 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  no,             
189b2 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
189b3 20 74 6f 20 63 6c 65 61 72 20 2a 2f 0a 20 20 69   to clear */.  i
189b4 6e 74 20 66 72 65 65 50 61 67 65 46 6c 61 67 2c  nt freePageFlag,
189b5 20 20 20 20 20 20 20 20 2f 2a 20 44 65 61 6c 6c          /* Deall
189b6 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74 72  ocate page if tr
189b7 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43  ue */.  int *pnC
189b8 68 61 6e 67 65 20 20 20 20 20 20 20 20 20 20 20  hange           
189b9 20 2f 2a 20 41 64 64 20 6e 75 6d 62 65 72 20 6f   /* Add number o
189ba 66 20 43 65 6c 6c 73 20 66 72 65 65 64 20 74 6f  f Cells freed to
189bb 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 2a 2f   this counter */
189bc 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  .){.  MemPage *p
189bd 50 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Page;.  int rc;.
189be 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
189bf 2a 70 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b  *pCell;.  int i;
189c0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
189c1 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
189c2 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
189c3 69 66 28 20 70 67 6e 6f 3e 70 61 67 65 72 50 61  if( pgno>pagerPa
189c4 67 65 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a  gecount(pBt) ){.
189c5 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
189c6 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
189c7 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74 41    }..  rc = getA
189c8 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20  ndInitPage(pBt, 
189c9 70 67 6e 6f 2c 20 26 70 50 61 67 65 29 3b 0a 20  pgno, &pPage);. 
189ca 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
189cb 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20   rc;.  for(i=0; 
189cc 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20  i<pPage->nCell; 
189cd 69 2b 2b 29 7b 0a 20 20 20 20 70 43 65 6c 6c 20  i++){.    pCell 
189ce 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  = findCell(pPage
189cf 2c 20 69 29 3b 0a 20 20 20 20 69 66 28 20 21 70  , i);.    if( !p
189d0 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
189d1 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61      rc = clearDa
189d2 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20  tabasePage(pBt, 
189d3 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c  get4byte(pCell),
189d4 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20   1, pnChange);. 
189d5 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f       if( rc ) go
189d6 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73 65  to cleardatabase
189d7 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  page_out;.    }.
189d8 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 43 65      rc = clearCe
189d9 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29  ll(pPage, pCell)
189da 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 67  ;.    if( rc ) g
189db 6f 74 6f 20 63 6c 65 61 72 64 61 74 61 62 61 73  oto cleardatabas
189dc 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20  epage_out;.  }. 
189dd 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
189de 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c  f ){.    rc = cl
189df 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28  earDatabasePage(
189e0 70 42 74 2c 20 67 65 74 34 62 79 74 65 28 26 70  pBt, get4byte(&p
189e1 50 61 67 65 2d 3e 61 44 61 74 61 5b 38 5d 29 2c  Page->aData[8]),
189e2 20 31 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20   1, pnChange);. 
189e3 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f     if( rc ) goto
189e4 20 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61   cleardatabasepa
189e5 67 65 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 20  ge_out;.  }else 
189e6 69 66 28 20 70 6e 43 68 61 6e 67 65 20 29 7b 0a  if( pnChange ){.
189e7 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
189e8 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 20  e->intKey );.   
189e9 20 2a 70 6e 43 68 61 6e 67 65 20 2b 3d 20 70 50   *pnChange += pP
189ea 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 7d 0a  age->nCell;.  }.
189eb 20 20 69 66 28 20 66 72 65 65 50 61 67 65 46 6c    if( freePageFl
189ec 61 67 20 29 7b 0a 20 20 20 20 66 72 65 65 50 61  ag ){.    freePa
189ed 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b 0a  ge(pPage, &rc);.
189ee 20 20 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20    }else if( (rc 
189ef 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
189f0 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
189f1 67 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 7a  ge))==0 ){.    z
189f2 65 72 6f 50 61 67 65 28 70 50 61 67 65 2c 20 70  eroPage(pPage, p
189f3 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 7c  Page->aData[0] |
189f4 20 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20 7d 0a   PTF_LEAF);.  }.
189f5 0a 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61  .cleardatabasepa
189f6 67 65 5f 6f 75 74 3a 0a 20 20 72 65 6c 65 61 73  ge_out:.  releas
189f7 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
189f8 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
189f9 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 69  .** Delete all i
189fa 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
189fb 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69  a single table i
189fc 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
189fd 20 69 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68   iTable is.** th
189fe 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  e page number of
189ff 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65   the root of the
18a00 20 74 61 62 6c 65 2e 20 20 41 66 74 65 72 20 74   table.  After t
18a01 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
18a02 72 6e 73 2c 0a 2a 2a 20 74 68 65 20 72 6f 6f 74  rns,.** the root
18a03 20 70 61 67 65 20 69 73 20 65 6d 70 74 79 2c 20   page is empty, 
18a04 62 75 74 20 73 74 69 6c 6c 20 65 78 69 73 74 73  but still exists
18a05 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
18a06 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 77  tine will fail w
18a07 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  ith SQLITE_LOCKE
18a08 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  D if there are a
18a09 6e 79 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 20  ny open.** read 
18a0a 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20 74  cursors on the t
18a0b 61 62 6c 65 2e 20 20 4f 70 65 6e 20 77 72 69 74  able.  Open writ
18a0c 65 20 63 75 72 73 6f 72 73 20 61 72 65 20 6d 6f  e cursors are mo
18a0d 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20 72 6f  ved to the.** ro
18a0e 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e  ot of the table.
18a0f 0a 2a 2a 0a 2a 2a 20 49 66 20 70 6e 43 68 61 6e  .**.** If pnChan
18a10 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  ge is not NULL, 
18a11 74 68 65 6e 20 74 61 62 6c 65 20 69 54 61 62 6c  then table iTabl
18a12 65 20 6d 75 73 74 20 62 65 20 61 6e 20 69 6e 74  e must be an int
18a13 6b 65 79 20 74 61 62 6c 65 2e 20 54 68 65 0a 2a  key table. The.*
18a14 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20  * integer value 
18a15 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 6e  pointed to by pn
18a16 43 68 61 6e 67 65 20 69 73 20 69 6e 63 72 65 6d  Change is increm
18a17 65 6e 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d  ented by the num
18a18 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72 69 65  ber of.** entrie
18a19 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  s in the table..
18a1a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
18a1b 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
18a1c 65 65 43 6c 65 61 72 54 61 62 6c 65 28 42 74 72  eeClearTable(Btr
18a1d 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 6c  ee *p, int iTabl
18a1e 65 2c 20 69 6e 74 20 2a 70 6e 43 68 61 6e 67 65  e, int *pnChange
18a1f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 42  ){.  int rc;.  B
18a20 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
18a21 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33  ->pBt;.  sqlite3
18a22 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
18a23 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72   assert( p->inTr
18a24 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
18a25 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61 6c 69   );..  /* Invali
18a26 64 61 74 65 20 61 6c 6c 20 69 6e 63 72 62 6c 6f  date all incrblo
18a27 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f  b cursors open o
18a28 6e 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20 28  n table iTable (
18a29 61 73 73 75 6d 69 6e 67 20 69 54 61 62 6c 65 0a  assuming iTable.
18a2a 20 20 2a 2a 20 69 73 20 74 68 65 20 72 6f 6f 74    ** is the root
18a2b 20 6f 66 20 61 20 74 61 62 6c 65 20 62 2d 74 72   of a table b-tr
18a2c 65 65 20 2d 20 69 66 20 69 74 20 69 73 20 6e 6f  ee - if it is no
18a2d 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t, the following
18a2e 20 63 61 6c 6c 20 69 73 0a 20 20 2a 2a 20 61 20   call is.  ** a 
18a2f 6e 6f 2d 6f 70 29 2e 20 20 2a 2f 0a 20 20 69 6e  no-op).  */.  in
18a30 76 61 6c 69 64 61 74 65 49 6e 63 72 62 6c 6f 62  validateIncrblob
18a31 43 75 72 73 6f 72 73 28 70 2c 20 30 2c 20 31 29  Cursors(p, 0, 1)
18a32 3b 0a 0a 20 20 72 63 20 3d 20 73 61 76 65 41 6c  ;..  rc = saveAl
18a33 6c 43 75 72 73 6f 72 73 28 70 42 74 2c 20 28 50  lCursors(pBt, (P
18a34 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30 29 3b 0a  gno)iTable, 0);.
18a35 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
18a36 3d 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  =rc ){.    rc = 
18a37 63 6c 65 61 72 44 61 74 61 62 61 73 65 50 61 67  clearDatabasePag
18a38 65 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61  e(pBt, (Pgno)iTa
18a39 62 6c 65 2c 20 30 2c 20 70 6e 43 68 61 6e 67 65  ble, 0, pnChange
18a3a 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
18a3b 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
18a3c 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
18a3d 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c 20 69  *.** Erase all i
18a3e 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 61 20  nformation in a 
18a3f 74 61 62 6c 65 20 61 6e 64 20 61 64 64 20 74 68  table and add th
18a40 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61  e root of the ta
18a41 62 6c 65 20 74 6f 0a 2a 2a 20 74 68 65 20 66 72  ble to.** the fr
18a42 65 65 6c 69 73 74 2e 20 20 45 78 63 65 70 74 2c  eelist.  Except,
18a43 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65   the root of the
18a44 20 70 72 69 6e 63 69 70 6c 65 20 74 61 62 6c 65   principle table
18a45 20 28 74 68 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20   (the one on.** 
18a46 70 61 67 65 20 31 29 20 69 73 20 6e 65 76 65 72  page 1) is never
18a47 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66 72   added to the fr
18a48 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  eelist..**.** Th
18a49 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
18a4a 66 61 69 6c 20 77 69 74 68 20 53 51 4c 49 54 45  fail with SQLITE
18a4b 5f 4c 4f 43 4b 45 44 20 69 66 20 74 68 65 72 65  _LOCKED if there
18a4c 20 61 72 65 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a   are any open.**
18a4d 20 63 75 72 73 6f 72 73 20 6f 6e 20 74 68 65 20   cursors on the 
18a4e 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  table..**.** If 
18a4f 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e  AUTOVACUUM is en
18a50 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 70 61  abled and the pa
18a51 67 65 20 61 74 20 69 54 61 62 6c 65 20 69 73 20  ge at iTable is 
18a52 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20  not the last.** 
18a53 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65  root page in the
18a54 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
18a55 74 68 65 6e 20 74 68 65 20 6c 61 73 74 20 72 6f  then the last ro
18a56 6f 74 20 70 61 67 65 20 0a 2a 2a 20 69 6e 20 74  ot page .** in t
18a57 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
18a58 20 69 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74   is moved into t
18a59 68 65 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79  he slot formerly
18a5a 20 6f 63 63 75 70 69 65 64 20 62 79 0a 2a 2a 20   occupied by.** 
18a5b 69 54 61 62 6c 65 20 61 6e 64 20 74 68 61 74 20  iTable and that 
18a5c 6c 61 73 74 20 73 6c 6f 74 20 66 6f 72 6d 65 72  last slot former
18a5d 6c 79 20 6f 63 63 75 70 69 65 64 20 62 79 20 74  ly occupied by t
18a5e 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67  he last root pag
18a5f 65 0a 2a 2a 20 69 73 20 61 64 64 65 64 20 74 6f  e.** is added to
18a60 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 69 6e   the freelist in
18a61 73 74 65 61 64 20 6f 66 20 69 54 61 62 6c 65 2e  stead of iTable.
18a62 20 20 49 6e 20 74 68 69 73 20 73 61 79 2c 20 61    In this say, a
18a63 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73  ll.** root pages
18a64 20 61 72 65 20 6b 65 70 74 20 61 74 20 74 68 65   are kept at the
18a65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
18a66 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
18a67 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 6e 65 63   which.** is nec
18a68 65 73 73 61 72 79 20 66 6f 72 20 41 55 54 4f 56  essary for AUTOV
18a69 41 43 55 55 4d 20 74 6f 20 77 6f 72 6b 20 72 69  ACUUM to work ri
18a6a 67 68 74 2e 20 20 2a 70 69 4d 6f 76 65 64 20 69  ght.  *piMoved i
18a6b 73 20 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a  s set to the .**
18a6c 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 68 61   page number tha
18a6d 74 20 75 73 65 64 20 74 6f 20 62 65 20 74 68 65  t used to be the
18a6e 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20   last root page 
18a6f 69 6e 20 74 68 65 20 66 69 6c 65 20 62 65 66 6f  in the file befo
18a70 72 65 0a 2a 2a 20 74 68 65 20 6d 6f 76 65 2e 20  re.** the move. 
18a71 20 49 66 20 6e 6f 20 70 61 67 65 20 67 65 74 73   If no page gets
18a72 20 6d 6f 76 65 64 2c 20 2a 70 69 4d 6f 76 65 64   moved, *piMoved
18a73 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a   is set to 0..**
18a74 20 54 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70   The last root p
18a75 61 67 65 20 69 73 20 72 65 63 6f 72 64 65 64 20  age is recorded 
18a76 69 6e 20 6d 65 74 61 5b 33 5d 20 61 6e 64 20 74  in meta[3] and t
18a77 68 65 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d  he value of.** m
18a78 65 74 61 5b 33 5d 20 69 73 20 75 70 64 61 74 65  eta[3] is update
18a79 64 20 62 79 20 74 68 69 73 20 70 72 6f 63 65 64  d by this proced
18a7a 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ure..*/.static i
18a7b 6e 74 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c  nt btreeDropTabl
18a7c 65 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f  e(Btree *p, Pgno
18a7d 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69   iTable, int *pi
18a7e 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72 63  Moved){.  int rc
18a7f 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
18a80 67 65 20 3d 20 30 3b 0a 20 20 42 74 53 68 61 72  ge = 0;.  BtShar
18a81 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
18a82 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
18a83 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75  ite3BtreeHoldsMu
18a84 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73 73 65  tex(p) );.  asse
18a85 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  rt( p->inTrans==
18a86 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 0a  TRANS_WRITE );..
18a87 20 20 2f 2a 20 49 74 20 69 73 20 69 6c 6c 65 67    /* It is illeg
18a88 61 6c 20 74 6f 20 64 72 6f 70 20 61 20 74 61 62  al to drop a tab
18a89 6c 65 20 69 66 20 61 6e 79 20 63 75 72 73 6f 72  le if any cursor
18a8a 73 20 61 72 65 20 6f 70 65 6e 20 6f 6e 20 74 68  s are open on th
18a8b 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e  e.  ** database.
18a8c 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
18a8d 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   in auto-vacuum 
18a8e 6d 6f 64 65 20 74 68 65 20 62 61 63 6b 65 6e 64  mode the backend
18a8f 20 6d 61 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74   may.  ** need t
18a90 6f 20 6d 6f 76 65 20 61 6e 6f 74 68 65 72 20 72  o move another r
18a91 6f 6f 74 2d 70 61 67 65 20 74 6f 20 66 69 6c 6c  oot-page to fill
18a92 20 61 20 67 61 70 20 6c 65 66 74 20 62 79 20 74   a gap left by t
18a93 68 65 20 64 65 6c 65 74 65 64 0a 20 20 2a 2a 20  he deleted.  ** 
18a94 72 6f 6f 74 20 70 61 67 65 2e 20 49 66 20 61 6e  root page. If an
18a95 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 77 61 73   open cursor was
18a96 20 75 73 69 6e 67 20 74 68 69 73 20 70 61 67 65   using this page
18a97 20 61 20 70 72 6f 62 6c 65 6d 20 77 6f 75 6c 64   a problem would
18a98 20 0a 20 20 2a 2a 20 6f 63 63 75 72 2e 0a 20 20   .  ** occur..  
18a99 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 65 72 72  **.  ** This err
18a9a 6f 72 20 69 73 20 63 61 75 67 68 74 20 6c 6f 6e  or is caught lon
18a9b 67 20 62 65 66 6f 72 65 20 63 6f 6e 74 72 6f 6c  g before control
18a9c 20 72 65 61 63 68 65 73 20 74 68 69 73 20 70 6f   reaches this po
18a9d 69 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  int..  */.  if( 
18a9e 4e 45 56 45 52 28 70 42 74 2d 3e 70 43 75 72 73  NEVER(pBt->pCurs
18a9f 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  or) ){.    sqlit
18aa0 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63  e3ConnectionBloc
18aa1 6b 65 64 28 70 2d 3e 64 62 2c 20 70 42 74 2d 3e  ked(p->db, pBt->
18aa2 70 43 75 72 73 6f 72 2d 3e 70 42 74 72 65 65 2d  pCursor->pBtree-
18aa3 3e 64 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  >db);.    return
18aa4 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 5f 53   SQLITE_LOCKED_S
18aa5 48 41 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 0a  HAREDCACHE;.  }.
18aa6 0a 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74  .  rc = btreeGet
18aa7 50 61 67 65 28 70 42 74 2c 20 28 50 67 6e 6f 29  Page(pBt, (Pgno)
18aa8 69 54 61 62 6c 65 2c 20 26 70 50 61 67 65 2c 20  iTable, &pPage, 
18aa9 30 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  0);.  if( rc ) r
18aaa 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d  eturn rc;.  rc =
18aab 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
18aac 61 72 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c  arTable(p, iTabl
18aad 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20  e, 0);.  if( rc 
18aae 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61  ){.    releasePa
18aaf 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 72  ge(pPage);.    r
18ab0 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
18ab1 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a   *piMoved = 0;..
18ab2 20 20 69 66 28 20 69 54 61 62 6c 65 3e 31 20 29    if( iTable>1 )
18ab3 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
18ab4 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
18ab5 20 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61      freePage(pPa
18ab6 67 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 72 65  ge, &rc);.    re
18ab7 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
18ab8 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69 66 28 20  ;.#else.    if( 
18ab9 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
18aba 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6d 61  ){.      Pgno ma
18abb 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20 20 20 20  xRootPgno;.     
18abc 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
18abd 4d 65 74 61 28 70 2c 20 42 54 52 45 45 5f 4c 41  Meta(p, BTREE_LA
18abe 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 2c  RGEST_ROOT_PAGE,
18abf 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a   &maxRootPgno);.
18ac0 0a 20 20 20 20 20 20 69 66 28 20 69 54 61 62 6c  .      if( iTabl
18ac1 65 3d 3d 6d 61 78 52 6f 6f 74 50 67 6e 6f 20 29  e==maxRootPgno )
18ac2 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
18ac3 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
18ac4 64 72 6f 70 70 65 64 20 69 73 20 74 68 65 20 74  dropped is the t
18ac5 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 6c 61  able with the la
18ac6 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a  rgest root-page.
18ac7 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65          ** numbe
18ac8 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  r in the databas
18ac9 65 2c 20 70 75 74 20 74 68 65 20 72 6f 6f 74 20  e, put the root 
18aca 70 61 67 65 20 6f 6e 20 74 68 65 20 66 72 65 65  page on the free
18acb 20 6c 69 73 74 2e 20 0a 20 20 20 20 20 20 20 20   list. .        
18acc 2a 2f 0a 20 20 20 20 20 20 20 20 66 72 65 65 50  */.        freeP
18acd 61 67 65 28 70 50 61 67 65 2c 20 26 72 63 29 3b  age(pPage, &rc);
18ace 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
18acf 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
18ad0 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
18ad1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
18ad2 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
18ad3 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
18ad4 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
18ad5 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   The table being
18ad6 20 64 72 6f 70 70 65 64 20 64 6f 65 73 20 6e 6f   dropped does no
18ad7 74 20 68 61 76 65 20 74 68 65 20 6c 61 72 67 65  t have the large
18ad8 73 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20  st root-page.   
18ad9 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69       ** number i
18ada 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
18adb 53 6f 20 6d 6f 76 65 20 74 68 65 20 70 61 67 65  So move the page
18adc 20 74 68 61 74 20 64 6f 65 73 20 69 6e 74 6f 20   that does into 
18add 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
18ade 67 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20  gap left by the 
18adf 64 65 6c 65 74 65 64 20 72 6f 6f 74 2d 70 61 67  deleted root-pag
18ae0 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
18ae1 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70        MemPage *p
18ae2 4d 6f 76 65 3b 0a 20 20 20 20 20 20 20 20 72 65  Move;.        re
18ae3 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
18ae4 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 62  ;.        rc = b
18ae5 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
18ae6 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26 70   maxRootPgno, &p
18ae7 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Move, 0);.      
18ae8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
18ae9 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
18aea 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
18aeb 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63      }.        rc
18aec 20 3d 20 72 65 6c 6f 63 61 74 65 50 61 67 65 28   = relocatePage(
18aed 70 42 74 2c 20 70 4d 6f 76 65 2c 20 50 54 52 4d  pBt, pMove, PTRM
18aee 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20  AP_ROOTPAGE, 0, 
18aef 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 20 20  iTable, 0);.    
18af0 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
18af1 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20  pMove);.        
18af2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
18af3 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
18af4 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
18af5 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d 6f 76    }.        pMov
18af6 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72  e = 0;.        r
18af7 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
18af8 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e  (pBt, maxRootPgn
18af9 6f 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20  o, &pMove, 0);. 
18afa 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28         freePage(
18afb 70 4d 6f 76 65 2c 20 26 72 63 29 3b 0a 20 20 20  pMove, &rc);.   
18afc 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
18afd 28 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20  (pMove);.       
18afe 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
18aff 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
18b00 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
18b01 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 69     }.        *pi
18b02 4d 6f 76 65 64 20 3d 20 6d 61 78 52 6f 6f 74 50  Moved = maxRootP
18b03 67 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  gno;.      }..  
18b04 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6e      /* Set the n
18b05 65 77 20 27 6d 61 78 2d 72 6f 6f 74 2d 70 61 67  ew 'max-root-pag
18b06 65 27 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20  e' value in the 
18b07 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72 2e  database header.
18b08 20 54 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69   This.      ** i
18b09 73 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 20  s the old value 
18b0a 6c 65 73 73 20 6f 6e 65 2c 20 6c 65 73 73 20 6f  less one, less o
18b0b 6e 65 20 6d 6f 72 65 20 69 66 20 74 68 61 74 20  ne more if that 
18b0c 68 61 70 70 65 6e 73 20 74 6f 0a 20 20 20 20 20  happens to.     
18b0d 20 2a 2a 20 62 65 20 61 20 72 6f 6f 74 2d 70 61   ** be a root-pa
18b0e 67 65 20 6e 75 6d 62 65 72 2c 20 6c 65 73 73 20  ge number, less 
18b0f 6f 6e 65 20 61 67 61 69 6e 20 69 66 20 74 68 61  one again if tha
18b10 74 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 2a  t is the.      *
18b11 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  * PENDING_BYTE_P
18b12 41 47 45 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  AGE..      */.  
18b13 20 20 20 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d      maxRootPgno-
18b14 2d 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  -;.      while( 
18b15 6d 61 78 52 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e  maxRootPgno==PEN
18b16 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
18b17 42 74 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  Bt).            
18b18 20 7c 7c 20 50 54 52 4d 41 50 5f 49 53 50 41 47   || PTRMAP_ISPAG
18b19 45 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67  E(pBt, maxRootPg
18b1a 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  no) ){.        m
18b1b 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20  axRootPgno--;.  
18b1c 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
18b1d 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 21  rt( maxRootPgno!
18b1e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
18b1f 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20 20 20  GE(pBt) );..    
18b20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
18b21 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2c  reeUpdateMeta(p,
18b22 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29   4, maxRootPgno)
18b23 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
18b24 20 20 20 66 72 65 65 50 61 67 65 28 70 50 61 67     freePage(pPag
18b25 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 72  e, &rc);.      r
18b26 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65  eleasePage(pPage
18b27 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
18b28 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
18b29 49 66 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  If sqlite3BtreeD
18b2a 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63 61 6c  ropTable was cal
18b2b 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e 0a 20  led on page 1.. 
18b2c 20 20 20 2a 2a 20 54 68 69 73 20 72 65 61 6c 6c     ** This reall
18b2d 79 20 6e 65 76 65 72 20 73 68 6f 75 6c 64 20 68  y never should h
18b2e 61 70 70 65 6e 20 65 78 63 65 70 74 20 69 6e 20  appen except in 
18b2f 61 20 63 6f 72 72 75 70 74 0a 20 20 20 20 2a 2a  a corrupt.    **
18b30 20 64 61 74 61 62 61 73 65 2e 20 0a 20 20 20 20   database. .    
18b31 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28  */.    zeroPage(
18b32 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b 45  pPage, PTF_INTKE
18b33 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20 20  Y|PTF_LEAF );.  
18b34 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
18b35 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  age);.  }.  retu
18b36 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54  rn rc;  .}.SQLIT
18b37 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
18b38 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61  lite3BtreeDropTa
18b39 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ble(Btree *p, in
18b3a 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70  t iTable, int *p
18b3b 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72  iMoved){.  int r
18b3c 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  c;.  sqlite3Btre
18b3d 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20  eEnter(p);.  rc 
18b3e 3d 20 62 74 72 65 65 44 72 6f 70 54 61 62 6c 65  = btreeDropTable
18b3f 28 70 2c 20 69 54 61 62 6c 65 2c 20 70 69 4d 6f  (p, iTable, piMo
18b40 76 65 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  ved);.  sqlite3B
18b41 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
18b42 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
18b43 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
18b44 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63  on may only be c
18b45 61 6c 6c 65 64 20 69 66 20 74 68 65 20 62 2d 74  alled if the b-t
18b46 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61  ree connection a
18b47 6c 72 65 61 64 79 0a 2a 2a 20 68 61 73 20 61 20  lready.** has a 
18b48 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72  read or write tr
18b49 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f  ansaction open o
18b4a 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  n the database..
18b4b 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 6d  **.** Read the m
18b4c 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  eta-information 
18b4d 6f 75 74 20 6f 66 20 61 20 64 61 74 61 62 61 73  out of a databas
18b4e 65 20 66 69 6c 65 2e 20 20 4d 65 74 61 5b 30 5d  e file.  Meta[0]
18b4f 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65  .** is the numbe
18b50 72 20 6f 66 20 66 72 65 65 20 70 61 67 65 73 20  r of free pages 
18b51 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65  currently in the
18b52 20 64 61 74 61 62 61 73 65 2e 20 20 4d 65 74 61   database.  Meta
18b53 5b 31 5d 0a 2a 2a 20 74 68 72 6f 75 67 68 20 6d  [1].** through m
18b54 65 74 61 5b 31 35 5d 20 61 72 65 20 61 76 61 69  eta[15] are avai
18b55 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62 79  lable for use by
18b56 20 68 69 67 68 65 72 20 6c 61 79 65 72 73 2e 20   higher layers. 
18b57 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 72   Meta[0].** is r
18b58 65 61 64 2d 6f 6e 6c 79 2c 20 74 68 65 20 6f 74  ead-only, the ot
18b59 68 65 72 73 20 61 72 65 20 72 65 61 64 2f 77 72  hers are read/wr
18b5a 69 74 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20  ite..** .** The 
18b5b 73 63 68 65 6d 61 20 6c 61 79 65 72 20 6e 75 6d  schema layer num
18b5c 62 65 72 73 20 6d 65 74 61 20 76 61 6c 75 65 73  bers meta values
18b5d 20 64 69 66 66 65 72 65 6e 74 6c 79 2e 20 20 41   differently.  A
18b5e 74 20 74 68 65 20 73 63 68 65 6d 61 0a 2a 2a 20  t the schema.** 
18b5f 6c 61 79 65 72 20 28 61 6e 64 20 74 68 65 20 53  layer (and the S
18b60 65 74 43 6f 6f 6b 69 65 20 61 6e 64 20 52 65 61  etCookie and Rea
18b61 64 43 6f 6f 6b 69 65 20 6f 70 63 6f 64 65 73 29  dCookie opcodes)
18b62 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
18b63 2a 20 66 72 65 65 20 70 61 67 65 73 20 69 73 20  * free pages is 
18b64 6e 6f 74 20 76 69 73 69 62 6c 65 2e 20 20 53 6f  not visible.  So
18b65 20 43 6f 6f 6b 69 65 5b 30 5d 20 69 73 20 74 68   Cookie[0] is th
18b66 65 20 73 61 6d 65 20 61 73 20 4d 65 74 61 5b 31  e same as Meta[1
18b67 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ]..*/.SQLITE_PRI
18b68 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
18b69 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74  3BtreeGetMeta(Bt
18b6a 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c  ree *p, int idx,
18b6b 20 75 33 32 20 2a 70 4d 65 74 61 29 7b 0a 20 20   u32 *pMeta){.  
18b6c 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
18b6d 70 2d 3e 70 42 74 3b 0a 0a 20 20 73 71 6c 69 74  p->pBt;..  sqlit
18b6e 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
18b6f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e  .  assert( p->in
18b70 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45  Trans>TRANS_NONE
18b71 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 53 51   );.  assert( SQ
18b72 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 53 68  LITE_OK==querySh
18b73 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
18b74 63 6b 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f  ck(p, MASTER_ROO
18b75 54 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 20 29 3b  T, READ_LOCK) );
18b76 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e  .  assert( pBt->
18b77 70 50 61 67 65 31 20 29 3b 0a 20 20 61 73 73 65  pPage1 );.  asse
18b78 72 74 28 20 69 64 78 3e 3d 30 20 26 26 20 69 64  rt( idx>=0 && id
18b79 78 3c 3d 31 35 20 29 3b 0a 0a 20 20 2a 70 4d 65  x<=15 );..  *pMe
18b7a 74 61 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  ta = get4byte(&p
18b7b 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
18b7c 61 5b 33 36 20 2b 20 69 64 78 2a 34 5d 29 3b 0a  a[36 + idx*4]);.
18b7d 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f 2d 76 61  .  /* If auto-va
18b7e 63 75 75 6d 20 69 73 20 64 69 73 61 62 6c 65 64  cuum is disabled
18b7f 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 20 61   in this build a
18b80 6e 64 20 74 68 69 73 20 69 73 20 61 6e 20 61 75  nd this is an au
18b81 74 6f 2d 76 61 63 75 75 6d 0a 20 20 2a 2a 20 64  to-vacuum.  ** d
18b82 61 74 61 62 61 73 65 2c 20 6d 61 72 6b 20 74 68  atabase, mark th
18b83 65 20 64 61 74 61 62 61 73 65 20 61 73 20 72 65  e database as re
18b84 61 64 2d 6f 6e 6c 79 2e 20 20 2a 2f 0a 23 69 66  ad-only.  */.#if
18b85 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18b86 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28  AUTOVACUUM.  if(
18b87 20 69 64 78 3d 3d 42 54 52 45 45 5f 4c 41 52 47   idx==BTREE_LARG
18b88 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20 26 26  EST_ROOT_PAGE &&
18b89 20 2a 70 4d 65 74 61 3e 30 20 29 20 70 42 74 2d   *pMeta>0 ) pBt-
18b8a 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23  >readOnly = 1;.#
18b8b 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
18b8c 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 7d  BtreeLeave(p);.}
18b8d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 65  ../*.** Write me
18b8e 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 62  ta-information b
18b8f 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74  ack into the dat
18b90 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 30 5d 20  abase.  Meta[0] 
18b91 69 73 0a 2a 2a 20 72 65 61 64 2d 6f 6e 6c 79 20  is.** read-only 
18b92 61 6e 64 20 6d 61 79 20 6e 6f 74 20 62 65 20 77  and may not be w
18b93 72 69 74 74 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ritten..*/.SQLIT
18b94 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
18b95 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
18b96 4d 65 74 61 28 42 74 72 65 65 20 2a 70 2c 20 69  Meta(Btree *p, i
18b97 6e 74 20 69 64 78 2c 20 75 33 32 20 69 4d 65 74  nt idx, u32 iMet
18b98 61 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  a){.  BtShared *
18b99 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
18b9a 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
18b9b 50 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  P1;.  int rc;.  
18b9c 61 73 73 65 72 74 28 20 69 64 78 3e 3d 31 20 26  assert( idx>=1 &
18b9d 26 20 69 64 78 3c 3d 31 35 20 29 3b 0a 20 20 73  & idx<=15 );.  s
18b9e 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
18b9f 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  (p);.  assert( p
18ba0 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
18ba1 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65  _WRITE );.  asse
18ba2 72 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 21  rt( pBt->pPage1!
18ba3 3d 30 20 29 3b 0a 20 20 70 50 31 20 3d 20 70 42  =0 );.  pP1 = pB
18ba4 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61  t->pPage1->aData
18ba5 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
18ba6 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e  PagerWrite(pBt->
18ba7 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
18ba8 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
18ba9 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 75 74  TE_OK ){.    put
18baa 34 62 79 74 65 28 26 70 50 31 5b 33 36 20 2b 20  4byte(&pP1[36 + 
18bab 69 64 78 2a 34 5d 2c 20 69 4d 65 74 61 29 3b 0a  idx*4], iMeta);.
18bac 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18bad 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
18bae 20 20 20 69 66 28 20 69 64 78 3d 3d 42 54 52 45     if( idx==BTRE
18baf 45 5f 49 4e 43 52 5f 56 41 43 55 55 4d 20 29 7b  E_INCR_VACUUM ){
18bb0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
18bb1 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 7c  Bt->autoVacuum |
18bb2 7c 20 69 4d 65 74 61 3d 3d 30 20 29 3b 0a 20 20  | iMeta==0 );.  
18bb3 20 20 20 20 61 73 73 65 72 74 28 20 69 4d 65 74      assert( iMet
18bb4 61 3d 3d 30 20 7c 7c 20 69 4d 65 74 61 3d 3d 31  a==0 || iMeta==1
18bb5 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69   );.      pBt->i
18bb6 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 75 38 29  ncrVacuum = (u8)
18bb7 69 4d 65 74 61 3b 0a 20 20 20 20 7d 0a 23 65 6e  iMeta;.    }.#en
18bb8 64 69 66 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  dif.  }.  sqlite
18bb9 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
18bba 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
18bbb 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
18bbc 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 0a 2f  MIT_BTREECOUNT./
18bbd 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  *.** The first a
18bbe 72 67 75 6d 65 6e 74 2c 20 70 43 75 72 2c 20 69  rgument, pCur, i
18bbf 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 65  s a cursor opene
18bc0 64 20 6f 6e 20 73 6f 6d 65 20 62 2d 74 72 65 65  d on some b-tree
18bc1 2e 20 43 6f 75 6e 74 20 74 68 65 0a 2a 2a 20 6e  . Count the.** n
18bc2 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
18bc3 20 69 6e 20 74 68 65 20 62 2d 74 72 65 65 20 61   in the b-tree a
18bc4 6e 64 20 77 72 69 74 65 20 74 68 65 20 72 65 73  nd write the res
18bc5 75 6c 74 20 74 6f 20 2a 70 6e 45 6e 74 72 79 2e  ult to *pnEntry.
18bc6 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
18bc7 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
18bc8 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73  the operation is
18bc9 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 65 78   successfully ex
18bca 65 63 75 74 65 64 2e 20 0a 2a 2a 20 4f 74 68 65  ecuted. .** Othe
18bcb 72 77 69 73 65 2c 20 69 66 20 61 6e 20 65 72 72  rwise, if an err
18bcc 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
18bcd 64 20 28 69 2e 65 2e 20 61 6e 20 49 4f 20 65 72  d (i.e. an IO er
18bce 72 6f 72 20 6f 72 20 64 61 74 61 62 61 73 65 0a  ror or database.
18bcf 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 29 20 61  ** corruption) a
18bd0 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
18bd1 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
18bd2 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
18bd3 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
18bd4 72 65 65 43 6f 75 6e 74 28 42 74 43 75 72 73 6f  reeCount(BtCurso
18bd5 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a 70 6e  r *pCur, i64 *pn
18bd6 45 6e 74 72 79 29 7b 0a 20 20 69 36 34 20 6e 45  Entry){.  i64 nE
18bd7 6e 74 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20  ntry = 0;       
18bd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
18bd9 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
18bda 6e 20 69 6e 20 2a 70 6e 45 6e 74 72 79 20 2a 2f  n in *pnEntry */
18bdb 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
18bdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18bdd 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
18bde 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 72 63 20 3d  n code */.  rc =
18bdf 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72   moveToRoot(pCur
18be0 29 3b 0a 0a 20 20 2f 2a 20 55 6e 6c 65 73 73 20  );..  /* Unless 
18be1 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
18be2 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c   the following l
18be3 6f 6f 70 20 72 75 6e 73 20 6f 6e 65 20 69 74 65  oop runs one ite
18be4 72 61 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 0a  ration for each.
18be5 20 20 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65    ** page in the
18be6 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72   B-Tree structur
18be7 65 20 28 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67  e (not including
18be8 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29   overflow pages)
18be9 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28  . .  */.  while(
18bea 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
18beb 7b 0a 20 20 20 20 69 6e 74 20 69 49 64 78 3b 20  {.    int iIdx; 
18bec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18bed 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
18bee 78 20 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65 20  x of child node 
18bef 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a 20 20 20  in parent */.   
18bf0 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
18bf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18bf2 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70      /* Current p
18bf3 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65  age of the b-tre
18bf4 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66 20  e */..    /* If 
18bf5 74 68 69 73 20 69 73 20 61 20 6c 65 61 66 20 70  this is a leaf p
18bf6 61 67 65 20 6f 72 20 74 68 65 20 74 72 65 65 20  age or the tree 
18bf7 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 2d 6b 65  is not an int-ke
18bf8 79 20 74 72 65 65 2c 20 74 68 65 6e 20 0a 20 20  y tree, then .  
18bf9 20 20 2a 2a 20 74 68 69 73 20 70 61 67 65 20 63    ** this page c
18bfa 6f 6e 74 61 69 6e 73 20 63 6f 75 6e 74 61 62 6c  ontains countabl
18bfb 65 20 65 6e 74 72 69 65 73 2e 20 49 6e 63 72 65  e entries. Incre
18bfc 6d 65 6e 74 20 74 68 65 20 65 6e 74 72 79 20 63  ment the entry c
18bfd 6f 75 6e 74 65 72 0a 20 20 20 20 2a 2a 20 61 63  ounter.    ** ac
18bfe 63 6f 72 64 69 6e 67 6c 79 2e 0a 20 20 20 20 2a  cordingly..    *
18bff 2f 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 43  /.    pPage = pC
18c00 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
18c01 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28  >iPage];.    if(
18c02 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20   pPage->leaf || 
18c03 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29  !pPage->intKey )
18c04 7b 0a 20 20 20 20 20 20 6e 45 6e 74 72 79 20 2b  {.      nEntry +
18c05 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a  = pPage->nCell;.
18c06 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 70 50      }..    /* pP
18c07 61 67 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f  age is a leaf no
18c08 64 65 2e 20 54 68 69 73 20 6c 6f 6f 70 20 6e 61  de. This loop na
18c09 76 69 67 61 74 65 73 20 74 68 65 20 63 75 72 73  vigates the curs
18c0a 6f 72 20 73 6f 20 74 68 61 74 20 69 74 20 0a 20  or so that it . 
18c0b 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20     ** points to 
18c0c 74 68 65 20 66 69 72 73 74 20 69 6e 74 65 72 69  the first interi
18c0d 6f 72 20 63 65 6c 6c 20 74 68 61 74 20 69 74 20  or cell that it 
18c0e 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70 61  points to the pa
18c0f 72 65 6e 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74  rent of.    ** t
18c10 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e 20  he next page in 
18c11 74 68 65 20 74 72 65 65 20 74 68 61 74 20 68 61  the tree that ha
18c12 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 76  s not yet been v
18c13 69 73 69 74 65 64 2e 20 54 68 65 0a 20 20 20 20  isited. The.    
18c14 2a 2a 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70  ** pCur->aiIdx[p
18c15 43 75 72 2d 3e 69 50 61 67 65 5d 20 76 61 6c 75  Cur->iPage] valu
18c16 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  e is set to the 
18c17 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 72  index of the par
18c18 65 6e 74 20 63 65 6c 6c 0a 20 20 20 20 2a 2a 20  ent cell.    ** 
18c19 6f 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20  of the page, or 
18c1a 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
18c1b 20 63 65 6c 6c 73 20 69 6e 20 74 68 65 20 70 61   cells in the pa
18c1c 67 65 20 69 66 20 74 68 65 20 6e 65 78 74 20 70  ge if the next p
18c1d 61 67 65 0a 20 20 20 20 2a 2a 20 74 6f 20 76 69  age.    ** to vi
18c1e 73 69 74 20 69 73 20 74 68 65 20 72 69 67 68 74  sit is the right
18c1f 2d 63 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61  -child of its pa
18c20 72 65 6e 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  rent..    **.   
18c21 20 2a 2a 20 49 66 20 61 6c 6c 20 70 61 67 65 73   ** If all pages
18c22 20 69 6e 20 74 68 65 20 74 72 65 65 20 68 61 76   in the tree hav
18c23 65 20 62 65 65 6e 20 76 69 73 69 74 65 64 2c 20  e been visited, 
18c24 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
18c25 20 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63   to the.    ** c
18c26 61 6c 6c 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  aller..    */.  
18c27 20 20 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61    if( pPage->lea
18c28 66 20 29 7b 0a 20 20 20 20 20 20 64 6f 20 7b 0a  f ){.      do {.
18c29 20 20 20 20 20 20 20 20 69 66 28 20 70 43 75 72          if( pCur
18c2a 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20  ->iPage==0 ){.  
18c2b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 70          /* All p
18c2c 61 67 65 73 20 6f 66 20 74 68 65 20 62 2d 74 72  ages of the b-tr
18c2d 65 65 20 68 61 76 65 20 62 65 65 6e 20 76 69 73  ee have been vis
18c2e 69 74 65 64 2e 20 52 65 74 75 72 6e 20 73 75 63  ited. Return suc
18c2f 63 65 73 73 66 75 6c 6c 79 2e 20 2a 2f 0a 20 20  cessfully. */.  
18c30 20 20 20 20 20 20 20 20 2a 70 6e 45 6e 74 72 79          *pnEntry
18c31 20 3d 20 6e 45 6e 74 72 79 3b 0a 20 20 20 20 20   = nEntry;.     
18c32 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
18c33 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d  TE_OK;.        }
18c34 0a 20 20 20 20 20 20 20 20 6d 6f 76 65 54 6f 50  .        moveToP
18c35 61 72 65 6e 74 28 70 43 75 72 29 3b 0a 20 20 20  arent(pCur);.   
18c36 20 20 20 7d 77 68 69 6c 65 20 28 20 70 43 75 72     }while ( pCur
18c37 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
18c38 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50 61  age]>=pCur->apPa
18c39 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
18c3a 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 20 20 20  >nCell );..     
18c3b 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
18c3c 72 2d 3e 69 50 61 67 65 5d 2b 2b 3b 0a 20 20 20  r->iPage]++;.   
18c3d 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d     pPage = pCur-
18c3e 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
18c3f 61 67 65 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  age];.    }..   
18c40 20 2f 2a 20 44 65 73 63 65 6e 64 20 74 6f 20 74   /* Descend to t
18c41 68 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 6f 66  he child node of
18c42 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20 74   the cell that t
18c43 68 65 20 63 75 72 73 6f 72 20 63 75 72 72 65 6e  he cursor curren
18c44 74 6c 79 20 0a 20 20 20 20 2a 2a 20 70 6f 69 6e  tly .    ** poin
18c45 74 73 20 61 74 2e 20 54 68 69 73 20 69 73 20 74  ts at. This is t
18c46 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 69  he right-child i
18c47 66 20 28 69 49 64 78 3d 3d 70 50 61 67 65 2d 3e  f (iIdx==pPage->
18c48 6e 43 65 6c 6c 29 2e 0a 20 20 20 20 2a 2f 0a 20  nCell)..    */. 
18c49 20 20 20 69 49 64 78 20 3d 20 70 43 75 72 2d 3e     iIdx = pCur->
18c4a 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
18c4b 65 5d 3b 0a 20 20 20 20 69 66 28 20 69 49 64 78  e];.    if( iIdx
18c4c 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  ==pPage->nCell )
18c4d 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76  {.      rc = mov
18c4e 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67  eToChild(pCur, g
18c4f 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
18c50 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
18c51 4f 66 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20  Offset+8]));.   
18c52 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
18c53 20 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70   = moveToChild(p
18c54 43 75 72 2c 20 67 65 74 34 62 79 74 65 28 66 69  Cur, get4byte(fi
18c55 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 49  ndCell(pPage, iI
18c56 64 78 29 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  dx)));.    }.  }
18c57 0a 0a 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20  ..  /* An error 
18c58 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 52 65  has occurred. Re
18c59 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f  turn an error co
18c5a 64 65 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  de. */.  return 
18c5b 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  rc;.}.#endif../*
18c5c 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70  .** Return the p
18c5d 61 67 65 72 20 61 73 73 6f 63 69 61 74 65 64 20  ager associated 
18c5e 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 54  with a BTree.  T
18c5f 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
18c60 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74 69  sed for.** testi
18c61 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
18c62 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45   only..*/.SQLITE
18c63 5f 50 52 49 56 41 54 45 20 50 61 67 65 72 20 2a  _PRIVATE Pager *
18c64 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
18c65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 72  r(Btree *p){.  r
18c66 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d 3e 70 50  eturn p->pBt->pP
18c67 61 67 65 72 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  ager;.}..#ifndef
18c68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
18c69 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a  EGRITY_CHECK./*.
18c6a 2a 2a 20 41 70 70 65 6e 64 20 61 20 6d 65 73 73  ** Append a mess
18c6b 61 67 65 20 74 6f 20 74 68 65 20 65 72 72 6f 72  age to the error
18c6c 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 2e   message string.
18c6d 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
18c6e 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 0a  checkAppendMsg(.
18c6f 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70    IntegrityCk *p
18c70 43 68 65 63 6b 2c 0a 20 20 63 68 61 72 20 2a 7a  Check,.  char *z
18c71 4d 73 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68  Msg1,.  const ch
18c72 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e  ar *zFormat,.  .
18c73 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
18c74 61 70 3b 0a 20 20 69 66 28 20 21 70 43 68 65 63  ap;.  if( !pChec
18c75 6b 2d 3e 6d 78 45 72 72 20 29 20 72 65 74 75 72  k->mxErr ) retur
18c76 6e 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6d 78 45  n;.  pCheck->mxE
18c77 72 72 2d 2d 3b 0a 20 20 70 43 68 65 63 6b 2d 3e  rr--;.  pCheck->
18c78 6e 45 72 72 2b 2b 3b 0a 20 20 76 61 5f 73 74 61  nErr++;.  va_sta
18c79 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
18c7a 0a 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65  .  if( pCheck->e
18c7b 72 72 4d 73 67 2e 6e 43 68 61 72 20 29 7b 0a 20  rrMsg.nChar ){. 
18c7c 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
18c7d 75 6d 41 70 70 65 6e 64 28 26 70 43 68 65 63 6b  umAppend(&pCheck
18c7e 2d 3e 65 72 72 4d 73 67 2c 20 22 5c 6e 22 2c 20  ->errMsg, "\n", 
18c7f 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 4d  1);.  }.  if( zM
18c80 73 67 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  sg1 ){.    sqlit
18c81 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
18c82 28 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67  (&pCheck->errMsg
18c83 2c 20 7a 4d 73 67 31 2c 20 2d 31 29 3b 0a 20 20  , zMsg1, -1);.  
18c84 7d 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72 69  }.  sqlite3VXPri
18c85 6e 74 66 28 26 70 43 68 65 63 6b 2d 3e 65 72 72  ntf(&pCheck->err
18c86 4d 73 67 2c 20 31 2c 20 7a 46 6f 72 6d 61 74 2c  Msg, 1, zFormat,
18c87 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
18c88 70 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63 6b  p);.  if( pCheck
18c89 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46  ->errMsg.mallocF
18c8a 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 43 68  ailed ){.    pCh
18c8b 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  eck->mallocFaile
18c8c 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  d = 1;.  }.}.#en
18c8d 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
18c8e 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
18c8f 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  CK */..#ifndef S
18c90 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
18c91 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a  RITY_CHECK./*.**
18c92 20 41 64 64 20 31 20 74 6f 20 74 68 65 20 72 65   Add 1 to the re
18c93 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f  ference count fo
18c94 72 20 70 61 67 65 20 69 50 61 67 65 2e 20 20 49  r page iPage.  I
18c95 66 20 74 68 69 73 20 69 73 20 74 68 65 20 73 65  f this is the se
18c96 63 6f 6e 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63  cond.** referenc
18c97 65 20 74 6f 20 74 68 65 20 70 61 67 65 2c 20 61  e to the page, a
18c98 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  dd an error mess
18c99 61 67 65 20 74 6f 20 70 43 68 65 63 6b 2d 3e 7a  age to pCheck->z
18c9a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52 65 74 75 72  ErrMsg..** Retur
18c9b 6e 20 31 20 69 66 20 74 68 65 72 65 20 61 72 65  n 1 if there are
18c9c 20 32 20 6f 72 65 20 6d 6f 72 65 20 72 65 66 65   2 ore more refe
18c9d 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 70 61  rences to the pa
18c9e 67 65 20 61 6e 64 20 30 20 69 66 0a 2a 2a 20 69  ge and 0 if.** i
18c9f 66 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69  f this is the fi
18ca0 72 73 74 20 72 65 66 65 72 65 6e 63 65 20 74 6f  rst reference to
18ca1 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
18ca2 20 41 6c 73 6f 20 63 68 65 63 6b 20 74 68 61 74   Also check that
18ca3 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
18ca4 20 69 73 20 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a   is in bounds..*
18ca5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68 65  /.static int che
18ca6 63 6b 52 65 66 28 49 6e 74 65 67 72 69 74 79 43  ckRef(IntegrityC
18ca7 6b 20 2a 70 43 68 65 63 6b 2c 20 50 67 6e 6f 20  k *pCheck, Pgno 
18ca8 69 50 61 67 65 2c 20 63 68 61 72 20 2a 7a 43 6f  iPage, char *zCo
18ca9 6e 74 65 78 74 29 7b 0a 20 20 69 66 28 20 69 50  ntext){.  if( iP
18caa 61 67 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  age==0 ) return 
18cab 31 3b 0a 20 20 69 66 28 20 69 50 61 67 65 3e 70  1;.  if( iPage>p
18cac 43 68 65 63 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a  Check->nPage ){.
18cad 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18cae 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
18caf 65 78 74 2c 20 22 69 6e 76 61 6c 69 64 20 70 61  ext, "invalid pa
18cb0 67 65 20 6e 75 6d 62 65 72 20 25 64 22 2c 20 69  ge number %d", i
18cb1 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Page);.    retur
18cb2 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n 1;.  }.  if( p
18cb3 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61  Check->anRef[iPa
18cb4 67 65 5d 3d 3d 31 20 29 7b 0a 20 20 20 20 63 68  ge]==1 ){.    ch
18cb5 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
18cb6 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22  eck, zContext, "
18cb7 32 6e 64 20 72 65 66 65 72 65 6e 63 65 20 74 6f  2nd reference to
18cb8 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65   page %d", iPage
18cb9 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
18cba 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 20 28  .  }.  return  (
18cbb 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66 5b 69 50  pCheck->anRef[iP
18cbc 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69  age]++)>1;.}..#i
18cbd 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
18cbe 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a  T_AUTOVACUUM./*.
18cbf 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  ** Check that th
18cc0 65 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70  e entry in the p
18cc1 6f 69 6e 74 65 72 2d 6d 61 70 20 66 6f 72 20 70  ointer-map for p
18cc2 61 67 65 20 69 43 68 69 6c 64 20 6d 61 70 73 20  age iChild maps 
18cc3 74 6f 20 0a 2a 2a 20 70 61 67 65 20 69 50 61 72  to .** page iPar
18cc4 65 6e 74 2c 20 70 6f 69 6e 74 65 72 20 74 79 70  ent, pointer typ
18cc5 65 20 70 74 72 54 79 70 65 2e 20 49 66 20 6e 6f  e ptrType. If no
18cc6 74 2c 20 61 70 70 65 6e 64 20 61 6e 20 65 72 72  t, append an err
18cc7 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f  or message.** to
18cc8 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74   pCheck..*/.stat
18cc9 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50 74 72  ic void checkPtr
18cca 6d 61 70 28 0a 20 20 49 6e 74 65 67 72 69 74 79  map(.  Integrity
18ccb 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 20 2f 2a  Ck *pCheck,   /*
18ccc 20 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b   Integrity check
18ccd 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 50 67   context */.  Pg
18cce 6e 6f 20 69 43 68 69 6c 64 2c 20 20 20 20 20 20  no iChild,      
18ccf 20 20 20 20 20 2f 2a 20 43 68 69 6c 64 20 70 61       /* Child pa
18cd0 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75  ge number */.  u
18cd1 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20  8 eType,        
18cd2 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74 65        /* Expecte
18cd3 64 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 74 79  d pointer map ty
18cd4 70 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 61  pe */.  Pgno iPa
18cd5 72 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20 2f  rent,          /
18cd6 2a 20 45 78 70 65 63 74 65 64 20 70 6f 69 6e 74  * Expected point
18cd7 65 72 20 6d 61 70 20 70 61 72 65 6e 74 20 70 61  er map parent pa
18cd8 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63  ge number */.  c
18cd9 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20 20  har *zContext   
18cda 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
18cdb 20 64 65 73 63 72 69 70 74 69 6f 6e 20 28 75 73   description (us
18cdc 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d 73 67  ed for error msg
18cdd 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  ) */.){.  int rc
18cde 3b 0a 20 20 75 38 20 65 50 74 72 6d 61 70 54 79  ;.  u8 ePtrmapTy
18cdf 70 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d  pe;.  Pgno iPtrm
18ce0 61 70 50 61 72 65 6e 74 3b 0a 0a 20 20 72 63 20  apParent;..  rc 
18ce1 3d 20 70 74 72 6d 61 70 47 65 74 28 70 43 68 65  = ptrmapGet(pChe
18ce2 63 6b 2d 3e 70 42 74 2c 20 69 43 68 69 6c 64 2c  ck->pBt, iChild,
18ce3 20 26 65 50 74 72 6d 61 70 54 79 70 65 2c 20 26   &ePtrmapType, &
18ce4 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b 0a  iPtrmapParent);.
18ce5 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
18ce6 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72  _OK ){.    if( r
18ce7 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
18ce8 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f  || rc==SQLITE_IO
18ce9 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 70 43 68 65  ERR_NOMEM ) pChe
18cea 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  ck->mallocFailed
18ceb 20 3d 20 31 3b 0a 20 20 20 20 63 68 65 63 6b 41   = 1;.    checkA
18cec 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
18ced 20 7a 43 6f 6e 74 65 78 74 2c 20 22 46 61 69 6c   zContext, "Fail
18cee 65 64 20 74 6f 20 72 65 61 64 20 70 74 72 6d 61  ed to read ptrma
18cef 70 20 6b 65 79 3d 25 64 22 2c 20 69 43 68 69 6c  p key=%d", iChil
18cf0 64 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  d);.    return;.
18cf1 20 20 7d 0a 0a 20 20 69 66 28 20 65 50 74 72 6d    }..  if( ePtrm
18cf2 61 70 54 79 70 65 21 3d 65 54 79 70 65 20 7c 7c  apType!=eType ||
18cf3 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 21 3d   iPtrmapParent!=
18cf4 69 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 63  iParent ){.    c
18cf5 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
18cf6 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
18cf7 0a 20 20 20 20 20 20 22 42 61 64 20 70 74 72 20  .      "Bad ptr 
18cf8 6d 61 70 20 65 6e 74 72 79 20 6b 65 79 3d 25 64  map entry key=%d
18cf9 20 65 78 70 65 63 74 65 64 3d 28 25 64 2c 25 64   expected=(%d,%d
18cfa 29 20 67 6f 74 3d 28 25 64 2c 25 64 29 22 2c 20  ) got=(%d,%d)", 
18cfb 0a 20 20 20 20 20 20 69 43 68 69 6c 64 2c 20 65  .      iChild, e
18cfc 54 79 70 65 2c 20 69 50 61 72 65 6e 74 2c 20 65  Type, iParent, e
18cfd 50 74 72 6d 61 70 54 79 70 65 2c 20 69 50 74 72  PtrmapType, iPtr
18cfe 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a  mapParent);.  }.
18cff 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
18d00 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72  Check the integr
18d01 69 74 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c  ity of the freel
18d02 69 73 74 20 6f 72 20 6f 66 20 61 6e 20 6f 76 65  ist or of an ove
18d03 72 66 6c 6f 77 20 70 61 67 65 20 6c 69 73 74 2e  rflow page list.
18d04 0a 2a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20  .** Verify that 
18d05 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
18d06 67 65 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  ges on the list 
18d07 69 73 20 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  is N..*/.static 
18d08 76 6f 69 64 20 63 68 65 63 6b 4c 69 73 74 28 0a  void checkList(.
18d09 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70    IntegrityCk *p
18d0a 43 68 65 63 6b 2c 20 20 2f 2a 20 49 6e 74 65 67  Check,  /* Integ
18d0b 72 69 74 79 20 63 68 65 63 6b 69 6e 67 20 63 6f  rity checking co
18d0c 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
18d0d 73 46 72 65 65 4c 69 73 74 2c 20 20 20 20 20 20  sFreeList,      
18d0e 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 66   /* True for a f
18d0f 72 65 65 6c 69 73 74 2e 20 20 46 61 6c 73 65 20  reelist.  False 
18d10 66 6f 72 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  for overflow pag
18d11 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  e list */.  int 
18d12 69 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  iPage,          
18d13 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
18d14 20 66 6f 72 20 66 69 72 73 74 20 70 61 67 65 20   for first page 
18d15 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  in the list */. 
18d16 20 69 6e 74 20 4e 2c 20 20 20 20 20 20 20 20 20   int N,         
18d17 20 20 20 20 20 20 20 2f 2a 20 45 78 70 65 63 74         /* Expect
18d18 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  ed number of pag
18d19 65 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a  es in the list *
18d1a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65  /.  char *zConte
18d1b 78 74 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  xt        /* Con
18d1c 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d  text for error m
18d1d 65 73 73 61 67 65 73 20 2a 2f 0a 29 7b 0a 20 20  essages */.){.  
18d1e 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 65 78 70  int i;.  int exp
18d1f 65 63 74 65 64 20 3d 20 4e 3b 0a 20 20 69 6e 74  ected = N;.  int
18d20 20 69 46 69 72 73 74 20 3d 20 69 50 61 67 65 3b   iFirst = iPage;
18d21 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 3e 20  .  while( N-- > 
18d22 30 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45  0 && pCheck->mxE
18d23 72 72 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65  rr ){.    DbPage
18d24 20 2a 70 4f 76 66 6c 50 61 67 65 3b 0a 20 20 20   *pOvflPage;.   
18d25 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
18d26 70 4f 76 66 6c 44 61 74 61 3b 0a 20 20 20 20 69  pOvflData;.    i
18d27 66 28 20 69 50 61 67 65 3c 31 20 29 7b 0a 20 20  f( iPage<1 ){.  
18d28 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18d29 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
18d2a 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 22 25  ext,.         "%
18d2b 64 20 6f 66 20 25 64 20 70 61 67 65 73 20 6d 69  d of %d pages mi
18d2c 73 73 69 6e 67 20 66 72 6f 6d 20 6f 76 65 72 66  ssing from overf
18d2d 6c 6f 77 20 6c 69 73 74 20 73 74 61 72 74 69 6e  low list startin
18d2e 67 20 61 74 20 25 64 22 2c 0a 20 20 20 20 20 20  g at %d",.      
18d2f 20 20 20 20 4e 2b 31 2c 20 65 78 70 65 63 74 65      N+1, expecte
18d30 64 2c 20 69 46 69 72 73 74 29 3b 0a 20 20 20 20  d, iFirst);.    
18d31 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
18d32 20 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28     if( checkRef(
18d33 70 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a  pCheck, iPage, z
18d34 43 6f 6e 74 65 78 74 29 20 29 20 62 72 65 61 6b  Context) ) break
18d35 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
18d36 33 50 61 67 65 72 47 65 74 28 70 43 68 65 63 6b  3PagerGet(pCheck
18d37 2d 3e 70 50 61 67 65 72 2c 20 28 50 67 6e 6f 29  ->pPager, (Pgno)
18d38 69 50 61 67 65 2c 20 26 70 4f 76 66 6c 50 61 67  iPage, &pOvflPag
18d39 65 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  e) ){.      chec
18d3a 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
18d3b 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 66 61  k, zContext, "fa
18d3c 69 6c 65 64 20 74 6f 20 67 65 74 20 70 61 67 65  iled to get page
18d3d 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20 20   %d", iPage);.  
18d3e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
18d3f 0a 20 20 20 20 70 4f 76 66 6c 44 61 74 61 20 3d  .    pOvflData =
18d40 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
18d41 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
18d42 74 44 61 74 61 28 70 4f 76 66 6c 50 61 67 65 29  tData(pOvflPage)
18d43 3b 0a 20 20 20 20 69 66 28 20 69 73 46 72 65 65  ;.    if( isFree
18d44 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 69 6e  List ){.      in
18d45 74 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26  t n = get4byte(&
18d46 70 4f 76 66 6c 44 61 74 61 5b 34 5d 29 3b 0a 23  pOvflData[4]);.#
18d47 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18d48 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
18d49 20 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e      if( pCheck->
18d4a 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
18d4b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
18d4c 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69  Ptrmap(pCheck, i
18d4d 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45  Page, PTRMAP_FRE
18d4e 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65  EPAGE, 0, zConte
18d4f 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  xt);.      }.#en
18d50 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 6e 3e  dif.      if( n>
18d51 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 75 73 61  pCheck->pBt->usa
18d52 62 6c 65 53 69 7a 65 2f 34 2d 32 20 29 7b 0a 20  bleSize/4-2 ){. 
18d53 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65         checkAppe
18d54 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43  ndMsg(pCheck, zC
18d55 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20  ontext,.        
18d56 20 20 20 22 66 72 65 65 6c 69 73 74 20 6c 65 61     "freelist lea
18d57 66 20 63 6f 75 6e 74 20 74 6f 6f 20 62 69 67 20  f count too big 
18d58 6f 6e 20 70 61 67 65 20 25 64 22 2c 20 69 50 61  on page %d", iPa
18d59 67 65 29 3b 0a 20 20 20 20 20 20 20 20 4e 2d 2d  ge);.        N--
18d5a 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
18d5b 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
18d5c 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  i<n; i++){.     
18d5d 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 50       Pgno iFreeP
18d5e 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28 26  age = get4byte(&
18d5f 70 4f 76 66 6c 44 61 74 61 5b 38 2b 69 2a 34 5d  pOvflData[8+i*4]
18d60 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
18d61 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
18d62 4d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  M.          if( 
18d63 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74  pCheck->pBt->aut
18d64 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
18d65 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d         checkPtrm
18d66 61 70 28 70 43 68 65 63 6b 2c 20 69 46 72 65 65  ap(pCheck, iFree
18d67 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45  Page, PTRMAP_FRE
18d68 45 50 41 47 45 2c 20 30 2c 20 7a 43 6f 6e 74 65  EPAGE, 0, zConte
18d69 78 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  xt);.          }
18d6a 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
18d6b 20 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63    checkRef(pChec
18d6c 6b 2c 20 69 46 72 65 65 50 61 67 65 2c 20 7a 43  k, iFreePage, zC
18d6d 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 20  ontext);.       
18d6e 20 7d 0a 20 20 20 20 20 20 20 20 4e 20 2d 3d 20   }.        N -= 
18d6f 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  n;.      }.    }
18d70 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18d71 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
18d72 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20 20      else{.      
18d73 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62  /* If this datab
18d74 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74  ase supports aut
18d75 6f 2d 76 61 63 75 75 6d 20 61 6e 64 20 69 50 61  o-vacuum and iPa
18d76 67 65 20 69 73 20 6e 6f 74 20 74 68 65 20 6c 61  ge is not the la
18d77 73 74 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65  st.      ** page
18d78 20 69 6e 20 74 68 69 73 20 6f 76 65 72 66 6c 6f   in this overflo
18d79 77 20 6c 69 73 74 2c 20 63 68 65 63 6b 20 74 68  w list, check th
18d7a 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  at the pointer-m
18d7b 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20 20 20  ap entry for.   
18d7c 20 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77     ** the follow
18d7d 69 6e 67 20 70 61 67 65 20 6d 61 74 63 68 65 73  ing page matches
18d7e 20 69 50 61 67 65 2e 0a 20 20 20 20 20 20 2a 2f   iPage..      */
18d7f 0a 20 20 20 20 20 20 69 66 28 20 70 43 68 65 63  .      if( pChec
18d80 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  k->pBt->autoVacu
18d81 75 6d 20 26 26 20 4e 3e 30 20 29 7b 0a 20 20 20  um && N>0 ){.   
18d82 20 20 20 20 20 69 20 3d 20 67 65 74 34 62 79 74       i = get4byt
18d83 65 28 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20  e(pOvflData);.  
18d84 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61        checkPtrma
18d85 70 28 70 43 68 65 63 6b 2c 20 69 2c 20 50 54 52  p(pCheck, i, PTR
18d86 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69  MAP_OVERFLOW2, i
18d87 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  Page, zContext);
18d88 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
18d89 65 6e 64 69 66 0a 20 20 20 20 69 50 61 67 65 20  endif.    iPage 
18d8a 3d 20 67 65 74 34 62 79 74 65 28 70 4f 76 66 6c  = get4byte(pOvfl
18d8b 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69 74  Data);.    sqlit
18d8c 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76  e3PagerUnref(pOv
18d8d 66 6c 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23  flPage);.  }.}.#
18d8e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
18d8f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
18d90 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  HECK */..#ifndef
18d91 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54   SQLITE_OMIT_INT
18d92 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a  EGRITY_CHECK./*.
18d93 2a 2a 20 44 6f 20 76 61 72 69 6f 75 73 20 73 61  ** Do various sa
18d94 6e 69 74 79 20 63 68 65 63 6b 73 20 6f 6e 20 61  nity checks on a
18d95 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20   single page of 
18d96 61 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 0a  a tree.  Return.
18d97 2a 2a 20 74 68 65 20 74 72 65 65 20 64 65 70 74  ** the tree dept
18d98 68 2e 20 20 52 6f 6f 74 20 70 61 67 65 73 20 72  h.  Root pages r
18d99 65 74 75 72 6e 20 30 2e 20 20 50 61 72 65 6e 74  eturn 0.  Parent
18d9a 73 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 0a  s of root pages.
18d9b 2a 2a 20 72 65 74 75 72 6e 20 31 2c 20 61 6e 64  ** return 1, and
18d9c 20 73 6f 20 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a   so forth..** .*
18d9d 2a 20 54 68 65 73 65 20 63 68 65 63 6b 73 20 61  * These checks a
18d9e 72 65 20 64 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20  re done:.**.**  
18d9f 20 20 20 20 31 2e 20 20 4d 61 6b 65 20 73 75 72      1.  Make sur
18da0 65 20 74 68 61 74 20 63 65 6c 6c 73 20 61 6e 64  e that cells and
18da1 20 66 72 65 65 62 6c 6f 63 6b 73 20 64 6f 20 6e   freeblocks do n
18da2 6f 74 20 6f 76 65 72 6c 61 70 0a 2a 2a 20 20 20  ot overlap.**   
18da3 20 20 20 20 20 20 20 62 75 74 20 63 6f 6d 62 69         but combi
18da4 6e 65 20 74 6f 20 63 6f 6d 70 6c 65 74 65 6c 79  ne to completely
18da5 20 63 6f 76 65 72 20 74 68 65 20 70 61 67 65 2e   cover the page.
18da6 0a 2a 2a 20 20 4e 4f 20 20 32 2e 20 20 4d 61 6b  .**  NO  2.  Mak
18da7 65 20 73 75 72 65 20 63 65 6c 6c 20 6b 65 79 73  e sure cell keys
18da8 20 61 72 65 20 69 6e 20 6f 72 64 65 72 2e 0a 2a   are in order..*
18da9 2a 20 20 4e 4f 20 20 33 2e 20 20 4d 61 6b 65 20  *  NO  3.  Make 
18daa 73 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 6c  sure no key is l
18dab 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
18dac 6c 20 74 6f 20 7a 4c 6f 77 65 72 42 6f 75 6e 64  l to zLowerBound
18dad 2e 0a 2a 2a 20 20 4e 4f 20 20 34 2e 20 20 4d 61  ..**  NO  4.  Ma
18dae 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79 20 69  ke sure no key i
18daf 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
18db0 72 20 65 71 75 61 6c 20 74 6f 20 7a 55 70 70 65  r equal to zUppe
18db1 72 42 6f 75 6e 64 2e 0a 2a 2a 20 20 20 20 20 20  rBound..**      
18db2 35 2e 20 20 43 68 65 63 6b 20 74 68 65 20 69 6e  5.  Check the in
18db3 74 65 67 72 69 74 79 20 6f 66 20 6f 76 65 72 66  tegrity of overf
18db4 6c 6f 77 20 70 61 67 65 73 2e 0a 2a 2a 20 20 20  low pages..**   
18db5 20 20 20 36 2e 20 20 52 65 63 75 72 73 69 76 65     6.  Recursive
18db6 6c 79 20 63 61 6c 6c 20 63 68 65 63 6b 54 72 65  ly call checkTre
18db7 65 50 61 67 65 20 6f 6e 20 61 6c 6c 20 63 68 69  ePage on all chi
18db8 6c 64 72 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 37  ldren..**      7
18db9 2e 20 20 56 65 72 69 66 79 20 74 68 61 74 20 74  .  Verify that t
18dba 68 65 20 64 65 70 74 68 20 6f 66 20 61 6c 6c 20  he depth of all 
18dbb 63 68 69 6c 64 72 65 6e 20 69 73 20 74 68 65 20  children is the 
18dbc 73 61 6d 65 2e 0a 2a 2a 20 20 20 20 20 20 38 2e  same..**      8.
18dbd 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73    Make sure this
18dbe 20 70 61 67 65 20 69 73 20 61 74 20 6c 65 61 73   page is at leas
18dbf 74 20 33 33 25 20 66 75 6c 6c 20 6f 72 20 65 6c  t 33% full or el
18dc0 73 65 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20  se it is.**     
18dc1 20 20 20 20 20 74 68 65 20 72 6f 6f 74 20 6f 66       the root of
18dc2 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 73 74   the tree..*/.st
18dc3 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 54 72  atic int checkTr
18dc4 65 65 50 61 67 65 28 0a 20 20 49 6e 74 65 67 72  eePage(.  Integr
18dc5 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20  ityCk *pCheck,  
18dc6 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 74  /* Context for t
18dc7 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b 20  he sanity check 
18dc8 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67 65 2c 20  */.  int iPage, 
18dc9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
18dca 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
18dcb 20 70 61 67 65 20 74 6f 20 63 68 65 63 6b 20 2a   page to check *
18dcc 2f 0a 20 20 63 68 61 72 20 2a 7a 50 61 72 65 6e  /.  char *zParen
18dcd 74 43 6f 6e 74 65 78 74 20 20 2f 2a 20 50 61 72  tContext  /* Par
18dce 65 6e 74 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 29  ent context */.)
18dcf 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  {.  MemPage *pPa
18dd0 67 65 3b 0a 20 20 69 6e 74 20 69 2c 20 72 63 2c  ge;.  int i, rc,
18dd1 20 64 65 70 74 68 2c 20 64 32 2c 20 70 67 6e 6f   depth, d2, pgno
18dd2 2c 20 63 6e 74 3b 0a 20 20 69 6e 74 20 68 64 72  , cnt;.  int hdr
18dd3 2c 20 63 65 6c 6c 53 74 61 72 74 3b 0a 20 20 69  , cellStart;.  i
18dd4 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 75 38 20 2a  nt nCell;.  u8 *
18dd5 64 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64  data;.  BtShared
18dd6 20 2a 70 42 74 3b 0a 20 20 69 6e 74 20 75 73 61   *pBt;.  int usa
18dd7 62 6c 65 53 69 7a 65 3b 0a 20 20 63 68 61 72 20  bleSize;.  char 
18dd8 7a 43 6f 6e 74 65 78 74 5b 31 30 30 5d 3b 0a 20  zContext[100];. 
18dd9 20 63 68 61 72 20 2a 68 69 74 20 3d 20 30 3b 0a   char *hit = 0;.
18dda 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
18ddb 6e 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74  ntf(sizeof(zCont
18ddc 65 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  ext), zContext, 
18ddd 22 50 61 67 65 20 25 64 3a 20 22 2c 20 69 50 61  "Page %d: ", iPa
18dde 67 65 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ge);..  /* Check
18ddf 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20 65   that the page e
18de0 78 69 73 74 73 0a 20 20 2a 2f 0a 20 20 70 42 74  xists.  */.  pBt
18de1 20 3d 20 70 43 68 65 63 6b 2d 3e 70 42 74 3b 0a   = pCheck->pBt;.
18de2 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70    usableSize = p
18de3 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
18de4 20 20 69 66 28 20 69 50 61 67 65 3d 3d 30 20 29    if( iPage==0 )
18de5 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
18de6 20 63 68 65 63 6b 52 65 66 28 70 43 68 65 63 6b   checkRef(pCheck
18de7 2c 20 69 50 61 67 65 2c 20 7a 50 61 72 65 6e 74  , iPage, zParent
18de8 43 6f 6e 74 65 78 74 29 20 29 20 72 65 74 75 72  Context) ) retur
18de9 6e 20 30 3b 0a 20 20 69 66 28 20 28 72 63 20 3d  n 0;.  if( (rc =
18dea 20 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42   btreeGetPage(pB
18deb 74 2c 20 28 50 67 6e 6f 29 69 50 61 67 65 2c 20  t, (Pgno)iPage, 
18dec 26 70 50 61 67 65 2c 20 30 29 29 21 3d 30 20 29  &pPage, 0))!=0 )
18ded 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e  {.    checkAppen
18dee 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f  dMsg(pCheck, zCo
18def 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 22 75  ntext,.       "u
18df0 6e 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65  nable to get the
18df1 20 70 61 67 65 2e 20 65 72 72 6f 72 20 63 6f 64   page. error cod
18df2 65 3d 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20  e=%d", rc);.    
18df3 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
18df4 20 2f 2a 20 43 6c 65 61 72 20 4d 65 6d 50 61 67   /* Clear MemPag
18df5 65 2e 69 73 49 6e 69 74 20 74 6f 20 6d 61 6b 65  e.isInit to make
18df6 20 73 75 72 65 20 74 68 65 20 63 6f 72 72 75 70   sure the corrup
18df7 74 69 6f 6e 20 64 65 74 65 63 74 69 6f 6e 20 63  tion detection c
18df8 6f 64 65 20 69 6e 0a 20 20 2a 2a 20 62 74 72 65  ode in.  ** btre
18df9 65 49 6e 69 74 50 61 67 65 28 29 20 69 73 20 65  eInitPage() is e
18dfa 78 65 63 75 74 65 64 2e 20 20 2a 2f 0a 20 20 70  xecuted.  */.  p
18dfb 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30  Page->isInit = 0
18dfc 3b 0a 20 20 69 66 28 20 28 72 63 20 3d 20 62 74  ;.  if( (rc = bt
18dfd 72 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67  reeInitPage(pPag
18dfe 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73  e))!=0 ){.    as
18dff 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
18e00 5f 43 4f 52 52 55 50 54 20 29 3b 20 20 2f 2a 20  _CORRUPT );  /* 
18e01 54 68 65 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c  The only possibl
18e02 65 20 65 72 72 6f 72 20 66 72 6f 6d 20 49 6e 69  e error from Ini
18e03 74 50 61 67 65 20 2a 2f 0a 20 20 20 20 63 68 65  tPage */.    che
18e04 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
18e05 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20  ck, zContext, . 
18e06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18e07 20 20 22 62 74 72 65 65 49 6e 69 74 50 61 67 65    "btreeInitPage
18e08 28 29 20 72 65 74 75 72 6e 73 20 65 72 72 6f 72  () returns error
18e09 20 63 6f 64 65 20 25 64 22 2c 20 72 63 29 3b 0a   code %d", rc);.
18e0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
18e0b 70 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75  pPage);.    retu
18e0c 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
18e0d 43 68 65 63 6b 20 6f 75 74 20 61 6c 6c 20 74 68  Check out all th
18e0e 65 20 63 65 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20  e cells..  */.  
18e0f 64 65 70 74 68 20 3d 20 30 3b 0a 20 20 66 6f 72  depth = 0;.  for
18e10 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e  (i=0; i<pPage->n
18e11 43 65 6c 6c 20 26 26 20 70 43 68 65 63 6b 2d 3e  Cell && pCheck->
18e12 6d 78 45 72 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  mxErr; i++){.   
18e13 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20   u8 *pCell;.    
18e14 75 33 32 20 73 7a 3b 0a 20 20 20 20 43 65 6c 6c  u32 sz;.    Cell
18e15 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20 20 20 20  Info info;..    
18e16 2f 2a 20 43 68 65 63 6b 20 70 61 79 6c 6f 61 64  /* Check payload
18e17 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a   overflow pages.
18e18 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
18e19 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
18e1a 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43  of(zContext), zC
18e1b 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20  ontext,.        
18e1c 20 20 20 20 20 22 4f 6e 20 74 72 65 65 20 70 61       "On tree pa
18e1d 67 65 20 25 64 20 63 65 6c 6c 20 25 64 3a 20 22  ge %d cell %d: "
18e1e 2c 20 69 50 61 67 65 2c 20 69 29 3b 0a 20 20 20  , iPage, i);.   
18e1f 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c   pCell = findCel
18e20 6c 28 70 50 61 67 65 2c 69 29 3b 0a 20 20 20 20  l(pPage,i);.    
18e21 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
18e22 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20  r(pPage, pCell, 
18e23 26 69 6e 66 6f 29 3b 0a 20 20 20 20 73 7a 20 3d  &info);.    sz =
18e24 20 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20   info.nData;.   
18e25 20 69 66 28 20 21 70 50 61 67 65 2d 3e 69 6e 74   if( !pPage->int
18e26 4b 65 79 20 29 20 73 7a 20 2b 3d 20 28 69 6e 74  Key ) sz += (int
18e27 29 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20  )info.nKey;.    
18e28 61 73 73 65 72 74 28 20 73 7a 3d 3d 69 6e 66 6f  assert( sz==info
18e29 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 20  .nPayload );.   
18e2a 20 69 66 28 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c   if( (sz>info.nL
18e2b 6f 63 61 6c 29 20 0a 20 20 20 20 20 26 26 20 28  ocal) .     && (
18e2c 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65  &pCell[info.iOve
18e2d 72 66 6c 6f 77 5d 3c 3d 26 70 50 61 67 65 2d 3e  rflow]<=&pPage->
18e2e 61 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c  aData[pBt->usabl
18e2f 65 53 69 7a 65 5d 29 0a 20 20 20 20 29 7b 0a 20  eSize]).    ){. 
18e30 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20 3d       int nPage =
18e31 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63   (sz - info.nLoc
18e32 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a 65 20  al + usableSize 
18e33 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 7a 65  - 5)/(usableSize
18e34 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 67 6e   - 4);.      Pgn
18e35 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65 74  o pgnoOvfl = get
18e36 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66  4byte(&pCell[inf
18e37 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 23  o.iOverflow]);.#
18e38 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18e39 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
18e3a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74      if( pBt->aut
18e3b 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20  oVacuum ){.     
18e3c 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70     checkPtrmap(p
18e3d 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c  Check, pgnoOvfl,
18e3e 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
18e3f 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65  1, iPage, zConte
18e40 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  xt);.      }.#en
18e41 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 6b 4c  dif.      checkL
18e42 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c 20 70  ist(pCheck, 0, p
18e43 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c 20  gnoOvfl, nPage, 
18e44 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d  zContext);.    }
18e45 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 73  ..    /* Check s
18e46 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 63 68  anity of left ch
18e47 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  ild page..    */
18e48 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
18e49 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 70  >leaf ){.      p
18e4a 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 70  gno = get4byte(p
18e4b 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53  Cell);.#ifndef S
18e4c 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
18e4d 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28 20  ACUUM.      if( 
18e4e 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20  pBt->autoVacuum 
18e4f 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  ){.        check
18e50 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70  Ptrmap(pCheck, p
18e51 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
18e52 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74 65  E, iPage, zConte
18e53 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  xt);.      }.#en
18e54 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d 20 63  dif.      d2 = c
18e55 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68  heckTreePage(pCh
18e56 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74  eck, pgno, zCont
18e57 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ext);.      if( 
18e58 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74 68  i>0 && d2!=depth
18e59 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
18e5a 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
18e5b 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43 68  k, zContext, "Ch
18e5c 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20 64  ild page depth d
18e5d 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20 20  iffers");.      
18e5e 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d 20  }.      depth = 
18e5f 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  d2;.    }.  }.  
18e60 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
18e61 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67   ){.    pgno = g
18e62 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
18e63 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
18e64 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
18e65 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
18e66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74  (sizeof(zContext
18e67 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20  ), zContext, .  
18e68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18e69 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20 61     "On page %d a
18e6a 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20 22  t right child: "
18e6b 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64 65  , iPage);.#ifnde
18e6c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
18e6d 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28  TOVACUUM.    if(
18e6e 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
18e6f 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 50   ){.      checkP
18e70 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70 67  trmap(pCheck, pg
18e71 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45  no, PTRMAP_BTREE
18e72 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20 20  , iPage, 0);.   
18e73 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63 68   }.#endif.    ch
18e74 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68 65  eckTreePage(pChe
18e75 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65  ck, pgno, zConte
18e76 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20  xt);.  }. .  /* 
18e77 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c 65  Check for comple
18e78 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 20 74  te coverage of t
18e79 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 20 64  he page.  */.  d
18e7a 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ata = pPage->aDa
18e7b 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61 67  ta;.  hdr = pPag
18e7c 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
18e7d 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61 67  hit = sqlite3Pag
18e7e 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70 61  eMalloc( pBt->pa
18e7f 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28 20  geSize );.  if( 
18e80 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 43  hit==0 ){.    pC
18e81 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  heck->mallocFail
18e82 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  ed = 1;.  }else{
18e83 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 6e 74  .    u16 content
18e84 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 79 74  Offset = get2byt
18e85 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b  e(&data[hdr+5]);
18e86 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f 6e  .    assert( con
18e87 74 65 6e 74 4f 66 66 73 65 74 3c 3d 75 73 61 62  tentOffset<=usab
18e88 6c 65 53 69 7a 65 20 29 3b 20 20 2f 2a 20 45 6e  leSize );  /* En
18e89 66 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49  forced by btreeI
18e8a 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20  nitPage() */.   
18e8b 20 6d 65 6d 73 65 74 28 68 69 74 2b 63 6f 6e 74   memset(hit+cont
18e8c 65 6e 74 4f 66 66 73 65 74 2c 20 30 2c 20 75 73  entOffset, 0, us
18e8d 61 62 6c 65 53 69 7a 65 2d 63 6f 6e 74 65 6e 74  ableSize-content
18e8e 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 6d 65 6d  Offset);.    mem
18e8f 73 65 74 28 68 69 74 2c 20 31 2c 20 63 6f 6e 74  set(hit, 1, cont
18e90 65 6e 74 4f 66 66 73 65 74 29 3b 0a 20 20 20 20  entOffset);.    
18e91 6e 43 65 6c 6c 20 3d 20 67 65 74 32 62 79 74 65  nCell = get2byte
18e92 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 29 3b 0a  (&data[hdr+3]);.
18e93 20 20 20 20 63 65 6c 6c 53 74 61 72 74 20 3d 20      cellStart = 
18e94 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61  hdr + 12 - 4*pPa
18e95 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 66 6f  ge->leaf;.    fo
18e96 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20  r(i=0; i<nCell; 
18e97 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  i++){.      int 
18e98 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  pc = get2byte(&d
18e99 61 74 61 5b 63 65 6c 6c 53 74 61 72 74 2b 69 2a  ata[cellStart+i*
18e9a 32 5d 29 3b 0a 20 20 20 20 20 20 75 31 36 20 73  2]);.      u16 s
18e9b 69 7a 65 20 3d 20 31 30 32 34 3b 0a 20 20 20 20  ize = 1024;.    
18e9c 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69    int j;.      i
18e9d 66 28 20 70 63 3c 3d 75 73 61 62 6c 65 53 69 7a  f( pc<=usableSiz
18e9e 65 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 73  e-4 ){.        s
18e9f 69 7a 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  ize = cellSizePt
18ea0 72 28 70 50 61 67 65 2c 20 26 64 61 74 61 5b 70  r(pPage, &data[p
18ea1 63 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  c]);.      }.   
18ea2 20 20 20 69 66 28 20 28 70 63 2b 73 69 7a 65 2d     if( (pc+size-
18ea3 31 29 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 29  1)>=usableSize )
18ea4 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41  {.        checkA
18ea5 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
18ea6 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20   0, .           
18ea7 20 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74   "Corruption det
18ea8 65 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64  ected in cell %d
18ea9 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69   on page %d",i,i
18eaa 50 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d  Page,0);.      }
18eab 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f  else{.        fo
18eac 72 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a  r(j=pc+size-1; j
18ead 3e 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a  >=pc; j--) hit[j
18eae 5d 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ]++;.      }.   
18eaf 20 7d 0a 20 20 20 20 69 20 3d 20 67 65 74 32 62   }.    i = get2b
18eb0 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d  yte(&data[hdr+1]
18eb1 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 3e  );.    while( i>
18eb2 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  0 ){.      int s
18eb3 69 7a 65 2c 20 6a 3b 0a 20 20 20 20 20 20 61 73  ize, j;.      as
18eb4 73 65 72 74 28 20 69 3c 3d 75 73 61 62 6c 65 53  sert( i<=usableS
18eb5 69 7a 65 2d 34 20 29 3b 20 20 20 20 20 2f 2a 20  ize-4 );     /* 
18eb6 45 6e 66 6f 72 63 65 64 20 62 79 20 62 74 72 65  Enforced by btre
18eb7 65 49 6e 69 74 50 61 67 65 28 29 20 2a 2f 0a 20  eInitPage() */. 
18eb8 20 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32       size = get2
18eb9 62 79 74 65 28 26 64 61 74 61 5b 69 2b 32 5d 29  byte(&data[i+2])
18eba 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
18ebb 69 2b 73 69 7a 65 3c 3d 75 73 61 62 6c 65 53 69  i+size<=usableSi
18ebc 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63  ze );  /* Enforc
18ebd 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50  ed by btreeInitP
18ebe 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 66  age() */.      f
18ebf 6f 72 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a  or(j=i+size-1; j
18ec0 3e 3d 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d  >=i; j--) hit[j]
18ec1 2b 2b 3b 0a 20 20 20 20 20 20 6a 20 3d 20 67 65  ++;.      j = ge
18ec2 74 32 62 79 74 65 28 26 64 61 74 61 5b 69 5d 29  t2byte(&data[i])
18ec3 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
18ec4 6a 3d 3d 30 20 7c 7c 20 6a 3e 69 2b 73 69 7a 65  j==0 || j>i+size
18ec5 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64   );  /* Enforced
18ec6 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61 67   by btreeInitPag
18ec7 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  e() */.      ass
18ec8 65 72 74 28 20 6a 3c 3d 75 73 61 62 6c 65 53 69  ert( j<=usableSi
18ec9 7a 65 2d 34 20 29 3b 20 20 20 2f 2a 20 45 6e 66  ze-4 );   /* Enf
18eca 6f 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e  orced by btreeIn
18ecb 69 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20  itPage() */.    
18ecc 20 20 69 20 3d 20 6a 3b 0a 20 20 20 20 7d 0a 20    i = j;.    }. 
18ecd 20 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20     for(i=cnt=0; 
18ece 69 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b  i<usableSize; i+
18ecf 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69  +){.      if( hi
18ed0 74 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  t[i]==0 ){.     
18ed1 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20     cnt++;.      
18ed2 7d 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d  }else if( hit[i]
18ed3 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68  >1 ){.        ch
18ed4 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
18ed5 65 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  eck, 0,.        
18ed6 20 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73    "Multiple uses
18ed7 20 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20   for byte %d of 
18ed8 70 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61  page %d", i, iPa
18ed9 67 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ge);.        bre
18eda 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
18edb 7d 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64  }.    if( cnt!=d
18edc 61 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20  ata[hdr+7] ){.  
18edd 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
18ede 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20  sg(pCheck, 0, . 
18edf 20 20 20 20 20 20 20 20 20 22 46 72 61 67 6d 65           "Fragme
18ee0 6e 74 61 74 69 6f 6e 20 6f 66 20 25 64 20 62 79  ntation of %d by
18ee1 74 65 73 20 72 65 70 6f 72 74 65 64 20 61 73 20  tes reported as 
18ee2 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c 0a  %d on page %d",.
18ee3 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20 64            cnt, d
18ee4 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61 67  ata[hdr+7], iPag
18ee5 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
18ee6 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
18ee7 68 69 74 29 3b 0a 20 20 72 65 6c 65 61 73 65 50  hit);.  releaseP
18ee8 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65  age(pPage);.  re
18ee9 74 75 72 6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a  turn depth+1;.}.
18eea 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
18eeb 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
18eec 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65  CHECK */..#ifnde
18eed 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
18eee 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a  TEGRITY_CHECK./*
18eef 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
18ef0 20 64 6f 65 73 20 61 20 63 6f 6d 70 6c 65 74 65   does a complete
18ef1 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 67 69   check of the gi
18ef2 76 65 6e 20 42 54 72 65 65 20 66 69 6c 65 2e 20  ven BTree file. 
18ef3 20 61 52 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61   aRoot[] is.** a
18ef4 6e 20 61 72 72 61 79 20 6f 66 20 70 61 67 65 73  n array of pages
18ef5 20 6e 75 6d 62 65 72 73 20 77 65 72 65 20 65 61   numbers were ea
18ef6 63 68 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  ch page number i
18ef7 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  s the root page 
18ef8 6f 66 0a 2a 2a 20 61 20 74 61 62 6c 65 2e 20 20  of.** a table.  
18ef9 6e 52 6f 6f 74 20 69 73 20 74 68 65 20 6e 75 6d  nRoot is the num
18efa 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
18efb 6e 20 61 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 41  n aRoot..**.** A
18efc 20 72 65 61 64 2d 6f 6e 6c 79 20 6f 72 20 72 65   read-only or re
18efd 61 64 2d 77 72 69 74 65 20 74 72 61 6e 73 61 63  ad-write transac
18efe 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65  tion must be ope
18eff 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  ned before calli
18f00 6e 67 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74  ng.** this funct
18f01 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65  ion..**.** Write
18f02 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
18f03 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70 6e  rror seen in *pn
18f04 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f 72  Err.  Except for
18f05 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20   some memory.** 
18f06 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
18f07 73 2c 20 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  s,  an error mes
18f08 73 61 67 65 20 68 65 6c 64 20 69 6e 20 6d 65 6d  sage held in mem
18f09 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
18f0a 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 69 73 20 72  m.** malloc is r
18f0b 65 74 75 72 6e 65 64 20 69 66 20 2a 70 6e 45 72  eturned if *pnEr
18f0c 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20  r is non-zero.  
18f0d 49 66 20 2a 70 6e 45 72 72 3d 3d 30 20 74 68 65  If *pnErr==0 the
18f0e 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20 72 65 74  n NULL is.** ret
18f0f 75 72 6e 65 64 2e 20 20 49 66 20 61 20 6d 65 6d  urned.  If a mem
18f10 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
18f11 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c  rror occurs, NUL
18f12 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  L is returned..*
18f13 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18f14 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42 74   char *sqlite3Bt
18f15 72 65 65 49 6e 74 65 67 72 69 74 79 43 68 65 63  reeIntegrityChec
18f16 6b 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20  k(.  Btree *p,  
18f17 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65 20     /* The btree 
18f18 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f  to be checked */
18f19 0a 20 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 20  .  int *aRoot,  
18f1a 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20   /* An array of 
18f1b 72 6f 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65  root pages numbe
18f1c 72 73 20 66 6f 72 20 69 6e 64 69 76 69 64 75 61  rs for individua
18f1d 6c 20 74 72 65 65 73 20 2a 2f 0a 20 20 69 6e 74  l trees */.  int
18f1e 20 6e 52 6f 6f 74 2c 20 20 20 20 2f 2a 20 4e 75   nRoot,    /* Nu
18f1f 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
18f20 69 6e 20 61 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20  in aRoot[] */.  
18f21 69 6e 74 20 6d 78 45 72 72 2c 20 20 20 20 2f 2a  int mxErr,    /*
18f22 20 53 74 6f 70 20 72 65 70 6f 72 74 69 6e 67 20   Stop reporting 
18f23 65 72 72 6f 72 73 20 61 66 74 65 72 20 74 68 69  errors after thi
18f24 73 20 6d 61 6e 79 20 2a 2f 0a 20 20 69 6e 74 20  s many */.  int 
18f25 2a 70 6e 45 72 72 20 20 20 20 2f 2a 20 57 72 69  *pnErr    /* Wri
18f26 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  te number of err
18f27 6f 72 73 20 73 65 65 6e 20 74 6f 20 74 68 69 73  ors seen to this
18f28 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a   variable */.){.
18f29 20 20 50 67 6e 6f 20 69 3b 0a 20 20 69 6e 74 20    Pgno i;.  int 
18f2a 6e 52 65 66 3b 0a 20 20 49 6e 74 65 67 72 69 74  nRef;.  Integrit
18f2b 79 43 6b 20 73 43 68 65 63 6b 3b 0a 20 20 42 74  yCk sCheck;.  Bt
18f2c 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
18f2d 3e 70 42 74 3b 0a 20 20 63 68 61 72 20 7a 45 72  >pBt;.  char zEr
18f2e 72 5b 31 30 30 5d 3b 0a 0a 20 20 73 71 6c 69 74  r[100];..  sqlit
18f2f 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
18f30 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e  .  assert( p->in
18f31 54 72 61 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45  Trans>TRANS_NONE
18f32 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73   && pBt->inTrans
18f33 61 63 74 69 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e  action>TRANS_NON
18f34 45 20 29 3b 0a 20 20 6e 52 65 66 20 3d 20 73 71  E );.  nRef = sq
18f35 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
18f36 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  nt(pBt->pPager);
18f37 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d 20  .  sCheck.pBt = 
18f38 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 50  pBt;.  sCheck.pP
18f39 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67  ager = pBt->pPag
18f3a 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 61  er;.  sCheck.nPa
18f3b 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f  ge = pagerPageco
18f3c 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29 3b  unt(sCheck.pBt);
18f3d 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 20  .  sCheck.mxErr 
18f3e 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 63  = mxErr;.  sChec
18f3f 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73 43  k.nErr = 0;.  sC
18f40 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65  heck.mallocFaile
18f41 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72 20  d = 0;.  *pnErr 
18f42 3d 20 30 3b 0a 20 20 69 66 28 20 73 43 68 65 63  = 0;.  if( sChec
18f43 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20  k.nPage==0 ){.  
18f44 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
18f45 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75  ave(p);.    retu
18f46 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65  rn 0;.  }.  sChe
18f47 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74  ck.anRef = sqlit
18f48 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63  e3Malloc( (sChec
18f49 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f  k.nPage+1)*sizeo
18f4a 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30  f(sCheck.anRef[0
18f4b 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68  ]) );.  if( !sCh
18f4c 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20  eck.anRef ){.   
18f4d 20 2a 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20   *pnErr = 1;.   
18f4e 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
18f4f 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72  ve(p);.    retur
18f50 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  n 0;.  }.  for(i
18f51 3d 30 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50  =0; i<=sCheck.nP
18f52 61 67 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63  age; i++){ sChec
18f53 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20  k.anRef[i] = 0; 
18f54 7d 0a 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f  }.  i = PENDING_
18f55 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a  BYTE_PAGE(pBt);.
18f56 20 20 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e    if( i<=sCheck.
18f57 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68  nPage ){.    sCh
18f58 65 63 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31  eck.anRef[i] = 1
18f59 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53  ;.  }.  sqlite3S
18f5a 74 72 41 63 63 75 6d 49 6e 69 74 28 26 73 43 68  trAccumInit(&sCh
18f5b 65 63 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 72 72  eck.errMsg, zErr
18f5c 2c 20 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20  , sizeof(zErr), 
18f5d 32 30 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 68  20000);..  /* Ch
18f5e 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74  eck the integrit
18f5f 79 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73  y of the freelis
18f60 74 0a 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c 69  t.  */.  checkLi
18f61 73 74 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 67  st(&sCheck, 1, g
18f62 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50  et4byte(&pBt->pP
18f63 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29  age1->aData[32])
18f64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 65  ,.            ge
18f65 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61  t4byte(&pBt->pPa
18f66 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c  ge1->aData[36]),
18f67 20 22 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 3a   "Main freelist:
18f68 20 22 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b   ");..  /* Check
18f69 20 61 6c 6c 20 74 68 65 20 74 61 62 6c 65 73 2e   all the tables.
18f6a 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b  .  */.  for(i=0;
18f6b 20 28 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 26   (int)i<nRoot &&
18f6c 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69   sCheck.mxErr; i
18f6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f  ++){.    if( aRo
18f6e 6f 74 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  ot[i]==0 ) conti
18f6f 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  nue;.#ifndef SQL
18f70 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
18f71 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d  UUM.    if( pBt-
18f72 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61  >autoVacuum && a
18f73 52 6f 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20  Root[i]>1 ){.   
18f74 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26     checkPtrmap(&
18f75 73 43 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d  sCheck, aRoot[i]
18f76 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47  , PTRMAP_ROOTPAG
18f77 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  E, 0, 0);.    }.
18f78 23 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b  #endif.    check
18f79 54 72 65 65 50 61 67 65 28 26 73 43 68 65 63 6b  TreePage(&sCheck
18f7a 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73  , aRoot[i], "Lis
18f7b 74 20 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 3a  t of tree roots:
18f7c 20 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d   ");.  }..  /* M
18f7d 61 6b 65 20 73 75 72 65 20 65 76 65 72 79 20 70  ake sure every p
18f7e 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20  age in the file 
18f7f 69 73 20 72 65 66 65 72 65 6e 63 65 64 0a 20 20  is referenced.  
18f80 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c  */.  for(i=1; i<
18f81 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 26 26  =sCheck.nPage &&
18f82 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69   sCheck.mxErr; i
18f83 2b 2b 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  ++){.#ifdef SQLI
18f84 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
18f85 55 4d 0a 20 20 20 20 69 66 28 20 73 43 68 65 63  UM.    if( sChec
18f86 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b  k.anRef[i]==0 ){
18f87 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65  .      checkAppe
18f88 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30  ndMsg(&sCheck, 0
18f89 2c 20 22 50 61 67 65 20 25 64 20 69 73 20 6e 65  , "Page %d is ne
18f8a 76 65 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20  ver used", i);. 
18f8b 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f     }.#else.    /
18f8c 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
18f8d 65 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d  e supports auto-
18f8e 76 61 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72  vacuum, make sur
18f8f 65 20 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74  e no tables cont
18f90 61 69 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65 72  ain.    ** refer
18f91 65 6e 63 65 73 20 74 6f 20 70 6f 69 6e 74 65 72  ences to pointer
18f92 2d 6d 61 70 20 70 61 67 65 73 2e 0a 20 20 20 20  -map pages..    
18f93 2a 2f 0a 20 20 20 20 69 66 28 20 73 43 68 65 63  */.    if( sChec
18f94 6b 2e 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26  k.anRef[i]==0 &&
18f95 20 0a 20 20 20 20 20 20 20 28 50 54 52 4d 41 50   .       (PTRMAP
18f96 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 21  _PAGENO(pBt, i)!
18f97 3d 69 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f  =i || !pBt->auto
18f98 56 61 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20  Vacuum) ){.     
18f99 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
18f9a 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 67  &sCheck, 0, "Pag
18f9b 65 20 25 64 20 69 73 20 6e 65 76 65 72 20 75 73  e %d is never us
18f9c 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20  ed", i);.    }. 
18f9d 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e     if( sCheck.an
18f9e 52 65 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20  Ref[i]!=0 && .  
18f9f 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47       (PTRMAP_PAG
18fa0 45 4e 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20 26  ENO(pBt, i)==i &
18fa1 26 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  & pBt->autoVacuu
18fa2 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63  m) ){.      chec
18fa3 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65  kAppendMsg(&sChe
18fa4 63 6b 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72 20  ck, 0, "Pointer 
18fa5 6d 61 70 20 70 61 67 65 20 25 64 20 69 73 20 72  map page %d is r
18fa6 65 66 65 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a  eferenced", i);.
18fa7 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
18fa8 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
18fa9 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64   this analysis d
18faa 69 64 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79  id not leave any
18fab 20 75 6e 72 65 66 28 29 20 70 61 67 65 73 2e 0a   unref() pages..
18fac 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20    ** This is an 
18fad 69 6e 74 65 72 6e 61 6c 20 63 6f 6e 73 69 73 74  internal consist
18fae 65 6e 63 79 20 63 68 65 63 6b 3b 20 61 6e 20 69  ency check; an i
18faf 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 0a 20  ntegrity check. 
18fb0 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e 74 65 67   ** of the integ
18fb1 72 69 74 79 20 63 68 65 63 6b 2e 0a 20 20 2a 2f  rity check..  */
18fb2 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6e 52 65  .  if( NEVER(nRe
18fb3 66 20 21 3d 20 73 71 6c 69 74 65 33 50 61 67 65  f != sqlite3Page
18fb4 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70  rRefcount(pBt->p
18fb5 50 61 67 65 72 29 29 20 29 7b 0a 20 20 20 20 63  Pager)) ){.    c
18fb6 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73  heckAppendMsg(&s
18fb7 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20  Check, 0, .     
18fb8 20 22 4f 75 74 73 74 61 6e 64 69 6e 67 20 70 61   "Outstanding pa
18fb9 67 65 20 63 6f 75 6e 74 20 67 6f 65 73 20 66 72  ge count goes fr
18fba 6f 6d 20 25 64 20 74 6f 20 25 64 20 64 75 72 69  om %d to %d duri
18fbb 6e 67 20 74 68 69 73 20 61 6e 61 6c 79 73 69 73  ng this analysis
18fbc 22 2c 0a 20 20 20 20 20 20 6e 52 65 66 2c 20 73  ",.      nRef, s
18fbd 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
18fbe 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 29  unt(pBt->pPager)
18fbf 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f  .    );.  }..  /
18fc0 2a 20 43 6c 65 61 6e 20 20 75 70 20 61 6e 64 20  * Clean  up and 
18fc1 72 65 70 6f 72 74 20 65 72 72 6f 72 73 2e 0a 20  report errors.. 
18fc2 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72   */.  sqlite3Btr
18fc3 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 73 71  eeLeave(p);.  sq
18fc4 6c 69 74 65 33 5f 66 72 65 65 28 73 43 68 65 63  lite3_free(sChec
18fc5 6b 2e 61 6e 52 65 66 29 3b 0a 20 20 69 66 28 20  k.anRef);.  if( 
18fc6 73 43 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69  sCheck.mallocFai
18fc7 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  led ){.    sqlit
18fc8 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28  e3StrAccumReset(
18fc9 26 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b  &sCheck.errMsg);
18fca 0a 20 20 20 20 2a 70 6e 45 72 72 20 3d 20 73 43  .    *pnErr = sC
18fcb 68 65 63 6b 2e 6e 45 72 72 2b 31 3b 0a 20 20 20  heck.nErr+1;.   
18fcc 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
18fcd 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b   *pnErr = sCheck
18fce 2e 6e 45 72 72 3b 0a 20 20 69 66 28 20 73 43 68  .nErr;.  if( sCh
18fcf 65 63 6b 2e 6e 45 72 72 3d 3d 30 20 29 20 73 71  eck.nErr==0 ) sq
18fd0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 52 65 73  lite3StrAccumRes
18fd1 65 74 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73  et(&sCheck.errMs
18fd2 67 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  g);.  return sql
18fd3 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
18fd4 73 68 28 26 73 43 68 65 63 6b 2e 65 72 72 4d 73  sh(&sCheck.errMs
18fd5 67 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  g);.}.#endif /* 
18fd6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45  SQLITE_OMIT_INTE
18fd7 47 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a  GRITY_CHECK */..
18fd8 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
18fd9 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f   full pathname o
18fda 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  f the underlying
18fdb 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
18fdc 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  **.** The pager 
18fdd 66 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 76 61  filename is inva
18fde 72 69 61 6e 74 20 61 73 20 6c 6f 6e 67 20 61 73  riant as long as
18fdf 20 74 68 65 20 70 61 67 65 72 20 69 73 0a 2a 2a   the pager is.**
18fe0 20 6f 70 65 6e 20 73 6f 20 69 74 20 69 73 20 73   open so it is s
18fe1 61 66 65 20 74 6f 20 61 63 63 65 73 73 20 77 69  afe to access wi
18fe2 74 68 6f 75 74 20 74 68 65 20 42 74 53 68 61 72  thout the BtShar
18fe3 65 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c  ed mutex..*/.SQL
18fe4 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
18fe5 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42  t char *sqlite3B
18fe6 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28  treeGetFilename(
18fe7 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73  Btree *p){.  ass
18fe8 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61  ert( p->pBt->pPa
18fe9 67 65 72 21 3d 30 20 29 3b 0a 20 20 72 65 74 75  ger!=0 );.  retu
18fea 72 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 46  rn sqlite3PagerF
18feb 69 6c 65 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e  ilename(p->pBt->
18fec 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  pPager);.}../*.*
18fed 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61 74  * Return the pat
18fee 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75  hname of the jou
18fef 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 74 68  rnal file for th
18ff0 69 73 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  is database. The
18ff1 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65   return.** value
18ff2 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
18ff3 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65 67   is the same reg
18ff4 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
18ff5 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  er the journal f
18ff6 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ile.** has been 
18ff7 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74 2e 0a  created or not..
18ff8 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20  **.** The pager 
18ff9 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61 6d 65  journal filename
18ffa 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73   is invariant as
18ffb 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67   long as the pag
18ffc 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f  er is.** open so
18ffd 20 69 74 20 69 73 20 73 61 66 65 20 74 6f 20 61   it is safe to a
18ffe 63 63 65 73 73 20 77 69 74 68 6f 75 74 20 74 68  ccess without th
18fff 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78  e BtShared mutex
19000 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19001 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
19002 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a  sqlite3BtreeGetJ
19003 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65  ournalname(Btree
19004 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
19005 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21 3d  p->pBt->pPager!=
19006 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  0 );.  return sq
19007 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
19008 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50  lname(p->pBt->pP
19009 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ager);.}../*.** 
1900a 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
1900b 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  if a transaction
1900c 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53   is active..*/.S
1900d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1900e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  t sqlite3BtreeIs
1900f 49 6e 54 72 61 6e 73 28 42 74 72 65 65 20 2a 70  InTrans(Btree *p
19010 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d  ){.  assert( p==
19011 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
19012 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
19013 75 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72  utex) );.  retur
19014 6e 20 28 70 20 26 26 20 28 70 2d 3e 69 6e 54 72  n (p && (p->inTr
19015 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
19016 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ));.}../*.** Ret
19017 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20  urn non-zero if 
19018 61 20 72 65 61 64 20 28 6f 72 20 77 72 69 74 65  a read (or write
19019 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  ) transaction is
1901a 20 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49   active..*/.SQLI
1901b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1901c 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52  qlite3BtreeIsInR
1901d 65 61 64 54 72 61 6e 73 28 42 74 72 65 65 20 2a  eadTrans(Btree *
1901e 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 20  p){.  assert( p 
1901f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
19020 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19021 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
19022 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 69 6e 54  .  return p->inT
19023 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45  rans!=TRANS_NONE
19024 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.}..SQLITE_PRIV
19025 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
19026 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70 28 42  treeIsInBackup(B
19027 74 72 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65  tree *p){.  asse
19028 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65 72  rt( p );.  asser
19029 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1902a 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
1902b 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ex) );.  return 
1902c 70 2d 3e 6e 42 61 63 6b 75 70 21 3d 30 3b 0a 7d  p->nBackup!=0;.}
1902d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
1902e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20  ction returns a 
1902f 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f  pointer to a blo
19030 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73 6f  b of memory asso
19031 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 61  ciated with.** a
19032 20 73 69 6e 67 6c 65 20 73 68 61 72 65 64 2d 62   single shared-b
19033 74 72 65 65 2e 20 54 68 65 20 6d 65 6d 6f 72 79  tree. The memory
19034 20 69 73 20 75 73 65 64 20 62 79 20 63 6c 69 65   is used by clie
19035 6e 74 20 63 6f 64 65 20 66 6f 72 20 69 74 73 20  nt code for its 
19036 6f 77 6e 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20  own.** purposes 
19037 28 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 6f  (for example, to
19038 20 73 74 6f 72 65 20 61 20 68 69 67 68 2d 6c 65   store a high-le
19039 76 65 6c 20 73 63 68 65 6d 61 20 61 73 73 6f 63  vel schema assoc
1903a 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20 74  iated with .** t
1903b 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 29  he shared-btree)
1903c 2e 20 54 68 65 20 62 74 72 65 65 20 6c 61 79 65  . The btree laye
1903d 72 20 6d 61 6e 61 67 65 73 20 72 65 66 65 72 65  r manages refere
1903e 6e 63 65 20 63 6f 75 6e 74 69 6e 67 20 69 73 73  nce counting iss
1903f 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ues..**.** The f
19040 69 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 69  irst time this i
19041 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 73 68  s called on a sh
19042 61 72 65 64 2d 62 74 72 65 65 2c 20 6e 42 79 74  ared-btree, nByt
19043 65 73 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  es bytes of memo
19044 72 79 0a 2a 2a 20 61 72 65 20 61 6c 6c 6f 63 61  ry.** are alloca
19045 74 65 64 2c 20 7a 65 72 6f 65 64 2c 20 61 6e 64  ted, zeroed, and
19046 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
19047 20 63 61 6c 6c 65 72 2e 20 46 6f 72 20 65 61 63   caller. For eac
19048 68 20 73 75 62 73 65 71 75 65 6e 74 20 0a 2a 2a  h subsequent .**
19049 20 63 61 6c 6c 20 74 68 65 20 6e 42 79 74 65 73   call the nBytes
1904a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 69 67   parameter is ig
1904b 6e 6f 72 65 64 20 61 6e 64 20 61 20 70 6f 69 6e  nored and a poin
1904c 74 65 72 20 74 6f 20 74 68 65 20 73 61 6d 65 20  ter to the same 
1904d 62 6c 6f 62 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72  blob.** of memor
1904e 79 20 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a  y returned. .**.
1904f 2a 2a 20 49 66 20 74 68 65 20 6e 42 79 74 65 73  ** If the nBytes
19050 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 30 20   parameter is 0 
19051 61 6e 64 20 74 68 65 20 62 6c 6f 62 20 6f 66 20  and the blob of 
19052 6d 65 6d 6f 72 79 20 68 61 73 20 6e 6f 74 20 79  memory has not y
19053 65 74 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63  et been.** alloc
19054 61 74 65 64 2c 20 61 20 6e 75 6c 6c 20 70 6f 69  ated, a null poi
19055 6e 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  nter is returned
19056 2e 20 49 66 20 74 68 65 20 62 6c 6f 62 20 68 61  . If the blob ha
19057 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a 2a  s already been.*
19058 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 69 74 20  * allocated, it 
19059 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 6e  is returned as n
1905a 6f 72 6d 61 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73  ormal..**.** Jus
1905b 74 20 62 65 66 6f 72 65 20 74 68 65 20 73 68 61  t before the sha
1905c 72 65 64 2d 62 74 72 65 65 20 69 73 20 63 6c 6f  red-btree is clo
1905d 73 65 64 2c 20 74 68 65 20 66 75 6e 63 74 69 6f  sed, the functio
1905e 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  n passed as the 
1905f 0a 2a 2a 20 78 46 72 65 65 20 61 72 67 75 6d 65  .** xFree argume
19060 6e 74 20 77 68 65 6e 20 74 68 65 20 6d 65 6d 6f  nt when the memo
19061 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61  ry allocation wa
19062 73 20 6d 61 64 65 20 69 73 20 69 6e 76 6f 6b 65  s made is invoke
19063 64 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 62 6c 6f  d on the .** blo
19064 62 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d  b of allocated m
19065 65 6d 6f 72 79 2e 20 54 68 69 73 20 66 75 6e 63  emory. This func
19066 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20  tion should not 
19067 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 66 72 65  call sqlite3_fre
19068 65 28 29 0a 2a 2a 20 6f 6e 20 74 68 65 20 6d 65  e().** on the me
19069 6d 6f 72 79 2c 20 74 68 65 20 62 74 72 65 65 20  mory, the btree 
1906a 6c 61 79 65 72 20 64 6f 65 73 20 74 68 61 74 2e  layer does that.
1906b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1906c 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
1906d 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65  BtreeSchema(Btre
1906e 65 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74 65 73  e *p, int nBytes
1906f 2c 20 76 6f 69 64 28 2a 78 46 72 65 65 29 28 76  , void(*xFree)(v
19070 6f 69 64 20 2a 29 29 7b 0a 20 20 42 74 53 68 61  oid *)){.  BtSha
19071 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
19072 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  t;.  sqlite3Btre
19073 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28  eEnter(p);.  if(
19074 20 21 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 26   !pBt->pSchema &
19075 26 20 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20  & nBytes ){.    
19076 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 3d 20 73  pBt->pSchema = s
19077 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
19078 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 70 42  (nBytes);.    pB
19079 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 3d  t->xFreeSchema =
1907a 20 78 46 72 65 65 3b 0a 20 20 7d 0a 20 20 73 71   xFree;.  }.  sq
1907b 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
1907c 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 42 74  p);.  return pBt
1907d 2d 3e 70 53 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a  ->pSchema;.}../*
1907e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
1907f 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43  E_LOCKED_SHAREDC
19080 41 43 48 45 20 69 66 20 61 6e 6f 74 68 65 72 20  ACHE if another 
19081 75 73 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65  user of the same
19082 20 73 68 61 72 65 64 20 0a 2a 2a 20 62 74 72 65   shared .** btre
19083 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e  e as the argumen
19084 74 20 68 61 6e 64 6c 65 20 68 6f 6c 64 73 20 61  t handle holds a
19085 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
19086 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69   on the .** sqli
19087 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  te_master table.
19088 20 4f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54   Otherwise SQLIT
19089 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  E_OK..*/.SQLITE_
1908a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1908b 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c 6f  te3BtreeSchemaLo
1908c 63 6b 65 64 28 42 74 72 65 65 20 2a 70 29 7b 0a  cked(Btree *p){.
1908d 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
1908e 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
1908f 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
19090 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
19091 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
19092 20 20 72 63 20 3d 20 71 75 65 72 79 53 68 61 72    rc = queryShar
19093 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
19094 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c  (p, MASTER_ROOT,
19095 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20 20 61   READ_LOCK);.  a
19096 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
19097 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  E_OK || rc==SQLI
19098 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44  TE_LOCKED_SHARED
19099 43 41 43 48 45 20 29 3b 0a 20 20 73 71 6c 69 74  CACHE );.  sqlit
1909a 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
1909b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1909c 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1909d 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1909e 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  HE./*.** Obtain 
1909f 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61  a lock on the ta
190a0 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70  ble whose root p
190a1 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 68  age is iTab.  Th
190a2 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77  e.** lock is a w
190a3 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 57  rite lock if isW
190a4 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 65  ritelock is true
190a5 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a   or a read lock.
190a6 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c 73  ** if it is fals
190a7 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
190a8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
190a9 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42  BtreeLockTable(B
190aa 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61  tree *p, int iTa
190ab 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63  b, u8 isWriteLoc
190ac 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  k){.  int rc = S
190ad 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65  QLITE_OK;.  asse
190ae 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d  rt( p->inTrans!=
190af 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20  TRANS_NONE );.  
190b0 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  if( p->sharable 
190b1 29 7b 0a 20 20 20 20 75 38 20 6c 6f 63 6b 54 79  ){.    u8 lockTy
190b2 70 65 20 3d 20 52 45 41 44 5f 4c 4f 43 4b 20 2b  pe = READ_LOCK +
190b3 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20 20   isWriteLock;.  
190b4 20 20 61 73 73 65 72 74 28 20 52 45 41 44 5f 4c    assert( READ_L
190b5 4f 43 4b 2b 31 3d 3d 57 52 49 54 45 5f 4c 4f 43  OCK+1==WRITE_LOC
190b6 4b 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  K );.    assert(
190b7 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 30 20   isWriteLock==0 
190b8 7c 7c 20 69 73 57 72 69 74 65 4c 6f 63 6b 3d 3d  || isWriteLock==
190b9 31 20 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  1 );..    sqlite
190ba 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
190bb 20 20 20 20 72 63 20 3d 20 71 75 65 72 79 53 68      rc = querySh
190bc 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
190bd 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b  ck(p, iTab, lock
190be 54 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Type);.    if( r
190bf 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
190c0 20 20 20 20 20 20 72 63 20 3d 20 73 65 74 53 68        rc = setSh
190c1 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f  aredCacheTableLo
190c2 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b  ck(p, iTab, lock
190c3 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Type);.    }.   
190c4 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
190c5 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ve(p);.  }.  ret
190c6 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
190c7 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
190c8 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f  _OMIT_INCRBLOB./
190c9 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 43  *.** Argument pC
190ca 73 72 20 6d 75 73 74 20 62 65 20 61 20 63 75 72  sr must be a cur
190cb 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f 72 20 77  sor opened for w
190cc 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a  riting on an .**
190cd 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 20 63 75   INTKEY table cu
190ce 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
190cf 20 61 74 20 61 20 76 61 6c 69 64 20 74 61 62 6c   at a valid tabl
190d0 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 54 68 69  e entry. .** Thi
190d1 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64 69 66  s function modif
190d2 69 65 73 20 74 68 65 20 64 61 74 61 20 73 74 6f  ies the data sto
190d3 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74  red as part of t
190d4 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a  hat entry..**.**
190d5 20 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 63   Only the data c
190d6 6f 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79 20  ontent may only 
190d7 62 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74 20  be modified, it 
190d8 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
190d9 74 6f 20 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68  to .** change th
190da 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
190db 64 61 74 61 20 73 74 6f 72 65 64 2e 20 49 66 20  data stored. If 
190dc 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
190dd 20 63 61 6c 6c 65 64 20 77 69 74 68 0a 2a 2a 20   called with.** 
190de 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 20  parameters that 
190df 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  attempt to write
190e0 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66   past the end of
190e1 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 64 61   the existing da
190e2 74 61 2c 0a 2a 2a 20 6e 6f 20 6d 6f 64 69 66 69  ta,.** no modifi
190e3 63 61 74 69 6f 6e 73 20 61 72 65 20 6d 61 64 65  cations are made
190e4 20 61 6e 64 20 53 51 4c 49 54 45 5f 43 4f 52 52   and SQLITE_CORR
190e5 55 50 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e  UPT is returned.
190e6 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
190e7 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
190e8 72 65 65 50 75 74 44 61 74 61 28 42 74 43 75 72  reePutData(BtCur
190e9 73 6f 72 20 2a 70 43 73 72 2c 20 75 33 32 20 6f  sor *pCsr, u32 o
190ea 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20  ffset, u32 amt, 
190eb 76 6f 69 64 20 2a 7a 29 7b 0a 20 20 69 6e 74 20  void *z){.  int 
190ec 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75  rc;.  assert( cu
190ed 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
190ee 43 73 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Csr) );.  assert
190ef 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
190f0 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 74 72 65  held(pCsr->pBtre
190f1 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  e->db->mutex) );
190f2 0a 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d  .  assert( pCsr-
190f3 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c  >isIncrblobHandl
190f4 65 20 29 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73  e );..  rc = res
190f5 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69  toreCursorPositi
190f6 6f 6e 28 70 43 73 72 29 3b 0a 20 20 69 66 28 20  on(pCsr);.  if( 
190f7 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
190f8 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
190f9 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 43    }.  assert( pC
190fa 73 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53  sr->eState!=CURS
190fb 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29  OR_REQUIRESEEK )
190fc 3b 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53  ;.  if( pCsr->eS
190fd 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c  tate!=CURSOR_VAL
190fe 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ID ){.    return
190ff 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20   SQLITE_ABORT;. 
19100 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 73   }..  /* Check s
19101 6f 6d 65 20 61 73 73 75 6d 70 74 69 6f 6e 73 3a  ome assumptions:
19102 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68 65   .  **   (a) the
19103 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e 20   cursor is open 
19104 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20 2a  for writing,.  *
19105 2a 20 20 20 28 62 29 20 74 68 65 72 65 20 69 73  *   (b) there is
19106 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 74 72   a read/write tr
19107 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 0a  ansaction open,.
19108 20 20 2a 2a 20 20 20 28 63 29 20 74 68 65 20 63    **   (c) the c
19109 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f 6c 64 73 20  onnection holds 
1910a 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
1910b 74 68 65 20 74 61 62 6c 65 20 28 69 66 20 72 65  the table (if re
1910c 71 75 69 72 65 64 29 2c 0a 20 20 2a 2a 20 20 20  quired),.  **   
1910d 28 64 29 20 74 68 65 72 65 20 61 72 65 20 6e 6f  (d) there are no
1910e 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72 65 61   conflicting rea
1910f 64 2d 6c 6f 63 6b 73 2c 20 61 6e 64 0a 20 20 2a  d-locks, and.  *
19110 2a 20 20 20 28 65 29 20 74 68 65 20 63 75 72 73  *   (e) the curs
19111 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 76  or points at a v
19112 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e 20 69  alid row of an i
19113 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20 20 2a  ntKey table..  *
19114 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d 3e 77  /.  if( !pCsr->w
19115 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72 65 74  rFlag ){.    ret
19116 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  urn SQLITE_READO
19117 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  NLY;.  }.  asser
19118 74 28 20 21 70 43 73 72 2d 3e 70 42 74 2d 3e 72  t( !pCsr->pBt->r
19119 65 61 64 4f 6e 6c 79 20 26 26 20 70 43 73 72 2d  eadOnly && pCsr-
1911a 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74  >pBt->inTransact
1911b 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
1911c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61   );.  assert( ha
1911d 73 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  sSharedCacheTabl
1911e 65 4c 6f 63 6b 28 70 43 73 72 2d 3e 70 42 74 72  eLock(pCsr->pBtr
1911f 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f  ee, pCsr->pgnoRo
19120 6f 74 2c 20 30 2c 20 32 29 20 29 3b 0a 20 20 61  ot, 0, 2) );.  a
19121 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64 43  ssert( !hasReadC
19122 6f 6e 66 6c 69 63 74 73 28 70 43 73 72 2d 3e 70  onflicts(pCsr->p
19123 42 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e  Btree, pCsr->pgn
19124 6f 52 6f 6f 74 29 20 29 3b 0a 20 20 61 73 73 65  oRoot) );.  asse
19125 72 74 28 20 70 43 73 72 2d 3e 61 70 50 61 67 65  rt( pCsr->apPage
19126 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e 69  [pCsr->iPage]->i
19127 6e 74 4b 65 79 20 29 3b 0a 0a 20 20 72 65 74 75  ntKey );..  retu
19128 72 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64  rn accessPayload
19129 28 70 43 73 72 2c 20 6f 66 66 73 65 74 2c 20 61  (pCsr, offset, a
1912a 6d 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68  mt, (unsigned ch
1912b 61 72 20 2a 29 7a 2c 20 31 29 3b 0a 7d 0a 0a 2f  ar *)z, 1);.}../
1912c 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67  * .** Set a flag
1912d 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20   on this cursor 
1912e 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f 63  to cache the loc
1912f 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 20  ations of pages 
19130 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65  from the .** ove
19131 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 74  rflow list for t
19132 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20  he current row. 
19133 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
19134 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a  cursors opened.*
19135 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61  * for incrementa
19136 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a  l blob IO only..
19137 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
19138 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20  ion sets a flag 
19139 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c  only. The actual
1913a 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63   page location c
1913b 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20  ache.** (stored 
1913c 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65  in BtCursor.aOve
1913d 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f  rflow[]) is allo
1913e 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62  cated and used b
1913f 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63  y function.** ac
19140 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 74  cessPayload() (t
19141 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 69  he worker functi
19142 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 74  on for sqlite3Bt
19143 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a  reeData() and.**
19144 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74   sqlite3BtreePut
19145 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49  Data())..*/.SQLI
19146 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19147 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68  sqlite3BtreeCach
19148 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73  eOverflow(BtCurs
19149 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
1914a 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
1914b 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
1914c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1914d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72  _mutex_held(pCur
1914e 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75  ->pBtree->db->mu
1914f 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
19150 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c  (!pCur->isIncrbl
19151 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73  obHandle);.  ass
19152 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72  ert(!pCur->aOver
19153 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69  flow);.  pCur->i
19154 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20  sIncrblobHandle 
19155 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  = 1;.}.#endif../
19156 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
19157 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a  nd of btree.c **
19158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19159 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1915a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1915b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1915c 65 67 69 6e 20 66 69 6c 65 20 62 61 63 6b 75 70  egin file backup
1915d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1915e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1915f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
19160 2a 0a 2a 2a 20 32 30 30 39 20 4a 61 6e 75 61 72  *.** 2009 Januar
19161 79 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 28.**.** The a
19162 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
19163 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
19164 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
19165 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
19166 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
19167 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
19168 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
19169 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
1916a 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
1916b 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
1916c 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
1916d 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
1916e 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
1916f 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
19170 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
19171 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
19172 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
19173 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19174 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19176 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19177 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
19178 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69 6d   contains the im
19179 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
1917a 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  the sqlite3_back
1917b 75 70 5f 58 58 58 28 29 20 0a 2a 2a 20 41 50 49  up_XXX() .** API
1917c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74   functions and t
1917d 68 65 20 72 65 6c 61 74 65 64 20 66 65 61 74 75  he related featu
1917e 72 65 73 2e 0a 2a 2f 0a 0a 2f 2a 20 4d 61 63 72  res..*/../* Macr
1917f 6f 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69  o to find the mi
19180 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d  nimum of two num
19181 65 72 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a  eric values..*/.
19182 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65  #ifndef MIN.# de
19183 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28  fine MIN(x,y) ((
19184 78 29 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a  x)<(y)?(x):(y)).
19185 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 74  #endif../*.** St
19186 72 75 63 74 75 72 65 20 61 6c 6c 6f 63 61 74 65  ructure allocate
19187 64 20 66 6f 72 20 65 61 63 68 20 62 61 63 6b 75  d for each backu
19188 70 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a  p operation..*/.
19189 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62  struct sqlite3_b
1918a 61 63 6b 75 70 20 7b 0a 20 20 73 71 6c 69 74 65  ackup {.  sqlite
1918b 33 2a 20 70 44 65 73 74 44 62 3b 20 20 20 20 20  3* pDestDb;     
1918c 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f     /* Destinatio
1918d 6e 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  n database handl
1918e 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 44  e */.  Btree *pD
1918f 65 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  est;            
19190 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 62  /* Destination b
19191 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a 20 20  -tree file */.  
19192 75 33 32 20 69 44 65 73 74 53 63 68 65 6d 61 3b  u32 iDestSchema;
19193 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67           /* Orig
19194 69 6e 61 6c 20 73 63 68 65 6d 61 20 63 6f 6f 6b  inal schema cook
19195 69 65 20 69 6e 20 64 65 73 74 69 6e 61 74 69 6f  ie in destinatio
19196 6e 20 2a 2f 0a 20 20 69 6e 74 20 62 44 65 73 74  n */.  int bDest
19197 4c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20 20  Locked;         
19198 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20 61 20 77  /* True once a w
19199 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
1919a 20 69 73 20 6f 70 65 6e 20 6f 6e 20 70 44 65 73   is open on pDes
1919b 74 20 2a 2f 0a 0a 20 20 50 67 6e 6f 20 69 4e 65  t */..  Pgno iNe
1919c 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  xt;             
1919d 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
1919e 6f 66 20 74 68 65 20 6e 65 78 74 20 73 6f 75 72  of the next sour
1919f 63 65 20 70 61 67 65 20 74 6f 20 63 6f 70 79 20  ce page to copy 
191a0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 2a 20 70 53  */.  sqlite3* pS
191a1 72 63 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a  rcDb;         /*
191a2 20 53 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   Source database
191a3 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72   handle */.  Btr
191a4 65 65 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20  ee *pSrc;       
191a5 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20        /* Source 
191a6 62 2d 74 72 65 65 20 66 69 6c 65 20 2a 2f 0a 0a  b-tree file */..
191a7 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
191a8 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61             /* Ba
191a9 63 6b 75 70 20 70 72 6f 63 65 73 73 20 65 72 72  ckup process err
191aa 6f 72 20 63 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a  or code */..  /*
191ab 20 54 68 65 73 65 20 74 77 6f 20 76 61 72 69 61   These two varia
191ac 62 6c 65 73 20 61 72 65 20 73 65 74 20 62 79 20  bles are set by 
191ad 65 76 65 72 79 20 63 61 6c 6c 20 74 6f 20 62 61  every call to ba
191ae 63 6b 75 70 5f 73 74 65 70 28 29 2e 20 54 68 65  ckup_step(). The
191af 79 20 61 72 65 0a 20 20 2a 2a 20 72 65 61 64 20  y are.  ** read 
191b0 62 79 20 63 61 6c 6c 73 20 74 6f 20 62 61 63 6b  by calls to back
191b1 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61  up_remaining() a
191b2 6e 64 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f  nd backup_pageco
191b3 75 6e 74 28 29 2e 0a 20 20 2a 2f 0a 20 20 50 67  unt()..  */.  Pg
191b4 6e 6f 20 6e 52 65 6d 61 69 6e 69 6e 67 3b 20 20  no nRemaining;  
191b5 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
191b6 20 6f 66 20 70 61 67 65 73 20 6c 65 66 74 20 74   of pages left t
191b7 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 50 67 6e 6f  o copy */.  Pgno
191b8 20 6e 50 61 67 65 63 6f 75 6e 74 3b 20 20 20 20   nPagecount;    
191b9 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
191ba 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f  mber of pages to
191bb 20 63 6f 70 79 20 2a 2f 0a 0a 20 20 69 6e 74 20   copy */..  int 
191bc 69 73 41 74 74 61 63 68 65 64 3b 20 20 20 20 20  isAttached;     
191bd 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63       /* True onc
191be 65 20 62 61 63 6b 75 70 20 68 61 73 20 62 65 65  e backup has bee
191bf 6e 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74  n registered wit
191c0 68 20 70 61 67 65 72 20 2a 2f 0a 20 20 73 71 6c  h pager */.  sql
191c1 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 4e 65  ite3_backup *pNe
191c2 78 74 3b 20 20 20 2f 2a 20 4e 65 78 74 20 62 61  xt;   /* Next ba
191c3 63 6b 75 70 20 61 73 73 6f 63 69 61 74 65 64 20  ckup associated 
191c4 77 69 74 68 20 73 6f 75 72 63 65 20 70 61 67 65  with source page
191c5 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  r */.};../*.** T
191c6 48 52 45 41 44 20 53 41 46 45 54 59 20 4e 4f 54  HREAD SAFETY NOT
191c7 45 53 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 6e 63 65  ES:.**.**   Once
191c8 20 69 74 20 68 61 73 20 62 65 65 6e 20 63 72 65   it has been cre
191c9 61 74 65 64 20 75 73 69 6e 67 20 62 61 63 6b 75  ated using backu
191ca 70 5f 69 6e 69 74 28 29 2c 20 61 20 73 69 6e 67  p_init(), a sing
191cb 6c 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  le sqlite3_backu
191cc 70 0a 2a 2a 20 20 20 73 74 72 75 63 74 75 72 65  p.**   structure
191cd 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64   may be accessed
191ce 20 76 69 61 20 74 77 6f 20 67 72 6f 75 70 73 20   via two groups 
191cf 6f 66 20 74 68 72 65 61 64 2d 73 61 66 65 20 65  of thread-safe e
191d0 6e 74 72 79 20 70 6f 69 6e 74 73 3a 0a 2a 2a 0a  ntry points:.**.
191d1 2a 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65  **     * Via the
191d2 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
191d3 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74 69  XXX() API functi
191d4 6f 6e 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29  on backup_step()
191d5 20 61 6e 64 20 0a 2a 2a 20 20 20 20 20 20 20 62   and .**       b
191d6 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2e 20  ackup_finish(). 
191d7 42 6f 74 68 20 74 68 65 73 65 20 66 75 6e 63 74  Both these funct
191d8 69 6f 6e 73 20 6f 62 74 61 69 6e 20 74 68 65 20  ions obtain the 
191d9 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 0a  source database.
191da 2a 2a 20 20 20 20 20 20 20 68 61 6e 64 6c 65 20  **       handle 
191db 6d 75 74 65 78 20 61 6e 64 20 74 68 65 20 6d 75  mutex and the mu
191dc 74 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  tex associated w
191dd 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 42  ith the source B
191de 74 53 68 61 72 65 64 20 0a 2a 2a 20 20 20 20 20  tShared .**     
191df 20 20 73 74 72 75 63 74 75 72 65 2c 20 69 6e 20    structure, in 
191e0 74 68 61 74 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a  that order..**.*
191e1 2a 20 20 20 20 20 2a 20 56 69 61 20 74 68 65 20  *     * Via the 
191e2 42 61 63 6b 75 70 55 70 64 61 74 65 28 29 20 61  BackupUpdate() a
191e3 6e 64 20 42 61 63 6b 75 70 52 65 73 74 61 72 74  nd BackupRestart
191e4 28 29 20 66 75 6e 63 74 69 6f 6e 73 2c 20 77 68  () functions, wh
191e5 69 63 68 20 61 72 65 0a 2a 2a 20 20 20 20 20 20  ich are.**      
191e6 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20   invoked by the 
191e7 70 61 67 65 72 20 6c 61 79 65 72 20 74 6f 20 72  pager layer to r
191e8 65 70 6f 72 74 20 76 61 72 69 6f 75 73 20 73 74  eport various st
191e9 61 74 65 20 63 68 61 6e 67 65 73 20 69 6e 0a 2a  ate changes in.*
191ea 2a 20 20 20 20 20 20 20 74 68 65 20 70 61 67 65  *       the page
191eb 20 63 61 63 68 65 20 61 73 73 6f 63 69 61 74 65   cache associate
191ec 64 20 77 69 74 68 20 74 68 65 20 73 6f 75 72 63  d with the sourc
191ed 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20  e database. The 
191ee 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20 20 20 61  mutex.**       a
191ef 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
191f0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
191f1 73 65 20 42 74 53 68 61 72 65 64 20 73 74 72 75  se BtShared stru
191f2 63 74 75 72 65 20 77 69 6c 6c 20 61 6c 77 61 79  cture will alway
191f3 73 20 0a 2a 2a 20 20 20 20 20 20 20 62 65 20 68  s .**       be h
191f4 65 6c 64 20 77 68 65 6e 20 65 69 74 68 65 72 20  eld when either 
191f5 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  of these functio
191f6 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65 64 2e 0a  ns are invoked..
191f7 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 6f 74 68 65  **.**   The othe
191f8 72 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  r sqlite3_backup
191f9 5f 58 58 58 28 29 20 41 50 49 20 66 75 6e 63 74  _XXX() API funct
191fa 69 6f 6e 73 2c 20 62 61 63 6b 75 70 5f 72 65 6d  ions, backup_rem
191fb 61 69 6e 69 6e 67 28 29 20 61 6e 64 0a 2a 2a 20  aining() and.** 
191fc 20 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75    backup_pagecou
191fd 6e 74 28 29 20 61 72 65 20 6e 6f 74 20 74 68 72  nt() are not thr
191fe 65 61 64 2d 73 61 66 65 20 66 75 6e 63 74 69 6f  ead-safe functio
191ff 6e 73 2e 20 49 66 20 74 68 65 79 20 61 72 65 20  ns. If they are 
19200 63 61 6c 6c 65 64 0a 2a 2a 20 20 20 77 68 69 6c  called.**   whil
19201 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72  e some other thr
19202 65 61 64 20 69 73 20 63 61 6c 6c 69 6e 67 20 62  ead is calling b
19203 61 63 6b 75 70 5f 73 74 65 70 28 29 20 6f 72 20  ackup_step() or 
19204 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 2c  backup_finish(),
19205 0a 2a 2a 20 20 20 74 68 65 20 76 61 6c 75 65 73  .**   the values
19206 20 72 65 74 75 72 6e 65 64 20 6d 61 79 20 62 65   returned may be
19207 20 69 6e 76 61 6c 69 64 2e 20 54 68 65 72 65 20   invalid. There 
19208 69 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 61 20  is no way for a 
19209 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 42 61 63  call to.**   Bac
1920a 6b 75 70 55 70 64 61 74 65 28 29 20 6f 72 20 42  kupUpdate() or B
1920b 61 63 6b 75 70 52 65 73 74 61 72 74 28 29 20 74  ackupRestart() t
1920c 6f 20 69 6e 74 65 72 66 65 72 65 20 77 69 74 68  o interfere with
1920d 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e   backup_remainin
1920e 67 28 29 0a 2a 2a 20 20 20 6f 72 20 62 61 63 6b  g().**   or back
1920f 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 2e 0a  up_pagecount()..
19210 2a 2a 0a 2a 2a 20 20 20 44 65 70 65 6e 64 69 6e  **.**   Dependin
19211 67 20 6f 6e 20 74 68 65 20 53 51 4c 69 74 65 20  g on the SQLite 
19212 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 74  configuration, t
19213 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
19214 6c 65 73 20 61 6e 64 2f 6f 72 0a 2a 2a 20 20 20  les and/or.**   
19215 74 68 65 20 42 74 72 65 65 20 6f 62 6a 65 63 74  the Btree object
19216 73 20 6d 61 79 20 68 61 76 65 20 74 68 65 69 72  s may have their
19217 20 6f 77 6e 20 6d 75 74 65 78 65 73 20 74 68 61   own mutexes tha
19218 74 20 72 65 71 75 69 72 65 20 6c 6f 63 6b 69 6e  t require lockin
19219 67 2e 0a 2a 2a 20 20 20 4e 6f 6e 2d 73 68 61 72  g..**   Non-shar
1921a 61 62 6c 65 20 42 74 72 65 65 73 20 28 69 6e 2d  able Btrees (in-
1921b 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 73  memory databases
1921c 20 66 6f 72 20 65 78 61 6d 70 6c 65 29 2c 20 64   for example), d
1921d 6f 20 6e 6f 74 20 68 61 76 65 0a 2a 2a 20 20 20  o not have.**   
1921e 61 73 73 6f 63 69 61 74 65 64 20 6d 75 74 65 78  associated mutex
1921f 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  es..*/../*.** Re
19220 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 63  turn a pointer c
19221 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
19222 64 61 74 61 62 61 73 65 20 7a 44 62 20 28 69 2e  database zDb (i.
19223 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70  e. "main", "temp
19224 22 29 0a 2a 2a 20 69 6e 20 63 6f 6e 6e 65 63 74  ").** in connect
19225 69 6f 6e 20 68 61 6e 64 6c 65 20 70 44 62 2e 20  ion handle pDb. 
19226 49 66 20 73 75 63 68 20 61 20 64 61 74 61 62 61  If such a databa
19227 73 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  se cannot be fou
19228 6e 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  nd, return.** a 
19229 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 6e 64  NULL pointer and
1922a 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20   write an error 
1922b 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72 72 6f  message to pErro
1922c 72 44 62 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  rDb..**.** If th
1922d 65 20 22 74 65 6d 70 22 20 64 61 74 61 62 61 73  e "temp" databas
1922e 65 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20  e is requested, 
1922f 69 74 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62  it may need to b
19230 65 20 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73  e opened by this
19231 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49   .** function. I
19232 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
19233 73 20 77 68 69 6c 65 20 64 6f 69 6e 67 20 73 6f  s while doing so
19234 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 77  , return 0 and w
19235 72 69 74 65 20 61 6e 20 0a 2a 2a 20 65 72 72 6f  rite an .** erro
19236 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70 45 72  r message to pEr
19237 72 6f 72 44 62 2e 0a 2a 2f 0a 73 74 61 74 69 63  rorDb..*/.static
19238 20 42 74 72 65 65 20 2a 66 69 6e 64 42 74 72 65   Btree *findBtre
19239 65 28 73 71 6c 69 74 65 33 20 2a 70 45 72 72 6f  e(sqlite3 *pErro
1923a 72 44 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 44  rDb, sqlite3 *pD
1923b 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
1923c 44 62 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 73  Db){.  int i = s
1923d 71 6c 69 74 65 33 46 69 6e 64 44 62 4e 61 6d 65  qlite3FindDbName
1923e 28 70 44 62 2c 20 7a 44 62 29 3b 0a 0a 20 20 69  (pDb, zDb);..  i
1923f 66 28 20 69 3d 3d 31 20 29 7b 0a 20 20 20 20 50  f( i==1 ){.    P
19240 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 20 20  arse *pParse;.  
19241 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20    int rc = 0;.  
19242 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c 69 74    pParse = sqlit
19243 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f  e3StackAllocZero
19244 28 70 45 72 72 6f 72 44 62 2c 20 73 69 7a 65 6f  (pErrorDb, sizeo
19245 66 28 2a 70 50 61 72 73 65 29 29 3b 0a 20 20 20  f(*pParse));.   
19246 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30 20 29   if( pParse==0 )
19247 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
19248 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 53  rror(pErrorDb, S
19249 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 22 6f 75  QLITE_NOMEM, "ou
1924a 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20  t of memory");. 
1924b 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1924c 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73  _NOMEM;.    }els
1924d 65 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  e{.      pParse-
1924e 3e 64 62 20 3d 20 70 44 62 3b 0a 20 20 20 20 20  >db = pDb;.     
1924f 20 69 66 28 20 73 71 6c 69 74 65 33 4f 70 65 6e   if( sqlite3Open
19250 54 65 6d 70 44 61 74 61 62 61 73 65 28 70 50 61  TempDatabase(pPa
19251 72 73 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20  rse) ){.        
19252 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61  sqlite3ErrorClea
19253 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  r(pParse);.     
19254 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
19255 70 45 72 72 6f 72 44 62 2c 20 70 50 61 72 73 65  pErrorDb, pParse
19256 2d 3e 72 63 2c 20 22 25 73 22 2c 20 70 50 61 72  ->rc, "%s", pPar
19257 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  se->zErrMsg);.  
19258 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
19259 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d  E_ERROR;.      }
1925a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
1925b 61 63 6b 46 72 65 65 28 70 45 72 72 6f 72 44 62  ackFree(pErrorDb
1925c 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d  , pParse);.    }
1925d 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
1925e 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
1925f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
19260 69 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  i<0 ){.    sqlit
19261 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72 44 62  e3Error(pErrorDb
19262 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20  , SQLITE_ERROR, 
19263 22 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73  "unknown databas
19264 65 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20  e %s", zDb);.   
19265 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
19266 20 20 72 65 74 75 72 6e 20 70 44 62 2d 3e 61 44    return pDb->aD
19267 62 5b 69 5d 2e 70 42 74 3b 0a 7d 0a 0a 2f 2a 0a  b[i].pBt;.}../*.
19268 2a 2a 20 43 72 65 61 74 65 20 61 6e 20 73 71 6c  ** Create an sql
19269 69 74 65 33 5f 62 61 63 6b 75 70 20 70 72 6f 63  ite3_backup proc
1926a 65 73 73 20 74 6f 20 63 6f 70 79 20 74 68 65 20  ess to copy the 
1926b 63 6f 6e 74 65 6e 74 73 20 6f 66 20 7a 53 72 63  contents of zSrc
1926c 44 62 20 66 72 6f 6d 0a 2a 2a 20 63 6f 6e 6e 65  Db from.** conne
1926d 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 70 53 72  ction handle pSr
1926e 63 44 62 20 74 6f 20 7a 44 65 73 74 44 62 20 69  cDb to zDestDb i
1926f 6e 20 70 44 65 73 74 44 62 2e 20 49 66 20 73 75  n pDestDb. If su
19270 63 63 65 73 73 66 75 6c 2c 20 72 65 74 75 72 6e  ccessful, return
19271 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  .** a pointer to
19272 20 74 68 65 20 6e 65 77 20 73 71 6c 69 74 65 33   the new sqlite3
19273 5f 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 2e 0a  _backup object..
19274 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f  **.** If an erro
19275 72 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69  r occurs, NULL i
19276 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61  s returned and a
19277 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  n error code and
19278 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a   error message.*
19279 2a 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61  * stored in data
1927a 62 61 73 65 20 68 61 6e 64 6c 65 20 70 44 65 73  base handle pDes
1927b 74 44 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  tDb..*/.SQLITE_A
1927c 50 49 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  PI sqlite3_backu
1927d 70 20 2a 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  p *sqlite3_backu
1927e 70 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65  p_init(.  sqlite
1927f 33 2a 20 70 44 65 73 74 44 62 2c 20 20 20 20 20  3* pDestDb,     
19280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19281 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20 77  /* Database to w
19282 72 69 74 65 20 74 6f 20 2a 2f 0a 20 20 63 6f 6e  rite to */.  con
19283 73 74 20 63 68 61 72 20 2a 7a 44 65 73 74 44 62  st char *zDestDb
19284 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
19285 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
19286 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 70 44  tabase within pD
19287 65 73 74 44 62 20 2a 2f 0a 20 20 73 71 6c 69 74  estDb */.  sqlit
19288 65 33 2a 20 70 53 72 63 44 62 2c 20 20 20 20 20  e3* pSrcDb,     
19289 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1928a 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
1928b 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 61 64 20  nection to read 
1928c 66 72 6f 6d 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  from */.  const 
1928d 63 68 61 72 20 2a 7a 53 72 63 44 62 20 20 20 20  char *zSrcDb    
1928e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1928f 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62  /* Name of datab
19290 61 73 65 20 77 69 74 68 69 6e 20 70 53 72 63 44  ase within pSrcD
19291 62 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  b */.){.  sqlite
19292 33 5f 62 61 63 6b 75 70 20 2a 70 3b 20 20 20 20  3_backup *p;    
19293 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19294 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75  /* Value to retu
19295 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 6b  rn */..  /* Lock
19296 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
19297 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 65  base handle. The
19298 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
19299 61 62 61 73 65 0a 20 20 2a 2a 20 68 61 6e 64 6c  abase.  ** handl
1929a 65 20 69 73 20 6e 6f 74 20 6c 6f 63 6b 65 64 20  e is not locked 
1929b 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  in this routine,
1929c 20 62 75 74 20 69 74 20 69 73 20 6c 6f 63 6b 65   but it is locke
1929d 64 20 69 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65  d in.  ** sqlite
1929e 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e  3_backup_step().
1929f 20 54 68 65 20 75 73 65 72 20 69 73 20 72 65 71   The user is req
192a0 75 69 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20  uired to ensure 
192a1 74 68 61 74 20 6e 6f 0a 20 20 2a 2a 20 6f 74 68  that no.  ** oth
192a2 65 72 20 74 68 72 65 61 64 20 61 63 63 65 73 73  er thread access
192a3 65 73 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  es the destinati
192a4 6f 6e 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68  on handle for th
192a5 65 20 64 75 72 61 74 69 6f 6e 0a 20 20 2a 2a 20  e duration.  ** 
192a6 6f 66 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70  of the backup op
192a7 65 72 61 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74  eration.  Any at
192a8 74 65 6d 70 74 20 74 6f 20 75 73 65 20 74 68 65  tempt to use the
192a9 20 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 2a   destination.  *
192aa 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
192ab 63 74 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61  ction while a ba
192ac 63 6b 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72  ckup is in progr
192ad 65 73 73 20 6d 61 79 20 63 61 75 73 65 0a 20 20  ess may cause.  
192ae 2a 2a 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e  ** a malfunction
192af 20 6f 72 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a   or a deadlock..
192b0 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d    */.  sqlite3_m
192b1 75 74 65 78 5f 65 6e 74 65 72 28 70 53 72 63 44  utex_enter(pSrcD
192b2 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  b->mutex);.  sql
192b3 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
192b4 28 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29  (pDestDb->mutex)
192b5 3b 0a 0a 20 20 69 66 28 20 70 53 72 63 44 62 3d  ;..  if( pSrcDb=
192b6 3d 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20  =pDestDb ){.    
192b7 73 71 6c 69 74 65 33 45 72 72 6f 72 28 0a 20 20  sqlite3Error(.  
192b8 20 20 20 20 20 20 70 44 65 73 74 44 62 2c 20 53        pDestDb, S
192b9 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 22 73 6f  QLITE_ERROR, "so
192ba 75 72 63 65 20 61 6e 64 20 64 65 73 74 69 6e 61  urce and destina
192bb 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 69 73  tion must be dis
192bc 74 69 6e 63 74 22 0a 20 20 20 20 29 3b 0a 20 20  tinct".    );.  
192bd 20 20 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65    p = 0;.  }else
192be 20 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61   {.    /* Alloca
192bf 74 65 20 73 70 61 63 65 20 66 6f 72 20 61 20 6e  te space for a n
192c0 65 77 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ew sqlite3_backu
192c1 70 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20  p object */.    
192c2 70 20 3d 20 28 73 71 6c 69 74 65 33 5f 62 61 63  p = (sqlite3_bac
192c3 6b 75 70 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61  kup *)sqlite3_ma
192c4 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 73 71 6c 69  lloc(sizeof(sqli
192c5 74 65 33 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20  te3_backup));.  
192c6 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20    if( !p ){.    
192c7 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70    sqlite3Error(p
192c8 44 65 73 74 44 62 2c 20 53 51 4c 49 54 45 5f 4e  DestDb, SQLITE_N
192c9 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  OMEM, 0);.    }.
192ca 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
192cb 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 63 63   allocation succ
192cc 65 65 64 65 64 2c 20 70 6f 70 75 6c 61 74 65 20  eeded, populate 
192cd 74 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20  the new object. 
192ce 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  */.  if( p ){.  
192cf 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73    memset(p, 0, s
192d0 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 62 61  izeof(sqlite3_ba
192d1 63 6b 75 70 29 29 3b 0a 20 20 20 20 70 2d 3e 70  ckup));.    p->p
192d2 53 72 63 20 3d 20 66 69 6e 64 42 74 72 65 65 28  Src = findBtree(
192d3 70 44 65 73 74 44 62 2c 20 70 53 72 63 44 62 2c  pDestDb, pSrcDb,
192d4 20 7a 53 72 63 44 62 29 3b 0a 20 20 20 20 70 2d   zSrcDb);.    p-
192d5 3e 70 44 65 73 74 20 3d 20 66 69 6e 64 42 74 72  >pDest = findBtr
192d6 65 65 28 70 44 65 73 74 44 62 2c 20 70 44 65 73  ee(pDestDb, pDes
192d7 74 44 62 2c 20 7a 44 65 73 74 44 62 29 3b 0a 20  tDb, zDestDb);. 
192d8 20 20 20 70 2d 3e 70 44 65 73 74 44 62 20 3d 20     p->pDestDb = 
192d9 70 44 65 73 74 44 62 3b 0a 20 20 20 20 70 2d 3e  pDestDb;.    p->
192da 70 53 72 63 44 62 20 3d 20 70 53 72 63 44 62 3b  pSrcDb = pSrcDb;
192db 0a 20 20 20 20 70 2d 3e 69 4e 65 78 74 20 3d 20  .    p->iNext = 
192dc 31 3b 0a 20 20 20 20 70 2d 3e 69 73 41 74 74 61  1;.    p->isAtta
192dd 63 68 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 69  ched = 0;..    i
192de 66 28 20 30 3d 3d 70 2d 3e 70 53 72 63 20 7c 7c  f( 0==p->pSrc ||
192df 20 30 3d 3d 70 2d 3e 70 44 65 73 74 20 29 7b 0a   0==p->pDest ){.
192e0 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 28 6f 72        /* One (or
192e1 20 62 6f 74 68 29 20 6f 66 20 74 68 65 20 6e 61   both) of the na
192e2 6d 65 64 20 64 61 74 61 62 61 73 65 73 20 64 69  med databases di
192e3 64 20 6e 6f 74 20 65 78 69 73 74 2e 20 41 6e 20  d not exist. An 
192e4 65 72 72 6f 72 20 68 61 73 0a 20 20 20 20 20 20  error has.      
192e5 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ** already been 
192e6 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
192e7 20 70 44 65 73 74 44 62 20 68 61 6e 64 6c 65 2e   pDestDb handle.
192e8 20 41 6c 6c 20 74 68 61 74 20 69 73 20 6c 65 66   All that is lef
192e9 74 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 64 6f  t.      ** to do
192ea 20 68 65 72 65 20 69 73 20 66 72 65 65 20 74 68   here is free th
192eb 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  e sqlite3_backup
192ec 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20   structure..    
192ed 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
192ee 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20 20  e3_free(p);.    
192ef 20 20 70 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20    p = 0;.    }. 
192f0 20 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20   }.  if( p ){.  
192f1 20 20 70 2d 3e 70 53 72 63 2d 3e 6e 42 61 63 6b    p->pSrc->nBack
192f2 75 70 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  up++;.  }..  sql
192f3 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
192f4 28 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29  (pDestDb->mutex)
192f5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
192f6 78 5f 6c 65 61 76 65 28 70 53 72 63 44 62 2d 3e  x_leave(pSrcDb->
192f7 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
192f8 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67   p;.}../*.** Arg
192f9 75 6d 65 6e 74 20 72 63 20 69 73 20 61 6e 20 53  ument rc is an S
192fa 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
192fb 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  . Return true if
192fc 20 74 68 69 73 20 65 72 72 6f 72 20 69 73 20 0a   this error is .
192fd 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64 20 66 61  ** considered fa
192fe 74 61 6c 20 69 66 20 65 6e 63 6f 75 6e 74 65 72  tal if encounter
192ff 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b  ed during a back
19300 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 41 6c  up operation. Al
19301 6c 20 65 72 72 6f 72 73 0a 2a 2a 20 61 72 65 20  l errors.** are 
19302 63 6f 6e 73 69 64 65 72 65 64 20 66 61 74 61 6c  considered fatal
19303 20 65 78 63 65 70 74 20 66 6f 72 20 53 51 4c 49   except for SQLI
19304 54 45 5f 42 55 53 59 20 61 6e 64 20 53 51 4c 49  TE_BUSY and SQLI
19305 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 73 74  TE_LOCKED..*/.st
19306 61 74 69 63 20 69 6e 74 20 69 73 46 61 74 61 6c  atic int isFatal
19307 45 72 72 6f 72 28 69 6e 74 20 72 63 29 7b 0a 20  Error(int rc){. 
19308 20 72 65 74 75 72 6e 20 28 72 63 21 3d 53 51 4c   return (rc!=SQL
19309 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d 53 51  ITE_OK && rc!=SQ
1930a 4c 49 54 45 5f 42 55 53 59 20 26 26 20 41 4c 57  LITE_BUSY && ALW
1930b 41 59 53 28 72 63 21 3d 53 51 4c 49 54 45 5f 4c  AYS(rc!=SQLITE_L
1930c 4f 43 4b 45 44 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  OCKED));.}../*.*
1930d 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 53 72 63  * Parameter zSrc
1930e 44 61 74 61 20 70 6f 69 6e 74 73 20 74 6f 20 61  Data points to a
1930f 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   buffer containi
19310 6e 67 20 74 68 65 20 64 61 74 61 20 66 6f 72 20  ng the data for 
19311 0a 2a 2a 20 70 61 67 65 20 69 53 72 63 50 67 20  .** page iSrcPg 
19312 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20  from the source 
19313 64 61 74 61 62 61 73 65 2e 20 43 6f 70 79 20 74  database. Copy t
19314 68 69 73 20 64 61 74 61 20 69 6e 74 6f 20 74 68  his data into th
19315 65 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f  e .** destinatio
19316 6e 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73  n database..*/.s
19317 74 61 74 69 63 20 69 6e 74 20 62 61 63 6b 75 70  tatic int backup
19318 4f 6e 65 50 61 67 65 28 73 71 6c 69 74 65 33 5f  OnePage(sqlite3_
19319 62 61 63 6b 75 70 20 2a 70 2c 20 50 67 6e 6f 20  backup *p, Pgno 
1931a 69 53 72 63 50 67 2c 20 63 6f 6e 73 74 20 75 38  iSrcPg, const u8
1931b 20 2a 7a 53 72 63 44 61 74 61 29 7b 0a 20 20 50   *zSrcData){.  P
1931c 61 67 65 72 20 2a 20 63 6f 6e 73 74 20 70 44 65  ager * const pDe
1931d 73 74 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65  stPager = sqlite
1931e 33 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70  3BtreePager(p->p
1931f 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73 74 20 69  Dest);.  const i
19320 6e 74 20 6e 53 72 63 50 67 73 7a 20 3d 20 73 71  nt nSrcPgsz = sq
19321 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
19322 65 53 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0a  eSize(p->pSrc);.
19323 20 20 69 6e 74 20 6e 44 65 73 74 50 67 73 7a 20    int nDestPgsz 
19324 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
19325 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44 65  tPageSize(p->pDe
19326 73 74 29 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74  st);.  const int
19327 20 6e 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 53 72   nCopy = MIN(nSr
19328 63 50 67 73 7a 2c 20 6e 44 65 73 74 50 67 73 7a  cPgsz, nDestPgsz
19329 29 3b 0a 20 20 63 6f 6e 73 74 20 69 36 34 20 69  );.  const i64 i
1932a 45 6e 64 20 3d 20 28 69 36 34 29 69 53 72 63 50  End = (i64)iSrcP
1932b 67 2a 28 69 36 34 29 6e 53 72 63 50 67 73 7a 3b  g*(i64)nSrcPgsz;
1932c 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
1932d 49 54 45 5f 4f 4b 3b 0a 20 20 69 36 34 20 69 4f  ITE_OK;.  i64 iO
1932e 66 66 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  ff;..  assert( p
1932f 2d 3e 62 44 65 73 74 4c 6f 63 6b 65 64 20 29 3b  ->bDestLocked );
19330 0a 20 20 61 73 73 65 72 74 28 20 21 69 73 46 61  .  assert( !isFa
19331 74 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63 29 20  talError(p->rc) 
19332 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 53 72  );.  assert( iSr
19333 63 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54  cPg!=PENDING_BYT
19334 45 5f 50 41 47 45 28 70 2d 3e 70 53 72 63 2d 3e  E_PAGE(p->pSrc->
19335 70 42 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74  pBt) );.  assert
19336 28 20 7a 53 72 63 44 61 74 61 20 29 3b 0a 0a 20  ( zSrcData );.. 
19337 20 2f 2a 20 43 61 74 63 68 20 74 68 65 20 63 61   /* Catch the ca
19338 73 65 20 77 68 65 72 65 20 74 68 65 20 64 65 73  se where the des
19339 74 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69  tination is an i
1933a 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
1933b 65 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 70  e and the.  ** p
1933c 61 67 65 20 73 69 7a 65 73 20 6f 66 20 74 68 65  age sizes of the
1933d 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73 74   source and dest
1933e 69 6e 61 74 69 6f 6e 20 64 69 66 66 65 72 2e 20  ination differ. 
1933f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53 72 63  .  */.  if( nSrc
19340 50 67 73 7a 21 3d 6e 44 65 73 74 50 67 73 7a 20  Pgsz!=nDestPgsz 
19341 26 26 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  && sqlite3PagerI
19342 73 4d 65 6d 64 62 28 73 71 6c 69 74 65 33 42 74  sMemdb(sqlite3Bt
19343 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73  reePager(p->pDes
19344 74 29 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  t)) ){.    rc = 
19345 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
19346 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20  .  }..  /* This 
19347 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66  loop runs once f
19348 6f 72 20 65 61 63 68 20 64 65 73 74 69 6e 61 74  or each destinat
19349 69 6f 6e 20 70 61 67 65 20 73 70 61 6e 6e 65 64  ion page spanned
1934a 20 62 79 20 74 68 65 20 73 6f 75 72 63 65 20 0a   by the source .
1934b 20 20 2a 2a 20 70 61 67 65 2e 20 46 6f 72 20 65    ** page. For e
1934c 61 63 68 20 69 74 65 72 61 74 69 6f 6e 2c 20 76  ach iteration, v
1934d 61 72 69 61 62 6c 65 20 69 4f 66 66 20 69 73 20  ariable iOff is 
1934e 73 65 74 20 74 6f 20 74 68 65 20 62 79 74 65 20  set to the byte 
1934f 6f 66 66 73 65 74 0a 20 20 2a 2a 20 6f 66 20 74  offset.  ** of t
19350 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70  he destination p
19351 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  age..  */.  for(
19352 69 4f 66 66 3d 69 45 6e 64 2d 28 69 36 34 29 6e  iOff=iEnd-(i64)n
19353 53 72 63 50 67 73 7a 3b 20 72 63 3d 3d 53 51 4c  SrcPgsz; rc==SQL
19354 49 54 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c 69  ITE_OK && iOff<i
19355 45 6e 64 3b 20 69 4f 66 66 2b 3d 6e 44 65 73 74  End; iOff+=nDest
19356 50 67 73 7a 29 7b 0a 20 20 20 20 44 62 50 61 67  Pgsz){.    DbPag
19357 65 20 2a 70 44 65 73 74 50 67 20 3d 20 30 3b 0a  e *pDestPg = 0;.
19358 20 20 20 20 50 67 6e 6f 20 69 44 65 73 74 20 3d      Pgno iDest =
19359 20 28 50 67 6e 6f 29 28 69 4f 66 66 2f 6e 44 65   (Pgno)(iOff/nDe
1935a 73 74 50 67 73 7a 29 2b 31 3b 0a 20 20 20 20 69  stPgsz)+1;.    i
1935b 66 28 20 69 44 65 73 74 3d 3d 50 45 4e 44 49 4e  f( iDest==PENDIN
1935c 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70  G_BYTE_PAGE(p->p
1935d 44 65 73 74 2d 3e 70 42 74 29 20 29 20 63 6f 6e  Dest->pBt) ) con
1935e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 53  tinue;.    if( S
1935f 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
19360 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
19361 70 44 65 73 74 50 61 67 65 72 2c 20 69 44 65 73  pDestPager, iDes
19362 74 2c 20 26 70 44 65 73 74 50 67 29 29 0a 20 20  t, &pDestPg)).  
19363 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
19364 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61  =(rc = sqlite3Pa
19365 67 65 72 57 72 69 74 65 28 70 44 65 73 74 50 67  gerWrite(pDestPg
19366 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  )).    ){.      
19367 63 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 20 3d 20  const u8 *zIn = 
19368 26 7a 53 72 63 44 61 74 61 5b 69 4f 66 66 25 6e  &zSrcData[iOff%n
19369 53 72 63 50 67 73 7a 5d 3b 0a 20 20 20 20 20 20  SrcPgsz];.      
1936a 75 38 20 2a 7a 44 65 73 74 44 61 74 61 20 3d 20  u8 *zDestData = 
1936b 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44  sqlite3PagerGetD
1936c 61 74 61 28 70 44 65 73 74 50 67 29 3b 0a 20 20  ata(pDestPg);.  
1936d 20 20 20 20 75 38 20 2a 7a 4f 75 74 20 3d 20 26      u8 *zOut = &
1936e 7a 44 65 73 74 44 61 74 61 5b 69 4f 66 66 25 6e  zDestData[iOff%n
1936f 44 65 73 74 50 67 73 7a 5d 3b 0a 0a 20 20 20 20  DestPgsz];..    
19370 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 64 61    /* Copy the da
19371 74 61 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72  ta from the sour
19372 63 65 20 70 61 67 65 20 69 6e 74 6f 20 74 68 65  ce page into the
19373 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67   destination pag
19374 65 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e  e..      ** Then
19375 20 63 6c 65 61 72 20 74 68 65 20 42 74 72 65 65   clear the Btree
19376 20 6c 61 79 65 72 20 4d 65 6d 50 61 67 65 2e 69   layer MemPage.i
19377 73 49 6e 69 74 20 66 6c 61 67 2e 20 42 6f 74 68  sInit flag. Both
19378 20 74 68 69 73 20 6d 6f 64 75 6c 65 0a 20 20 20   this module.   
19379 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 70 61     ** and the pa
1937a 67 65 72 20 63 6f 64 65 20 75 73 65 20 74 68 69  ger code use thi
1937b 73 20 74 72 69 63 6b 20 28 63 6c 65 61 72 69 6e  s trick (clearin
1937c 67 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  g the first byte
1937d 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  .      ** of the
1937e 20 70 61 67 65 20 27 65 78 74 72 61 27 20 73 70   page 'extra' sp
1937f 61 63 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74  ace to invalidat
19380 65 20 74 68 65 20 42 74 72 65 65 20 6c 61 79 65  e the Btree laye
19381 72 73 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68  rs.      ** cach
19382 65 64 20 70 61 72 73 65 20 6f 66 20 74 68 65 20  ed parse of the 
19383 70 61 67 65 29 2e 20 4d 65 6d 50 61 67 65 2e 69  page). MemPage.i
19384 73 49 6e 69 74 20 69 73 20 6d 61 72 6b 65 64 20  sInit is marked 
19385 0a 20 20 20 20 20 20 2a 2a 20 22 4d 55 53 54 20  .      ** "MUST 
19386 42 45 20 46 49 52 53 54 22 20 66 6f 72 20 74 68  BE FIRST" for th
19387 69 73 20 70 75 72 70 6f 73 65 2e 0a 20 20 20 20  is purpose..    
19388 20 20 2a 2f 0a 20 20 20 20 20 20 6d 65 6d 63 70    */.      memcp
19389 79 28 7a 4f 75 74 2c 20 7a 49 6e 2c 20 6e 43 6f  y(zOut, zIn, nCo
1938a 70 79 29 3b 0a 20 20 20 20 20 20 28 28 75 38 20  py);.      ((u8 
1938b 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  *)sqlite3PagerGe
1938c 74 45 78 74 72 61 28 70 44 65 73 74 50 67 29 29  tExtra(pDestPg))
1938d 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  [0] = 0;.    }. 
1938e 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
1938f 6e 72 65 66 28 70 44 65 73 74 50 67 29 3b 0a 20  nref(pDestPg);. 
19390 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
19391 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 46 69  .}../*.** If pFi
19392 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  le is currently 
19393 6c 61 72 67 65 72 20 74 68 61 6e 20 69 53 69 7a  larger than iSiz
19394 65 20 62 79 74 65 73 2c 20 74 68 65 6e 20 74 72  e bytes, then tr
19395 75 6e 63 61 74 65 20 69 74 20 74 6f 0a 2a 2a 20  uncate it to.** 
19396 65 78 61 63 74 6c 79 20 69 53 69 7a 65 20 62 79  exactly iSize by
19397 74 65 73 2e 20 49 66 20 70 46 69 6c 65 20 69 73  tes. If pFile is
19398 20 6e 6f 74 20 6c 61 72 67 65 72 20 74 68 61 6e   not larger than
19399 20 69 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68   iSize bytes, th
1939a 65 6e 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74  en.** this funct
1939b 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ion is a no-op..
1939c 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
1939d 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74  ITE_OK if everyt
1939e 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66  hing is successf
1939f 75 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65  ul, or an SQLite
193a0 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65 20   error .** code 
193a1 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  if an error occu
193a2 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  rs..*/.static in
193a3 74 20 62 61 63 6b 75 70 54 72 75 6e 63 61 74 65  t backupTruncate
193a4 46 69 6c 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  File(sqlite3_fil
193a5 65 20 2a 70 46 69 6c 65 2c 20 69 36 34 20 69 53  e *pFile, i64 iS
193a6 69 7a 65 29 7b 0a 20 20 69 36 34 20 69 43 75 72  ize){.  i64 iCur
193a7 72 65 6e 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d  rent;.  int rc =
193a8 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69   sqlite3OsFileSi
193a9 7a 65 28 70 46 69 6c 65 2c 20 26 69 43 75 72 72  ze(pFile, &iCurr
193aa 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ent);.  if( rc==
193ab 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 43 75  SQLITE_OK && iCu
193ac 72 72 65 6e 74 3e 69 53 69 7a 65 20 29 7b 0a 20  rrent>iSize ){. 
193ad 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
193ae 73 54 72 75 6e 63 61 74 65 28 70 46 69 6c 65 2c  sTruncate(pFile,
193af 20 69 53 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 72   iSize);.  }.  r
193b0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
193b1 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 69 73  ** Register this
193b2 20 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 77   backup object w
193b3 69 74 68 20 74 68 65 20 61 73 73 6f 63 69 61 74  ith the associat
193b4 65 64 20 73 6f 75 72 63 65 20 70 61 67 65 72 20  ed source pager 
193b5 66 6f 72 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 73  for.** callbacks
193b6 20 77 68 65 6e 20 70 61 67 65 73 20 61 72 65 20   when pages are 
193b7 63 68 61 6e 67 65 64 20 6f 72 20 74 68 65 20 63  changed or the c
193b8 61 63 68 65 20 69 6e 76 61 6c 69 64 61 74 65 64  ache invalidated
193b9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
193ba 20 61 74 74 61 63 68 42 61 63 6b 75 70 4f 62 6a   attachBackupObj
193bb 65 63 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  ect(sqlite3_back
193bc 75 70 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  up *p){.  sqlite
193bd 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70 3b 0a 20  3_backup **pp;. 
193be 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
193bf 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
193c0 70 2d 3e 70 53 72 63 29 20 29 3b 0a 20 20 70 70  p->pSrc) );.  pp
193c1 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 42   = sqlite3PagerB
193c2 61 63 6b 75 70 50 74 72 28 73 71 6c 69 74 65 33  ackupPtr(sqlite3
193c3 42 74 72 65 65 50 61 67 65 72 28 70 2d 3e 70 53  BtreePager(p->pS
193c4 72 63 29 29 3b 0a 20 20 70 2d 3e 70 4e 65 78 74  rc));.  p->pNext
193c5 20 3d 20 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20   = *pp;.  *pp = 
193c6 70 3b 0a 20 20 70 2d 3e 69 73 41 74 74 61 63 68  p;.  p->isAttach
193c7 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ed = 1;.}../*.**
193c8 20 43 6f 70 79 20 6e 50 61 67 65 20 70 61 67 65   Copy nPage page
193c9 73 20 66 72 6f 6d 20 74 68 65 20 73 6f 75 72 63  s from the sourc
193ca 65 20 62 2d 74 72 65 65 20 74 6f 20 74 68 65 20  e b-tree to the 
193cb 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a  destination..*/.
193cc 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
193cd 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
193ce 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ep(sqlite3_backu
193cf 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 29  p *p, int nPage)
193d0 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 73  {.  int rc;..  s
193d1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
193d2 65 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75  er(p->pSrcDb->mu
193d3 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  tex);.  sqlite3B
193d4 74 72 65 65 45 6e 74 65 72 28 70 2d 3e 70 53 72  treeEnter(p->pSr
193d5 63 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65  c);.  if( p->pDe
193d6 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69  stDb ){.    sqli
193d7 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
193d8 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65  p->pDestDb->mute
193d9 78 29 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  x);.  }..  rc = 
193da 70 2d 3e 72 63 3b 0a 20 20 69 66 28 20 21 69 73  p->rc;.  if( !is
193db 46 61 74 61 6c 45 72 72 6f 72 28 72 63 29 20 29  FatalError(rc) )
193dc 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 20 63 6f  {.    Pager * co
193dd 6e 73 74 20 70 53 72 63 50 61 67 65 72 20 3d 20  nst pSrcPager = 
193de 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
193df 72 28 70 2d 3e 70 53 72 63 29 3b 20 20 20 20 20  r(p->pSrc);     
193e0 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 72 20  /* Source pager 
193e1 2a 2f 0a 20 20 20 20 50 61 67 65 72 20 2a 20 63  */.    Pager * c
193e2 6f 6e 73 74 20 70 44 65 73 74 50 61 67 65 72 20  onst pDestPager 
193e3 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
193e4 67 65 72 28 70 2d 3e 70 44 65 73 74 29 3b 20 20  ger(p->pDest);  
193e5 20 2f 2a 20 44 65 73 74 20 70 61 67 65 72 20 2a   /* Dest pager *
193e6 2f 0a 20 20 20 20 69 6e 74 20 69 69 3b 20 20 20  /.    int ii;   
193e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
193e8 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
193e9 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
193ea 0a 20 20 20 20 69 6e 74 20 6e 53 72 63 50 61 67  .    int nSrcPag
193eb 65 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20  e = -1;         
193ec 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
193ed 6f 66 20 73 6f 75 72 63 65 20 64 62 20 69 6e 20  of source db in 
193ee 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 69 6e 74  pages */.    int
193ef 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20 30   bCloseTrans = 0
193f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
193f1 2f 2a 20 54 72 75 65 20 69 66 20 73 72 63 20 64  /* True if src d
193f2 62 20 72 65 71 75 69 72 65 73 20 75 6e 6c 6f 63  b requires unloc
193f3 6b 69 6e 67 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  king */..    /* 
193f4 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 70 61  If the source pa
193f5 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ger is currently
193f6 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e   in a write-tran
193f7 73 61 63 74 69 6f 6e 2c 20 72 65 74 75 72 6e 0a  saction, return.
193f8 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 42 55      ** SQLITE_BU
193f9 53 59 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a  SY immediately..
193fa 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
193fb 2d 3e 70 44 65 73 74 44 62 20 26 26 20 70 2d 3e  ->pDestDb && p->
193fc 70 53 72 63 2d 3e 70 42 74 2d 3e 69 6e 54 72 61  pSrc->pBt->inTra
193fd 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
193fe 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72  WRITE ){.      r
193ff 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
19400 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
19401 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
19402 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
19403 4c 6f 63 6b 20 74 68 65 20 64 65 73 74 69 6e 61  Lock the destina
19404 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2c 20 69  tion database, i
19405 66 20 69 74 20 69 73 20 6e 6f 74 20 6c 6f 63 6b  f it is not lock
19406 65 64 20 61 6c 72 65 61 64 79 2e 20 2a 2f 0a 20  ed already. */. 
19407 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
19408 3d 3d 72 63 20 26 26 20 70 2d 3e 62 44 65 73 74  ==rc && p->bDest
19409 4c 6f 63 6b 65 64 3d 3d 30 0a 20 20 20 20 20 26  Locked==0.     &
1940a 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  & SQLITE_OK==(rc
1940b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42   = sqlite3BtreeB
1940c 65 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70 44 65  eginTrans(p->pDe
1940d 73 74 2c 20 32 29 29 20 0a 20 20 20 20 29 7b 0a  st, 2)) .    ){.
1940e 20 20 20 20 20 20 70 2d 3e 62 44 65 73 74 4c 6f        p->bDestLo
1940f 63 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  cked = 1;.      
19410 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
19411 65 74 61 28 70 2d 3e 70 44 65 73 74 2c 20 42 54  eta(p->pDest, BT
19412 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49  REE_SCHEMA_VERSI
19413 4f 4e 2c 20 26 70 2d 3e 69 44 65 73 74 53 63 68  ON, &p->iDestSch
19414 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ema);.    }..   
19415 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
19416 6e 6f 20 6f 70 65 6e 20 72 65 61 64 2d 74 72 61  no open read-tra
19417 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20  nsaction on the 
19418 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2c  source database,
19419 20 6f 70 65 6e 0a 20 20 20 20 2a 2a 20 6f 6e 65   open.    ** one
1941a 20 6e 6f 77 2e 20 49 66 20 61 20 74 72 61 6e 73   now. If a trans
1941b 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64  action is opened
1941c 20 68 65 72 65 2c 20 74 68 65 6e 20 69 74 20 77   here, then it w
1941d 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64 0a 20 20  ill be closed.  
1941e 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 69 73    ** before this
1941f 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 74 73 2e   function exits.
19420 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
19421 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
19422 20 30 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65   0==sqlite3Btree
19423 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70 2d  IsInReadTrans(p-
19424 3e 70 53 72 63 29 20 29 7b 0a 20 20 20 20 20 20  >pSrc) ){.      
19425 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
19426 65 42 65 67 69 6e 54 72 61 6e 73 28 70 2d 3e 70  eBeginTrans(p->p
19427 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 62  Src, 0);.      b
19428 43 6c 6f 73 65 54 72 61 6e 73 20 3d 20 31 3b 0a  CloseTrans = 1;.
19429 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
1942a 4e 6f 77 20 74 68 61 74 20 74 68 65 72 65 20 69  Now that there i
1942b 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e  s a read-lock on
1942c 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
1942d 62 61 73 65 2c 20 71 75 65 72 79 20 74 68 65 0a  base, query the.
1942e 20 20 20 20 2a 2a 20 73 6f 75 72 63 65 20 70 61      ** source pa
1942f 67 65 72 20 66 6f 72 20 74 68 65 20 6e 75 6d 62  ger for the numb
19430 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
19431 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20  he database..   
19432 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
19433 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
19434 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
19435 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 53  agerPagecount(pS
19436 72 63 50 61 67 65 72 2c 20 26 6e 53 72 63 50 61  rcPager, &nSrcPa
19437 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  ge);.    }.    f
19438 6f 72 28 69 69 3d 30 3b 20 28 6e 50 61 67 65 3c  or(ii=0; (nPage<
19439 30 20 7c 7c 20 69 69 3c 6e 50 61 67 65 29 20 26  0 || ii<nPage) &
1943a 26 20 70 2d 3e 69 4e 65 78 74 3c 3d 28 50 67 6e  & p->iNext<=(Pgn
1943b 6f 29 6e 53 72 63 50 61 67 65 20 26 26 20 21 72  o)nSrcPage && !r
1943c 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20  c; ii++){.      
1943d 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53 72 63 50  const Pgno iSrcP
1943e 67 20 3d 20 70 2d 3e 69 4e 65 78 74 3b 20 20 20  g = p->iNext;   
1943f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19440 20 53 6f 75 72 63 65 20 70 61 67 65 20 6e 75 6d   Source page num
19441 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ber */.      if(
19442 20 69 53 72 63 50 67 21 3d 50 45 4e 44 49 4e 47   iSrcPg!=PENDING
19443 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 53  _BYTE_PAGE(p->pS
19444 72 63 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20  rc->pBt) ){.    
19445 20 20 20 20 44 62 50 61 67 65 20 2a 70 53 72 63      DbPage *pSrc
19446 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Pg;             
19447 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19448 2f 2a 20 53 6f 75 72 63 65 20 70 61 67 65 20 6f  /* Source page o
19449 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 20 20 20  bject */.       
1944a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
1944b 65 72 47 65 74 28 70 53 72 63 50 61 67 65 72 2c  erGet(pSrcPager,
1944c 20 69 53 72 63 50 67 2c 20 26 70 53 72 63 50 67   iSrcPg, &pSrcPg
1944d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1944e 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1944f 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 62            rc = b
19450 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 70 2c 20  ackupOnePage(p, 
19451 69 53 72 63 50 67 2c 20 73 71 6c 69 74 65 33 50  iSrcPg, sqlite3P
19452 61 67 65 72 47 65 74 44 61 74 61 28 70 53 72 63  agerGetData(pSrc
19453 50 67 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Pg));.          
19454 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
19455 66 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20 20  f(pSrcPg);.     
19456 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
19457 20 20 20 70 2d 3e 69 4e 65 78 74 2b 2b 3b 0a 20     p->iNext++;. 
19458 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
19459 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1945a 20 20 20 20 70 2d 3e 6e 50 61 67 65 63 6f 75 6e      p->nPagecoun
1945b 74 20 3d 20 6e 53 72 63 50 61 67 65 3b 0a 20 20  t = nSrcPage;.  
1945c 20 20 20 20 70 2d 3e 6e 52 65 6d 61 69 6e 69 6e      p->nRemainin
1945d 67 20 3d 20 6e 53 72 63 50 61 67 65 2b 31 2d 70  g = nSrcPage+1-p
1945e 2d 3e 69 4e 65 78 74 3b 0a 20 20 20 20 20 20 69  ->iNext;.      i
1945f 66 28 20 70 2d 3e 69 4e 65 78 74 3e 28 50 67 6e  f( p->iNext>(Pgn
19460 6f 29 6e 53 72 63 50 61 67 65 20 29 7b 0a 20 20  o)nSrcPage ){.  
19461 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
19462 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 65  E_DONE;.      }e
19463 6c 73 65 20 69 66 28 20 21 70 2d 3e 69 73 41 74  lse if( !p->isAt
19464 74 61 63 68 65 64 20 29 7b 0a 20 20 20 20 20 20  tached ){.      
19465 20 20 61 74 74 61 63 68 42 61 63 6b 75 70 4f 62    attachBackupOb
19466 6a 65 63 74 28 70 29 3b 0a 20 20 20 20 20 20 7d  ject(p);.      }
19467 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
19468 20 55 70 64 61 74 65 20 74 68 65 20 73 63 68 65   Update the sche
19469 6d 61 20 76 65 72 73 69 6f 6e 20 66 69 65 6c 64  ma version field
1946a 20 69 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74   in the destinat
1946b 69 6f 6e 20 64 61 74 61 62 61 73 65 2e 20 54 68  ion database. Th
1946c 69 73 0a 20 20 20 20 2a 2a 20 69 73 20 74 6f 20  is.    ** is to 
1946d 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
1946e 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f  he schema-versio
1946f 6e 20 72 65 61 6c 6c 79 20 64 6f 65 73 20 63 68  n really does ch
19470 61 6e 67 65 20 69 6e 0a 20 20 20 20 2a 2a 20 74  ange in.    ** t
19471 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68  he case where th
19472 65 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73  e source and des
19473 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
19474 65 73 20 68 61 76 65 20 74 68 65 0a 20 20 20 20  es have the.    
19475 2a 2a 20 73 61 6d 65 20 73 63 68 65 6d 61 20 76  ** same schema v
19476 65 72 73 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20  ersion..    */. 
19477 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
19478 45 5f 44 4f 4e 45 20 0a 20 20 20 20 20 26 26 20  E_DONE .     && 
19479 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72  (rc = sqlite3Btr
1947a 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 2d 3e  eeUpdateMeta(p->
1947b 70 44 65 73 74 2c 31 2c 70 2d 3e 69 44 65 73 74  pDest,1,p->iDest
1947c 53 63 68 65 6d 61 2b 31 29 29 3d 3d 53 51 4c 49  Schema+1))==SQLI
1947d 54 45 5f 4f 4b 0a 20 20 20 20 29 7b 0a 20 20 20  TE_OK.    ){.   
1947e 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 53 72     const int nSr
1947f 63 50 61 67 65 73 69 7a 65 20 3d 20 73 71 6c 69  cPagesize = sqli
19480 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 53  te3BtreeGetPageS
19481 69 7a 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20  ize(p->pSrc);.  
19482 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44      const int nD
19483 65 73 74 50 61 67 65 73 69 7a 65 20 3d 20 73 71  estPagesize = sq
19484 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
19485 65 53 69 7a 65 28 70 2d 3e 70 44 65 73 74 29 3b  eSize(p->pDest);
19486 0a 20 20 20 20 20 20 69 6e 74 20 6e 44 65 73 74  .      int nDest
19487 54 72 75 6e 63 61 74 65 3b 0a 20 20 0a 20 20 20  Truncate;.  .   
19488 20 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74 44     if( p->pDestD
19489 62 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  b ){.        sql
1948a 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
1948b 6c 53 63 68 65 6d 61 28 70 2d 3e 70 44 65 73 74  lSchema(p->pDest
1948c 44 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  Db, 0);.      }.
1948d 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 6e 44  .      /* Set nD
1948e 65 73 74 54 72 75 6e 63 61 74 65 20 74 6f 20 74  estTruncate to t
1948f 68 65 20 66 69 6e 61 6c 20 6e 75 6d 62 65 72 20  he final number 
19490 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
19491 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20  destination.    
19492 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54    ** database. T
19493 68 65 20 63 6f 6d 70 6c 69 63 61 74 69 6f 6e 20  he complication 
19494 68 65 72 65 20 69 73 20 74 68 61 74 20 74 68 65  here is that the
19495 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70 61 67   destination pag
19496 65 0a 20 20 20 20 20 20 2a 2a 20 73 69 7a 65 20  e.      ** size 
19497 6d 61 79 20 62 65 20 64 69 66 66 65 72 65 6e 74  may be different
19498 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 70   to the source p
19499 61 67 65 20 73 69 7a 65 2e 20 0a 20 20 20 20 20  age size. .     
1949a 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66 20   **.      ** If 
1949b 74 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 20  the source page 
1949c 73 69 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20  size is smaller 
1949d 74 68 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61  than the destina
1949e 74 69 6f 6e 20 70 61 67 65 20 73 69 7a 65 2c 20  tion page size, 
1949f 0a 20 20 20 20 20 20 2a 2a 20 72 6f 75 6e 64 20  .      ** round 
194a0 75 70 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  up. In this case
194a1 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c   the call to sql
194a2 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 29  ite3OsTruncate()
194a3 20 62 65 6c 6f 77 20 77 69 6c 6c 0a 20 20 20 20   below will.    
194a4 20 20 2a 2a 20 66 69 78 20 74 68 65 20 73 69 7a    ** fix the siz
194a5 65 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 20 48  e of the file. H
194a6 6f 77 65 76 65 72 20 69 74 20 69 73 20 69 6d 70  owever it is imp
194a7 6f 72 74 61 6e 74 20 74 6f 20 63 61 6c 6c 0a 20  ortant to call. 
194a8 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 50       ** sqlite3P
194a9 61 67 65 72 54 72 75 6e 63 61 74 65 49 6d 61 67  agerTruncateImag
194aa 65 28 29 20 68 65 72 65 20 73 6f 20 74 68 61 74  e() here so that
194ab 20 61 6e 79 20 70 61 67 65 73 20 69 6e 20 74 68   any pages in th
194ac 65 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 74  e .      ** dest
194ad 69 6e 61 74 69 6f 6e 20 66 69 6c 65 20 74 68 61  ination file tha
194ae 74 20 6c 69 65 20 62 65 79 6f 6e 64 20 74 68 65  t lie beyond the
194af 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65 20 70   nDestTruncate p
194b0 61 67 65 20 6d 61 72 6b 20 61 72 65 0a 20 20 20  age mark are.   
194b1 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 6c 65 64     ** journalled
194b2 20 62 79 20 50 61 67 65 72 43 6f 6d 6d 69 74 50   by PagerCommitP
194b3 68 61 73 65 4f 6e 65 28 29 20 62 65 66 6f 72 65  haseOne() before
194b4 20 74 68 65 79 20 61 72 65 20 64 65 73 74 72 6f   they are destro
194b5 79 65 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20  yed.      ** by 
194b6 74 68 65 20 66 69 6c 65 20 74 72 75 6e 63 61 74  the file truncat
194b7 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ion..      */.  
194b8 20 20 20 20 69 66 28 20 6e 53 72 63 50 61 67 65      if( nSrcPage
194b9 73 69 7a 65 3c 6e 44 65 73 74 50 61 67 65 73 69  size<nDestPagesi
194ba 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  ze ){.        in
194bb 74 20 72 61 74 69 6f 20 3d 20 6e 44 65 73 74 50  t ratio = nDestP
194bc 61 67 65 73 69 7a 65 2f 6e 53 72 63 50 61 67 65  agesize/nSrcPage
194bd 73 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 6e 44  size;.        nD
194be 65 73 74 54 72 75 6e 63 61 74 65 20 3d 20 28 6e  estTruncate = (n
194bf 53 72 63 50 61 67 65 2b 72 61 74 69 6f 2d 31 29  SrcPage+ratio-1)
194c0 2f 72 61 74 69 6f 3b 0a 20 20 20 20 20 20 20 20  /ratio;.        
194c1 69 66 28 20 6e 44 65 73 74 54 72 75 6e 63 61 74  if( nDestTruncat
194c2 65 3d 3d 28 69 6e 74 29 50 45 4e 44 49 4e 47 5f  e==(int)PENDING_
194c3 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65  BYTE_PAGE(p->pDe
194c4 73 74 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20  st->pBt) ){.    
194c5 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63        nDestTrunc
194c6 61 74 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d  ate--;.        }
194c7 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
194c8 20 20 20 20 20 20 6e 44 65 73 74 54 72 75 6e 63        nDestTrunc
194c9 61 74 65 20 3d 20 6e 53 72 63 50 61 67 65 20 2a  ate = nSrcPage *
194ca 20 28 6e 53 72 63 50 61 67 65 73 69 7a 65 2f 6e   (nSrcPagesize/n
194cb 44 65 73 74 50 61 67 65 73 69 7a 65 29 3b 0a 20  DestPagesize);. 
194cc 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
194cd 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
194ce 65 49 6d 61 67 65 28 70 44 65 73 74 50 61 67 65  eImage(pDestPage
194cf 72 2c 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65  r, nDestTruncate
194d0 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 6e 53  );..      if( nS
194d1 72 63 50 61 67 65 73 69 7a 65 3c 6e 44 65 73 74  rcPagesize<nDest
194d2 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20  Pagesize ){.    
194d3 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 6f      /* If the so
194d4 75 72 63 65 20 70 61 67 65 2d 73 69 7a 65 20 69  urce page-size i
194d5 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74  s smaller than t
194d6 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70  he destination p
194d7 61 67 65 2d 73 69 7a 65 2c 0a 20 20 20 20 20 20  age-size,.      
194d8 20 20 2a 2a 20 74 77 6f 20 65 78 74 72 61 20 74    ** two extra t
194d9 68 69 6e 67 73 20 6d 61 79 20 6e 65 65 64 20 74  hings may need t
194da 6f 20 68 61 70 70 65 6e 3a 0a 20 20 20 20 20 20  o happen:.      
194db 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
194dc 20 20 2a 20 54 68 65 20 64 65 73 74 69 6e 61 74    * The destinat
194dd 69 6f 6e 20 6d 61 79 20 6e 65 65 64 20 74 6f 20  ion may need to 
194de 62 65 20 74 72 75 6e 63 61 74 65 64 2c 20 61 6e  be truncated, an
194df 64 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  d.        **.   
194e0 20 20 20 20 20 2a 2a 20 20 20 2a 20 44 61 74 61       **   * Data
194e1 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 70   stored on the p
194e2 61 67 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  ages immediately
194e3 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 0a   following the .
194e4 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 70          **     p
194e5 65 6e 64 69 6e 67 2d 62 79 74 65 20 70 61 67 65  ending-byte page
194e6 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
194e7 61 74 61 62 61 73 65 20 6d 61 79 20 6e 65 65 64  atabase may need
194e8 20 74 6f 20 62 65 0a 20 20 20 20 20 20 20 20 2a   to be.        *
194e9 2a 20 20 20 20 20 63 6f 70 69 65 64 20 69 6e 74  *     copied int
194ea 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  o the destinatio
194eb 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20  n database..    
194ec 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63      */.        c
194ed 6f 6e 73 74 20 69 36 34 20 69 53 69 7a 65 20 3d  onst i64 iSize =
194ee 20 28 69 36 34 29 6e 53 72 63 50 61 67 65 73 69   (i64)nSrcPagesi
194ef 7a 65 20 2a 20 28 69 36 34 29 6e 53 72 63 50 61  ze * (i64)nSrcPa
194f0 67 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ge;.        sqli
194f1 74 65 33 5f 66 69 6c 65 20 2a 20 63 6f 6e 73 74  te3_file * const
194f2 20 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33   pFile = sqlite3
194f3 50 61 67 65 72 46 69 6c 65 28 70 44 65 73 74 50  PagerFile(pDestP
194f4 61 67 65 72 29 3b 0a 0a 20 20 20 20 20 20 20 20  ager);..        
194f5 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
194f6 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
194f7 20 28 69 36 34 29 6e 44 65 73 74 54 72 75 6e 63   (i64)nDestTrunc
194f8 61 74 65 2a 28 69 36 34 29 6e 44 65 73 74 50 61  ate*(i64)nDestPa
194f9 67 65 73 69 7a 65 20 3e 3d 20 69 53 69 7a 65 20  gesize >= iSize 
194fa 7c 7c 20 28 0a 20 20 20 20 20 20 20 20 20 20 20  || (.           
194fb 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65     nDestTruncate
194fc 3d 3d 28 69 6e 74 29 28 50 45 4e 44 49 4e 47 5f  ==(int)(PENDING_
194fd 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70 44 65  BYTE_PAGE(p->pDe
194fe 73 74 2d 3e 70 42 74 29 2d 31 29 0a 20 20 20 20  st->pBt)-1).    
194ff 20 20 20 20 20 20 20 26 26 20 69 53 69 7a 65 3e         && iSize>
19500 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26  =PENDING_BYTE &&
19501 20 69 53 69 7a 65 3c 3d 50 45 4e 44 49 4e 47 5f   iSize<=PENDING_
19502 42 59 54 45 2b 6e 44 65 73 74 50 61 67 65 73 69  BYTE+nDestPagesi
19503 7a 65 0a 20 20 20 20 20 20 20 20 29 29 3b 0a 20  ze.        ));. 
19504 20 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54         if( SQLIT
19505 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
19506 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68  te3PagerCommitPh
19507 61 73 65 4f 6e 65 28 70 44 65 73 74 50 61 67 65  aseOne(pDestPage
19508 72 2c 20 30 2c 20 31 29 29 0a 20 20 20 20 20 20  r, 0, 1)).      
19509 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
1950a 3d 28 72 63 20 3d 20 62 61 63 6b 75 70 54 72 75  =(rc = backupTru
1950b 6e 63 61 74 65 46 69 6c 65 28 70 46 69 6c 65 2c  ncateFile(pFile,
1950c 20 69 53 69 7a 65 29 29 0a 20 20 20 20 20 20 20   iSize)).       
1950d 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
1950e 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67  (rc = sqlite3Pag
1950f 65 72 53 79 6e 63 28 70 44 65 73 74 50 61 67 65  erSync(pDestPage
19510 72 29 29 0a 20 20 20 20 20 20 20 20 29 7b 0a 20  r)).        ){. 
19511 20 20 20 20 20 20 20 20 20 69 36 34 20 69 4f 66           i64 iOf
19512 66 3b 0a 20 20 20 20 20 20 20 20 20 20 69 36 34  f;.          i64
19513 20 69 45 6e 64 20 3d 20 4d 49 4e 28 50 45 4e 44   iEnd = MIN(PEND
19514 49 4e 47 5f 42 59 54 45 20 2b 20 6e 44 65 73 74  ING_BYTE + nDest
19515 50 61 67 65 73 69 7a 65 2c 20 69 53 69 7a 65 29  Pagesize, iSize)
19516 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
19517 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 4f 66  .            iOf
19518 66 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 6e  f=PENDING_BYTE+n
19519 53 72 63 50 61 67 65 73 69 7a 65 3b 20 0a 20 20  SrcPagesize; .  
1951a 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51            rc==SQ
1951b 4c 49 54 45 5f 4f 4b 20 26 26 20 69 4f 66 66 3c  LITE_OK && iOff<
1951c 69 45 6e 64 3b 20 0a 20 20 20 20 20 20 20 20 20  iEnd; .         
1951d 20 20 20 69 4f 66 66 2b 3d 6e 53 72 63 50 61 67     iOff+=nSrcPag
1951e 65 73 69 7a 65 0a 20 20 20 20 20 20 20 20 20 20  esize.          
1951f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 50  ){.            P
19520 67 48 64 72 20 2a 70 53 72 63 50 67 20 3d 20 30  gHdr *pSrcPg = 0
19521 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 6f  ;.            co
19522 6e 73 74 20 50 67 6e 6f 20 69 53 72 63 50 67 20  nst Pgno iSrcPg 
19523 3d 20 28 50 67 6e 6f 29 28 28 69 4f 66 66 2f 6e  = (Pgno)((iOff/n
19524 53 72 63 50 61 67 65 73 69 7a 65 29 2b 31 29 3b  SrcPagesize)+1);
19525 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
19526 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
19527 74 28 70 53 72 63 50 61 67 65 72 2c 20 69 53 72  t(pSrcPager, iSr
19528 63 50 67 2c 20 26 70 53 72 63 50 67 29 3b 0a 20  cPg, &pSrcPg);. 
19529 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72             if( r
1952a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1952b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75 38                u8
1952c 20 2a 7a 44 61 74 61 20 3d 20 73 71 6c 69 74 65   *zData = sqlite
1952d 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70 53  3PagerGetData(pS
1952e 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20  rcPg);.         
1952f 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
19530 33 4f 73 57 72 69 74 65 28 70 46 69 6c 65 2c 20  3OsWrite(pFile, 
19531 7a 44 61 74 61 2c 20 6e 53 72 63 50 61 67 65 73  zData, nSrcPages
19532 69 7a 65 2c 20 69 4f 66 66 29 3b 0a 20 20 20 20  ize, iOff);.    
19533 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
19534 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
19535 65 72 55 6e 72 65 66 28 70 53 72 63 50 67 29 3b  erUnref(pSrcPg);
19536 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
19537 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
19538 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  se{.        rc =
19539 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
1953a 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 44 65 73  mitPhaseOne(pDes
1953b 74 50 61 67 65 72 2c 20 30 2c 20 30 29 3b 0a 20  tPager, 0, 0);. 
1953c 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20       }.  .      
1953d 2f 2a 20 46 69 6e 69 73 68 20 63 6f 6d 6d 69 74  /* Finish commit
1953e 74 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63  ting the transac
1953f 74 69 6f 6e 20 74 6f 20 74 68 65 20 64 65 73 74  tion to the dest
19540 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
19541 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 53  . */.      if( S
19542 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 0a 20 20 20  QLITE_OK==rc.   
19543 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b      && SQLITE_OK
19544 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42  ==(rc = sqlite3B
19545 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54  treeCommitPhaseT
19546 77 6f 28 70 2d 3e 70 44 65 73 74 29 29 0a 20 20  wo(p->pDest)).  
19547 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 72      ){.        r
19548 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
19549 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1954a 20 0a 20 20 20 20 2f 2a 20 49 66 20 62 43 6c 6f   .    /* If bClo
1954b 73 65 54 72 61 6e 73 20 69 73 20 74 72 75 65 2c  seTrans is true,
1954c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
1954d 69 6f 6e 20 6f 70 65 6e 65 64 20 61 20 72 65 61  ion opened a rea
1954e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  d transaction.  
1954f 20 20 2a 2a 20 6f 6e 20 74 68 65 20 73 6f 75 72    ** on the sour
19550 63 65 20 64 61 74 61 62 61 73 65 2e 20 43 6c 6f  ce database. Clo
19551 73 65 20 74 68 65 20 72 65 61 64 20 74 72 61 6e  se the read tran
19552 73 61 63 74 69 6f 6e 20 68 65 72 65 2e 20 54 68  saction here. Th
19553 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f  ere is.    ** no
19554 20 6e 65 65 64 20 74 6f 20 63 68 65 63 6b 20 74   need to check t
19555 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73  he return values
19556 20 6f 66 20 74 68 65 20 62 74 72 65 65 20 6d 65   of the btree me
19557 74 68 6f 64 73 20 68 65 72 65 2c 20 61 73 0a 20  thods here, as. 
19558 20 20 20 2a 2a 20 22 63 6f 6d 6d 69 74 74 69 6e     ** "committin
19559 67 22 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74  g" a read-only t
1955a 72 61 6e 73 61 63 74 69 6f 6e 20 63 61 6e 6e 6f  ransaction canno
1955b 74 20 66 61 69 6c 2e 0a 20 20 20 20 2a 2f 0a 20  t fail..    */. 
1955c 20 20 20 69 66 28 20 62 43 6c 6f 73 65 54 72 61     if( bCloseTra
1955d 6e 73 20 29 7b 0a 20 20 20 20 20 20 54 45 53 54  ns ){.      TEST
1955e 4f 4e 4c 59 28 20 69 6e 74 20 72 63 32 20 29 3b  ONLY( int rc2 );
1955f 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28  .      TESTONLY(
19560 20 72 63 32 20 20 3d 20 29 20 73 71 6c 69 74 65   rc2  = ) sqlite
19561 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
19562 65 4f 6e 65 28 70 2d 3e 70 53 72 63 2c 20 30 29  eOne(p->pSrc, 0)
19563 3b 0a 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59  ;.      TESTONLY
19564 28 20 72 63 32 20 7c 3d 20 29 20 73 71 6c 69 74  ( rc2 |= ) sqlit
19565 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
19566 73 65 54 77 6f 28 70 2d 3e 70 53 72 63 29 3b 0a  seTwo(p->pSrc);.
19567 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
19568 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  2==SQLITE_OK );.
19569 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 70 2d 3e      }.  .    p->
1956a 72 63 20 3d 20 72 63 3b 0a 20 20 7d 0a 20 20 69  rc = rc;.  }.  i
1956b 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b  f( p->pDestDb ){
1956c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1956d 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 44 65 73  ex_leave(p->pDes
1956e 74 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  tDb->mutex);.  }
1956f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
19570 65 61 76 65 28 70 2d 3e 70 53 72 63 29 3b 0a 20  eave(p->pSrc);. 
19571 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
19572 65 61 76 65 28 70 2d 3e 70 53 72 63 44 62 2d 3e  eave(p->pSrcDb->
19573 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
19574 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
19575 6c 65 61 73 65 20 61 6c 6c 20 72 65 73 6f 75 72  lease all resour
19576 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
19577 69 74 68 20 61 6e 20 73 71 6c 69 74 65 33 5f 62  ith an sqlite3_b
19578 61 63 6b 75 70 2a 20 68 61 6e 64 6c 65 2e 0a 2a  ackup* handle..*
19579 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1957a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
1957b 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f 62  finish(sqlite3_b
1957c 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 73 71 6c  ackup *p){.  sql
1957d 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 70 70  ite3_backup **pp
1957e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1957f 20 20 2f 2a 20 50 74 72 20 74 6f 20 68 65 61 64    /* Ptr to head
19580 20 6f 66 20 70 61 67 65 72 73 20 62 61 63 6b 75   of pagers backu
19581 70 20 6c 69 73 74 20 2a 2f 0a 20 20 73 71 6c 69  p list */.  sqli
19582 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
19583 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
19584 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 70 72 6f   /* Mutex to pro
19585 74 65 63 74 20 73 6f 75 72 63 65 20 64 61 74 61  tect source data
19586 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63  base */.  int rc
19587 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
19588 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19589 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
1958a 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 45 6e 74 65 72  n */..  /* Enter
1958b 20 74 68 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a   the mutexes */.
1958c 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
1958d 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
1958e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1958f 6e 74 65 72 28 70 2d 3e 70 53 72 63 44 62 2d 3e  nter(p->pSrcDb->
19590 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
19591 33 42 74 72 65 65 45 6e 74 65 72 28 70 2d 3e 70  3BtreeEnter(p->p
19592 53 72 63 29 3b 0a 20 20 6d 75 74 65 78 20 3d 20  Src);.  mutex = 
19593 70 2d 3e 70 53 72 63 44 62 2d 3e 6d 75 74 65 78  p->pSrcDb->mutex
19594 3b 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74  ;.  if( p->pDest
19595 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Db ){.    sqlite
19596 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d  3_mutex_enter(p-
19597 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29  >pDestDb->mutex)
19598 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 61  ;.  }..  /* Deta
19599 63 68 20 74 68 69 73 20 62 61 63 6b 75 70 20 66  ch this backup f
1959a 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 70  rom the source p
1959b 61 67 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ager. */.  if( p
1959c 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20  ->pDestDb ){.   
1959d 20 70 2d 3e 70 53 72 63 2d 3e 6e 42 61 63 6b 75   p->pSrc->nBacku
1959e 70 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  p--;.  }.  if( p
1959f 2d 3e 69 73 41 74 74 61 63 68 65 64 20 29 7b 0a  ->isAttached ){.
195a0 20 20 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33      pp = sqlite3
195a1 50 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 73  PagerBackupPtr(s
195a2 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
195a3 28 70 2d 3e 70 53 72 63 29 29 3b 0a 20 20 20 20  (p->pSrc));.    
195a4 77 68 69 6c 65 28 20 2a 70 70 21 3d 70 20 29 7b  while( *pp!=p ){
195a5 0a 20 20 20 20 20 20 70 70 20 3d 20 26 28 2a 70  .      pp = &(*p
195a6 70 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  p)->pNext;.    }
195a7 0a 20 20 20 20 2a 70 70 20 3d 20 70 2d 3e 70 4e  .    *pp = p->pN
195a8 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ext;.  }..  /* I
195a9 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
195aa 69 73 20 73 74 69 6c 6c 20 6f 70 65 6e 20 6f 6e  is still open on
195ab 20 74 68 65 20 42 74 72 65 65 2c 20 72 6f 6c 6c   the Btree, roll
195ac 20 69 74 20 62 61 63 6b 2e 20 2a 2f 0a 20 20 73   it back. */.  s
195ad 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
195ae 61 63 6b 28 70 2d 3e 70 44 65 73 74 29 3b 0a 0a  ack(p->pDest);..
195af 20 20 2f 2a 20 53 65 74 20 74 68 65 20 65 72 72    /* Set the err
195b0 6f 72 20 63 6f 64 65 20 6f 66 20 74 68 65 20 64  or code of the d
195b1 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62  estination datab
195b2 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20  ase handle. */. 
195b3 20 72 63 20 3d 20 28 70 2d 3e 72 63 3d 3d 53 51   rc = (p->rc==SQ
195b4 4c 49 54 45 5f 44 4f 4e 45 29 20 3f 20 53 51 4c  LITE_DONE) ? SQL
195b5 49 54 45 5f 4f 4b 20 3a 20 70 2d 3e 72 63 3b 0a  ITE_OK : p->rc;.
195b6 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70    sqlite3Error(p
195b7 2d 3e 70 44 65 73 74 44 62 2c 20 72 63 2c 20 30  ->pDestDb, rc, 0
195b8 29 3b 0a 0a 20 20 2f 2a 20 45 78 69 74 20 74 68  );..  /* Exit th
195b9 65 20 6d 75 74 65 78 65 73 20 61 6e 64 20 66 72  e mutexes and fr
195ba 65 65 20 74 68 65 20 62 61 63 6b 75 70 20 63 6f  ee the backup co
195bb 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 2e  ntext structure.
195bc 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 44 65   */.  if( p->pDe
195bd 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69  stDb ){.    sqli
195be 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
195bf 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65  p->pDestDb->mute
195c0 78 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  x);.  }.  sqlite
195c1 33 42 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70  3BtreeLeave(p->p
195c2 53 72 63 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  Src);.  if( p->p
195c3 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73 71  DestDb ){.    sq
195c4 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20  lite3_free(p);. 
195c5 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
195c6 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b  ex_leave(mutex);
195c7 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
195c8 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
195c9 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
195ca 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61  s still to be ba
195cb 63 6b 65 64 20 75 70 20 61 73 20 6f 66 20 74 68  cked up as of th
195cc 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a  e most recent.**
195cd 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
195ce 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 0a  _backup_step()..
195cf 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
195d0 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  t sqlite3_backup
195d1 5f 72 65 6d 61 69 6e 69 6e 67 28 73 71 6c 69 74  _remaining(sqlit
195d2 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20  e3_backup *p){. 
195d3 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 6d 61   return p->nRema
195d4 69 6e 69 6e 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ining;.}../*.** 
195d5 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c  Return the total
195d6 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
195d7 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
195d8 61 74 61 62 61 73 65 20 61 73 20 6f 66 20 74 68  atabase as of th
195d9 65 20 6d 6f 73 74 20 0a 2a 2a 20 72 65 63 65 6e  e most .** recen
195da 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  t call to sqlite
195db 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e  3_backup_step().
195dc 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
195dd 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  nt sqlite3_backu
195de 70 5f 70 61 67 65 63 6f 75 6e 74 28 73 71 6c 69  p_pagecount(sqli
195df 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a  te3_backup *p){.
195e0 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 50 61 67    return p->nPag
195e1 65 63 6f 75 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ecount;.}../*.**
195e2 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
195e3 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 74  s called after t
195e4 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70  he contents of p
195e5 61 67 65 20 69 50 61 67 65 20 6f 66 20 74 68 65  age iPage of the
195e6 0a 2a 2a 20 73 6f 75 72 63 65 20 64 61 74 61 62  .** source datab
195e7 61 73 65 20 68 61 76 65 20 62 65 65 6e 20 6d 6f  ase have been mo
195e8 64 69 66 69 65 64 2e 20 49 66 20 70 61 67 65 20  dified. If page 
195e9 69 50 61 67 65 20 68 61 73 20 61 6c 72 65 61 64  iPage has alread
195ea 79 20 62 65 65 6e 20 0a 2a 2a 20 63 6f 70 69 65  y been .** copie
195eb 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69  d into the desti
195ec 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2c  nation database,
195ed 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20 77   then the data w
195ee 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a 2a 2a  ritten to the.**
195ef 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20   destination is 
195f0 6e 6f 77 20 69 6e 76 61 6c 69 64 61 74 65 64 2e  now invalidated.
195f1 20 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   The destination
195f2 20 63 6f 70 79 20 6f 66 20 69 50 61 67 65 20 6e   copy of iPage n
195f3 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 75 70  eeds.** to be up
195f4 64 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e  dated with the n
195f5 65 77 20 64 61 74 61 20 62 65 66 6f 72 65 20 74  ew data before t
195f6 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74  he backup operat
195f7 69 6f 6e 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65  ion is.** comple
195f8 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  te..**.** It is 
195f9 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65  assumed that the
195fa 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65   mutex associate
195fb 64 20 77 69 74 68 20 74 68 65 20 42 74 53 68 61  d with the BtSha
195fc 72 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f  red object.** co
195fd 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74  rresponding to t
195fe 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
195ff 73 65 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20  se is held when 
19600 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
19601 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53  .** called..*/.S
19602 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
19603 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70  id sqlite3Backup
19604 55 70 64 61 74 65 28 73 71 6c 69 74 65 33 5f 62  Update(sqlite3_b
19605 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70 2c 20  ackup *pBackup, 
19606 50 67 6e 6f 20 69 50 61 67 65 2c 20 63 6f 6e 73  Pgno iPage, cons
19607 74 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20 20  t u8 *aData){.  
19608 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
19609 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
1960a 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
1960b 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66   variable */.  f
1960c 6f 72 28 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b  or(p=pBackup; p;
1960d 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
1960e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1960f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
19610 70 53 72 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  pSrc->pBt->mutex
19611 29 20 29 3b 0a 20 20 20 20 69 66 28 20 21 69 73  ) );.    if( !is
19612 46 61 74 61 6c 45 72 72 6f 72 28 70 2d 3e 72 63  FatalError(p->rc
19613 29 20 26 26 20 69 50 61 67 65 3c 70 2d 3e 69 4e  ) && iPage<p->iN
19614 65 78 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ext ){.      /* 
19615 54 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63 65  The backup proce
19616 73 73 20 70 20 68 61 73 20 61 6c 72 65 61 64 79  ss p has already
19617 20 63 6f 70 69 65 64 20 70 61 67 65 20 69 50 61   copied page iPa
19618 67 65 2e 20 42 75 74 20 6e 6f 77 20 69 74 0a 20  ge. But now it. 
19619 20 20 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e       ** has been
1961a 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 20 74   modified by a t
1961b 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68  ransaction on th
1961c 65 20 73 6f 75 72 63 65 20 70 61 67 65 72 2e 20  e source pager. 
1961d 43 6f 70 79 0a 20 20 20 20 20 20 2a 2a 20 74 68  Copy.      ** th
1961e 65 20 6e 65 77 20 64 61 74 61 20 69 6e 74 6f 20  e new data into 
1961f 74 68 65 20 62 61 63 6b 75 70 2e 0a 20 20 20 20  the backup..    
19620 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72    */.      int r
19621 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67  c = backupOnePag
19622 65 28 70 2c 20 69 50 61 67 65 2c 20 61 44 61 74  e(p, iPage, aDat
19623 61 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  a);.      assert
19624 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53  ( rc!=SQLITE_BUS
19625 59 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  Y && rc!=SQLITE_
19626 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20 20  LOCKED );.      
19627 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
19628 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  K ){.        p->
19629 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 7d  rc = rc;.      }
1962a 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
1962b 0a 2a 2a 20 52 65 73 74 61 72 74 20 74 68 65 20  .** Restart the 
1962c 62 61 63 6b 75 70 20 70 72 6f 63 65 73 73 2e 20  backup process. 
1962d 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 77  This is called w
1962e 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 6c 61  hen the pager la
1962f 79 65 72 0a 2a 2a 20 64 65 74 65 63 74 73 20 74  yer.** detects t
19630 68 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65  hat the database
19631 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69   has been modifi
19632 65 64 20 62 79 20 61 6e 20 65 78 74 65 72 6e 61  ed by an externa
19633 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  l database.** co
19634 6e 6e 65 63 74 69 6f 6e 2e 20 49 6e 20 74 68 69  nnection. In thi
19635 73 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20  s case there is 
19636 6e 6f 20 77 61 79 20 6f 66 20 6b 6e 6f 77 69 6e  no way of knowin
19637 67 20 77 68 69 63 68 20 6f 66 20 74 68 65 0a 2a  g which of the.*
19638 2a 20 70 61 67 65 73 20 74 68 61 74 20 68 61 76  * pages that hav
19639 65 20 62 65 65 6e 20 63 6f 70 69 65 64 20 69 6e  e been copied in
1963a 74 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69  to the destinati
1963b 6f 6e 20 64 61 74 61 62 61 73 65 20 61 72 65 20  on database are 
1963c 73 74 69 6c 6c 20 0a 2a 2a 20 76 61 6c 69 64 20  still .** valid 
1963d 61 6e 64 20 77 68 69 63 68 20 61 72 65 20 6e 6f  and which are no
1963e 74 2c 20 73 6f 20 74 68 65 20 65 6e 74 69 72 65  t, so the entire
1963f 20 70 72 6f 63 65 73 73 20 6e 65 65 64 73 20 74   process needs t
19640 6f 20 62 65 20 72 65 73 74 61 72 74 65 64 2e 0a  o be restarted..
19641 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75  **.** It is assu
19642 6d 65 64 20 74 68 61 74 20 74 68 65 20 6d 75 74  med that the mut
19643 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ex associated wi
19644 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64 20  th the BtShared 
19645 6f 62 6a 65 63 74 0a 2a 2a 20 63 6f 72 72 65 73  object.** corres
19646 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73  ponding to the s
19647 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 69  ource database i
19648 73 20 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73  s held when this
19649 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20   function is.** 
1964a 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  called..*/.SQLIT
1964b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1964c 71 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74  qlite3BackupRest
1964d 61 72 74 28 73 71 6c 69 74 65 33 5f 62 61 63 6b  art(sqlite3_back
1964e 75 70 20 2a 70 42 61 63 6b 75 70 29 7b 0a 20 20  up *pBackup){.  
1964f 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
19650 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
19651 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
19652 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 66   variable */.  f
19653 6f 72 28 70 3d 70 42 61 63 6b 75 70 3b 20 70 3b  or(p=pBackup; p;
19654 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
19655 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
19656 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
19657 70 53 72 63 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  pSrc->pBt->mutex
19658 29 20 29 3b 0a 20 20 20 20 70 2d 3e 69 4e 65 78  ) );.    p->iNex
19659 74 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 69  t = 1;.  }.}..#i
1965a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1965b 54 5f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43  T_VACUUM./*.** C
1965c 6f 70 79 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  opy the complete
1965d 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70 42 74 46   content of pBtF
1965e 72 6f 6d 20 69 6e 74 6f 20 70 42 74 54 6f 2e 20  rom into pBtTo. 
1965f 20 41 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a   A transaction.*
19660 2a 20 6d 75 73 74 20 62 65 20 61 63 74 69 76 65  * must be active
19661 20 66 6f 72 20 62 6f 74 68 20 66 69 6c 65 73 2e   for both files.
19662 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20  .**.** The size 
19663 6f 66 20 66 69 6c 65 20 70 54 6f 20 6d 61 79 20  of file pTo may 
19664 62 65 20 72 65 64 75 63 65 64 20 62 79 20 74 68  be reduced by th
19665 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66  is operation. If
19666 20 61 6e 79 74 68 69 6e 67 20 0a 2a 2a 20 67 6f   anything .** go
19667 65 73 20 77 72 6f 6e 67 2c 20 74 68 65 20 74 72  es wrong, the tr
19668 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 70 54 6f  ansaction on pTo
19669 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   is rolled back.
1966a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
1966b 74 68 65 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74  the .** transact
1966c 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
1966d 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
1966e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
1966f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
19670 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74  BtreeCopyFile(Bt
19671 72 65 65 20 2a 70 54 6f 2c 20 42 74 72 65 65 20  ree *pTo, Btree 
19672 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72  *pFrom){.  int r
19673 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63  c;.  sqlite3_bac
19674 6b 75 70 20 62 3b 0a 20 20 73 71 6c 69 74 65 33  kup b;.  sqlite3
19675 42 74 72 65 65 45 6e 74 65 72 28 70 54 6f 29 3b  BtreeEnter(pTo);
19676 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
19677 6e 74 65 72 28 70 46 72 6f 6d 29 3b 0a 0a 20 20  nter(pFrom);..  
19678 2f 2a 20 53 65 74 20 75 70 20 61 6e 20 73 71 6c  /* Set up an sql
19679 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a 65  ite3_backup obje
1967a 63 74 2e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  ct. sqlite3_back
1967b 75 70 2e 70 44 65 73 74 44 62 20 6d 75 73 74 20  up.pDestDb must 
1967c 62 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f 20 30  be set.  ** to 0
1967d 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62  . This is used b
1967e 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  y the implementa
1967f 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33  tions of sqlite3
19680 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 0a 20  _backup_step(). 
19681 20 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 5f   ** and sqlite3_
19682 62 61 63 6b 75 70 5f 66 69 6e 69 73 68 28 29 20  backup_finish() 
19683 74 6f 20 64 65 74 65 63 74 20 74 68 61 74 20 74  to detect that t
19684 68 65 79 20 61 72 65 20 62 65 69 6e 67 20 63 61  hey are being ca
19685 6c 6c 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74  lled.  ** from t
19686 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 6e 6f  his function, no
19687 74 20 64 69 72 65 63 74 6c 79 20 62 79 20 74 68  t directly by th
19688 65 20 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 6d  e user..  */.  m
19689 65 6d 73 65 74 28 26 62 2c 20 30 2c 20 73 69 7a  emset(&b, 0, siz
1968a 65 6f 66 28 62 29 29 3b 0a 20 20 62 2e 70 53 72  eof(b));.  b.pSr
1968b 63 44 62 20 3d 20 70 46 72 6f 6d 2d 3e 64 62 3b  cDb = pFrom->db;
1968c 0a 20 20 62 2e 70 53 72 63 20 3d 20 70 46 72 6f  .  b.pSrc = pFro
1968d 6d 3b 0a 20 20 62 2e 70 44 65 73 74 20 3d 20 70  m;.  b.pDest = p
1968e 54 6f 3b 0a 20 20 62 2e 69 4e 65 78 74 20 3d 20  To;.  b.iNext = 
1968f 31 3b 0a 0a 20 20 2f 2a 20 30 78 37 46 46 46 46  1;..  /* 0x7FFFF
19690 46 46 46 20 69 73 20 74 68 65 20 68 61 72 64 20  FFF is the hard 
19691 6c 69 6d 69 74 20 66 6f 72 20 74 68 65 20 6e 75  limit for the nu
19692 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
19693 20 61 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a   a database.  **
19694 20 66 69 6c 65 2e 20 42 79 20 70 61 73 73 69 6e   file. By passin
19695 67 20 74 68 69 73 20 61 73 20 74 68 65 20 6e 75  g this as the nu
19696 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f  mber of pages to
19697 20 63 6f 70 79 20 74 6f 0a 20 20 2a 2a 20 73 71   copy to.  ** sq
19698 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
19699 70 28 29 2c 20 77 65 20 63 61 6e 20 67 75 61 72  p(), we can guar
1969a 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 63  antee that the c
1969b 6f 70 79 20 66 69 6e 69 73 68 65 73 20 0a 20 20  opy finishes .  
1969c 2a 2a 20 77 69 74 68 69 6e 20 61 20 73 69 6e 67  ** within a sing
1969d 6c 65 20 63 61 6c 6c 20 28 75 6e 6c 65 73 73 20  le call (unless 
1969e 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 29  an error occurs)
1969f 2e 20 54 68 65 20 61 73 73 65 72 74 28 29 20 73  . The assert() s
196a0 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 63 68  tatement.  ** ch
196a1 65 63 6b 73 20 74 68 69 73 20 61 73 73 75 6d 70  ecks this assump
196a2 74 69 6f 6e 20 2d 20 28 70 2d 3e 72 63 29 20 73  tion - (p->rc) s
196a3 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20  hould be set to 
196a4 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 44 4f  either SQLITE_DO
196a5 4e 45 20 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 65  NE .  ** or an e
196a6 72 72 6f 72 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a  rror code..  */.
196a7 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70    sqlite3_backup
196a8 5f 73 74 65 70 28 26 62 2c 20 30 78 37 46 46 46  _step(&b, 0x7FFF
196a9 46 46 46 46 29 3b 0a 20 20 61 73 73 65 72 74 28  FFFF);.  assert(
196aa 20 62 2e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b   b.rc!=SQLITE_OK
196ab 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
196ac 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
196ad 28 26 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  (&b);.  if( rc==
196ae 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
196af 20 70 54 6f 2d 3e 70 42 74 2d 3e 70 61 67 65 53   pTo->pBt->pageS
196b0 69 7a 65 46 69 78 65 64 20 3d 20 30 3b 0a 20 20  izeFixed = 0;.  
196b1 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  }..  sqlite3Btre
196b2 65 4c 65 61 76 65 28 70 46 72 6f 6d 29 3b 0a 20  eLeave(pFrom);. 
196b3 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
196b4 76 65 28 70 54 6f 29 3b 0a 20 20 72 65 74 75 72  ve(pTo);.  retur
196b5 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  n rc;.}.#endif /
196b6 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  * SQLITE_OMIT_VA
196b7 43 55 55 4d 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  CUUM */../******
196b8 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
196b9 62 61 63 6b 75 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a  backup.c *******
196ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196bc 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
196bd 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
196be 69 6c 65 20 76 64 62 65 6d 65 6d 2e 63 20 2a 2a  ile vdbemem.c **
196bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196c1 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
196c2 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a  004 May 26.**.**
196c3 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
196c4 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
196c5 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
196c6 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
196c7 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
196c8 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
196c9 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
196ca 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
196cb 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
196cc 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
196cd 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
196ce 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
196cf 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
196d0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
196d1 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
196d2 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
196d3 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
196d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
196d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
196d9 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
196da 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 6d  ns code use to m
196db 61 6e 69 70 75 6c 61 74 65 20 22 4d 65 6d 22 20  anipulate "Mem" 
196dc 73 74 72 75 63 74 75 72 65 2e 20 20 41 20 22 4d  structure.  A "M
196dd 65 6d 22 0a 2a 2a 20 73 74 6f 72 65 73 20 61 20  em".** stores a 
196de 73 69 6e 67 6c 65 20 76 61 6c 75 65 20 69 6e 20  single value in 
196df 74 68 65 20 56 44 42 45 2e 20 20 4d 65 6d 20 69  the VDBE.  Mem i
196e0 73 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75  s an opaque stru
196e1 63 74 75 72 65 20 76 69 73 69 62 6c 65 0a 2a 2a  cture visible.**
196e2 20 6f 6e 6c 79 20 77 69 74 68 69 6e 20 74 68 65   only within the
196e3 20 56 44 42 45 2e 20 20 49 6e 74 65 72 66 61 63   VDBE.  Interfac
196e4 65 20 72 6f 75 74 69 6e 65 73 20 72 65 66 65 72  e routines refer
196e5 20 74 6f 20 61 20 4d 65 6d 20 75 73 69 6e 67 20   to a Mem using 
196e6 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 73 71 6c 69  the.** name sqli
196e7 74 65 5f 76 61 6c 75 65 0a 2a 2f 0a 0a 2f 2a 0a  te_value.*/../*.
196e8 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56  ** Call sqlite3V
196e9 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
196ea 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69  () on the suppli
196eb 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d  ed value (type M
196ec 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71  em*).** P if req
196ed 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  uired..*/.#defin
196ee 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 50 29 20  e expandBlob(P) 
196ef 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d  (((P)->flags&MEM
196f0 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64  _Zero)?sqlite3Vd
196f1 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28  beMemExpandBlob(
196f2 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  P):0)../*.** If 
196f3 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 6a 65 63  pMem is an objec
196f4 74 20 77 69 74 68 20 61 20 76 61 6c 69 64 20 73  t with a valid s
196f5 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
196f6 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f 75 74 69  tion, this routi
196f7 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 20 74 68  ne.** ensures th
196f8 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e 63 6f 64  e internal encod
196f9 69 6e 67 20 66 6f 72 20 74 68 65 20 73 74 72 69  ing for the stri
196fa 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
196fb 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 72 65 64  n is.** 'desired
196fc 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 53 51 4c  Enc', one of SQL
196fd 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
196fe 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49  _UTF16LE or SQLI
196ff 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a 2a 0a 2a  TE_UTF16BE..**.*
19700 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 6e 6f 74  * If pMem is not
19701 20 61 20 73 74 72 69 6e 67 20 6f 62 6a 65 63 74   a string object
19702 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e  , or the encodin
19703 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a  g of the string.
19704 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ** representatio
19705 6e 20 69 73 20 61 6c 72 65 61 64 79 20 73 74 6f  n is already sto
19706 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 65  red using the re
19707 71 75 65 73 74 65 64 20 65 6e 63 6f 64 69 6e 67  quested encoding
19708 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72  , then this.** r
19709 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
1970a 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  p..**.** SQLITE_
1970b 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  OK is returned i
1970c 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  f the conversion
1970d 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 20 28   is successful (
1970e 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65 64 29  or not required)
1970f 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ..** SQLITE_NOME
19710 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65  M may be returne
19711 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  d if a malloc() 
19712 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63 6f 6e  fails during con
19713 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 74 77 65  version.** betwe
19714 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f 0a 53  en formats..*/.S
19715 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
19716 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  t sqlite3VdbeCha
19717 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20  ngeEncoding(Mem 
19718 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 73 69 72  *pMem, int desir
19719 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63  edEnc){.  int rc
1971a 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  ;.  assert( (pMe
1971b 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77  m->flags&MEM_Row
1971c 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Set)==0 );.  ass
1971d 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d  ert( desiredEnc=
1971e 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20  =SQLITE_UTF8 || 
1971f 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49  desiredEnc==SQLI
19720 54 45 5f 55 54 46 31 36 4c 45 0a 20 20 20 20 20  TE_UTF16LE.     
19721 20 20 20 20 20 20 7c 7c 20 64 65 73 69 72 65 64        || desired
19722 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  Enc==SQLITE_UTF1
19723 36 42 45 20 29 3b 0a 20 20 69 66 28 20 21 28 70  6BE );.  if( !(p
19724 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53  Mem->flags&MEM_S
19725 74 72 29 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63  tr) || pMem->enc
19726 3d 3d 64 65 73 69 72 65 64 45 6e 63 20 29 7b 0a  ==desiredEnc ){.
19727 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
19728 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65  E_OK;.  }.  asse
19729 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
1972a 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
1972b 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
1972c 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 64 65 66  mutex) );.#ifdef
1972d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1972e 31 36 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  16.  return SQLI
1972f 54 45 5f 45 52 52 4f 52 3b 0a 23 65 6c 73 65 0a  TE_ERROR;.#else.
19730 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c 61  .  /* MemTransla
19731 74 65 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20  te() may return 
19732 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c  SQLITE_OK or SQL
19733 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f  ITE_NOMEM. If NO
19734 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2c  MEM is returned,
19735 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 65  .  ** then the e
19736 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 76  ncoding of the v
19737 61 6c 75 65 20 6d 61 79 20 6e 6f 74 20 68 61 76  alue may not hav
19738 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a  e changed..  */.
19739 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1973a 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 70  beMemTranslate(p
1973b 4d 65 6d 2c 20 28 75 38 29 64 65 73 69 72 65 64  Mem, (u8)desired
1973c 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72  Enc);.  assert(r
1973d 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20  c==SQLITE_OK    
1973e 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  || rc==SQLITE_NO
1973f 4d 45 4d 29 3b 0a 20 20 61 73 73 65 72 74 28 72  MEM);.  assert(r
19740 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20  c==SQLITE_OK    
19741 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65  || pMem->enc!=de
19742 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73  siredEnc);.  ass
19743 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  ert(rc==SQLITE_N
19744 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e  OMEM || pMem->en
19745 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a  c==desiredEnc);.
19746 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e    return rc;.#en
19747 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  dif.}../*.** Mak
19748 65 20 73 75 72 65 20 70 4d 65 6d 2d 3e 7a 20 70  e sure pMem->z p
19749 6f 69 6e 74 73 20 74 6f 20 61 20 77 72 69 74 61  oints to a writa
1974a 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  ble allocation o
1974b 66 20 61 74 20 6c 65 61 73 74 20 0a 2a 2a 20 6e  f at least .** n
1974c 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   bytes..**.** If
1974d 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
1974e 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
1974f 69 6e 73 20 73 74 72 69 6e 67 20 6f 72 20 62 6c  ins string or bl
19750 6f 62 20 64 61 74 61 0a 2a 2a 20 61 6e 64 20 74  ob data.** and t
19751 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
19752 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73  t passed to this
19753 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72 75   function is tru
19754 65 2c 20 74 68 65 20 0a 2a 2a 20 63 75 72 72 65  e, the .** curre
19755 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  nt content of th
19756 65 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 72  e cell is preser
19757 76 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ved. Otherwise, 
19758 69 74 20 6d 61 79 0a 2a 2a 20 62 65 20 64 69 73  it may.** be dis
19759 63 61 72 64 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20  carded.  .**.** 
1975a 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65  This function se
1975b 74 73 20 74 68 65 20 4d 45 4d 5f 44 79 6e 20 66  ts the MEM_Dyn f
1975c 6c 61 67 20 61 6e 64 20 63 6c 65 61 72 73 20 61  lag and clears a
1975d 6e 79 20 78 44 65 6c 20 63 61 6c 6c 62 61 63 6b  ny xDel callback
1975e 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f 20 63 6c 65  ..** It also cle
1975f 61 72 73 20 4d 45 4d 5f 45 70 68 65 6d 20 61 6e  ars MEM_Ephem an
19760 64 20 4d 45 4d 5f 53 74 61 74 69 63 2e 20 49 66  d MEM_Static. If
19761 20 74 68 65 20 70 72 65 73 65 72 76 65 20 66 6c   the preserve fl
19762 61 67 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 73 65  ag is .** not se
19763 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 7a 65 72 6f  t, Mem.n is zero
19764 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
19765 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19766 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d  3VdbeMemGrow(Mem
19767 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69   *pMem, int n, i
19768 6e 74 20 70 72 65 73 65 72 76 65 29 7b 0a 20 20  nt preserve){.  
19769 61 73 73 65 72 74 28 20 31 20 3e 3d 0a 20 20 20  assert( 1 >=.   
1976a 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63   ((pMem->zMalloc
1976b 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f   && pMem->zMallo
1976c 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 3f 20 31 20  c==pMem->z) ? 1 
1976d 3a 20 30 29 20 2b 0a 20 20 20 20 28 28 28 70 4d  : 0) +.    (((pM
1976e 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79  em->flags&MEM_Dy
1976f 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 65 6c 29 20  n)&&pMem->xDel) 
19770 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20  ? 1 : 0) + .    
19771 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ((pMem->flags&ME
19772 4d 5f 45 70 68 65 6d 29 20 3f 20 31 20 3a 20 30  M_Ephem) ? 1 : 0
19773 29 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d  ) + .    ((pMem-
19774 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69  >flags&MEM_Stati
19775 63 29 20 3f 20 31 20 3a 20 30 29 0a 20 20 29 3b  c) ? 1 : 0).  );
19776 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
19777 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53  ->flags&MEM_RowS
19778 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28  et)==0 );..  if(
19779 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32 3b 0a   n<32 ) n = 32;.
1977a 20 20 69 66 28 20 73 71 6c 69 74 65 33 44 62 4d    if( sqlite3DbM
1977b 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d 3e  allocSize(pMem->
1977c 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  db, pMem->zMallo
1977d 63 29 3c 6e 20 29 7b 0a 20 20 20 20 69 66 28 20  c)<n ){.    if( 
1977e 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 6d  preserve && pMem
1977f 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  ->z==pMem->zMall
19780 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d  oc ){.      pMem
19781 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c  ->z = pMem->zMal
19782 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52  loc = sqlite3DbR
19783 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 4d 65  eallocOrFree(pMe
19784 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c 20  m->db, pMem->z, 
19785 6e 29 3b 0a 20 20 20 20 20 20 70 72 65 73 65 72  n);.      preser
19786 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  ve = 0;.    }els
19787 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
19788 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c  DbFree(pMem->db,
19789 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b   pMem->zMalloc);
1978a 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61  .      pMem->zMa
1978b 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62  lloc = sqlite3Db
1978c 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e  MallocRaw(pMem->
1978d 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20  db, n);.    }.  
1978e 7d 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a  }..  if( pMem->z
1978f 20 26 26 20 70 72 65 73 65 72 76 65 20 26 26 20   && preserve && 
19790 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26  pMem->zMalloc &&
19791 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e   pMem->z!=pMem->
19792 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 6d  zMalloc ){.    m
19793 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c  emcpy(pMem->zMal
19794 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d  loc, pMem->z, pM
19795 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69 66  em->n);.  }.  if
19796 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ( pMem->flags&ME
19797 4d 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e 78  M_Dyn && pMem->x
19798 44 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  Del ){.    pMem-
19799 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70  >xDel((void *)(p
1979a 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20  Mem->z));.  }.. 
1979b 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d   pMem->z = pMem-
1979c 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 66 28 20  >zMalloc;.  if( 
1979d 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20  pMem->z==0 ){.  
1979e 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1979f 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73  MEM_Null;.  }els
197a0 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  e{.    pMem->fla
197a1 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65  gs &= ~(MEM_Ephe
197a2 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20  m|MEM_Static);. 
197a3 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20   }.  pMem->xDel 
197a4 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  = 0;.  return (p
197a5 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 5f  Mem->z ? SQLITE_
197a6 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  OK : SQLITE_NOME
197a7 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  M);.}../*.** Mak
197a8 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20  e the given Mem 
197a9 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20  object MEM_Dyn. 
197aa 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
197ab 20 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 74   make it so.** t
197ac 68 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20  hat any TEXT or 
197ad 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20  BLOB content is 
197ae 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
197af 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
197b0 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e 20  * malloc().  In 
197b1 74 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e 6f  this way, we kno
197b2 77 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72  w that the memor
197b3 79 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 0a  y is safe to be.
197b4 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f  ** overwritten o
197b5 72 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a  r altered..**.**
197b6 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
197b7 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20  K on success or 
197b8 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20  SQLITE_NOMEM if 
197b9 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f  malloc fails..*/
197ba 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
197bb 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
197bc 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
197bd 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e  Mem *pMem){.  in
197be 74 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t f;.  assert( p
197bf 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
197c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
197c1 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
197c2 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ) );.  assert( (
197c3 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
197c4 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20  RowSet)==0 );.  
197c5 65 78 70 61 6e 64 42 6c 6f 62 28 70 4d 65 6d 29  expandBlob(pMem)
197c6 3b 0a 20 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c  ;.  f = pMem->fl
197c7 61 67 73 3b 0a 20 20 69 66 28 20 28 66 26 28 4d  ags;.  if( (f&(M
197c8 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29  EM_Str|MEM_Blob)
197c9 29 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d  ) && pMem->z!=pM
197ca 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20  em->zMalloc ){. 
197cb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
197cc 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20  beMemGrow(pMem, 
197cd 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c 20 31 29 20  pMem->n + 2, 1) 
197ce 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
197cf 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
197d0 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b    }.    pMem->z[
197d1 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20  pMem->n] = 0;.  
197d2 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
197d3 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d  n+1] = 0;.    pM
197d4 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  em->flags |= MEM
197d5 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a 20 20 72 65  _Term;.  }..  re
197d6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
197d7 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
197d8 67 69 76 65 6e 20 4d 65 6d 2a 20 68 61 73 20 61  given Mem* has a
197d9 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69   zero-filled tai
197da 6c 2c 20 74 75 72 6e 20 69 74 20 69 6e 74 6f 20  l, turn it into 
197db 61 6e 20 6f 72 64 69 6e 61 72 79 0a 2a 2a 20 62  an ordinary.** b
197dc 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e 20 64 79  lob stored in dy
197dd 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
197de 74 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 23 69  ted space..*/.#i
197df 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
197e0 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54  T_INCRBLOB.SQLIT
197e1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
197e2 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61  lite3VdbeMemExpa
197e3 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d  ndBlob(Mem *pMem
197e4 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66  ){.  if( pMem->f
197e5 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
197e6 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  ){.    int nByte
197e7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d  ;.    assert( pM
197e8 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c  em->flags&MEM_Bl
197e9 6f 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ob );.    assert
197ea 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  ( (pMem->flags&M
197eb 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
197ec 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
197ed 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
197ee 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
197ef 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
197f0 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 6e  );..    /* Set n
197f1 42 79 74 65 20 74 6f 20 74 68 65 20 6e 75 6d 62  Byte to the numb
197f2 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75  er of bytes requ
197f3 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  ired to store th
197f4 65 20 65 78 70 61 6e 64 65 64 20 62 6c 6f 62 2e  e expanded blob.
197f5 20 2a 2f 0a 20 20 20 20 6e 42 79 74 65 20 3d 20   */.    nByte = 
197f6 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e  pMem->n + pMem->
197f7 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 69 66 28  u.nZero;.    if(
197f8 20 6e 42 79 74 65 3c 3d 30 20 29 7b 0a 20 20 20   nByte<=0 ){.   
197f9 20 20 20 6e 42 79 74 65 20 3d 20 31 3b 0a 20 20     nByte = 1;.  
197fa 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
197fb 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
197fc 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 31 29 20 29  Mem, nByte, 1) )
197fd 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
197fe 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
197ff 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26   }..    memset(&
19800 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d  pMem->z[pMem->n]
19801 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65  , 0, pMem->u.nZe
19802 72 6f 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e  ro);.    pMem->n
19803 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72   += pMem->u.nZer
19804 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  o;.    pMem->fla
19805 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a 65 72 6f  gs &= ~(MEM_Zero
19806 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 20 7d 0a  |MEM_Term);.  }.
19807 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
19808 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f  OK;.}.#endif.../
19809 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  *.** Make sure t
1980a 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73 20  he given Mem is 
1980b 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 65  \u0000 terminate
1980c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1980d 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1980e 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
1980f 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ate(Mem *pMem){.
19810 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
19811 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
19812 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
19813 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
19814 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61    if( (pMem->fla
19815 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d  gs & MEM_Term)!=
19816 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  0 || (pMem->flag
19817 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20  s & MEM_Str)==0 
19818 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
19819 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e 6f  LITE_OK;   /* No
1981a 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20  thing to do */. 
1981b 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1981c 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
1981d 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 31 29 20  , pMem->n+2, 1) 
1981e 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
1981f 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
19820 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
19821 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e  n] = 0;.  pMem->
19822 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30  z[pMem->n+1] = 0
19823 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  ;.  pMem->flags 
19824 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 72  |= MEM_Term;.  r
19825 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
19826 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45  .}../*.** Add ME
19827 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 74  M_Str to the set
19828 20 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 69   of representati
19829 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 65  ons for the give
1982a 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 0a  n Mem.  Numbers.
1982b 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64  ** are converted
1982c 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 73   using sqlite3_s
1982d 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e 76  nprintf().  Conv
1982e 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74 6f  erting a BLOB to
1982f 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20   a string.** is 
19830 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45  a no-op..**.** E
19831 78 69 73 74 69 6e 67 20 72 65 70 72 65 73 65 6e  xisting represen
19832 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20  tations MEM_Int 
19833 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72 65  and MEM_Real are
19834 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61 74   *not* invalidat
19835 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f  ed..**.** A MEM_
19836 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c 20  Null value will 
19837 6e 65 76 65 72 20 62 65 20 70 61 73 73 65 64 20  never be passed 
19838 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
19839 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
1983a 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20 63  is.** used for c
1983b 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65 73  onverting values
1983c 20 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65 74   to text for ret
1983d 75 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75 73  urning to the us
1983e 65 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a 20  er (i.e. via.** 
1983f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
19840 78 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65 6e  xt()), or for en
19841 73 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c 75  suring that valu
19842 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 73  es to be used as
19843 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20 61   btree.** keys a
19844 72 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20 74  re strings. In t
19845 68 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20 61  he former case a
19846 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73   NULL pointer is
19847 20 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a 2a   returned the.**
19848 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c 61   user and the la
19849 74 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  ter is an intern
1984a 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 65  al programming e
1984b 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rror..*/.SQLITE_
1984c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1984d 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67  te3VdbeMemString
1984e 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  ify(Mem *pMem, i
1984f 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 72  nt enc){.  int r
19850 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
19851 20 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d 3e   int fg = pMem->
19852 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 74 20 69  flags;.  const i
19853 6e 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0a 0a  nt nByte = 32;..
19854 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
19855 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
19856 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
19857 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
19858 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 4d    assert( !(fg&M
19859 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20 61 73  EM_Zero) );.  as
1985a 73 65 72 74 28 20 21 28 66 67 26 28 4d 45 4d 5f  sert( !(fg&(MEM_
1985b 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 29  Str|MEM_Blob)) )
1985c 3b 0a 20 20 61 73 73 65 72 74 28 20 66 67 26 28  ;.  assert( fg&(
1985d 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c  MEM_Int|MEM_Real
1985e 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ) );.  assert( (
1985f 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
19860 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20  RowSet)==0 );.  
19861 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
19862 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65  TE_ALIGNMENT(pMe
19863 6d 29 20 29 3b 0a 0a 0a 20 20 69 66 28 20 73 71  m) );...  if( sq
19864 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
19865 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29  (pMem, nByte, 0)
19866 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
19867 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
19868 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20 52 65 61  ..  /* For a Rea
19869 6c 20 6f 72 20 49 6e 74 65 67 65 72 2c 20 75 73  l or Integer, us
1986a 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  e sqlite3_mprint
1986b 66 28 29 20 74 6f 20 70 72 6f 64 75 63 65 20 74  f() to produce t
1986c 68 65 20 55 54 46 2d 38 0a 20 20 2a 2a 20 73 74  he UTF-8.  ** st
1986d 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74  ring representat
1986e 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ion of the value
1986f 2e 20 54 68 65 6e 2c 20 69 66 20 74 68 65 20 72  . Then, if the r
19870 65 71 75 69 72 65 64 20 65 6e 63 6f 64 69 6e 67  equired encoding
19871 0a 20 20 2a 2a 20 69 73 20 55 54 46 2d 31 36 6c  .  ** is UTF-16l
19872 65 20 6f 72 20 55 54 46 2d 31 36 62 65 20 64 6f  e or UTF-16be do
19873 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2e 0a   a translation..
19874 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 49 58 20 4d    ** .  ** FIX M
19875 45 3a 20 49 74 20 77 6f 75 6c 64 20 62 65 20 62  E: It would be b
19876 65 74 74 65 72 20 69 66 20 73 71 6c 69 74 65 33  etter if sqlite3
19877 5f 73 6e 70 72 69 6e 74 66 28 29 20 63 6f 75 6c  _snprintf() coul
19878 64 20 64 6f 20 55 54 46 2d 31 36 2e 0a 20 20 2a  d do UTF-16..  *
19879 2f 0a 20 20 69 66 28 20 66 67 20 26 20 4d 45 4d  /.  if( fg & MEM
1987a 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  _Int ){.    sqli
1987b 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79  te3_snprintf(nBy
1987c 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 6c  te, pMem->z, "%l
1987d 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b  ld", pMem->u.i);
1987e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
1987f 73 65 72 74 28 20 66 67 20 26 20 4d 45 4d 5f 52  sert( fg & MEM_R
19880 65 61 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  eal );.    sqlit
19881 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 74  e3_snprintf(nByt
19882 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 21 2e  e, pMem->z, "%!.
19883 31 35 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a  15g", pMem->r);.
19884 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20    }.  pMem->n = 
19885 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
19886 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 70 4d 65 6d  pMem->z);.  pMem
19887 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ->enc = SQLITE_U
19888 54 46 38 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  TF8;.  pMem->fla
19889 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45  gs |= MEM_Str|ME
1988a 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 6c 69 74 65  M_Term;.  sqlite
1988b 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
1988c 69 6e 67 28 70 4d 65 6d 2c 20 65 6e 63 29 3b 0a  ing(pMem, enc);.
1988d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1988e 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c  /*.** Memory cel
1988f 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20  l pMem contains 
19890 74 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 61  the context of a
19891 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  n aggregate func
19892 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  tion..** This ro
19893 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 68 65 20  utine calls the 
19894 66 69 6e 61 6c 69 7a 65 20 6d 65 74 68 6f 64 20  finalize method 
19895 66 6f 72 20 74 68 61 74 20 66 75 6e 63 74 69 6f  for that functio
19896 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 65 73 75 6c  n.  The.** resul
19897 74 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61  t of the aggrega
19898 74 65 20 69 73 20 73 74 6f 72 65 64 20 62 61 63  te is stored bac
19899 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e 0a 2a 2a 0a  k into pMem..**.
1989a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
1989b 5f 45 52 52 4f 52 20 69 66 20 74 68 65 20 66 69  _ERROR if the fi
1989c 6e 61 6c 69 7a 65 72 20 72 65 70 6f 72 74 73 20  nalizer reports 
1989d 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 54  an error.  SQLIT
1989e 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 72 77 69 73  E_OK.** otherwis
1989f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
198a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
198a1 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28  VdbeMemFinalize(
198a2 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 75 6e 63 44  Mem *pMem, FuncD
198a3 65 66 20 2a 70 46 75 6e 63 29 7b 0a 20 20 69 6e  ef *pFunc){.  in
198a4 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
198a5 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  ;.  if( ALWAYS(p
198a6 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e 78  Func && pFunc->x
198a7 46 69 6e 61 6c 69 7a 65 29 20 29 7b 0a 20 20 20  Finalize) ){.   
198a8 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
198a9 20 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 74   ctx;.    assert
198aa 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
198ab 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c   MEM_Null)!=0 ||
198ac 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e   pFunc==pMem->u.
198ad 70 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73 65  pDef );.    asse
198ae 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
198af 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
198b0 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
198b1 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 6d 65  mutex) );.    me
198b2 6d 73 65 74 28 26 63 74 78 2c 20 30 2c 20 73 69  mset(&ctx, 0, si
198b3 7a 65 6f 66 28 63 74 78 29 29 3b 0a 20 20 20 20  zeof(ctx));.    
198b4 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45  ctx.s.flags = ME
198b5 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e  M_Null;.    ctx.
198b6 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b  s.db = pMem->db;
198b7 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20  .    ctx.pMem = 
198b8 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46  pMem;.    ctx.pF
198b9 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20  unc = pFunc;.   
198ba 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a   pFunc->xFinaliz
198bb 65 28 26 63 74 78 29 3b 0a 20 20 20 20 61 73 73  e(&ctx);.    ass
198bc 65 72 74 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66  ert( 0==(pMem->f
198bd 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26  lags&MEM_Dyn) &&
198be 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a   !pMem->xDel );.
198bf 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
198c0 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d  e(pMem->db, pMem
198c1 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20  ->zMalloc);.    
198c2 6d 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63 74  memcpy(pMem, &ct
198c3 78 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e  x.s, sizeof(ctx.
198c4 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 74  s));.    rc = ct
198c5 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20  x.isError;.  }. 
198c6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
198c7 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f  *.** If the memo
198c8 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 73  ry cell contains
198c9 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20   a string value 
198ca 74 68 61 74 20 6d 75 73 74 20 62 65 20 66 72 65  that must be fre
198cb 65 64 20 62 79 0a 2a 2a 20 69 6e 76 6f 6b 69 6e  ed by.** invokin
198cc 67 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 63 61  g an external ca
198cd 6c 6c 62 61 63 6b 2c 20 66 72 65 65 20 69 74 20  llback, free it 
198ce 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 20 74 68 69  now. Calling thi
198cf 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 64 6f  s function.** do
198d0 65 73 20 6e 6f 74 20 66 72 65 65 20 61 6e 79 20  es not free any 
198d1 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62 75 66 66  Mem.zMalloc buff
198d2 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
198d3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
198d4 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
198d5 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29  External(Mem *p)
198d6 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64  {.  assert( p->d
198d7 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
198d8 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
198d9 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65  ->mutex) );.  te
198da 73 74 63 61 73 65 28 20 70 2d 3e 66 6c 61 67 73  stcase( p->flags
198db 20 26 20 4d 45 4d 5f 41 67 67 20 29 3b 0a 20 20   & MEM_Agg );.  
198dc 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66 6c 61  testcase( p->fla
198dd 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 3b 0a  gs & MEM_Dyn );.
198de 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66    testcase( p->f
198df 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
198e0 74 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  t );.  testcase(
198e1 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   p->flags & MEM_
198e2 46 72 61 6d 65 20 29 3b 0a 20 20 69 66 28 20 70  Frame );.  if( p
198e3 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67  ->flags&(MEM_Agg
198e4 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 52 6f 77  |MEM_Dyn|MEM_Row
198e5 53 65 74 7c 4d 45 4d 5f 46 72 61 6d 65 29 20 29  Set|MEM_Frame) )
198e6 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61  {.    if( p->fla
198e7 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b 0a 20 20  gs&MEM_Agg ){.  
198e8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
198e9 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c 20 70 2d  emFinalize(p, p-
198ea 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 20 20 20  >u.pDef);.      
198eb 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67  assert( (p->flag
198ec 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20  s & MEM_Agg)==0 
198ed 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
198ee 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
198ef 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
198f0 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79   p->flags&MEM_Dy
198f1 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0a  n && p->xDel ){.
198f2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
198f3 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53  ->flags&MEM_RowS
198f4 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  et)==0 );.      
198f5 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29  p->xDel((void *)
198f6 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 2d 3e  p->z);.      p->
198f7 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 7d 65  xDel = 0;.    }e
198f8 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  lse if( p->flags
198f9 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20  &MEM_RowSet ){. 
198fa 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53       sqlite3RowS
198fb 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e 70 52 6f  etClear(p->u.pRo
198fc 77 53 65 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  wSet);.    }else
198fd 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45   if( p->flags&ME
198fe 4d 5f 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 20  M_Frame ){.     
198ff 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19900 65 74 4e 75 6c 6c 28 70 29 3b 0a 20 20 20 20 7d  etNull(p);.    }
19901 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
19902 6c 65 61 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79  lease any memory
19903 20 68 65 6c 64 20 62 79 20 74 68 65 20 4d 65 6d   held by the Mem
19904 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 76 65  . This may leave
19905 20 74 68 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a   the Mem in an.*
19906 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73  * inconsistent s
19907 74 61 74 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c  tate, for exampl
19908 65 20 77 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30  e with (Mem.z==0
19909 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79  ) and.** (Mem.ty
1990a 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 29  pe==SQLITE_TEXT)
1990b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1990c 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1990d 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d  VdbeMemRelease(M
1990e 65 6d 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  em *p){.  sqlite
1990f 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45  3VdbeMemReleaseE
19910 78 74 65 72 6e 61 6c 28 70 29 3b 0a 20 20 73 71  xternal(p);.  sq
19911 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64  lite3DbFree(p->d
19912 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a  b, p->zMalloc);.
19913 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70 2d    p->z = 0;.  p-
19914 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20  >zMalloc = 0;.  
19915 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a 0a  p->xDel = 0;.}..
19916 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
19917 36 34 2d 62 69 74 20 49 45 45 45 20 64 6f 75 62  64-bit IEEE doub
19918 6c 65 20 69 6e 74 6f 20 61 20 36 34 2d 62 69 74  le into a 64-bit
19919 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e   signed integer.
1991a 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 75 62 6c  .** If the doubl
1991b 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20  e is too large, 
1991c 72 65 74 75 72 6e 20 30 78 38 30 30 30 30 30 30  return 0x8000000
1991d 30 30 30 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a 2a  000000000..**.**
1991e 20 4d 6f 73 74 20 73 79 73 74 65 6d 73 20 61 70   Most systems ap
1991f 70 65 61 72 20 74 6f 20 64 6f 20 74 68 69 73 20  pear to do this 
19920 73 69 6d 70 6c 79 20 62 79 20 61 73 73 69 67 6e  simply by assign
19921 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73  ing.** variables
19922 20 61 6e 64 20 77 69 74 68 6f 75 74 20 74 68 65   and without the
19923 20 65 78 74 72 61 20 72 61 6e 67 65 20 74 65 73   extra range tes
19924 74 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 72  ts.  But.** ther
19925 65 20 61 72 65 20 72 65 70 6f 72 74 73 20 74 68  e are reports th
19926 61 74 20 77 69 6e 64 6f 77 73 20 74 68 72 6f 77  at windows throw
19927 73 20 61 6e 20 65 78 70 65 63 74 69 6f 6e 0a 2a  s an expection.*
19928 2a 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e  * if the floatin
19929 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73  g point value is
1992a 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 28   out of range. (
1992b 53 65 65 20 74 69 63 6b 65 74 20 23 32 38 38 30  See ticket #2880
1992c 2e 29 0a 2a 2a 20 42 65 63 61 75 73 65 20 77 65  .).** Because we
1992d 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65   do not complete
1992e 6c 79 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68  ly understand th
1992f 65 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 77 69  e problem, we wi
19930 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 63  ll.** take the c
19931 6f 6e 73 65 72 76 61 74 69 76 65 20 61 70 70 72  onservative appr
19932 6f 61 63 68 20 61 6e 64 20 61 6c 77 61 79 73 20  oach and always 
19933 64 6f 20 72 61 6e 67 65 20 74 65 73 74 73 0a 2a  do range tests.*
19934 2a 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74  * before attempt
19935 69 6e 67 20 74 68 65 20 63 6f 6e 76 65 72 73 69  ing the conversi
19936 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36  on..*/.static i6
19937 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28  4 doubleToInt64(
19938 64 6f 75 62 6c 65 20 72 29 7b 0a 20 20 2f 2a 0a  double r){.  /*.
19939 20 20 2a 2a 20 4d 61 6e 79 20 63 6f 6d 70 69 6c    ** Many compil
1993a 65 72 73 20 77 65 20 65 6e 63 6f 75 6e 74 65 72  ers we encounter
1993b 20 64 6f 20 6e 6f 74 20 64 65 66 69 6e 65 20 63   do not define c
1993c 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65  onstants for the
1993d 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61 6e  .  ** minimum an
1993e 64 20 6d 61 78 69 6d 75 6d 20 36 34 2d 62 69 74  d maximum 64-bit
1993f 20 69 6e 74 65 67 65 72 73 2c 20 6f 72 20 74 68   integers, or th
19940 65 79 20 64 65 66 69 6e 65 20 74 68 65 6d 0a 20  ey define them. 
19941 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74   ** inconsistent
19942 6c 79 2e 20 20 41 6e 64 20 6d 61 6e 79 20 64 6f  ly.  And many do
19943 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20   not understand 
19944 74 68 65 20 22 4c 4c 22 20 6e 6f 74 61 74 69 6f  the "LL" notatio
19945 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20 64 65  n..  ** So we de
19946 66 69 6e 65 20 6f 75 72 20 6f 77 6e 20 73 74 61  fine our own sta
19947 74 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 68 65  tic constants he
19948 72 65 20 75 73 69 6e 67 20 6e 6f 74 68 69 6e 67  re using nothing
19949 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61  .  ** larger tha
1994a 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  n a 32-bit integ
1994b 65 72 20 63 6f 6e 73 74 61 6e 74 2e 0a 20 20 2a  er constant..  *
1994c 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  /.  static const
1994d 20 69 36 34 20 6d 61 78 49 6e 74 20 3d 20 4c 41   i64 maxInt = LA
1994e 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a 20 20 73  RGEST_INT64;.  s
1994f 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20  tatic const i64 
19950 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 53  minInt = SMALLES
19951 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 69 66 28 20  T_INT64;..  if( 
19952 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e 74  r<(double)minInt
19953 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d   ){.    return m
19954 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69  inInt;.  }else i
19955 66 28 20 72 3e 28 64 6f 75 62 6c 65 29 6d 61 78  f( r>(double)max
19956 49 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20 6d 69  Int ){.    /* mi
19957 6e 49 6e 74 20 69 73 20 63 6f 72 72 65 63 74 20  nInt is correct 
19958 68 65 72 65 20 2d 20 6e 6f 74 20 6d 61 78 49 6e  here - not maxIn
19959 74 2e 20 20 49 74 20 74 75 72 6e 73 20 6f 75 74  t.  It turns out
1995a 20 74 68 61 74 20 61 73 73 69 67 6e 69 6e 67 0a   that assigning.
1995b 20 20 20 20 2a 2a 20 61 20 76 65 72 79 20 6c 61      ** a very la
1995c 72 67 65 20 70 6f 73 69 74 69 76 65 20 6e 75 6d  rge positive num
1995d 62 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 65  ber to an intege
1995e 72 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 76  r results in a v
1995f 65 72 79 20 6c 61 72 67 65 0a 20 20 20 20 2a 2a  ery large.    **
19960 20 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67 65   negative intege
19961 72 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 6e  r.  This makes n
19962 6f 20 73 65 6e 73 65 2c 20 62 75 74 20 69 74 20  o sense, but it 
19963 69 73 20 77 68 61 74 20 78 38 36 20 68 61 72 64  is what x86 hard
19964 77 61 72 65 0a 20 20 20 20 2a 2a 20 64 6f 65 73  ware.    ** does
19965 20 73 6f 20 66 6f 72 20 63 6f 6d 70 61 74 69 62   so for compatib
19966 69 6c 69 74 79 20 77 65 20 77 69 6c 6c 20 64 6f  ility we will do
19967 20 74 68 65 20 73 61 6d 65 20 69 6e 20 73 6f 66   the same in sof
19968 74 77 61 72 65 2e 20 2a 2f 0a 20 20 20 20 72 65  tware. */.    re
19969 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d  turn minInt;.  }
1996a 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
1996b 20 28 69 36 34 29 72 3b 0a 20 20 7d 0a 7d 0a 0a   (i64)r;.  }.}..
1996c 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d  /*.** Return som
1996d 65 20 6b 69 6e 64 20 6f 66 20 69 6e 74 65 67 65  e kind of intege
1996e 72 20 76 61 6c 75 65 20 77 68 69 63 68 20 69 73  r value which is
1996f 20 74 68 65 20 62 65 73 74 20 77 65 20 63 61 6e   the best we can
19970 20 64 6f 0a 2a 2a 20 61 74 20 72 65 70 72 65 73   do.** at repres
19971 65 6e 74 69 6e 67 20 74 68 65 20 76 61 6c 75 65  enting the value
19972 20 74 68 61 74 20 2a 70 4d 65 6d 20 64 65 73 63   that *pMem desc
19973 72 69 62 65 73 20 61 73 20 61 6e 20 69 6e 74 65  ribes as an inte
19974 67 65 72 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20  ger..** If pMem 
19975 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74  is an integer, t
19976 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73  hen the value is
19977 20 65 78 61 63 74 2e 20 20 49 66 20 70 4d 65 6d   exact.  If pMem
19978 20 69 73 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e   is.** a floatin
19979 67 2d 70 6f 69 6e 74 20 74 68 65 6e 20 74 68 65  g-point then the
1997a 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
1997b 69 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 70  is the integer p
1997c 61 72 74 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20  art..** If pMem 
1997d 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62  is a string or b
1997e 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b  lob, then we mak
1997f 65 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  e an attempt to 
19980 63 6f 6e 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e  convert.** it in
19981 74 6f 20 61 20 69 6e 74 65 67 65 72 20 61 6e 64  to a integer and
19982 20 72 65 74 75 72 6e 20 74 68 61 74 2e 20 20 49   return that.  I
19983 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74  f pMem represent
19984 73 20 61 6e 0a 2a 2a 20 61 6e 20 53 51 4c 2d 4e  s an.** an SQL-N
19985 55 4c 4c 20 76 61 6c 75 65 2c 20 72 65 74 75 72  ULL value, retur
19986 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d  n 0..**.** If pM
19987 65 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 20  em represents a 
19988 73 74 72 69 6e 67 20 76 61 6c 75 65 2c 20 69 74  string value, it
19989 73 20 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74  s encoding might
1998a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a   be changed..*/.
1998b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1998c 36 34 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e  64 sqlite3VdbeIn
1998d 74 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d  tValue(Mem *pMem
1998e 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a  ){.  int flags;.
1998f 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
19990 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
19991 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
19992 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
19993 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f    assert( EIGHT_
19994 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70  BYTE_ALIGNMENT(p
19995 4d 65 6d 29 20 29 3b 0a 20 20 66 6c 61 67 73 20  Mem) );.  flags 
19996 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20  = pMem->flags;. 
19997 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d   if( flags & MEM
19998 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75  _Int ){.    retu
19999 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20  rn pMem->u.i;.  
1999a 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20  }else if( flags 
1999b 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20  & MEM_Real ){.  
1999c 20 20 72 65 74 75 72 6e 20 64 6f 75 62 6c 65 54    return doubleT
1999d 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b  oInt64(pMem->r);
1999e 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61  .  }else if( fla
1999f 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45  gs & (MEM_Str|ME
199a0 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69  M_Blob) ){.    i
199a1 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d  64 value;.    pM
199a2 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  em->flags |= MEM
199a3 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71  _Str;.    if( sq
199a4 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
199a5 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51  ncoding(pMem, SQ
199a6 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20  LITE_UTF8).     
199a7 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65    || sqlite3Vdbe
199a8 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28  MemNulTerminate(
199a9 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 72  pMem) ){.      r
199aa 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
199ab 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d     assert( pMem-
199ac 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  >z );.    sqlite
199ad 33 41 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c  3Atoi64(pMem->z,
199ae 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 72 65   &value);.    re
199af 74 75 72 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65  turn value;.  }e
199b0 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
199b1 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
199b2 52 65 74 75 72 6e 20 74 68 65 20 62 65 73 74 20  Return the best 
199b3 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
199b4 66 20 70 4d 65 6d 20 74 68 61 74 20 77 65 20 63  f pMem that we c
199b5 61 6e 20 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a  an get into a.**
199b6 20 64 6f 75 62 6c 65 2e 20 20 49 66 20 70 4d 65   double.  If pMe
199b7 6d 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 64  m is already a d
199b8 6f 75 62 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65  ouble or an inte
199b9 67 65 72 2c 20 72 65 74 75 72 6e 20 69 74 73 0a  ger, return its.
199ba 2a 2a 20 76 61 6c 75 65 2e 20 20 49 66 20 69 74  ** value.  If it
199bb 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20   is a string or 
199bc 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e  blob, try to con
199bd 76 65 72 74 20 69 74 20 74 6f 20 61 20 64 6f 75  vert it to a dou
199be 62 6c 65 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73  ble..** If it is
199bf 20 61 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20   a NULL, return 
199c0 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  0.0..*/.SQLITE_P
199c1 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71  RIVATE double sq
199c2 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
199c3 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  ue(Mem *pMem){. 
199c4 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
199c5 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
199c6 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
199c7 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
199c8 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42   assert( EIGHT_B
199c9 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d  YTE_ALIGNMENT(pM
199ca 65 6d 29 20 29 3b 0a 20 20 69 66 28 20 70 4d 65  em) );.  if( pMe
199cb 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  m->flags & MEM_R
199cc 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  eal ){.    retur
199cd 6e 20 70 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c  n pMem->r;.  }el
199ce 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  se if( pMem->fla
199cf 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a  gs & MEM_Int ){.
199d0 20 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62      return (doub
199d1 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20  le)pMem->u.i;.  
199d2 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e  }else if( pMem->
199d3 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72  flags & (MEM_Str
199d4 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20  |MEM_Blob) ){.  
199d5 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49    /* (double)0 I
199d6 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45  n case of SQLITE
199d7 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
199d8 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 64  OINT... */.    d
199d9 6f 75 62 6c 65 20 76 61 6c 20 3d 20 28 64 6f 75  ouble val = (dou
199da 62 6c 65 29 30 3b 0a 20 20 20 20 70 4d 65 6d 2d  ble)0;.    pMem-
199db 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74  >flags |= MEM_St
199dc 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  r;.    if( sqlit
199dd 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
199de 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54  ding(pMem, SQLIT
199df 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20 7c  E_UTF8).       |
199e0 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  | sqlite3VdbeMem
199e1 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d 65  NulTerminate(pMe
199e2 6d 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 28  m) ){.      /* (
199e3 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65  double)0 In case
199e4 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   of SQLITE_OMIT_
199e5 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e  FLOATING_POINT..
199e6 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72  . */.      retur
199e7 6e 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20  n (double)0;.   
199e8 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
199e9 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71  Mem->z );.    sq
199ea 6c 69 74 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e  lite3AtoF(pMem->
199eb 7a 2c 20 26 76 61 6c 29 3b 0a 20 20 20 20 72 65  z, &val);.    re
199ec 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c 73  turn val;.  }els
199ed 65 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c  e{.    /* (doubl
199ee 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53  e)0 In case of S
199ef 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
199f0 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a  ING_POINT... */.
199f1 20 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62      return (doub
199f2 6c 65 29 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  le)0;.  }.}../*.
199f3 2a 2a 20 54 68 65 20 4d 45 4d 20 73 74 72 75 63  ** The MEM struc
199f4 74 75 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  ture is already 
199f5 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 20 54 72 79  a MEM_Real.  Try
199f6 20 74 6f 20 61 6c 73 6f 20 6d 61 6b 65 20 69 74   to also make it
199f7 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69 66   a.** MEM_Int if
199f8 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c 49   we can..*/.SQLI
199f9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
199fa 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67  sqlite3VdbeInteg
199fb 65 72 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a  erAffinity(Mem *
199fc 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28  pMem){.  assert(
199fd 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
199fe 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 61 73 73  EM_Real );.  ass
199ff 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  ert( (pMem->flag
19a00 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
19a01 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
19a02 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
19a03 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
19a04 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
19a05 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
19a06 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e  EIGHT_BYTE_ALIGN
19a07 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20  MENT(pMem) );.. 
19a08 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75   pMem->u.i = dou
19a09 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d  bleToInt64(pMem-
19a0a 3e 72 29 3b 0a 0a 20 20 2f 2a 20 4f 6e 6c 79 20  >r);..  /* Only 
19a0b 6d 61 72 6b 20 74 68 65 20 76 61 6c 75 65 20 61  mark the value a
19a0c 73 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66 0a  s an integer if.
19a0d 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 28 31 29    **.  **    (1)
19a0e 20 74 68 65 20 72 6f 75 6e 64 2d 74 72 69 70 20   the round-trip 
19a0f 63 6f 6e 76 65 72 73 69 6f 6e 20 72 65 61 6c 2d  conversion real-
19a10 3e 69 6e 74 2d 3e 72 65 61 6c 20 69 73 20 61 20  >int->real is a 
19a11 6e 6f 2d 6f 70 2c 20 61 6e 64 0a 20 20 2a 2a 20  no-op, and.  ** 
19a12 20 20 20 28 32 29 20 54 68 65 20 69 6e 74 65 67     (2) The integ
19a13 65 72 20 69 73 20 6e 65 69 74 68 65 72 20 74 68  er is neither th
19a14 65 20 6c 61 72 67 65 73 74 20 6e 6f 72 20 74 68  e largest nor th
19a15 65 20 73 6d 61 6c 6c 65 73 74 0a 20 20 2a 2a 20  e smallest.  ** 
19a16 20 20 20 20 20 20 20 70 6f 73 73 69 62 6c 65 20         possible 
19a17 69 6e 74 65 67 65 72 20 28 74 69 63 6b 65 74 20  integer (ticket 
19a18 23 33 39 32 32 29 0a 20 20 2a 2a 0a 20 20 2a 2a  #3922).  **.  **
19a19 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20   The second and 
19a1a 74 68 69 72 64 20 74 65 72 6d 73 20 69 6e 20 74  third terms in t
19a1b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e  he following con
19a1c 64 69 74 69 6f 6e 61 6c 20 65 6e 66 6f 72 63 65  ditional enforce
19a1d 73 0a 20 20 2a 2a 20 74 68 65 20 73 65 63 6f 6e  s.  ** the secon
19a1e 64 20 63 6f 6e 64 69 74 69 6f 6e 20 75 6e 64 65  d condition unde
19a1f 72 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e  r the assumption
19a20 20 74 68 61 74 20 61 64 64 69 74 69 6f 6e 20 6f   that addition o
19a21 76 65 72 66 6c 6f 77 20 63 61 75 73 65 73 0a 20  verflow causes. 
19a22 20 2a 2a 20 76 61 6c 75 65 73 20 74 6f 20 77 72   ** values to wr
19a23 61 70 20 61 72 6f 75 6e 64 2e 20 20 4f 6e 20 78  ap around.  On x
19a24 38 36 20 68 61 72 64 77 61 72 65 2c 20 74 68 65  86 hardware, the
19a25 20 74 68 69 72 64 20 74 65 72 6d 20 69 73 20 61   third term is a
19a26 6c 77 61 79 73 0a 20 20 2a 2a 20 74 72 75 65 20  lways.  ** true 
19a27 61 6e 64 20 63 6f 75 6c 64 20 62 65 20 6f 6d 69  and could be omi
19a28 74 74 65 64 2e 20 20 42 75 74 20 77 65 20 6c 65  tted.  But we le
19a29 61 76 65 20 69 74 20 69 6e 20 62 65 63 61 75 73  ave it in becaus
19a2a 65 20 6f 74 68 65 72 0a 20 20 2a 2a 20 61 72 63  e other.  ** arc
19a2b 68 69 74 65 63 74 75 72 65 73 20 6d 69 67 68 74  hitectures might
19a2c 20 62 65 68 61 76 65 20 64 69 66 66 65 72 65 6e   behave differen
19a2d 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  tly..  */.  if( 
19a2e 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65  pMem->r==(double
19a2f 29 70 4d 65 6d 2d 3e 75 2e 69 20 26 26 20 70 4d  )pMem->u.i && pM
19a30 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c 4c 45 53 54  em->u.i>SMALLEST
19a31 5f 49 4e 54 36 34 0a 20 20 20 20 20 20 26 26 20  _INT64.      && 
19a32 41 4c 57 41 59 53 28 70 4d 65 6d 2d 3e 75 2e 69  ALWAYS(pMem->u.i
19a33 3c 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 20  <LARGEST_INT64) 
19a34 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ){.    pMem->fla
19a35 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  gs |= MEM_Int;. 
19a36 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76   }.}../*.** Conv
19a37 65 72 74 20 70 4d 65 6d 20 74 6f 20 74 79 70 65  ert pMem to type
19a38 20 69 6e 74 65 67 65 72 2e 20 20 49 6e 76 61 6c   integer.  Inval
19a39 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
19a3a 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
19a3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19a3c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19a3d 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
19a3e 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73  Mem *pMem){.  as
19a3f 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
19a40 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
19a41 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
19a42 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
19a43 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
19a44 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
19a45 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
19a46 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
19a47 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a  NMENT(pMem) );..
19a48 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 71    pMem->u.i = sq
19a49 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
19a4a 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65  e(pMem);.  MemSe
19a4b 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20  tTypeFlag(pMem, 
19a4c 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72 65 74 75  MEM_Int);.  retu
19a4d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
19a4e 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70  ./*.** Convert p
19a4f 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 69  Mem so that it i
19a50 73 20 6f 66 20 74 79 70 65 20 4d 45 4d 5f 52 65  s of type MEM_Re
19a51 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74  al..** Invalidat
19a52 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72  e any prior repr
19a53 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  esentations..*/.
19a54 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19a55 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
19a56 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20 2a 70 4d  mRealify(Mem *pM
19a57 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  em){.  assert( p
19a58 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
19a59 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
19a5a 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
19a5b 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45  ) );.  assert( E
19a5c 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d  IGHT_BYTE_ALIGNM
19a5d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 20  ENT(pMem) );..  
19a5e 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65  pMem->r = sqlite
19a5f 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
19a60 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79  Mem);.  MemSetTy
19a61 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d  peFlag(pMem, MEM
19a62 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e  _Real);.  return
19a63 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
19a64 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65  *.** Convert pMe
19a65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 68 61 73  m so that it has
19a66 20 74 79 70 65 73 20 4d 45 4d 5f 52 65 61 6c 20   types MEM_Real 
19a67 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72 20 62 6f  or MEM_Int or bo
19a68 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74  th..** Invalidat
19a69 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72  e any prior repr
19a6a 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  esentations..*/.
19a6b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19a6c 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
19a6d 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 20 2a 70  mNumerify(Mem *p
19a6e 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72  Mem){.  double r
19a6f 31 2c 20 72 32 3b 0a 20 20 69 36 34 20 69 3b 0a  1, r2;.  i64 i;.
19a70 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
19a71 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e  >flags & (MEM_In
19a72 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 4e  t|MEM_Real|MEM_N
19a73 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73  ull))==0 );.  as
19a74 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
19a75 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d  gs & (MEM_Blob|M
19a76 45 4d 5f 53 74 72 29 29 21 3d 30 20 29 3b 0a 20  EM_Str))!=0 );. 
19a77 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
19a78 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
19a79 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
19a7a 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
19a7b 20 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   r1 = sqlite3Vdb
19a7c 65 52 65 61 6c 56 61 6c 75 65 28 70 4d 65 6d 29  eRealValue(pMem)
19a7d 3b 0a 20 20 69 20 3d 20 64 6f 75 62 6c 65 54 6f  ;.  i = doubleTo
19a7e 49 6e 74 36 34 28 72 31 29 3b 0a 20 20 72 32 20  Int64(r1);.  r2 
19a7f 3d 20 28 64 6f 75 62 6c 65 29 69 3b 0a 20 20 69  = (double)i;.  i
19a80 66 28 20 72 31 3d 3d 72 32 20 29 7b 0a 20 20 20  f( r1==r2 ){.   
19a81 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
19a82 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b  ntegerify(pMem);
19a83 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d  .  }else{.    pM
19a84 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a 20 20 20 20  em->r = r1;.    
19a85 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
19a86 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a  Mem, MEM_Real);.
19a87 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
19a88 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
19a89 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76   Delete any prev
19a8a 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73  ious value and s
19a8b 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  et the value sto
19a8c 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20  red in *pMem to 
19a8d 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NULL..*/.SQLITE_
19a8e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19a8f 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
19a90 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  ll(Mem *pMem){. 
19a91 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
19a92 20 26 20 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a   & MEM_Frame ){.
19a93 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
19a94 72 61 6d 65 44 65 6c 65 74 65 28 70 4d 65 6d 2d  rameDelete(pMem-
19a95 3e 75 2e 70 46 72 61 6d 65 29 3b 0a 20 20 7d 0a  >u.pFrame);.  }.
19a96 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67    if( pMem->flag
19a97 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29  s & MEM_RowSet )
19a98 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77  {.    sqlite3Row
19a99 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75  SetClear(pMem->u
19a9a 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 7d 0a 20  .pRowSet);.  }. 
19a9b 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
19a9c 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b  pMem, MEM_Null);
19a9d 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20  .  pMem->type = 
19a9e 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 7d 0a 0a  SQLITE_NULL;.}..
19a9f 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79  /*.** Delete any
19aa0 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20   previous value 
19aa1 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75  and set the valu
19aa2 65 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 6f  e to be a BLOB o
19aa3 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e 20 63 6f  f length.** n co
19aa4 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72  ntaining all zer
19aa5 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  os..*/.SQLITE_PR
19aa6 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
19aa7 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f  e3VdbeMemSetZero
19aa8 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  Blob(Mem *pMem, 
19aa9 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65  int n){.  sqlite
19aaa 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
19aab 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66  pMem);.  pMem->f
19aac 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c  lags = MEM_Blob|
19aad 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 4d 65 6d  MEM_Zero;.  pMem
19aae 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
19aaf 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d 3e 6e 20  BLOB;.  pMem->n 
19ab0 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29  = 0;.  if( n<0 )
19ab1 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e   n = 0;.  pMem->
19ab2 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a 20 20 70  u.nZero = n;.  p
19ab3 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
19ab4 45 5f 55 54 46 38 3b 0a 0a 23 69 66 64 65 66 20  E_UTF8;..#ifdef 
19ab5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
19ab6 42 4c 4f 42 0a 20 20 73 71 6c 69 74 65 33 56 64  BLOB.  sqlite3Vd
19ab7 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20  beMemGrow(pMem, 
19ab8 6e 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 4d 65  n, 0);.  if( pMe
19ab9 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 70 4d 65 6d  m->z ){.    pMem
19aba 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20 20 6d 65 6d  ->n = n;.    mem
19abb 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20  set(pMem->z, 0, 
19abc 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  n);.  }.#endif.}
19abd 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
19abe 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75  ny previous valu
19abf 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61  e and set the va
19ac0 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70  lue stored in *p
19ac1 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d  Mem to val,.** m
19ac2 61 6e 69 66 65 73 74 20 74 79 70 65 20 49 4e 54  anifest type INT
19ac3 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  EGER..*/.SQLITE_
19ac4 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19ac5 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e  ite3VdbeMemSetIn
19ac6 74 36 34 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  t64(Mem *pMem, i
19ac7 36 34 20 76 61 6c 29 7b 0a 20 20 73 71 6c 69 74  64 val){.  sqlit
19ac8 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
19ac9 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e  (pMem);.  pMem->
19aca 75 2e 69 20 3d 20 76 61 6c 3b 0a 20 20 70 4d 65  u.i = val;.  pMe
19acb 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
19acc 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65  nt;.  pMem->type
19acd 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45   = SQLITE_INTEGE
19ace 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  R;.}../*.** Dele
19acf 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20  te any previous 
19ad0 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68  value and set th
19ad1 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  e value stored i
19ad2 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a  n *pMem to val,.
19ad3 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65  ** manifest type
19ad4 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45   REAL..*/.SQLITE
19ad5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
19ad6 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44  lite3VdbeMemSetD
19ad7 6f 75 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c  ouble(Mem *pMem,
19ad8 20 64 6f 75 62 6c 65 20 76 61 6c 29 7b 0a 20 20   double val){.  
19ad9 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  if( sqlite3IsNaN
19ada 28 76 61 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c  (val) ){.    sql
19adb 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
19adc 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73  ll(pMem);.  }els
19add 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  e{.    sqlite3Vd
19ade 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
19adf 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20  m);.    pMem->r 
19ae0 3d 20 76 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d  = val;.    pMem-
19ae1 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61  >flags = MEM_Rea
19ae2 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  l;.    pMem->typ
19ae3 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  e = SQLITE_FLOAT
19ae4 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
19ae5 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f  elete any previo
19ae6 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74  us value and set
19ae7 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 4d   the value of pM
19ae8 65 6d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 65  em to be an.** e
19ae9 6d 70 74 79 20 62 6f 6f 6c 65 61 6e 20 69 6e 64  mpty boolean ind
19aea 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ex..*/.SQLITE_PR
19aeb 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
19aec 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53  e3VdbeMemSetRowS
19aed 65 74 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  et(Mem *pMem){. 
19aee 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
19aef 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72  Mem->db;.  asser
19af0 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73  t( db!=0 );.  as
19af1 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
19af2 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
19af3 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ==0 );.  sqlite3
19af4 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
19af5 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d  Mem);.  pMem->zM
19af6 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  alloc = sqlite3D
19af7 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 36  bMallocRaw(db, 6
19af8 34 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  4);.  if( db->ma
19af9 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
19afa 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
19afb 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73  MEM_Null;.  }els
19afc 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
19afd 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a  Mem->zMalloc );.
19afe 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77      pMem->u.pRow
19aff 53 65 74 20 3d 20 73 71 6c 69 74 65 33 52 6f 77  Set = sqlite3Row
19b00 53 65 74 49 6e 69 74 28 64 62 2c 20 70 4d 65 6d  SetInit(db, pMem
19b01 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a 20 20 20 20  ->zMalloc, .    
19b02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19b03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19b04 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c     sqlite3DbMall
19b05 6f 63 53 69 7a 65 28 64 62 2c 20 70 4d 65 6d 2d  ocSize(db, pMem-
19b06 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a 20 20 20 20  >zMalloc));.    
19b07 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 75 2e  assert( pMem->u.
19b08 70 52 6f 77 53 65 74 21 3d 30 20 29 3b 0a 20 20  pRowSet!=0 );.  
19b09 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
19b0a 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a 20 20 7d 0a  MEM_RowSet;.  }.
19b0b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
19b0c 74 72 75 65 20 69 66 20 74 68 65 20 4d 65 6d 20  true if the Mem 
19b0d 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 20  object contains 
19b0e 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 74  a TEXT or BLOB t
19b0f 68 61 74 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61  hat is.** too la
19b10 72 67 65 20 2d 20 77 68 6f 73 65 20 73 69 7a 65  rge - whose size
19b11 20 65 78 63 65 65 64 73 20 53 51 4c 49 54 45 5f   exceeds SQLITE_
19b12 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53  MAX_LENGTH..*/.S
19b13 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
19b14 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
19b15 54 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 29 7b 0a  TooBig(Mem *p){.
19b16 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21    assert( p->db!
19b17 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66  =0 );.  if( p->f
19b18 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c  lags & (MEM_Str|
19b19 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20  MEM_Blob) ){.   
19b1a 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20   int n = p->n;. 
19b1b 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20     if( p->flags 
19b1c 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
19b1d 20 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a      n += p->u.nZ
19b1e 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ero;.    }.    r
19b1f 65 74 75 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61  eturn n>p->db->a
19b20 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
19b21 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a  IT_LENGTH];.  }.
19b22 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a    return 0; .}..
19b23 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 73 74  /*.** Size of st
19b24 72 75 63 74 20 4d 65 6d 20 6e 6f 74 20 69 6e 63  ruct Mem not inc
19b25 6c 75 64 69 6e 67 20 74 68 65 20 4d 65 6d 2e 7a  luding the Mem.z
19b26 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a  Malloc member..*
19b27 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 43 45 4c  /.#define MEMCEL
19b28 4c 53 49 5a 45 20 28 73 69 7a 65 5f 74 29 28 26  LSIZE (size_t)(&
19b29 28 28 28 4d 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61  (((Mem *)0)->zMa
19b2a 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61  lloc))../*.** Ma
19b2b 6b 65 20 61 6e 20 73 68 61 6c 6c 6f 77 20 63 6f  ke an shallow co
19b2c 70 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f  py of pFrom into
19b2d 20 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e   pTo.  Prior con
19b2e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 70 54 6f 20  tents of.** pTo 
19b2f 61 72 65 20 66 72 65 65 64 2e 20 20 54 68 65 20  are freed.  The 
19b30 70 46 72 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69  pFrom->z field i
19b31 73 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64  s not duplicated
19b32 2e 20 20 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e  .  If.** pFrom->
19b33 7a 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20  z is used, then 
19b34 70 54 6f 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f  pTo->z points to
19b35 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20   the same thing 
19b36 61 73 20 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61  as pFrom->z.** a
19b37 6e 64 20 66 6c 61 67 73 20 67 65 74 73 20 73 72  nd flags gets sr
19b38 63 54 79 70 65 20 28 65 69 74 68 65 72 20 4d 45  cType (either ME
19b39 4d 5f 45 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53  M_Ephem or MEM_S
19b3a 74 61 74 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54  tatic)..*/.SQLIT
19b3b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19b3c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
19b3d 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54  llowCopy(Mem *pT
19b3e 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46  o, const Mem *pF
19b3f 72 6f 6d 2c 20 69 6e 74 20 73 72 63 54 79 70 65  rom, int srcType
19b40 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 70 46  ){.  assert( (pF
19b41 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  rom->flags & MEM
19b42 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
19b43 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19b44 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70  eleaseExternal(p
19b45 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54  To);.  memcpy(pT
19b46 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c  o, pFrom, MEMCEL
19b47 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 78  LSIZE);.  pTo->x
19b48 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 28  Del = 0;.  if( (
19b49 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  pFrom->flags&MEM
19b4a 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 70 46 72 6f  _Dyn)!=0 || pFro
19b4b 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61  m->z==pFrom->zMa
19b4c 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 54 6f 2d  lloc ){.    pTo-
19b4d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
19b4e 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d  Dyn|MEM_Static|M
19b4f 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 61  EM_Ephem);.    a
19b50 73 73 65 72 74 28 20 73 72 63 54 79 70 65 3d 3d  ssert( srcType==
19b51 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72 63  MEM_Ephem || src
19b52 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 63  Type==MEM_Static
19b53 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61   );.    pTo->fla
19b54 67 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a 20  gs |= srcType;. 
19b55 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65   }.}../*.** Make
19b56 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66 20   a full copy of 
19b57 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20  pFrom into pTo. 
19b58 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20   Prior contents 
19b59 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66 72  of pTo are.** fr
19b5a 65 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 63  eed before the c
19b5b 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a  opy is made..*/.
19b5c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19b5d 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
19b5e 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20  mCopy(Mem *pTo, 
19b5f 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d  const Mem *pFrom
19b60 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
19b61 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65  LITE_OK;..  asse
19b62 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67  rt( (pFrom->flag
19b63 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
19b64 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  =0 );.  sqlite3V
19b65 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74  dbeMemReleaseExt
19b66 65 72 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65  ernal(pTo);.  me
19b67 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c  mcpy(pTo, pFrom,
19b68 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20   MEMCELLSIZE);. 
19b69 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e   pTo->flags &= ~
19b6a 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69 66 28 20  MEM_Dyn;..  if( 
19b6b 70 54 6f 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f  pTo->flags&(MEM_
19b6c 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b  Str|MEM_Blob) ){
19b6d 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 46 72  .    if( 0==(pFr
19b6e 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74  om->flags&MEM_St
19b6f 61 74 69 63 29 20 29 7b 0a 20 20 20 20 20 20 70  atic) ){.      p
19b70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  To->flags |= MEM
19b71 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 72 63  _Ephem;.      rc
19b72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
19b73 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70  mMakeWriteable(p
19b74 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  To);.    }.  }..
19b75 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
19b76 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74  /*.** Transfer t
19b77 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70  he contents of p
19b78 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 20 41 6e 79  From to pTo. Any
19b79 20 65 78 69 73 74 69 6e 67 20 76 61 6c 75 65 20   existing value 
19b7a 69 6e 20 70 54 6f 20 69 73 0a 2a 2a 20 66 72 65  in pTo is.** fre
19b7b 65 64 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f 6e  ed. If pFrom con
19b7c 74 61 69 6e 73 20 65 70 68 65 6d 65 72 61 6c 20  tains ephemeral 
19b7d 64 61 74 61 2c 20 61 20 63 6f 70 79 20 69 73 20  data, a copy is 
19b7e 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f  made..**.** pFro
19b7f 6d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51  m contains an SQ
19b80 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 74 68 69 73  L NULL when this
19b81 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
19b82 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19b83 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
19b84 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20  VdbeMemMove(Mem 
19b85 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d  *pTo, Mem *pFrom
19b86 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72  ){.  assert( pFr
19b87 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  om->db==0 || sql
19b88 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19b89 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  pFrom->db->mutex
19b8a 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
19b8b 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  To->db==0 || sql
19b8c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19b8d 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  pTo->db->mutex) 
19b8e 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72  );.  assert( pFr
19b8f 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 54 6f  om->db==0 || pTo
19b90 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d  ->db==0 || pFrom
19b91 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b  ->db==pTo->db );
19b92 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ..  sqlite3VdbeM
19b93 65 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a  emRelease(pTo);.
19b94 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46    memcpy(pTo, pF
19b95 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29  rom, sizeof(Mem)
19b96 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66 6c 61 67  );.  pFrom->flag
19b97 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
19b98 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b  pFrom->xDel = 0;
19b99 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f  .  pFrom->zMallo
19b9a 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  c = 0;.}../*.** 
19b9b 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
19b9c 20 6f 66 20 61 20 4d 65 6d 20 74 6f 20 62 65 20   of a Mem to be 
19b9d 61 20 73 74 72 69 6e 67 20 6f 72 20 61 20 42 4c  a string or a BL
19b9e 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65  OB..**.** The me
19b9f 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20  mory management 
19ba0 73 74 72 61 74 65 67 79 20 64 65 70 65 6e 64 73  strategy depends
19ba1 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66   on the value of
19ba2 20 74 68 65 20 78 44 65 6c 0a 2a 2a 20 70 61 72   the xDel.** par
19ba3 61 6d 65 74 65 72 2e 20 49 66 20 74 68 65 20 76  ameter. If the v
19ba4 61 6c 75 65 20 70 61 73 73 65 64 20 69 73 20 53  alue passed is S
19ba5 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c  QLITE_TRANSIENT,
19ba6 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74   then the .** st
19ba7 72 69 6e 67 20 69 73 20 63 6f 70 69 65 64 20 69  ring is copied i
19ba8 6e 74 6f 20 61 20 28 70 6f 73 73 69 62 6c 79 20  nto a (possibly 
19ba9 65 78 69 73 74 69 6e 67 29 20 62 75 66 66 65 72  existing) buffer
19baa 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20   managed by the 
19bab 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 63 74 75 72  .** Mem structur
19bac 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e  e. Otherwise, an
19bad 79 20 65 78 69 73 74 69 6e 67 20 62 75 66 66 65  y existing buffe
19bae 72 20 69 73 20 66 72 65 65 64 20 61 6e 64 20 74  r is freed and t
19baf 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 63 6f  he.** pointer co
19bb0 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  pied..**.** If t
19bb1 68 65 20 73 74 72 69 6e 67 20 69 73 20 74 6f 6f  he string is too
19bb2 20 6c 61 72 67 65 20 28 69 66 20 69 74 20 65 78   large (if it ex
19bb3 63 65 65 64 73 20 74 68 65 20 53 51 4c 49 54 45  ceeds the SQLITE
19bb4 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 0a 2a 2a  _LIMIT_LENGTH.**
19bb5 20 73 69 7a 65 20 6c 69 6d 69 74 29 20 74 68 65   size limit) the
19bb6 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  n no memory allo
19bb7 63 61 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20  cation occurs.  
19bb8 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 63 61  If the string ca
19bb9 6e 20 62 65 0a 2a 2a 20 73 74 6f 72 65 64 20 77  n be.** stored w
19bba 69 74 68 6f 75 74 20 61 6c 6c 6f 63 61 74 69 6e  ithout allocatin
19bbb 67 20 6d 65 6d 6f 72 79 2c 20 74 68 65 6e 20 69  g memory, then i
19bbc 74 20 69 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f  t is.  If a memo
19bbd 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  ry allocation.**
19bbe 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20   is required to 
19bbf 73 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67  store the string
19bc0 2c 20 74 68 65 6e 20 76 61 6c 75 65 20 6f 66 20  , then value of 
19bc1 70 4d 65 6d 20 69 73 20 75 6e 63 68 61 6e 67 65  pMem is unchange
19bc2 64 2e 20 20 49 6e 0a 2a 2a 20 65 69 74 68 65 72  d.  In.** either
19bc3 20 63 61 73 65 2c 20 53 51 4c 49 54 45 5f 54 4f   case, SQLITE_TO
19bc4 4f 42 49 47 20 69 73 20 72 65 74 75 72 6e 65 64  OBIG is returned
19bc5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19bc6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
19bc7 64 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 20 20  dbeMemSetStr(.  
19bc8 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 20 20  Mem *pMem,      
19bc9 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65      /* Memory ce
19bca 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 74 72  ll to set to str
19bcb 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63  ing value */.  c
19bcc 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 20  onst char *z,   
19bcd 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f 69     /* String poi
19bce 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c  nter */.  int n,
19bcf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19bd0 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67   Bytes in string
19bd1 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f  , or negative */
19bd2 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20  .  u8 enc,      
19bd3 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69         /* Encodi
19bd4 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20  ng of z.  0 for 
19bd5 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20  BLOBs */.  void 
19bd6 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 2f  (*xDel)(void*) /
19bd7 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 6e  * Destructor fun
19bd8 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ction */.){.  in
19bd9 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20 20  t nByte = n;    
19bda 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 66    /* New value f
19bdb 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a 20 20  or pMem->n */.  
19bdc 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20 20  int iLimit;     
19bdd 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61      /* Maximum a
19bde 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f 72  llowed string or
19bdf 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a 20 20   blob size */.  
19be0 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20 20  u16 flags = 0;  
19be1 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65      /* New value
19be2 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   for pMem->flags
19be3 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
19be4 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
19be5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
19be6 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
19be7 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ) );.  assert( (
19be8 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
19be9 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a  M_RowSet)==0 );.
19bea 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 20  .  /* If z is a 
19beb 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65  NULL pointer, se
19bec 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69  t pMem to contai
19bed 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a  n an SQL NULL. *
19bee 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20  /.  if( !z ){.  
19bef 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19bf0 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20  SetNull(pMem);. 
19bf1 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
19bf2 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  _OK;.  }..  if( 
19bf3 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20  pMem->db ){.    
19bf4 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64  iLimit = pMem->d
19bf5 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
19bf6 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a  _LIMIT_LENGTH];.
19bf7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69    }else{.    iLi
19bf8 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58  mit = SQLITE_MAX
19bf9 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66  _LENGTH;.  }.  f
19bfa 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d  lags = (enc==0?M
19bfb 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29  EM_Blob:MEM_Str)
19bfc 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20  ;.  if( nByte<0 
19bfd 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65  ){.    assert( e
19bfe 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  nc!=0 );.    if(
19bff 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46   enc==SQLITE_UTF
19c00 38 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e  8 ){.      for(n
19c01 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69  Byte=0; nByte<=i
19c02 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 65  Limit && z[nByte
19c03 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20  ]; nByte++){}.  
19c04 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66    }else{.      f
19c05 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74  or(nByte=0; nByt
19c06 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b  e<=iLimit && (z[
19c07 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65  nByte] | z[nByte
19c08 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b  +1]); nByte+=2){
19c09 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 67  }.    }.    flag
19c0a 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20  s |= MEM_Term;. 
19c0b 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c   }..  /* The fol
19c0c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74  lowing block set
19c0d 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73  s the new values
19c0e 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65   of Mem.z and Me
19c0f 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20  m.xDel. It.  ** 
19c10 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67  also sets a flag
19c11 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   in local variab
19c12 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69 6e  le "flags" to in
19c13 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f 72  dicate the memor
19c14 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e  y.  ** managemen
19c15 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79  t (one of MEM_Dy
19c16 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29  n or MEM_Static)
19c17 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 65  ..  */.  if( xDe
19c18 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  l==SQLITE_TRANSI
19c19 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  ENT ){.    int n
19c1a 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20  Alloc = nByte;. 
19c1b 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d     if( flags&MEM
19c1c 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e  _Term ){.      n
19c1d 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53  Alloc += (enc==S
19c1e 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b  QLITE_UTF8?1:2);
19c1f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e  .    }.    if( n
19c20 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20  Byte>iLimit ){. 
19c21 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
19c22 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d  TE_TOOBIG;.    }
19c23 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
19c24 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
19c25 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a  , nAlloc, 0) ){.
19c26 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
19c27 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
19c28 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d  .    memcpy(pMem
19c29 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b  ->z, z, nAlloc);
19c2a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65  .  }else if( xDe
19c2b 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  l==SQLITE_DYNAMI
19c2c 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  C ){.    sqlite3
19c2d 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
19c2e 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  Mem);.    pMem->
19c2f 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e  zMalloc = pMem->
19c30 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20  z = (char *)z;. 
19c31 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20     pMem->xDel = 
19c32 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
19c33 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
19c34 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20  lease(pMem);.   
19c35 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72   pMem->z = (char
19c36 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e   *)z;.    pMem->
19c37 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20  xDel = xDel;.   
19c38 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 6c   flags |= ((xDel
19c39 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  ==SQLITE_STATIC)
19c3a 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f  ?MEM_Static:MEM_
19c3b 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65  Dyn);.  }..  pMe
19c3c 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a 20 20  m->n = nByte;.  
19c3d 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 66 6c  pMem->flags = fl
19c3e 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63  ags;.  pMem->enc
19c3f 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c   = (enc==0 ? SQL
19c40 49 54 45 5f 55 54 46 38 20 3a 20 65 6e 63 29 3b  ITE_UTF8 : enc);
19c41 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20  .  pMem->type = 
19c42 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c 49 54 45  (enc==0 ? SQLITE
19c43 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 45 5f 54  _BLOB : SQLITE_T
19c44 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  EXT);..#ifndef S
19c45 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
19c46 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63  .  if( pMem->enc
19c47 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26  !=SQLITE_UTF8 &&
19c48 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48   sqlite3VdbeMemH
19c49 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d 29 20 29  andleBom(pMem) )
19c4a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
19c4b 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23  ITE_NOMEM;.  }.#
19c4c 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 42 79  endif..  if( nBy
19c4d 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  te>iLimit ){.   
19c4e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 54   return SQLITE_T
19c4f 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20 72 65  OOBIG;.  }..  re
19c50 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
19c51 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
19c52 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74   the values cont
19c53 61 69 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f  ained by the two
19c54 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72   memory cells, r
19c55 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61  eturning.** nega
19c56 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f  tive, zero or po
19c57 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20  sitive if pMem1 
19c58 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71  is less than, eq
19c59 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
19c5a 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32  er.** than pMem2
19c5b 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20  . Sorting order 
19c5c 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c  is NULL's first,
19c5d 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d   followed by num
19c5e 62 65 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a  bers (integers.*
19c5f 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72  * and reals) sor
19c60 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c  ted numerically,
19c61 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78   followed by tex
19c62 74 20 6f 72 64 65 72 65 64 20 62 79 20 74 68 65  t ordered by the
19c63 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65   collating.** se
19c64 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64  quence pColl and
19c65 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20   finally blob's 
19c66 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d  ordered by memcm
19c67 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e  p()..**.** Two N
19c68 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63  ULL values are c
19c69 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61 6c 20  onsidered equal 
19c6a 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  by this function
19c6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19c6c 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
19c6d 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20  emCompare(const 
19c6e 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73  Mem *pMem1, cons
19c6f 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f  t Mem *pMem2, co
19c70 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  nst CollSeq *pCo
19c71 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ll){.  int rc;. 
19c72 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69   int f1, f2;.  i
19c73 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  nt combined_flag
19c74 73 3b 0a 0a 20 20 66 31 20 3d 20 70 4d 65 6d 31  s;..  f1 = pMem1
19c75 2d 3e 66 6c 61 67 73 3b 0a 20 20 66 32 20 3d 20  ->flags;.  f2 = 
19c76 70 4d 65 6d 32 2d 3e 66 6c 61 67 73 3b 0a 20 20  pMem2->flags;.  
19c77 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 3d  combined_flags =
19c78 20 66 31 7c 66 32 3b 0a 20 20 61 73 73 65 72 74   f1|f2;.  assert
19c79 28 20 28 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  ( (combined_flag
19c7a 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
19c7b 3d 30 20 29 3b 0a 20 0a 20 20 2f 2a 20 49 66 20  =0 );. .  /* If 
19c7c 6f 6e 65 20 76 61 6c 75 65 20 69 73 20 4e 55 4c  one value is NUL
19c7d 4c 2c 20 69 74 20 69 73 20 6c 65 73 73 20 74 68  L, it is less th
19c7e 61 6e 20 74 68 65 20 6f 74 68 65 72 2e 20 49 66  an the other. If
19c7f 20 62 6f 74 68 20 76 61 6c 75 65 73 0a 20 20 2a   both values.  *
19c80 2a 20 61 72 65 20 4e 55 4c 4c 2c 20 72 65 74 75  * are NULL, retu
19c81 72 6e 20 30 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rn 0..  */.  if(
19c82 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26   combined_flags&
19c83 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
19c84 72 65 74 75 72 6e 20 28 66 32 26 4d 45 4d 5f 4e  return (f2&MEM_N
19c85 75 6c 6c 29 20 2d 20 28 66 31 26 4d 45 4d 5f 4e  ull) - (f1&MEM_N
19c86 75 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ull);.  }..  /* 
19c87 49 66 20 6f 6e 65 20 76 61 6c 75 65 20 69 73 20  If one value is 
19c88 61 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65  a number and the
19c89 20 6f 74 68 65 72 20 69 73 20 6e 6f 74 2c 20 74   other is not, t
19c8a 68 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73  he number is les
19c8b 73 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20  s..  ** If both 
19c8c 61 72 65 20 6e 75 6d 62 65 72 73 2c 20 63 6f 6d  are numbers, com
19c8d 70 61 72 65 20 61 73 20 72 65 61 6c 73 20 69 66  pare as reals if
19c8e 20 6f 6e 65 20 69 73 20 61 20 72 65 61 6c 2c 20   one is a real, 
19c8f 6f 72 20 61 73 20 69 6e 74 65 67 65 72 73 0a 20  or as integers. 
19c90 20 2a 2a 20 69 66 20 62 6f 74 68 20 76 61 6c 75   ** if both valu
19c91 65 73 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e  es are integers.
19c92 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62  .  */.  if( comb
19c93 69 6e 65 64 5f 66 6c 61 67 73 26 28 4d 45 4d 5f  ined_flags&(MEM_
19c94 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 29 7b  Int|MEM_Real) ){
19c95 0a 20 20 20 20 69 66 28 20 21 28 66 31 26 28 4d  .    if( !(f1&(M
19c96 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29  EM_Int|MEM_Real)
19c97 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
19c98 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
19c99 66 28 20 21 28 66 32 26 28 4d 45 4d 5f 49 6e 74  f( !(f2&(MEM_Int
19c9a 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20  |MEM_Real)) ){. 
19c9b 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a       return -1;.
19c9c 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66      }.    if( (f
19c9d 31 20 26 20 66 32 20 26 20 4d 45 4d 5f 49 6e 74  1 & f2 & MEM_Int
19c9e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f  )==0 ){.      do
19c9f 75 62 6c 65 20 72 31 2c 20 72 32 3b 0a 20 20 20  uble r1, r2;.   
19ca0 20 20 20 69 66 28 20 28 66 31 26 4d 45 4d 5f 52     if( (f1&MEM_R
19ca1 65 61 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  eal)==0 ){.     
19ca2 20 20 20 72 31 20 3d 20 28 64 6f 75 62 6c 65 29     r1 = (double)
19ca3 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20 20  pMem1->u.i;.    
19ca4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
19ca5 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b 0a   r1 = pMem1->r;.
19ca6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
19ca7 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c 29 3d  ( (f2&MEM_Real)=
19ca8 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 32  =0 ){.        r2
19ca9 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 32   = (double)pMem2
19caa 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c  ->u.i;.      }el
19cab 73 65 7b 0a 20 20 20 20 20 20 20 20 72 32 20 3d  se{.        r2 =
19cac 20 70 4d 65 6d 32 2d 3e 72 3b 0a 20 20 20 20 20   pMem2->r;.     
19cad 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 31 3c   }.      if( r1<
19cae 72 32 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a  r2 ) return -1;.
19caf 20 20 20 20 20 20 69 66 28 20 72 31 3e 72 32 20        if( r1>r2 
19cb0 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
19cb1 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
19cb2 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
19cb3 65 72 74 28 20 66 31 26 4d 45 4d 5f 49 6e 74 20  ert( f1&MEM_Int 
19cb4 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
19cb5 20 66 32 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20   f2&MEM_Int );. 
19cb6 20 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e       if( pMem1->
19cb7 75 2e 69 20 3c 20 70 4d 65 6d 32 2d 3e 75 2e 69  u.i < pMem2->u.i
19cb8 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20   ) return -1;.  
19cb9 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 75      if( pMem1->u
19cba 2e 69 20 3e 20 70 4d 65 6d 32 2d 3e 75 2e 69 20  .i > pMem2->u.i 
19cbb 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20  ) return 1;.    
19cbc 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
19cbd 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f  }.  }..  /* If o
19cbe 6e 65 20 76 61 6c 75 65 20 69 73 20 61 20 73 74  ne value is a st
19cbf 72 69 6e 67 20 61 6e 64 20 74 68 65 20 6f 74 68  ring and the oth
19cc0 65 72 20 69 73 20 61 20 62 6c 6f 62 2c 20 74 68  er is a blob, th
19cc1 65 20 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73  e string is less
19cc2 2e 0a 20 20 2a 2a 20 49 66 20 62 6f 74 68 20 61  ..  ** If both a
19cc3 72 65 20 73 74 72 69 6e 67 73 2c 20 63 6f 6d 70  re strings, comp
19cc4 61 72 65 20 75 73 69 6e 67 20 74 68 65 20 63 6f  are using the co
19cc5 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
19cc6 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f  s..  */.  if( co
19cc7 6d 62 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d  mbined_flags&MEM
19cc8 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 66 28 20  _Str ){.    if( 
19cc9 28 66 31 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d  (f1 & MEM_Str)==
19cca 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
19ccb 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
19ccc 66 28 20 28 66 32 20 26 20 4d 45 4d 5f 53 74 72  f( (f2 & MEM_Str
19ccd 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  )==0 ){.      re
19cce 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a  turn -1;.    }..
19ccf 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
19cd0 31 2d 3e 65 6e 63 3d 3d 70 4d 65 6d 32 2d 3e 65  1->enc==pMem2->e
19cd1 6e 63 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  nc );.    assert
19cd2 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51  ( pMem1->enc==SQ
19cd3 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20 0a 20 20  LITE_UTF8 || .  
19cd4 20 20 20 20 20 20 20 20 20 20 70 4d 65 6d 31 2d            pMem1-
19cd5 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  >enc==SQLITE_UTF
19cd6 31 36 4c 45 20 7c 7c 20 70 4d 65 6d 31 2d 3e 65  16LE || pMem1->e
19cd7 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
19cd8 42 45 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68  BE );..    /* Th
19cd9 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
19cda 65 6e 63 65 20 6d 75 73 74 20 62 65 20 64 65 66  ence must be def
19cdb 69 6e 65 64 20 61 74 20 74 68 69 73 20 70 6f 69  ined at this poi
19cdc 6e 74 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20  nt, even if.    
19cdd 2a 2a 20 74 68 65 20 75 73 65 72 20 64 65 6c 65  ** the user dele
19cde 74 65 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  tes the collatio
19cdf 6e 20 73 65 71 75 65 6e 63 65 20 61 66 74 65 72  n sequence after
19ce0 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61   the vdbe progra
19ce1 6d 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f 6d 70  m is.    ** comp
19ce2 69 6c 65 64 20 28 74 68 69 73 20 77 61 73 20 6e  iled (this was n
19ce3 6f 74 20 61 6c 77 61 79 73 20 74 68 65 20 63 61  ot always the ca
19ce4 73 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  se)..    */.    
19ce5 61 73 73 65 72 74 28 20 21 70 43 6f 6c 6c 20 7c  assert( !pColl |
19ce6 7c 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b  | pColl->xCmp );
19ce7 0a 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20  ..    if( pColl 
19ce8 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4d 65  ){.      if( pMe
19ce9 6d 31 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e  m1->enc==pColl->
19cea 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  enc ){.        /
19ceb 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 61 72  * The strings ar
19cec 65 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  e already in the
19ced 20 63 6f 72 72 65 63 74 20 65 6e 63 6f 64 69 6e   correct encodin
19cee 67 2e 20 20 43 61 6c 6c 20 74 68 65 0a 20 20 20  g.  Call the.   
19cef 20 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73       ** comparis
19cf0 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 64 69 72 65  on function dire
19cf1 63 74 6c 79 20 2a 2f 0a 20 20 20 20 20 20 20 20  ctly */.        
19cf2 72 65 74 75 72 6e 20 70 43 6f 6c 6c 2d 3e 78 43  return pColl->xC
19cf3 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c  mp(pColl->pUser,
19cf4 70 4d 65 6d 31 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e  pMem1->n,pMem1->
19cf5 7a 2c 70 4d 65 6d 32 2d 3e 6e 2c 70 4d 65 6d 32  z,pMem2->n,pMem2
19cf6 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ->z);.      }els
19cf7 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  e{.        const
19cf8 20 76 6f 69 64 20 2a 76 31 2c 20 2a 76 32 3b 0a   void *v1, *v2;.
19cf9 20 20 20 20 20 20 20 20 69 6e 74 20 6e 31 2c 20          int n1, 
19cfa 6e 32 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20  n2;.        Mem 
19cfb 63 31 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20  c1;.        Mem 
19cfc 63 32 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73  c2;.        mems
19cfd 65 74 28 26 63 31 2c 20 30 2c 20 73 69 7a 65 6f  et(&c1, 0, sizeo
19cfe 66 28 63 31 29 29 3b 0a 20 20 20 20 20 20 20 20  f(c1));.        
19cff 6d 65 6d 73 65 74 28 26 63 32 2c 20 30 2c 20 73  memset(&c2, 0, s
19d00 69 7a 65 6f 66 28 63 32 29 29 3b 0a 20 20 20 20  izeof(c2));.    
19d01 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19d02 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 26 63  emShallowCopy(&c
19d03 31 2c 20 70 4d 65 6d 31 2c 20 4d 45 4d 5f 45 70  1, pMem1, MEM_Ep
19d04 68 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71  hem);.        sq
19d05 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c  lite3VdbeMemShal
19d06 6c 6f 77 43 6f 70 79 28 26 63 32 2c 20 70 4d 65  lowCopy(&c2, pMe
19d07 6d 32 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a  m2, MEM_Ephem);.
19d08 20 20 20 20 20 20 20 20 76 31 20 3d 20 73 71 6c          v1 = sql
19d09 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73  ite3ValueText((s
19d0a 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63  qlite3_value*)&c
19d0b 31 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a  1, pColl->enc);.
19d0c 20 20 20 20 20 20 20 20 6e 31 20 3d 20 76 31 3d          n1 = v1=
19d0d 3d 30 20 3f 20 30 20 3a 20 63 31 2e 6e 3b 0a 20  =0 ? 0 : c1.n;. 
19d0e 20 20 20 20 20 20 20 76 32 20 3d 20 73 71 6c 69         v2 = sqli
19d0f 74 65 33 56 61 6c 75 65 54 65 78 74 28 28 73 71  te3ValueText((sq
19d10 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 26 63 32  lite3_value*)&c2
19d11 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20  , pColl->enc);. 
19d12 20 20 20 20 20 20 20 6e 32 20 3d 20 76 32 3d 3d         n2 = v2==
19d13 30 20 3f 20 30 20 3a 20 63 32 2e 6e 3b 0a 20 20  0 ? 0 : c2.n;.  
19d14 20 20 20 20 20 20 72 63 20 3d 20 70 43 6f 6c 6c        rc = pColl
19d15 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55  ->xCmp(pColl->pU
19d16 73 65 72 2c 20 6e 31 2c 20 76 31 2c 20 6e 32 2c  ser, n1, v1, n2,
19d17 20 76 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71   v2);.        sq
19d18 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19d19 61 73 65 28 26 63 31 29 3b 0a 20 20 20 20 20 20  ase(&c1);.      
19d1a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19d1b 52 65 6c 65 61 73 65 28 26 63 32 29 3b 0a 20 20  Release(&c2);.  
19d1c 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
19d1d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
19d1e 20 20 20 2f 2a 20 49 66 20 61 20 4e 55 4c 4c 20     /* If a NULL 
19d1f 70 6f 69 6e 74 65 72 20 77 61 73 20 70 61 73 73  pointer was pass
19d20 65 64 20 61 73 20 74 68 65 20 63 6f 6c 6c 61 74  ed as the collat
19d21 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 61 6c 6c  e function, fall
19d22 20 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20   through.    ** 
19d23 74 6f 20 74 68 65 20 62 6c 6f 62 20 63 61 73 65  to the blob case
19d24 20 61 6e 64 20 75 73 65 20 6d 65 6d 63 6d 70 28   and use memcmp(
19d25 29 2e 20 20 2a 2f 0a 20 20 7d 0a 20 0a 20 20 2f  ).  */.  }. .  /
19d26 2a 20 42 6f 74 68 20 76 61 6c 75 65 73 20 6d 75  * Both values mu
19d27 73 74 20 62 65 20 62 6c 6f 62 73 2e 20 20 43 6f  st be blobs.  Co
19d28 6d 70 61 72 65 20 75 73 69 6e 67 20 6d 65 6d 63  mpare using memc
19d29 6d 70 28 29 2e 20 20 2a 2f 0a 20 20 72 63 20 3d  mp().  */.  rc =
19d2a 20 6d 65 6d 63 6d 70 28 70 4d 65 6d 31 2d 3e 7a   memcmp(pMem1->z
19d2b 2c 20 70 4d 65 6d 32 2d 3e 7a 2c 20 28 70 4d 65  , pMem2->z, (pMe
19d2c 6d 31 2d 3e 6e 3e 70 4d 65 6d 32 2d 3e 6e 29 3f  m1->n>pMem2->n)?
19d2d 70 4d 65 6d 32 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e  pMem2->n:pMem1->
19d2e 6e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20  n);.  if( rc==0 
19d2f 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 4d 65 6d  ){.    rc = pMem
19d30 31 2d 3e 6e 20 2d 20 70 4d 65 6d 32 2d 3e 6e 3b  1->n - pMem2->n;
19d31 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
19d32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  ;.}../*.** Move 
19d33 64 61 74 61 20 6f 75 74 20 6f 66 20 61 20 62 74  data out of a bt
19d34 72 65 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20  ree key or data 
19d35 66 69 65 6c 64 20 61 6e 64 20 69 6e 74 6f 20 61  field and into a
19d36 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e 0a   Mem structure..
19d37 2a 2a 20 54 68 65 20 64 61 74 61 20 6f 72 20 6b  ** The data or k
19d38 65 79 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d  ey is taken from
19d39 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20   the entry that 
19d3a 70 43 75 72 20 69 73 20 63 75 72 72 65 6e 74 6c  pCur is currentl
19d3b 79 20 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f  y pointing.** to
19d3c 2e 20 20 6f 66 66 73 65 74 20 61 6e 64 20 61 6d  .  offset and am
19d3d 74 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74  t determine what
19d3e 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20   portion of the 
19d3f 64 61 74 61 20 6f 72 20 6b 65 79 20 74 6f 20 72  data or key to r
19d40 65 74 72 69 65 76 65 2e 0a 2a 2a 20 6b 65 79 20  etrieve..** key 
19d41 69 73 20 74 72 75 65 20 74 6f 20 67 65 74 20 74  is true to get t
19d42 68 65 20 6b 65 79 20 6f 72 20 66 61 6c 73 65 20  he key or false 
19d43 74 6f 20 67 65 74 20 64 61 74 61 2e 20 20 54 68  to get data.  Th
19d44 65 20 72 65 73 75 6c 74 20 69 73 20 77 72 69 74  e result is writ
19d45 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20  ten.** into the 
19d46 70 4d 65 6d 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2a  pMem element..**
19d47 0a 2a 2a 20 54 68 65 20 70 4d 65 6d 20 73 74 72  .** The pMem str
19d48 75 63 74 75 72 65 20 69 73 20 61 73 73 75 6d 65  ucture is assume
19d49 64 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69 61  d to be uninitia
19d4a 6c 69 7a 65 64 2e 20 20 41 6e 79 20 70 72 69 6f  lized.  Any prio
19d4b 72 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69 73 20  r content.** is 
19d4c 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68  overwritten with
19d4d 6f 75 74 20 62 65 69 6e 67 20 66 72 65 65 64 2e  out being freed.
19d4e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 72  .**.** If this r
19d4f 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66 6f 72  outine fails for
19d50 20 61 6e 79 20 72 65 61 73 6f 6e 20 28 6d 61 6c   any reason (mal
19d51 6c 6f 63 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  loc returns NULL
19d52 20 6f 72 20 75 6e 61 62 6c 65 0a 2a 2a 20 74 6f   or unable.** to
19d53 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 64   read from the d
19d54 69 73 6b 29 20 74 68 65 6e 20 74 68 65 20 70 4d  isk) then the pM
19d55 65 6d 20 69 73 20 6c 65 66 74 20 69 6e 20 61 6e  em is left in an
19d56 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73 74   inconsistent st
19d57 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ate..*/.SQLITE_P
19d58 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19d59 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72  e3VdbeMemFromBtr
19d5a 65 65 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  ee(.  BtCursor *
19d5b 70 43 75 72 2c 20 20 20 2f 2a 20 43 75 72 73 6f  pCur,   /* Curso
19d5c 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 72 65  r pointing at re
19d5d 63 6f 72 64 20 74 6f 20 72 65 74 72 69 65 76 65  cord to retrieve
19d5e 2e 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65  . */.  int offse
19d5f 74 2c 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73  t,       /* Offs
19d60 65 74 20 66 72 6f 6d 20 74 68 65 20 73 74 61 72  et from the star
19d61 74 20 6f 66 20 64 61 74 61 20 74 6f 20 72 65 74  t of data to ret
19d62 75 72 6e 20 62 79 74 65 73 20 66 72 6f 6d 2e 20  urn bytes from. 
19d63 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20  */.  int amt,   
19d64 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
19d65 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 74   of bytes to ret
19d66 75 72 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 6b 65  urn. */.  int ke
19d67 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  y,          /* I
19d68 66 20 74 72 75 65 2c 20 72 65 74 72 69 65 76 65  f true, retrieve
19d69 20 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20   from the btree 
19d6a 6b 65 79 2c 20 6e 6f 74 20 64 61 74 61 2e 20 2a  key, not data. *
19d6b 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20  /.  Mem *pMem   
19d6c 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65        /* OUT: Re
19d6d 74 75 72 6e 20 64 61 74 61 20 69 6e 20 74 68 69  turn data in thi
19d6e 73 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 2e  s Mem structure.
19d6f 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a   */.){.  char *z
19d70 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a 20  Data;        /* 
19d71 44 61 74 61 20 66 72 6f 6d 20 74 68 65 20 62 74  Data from the bt
19d72 72 65 65 20 6c 61 79 65 72 20 2a 2f 0a 20 20 69  ree layer */.  i
19d73 6e 74 20 61 76 61 69 6c 61 62 6c 65 20 3d 20 30  nt available = 0
19d74 3b 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  ;  /* Number of 
19d75 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20  bytes available 
19d76 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 62 74 72  on the local btr
19d77 65 65 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ee page */.  int
19d78 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
19d79 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
19d7a 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  */..  assert( sq
19d7b 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
19d7c 49 73 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b  IsValid(pCur) );
19d7d 0a 0a 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 65  ..  /* Note: the
19d7e 20 63 61 6c 6c 73 20 74 6f 20 42 74 72 65 65 4b   calls to BtreeK
19d7f 65 79 46 65 74 63 68 28 29 20 61 6e 64 20 44 61  eyFetch() and Da
19d80 74 61 46 65 74 63 68 28 29 20 62 65 6c 6f 77 20  taFetch() below 
19d81 61 73 73 65 72 74 28 29 20 0a 20 20 2a 2a 20 74  assert() .  ** t
19d82 68 61 74 20 62 6f 74 68 20 74 68 65 20 42 74 53  hat both the BtS
19d83 68 61 72 65 64 20 61 6e 64 20 64 61 74 61 62 61  hared and databa
19d84 73 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 65  se handle mutexe
19d85 73 20 61 72 65 20 68 65 6c 64 2e 20 2a 2f 0a 20  s are held. */. 
19d86 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
19d87 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
19d88 65 74 29 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20  et)==0 );.  if( 
19d89 6b 65 79 20 29 7b 0a 20 20 20 20 7a 44 61 74 61  key ){.    zData
19d8a 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
19d8b 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28  e3BtreeKeyFetch(
19d8c 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65  pCur, &available
19d8d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
19d8e 7a 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29  zData = (char *)
19d8f 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
19d90 46 65 74 63 68 28 70 43 75 72 2c 20 26 61 76 61  Fetch(pCur, &ava
19d91 69 6c 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 61  ilable);.  }.  a
19d92 73 73 65 72 74 28 20 7a 44 61 74 61 21 3d 30 20  ssert( zData!=0 
19d93 29 3b 0a 0a 20 20 69 66 28 20 6f 66 66 73 65 74  );..  if( offset
19d94 2b 61 6d 74 3c 3d 61 76 61 69 6c 61 62 6c 65 20  +amt<=available 
19d95 26 26 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  && (pMem->flags&
19d96 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 7b 0a 20  MEM_Dyn)==0 ){. 
19d97 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
19d98 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a  mRelease(pMem);.
19d99 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a      pMem->z = &z
19d9a 44 61 74 61 5b 6f 66 66 73 65 74 5d 3b 0a 20 20  Data[offset];.  
19d9b 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
19d9c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68  MEM_Blob|MEM_Eph
19d9d 65 6d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  em;.  }else if( 
19d9e 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
19d9f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
19da0 72 6f 77 28 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c  row(pMem, amt+2,
19da1 20 30 29 29 20 29 7b 0a 20 20 20 20 70 4d 65 6d   0)) ){.    pMem
19da2 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c  ->flags = MEM_Bl
19da3 6f 62 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54  ob|MEM_Dyn|MEM_T
19da4 65 72 6d 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65  erm;.    pMem->e
19da5 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d  nc = 0;.    pMem
19da6 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
19da7 42 4c 4f 42 3b 0a 20 20 20 20 69 66 28 20 6b 65  BLOB;.    if( ke
19da8 79 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  y ){.      rc = 
19da9 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 28  sqlite3BtreeKey(
19daa 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d  pCur, offset, am
19dab 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20  t, pMem->z);.   
19dac 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
19dad 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44   = sqlite3BtreeD
19dae 61 74 61 28 70 43 75 72 2c 20 6f 66 66 73 65 74  ata(pCur, offset
19daf 2c 20 61 6d 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b  , amt, pMem->z);
19db0 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
19db1 3e 7a 5b 61 6d 74 5d 20 3d 20 30 3b 0a 20 20 20  >z[amt] = 0;.   
19db2 20 70 4d 65 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20   pMem->z[amt+1] 
19db3 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 21  = 0;.    if( rc!
19db4 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
19db5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19db6 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b  emRelease(pMem);
19db7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 4d 65  .    }.  }.  pMe
19db8 6d 2d 3e 6e 20 3d 20 61 6d 74 3b 0a 0a 20 20 72  m->n = amt;..  r
19db9 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
19dba 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
19dbb 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
19dbc 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69  internally, it i
19dbd 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68  s not part of th
19dbe 65 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50  e.** external AP
19dbf 49 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61  I. It works in a
19dc0 20 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20   similar way to 
19dc1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
19dc2 78 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20  xt(),.** except 
19dc3 74 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65  the data returne
19dc4 64 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f  d is in the enco
19dc5 64 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62  ding specified b
19dc6 79 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  y the second.** 
19dc7 70 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68  parameter, which
19dc8 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20   must be one of 
19dc9 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20  SQLITE_UTF16BE, 
19dca 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f  SQLITE_UTF16LE o
19dcb 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38  r.** SQLITE_UTF8
19dcc 2e 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32  ..**.** (2006-02
19dcd 2d 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76  -16:)  The enc v
19dce 61 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65  alue can be or-e
19dcf 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54  d with SQLITE_UT
19dd0 46 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20  F16_ALIGNED..** 
19dd1 49 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63  If that is the c
19dd2 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65  ase, then the re
19dd3 73 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69  sult must be ali
19dd4 67 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20  gned on an even 
19dd5 62 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79  byte.** boundary
19dd6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19dd7 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  ATE const void *
19dd8 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
19dd9 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20  (sqlite3_value* 
19dda 70 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20  pVal, u8 enc){. 
19ddb 20 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74   if( !pVal ) ret
19ddc 75 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74  urn 0;..  assert
19ddd 28 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c  ( pVal->db==0 ||
19dde 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
19ddf 65 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75  eld(pVal->db->mu
19de0 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
19de1 28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26  ( (enc&3)==(enc&
19de2 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  ~SQLITE_UTF16_AL
19de3 49 47 4e 45 44 29 20 29 3b 0a 20 20 61 73 73 65  IGNED) );.  asse
19de4 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73  rt( (pVal->flags
19de5 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
19de6 30 20 29 3b 0a 0a 20 20 69 66 28 20 70 56 61 6c  0 );..  if( pVal
19de7 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c  ->flags&MEM_Null
19de8 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
19de9 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
19dea 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20 3d 3d  (MEM_Blob>>3) ==
19deb 20 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 70 56   MEM_Str );.  pV
19dec 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 56  al->flags |= (pV
19ded 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  al->flags & MEM_
19dee 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20 65 78 70 61  Blob)>>3;.  expa
19def 6e 64 42 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20  ndBlob(pVal);.  
19df0 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61 67 73 26  if( pVal->flags&
19df1 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 73  MEM_Str ){.    s
19df2 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
19df3 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c 20 65  Encoding(pVal, e
19df4 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46  nc & ~SQLITE_UTF
19df5 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a 20 20 20  16_ALIGNED);.   
19df6 20 69 66 28 20 28 65 6e 63 20 26 20 53 51 4c 49   if( (enc & SQLI
19df7 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
19df8 29 21 3d 30 20 26 26 20 31 3d 3d 28 31 26 53 51  )!=0 && 1==(1&SQ
19df9 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28  LITE_PTR_TO_INT(
19dfa 70 56 61 6c 2d 3e 7a 29 29 20 29 7b 0a 20 20 20  pVal->z)) ){.   
19dfb 20 20 20 61 73 73 65 72 74 28 20 28 70 56 61 6c     assert( (pVal
19dfc 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 45  ->flags & (MEM_E
19dfd 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29  phem|MEM_Static)
19dfe 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  )!=0 );.      if
19dff 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  ( sqlite3VdbeMem
19e00 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 56  MakeWriteable(pV
19e01 61 6c 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  al)!=SQLITE_OK )
19e02 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
19e03 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
19e04 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
19e05 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65  eMemNulTerminate
19e06 28 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b  (pVal);.  }else{
19e07 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 56  .    assert( (pV
19e08 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c  al->flags&MEM_Bl
19e09 6f 62 29 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71  ob)==0 );.    sq
19e0a 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69  lite3VdbeMemStri
19e0b 6e 67 69 66 79 28 70 56 61 6c 2c 20 65 6e 63 29  ngify(pVal, enc)
19e0c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 30 3d  ;.    assert( 0=
19e0d 3d 28 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54  =(1&SQLITE_PTR_T
19e0e 4f 5f 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20  O_INT(pVal->z)) 
19e0f 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  );.  }.  assert(
19e10 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20  pVal->enc==(enc 
19e11 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  & ~SQLITE_UTF16_
19e12 41 4c 49 47 4e 45 44 29 20 7c 7c 20 70 56 61 6c  ALIGNED) || pVal
19e13 2d 3e 64 62 3d 3d 30 0a 20 20 20 20 20 20 20 20  ->db==0.        
19e14 20 20 20 20 20 20 7c 7c 20 70 56 61 6c 2d 3e 64        || pVal->d
19e15 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
19e16 29 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 65  );.  if( pVal->e
19e17 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49  nc==(enc & ~SQLI
19e18 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
19e19 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
19e1a 70 56 61 6c 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65  pVal->z;.  }else
19e1b 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
19e1c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65    }.}../*.** Cre
19e1d 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65  ate a new sqlite
19e1e 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2e 0a  3_value object..
19e1f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19e20 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  E sqlite3_value 
19e21 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77  *sqlite3ValueNew
19e22 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
19e23 20 4d 65 6d 20 2a 70 20 3d 20 73 71 6c 69 74 65   Mem *p = sqlite
19e24 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
19e25 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20  , sizeof(*p));. 
19e26 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d   if( p ){.    p-
19e27 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
19e28 6c 3b 0a 20 20 20 20 70 2d 3e 74 79 70 65 20 3d  l;.    p->type =
19e29 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20   SQLITE_NULL;.  
19e2a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20    p->db = db;.  
19e2b 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
19e2c 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
19e2d 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  new sqlite3_valu
19e2e 65 20 6f 62 6a 65 63 74 2c 20 63 6f 6e 74 61 69  e object, contai
19e2f 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f  ning the value o
19e30 66 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20 54  f pExpr..**.** T
19e31 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66  his only works f
19e32 6f 72 20 76 65 72 79 20 73 69 6d 70 6c 65 20 65  or very simple e
19e33 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20  xpressions that 
19e34 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 63  consist of one c
19e35 6f 6e 73 74 61 6e 74 0a 2a 2a 20 74 6f 6b 65 6e  onstant.** token
19e36 20 28 69 2e 65 2e 20 22 35 22 2c 20 22 35 2e 31   (i.e. "5", "5.1
19e37 22 2c 20 22 27 61 20 73 74 72 69 6e 67 27 22 29  ", "'a string'")
19e38 2e 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  . If the express
19e39 69 6f 6e 20 63 61 6e 0a 2a 2a 20 62 65 20 63 6f  ion can.** be co
19e3a 6e 76 65 72 74 65 64 20 64 69 72 65 63 74 6c 79  nverted directly
19e3b 20 69 6e 74 6f 20 61 20 76 61 6c 75 65 2c 20 74   into a value, t
19e3c 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73  hen the value is
19e3d 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 0a 2a   allocated and.*
19e3e 2a 20 61 20 70 6f 69 6e 74 65 72 20 77 72 69 74  * a pointer writ
19e3f 74 65 6e 20 74 6f 20 2a 70 70 56 61 6c 2e 20 54  ten to *ppVal. T
19e40 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65 73  he caller is res
19e41 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61  ponsible for dea
19e42 6c 6c 6f 63 61 74 69 6e 67 0a 2a 2a 20 74 68 65  llocating.** the
19e43 20 76 61 6c 75 65 20 62 79 20 70 61 73 73 69 6e   value by passin
19e44 67 20 69 74 20 74 6f 20 73 71 6c 69 74 65 33 56  g it to sqlite3V
19e45 61 6c 75 65 46 72 65 65 28 29 20 6c 61 74 65 72  alueFree() later
19e46 20 6f 6e 2e 20 49 66 20 74 68 65 20 65 78 70 72   on. If the expr
19e47 65 73 73 69 6f 6e 0a 2a 2a 20 63 61 6e 6e 6f 74  ession.** cannot
19e48 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f   be converted to
19e49 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a   a value, then *
19e4a 70 70 56 61 6c 20 69 73 20 73 65 74 20 74 6f 20  ppVal is set to 
19e4b 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NULL..*/.SQLITE_
19e4c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
19e4d 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
19e4e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
19e4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19e50 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
19e51 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  nnection */.  Ex
19e52 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20  pr *pExpr,      
19e53 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
19e54 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 65 76 61  xpression to eva
19e55 6c 75 61 74 65 20 2a 2f 0a 20 20 75 38 20 65 6e  luate */.  u8 en
19e56 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
19e57 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67       /* Encoding
19e58 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 75 38 20   to use */.  u8 
19e59 61 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 20  affinity,       
19e5a 20 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e 69         /* Affini
19e5b 74 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 73  ty to use */.  s
19e5c 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70  qlite3_value **p
19e5d 70 56 61 6c 20 20 20 20 20 2f 2a 20 57 72 69 74  pVal     /* Writ
19e5e 65 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 20  e the new value 
19e5f 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
19e60 20 6f 70 3b 0a 20 20 63 68 61 72 20 2a 7a 56 61   op;.  char *zVa
19e61 6c 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  l = 0;.  sqlite3
19e62 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20 30  _value *pVal = 0
19e63 3b 0a 0a 20 20 69 66 28 20 21 70 45 78 70 72 20  ;..  if( !pExpr 
19e64 29 7b 0a 20 20 20 20 2a 70 70 56 61 6c 20 3d 20  ){.    *ppVal = 
19e65 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  0;.    return SQ
19e66 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 6f  LITE_OK;.  }.  o
19e67 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20  p = pExpr->op;. 
19e68 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49   if( op==TK_REGI
19e69 53 54 45 52 20 29 7b 0a 20 20 20 20 6f 70 20 3d  STER ){.    op =
19e6a 20 70 45 78 70 72 2d 3e 6f 70 32 3b 20 20 2f 2a   pExpr->op2;  /*
19e6b 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65   This only happe
19e6c 6e 73 20 77 69 74 68 20 53 51 4c 49 54 45 5f 45  ns with SQLITE_E
19e6d 4e 41 42 4c 45 5f 53 54 41 54 32 20 2a 2f 0a 20  NABLE_STAT2 */. 
19e6e 20 7d 0a 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b   }..  if( op==TK
19e6f 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70 3d 3d 54  _STRING || op==T
19e70 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70 3d 3d 54  K_FLOAT || op==T
19e71 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20  K_INTEGER ){.   
19e72 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56   pVal = sqlite3V
19e73 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20  alueNew(db);.   
19e74 20 69 66 28 20 70 56 61 6c 3d 3d 30 20 29 20 67   if( pVal==0 ) g
19e75 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20  oto no_mem;.    
19e76 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
19e77 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e  rty(pExpr, EP_In
19e78 74 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20  tValue) ){.     
19e79 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19e7a 65 74 49 6e 74 36 34 28 70 56 61 6c 2c 20 28 69  etInt64(pVal, (i
19e7b 36 34 29 70 45 78 70 72 2d 3e 75 2e 69 56 61 6c  64)pExpr->u.iVal
19e7c 75 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ue);.    }else{.
19e7d 20 20 20 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c        zVal = sql
19e7e 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
19e7f 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
19e80 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 56 61  );.      if( zVa
19e81 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d  l==0 ) goto no_m
19e82 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  em;.      sqlite
19e83 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 61  3ValueSetStr(pVa
19e84 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51 4c  l, -1, zVal, SQL
19e85 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
19e86 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20  _DYNAMIC);.     
19e87 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41   if( op==TK_FLOA
19e88 54 20 29 20 70 56 61 6c 2d 3e 74 79 70 65 20 3d  T ) pVal->type =
19e89 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20   SQLITE_FLOAT;. 
19e8a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 6f 70     }.    if( (op
19e8b 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 7c 7c 20  ==TK_INTEGER || 
19e8c 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 26  op==TK_FLOAT ) &
19e8d 26 20 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49  & affinity==SQLI
19e8e 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20  TE_AFF_NONE ){. 
19e8f 20 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75       sqlite3Valu
19e90 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70  eApplyAffinity(p
19e91 56 61 6c 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  Val, SQLITE_AFF_
19e92 4e 55 4d 45 52 49 43 2c 20 53 51 4c 49 54 45 5f  NUMERIC, SQLITE_
19e93 55 54 46 38 29 3b 0a 20 20 20 20 7d 65 6c 73 65  UTF8);.    }else
19e94 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
19e95 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74  alueApplyAffinit
19e96 79 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79  y(pVal, affinity
19e97 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
19e98 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e      }.    if( en
19e99 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c!=SQLITE_UTF8 )
19e9a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
19e9b 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
19e9c 67 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20  g(pVal, enc);.  
19e9d 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
19e9e 6f 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 20  op==TK_UMINUS ) 
19e9f 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  {.    if( SQLITE
19ea0 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 61 6c 75  _OK==sqlite3Valu
19ea1 65 46 72 6f 6d 45 78 70 72 28 64 62 2c 70 45 78  eFromExpr(db,pEx
19ea2 70 72 2d 3e 70 4c 65 66 74 2c 65 6e 63 2c 61 66  pr->pLeft,enc,af
19ea3 66 69 6e 69 74 79 2c 26 70 56 61 6c 29 20 29 7b  finity,&pVal) ){
19ea4 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 75 2e 69  .      pVal->u.i
19ea5 20 3d 20 2d 31 20 2a 20 70 56 61 6c 2d 3e 75 2e   = -1 * pVal->u.
19ea6 69 3b 0a 20 20 20 20 20 20 2f 2a 20 28 64 6f 75  i;.      /* (dou
19ea7 62 6c 65 29 2d 31 20 49 6e 20 63 61 73 65 20 6f  ble)-1 In case o
19ea8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
19ea9 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20  OATING_POINT... 
19eaa 2a 2f 0a 20 20 20 20 20 20 70 56 61 6c 2d 3e 72  */.      pVal->r
19eab 20 3d 20 28 64 6f 75 62 6c 65 29 2d 31 20 2a 20   = (double)-1 * 
19eac 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20 7d 0a 20  pVal->r;.    }. 
19ead 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
19eae 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45  E_OMIT_BLOB_LITE
19eaf 52 41 4c 0a 20 20 65 6c 73 65 20 69 66 28 20 6f  RAL.  else if( o
19eb0 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b 0a 20 20  p==TK_BLOB ){.  
19eb1 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20 20 20 20    int nVal;.    
19eb2 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75  assert( pExpr->u
19eb3 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 78 27 20  .zToken[0]=='x' 
19eb4 7c 7c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  || pExpr->u.zTok
19eb5 65 6e 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20  en[0]=='X' );.  
19eb6 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
19eb7 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 27 5c  >u.zToken[1]=='\
19eb8 27 27 20 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d  '' );.    pVal =
19eb9 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
19eba 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 21 70  (db);.    if( !p
19ebb 56 61 6c 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  Val ) goto no_me
19ebc 6d 3b 0a 20 20 20 20 7a 56 61 6c 20 3d 20 26 70  m;.    zVal = &p
19ebd 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 32  Expr->u.zToken[2
19ebe 5d 3b 0a 20 20 20 20 6e 56 61 6c 20 3d 20 73 71  ];.    nVal = sq
19ebf 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 56  lite3Strlen30(zV
19ec0 61 6c 29 2d 31 3b 0a 20 20 20 20 61 73 73 65 72  al)-1;.    asser
19ec1 74 28 20 7a 56 61 6c 5b 6e 56 61 6c 5d 3d 3d 27  t( zVal[nVal]=='
19ec2 5c 27 27 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  \'' );.    sqlit
19ec3 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28  e3VdbeMemSetStr(
19ec4 70 56 61 6c 2c 20 73 71 6c 69 74 65 33 48 65 78  pVal, sqlite3Hex
19ec5 54 6f 42 6c 6f 62 28 64 62 2c 20 7a 56 61 6c 2c  ToBlob(db, zVal,
19ec6 20 6e 56 61 6c 29 2c 20 6e 56 61 6c 2f 32 2c 0a   nVal), nVal/2,.
19ec7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19ec8 20 20 20 20 20 20 20 20 20 30 2c 20 53 51 4c 49           0, SQLI
19ec9 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d  TE_DYNAMIC);.  }
19eca 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70  .#endif..  if( p
19ecb 56 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Val ){.    sqlit
19ecc 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79  e3VdbeMemStoreTy
19ecd 70 65 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 20 20  pe(pVal);.  }.  
19ece 2a 70 70 56 61 6c 20 3d 20 70 56 61 6c 3b 0a 20  *ppVal = pVal;. 
19ecf 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
19ed0 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64 62  K;..no_mem:.  db
19ed1 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
19ed2 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46   1;.  sqlite3DbF
19ed3 72 65 65 28 64 62 2c 20 7a 56 61 6c 29 3b 0a 20  ree(db, zVal);. 
19ed4 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
19ed5 65 28 70 56 61 6c 29 3b 0a 20 20 2a 70 70 56 61  e(pVal);.  *ppVa
19ed6 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  l = 0;.  return 
19ed7 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a  SQLITE_NOMEM;.}.
19ed8 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
19ed9 65 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 6f  e string value o
19eda 66 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  f an sqlite3_val
19edb 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c  ue object.*/.SQL
19edc 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
19edd 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
19ede 53 74 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  Str(.  sqlite3_v
19edf 61 6c 75 65 20 2a 76 2c 20 20 20 20 20 2f 2a 20  alue *v,     /* 
19ee0 56 61 6c 75 65 20 74 6f 20 62 65 20 73 65 74 20  Value to be set 
19ee1 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20 20 20 20  */.  int n,     
19ee2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
19ee3 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 7a  ngth of string z
19ee4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
19ee5 20 2a 7a 2c 20 20 20 20 20 20 20 20 2f 2a 20 54   *z,        /* T
19ee6 65 78 74 20 6f 66 20 74 68 65 20 6e 65 77 20 73  ext of the new s
19ee7 74 72 69 6e 67 20 2a 2f 0a 20 20 75 38 20 65 6e  tring */.  u8 en
19ee8 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
19ee9 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20   /* Encoding to 
19eea 75 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  use */.  void (*
19eeb 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 2f  xDel)(void*)   /
19eec 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72  * Destructor for
19eed 20 74 68 65 20 73 74 72 69 6e 67 20 2a 2f 0a 29   the string */.)
19eee 7b 0a 20 20 69 66 28 20 76 20 29 20 73 71 6c 69  {.  if( v ) sqli
19eef 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
19ef0 28 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c 20 6e 2c  ((Mem *)v, z, n,
19ef1 20 65 6e 63 2c 20 78 44 65 6c 29 3b 0a 7d 0a 0a   enc, xDel);.}..
19ef2 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 73 71  /*.** Free an sq
19ef3 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
19ef4 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ct.*/.SQLITE_PRI
19ef5 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
19ef6 33 56 61 6c 75 65 46 72 65 65 28 73 71 6c 69 74  3ValueFree(sqlit
19ef7 65 33 5f 76 61 6c 75 65 20 2a 76 29 7b 0a 20 20  e3_value *v){.  
19ef8 69 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b  if( !v ) return;
19ef9 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
19efa 6d 52 65 6c 65 61 73 65 28 28 4d 65 6d 20 2a 29  mRelease((Mem *)
19efb 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  v);.  sqlite3DbF
19efc 72 65 65 28 28 28 4d 65 6d 2a 29 76 29 2d 3e 64  ree(((Mem*)v)->d
19efd 62 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  b, v);.}../*.** 
19efe 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
19eff 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
19f00 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  e sqlite3_value 
19f01 6f 62 6a 65 63 74 20 61 73 73 75 6d 69 6e 67 0a  object assuming.
19f02 2a 2a 20 74 68 61 74 20 69 74 20 75 73 65 73 20  ** that it uses 
19f03 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 22 65 6e  the encoding "en
19f04 63 22 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  c".*/.SQLITE_PRI
19f05 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
19f06 56 61 6c 75 65 42 79 74 65 73 28 73 71 6c 69 74  ValueBytes(sqlit
19f07 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20  e3_value *pVal, 
19f08 75 38 20 65 6e 63 29 7b 0a 20 20 4d 65 6d 20 2a  u8 enc){.  Mem *
19f09 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a  p = (Mem*)pVal;.
19f0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20    if( (p->flags 
19f0b 26 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d 30 20 7c  & MEM_Blob)!=0 |
19f0c 7c 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  | sqlite3ValueTe
19f0d 78 74 28 70 56 61 6c 2c 20 65 6e 63 29 20 29 7b  xt(pVal, enc) ){
19f0e 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67  .    if( p->flag
19f0f 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a  s & MEM_Zero ){.
19f10 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e        return p->
19f11 6e 20 2b 20 70 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a  n + p->u.nZero;.
19f12 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
19f13 20 72 65 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20   return p->n;.  
19f14 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
19f15 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a   0;.}../********
19f16 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64  ****** End of vd
19f17 62 65 6d 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  bemem.c ********
19f18 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f1a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
19f1b 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
19f1c 65 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a  e vdbeaux.c ****
19f1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f1f 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
19f20 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a  3 September 6.**
19f21 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
19f22 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
19f23 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
19f24 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
19f25 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
19f26 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
19f27 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
19f28 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
19f29 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
19f2a 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
19f2b 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
19f2c 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
19f2d 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
19f2e 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
19f2f 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
19f30 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
19f31 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
19f32 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
19f33 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19f36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
19f37 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
19f38 6e 73 20 63 6f 64 65 20 75 73 65 64 20 66 6f 72  ns code used for
19f39 20 63 72 65 61 74 69 6e 67 2c 20 64 65 73 74 72   creating, destr
19f3a 6f 79 69 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c  oying, and popul
19f3b 61 74 69 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20  ating.** a VDBE 
19f3c 28 6f 72 20 61 6e 20 22 73 71 6c 69 74 65 33 5f  (or an "sqlite3_
19f3d 73 74 6d 74 22 20 61 73 20 69 74 20 69 73 20 6b  stmt" as it is k
19f3e 6e 6f 77 6e 20 74 6f 20 74 68 65 20 6f 75 74 73  nown to the outs
19f3f 69 64 65 20 77 6f 72 6c 64 2e 29 20 20 50 72 69  ide world.)  Pri
19f40 6f 72 0a 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e  or.** to version
19f41 20 32 2e 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73   2.8.7, all this
19f42 20 63 6f 64 65 20 77 61 73 20 63 6f 6d 62 69 6e   code was combin
19f43 65 64 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65  ed into the vdbe
19f44 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a  .c source file..
19f45 2a 2a 20 42 75 74 20 74 68 61 74 20 66 69 6c 65  ** But that file
19f46 20 77 61 73 20 67 65 74 74 69 6e 67 20 74 6f 6f   was getting too
19f47 20 62 69 67 20 73 6f 20 74 68 69 73 20 73 75 62   big so this sub
19f48 72 6f 75 74 69 6e 65 73 20 77 65 72 65 20 73 70  routines were sp
19f49 6c 69 74 20 6f 75 74 2e 0a 2a 2f 0a 0a 0a 0a 2f  lit out..*/..../
19f4a 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62 75 67 67  *.** When debugg
19f4b 69 6e 67 20 74 68 65 20 63 6f 64 65 20 67 65 6e  ing the code gen
19f4c 65 72 61 74 6f 72 20 69 6e 20 61 20 73 79 6d 62  erator in a symb
19f4d 6f 6c 69 63 20 64 65 62 75 67 67 65 72 2c 20 6f  olic debugger, o
19f4e 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68  ne can.** set th
19f4f 65 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  e sqlite3VdbeAdd
19f50 6f 70 54 72 61 63 65 20 74 6f 20 31 20 61 6e 64  opTrace to 1 and
19f51 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c   all opcodes wil
19f52 6c 20 62 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20  l be printed.** 
19f53 61 73 20 74 68 65 79 20 61 72 65 20 61 64 64 65  as they are adde
19f54 64 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63  d to the instruc
19f55 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a  tion stream..*/.
19f56 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
19f57 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
19f58 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
19f59 62 65 41 64 64 6f 70 54 72 61 63 65 20 3d 20 30  beAddopTrace = 0
19f5a 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  ;.#endif.../*.**
19f5b 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 76 69   Create a new vi
19f5c 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
19f5d 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ngine..*/.SQLITE
19f5e 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a 73  _PRIVATE Vdbe *s
19f5f 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65  qlite3VdbeCreate
19f60 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
19f61 20 56 64 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20   Vdbe *p;.  p = 
19f62 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
19f63 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 56  ero(db, sizeof(V
19f64 64 62 65 29 20 29 3b 0a 20 20 69 66 28 20 70 3d  dbe) );.  if( p=
19f65 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
19f66 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69   p->db = db;.  i
19f67 66 28 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a  f( db->pVdbe ){.
19f68 20 20 20 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70      db->pVdbe->p
19f69 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20  Prev = p;.  }.  
19f6a 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70  p->pNext = db->p
19f6b 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76  Vdbe;.  p->pPrev
19f6c 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62   = 0;.  db->pVdb
19f6d 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69  e = p;.  p->magi
19f6e 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49  c = VDBE_MAGIC_I
19f6f 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b  NIT;.  return p;
19f70 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62  .}../*.** Rememb
19f71 65 72 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e  er the SQL strin
19f72 67 20 66 6f 72 20 61 20 70 72 65 70 61 72 65 64  g for a prepared
19f73 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
19f74 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
19f75 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  id sqlite3VdbeSe
19f76 74 53 71 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f  tSql(Vdbe *p, co
19f77 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
19f78 20 6e 2c 20 69 6e 74 20 69 73 50 72 65 70 61 72   n, int isPrepar
19f79 65 56 32 29 7b 0a 20 20 61 73 73 65 72 74 28 20  eV2){.  assert( 
19f7a 69 73 50 72 65 70 61 72 65 56 32 3d 3d 31 20 7c  isPrepareV2==1 |
19f7b 7c 20 69 73 50 72 65 70 61 72 65 56 32 3d 3d 30  | isPrepareV2==0
19f7c 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29   );.  if( p==0 )
19f7d 20 72 65 74 75 72 6e 3b 0a 23 69 66 64 65 66 20   return;.#ifdef 
19f7e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
19f7f 45 0a 20 20 69 66 28 20 21 69 73 50 72 65 70 61  E.  if( !isPrepa
19f80 72 65 56 32 20 29 20 72 65 74 75 72 6e 3b 0a 23  reV2 ) return;.#
19f81 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20  endif.  assert( 
19f82 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29 3b 0a 20 20  p->zSql==0 );.  
19f83 70 2d 3e 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  p->zSql = sqlite
19f84 33 44 62 53 74 72 4e 44 75 70 28 70 2d 3e 64 62  3DbStrNDup(p->db
19f85 2c 20 7a 2c 20 6e 29 3b 0a 20 20 70 2d 3e 69 73  , z, n);.  p->is
19f86 50 72 65 70 61 72 65 56 32 20 3d 20 28 75 38 29  PrepareV2 = (u8)
19f87 69 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a  isPrepareV2;.}..
19f88 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
19f89 20 53 51 4c 20 61 73 73 6f 63 69 61 74 65 64 20   SQL associated 
19f8a 77 69 74 68 20 61 20 70 72 65 70 61 72 65 64 20  with a prepared 
19f8b 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c  statement.*/.SQL
19f8c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
19f8d 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28  ar *sqlite3_sql(
19f8e 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
19f8f 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20  tmt){.  Vdbe *p 
19f90 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
19f91 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 69 73  .  return (p->is
19f92 50 72 65 70 61 72 65 56 32 20 3f 20 70 2d 3e 7a  PrepareV2 ? p->z
19f93 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  Sql : 0);.}../*.
19f94 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 6f 6e 74  ** Swap all cont
19f95 65 6e 74 20 62 65 74 77 65 65 6e 20 74 77 6f 20  ent between two 
19f96 56 44 42 45 20 73 74 72 75 63 74 75 72 65 73 2e  VDBE structures.
19f97 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19f98 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
19f99 64 62 65 53 77 61 70 28 56 64 62 65 20 2a 70 41  dbeSwap(Vdbe *pA
19f9a 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a 20 20 56  , Vdbe *pB){.  V
19f9b 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a  dbe tmp, *pTmp;.
19f9c 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20    char *zTmp;.  
19f9d 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a 70 41  tmp = *pA;.  *pA
19f9e 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20 3d 20   = *pB;.  *pB = 
19f9f 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70 41  tmp;.  pTmp = pA
19fa0 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d 3e 70  ->pNext;.  pA->p
19fa1 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65 78 74  Next = pB->pNext
19fa2 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20 3d 20  ;.  pB->pNext = 
19fa3 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70  pTmp;.  pTmp = p
19fa4 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41 2d 3e  A->pPrev;.  pA->
19fa5 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50 72 65  pPrev = pB->pPre
19fa6 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76 20 3d  v;.  pB->pPrev =
19fa7 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20 3d 20   pTmp;.  zTmp = 
19fa8 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41 2d 3e  pA->zSql;.  pA->
19fa9 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71 6c 3b  zSql = pB->zSql;
19faa 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20 7a 54  .  pB->zSql = zT
19fab 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 72 65 70  mp;.  pB->isPrep
19fac 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 73 50 72  areV2 = pA->isPr
19fad 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 69 66 64  epareV2;.}..#ifd
19fae 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
19faf 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61 63 69  /*.** Turn traci
19fb0 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a  ng on or off.*/.
19fb1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19fb2 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54  oid sqlite3VdbeT
19fb3 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20 46 49  race(Vdbe *p, FI
19fb4 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d  LE *trace){.  p-
19fb5 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65 3b 0a  >trace = trace;.
19fb6 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
19fb7 52 65 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e  Resize the Vdbe.
19fb8 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74 68 61  aOp array so tha
19fb9 74 20 69 74 20 69 73 20 61 74 20 6c 65 61 73 74  t it is at least
19fba 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 20 74   one op larger t
19fbb 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73 2e 0a  han .** it was..
19fbc 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d  **.** If an out-
19fbd 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20  of-memory error 
19fbe 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 73  occurs while res
19fbf 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79 2c  izing the array,
19fc0 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54   return.** SQLIT
19fc1 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 69 73  E_NOMEM. In this
19fc2 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70 20 61   case Vdbe.aOp a
19fc3 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63  nd Vdbe.nOpAlloc
19fc4 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e 63 68   remain .** unch
19fc5 61 6e 67 65 64 20 28 74 68 69 73 20 69 73 20 73  anged (this is s
19fc6 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63 6f 64  o that any opcod
19fc7 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63  es already alloc
19fc8 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a 2a 20  ated can be .** 
19fc9 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c 6c 6f  correctly deallo
19fca 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68  cated along with
19fcb 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65   the rest of the
19fcc 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69   Vdbe)..*/.stati
19fcd 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72 72 61  c int growOpArra
19fce 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 56 64  y(Vdbe *p){.  Vd
19fcf 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e  beOp *pNew;.  in
19fd0 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e 4f 70  t nNew = (p->nOp
19fd1 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c  Alloc ? p->nOpAl
19fd2 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28 31 30  loc*2 : (int)(10
19fd3 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 29 3b  24/sizeof(Op)));
19fd4 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
19fd5 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62  3DbRealloc(p->db
19fd6 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73  , p->aOp, nNew*s
19fd7 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66  izeof(Op));.  if
19fd8 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d  ( pNew ){.    p-
19fd9 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 71 6c 69  >nOpAlloc = sqli
19fda 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
19fdb 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f 73 69 7a  p->db, pNew)/siz
19fdc 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 70 2d 3e  eof(Op);.    p->
19fdd 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  aOp = pNew;.  }.
19fde 20 20 72 65 74 75 72 6e 20 28 70 4e 65 77 20 3f    return (pNew ?
19fdf 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c   SQLITE_OK : SQL
19fe0 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f  ITE_NOMEM);.}../
19fe1 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 69  *.** Add a new i
19fe2 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 74 68  nstruction to th
19fe3 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 74 72 75  e list of instru
19fe4 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 20 69  ctions current i
19fe5 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 20 20  n the.** VDBE.  
19fe6 52 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 65  Return the addre
19fe7 73 73 20 6f 66 20 74 68 65 20 6e 65 77 20 69 6e  ss of the new in
19fe8 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  struction..**.**
19fe9 20 50 61 72 61 6d 65 74 65 72 73 3a 0a 2a 2a 0a   Parameters:.**.
19fea 2a 2a 20 20 20 20 70 20 20 20 20 20 20 20 20 20  **    p         
19feb 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f        Pointer to
19fec 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a 2a 2a 20   the VDBE.**.** 
19fed 20 20 20 6f 70 20 20 20 20 20 20 20 20 20 20 20     op           
19fee 20 20 20 54 68 65 20 6f 70 63 6f 64 65 20 66 6f     The opcode fo
19fef 72 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  r this instructi
19ff0 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 31 2c 20  on.**.**    p1, 
19ff1 70 32 2c 20 70 33 20 20 20 20 20 20 4f 70 65 72  p2, p3      Oper
19ff2 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74  ands.**.** Use t
19ff3 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  he sqlite3VdbeRe
19ff4 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 66 75 6e  solveLabel() fun
19ff5 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 61 6e 20  ction to fix an 
19ff6 61 64 64 72 65 73 73 20 61 6e 64 0a 2a 2a 20 74  address and.** t
19ff7 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  he sqlite3VdbeCh
19ff8 61 6e 67 65 50 34 28 29 20 66 75 6e 63 74 69 6f  angeP4() functio
19ff9 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20  n to change the 
19ffa 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34 0a  value of the P4.
19ffb 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a 2f 0a 53  ** operand..*/.S
19ffc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
19ffd 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
19ffe 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  Op3(Vdbe *p, int
19fff 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74   op, int p1, int
1a000 20 70 32 2c 20 69 6e 74 20 70 33 29 7b 0a 20 20   p2, int p3){.  
1a001 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 4f 70 20  int i;.  VdbeOp 
1a002 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 70 2d 3e  *pOp;..  i = p->
1a003 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70  nOp;.  assert( p
1a004 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
1a005 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73  GIC_INIT );.  as
1a006 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20 6f 70  sert( op>0 && op
1a007 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 28 20 70  <0xff );.  if( p
1a008 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 20 29 7b  ->nOpAlloc<=i ){
1a009 0a 20 20 20 20 69 66 28 20 67 72 6f 77 4f 70 41  .    if( growOpA
1a00a 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 20  rray(p) ){.     
1a00b 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
1a00c 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b 2b 3b  .  }.  p->nOp++;
1a00d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70  .  pOp = &p->aOp
1a00e 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f  [i];.  pOp->opco
1a00f 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 70  de = (u8)op;.  p
1a010 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 20 70 4f  Op->p5 = 0;.  pO
1a011 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20 70 4f  p->p1 = p1;.  pO
1a012 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20 70 4f  p->p2 = p2;.  pO
1a013 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 20 70 4f  p->p3 = p3;.  pO
1a014 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 70  p->p4.p = 0;.  p
1a015 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f  Op->p4type = P4_
1a016 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d 3e 65 78  NOTUSED;.  p->ex
1a017 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 66 64 65  pired = 0;.#ifde
1a018 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
1a019 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d   pOp->zComment =
1a01a 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   0;.  if( sqlite
1a01b 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20  3VdbeAddopTrace 
1a01c 29 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69  ) sqlite3VdbePri
1a01d 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 2d 3e 61  ntOp(0, i, &p->a
1a01e 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 23  Op[i]);.#endif.#
1a01f 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
1a020 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73  LE.  pOp->cycles
1a021 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 63 6e 74   = 0;.  pOp->cnt
1a022 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72   = 0;.#endif.  r
1a023 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54  eturn i;.}.SQLIT
1a024 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1a025 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
1a026 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 29  Vdbe *p, int op)
1a027 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
1a028 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20  e3VdbeAddOp3(p, 
1a029 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 7d 0a  op, 0, 0, 0);.}.
1a02a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1a02b 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
1a02c 64 4f 70 31 28 56 64 62 65 20 2a 70 2c 20 69 6e  dOp1(Vdbe *p, in
1a02d 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 7b 0a 20  t op, int p1){. 
1a02e 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
1a02f 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c  dbeAddOp3(p, op,
1a030 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51   p1, 0, 0);.}.SQ
1a031 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1a032 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1a033 70 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  p2(Vdbe *p, int 
1a034 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 20  op, int p1, int 
1a035 70 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  p2){.  return sq
1a036 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1a037 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 30  p, op, p1, p2, 0
1a038 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  );.}.../*.** Add
1a039 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61 74 20   an opcode that 
1a03a 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70 34 20  includes the p4 
1a03b 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69 6e 74  value as a point
1a03c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
1a03d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1a03e 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20 56  3VdbeAddOp4(.  V
1a03f 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  dbe *p,         
1a040 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6f 70     /* Add the op
1a041 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 4d 20  code to this VM 
1a042 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
1a043 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1a044 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  new opcode */.  
1a045 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20 20 20  int p1,         
1a046 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20 6f 70      /* The P1 op
1a047 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70  erand */.  int p
1a048 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  2,             /
1a049 2a 20 54 68 65 20 50 32 20 6f 70 65 72 61 6e 64  * The P2 operand
1a04a 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c 20 20 20   */.  int p3,   
1a04b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1a04c 20 50 33 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20   P3 operand */. 
1a04d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34   const char *zP4
1a04e 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 34 20 6f  ,    /* The P4 o
1a04f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
1a050 70 34 74 79 70 65 20 20 20 20 20 20 20 20 20 20  p4type          
1a051 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 20 74 79  /* P4 operand ty
1a052 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61  pe */.){.  int a
1a053 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1a054 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70  eAddOp3(p, op, p
1a055 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71  1, p2, p3);.  sq
1a056 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1a057 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20  4(p, addr, zP4, 
1a058 70 34 74 79 70 65 29 3b 0a 20 20 72 65 74 75 72  p4type);.  retur
1a059 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n addr;.}../*.**
1a05a 20 41 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74   Add an opcode t
1a05b 68 61 74 20 69 6e 63 6c 75 64 65 73 20 74 68 65  hat includes the
1a05c 20 70 34 20 76 61 6c 75 65 20 61 73 20 61 6e 20   p4 value as an 
1a05d 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49  integer..*/.SQLI
1a05e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1a05f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1a060 49 6e 74 28 0a 20 20 56 64 62 65 20 2a 70 2c 20  Int(.  Vdbe *p, 
1a061 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
1a062 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f 20  d the opcode to 
1a063 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e 74  this VM */.  int
1a064 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   op,            
1a065 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63 6f   /* The new opco
1a066 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20  de */.  int p1, 
1a067 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1a068 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f  he P1 operand */
1a069 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20 20  .  int p2,      
1a06a 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 32         /* The P2
1a06b 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e   operand */.  in
1a06c 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20 20  t p3,           
1a06d 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65 72    /* The P3 oper
1a06e 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70 34 20  and */.  int p4 
1a06f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a070 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 61  The P4 operand a
1a071 73 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a  s an integer */.
1a072 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20  ){.  int addr = 
1a073 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1a074 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c  3(p, op, p1, p2,
1a075 20 70 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56   p3);.  sqlite3V
1a076 64 62 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61  dbeChangeP4(p, a
1a077 64 64 72 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f  ddr, SQLITE_INT_
1a078 54 4f 5f 50 54 52 28 70 34 29 2c 20 50 34 5f 49  TO_PTR(p4), P4_I
1a079 4e 54 33 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  NT32);.  return 
1a07a 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  addr;.}../*.** C
1a07b 72 65 61 74 65 20 61 20 6e 65 77 20 73 79 6d 62  reate a new symb
1a07c 6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f 72 20 61  olic label for a
1a07d 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  n instruction th
1a07e 61 74 20 68 61 73 20 79 65 74 20 74 6f 20 62 65  at has yet to be
1a07f 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 54 68 65 20  .** coded.  The 
1a080 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 69  symbolic label i
1a081 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 61 20  s really just a 
1a082 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 2e  negative number.
1a083 20 20 54 68 65 0a 2a 2a 20 6c 61 62 65 6c 20 63    The.** label c
1a084 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 74 68  an be used as th
1a085 65 20 50 32 20 76 61 6c 75 65 20 6f 66 20 61 6e  e P2 value of an
1a086 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4c 61 74   operation.  Lat
1a087 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  er, when.** the 
1a088 6c 61 62 65 6c 20 69 73 20 72 65 73 6f 6c 76 65  label is resolve
1a089 64 20 74 6f 20 61 20 73 70 65 63 69 66 69 63 20  d to a specific 
1a08a 61 64 64 72 65 73 73 2c 20 74 68 65 20 56 44 42  address, the VDB
1a08b 45 20 77 69 6c 6c 20 73 63 61 6e 0a 2a 2a 20 74  E will scan.** t
1a08c 68 72 6f 75 67 68 20 69 74 73 20 6f 70 65 72 61  hrough its opera
1a08d 74 69 6f 6e 20 6c 69 73 74 20 61 6e 64 20 63 68  tion list and ch
1a08e 61 6e 67 65 20 61 6c 6c 20 76 61 6c 75 65 73 20  ange all values 
1a08f 6f 66 20 50 32 20 77 68 69 63 68 20 6d 61 74 63  of P2 which matc
1a090 68 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c 20 69  h.** the label i
1a091 6e 74 6f 20 74 68 65 20 72 65 73 6f 6c 76 65 64  nto the resolved
1a092 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20   address..**.** 
1a093 54 68 65 20 56 44 42 45 20 6b 6e 6f 77 73 20 74  The VDBE knows t
1a094 68 61 74 20 61 20 50 32 20 76 61 6c 75 65 20 69  hat a P2 value i
1a095 73 20 61 20 6c 61 62 65 6c 20 62 65 63 61 75 73  s a label becaus
1a096 65 20 6c 61 62 65 6c 73 20 61 72 65 0a 2a 2a 20  e labels are.** 
1a097 61 6c 77 61 79 73 20 6e 65 67 61 74 69 76 65 20  always negative 
1a098 61 6e 64 20 50 32 20 76 61 6c 75 65 73 20 61 72  and P2 values ar
1a099 65 20 73 75 70 70 6f 73 65 20 74 6f 20 62 65 20  e suppose to be 
1a09a 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a 2a  non-negative..**
1a09b 20 48 65 6e 63 65 2c 20 61 20 6e 65 67 61 74 69   Hence, a negati
1a09c 76 65 20 50 32 20 76 61 6c 75 65 20 69 73 20 61  ve P2 value is a
1a09d 20 6c 61 62 65 6c 20 74 68 61 74 20 68 61 73 20   label that has 
1a09e 79 65 74 20 74 6f 20 62 65 20 72 65 73 6f 6c 76  yet to be resolv
1a09f 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 6f 20 69  ed..**.** Zero i
1a0a0 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61 20  s returned if a 
1a0a1 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a  malloc() fails..
1a0a2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a0a3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
1a0a4 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62 65 20  eMakeLabel(Vdbe 
1a0a5 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  *p){.  int i;.  
1a0a6 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b 2b 3b  i = p->nLabel++;
1a0a7 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61  .  assert( p->ma
1a0a8 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
1a0a9 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 69 3e  INIT );.  if( i>
1a0aa 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20  =p->nLabelAlloc 
1a0ab 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70  ){.    int n = p
1a0ac 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 2a 32 20  ->nLabelAlloc*2 
1a0ad 2b 20 35 3b 0a 20 20 20 20 70 2d 3e 61 4c 61 62  + 5;.    p->aLab
1a0ae 65 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65  el = sqlite3DbRe
1a0af 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2d 3e 64  allocOrFree(p->d
1a0b0 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 2c 0a 20 20  b, p->aLabel,.  
1a0b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a0b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a0b3 20 20 20 20 20 6e 2a 73 69 7a 65 6f 66 28 70 2d       n*sizeof(p-
1a0b4 3e 61 4c 61 62 65 6c 5b 30 5d 29 29 3b 0a 20 20  >aLabel[0]));.  
1a0b5 20 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63    p->nLabelAlloc
1a0b6 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1a0b7 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20 70 2d  ocSize(p->db, p-
1a0b8 3e 61 4c 61 62 65 6c 29 2f 73 69 7a 65 6f 66 28  >aLabel)/sizeof(
1a0b9 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 3b 0a 20  p->aLabel[0]);. 
1a0ba 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4c 61 62   }.  if( p->aLab
1a0bb 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61  el ){.    p->aLa
1a0bc 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d  bel[i] = -1;.  }
1a0bd 0a 20 20 72 65 74 75 72 6e 20 2d 31 2d 69 3b 0a  .  return -1-i;.
1a0be 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65  }../*.** Resolve
1a0bf 20 6c 61 62 65 6c 20 22 78 22 20 74 6f 20 62 65   label "x" to be
1a0c0 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
1a0c1 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
1a0c2 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62 65 20 69 6e  tion to.** be in
1a0c3 73 65 72 74 65 64 2e 20 20 54 68 65 20 70 61 72  serted.  The par
1a0c4 61 6d 65 74 65 72 20 22 78 22 20 6d 75 73 74 20  ameter "x" must 
1a0c5 68 61 76 65 20 62 65 65 6e 20 6f 62 74 61 69 6e  have been obtain
1a0c6 65 64 20 66 72 6f 6d 0a 2a 2a 20 61 20 70 72 69  ed from.** a pri
1a0c7 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  or call to sqlit
1a0c8 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
1a0c9 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
1a0ca 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a0cb 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
1a0cc 6c 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 78  l(Vdbe *p, int x
1a0cd 29 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 2d 31 2d  ){.  int j = -1-
1a0ce 78 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  x;.  assert( p->
1a0cf 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
1a0d0 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73 73 65  C_INIT );.  asse
1a0d1 72 74 28 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d  rt( j>=0 && j<p-
1a0d2 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20 20 69 66 28  >nLabel );.  if(
1a0d3 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20   p->aLabel ){.  
1a0d4 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 6a 5d 20 3d    p->aLabel[j] =
1a0d5 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a   p->nOp;.  }.}..
1a0d6 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
1a0d7 42 55 47 20 2f 2a 20 73 71 6c 69 74 65 33 41 73  BUG /* sqlite3As
1a0d8 73 65 72 74 4d 61 79 41 62 6f 72 74 28 29 20 6c  sertMayAbort() l
1a0d9 6f 67 69 63 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ogic */../*.** T
1a0da 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 79 70  he following typ
1a0db 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61  e and function a
1a0dc 72 65 20 75 73 65 64 20 74 6f 20 69 74 65 72 61  re used to itera
1a0dd 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 6f  te through all o
1a0de 70 63 6f 64 65 73 0a 2a 2a 20 69 6e 20 61 20 56  pcodes.** in a V
1a0df 64 62 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d  dbe main program
1a0e0 20 61 6e 64 20 65 61 63 68 20 6f 66 20 74 68 65   and each of the
1a0e1 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 28 74   sub-programs (t
1a0e2 72 69 67 67 65 72 73 29 20 69 74 20 6d 61 79 20  riggers) it may 
1a0e3 0a 2a 2a 20 69 6e 76 6f 6b 65 20 64 69 72 65 63  .** invoke direc
1a0e4 74 6c 79 20 6f 72 20 69 6e 64 69 72 65 63 74 6c  tly or indirectl
1a0e5 79 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20  y. It should be 
1a0e6 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  used as follows:
1a0e7 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 20 2a 70 4f 70  .**.**   Op *pOp
1a0e8 3b 0a 2a 2a 20 20 20 56 64 62 65 4f 70 49 74 65  ;.**   VdbeOpIte
1a0e9 72 20 73 49 74 65 72 3b 0a 2a 2a 0a 2a 2a 20 20  r sIter;.**.**  
1a0ea 20 6d 65 6d 73 65 74 28 26 73 49 74 65 72 2c 20   memset(&sIter, 
1a0eb 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 65 72 29  0, sizeof(sIter)
1a0ec 29 3b 0a 2a 2a 20 20 20 73 49 74 65 72 2e 76 20  );.**   sIter.v 
1a0ed 3d 20 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  = v;            
1a0ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a0ef 2f 2f 20 76 20 69 73 20 6f 66 20 74 79 70 65 20  // v is of type 
1a0f0 56 64 62 65 2a 20 0a 2a 2a 20 20 20 77 68 69 6c  Vdbe* .**   whil
1a0f1 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 74 65 72  e( (pOp = opIter
1a0f2 4e 65 78 74 28 26 73 49 74 65 72 29 29 20 29 7b  Next(&sIter)) ){
1a0f3 0a 2a 2a 20 20 20 20 20 2f 2f 20 44 6f 20 73 6f  .**     // Do so
1a0f4 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70 4f 70  mething with pOp
1a0f5 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 73 71 6c  .**   }.**   sql
1a0f6 69 74 65 33 44 62 46 72 65 65 28 76 2d 3e 64 62  ite3DbFree(v->db
1a0f7 2c 20 73 49 74 65 72 2e 61 70 53 75 62 29 3b 0a  , sIter.apSub);.
1a0f8 2a 2a 20 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ** .*/.typedef s
1a0f9 74 72 75 63 74 20 56 64 62 65 4f 70 49 74 65 72  truct VdbeOpIter
1a0fa 20 56 64 62 65 4f 70 49 74 65 72 3b 0a 73 74 72   VdbeOpIter;.str
1a0fb 75 63 74 20 56 64 62 65 4f 70 49 74 65 72 20 7b  uct VdbeOpIter {
1a0fc 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20  .  Vdbe *v;     
1a0fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a0fe 20 56 64 62 65 20 74 6f 20 69 74 65 72 61 74 65   Vdbe to iterate
1a0ff 20 74 68 72 6f 75 67 68 20 74 68 65 20 6f 70 63   through the opc
1a100 6f 64 65 73 20 6f 66 20 2a 2f 0a 20 20 53 75 62  odes of */.  Sub
1a101 50 72 6f 67 72 61 6d 20 2a 2a 61 70 53 75 62 3b  Program **apSub;
1a102 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79          /* Array
1a103 20 6f 66 20 73 75 62 70 72 6f 67 72 61 6d 73 20   of subprograms 
1a104 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62 3b 20 20  */.  int nSub;  
1a105 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a106 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
1a107 72 69 65 73 20 69 6e 20 61 70 53 75 62 20 2a 2f  ries in apSub */
1a108 0a 20 20 69 6e 74 20 69 41 64 64 72 3b 20 20 20  .  int iAddr;   
1a109 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a10a 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74   Address of next
1a10b 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20   instruction to 
1a10c 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20  return */.  int 
1a10d 69 53 75 62 3b 20 20 20 20 20 20 20 20 20 20 20  iSub;           
1a10e 20 20 20 20 20 20 20 2f 2a 20 30 20 3d 20 6d 61         /* 0 = ma
1a10f 69 6e 20 70 72 6f 67 72 61 6d 2c 20 31 20 3d 20  in program, 1 = 
1a110 66 69 72 73 74 20 73 75 62 2d 70 72 6f 67 72 61  first sub-progra
1a111 6d 20 65 74 63 2e 20 2a 2f 0a 7d 3b 0a 73 74 61  m etc. */.};.sta
1a112 74 69 63 20 4f 70 20 2a 6f 70 49 74 65 72 4e 65  tic Op *opIterNe
1a113 78 74 28 56 64 62 65 4f 70 49 74 65 72 20 2a 70  xt(VdbeOpIter *p
1a114 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
1a115 2d 3e 76 3b 0a 20 20 4f 70 20 2a 70 52 65 74 20  ->v;.  Op *pRet 
1a116 3d 20 30 3b 0a 20 20 4f 70 20 2a 61 4f 70 3b 0a  = 0;.  Op *aOp;.
1a117 20 20 69 6e 74 20 6e 4f 70 3b 0a 0a 20 20 69 66    int nOp;..  if
1a118 28 20 70 2d 3e 69 53 75 62 3c 3d 70 2d 3e 6e 53  ( p->iSub<=p->nS
1a119 75 62 20 29 7b 0a 0a 20 20 20 20 69 66 28 20 70  ub ){..    if( p
1a11a 2d 3e 69 53 75 62 3d 3d 30 20 29 7b 0a 20 20 20  ->iSub==0 ){.   
1a11b 20 20 20 61 4f 70 20 3d 20 76 2d 3e 61 4f 70 3b     aOp = v->aOp;
1a11c 0a 20 20 20 20 20 20 6e 4f 70 20 3d 20 76 2d 3e  .      nOp = v->
1a11d 6e 4f 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  nOp;.    }else{.
1a11e 20 20 20 20 20 20 61 4f 70 20 3d 20 70 2d 3e 61        aOp = p->a
1a11f 70 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31 5d 2d  pSub[p->iSub-1]-
1a120 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f 70 20  >aOp;.      nOp 
1a121 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e 69 53  = p->apSub[p->iS
1a122 75 62 2d 31 5d 2d 3e 6e 4f 70 3b 0a 20 20 20 20  ub-1]->nOp;.    
1a123 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  }.    assert( p-
1a124 3e 69 41 64 64 72 3c 6e 4f 70 20 29 3b 0a 0a 20  >iAddr<nOp );.. 
1a125 20 20 20 70 52 65 74 20 3d 20 26 61 4f 70 5b 70     pRet = &aOp[p
1a126 2d 3e 69 41 64 64 72 5d 3b 0a 20 20 20 20 70 2d  ->iAddr];.    p-
1a127 3e 69 41 64 64 72 2b 2b 3b 0a 20 20 20 20 69 66  >iAddr++;.    if
1a128 28 20 70 2d 3e 69 41 64 64 72 3d 3d 6e 4f 70 20  ( p->iAddr==nOp 
1a129 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 53 75 62  ){.      p->iSub
1a12a 2b 2b 3b 0a 20 20 20 20 20 20 70 2d 3e 69 41 64  ++;.      p->iAd
1a12b 64 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  dr = 0;.    }.  
1a12c 0a 20 20 20 20 69 66 28 20 70 52 65 74 2d 3e 70  .    if( pRet->p
1a12d 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50 52 4f  4type==P4_SUBPRO
1a12e 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 20 69 6e  GRAM ){.      in
1a12f 74 20 6e 42 79 74 65 20 3d 20 28 70 2d 3e 6e 53  t nByte = (p->nS
1a130 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75 62  ub+1)*sizeof(Sub
1a131 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20  Program*);.     
1a132 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f   int j;.      fo
1a133 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 53 75 62  r(j=0; j<p->nSub
1a134 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
1a135 69 66 28 20 70 2d 3e 61 70 53 75 62 5b 6a 5d 3d  if( p->apSub[j]=
1a136 3d 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f 67 72  =pRet->p4.pProgr
1a137 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  am ) break;.    
1a138 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3d    }.      if( j=
1a139 3d 70 2d 3e 6e 53 75 62 20 29 7b 0a 20 20 20 20  =p->nSub ){.    
1a13a 20 20 20 20 70 2d 3e 61 70 53 75 62 20 3d 20 73      p->apSub = s
1a13b 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f  qlite3DbReallocO
1a13c 72 46 72 65 65 28 76 2d 3e 64 62 2c 20 70 2d 3e  rFree(v->db, p->
1a13d 61 70 53 75 62 2c 20 6e 42 79 74 65 29 3b 0a 20  apSub, nByte);. 
1a13e 20 20 20 20 20 20 20 69 66 28 20 21 70 2d 3e 61         if( !p->a
1a13f 70 53 75 62 20 29 7b 0a 20 20 20 20 20 20 20 20  pSub ){.        
1a140 20 20 70 52 65 74 20 3d 20 30 3b 0a 20 20 20 20    pRet = 0;.    
1a141 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1a142 20 20 20 20 20 70 2d 3e 61 70 53 75 62 5b 70 2d       p->apSub[p-
1a143 3e 6e 53 75 62 2b 2b 5d 20 3d 20 70 52 65 74 2d  >nSub++] = pRet-
1a144 3e 70 34 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20  >p4.pProgram;.  
1a145 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1a146 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
1a147 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a  urn pRet;.}../*.
1a148 2a 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20  ** Check if the 
1a149 70 72 6f 67 72 61 6d 20 73 74 6f 72 65 64 20 69  program stored i
1a14a 6e 20 74 68 65 20 56 4d 20 61 73 73 6f 63 69 61  n the VM associa
1a14b 74 65 64 20 77 69 74 68 20 70 50 61 72 73 65 20  ted with pParse 
1a14c 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e 20  may.** throw an 
1a14d 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e 20  ABORT exception 
1a14e 28 63 61 75 73 69 6e 67 20 74 68 65 20 73 74 61  (causing the sta
1a14f 74 65 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20  tement, but not 
1a150 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69  entire transacti
1a151 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 6f 6c 6c  on.** to be roll
1a152 65 64 20 62 61 63 6b 29 2e 20 54 68 69 73 20 63  ed back). This c
1a153 6f 6e 64 69 74 69 6f 6e 20 69 73 20 74 72 75 65  ondition is true
1a154 20 69 66 20 74 68 65 20 6d 61 69 6e 20 70 72 6f   if the main pro
1a155 67 72 61 6d 20 6f 72 20 61 6e 79 0a 2a 2a 20 73  gram or any.** s
1a156 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 6f 6e 74  ub-programs cont
1a157 61 69 6e 73 20 61 6e 79 20 6f 66 20 74 68 65 20  ains any of the 
1a158 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a  following:.**.**
1a159 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 20 77 69     *  OP_Halt wi
1a15a 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f 4e  th P1=SQLITE_CON
1a15b 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d 4f  STRAINT and P2=O
1a15c 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20  E_Abort..**   * 
1a15d 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 20 77   OP_HaltIfNull w
1a15e 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43 4f  ith P1=SQLITE_CO
1a15f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32 3d  NSTRAINT and P2=
1a160 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20 2a  OE_Abort..**   *
1a161 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a 2a 2a 20    OP_Destroy.** 
1a162 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 74 65 0a    *  OP_VUpdate.
1a163 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65 6e 61  **   *  OP_VRena
1a164 6d 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 46 6b  me.**   *  OP_Fk
1a165 43 6f 75 6e 74 65 72 20 77 69 74 68 20 50 32 3d  Counter with P2=
1a166 3d 30 20 28 69 6d 6d 65 64 69 61 74 65 20 66 6f  =0 (immediate fo
1a167 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
1a168 61 69 6e 74 29 0a 2a 2a 0a 2a 2a 20 54 68 65 6e  aint).**.** Then
1a169 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   check that the 
1a16a 76 61 6c 75 65 20 6f 66 20 50 61 72 73 65 2e 6d  value of Parse.m
1a16b 61 79 41 62 6f 72 74 20 69 73 20 74 72 75 65 20  ayAbort is true 
1a16c 69 66 20 61 6e 0a 2a 2a 20 41 42 4f 52 54 20 6d  if an.** ABORT m
1a16d 61 79 20 62 65 20 74 68 72 6f 77 6e 2c 20 6f 72  ay be thrown, or
1a16e 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65   false otherwise
1a16f 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  . Return true if
1a170 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6d 61 74 63   it does.** matc
1a171 68 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65  h, or false othe
1a172 72 77 69 73 65 2e 20 54 68 69 73 20 66 75 6e 63  rwise. This func
1a173 74 69 6f 6e 20 69 73 20 69 6e 74 65 6e 64 65 64  tion is intended
1a174 20 74 6f 20 62 65 20 75 73 65 64 20 61 73 0a 2a   to be used as.*
1a175 2a 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73 73  * part of an ass
1a176 65 72 74 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  ert statement in
1a177 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e 20 53   the compiler. S
1a178 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a  imilar to:.**.**
1a179 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
1a17a 65 33 56 64 62 65 41 73 73 65 72 74 4d 61 79 41  e3VdbeAssertMayA
1a17b 62 6f 72 74 28 70 50 61 72 73 65 2d 3e 70 56 64  bort(pParse->pVd
1a17c 62 65 2c 20 70 50 61 72 73 65 2d 3e 6d 61 79 41  be, pParse->mayA
1a17d 62 6f 72 74 29 20 29 3b 0a 2a 2f 0a 53 51 4c 49  bort) );.*/.SQLI
1a17e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1a17f 71 6c 69 74 65 33 56 64 62 65 41 73 73 65 72 74  qlite3VdbeAssert
1a180 4d 61 79 41 62 6f 72 74 28 56 64 62 65 20 2a 76  MayAbort(Vdbe *v
1a181 2c 20 69 6e 74 20 6d 61 79 41 62 6f 72 74 29 7b  , int mayAbort){
1a182 0a 20 20 69 6e 74 20 68 61 73 41 62 6f 72 74 20  .  int hasAbort 
1a183 3d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a  = 0;.  Op *pOp;.
1a184 20 20 56 64 62 65 4f 70 49 74 65 72 20 73 49 74    VdbeOpIter sIt
1a185 65 72 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 49  er;.  memset(&sI
1a186 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  ter, 0, sizeof(s
1a187 49 74 65 72 29 29 3b 0a 20 20 73 49 74 65 72 2e  Iter));.  sIter.
1a188 76 20 3d 20 76 3b 0a 0a 20 20 77 68 69 6c 65 28  v = v;..  while(
1a189 20 28 70 4f 70 20 3d 20 6f 70 49 74 65 72 4e 65   (pOp = opIterNe
1a18a 78 74 28 26 73 49 74 65 72 29 29 21 3d 30 20 29  xt(&sIter))!=0 )
1a18b 7b 0a 20 20 20 20 69 6e 74 20 6f 70 63 6f 64 65  {.    int opcode
1a18c 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b 0a   = pOp->opcode;.
1a18d 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d      if( opcode==
1a18e 4f 50 5f 44 65 73 74 72 6f 79 20 7c 7c 20 6f 70  OP_Destroy || op
1a18f 63 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65  code==OP_VUpdate
1a190 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56   || opcode==OP_V
1a191 52 65 6e 61 6d 65 20 0a 23 69 66 6e 64 65 66 20  Rename .#ifndef 
1a192 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
1a193 49 47 4e 5f 4b 45 59 0a 20 20 20 20 20 7c 7c 20  IGN_KEY.     || 
1a194 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6b 43 6f  (opcode==OP_FkCo
1a195 75 6e 74 65 72 20 26 26 20 70 4f 70 2d 3e 70 31  unter && pOp->p1
1a196 3d 3d 30 20 26 26 20 70 4f 70 2d 3e 70 32 3d 3d  ==0 && pOp->p2==
1a197 31 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  1) .#endif.     
1a198 7c 7c 20 28 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f  || ((opcode==OP_
1a199 48 61 6c 74 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d  Halt || opcode==
1a19a 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 29 20 0a  OP_HaltIfNull) .
1a19b 20 20 20 20 20 20 26 26 20 28 70 4f 70 2d 3e 70        && (pOp->p
1a19c 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  1==SQLITE_CONSTR
1a19d 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e 70 32 3d  AINT && pOp->p2=
1a19e 3d 4f 45 5f 41 62 6f 72 74 29 29 0a 20 20 20 20  =OE_Abort)).    
1a19f 29 7b 0a 20 20 20 20 20 20 68 61 73 41 62 6f 72  ){.      hasAbor
1a1a0 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 62 72 65  t = 1;.      bre
1a1a1 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
1a1a2 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 76 2d  sqlite3DbFree(v-
1a1a3 3e 64 62 2c 20 73 49 74 65 72 2e 61 70 53 75 62  >db, sIter.apSub
1a1a4 29 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20  );..  /* Return 
1a1a5 74 72 75 65 20 69 66 20 68 61 73 41 62 6f 72 74  true if hasAbort
1a1a6 3d 3d 6d 61 79 41 62 6f 72 74 2e 20 4f 72 20 69  ==mayAbort. Or i
1a1a7 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  f a malloc failu
1a1a8 72 65 20 6f 63 63 75 72 65 64 2e 0a 20 20 2a 2a  re occured..  **
1a1a9 20 49 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 65   If malloc faile
1a1aa 64 2c 20 74 68 65 6e 20 74 68 65 20 77 68 69 6c  d, then the whil
1a1ab 65 28 29 20 6c 6f 6f 70 20 61 62 6f 76 65 20 6d  e() loop above m
1a1ac 61 79 20 6e 6f 74 20 68 61 76 65 20 69 74 65 72  ay not have iter
1a1ad 61 74 65 64 0a 20 20 2a 2a 20 74 68 72 6f 75 67  ated.  ** throug
1a1ae 68 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 61 6e  h all opcodes an
1a1af 64 20 68 61 73 41 62 6f 72 74 20 6d 61 79 20 62  d hasAbort may b
1a1b0 65 20 73 65 74 20 69 6e 63 6f 72 72 65 63 74 6c  e set incorrectl
1a1b1 79 2e 20 52 65 74 75 72 6e 0a 20 20 2a 2a 20 74  y. Return.  ** t
1a1b2 72 75 65 20 66 6f 72 20 74 68 69 73 20 63 61 73  rue for this cas
1a1b3 65 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65  e to prevent the
1a1b4 20 61 73 73 65 72 74 28 29 20 69 6e 20 74 68 65   assert() in the
1a1b5 20 63 61 6c 6c 65 72 73 20 66 72 61 6d 65 0a 20   callers frame. 
1a1b6 20 2a 2a 20 66 72 6f 6d 20 66 61 69 6c 69 6e 67   ** from failing
1a1b7 2e 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28  .  */.  return (
1a1b8 20 76 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   v->db->mallocFa
1a1b9 69 6c 65 64 20 7c 7c 20 68 61 73 41 62 6f 72 74  iled || hasAbort
1a1ba 3d 3d 6d 61 79 41 62 6f 72 74 20 29 3b 0a 7d 0a  ==mayAbort );.}.
1a1bb 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1a1bc 5f 44 45 42 55 47 20 2d 20 74 68 65 20 73 71 6c  _DEBUG - the sql
1a1bd 69 74 65 33 41 73 73 65 72 74 4d 61 79 41 62 6f  ite3AssertMayAbo
1a1be 72 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  rt() function */
1a1bf 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74 68 72  ../*.** Loop thr
1a1c0 6f 75 67 68 20 74 68 65 20 70 72 6f 67 72 61 6d  ough the program
1a1c1 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50 32 20   looking for P2 
1a1c2 76 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20  values that are 
1a1c3 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f 6e 20 6a  negative.** on j
1a1c4 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ump instructions
1a1c5 2e 20 20 45 61 63 68 20 73 75 63 68 20 76 61 6c  .  Each such val
1a1c6 75 65 20 69 73 20 61 20 6c 61 62 65 6c 2e 20 20  ue is a label.  
1a1c7 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a 2a 20 6c  Resolve the.** l
1a1c8 61 62 65 6c 20 62 79 20 73 65 74 74 69 6e 67 20  abel by setting 
1a1c9 74 68 65 20 50 32 20 76 61 6c 75 65 20 74 6f 20  the P2 value to 
1a1ca 69 74 73 20 63 6f 72 72 65 63 74 20 6e 6f 6e 2d  its correct non-
1a1cb 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  zero value..**.*
1a1cc 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1a1cd 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 61 66  s called once af
1a1ce 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20  ter all opcodes 
1a1cf 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74  have been insert
1a1d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62  ed..**.** Variab
1a1d1 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73  le *pMaxFuncArgs
1a1d2 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6d   is set to the m
1a1d3 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20  aximum value of 
1a1d4 61 6e 79 20 50 32 20 61 72 67 75 6d 65 6e 74 20  any P2 argument 
1a1d5 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f 46 75 6e  .** to an OP_Fun
1a1d6 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 53 74 65  ction, OP_AggSte
1a1d7 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 65 72 20  p or OP_VFilter 
1a1d8 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 69 73 20  opcode. This is 
1a1d9 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 71 6c 69  used by .** sqli
1a1da 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
1a1db 28 29 20 74 6f 20 73 69 7a 65 20 74 68 65 20 56  () to size the V
1a1dc 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72 72 61  dbe.apArg[] arra
1a1dd 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4f 70 2e  y..**.** The Op.
1a1de 6f 70 66 6c 61 67 73 20 66 69 65 6c 64 20 69 73  opflags field is
1a1df 20 73 65 74 20 6f 6e 20 61 6c 6c 20 6f 70 63 6f   set on all opco
1a1e0 64 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  des..*/.static v
1a1e1 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61 6c  oid resolveP2Val
1a1e2 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ues(Vdbe *p, int
1a1e3 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29 7b   *pMaxFuncArgs){
1a1e4 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1a1e5 6e 4d 61 78 41 72 67 73 20 3d 20 2a 70 4d 61 78  nMaxArgs = *pMax
1a1e6 46 75 6e 63 41 72 67 73 3b 0a 20 20 4f 70 20 2a  FuncArgs;.  Op *
1a1e7 70 4f 70 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62  pOp;.  int *aLab
1a1e8 65 6c 20 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a  el = p->aLabel;.
1a1e9 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20    p->readOnly = 
1a1ea 31 3b 0a 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e  1;.  for(pOp=p->
1a1eb 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b  aOp, i=p->nOp-1;
1a1ec 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b   i>=0; i--, pOp+
1a1ed 2b 29 7b 0a 20 20 20 20 75 38 20 6f 70 63 6f 64  +){.    u8 opcod
1a1ee 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b  e = pOp->opcode;
1a1ef 0a 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 66 6c 61  ..    pOp->opfla
1a1f0 67 73 20 3d 20 73 71 6c 69 74 65 33 4f 70 63 6f  gs = sqlite3Opco
1a1f1 64 65 50 72 6f 70 65 72 74 79 5b 6f 70 63 6f 64  deProperty[opcod
1a1f2 65 5d 3b 0a 20 20 20 20 69 66 28 20 6f 70 63 6f  e];.    if( opco
1a1f3 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69 6f 6e 20  de==OP_Function 
1a1f4 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 67  || opcode==OP_Ag
1a1f5 67 53 74 65 70 20 29 7b 0a 20 20 20 20 20 20 69  gStep ){.      i
1a1f6 66 28 20 70 4f 70 2d 3e 70 35 3e 6e 4d 61 78 41  f( pOp->p5>nMaxA
1a1f7 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d  rgs ) nMaxArgs =
1a1f8 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 20 20 7d 65   pOp->p5;.    }e
1a1f9 6c 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d  lse if( opcode==
1a1fa 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 26  OP_Transaction &
1a1fb 26 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29 7b 0a  & pOp->p2!=0 ){.
1a1fc 20 20 20 20 20 20 70 2d 3e 72 65 61 64 4f 6e 6c        p->readOnl
1a1fd 79 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53  y = 0;.#ifndef S
1a1fe 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1a1ff 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73  ALTABLE.    }els
1a200 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50  e if( opcode==OP
1a201 5f 56 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20  _VUpdate ){.    
1a202 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e 4d    if( pOp->p2>nM
1a203 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67  axArgs ) nMaxArg
1a204 73 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 20  s = pOp->p2;.   
1a205 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f 64   }else if( opcod
1a206 65 3d 3d 4f 50 5f 56 46 69 6c 74 65 72 20 29 7b  e==OP_VFilter ){
1a207 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20  .      int n;.  
1a208 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e      assert( p->n
1a209 4f 70 20 2d 20 69 20 3e 3d 20 33 20 29 3b 0a 20  Op - i >= 3 );. 
1a20a 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1a20b 5b 2d 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  [-1].opcode==OP_
1a20c 49 6e 74 65 67 65 72 20 29 3b 0a 20 20 20 20 20  Integer );.     
1a20d 20 6e 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 31 3b   n = pOp[-1].p1;
1a20e 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 6e 4d 61  .      if( n>nMa
1a20f 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67 73  xArgs ) nMaxArgs
1a210 20 3d 20 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 20   = n;.#endif.   
1a211 20 7d 0a 0a 20 20 20 20 69 66 28 20 28 70 4f 70   }..    if( (pOp
1a212 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c  ->opflags & OPFL
1a213 47 5f 4a 55 4d 50 29 21 3d 30 20 26 26 20 70 4f  G_JUMP)!=0 && pO
1a214 70 2d 3e 70 32 3c 30 20 29 7b 0a 20 20 20 20 20  p->p2<0 ){.     
1a215 20 61 73 73 65 72 74 28 20 2d 31 2d 70 4f 70 2d   assert( -1-pOp-
1a216 3e 70 32 3c 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b  >p2<p->nLabel );
1a217 0a 20 20 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d  .      pOp->p2 =
1a218 20 61 4c 61 62 65 6c 5b 2d 31 2d 70 4f 70 2d 3e   aLabel[-1-pOp->
1a219 70 32 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  p2];.    }.  }. 
1a21a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
1a21b 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 29  ->db, p->aLabel)
1a21c 3b 0a 20 20 70 2d 3e 61 4c 61 62 65 6c 20 3d 20  ;.  p->aLabel = 
1a21d 30 3b 0a 0a 20 20 2a 70 4d 61 78 46 75 6e 63 41  0;..  *pMaxFuncA
1a21e 72 67 73 20 3d 20 6e 4d 61 78 41 72 67 73 3b 0a  rgs = nMaxArgs;.
1a21f 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1a220 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
1a221 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
1a222 69 6f 6e 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ion to be insert
1a223 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1a224 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1a225 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
1a226 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 61 73 73  (Vdbe *p){.  ass
1a227 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  ert( p->magic==V
1a228 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
1a229 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e 4f  ;.  return p->nO
1a22a 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  p;.}../*.** This
1a22b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
1a22c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
1a22d 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70 63 6f  he array of opco
1a22e 64 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  des associated w
1a22f 69 74 68 0a 2a 2a 20 74 68 65 20 56 64 62 65 20  ith.** the Vdbe 
1a230 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
1a231 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 49 74  rst argument. It
1a232 20 69 73 20 74 68 65 20 63 61 6c 6c 65 72 73 20   is the callers 
1a233 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a 2a  responsibility.*
1a234 2a 20 74 6f 20 61 72 72 61 6e 67 65 20 66 6f 72  * to arrange for
1a235 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61 72   the returned ar
1a236 72 61 79 20 74 6f 20 62 65 20 65 76 65 6e 74 75  ray to be eventu
1a237 61 6c 6c 79 20 66 72 65 65 64 20 75 73 69 6e 67  ally freed using
1a238 20 74 68 65 20 0a 2a 2a 20 76 64 62 65 46 72 65   the .** vdbeFre
1a239 65 4f 70 41 72 72 61 79 28 29 20 66 75 6e 63 74  eOpArray() funct
1a23a 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72  ion..**.** Befor
1a23b 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 6e  e returning, *pn
1a23c 4f 70 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  Op is set to the
1a23d 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   number of entri
1a23e 65 73 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e  es in the return
1a23f 65 64 0a 2a 2a 20 61 72 72 61 79 2e 20 41 6c 73  ed.** array. Als
1a240 6f 2c 20 2a 70 6e 4d 61 78 41 72 67 20 69 73 20  o, *pnMaxArg is 
1a241 73 65 74 20 74 6f 20 74 68 65 20 6c 61 72 67 65  set to the large
1a242 72 20 6f 66 20 69 74 73 20 63 75 72 72 65 6e 74  r of its current
1a243 20 76 61 6c 75 65 20 61 6e 64 20 0a 2a 2a 20 74   value and .** t
1a244 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
1a245 72 69 65 73 20 69 6e 20 74 68 65 20 56 64 62 65  ries in the Vdbe
1a246 2e 61 70 41 72 67 5b 5d 20 61 72 72 61 79 20 72  .apArg[] array r
1a247 65 71 75 69 72 65 64 20 74 6f 20 65 78 65 63 75  equired to execu
1a248 74 65 20 74 68 65 20 0a 2a 2a 20 72 65 74 75 72  te the .** retur
1a249 6e 65 64 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a  ned program..*/.
1a24a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56  SQLITE_PRIVATE V
1a24b 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64  dbeOp *sqlite3Vd
1a24c 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 56 64  beTakeOpArray(Vd
1a24d 62 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 4f 70  be *p, int *pnOp
1a24e 2c 20 69 6e 74 20 2a 70 6e 4d 61 78 41 72 67 29  , int *pnMaxArg)
1a24f 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f 70 20  {.  VdbeOp *aOp 
1a250 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 61 73 73 65  = p->aOp;.  asse
1a251 72 74 28 20 61 4f 70 20 26 26 20 21 70 2d 3e 64  rt( aOp && !p->d
1a252 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a253 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  );..  /* Check t
1a254 68 61 74 20 73 71 6c 69 74 65 33 56 64 62 65 55  hat sqlite3VdbeU
1a255 73 65 73 42 74 72 65 65 28 29 20 77 61 73 20 6e  sesBtree() was n
1a256 6f 74 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69  ot called on thi
1a257 73 20 56 4d 20 2a 2f 0a 20 20 61 73 73 65 72 74  s VM */.  assert
1a258 28 20 70 2d 3e 61 4d 75 74 65 78 2e 6e 4d 75 74  ( p->aMutex.nMut
1a259 65 78 3d 3d 30 20 29 3b 0a 0a 20 20 72 65 73 6f  ex==0 );..  reso
1a25a 6c 76 65 50 32 56 61 6c 75 65 73 28 70 2c 20 70  lveP2Values(p, p
1a25b 6e 4d 61 78 41 72 67 29 3b 0a 20 20 2a 70 6e 4f  nMaxArg);.  *pnO
1a25c 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 70 2d  p = p->nOp;.  p-
1a25d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 72 65 74 75  >aOp = 0;.  retu
1a25e 72 6e 20 61 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn aOp;.}../*.**
1a25f 20 41 64 64 20 61 20 77 68 6f 6c 65 20 6c 69 73   Add a whole lis
1a260 74 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 20  t of operations 
1a261 74 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  to the operation
1a262 20 73 74 61 63 6b 2e 20 20 52 65 74 75 72 6e 20   stack.  Return 
1a263 74 68 65 0a 2a 2a 20 61 64 64 72 65 73 73 20 6f  the.** address o
1a264 66 20 74 68 65 20 66 69 72 73 74 20 6f 70 65 72  f the first oper
1a265 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f 0a  ation added..*/.
1a266 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1a267 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
1a268 64 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70 2c  dOpList(Vdbe *p,
1a269 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f 70   int nOp, VdbeOp
1a26a 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70 29  List const *aOp)
1a26b 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20  {.  int addr;.  
1a26c 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
1a26d 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
1a26e 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f  T );.  if( p->nO
1a26f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f 70  p + nOp > p->nOp
1a270 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f 70 41  Alloc && growOpA
1a271 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 72  rray(p) ){.    r
1a272 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61  eturn 0;.  }.  a
1a273 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20  ddr = p->nOp;.  
1a274 69 66 28 20 41 4c 57 41 59 53 28 6e 4f 70 3e 30  if( ALWAYS(nOp>0
1a275 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  ) ){.    int i;.
1a276 20 20 20 20 56 64 62 65 4f 70 4c 69 73 74 20 63      VdbeOpList c
1a277 6f 6e 73 74 20 2a 70 49 6e 20 3d 20 61 4f 70 3b  onst *pIn = aOp;
1a278 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1a279 6e 4f 70 3b 20 69 2b 2b 2c 20 70 49 6e 2b 2b 29  nOp; i++, pIn++)
1a27a 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 20 3d  {.      int p2 =
1a27b 20 70 49 6e 2d 3e 70 32 3b 0a 20 20 20 20 20 20   pIn->p2;.      
1a27c 56 64 62 65 4f 70 20 2a 70 4f 75 74 20 3d 20 26  VdbeOp *pOut = &
1a27d 70 2d 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 3b 0a  p->aOp[i+addr];.
1a27e 20 20 20 20 20 20 70 4f 75 74 2d 3e 6f 70 63 6f        pOut->opco
1a27f 64 65 20 3d 20 70 49 6e 2d 3e 6f 70 63 6f 64 65  de = pIn->opcode
1a280 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 31  ;.      pOut->p1
1a281 20 3d 20 70 49 6e 2d 3e 70 31 3b 0a 20 20 20 20   = pIn->p1;.    
1a282 20 20 69 66 28 20 70 32 3c 30 20 26 26 20 28 73    if( p2<0 && (s
1a283 71 6c 69 74 65 33 4f 70 63 6f 64 65 50 72 6f 70  qlite3OpcodeProp
1a284 65 72 74 79 5b 70 4f 75 74 2d 3e 6f 70 63 6f 64  erty[pOut->opcod
1a285 65 5d 20 26 20 4f 50 46 4c 47 5f 4a 55 4d 50 29  e] & OPFLG_JUMP)
1a286 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  !=0 ){.        p
1a287 4f 75 74 2d 3e 70 32 20 3d 20 61 64 64 72 20 2b  Out->p2 = addr +
1a288 20 41 44 44 52 28 70 32 29 3b 0a 20 20 20 20 20   ADDR(p2);.     
1a289 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1a28a 70 4f 75 74 2d 3e 70 32 20 3d 20 70 32 3b 0a 20  pOut->p2 = p2;. 
1a28b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 75       }.      pOu
1a28c 74 2d 3e 70 33 20 3d 20 70 49 6e 2d 3e 70 33 3b  t->p3 = pIn->p3;
1a28d 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 34 74  .      pOut->p4t
1a28e 79 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44  ype = P4_NOTUSED
1a28f 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 34  ;.      pOut->p4
1a290 2e 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f  .p = 0;.      pO
1a291 75 74 2d 3e 70 35 20 3d 20 30 3b 0a 23 69 66 64  ut->p5 = 0;.#ifd
1a292 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1a293 20 20 20 20 20 20 70 4f 75 74 2d 3e 7a 43 6f 6d        pOut->zCom
1a294 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  ment = 0;.      
1a295 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 41  if( sqlite3VdbeA
1a296 64 64 6f 70 54 72 61 63 65 20 29 7b 0a 20 20 20  ddopTrace ){.   
1a297 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1a298 50 72 69 6e 74 4f 70 28 30 2c 20 69 2b 61 64 64  PrintOp(0, i+add
1a299 72 2c 20 26 70 2d 3e 61 4f 70 5b 69 2b 61 64 64  r, &p->aOp[i+add
1a29a 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  r]);.      }.#en
1a29b 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  dif.    }.    p-
1a29c 3e 6e 4f 70 20 2b 3d 20 6e 4f 70 3b 0a 20 20 7d  >nOp += nOp;.  }
1a29d 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a  .  return addr;.
1a29e 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
1a29f 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1a2a0 20 50 31 20 6f 70 65 72 61 6e 64 20 66 6f 72 20   P1 operand for 
1a2a1 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72  a specific instr
1a2a2 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20  uction..** This 
1a2a3 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75  routine is usefu
1a2a4 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70  l when a large p
1a2a5 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64  rogram is loaded
1a2a6 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69   from a.** stati
1a2a7 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 71  c array using sq
1a2a8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
1a2a9 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 74  st but we want t
1a2aa 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20  o make a.** few 
1a2ab 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f  minor changes to
1a2ac 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f   the program..*/
1a2ad 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a2ae 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1a2af 43 68 61 6e 67 65 50 31 28 56 64 62 65 20 2a 70  ChangeP1(Vdbe *p
1a2b0 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20  , int addr, int 
1a2b1 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  val){.  assert( 
1a2b2 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  p!=0 );.  assert
1a2b3 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20 20 69  ( addr>=0 );.  i
1a2b4 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 29  f( p->nOp>addr )
1a2b5 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64  {.    p->aOp[add
1a2b6 72 5d 2e 70 31 20 3d 20 76 61 6c 3b 0a 20 20 7d  r].p1 = val;.  }
1a2b7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
1a2b8 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1a2b9 65 20 50 32 20 6f 70 65 72 61 6e 64 20 66 6f 72  e P2 operand for
1a2ba 20 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74   a specific inst
1a2bb 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73  ruction..** This
1a2bc 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66   routine is usef
1a2bd 75 6c 20 66 6f 72 20 73 65 74 74 69 6e 67 20 61  ul for setting a
1a2be 20 6a 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f   jump destinatio
1a2bf 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1a2c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a2c1 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 56 64  3VdbeChangeP2(Vd
1a2c2 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c  be *p, int addr,
1a2c3 20 69 6e 74 20 76 61 6c 29 7b 0a 20 20 61 73 73   int val){.  ass
1a2c4 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61  ert( p!=0 );.  a
1a2c5 73 73 65 72 74 28 20 61 64 64 72 3e 3d 30 20 29  ssert( addr>=0 )
1a2c6 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 3e 61  ;.  if( p->nOp>a
1a2c7 64 64 72 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f  ddr ){.    p->aO
1a2c8 70 5b 61 64 64 72 5d 2e 70 32 20 3d 20 76 61 6c  p[addr].p2 = val
1a2c9 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ;.  }.}../*.** C
1a2ca 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20  hange the value 
1a2cb 6f 66 20 74 68 65 20 50 33 20 6f 70 65 72 61 6e  of the P3 operan
1a2cc 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69 63  d for a specific
1a2cd 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f   instruction..*/
1a2ce 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1a2cf 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1a2d0 43 68 61 6e 67 65 50 33 28 56 64 62 65 20 2a 70  ChangeP3(Vdbe *p
1a2d1 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20  , int addr, int 
1a2d2 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  val){.  assert( 
1a2d3 70 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  p!=0 );.  assert
1a2d4 28 20 61 64 64 72 3e 3d 30 20 29 3b 0a 20 20 69  ( addr>=0 );.  i
1a2d5 66 28 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 29  f( p->nOp>addr )
1a2d6 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64  {.    p->aOp[add
1a2d7 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a 20 20 7d  r].p3 = val;.  }
1a2d8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
1a2d9 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1a2da 65 20 50 35 20 6f 70 65 72 61 6e 64 20 66 6f 72  e P5 operand for
1a2db 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
1a2dc 6c 79 0a 2a 2a 20 61 64 64 65 64 20 6f 70 65 72  ly.** added oper
1a2dd 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
1a2de 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a2df 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1a2e0 35 28 56 64 62 65 20 2a 70 2c 20 75 38 20 76 61  5(Vdbe *p, u8 va
1a2e1 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 21  l){.  assert( p!
1a2e2 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  =0 );.  if( p->a
1a2e3 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  Op ){.    assert
1a2e4 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 20 20  ( p->nOp>0 );.  
1a2e5 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d    p->aOp[p->nOp-
1a2e6 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20 20 7d  1].p5 = val;.  }
1a2e7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
1a2e8 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e 64 20   the P2 operand 
1a2e9 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61  of instruction a
1a2ea 64 64 72 20 73 6f 20 74 68 61 74 20 69 74 20 70  ddr so that it p
1a2eb 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68 65 20  oints to.** the 
1a2ec 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6e  address of the n
1a2ed 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
1a2ee 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a 2f 0a  to be coded..*/.
1a2ef 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a2f0 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4a  oid sqlite3VdbeJ
1a2f1 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a 70 2c  umpHere(Vdbe *p,
1a2f2 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73 71   int addr){.  sq
1a2f3 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1a2f4 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e 6e 4f  2(p, addr, p->nO
1a2f5 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66  p);.}.../*.** If
1a2f6 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e 63 44   the input FuncD
1a2f7 65 66 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ef structure is 
1a2f8 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65 6e 20  ephemeral, then 
1a2f9 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a 2a 20  free it.  If.** 
1a2fa 74 68 65 20 46 75 6e 63 44 65 66 20 69 73 20 6e  the FuncDef is n
1a2fb 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74 68 65  ot ephermal, the
1a2fc 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2f  n do nothing..*/
1a2fd 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65  .static void fre
1a2fe 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69  eEphemeralFuncti
1a2ff 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  on(sqlite3 *db, 
1a300 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29 7b 0a  FuncDef *pDef){.
1a301 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 44 65    if( ALWAYS(pDe
1a302 66 29 20 26 26 20 28 70 44 65 66 2d 3e 66 6c 61  f) && (pDef->fla
1a303 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43  gs & SQLITE_FUNC
1a304 5f 45 50 48 45 4d 29 21 3d 30 20 29 7b 0a 20 20  _EPHEM)!=0 ){.  
1a305 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1a306 64 62 2c 20 70 44 65 66 29 3b 0a 20 20 7d 0a 7d  db, pDef);.  }.}
1a307 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
1a308 20 50 34 20 76 61 6c 75 65 20 69 66 20 6e 65 63   P4 value if nec
1a309 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  essary..*/.stati
1a30a 63 20 76 6f 69 64 20 66 72 65 65 50 34 28 73 71  c void freeP4(sq
1a30b 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 70  lite3 *db, int p
1a30c 34 74 79 70 65 2c 20 76 6f 69 64 20 2a 70 34 29  4type, void *p4)
1a30d 7b 0a 20 20 69 66 28 20 70 34 20 29 7b 0a 20 20  {.  if( p4 ){.  
1a30e 20 20 73 77 69 74 63 68 28 20 70 34 74 79 70 65    switch( p4type
1a30f 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20 50   ){.      case P
1a310 34 5f 52 45 41 4c 3a 0a 20 20 20 20 20 20 63 61  4_REAL:.      ca
1a311 73 65 20 50 34 5f 49 4e 54 36 34 3a 0a 20 20 20  se P4_INT64:.   
1a312 20 20 20 63 61 73 65 20 50 34 5f 4d 50 52 49 4e     case P4_MPRIN
1a313 54 46 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50  TF:.      case P
1a314 34 5f 44 59 4e 41 4d 49 43 3a 0a 20 20 20 20 20  4_DYNAMIC:.     
1a315 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f   case P4_KEYINFO
1a316 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  :.      case P4_
1a317 49 4e 54 41 52 52 41 59 3a 0a 20 20 20 20 20 20  INTARRAY:.      
1a318 63 61 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  case P4_KEYINFO_
1a319 48 41 4e 44 4f 46 46 3a 20 7b 0a 20 20 20 20 20  HANDOFF: {.     
1a31a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1a31b 28 64 62 2c 20 70 34 29 3b 0a 20 20 20 20 20 20  (db, p4);.      
1a31c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
1a31d 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 56  .      case P4_V
1a31e 44 42 45 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20  DBEFUNC: {.     
1a31f 20 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64     VdbeFunc *pVd
1a320 62 65 46 75 6e 63 20 3d 20 28 56 64 62 65 46 75  beFunc = (VdbeFu
1a321 6e 63 20 2a 29 70 34 3b 0a 20 20 20 20 20 20 20  nc *)p4;.       
1a322 20 66 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75   freeEphemeralFu
1a323 6e 63 74 69 6f 6e 28 64 62 2c 20 70 56 64 62 65  nction(db, pVdbe
1a324 46 75 6e 63 2d 3e 70 46 75 6e 63 29 3b 0a 20 20  Func->pFunc);.  
1a325 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1a326 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 70  eDeleteAuxData(p
1a327 56 64 62 65 46 75 6e 63 2c 20 30 29 3b 0a 20 20  VdbeFunc, 0);.  
1a328 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1a329 72 65 65 28 64 62 2c 20 70 56 64 62 65 46 75 6e  ree(db, pVdbeFun
1a32a 63 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  c);.        brea
1a32b 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
1a32c 20 63 61 73 65 20 50 34 5f 46 55 4e 43 44 45 46   case P4_FUNCDEF
1a32d 3a 20 7b 0a 20 20 20 20 20 20 20 20 66 72 65 65  : {.        free
1a32e 45 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f  EphemeralFunctio
1a32f 6e 28 64 62 2c 20 28 46 75 6e 63 44 65 66 2a 29  n(db, (FuncDef*)
1a330 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  p4);.        bre
1a331 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1a332 20 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b    case P4_MEM: {
1a333 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a334 56 61 6c 75 65 46 72 65 65 28 28 73 71 6c 69 74  ValueFree((sqlit
1a335 65 33 5f 76 61 6c 75 65 2a 29 70 34 29 3b 0a 20  e3_value*)p4);. 
1a336 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1a337 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
1a338 20 50 34 5f 56 54 41 42 20 3a 20 7b 0a 20 20 20   P4_VTAB : {.   
1a339 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
1a33a 55 6e 6c 6f 63 6b 28 28 56 54 61 62 6c 65 20 2a  Unlock((VTable *
1a33b 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72  )p4);.        br
1a33c 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
1a33d 20 20 20 63 61 73 65 20 50 34 5f 53 55 42 50 52     case P4_SUBPR
1a33e 4f 47 52 41 4d 20 3a 20 7b 0a 20 20 20 20 20 20  OGRAM : {.      
1a33f 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f    sqlite3VdbePro
1a340 67 72 61 6d 44 65 6c 65 74 65 28 64 62 2c 20 28  gramDelete(db, (
1a341 53 75 62 50 72 6f 67 72 61 6d 20 2a 29 70 34 2c  SubProgram *)p4,
1a342 20 31 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65   1);.        bre
1a343 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1a344 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  }.  }.}../*.** F
1a345 72 65 65 20 74 68 65 20 73 70 61 63 65 20 61 6c  ree the space al
1a346 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 4f 70 20  located for aOp 
1a347 61 6e 64 20 61 6e 79 20 70 34 20 76 61 6c 75 65  and any p4 value
1a348 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
1a349 74 68 65 0a 2a 2a 20 6f 70 63 6f 64 65 73 20 63  the.** opcodes c
1a34a 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 2e  ontained within.
1a34b 20 49 66 20 61 4f 70 20 69 73 20 6e 6f 74 20 4e   If aOp is not N
1a34c 55 4c 4c 20 69 74 20 69 73 20 61 73 73 75 6d 65  ULL it is assume
1a34d 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 0a 2a 2a  d to contain .**
1a34e 20 6e 4f 70 20 65 6e 74 72 69 65 73 2e 20 0a 2a   nOp entries. .*
1a34f 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 76 64  /.static void vd
1a350 62 65 46 72 65 65 4f 70 41 72 72 61 79 28 73 71  beFreeOpArray(sq
1a351 6c 69 74 65 33 20 2a 64 62 2c 20 4f 70 20 2a 61  lite3 *db, Op *a
1a352 4f 70 2c 20 69 6e 74 20 6e 4f 70 29 7b 0a 20 20  Op, int nOp){.  
1a353 69 66 28 20 61 4f 70 20 29 7b 0a 20 20 20 20 4f  if( aOp ){.    O
1a354 70 20 2a 70 4f 70 3b 0a 20 20 20 20 66 6f 72 28  p *pOp;.    for(
1a355 70 4f 70 3d 61 4f 70 3b 20 70 4f 70 3c 26 61 4f  pOp=aOp; pOp<&aO
1a356 70 5b 6e 4f 70 5d 3b 20 70 4f 70 2b 2b 29 7b 0a  p[nOp]; pOp++){.
1a357 20 20 20 20 20 20 66 72 65 65 50 34 28 64 62 2c        freeP4(db,
1a358 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f   pOp->p4type, pO
1a359 70 2d 3e 70 34 2e 70 29 3b 0a 23 69 66 64 65 66  p->p4.p);.#ifdef
1a35a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
1a35b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1a35c 65 28 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d 6d  e(db, pOp->zComm
1a35d 65 6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 20 20  ent);.#endif    
1a35e 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71   .    }.  }.  sq
1a35f 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1a360 61 4f 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  aOp);.}../*.** D
1a361 65 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66  ecrement the ref
1a362 2d 63 6f 75 6e 74 20 6f 6e 20 74 68 65 20 53 75  -count on the Su
1a363 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75  bProgram structu
1a364 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  re passed as the
1a365 0a 2a 2a 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  .** second argum
1a366 65 6e 74 2e 20 49 66 20 74 68 65 20 72 65 66 2d  ent. If the ref-
1a367 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65  count reaches ze
1a368 72 6f 2c 20 66 72 65 65 20 74 68 65 20 73 74 72  ro, free the str
1a369 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
1a36a 65 20 61 72 72 61 79 20 6f 66 20 56 44 42 45 20  e array of VDBE 
1a36b 6f 70 63 6f 64 65 73 20 73 74 6f 72 65 64 20 61  opcodes stored a
1a36c 73 20 53 75 62 50 72 6f 67 72 61 6d 2e 61 4f 70  s SubProgram.aOp
1a36d 20 69 73 20 66 72 65 65 64 20 69 66 0a 2a 2a 20   is freed if.** 
1a36e 65 69 74 68 65 72 20 74 68 65 20 72 65 66 2d 63  either the ref-c
1a36f 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65 72  ount reaches zer
1a370 6f 20 6f 72 20 70 61 72 61 6d 65 74 65 72 20 66  o or parameter f
1a371 72 65 65 6f 70 20 69 73 20 6e 6f 6e 2d 7a 65 72  reeop is non-zer
1a372 6f 2e 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 74  o..**.** Since t
1a373 68 65 20 61 72 72 61 79 20 6f 66 20 6f 70 63 6f  he array of opco
1a374 64 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  des pointed to b
1a375 79 20 53 75 62 50 72 6f 67 72 61 6d 2e 61 4f 70  y SubProgram.aOp
1a376 20 6d 61 79 20 64 69 72 65 63 74 6c 79 0a 2a 2a   may directly.**
1a377 20 6f 72 20 69 6e 64 69 72 65 63 74 6c 79 20 63   or indirectly c
1a378 6f 6e 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e  ontain a referen
1a379 63 65 20 74 6f 20 74 68 65 20 53 75 62 50 72 6f  ce to the SubPro
1a37a 67 72 61 6d 20 73 74 72 75 63 74 75 72 65 20 69  gram structure i
1a37b 74 73 65 6c 66 2e 0a 2a 2a 20 42 79 20 70 61 73  tself..** By pas
1a37c 73 69 6e 67 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20  sing a non-zero 
1a37d 66 72 65 65 6f 70 20 70 61 72 61 6d 65 74 65 72  freeop parameter
1a37e 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 61 79  , the caller may
1a37f 20 65 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c   ensure that all
1a380 0a 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d 20 73  .** SubProgram s
1a381 74 72 75 63 74 75 72 65 73 20 61 6e 64 20 74 68  tructures and th
1a382 65 69 72 20 61 4f 70 20 61 72 72 61 79 73 20 61  eir aOp arrays a
1a383 72 65 20 66 72 65 65 64 2c 20 65 76 65 6e 20 77  re freed, even w
1a384 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 61 72 65  hen there.** are
1a385 20 73 75 63 68 20 63 69 72 63 75 6c 61 72 20 72   such circular r
1a386 65 66 65 72 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51  eferences..*/.SQ
1a387 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1a388 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f  d sqlite3VdbePro
1a389 67 72 61 6d 44 65 6c 65 74 65 28 73 71 6c 69 74  gramDelete(sqlit
1a38a 65 33 20 2a 64 62 2c 20 53 75 62 50 72 6f 67 72  e3 *db, SubProgr
1a38b 61 6d 20 2a 70 2c 20 69 6e 74 20 66 72 65 65 6f  am *p, int freeo
1a38c 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  p){.  if( p ){. 
1a38d 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52     assert( p->nR
1a38e 65 66 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20  ef>0 );.    if( 
1a38f 66 72 65 65 6f 70 20 7c 7c 20 70 2d 3e 6e 52 65  freeop || p->nRe
1a390 66 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 4f 70  f==1 ){.      Op
1a391 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a   *aOp = p->aOp;.
1a392 20 20 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 30        p->aOp = 0
1a393 3b 0a 20 20 20 20 20 20 76 64 62 65 46 72 65 65  ;.      vdbeFree
1a394 4f 70 41 72 72 61 79 28 64 62 2c 20 61 4f 70 2c  OpArray(db, aOp,
1a395 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 20 20 20 20   p->nOp);.      
1a396 70 2d 3e 6e 4f 70 20 3d 20 30 3b 0a 20 20 20 20  p->nOp = 0;.    
1a397 7d 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b  }.    p->nRef--;
1a398 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 52 65 66  .    if( p->nRef
1a399 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
1a39a 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1a39b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
1a39c 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 4e 20  ./*.** Change N 
1a39d 6f 70 63 6f 64 65 73 20 73 74 61 72 74 69 6e 67  opcodes starting
1a39e 20 61 74 20 61 64 64 72 20 74 6f 20 4e 6f 2d 6f   at addr to No-o
1a39f 70 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ps..*/.SQLITE_PR
1a3a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a3a1 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f  e3VdbeChangeToNo
1a3a2 6f 70 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  op(Vdbe *p, int 
1a3a3 61 64 64 72 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  addr, int N){.  
1a3a4 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20  if( p->aOp ){.  
1a3a5 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20    VdbeOp *pOp = 
1a3a6 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20  &p->aOp[addr];. 
1a3a7 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
1a3a8 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 68 69 6c   p->db;.    whil
1a3a9 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20 20 20  e( N-- ){.      
1a3aa 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e  freeP4(db, pOp->
1a3ab 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e  p4type, pOp->p4.
1a3ac 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74  p);.      memset
1a3ad 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28  (pOp, 0, sizeof(
1a3ae 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20  pOp[0]));.      
1a3af 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50  pOp->opcode = OP
1a3b0 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70 4f 70  _Noop;.      pOp
1a3b1 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ++;.    }.  }.}.
1a3b2 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
1a3b3 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50  e value of the P
1a3b4 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20  4 operand for a 
1a3b5 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63  specific instruc
1a3b6 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  tion..** This ro
1a3b7 75 74 69 6e 65 20 69 73 20 75 73 65 66 75 6c 20  utine is useful 
1a3b8 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70 72 6f  when a large pro
1a3b9 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64 20 66  gram is loaded f
1a3ba 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69 63 20  rom a.** static 
1a3bb 61 72 72 61 79 20 75 73 69 6e 67 20 73 71 6c 69  array using sqli
1a3bc 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
1a3bd 20 62 75 74 20 77 65 20 77 61 6e 74 20 74 6f 20   but we want to 
1a3be 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20 6d 69  make a.** few mi
1a3bf 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20 74  nor changes to t
1a3c0 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a  he program..**.*
1a3c1 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e 20 74  * If n>=0 then t
1a3c2 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 69 73  he P4 operand is
1a3c3 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e 69 6e   dynamic, meanin
1a3c4 67 20 74 68 61 74 20 61 20 63 6f 70 79 20 6f 66  g that a copy of
1a3c5 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 69  .** the string i
1a3c6 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65 6d 6f  s made into memo
1a3c7 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
1a3c8 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
1a3c9 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66  )..** A value of
1a3ca 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f 70 79   n==0 means copy
1a3cb 20 62 79 74 65 73 20 6f 66 20 7a 50 34 20 75 70   bytes of zP4 up
1a3cc 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64 69 6e   to and includin
1a3cd 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6e  g the.** first n
1a3ce 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20 6e 3e  ull byte.  If n>
1a3cf 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b 31 20  0 then copy n+1 
1a3d0 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0a 2a 2a  bytes of zP4..**
1a3d1 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b 45 59  .** If n==P4_KEY
1a3d2 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20 74 68  INFO it means th
1a3d3 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f 69 6e  at zP4 is a poin
1a3d4 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f  ter to a KeyInfo
1a3d5 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 41   structure..** A
1a3d6 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 6f 66   copy is made of
1a3d7 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72   the KeyInfo str
1a3d8 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65 6d 6f  ucture into memo
1a3d9 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
1a3da 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  .** sqlite3_mall
1a3db 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65 64 20  oc, to be freed 
1a3dc 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73  when the Vdbe is
1a3dd 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6e   finalized..** n
1a3de 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  ==P4_KEYINFO_HAN
1a3df 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73 20 74  DOFF indicates t
1a3e0 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73 20 74  hat zP4 points t
1a3e1 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  o a KeyInfo stru
1a3e2 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65 64 20  cture.** stored 
1a3e3 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 74  in memory that t
1a3e4 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62  he caller has ob
1a3e5 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
1a3e6 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68 65 20  te3_malloc. The 
1a3e7 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c  .** caller shoul
1a3e8 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65 20 61  d not free the a
1a3e9 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 77 69  llocation, it wi
1a3ea 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68 65 6e  ll be freed when
1a3eb 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a 2a 20   the Vdbe is.** 
1a3ec 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20 0a 2a  finalized..** .*
1a3ed 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20 6f  * Other values o
1a3ee 66 20 6e 20 28 50 34 5f 53 54 41 54 49 43 2c 20  f n (P4_STATIC, 
1a3ef 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 63 2e 29  P4_COLLSEQ etc.)
1a3f0 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 7a   indicate that z
1a3f1 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20  P4 points.** to 
1a3f2 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74 72 75  a string or stru
1a3f3 63 74 75 72 65 20 74 68 61 74 20 69 73 20 67 75  cture that is gu
1a3f4 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78 69 73  aranteed to exis
1a3f5 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65 74 69  t for the lifeti
1a3f6 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56 64 62  me of.** the Vdb
1a3f7 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61 73 65  e. In these case
1a3f8 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20 63 6f  s we can just co
1a3f9 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72 2e 0a  py the pointer..
1a3fa 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c 30 20  **.** If addr<0 
1a3fb 74 68 65 6e 20 63 68 61 6e 67 65 20 50 34 20 6f  then change P4 o
1a3fc 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  n the most recen
1a3fd 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69 6e 73  tly inserted ins
1a3fe 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  truction..*/.SQL
1a3ff 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1a400 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1a401 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20 69 6e  geP4(Vdbe *p, in
1a402 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20 63 68  t addr, const ch
1a403 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e 29 7b  ar *zP4, int n){
1a404 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 73 71  .  Op *pOp;.  sq
1a405 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 61 73 73  lite3 *db;.  ass
1a406 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 64  ert( p!=0 );.  d
1a407 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73  b = p->db;.  ass
1a408 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  ert( p->magic==V
1a409 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
1a40a 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70 3d 3d  ;.  if( p->aOp==
1a40b 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  0 || db->mallocF
1a40c 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69 66 20  ailed ){.    if 
1a40d 28 20 6e 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 20  ( n!=P4_KEYINFO 
1a40e 26 26 20 6e 21 3d 50 34 5f 56 54 41 42 20 29 20  && n!=P4_VTAB ) 
1a40f 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28 64  {.      freeP4(d
1a410 62 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 2a 28 63  b, n, (void*)*(c
1a411 68 61 72 2a 2a 29 26 7a 50 34 29 3b 0a 20 20 20  har**)&zP4);.   
1a412 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20   }.    return;. 
1a413 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e   }.  assert( p->
1a414 6e 4f 70 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  nOp>0 );.  asser
1a415 74 28 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 20 29  t( addr<p->nOp )
1a416 3b 0a 20 20 69 66 28 20 61 64 64 72 3c 30 20 29  ;.  if( addr<0 )
1a417 7b 0a 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e  {.    addr = p->
1a418 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 70  nOp - 1;.  }.  p
1a419 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64  Op = &p->aOp[add
1a41a 72 5d 3b 0a 20 20 66 72 65 65 50 34 28 64 62 2c  r];.  freeP4(db,
1a41b 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f   pOp->p4type, pO
1a41c 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 70 4f 70 2d  p->p4.p);.  pOp-
1a41d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 69 66 28  >p4.p = 0;.  if(
1a41e 20 6e 3d 3d 50 34 5f 49 4e 54 33 32 20 29 7b 0a   n==P4_INT32 ){.
1a41f 20 20 20 20 2f 2a 20 4e 6f 74 65 3a 20 74 68 69      /* Note: thi
1a420 73 20 63 61 73 74 20 69 73 20 73 61 66 65 2c 20  s cast is safe, 
1a421 62 65 63 61 75 73 65 20 74 68 65 20 6f 72 69 67  because the orig
1a422 69 6e 20 64 61 74 61 20 70 6f 69 6e 74 20 77 61  in data point wa
1a423 73 20 61 6e 20 69 6e 74 0a 20 20 20 20 2a 2a 20  s an int.    ** 
1a424 74 68 61 74 20 77 61 73 20 63 61 73 74 20 74 6f  that was cast to
1a425 20 61 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   a (const char *
1a426 29 2e 20 2a 2f 0a 20 20 20 20 70 4f 70 2d 3e 70  ). */.    pOp->p
1a427 34 2e 69 20 3d 20 53 51 4c 49 54 45 5f 50 54 52  4.i = SQLITE_PTR
1a428 5f 54 4f 5f 49 4e 54 28 7a 50 34 29 3b 0a 20 20  _TO_INT(zP4);.  
1a429 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20    pOp->p4type = 
1a42a 50 34 5f 49 4e 54 33 32 3b 0a 20 20 7d 65 6c 73  P4_INT32;.  }els
1a42b 65 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a  e if( zP4==0 ){.
1a42c 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20      pOp->p4.p = 
1a42d 30 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  0;.    pOp->p4ty
1a42e 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b  pe = P4_NOTUSED;
1a42f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d  .  }else if( n==
1a430 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20  P4_KEYINFO ){.  
1a431 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
1a432 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69  nfo;.    int nFi
1a433 65 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20  eld, nByte;..   
1a434 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49   nField = ((KeyI
1a435 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c  nfo*)zP4)->nFiel
1a436 64 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73  d;.    nByte = s
1a437 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29  izeof(*pKeyInfo)
1a438 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69   + (nField-1)*si
1a439 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61  zeof(pKeyInfo->a
1a43a 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c  Coll[0]) + nFiel
1a43b 64 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20  d;.    pKeyInfo 
1a43c 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
1a43d 20 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f   nByte );.    pO
1a43e 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d  p->p4.pKeyInfo =
1a43f 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69   pKeyInfo;.    i
1a440 66 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20  f( pKeyInfo ){. 
1a441 20 20 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72       u8 *aSortOr
1a442 64 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  der;.      memcp
1a443 79 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 34 2c  y(pKeyInfo, zP4,
1a444 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61   nByte);.      a
1a445 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79  SortOrder = pKey
1a446 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
1a447 3b 0a 20 20 20 20 20 20 69 66 28 20 61 53 6f 72  ;.      if( aSor
1a448 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20  tOrder ){.      
1a449 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72    pKeyInfo->aSor
1a44a 74 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e  tOrder = (unsign
1a44b 65 64 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e  ed char*)&pKeyIn
1a44c 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64  fo->aColl[nField
1a44d 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ];.        memcp
1a44e 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  y(pKeyInfo->aSor
1a44f 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64  tOrder, aSortOrd
1a450 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20  er, nField);.   
1a451 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 2d 3e     }.      pOp->
1a452 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49  p4type = P4_KEYI
1a453 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  NFO;.    }else{.
1a454 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c        p->db->mal
1a455 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
1a456 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65       pOp->p4type
1a457 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20   = P4_NOTUSED;. 
1a458 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
1a459 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48   n==P4_KEYINFO_H
1a45a 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f  ANDOFF ){.    pO
1a45b 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a  p->p4.p = (void*
1a45c 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  )zP4;.    pOp->p
1a45d 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e  4type = P4_KEYIN
1a45e 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  FO;.  }else if( 
1a45f 6e 3d 3d 50 34 5f 56 54 41 42 20 29 7b 0a 20 20  n==P4_VTAB ){.  
1a460 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28 76    pOp->p4.p = (v
1a461 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70 4f  oid*)zP4;.    pO
1a462 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56  p->p4type = P4_V
1a463 54 41 42 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  TAB;.    sqlite3
1a464 56 74 61 62 4c 6f 63 6b 28 28 56 54 61 62 6c 65  VtabLock((VTable
1a465 20 2a 29 7a 50 34 29 3b 0a 20 20 20 20 61 73 73   *)zP4);.    ass
1a466 65 72 74 28 20 28 28 56 54 61 62 6c 65 20 2a 29  ert( ((VTable *)
1a467 7a 50 34 29 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20  zP4)->db==p->db 
1a468 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e  );.  }else if( n
1a469 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70  <0 ){.    pOp->p
1a46a 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34  4.p = (void*)zP4
1a46b 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70  ;.    pOp->p4typ
1a46c 65 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72  e = (signed char
1a46d 29 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  )n;.  }else{.   
1a46e 20 69 66 28 20 6e 3d 3d 30 20 29 20 6e 20 3d 20   if( n==0 ) n = 
1a46f 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1a470 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  zP4);.    pOp->p
1a471 34 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 53  4.z = sqlite3DbS
1a472 74 72 4e 44 75 70 28 70 2d 3e 64 62 2c 20 7a 50  trNDup(p->db, zP
1a473 34 2c 20 6e 29 3b 0a 20 20 20 20 70 4f 70 2d 3e  4, n);.    pOp->
1a474 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 4e 41  p4type = P4_DYNA
1a475 4d 49 43 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  MIC;.  }.}..#ifn
1a476 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a  def NDEBUG./*.**
1a477 20 43 68 61 6e 67 65 20 74 68 65 20 63 6f 6d 6d   Change the comm
1a478 65 6e 74 20 6f 6e 20 74 68 65 20 74 68 65 20 6d  ent on the the m
1a479 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 63 6f 64  ost recently cod
1a47a 65 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  ed instruction. 
1a47b 20 4f 72 0a 2a 2a 20 69 6e 73 65 72 74 20 61 20   Or.** insert a 
1a47c 4e 6f 2d 6f 70 20 61 6e 64 20 61 64 64 20 74 68  No-op and add th
1a47d 65 20 63 6f 6d 6d 65 6e 74 20 74 6f 20 74 68 61  e comment to tha
1a47e 74 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69 6f  t new instructio
1a47f 6e 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 61 6b 65  n.  This.** make
1a480 73 20 74 68 65 20 63 6f 64 65 20 65 61 73 69 65  s the code easie
1a481 72 20 74 6f 20 72 65 61 64 20 64 75 72 69 6e 67  r to read during
1a482 20 64 65 62 75 67 67 69 6e 67 2e 20 20 4e 6f 6e   debugging.  Non
1a483 65 20 6f 66 20 74 68 69 73 20 68 61 70 70 65 6e  e of this happen
1a484 73 0a 2a 2a 20 69 6e 20 61 20 70 72 6f 64 75 63  s.** in a produc
1a485 74 69 6f 6e 20 62 75 69 6c 64 2e 0a 2a 2f 0a 53  tion build..*/.S
1a486 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1a487 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f  id sqlite3VdbeCo
1a488 6d 6d 65 6e 74 28 56 64 62 65 20 2a 70 2c 20 63  mment(Vdbe *p, c
1a489 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
1a48a 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  at, ...){.  va_l
1a48b 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70  ist ap;.  if( !p
1a48c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
1a48d 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c  ert( p->nOp>0 ||
1a48e 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20   p->aOp==0 );.  
1a48f 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d  assert( p->aOp==
1a490 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e  0 || p->aOp[p->n
1a491 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d  Op-1].zComment==
1a492 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c  0 || p->db->mall
1a493 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66  ocFailed );.  if
1a494 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20  ( p->nOp ){.    
1a495 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e  char **pz = &p->
1a496 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43  aOp[p->nOp-1].zC
1a497 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73  omment;.    va_s
1a498 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
1a499 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
1a49a 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29  Free(p->db, *pz)
1a49b 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69  ;.    *pz = sqli
1a49c 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64  te3VMPrintf(p->d
1a49d 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b  b, zFormat, ap);
1a49e 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  .    va_end(ap);
1a49f 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  .  }.}.SQLITE_PR
1a4a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a4a1 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e  e3VdbeNoopCommen
1a4a2 74 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74  t(Vdbe *p, const
1a4a3 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
1a4a4 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
1a4a5 61 70 3b 0a 20 20 69 66 28 20 21 70 20 29 20 72  ap;.  if( !p ) r
1a4a6 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
1a4a7 56 64 62 65 41 64 64 4f 70 30 28 70 2c 20 4f 50  VdbeAddOp0(p, OP
1a4a8 5f 4e 6f 6f 70 29 3b 0a 20 20 61 73 73 65 72 74  _Noop);.  assert
1a4a9 28 20 70 2d 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d  ( p->nOp>0 || p-
1a4aa 3e 61 4f 70 3d 3d 30 20 29 3b 0a 20 20 61 73 73  >aOp==0 );.  ass
1a4ab 65 72 74 28 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c  ert( p->aOp==0 |
1a4ac 7c 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d  | p->aOp[p->nOp-
1a4ad 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c  1].zComment==0 |
1a4ae 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  | p->db->mallocF
1a4af 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70  ailed );.  if( p
1a4b0 2d 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 63 68 61  ->nOp ){.    cha
1a4b1 72 20 2a 2a 70 7a 20 3d 20 26 70 2d 3e 61 4f 70  r **pz = &p->aOp
1a4b2 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d  [p->nOp-1].zComm
1a4b3 65 6e 74 3b 0a 20 20 20 20 76 61 5f 73 74 61 72  ent;.    va_star
1a4b4 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
1a4b5 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1a4b6 65 28 70 2d 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20  e(p->db, *pz);. 
1a4b7 20 20 20 2a 70 7a 20 3d 20 73 71 6c 69 74 65 33     *pz = sqlite3
1a4b8 56 4d 50 72 69 6e 74 66 28 70 2d 3e 64 62 2c 20  VMPrintf(p->db, 
1a4b9 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
1a4ba 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
1a4bb 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 4e  }.}.#endif  /* N
1a4bc 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  DEBUG */../*.** 
1a4bd 52 65 74 75 72 6e 20 74 68 65 20 6f 70 63 6f 64  Return the opcod
1a4be 65 20 66 6f 72 20 61 20 67 69 76 65 6e 20 61 64  e for a given ad
1a4bf 64 72 65 73 73 2e 20 20 49 66 20 74 68 65 20 61  dress.  If the a
1a4c0 64 64 72 65 73 73 20 69 73 20 2d 31 2c 20 74 68  ddress is -1, th
1a4c1 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68 65  en.** return the
1a4c2 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69   most recently i
1a4c3 6e 73 65 72 74 65 64 20 6f 70 63 6f 64 65 2e 0a  nserted opcode..
1a4c4 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72  **.** If a memor
1a4c5 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
1a4c6 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64 20  or has occurred 
1a4c7 70 72 69 6f 72 20 74 6f 20 74 68 65 20 63 61 6c  prior to the cal
1a4c8 6c 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a 2a 20  ling of this.** 
1a4c9 72 6f 75 74 69 6e 65 2c 20 74 68 65 6e 20 61 20  routine, then a 
1a4ca 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 64 75 6d  pointer to a dum
1a4cb 6d 79 20 56 64 62 65 4f 70 20 77 69 6c 6c 20 62  my VdbeOp will b
1a4cc 65 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 61  e returned.  Tha
1a4cd 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 72  t opcode.** is r
1a4ce 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74  eadable and writ
1a4cf 61 62 6c 65 2c 20 62 75 74 20 69 74 20 68 61 73  able, but it has
1a4d0 20 6e 6f 20 65 66 66 65 63 74 2e 20 20 54 68 65   no effect.  The
1a4d1 20 72 65 74 75 72 6e 20 6f 66 20 61 20 64 75 6d   return of a dum
1a4d2 6d 79 0a 2a 2a 20 6f 70 63 6f 64 65 20 61 6c 6c  my.** opcode all
1a4d3 6f 77 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  ows the call to 
1a4d4 63 6f 6e 74 69 6e 75 65 20 66 75 6e 63 74 69 6f  continue functio
1a4d5 6e 69 6e 67 20 61 66 74 65 72 20 61 20 4f 4f 4d  ning after a OOM
1a4d6 20 66 61 75 6c 74 20 77 69 74 68 6f 75 74 0a 2a   fault without.*
1a4d7 2a 20 68 61 76 69 6e 67 20 74 6f 20 63 68 65 63  * having to chec
1a4d8 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
1a4d9 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 69 73  return from this
1a4da 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 76 61   routine is a va
1a4db 6c 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  lid pointer..**.
1a4dc 2a 2a 20 41 62 6f 75 74 20 74 68 65 20 23 69 66  ** About the #if
1a4dd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1a4de 54 52 41 43 45 3a 20 20 4e 6f 72 6d 61 6c 6c 79  TRACE:  Normally
1a4df 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
1a4e0 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 0a 2a  s never called.*
1a4e1 2a 20 75 6e 6c 65 73 73 20 70 2d 3e 6e 4f 70 3e  * unless p->nOp>
1a4e2 30 2e 20 20 54 68 69 73 20 69 73 20 62 65 63 61  0.  This is beca
1a4e3 75 73 65 20 69 6e 20 74 68 65 20 61 62 73 65 6e  use in the absen
1a4e4 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49  se of SQLITE_OMI
1a4e5 54 5f 54 52 41 43 45 2c 0a 2a 2a 20 61 6e 20 4f  T_TRACE,.** an O
1a4e6 50 5f 54 72 61 63 65 20 69 6e 73 74 72 75 63 74  P_Trace instruct
1a4e7 69 6f 6e 20 69 73 20 61 6c 77 61 79 73 20 69 6e  ion is always in
1a4e8 73 65 72 74 65 64 20 62 79 20 73 71 6c 69 74 65  serted by sqlite
1a4e9 33 56 64 62 65 47 65 74 28 29 20 61 73 20 73 6f  3VdbeGet() as so
1a4ea 6f 6e 20 61 73 0a 2a 2a 20 61 20 6e 65 77 20 56  on as.** a new V
1a4eb 44 42 45 20 69 73 20 63 72 65 61 74 65 64 2e 20  DBE is created. 
1a4ec 20 53 6f 20 77 65 20 61 72 65 20 66 72 65 65 20   So we are free 
1a4ed 74 6f 20 73 65 74 20 61 64 64 72 20 74 6f 20 70  to set addr to p
1a4ee 2d 3e 6e 4f 70 2d 31 20 77 69 74 68 6f 75 74 0a  ->nOp-1 without.
1a4ef 2a 2a 20 68 61 76 69 6e 67 20 74 6f 20 64 6f 75  ** having to dou
1a4f0 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20 6d 61 6b  ble-check to mak
1a4f1 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20  e sure that the 
1a4f2 72 65 73 75 6c 74 20 69 73 20 6e 6f 6e 2d 6e 65  result is non-ne
1a4f3 67 61 74 69 76 65 2e 20 42 75 74 0a 2a 2a 20 69  gative. But.** i
1a4f4 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
1a4f5 41 43 45 20 69 73 20 64 65 66 69 6e 65 64 2c 20  ACE is defined, 
1a4f6 74 68 65 20 4f 50 5f 54 72 61 63 65 20 69 73 20  the OP_Trace is 
1a4f7 6f 6d 69 74 74 65 64 20 61 6e 64 20 77 65 20 64  omitted and we d
1a4f8 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 63 68 65  o need to.** che
1a4f9 63 6b 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ck the value of 
1a4fa 70 2d 3e 6e 4f 70 2d 31 20 62 65 66 6f 72 65 20  p->nOp-1 before 
1a4fb 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2f 0a 53  continuing..*/.S
1a4fc 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
1a4fd 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62  beOp *sqlite3Vdb
1a4fe 65 47 65 74 4f 70 28 56 64 62 65 20 2a 70 2c 20  eGetOp(Vdbe *p, 
1a4ff 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 73 74 61  int addr){.  sta
1a500 74 69 63 20 56 64 62 65 4f 70 20 64 75 6d 6d 79  tic VdbeOp dummy
1a501 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d  ;.  assert( p->m
1a502 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
1a503 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20 61  _INIT );.  if( a
1a504 64 64 72 3c 30 20 29 7b 0a 23 69 66 64 65 66 20  ddr<0 ){.#ifdef 
1a505 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1a506 45 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 4f 70  E.    if( p->nOp
1a507 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 26 64 75  ==0 ) return &du
1a508 6d 6d 79 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  mmy;.#endif.    
1a509 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20  addr = p->nOp - 
1a50a 31 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  1;.  }.  assert(
1a50b 20 28 61 64 64 72 3e 3d 30 20 26 26 20 61 64 64   (addr>=0 && add
1a50c 72 3c 70 2d 3e 6e 4f 70 29 20 7c 7c 20 70 2d 3e  r<p->nOp) || p->
1a50d 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1a50e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d   );.  if( p->db-
1a50f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
1a510 0a 20 20 20 20 72 65 74 75 72 6e 20 26 64 75 6d  .    return &dum
1a511 6d 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  my;.  }else{.   
1a512 20 72 65 74 75 72 6e 20 26 70 2d 3e 61 4f 70 5b   return &p->aOp[
1a513 61 64 64 72 5d 3b 0a 20 20 7d 0a 7d 0a 0a 23 69  addr];.  }.}..#i
1a514 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
1a515 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 29 20  E_OMIT_EXPLAIN) 
1a516 7c 7c 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42  || !defined(NDEB
1a517 55 47 29 20 5c 0a 20 20 20 20 20 7c 7c 20 64 65  UG) \.     || de
1a518 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49  fined(VDBE_PROFI
1a519 4c 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  LE) || defined(S
1a51a 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a  QLITE_DEBUG)./*.
1a51b 2a 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72  ** Compute a str
1a51c 69 6e 67 20 74 68 61 74 20 64 65 73 63 72 69 62  ing that describ
1a51d 65 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65  es the P4 parame
1a51e 74 65 72 20 66 6f 72 20 61 6e 20 6f 70 63 6f 64  ter for an opcod
1a51f 65 2e 0a 2a 2a 20 55 73 65 20 7a 54 65 6d 70 20  e..** Use zTemp 
1a520 66 6f 72 20 61 6e 79 20 72 65 71 75 69 72 65 64  for any required
1a521 20 74 65 6d 70 6f 72 61 72 79 20 62 75 66 66 65   temporary buffe
1a522 72 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74  r space..*/.stat
1a523 69 63 20 63 68 61 72 20 2a 64 69 73 70 6c 61 79  ic char *display
1a524 50 34 28 4f 70 20 2a 70 4f 70 2c 20 63 68 61 72  P4(Op *pOp, char
1a525 20 2a 7a 54 65 6d 70 2c 20 69 6e 74 20 6e 54 65   *zTemp, int nTe
1a526 6d 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34  mp){.  char *zP4
1a527 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 61 73 73 65   = zTemp;.  asse
1a528 72 74 28 20 6e 54 65 6d 70 3e 3d 32 30 20 29 3b  rt( nTemp>=20 );
1a529 0a 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e  .  switch( pOp->
1a52a 70 34 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61  p4type ){.    ca
1a52b 73 65 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54  se P4_KEYINFO_ST
1a52c 41 54 49 43 3a 0a 20 20 20 20 63 61 73 65 20 50  ATIC:.    case P
1a52d 34 5f 4b 45 59 49 4e 46 4f 3a 20 7b 0a 20 20 20  4_KEYINFO: {.   
1a52e 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20     int i, j;.   
1a52f 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
1a530 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Info = pOp->p4.p
1a531 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73  KeyInfo;.      s
1a532 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a533 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 6b  nTemp, zTemp, "k
1a534 65 79 69 6e 66 6f 28 25 64 22 2c 20 70 4b 65 79  eyinfo(%d", pKey
1a535 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 29 3b 0a 20  Info->nField);. 
1a536 20 20 20 20 20 69 20 3d 20 73 71 6c 69 74 65 33       i = sqlite3
1a537 53 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 29 3b  Strlen30(zTemp);
1a538 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  .      for(j=0; 
1a539 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  j<pKeyInfo->nFie
1a53a 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ld; j++){.      
1a53b 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
1a53c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f   = pKeyInfo->aCo
1a53d 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69  ll[j];.        i
1a53e 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
1a53f 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71        int n = sq
1a540 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 43  lite3Strlen30(pC
1a541 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  oll->zName);.   
1a542 20 20 20 20 20 20 20 69 66 28 20 69 2b 6e 3e 6e         if( i+n>n
1a543 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20  Temp-6 ){.      
1a544 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54        memcpy(&zT
1a545 65 6d 70 5b 69 5d 2c 22 2c 2e 2e 2e 22 2c 34 29  emp[i],",...",4)
1a546 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72  ;.            br
1a547 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  eak;.          }
1a548 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70  .          zTemp
1a549 5b 69 2b 2b 5d 20 3d 20 27 2c 27 3b 0a 20 20 20  [i++] = ',';.   
1a54a 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49         if( pKeyI
1a54b 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  nfo->aSortOrder 
1a54c 26 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f  && pKeyInfo->aSo
1a54d 72 74 4f 72 64 65 72 5b 6a 5d 20 29 7b 0a 20 20  rtOrder[j] ){.  
1a54e 20 20 20 20 20 20 20 20 20 20 7a 54 65 6d 70 5b            zTemp[
1a54f 69 2b 2b 5d 20 3d 20 27 2d 27 3b 0a 20 20 20 20  i++] = '-';.    
1a550 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1a551 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b    memcpy(&zTemp[
1a552 69 5d 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  i], pColl->zName
1a553 2c 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20  ,n+1);.         
1a554 20 69 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20   i += n;.       
1a555 20 7d 65 6c 73 65 20 69 66 28 20 69 2b 34 3c 6e   }else if( i+4<n
1a556 54 65 6d 70 2d 36 20 29 7b 0a 20 20 20 20 20 20  Temp-6 ){.      
1a557 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d      memcpy(&zTem
1a558 70 5b 69 5d 2c 22 2c 6e 69 6c 22 2c 34 29 3b 0a  p[i],",nil",4);.
1a559 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 34            i += 4
1a55a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1a55b 20 20 7d 0a 20 20 20 20 20 20 7a 54 65 6d 70 5b    }.      zTemp[
1a55c 69 2b 2b 5d 20 3d 20 27 29 27 3b 0a 20 20 20 20  i++] = ')';.    
1a55d 20 20 7a 54 65 6d 70 5b 69 5d 20 3d 20 30 3b 0a    zTemp[i] = 0;.
1a55e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3c        assert( i<
1a55f 6e 54 65 6d 70 20 29 3b 0a 20 20 20 20 20 20 62  nTemp );.      b
1a560 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1a561 63 61 73 65 20 50 34 5f 43 4f 4c 4c 53 45 51 3a  case P4_COLLSEQ:
1a562 20 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71   {.      CollSeq
1a563 20 2a 70 43 6f 6c 6c 20 3d 20 70 4f 70 2d 3e 70   *pColl = pOp->p
1a564 34 2e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 73  4.pColl;.      s
1a565 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a566 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 63  nTemp, zTemp, "c
1a567 6f 6c 6c 73 65 71 28 25 2e 32 30 73 29 22 2c 20  ollseq(%.20s)", 
1a568 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pColl->zName);. 
1a569 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a56a 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 46 55  }.    case P4_FU
1a56b 4e 43 44 45 46 3a 20 7b 0a 20 20 20 20 20 20 46  NCDEF: {.      F
1a56c 75 6e 63 44 65 66 20 2a 70 44 65 66 20 3d 20 70  uncDef *pDef = p
1a56d 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20  Op->p4.pFunc;.  
1a56e 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1a56f 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
1a570 70 2c 20 22 25 73 28 25 64 29 22 2c 20 70 44 65  p, "%s(%d)", pDe
1a571 66 2d 3e 7a 4e 61 6d 65 2c 20 70 44 65 66 2d 3e  f->zName, pDef->
1a572 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 62 72 65  nArg);.      bre
1a573 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1a574 73 65 20 50 34 5f 49 4e 54 36 34 3a 20 7b 0a 20  se P4_INT64: {. 
1a575 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
1a576 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65  rintf(nTemp, zTe
1a577 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 2a 70 4f 70  mp, "%lld", *pOp
1a578 2d 3e 70 34 2e 70 49 36 34 29 3b 0a 20 20 20 20  ->p4.pI64);.    
1a579 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1a57a 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 33 32     case P4_INT32
1a57b 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
1a57c 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
1a57d 2c 20 7a 54 65 6d 70 2c 20 22 25 64 22 2c 20 70  , zTemp, "%d", p
1a57e 4f 70 2d 3e 70 34 2e 69 29 3b 0a 20 20 20 20 20  Op->p4.i);.     
1a57f 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1a580 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a 20    case P4_REAL: 
1a581 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
1a582 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
1a583 7a 54 65 6d 70 2c 20 22 25 2e 31 36 67 22 2c 20  zTemp, "%.16g", 
1a584 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 29 3b  *pOp->p4.pReal);
1a585 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1a586 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f    }.    case P4_
1a587 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20 4d 65 6d  MEM: {.      Mem
1a588 20 2a 70 4d 65 6d 20 3d 20 70 4f 70 2d 3e 70 34   *pMem = pOp->p4
1a589 2e 70 4d 65 6d 3b 0a 20 20 20 20 20 20 61 73 73  .pMem;.      ass
1a58a 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  ert( (pMem->flag
1a58b 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
1a58c 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4d   );.      if( pM
1a58d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
1a58e 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  Str ){.        z
1a58f 50 34 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 20 20  P4 = pMem->z;.  
1a590 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4d      }else if( pM
1a591 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  em->flags & MEM_
1a592 49 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Int ){.        s
1a593 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a594 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25  nTemp, zTemp, "%
1a595 6c 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29  lld", pMem->u.i)
1a596 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
1a597 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
1a598 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
1a599 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1a59a 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d  intf(nTemp, zTem
1a59b 70 2c 20 22 25 2e 31 36 67 22 2c 20 70 4d 65 6d  p, "%.16g", pMem
1a59c 2d 3e 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ->r);.      }els
1a59d 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e{.        asser
1a59e 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  t( pMem->flags &
1a59f 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20   MEM_Blob );.   
1a5a0 20 20 20 20 20 7a 50 34 20 3d 20 22 28 62 6c 6f       zP4 = "(blo
1a5a1 62 29 22 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  b)";.      }.   
1a5a2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1a5a3 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1a5a4 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1a5a5 0a 20 20 20 20 63 61 73 65 20 50 34 5f 56 54 41  .    case P4_VTA
1a5a6 42 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  B: {.      sqlit
1a5a7 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d  e3_vtab *pVtab =
1a5a8 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d 3e   pOp->p4.pVtab->
1a5a9 70 56 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c  pVtab;.      sql
1a5aa 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
1a5ab 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 76 74 61  emp, zTemp, "vta
1a5ac 62 3a 25 70 3a 25 70 22 2c 20 70 56 74 61 62 2c  b:%p:%p", pVtab,
1a5ad 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 29   pVtab->pModule)
1a5ae 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1a5af 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
1a5b0 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 41 59  case P4_INTARRAY
1a5b1 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
1a5b2 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
1a5b3 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74 61 72 72  , zTemp, "intarr
1a5b4 61 79 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ay");.      brea
1a5b5 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
1a5b6 65 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 3a  e P4_SUBPROGRAM:
1a5b7 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
1a5b8 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c  _snprintf(nTemp,
1a5b9 20 7a 54 65 6d 70 2c 20 22 70 72 6f 67 72 61 6d   zTemp, "program
1a5ba 22 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ");.      break;
1a5bb 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
1a5bc 6c 74 3a 20 7b 0a 20 20 20 20 20 20 7a 50 34 20  lt: {.      zP4 
1a5bd 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20  = pOp->p4.z;.   
1a5be 20 20 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b     if( zP4==0 ){
1a5bf 0a 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 7a  .        zP4 = z
1a5c0 54 65 6d 70 3b 0a 20 20 20 20 20 20 20 20 7a 54  Temp;.        zT
1a5c1 65 6d 70 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20  emp[0] = 0;.    
1a5c2 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
1a5c3 61 73 73 65 72 74 28 20 7a 50 34 21 3d 30 20 29  assert( zP4!=0 )
1a5c4 3b 0a 20 20 72 65 74 75 72 6e 20 7a 50 34 3b 0a  ;.  return zP4;.
1a5c5 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
1a5c6 44 65 63 6c 61 72 65 20 74 6f 20 74 68 65 20 56  Declare to the V
1a5c7 64 62 65 20 74 68 61 74 20 74 68 65 20 42 54 72  dbe that the BTr
1a5c8 65 65 20 6f 62 6a 65 63 74 20 61 74 20 64 62 2d  ee object at db-
1a5c9 3e 61 44 62 5b 69 5d 20 69 73 20 75 73 65 64 2e  >aDb[i] is used.
1a5ca 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a5cb 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
1a5cc 64 62 65 55 73 65 73 42 74 72 65 65 28 56 64 62  dbeUsesBtree(Vdb
1a5cd 65 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20  e *p, int i){.  
1a5ce 69 6e 74 20 6d 61 73 6b 3b 0a 20 20 61 73 73 65  int mask;.  asse
1a5cf 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1a5d0 3e 64 62 2d 3e 6e 44 62 20 26 26 20 69 3c 73 69  >db->nDb && i<si
1a5d1 7a 65 6f 66 28 75 33 32 29 2a 38 20 29 3b 0a 20  zeof(u32)*8 );. 
1a5d2 20 61 73 73 65 72 74 28 20 69 3c 28 69 6e 74 29   assert( i<(int)
1a5d3 73 69 7a 65 6f 66 28 70 2d 3e 62 74 72 65 65 4d  sizeof(p->btreeM
1a5d4 61 73 6b 29 2a 38 20 29 3b 0a 20 20 6d 61 73 6b  ask)*8 );.  mask
1a5d5 20 3d 20 28 28 75 33 32 29 31 29 3c 3c 69 3b 0a   = ((u32)1)<<i;.
1a5d6 20 20 69 66 28 20 28 70 2d 3e 62 74 72 65 65 4d    if( (p->btreeM
1a5d7 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29  ask & mask)==0 )
1a5d8 7b 0a 20 20 20 20 70 2d 3e 62 74 72 65 65 4d 61  {.    p->btreeMa
1a5d9 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20  sk |= mask;.    
1a5da 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
1a5db 78 41 72 72 61 79 49 6e 73 65 72 74 28 26 70 2d  xArrayInsert(&p-
1a5dc 3e 61 4d 75 74 65 78 2c 20 70 2d 3e 64 62 2d 3e  >aMutex, p->db->
1a5dd 61 44 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 7d  aDb[i].pBt);.  }
1a5de 0a 7d 0a 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  .}...#if defined
1a5df 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c  (VDBE_PROFILE) |
1a5e0 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
1a5e1 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 50 72  _DEBUG)./*.** Pr
1a5e2 69 6e 74 20 61 20 73 69 6e 67 6c 65 20 6f 70 63  int a single opc
1a5e3 6f 64 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ode.  This routi
1a5e4 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 64  ne is used for d
1a5e5 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a  ebugging only..*
1a5e6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a5e7 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1a5e8 65 50 72 69 6e 74 4f 70 28 46 49 4c 45 20 2a 70  ePrintOp(FILE *p
1a5e9 4f 75 74 2c 20 69 6e 74 20 70 63 2c 20 4f 70 20  Out, int pc, Op 
1a5ea 2a 70 4f 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a  *pOp){.  char *z
1a5eb 50 34 3b 0a 20 20 63 68 61 72 20 7a 50 74 72 5b  P4;.  char zPtr[
1a5ec 35 30 5d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f  50];.  static co
1a5ed 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
1a5ee 74 31 20 3d 20 22 25 34 64 20 25 2d 31 33 73 20  t1 = "%4d %-13s 
1a5ef 25 34 64 20 25 34 64 20 25 34 64 20 25 2d 34 73  %4d %4d %4d %-4s
1a5f0 20 25 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 20 69   %.2X %s\n";.  i
1a5f1 66 28 20 70 4f 75 74 3d 3d 30 20 29 20 70 4f 75  f( pOut==0 ) pOu
1a5f2 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7a 50  t = stdout;.  zP
1a5f3 34 20 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f  4 = displayP4(pO
1a5f4 70 2c 20 7a 50 74 72 2c 20 73 69 7a 65 6f 66 28  p, zPtr, sizeof(
1a5f5 7a 50 74 72 29 29 3b 0a 20 20 66 70 72 69 6e 74  zPtr));.  fprint
1a5f6 66 28 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 74 31  f(pOut, zFormat1
1a5f7 2c 20 70 63 2c 20 0a 20 20 20 20 20 20 73 71 6c  , pc, .      sql
1a5f8 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70  ite3OpcodeName(p
1a5f9 4f 70 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 4f 70  Op->opcode), pOp
1a5fa 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 70  ->p1, pOp->p2, p
1a5fb 4f 70 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 4f 70  Op->p3, zP4, pOp
1a5fc 2d 3e 70 35 2c 0a 23 69 66 64 65 66 20 53 51 4c  ->p5,.#ifdef SQL
1a5fd 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20  ITE_DEBUG.      
1a5fe 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3f 20  pOp->zComment ? 
1a5ff 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3a 20  pOp->zComment : 
1a600 22 22 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 22  "".#else.      "
1a601 22 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 20  ".#endif.  );.  
1a602 66 66 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d 0a  fflush(pOut);.}.
1a603 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
1a604 6c 65 61 73 65 20 61 6e 20 61 72 72 61 79 20 6f  lease an array o
1a605 66 20 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74 73  f N Mem elements
1a606 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1a607 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28  releaseMemArray(
1a608 4d 65 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a  Mem *p, int N){.
1a609 20 20 69 66 28 20 70 20 26 26 20 4e 20 29 7b 0a    if( p && N ){.
1a60a 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 0a 20      Mem *pEnd;. 
1a60b 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
1a60c 20 70 2d 3e 64 62 3b 0a 20 20 20 20 75 38 20 6d   p->db;.    u8 m
1a60d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d 20 64  alloc_failed = d
1a60e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b  b->mallocFailed;
1a60f 0a 20 20 20 20 66 6f 72 28 70 45 6e 64 3d 26 70  .    for(pEnd=&p
1a610 5b 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b 2b  [N]; p<pEnd; p++
1a611 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
1a612 20 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64 20 7c   (&p[1])==pEnd |
1a613 7c 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 5d 2e  | p[0].db==p[1].
1a614 64 62 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  db );..      /* 
1a615 54 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 72 65  This block is re
1a616 61 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e 65 64 20  ally an inlined 
1a617 76 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74  version of sqlit
1a618 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1a619 28 29 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74  ().      ** that
1a61a 20 74 61 6b 65 73 20 61 64 76 61 6e 74 61 67 65   takes advantage
1a61b 20 6f 66 20 74 68 65 20 66 61 63 74 20 74 68 61   of the fact tha
1a61c 74 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c  t the memory cel
1a61d 6c 20 76 61 6c 75 65 20 69 73 20 0a 20 20 20 20  l value is .    
1a61e 20 20 2a 2a 20 62 65 69 6e 67 20 73 65 74 20 74    ** being set t
1a61f 6f 20 4e 55 4c 4c 20 61 66 74 65 72 20 72 65 6c  o NULL after rel
1a620 65 61 73 69 6e 67 20 61 6e 79 20 64 79 6e 61 6d  easing any dynam
1a621 69 63 20 72 65 73 6f 75 72 63 65 73 2e 0a 20 20  ic resources..  
1a622 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
1a623 54 68 65 20 6a 75 73 74 69 66 69 63 61 74 69 6f  The justificatio
1a624 6e 20 66 6f 72 20 64 75 70 6c 69 63 61 74 69 6e  n for duplicatin
1a625 67 20 63 6f 64 65 20 69 73 20 74 68 61 74 20 61  g code is that a
1a626 63 63 6f 72 64 69 6e 67 20 74 6f 20 0a 20 20 20  ccording to .   
1a627 20 20 20 2a 2a 20 63 61 6c 6c 67 72 69 6e 64 2c     ** callgrind,
1a628 20 74 68 69 73 20 63 61 75 73 65 73 20 61 20 63   this causes a c
1a629 65 72 74 61 69 6e 20 74 65 73 74 20 63 61 73 65  ertain test case
1a62a 20 74 6f 20 68 69 74 20 74 68 65 20 43 50 55 20   to hit the CPU 
1a62b 34 2e 37 20 0a 20 20 20 20 20 20 2a 2a 20 70 65  4.7 .      ** pe
1a62c 72 63 65 6e 74 20 6c 65 73 73 20 28 78 38 36 20  rcent less (x86 
1a62d 6c 69 6e 75 78 2c 20 67 63 63 20 76 65 72 73 69  linux, gcc versi
1a62e 6f 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 29 20 74  on 4.1.2, -O6) t
1a62f 68 61 6e 20 69 66 20 0a 20 20 20 20 20 20 2a 2a  han if .      **
1a630 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 6c 65 61   sqlite3MemRelea
1a631 73 65 28 29 20 77 65 72 65 20 63 61 6c 6c 65 64  se() were called
1a632 20 66 72 6f 6d 20 68 65 72 65 2e 20 57 69 74 68   from here. With
1a633 20 2d 4f 32 2c 20 74 68 69 73 20 6a 75 6d 70 73   -O2, this jumps
1a634 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 36 2e 36  .      ** to 6.6
1a635 20 70 65 72 63 65 6e 74 2e 20 54 68 65 20 74 65   percent. The te
1a636 73 74 20 63 61 73 65 20 69 73 20 69 6e 73 65 72  st case is inser
1a637 74 69 6e 67 20 31 30 30 30 20 72 6f 77 73 20 69  ting 1000 rows i
1a638 6e 74 6f 20 61 20 74 61 62 6c 65 20 0a 20 20 20  nto a table .   
1a639 20 20 20 2a 2a 20 77 69 74 68 20 6e 6f 20 69 6e     ** with no in
1a63a 64 65 78 65 73 20 75 73 69 6e 67 20 61 20 73 69  dexes using a si
1a63b 6e 67 6c 65 20 70 72 65 70 61 72 65 64 20 49 4e  ngle prepared IN
1a63c 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2c 20  SERT statement, 
1a63d 62 69 6e 64 28 29 20 0a 20 20 20 20 20 20 2a 2a  bind() .      **
1a63e 20 61 6e 64 20 72 65 73 65 74 28 29 2e 20 49 6e   and reset(). In
1a63f 73 65 72 74 73 20 61 72 65 20 67 72 6f 75 70 65  serts are groupe
1a640 64 20 69 6e 74 6f 20 61 20 74 72 61 6e 73 61 63  d into a transac
1a641 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  tion..      */. 
1a642 20 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67       if( p->flag
1a643 73 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44  s&(MEM_Agg|MEM_D
1a644 79 6e 7c 4d 45 4d 5f 46 72 61 6d 65 7c 4d 45 4d  yn|MEM_Frame|MEM
1a645 5f 52 6f 77 53 65 74 29 20 29 7b 0a 20 20 20 20  _RowSet) ){.    
1a646 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1a647 65 6d 52 65 6c 65 61 73 65 28 70 29 3b 0a 20 20  emRelease(p);.  
1a648 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d      }else if( p-
1a649 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  >zMalloc ){.    
1a64a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1a64b 65 28 64 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63  e(db, p->zMalloc
1a64c 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 7a 4d  );.        p->zM
1a64d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20 20  alloc = 0;.     
1a64e 20 7d 0a 0a 20 20 20 20 20 20 70 2d 3e 66 6c 61   }..      p->fla
1a64f 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1a650 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6d 61 6c     }.    db->mal
1a651 6c 6f 63 46 61 69 6c 65 64 20 3d 20 6d 61 6c 6c  locFailed = mall
1a652 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 7d 0a 7d  oc_failed;.  }.}
1a653 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
1a654 20 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63   VdbeFrame objec
1a655 74 20 61 6e 64 20 69 74 73 20 63 6f 6e 74 65 6e  t and its conten
1a656 74 73 2e 20 56 64 62 65 46 72 61 6d 65 20 6f 62  ts. VdbeFrame ob
1a657 6a 65 63 74 73 20 61 72 65 0a 2a 2a 20 61 6c 6c  jects are.** all
1a658 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 4f 50  ocated by the OP
1a659 5f 50 72 6f 67 72 61 6d 20 6f 70 63 6f 64 65 20  _Program opcode 
1a65a 69 6e 20 73 71 6c 69 74 65 33 56 64 62 65 45 78  in sqlite3VdbeEx
1a65b 65 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ec()..*/.SQLITE_
1a65c 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a65d 69 74 65 33 56 64 62 65 46 72 61 6d 65 44 65 6c  ite3VdbeFrameDel
1a65e 65 74 65 28 56 64 62 65 46 72 61 6d 65 20 2a 70  ete(VdbeFrame *p
1a65f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65  ){.  int i;.  Me
1a660 6d 20 2a 61 4d 65 6d 20 3d 20 56 64 62 65 46 72  m *aMem = VdbeFr
1a661 61 6d 65 4d 65 6d 28 70 29 3b 0a 20 20 56 64 62  ameMem(p);.  Vdb
1a662 65 43 75 72 73 6f 72 20 2a 2a 61 70 43 73 72 20  eCursor **apCsr 
1a663 3d 20 28 56 64 62 65 43 75 72 73 6f 72 20 2a 2a  = (VdbeCursor **
1a664 29 26 61 4d 65 6d 5b 70 2d 3e 6e 43 68 69 6c 64  )&aMem[p->nChild
1a665 4d 65 6d 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  Mem];.  for(i=0;
1a666 20 69 3c 70 2d 3e 6e 43 68 69 6c 64 43 73 72 3b   i<p->nChildCsr;
1a667 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
1a668 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72  e3VdbeFreeCursor
1a669 28 70 2d 3e 76 2c 20 61 70 43 73 72 5b 69 5d 29  (p->v, apCsr[i])
1a66a 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65 4d  ;.  }.  releaseM
1a66b 65 6d 41 72 72 61 79 28 61 4d 65 6d 2c 20 70 2d  emArray(aMem, p-
1a66c 3e 6e 43 68 69 6c 64 4d 65 6d 29 3b 0a 20 20 73  >nChildMem);.  s
1a66d 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e  qlite3DbFree(p->
1a66e 76 2d 3e 64 62 2c 20 70 29 3b 0a 7d 0a 0a 23 69  v->db, p);.}..#i
1a66f 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1a670 54 5f 45 58 50 4c 41 49 4e 0a 2f 2a 0a 2a 2a 20  T_EXPLAIN./*.** 
1a671 47 69 76 65 20 61 20 6c 69 73 74 69 6e 67 20 6f  Give a listing o
1a672 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20 69 6e  f the program in
1a673 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63   the virtual mac
1a674 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  hine..**.** The 
1a675 69 6e 74 65 72 66 61 63 65 20 69 73 20 74 68 65  interface is the
1a676 20 73 61 6d 65 20 61 73 20 73 71 6c 69 74 65 33   same as sqlite3
1a677 56 64 62 65 45 78 65 63 28 29 2e 20 20 42 75 74  VdbeExec().  But
1a678 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 72   instead of.** r
1a679 75 6e 6e 69 6e 67 20 74 68 65 20 63 6f 64 65 2c  unning the code,
1a67a 20 69 74 20 69 6e 76 6f 6b 65 73 20 74 68 65 20   it invokes the 
1a67b 63 61 6c 6c 62 61 63 6b 20 6f 6e 63 65 20 66 6f  callback once fo
1a67c 72 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69  r each instructi
1a67d 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 66 65 61 74  on..** This feat
1a67e 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 69  ure is used to i
1a67f 6d 70 6c 65 6d 65 6e 74 20 22 45 58 50 4c 41 49  mplement "EXPLAI
1a680 4e 22 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70  N"..**.** When p
1a681 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 2c 20 65 61  ->explain==1, ea
1a682 63 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  ch instruction i
1a683 73 20 6c 69 73 74 65 64 2e 20 20 57 68 65 6e 0a  s listed.  When.
1a684 2a 2a 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32  ** p->explain==2
1a685 2c 20 6f 6e 6c 79 20 4f 50 5f 45 78 70 6c 61 69  , only OP_Explai
1a686 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 61  n instructions a
1a687 72 65 20 6c 69 73 74 65 64 20 61 6e 64 20 74 68  re listed and th
1a688 65 73 65 0a 2a 2a 20 61 72 65 20 73 68 6f 77 6e  ese.** are shown
1a689 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20   in a different 
1a68a 66 6f 72 6d 61 74 2e 20 20 70 2d 3e 65 78 70 6c  format.  p->expl
1a68b 61 69 6e 3d 3d 32 20 69 73 20 75 73 65 64 20 74  ain==2 is used t
1a68c 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 45  o implement.** E
1a68d 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41  XPLAIN QUERY PLA
1a68e 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  N..*/.SQLITE_PRI
1a68f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1a690 56 64 62 65 4c 69 73 74 28 0a 20 20 56 64 62 65  VdbeList(.  Vdbe
1a691 20 2a 70 20 20 20 20 20 20 20 20 20 20 20 20 20   *p             
1a692 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
1a693 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 52  E */.){.  int nR
1a694 6f 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ow;             
1a695 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a696 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
1a697 66 20 72 6f 77 73 20 74 6f 20 72 65 74 75 72 6e  f rows to return
1a698 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62 20 3d   */.  int nSub =
1a699 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1a69a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1a69b 6d 62 65 72 20 6f 66 20 73 75 62 2d 76 64 62 65  mber of sub-vdbe
1a69c 73 20 73 65 65 6e 20 73 6f 20 66 61 72 20 2a 2f  s seen so far */
1a69d 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a  .  SubProgram **
1a69e 61 70 53 75 62 20 3d 20 30 3b 20 20 20 20 20 20  apSub = 0;      
1a69f 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79          /* Array
1a6a0 20 6f 66 20 73 75 62 2d 76 64 62 65 73 20 2a 2f   of sub-vdbes */
1a6a1 0a 20 20 4d 65 6d 20 2a 70 53 75 62 20 3d 20 30  .  Mem *pSub = 0
1a6a2 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
1a6a3 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  = p->db;.  int i
1a6a4 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
1a6a5 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20 2a 70  ITE_OK;.  Mem *p
1a6a6 4d 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74  Mem = p->pResult
1a6a7 53 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 31  Set = &p->aMem[1
1a6a8 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d  ];..  assert( p-
1a6a9 3e 65 78 70 6c 61 69 6e 20 29 3b 0a 20 20 61 73  >explain );.  as
1a6aa 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
1a6ab 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29  VDBE_MAGIC_RUN )
1a6ac 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
1a6ad 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41  magic==SQLITE_MA
1a6ae 47 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 61 73  GIC_BUSY );.  as
1a6af 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c  sert( p->rc==SQL
1a6b0 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d  ITE_OK || p->rc=
1a6b1 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20  =SQLITE_BUSY || 
1a6b2 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  p->rc==SQLITE_NO
1a6b3 4d 45 4d 20 29 3b 0a 0a 20 20 2f 2a 20 45 76 65  MEM );..  /* Eve
1a6b4 6e 20 74 68 6f 75 67 68 20 74 68 69 73 20 6f 70  n though this op
1a6b5 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74 20 75 73  code does not us
1a6b6 65 20 64 79 6e 61 6d 69 63 20 73 74 72 69 6e 67  e dynamic string
1a6b7 73 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72  s for.  ** the r
1a6b8 65 73 75 6c 74 2c 20 72 65 73 75 6c 74 20 63 6f  esult, result co
1a6b9 6c 75 6d 6e 73 20 6d 61 79 20 62 65 63 6f 6d 65  lumns may become
1a6ba 20 64 79 6e 61 6d 69 63 20 69 66 20 74 68 65 20   dynamic if the 
1a6bb 75 73 65 72 20 63 61 6c 6c 73 0a 20 20 2a 2a 20  user calls.  ** 
1a6bc 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
1a6bd 65 78 74 31 36 28 29 2c 20 63 61 75 73 69 6e 67  ext16(), causing
1a6be 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74   a translation t
1a6bf 6f 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e  o UTF-16 encodin
1a6c0 67 2e 0a 20 20 2a 2f 0a 20 20 72 65 6c 65 61 73  g..  */.  releas
1a6c1 65 4d 65 6d 41 72 72 61 79 28 70 4d 65 6d 2c 20  eMemArray(pMem, 
1a6c2 38 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 72 63  8);..  if( p->rc
1a6c3 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
1a6c4 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61  {.    /* This ha
1a6c5 70 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f  ppens if a mallo
1a6c6 63 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c  c() inside a cal
1a6c7 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  l to sqlite3_col
1a6c8 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20  umn_text() or.  
1a6c9 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c    ** sqlite3_col
1a6ca 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69  umn_text16() fai
1a6cb 6c 65 64 2e 20 20 2a 2f 0a 20 20 20 20 64 62 2d  led.  */.    db-
1a6cc 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
1a6cd 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  1;.    return SQ
1a6ce 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
1a6cf 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
1a6d0 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
1a6d1 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c 6c 20   rows that will 
1a6d2 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  be returned by t
1a6d3 68 69 73 20 0a 20 20 2a 2a 20 45 58 50 4c 41 49  his .  ** EXPLAI
1a6d4 4e 20 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20  N program.  */. 
1a6d5 20 6e 52 6f 77 20 3d 20 70 2d 3e 6e 4f 70 3b 0a   nRow = p->nOp;.
1a6d6 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e    if( p->explain
1a6d7 3d 3d 31 20 29 7b 0a 20 20 20 20 70 53 75 62 20  ==1 ){.    pSub 
1a6d8 3d 20 26 70 2d 3e 61 4d 65 6d 5b 39 5d 3b 0a 20  = &p->aMem[9];. 
1a6d9 20 20 20 69 66 28 20 70 53 75 62 2d 3e 66 6c 61     if( pSub->fla
1a6da 67 73 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20  gs&MEM_Blob ){. 
1a6db 20 20 20 20 20 6e 53 75 62 20 3d 20 70 53 75 62       nSub = pSub
1a6dc 2d 3e 6e 2f 73 69 7a 65 6f 66 28 56 64 62 65 2a  ->n/sizeof(Vdbe*
1a6dd 29 3b 0a 20 20 20 20 20 20 61 70 53 75 62 20 3d  );.      apSub =
1a6de 20 28 53 75 62 50 72 6f 67 72 61 6d 20 2a 2a 29   (SubProgram **)
1a6df 70 53 75 62 2d 3e 7a 3b 0a 20 20 20 20 7d 0a 20  pSub->z;.    }. 
1a6e0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53     for(i=0; i<nS
1a6e1 75 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ub; i++){.      
1a6e2 6e 52 6f 77 20 2b 3d 20 61 70 53 75 62 5b 69 5d  nRow += apSub[i]
1a6e3 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 0a 20 20 7d  ->nOp;.    }.  }
1a6e4 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20 3d 20  ..  do{.    i = 
1a6e5 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68 69 6c  p->pc++;.  }whil
1a6e6 65 28 20 69 3c 6e 52 6f 77 20 26 26 20 70 2d 3e  e( i<nRow && p->
1a6e7 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26 20 70 2d  explain==2 && p-
1a6e8 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 21 3d  >aOp[i].opcode!=
1a6e9 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b 0a 20 20  OP_Explain );.  
1a6ea 69 66 28 20 69 3e 3d 6e 52 6f 77 20 29 7b 0a 20  if( i>=nRow ){. 
1a6eb 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54     p->rc = SQLIT
1a6ec 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d 20 53  E_OK;.    rc = S
1a6ed 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 65  QLITE_DONE;.  }e
1a6ee 6c 73 65 20 69 66 28 20 64 62 2d 3e 75 31 2e 69  lse if( db->u1.i
1a6ef 73 49 6e 74 65 72 72 75 70 74 65 64 20 29 7b 0a  sInterrupted ){.
1a6f0 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49      p->rc = SQLI
1a6f1 54 45 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20  TE_INTERRUPT;.  
1a6f2 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
1a6f3 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ROR;.    sqlite3
1a6f4 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1a6f5 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c  rrMsg, db, "%s",
1a6f6 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 70   sqlite3ErrStr(p
1a6f7 2d 3e 72 63 29 29 3b 0a 20 20 7d 65 6c 73 65 7b  ->rc));.  }else{
1a6f8 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20  .    char *z;.  
1a6f9 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20 69    Op *pOp;.    i
1a6fa 66 28 20 69 3c 70 2d 3e 6e 4f 70 20 29 7b 0a 20  f( i<p->nOp ){. 
1a6fb 20 20 20 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61       pOp = &p->a
1a6fc 4f 70 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65  Op[i];.    }else
1a6fd 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  {.      int j;. 
1a6fe 20 20 20 20 20 69 20 2d 3d 20 70 2d 3e 6e 4f 70       i -= p->nOp
1a6ff 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ;.      for(j=0;
1a700 20 69 3e 3d 61 70 53 75 62 5b 6a 5d 2d 3e 6e 4f   i>=apSub[j]->nO
1a701 70 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  p; j++){.       
1a702 20 69 20 2d 3d 20 61 70 53 75 62 5b 6a 5d 2d 3e   i -= apSub[j]->
1a703 6e 4f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  nOp;.      }.   
1a704 20 20 20 70 4f 70 20 3d 20 26 61 70 53 75 62 5b     pOp = &apSub[
1a705 6a 5d 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20 20  j]->aOp[i];.    
1a706 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70  }.    if( p->exp
1a707 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20  lain==1 ){.     
1a708 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1a709 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d  EM_Int;.      pM
1a70a 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
1a70b 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20  E_INTEGER;.     
1a70c 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 69 3b 20   pMem->u.i = i; 
1a70d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a70e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a70f 2a 20 50 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65  * Program counte
1a710 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2b  r */.      pMem+
1a711 2b 3b 0a 20 20 0a 20 20 20 20 20 20 70 4d 65 6d  +;.  .      pMem
1a712 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53 74  ->flags = MEM_St
1a713 61 74 69 63 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d  atic|MEM_Str|MEM
1a714 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65  _Term;.      pMe
1a715 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 73 71  m->z = (char*)sq
1a716 6c 69 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28  lite3OpcodeName(
1a717 70 4f 70 2d 3e 6f 70 63 6f 64 65 29 3b 20 20 2f  pOp->opcode);  /
1a718 2a 20 4f 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20  * Opcode */.    
1a719 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1a71a 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4d  z!=0 );.      pM
1a71b 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65 33 53  em->n = sqlite3S
1a71c 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29  trlen30(pMem->z)
1a71d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79  ;.      pMem->ty
1a71e 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54  pe = SQLITE_TEXT
1a71f 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e  ;.      pMem->en
1a720 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
1a721 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a  .      pMem++;..
1a722 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70        if( pOp->p
1a723 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50 52 4f  4type==P4_SUBPRO
1a724 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 20 20 20  GRAM ){.        
1a725 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 6e 53 75  int nByte = (nSu
1a726 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53 75 62 50  b+1)*sizeof(SubP
1a727 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20 20 20  rogram*);.      
1a728 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20    int j;.       
1a729 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 53 75 62   for(j=0; j<nSub
1a72a 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
1a72b 20 20 69 66 28 20 61 70 53 75 62 5b 6a 5d 3d 3d    if( apSub[j]==
1a72c 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d  pOp->p4.pProgram
1a72d 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
1a72e 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
1a72f 6a 3d 3d 6e 53 75 62 20 26 26 20 53 51 4c 49 54  j==nSub && SQLIT
1a730 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62  E_OK==sqlite3Vdb
1a731 65 4d 65 6d 47 72 6f 77 28 70 53 75 62 2c 20 6e  eMemGrow(pSub, n
1a732 42 79 74 65 2c 20 31 29 20 29 7b 0a 20 20 20 20  Byte, 1) ){.    
1a733 20 20 20 20 20 20 61 70 53 75 62 20 3d 20 28 53        apSub = (S
1a734 75 62 50 72 6f 67 72 61 6d 20 2a 2a 29 70 53 75  ubProgram **)pSu
1a735 62 2d 3e 7a 3b 0a 20 20 20 20 20 20 20 20 20 20  b->z;.          
1a736 61 70 53 75 62 5b 6e 53 75 62 2b 2b 5d 20 3d 20  apSub[nSub++] = 
1a737 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d  pOp->p4.pProgram
1a738 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53 75 62  ;.          pSub
1a739 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 42  ->flags |= MEM_B
1a73a 6c 6f 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70  lob;.          p
1a73b 53 75 62 2d 3e 6e 20 3d 20 6e 53 75 62 2a 73 69  Sub->n = nSub*si
1a73c 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 2a  zeof(SubProgram*
1a73d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1a73e 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
1a73f 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1a740 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d  M_Int;.    pMem-
1a741 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 20  >u.i = pOp->p1; 
1a742 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a743 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31 20 2a           /* P1 *
1a744 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65  /.    pMem->type
1a745 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45   = SQLITE_INTEGE
1a746 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a  R;.    pMem++;..
1a747 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1a748 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70  = MEM_Int;.    p
1a749 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e  Mem->u.i = pOp->
1a74a 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p2;             
1a74b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1a74c 50 32 20 2a 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e  P2 */.    pMem->
1a74d 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e  type = SQLITE_IN
1a74e 54 45 47 45 52 3b 0a 20 20 20 20 70 4d 65 6d 2b  TEGER;.    pMem+
1a74f 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 65  +;..    if( p->e
1a750 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20  xplain==1 ){.   
1a751 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
1a752 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
1a753 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d  pMem->u.i = pOp-
1a754 3e 70 33 3b 20 20 20 20 20 20 20 20 20 20 20 20  >p3;            
1a755 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a756 20 50 33 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65   P3 */.      pMe
1a757 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
1a758 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20  _INTEGER;.      
1a759 70 4d 65 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20  pMem++;.    }.. 
1a75a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
1a75b 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20  beMemGrow(pMem, 
1a75c 33 32 2c 20 30 29 20 29 7b 20 20 20 20 20 20 20  32, 0) ){       
1a75d 20 20 20 20 20 2f 2a 20 50 34 20 2a 2f 0a 20 20       /* P4 */.  
1a75e 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64      assert( p->d
1a75f 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a760 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1a761 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1a762 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c    }.    pMem->fl
1a763 61 67 73 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45  ags = MEM_Dyn|ME
1a764 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
1a765 20 20 20 20 7a 20 3d 20 64 69 73 70 6c 61 79 50      z = displayP
1a766 34 28 70 4f 70 2c 20 70 4d 65 6d 2d 3e 7a 2c 20  4(pOp, pMem->z, 
1a767 33 32 29 3b 0a 20 20 20 20 69 66 28 20 7a 21 3d  32);.    if( z!=
1a768 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20  pMem->z ){.     
1a769 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1a76a 65 74 53 74 72 28 70 4d 65 6d 2c 20 7a 2c 20 2d  etStr(pMem, z, -
1a76b 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  1, SQLITE_UTF8, 
1a76c 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  0);.    }else{. 
1a76d 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65       assert( pMe
1a76e 6d 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 20 20 20  m->z!=0 );.     
1a76f 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74   pMem->n = sqlit
1a770 65 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d  e3Strlen30(pMem-
1a771 3e 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  >z);.      pMem-
1a772 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  >enc = SQLITE_UT
1a773 46 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d  F8;.    }.    pM
1a774 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
1a775 45 5f 54 45 58 54 3b 0a 20 20 20 20 70 4d 65 6d  E_TEXT;.    pMem
1a776 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e  ++;..    if( p->
1a777 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20  explain==1 ){.  
1a778 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
1a779 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c  dbeMemGrow(pMem,
1a77a 20 34 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20   4, 0) ){.      
1a77b 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 2d    assert( p->db-
1a77c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
1a77d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1a77e 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1a77f 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4d 65 6d      }.      pMem
1a780 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 44 79  ->flags = MEM_Dy
1a781 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65  n|MEM_Str|MEM_Te
1a782 72 6d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  rm;.      pMem->
1a783 6e 20 3d 20 32 3b 0a 20 20 20 20 20 20 73 71 6c  n = 2;.      sql
1a784 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c  ite3_snprintf(3,
1a785 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 2e 32 78 22   pMem->z, "%.2x"
1a786 2c 20 70 4f 70 2d 3e 70 35 29 3b 20 20 20 2f 2a  , pOp->p5);   /*
1a787 20 50 35 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65   P5 */.      pMe
1a788 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
1a789 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 70 4d 65  _TEXT;.      pMe
1a78a 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  m->enc = SQLITE_
1a78b 55 54 46 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d  UTF8;.      pMem
1a78c 2b 2b 3b 0a 20 20 0a 23 69 66 64 65 66 20 53 51  ++;.  .#ifdef SQ
1a78d 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20  LITE_DEBUG.     
1a78e 20 69 66 28 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65   if( pOp->zComme
1a78f 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d  nt ){.        pM
1a790 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1a791 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20  Str|MEM_Term;.  
1a792 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20        pMem->z = 
1a793 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20  pOp->zComment;. 
1a794 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d         pMem->n =
1a795 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1a796 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20  (pMem->z);.     
1a797 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53     pMem->enc = S
1a798 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20  QLITE_UTF8;.    
1a799 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1a79a 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20   SQLITE_TEXT;.  
1a79b 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
1a79c 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20  .      {.       
1a79d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1a79e 45 4d 5f 4e 75 6c 6c 3b 20 20 20 20 20 20 20 20  EM_Null;        
1a79f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a7a0 2a 20 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20  * Comment */.   
1a7a1 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20       pMem->type 
1a7a2 3d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20  = SQLITE_NULL;. 
1a7a3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
1a7a4 20 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20    p->nResColumn 
1a7a5 3d 20 38 20 2d 20 35 2a 28 70 2d 3e 65 78 70 6c  = 8 - 5*(p->expl
1a7a6 61 69 6e 2d 31 29 3b 0a 20 20 20 20 70 2d 3e 72  ain-1);.    p->r
1a7a7 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1a7a8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52     rc = SQLITE_R
1a7a9 4f 57 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  OW;.  }.  return
1a7aa 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a   rc;.}.#endif /*
1a7ab 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50   SQLITE_OMIT_EXP
1a7ac 4c 41 49 4e 20 2a 2f 0a 0a 23 69 66 64 65 66 20  LAIN */..#ifdef 
1a7ad 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
1a7ae 2a 2a 20 50 72 69 6e 74 20 74 68 65 20 53 51 4c  ** Print the SQL
1a7af 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74   that was used t
1a7b0 6f 20 67 65 6e 65 72 61 74 65 20 61 20 56 44 42  o generate a VDB
1a7b1 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51  E program..*/.SQ
1a7b2 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1a7b3 64 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69  d sqlite3VdbePri
1a7b4 6e 74 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a  ntSql(Vdbe *p){.
1a7b5 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e    int nOp = p->n
1a7b6 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f  Op;.  VdbeOp *pO
1a7b7 70 3b 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29  p;.  if( nOp<1 )
1a7b8 20 72 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d   return;.  pOp =
1a7b9 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69   &p->aOp[0];.  i
1a7ba 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
1a7bb 4f 50 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d  OP_Trace && pOp-
1a7bc 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20  >p4.z!=0 ){.    
1a7bd 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
1a7be 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 77  pOp->p4.z;.    w
1a7bf 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73  hile( sqlite3Iss
1a7c0 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a  pace(*z) ) z++;.
1a7c1 20 20 20 20 70 72 69 6e 74 66 28 22 53 51 4c 3a      printf("SQL:
1a7c2 20 5b 25 73 5d 5c 6e 22 2c 20 7a 29 3b 0a 20 20   [%s]\n", z);.  
1a7c3 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20  }.}.#endif..#if 
1a7c4 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1a7c5 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26 20 64  OMIT_TRACE) && d
1a7c6 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
1a7c7 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a 2f 2a  ABLE_IOTRACE)./*
1a7c8 0a 2a 2a 20 50 72 69 6e 74 20 61 6e 20 49 4f 54  .** Print an IOT
1a7c9 52 41 43 45 20 6d 65 73 73 61 67 65 20 73 68 6f  RACE message sho
1a7ca 77 69 6e 67 20 53 51 4c 20 63 6f 6e 74 65 6e 74  wing SQL content
1a7cb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a7cc 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1a7cd 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56  VdbeIOTraceSql(V
1a7ce 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  dbe *p){.  int n
1a7cf 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56  Op = p->nOp;.  V
1a7d0 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66  dbeOp *pOp;.  if
1a7d1 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65  ( sqlite3IoTrace
1a7d2 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1a7d3 69 66 28 20 6e 4f 70 3c 31 20 29 20 72 65 74 75  if( nOp<1 ) retu
1a7d4 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e  rn;.  pOp = &p->
1a7d5 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28 20 70 4f  aOp[0];.  if( pO
1a7d6 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72  p->opcode==OP_Tr
1a7d7 61 63 65 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a  ace && pOp->p4.z
1a7d8 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  !=0 ){.    int i
1a7d9 2c 20 6a 3b 0a 20 20 20 20 63 68 61 72 20 7a 5b  , j;.    char z[
1a7da 31 30 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74  1000];.    sqlit
1a7db 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
1a7dc 6f 66 28 7a 29 2c 20 7a 2c 20 22 25 73 22 2c 20  of(z), z, "%s", 
1a7dd 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20  pOp->p4.z);.    
1a7de 66 6f 72 28 69 3d 30 3b 20 73 71 6c 69 74 65 33  for(i=0; sqlite3
1a7df 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69  Isspace(z[i]); i
1a7e0 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f 72 28 6a 3d  ++){}.    for(j=
1a7e1 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  0; z[i]; i++){. 
1a7e2 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1a7e3 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 20 29 7b  Isspace(z[i]) ){
1a7e4 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 69  .        if( z[i
1a7e5 2d 31 5d 21 3d 27 20 27 20 29 7b 0a 20 20 20 20  -1]!=' ' ){.    
1a7e6 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27        z[j++] = '
1a7e7 20 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   ';.        }.  
1a7e8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1a7e9 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d     z[j++] = z[i]
1a7ea 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1a7eb 20 20 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20      z[j] = 0;.  
1a7ec 20 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65    sqlite3IoTrace
1a7ed 28 22 53 51 4c 20 25 73 5c 6e 22 2c 20 7a 29 3b  ("SQL %s\n", z);
1a7ee 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
1a7ef 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52   !SQLITE_OMIT_TR
1a7f0 41 43 45 20 26 26 20 53 51 4c 49 54 45 5f 45 4e  ACE && SQLITE_EN
1a7f1 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 2a 2f 0a  ABLE_IOTRACE */.
1a7f2 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
1a7f3 73 70 61 63 65 20 66 72 6f 6d 20 61 20 66 69 78  space from a fix
1a7f4 65 64 20 73 69 7a 65 20 62 75 66 66 65 72 2e 20  ed size buffer. 
1a7f5 20 4d 61 6b 65 20 2a 70 70 20 70 6f 69 6e 74 20   Make *pp point 
1a7f6 74 6f 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61  to the.** alloca
1a7f7 74 65 64 20 73 70 61 63 65 2e 20 20 28 4e 6f 74  ted space.  (Not
1a7f8 65 3a 20 20 70 70 20 69 73 20 61 20 63 68 61 72  e:  pp is a char
1a7f9 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 20  * rather than a 
1a7fa 76 6f 69 64 2a 2a 20 74 6f 0a 2a 2a 20 77 6f 72  void** to.** wor
1a7fb 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 6f 69  k around the poi
1a7fc 6e 74 65 72 20 61 6c 69 61 73 69 6e 67 20 72 75  nter aliasing ru
1a7fd 6c 65 73 20 6f 66 20 43 2e 29 20 20 2a 70 70 20  les of C.)  *pp 
1a7fe 73 68 6f 75 6c 64 20 69 6e 69 74 69 61 6c 6c 79  should initially
1a7ff 0a 2a 2a 20 62 65 20 7a 65 72 6f 2e 20 20 49 66  .** be zero.  If
1a800 20 2a 70 70 20 69 73 20 6e 6f 74 20 7a 65 72 6f   *pp is not zero
1a801 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61  , that means tha
1a802 74 20 74 68 65 20 73 70 61 63 65 20 68 61 73 20  t the space has 
1a803 61 6c 72 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20  already.** been 
1a804 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74 68  allocated and th
1a805 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
1a806 6e 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 6e 42 79 74  noop..**.** nByt
1a807 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  e is the number 
1a808 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  of bytes of spac
1a809 65 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20  e needed..**.** 
1a80a 2a 70 70 46 72 6f 6d 20 70 6f 69 6e 74 20 74 6f  *ppFrom point to
1a80b 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65   available space
1a80c 20 61 6e 64 20 70 45 6e 64 20 70 6f 69 6e 74 73   and pEnd points
1a80d 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
1a80e 68 65 0a 2a 2a 20 61 76 61 69 6c 61 62 6c 65 20  he.** available 
1a80f 73 70 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 2a 70 6e  space..**.** *pn
1a810 42 79 74 65 20 69 73 20 61 20 63 6f 75 6e 74 65  Byte is a counte
1a811 72 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  r of the number 
1a812 6f 66 20 62 79 74 65 73 20 6f 66 20 73 70 61 63  of bytes of spac
1a813 65 20 74 68 61 74 20 68 61 76 65 20 66 61 69 6c  e that have fail
1a814 65 64 0a 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74  ed.** to allocat
1a815 65 2e 20 20 49 66 20 74 68 65 72 65 20 69 73 20  e.  If there is 
1a816 69 6e 73 75 66 66 69 63 69 65 6e 74 20 73 70 61  insufficient spa
1a817 63 65 20 69 6e 20 2a 70 70 46 72 6f 6d 20 74 6f  ce in *ppFrom to
1a818 20 73 61 74 69 73 66 79 20 74 68 65 0a 2a 2a 20   satisfy the.** 
1a819 72 65 71 75 65 73 74 2c 20 74 68 65 6e 20 69 6e  request, then in
1a81a 63 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 65 20  crement *pnByte 
1a81b 62 79 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  by the amount of
1a81c 20 74 68 65 20 72 65 71 75 65 73 74 2e 0a 2a 2f   the request..*/
1a81d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 6c 6c  .static void all
1a81e 6f 63 53 70 61 63 65 28 0a 20 20 63 68 61 72 20  ocSpace(.  char 
1a81f 2a 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20  *pp,            
1a820 2f 2a 20 49 4e 2f 4f 55 54 3a 20 53 65 74 20 2a  /* IN/OUT: Set *
1a821 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61  pp to point to a
1a822 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20  llocated buffer 
1a823 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20  */.  int nByte, 
1a824 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1a825 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20  ber of bytes to 
1a826 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 75 38  allocate */.  u8
1a827 20 2a 2a 70 70 46 72 6f 6d 2c 20 20 20 20 20 20   **ppFrom,      
1a828 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 41 6c     /* IN/OUT: Al
1a829 6c 6f 63 61 74 65 20 66 72 6f 6d 20 2a 70 70 46  locate from *ppF
1a82a 72 6f 6d 20 2a 2f 0a 20 20 75 38 20 2a 70 45 6e  rom */.  u8 *pEn
1a82b 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  d,            /*
1a82c 20 50 6f 69 6e 74 65 72 20 74 6f 20 31 20 62 79   Pointer to 1 by
1a82d 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  te past the end 
1a82e 6f 66 20 2a 70 70 46 72 6f 6d 20 62 75 66 66 65  of *ppFrom buffe
1a82f 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79  r */.  int *pnBy
1a830 74 65 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  te          /* I
1a831 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 63 61 6e  f allocation can
1a832 6e 6f 74 20 62 65 20 6d 61 64 65 2c 20 69 6e 63  not be made, inc
1a833 72 65 6d 65 6e 74 20 2a 70 6e 42 79 74 65 20 2a  rement *pnByte *
1a834 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 45  /.){.  assert( E
1a835 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d  IGHT_BYTE_ALIGNM
1a836 45 4e 54 28 2a 70 70 46 72 6f 6d 29 20 29 3b 0a  ENT(*ppFrom) );.
1a837 20 20 69 66 28 20 28 2a 28 76 6f 69 64 2a 2a 29    if( (*(void**)
1a838 70 70 29 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 42  pp)==0 ){.    nB
1a839 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79  yte = ROUND8(nBy
1a83a 74 65 29 3b 0a 20 20 20 20 69 66 28 20 26 28 2a  te);.    if( &(*
1a83b 70 70 46 72 6f 6d 29 5b 6e 42 79 74 65 5d 20 3c  ppFrom)[nByte] <
1a83c 3d 20 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20  = pEnd ){.      
1a83d 2a 28 76 6f 69 64 2a 2a 29 70 70 20 3d 20 28 76  *(void**)pp = (v
1a83e 6f 69 64 20 2a 29 2a 70 70 46 72 6f 6d 3b 0a 20  oid *)*ppFrom;. 
1a83f 20 20 20 20 20 2a 70 70 46 72 6f 6d 20 2b 3d 20       *ppFrom += 
1a840 6e 42 79 74 65 3b 0a 20 20 20 20 7d 65 6c 73 65  nByte;.    }else
1a841 7b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74 65 20  {.      *pnByte 
1a842 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 0a  += nByte;.    }.
1a843 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65    }.}../*.** Pre
1a844 70 61 72 65 20 61 20 76 69 72 74 75 61 6c 20 6d  pare a virtual m
1a845 61 63 68 69 6e 65 20 66 6f 72 20 65 78 65 63 75  achine for execu
1a846 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 76 6f  tion.  This invo
1a847 6c 76 65 73 20 74 68 69 6e 67 73 20 73 75 63 68  lves things such
1a848 0a 2a 2a 20 61 73 20 61 6c 6c 6f 63 61 74 69 6e  .** as allocatin
1a849 67 20 73 74 61 63 6b 20 73 70 61 63 65 20 61 6e  g stack space an
1a84a 64 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74  d initializing t
1a84b 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74  he program count
1a84c 65 72 2e 0a 2a 2a 20 41 66 74 65 72 20 74 68 65  er..** After the
1a84d 20 56 44 42 45 20 68 61 73 20 62 65 20 70 72 65   VDBE has be pre
1a84e 70 70 65 64 2c 20 69 74 20 63 61 6e 20 62 65 20  pped, it can be 
1a84f 65 78 65 63 75 74 65 64 20 62 79 20 6f 6e 65 20  executed by one 
1a850 6f 72 20 6d 6f 72 65 0a 2a 2a 20 63 61 6c 6c 73  or more.** calls
1a851 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 45   to sqlite3VdbeE
1a852 78 65 63 28 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  xec().  .**.** T
1a853 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  his is the only 
1a854 77 61 79 20 74 6f 20 6d 6f 76 65 20 61 20 56 44  way to move a VD
1a855 42 45 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47  BE from VDBE_MAG
1a856 49 43 5f 49 4e 49 54 20 74 6f 0a 2a 2a 20 56 44  IC_INIT to.** VD
1a857 42 45 5f 4d 41 47 49 43 5f 52 55 4e 2e 0a 2a 2a  BE_MAGIC_RUN..**
1a858 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1a859 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20  n may be called 
1a85a 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20 6f  more than once o
1a85b 6e 20 61 20 73 69 6e 67 6c 65 20 76 69 72 74 75  n a single virtu
1a85c 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 20 54  al machine..** T
1a85d 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20 69 73  he first call is
1a85e 20 6d 61 64 65 20 77 68 69 6c 65 20 63 6f 6d 70   made while comp
1a85f 69 6c 69 6e 67 20 74 68 65 20 53 51 4c 20 73 74  iling the SQL st
1a860 61 74 65 6d 65 6e 74 2e 20 53 75 62 73 65 71 75  atement. Subsequ
1a861 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 61 72 65  ent.** calls are
1a862 20 6d 61 64 65 20 61 73 20 70 61 72 74 20 6f 66   made as part of
1a863 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66 20   the process of 
1a864 72 65 73 65 74 74 69 6e 67 20 61 20 73 74 61 74  resetting a stat
1a865 65 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72  ement to be.** r
1a866 65 2d 65 78 65 63 75 74 65 64 20 28 66 72 6f 6d  e-executed (from
1a867 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
1a868 65 33 5f 72 65 73 65 74 28 29 29 2e 20 54 68 65  e3_reset()). The
1a869 20 6e 56 61 72 2c 20 6e 4d 65 6d 2c 20 6e 43 75   nVar, nMem, nCu
1a86a 72 73 6f 72 20 0a 2a 2a 20 61 6e 64 20 69 73 45  rsor .** and isE
1a86b 78 70 6c 61 69 6e 20 70 61 72 61 6d 65 74 65 72  xplain parameter
1a86c 73 20 61 72 65 20 6f 6e 6c 79 20 70 61 73 73 65  s are only passe
1a86d 64 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65 73  d correct values
1a86e 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 0a   the first time.
1a86f 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  ** the function 
1a870 69 73 20 63 61 6c 6c 65 64 2e 20 4f 6e 20 73 75  is called. On su
1a871 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 2c 20  bsequent calls, 
1a872 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65 73  from sqlite3_res
1a873 65 74 28 29 2c 20 6e 56 61 72 0a 2a 2a 20 69 73  et(), nVar.** is
1a874 20 70 61 73 73 65 64 20 2d 31 20 61 6e 64 20 6e   passed -1 and n
1a875 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 61 6e 64  Mem, nCursor and
1a876 20 69 73 45 78 70 6c 61 69 6e 20 61 72 65 20 61   isExplain are a
1a877 6c 6c 20 70 61 73 73 65 64 20 7a 65 72 6f 2e 0a  ll passed zero..
1a878 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a879 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a87a 62 65 4d 61 6b 65 52 65 61 64 79 28 0a 20 20 56  beMakeReady(.  V
1a87b 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  dbe *p,         
1a87c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a87d 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 20 20 69   The VDBE */.  i
1a87e 6e 74 20 6e 56 61 72 2c 20 20 20 20 20 20 20 20  nt nVar,        
1a87f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a880 20 4e 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 73   Number of '?' s
1a881 65 65 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74  ee in the SQL st
1a882 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  atement */.  int
1a883 20 6e 4d 65 6d 2c 20 20 20 20 20 20 20 20 20 20   nMem,          
1a884 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1a885 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20  umber of memory 
1a886 63 65 6c 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74  cells to allocat
1a887 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75 72 73  e */.  int nCurs
1a888 6f 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  or,             
1a889 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1a88a 6f 66 20 63 75 72 73 6f 72 73 20 74 6f 20 61 6c  of cursors to al
1a88b 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20  locate */.  int 
1a88c 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20 20 20  nArg,           
1a88d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
1a88e 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
1a88f 61 72 67 73 20 69 6e 20 53 75 62 50 72 6f 67 72  args in SubProgr
1a890 61 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45  ams */.  int isE
1a891 78 70 6c 61 69 6e 2c 20 20 20 20 20 20 20 20 20  xplain,         
1a892 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1a893 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20 6b  if the EXPLAIN k
1a894 65 79 77 6f 72 64 73 20 69 73 20 70 72 65 73 65  eywords is prese
1a895 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 73  nt */.  int uses
1a896 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 20 20 20 20  StmtJournal     
1a897 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
1a898 6f 20 73 65 74 20 56 64 62 65 2e 75 73 65 73 53  o set Vdbe.usesS
1a899 74 6d 74 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 29 7b  tmtJournal */.){
1a89a 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69  .  int n;.  sqli
1a89b 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
1a89c 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30  ..  assert( p!=0
1a89d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1a89e 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
1a89f 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 2f 2a  IC_INIT );..  /*
1a8a0 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65   There should be
1a8a1 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70   at least one op
1a8a2 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  code..  */.  ass
1a8a3 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b  ert( p->nOp>0 );
1a8a4 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d  ..  /* Set the m
1a8a5 61 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41 47  agic to VDBE_MAG
1a8a6 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72 61  IC_RUN sooner ra
1a8a7 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 2e  ther than later.
1a8a8 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d   */.  p->magic =
1a8a9 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b   VDBE_MAGIC_RUN;
1a8aa 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20  ..  /* For each 
1a8ab 63 75 72 73 6f 72 20 72 65 71 75 69 72 65 64 2c  cursor required,
1a8ac 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20 61   also allocate a
1a8ad 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d 65   memory cell. Me
1a8ae 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73 20  mory.  ** cells 
1a8af 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 29  (nMem+1-nCursor)
1a8b0 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 76  ..nMem, inclusiv
1a8b1 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  e, will never be
1a8b2 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68   used by.  ** th
1a8b3 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20  e vdbe program. 
1a8b4 49 6e 73 74 65 61 64 20 74 68 65 79 20 61 72 65  Instead they are
1a8b5 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74   used to allocat
1a8b6 65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a  e space for.  **
1a8b7 20 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 75   VdbeCursor/BtCu
1a8b8 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 2e  rsor structures.
1a8b9 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d   The blob of mem
1a8ba 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  ory associated w
1a8bb 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72  ith .  ** cursor
1a8bc 20 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20   0 is stored in 
1a8bd 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 6d  memory cell nMem
1a8be 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 6e  . Memory cell (n
1a8bf 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f 72  Mem-1).  ** stor
1a8c0 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d  es the blob of m
1a8c1 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64  emory associated
1a8c2 20 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c 20   with cursor 1, 
1a8c3 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53  etc..  **.  ** S
1a8c4 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61 74  ee also: allocat
1a8c5 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f 0a  eCursor()..  */.
1a8c6 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73 6f    nMem += nCurso
1a8c7 72 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  r;..  /* Allocat
1a8c8 65 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d 6f  e space for memo
1a8c9 72 79 20 72 65 67 69 73 74 65 72 73 2c 20 53 51  ry registers, SQ
1a8ca 4c 20 76 61 72 69 61 62 6c 65 73 2c 20 56 44 42  L variables, VDB
1a8cb 45 20 63 75 72 73 6f 72 73 20 61 6e 64 20 0a 20  E cursors and . 
1a8cc 20 2a 2a 20 61 6e 20 61 72 72 61 79 20 74 6f 20   ** an array to 
1a8cd 6d 61 72 73 68 61 6c 20 53 51 4c 20 66 75 6e 63  marshal SQL func
1a8ce 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 73 20 69  tion arguments i
1a8cf 6e 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20  n. This is only 
1a8d0 64 6f 6e 65 20 74 68 65 0a 20 20 2a 2a 20 66 69  done the.  ** fi
1a8d1 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 66 75  rst time this fu
1a8d2 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
1a8d3 20 66 6f 72 20 61 20 67 69 76 65 6e 20 56 44 42   for a given VDB
1a8d4 45 2c 20 6e 6f 74 20 77 68 65 6e 20 69 74 20 69  E, not when it i
1a8d5 73 0a 20 20 2a 2a 20 62 65 69 6e 67 20 63 61 6c  s.  ** being cal
1a8d6 6c 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  led from sqlite3
1a8d7 5f 72 65 73 65 74 28 29 20 74 6f 20 72 65 73 65  _reset() to rese
1a8d8 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  t the virtual ma
1a8d9 63 68 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  chine..  */.  if
1a8da 28 20 6e 56 61 72 3e 3d 30 20 26 26 20 41 4c 57  ( nVar>=0 && ALW
1a8db 41 59 53 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  AYS(db->mallocFa
1a8dc 69 6c 65 64 3d 3d 30 29 20 29 7b 0a 20 20 20 20  iled==0) ){.    
1a8dd 75 38 20 2a 7a 43 73 72 20 3d 20 28 75 38 20 2a  u8 *zCsr = (u8 *
1a8de 29 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 5d  )&p->aOp[p->nOp]
1a8df 3b 0a 20 20 20 20 75 38 20 2a 7a 45 6e 64 20 3d  ;.    u8 *zEnd =
1a8e0 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f 70 5b 70   (u8 *)&p->aOp[p
1a8e1 2d 3e 6e 4f 70 41 6c 6c 6f 63 5d 3b 0a 20 20 20  ->nOpAlloc];.   
1a8e2 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20 20   int nByte;.    
1a8e3 72 65 73 6f 6c 76 65 50 32 56 61 6c 75 65 73 28  resolveP2Values(
1a8e4 70 2c 20 26 6e 41 72 67 29 3b 0a 20 20 20 20 70  p, &nArg);.    p
1a8e5 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61  ->usesStmtJourna
1a8e6 6c 20 3d 20 28 75 38 29 75 73 65 73 53 74 6d 74  l = (u8)usesStmt
1a8e7 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20 20 69 66 28  Journal;.    if(
1a8e8 20 69 73 45 78 70 6c 61 69 6e 20 26 26 20 6e 4d   isExplain && nM
1a8e9 65 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 20 6e  em<10 ){.      n
1a8ea 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 7d 0a  Mem = 10;.    }.
1a8eb 20 20 20 20 6d 65 6d 73 65 74 28 7a 43 73 72 2c      memset(zCsr,
1a8ec 20 30 2c 20 7a 45 6e 64 2d 7a 43 73 72 29 3b 0a   0, zEnd-zCsr);.
1a8ed 20 20 20 20 7a 43 73 72 20 2b 3d 20 28 7a 43 73      zCsr += (zCs
1a8ee 72 20 2d 20 28 75 38 2a 29 30 29 26 37 3b 0a 20  r - (u8*)0)&7;. 
1a8ef 20 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54     assert( EIGHT
1a8f0 5f 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28  _BYTE_ALIGNMENT(
1a8f1 7a 43 73 72 29 20 29 3b 0a 0a 20 20 20 20 64 6f  zCsr) );..    do
1a8f2 20 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d   {.      nByte =
1a8f3 20 30 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53   0;.      allocS
1a8f4 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e  pace((char*)&p->
1a8f5 61 4d 65 6d 2c 20 6e 4d 65 6d 2a 73 69 7a 65 6f  aMem, nMem*sizeo
1a8f6 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72 2c 20 7a  f(Mem), &zCsr, z
1a8f7 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20  End, &nByte);.  
1a8f8 20 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28      allocSpace((
1a8f9 63 68 61 72 2a 29 26 70 2d 3e 61 56 61 72 2c 20  char*)&p->aVar, 
1a8fa 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29  nVar*sizeof(Mem)
1a8fb 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26  , &zCsr, zEnd, &
1a8fc 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c  nByte);.      al
1a8fd 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29  locSpace((char*)
1a8fe 26 70 2d 3e 61 70 41 72 67 2c 20 6e 41 72 67 2a  &p->apArg, nArg*
1a8ff 73 69 7a 65 6f 66 28 4d 65 6d 2a 29 2c 20 26 7a  sizeof(Mem*), &z
1a900 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74  Csr, zEnd, &nByt
1a901 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53  e);.      allocS
1a902 70 61 63 65 28 28 63 68 61 72 2a 29 26 70 2d 3e  pace((char*)&p->
1a903 61 7a 56 61 72 2c 20 6e 56 61 72 2a 73 69 7a 65  azVar, nVar*size
1a904 6f 66 28 63 68 61 72 2a 29 2c 20 26 7a 43 73 72  of(char*), &zCsr
1a905 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b  , zEnd, &nByte);
1a906 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63  .      allocSpac
1a907 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 70 43  e((char*)&p->apC
1a908 73 72 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  sr, .           
1a909 20 20 20 20 20 20 6e 43 75 72 73 6f 72 2a 73 69        nCursor*si
1a90a 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 2a  zeof(VdbeCursor*
1a90b 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20  ), &zCsr, zEnd, 
1a90c 26 6e 42 79 74 65 0a 20 20 20 20 20 20 29 3b 0a  &nByte.      );.
1a90d 20 20 20 20 20 20 69 66 28 20 6e 42 79 74 65 20        if( nByte 
1a90e 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 46  ){.        p->pF
1a90f 72 65 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  ree = sqlite3DbM
1a910 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42  allocZero(db, nB
1a911 79 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  yte);.      }.  
1a912 20 20 20 20 7a 43 73 72 20 3d 20 70 2d 3e 70 46      zCsr = p->pF
1a913 72 65 65 3b 0a 20 20 20 20 20 20 7a 45 6e 64 20  ree;.      zEnd 
1a914 3d 20 26 7a 43 73 72 5b 6e 42 79 74 65 5d 3b 0a  = &zCsr[nByte];.
1a915 20 20 20 20 7d 77 68 69 6c 65 28 20 6e 42 79 74      }while( nByt
1a916 65 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  e && !db->malloc
1a917 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 20 20 70  Failed );..    p
1a918 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75 31 36  ->nCursor = (u16
1a919 29 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66  )nCursor;.    if
1a91a 28 20 70 2d 3e 61 56 61 72 20 29 7b 0a 20 20 20  ( p->aVar ){.   
1a91b 20 20 20 70 2d 3e 6e 56 61 72 20 3d 20 28 79 6e     p->nVar = (yn
1a91c 56 61 72 29 6e 56 61 72 3b 0a 20 20 20 20 20 20  Var)nVar;.      
1a91d 66 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b  for(n=0; n<nVar;
1a91e 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70   n++){.        p
1a91f 2d 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20  ->aVar[n].flags 
1a920 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20  = MEM_Null;.    
1a921 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64      p->aVar[n].d
1a922 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a  b = db;.      }.
1a923 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d      }.    if( p-
1a924 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20 20 20 70  >aMem ){.      p
1a925 2d 3e 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20  ->aMem--;       
1a926 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a927 2a 20 61 4d 65 6d 5b 5d 20 67 6f 65 73 20 66 72  * aMem[] goes fr
1a928 6f 6d 20 31 2e 2e 6e 4d 65 6d 20 2a 2f 0a 20 20  om 1..nMem */.  
1a929 20 20 20 20 70 2d 3e 6e 4d 65 6d 20 3d 20 6e 4d      p->nMem = nM
1a92a 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
1a92b 20 20 20 20 2f 2a 20 20 20 20 20 20 20 6e 6f 74      /*       not
1a92c 20 66 72 6f 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20   from 0..nMem-1 
1a92d 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d 31  */.      for(n=1
1a92e 3b 20 6e 3c 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b  ; n<=nMem; n++){
1a92f 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d  .        p->aMem
1a930 5b 6e 5d 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  [n].flags = MEM_
1a931 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d  Null;.        p-
1a932 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62  >aMem[n].db = db
1a933 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1a934 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
1a935 45 5f 44 45 42 55 47 0a 20 20 66 6f 72 28 6e 3d  E_DEBUG.  for(n=
1a936 31 3b 20 6e 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b  1; n<p->nMem; n+
1a937 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  +){.    assert( 
1a938 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64  p->aMem[n].db==d
1a939 62 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  b );.  }.#endif.
1a93a 0a 20 20 70 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20  .  p->pc = -1;. 
1a93b 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
1a93c 4f 4b 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63  OK;.  p->errorAc
1a93d 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  tion = OE_Abort;
1a93e 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d  .  p->explain |=
1a93f 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d   isExplain;.  p-
1a940 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41  >magic = VDBE_MA
1a941 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43  GIC_RUN;.  p->nC
1a942 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  hange = 0;.  p->
1a943 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20  cacheCtr = 1;.  
1a944 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46  p->minWriteFileF
1a945 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70  ormat = 255;.  p
1a946 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 3d 20 30  ->iStatement = 0
1a947 3b 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52  ;.#ifdef VDBE_PR
1a948 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 69 6e  OFILE.  {.    in
1a949 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
1a94a 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29  ; i<p->nOp; i++)
1a94b 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69  {.      p->aOp[i
1a94c 5d 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20  ].cnt = 0;.     
1a94d 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65   p->aOp[i].cycle
1a94e 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  s = 0;.    }.  }
1a94f 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
1a950 20 43 6c 6f 73 65 20 61 20 56 44 42 45 20 63 75   Close a VDBE cu
1a951 72 73 6f 72 20 61 6e 64 20 72 65 6c 65 61 73 65  rsor and release
1a952 20 61 6c 6c 20 74 68 65 20 72 65 73 6f 75 72 63   all the resourc
1a953 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 0a  es that cursor .
1a954 2a 2a 20 68 61 70 70 65 6e 73 20 74 6f 20 68 6f  ** happens to ho
1a955 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ld..*/.SQLITE_PR
1a956 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a957 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72  e3VdbeFreeCursor
1a958 28 56 64 62 65 20 2a 70 2c 20 56 64 62 65 43 75  (Vdbe *p, VdbeCu
1a959 72 73 6f 72 20 2a 70 43 78 29 7b 0a 20 20 69 66  rsor *pCx){.  if
1a95a 28 20 70 43 78 3d 3d 30 20 29 7b 0a 20 20 20 20  ( pCx==0 ){.    
1a95b 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66  return;.  }.  if
1a95c 28 20 70 43 78 2d 3e 70 42 74 20 29 7b 0a 20 20  ( pCx->pBt ){.  
1a95d 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c    sqlite3BtreeCl
1a95e 6f 73 65 28 70 43 78 2d 3e 70 42 74 29 3b 0a 20  ose(pCx->pBt);. 
1a95f 20 20 20 2f 2a 20 54 68 65 20 70 43 78 2d 3e 70     /* The pCx->p
1a960 43 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 63  Cursor will be c
1a961 6c 6f 73 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  lose automatical
1a962 6c 79 2c 20 69 66 20 69 74 20 65 78 69 73 74 73  ly, if it exists
1a963 2c 20 62 79 0a 20 20 20 20 2a 2a 20 74 68 65 20  , by.    ** the 
1a964 63 61 6c 6c 20 61 62 6f 76 65 2e 20 2a 2f 0a 20  call above. */. 
1a965 20 7d 65 6c 73 65 20 69 66 28 20 70 43 78 2d 3e   }else if( pCx->
1a966 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73  pCursor ){.    s
1a967 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
1a968 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 43 75 72  Cursor(pCx->pCur
1a969 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  sor);.  }.#ifnde
1a96a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1a96b 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
1a96c 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f   pCx->pVtabCurso
1a96d 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
1a96e 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56  _vtab_cursor *pV
1a96f 74 61 62 43 75 72 73 6f 72 20 3d 20 70 43 78 2d  tabCursor = pCx-
1a970 3e 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20  >pVtabCursor;.  
1a971 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
1a972 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20  module *pModule 
1a973 3d 20 70 43 78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a  = pCx->pModule;.
1a974 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74      p->inVtabMet
1a975 68 6f 64 20 3d 20 31 3b 0a 20 20 20 20 28 76 6f  hod = 1;.    (vo
1a976 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
1a977 4f 66 66 28 70 2d 3e 64 62 29 3b 0a 20 20 20 20  Off(p->db);.    
1a978 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28  pModule->xClose(
1a979 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20  pVtabCursor);.  
1a97a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
1a97b 61 66 65 74 79 4f 6e 28 70 2d 3e 64 62 29 3b 0a  afetyOn(p->db);.
1a97c 20 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74      p->inVtabMet
1a97d 68 6f 64 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e  hod = 0;.  }.#en
1a97e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 70  dif.}../*.** Cop
1a97f 79 20 74 68 65 20 76 61 6c 75 65 73 20 73 74 6f  y the values sto
1a980 72 65 64 20 69 6e 20 74 68 65 20 56 64 62 65 46  red in the VdbeF
1a981 72 61 6d 65 20 73 74 72 75 63 74 75 72 65 20 74  rame structure t
1a982 6f 20 69 74 73 20 56 64 62 65 2e 20 54 68 69 73  o its Vdbe. This
1a983 0a 2a 2a 20 69 73 20 75 73 65 64 2c 20 66 6f 72  .** is used, for
1a984 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 61   example, when a
1a985 20 74 72 69 67 67 65 72 20 73 75 62 2d 70 72 6f   trigger sub-pro
1a986 67 72 61 6d 20 69 73 20 68 61 6c 74 65 64 20 74  gram is halted t
1a987 6f 20 72 65 73 74 6f 72 65 0a 2a 2a 20 63 6f 6e  o restore.** con
1a988 74 72 6f 6c 20 74 6f 20 74 68 65 20 6d 61 69 6e  trol to the main
1a989 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c   program..*/.SQL
1a98a 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1a98b 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65  sqlite3VdbeFrame
1a98c 52 65 73 74 6f 72 65 28 56 64 62 65 46 72 61 6d  Restore(VdbeFram
1a98d 65 20 2a 70 46 72 61 6d 65 29 7b 0a 20 20 56 64  e *pFrame){.  Vd
1a98e 62 65 20 2a 76 20 3d 20 70 46 72 61 6d 65 2d 3e  be *v = pFrame->
1a98f 76 3b 0a 20 20 76 2d 3e 61 4f 70 20 3d 20 70 46  v;.  v->aOp = pF
1a990 72 61 6d 65 2d 3e 61 4f 70 3b 0a 20 20 76 2d 3e  rame->aOp;.  v->
1a991 6e 4f 70 20 3d 20 70 46 72 61 6d 65 2d 3e 6e 4f  nOp = pFrame->nO
1a992 70 3b 0a 20 20 76 2d 3e 61 4d 65 6d 20 3d 20 70  p;.  v->aMem = p
1a993 46 72 61 6d 65 2d 3e 61 4d 65 6d 3b 0a 20 20 76  Frame->aMem;.  v
1a994 2d 3e 6e 4d 65 6d 20 3d 20 70 46 72 61 6d 65 2d  ->nMem = pFrame-
1a995 3e 6e 4d 65 6d 3b 0a 20 20 76 2d 3e 61 70 43 73  >nMem;.  v->apCs
1a996 72 20 3d 20 70 46 72 61 6d 65 2d 3e 61 70 43 73  r = pFrame->apCs
1a997 72 3b 0a 20 20 76 2d 3e 6e 43 75 72 73 6f 72 20  r;.  v->nCursor 
1a998 3d 20 70 46 72 61 6d 65 2d 3e 6e 43 75 72 73 6f  = pFrame->nCurso
1a999 72 3b 0a 20 20 76 2d 3e 64 62 2d 3e 6c 61 73 74  r;.  v->db->last
1a99a 52 6f 77 69 64 20 3d 20 70 46 72 61 6d 65 2d 3e  Rowid = pFrame->
1a99b 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 76 2d 3e  lastRowid;.  v->
1a99c 6e 43 68 61 6e 67 65 20 3d 20 70 46 72 61 6d 65  nChange = pFrame
1a99d 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 72 65 74  ->nChange;.  ret
1a99e 75 72 6e 20 70 46 72 61 6d 65 2d 3e 70 63 3b 0a  urn pFrame->pc;.
1a99f 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
1a9a0 6c 6c 20 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a 2a  ll cursors..**.*
1a9a1 2a 20 41 6c 73 6f 20 72 65 6c 65 61 73 65 20 61  * Also release a
1a9a2 6e 79 20 64 79 6e 61 6d 69 63 20 6d 65 6d 6f 72  ny dynamic memor
1a9a3 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 56 4d  y held by the VM
1a9a4 20 69 6e 20 74 68 65 20 56 64 62 65 2e 61 4d 65   in the Vdbe.aMe
1a9a5 6d 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c  m memory .** cel
1a9a6 6c 20 61 72 72 61 79 2e 20 54 68 69 73 20 69 73  l array. This is
1a9a7 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 74 68   necessary as th
1a9a8 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61 72  e memory cell ar
1a9a9 72 61 79 20 6d 61 79 20 63 6f 6e 74 61 69 6e 0a  ray may contain.
1a9aa 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 56  ** pointers to V
1a9ab 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 74 73  dbeFrame objects
1a9ac 2c 20 77 68 69 63 68 20 6d 61 79 20 69 6e 20 74  , which may in t
1a9ad 75 72 6e 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e  urn contain poin
1a9ae 74 65 72 73 20 74 6f 0a 2a 2a 20 6f 70 65 6e 20  ters to.** open 
1a9af 63 75 72 73 6f 72 73 2e 0a 2a 2f 0a 73 74 61 74  cursors..*/.stat
1a9b0 69 63 20 76 6f 69 64 20 63 6c 6f 73 65 41 6c 6c  ic void closeAll
1a9b1 43 75 72 73 6f 72 73 28 56 64 62 65 20 2a 70 29  Cursors(Vdbe *p)
1a9b2 7b 0a 20 20 69 66 28 20 70 2d 3e 70 46 72 61 6d  {.  if( p->pFram
1a9b3 65 20 29 7b 0a 20 20 20 20 56 64 62 65 46 72 61  e ){.    VdbeFra
1a9b4 6d 65 20 2a 70 46 72 61 6d 65 20 3d 20 70 2d 3e  me *pFrame = p->
1a9b5 70 46 72 61 6d 65 3b 0a 20 20 20 20 66 6f 72 28  pFrame;.    for(
1a9b6 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65  pFrame=p->pFrame
1a9b7 3b 20 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e  ; pFrame->pParen
1a9b8 74 3b 20 70 46 72 61 6d 65 3d 70 46 72 61 6d 65  t; pFrame=pFrame
1a9b9 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20  ->pParent);.    
1a9ba 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65  sqlite3VdbeFrame
1a9bb 52 65 73 74 6f 72 65 28 70 46 72 61 6d 65 29 3b  Restore(pFrame);
1a9bc 0a 20 20 7d 0a 20 20 70 2d 3e 70 46 72 61 6d 65  .  }.  p->pFrame
1a9bd 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 61 6d   = 0;.  p->nFram
1a9be 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 2d  e = 0;..  if( p-
1a9bf 3e 61 70 43 73 72 20 29 7b 0a 20 20 20 20 69 6e  >apCsr ){.    in
1a9c0 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
1a9c1 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 3b 20  ; i<p->nCursor; 
1a9c2 69 2b 2b 29 7b 0a 20 20 20 20 20 20 56 64 62 65  i++){.      Vdbe
1a9c3 43 75 72 73 6f 72 20 2a 70 43 20 3d 20 70 2d 3e  Cursor *pC = p->
1a9c4 61 70 43 73 72 5b 69 5d 3b 0a 20 20 20 20 20 20  apCsr[i];.      
1a9c5 69 66 28 20 70 43 20 29 7b 0a 20 20 20 20 20 20  if( pC ){.      
1a9c6 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65    sqlite3VdbeFre
1a9c7 65 43 75 72 73 6f 72 28 70 2c 20 70 43 29 3b 0a  eCursor(p, pC);.
1a9c8 20 20 20 20 20 20 20 20 70 2d 3e 61 70 43 73 72          p->apCsr
1a9c9 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  [i] = 0;.      }
1a9ca 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
1a9cb 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20 20 20 20   p->aMem ){.    
1a9cc 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28  releaseMemArray(
1a9cd 26 70 2d 3e 61 4d 65 6d 5b 31 5d 2c 20 70 2d 3e  &p->aMem[1], p->
1a9ce 6e 4d 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  nMem);.  }.}../*
1a9cf 0a 2a 2a 20 43 6c 65 61 6e 20 75 70 20 74 68 65  .** Clean up the
1a9d0 20 56 4d 20 61 66 74 65 72 20 65 78 65 63 75 74   VM after execut
1a9d1 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ion..**.** This 
1a9d2 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 75 74  routine will aut
1a9d3 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65  omatically close
1a9d4 20 61 6e 79 20 63 75 72 73 6f 72 73 2c 20 6c 69   any cursors, li
1a9d5 73 74 73 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73  sts, and/or.** s
1a9d6 6f 72 74 65 72 73 20 74 68 61 74 20 77 65 72 65  orters that were
1a9d7 20 6c 65 66 74 20 6f 70 65 6e 2e 20 20 49 74 20   left open.  It 
1a9d8 61 6c 73 6f 20 64 65 6c 65 74 65 73 20 74 68 65  also deletes the
1a9d9 20 76 61 6c 75 65 73 20 6f 66 0a 2a 2a 20 76 61   values of.** va
1a9da 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65 20 61  riables in the a
1a9db 56 61 72 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a  Var[] array..*/.
1a9dc 73 74 61 74 69 63 20 76 6f 69 64 20 43 6c 65 61  static void Clea
1a9dd 6e 75 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  nup(Vdbe *p){.  
1a9de 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d  sqlite3 *db = p-
1a9df 3e 64 62 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  >db;..#ifdef SQL
1a9e0 49 54 45 5f 44 45 42 55 47 0a 20 20 2f 2a 20 45  ITE_DEBUG.  /* E
1a9e1 78 65 63 75 74 65 20 61 73 73 65 72 74 28 29 20  xecute assert() 
1a9e2 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 65 6e  statements to en
1a9e3 73 75 72 65 20 74 68 61 74 20 74 68 65 20 56 64  sure that the Vd
1a9e4 62 65 2e 61 70 43 73 72 5b 5d 20 61 6e 64 20 0a  be.apCsr[] and .
1a9e5 20 20 2a 2a 20 56 64 62 65 2e 61 4d 65 6d 5b 5d    ** Vdbe.aMem[]
1a9e6 20 61 72 72 61 79 73 20 68 61 76 65 20 61 6c 72   arrays have alr
1a9e7 65 61 64 79 20 62 65 65 6e 20 63 6c 65 61 6e 65  eady been cleane
1a9e8 64 20 75 70 2e 20 20 2a 2f 0a 20 20 69 6e 74 20  d up.  */.  int 
1a9e9 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
1a9ea 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b 29  p->nCursor; i++)
1a9eb 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73   assert( p->apCs
1a9ec 72 3d 3d 30 20 7c 7c 20 70 2d 3e 61 70 43 73 72  r==0 || p->apCsr
1a9ed 5b 69 5d 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28  [i]==0 );.  for(
1a9ee 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b  i=1; i<=p->nMem;
1a9ef 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20 70 2d   i++) assert( p-
1a9f0 3e 61 4d 65 6d 3d 3d 30 20 7c 7c 20 70 2d 3e 61  >aMem==0 || p->a
1a9f1 4d 65 6d 5b 69 5d 2e 66 6c 61 67 73 3d 3d 4d 45  Mem[i].flags==ME
1a9f2 4d 5f 4e 75 6c 6c 20 29 3b 0a 23 65 6e 64 69 66  M_Null );.#endif
1a9f3 0a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ..  sqlite3DbFre
1a9f4 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67  e(db, p->zErrMsg
1a9f5 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  );.  p->zErrMsg 
1a9f6 3d 20 30 3b 0a 20 20 70 2d 3e 70 52 65 73 75 6c  = 0;.  p->pResul
1a9f7 74 53 65 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  tSet = 0;.}../*.
1a9f8 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65  ** Set the numbe
1a9f9 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75  r of result colu
1a9fa 6d 6e 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65  mns that will be
1a9fb 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69   returned by thi
1a9fc 73 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65  s SQL.** stateme
1a9fd 6e 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 77 20  nt. This is now 
1a9fe 73 65 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74  set at compile t
1a9ff 69 6d 65 2c 20 72 61 74 68 65 72 20 74 68 61 6e  ime, rather than
1aa00 20 64 75 72 69 6e 67 0a 2a 2a 20 65 78 65 63 75   during.** execu
1aa01 74 69 6f 6e 20 6f 66 20 74 68 65 20 76 64 62 65  tion of the vdbe
1aa02 20 70 72 6f 67 72 61 6d 20 73 6f 20 74 68 61 74   program so that
1aa03 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1aa04 63 6f 75 6e 74 28 29 20 63 61 6e 0a 2a 2a 20 62  count() can.** b
1aa05 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 53  e called on an S
1aa06 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 66  QL statement bef
1aa07 6f 72 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ore sqlite3_step
1aa08 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
1aa09 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1aa0a 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
1aa0b 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 52  (Vdbe *p, int nR
1aa0c 65 73 43 6f 6c 75 6d 6e 29 7b 0a 20 20 4d 65 6d  esColumn){.  Mem
1aa0d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 69 6e   *pColName;.  in
1aa0e 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  t n;.  sqlite3 *
1aa0f 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 72  db = p->db;..  r
1aa10 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70  eleaseMemArray(p
1aa11 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e  ->aColName, p->n
1aa12 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d  ResColumn*COLNAM
1aa13 45 5f 4e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  E_N);.  sqlite3D
1aa14 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f  bFree(db, p->aCo
1aa15 6c 4e 61 6d 65 29 3b 0a 20 20 6e 20 3d 20 6e 52  lName);.  n = nR
1aa16 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45  esColumn*COLNAME
1aa17 5f 4e 3b 0a 20 20 70 2d 3e 6e 52 65 73 43 6f 6c  _N;.  p->nResCol
1aa18 75 6d 6e 20 3d 20 28 75 31 36 29 6e 52 65 73 43  umn = (u16)nResC
1aa19 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e 61 43 6f 6c  olumn;.  p->aCol
1aa1a 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e 61 6d 65 20  Name = pColName 
1aa1b 3d 20 28 4d 65 6d 2a 29 73 71 6c 69 74 65 33 44  = (Mem*)sqlite3D
1aa1c 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
1aa1d 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 6e 20 29 3b  sizeof(Mem)*n );
1aa1e 0a 20 20 69 66 28 20 70 2d 3e 61 43 6f 6c 4e 61  .  if( p->aColNa
1aa1f 6d 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  me==0 ) return;.
1aa20 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 3e 20 30    while( n-- > 0
1aa21 20 29 7b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65   ){.    pColName
1aa22 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  ->flags = MEM_Nu
1aa23 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65  ll;.    pColName
1aa24 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20  ->db = p->db;.  
1aa25 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b 3b 0a 20 20    pColName++;.  
1aa26 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  }.}../*.** Set t
1aa27 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 69  he name of the i
1aa28 64 78 27 74 68 20 63 6f 6c 75 6d 6e 20 74 6f 20  dx'th column to 
1aa29 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  be returned by t
1aa2a 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1aa2b 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d 75 73 74 20  ..** zName must 
1aa2c 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  be a pointer to 
1aa2d 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  a nul terminated
1aa2e 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54   string..**.** T
1aa2f 68 69 73 20 63 61 6c 6c 20 6d 75 73 74 20 62 65  his call must be
1aa30 20 6d 61 64 65 20 61 66 74 65 72 20 61 20 63 61   made after a ca
1aa31 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62  ll to sqlite3Vdb
1aa32 65 53 65 74 4e 75 6d 43 6f 6c 73 28 29 2e 0a 2a  eSetNumCols()..*
1aa33 2a 0a 2a 2a 20 54 68 65 20 66 69 6e 61 6c 20 70  *.** The final p
1aa34 61 72 61 6d 65 74 65 72 2c 20 78 44 65 6c 2c 20  arameter, xDel, 
1aa35 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53  must be one of S
1aa36 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c 20 53  QLITE_DYNAMIC, S
1aa37 51 4c 49 54 45 5f 53 54 41 54 49 43 0a 2a 2a 20  QLITE_STATIC.** 
1aa38 6f 72 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  or SQLITE_TRANSI
1aa39 45 4e 54 2e 20 49 66 20 69 74 20 69 73 20 53 51  ENT. If it is SQ
1aa3a 4c 49 54 45 5f 44 59 4e 41 4d 49 43 2c 20 74 68  LITE_DYNAMIC, th
1aa3b 65 6e 20 74 68 65 20 62 75 66 66 65 72 20 70 6f  en the buffer po
1aa3c 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 7a  inted.** to by z
1aa3d 4e 61 6d 65 20 77 69 6c 6c 20 62 65 20 66 72 65  Name will be fre
1aa3e 65 64 20 62 79 20 73 71 6c 69 74 65 33 44 62 46  ed by sqlite3DbF
1aa3f 72 65 65 28 29 20 77 68 65 6e 20 74 68 65 20 76  ree() when the v
1aa40 64 62 65 20 69 73 20 64 65 73 74 72 6f 79 65 64  dbe is destroyed
1aa41 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1aa42 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1aa43 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 0a 20  dbeSetColName(. 
1aa44 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20   Vdbe *p,       
1aa45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aa46 20 20 2f 2a 20 56 64 62 65 20 62 65 69 6e 67 20    /* Vdbe being 
1aa47 63 6f 6e 66 69 67 75 72 65 64 20 2a 2f 0a 20 20  configured */.  
1aa48 69 6e 74 20 69 64 78 2c 20 20 20 20 20 20 20 20  int idx,        
1aa49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1aa4a 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
1aa4b 75 6d 6e 20 7a 4e 61 6d 65 20 61 70 70 6c 69 65  umn zName applie
1aa4c 73 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 76 61  s to */.  int va
1aa4d 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
1aa4e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
1aa4f 65 20 6f 66 20 74 68 65 20 43 4f 4c 4e 41 4d 45  e of the COLNAME
1aa50 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a  _* constants */.
1aa51 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
1aa52 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ame,            
1aa53 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
1aa54 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   buffer containi
1aa55 6e 67 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69  ng name */.  voi
1aa56 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
1aa57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1aa58 20 4d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65   Memory manageme
1aa59 6e 74 20 73 74 72 61 74 65 67 79 20 66 6f 72 20  nt strategy for 
1aa5a 7a 4e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  zName */.){.  in
1aa5b 74 20 72 63 3b 0a 20 20 4d 65 6d 20 2a 70 43 6f  t rc;.  Mem *pCo
1aa5c 6c 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 28  lName;.  assert(
1aa5d 20 69 64 78 3c 70 2d 3e 6e 52 65 73 43 6f 6c 75   idx<p->nResColu
1aa5e 6d 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mn );.  assert( 
1aa5f 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f 4e 20 29 3b  var<COLNAME_N );
1aa60 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61  .  if( p->db->ma
1aa61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1aa62 20 20 61 73 73 65 72 74 28 20 21 7a 4e 61 6d 65    assert( !zName
1aa63 20 7c 7c 20 78 44 65 6c 21 3d 53 51 4c 49 54 45   || xDel!=SQLITE
1aa64 5f 44 59 4e 41 4d 49 43 20 29 3b 0a 20 20 20 20  _DYNAMIC );.    
1aa65 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
1aa66 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  MEM;.  }.  asser
1aa67 74 28 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 21 3d  t( p->aColName!=
1aa68 30 20 29 3b 0a 20 20 70 43 6f 6c 4e 61 6d 65 20  0 );.  pColName 
1aa69 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b  = &(p->aColName[
1aa6a 69 64 78 2b 76 61 72 2a 70 2d 3e 6e 52 65 73 43  idx+var*p->nResC
1aa6b 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 72 63 20 3d 20  olumn]);.  rc = 
1aa6c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1aa6d 74 53 74 72 28 70 43 6f 6c 4e 61 6d 65 2c 20 7a  tStr(pColName, z
1aa6e 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54 45  Name, -1, SQLITE
1aa6f 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 20 20  _UTF8, xDel);.  
1aa70 61 73 73 65 72 74 28 20 72 63 21 3d 30 20 7c 7c  assert( rc!=0 ||
1aa71 20 21 7a 4e 61 6d 65 20 7c 7c 20 28 70 43 6f 6c   !zName || (pCol
1aa72 4e 61 6d 65 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  Name->flags&MEM_
1aa73 54 65 72 6d 29 21 3d 30 20 29 3b 0a 20 20 72 65  Term)!=0 );.  re
1aa74 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1aa75 2a 20 41 20 72 65 61 64 20 6f 72 20 77 72 69 74  * A read or writ
1aa76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61  e transaction ma
1aa77 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20  y or may not be 
1aa78 61 63 74 69 76 65 20 6f 6e 20 64 61 74 61 62 61  active on databa
1aa79 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e  se handle.** db.
1aa7a 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   If a transactio
1aa7b 6e 20 69 73 20 61 63 74 69 76 65 2c 20 63 6f 6d  n is active, com
1aa7c 6d 69 74 20 69 74 2e 20 49 66 20 74 68 65 72 65  mit it. If there
1aa7d 20 69 73 20 61 0a 2a 2a 20 77 72 69 74 65 2d 74   is a.** write-t
1aa7e 72 61 6e 73 61 63 74 69 6f 6e 20 73 70 61 6e 6e  ransaction spann
1aa7f 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  ing more than on
1aa80 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
1aa81 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
1aa82 20 74 61 6b 65 73 20 63 61 72 65 20 6f 66 20 74   takes care of t
1aa83 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
1aa84 6c 20 74 72 69 63 6b 65 72 79 2e 0a 2a 2f 0a 73  l trickery..*/.s
1aa85 74 61 74 69 63 20 69 6e 74 20 76 64 62 65 43 6f  tatic int vdbeCo
1aa86 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  mmit(sqlite3 *db
1aa87 2c 20 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  , Vdbe *p){.  in
1aa88 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 72 61 6e  t i;.  int nTran
1aa89 73 20 3d 20 30 3b 20 20 2f 2a 20 4e 75 6d 62 65  s = 0;  /* Numbe
1aa8a 72 20 6f 66 20 64 61 74 61 62 61 73 65 73 20 77  r of databases w
1aa8b 69 74 68 20 61 6e 20 61 63 74 69 76 65 20 77 72  ith an active wr
1aa8c 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
1aa8d 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
1aa8e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e  LITE_OK;.  int n
1aa8f 65 65 64 58 63 6f 6d 6d 69 74 20 3d 20 30 3b 0a  eedXcommit = 0;.
1aa90 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
1aa91 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1aa92 0a 20 20 2f 2a 20 57 69 74 68 20 74 68 69 73 20  .  /* With this 
1aa93 6f 70 74 69 6f 6e 2c 20 73 71 6c 69 74 65 33 56  option, sqlite3V
1aa94 74 61 62 53 79 6e 63 28 29 20 69 73 20 64 65 66  tabSync() is def
1aa95 69 6e 65 64 20 74 6f 20 62 65 20 73 69 6d 70 6c  ined to be simpl
1aa96 79 20 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f  y .  ** SQLITE_O
1aa97 4b 20 73 6f 20 70 20 69 73 20 6e 6f 74 20 75 73  K so p is not us
1aa98 65 64 2e 20 0a 20 20 2a 2f 0a 20 20 55 4e 55 53  ed. .  */.  UNUS
1aa99 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 29 3b  ED_PARAMETER(p);
1aa9a 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65  .#endif..  /* Be
1aa9b 66 6f 72 65 20 64 6f 69 6e 67 20 61 6e 79 74 68  fore doing anyth
1aa9c 69 6e 67 20 65 6c 73 65 2c 20 63 61 6c 6c 20 74  ing else, call t
1aa9d 68 65 20 78 53 79 6e 63 28 29 20 63 61 6c 6c 62  he xSync() callb
1aa9e 61 63 6b 20 66 6f 72 20 61 6e 79 0a 20 20 2a 2a  ack for any.  **
1aa9f 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20   virtual module 
1aaa0 74 61 62 6c 65 73 20 77 72 69 74 74 65 6e 20 69  tables written i
1aaa1 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  n this transacti
1aaa2 6f 6e 2e 20 54 68 69 73 20 68 61 73 20 74 6f 0a  on. This has to.
1aaa3 20 20 2a 2a 20 62 65 20 64 6f 6e 65 20 62 65 66    ** be done bef
1aaa4 6f 72 65 20 64 65 74 65 72 6d 69 6e 69 6e 67 20  ore determining 
1aaa5 77 68 65 74 68 65 72 20 61 20 6d 61 73 74 65 72  whether a master
1aaa6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
1aaa7 20 0a 20 20 2a 2a 20 72 65 71 75 69 72 65 64 2c   .  ** required,
1aaa8 20 61 73 20 61 6e 20 78 53 79 6e 63 28 29 20 63   as an xSync() c
1aaa9 61 6c 6c 62 61 63 6b 20 6d 61 79 20 61 64 64 20  allback may add 
1aaaa 61 6e 20 61 74 74 61 63 68 65 64 20 64 61 74 61  an attached data
1aaab 62 61 73 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65  base.  ** to the
1aaac 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20   transaction..  
1aaad 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
1aaae 33 56 74 61 62 53 79 6e 63 28 64 62 2c 20 26 70  3VtabSync(db, &p
1aaaf 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 69 66  ->zErrMsg);.  if
1aab0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1aab1 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
1aab2 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73  ;.  }..  /* This
1aab3 20 6c 6f 6f 70 20 64 65 74 65 72 6d 69 6e 65 73   loop determines
1aab4 20 28 61 29 20 69 66 20 74 68 65 20 63 6f 6d 6d   (a) if the comm
1aab5 69 74 20 68 6f 6f 6b 20 73 68 6f 75 6c 64 20 62  it hook should b
1aab6 65 20 69 6e 76 6f 6b 65 64 20 61 6e 64 0a 20 20  e invoked and.  
1aab7 2a 2a 20 28 62 29 20 68 6f 77 20 6d 61 6e 79 20  ** (b) how many 
1aab8 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 68  database files h
1aab9 61 76 65 20 6f 70 65 6e 20 77 72 69 74 65 20 74  ave open write t
1aaba 72 61 6e 73 61 63 74 69 6f 6e 73 2c 20 6e 6f 74  ransactions, not
1aabb 20 0a 20 20 2a 2a 20 69 6e 63 6c 75 64 69 6e 67   .  ** including
1aabc 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61   the temp databa
1aabd 73 65 2e 20 28 62 29 20 69 73 20 69 6d 70 6f 72  se. (b) is impor
1aabe 74 61 6e 74 20 62 65 63 61 75 73 65 20 69 66 20  tant because if 
1aabf 6d 6f 72 65 20 74 68 61 6e 20 0a 20 20 2a 2a 20  more than .  ** 
1aac0 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  one database fil
1aac1 65 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 77 72  e has an open wr
1aac2 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  ite transaction,
1aac3 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
1aac4 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 73 20 72  l.  ** file is r
1aac5 65 71 75 69 72 65 64 20 66 6f 72 20 61 6e 20 61  equired for an a
1aac6 74 6f 6d 69 63 20 63 6f 6d 6d 69 74 2e 0a 20 20  tomic commit..  
1aac7 2a 2f 20 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  */ .  for(i=0; i
1aac8 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20  <db->nDb; i++){ 
1aac9 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20  .    Btree *pBt 
1aaca 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
1aacb 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1aacc 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28  3BtreeIsInTrans(
1aacd 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 6e 65  pBt) ){.      ne
1aace 65 64 58 63 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20  edXcommit = 1;. 
1aacf 20 20 20 20 20 69 66 28 20 69 21 3d 31 20 29 20       if( i!=1 ) 
1aad0 6e 54 72 61 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a  nTrans++;.    }.
1aad1 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
1aad2 72 65 20 61 72 65 20 61 6e 79 20 77 72 69 74 65  re are any write
1aad3 2d 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 74  -transactions at
1aad4 20 61 6c 6c 2c 20 69 6e 76 6f 6b 65 20 74 68 65   all, invoke the
1aad5 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a   commit hook */.
1aad6 20 20 69 66 28 20 6e 65 65 64 58 63 6f 6d 6d 69    if( needXcommi
1aad7 74 20 26 26 20 64 62 2d 3e 78 43 6f 6d 6d 69 74  t && db->xCommit
1aad8 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20  Callback ){.    
1aad9 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1aada 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20  etyOff(db);.    
1aadb 72 63 20 3d 20 64 62 2d 3e 78 43 6f 6d 6d 69 74  rc = db->xCommit
1aadc 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 43 6f  Callback(db->pCo
1aadd 6d 6d 69 74 41 72 67 29 3b 0a 20 20 20 20 28 76  mmitArg);.    (v
1aade 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
1aadf 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 69 66 28  yOn(db);.    if(
1aae0 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74   rc ){.      ret
1aae1 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  urn SQLITE_CONST
1aae2 52 41 49 4e 54 3b 0a 20 20 20 20 7d 0a 20 20 7d  RAINT;.    }.  }
1aae3 0a 0a 20 20 2f 2a 20 54 68 65 20 73 69 6d 70 6c  ..  /* The simpl
1aae4 65 20 63 61 73 65 20 2d 20 6e 6f 20 6d 6f 72 65  e case - no more
1aae5 20 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61   than one databa
1aae6 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 63 6f 75  se file (not cou
1aae7 6e 74 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 54  nting the.  ** T
1aae8 45 4d 50 20 64 61 74 61 62 61 73 65 29 20 68 61  EMP database) ha
1aae9 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  s a transaction 
1aaea 61 63 74 69 76 65 2e 20 20 20 54 68 65 72 65 20  active.   There 
1aaeb 69 73 20 6e 6f 20 6e 65 65 64 20 66 6f 72 20 74  is no need for t
1aaec 68 65 0a 20 20 2a 2a 20 6d 61 73 74 65 72 2d 6a  he.  ** master-j
1aaed 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a  ournal..  **.  *
1aaee 2a 20 49 66 20 74 68 65 20 72 65 74 75 72 6e 20  * If the return 
1aaef 76 61 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 33  value of sqlite3
1aaf0 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
1aaf1 28 29 20 69 73 20 61 20 7a 65 72 6f 20 6c 65 6e  () is a zero len
1aaf2 67 74 68 0a 20 20 2a 2a 20 73 74 72 69 6e 67 2c  gth.  ** string,
1aaf3 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 6d 61   it means the ma
1aaf4 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 3a  in database is :
1aaf5 6d 65 6d 6f 72 79 3a 20 6f 72 20 61 20 74 65 6d  memory: or a tem
1aaf6 70 20 66 69 6c 65 2e 20 20 49 6e 20 0a 20 20 2a  p file.  In .  *
1aaf7 2a 20 74 68 61 74 20 63 61 73 65 20 77 65 20 64  * that case we d
1aaf8 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61 74  o not support at
1aaf9 6f 6d 69 63 20 6d 75 6c 74 69 2d 66 69 6c 65 20  omic multi-file 
1aafa 63 6f 6d 6d 69 74 73 2c 20 73 6f 20 75 73 65 20  commits, so use 
1aafb 74 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65  the .  ** simple
1aafc 20 63 61 73 65 20 74 68 65 6e 20 74 6f 6f 2e 0a   case then too..
1aafd 20 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 73 71    */.  if( 0==sq
1aafe 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 73 71  lite3Strlen30(sq
1aaff 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c  lite3BtreeGetFil
1ab00 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b 30 5d  ename(db->aDb[0]
1ab01 2e 70 42 74 29 29 0a 20 20 20 7c 7c 20 6e 54 72  .pBt)).   || nTr
1ab02 61 6e 73 3c 3d 31 0a 20 20 29 7b 0a 20 20 20 20  ans<=1.  ){.    
1ab03 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  for(i=0; rc==SQL
1ab04 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e  ITE_OK && i<db->
1ab05 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nDb; i++){.     
1ab06 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
1ab07 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
1ab08 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20      if( pBt ){. 
1ab09 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1ab0a 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
1ab0b 61 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a  aseOne(pBt, 0);.
1ab0c 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
1ab0d 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d     /* Do the com
1ab0e 6d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20  mit only if all 
1ab0f 64 61 74 61 62 61 73 65 73 20 73 75 63 63 65 73  databases succes
1ab10 73 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20  sfully complete 
1ab11 70 68 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a  phase 1. .    **
1ab12 20 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42   If one of the B
1ab13 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f  treeCommitPhaseO
1ab14 6e 65 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73  ne() calls fails
1ab15 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73  , this indicates
1ab16 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72   an.    ** IO er
1ab17 72 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69  ror while deleti
1ab18 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67  ng or truncating
1ab19 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e   a journal file.
1ab1a 20 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c   It is unlikely,
1ab1b 0a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c  .    ** but coul
1ab1c 64 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69  d happen. In thi
1ab1d 73 20 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70  s case abandon p
1ab1e 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65  rocessing and re
1ab1f 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a  turn the error..
1ab20 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
1ab21 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
1ab22 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  K && i<db->nDb; 
1ab23 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65  i++){.      Btre
1ab24 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  e *pBt = db->aDb
1ab25 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69  [i].pBt;.      i
1ab26 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20  f( pBt ){.      
1ab27 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1ab28 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  reeCommitPhaseTw
1ab29 6f 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a  o(pBt);.      }.
1ab2a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
1ab2b 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1ab2c 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
1ab2d 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20  Commit(db);.    
1ab2e 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  }.  }..  /* The 
1ab2f 63 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54  complex case - T
1ab30 68 65 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d  here is a multi-
1ab31 66 69 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73  file write-trans
1ab32 61 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20  action active.. 
1ab33 20 2a 2a 20 54 68 69 73 20 72 65 71 75 69 72 65   ** This require
1ab34 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  s a master journ
1ab35 61 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72  al file to ensur
1ab36 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  e the transactio
1ab37 6e 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74  n is.  ** commit
1ab38 74 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20  ted atomicly..  
1ab39 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1ab3a 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20  E_OMIT_DISKIO.  
1ab3b 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
1ab3c 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62  3_vfs *pVfs = db
1ab3d 2d 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20  ->pVfs;.    int 
1ab3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20  needSync = 0;.  
1ab3f 20 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20    char *zMaster 
1ab40 3d 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e  = 0;   /* File-n
1ab41 61 6d 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74  ame for the mast
1ab42 65 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  er journal */.  
1ab43 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d    char const *zM
1ab44 61 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65  ainFile = sqlite
1ab45 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d  3BtreeGetFilenam
1ab46 65 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  e(db->aDb[0].pBt
1ab47 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
1ab48 69 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30  ile *pMaster = 0
1ab49 3b 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74  ;.    i64 offset
1ab4a 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65   = 0;.    int re
1ab4b 73 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63  s;..    /* Selec
1ab4c 74 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  t a master journ
1ab4d 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a  al file name */.
1ab4e 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 75      do {.      u
1ab4f 33 32 20 69 52 61 6e 64 6f 6d 3b 0a 20 20 20 20  32 iRandom;.    
1ab50 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1ab51 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20  db, zMaster);.  
1ab52 20 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64      sqlite3_rand
1ab53 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 69 52  omness(sizeof(iR
1ab54 61 6e 64 6f 6d 29 2c 20 26 69 52 61 6e 64 6f 6d  andom), &iRandom
1ab55 29 3b 0a 20 20 20 20 20 20 7a 4d 61 73 74 65 72  );.      zMaster
1ab56 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
1ab57 66 28 64 62 2c 20 22 25 73 2d 6d 6a 25 30 38 58  f(db, "%s-mj%08X
1ab58 22 2c 20 7a 4d 61 69 6e 46 69 6c 65 2c 20 69 52  ", zMainFile, iR
1ab59 61 6e 64 6f 6d 26 30 78 37 66 66 66 66 66 66 66  andom&0x7fffffff
1ab5a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 4d  );.      if( !zM
1ab5b 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 20  aster ){.       
1ab5c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
1ab5d 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OMEM;.      }.  
1ab5e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1ab5f 4f 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a  OsAccess(pVfs, z
1ab60 4d 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41  Master, SQLITE_A
1ab61 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72  CCESS_EXISTS, &r
1ab62 65 73 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  es);.    }while(
1ab63 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
1ab64 26 20 72 65 73 20 29 3b 0a 20 20 20 20 69 66 28  & res );.    if(
1ab65 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1ab66 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20  {.      /* Open 
1ab67 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1ab68 61 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  al. */.      rc 
1ab69 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d  = sqlite3OsOpenM
1ab6a 61 6c 6c 6f 63 28 70 56 66 73 2c 20 7a 4d 61 73  alloc(pVfs, zMas
1ab6b 74 65 72 2c 20 26 70 4d 61 73 74 65 72 2c 20 0a  ter, &pMaster, .
1ab6c 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
1ab6d 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c  _OPEN_READWRITE|
1ab6e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
1ab6f 54 45 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51  TE|.          SQ
1ab70 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
1ab71 49 56 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  IVE|SQLITE_OPEN_
1ab72 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 2c 20  MASTER_JOURNAL, 
1ab73 30 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d  0.      );.    }
1ab74 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1ab75 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1ab76 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1ab77 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  , zMaster);.    
1ab78 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1ab79 20 7d 0a 20 0a 20 20 20 20 2f 2a 20 57 72 69 74   }. .    /* Writ
1ab7a 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 65 61  e the name of ea
1ab7b 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ch database file
1ab7c 20 69 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74   in the transact
1ab7d 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20 6e 65 77  ion into the new
1ab7e 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 20 6a  .    ** master j
1ab7f 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
1ab80 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
1ab81 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 63 6c  at this point cl
1ab82 6f 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 64  ose.    ** and d
1ab83 65 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72  elete the master
1ab84 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41   journal file. A
1ab85 6c 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75 61  ll the individua
1ab86 6c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 0a  l journal files.
1ab87 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 68 61 76      ** still hav
1ab88 65 20 27 6e 75 6c 6c 27 20 61 73 20 74 68 65 20  e 'null' as the 
1ab89 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70  master journal p
1ab8a 6f 69 6e 74 65 72 2c 20 73 6f 20 74 68 65 79 20  ointer, so they 
1ab8b 77 69 6c 6c 20 72 6f 6c 6c 0a 20 20 20 20 2a 2a  will roll.    **
1ab8c 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64 65 6e   back independen
1ab8d 74 6c 79 20 69 66 20 61 20 66 61 69 6c 75 72 65  tly if a failure
1ab8e 20 6f 63 63 75 72 73 2e 0a 20 20 20 20 2a 2f 0a   occurs..    */.
1ab8f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64      for(i=0; i<d
1ab90 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
1ab91 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
1ab92 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
1ab93 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 31 20  .      if( i==1 
1ab94 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 20 2f 2a  ) continue;   /*
1ab95 20 49 67 6e 6f 72 65 20 74 68 65 20 54 45 4d 50   Ignore the TEMP
1ab96 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20   database */.   
1ab97 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74     if( sqlite3Bt
1ab98 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74  reeIsInTrans(pBt
1ab99 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  ) ){.        cha
1ab9a 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 20 3d  r const *zFile =
1ab9b 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
1ab9c 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 70 42 74 29  Journalname(pBt)
1ab9d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 46  ;.        if( zF
1ab9e 69 6c 65 5b 30 5d 3d 3d 30 20 29 20 63 6f 6e 74  ile[0]==0 ) cont
1ab9f 69 6e 75 65 3b 20 20 2f 2a 20 49 67 6e 6f 72 65  inue;  /* Ignore
1aba0 20 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61 62 61   :memory: databa
1aba1 73 65 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ses */.        i
1aba2 66 28 20 21 6e 65 65 64 53 79 6e 63 20 26 26 20  f( !needSync && 
1aba3 21 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e  !sqlite3BtreeSyn
1aba4 63 44 69 73 61 62 6c 65 64 28 70 42 74 29 20 29  cDisabled(pBt) )
1aba5 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 65 64  {.          need
1aba6 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20  Sync = 1;.      
1aba7 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d    }.        rc =
1aba8 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
1aba9 70 4d 61 73 74 65 72 2c 20 7a 46 69 6c 65 2c 20  pMaster, zFile, 
1abaa 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1abab 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66 73 65 74  zFile)+1, offset
1abac 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73 65  );.        offse
1abad 74 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  t += sqlite3Strl
1abae 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 3b 0a 20  en30(zFile)+1;. 
1abaf 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
1abb0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1abb1 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
1abb2 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72  loseFree(pMaster
1abb3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1abb4 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66  ite3OsDelete(pVf
1abb5 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a  s, zMaster, 0);.
1abb6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1abb7 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73  3DbFree(db, zMas
1abb8 74 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ter);.          
1abb9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
1abba 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1abbb 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20   }..    /* Sync 
1abbc 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1abbd 61 6c 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20  al file. If the 
1abbe 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c  IOCAP_SEQUENTIAL
1abbf 20 64 65 76 69 63 65 0a 20 20 20 20 2a 2a 20 66   device.    ** f
1abc0 6c 61 67 20 69 73 20 73 65 74 20 74 68 69 73 20  lag is set this 
1abc1 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e  is not required.
1abc2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1abc3 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 20 20 26  needSync .     &
1abc4 26 20 30 3d 3d 28 73 71 6c 69 74 65 33 4f 73 44  & 0==(sqlite3OsD
1abc5 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
1abc6 74 69 63 73 28 70 4d 61 73 74 65 72 29 26 53 51  tics(pMaster)&SQ
1abc7 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45  LITE_IOCAP_SEQUE
1abc8 4e 54 49 41 4c 29 0a 20 20 20 20 20 26 26 20 53  NTIAL).     && S
1abc9 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
1abca 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70 4d  sqlite3OsSync(pM
1abcb 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 53 59  aster, SQLITE_SY
1abcc 4e 43 5f 4e 4f 52 4d 41 4c 29 29 0a 20 20 20 20  NC_NORMAL)).    
1abcd 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1abce 4f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73  OsCloseFree(pMas
1abcf 74 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ter);.      sqli
1abd0 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73  te3OsDelete(pVfs
1abd1 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20  , zMaster, 0);. 
1abd2 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1abd3 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b  ee(db, zMaster);
1abd4 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
1abd5 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1abd6 53 79 6e 63 20 61 6c 6c 20 74 68 65 20 64 62 20  Sync all the db 
1abd7 66 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64 20 69  files involved i
1abd8 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  n the transactio
1abd9 6e 2e 20 54 68 65 20 73 61 6d 65 20 63 61 6c 6c  n. The same call
1abda 0a 20 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65  .    ** sets the
1abdb 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1abdc 70 6f 69 6e 74 65 72 20 69 6e 20 65 61 63 68 20  pointer in each 
1abdd 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e  individual journ
1abde 61 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e  al. If.    ** an
1abdf 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 68 65   error occurs he
1abe0 72 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74  re, do not delet
1abe1 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  e the master jou
1abe2 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a  rnal file..    *
1abe3 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20  *.    ** If the 
1abe4 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
1abe5 69 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61  ing the first ca
1abe6 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c  ll to.    ** sql
1abe7 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
1abe8 68 61 73 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20  haseOne(), then 
1abe9 74 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63  there is a chanc
1abea 65 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a  e that the.    *
1abeb 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  * master journal
1abec 20 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72   file will be or
1abed 70 68 61 6e 65 64 2e 20 42 75 74 20 77 65 20 63  phaned. But we c
1abee 61 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c  annot delete it,
1abef 0a 20 20 20 20 2a 2a 20 69 6e 20 63 61 73 65 20  .    ** in case 
1abf0 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
1abf1 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77 61 73  al file name was
1abf2 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
1abf3 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a  e journal.    **
1abf4 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 65   file before the
1abf5 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72 72 65   failure occurre
1abf6 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  d..    */.    fo
1abf7 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
1abf8 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44  E_OK && i<db->nD
1abf9 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20  b; i++){ .      
1abfa 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d  Btree *pBt = db-
1abfb 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
1abfc 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20     if( pBt ){.  
1abfd 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1abfe 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
1abff 73 65 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74  seOne(pBt, zMast
1ac00 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  er);.      }.   
1ac01 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73   }.    sqlite3Os
1ac02 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65  CloseFree(pMaste
1ac03 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  r);.    if( rc!=
1ac04 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1ac05 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1ac06 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20  (db, zMaster);. 
1ac07 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
1ac08 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65      }..    /* De
1ac09 6c 65 74 65 20 74 68 65 20 6d 61 73 74 65 72 20  lete the master 
1ac0a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68  journal file. Th
1ac0b 69 73 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 74  is commits the t
1ac0c 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 66 74 65  ransaction. Afte
1ac0d 72 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74  r.    ** doing t
1ac0e 68 69 73 20 74 68 65 20 64 69 72 65 63 74 6f 72  his the director
1ac0f 79 20 69 73 20 73 79 6e 63 65 64 20 61 67 61 69  y is synced agai
1ac10 6e 20 62 65 66 6f 72 65 20 61 6e 79 20 69 6e 64  n before any ind
1ac11 69 76 69 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74  ividual.    ** t
1ac12 72 61 6e 73 61 63 74 69 6f 6e 20 66 69 6c 65 73  ransaction files
1ac13 20 61 72 65 20 64 65 6c 65 74 65 64 2e 0a 20 20   are deleted..  
1ac14 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71    */.    rc = sq
1ac15 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56  lite3OsDelete(pV
1ac16 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b  fs, zMaster, 1);
1ac17 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1ac18 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b  ee(db, zMaster);
1ac19 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30  .    zMaster = 0
1ac1a 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
1ac1b 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1ac1c 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
1ac1d 6c 6c 20 66 69 6c 65 73 20 61 6e 64 20 64 69 72  ll files and dir
1ac1e 65 63 74 6f 72 69 65 73 20 68 61 76 65 20 61 6c  ectories have al
1ac1f 72 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65  ready been synce
1ac20 64 2c 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  d, so the follow
1ac21 69 6e 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73  ing.    ** calls
1ac22 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
1ac23 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29  CommitPhaseTwo()
1ac24 20 61 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e   are only closin
1ac25 67 20 66 69 6c 65 73 20 61 6e 64 0a 20 20 20 20  g files and.    
1ac26 2a 2a 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74  ** deleting or t
1ac27 72 75 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61  runcating journa
1ac28 6c 73 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e 67  ls. If something
1ac29 20 67 6f 65 73 20 77 72 6f 6e 67 20 77 68 69 6c   goes wrong whil
1ac2a 65 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73  e.    ** this is
1ac2b 20 68 61 70 70 65 6e 69 6e 67 20 77 65 20 64 6f   happening we do
1ac2c 6e 27 74 20 72 65 61 6c 6c 79 20 63 61 72 65 2e  n't really care.
1ac2d 20 54 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f   The integrity o
1ac2e 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61  f the.    ** tra
1ac2f 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65  nsaction is alre
1ac30 61 64 79 20 67 75 61 72 61 6e 74 65 65 64 2c 20  ady guaranteed, 
1ac31 62 75 74 20 73 6f 6d 65 20 73 74 72 61 79 20 27  but some stray '
1ac32 63 6f 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20  cold' journals. 
1ac33 20 20 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69     ** may be lyi
1ac34 6e 67 20 61 72 6f 75 6e 64 2e 20 52 65 74 75 72  ng around. Retur
1ac35 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f  ning an error co
1ac36 64 65 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61  de won't help ma
1ac37 74 74 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  tters..    */.  
1ac38 20 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61    disable_simula
1ac39 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b  ted_io_errors();
1ac3a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69  .    sqlite3Begi
1ac3b 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
1ac3c 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1ac3d 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a  db->nDb; i++){ .
1ac3e 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74        Btree *pBt
1ac3f 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
1ac40 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  t;.      if( pBt
1ac41 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1ac42 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
1ac43 61 73 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20  aseTwo(pBt);.   
1ac44 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
1ac45 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
1ac46 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61  alloc();.    ena
1ac47 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
1ac48 5f 65 72 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20  _errors();..    
1ac49 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69  sqlite3VtabCommi
1ac4a 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  t(db);.  }.#endi
1ac4b 66 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  f..  return rc;.
1ac4c 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72  }../* .** This r
1ac4d 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68  outine checks th
1ac4e 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61  at the sqlite3.a
1ac4f 63 74 69 76 65 56 64 62 65 43 6e 74 20 63 6f 75  ctiveVdbeCnt cou
1ac50 6e 74 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d  nt variable.** m
1ac51 61 74 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65  atches the numbe
1ac52 72 20 6f 66 20 76 64 62 65 27 73 20 69 6e 20 74  r of vdbe's in t
1ac53 68 65 20 6c 69 73 74 20 73 71 6c 69 74 65 33 2e  he list sqlite3.
1ac54 70 56 64 62 65 20 74 68 61 74 20 61 72 65 0a 2a  pVdbe that are.*
1ac55 2a 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69  * currently acti
1ac56 76 65 2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  ve. An assertion
1ac57 20 66 61 69 6c 73 20 69 66 20 74 68 65 20 74 77   fails if the tw
1ac58 6f 20 63 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20  o counts do not 
1ac59 6d 61 74 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69  match..** This i
1ac5a 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65  s an internal se
1ac5b 6c 66 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20  lf-check only - 
1ac5c 69 74 20 69 73 20 6e 6f 74 20 61 6e 20 65 73 73  it is not an ess
1ac5d 65 6e 74 69 61 6c 20 70 72 6f 63 65 73 73 69 6e  ential processin
1ac5e 67 0a 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a  g.** step..**.**
1ac5f 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70   This is a no-op
1ac60 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 64 65   if NDEBUG is de
1ac61 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  fined..*/.#ifnde
1ac62 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20  f NDEBUG.static 
1ac63 76 6f 69 64 20 63 68 65 63 6b 41 63 74 69 76 65  void checkActive
1ac64 56 64 62 65 43 6e 74 28 73 71 6c 69 74 65 33 20  VdbeCnt(sqlite3 
1ac65 2a 64 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b  *db){.  Vdbe *p;
1ac66 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a  .  int cnt = 0;.
1ac67 20 20 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30    int nWrite = 0
1ac68 3b 0a 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62  ;.  p = db->pVdb
1ac69 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b  e;.  while( p ){
1ac6a 0a 20 20 20 20 69 66 28 20 70 2d 3e 6d 61 67 69  .    if( p->magi
1ac6b 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c==VDBE_MAGIC_RU
1ac6c 4e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b  N && p->pc>=0 ){
1ac6d 0a 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20  .      cnt++;.  
1ac6e 20 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f      if( p->readO
1ac6f 6e 6c 79 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b  nly==0 ) nWrite+
1ac70 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d  +;.    }.    p =
1ac71 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20   p->pNext;.  }. 
1ac72 20 61 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62   assert( cnt==db
1ac73 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 20  ->activeVdbeCnt 
1ac74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 57 72  );.  assert( nWr
1ac75 69 74 65 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64  ite==db->writeVd
1ac76 62 65 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65  beCnt );.}.#else
1ac77 0a 23 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63  .#define checkAc
1ac78 74 69 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23  tiveVdbeCnt(x).#
1ac79 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72  endif../*.** For
1ac7a 20 65 76 65 72 79 20 42 74 72 65 65 20 74 68 61   every Btree tha
1ac7b 74 20 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f  t in database co
1ac7c 6e 6e 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63  nnection db whic
1ac7d 68 20 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d  h .** has been m
1ac7e 6f 64 69 66 69 65 64 2c 20 22 74 72 69 70 22 20  odified, "trip" 
1ac7f 6f 72 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61  or invalidate ea
1ac80 63 68 20 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20  ch cursor in.** 
1ac81 74 68 61 74 20 42 74 72 65 65 20 6d 69 67 68 74  that Btree might
1ac82 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66   have been modif
1ac83 69 65 64 20 73 6f 20 74 68 61 74 20 74 68 65 20  ied so that the 
1ac84 63 75 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65  cursor.** can ne
1ac85 76 65 72 20 62 65 20 75 73 65 64 20 61 67 61 69  ver be used agai
1ac86 6e 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73  n.  This happens
1ac87 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b   when a rollback
1ac88 0a 2a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 57 65  .*** occurs.  We
1ac89 20 68 61 76 65 20 74 6f 20 74 72 69 70 20 61 6c   have to trip al
1ac8a 6c 20 74 68 65 20 6f 74 68 65 72 20 63 75 72 73  l the other curs
1ac8b 6f 72 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72  ors, even.** cur
1ac8c 73 6f 72 20 66 72 6f 6d 20 6f 74 68 65 72 20 56  sor from other V
1ac8d 4d 73 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20  Ms in different 
1ac8e 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1ac8f 69 6f 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74  ions,.** so that
1ac90 20 6e 6f 6e 65 20 6f 66 20 74 68 65 6d 20 74 72   none of them tr
1ac91 79 20 74 6f 20 75 73 65 20 74 68 65 20 64 61 74  y to use the dat
1ac92 61 20 61 74 20 77 68 69 63 68 20 74 68 65 79 0a  a at which they.
1ac93 2a 2a 20 77 65 72 65 20 70 6f 69 6e 74 69 6e 67  ** were pointing
1ac94 20 61 6e 64 20 77 68 69 63 68 20 6e 6f 77 20 6d   and which now m
1ac95 61 79 20 68 61 76 65 20 62 65 65 6e 20 63 68 61  ay have been cha
1ac96 6e 67 65 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74  nged due.** to t
1ac97 68 65 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a  he rollback..**.
1ac98 2a 2a 20 52 65 6d 65 6d 62 65 72 20 74 68 61 74  ** Remember that
1ac99 20 61 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20   a rollback can 
1ac9a 64 65 6c 65 74 65 20 74 61 62 6c 65 73 20 63 6f  delete tables co
1ac9b 6d 70 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65  mplete and.** re
1ac9c 6f 72 64 65 72 20 72 6f 6f 74 70 61 67 65 73 2e  order rootpages.
1ac9d 20 20 53 6f 20 69 74 20 69 73 20 6e 6f 74 20 73    So it is not s
1ac9e 75 66 66 69 63 69 65 6e 74 20 6a 75 73 74 20 74  ufficient just t
1ac9f 6f 20 73 61 76 65 0a 2a 2a 20 74 68 65 20 73 74  o save.** the st
1aca0 61 74 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f  ate of the curso
1aca1 72 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 69  r.  We have to i
1aca2 6e 76 61 6c 69 64 61 74 65 20 74 68 65 20 63 75  nvalidate the cu
1aca3 72 73 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20  rsor.** so that 
1aca4 69 74 20 69 73 20 6e 65 76 65 72 20 75 73 65 64  it is never used
1aca5 20 61 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69   again..*/.stati
1aca6 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74  c void invalidat
1aca7 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69  eCursorsOnModifi
1aca8 65 64 42 74 72 65 65 73 28 73 71 6c 69 74 65 33  edBtrees(sqlite3
1aca9 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   *db){.  int i;.
1acaa 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
1acab 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
1acac 42 74 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61  Btree *p = db->a
1acad 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69  Db[i].pBt;.    i
1acae 66 28 20 70 20 26 26 20 73 71 6c 69 74 65 33 42  f( p && sqlite3B
1acaf 74 72 65 65 49 73 49 6e 54 72 61 6e 73 28 70 29  treeIsInTrans(p)
1acb0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1acb1 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72  3BtreeTripAllCur
1acb2 73 6f 72 73 28 70 2c 20 53 51 4c 49 54 45 5f 41  sors(p, SQLITE_A
1acb3 42 4f 52 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  BORT);.    }.  }
1acb4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
1acb5 20 56 64 62 65 20 70 61 73 73 65 64 20 61 73 20   Vdbe passed as 
1acb6 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
1acb7 6e 74 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74  nt opened a stat
1acb8 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f  ement-transactio
1acb9 6e 2c 0a 2a 2a 20 63 6c 6f 73 65 20 69 74 20 6e  n,.** close it n
1acba 6f 77 2e 20 41 72 67 75 6d 65 6e 74 20 65 4f 70  ow. Argument eOp
1acbb 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
1acbc 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
1acbd 43 4b 20 6f 72 0a 2a 2a 20 53 41 56 45 50 4f 49  CK or.** SAVEPOI
1acbe 4e 54 5f 52 45 4c 45 41 53 45 2e 20 49 66 20 69  NT_RELEASE. If i
1acbf 74 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52  t is SAVEPOINT_R
1acc0 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 74 68  OLLBACK, then th
1acc1 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74  e statement.** t
1acc2 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f  ransaction is ro
1acc3 6c 6c 65 64 20 62 61 63 6b 2e 20 49 66 20 65 4f  lled back. If eO
1acc4 70 20 69 73 20 53 41 56 45 50 4f 49 4e 54 5f 52  p is SAVEPOINT_R
1acc5 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 74 68 65  ELEASE, then the
1acc6 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74   .** statement t
1acc7 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
1acc8 6d 6d 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  mmtted..**.** If
1acc9 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63 63   an IO error occ
1acca 75 72 73 2c 20 61 6e 20 53 51 4c 49 54 45 5f 49  urs, an SQLITE_I
1accb 4f 45 52 52 5f 58 58 58 20 65 72 72 6f 72 20 63  OERR_XXX error c
1accc 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
1accd 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 20 53   .** Otherwise S
1acce 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c  QLITE_OK..*/.SQL
1accf 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1acd0 73 71 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65  sqlite3VdbeClose
1acd1 53 74 61 74 65 6d 65 6e 74 28 56 64 62 65 20 2a  Statement(Vdbe *
1acd2 70 2c 20 69 6e 74 20 65 4f 70 29 7b 0a 20 20 73  p, int eOp){.  s
1acd3 71 6c 69 74 65 33 20 2a 63 6f 6e 73 74 20 64 62  qlite3 *const db
1acd4 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20   = p->db;.  int 
1acd5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1acd6 0a 20 20 2f 2a 20 49 66 20 70 2d 3e 69 53 74 61  .  /* If p->iSta
1acd7 74 65 6d 65 6e 74 20 69 73 20 67 72 65 61 74 65  tement is greate
1acd8 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65  r than zero, the
1acd9 6e 20 74 68 69 73 20 56 64 62 65 20 6f 70 65 6e  n this Vdbe open
1acda 65 64 20 61 20 0a 20 20 2a 2a 20 73 74 61 74 65  ed a .  ** state
1acdb 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1acdc 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20   that should be 
1acdd 63 6c 6f 73 65 64 20 68 65 72 65 2e 20 54 68 65  closed here. The
1acde 20 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 0a   only exception.
1acdf 20 20 2a 2a 20 69 73 20 74 68 61 74 20 61 6e 20    ** is that an 
1ace0 49 4f 20 65 72 72 6f 72 20 6d 61 79 20 68 61 76  IO error may hav
1ace1 65 20 6f 63 63 75 72 65 64 2c 20 63 61 75 73 69  e occured, causi
1ace2 6e 67 20 61 6e 20 65 6d 65 72 67 65 6e 63 79 20  ng an emergency 
1ace3 72 6f 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2a 20 49  rollback..  ** I
1ace4 6e 20 74 68 69 73 20 63 61 73 65 20 28 64 62 2d  n this case (db-
1ace5 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 29 2c  >nStatement==0),
1ace6 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f   and there is no
1ace7 74 68 69 6e 67 20 74 6f 20 64 6f 2e 0a 20 20 2a  thing to do..  *
1ace8 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6e 53 74 61  /.  if( db->nSta
1ace9 74 65 6d 65 6e 74 20 26 26 20 70 2d 3e 69 53 74  tement && p->iSt
1acea 61 74 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69  atement ){.    i
1aceb 6e 74 20 69 3b 0a 20 20 20 20 63 6f 6e 73 74 20  nt i;.    const 
1acec 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 20 3d  int iSavepoint =
1aced 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 2d 31   p->iStatement-1
1acee 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 65  ;..    assert( e
1acef 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  Op==SAVEPOINT_RO
1acf0 4c 4c 42 41 43 4b 20 7c 7c 20 65 4f 70 3d 3d 53  LLBACK || eOp==S
1acf1 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
1acf2 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64  );.    assert( d
1acf3 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3e 30 20  b->nStatement>0 
1acf4 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1acf5 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d 3d 28 64  ->iStatement==(d
1acf6 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 64 62  b->nStatement+db
1acf7 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 29 20 29 3b  ->nSavepoint) );
1acf8 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ..    for(i=0; i
1acf9 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20  <db->nDb; i++){ 
1acfa 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 20 3d  .      int rc2 =
1acfb 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
1acfc 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64    Btree *pBt = d
1acfd 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
1acfe 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a       if( pBt ){.
1acff 20 20 20 20 20 20 20 20 69 66 28 20 65 4f 70 3d          if( eOp=
1ad00 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42  =SAVEPOINT_ROLLB
1ad01 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ACK ){.         
1ad02 20 72 63 32 20 3d 20 73 71 6c 69 74 65 33 42 74   rc2 = sqlite3Bt
1ad03 72 65 65 53 61 76 65 70 6f 69 6e 74 28 70 42 74  reeSavepoint(pBt
1ad04 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  , SAVEPOINT_ROLL
1ad05 42 41 43 4b 2c 20 69 53 61 76 65 70 6f 69 6e 74  BACK, iSavepoint
1ad06 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1ad07 20 20 20 20 20 69 66 28 20 72 63 32 3d 3d 53 51       if( rc2==SQ
1ad08 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1ad09 20 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69 74       rc2 = sqlit
1ad0a 65 33 42 74 72 65 65 53 61 76 65 70 6f 69 6e 74  e3BtreeSavepoint
1ad0b 28 70 42 74 2c 20 53 41 56 45 50 4f 49 4e 54 5f  (pBt, SAVEPOINT_
1ad0c 52 45 4c 45 41 53 45 2c 20 69 53 61 76 65 70 6f  RELEASE, iSavepo
1ad0d 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  int);.        }.
1ad0e 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
1ad0f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1ad10 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b         rc = rc2;
1ad11 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1ad12 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d   }.    }.    db-
1ad13 3e 6e 53 74 61 74 65 6d 65 6e 74 2d 2d 3b 0a 20  >nStatement--;. 
1ad14 20 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74     p->iStatement
1ad15 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66   = 0;..    /* If
1ad16 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74   the statement t
1ad17 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 62 65  ransaction is be
1ad18 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c  ing rolled back,
1ad19 20 61 6c 73 6f 20 72 65 73 74 6f 72 65 20 74 68   also restore th
1ad1a 65 20 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61  e .    ** databa
1ad1b 73 65 20 68 61 6e 64 6c 65 73 20 64 65 66 65 72  se handles defer
1ad1c 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  red constraint c
1ad1d 6f 75 6e 74 65 72 20 74 6f 20 74 68 65 20 76 61  ounter to the va
1ad1e 6c 75 65 20 69 74 20 68 61 64 20 77 68 65 6e 20  lue it had when 
1ad1f 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 74 61 74  .    ** the stat
1ad20 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1ad21 6e 20 77 61 73 20 6f 70 65 6e 65 64 2e 20 20 2a  n was opened.  *
1ad22 2f 0a 20 20 20 20 69 66 28 20 65 4f 70 3d 3d 53  /.    if( eOp==S
1ad23 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
1ad24 4b 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6e  K ){.      db->n
1ad25 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 70  DeferredCons = p
1ad26 2d 3e 6e 53 74 6d 74 44 65 66 43 6f 6e 73 3b 0a  ->nStmtDefCons;.
1ad27 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1ad28 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1ad29 49 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  If SQLite is com
1ad2a 70 69 6c 65 64 20 74 6f 20 73 75 70 70 6f 72 74  piled to support
1ad2b 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f   shared-cache mo
1ad2c 64 65 20 61 6e 64 20 74 6f 20 62 65 20 74 68 72  de and to be thr
1ad2d 65 61 64 73 61 66 65 2c 0a 2a 2a 20 74 68 69 73  eadsafe,.** this
1ad2e 20 72 6f 75 74 69 6e 65 20 6f 62 74 61 69 6e 73   routine obtains
1ad2f 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63   the mutex assoc
1ad30 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20  iated with each 
1ad31 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75  BtShared structu
1ad32 72 65 0a 2a 2a 20 74 68 61 74 20 6d 61 79 20 62  re.** that may b
1ad33 65 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68  e accessed by th
1ad34 65 20 56 4d 20 70 61 73 73 65 64 20 61 73 20 61  e VM passed as a
1ad35 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49 6e 20 64  n argument. In d
1ad36 6f 69 6e 67 20 73 6f 20 69 74 0a 2a 2a 20 73 65  oing so it.** se
1ad37 74 73 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  ts the BtShared.
1ad38 64 62 20 6d 65 6d 62 65 72 20 6f 66 20 65 61 63  db member of eac
1ad39 68 20 6f 66 20 74 68 65 20 42 74 53 68 61 72 65  h of the BtShare
1ad3a 64 20 73 74 72 75 63 74 75 72 65 73 2c 20 65 6e  d structures, en
1ad3b 73 75 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 74  suring.** that t
1ad3c 68 65 20 63 6f 72 72 65 63 74 20 62 75 73 79 2d  he correct busy-
1ad3d 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b  handler callback
1ad3e 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 66 20 72   is invoked if r
1ad3f 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  equired..**.** I
1ad40 66 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74 20  f SQLite is not 
1ad41 74 68 72 65 61 64 73 61 66 65 20 62 75 74 20 64  threadsafe but d
1ad42 6f 65 73 20 73 75 70 70 6f 72 74 20 73 68 61 72  oes support shar
1ad43 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c 20 74  ed-cache mode, t
1ad44 68 65 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 42 74  hen.** sqlite3Bt
1ad45 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 20 69 73  reeEnterAll() is
1ad46 20 69 6e 76 6f 6b 65 64 20 74 6f 20 73 65 74 20   invoked to set 
1ad47 74 68 65 20 42 74 53 68 61 72 65 64 2e 64 62 20  the BtShared.db 
1ad48 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20 6f 66 20  variables.** of 
1ad49 61 6c 6c 20 6f 66 20 42 74 53 68 61 72 65 64 20  all of BtShared 
1ad4a 73 74 72 75 63 74 75 72 65 73 20 61 63 63 65 73  structures acces
1ad4b 73 69 62 6c 65 20 76 69 61 20 74 68 65 20 64 61  sible via the da
1ad4c 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 0a 2a  tabase handle .*
1ad4d 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
1ad4e 68 20 74 68 65 20 56 4d 2e 20 4f 66 20 63 6f 75  h the VM. Of cou
1ad4f 72 73 65 20 6f 6e 6c 79 20 61 20 73 75 62 73 65  rse only a subse
1ad50 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63  t of these struc
1ad51 74 75 72 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65  tures.** will be
1ad52 20 61 63 63 65 73 73 65 64 20 62 79 20 74 68 65   accessed by the
1ad53 20 56 4d 2c 20 61 6e 64 20 77 65 20 63 6f 75 6c   VM, and we coul
1ad54 64 20 75 73 65 20 56 64 62 65 2e 62 74 72 65 65  d use Vdbe.btree
1ad55 4d 61 73 6b 20 74 6f 20 66 69 67 75 72 65 0a 2a  Mask to figure.*
1ad56 2a 20 74 68 61 74 20 73 75 62 73 65 74 20 6f 75  * that subset ou
1ad57 74 2c 20 62 75 74 20 74 68 65 72 65 20 69 73 20  t, but there is 
1ad58 6e 6f 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20  no advantage to 
1ad59 64 6f 69 6e 67 20 73 6f 2e 0a 2a 2a 0a 2a 2a 20  doing so..**.** 
1ad5a 49 66 20 53 51 4c 69 74 65 20 69 73 20 6e 6f 74  If SQLite is not
1ad5b 20 74 68 72 65 61 64 73 61 66 65 20 61 6e 64 20   threadsafe and 
1ad5c 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74  does not support
1ad5d 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d 6f   shared-cache mo
1ad5e 64 65 2c 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63  de, this.** func
1ad5f 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
1ad60 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1ad61 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
1ad62 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49 56  ACHE.SQLITE_PRIV
1ad63 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1ad64 56 64 62 65 4d 75 74 65 78 41 72 72 61 79 45 6e  VdbeMutexArrayEn
1ad65 74 65 72 28 56 64 62 65 20 2a 70 29 7b 0a 23 69  ter(Vdbe *p){.#i
1ad66 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
1ad67 41 46 45 0a 20 20 73 71 6c 69 74 65 33 42 74 72  AFE.  sqlite3Btr
1ad68 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65  eeMutexArrayEnte
1ad69 72 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 23  r(&p->aMutex);.#
1ad6a 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 42 74  else.  sqlite3Bt
1ad6b 72 65 65 45 6e 74 65 72 41 6c 6c 28 70 2d 3e 64  reeEnterAll(p->d
1ad6c 62 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 23 65 6e  b);.#endif.}.#en
1ad6d 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
1ad6e 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1ad6f 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61  ed when a transa
1ad70 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 62 79 20  ction opened by 
1ad71 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 2a 2a  the database .**
1ad72 20 68 61 6e 64 6c 65 20 61 73 73 6f 63 69 61 74   handle associat
1ad73 65 64 20 77 69 74 68 20 74 68 65 20 56 4d 20 70  ed with the VM p
1ad74 61 73 73 65 64 20 61 73 20 61 6e 20 61 72 67 75  assed as an argu
1ad75 6d 65 6e 74 20 69 73 20 61 62 6f 75 74 20 74 6f  ment is about to
1ad76 20 62 65 20 0a 2a 2a 20 63 6f 6d 6d 69 74 74 65   be .** committe
1ad77 64 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20  d. If there are 
1ad78 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65 66 65  outstanding defe
1ad79 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79  rred foreign key
1ad7a 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76   constraint.** v
1ad7b 69 6f 6c 61 74 69 6f 6e 73 2c 20 72 65 74 75 72  iolations, retur
1ad7c 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20  n SQLITE_ERROR. 
1ad7d 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c 49 54  Otherwise, SQLIT
1ad7e 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  E_OK..**.** If t
1ad7f 68 65 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e  here are outstan
1ad80 64 69 6e 67 20 46 4b 20 76 69 6f 6c 61 74 69 6f  ding FK violatio
1ad81 6e 73 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63  ns and this func
1ad82 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a 2a 2a  tion returns .**
1ad83 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c 20 73   SQLITE_ERROR, s
1ad84 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  et the result of
1ad85 20 74 68 65 20 56 4d 20 74 6f 20 53 51 4c 49 54   the VM to SQLIT
1ad86 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64  E_CONSTRAINT and
1ad87 20 77 72 69 74 65 0a 2a 2a 20 61 6e 20 65 72 72   write.** an err
1ad88 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 69 74  or message to it
1ad89 2e 20 54 68 65 6e 20 72 65 74 75 72 6e 20 53 51  . Then return SQ
1ad8a 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2f 0a 23  LITE_ERROR..*/.#
1ad8b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1ad8c 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 53  IT_FOREIGN_KEY.S
1ad8d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1ad8e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 65  t sqlite3VdbeChe
1ad8f 63 6b 46 6b 28 56 64 62 65 20 2a 70 2c 20 69 6e  ckFk(Vdbe *p, in
1ad90 74 20 64 65 66 65 72 72 65 64 29 7b 0a 20 20 73  t deferred){.  s
1ad91 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
1ad92 64 62 3b 0a 20 20 69 66 28 20 28 64 65 66 65 72  db;.  if( (defer
1ad93 72 65 64 20 26 26 20 64 62 2d 3e 6e 44 65 66 65  red && db->nDefe
1ad94 72 72 65 64 43 6f 6e 73 3e 30 29 20 7c 7c 20 28  rredCons>0) || (
1ad95 21 64 65 66 65 72 72 65 64 20 26 26 20 70 2d 3e  !deferred && p->
1ad96 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3e 30 29  nFkConstraint>0)
1ad97 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
1ad98 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
1ad99 54 3b 0a 20 20 20 20 70 2d 3e 65 72 72 6f 72 41  T;.    p->errorA
1ad9a 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74  ction = OE_Abort
1ad9b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  ;.    sqlite3Set
1ad9c 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1ad9d 73 67 2c 20 64 62 2c 20 22 66 6f 72 65 69 67 6e  sg, db, "foreign
1ad9e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
1ad9f 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20 72 65  failed");.    re
1ada0 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1ada1 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  R;.  }.  return 
1ada2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
1ada3 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
1ada4 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
1ada5 64 20 74 68 65 20 77 68 65 6e 20 61 20 56 44 42  d the when a VDB
1ada6 45 20 74 72 69 65 73 20 74 6f 20 68 61 6c 74 2e  E tries to halt.
1ada7 20 20 49 66 20 74 68 65 20 56 44 42 45 0a 2a 2a    If the VDBE.**
1ada8 20 68 61 73 20 6d 61 64 65 20 63 68 61 6e 67 65   has made change
1ada9 73 20 61 6e 64 20 69 73 20 69 6e 20 61 75 74 6f  s and is in auto
1adaa 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 74 68 65  commit mode, the
1adab 6e 20 63 6f 6d 6d 69 74 20 74 68 6f 73 65 0a 2a  n commit those.*
1adac 2a 20 63 68 61 6e 67 65 73 2e 20 20 49 66 20 61  * changes.  If a
1adad 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 6e 65 65   rollback is nee
1adae 64 65 64 2c 20 74 68 65 6e 20 64 6f 20 74 68 65  ded, then do the
1adaf 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   rollback..**.**
1adb0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1adb1 20 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f   the only way to
1adb2 20 6d 6f 76 65 20 74 68 65 20 73 74 61 74 65 20   move the state 
1adb3 6f 66 20 61 20 56 4d 20 66 72 6f 6d 0a 2a 2a 20  of a VM from.** 
1adb4 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 52 55 4e  SQLITE_MAGIC_RUN
1adb5 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49 43   to SQLITE_MAGIC
1adb6 5f 48 41 4c 54 2e 20 20 49 74 20 69 73 20 68 61  _HALT.  It is ha
1adb7 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20 63 61 6c  rmless to.** cal
1adb8 6c 20 74 68 69 73 20 6f 6e 20 61 20 56 4d 20 74  l this on a VM t
1adb9 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 53 51  hat is in the SQ
1adba 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20  LITE_MAGIC_HALT 
1adbb 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  state..**.** Ret
1adbc 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  urn an error cod
1adbd 65 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d 69  e.  If the commi
1adbe 74 20 63 6f 75 6c 64 20 6e 6f 74 20 63 6f 6d 70  t could not comp
1adbf 6c 65 74 65 20 62 65 63 61 75 73 65 20 6f 66 0a  lete because of.
1adc0 2a 2a 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69  ** lock contenti
1adc1 6f 6e 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  on, return SQLIT
1adc2 45 5f 42 55 53 59 2e 20 20 49 66 20 53 51 4c 49  E_BUSY.  If SQLI
1adc3 54 45 5f 42 55 53 59 20 69 73 20 72 65 74 75 72  TE_BUSY is retur
1adc4 6e 65 64 2c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73  ned, it.** means
1adc5 20 74 68 65 20 63 6c 6f 73 65 20 64 69 64 20 6e   the close did n
1adc6 6f 74 20 68 61 70 70 65 6e 20 61 6e 64 20 6e 65  ot happen and ne
1adc7 65 64 73 20 74 6f 20 62 65 20 72 65 70 65 61 74  eds to be repeat
1adc8 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
1adc9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1adca 33 56 64 62 65 48 61 6c 74 28 56 64 62 65 20 2a  3VdbeHalt(Vdbe *
1adcb 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  p){.  int rc;   
1adcc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1adcd 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
1adce 20 73 74 6f 72 65 20 74 72 61 6e 73 69 65 6e 74   store transient
1adcf 20 72 65 74 75 72 6e 20 63 6f 64 65 73 20 2a 2f   return codes */
1add0 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1add1 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 54 68   p->db;..  /* Th
1add2 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74  is function cont
1add3 61 69 6e 73 20 74 68 65 20 6c 6f 67 69 63 20 74  ains the logic t
1add4 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 20 69  hat determines i
1add5 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 72  f a statement or
1add6 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  .  ** transactio
1add7 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74  n will be commit
1add8 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64 20 62 61  ted or rolled ba
1add9 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74 20 6f  ck as a result o
1adda 66 20 74 68 65 0a 20 20 2a 2a 20 65 78 65 63 75  f the.  ** execu
1addb 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 76 69 72  tion of this vir
1addc 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 0a 20  tual machine. . 
1addd 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61 6e 79 20   **.  ** If any 
1adde 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
1addf 20 65 72 72 6f 72 73 20 6f 63 63 75 72 3a 0a 20   errors occur:. 
1ade0 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c   **.  **     SQL
1ade1 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a 2a 20 20  ITE_NOMEM.  **  
1ade2 20 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a     SQLITE_IOERR.
1ade3 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f    **     SQLITE_
1ade4 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 53 51  FULL.  **     SQ
1ade5 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 0a 20  LITE_INTERRUPT. 
1ade6 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e 20 74 68   **.  ** Then th
1ade7 65 20 69 6e 74 65 72 6e 61 6c 20 63 61 63 68 65  e internal cache
1ade8 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 65 6e   might have been
1ade9 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f   left in an inco
1adea 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a 20 73 74  nsistent.  ** st
1adeb 61 74 65 2e 20 20 57 65 20 6e 65 65 64 20 74 6f  ate.  We need to
1adec 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 74   rollback the st
1aded 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74  atement transact
1adee 69 6f 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73  ion, if there is
1adef 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72 20 74 68  .  ** one, or th
1adf0 65 20 63 6f 6d 70 6c 65 74 65 20 74 72 61 6e 73  e complete trans
1adf1 61 63 74 69 6f 6e 20 69 66 20 74 68 65 72 65 20  action if there 
1adf2 69 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20  is no statement 
1adf3 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a  transaction..  *
1adf4 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64 62 2d 3e  /..  if( p->db->
1adf5 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
1adf6 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49      p->rc = SQLI
1adf7 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
1adf8 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 28  closeAllCursors(
1adf9 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67  p);.  if( p->mag
1adfa 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic!=VDBE_MAGIC_R
1adfb 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  UN ){.    return
1adfc 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
1adfd 20 20 63 68 65 63 6b 41 63 74 69 76 65 56 64 62    checkActiveVdb
1adfe 65 43 6e 74 28 64 62 29 3b 0a 0a 20 20 2f 2a 20  eCnt(db);..  /* 
1adff 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c  No commit or rol
1ae00 6c 62 61 63 6b 20 6e 65 65 64 65 64 20 69 66 20  lback needed if 
1ae01 74 68 65 20 70 72 6f 67 72 61 6d 20 6e 65 76 65  the program neve
1ae02 72 20 73 74 61 72 74 65 64 20 2a 2f 0a 20 20 69  r started */.  i
1ae03 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  f( p->pc>=0 ){. 
1ae04 20 20 20 69 6e 74 20 6d 72 63 3b 20 20 20 2f 2a     int mrc;   /*
1ae05 20 50 72 69 6d 61 72 79 20 65 72 72 6f 72 20 63   Primary error c
1ae06 6f 64 65 20 66 72 6f 6d 20 70 2d 3e 72 63 20 2a  ode from p->rc *
1ae07 2f 0a 20 20 20 20 69 6e 74 20 65 53 74 61 74 65  /.    int eState
1ae08 6d 65 6e 74 4f 70 20 3d 20 30 3b 0a 20 20 20 20  mentOp = 0;.    
1ae09 69 6e 74 20 69 73 53 70 65 63 69 61 6c 45 72 72  int isSpecialErr
1ae0a 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  or;            /
1ae0b 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 66  * Set to true if
1ae0c 20 61 20 27 73 70 65 63 69 61 6c 27 20 65 72 72   a 'special' err
1ae0d 6f 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f  or */..    /* Lo
1ae0e 63 6b 20 61 6c 6c 20 62 74 72 65 65 73 20 75 73  ck all btrees us
1ae0f 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d  ed by the statem
1ae10 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ent */.    sqlit
1ae11 65 33 56 64 62 65 4d 75 74 65 78 41 72 72 61 79  e3VdbeMutexArray
1ae12 45 6e 74 65 72 28 70 29 3b 0a 0a 20 20 20 20 2f  Enter(p);..    /
1ae13 2a 20 43 68 65 63 6b 20 66 6f 72 20 6f 6e 65 20  * Check for one 
1ae14 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65  of the special e
1ae15 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 6d 72 63  rrors */.    mrc
1ae16 20 3d 20 70 2d 3e 72 63 20 26 20 30 78 66 66 3b   = p->rc & 0xff;
1ae17 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e  .    assert( p->
1ae18 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc!=SQLITE_IOERR
1ae19 5f 42 4c 4f 43 4b 45 44 20 29 3b 20 20 2f 2a 20  _BLOCKED );  /* 
1ae1a 54 68 69 73 20 65 72 72 6f 72 20 6e 6f 20 6c 6f  This error no lo
1ae1b 6e 67 65 72 20 65 78 69 73 74 73 20 2a 2f 0a 20  nger exists */. 
1ae1c 20 20 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f     isSpecialErro
1ae1d 72 20 3d 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f  r = mrc==SQLITE_
1ae1e 4e 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51  NOMEM || mrc==SQ
1ae1f 4c 49 54 45 5f 49 4f 45 52 52 0a 20 20 20 20 20  LITE_IOERR.     
1ae20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ae21 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49  || mrc==SQLITE_I
1ae22 4e 54 45 52 52 55 50 54 20 7c 7c 20 6d 72 63 3d  NTERRUPT || mrc=
1ae23 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20  =SQLITE_FULL;.  
1ae24 20 20 69 66 28 20 69 73 53 70 65 63 69 61 6c 45    if( isSpecialE
1ae25 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 2f 2a  rror ){.      /*
1ae26 20 49 66 20 74 68 65 20 71 75 65 72 79 20 77 61   If the query wa
1ae27 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 77 65 20  s read-only, we 
1ae28 6e 65 65 64 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62  need do no rollb
1ae29 61 63 6b 20 61 74 20 61 6c 6c 2e 20 4f 74 68 65  ack at all. Othe
1ae2a 72 77 69 73 65 2c 0a 20 20 20 20 20 20 2a 2a 20  rwise,.      ** 
1ae2b 70 72 6f 63 65 65 64 20 77 69 74 68 20 74 68 65  proceed with the
1ae2c 20 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e   special handlin
1ae2d 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  g..      */.    
1ae2e 20 20 69 66 28 20 21 70 2d 3e 72 65 61 64 4f 6e    if( !p->readOn
1ae2f 6c 79 20 7c 7c 20 6d 72 63 21 3d 53 51 4c 49 54  ly || mrc!=SQLIT
1ae30 45 5f 49 4e 54 45 52 52 55 50 54 20 29 7b 0a 20  E_INTERRUPT ){. 
1ae31 20 20 20 20 20 20 20 69 66 28 20 28 6d 72 63 3d         if( (mrc=
1ae32 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c  =SQLITE_NOMEM ||
1ae33 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c   mrc==SQLITE_FUL
1ae34 4c 29 20 26 26 20 70 2d 3e 75 73 65 73 53 74 6d  L) && p->usesStm
1ae35 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  tJournal ){.    
1ae36 20 20 20 20 20 20 65 53 74 61 74 65 6d 65 6e 74        eStatement
1ae37 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52  Op = SAVEPOINT_R
1ae38 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20 20 20 20  OLLBACK;.       
1ae39 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1ae3a 20 20 2f 2a 20 57 65 20 61 72 65 20 66 6f 72 63    /* We are forc
1ae3b 65 64 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20  ed to roll back 
1ae3c 74 68 65 20 61 63 74 69 76 65 20 74 72 61 6e 73  the active trans
1ae3d 61 63 74 69 6f 6e 2e 20 42 65 66 6f 72 65 20 64  action. Before d
1ae3e 6f 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 2a  oing.          *
1ae3f 2a 20 73 6f 2c 20 61 62 6f 72 74 20 61 6e 79 20  * so, abort any 
1ae40 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73  other statements
1ae41 20 74 68 69 73 20 68 61 6e 64 6c 65 20 63 75 72   this handle cur
1ae42 72 65 6e 74 6c 79 20 68 61 73 20 61 63 74 69 76  rently has activ
1ae43 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
1ae44 20 20 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69            invali
1ae45 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64  dateCursorsOnMod
1ae46 69 66 69 65 64 42 74 72 65 65 73 28 64 62 29 3b  ifiedBtrees(db);
1ae47 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1ae48 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62  e3RollbackAll(db
1ae49 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1ae4a 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69  ite3CloseSavepoi
1ae4b 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20  nts(db);.       
1ae4c 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69     db->autoCommi
1ae4d 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  t = 1;.        }
1ae4e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
1ae4f 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72      /* Check for
1ae50 20 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69   immediate forei
1ae51 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e  gn key violation
1ae52 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d  s. */.    if( p-
1ae53 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  >rc==SQLITE_OK )
1ae54 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1ae55 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 20 30 29  dbeCheckFk(p, 0)
1ae56 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
1ae57 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d 63 6f  * If the auto-co
1ae58 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65 74  mmit flag is set
1ae59 20 61 6e 64 20 74 68 69 73 20 69 73 20 74 68 65   and this is the
1ae5a 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 77 72 69   only active wri
1ae5b 74 65 72 20 0a 20 20 20 20 2a 2a 20 56 4d 2c 20  ter .    ** VM, 
1ae5c 74 68 65 6e 20 77 65 20 64 6f 20 65 69 74 68 65  then we do eithe
1ae5d 72 20 61 20 63 6f 6d 6d 69 74 20 6f 72 20 72 6f  r a commit or ro
1ae5e 6c 6c 62 61 63 6b 20 6f 66 20 74 68 65 20 63 75  llback of the cu
1ae5f 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  rrent transactio
1ae60 6e 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  n. .    **.    *
1ae61 2a 20 4e 6f 74 65 3a 20 54 68 69 73 20 62 6c 6f  * Note: This blo
1ae62 63 6b 20 61 6c 73 6f 20 72 75 6e 73 20 69 66 20  ck also runs if 
1ae63 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63 69  one of the speci
1ae64 61 6c 20 65 72 72 6f 72 73 20 68 61 6e 64 6c 65  al errors handle
1ae65 64 20 0a 20 20 20 20 2a 2a 20 61 62 6f 76 65 20  d .    ** above 
1ae66 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 0a 20  has occurred. . 
1ae67 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 73     */.    if( !s
1ae68 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63  qlite3VtabInSync
1ae69 28 64 62 29 20 0a 20 20 20 20 20 26 26 20 64 62  (db) .     && db
1ae6a 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 0a 20 20  ->autoCommit .  
1ae6b 20 20 20 26 26 20 64 62 2d 3e 77 72 69 74 65 56     && db->writeV
1ae6c 64 62 65 43 6e 74 3d 3d 28 70 2d 3e 72 65 61 64  dbeCnt==(p->read
1ae6d 4f 6e 6c 79 3d 3d 30 29 20 0a 20 20 20 20 29 7b  Only==0) .    ){
1ae6e 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72 63  .      if( p->rc
1ae6f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28  ==SQLITE_OK || (
1ae70 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d  p->errorAction==
1ae71 4f 45 5f 46 61 69 6c 20 26 26 20 21 69 73 53 70  OE_Fail && !isSp
1ae72 65 63 69 61 6c 45 72 72 6f 72 29 20 29 7b 0a 20  ecialError) ){. 
1ae73 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
1ae74 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c  e3VdbeCheckFk(p,
1ae75 20 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20   1) ){.         
1ae76 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
1ae77 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d  exArrayLeave(&p-
1ae78 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  >aMutex);.      
1ae79 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1ae7a 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20  E_ERROR;.       
1ae7b 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68   }.        /* Th
1ae7c 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c  e auto-commit fl
1ae7d 61 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 20  ag is true, the 
1ae7e 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 61 73  vdbe program was
1ae7f 20 73 75 63 63 65 73 73 66 75 6c 20 0a 20 20 20   successful .   
1ae80 20 20 20 20 20 2a 2a 20 6f 72 20 68 69 74 20 61       ** or hit a
1ae81 6e 20 27 4f 52 20 46 41 49 4c 27 20 63 6f 6e 73  n 'OR FAIL' cons
1ae82 74 72 61 69 6e 74 20 61 6e 64 20 74 68 65 72 65  traint and there
1ae83 20 61 72 65 20 6e 6f 20 64 65 66 65 72 72 65 64   are no deferred
1ae84 20 66 6f 72 65 69 67 6e 0a 20 20 20 20 20 20 20   foreign.       
1ae85 20 2a 2a 20 6b 65 79 20 63 6f 6e 73 74 72 61 69   ** key constrai
1ae86 6e 74 73 20 74 6f 20 68 6f 6c 64 20 75 70 20 74  nts to hold up t
1ae87 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  he transaction. 
1ae88 54 68 69 73 20 6d 65 61 6e 73 20 61 20 63 6f 6d  This means a com
1ae89 6d 69 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  mit .        ** 
1ae8a 69 73 20 72 65 71 75 69 72 65 64 2e 20 20 2a 2f  is required.  */
1ae8b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 76 64  .        rc = vd
1ae8c 62 65 43 6f 6d 6d 69 74 28 64 62 2c 20 70 29 3b  beCommit(db, p);
1ae8d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
1ae8e 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a  =SQLITE_BUSY ){.
1ae8f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1ae90 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  3BtreeMutexArray
1ae91 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78  Leave(&p->aMutex
1ae92 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
1ae93 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  urn SQLITE_BUSY;
1ae94 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
1ae95 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1ae96 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d   ){.          p-
1ae97 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20  >rc = rc;.      
1ae98 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62      sqlite3Rollb
1ae99 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  ackAll(db);.    
1ae9a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ae9b 20 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72       db->nDeferr
1ae9c 65 64 43 6f 6e 73 20 3d 20 30 3b 0a 20 20 20 20  edCons = 0;.    
1ae9d 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d        sqlite3Com
1ae9e 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67  mitInternalChang
1ae9f 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  es(db);.        
1aea0 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
1aea1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f         sqlite3Ro
1aea2 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20  llbackAll(db);. 
1aea3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 2d       }.      db-
1aea4 3e 6e 53 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b  >nStatement = 0;
1aea5 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65  .    }else if( e
1aea6 53 74 61 74 65 6d 65 6e 74 4f 70 3d 3d 30 20 29  StatementOp==0 )
1aea7 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 72  {.      if( p->r
1aea8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
1aea9 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 3d 3d  p->errorAction==
1aeaa 4f 45 5f 46 61 69 6c 20 29 7b 0a 20 20 20 20 20  OE_Fail ){.     
1aeab 20 20 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20     eStatementOp 
1aeac 3d 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45  = SAVEPOINT_RELE
1aead 41 53 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ASE;.      }else
1aeae 20 69 66 28 20 70 2d 3e 65 72 72 6f 72 41 63 74   if( p->errorAct
1aeaf 69 6f 6e 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b  ion==OE_Abort ){
1aeb0 0a 20 20 20 20 20 20 20 20 65 53 74 61 74 65 6d  .        eStatem
1aeb1 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49 4e  entOp = SAVEPOIN
1aeb2 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a 20 20 20 20  T_ROLLBACK;.    
1aeb3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1aeb4 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f   invalidateCurso
1aeb5 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65  rsOnModifiedBtre
1aeb6 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  es(db);.        
1aeb7 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41  sqlite3RollbackA
1aeb8 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  ll(db);.        
1aeb9 73 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65  sqlite3CloseSave
1aeba 70 6f 69 6e 74 73 28 64 62 29 3b 0a 20 20 20 20  points(db);.    
1aebb 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
1aebc 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  it = 1;.      }.
1aebd 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
1aebe 49 66 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20  If eStatementOp 
1aebf 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
1aec0 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72  n a statement tr
1aec1 61 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20  ansaction needs 
1aec2 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 63 6f 6d  to.    ** be com
1aec3 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64  mitted or rolled
1aec4 20 62 61 63 6b 2e 20 43 61 6c 6c 20 73 71 6c 69   back. Call sqli
1aec5 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74 61 74  te3VdbeCloseStat
1aec6 65 6d 65 6e 74 28 29 20 74 6f 0a 20 20 20 20 2a  ement() to.    *
1aec7 2a 20 64 6f 20 73 6f 2e 20 49 66 20 74 68 69 73  * do so. If this
1aec8 20 6f 70 65 72 61 74 69 6f 6e 20 72 65 74 75 72   operation retur
1aec9 6e 73 20 61 6e 20 65 72 72 6f 72 2c 20 61 6e 64  ns an error, and
1aeca 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
1aecb 74 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 65 72  tement.    ** er
1aecc 72 6f 72 20 63 6f 64 65 20 69 73 20 53 51 4c 49  ror code is SQLI
1aecd 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 45 5f  TE_OK or SQLITE_
1aece 43 4f 4e 53 54 52 41 49 4e 54 2c 20 74 68 65 6e  CONSTRAINT, then
1aecf 20 73 65 74 20 74 68 65 20 65 72 72 6f 72 0a 20   set the error. 
1aed0 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f 20 74 68     ** code to th
1aed1 65 20 6e 65 77 20 76 61 6c 75 65 2e 0a 20 20 20  e new value..   
1aed2 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 53 74 61   */.    if( eSta
1aed3 74 65 6d 65 6e 74 4f 70 20 29 7b 0a 20 20 20 20  tementOp ){.    
1aed4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1aed5 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74  beCloseStatement
1aed6 28 70 2c 20 65 53 74 61 74 65 6d 65 6e 74 4f 70  (p, eStatementOp
1aed7 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  );.      if( rc 
1aed8 26 26 20 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54  && (p->rc==SQLIT
1aed9 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53  E_OK || p->rc==S
1aeda 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
1aedb 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  ) ){.        p->
1aedc 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 20  rc = rc;.       
1aedd 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1aede 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a  b, p->zErrMsg);.
1aedf 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d          p->zErrM
1aee0 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  sg = 0;.      }.
1aee1 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
1aee2 49 66 20 74 68 69 73 20 77 61 73 20 61 6e 20 49  If this was an I
1aee3 4e 53 45 52 54 2c 20 55 50 44 41 54 45 20 6f 72  NSERT, UPDATE or
1aee4 20 44 45 4c 45 54 45 20 61 6e 64 20 6e 6f 20 73   DELETE and no s
1aee5 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
1aee6 74 69 6f 6e 0a 20 20 20 20 2a 2a 20 68 61 73 20  tion.    ** has 
1aee7 62 65 65 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b  been rolled back
1aee8 2c 20 75 70 64 61 74 65 20 74 68 65 20 64 61 74  , update the dat
1aee9 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1aeea 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2e   change-counter.
1aeeb 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28   .    */.    if(
1aeec 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20   p->changeCntOn 
1aeed 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 53 74  ){.      if( eSt
1aeee 61 74 65 6d 65 6e 74 4f 70 21 3d 53 41 56 45 50  atementOp!=SAVEP
1aeef 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 7b  OINT_ROLLBACK ){
1aef0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1aef1 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28 64  VdbeSetChanges(d
1aef2 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a  b, p->nChange);.
1aef3 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1aef4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1aef5 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c 20 30  SetChanges(db, 0
1aef6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1aef7 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30 3b   p->nChange = 0;
1aef8 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a  .    }.  .    /*
1aef9 20 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 63 6f 6d   Rollback or com
1aefa 6d 69 74 20 61 6e 79 20 73 63 68 65 6d 61 20 63  mit any schema c
1aefb 68 61 6e 67 65 73 20 74 68 61 74 20 6f 63 63 75  hanges that occu
1aefc 72 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28  rred. */.    if(
1aefd 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f   p->rc!=SQLITE_O
1aefe 4b 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26 53  K && db->flags&S
1aeff 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
1af00 67 65 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ges ){.      sql
1af01 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
1af02 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
1af03 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20        db->flags 
1af04 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20 7c 20 53  = (db->flags | S
1af05 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
1af06 67 65 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ges);.    }..   
1af07 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20   /* Release the 
1af08 6c 6f 63 6b 73 20 2a 2f 0a 20 20 20 20 73 71 6c  locks */.    sql
1af09 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
1af0a 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75  rayLeave(&p->aMu
1af0b 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  tex);.  }..  /* 
1af0c 57 65 20 68 61 76 65 20 73 75 63 63 65 73 73 66  We have successf
1af0d 75 6c 6c 79 20 68 61 6c 74 65 64 20 61 6e 64 20  ully halted and 
1af0e 63 6c 6f 73 65 64 20 74 68 65 20 56 4d 2e 20 20  closed the VM.  
1af0f 52 65 63 6f 72 64 20 74 68 69 73 20 66 61 63 74  Record this fact
1af10 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63  . */.  if( p->pc
1af11 3e 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 61  >=0 ){.    db->a
1af12 63 74 69 76 65 56 64 62 65 43 6e 74 2d 2d 3b 0a  ctiveVdbeCnt--;.
1af13 20 20 20 20 69 66 28 20 21 70 2d 3e 72 65 61 64      if( !p->read
1af14 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20 20 64 62  Only ){.      db
1af15 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2d 2d  ->writeVdbeCnt--
1af16 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
1af17 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64  rt( db->activeVd
1af18 62 65 43 6e 74 3e 3d 64 62 2d 3e 77 72 69 74 65  beCnt>=db->write
1af19 56 64 62 65 43 6e 74 20 29 3b 0a 20 20 7d 0a 20  VdbeCnt );.  }. 
1af1a 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45   p->magic = VDBE
1af1b 5f 4d 41 47 49 43 5f 48 41 4c 54 3b 0a 20 20 63  _MAGIC_HALT;.  c
1af1c 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43 6e  heckActiveVdbeCn
1af1d 74 28 64 62 29 3b 0a 20 20 69 66 28 20 70 2d 3e  t(db);.  if( p->
1af1e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1af1f 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
1af20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1af21 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61  }..  /* If the a
1af22 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67 20  uto-commit flag 
1af23 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 2c 20  is set to true, 
1af24 74 68 65 6e 20 61 6e 79 20 6c 6f 63 6b 73 20 74  then any locks t
1af25 68 61 74 20 77 65 72 65 20 68 65 6c 64 0a 20 20  hat were held.  
1af26 2a 2a 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e  ** by connection
1af27 20 64 62 20 68 61 76 65 20 6e 6f 77 20 62 65 65   db have now bee
1af28 6e 20 72 65 6c 65 61 73 65 64 2e 20 43 61 6c 6c  n released. Call
1af29 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69   sqlite3Connecti
1af2a 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 20 0a 20 20  onUnlocked() .  
1af2b 2a 2a 20 74 6f 20 69 6e 76 6f 6b 65 20 61 6e 79  ** to invoke any
1af2c 20 72 65 71 75 69 72 65 64 20 75 6e 6c 6f 63 6b   required unlock
1af2d 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
1af2e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  s..  */.  if( db
1af2f 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a  ->autoCommit ){.
1af30 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65      sqlite3Conne
1af31 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64 62  ctionUnlocked(db
1af32 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  );.  }..  assert
1af33 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  ( db->activeVdbe
1af34 43 6e 74 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 74  Cnt>0 || db->aut
1af35 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62  oCommit==0 || db
1af36 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20  ->nStatement==0 
1af37 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
1af38 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  TE_OK;.}.../*.**
1af39 20 45 61 63 68 20 56 44 42 45 20 68 6f 6c 64 73   Each VDBE holds
1af3a 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
1af3b 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 73  he most recent s
1af3c 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 63 61  qlite3_step() ca
1af3d 6c 6c 0a 2a 2a 20 69 6e 20 70 2d 3e 72 63 2e 20  ll.** in p->rc. 
1af3e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
1af3f 74 73 20 74 68 61 74 20 72 65 73 75 6c 74 20 62  ts that result b
1af40 61 63 6b 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b  ack to SQLITE_OK
1af41 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1af42 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1af43 56 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73  VdbeResetStepRes
1af44 75 6c 74 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ult(Vdbe *p){.  
1af45 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  p->rc = SQLITE_O
1af46 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  K;.}../*.** Clea
1af47 6e 20 75 70 20 61 20 56 44 42 45 20 61 66 74 65  n up a VDBE afte
1af48 72 20 65 78 65 63 75 74 69 6f 6e 20 62 75 74 20  r execution but 
1af49 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68  do not delete th
1af4a 65 20 56 44 42 45 20 6a 75 73 74 20 79 65 74 2e  e VDBE just yet.
1af4b 0a 2a 2a 20 57 72 69 74 65 20 61 6e 79 20 65 72  .** Write any er
1af4c 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e 74  ror messages int
1af4d 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 20 20 52 65  o *pzErrMsg.  Re
1af4e 74 75 72 6e 20 74 68 65 20 72 65 73 75 6c 74 20  turn the result 
1af4f 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65  code..**.** Afte
1af50 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  r this routine i
1af51 73 20 72 75 6e 2c 20 74 68 65 20 56 44 42 45 20  s run, the VDBE 
1af52 73 68 6f 75 6c 64 20 62 65 20 72 65 61 64 79 20  should be ready 
1af53 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 0a 2a  to be executed.*
1af54 2a 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54  * again..**.** T
1af55 6f 20 6c 6f 6f 6b 20 61 74 20 69 74 20 61 6e 6f  o look at it ano
1af56 74 68 65 72 20 77 61 79 2c 20 74 68 69 73 20 72  ther way, this r
1af57 6f 75 74 69 6e 65 20 72 65 73 65 74 73 20 74 68  outine resets th
1af58 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 0a 2a  e state of the.*
1af59 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  * virtual machin
1af5a 65 20 66 72 6f 6d 20 56 44 42 45 5f 4d 41 47 49  e from VDBE_MAGI
1af5b 43 5f 52 55 4e 20 6f 72 20 56 44 42 45 5f 4d 41  C_RUN or VDBE_MA
1af5c 47 49 43 5f 48 41 4c 54 20 62 61 63 6b 20 74 6f  GIC_HALT back to
1af5d 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f 49  .** VDBE_MAGIC_I
1af5e 4e 49 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  NIT..*/.SQLITE_P
1af5f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1af60 65 33 56 64 62 65 52 65 73 65 74 28 56 64 62 65  e3VdbeReset(Vdbe
1af61 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20   *p){.  sqlite3 
1af62 2a 64 62 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64  *db;.  db = p->d
1af63 62 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  b;..  /* If the 
1af64 56 4d 20 64 69 64 20 6e 6f 74 20 72 75 6e 20 74  VM did not run t
1af65 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 72 20  o completion or 
1af66 69 66 20 69 74 20 65 6e 63 6f 75 6e 74 65 72 65  if it encountere
1af67 64 20 61 6e 0a 20 20 2a 2a 20 65 72 72 6f 72 2c  d an.  ** error,
1af68 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e   then it might n
1af69 6f 74 20 68 61 76 65 20 62 65 65 6e 20 68 61 6c  ot have been hal
1af6a 74 65 64 20 70 72 6f 70 65 72 6c 79 2e 20 20 53  ted properly.  S
1af6b 6f 20 68 61 6c 74 0a 20 20 2a 2a 20 69 74 20 6e  o halt.  ** it n
1af6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69 64  ow..  */.  (void
1af6d 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
1af6e 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  (db);.  sqlite3V
1af6f 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20 28 76  dbeHalt(p);.  (v
1af70 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
1af71 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20  yOff(db);..  /* 
1af72 49 66 20 74 68 65 20 56 44 42 45 20 68 61 73 20  If the VDBE has 
1af73 62 65 20 72 75 6e 20 65 76 65 6e 20 70 61 72 74  be run even part
1af74 69 61 6c 6c 79 2c 20 74 68 65 6e 20 74 72 61 6e  ially, then tran
1af75 73 66 65 72 20 74 68 65 20 65 72 72 6f 72 20 63  sfer the error c
1af76 6f 64 65 0a 20 20 2a 2a 20 61 6e 64 20 65 72 72  ode.  ** and err
1af77 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20  or message from 
1af78 74 68 65 20 56 44 42 45 20 69 6e 74 6f 20 74 68  the VDBE into th
1af79 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1af7a 73 74 72 75 63 74 75 72 65 2e 20 20 42 75 74 0a  structure.  But.
1af7b 20 20 2a 2a 20 69 66 20 74 68 65 20 56 44 42 45    ** if the VDBE
1af7c 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 73   has just been s
1af7d 65 74 20 74 6f 20 72 75 6e 20 62 75 74 20 68 61  et to run but ha
1af7e 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 65  s not actually e
1af7f 78 65 63 75 74 65 64 20 61 6e 79 0a 20 20 2a 2a  xecuted any.  **
1af80 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 79 65   instructions ye
1af81 74 2c 20 6c 65 61 76 65 20 74 68 65 20 6d 61 69  t, leave the mai
1af82 6e 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72  n database error
1af83 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 6e 63   information unc
1af84 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  hanged..  */.  i
1af85 66 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  f( p->pc>=0 ){. 
1af86 20 20 20 69 66 28 20 70 2d 3e 7a 45 72 72 4d 73     if( p->zErrMs
1af87 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  g ){.      sqlit
1af88 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
1af89 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 73 71 6c  loc();.      sql
1af8a 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
1af8b 64 62 2d 3e 70 45 72 72 2c 2d 31 2c 70 2d 3e 7a  db->pErr,-1,p->z
1af8c 45 72 72 4d 73 67 2c 53 51 4c 49 54 45 5f 55 54  ErrMsg,SQLITE_UT
1af8d 46 38 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  F8,SQLITE_TRANSI
1af8e 45 4e 54 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ENT);.      sqli
1af8f 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
1af90 6f 63 28 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e  oc();.      db->
1af91 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 3b  errCode = p->rc;
1af92 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
1af93 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72  Free(db, p->zErr
1af94 4d 73 67 29 3b 0a 20 20 20 20 20 20 70 2d 3e 7a  Msg);.      p->z
1af95 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20  ErrMsg = 0;.    
1af96 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20  }else if( p->rc 
1af97 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1af98 45 72 72 6f 72 28 64 62 2c 20 70 2d 3e 72 63 2c  Error(db, p->rc,
1af99 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   0);.    }else{.
1af9a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1af9b 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  or(db, SQLITE_OK
1af9c 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  , 0);.    }.  }e
1af9d 6c 73 65 20 69 66 28 20 70 2d 3e 72 63 20 26 26  lse if( p->rc &&
1af9e 20 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20   p->expired ){. 
1af9f 20 20 20 2f 2a 20 54 68 65 20 65 78 70 69 72 65     /* The expire
1afa0 64 20 66 6c 61 67 20 77 61 73 20 73 65 74 20 6f  d flag was set o
1afa1 6e 20 74 68 65 20 56 44 42 45 20 62 65 66 6f 72  n the VDBE befor
1afa2 65 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c  e the first call
1afa3 0a 20 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74  .    ** to sqlit
1afa4 65 33 5f 73 74 65 70 28 29 2e 20 46 6f 72 20 63  e3_step(). For c
1afa5 6f 6e 73 69 73 74 65 6e 63 79 20 28 73 69 6e 63  onsistency (sinc
1afa6 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  e sqlite3_step()
1afa7 20 77 61 73 0a 20 20 20 20 2a 2a 20 63 61 6c 6c   was.    ** call
1afa8 65 64 29 2c 20 73 65 74 20 74 68 65 20 64 61 74  ed), set the dat
1afa9 61 62 61 73 65 20 65 72 72 6f 72 20 69 6e 20 74  abase error in t
1afaa 68 69 73 20 63 61 73 65 20 61 73 20 77 65 6c 6c  his case as well
1afab 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
1afac 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70 2d  ite3Error(db, p-
1afad 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  >rc, 0);.    sql
1afae 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
1afaf 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 70 2d  db->pErr, -1, p-
1afb0 3e 7a 45 72 72 4d 73 67 2c 20 53 51 4c 49 54 45  >zErrMsg, SQLITE
1afb1 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54 52  _UTF8, SQLITE_TR
1afb2 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 71  ANSIENT);.    sq
1afb3 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1afb4 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  p->zErrMsg);.   
1afb5 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b   p->zErrMsg = 0;
1afb6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 63 6c 61  .  }..  /* Recla
1afb7 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 75 73  im all memory us
1afb8 65 64 20 62 79 20 74 68 65 20 56 44 42 45 0a 20  ed by the VDBE. 
1afb9 20 2a 2f 0a 20 20 43 6c 65 61 6e 75 70 28 70 29   */.  Cleanup(p)
1afba 3b 0a 0a 20 20 2f 2a 20 53 61 76 65 20 70 72 6f  ;..  /* Save pro
1afbb 66 69 6c 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  filing informati
1afbc 6f 6e 20 66 72 6f 6d 20 74 68 69 73 20 56 44 42  on from this VDB
1afbd 45 20 72 75 6e 2e 0a 20 20 2a 2f 0a 23 69 66 64  E run..  */.#ifd
1afbe 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a  ef VDBE_PROFILE.
1afbf 20 20 7b 0a 20 20 20 20 46 49 4c 45 20 2a 6f 75    {.    FILE *ou
1afc0 74 20 3d 20 66 6f 70 65 6e 28 22 76 64 62 65 5f  t = fopen("vdbe_
1afc1 70 72 6f 66 69 6c 65 2e 6f 75 74 22 2c 20 22 61  profile.out", "a
1afc2 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 20  ");.    if( out 
1afc3 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  ){.      int i;.
1afc4 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
1afc5 74 2c 20 22 2d 2d 2d 2d 20 22 29 3b 0a 20 20 20  t, "---- ");.   
1afc6 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d     for(i=0; i<p-
1afc7 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nOp; i++){.    
1afc8 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
1afc9 20 22 25 30 32 78 22 2c 20 70 2d 3e 61 4f 70 5b   "%02x", p->aOp[
1afca 69 5d 2e 6f 70 63 6f 64 65 29 3b 0a 20 20 20 20  i].opcode);.    
1afcb 20 20 7d 0a 20 20 20 20 20 20 66 70 72 69 6e 74    }.      fprint
1afcc 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20  f(out, "\n");.  
1afcd 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1afce 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nOp; i++){.   
1afcf 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74       fprintf(out
1afd0 2c 20 22 25 36 64 20 25 31 30 6c 6c 64 20 25 38  , "%6d %10lld %8
1afd1 6c 6c 64 20 22 2c 0a 20 20 20 20 20 20 20 20 20  lld ",.         
1afd2 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 2c    p->aOp[i].cnt,
1afd3 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61  .           p->a
1afd4 4f 70 5b 69 5d 2e 63 79 63 6c 65 73 2c 0a 20 20  Op[i].cycles,.  
1afd5 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b           p->aOp[
1afd6 69 5d 2e 63 6e 74 3e 30 20 3f 20 70 2d 3e 61 4f  i].cnt>0 ? p->aO
1afd7 70 5b 69 5d 2e 63 79 63 6c 65 73 2f 70 2d 3e 61  p[i].cycles/p->a
1afd8 4f 70 5b 69 5d 2e 63 6e 74 20 3a 20 30 0a 20 20  Op[i].cnt : 0.  
1afd9 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
1afda 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
1afdb 74 4f 70 28 6f 75 74 2c 20 69 2c 20 26 70 2d 3e  tOp(out, i, &p->
1afdc 61 4f 70 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d  aOp[i]);.      }
1afdd 0a 20 20 20 20 20 20 66 63 6c 6f 73 65 28 6f 75  .      fclose(ou
1afde 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  t);.    }.  }.#e
1afdf 6e 64 69 66 0a 20 20 70 2d 3e 6d 61 67 69 63 20  ndif.  p->magic 
1afe0 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  = VDBE_MAGIC_INI
1afe1 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 72  T;.  return p->r
1afe2 63 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b  c & db->errMask;
1afe3 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e  .}. ./*.** Clean
1afe4 20 75 70 20 61 6e 64 20 64 65 6c 65 74 65 20 61   up and delete a
1afe5 20 56 44 42 45 20 61 66 74 65 72 20 65 78 65 63   VDBE after exec
1afe6 75 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 61  ution.  Return a
1afe7 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
1afe8 69 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  is.** the result
1afe9 20 63 6f 64 65 2e 20 20 57 72 69 74 65 20 61 6e   code.  Write an
1afea 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  y error message 
1afeb 74 65 78 74 20 69 6e 74 6f 20 2a 70 7a 45 72 72  text into *pzErr
1afec 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Msg..*/.SQLITE_P
1afed 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1afee 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 56  e3VdbeFinalize(V
1afef 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  dbe *p){.  int r
1aff0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1aff1 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56   if( p->magic==V
1aff2 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c 7c  DBE_MAGIC_RUN ||
1aff3 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f   p->magic==VDBE_
1aff4 4d 41 47 49 43 5f 48 41 4c 54 20 29 7b 0a 20 20  MAGIC_HALT ){.  
1aff5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1aff6 62 65 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20  beReset(p);.    
1aff7 61 73 73 65 72 74 28 20 28 72 63 20 26 20 70 2d  assert( (rc & p-
1aff8 3e 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72  >db->errMask)==r
1aff9 63 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  c );.  }.  sqlit
1affa 65 33 56 64 62 65 44 65 6c 65 74 65 28 70 29 3b  e3VdbeDelete(p);
1affb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1affc 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 65 20  ./*.** Call the 
1affd 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 65  destructor for e
1affe 61 63 68 20 61 75 78 64 61 74 61 20 65 6e 74 72  ach auxdata entr
1afff 79 20 69 6e 20 70 56 64 62 65 46 75 6e 63 20 66  y in pVdbeFunc f
1b000 6f 72 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20  or which.** the 
1b001 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 62 69  corresponding bi
1b002 74 20 69 6e 20 6d 61 73 6b 20 69 73 20 63 6c 65  t in mask is cle
1b003 61 72 2e 20 20 41 75 78 64 61 74 61 20 65 6e 74  ar.  Auxdata ent
1b004 72 69 65 73 20 62 65 79 6f 6e 64 20 33 31 0a 2a  ries beyond 31.*
1b005 2a 20 61 72 65 20 61 6c 77 61 79 73 20 64 65 73  * are always des
1b006 74 72 6f 79 65 64 2e 20 20 54 6f 20 64 65 73 74  troyed.  To dest
1b007 72 6f 79 20 61 6c 6c 20 61 75 78 64 61 74 61 20  roy all auxdata 
1b008 65 6e 74 72 69 65 73 2c 20 63 61 6c 6c 20 74 68  entries, call th
1b009 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 69  is.** routine wi
1b00a 74 68 20 6d 61 73 6b 3d 3d 30 2e 0a 2a 2f 0a 53  th mask==0..*/.S
1b00b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1b00c 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65  id sqlite3VdbeDe
1b00d 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 62 65  leteAuxData(Vdbe
1b00e 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 2c  Func *pVdbeFunc,
1b00f 20 69 6e 74 20 6d 61 73 6b 29 7b 0a 20 20 69 6e   int mask){.  in
1b010 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  t i;.  for(i=0; 
1b011 69 3c 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75  i<pVdbeFunc->nAu
1b012 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72  x; i++){.    str
1b013 75 63 74 20 41 75 78 44 61 74 61 20 2a 70 41 75  uct AuxData *pAu
1b014 78 20 3d 20 26 70 56 64 62 65 46 75 6e 63 2d 3e  x = &pVdbeFunc->
1b015 61 70 41 75 78 5b 69 5d 3b 0a 20 20 20 20 69 66  apAux[i];.    if
1b016 28 20 28 69 3e 33 31 20 7c 7c 20 21 28 6d 61 73  ( (i>31 || !(mas
1b017 6b 26 28 28 28 75 33 32 29 31 29 3c 3c 69 29 29  k&(((u32)1)<<i))
1b018 29 20 26 26 20 70 41 75 78 2d 3e 70 41 75 78 20  ) && pAux->pAux 
1b019 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 41 75  ){.      if( pAu
1b01a 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20 20  x->xDelete ){.  
1b01b 20 20 20 20 20 20 70 41 75 78 2d 3e 78 44 65 6c        pAux->xDel
1b01c 65 74 65 28 70 41 75 78 2d 3e 70 41 75 78 29 3b  ete(pAux->pAux);
1b01d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1b01e 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b 0a 20  Aux->pAux = 0;. 
1b01f 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
1b020 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69  * Delete an enti
1b021 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49  re VDBE..*/.SQLI
1b022 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1b023 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
1b024 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71  e(Vdbe *p){.  sq
1b025 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66  lite3 *db;..  if
1b026 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20 29 20  ( NEVER(p==0) ) 
1b027 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70  return;.  db = p
1b028 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 2d 3e 70  ->db;.  if( p->p
1b029 50 72 65 76 20 29 7b 0a 20 20 20 20 70 2d 3e 70  Prev ){.    p->p
1b02a 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d  Prev->pNext = p-
1b02b 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b  >pNext;.  }else{
1b02c 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d  .    assert( db-
1b02d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a 20 20 20  >pVdbe==p );.   
1b02e 20 64 62 2d 3e 70 56 64 62 65 20 3d 20 70 2d 3e   db->pVdbe = p->
1b02f 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28  pNext;.  }.  if(
1b030 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20   p->pNext ){.   
1b031 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76   p->pNext->pPrev
1b032 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20 7d   = p->pPrev;.  }
1b033 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72  .  releaseMemArr
1b034 61 79 28 70 2d 3e 61 56 61 72 2c 20 70 2d 3e 6e  ay(p->aVar, p->n
1b035 56 61 72 29 3b 0a 20 20 72 65 6c 65 61 73 65 4d  Var);.  releaseM
1b036 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e  emArray(p->aColN
1b037 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75  ame, p->nResColu
1b038 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20  mn*COLNAME_N);. 
1b039 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79   vdbeFreeOpArray
1b03a 28 64 62 2c 20 70 2d 3e 61 4f 70 2c 20 70 2d 3e  (db, p->aOp, p->
1b03b 6e 4f 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  nOp);.  sqlite3D
1b03c 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 4c 61  bFree(db, p->aLa
1b03d 62 65 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  bel);.  sqlite3D
1b03e 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61 43 6f  bFree(db, p->aCo
1b03f 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65  lName);.  sqlite
1b040 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a  3DbFree(db, p->z
1b041 53 71 6c 29 3b 0a 20 20 70 2d 3e 6d 61 67 69 63  Sql);.  p->magic
1b042 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45   = VDBE_MAGIC_DE
1b043 41 44 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  AD;.  sqlite3DbF
1b044 72 65 65 28 64 62 2c 20 70 2d 3e 70 46 72 65 65  ree(db, p->pFree
1b045 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
1b046 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a  ee(db, p);.}../*
1b047 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
1b048 65 20 63 75 72 73 6f 72 20 70 20 69 73 20 72 65  e cursor p is re
1b049 61 64 79 20 74 6f 20 72 65 61 64 20 6f 72 20 77  ady to read or w
1b04a 72 69 74 65 20 74 68 65 20 72 6f 77 20 74 6f 20  rite the row to 
1b04b 77 68 69 63 68 20 69 74 0a 2a 2a 20 77 61 73 20  which it.** was 
1b04c 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 65 64 2e  last positioned.
1b04d 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f    Return an erro
1b04e 72 20 63 6f 64 65 20 69 66 20 61 6e 20 4f 4f 4d  r code if an OOM
1b04f 20 66 61 75 6c 74 20 6f 72 20 49 2f 4f 20 65 72   fault or I/O er
1b050 72 6f 72 0a 2a 2a 20 70 72 65 76 65 6e 74 73 20  ror.** prevents 
1b051 75 73 20 66 72 6f 6d 20 70 6f 73 69 74 69 6f 6e  us from position
1b052 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 20 74  ing the cursor t
1b053 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 70 6f  o its correct po
1b054 73 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  sition..**.** If
1b055 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 61 74   a MoveTo operat
1b056 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 20 6f  ion is pending o
1b057 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 72 73  n the given curs
1b058 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 61 74  or, then do that
1b059 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e 20  .** MoveTo now. 
1b05a 20 49 66 20 6e 6f 20 6d 6f 76 65 20 69 73 20 70   If no move is p
1b05b 65 6e 64 69 6e 67 2c 20 63 68 65 63 6b 20 74 6f  ending, check to
1b05c 20 73 65 65 20 69 66 20 74 68 65 20 72 6f 77 20   see if the row 
1b05d 68 61 73 20 62 65 65 6e 0a 2a 2a 20 64 65 6c 65  has been.** dele
1b05e 74 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64  ted out from und
1b05f 65 72 20 74 68 65 20 63 75 72 73 6f 72 20 61 6e  er the cursor an
1b060 64 20 69 66 20 69 74 20 68 61 73 2c 20 6d 61 72  d if it has, mar
1b061 6b 20 74 68 65 20 72 6f 77 20 61 73 0a 2a 2a 20  k the row as.** 
1b062 61 20 4e 55 4c 4c 20 72 6f 77 2e 0a 2a 2a 0a 2a  a NULL row..**.*
1b063 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20  * If the cursor 
1b064 69 73 20 61 6c 72 65 61 64 79 20 70 6f 69 6e 74  is already point
1b065 69 6e 67 20 74 6f 20 74 68 65 20 63 6f 72 72 65  ing to the corre
1b066 63 74 20 72 6f 77 20 61 6e 64 20 74 68 61 74 20  ct row and that 
1b067 72 6f 77 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 62  row has.** not b
1b068 65 65 6e 20 64 65 6c 65 74 65 64 20 6f 75 74 20  een deleted out 
1b069 66 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 20 63  from under the c
1b06a 75 72 73 6f 72 2c 20 74 68 65 6e 20 74 68 69 73  ursor, then this
1b06b 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
1b06c 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  -op..*/.SQLITE_P
1b06d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1b06e 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
1b06f 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20 2a 70  to(VdbeCursor *p
1b070 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65 66 65  ){.  if( p->defe
1b071 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20 20  rredMoveto ){.  
1b072 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b 0a 23    int res, rc;.#
1b073 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1b074 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74  T.    extern int
1b075 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f   sqlite3_search_
1b076 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 20  count;.#endif.  
1b077 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73 54    assert( p->isT
1b078 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63 20 3d  able );.    rc =
1b079 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
1b07a 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d 3e 70  etoUnpacked(p->p
1b07b 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e 6d 6f  Cursor, 0, p->mo
1b07c 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c 20 26  vetoTarget, 0, &
1b07d 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63  res);.    if( rc
1b07e 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
1b07f 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d    p->lastRowid =
1b080 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65 74   p->movetoTarget
1b081 3b 0a 20 20 20 20 70 2d 3e 72 6f 77 69 64 49 73  ;.    p->rowidIs
1b082 56 61 6c 69 64 20 3d 20 41 4c 57 41 59 53 28 72  Valid = ALWAYS(r
1b083 65 73 3d 3d 30 29 20 3f 31 3a 30 3b 0a 20 20 20  es==0) ?1:0;.   
1b084 20 69 66 28 20 4e 45 56 45 52 28 72 65 73 3c 30   if( NEVER(res<0
1b085 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) ){.      rc = 
1b086 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74  sqlite3BtreeNext
1b087 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 72 65  (p->pCursor, &re
1b088 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  s);.      if( rc
1b089 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
1b08a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54    }.#ifdef SQLIT
1b08b 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74  E_TEST.    sqlit
1b08c 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b  e3_search_count+
1b08d 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 2d  +;.#endif.    p-
1b08e 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1b08f 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 63 61 63 68  = 0;.    p->cach
1b090 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1b091 53 54 41 4c 45 3b 0a 20 20 7d 65 6c 73 65 20 69  STALE;.  }else i
1b092 66 28 20 41 4c 57 41 59 53 28 70 2d 3e 70 43 75  f( ALWAYS(p->pCu
1b093 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 69 6e 74  rsor) ){.    int
1b094 20 68 61 73 4d 6f 76 65 64 3b 0a 20 20 20 20 69   hasMoved;.    i
1b095 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  nt rc = sqlite3B
1b096 74 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76  treeCursorHasMov
1b097 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26  ed(p->pCursor, &
1b098 68 61 73 4d 6f 76 65 64 29 3b 0a 20 20 20 20 69  hasMoved);.    i
1b099 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
1b09a 63 3b 0a 20 20 20 20 69 66 28 20 68 61 73 4d 6f  c;.    if( hasMo
1b09b 76 65 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ved ){.      p->
1b09c 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1b09d 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 20  CHE_STALE;.     
1b09e 20 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b   p->nullRow = 1;
1b09f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1b0a0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1b0a1 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
1b0a2 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 3a  owing functions:
1b0a3 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  .**.** sqlite3Vd
1b0a4 62 65 53 65 72 69 61 6c 54 79 70 65 28 29 0a 2a  beSerialType().*
1b0a5 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  * sqlite3VdbeSer
1b0a6 69 61 6c 54 79 70 65 4c 65 6e 28 29 0a 2a 2a 20  ialTypeLen().** 
1b0a7 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1b0a8 6c 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65  lLen().** sqlite
1b0a9 33 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 29  3VdbeSerialPut()
1b0aa 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53  .** sqlite3VdbeS
1b0ab 65 72 69 61 6c 47 65 74 28 29 0a 2a 2a 0a 2a 2a  erialGet().**.**
1b0ac 20 65 6e 63 61 70 73 75 6c 61 74 65 20 74 68 65   encapsulate the
1b0ad 20 63 6f 64 65 20 74 68 61 74 20 73 65 72 69 61   code that seria
1b0ae 6c 69 7a 65 73 20 76 61 6c 75 65 73 20 66 6f 72  lizes values for
1b0af 20 73 74 6f 72 61 67 65 20 69 6e 20 53 51 4c 69   storage in SQLi
1b0b0 74 65 0a 2a 2a 20 64 61 74 61 20 61 6e 64 20 69  te.** data and i
1b0b1 6e 64 65 78 20 72 65 63 6f 72 64 73 2e 20 45 61  ndex records. Ea
1b0b2 63 68 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61  ch serialized va
1b0b3 6c 75 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  lue consists of 
1b0b4 61 0a 2a 2a 20 27 73 65 72 69 61 6c 2d 74 79 70  a.** 'serial-typ
1b0b5 65 27 20 61 6e 64 20 61 20 62 6c 6f 62 20 6f 66  e' and a blob of
1b0b6 20 64 61 74 61 2e 20 54 68 65 20 73 65 72 69 61   data. The seria
1b0b7 6c 20 74 79 70 65 20 69 73 20 61 6e 20 38 2d 62  l type is an 8-b
1b0b8 79 74 65 20 75 6e 73 69 67 6e 65 64 0a 2a 2a 20  yte unsigned.** 
1b0b9 69 6e 74 65 67 65 72 2c 20 73 74 6f 72 65 64 20  integer, stored 
1b0ba 61 73 20 61 20 76 61 72 69 6e 74 2e 0a 2a 2a 0a  as a varint..**.
1b0bb 2a 2a 20 49 6e 20 61 6e 20 53 51 4c 69 74 65 20  ** In an SQLite 
1b0bc 69 6e 64 65 78 20 72 65 63 6f 72 64 2c 20 74 68  index record, th
1b0bd 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 69 73  e serial type is
1b0be 20 73 74 6f 72 65 64 20 64 69 72 65 63 74 6c 79   stored directly
1b0bf 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 62   before.** the b
1b0c0 6c 6f 62 20 6f 66 20 64 61 74 61 20 74 68 61 74  lob of data that
1b0c1 20 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20   it corresponds 
1b0c2 74 6f 2e 20 49 6e 20 61 20 74 61 62 6c 65 20 72  to. In a table r
1b0c3 65 63 6f 72 64 2c 20 61 6c 6c 20 73 65 72 69 61  ecord, all seria
1b0c4 6c 0a 2a 2a 20 74 79 70 65 73 20 61 72 65 20 73  l.** types are s
1b0c5 74 6f 72 65 64 20 61 74 20 74 68 65 20 73 74 61  tored at the sta
1b0c6 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  rt of the record
1b0c7 2c 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 73 20  , and the blobs 
1b0c8 6f 66 20 64 61 74 61 20 61 74 0a 2a 2a 20 74 68  of data at.** th
1b0c9 65 20 65 6e 64 2e 20 48 65 6e 63 65 20 74 68 65  e end. Hence the
1b0ca 73 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6c 6c  se functions all
1b0cb 6f 77 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  ow the caller to
1b0cc 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 73   handle the.** s
1b0cd 65 72 69 61 6c 2d 74 79 70 65 20 61 6e 64 20 64  erial-type and d
1b0ce 61 74 61 20 62 6c 6f 62 20 73 65 70 65 72 61 74  ata blob seperat
1b0cf 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ely..**.** The f
1b0d0 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64  ollowing table d
1b0d1 65 73 63 72 69 62 65 73 20 74 68 65 20 76 61 72  escribes the var
1b0d2 69 6f 75 73 20 73 74 6f 72 61 67 65 20 63 6c 61  ious storage cla
1b0d3 73 73 65 73 20 66 6f 72 20 64 61 74 61 3a 0a 2a  sses for data:.*
1b0d4 2a 0a 2a 2a 20 20 20 73 65 72 69 61 6c 20 74 79  *.**   serial ty
1b0d5 70 65 20 20 20 20 20 20 20 20 62 79 74 65 73 20  pe        bytes 
1b0d6 6f 66 20 64 61 74 61 20 20 20 20 20 20 74 79 70  of data      typ
1b0d7 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  e.**   ---------
1b0d8 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d  -----     ------
1b0d9 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d  ---------    ---
1b0da 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
1b0db 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20       0          
1b0dc 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20             0    
1b0dd 20 20 20 20 20 20 20 20 4e 55 4c 4c 0a 2a 2a 20          NULL.** 
1b0de 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20       1          
1b0df 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20 20             1    
1b0e0 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69          signed i
1b0e1 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 32  nteger.**      2
1b0e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b0e3 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20       2          
1b0e4 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72    signed integer
1b0e5 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 20 20 20  .**      3      
1b0e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
1b0e7 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e              sign
1b0e8 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  ed integer.**   
1b0e9 20 20 20 34 20 20 20 20 20 20 20 20 20 20 20 20     4            
1b0ea 20 20 20 20 20 20 20 20 20 34 20 20 20 20 20 20           4      
1b0eb 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74        signed int
1b0ec 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 35 20 20  eger.**      5  
1b0ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b0ee 20 20 20 36 20 20 20 20 20 20 20 20 20 20 20 20     6            
1b0ef 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a  signed integer.*
1b0f0 2a 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20  *      6        
1b0f1 20 20 20 20 20 20 20 20 20 20 20 20 20 38 20 20               8  
1b0f2 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64            signed
1b0f3 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20   integer.**     
1b0f4 20 37 20 20 20 20 20 20 20 20 20 20 20 20 20 20   7              
1b0f5 20 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20         8        
1b0f6 20 20 20 20 49 45 45 45 20 66 6c 6f 61 74 0a 2a      IEEE float.*
1b0f7 2a 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20  *      8        
1b0f8 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20               0  
1b0f9 20 20 20 20 20 20 20 20 20 20 49 6e 74 65 67 65            Intege
1b0fa 72 20 63 6f 6e 73 74 61 6e 74 20 30 0a 2a 2a 20  r constant 0.** 
1b0fb 20 20 20 20 20 39 20 20 20 20 20 20 20 20 20 20       9          
1b0fc 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20             0    
1b0fd 20 20 20 20 20 20 20 20 49 6e 74 65 67 65 72 20          Integer 
1b0fe 63 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a 20 20 20  constant 1.**   
1b0ff 20 20 31 30 2c 31 31 20 20 20 20 20 20 20 20 20    10,11         
1b100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b101 20 20 20 20 20 20 72 65 73 65 72 76 65 64 20 66        reserved f
1b102 6f 72 20 65 78 70 61 6e 73 69 6f 6e 0a 2a 2a 20  or expansion.** 
1b103 20 20 20 4e 3e 3d 31 32 20 61 6e 64 20 65 76 65     N>=12 and eve
1b104 6e 20 20 20 20 20 20 20 28 4e 2d 31 32 29 2f 32  n       (N-12)/2
1b105 20 20 20 20 20 20 20 20 42 4c 4f 42 0a 2a 2a 20          BLOB.** 
1b106 20 20 20 4e 3e 3d 31 33 20 61 6e 64 20 6f 64 64     N>=13 and odd
1b107 20 20 20 20 20 20 20 20 28 4e 2d 31 33 29 2f 32          (N-13)/2
1b108 20 20 20 20 20 20 20 20 74 65 78 74 0a 2a 2a 0a          text.**.
1b109 2a 2a 20 54 68 65 20 38 20 61 6e 64 20 39 20 74  ** The 8 and 9 t
1b10a 79 70 65 73 20 77 65 72 65 20 61 64 64 65 64 20  ypes were added 
1b10b 69 6e 20 33 2e 33 2e 30 2c 20 66 69 6c 65 20 66  in 3.3.0, file f
1b10c 6f 72 6d 61 74 20 34 2e 20 20 50 72 69 6f 72 20  ormat 4.  Prior 
1b10d 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53  versions.** of S
1b10e 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 75  QLite will not u
1b10f 6e 64 65 72 73 74 61 6e 64 20 74 68 6f 73 65 20  nderstand those 
1b110 73 65 72 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2f  serial types..*/
1b111 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1b112 68 65 20 73 65 72 69 61 6c 2d 74 79 70 65 20 66  he serial-type f
1b113 6f 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  or the value sto
1b114 72 65 64 20 69 6e 20 70 4d 65 6d 2e 0a 2a 2f 0a  red in pMem..*/.
1b115 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75  SQLITE_PRIVATE u
1b116 33 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  32 sqlite3VdbeSe
1b117 72 69 61 6c 54 79 70 65 28 4d 65 6d 20 2a 70 4d  rialType(Mem *pM
1b118 65 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72  em, int file_for
1b119 6d 61 74 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67  mat){.  int flag
1b11a 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b  s = pMem->flags;
1b11b 0a 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28  .  int n;..  if(
1b11c 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20   flags&MEM_Null 
1b11d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
1b11e 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73  .  }.  if( flags
1b11f 26 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20  &MEM_Int ){.    
1b120 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 68  /* Figure out wh
1b121 65 74 68 65 72 20 74 6f 20 75 73 65 20 31 2c 20  ether to use 1, 
1b122 32 2c 20 34 2c 20 36 20 6f 72 20 38 20 62 79 74  2, 4, 6 or 8 byt
1b123 65 73 2e 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e  es. */.#   defin
1b124 65 20 4d 41 58 5f 36 42 59 54 45 20 28 28 28 28  e MAX_6BYTE ((((
1b125 69 36 34 29 30 78 30 30 30 30 38 30 30 30 29 3c  i64)0x00008000)<
1b126 3c 33 32 29 2d 31 29 0a 20 20 20 20 69 36 34 20  <32)-1).    i64 
1b127 69 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20  i = pMem->u.i;. 
1b128 20 20 20 75 36 34 20 75 3b 0a 20 20 20 20 69 66     u64 u;.    if
1b129 28 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34  ( file_format>=4
1b12a 20 26 26 20 28 69 26 31 29 3d 3d 69 20 29 7b 0a   && (i&1)==i ){.
1b12b 20 20 20 20 20 20 72 65 74 75 72 6e 20 38 2b 28        return 8+(
1b12c 75 33 32 29 69 3b 0a 20 20 20 20 7d 0a 20 20 20  u32)i;.    }.   
1b12d 20 75 20 3d 20 69 3c 30 20 3f 20 2d 69 20 3a 20   u = i<0 ? -i : 
1b12e 69 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 31 32  i;.    if( u<=12
1b12f 37 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  7 ) return 1;.  
1b130 20 20 69 66 28 20 75 3c 3d 33 32 37 36 37 20 29    if( u<=32767 )
1b131 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 69   return 2;.    i
1b132 66 28 20 75 3c 3d 38 33 38 38 36 30 37 20 29 20  f( u<=8388607 ) 
1b133 72 65 74 75 72 6e 20 33 3b 0a 20 20 20 20 69 66  return 3;.    if
1b134 28 20 75 3c 3d 32 31 34 37 34 38 33 36 34 37 20  ( u<=2147483647 
1b135 29 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20  ) return 4;.    
1b136 69 66 28 20 75 3c 3d 4d 41 58 5f 36 42 59 54 45  if( u<=MAX_6BYTE
1b137 20 29 20 72 65 74 75 72 6e 20 35 3b 0a 20 20 20   ) return 5;.   
1b138 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 7d 0a 20   return 6;.  }. 
1b139 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 52   if( flags&MEM_R
1b13a 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  eal ){.    retur
1b13b 6e 20 37 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  n 7;.  }.  asser
1b13c 74 28 20 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 61 6c  t( pMem->db->mal
1b13d 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 66 6c 61  locFailed || fla
1b13e 67 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  gs&(MEM_Str|MEM_
1b13f 42 6c 6f 62 29 20 29 3b 0a 20 20 6e 20 3d 20 70  Blob) );.  n = p
1b140 4d 65 6d 2d 3e 6e 3b 0a 20 20 69 66 28 20 66 6c  Mem->n;.  if( fl
1b141 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ags & MEM_Zero )
1b142 7b 0a 20 20 20 20 6e 20 2b 3d 20 70 4d 65 6d 2d  {.    n += pMem-
1b143 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20  >u.nZero;.  }.  
1b144 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a  assert( n>=0 );.
1b145 20 20 72 65 74 75 72 6e 20 28 28 6e 2a 32 29 20    return ((n*2) 
1b146 2b 20 31 32 20 2b 20 28 28 66 6c 61 67 73 26 4d  + 12 + ((flags&M
1b147 45 4d 5f 53 74 72 29 21 3d 30 29 29 3b 0a 7d 0a  EM_Str)!=0));.}.
1b148 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1b149 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
1b14a 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  data correspondi
1b14b 6e 67 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69  ng to the suppli
1b14c 65 64 20 73 65 72 69 61 6c 2d 74 79 70 65 2e 0a  ed serial-type..
1b14d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b14e 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62  E u32 sqlite3Vdb
1b14f 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75  eSerialTypeLen(u
1b150 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65 29 7b  32 serial_type){
1b151 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79  .  if( serial_ty
1b152 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 72 65  pe>=12 ){.    re
1b153 74 75 72 6e 20 28 73 65 72 69 61 6c 5f 74 79 70  turn (serial_typ
1b154 65 2d 31 32 29 2f 32 3b 0a 20 20 7d 65 6c 73 65  e-12)/2;.  }else
1b155 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e  {.    static con
1b156 73 74 20 75 38 20 61 53 69 7a 65 5b 5d 20 3d 20  st u8 aSize[] = 
1b157 7b 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 34 2c  { 0, 1, 2, 3, 4,
1b158 20 36 2c 20 38 2c 20 38 2c 20 30 2c 20 30 2c 20   6, 8, 8, 0, 0, 
1b159 30 2c 20 30 20 7d 3b 0a 20 20 20 20 72 65 74 75  0, 0 };.    retu
1b15a 72 6e 20 61 53 69 7a 65 5b 73 65 72 69 61 6c 5f  rn aSize[serial_
1b15b 74 79 70 65 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  type];.  }.}../*
1b15c 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 6f 6e  .** If we are on
1b15d 20 61 6e 20 61 72 63 68 69 74 65 63 74 75 72 65   an architecture
1b15e 20 77 69 74 68 20 6d 69 78 65 64 2d 65 6e 64 69   with mixed-endi
1b15f 61 6e 20 66 6c 6f 61 74 69 6e 67 20 0a 2a 2a 20  an floating .** 
1b160 70 6f 69 6e 74 73 20 28 65 78 3a 20 41 52 4d 37  points (ex: ARM7
1b161 29 20 74 68 65 6e 20 73 77 61 70 20 74 68 65 20  ) then swap the 
1b162 6c 6f 77 65 72 20 34 20 62 79 74 65 73 20 77 69  lower 4 bytes wi
1b163 74 68 20 74 68 65 20 0a 2a 2a 20 75 70 70 65 72  th the .** upper
1b164 20 34 20 62 79 74 65 73 2e 20 20 52 65 74 75 72   4 bytes.  Retur
1b165 6e 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a  n the result..**
1b166 0a 2a 2a 20 46 6f 72 20 6d 6f 73 74 20 61 72 63  .** For most arc
1b167 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 69 73  hitectures, this
1b168 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
1b169 2a 2a 20 28 6c 61 74 65 72 29 3a 20 20 49 74 20  ** (later):  It 
1b16a 69 73 20 72 65 70 6f 72 74 65 64 20 74 6f 20 6d  is reported to m
1b16b 65 20 74 68 61 74 20 74 68 65 20 6d 69 78 65 64  e that the mixed
1b16c 2d 65 6e 64 69 61 6e 20 70 72 6f 62 6c 65 6d 0a  -endian problem.
1b16d 2a 2a 20 6f 6e 20 41 52 4d 37 20 69 73 20 61 6e  ** on ARM7 is an
1b16e 20 69 73 73 75 65 20 77 69 74 68 20 47 43 43 2c   issue with GCC,
1b16f 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 41 52   not with the AR
1b170 4d 37 20 63 68 69 70 2e 20 20 49 74 20 73 65 65  M7 chip.  It see
1b171 6d 73 0a 2a 2a 20 74 68 61 74 20 65 61 72 6c 79  ms.** that early
1b172 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 43 43   versions of GCC
1b173 20 73 74 6f 72 65 64 20 74 68 65 20 74 77 6f 20   stored the two 
1b174 77 6f 72 64 73 20 6f 66 20 61 20 36 34 2d 62 69  words of a 64-bi
1b175 74 0a 2a 2a 20 66 6c 6f 61 74 20 69 6e 20 74 68  t.** float in th
1b176 65 20 77 72 6f 6e 67 20 6f 72 64 65 72 2e 20 20  e wrong order.  
1b177 41 6e 64 20 74 68 61 74 20 65 72 72 6f 72 20 68  And that error h
1b178 61 73 20 62 65 65 6e 20 70 72 6f 70 61 67 61 74  as been propagat
1b179 65 64 0a 2a 2a 20 65 76 65 72 20 73 69 6e 63 65  ed.** ever since
1b17a 2e 20 20 54 68 65 20 62 6c 61 6d 65 20 69 73 20  .  The blame is 
1b17b 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20  not necessarily 
1b17c 77 69 74 68 20 47 43 43 2c 20 74 68 6f 75 67 68  with GCC, though
1b17d 2e 0a 2a 2a 20 47 43 43 20 6d 69 67 68 74 20 68  ..** GCC might h
1b17e 61 76 65 20 6a 75 73 74 20 63 6f 70 79 69 6e 67  ave just copying
1b17f 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 66 72 6f   the problem fro
1b180 6d 20 61 20 70 72 69 6f 72 20 63 6f 6d 70 69 6c  m a prior compil
1b181 65 72 2e 0a 2a 2a 20 49 20 61 6d 20 61 6c 73 6f  er..** I am also
1b182 20 74 6f 6c 64 20 74 68 61 74 20 6e 65 77 65 72   told that newer
1b183 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 47 43 43   versions of GCC
1b184 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 20 64   that follow a d
1b185 69 66 66 65 72 65 6e 74 0a 2a 2a 20 41 42 49 20  ifferent.** ABI 
1b186 67 65 74 20 74 68 65 20 62 79 74 65 20 6f 72 64  get the byte ord
1b187 65 72 20 72 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20  er right..**.** 
1b188 44 65 76 65 6c 6f 70 65 72 73 20 75 73 69 6e 67  Developers using
1b189 20 53 51 4c 69 74 65 20 6f 6e 20 61 6e 20 41 52   SQLite on an AR
1b18a 4d 37 20 73 68 6f 75 6c 64 20 63 6f 6d 70 69 6c  M7 should compil
1b18b 65 20 61 6e 64 20 72 75 6e 20 74 68 65 69 72 0a  e and run their.
1b18c 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 75  ** application u
1b18d 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f 44 45  sing -DSQLITE_DE
1b18e 42 55 47 3d 31 20 61 74 20 6c 65 61 73 74 20 6f  BUG=1 at least o
1b18f 6e 63 65 2e 20 20 57 69 74 68 20 44 45 42 55 47  nce.  With DEBUG
1b190 0a 2a 2a 20 65 6e 61 62 6c 65 64 2c 20 73 6f 6d  .** enabled, som
1b191 65 20 61 73 73 65 72 74 73 20 62 65 6c 6f 77 20  e asserts below 
1b192 77 69 6c 6c 20 65 6e 73 75 72 65 20 74 68 61 74  will ensure that
1b193 20 74 68 65 20 62 79 74 65 20 6f 72 64 65 72 20   the byte order 
1b194 6f 66 0a 2a 2a 20 66 6c 6f 61 74 69 6e 67 20 70  of.** floating p
1b195 6f 69 6e 74 20 76 61 6c 75 65 73 20 69 73 20 63  oint values is c
1b196 6f 72 72 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 28 32  orrect..**.** (2
1b197 30 30 37 2d 30 38 2d 33 30 29 20 20 46 72 61 6e  007-08-30)  Fran
1b198 6b 20 76 61 6e 20 56 75 67 74 20 68 61 73 20 73  k van Vugt has s
1b199 74 75 64 69 65 64 20 74 68 69 73 20 70 72 6f 62  tudied this prob
1b19a 6c 65 6d 20 63 6c 6f 73 65 6c 79 0a 2a 2a 20 61  lem closely.** a
1b19b 6e 64 20 68 61 73 20 73 65 6e 64 20 68 69 73 20  nd has send his 
1b19c 66 69 6e 64 69 6e 67 73 20 74 6f 20 74 68 65 20  findings to the 
1b19d 53 51 4c 69 74 65 20 64 65 76 65 6c 6f 70 65 72  SQLite developer
1b19e 73 2e 20 20 46 72 61 6e 6b 0a 2a 2a 20 77 72 69  s.  Frank.** wri
1b19f 74 65 73 20 74 68 61 74 20 73 6f 6d 65 20 4c 69  tes that some Li
1b1a0 6e 75 78 20 6b 65 72 6e 65 6c 73 20 6f 66 66 65  nux kernels offe
1b1a1 72 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  r floating point
1b1a2 20 68 61 72 64 77 61 72 65 0a 2a 2a 20 65 6d 75   hardware.** emu
1b1a3 6c 61 74 69 6f 6e 20 74 68 61 74 20 75 73 65 73  lation that uses
1b1a4 20 6f 6e 6c 79 20 33 32 2d 62 69 74 20 6d 61 6e   only 32-bit man
1b1a5 74 69 73 73 61 73 20 69 6e 73 74 65 61 64 20 6f  tissas instead o
1b1a6 66 20 61 20 66 75 6c 6c 20 0a 2a 2a 20 34 38 2d  f a full .** 48-
1b1a7 62 69 74 73 20 61 73 20 72 65 71 75 69 72 65 64  bits as required
1b1a8 20 62 79 20 74 68 65 20 49 45 45 45 20 73 74 61   by the IEEE sta
1b1a9 6e 64 61 72 64 2e 20 20 28 54 68 69 73 20 69 73  ndard.  (This is
1b1aa 20 74 68 65 0a 2a 2a 20 43 4f 4e 46 49 47 5f 46   the.** CONFIG_F
1b1ab 50 45 5f 46 41 53 54 46 50 45 20 6f 70 74 69 6f  PE_FASTFPE optio
1b1ac 6e 2e 29 20 20 4f 6e 20 73 75 63 68 20 73 79 73  n.)  On such sys
1b1ad 74 65 6d 73 2c 20 66 6c 6f 61 74 69 6e 67 20 70  tems, floating p
1b1ae 6f 69 6e 74 0a 2a 2a 20 62 79 74 65 20 73 77 61  oint.** byte swa
1b1af 70 70 69 6e 67 20 62 65 63 6f 6d 65 73 20 76 65  pping becomes ve
1b1b0 72 79 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 20  ry complicated. 
1b1b1 20 54 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65   To avoid proble
1b1b2 6d 73 2c 0a 2a 2a 20 74 68 65 20 6e 65 63 65 73  ms,.** the neces
1b1b3 73 61 72 79 20 62 79 74 65 20 73 77 61 70 70 69  sary byte swappi
1b1b4 6e 67 20 69 73 20 63 61 72 72 69 65 64 20 6f 75  ng is carried ou
1b1b5 74 20 75 73 69 6e 67 20 61 20 36 34 2d 62 69 74  t using a 64-bit
1b1b6 20 69 6e 74 65 67 65 72 0a 2a 2a 20 72 61 74 68   integer.** rath
1b1b7 65 72 20 74 68 61 6e 20 61 20 36 34 2d 62 69 74  er than a 64-bit
1b1b8 20 66 6c 6f 61 74 2e 20 20 46 72 61 6e 6b 20 61   float.  Frank a
1b1b9 73 73 75 72 65 73 20 75 73 20 74 68 61 74 20 74  ssures us that t
1b1ba 68 65 20 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20  he code here.** 
1b1bb 77 6f 72 6b 73 20 66 6f 72 20 68 69 6d 2e 20 20  works for him.  
1b1bc 57 65 2c 20 74 68 65 20 64 65 76 65 6c 6f 70 65  We, the develope
1b1bd 72 73 2c 20 68 61 76 65 20 6e 6f 20 77 61 79 20  rs, have no way 
1b1be 74 6f 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79  to independently
1b1bf 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 69 73 2c  .** verify this,
1b1c0 20 62 75 74 20 46 72 61 6e 6b 20 73 65 65 6d 73   but Frank seems
1b1c1 20 74 6f 20 6b 6e 6f 77 20 77 68 61 74 20 68 65   to know what he
1b1c2 20 69 73 20 74 61 6c 6b 69 6e 67 20 61 62 6f 75   is talking abou
1b1c3 74 0a 2a 2a 20 73 6f 20 77 65 20 74 72 75 73 74  t.** so we trust
1b1c4 20 68 69 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20   him..*/.#ifdef 
1b1c5 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 4e 44  SQLITE_MIXED_END
1b1c6 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 54 0a  IAN_64BIT_FLOAT.
1b1c7 73 74 61 74 69 63 20 75 36 34 20 66 6c 6f 61 74  static u64 float
1b1c8 53 77 61 70 28 75 36 34 20 69 6e 29 7b 0a 20 20  Swap(u64 in){.  
1b1c9 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 75 36 34 20  union {.    u64 
1b1ca 72 3b 0a 20 20 20 20 75 33 32 20 69 5b 32 5d 3b  r;.    u32 i[2];
1b1cb 0a 20 20 7d 20 75 3b 0a 20 20 75 33 32 20 74 3b  .  } u;.  u32 t;
1b1cc 0a 0a 20 20 75 2e 72 20 3d 20 69 6e 3b 0a 20 20  ..  u.r = in;.  
1b1cd 74 20 3d 20 75 2e 69 5b 30 5d 3b 0a 20 20 75 2e  t = u.i[0];.  u.
1b1ce 69 5b 30 5d 20 3d 20 75 2e 69 5b 31 5d 3b 0a 20  i[0] = u.i[1];. 
1b1cf 20 75 2e 69 5b 31 5d 20 3d 20 74 3b 0a 20 20 72   u.i[1] = t;.  r
1b1d0 65 74 75 72 6e 20 75 2e 72 3b 0a 7d 0a 23 20 64  eturn u.r;.}.# d
1b1d1 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65 64 45  efine swapMixedE
1b1d2 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 20 20 58  ndianFloat(X)  X
1b1d3 20 3d 20 66 6c 6f 61 74 53 77 61 70 28 58 29 0a   = floatSwap(X).
1b1d4 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
1b1d5 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c  wapMixedEndianFl
1b1d6 6f 61 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f  oat(X).#endif../
1b1d7 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73  *.** Write the s
1b1d8 65 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 62  erialized data b
1b1d9 6c 6f 62 20 66 6f 72 20 74 68 65 20 76 61 6c 75  lob for the valu
1b1da 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d 65 6d  e stored in pMem
1b1db 20 69 6e 74 6f 20 0a 2a 2a 20 62 75 66 2e 20 49   into .** buf. I
1b1dc 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
1b1dd 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73  t the caller has
1b1de 20 61 6c 6c 6f 63 61 74 65 64 20 73 75 66 66 69   allocated suffi
1b1df 63 69 65 6e 74 20 73 70 61 63 65 2e 0a 2a 2a 20  cient space..** 
1b1e0 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1b1e1 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74  r of bytes writt
1b1e2 65 6e 2e 0a 2a 2a 0a 2a 2a 20 6e 42 75 66 20 69  en..**.** nBuf i
1b1e3 73 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  s the amount of 
1b1e4 73 70 61 63 65 20 6c 65 66 74 20 69 6e 20 62 75  space left in bu
1b1e5 66 5b 5d 2e 20 20 6e 42 75 66 20 6d 75 73 74 20  f[].  nBuf must 
1b1e6 61 6c 77 61 79 73 20 62 65 0a 2a 2a 20 6c 61 72  always be.** lar
1b1e7 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c  ge enough to hol
1b1e8 64 20 74 68 65 20 65 6e 74 69 72 65 20 66 69 65  d the entire fie
1b1e9 6c 64 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20  ld.  Except, if 
1b1ea 74 68 65 20 66 69 65 6c 64 20 69 73 0a 2a 2a 20  the field is.** 
1b1eb 61 20 62 6c 6f 62 20 77 69 74 68 20 61 20 7a 65  a blob with a ze
1b1ec 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2c 20  ro-filled tail, 
1b1ed 74 68 65 6e 20 62 75 66 5b 5d 20 6d 69 67 68 74  then buf[] might
1b1ee 20 62 65 20 6a 75 73 74 20 74 68 65 20 72 69 67   be just the rig
1b1ef 68 74 0a 2a 2a 20 73 69 7a 65 20 74 6f 20 68 6f  ht.** size to ho
1b1f0 6c 64 20 65 76 65 72 79 74 68 69 6e 67 20 65 78  ld everything ex
1b1f1 63 65 70 74 20 66 6f 72 20 74 68 65 20 7a 65 72  cept for the zer
1b1f2 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e 20 20  o-filled tail.  
1b1f3 49 66 20 62 75 66 5b 5d 0a 2a 2a 20 69 73 20 6f  If buf[].** is o
1b1f4 6e 6c 79 20 62 69 67 20 65 6e 6f 75 67 68 20 74  nly big enough t
1b1f5 6f 20 68 6f 6c 64 20 74 68 65 20 6e 6f 6e 2d 7a  o hold the non-z
1b1f6 65 72 6f 20 70 72 65 66 69 78 2c 20 74 68 65 6e  ero prefix, then
1b1f7 20 6f 6e 6c 79 20 77 72 69 74 65 20 74 68 61 74   only write that
1b1f8 0a 2a 2a 20 70 72 65 66 69 78 20 69 6e 74 6f 20  .** prefix into 
1b1f9 62 75 66 5b 5d 2e 20 20 42 75 74 20 69 66 20 62  buf[].  But if b
1b1fa 75 66 5b 5d 20 69 73 20 6c 61 72 67 65 20 65 6e  uf[] is large en
1b1fb 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 62 6f 74  ough to hold bot
1b1fc 68 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78 20  h the.** prefix 
1b1fd 61 6e 64 20 74 68 65 20 74 61 69 6c 20 74 68 65  and the tail the
1b1fe 6e 20 77 72 69 74 65 20 74 68 65 20 70 72 65 66  n write the pref
1b1ff 69 78 20 61 6e 64 20 73 65 74 20 74 68 65 20 74  ix and set the t
1b200 61 69 6c 20 74 6f 20 61 6c 6c 0a 2a 2a 20 7a 65  ail to all.** ze
1b201 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ros..**.** Retur
1b202 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1b203 62 79 74 65 73 20 61 63 74 75 61 6c 6c 79 20 77  bytes actually w
1b204 72 69 74 74 65 6e 20 69 6e 74 6f 20 62 75 66 5b  ritten into buf[
1b205 5d 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a  ].  The number.*
1b206 2a 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  * of bytes in th
1b207 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61  e zero-filled ta
1b208 69 6c 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69  il is included i
1b209 6e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  n the return val
1b20a 75 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 74 68  ue only.** if th
1b20b 6f 73 65 20 62 79 74 65 73 20 77 65 72 65 20 7a  ose bytes were z
1b20c 65 72 6f 65 64 20 69 6e 20 62 75 66 5b 5d 2e 0a  eroed in buf[]..
1b20d 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */ .SQLITE_PRIVA
1b20e 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64  TE u32 sqlite3Vd
1b20f 62 65 53 65 72 69 61 6c 50 75 74 28 75 38 20 2a  beSerialPut(u8 *
1b210 62 75 66 2c 20 69 6e 74 20 6e 42 75 66 2c 20 4d  buf, int nBuf, M
1b211 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66 69  em *pMem, int fi
1b212 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 75 33  le_format){.  u3
1b213 32 20 73 65 72 69 61 6c 5f 74 79 70 65 20 3d 20  2 serial_type = 
1b214 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1b215 6c 54 79 70 65 28 70 4d 65 6d 2c 20 66 69 6c 65  lType(pMem, file
1b216 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 75 33 32 20  _format);.  u32 
1b217 6c 65 6e 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 67  len;..  /* Integ
1b218 65 72 20 61 6e 64 20 52 65 61 6c 20 2a 2f 0a 20  er and Real */. 
1b219 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65   if( serial_type
1b21a 3c 3d 37 20 26 26 20 73 65 72 69 61 6c 5f 74 79  <=7 && serial_ty
1b21b 70 65 3e 30 20 29 7b 0a 20 20 20 20 75 36 34 20  pe>0 ){.    u64 
1b21c 76 3b 0a 20 20 20 20 75 33 32 20 69 3b 0a 20 20  v;.    u32 i;.  
1b21d 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70    if( serial_typ
1b21e 65 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20 61 73  e==7 ){.      as
1b21f 73 65 72 74 28 20 73 69 7a 65 6f 66 28 76 29 3d  sert( sizeof(v)=
1b220 3d 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29  =sizeof(pMem->r)
1b221 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   );.      memcpy
1b222 28 26 76 2c 20 26 70 4d 65 6d 2d 3e 72 2c 20 73  (&v, &pMem->r, s
1b223 69 7a 65 6f 66 28 76 29 29 3b 0a 20 20 20 20 20  izeof(v));.     
1b224 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61 6e   swapMixedEndian
1b225 46 6c 6f 61 74 28 76 29 3b 0a 20 20 20 20 7d 65  Float(v);.    }e
1b226 6c 73 65 7b 0a 20 20 20 20 20 20 76 20 3d 20 70  lse{.      v = p
1b227 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 7d 0a  Mem->u.i;.    }.
1b228 20 20 20 20 6c 65 6e 20 3d 20 69 20 3d 20 73 71      len = i = sq
1b229 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
1b22a 79 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79  ypeLen(serial_ty
1b22b 70 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  pe);.    assert(
1b22c 20 6c 65 6e 3c 3d 28 75 33 32 29 6e 42 75 66 20   len<=(u32)nBuf 
1b22d 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d  );.    while( i-
1b22e 2d 20 29 7b 0a 20 20 20 20 20 20 62 75 66 5b 69  - ){.      buf[i
1b22f 5d 20 3d 20 28 75 38 29 28 76 26 30 78 46 46 29  ] = (u8)(v&0xFF)
1b230 3b 0a 20 20 20 20 20 20 76 20 3e 3e 3d 20 38 3b  ;.      v >>= 8;
1b231 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
1b232 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  n len;.  }..  /*
1b233 20 53 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20   String or blob 
1b234 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61 6c 5f  */.  if( serial_
1b235 74 79 70 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20  type>=12 ){.    
1b236 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 20  assert( pMem->n 
1b237 2b 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  + ((pMem->flags 
1b238 26 20 4d 45 4d 5f 5a 65 72 6f 29 3f 70 4d 65 6d  & MEM_Zero)?pMem
1b239 2d 3e 75 2e 6e 5a 65 72 6f 3a 30 29 0a 20 20 20  ->u.nZero:0).   
1b23a 20 20 20 20 20 20 20 20 20 20 3d 3d 20 28 69 6e            == (in
1b23b 74 29 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  t)sqlite3VdbeSer
1b23c 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72 69 61  ialTypeLen(seria
1b23d 6c 5f 74 79 70 65 29 20 29 3b 0a 20 20 20 20 61  l_type) );.    a
1b23e 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 3c 3d  ssert( pMem->n<=
1b23f 6e 42 75 66 20 29 3b 0a 20 20 20 20 6c 65 6e 20  nBuf );.    len 
1b240 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20 20 20 20 6d  = pMem->n;.    m
1b241 65 6d 63 70 79 28 62 75 66 2c 20 70 4d 65 6d 2d  emcpy(buf, pMem-
1b242 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 69 66  >z, len);.    if
1b243 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  ( pMem->flags & 
1b244 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20  MEM_Zero ){.    
1b245 20 20 6c 65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75    len += pMem->u
1b246 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 20 20 61 73  .nZero;.      as
1b247 73 65 72 74 28 20 6e 42 75 66 3e 3d 30 20 29 3b  sert( nBuf>=0 );
1b248 0a 20 20 20 20 20 20 69 66 28 20 6c 65 6e 20 3e  .      if( len >
1b249 20 28 75 33 32 29 6e 42 75 66 20 29 7b 0a 20 20   (u32)nBuf ){.  
1b24a 20 20 20 20 20 20 6c 65 6e 20 3d 20 28 75 33 32        len = (u32
1b24b 29 6e 42 75 66 3b 0a 20 20 20 20 20 20 7d 0a 20  )nBuf;.      }. 
1b24c 20 20 20 20 20 6d 65 6d 73 65 74 28 26 62 75 66       memset(&buf
1b24d 5b 70 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 6c 65  [pMem->n], 0, le
1b24e 6e 2d 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20 20  n-pMem->n);.    
1b24f 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 65 6e  }.    return len
1b250 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 55 4c 4c  ;.  }..  /* NULL
1b251 20 6f 72 20 63 6f 6e 73 74 61 6e 74 73 20 30 20   or constants 0 
1b252 6f 72 20 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e  or 1 */.  return
1b253 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73   0;.}../*.** Des
1b254 65 72 69 61 6c 69 7a 65 20 74 68 65 20 64 61 74  erialize the dat
1b255 61 20 62 6c 6f 62 20 70 6f 69 6e 74 65 64 20 74  a blob pointed t
1b256 6f 20 62 79 20 62 75 66 20 61 73 20 73 65 72 69  o by buf as seri
1b257 61 6c 20 74 79 70 65 20 73 65 72 69 61 6c 5f 74  al type serial_t
1b258 79 70 65 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65  ype.** and store
1b259 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 70   the result in p
1b25a 4d 65 6d 2e 20 20 52 65 74 75 72 6e 20 74 68 65  Mem.  Return the
1b25b 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1b25c 20 72 65 61 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54   read..*/ .SQLIT
1b25d 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71  E_PRIVATE u32 sq
1b25e 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47  lite3VdbeSerialG
1b25f 65 74 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  et(.  const unsi
1b260 67 6e 65 64 20 63 68 61 72 20 2a 62 75 66 2c 20  gned char *buf, 
1b261 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 74 6f      /* Buffer to
1b262 20 64 65 73 65 72 69 61 6c 69 7a 65 20 66 72 6f   deserialize fro
1b263 6d 20 2a 2f 0a 20 20 75 33 32 20 73 65 72 69 61  m */.  u32 seria
1b264 6c 5f 74 79 70 65 2c 20 20 20 20 20 20 20 20 20  l_type,         
1b265 20 20 20 20 20 2f 2a 20 53 65 72 69 61 6c 20 74       /* Serial t
1b266 79 70 65 20 74 6f 20 64 65 73 65 72 69 61 6c 69  ype to deseriali
1b267 7a 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65  ze */.  Mem *pMe
1b268 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m               
1b269 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
1b26a 63 65 6c 6c 20 74 6f 20 77 72 69 74 65 20 76 61  cell to write va
1b26b 6c 75 65 20 69 6e 74 6f 20 2a 2f 0a 29 7b 0a 20  lue into */.){. 
1b26c 20 73 77 69 74 63 68 28 20 73 65 72 69 61 6c 5f   switch( serial_
1b26d 74 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65  type ){.    case
1b26e 20 31 30 3a 20 20 20 2f 2a 20 52 65 73 65 72 76   10:   /* Reserv
1b26f 65 64 20 66 6f 72 20 66 75 74 75 72 65 20 75 73  ed for future us
1b270 65 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 31  e */.    case 11
1b271 3a 20 20 20 2f 2a 20 52 65 73 65 72 76 65 64 20  :   /* Reserved 
1b272 66 6f 72 20 66 75 74 75 72 65 20 75 73 65 20 2a  for future use *
1b273 2f 0a 20 20 20 20 63 61 73 65 20 30 3a 20 7b 20  /.    case 0: { 
1b274 20 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20   /* NULL */.    
1b275 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1b276 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20  MEM_Null;.      
1b277 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1b278 20 63 61 73 65 20 31 3a 20 7b 20 2f 2a 20 31 2d   case 1: { /* 1-
1b279 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65  byte signed inte
1b27a 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65  ger */.      pMe
1b27b 6d 2d 3e 75 2e 69 20 3d 20 28 73 69 67 6e 65 64  m->u.i = (signed
1b27c 20 63 68 61 72 29 62 75 66 5b 30 5d 3b 0a 20 20   char)buf[0];.  
1b27d 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1b27e 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
1b27f 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
1b280 0a 20 20 20 20 63 61 73 65 20 32 3a 20 7b 20 2f  .    case 2: { /
1b281 2a 20 32 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 2-byte signed 
1b282 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20  integer */.     
1b283 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28   pMem->u.i = (((
1b284 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b  signed char)buf[
1b285 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31 5d  0])<<8) | buf[1]
1b286 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c  ;.      pMem->fl
1b287 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  ags = MEM_Int;. 
1b288 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
1b289 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 33 3a     }.    case 3:
1b28a 20 7b 20 2f 2a 20 33 2d 62 79 74 65 20 73 69 67   { /* 3-byte sig
1b28b 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20  ned integer */. 
1b28c 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d       pMem->u.i =
1b28d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61 72 29   (((signed char)
1b28e 62 75 66 5b 30 5d 29 3c 3c 31 36 29 20 7c 20 28  buf[0])<<16) | (
1b28f 62 75 66 5b 31 5d 3c 3c 38 29 20 7c 20 62 75 66  buf[1]<<8) | buf
1b290 5b 32 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  [2];.      pMem-
1b291 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1b292 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 33  ;.      return 3
1b293 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1b294 20 34 3a 20 7b 20 2f 2a 20 34 2d 62 79 74 65 20   4: { /* 4-byte 
1b295 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
1b296 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  /.      pMem->u.
1b297 69 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29  i = (buf[0]<<24)
1b298 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20   | (buf[1]<<16) 
1b299 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20  | (buf[2]<<8) | 
1b29a 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 70 4d  buf[3];.      pM
1b29b 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1b29c 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75 72  Int;.      retur
1b29d 6e 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 4;.    }.    c
1b29e 61 73 65 20 35 3a 20 7b 20 2f 2a 20 36 2d 62 79  ase 5: { /* 6-by
1b29f 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  te signed intege
1b2a0 72 20 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78  r */.      u64 x
1b2a1 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68 61   = (((signed cha
1b2a2 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c 20  r)buf[0])<<8) | 
1b2a3 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 75 33  buf[1];.      u3
1b2a4 32 20 79 20 3d 20 28 62 75 66 5b 32 5d 3c 3c 32  2 y = (buf[2]<<2
1b2a5 34 29 20 7c 20 28 62 75 66 5b 33 5d 3c 3c 31 36  4) | (buf[3]<<16
1b2a6 29 20 7c 20 28 62 75 66 5b 34 5d 3c 3c 38 29 20  ) | (buf[4]<<8) 
1b2a7 7c 20 62 75 66 5b 35 5d 3b 0a 20 20 20 20 20 20  | buf[5];.      
1b2a8 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 79 3b  x = (x<<32) | y;
1b2a9 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  .      pMem->u.i
1b2aa 20 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 20   = *(i64*)&x;.  
1b2ab 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1b2ac 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20  = MEM_Int;.     
1b2ad 20 72 65 74 75 72 6e 20 36 3b 0a 20 20 20 20 7d   return 6;.    }
1b2ae 0a 20 20 20 20 63 61 73 65 20 36 3a 20 20 20 2f  .    case 6:   /
1b2af 2a 20 38 2d 62 79 74 65 20 73 69 67 6e 65 64 20  * 8-byte signed 
1b2b0 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 63  integer */.    c
1b2b1 61 73 65 20 37 3a 20 7b 20 2f 2a 20 49 45 45 45  ase 7: { /* IEEE
1b2b2 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
1b2b3 2a 2f 0a 20 20 20 20 20 20 75 36 34 20 78 3b 0a  */.      u64 x;.
1b2b4 20 20 20 20 20 20 75 33 32 20 79 3b 0a 23 69 66        u32 y;.#if
1b2b5 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
1b2b6 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
1b2b7 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
1b2b8 4e 47 5f 50 4f 49 4e 54 29 0a 20 20 20 20 20 20  NG_POINT).      
1b2b9 2f 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 69  /* Verify that i
1b2ba 6e 74 65 67 65 72 73 20 61 6e 64 20 66 6c 6f 61  ntegers and floa
1b2bb 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
1b2bc 73 20 75 73 65 20 74 68 65 20 73 61 6d 65 0a 20  s use the same. 
1b2bd 20 20 20 20 20 2a 2a 20 62 79 74 65 20 6f 72 64       ** byte ord
1b2be 65 72 2e 20 20 4f 72 2c 20 74 68 61 74 20 69 66  er.  Or, that if
1b2bf 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45 4e   SQLITE_MIXED_EN
1b2c0 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41 54  DIAN_64BIT_FLOAT
1b2c1 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 64 65 66   is.      ** def
1b2c2 69 6e 65 64 20 74 68 61 74 20 36 34 2d 62 69 74  ined that 64-bit
1b2c3 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
1b2c4 76 61 6c 75 65 73 20 72 65 61 6c 6c 79 20 61 72  values really ar
1b2c5 65 20 6d 69 78 65 64 0a 20 20 20 20 20 20 2a 2a  e mixed.      **
1b2c6 20 65 6e 64 69 61 6e 2e 0a 20 20 20 20 20 20 2a   endian..      *
1b2c7 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63  /.      static c
1b2c8 6f 6e 73 74 20 75 36 34 20 74 31 20 3d 20 28 28  onst u64 t1 = ((
1b2c9 75 36 34 29 30 78 33 66 66 30 30 30 30 30 29 3c  u64)0x3ff00000)<
1b2ca 3c 33 32 3b 0a 20 20 20 20 20 20 73 74 61 74 69  <32;.      stati
1b2cb 63 20 63 6f 6e 73 74 20 64 6f 75 62 6c 65 20 72  c const double r
1b2cc 31 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 75  1 = 1.0;.      u
1b2cd 36 34 20 74 32 20 3d 20 74 31 3b 0a 20 20 20 20  64 t2 = t1;.    
1b2ce 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61    swapMixedEndia
1b2cf 6e 46 6c 6f 61 74 28 74 32 29 3b 0a 20 20 20 20  nFloat(t2);.    
1b2d0 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
1b2d1 28 72 31 29 3d 3d 73 69 7a 65 6f 66 28 74 32 29  (r1)==sizeof(t2)
1b2d2 20 26 26 20 6d 65 6d 63 6d 70 28 26 72 31 2c 20   && memcmp(&r1, 
1b2d3 26 74 32 2c 20 73 69 7a 65 6f 66 28 72 31 29 29  &t2, sizeof(r1))
1b2d4 3d 3d 30 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  ==0 );.#endif.. 
1b2d5 20 20 20 20 20 78 20 3d 20 28 62 75 66 5b 30 5d       x = (buf[0]
1b2d6 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31 5d 3c  <<24) | (buf[1]<
1b2d7 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d 3c 3c  <16) | (buf[2]<<
1b2d8 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20 20 20  8) | buf[3];.   
1b2d9 20 20 20 79 20 3d 20 28 62 75 66 5b 34 5d 3c 3c     y = (buf[4]<<
1b2da 32 34 29 20 7c 20 28 62 75 66 5b 35 5d 3c 3c 31  24) | (buf[5]<<1
1b2db 36 29 20 7c 20 28 62 75 66 5b 36 5d 3c 3c 38 29  6) | (buf[6]<<8)
1b2dc 20 7c 20 62 75 66 5b 37 5d 3b 0a 20 20 20 20 20   | buf[7];.     
1b2dd 20 78 20 3d 20 28 78 3c 3c 33 32 29 20 7c 20 79   x = (x<<32) | y
1b2de 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69  ;.      if( seri
1b2df 61 6c 5f 74 79 70 65 3d 3d 36 20 29 7b 0a 20 20  al_type==6 ){.  
1b2e0 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20        pMem->u.i 
1b2e1 3d 20 2a 28 69 36 34 2a 29 26 78 3b 0a 20 20 20  = *(i64*)&x;.   
1b2e2 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1b2e3 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1b2e4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1b2e5 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
1b2e6 78 29 3d 3d 38 20 26 26 20 73 69 7a 65 6f 66 28  x)==8 && sizeof(
1b2e7 70 4d 65 6d 2d 3e 72 29 3d 3d 38 20 29 3b 0a 20  pMem->r)==8 );. 
1b2e8 20 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64         swapMixed
1b2e9 45 6e 64 69 61 6e 46 6c 6f 61 74 28 78 29 3b 0a  EndianFloat(x);.
1b2ea 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
1b2eb 70 4d 65 6d 2d 3e 72 2c 20 26 78 2c 20 73 69 7a  pMem->r, &x, siz
1b2ec 65 6f 66 28 78 29 29 3b 0a 20 20 20 20 20 20 20  eof(x));.       
1b2ed 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 73   pMem->flags = s
1b2ee 71 6c 69 74 65 33 49 73 4e 61 4e 28 70 4d 65 6d  qlite3IsNaN(pMem
1b2ef 2d 3e 72 29 20 3f 20 4d 45 4d 5f 4e 75 6c 6c 20  ->r) ? MEM_Null 
1b2f0 3a 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20 20  : MEM_Real;.    
1b2f1 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e    }.      return
1b2f2 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   8;.    }.    ca
1b2f3 73 65 20 38 3a 20 20 20 20 2f 2a 20 49 6e 74 65  se 8:    /* Inte
1b2f4 67 65 72 20 30 20 2a 2f 0a 20 20 20 20 63 61 73  ger 0 */.    cas
1b2f5 65 20 39 3a 20 7b 20 20 2f 2a 20 49 6e 74 65 67  e 9: {  /* Integ
1b2f6 65 72 20 31 20 2a 2f 0a 20 20 20 20 20 20 70 4d  er 1 */.      pM
1b2f7 65 6d 2d 3e 75 2e 69 20 3d 20 73 65 72 69 61 6c  em->u.i = serial
1b2f8 5f 74 79 70 65 2d 38 3b 0a 20 20 20 20 20 20 70  _type-8;.      p
1b2f9 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1b2fa 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75  _Int;.      retu
1b2fb 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 0;.    }.    
1b2fc 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
1b2fd 20 75 33 32 20 6c 65 6e 20 3d 20 28 73 65 72 69   u32 len = (seri
1b2fe 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20  al_type-12)/2;. 
1b2ff 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28       pMem->z = (
1b300 63 68 61 72 20 2a 29 62 75 66 3b 0a 20 20 20 20  char *)buf;.    
1b301 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 6c 65 6e 3b    pMem->n = len;
1b302 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65  .      pMem->xDe
1b303 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  l = 0;.      if(
1b304 20 73 65 72 69 61 6c 5f 74 79 70 65 26 30 78 30   serial_type&0x0
1b305 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65  1 ){.        pMe
1b306 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 53  m->flags = MEM_S
1b307 74 72 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a  tr | MEM_Ephem;.
1b308 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1b309 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1b30a 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45   = MEM_Blob | ME
1b30b 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 7d  M_Ephem;.      }
1b30c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 6c 65  .      return le
1b30d 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  n;.    }.  }.  r
1b30e 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn 0;.}.../*.
1b30f 2a 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 4b 65  ** Given the nKe
1b310 79 2d 62 79 74 65 20 65 6e 63 6f 64 69 6e 67 20  y-byte encoding 
1b311 6f 66 20 61 20 72 65 63 6f 72 64 20 69 6e 20 70  of a record in p
1b312 4b 65 79 5b 5d 2c 20 70 61 72 73 65 20 74 68 65  Key[], parse the
1b313 0a 2a 2a 20 72 65 63 6f 72 64 20 69 6e 74 6f 20  .** record into 
1b314 61 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  a UnpackedRecord
1b315 20 73 74 72 75 63 74 75 72 65 2e 20 20 52 65 74   structure.  Ret
1b316 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
1b317 0a 2a 2a 20 74 68 61 74 20 73 74 72 75 63 74 75  .** that structu
1b318 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  re..**.** The ca
1b319 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d  lling function m
1b31a 69 67 68 74 20 70 72 6f 76 69 64 65 20 73 7a 53  ight provide szS
1b31b 70 61 63 65 20 62 79 74 65 73 20 6f 66 20 6d 65  pace bytes of me
1b31c 6d 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 61 74  mory.** space at
1b31d 20 70 53 70 61 63 65 2e 20 20 54 68 69 73 20 73   pSpace.  This s
1b31e 70 61 63 65 20 63 61 6e 20 62 65 20 75 73 65 64  pace can be used
1b31f 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 74   to hold the ret
1b320 75 72 6e 65 64 0a 2a 2a 20 56 44 62 65 50 61 72  urned.** VDbePar
1b321 73 65 64 52 65 63 6f 72 64 20 73 74 72 75 63 74  sedRecord struct
1b322 75 72 65 20 69 66 20 69 74 20 69 73 20 6c 61 72  ure if it is lar
1b323 67 65 20 65 6e 6f 75 67 68 2e 20 20 49 66 20 69  ge enough.  If i
1b324 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 62 69 67 20  t is.** not big 
1b325 65 6e 6f 75 67 68 2c 20 73 70 61 63 65 20 69 73  enough, space is
1b326 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
1b327 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
1b328 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
1b329 6e 65 64 20 73 74 72 75 63 74 75 72 65 20 73 68  ned structure sh
1b32a 6f 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 20 62  ould be closed b
1b32b 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73  y a call to.** s
1b32c 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
1b32d 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 29  UnpackedRecord()
1b32e 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/ .SQLITE_PRI
1b32f 56 41 54 45 20 55 6e 70 61 63 6b 65 64 52 65 63  VATE UnpackedRec
1b330 6f 72 64 20 2a 73 71 6c 69 74 65 33 56 64 62 65  ord *sqlite3Vdbe
1b331 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 0a 20 20  RecordUnpack(.  
1b332 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
1b333 6f 2c 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d  o,     /* Inform
1b334 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
1b335 72 65 63 6f 72 64 20 66 6f 72 6d 61 74 20 2a 2f  record format */
1b336 0a 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20  .  int nKey,    
1b337 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
1b338 65 20 6f 66 20 74 68 65 20 62 69 6e 61 72 79 20  e of the binary 
1b339 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 6f 6e 73  record */.  cons
1b33a 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20  t void *pKey,   
1b33b 20 20 20 2f 2a 20 54 68 65 20 62 69 6e 61 72 79     /* The binary
1b33c 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61   record */.  cha
1b33d 72 20 2a 70 53 70 61 63 65 2c 20 20 20 20 20 20  r *pSpace,      
1b33e 20 20 20 20 2f 2a 20 55 6e 61 6c 69 67 6e 65 64      /* Unaligned
1b33f 20 73 70 61 63 65 20 61 76 61 69 6c 61 62 6c 65   space available
1b340 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6f 62 6a   to hold the obj
1b341 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53  ect */.  int szS
1b342 70 61 63 65 20 20 20 20 20 20 20 20 20 20 20 20  pace            
1b343 2f 2a 20 53 69 7a 65 20 6f 66 20 70 53 70 61 63  /* Size of pSpac
1b344 65 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  e[] in bytes */.
1b345 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
1b346 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 20 3d  ned char *aKey =
1b347 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
1b348 20 63 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20   char *)pKey;.  
1b349 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
1b34a 70 3b 20 20 2f 2a 20 54 68 65 20 75 6e 70 61 63  p;  /* The unpac
1b34b 6b 65 64 20 72 65 63 6f 72 64 20 74 68 61 74 20  ked record that 
1b34c 77 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 2a  we will return *
1b34d 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20  /.  int nByte;  
1b34e 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
1b34f 79 20 73 70 61 63 65 20 6e 65 65 64 65 64 20 74  y space needed t
1b350 6f 20 68 6f 6c 64 20 70 2c 20 69 6e 20 62 79 74  o hold p, in byt
1b351 65 73 20 2a 2f 0a 20 20 69 6e 74 20 64 3b 0a 20  es */.  int d;. 
1b352 20 75 33 32 20 69 64 78 3b 0a 20 20 75 31 36 20   u32 idx;.  u16 
1b353 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  u;              
1b354 2f 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70  /* Unsigned loop
1b355 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 33   counter */.  u3
1b356 32 20 73 7a 48 64 72 3b 0a 20 20 4d 65 6d 20 2a  2 szHdr;.  Mem *
1b357 70 4d 65 6d 3b 0a 20 20 69 6e 74 20 6e 4f 66 66  pMem;.  int nOff
1b358 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ;           /* I
1b359 6e 63 72 65 61 73 65 20 70 53 70 61 63 65 20 62  ncrease pSpace b
1b35a 79 20 74 68 69 73 20 6d 75 63 68 20 74 6f 20 38  y this much to 8
1b35b 2d 62 79 74 65 20 61 6c 69 67 6e 20 69 74 20 2a  -byte align it *
1b35c 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 57  /.  .  /*.  ** W
1b35d 65 20 77 61 6e 74 20 74 6f 20 73 68 69 66 74 20  e want to shift 
1b35e 74 68 65 20 70 6f 69 6e 74 65 72 20 70 53 70 61  the pointer pSpa
1b35f 63 65 20 75 70 20 73 75 63 68 20 74 68 61 74 20  ce up such that 
1b360 69 74 20 69 73 20 38 2d 62 79 74 65 20 61 6c 69  it is 8-byte ali
1b361 67 6e 65 64 2e 0a 20 20 2a 2a 20 54 68 75 73 2c  gned..  ** Thus,
1b362 20 77 65 20 6e 65 65 64 20 74 6f 20 63 61 6c 63   we need to calc
1b363 75 6c 61 74 65 20 61 20 76 61 6c 75 65 2c 20 6e  ulate a value, n
1b364 4f 66 66 2c 20 62 65 74 77 65 65 6e 20 30 20 61  Off, between 0 a
1b365 6e 64 20 37 2c 20 74 6f 20 73 68 69 66 74 20 0a  nd 7, to shift .
1b366 20 20 2a 2a 20 69 74 20 62 79 2e 20 20 49 66 20    ** it by.  If 
1b367 70 53 70 61 63 65 20 69 73 20 61 6c 72 65 61 64  pSpace is alread
1b368 79 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64  y 8-byte aligned
1b369 2c 20 6e 4f 66 66 20 73 68 6f 75 6c 64 20 62 65  , nOff should be
1b36a 20 7a 65 72 6f 2e 0a 20 20 2a 2f 0a 20 20 6e 4f   zero..  */.  nO
1b36b 66 66 20 3d 20 28 38 20 2d 20 28 53 51 4c 49 54  ff = (8 - (SQLIT
1b36c 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 53 70  E_PTR_TO_INT(pSp
1b36d 61 63 65 29 20 26 20 37 29 29 20 26 20 37 3b 0a  ace) & 7)) & 7;.
1b36e 20 20 70 53 70 61 63 65 20 2b 3d 20 6e 4f 66 66    pSpace += nOff
1b36f 3b 0a 20 20 73 7a 53 70 61 63 65 20 2d 3d 20 6e  ;.  szSpace -= n
1b370 4f 66 66 3b 0a 20 20 6e 42 79 74 65 20 3d 20 52  Off;.  nByte = R
1b371 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 55 6e 70  OUND8(sizeof(Unp
1b372 61 63 6b 65 64 52 65 63 6f 72 64 29 29 20 2b 20  ackedRecord)) + 
1b373 73 69 7a 65 6f 66 28 4d 65 6d 29 2a 28 70 4b 65  sizeof(Mem)*(pKe
1b374 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 29  yInfo->nField+1)
1b375 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3e 73 7a  ;.  if( nByte>sz
1b376 53 70 61 63 65 20 29 7b 0a 20 20 20 20 70 20 3d  Space ){.    p =
1b377 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1b378 52 61 77 28 70 4b 65 79 49 6e 66 6f 2d 3e 64 62  Raw(pKeyInfo->db
1b379 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66  , nByte);.    if
1b37a 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
1b37b 30 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20  0;.    p->flags 
1b37c 3d 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f  = UNPACKED_NEED_
1b37d 46 52 45 45 20 7c 20 55 4e 50 41 43 4b 45 44 5f  FREE | UNPACKED_
1b37e 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20  NEED_DESTROY;.  
1b37f 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 28  }else{.    p = (
1b380 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 2a 29  UnpackedRecord*)
1b381 70 53 70 61 63 65 3b 0a 20 20 20 20 70 2d 3e 66  pSpace;.    p->f
1b382 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f  lags = UNPACKED_
1b383 4e 45 45 44 5f 44 45 53 54 52 4f 59 3b 0a 20 20  NEED_DESTROY;.  
1b384 7d 0a 20 20 70 2d 3e 70 4b 65 79 49 6e 66 6f 20  }.  p->pKeyInfo 
1b385 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 70 2d  = pKeyInfo;.  p-
1b386 3e 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e  >nField = pKeyIn
1b387 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 3b 0a  fo->nField + 1;.
1b388 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 70 4d 65 6d    p->aMem = pMem
1b389 20 3d 20 28 4d 65 6d 2a 29 26 28 28 63 68 61 72   = (Mem*)&((char
1b38a 2a 29 70 29 5b 52 4f 55 4e 44 38 28 73 69 7a 65  *)p)[ROUND8(size
1b38b 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  of(UnpackedRecor
1b38c 64 29 29 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  d))];.  assert( 
1b38d 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e  EIGHT_BYTE_ALIGN
1b38e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 20 20  MENT(pMem) );.  
1b38f 69 64 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33  idx = getVarint3
1b390 32 28 61 4b 65 79 2c 20 73 7a 48 64 72 29 3b 0a  2(aKey, szHdr);.
1b391 20 20 64 20 3d 20 73 7a 48 64 72 3b 0a 20 20 75    d = szHdr;.  u
1b392 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69   = 0;.  while( i
1b393 64 78 3c 73 7a 48 64 72 20 26 26 20 75 3c 70 2d  dx<szHdr && u<p-
1b394 3e 6e 46 69 65 6c 64 20 26 26 20 64 3c 3d 6e 4b  >nField && d<=nK
1b395 65 79 20 29 7b 0a 20 20 20 20 75 33 32 20 73 65  ey ){.    u32 se
1b396 72 69 61 6c 5f 74 79 70 65 3b 0a 0a 20 20 20 20  rial_type;..    
1b397 69 64 78 20 2b 3d 20 67 65 74 56 61 72 69 6e 74  idx += getVarint
1b398 33 32 28 26 61 4b 65 79 5b 69 64 78 5d 2c 20 73  32(&aKey[idx], s
1b399 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20  erial_type);.   
1b39a 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 70 4b 65   pMem->enc = pKe
1b39b 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 20 20  yInfo->enc;.    
1b39c 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b 65 79 49  pMem->db = pKeyI
1b39d 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20 70 4d 65  nfo->db;.    pMe
1b39e 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20  m->flags = 0;.  
1b39f 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20    pMem->zMalloc 
1b3a0 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d 20 73 71  = 0;.    d += sq
1b3a1 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47  lite3VdbeSerialG
1b3a2 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20 73 65 72  et(&aKey[d], ser
1b3a3 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65 6d 29 3b  ial_type, pMem);
1b3a4 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 20  .    pMem++;.   
1b3a5 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61 73 73 65   u++;.  }.  asse
1b3a6 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e 66 6f 2d  rt( u<=pKeyInfo-
1b3a7 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29 3b 0a 20  >nField + 1 );. 
1b3a8 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 75 3b 0a   p->nField = u;.
1b3a9 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
1b3aa 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  p;.}../*.** This
1b3ab 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 6f 79   routine destroy
1b3ac 73 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  s a UnpackedReco
1b3ad 72 64 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51  rd object..*/.SQ
1b3ae 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1b3af 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c  d sqlite3VdbeDel
1b3b0 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
1b3b1 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  d(UnpackedRecord
1b3b2 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20   *p){.  int i;. 
1b3b3 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 0a 20 20 61   Mem *pMem;..  a
1b3b4 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
1b3b5 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c 61 67   assert( p->flag
1b3b6 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45  s & UNPACKED_NEE
1b3b7 44 5f 44 45 53 54 52 4f 59 20 29 3b 0a 20 20 66  D_DESTROY );.  f
1b3b8 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d 70 2d 3e  or(i=0, pMem=p->
1b3b9 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46 69 65 6c  aMem; i<p->nFiel
1b3ba 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b 2b 29 7b  d; i++, pMem++){
1b3bb 0a 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 70 61  .    /* The unpa
1b3bc 63 6b 65 64 20 72 65 63 6f 72 64 20 69 73 20 61  cked record is a
1b3bd 6c 77 61 79 73 20 63 6f 6e 73 74 72 75 63 74 65  lways constructe
1b3be 64 20 62 79 20 74 68 65 0a 20 20 20 20 2a 2a 20  d by the.    ** 
1b3bf 73 71 6c 69 74 65 33 56 64 62 65 55 6e 70 61 63  sqlite3VdbeUnpac
1b3c0 6b 52 65 63 6f 72 64 28 29 20 66 75 6e 63 74 69  kRecord() functi
1b3c1 6f 6e 20 61 62 6f 76 65 2c 20 77 68 69 63 68 20  on above, which 
1b3c2 6d 61 6b 65 73 20 61 6c 6c 0a 20 20 20 20 2a 2a  makes all.    **
1b3c3 20 73 74 72 69 6e 67 73 20 61 6e 64 20 62 6c 6f   strings and blo
1b3c4 62 73 20 73 74 61 74 69 63 2e 20 20 41 6e 64 20  bs static.  And 
1b3c5 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65 6c 65 6d  none of the elem
1b3c6 65 6e 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20  ents are.    ** 
1b3c7 65 76 65 72 20 74 72 61 6e 73 66 6f 72 6d 65 64  ever transformed
1b3c8 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65  , so there is ne
1b3c9 76 65 72 20 61 6e 79 74 68 69 6e 67 20 74 6f 20  ver anything to 
1b3ca 64 65 6c 65 74 65 2e 0a 20 20 20 20 2a 2f 0a 20  delete..    */. 
1b3cb 20 20 20 69 66 28 20 4e 45 56 45 52 28 70 4d 65     if( NEVER(pMe
1b3cc 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 20 29 20 73 71  m->zMalloc) ) sq
1b3cd 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1b3ce 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 7d 0a 20  ase(pMem);.  }. 
1b3cf 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20   if( p->flags & 
1b3d0 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f 46 52  UNPACKED_NEED_FR
1b3d1 45 45 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  EE ){.    sqlite
1b3d2 33 44 62 46 72 65 65 28 70 2d 3e 70 4b 65 79 49  3DbFree(p->pKeyI
1b3d3 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 7d  nfo->db, p);.  }
1b3d4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
1b3d5 75 6e 63 74 69 6f 6e 20 63 6f 6d 70 61 72 65 73  unction compares
1b3d6 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 20 72   the two table r
1b3d7 6f 77 73 20 6f 72 20 69 6e 64 65 78 20 72 65 63  ows or index rec
1b3d8 6f 72 64 73 0a 2a 2a 20 73 70 65 63 69 66 69 65  ords.** specifie
1b3d9 64 20 62 79 20 7b 6e 4b 65 79 31 2c 20 70 4b 65  d by {nKey1, pKe
1b3da 79 31 7d 20 61 6e 64 20 70 50 4b 65 79 32 2e 20  y1} and pPKey2. 
1b3db 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 6e 65   It returns a ne
1b3dc 67 61 74 69 76 65 2c 20 7a 65 72 6f 0a 2a 2a 20  gative, zero.** 
1b3dd 6f 72 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65  or positive inte
1b3de 67 65 72 20 69 66 20 6b 65 79 31 20 69 73 20 6c  ger if key1 is l
1b3df 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20  ess than, equal 
1b3e0 74 6f 20 6f 72 20 0a 2a 2a 20 67 72 65 61 74 65  to or .** greate
1b3e1 72 20 74 68 61 6e 20 6b 65 79 32 2e 20 20 54 68  r than key2.  Th
1b3e2 65 20 7b 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d  e {nKey1, pKey1}
1b3e3 20 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20 62   key must be a b
1b3e4 6c 6f 62 0a 2a 2a 20 63 72 65 61 74 65 64 20 62  lob.** created b
1b3e5 79 20 74 68 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  y th OP_MakeReco
1b3e6 72 64 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65  rd opcode of the
1b3e7 20 56 44 42 45 2e 20 20 54 68 65 20 70 50 4b 65   VDBE.  The pPKe
1b3e8 79 32 0a 2a 2a 20 6b 65 79 20 6d 75 73 74 20 62  y2.** key must b
1b3e9 65 20 61 20 70 61 72 73 65 64 20 6b 65 79 20 73  e a parsed key s
1b3ea 75 63 68 20 61 73 20 6f 62 74 61 69 6e 65 64 20  uch as obtained 
1b3eb 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 56  from.** sqlite3V
1b3ec 64 62 65 50 61 72 73 65 52 65 63 6f 72 64 2e 0a  dbeParseRecord..
1b3ed 2a 2a 0a 2a 2a 20 4b 65 79 31 20 61 6e 64 20 4b  **.** Key1 and K
1b3ee 65 79 32 20 64 6f 20 6e 6f 74 20 68 61 76 65 20  ey2 do not have 
1b3ef 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 73  to contain the s
1b3f0 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 69  ame number of fi
1b3f1 65 6c 64 73 2e 0a 2a 2a 20 54 68 65 20 6b 65 79  elds..** The key
1b3f2 20 77 69 74 68 20 66 65 77 65 72 20 66 69 65 6c   with fewer fiel
1b3f3 64 73 20 69 73 20 75 73 75 61 6c 6c 79 20 63 6f  ds is usually co
1b3f4 6d 70 61 72 65 73 20 6c 65 73 73 20 74 68 61 6e  mpares less than
1b3f5 20 74 68 65 20 0a 2a 2a 20 6c 6f 6e 67 65 72 20   the .** longer 
1b3f6 6b 65 79 2e 20 20 48 6f 77 65 76 65 72 20 69 66  key.  However if
1b3f7 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e   the UNPACKED_IN
1b3f8 43 52 4b 45 59 20 66 6c 61 67 73 20 69 6e 20 70  CRKEY flags in p
1b3f9 50 4b 65 79 32 20 69 73 20 73 65 74 0a 2a 2a 20  PKey2 is set.** 
1b3fa 61 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70  and the common p
1b3fb 72 65 66 69 78 65 73 20 61 72 65 20 65 71 75 61  refixes are equa
1b3fc 6c 2c 20 74 68 65 6e 20 6b 65 79 31 20 69 73 20  l, then key1 is 
1b3fd 6c 65 73 73 20 74 68 61 6e 20 6b 65 79 32 2e 0a  less than key2..
1b3fe 2a 2a 20 4f 72 20 69 66 20 74 68 65 20 55 4e 50  ** Or if the UNP
1b3ff 41 43 4b 45 44 5f 4d 41 54 43 48 5f 50 52 45 46  ACKED_MATCH_PREF
1b400 49 58 20 66 6c 61 67 20 69 73 20 73 65 74 20 61  IX flag is set a
1b401 6e 64 20 74 68 65 20 70 72 65 66 69 78 65 73 20  nd the prefixes 
1b402 61 72 65 0a 2a 2a 20 65 71 75 61 6c 2c 20 74 68  are.** equal, th
1b403 65 6e 20 74 68 65 20 6b 65 79 73 20 61 72 65 20  en the keys are 
1b404 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62 65  considered to be
1b405 20 65 71 75 61 6c 20 61 6e 64 0a 2a 2a 20 74 68   equal and.** th
1b406 65 20 70 61 72 74 73 20 62 65 79 6f 6e 64 20 74  e parts beyond t
1b407 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78  he common prefix
1b408 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a   are ignored..**
1b409 0a 2a 2a 20 49 66 20 74 68 65 20 55 4e 50 41 43  .** If the UNPAC
1b40a 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44  KED_IGNORE_ROWID
1b40b 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
1b40c 65 6e 20 74 68 65 20 6c 61 73 74 20 62 79 74 65  en the last byte
1b40d 20 6f 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65   of.** the heade
1b40e 72 20 6f 66 20 70 4b 65 79 31 20 69 73 20 69 67  r of pKey1 is ig
1b40f 6e 6f 72 65 64 2e 20 20 49 74 20 69 73 20 61 73  nored.  It is as
1b410 73 75 6d 65 64 20 74 68 61 74 20 70 4b 65 79 31  sumed that pKey1
1b411 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 20   is.** an index 
1b412 6b 65 79 2c 20 61 6e 64 20 74 68 75 73 20 65 6e  key, and thus en
1b413 64 73 20 77 69 74 68 20 61 20 72 6f 77 69 64 20  ds with a rowid 
1b414 76 61 6c 75 65 2e 20 20 54 68 65 20 6c 61 73 74  value.  The last
1b415 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20   byte.** of the 
1b416 68 65 61 64 65 72 20 77 69 6c 6c 20 74 68 65 72  header will ther
1b417 65 66 6f 72 65 20 62 65 20 74 68 65 20 73 65 72  efore be the ser
1b418 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20  ial type of the 
1b419 72 6f 77 69 64 3a 0a 2a 2a 20 6f 6e 65 20 6f 66  rowid:.** one of
1b41a 20 31 2c 20 32 2c 20 33 2c 20 34 2c 20 35 2c 20   1, 2, 3, 4, 5, 
1b41b 36 2c 20 38 2c 20 6f 72 20 39 20 2d 20 74 68 65  6, 8, or 9 - the
1b41c 20 69 6e 74 65 67 65 72 20 73 65 72 69 61 6c 20   integer serial 
1b41d 74 79 70 65 73 2e 0a 2a 2a 20 54 68 65 20 73 65  types..** The se
1b41e 72 69 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65  rial type of the
1b41f 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 77 69 6c   final rowid wil
1b420 6c 20 61 6c 77 61 79 73 20 62 65 20 61 20 73 69  l always be a si
1b421 6e 67 6c 65 20 62 79 74 65 2e 0a 2a 2a 20 42 79  ngle byte..** By
1b422 20 69 67 6e 6f 72 69 6e 67 20 74 68 69 73 20 6c   ignoring this l
1b423 61 73 74 20 62 79 74 65 20 6f 66 20 74 68 65 20  ast byte of the 
1b424 68 65 61 64 65 72 2c 20 77 65 20 66 6f 72 63 65  header, we force
1b425 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 0a   the comparison.
1b426 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 65  ** to ignore the
1b427 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e   rowid at the en
1b428 64 20 6f 66 20 6b 65 79 31 2e 0a 2a 2f 0a 53 51  d of key1..*/.SQ
1b429 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1b42a 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f   sqlite3VdbeReco
1b42b 72 64 43 6f 6d 70 61 72 65 28 0a 20 20 69 6e 74  rdCompare(.  int
1b42c 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f   nKey1, const vo
1b42d 69 64 20 2a 70 4b 65 79 31 2c 20 2f 2a 20 4c 65  id *pKey1, /* Le
1b42e 66 74 20 6b 65 79 20 2a 2f 0a 20 20 55 6e 70 61  ft key */.  Unpa
1b42f 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 50 4b 65  ckedRecord *pPKe
1b430 79 32 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67  y2        /* Rig
1b431 68 74 20 6b 65 79 20 2a 2f 0a 29 7b 0a 20 20 69  ht key */.){.  i
1b432 6e 74 20 64 31 3b 20 20 20 20 20 20 20 20 20 20  nt d1;          
1b433 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
1b434 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20   aKey[] of next 
1b435 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a  data element */.
1b436 20 20 75 33 32 20 69 64 78 31 3b 20 20 20 20 20    u32 idx1;     
1b437 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
1b438 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f 66 20 6e 65  nto aKey[] of ne
1b439 78 74 20 68 65 61 64 65 72 20 65 6c 65 6d 65 6e  xt header elemen
1b43a 74 20 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72  t */.  u32 szHdr
1b43b 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  1;        /* Num
1b43c 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1b43d 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 6e 74 20  header */.  int 
1b43e 69 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 46 69  i = 0;.  int nFi
1b43f 65 6c 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  eld;.  int rc = 
1b440 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  0;.  const unsig
1b441 6e 65 64 20 63 68 61 72 20 2a 61 4b 65 79 31 20  ned char *aKey1 
1b442 3d 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  = (const unsigne
1b443 64 20 63 68 61 72 20 2a 29 70 4b 65 79 31 3b 0a  d char *)pKey1;.
1b444 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
1b445 6e 66 6f 3b 0a 20 20 4d 65 6d 20 6d 65 6d 31 3b  nfo;.  Mem mem1;
1b446 0a 0a 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 70  ..  pKeyInfo = p
1b447 50 4b 65 79 32 2d 3e 70 4b 65 79 49 6e 66 6f 3b  PKey2->pKeyInfo;
1b448 0a 20 20 6d 65 6d 31 2e 65 6e 63 20 3d 20 70 4b  .  mem1.enc = pK
1b449 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 6d  eyInfo->enc;.  m
1b44a 65 6d 31 2e 64 62 20 3d 20 70 4b 65 79 49 6e 66  em1.db = pKeyInf
1b44b 6f 2d 3e 64 62 3b 0a 20 20 2f 2a 20 6d 65 6d 31  o->db;.  /* mem1
1b44c 2e 66 6c 61 67 73 20 3d 20 30 3b 20 20 2f 2f 20  .flags = 0;  // 
1b44d 57 69 6c 6c 20 62 65 20 69 6e 69 74 69 61 6c 69  Will be initiali
1b44e 7a 65 64 20 62 79 20 73 71 6c 69 74 65 33 56 64  zed by sqlite3Vd
1b44f 62 65 53 65 72 69 61 6c 47 65 74 28 29 20 2a 2f  beSerialGet() */
1b450 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 6d 65 6d  .  VVA_ONLY( mem
1b451 31 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 20 29  1.zMalloc = 0; )
1b452 20 2f 2a 20 4f 6e 6c 79 20 6e 65 65 64 65 64 20   /* Only needed 
1b453 62 79 20 61 73 73 65 72 74 28 29 20 73 74 61 74  by assert() stat
1b454 65 6d 65 6e 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20  ements */..  /* 
1b455 43 6f 6d 70 69 6c 65 72 73 20 6d 61 79 20 63 6f  Compilers may co
1b456 6d 70 6c 61 69 6e 20 74 68 61 74 20 6d 65 6d 31  mplain that mem1
1b457 2e 75 2e 69 20 69 73 20 70 6f 74 65 6e 74 69 61  .u.i is potentia
1b458 6c 6c 79 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65  lly uninitialize
1b459 64 2e 0a 20 20 2a 2a 20 57 65 20 63 6f 75 6c 64  d..  ** We could
1b45a 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 2c 20   initialize it, 
1b45b 61 73 20 73 68 6f 77 6e 20 68 65 72 65 2c 20 74  as shown here, t
1b45c 6f 20 73 69 6c 65 6e 63 65 20 74 68 6f 73 65 20  o silence those 
1b45d 63 6f 6d 70 6c 61 69 6e 74 73 2e 0a 20 20 2a 2a  complaints..  **
1b45e 20 42 75 74 20 69 6e 20 66 61 63 74 2c 20 6d 65   But in fact, me
1b45f 6d 31 2e 75 2e 69 20 77 69 6c 6c 20 6e 65 76 65  m1.u.i will neve
1b460 72 20 61 63 74 75 61 6c 6c 79 20 62 65 20 75 73  r actually be us
1b461 65 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20  ed initialized, 
1b462 61 6e 64 20 64 6f 69 6e 67 20 0a 20 20 2a 2a 20  and doing .  ** 
1b463 74 68 65 20 75 6e 6e 65 63 65 73 73 61 72 79 20  the unnecessary 
1b464 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 68  initialization h
1b465 61 73 20 61 20 6d 65 61 73 75 72 61 62 6c 65 20  as a measurable 
1b466 6e 65 67 61 74 69 76 65 20 70 65 72 66 6f 72 6d  negative perform
1b467 61 6e 63 65 0a 20 20 2a 2a 20 69 6d 70 61 63 74  ance.  ** impact
1b468 2c 20 73 69 6e 63 65 20 74 68 69 73 20 72 6f 75  , since this rou
1b469 74 69 6e 65 20 69 73 20 61 20 76 65 72 79 20 68  tine is a very h
1b46a 69 67 68 20 72 75 6e 6e 65 72 2e 20 20 41 6e 64  igh runner.  And
1b46b 20 73 6f 2c 20 77 65 20 63 68 6f 6f 73 65 0a 20   so, we choose. 
1b46c 20 2a 2a 20 74 6f 20 69 67 6e 6f 72 65 20 74 68   ** to ignore th
1b46d 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69  e compiler warni
1b46e 6e 67 73 20 61 6e 64 20 6c 65 61 76 65 20 74 68  ngs and leave th
1b46f 69 73 20 76 61 72 69 61 62 6c 65 20 75 6e 69 6e  is variable unin
1b470 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a  itialized..  */.
1b471 20 20 2f 2a 20 20 6d 65 6d 31 2e 75 2e 69 20 3d    /*  mem1.u.i =
1b472 20 30 3b 20 20 2f 2f 20 6e 6f 74 20 6e 65 65 64   0;  // not need
1b473 65 64 2c 20 68 65 72 65 20 74 6f 20 73 69 6c 65  ed, here to sile
1b474 6e 63 65 20 63 6f 6d 70 69 6c 65 72 20 77 61 72  nce compiler war
1b475 6e 69 6e 67 20 2a 2f 0a 20 20 0a 20 20 69 64 78  ning */.  .  idx
1b476 31 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28  1 = getVarint32(
1b477 61 4b 65 79 31 2c 20 73 7a 48 64 72 31 29 3b 0a  aKey1, szHdr1);.
1b478 20 20 64 31 20 3d 20 73 7a 48 64 72 31 3b 0a 20    d1 = szHdr1;. 
1b479 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61   if( pPKey2->fla
1b47a 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 47  gs & UNPACKED_IG
1b47b 4e 4f 52 45 5f 52 4f 57 49 44 20 29 7b 0a 20 20  NORE_ROWID ){.  
1b47c 20 20 73 7a 48 64 72 31 2d 2d 3b 0a 20 20 7d 0a    szHdr1--;.  }.
1b47d 20 20 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49    nField = pKeyI
1b47e 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 77  nfo->nField;.  w
1b47f 68 69 6c 65 28 20 69 64 78 31 3c 73 7a 48 64 72  hile( idx1<szHdr
1b480 31 20 26 26 20 69 3c 70 50 4b 65 79 32 2d 3e 6e  1 && i<pPKey2->n
1b481 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 75 33 32  Field ){.    u32
1b482 20 73 65 72 69 61 6c 5f 74 79 70 65 31 3b 0a 0a   serial_type1;..
1b483 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20      /* Read the 
1b484 73 65 72 69 61 6c 20 74 79 70 65 73 20 66 6f 72  serial types for
1b485 20 74 68 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e   the next elemen
1b486 74 20 69 6e 20 65 61 63 68 20 6b 65 79 2e 20 2a  t in each key. *
1b487 2f 0a 20 20 20 20 69 64 78 31 20 2b 3d 20 67 65  /.    idx1 += ge
1b488 74 56 61 72 69 6e 74 33 32 28 20 61 4b 65 79 31  tVarint32( aKey1
1b489 2b 69 64 78 31 2c 20 73 65 72 69 61 6c 5f 74 79  +idx1, serial_ty
1b48a 70 65 31 20 29 3b 0a 20 20 20 20 69 66 28 20 64  pe1 );.    if( d
1b48b 31 3e 3d 6e 4b 65 79 31 20 26 26 20 73 71 6c 69  1>=nKey1 && sqli
1b48c 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1b48d 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65  eLen(serial_type
1b48e 31 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20  1)>0 ) break;.. 
1b48f 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68     /* Extract th
1b490 65 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 63  e values to be c
1b491 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a  ompared..    */.
1b492 20 20 20 20 64 31 20 2b 3d 20 73 71 6c 69 74 65      d1 += sqlite
1b493 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 26  3VdbeSerialGet(&
1b494 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65 72 69 61  aKey1[d1], seria
1b495 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d 31 29 3b  l_type1, &mem1);
1b496 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20  ..    /* Do the 
1b497 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 20 20 2a  comparison.    *
1b498 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
1b499 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 6d 65  e3MemCompare(&me
1b49a 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e 61 4d 65  m1, &pPKey2->aMe
1b49b 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  m[i],.          
1b49c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b49d 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70 4b 65 79   i<nField ? pKey
1b49e 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3a  Info->aColl[i] :
1b49f 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21   0);.    if( rc!
1b4a0 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  =0 ){.      asse
1b4a1 72 74 28 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f 63  rt( mem1.zMalloc
1b4a2 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 65 65 20 63  ==0 );  /* See c
1b4a3 6f 6d 6d 65 6e 74 20 62 65 6c 6f 77 20 2a 2f 0a  omment below */.
1b4a4 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 65 72 74  .      /* Invert
1b4a5 20 74 68 65 20 72 65 73 75 6c 74 20 69 66 20 77   the result if w
1b4a6 65 20 61 72 65 20 75 73 69 6e 67 20 44 45 53 43  e are using DESC
1b4a7 20 73 6f 72 74 20 6f 72 64 65 72 2e 20 2a 2f 0a   sort order. */.
1b4a8 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e        if( pKeyIn
1b4a9 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 26  fo->aSortOrder &
1b4aa 26 20 69 3c 6e 46 69 65 6c 64 20 26 26 20 70 4b  & i<nField && pK
1b4ab 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64  eyInfo->aSortOrd
1b4ac 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 20  er[i] ){.       
1b4ad 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20 20 20 20   rc = -rc;.     
1b4ae 20 7d 0a 20 20 20 20 0a 20 20 20 20 20 20 2f 2a   }.    .      /*
1b4af 20 49 66 20 74 68 65 20 50 52 45 46 49 58 5f 53   If the PREFIX_S
1b4b0 45 41 52 43 48 20 66 6c 61 67 20 69 73 20 73 65  EARCH flag is se
1b4b1 74 20 61 6e 64 20 61 6c 6c 20 66 69 65 6c 64 73  t and all fields
1b4b2 20 65 78 63 65 70 74 20 74 68 65 20 66 69 6e 61   except the fina
1b4b3 6c 0a 20 20 20 20 20 20 2a 2a 20 72 6f 77 69 64  l.      ** rowid
1b4b4 20 66 69 65 6c 64 20 77 65 72 65 20 65 71 75 61   field were equa
1b4b5 6c 2c 20 74 68 65 6e 20 63 6c 65 61 72 20 74 68  l, then clear th
1b4b6 65 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20  e PREFIX_SEARCH 
1b4b7 66 6c 61 67 20 61 6e 64 20 73 65 74 20 0a 20 20  flag and set .  
1b4b8 20 20 20 20 2a 2a 20 70 50 4b 65 79 32 2d 3e 72      ** pPKey2->r
1b4b9 6f 77 69 64 20 74 6f 20 74 68 65 20 76 61 6c 75  owid to the valu
1b4ba 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 66  e of the rowid f
1b4bb 69 65 6c 64 20 69 6e 20 28 70 4b 65 79 31 2c 20  ield in (pKey1, 
1b4bc 6e 4b 65 79 31 29 2e 0a 20 20 20 20 20 20 2a 2a  nKey1)..      **
1b4bd 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
1b4be 20 74 68 65 20 4f 50 5f 49 73 55 6e 69 71 75 65   the OP_IsUnique
1b4bf 20 6f 70 63 6f 64 65 2e 0a 20 20 20 20 20 20 2a   opcode..      *
1b4c0 2f 0a 20 20 20 20 20 20 69 66 28 20 28 70 50 4b  /.      if( (pPK
1b4c1 65 79 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50  ey2->flags & UNP
1b4c2 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41  ACKED_PREFIX_SEA
1b4c3 52 43 48 29 20 26 26 20 69 3d 3d 28 70 50 4b 65  RCH) && i==(pPKe
1b4c4 79 32 2d 3e 6e 46 69 65 6c 64 2d 31 29 20 29 7b  y2->nField-1) ){
1b4c5 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1b4c6 20 69 64 78 31 3d 3d 73 7a 48 64 72 31 20 26 26   idx1==szHdr1 &&
1b4c7 20 72 63 20 29 3b 0a 20 20 20 20 20 20 20 20 61   rc );.        a
1b4c8 73 73 65 72 74 28 20 6d 65 6d 31 2e 66 6c 61 67  ssert( mem1.flag
1b4c9 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20  s & MEM_Int );. 
1b4ca 20 20 20 20 20 20 20 70 50 4b 65 79 32 2d 3e 66         pPKey2->f
1b4cb 6c 61 67 73 20 26 3d 20 7e 55 4e 50 41 43 4b 45  lags &= ~UNPACKE
1b4cc 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48 3b  D_PREFIX_SEARCH;
1b4cd 0a 20 20 20 20 20 20 20 20 70 50 4b 65 79 32 2d  .        pPKey2-
1b4ce 3e 72 6f 77 69 64 20 3d 20 6d 65 6d 31 2e 75 2e  >rowid = mem1.u.
1b4cf 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 0a  i;.      }.    .
1b4d0 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1b4d1 0a 20 20 20 20 7d 0a 20 20 20 20 69 2b 2b 3b 0a  .    }.    i++;.
1b4d2 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d 65 6d    }..  /* No mem
1b4d3 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  ory allocation i
1b4d4 73 20 65 76 65 72 20 75 73 65 64 20 6f 6e 20 6d  s ever used on m
1b4d5 65 6d 31 2e 20 20 50 72 6f 76 65 20 74 68 69 73  em1.  Prove this
1b4d6 20 75 73 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20   using.  ** the 
1b4d7 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
1b4d8 28 29 2e 20 20 49 66 20 74 68 65 20 61 73 73 65  ().  If the asse
1b4d9 72 74 28 29 20 66 61 69 6c 73 2c 20 69 74 20 69  rt() fails, it i
1b4da 6e 64 69 63 61 74 65 73 20 61 0a 20 20 2a 2a 20  ndicates a.  ** 
1b4db 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 61 6e 64 20  memory leak and 
1b4dc 61 20 6e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73  a need to call s
1b4dd 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1b4de 65 61 73 65 28 26 6d 65 6d 31 29 2e 0a 20 20 2a  ease(&mem1)..  *
1b4df 2f 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 31  /.  assert( mem1
1b4e0 2e 7a 4d 61 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 0a  .zMalloc==0 );..
1b4e1 20 20 2f 2a 20 72 63 3d 3d 30 20 68 65 72 65 20    /* rc==0 here 
1b4e2 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f  means that one o
1b4e3 66 20 74 68 65 20 6b 65 79 73 20 72 61 6e 20 6f  f the keys ran o
1b4e4 75 74 20 6f 66 20 66 69 65 6c 64 73 20 61 6e 64  ut of fields and
1b4e5 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 65 20 66 69  .  ** all the fi
1b4e6 65 6c 64 73 20 75 70 20 74 6f 20 74 68 61 74 20  elds up to that 
1b4e7 70 6f 69 6e 74 20 77 65 72 65 20 65 71 75 61 6c  point were equal
1b4e8 2e 20 49 66 20 74 68 65 20 55 4e 50 41 43 4b 45  . If the UNPACKE
1b4e9 44 5f 49 4e 43 52 4b 45 59 0a 20 20 2a 2a 20 66  D_INCRKEY.  ** f
1b4ea 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e  lag is set, then
1b4eb 20 62 72 65 61 6b 20 74 68 65 20 74 69 65 20 62   break the tie b
1b4ec 79 20 74 72 65 61 74 69 6e 67 20 6b 65 79 32 20  y treating key2 
1b4ed 61 73 20 6c 61 72 67 65 72 2e 0a 20 20 2a 2a 20  as larger..  ** 
1b4ee 49 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50  If the UPACKED_P
1b4ef 52 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67  REFIX_MATCH flag
1b4f0 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65   is set, then ke
1b4f1 79 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70  ys with common p
1b4f2 72 65 66 69 78 65 73 0a 20 20 2a 2a 20 61 72 65  refixes.  ** are
1b4f3 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62   considered to b
1b4f4 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65 72 77  e equal.  Otherw
1b4f5 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65 72 20  ise, the longer 
1b4f6 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20 2a 2a  key is the .  **
1b4f7 20 6c 61 72 67 65 72 2e 20 20 41 73 20 69 74 20   larger.  As it 
1b4f8 68 61 70 70 65 6e 73 2c 20 74 68 65 20 70 50 4b  happens, the pPK
1b4f9 65 79 32 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  ey2 will always 
1b4fa 62 65 20 74 68 65 20 6c 6f 6e 67 65 72 0a 20 20  be the longer.  
1b4fb 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ** if there is a
1b4fc 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 20 20 2a   difference..  *
1b4fd 2f 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  /.  assert( rc==
1b4fe 30 20 29 3b 0a 20 20 69 66 28 20 70 50 4b 65 79  0 );.  if( pPKey
1b4ff 32 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43  2->flags & UNPAC
1b500 4b 45 44 5f 49 4e 43 52 4b 45 59 20 29 7b 0a 20  KED_INCRKEY ){. 
1b501 20 20 20 72 63 20 3d 20 2d 31 3b 0a 20 20 7d 65     rc = -1;.  }e
1b502 6c 73 65 20 69 66 28 20 70 50 4b 65 79 32 2d 3e  lse if( pPKey2->
1b503 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44  flags & UNPACKED
1b504 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 29 7b  _PREFIX_MATCH ){
1b505 0a 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 72 63  .    /* Leave rc
1b506 3d 3d 30 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69  ==0 */.  }else i
1b507 66 28 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29  f( idx1<szHdr1 )
1b508 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20  {.    rc = 1;.  
1b509 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1b50a 0a 20 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 72 20 70  . ../*.** pCur p
1b50b 6f 69 6e 74 73 20 61 74 20 61 6e 20 69 6e 64 65  oints at an inde
1b50c 78 20 65 6e 74 72 79 20 63 72 65 61 74 65 64 20  x entry created 
1b50d 75 73 69 6e 67 20 74 68 65 20 4f 50 5f 4d 61 6b  using the OP_Mak
1b50e 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 0a  eRecord opcode..
1b50f 2a 2a 20 52 65 61 64 20 74 68 65 20 72 6f 77 69  ** Read the rowi
1b510 64 20 28 74 68 65 20 6c 61 73 74 20 66 69 65 6c  d (the last fiel
1b511 64 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 29  d in the record)
1b512 20 61 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e   and store it in
1b513 20 2a 72 6f 77 69 64 2e 0a 2a 2a 20 52 65 74 75   *rowid..** Retu
1b514 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
1b515 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73  everything works
1b516 2c 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f  , or an error co
1b517 64 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a  de otherwise..**
1b518 0a 2a 2a 20 70 43 75 72 20 6d 69 67 68 74 20 62  .** pCur might b
1b519 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 65  e pointing to te
1b51a 78 74 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  xt obtained from
1b51b 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62   a corrupt datab
1b51c 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 53 6f 20  ase file..** So 
1b51d 74 68 65 20 63 6f 6e 74 65 6e 74 20 63 61 6e 6e  the content cann
1b51e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 20 20  ot be trusted.  
1b51f 44 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20 63  Do appropriate c
1b520 68 65 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 6e  hecks on the con
1b521 74 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tent..*/.SQLITE_
1b522 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1b523 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64 28  te3VdbeIdxRowid(
1b524 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 43  sqlite3 *db, BtC
1b525 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34  ursor *pCur, i64
1b526 20 2a 72 6f 77 69 64 29 7b 0a 20 20 69 36 34 20   *rowid){.  i64 
1b527 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20  nCellKey = 0;.  
1b528 69 6e 74 20 72 63 3b 0a 20 20 75 33 32 20 73 7a  int rc;.  u32 sz
1b529 48 64 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  Hdr;        /* S
1b52a 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64 65  ize of the heade
1b52b 72 20 2a 2f 0a 20 20 75 33 32 20 74 79 70 65 52  r */.  u32 typeR
1b52c 6f 77 69 64 3b 20 20 20 20 2f 2a 20 53 65 72 69  owid;    /* Seri
1b52d 61 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 72  al type of the r
1b52e 6f 77 69 64 20 2a 2f 0a 20 20 75 33 32 20 6c 65  owid */.  u32 le
1b52f 6e 52 6f 77 69 64 3b 20 20 20 20 20 2f 2a 20 53  nRowid;     /* S
1b530 69 7a 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64  ize of the rowid
1b531 20 2a 2f 0a 20 20 4d 65 6d 20 6d 2c 20 76 3b 0a   */.  Mem m, v;.
1b532 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1b533 54 45 52 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 47  TER(db);..  /* G
1b534 65 74 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  et the size of t
1b535 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 2e 20  he index entry. 
1b536 20 4f 6e 6c 79 20 69 6e 64 69 63 65 73 20 65 6e   Only indices en
1b537 74 72 69 65 73 20 6f 66 20 6c 65 73 73 0a 20 20  tries of less.  
1b538 2a 2a 20 74 68 61 6e 20 32 47 69 42 20 61 72 65  ** than 2GiB are
1b539 20 73 75 70 70 6f 72 74 20 2d 20 61 6e 79 74 68   support - anyth
1b53a 69 6e 67 20 6c 61 72 67 65 20 6d 75 73 74 20 62  ing large must b
1b53b 65 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  e database corru
1b53c 70 74 69 6f 6e 2e 0a 20 20 2a 2a 20 41 6e 79 20  ption..  ** Any 
1b53d 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 64 65  corruption is de
1b53e 74 65 63 74 65 64 20 69 6e 20 73 71 6c 69 74 65  tected in sqlite
1b53f 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
1b540 74 72 28 29 2c 20 74 68 6f 75 67 68 2c 20 73 6f  tr(), though, so
1b541 0a 20 20 2a 2a 20 74 68 69 73 20 63 6f 64 65 20  .  ** this code 
1b542 63 61 6e 20 73 61 66 65 6c 79 20 61 73 73 75 6d  can safely assum
1b543 65 20 74 68 61 74 20 6e 43 65 6c 6c 4b 65 79 20  e that nCellKey 
1b544 69 73 20 33 32 2d 62 69 74 73 20 20 0a 20 20 2a  is 32-bits  .  *
1b545 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  /.  assert( sqli
1b546 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73  te3BtreeCursorIs
1b547 56 61 6c 69 64 28 70 43 75 72 29 20 29 3b 0a 20  Valid(pCur) );. 
1b548 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1b549 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20  eeKeySize(pCur, 
1b54a 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 61 73  &nCellKey);.  as
1b54b 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
1b54c 5f 4f 4b 20 29 3b 20 20 20 20 20 2f 2a 20 70 43  _OK );     /* pC
1b54d 75 72 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c  ur is always val
1b54e 69 64 20 73 6f 20 4b 65 79 53 69 7a 65 20 63 61  id so KeySize ca
1b54f 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 61  nnot fail */.  a
1b550 73 73 65 72 74 28 20 28 6e 43 65 6c 6c 4b 65 79  ssert( (nCellKey
1b551 20 26 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33   & SQLITE_MAX_U3
1b552 32 29 3d 3d 28 75 36 34 29 6e 43 65 6c 6c 4b 65  2)==(u64)nCellKe
1b553 79 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20  y );..  /* Read 
1b554 69 6e 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20  in the complete 
1b555 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 69  content of the i
1b556 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f 0a 20 20  ndex entry */.  
1b557 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69  memset(&m, 0, si
1b558 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72 63 20 3d  zeof(m));.  rc =
1b559 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
1b55a 72 6f 6d 42 74 72 65 65 28 70 43 75 72 2c 20 30  romBtree(pCur, 0
1b55b 2c 20 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c  , (int)nCellKey,
1b55c 20 31 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20 72   1, &m);.  if( r
1b55d 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
1b55e 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  rc;.  }..  /* Th
1b55f 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20 6d 75  e index entry mu
1b560 73 74 20 62 65 67 69 6e 20 77 69 74 68 20 61 20  st begin with a 
1b561 68 65 61 64 65 72 20 73 69 7a 65 20 2a 2f 0a 20  header size */. 
1b562 20 28 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74   (void)getVarint
1b563 33 32 28 28 75 38 2a 29 6d 2e 7a 2c 20 73 7a 48  32((u8*)m.z, szH
1b564 64 72 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  dr);.  testcase(
1b565 20 73 7a 48 64 72 3d 3d 33 20 29 3b 0a 20 20 74   szHdr==3 );.  t
1b566 65 73 74 63 61 73 65 28 20 73 7a 48 64 72 3d 3d  estcase( szHdr==
1b567 6d 2e 6e 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c  m.n );.  if( unl
1b568 69 6b 65 6c 79 28 73 7a 48 64 72 3c 33 20 7c 7c  ikely(szHdr<3 ||
1b569 20 28 69 6e 74 29 73 7a 48 64 72 3e 6d 2e 6e 29   (int)szHdr>m.n)
1b56a 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64 78   ){.    goto idx
1b56b 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f  _rowid_corruptio
1b56c 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65  n;.  }..  /* The
1b56d 20 6c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74   last field of t
1b56e 68 65 20 69 6e 64 65 78 20 73 68 6f 75 6c 64 20  he index should 
1b56f 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20 2d 20  be an integer - 
1b570 74 68 65 20 52 4f 57 49 44 2e 0a 20 20 2a 2a 20  the ROWID..  ** 
1b571 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
1b572 6c 61 73 74 20 65 6e 74 72 79 20 72 65 61 6c 6c  last entry reall
1b573 79 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2e  y is an integer.
1b574 20 2a 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56   */.  (void)getV
1b575 61 72 69 6e 74 33 32 28 28 75 38 2a 29 26 6d 2e  arint32((u8*)&m.
1b576 7a 5b 73 7a 48 64 72 2d 31 5d 2c 20 74 79 70 65  z[szHdr-1], type
1b577 52 6f 77 69 64 29 3b 0a 20 20 74 65 73 74 63 61  Rowid);.  testca
1b578 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 31  se( typeRowid==1
1b579 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
1b57a 74 79 70 65 52 6f 77 69 64 3d 3d 32 20 29 3b 0a  typeRowid==2 );.
1b57b 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65    testcase( type
1b57c 52 6f 77 69 64 3d 3d 33 20 29 3b 0a 20 20 74 65  Rowid==3 );.  te
1b57d 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69  stcase( typeRowi
1b57e 64 3d 3d 34 20 29 3b 0a 20 20 74 65 73 74 63 61  d==4 );.  testca
1b57f 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 35  se( typeRowid==5
1b580 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
1b581 74 79 70 65 52 6f 77 69 64 3d 3d 36 20 29 3b 0a  typeRowid==6 );.
1b582 20 20 74 65 73 74 63 61 73 65 28 20 74 79 70 65    testcase( type
1b583 52 6f 77 69 64 3d 3d 38 20 29 3b 0a 20 20 74 65  Rowid==8 );.  te
1b584 73 74 63 61 73 65 28 20 74 79 70 65 52 6f 77 69  stcase( typeRowi
1b585 64 3d 3d 39 20 29 3b 0a 20 20 69 66 28 20 75 6e  d==9 );.  if( un
1b586 6c 69 6b 65 6c 79 28 74 79 70 65 52 6f 77 69 64  likely(typeRowid
1b587 3c 31 20 7c 7c 20 74 79 70 65 52 6f 77 69 64 3e  <1 || typeRowid>
1b588 39 20 7c 7c 20 74 79 70 65 52 6f 77 69 64 3d 3d  9 || typeRowid==
1b589 37 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69  7) ){.    goto i
1b58a 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74  dx_rowid_corrupt
1b58b 69 6f 6e 3b 0a 20 20 7d 0a 20 20 6c 65 6e 52 6f  ion;.  }.  lenRo
1b58c 77 69 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62  wid = sqlite3Vdb
1b58d 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74  eSerialTypeLen(t
1b58e 79 70 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 73  ypeRowid);.  tes
1b58f 74 63 61 73 65 28 20 28 75 33 32 29 6d 2e 6e 3d  tcase( (u32)m.n=
1b590 3d 73 7a 48 64 72 2b 6c 65 6e 52 6f 77 69 64 20  =szHdr+lenRowid 
1b591 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c  );.  if( unlikel
1b592 79 28 28 75 33 32 29 6d 2e 6e 3c 73 7a 48 64 72  y((u32)m.n<szHdr
1b593 2b 6c 65 6e 52 6f 77 69 64 29 20 29 7b 0a 20 20  +lenRowid) ){.  
1b594 20 20 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64    goto idx_rowid
1b595 5f 63 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d  _corruption;.  }
1b596 0a 0a 20 20 2f 2a 20 46 65 74 63 68 20 74 68 65  ..  /* Fetch the
1b597 20 69 6e 74 65 67 65 72 20 6f 66 66 20 74 68 65   integer off the
1b598 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65   end of the inde
1b599 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 71  x record */.  sq
1b59a 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 47  lite3VdbeSerialG
1b59b 65 74 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e  et((u8*)&m.z[m.n
1b59c 2d 6c 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70 65  -lenRowid], type
1b59d 52 6f 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a 72  Rowid, &v);.  *r
1b59e 6f 77 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20 20  owid = v.u.i;.  
1b59f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1b5a0 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74  lease(&m);.  ret
1b5a1 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  urn SQLITE_OK;..
1b5a2 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
1b5a3 66 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  f database corru
1b5a4 70 74 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65  ption is detecte
1b5a5 64 20 61 66 74 65 72 20 6d 20 68 61 73 20 62 65  d after m has be
1b5a6 65 6e 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65  en.  ** allocate
1b5a7 64 2e 20 20 46 72 65 65 20 74 68 65 20 6d 20 6f  d.  Free the m o
1b5a8 62 6a 65 63 74 20 61 6e 64 20 72 65 74 75 72 6e  bject and return
1b5a9 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e   SQLITE_CORRUPT.
1b5aa 20 2a 2f 0a 69 64 78 5f 72 6f 77 69 64 5f 63 6f   */.idx_rowid_co
1b5ab 72 72 75 70 74 69 6f 6e 3a 0a 20 20 74 65 73 74  rruption:.  test
1b5ac 63 61 73 65 28 20 6d 2e 7a 4d 61 6c 6c 6f 63 21  case( m.zMalloc!
1b5ad 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  =0 );.  sqlite3V
1b5ae 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d  dbeMemRelease(&m
1b5af 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
1b5b0 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
1b5b1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72  .}../*.** Compar
1b5b2 65 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65  e the key of the
1b5b3 20 69 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61   index entry tha
1b5b4 74 20 63 75 72 73 6f 72 20 70 43 20 69 73 20 70  t cursor pC is p
1b5b5 6f 69 6e 74 69 6e 67 20 74 6f 20 61 67 61 69 6e  ointing to again
1b5b6 73 74 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73 74  st.** the key st
1b5b7 72 69 6e 67 20 69 6e 20 70 55 6e 70 61 63 6b 65  ring in pUnpacke
1b5b8 64 2e 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a  d.  Write into *
1b5b9 70 52 65 73 20 61 20 6e 75 6d 62 65 72 0a 2a 2a  pRes a number.**
1b5ba 20 74 68 61 74 20 69 73 20 6e 65 67 61 74 69 76   that is negativ
1b5bb 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69  e, zero, or posi
1b5bc 74 69 76 65 20 69 66 20 70 43 20 69 73 20 6c 65  tive if pC is le
1b5bd 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74  ss than, equal t
1b5be 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 72  o,.** or greater
1b5bf 20 74 68 61 6e 20 70 55 6e 70 61 63 6b 65 64 2e   than pUnpacked.
1b5c0 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
1b5c1 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a  OK on success..*
1b5c2 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65 64 20 69  *.** pUnpacked i
1b5c3 73 20 65 69 74 68 65 72 20 63 72 65 61 74 65 64  s either created
1b5c4 20 77 69 74 68 6f 75 74 20 61 20 72 6f 77 69 64   without a rowid
1b5c5 20 6f 72 20 69 73 20 74 72 75 6e 63 61 74 65 64   or is truncated
1b5c6 20 73 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f   so that it.** o
1b5c7 6d 69 74 73 20 74 68 65 20 72 6f 77 69 64 20 61  mits the rowid a
1b5c8 74 20 74 68 65 20 65 6e 64 2e 20 20 54 68 65 20  t the end.  The 
1b5c9 72 6f 77 69 64 20 61 74 20 74 68 65 20 65 6e 64  rowid at the end
1b5ca 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e   of the index en
1b5cb 74 72 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65  try.** is ignore
1b5cc 64 20 61 73 20 77 65 6c 6c 2e 20 20 48 65 6e 63  d as well.  Henc
1b5cd 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  e, this routine 
1b5ce 6f 6e 6c 79 20 63 6f 6d 70 61 72 65 73 20 74 68  only compares th
1b5cf 65 20 70 72 65 66 69 78 65 73 20 0a 2a 2a 20 6f  e prefixes .** o
1b5d0 66 20 74 68 65 20 6b 65 79 73 20 70 72 69 6f 72  f the keys prior
1b5d1 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f   to the final ro
1b5d2 77 69 64 2c 20 6e 6f 74 20 74 68 65 20 65 6e 74  wid, not the ent
1b5d3 69 72 65 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49  ire key..*/.SQLI
1b5d4 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1b5d5 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79  qlite3VdbeIdxKey
1b5d6 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 62 65 43  Compare(.  VdbeC
1b5d7 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20 20  ursor *pC,      
1b5d8 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75         /* The cu
1b5d9 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65 20  rsor to compare 
1b5da 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e 70  against */.  Unp
1b5db 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e  ackedRecord *pUn
1b5dc 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70 61  packed,  /* Unpa
1b5dd 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  cked version of 
1b5de 6b 65 79 20 74 6f 20 63 6f 6d 70 61 72 65 20 61  key to compare a
1b5df 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20  gainst */.  int 
1b5e0 2a 72 65 73 20 20 20 20 20 20 20 20 20 20 20 20  *res            
1b5e1 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
1b5e2 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
1b5e3 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 29  result here */.)
1b5e4 7b 0a 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79  {.  i64 nCellKey
1b5e5 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a   = 0;.  int rc;.
1b5e6 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
1b5e7 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a   = pC->pCursor;.
1b5e8 20 20 4d 65 6d 20 6d 3b 0a 0a 20 20 61 73 73 65    Mem m;..  asse
1b5e9 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
1b5ea 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 70 43  CursorIsValid(pC
1b5eb 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ur) );.  rc = sq
1b5ec 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a  lite3BtreeKeySiz
1b5ed 65 28 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65  e(pCur, &nCellKe
1b5ee 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  y);.  assert( rc
1b5ef 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20  ==SQLITE_OK );  
1b5f0 20 20 2f 2a 20 70 43 75 72 20 69 73 20 61 6c 77    /* pCur is alw
1b5f1 61 79 73 20 76 61 6c 69 64 20 73 6f 20 4b 65 79  ays valid so Key
1b5f2 53 69 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c  Size cannot fail
1b5f3 20 2a 2f 0a 20 20 2f 2a 20 6e 43 65 6c 6c 4b 65   */.  /* nCellKe
1b5f4 79 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65  y will always be
1b5f5 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 30   between 0 and 0
1b5f6 78 66 66 66 66 66 66 66 66 20 62 65 63 61 75 73  xffffffff becaus
1b5f7 65 20 6f 66 20 74 68 65 20 73 61 79 0a 20 20 2a  e of the say.  *
1b5f8 2a 20 74 68 61 74 20 62 74 72 65 65 50 61 72 73  * that btreePars
1b5f9 65 43 65 6c 6c 50 74 72 28 29 20 61 6e 64 20 73  eCellPtr() and s
1b5fa 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33  qlite3GetVarint3
1b5fb 32 28 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e  2() are implemen
1b5fc 74 65 64 20 2a 2f 0a 20 20 69 66 28 20 6e 43 65  ted */.  if( nCe
1b5fd 6c 6c 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65 6c  llKey<=0 || nCel
1b5fe 6c 4b 65 79 3e 30 78 37 66 66 66 66 66 66 66 20  lKey>0x7fffffff 
1b5ff 29 7b 0a 20 20 20 20 2a 72 65 73 20 3d 20 30 3b  ){.    *res = 0;
1b600 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1b601 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20 20 7d 0a  TE_CORRUPT;.  }.
1b602 20 20 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20    memset(&m, 0, 
1b603 73 69 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72 63  sizeof(m));.  rc
1b604 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
1b605 6d 46 72 6f 6d 42 74 72 65 65 28 70 43 2d 3e 70  mFromBtree(pC->p
1b606 43 75 72 73 6f 72 2c 20 30 2c 20 28 69 6e 74 29  Cursor, 0, (int)
1b607 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29  nCellKey, 1, &m)
1b608 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20  ;.  if( rc ){.  
1b609 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
1b60a 0a 20 20 61 73 73 65 72 74 28 20 70 55 6e 70 61  .  assert( pUnpa
1b60b 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 20 55 4e  cked->flags & UN
1b60c 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f  PACKED_IGNORE_RO
1b60d 57 49 44 20 29 3b 0a 20 20 2a 72 65 73 20 3d 20  WID );.  *res = 
1b60e 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
1b60f 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d 2e  dCompare(m.n, m.
1b610 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 3b 0a 20  z, pUnpacked);. 
1b611 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1b612 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65  elease(&m);.  re
1b613 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1b614 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
1b615 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 76  utine sets the v
1b616 61 6c 75 65 20 74 6f 20 62 65 20 72 65 74 75 72  alue to be retur
1b617 6e 65 64 20 62 79 20 73 75 62 73 65 71 75 65 6e  ned by subsequen
1b618 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71  t calls to.** sq
1b619 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20  lite3_changes() 
1b61a 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
1b61b 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 0a 2a 2f  handle 'db'. .*/
1b61c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1b61d 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1b61e 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c 69 74  SetChanges(sqlit
1b61f 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 43 68 61  e3 *db, int nCha
1b620 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  nge){.  assert( 
1b621 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b622 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
1b623 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d  .  db->nChange =
1b624 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e   nChange;.  db->
1b625 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2b 3d 20  nTotalChange += 
1b626 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  nChange;.}../*.*
1b627 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69 6e 20  * Set a flag in 
1b628 74 68 65 20 76 64 62 65 20 74 6f 20 75 70 64 61  the vdbe to upda
1b629 74 65 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f  te the change co
1b62a 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20 69 73  unter when it is
1b62b 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f 72   finalised.** or
1b62c 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54   reset..*/.SQLIT
1b62d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1b62e 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43  qlite3VdbeCountC
1b62f 68 61 6e 67 65 73 28 56 64 62 65 20 2a 76 29 7b  hanges(Vdbe *v){
1b630 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 6e 74 4f  .  v->changeCntO
1b631 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n = 1;.}../*.** 
1b632 4d 61 72 6b 20 65 76 65 72 79 20 70 72 65 70 61  Mark every prepa
1b633 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61 73  red statement as
1b634 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
1b635 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1b636 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 69 72 65  ion.** as expire
1b637 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 69  d..**.** An expi
1b638 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 65  red statement me
1b639 61 6e 73 20 74 68 61 74 20 72 65 63 6f 6d 70 69  ans that recompi
1b63a 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74  lation of the st
1b63b 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 72 65  atement is.** re
1b63c 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 74 65 6d  commend.  Statem
1b63d 65 6e 74 73 20 65 78 70 69 72 65 20 77 68 65 6e  ents expire when
1b63e 20 74 68 69 6e 67 73 20 68 61 70 70 65 6e 20 74   things happen t
1b63f 68 61 74 20 6d 61 6b 65 20 74 68 65 69 72 0a 2a  hat make their.*
1b640 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f 6c  * programs obsol
1b641 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20 75  ete.  Removing u
1b642 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ser-defined func
1b643 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 69  tions or collati
1b644 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 2c  ng.** sequences,
1b645 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 61 6e 20   or changing an 
1b646 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75  authorization fu
1b647 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74  nction are the t
1b648 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 69 6e 67  ypes of.** thing
1b649 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72 65 70  s that make prep
1b64a 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
1b64b 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51 4c  obsolete..*/.SQL
1b64c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1b64d 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72   sqlite3ExpirePr
1b64e 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73  eparedStatements
1b64f 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
1b650 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28   Vdbe *p;.  for(
1b651 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b 20 70  p = db->pVdbe; p
1b652 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20  ; p=p->pNext){. 
1b653 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20     p->expired = 
1b654 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  1;.  }.}../*.** 
1b655 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 62  Return the datab
1b656 61 73 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ase associated w
1b657 69 74 68 20 74 68 65 20 56 64 62 65 2e 0a 2a 2f  ith the Vdbe..*/
1b658 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1b659 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33  sqlite3 *sqlite3
1b65a 56 64 62 65 44 62 28 56 64 62 65 20 2a 76 29 7b  VdbeDb(Vdbe *v){
1b65b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e 64 62 3b  .  return v->db;
1b65c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1b65d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
1b65e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 73   sqlite3_value s
1b65f 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
1b660 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 62 6f  ing the value bo
1b661 75 6e 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  und.** parameter
1b662 20 69 56 61 72 20 6f 66 20 56 4d 20 76 2e 20 45   iVar of VM v. E
1b663 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 76 61  xcept, if the va
1b664 6c 75 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55  lue is an SQL NU
1b665 4c 4c 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 30  LL, return .** 0
1b666 20 69 6e 73 74 65 61 64 2e 20 55 6e 6c 65 73 73   instead. Unless
1b667 20 69 74 20 69 73 20 4e 55 4c 4c 2c 20 61 70 70   it is NULL, app
1b668 6c 79 20 61 66 66 69 6e 69 74 79 20 61 66 66 20  ly affinity aff 
1b669 28 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49  (one of the SQLI
1b66a 54 45 5f 41 46 46 5f 2a 0a 2a 2a 20 63 6f 6e 73  TE_AFF_*.** cons
1b66b 74 61 6e 74 73 29 20 74 6f 20 74 68 65 20 76 61  tants) to the va
1b66c 6c 75 65 20 62 65 66 6f 72 65 20 72 65 74 75 72  lue before retur
1b66d 6e 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54  ning it..**.** T
1b66e 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  he returned valu
1b66f 65 20 6d 75 73 74 20 62 65 20 66 72 65 65 64 20  e must be freed 
1b670 62 79 20 74 68 65 20 63 61 6c 6c 65 72 20 75 73  by the caller us
1b671 69 6e 67 20 73 71 6c 69 74 65 33 56 61 6c 75 65  ing sqlite3Value
1b672 46 72 65 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  Free()..*/.SQLIT
1b673 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65  E_PRIVATE sqlite
1b674 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33  3_value *sqlite3
1b675 56 64 62 65 47 65 74 56 61 6c 75 65 28 56 64 62  VdbeGetValue(Vdb
1b676 65 20 2a 76 2c 20 69 6e 74 20 69 56 61 72 2c 20  e *v, int iVar, 
1b677 75 38 20 61 66 66 29 7b 0a 20 20 61 73 73 65 72  u8 aff){.  asser
1b678 74 28 20 69 56 61 72 3e 30 20 29 3b 0a 20 20 69  t( iVar>0 );.  i
1b679 66 28 20 76 20 29 7b 0a 20 20 20 20 4d 65 6d 20  f( v ){.    Mem 
1b67a 2a 70 4d 65 6d 20 3d 20 26 76 2d 3e 61 56 61 72  *pMem = &v->aVar
1b67b 5b 69 56 61 72 2d 31 5d 3b 0a 20 20 20 20 69 66  [iVar-1];.    if
1b67c 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67  ( 0==(pMem->flag
1b67d 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 20 29 7b  s & MEM_Null) ){
1b67e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76  .      sqlite3_v
1b67f 61 6c 75 65 20 2a 70 52 65 74 20 3d 20 73 71 6c  alue *pRet = sql
1b680 69 74 65 33 56 61 6c 75 65 4e 65 77 28 76 2d 3e  ite3ValueNew(v->
1b681 64 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  db);.      if( p
1b682 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Ret ){.        s
1b683 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70  qlite3VdbeMemCop
1b684 79 28 28 4d 65 6d 20 2a 29 70 52 65 74 2c 20 70  y((Mem *)pRet, p
1b685 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71  Mem);.        sq
1b686 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41  lite3ValueApplyA
1b687 66 66 69 6e 69 74 79 28 70 52 65 74 2c 20 61 66  ffinity(pRet, af
1b688 66 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  f, SQLITE_UTF8);
1b689 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1b68a 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65  VdbeMemStoreType
1b68b 28 28 4d 65 6d 20 2a 29 70 52 65 74 29 3b 0a 20  ((Mem *)pRet);. 
1b68c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
1b68d 75 72 6e 20 70 52 65 74 3b 0a 20 20 20 20 7d 0a  urn pRet;.    }.
1b68e 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
1b68f 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75  }../*.** Configu
1b690 72 65 20 53 51 4c 20 76 61 72 69 61 62 6c 65 20  re SQL variable 
1b691 69 56 61 72 20 73 6f 20 74 68 61 74 20 62 69 6e  iVar so that bin
1b692 64 69 6e 67 20 61 20 6e 65 77 20 76 61 6c 75 65  ding a new value
1b693 20 74 6f 20 69 74 20 73 69 67 6e 61 6c 73 0a 2a   to it signals.*
1b694 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 6f  * to sqlite3_reo
1b695 70 74 69 6d 69 7a 65 28 29 20 74 68 61 74 20 72  ptimize() that r
1b696 65 2d 70 72 65 70 61 72 69 6e 67 20 74 68 65 20  e-preparing the 
1b697 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 72 65  statement may re
1b698 73 75 6c 74 0a 2a 2a 20 69 6e 20 61 20 62 65 74  sult.** in a bet
1b699 74 65 72 20 71 75 65 72 79 20 70 6c 61 6e 2e 0a  ter query plan..
1b69a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1b69b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1b69c 62 65 53 65 74 56 61 72 6d 61 73 6b 28 56 64 62  beSetVarmask(Vdb
1b69d 65 20 2a 76 2c 20 69 6e 74 20 69 56 61 72 29 7b  e *v, int iVar){
1b69e 0a 20 20 61 73 73 65 72 74 28 20 69 56 61 72 3e  .  assert( iVar>
1b69f 30 20 29 3b 0a 20 20 69 66 28 20 69 56 61 72 3e  0 );.  if( iVar>
1b6a0 33 32 20 29 7b 0a 20 20 20 20 76 2d 3e 65 78 70  32 ){.    v->exp
1b6a1 6d 61 73 6b 20 3d 20 30 78 66 66 66 66 66 66 66  mask = 0xfffffff
1b6a2 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  f;.  }else{.    
1b6a3 76 2d 3e 65 78 70 6d 61 73 6b 20 7c 3d 20 28 28  v->expmask |= ((
1b6a4 75 33 32 29 31 20 3c 3c 20 28 69 56 61 72 2d 31  u32)1 << (iVar-1
1b6a5 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a  ));.  }.}../****
1b6a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1b6a7 66 20 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a  f vdbeaux.c ****
1b6a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1b6ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
1b6ac 20 66 69 6c 65 20 76 64 62 65 61 70 69 2e 63 20   file vdbeapi.c 
1b6ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
1b6b0 20 32 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a   2004 May 26.**.
1b6b1 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1b6b2 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1b6b3 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1b6b4 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1b6b5 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1b6b6 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1b6b7 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1b6b8 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1b6b9 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1b6ba 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1b6bb 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1b6bc 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1b6bd 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1b6be 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1b6bf 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1b6c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1b6c1 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1b6c2 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1b6c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
1b6c7 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1b6c8 61 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f  ains code use to
1b6c9 20 69 6d 70 6c 65 6d 65 6e 74 20 41 50 49 73 20   implement APIs 
1b6ca 74 68 61 74 20 61 72 65 20 70 61 72 74 20 6f 66  that are part of
1b6cb 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 0a 2a 2f   the.** VDBE..*/
1b6cc 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1b6cd 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
1b6ce 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
1b6cf 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 6f 66  UE (non-zero) of
1b6d0 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73   the statement s
1b6d1 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72  upplied as an ar
1b6d2 67 75 6d 65 6e 74 20 6e 65 65 64 73 0a 2a 2a 20  gument needs.** 
1b6d3 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64  to be recompiled
1b6d4 2e 20 20 41 20 73 74 61 74 65 6d 65 6e 74 20 6e  .  A statement n
1b6d5 65 65 64 73 20 74 6f 20 62 65 20 72 65 63 6f 6d  eeds to be recom
1b6d6 70 69 6c 65 64 20 77 68 65 6e 65 76 65 72 20 74  piled whenever t
1b6d7 68 65 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20  he.** execution 
1b6d8 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e  environment chan
1b6d9 67 65 73 20 69 6e 20 61 20 77 61 79 20 74 68 61  ges in a way tha
1b6da 74 20 77 6f 75 6c 64 20 61 6c 74 65 72 20 74 68  t would alter th
1b6db 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 74 68 61  e program.** tha
1b6dc 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
1b6dd 65 28 29 20 67 65 6e 65 72 61 74 65 73 2e 20 20  e() generates.  
1b6de 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
1b6df 6e 65 77 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72  new functions or
1b6e0 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  .** collating se
1b6e1 71 75 65 6e 63 65 73 20 61 72 65 20 72 65 67 69  quences are regi
1b6e2 73 74 65 72 65 64 20 6f 72 20 69 66 20 61 6e 20  stered or if an 
1b6e3 61 75 74 68 6f 72 69 7a 65 72 20 66 75 6e 63 74  authorizer funct
1b6e4 69 6f 6e 20 69 73 0a 2a 2a 20 61 64 64 65 64 20  ion is.** added 
1b6e5 6f 72 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53  or changed..*/.S
1b6e6 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1b6e7 6c 69 74 65 33 5f 65 78 70 69 72 65 64 28 73 71  lite3_expired(sq
1b6e8 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1b6e9 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  t){.  Vdbe *p = 
1b6ea 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20  (Vdbe*)pStmt;.  
1b6eb 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70  return p==0 || p
1b6ec 2d 3e 65 78 70 69 72 65 64 3b 0a 7d 0a 23 65 6e  ->expired;.}.#en
1b6ed 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
1b6ee 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
1b6ef 20 64 65 73 74 72 6f 79 73 20 61 20 76 69 72 74   destroys a virt
1b6f0 75 61 6c 20 6d 61 63 68 69 6e 65 20 74 68 61 74  ual machine that
1b6f1 20 69 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a   is created by.*
1b6f2 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  * the sqlite3_co
1b6f3 6d 70 69 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e  mpile() routine.
1b6f4 20 54 68 65 20 69 6e 74 65 67 65 72 20 72 65 74   The integer ret
1b6f5 75 72 6e 65 64 20 69 73 20 61 6e 20 53 51 4c 49  urned is an SQLI
1b6f6 54 45 5f 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66  TE_.** success/f
1b6f7 61 69 6c 75 72 65 20 63 6f 64 65 20 74 68 61 74  ailure code that
1b6f8 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 72   describes the r
1b6f9 65 73 75 6c 74 20 6f 66 20 65 78 65 63 75 74 69  esult of executi
1b6fa 6e 67 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a  ng the virtual.*
1b6fb 2a 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a  * machine..**.**
1b6fc 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
1b6fd 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ts the error cod
1b6fe 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74  e and string ret
1b6ff 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69  urned by.** sqli
1b700 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73  te3_errcode(), s
1b701 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20  qlite3_errmsg() 
1b702 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  and sqlite3_errm
1b703 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  sg16()..*/.SQLIT
1b704 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1b705 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  3_finalize(sqlit
1b706 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1b707 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28  .  int rc;.  if(
1b708 20 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20   pStmt==0 ){.   
1b709 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1b70a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64  .  }else{.    Vd
1b70b 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a 29 70  be *v = (Vdbe*)p
1b70c 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Stmt;.    sqlite
1b70d 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b 0a 23  3 *db = v->db;.#
1b70e 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  if SQLITE_THREAD
1b70f 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65 33  SAFE.    sqlite3
1b710 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20  _mutex *mutex = 
1b711 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23 65  v->db->mutex;.#e
1b712 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74 65 33  ndif.    sqlite3
1b713 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74  _mutex_enter(mut
1b714 65 78 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  ex);.    rc = sq
1b715 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a  lite3VdbeFinaliz
1b716 65 28 76 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  e(v);.    rc = s
1b717 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
1b718 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74  , rc);.    sqlit
1b719 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
1b71a 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74  utex);.  }.  ret
1b71b 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1b71c 20 54 65 72 6d 69 6e 61 74 65 20 74 68 65 20 63   Terminate the c
1b71d 75 72 72 65 6e 74 20 65 78 65 63 75 74 69 6f 6e  urrent execution
1b71e 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65   of an SQL state
1b71f 6d 65 6e 74 20 61 6e 64 20 72 65 73 65 74 20 69  ment and reset i
1b720 74 0a 2a 2a 20 62 61 63 6b 20 74 6f 20 69 74 73  t.** back to its
1b721 20 73 74 61 72 74 69 6e 67 20 73 74 61 74 65 20   starting state 
1b722 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62  so that it can b
1b723 65 20 72 65 75 73 65 64 2e 20 41 20 73 75 63 63  e reused. A succ
1b724 65 73 73 20 63 6f 64 65 20 66 72 6f 6d 0a 2a 2a  ess code from.**
1b725 20 74 68 65 20 70 72 69 6f 72 20 65 78 65 63 75   the prior execu
1b726 74 69 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64  tion is returned
1b727 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1b728 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 65 72  tine sets the er
1b729 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 73 74 72  ror code and str
1b72a 69 6e 67 20 72 65 74 75 72 6e 65 64 20 62 79 0a  ing returned by.
1b72b 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  ** sqlite3_errco
1b72c 64 65 28 29 2c 20 73 71 6c 69 74 65 33 5f 65 72  de(), sqlite3_er
1b72d 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74  rmsg() and sqlit
1b72e 65 33 5f 65 72 72 6d 73 67 31 36 28 29 2e 0a 2a  e3_errmsg16()..*
1b72f 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1b730 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73   sqlite3_reset(s
1b731 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1b732 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  mt){.  int rc;. 
1b733 20 69 66 28 20 70 53 74 6d 74 3d 3d 30 20 29 7b   if( pStmt==0 ){
1b734 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1b735 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
1b736 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62    Vdbe *v = (Vdb
1b737 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71  e*)pStmt;.    sq
1b738 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1b739 72 28 76 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  r(v->db->mutex);
1b73a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1b73b 33 56 64 62 65 52 65 73 65 74 28 76 29 3b 0a 20  3VdbeReset(v);. 
1b73c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61     sqlite3VdbeMa
1b73d 6b 65 52 65 61 64 79 28 76 2c 20 2d 31 2c 20 30  keReady(v, -1, 0
1b73e 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
1b73f 20 20 20 61 73 73 65 72 74 28 20 28 72 63 20 26     assert( (rc &
1b740 20 28 76 2d 3e 64 62 2d 3e 65 72 72 4d 61 73 6b   (v->db->errMask
1b741 29 29 3d 3d 72 63 20 29 3b 0a 20 20 20 20 72 63  ))==rc );.    rc
1b742 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
1b743 74 28 76 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20  t(v->db, rc);.  
1b744 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1b745 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d 75 74  leave(v->db->mut
1b746 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ex);.  }.  retur
1b747 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
1b748 65 74 20 61 6c 6c 20 74 68 65 20 70 61 72 61 6d  et all the param
1b749 65 74 65 72 73 20 69 6e 20 74 68 65 20 63 6f 6d  eters in the com
1b74a 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d  piled SQL statem
1b74b 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ent to NULL..*/.
1b74c 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1b74d 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e  qlite3_clear_bin
1b74e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74  dings(sqlite3_st
1b74f 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e  mt *pStmt){.  in
1b750 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  t i;.  int rc = 
1b751 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 56 64 62  SQLITE_OK;.  Vdb
1b752 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29 70 53  e *p = (Vdbe*)pS
1b753 74 6d 74 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f  tmt;.#if SQLITE_
1b754 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c  THREADSAFE.  sql
1b755 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
1b756 78 20 3d 20 28 28 56 64 62 65 2a 29 70 53 74 6d  x = ((Vdbe*)pStm
1b757 74 29 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b 0a 23  t)->db->mutex;.#
1b758 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
1b759 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65  mutex_enter(mute
1b75a 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  x);.  for(i=0; i
1b75b 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a  <p->nVar; i++){.
1b75c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1b75d 65 6d 52 65 6c 65 61 73 65 28 26 70 2d 3e 61 56  emRelease(&p->aV
1b75e 61 72 5b 69 5d 29 3b 0a 20 20 20 20 70 2d 3e 61  ar[i]);.    p->a
1b75f 56 61 72 5b 69 5d 2e 66 6c 61 67 73 20 3d 20 4d  Var[i].flags = M
1b760 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20 20 69  EM_Null;.  }.  i
1b761 66 28 20 70 2d 3e 69 73 50 72 65 70 61 72 65 56  f( p->isPrepareV
1b762 32 20 26 26 20 70 2d 3e 65 78 70 6d 61 73 6b 20  2 && p->expmask 
1b763 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65  ){.    p->expire
1b764 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c  d = 1;.  }.  sql
1b765 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1b766 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  (mutex);.  retur
1b767 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a  n rc;.}.../*****
1b768 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b769 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f  ******* sqlite3_
1b76a 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a  value_  ********
1b76b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b76c 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66  *******.** The f
1b76d 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
1b76e 73 20 65 78 74 72 61 63 74 20 69 6e 66 6f 72 6d  s extract inform
1b76f 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 4d 65 6d  ation from a Mem
1b770 20 6f 72 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   or sqlite3_valu
1b771 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a  e.** structure..
1b772 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
1b773 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
1b774 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c  3_value_blob(sql
1b775 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
1b776 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d  ){.  Mem *p = (M
1b777 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20  em*)pVal;.  if( 
1b778 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  p->flags & (MEM_
1b779 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29 7b  Blob|MEM_Str) ){
1b77a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1b77b 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 70 29  MemExpandBlob(p)
1b77c 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 26  ;.    p->flags &
1b77d 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20  = ~MEM_Str;.    
1b77e 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  p->flags |= MEM_
1b77f 42 6c 6f 62 3b 0a 20 20 20 20 72 65 74 75 72 6e  Blob;.    return
1b780 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a   p->z;.  }else{.
1b781 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
1b782 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 56  e3_value_text(pV
1b783 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54  al);.  }.}.SQLIT
1b784 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1b785 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 73 71  3_value_bytes(sq
1b786 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1b787 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  l){.  return sql
1b788 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70  ite3ValueBytes(p
1b789 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Val, SQLITE_UTF8
1b78a 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1b78b 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  int sqlite3_valu
1b78c 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74 65  e_bytes16(sqlite
1b78d 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
1b78e 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
1b78f 56 61 6c 75 65 42 79 74 65 73 28 70 56 61 6c 2c  ValueBytes(pVal,
1b790 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
1b791 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  IVE);.}.SQLITE_A
1b792 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  PI double sqlite
1b793 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73  3_value_double(s
1b794 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
1b795 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
1b796 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
1b797 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a  ue((Mem*)pVal);.
1b798 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
1b799 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
1b79a 6e 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt(sqlite3_value
1b79b 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72   *pVal){.  retur
1b79c 6e 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64  n (int)sqlite3Vd
1b79d 62 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a  beIntValue((Mem*
1b79e 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45  )pVal);.}.SQLITE
1b79f 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36  _API sqlite_int6
1b7a0 34 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  4 sqlite3_value_
1b7a1 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61  int64(sqlite3_va
1b7a2 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65  lue *pVal){.  re
1b7a3 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65  turn sqlite3Vdbe
1b7a4 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70  IntValue((Mem*)p
1b7a5 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  Val);.}.SQLITE_A
1b7a6 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  PI const unsigne
1b7a7 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  d char *sqlite3_
1b7a8 76 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74  value_text(sqlit
1b7a9 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b  e3_value *pVal){
1b7aa 0a 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74  .  return (const
1b7ab 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
1b7ac 29 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78  )sqlite3ValueTex
1b7ad 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55  t(pVal, SQLITE_U
1b7ae 54 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  TF8);.}.#ifndef 
1b7af 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1b7b0 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  6.SQLITE_API con
1b7b1 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1b7b2 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71  _value_text16(sq
1b7b3 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61  lite3_value* pVa
1b7b4 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  l){.  return sql
1b7b5 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56  ite3ValueText(pV
1b7b6 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  al, SQLITE_UTF16
1b7b7 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54  NATIVE);.}.SQLIT
1b7b8 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
1b7b9 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   *sqlite3_value_
1b7ba 74 65 78 74 31 36 62 65 28 73 71 6c 69 74 65 33  text16be(sqlite3
1b7bb 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20  _value *pVal){. 
1b7bc 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
1b7bd 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53  alueText(pVal, S
1b7be 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b 0a  QLITE_UTF16BE);.
1b7bf 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  }.SQLITE_API con
1b7c0 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1b7c1 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28  _value_text16le(
1b7c2 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1b7c3 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  Val){.  return s
1b7c4 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
1b7c5 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  pVal, SQLITE_UTF
1b7c6 31 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  16LE);.}.#endif 
1b7c7 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
1b7c8 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  TF16 */.SQLITE_A
1b7c9 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76  PI int sqlite3_v
1b7ca 61 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65  alue_type(sqlite
1b7cb 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a  3_value* pVal){.
1b7cc 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 74    return pVal->t
1b7cd 79 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ype;.}../*******
1b7ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7cf 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65  ***** sqlite3_re
1b7d0 73 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  sult_  *********
1b7d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b7d2 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ******.** The fo
1b7d3 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73  llowing routines
1b7d4 20 61 72 65 20 75 73 65 64 20 62 79 20 75 73 65   are used by use
1b7d5 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  r-defined functi
1b7d6 6f 6e 73 20 74 6f 20 73 70 65 63 69 66 79 0a 2a  ons to specify.*
1b7d7 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72  * the function r
1b7d8 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  esult..**.** The
1b7d9 20 73 65 74 53 74 72 4f 72 45 72 72 6f 72 28 29   setStrOrError()
1b7da 20 66 75 6e 74 69 6f 6e 20 63 61 6c 6c 73 20 73   funtion calls s
1b7db 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1b7dc 53 74 72 28 29 20 74 6f 20 73 74 6f 72 65 20 74  Str() to store t
1b7dd 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 61 73 20  he.** result as 
1b7de 61 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62  a string or blob
1b7df 20 62 75 74 20 69 66 20 74 68 65 20 73 74 72 69   but if the stri
1b7e0 6e 67 20 6f 72 20 62 6c 6f 62 20 69 73 20 74 6f  ng or blob is to
1b7e1 6f 20 6c 61 72 67 65 2c 20 69 74 0a 2a 2a 20 74  o large, it.** t
1b7e2 68 65 6e 20 73 65 74 73 20 74 68 65 20 65 72 72  hen sets the err
1b7e3 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54  or code to SQLIT
1b7e4 45 5f 54 4f 4f 42 49 47 0a 2a 2f 0a 73 74 61 74  E_TOOBIG.*/.stat
1b7e5 69 63 20 76 6f 69 64 20 73 65 74 52 65 73 75 6c  ic void setResul
1b7e6 74 53 74 72 4f 72 45 72 72 6f 72 28 0a 20 20 73  tStrOrError(.  s
1b7e7 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b7e8 70 43 74 78 2c 20 20 2f 2a 20 46 75 6e 63 74 69  pCtx,  /* Functi
1b7e9 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  on context */.  
1b7ea 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20  const char *z,  
1b7eb 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e          /* Strin
1b7ec 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69  g pointer */.  i
1b7ed 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20  nt n,           
1b7ee 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
1b7ef 69 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65  in string, or ne
1b7f0 67 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65  gative */.  u8 e
1b7f1 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  nc,             
1b7f2 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20      /* Encoding 
1b7f3 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20 42 4c 4f  of z.  0 for BLO
1b7f4 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78  Bs */.  void (*x
1b7f5 44 65 6c 29 28 76 6f 69 64 2a 29 20 20 20 20 20  Del)(void*)     
1b7f6 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75  /* Destructor fu
1b7f7 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69  nction */.){.  i
1b7f8 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
1b7f9 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73  mSetStr(&pCtx->s
1b7fa 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78 44 65  , z, n, enc, xDe
1b7fb 6c 29 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42 49  l)==SQLITE_TOOBI
1b7fc 47 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  G ){.    sqlite3
1b7fd 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  _result_error_to
1b7fe 6f 62 69 67 28 70 43 74 78 29 3b 0a 20 20 7d 0a  obig(pCtx);.  }.
1b7ff 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  }.SQLITE_API voi
1b800 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1b801 5f 62 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33  _blob(.  sqlite3
1b802 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1b803 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1b804 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76  , .  int n, .  v
1b805 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64  oid (*xDel)(void
1b806 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72 74 28   *).){.  assert(
1b807 20 6e 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   n>=0 );.  asser
1b808 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1b809 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1b80a 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65  ->mutex) );.  se
1b80b 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f  tResultStrOrErro
1b80c 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 30 2c  r(pCtx, z, n, 0,
1b80d 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45   xDel);.}.SQLITE
1b80e 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1b80f 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28  3_result_double(
1b810 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b811 2a 70 43 74 78 2c 20 64 6f 75 62 6c 65 20 72 56  *pCtx, double rV
1b812 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  al){.  assert( s
1b813 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1b814 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
1b815 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
1b816 33 56 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c  3VdbeMemSetDoubl
1b817 65 28 26 70 43 74 78 2d 3e 73 2c 20 72 56 61 6c  e(&pCtx->s, rVal
1b818 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1b819 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1b81a 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c 69 74 65  ult_error(sqlite
1b81b 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
1b81c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
1b81d 69 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74  int n){.  assert
1b81e 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1b81f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
1b820 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74  >mutex) );.  pCt
1b821 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c  x->isError = SQL
1b822 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 73 71 6c  ITE_ERROR;.  sql
1b823 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
1b824 72 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e  r(&pCtx->s, z, n
1b825 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
1b826 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
1b827 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
1b828 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
1b829 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1b82a 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1b82b 6f 72 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e  or16(sqlite3_con
1b82c 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73  text *pCtx, cons
1b82d 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e  t void *z, int n
1b82e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
1b82f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1b830 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
1b831 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73  x) );.  pCtx->is
1b832 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45  Error = SQLITE_E
1b833 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56  RROR;.  sqlite3V
1b834 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43  dbeMemSetStr(&pC
1b835 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c  tx->s, z, n, SQL
1b836 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c  ITE_UTF16NATIVE,
1b837 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
1b838 54 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 53 51 4c  T);.}.#endif.SQL
1b839 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1b83a 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
1b83b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b83c 2a 70 43 74 78 2c 20 69 6e 74 20 69 56 61 6c 29  *pCtx, int iVal)
1b83d 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1b83e 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1b83f 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1b840 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ) );.  sqlite3Vd
1b841 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70  beMemSetInt64(&p
1b842 43 74 78 2d 3e 73 2c 20 28 69 36 34 29 69 56 61  Ctx->s, (i64)iVa
1b843 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  l);.}.SQLITE_API
1b844 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1b845 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c 69 74  sult_int64(sqlit
1b846 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1b847 2c 20 69 36 34 20 69 56 61 6c 29 7b 0a 20 20 61  , i64 iVal){.  a
1b848 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1b849 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1b84a 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1b84b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1b84c 53 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e  SetInt64(&pCtx->
1b84d 73 2c 20 69 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49  s, iVal);.}.SQLI
1b84e 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1b84f 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28  te3_result_null(
1b850 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b851 2a 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74  *pCtx){.  assert
1b852 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1b853 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d  held(pCtx->s.db-
1b854 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c  >mutex) );.  sql
1b855 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
1b856 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b 0a 7d 0a  ll(&pCtx->s);.}.
1b857 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1b858 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
1b859 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ext(.  sqlite3_c
1b85a 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20  ontext *pCtx, . 
1b85b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
1b85c 0a 20 20 69 6e 74 20 6e 2c 0a 20 20 76 6f 69 64  .  int n,.  void
1b85d 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29   (*xDel)(void *)
1b85e 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  .){.  assert( sq
1b85f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1b860 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1b861 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75  ex) );.  setResu
1b862 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74  ltStrOrError(pCt
1b863 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f  x, z, n, SQLITE_
1b864 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a 23  UTF8, xDel);.}.#
1b865 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1b866 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1b867 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1b868 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 0a  _result_text16(.
1b869 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1b86a 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73  t *pCtx, .  cons
1b86b 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e  t void *z, .  in
1b86c 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  t n, .  void (*x
1b86d 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a  Del)(void *).){.
1b86e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1b86f 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1b870 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1b871 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74 53 74  );.  setResultSt
1b872 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c 20 7a  rOrError(pCtx, z
1b873 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  , n, SQLITE_UTF1
1b874 36 4e 41 54 49 56 45 2c 20 78 44 65 6c 29 3b 0a  6NATIVE, xDel);.
1b875 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  }.SQLITE_API voi
1b876 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1b877 5f 74 65 78 74 31 36 62 65 28 0a 20 20 73 71 6c  _text16be(.  sql
1b878 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
1b879 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69  tx, .  const voi
1b87a 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20  d *z, .  int n, 
1b87b 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
1b87c 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73  void *).){.  ass
1b87d 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b87e 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1b87f 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1b880 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72  setResultStrOrEr
1b881 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20  ror(pCtx, z, n, 
1b882 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20  SQLITE_UTF16BE, 
1b883 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  xDel);.}.SQLITE_
1b884 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1b885 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65  _result_text16le
1b886 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
1b887 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f  ext *pCtx, .  co
1b888 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20  nst void *z, .  
1b889 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28  int n, .  void (
1b88a 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29  *xDel)(void *).)
1b88b 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1b88c 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1b88d 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1b88e 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74  ) );.  setResult
1b88f 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c  StrOrError(pCtx,
1b890 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
1b891 46 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a  F16LE, xDel);.}.
1b892 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1b893 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53  _OMIT_UTF16 */.S
1b894 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1b895 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61  qlite3_result_va
1b896 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  lue(sqlite3_cont
1b897 65 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69 74  ext *pCtx, sqlit
1b898 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65  e3_value *pValue
1b899 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
1b89a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1b89b 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
1b89c 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
1b89d 64 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74 78  dbeMemCopy(&pCtx
1b89e 2d 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d 0a  ->s, pValue);.}.
1b89f 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1b8a0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a  sqlite3_result_z
1b8a1 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  eroblob(sqlite3_
1b8a2 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69  context *pCtx, i
1b8a3 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28  nt n){.  assert(
1b8a4 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b8a5 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b8a6 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69  mutex) );.  sqli
1b8a7 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72  te3VdbeMemSetZer
1b8a8 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c 20  oBlob(&pCtx->s, 
1b8a9 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  n);.}.SQLITE_API
1b8aa 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1b8ab 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28  sult_error_code(
1b8ac 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b8ad 2a 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43 6f  *pCtx, int errCo
1b8ae 64 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45  de){.  pCtx->isE
1b8af 72 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b 0a  rror = errCode;.
1b8b0 20 20 69 66 28 20 70 43 74 78 2d 3e 73 2e 66 6c    if( pCtx->s.fl
1b8b1 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
1b8b2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1b8b3 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78  eMemSetStr(&pCtx
1b8b4 2d 3e 73 2c 20 73 71 6c 69 74 65 33 45 72 72 53  ->s, sqlite3ErrS
1b8b5 74 72 28 65 72 72 43 6f 64 65 29 2c 20 2d 31 2c  tr(errCode), -1,
1b8b6 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1b8b7 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
1b8b8 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53  E_UTF8, SQLITE_S
1b8b9 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  TATIC);.  }.}../
1b8ba 2a 20 46 6f 72 63 65 20 61 6e 20 53 51 4c 49 54  * Force an SQLIT
1b8bb 45 5f 54 4f 4f 42 49 47 20 65 72 72 6f 72 2e 20  E_TOOBIG error. 
1b8bc 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
1b8bd 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1b8be 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 73  t_error_toobig(s
1b8bf 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1b8c0 70 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28  pCtx){.  assert(
1b8c1 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1b8c2 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1b8c3 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 43 74 78  mutex) );.  pCtx
1b8c4 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49  ->isError = SQLI
1b8c5 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 73 71 6c  TE_TOOBIG;.  sql
1b8c6 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74  ite3VdbeMemSetSt
1b8c7 72 28 26 70 43 74 78 2d 3e 73 2c 20 22 73 74 72  r(&pCtx->s, "str
1b8c8 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20  ing or blob too 
1b8c9 62 69 67 22 2c 20 2d 31 2c 20 0a 20 20 20 20 20  big", -1, .     
1b8ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b8cb 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53    SQLITE_UTF8, S
1b8cc 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d  QLITE_STATIC);.}
1b8cd 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49 54 45 5f 4e  ../* An SQLITE_N
1b8ce 4f 4d 45 4d 20 65 72 72 6f 72 2e 20 2a 2f 0a 53  OMEM error. */.S
1b8cf 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1b8d0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1b8d1 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65  ror_nomem(sqlite
1b8d2 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29  3_context *pCtx)
1b8d3 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1b8d4 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1b8d5 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1b8d6 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ) );.  sqlite3Vd
1b8d7 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43  beMemSetNull(&pC
1b8d8 74 78 2d 3e 73 29 3b 0a 20 20 70 43 74 78 2d 3e  tx->s);.  pCtx->
1b8d9 69 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45  isError = SQLITE
1b8da 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43 74 78 2d 3e  _NOMEM;.  pCtx->
1b8db 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  s.db->mallocFail
1b8dc 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ed = 1;.}../*.**
1b8dd 20 45 78 65 63 75 74 65 20 74 68 65 20 73 74 61   Execute the sta
1b8de 74 65 6d 65 6e 74 20 70 53 74 6d 74 2c 20 65 69  tement pStmt, ei
1b8df 74 68 65 72 20 75 6e 74 69 6c 20 61 20 72 6f 77  ther until a row
1b8e0 20 6f 66 20 64 61 74 61 20 69 73 20 72 65 61 64   of data is read
1b8e1 79 2c 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d  y, the.** statem
1b8e2 65 6e 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c  ent is completel
1b8e3 79 20 65 78 65 63 75 74 65 64 20 6f 72 20 61 6e  y executed or an
1b8e4 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a   error occurs..*
1b8e5 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1b8e6 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
1b8e7 20 62 75 6c 6b 20 6f 66 20 74 68 65 20 6c 6f 67   bulk of the log
1b8e8 69 63 20 62 65 68 69 6e 64 20 74 68 65 20 73 71  ic behind the sq
1b8e9 6c 69 74 65 5f 73 74 65 70 28 29 0a 2a 2a 20 41  lite_step().** A
1b8ea 50 49 2e 20 20 54 68 65 20 6f 6e 6c 79 20 74 68  PI.  The only th
1b8eb 69 6e 67 20 6f 6d 69 74 74 65 64 20 69 73 20 74  ing omitted is t
1b8ec 68 65 20 61 75 74 6f 6d 61 74 69 63 20 72 65 63  he automatic rec
1b8ed 6f 6d 70 69 6c 65 20 69 66 20 61 20 0a 2a 2a 20  ompile if a .** 
1b8ee 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 68 61  schema change ha
1b8ef 73 20 6f 63 63 75 72 72 65 64 2e 20 20 54 68 61  s occurred.  Tha
1b8f0 74 20 64 65 74 61 69 6c 20 69 73 20 68 61 6e 64  t detail is hand
1b8f1 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 6f 75  led by the.** ou
1b8f2 74 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ter sqlite3_step
1b8f3 28 29 20 77 72 61 70 70 65 72 20 70 72 6f 63 65  () wrapper proce
1b8f4 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  dure..*/.static 
1b8f5 69 6e 74 20 73 71 6c 69 74 65 33 53 74 65 70 28  int sqlite3Step(
1b8f6 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c 69  Vdbe *p){.  sqli
1b8f7 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74 20 72  te3 *db;.  int r
1b8f8 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 29 3b  c;..  assert(p);
1b8f9 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21  .  if( p->magic!
1b8fa 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20  =VDBE_MAGIC_RUN 
1b8fb 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
1b8fc 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
1b8fd 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68  ..  /* Assert th
1b8fe 61 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20  at malloc() has 
1b8ff 6e 6f 74 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20  not failed */.  
1b900 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66  db = p->db;.  if
1b901 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
1b902 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
1b903 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1b904 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c   }..  if( p->pc<
1b905 3d 30 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64  =0 && p->expired
1b906 20 29 7b 0a 20 20 20 20 69 66 28 20 41 4c 57 41   ){.    if( ALWA
1b907 59 53 28 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  YS(p->rc==SQLITE
1b908 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51  _OK || p->rc==SQ
1b909 4c 49 54 45 5f 53 43 48 45 4d 41 29 20 29 7b 0a  LITE_SCHEMA) ){.
1b90a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51        p->rc = SQ
1b90b 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 20  LITE_SCHEMA;.   
1b90c 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49   }.    rc = SQLI
1b90d 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 67 6f  TE_ERROR;.    go
1b90e 74 6f 20 65 6e 64 5f 6f 66 5f 73 74 65 70 3b 0a  to end_of_step;.
1b90f 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
1b910 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b  3SafetyOn(db) ){
1b911 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  .    p->rc = SQL
1b912 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20  ITE_MISUSE;.    
1b913 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
1b914 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  SUSE;.  }.  if( 
1b915 70 2d 3e 70 63 3c 30 20 29 7b 0a 20 20 20 20 2f  p->pc<0 ){.    /
1b916 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6e  * If there are n
1b917 6f 20 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e  o other statemen
1b918 74 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e  ts currently run
1b919 6e 69 6e 67 2c 20 74 68 65 6e 0a 20 20 20 20 2a  ning, then.    *
1b91a 2a 20 72 65 73 65 74 20 74 68 65 20 69 6e 74 65  * reset the inte
1b91b 72 72 75 70 74 20 66 6c 61 67 2e 20 20 54 68 69  rrupt flag.  Thi
1b91c 73 20 70 72 65 76 65 6e 74 73 20 61 20 63 61 6c  s prevents a cal
1b91d 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74  l to sqlite3_int
1b91e 65 72 72 75 70 74 0a 20 20 20 20 2a 2a 20 66 72  errupt.    ** fr
1b91f 6f 6d 20 69 6e 74 65 72 72 75 70 74 69 6e 67 20  om interrupting 
1b920 61 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  a statement that
1b921 20 68 61 73 20 6e 6f 74 20 79 65 74 20 73 74 61   has not yet sta
1b922 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rted..    */.   
1b923 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56   if( db->activeV
1b924 64 62 65 43 6e 74 3d 3d 30 20 29 7b 0a 20 20 20  dbeCnt==0 ){.   
1b925 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65     db->u1.isInte
1b926 72 72 75 70 74 65 64 20 3d 20 30 3b 0a 20 20 20  rrupted = 0;.   
1b927 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20   }..    assert( 
1b928 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74  db->writeVdbeCnt
1b929 3e 30 20 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f  >0 || db->autoCo
1b92a 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6e  mmit==0 || db->n
1b92b 44 65 66 65 72 72 65 64 43 6f 6e 73 3d 3d 30 20  DeferredCons==0 
1b92c 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
1b92d 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20  TE_OMIT_TRACE.  
1b92e 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f 66 69    if( db->xProfi
1b92f 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74 2e  le && !db->init.
1b930 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 64 6f  busy ){.      do
1b931 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20 20 20 20  uble rNow;.     
1b932 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e   sqlite3OsCurren
1b933 74 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20  tTime(db->pVfs, 
1b934 26 72 4e 6f 77 29 3b 0a 20 20 20 20 20 20 70 2d  &rNow);.      p-
1b935 3e 73 74 61 72 74 54 69 6d 65 20 3d 20 28 75 36  >startTime = (u6
1b936 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29  4)((rNow - (int)
1b937 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e  rNow)*3600.0*24.
1b938 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30 29 3b  0*1000000000.0);
1b939 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
1b93a 20 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62     db->activeVdb
1b93b 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20  eCnt++;.    if( 
1b93c 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29  p->readOnly==0 )
1b93d 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e   db->writeVdbeCn
1b93e 74 2b 2b 3b 0a 20 20 20 20 70 2d 3e 70 63 20 3d  t++;.    p->pc =
1b93f 20 30 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20   0;.  }.#ifndef 
1b940 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
1b941 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e 65 78 70  AIN.  if( p->exp
1b942 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d  lain ){.    rc =
1b943 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74   sqlite3VdbeList
1b944 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e  (p);.  }else.#en
1b945 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1b946 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 20 20  IT_EXPLAIN */.  
1b947 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1b948 65 33 56 64 62 65 45 78 65 63 28 70 29 3b 0a 20  e3VdbeExec(p);. 
1b949 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65   }..  if( sqlite
1b94a 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1b94b 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
1b94c 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a 23  E_MISUSE;.  }..#
1b94d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1b94e 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a 20 49 6e  IT_TRACE.  /* In
1b94f 76 6f 6b 65 20 74 68 65 20 70 72 6f 66 69 6c 65  voke the profile
1b950 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65   callback if the
1b951 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a 2f 0a 20  re is one.  */. 
1b952 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1b953 52 4f 57 20 26 26 20 64 62 2d 3e 78 50 72 6f 66  ROW && db->xProf
1b954 69 6c 65 20 26 26 20 21 64 62 2d 3e 69 6e 69 74  ile && !db->init
1b955 2e 62 75 73 79 20 26 26 20 70 2d 3e 7a 53 71 6c  .busy && p->zSql
1b956 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72   ){.    double r
1b957 4e 6f 77 3b 0a 20 20 20 20 75 36 34 20 65 6c 61  Now;.    u64 ela
1b958 70 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20 73 71  pseTime;..    sq
1b959 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
1b95a 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 4e  me(db->pVfs, &rN
1b95b 6f 77 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54  ow);.    elapseT
1b95c 69 6d 65 20 3d 20 28 75 36 34 29 28 28 72 4e 6f  ime = (u64)((rNo
1b95d 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33  w - (int)rNow)*3
1b95e 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30  600.0*24.0*10000
1b95f 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 65 6c  00000.0);.    el
1b960 61 70 73 65 54 69 6d 65 20 2d 3d 20 70 2d 3e 73  apseTime -= p->s
1b961 74 61 72 74 54 69 6d 65 3b 0a 20 20 20 20 64 62  tartTime;.    db
1b962 2d 3e 78 50 72 6f 66 69 6c 65 28 64 62 2d 3e 70  ->xProfile(db->p
1b963 50 72 6f 66 69 6c 65 41 72 67 2c 20 70 2d 3e 7a  ProfileArg, p->z
1b964 53 71 6c 2c 20 65 6c 61 70 73 65 54 69 6d 65 29  Sql, elapseTime)
1b965 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
1b966 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63  db->errCode = rc
1b967 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e  ;.  if( SQLITE_N
1b968 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 33 41 70 69  OMEM==sqlite3Api
1b969 45 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72  Exit(p->db, p->r
1b96a 63 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20  c) ){.    p->rc 
1b96b 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1b96c 20 20 7d 0a 65 6e 64 5f 6f 66 5f 73 74 65 70 3a    }.end_of_step:
1b96d 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f  .  /* At this po
1b96e 69 6e 74 20 6c 6f 63 61 6c 20 76 61 72 69 61 62  int local variab
1b96f 6c 65 20 72 63 20 68 6f 6c 64 73 20 74 68 65 20  le rc holds the 
1b970 76 61 6c 75 65 20 74 68 61 74 20 73 68 6f 75 6c  value that shoul
1b971 64 20 62 65 20 0a 20 20 2a 2a 20 72 65 74 75 72  d be .  ** retur
1b972 6e 65 64 20 69 66 20 74 68 69 73 20 73 74 61 74  ned if this stat
1b973 65 6d 65 6e 74 20 77 61 73 20 63 6f 6d 70 69 6c  ement was compil
1b974 65 64 20 75 73 69 6e 67 20 74 68 65 20 6c 65 67  ed using the leg
1b975 61 63 79 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65  acy .  ** sqlite
1b976 33 5f 70 72 65 70 61 72 65 28 29 20 69 6e 74 65  3_prepare() inte
1b977 72 66 61 63 65 2e 20 41 63 63 6f 72 64 69 6e 67  rface. According
1b978 20 74 6f 20 74 68 65 20 64 6f 63 73 2c 20 74 68   to the docs, th
1b979 69 73 20 63 61 6e 20 6f 6e 6c 79 0a 20 20 2a 2a  is can only.  **
1b97a 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20 76   be one of the v
1b97b 61 6c 75 65 73 20 69 6e 20 74 68 65 20 66 69 72  alues in the fir
1b97c 73 74 20 61 73 73 65 72 74 28 29 20 62 65 6c 6f  st assert() belo
1b97d 77 2e 20 56 61 72 69 61 62 6c 65 20 70 2d 3e 72  w. Variable p->r
1b97e 63 20 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 6e 73  c .  ** contains
1b97f 20 74 68 65 20 76 61 6c 75 65 20 74 68 61 74 20   the value that 
1b980 77 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65  would be returne
1b981 64 20 69 66 20 73 71 6c 69 74 65 33 5f 66 69 6e  d if sqlite3_fin
1b982 61 6c 69 7a 65 28 29 20 0a 20 20 2a 2a 20 77 65  alize() .  ** we
1b983 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 73 74 61  re called on sta
1b984 74 65 6d 65 6e 74 20 70 2e 0a 20 20 2a 2f 0a 20  tement p..  */. 
1b985 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
1b986 49 54 45 5f 52 4f 57 20 20 7c 7c 20 72 63 3d 3d  ITE_ROW  || rc==
1b987 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 7c 7c  SQLITE_DONE   ||
1b988 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52 4f   rc==SQLITE_ERRO
1b989 52 20 0a 20 20 20 20 20 20 20 7c 7c 20 72 63 3d  R .       || rc=
1b98a 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c 20  =SQLITE_BUSY || 
1b98b 72 63 3d 3d 53 51 4c 49 54 45 5f 4d 49 53 55 53  rc==SQLITE_MISUS
1b98c 45 0a 20 20 29 3b 0a 20 20 61 73 73 65 72 74 28  E.  );.  assert(
1b98d 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 52   p->rc!=SQLITE_R
1b98e 4f 57 20 26 26 20 70 2d 3e 72 63 21 3d 53 51 4c  OW && p->rc!=SQL
1b98f 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 69 66  ITE_DONE );.  if
1b990 28 20 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32  ( p->isPrepareV2
1b991 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 52   && rc!=SQLITE_R
1b992 4f 57 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45  OW && rc!=SQLITE
1b993 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2f 2a 20  _DONE ){.    /* 
1b994 49 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e  If this statemen
1b995 74 20 77 61 73 20 70 72 65 70 61 72 65 64 20 75  t was prepared u
1b996 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 70 72 65  sing sqlite3_pre
1b997 70 61 72 65 5f 76 32 28 29 2c 20 61 6e 64 20 61  pare_v2(), and a
1b998 6e 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72 20 68  n.    ** error h
1b999 61 73 20 6f 63 63 75 72 65 64 2c 20 74 68 65 6e  as occured, then
1b99a 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   return the erro
1b99b 72 20 63 6f 64 65 20 69 6e 20 70 2d 3e 72 63 20  r code in p->rc 
1b99c 74 6f 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61  to the.    ** ca
1b99d 6c 6c 65 72 2e 20 53 65 74 20 74 68 65 20 65 72  ller. Set the er
1b99e 72 6f 72 20 63 6f 64 65 20 69 6e 20 74 68 65 20  ror code in the 
1b99f 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
1b9a0 74 6f 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75  to the same valu
1b9a1 65 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 72  e..    */ .    r
1b9a2 63 20 3d 20 64 62 2d 3e 65 72 72 43 6f 64 65 20  c = db->errCode 
1b9a3 3d 20 70 2d 3e 72 63 3b 0a 20 20 7d 0a 20 20 72  = p->rc;.  }.  r
1b9a4 65 74 75 72 6e 20 28 72 63 26 64 62 2d 3e 65 72  eturn (rc&db->er
1b9a5 72 4d 61 73 6b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rMask);.}../*.**
1b9a6 20 54 68 69 73 20 69 73 20 74 68 65 20 74 6f 70   This is the top
1b9a7 2d 6c 65 76 65 6c 20 69 6d 70 6c 65 6d 65 6e 74  -level implement
1b9a8 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  ation of sqlite3
1b9a9 5f 73 74 65 70 28 29 2e 20 20 43 61 6c 6c 0a 2a  _step().  Call.*
1b9aa 2a 20 73 71 6c 69 74 65 33 53 74 65 70 28 29 20  * sqlite3Step() 
1b9ab 74 6f 20 64 6f 20 6d 6f 73 74 20 6f 66 20 74 68  to do most of th
1b9ac 65 20 77 6f 72 6b 2e 20 20 49 66 20 61 20 73 63  e work.  If a sc
1b9ad 68 65 6d 61 20 65 72 72 6f 72 20 6f 63 63 75 72  hema error occur
1b9ae 73 2c 0a 2a 2a 20 63 61 6c 6c 20 73 71 6c 69 74  s,.** call sqlit
1b9af 65 33 52 65 70 72 65 70 61 72 65 28 29 20 61 6e  e3Reprepare() an
1b9b0 64 20 74 72 79 20 61 67 61 69 6e 2e 0a 2a 2f 0a  d try again..*/.
1b9b1 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1b9b2 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71 6c 69  qlite3_step(sqli
1b9b3 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
1b9b4 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
1b9b5 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 69 66  ITE_MISUSE;.  if
1b9b6 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 69  ( pStmt ){.    i
1b9b7 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20  nt cnt = 0;.    
1b9b8 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62 65 2a  Vdbe *v = (Vdbe*
1b9b9 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71 6c 69  )pStmt;.    sqli
1b9ba 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64 62 3b  te3 *db = v->db;
1b9bb 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1b9bc 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
1b9bd 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ex);.    while( 
1b9be 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53 74 65  (rc = sqlite3Ste
1b9bf 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 53 43  p(v))==SQLITE_SC
1b9c0 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20 20 20  HEMA.           
1b9c1 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20 20 20  && cnt++ < 5.   
1b9c2 20 20 20 20 20 20 20 20 26 26 20 28 72 63 20 3d          && (rc =
1b9c3 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72   sqlite3Reprepar
1b9c4 65 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e(v))==SQLITE_OK
1b9c5 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1b9c6 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a  3_reset(pStmt);.
1b9c7 20 20 20 20 20 20 76 2d 3e 65 78 70 69 72 65 64        v->expired
1b9c8 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
1b9c9 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53  if( rc==SQLITE_S
1b9ca 43 48 45 4d 41 20 26 26 20 41 4c 57 41 59 53 28  CHEMA && ALWAYS(
1b9cb 76 2d 3e 69 73 50 72 65 70 61 72 65 56 32 29 20  v->isPrepareV2) 
1b9cc 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e 70 45  && ALWAYS(db->pE
1b9cd 72 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  rr) ){.      /* 
1b9ce 54 68 69 73 20 63 61 73 65 20 6f 63 63 75 72 73  This case occurs
1b9cf 20 61 66 74 65 72 20 66 61 69 6c 69 6e 67 20 74   after failing t
1b9d0 6f 20 72 65 63 6f 6d 70 69 6c 65 20 61 6e 20 73  o recompile an s
1b9d1 71 6c 20 73 74 61 74 65 6d 65 6e 74 2e 20 0a 20  ql statement. . 
1b9d2 20 20 20 20 20 2a 2a 20 54 68 65 20 65 72 72 6f       ** The erro
1b9d3 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 74  r message from t
1b9d4 68 65 20 53 51 4c 20 63 6f 6d 70 69 6c 65 72 20  he SQL compiler 
1b9d5 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
1b9d6 20 6c 6f 61 64 65 64 20 0a 20 20 20 20 20 20 2a   loaded .      *
1b9d7 2a 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  * into the datab
1b9d8 61 73 65 20 68 61 6e 64 6c 65 2e 20 54 68 69 73  ase handle. This
1b9d9 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73 20 74 68   block copies th
1b9da 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
1b9db 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74  .      ** from t
1b9dc 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
1b9dd 6c 65 20 69 6e 74 6f 20 74 68 65 20 73 74 61 74  le into the stat
1b9de 65 6d 65 6e 74 20 61 6e 64 20 73 65 74 73 20 74  ement and sets t
1b9df 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20  he statement.   
1b9e0 20 20 20 2a 2a 20 70 72 6f 67 72 61 6d 20 63 6f     ** program co
1b9e1 75 6e 74 65 72 20 74 6f 20 30 20 74 6f 20 65 6e  unter to 0 to en
1b9e2 73 75 72 65 20 74 68 61 74 20 77 68 65 6e 20 74  sure that when t
1b9e3 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
1b9e4 0a 20 20 20 20 20 20 2a 2a 20 66 69 6e 61 6c 69  .      ** finali
1b9e5 7a 65 64 20 6f 72 20 72 65 73 65 74 20 74 68 65  zed or reset the
1b9e6 20 70 61 72 73 65 72 20 65 72 72 6f 72 20 6d 65   parser error me
1b9e7 73 73 61 67 65 20 69 73 20 61 76 61 69 6c 61 62  ssage is availab
1b9e8 6c 65 20 76 69 61 0a 20 20 20 20 20 20 2a 2a 20  le via.      ** 
1b9e9 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
1b9ea 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72   and sqlite3_err
1b9eb 63 6f 64 65 28 29 2e 0a 20 20 20 20 20 20 2a 2f  code()..      */
1b9ec 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
1b9ed 72 20 2a 7a 45 72 72 20 3d 20 28 63 6f 6e 73 74  r *zErr = (const
1b9ee 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
1b9ef 76 61 6c 75 65 5f 74 65 78 74 28 64 62 2d 3e 70  value_text(db->p
1b9f0 45 72 72 29 3b 20 0a 20 20 20 20 20 20 73 71 6c  Err); .      sql
1b9f1 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 76  ite3DbFree(db, v
1b9f2 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
1b9f3 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
1b9f4 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
1b9f5 20 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20     v->zErrMsg = 
1b9f6 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
1b9f7 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20  db, zErr);.     
1b9f8 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
1b9f9 20 20 76 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30    v->zErrMsg = 0
1b9fa 3b 0a 20 20 20 20 20 20 20 20 76 2d 3e 72 63 20  ;.        v->rc 
1b9fb 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1b9fc 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1b9fd 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
1b9fe 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
1b9ff 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1ba00 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
1ba01 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1ba02 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  rc;.}../*.** Ext
1ba03 72 61 63 74 20 74 68 65 20 75 73 65 72 20 64 61  ract the user da
1ba04 74 61 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65  ta from a sqlite
1ba05 33 5f 63 6f 6e 74 65 78 74 20 73 74 72 75 63 74  3_context struct
1ba06 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ure and return a
1ba07 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  .** pointer to i
1ba08 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
1ba09 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 75   void *sqlite3_u
1ba0a 73 65 72 5f 64 61 74 61 28 73 71 6c 69 74 65 33  ser_data(sqlite3
1ba0b 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20  _context *p){.  
1ba0c 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e  assert( p && p->
1ba0d 70 46 75 6e 63 20 29 3b 0a 20 20 72 65 74 75 72  pFunc );.  retur
1ba0e 6e 20 70 2d 3e 70 46 75 6e 63 2d 3e 70 55 73 65  n p->pFunc->pUse
1ba0f 72 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rData;.}../*.** 
1ba10 45 78 74 72 61 63 74 20 74 68 65 20 75 73 65 72  Extract the user
1ba11 20 64 61 74 61 20 66 72 6f 6d 20 61 20 73 71 6c   data from a sql
1ba12 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 74 72  ite3_context str
1ba13 75 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72  ucture and retur
1ba14 6e 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74  n a.** pointer t
1ba15 6f 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  o it..*/.SQLITE_
1ba16 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  API sqlite3 *sql
1ba17 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
1ba18 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63  handle(sqlite3_c
1ba19 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61 73  ontext *p){.  as
1ba1a 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46  sert( p && p->pF
1ba1b 75 6e 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  unc );.  return 
1ba1c 70 2d 3e 73 2e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a  p->s.db;.}../*.*
1ba1d 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1ba1e 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
1ba1f 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20  ation of an SQL 
1ba20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 61 6c  function that al
1ba21 77 61 79 73 0a 2a 2a 20 66 61 69 6c 73 20 77 69  ways.** fails wi
1ba22 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  th an error mess
1ba23 61 67 65 20 73 74 61 74 69 6e 67 20 74 68 61 74  age stating that
1ba24 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
1ba25 20 75 73 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20   used in the.** 
1ba26 77 72 6f 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20  wrong context.  
1ba27 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 76 65 72  The sqlite3_over
1ba28 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 29 20  load_function() 
1ba29 41 50 49 20 6d 69 67 68 74 20 63 6f 6e 73 74 72  API might constr
1ba2a 75 63 74 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74  uct.** SQL funct
1ba2b 69 6f 6e 20 74 68 61 74 20 75 73 65 20 74 68 69  ion that use thi
1ba2c 73 20 72 6f 75 74 69 6e 65 20 73 6f 20 74 68 61  s routine so tha
1ba2d 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 20  t the functions 
1ba2e 77 69 6c 6c 20 65 78 69 73 74 0a 2a 2a 20 66 6f  will exist.** fo
1ba2f 72 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f  r name resolutio
1ba30 6e 20 62 75 74 20 61 72 65 20 61 63 74 75 61 6c  n but are actual
1ba31 6c 79 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79  ly overloaded by
1ba32 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63 74 69   the xFindFuncti
1ba33 6f 6e 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20  on.** method of 
1ba34 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a  virtual tables..
1ba35 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1ba36 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e  E void sqlite3In
1ba37 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 28 0a 20  validFunction(. 
1ba38 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1ba39 20 2a 63 6f 6e 74 65 78 74 2c 20 20 2f 2a 20 54   *context,  /* T
1ba3a 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c  he function call
1ba3b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
1ba3c 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 20 20   int NotUsed,   
1ba3d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1ba3e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
1ba3f 74 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  ts to the functi
1ba40 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  on */.  sqlite3_
1ba41 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32  value **NotUsed2
1ba42 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 65     /* Value of e
1ba43 61 63 68 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a  ach argument */.
1ba44 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
1ba45 2a 7a 4e 61 6d 65 20 3d 20 63 6f 6e 74 65 78 74  *zName = context
1ba46 2d 3e 70 46 75 6e 63 2d 3e 7a 4e 61 6d 65 3b 0a  ->pFunc->zName;.
1ba47 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20    char *zErr;.  
1ba48 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1ba49 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
1ba4a 65 64 32 29 3b 0a 20 20 7a 45 72 72 20 3d 20 73  ed2);.  zErr = s
1ba4b 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a  qlite3_mprintf(.
1ba4c 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f        "unable to
1ba4d 20 75 73 65 20 66 75 6e 63 74 69 6f 6e 20 25 73   use function %s
1ba4e 20 69 6e 20 74 68 65 20 72 65 71 75 65 73 74 65   in the requeste
1ba4f 64 20 63 6f 6e 74 65 78 74 22 2c 20 7a 4e 61 6d  d context", zNam
1ba50 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  e);.  sqlite3_re
1ba51 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
1ba52 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20  xt, zErr, -1);. 
1ba53 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
1ba54 72 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  rr);.}../*.** Al
1ba55 6c 6f 63 61 74 65 20 6f 72 20 72 65 74 75 72 6e  locate or return
1ba56 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 63   the aggregate c
1ba57 6f 6e 74 65 78 74 20 66 6f 72 20 61 20 75 73 65  ontext for a use
1ba58 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 6e  r function.  A n
1ba59 65 77 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73  ew.** context is
1ba5a 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68   allocated on th
1ba5b 65 20 66 69 72 73 74 20 63 61 6c 6c 2e 20 20 53  e first call.  S
1ba5c 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20  ubsequent calls 
1ba5d 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 73 61  return the.** sa
1ba5e 6d 65 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20  me context that 
1ba5f 77 61 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20  was returned on 
1ba60 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a  prior calls..*/.
1ba61 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1ba62 2a 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  *sqlite3_aggrega
1ba63 74 65 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69 74  te_context(sqlit
1ba64 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69  e3_context *p, i
1ba65 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d  nt nByte){.  Mem
1ba66 20 2a 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74   *pMem;.  assert
1ba67 28 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20  ( p && p->pFunc 
1ba68 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 53 74  && p->pFunc->xSt
1ba69 65 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ep );.  assert( 
1ba6a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1ba6b 6c 64 28 70 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  ld(p->s.db->mute
1ba6c 78 29 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 70  x) );.  pMem = p
1ba6d 2d 3e 70 4d 65 6d 3b 0a 20 20 69 66 28 20 28 70  ->pMem;.  if( (p
1ba6e 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
1ba6f 5f 41 67 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20  _Agg)==0 ){.    
1ba70 69 66 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a  if( nByte==0 ){.
1ba71 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1ba72 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72  eMemReleaseExter
1ba73 6e 61 6c 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20  nal(pMem);.     
1ba74 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1ba75 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 70  EM_Null;.      p
1ba76 4d 65 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20  Mem->z = 0;.    
1ba77 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
1ba78 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1ba79 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 3b  pMem, nByte, 0);
1ba7a 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
1ba7b 67 73 20 3d 20 4d 45 4d 5f 41 67 67 3b 0a 20 20  gs = MEM_Agg;.  
1ba7c 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 44 65 66      pMem->u.pDef
1ba7d 20 3d 20 70 2d 3e 70 46 75 6e 63 3b 0a 20 20 20   = p->pFunc;.   
1ba7e 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29     if( pMem->z )
1ba7f 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  {.        memset
1ba80 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79  (pMem->z, 0, nBy
1ba81 74 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  te);.      }.   
1ba82 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1ba83 28 76 6f 69 64 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a  (void*)pMem->z;.
1ba84 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1ba85 74 68 65 20 61 75 78 69 6c 61 72 79 20 64 61 74  the auxilary dat
1ba86 61 20 70 6f 69 6e 74 65 72 2c 20 69 66 20 61 6e  a pointer, if an
1ba87 79 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 27  y, for the iArg'
1ba88 74 68 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a  th argument to.*
1ba89 2a 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74  * the user-funct
1ba8a 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70  ion defined by p
1ba8b 43 74 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  Ctx..*/.SQLITE_A
1ba8c 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
1ba8d 5f 67 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c  _get_auxdata(sql
1ba8e 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
1ba8f 74 78 2c 20 69 6e 74 20 69 41 72 67 29 7b 0a 20  tx, int iArg){. 
1ba90 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65   VdbeFunc *pVdbe
1ba91 46 75 6e 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  Func;..  assert(
1ba92 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1ba93 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1ba94 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 56 64 62  mutex) );.  pVdb
1ba95 65 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56  eFunc = pCtx->pV
1ba96 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 21  dbeFunc;.  if( !
1ba97 70 56 64 62 65 46 75 6e 63 20 7c 7c 20 69 41 72  pVdbeFunc || iAr
1ba98 67 3e 3d 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41  g>=pVdbeFunc->nA
1ba99 75 78 20 7c 7c 20 69 41 72 67 3c 30 20 29 7b 0a  ux || iArg<0 ){.
1ba9a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1ba9b 7d 0a 20 20 72 65 74 75 72 6e 20 70 56 64 62 65  }.  return pVdbe
1ba9c 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67  Func->apAux[iArg
1ba9d 5d 2e 70 41 75 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ].pAux;.}../*.**
1ba9e 20 53 65 74 20 74 68 65 20 61 75 78 69 6c 61 72   Set the auxilar
1ba9f 79 20 64 61 74 61 20 70 6f 69 6e 74 65 72 20 61  y data pointer a
1baa0 6e 64 20 64 65 6c 65 74 65 20 66 75 6e 63 74 69  nd delete functi
1baa1 6f 6e 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67  on, for the iArg
1baa2 27 74 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20  'th.** argument 
1baa3 74 6f 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63  to the user-func
1baa4 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20  tion defined by 
1baa5 70 43 74 78 2e 20 41 6e 79 20 70 72 65 76 69 6f  pCtx. Any previo
1baa6 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 64  us value is.** d
1baa7 65 6c 65 74 65 64 20 62 79 20 63 61 6c 6c 69 6e  eleted by callin
1baa8 67 20 74 68 65 20 64 65 6c 65 74 65 20 66 75 6e  g the delete fun
1baa9 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20  ction specified 
1baaa 77 68 65 6e 20 69 74 20 77 61 73 20 73 65 74 2e  when it was set.
1baab 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
1baac 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  oid sqlite3_set_
1baad 61 75 78 64 61 74 61 28 0a 20 20 73 71 6c 69 74  auxdata(.  sqlit
1baae 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1baaf 2c 20 0a 20 20 69 6e 74 20 69 41 72 67 2c 20 0a  , .  int iArg, .
1bab0 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 0a 20    void *pAux, . 
1bab1 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 29   void (*xDelete)
1bab2 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 74 72  (void*).){.  str
1bab3 75 63 74 20 41 75 78 44 61 74 61 20 2a 70 41 75  uct AuxData *pAu
1bab4 78 44 61 74 61 3b 0a 20 20 56 64 62 65 46 75 6e  xData;.  VdbeFun
1bab5 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 20 20  c *pVdbeFunc;.  
1bab6 69 66 28 20 69 41 72 67 3c 30 20 29 20 67 6f 74  if( iArg<0 ) got
1bab7 6f 20 66 61 69 6c 65 64 3b 0a 0a 20 20 61 73 73  o failed;..  ass
1bab8 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1bab9 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1baba 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1babb 70 56 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78  pVdbeFunc = pCtx
1babc 2d 3e 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69  ->pVdbeFunc;.  i
1babd 66 28 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c  f( !pVdbeFunc ||
1babe 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78   pVdbeFunc->nAux
1babf 3c 3d 69 41 72 67 20 29 7b 0a 20 20 20 20 69 6e  <=iArg ){.    in
1bac0 74 20 6e 41 75 78 20 3d 20 28 70 56 64 62 65 46  t nAux = (pVdbeF
1bac1 75 6e 63 20 3f 20 70 56 64 62 65 46 75 6e 63 2d  unc ? pVdbeFunc-
1bac2 3e 6e 41 75 78 20 3a 20 30 29 3b 0a 20 20 20 20  >nAux : 0);.    
1bac3 69 6e 74 20 6e 4d 61 6c 6c 6f 63 20 3d 20 73 69  int nMalloc = si
1bac4 7a 65 6f 66 28 56 64 62 65 46 75 6e 63 29 20 2b  zeof(VdbeFunc) +
1bac5 20 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41   sizeof(struct A
1bac6 75 78 44 61 74 61 29 2a 69 41 72 67 3b 0a 20 20  uxData)*iArg;.  
1bac7 20 20 70 56 64 62 65 46 75 6e 63 20 3d 20 73 71    pVdbeFunc = sq
1bac8 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70  lite3DbRealloc(p
1bac9 43 74 78 2d 3e 73 2e 64 62 2c 20 70 56 64 62 65  Ctx->s.db, pVdbe
1baca 46 75 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 29 3b 0a  Func, nMalloc);.
1bacb 20 20 20 20 69 66 28 20 21 70 56 64 62 65 46 75      if( !pVdbeFu
1bacc 6e 63 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  nc ){.      goto
1bacd 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20   failed;.    }. 
1bace 20 20 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75     pCtx->pVdbeFu
1bacf 6e 63 20 3d 20 70 56 64 62 65 46 75 6e 63 3b 0a  nc = pVdbeFunc;.
1bad0 20 20 20 20 6d 65 6d 73 65 74 28 26 70 56 64 62      memset(&pVdb
1bad1 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 6e 41 75  eFunc->apAux[nAu
1bad2 78 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 74  x], 0, sizeof(st
1bad3 72 75 63 74 20 41 75 78 44 61 74 61 29 2a 28 69  ruct AuxData)*(i
1bad4 41 72 67 2b 31 2d 6e 41 75 78 29 29 3b 0a 20 20  Arg+1-nAux));.  
1bad5 20 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75    pVdbeFunc->nAu
1bad6 78 20 3d 20 69 41 72 67 2b 31 3b 0a 20 20 20 20  x = iArg+1;.    
1bad7 70 56 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63  pVdbeFunc->pFunc
1bad8 20 3d 20 70 43 74 78 2d 3e 70 46 75 6e 63 3b 0a   = pCtx->pFunc;.
1bad9 20 20 7d 0a 0a 20 20 70 41 75 78 44 61 74 61 20    }..  pAuxData 
1bada 3d 20 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70  = &pVdbeFunc->ap
1badb 41 75 78 5b 69 41 72 67 5d 3b 0a 20 20 69 66 28  Aux[iArg];.  if(
1badc 20 70 41 75 78 44 61 74 61 2d 3e 70 41 75 78 20   pAuxData->pAux 
1badd 26 26 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65  && pAuxData->xDe
1bade 6c 65 74 65 20 29 7b 0a 20 20 20 20 70 41 75 78  lete ){.    pAux
1badf 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 28 70 41  Data->xDelete(pA
1bae0 75 78 44 61 74 61 2d 3e 70 41 75 78 29 3b 0a 20  uxData->pAux);. 
1bae1 20 7d 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 70   }.  pAuxData->p
1bae2 41 75 78 20 3d 20 70 41 75 78 3b 0a 20 20 70 41  Aux = pAux;.  pA
1bae3 75 78 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20  uxData->xDelete 
1bae4 3d 20 78 44 65 6c 65 74 65 3b 0a 20 20 72 65 74  = xDelete;.  ret
1bae5 75 72 6e 3b 0a 0a 66 61 69 6c 65 64 3a 0a 20 20  urn;..failed:.  
1bae6 69 66 28 20 78 44 65 6c 65 74 65 20 29 7b 0a 20  if( xDelete ){. 
1bae7 20 20 20 78 44 65 6c 65 74 65 28 70 41 75 78 29     xDelete(pAux)
1bae8 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66  ;.  }.}..#ifndef
1bae9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
1baea 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65  RECATED./*.** Re
1baeb 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
1baec 6f 66 20 74 69 6d 65 73 20 74 68 65 20 53 74 65  of times the Ste
1baed 70 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20 61 20  p function of a 
1baee 61 67 67 72 65 67 61 74 65 20 68 61 73 20 62 65  aggregate has be
1baef 65 6e 20 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a  en .** called..*
1baf0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1baf1 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64  on is deprecated
1baf2 2e 20 20 44 6f 20 6e 6f 74 20 75 73 65 20 69 74  .  Do not use it
1baf3 20 66 6f 72 20 6e 65 77 20 63 6f 64 65 2e 20 20   for new code.  
1baf4 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 65  It is.** provide
1baf5 20 6f 6e 6c 79 20 74 6f 20 61 76 6f 69 64 20 62   only to avoid b
1baf6 72 65 61 6b 69 6e 67 20 6c 65 67 61 63 79 20 63  reaking legacy c
1baf7 6f 64 65 2e 20 20 4e 65 77 20 61 67 67 72 65 67  ode.  New aggreg
1baf8 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ate function.** 
1baf9 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
1bafa 73 68 6f 75 6c 64 20 6b 65 65 70 20 74 68 65 69  should keep thei
1bafb 72 20 6f 77 6e 20 63 6f 75 6e 74 73 20 77 69 74  r own counts wit
1bafc 68 69 6e 20 74 68 65 69 72 20 61 67 67 72 65 67  hin their aggreg
1bafd 61 74 65 0a 2a 2a 20 63 6f 6e 74 65 78 74 2e 0a  ate.** context..
1bafe 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1baff 74 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  t sqlite3_aggreg
1bb00 61 74 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ate_count(sqlite
1bb01 33 5f 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20  3_context *p){. 
1bb02 20 61 73 73 65 72 74 28 20 70 20 26 26 20 70 2d   assert( p && p-
1bb03 3e 70 4d 65 6d 20 26 26 20 70 2d 3e 70 46 75 6e  >pMem && p->pFun
1bb04 63 20 26 26 20 70 2d 3e 70 46 75 6e 63 2d 3e 78  c && p->pFunc->x
1bb05 53 74 65 70 20 29 3b 0a 20 20 72 65 74 75 72 6e  Step );.  return
1bb06 20 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0a 7d 0a 23   p->pMem->n;.}.#
1bb07 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  endif../*.** Ret
1bb08 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1bb09 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
1bb0a 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20   result set for 
1bb0b 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 70 53  the statement pS
1bb0c 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  tmt..*/.SQLITE_A
1bb0d 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
1bb0e 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73 71 6c 69  olumn_count(sqli
1bb0f 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
1bb10 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20  {.  Vdbe *pVm = 
1bb11 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20  (Vdbe *)pStmt;. 
1bb12 20 72 65 74 75 72 6e 20 70 56 6d 20 3f 20 70 56   return pVm ? pV
1bb13 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3a 20  m->nResColumn : 
1bb14 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  0;.}../*.** Retu
1bb15 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
1bb16 20 76 61 6c 75 65 73 20 61 76 61 69 6c 61 62 6c   values availabl
1bb17 65 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65  e from the curre
1bb18 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 0a 2a 2a  nt row of the.**
1bb19 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75   currently execu
1bb1a 74 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70  ting statement p
1bb1b 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Stmt..*/.SQLITE_
1bb1c 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1bb1d 64 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74  data_count(sqlit
1bb1e 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1bb1f 0a 20 20 56 64 62 65 20 2a 70 56 6d 20 3d 20 28  .  Vdbe *pVm = (
1bb20 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
1bb21 69 66 28 20 70 56 6d 3d 3d 30 20 7c 7c 20 70 56  if( pVm==0 || pV
1bb22 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 3d 3d 30  m->pResultSet==0
1bb23 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 72   ) return 0;.  r
1bb24 65 74 75 72 6e 20 70 56 6d 2d 3e 6e 52 65 73 43  eturn pVm->nResC
1bb25 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  olumn;.}.../*.**
1bb26 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
1bb27 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20 6f 66 20   column iCol of 
1bb28 74 68 65 20 67 69 76 65 6e 20 73 74 61 74 65 6d  the given statem
1bb29 65 6e 74 20 69 73 20 76 61 6c 69 64 2e 20 20 49  ent is valid.  I
1bb2a 66 0a 2a 2a 20 69 74 20 69 73 2c 20 72 65 74 75  f.** it is, retu
1bb2b 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
1bb2c 74 68 65 20 4d 65 6d 20 66 6f 72 20 74 68 65 20  the Mem for the 
1bb2d 76 61 6c 75 65 20 6f 66 20 74 68 61 74 20 63 6f  value of that co
1bb2e 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 20 69 43 6f 6c  lumn..** If iCol
1bb2f 20 69 73 20 6e 6f 74 20 76 61 6c 69 64 2c 20 72   is not valid, r
1bb30 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
1bb31 74 6f 20 61 20 4d 65 6d 20 77 68 69 63 68 20 68  to a Mem which h
1bb32 61 73 20 61 20 76 61 6c 75 65 0a 2a 2a 20 6f 66  as a value.** of
1bb33 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63   NULL..*/.static
1bb34 20 4d 65 6d 20 2a 63 6f 6c 75 6d 6e 4d 65 6d 28   Mem *columnMem(
1bb35 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1bb36 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 56  tmt, int i){.  V
1bb37 64 62 65 20 2a 70 56 6d 3b 0a 20 20 69 6e 74 20  dbe *pVm;.  int 
1bb38 76 61 6c 73 3b 0a 20 20 4d 65 6d 20 2a 70 4f 75  vals;.  Mem *pOu
1bb39 74 3b 0a 0a 20 20 70 56 6d 20 3d 20 28 56 64 62  t;..  pVm = (Vdb
1bb3a 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28  e *)pStmt;.  if(
1bb3b 20 70 56 6d 20 26 26 20 70 56 6d 2d 3e 70 52 65   pVm && pVm->pRe
1bb3c 73 75 6c 74 53 65 74 21 3d 30 20 26 26 20 69 3c  sultSet!=0 && i<
1bb3d 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20  pVm->nResColumn 
1bb3e 26 26 20 69 3e 3d 30 20 29 7b 0a 20 20 20 20 73  && i>=0 ){.    s
1bb3f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
1bb40 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75 74 65  er(pVm->db->mute
1bb41 78 29 3b 0a 20 20 20 20 76 61 6c 73 20 3d 20 73  x);.    vals = s
1bb42 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
1bb43 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 70 4f  t(pStmt);.    pO
1bb44 75 74 20 3d 20 26 70 56 6d 2d 3e 70 52 65 73 75  ut = &pVm->pResu
1bb45 6c 74 53 65 74 5b 69 5d 3b 0a 20 20 7d 65 6c 73  ltSet[i];.  }els
1bb46 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  e{.    /* If the
1bb47 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73   value passed as
1bb48 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
1bb49 6d 65 6e 74 20 69 73 20 6f 75 74 20 6f 66 20 72  ment is out of r
1bb4a 61 6e 67 65 2c 20 72 65 74 75 72 6e 0a 20 20 20  ange, return.   
1bb4b 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   ** a pointer to
1bb4c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
1bb4d 74 61 74 69 63 20 4d 65 6d 20 6f 62 6a 65 63 74  tatic Mem object
1bb4e 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e 73 20   which contains 
1bb4f 74 68 65 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65  the.    ** value
1bb50 20 53 51 4c 20 4e 55 4c 4c 2e 20 45 76 65 6e 20   SQL NULL. Even 
1bb51 74 68 6f 75 67 68 20 74 68 65 20 4d 65 6d 20 73  though the Mem s
1bb52 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
1bb53 73 20 61 6e 20 65 6c 65 6d 65 6e 74 0a 20 20 20  s an element.   
1bb54 20 2a 2a 20 6f 66 20 74 79 70 65 20 69 36 34 2c   ** of type i64,
1bb55 20 6f 6e 20 63 65 72 74 61 69 6e 20 61 72 63 68   on certain arch
1bb56 69 74 65 63 74 75 72 65 20 28 78 38 36 29 20 77  itecture (x86) w
1bb57 69 74 68 20 63 65 72 74 61 69 6e 20 63 6f 6d 70  ith certain comp
1bb58 69 6c 65 72 0a 20 20 20 20 2a 2a 20 73 77 69 74  iler.    ** swit
1bb59 63 68 65 73 20 28 2d 4f 73 29 2c 20 67 63 63 20  ches (-Os), gcc 
1bb5a 6d 61 79 20 61 6c 69 67 6e 20 74 68 69 73 20 4d  may align this M
1bb5b 65 6d 20 6f 62 6a 65 63 74 20 6f 6e 20 61 20 34  em object on a 4
1bb5c 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 0a 20  -byte boundary. 
1bb5d 20 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66     ** instead of
1bb5e 20 61 6e 20 38 2d 62 79 74 65 20 6f 6e 65 2e 20   an 8-byte one. 
1bb5f 54 68 69 73 20 61 6c 6c 20 77 6f 72 6b 73 20 66  This all works f
1bb60 69 6e 65 2c 20 65 78 63 65 70 74 20 74 68 61 74  ine, except that
1bb61 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 72 75 6e   when.    ** run
1bb62 6e 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54 45  ning with SQLITE
1bb63 5f 44 45 42 55 47 20 64 65 66 69 6e 65 64 20 74  _DEBUG defined t
1bb64 68 65 20 53 51 4c 69 74 65 20 63 6f 64 65 20 73  he SQLite code s
1bb65 6f 6d 65 74 69 6d 65 73 20 61 73 73 65 72 74 28  ometimes assert(
1bb66 29 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 61  )s.    ** that a
1bb67 20 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20 69   Mem structure i
1bb68 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 6e 20  s located on an 
1bb69 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79 2e  8-byte boundary.
1bb6a 20 54 6f 20 70 72 65 76 65 6e 74 0a 20 20 20 20   To prevent.    
1bb6b 2a 2a 20 74 68 69 73 20 61 73 73 65 72 74 28 29  ** this assert()
1bb6c 20 66 72 6f 6d 20 66 61 69 6c 69 6e 67 2c 20 77   from failing, w
1bb6d 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 77 69 74  hen building wit
1bb6e 68 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 64  h SQLITE_DEBUG d
1bb6f 65 66 69 6e 65 64 0a 20 20 20 20 2a 2a 20 75 73  efined.    ** us
1bb70 69 6e 67 20 67 63 63 2c 20 66 6f 72 63 65 20 6e  ing gcc, force n
1bb71 75 6c 6c 4d 65 6d 20 74 6f 20 62 65 20 38 2d 62  ullMem to be 8-b
1bb72 79 74 65 20 61 6c 69 67 6e 65 64 20 75 73 69 6e  yte aligned usin
1bb73 67 20 74 68 65 20 6d 61 67 69 63 61 6c 0a 20 20  g the magical.  
1bb74 20 20 2a 2a 20 5f 5f 61 74 74 72 69 62 75 74 65    ** __attribute
1bb75 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38 29 29 29  __((aligned(8)))
1bb76 20 6d 61 63 72 6f 2e 20 20 2a 2f 0a 20 20 20 20   macro.  */.    
1bb77 73 74 61 74 69 63 20 63 6f 6e 73 74 20 4d 65 6d  static const Mem
1bb78 20 6e 75 6c 6c 4d 65 6d 20 0a 23 69 66 20 64 65   nullMem .#if de
1bb79 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
1bb7a 55 47 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f  UG) && defined(_
1bb7b 5f 47 4e 55 43 5f 5f 29 0a 20 20 20 20 20 20 5f  _GNUC__).      _
1bb7c 5f 61 74 74 72 69 62 75 74 65 5f 5f 28 28 61 6c  _attribute__((al
1bb7d 69 67 6e 65 64 28 38 29 29 29 20 0a 23 65 6e 64  igned(8))) .#end
1bb7e 69 66 0a 20 20 20 20 20 20 3d 20 7b 7b 30 7d 2c  if.      = {{0},
1bb7f 20 28 64 6f 75 62 6c 65 29 30 2c 20 30 2c 20 22   (double)0, 0, "
1bb80 22 2c 20 30 2c 20 4d 45 4d 5f 4e 75 6c 6c 2c 20  ", 0, MEM_Null, 
1bb81 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 30 2c 20  SQLITE_NULL, 0, 
1bb82 30 2c 20 30 20 7d 3b 0a 0a 20 20 20 20 69 66 28  0, 0 };..    if(
1bb83 20 70 56 6d 20 26 26 20 41 4c 57 41 59 53 28 70   pVm && ALWAYS(p
1bb84 56 6d 2d 3e 64 62 29 20 29 7b 0a 20 20 20 20 20  Vm->db) ){.     
1bb85 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1bb86 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d 75  nter(pVm->db->mu
1bb87 74 65 78 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  tex);.      sqli
1bb88 74 65 33 45 72 72 6f 72 28 70 56 6d 2d 3e 64 62  te3Error(pVm->db
1bb89 2c 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20  , SQLITE_RANGE, 
1bb8a 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f  0);.    }.    pO
1bb8b 75 74 20 3d 20 28 4d 65 6d 2a 29 26 6e 75 6c 6c  ut = (Mem*)&null
1bb8c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  Mem;.  }.  retur
1bb8d 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pOut;.}../*.**
1bb8e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
1bb8f 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 69  s called after i
1bb90 6e 76 6f 6b 69 6e 67 20 61 6e 20 73 71 6c 69 74  nvoking an sqlit
1bb91 65 33 5f 76 61 6c 75 65 5f 58 58 58 20 66 75 6e  e3_value_XXX fun
1bb92 63 74 69 6f 6e 20 6f 6e 20 61 20 0a 2a 2a 20 63  ction on a .** c
1bb93 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 28 69 2e 65  olumn value (i.e
1bb94 2e 20 61 20 76 61 6c 75 65 20 72 65 74 75 72 6e  . a value return
1bb95 65 64 20 62 79 20 65 76 61 6c 75 61 74 69 6e 67  ed by evaluating
1bb96 20 61 6e 20 53 51 4c 20 65 78 70 72 65 73 73 69   an SQL expressi
1bb97 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a 20 73 65 6c  on in the.** sel
1bb98 65 63 74 20 6c 69 73 74 20 6f 66 20 61 20 53 45  ect list of a SE
1bb99 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 29 20  LECT statement) 
1bb9a 74 68 61 74 20 6d 61 79 20 63 61 75 73 65 20 61  that may cause a
1bb9b 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
1bb9c 65 2e 20 49 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63  e. If .** malloc
1bb9d 28 29 20 68 61 73 20 66 61 69 6c 65 64 2c 20 74  () has failed, t
1bb9e 68 65 20 74 68 72 65 61 64 73 20 6d 61 6c 6c 6f  he threads mallo
1bb9f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69 73 20  cFailed flag is 
1bba0 63 6c 65 61 72 65 64 20 61 6e 64 20 74 68 65 20  cleared and the 
1bba1 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 64 65 20 6f  result.** code o
1bba2 66 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d  f statement pStm
1bba3 74 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  t set to SQLITE_
1bba4 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a 20 53 70 65  NOMEM..**.** Spe
1bba5 63 69 66 69 63 61 6c 6c 79 2c 20 74 68 69 73 20  cifically, this 
1bba6 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77  is called from w
1bba7 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ithin:.**.**    
1bba8 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1bba9 69 6e 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  int().**     sql
1bbaa 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
1bbab 34 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  4().**     sqlit
1bbac 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29  e3_column_text()
1bbad 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .**     sqlite3_
1bbae 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a  column_text16().
1bbaf 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  **     sqlite3_c
1bbb0 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29 0a 2a 2a 20  olumn_real().** 
1bbb1 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
1bbb2 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 20 20  mn_bytes().**   
1bbb3 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
1bbb4 5f 62 79 74 65 73 31 36 28 29 0a 2a 2a 0a 2a 2a  _bytes16().**.**
1bbb5 20 42 75 74 20 6e 6f 74 20 66 6f 72 20 73 71 6c   But not for sql
1bbb6 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
1bbb7 28 29 2c 20 77 68 69 63 68 20 6e 65 76 65 72 20  (), which never 
1bbb8 63 61 6c 6c 73 20 6d 61 6c 6c 6f 63 28 29 2e 0a  calls malloc()..
1bbb9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
1bbba 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
1bbbb 72 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  re(sqlite3_stmt 
1bbbc 2a 70 53 74 6d 74 29 0a 7b 0a 20 20 2f 2a 20 49  *pStmt).{.  /* I
1bbbd 66 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65  f malloc() faile
1bbbe 64 20 64 75 72 69 6e 67 20 61 6e 20 65 6e 63 6f  d during an enco
1bbbf 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ding conversion 
1bbc0 77 69 74 68 69 6e 20 61 6e 0a 20 20 2a 2a 20 73  within an.  ** s
1bbc1 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 58 58  qlite3_column_XX
1bbc2 58 20 41 50 49 2c 20 74 68 65 6e 20 73 65 74 20  X API, then set 
1bbc3 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20  the return code 
1bbc4 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  of the statement
1bbc5 20 74 6f 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f   to.  ** SQLITE_
1bbc6 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e 65 78 74 20  NOMEM. The next 
1bbc7 63 61 6c 6c 20 74 6f 20 5f 73 74 65 70 28 29 20  call to _step() 
1bbc8 28 69 66 20 61 6e 79 29 20 77 69 6c 6c 20 72 65  (if any) will re
1bbc9 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1bbca 52 0a 20 20 2a 2a 20 61 6e 64 20 5f 66 69 6e 61  R.  ** and _fina
1bbcb 6c 69 7a 65 28 29 20 77 69 6c 6c 20 72 65 74 75  lize() will retu
1bbcc 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 20 2a 2f 0a 20  rn NOMEM..  */. 
1bbcd 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
1bbce 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20   *)pStmt;.  if( 
1bbcf 70 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  p ){.    p->rc =
1bbd0 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
1bbd1 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 29 3b 0a 20  p->db, p->rc);. 
1bbd2 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1bbd3 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75  _leave(p->db->mu
1bbd4 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a  tex);.  }.}../**
1bbd5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bbd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74  ********** sqlit
1bbd7 65 33 5f 63 6f 6c 75 6d 6e 5f 20 20 2a 2a 2a 2a  e3_column_  ****
1bbd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bbd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
1bbda 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
1bbdb 74 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74  tines are used t
1bbdc 6f 20 61 63 63 65 73 73 20 65 6c 65 6d 65 6e 74  o access element
1bbdd 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  s of the current
1bbde 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 68 65 20 72   row.** in the r
1bbdf 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51  esult set..*/.SQ
1bbe0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
1bbe1 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
1bbe2 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33  umn_blob(sqlite3
1bbe3 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1bbe4 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f  t i){.  const vo
1bbe5 69 64 20 2a 76 61 6c 3b 0a 20 20 76 61 6c 20 3d  id *val;.  val =
1bbe6 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
1bbe7 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70  lob( columnMem(p
1bbe8 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 2f 2a 20  Stmt,i) );.  /* 
1bbe9 45 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 72  Even though ther
1bbea 65 20 69 73 20 6e 6f 20 65 6e 63 6f 64 69 6e 67  e is no encoding
1bbeb 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 20 76 61 6c   conversion, val
1bbec 75 65 5f 62 6c 6f 62 28 29 20 6d 69 67 68 74 0a  ue_blob() might.
1bbed 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 63 61 6c    ** need to cal
1bbee 6c 20 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 65 78  l malloc() to ex
1bbef 70 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20  pand the result 
1bbf0 6f 66 20 61 20 7a 65 72 6f 62 6c 6f 62 28 29 20  of a zeroblob() 
1bbf1 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  .  ** expression
1bbf2 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f 6c 75 6d 6e  . .  */.  column
1bbf3 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53  MallocFailure(pS
1bbf4 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76  tmt);.  return v
1bbf5 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  al;.}.SQLITE_API
1bbf6 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c   int sqlite3_col
1bbf7 75 6d 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65  umn_bytes(sqlite
1bbf8 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1bbf9 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c  nt i){.  int val
1bbfa 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1bbfb 5f 62 79 74 65 73 28 20 63 6f 6c 75 6d 6e 4d 65  _bytes( columnMe
1bbfc 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20  m(pStmt,i) );.  
1bbfd 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c  columnMallocFail
1bbfe 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65  ure(pStmt);.  re
1bbff 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49  turn val;.}.SQLI
1bc00 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1bc01 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
1bc02 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  6(sqlite3_stmt *
1bc03 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
1bc04 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69 74   int val = sqlit
1bc05 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36  e3_value_bytes16
1bc06 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d  ( columnMem(pStm
1bc07 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e  t,i) );.  column
1bc08 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53  MallocFailure(pS
1bc09 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76  tmt);.  return v
1bc0a 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  al;.}.SQLITE_API
1bc0b 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 5f   double sqlite3_
1bc0c 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 73 71  column_double(sq
1bc0d 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bc0e 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 64 6f 75  t, int i){.  dou
1bc0f 62 6c 65 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  ble val = sqlite
1bc10 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 20  3_value_double( 
1bc11 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c  columnMem(pStmt,
1bc12 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61  i) );.  columnMa
1bc13 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d  llocFailure(pStm
1bc14 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c  t);.  return val
1bc15 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
1bc16 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
1bc17 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74  n_int(sqlite3_st
1bc18 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
1bc19 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73  ){.  int val = s
1bc1a 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
1bc1b 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d  ( columnMem(pStm
1bc1c 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e  t,i) );.  column
1bc1d 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53  MallocFailure(pS
1bc1e 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76  tmt);.  return v
1bc1f 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  al;.}.SQLITE_API
1bc20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71   sqlite_int64 sq
1bc21 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
1bc22 36 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  64(sqlite3_stmt 
1bc23 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
1bc24 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76    sqlite_int64 v
1bc25 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
1bc26 75 65 5f 69 6e 74 36 34 28 20 63 6f 6c 75 6d 6e  ue_int64( column
1bc27 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a  Mem(pStmt,i) );.
1bc28 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61    columnMallocFa
1bc29 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20  ilure(pStmt);.  
1bc2a 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51  return val;.}.SQ
1bc2b 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 75  LITE_API const u
1bc2c 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73 71  nsigned char *sq
1bc2d 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
1bc2e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
1bc2f 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
1bc30 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
1bc31 63 68 61 72 20 2a 76 61 6c 20 3d 20 73 71 6c 69  char *val = sqli
1bc32 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 20  te3_value_text( 
1bc33 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c  columnMem(pStmt,
1bc34 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61  i) );.  columnMa
1bc35 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d  llocFailure(pStm
1bc36 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c  t);.  return val
1bc37 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  ;.}.SQLITE_API s
1bc38 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71  qlite3_value *sq
1bc39 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
1bc3a 75 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ue(sqlite3_stmt 
1bc3b 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
1bc3c 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20 63 6f    Mem *pOut = co
1bc3d 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 20 69  lumnMem(pStmt, i
1bc3e 29 3b 0a 20 20 69 66 28 20 70 4f 75 74 2d 3e 66  );.  if( pOut->f
1bc3f 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69 63 20  lags&MEM_Static 
1bc40 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61  ){.    pOut->fla
1bc41 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 61 74 69  gs &= ~MEM_Stati
1bc42 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61  c;.    pOut->fla
1bc43 67 73 20 7c 3d 20 4d 45 4d 5f 45 70 68 65 6d 3b  gs |= MEM_Ephem;
1bc44 0a 20 20 7d 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c  .  }.  columnMal
1bc45 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74  locFailure(pStmt
1bc46 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 73 71 6c  );.  return (sql
1bc47 69 74 65 33 5f 76 61 6c 75 65 20 2a 29 70 4f 75  ite3_value *)pOu
1bc48 74 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  t;.}.#ifndef SQL
1bc49 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
1bc4a 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
1bc4b 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
1bc4c 6c 75 6d 6e 5f 74 65 78 74 31 36 28 73 71 6c 69  lumn_text16(sqli
1bc4d 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bc4e 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74   int i){.  const
1bc4f 20 76 6f 69 64 20 2a 76 61 6c 20 3d 20 73 71 6c   void *val = sql
1bc50 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
1bc51 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  6( columnMem(pSt
1bc52 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
1bc53 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
1bc54 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
1bc55 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  val;.}.#endif /*
1bc56 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1bc57 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  16 */.SQLITE_API
1bc58 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c   int sqlite3_col
1bc59 75 6d 6e 5f 74 79 70 65 28 73 71 6c 69 74 65 33  umn_type(sqlite3
1bc5a 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1bc5b 74 20 69 29 7b 0a 20 20 69 6e 74 20 69 54 79 70  t i){.  int iTyp
1bc5c 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
1bc5d 65 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d 65  e_type( columnMe
1bc5e 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20  m(pStmt,i) );.  
1bc5f 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c  columnMallocFail
1bc60 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65  ure(pStmt);.  re
1bc61 74 75 72 6e 20 69 54 79 70 65 3b 0a 7d 0a 0a 2f  turn iType;.}../
1bc62 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1bc63 66 75 6e 63 74 69 6f 6e 20 69 73 20 65 78 70 65  function is expe
1bc64 72 69 6d 65 6e 74 61 6c 20 61 6e 64 20 73 75 62  rimental and sub
1bc65 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f  ject to change o
1bc66 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 2a 2f 0a  r.** removal */.
1bc67 2f 2a 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f  /*int sqlite3_co
1bc68 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63 5f 74 79 70  lumn_numeric_typ
1bc69 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1bc6a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 2a  pStmt, int i){.*
1bc6b 2a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  *  return sqlite
1bc6c 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
1bc6d 74 79 70 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  type( columnMem(
1bc6e 70 53 74 6d 74 2c 69 29 20 29 3b 0a 2a 2a 7d 0a  pStmt,i) );.**}.
1bc6f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72  */../*.** Conver
1bc70 74 20 74 68 65 20 4e 2d 74 68 20 65 6c 65 6d 65  t the N-th eleme
1bc71 6e 74 20 6f 66 20 70 53 74 6d 74 2d 3e 70 43 6f  nt of pStmt->pCo
1bc72 6c 4e 61 6d 65 5b 5d 20 69 6e 74 6f 20 61 20 73  lName[] into a s
1bc73 74 72 69 6e 67 20 75 73 69 6e 67 0a 2a 2a 20 78  tring using.** x
1bc74 46 75 6e 63 28 29 20 74 68 65 6e 20 72 65 74 75  Func() then retu
1bc75 72 6e 20 74 68 61 74 20 73 74 72 69 6e 67 2e 20  rn that string. 
1bc76 20 49 66 20 4e 20 69 73 20 6f 75 74 20 6f 66 20   If N is out of 
1bc77 72 61 6e 67 65 2c 20 72 65 74 75 72 6e 20 30 2e  range, return 0.
1bc78 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
1bc79 20 75 70 20 74 6f 20 35 20 6e 61 6d 65 73 20 66   up to 5 names f
1bc7a 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 20  or each column. 
1bc7b 20 75 73 65 54 79 70 65 20 64 65 74 65 72 6d 69   useType determi
1bc7c 6e 65 73 20 77 68 69 63 68 0a 2a 2a 20 6e 61 6d  nes which.** nam
1bc7d 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  e is returned.  
1bc7e 48 65 72 65 20 61 72 65 20 74 68 65 20 6e 61 6d  Here are the nam
1bc7f 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 30 20 20  es:.**.**    0  
1bc80 20 20 20 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6e      The column n
1bc81 61 6d 65 20 61 73 20 69 74 20 73 68 6f 75 6c 64  ame as it should
1bc82 20 62 65 20 64 69 73 70 6c 61 79 65 64 20 66 6f   be displayed fo
1bc83 72 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 31  r output.**    1
1bc84 20 20 20 20 20 20 54 68 65 20 64 61 74 61 74 79        The dataty
1bc85 70 65 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20  pe name for the 
1bc86 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 32 20 20  column.**    2  
1bc87 20 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20      The name of 
1bc88 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61  the database tha
1bc89 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72  t the column der
1bc8a 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20  ives from.**    
1bc8b 33 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20  3      The name 
1bc8c 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  of the table tha
1bc8d 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 72  t the column der
1bc8e 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 20 20 20 20  ives from.**    
1bc8f 34 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65 20  4      The name 
1bc90 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c  of the table col
1bc91 75 6d 6e 20 74 68 61 74 20 74 68 65 20 72 65 73  umn that the res
1bc92 75 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76  ult column deriv
1bc93 65 73 20 66 72 6f 6d 0a 2a 2a 0a 2a 2a 20 49 66  es from.**.** If
1bc94 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 6e   the result is n
1bc95 6f 74 20 61 20 73 69 6d 70 6c 65 20 63 6f 6c 75  ot a simple colu
1bc96 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 28 69 66  mn reference (if
1bc97 20 69 74 20 69 73 20 61 6e 20 65 78 70 72 65 73   it is an expres
1bc98 73 69 6f 6e 0a 2a 2a 20 6f 72 20 61 20 63 6f 6e  sion.** or a con
1bc99 73 74 61 6e 74 29 20 74 68 65 6e 20 75 73 65 54  stant) then useT
1bc9a 79 70 65 73 20 32 2c 20 33 2c 20 61 6e 64 20 34  ypes 2, 3, and 4
1bc9b 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f   return NULL..*/
1bc9c 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 76 6f  .static const vo
1bc9d 69 64 20 2a 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  id *columnName(.
1bc9e 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1bc9f 70 53 74 6d 74 2c 0a 20 20 69 6e 74 20 4e 2c 0a  pStmt,.  int N,.
1bca0 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 28 2a    const void *(*
1bca1 78 46 75 6e 63 29 28 4d 65 6d 2a 29 2c 0a 20 20  xFunc)(Mem*),.  
1bca2 69 6e 74 20 75 73 65 54 79 70 65 0a 29 7b 0a 20  int useType.){. 
1bca3 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 72 65 74   const void *ret
1bca4 20 3d 20 30 3b 0a 20 20 56 64 62 65 20 2a 70 20   = 0;.  Vdbe *p 
1bca5 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
1bca6 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69  .  int n;.  sqli
1bca7 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
1bca8 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 64 62  .  .  assert( db
1bca9 21 3d 30 20 29 3b 0a 20 20 6e 20 3d 20 73 71 6c  !=0 );.  n = sql
1bcaa 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  ite3_column_coun
1bcab 74 28 70 53 74 6d 74 29 3b 0a 20 20 69 66 28 20  t(pStmt);.  if( 
1bcac 4e 3c 6e 20 26 26 20 4e 3e 3d 30 20 29 7b 0a 20  N<n && N>=0 ){. 
1bcad 20 20 20 4e 20 2b 3d 20 75 73 65 54 79 70 65 2a     N += useType*
1bcae 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  n;.    sqlite3_m
1bcaf 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
1bcb0 75 74 65 78 29 3b 0a 20 20 20 20 61 73 73 65 72  utex);.    asser
1bcb1 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
1bcb2 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65  led==0 );.    re
1bcb3 74 20 3d 20 78 46 75 6e 63 28 26 70 2d 3e 61 43  t = xFunc(&p->aC
1bcb4 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 20 20 20 20  olName[N]);.    
1bcb5 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 20 6d 61 79   /* A malloc may
1bcb6 20 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73   have failed ins
1bcb7 69 64 65 20 6f 66 20 74 68 65 20 78 46 75 6e 63  ide of the xFunc
1bcb8 28 29 20 63 61 6c 6c 2e 20 49 66 20 74 68 69 73  () call. If this
1bcb9 0a 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20 63  .    ** is the c
1bcba 61 73 65 2c 20 63 6c 65 61 72 20 74 68 65 20 6d  ase, clear the m
1bcbb 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
1bcbc 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c   and return NULL
1bcbd 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1bcbe 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1bcbf 64 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  d ){.      db->m
1bcc0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b  allocFailed = 0;
1bcc1 0a 20 20 20 20 20 20 72 65 74 20 3d 20 30 3b 0a  .      ret = 0;.
1bcc2 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1bcc3 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
1bcc4 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20  ->mutex);.  }.  
1bcc5 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f  return ret;.}../
1bcc6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1bcc7 6e 61 6d 65 20 6f 66 20 74 68 65 20 4e 74 68 20  name of the Nth 
1bcc8 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65  column of the re
1bcc9 73 75 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65  sult set returne
1bcca 64 20 62 79 20 53 51 4c 0a 2a 2a 20 73 74 61 74  d by SQL.** stat
1bccb 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a  ement pStmt..*/.
1bccc 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
1bccd 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
1bcce 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74  olumn_name(sqlit
1bccf 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bcd0 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
1bcd1 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20   columnName(.   
1bcd2 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
1bcd3 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
1bcd4 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
1bcd5 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  _text, COLNAME_N
1bcd6 41 4d 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  AME);.}.#ifndef 
1bcd7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1bcd8 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  6.SQLITE_API con
1bcd9 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
1bcda 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73  _column_name16(s
1bcdb 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bcdc 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
1bcdd 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
1bcde 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
1bcdf 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
1bce0 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
1bce1 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
1bce2 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d 0a 23 65  NAME_NAME);.}.#e
1bce3 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73  ndif../*.** Cons
1bce4 74 72 61 69 6e 74 3a 20 20 49 66 20 79 6f 75 20  traint:  If you 
1bce5 68 61 76 65 20 45 4e 41 42 4c 45 5f 43 4f 4c 55  have ENABLE_COLU
1bce6 4d 4e 5f 4d 45 54 41 44 41 54 41 20 74 68 65 6e  MN_METADATA then
1bce7 20 79 6f 75 20 6d 75 73 74 0a 2a 2a 20 6e 6f 74   you must.** not
1bce8 20 64 65 66 69 6e 65 20 4f 4d 49 54 5f 44 45 43   define OMIT_DEC
1bce9 4c 54 59 50 45 2e 0a 2a 2f 0a 23 69 66 20 64 65  LTYPE..*/.#if de
1bcea 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1bceb 54 5f 44 45 43 4c 54 59 50 45 29 20 26 26 20 64  T_DECLTYPE) && d
1bcec 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
1bced 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
1bcee 44 41 54 41 29 0a 23 20 65 72 72 6f 72 20 22 4d  DATA).# error "M
1bcef 75 73 74 20 6e 6f 74 20 64 65 66 69 6e 65 20 62  ust not define b
1bcf0 6f 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  oth SQLITE_OMIT_
1bcf1 44 45 43 4c 54 59 50 45 20 5c 0a 20 20 20 20 20  DECLTYPE \.     
1bcf2 20 20 20 20 61 6e 64 20 53 51 4c 49 54 45 5f 45      and SQLITE_E
1bcf3 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54  NABLE_COLUMN_MET
1bcf4 41 44 41 54 41 22 0a 23 65 6e 64 69 66 0a 0a 23  ADATA".#endif..#
1bcf5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1bcf6 49 54 5f 44 45 43 4c 54 59 50 45 0a 2f 2a 0a 2a  IT_DECLTYPE./*.*
1bcf7 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c  * Return the col
1bcf8 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  umn declaration 
1bcf9 74 79 70 65 20 28 69 66 20 61 70 70 6c 69 63 61  type (if applica
1bcfa 62 6c 65 29 20 6f 66 20 74 68 65 20 27 69 27 74  ble) of the 'i't
1bcfb 68 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74  h column.** of t
1bcfc 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
1bcfd 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 70   SQL statement p
1bcfe 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Stmt..*/.SQLITE_
1bcff 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
1bd00 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
1bd01 65 63 6c 74 79 70 65 28 73 71 6c 69 74 65 33 5f  ecltype(sqlite3_
1bd02 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bd03 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
1bd04 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
1bd05 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
1bd06 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
1bd07 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bd08 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c  xt, COLNAME_DECL
1bd09 54 59 50 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66  TYPE);.}.#ifndef
1bd0a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1bd0b 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  16.SQLITE_API co
1bd0c 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
1bd0d 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
1bd0e 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  e16(sqlite3_stmt
1bd0f 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
1bd10 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
1bd11 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
1bd12 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
1bd13 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
1bd14 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
1bd15 2c 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59  , COLNAME_DECLTY
1bd16 50 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  PE);.}.#endif /*
1bd17 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1bd18 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  16 */.#endif /* 
1bd19 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
1bd1a 54 59 50 45 20 2a 2f 0a 0a 23 69 66 64 65 66 20  TYPE */..#ifdef 
1bd1b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
1bd1c 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 2f 2a  LUMN_METADATA./*
1bd1d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1bd1e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
1bd1f 61 73 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61  ase from which a
1bd20 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64   result column d
1bd21 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20  erives..** NULL 
1bd22 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
1bd23 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  he result column
1bd24 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f   is an expressio
1bd25 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72  n or constant or
1bd26 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73  .** anything els
1bd27 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61  e which is not a
1bd28 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66  n unabiguous ref
1bd29 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61  erence to a data
1bd2a 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  base column..*/.
1bd2b 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
1bd2c 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
1bd2d 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e  olumn_database_n
1bd2e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
1bd2f 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
1bd30 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
1bd31 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
1bd32 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
1bd33 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
1bd34 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20  te3_value_text, 
1bd35 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45  COLNAME_DATABASE
1bd36 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  );.}.#ifndef SQL
1bd37 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
1bd38 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
1bd39 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
1bd3a 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
1bd3b 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
1bd3c 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29  t *pStmt, int N)
1bd3d 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d  {.  return colum
1bd3e 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74  nName(.      pSt
1bd3f 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f  mt, N, (const vo
1bd40 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c  id*(*)(Mem*))sql
1bd41 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
1bd42 36 2c 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42  6, COLNAME_DATAB
1bd43 41 53 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  ASE);.}.#endif /
1bd44 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
1bd45 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  F16 */../*.** Re
1bd46 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  turn the name of
1bd47 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20   the table from 
1bd48 77 68 69 63 68 20 61 20 72 65 73 75 6c 74 20 63  which a result c
1bd49 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a  olumn derives..*
1bd4a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  * NULL is return
1bd4b 65 64 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  ed if the result
1bd4c 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78   column is an ex
1bd4d 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73  pression or cons
1bd4e 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68  tant or.** anyth
1bd4f 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68 20 69  ing else which i
1bd50 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69 67 75  s not an unabigu
1bd51 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20 74 6f  ous reference to
1bd52 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6c 75   a database colu
1bd53 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  mn..*/.SQLITE_AP
1bd54 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
1bd55 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
1bd56 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  le_name(sqlite3_
1bd57 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bd58 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
1bd59 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
1bd5a 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
1bd5b 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
1bd5c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bd5d 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c  xt, COLNAME_TABL
1bd5e 45 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51  E);.}.#ifndef SQ
1bd5f 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
1bd60 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
1bd61 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
1bd62 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
1bd63 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  16(sqlite3_stmt 
1bd64 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
1bd65 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
1bd66 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
1bd67 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
1bd68 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
1bd69 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c  e3_value_text16,
1bd6a 20 43 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b   COLNAME_TABLE);
1bd6b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1bd6c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
1bd6d 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  /../*.** Return 
1bd6e 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1bd6f 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 66 72 6f  table column fro
1bd70 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74  m which a result
1bd71 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e   column derives.
1bd72 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  .** NULL is retu
1bd73 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75  rned if the resu
1bd74 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20  lt column is an 
1bd75 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f  expression or co
1bd76 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79  nstant or.** any
1bd77 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68  thing else which
1bd78 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69   is not an unabi
1bd79 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20  guous reference 
1bd7a 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  to a database co
1bd7b 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lumn..*/.SQLITE_
1bd7c 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
1bd7d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
1bd7e 72 69 67 69 6e 5f 6e 61 6d 65 28 73 71 6c 69 74  rigin_name(sqlit
1bd7f 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bd80 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e  int N){.  return
1bd81 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20   columnName(.   
1bd82 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f     pStmt, N, (co
1bd83 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d  nst void*(*)(Mem
1bd84 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  *))sqlite3_value
1bd85 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45 5f 43  _text, COLNAME_C
1bd86 4f 4c 55 4d 4e 29 3b 0a 7d 0a 23 69 66 6e 64 65  OLUMN);.}.#ifnde
1bd87 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
1bd88 46 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  F16.SQLITE_API c
1bd89 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
1bd8a 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
1bd8b 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f  _name16(sqlite3_
1bd8c 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bd8d 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
1bd8e 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
1bd8f 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
1bd90 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
1bd91 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bd92 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f  xt16, COLNAME_CO
1bd93 4c 55 4d 4e 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  LUMN);.}.#endif 
1bd94 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
1bd95 54 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  TF16 */.#endif /
1bd96 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
1bd97 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 20  COLUMN_METADATA 
1bd98 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */.../**********
1bd99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd9a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69  ***** sqlite3_bi
1bd9b 6e 64 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nd_  ***********
1bd9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bd9d 0a 2a 2a 20 0a 2a 2a 20 52 6f 75 74 69 6e 65 73  .** .** Routines
1bd9e 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 68 20   used to attach 
1bd9f 76 61 6c 75 65 73 20 74 6f 20 77 69 6c 64 63 61  values to wildca
1bda0 72 64 73 20 69 6e 20 61 20 63 6f 6d 70 69 6c 65  rds in a compile
1bda1 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
1bda2 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 55 6e 62 69 6e 64  .*/./*.** Unbind
1bda3 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75 6e 64   the value bound
1bda4 20 74 6f 20 76 61 72 69 61 62 6c 65 20 69 20 69   to variable i i
1bda5 6e 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  n virtual machin
1bda6 65 20 70 2e 20 54 68 69 73 20 69 73 20 74 68 65  e p. This is the
1bda7 20 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73   .** the same as
1bda8 20 62 69 6e 64 69 6e 67 20 61 20 4e 55 4c 4c 20   binding a NULL 
1bda9 76 61 6c 75 65 20 74 6f 20 74 68 65 20 63 6f 6c  value to the col
1bdaa 75 6d 6e 2e 20 49 66 20 74 68 65 20 22 69 22 20  umn. If the "i" 
1bdab 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20  parameter is.** 
1bdac 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74 68  out of range, th
1bdad 65 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45 20  en SQLITE_RANGE 
1bdae 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  is returned. Oth
1bdaf 65 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e  ewise SQLITE_OK.
1bdb0 0a 2a 2a 0a 2a 2a 20 41 20 73 75 63 63 65 73 73  .**.** A success
1bdb1 66 75 6c 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f  ful evaluation o
1bdb2 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  f this routine a
1bdb3 63 71 75 69 72 65 73 20 74 68 65 20 6d 75 74 65  cquires the mute
1bdb4 78 20 6f 6e 20 70 2e 0a 2a 2a 20 74 68 65 20 6d  x on p..** the m
1bdb5 75 74 65 78 20 69 73 20 72 65 6c 65 61 73 65 64  utex is released
1bdb6 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20   if any kind of 
1bdb7 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2a  error occurs..**
1bdb8 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f  .** The error co
1bdb9 64 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74  de stored in dat
1bdba 61 62 61 73 65 20 70 2d 3e 64 62 20 69 73 20 6f  abase p->db is o
1bdbb 76 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20  verwritten with 
1bdbc 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61  the return.** va
1bdbd 6c 75 65 20 69 6e 20 61 6e 79 20 63 61 73 65 2e  lue in any case.
1bdbe 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
1bdbf 64 62 65 55 6e 62 69 6e 64 28 56 64 62 65 20 2a  dbeUnbind(Vdbe *
1bdc0 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d  p, int i){.  Mem
1bdc1 20 2a 70 56 61 72 3b 0a 20 20 69 66 28 20 70 3d   *pVar;.  if( p=
1bdc2 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
1bdc3 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 73 71 6c  TE_MISUSE;.  sql
1bdc4 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
1bdc5 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1bdc6 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d    if( p->magic!=
1bdc7 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 7c  VDBE_MAGIC_RUN |
1bdc8 7c 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  | p->pc>=0 ){.  
1bdc9 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70    sqlite3Error(p
1bdca 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 4d 49 53  ->db, SQLITE_MIS
1bdcb 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  USE, 0);.    sql
1bdcc 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1bdcd 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  (p->db->mutex);.
1bdce 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1bdcf 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
1bdd0 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 70 2d 3e  if( i<1 || i>p->
1bdd1 6e 56 61 72 20 29 7b 0a 20 20 20 20 73 71 6c 69  nVar ){.    sqli
1bdd2 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20  te3Error(p->db, 
1bdd3 53 51 4c 49 54 45 5f 52 41 4e 47 45 2c 20 30 29  SQLITE_RANGE, 0)
1bdd4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1bdd5 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
1bdd6 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74  >mutex);.    ret
1bdd7 75 72 6e 20 53 51 4c 49 54 45 5f 52 41 4e 47 45  urn SQLITE_RANGE
1bdd8 3b 0a 20 20 7d 0a 20 20 69 2d 2d 3b 0a 20 20 70  ;.  }.  i--;.  p
1bdd9 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69  Var = &p->aVar[i
1bdda 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ];.  sqlite3Vdbe
1bddb 4d 65 6d 52 65 6c 65 61 73 65 28 70 56 61 72 29  MemRelease(pVar)
1bddc 3b 0a 20 20 70 56 61 72 2d 3e 66 6c 61 67 73 20  ;.  pVar->flags 
1bddd 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 73 71  = MEM_Null;.  sq
1bdde 6c 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62  lite3Error(p->db
1bddf 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b  , SQLITE_OK, 0);
1bde0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 62 69  ..  /* If the bi
1bde1 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  t corresponding 
1bde2 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c 65  to this variable
1bde3 20 69 6e 20 56 64 62 65 2e 65 78 70 6d 61 73 6b   in Vdbe.expmask
1bde4 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 0a 20   is set, then . 
1bde5 20 2a 2a 20 62 69 6e 64 69 6e 67 20 61 20 6e 65   ** binding a ne
1bde6 77 20 76 61 6c 75 65 20 74 6f 20 74 68 69 73 20  w value to this 
1bde7 76 61 72 69 61 62 6c 65 20 69 6e 76 61 6c 69 64  variable invalid
1bde8 61 74 65 73 20 74 68 65 20 63 75 72 72 65 6e 74  ates the current
1bde9 20 71 75 65 72 79 20 70 6c 61 6e 2e 0a 20 20 2a   query plan..  *
1bdea 2f 0a 20 20 69 66 28 20 70 2d 3e 69 73 50 72 65  /.  if( p->isPre
1bdeb 70 61 72 65 56 32 20 26 26 0a 20 20 20 20 20 28  pareV2 &&.     (
1bdec 28 69 3c 33 32 20 26 26 20 70 2d 3e 65 78 70 6d  (i<32 && p->expm
1bded 61 73 6b 20 26 20 28 28 75 33 32 29 31 20 3c 3c  ask & ((u32)1 <<
1bdee 20 69 29 29 20 7c 7c 20 70 2d 3e 65 78 70 6d 61   i)) || p->expma
1bdef 73 6b 3d 3d 30 78 66 66 66 66 66 66 66 66 29 0a  sk==0xffffffff).
1bdf0 20 20 29 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69    ){.    p->expi
1bdf1 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  red = 1;.  }.  r
1bdf2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1bdf3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61  .}../*.** Bind a
1bdf4 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61   text or BLOB va
1bdf5 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lue..*/.static i
1bdf6 6e 74 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73  nt bindText(.  s
1bdf7 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bdf8 6d 74 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61  mt,   /* The sta
1bdf9 74 65 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61  tement to bind a
1bdfa 67 61 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20  gainst */.  int 
1bdfb 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i,              
1bdfc 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74     /* Index of t
1bdfd 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  he parameter to 
1bdfe 62 69 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  bind */.  const 
1bdff 76 6f 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20  void *zData,    
1be00 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74   /* Pointer to t
1be01 68 65 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f  he data to be bo
1be02 75 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61  und */.  int nDa
1be03 74 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ta,             
1be04 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1be05 65 73 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65  es of data to be
1be06 20 62 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64   bound */.  void
1be07 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c   (*xDel)(void*),
1be08 20 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72     /* Destructor
1be09 20 66 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f   for the data */
1be0a 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67 20 20  .  u8 encoding  
1be0b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63            /* Enc
1be0c 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61  oding for the da
1be0d 74 61 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20  ta */.){.  Vdbe 
1be0e 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  *p = (Vdbe *)pSt
1be0f 6d 74 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b  mt;.  Mem *pVar;
1be10 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63  .  int rc;..  rc
1be11 20 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c   = vdbeUnbind(p,
1be12 20 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53   i);.  if( rc==S
1be13 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1be14 69 66 28 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a  if( zData!=0 ){.
1be15 20 20 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d        pVar = &p-
1be16 3e 61 56 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20  >aVar[i-1];.    
1be17 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1be18 62 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61 72  beMemSetStr(pVar
1be19 2c 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , zData, nData, 
1be1a 65 6e 63 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b  encoding, xDel);
1be1b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
1be1c 51 4c 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f  QLITE_OK && enco
1be1d 64 69 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 20  ding!=0 ){.     
1be1e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1be1f 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
1be20 67 28 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64  g(pVar, ENC(p->d
1be21 62 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  b));.      }.   
1be22 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
1be23 70 2d 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20  p->db, rc, 0);. 
1be24 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1be25 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20  3ApiExit(p->db, 
1be26 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  rc);.    }.    s
1be27 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1be28 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
1be29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1be2a 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e  c;.}.../*.** Bin
1be2b 64 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74  d a blob value t
1be2c 6f 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  o an SQL stateme
1be2d 6e 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a  nt variable..*/.
1be2e 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1be2f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
1be30 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  (.  sqlite3_stmt
1be31 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20   *pStmt, .  int 
1be32 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  i, .  const void
1be33 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20   *zData, .  int 
1be34 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28  nData, .  void (
1be35 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b  *xDel)(void*).){
1be36 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65  .  return bindTe
1be37 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61  xt(pStmt, i, zDa
1be38 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c  ta, nData, xDel,
1be39 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   0);.}.SQLITE_AP
1be3a 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
1be3b 6e 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65  nd_double(sqlite
1be3c 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1be3d 6e 74 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61  nt i, double rVa
1be3e 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  lue){.  int rc;.
1be3f 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62    Vdbe *p = (Vdb
1be40 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20  e *)pStmt;.  rc 
1be41 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20  = vdbeUnbind(p, 
1be42 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  i);.  if( rc==SQ
1be43 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
1be44 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1be45 44 6f 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b  Double(&p->aVar[
1be46 69 2d 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20  i-1], rValue);. 
1be47 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1be48 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75  _leave(p->db->mu
1be49 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  tex);.  }.  retu
1be4a 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f  rn rc;.}.SQLITE_
1be4b 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1be4c 62 69 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33  bind_int(sqlite3
1be4d 5f 73 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c  _stmt *p, int i,
1be4e 20 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20   int iValue){.  
1be4f 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62  return sqlite3_b
1be50 69 6e 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20  ind_int64(p, i, 
1be51 28 69 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a  (i64)iValue);.}.
1be52 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1be53 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
1be54 34 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  4(sqlite3_stmt *
1be55 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71  pStmt, int i, sq
1be56 6c 69 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75  lite_int64 iValu
1be57 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  e){.  int rc;.  
1be58 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20  Vdbe *p = (Vdbe 
1be59 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20  *)pStmt;.  rc = 
1be5a 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29  vdbeUnbind(p, i)
1be5b 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
1be5c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
1be5d 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e  ite3VdbeMemSetIn
1be5e 74 36 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31  t64(&p->aVar[i-1
1be5f 5d 2c 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20  ], iValue);.    
1be60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1be61 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  ave(p->db->mutex
1be62 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1be63 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  rc;.}.SQLITE_API
1be64 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
1be65 64 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73  d_null(sqlite3_s
1be66 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1be67 69 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  i){.  int rc;.  
1be68 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a  Vdbe *p = (Vdbe*
1be69 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76  )pStmt;.  rc = v
1be6a 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b  dbeUnbind(p, i);
1be6b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1be6c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
1be6d 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1be6e 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
1be6f 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1be70 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
1be71 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65   sqlite3_bind_te
1be72 78 74 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73  xt( .  sqlite3_s
1be73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69  tmt *pStmt, .  i
1be74 6e 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63  nt i, .  const c
1be75 68 61 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69  har *zData, .  i
1be76 6e 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69  nt nData, .  voi
1be77 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  d (*xDel)(void*)
1be78 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e  .){.  return bin
1be79 64 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20  dText(pStmt, i, 
1be7a 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44  zData, nData, xD
1be7b 65 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29  el, SQLITE_UTF8)
1be7c 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
1be7d 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
1be7e 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1be7f 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36  ite3_bind_text16
1be80 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  (.  sqlite3_stmt
1be81 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20   *pStmt, .  int 
1be82 69 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  i, .  const void
1be83 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20   *zData, .  int 
1be84 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28  nData, .  void (
1be85 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b  *xDel)(void*).){
1be86 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65  .  return bindTe
1be87 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61  xt(pStmt, i, zDa
1be88 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c  ta, nData, xDel,
1be89 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
1be8a 49 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  IVE);.}.#endif /
1be8b 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
1be8c 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  F16 */.SQLITE_AP
1be8d 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
1be8e 6e 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33  nd_value(sqlite3
1be8f 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1be90 74 20 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74  t i, const sqlit
1be91 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65  e3_value *pValue
1be92 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
1be93 77 69 74 63 68 28 20 70 56 61 6c 75 65 2d 3e 74  witch( pValue->t
1be94 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ype ){.    case 
1be95 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20  SQLITE_INTEGER: 
1be96 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1be97 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
1be98 70 53 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65  pStmt, i, pValue
1be99 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 62 72  ->u.i);.      br
1be9a 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1be9b 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ase SQLITE_FLOAT
1be9c 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  : {.      rc = s
1be9d 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62  qlite3_bind_doub
1be9e 6c 65 28 70 53 74 6d 74 2c 20 69 2c 20 70 56 61  le(pStmt, i, pVa
1be9f 6c 75 65 2d 3e 72 29 3b 0a 20 20 20 20 20 20 62  lue->r);.      b
1bea0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1bea1 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42  case SQLITE_BLOB
1bea2 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 56  : {.      if( pV
1bea3 61 6c 75 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45  alue->flags & ME
1bea4 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
1bea5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
1bea6 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 70 53 74  ind_zeroblob(pSt
1bea7 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 75  mt, i, pValue->u
1bea8 2e 6e 5a 65 72 6f 29 3b 0a 20 20 20 20 20 20 7d  .nZero);.      }
1bea9 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
1beaa 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
1beab 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69 2c 20 70  blob(pStmt, i, p
1beac 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c 75 65  Value->z, pValue
1bead 2d 3e 6e 2c 53 51 4c 49 54 45 5f 54 52 41 4e 53  ->n,SQLITE_TRANS
1beae 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a 20  IENT);.      }. 
1beaf 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1beb0 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
1beb1 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20 20 20  E_TEXT: {.      
1beb2 72 63 20 3d 20 62 69 6e 64 54 65 78 74 28 70 53  rc = bindText(pS
1beb3 74 6d 74 2c 69 2c 20 20 70 56 61 6c 75 65 2d 3e  tmt,i,  pValue->
1beb4 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c 20 53 51  z, pValue->n, SQ
1beb5 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c 0a  LITE_TRANSIENT,.
1beb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1beb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56                pV
1beb8 61 6c 75 65 2d 3e 65 6e 63 29 3b 0a 20 20 20 20  alue->enc);.    
1beb9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1beba 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
1bebb 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1bebc 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 53 74 6d 74  _bind_null(pStmt
1bebd 2c 20 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61  , i);.      brea
1bebe 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  k;.    }.  }.  r
1bebf 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
1bec0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1bec1 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  e3_bind_zeroblob
1bec2 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bec3 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 69 6e 74  Stmt, int i, int
1bec4 20 6e 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20   n){.  int rc;. 
1bec5 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
1bec6 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d   *)pStmt;.  rc =
1bec7 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69   vdbeUnbind(p, i
1bec8 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1bec9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
1beca 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a  lite3VdbeMemSetZ
1becb 65 72 6f 42 6c 6f 62 28 26 70 2d 3e 61 56 61 72  eroBlob(&p->aVar
1becc 5b 69 2d 31 5d 2c 20 6e 29 3b 0a 20 20 20 20 73  [i-1], n);.    s
1becd 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1bece 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
1becf 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1bed0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
1bed1 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
1bed2 20 77 69 6c 64 63 61 72 64 73 20 74 68 61 74 20   wildcards that 
1bed3 63 61 6e 20 62 65 20 70 6f 74 65 6e 74 69 61 6c  can be potential
1bed4 6c 79 20 62 6f 75 6e 64 20 74 6f 2e 0a 2a 2a 20  ly bound to..** 
1bed5 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
1bed6 61 64 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74  added to support
1bed7 20 44 42 44 3a 3a 53 51 4c 69 74 65 2e 20 20 0a   DBD::SQLite.  .
1bed8 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1bed9 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70  t sqlite3_bind_p
1beda 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 73  arameter_count(s
1bedb 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bedc 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d  mt){.  Vdbe *p =
1bedd 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
1bede 20 72 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e   return p ? p->n
1bedf 56 61 72 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  Var : 0;.}../*.*
1bee0 2a 20 43 72 65 61 74 65 20 61 20 6d 61 70 70 69  * Create a mappi
1bee1 6e 67 20 66 72 6f 6d 20 76 61 72 69 61 62 6c 65  ng from variable
1bee2 20 6e 75 6d 62 65 72 73 20 74 6f 20 76 61 72 69   numbers to vari
1bee3 61 62 6c 65 20 6e 61 6d 65 73 0a 2a 2a 20 69 6e  able names.** in
1bee4 20 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b   the Vdbe.azVar[
1bee5 5d 20 61 72 72 61 79 2c 20 69 66 20 73 75 63 68  ] array, if such
1bee6 20 61 20 6d 61 70 70 69 6e 67 20 64 6f 65 73 20   a mapping does 
1bee7 6e 6f 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 65  not already.** e
1bee8 78 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  xist..*/.static 
1bee9 76 6f 69 64 20 63 72 65 61 74 65 56 61 72 4d 61  void createVarMa
1beea 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 66  p(Vdbe *p){.  if
1beeb 28 20 21 70 2d 3e 6f 6b 56 61 72 20 29 7b 0a 20  ( !p->okVar ){. 
1beec 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 4f 70     int j;.    Op
1beed 20 2a 70 4f 70 3b 0a 20 20 20 20 73 71 6c 69 74   *pOp;.    sqlit
1beee 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70  e3_mutex_enter(p
1beef 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
1bef0 20 20 2f 2a 20 54 68 65 20 72 61 63 65 20 63 6f    /* The race co
1bef1 6e 64 69 74 69 6f 6e 20 68 65 72 65 20 69 73 20  ndition here is 
1bef2 68 61 72 6d 6c 65 73 73 2e 20 20 49 66 20 74 77  harmless.  If tw
1bef3 6f 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 74  o threads call t
1bef4 68 69 73 0a 20 20 20 20 2a 2a 20 72 6f 75 74 69  his.    ** routi
1bef5 6e 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 56  ne on the same V
1bef6 64 62 65 20 61 74 20 74 68 65 20 73 61 6d 65 20  dbe at the same 
1bef7 74 69 6d 65 2c 20 74 68 65 79 20 62 6f 74 68 20  time, they both 
1bef8 6d 69 67 68 74 20 65 6e 64 0a 20 20 20 20 2a 2a  might end.    **
1bef9 20 75 70 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67   up initializing
1befa 20 74 68 65 20 56 64 62 65 2e 61 7a 56 61 72 5b   the Vdbe.azVar[
1befb 5d 20 61 72 72 61 79 2e 20 20 54 68 61 74 20 69  ] array.  That i
1befc 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74 72 61  s a little extra
1befd 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 20 62 75 74  .    ** work but
1befe 20 69 74 20 72 65 73 75 6c 74 73 20 69 6e 20 74   it results in t
1beff 68 65 20 73 61 6d 65 20 61 6e 73 77 65 72 2e 0a  he same answer..
1bf00 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a      */.    for(j
1bf01 3d 30 2c 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20  =0, pOp=p->aOp; 
1bf02 6a 3c 70 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70  j<p->nOp; j++, p
1bf03 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28  Op++){.      if(
1bf04 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1bf05 5f 56 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20  _Variable ){.   
1bf06 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1bf07 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p1>0 && pOp->p
1bf08 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20 20  1<=p->nVar );.  
1bf09 20 20 20 20 20 20 70 2d 3e 61 7a 56 61 72 5b 70        p->azVar[p
1bf0a 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 70 4f 70 2d  Op->p1-1] = pOp-
1bf0b 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 7d 0a 20  >p4.z;.      }. 
1bf0c 20 20 20 7d 0a 20 20 20 20 70 2d 3e 6f 6b 56 61     }.    p->okVa
1bf0d 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74  r = 1;.    sqlit
1bf0e 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
1bf0f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
1bf10 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
1bf11 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  n the name of a 
1bf12 77 69 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74  wildcard paramet
1bf13 65 72 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c  er.  Return NULL
1bf14 20 69 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a   if the index.**
1bf15 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
1bf16 20 6f 72 20 69 66 20 74 68 65 20 77 69 6c 64 63   or if the wildc
1bf17 61 72 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a  ard is unnamed..
1bf18 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  **.** The result
1bf19 20 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38   is always UTF-8
1bf1a 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1bf1b 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
1bf1c 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
1bf1d 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  er_name(sqlite3_
1bf1e 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bf1f 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d   i){.  Vdbe *p =
1bf20 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
1bf21 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31   if( p==0 || i<1
1bf22 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b   || i>p->nVar ){
1bf23 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
1bf24 20 7d 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61   }.  createVarMa
1bf25 70 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  p(p);.  return p
1bf26 2d 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a  ->azVar[i-1];.}.
1bf27 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77  ./*.** Given a w
1bf28 69 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65  ildcard paramete
1bf29 72 20 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20 74  r name, return t
1bf2a 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
1bf2b 76 61 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68  variable.** with
1bf2c 20 74 68 61 74 20 6e 61 6d 65 2e 20 20 49 66 20   that name.  If 
1bf2d 74 68 65 72 65 20 69 73 20 6e 6f 20 76 61 72 69  there is no vari
1bf2e 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 67 69  able with the gi
1bf2f 76 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74  ven name,.** ret
1bf30 75 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urn 0..*/.SQLITE
1bf31 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1bf32 69 74 65 33 56 64 62 65 50 61 72 61 6d 65 74 65  ite3VdbeParamete
1bf33 72 49 6e 64 65 78 28 56 64 62 65 20 2a 70 2c 20  rIndex(Vdbe *p, 
1bf34 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1bf35 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 7b 0a 20  e, int nName){. 
1bf36 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d   int i;.  if( p=
1bf37 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1bf38 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65 61 74 65   0;.  }.  create
1bf39 56 61 72 4d 61 70 28 70 29 3b 20 0a 20 20 69 66  VarMap(p); .  if
1bf3a 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 66  ( zName ){.    f
1bf3b 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61  or(i=0; i<p->nVa
1bf3c 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63  r; i++){.      c
1bf3d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70  onst char *z = p
1bf3e 2d 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20 20 20 20  ->azVar[i];.    
1bf3f 20 20 69 66 28 20 7a 20 26 26 20 6d 65 6d 63 6d    if( z && memcm
1bf40 70 28 7a 2c 7a 4e 61 6d 65 2c 6e 4e 61 6d 65 29  p(z,zName,nName)
1bf41 3d 3d 30 20 26 26 20 7a 5b 6e 4e 61 6d 65 5d 3d  ==0 && z[nName]=
1bf42 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
1bf43 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20  turn i+1;.      
1bf44 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
1bf45 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45  turn 0;.}.SQLITE
1bf46 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1bf47 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
1bf48 69 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74  index(sqlite3_st
1bf49 6d 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74  mt *pStmt, const
1bf4a 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20   char *zName){. 
1bf4b 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
1bf4c 64 62 65 50 61 72 61 6d 65 74 65 72 49 6e 64 65  dbeParameterInde
1bf4d 78 28 28 56 64 62 65 2a 29 70 53 74 6d 74 2c 20  x((Vdbe*)pStmt, 
1bf4e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74 65 33 53 74  zName, sqlite3St
1bf4f 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 29 3b 0a  rlen30(zName));.
1bf50 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65  }../*.** Transfe
1bf51 72 20 61 6c 6c 20 62 69 6e 64 69 6e 67 73 20 66  r all bindings f
1bf52 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 73 74  rom the first st
1bf53 61 74 65 6d 65 6e 74 20 6f 76 65 72 20 74 6f 20  atement over to 
1bf54 74 68 65 20 73 65 63 6f 6e 64 2e 0a 2a 2f 0a 53  the second..*/.S
1bf55 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1bf56 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65  t sqlite3Transfe
1bf57 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65  rBindings(sqlite
1bf58 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 6d  3_stmt *pFromStm
1bf59 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  t, sqlite3_stmt 
1bf5a 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64 62  *pToStmt){.  Vdb
1bf5b 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62 65  e *pFrom = (Vdbe
1bf5c 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20 56  *)pFromStmt;.  V
1bf5d 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62 65  dbe *pTo = (Vdbe
1bf5e 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 6e 74  *)pToStmt;.  int
1bf5f 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   i;.  assert( pT
1bf60 6f 2d 3e 64 62 3d 3d 70 46 72 6f 6d 2d 3e 64 62  o->db==pFrom->db
1bf61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
1bf62 6f 2d 3e 6e 56 61 72 3d 3d 70 46 72 6f 6d 2d 3e  o->nVar==pFrom->
1bf63 6e 56 61 72 20 29 3b 0a 20 20 73 71 6c 69 74 65  nVar );.  sqlite
1bf64 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 54  3_mutex_enter(pT
1bf65 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  o->db->mutex);. 
1bf66 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 72 6f   for(i=0; i<pFro
1bf67 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20  m->nVar; i++){. 
1bf68 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1bf69 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 61 72  mMove(&pTo->aVar
1bf6a 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 56 61  [i], &pFrom->aVa
1bf6b 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  r[i]);.  }.  sql
1bf6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
1bf6d 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  (pTo->db->mutex)
1bf6e 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1bf6f 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  E_OK;.}..#ifndef
1bf70 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50   SQLITE_OMIT_DEP
1bf71 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65  RECATED./*.** De
1bf72 70 72 65 63 61 74 65 64 20 65 78 74 65 72 6e 61  precated externa
1bf73 6c 20 69 6e 74 65 72 66 61 63 65 2e 20 20 49 6e  l interface.  In
1bf74 74 65 72 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69  ternal/core SQLi
1bf75 74 65 20 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c  te code.** shoul
1bf76 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 54 72  d call sqlite3Tr
1bf77 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 2e 0a  ansferBindings..
1bf78 2a 2a 0a 2a 2a 20 49 73 20 69 73 20 6d 69 73 75  **.** Is is misu
1bf79 73 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  se to call this 
1bf7a 72 6f 75 74 69 6e 65 20 77 69 74 68 20 73 74 61  routine with sta
1bf7b 74 65 6d 65 6e 74 73 20 66 72 6f 6d 20 64 69 66  tements from dif
1bf7c 66 65 72 65 6e 74 0a 2a 2a 20 64 61 74 61 62 61  ferent.** databa
1bf7d 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20  se connections. 
1bf7e 20 42 75 74 20 61 73 20 74 68 69 73 20 69 73 20   But as this is 
1bf7f 61 20 64 65 70 72 65 63 61 74 65 64 20 69 6e 74  a deprecated int
1bf80 65 72 66 61 63 65 2c 20 77 65 0a 2a 2a 20 77 69  erface, we.** wi
1bf81 6c 6c 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f  ll not bother to
1bf82 20 63 68 65 63 6b 20 66 6f 72 20 74 68 61 74 20   check for that 
1bf83 63 6f 6e 64 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  condition..**.**
1bf84 20 49 66 20 74 68 65 20 74 77 6f 20 73 74 61 74   If the two stat
1bf85 65 6d 65 6e 74 73 20 63 6f 6e 74 61 69 6e 20 61  ements contain a
1bf86 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65   different numbe
1bf87 72 20 6f 66 20 62 69 6e 64 69 6e 67 73 2c 20 74  r of bindings, t
1bf88 68 65 6e 0a 2a 2a 20 61 6e 20 53 51 4c 49 54 45  hen.** an SQLITE
1bf89 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72 6e  _ERROR is return
1bf8a 65 64 2e 20 20 4e 6f 74 68 69 6e 67 20 65 6c 73  ed.  Nothing els
1bf8b 65 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 2c 20  e can go wrong, 
1bf8c 73 6f 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20  so otherwise.** 
1bf8d 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
1bf8e 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
1bf8f 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1bf90 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e  _transfer_bindin
1bf91 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  gs(sqlite3_stmt 
1bf92 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69  *pFromStmt, sqli
1bf93 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d  te3_stmt *pToStm
1bf94 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 46 72 6f  t){.  Vdbe *pFro
1bf95 6d 20 3d 20 28 56 64 62 65 2a 29 70 46 72 6f 6d  m = (Vdbe*)pFrom
1bf96 53 74 6d 74 3b 0a 20 20 56 64 62 65 20 2a 70 54  Stmt;.  Vdbe *pT
1bf97 6f 20 3d 20 28 56 64 62 65 2a 29 70 54 6f 53 74  o = (Vdbe*)pToSt
1bf98 6d 74 3b 0a 20 20 69 66 28 20 70 46 72 6f 6d 2d  mt;.  if( pFrom-
1bf99 3e 6e 56 61 72 21 3d 70 54 6f 2d 3e 6e 56 61 72  >nVar!=pTo->nVar
1bf9a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1bf9b 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
1bf9c 0a 20 20 69 66 28 20 70 54 6f 2d 3e 69 73 50 72  .  if( pTo->isPr
1bf9d 65 70 61 72 65 56 32 20 26 26 20 70 54 6f 2d 3e  epareV2 && pTo->
1bf9e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20 20 20 70  expmask ){.    p
1bf9f 54 6f 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b  To->expired = 1;
1bfa0 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 72 6f 6d  .  }.  if( pFrom
1bfa1 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 26 26  ->isPrepareV2 &&
1bfa2 20 70 46 72 6f 6d 2d 3e 65 78 70 6d 61 73 6b 20   pFrom->expmask 
1bfa3 29 7b 0a 20 20 20 20 70 46 72 6f 6d 2d 3e 65 78  ){.    pFrom->ex
1bfa4 70 69 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  pired = 1;.  }. 
1bfa5 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 54   return sqlite3T
1bfa6 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28  ransferBindings(
1bfa7 70 46 72 6f 6d 53 74 6d 74 2c 20 70 54 6f 53 74  pFromStmt, pToSt
1bfa8 6d 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  mt);.}.#endif../
1bfa9 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1bfaa 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73  sqlite3* databas
1bfab 65 20 68 61 6e 64 6c 65 20 74 6f 20 77 68 69 63  e handle to whic
1bfac 68 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  h the prepared s
1bfad 74 61 74 65 6d 65 6e 74 20 67 69 76 65 6e 0a 2a  tatement given.*
1bfae 2a 20 69 6e 20 74 68 65 20 61 72 67 75 6d 65 6e  * in the argumen
1bfaf 74 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 69 73  t belongs.  This
1bfb0 20 69 73 20 74 68 65 20 73 61 6d 65 20 64 61 74   is the same dat
1bfb1 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61  abase handle tha
1bfb2 74 20 77 61 73 0a 2a 2a 20 74 68 65 20 66 69 72  t was.** the fir
1bfb3 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  st argument to t
1bfb4 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  he sqlite3_prepa
1bfb5 72 65 28 29 20 74 68 61 74 20 77 61 73 20 75 73  re() that was us
1bfb6 65 64 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20  ed to create.** 
1bfb7 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  the statement in
1bfb8 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 65   the first place
1bfb9 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1bfba 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33  sqlite3 *sqlite3
1bfbb 5f 64 62 5f 68 61 6e 64 6c 65 28 73 71 6c 69 74  _db_handle(sqlit
1bfbc 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
1bfbd 0a 20 20 72 65 74 75 72 6e 20 70 53 74 6d 74 20  .  return pStmt 
1bfbe 3f 20 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29  ? ((Vdbe*)pStmt)
1bfbf 2d 3e 64 62 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a  ->db : 0;.}../*.
1bfc0 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
1bfc1 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ter to the next 
1bfc2 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1bfc3 6e 74 20 61 66 74 65 72 20 70 53 74 6d 74 20 61  nt after pStmt a
1bfc4 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74  ssociated.** wit
1bfc5 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  h database conne
1bfc6 63 74 69 6f 6e 20 70 44 62 2e 20 20 49 66 20 70  ction pDb.  If p
1bfc7 53 74 6d 74 20 69 73 20 4e 55 4c 4c 2c 20 72 65  Stmt is NULL, re
1bfc8 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 0a 2a  turn the first.*
1bfc9 2a 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  * prepared state
1bfca 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 64 61 74  ment for the dat
1bfcb 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1bfcc 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69  .  Return NULL i
1bfcd 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e  f there.** are n
1bfce 6f 20 6d 6f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  o more..*/.SQLIT
1bfcf 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74  E_API sqlite3_st
1bfd0 6d 74 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74  mt *sqlite3_next
1bfd1 5f 73 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70  _stmt(sqlite3 *p
1bfd2 44 62 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  Db, sqlite3_stmt
1bfd3 20 2a 70 53 74 6d 74 29 7b 0a 20 20 73 71 6c 69   *pStmt){.  sqli
1bfd4 74 65 33 5f 73 74 6d 74 20 2a 70 4e 65 78 74 3b  te3_stmt *pNext;
1bfd5 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
1bfd6 5f 65 6e 74 65 72 28 70 44 62 2d 3e 6d 75 74 65  _enter(pDb->mute
1bfd7 78 29 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d  x);.  if( pStmt=
1bfd8 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65 78 74 20  =0 ){.    pNext 
1bfd9 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  = (sqlite3_stmt*
1bfda 29 70 44 62 2d 3e 70 56 64 62 65 3b 0a 20 20 7d  )pDb->pVdbe;.  }
1bfdb 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 78 74 20  else{.    pNext 
1bfdc 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  = (sqlite3_stmt*
1bfdd 29 28 28 56 64 62 65 2a 29 70 53 74 6d 74 29 2d  )((Vdbe*)pStmt)-
1bfde 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 73 71  >pNext;.  }.  sq
1bfdf 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1bfe0 65 28 70 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  e(pDb->mutex);. 
1bfe1 20 72 65 74 75 72 6e 20 70 4e 65 78 74 3b 0a 7d   return pNext;.}
1bfe2 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1bfe3 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74  he value of a st
1bfe4 61 74 75 73 20 63 6f 75 6e 74 65 72 20 66 6f 72  atus counter for
1bfe5 20 61 20 70 72 65 70 61 72 65 64 20 73 74 61 74   a prepared stat
1bfe6 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ement.*/.SQLITE_
1bfe7 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1bfe8 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69  stmt_status(sqli
1bfe9 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bfea 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 72 65 73   int op, int res
1bfeb 65 74 46 6c 61 67 29 7b 0a 20 20 56 64 62 65 20  etFlag){.  Vdbe 
1bfec 2a 70 56 64 62 65 20 3d 20 28 56 64 62 65 2a 29  *pVdbe = (Vdbe*)
1bfed 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 76 20 3d  pStmt;.  int v =
1bfee 20 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72   pVdbe->aCounter
1bfef 5b 6f 70 2d 31 5d 3b 0a 20 20 69 66 28 20 72 65  [op-1];.  if( re
1bff0 73 65 74 46 6c 61 67 20 29 20 70 56 64 62 65 2d  setFlag ) pVdbe-
1bff1 3e 61 43 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 20  >aCounter[op-1] 
1bff2 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 76 3b  = 0;.  return v;
1bff3 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
1bff4 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 61  *** End of vdbea
1bff5 70 69 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  pi.c ***********
1bff6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bff7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bff8 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
1bff9 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76  *** Begin file v
1bffa 64 62 65 74 72 61 63 65 2e 63 20 2a 2a 2a 2a 2a  dbetrace.c *****
1bffb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bffc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bffd 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4e  **/./*.** 2009 N
1bffe 6f 76 65 6d 62 65 72 20 32 35 0a 2a 2a 0a 2a 2a  ovember 25.**.**
1bfff 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1c000 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1c001 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1c002 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1c003 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1c004 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1c005 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1c006 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1c007 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1c008 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1c009 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1c00a 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1c00b 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1c00c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1c00d 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1c00e 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1c00f 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1c010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c011 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c012 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c013 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c014 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1c015 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1c016 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  ns code used to 
1c017 69 6e 73 65 72 74 20 74 68 65 20 76 61 6c 75 65  insert the value
1c018 73 20 6f 66 20 68 6f 73 74 20 70 61 72 61 6d 65  s of host parame
1c019 74 65 72 73 0a 2a 2a 20 28 61 6b 61 20 22 77 69  ters.** (aka "wi
1c01a 6c 64 63 61 72 64 73 22 29 20 69 6e 74 6f 20 74  ldcards") into t
1c01b 68 65 20 53 51 4c 20 74 65 78 74 20 6f 75 74 70  he SQL text outp
1c01c 75 74 20 62 79 20 73 71 6c 69 74 65 33 5f 74 72  ut by sqlite3_tr
1c01d 61 63 65 28 29 2e 0a 2a 2f 0a 0a 23 69 66 6e 64  ace()..*/..#ifnd
1c01e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
1c01f 52 41 43 45 0a 0a 2f 2a 0a 2a 2a 20 7a 53 71 6c  RACE../*.** zSql
1c020 20 69 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69   is a zero-termi
1c021 6e 61 74 65 64 20 73 74 72 69 6e 67 20 6f 66 20  nated string of 
1c022 55 54 46 2d 38 20 53 51 4c 20 74 65 78 74 2e 20  UTF-8 SQL text. 
1c023 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1c024 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69  er of.** bytes i
1c025 6e 20 74 68 69 73 20 74 65 78 74 20 75 70 20 74  n this text up t
1c026 6f 20 62 75 74 20 65 78 63 6c 75 64 69 6e 67 20  o but excluding 
1c027 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63  the first charac
1c028 74 65 72 20 69 6e 0a 2a 2a 20 61 20 68 6f 73 74  ter in.** a host
1c029 20 70 61 72 61 6d 65 74 65 72 2e 20 20 49 66 20   parameter.  If 
1c02a 74 68 65 20 74 65 78 74 20 63 6f 6e 74 61 69 6e  the text contain
1c02b 73 20 6e 6f 20 68 6f 73 74 20 70 61 72 61 6d 65  s no host parame
1c02c 74 65 72 73 2c 20 72 65 74 75 72 6e 0a 2a 2a 20  ters, return.** 
1c02d 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
1c02e 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
1c02f 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74 69 63   text..*/.static
1c030 20 69 6e 74 20 66 69 6e 64 4e 65 78 74 48 6f 73   int findNextHos
1c031 74 50 61 72 61 6d 65 74 65 72 28 63 6f 6e 73 74  tParameter(const
1c032 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 69 6e 74   char *zSql, int
1c033 20 2a 70 6e 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e   *pnToken){.  in
1c034 74 20 74 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 69  t tokenType;.  i
1c035 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20  nt nTotal = 0;. 
1c036 20 69 6e 74 20 6e 3b 0a 0a 20 20 2a 70 6e 54 6f   int n;..  *pnTo
1c037 6b 65 6e 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65  ken = 0;.  while
1c038 28 20 7a 53 71 6c 5b 30 5d 20 29 7b 0a 20 20 20  ( zSql[0] ){.   
1c039 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54   n = sqlite3GetT
1c03a 6f 6b 65 6e 28 28 75 38 2a 29 7a 53 71 6c 2c 20  oken((u8*)zSql, 
1c03b 26 74 6f 6b 65 6e 54 79 70 65 29 3b 0a 20 20 20  &tokenType);.   
1c03c 20 61 73 73 65 72 74 28 20 6e 3e 30 20 26 26 20   assert( n>0 && 
1c03d 74 6f 6b 65 6e 54 79 70 65 21 3d 54 4b 5f 49 4c  tokenType!=TK_IL
1c03e 4c 45 47 41 4c 20 29 3b 0a 20 20 20 20 69 66 28  LEGAL );.    if(
1c03f 20 74 6f 6b 65 6e 54 79 70 65 3d 3d 54 4b 5f 56   tokenType==TK_V
1c040 41 52 49 41 42 4c 45 20 29 7b 0a 20 20 20 20 20  ARIABLE ){.     
1c041 20 2a 70 6e 54 6f 6b 65 6e 20 3d 20 6e 3b 0a 20   *pnToken = n;. 
1c042 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1c043 7d 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b 3d 20  }.    nTotal += 
1c044 6e 3b 0a 20 20 20 20 7a 53 71 6c 20 2b 3d 20 6e  n;.    zSql += n
1c045 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  ;.  }.  return n
1c046 54 6f 74 61 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Total;.}../*.** 
1c047 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
1c048 20 74 6f 20 61 20 73 74 72 69 6e 67 20 69 6e 20   to a string in 
1c049 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
1c04a 66 6f 72 6d 20 73 71 6c 69 74 65 33 44 62 4d 61  form sqlite3DbMa
1c04b 6c 6c 6f 63 28 29 20 77 68 69 63 68 0a 2a 2a 20  lloc() which.** 
1c04c 68 6f 6c 64 73 20 61 20 63 6f 70 79 20 6f 66 20  holds a copy of 
1c04d 7a 52 61 77 53 71 6c 20 62 75 74 20 77 69 74 68  zRawSql but with
1c04e 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73   host parameters
1c04f 20 65 78 70 61 6e 64 65 64 20 74 6f 20 74 68 65   expanded to the
1c050 69 72 0a 2a 2a 20 63 75 72 72 65 6e 74 20 62 69  ir.** current bi
1c051 6e 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ndings..**.** Th
1c052 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
1c053 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  on is responsibl
1c054 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72  e for making sur
1c055 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 74  e the memory ret
1c056 75 72 6e 65 64 0a 2a 2a 20 69 73 20 65 76 65 6e  urned.** is even
1c057 74 75 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a  tually freed..**
1c058 0a 2a 2a 20 41 4c 47 4f 52 49 54 48 4d 3a 20 20  .** ALGORITHM:  
1c059 53 63 61 6e 20 74 68 65 20 69 6e 70 75 74 20 73  Scan the input s
1c05a 74 72 69 6e 67 20 6c 6f 6f 6b 69 6e 67 20 66 6f  tring looking fo
1c05b 72 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72  r host parameter
1c05c 73 20 69 6e 20 61 6e 79 20 6f 66 0a 2a 2a 20 74  s in any of.** t
1c05d 68 65 73 65 20 66 6f 72 6d 73 3a 20 20 3f 2c 20  hese forms:  ?, 
1c05e 3f 4e 2c 20 24 41 2c 20 40 41 2c 20 3a 41 2e 20  ?N, $A, @A, :A. 
1c05f 20 54 61 6b 65 20 63 61 72 65 20 74 6f 20 61 76   Take care to av
1c060 6f 69 64 20 74 65 78 74 20 77 69 74 68 69 6e 0a  oid text within.
1c061 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61  ** string litera
1c062 6c 73 2c 20 71 75 6f 74 65 64 20 69 64 65 6e 74  ls, quoted ident
1c063 69 66 69 65 72 20 6e 61 6d 65 73 2c 20 61 6e 64  ifier names, and
1c064 20 63 6f 6d 6d 65 6e 74 73 2e 20 20 46 6f 72 20   comments.  For 
1c065 74 65 78 74 20 66 6f 72 6d 73 2c 0a 2a 2a 20 74  text forms,.** t
1c066 68 65 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  he host paramete
1c067 72 20 69 6e 64 65 78 20 69 73 20 66 6f 75 6e 64  r index is found
1c068 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 74 68 65   by scanning the
1c069 20 70 65 72 70 61 72 65 64 0a 2a 2a 20 73 74 61   perpared.** sta
1c06a 74 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 63  tement for the c
1c06b 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 4f 50 5f  orresponding OP_
1c06c 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e  Variable opcode.
1c06d 20 20 4f 6e 63 65 20 74 68 65 20 68 6f 73 74 0a    Once the host.
1c06e 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69 6e 64  ** parameter ind
1c06f 65 78 20 69 73 20 6b 6e 6f 77 6e 2c 20 6c 6f 63  ex is known, loc
1c070 61 74 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  ate the value in
1c071 20 70 2d 3e 61 56 61 72 5b 5d 2e 20 20 54 68 65   p->aVar[].  The
1c072 6e 20 72 65 6e 64 65 72 0a 2a 2a 20 74 68 65 20  n render.** the 
1c073 76 61 6c 75 65 20 61 73 20 61 20 6c 69 74 65 72  value as a liter
1c074 61 6c 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74  al in place of t
1c075 68 65 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  he host paramete
1c076 72 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54  r name..*/.SQLIT
1c077 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
1c078 73 71 6c 69 74 65 33 56 64 62 65 45 78 70 61 6e  sqlite3VdbeExpan
1c079 64 53 71 6c 28 0a 20 20 56 64 62 65 20 2a 70 2c  dSql(.  Vdbe *p,
1c07a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c07b 20 2f 2a 20 54 68 65 20 70 72 65 70 61 72 65 64   /* The prepared
1c07c 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67   statement being
1c07d 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20   evaluated */.  
1c07e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 61 77  const char *zRaw
1c07f 53 71 6c 20 20 20 20 20 20 2f 2a 20 52 61 77 20  Sql      /* Raw 
1c080 74 65 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20  text of the SQL 
1c081 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a  statement */.){.
1c082 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
1c083 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1c084 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1c085 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ction */.  int i
1c086 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  dx = 0;         
1c087 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
1c088 61 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72  a host parameter
1c089 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74 49 6e   */.  int nextIn
1c08a 64 65 78 20 3d 20 31 3b 20 20 20 20 20 20 20 2f  dex = 1;       /
1c08b 2a 20 49 6e 64 65 78 20 6f 66 20 6e 65 78 74 20  * Index of next 
1c08c 3f 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72  ? host parameter
1c08d 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20   */.  int n;    
1c08e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c08f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 74 6f  * Length of a to
1c090 6b 65 6e 20 70 72 65 66 69 78 20 2a 2f 0a 20 20  ken prefix */.  
1c091 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20  int nToken;     
1c092 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
1c093 74 68 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65  th of the parame
1c094 74 65 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69  ter token */.  i
1c095 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
1c096 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1c097 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4d 65 6d  counter */.  Mem
1c098 20 2a 70 56 61 72 3b 20 20 20 20 20 20 20 20 20   *pVar;         
1c099 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f        /* Value o
1c09a 66 20 61 20 68 6f 73 74 20 70 61 72 61 6d 65 74  f a host paramet
1c09b 65 72 20 2a 2f 0a 20 20 53 74 72 41 63 63 75 6d  er */.  StrAccum
1c09c 20 6f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20   out;           
1c09d 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 74   /* Accumulate t
1c09e 68 65 20 6f 75 74 70 75 74 20 68 65 72 65 20 2a  he output here *
1c09f 2f 0a 20 20 63 68 61 72 20 7a 42 61 73 65 5b 31  /.  char zBase[1
1c0a0 30 30 5d 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  00];         /* 
1c0a1 49 6e 69 74 69 61 6c 20 77 6f 72 6b 69 6e 67 20  Initial working 
1c0a2 73 70 61 63 65 20 2a 2f 0a 0a 20 20 64 62 20 3d  space */..  db =
1c0a3 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65   p->db;.  sqlite
1c0a4 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 6f  3StrAccumInit(&o
1c0a5 75 74 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f  ut, zBase, sizeo
1c0a6 66 28 7a 42 61 73 65 29 2c 20 0a 20 20 20 20 20  f(zBase), .     
1c0a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c0a8 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
1c0a9 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
1c0aa 29 3b 0a 20 20 6f 75 74 2e 64 62 20 3d 20 64 62  );.  out.db = db
1c0ab 3b 0a 20 20 77 68 69 6c 65 28 20 7a 52 61 77 53  ;.  while( zRawS
1c0ac 71 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 20 3d  ql[0] ){.    n =
1c0ad 20 66 69 6e 64 4e 65 78 74 48 6f 73 74 50 61 72   findNextHostPar
1c0ae 61 6d 65 74 65 72 28 7a 52 61 77 53 71 6c 2c 20  ameter(zRawSql, 
1c0af 26 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 61 73  &nToken);.    as
1c0b0 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20  sert( n>0 );.   
1c0b1 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
1c0b2 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 7a 52 61  Append(&out, zRa
1c0b3 77 53 71 6c 2c 20 6e 29 3b 0a 20 20 20 20 7a 52  wSql, n);.    zR
1c0b4 61 77 53 71 6c 20 2b 3d 20 6e 3b 0a 20 20 20 20  awSql += n;.    
1c0b5 61 73 73 65 72 74 28 20 7a 52 61 77 53 71 6c 5b  assert( zRawSql[
1c0b6 30 5d 20 7c 7c 20 6e 54 6f 6b 65 6e 3d 3d 30 20  0] || nToken==0 
1c0b7 29 3b 0a 20 20 20 20 69 66 28 20 6e 54 6f 6b 65  );.    if( nToke
1c0b8 6e 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  n==0 ) break;.  
1c0b9 20 20 69 66 28 20 7a 52 61 77 53 71 6c 5b 30 5d    if( zRawSql[0]
1c0ba 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 20 20 69  =='?' ){.      i
1c0bb 66 28 20 6e 54 6f 6b 65 6e 3e 31 20 29 7b 0a 20  f( nToken>1 ){. 
1c0bc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73         assert( s
1c0bd 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 7a 52  qlite3Isdigit(zR
1c0be 61 77 53 71 6c 5b 31 5d 29 20 29 3b 0a 20 20 20  awSql[1]) );.   
1c0bf 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 74 49       sqlite3GetI
1c0c0 6e 74 33 32 28 26 7a 52 61 77 53 71 6c 5b 31 5d  nt32(&zRawSql[1]
1c0c1 2c 20 26 69 64 78 29 3b 0a 20 20 20 20 20 20 7d  , &idx);.      }
1c0c2 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 64  else{.        id
1c0c3 78 20 3d 20 6e 65 78 74 49 6e 64 65 78 3b 0a 20  x = nextIndex;. 
1c0c4 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
1c0c5 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1c0c6 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 3a 27 20  zRawSql[0]==':' 
1c0c7 7c 7c 20 7a 52 61 77 53 71 6c 5b 30 5d 3d 3d 27  || zRawSql[0]=='
1c0c8 24 27 20 7c 7c 20 7a 52 61 77 53 71 6c 5b 30 5d  $' || zRawSql[0]
1c0c9 3d 3d 27 40 27 20 29 3b 0a 20 20 20 20 20 20 74  =='@' );.      t
1c0ca 65 73 74 63 61 73 65 28 20 7a 52 61 77 53 71 6c  estcase( zRawSql
1c0cb 5b 30 5d 3d 3d 27 3a 27 20 29 3b 0a 20 20 20 20  [0]==':' );.    
1c0cc 20 20 74 65 73 74 63 61 73 65 28 20 7a 52 61 77    testcase( zRaw
1c0cd 53 71 6c 5b 30 5d 3d 3d 27 24 27 20 29 3b 0a 20  Sql[0]=='$' );. 
1c0ce 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
1c0cf 52 61 77 53 71 6c 5b 30 5d 3d 3d 27 40 27 20 29  RawSql[0]=='@' )
1c0d0 3b 0a 20 20 20 20 20 20 69 64 78 20 3d 20 73 71  ;.      idx = sq
1c0d1 6c 69 74 65 33 56 64 62 65 50 61 72 61 6d 65 74  lite3VdbeParamet
1c0d2 65 72 49 6e 64 65 78 28 70 2c 20 7a 52 61 77 53  erIndex(p, zRawS
1c0d3 71 6c 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20  ql, nToken);.   
1c0d4 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 30     assert( idx>0
1c0d5 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 52   );.    }.    zR
1c0d6 61 77 53 71 6c 20 2b 3d 20 6e 54 6f 6b 65 6e 3b  awSql += nToken;
1c0d7 0a 20 20 20 20 6e 65 78 74 49 6e 64 65 78 20 3d  .    nextIndex =
1c0d8 20 69 64 78 20 2b 20 31 3b 0a 20 20 20 20 61 73   idx + 1;.    as
1c0d9 73 65 72 74 28 20 69 64 78 3e 30 20 26 26 20 69  sert( idx>0 && i
1c0da 64 78 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20  dx<=p->nVar );. 
1c0db 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56     pVar = &p->aV
1c0dc 61 72 5b 69 64 78 2d 31 5d 3b 0a 20 20 20 20 69  ar[idx-1];.    i
1c0dd 66 28 20 70 56 61 72 2d 3e 66 6c 61 67 73 20 26  f( pVar->flags &
1c0de 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1c0df 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
1c0e0 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 22  umAppend(&out, "
1c0e1 4e 55 4c 4c 22 2c 20 34 29 3b 0a 20 20 20 20 7d  NULL", 4);.    }
1c0e2 65 6c 73 65 20 69 66 28 20 70 56 61 72 2d 3e 66  else if( pVar->f
1c0e3 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29  lags & MEM_Int )
1c0e4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 58  {.      sqlite3X
1c0e5 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22 25 6c  Printf(&out, "%l
1c0e6 6c 64 22 2c 20 70 56 61 72 2d 3e 75 2e 69 29 3b  ld", pVar->u.i);
1c0e7 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
1c0e8 56 61 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Var->flags & MEM
1c0e9 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 73  _Real ){.      s
1c0ea 71 6c 69 74 65 33 58 50 72 69 6e 74 66 28 26 6f  qlite3XPrintf(&o
1c0eb 75 74 2c 20 22 25 21 2e 31 35 67 22 2c 20 70 56  ut, "%!.15g", pV
1c0ec 61 72 2d 3e 72 29 3b 0a 20 20 20 20 7d 65 6c 73  ar->r);.    }els
1c0ed 65 20 69 66 28 20 70 56 61 72 2d 3e 66 6c 61 67  e if( pVar->flag
1c0ee 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 23  s & MEM_Str ){.#
1c0ef 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1c0f0 49 54 5f 55 54 46 31 36 0a 20 20 20 20 20 20 75  IT_UTF16.      u
1c0f1 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b  8 enc = ENC(db);
1c0f2 0a 20 20 20 20 20 20 69 66 28 20 65 6e 63 21 3d  .      if( enc!=
1c0f3 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20  SQLITE_UTF8 ){. 
1c0f4 20 20 20 20 20 20 20 4d 65 6d 20 75 74 66 38 3b         Mem utf8;
1c0f5 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
1c0f6 26 75 74 66 38 2c 20 30 2c 20 73 69 7a 65 6f 66  &utf8, 0, sizeof
1c0f7 28 75 74 66 38 29 29 3b 0a 20 20 20 20 20 20 20  (utf8));.       
1c0f8 20 75 74 66 38 2e 64 62 20 3d 20 64 62 3b 0a 20   utf8.db = db;. 
1c0f9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1c0fa 62 65 4d 65 6d 53 65 74 53 74 72 28 26 75 74 66  beMemSetStr(&utf
1c0fb 38 2c 20 70 56 61 72 2d 3e 7a 2c 20 70 56 61 72  8, pVar->z, pVar
1c0fc 2d 3e 6e 2c 20 65 6e 63 2c 20 53 51 4c 49 54 45  ->n, enc, SQLITE
1c0fd 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
1c0fe 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1c0ff 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 75 74 66  ngeEncoding(&utf
1c100 38 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  8, SQLITE_UTF8);
1c101 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1c102 58 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22 27  XPrintf(&out, "'
1c103 25 2e 2a 71 27 22 2c 20 75 74 66 38 2e 6e 2c 20  %.*q'", utf8.n, 
1c104 75 74 66 38 2e 7a 29 3b 0a 20 20 20 20 20 20 20  utf8.z);.       
1c105 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1c106 65 6c 65 61 73 65 28 26 75 74 66 38 29 3b 0a 20  elease(&utf8);. 
1c107 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69       }else.#endi
1c108 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  f.      {.      
1c109 20 20 73 71 6c 69 74 65 33 58 50 72 69 6e 74 66    sqlite3XPrintf
1c10a 28 26 6f 75 74 2c 20 22 27 25 2e 2a 71 27 22 2c  (&out, "'%.*q'",
1c10b 20 70 56 61 72 2d 3e 6e 2c 20 70 56 61 72 2d 3e   pVar->n, pVar->
1c10c 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  z);.      }.    
1c10d 7d 65 6c 73 65 20 69 66 28 20 70 56 61 72 2d 3e  }else if( pVar->
1c10e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
1c10f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1c110 33 58 50 72 69 6e 74 66 28 26 6f 75 74 2c 20 22  3XPrintf(&out, "
1c111 7a 65 72 6f 62 6c 6f 62 28 25 64 29 22 2c 20 70  zeroblob(%d)", p
1c112 56 61 72 2d 3e 75 2e 6e 5a 65 72 6f 29 3b 0a 20  Var->u.nZero);. 
1c113 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1c114 61 73 73 65 72 74 28 20 70 56 61 72 2d 3e 66 6c  assert( pVar->fl
1c115 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29  ags & MEM_Blob )
1c116 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ;.      sqlite3S
1c117 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 6f  trAccumAppend(&o
1c118 75 74 2c 20 22 78 27 22 2c 20 32 29 3b 0a 20 20  ut, "x'", 2);.  
1c119 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1c11a 56 61 72 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Var->n; i++){.  
1c11b 20 20 20 20 20 20 73 71 6c 69 74 65 33 58 50 72        sqlite3XPr
1c11c 69 6e 74 66 28 26 6f 75 74 2c 20 22 25 30 32 78  intf(&out, "%02x
1c11d 22 2c 20 70 56 61 72 2d 3e 7a 5b 69 5d 26 30 78  ", pVar->z[i]&0x
1c11e 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ff);.      }.   
1c11f 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
1c120 75 6d 41 70 70 65 6e 64 28 26 6f 75 74 2c 20 22  umAppend(&out, "
1c121 27 22 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20  '", 1);.    }.  
1c122 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
1c123 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68  e3StrAccumFinish
1c124 28 26 6f 75 74 29 3b 0a 7d 0a 0a 23 65 6e 64 69  (&out);.}..#endi
1c125 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c  f /* #ifndef SQL
1c126 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 2a  ITE_OMIT_TRACE *
1c127 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
1c128 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 74 72  ** End of vdbetr
1c129 61 63 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ace.c **********
1c12a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c12b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c12c 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
1c12d 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 64  ** Begin file vd
1c12e 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  be.c ***********
1c12f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c131 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
1c132 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
1c133 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1c134 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1c135 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1c136 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1c137 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1c138 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1c139 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1c13a 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1c13b 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1c13c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1c13d 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1c13e 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1c13f 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1c140 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1c141 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1c142 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1c143 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1c144 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c145 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c146 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c147 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c148 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  *********.** The
1c149 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
1c14a 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 65 78  le implements ex
1c14b 65 63 75 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f  ecution method o
1c14c 66 20 74 68 65 20 0a 2a 2a 20 56 69 72 74 75 61  f the .** Virtua
1c14d 6c 20 44 61 74 61 62 61 73 65 20 45 6e 67 69 6e  l Database Engin
1c14e 65 20 28 56 44 42 45 29 2e 20 20 41 20 73 65 70  e (VDBE).  A sep
1c14f 61 72 61 74 65 20 66 69 6c 65 20 28 22 76 64 62  arate file ("vdb
1c150 65 61 75 78 2e 63 22 29 0a 2a 2a 20 68 61 6e 64  eaux.c").** hand
1c151 6c 65 73 20 68 6f 75 73 65 6b 65 65 70 69 6e 67  les housekeeping
1c152 20 64 65 74 61 69 6c 73 20 73 75 63 68 20 61 73   details such as
1c153 20 63 72 65 61 74 69 6e 67 20 61 6e 64 20 64 65   creating and de
1c154 6c 65 74 69 6e 67 0a 2a 2a 20 56 44 42 45 20 69  leting.** VDBE i
1c155 6e 73 74 61 6e 63 65 73 2e 20 20 54 68 69 73 20  nstances.  This 
1c156 66 69 6c 65 20 69 73 20 73 6f 6c 65 6c 79 20 69  file is solely i
1c157 6e 74 65 72 65 73 74 65 64 20 69 6e 20 65 78 65  nterested in exe
1c158 63 75 74 69 6e 67 0a 2a 2a 20 74 68 65 20 56 44  cuting.** the VD
1c159 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a  BE program..**.*
1c15a 2a 20 49 6e 20 74 68 65 20 65 78 74 65 72 6e 61  * In the externa
1c15b 6c 20 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 20  l interface, an 
1c15c 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 22 20  "sqlite3_stmt*" 
1c15d 69 73 20 61 6e 20 6f 70 61 71 75 65 20 70 6f 69  is an opaque poi
1c15e 6e 74 65 72 0a 2a 2a 20 74 6f 20 61 20 56 44 42  nter.** to a VDB
1c15f 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c  E..**.** The SQL
1c160 20 70 61 72 73 65 72 20 67 65 6e 65 72 61 74 65   parser generate
1c161 73 20 61 20 70 72 6f 67 72 61 6d 20 77 68 69 63  s a program whic
1c162 68 20 69 73 20 74 68 65 6e 20 65 78 65 63 75 74  h is then execut
1c163 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 56 44 42  ed by.** the VDB
1c164 45 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  E to do the work
1c165 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61 74   of the SQL stat
1c166 65 6d 65 6e 74 2e 20 20 56 44 42 45 20 70 72 6f  ement.  VDBE pro
1c167 67 72 61 6d 73 20 61 72 65 20 0a 2a 2a 20 73 69  grams are .** si
1c168 6d 69 6c 61 72 20 69 6e 20 66 6f 72 6d 20 74 6f  milar in form to
1c169 20 61 73 73 65 6d 62 6c 79 20 6c 61 6e 67 75 61   assembly langua
1c16a 67 65 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d  ge.  The program
1c16b 20 63 6f 6e 73 69 73 74 73 20 6f 66 0a 2a 2a 20   consists of.** 
1c16c 61 20 6c 69 6e 65 61 72 20 73 65 71 75 65 6e 63  a linear sequenc
1c16d 65 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73 2e  e of operations.
1c16e 20 20 45 61 63 68 20 6f 70 65 72 61 74 69 6f 6e    Each operation
1c16f 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 20 0a   has an opcode .
1c170 2a 2a 20 61 6e 64 20 35 20 6f 70 65 72 61 6e 64  ** and 5 operand
1c171 73 2e 20 20 4f 70 65 72 61 6e 64 73 20 50 31 2c  s.  Operands P1,
1c172 20 50 32 2c 20 61 6e 64 20 50 33 20 61 72 65 20   P2, and P3 are 
1c173 69 6e 74 65 67 65 72 73 2e 20 20 4f 70 65 72 61  integers.  Opera
1c174 6e 64 20 50 34 20 0a 2a 2a 20 69 73 20 61 20 6e  nd P4 .** is a n
1c175 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  ull-terminated s
1c176 74 72 69 6e 67 2e 20 20 4f 70 65 72 61 6e 64 20  tring.  Operand 
1c177 50 35 20 69 73 20 61 6e 20 75 6e 73 69 67 6e 65  P5 is an unsigne
1c178 64 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20  d character..** 
1c179 46 65 77 20 6f 70 63 6f 64 65 73 20 75 73 65 20  Few opcodes use 
1c17a 61 6c 6c 20 35 20 6f 70 65 72 61 6e 64 73 2e 0a  all 5 operands..
1c17b 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 61 74 69 6f  **.** Computatio
1c17c 6e 20 72 65 73 75 6c 74 73 20 61 72 65 20 73 74  n results are st
1c17d 6f 72 65 64 20 6f 6e 20 61 20 73 65 74 20 6f 66  ored on a set of
1c17e 20 72 65 67 69 73 74 65 72 73 20 6e 75 6d 62 65   registers numbe
1c17f 72 65 64 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a  red beginning.**
1c180 20 77 69 74 68 20 31 20 61 6e 64 20 67 6f 69 6e   with 1 and goin
1c181 67 20 75 70 20 74 6f 20 56 64 62 65 2e 6e 4d 65  g up to Vdbe.nMe
1c182 6d 2e 20 20 45 61 63 68 20 72 65 67 69 73 74 65  m.  Each registe
1c183 72 20 63 61 6e 20 73 74 6f 72 65 0a 2a 2a 20 65  r can store.** e
1c184 69 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72  ither an integer
1c185 2c 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61  , a null-termina
1c186 74 65 64 20 73 74 72 69 6e 67 2c 20 61 20 66 6c  ted string, a fl
1c187 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20  oating point.** 
1c188 6e 75 6d 62 65 72 2c 20 6f 72 20 74 68 65 20 53  number, or the S
1c189 51 4c 20 22 4e 55 4c 4c 22 20 76 61 6c 75 65 2e  QL "NULL" value.
1c18a 20 20 41 6e 20 69 6d 70 6c 69 63 69 74 20 63 6f    An implicit co
1c18b 6e 76 65 72 73 69 6f 6e 20 66 72 6f 6d 20 6f 6e  nversion from on
1c18c 65 0a 2a 2a 20 74 79 70 65 20 74 6f 20 74 68 65  e.** type to the
1c18d 20 6f 74 68 65 72 20 6f 63 63 75 72 73 20 61 73   other occurs as
1c18e 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 0a   necessary..** .
1c18f 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20 63  ** Most of the c
1c190 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
1c191 20 69 73 20 74 61 6b 65 6e 20 75 70 20 62 79 20   is taken up by 
1c192 74 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 45  the sqlite3VdbeE
1c193 78 65 63 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f  xec().** functio
1c194 6e 20 77 68 69 63 68 20 64 6f 65 73 20 74 68 65  n which does the
1c195 20 77 6f 72 6b 20 6f 66 20 69 6e 74 65 72 70 72   work of interpr
1c196 65 74 69 6e 67 20 61 20 56 44 42 45 20 70 72 6f  eting a VDBE pro
1c197 67 72 61 6d 2e 0a 2a 2a 20 42 75 74 20 6f 74 68  gram..** But oth
1c198 65 72 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  er routines are 
1c199 61 6c 73 6f 20 70 72 6f 76 69 64 65 64 20 74 6f  also provided to
1c19a 20 68 65 6c 70 20 69 6e 20 62 75 69 6c 64 69 6e   help in buildin
1c19b 67 20 75 70 0a 2a 2a 20 61 20 70 72 6f 67 72 61  g up.** a progra
1c19c 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 62 79  m instruction by
1c19d 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a   instruction..**
1c19e 0a 2a 2a 20 56 61 72 69 6f 75 73 20 73 63 72 69  .** Various scri
1c19f 70 74 73 20 73 63 61 6e 20 74 68 69 73 20 73 6f  pts scan this so
1c1a0 75 72 63 65 20 66 69 6c 65 20 69 6e 20 6f 72 64  urce file in ord
1c1a1 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65 20 48  er to generate H
1c1a2 54 4d 4c 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 61  TML.** documenta
1c1a3 74 69 6f 6e 2c 20 68 65 61 64 65 72 73 20 66 69  tion, headers fi
1c1a4 6c 65 73 2c 20 6f 72 20 6f 74 68 65 72 20 64 65  les, or other de
1c1a5 72 69 76 65 64 20 66 69 6c 65 73 2e 20 20 54 68  rived files.  Th
1c1a6 65 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20  e formatting.** 
1c1a7 6f 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74  of the code in t
1c1a8 68 69 73 20 66 69 6c 65 20 69 73 2c 20 74 68 65  his file is, the
1c1a9 72 65 66 6f 72 65 2c 20 69 6d 70 6f 72 74 61 6e  refore, importan
1c1aa 74 2e 20 20 53 65 65 20 6f 74 68 65 72 20 63 6f  t.  See other co
1c1ab 6d 6d 65 6e 74 73 0a 2a 2a 20 69 6e 20 74 68 69  mments.** in thi
1c1ac 73 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61 69  s file for detai
1c1ad 6c 73 2e 20 20 49 66 20 69 6e 20 64 6f 75 62 74  ls.  If in doubt
1c1ae 2c 20 64 6f 20 6e 6f 74 20 64 65 76 69 61 74 65  , do not deviate
1c1af 20 66 72 6f 6d 20 65 78 69 73 74 69 6e 67 0a 2a   from existing.*
1c1b0 2a 20 63 6f 6d 6d 65 6e 74 69 6e 67 20 61 6e 64  * commenting and
1c1b1 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20 70 72 61   indentation pra
1c1b2 63 74 69 63 65 73 20 77 68 65 6e 20 63 68 61 6e  ctices when chan
1c1b3 67 69 6e 67 20 6f 72 20 61 64 64 69 6e 67 20 63  ging or adding c
1c1b4 6f 64 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ode..*/../*.** T
1c1b5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f  he following glo
1c1b6 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20  bal variable is 
1c1b7 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 76 65 72  incremented ever
1c1b8 79 20 74 69 6d 65 20 61 20 63 75 72 73 6f 72 0a  y time a cursor.
1c1b9 2a 2a 20 6d 6f 76 65 73 2c 20 65 69 74 68 65 72  ** moves, either
1c1ba 20 62 79 20 74 68 65 20 4f 50 5f 53 65 65 6b 58   by the OP_SeekX
1c1bb 58 2c 20 4f 50 5f 4e 65 78 74 2c 20 6f 72 20 4f  X, OP_Next, or O
1c1bc 50 5f 50 72 65 76 20 6f 70 63 6f 64 65 73 2e 20  P_Prev opcodes. 
1c1bd 20 54 68 65 20 74 65 73 74 0a 2a 2a 20 70 72 6f   The test.** pro
1c1be 63 65 64 75 72 65 73 20 75 73 65 20 74 68 69 73  cedures use this
1c1bf 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20   information to 
1c1c0 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 69  make sure that i
1c1c1 6e 64 69 63 65 73 20 61 72 65 0a 2a 2a 20 77 6f  ndices are.** wo
1c1c2 72 6b 69 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e  rking correctly.
1c1c3 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20    This variable 
1c1c4 68 61 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20  has no function 
1c1c5 6f 74 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a 2a  other than to.**
1c1c6 20 68 65 6c 70 20 76 65 72 69 66 79 20 74 68 65   help verify the
1c1c7 20 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69   correct operati
1c1c8 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  on of the librar
1c1c9 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  y..*/.#ifdef SQL
1c1ca 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
1c1cb 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1c1cc 73 65 61 72 63 68 5f 63 6f 75 6e 74 20 3d 20 30  search_count = 0
1c1cd 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
1c1ce 57 68 65 6e 20 74 68 69 73 20 67 6c 6f 62 61 6c  When this global
1c1cf 20 76 61 72 69 61 62 6c 65 20 69 73 20 70 6f 73   variable is pos
1c1d0 69 74 69 76 65 2c 20 69 74 20 67 65 74 73 20 64  itive, it gets d
1c1d1 65 63 72 65 6d 65 6e 74 65 64 20 6f 6e 63 65 20  ecremented once 
1c1d2 62 65 66 6f 72 65 0a 2a 2a 20 65 61 63 68 20 69  before.** each i
1c1d3 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68  nstruction in th
1c1d4 65 20 56 44 42 45 2e 20 20 57 68 65 6e 20 72 65  e VDBE.  When re
1c1d5 61 63 68 65 73 20 7a 65 72 6f 2c 20 74 68 65 20  aches zero, the 
1c1d6 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
1c1d7 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65  .** field of the
1c1d8 20 73 71 6c 69 74 65 33 20 73 74 72 75 63 74 75   sqlite3 structu
1c1d9 72 65 20 69 73 20 73 65 74 20 69 6e 20 6f 72 64  re is set in ord
1c1da 65 72 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61  er to simulate a
1c1db 6e 64 20 69 6e 74 65 72 72 75 70 74 2e 0a 2a 2a  nd interrupt..**
1c1dc 0a 2a 2a 20 54 68 69 73 20 66 61 63 69 6c 69 74  .** This facilit
1c1dd 79 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  y is used for te
1c1de 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f  sting purposes o
1c1df 6e 6c 79 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f  nly.  It does no
1c1e0 74 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e  t function.** in
1c1e1 20 61 6e 20 6f 72 64 69 6e 61 72 79 20 62 75 69   an ordinary bui
1c1e2 6c 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ld..*/.#ifdef SQ
1c1e3 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
1c1e4 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1c1e5 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74  _interrupt_count
1c1e6 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a   = 0;.#endif../*
1c1e7 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 6c 6f  .** The next glo
1c1e8 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20  bal variable is 
1c1e9 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 63 68  incremented each
1c1ea 20 74 79 70 65 20 74 68 65 20 4f 50 5f 53 6f 72   type the OP_Sor
1c1eb 74 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73 20 65  t opcode.** is e
1c1ec 78 65 63 75 74 65 64 2e 20 20 54 68 65 20 74 65  xecuted.  The te
1c1ed 73 74 20 70 72 6f 63 65 64 75 72 65 73 20 75 73  st procedures us
1c1ee 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  e this informati
1c1ef 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  on to make sure 
1c1f0 74 68 61 74 0a 2a 2a 20 73 6f 72 74 69 6e 67 20  that.** sorting 
1c1f1 69 73 20 6f 63 63 75 72 72 69 6e 67 20 6f 72 20  is occurring or 
1c1f2 6e 6f 74 20 6f 63 63 75 72 72 69 6e 67 20 61 74  not occurring at
1c1f3 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 69 6d   appropriate tim
1c1f4 65 73 2e 20 20 20 54 68 69 73 20 76 61 72 69 61  es.   This varia
1c1f5 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20 66 75  ble.** has no fu
1c1f6 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61  nction other tha
1c1f7 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 69 66 79  n to help verify
1c1f8 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65   the correct ope
1c1f9 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  ration of the.**
1c1fa 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66   library..*/.#if
1c1fb 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
1c1fc 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1c1fd 71 6c 69 74 65 33 5f 73 6f 72 74 5f 63 6f 75 6e  qlite3_sort_coun
1c1fe 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  t = 0;.#endif../
1c1ff 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 6c  *.** The next gl
1c200 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 72 65  obal variable re
1c201 63 6f 72 64 73 20 74 68 65 20 73 69 7a 65 20 6f  cords the size o
1c202 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 4d 45  f the largest ME
1c203 4d 5f 42 6c 6f 62 0a 2a 2a 20 6f 72 20 4d 45 4d  M_Blob.** or MEM
1c204 5f 53 74 72 20 74 68 61 74 20 68 61 73 20 62 65  _Str that has be
1c205 65 6e 20 75 73 65 64 20 62 79 20 61 20 56 44 42  en used by a VDB
1c206 45 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 74  E opcode.  The t
1c207 65 73 74 20 70 72 6f 63 65 64 75 72 65 73 0a 2a  est procedures.*
1c208 2a 20 75 73 65 20 74 68 69 73 20 69 6e 66 6f 72  * use this infor
1c209 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73  mation to make s
1c20a 75 72 65 20 74 68 61 74 20 74 68 65 20 7a 65 72  ure that the zer
1c20b 6f 2d 62 6c 6f 62 20 66 75 6e 63 74 69 6f 6e 61  o-blob functiona
1c20c 6c 69 74 79 0a 2a 2a 20 69 73 20 77 6f 72 6b 69  lity.** is worki
1c20d 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 20  ng correctly.   
1c20e 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61  This variable ha
1c20f 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74  s no function ot
1c210 68 65 72 20 74 68 61 6e 20 74 6f 0a 2a 2a 20 68  her than to.** h
1c211 65 6c 70 20 76 65 72 69 66 79 20 74 68 65 20 63  elp verify the c
1c212 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f 6e  orrect operation
1c213 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e   of the library.
1c214 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1c215 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
1c216 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 61  I int sqlite3_ma
1c217 78 5f 62 6c 6f 62 73 69 7a 65 20 3d 20 30 3b 0a  x_blobsize = 0;.
1c218 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64 61  static void upda
1c219 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65 28 4d 65  teMaxBlobsize(Me
1c21a 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d  m *p){.  if( (p-
1c21b 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74  >flags & (MEM_St
1c21c 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 21 3d 30 20  r|MEM_Blob))!=0 
1c21d 26 26 20 70 2d 3e 6e 3e 73 71 6c 69 74 65 33 5f  && p->n>sqlite3_
1c21e 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20 29 7b 0a  max_blobsize ){.
1c21f 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 61 78 5f      sqlite3_max_
1c220 62 6c 6f 62 73 69 7a 65 20 3d 20 70 2d 3e 6e 3b  blobsize = p->n;
1c221 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
1c222 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 6c  *.** The next gl
1c223 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73  obal variable is
1c224 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 63   incremented eac
1c225 68 20 74 79 70 65 20 74 68 65 20 4f 50 5f 46 6f  h type the OP_Fo
1c226 75 6e 64 20 6f 70 63 6f 64 65 0a 2a 2a 20 69 73  und opcode.** is
1c227 20 65 78 65 63 75 74 65 64 2e 20 54 68 69 73 20   executed. This 
1c228 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 20  is used to test 
1c229 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
1c22a 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 2a  he foreign key.*
1c22b 2a 20 6f 70 65 72 61 74 69 6f 6e 20 69 6d 70 6c  * operation impl
1c22c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 4f 50  emented using OP
1c22d 5f 46 6b 49 73 5a 65 72 6f 20 69 73 20 77 6f 72  _FkIsZero is wor
1c22e 6b 69 6e 67 2e 20 54 68 69 73 20 76 61 72 69 61  king. This varia
1c22f 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20 66 75  ble.** has no fu
1c230 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61  nction other tha
1c231 6e 20 74 6f 20 68 65 6c 70 20 76 65 72 69 66 79  n to help verify
1c232 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70 65   the correct ope
1c233 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  ration of the.**
1c234 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66   library..*/.#if
1c235 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
1c236 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1c237 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f 63 6f 75  qlite3_found_cou
1c238 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a  nt = 0;.#endif..
1c239 2f 2a 0a 2a 2a 20 54 65 73 74 20 61 20 72 65 67  /*.** Test a reg
1c23a 69 73 74 65 72 20 74 6f 20 73 65 65 20 69 66 20  ister to see if 
1c23b 69 74 20 65 78 63 65 65 64 73 20 74 68 65 20 63  it exceeds the c
1c23c 75 72 72 65 6e 74 20 6d 61 78 69 6d 75 6d 20 62  urrent maximum b
1c23d 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2a 20 49 66 20  lob size..** If 
1c23e 69 74 20 64 6f 65 73 2c 20 72 65 63 6f 72 64 20  it does, record 
1c23f 74 68 65 20 6e 65 77 20 6d 61 78 69 6d 75 6d 20  the new maximum 
1c240 62 6c 6f 62 20 73 69 7a 65 2e 0a 2a 2f 0a 23 69  blob size..*/.#i
1c241 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
1c242 5f 54 45 53 54 29 20 26 26 20 21 64 65 66 69 6e  _TEST) && !defin
1c243 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ed(SQLITE_OMIT_B
1c244 55 49 4c 54 49 4e 5f 54 45 53 54 29 0a 23 20 64  UILTIN_TEST).# d
1c245 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d 41 58  efine UPDATE_MAX
1c246 5f 42 4c 4f 42 53 49 5a 45 28 50 29 20 20 75 70  _BLOBSIZE(P)  up
1c247 64 61 74 65 4d 61 78 42 6c 6f 62 73 69 7a 65 28  dateMaxBlobsize(
1c248 50 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  P).#else.# defin
1c249 65 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f  e UPDATE_MAX_BLO
1c24a 42 53 49 5a 45 28 50 29 0a 23 65 6e 64 69 66 0a  BSIZE(P).#endif.
1c24b 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74  ./*.** Convert t
1c24c 68 65 20 67 69 76 65 6e 20 72 65 67 69 73 74 65  he given registe
1c24d 72 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20  r into a string 
1c24e 69 66 20 69 74 20 69 73 6e 27 74 20 6f 6e 65 0a  if it isn't one.
1c24f 2a 2a 20 61 6c 72 65 61 64 79 2e 20 52 65 74 75  ** already. Retu
1c250 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61  rn non-zero if a
1c251 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e   malloc() fails.
1c252 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 74 72 69  .*/.#define Stri
1c253 6e 67 69 66 79 28 50 2c 20 65 6e 63 29 20 5c 0a  ngify(P, enc) \.
1c254 20 20 20 69 66 28 28 28 50 29 2d 3e 66 6c 61 67     if(((P)->flag
1c255 73 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42  s&(MEM_Str|MEM_B
1c256 6c 6f 62 29 29 3d 3d 30 20 26 26 20 73 71 6c 69  lob))==0 && sqli
1c257 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67  te3VdbeMemString
1c258 69 66 79 28 50 2c 65 6e 63 29 29 20 5c 0a 20 20  ify(P,enc)) \.  
1c259 20 20 20 7b 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d     { goto no_mem
1c25a 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 65 70  ; }../*.** An ep
1c25b 68 65 6d 65 72 61 6c 20 73 74 72 69 6e 67 20 76  hemeral string v
1c25c 61 6c 75 65 20 28 73 69 67 6e 69 66 69 65 64 20  alue (signified 
1c25d 62 79 20 74 68 65 20 4d 45 4d 5f 45 70 68 65 6d  by the MEM_Ephem
1c25e 20 66 6c 61 67 29 20 63 6f 6e 74 61 69 6e 73 0a   flag) contains.
1c25f 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
1c260 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  a dynamically al
1c261 6c 6f 63 61 74 65 64 20 73 74 72 69 6e 67 20 77  located string w
1c262 68 65 72 65 20 73 6f 6d 65 20 6f 74 68 65 72 20  here some other 
1c263 65 6e 74 69 74 79 0a 2a 2a 20 69 73 20 72 65 73  entity.** is res
1c264 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 64 65 61  ponsible for dea
1c265 6c 6c 6f 63 61 74 69 6e 67 20 74 68 61 74 20 73  llocating that s
1c266 74 72 69 6e 67 2e 20 20 42 65 63 61 75 73 65 20  tring.  Because 
1c267 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20  the register.** 
1c268 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 72 6f 6c  does not control
1c269 20 74 68 65 20 73 74 72 69 6e 67 2c 20 69 74 20   the string, it 
1c26a 6d 69 67 68 74 20 62 65 20 64 65 6c 65 74 65 64  might be deleted
1c26b 20 77 69 74 68 6f 75 74 20 74 68 65 20 72 65 67   without the reg
1c26c 69 73 74 65 72 0a 2a 2a 20 6b 6e 6f 77 69 6e 67  ister.** knowing
1c26d 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20   it..**.** This 
1c26e 72 6f 75 74 69 6e 65 20 63 6f 6e 76 65 72 74 73  routine converts
1c26f 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74   an ephemeral st
1c270 72 69 6e 67 20 69 6e 74 6f 20 61 20 64 79 6e 61  ring into a dyna
1c271 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
1c272 64 0a 2a 2a 20 73 74 72 69 6e 67 20 74 68 61 74  d.** string that
1c273 20 74 68 65 20 72 65 67 69 73 74 65 72 20 69 74   the register it
1c274 73 65 6c 66 20 63 6f 6e 74 72 6f 6c 73 2e 20 20  self controls.  
1c275 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
1c276 69 74 0a 2a 2a 20 63 6f 6e 76 65 72 74 73 20 61  it.** converts a
1c277 6e 20 4d 45 4d 5f 45 70 68 65 6d 20 73 74 72 69  n MEM_Ephem stri
1c278 6e 67 20 69 6e 74 6f 20 61 6e 20 4d 45 4d 5f 44  ng into an MEM_D
1c279 79 6e 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 23 64  yn string..*/.#d
1c27a 65 66 69 6e 65 20 44 65 65 70 68 65 6d 65 72 61  efine Deephemera
1c27b 6c 69 7a 65 28 50 29 20 5c 0a 20 20 20 69 66 28  lize(P) \.   if(
1c27c 20 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d   ((P)->flags&MEM
1c27d 5f 45 70 68 65 6d 29 21 3d 30 20 5c 0a 20 20 20  _Ephem)!=0 \.   
1c27e 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 56 64      && sqlite3Vd
1c27f 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
1c280 6c 65 28 50 29 20 29 7b 20 67 6f 74 6f 20 6e 6f  le(P) ){ goto no
1c281 5f 6d 65 6d 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61  _mem;}../*.** Ca
1c282 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  ll sqlite3VdbeMe
1c283 6d 45 78 70 61 6e 64 42 6c 6f 62 28 29 20 6f 6e  mExpandBlob() on
1c284 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 76 61   the supplied va
1c285 6c 75 65 20 28 74 79 70 65 20 4d 65 6d 2a 29 0a  lue (type Mem*).
1c286 2a 2a 20 50 20 69 66 20 72 65 71 75 69 72 65 64  ** P if required
1c287 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 78 70  ..*/.#define Exp
1c288 61 6e 64 42 6c 6f 62 28 50 29 20 28 28 28 50 29  andBlob(P) (((P)
1c289 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f  ->flags&MEM_Zero
1c28a 29 3f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  )?sqlite3VdbeMem
1c28b 45 78 70 61 6e 64 42 6c 6f 62 28 50 29 3a 30 29  ExpandBlob(P):0)
1c28c 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
1c28d 20 70 4d 65 6d 20 70 6f 69 6e 74 73 20 61 74 20   pMem points at 
1c28e 61 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20  a register that 
1c28f 77 69 6c 6c 20 62 65 20 70 61 73 73 65 64 20 74  will be passed t
1c290 6f 20 61 0a 2a 2a 20 75 73 65 72 2d 64 65 66 69  o a.** user-defi
1c291 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20  ned function or 
1c292 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
1c293 75 73 65 72 20 61 73 20 74 68 65 20 72 65 73 75  user as the resu
1c294 6c 74 20 6f 66 20 61 20 71 75 65 72 79 2e 0a 2a  lt of a query..*
1c295 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
1c296 65 74 73 20 74 68 65 20 70 4d 65 6d 2d 3e 74 79  ets the pMem->ty
1c297 70 65 20 76 61 72 69 61 62 6c 65 20 75 73 65 64  pe variable used
1c298 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f   by the sqlite3_
1c299 76 61 6c 75 65 5f 2a 28 29 20 0a 2a 2a 20 72 6f  value_*() .** ro
1c29a 75 74 69 6e 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  utines..*/.SQLIT
1c29b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1c29c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f  qlite3VdbeMemSto
1c29d 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d 65 6d  reType(Mem *pMem
1c29e 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d  ){.  int flags =
1c29f 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20   pMem->flags;.  
1c2a0 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f  if( flags & MEM_
1c2a1 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d  Null ){.    pMem
1c2a2 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
1c2a3 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65  NULL;.  }.  else
1c2a4 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d   if( flags & MEM
1c2a5 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d  _Int ){.    pMem
1c2a6 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
1c2a7 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65  INTEGER;.  }.  e
1c2a8 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
1c2a9 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
1c2aa 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
1c2ab 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20  ITE_FLOAT;.  }. 
1c2ac 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20   else if( flags 
1c2ad 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20  & MEM_Str ){.   
1c2ae 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
1c2af 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c  LITE_TEXT;.  }el
1c2b0 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79  se{.    pMem->ty
1c2b1 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  pe = SQLITE_BLOB
1c2b2 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
1c2b3 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 75 72 73  llocate VdbeCurs
1c2b4 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2e 20  or number iCur. 
1c2b5 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
1c2b6 72 20 74 6f 20 69 74 2e 20 20 52 65 74 75 72 6e  r to it.  Return
1c2b7 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 65 20 72   NULL.** if we r
1c2b8 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  un out of memory
1c2b9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 64 62 65  ..*/.static Vdbe
1c2ba 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 61 74 65  Cursor *allocate
1c2bb 43 75 72 73 6f 72 28 0a 20 20 56 64 62 65 20 2a  Cursor(.  Vdbe *
1c2bc 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
1c2bd 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 6d  /* The virtual m
1c2be 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  achine */.  int 
1c2bf 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20  iCur,           
1c2c0 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
1c2c1 65 20 6e 65 77 20 56 64 62 65 43 75 72 73 6f 72  e new VdbeCursor
1c2c2 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64   */.  int nField
1c2c3 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
1c2c4 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20  umber of fields 
1c2c5 69 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  in the table or 
1c2c6 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69  index */.  int i
1c2c7 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  Db,             
1c2c8 20 2f 2a 20 57 68 65 6e 20 64 61 74 61 62 61 73   /* When databas
1c2c9 65 20 74 68 65 20 63 75 72 73 6f 72 20 62 65 6c  e the cursor bel
1c2ca 6f 6e 67 73 20 74 6f 2c 20 6f 72 20 2d 31 20 2a  ongs to, or -1 *
1c2cb 2f 0a 20 20 69 6e 74 20 69 73 42 74 72 65 65 43  /.  int isBtreeC
1c2cc 75 72 73 6f 72 20 20 20 20 20 2f 2a 20 54 72 75  ursor     /* Tru
1c2cd 65 20 66 6f 72 20 42 2d 54 72 65 65 2e 20 20 46  e for B-Tree.  F
1c2ce 61 6c 73 65 20 66 6f 72 20 70 73 65 75 64 6f 2d  alse for pseudo-
1c2cf 74 61 62 6c 65 20 6f 72 20 76 74 61 62 20 2a 2f  table or vtab */
1c2d0 0a 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68  .){.  /* Find th
1c2d1 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68  e memory cell th
1c2d2 61 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  at will be used 
1c2d3 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 6c 6f  to store the blo
1c2d4 62 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a  b of memory.  **
1c2d5 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68   required for th
1c2d6 69 73 20 56 64 62 65 43 75 72 73 6f 72 20 73 74  is VdbeCursor st
1c2d7 72 75 63 74 75 72 65 2e 20 49 74 20 69 73 20 63  ructure. It is c
1c2d8 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 75 73 65  onvenient to use
1c2d9 20 61 20 0a 20 20 2a 2a 20 76 64 62 65 20 6d 65   a .  ** vdbe me
1c2da 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 6d 61 6e  mory cell to man
1c2db 61 67 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  age the memory a
1c2dc 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72  llocation requir
1c2dd 65 64 20 66 6f 72 20 61 0a 20 20 2a 2a 20 56 64  ed for a.  ** Vd
1c2de 62 65 43 75 72 73 6f 72 20 73 74 72 75 63 74 75  beCursor structu
1c2df 72 65 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f  re for the follo
1c2e0 77 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 20 20  wing reasons:.  
1c2e1 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65  **.  **   * Some
1c2e2 74 69 6d 65 73 20 63 75 72 73 6f 72 20 6e 75 6d  times cursor num
1c2e3 62 65 72 73 20 61 72 65 20 75 73 65 64 20 66 6f  bers are used fo
1c2e4 72 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69  r a couple of di
1c2e5 66 66 65 72 65 6e 74 0a 20 20 2a 2a 20 20 20 20  fferent.  **    
1c2e6 20 70 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76   purposes in a v
1c2e7 64 62 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65  dbe program. The
1c2e8 20 64 69 66 66 65 72 65 6e 74 20 75 73 65 73 20   different uses 
1c2e9 6d 69 67 68 74 20 72 65 71 75 69 72 65 0a 20 20  might require.  
1c2ea 2a 2a 20 20 20 20 20 64 69 66 66 65 72 65 6e 74  **     different
1c2eb 20 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f   sized allocatio
1c2ec 6e 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73  ns. Memory cells
1c2ed 20 70 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c   provide growabl
1c2ee 65 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63  e.  **     alloc
1c2ef 61 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a  ations..  **.  *
1c2f0 2a 20 20 20 2a 20 57 68 65 6e 20 75 73 69 6e 67  *   * When using
1c2f1 20 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d   ENABLE_MEMORY_M
1c2f2 41 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d 6f 72  ANAGEMENT, memor
1c2f3 79 20 63 65 6c 6c 20 62 75 66 66 65 72 73 20 63  y cell buffers c
1c2f4 61 6e 0a 20 20 2a 2a 20 20 20 20 20 62 65 20 66  an.  **     be f
1c2f5 72 65 65 64 20 6c 61 7a 69 6c 79 20 76 69 61 20  reed lazily via 
1c2f6 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  the sqlite3_rele
1c2f7 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 41 50 49  ase_memory() API
1c2f8 2e 20 54 68 69 73 0a 20 20 2a 2a 20 20 20 20 20  . This.  **     
1c2f9 6d 69 6e 69 6d 69 7a 65 73 20 74 68 65 20 6e 75  minimizes the nu
1c2fa 6d 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 63  mber of malloc c
1c2fb 61 6c 6c 73 20 6d 61 64 65 20 62 79 20 74 68 65  alls made by the
1c2fc 20 73 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a 20 20   system..  **.  
1c2fd 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  ** Memory cells 
1c2fe 66 6f 72 20 63 75 72 73 6f 72 73 20 61 72 65 20  for cursors are 
1c2ff 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65  allocated at the
1c300 20 74 6f 70 20 6f 66 20 74 68 65 20 61 64 64 72   top of the addr
1c301 65 73 73 0a 20 20 2a 2a 20 73 70 61 63 65 2e 20  ess.  ** space. 
1c302 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e  Memory cell (p->
1c303 6e 4d 65 6d 29 20 63 6f 72 72 65 73 70 6f 6e 64  nMem) correspond
1c304 73 20 74 6f 20 63 75 72 73 6f 72 20 30 2e 20 53  s to cursor 0. S
1c305 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 63 75  pace for.  ** cu
1c306 72 73 6f 72 20 31 20 69 73 20 6d 61 6e 61 67 65  rsor 1 is manage
1c307 64 20 62 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  d by memory cell
1c308 20 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 65 74   (p->nMem-1), et
1c309 63 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  c..  */.  Mem *p
1c30a 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Mem = &p->aMem[p
1c30b 2d 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a 0a 20  ->nMem-iCur];.. 
1c30c 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 56 64   int nByte;.  Vd
1c30d 62 65 43 75 72 73 6f 72 20 2a 70 43 78 20 3d 20  beCursor *pCx = 
1c30e 30 3b 0a 20 20 6e 42 79 74 65 20 3d 20 0a 20 20  0;.  nByte = .  
1c30f 20 20 20 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f      ROUND8(sizeo
1c310 66 28 56 64 62 65 43 75 72 73 6f 72 29 29 20 2b  f(VdbeCursor)) +
1c311 20 0a 20 20 20 20 20 20 28 69 73 42 74 72 65 65   .      (isBtree
1c312 43 75 72 73 6f 72 3f 73 71 6c 69 74 65 33 42 74  Cursor?sqlite3Bt
1c313 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29 3a  reeCursorSize():
1c314 30 29 20 2b 20 0a 20 20 20 20 20 20 32 2a 6e 46  0) + .      2*nF
1c315 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32 29  ield*sizeof(u32)
1c316 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 75  ;..  assert( iCu
1c317 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  r<p->nCursor );.
1c318 20 20 69 66 28 20 70 2d 3e 61 70 43 73 72 5b 69    if( p->apCsr[i
1c319 43 75 72 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69  Cur] ){.    sqli
1c31a 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f  te3VdbeFreeCurso
1c31b 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69 43  r(p, p->apCsr[iC
1c31c 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 70 43  ur]);.    p->apC
1c31d 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b 0a 20 20  sr[iCur] = 0;.  
1c31e 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  }.  if( SQLITE_O
1c31f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62 65 4d 65  K==sqlite3VdbeMe
1c320 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74  mGrow(pMem, nByt
1c321 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 70 2d 3e  e, 0) ){.    p->
1c322 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 70 43  apCsr[iCur] = pC
1c323 78 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72 2a  x = (VdbeCursor*
1c324 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d 65  )pMem->z;.    me
1c325 6d 73 65 74 28 70 43 78 2c 20 30 2c 20 73 69 7a  mset(pCx, 0, siz
1c326 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 29  eof(VdbeCursor))
1c327 3b 0a 20 20 20 20 70 43 78 2d 3e 69 44 62 20 3d  ;.    pCx->iDb =
1c328 20 69 44 62 3b 0a 20 20 20 20 70 43 78 2d 3e 6e   iDb;.    pCx->n
1c329 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b 0a  Field = nField;.
1c32a 20 20 20 20 69 66 28 20 6e 46 69 65 6c 64 20 29      if( nField )
1c32b 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 61 54 79  {.      pCx->aTy
1c32c 70 65 20 3d 20 28 75 33 32 20 2a 29 26 70 4d 65  pe = (u32 *)&pMe
1c32d 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 69 7a 65  m->z[ROUND8(size
1c32e 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 29 5d  of(VdbeCursor))]
1c32f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1c330 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 29 7b  isBtreeCursor ){
1c331 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 43 75 72  .      pCx->pCur
1c332 73 6f 72 20 3d 20 28 42 74 43 75 72 73 6f 72 2a  sor = (BtCursor*
1c333 29 0a 20 20 20 20 20 20 20 20 20 20 26 70 4d 65  ).          &pMe
1c334 6d 2d 3e 7a 5b 52 4f 55 4e 44 38 28 73 69 7a 65  m->z[ROUND8(size
1c335 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 29 2b  of(VdbeCursor))+
1c336 32 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28  2*nField*sizeof(
1c337 75 33 32 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c  u32)];.      sql
1c338 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 5a  ite3BtreeCursorZ
1c339 65 72 6f 28 70 43 78 2d 3e 70 43 75 72 73 6f 72  ero(pCx->pCursor
1c33a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
1c33b 65 74 75 72 6e 20 70 43 78 3b 0a 7d 0a 0a 2f 2a  eturn pCx;.}../*
1c33c 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 65  .** Try to conve
1c33d 72 74 20 61 20 76 61 6c 75 65 20 69 6e 74 6f 20  rt a value into 
1c33e 61 20 6e 75 6d 65 72 69 63 20 72 65 70 72 65 73  a numeric repres
1c33f 65 6e 74 61 74 69 6f 6e 20 69 66 20 77 65 20 63  entation if we c
1c340 61 6e 0a 2a 2a 20 64 6f 20 73 6f 20 77 69 74 68  an.** do so with
1c341 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f  out loss of info
1c342 72 6d 61 74 69 6f 6e 2e 20 20 49 6e 20 6f 74 68  rmation.  In oth
1c343 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65  er words, if the
1c344 20 73 74 72 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b 73   string.** looks
1c345 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20   like a number, 
1c346 63 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20  convert it into 
1c347 61 20 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74  a number.  If it
1c348 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 6f   does not.** loo
1c349 6b 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c  k like a number,
1c34a 20 6c 65 61 76 65 20 69 74 20 61 6c 6f 6e 65 2e   leave it alone.
1c34b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1c34c 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69  applyNumericAffi
1c34d 6e 69 74 79 28 4d 65 6d 20 2a 70 52 65 63 29 7b  nity(Mem *pRec){
1c34e 0a 20 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c  .  if( (pRec->fl
1c34f 61 67 73 20 26 20 28 4d 45 4d 5f 52 65 61 6c 7c  ags & (MEM_Real|
1c350 4d 45 4d 5f 49 6e 74 29 29 3d 3d 30 20 29 7b 0a  MEM_Int))==0 ){.
1c351 20 20 20 20 69 6e 74 20 72 65 61 6c 6e 75 6d 3b      int realnum;
1c352 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1c353 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28  MemNulTerminate(
1c354 70 52 65 63 29 3b 0a 20 20 20 20 69 66 28 20 28  pRec);.    if( (
1c355 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pRec->flags&MEM_
1c356 53 74 72 29 0a 20 20 20 20 20 20 20 20 20 26 26  Str).         &&
1c357 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72   sqlite3IsNumber
1c358 28 70 52 65 63 2d 3e 7a 2c 20 26 72 65 61 6c 6e  (pRec->z, &realn
1c359 75 6d 2c 20 70 52 65 63 2d 3e 65 6e 63 29 20 29  um, pRec->enc) )
1c35a 7b 0a 20 20 20 20 20 20 69 36 34 20 76 61 6c 75  {.      i64 valu
1c35b 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e;.      sqlite3
1c35c 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
1c35d 6e 67 28 70 52 65 63 2c 20 53 51 4c 49 54 45 5f  ng(pRec, SQLITE_
1c35e 55 54 46 38 29 3b 0a 20 20 20 20 20 20 69 66 28  UTF8);.      if(
1c35f 20 21 72 65 61 6c 6e 75 6d 20 26 26 20 73 71 6c   !realnum && sql
1c360 69 74 65 33 41 74 6f 69 36 34 28 70 52 65 63 2d  ite3Atoi64(pRec-
1c361 3e 7a 2c 20 26 76 61 6c 75 65 29 20 29 7b 0a 20  >z, &value) ){. 
1c362 20 20 20 20 20 20 20 70 52 65 63 2d 3e 75 2e 69         pRec->u.i
1c363 20 3d 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20   = value;.      
1c364 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1c365 28 70 52 65 63 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pRec, MEM_Int);
1c366 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1c367 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1c368 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 52 65 63  eMemRealify(pRec
1c369 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1c36a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  .  }.}../*.** Pr
1c36b 6f 63 65 73 73 69 6e 67 20 69 73 20 64 65 74 65  ocessing is dete
1c36c 72 6d 69 6e 65 20 62 79 20 74 68 65 20 61 66 66  rmine by the aff
1c36d 69 6e 69 74 79 20 70 61 72 61 6d 65 74 65 72 3a  inity parameter:
1c36e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46  .**.** SQLITE_AF
1c36f 46 5f 49 4e 54 45 47 45 52 3a 0a 2a 2a 20 53 51  F_INTEGER:.** SQ
1c370 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3a 0a 2a  LITE_AFF_REAL:.*
1c371 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  * SQLITE_AFF_NUM
1c372 45 52 49 43 3a 0a 2a 2a 20 20 20 20 54 72 79 20  ERIC:.**    Try 
1c373 74 6f 20 63 6f 6e 76 65 72 74 20 70 52 65 63 20  to convert pRec 
1c374 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65  to an integer re
1c375 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 72 20  presentation or 
1c376 61 20 0a 2a 2a 20 20 20 20 66 6c 6f 61 74 69 6e  a .**    floatin
1c377 67 2d 70 6f 69 6e 74 20 72 65 70 72 65 73 65 6e  g-point represen
1c378 74 61 74 69 6f 6e 20 69 66 20 61 6e 20 69 6e 74  tation if an int
1c379 65 67 65 72 20 72 65 70 72 65 73 65 6e 74 61 74  eger representat
1c37a 69 6f 6e 0a 2a 2a 20 20 20 20 69 73 20 6e 6f 74  ion.**    is not
1c37b 20 70 6f 73 73 69 62 6c 65 2e 20 20 4e 6f 74 65   possible.  Note
1c37c 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 67 65   that the intege
1c37d 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  r representation
1c37e 20 69 73 0a 2a 2a 20 20 20 20 61 6c 77 61 79 73   is.**    always
1c37f 20 70 72 65 66 65 72 72 65 64 2c 20 65 76 65 6e   preferred, even
1c380 20 69 66 20 74 68 65 20 61 66 66 69 6e 69 74 79   if the affinity
1c381 20 69 73 20 52 45 41 4c 2c 20 62 65 63 61 75 73   is REAL, becaus
1c382 65 0a 2a 2a 20 20 20 20 61 6e 20 69 6e 74 65 67  e.**    an integ
1c383 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  er representatio
1c384 6e 20 69 73 20 6d 6f 72 65 20 73 70 61 63 65 20  n is more space 
1c385 65 66 66 69 63 69 65 6e 74 20 6f 6e 20 64 69 73  efficient on dis
1c386 6b 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  k..**.** SQLITE_
1c387 41 46 46 5f 54 45 58 54 3a 0a 2a 2a 20 20 20 20  AFF_TEXT:.**    
1c388 43 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f 20  Convert pRec to 
1c389 61 20 74 65 78 74 20 72 65 70 72 65 73 65 6e 74  a text represent
1c38a 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  ation..**.** SQL
1c38b 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 2a 2a  ITE_AFF_NONE:.**
1c38c 20 20 20 20 4e 6f 2d 6f 70 2e 20 20 70 52 65 63      No-op.  pRec
1c38d 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a   is unchanged..*
1c38e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 70  /.static void ap
1c38f 70 6c 79 41 66 66 69 6e 69 74 79 28 0a 20 20 4d  plyAffinity(.  M
1c390 65 6d 20 2a 70 52 65 63 2c 20 20 20 20 20 20 20  em *pRec,       
1c391 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20     /* The value 
1c392 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74  to apply affinit
1c393 79 20 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20 61  y to */.  char a
1c394 66 66 69 6e 69 74 79 2c 20 20 20 20 20 20 2f 2a  ffinity,      /*
1c395 20 54 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f   The affinity to
1c396 20 62 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20   be applied */. 
1c397 20 75 38 20 65 6e 63 20 20 20 20 20 20 20 20 20   u8 enc         
1c398 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 69 73       /* Use this
1c399 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a   text encoding *
1c39a 2f 0a 29 7b 0a 20 20 69 66 28 20 61 66 66 69 6e  /.){.  if( affin
1c39b 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
1c39c 54 45 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20 4f  TEXT ){.    /* O
1c39d 6e 6c 79 20 61 74 74 65 6d 70 74 20 74 68 65 20  nly attempt the 
1c39e 63 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20 54 45  conversion to TE
1c39f 58 54 20 69 66 20 74 68 65 72 65 20 69 73 20 61  XT if there is a
1c3a0 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 72 65 61  n integer or rea
1c3a1 6c 0a 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65  l.    ** represe
1c3a2 6e 74 61 74 69 6f 6e 20 28 62 6c 6f 62 20 61 6e  ntation (blob an
1c3a3 64 20 4e 55 4c 4c 20 64 6f 20 6e 6f 74 20 67 65  d NULL do not ge
1c3a4 74 20 63 6f 6e 76 65 72 74 65 64 29 20 62 75 74  t converted) but
1c3a5 20 6e 6f 20 73 74 72 69 6e 67 0a 20 20 20 20 2a   no string.    *
1c3a6 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  * representation
1c3a7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1c3a8 20 30 3d 3d 28 70 52 65 63 2d 3e 66 6c 61 67 73   0==(pRec->flags
1c3a9 26 4d 45 4d 5f 53 74 72 29 20 26 26 20 28 70 52  &MEM_Str) && (pR
1c3aa 65 63 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 52  ec->flags&(MEM_R
1c3ab 65 61 6c 7c 4d 45 4d 5f 49 6e 74 29 29 20 29 7b  eal|MEM_Int)) ){
1c3ac 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1c3ad 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70  beMemStringify(p
1c3ae 52 65 63 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d  Rec, enc);.    }
1c3af 0a 20 20 20 20 70 52 65 63 2d 3e 66 6c 61 67 73  .    pRec->flags
1c3b0 20 26 3d 20 7e 28 4d 45 4d 5f 52 65 61 6c 7c 4d   &= ~(MEM_Real|M
1c3b1 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65  EM_Int);.  }else
1c3b2 20 69 66 28 20 61 66 66 69 6e 69 74 79 21 3d 53   if( affinity!=S
1c3b3 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29  QLITE_AFF_NONE )
1c3b4 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 66  {.    assert( af
1c3b5 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
1c3b6 46 46 5f 49 4e 54 45 47 45 52 20 7c 7c 20 61 66  FF_INTEGER || af
1c3b7 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41  finity==SQLITE_A
1c3b8 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 20 20 20  FF_REAL.        
1c3b9 20 20 20 20 20 7c 7c 20 61 66 66 69 6e 69 74 79       || affinity
1c3ba 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  ==SQLITE_AFF_NUM
1c3bb 45 52 49 43 20 29 3b 0a 20 20 20 20 61 70 70 6c  ERIC );.    appl
1c3bc 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79  yNumericAffinity
1c3bd 28 70 52 65 63 29 3b 0a 20 20 20 20 69 66 28 20  (pRec);.    if( 
1c3be 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pRec->flags & ME
1c3bf 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20  M_Real ){.      
1c3c0 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67  sqlite3VdbeInteg
1c3c1 65 72 41 66 66 69 6e 69 74 79 28 70 52 65 63 29  erAffinity(pRec)
1c3c2 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
1c3c3 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76  *.** Try to conv
1c3c4 65 72 74 20 74 68 65 20 74 79 70 65 20 6f 66 20  ert the type of 
1c3c5 61 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d  a function argum
1c3c6 65 6e 74 20 6f 72 20 61 20 72 65 73 75 6c 74 20  ent or a result 
1c3c7 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 61  column.** into a
1c3c8 20 6e 75 6d 65 72 69 63 20 72 65 70 72 65 73 65   numeric represe
1c3c9 6e 74 61 74 69 6f 6e 2e 20 20 55 73 65 20 65 69  ntation.  Use ei
1c3ca 74 68 65 72 20 49 4e 54 45 47 45 52 20 6f 72 20  ther INTEGER or 
1c3cb 52 45 41 4c 20 77 68 69 63 68 65 76 65 72 0a 2a  REAL whichever.*
1c3cc 2a 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65  * is appropriate
1c3cd 2e 20 20 42 75 74 20 6f 6e 6c 79 20 64 6f 20 74  .  But only do t
1c3ce 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66  he conversion if
1c3cf 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
1c3d0 77 69 74 68 6f 75 74 0a 2a 2a 20 6c 6f 73 73 20  without.** loss 
1c3d1 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  of information a
1c3d2 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 72 65  nd return the re
1c3d3 76 69 73 65 64 20 74 79 70 65 20 6f 66 20 74 68  vised type of th
1c3d4 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a  e argument..**.*
1c3d5 2a 20 54 68 69 73 20 69 73 20 61 6e 20 45 58 50  * This is an EXP
1c3d6 45 52 49 4d 45 4e 54 41 4c 20 61 70 69 20 61 6e  ERIMENTAL api an
1c3d7 64 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20  d is subject to 
1c3d8 63 68 61 6e 67 65 20 6f 72 20 72 65 6d 6f 76 61  change or remova
1c3d9 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  l..*/.SQLITE_API
1c3da 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
1c3db 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28  ue_numeric_type(
1c3dc 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
1c3dd 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65  Val){.  Mem *pMe
1c3de 6d 20 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a  m = (Mem*)pVal;.
1c3df 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66    applyNumericAf
1c3e0 66 69 6e 69 74 79 28 70 4d 65 6d 29 3b 0a 20 20  finity(pMem);.  
1c3e1 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74  sqlite3VdbeMemSt
1c3e2 6f 72 65 54 79 70 65 28 70 4d 65 6d 29 3b 0a 20  oreType(pMem);. 
1c3e3 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 74 79   return pMem->ty
1c3e4 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70  pe;.}../*.** Exp
1c3e5 6f 72 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  orted version of
1c3e6 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 29   applyAffinity()
1c3e7 2e 20 54 68 69 73 20 6f 6e 65 20 77 6f 72 6b 73  . This one works
1c3e8 20 6f 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   on sqlite3_valu
1c3e9 65 2a 2c 20 0a 2a 2a 20 6e 6f 74 20 74 68 65 20  e*, .** not the 
1c3ea 69 6e 74 65 72 6e 61 6c 20 4d 65 6d 2a 20 74 79  internal Mem* ty
1c3eb 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  pe..*/.SQLITE_PR
1c3ec 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1c3ed 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69  e3ValueApplyAffi
1c3ee 6e 69 74 79 28 0a 20 20 73 71 6c 69 74 65 33 5f  nity(.  sqlite3_
1c3ef 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 0a 20 20  value *pVal, .  
1c3f0 75 38 20 61 66 66 69 6e 69 74 79 2c 20 0a 20 20  u8 affinity, .  
1c3f1 75 38 20 65 6e 63 0a 29 7b 0a 20 20 61 70 70 6c  u8 enc.){.  appl
1c3f2 79 41 66 66 69 6e 69 74 79 28 28 4d 65 6d 20 2a  yAffinity((Mem *
1c3f3 29 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c  )pVal, affinity,
1c3f4 20 65 6e 63 29 3b 0a 7d 0a 0a 23 69 66 64 65 66   enc);.}..#ifdef
1c3f5 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a   SQLITE_DEBUG./*
1c3f6 0a 2a 2a 20 57 72 69 74 65 20 61 20 6e 69 63 65  .** Write a nice
1c3f7 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e   string represen
1c3f8 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f  tation of the co
1c3f9 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70  ntents of cell p
1c3fa 4d 65 6d 0a 2a 2a 20 69 6e 74 6f 20 62 75 66 66  Mem.** into buff
1c3fb 65 72 20 7a 42 75 66 2c 20 6c 65 6e 67 74 68 20  er zBuf, length 
1c3fc 6e 42 75 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nBuf..*/.SQLITE_
1c3fd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1c3fe 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74  ite3VdbeMemPrett
1c3ff 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d  yPrint(Mem *pMem
1c400 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20  , char *zBuf){. 
1c401 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20 7a 42   char *zCsr = zB
1c402 75 66 3b 0a 20 20 69 6e 74 20 66 20 3d 20 70 4d  uf;.  int f = pM
1c403 65 6d 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20 73 74  em->flags;..  st
1c404 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
1c405 2a 63 6f 6e 73 74 20 65 6e 63 6e 61 6d 65 73 5b  *const encnames[
1c406 5d 20 3d 20 7b 22 28 58 29 22 2c 20 22 28 38 29  ] = {"(X)", "(8)
1c407 22 2c 20 22 28 31 36 4c 45 29 22 2c 20 22 28 31  ", "(16LE)", "(1
1c408 36 42 45 29 22 7d 3b 0a 0a 20 20 69 66 28 20 66  6BE)"};..  if( f
1c409 26 4d 45 4d 5f 42 6c 6f 62 20 29 7b 0a 20 20 20  &MEM_Blob ){.   
1c40a 20 69 6e 74 20 69 3b 0a 20 20 20 20 63 68 61 72   int i;.    char
1c40b 20 63 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20   c;.    if( f & 
1c40c 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20  MEM_Dyn ){.     
1c40d 20 63 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20   c = 'z';.      
1c40e 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45  assert( (f & (ME
1c40f 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68  M_Static|MEM_Eph
1c410 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  em))==0 );.    }
1c411 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d  else if( f & MEM
1c412 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20  _Static ){.     
1c413 20 63 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20   c = 't';.      
1c414 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45  assert( (f & (ME
1c415 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29  M_Dyn|MEM_Ephem)
1c416 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73  )==0 );.    }els
1c417 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70  e if( f & MEM_Ep
1c418 68 65 6d 20 29 7b 0a 20 20 20 20 20 20 63 20 3d  hem ){.      c =
1c419 20 27 65 27 3b 0a 20 20 20 20 20 20 61 73 73 65   'e';.      asse
1c41a 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74  rt( (f & (MEM_St
1c41b 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d  atic|MEM_Dyn))==
1c41c 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  0 );.    }else{.
1c41d 20 20 20 20 20 20 63 20 3d 20 27 73 27 3b 0a 20        c = 's';. 
1c41e 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
1c41f 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20  3_snprintf(100, 
1c420 7a 43 73 72 2c 20 22 25 63 22 2c 20 63 29 3b 0a  zCsr, "%c", c);.
1c421 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69      zCsr += sqli
1c422 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72  te3Strlen30(zCsr
1c423 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  );.    sqlite3_s
1c424 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73  nprintf(100, zCs
1c425 72 2c 20 22 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e  r, "%d[", pMem->
1c426 6e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20  n);.    zCsr += 
1c427 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1c428 7a 43 73 72 29 3b 0a 20 20 20 20 66 6f 72 28 69  zCsr);.    for(i
1c429 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c 70 4d  =0; i<16 && i<pM
1c42a 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  em->n; i++){.   
1c42b 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1c42c 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22  ntf(100, zCsr, "
1c42d 25 30 32 58 22 2c 20 28 28 69 6e 74 29 70 4d 65  %02X", ((int)pMe
1c42e 6d 2d 3e 7a 5b 69 5d 20 26 20 30 78 46 46 29 29  m->z[i] & 0xFF))
1c42f 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20  ;.      zCsr += 
1c430 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1c431 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  zCsr);.    }.   
1c432 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 26   for(i=0; i<16 &
1c433 26 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b  & i<pMem->n; i++
1c434 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a 20  ){.      char z 
1c435 3d 20 70 4d 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20  = pMem->z[i];.  
1c436 20 20 20 20 69 66 28 20 7a 3c 33 32 20 7c 7c 20      if( z<32 || 
1c437 7a 3e 31 32 36 20 29 20 2a 7a 43 73 72 2b 2b 20  z>126 ) *zCsr++ 
1c438 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 65 6c 73  = '.';.      els
1c439 65 20 2a 7a 43 73 72 2b 2b 20 3d 20 7a 3b 0a 20  e *zCsr++ = z;. 
1c43a 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65     }..    sqlite
1c43b 33 5f 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20  3_snprintf(100, 
1c43c 7a 43 73 72 2c 20 22 5d 25 73 22 2c 20 65 6e 63  zCsr, "]%s", enc
1c43d 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d  names[pMem->enc]
1c43e 29 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73  );.    zCsr += s
1c43f 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
1c440 43 73 72 29 3b 0a 20 20 20 20 69 66 28 20 66 20  Csr);.    if( f 
1c441 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
1c442 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1c443 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 22  intf(100, zCsr,"
1c444 2b 25 64 7a 22 2c 70 4d 65 6d 2d 3e 75 2e 6e 5a  +%dz",pMem->u.nZ
1c445 65 72 6f 29 3b 0a 20 20 20 20 20 20 7a 43 73 72  ero);.      zCsr
1c446 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65   += sqlite3Strle
1c447 6e 33 30 28 7a 43 73 72 29 3b 0a 20 20 20 20 7d  n30(zCsr);.    }
1c448 0a 20 20 20 20 2a 7a 43 73 72 20 3d 20 27 5c 30  .    *zCsr = '\0
1c449 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66  ';.  }else if( f
1c44a 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20   & MEM_Str ){.  
1c44b 20 20 69 6e 74 20 6a 2c 20 6b 3b 0a 20 20 20 20    int j, k;.    
1c44c 7a 42 75 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20  zBuf[0] = ' ';. 
1c44d 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 44     if( f & MEM_D
1c44e 79 6e 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66  yn ){.      zBuf
1c44f 5b 31 5d 20 3d 20 27 7a 27 3b 0a 20 20 20 20 20  [1] = 'z';.     
1c450 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d   assert( (f & (M
1c451 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 45 70  EM_Static|MEM_Ep
1c452 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  hem))==0 );.    
1c453 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45  }else if( f & ME
1c454 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  M_Static ){.    
1c455 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 74 27 3b    zBuf[1] = 't';
1c456 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
1c457 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d  f & (MEM_Dyn|MEM
1c458 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20  _Ephem))==0 );. 
1c459 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26     }else if( f &
1c45a 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20   MEM_Ephem ){.  
1c45b 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 65      zBuf[1] = 'e
1c45c 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ';.      assert(
1c45d 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69   (f & (MEM_Stati
1c45e 63 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29  c|MEM_Dyn))==0 )
1c45f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1c460 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 73 27     zBuf[1] = 's'
1c461 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6b 20 3d 20  ;.    }.    k = 
1c462 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  2;.    sqlite3_s
1c463 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 26 7a 42  nprintf(100, &zB
1c464 75 66 5b 6b 5d 2c 20 22 25 64 22 2c 20 70 4d 65  uf[k], "%d", pMe
1c465 6d 2d 3e 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20  m->n);.    k += 
1c466 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1c467 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a  &zBuf[k]);.    z
1c468 42 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a  Buf[k++] = '[';.
1c469 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31      for(j=0; j<1
1c46a 35 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20  5 && j<pMem->n; 
1c46b 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 63  j++){.      u8 c
1c46c 20 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20   = pMem->z[j];. 
1c46d 20 20 20 20 20 69 66 28 20 63 3e 3d 30 78 32 30       if( c>=0x20
1c46e 20 26 26 20 63 3c 30 78 37 66 20 29 7b 0a 20 20   && c<0x7f ){.  
1c46f 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20        zBuf[k++] 
1c470 3d 20 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = c;.      }else
1c471 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b  {.        zBuf[k
1c472 2b 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20  ++] = '.';.     
1c473 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75   }.    }.    zBu
1c474 66 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20  f[k++] = ']';.  
1c475 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1c476 74 66 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c  tf(100,&zBuf[k],
1c477 20 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e   encnames[pMem->
1c478 65 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d 20  enc]);.    k += 
1c479 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1c47a 26 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a  &zBuf[k]);.    z
1c47b 42 75 66 5b 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20  Buf[k++] = 0;.  
1c47c 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  }.}.#endif..#ifd
1c47d 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1c47e 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 74 68 65 20  /*.** Print the 
1c47f 76 61 6c 75 65 20 6f 66 20 61 20 72 65 67 69 73  value of a regis
1c480 74 65 72 20 66 6f 72 20 74 72 61 63 69 6e 67 20  ter for tracing 
1c481 70 75 72 70 6f 73 65 73 3a 0a 2a 2f 0a 73 74 61  purposes:.*/.sta
1c482 74 69 63 20 76 6f 69 64 20 6d 65 6d 54 72 61 63  tic void memTrac
1c483 65 50 72 69 6e 74 28 46 49 4c 45 20 2a 6f 75 74  ePrint(FILE *out
1c484 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 69 66 28  , Mem *p){.  if(
1c485 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   p->flags & MEM_
1c486 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 66 70 72 69  Null ){.    fpri
1c487 6e 74 66 28 6f 75 74 2c 20 22 20 4e 55 4c 4c 22  ntf(out, " NULL"
1c488 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28  );.  }else if( (
1c489 70 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  p->flags & (MEM_
1c48a 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 29 3d 3d 28  Int|MEM_Str))==(
1c48b 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74 72 29  MEM_Int|MEM_Str)
1c48c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28   ){.    fprintf(
1c48d 6f 75 74 2c 20 22 20 73 69 3a 25 6c 6c 64 22 2c  out, " si:%lld",
1c48e 20 70 2d 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73   p->u.i);.  }els
1c48f 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26  e if( p->flags &
1c490 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20   MEM_Int ){.    
1c491 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 69  fprintf(out, " i
1c492 3a 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b  :%lld", p->u.i);
1c493 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1c494 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
1c495 49 4e 54 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  INT.  }else if( 
1c496 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  p->flags & MEM_R
1c497 65 61 6c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  eal ){.    fprin
1c498 74 66 28 6f 75 74 2c 20 22 20 72 3a 25 67 22 2c  tf(out, " r:%g",
1c499 20 70 2d 3e 72 29 3b 0a 23 65 6e 64 69 66 0a 20   p->r);.#endif. 
1c49a 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c   }else if( p->fl
1c49b 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74  ags & MEM_RowSet
1c49c 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28   ){.    fprintf(
1c49d 6f 75 74 2c 20 22 20 28 72 6f 77 73 65 74 29 22  out, " (rowset)"
1c49e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1c49f 63 68 61 72 20 7a 42 75 66 5b 32 30 30 5d 3b 0a  char zBuf[200];.
1c4a0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1c4a1 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 2c  emPrettyPrint(p,
1c4a2 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69   zBuf);.    fpri
1c4a3 6e 74 66 28 6f 75 74 2c 20 22 20 22 29 3b 0a 20  ntf(out, " ");. 
1c4a4 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
1c4a5 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20 20 7d  "%s", zBuf);.  }
1c4a6 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  .}.static void r
1c4a7 65 67 69 73 74 65 72 54 72 61 63 65 28 46 49 4c  egisterTrace(FIL
1c4a8 45 20 2a 6f 75 74 2c 20 69 6e 74 20 69 52 65 67  E *out, int iReg
1c4a9 2c 20 4d 65 6d 20 2a 70 29 7b 0a 20 20 66 70 72  , Mem *p){.  fpr
1c4aa 69 6e 74 66 28 6f 75 74 2c 20 22 52 45 47 5b 25  intf(out, "REG[%
1c4ab 64 5d 20 3d 20 22 2c 20 69 52 65 67 29 3b 0a 20  d] = ", iReg);. 
1c4ac 20 6d 65 6d 54 72 61 63 65 50 72 69 6e 74 28 6f   memTracePrint(o
1c4ad 75 74 2c 20 70 29 3b 0a 20 20 66 70 72 69 6e 74  ut, p);.  fprint
1c4ae 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b 0a 7d 0a  f(out, "\n");.}.
1c4af 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
1c4b0 51 4c 49 54 45 5f 44 45 42 55 47 0a 23 20 20 64  QLITE_DEBUG.#  d
1c4b1 65 66 69 6e 65 20 52 45 47 49 53 54 45 52 5f 54  efine REGISTER_T
1c4b2 52 41 43 45 28 52 2c 4d 29 20 69 66 28 70 2d 3e  RACE(R,M) if(p->
1c4b3 74 72 61 63 65 29 72 65 67 69 73 74 65 72 54 72  trace)registerTr
1c4b4 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 52 2c 4d  ace(p->trace,R,M
1c4b5 29 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  ).#else.#  defin
1c4b6 65 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45  e REGISTER_TRACE
1c4b7 28 52 2c 4d 29 0a 23 65 6e 64 69 66 0a 0a 0a 23  (R,M).#endif...#
1c4b8 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
1c4b9 4c 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d  LE../* .** hwtim
1c4ba 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c  e.h contains inl
1c4bb 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f  ine assembler co
1c4bc 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74  de for implement
1c4bd 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72  ing .** high-per
1c4be 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20  formance timing 
1c4bf 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a  routines..*/./**
1c4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
1c4c1 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e  lude hwtime.h in
1c4c2 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76   the middle of v
1c4c3 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dbe.c **********
1c4c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
1c4c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
1c4c6 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68  in file hwtime.h
1c4c7 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1c4c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
1c4ca 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a  ** 2008 May 27.*
1c4cb 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
1c4cc 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
1c4cd 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
1c4ce 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
1c4cf 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
1c4d0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
1c4d1 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
1c4d2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
1c4d3 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
1c4d4 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
1c4d5 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
1c4d6 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
1c4d7 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
1c4d8 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
1c4d9 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
1c4da 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
1c4db 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
1c4dc 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
1c4dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c4e1 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
1c4e2 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69  le contains inli
1c4e3 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20  ne asm code for 
1c4e4 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68  retrieving "high
1c4e5 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a  -performance".**
1c4e6 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38   counters for x8
1c4e7 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2f  6 class CPUs..*/
1c4e8 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45  .#ifndef _HWTIME
1c4e9 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54  _H_.#define _HWT
1c4ea 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68  IME_H_../*.** Th
1c4eb 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
1c4ec 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f  ine only works o
1c4ed 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20  n pentium-class 
1c4ee 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65  (or newer) proce
1c4ef 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65  ssors..** It use
1c4f0 73 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f  s the RDTSC opco
1c4f1 64 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63  de to read the c
1c4f2 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65  ycle count value
1c4f3 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70   out of the.** p
1c4f4 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74  rocessor and ret
1c4f5 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e  urns that value.
1c4f6 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73    This can be us
1c4f7 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a  ed for high-res.
1c4f8 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f  ** profiling..*/
1c4f9 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f  .#if (defined(__
1c4fa 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e  GNUC__) || defin
1c4fb 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26  ed(_MSC_VER)) &&
1c4fc 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65   \.      (define
1c4fd 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e  d(i386) || defin
1c4fe 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20  ed(__i386__) || 
1c4ff 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29  defined(_M_IX86)
1c500 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64  )..  #if defined
1c501 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f  (__GNUC__)..  __
1c502 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f  inline__ sqlite_
1c503 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
1c504 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
1c505 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f   unsigned int lo
1c506 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d  , hi;.     __asm
1c507 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20  __ __volatile__ 
1c508 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20  ("rdtsc" : "=a" 
1c509 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29  (lo), "=d" (hi))
1c50a 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73  ;.     return (s
1c50b 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20  qlite_uint64)hi 
1c50c 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a  << 32 | lo;.  }.
1c50d 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64  .  #elif defined
1c50e 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f  (_MSC_VER)..  __
1c50f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20  declspec(naked) 
1c510 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f  __inline sqlite_
1c511 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73  uint64 __cdecl s
1c512 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69  qlite3Hwtime(voi
1c513 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b  d){.     __asm {
1c514 0a 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20  .        rdtsc. 
1c515 20 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20         ret      
1c516 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20   ; return value 
1c517 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20  at EDX:EAX.     
1c518 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a  }.  }..  #endif.
1c519 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28  .#elif (defined(
1c51a 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66  __GNUC__) && def
1c51b 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29  ined(__x86_64__)
1c51c 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20  )..  __inline__ 
1c51d 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
1c51e 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
1c51f 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  ){.      unsigne
1c520 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20  d long val;.    
1c521 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61    __asm__ __vola
1c522 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20  tile__ ("rdtsc" 
1c523 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20  : "=A" (val));. 
1c524 20 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b       return val;
1c525 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65  .  }. .#elif (de
1c526 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
1c527 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63  && defined(__ppc
1c528 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  __))..  __inline
1c529 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
1c52a 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
1c52b 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  oid){.      unsi
1c52c 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72  gned long long r
1c52d 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73  etval;.      uns
1c52e 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b  igned long junk;
1c52f 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f  .      __asm__ _
1c530 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e  _volatile__ ("\n
1c531 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20  \.          1:  
1c532 20 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e      mftbu   %1\n
1c533 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
1c534 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c      mftb    %L0\
1c535 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
1c536 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c       mftbu   %0\
1c537 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
1c538 20 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c       cmpw    %0,
1c539 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %1\n\.          
1c53a 20 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20          bne     
1c53b 31 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  1b".            
1c53c 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65        : "=r" (re
1c53d 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e  tval), "=r" (jun
1c53e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  k));.      retur
1c53f 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23  n retval;.  }..#
1c540 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e  else..  #error N
1c541 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  eed implementati
1c542 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74  on of sqlite3Hwt
1c543 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70  ime() for your p
1c544 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20  latform...  /*. 
1c545 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77   ** To compile w
1c546 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74  ithout implement
1c547 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d  ing sqlite3Hwtim
1c548 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61  e() for your pla
1c549 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20  tform,.  ** you 
1c54a 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61  can remove the a
1c54b 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20  bove #error and 
1c54c 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  use the followin
1c54d 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63  g.  ** stub func
1c54e 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20  tion.  You will 
1c54f 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70  lose timing supp
1c550 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a  ort for many.  *
1c551 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69  * of the debuggi
1c552 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75  ng and testing u
1c553 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74  tilities, but it
1c554 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20   should at.  ** 
1c555 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e  least compile an
1c556 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49  d run..  */.SQLI
1c557 54 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c  TE_PRIVATE   sql
1c558 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
1c559 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20  e3Hwtime(void){ 
1c55a 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f  return ((sqlite_
1c55b 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65  uint64)0); }..#e
1c55c 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  ndif..#endif /* 
1c55d 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45  !defined(_HWTIME
1c55e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _H_) */../******
1c55f 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1c560 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  hwtime.h *******
1c561 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c562 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c563 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1c564 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
1c565 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
1c566 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e 63 20  t off in vdbe.c 
1c567 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c568 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64 69 66  *******/..#endif
1c569 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48 45 43  ../*.** The CHEC
1c56a 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 20  K_FOR_INTERRUPT 
1c56b 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 68 65  macro defined he
1c56c 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 65 65 20  re looks to see 
1c56d 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65  if the.** sqlite
1c56e 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 6f  3_interrupt() ro
1c56f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20 63  utine has been c
1c570 61 6c 6c 65 64 2e 20 20 49 66 20 69 74 20 68 61  alled.  If it ha
1c571 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a 2a 2a 20  s been, then.** 
1c572 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 74 68  processing of th
1c573 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 69  e VDBE program i
1c574 73 20 69 6e 74 65 72 72 75 70 74 65 64 2e 0a 2a  s interrupted..*
1c575 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20  *.** This macro 
1c576 61 64 64 65 64 20 74 6f 20 65 76 65 72 79 20 69  added to every i
1c577 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20  nstruction that 
1c578 64 6f 65 73 20 61 20 6a 75 6d 70 20 69 6e 20 6f  does a jump in o
1c579 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65  rder to.** imple
1c57a 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 20 54 68  ment a loop.  Th
1c57b 69 73 20 74 65 73 74 20 75 73 65 64 20 74 6f 20  is test used to 
1c57c 62 65 20 6f 6e 20 65 76 65 72 79 20 73 69 6e 67  be on every sing
1c57d 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 0a  le instruction,.
1c57e 2a 2a 20 62 75 74 20 74 68 61 74 20 6d 65 61 6e  ** but that mean
1c57f 74 20 77 65 20 6d 6f 72 65 20 74 65 73 74 69 6e  t we more testin
1c580 67 20 74 68 61 74 20 77 65 20 6e 65 65 64 65 64  g that we needed
1c581 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65 73 74 69  .  By only testi
1c582 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61 67 20 6f  ng the.** flag o
1c583 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69  n jump instructi
1c584 6f 6e 73 2c 20 77 65 20 67 65 74 20 61 20 28 73  ons, we get a (s
1c585 6d 61 6c 6c 29 20 73 70 65 65 64 20 69 6d 70 72  mall) speed impr
1c586 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66  ovement..*/.#def
1c587 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e  ine CHECK_FOR_IN
1c588 54 45 52 52 55 50 54 20 5c 0a 20 20 20 69 66 28  TERRUPT \.   if(
1c589 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
1c58a 75 70 74 65 64 20 29 20 67 6f 74 6f 20 61 62 6f  upted ) goto abo
1c58b 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72 72  rt_due_to_interr
1c58c 75 70 74 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  upt;..#ifdef SQL
1c58d 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63  ITE_DEBUG.static
1c58e 20 69 6e 74 20 66 69 6c 65 45 78 69 73 74 73 28   int fileExists(
1c58f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
1c590 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 29 7b  st char *zFile){
1c591 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a  .  int res = 0;.
1c592 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1c593 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 53 51 4c  E_OK;.#ifdef SQL
1c594 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20 49 66  ITE_TEST.  /* If
1c595 20 77 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c   we are currentl
1c596 79 20 74 65 73 74 69 6e 67 20 49 4f 20 65 72 72  y testing IO err
1c597 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74  ors, then do not
1c598 20 63 61 6c 6c 20 4f 73 41 63 63 65 73 73 28 29   call OsAccess()
1c599 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74 20 66 6f   to.  ** test fo
1c59a 72 20 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f  r the presence o
1c59b 66 20 7a 46 69 6c 65 2e 20 54 68 69 73 20 69 73  f zFile. This is
1c59c 20 62 65 63 61 75 73 65 20 61 6e 79 20 49 4f 20   because any IO 
1c59d 65 72 72 6f 72 20 74 68 61 74 0a 20 20 2a 2a 20  error that.  ** 
1c59e 6f 63 63 75 72 73 20 68 65 72 65 20 77 69 6c 6c  occurs here will
1c59f 20 6e 6f 74 20 62 65 20 72 65 70 6f 72 74 65 64   not be reported
1c5a0 2c 20 63 61 75 73 69 6e 67 20 74 68 65 20 74 65  , causing the te
1c5a1 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20 20 2a 2f  st to fail..  */
1c5a2 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71  .  extern int sq
1c5a3 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
1c5a4 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28 20 73 71  ending;.  if( sq
1c5a5 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
1c5a6 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 65 6e 64  ending<=0 ).#end
1c5a7 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  if.    rc = sqli
1c5a8 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e  te3OsAccess(db->
1c5a9 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 53 51 4c  pVfs, zFile, SQL
1c5aa 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
1c5ab 53 2c 20 26 72 65 73 29 3b 0a 20 20 72 65 74 75  S, &res);.  retu
1c5ac 72 6e 20 28 72 65 73 20 26 26 20 72 63 3d 3d 53  rn (res && rc==S
1c5ad 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23 65 6e  QLITE_OK);.}.#en
1c5ae 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  dif..#ifndef NDE
1c5af 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  BUG./*.** This f
1c5b0 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20  unction is only 
1c5b1 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68  called from with
1c5b2 69 6e 20 61 6e 20 61 73 73 65 72 74 28 29 20 65  in an assert() e
1c5b3 78 70 72 65 73 73 69 6f 6e 2e 20 49 74 0a 2a 2a  xpression. It.**
1c5b4 20 63 68 65 63 6b 73 20 74 68 61 74 20 74 68 65   checks that the
1c5b5 20 73 71 6c 69 74 65 33 2e 6e 54 72 61 6e 73 61   sqlite3.nTransa
1c5b6 63 74 69 6f 6e 20 76 61 72 69 61 62 6c 65 20 69  ction variable i
1c5b7 73 20 63 6f 72 72 65 63 74 6c 79 20 73 65 74 20  s correctly set 
1c5b8 74 6f 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72  to.** the number
1c5b9 20 6f 66 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74   of non-transact
1c5ba 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 73 20 63  ion savepoints c
1c5bb 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20  urrently in the 
1c5bc 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  .** linked list 
1c5bd 73 74 61 72 74 69 6e 67 20 61 74 20 73 71 6c 69  starting at sqli
1c5be 74 65 33 2e 70 53 61 76 65 70 6f 69 6e 74 2e 0a  te3.pSavepoint..
1c5bf 2a 2a 20 0a 2a 2a 20 55 73 61 67 65 3a 0a 2a 2a  ** .** Usage:.**
1c5c0 0a 2a 2a 20 20 20 20 20 61 73 73 65 72 74 28 20  .**     assert( 
1c5c1 63 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f  checkSavepointCo
1c5c2 75 6e 74 28 64 62 29 20 29 3b 0a 2a 2f 0a 73 74  unt(db) );.*/.st
1c5c3 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 53 61  atic int checkSa
1c5c4 76 65 70 6f 69 6e 74 43 6f 75 6e 74 28 73 71 6c  vepointCount(sql
1c5c5 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74  ite3 *db){.  int
1c5c6 20 6e 20 3d 20 30 3b 0a 20 20 53 61 76 65 70 6f   n = 0;.  Savepo
1c5c7 69 6e 74 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d  int *p;.  for(p=
1c5c8 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 20  db->pSavepoint; 
1c5c9 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 20 6e  p; p=p->pNext) n
1c5ca 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3d  ++;.  assert( n=
1c5cb 3d 28 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  =(db->nSavepoint
1c5cc 20 2b 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63   + db->isTransac
1c5cd 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 29 20 29  tionSavepoint) )
1c5ce 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  ;.  return 1;.}.
1c5cf 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78  #endif../*.** Ex
1c5d0 65 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f 66  ecute as much of
1c5d1 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20   a VDBE program 
1c5d2 61 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20 72  as we can then r
1c5d3 65 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c  eturn..**.** sql
1c5d4 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64  ite3VdbeMakeRead
1c5d5 79 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  y() must be call
1c5d6 65 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 72  ed before this r
1c5d7 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20  outine in order 
1c5d8 74 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20  to.** close the 
1c5d9 70 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20 66  program with a f
1c5da 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64  inal OP_Halt and
1c5db 20 74 6f 20 73 65 74 20 75 70 20 74 68 65 20 63   to set up the c
1c5dc 61 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20  allbacks.** and 
1c5dd 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
1c5de 65 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  e pointer..**.**
1c5df 20 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20   Whenever a row 
1c5e0 6f 72 20 72 65 73 75 6c 74 20 64 61 74 61 20 69  or result data i
1c5e1 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69  s available, thi
1c5e2 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65  s routine will e
1c5e3 69 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20  ither.** invoke 
1c5e4 74 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c 62  the result callb
1c5e5 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73  ack (if there is
1c5e6 20 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e 20   one) or return 
1c5e7 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52  with.** SQLITE_R
1c5e8 4f 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  OW..**.** If an 
1c5e9 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
1c5ea 74 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64  to open a locked
1c5eb 20 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20   database, then 
1c5ec 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
1c5ed 77 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 6f  will either invo
1c5ee 6b 65 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  ke the busy call
1c5ef 62 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69  back (if there i
1c5f0 73 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 6c  s one) or it wil
1c5f1 6c 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49  l.** return SQLI
1c5f2 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49  TE_BUSY..**.** I
1c5f3 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
1c5f4 73 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  s, an error mess
1c5f5 61 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74  age is written t
1c5f6 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
1c5f7 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65  d.** from sqlite
1c5f8 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70  3_malloc() and p
1c5f9 2d 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 64  ->zErrMsg is mad
1c5fa 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  e to point to th
1c5fb 61 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68  at memory..** Th
1c5fc 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
1c5fd 73 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 20  stored in p->rc 
1c5fe 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65  and this routine
1c5ff 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
1c600 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ERROR..**.** If 
1c601 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 65  the callback eve
1c602 72 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65  r returns non-ze
1c603 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f  ro, then the pro
1c604 67 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d  gram exits.** im
1c605 6d 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65 72  mediately.  Ther
1c606 65 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72  e will be no err
1c607 6f 72 20 6d 65 73 73 61 67 65 20 62 75 74 20 74  or message but t
1c608 68 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 69  he p->rc field i
1c609 73 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c 49  s.** set to SQLI
1c60a 54 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 69  TE_ABORT and thi
1c60b 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72  s routine will r
1c60c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
1c60d 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f  OR..**.** A memo
1c60e 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
1c60f 72 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72 63  ror causes p->rc
1c610 20 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53 51   to be set to SQ
1c611 4c 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74  LITE_NOMEM and t
1c612 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74  his.** routine t
1c613 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  o return SQLITE_
1c614 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68  ERROR..**.** Oth
1c615 65 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73 20  er fatal errors 
1c616 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1c617 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72  ROR..**.** After
1c618 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61   this routine ha
1c619 73 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c 69  s finished, sqli
1c61a 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28  te3VdbeFinalize(
1c61b 29 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75  ) should be.** u
1c61c 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20  sed to clean up 
1c61d 74 68 65 20 6d 65 73 73 20 74 68 61 74 20 77 61  the mess that wa
1c61e 73 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a  s left behind..*
1c61f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1c620 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
1c621 45 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70 20  Exec(.  Vdbe *p 
1c622 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c623 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a     /* The VDBE *
1c624 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20  /.){.  int pc;  
1c625 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c626 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d    /* The program
1c627 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70   counter */.  Op
1c628 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 20   *aOp = p->aOp; 
1c629 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
1c62a 20 6f 66 20 70 2d 3e 61 4f 70 20 2a 2f 0a 20 20   of p->aOp */.  
1c62b 4f 70 20 2a 70 4f 70 3b 20 20 20 20 20 20 20 20  Op *pOp;        
1c62c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
1c62d 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20  rrent operation 
1c62e 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
1c62f 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
1c630 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75  /* Value to retu
1c631 72 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  rn */.  sqlite3 
1c632 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20  *db = p->db;    
1c633 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
1c634 73 65 20 2a 2f 0a 20 20 75 38 20 72 65 73 65 74  se */.  u8 reset
1c635 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 20 3d 20  SchemaOnFault = 
1c636 30 3b 20 2f 2a 20 52 65 73 65 74 20 73 63 68 65  0; /* Reset sche
1c637 6d 61 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f  ma after an erro
1c638 72 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 75  r if true */.  u
1c639 38 20 65 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43  8 encoding = ENC
1c63a 28 64 62 29 3b 20 20 20 20 20 2f 2a 20 54 68 65  (db);     /* The
1c63b 20 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69   database encodi
1c63c 6e 67 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ng */.#ifndef SQ
1c63d 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45  LITE_OMIT_PROGRE
1c63e 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e  SS_CALLBACK.  in
1c63f 74 20 63 68 65 63 6b 50 72 6f 67 72 65 73 73 3b  t checkProgress;
1c640 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
1c641 20 69 66 20 70 72 6f 67 72 65 73 73 20 63 61 6c   if progress cal
1c642 6c 62 61 63 6b 73 20 61 72 65 20 65 6e 61 62 6c  lbacks are enabl
1c643 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 6f  ed */.  int nPro
1c644 67 72 65 73 73 4f 70 73 20 3d 20 30 3b 20 20 20  gressOps = 0;   
1c645 20 20 20 2f 2a 20 4f 70 63 6f 64 65 73 20 65 78     /* Opcodes ex
1c646 65 63 75 74 65 64 20 73 69 6e 63 65 20 70 72 6f  ecuted since pro
1c647 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 2e 20  gress callback. 
1c648 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 4d 65 6d 20  */.#endif.  Mem 
1c649 2a 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65 6d 3b  *aMem = p->aMem;
1c64a 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
1c64b 66 20 70 2d 3e 61 4d 65 6d 20 2a 2f 0a 20 20 4d  f p->aMem */.  M
1c64c 65 6d 20 2a 70 49 6e 31 20 3d 20 30 3b 20 20 20  em *pIn1 = 0;   
1c64d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73 74            /* 1st
1c64e 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a   input operand *
1c64f 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 32 20 3d 20  /.  Mem *pIn2 = 
1c650 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
1c651 2a 20 32 6e 64 20 69 6e 70 75 74 20 6f 70 65 72  * 2nd input oper
1c652 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49  and */.  Mem *pI
1c653 6e 33 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  n3 = 0;         
1c654 20 20 20 20 2f 2a 20 33 72 64 20 69 6e 70 75 74      /* 3rd input
1c655 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d 65   operand */.  Me
1c656 6d 20 2a 70 4f 75 74 20 3d 20 30 3b 20 20 20 20  m *pOut = 0;    
1c657 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
1c658 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ut operand */.  
1c659 69 6e 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 30  int iCompare = 0
1c65a 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  ;          /* Re
1c65b 73 75 6c 74 20 6f 66 20 6c 61 73 74 20 4f 50 5f  sult of last OP_
1c65c 43 6f 6d 70 61 72 65 20 6f 70 65 72 61 74 69 6f  Compare operatio
1c65d 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72  n */.  int *aPer
1c65e 6d 75 74 65 20 3d 20 30 3b 20 20 20 20 20 20 20  mute = 0;       
1c65f 20 20 2f 2a 20 50 65 72 6d 75 74 61 74 69 6f 6e    /* Permutation
1c660 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20   of columns for 
1c661 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a 23 69  OP_Compare */.#i
1c662 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c  fdef VDBE_PROFIL
1c663 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b 20 20  E.  u64 start;  
1c664 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c665 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f 75 6e  * CPU clock coun
1c666 74 20 61 74 20 73 74 61 72 74 20 6f 66 20 6f 70  t at start of op
1c667 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  code */.  int or
1c668 69 67 50 63 3b 20 20 20 20 20 20 20 20 20 20 20  igPc;           
1c669 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61 6d 20       /* Program 
1c66a 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61 72 74  counter at start
1c66b 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 23 65   of opcode */.#e
1c66c 6e 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a  ndif.  /********
1c66d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c66e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c66f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a  ************.  *
1c671 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * Automatically 
1c672 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 0a 20  generated code. 
1c673 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c   **.  ** The fol
1c674 6c 6f 77 69 6e 67 20 75 6e 69 6f 6e 20 69 73 20  lowing union is 
1c675 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  automatically ge
1c676 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 0a 20  nerated by the. 
1c677 20 2a 2a 20 76 64 62 65 2d 63 6f 6d 70 72 65 73   ** vdbe-compres
1c678 73 2e 74 63 6c 20 73 63 72 69 70 74 2e 20 20 54  s.tcl script.  T
1c679 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68  he purpose of th
1c67a 69 73 20 75 6e 69 6f 6e 20 69 73 20 74 6f 0a 20  is union is to. 
1c67b 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20 61   ** reduce the a
1c67c 6d 6f 75 6e 74 20 6f 66 20 73 74 61 63 6b 20 73  mount of stack s
1c67d 70 61 63 65 20 72 65 71 75 69 72 65 64 20 62 79  pace required by
1c67e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
1c67f 20 20 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74    ** See comment
1c680 73 20 69 6e 20 74 68 65 20 76 64 62 65 2d 63 6f  s in the vdbe-co
1c681 6d 70 72 65 73 73 2e 74 63 6c 20 73 63 72 69 70  mpress.tcl scrip
1c682 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 20  t for details.. 
1c683 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76 64 62 65   */.  union vdbe
1c684 45 78 65 63 55 6e 69 6f 6e 20 7b 0a 20 20 20 20  ExecUnion {.    
1c685 73 74 72 75 63 74 20 4f 50 5f 59 69 65 6c 64 5f  struct OP_Yield_
1c686 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c687 20 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a 20     int pcDest;. 
1c688 20 20 20 7d 20 61 61 3b 0a 20 20 20 20 73 74 72     } aa;.    str
1c689 75 63 74 20 4f 50 5f 56 61 72 69 61 62 6c 65 5f  uct OP_Variable_
1c68a 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c68b 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20     int p1;      
1c68c 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20      /* Variable 
1c68d 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a  to copy from */.
1c68e 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20        int p2;   
1c68f 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
1c690 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f  er to copy to */
1c691 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 20  .      int n;   
1c692 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1c693 72 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66 74  r of values left
1c694 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20   to copy */.    
1c695 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20 20    Mem *pVar;    
1c696 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e     /* Value bein
1c697 67 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a 2f  g transferred */
1c698 0a 20 20 20 20 7d 20 61 62 3b 0a 20 20 20 20 73  .    } ab;.    s
1c699 74 72 75 63 74 20 4f 50 5f 4d 6f 76 65 5f 73 74  truct OP_Move_st
1c69a 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c69b 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20   char *zMalloc; 
1c69c 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 72    /* Holding var
1c69d 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61  iable for alloca
1c69e 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20  ted memory */.  
1c69f 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20      int n;      
1c6a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c6a1 66 20 72 65 67 69 73 74 65 72 73 20 6c 65 66 74  f registers left
1c6a2 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20 20   to copy */.    
1c6a3 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20    int p1;       
1c6a4 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74     /* Register t
1c6a5 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a 20  o copy from */. 
1c6a6 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20 20       int p2;    
1c6a7 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
1c6a8 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f 0a  r to copy to */.
1c6a9 20 20 20 20 7d 20 61 63 3b 0a 20 20 20 20 73 74      } ac;.    st
1c6aa 72 75 63 74 20 4f 50 5f 52 65 73 75 6c 74 52 6f  ruct OP_ResultRo
1c6ab 77 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  w_stack_vars {. 
1c6ac 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a       Mem *pMem;.
1c6ad 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
1c6ae 20 7d 20 61 64 3b 0a 20 20 20 20 73 74 72 75 63   } ad;.    struc
1c6af 74 20 4f 50 5f 43 6f 6e 63 61 74 5f 73 74 61 63  t OP_Concat_stac
1c6b0 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c6b1 36 34 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 20  64 nByte;.    } 
1c6b2 61 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  ae;.    struct O
1c6b3 50 5f 52 65 6d 61 69 6e 64 65 72 5f 73 74 61 63  P_Remainder_stac
1c6b4 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c6b5 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 2f  nt flags;      /
1c6b6 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d 5f 2a  * Combined MEM_*
1c6b7 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f 74 68   flags from both
1c6b8 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 20 20 20   inputs */.     
1c6b9 20 69 36 34 20 69 41 3b 20 20 20 20 20 20 20 20   i64 iA;        
1c6ba 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75   /* Integer valu
1c6bb 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e  e of left operan
1c6bc 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 69  d */.      i64 i
1c6bd 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  B;         /* In
1c6be 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 72  teger value of r
1c6bf 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  ight operand */.
1c6c0 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 41 3b        double rA;
1c6c1 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61        /* Real va
1c6c2 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72  lue of left oper
1c6c3 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 64 6f 75  and */.      dou
1c6c4 62 6c 65 20 72 42 3b 20 20 20 20 20 20 2f 2a 20  ble rB;      /* 
1c6c5 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 72 69  Real value of ri
1c6c6 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  ght operand */. 
1c6c7 20 20 20 7d 20 61 66 3b 0a 20 20 20 20 73 74 72     } af;.    str
1c6c8 75 63 74 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 5f  uct OP_Function_
1c6c9 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c6ca 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
1c6cb 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 20 20 20  Mem *pArg;.     
1c6cc 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1c6cd 20 63 74 78 3b 0a 20 20 20 20 20 20 73 71 6c 69   ctx;.      sqli
1c6ce 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61  te3_value **apVa
1c6cf 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a  l;.      int n;.
1c6d0 20 20 20 20 7d 20 61 67 3b 0a 20 20 20 20 73 74      } ag;.    st
1c6d1 72 75 63 74 20 4f 50 5f 53 68 69 66 74 52 69 67  ruct OP_ShiftRig
1c6d2 68 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ht_stack_vars {.
1c6d3 20 20 20 20 20 20 69 36 34 20 61 3b 0a 20 20 20        i64 a;.   
1c6d4 20 20 20 69 36 34 20 62 3b 0a 20 20 20 20 7d 20     i64 b;.    } 
1c6d5 61 68 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  ah;.    struct O
1c6d6 50 5f 47 65 5f 73 74 61 63 6b 5f 76 61 72 73 20  P_Ge_stack_vars 
1c6d7 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b  {.      int res;
1c6d8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1c6d9 65 73 75 6c 74 20 6f 66 20 74 68 65 20 63 6f 6d  esult of the com
1c6da 70 61 72 69 73 6f 6e 20 6f 66 20 70 49 6e 31 20  parison of pIn1 
1c6db 61 67 61 69 6e 73 74 20 70 49 6e 33 20 2a 2f 0a  against pIn3 */.
1c6dc 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69 6e        char affin
1c6dd 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66 66  ity;      /* Aff
1c6de 69 6e 69 74 79 20 74 6f 20 75 73 65 20 66 6f 72  inity to use for
1c6df 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a 20   comparison */. 
1c6e0 20 20 20 7d 20 61 69 3b 0a 20 20 20 20 73 74 72     } ai;.    str
1c6e1 75 63 74 20 4f 50 5f 43 6f 6d 70 61 72 65 5f 73  uct OP_Compare_s
1c6e2 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c6e3 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20 69    int n;.      i
1c6e4 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20  nt i;.      int 
1c6e5 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 32  p1;.      int p2
1c6e6 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 4b 65  ;.      const Ke
1c6e7 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
1c6e8 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 3b 0a  .      int idx;.
1c6e9 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
1c6ea 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c  Coll;    /* Coll
1c6eb 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74  ating sequence t
1c6ec 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 20 74 65  o use on this te
1c6ed 72 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  rm */.      int 
1c6ee 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f  bRev;          /
1c6ef 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45  * True for DESCE
1c6f0 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72  NDING sort order
1c6f1 20 2a 2f 0a 20 20 20 20 7d 20 61 6a 3b 0a 20 20   */.    } aj;.  
1c6f2 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 72 5f 73    struct OP_Or_s
1c6f3 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c6f4 20 20 69 6e 74 20 76 31 3b 20 20 20 20 2f 2a 20    int v1;    /* 
1c6f5 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a 20 20 30  Left operand:  0
1c6f6 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45  ==FALSE, 1==TRUE
1c6f7 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20  , 2==UNKNOWN or 
1c6f8 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 69 6e  NULL */.      in
1c6f9 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 68  t v2;    /* Righ
1c6fa 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 41  t operand: 0==FA
1c6fb 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d  LSE, 1==TRUE, 2=
1c6fc 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c  =UNKNOWN or NULL
1c6fd 20 2a 2f 0a 20 20 20 20 7d 20 61 6b 3b 0a 20 20   */.    } ak;.  
1c6fe 20 20 73 74 72 75 63 74 20 4f 50 5f 49 66 4e 6f    struct OP_IfNo
1c6ff 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c700 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20       int c;.    
1c701 7d 20 61 6c 3b 0a 20 20 20 20 73 74 72 75 63 74  } al;.    struct
1c702 20 4f 50 5f 43 6f 6c 75 6d 6e 5f 73 74 61 63 6b   OP_Column_stack
1c703 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 75 33  _vars {.      u3
1c704 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20 20  2 payloadSize;  
1c705 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1c706 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  tes in the recor
1c707 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 70  d */.      i64 p
1c708 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a  ayloadSize64; /*
1c709 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1c70a 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a   in the record *
1c70b 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b 20  /.      int p1; 
1c70c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 31             /* P1
1c70d 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70   value of the op
1c70e 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e  code */.      in
1c70f 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20  t p2;           
1c710 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65   /* column numbe
1c711 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a 2f  r to retrieve */
1c712 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c713 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20 54 68 65  r *pC;    /* The
1c714 20 56 44 42 45 20 63 75 72 73 6f 72 20 2a 2f 0a   VDBE cursor */.
1c715 20 20 20 20 20 20 63 68 61 72 20 2a 7a 52 65 63        char *zRec
1c716 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
1c717 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20  ter to complete 
1c718 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20  record-data */. 
1c719 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70       BtCursor *p
1c71a 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20 42  Crsr;   /* The B
1c71b 54 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20  Tree cursor */. 
1c71c 20 20 20 20 20 75 33 32 20 2a 61 54 79 70 65 3b       u32 *aType;
1c71d 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70 65          /* aType
1c71e 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e 75  [i] holds the nu
1c71f 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20 74 68  meric type of th
1c720 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f  e i-th column */
1c721 0a 20 20 20 20 20 20 75 33 32 20 2a 61 4f 66 66  .      u32 *aOff
1c722 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f 66  set;      /* aOf
1c723 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73 65  fset[i] is offse
1c724 74 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64 61  t to start of da
1c725 74 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c 75  ta for i-th colu
1c726 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  mn */.      int 
1c727 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f  nField;        /
1c728 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c  * number of fiel
1c729 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ds in the record
1c72a 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c 65   */.      int le
1c72b 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  n;           /* 
1c72c 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  The length of th
1c72d 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61 74  e serialized dat
1c72e 61 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e  a for the column
1c72f 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   */.      int i;
1c730 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c731 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
1c732 20 20 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74        char *zDat
1c733 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72 74  a;       /* Part
1c734 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 62   of the record b
1c735 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f 0a  eing decoded */.
1c736 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 74        Mem *pDest
1c737 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72  ;        /* Wher
1c738 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65  e to write the e
1c739 78 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a  xtracted value *
1c73a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 73 4d 65 6d  /.      Mem sMem
1c73b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  ;          /* Fo
1c73c 72 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72 65  r storing the re
1c73d 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64  cord being decod
1c73e 65 64 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a  ed */.      u8 *
1c73f 7a 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f  zIdx;          /
1c740 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65 61  * Index into hea
1c741 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 38 20  der */.      u8 
1c742 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 20 20  *zEndHdr;       
1c743 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69  /* Pointer to fi
1c744 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74  rst byte after t
1c745 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20  he header */.   
1c746 20 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20     u32 offset;  
1c747 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1c748 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f  into the data */
1c749 0a 20 20 20 20 20 20 75 36 34 20 6f 66 66 73 65  .      u64 offse
1c74a 74 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34 2d  t64;      /* 64-
1c74b 62 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34 20  bit offset.  64 
1c74c 62 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20 63  bits needed to c
1c74d 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a 2f  atch overflow */
1c74e 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 48 64 72  .      int szHdr
1c74f 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a  ;         /* Siz
1c750 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20  e of the header 
1c751 73 69 7a 65 20 66 69 65 6c 64 20 61 74 20 73 74  size field at st
1c752 61 72 74 20 6f 66 20 72 65 63 6f 72 64 20 2a 2f  art of record */
1c753 0a 20 20 20 20 20 20 69 6e 74 20 61 76 61 69 6c  .      int avail
1c754 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
1c755 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
1c756 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20 2a  available data *
1c757 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 65  /.      Mem *pRe
1c758 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 73  g;         /* Ps
1c759 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 75 74 20  eudoTable input 
1c75a 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20  register */.    
1c75b 7d 20 61 6d 3b 0a 20 20 20 20 73 74 72 75 63 74  } am;.    struct
1c75c 20 4f 50 5f 41 66 66 69 6e 69 74 79 5f 73 74 61   OP_Affinity_sta
1c75d 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c75e 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 66 66  const char *zAff
1c75f 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68 65 20  inity;   /* The 
1c760 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20 61  affinity to be a
1c761 70 70 6c 69 65 64 20 2a 2f 0a 20 20 20 20 20 20  pplied */.      
1c762 63 68 61 72 20 63 41 66 66 3b 20 20 20 20 20 20  char cAff;      
1c763 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69           /* A si
1c764 6e 67 6c 65 20 63 68 61 72 61 63 74 65 72 20 6f  ngle character o
1c765 66 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20  f affinity */.  
1c766 20 20 7d 20 61 6e 3b 0a 20 20 20 20 73 74 72 75    } an;.    stru
1c767 63 74 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  ct OP_MakeRecord
1c768 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c769 20 20 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f      u8 *zNewReco
1c76a 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20  rd;        /* A 
1c76b 62 75 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74  buffer to hold t
1c76c 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  he data for the 
1c76d 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  new record */.  
1c76e 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20 20      Mem *pRec;  
1c76f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1c770 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f 0a  e new record */.
1c771 20 20 20 20 20 20 75 36 34 20 6e 44 61 74 61 3b        u64 nData;
1c772 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c773 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1c774 6f 66 20 64 61 74 61 20 73 70 61 63 65 20 2a 2f  of data space */
1c775 0a 20 20 20 20 20 20 69 6e 74 20 6e 48 64 72 3b  .      int nHdr;
1c776 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1c777 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1c778 20 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 65   of header space
1c779 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20 6e 42   */.      i64 nB
1c77a 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  yte;            
1c77b 20 2f 2a 20 44 61 74 61 20 73 70 61 63 65 20 72   /* Data space r
1c77c 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69 73  equired for this
1c77d 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20   record */.     
1c77e 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20   int nZero;     
1c77f 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1c780 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 20  r of zero bytes 
1c781 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  at the end of th
1c782 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20  e record */.    
1c783 20 20 69 6e 74 20 6e 56 61 72 69 6e 74 3b 20 20    int nVarint;  
1c784 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1c785 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61  er of bytes in a
1c786 20 76 61 72 69 6e 74 20 2a 2f 0a 20 20 20 20 20   varint */.     
1c787 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65   u32 serial_type
1c788 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70 65 20  ;       /* Type 
1c789 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 20 20 4d  field */.      M
1c78a 65 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20  em *pData0;     
1c78b 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66        /* First f
1c78c 69 65 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69  ield to be combi
1c78d 6e 65 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63  ned into the rec
1c78e 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d  ord */.      Mem
1c78f 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20 20   *pLast;        
1c790 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 69 65 6c      /* Last fiel
1c791 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  d of the record 
1c792 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69  */.      int nFi
1c793 65 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  eld;            
1c794 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 69 65  /* Number of fie
1c795 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  lds in the recor
1c796 64 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20  d */.      char 
1c797 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20 20 20  *zAffinity;     
1c798 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74    /* The affinit
1c799 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65  y string for the
1c79a 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20   record */.     
1c79b 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74   int file_format
1c79c 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20  ;       /* File 
1c79d 66 6f 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f  format to use fo
1c79e 72 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  r encoding */.  
1c79f 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20      int i;      
1c7a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70             /* Sp
1c7a1 61 63 65 20 75 73 65 64 20 69 6e 20 7a 4e 65 77  ace used in zNew
1c7a2 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20 20 20  Record[] */.    
1c7a3 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20    int len;      
1c7a4 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
1c7a5 74 68 20 6f 66 20 61 20 66 69 65 6c 64 20 2a 2f  th of a field */
1c7a6 0a 20 20 20 20 7d 20 61 6f 3b 0a 20 20 20 20 73  .    } ao;.    s
1c7a7 74 72 75 63 74 20 4f 50 5f 43 6f 75 6e 74 5f 73  truct OP_Count_s
1c7a8 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c7a9 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a 20 20    i64 nEntry;.  
1c7aa 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43      BtCursor *pC
1c7ab 72 73 72 3b 0a 20 20 20 20 7d 20 61 70 3b 0a 20  rsr;.    } ap;. 
1c7ac 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 61 76     struct OP_Sav
1c7ad 65 70 6f 69 6e 74 5f 73 74 61 63 6b 5f 76 61 72  epoint_stack_var
1c7ae 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 31  s {.      int p1
1c7af 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1c7b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
1c7b1 75 65 20 6f 66 20 50 31 20 6f 70 65 72 61 6e 64  ue of P1 operand
1c7b2 20 2a 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a   */.      char *
1c7b3 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
1c7b4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
1c7b5 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 20 2a  e of savepoint *
1c7b6 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e 61 6d  /.      int nNam
1c7b7 65 3b 0a 20 20 20 20 20 20 53 61 76 65 70 6f 69  e;.      Savepoi
1c7b8 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20  nt *pNew;.      
1c7b9 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65  Savepoint *pSave
1c7ba 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 53 61 76  point;.      Sav
1c7bb 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20  epoint *pTmp;.  
1c7bc 20 20 20 20 69 6e 74 20 69 53 61 76 65 70 6f 69      int iSavepoi
1c7bd 6e 74 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 69  nt;.      int ii
1c7be 3b 0a 20 20 20 20 7d 20 61 71 3b 0a 20 20 20 20  ;.    } aq;.    
1c7bf 73 74 72 75 63 74 20 4f 50 5f 41 75 74 6f 43 6f  struct OP_AutoCo
1c7c0 6d 6d 69 74 5f 73 74 61 63 6b 5f 76 61 72 73 20  mmit_stack_vars 
1c7c1 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65 73 69  {.      int desi
1c7c2 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20  redAutoCommit;. 
1c7c3 20 20 20 20 20 69 6e 74 20 69 52 6f 6c 6c 62 61       int iRollba
1c7c4 63 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 74 75  ck;.      int tu
1c7c5 72 6e 4f 6e 41 43 3b 0a 20 20 20 20 7d 20 61 72  rnOnAC;.    } ar
1c7c6 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c7c7 54 72 61 6e 73 61 63 74 69 6f 6e 5f 73 74 61 63  Transaction_stac
1c7c8 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42  k_vars {.      B
1c7c9 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d  tree *pBt;.    }
1c7ca 20 61 73 3b 0a 20 20 20 20 73 74 72 75 63 74 20   as;.    struct 
1c7cb 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 5f 73 74  OP_ReadCookie_st
1c7cc 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c7cd 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 20 20   int iMeta;.    
1c7ce 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20    int iDb;.     
1c7cf 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a 20 20   int iCookie;.  
1c7d0 20 20 7d 20 61 74 3b 0a 20 20 20 20 73 74 72 75    } at;.    stru
1c7d1 63 74 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 5f  ct OP_SetCookie_
1c7d2 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c7d3 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 20     Db *pDb;.    
1c7d4 7d 20 61 75 3b 0a 20 20 20 20 73 74 72 75 63 74  } au;.    struct
1c7d5 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65   OP_VerifyCookie
1c7d6 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c7d7 20 20 20 20 69 6e 74 20 69 4d 65 74 61 3b 0a 20      int iMeta;. 
1c7d8 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b       Btree *pBt;
1c7d9 0a 20 20 20 20 7d 20 61 76 3b 0a 20 20 20 20 73  .    } av;.    s
1c7da 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 57 72 69  truct OP_OpenWri
1c7db 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  te_stack_vars {.
1c7dc 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65 6c 64        int nField
1c7dd 3b 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20  ;.      KeyInfo 
1c7de 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20  *pKeyInfo;.     
1c7df 20 69 6e 74 20 70 32 3b 0a 20 20 20 20 20 20 69   int p2;.      i
1c7e0 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 69 6e  nt iDb;.      in
1c7e1 74 20 77 72 46 6c 61 67 3b 0a 20 20 20 20 20 20  t wrFlag;.      
1c7e2 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 20 20 20  Btree *pX;.     
1c7e3 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1c7e4 72 3b 0a 20 20 20 20 20 20 44 62 20 2a 70 44 62  r;.      Db *pDb
1c7e5 3b 0a 20 20 20 20 7d 20 61 77 3b 0a 20 20 20 20  ;.    } aw;.    
1c7e6 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e 45 70  struct OP_OpenEp
1c7e7 68 65 6d 65 72 61 6c 5f 73 74 61 63 6b 5f 76 61  hemeral_stack_va
1c7e8 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43  rs {.      VdbeC
1c7e9 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20  ursor *pCx;.    
1c7ea 7d 20 61 78 3b 0a 20 20 20 20 73 74 72 75 63 74  } ax;.    struct
1c7eb 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 5f 73   OP_OpenPseudo_s
1c7ec 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c7ed 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c7ee 78 3b 0a 20 20 20 20 7d 20 61 79 3b 0a 20 20 20  x;.    } ay;.   
1c7ef 20 73 74 72 75 63 74 20 4f 50 5f 53 65 65 6b 47   struct OP_SeekG
1c7f0 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c7f1 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20       int res;.  
1c7f2 20 20 20 20 69 6e 74 20 6f 63 3b 0a 20 20 20 20      int oc;.    
1c7f3 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1c7f4 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64  ;.      Unpacked
1c7f5 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 20 20  Record r;.      
1c7f6 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20  int nField;.    
1c7f7 20 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20    i64 iKey;     
1c7f8 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20 77 65   /* The rowid we
1c7f9 20 61 72 65 20 74 6f 20 73 65 65 6b 20 74 6f 20   are to seek to 
1c7fa 2a 2f 0a 20 20 20 20 7d 20 61 7a 3b 0a 20 20 20  */.    } az;.   
1c7fb 20 73 74 72 75 63 74 20 4f 50 5f 53 65 65 6b 5f   struct OP_Seek_
1c7fc 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c7fd 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
1c7fe 43 3b 0a 20 20 20 20 7d 20 62 61 3b 0a 20 20 20  C;.    } ba;.   
1c7ff 20 73 74 72 75 63 74 20 4f 50 5f 46 6f 75 6e 64   struct OP_Found
1c800 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c801 20 20 20 20 69 6e 74 20 61 6c 72 65 61 64 79 45      int alreadyE
1c802 78 69 73 74 73 3b 0a 20 20 20 20 20 20 56 64 62  xists;.      Vdb
1c803 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20  eCursor *pC;.   
1c804 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
1c805 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1c806 20 2a 70 49 64 78 4b 65 79 3b 0a 20 20 20 20 20   *pIdxKey;.     
1c807 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20   UnpackedRecord 
1c808 72 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61 54  r;.      char aT
1c809 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69  empRec[ROUND8(si
1c80a 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63  zeof(UnpackedRec
1c80b 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d  ord)) + sizeof(M
1c80c 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 20 20 20 20  em)*3 + 7];.    
1c80d 7d 20 62 62 3b 0a 20 20 20 20 73 74 72 75 63 74  } bb;.    struct
1c80e 20 4f 50 5f 49 73 55 6e 69 71 75 65 5f 73 74 61   OP_IsUnique_sta
1c80f 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c810 75 31 36 20 69 69 3b 0a 20 20 20 20 20 20 56 64  u16 ii;.      Vd
1c811 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20  beCursor *pCx;. 
1c812 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70       BtCursor *p
1c813 43 72 73 72 3b 0a 20 20 20 20 20 20 75 31 36 20  Crsr;.      u16 
1c814 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 4d 65  nField;.      Me
1c815 6d 20 2a 61 4d 78 3b 0a 20 20 20 20 20 20 55 6e  m *aMx;.      Un
1c816 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 20  packedRecord r; 
1c817 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c818 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65 78   /* B-Tree index
1c819 20 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20   search key */. 
1c81a 20 20 20 20 20 69 36 34 20 52 3b 20 20 20 20 20       i64 R;     
1c81b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c81c 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64          /* Rowid
1c81d 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1c81e 74 65 72 20 50 33 20 2a 2f 0a 20 20 20 20 7d 20  ter P3 */.    } 
1c81f 62 63 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bc;.    struct O
1c820 50 5f 4e 6f 74 45 78 69 73 74 73 5f 73 74 61 63  P_NotExists_stac
1c821 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56  k_vars {.      V
1c822 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  dbeCursor *pC;. 
1c823 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70       BtCursor *p
1c824 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20  Crsr;.      int 
1c825 72 65 73 3b 0a 20 20 20 20 20 20 75 36 34 20 69  res;.      u64 i
1c826 4b 65 79 3b 0a 20 20 20 20 7d 20 62 64 3b 0a 20  Key;.    } bd;. 
1c827 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 65 77     struct OP_New
1c828 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72 73  Rowid_stack_vars
1c829 20 7b 0a 20 20 20 20 20 20 69 36 34 20 76 3b 20   {.      i64 v; 
1c82a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c82b 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69 64  /* The new rowid
1c82c 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75   */.      VdbeCu
1c82d 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20 20  rsor *pC;       
1c82e 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 61   /* Cursor of ta
1c82f 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20 6e  ble to get the n
1c830 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20  ew rowid */.    
1c831 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20    int res;      
1c832 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
1c833 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33  lt of an sqlite3
1c834 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a 20  BtreeLast() */. 
1c835 20 20 20 20 20 69 6e 74 20 63 6e 74 3b 20 20 20       int cnt;   
1c836 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
1c837 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74 20  ounter to limit 
1c838 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65  the number of se
1c839 61 72 63 68 65 73 20 2a 2f 0a 20 20 20 20 20 20  arches */.      
1c83a 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20  Mem *pMem;      
1c83b 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
1c83c 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67 65  er holding large
1c83d 73 74 20 72 6f 77 69 64 20 66 6f 72 20 41 55 54  st rowid for AUT
1c83e 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20 20  OINCREMENT */.  
1c83f 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70      VdbeFrame *p
1c840 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52 6f  Frame;     /* Ro
1c841 6f 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42 45  ot frame of VDBE
1c842 20 2a 2f 0a 20 20 20 20 7d 20 62 65 3b 0a 20 20   */.    } be;.  
1c843 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 73 65    struct OP_Inse
1c844 72 74 49 6e 74 5f 73 74 61 63 6b 5f 76 61 72 73  rtInt_stack_vars
1c845 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44   {.      Mem *pD
1c846 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 4d 45  ata;       /* ME
1c847 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20 64  M cell holding d
1c848 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 63 6f  ata for the reco
1c849 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  rd to be inserte
1c84a 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a  d */.      Mem *
1c84b 70 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a 20  pKey;        /* 
1c84c 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67  MEM cell holding
1c84d 20 6b 65 79 20 20 66 6f 72 20 74 68 65 20 72 65   key  for the re
1c84e 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 36  cord */.      i6
1c84f 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20  4 iKey;         
1c850 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20 52  /* The integer R
1c851 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 6f 72 20  OWID or key for 
1c852 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62 65  the record to be
1c853 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 20   inserted */.   
1c854 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
1c855 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74  C;   /* Cursor t
1c856 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69  o table into whi
1c857 63 68 20 69 6e 73 65 72 74 20 69 73 20 77 72 69  ch insert is wri
1c858 74 74 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e  tten */.      in
1c859 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20  t nZero;        
1c85a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72  /* Number of zer
1c85b 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 70 65 6e  o-bytes to appen
1c85c 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 73  d */.      int s
1c85d 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20  eekResult;   /* 
1c85e 52 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72 20  Result of prior 
1c85f 73 65 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f 20  seek or 0 if no 
1c860 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66 6c  USESEEKRESULT fl
1c861 61 67 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73  ag */.      cons
1c862 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 2f 2a  t char *zDb;  /*
1c863 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2d   database name -
1c864 20 75 73 65 64 20 62 79 20 74 68 65 20 75 70 64   used by the upd
1c865 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 20 20  ate hook */.    
1c866 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
1c867 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d  bl; /* Table nam
1c868 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20  e - used by the 
1c869 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20  opdate hook */. 
1c86a 20 20 20 20 20 69 6e 74 20 6f 70 3b 20 20 20 20       int op;    
1c86b 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65         /* Opcode
1c86c 20 66 6f 72 20 75 70 64 61 74 65 20 68 6f 6f 6b   for update hook
1c86d 3a 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20  : SQLITE_UPDATE 
1c86e 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54  or SQLITE_INSERT
1c86f 20 2a 2f 0a 20 20 20 20 7d 20 62 66 3b 0a 20 20   */.    } bf;.  
1c870 20 20 73 74 72 75 63 74 20 4f 50 5f 44 65 6c 65    struct OP_Dele
1c871 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  te_stack_vars {.
1c872 20 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b 0a        i64 iKey;.
1c873 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c874 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 67 3b 0a   *pC;.    } bg;.
1c875 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52 6f      struct OP_Ro
1c876 77 44 61 74 61 5f 73 74 61 63 6b 5f 76 61 72 73  wData_stack_vars
1c877 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72   {.      VdbeCur
1c878 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42  sor *pC;.      B
1c879 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1c87a 20 20 20 20 20 20 75 33 32 20 6e 3b 0a 20 20 20        u32 n;.   
1c87b 20 20 20 69 36 34 20 6e 36 34 3b 0a 20 20 20 20     i64 n64;.    
1c87c 7d 20 62 68 3b 0a 20 20 20 20 73 74 72 75 63 74  } bh;.    struct
1c87d 20 4f 50 5f 52 6f 77 69 64 5f 73 74 61 63 6b 5f   OP_Rowid_stack_
1c87e 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62  vars {.      Vdb
1c87f 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20  eCursor *pC;.   
1c880 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20     i64 v;.      
1c881 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1c882 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  tab;.      const
1c883 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
1c884 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 20  *pModule;.    } 
1c885 62 69 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  bi;.    struct O
1c886 50 5f 4e 75 6c 6c 52 6f 77 5f 73 74 61 63 6b 5f  P_NullRow_stack_
1c887 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62  vars {.      Vdb
1c888 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20  eCursor *pC;.   
1c889 20 7d 20 62 6a 3b 0a 20 20 20 20 73 74 72 75 63   } bj;.    struc
1c88a 74 20 4f 50 5f 4c 61 73 74 5f 73 74 61 63 6b 5f  t OP_Last_stack_
1c88b 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62  vars {.      Vdb
1c88c 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20  eCursor *pC;.   
1c88d 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72     BtCursor *pCr
1c88e 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65  sr;.      int re
1c88f 73 3b 0a 20 20 20 20 7d 20 62 6b 3b 0a 20 20 20  s;.    } bk;.   
1c890 20 73 74 72 75 63 74 20 4f 50 5f 52 65 77 69 6e   struct OP_Rewin
1c891 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  d_stack_vars {. 
1c892 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c893 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75 72  *pC;.      BtCur
1c894 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20 20  sor *pCrsr;.    
1c895 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 7d    int res;.    }
1c896 20 62 6c 3b 0a 20 20 20 20 73 74 72 75 63 74 20   bl;.    struct 
1c897 4f 50 5f 4e 65 78 74 5f 73 74 61 63 6b 5f 76 61  OP_Next_stack_va
1c898 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43  rs {.      VdbeC
1c899 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20  ursor *pC;.     
1c89a 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1c89b 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b  ;.      int res;
1c89c 0a 20 20 20 20 7d 20 62 6d 3b 0a 20 20 20 20 73  .    } bm;.    s
1c89d 74 72 75 63 74 20 4f 50 5f 49 64 78 49 6e 73 65  truct OP_IdxInse
1c89e 72 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  rt_stack_vars {.
1c89f 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c8a0 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75   *pC;.      BtCu
1c8a1 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20  rsor *pCrsr;.   
1c8a2 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 20     int nKey;.   
1c8a3 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1c8a4 4b 65 79 3b 0a 20 20 20 20 7d 20 62 6e 3b 0a 20  Key;.    } bn;. 
1c8a5 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78     struct OP_Idx
1c8a6 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61 72  Delete_stack_var
1c8a7 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75  s {.      VdbeCu
1c8a8 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20  rsor *pC;.      
1c8a9 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1c8aa 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a  .      int res;.
1c8ab 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65        UnpackedRe
1c8ac 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d 20 62 6f  cord r;.    } bo
1c8ad 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c8ae 49 64 78 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76  IdxRowid_stack_v
1c8af 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 43 75  ars {.      BtCu
1c8b0 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20  rsor *pCrsr;.   
1c8b1 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
1c8b2 43 3b 0a 20 20 20 20 20 20 69 36 34 20 72 6f 77  C;.      i64 row
1c8b3 69 64 3b 0a 20 20 20 20 7d 20 62 70 3b 0a 20 20  id;.    } bp;.  
1c8b4 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78 47    struct OP_IdxG
1c8b5 45 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  E_stack_vars {. 
1c8b6 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c8b7 2a 70 43 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  *pC;.      int r
1c8b8 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b  es;.      Unpack
1c8b9 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20  edRecord r;.    
1c8ba 7d 20 62 71 3b 0a 20 20 20 20 73 74 72 75 63 74  } bq;.    struct
1c8bb 20 4f 50 5f 44 65 73 74 72 6f 79 5f 73 74 61 63   OP_Destroy_stac
1c8bc 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c8bd 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20 20 20 20  nt iMoved;.     
1c8be 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20 20 20 20   int iCnt;.     
1c8bf 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20 20   Vdbe *pVdbe;.  
1c8c0 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20      int iDb;.   
1c8c1 20 7d 20 62 72 3b 0a 20 20 20 20 73 74 72 75 63   } br;.    struc
1c8c2 74 20 4f 50 5f 43 6c 65 61 72 5f 73 74 61 63 6b  t OP_Clear_stack
1c8c3 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
1c8c4 74 20 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 7d  t nChange;.    }
1c8c5 20 62 73 3b 0a 20 20 20 20 73 74 72 75 63 74 20   bs;.    struct 
1c8c6 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 5f 73  OP_CreateTable_s
1c8c7 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c8c8 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20 20    int pgno;.    
1c8c9 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 20    int flags;.   
1c8ca 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 20     Db *pDb;.    
1c8cb 7d 20 62 74 3b 0a 20 20 20 20 73 74 72 75 63 74  } bt;.    struct
1c8cc 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 5f   OP_ParseSchema_
1c8cd 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c8ce 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20     int iDb;.    
1c8cf 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d    const char *zM
1c8d0 61 73 74 65 72 3b 0a 20 20 20 20 20 20 63 68 61  aster;.      cha
1c8d1 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 20 20 49  r *zSql;.      I
1c8d2 6e 69 74 44 61 74 61 20 69 6e 69 74 44 61 74 61  nitData initData
1c8d3 3b 0a 20 20 20 20 7d 20 62 75 3b 0a 20 20 20 20  ;.    } bu;.    
1c8d4 73 74 72 75 63 74 20 4f 50 5f 49 6e 74 65 67 72  struct OP_Integr
1c8d5 69 74 79 43 6b 5f 73 74 61 63 6b 5f 76 61 72 73  ityCk_stack_vars
1c8d6 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 52 6f   {.      int nRo
1c8d7 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ot;      /* Numb
1c8d8 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74 6f 20  er of tables to 
1c8d9 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65 72 20  check.  (Number 
1c8da 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e 29 20  of root pages.) 
1c8db 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 2a 61 52  */.      int *aR
1c8dc 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61  oot;     /* Arra
1c8dd 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75  y of rootpage nu
1c8de 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73  mbers for tables
1c8df 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a   to be checked *
1c8e0 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 20 20  /.      int j;  
1c8e1 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1c8e2 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20  counter */.     
1c8e3 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20 20   int nErr;      
1c8e4 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72   /* Number of er
1c8e5 72 6f 72 73 20 72 65 70 6f 72 74 65 64 20 2a 2f  rors reported */
1c8e6 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b 20  .      char *z; 
1c8e7 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f         /* Text o
1c8e8 66 20 74 68 65 20 65 72 72 6f 72 20 72 65 70 6f  f the error repo
1c8e9 72 74 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20  rt */.      Mem 
1c8ea 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52  *pnErr;     /* R
1c8eb 65 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20  egister keeping 
1c8ec 74 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20  track of errors 
1c8ed 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 20 20 20  remaining */.   
1c8ee 20 7d 20 62 76 3b 0a 20 20 20 20 73 74 72 75 63   } bv;.    struc
1c8ef 74 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 5f  t OP_RowSetRead_
1c8f0 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c8f1 20 20 20 69 36 34 20 76 61 6c 3b 0a 20 20 20 20     i64 val;.    
1c8f2 7d 20 62 77 3b 0a 20 20 20 20 73 74 72 75 63 74  } bw;.    struct
1c8f3 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 5f 73   OP_RowSetTest_s
1c8f4 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c8f5 20 20 69 6e 74 20 69 53 65 74 3b 0a 20 20 20 20    int iSet;.    
1c8f6 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a 20 20    int exists;.  
1c8f7 20 20 7d 20 62 78 3b 0a 20 20 20 20 73 74 72 75    } bx;.    stru
1c8f8 63 74 20 4f 50 5f 50 72 6f 67 72 61 6d 5f 73 74  ct OP_Program_st
1c8f9 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c8fa 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20   int nMem;      
1c8fb 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1c8fc 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 67  er of memory reg
1c8fd 69 73 74 65 72 73 20 66 6f 72 20 73 75 62 2d 70  isters for sub-p
1c8fe 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20 20 20  rogram */.      
1c8ff 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20  int nByte;      
1c900 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
1c901 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63   of runtime spac
1c902 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 73  e required for s
1c903 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  ub-program */.  
1c904 20 20 20 20 4d 65 6d 20 2a 70 52 74 3b 20 20 20      Mem *pRt;   
1c905 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1c906 65 67 69 73 74 65 72 20 74 6f 20 61 6c 6c 6f 63  egister to alloc
1c907 61 74 65 20 72 75 6e 74 69 6d 65 20 73 70 61 63  ate runtime spac
1c908 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a  e */.      Mem *
1c909 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  pMem;           
1c90a 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74     /* Used to it
1c90b 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65  erate through me
1c90c 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20  mory cells */.  
1c90d 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 20 20      Mem *pEnd;  
1c90e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1c90f 61 73 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  ast memory cell 
1c910 69 6e 20 6e 65 77 20 61 72 72 61 79 20 2a 2f 0a  in new array */.
1c911 20 20 20 20 20 20 56 64 62 65 46 72 61 6d 65 20        VdbeFrame 
1c912 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a  *pFrame;      /*
1c913 20 4e 65 77 20 76 64 62 65 20 66 72 61 6d 65 20   New vdbe frame 
1c914 74 6f 20 65 78 65 63 75 74 65 20 69 6e 20 2a 2f  to execute in */
1c915 0a 20 20 20 20 20 20 53 75 62 50 72 6f 67 72 61  .      SubProgra
1c916 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f  m *pProgram;   /
1c917 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d 20 74 6f  * Sub-program to
1c918 20 65 78 65 63 75 74 65 20 2a 2f 0a 20 20 20 20   execute */.    
1c919 20 20 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20    void *t;      
1c91a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
1c91b 65 6e 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74  en identifying t
1c91c 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 20 7d 20  rigger */.    } 
1c91d 62 79 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  by;.    struct O
1c91e 50 5f 50 61 72 61 6d 5f 73 74 61 63 6b 5f 76 61  P_Param_stack_va
1c91f 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 46  rs {.      VdbeF
1c920 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20  rame *pFrame;.  
1c921 20 20 20 20 4d 65 6d 20 2a 70 49 6e 3b 0a 20 20      Mem *pIn;.  
1c922 20 20 7d 20 62 7a 3b 0a 20 20 20 20 73 74 72 75    } bz;.    stru
1c923 63 74 20 4f 50 5f 4d 65 6d 4d 61 78 5f 73 74 61  ct OP_MemMax_sta
1c924 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c925 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 20 20 20  Mem *pIn1;.     
1c926 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61   VdbeFrame *pFra
1c927 6d 65 3b 0a 20 20 20 20 7d 20 63 61 3b 0a 20 20  me;.    } ca;.  
1c928 20 20 73 74 72 75 63 74 20 4f 50 5f 41 67 67 53    struct OP_AggS
1c929 74 65 70 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  tep_stack_vars {
1c92a 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20  .      int n;.  
1c92b 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
1c92c 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20   Mem *pMem;.    
1c92d 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a 20 20 20    Mem *pRec;.   
1c92e 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65     sqlite3_conte
1c92f 78 74 20 63 74 78 3b 0a 20 20 20 20 20 20 73 71  xt ctx;.      sq
1c930 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
1c931 56 61 6c 3b 0a 20 20 20 20 7d 20 63 62 3b 0a 20  Val;.    } cb;. 
1c932 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41 67 67     struct OP_Agg
1c933 46 69 6e 61 6c 5f 73 74 61 63 6b 5f 76 61 72 73  Final_stack_vars
1c934 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d   {.      Mem *pM
1c935 65 6d 3b 0a 20 20 20 20 7d 20 63 63 3b 0a 20 20  em;.    } cc;.  
1c936 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 63 72    struct OP_Incr
1c937 56 61 63 75 75 6d 5f 73 74 61 63 6b 5f 76 61 72  Vacuum_stack_var
1c938 73 20 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20  s {.      Btree 
1c939 2a 70 42 74 3b 0a 20 20 20 20 7d 20 63 64 3b 0a  *pBt;.    } cd;.
1c93a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56 42      struct OP_VB
1c93b 65 67 69 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20  egin_stack_vars 
1c93c 7b 0a 20 20 20 20 20 20 56 54 61 62 6c 65 20 2a  {.      VTable *
1c93d 70 56 54 61 62 3b 0a 20 20 20 20 7d 20 63 65 3b  pVTab;.    } ce;
1c93e 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 56  .    struct OP_V
1c93f 4f 70 65 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20  Open_stack_vars 
1c940 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  {.      VdbeCurs
1c941 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 20 20  or *pCur;.      
1c942 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
1c943 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72  sor *pVtabCursor
1c944 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
1c945 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20  vtab *pVtab;.   
1c946 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c     sqlite3_modul
1c947 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20  e *pModule;.    
1c948 7d 20 63 66 3b 0a 20 20 20 20 73 74 72 75 63 74  } cf;.    struct
1c949 20 4f 50 5f 56 46 69 6c 74 65 72 5f 73 74 61 63   OP_VFilter_stac
1c94a 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c94b 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 20 20 69  nt nArg;.      i
1c94c 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 20 20 20  nt iQuery;.     
1c94d 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
1c94e 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
1c94f 20 20 20 20 20 20 4d 65 6d 20 2a 70 51 75 65 72        Mem *pQuer
1c950 79 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 41  y;.      Mem *pA
1c951 72 67 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  rgc;.      sqlit
1c952 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
1c953 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20  pVtabCursor;.   
1c954 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20     sqlite3_vtab 
1c955 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 56 64  *pVtab;.      Vd
1c956 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a  beCursor *pCur;.
1c957 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
1c958 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
1c959 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20    Mem **apArg;. 
1c95a 20 20 20 7d 20 63 67 3b 0a 20 20 20 20 73 74 72     } cg;.    str
1c95b 75 63 74 20 4f 50 5f 56 43 6f 6c 75 6d 6e 5f 73  uct OP_VColumn_s
1c95c 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c95d 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1c95e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e  pVtab;.      con
1c95f 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
1c960 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20  e *pModule;.    
1c961 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20 20    Mem *pDest;.  
1c962 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74      sqlite3_cont
1c963 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 0a 20 20  ext sContext;.  
1c964 20 20 7d 20 63 68 3b 0a 20 20 20 20 73 74 72 75    } ch;.    stru
1c965 63 74 20 4f 50 5f 56 4e 65 78 74 5f 73 74 61 63  ct OP_VNext_stac
1c966 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 73  k_vars {.      s
1c967 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
1c968 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  ab;.      const 
1c969 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
1c96a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 20 20 69  pModule;.      i
1c96b 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 56 64  nt res;.      Vd
1c96c 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a  beCursor *pCur;.
1c96d 20 20 20 20 7d 20 63 69 3b 0a 20 20 20 20 73 74      } ci;.    st
1c96e 72 75 63 74 20 4f 50 5f 56 52 65 6e 61 6d 65 5f  ruct OP_VRename_
1c96f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c970 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20     sqlite3_vtab 
1c971 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20 4d 65  *pVtab;.      Me
1c972 6d 20 2a 70 4e 61 6d 65 3b 0a 20 20 20 20 7d 20  m *pName;.    } 
1c973 63 6a 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  cj;.    struct O
1c974 50 5f 56 55 70 64 61 74 65 5f 73 74 61 63 6b 5f  P_VUpdate_stack_
1c975 76 61 72 73 20 7b 0a 20 20 20 20 20 20 73 71 6c  vars {.      sql
1c976 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1c977 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
1c978 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
1c979 0a 20 20 20 20 20 20 69 6e 74 20 6e 41 72 67 3b  .      int nArg;
1c97a 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
1c97b 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
1c97c 20 72 6f 77 69 64 3b 0a 20 20 20 20 20 20 4d 65   rowid;.      Me
1c97d 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20 20 20 20  m **apArg;.     
1c97e 20 4d 65 6d 20 2a 70 58 3b 0a 20 20 20 20 7d 20   Mem *pX;.    } 
1c97f 63 6b 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  ck;.    struct O
1c980 50 5f 50 61 67 65 63 6f 75 6e 74 5f 73 74 61 63  P_Pagecount_stac
1c981 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c982 6e 74 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74  nt p1;.      int
1c983 20 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 50 61   nPage;.      Pa
1c984 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20  ger *pPager;.   
1c985 20 7d 20 63 6c 3b 0a 20 20 20 20 73 74 72 75 63   } cl;.    struc
1c986 74 20 4f 50 5f 54 72 61 63 65 5f 73 74 61 63 6b  t OP_Trace_stack
1c987 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 63 68  _vars {.      ch
1c988 61 72 20 2a 7a 54 72 61 63 65 3b 0a 20 20 20 20  ar *zTrace;.    
1c989 7d 20 63 6d 3b 0a 20 20 7d 20 75 3b 0a 20 20 2f  } cm;.  } u;.  /
1c98a 2a 20 45 6e 64 20 61 75 74 6f 6d 61 74 69 63 61  * End automatica
1c98b 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f  lly generated co
1c98c 64 65 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  de.  ***********
1c98d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c98e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c98f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 61 73  *********/..  as
1c991 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d  sert( p->magic==
1c992 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 29  VDBE_MAGIC_RUN )
1c993 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74  ;  /* sqlite3_st
1c994 65 70 28 29 20 76 65 72 69 66 69 65 73 20 74 68  ep() verifies th
1c995 69 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  is */.  assert( 
1c996 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54  db->magic==SQLIT
1c997 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 3b 0a  E_MAGIC_BUSY );.
1c998 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74    sqlite3VdbeMut
1c999 65 78 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b  exArrayEnter(p);
1c99a 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51  .  if( p->rc==SQ
1c99b 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
1c99c 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e    /* This happen
1c99d 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  s if a malloc() 
1c99e 69 6e 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f  inside a call to
1c99f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1c9a0 74 65 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a  text() or.    **
1c9a1 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1c9a2 74 65 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e  text16() failed.
1c9a3 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e 6f    */.    goto no
1c9a4 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65  _mem;.  }.  asse
1c9a5 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54  rt( p->rc==SQLIT
1c9a6 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53  E_OK || p->rc==S
1c9a7 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20  QLITE_BUSY );.  
1c9a8 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  p->rc = SQLITE_O
1c9a9 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  K;.  assert( p->
1c9aa 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20 20  explain==0 );.  
1c9ab 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20  p->pResultSet = 
1c9ac 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e  0;.  db->busyHan
1c9ad 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a  dler.nBusy = 0;.
1c9ae 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45    CHECK_FOR_INTE
1c9af 52 52 55 50 54 3b 0a 20 20 73 71 6c 69 74 65 33  RRUPT;.  sqlite3
1c9b0 56 64 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70  VdbeIOTraceSql(p
1c9b1 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
1c9b2 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f  E_OMIT_PROGRESS_
1c9b3 43 41 4c 4c 42 41 43 4b 0a 20 20 63 68 65 63 6b  CALLBACK.  check
1c9b4 50 72 6f 67 72 65 73 73 20 3d 20 64 62 2d 3e 78  Progress = db->x
1c9b5 50 72 6f 67 72 65 73 73 21 3d 30 3b 0a 23 65 6e  Progress!=0;.#en
1c9b6 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
1c9b7 45 5f 44 45 42 55 47 0a 20 20 73 71 6c 69 74 65  E_DEBUG.  sqlite
1c9b8 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
1c9b9 6f 63 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  oc();.  if( p->p
1c9ba 63 3d 3d 30 20 0a 20 20 20 26 26 20 28 28 70 2d  c==0 .   && ((p-
1c9bb 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  >db->flags & SQL
1c9bc 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 29  ITE_VdbeListing)
1c9bd 20 7c 7c 20 66 69 6c 65 45 78 69 73 74 73 28 64   || fileExists(d
1c9be 62 2c 20 22 76 64 62 65 5f 65 78 70 6c 61 69 6e  b, "vdbe_explain
1c9bf 22 29 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74  ")).  ){.    int
1c9c0 20 69 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22   i;.    printf("
1c9c1 56 44 42 45 20 50 72 6f 67 72 61 6d 20 4c 69 73  VDBE Program Lis
1c9c2 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73  ting:\n");.    s
1c9c3 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 53  qlite3VdbePrintS
1c9c4 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f 72 28 69  ql(p);.    for(i
1c9c5 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b  =0; i<p->nOp; i+
1c9c6 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
1c9c7 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73 74 64  3VdbePrintOp(std
1c9c8 6f 75 74 2c 20 69 2c 20 26 61 4f 70 5b 69 5d 29  out, i, &aOp[i])
1c9c9 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
1c9ca 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c  ( fileExists(db,
1c9cb 20 22 76 64 62 65 5f 74 72 61 63 65 22 29 20 29   "vdbe_trace") )
1c9cc 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 65 20 3d  {.    p->trace =
1c9cd 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a 20 20 73   stdout;.  }.  s
1c9ce 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
1c9cf 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69 66 0a  alloc();.#endif.
1c9d0 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63 3b 20    for(pc=p->pc; 
1c9d1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70  rc==SQLITE_OK; p
1c9d2 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74  c++){.    assert
1c9d3 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c 70 2d  ( pc>=0 && pc<p-
1c9d4 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66 28 20  >nOp );.    if( 
1c9d5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1c9d6 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1c9d7 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
1c9d8 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63 20 3d  ILE.    origPc =
1c9d9 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74 20 3d   pc;.    start =
1c9da 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
1c9db 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4f 70  ;.#endif.    pOp
1c9dc 20 3d 20 26 61 4f 70 5b 70 63 5d 3b 0a 0a 20 20   = &aOp[pc];..  
1c9dd 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c 6f 77 20    /* Only allow 
1c9de 74 72 61 63 69 6e 67 20 69 66 20 53 51 4c 49 54  tracing if SQLIT
1c9df 45 5f 44 45 42 55 47 20 69 73 20 64 65 66 69 6e  E_DEBUG is defin
1c9e0 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65  ed..    */.#ifde
1c9e1 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
1c9e2 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20     if( p->trace 
1c9e3 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 63 3d  ){.      if( pc=
1c9e4 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72  =0 ){.        pr
1c9e5 69 6e 74 66 28 22 56 44 42 45 20 45 78 65 63 75  intf("VDBE Execu
1c9e6 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e 22 29 3b  tion Trace:\n");
1c9e7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1c9e8 56 64 62 65 50 72 69 6e 74 53 71 6c 28 70 29 3b  VdbePrintSql(p);
1c9e9 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
1c9ea 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f  qlite3VdbePrintO
1c9eb 70 28 70 2d 3e 74 72 61 63 65 2c 20 70 63 2c 20  p(p->trace, pc, 
1c9ec 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  pOp);.    }.    
1c9ed 69 66 28 20 70 2d 3e 74 72 61 63 65 3d 3d 30 20  if( p->trace==0 
1c9ee 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20  && pc==0 ){.    
1c9ef 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
1c9f0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
1c9f1 20 20 20 20 69 66 28 20 66 69 6c 65 45 78 69 73      if( fileExis
1c9f2 74 73 28 64 62 2c 20 22 76 64 62 65 5f 73 71 6c  ts(db, "vdbe_sql
1c9f3 74 72 61 63 65 22 29 20 29 7b 0a 20 20 20 20 20  trace") ){.     
1c9f4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72     sqlite3VdbePr
1c9f5 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20  intSql(p);.     
1c9f6 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1c9f7 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
1c9f8 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
1c9f9 20 20 20 20 20 20 0a 0a 20 20 20 20 2f 2a 20 43        ..    /* C
1c9fa 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77  heck to see if w
1c9fb 65 20 6e 65 65 64 20 74 6f 20 73 69 6d 75 6c 61  e need to simula
1c9fc 74 65 20 61 6e 20 69 6e 74 65 72 72 75 70 74 2e  te an interrupt.
1c9fd 20 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70    This only happ
1c9fe 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66 20 77 65  ens.    ** if we
1c9ff 20 68 61 76 65 20 61 20 73 70 65 63 69 61 6c 20   have a special 
1ca00 74 65 73 74 20 62 75 69 6c 64 2e 0a 20 20 20 20  test build..    
1ca01 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1ca02 5f 54 45 53 54 0a 20 20 20 20 69 66 28 20 73 71  _TEST.    if( sq
1ca03 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f  lite3_interrupt_
1ca04 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20  count>0 ){.     
1ca05 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
1ca06 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20 20 20 20  pt_count--;.    
1ca07 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
1ca08 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 3d 3d 30  terrupt_count==0
1ca09 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1ca0a 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 64 62  te3_interrupt(db
1ca0b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1ca0c 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66  .#endif..#ifndef
1ca0d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f   SQLITE_OMIT_PRO
1ca0e 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20  GRESS_CALLBACK. 
1ca0f 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70     /* Call the p
1ca10 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b  rogress callback
1ca11 20 69 66 20 69 74 20 69 73 20 63 6f 6e 66 69 67   if it is config
1ca12 75 72 65 64 20 61 6e 64 20 74 68 65 20 72 65 71  ured and the req
1ca13 75 69 72 65 64 20 6e 75 6d 62 65 72 0a 20 20 20  uired number.   
1ca14 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f 70 73 20   ** of VDBE ops 
1ca15 68 61 76 65 20 62 65 65 6e 20 65 78 65 63 75 74  have been execut
1ca16 65 64 20 28 65 69 74 68 65 72 20 73 69 6e 63 65  ed (either since
1ca17 20 74 68 69 73 20 69 6e 76 6f 63 61 74 69 6f 6e   this invocation
1ca18 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74   of.    ** sqlit
1ca19 65 33 56 64 62 65 45 78 65 63 28 29 20 6f 72 20  e3VdbeExec() or 
1ca1a 73 69 6e 63 65 20 6c 61 73 74 20 74 69 6d 65 20  since last time 
1ca1b 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
1ca1c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c 6c 65 64  lback was called
1ca1d 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65  )..    ** If the
1ca1e 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
1ca1f 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a  ck returns non-z
1ca20 65 72 6f 2c 20 65 78 69 74 20 74 68 65 20 76 69  ero, exit the vi
1ca21 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 77 69  rtual machine wi
1ca22 74 68 0a 20 20 20 20 2a 2a 20 61 20 72 65 74 75  th.    ** a retu
1ca23 72 6e 20 63 6f 64 65 20 53 51 4c 49 54 45 5f 41  rn code SQLITE_A
1ca24 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  BORT..    */.   
1ca25 20 69 66 28 20 63 68 65 63 6b 50 72 6f 67 72 65   if( checkProgre
1ca26 73 73 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ss ){.      if( 
1ca27 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70 73  db->nProgressOps
1ca28 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70 73 20 29  ==nProgressOps )
1ca29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 70 72  {.        int pr
1ca2a 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  c;.        if( s
1ca2b 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1ca2c 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1ca2d 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1ca2e 20 20 20 20 20 20 20 20 70 72 63 20 3d 64 62 2d          prc =db-
1ca2f 3e 78 50 72 6f 67 72 65 73 73 28 64 62 2d 3e 70  >xProgress(db->p
1ca30 50 72 6f 67 72 65 73 73 41 72 67 29 3b 0a 20 20  ProgressArg);.  
1ca31 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1ca32 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20  3SafetyOn(db) ) 
1ca33 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1ca34 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20  o_misuse;.      
1ca35 20 20 69 66 28 20 70 72 63 21 3d 30 20 29 7b 0a    if( prc!=0 ){.
1ca36 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
1ca37 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b  QLITE_INTERRUPT;
1ca38 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1ca39 76 64 62 65 5f 65 72 72 6f 72 5f 68 61 6c 74 3b  vdbe_error_halt;
1ca3a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1ca3b 20 20 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20     nProgressOps 
1ca3c 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
1ca3d 20 20 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 2b     nProgressOps+
1ca3e 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  +;.    }.#endif.
1ca3f 0a 20 20 20 20 2f 2a 20 4f 6e 20 61 6e 79 20 6f  .    /* On any o
1ca40 70 63 6f 64 65 20 77 69 74 68 20 74 68 65 20 22  pcode with the "
1ca41 6f 75 74 32 2d 70 72 65 72 65 6c 61 73 65 22 20  out2-prerelase" 
1ca42 74 61 67 2c 20 66 72 65 65 20 61 6e 79 0a 20 20  tag, free any.  
1ca43 20 20 2a 2a 20 65 78 74 65 72 6e 61 6c 20 61 6c    ** external al
1ca44 6c 6f 63 61 74 69 6f 6e 73 20 6f 75 74 20 6f 66  locations out of
1ca45 20 6d 65 6d 5b 70 32 5d 20 61 6e 64 20 73 65 74   mem[p2] and set
1ca46 20 6d 65 6d 5b 70 32 5d 20 74 6f 20 62 65 0a 20   mem[p2] to be. 
1ca47 20 20 20 2a 2a 20 61 6e 20 75 6e 64 65 66 69 6e     ** an undefin
1ca48 65 64 20 69 6e 74 65 67 65 72 2e 20 20 4f 70 63  ed integer.  Opc
1ca49 6f 64 65 73 20 77 69 6c 6c 20 65 69 74 68 65 72  odes will either
1ca4a 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 69 6e 74   fill in the int
1ca4b 65 67 65 72 0a 20 20 20 20 2a 2a 20 76 61 6c 75  eger.    ** valu
1ca4c 65 20 6f 72 20 63 6f 6e 76 65 72 74 20 6d 65 6d  e or convert mem
1ca4d 5b 70 32 5d 20 74 6f 20 61 20 64 69 66 66 65 72  [p2] to a differ
1ca4e 65 6e 74 20 74 79 70 65 2e 0a 20 20 20 20 2a 2f  ent type..    */
1ca4f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
1ca50 2d 3e 6f 70 66 6c 61 67 73 3d 3d 73 71 6c 69 74  ->opflags==sqlit
1ca51 65 33 4f 70 63 6f 64 65 50 72 6f 70 65 72 74 79  e3OpcodeProperty
1ca52 5b 70 4f 70 2d 3e 6f 70 63 6f 64 65 5d 20 29 3b  [pOp->opcode] );
1ca53 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  .    if( pOp->op
1ca54 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f 4f 55  flags & OPFLG_OU
1ca55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 20 29 7b  T2_PRERELEASE ){
1ca56 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1ca57 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20  Op->p2>0 );.    
1ca58 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1ca59 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  2<=p->nMem );.  
1ca5a 20 20 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d      pOut = &aMem
1ca5b 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20  [pOp->p2];.     
1ca5c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1ca5d 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70  eleaseExternal(p
1ca5e 4f 75 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74  Out);.      pOut
1ca5f 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1ca60 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  t;.    }..    /*
1ca61 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67   Sanity checking
1ca62 20 6f 6e 20 6f 74 68 65 72 20 6f 70 65 72 61 6e   on other operan
1ca63 64 73 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ds */.#ifdef SQL
1ca64 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66  ITE_DEBUG.    if
1ca65 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20  ( (pOp->opflags 
1ca66 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 3d 30 20  & OPFLG_IN1)!=0 
1ca67 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
1ca68 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20   pOp->p1>0 );.  
1ca69 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1ca6a 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p1<=p->nMem );.
1ca6b 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54        REGISTER_T
1ca6c 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 26 61  RACE(pOp->p1, &a
1ca6d 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 29 3b 0a 20  Mem[pOp->p1]);. 
1ca6e 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 70 4f     }.    if( (pO
1ca6f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20 4f 50 46  p->opflags & OPF
1ca70 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20  LG_IN2)!=0 ){.  
1ca71 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1ca72 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61  >p2>0 );.      a
1ca73 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d  ssert( pOp->p2<=
1ca74 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  p->nMem );.     
1ca75 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1ca76 70 4f 70 2d 3e 70 32 2c 20 26 61 4d 65 6d 5b 70  pOp->p2, &aMem[p
1ca77 4f 70 2d 3e 70 32 5d 29 3b 0a 20 20 20 20 7d 0a  Op->p2]);.    }.
1ca78 20 20 20 20 69 66 28 20 28 70 4f 70 2d 3e 6f 70      if( (pOp->op
1ca79 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f 49 4e  flags & OPFLG_IN
1ca7a 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61  3)!=0 ){.      a
1ca7b 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30  ssert( pOp->p3>0
1ca7c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1ca7d 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  ( pOp->p3<=p->nM
1ca7e 65 6d 20 29 3b 0a 20 20 20 20 20 20 52 45 47 49  em );.      REGI
1ca7f 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1ca80 70 33 2c 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  p3, &aMem[pOp->p
1ca81 33 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  3]);.    }.    i
1ca82 66 28 20 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73  f( (pOp->opflags
1ca83 20 26 20 4f 50 46 4c 47 5f 4f 55 54 32 29 21 3d   & OPFLG_OUT2)!=
1ca84 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
1ca85 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a  t( pOp->p2>0 );.
1ca86 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1ca87 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p2<=p->nMem )
1ca88 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1ca89 28 70 4f 70 2d 3e 6f 70 66 6c 61 67 73 20 26 20  (pOp->opflags & 
1ca8a 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d 30 20 29  OPFLG_OUT3)!=0 )
1ca8b 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1ca8c 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 20  pOp->p3>0 );.   
1ca8d 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1ca8e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p3<=p->nMem );. 
1ca8f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 0a 20     }.#endif.  . 
1ca90 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e     switch( pOp->
1ca91 6f 70 63 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a  opcode ){../****
1ca92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ca93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ca94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ca95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ca96 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61  *********.** Wha
1ca97 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d  t follows is a m
1ca98 61 73 73 69 76 65 20 73 77 69 74 63 68 20 73 74  assive switch st
1ca99 61 74 65 6d 65 6e 74 20 77 68 65 72 65 20 65 61  atement where ea
1ca9a 63 68 20 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e  ch case implemen
1ca9b 74 73 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65  ts a.** separate
1ca9c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20   instruction in 
1ca9d 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
1ca9e 69 6e 65 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c  ine.  If we foll
1ca9f 6f 77 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20  ow the usual.** 
1caa0 69 6e 64 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76  indentation conv
1caa1 65 6e 74 69 6f 6e 73 2c 20 65 61 63 68 20 63 61  entions, each ca
1caa2 73 65 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 64  se should be ind
1caa3 65 6e 74 65 64 20 62 79 20 36 20 73 70 61 63 65  ented by 6 space
1caa4 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20  s.  But.** that 
1caa5 69 73 20 61 20 6c 6f 74 20 6f 66 20 77 61 73 74  is a lot of wast
1caa6 65 64 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20  ed space on the 
1caa7 6c 65 66 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f  left margin.  So
1caa8 20 74 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e   the code within
1caa9 0a 2a 2a 20 74 68 65 20 73 77 69 74 63 68 20 73  .** the switch s
1caaa 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72  tatement will br
1caab 65 61 6b 20 77 69 74 68 20 63 6f 6e 76 65 6e 74  eak with convent
1caac 69 6f 6e 20 61 6e 64 20 62 65 20 66 6c 75 73 68  ion and be flush
1caad 2d 6c 65 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a  -left. Another.*
1caae 2a 20 62 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73  * big comment (s
1caaf 69 6d 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f  imilar to this o
1cab0 6e 65 29 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68  ne) will mark th
1cab1 65 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63  e point in the c
1cab2 6f 64 65 20 77 68 65 72 65 0a 2a 2a 20 77 65 20  ode where.** we 
1cab3 74 72 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20  transition back 
1cab4 74 6f 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74  to normal indent
1cab5 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ation..**.** The
1cab6 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65   formatting of e
1cab7 61 63 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f  ach case is impo
1cab8 72 74 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65  rtant.  The make
1cab9 66 69 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0a  file for SQLite.
1caba 2a 2a 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f  ** generates two
1cabb 20 43 20 66 69 6c 65 73 20 22 6f 70 63 6f 64 65   C files "opcode
1cabc 73 2e 68 22 20 61 6e 64 20 22 6f 70 63 6f 64 65  s.h" and "opcode
1cabd 73 2e 63 22 20 62 79 20 73 63 61 6e 6e 69 6e 67  s.c" by scanning
1cabe 20 74 68 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f   this.** file lo
1cabf 6f 6b 69 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20  oking for lines 
1cac0 74 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20  that begin with 
1cac1 22 63 61 73 65 20 4f 50 5f 22 2e 20 20 54 68 65  "case OP_".  The
1cac2 20 6f 70 63 6f 64 65 73 2e 68 20 66 69 6c 65 73   opcodes.h files
1cac3 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c  .** will be fill
1cac4 65 64 20 77 69 74 68 20 23 64 65 66 69 6e 65 73  ed with #defines
1cac5 20 74 68 61 74 20 67 69 76 65 20 75 6e 69 71 75   that give uniqu
1cac6 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73  e integer values
1cac7 20 74 6f 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f   to each.** opco
1cac8 64 65 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64  de and the opcod
1cac9 65 73 2e 63 20 66 69 6c 65 20 69 73 20 66 69 6c  es.c file is fil
1caca 6c 65 64 20 77 69 74 68 20 61 6e 20 61 72 72 61  led with an arra
1cacb 79 20 6f 66 20 73 74 72 69 6e 67 73 20 77 68 65  y of strings whe
1cacc 72 65 0a 2a 2a 20 65 61 63 68 20 73 74 72 69 6e  re.** each strin
1cacd 67 20 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 69  g is the symboli
1cace 63 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63  c name for the c
1cacf 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63  orresponding opc
1cad0 6f 64 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20  ode.  If the.** 
1cad1 63 61 73 65 20 73 74 61 74 65 6d 65 6e 74 20 69  case statement i
1cad2 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20  s followed by a 
1cad3 63 6f 6d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66  comment of the f
1cad4 6f 72 6d 20 22 2f 23 20 73 61 6d 65 20 61 73 20  orm "/# same as 
1cad5 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20  ... #/".** that 
1cad6 63 6f 6d 6d 65 6e 74 20 69 73 20 75 73 65 64 20  comment is used 
1cad7 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  to determine the
1cad8 20 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75   particular valu
1cad9 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e  e of the opcode.
1cada 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 20 6b 65 79  .**.** Other key
1cadb 77 6f 72 64 73 20 69 6e 20 74 68 65 20 63 6f 6d  words in the com
1cadc 6d 65 6e 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77  ment that follow
1cadd 73 20 65 61 63 68 20 63 61 73 65 20 61 72 65 20  s each case are 
1cade 75 73 65 64 20 74 6f 0a 2a 2a 20 63 6f 6e 73 74  used to.** const
1cadf 72 75 63 74 20 74 68 65 20 4f 50 46 4c 47 5f 49  ruct the OPFLG_I
1cae0 4e 49 54 49 41 4c 49 5a 45 52 20 76 61 6c 75 65  NITIALIZER value
1cae1 20 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65   that initialize
1cae2 73 20 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79  s opcodeProperty
1cae3 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 6f 72 64 73 20  []..** Keywords 
1cae4 69 6e 63 6c 75 64 65 3a 20 69 6e 31 2c 20 69 6e  include: in1, in
1cae5 32 2c 20 69 6e 33 2c 20 6f 75 74 32 5f 70 72 65  2, in3, out2_pre
1cae6 72 65 6c 65 61 73 65 2c 20 6f 75 74 32 2c 20 6f  release, out2, o
1cae7 75 74 33 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65  ut3.  See.** the
1cae8 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73   mkopcodeh.awk s
1cae9 63 72 69 70 74 20 66 6f 72 20 61 64 64 69 74 69  cript for additi
1caea 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1caeb 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74  ..**.** Document
1caec 61 74 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45  ation about VDBE
1caed 20 6f 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65   opcodes is gene
1caee 72 61 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e  rated by scannin
1caef 67 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 66  g this file.** f
1caf0 6f 72 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74  or lines of that
1caf1 20 63 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65   contain "Opcode
1caf2 3a 22 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61  :".  That line a
1caf3 6e 64 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e  nd all subsequen
1caf4 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e  t.** comment lin
1caf5 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74  es are used in t
1caf6 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66  he generation of
1caf7 20 74 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c   the opcode.html
1caf8 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a   documentation.*
1caf9 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 55  * file..**.** SU
1cafa 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  MMARY:.**.**    
1cafb 20 46 6f 72 6d 61 74 74 69 6e 67 20 69 73 20 69   Formatting is i
1cafc 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73 63 72 69  mportant to scri
1cafd 70 74 73 20 74 68 61 74 20 73 63 61 6e 20 74 68  pts that scan th
1cafe 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20  is file..**     
1caff 44 6f 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66  Do not deviate f
1cb00 72 6f 6d 20 74 68 65 20 66 6f 72 6d 61 74 74 69  rom the formatti
1cb01 6e 67 20 73 74 79 6c 65 20 63 75 72 72 65 6e 74  ng style current
1cb02 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a  ly in use..**.**
1cb03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cb04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cb05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cb06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1cb07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
1cb08 20 4f 70 63 6f 64 65 3a 20 20 47 6f 74 6f 20 2a   Opcode:  Goto *
1cb09 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1cb0a 41 6e 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c  An unconditional
1cb0b 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73   jump to address
1cb0c 20 50 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74   P2..** The next
1cb0d 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65   instruction exe
1cb0e 63 75 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a  cuted will be .*
1cb0f 2a 20 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64  * the one at ind
1cb10 65 78 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62  ex P2 from the b
1cb11 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74  eginning of.** t
1cb12 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63  he program..*/.c
1cb13 61 73 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20  ase OP_Goto: {  
1cb14 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75             /* ju
1cb15 6d 70 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f  mp */.  CHECK_FO
1cb16 52 5f 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70  R_INTERRUPT;.  p
1cb17 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1cb18 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1cb19 4f 70 63 6f 64 65 3a 20 20 47 6f 73 75 62 20 50  Opcode:  Gosub P
1cb1a 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1cb1b 20 57 72 69 74 65 20 74 68 65 20 63 75 72 72 65   Write the curre
1cb1c 6e 74 20 61 64 64 72 65 73 73 20 6f 6e 74 6f 20  nt address onto 
1cb1d 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 61  register P1.** a
1cb1e 6e 64 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  nd then jump to 
1cb1f 61 64 64 72 65 73 73 20 50 32 2e 0a 2a 2f 0a 63  address P2..*/.c
1cb20 61 73 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20  ase OP_Gosub: { 
1cb21 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75             /* ju
1cb22 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 6e  mp, in1 */.  pIn
1cb23 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  1 = &aMem[pOp->p
1cb24 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  1];.  assert( (p
1cb25 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1cb26 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 49  _Dyn)==0 );.  pI
1cb27 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  n1->flags = MEM_
1cb28 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69  Int;.  pIn1->u.i
1cb29 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54 45   = pc;.  REGISTE
1cb2a 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c  R_TRACE(pOp->p1,
1cb2b 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20 70   pIn1);.  pc = p
1cb2c 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 62 72  Op->p2 - 1;.  br
1cb2d 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1cb2e 65 3a 20 20 52 65 74 75 72 6e 20 50 31 20 2a 20  e:  Return P1 * 
1cb2f 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  * * *.**.** Jump
1cb30 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73   to the next ins
1cb31 74 72 75 63 74 69 6f 6e 20 61 66 74 65 72 20 74  truction after t
1cb32 68 65 20 61 64 64 72 65 73 73 20 69 6e 20 72 65  he address in re
1cb33 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a 63 61  gister P1..*/.ca
1cb34 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20 7b 20  se OP_Return: { 
1cb35 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31            /* in1
1cb36 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d   */.  pIn1 = &aM
1cb37 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  em[pOp->p1];.  a
1cb38 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61  ssert( pIn1->fla
1cb39 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a  gs & MEM_Int );.
1cb3a 20 20 70 63 20 3d 20 28 69 6e 74 29 70 49 6e 31    pc = (int)pIn1
1cb3b 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61 6b 3b 0a  ->u.i;.  break;.
1cb3c 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 59  }../* Opcode:  Y
1cb3d 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a 20 2a 0a  ield P1 * * * *.
1cb3e 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68 65 20 70  **.** Swap the p
1cb3f 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 77  rogram counter w
1cb40 69 74 68 20 74 68 65 20 76 61 6c 75 65 20 69 6e  ith the value in
1cb41 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f   register P1..*/
1cb42 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c 64 3a 20  .case OP_Yield: 
1cb43 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1cb44 69 6e 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  in1 */.#if 0  /*
1cb45 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1cb46 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 61   moved into u.aa
1cb47 20 2a 2f 0a 20 20 69 6e 74 20 70 63 44 65 73 74   */.  int pcDest
1cb48 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1cb49 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1cb4a 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a 20  d into u.aa */. 
1cb4b 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f   pIn1 = &aMem[pO
1cb4c 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1cb4d 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1cb4e 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a   MEM_Dyn)==0 );.
1cb4f 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20    pIn1->flags = 
1cb50 4d 45 4d 5f 49 6e 74 3b 0a 20 20 75 2e 61 61 2e  MEM_Int;.  u.aa.
1cb51 70 63 44 65 73 74 20 3d 20 28 69 6e 74 29 70 49  pcDest = (int)pI
1cb52 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 6e 31 2d  n1->u.i;.  pIn1-
1cb53 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47  >u.i = pc;.  REG
1cb54 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1cb55 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63  >p1, pIn1);.  pc
1cb56 20 3d 20 75 2e 61 61 2e 70 63 44 65 73 74 3b 0a   = u.aa.pcDest;.
1cb57 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1cb58 70 63 6f 64 65 3a 20 20 48 61 6c 74 49 66 4e 75  pcode:  HaltIfNu
1cb59 6c 6c 20 20 50 31 20 50 32 20 50 33 20 50 34 20  ll  P1 P2 P3 P4 
1cb5a 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  *.**.** Check th
1cb5b 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1cb5c 74 65 72 20 50 33 2e 20 20 49 66 20 69 73 20 69  ter P3.  If is i
1cb5d 73 20 4e 55 4c 4c 20 74 68 65 6e 20 48 61 6c 74  s NULL then Halt
1cb5e 20 75 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65   using.** parame
1cb5f 74 65 72 20 50 31 2c 20 50 32 2c 20 61 6e 64 20  ter P1, P2, and 
1cb60 50 34 20 61 73 20 69 66 20 74 68 69 73 20 77 65  P4 as if this we
1cb61 72 65 20 61 20 48 61 6c 74 20 69 6e 73 74 72 75  re a Halt instru
1cb62 63 74 69 6f 6e 2e 20 20 49 66 20 74 68 65 0a 2a  ction.  If the.*
1cb63 2a 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  * value in regis
1cb64 74 65 72 20 50 33 20 69 73 20 6e 6f 74 20 4e 55  ter P3 is not NU
1cb65 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  LL, then this ro
1cb66 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
1cb67 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 48 61 6c  ..*/.case OP_Hal
1cb68 74 49 66 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20  tIfNull: {      
1cb69 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 70 49 6e 33  /* in3 */.  pIn3
1cb6a 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   = &aMem[pOp->p3
1cb6b 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e 33 2d 3e  ];.  if( (pIn3->
1cb6c 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1cb6d 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  )==0 ) break;.  
1cb6e 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20  /* Fall through 
1cb6f 69 6e 74 6f 20 4f 50 5f 48 61 6c 74 20 2a 2f 0a  into OP_Halt */.
1cb70 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48  }../* Opcode:  H
1cb71 61 6c 74 20 50 31 20 50 32 20 2a 20 50 34 20 2a  alt P1 P2 * P4 *
1cb72 0a 2a 2a 0a 2a 2a 20 45 78 69 74 20 69 6d 6d 65  .**.** Exit imme
1cb73 64 69 61 74 65 6c 79 2e 20 20 41 6c 6c 20 6f 70  diately.  All op
1cb74 65 6e 20 63 75 72 73 6f 72 73 2c 20 65 74 63 20  en cursors, etc 
1cb75 61 72 65 20 63 6c 6f 73 65 64 0a 2a 2a 20 61 75  are closed.** au
1cb76 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a  tomatically..**.
1cb77 2a 2a 20 50 31 20 69 73 20 74 68 65 20 72 65 73  ** P1 is the res
1cb78 75 6c 74 20 63 6f 64 65 20 72 65 74 75 72 6e 65  ult code returne
1cb79 64 20 62 79 20 73 71 6c 69 74 65 33 5f 65 78 65  d by sqlite3_exe
1cb7a 63 28 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73  c(), sqlite3_res
1cb7b 65 74 28 29 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69  et(),.** or sqli
1cb7c 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 2e 20  te3_finalize(). 
1cb7d 20 46 6f 72 20 61 20 6e 6f 72 6d 61 6c 20 68 61   For a normal ha
1cb7e 6c 74 2c 20 74 68 69 73 20 73 68 6f 75 6c 64 20  lt, this should 
1cb7f 62 65 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29  be SQLITE_OK (0)
1cb80 2e 0a 2a 2a 20 46 6f 72 20 65 72 72 6f 72 73 2c  ..** For errors,
1cb81 20 69 74 20 63 61 6e 20 62 65 20 73 6f 6d 65 20   it can be some 
1cb82 6f 74 68 65 72 20 76 61 6c 75 65 2e 20 20 49 66  other value.  If
1cb83 20 50 31 21 3d 30 20 74 68 65 6e 20 50 32 20 77   P1!=0 then P2 w
1cb84 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a  ill determine.**
1cb85 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1cb86 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20  to rollback the 
1cb87 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
1cb88 69 6f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72 6f 6c  ion.  Do not rol
1cb89 6c 62 61 63 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d  lback.** if P2==
1cb8a 4f 45 5f 46 61 69 6c 2e 20 44 6f 20 74 68 65 20  OE_Fail. Do the 
1cb8b 72 6f 6c 6c 62 61 63 6b 20 69 66 20 50 32 3d 3d  rollback if P2==
1cb8c 4f 45 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20 49 66  OE_Rollback.  If
1cb8d 20 50 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a   P2==OE_Abort,.*
1cb8e 2a 20 74 68 65 6e 20 62 61 63 6b 20 6f 75 74 20  * then back out 
1cb8f 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 68 61 74  all changes that
1cb90 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 64   have occurred d
1cb91 75 72 69 6e 67 20 74 68 69 73 20 65 78 65 63 75  uring this execu
1cb92 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 56  tion of the.** V
1cb93 44 42 45 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20  DBE, but do not 
1cb94 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61  rollback the tra
1cb95 6e 73 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a  nsaction. .**.**
1cb96 20 49 66 20 50 34 20 69 73 20 6e 6f 74 20 6e 75   If P4 is not nu
1cb97 6c 6c 20 74 68 65 6e 20 69 74 20 69 73 20 61 6e  ll then it is an
1cb98 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73   error message s
1cb99 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tring..**.** The
1cb9a 72 65 20 69 73 20 61 6e 20 69 6d 70 6c 69 65 64  re is an implied
1cb9b 20 22 48 61 6c 74 20 30 20 30 20 30 22 20 69 6e   "Halt 0 0 0" in
1cb9c 73 74 72 75 63 74 69 6f 6e 20 69 6e 73 65 72 74  struction insert
1cb9d 65 64 20 61 74 20 74 68 65 20 76 65 72 79 20 65  ed at the very e
1cb9e 6e 64 20 6f 66 0a 2a 2a 20 65 76 65 72 79 20 70  nd of.** every p
1cb9f 72 6f 67 72 61 6d 2e 20 20 53 6f 20 61 20 6a 75  rogram.  So a ju
1cba0 6d 70 20 70 61 73 74 20 74 68 65 20 6c 61 73 74  mp past the last
1cba1 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20   instruction of 
1cba2 74 68 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69  the program.** i
1cba3 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 65 78  s the same as ex
1cba4 65 63 75 74 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f  ecuting Halt..*/
1cba5 0a 63 61 73 65 20 4f 50 5f 48 61 6c 74 3a 20 7b  .case OP_Halt: {
1cba6 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d  .  if( pOp->p1==
1cba7 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 2d 3e  SQLITE_OK && p->
1cba8 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a  pFrame ){.    /*
1cba9 20 48 61 6c 74 20 74 68 65 20 73 75 62 2d 70 72   Halt the sub-pr
1cbaa 6f 67 72 61 6d 2e 20 52 65 74 75 72 6e 20 63 6f  ogram. Return co
1cbab 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 70 61 72  ntrol to the par
1cbac 65 6e 74 20 66 72 61 6d 65 2e 20 2a 2f 0a 20 20  ent frame. */.  
1cbad 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72    VdbeFrame *pFr
1cbae 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b  ame = p->pFrame;
1cbaf 0a 20 20 20 20 70 2d 3e 70 46 72 61 6d 65 20 3d  .    p->pFrame =
1cbb0 20 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74   pFrame->pParent
1cbb1 3b 0a 20 20 20 20 70 2d 3e 6e 46 72 61 6d 65 2d  ;.    p->nFrame-
1cbb2 2d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  -;.    sqlite3Vd
1cbb3 62 65 53 65 74 43 68 61 6e 67 65 73 28 64 62 2c  beSetChanges(db,
1cbb4 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b 0a 20 20   p->nChange);.  
1cbb5 20 20 70 63 20 3d 20 73 71 6c 69 74 65 33 56 64    pc = sqlite3Vd
1cbb6 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70  beFrameRestore(p
1cbb7 46 72 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  Frame);.    if( 
1cbb8 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 49 67 6e 6f  pOp->p2==OE_Igno
1cbb9 72 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  re ){.      /* I
1cbba 6e 73 74 72 75 63 74 69 6f 6e 20 70 63 20 69 73  nstruction pc is
1cbbb 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20   the OP_Program 
1cbbc 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65  that invoked the
1cbbd 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 0a 20 20   sub-program .  
1cbbe 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 6c 79      ** currently
1cbbf 20 62 65 69 6e 67 20 68 61 6c 74 65 64 2e 20 49   being halted. I
1cbc0 66 20 74 68 65 20 70 32 20 69 6e 73 74 72 75 63  f the p2 instruc
1cbc1 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 4f 50 5f  tion of this OP_
1cbc2 48 61 6c 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e  Halt.      ** in
1cbc3 73 74 72 75 63 74 69 6f 6e 20 69 73 20 73 65 74  struction is set
1cbc4 20 74 6f 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 74   to OE_Ignore, t
1cbc5 68 65 6e 20 74 68 65 20 73 75 62 2d 70 72 6f 67  hen the sub-prog
1cbc6 72 61 6d 20 69 73 20 74 68 72 6f 77 69 6e 67 0a  ram is throwing.
1cbc7 20 20 20 20 20 20 2a 2a 20 61 6e 20 49 47 4e 4f        ** an IGNO
1cbc8 52 45 20 65 78 63 65 70 74 69 6f 6e 2e 20 49 6e  RE exception. In
1cbc9 20 74 68 69 73 20 63 61 73 65 20 6a 75 6d 70 20   this case jump 
1cbca 74 6f 20 74 68 65 20 61 64 64 72 65 73 73 20 73  to the address s
1cbcb 70 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 2a  pecified.      *
1cbcc 2a 20 61 73 20 74 68 65 20 70 32 20 6f 66 20 74  * as the p2 of t
1cbcd 68 65 20 63 61 6c 6c 69 6e 67 20 4f 50 5f 50 72  he calling OP_Pr
1cbce 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 20 20 20  ogram.  */.     
1cbcf 20 70 63 20 3d 20 70 2d 3e 61 4f 70 5b 70 63 5d   pc = p->aOp[pc]
1cbd0 2e 70 32 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20  .p2-1;.    }.   
1cbd1 20 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20   aOp = p->aOp;. 
1cbd2 20 20 20 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65     aMem = p->aMe
1cbd3 6d 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  m;.    break;.  
1cbd4 7d 0a 0a 20 20 70 2d 3e 72 63 20 3d 20 70 4f 70  }..  p->rc = pOp
1cbd5 2d 3e 70 31 3b 0a 20 20 70 2d 3e 65 72 72 6f 72  ->p1;.  p->error
1cbd6 41 63 74 69 6f 6e 20 3d 20 28 75 38 29 70 4f 70  Action = (u8)pOp
1cbd7 2d 3e 70 32 3b 0a 20 20 70 2d 3e 70 63 20 3d 20  ->p2;.  p->pc = 
1cbd8 70 63 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70  pc;.  if( pOp->p
1cbd9 34 2e 7a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  4.z ){.    sqlit
1cbda 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1cbdb 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73  zErrMsg, db, "%s
1cbdc 22 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  ", pOp->p4.z);. 
1cbdd 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65   }.  rc = sqlite
1cbde 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20  3VdbeHalt(p);.  
1cbdf 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1cbe0 54 45 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53  TE_BUSY || rc==S
1cbe1 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d 3d  QLITE_OK || rc==
1cbe2 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a  SQLITE_ERROR );.
1cbe3 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1cbe4 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 70 2d 3e  _BUSY ){.    p->
1cbe5 72 63 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45  rc = rc = SQLITE
1cbe6 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a  _BUSY;.  }else{.
1cbe7 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
1cbe8 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e  SQLITE_OK || p->
1cbe9 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54  rc==SQLITE_CONST
1cbea 52 41 49 4e 54 20 29 3b 0a 20 20 20 20 61 73 73  RAINT );.    ass
1cbeb 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
1cbec 4f 4b 20 7c 7c 20 64 62 2d 3e 6e 44 65 66 65 72  OK || db->nDefer
1cbed 72 65 64 43 6f 6e 73 3e 30 20 29 3b 0a 20 20 20  redCons>0 );.   
1cbee 20 72 63 20 3d 20 70 2d 3e 72 63 20 3f 20 53 51   rc = p->rc ? SQ
1cbef 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 53 51 4c  LITE_ERROR : SQL
1cbf0 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20  ITE_DONE;.  }.  
1cbf1 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1cbf2 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1cbf3 49 6e 74 65 67 65 72 20 50 31 20 50 32 20 2a 20  Integer P1 P2 * 
1cbf4 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 33 32  * *.**.** The 32
1cbf5 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76 61 6c  -bit integer val
1cbf6 75 65 20 50 31 20 69 73 20 77 72 69 74 74 65 6e  ue P1 is written
1cbf7 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1cbf8 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e  2..*/.case OP_In
1cbf9 74 65 67 65 72 3a 20 7b 20 20 20 20 20 20 20 20  teger: {        
1cbfa 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65   /* out2-prerele
1cbfb 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e 75  ase */.  pOut->u
1cbfc 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  .i = pOp->p1;.  
1cbfd 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1cbfe 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 50 32 20  ode: Int64 * P2 
1cbff 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1cc00 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1cc01 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  a 64-bit integer
1cc02 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65   value..** Write
1cc03 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f   that value into
1cc04 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f   register P2..*/
1cc05 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 34 3a 20  .case OP_Int64: 
1cc06 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f  {           /* o
1cc07 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1cc08 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
1cc09 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 3b 0a 20  >p4.pI64!=0 );. 
1cc0a 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 2a 70 4f   pOut->u.i = *pO
1cc0b 70 2d 3e 70 34 2e 70 49 36 34 3b 0a 20 20 62 72  p->p4.pI64;.  br
1cc0c 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1cc0d 65 3a 20 52 65 61 6c 20 2a 20 50 32 20 2a 20 50  e: Real * P2 * P
1cc0e 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20  4 *.**.** P4 is 
1cc0f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 36  a pointer to a 6
1cc10 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67 20 70  4-bit floating p
1cc11 6f 69 6e 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 57  oint value..** W
1cc12 72 69 74 65 20 74 68 61 74 20 76 61 6c 75 65 20  rite that value 
1cc13 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  into register P2
1cc14 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 61  ..*/.case OP_Rea
1cc15 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  l: {            
1cc16 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 46 4c  /* same as TK_FL
1cc17 4f 41 54 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c  OAT, out2-prerel
1cc18 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d 3e  ease */.  pOut->
1cc19 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61 6c  flags = MEM_Real
1cc1a 3b 0a 20 20 61 73 73 65 72 74 28 20 21 73 71 6c  ;.  assert( !sql
1cc1b 69 74 65 33 49 73 4e 61 4e 28 2a 70 4f 70 2d 3e  ite3IsNaN(*pOp->
1cc1c 70 34 2e 70 52 65 61 6c 29 20 29 3b 0a 20 20 70  p4.pReal) );.  p
1cc1d 4f 75 74 2d 3e 72 20 3d 20 2a 70 4f 70 2d 3e 70  Out->r = *pOp->p
1cc1e 34 2e 70 52 65 61 6c 3b 0a 20 20 62 72 65 61 6b  4.pReal;.  break
1cc1f 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1cc20 53 74 72 69 6e 67 38 20 2a 20 50 32 20 2a 20 50  String8 * P2 * P
1cc21 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69  4 *.**.** P4 poi
1cc22 6e 74 73 20 74 6f 20 61 20 6e 75 6c 20 74 65 72  nts to a nul ter
1cc23 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
1cc24 72 69 6e 67 2e 20 54 68 69 73 20 6f 70 63 6f 64  ring. This opcod
1cc25 65 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64  e is transformed
1cc26 20 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f   .** into an OP_
1cc27 53 74 72 69 6e 67 20 62 65 66 6f 72 65 20 69 74  String before it
1cc28 20 69 73 20 65 78 65 63 75 74 65 64 20 66 6f 72   is executed for
1cc29 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 2e   the first time.
1cc2a 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69  .*/.case OP_Stri
1cc2b 6e 67 38 3a 20 7b 20 20 20 20 20 20 20 20 20 2f  ng8: {         /
1cc2c 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52  * same as TK_STR
1cc2d 49 4e 47 2c 20 6f 75 74 32 2d 70 72 65 72 65 6c  ING, out2-prerel
1cc2e 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74  ease */.  assert
1cc2f 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29  ( pOp->p4.z!=0 )
1cc30 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20  ;.  pOp->opcode 
1cc31 3d 20 4f 50 5f 53 74 72 69 6e 67 3b 0a 20 20 70  = OP_String;.  p
1cc32 4f 70 2d 3e 70 31 20 3d 20 73 71 6c 69 74 65 33  Op->p1 = sqlite3
1cc33 53 74 72 6c 65 6e 33 30 28 70 4f 70 2d 3e 70 34  Strlen30(pOp->p4
1cc34 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  .z);..#ifndef SQ
1cc35 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
1cc36 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 67 21 3d    if( encoding!=
1cc37 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20  SQLITE_UTF8 ){. 
1cc38 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1cc39 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4f 75  dbeMemSetStr(pOu
1cc3a 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 2d 31  t, pOp->p4.z, -1
1cc3b 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
1cc3c 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
1cc3d 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1cc3e 45 5f 54 4f 4f 42 49 47 20 29 20 67 6f 74 6f 20  E_TOOBIG ) goto 
1cc3f 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 69 66 28  too_big;.    if(
1cc40 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69   SQLITE_OK!=sqli
1cc41 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63  te3VdbeChangeEnc
1cc42 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 6e 63 6f  oding(pOut, enco
1cc43 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 6e 6f 5f  ding) ) goto no_
1cc44 6d 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  mem;.    assert(
1cc45 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d   pOut->zMalloc==
1cc46 70 4f 75 74 2d 3e 7a 20 29 3b 0a 20 20 20 20 61  pOut->z );.    a
1cc47 73 73 65 72 74 28 20 70 4f 75 74 2d 3e 66 6c 61  ssert( pOut->fla
1cc48 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 3b 0a  gs & MEM_Dyn );.
1cc49 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f      pOut->zMallo
1cc4a 63 20 3d 20 30 3b 0a 20 20 20 20 70 4f 75 74 2d  c = 0;.    pOut-
1cc4b 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74  >flags |= MEM_St
1cc4c 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  atic;.    pOut->
1cc4d 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 44 79  flags &= ~MEM_Dy
1cc4e 6e 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  n;.    if( pOp->
1cc4f 70 34 74 79 70 65 3d 3d 50 34 5f 44 59 4e 41 4d  p4type==P4_DYNAM
1cc50 49 43 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  IC ){.      sqli
1cc51 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f  te3DbFree(db, pO
1cc52 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d 0a  p->p4.z);.    }.
1cc53 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
1cc54 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20  = P4_DYNAMIC;.  
1cc55 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 70 4f    pOp->p4.z = pO
1cc56 75 74 2d 3e 7a 3b 0a 20 20 20 20 70 4f 70 2d 3e  ut->z;.    pOp->
1cc57 70 31 20 3d 20 70 4f 75 74 2d 3e 6e 3b 0a 20 20  p1 = pOut->n;.  
1cc58 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  }.#endif.  if( p
1cc59 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d 69  Op->p1>db->aLimi
1cc5a 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1cc5b 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f  ENGTH] ){.    go
1cc5c 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a  to too_big;.  }.
1cc5d 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67    /* Fall throug
1cc5e 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61  h to the next ca
1cc5f 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 20 2a 2f  se, OP_String */
1cc60 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64 65 3a  .}.  ./* Opcode:
1cc61 20 53 74 72 69 6e 67 20 50 31 20 50 32 20 2a 20   String P1 P2 * 
1cc62 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  P4 *.**.** The s
1cc63 74 72 69 6e 67 20 76 61 6c 75 65 20 50 34 20 6f  tring value P4 o
1cc64 66 20 6c 65 6e 67 74 68 20 50 31 20 28 62 79 74  f length P1 (byt
1cc65 65 73 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e  es) is stored in
1cc66 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f   register P2..*/
1cc67 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e 67 3a  .case OP_String:
1cc68 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f   {          /* o
1cc69 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1cc6a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
1cc6b 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f  >p4.z!=0 );.  pO
1cc6c 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ut->flags = MEM_
1cc6d 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d  Str|MEM_Static|M
1cc6e 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d  EM_Term;.  pOut-
1cc6f 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a  >z = pOp->p4.z;.
1cc70 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f 70 2d    pOut->n = pOp-
1cc71 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63  >p1;.  pOut->enc
1cc72 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55   = encoding;.  U
1cc73 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1cc74 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61  ZE(pOut);.  brea
1cc75 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1cc76 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 2a 20 2a   Null * P2 * * *
1cc77 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 4e  .**.** Write a N
1cc78 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ULL into registe
1cc79 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  r P2..*/.case OP
1cc7a 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20  _Null: {        
1cc7b 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1cc7c 6c 65 61 73 65 20 2a 2f 0a 20 20 70 4f 75 74 2d  lease */.  pOut-
1cc7d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
1cc7e 6c 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a  l;.  break;.}...
1cc7f 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 6c 6f 62 20  /* Opcode: Blob 
1cc80 50 31 20 50 32 20 2a 20 50 34 0a 2a 2a 0a 2a 2a  P1 P2 * P4.**.**
1cc81 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20   P4 points to a 
1cc82 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 50 31 20  blob of data P1 
1cc83 62 79 74 65 73 20 6c 6f 6e 67 2e 20 20 53 74 6f  bytes long.  Sto
1cc84 72 65 20 74 68 69 73 0a 2a 2a 20 62 6c 6f 62 20  re this.** blob 
1cc85 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 20  in register P2. 
1cc86 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1cc87 20 69 73 20 6e 6f 74 20 63 6f 64 65 64 20 64 69   is not coded di
1cc88 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20 74 68 65  rectly.** by the
1cc89 20 63 6f 6d 70 69 6c 65 72 2e 20 49 6e 73 74 65   compiler. Inste
1cc8a 61 64 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72  ad, the compiler
1cc8b 20 6c 61 79 65 72 20 73 70 65 63 69 66 69 65 73   layer specifies
1cc8c 0a 2a 2a 20 61 6e 20 4f 50 5f 48 65 78 42 6c 6f  .** an OP_HexBlo
1cc8d 62 20 6f 70 63 6f 64 65 2c 20 77 69 74 68 20 74  b opcode, with t
1cc8e 68 65 20 68 65 78 20 73 74 72 69 6e 67 20 72 65  he hex string re
1cc8f 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 0a  presentation of.
1cc90 2a 2a 20 74 68 65 20 62 6c 6f 62 20 61 73 20 50  ** the blob as P
1cc91 34 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  4. This opcode i
1cc92 73 20 74 72 61 6e 73 66 6f 72 6d 65 64 20 74 6f  s transformed to
1cc93 20 61 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a 20 74   an OP_Blob.** t
1cc94 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 74  he first time it
1cc95 20 69 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2f   is executed..*/
1cc96 0a 63 61 73 65 20 4f 50 5f 42 6c 6f 62 3a 20 7b  .case OP_Blob: {
1cc97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cc98 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
1cc99 73 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  se */.  assert( 
1cc9a 70 4f 70 2d 3e 70 31 20 3c 3d 20 53 51 4c 49 54  pOp->p1 <= SQLIT
1cc9b 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a  E_MAX_LENGTH );.
1cc9c 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cc9d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70  SetStr(pOut, pOp
1cc9e 2d 3e 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70 31 2c  ->p4.z, pOp->p1,
1cc9f 20 30 2c 20 30 29 3b 0a 20 20 70 4f 75 74 2d 3e   0, 0);.  pOut->
1cca0 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a  enc = encoding;.
1cca1 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1cca2 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62  BSIZE(pOut);.  b
1cca3 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cca4 64 65 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20  de: Variable P1 
1cca5 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  P2 P3 P4 *.**.**
1cca6 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 76 61   Transfer the va
1cca7 6c 75 65 73 20 6f 66 20 62 6f 75 6e 64 20 70 61  lues of bound pa
1cca8 72 61 6d 65 74 65 72 73 20 50 31 2e 2e 50 31 2b  rameters P1..P1+
1cca9 50 33 2d 31 20 69 6e 74 6f 20 72 65 67 69 73 74  P3-1 into regist
1ccaa 65 72 73 0a 2a 2a 20 50 32 2e 2e 50 32 2b 50 33  ers.** P2..P2+P3
1ccab 2d 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  -1..**.** If the
1ccac 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 61   parameter is na
1ccad 6d 65 64 2c 20 74 68 65 6e 20 69 74 73 20 6e 61  med, then its na
1ccae 6d 65 20 61 70 70 65 61 72 73 20 69 6e 20 50 34  me appears in P4
1ccaf 20 61 6e 64 20 50 33 3d 3d 31 2e 0a 2a 2a 20 54   and P3==1..** T
1ccb0 68 65 20 50 34 20 76 61 6c 75 65 20 69 73 20 75  he P4 value is u
1ccb1 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62  sed by sqlite3_b
1ccb2 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
1ccb3 6d 65 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  me()..*/.case OP
1ccb4 5f 56 61 72 69 61 62 6c 65 3a 20 7b 0a 23 69 66  _Variable: {.#if
1ccb5 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1ccb6 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1ccb7 6f 20 75 2e 61 62 20 2a 2f 0a 20 20 69 6e 74 20  o u.ab */.  int 
1ccb8 70 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p1;          /* 
1ccb9 56 61 72 69 61 62 6c 65 20 74 6f 20 63 6f 70 79  Variable to copy
1ccba 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70   from */.  int p
1ccbb 32 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  2;          /* R
1ccbc 65 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20  egister to copy 
1ccbd 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20  to */.  int n;  
1ccbe 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1ccbf 65 72 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66  er of values lef
1ccc0 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 4d  t to copy */.  M
1ccc1 65 6d 20 2a 70 56 61 72 3b 20 20 20 20 20 20 20  em *pVar;       
1ccc2 2f 2a 20 56 61 6c 75 65 20 62 65 69 6e 67 20 74  /* Value being t
1ccc3 72 61 6e 73 66 65 72 72 65 64 20 2a 2f 0a 23 65  ransferred */.#e
1ccc4 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1ccc5 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ccc6 74 6f 20 75 2e 61 62 20 2a 2f 0a 0a 20 20 75 2e  to u.ab */..  u.
1ccc7 61 62 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 20  ab.p1 = pOp->p1 
1ccc8 2d 20 31 3b 0a 20 20 75 2e 61 62 2e 70 32 20 3d  - 1;.  u.ab.p2 =
1ccc9 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 62   pOp->p2;.  u.ab
1ccca 2e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  .n = pOp->p3;.  
1cccb 61 73 73 65 72 74 28 20 75 2e 61 62 2e 70 31 3e  assert( u.ab.p1>
1cccc 3d 30 20 26 26 20 75 2e 61 62 2e 70 31 2b 75 2e  =0 && u.ab.p1+u.
1cccd 61 62 2e 6e 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b  ab.n<=p->nVar );
1ccce 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 62 2e  .  assert( u.ab.
1cccf 70 32 3e 3d 31 20 26 26 20 75 2e 61 62 2e 70 32  p2>=1 && u.ab.p2
1ccd0 2b 75 2e 61 62 2e 6e 2d 31 3c 3d 70 2d 3e 6e 4d  +u.ab.n-1<=p->nM
1ccd1 65 6d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  em );.  assert( 
1ccd2 70 4f 70 2d 3e 70 34 2e 7a 3d 3d 30 20 7c 7c 20  pOp->p4.z==0 || 
1ccd3 70 4f 70 2d 3e 70 33 3d 3d 31 20 7c 7c 20 70 4f  pOp->p3==1 || pO
1ccd4 70 2d 3e 70 33 3d 3d 30 20 29 3b 0a 0a 20 20 77  p->p3==0 );..  w
1ccd5 68 69 6c 65 28 20 75 2e 61 62 2e 6e 2d 2d 20 3e  hile( u.ab.n-- >
1ccd6 20 30 20 29 7b 0a 20 20 20 20 75 2e 61 62 2e 70   0 ){.    u.ab.p
1ccd7 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 75  Var = &p->aVar[u
1ccd8 2e 61 62 2e 70 31 2b 2b 5d 3b 0a 20 20 20 20 69  .ab.p1++];.    i
1ccd9 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
1ccda 6d 54 6f 6f 42 69 67 28 75 2e 61 62 2e 70 56 61  mTooBig(u.ab.pVa
1ccdb 72 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  r) ){.      goto
1ccdc 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a   too_big;.    }.
1ccdd 20 20 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d      pOut = &aMem
1ccde 5b 75 2e 61 62 2e 70 32 2b 2b 5d 3b 0a 20 20 20  [u.ab.p2++];.   
1ccdf 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1cce0 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70  eleaseExternal(p
1cce1 4f 75 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e  Out);.    pOut->
1cce2 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1cce3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
1cce4 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28  eMemShallowCopy(
1cce5 70 4f 75 74 2c 20 75 2e 61 62 2e 70 56 61 72 2c  pOut, u.ab.pVar,
1cce6 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20   MEM_Static);.  
1cce7 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1cce8 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 7d  BSIZE(pOut);.  }
1cce9 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1ccea 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20 50 31 20  Opcode: Move P1 
1cceb 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
1ccec 4d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 73 20  Move the values 
1cced 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 2e  in register P1..
1ccee 50 31 2b 50 33 2d 31 20 6f 76 65 72 20 69 6e 74  P1+P3-1 over int
1ccef 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 50  o.** registers P
1ccf0 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20 52 65 67  2..P2+P3-1.  Reg
1ccf1 69 73 74 65 72 73 20 50 31 2e 2e 50 31 2b 50 31  isters P1..P1+P1
1ccf2 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66 74 20 68  -1 are.** left h
1ccf3 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c 2e 20 20  olding a NULL.  
1ccf4 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66  It is an error f
1ccf5 6f 72 20 72 65 67 69 73 74 65 72 20 72 61 6e 67  or register rang
1ccf6 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b 50 33 2d  es.** P1..P1+P3-
1ccf7 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b 50 33 2d  1 and P2..P2+P3-
1ccf8 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e 0a 2a 2f  1 to overlap..*/
1ccf9 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 3a 20 7b  .case OP_Move: {
1ccfa 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1ccfb 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ccfc 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f 0a 20 20   into u.ac */.  
1ccfd 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20  char *zMalloc;  
1ccfe 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 72 69   /* Holding vari
1ccff 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74  able for allocat
1cd00 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69  ed memory */.  i
1cd01 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20  nt n;           
1cd02 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 67  /* Number of reg
1cd03 69 73 74 65 72 73 20 6c 65 66 74 20 74 6f 20 63  isters left to c
1cd04 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b  opy */.  int p1;
1cd05 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1cd06 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 66 72  ister to copy fr
1cd07 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b 20  om */.  int p2; 
1cd08 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
1cd09 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20  ster to copy to 
1cd0a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
1cd0b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1cd0c 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f 0a  ed into u.ac */.
1cd0d 0a 20 20 75 2e 61 63 2e 6e 20 3d 20 70 4f 70 2d  .  u.ac.n = pOp-
1cd0e 3e 70 33 3b 0a 20 20 75 2e 61 63 2e 70 31 20 3d  >p3;.  u.ac.p1 =
1cd0f 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 63   pOp->p1;.  u.ac
1cd10 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  .p2 = pOp->p2;. 
1cd11 20 61 73 73 65 72 74 28 20 75 2e 61 63 2e 6e 3e   assert( u.ac.n>
1cd12 30 20 26 26 20 75 2e 61 63 2e 70 31 3e 30 20 26  0 && u.ac.p1>0 &
1cd13 26 20 75 2e 61 63 2e 70 32 3e 30 20 29 3b 0a 20  & u.ac.p2>0 );. 
1cd14 20 61 73 73 65 72 74 28 20 75 2e 61 63 2e 70 31   assert( u.ac.p1
1cd15 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70 32  +u.ac.n<=u.ac.p2
1cd16 20 7c 7c 20 75 2e 61 63 2e 70 32 2b 75 2e 61 63   || u.ac.p2+u.ac
1cd17 2e 6e 3c 3d 75 2e 61 63 2e 70 31 20 29 3b 0a 0a  .n<=u.ac.p1 );..
1cd18 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 75    pIn1 = &aMem[u
1cd19 2e 61 63 2e 70 31 5d 3b 0a 20 20 70 4f 75 74 20  .ac.p1];.  pOut 
1cd1a 3d 20 26 61 4d 65 6d 5b 75 2e 61 63 2e 70 32 5d  = &aMem[u.ac.p2]
1cd1b 3b 0a 20 20 77 68 69 6c 65 28 20 75 2e 61 63 2e  ;.  while( u.ac.
1cd1c 6e 2d 2d 20 29 7b 0a 20 20 20 20 61 73 73 65 72  n-- ){.    asser
1cd1d 74 28 20 70 4f 75 74 3c 3d 26 61 4d 65 6d 5b 70  t( pOut<=&aMem[p
1cd1e 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20 20 61  ->nMem] );.    a
1cd1f 73 73 65 72 74 28 20 70 49 6e 31 3c 3d 26 61 4d  ssert( pIn1<=&aM
1cd20 65 6d 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20  em[p->nMem] );. 
1cd21 20 20 20 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 20     u.ac.zMalloc 
1cd22 3d 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b  = pOut->zMalloc;
1cd23 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c  .    pOut->zMall
1cd24 6f 63 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  oc = 0;.    sqli
1cd25 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70  te3VdbeMemMove(p
1cd26 4f 75 74 2c 20 70 49 6e 31 29 3b 0a 20 20 20 20  Out, pIn1);.    
1cd27 70 49 6e 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  pIn1->zMalloc = 
1cd28 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20  u.ac.zMalloc;.  
1cd29 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1cd2a 28 75 2e 61 63 2e 70 32 2b 2b 2c 20 70 4f 75 74  (u.ac.p2++, pOut
1cd2b 29 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20  );.    pIn1++;. 
1cd2c 20 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a 20     pOut++;.  }. 
1cd2d 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1cd2e 63 6f 64 65 3a 20 43 6f 70 79 20 50 31 20 50 32  code: Copy P1 P2
1cd2f 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b   * * *.**.** Mak
1cd30 65 20 61 20 63 6f 70 79 20 6f 66 20 72 65 67 69  e a copy of regi
1cd31 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67  ster P1 into reg
1cd32 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ister P2..**.** 
1cd33 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1cd34 20 6d 61 6b 65 73 20 61 20 64 65 65 70 20 63 6f   makes a deep co
1cd35 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e  py of the value.
1cd36 20 20 41 20 64 75 70 6c 69 63 61 74 65 0a 2a 2a    A duplicate.**
1cd37 20 69 73 20 6d 61 64 65 20 6f 66 20 61 6e 79 20   is made of any 
1cd38 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 63  string or blob c
1cd39 6f 6e 73 74 61 6e 74 2e 20 20 53 65 65 20 61 6c  onstant.  See al
1cd3a 73 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a  so OP_SCopy..*/.
1cd3b 63 61 73 65 20 4f 50 5f 43 6f 70 79 3a 20 7b 20  case OP_Copy: { 
1cd3c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69              /* i
1cd3d 6e 31 2c 20 6f 75 74 32 20 2a 2f 0a 20 20 70 49  n1, out2 */.  pI
1cd3e 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
1cd3f 70 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61  p1];.  pOut = &a
1cd40 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
1cd41 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49  assert( pOut!=pI
1cd42 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  n1 );.  sqlite3V
1cd43 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
1cd44 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45  y(pOut, pIn1, ME
1cd45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44 65 65 70  M_Ephem);.  Deep
1cd46 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f 75 74 29  hemeralize(pOut)
1cd47 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41  ;.  REGISTER_TRA
1cd48 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74  CE(pOp->p2, pOut
1cd49 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1cd4a 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f 70 79 20  * Opcode: SCopy 
1cd4b 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1cd4c 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c 6c 6f 77  * Make a shallow
1cd4d 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65   copy of registe
1cd4e 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74  r P1 into regist
1cd4f 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  er P2..**.** Thi
1cd50 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61  s instruction ma
1cd51 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77 20 63 6f  kes a shallow co
1cd52 70 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e  py of the value.
1cd53 20 20 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a    If the value.*
1cd54 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72  * is a string or
1cd55 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74 68 65 20   blob, then the 
1cd56 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20 61 20 70  copy is only a p
1cd57 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a  ointer to the.**
1cd58 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64 20 68 65   original and he
1cd59 6e 63 65 20 69 66 20 74 68 65 20 6f 72 69 67 69  nce if the origi
1cd5a 6e 61 6c 20 63 68 61 6e 67 65 73 20 73 6f 20 77  nal changes so w
1cd5b 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a  ill the copy..**
1cd5c 20 57 6f 72 73 65 2c 20 69 66 20 74 68 65 20 6f   Worse, if the o
1cd5d 72 69 67 69 6e 61 6c 20 69 73 20 64 65 61 6c 6c  riginal is deall
1cd5e 6f 63 61 74 65 64 2c 20 74 68 65 20 63 6f 70 79  ocated, the copy
1cd5f 20 62 65 63 6f 6d 65 73 20 69 6e 76 61 6c 69 64   becomes invalid
1cd60 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65 20 70 72  ..** Thus the pr
1cd61 6f 67 72 61 6d 20 6d 75 73 74 20 67 75 61 72 61  ogram must guara
1cd62 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 6f 72  ntee that the or
1cd63 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20  iginal will not 
1cd64 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72 69 6e 67  change.** during
1cd65 20 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66   the lifetime of
1cd66 20 74 68 65 20 63 6f 70 79 2e 20 20 55 73 65 20   the copy.  Use 
1cd67 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61 6b 65 20  OP_Copy to make 
1cd68 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 63 6f  a complete.** co
1cd69 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  py..*/.case OP_S
1cd6a 43 6f 70 79 3a 20 7b 20 20 20 20 20 20 20 20 20  Copy: {         
1cd6b 20 20 20 2f 2a 20 69 6e 31 2c 20 6f 75 74 32 20     /* in1, out2 
1cd6c 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  */.  pIn1 = &aMe
1cd6d 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f  m[pOp->p1];.  pO
1cd6e 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  ut = &aMem[pOp->
1cd6f 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p2];.  assert( p
1cd70 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a 20 20 73  Out!=pIn1 );.  s
1cd71 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
1cd72 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 70  llowCopy(pOut, p
1cd73 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b  In1, MEM_Ephem);
1cd74 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1cd75 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29  E(pOp->p2, pOut)
1cd76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1cd77 20 4f 70 63 6f 64 65 3a 20 52 65 73 75 6c 74 52   Opcode: ResultR
1cd78 6f 77 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ow P1 P2 * * *.*
1cd79 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65  *.** The registe
1cd7a 72 73 20 50 31 20 74 68 72 6f 75 67 68 20 50 31  rs P1 through P1
1cd7b 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e 20 61 20  +P2-1 contain a 
1cd7c 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 0a 2a 2a  single row of.**
1cd7d 20 72 65 73 75 6c 74 73 2e 20 54 68 69 73 20 6f   results. This o
1cd7e 70 63 6f 64 65 20 63 61 75 73 65 73 20 74 68 65  pcode causes the
1cd7f 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
1cd80 63 61 6c 6c 20 74 6f 20 74 65 72 6d 69 6e 61 74  call to terminat
1cd81 65 0a 2a 2a 20 77 69 74 68 20 61 6e 20 53 51 4c  e.** with an SQL
1cd82 49 54 45 5f 52 4f 57 20 72 65 74 75 72 6e 20 63  ITE_ROW return c
1cd83 6f 64 65 20 61 6e 64 20 69 74 20 73 65 74 73 20  ode and it sets 
1cd84 75 70 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  up the sqlite3_s
1cd85 74 6d 74 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  tmt.** structure
1cd86 20 74 6f 20 70 72 6f 76 69 64 65 20 61 63 63 65   to provide acce
1cd87 73 73 20 74 6f 20 74 68 65 20 74 6f 70 20 50 31  ss to the top P1
1cd88 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 72   values as the r
1cd89 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 2e 0a 2a 2f  esult.** row..*/
1cd8a 0a 63 61 73 65 20 4f 50 5f 52 65 73 75 6c 74 52  .case OP_ResultR
1cd8b 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  ow: {.#if 0  /* 
1cd8c 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1cd8d 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 64 20  moved into u.ad 
1cd8e 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a  */.  Mem *pMem;.
1cd8f 20 20 69 6e 74 20 69 3b 0a 23 65 6e 64 69 66 20    int i;.#endif 
1cd90 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1cd91 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1cd92 61 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  ad */.  assert( 
1cd93 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70  p->nResColumn==p
1cd94 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65  Op->p2 );.  asse
1cd95 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b  rt( pOp->p1>0 );
1cd96 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1cd97 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e  p1+pOp->p2<=p->n
1cd98 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f 2a 20 49  Mem+1 );..  /* I
1cd99 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74  f this statement
1cd9a 20 68 61 73 20 76 69 6f 6c 61 74 65 64 20 69 6d   has violated im
1cd9b 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20  mediate foreign 
1cd9c 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c  key constraints,
1cd9d 20 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 74   do.  ** not ret
1cd9e 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1cd9f 66 20 72 6f 77 73 20 6d 6f 64 69 66 69 65 64 2e  f rows modified.
1cda0 20 41 6e 64 20 64 6f 20 6e 6f 74 20 52 45 4c 45   And do not RELE
1cda1 41 53 45 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ASE the statemen
1cda2 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  t.  ** transacti
1cda3 6f 6e 2e 20 49 74 20 6e 65 65 64 73 20 74 6f 20  on. It needs to 
1cda4 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  be rolled back. 
1cda5 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45   */.  if( SQLITE
1cda6 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK!=(rc = sqlit
1cda7 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c  e3VdbeCheckFk(p,
1cda8 20 30 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65   0)) ){.    asse
1cda9 72 74 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51  rt( db->flags&SQ
1cdaa 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29  LITE_CountRows )
1cdab 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ;.    assert( p-
1cdac 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c  >usesStmtJournal
1cdad 20 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20   );.    break;. 
1cdae 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
1cdaf 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
1cdb0 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20   flag is set in 
1cdb1 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 20 6d 61  sqlite3.flags ma
1cdb2 73 6b 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 44 4d  sk, then.  ** DM
1cdb3 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 76  L statements inv
1cdb4 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 65 20  oke this opcode 
1cdb5 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  to return the nu
1cdb6 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a 20 20 2a  mber of rows.  *
1cdb7 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f 20 74 68  * modified to th
1cdb8 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 73 20  e user. This is 
1cdb9 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 68 61  the only way tha
1cdba 74 20 61 20 56 4d 20 74 68 61 74 0a 20 20 2a 2a  t a VM that.  **
1cdbb 20 6f 70 65 6e 73 20 61 20 73 74 61 74 65 6d 65   opens a stateme
1cdbc 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d  nt transaction m
1cdbd 61 79 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f  ay invoke this o
1cdbe 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  pcode..  **.  **
1cdbf 20 49 6e 20 63 61 73 65 20 74 68 69 73 20 69 73   In case this is
1cdc0 20 73 75 63 68 20 61 20 73 74 61 74 65 6d 65 6e   such a statemen
1cdc1 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 73 74 61  t, close any sta
1cdc2 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1cdc3 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 20 62  on.  ** opened b
1cdc4 79 20 74 68 69 73 20 56 4d 20 62 65 66 6f 72 65  y this VM before
1cdc5 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72   returning contr
1cdc6 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20  ol to the user. 
1cdc7 54 68 69 73 20 69 73 20 74 6f 0a 20 20 2a 2a 20  This is to.  ** 
1cdc8 65 6e 73 75 72 65 20 74 68 61 74 20 73 74 61 74  ensure that stat
1cdc9 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f  ement-transactio
1cdca 6e 73 20 61 72 65 20 61 6c 77 61 79 73 20 6e 65  ns are always ne
1cdcb 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 72 6c 61  sted, not overla
1cdcc 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49 66 20 74  pping..  ** If t
1cdcd 68 65 20 6f 70 65 6e 20 73 74 61 74 65 6d 65 6e  he open statemen
1cdce 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  t-transaction is
1cdcf 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68 65 72 65   not closed here
1cdd0 2c 20 74 68 65 6e 20 74 68 65 20 75 73 65 72 0a  , then the user.
1cdd1 20 20 2a 2a 20 6d 61 79 20 73 74 65 70 20 61 6e    ** may step an
1cdd2 6f 74 68 65 72 20 56 4d 20 74 68 61 74 20 6f 70  other VM that op
1cdd3 65 6e 73 20 69 74 73 20 6f 77 6e 20 73 74 61 74  ens its own stat
1cdd4 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1cdd5 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d 61 79  n. This.  ** may
1cdd6 20 6c 65 61 64 20 74 6f 20 6f 76 65 72 6c 61 70   lead to overlap
1cdd7 70 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74  ping statement t
1cdd8 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20 2a  ransactions..  *
1cdd9 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 61 74 65  *.  ** The state
1cdda 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1cddb 20 69 73 20 6e 65 76 65 72 20 61 20 74 6f 70 2d   is never a top-
1cddc 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 74 69 6f  level transactio
1cddd 6e 2e 20 20 48 65 6e 63 65 0a 20 20 2a 2a 20 74  n.  Hence.  ** t
1cdde 68 65 20 52 45 4c 45 41 53 45 20 63 61 6c 6c 20  he RELEASE call 
1cddf 62 65 6c 6f 77 20 63 61 6e 20 6e 65 76 65 72 20  below can never 
1cde0 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  fail..  */.  ass
1cde1 65 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65  ert( p->iStateme
1cde2 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 66 6c 61  nt==0 || db->fla
1cde3 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52  gs&SQLITE_CountR
1cde4 6f 77 73 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ows );.  rc = sq
1cde5 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74  lite3VdbeCloseSt
1cde6 61 74 65 6d 65 6e 74 28 70 2c 20 53 41 56 45 50  atement(p, SAVEP
1cde7 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a 20  OINT_RELEASE);. 
1cde8 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53   if( NEVER(rc!=S
1cde9 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20  QLITE_OK) ){.   
1cdea 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   break;.  }..  /
1cdeb 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c  * Invalidate all
1cdec 20 65 70 68 65 6d 65 72 61 6c 20 63 75 72 73 6f   ephemeral curso
1cded 72 20 72 6f 77 20 63 61 63 68 65 73 20 2a 2f 0a  r row caches */.
1cdee 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20    p->cacheCtr = 
1cdef 28 70 2d 3e 63 61 63 68 65 43 74 72 20 2b 20 32  (p->cacheCtr + 2
1cdf0 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  )|1;..  /* Make 
1cdf1 73 75 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  sure the results
1cdf2 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
1cdf3 72 6f 77 20 61 72 65 20 5c 30 30 30 20 74 65 72  row are \000 ter
1cdf4 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 61 6e 64  minated.  ** and
1cdf5 20 68 61 76 65 20 61 6e 20 61 73 73 69 67 6e 65   have an assigne
1cdf6 64 20 74 79 70 65 2e 20 20 54 68 65 20 72 65 73  d type.  The res
1cdf7 75 6c 74 73 20 61 72 65 20 64 65 2d 65 70 68 65  ults are de-ephe
1cdf8 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a 20 20 2a  meralized as.  *
1cdf9 2a 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74  * as side effect
1cdfa 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 64 2e 70 4d  ..  */.  u.ad.pM
1cdfb 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53  em = p->pResultS
1cdfc 65 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  et = &aMem[pOp->
1cdfd 70 31 5d 3b 0a 20 20 66 6f 72 28 75 2e 61 64 2e  p1];.  for(u.ad.
1cdfe 69 3d 30 3b 20 75 2e 61 64 2e 69 3c 70 4f 70 2d  i=0; u.ad.i<pOp-
1cdff 3e 70 32 3b 20 75 2e 61 64 2e 69 2b 2b 29 7b 0a  >p2; u.ad.i++){.
1ce00 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1ce01 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 26  emNulTerminate(&
1ce02 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69  u.ad.pMem[u.ad.i
1ce03 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ]);.    sqlite3V
1ce04 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28  dbeMemStoreType(
1ce05 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61 64 2e  &u.ad.pMem[u.ad.
1ce06 69 5d 29 3b 0a 20 20 20 20 52 45 47 49 53 54 45  i]);.    REGISTE
1ce07 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2b  R_TRACE(pOp->p1+
1ce08 75 2e 61 64 2e 69 2c 20 26 75 2e 61 64 2e 70 4d  u.ad.i, &u.ad.pM
1ce09 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b 0a 20 20 7d  em[u.ad.i]);.  }
1ce0a 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
1ce0b 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e  cFailed ) goto n
1ce0c 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20 52 65 74  o_mem;..  /* Ret
1ce0d 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57 0a 20  urn SQLITE_ROW. 
1ce0e 20 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63   */.  p->pc = pc
1ce0f 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c   + 1;.  rc = SQL
1ce10 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f 74 6f 20  ITE_ROW;.  goto 
1ce11 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a  vdbe_return;.}..
1ce12 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 63 61  /* Opcode: Conca
1ce13 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  t P1 P2 P3 * *.*
1ce14 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 74 65 78  *.** Add the tex
1ce15 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  t in register P1
1ce16 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66   onto the end of
1ce17 20 74 68 65 20 74 65 78 74 20 69 6e 0a 2a 2a 20   the text in.** 
1ce18 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 20  register P2 and 
1ce19 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
1ce1a 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
1ce1b 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 74 68  .** If either th
1ce1c 65 20 50 31 20 6f 72 20 50 32 20 74 65 78 74 20  e P1 or P2 text 
1ce1d 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 73 74  are NULL then st
1ce1e 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50 33 2e 0a  ore NULL in P3..
1ce1f 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20 50 32 20  **.**   P3 = P2 
1ce20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49 74 20 69  || P1.**.** It i
1ce21 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 50 31  s illegal for P1
1ce22 20 61 6e 64 20 50 33 20 74 6f 20 62 65 20 74 68   and P3 to be th
1ce23 65 20 73 61 6d 65 20 72 65 67 69 73 74 65 72 2e  e same register.
1ce24 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a 2a 20 69   Sometimes,.** i
1ce25 66 20 50 33 20 69 73 20 74 68 65 20 73 61 6d 65  f P3 is the same
1ce26 20 72 65 67 69 73 74 65 72 20 61 73 20 50 32 2c   register as P2,
1ce27 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
1ce28 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a 2a 20 74  ion is able.** t
1ce29 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 63 70 79  o avoid a memcpy
1ce2a 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  ()..*/.case OP_C
1ce2b 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20 20 20 20  oncat: {        
1ce2c 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1ce2d 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c 20 69 6e  _CONCAT, in1, in
1ce2e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30  2, out3 */.#if 0
1ce2f 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1ce30 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1ce31 75 2e 61 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42  u.ae */.  i64 nB
1ce32 79 74 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  yte;.#endif /* l
1ce33 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1ce34 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 65 20 2a  oved into u.ae *
1ce35 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  /..  pIn1 = &aMe
1ce36 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 49  m[pOp->p1];.  pI
1ce37 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n2 = &aMem[pOp->
1ce38 70 32 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61  p2];.  pOut = &a
1ce39 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20  Mem[pOp->p3];.  
1ce3a 61 73 73 65 72 74 28 20 70 49 6e 31 21 3d 70 4f  assert( pIn1!=pO
1ce3b 75 74 20 29 3b 0a 20 20 69 66 28 20 28 70 49 6e  ut );.  if( (pIn
1ce3c 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d  1->flags | pIn2-
1ce3d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75  >flags) & MEM_Nu
1ce3e 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ll ){.    sqlite
1ce3f 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
1ce40 70 4f 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b  pOut);.    break
1ce41 3b 0a 20 20 7d 0a 20 20 69 66 28 20 45 78 70 61  ;.  }.  if( Expa
1ce42 6e 64 42 6c 6f 62 28 70 49 6e 31 29 20 7c 7c 20  ndBlob(pIn1) || 
1ce43 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29  ExpandBlob(pIn2)
1ce44 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1ce45 20 20 53 74 72 69 6e 67 69 66 79 28 70 49 6e 31    Stringify(pIn1
1ce46 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 53  , encoding);.  S
1ce47 74 72 69 6e 67 69 66 79 28 70 49 6e 32 2c 20 65  tringify(pIn2, e
1ce48 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 75 2e 61 65  ncoding);.  u.ae
1ce49 2e 6e 42 79 74 65 20 3d 20 70 49 6e 31 2d 3e 6e  .nByte = pIn1->n
1ce4a 20 2b 20 70 49 6e 32 2d 3e 6e 3b 0a 20 20 69 66   + pIn2->n;.  if
1ce4b 28 20 75 2e 61 65 2e 6e 42 79 74 65 3e 64 62 2d  ( u.ae.nByte>db-
1ce4c 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1ce4d 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
1ce4e 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67      goto too_big
1ce4f 3b 0a 20 20 7d 0a 20 20 4d 65 6d 53 65 74 54 79  ;.  }.  MemSetTy
1ce50 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1ce51 5f 53 74 72 29 3b 0a 20 20 69 66 28 20 73 71 6c  _Str);.  if( sql
1ce52 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1ce53 70 4f 75 74 2c 20 28 69 6e 74 29 75 2e 61 65 2e  pOut, (int)u.ae.
1ce54 6e 42 79 74 65 2b 32 2c 20 70 4f 75 74 3d 3d 70  nByte+2, pOut==p
1ce55 49 6e 32 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  In2) ){.    goto
1ce56 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 69   no_mem;.  }.  i
1ce57 66 28 20 70 4f 75 74 21 3d 70 49 6e 32 20 29 7b  f( pOut!=pIn2 ){
1ce58 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4f 75 74  .    memcpy(pOut
1ce59 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 7a 2c 20 70 49  ->z, pIn2->z, pI
1ce5a 6e 32 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65  n2->n);.  }.  me
1ce5b 6d 63 70 79 28 26 70 4f 75 74 2d 3e 7a 5b 70 49  mcpy(&pOut->z[pI
1ce5c 6e 32 2d 3e 6e 5d 2c 20 70 49 6e 31 2d 3e 7a 2c  n2->n], pIn1->z,
1ce5d 20 70 49 6e 31 2d 3e 6e 29 3b 0a 20 20 70 4f 75   pIn1->n);.  pOu
1ce5e 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79 74 65 5d  t->z[u.ae.nByte]
1ce5f 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b   = 0;.  pOut->z[
1ce60 75 2e 61 65 2e 6e 42 79 74 65 2b 31 5d 20 3d 20  u.ae.nByte+1] = 
1ce61 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  0;.  pOut->flags
1ce62 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20   |= MEM_Term;.  
1ce63 70 4f 75 74 2d 3e 6e 20 3d 20 28 69 6e 74 29 75  pOut->n = (int)u
1ce64 2e 61 65 2e 6e 42 79 74 65 3b 0a 20 20 70 4f 75  .ae.nByte;.  pOu
1ce65 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e  t->enc = encodin
1ce66 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f  g;.  UPDATE_MAX_
1ce67 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a  BLOBSIZE(pOut);.
1ce68 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1ce69 70 63 6f 64 65 3a 20 41 64 64 20 50 31 20 50 32  pcode: Add P1 P2
1ce6a 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64   P3 * *.**.** Ad
1ce6b 64 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  d the value in r
1ce6c 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74 68  egister P1 to th
1ce6d 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1ce6e 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74  ter P2.** and st
1ce6f 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1ce70 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1ce71 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
1ce72 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
1ce73 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
1ce74 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 6c  /./* Opcode: Mul
1ce75 74 69 70 6c 79 20 50 31 20 50 32 20 50 33 20 2a  tiply P1 P2 P3 *
1ce76 20 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74   *.**.**.** Mult
1ce77 69 70 6c 79 20 74 68 65 20 76 61 6c 75 65 20 69  iply the value i
1ce78 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79  n register P1 by
1ce79 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1ce7a 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64  gister P2.** and
1ce7b 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
1ce7c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  t in register P3
1ce7d 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 69  ..** If either i
1ce7e 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68  nput is NULL, th
1ce7f 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c  e result is NULL
1ce80 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1ce81 53 75 62 74 72 61 63 74 20 50 31 20 50 32 20 50  Subtract P1 P2 P
1ce82 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 75 62 74  3 * *.**.** Subt
1ce83 72 61 63 74 20 74 68 65 20 76 61 6c 75 65 20 69  ract the value i
1ce84 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 66 72  n register P1 fr
1ce85 6f 6d 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  om the value in 
1ce86 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 20 61  register P2.** a
1ce87 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
1ce88 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1ce89 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1ce8a 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
1ce8b 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
1ce8c 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
1ce8d 3a 20 44 69 76 69 64 65 20 50 31 20 50 32 20 50  : Divide P1 P2 P
1ce8e 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 69 76 69  3 * *.**.** Divi
1ce8f 64 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  de the value in 
1ce90 72 65 67 69 73 74 65 72 20 50 31 20 62 79 20 74  register P1 by t
1ce91 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1ce92 73 74 65 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73  ster P2.** and s
1ce93 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
1ce94 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 28  in register P3 (
1ce95 50 33 3d 50 32 2f 50 31 29 2e 20 49 66 20 74 68  P3=P2/P1). If th
1ce96 65 20 76 61 6c 75 65 20 69 6e 20 0a 2a 2a 20 72  e value in .** r
1ce97 65 67 69 73 74 65 72 20 50 31 20 69 73 20 7a 65  egister P1 is ze
1ce98 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  ro, then the res
1ce99 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 20 49 66 20  ult is NULL. If 
1ce9a 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20  either input is 
1ce9b 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65  .** NULL, the re
1ce9c 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
1ce9d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61  ./* Opcode: Rema
1ce9e 69 6e 64 65 72 20 50 31 20 50 32 20 50 33 20 2a  inder P1 P2 P3 *
1ce9f 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65   *.**.** Compute
1cea0 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20 61   the remainder a
1cea1 66 74 65 72 20 69 6e 74 65 67 65 72 20 64 69 76  fter integer div
1cea2 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c  ision of the val
1cea3 75 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65  ue in.** registe
1cea4 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75  r P1 by the valu
1cea5 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  e in register P2
1cea6 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72   and store the r
1cea7 65 73 75 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a  esult in P3. .**
1cea8 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   If the value in
1cea9 20 72 65 67 69 73 74 65 72 20 50 32 20 69 73 20   register P2 is 
1ceaa 7a 65 72 6f 20 74 68 65 20 72 65 73 75 6c 74 20  zero the result 
1ceab 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65  is NULL..** If e
1ceac 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1cead 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c   NULL, the resul
1ceae 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61  t is NULL..*/.ca
1ceaf 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20  se OP_Add:      
1ceb0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ceb1 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c  same as TK_PLUS,
1ceb2 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
1ceb3 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 75 62 74 72  */.case OP_Subtr
1ceb4 61 63 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  act:            
1ceb5 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1ceb6 4d 49 4e 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c  MINUS, in1, in2,
1ceb7 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50   out3 */.case OP
1ceb8 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 20 20  _Multiply:      
1ceb9 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1ceba 61 73 20 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c  as TK_STAR, in1,
1cebb 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61   in2, out3 */.ca
1cebc 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 20 20  se OP_Divide:   
1cebd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1cebe 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41 53 48  same as TK_SLASH
1cebf 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33  , in1, in2, out3
1cec0 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61   */.case OP_Rema
1cec1 69 6e 64 65 72 3a 20 7b 20 20 20 20 20 20 20 20  inder: {        
1cec2 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1cec3 5f 52 45 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  _REM, in1, in2, 
1cec4 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f  out3 */.#if 0  /
1cec5 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1cec6 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1cec7 66 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  f */.  int flags
1cec8 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6d 62 69 6e  ;      /* Combin
1cec9 65 64 20 4d 45 4d 5f 2a 20 66 6c 61 67 73 20 66  ed MEM_* flags f
1ceca 72 6f 6d 20 62 6f 74 68 20 69 6e 70 75 74 73 20  rom both inputs 
1cecb 2a 2f 0a 20 20 69 36 34 20 69 41 3b 20 20 20 20  */.  i64 iA;    
1cecc 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20       /* Integer 
1cecd 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70  value of left op
1cece 65 72 61 6e 64 20 2a 2f 0a 20 20 69 36 34 20 69  erand */.  i64 i
1cecf 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  B;         /* In
1ced0 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20 72  teger value of r
1ced1 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  ight operand */.
1ced2 20 20 64 6f 75 62 6c 65 20 72 41 3b 20 20 20 20    double rA;    
1ced3 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c 75 65 20    /* Real value 
1ced4 6f 66 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  of left operand 
1ced5 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 42 3b 20  */.  double rB; 
1ced6 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76 61 6c       /* Real val
1ced7 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65 72  ue of right oper
1ced8 61 6e 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  and */.#endif /*
1ced9 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1ceda 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 66   moved into u.af
1cedb 20 2a 2f 0a 0a 20 20 70 49 6e 31 20 3d 20 26 61   */..  pIn1 = &a
1cedc 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1cedd 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69  applyNumericAffi
1cede 6e 69 74 79 28 70 49 6e 31 29 3b 0a 20 20 70 49  nity(pIn1);.  pI
1cedf 6e 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n2 = &aMem[pOp->
1cee0 70 32 5d 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65  p2];.  applyNume
1cee1 72 69 63 41 66 66 69 6e 69 74 79 28 70 49 6e 32  ricAffinity(pIn2
1cee2 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d 65  );.  pOut = &aMe
1cee3 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e  m[pOp->p3];.  u.
1cee4 61 66 2e 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d  af.flags = pIn1-
1cee5 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66  >flags | pIn2->f
1cee6 6c 61 67 73 3b 0a 20 20 69 66 28 20 28 75 2e 61  lags;.  if( (u.a
1cee7 66 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  f.flags & MEM_Nu
1cee8 6c 6c 29 21 3d 30 20 29 20 67 6f 74 6f 20 61 72  ll)!=0 ) goto ar
1cee9 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f  ithmetic_result_
1ceea 69 73 5f 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 28  is_null;.  if( (
1ceeb 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 70 49  pIn1->flags & pI
1ceec 6e 32 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n2->flags & MEM_
1ceed 49 6e 74 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b  Int)==MEM_Int ){
1ceee 0a 20 20 20 20 75 2e 61 66 2e 69 41 20 3d 20 70  .    u.af.iA = p
1ceef 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 2e  In1->u.i;.    u.
1cef0 61 66 2e 69 42 20 3d 20 70 49 6e 32 2d 3e 75 2e  af.iB = pIn2->u.
1cef1 69 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  i;.    switch( p
1cef2 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20  Op->opcode ){.  
1cef3 20 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a      case OP_Add:
1cef4 20 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42           u.af.iB
1cef5 20 2b 3d 20 75 2e 61 66 2e 69 41 3b 20 20 20 20   += u.af.iA;    
1cef6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1cef7 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74  case OP_Subtract
1cef8 3a 20 20 20 20 75 2e 61 66 2e 69 42 20 2d 3d 20  :    u.af.iB -= 
1cef9 75 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20 62  u.af.iA;       b
1cefa 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
1cefb 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20   OP_Multiply:   
1cefc 20 75 2e 61 66 2e 69 42 20 2a 3d 20 75 2e 61 66   u.af.iB *= u.af
1cefd 2e 69 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b  .iA;       break
1cefe 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f  ;.      case OP_
1ceff 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20  Divide: {.      
1cf00 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30    if( u.af.iA==0
1cf01 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74   ) goto arithmet
1cf02 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
1cf03 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 69  l;.        /* Di
1cf04 76 69 64 69 6e 67 20 74 68 65 20 6c 61 72 67 65  viding the large
1cf05 73 74 20 70 6f 73 73 69 62 6c 65 20 6e 65 67 61  st possible nega
1cf06 74 69 76 65 20 36 34 2d 62 69 74 20 69 6e 74 65  tive 64-bit inte
1cf07 67 65 72 20 28 31 3c 3c 36 33 29 20 62 79 0a 20  ger (1<<63) by. 
1cf08 20 20 20 20 20 20 20 2a 2a 20 2d 31 20 72 65 74         ** -1 ret
1cf09 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20  urns an integer 
1cf0a 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 73 74 6f  too large to sto
1cf0b 72 65 20 69 6e 20 61 20 36 34 2d 62 69 74 20 64  re in a 64-bit d
1cf0c 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a 20 20 20  ata-type. On.   
1cf0d 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20 61 72 63       ** some arc
1cf0e 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 65 20  hitectures, the 
1cf0f 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f 77 73 20  value overflows 
1cf10 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f 6e 20 6f  to (1<<63). On o
1cf11 74 68 65 72 73 2c 0a 20 20 20 20 20 20 20 20 2a  thers,.        *
1cf12 2a 20 61 20 53 49 47 46 50 45 20 69 73 20 69 73  * a SIGFPE is is
1cf13 73 75 65 64 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  sued. The follow
1cf14 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f  ing statement no
1cf15 72 6d 61 6c 69 7a 65 73 20 74 68 69 73 0a 20 20  rmalizes this.  
1cf16 20 20 20 20 20 20 2a 2a 20 62 65 68 61 76 69 6f        ** behavio
1cf17 72 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 61 72  r so that all ar
1cf18 63 68 69 74 65 63 74 75 72 65 73 20 62 65 68 61  chitectures beha
1cf19 76 65 20 61 73 20 69 66 20 69 6e 74 65 67 65 72  ve as if integer
1cf1a 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 76 65 72  .        ** over
1cf1b 66 6c 6f 77 20 6f 63 63 75 72 72 65 64 2e 0a 20  flow occurred.. 
1cf1c 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
1cf1d 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d    if( u.af.iA==-
1cf1e 31 20 26 26 20 75 2e 61 66 2e 69 42 3d 3d 53 4d  1 && u.af.iB==SM
1cf1f 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 29 20 75  ALLEST_INT64 ) u
1cf20 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 20 20 20  .af.iA = 1;.    
1cf21 20 20 20 20 75 2e 61 66 2e 69 42 20 2f 3d 20 75      u.af.iB /= u
1cf22 2e 61 66 2e 69 41 3b 0a 20 20 20 20 20 20 20 20  .af.iA;.        
1cf23 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1cf24 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
1cf25 20 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66          if( u.af
1cf26 2e 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72  .iA==0 ) goto ar
1cf27 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f  ithmetic_result_
1cf28 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20  is_null;.       
1cf29 20 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31   if( u.af.iA==-1
1cf2a 20 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a   ) u.af.iA = 1;.
1cf2b 20 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20          u.af.iB 
1cf2c 25 3d 20 75 2e 61 66 2e 69 41 3b 0a 20 20 20 20  %= u.af.iA;.    
1cf2d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1cf2e 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75   }.    }.    pOu
1cf2f 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 66 2e 69 42  t->u.i = u.af.iB
1cf30 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ;.    MemSetType
1cf31 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1cf32 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  nt);.  }else{.  
1cf33 20 20 75 2e 61 66 2e 72 41 20 3d 20 73 71 6c 69    u.af.rA = sqli
1cf34 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65  te3VdbeRealValue
1cf35 28 70 49 6e 31 29 3b 0a 20 20 20 20 75 2e 61 66  (pIn1);.    u.af
1cf36 2e 72 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62  .rB = sqlite3Vdb
1cf37 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 32 29  eRealValue(pIn2)
1cf38 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 4f  ;.    switch( pO
1cf39 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20  p->opcode ){.   
1cf3a 20 20 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20     case OP_Add: 
1cf3b 20 20 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20          u.af.rB 
1cf3c 2b 3d 20 75 2e 61 66 2e 72 41 3b 20 20 20 20 20  += u.af.rA;     
1cf3d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
1cf3e 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a  ase OP_Subtract:
1cf3f 20 20 20 20 75 2e 61 66 2e 72 42 20 2d 3d 20 75      u.af.rB -= u
1cf40 2e 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62 72  .af.rA;       br
1cf41 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
1cf42 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20  OP_Multiply:    
1cf43 75 2e 61 66 2e 72 42 20 2a 3d 20 75 2e 61 66 2e  u.af.rB *= u.af.
1cf44 72 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b  rA;       break;
1cf45 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44  .      case OP_D
1cf46 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20  ivide: {.       
1cf47 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e   /* (double)0 In
1cf48 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f   case of SQLITE_
1cf49 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
1cf4a 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20  INT... */.      
1cf4b 20 20 69 66 28 20 75 2e 61 66 2e 72 41 3d 3d 28    if( u.af.rA==(
1cf4c 64 6f 75 62 6c 65 29 30 20 29 20 67 6f 74 6f 20  double)0 ) goto 
1cf4d 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73 75 6c  arithmetic_resul
1cf4e 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20  t_is_null;.     
1cf4f 20 20 20 75 2e 61 66 2e 72 42 20 2f 3d 20 75 2e     u.af.rB /= u.
1cf50 61 66 2e 72 41 3b 0a 20 20 20 20 20 20 20 20 62  af.rA;.        b
1cf51 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1cf52 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
1cf53 20 20 20 20 20 20 20 75 2e 61 66 2e 69 41 20 3d         u.af.iA =
1cf54 20 28 69 36 34 29 75 2e 61 66 2e 72 41 3b 0a 20   (i64)u.af.rA;. 
1cf55 20 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 3d         u.af.iB =
1cf56 20 28 69 36 34 29 75 2e 61 66 2e 72 42 3b 0a 20   (i64)u.af.rB;. 
1cf57 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e         if( u.af.
1cf58 69 41 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69  iA==0 ) goto ari
1cf59 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69  thmetic_result_i
1cf5a 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20  s_null;.        
1cf5b 69 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20  if( u.af.iA==-1 
1cf5c 29 20 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20  ) u.af.iA = 1;. 
1cf5d 20 20 20 20 20 20 20 75 2e 61 66 2e 72 42 20 3d         u.af.rB =
1cf5e 20 28 64 6f 75 62 6c 65 29 28 75 2e 61 66 2e 69   (double)(u.af.i
1cf5f 42 20 25 20 75 2e 61 66 2e 69 41 29 3b 0a 20 20  B % u.af.iA);.  
1cf60 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1cf61 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1cf62 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28  f( sqlite3IsNaN(
1cf63 75 2e 61 66 2e 72 42 29 20 29 7b 0a 20 20 20 20  u.af.rB) ){.    
1cf64 20 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69    goto arithmeti
1cf65 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c  c_result_is_null
1cf66 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74  ;.    }.    pOut
1cf67 2d 3e 72 20 3d 20 75 2e 61 66 2e 72 42 3b 0a 20  ->r = u.af.rB;. 
1cf68 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1cf69 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 52 65 61 6c  g(pOut, MEM_Real
1cf6a 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e 61 66  );.    if( (u.af
1cf6b 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  .flags & MEM_Rea
1cf6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  l)==0 ){.      s
1cf6d 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65  qlite3VdbeIntege
1cf6e 72 41 66 66 69 6e 69 74 79 28 70 4f 75 74 29 3b  rAffinity(pOut);
1cf6f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
1cf70 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 69 63 5f  ak;..arithmetic_
1cf71 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a 0a  result_is_null:.
1cf72 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cf73 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20  SetNull(pOut);. 
1cf74 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1cf75 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20  code: CollSeq * 
1cf76 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  * P4.**.** P4 is
1cf77 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
1cf78 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 2e 20  CollSeq struct. 
1cf79 49 66 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  If the next call
1cf7a 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e 63 74   to a user funct
1cf7b 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67  ion.** or aggreg
1cf7c 61 74 65 20 63 61 6c 6c 73 20 73 71 6c 69 74 65  ate calls sqlite
1cf7d 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28  3GetFuncCollSeq(
1cf7e 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f  ), this collatio
1cf7f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a  n sequence will.
1cf80 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20  ** be returned. 
1cf81 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
1cf82 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e  the built-in min
1cf83 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 20 6e 75  (), max() and nu
1cf84 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69  llif().** functi
1cf85 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ons..**.** The i
1cf86 6e 74 65 72 66 61 63 65 20 75 73 65 64 20 62 79  nterface used by
1cf87 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
1cf88 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f 72 65  ion of the afore
1cf89 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63 74 69  mentioned functi
1cf8a 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65  ons.** to retrie
1cf8b 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ve the collation
1cf8c 20 73 65 71 75 65 6e 63 65 20 73 65 74 20 62 79   sequence set by
1cf8d 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20   this opcode is 
1cf8e 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a  not available.**
1cf8f 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20   publicly, only 
1cf90 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  to user function
1cf91 73 20 64 65 66 69 6e 65 64 20 69 6e 20 66 75 6e  s defined in fun
1cf92 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  c.c..*/.case OP_
1cf93 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 61 73 73  CollSeq: {.  ass
1cf94 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
1cf95 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a  ==P4_COLLSEQ );.
1cf96 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1cf97 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20  pcode: Function 
1cf98 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1cf99 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 75 73  *.** Invoke a us
1cf9a 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 50 34 20  er function (P4 
1cf9b 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1cf9c 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 72 75 63  a Function struc
1cf9d 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 64 65 66  ture that.** def
1cf9e 69 6e 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f  ines the functio
1cf9f 6e 29 20 77 69 74 68 20 50 35 20 61 72 67 75 6d  n) with P5 argum
1cfa0 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20  ents taken from 
1cfa1 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 0a  register P2 and.
1cfa2 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e 20 20  ** successors.  
1cfa3 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  The result of th
1cfa4 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 74  e function is st
1cfa5 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
1cfa6 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 74 65 72   P3..** Register
1cfa7 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 62 65 20   P3 must not be 
1cfa8 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74  one of the funct
1cfa9 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a 2a  ion inputs..**.*
1cfaa 2a 20 50 31 20 69 73 20 61 20 33 32 2d 62 69 74  * P1 is a 32-bit
1cfab 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74   bitmask indicat
1cfac 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ing whether or n
1cfad 6f 74 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74  ot each argument
1cfae 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63   to the .** func
1cfaf 74 69 6f 6e 20 77 61 73 20 64 65 74 65 72 6d 69  tion was determi
1cfb0 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 74 61  ned to be consta
1cfb1 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69  nt at compile ti
1cfb2 6d 65 2e 20 49 66 20 74 68 65 20 66 69 72 73 74  me. If the first
1cfb3 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 61 73  .** argument was
1cfb4 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 62   constant then b
1cfb5 69 74 20 30 20 6f 66 20 50 31 20 69 73 20 73 65  it 0 of P1 is se
1cfb6 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  t. This is used 
1cfb7 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20  to determine.** 
1cfb8 77 68 65 74 68 65 72 20 6d 65 74 61 20 64 61 74  whether meta dat
1cfb9 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
1cfba 68 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f  h a user functio
1cfbb 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 69 6e 67  n argument using
1cfbc 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
1cfbd 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 50  set_auxdata() AP
1cfbe 49 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20  I may be safely 
1cfbf 72 65 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 74  retained until t
1cfc0 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63  he next.** invoc
1cfc1 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70  ation of this op
1cfc2 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  code..**.** See 
1cfc3 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 61 6e  also: AggStep an
1cfc4 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61  d AggFinal.*/.ca
1cfc5 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20  se OP_Function: 
1cfc6 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1cfc7 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1cfc8 64 20 69 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 20  d into u.ag */. 
1cfc9 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70   int i;.  Mem *p
1cfca 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63  Arg;.  sqlite3_c
1cfcb 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 73 71  ontext ctx;.  sq
1cfcc 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
1cfcd 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 3b 0a 23 65  Val;.  int n;.#e
1cfce 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1cfcf 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1cfd0 74 6f 20 75 2e 61 67 20 2a 2f 0a 0a 20 20 75 2e  to u.ag */..  u.
1cfd1 61 67 2e 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a  ag.n = pOp->p5;.
1cfd2 20 20 75 2e 61 67 2e 61 70 56 61 6c 20 3d 20 70    u.ag.apVal = p
1cfd3 2d 3e 61 70 41 72 67 3b 0a 20 20 61 73 73 65 72  ->apArg;.  asser
1cfd4 74 28 20 75 2e 61 67 2e 61 70 56 61 6c 20 7c 7c  t( u.ag.apVal ||
1cfd5 20 75 2e 61 67 2e 6e 3d 3d 30 20 29 3b 0a 0a 20   u.ag.n==0 );.. 
1cfd6 20 61 73 73 65 72 74 28 20 75 2e 61 67 2e 6e 3d   assert( u.ag.n=
1cfd7 3d 30 20 7c 7c 20 28 70 4f 70 2d 3e 70 32 3e 30  =0 || (pOp->p2>0
1cfd8 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 67   && pOp->p2+u.ag
1cfd9 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 29 20 29  .n<=p->nMem+1) )
1cfda 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1cfdb 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20 70  >p3<pOp->p2 || p
1cfdc 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32 2b  Op->p3>=pOp->p2+
1cfdd 75 2e 61 67 2e 6e 20 29 3b 0a 20 20 75 2e 61 67  u.ag.n );.  u.ag
1cfde 2e 70 41 72 67 20 3d 20 26 61 4d 65 6d 5b 70 4f  .pArg = &aMem[pO
1cfdf 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 28 75 2e  p->p2];.  for(u.
1cfe0 61 67 2e 69 3d 30 3b 20 75 2e 61 67 2e 69 3c 75  ag.i=0; u.ag.i<u
1cfe1 2e 61 67 2e 6e 3b 20 75 2e 61 67 2e 69 2b 2b 2c  .ag.n; u.ag.i++,
1cfe2 20 75 2e 61 67 2e 70 41 72 67 2b 2b 29 7b 0a 20   u.ag.pArg++){. 
1cfe3 20 20 20 75 2e 61 67 2e 61 70 56 61 6c 5b 75 2e     u.ag.apVal[u.
1cfe4 61 67 2e 69 5d 20 3d 20 75 2e 61 67 2e 70 41 72  ag.i] = u.ag.pAr
1cfe5 67 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  g;.    sqlite3Vd
1cfe6 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 75  beMemStoreType(u
1cfe7 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 20 20 52  .ag.pArg);.    R
1cfe8 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1cfe9 70 2d 3e 70 32 2c 20 75 2e 61 67 2e 70 41 72 67  p->p2, u.ag.pArg
1cfea 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  );.  }..  assert
1cfeb 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1cfec 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20 70 4f 70  4_FUNCDEF || pOp
1cfed 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 44 42  ->p4type==P4_VDB
1cfee 45 46 55 4e 43 20 29 3b 0a 20 20 69 66 28 20 70  EFUNC );.  if( p
1cfef 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46  Op->p4type==P4_F
1cff0 55 4e 43 44 45 46 20 29 7b 0a 20 20 20 20 75 2e  UNCDEF ){.    u.
1cff1 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d 20 70  ag.ctx.pFunc = p
1cff2 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20  Op->p4.pFunc;.  
1cff3 20 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65    u.ag.ctx.pVdbe
1cff4 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  Func = 0;.  }els
1cff5 65 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e  e{.    u.ag.ctx.
1cff6 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62  pVdbeFunc = (Vdb
1cff7 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 34 2e 70  eFunc*)pOp->p4.p
1cff8 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 75 2e  VdbeFunc;.    u.
1cff9 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d 20 75  ag.ctx.pFunc = u
1cffa 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e  .ag.ctx.pVdbeFun
1cffb 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20  c->pFunc;.  }.. 
1cffc 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1cffd 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70  >0 && pOp->p3<=p
1cffe 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74  ->nMem );.  pOut
1cfff 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   = &aMem[pOp->p3
1d000 5d 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e  ];.  u.ag.ctx.s.
1d001 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1d002 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 64  ;.  u.ag.ctx.s.d
1d003 62 20 3d 20 64 62 3b 0a 20 20 75 2e 61 67 2e 63  b = db;.  u.ag.c
1d004 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20  tx.s.xDel = 0;. 
1d005 20 75 2e 61 67 2e 63 74 78 2e 73 2e 7a 4d 61 6c   u.ag.ctx.s.zMal
1d006 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54  loc = 0;..  /* T
1d007 68 65 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d  he output cell m
1d008 61 79 20 61 6c 72 65 61 64 79 20 68 61 76 65 20  ay already have 
1d009 61 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74  a buffer allocat
1d00a 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68  ed. Move.  ** th
1d00b 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 75 2e 61  e pointer to u.a
1d00c 67 2e 63 74 78 2e 73 20 73 6f 20 69 6e 20 63 61  g.ctx.s so in ca
1d00d 73 65 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63  se the user-func
1d00e 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a 20 20 2a  tion can use.  *
1d00f 2a 20 74 68 65 20 61 6c 72 65 61 64 79 20 61 6c  * the already al
1d010 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 69  located buffer i
1d011 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f 63 61  nstead of alloca
1d012 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65 2e 0a  ting a new one..
1d013 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
1d014 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 67 2e  beMemMove(&u.ag.
1d015 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b 0a 20 20  ctx.s, pOut);.  
1d016 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 26  MemSetTypeFlag(&
1d017 75 2e 61 67 2e 63 74 78 2e 73 2c 20 4d 45 4d 5f  u.ag.ctx.s, MEM_
1d018 4e 75 6c 6c 29 3b 0a 0a 20 20 75 2e 61 67 2e 63  Null);..  u.ag.c
1d019 74 78 2e 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a  tx.isError = 0;.
1d01a 20 20 69 66 28 20 75 2e 61 67 2e 63 74 78 2e 70    if( u.ag.ctx.p
1d01b 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53 51  Func->flags & SQ
1d01c 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f  LITE_FUNC_NEEDCO
1d01d 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  LL ){.    assert
1d01e 28 20 70 4f 70 3e 61 4f 70 20 29 3b 0a 20 20 20  ( pOp>aOp );.   
1d01f 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d   assert( pOp[-1]
1d020 2e 70 34 74 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c  .p4type==P4_COLL
1d021 53 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65 72  SEQ );.    asser
1d022 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64  t( pOp[-1].opcod
1d023 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65 71 20 29 3b  e==OP_CollSeq );
1d024 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e 70 43  .    u.ag.ctx.pC
1d025 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d 2e 70 34  oll = pOp[-1].p4
1d026 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66  .pColl;.  }.  if
1d027 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1d028 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
1d029 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1d02a 65 3b 0a 20 20 28 2a 75 2e 61 67 2e 63 74 78 2e  e;.  (*u.ag.ctx.
1d02b 70 46 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 75  pFunc->xFunc)(&u
1d02c 2e 61 67 2e 63 74 78 2c 20 75 2e 61 67 2e 6e 2c  .ag.ctx, u.ag.n,
1d02d 20 75 2e 61 67 2e 61 70 56 61 6c 29 3b 0a 20 20   u.ag.apVal);.  
1d02e 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1d02f 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73  yOn(db) ){.    s
1d030 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1d031 65 61 73 65 28 26 75 2e 61 67 2e 63 74 78 2e 73  ease(&u.ag.ctx.s
1d032 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  );.    goto abor
1d033 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1d034 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d  .  }.  if( db->m
1d035 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1d036 20 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67     /* Even thoug
1d037 68 20 61 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73  h a malloc() has
1d038 20 66 61 69 6c 65 64 2c 20 74 68 65 20 69 6d 70   failed, the imp
1d039 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
1d03a 68 65 0a 20 20 20 20 2a 2a 20 75 73 65 72 20 66  he.    ** user f
1d03b 75 6e 63 74 69 6f 6e 20 6d 61 79 20 68 61 76 65  unction may have
1d03c 20 63 61 6c 6c 65 64 20 61 6e 20 73 71 6c 69 74   called an sqlit
1d03d 65 33 5f 72 65 73 75 6c 74 5f 58 58 58 28 29 20  e3_result_XXX() 
1d03e 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 2a 2a 20  function.    ** 
1d03f 74 6f 20 72 65 74 75 72 6e 20 61 20 76 61 6c 75  to return a valu
1d040 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  e. The following
1d041 20 63 61 6c 6c 20 72 65 6c 65 61 73 65 73 20 61   call releases a
1d042 6e 79 20 72 65 73 6f 75 72 63 65 73 0a 20 20 20  ny resources.   
1d043 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77   ** associated w
1d044 69 74 68 20 73 75 63 68 20 61 20 76 61 6c 75 65  ith such a value
1d045 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1d046 4e 6f 74 65 3a 20 4d 61 79 62 65 20 4d 65 6d 52  Note: Maybe MemR
1d047 65 6c 65 61 73 65 28 29 20 73 68 6f 75 6c 64 20  elease() should 
1d048 62 65 20 63 61 6c 6c 65 64 20 69 66 20 73 71 6c  be called if sql
1d049 69 74 65 33 53 61 66 65 74 79 4f 6e 28 29 0a 20  ite3SafetyOn(). 
1d04a 20 20 20 2a 2a 20 66 61 69 6c 73 20 61 6c 73 6f     ** fails also
1d04b 20 28 74 68 65 20 69 66 28 2e 2e 2e 29 20 73 74   (the if(...) st
1d04c 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 29 2e 20  atement above). 
1d04d 42 75 74 20 69 66 20 70 65 6f 70 6c 65 20 61 72  But if people ar
1d04e 65 0a 20 20 20 20 2a 2a 20 6d 69 73 75 73 69 6e  e.    ** misusin
1d04f 67 20 73 71 6c 69 74 65 2c 20 74 68 65 79 20 68  g sqlite, they h
1d050 61 76 65 20 62 69 67 67 65 72 20 70 72 6f 62 6c  ave bigger probl
1d051 65 6d 73 20 74 68 61 6e 20 61 20 6c 65 61 6b 65  ems than a leake
1d052 64 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a  d value..    */.
1d053 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1d054 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 61 67 2e  emRelease(&u.ag.
1d055 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74 6f  ctx.s);.    goto
1d056 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20   no_mem;.  }..  
1d057 2f 2a 20 49 66 20 61 6e 79 20 61 75 78 69 6c 69  /* If any auxili
1d058 61 72 79 20 64 61 74 61 20 66 75 6e 63 74 69 6f  ary data functio
1d059 6e 73 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c  ns have been cal
1d05a 6c 65 64 20 62 79 20 74 68 69 73 20 75 73 65 72  led by this user
1d05b 20 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20   function,.  ** 
1d05c 69 6d 6d 65 64 69 61 74 65 6c 79 20 63 61 6c 6c  immediately call
1d05d 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
1d05e 66 6f 72 20 61 6e 79 20 6e 6f 6e 2d 73 74 61 74  for any non-stat
1d05f 69 63 20 76 61 6c 75 65 73 2e 0a 20 20 2a 2f 0a  ic values..  */.
1d060 20 20 69 66 28 20 75 2e 61 67 2e 63 74 78 2e 70    if( u.ag.ctx.p
1d061 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20  VdbeFunc ){.    
1d062 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
1d063 65 41 75 78 44 61 74 61 28 75 2e 61 67 2e 63 74  eAuxData(u.ag.ct
1d064 78 2e 70 56 64 62 65 46 75 6e 63 2c 20 70 4f 70  x.pVdbeFunc, pOp
1d065 2d 3e 70 31 29 3b 0a 20 20 20 20 70 4f 70 2d 3e  ->p1);.    pOp->
1d066 70 34 2e 70 56 64 62 65 46 75 6e 63 20 3d 20 75  p4.pVdbeFunc = u
1d067 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e  .ag.ctx.pVdbeFun
1d068 63 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  c;.    pOp->p4ty
1d069 70 65 20 3d 20 50 34 5f 56 44 42 45 46 55 4e 43  pe = P4_VDBEFUNC
1d06a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
1d06b 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  he function retu
1d06c 72 6e 65 64 20 61 6e 20 65 72 72 6f 72 2c 20 74  rned an error, t
1d06d 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74 69 6f  hrow an exceptio
1d06e 6e 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67 2e  n */.  if( u.ag.
1d06f 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20  ctx.isError ){. 
1d070 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1d071 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1d072 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
1d073 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26 75  e3_value_text(&u
1d074 2e 61 67 2e 63 74 78 2e 73 29 29 3b 0a 20 20 20  .ag.ctx.s));.   
1d075 20 72 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e 69   rc = u.ag.ctx.i
1d076 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f  sError;.  }..  /
1d077 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c  * Copy the resul
1d078 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  t of the functio
1d079 6e 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20  n into register 
1d07a 50 33 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  P3 */.  sqlite3V
1d07b 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e  dbeChangeEncodin
1d07c 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 65  g(&u.ag.ctx.s, e
1d07d 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71 6c 69  ncoding);.  sqli
1d07e 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70  te3VdbeMemMove(p
1d07f 4f 75 74 2c 20 26 75 2e 61 67 2e 63 74 78 2e 73  Out, &u.ag.ctx.s
1d080 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
1d081 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f  VdbeMemTooBig(pO
1d082 75 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ut) ){.    goto 
1d083 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 52  too_big;.  }.  R
1d084 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1d085 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20  p->p3, pOut);.  
1d086 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1d087 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65  IZE(pOut);.  bre
1d088 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1d089 3a 20 42 69 74 41 6e 64 20 50 31 20 50 32 20 50  : BitAnd P1 P2 P
1d08a 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65  3 * *.**.** Take
1d08b 20 74 68 65 20 62 69 74 2d 77 69 73 65 20 41 4e   the bit-wise AN
1d08c 44 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20  D of the values 
1d08d 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61  in register P1 a
1d08e 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f  nd P2 and.** sto
1d08f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
1d090 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
1d091 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74   If either input
1d092 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
1d093 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f  sult is NULL..*/
1d094 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f  ./* Opcode: BitO
1d095 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  r P1 P2 P3 * *.*
1d096 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 62 69  *.** Take the bi
1d097 74 2d 77 69 73 65 20 4f 52 20 6f 66 20 74 68 65  t-wise OR of the
1d098 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73   values in regis
1d099 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e  ter P1 and P2 an
1d09a 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72  d.** store the r
1d09b 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65  esult in registe
1d09c 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68  r P3..** If eith
1d09d 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
1d09e 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
1d09f 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  NULL..*/./* Opco
1d0a0 64 65 3a 20 53 68 69 66 74 4c 65 66 74 20 50 31  de: ShiftLeft P1
1d0a1 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1d0a2 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65 67   Shift the integ
1d0a3 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  er value in regi
1d0a4 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20 6c  ster P2 to the l
1d0a5 65 66 74 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75  eft by the.** nu
1d0a6 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73 70 65  mber of bits spe
1d0a7 63 69 66 69 65 64 20 62 79 20 74 68 65 20 69 6e  cified by the in
1d0a8 74 65 67 65 72 20 69 6e 20 72 65 67 69 73 65 72  teger in regiser
1d0a9 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68   P1..** Store th
1d0aa 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69  e result in regi
1d0ab 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65  ster P3..** If e
1d0ac 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e  ither input is N
1d0ad 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20  ULL, the result 
1d0ae 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f  is NULL..*/./* O
1d0af 70 63 6f 64 65 3a 20 53 68 69 66 74 52 69 67 68  pcode: ShiftRigh
1d0b0 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  t P1 P2 P3 * *.*
1d0b1 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65 20 69  *.** Shift the i
1d0b2 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e 20  nteger value in 
1d0b3 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20 74  register P2 to t
1d0b4 68 65 20 72 69 67 68 74 20 62 79 20 74 68 65 0a  he right by the.
1d0b5 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74  ** number of bit
1d0b6 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 74  s specified by t
1d0b7 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72 65  he integer in re
1d0b8 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74  gister P1..** St
1d0b9 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1d0ba 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1d0bb 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
1d0bc 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
1d0bd 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
1d0be 2f 0a 63 61 73 65 20 4f 50 5f 42 69 74 41 6e 64  /.case OP_BitAnd
1d0bf 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
1d0c0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1d0c1 42 49 54 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32  BITAND, in1, in2
1d0c2 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
1d0c3 50 5f 42 69 74 4f 72 3a 20 20 20 20 20 20 20 20  P_BitOr:        
1d0c4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1d0c5 65 20 61 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69  e as TK_BITOR, i
1d0c6 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1d0c7 0a 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c 65  .case OP_ShiftLe
1d0c8 66 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  ft:             
1d0c9 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c   /* same as TK_L
1d0ca 53 48 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c  SHIFT, in1, in2,
1d0cb 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50   out3 */.case OP
1d0cc 5f 53 68 69 66 74 52 69 67 68 74 3a 20 7b 20 20  _ShiftRight: {  
1d0cd 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1d0ce 20 61 73 20 54 4b 5f 52 53 48 49 46 54 2c 20 69   as TK_RSHIFT, i
1d0cf 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1d0d0 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1d0d1 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d0d2 20 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 20 20   into u.ah */.  
1d0d3 69 36 34 20 61 3b 0a 20 20 69 36 34 20 62 3b 0a  i64 a;.  i64 b;.
1d0d4 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1d0d5 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1d0d6 69 6e 74 6f 20 75 2e 61 68 20 2a 2f 0a 0a 20 20  into u.ah */..  
1d0d7 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
1d0d8 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 32 20 3d 20  ->p1];.  pIn2 = 
1d0d9 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  &aMem[pOp->p2];.
1d0da 20 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70    pOut = &aMem[p
1d0db 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28  Op->p3];.  if( (
1d0dc 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49  pIn1->flags | pI
1d0dd 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d  n2->flags) & MEM
1d0de 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c  _Null ){.    sql
1d0df 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
1d0e0 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72  ll(pOut);.    br
1d0e1 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 61 68 2e  eak;.  }.  u.ah.
1d0e2 61 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  a = sqlite3VdbeI
1d0e3 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20  ntValue(pIn2);. 
1d0e4 20 75 2e 61 68 2e 62 20 3d 20 73 71 6c 69 74 65   u.ah.b = sqlite
1d0e5 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49  3VdbeIntValue(pI
1d0e6 6e 31 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70  n1);.  switch( p
1d0e7 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20  Op->opcode ){.  
1d0e8 20 20 63 61 73 65 20 4f 50 5f 42 69 74 41 6e 64    case OP_BitAnd
1d0e9 3a 20 20 20 20 20 20 75 2e 61 68 2e 61 20 26 3d  :      u.ah.a &=
1d0ea 20 75 2e 61 68 2e 62 3b 20 20 20 20 20 62 72 65   u.ah.b;     bre
1d0eb 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f  ak;.    case OP_
1d0ec 42 69 74 4f 72 3a 20 20 20 20 20 20 20 75 2e 61  BitOr:       u.a
1d0ed 68 2e 61 20 7c 3d 20 75 2e 61 68 2e 62 3b 20 20  h.a |= u.ah.b;  
1d0ee 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
1d0ef 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a  se OP_ShiftLeft:
1d0f0 20 20 20 75 2e 61 68 2e 61 20 3c 3c 3d 20 75 2e     u.ah.a <<= u.
1d0f1 61 68 2e 62 3b 20 20 20 20 62 72 65 61 6b 3b 0a  ah.b;    break;.
1d0f2 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 61 73      default:  as
1d0f3 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  sert( pOp->opcod
1d0f4 65 3d 3d 4f 50 5f 53 68 69 66 74 52 69 67 68 74  e==OP_ShiftRight
1d0f5 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
1d0f6 20 20 20 20 20 20 20 20 20 20 20 20 20 75 2e 61               u.a
1d0f7 68 2e 61 20 3e 3e 3d 20 75 2e 61 68 2e 62 3b 20  h.a >>= u.ah.b; 
1d0f8 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20     break;.  }.  
1d0f9 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 68  pOut->u.i = u.ah
1d0fa 2e 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65  .a;.  MemSetType
1d0fb 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1d0fc 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  nt);.  break;.}.
1d0fd 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 64 64 49  ./* Opcode: AddI
1d0fe 6d 6d 20 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  mm  P1 P2 * * *.
1d0ff 2a 2a 20 0a 2a 2a 20 41 64 64 20 74 68 65 20 63  ** .** Add the c
1d100 6f 6e 73 74 61 6e 74 20 50 32 20 74 6f 20 74 68  onstant P2 to th
1d101 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1d102 74 65 72 20 50 31 2e 0a 2a 2a 20 54 68 65 20 72  ter P1..** The r
1d103 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73 20  esult is always 
1d104 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a  an integer..**.*
1d105 2a 20 54 6f 20 66 6f 72 63 65 20 61 6e 79 20 72  * To force any r
1d106 65 67 69 73 74 65 72 20 74 6f 20 62 65 20 61 6e  egister to be an
1d107 20 69 6e 74 65 67 65 72 2c 20 6a 75 73 74 20 61   integer, just a
1d108 64 64 20 30 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  dd 0..*/.case OP
1d109 5f 41 64 64 49 6d 6d 3a 20 7b 20 20 20 20 20 20  _AddImm: {      
1d10a 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a 2f 0a        /* in1 */.
1d10b 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
1d10c 4f 70 2d 3e 70 31 5d 3b 0a 20 20 73 71 6c 69 74  Op->p1];.  sqlit
1d10d 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72  e3VdbeMemInteger
1d10e 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70 49 6e  ify(pIn1);.  pIn
1d10f 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70  1->u.i += pOp->p
1d110 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  2;.  break;.}../
1d111 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73 74 42 65  * Opcode: MustBe
1d112 49 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  Int P1 P2 * * *.
1d113 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65  ** .** Force the
1d114 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1d115 65 72 20 50 31 20 74 6f 20 62 65 20 61 6e 20 69  er P1 to be an i
1d116 6e 74 65 67 65 72 2e 20 20 49 66 20 74 68 65 20  nteger.  If the 
1d117 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20 69  value.** in P1 i
1d118 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67 65 72  s not an integer
1d119 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62 65 20 63   and cannot be c
1d11a 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e  onverted into an
1d11b 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 69 74 68   integer.** with
1d11c 6f 75 74 20 64 61 74 61 20 6c 6f 73 73 2c 20 74  out data loss, t
1d11d 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61  hen jump immedia
1d11e 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f 72 20 69  tely to P2, or i
1d11f 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61 69 73 65  f P2==0.** raise
1d120 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41   an SQLITE_MISMA
1d121 54 43 48 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a  TCH exception..*
1d122 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73 74 42 65  /.case OP_MustBe
1d123 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  Int: {          
1d124 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a    /* jump, in1 *
1d125 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  /.  pIn1 = &aMem
1d126 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 70 70  [pOp->p1];.  app
1d127 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 31 2c  lyAffinity(pIn1,
1d128 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45   SQLITE_AFF_NUME
1d129 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  RIC, encoding);.
1d12a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1d12b 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30  gs & MEM_Int)==0
1d12c 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   ){.    if( pOp-
1d12d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  >p2==0 ){.      
1d12e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 4d  rc = SQLITE_MISM
1d12f 41 54 43 48 3b 0a 20 20 20 20 20 20 67 6f 74 6f  ATCH;.      goto
1d130 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1d131 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ror;.    }else{.
1d132 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1d133 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  p2 - 1;.    }.  
1d134 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65 6d 53 65  }else{.    MemSe
1d135 74 54 79 70 65 46 6c 61 67 28 70 49 6e 31 2c 20  tTypeFlag(pIn1, 
1d136 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20  MEM_Int);.  }.  
1d137 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1d138 6f 64 65 3a 20 52 65 61 6c 41 66 66 69 6e 69 74  ode: RealAffinit
1d139 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  y P1 * * * *.**.
1d13a 2a 2a 20 49 66 20 72 65 67 69 73 74 65 72 20 50  ** If register P
1d13b 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65 67  1 holds an integ
1d13c 65 72 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f  er convert it to
1d13d 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a   a real value..*
1d13e 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65  *.** This opcode
1d13f 20 69 73 20 75 73 65 64 20 77 68 65 6e 20 65 78   is used when ex
1d140 74 72 61 63 74 69 6e 67 20 69 6e 66 6f 72 6d 61  tracting informa
1d141 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63 6f 6c 75  tion from a colu
1d142 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73 20 52  mn that.** has R
1d143 45 41 4c 20 61 66 66 69 6e 69 74 79 2e 20 20 53  EAL affinity.  S
1d144 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  uch column value
1d145 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 73  s may still be s
1d146 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69 6e 74 65  tored as.** inte
1d147 67 65 72 73 2c 20 66 6f 72 20 73 70 61 63 65 20  gers, for space 
1d148 65 66 66 69 63 69 65 6e 63 79 2c 20 62 75 74 20  efficiency, but 
1d149 61 66 74 65 72 20 65 78 74 72 61 63 74 69 6f 6e  after extraction
1d14a 20 77 65 20 77 61 6e 74 20 74 68 65 6d 0a 2a 2a   we want them.**
1d14b 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 20 61 20   to have only a 
1d14c 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a 63  real value..*/.c
1d14d 61 73 65 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e  ase OP_RealAffin
1d14e 69 74 79 3a 20 7b 20 20 20 20 20 20 20 20 20 20  ity: {          
1d14f 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20 2a          /* in1 *
1d150 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  /.  pIn1 = &aMem
1d151 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28  [pOp->p1];.  if(
1d152 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
1d153 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71  EM_Int ){.    sq
1d154 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c  lite3VdbeMemReal
1d155 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20  ify(pIn1);.  }. 
1d156 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64   break;.}..#ifnd
1d157 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
1d158 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54  AST./* Opcode: T
1d159 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a 20 2a  oText P1 * * * *
1d15a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65  .**.** Force the
1d15b 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1d15c 65 72 20 50 31 20 74 6f 20 62 65 20 74 65 78 74  er P1 to be text
1d15d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  ..** If the valu
1d15e 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f  e is numeric, co
1d15f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74  nvert it to a st
1d160 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65 0a 2a  ring using the.*
1d161 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20  * equivalent of 
1d162 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f 62 20  printf().  Blob 
1d163 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63 68 61  values are uncha
1d164 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 65 20  nged and.** are 
1d165 61 66 74 65 72 77 61 72 64 73 20 73 69 6d 70 6c  afterwards simpl
1d166 79 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  y interpreted as
1d167 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e   text..**.** A N
1d168 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74  ULL value is not
1d169 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73   changed by this
1d16a 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65   routine.  It re
1d16b 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63  mains NULL..*/.c
1d16c 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20 7b  ase OP_ToText: {
1d16d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d16e 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1d16f 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a 2f 0a  TO_TEXT, in1 */.
1d170 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
1d171 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 70  Op->p1];.  if( p
1d172 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1d173 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20  _Null ) break;. 
1d174 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 74 72   assert( MEM_Str
1d175 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20  ==(MEM_Blob>>3) 
1d176 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73  );.  pIn1->flags
1d177 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73   |= (pIn1->flags
1d178 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20  &MEM_Blob)>>3;. 
1d179 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70   applyAffinity(p
1d17a 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  In1, SQLITE_AFF_
1d17b 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  TEXT, encoding);
1d17c 0a 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c  .  rc = ExpandBl
1d17d 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 73 73 65  ob(pIn1);.  asse
1d17e 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  rt( pIn1->flags 
1d17f 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d  & MEM_Str || db-
1d180 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
1d181 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  .  pIn1->flags &
1d182 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  = ~(MEM_Int|MEM_
1d183 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 7c 4d 45  Real|MEM_Blob|ME
1d184 4d 5f 5a 65 72 6f 29 3b 0a 20 20 55 50 44 41 54  M_Zero);.  UPDAT
1d185 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70  E_MAX_BLOBSIZE(p
1d186 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  In1);.  break;.}
1d187 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 42  ../* Opcode: ToB
1d188 6c 6f 62 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  lob P1 * * * *.*
1d189 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76  *.** Force the v
1d18a 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1d18b 20 50 31 20 74 6f 20 62 65 20 61 20 42 4c 4f 42   P1 to be a BLOB
1d18c 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  ..** If the valu
1d18d 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f  e is numeric, co
1d18e 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74  nvert it to a st
1d18f 72 69 6e 67 20 66 69 72 73 74 2e 0a 2a 2a 20 53  ring first..** S
1d190 74 72 69 6e 67 73 20 61 72 65 20 73 69 6d 70 6c  trings are simpl
1d191 79 20 72 65 69 6e 74 65 72 70 72 65 74 65 64 20  y reinterpreted 
1d192 61 73 20 62 6c 6f 62 73 20 77 69 74 68 20 6e 6f  as blobs with no
1d193 20 63 68 61 6e 67 65 0a 2a 2a 20 74 6f 20 74 68   change.** to th
1d194 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74  e underlying dat
1d195 61 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20  a..**.** A NULL 
1d196 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61  value is not cha
1d197 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  nged by this rou
1d198 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e  tine.  It remain
1d199 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20  s NULL..*/.case 
1d19a 4f 50 5f 54 6f 42 6c 6f 62 3a 20 7b 20 20 20 20  OP_ToBlob: {    
1d19b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d19c 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42   same as TK_TO_B
1d19d 4c 4f 42 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49  LOB, in1 */.  pI
1d19e 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  n1 = &aMem[pOp->
1d19f 70 31 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d  p1];.  if( pIn1-
1d1a0 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1d1a1 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28  l ) break;.  if(
1d1a2 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1d1a3 4d 45 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a  MEM_Blob)==0 ){.
1d1a4 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74      applyAffinit
1d1a5 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41  y(pIn1, SQLITE_A
1d1a6 46 46 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e  FF_TEXT, encodin
1d1a7 67 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  g);.    assert( 
1d1a8 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1d1a9 4d 5f 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  M_Str || db->mal
1d1aa 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20  locFailed );.   
1d1ab 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
1d1ac 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b  pIn1, MEM_Blob);
1d1ad 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49  .  }else{.    pI
1d1ae 6e 31 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d  n1->flags &= ~(M
1d1af 45 4d 5f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d  EM_TypeMask&~MEM
1d1b0 5f 42 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50  _Blob);.  }.  UP
1d1b1 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1d1b2 45 28 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b  E(pIn1);.  break
1d1b3 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1d1b4 54 6f 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a  ToNumeric P1 * *
1d1b5 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65   * *.**.** Force
1d1b6 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1d1b7 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65 20  gister P1 to be 
1d1b8 6e 75 6d 65 72 69 63 20 28 65 69 74 68 65 72 20  numeric (either 
1d1b9 61 6e 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72  an.** integer or
1d1ba 20 61 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e   a floating-poin
1d1bb 74 20 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66  t number.).** If
1d1bc 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65   the value is te
1d1bd 78 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20  xt or blob, try 
1d1be 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f  to convert it to
1d1bf 20 61 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a   an using the.**
1d1c0 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61   equivalent of a
1d1c1 74 6f 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20  toi() or atof() 
1d1c2 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e  and store 0 if n
1d1c3 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f  o such conversio
1d1c4 6e 20 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c  n .** is possibl
1d1c5 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20  e..**.** A NULL 
1d1c6 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61  value is not cha
1d1c7 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f 75  nged by this rou
1d1c8 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e  tine.  It remain
1d1c9 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20  s NULL..*/.case 
1d1ca 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20  OP_ToNumeric: { 
1d1cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d1cc 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
1d1cd 4f 5f 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a  O_NUMERIC, in1 *
1d1ce 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d  /.  pIn1 = &aMem
1d1cf 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28  [pOp->p1];.  if(
1d1d0 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1d1d1 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e  (MEM_Null|MEM_In
1d1d2 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d 3d 30 20  t|MEM_Real))==0 
1d1d3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1d1d4 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28 70 49  beMemNumerify(pI
1d1d5 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  n1);.  }.  break
1d1d6 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1d1d7 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a  LITE_OMIT_CAST *
1d1d8 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f  /../* Opcode: To
1d1d9 49 6e 74 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  Int P1 * * * *.*
1d1da 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76  *.** Force the v
1d1db 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1d1dc 20 50 31 20 62 65 20 61 6e 20 69 6e 74 65 67 65   P1 be an intege
1d1dd 72 2e 20 20 49 66 0a 2a 2a 20 54 68 65 20 76 61  r.  If.** The va
1d1de 6c 75 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79  lue is currently
1d1df 20 61 20 72 65 61 6c 20 6e 75 6d 62 65 72 2c 20   a real number, 
1d1e0 64 72 6f 70 20 69 74 73 20 66 72 61 63 74 69 6f  drop its fractio
1d1e1 6e 61 6c 20 70 61 72 74 2e 0a 2a 2a 20 49 66 20  nal part..** If 
1d1e2 74 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78  the value is tex
1d1e3 74 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74  t or blob, try t
1d1e4 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20  o convert it to 
1d1e5 61 6e 20 69 6e 74 65 67 65 72 20 75 73 69 6e 67  an integer using
1d1e6 20 74 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65   the.** equivale
1d1e7 6e 74 20 6f 66 20 61 74 6f 69 28 29 20 61 6e 64  nt of atoi() and
1d1e8 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20 73   store 0 if no s
1d1e9 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69  uch conversion i
1d1ea 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a  s possible..**.*
1d1eb 2a 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69  * A NULL value i
1d1ec 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79  s not changed by
1d1ed 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20   this routine.  
1d1ee 49 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e  It remains NULL.
1d1ef 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 49 6e  .*/.case OP_ToIn
1d1f0 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  t: {            
1d1f1 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1d1f2 20 54 4b 5f 54 4f 5f 49 4e 54 2c 20 69 6e 31 20   TK_TO_INT, in1 
1d1f3 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  */.  pIn1 = &aMe
1d1f4 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66  m[pOp->p1];.  if
1d1f5 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  ( (pIn1->flags &
1d1f6 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b   MEM_Null)==0 ){
1d1f7 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1d1f8 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49  MemIntegerify(pI
1d1f9 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  n1);.  }.  break
1d1fa 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
1d1fb 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a  ITE_OMIT_CAST./*
1d1fc 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65 61 6c 20   Opcode: ToReal 
1d1fd 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
1d1fe 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
1d1ff 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1d200 74 6f 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67  to be a floating
1d201 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a   point number..*
1d202 2a 20 49 66 20 54 68 65 20 76 61 6c 75 65 20 69  * If The value i
1d203 73 20 63 75 72 72 65 6e 74 6c 79 20 61 6e 20 69  s currently an i
1d204 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65 72 74 20  nteger, convert 
1d205 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61  it..** If the va
1d206 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72 20 62  lue is text or b
1d207 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e 76  lob, try to conv
1d208 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69 6e 74  ert it to an int
1d209 65 67 65 72 20 75 73 69 6e 67 20 74 68 65 0a 2a  eger using the.*
1d20a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20  * equivalent of 
1d20b 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f 72 65  atoi() and store
1d20c 20 30 2e 30 20 69 66 20 6e 6f 20 73 75 63 68 20   0.0 if no such 
1d20d 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f  conversion is po
1d20e 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20  ssible..**.** A 
1d20f 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f  NULL value is no
1d210 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  t changed by thi
1d211 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72  s routine.  It r
1d212 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  emains NULL..*/.
1d213 63 61 73 65 20 4f 50 5f 54 6f 52 65 61 6c 3a 20  case OP_ToReal: 
1d214 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
1d215 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1d216 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31 20 2a 2f  _TO_REAL, in1 */
1d217 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b  .  pIn1 = &aMem[
1d218 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20  pOp->p1];.  if( 
1d219 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn1->flags & M
1d21a 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20  EM_Null)==0 ){. 
1d21b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1d21c 6d 52 65 61 6c 69 66 79 28 70 49 6e 31 29 3b 0a  mRealify(pIn1);.
1d21d 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1d21e 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1d21f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f 2a  OMIT_CAST */../*
1d220 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50 31 20 50   Opcode: Lt P1 P
1d221 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
1d222 20 43 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c   Compare the val
1d223 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72 20  ues in register 
1d224 50 31 20 61 6e 64 20 50 33 2e 20 20 49 66 20 72  P1 and P3.  If r
1d225 65 67 28 50 33 29 3c 72 65 67 28 50 31 29 20 74  eg(P3)<reg(P1) t
1d226 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 61  hen.** jump to a
1d227 64 64 72 65 73 73 20 50 32 2e 20 20 0a 2a 2a 0a  ddress P2.  .**.
1d228 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54 45  ** If the SQLITE
1d229 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20  _JUMPIFNULL bit 
1d22a 6f 66 20 50 35 20 69 73 20 73 65 74 20 61 6e 64  of P5 is set and
1d22b 20 65 69 74 68 65 72 20 72 65 67 28 50 31 29 20   either reg(P1) 
1d22c 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29 20 69 73  or.** reg(P3) is
1d22d 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b 65 20   NULL then take 
1d22e 74 68 65 20 6a 75 6d 70 2e 20 20 49 66 20 74 68  the jump.  If th
1d22f 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  e SQLITE_JUMPIFN
1d230 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69 73 20 63  ULL .** bit is c
1d231 6c 65 61 72 20 74 68 65 6e 20 66 61 6c 6c 20 74  lear then fall t
1d232 68 72 75 20 69 66 20 65 69 74 68 65 72 20 6f 70  hru if either op
1d233 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2e 0a 2a  erand is NULL..*
1d234 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
1d235 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74 69 6f 6e  AFF_MASK portion
1d236 20 6f 66 20 50 35 20 6d 75 73 74 20 62 65 20 61   of P5 must be a
1d237 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 72 61  n affinity chara
1d238 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c 49 54 45  cter -.** SQLITE
1d239 5f 41 46 46 5f 54 45 58 54 2c 20 53 51 4c 49 54  _AFF_TEXT, SQLIT
1d23a 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 2c 20 61  E_AFF_INTEGER, a
1d23b 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 41 6e 20  nd so forth. An 
1d23c 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
1d23d 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65 20 62 6f  .** to coerce bo
1d23e 74 68 20 69 6e 70 75 74 73 20 61 63 63 6f 72 64  th inputs accord
1d23f 69 6e 67 20 74 6f 20 74 68 69 73 20 61 66 66 69  ing to this affi
1d240 6e 69 74 79 20 62 65 66 6f 72 65 20 74 68 65 0a  nity before the.
1d241 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  ** comparison is
1d242 20 6d 61 64 65 2e 20 49 66 20 74 68 65 20 53 51   made. If the SQ
1d243 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 69 73  LITE_AFF_MASK is
1d244 20 30 78 30 30 2c 20 74 68 65 6e 20 6e 75 6d 65   0x00, then nume
1d245 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20  ric.** affinity 
1d246 69 73 20 75 73 65 64 2e 20 4e 6f 74 65 20 74 68  is used. Note th
1d247 61 74 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  at the affinity 
1d248 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 72 65 20  conversions are 
1d249 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63 6b 20 69  stored.** back i
1d24a 6e 74 6f 20 74 68 65 20 69 6e 70 75 74 20 72 65  nto the input re
1d24b 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50  gisters P1 and P
1d24c 33 2e 20 20 53 6f 20 74 68 69 73 20 6f 70 63 6f  3.  So this opco
1d24d 64 65 20 63 61 6e 20 63 61 75 73 65 0a 2a 2a 20  de can cause.** 
1d24e 70 65 72 73 69 73 74 65 6e 74 20 63 68 61 6e 67  persistent chang
1d24f 65 73 20 74 6f 20 72 65 67 69 73 74 65 72 73 20  es to registers 
1d250 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a 0a 2a 2a  P1 and P3..**.**
1d251 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e 76 65 72   Once any conver
1d252 73 69 6f 6e 73 20 68 61 76 65 20 74 61 6b 65 6e  sions have taken
1d253 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e 65 69 74   place, and neit
1d254 68 65 72 20 76 61 6c 75 65 20 69 73 20 4e 55 4c  her value is NUL
1d255 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  L, .** the value
1d256 73 20 61 72 65 20 63 6f 6d 70 61 72 65 64 2e 20  s are compared. 
1d257 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61  If both values a
1d258 72 65 20 62 6c 6f 62 73 20 74 68 65 6e 20 6d 65  re blobs then me
1d259 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20 75 73 65  mcmp() is.** use
1d25a 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  d to determine t
1d25b 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  he results of th
1d25c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 49  e comparison.  I
1d25d 66 20 62 6f 74 68 20 76 61 6c 75 65 73 0a 2a 2a  f both values.**
1d25e 20 61 72 65 20 74 65 78 74 2c 20 74 68 65 6e 20   are text, then 
1d25f 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
1d260 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69  collating functi
1d261 6f 6e 20 73 70 65 63 69 66 69 65 64 20 69 6e 0a  on specified in.
1d262 2a 2a 20 50 34 20 69 73 20 20 75 73 65 64 20 74  ** P4 is  used t
1d263 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 69  o do the compari
1d264 73 6f 6e 2e 20 20 49 66 20 50 34 20 69 73 20 6e  son.  If P4 is n
1d265 6f 74 20 73 70 65 63 69 66 69 65 64 20 74 68 65  ot specified the
1d266 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29 20 69 73  n.** memcmp() is
1d267 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65   used to compare
1d268 20 74 65 78 74 20 73 74 72 69 6e 67 2e 20 20 49   text string.  I
1d269 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72  f both values ar
1d26a 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c 20 74 68  e.** numeric, th
1d26b 65 6e 20 61 20 6e 75 6d 65 72 69 63 20 63 6f 6d  en a numeric com
1d26c 70 61 72 69 73 6f 6e 20 69 73 20 75 73 65 64 2e  parison is used.
1d26d 20 49 66 20 74 68 65 20 74 77 6f 20 76 61 6c 75   If the two valu
1d26e 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20 64 69 66  es.** are of dif
1d26f 66 65 72 65 6e 74 20 74 79 70 65 73 2c 20 74 68  ferent types, th
1d270 65 6e 20 6e 75 6d 62 65 72 73 20 61 72 65 20 63  en numbers are c
1d271 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 73 20 74  onsidered less t
1d272 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67 73 20 61  han.** strings a
1d273 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 63  nd strings are c
1d274 6f 6e 73 69 64 65 72 65 64 20 6c 65 73 73 20 74  onsidered less t
1d275 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a 0a 2a 2a  han blobs..**.**
1d276 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 53   If the SQLITE_S
1d277 54 4f 52 45 50 32 20 62 69 74 20 6f 66 20 50 35  TOREP2 bit of P5
1d278 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 64 6f   is set, then do
1d279 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49 6e 73 74   not jump.  Inst
1d27a 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65 20 61 20  ead,.** store a 
1d27b 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c 74 20 28  boolean result (
1d27c 65 69 74 68 65 72 20 30 2c 20 6f 72 20 31 2c 20  either 0, or 1, 
1d27d 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72 65 67 69  or NULL) in regi
1d27e 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f 2a 20 4f  ster P2..*/./* O
1d27f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20 50 32 20  pcode: Ne P1 P2 
1d280 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54  P3 P4 P5.**.** T
1d281 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c  his works just l
1d282 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64  ike the Lt opcod
1d283 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68  e except that th
1d284 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20  e jump is taken 
1d285 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e  if.** the operan
1d286 64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 20  ds in registers 
1d287 50 31 20 61 6e 64 20 50 33 20 61 72 65 20 6e 6f  P1 and P3 are no
1d288 74 20 65 71 75 61 6c 2e 20 20 53 65 65 20 74 68  t equal.  See th
1d289 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 0a  e Lt opcode for.
1d28a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  ** additional in
1d28b 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  formation..**.**
1d28c 20 49 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45   If SQLITE_NULLE
1d28d 51 20 69 73 20 73 65 74 20 69 6e 20 50 35 20 74  Q is set in P5 t
1d28e 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f  hen the result o
1d28f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  f comparison is 
1d290 61 6c 77 61 79 73 20 65 69 74 68 65 72 0a 2a 2a  always either.**
1d291 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 20 61   true or false a
1d292 6e 64 20 69 73 20 6e 65 76 65 72 20 4e 55 4c 4c  nd is never NULL
1d293 2e 20 20 49 66 20 62 6f 74 68 20 6f 70 65 72 61  .  If both opera
1d294 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20 74 68 65  nds are NULL the
1d295 6e 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  n the result.** 
1d296 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  of comparison is
1d297 20 66 61 6c 73 65 2e 20 20 49 66 20 65 69 74 68   false.  If eith
1d298 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55  er operand is NU
1d299 4c 4c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  LL then the resu
1d29a 6c 74 20 69 73 20 74 72 75 65 2e 0a 2a 2a 20 49  lt is true..** I
1d29b 66 20 6e 65 69 74 68 65 72 20 6f 70 65 72 61 6e  f neither operan
1d29c 64 20 69 73 20 4e 55 4c 4c 20 74 68 65 20 74 68  d is NULL the th
1d29d 65 20 72 65 73 75 6c 74 20 69 73 20 74 68 65 20  e result is the 
1d29e 73 61 6d 65 20 61 73 20 69 74 20 77 6f 75 6c 64  same as it would
1d29f 20 62 65 20 69 66 0a 2a 2a 20 74 68 65 20 53 51   be if.** the SQ
1d2a0 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66 6c 61 67  LITE_NULLEQ flag
1d2a1 20 77 65 72 65 20 6f 6d 69 74 74 65 64 20 66 72   were omitted fr
1d2a2 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63  om P5..*/./* Opc
1d2a3 6f 64 65 3a 20 45 71 20 50 31 20 50 32 20 50 33  ode: Eq P1 P2 P3
1d2a4 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69   P4 P5.**.** Thi
1d2a5 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b  s works just lik
1d2a6 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20  e the Lt opcode 
1d2a7 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
1d2a8 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66  jump is taken if
1d2a9 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73  .** the operands
1d2aa 20 69 6e 20 72 65 67 69 73 74 65 72 73 20 50 31   in registers P1
1d2ab 20 61 6e 64 20 50 33 20 61 72 65 20 65 71 75 61   and P3 are equa
1d2ac 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 4c 74  l..** See the Lt
1d2ad 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69   opcode for addi
1d2ae 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
1d2af 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c  on..**.** If SQL
1d2b0 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73 20 73 65  ITE_NULLEQ is se
1d2b1 74 20 69 6e 20 50 35 20 74 68 65 6e 20 74 68 65  t in P5 then the
1d2b2 20 72 65 73 75 6c 74 20 6f 66 20 63 6f 6d 70 61   result of compa
1d2b3 72 69 73 6f 6e 20 69 73 20 61 6c 77 61 79 73 20  rison is always 
1d2b4 65 69 74 68 65 72 0a 2a 2a 20 74 72 75 65 20 6f  either.** true o
1d2b5 72 20 66 61 6c 73 65 20 61 6e 64 20 69 73 20 6e  r false and is n
1d2b6 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49 66 20 62  ever NULL.  If b
1d2b7 6f 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65  oth operands are
1d2b8 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72   NULL then the r
1d2b9 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63 6f 6d 70  esult.** of comp
1d2ba 61 72 69 73 6f 6e 20 69 73 20 74 72 75 65 2e 20  arison is true. 
1d2bb 20 49 66 20 65 69 74 68 65 72 20 6f 70 65 72 61   If either opera
1d2bc 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  nd is NULL then 
1d2bd 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 66 61  the result is fa
1d2be 6c 73 65 2e 0a 2a 2a 20 49 66 20 6e 65 69 74 68  lse..** If neith
1d2bf 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55  er operand is NU
1d2c0 4c 4c 20 74 68 65 20 74 68 65 20 72 65 73 75 6c  LL the the resul
1d2c1 74 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  t is the same as
1d2c2 20 69 74 20 77 6f 75 6c 64 20 62 65 20 69 66 0a   it would be if.
1d2c3 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 55  ** the SQLITE_NU
1d2c4 4c 4c 45 51 20 66 6c 61 67 20 77 65 72 65 20 6f  LLEQ flag were o
1d2c5 6d 69 74 74 65 64 20 66 72 6f 6d 20 50 35 2e 0a  mitted from P5..
1d2c6 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 65  */./* Opcode: Le
1d2c7 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
1d2c8 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73  **.** This works
1d2c9 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c   just like the L
1d2ca 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20  t opcode except 
1d2cb 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73  that the jump is
1d2cc 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65   taken if.** the
1d2cd 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
1d2ce 73 74 65 72 20 50 33 20 69 73 20 6c 65 73 73 20  ster P3 is less 
1d2cf 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
1d2d0 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a   the content of.
1d2d1 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31 2e 20  ** register P1. 
1d2d2 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70 63 6f   See the Lt opco
1d2d3 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  de for additiona
1d2d4 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
1d2d5 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47 74 20  /./* Opcode: Gt 
1d2d6 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1d2d7 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20  *.** This works 
1d2d8 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20 4c 74  just like the Lt
1d2d9 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74 20 74   opcode except t
1d2da 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69 73 20  hat the jump is 
1d2db 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20  taken if.** the 
1d2dc 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73  content of regis
1d2dd 74 65 72 20 50 33 20 69 73 20 67 72 65 61 74 65  ter P3 is greate
1d2de 72 20 74 68 61 6e 20 74 68 65 20 63 6f 6e 74 65  r than the conte
1d2df 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65  nt of.** registe
1d2e0 72 20 50 31 2e 20 20 53 65 65 20 74 68 65 20 4c  r P1.  See the L
1d2e1 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64  t opcode for add
1d2e2 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
1d2e3 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ion..*/./* Opcod
1d2e4 65 3a 20 47 65 20 50 31 20 50 32 20 50 33 20 50  e: Ge P1 P2 P3 P
1d2e5 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  4 P5.**.** This 
1d2e6 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20  works just like 
1d2e7 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78  the Lt opcode ex
1d2e8 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75  cept that the ju
1d2e9 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a  mp is taken if.*
1d2ea 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
1d2eb 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20   register P3 is 
1d2ec 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
1d2ed 65 71 75 61 6c 20 74 6f 20 74 68 65 20 63 6f 6e  equal to the con
1d2ee 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73  tent of.** regis
1d2ef 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 65  ter P1.  See the
1d2f0 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61   Lt opcode for a
1d2f1 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1d2f2 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ation..*/.case O
1d2f3 50 5f 45 71 3a 20 20 20 20 20 20 20 20 20 20 20  P_Eq:           
1d2f4 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1d2f5 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c  K_EQ, jump, in1,
1d2f6 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f   in3 */.case OP_
1d2f7 4e 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  Ne:             
1d2f8 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1d2f9 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69  NE, jump, in1, i
1d2fa 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 74  n3 */.case OP_Lt
1d2fb 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
1d2fc 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54  /* same as TK_LT
1d2fd 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33  , jump, in1, in3
1d2fe 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 65 3a 20   */.case OP_Le: 
1d2ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d300 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45 2c 20   same as TK_LE, 
1d301 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
1d302 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20  /.case OP_Gt:   
1d303 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
1d304 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20 6a 75  ame as TK_GT, ju
1d305 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a  mp, in1, in3 */.
1d306 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b 20 20 20  case OP_Ge: {   
1d307 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1d308 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a 75 6d 70  e as TK_GE, jump
1d309 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a 23 69  , in1, in3 */.#i
1d30a 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1d30b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d30c 74 6f 20 75 2e 61 69 20 2a 2f 0a 20 20 69 6e 74  to u.ai */.  int
1d30d 20 72 65 73 3b 20 20 20 20 20 20 20 20 20 20 20   res;           
1d30e 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68   /* Result of th
1d30f 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66 20  e comparison of 
1d310 70 49 6e 31 20 61 67 61 69 6e 73 74 20 70 49 6e  pIn1 against pIn
1d311 33 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69  3 */.  char affi
1d312 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66  nity;      /* Af
1d313 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 66 6f  finity to use fo
1d314 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a  r comparison */.
1d315 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1d316 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1d317 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a 0a 20 20  into u.ai */..  
1d318 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
1d319 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33 20 3d 20  ->p1];.  pIn3 = 
1d31a 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
1d31b 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1d31c 67 73 20 7c 20 70 49 6e 33 2d 3e 66 6c 61 67 73  gs | pIn3->flags
1d31d 29 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  )&MEM_Null ){.  
1d31e 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 62 6f 74 68    /* One or both
1d31f 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 4e 55   operands are NU
1d320 4c 4c 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f  LL */.    if( pO
1d321 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 4e  p->p5 & SQLITE_N
1d322 55 4c 4c 45 51 20 29 7b 0a 20 20 20 20 20 20 2f  ULLEQ ){.      /
1d323 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  * If SQLITE_NULL
1d324 45 51 20 69 73 20 73 65 74 20 28 77 68 69 63 68  EQ is set (which
1d325 20 77 69 6c 6c 20 6f 6e 6c 79 20 68 61 70 70 65   will only happe
1d326 6e 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 6f  n if the operato
1d327 72 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 4f 50  r is.      ** OP
1d328 5f 45 71 20 6f 72 20 4f 50 5f 4e 65 29 20 74 68  _Eq or OP_Ne) th
1d329 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  en take the jump
1d32a 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e 64 69 6e   or not dependin
1d32b 67 20 6f 6e 20 77 68 65 74 68 65 72 0a 20 20 20  g on whether.   
1d32c 20 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 62 6f 74     ** or not bot
1d32d 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 6e  h operands are n
1d32e 75 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ull..      */.  
1d32f 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1d330 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 45 71 20 7c  >opcode==OP_Eq |
1d331 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  | pOp->opcode==O
1d332 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 75 2e  P_Ne );.      u.
1d333 61 69 2e 72 65 73 20 3d 20 28 70 49 6e 31 2d 3e  ai.res = (pIn1->
1d334 66 6c 61 67 73 20 26 20 70 49 6e 33 2d 3e 66 6c  flags & pIn3->fl
1d335 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d  ags & MEM_Null)=
1d336 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  =0;.    }else{. 
1d337 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e       /* SQLITE_N
1d338 55 4c 4c 45 51 20 69 73 20 63 6c 65 61 72 20 61  ULLEQ is clear a
1d339 6e 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20  nd at least one 
1d33a 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c  operand is NULL,
1d33b 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  .      ** then t
1d33c 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77  he result is alw
1d33d 61 79 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20  ays NULL..      
1d33e 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69 73 20 74  ** The jump is t
1d33f 61 6b 65 6e 20 69 66 20 74 68 65 20 53 51 4c 49  aken if the SQLI
1d340 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62 69  TE_JUMPIFNULL bi
1d341 74 20 69 73 20 73 65 74 2e 0a 20 20 20 20 20 20  t is set..      
1d342 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  */.      if( pOp
1d343 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54  ->p5 & SQLITE_ST
1d344 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 20 20 20  OREP2 ){.       
1d345 20 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f   pOut = &aMem[pO
1d346 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20 20  p->p2];.        
1d347 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
1d348 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a  Out, MEM_Null);.
1d349 20 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52          REGISTER
1d34a 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20  _TRACE(pOp->p2, 
1d34b 70 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  pOut);.      }el
1d34c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26  se if( pOp->p5 &
1d34d 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
1d34e 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 63  LL ){.        pc
1d34f 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20   = pOp->p2-1;.  
1d350 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
1d351 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  k;.    }.  }else
1d352 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74 68 65 72  {.    /* Neither
1d353 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
1d354 2e 20 20 44 6f 20 61 20 63 6f 6d 70 61 72 69 73  .  Do a comparis
1d355 6f 6e 2e 20 2a 2f 0a 20 20 20 20 75 2e 61 69 2e  on. */.    u.ai.
1d356 61 66 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e  affinity = pOp->
1d357 70 35 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f  p5 & SQLITE_AFF_
1d358 4d 41 53 4b 3b 0a 20 20 20 20 69 66 28 20 75 2e  MASK;.    if( u.
1d359 61 69 2e 61 66 66 69 6e 69 74 79 20 29 7b 0a 20  ai.affinity ){. 
1d35a 20 20 20 20 20 61 70 70 6c 79 41 66 66 69 6e 69       applyAffini
1d35b 74 79 28 70 49 6e 31 2c 20 75 2e 61 69 2e 61 66  ty(pIn1, u.ai.af
1d35c 66 69 6e 69 74 79 2c 20 65 6e 63 6f 64 69 6e 67  finity, encoding
1d35d 29 3b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66  );.      applyAf
1d35e 66 69 6e 69 74 79 28 70 49 6e 33 2c 20 75 2e 61  finity(pIn3, u.a
1d35f 69 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f  i.affinity, enco
1d360 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28  ding);.      if(
1d361 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1d362 64 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  d ) goto no_mem;
1d363 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73 65  .    }..    asse
1d364 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  rt( pOp->p4type=
1d365 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 7c 7c 20 70  =P4_COLLSEQ || p
1d366 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d 3d 30 20  Op->p4.pColl==0 
1d367 29 3b 0a 20 20 20 20 45 78 70 61 6e 64 42 6c 6f  );.    ExpandBlo
1d368 62 28 70 49 6e 31 29 3b 0a 20 20 20 20 45 78 70  b(pIn1);.    Exp
1d369 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20  andBlob(pIn3);. 
1d36a 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 73 71     u.ai.res = sq
1d36b 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28  lite3MemCompare(
1d36c 70 49 6e 33 2c 20 70 49 6e 31 2c 20 70 4f 70 2d  pIn3, pIn1, pOp-
1d36d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a 20 20 7d 0a  >p4.pColl);.  }.
1d36e 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f    switch( pOp->o
1d36f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 73  pcode ){.    cas
1d370 65 20 4f 50 5f 45 71 3a 20 20 20 20 75 2e 61 69  e OP_Eq:    u.ai
1d371 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3d  .res = u.ai.res=
1d372 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  =0;     break;. 
1d373 20 20 20 63 61 73 65 20 4f 50 5f 4e 65 3a 20 20     case OP_Ne:  
1d374 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61    u.ai.res = u.a
1d375 69 2e 72 65 73 21 3d 30 3b 20 20 20 20 20 62 72  i.res!=0;     br
1d376 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50  eak;.    case OP
1d377 5f 4c 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73  _Lt:    u.ai.res
1d378 20 3d 20 75 2e 61 69 2e 72 65 73 3c 30 3b 20 20   = u.ai.res<0;  
1d379 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
1d37a 61 73 65 20 4f 50 5f 4c 65 3a 20 20 20 20 75 2e  ase OP_Le:    u.
1d37b 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65  ai.res = u.ai.re
1d37c 73 3c 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b  s<=0;     break;
1d37d 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 47 74 3a  .    case OP_Gt:
1d37e 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75      u.ai.res = u
1d37f 2e 61 69 2e 72 65 73 3e 30 3b 20 20 20 20 20 20  .ai.res>0;      
1d380 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75  break;.    defau
1d381 6c 74 3a 20 20 20 20 20 20 20 75 2e 61 69 2e 72  lt:       u.ai.r
1d382 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3e 3d 30  es = u.ai.res>=0
1d383 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  ;     break;.  }
1d384 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20  ..  if( pOp->p5 
1d385 26 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32  & SQLITE_STOREP2
1d386 20 29 7b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26   ){.    pOut = &
1d387 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20  aMem[pOp->p2];. 
1d388 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1d389 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
1d38a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20  ;.    pOut->u.i 
1d38b 3d 20 75 2e 61 69 2e 72 65 73 3b 0a 20 20 20 20  = u.ai.res;.    
1d38c 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1d38d 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29 3b 0a 20  Op->p2, pOut);. 
1d38e 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 69 2e   }else if( u.ai.
1d38f 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d 20  res ){.    pc = 
1d390 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20  pOp->p2-1;.  }. 
1d391 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1d392 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 69 6f  code: Permutatio
1d393 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a  n * * * P4 *.**.
1d394 2a 2a 20 53 65 74 20 74 68 65 20 70 65 72 6d 75  ** Set the permu
1d395 74 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74  tation used by t
1d396 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70  he OP_Compare op
1d397 65 72 61 74 6f 72 20 74 6f 20 62 65 20 74 68 65  erator to be the
1d398 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 6e 74   array.** of int
1d399 65 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a 2a 0a  egers in P4..**.
1d39a 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 61 74 69  ** The permutati
1d39b 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64  on is only valid
1d39c 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20   until the next 
1d39d 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20  OP_Permutation, 
1d39e 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a 20 4f  OP_Compare,.** O
1d39f 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f 52 65  P_Halt, or OP_Re
1d3a0 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 63 61  sultRow.  Typica
1d3a1 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 6d 75  lly the OP_Permu
1d3a2 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 63  tation should oc
1d3a3 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65  cur.** immediate
1d3a4 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  ly prior to the 
1d3a5 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f 0a 63  OP_Compare..*/.c
1d3a6 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69  ase OP_Permutati
1d3a7 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20  on: {.  assert( 
1d3a8 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1d3a9 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 61 73  INTARRAY );.  as
1d3aa 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61 69  sert( pOp->p4.ai
1d3ab 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 20 3d   );.  aPermute =
1d3ac 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 20 62   pOp->p4.ai;.  b
1d3ad 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d3ae 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 20 50  de: Compare P1 P
1d3af 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
1d3b0 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 74 6f  Compare to vecto
1d3b1 72 73 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  rs of registers 
1d3b2 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 67 28  in reg(P1)..reg(
1d3b3 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 74 68  P1+P3-1) (all th
1d3b4 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 20 61  is.** one "A") a
1d3b5 6e 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e 72  nd in reg(P2)..r
1d3b6 65 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42 22  eg(P2+P3-1) ("B"
1d3b7 29 2e 20 20 53 61 76 65 20 74 68 65 20 72 65 73  ).  Save the res
1d3b8 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f  ult of.** the co
1d3b9 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 65  mparison for use
1d3ba 20 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 5f   by the next OP_
1d3bb 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a 2a  Jump instruct..*
1d3bc 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65 79  *.** P4 is a Key
1d3bd 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74  Info structure t
1d3be 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c 6c  hat defines coll
1d3bf 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
1d3c0 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64 65  and sort.** orde
1d3c1 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61  rs for the compa
1d3c2 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72 6d  rison.  The perm
1d3c3 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73 20  utation applies 
1d3c4 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20  to registers.** 
1d3c5 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49 6e  only.  The KeyIn
1d3c6 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20  fo elements are 
1d3c7 75 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c 6c  used sequentiall
1d3c8 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d  y..**.** The com
1d3c9 70 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f 72  parison is a sor
1d3ca 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73 6f  t comparison, so
1d3cb 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20 65   NULLs compare e
1d3cc 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20 61  qual,.** NULLs a
1d3cd 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75 6d  re less than num
1d3ce 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61 72  bers, numbers ar
1d3cf 65 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72 69  e less than stri
1d3d0 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72 69  ngs,.** and stri
1d3d1 6e 67 73 20 61 72 65 20 6c 65 73 73 20 74 68 61  ngs are less tha
1d3d2 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73 65  n blobs..*/.case
1d3d3 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a 23   OP_Compare: {.#
1d3d4 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1d3d5 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1d3d6 6e 74 6f 20 75 2e 61 6a 20 2a 2f 0a 20 20 69 6e  nto u.aj */.  in
1d3d7 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  t n;.  int i;.  
1d3d8 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 70 32  int p1;.  int p2
1d3d9 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e 66  ;.  const KeyInf
1d3da 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69  o *pKeyInfo;.  i
1d3db 6e 74 20 69 64 78 3b 0a 20 20 43 6f 6c 6c 53 65  nt idx;.  CollSe
1d3dc 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20  q *pColl;    /* 
1d3dd 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
1d3de 63 65 20 74 6f 20 75 73 65 20 6f 6e 20 74 68 69  ce to use on thi
1d3df 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20  s term */.  int 
1d3e0 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f  bRev;          /
1d3e1 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45  * True for DESCE
1d3e2 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72  NDING sort order
1d3e3 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f   */.#endif /* lo
1d3e4 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1d3e5 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20 2a 2f  ved into u.aj */
1d3e6 0a 0a 20 20 75 2e 61 6a 2e 6e 20 3d 20 70 4f 70  ..  u.aj.n = pOp
1d3e7 2d 3e 70 33 3b 0a 20 20 75 2e 61 6a 2e 70 4b 65  ->p3;.  u.aj.pKe
1d3e8 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e  yInfo = pOp->p4.
1d3e9 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 61 73 73 65  pKeyInfo;.  asse
1d3ea 72 74 28 20 75 2e 61 6a 2e 6e 3e 30 20 29 3b 0a  rt( u.aj.n>0 );.
1d3eb 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 70    assert( u.aj.p
1d3ec 4b 65 79 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20  KeyInfo!=0 );.  
1d3ed 75 2e 61 6a 2e 70 31 20 3d 20 70 4f 70 2d 3e 70  u.aj.p1 = pOp->p
1d3ee 31 3b 0a 20 20 75 2e 61 6a 2e 70 32 20 3d 20 70  1;.  u.aj.p2 = p
1d3ef 4f 70 2d 3e 70 32 3b 0a 23 69 66 20 53 51 4c 49  Op->p2;.#if SQLI
1d3f0 54 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 61  TE_DEBUG.  if( a
1d3f1 50 65 72 6d 75 74 65 20 29 7b 0a 20 20 20 20 69  Permute ){.    i
1d3f2 6e 74 20 6b 2c 20 6d 78 20 3d 20 30 3b 0a 20 20  nt k, mx = 0;.  
1d3f3 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 75 2e 61    for(k=0; k<u.a
1d3f4 6a 2e 6e 3b 20 6b 2b 2b 29 20 69 66 28 20 61 50  j.n; k++) if( aP
1d3f5 65 72 6d 75 74 65 5b 6b 5d 3e 6d 78 20 29 20 6d  ermute[k]>mx ) m
1d3f6 78 20 3d 20 61 50 65 72 6d 75 74 65 5b 6b 5d 3b  x = aPermute[k];
1d3f7 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61  .    assert( u.a
1d3f8 6a 2e 70 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70  j.p1>0 && u.aj.p
1d3f9 31 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20  1+mx<=p->nMem+1 
1d3fa 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  );.    assert( u
1d3fb 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 2e 61 6a  .aj.p2>0 && u.aj
1d3fc 2e 70 32 2b 6d 78 3c 3d 70 2d 3e 6e 4d 65 6d 2b  .p2+mx<=p->nMem+
1d3fd 31 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  1 );.  }else{.  
1d3fe 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e 70    assert( u.aj.p
1d3ff 31 3e 30 20 26 26 20 75 2e 61 6a 2e 70 31 2b 75  1>0 && u.aj.p1+u
1d400 2e 61 6a 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31  .aj.n<=p->nMem+1
1d401 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1d402 75 2e 61 6a 2e 70 32 3e 30 20 26 26 20 75 2e 61  u.aj.p2>0 && u.a
1d403 6a 2e 70 32 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d 3e  j.p2+u.aj.n<=p->
1d404 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d 0a 23 65  nMem+1 );.  }.#e
1d405 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44  ndif /* SQLITE_D
1d406 45 42 55 47 20 2a 2f 0a 20 20 66 6f 72 28 75 2e  EBUG */.  for(u.
1d407 61 6a 2e 69 3d 30 3b 20 75 2e 61 6a 2e 69 3c 75  aj.i=0; u.aj.i<u
1d408 2e 61 6a 2e 6e 3b 20 75 2e 61 6a 2e 69 2b 2b 29  .aj.n; u.aj.i++)
1d409 7b 0a 20 20 20 20 75 2e 61 6a 2e 69 64 78 20 3d  {.    u.aj.idx =
1d40a 20 61 50 65 72 6d 75 74 65 20 3f 20 61 50 65 72   aPermute ? aPer
1d40b 6d 75 74 65 5b 75 2e 61 6a 2e 69 5d 20 3a 20 75  mute[u.aj.i] : u
1d40c 2e 61 6a 2e 69 3b 0a 20 20 20 20 52 45 47 49 53  .aj.i;.    REGIS
1d40d 54 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e 70  TER_TRACE(u.aj.p
1d40e 31 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 61 4d 65  1+u.aj.idx, &aMe
1d40f 6d 5b 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 69  m[u.aj.p1+u.aj.i
1d410 64 78 5d 29 3b 0a 20 20 20 20 52 45 47 49 53 54  dx]);.    REGIST
1d411 45 52 5f 54 52 41 43 45 28 75 2e 61 6a 2e 70 32  ER_TRACE(u.aj.p2
1d412 2b 75 2e 61 6a 2e 69 64 78 2c 20 26 61 4d 65 6d  +u.aj.idx, &aMem
1d413 5b 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64  [u.aj.p2+u.aj.id
1d414 78 5d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  x]);.    assert(
1d415 20 75 2e 61 6a 2e 69 3c 75 2e 61 6a 2e 70 4b 65   u.aj.i<u.aj.pKe
1d416 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 29 3b  yInfo->nField );
1d417 0a 20 20 20 20 75 2e 61 6a 2e 70 43 6f 6c 6c 20  .    u.aj.pColl 
1d418 3d 20 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 2d  = u.aj.pKeyInfo-
1d419 3e 61 43 6f 6c 6c 5b 75 2e 61 6a 2e 69 5d 3b 0a  >aColl[u.aj.i];.
1d41a 20 20 20 20 75 2e 61 6a 2e 62 52 65 76 20 3d 20      u.aj.bRev = 
1d41b 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 61  u.aj.pKeyInfo->a
1d41c 53 6f 72 74 4f 72 64 65 72 5b 75 2e 61 6a 2e 69  SortOrder[u.aj.i
1d41d 5d 3b 0a 20 20 20 20 69 43 6f 6d 70 61 72 65 20  ];.    iCompare 
1d41e 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70  = sqlite3MemComp
1d41f 61 72 65 28 26 61 4d 65 6d 5b 75 2e 61 6a 2e 70  are(&aMem[u.aj.p
1d420 31 2b 75 2e 61 6a 2e 69 64 78 5d 2c 20 26 61 4d  1+u.aj.idx], &aM
1d421 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e  em[u.aj.p2+u.aj.
1d422 69 64 78 5d 2c 20 75 2e 61 6a 2e 70 43 6f 6c 6c  idx], u.aj.pColl
1d423 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6d 70  );.    if( iComp
1d424 61 72 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28  are ){.      if(
1d425 20 75 2e 61 6a 2e 62 52 65 76 20 29 20 69 43 6f   u.aj.bRev ) iCo
1d426 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d 70 61 72  mpare = -iCompar
1d427 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  e;.      break;.
1d428 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 50 65 72      }.  }.  aPer
1d429 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62 72 65 61  mute = 0;.  brea
1d42a 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d42b 20 4a 75 6d 70 20 50 31 20 50 32 20 50 33 20 2a   Jump P1 P2 P3 *
1d42c 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f   *.**.** Jump to
1d42d 20 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e   the instruction
1d42e 20 61 74 20 61 64 64 72 65 73 73 20 50 31 2c 20   at address P1, 
1d42f 50 32 2c 20 6f 72 20 50 33 20 64 65 70 65 6e 64  P2, or P3 depend
1d430 69 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 0a 2a  ing on whether.*
1d431 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65  * in the most re
1d432 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20  cent OP_Compare 
1d433 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 65 20  instruction the 
1d434 50 31 20 76 65 63 74 6f 72 20 77 61 73 20 6c 65  P1 vector was le
1d435 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71 75 61 6c  ss than.** equal
1d436 20 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 20   to, or greater 
1d437 74 68 61 6e 20 74 68 65 20 50 32 20 76 65 63 74  than the P2 vect
1d438 6f 72 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  or, respectively
1d439 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4a 75 6d  ..*/.case OP_Jum
1d43a 70 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  p: {            
1d43b 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 69 66   /* jump */.  if
1d43c 28 20 69 43 6f 6d 70 61 72 65 3c 30 20 29 7b 0a  ( iCompare<0 ){.
1d43d 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 31      pc = pOp->p1
1d43e 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66   - 1;.  }else if
1d43f 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30 20 29 7b  ( iCompare==0 ){
1d440 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1d441 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  2 - 1;.  }else{.
1d442 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 33      pc = pOp->p3
1d443 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1d444 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d445 20 41 6e 64 20 50 31 20 50 32 20 50 33 20 2a 20   And P1 P2 P3 * 
1d446 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65  *.**.** Take the
1d447 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20 6f 66 20   logical AND of 
1d448 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65  the values in re
1d449 67 69 73 74 65 72 73 20 50 31 20 61 6e 64 20 50  gisters P1 and P
1d44a 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74 65 20 74  2 and.** write t
1d44b 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 72  he result into r
1d44c 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a  egister P3..**.*
1d44d 2a 20 49 66 20 65 69 74 68 65 72 20 50 31 20 6f  * If either P1 o
1d44e 72 20 50 32 20 69 73 20 30 20 28 66 61 6c 73 65  r P2 is 0 (false
1d44f 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  ) then the resul
1d450 74 20 69 73 20 30 20 65 76 65 6e 20 69 66 0a 2a  t is 0 even if.*
1d451 2a 20 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75  * the other inpu
1d452 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55  t is NULL.  A NU
1d453 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f 72 20 74  LL and true or t
1d454 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65 0a 2a 2a  wo NULLs give.**
1d455 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a   a NULL output..
1d456 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 72  */./* Opcode: Or
1d457 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1d458 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67  .** Take the log
1d459 69 63 61 6c 20 4f 52 20 6f 66 20 74 68 65 20 76  ical OR of the v
1d45a 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65  alues in registe
1d45b 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a  r P1 and P2 and.
1d45c 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 61 6e 73  ** store the ans
1d45d 77 65 72 20 69 6e 20 72 65 67 69 73 74 65 72 20  wer in register 
1d45e 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 69 74  P3..**.** If eit
1d45f 68 65 72 20 50 31 20 6f 72 20 50 32 20 69 73 20  her P1 or P2 is 
1d460 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65 29 20 74  nonzero (true) t
1d461 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69  hen the result i
1d462 73 20 31 20 28 74 72 75 65 29 0a 2a 2a 20 65 76  s 1 (true).** ev
1d463 65 6e 20 69 66 20 74 68 65 20 6f 74 68 65 72 20  en if the other 
1d464 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20  input is NULL.  
1d465 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61 6c 73 65  A NULL and false
1d466 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73 0a 2a 2a   or two NULLs.**
1d467 20 67 69 76 65 20 61 20 4e 55 4c 4c 20 6f 75 74   give a NULL out
1d468 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  put..*/.case OP_
1d469 41 6e 64 3a 20 20 20 20 20 20 20 20 20 20 20 20  And:            
1d46a 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1d46b 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  AND, in1, in2, o
1d46c 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f  ut3 */.case OP_O
1d46d 72 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  r: {            
1d46e 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f   /* same as TK_O
1d46f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74  R, in1, in2, out
1d470 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  3 */.#if 0  /* l
1d471 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d472 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a  oved into u.ak *
1d473 2f 0a 20 20 69 6e 74 20 76 31 3b 20 20 20 20 2f  /.  int v1;    /
1d474 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a 20  * Left operand: 
1d475 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52   0==FALSE, 1==TR
1d476 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f  UE, 2==UNKNOWN o
1d477 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20  r NULL */.  int 
1d478 76 32 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20  v2;    /* Right 
1d479 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46 41 4c 53  operand: 0==FALS
1d47a 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32 3d 3d 55  E, 1==TRUE, 2==U
1d47b 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c 4c 20 2a  NKNOWN or NULL *
1d47c 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  /.#endif /* loca
1d47d 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1d47e 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a 2f 0a 0a  d into u.ak */..
1d47f 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
1d480 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 70  Op->p1];.  if( p
1d481 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1d482 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 61  _Null ){.    u.a
1d483 6b 2e 76 31 20 3d 20 32 3b 0a 20 20 7d 65 6c 73  k.v1 = 2;.  }els
1d484 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d  e{.    u.ak.v1 =
1d485 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
1d486 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a 20  alue(pIn1)!=0;. 
1d487 20 7d 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65   }.  pIn2 = &aMe
1d488 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69 66  m[pOp->p2];.  if
1d489 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26 20  ( pIn2->flags & 
1d48a 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20  MEM_Null ){.    
1d48b 75 2e 61 6b 2e 76 32 20 3d 20 32 3b 0a 20 20 7d  u.ak.v2 = 2;.  }
1d48c 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e 76  else{.    u.ak.v
1d48d 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  2 = sqlite3VdbeI
1d48e 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d 30  ntValue(pIn2)!=0
1d48f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d  ;.  }.  if( pOp-
1d490 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64 20  >opcode==OP_And 
1d491 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  ){.    static co
1d492 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1d493 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d 20  r and_logic[] = 
1d494 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c  { 0, 0, 0, 0, 1,
1d495 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a 20   2, 0, 2, 2 };. 
1d496 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 61 6e 64     u.ak.v1 = and
1d497 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a 33  _logic[u.ak.v1*3
1d498 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 65 6c  +u.ak.v2];.  }el
1d499 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63  se{.    static c
1d49a 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1d49b 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d 20  ar or_logic[] = 
1d49c 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31 2c  { 0, 1, 2, 1, 1,
1d49d 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a 20   1, 2, 1, 2 };. 
1d49e 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 6f 72 5f     u.ak.v1 = or_
1d49f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a 33 2b  logic[u.ak.v1*3+
1d4a0 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 0a 20 20  u.ak.v2];.  }.  
1d4a1 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pOut = &aMem[pOp
1d4a2 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 75 2e 61  ->p3];.  if( u.a
1d4a3 6b 2e 76 31 3d 3d 32 20 29 7b 0a 20 20 20 20 4d  k.v1==2 ){.    M
1d4a4 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
1d4a5 75 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20  ut, MEM_Null);. 
1d4a6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4f 75 74   }else{.    pOut
1d4a7 2d 3e 75 2e 69 20 3d 20 75 2e 61 6b 2e 76 31 3b  ->u.i = u.ak.v1;
1d4a8 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46  .    MemSetTypeF
1d4a9 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e  lag(pOut, MEM_In
1d4aa 74 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  t);.  }.  break;
1d4ab 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  .}../* Opcode: N
1d4ac 6f 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ot P1 P2 * * *.*
1d4ad 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74  *.** Interpret t
1d4ae 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1d4af 73 74 65 72 20 50 31 20 61 73 20 61 20 62 6f 6f  ster P1 as a boo
1d4b0 6c 65 61 6e 20 76 61 6c 75 65 2e 20 20 53 74 6f  lean value.  Sto
1d4b1 72 65 20 74 68 65 0a 2a 2a 20 62 6f 6f 6c 65 61  re the.** boolea
1d4b2 6e 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 69 6e 20  n complement in 
1d4b3 72 65 67 69 73 74 65 72 20 50 32 2e 20 20 49 66  register P2.  If
1d4b4 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1d4b5 67 69 73 74 65 72 20 50 31 20 69 73 20 0a 2a 2a  gister P1 is .**
1d4b6 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 4e 55   NULL, then a NU
1d4b7 4c 4c 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  LL is stored in 
1d4b8 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e  P2..*/.case OP_N
1d4b9 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ot: {           
1d4ba 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d4bb 54 4b 5f 4e 4f 54 2c 20 69 6e 31 2c 20 6f 75 74  TK_NOT, in1, out
1d4bc 32 20 2a 2f 0a 20 20 70 49 6e 31 20 3d 20 26 61  2 */.  pIn1 = &a
1d4bd 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1d4be 70 4f 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pOut = &aMem[pOp
1d4bf 2d 3e 70 32 5d 3b 0a 20 20 69 66 28 20 70 49 6e  ->p2];.  if( pIn
1d4c0 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  1->flags & MEM_N
1d4c1 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ull ){.    sqlit
1d4c2 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
1d4c3 28 70 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  (pOut);.  }else{
1d4c4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1d4c5 4d 65 6d 53 65 74 49 6e 74 36 34 28 70 4f 75 74  MemSetInt64(pOut
1d4c6 2c 20 21 73 71 6c 69 74 65 33 56 64 62 65 49 6e  , !sqlite3VdbeIn
1d4c7 74 56 61 6c 75 65 28 70 49 6e 31 29 29 3b 0a 20  tValue(pIn1));. 
1d4c8 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1d4c9 2a 20 4f 70 63 6f 64 65 3a 20 42 69 74 4e 6f 74  * Opcode: BitNot
1d4ca 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1d4cb 2a 2a 20 49 6e 74 65 72 70 72 65 74 20 74 68 65  ** Interpret the
1d4cc 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
1d4cd 73 74 65 72 20 50 31 20 61 73 20 61 6e 20 69 6e  ster P1 as an in
1d4ce 74 65 67 65 72 2e 20 20 53 74 6f 72 65 20 74 68  teger.  Store th
1d4cf 65 0a 2a 2a 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65  e.** ones-comple
1d4d0 6d 65 6e 74 20 6f 66 20 74 68 65 20 50 31 20 76  ment of the P1 v
1d4d1 61 6c 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74  alue into regist
1d4d2 65 72 20 50 32 2e 20 20 49 66 20 50 31 20 68 6f  er P2.  If P1 ho
1d4d3 6c 64 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 74 68  lds.** a NULL th
1d4d4 65 6e 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20  en store a NULL 
1d4d5 69 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  in P2..*/.case O
1d4d6 50 5f 42 69 74 4e 6f 74 3a 20 7b 20 20 20 20 20  P_BitNot: {     
1d4d7 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1d4d8 61 73 20 54 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e  as TK_BITNOT, in
1d4d9 31 2c 20 6f 75 74 32 20 2a 2f 0a 20 20 70 49 6e  1, out2 */.  pIn
1d4da 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  1 = &aMem[pOp->p
1d4db 31 5d 3b 0a 20 20 70 4f 75 74 20 3d 20 26 61 4d  1];.  pOut = &aM
1d4dc 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69  em[pOp->p2];.  i
1d4dd 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  f( pIn1->flags &
1d4de 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1d4df 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1d4e0 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20  etNull(pOut);.  
1d4e1 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
1d4e2 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
1d4e3 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 74 65 33  4(pOut, ~sqlite3
1d4e4 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e  VdbeIntValue(pIn
1d4e5 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  1));.  }.  break
1d4e6 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1d4e7 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  If P1 P2 P3 * *.
1d4e8 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32  **.** Jump to P2
1d4e9 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   if the value in
1d4ea 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1d4eb 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65  true.  The value
1d4ec 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64   is.** is consid
1d4ed 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20  ered true if it 
1d4ee 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e  is numeric and n
1d4ef 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65  on-zero.  If the
1d4f0 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20   value.** in P1 
1d4f1 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b  is NULL then tak
1d4f2 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33  e the jump if P3
1d4f3 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20   is true..*/./* 
1d4f4 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31  Opcode: IfNot P1
1d4f5 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1d4f6 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74   Jump to P2 if t
1d4f7 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1d4f8 73 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 65  ster P1 is False
1d4f9 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a  .  The value is.
1d4fa 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ** is considered
1d4fb 20 74 72 75 65 20 69 66 20 69 74 20 68 61 73 20   true if it has 
1d4fc 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20  a numeric value 
1d4fd 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 65  of zero.  If the
1d4fe 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20   value.** in P1 
1d4ff 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b  is NULL then tak
1d500 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33  e the jump if P3
1d501 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73   is true..*/.cas
1d502 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 20  e OP_If:        
1d503 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
1d504 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f 50  , in1 */.case OP
1d505 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20  _IfNot: {       
1d506 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1d507 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  1 */.#if 0  /* l
1d508 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d509 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a  oved into u.al *
1d50a 2f 0a 20 20 69 6e 74 20 63 3b 0a 23 65 6e 64 69  /.  int c;.#endi
1d50b 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1d50c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1d50d 75 2e 61 6c 20 2a 2f 0a 20 20 70 49 6e 31 20 3d  u.al */.  pIn1 =
1d50e 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b   &aMem[pOp->p1];
1d50f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61  .  if( pIn1->fla
1d510 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b  gs & MEM_Null ){
1d511 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 70 4f  .    u.al.c = pO
1d512 70 2d 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a  p->p3;.  }else{.
1d513 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
1d514 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
1d515 54 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20 73  T.    u.al.c = s
1d516 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1d517 75 65 28 70 49 6e 31 29 21 3d 30 3b 0a 23 65 6c  ue(pIn1)!=0;.#el
1d518 73 65 0a 20 20 20 20 75 2e 61 6c 2e 63 20 3d 20  se.    u.al.c = 
1d519 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56  sqlite3VdbeRealV
1d51a 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e 30 3b  alue(pIn1)!=0.0;
1d51b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20  .#endif.    if( 
1d51c 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
1d51d 49 66 4e 6f 74 20 29 20 75 2e 61 6c 2e 63 20 3d  IfNot ) u.al.c =
1d51e 20 21 75 2e 61 6c 2e 63 3b 0a 20 20 7d 0a 20 20   !u.al.c;.  }.  
1d51f 69 66 28 20 75 2e 61 6c 2e 63 20 29 7b 0a 20 20  if( u.al.c ){.  
1d520 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31    pc = pOp->p2-1
1d521 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d522 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 4e  ../* Opcode: IsN
1d523 75 6c 6c 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a  ull P1 P2 * * *.
1d524 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32  **.** Jump to P2
1d525 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   if the value in
1d526 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1d527 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
1d528 5f 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20  _IsNull: {      
1d529 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1d52a 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75 6d 70   TK_ISNULL, jump
1d52b 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 6e 31 20  , in1 */.  pIn1 
1d52c 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  = &aMem[pOp->p1]
1d52d 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66  ;.  if( (pIn1->f
1d52e 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1d52f 21 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20  !=0 ){.    pc = 
1d530 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d  pOp->p2 - 1;.  }
1d531 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1d532 4f 70 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20  Opcode: NotNull 
1d533 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1d534 2a 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20  * Jump to P2 if 
1d535 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1d536 69 73 74 65 72 20 50 31 20 69 73 20 6e 6f 74 20  ister P1 is not 
1d537 4e 55 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73 65 20  NULL.  .*/.case 
1d538 4f 50 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20 20  OP_NotNull: {   
1d539 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
1d53a 20 61 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20   as TK_NOTNULL, 
1d53b 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70  jump, in1 */.  p
1d53c 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d  In1 = &aMem[pOp-
1d53d 3e 70 31 5d 3b 0a 20 20 69 66 28 20 28 70 49 6e  >p1];.  if( (pIn
1d53e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e  1->flags & MEM_N
1d53f 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70  ull)==0 ){.    p
1d540 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1d541 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1d542 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75  ./* Opcode: Colu
1d543 6d 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 50  mn P1 P2 P3 P4 P
1d544 35 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  5.**.** Interpre
1d545 74 20 74 68 65 20 64 61 74 61 20 74 68 61 74 20  t the data that 
1d546 63 75 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73  cursor P1 points
1d547 20 74 6f 20 61 73 20 61 20 73 74 72 75 63 74 75   to as a structu
1d548 72 65 20 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a  re built using.*
1d549 2a 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64  * the MakeRecord
1d54a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 28   instruction.  (
1d54b 53 65 65 20 74 68 65 20 4d 61 6b 65 52 65 63 6f  See the MakeReco
1d54c 72 64 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64  rd opcode for ad
1d54d 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f  ditional.** info
1d54e 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
1d54f 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20  e format of the 
1d550 64 61 74 61 2e 29 20 20 45 78 74 72 61 63 74 20  data.)  Extract 
1d551 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e  the P2-th column
1d552 0a 2a 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65  .** from this re
1d553 63 6f 72 64 2e 20 20 49 66 20 74 68 65 72 65 20  cord.  If there 
1d554 61 72 65 20 6c 65 73 73 20 74 68 61 74 20 28 50  are less that (P
1d555 32 2b 31 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20  2+1) .** values 
1d556 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65  in the record, e
1d557 78 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a  xtract a NULL..*
1d558 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 65  *.** The value e
1d559 78 74 72 61 63 74 65 64 20 69 73 20 73 74 6f 72  xtracted is stor
1d55a 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
1d55b 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  3..**.** If the 
1d55c 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20  column contains 
1d55d 66 65 77 65 72 20 74 68 61 6e 20 50 32 20 66 69  fewer than P2 fi
1d55e 65 6c 64 73 2c 20 74 68 65 6e 20 65 78 74 72 61  elds, then extra
1d55f 63 74 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a  ct a NULL.  Or,.
1d560 2a 2a 20 69 66 20 74 68 65 20 50 34 20 61 72 67  ** if the P4 arg
1d561 75 6d 65 6e 74 20 69 73 20 61 20 50 34 5f 4d 45  ument is a P4_ME
1d562 4d 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20  M use the value 
1d563 6f 66 20 74 68 65 20 50 34 20 61 72 67 75 6d 65  of the P4 argume
1d564 6e 74 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 73  nt as.** the res
1d565 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ult..**.** If th
1d566 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41  e OPFLAG_CLEARCA
1d567 43 48 45 20 62 69 74 20 69 73 20 73 65 74 20 6f  CHE bit is set o
1d568 6e 20 50 35 20 61 6e 64 20 50 31 20 69 73 20 61  n P5 and P1 is a
1d569 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 75   pseudo-table cu
1d56a 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 6e 20 74 68  rsor,.** then th
1d56b 65 20 63 61 63 68 65 20 6f 66 20 74 68 65 20 63  e cache of the c
1d56c 75 72 73 6f 72 20 69 73 20 72 65 73 65 74 20 70  ursor is reset p
1d56d 72 69 6f 72 20 74 6f 20 65 78 74 72 61 63 74 69  rior to extracti
1d56e 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a  ng the column..*
1d56f 2a 20 54 68 65 20 66 69 72 73 74 20 4f 50 5f 43  * The first OP_C
1d570 6f 6c 75 6d 6e 20 61 67 61 69 6e 73 74 20 61 20  olumn against a 
1d571 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 61 66 74  pseudo-table aft
1d572 65 72 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  er the value of 
1d573 74 68 65 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 72  the content.** r
1d574 65 67 69 73 74 65 72 20 68 61 73 20 63 68 61 6e  egister has chan
1d575 67 65 64 20 73 68 6f 75 6c 64 20 68 61 76 65 20  ged should have 
1d576 74 68 69 73 20 62 69 74 20 73 65 74 2e 0a 2a 2f  this bit set..*/
1d577 0a 63 61 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 3a  .case OP_Column:
1d578 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1d579 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d57a 65 64 20 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a  ed into u.am */.
1d57b 20 20 75 33 32 20 70 61 79 6c 6f 61 64 53 69 7a    u32 payloadSiz
1d57c 65 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  e;   /* Number o
1d57d 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72  f bytes in the r
1d57e 65 63 6f 72 64 20 2a 2f 0a 20 20 69 36 34 20 70  ecord */.  i64 p
1d57f 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f 2a  ayloadSize64; /*
1d580 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1d581 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a   in the record *
1d582 2f 0a 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20  /.  int p1;     
1d583 20 20 20 20 20 20 20 2f 2a 20 50 31 20 76 61 6c         /* P1 val
1d584 75 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65  ue of the opcode
1d585 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20   */.  int p2;   
1d586 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75           /* colu
1d587 6d 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74  mn number to ret
1d588 72 69 65 76 65 20 2a 2f 0a 20 20 56 64 62 65 43  rieve */.  VdbeC
1d589 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a  ursor *pC;    /*
1d58a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
1d58b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 65 63   */.  char *zRec
1d58c 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
1d58d 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20  ter to complete 
1d58e 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20  record-data */. 
1d58f 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1d590 3b 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65  ;   /* The BTree
1d591 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32   cursor */.  u32
1d592 20 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20   *aType;        
1d593 2f 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64  /* aType[i] hold
1d594 73 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79  s the numeric ty
1d595 70 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63  pe of the i-th c
1d596 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a  olumn */.  u32 *
1d597 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a  aOffset;      /*
1d598 20 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f   aOffset[i] is o
1d599 66 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f  ffset to start o
1d59a 66 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20  f data for i-th 
1d59b 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20  column */.  int 
1d59c 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f  nField;        /
1d59d 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c  * number of fiel
1d59e 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ds in the record
1d59f 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20   */.  int len;  
1d5a0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1d5a1 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65  length of the se
1d5a2 72 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f  rialized data fo
1d5a3 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  r the column */.
1d5a4 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
1d5a5 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
1d5a6 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a  nter */.  char *
1d5a7 7a 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20  zData;       /* 
1d5a8 50 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f  Part of the reco
1d5a9 72 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64  rd being decoded
1d5aa 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74   */.  Mem *pDest
1d5ab 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72  ;        /* Wher
1d5ac 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65  e to write the e
1d5ad 78 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a  xtracted value *
1d5ae 2f 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20  /.  Mem sMem;   
1d5af 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74         /* For st
1d5b0 6f 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  oring the record
1d5b1 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a   being decoded *
1d5b2 2f 0a 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20  /.  u8 *zIdx;   
1d5b3 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
1d5b4 69 6e 74 6f 20 68 65 61 64 65 72 20 2a 2f 0a 20  into header */. 
1d5b5 20 75 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20   u8 *zEndHdr;   
1d5b6 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
1d5b7 6f 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74  o first byte aft
1d5b8 65 72 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f  er the header */
1d5b9 0a 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20 20  .  u32 offset;  
1d5ba 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1d5bb 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a 2f  into the data */
1d5bc 0a 20 20 75 36 34 20 6f 66 66 73 65 74 36 34 3b  .  u64 offset64;
1d5bd 20 20 20 20 20 20 2f 2a 20 36 34 2d 62 69 74 20        /* 64-bit 
1d5be 6f 66 66 73 65 74 2e 20 20 36 34 20 62 69 74 73  offset.  64 bits
1d5bf 20 6e 65 65 64 65 64 20 74 6f 20 63 61 74 63 68   needed to catch
1d5c0 20 6f 76 65 72 66 6c 6f 77 20 2a 2f 0a 20 20 69   overflow */.  i
1d5c1 6e 74 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20  nt szHdr;       
1d5c2 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
1d5c3 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 69 65   header size fie
1d5c4 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66 20 72  ld at start of r
1d5c5 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 61  ecord */.  int a
1d5c6 76 61 69 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a  vail;         /*
1d5c7 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
1d5c8 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61   of available da
1d5c9 74 61 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65  ta */.  Mem *pRe
1d5ca 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50 73  g;         /* Ps
1d5cb 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 75 74 20  eudoTable input 
1d5cc 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 65 6e 64  register */.#end
1d5cd 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1d5ce 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1d5cf 20 75 2e 61 6d 20 2a 2f 0a 0a 0a 20 20 75 2e 61   u.am */...  u.a
1d5d0 6d 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  m.p1 = pOp->p1;.
1d5d1 20 20 75 2e 61 6d 2e 70 32 20 3d 20 70 4f 70 2d    u.am.p2 = pOp-
1d5d2 3e 70 32 3b 0a 20 20 75 2e 61 6d 2e 70 43 20 3d  >p2;.  u.am.pC =
1d5d3 20 30 3b 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e   0;.  memset(&u.
1d5d4 61 6d 2e 73 4d 65 6d 2c 20 30 2c 20 73 69 7a 65  am.sMem, 0, size
1d5d5 6f 66 28 75 2e 61 6d 2e 73 4d 65 6d 29 29 3b 0a  of(u.am.sMem));.
1d5d6 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70    assert( u.am.p
1d5d7 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1d5d8 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1d5d9 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d  3>0 && pOp->p3<=
1d5da 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 61  p->nMem );.  u.a
1d5db 6d 2e 70 44 65 73 74 20 3d 20 26 61 4d 65 6d 5b  m.pDest = &aMem[
1d5dc 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53  pOp->p3];.  MemS
1d5dd 65 74 54 79 70 65 46 6c 61 67 28 75 2e 61 6d 2e  etTypeFlag(u.am.
1d5de 70 44 65 73 74 2c 20 4d 45 4d 5f 4e 75 6c 6c 29  pDest, MEM_Null)
1d5df 3b 0a 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20  ;.  u.am.zRec = 
1d5e0 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 62 6c  0;..  /* This bl
1d5e1 6f 63 6b 20 73 65 74 73 20 74 68 65 20 76 61 72  ock sets the var
1d5e2 69 61 62 6c 65 20 75 2e 61 6d 2e 70 61 79 6c 6f  iable u.am.paylo
1d5e3 61 64 53 69 7a 65 20 74 6f 20 62 65 20 74 68 65  adSize to be the
1d5e4 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
1d5e5 0a 20 20 2a 2a 20 62 79 74 65 73 20 69 6e 20 74  .  ** bytes in t
1d5e6 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2a 0a  he record..  **.
1d5e7 20 20 2a 2a 20 75 2e 61 6d 2e 7a 52 65 63 20 69    ** u.am.zRec i
1d5e8 73 20 73 65 74 20 74 6f 20 62 65 20 74 68 65 20  s set to be the 
1d5e9 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
1d5ea 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 69   the record if i
1d5eb 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a  t is available..
1d5ec 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74    ** The complet
1d5ed 65 20 72 65 63 6f 72 64 20 74 65 78 74 20 69 73  e record text is
1d5ee 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c   always availabl
1d5ef 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 62  e for pseudo-tab
1d5f0 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74 68 65 20  les.  ** If the 
1d5f1 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64  record is stored
1d5f2 20 69 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68   in a cursor, th
1d5f3 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72  e complete recor
1d5f4 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68  d text.  ** migh
1d5f5 74 20 62 65 20 61 76 61 69 6c 61 62 6c 65 20 69  t be available i
1d5f6 6e 20 74 68 65 20 20 75 2e 61 6d 2e 70 43 2d 3e  n the  u.am.pC->
1d5f7 61 52 6f 77 20 63 61 63 68 65 2e 20 20 4f 72 20  aRow cache.  Or 
1d5f8 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 2e  it might not be.
1d5f9 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74  .  ** If the dat
1d5fa 61 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65  a is unavailable
1d5fb 2c 20 20 75 2e 61 6d 2e 7a 52 65 63 20 69 73 20  ,  u.am.zRec is 
1d5fc 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a  set to NULL..  *
1d5fd 2a 0a 20 20 2a 2a 20 57 65 20 61 6c 73 6f 20 63  *.  ** We also c
1d5fe 6f 6d 70 75 74 65 20 74 68 65 20 6e 75 6d 62 65  ompute the numbe
1d5ff 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
1d600 74 68 65 20 72 65 63 6f 72 64 2e 20 20 46 6f 72  the record.  For
1d601 20 63 75 72 73 6f 72 73 2c 0a 20 20 2a 2a 20 74   cursors,.  ** t
1d602 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1d603 75 6d 6e 73 20 69 73 20 73 74 6f 72 65 64 20 69  umns is stored i
1d604 6e 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72  n the VdbeCursor
1d605 2e 6e 46 69 65 6c 64 20 65 6c 65 6d 65 6e 74 2e  .nField element.
1d606 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e 70 43 20  .  */.  u.am.pC 
1d607 3d 20 70 2d 3e 61 70 43 73 72 5b 75 2e 61 6d 2e  = p->apCsr[u.am.
1d608 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1d609 2e 61 6d 2e 70 43 21 3d 30 20 29 3b 0a 23 69 66  .am.pC!=0 );.#if
1d60a 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1d60b 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
1d60c 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 43 2d  assert( u.am.pC-
1d60d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d 30 20  >pVtabCursor==0 
1d60e 29 3b 0a 23 65 6e 64 69 66 0a 20 20 75 2e 61 6d  );.#endif.  u.am
1d60f 2e 70 43 72 73 72 20 3d 20 75 2e 61 6d 2e 70 43  .pCrsr = u.am.pC
1d610 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28  ->pCursor;.  if(
1d611 20 75 2e 61 6d 2e 70 43 72 73 72 21 3d 30 20 29   u.am.pCrsr!=0 )
1d612 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 63  {.    /* The rec
1d613 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ord is stored in
1d614 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 20   a B-Tree */.   
1d615 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
1d616 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e  eCursorMoveto(u.
1d617 61 6d 2e 70 43 29 3b 0a 20 20 20 20 69 66 28 20  am.pC);.    if( 
1d618 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  rc ) goto abort_
1d619 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1d61a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 6e    if( u.am.pC->n
1d61b 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20  ullRow ){.      
1d61c 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d61d 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
1d61e 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 63 61 63  if( u.am.pC->cac
1d61f 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61 63  heStatus==p->cac
1d620 68 65 43 74 72 20 29 7b 0a 20 20 20 20 20 20 75  heCtr ){.      u
1d621 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20  .am.payloadSize 
1d622 3d 20 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c 6f  = u.am.pC->paylo
1d623 61 64 53 69 7a 65 3b 0a 20 20 20 20 20 20 75 2e  adSize;.      u.
1d624 61 6d 2e 7a 52 65 63 20 3d 20 28 63 68 61 72 2a  am.zRec = (char*
1d625 29 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 3b 0a  )u.am.pC->aRow;.
1d626 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e      }else if( u.
1d627 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20 29  am.pC->isIndex )
1d628 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1d629 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
1d62a 6f 72 49 73 56 61 6c 69 64 28 75 2e 61 6d 2e 70  orIsValid(u.am.p
1d62b 43 72 73 72 29 20 29 3b 0a 20 20 20 20 20 20 72  Crsr) );.      r
1d62c 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1d62d 4b 65 79 53 69 7a 65 28 75 2e 61 6d 2e 70 43 72  KeySize(u.am.pCr
1d62e 73 72 2c 20 26 75 2e 61 6d 2e 70 61 79 6c 6f 61  sr, &u.am.payloa
1d62f 64 53 69 7a 65 36 34 29 3b 0a 20 20 20 20 20 20  dSize64);.      
1d630 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1d631 54 45 5f 4f 4b 20 29 3b 20 20 20 2f 2a 20 54 72  TE_OK );   /* Tr
1d632 75 65 20 62 65 63 61 75 73 65 20 6f 66 20 43 75  ue because of Cu
1d633 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c  rsorMoveto() cal
1d634 6c 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 20  l above */.     
1d635 20 2f 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65   /* sqlite3Btree
1d636 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 75  ParseCellPtr() u
1d637 73 65 73 20 67 65 74 56 61 72 69 6e 74 33 32 28  ses getVarint32(
1d638 29 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65  ) to extract the
1d639 0a 20 20 20 20 20 20 2a 2a 20 70 61 79 6c 6f 61  .      ** payloa
1d63a 64 20 73 69 7a 65 2c 20 73 6f 20 69 74 20 69 73  d size, so it is
1d63b 20 69 6d 70 6f 73 73 69 62 6c 65 20 66 6f 72 20   impossible for 
1d63c 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d63d 36 34 20 74 6f 20 62 65 0a 20 20 20 20 20 20 2a  64 to be.      *
1d63e 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 33 32  * larger than 32
1d63f 20 62 69 74 73 2e 20 2a 2f 0a 20 20 20 20 20 20   bits. */.      
1d640 61 73 73 65 72 74 28 20 28 75 2e 61 6d 2e 70 61  assert( (u.am.pa
1d641 79 6c 6f 61 64 53 69 7a 65 36 34 20 26 20 53 51  yloadSize64 & SQ
1d642 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29 3d 3d 28  LITE_MAX_U32)==(
1d643 75 36 34 29 75 2e 61 6d 2e 70 61 79 6c 6f 61 64  u64)u.am.payload
1d644 53 69 7a 65 36 34 20 29 3b 0a 20 20 20 20 20 20  Size64 );.      
1d645 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d646 20 3d 20 28 75 33 32 29 75 2e 61 6d 2e 70 61 79   = (u32)u.am.pay
1d647 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 20 20  loadSize64;.    
1d648 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
1d649 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1d64a 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75  eCursorIsValid(u
1d64b 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b 0a 20 20  .am.pCrsr) );.  
1d64c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1d64d 42 74 72 65 65 44 61 74 61 53 69 7a 65 28 75 2e  BtreeDataSize(u.
1d64e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e  am.pCrsr, &u.am.
1d64f 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b 0a 20 20  payloadSize);.  
1d650 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
1d651 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f  SQLITE_OK );   /
1d652 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61 6e  * DataSize() can
1d653 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20 20  not fail */.    
1d654 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e  }.  }else if( u.
1d655 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  am.pC->pseudoTab
1d656 6c 65 52 65 67 3e 30 20 29 7b 0a 20 20 20 20 75  leReg>0 ){.    u
1d657 2e 61 6d 2e 70 52 65 67 20 3d 20 26 61 4d 65 6d  .am.pReg = &aMem
1d658 5b 75 2e 61 6d 2e 70 43 2d 3e 70 73 65 75 64 6f  [u.am.pC->pseudo
1d659 54 61 62 6c 65 52 65 67 5d 3b 0a 20 20 20 20 61  TableReg];.    a
1d65a 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 52 65 67  ssert( u.am.pReg
1d65b 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c  ->flags & MEM_Bl
1d65c 6f 62 20 29 3b 0a 20 20 20 20 75 2e 61 6d 2e 70  ob );.    u.am.p
1d65d 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61  ayloadSize = u.a
1d65e 6d 2e 70 52 65 67 2d 3e 6e 3b 0a 20 20 20 20 75  m.pReg->n;.    u
1d65f 2e 61 6d 2e 7a 52 65 63 20 3d 20 75 2e 61 6d 2e  .am.zRec = u.am.
1d660 70 52 65 67 2d 3e 7a 3b 0a 20 20 20 20 75 2e 61  pReg->z;.    u.a
1d661 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  m.pC->cacheStatu
1d662 73 20 3d 20 28 70 4f 70 2d 3e 70 35 26 4f 50 46  s = (pOp->p5&OPF
1d663 4c 41 47 5f 43 4c 45 41 52 43 41 43 48 45 29 20  LAG_CLEARCACHE) 
1d664 3f 20 43 41 43 48 45 5f 53 54 41 4c 45 20 3a 20  ? CACHE_STALE : 
1d665 70 2d 3e 63 61 63 68 65 43 74 72 3b 0a 20 20 20  p->cacheCtr;.   
1d666 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 61   assert( u.am.pa
1d667 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 7c 7c 20  yloadSize==0 || 
1d668 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20 29 3b 0a  u.am.zRec!=0 );.
1d669 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1d66a 43 6f 6e 73 69 64 65 72 20 74 68 65 20 72 6f 77  Consider the row
1d66b 20 74 6f 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20   to be NULL */. 
1d66c 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53     u.am.payloadS
1d66d 69 7a 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  ize = 0;.  }..  
1d66e 2f 2a 20 49 66 20 75 2e 61 6d 2e 70 61 79 6c 6f  /* If u.am.paylo
1d66f 61 64 53 69 7a 65 20 69 73 20 30 2c 20 74 68 65  adSize is 0, the
1d670 6e 20 6a 75 73 74 20 73 74 6f 72 65 20 61 20 4e  n just store a N
1d671 55 4c 4c 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61  ULL */.  if( u.a
1d672 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30  m.payloadSize==0
1d673 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1d674 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67  u.am.pDest->flag
1d675 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20  s&MEM_Null );.  
1d676 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e    goto op_column
1d677 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 73 73 65  _out;.  }.  asse
1d678 72 74 28 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  rt( db->aLimit[S
1d679 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
1d67a 54 48 5d 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20  TH]>=0 );.  if( 
1d67b 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d67c 20 3e 20 28 75 33 32 29 64 62 2d 3e 61 4c 69 6d   > (u32)db->aLim
1d67d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
1d67e 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67  LENGTH] ){.    g
1d67f 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d  oto too_big;.  }
1d680 0a 0a 20 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20  ..  u.am.nField 
1d681 3d 20 75 2e 61 6d 2e 70 43 2d 3e 6e 46 69 65 6c  = u.am.pC->nFiel
1d682 64 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  d;.  assert( u.a
1d683 6d 2e 70 32 3c 75 2e 61 6d 2e 6e 46 69 65 6c 64  m.p2<u.am.nField
1d684 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 61   );..  /* Read a
1d685 6e 64 20 70 61 72 73 65 20 74 68 65 20 74 61 62  nd parse the tab
1d686 6c 65 20 68 65 61 64 65 72 2e 20 20 53 74 6f 72  le header.  Stor
1d687 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66  e the results of
1d688 20 74 68 65 20 70 61 72 73 65 0a 20 20 2a 2a 20   the parse.  ** 
1d689 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20  into the record 
1d68a 68 65 61 64 65 72 20 63 61 63 68 65 20 66 69 65  header cache fie
1d68b 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72 73 6f  lds of the curso
1d68c 72 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 6d 2e 61  r..  */.  u.am.a
1d68d 54 79 70 65 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e  Type = u.am.pC->
1d68e 61 54 79 70 65 3b 0a 20 20 69 66 28 20 75 2e 61  aType;.  if( u.a
1d68f 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  m.pC->cacheStatu
1d690 73 3d 3d 70 2d 3e 63 61 63 68 65 43 74 72 20 29  s==p->cacheCtr )
1d691 7b 0a 20 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73  {.    u.am.aOffs
1d692 65 74 20 3d 20 75 2e 61 6d 2e 70 43 2d 3e 61 4f  et = u.am.pC->aO
1d693 66 66 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ffset;.  }else{.
1d694 20 20 20 20 61 73 73 65 72 74 28 75 2e 61 6d 2e      assert(u.am.
1d695 61 54 79 70 65 29 3b 0a 20 20 20 20 75 2e 61 6d  aType);.    u.am
1d696 2e 61 76 61 69 6c 20 3d 20 30 3b 0a 20 20 20 20  .avail = 0;.    
1d697 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66 73 65 74  u.am.pC->aOffset
1d698 20 3d 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 20   = u.am.aOffset 
1d699 3d 20 26 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e  = &u.am.aType[u.
1d69a 61 6d 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20  am.nField];.    
1d69b 75 2e 61 6d 2e 70 43 2d 3e 70 61 79 6c 6f 61 64  u.am.pC->payload
1d69c 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 61 79 6c  Size = u.am.payl
1d69d 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 75 2e 61  oadSize;.    u.a
1d69e 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75  m.pC->cacheStatu
1d69f 73 20 3d 20 70 2d 3e 63 61 63 68 65 43 74 72 3b  s = p->cacheCtr;
1d6a0 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20  ..    /* Figure 
1d6a1 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74  out how many byt
1d6a2 65 73 20 61 72 65 20 69 6e 20 74 68 65 20 68 65  es are in the he
1d6a3 61 64 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20  ader */.    if( 
1d6a4 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a 20 20 20  u.am.zRec ){.   
1d6a5 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 3d 20     u.am.zData = 
1d6a6 75 2e 61 6d 2e 7a 52 65 63 3b 0a 20 20 20 20 7d  u.am.zRec;.    }
1d6a7 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
1d6a8 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78  u.am.pC->isIndex
1d6a9 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d   ){.        u.am
1d6aa 2e 7a 44 61 74 61 20 3d 20 28 63 68 61 72 2a 29  .zData = (char*)
1d6ab 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 46  sqlite3BtreeKeyF
1d6ac 65 74 63 68 28 75 2e 61 6d 2e 70 43 72 73 72 2c  etch(u.am.pCrsr,
1d6ad 20 26 75 2e 61 6d 2e 61 76 61 69 6c 29 3b 0a 20   &u.am.avail);. 
1d6ae 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d6af 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 3d      u.am.zData =
1d6b0 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 42   (char*)sqlite3B
1d6b1 74 72 65 65 44 61 74 61 46 65 74 63 68 28 75 2e  treeDataFetch(u.
1d6b2 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61 6d 2e  am.pCrsr, &u.am.
1d6b3 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 7d 0a  avail);.      }.
1d6b4 20 20 20 20 20 20 2f 2a 20 49 66 20 4b 65 79 46        /* If KeyF
1d6b5 65 74 63 68 28 29 2f 44 61 74 61 46 65 74 63 68  etch()/DataFetch
1d6b6 28 29 20 6d 61 6e 61 67 65 64 20 74 6f 20 67 65  () managed to ge
1d6b7 74 20 74 68 65 20 65 6e 74 69 72 65 20 70 61 79  t the entire pay
1d6b8 6c 6f 61 64 2c 0a 20 20 20 20 20 20 2a 2a 20 73  load,.      ** s
1d6b9 61 76 65 20 74 68 65 20 70 61 79 6c 6f 61 64 20  ave the payload 
1d6ba 69 6e 20 74 68 65 20 75 2e 61 6d 2e 70 43 2d 3e  in the u.am.pC->
1d6bb 61 52 6f 77 20 63 61 63 68 65 2e 20 20 54 68 61  aRow cache.  Tha
1d6bc 74 20 77 69 6c 6c 20 73 61 76 65 20 75 73 20 66  t will save us f
1d6bd 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 68 61 76  rom.      ** hav
1d6be 69 6e 67 20 74 6f 20 6d 61 6b 65 20 61 64 64 69  ing to make addi
1d6bf 74 69 6f 6e 61 6c 20 63 61 6c 6c 73 20 74 6f 20  tional calls to 
1d6c0 66 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65 6e  fetch the conten
1d6c1 74 20 70 6f 72 74 69 6f 6e 20 6f 66 0a 20 20 20  t portion of.   
1d6c2 20 20 20 2a 2a 20 74 68 65 20 72 65 63 6f 72 64     ** the record
1d6c3 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
1d6c4 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 61 76   assert( u.am.av
1d6c5 61 69 6c 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20  ail>=0 );.      
1d6c6 69 66 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64  if( u.am.payload
1d6c7 53 69 7a 65 20 3c 3d 20 28 75 33 32 29 75 2e 61  Size <= (u32)u.a
1d6c8 6d 2e 61 76 61 69 6c 20 29 7b 0a 20 20 20 20 20  m.avail ){.     
1d6c9 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20 3d 20 75     u.am.zRec = u
1d6ca 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20 20 20 20 20  .am.zData;.     
1d6cb 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77     u.am.pC->aRow
1d6cc 20 3d 20 28 75 38 2a 29 75 2e 61 6d 2e 7a 44 61   = (u8*)u.am.zDa
1d6cd 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ta;.      }else{
1d6ce 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 70 43  .        u.am.pC
1d6cf 2d 3e 61 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20  ->aRow = 0;.    
1d6d0 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a    }.    }.    /*
1d6d1 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
1d6d2 73 73 65 72 74 20 69 73 20 74 72 75 65 20 69 6e  ssert is true in
1d6d3 20 61 6c 6c 20 63 61 73 65 73 20 61 63 63 65 70   all cases accep
1d6d4 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68  t when.    ** th
1d6d5 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1d6d6 68 61 73 20 62 65 65 6e 20 63 6f 72 72 75 70 74  has been corrupt
1d6d7 65 64 20 65 78 74 65 72 6e 61 6c 6c 79 2e 0a 20  ed externally.. 
1d6d8 20 20 20 2a 2a 20 20 20 20 61 73 73 65 72 74 28     **    assert(
1d6d9 20 75 2e 61 6d 2e 7a 52 65 63 21 3d 30 20 7c 7c   u.am.zRec!=0 ||
1d6da 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d 75 2e 61   u.am.avail>=u.a
1d6db 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 7c 7c  m.payloadSize ||
1d6dc 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d 39 20 29   u.am.avail>=9 )
1d6dd 3b 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 73 7a  ; */.    u.am.sz
1d6de 48 64 72 20 3d 20 67 65 74 56 61 72 69 6e 74 33  Hdr = getVarint3
1d6df 32 28 28 75 38 2a 29 75 2e 61 6d 2e 7a 44 61 74  2((u8*)u.am.zDat
1d6e0 61 2c 20 75 2e 61 6d 2e 6f 66 66 73 65 74 29 3b  a, u.am.offset);
1d6e1 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75  ..    /* Make su
1d6e2 72 65 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  re a corrupt dat
1d6e3 61 62 61 73 65 20 68 61 73 20 6e 6f 74 20 67 69  abase has not gi
1d6e4 76 65 6e 20 75 73 20 61 6e 20 6f 76 65 72 73 69  ven us an oversi
1d6e5 7a 65 20 68 65 61 64 65 72 2e 0a 20 20 20 20 2a  ze header..    *
1d6e6 2a 20 44 6f 20 74 68 69 73 20 6e 6f 77 20 74 6f  * Do this now to
1d6e7 20 61 76 6f 69 64 20 61 6e 20 6f 76 65 72 73 69   avoid an oversi
1d6e8 7a 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ze memory alloca
1d6e9 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  tion..    **.   
1d6ea 20 2a 2a 20 54 79 70 65 20 65 6e 74 72 69 65 73   ** Type entries
1d6eb 20 63 61 6e 20 62 65 20 62 65 74 77 65 65 6e 20   can be between 
1d6ec 31 20 61 6e 64 20 35 20 62 79 74 65 73 20 65 61  1 and 5 bytes ea
1d6ed 63 68 2e 20 20 42 75 74 20 34 20 61 6e 64 20 35  ch.  But 4 and 5
1d6ee 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 74 79 70   byte.    ** typ
1d6ef 65 73 20 75 73 65 20 73 6f 20 6d 75 63 68 20 64  es use so much d
1d6f0 61 74 61 20 73 70 61 63 65 20 74 68 61 74 20 74  ata space that t
1d6f1 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65  here can only be
1d6f2 20 34 30 39 36 20 61 6e 64 20 33 32 20 6f 66 0a   4096 and 32 of.
1d6f3 20 20 20 20 2a 2a 20 74 68 65 6d 2c 20 72 65 73      ** them, res
1d6f4 70 65 63 74 69 76 65 6c 79 2e 20 20 53 6f 20 74  pectively.  So t
1d6f5 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 61 64 65  he maximum heade
1d6f6 72 20 6c 65 6e 67 74 68 20 72 65 73 75 6c 74 73  r length results
1d6f7 20 66 72 6f 6d 20 61 0a 20 20 20 20 2a 2a 20 33   from a.    ** 3
1d6f8 2d 62 79 74 65 20 74 79 70 65 20 66 6f 72 20 65  -byte type for e
1d6f9 61 63 68 20 6f 66 20 74 68 65 20 6d 61 78 69 6d  ach of the maxim
1d6fa 75 6d 20 6f 66 20 33 32 37 36 38 20 63 6f 6c 75  um of 32768 colu
1d6fb 6d 6e 73 20 70 6c 75 73 20 74 68 72 65 65 0a 20  mns plus three. 
1d6fc 20 20 20 2a 2a 20 65 78 74 72 61 20 62 79 74 65     ** extra byte
1d6fd 73 20 66 6f 72 20 74 68 65 20 68 65 61 64 65 72  s for the header
1d6fe 20 6c 65 6e 67 74 68 20 69 74 73 65 6c 66 2e 20   length itself. 
1d6ff 20 33 32 37 36 38 2a 33 20 2b 20 33 20 3d 20 39   32768*3 + 3 = 9
1d700 38 33 30 37 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  8307..    */.   
1d701 20 69 66 28 20 75 2e 61 6d 2e 6f 66 66 73 65 74   if( u.am.offset
1d702 20 3e 20 39 38 33 30 37 20 29 7b 0a 20 20 20 20   > 98307 ){.    
1d703 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
1d704 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
1d705 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e    goto op_column
1d706 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  _out;.    }..   
1d707 20 2f 2a 20 43 6f 6d 70 75 74 65 20 69 6e 20 75   /* Compute in u
1d708 2e 61 6d 2e 6c 65 6e 20 74 68 65 20 6e 75 6d 62  .am.len the numb
1d709 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64  er of bytes of d
1d70a 61 74 61 20 77 65 20 6e 65 65 64 20 74 6f 20 72  ata we need to r
1d70b 65 61 64 20 69 6e 20 6f 72 64 65 72 0a 20 20 20  ead in order.   
1d70c 20 2a 2a 20 74 6f 20 67 65 74 20 75 2e 61 6d 2e   ** to get u.am.
1d70d 6e 46 69 65 6c 64 20 74 79 70 65 20 76 61 6c 75  nField type valu
1d70e 65 73 2e 20 20 75 2e 61 6d 2e 6f 66 66 73 65 74  es.  u.am.offset
1d70f 20 69 73 20 61 6e 20 75 70 70 65 72 20 62 6f 75   is an upper bou
1d710 6e 64 20 6f 6e 20 74 68 69 73 2e 20 20 42 75 74  nd on this.  But
1d711 0a 20 20 20 20 2a 2a 20 75 2e 61 6d 2e 6e 46 69  .    ** u.am.nFi
1d712 65 6c 64 20 6d 69 67 68 74 20 62 65 20 73 69 67  eld might be sig
1d713 6e 69 66 69 63 61 6e 74 6c 79 20 6c 65 73 73 20  nificantly less 
1d714 74 68 61 6e 20 74 68 65 20 74 72 75 65 20 6e 75  than the true nu
1d715 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a  mber of columns.
1d716 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 74 61      ** in the ta
1d717 62 6c 65 2c 20 61 6e 64 20 69 6e 20 74 68 61 74  ble, and in that
1d718 20 63 61 73 65 2c 20 35 2a 75 2e 61 6d 2e 6e 46   case, 5*u.am.nF
1d719 69 65 6c 64 2b 33 20 6d 69 67 68 74 20 62 65 20  ield+3 might be 
1d71a 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 75 2e 61  smaller than u.a
1d71b 6d 2e 6f 66 66 73 65 74 2e 0a 20 20 20 20 2a 2a  m.offset..    **
1d71c 20 57 65 20 77 61 6e 74 20 74 6f 20 6d 69 6e 69   We want to mini
1d71d 6d 69 7a 65 20 75 2e 61 6d 2e 6c 65 6e 20 69 6e  mize u.am.len in
1d71e 20 6f 72 64 65 72 20 74 6f 20 6c 69 6d 69 74 20   order to limit 
1d71f 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
1d720 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 61 6c  memory.    ** al
1d721 6c 6f 63 61 74 69 6f 6e 2c 20 65 73 70 65 63 69  location, especi
1d722 61 6c 6c 79 20 69 66 20 61 20 63 6f 72 72 75 70  ally if a corrup
1d723 74 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  t database file 
1d724 68 61 73 20 63 61 75 73 65 64 20 75 2e 61 6d 2e  has caused u.am.
1d725 6f 66 66 73 65 74 0a 20 20 20 20 2a 2a 20 74 6f  offset.    ** to
1d726 20 62 65 20 6f 76 65 72 73 69 7a 65 64 2e 20 4f   be oversized. O
1d727 66 66 73 65 74 20 69 73 20 6c 69 6d 69 74 65 64  ffset is limited
1d728 20 74 6f 20 39 38 33 30 37 20 61 62 6f 76 65 2e   to 98307 above.
1d729 20 20 42 75 74 20 39 38 33 30 37 20 6d 69 67 68    But 98307 migh
1d72a 74 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 65  t.    ** still e
1d72b 78 63 65 65 64 20 52 6f 62 73 6f 6e 20 6d 65 6d  xceed Robson mem
1d72c 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c  ory allocation l
1d72d 69 6d 69 74 73 20 6f 6e 20 73 6f 6d 65 20 63 6f  imits on some co
1d72e 6e 66 69 67 75 72 61 74 69 6f 6e 73 2e 0a 20 20  nfigurations..  
1d72f 20 20 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20    ** On systems 
1d730 74 68 61 74 20 63 61 6e 6e 6f 74 20 74 6f 6c 65  that cannot tole
1d731 72 61 74 65 20 6c 61 72 67 65 20 6d 65 6d 6f 72  rate large memor
1d732 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2c 20 75  y allocations, u
1d733 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 2b 33 0a 20  .am.nField*5+3. 
1d734 20 20 20 2a 2a 20 77 69 6c 6c 20 6c 69 6b 65 6c     ** will likel
1d735 79 20 62 65 20 6d 75 63 68 20 73 6d 61 6c 6c 65  y be much smalle
1d736 72 20 73 69 6e 63 65 20 75 2e 61 6d 2e 6e 46 69  r since u.am.nFi
1d737 65 6c 64 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20  eld will likely 
1d738 62 65 20 6c 65 73 73 20 74 68 61 6e 0a 20 20 20  be less than.   
1d739 20 2a 2a 20 32 30 20 6f 72 20 73 6f 2e 20 20 54   ** 20 or so.  T
1d73a 68 69 73 20 69 6e 73 75 72 65 73 20 74 68 61 74  his insures that
1d73b 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72 79 20 61   Robson memory a
1d73c 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d 69 74 73  llocation limits
1d73d 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f 74 20   are.    ** not 
1d73e 65 78 63 65 65 64 65 64 20 65 76 65 6e 20 66 6f  exceeded even fo
1d73f 72 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61  r corrupt databa
1d740 73 65 20 66 69 6c 65 73 2e 0a 20 20 20 20 2a 2f  se files..    */
1d741 0a 20 20 20 20 75 2e 61 6d 2e 6c 65 6e 20 3d 20  .    u.am.len = 
1d742 75 2e 61 6d 2e 6e 46 69 65 6c 64 2a 35 20 2b 20  u.am.nField*5 + 
1d743 33 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e  3;.    if( u.am.
1d744 6c 65 6e 20 3e 20 28 69 6e 74 29 75 2e 61 6d 2e  len > (int)u.am.
1d745 6f 66 66 73 65 74 20 29 20 75 2e 61 6d 2e 6c 65  offset ) u.am.le
1d746 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6d 2e 6f 66  n = (int)u.am.of
1d747 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a 20 54 68  fset;..    /* Th
1d748 65 20 4b 65 79 46 65 74 63 68 28 29 20 6f 72 20  e KeyFetch() or 
1d749 44 61 74 61 46 65 74 63 68 28 29 20 61 62 6f 76  DataFetch() abov
1d74a 65 20 61 72 65 20 66 61 73 74 20 61 6e 64 20 77  e are fast and w
1d74b 69 6c 6c 20 67 65 74 20 74 68 65 20 65 6e 74 69  ill get the enti
1d74c 72 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64  re.    ** record
1d74d 20 68 65 61 64 65 72 20 69 6e 20 6d 6f 73 74 20   header in most 
1d74e 63 61 73 65 73 2e 20 20 42 75 74 20 74 68 65 79  cases.  But they
1d74f 20 77 69 6c 6c 20 66 61 69 6c 20 74 6f 20 67 65   will fail to ge
1d750 74 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 20  t the complete. 
1d751 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 68 65 61     ** record hea
1d752 64 65 72 20 69 66 20 74 68 65 20 72 65 63 6f 72  der if the recor
1d753 64 20 68 65 61 64 65 72 20 64 6f 65 73 20 6e 6f  d header does no
1d754 74 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67 6c  t fit on a singl
1d755 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e  e page.    ** in
1d756 20 74 68 65 20 42 2d 54 72 65 65 2e 20 20 57 68   the B-Tree.  Wh
1d757 65 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c  en that happens,
1d758 20 75 73 65 20 73 71 6c 69 74 65 33 56 64 62 65   use sqlite3Vdbe
1d759 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 20 74  MemFromBtree() t
1d75a 6f 0a 20 20 20 20 2a 2a 20 61 63 71 75 69 72 65  o.    ** acquire
1d75b 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 65   the complete he
1d75c 61 64 65 72 20 74 65 78 74 2e 0a 20 20 20 20 2a  ader text..    *
1d75d 2f 0a 20 20 20 20 69 66 28 20 21 75 2e 61 6d 2e  /.    if( !u.am.
1d75e 7a 52 65 63 20 26 26 20 75 2e 61 6d 2e 61 76 61  zRec && u.am.ava
1d75f 69 6c 3c 75 2e 61 6d 2e 6c 65 6e 20 29 7b 0a 20  il<u.am.len ){. 
1d760 20 20 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 66       u.am.sMem.f
1d761 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 20 20  lags = 0;.      
1d762 75 2e 61 6d 2e 73 4d 65 6d 2e 64 62 20 3d 20 30  u.am.sMem.db = 0
1d763 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
1d764 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42  ite3VdbeMemFromB
1d765 74 72 65 65 28 75 2e 61 6d 2e 70 43 72 73 72 2c  tree(u.am.pCrsr,
1d766 20 30 2c 20 75 2e 61 6d 2e 6c 65 6e 2c 20 75 2e   0, u.am.len, u.
1d767 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65 78 2c 20  am.pC->isIndex, 
1d768 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a 20 20 20  &u.am.sMem);.   
1d769 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1d76a 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1d76b 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f  goto op_column_o
1d76c 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
1d76d 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 75    u.am.zData = u
1d76e 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20  .am.sMem.z;.    
1d76f 7d 0a 20 20 20 20 75 2e 61 6d 2e 7a 45 6e 64 48  }.    u.am.zEndH
1d770 64 72 20 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d  dr = (u8 *)&u.am
1d771 2e 7a 44 61 74 61 5b 75 2e 61 6d 2e 6c 65 6e 5d  .zData[u.am.len]
1d772 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a 49 64 78 20  ;.    u.am.zIdx 
1d773 3d 20 28 75 38 20 2a 29 26 75 2e 61 6d 2e 7a 44  = (u8 *)&u.am.zD
1d774 61 74 61 5b 75 2e 61 6d 2e 73 7a 48 64 72 5d 3b  ata[u.am.szHdr];
1d775 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 74 68  ..    /* Scan th
1d776 65 20 68 65 61 64 65 72 20 61 6e 64 20 75 73 65  e header and use
1d777 20 69 74 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74   it to fill in t
1d778 68 65 20 75 2e 61 6d 2e 61 54 79 70 65 5b 5d 20  he u.am.aType[] 
1d779 61 6e 64 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74  and u.am.aOffset
1d77a 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 73  [].    ** arrays
1d77b 2e 20 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e  .  u.am.aType[u.
1d77c 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 61  am.i] will conta
1d77d 69 6e 20 74 68 65 20 74 79 70 65 20 69 6e 74 65  in the type inte
1d77e 67 65 72 20 66 6f 72 20 74 68 65 20 75 2e 61 6d  ger for the u.am
1d77f 2e 69 2d 74 68 0a 20 20 20 20 2a 2a 20 63 6f 6c  .i-th.    ** col
1d780 75 6d 6e 20 61 6e 64 20 75 2e 61 6d 2e 61 4f 66  umn and u.am.aOf
1d781 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20 77 69 6c  fset[u.am.i] wil
1d782 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 75 2e  l contain the u.
1d783 61 6d 2e 6f 66 66 73 65 74 20 66 72 6f 6d 20 74  am.offset from t
1d784 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20  he beginning.   
1d785 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72   ** of the recor
1d786 64 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  d to the start o
1d787 66 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  f the data for t
1d788 68 65 20 75 2e 61 6d 2e 69 2d 74 68 20 63 6f 6c  he u.am.i-th col
1d789 75 6d 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75  umn.    */.    u
1d78a 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 3d 20 75  .am.offset64 = u
1d78b 2e 61 6d 2e 6f 66 66 73 65 74 3b 0a 20 20 20 20  .am.offset;.    
1d78c 66 6f 72 28 75 2e 61 6d 2e 69 3d 30 3b 20 75 2e  for(u.am.i=0; u.
1d78d 61 6d 2e 69 3c 75 2e 61 6d 2e 6e 46 69 65 6c 64  am.i<u.am.nField
1d78e 3b 20 75 2e 61 6d 2e 69 2b 2b 29 7b 0a 20 20 20  ; u.am.i++){.   
1d78f 20 20 20 69 66 28 20 75 2e 61 6d 2e 7a 49 64 78     if( u.am.zIdx
1d790 3c 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 20 29 7b  <u.am.zEndHdr ){
1d791 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 61 4f  .        u.am.aO
1d792 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20 3d 20  ffset[u.am.i] = 
1d793 28 75 33 32 29 75 2e 61 6d 2e 6f 66 66 73 65 74  (u32)u.am.offset
1d794 36 34 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 6d  64;.        u.am
1d795 2e 7a 49 64 78 20 2b 3d 20 67 65 74 56 61 72 69  .zIdx += getVari
1d796 6e 74 33 32 28 75 2e 61 6d 2e 7a 49 64 78 2c 20  nt32(u.am.zIdx, 
1d797 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e  u.am.aType[u.am.
1d798 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  i]);.        u.a
1d799 6d 2e 6f 66 66 73 65 74 36 34 20 2b 3d 20 73 71  m.offset64 += sq
1d79a 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
1d79b 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61 54 79 70  ypeLen(u.am.aTyp
1d79c 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a 20 20 20 20  e[u.am.i]);.    
1d79d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1d79e 20 2f 2a 20 49 66 20 75 2e 61 6d 2e 69 20 69 73   /* If u.am.i is
1d79f 20 6c 65 73 73 20 74 68 61 74 20 75 2e 61 6d 2e   less that u.am.
1d7a0 6e 46 69 65 6c 64 2c 20 74 68 65 6e 20 74 68 65  nField, then the
1d7a1 72 65 20 61 72 65 20 6c 65 73 73 20 66 69 65 6c  re are less fiel
1d7a2 64 73 20 69 6e 20 74 68 69 73 0a 20 20 20 20 20  ds in this.     
1d7a3 20 20 20 2a 2a 20 72 65 63 6f 72 64 20 74 68 61     ** record tha
1d7a4 6e 20 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20  n SetNumColumns 
1d7a5 69 6e 64 69 63 61 74 65 64 20 74 68 65 72 65 20  indicated there 
1d7a6 61 72 65 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74  are columns in t
1d7a7 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61  he.        ** ta
1d7a8 62 6c 65 2e 20 53 65 74 20 74 68 65 20 75 2e 61  ble. Set the u.a
1d7a9 6d 2e 6f 66 66 73 65 74 20 66 6f 72 20 61 6e 79  m.offset for any
1d7aa 20 65 78 74 72 61 20 63 6f 6c 75 6d 6e 73 20 6e   extra columns n
1d7ab 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 0a 20 20  ot present in.  
1d7ac 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 63        ** the rec
1d7ad 6f 72 64 20 74 6f 20 30 2e 20 54 68 69 73 20 74  ord to 0. This t
1d7ae 65 6c 6c 73 20 63 6f 64 65 20 62 65 6c 6f 77 20  ells code below 
1d7af 74 6f 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 0a  to store a NULL.
1d7b0 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 65          ** inste
1d7b1 61 64 20 6f 66 20 64 65 73 65 72 69 61 6c 69 7a  ad of deserializ
1d7b2 69 6e 67 20 61 20 76 61 6c 75 65 20 66 72 6f 6d  ing a value from
1d7b3 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 20   the record..   
1d7b4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
1d7b5 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61  u.am.aOffset[u.a
1d7b6 6d 2e 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  m.i] = 0;.      
1d7b7 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
1d7b8 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1d7b9 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a 20  e(&u.am.sMem);. 
1d7ba 20 20 20 75 2e 61 6d 2e 73 4d 65 6d 2e 66 6c 61     u.am.sMem.fla
1d7bb 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a  gs = MEM_Null;..
1d7bc 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76      /* If we hav
1d7bd 65 20 72 65 61 64 20 6d 6f 72 65 20 68 65 61 64  e read more head
1d7be 65 72 20 64 61 74 61 20 74 68 61 6e 20 77 61 73  er data than was
1d7bf 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68   contained in th
1d7c0 65 20 68 65 61 64 65 72 2c 0a 20 20 20 20 2a 2a  e header,.    **
1d7c1 20 6f 72 20 69 66 20 74 68 65 20 65 6e 64 20 6f   or if the end o
1d7c2 66 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64  f the last field
1d7c3 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20 70   appears to be p
1d7c4 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
1d7c5 68 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72 64  he.    ** record
1d7c6 2c 20 6f 72 20 69 66 20 74 68 65 20 65 6e 64 20  , or if the end 
1d7c7 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c  of the last fiel
1d7c8 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20  d appears to be 
1d7c9 62 65 66 6f 72 65 20 74 68 65 20 65 6e 64 0a 20  before the end. 
1d7ca 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65 63     ** of the rec
1d7cb 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c 20 66 69  ord (when all fi
1d7cc 65 6c 64 73 20 70 72 65 73 65 6e 74 29 2c 20 74  elds present), t
1d7cd 68 65 6e 20 77 65 20 6d 75 73 74 20 62 65 20 64  hen we must be d
1d7ce 65 61 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 77 69  ealing.    ** wi
1d7cf 74 68 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  th a corrupt dat
1d7d0 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  abase..    */.  
1d7d1 20 20 69 66 28 20 28 75 2e 61 6d 2e 7a 49 64 78    if( (u.am.zIdx
1d7d2 20 3e 20 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 29   > u.am.zEndHdr)
1d7d3 7c 7c 20 28 75 2e 61 6d 2e 6f 66 66 73 65 74 36  || (u.am.offset6
1d7d4 34 20 3e 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64  4 > u.am.payload
1d7d5 53 69 7a 65 29 0a 20 20 20 20 20 7c 7c 20 28 75  Size).     || (u
1d7d6 2e 61 6d 2e 7a 49 64 78 3d 3d 75 2e 61 6d 2e 7a  .am.zIdx==u.am.z
1d7d7 45 6e 64 48 64 72 20 26 26 20 75 2e 61 6d 2e 6f  EndHdr && u.am.o
1d7d8 66 66 73 65 74 36 34 21 3d 28 75 36 34 29 75 2e  ffset64!=(u64)u.
1d7d9 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29 20  am.payloadSize) 
1d7da 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
1d7db 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
1d7dc 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70  T;.      goto op
1d7dd 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20 20 20  _column_out;.   
1d7de 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 74   }.  }..  /* Get
1d7df 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 66 6f   the column info
1d7e0 72 6d 61 74 69 6f 6e 2e 20 49 66 20 75 2e 61 6d  rmation. If u.am
1d7e1 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 32  .aOffset[u.am.p2
1d7e2 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  ] is non-zero, t
1d7e3 68 65 6e 0a 20 20 2a 2a 20 64 65 73 65 72 69 61  hen.  ** deseria
1d7e4 6c 69 7a 65 20 74 68 65 20 76 61 6c 75 65 20 66  lize the value f
1d7e5 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64 2e 20  rom the record. 
1d7e6 49 66 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b  If u.am.aOffset[
1d7e7 75 2e 61 6d 2e 70 32 5d 20 69 73 20 7a 65 72 6f  u.am.p2] is zero
1d7e8 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72  ,.  ** then ther
1d7e9 65 20 61 72 65 20 6e 6f 74 20 65 6e 6f 75 67 68  e are not enough
1d7ea 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72   fields in the r
1d7eb 65 63 6f 72 64 20 74 6f 20 73 61 74 69 73 66 79  ecord to satisfy
1d7ec 20 74 68 65 0a 20 20 2a 2a 20 72 65 71 75 65 73   the.  ** reques
1d7ed 74 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73 65  t.  In this case
1d7ee 2c 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 20  , set the value 
1d7ef 4e 55 4c 4c 20 6f 72 20 74 6f 20 50 34 20 69 66  NULL or to P4 if
1d7f0 20 50 34 20 69 73 0a 20 20 2a 2a 20 61 20 70 6f   P4 is.  ** a po
1d7f1 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 20 6f  inter to a Mem o
1d7f2 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  bject..  */.  if
1d7f3 28 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75  ( u.am.aOffset[u
1d7f4 2e 61 6d 2e 70 32 5d 20 29 7b 0a 20 20 20 20 61  .am.p2] ){.    a
1d7f5 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
1d7f6 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20  E_OK );.    if( 
1d7f7 75 2e 61 6d 2e 7a 52 65 63 20 29 7b 0a 20 20 20  u.am.zRec ){.   
1d7f8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1d7f9 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c  mReleaseExternal
1d7fa 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20  (u.am.pDest);.  
1d7fb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
1d7fc 65 72 69 61 6c 47 65 74 28 28 75 38 20 2a 29 26  erialGet((u8 *)&
1d7fd 75 2e 61 6d 2e 7a 52 65 63 5b 75 2e 61 6d 2e 61  u.am.zRec[u.am.a
1d7fe 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 32 5d 5d  Offset[u.am.p2]]
1d7ff 2c 20 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61  , u.am.aType[u.a
1d800 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 70 44 65 73  m.p2], u.am.pDes
1d801 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
1d802 20 20 20 20 20 75 2e 61 6d 2e 6c 65 6e 20 3d 20       u.am.len = 
1d803 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1d804 6c 54 79 70 65 4c 65 6e 28 75 2e 61 6d 2e 61 54  lTypeLen(u.am.aT
1d805 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 29 3b 0a 20  ype[u.am.p2]);. 
1d806 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1d807 4d 65 6d 4d 6f 76 65 28 26 75 2e 61 6d 2e 73 4d  MemMove(&u.am.sM
1d808 65 6d 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b  em, u.am.pDest);
1d809 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1d80a 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
1d80b 72 65 65 28 75 2e 61 6d 2e 70 43 72 73 72 2c 20  ree(u.am.pCrsr, 
1d80c 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61  u.am.aOffset[u.a
1d80d 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e 6c 65 6e 2c  m.p2], u.am.len,
1d80e 20 75 2e 61 6d 2e 70 43 2d 3e 69 73 49 6e 64 65   u.am.pC->isInde
1d80f 78 2c 20 26 75 2e 61 6d 2e 73 4d 65 6d 29 3b 0a  x, &u.am.sMem);.
1d810 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1d811 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1d812 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d     goto op_colum
1d813 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20  n_out;.      }. 
1d814 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74 61 20       u.am.zData 
1d815 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20  = u.am.sMem.z;. 
1d816 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1d817 53 65 72 69 61 6c 47 65 74 28 28 75 38 2a 29 75  SerialGet((u8*)u
1d818 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e 61 6d 2e  .am.zData, u.am.
1d819 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d 2c 20  aType[u.am.p2], 
1d81a 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 20  u.am.pDest);.   
1d81b 20 7d 0a 20 20 20 20 75 2e 61 6d 2e 70 44 65 73   }.    u.am.pDes
1d81c 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64 69 6e  t->enc = encodin
1d81d 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  g;.  }else{.    
1d81e 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  if( pOp->p4type=
1d81f 3d 50 34 5f 4d 45 4d 20 29 7b 0a 20 20 20 20 20  =P4_MEM ){.     
1d820 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1d821 68 61 6c 6c 6f 77 43 6f 70 79 28 75 2e 61 6d 2e  hallowCopy(u.am.
1d822 70 44 65 73 74 2c 20 70 4f 70 2d 3e 70 34 2e 70  pDest, pOp->p4.p
1d823 4d 65 6d 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29  Mem, MEM_Static)
1d824 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1d825 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e     assert( u.am.
1d826 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d  pDest->flags&MEM
1d827 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20  _Null );.    }. 
1d828 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 64   }..  /* If we d
1d829 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
1d82a 61 74 65 64 20 73 70 61 63 65 20 74 6f 20 68 6f  ated space to ho
1d82b 6c 64 20 74 68 65 20 64 61 74 61 20 28 69 6e 20  ld the data (in 
1d82c 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  the.  ** sqlite3
1d82d 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65  VdbeMemFromBtree
1d82e 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 29 20 74  () call above) t
1d82f 68 65 6e 20 74 72 61 6e 73 66 65 72 20 63 6f 6e  hen transfer con
1d830 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a 20 20 2a  trol of that.  *
1d831 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  * dynamically al
1d832 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 6f 76  located space ov
1d833 65 72 20 74 6f 20 74 68 65 20 75 2e 61 6d 2e 70  er to the u.am.p
1d834 44 65 73 74 20 73 74 72 75 63 74 75 72 65 2e 0a  Dest structure..
1d835 20 20 2a 2a 20 54 68 69 73 20 70 72 65 76 65 6e    ** This preven
1d836 74 73 20 61 20 6d 65 6d 6f 72 79 20 63 6f 70 79  ts a memory copy
1d837 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61  ..  */.  if( u.a
1d838 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29  m.sMem.zMalloc )
1d839 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  {.    assert( u.
1d83a 61 6d 2e 73 4d 65 6d 2e 7a 3d 3d 75 2e 61 6d 2e  am.sMem.z==u.am.
1d83b 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a  sMem.zMalloc );.
1d83c 20 20 20 20 61 73 73 65 72 74 28 20 21 28 75 2e      assert( !(u.
1d83d 61 6d 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20  am.pDest->flags 
1d83e 26 20 4d 45 4d 5f 44 79 6e 29 20 29 3b 0a 20 20  & MEM_Dyn) );.  
1d83f 20 20 61 73 73 65 72 74 28 20 21 28 75 2e 61 6d    assert( !(u.am
1d840 2e 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 26 20  .pDest->flags & 
1d841 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74  (MEM_Blob|MEM_St
1d842 72 29 29 20 7c 7c 20 75 2e 61 6d 2e 70 44 65 73  r)) || u.am.pDes
1d843 74 2d 3e 7a 3d 3d 75 2e 61 6d 2e 73 4d 65 6d 2e  t->z==u.am.sMem.
1d844 7a 20 29 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 44  z );.    u.am.pD
1d845 65 73 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28  est->flags &= ~(
1d846 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74  MEM_Ephem|MEM_St
1d847 61 74 69 63 29 3b 0a 20 20 20 20 75 2e 61 6d 2e  atic);.    u.am.
1d848 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 7c 3d 20  pDest->flags |= 
1d849 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 75 2e  MEM_Term;.    u.
1d84a 61 6d 2e 70 44 65 73 74 2d 3e 7a 20 3d 20 75 2e  am.pDest->z = u.
1d84b 61 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 75  am.sMem.z;.    u
1d84c 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 4d 61 6c 6c  .am.pDest->zMall
1d84d 6f 63 20 3d 20 75 2e 61 6d 2e 73 4d 65 6d 2e 7a  oc = u.am.sMem.z
1d84e 4d 61 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72  Malloc;.  }..  r
1d84f 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
1d850 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
1d851 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 0a 6f 70  u.am.pDest);..op
1d852 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3a 0a 20 20 55  _column_out:.  U
1d853 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49  PDATE_MAX_BLOBSI
1d854 5a 45 28 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a  ZE(u.am.pDest);.
1d855 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1d856 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 61 6d 2e 70  (pOp->p3, u.am.p
1d857 44 65 73 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  Dest);.  break;.
1d858 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 66  }../* Opcode: Af
1d859 66 69 6e 69 74 79 20 50 31 20 50 32 20 2a 20 50  finity P1 P2 * P
1d85a 34 20 2a 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 79 20  4 *.**.** Apply 
1d85b 61 66 66 69 6e 69 74 69 65 73 20 74 6f 20 61 20  affinities to a 
1d85c 72 61 6e 67 65 20 6f 66 20 50 32 20 72 65 67 69  range of P2 regi
1d85d 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 77  sters starting w
1d85e 69 74 68 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 50 34  ith P1..**.** P4
1d85f 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61   is a string tha
1d860 74 20 69 73 20 50 32 20 63 68 61 72 61 63 74 65  t is P2 characte
1d861 72 73 20 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68  rs long. The nth
1d862 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68   character of th
1d863 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69  e.** string indi
1d864 63 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e  cates the column
1d865 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73   affinity that s
1d866 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f  hould be used fo
1d867 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20 6d 65 6d  r the nth.** mem
1d868 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 65 20  ory cell in the 
1d869 72 61 6e 67 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  range..*/.case O
1d86a 50 5f 41 66 66 69 6e 69 74 79 3a 20 7b 0a 23 69  P_Affinity: {.#i
1d86b 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1d86c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d86d 74 6f 20 75 2e 61 6e 20 2a 2f 0a 20 20 63 6f 6e  to u.an */.  con
1d86e 73 74 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69  st char *zAffini
1d86f 74 79 3b 20 20 20 2f 2a 20 54 68 65 20 61 66 66  ty;   /* The aff
1d870 69 6e 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c  inity to be appl
1d871 69 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 63 41  ied */.  char cA
1d872 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ff;             
1d873 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 63 68    /* A single ch
1d874 61 72 61 63 74 65 72 20 6f 66 20 61 66 66 69 6e  aracter of affin
1d875 69 74 79 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  ity */.#endif /*
1d876 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1d877 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6e   moved into u.an
1d878 20 2a 2f 0a 0a 20 20 75 2e 61 6e 2e 7a 41 66 66   */..  u.an.zAff
1d879 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e  inity = pOp->p4.
1d87a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  z;.  assert( u.a
1d87b 6e 2e 7a 41 66 66 69 6e 69 74 79 21 3d 30 20 29  n.zAffinity!=0 )
1d87c 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6e  ;.  assert( u.an
1d87d 2e 7a 41 66 66 69 6e 69 74 79 5b 70 4f 70 2d 3e  .zAffinity[pOp->
1d87e 70 32 5d 3d 3d 30 20 29 3b 0a 20 20 70 49 6e 31  p2]==0 );.  pIn1
1d87f 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31   = &aMem[pOp->p1
1d880 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28 75 2e 61  ];.  while( (u.a
1d881 6e 2e 63 41 66 66 20 3d 20 2a 28 75 2e 61 6e 2e  n.cAff = *(u.an.
1d882 7a 41 66 66 69 6e 69 74 79 2b 2b 29 29 21 3d 30  zAffinity++))!=0
1d883 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1d884 70 49 6e 31 20 3c 3d 20 26 70 2d 3e 61 4d 65 6d  pIn1 <= &p->aMem
1d885 5b 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20  [p->nMem] );.   
1d886 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31   ExpandBlob(pIn1
1d887 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66 66 69  );.    applyAffi
1d888 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e 61 6e 2e  nity(pIn1, u.an.
1d889 63 41 66 66 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  cAff, encoding);
1d88a 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20 7d  .    pIn1++;.  }
1d88b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1d88c 4f 70 63 6f 64 65 3a 20 4d 61 6b 65 52 65 63 6f  Opcode: MakeReco
1d88d 72 64 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  rd P1 P2 P3 P4 *
1d88e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 50  .**.** Convert P
1d88f 32 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69  2 registers begi
1d890 6e 6e 69 6e 67 20 77 69 74 68 20 50 31 20 69 6e  nning with P1 in
1d891 74 6f 20 61 20 73 69 6e 67 6c 65 20 65 6e 74 72  to a single entr
1d892 79 0a 2a 2a 20 73 75 69 74 61 62 6c 65 20 66 6f  y.** suitable fo
1d893 72 20 75 73 65 20 61 73 20 61 20 64 61 74 61 20  r use as a data 
1d894 72 65 63 6f 72 64 20 69 6e 20 61 20 64 61 74 61  record in a data
1d895 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 61 73  base table or as
1d896 20 61 20 6b 65 79 0a 2a 2a 20 69 6e 20 61 6e 20   a key.** in an 
1d897 69 6e 64 65 78 2e 20 20 54 68 65 20 64 65 74 61  index.  The deta
1d898 69 6c 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 61  ils of the forma
1d899 74 20 61 72 65 20 69 72 72 65 6c 65 76 61 6e 74  t are irrelevant
1d89a 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20 74   as long as.** t
1d89b 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63  he OP_Column opc
1d89c 6f 64 65 20 63 61 6e 20 64 65 63 6f 64 65 20 74  ode can decode t
1d89d 68 65 20 72 65 63 6f 72 64 20 6c 61 74 65 72 2e  he record later.
1d89e 0a 2a 2a 20 52 65 66 65 72 20 74 6f 20 73 6f 75  .** Refer to sou
1d89f 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74  rce code comment
1d8a0 73 20 66 6f 72 20 74 68 65 20 64 65 74 61 69 6c  s for the detail
1d8a1 73 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 0a  s of the record.
1d8a2 2a 2a 20 66 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a  ** format..**.**
1d8a3 20 50 34 20 6d 61 79 20 62 65 20 61 20 73 74 72   P4 may be a str
1d8a4 69 6e 67 20 74 68 61 74 20 69 73 20 50 32 20 63  ing that is P2 c
1d8a5 68 61 72 61 63 74 65 72 73 20 6c 6f 6e 67 2e 20  haracters long. 
1d8a6 20 54 68 65 20 6e 74 68 20 63 68 61 72 61 63 74   The nth charact
1d8a7 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 73 74 72  er of the.** str
1d8a8 69 6e 67 20 69 6e 64 69 63 61 74 65 73 20 74 68  ing indicates th
1d8a9 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74  e column affinit
1d8aa 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  y that should be
1d8ab 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 6e 74   used for the nt
1d8ac 68 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20 74 68  h.** field of th
1d8ad 65 20 69 6e 64 65 78 20 6b 65 79 2e 0a 2a 2a 0a  e index key..**.
1d8ae 2a 2a 20 54 68 65 20 6d 61 70 70 69 6e 67 20 66  ** The mapping f
1d8af 72 6f 6d 20 63 68 61 72 61 63 74 65 72 20 74 6f  rom character to
1d8b0 20 61 66 66 69 6e 69 74 79 20 69 73 20 67 69 76   affinity is giv
1d8b1 65 6e 20 62 79 20 74 68 65 20 53 51 4c 49 54 45  en by the SQLITE
1d8b2 5f 41 46 46 5f 0a 2a 2a 20 6d 61 63 72 6f 73 20  _AFF_.** macros 
1d8b3 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74  defined in sqlit
1d8b4 65 49 6e 74 2e 68 2e 0a 2a 2a 0a 2a 2a 20 49 66  eInt.h..**.** If
1d8b5 20 50 34 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e   P4 is NULL then
1d8b6 20 61 6c 6c 20 69 6e 64 65 78 20 66 69 65 6c 64   all index field
1d8b7 73 20 68 61 76 65 20 74 68 65 20 61 66 66 69 6e  s have the affin
1d8b8 69 74 79 20 4e 4f 4e 45 2e 0a 2a 2f 0a 63 61 73  ity NONE..*/.cas
1d8b9 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 3a  e OP_MakeRecord:
1d8ba 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1d8bb 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d8bc 65 64 20 69 6e 74 6f 20 75 2e 61 6f 20 2a 2f 0a  ed into u.ao */.
1d8bd 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64    u8 *zNewRecord
1d8be 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75  ;        /* A bu
1d8bf 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  ffer to hold the
1d8c0 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65   data for the ne
1d8c1 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65  w record */.  Me
1d8c2 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20  m *pRec;        
1d8c3 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
1d8c4 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20  record */.  u64 
1d8c5 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  nData;          
1d8c6 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1d8c7 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70  bytes of data sp
1d8c8 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64  ace */.  int nHd
1d8c9 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
1d8ca 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1d8cb 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61  es of header spa
1d8cc 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74  ce */.  i64 nByt
1d8cd 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
1d8ce 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71  * Data space req
1d8cf 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72  uired for this r
1d8d0 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
1d8d1 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 20 20 20  Zero;           
1d8d2 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a    /* Number of z
1d8d3 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65  ero bytes at the
1d8d4 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f   end of the reco
1d8d5 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72  rd */.  int nVar
1d8d6 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  int;           /
1d8d7 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1d8d8 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f  s in a varint */
1d8d9 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79  .  u32 serial_ty
1d8da 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70  pe;       /* Typ
1d8db 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d  e field */.  Mem
1d8dc 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 20   *pData0;       
1d8dd 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65      /* First fie
1d8de 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65  ld to be combine
1d8df 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72  d into the recor
1d8e0 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73  d */.  Mem *pLas
1d8e1 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
1d8e2 20 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74   Last field of t
1d8e3 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  he record */.  i
1d8e4 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  nt nField;      
1d8e5 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1d8e6 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65  of fields in the
1d8e7 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61   record */.  cha
1d8e8 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20  r *zAffinity;   
1d8e9 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e      /* The affin
1d8ea 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74  ity string for t
1d8eb 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  he record */.  i
1d8ec 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20  nt file_format; 
1d8ed 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f        /* File fo
1d8ee 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20  rmat to use for 
1d8ef 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e  encoding */.  in
1d8f0 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
1d8f1 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73       /* Space us
1d8f2 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64  ed in zNewRecord
1d8f3 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b  [] */.  int len;
1d8f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d8f5 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20 66 69  * Length of a fi
1d8f6 65 6c 64 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  eld */.#endif /*
1d8f7 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1d8f8 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6f   moved into u.ao
1d8f9 20 2a 2f 0a 0a 20 20 2f 2a 20 41 73 73 75 6d 69   */..  /* Assumi
1d8fa 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f  ng the record co
1d8fb 6e 74 61 69 6e 73 20 4e 20 66 69 65 6c 64 73 2c  ntains N fields,
1d8fc 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f 72 6d   the record form
1d8fd 61 74 20 6c 6f 6f 6b 73 0a 20 20 2a 2a 20 6c 69  at looks.  ** li
1d8fe 6b 65 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20  ke this:.  **.  
1d8ff 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ** -------------
1d900 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d901 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d902 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d903 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a  -----------.  **
1d904 20 7c 20 68 64 72 2d 73 69 7a 65 20 7c 20 74 79   | hdr-size | ty
1d905 70 65 20 30 20 7c 20 74 79 70 65 20 31 20 7c 20  pe 0 | type 1 | 
1d906 2e 2e 2e 20 7c 20 74 79 70 65 20 4e 2d 31 20 7c  ... | type N-1 |
1d907 20 64 61 74 61 30 20 7c 20 2e 2e 2e 20 7c 20 64   data0 | ... | d
1d908 61 74 61 20 4e 2d 31 20 7c 0a 20 20 2a 2a 20 2d  ata N-1 |.  ** -
1d909 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d90a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d90b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d90c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d90d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 0a 20 20 2a  -------.  **.  *
1d90e 2a 20 44 61 74 61 28 30 29 20 69 73 20 74 61 6b  * Data(0) is tak
1d90f 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72  en from register
1d910 20 50 31 2e 20 20 44 61 74 61 28 31 29 20 63 6f   P1.  Data(1) co
1d911 6d 65 73 20 66 72 6f 6d 20 72 65 67 69 73 74 65  mes from registe
1d912 72 20 50 31 2b 31 0a 20 20 2a 2a 20 61 6e 64 20  r P1+1.  ** and 
1d913 73 6f 20 66 72 6f 74 68 2e 0a 20 20 2a 2a 0a 20  so froth..  **. 
1d914 20 2a 2a 20 45 61 63 68 20 74 79 70 65 20 66 69   ** Each type fi
1d915 65 6c 64 20 69 73 20 61 20 76 61 72 69 6e 74 20  eld is a varint 
1d916 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65  representing the
1d917 20 73 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20   serial type of 
1d918 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72 65 73 70  the.  ** corresp
1d919 6f 6e 64 69 6e 67 20 64 61 74 61 20 65 6c 65 6d  onding data elem
1d91a 65 6e 74 20 28 73 65 65 20 73 71 6c 69 74 65 33  ent (see sqlite3
1d91b 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 29  VdbeSerialType()
1d91c 29 2e 20 54 68 65 0a 20 20 2a 2a 20 68 64 72 2d  ). The.  ** hdr-
1d91d 73 69 7a 65 20 66 69 65 6c 64 20 69 73 20 61 6c  size field is al
1d91e 73 6f 20 61 20 76 61 72 69 6e 74 20 77 68 69 63  so a varint whic
1d91f 68 20 69 73 20 74 68 65 20 6f 66 66 73 65 74 20  h is the offset 
1d920 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69  from the beginni
1d921 6e 67 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 72  ng.  ** of the r
1d922 65 63 6f 72 64 20 74 6f 20 64 61 74 61 30 2e 0a  ecord to data0..
1d923 20 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 44 61 74    */.  u.ao.nDat
1d924 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  a = 0;         /
1d925 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1d926 73 20 6f 66 20 64 61 74 61 20 73 70 61 63 65 20  s of data space 
1d927 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 48 64 72 20 3d  */.  u.ao.nHdr =
1d928 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
1d929 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1d92a 6f 66 20 68 65 61 64 65 72 20 73 70 61 63 65 20  of header space 
1d92b 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20  */.  u.ao.nByte 
1d92c 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
1d92d 44 61 74 61 20 73 70 61 63 65 20 72 65 71 75 69  Data space requi
1d92e 72 65 64 20 66 6f 72 20 74 68 69 73 20 72 65 63  red for this rec
1d92f 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 5a  ord */.  u.ao.nZ
1d930 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ero = 0;        
1d931 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65   /* Number of ze
1d932 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20  ro bytes at the 
1d933 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72  end of the recor
1d934 64 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e 46 69 65  d */.  u.ao.nFie
1d935 6c 64 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  ld = pOp->p1;.  
1d936 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74 79 20 3d  u.ao.zAffinity =
1d937 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 61 73   pOp->p4.z;.  as
1d938 73 65 72 74 28 20 75 2e 61 6f 2e 6e 46 69 65 6c  sert( u.ao.nFiel
1d939 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3e 30  d>0 && pOp->p2>0
1d93a 20 26 26 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 6f   && pOp->p2+u.ao
1d93b 2e 6e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d  .nField<=p->nMem
1d93c 2b 31 20 29 3b 0a 20 20 75 2e 61 6f 2e 70 44 61  +1 );.  u.ao.pDa
1d93d 74 61 30 20 3d 20 26 61 4d 65 6d 5b 75 2e 61 6f  ta0 = &aMem[u.ao
1d93e 2e 6e 46 69 65 6c 64 5d 3b 0a 20 20 75 2e 61 6f  .nField];.  u.ao
1d93f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70  .nField = pOp->p
1d940 32 3b 0a 20 20 75 2e 61 6f 2e 70 4c 61 73 74 20  2;.  u.ao.pLast 
1d941 3d 20 26 75 2e 61 6f 2e 70 44 61 74 61 30 5b 75  = &u.ao.pData0[u
1d942 2e 61 6f 2e 6e 46 69 65 6c 64 2d 31 5d 3b 0a 20  .ao.nField-1];. 
1d943 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d 61   u.ao.file_forma
1d944 74 20 3d 20 70 2d 3e 6d 69 6e 57 72 69 74 65 46  t = p->minWriteF
1d945 69 6c 65 46 6f 72 6d 61 74 3b 0a 0a 20 20 2f 2a  ileFormat;..  /*
1d946 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68   Loop through th
1d947 65 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20  e elements that 
1d948 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74 68 65  will make up the
1d949 20 72 65 63 6f 72 64 20 74 6f 20 66 69 67 75 72   record to figur
1d94a 65 0a 20 20 2a 2a 20 6f 75 74 20 68 6f 77 20 6d  e.  ** out how m
1d94b 75 63 68 20 73 70 61 63 65 20 69 73 20 72 65 71  uch space is req
1d94c 75 69 72 65 64 20 66 6f 72 20 74 68 65 20 6e 65  uired for the ne
1d94d 77 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20  w record..  */. 
1d94e 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d 75   for(u.ao.pRec=u
1d94f 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 6f  .ao.pData0; u.ao
1d950 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 73  .pRec<=u.ao.pLas
1d951 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 7b  t; u.ao.pRec++){
1d952 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e 7a 41  .    if( u.ao.zA
1d953 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20 20  ffinity ){.     
1d954 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 75   applyAffinity(u
1d955 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f 2e 7a  .ao.pRec, u.ao.z
1d956 41 66 66 69 6e 69 74 79 5b 75 2e 61 6f 2e 70 52  Affinity[u.ao.pR
1d957 65 63 2d 75 2e 61 6f 2e 70 44 61 74 61 30 5d 2c  ec-u.ao.pData0],
1d958 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20   encoding);.    
1d959 7d 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e 70  }.    if( u.ao.p
1d95a 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 5a  Rec->flags&MEM_Z
1d95b 65 72 6f 20 26 26 20 75 2e 61 6f 2e 70 52 65 63  ero && u.ao.pRec
1d95c 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 73  ->n>0 ){.      s
1d95d 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
1d95e 61 6e 64 42 6c 6f 62 28 75 2e 61 6f 2e 70 52 65  andBlob(u.ao.pRe
1d95f 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e  c);.    }.    u.
1d960 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 20 3d  ao.serial_type =
1d961 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1d962 61 6c 54 79 70 65 28 75 2e 61 6f 2e 70 52 65 63  alType(u.ao.pRec
1d963 2c 20 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72 6d  , u.ao.file_form
1d964 61 74 29 3b 0a 20 20 20 20 75 2e 61 6f 2e 6c 65  at);.    u.ao.le
1d965 6e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  n = sqlite3VdbeS
1d966 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 75 2e 61  erialTypeLen(u.a
1d967 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a  o.serial_type);.
1d968 20 20 20 20 75 2e 61 6f 2e 6e 44 61 74 61 20 2b      u.ao.nData +
1d969 3d 20 75 2e 61 6f 2e 6c 65 6e 3b 0a 20 20 20 20  = u.ao.len;.    
1d96a 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 73 71 6c  u.ao.nHdr += sql
1d96b 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 2e  ite3VarintLen(u.
1d96c 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29 3b  ao.serial_type);
1d96d 0a 20 20 20 20 69 66 28 20 75 2e 61 6f 2e 70 52  .    if( u.ao.pR
1d96e 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ec->flags & MEM_
1d96f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Zero ){.      /*
1d970 20 4f 6e 6c 79 20 70 75 72 65 20 7a 65 72 6f 2d   Only pure zero-
1d971 66 69 6c 6c 65 64 20 42 4c 4f 42 73 20 63 61 6e  filled BLOBs can
1d972 20 62 65 20 69 6e 70 75 74 20 74 6f 20 74 68 69   be input to thi
1d973 73 20 4f 70 63 6f 64 65 2e 0a 20 20 20 20 20 20  s Opcode..      
1d974 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 61 6c 6c  ** We do not all
1d975 6f 77 20 62 6c 6f 62 73 20 77 69 74 68 20 61 20  ow blobs with a 
1d976 70 72 65 66 69 78 20 61 6e 64 20 61 20 7a 65 72  prefix and a zer
1d977 6f 2d 66 69 6c 6c 65 64 20 74 61 69 6c 2e 20 2a  o-filled tail. *
1d978 2f 0a 20 20 20 20 20 20 75 2e 61 6f 2e 6e 5a 65  /.      u.ao.nZe
1d979 72 6f 20 2b 3d 20 75 2e 61 6f 2e 70 52 65 63 2d  ro += u.ao.pRec-
1d97a 3e 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65  >u.nZero;.    }e
1d97b 6c 73 65 20 69 66 28 20 75 2e 61 6f 2e 6c 65 6e  lse if( u.ao.len
1d97c 20 29 7b 0a 20 20 20 20 20 20 75 2e 61 6f 2e 6e   ){.      u.ao.n
1d97d 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  Zero = 0;.    }.
1d97e 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68    }..  /* Add th
1d97f 65 20 69 6e 69 74 69 61 6c 20 68 65 61 64 65 72  e initial header
1d980 20 76 61 72 69 6e 74 20 61 6e 64 20 74 6f 74 61   varint and tota
1d981 6c 20 74 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20  l the size */.  
1d982 75 2e 61 6f 2e 6e 48 64 72 20 2b 3d 20 75 2e 61  u.ao.nHdr += u.a
1d983 6f 2e 6e 56 61 72 69 6e 74 20 3d 20 73 71 6c 69  o.nVarint = sqli
1d984 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 2e 61  te3VarintLen(u.a
1d985 6f 2e 6e 48 64 72 29 3b 0a 20 20 69 66 28 20 75  o.nHdr);.  if( u
1d986 2e 61 6f 2e 6e 56 61 72 69 6e 74 3c 73 71 6c 69  .ao.nVarint<sqli
1d987 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 2e 61  te3VarintLen(u.a
1d988 6f 2e 6e 48 64 72 29 20 29 7b 0a 20 20 20 20 75  o.nHdr) ){.    u
1d989 2e 61 6f 2e 6e 48 64 72 2b 2b 3b 0a 20 20 7d 0a  .ao.nHdr++;.  }.
1d98a 20 20 75 2e 61 6f 2e 6e 42 79 74 65 20 3d 20 75    u.ao.nByte = u
1d98b 2e 61 6f 2e 6e 48 64 72 2b 75 2e 61 6f 2e 6e 44  .ao.nHdr+u.ao.nD
1d98c 61 74 61 2d 75 2e 61 6f 2e 6e 5a 65 72 6f 3b 0a  ata-u.ao.nZero;.
1d98d 20 20 69 66 28 20 75 2e 61 6f 2e 6e 42 79 74 65    if( u.ao.nByte
1d98e 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  >db->aLimit[SQLI
1d98f 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
1d990 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f   ){.    goto too
1d991 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  _big;.  }..  /* 
1d992 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 75  Make sure the ou
1d993 74 70 75 74 20 72 65 67 69 73 74 65 72 20 68 61  tput register ha
1d994 73 20 61 20 62 75 66 66 65 72 20 6c 61 72 67 65  s a buffer large
1d995 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f 72 65   enough to store
1d996 0a 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 72 65  .  ** the new re
1d997 63 6f 72 64 2e 20 54 68 65 20 6f 75 74 70 75 74  cord. The output
1d998 20 72 65 67 69 73 74 65 72 20 28 70 4f 70 2d 3e   register (pOp->
1d999 70 33 29 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77  p3) is not allow
1d99a 65 64 20 74 6f 0a 20 20 2a 2a 20 62 65 20 6f 6e  ed to.  ** be on
1d99b 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 72  e of the input r
1d99c 65 67 69 73 74 65 72 73 20 28 62 65 63 61 75 73  egisters (becaus
1d99d 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
1d99e 63 61 6c 6c 20 74 6f 0a 20 20 2a 2a 20 73 71 6c  call to.  ** sql
1d99f 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1d9a0 29 20 63 6f 75 6c 64 20 63 6c 6f 62 62 65 72 20  ) could clobber 
1d9a1 74 68 65 20 76 61 6c 75 65 20 62 65 66 6f 72 65  the value before
1d9a2 20 69 74 20 69 73 20 75 73 65 64 29 2e 0a 20 20   it is used)..  
1d9a3 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1d9a4 2d 3e 70 33 3c 70 4f 70 2d 3e 70 31 20 7c 7c 20  ->p3<pOp->p1 || 
1d9a5 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 31  pOp->p3>=pOp->p1
1d9a6 2b 70 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 70 4f  +pOp->p2 );.  pO
1d9a7 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  ut = &aMem[pOp->
1d9a8 70 33 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  p3];.  if( sqlit
1d9a9 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f  e3VdbeMemGrow(pO
1d9aa 75 74 2c 20 28 69 6e 74 29 75 2e 61 6f 2e 6e 42  ut, (int)u.ao.nB
1d9ab 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67  yte, 0) ){.    g
1d9ac 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1d9ad 20 20 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72    u.ao.zNewRecor
1d9ae 64 20 3d 20 28 75 38 20 2a 29 70 4f 75 74 2d 3e  d = (u8 *)pOut->
1d9af 7a 3b 0a 0a 20 20 2f 2a 20 57 72 69 74 65 20 74  z;..  /* Write t
1d9b0 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75  he record */.  u
1d9b1 2e 61 6f 2e 69 20 3d 20 70 75 74 56 61 72 69 6e  .ao.i = putVarin
1d9b2 74 33 32 28 75 2e 61 6f 2e 7a 4e 65 77 52 65 63  t32(u.ao.zNewRec
1d9b3 6f 72 64 2c 20 75 2e 61 6f 2e 6e 48 64 72 29 3b  ord, u.ao.nHdr);
1d9b4 0a 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63  .  for(u.ao.pRec
1d9b5 3d 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e  =u.ao.pData0; u.
1d9b6 61 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c  ao.pRec<=u.ao.pL
1d9b7 61 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b  ast; u.ao.pRec++
1d9b8 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 73 65 72 69  ){.    u.ao.seri
1d9b9 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65  al_type = sqlite
1d9ba 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28  3VdbeSerialType(
1d9bb 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f 2e  u.ao.pRec, u.ao.
1d9bc 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20  file_format);.  
1d9bd 20 20 75 2e 61 6f 2e 69 20 2b 3d 20 70 75 74 56    u.ao.i += putV
1d9be 61 72 69 6e 74 33 32 28 26 75 2e 61 6f 2e 7a 4e  arint32(&u.ao.zN
1d9bf 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d  ewRecord[u.ao.i]
1d9c0 2c 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79  , u.ao.serial_ty
1d9c1 70 65 29 3b 20 20 20 20 20 20 2f 2a 20 73 65 72  pe);      /* ser
1d9c2 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a  ial type */.  }.
1d9c3 20 20 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d    for(u.ao.pRec=
1d9c4 75 2e 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61  u.ao.pData0; u.a
1d9c5 6f 2e 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61  o.pRec<=u.ao.pLa
1d9c6 73 74 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29  st; u.ao.pRec++)
1d9c7 7b 20 20 2f 2a 20 73 65 72 69 61 6c 20 64 61 74  {  /* serial dat
1d9c8 61 20 2a 2f 0a 20 20 20 20 75 2e 61 6f 2e 69 20  a */.    u.ao.i 
1d9c9 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  += sqlite3VdbeSe
1d9ca 72 69 61 6c 50 75 74 28 26 75 2e 61 6f 2e 7a 4e  rialPut(&u.ao.zN
1d9cb 65 77 52 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d  ewRecord[u.ao.i]
1d9cc 2c 20 28 69 6e 74 29 28 75 2e 61 6f 2e 6e 42 79  , (int)(u.ao.nBy
1d9cd 74 65 2d 75 2e 61 6f 2e 69 29 2c 20 75 2e 61 6f  te-u.ao.i), u.ao
1d9ce 2e 70 52 65 63 2c 75 2e 61 6f 2e 66 69 6c 65 5f  .pRec,u.ao.file_
1d9cf 66 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61  format);.  }.  a
1d9d0 73 73 65 72 74 28 20 75 2e 61 6f 2e 69 3d 3d 75  ssert( u.ao.i==u
1d9d1 2e 61 6f 2e 6e 42 79 74 65 20 29 3b 0a 0a 20 20  .ao.nByte );..  
1d9d2 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
1d9d3 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  0 && pOp->p3<=p-
1d9d4 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 2d  >nMem );.  pOut-
1d9d5 3e 6e 20 3d 20 28 69 6e 74 29 75 2e 61 6f 2e 6e  >n = (int)u.ao.n
1d9d6 42 79 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c  Byte;.  pOut->fl
1d9d7 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c  ags = MEM_Blob |
1d9d8 20 4d 45 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74   MEM_Dyn;.  pOut
1d9d9 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66  ->xDel = 0;.  if
1d9da 28 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 29 7b 0a  ( u.ao.nZero ){.
1d9db 20 20 20 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72      pOut->u.nZer
1d9dc 6f 20 3d 20 75 2e 61 6f 2e 6e 5a 65 72 6f 3b 0a  o = u.ao.nZero;.
1d9dd 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20      pOut->flags 
1d9de 7c 3d 20 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d  |= MEM_Zero;.  }
1d9df 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53  .  pOut->enc = S
1d9e0 51 4c 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20  QLITE_UTF8;  /* 
1d9e1 49 6e 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62  In case the blob
1d9e2 20 69 73 20 65 76 65 72 20 63 6f 6e 76 65 72 74   is ever convert
1d9e3 65 64 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20  ed to text */.  
1d9e4 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1d9e5 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20  Op->p3, pOut);. 
1d9e6 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42   UPDATE_MAX_BLOB
1d9e7 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72  SIZE(pOut);.  br
1d9e8 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1d9e9 65 3a 20 43 6f 75 6e 74 20 50 31 20 50 32 20 2a  e: Count P1 P2 *
1d9ea 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65   * *.**.** Store
1d9eb 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
1d9ec 6e 74 72 69 65 73 20 28 61 6e 20 69 6e 74 65 67  ntries (an integ
1d9ed 65 72 20 76 61 6c 75 65 29 20 69 6e 20 74 68 65  er value) in the
1d9ee 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1d9ef 0a 2a 2a 20 6f 70 65 6e 65 64 20 62 79 20 63 75  .** opened by cu
1d9f0 72 73 6f 72 20 50 31 20 69 6e 20 72 65 67 69 73  rsor P1 in regis
1d9f1 74 65 72 20 50 32 0a 2a 2f 0a 23 69 66 6e 64 65  ter P2.*/.#ifnde
1d9f2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54  f SQLITE_OMIT_BT
1d9f3 52 45 45 43 4f 55 4e 54 0a 63 61 73 65 20 4f 50  REECOUNT.case OP
1d9f4 5f 43 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20 20  _Count: {       
1d9f5 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1d9f6 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f  ease */.#if 0  /
1d9f7 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d9f8 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d9f9 70 20 2a 2f 0a 20 20 69 36 34 20 6e 45 6e 74 72  p */.  i64 nEntr
1d9fa 79 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  y;.  BtCursor *p
1d9fb 43 72 73 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  Crsr;.#endif /* 
1d9fc 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d9fd 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 70 20  moved into u.ap 
1d9fe 2a 2f 0a 0a 20 20 75 2e 61 70 2e 70 43 72 73 72  */..  u.ap.pCrsr
1d9ff 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1da00 3e 70 31 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  >p1]->pCursor;. 
1da01 20 69 66 28 20 75 2e 61 70 2e 70 43 72 73 72 20   if( u.ap.pCrsr 
1da02 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1da03 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28 75 2e  te3BtreeCount(u.
1da04 61 70 2e 70 43 72 73 72 2c 20 26 75 2e 61 70 2e  ap.pCrsr, &u.ap.
1da05 6e 45 6e 74 72 79 29 3b 0a 20 20 7d 65 6c 73 65  nEntry);.  }else
1da06 7b 0a 20 20 20 20 75 2e 61 70 2e 6e 45 6e 74 72  {.    u.ap.nEntr
1da07 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4f 75  y = 0;.  }.  pOu
1da08 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 70 2e 6e 45  t->u.i = u.ap.nE
1da09 6e 74 72 79 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  ntry;.  break;.}
1da0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f  .#endif../* Opco
1da0b 64 65 3a 20 53 61 76 65 70 6f 69 6e 74 20 50 31  de: Savepoint P1
1da0c 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20   * * P4 *.**.** 
1da0d 4f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72  Open, release or
1da0e 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 73 61   rollback the sa
1da0f 76 65 70 6f 69 6e 74 20 6e 61 6d 65 64 20 62 79  vepoint named by
1da10 20 70 61 72 61 6d 65 74 65 72 20 50 34 2c 20 64   parameter P4, d
1da11 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e 20 74  epending.** on t
1da12 68 65 20 76 61 6c 75 65 20 6f 66 20 50 31 2e 20  he value of P1. 
1da13 54 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 73 61  To open a new sa
1da14 76 65 70 6f 69 6e 74 2c 20 50 31 3d 3d 30 2e 20  vepoint, P1==0. 
1da15 54 6f 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d  To release (comm
1da16 69 74 29 20 61 6e 0a 2a 2a 20 65 78 69 73 74 69  it) an.** existi
1da17 6e 67 20 73 61 76 65 70 6f 69 6e 74 2c 20 50 31  ng savepoint, P1
1da18 3d 3d 31 2c 20 6f 72 20 74 6f 20 72 6f 6c 6c 62  ==1, or to rollb
1da19 61 63 6b 20 61 6e 20 65 78 69 73 74 69 6e 67 20  ack an existing 
1da1a 73 61 76 65 70 6f 69 6e 74 20 50 31 3d 3d 32 2e  savepoint P1==2.
1da1b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 61 76 65  .*/.case OP_Save
1da1c 70 6f 69 6e 74 3a 20 7b 0a 23 69 66 20 30 20 20  point: {.#if 0  
1da1d 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1da1e 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1da1f 61 71 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b 20  aq */.  int p1; 
1da20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1da21 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
1da22 20 6f 66 20 50 31 20 6f 70 65 72 61 6e 64 20 2a   of P1 operand *
1da23 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  /.  char *zName;
1da24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1da25 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73      /* Name of s
1da26 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 69 6e  avepoint */.  in
1da27 74 20 6e 4e 61 6d 65 3b 0a 20 20 53 61 76 65 70  t nName;.  Savep
1da28 6f 69 6e 74 20 2a 70 4e 65 77 3b 0a 20 20 53 61  oint *pNew;.  Sa
1da29 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65 70 6f  vepoint *pSavepo
1da2a 69 6e 74 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74  int;.  Savepoint
1da2b 20 2a 70 54 6d 70 3b 0a 20 20 69 6e 74 20 69 53   *pTmp;.  int iS
1da2c 61 76 65 70 6f 69 6e 74 3b 0a 20 20 69 6e 74 20  avepoint;.  int 
1da2d 69 69 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  ii;.#endif /* lo
1da2e 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1da2f 76 65 64 20 69 6e 74 6f 20 75 2e 61 71 20 2a 2f  ved into u.aq */
1da30 0a 0a 20 20 75 2e 61 71 2e 70 31 20 3d 20 70 4f  ..  u.aq.p1 = pO
1da31 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 71 2e 7a 4e  p->p1;.  u.aq.zN
1da32 61 6d 65 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b  ame = pOp->p4.z;
1da33 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68  ..  /* Assert th
1da34 61 74 20 74 68 65 20 75 2e 61 71 2e 70 31 20 70  at the u.aq.p1 p
1da35 61 72 61 6d 65 74 65 72 20 69 73 20 76 61 6c 69  arameter is vali
1da36 64 2e 20 41 6c 73 6f 20 74 68 61 74 20 69 66 20  d. Also that if 
1da37 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e  there is no open
1da38 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  .  ** transactio
1da39 6e 2c 20 74 68 65 6e 20 74 68 65 72 65 20 63 61  n, then there ca
1da3a 6e 6e 6f 74 20 62 65 20 61 6e 79 20 73 61 76 65  nnot be any save
1da3b 70 6f 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 61  points..  */.  a
1da3c 73 73 65 72 74 28 20 64 62 2d 3e 70 53 61 76 65  ssert( db->pSave
1da3d 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e  point==0 || db->
1da3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b  autoCommit==0 );
1da3f 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 71 2e  .  assert( u.aq.
1da40 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45  p1==SAVEPOINT_BE
1da41 47 49 4e 7c 7c 75 2e 61 71 2e 70 31 3d 3d 53 41  GIN||u.aq.p1==SA
1da42 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 7c  VEPOINT_RELEASE|
1da43 7c 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f  |u.aq.p1==SAVEPO
1da44 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a  INT_ROLLBACK );.
1da45 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 70 53    assert( db->pS
1da46 61 76 65 70 6f 69 6e 74 20 7c 7c 20 64 62 2d 3e  avepoint || db->
1da47 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76  isTransactionSav
1da48 65 70 6f 69 6e 74 3d 3d 30 20 29 3b 0a 20 20 61  epoint==0 );.  a
1da49 73 73 65 72 74 28 20 63 68 65 63 6b 53 61 76 65  ssert( checkSave
1da4a 70 6f 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29  pointCount(db) )
1da4b 3b 0a 0a 20 20 69 66 28 20 75 2e 61 71 2e 70 31  ;..  if( u.aq.p1
1da4c 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49  ==SAVEPOINT_BEGI
1da4d 4e 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d  N ){.    if( db-
1da4e 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20  >writeVdbeCnt>0 
1da4f 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 6e 65  ){.      /* A ne
1da50 77 20 73 61 76 65 70 6f 69 6e 74 20 63 61 6e 6e  w savepoint cann
1da51 6f 74 20 62 65 20 63 72 65 61 74 65 64 20 69 66  ot be created if
1da52 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76   there are activ
1da53 65 20 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a  e write.      **
1da54 20 73 74 61 74 65 6d 65 6e 74 73 20 28 69 2e 65   statements (i.e
1da55 2e 20 6f 70 65 6e 20 72 65 61 64 2f 77 72 69 74  . open read/writ
1da56 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c  e incremental bl
1da57 6f 62 20 68 61 6e 64 6c 65 73 29 2e 0a 20 20 20  ob handles)..   
1da58 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
1da59 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1da5a 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63  >zErrMsg, db, "c
1da5b 61 6e 6e 6f 74 20 6f 70 65 6e 20 73 61 76 65 70  annot open savep
1da5c 6f 69 6e 74 20 2d 20 22 0a 20 20 20 20 20 20 20  oint - ".       
1da5d 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73   "SQL statements
1da5e 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b 0a   in progress");.
1da5f 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1da60 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73  E_BUSY;.    }els
1da61 65 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e 6e 4e  e{.      u.aq.nN
1da62 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
1da63 6c 65 6e 33 30 28 75 2e 61 71 2e 7a 4e 61 6d 65  len30(u.aq.zName
1da64 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 72 65  );..      /* Cre
1da65 61 74 65 20 61 20 6e 65 77 20 73 61 76 65 70 6f  ate a new savepo
1da66 69 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 2a  int structure. *
1da67 2f 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 4e 65  /.      u.aq.pNe
1da68 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
1da69 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
1da6a 66 28 53 61 76 65 70 6f 69 6e 74 29 2b 75 2e 61  f(Savepoint)+u.a
1da6b 71 2e 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20  q.nName+1);.    
1da6c 20 20 69 66 28 20 75 2e 61 71 2e 70 4e 65 77 20    if( u.aq.pNew 
1da6d 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e  ){.        u.aq.
1da6e 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63  pNew->zName = (c
1da6f 68 61 72 20 2a 29 26 75 2e 61 71 2e 70 4e 65 77  har *)&u.aq.pNew
1da70 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d  [1];.        mem
1da71 63 70 79 28 75 2e 61 71 2e 70 4e 65 77 2d 3e 7a  cpy(u.aq.pNew->z
1da72 4e 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65  Name, u.aq.zName
1da73 2c 20 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29 3b  , u.aq.nName+1);
1da74 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  ..        /* If 
1da75 74 68 65 72 65 20 69 73 20 6e 6f 20 6f 70 65 6e  there is no open
1da76 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68   transaction, th
1da77 65 6e 20 6d 61 72 6b 20 74 68 69 73 20 61 73 20  en mark this as 
1da78 61 20 73 70 65 63 69 61 6c 0a 20 20 20 20 20 20  a special.      
1da79 20 20 2a 2a 20 22 74 72 61 6e 73 61 63 74 69 6f    ** "transactio
1da7a 6e 20 73 61 76 65 70 6f 69 6e 74 22 2e 20 2a 2f  n savepoint". */
1da7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d  .        if( db-
1da7c 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
1da7d 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74           db->aut
1da7e 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20 20  oCommit = 0;.   
1da7f 20 20 20 20 20 20 20 64 62 2d 3e 69 73 54 72 61         db->isTra
1da80 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e  nsactionSavepoin
1da81 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  t = 1;.        }
1da82 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
1da83 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2b 2b  db->nSavepoint++
1da84 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20  ;.        }..   
1da85 20 20 20 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65       /* Link the
1da86 20 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 20 69   new savepoint i
1da87 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
1da88 20 68 61 6e 64 6c 65 27 73 20 6c 69 73 74 2e 20   handle's list. 
1da89 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e  */.        u.aq.
1da8a 70 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 64 62  pNew->pNext = db
1da8b 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20  ->pSavepoint;.  
1da8c 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70        db->pSavep
1da8d 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 4e 65 77  oint = u.aq.pNew
1da8e 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 70  ;.        u.aq.p
1da8f 4e 65 77 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f  New->nDeferredCo
1da90 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72 72  ns = db->nDeferr
1da91 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d 0a  edCons;.      }.
1da92 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1da93 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69     u.aq.iSavepoi
1da94 6e 74 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20  nt = 0;..    /* 
1da95 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 64 20 73  Find the named s
1da96 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 74 68 65  avepoint. If the
1da97 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 73 61  re is no such sa
1da98 76 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 61 6e  vepoint, then an
1da99 0a 20 20 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72  .    ** an error
1da9a 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20   is returned to 
1da9b 74 68 65 20 75 73 65 72 2e 20 20 2a 2f 0a 20 20  the user.  */.  
1da9c 20 20 66 6f 72 28 0a 20 20 20 20 20 20 75 2e 61    for(.      u.a
1da9d 71 2e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 64  q.pSavepoint = d
1da9e 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20  b->pSavepoint;. 
1da9f 20 20 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70       u.aq.pSavep
1daa0 6f 69 6e 74 20 26 26 20 73 71 6c 69 74 65 33 53  oint && sqlite3S
1daa1 74 72 49 43 6d 70 28 75 2e 61 71 2e 70 53 61 76  trICmp(u.aq.pSav
1daa2 65 70 6f 69 6e 74 2d 3e 7a 4e 61 6d 65 2c 20 75  epoint->zName, u
1daa3 2e 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  .aq.zName);.    
1daa4 20 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e    u.aq.pSavepoin
1daa5 74 20 3d 20 75 2e 61 71 2e 70 53 61 76 65 70 6f  t = u.aq.pSavepo
1daa6 69 6e 74 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29  int->pNext.    )
1daa7 7b 0a 20 20 20 20 20 20 75 2e 61 71 2e 69 53 61  {.      u.aq.iSa
1daa8 76 65 70 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d  vepoint++;.    }
1daa9 0a 20 20 20 20 69 66 28 20 21 75 2e 61 71 2e 70  .    if( !u.aq.p
1daaa 53 61 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20  Savepoint ){.   
1daab 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1daac 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1daad 20 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 73 61   db, "no such sa
1daae 76 65 70 6f 69 6e 74 3a 20 25 73 22 2c 20 75 2e  vepoint: %s", u.
1daaf 61 71 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  aq.zName);.     
1dab0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1dab1 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  OR;.    }else if
1dab2 28 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 77 72  (.        db->wr
1dab3 69 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20  iteVdbeCnt>0 || 
1dab4 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f  (u.aq.p1==SAVEPO
1dab5 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20  INT_ROLLBACK && 
1dab6 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
1dab7 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20 20 20 20  t>1).    ){.    
1dab8 20 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70    /* It is not p
1dab9 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 6c 65 61  ossible to relea
1daba 73 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 61  se (commit) a sa
1dabb 76 65 70 6f 69 6e 74 20 69 66 20 74 68 65 72 65  vepoint if there
1dabc 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 63   are.      ** ac
1dabd 74 69 76 65 20 77 72 69 74 65 20 73 74 61 74 65  tive write state
1dabe 6d 65 6e 74 73 2e 20 49 74 20 69 73 20 6e 6f 74  ments. It is not
1dabf 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 6f 6c   possible to rol
1dac0 6c 62 61 63 6b 20 61 20 73 61 76 65 70 6f 69 6e  lback a savepoin
1dac1 74 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 74 68  t.      ** if th
1dac2 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74 69  ere are any acti
1dac3 76 65 20 73 74 61 74 65 6d 65 6e 74 73 20 61 74  ve statements at
1dac4 20 61 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20   all..      */. 
1dac5 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53       sqlite3SetS
1dac6 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
1dac7 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 22  g, db,.        "
1dac8 63 61 6e 6e 6f 74 20 25 73 20 73 61 76 65 70 6f  cannot %s savepo
1dac9 69 6e 74 20 2d 20 53 51 4c 20 73 74 61 74 65 6d  int - SQL statem
1daca 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73  ents in progress
1dacb 22 2c 0a 20 20 20 20 20 20 20 20 28 75 2e 61 71  ",.        (u.aq
1dacc 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52  .p1==SAVEPOINT_R
1dacd 4f 4c 4c 42 41 43 4b 20 3f 20 22 72 6f 6c 6c 62  OLLBACK ? "rollb
1dace 61 63 6b 22 3a 20 22 72 65 6c 65 61 73 65 22 29  ack": "release")
1dacf 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20  .      );.      
1dad0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
1dad1 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 0a 20 20  ;.    }else{..  
1dad2 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65      /* Determine
1dad3 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1dad4 74 68 69 73 20 69 73 20 61 20 74 72 61 6e 73 61  this is a transa
1dad5 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 2e  ction savepoint.
1dad6 20 49 66 20 73 6f 2c 0a 20 20 20 20 20 20 2a 2a   If so,.      **
1dad7 20 61 6e 64 20 74 68 69 73 20 69 73 20 61 20 52   and this is a R
1dad8 45 4c 45 41 53 45 20 63 6f 6d 6d 61 6e 64 2c 20  ELEASE command, 
1dad9 74 68 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74  then the current
1dada 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20   transaction.   
1dadb 20 20 20 2a 2a 20 69 73 20 63 6f 6d 6d 69 74 74     ** is committ
1dadc 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
1dadd 20 20 20 69 6e 74 20 69 73 54 72 61 6e 73 61 63     int isTransac
1dade 74 69 6f 6e 20 3d 20 75 2e 61 71 2e 70 53 61 76  tion = u.aq.pSav
1dadf 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3d 3d 30  epoint->pNext==0
1dae0 20 26 26 20 64 62 2d 3e 69 73 54 72 61 6e 73 61   && db->isTransa
1dae1 63 74 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3b 0a  ctionSavepoint;.
1dae2 20 20 20 20 20 20 69 66 28 20 69 73 54 72 61 6e        if( isTran
1dae3 73 61 63 74 69 6f 6e 20 26 26 20 75 2e 61 71 2e  saction && u.aq.
1dae4 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  p1==SAVEPOINT_RE
1dae5 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20 20 20  LEASE ){.       
1dae6 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c 69 74   if( (rc = sqlit
1dae7 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c  e3VdbeCheckFk(p,
1dae8 20 31 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20   1))!=SQLITE_OK 
1dae9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
1daea 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20  o vdbe_return;. 
1daeb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1daec 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
1daed 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28  = 1;.        if(
1daee 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74   sqlite3VdbeHalt
1daef 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59  (p)==SQLITE_BUSY
1daf0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d   ){.          p-
1daf1 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20  >pc = pc;.      
1daf2 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d      db->autoComm
1daf3 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  it = 0;.        
1daf4 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53    p->rc = rc = S
1daf5 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
1daf6 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f        goto vdbe_
1daf7 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20  return;.        
1daf8 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 69 73  }.        db->is
1daf9 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70  TransactionSavep
1dafa 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  oint = 0;.      
1dafb 20 20 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20    rc = p->rc;.  
1dafc 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1dafd 20 20 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69     u.aq.iSavepoi
1dafe 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f  nt = db->nSavepo
1daff 69 6e 74 20 2d 20 75 2e 61 71 2e 69 53 61 76 65  int - u.aq.iSave
1db00 70 6f 69 6e 74 20 2d 20 31 3b 0a 20 20 20 20 20  point - 1;.     
1db01 20 20 20 66 6f 72 28 75 2e 61 71 2e 69 69 3d 30     for(u.aq.ii=0
1db02 3b 20 75 2e 61 71 2e 69 69 3c 64 62 2d 3e 6e 44  ; u.aq.ii<db->nD
1db03 62 3b 20 75 2e 61 71 2e 69 69 2b 2b 29 7b 0a 20  b; u.aq.ii++){. 
1db04 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
1db05 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f  lite3BtreeSavepo
1db06 69 6e 74 28 64 62 2d 3e 61 44 62 5b 75 2e 61 71  int(db->aDb[u.aq
1db07 2e 69 69 5d 2e 70 42 74 2c 20 75 2e 61 71 2e 70  .ii].pBt, u.aq.p
1db08 31 2c 20 75 2e 61 71 2e 69 53 61 76 65 70 6f 69  1, u.aq.iSavepoi
1db09 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  nt);.          i
1db0a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1db0b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1db0c 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1db0d 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  o_error;.       
1db0e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
1db0f 20 20 20 20 20 20 20 69 66 28 20 75 2e 61 71 2e         if( u.aq.
1db10 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f  p1==SAVEPOINT_RO
1db11 4c 4c 42 41 43 4b 20 26 26 20 28 64 62 2d 3e 66  LLBACK && (db->f
1db12 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65  lags&SQLITE_Inte
1db13 72 6e 43 68 61 6e 67 65 73 29 21 3d 30 20 29 7b  rnChanges)!=0 ){
1db14 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1db15 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
1db16 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
1db17 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1db18 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
1db19 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 20  hema(db, 0);.   
1db1a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
1db1b 20 20 20 20 20 20 2f 2a 20 52 65 67 61 72 64 6c        /* Regardl
1db1c 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 74  ess of whether t
1db1d 68 69 73 20 69 73 20 61 20 52 45 4c 45 41 53 45  his is a RELEASE
1db1e 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2c 20 64 65   or ROLLBACK, de
1db1f 73 74 72 6f 79 20 61 6c 6c 0a 20 20 20 20 20 20  stroy all.      
1db20 2a 2a 20 73 61 76 65 70 6f 69 6e 74 73 20 6e 65  ** savepoints ne
1db21 73 74 65 64 20 69 6e 73 69 64 65 20 6f 66 20 74  sted inside of t
1db22 68 65 20 73 61 76 65 70 6f 69 6e 74 20 62 65 69  he savepoint bei
1db23 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 2e 20  ng operated on. 
1db24 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  */.      while( 
1db25 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 21 3d  db->pSavepoint!=
1db26 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20  u.aq.pSavepoint 
1db27 29 7b 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e  ){.        u.aq.
1db28 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61 76 65  pTmp = db->pSave
1db29 70 6f 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 64  point;.        d
1db2a 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20  b->pSavepoint = 
1db2b 75 2e 61 71 2e 70 54 6d 70 2d 3e 70 4e 65 78 74  u.aq.pTmp->pNext
1db2c 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1db2d 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e 61 71  3DbFree(db, u.aq
1db2e 2e 70 54 6d 70 29 3b 0a 20 20 20 20 20 20 20 20  .pTmp);.        
1db2f 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 2d 2d  db->nSavepoint--
1db30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
1db31 20 2f 2a 20 49 66 20 69 74 20 69 73 20 61 20 52   /* If it is a R
1db32 45 4c 45 41 53 45 2c 20 74 68 65 6e 20 64 65 73  ELEASE, then des
1db33 74 72 6f 79 20 74 68 65 20 73 61 76 65 70 6f 69  troy the savepoi
1db34 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65  nt being operate
1db35 64 20 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 74 6f  d on.      ** to
1db36 6f 2e 20 49 66 20 69 74 20 69 73 20 61 20 52 4f  o. If it is a RO
1db37 4c 4c 42 41 43 4b 20 54 4f 2c 20 74 68 65 6e 20  LLBACK TO, then 
1db38 73 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  set the number o
1db39 66 20 64 65 66 65 72 72 65 64 0a 20 20 20 20 20  f deferred.     
1db3a 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 76   ** constraint v
1db3b 69 6f 6c 61 74 69 6f 6e 73 20 70 72 65 73 65 6e  iolations presen
1db3c 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  t in the databas
1db3d 65 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20 73  e to the value s
1db3e 74 6f 72 65 64 0a 20 20 20 20 20 20 2a 2a 20 77  tored.      ** w
1db3f 68 65 6e 20 74 68 65 20 73 61 76 65 70 6f 69 6e  hen the savepoin
1db40 74 20 77 61 73 20 63 72 65 61 74 65 64 2e 20 20  t was created.  
1db41 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61  */.      if( u.a
1db42 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f  q.p1==SAVEPOINT_
1db43 52 45 4c 45 41 53 45 20 29 7b 0a 20 20 20 20 20  RELEASE ){.     
1db44 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 71 2e     assert( u.aq.
1db45 70 53 61 76 65 70 6f 69 6e 74 3d 3d 64 62 2d 3e  pSavepoint==db->
1db46 70 53 61 76 65 70 6f 69 6e 74 20 29 3b 0a 20 20  pSavepoint );.  
1db47 20 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70        db->pSavep
1db48 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70 53 61 76  oint = u.aq.pSav
1db49 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74 3b 0a 20  epoint->pNext;. 
1db4a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
1db4b 46 72 65 65 28 64 62 2c 20 75 2e 61 71 2e 70 53  Free(db, u.aq.pS
1db4c 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20  avepoint);.     
1db4d 20 20 20 69 66 28 20 21 69 73 54 72 61 6e 73 61     if( !isTransa
1db4e 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20  ction ){.       
1db4f 20 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e     db->nSavepoin
1db50 74 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  t--;.        }. 
1db51 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1db52 20 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65      db->nDeferre
1db53 64 43 6f 6e 73 20 3d 20 75 2e 61 71 2e 70 53 61  dCons = u.aq.pSa
1db54 76 65 70 6f 69 6e 74 2d 3e 6e 44 65 66 65 72 72  vepoint->nDeferr
1db55 65 64 43 6f 6e 73 3b 0a 20 20 20 20 20 20 7d 0a  edCons;.      }.
1db56 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 62 72 65      }.  }..  bre
1db57 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1db58 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31 20  : AutoCommit P1 
1db59 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  P2 * * *.**.** S
1db5a 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  et the database 
1db5b 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67  auto-commit flag
1db5c 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29 2e   to P1 (1 or 0).
1db5d 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c 20   If P2 is true, 
1db5e 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e 79  roll.** back any
1db5f 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76   currently activ
1db60 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63 74  e btree transact
1db61 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20 61  ions. If there a
1db62 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a 2a  re any active.**
1db63 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f 6d   VMs (apart from
1db64 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65 6e   this one), then
1db65 20 61 20 52 4f 4c 4c 42 41 43 4b 20 66 61 69 6c   a ROLLBACK fail
1db66 73 2e 20 20 41 20 43 4f 4d 4d 49 54 20 66 61 69  s.  A COMMIT fai
1db67 6c 73 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 61  ls if.** there a
1db68 72 65 20 61 63 74 69 76 65 20 77 72 69 74 69 6e  re active writin
1db69 67 20 56 4d 73 20 6f 72 20 61 63 74 69 76 65 20  g VMs or active 
1db6a 56 4d 73 20 74 68 61 74 20 75 73 65 20 73 68 61  VMs that use sha
1db6b 72 65 64 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a  red cache..**.**
1db6c 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1db6d 6e 20 63 61 75 73 65 73 20 74 68 65 20 56 4d 20  n causes the VM 
1db6e 74 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65  to halt..*/.case
1db6f 20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a 20   OP_AutoCommit: 
1db70 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1db71 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1db72 64 20 69 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a 20  d into u.ar */. 
1db73 20 69 6e 74 20 64 65 73 69 72 65 64 41 75 74 6f   int desiredAuto
1db74 43 6f 6d 6d 69 74 3b 0a 20 20 69 6e 74 20 69 52  Commit;.  int iR
1db75 6f 6c 6c 62 61 63 6b 3b 0a 20 20 69 6e 74 20 74  ollback;.  int t
1db76 75 72 6e 4f 6e 41 43 3b 0a 23 65 6e 64 69 66 20  urnOnAC;.#endif 
1db77 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1db78 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1db79 61 72 20 2a 2f 0a 0a 20 20 75 2e 61 72 2e 64 65  ar */..  u.ar.de
1db7a 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20  siredAutoCommit 
1db7b 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61  = pOp->p1;.  u.a
1db7c 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 3d 20 70 4f  r.iRollback = pO
1db7d 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 72 2e 74 75  p->p2;.  u.ar.tu
1db7e 72 6e 4f 6e 41 43 20 3d 20 75 2e 61 72 2e 64 65  rnOnAC = u.ar.de
1db7f 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 20  siredAutoCommit 
1db80 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d  && !db->autoComm
1db81 69 74 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  it;.  assert( u.
1db82 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f  ar.desiredAutoCo
1db83 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75 2e 61 72 2e  mmit==1 || u.ar.
1db84 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
1db85 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  t==0 );.  assert
1db86 28 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75  ( u.ar.desiredAu
1db87 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c 20 75  toCommit==1 || u
1db88 2e 61 72 2e 69 52 6f 6c 6c 62 61 63 6b 3d 3d 30  .ar.iRollback==0
1db89 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62   );.  assert( db
1db8a 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e  ->activeVdbeCnt>
1db8b 30 20 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73  0 );  /* At leas
1db8c 74 20 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73  t this one VM is
1db8d 20 61 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66   active */..  if
1db8e 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20  ( u.ar.turnOnAC 
1db8f 26 26 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63  && u.ar.iRollbac
1db90 6b 20 26 26 20 64 62 2d 3e 61 63 74 69 76 65 56  k && db->activeV
1db91 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20  dbeCnt>1 ){.    
1db92 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74 72  /* If this instr
1db93 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  uction implement
1db94 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64  s a ROLLBACK and
1db95 20 6f 74 68 65 72 20 56 4d 73 20 61 72 65 0a 20   other VMs are. 
1db96 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72 75 6e 6e     ** still runn
1db97 69 6e 67 2c 20 61 6e 64 20 61 20 74 72 61 6e 73  ing, and a trans
1db98 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
1db99 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f  , return an erro
1db9a 72 20 69 6e 64 69 63 61 74 69 6e 67 0a 20 20 20  r indicating.   
1db9b 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 74 68   ** that the oth
1db9c 65 72 20 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70  er VMs must comp
1db9d 6c 65 74 65 20 66 69 72 73 74 2e 0a 20 20 20 20  lete first..    
1db9e 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  */.    sqlite3Se
1db9f 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1dba0 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74  Msg, db, "cannot
1dba1 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61   rollback transa
1dba2 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20 20 20  ction - ".      
1dba3 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65 6e 74    "SQL statement
1dba4 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22 29 3b  s in progress");
1dba5 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1dba6 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65 20 69  _BUSY;.  }else i
1dba7 66 28 20 75 2e 61 72 2e 74 75 72 6e 4f 6e 41 43  f( u.ar.turnOnAC
1dba8 20 26 26 20 21 75 2e 61 72 2e 69 52 6f 6c 6c 62   && !u.ar.iRollb
1dba9 61 63 6b 20 26 26 20 64 62 2d 3e 77 72 69 74 65  ack && db->write
1dbaa 56 64 62 65 43 6e 74 3e 30 20 29 7b 0a 20 20 20  VdbeCnt>0 ){.   
1dbab 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 73 74   /* If this inst
1dbac 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  ruction implemen
1dbad 74 73 20 61 20 43 4f 4d 4d 49 54 20 61 6e 64 20  ts a COMMIT and 
1dbae 6f 74 68 65 72 20 56 4d 73 20 61 72 65 20 77 72  other VMs are wr
1dbaf 69 74 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65 74  iting.    ** ret
1dbb0 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 64  urn an error ind
1dbb1 69 63 61 74 69 6e 67 20 74 68 61 74 20 74 68 65  icating that the
1dbb2 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20   other VMs must 
1dbb3 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 0a  complete first..
1dbb4 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
1dbb5 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1dbb6 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61  zErrMsg, db, "ca
1dbb7 6e 6e 6f 74 20 63 6f 6d 6d 69 74 20 74 72 61 6e  nnot commit tran
1dbb8 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20 20 20  saction - ".    
1dbb9 20 20 20 20 22 53 51 4c 20 73 74 61 74 65 6d 65      "SQL stateme
1dbba 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73 73 22  nts in progress"
1dbbb 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
1dbbc 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c 73 65  TE_BUSY;.  }else
1dbbd 20 69 66 28 20 75 2e 61 72 2e 64 65 73 69 72 65   if( u.ar.desire
1dbbe 64 41 75 74 6f 43 6f 6d 6d 69 74 21 3d 64 62 2d  dAutoCommit!=db-
1dbbf 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
1dbc0 20 20 20 69 66 28 20 75 2e 61 72 2e 69 52 6f 6c     if( u.ar.iRol
1dbc1 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20 61  lback ){.      a
1dbc2 73 73 65 72 74 28 20 75 2e 61 72 2e 64 65 73 69  ssert( u.ar.desi
1dbc3 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31  redAutoCommit==1
1dbc4 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
1dbc5 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29  3RollbackAll(db)
1dbc6 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f  ;.      db->auto
1dbc7 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20  Commit = 1;.    
1dbc8 7d 65 6c 73 65 20 69 66 28 20 28 72 63 20 3d 20  }else if( (rc = 
1dbc9 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b  sqlite3VdbeCheck
1dbca 46 6b 28 70 2c 20 31 29 29 21 3d 53 51 4c 49 54  Fk(p, 1))!=SQLIT
1dbcb 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
1dbcc 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a  to vdbe_return;.
1dbcd 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1dbce 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
1dbcf 3d 20 28 75 38 29 75 2e 61 72 2e 64 65 73 69 72  = (u8)u.ar.desir
1dbd0 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20  edAutoCommit;.  
1dbd1 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
1dbd2 64 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c 49  dbeHalt(p)==SQLI
1dbd3 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20  TE_BUSY ){.     
1dbd4 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a 20     p->pc = pc;. 
1dbd5 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
1dbd6 6f 6d 6d 69 74 20 3d 20 28 75 38 29 28 31 2d 75  ommit = (u8)(1-u
1dbd7 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43  .ar.desiredAutoC
1dbd8 6f 6d 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20  ommit);.        
1dbd9 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c  p->rc = rc = SQL
1dbda 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
1dbdb 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75    goto vdbe_retu
1dbdc 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rn;.      }.    
1dbdd 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62  }.    assert( db
1dbde 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 3d 3d 30 20  ->nStatement==0 
1dbdf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6c  );.    sqlite3Cl
1dbe0 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62  oseSavepoints(db
1dbe1 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63  );.    if( p->rc
1dbe2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1dbe3 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1dbe4 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65  _DONE;.    }else
1dbe5 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1dbe6 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d  ITE_ERROR;.    }
1dbe7 0a 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72  .    goto vdbe_r
1dbe8 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a  eturn;.  }else{.
1dbe9 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1dbea 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1dbeb 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 28 21  , db,.        (!
1dbec 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f  u.ar.desiredAuto
1dbed 43 6f 6d 6d 69 74 29 3f 22 63 61 6e 6e 6f 74 20  Commit)?"cannot 
1dbee 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74  start a transact
1dbef 69 6f 6e 20 77 69 74 68 69 6e 20 61 20 74 72 61  ion within a tra
1dbf0 6e 73 61 63 74 69 6f 6e 22 3a 28 0a 20 20 20 20  nsaction":(.    
1dbf1 20 20 20 20 28 75 2e 61 72 2e 69 52 6f 6c 6c 62      (u.ar.iRollb
1dbf2 61 63 6b 29 3f 22 63 61 6e 6e 6f 74 20 72 6f 6c  ack)?"cannot rol
1dbf3 6c 62 61 63 6b 20 2d 20 6e 6f 20 74 72 61 6e 73  lback - no trans
1dbf4 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65  action is active
1dbf5 22 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ":.             
1dbf6 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 63 6f        "cannot co
1dbf7 6d 6d 69 74 20 2d 20 6e 6f 20 74 72 61 6e 73 61  mmit - no transa
1dbf8 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 22  ction is active"
1dbf9 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 53 51  ));..    rc = SQ
1dbfa 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
1dbfb 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1dbfc 70 63 6f 64 65 3a 20 54 72 61 6e 73 61 63 74 69  pcode: Transacti
1dbfd 6f 6e 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  on P1 P2 * * *.*
1dbfe 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61  *.** Begin a tra
1dbff 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 74  nsaction.  The t
1dc00 72 61 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73 20  ransaction ends 
1dc01 77 68 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f 72  when a Commit or
1dc02 20 52 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63   Rollback.** opc
1dc03 6f 64 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ode is encounter
1dc04 65 64 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f  ed.  Depending o
1dc05 6e 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43  n the ON CONFLIC
1dc06 54 20 73 65 74 74 69 6e 67 2c 20 74 68 65 0a 2a  T setting, the.*
1dc07 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69  * transaction mi
1dc08 67 68 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c 6c  ght also be roll
1dc09 65 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 72  ed back if an er
1dc0a 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ror is encounter
1dc0b 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  ed..**.** P1 is 
1dc0c 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
1dc0d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f   database file o
1dc0e 6e 20 77 68 69 63 68 20 74 68 65 20 74 72 61 6e  n which the tran
1dc0f 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73 74  saction is.** st
1dc10 61 72 74 65 64 2e 20 20 49 6e 64 65 78 20 30 20  arted.  Index 0 
1dc11 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  is the main data
1dc12 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e  base file and in
1dc13 64 65 78 20 31 20 69 73 20 74 68 65 0a 2a 2a 20  dex 1 is the.** 
1dc14 66 69 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65  file used for te
1dc15 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20  mporary tables. 
1dc16 20 49 6e 64 69 63 65 73 20 6f 66 20 32 20 6f 72   Indices of 2 or
1dc17 20 6d 6f 72 65 20 61 72 65 20 75 73 65 64 20 66   more are used f
1dc18 6f 72 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64  or.** attached d
1dc19 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20  atabases..**.** 
1dc1a 49 66 20 50 32 20 69 73 20 6e 6f 6e 2d 7a 65 72  If P2 is non-zer
1dc1b 6f 2c 20 74 68 65 6e 20 61 20 77 72 69 74 65 2d  o, then a write-
1dc1c 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73  transaction is s
1dc1d 74 61 72 74 65 64 2e 20 20 41 20 52 45 53 45 52  tarted.  A RESER
1dc1e 56 45 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f  VED lock is.** o
1dc1f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64  btained on the d
1dc20 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 68 65  atabase file whe
1dc21 6e 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61  n a write-transa
1dc22 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64  ction is started
1dc23 2e 20 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 20 70  .  No.** other p
1dc24 72 6f 63 65 73 73 20 63 61 6e 20 73 74 61 72 74  rocess can start
1dc25 20 61 6e 6f 74 68 65 72 20 77 72 69 74 65 20 74   another write t
1dc26 72 61 6e 73 61 63 74 69 6f 6e 20 77 68 69 6c 65  ransaction while
1dc27 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
1dc28 6e 20 69 73 0a 2a 2a 20 75 6e 64 65 72 77 61 79  n is.** underway
1dc29 2e 20 20 53 74 61 72 74 69 6e 67 20 61 20 77 72  .  Starting a wr
1dc2a 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite transaction 
1dc2b 61 6c 73 6f 20 63 72 65 61 74 65 73 20 61 20 72  also creates a r
1dc2c 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
1dc2d 20 41 0a 2a 2a 20 77 72 69 74 65 20 74 72 61 6e   A.** write tran
1dc2e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20  saction must be 
1dc2f 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20 61  started before a
1dc30 6e 79 20 63 68 61 6e 67 65 73 20 63 61 6e 20 62  ny changes can b
1dc31 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a 2a 2a  e made to the.**
1dc32 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 50   database.  If P
1dc33 32 20 69 73 20 32 20 6f 72 20 67 72 65 61 74 65  2 is 2 or greate
1dc34 72 20 74 68 65 6e 20 61 6e 20 45 58 43 4c 55 53  r then an EXCLUS
1dc35 49 56 45 20 6c 6f 63 6b 20 69 73 20 61 6c 73 6f  IVE lock is also
1dc36 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20   obtained.** on 
1dc37 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  the file..**.** 
1dc38 49 66 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73  If a write-trans
1dc39 61 63 74 69 6f 6e 20 69 73 20 73 74 61 72 74 65  action is starte
1dc3a 64 20 61 6e 64 20 74 68 65 20 56 64 62 65 2e 75  d and the Vdbe.u
1dc3b 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 66  sesStmtJournal f
1dc3c 6c 61 67 20 69 73 0a 2a 2a 20 74 72 75 65 20 28  lag is.** true (
1dc3d 74 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 74  this flag is set
1dc3e 20 69 66 20 74 68 65 20 56 64 62 65 20 6d 61 79   if the Vdbe may
1dc3f 20 6d 6f 64 69 66 79 20 6d 6f 72 65 20 74 68 61   modify more tha
1dc40 6e 20 6f 6e 65 20 72 6f 77 20 61 6e 64 20 6d 61  n one row and ma
1dc41 79 0a 2a 2a 20 74 68 72 6f 77 20 61 6e 20 41 42  y.** throw an AB
1dc42 4f 52 54 20 65 78 63 65 70 74 69 6f 6e 29 2c 20  ORT exception), 
1dc43 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  a statement tran
1dc44 73 61 63 74 69 6f 6e 20 6d 61 79 20 61 6c 73 6f  saction may also
1dc45 20 62 65 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20 4d   be opened..** M
1dc46 6f 72 65 20 73 70 65 63 69 66 69 63 61 6c 6c 79  ore specifically
1dc47 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72  , a statement tr
1dc48 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
1dc49 6e 65 64 20 69 66 66 20 74 68 65 20 64 61 74 61  ned iff the data
1dc4a 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69  base.** connecti
1dc4b 6f 6e 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  on is currently 
1dc4c 6e 6f 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69  not in autocommi
1dc4d 74 20 6d 6f 64 65 2c 20 6f 72 20 69 66 20 74 68  t mode, or if th
1dc4e 65 72 65 20 61 72 65 20 6f 74 68 65 72 0a 2a 2a  ere are other.**
1dc4f 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e   active statemen
1dc50 74 73 2e 20 41 20 73 74 61 74 65 6d 65 6e 74 20  ts. A statement 
1dc51 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c 6c 6f  transaction allo
1dc52 77 73 20 74 68 65 20 61 66 66 65 63 74 73 20 6f  ws the affects o
1dc53 66 20 74 68 69 73 0a 2a 2a 20 56 44 42 45 20 74  f this.** VDBE t
1dc54 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  o be rolled back
1dc55 20 61 66 74 65 72 20 61 6e 20 65 72 72 6f 72 20   after an error 
1dc56 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20 74  without having t
1dc57 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 74 68 65 0a  o roll back the.
1dc58 2a 2a 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61  ** entire transa
1dc59 63 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 65 72 72  ction. If no err
1dc5a 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65  or is encountere
1dc5b 64 2c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  d, the statement
1dc5c 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20   transaction.** 
1dc5d 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c  will automatical
1dc5e 6c 79 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74  ly commit when t
1dc5f 68 65 20 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a  he VDBE halts..*
1dc60 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 7a 65  *.** If P2 is ze
1dc61 72 6f 2c 20 74 68 65 6e 20 61 20 72 65 61 64 2d  ro, then a read-
1dc62 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64  lock is obtained
1dc63 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
1dc64 20 66 69 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f   file..*/.case O
1dc65 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 3a 20 7b  P_Transaction: {
1dc66 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1dc67 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1dc68 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 20 20   into u.as */.  
1dc69 42 74 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64  Btree *pBt;.#end
1dc6a 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1dc6b 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1dc6c 20 75 2e 61 73 20 2a 2f 0a 0a 20 20 61 73 73 65   u.as */..  asse
1dc6d 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1dc6e 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44  & pOp->p1<db->nD
1dc6f 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
1dc70 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1dc71 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20  1<<pOp->p1))!=0 
1dc72 29 3b 0a 20 20 75 2e 61 73 2e 70 42 74 20 3d 20  );.  u.as.pBt = 
1dc73 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d  db->aDb[pOp->p1]
1dc74 2e 70 42 74 3b 0a 0a 20 20 69 66 28 20 75 2e 61  .pBt;..  if( u.a
1dc75 73 2e 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20  s.pBt ){.    rc 
1dc76 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  = sqlite3BtreeBe
1dc77 67 69 6e 54 72 61 6e 73 28 75 2e 61 73 2e 70 42  ginTrans(u.as.pB
1dc78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 20  t, pOp->p2);.   
1dc79 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1dc7a 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20 70 2d  BUSY ){.      p-
1dc7b 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20 20  >pc = pc;.      
1dc7c 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51 4c  p->rc = rc = SQL
1dc7d 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20  ITE_BUSY;.      
1dc7e 67 6f 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e  goto vdbe_return
1dc7f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1dc80 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1dc81 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  .      goto abor
1dc82 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1dc83 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70      }..    if( p
1dc84 4f 70 2d 3e 70 32 20 26 26 20 70 2d 3e 75 73 65  Op->p2 && p->use
1dc85 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 0a 20 20 20  sStmtJournal.   
1dc86 20 20 26 26 20 28 64 62 2d 3e 61 75 74 6f 43 6f    && (db->autoCo
1dc87 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61  mmit==0 || db->a
1dc88 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 29 0a  ctiveVdbeCnt>1).
1dc89 20 20 20 20 29 7b 0a 20 20 20 20 20 20 61 73 73      ){.      ass
1dc8a 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1dc8b 65 49 73 49 6e 54 72 61 6e 73 28 75 2e 61 73 2e  eIsInTrans(u.as.
1dc8c 70 42 74 29 20 29 3b 0a 20 20 20 20 20 20 69 66  pBt) );.      if
1dc8d 28 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 3d  ( p->iStatement=
1dc8e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  =0 ){.        as
1dc8f 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65  sert( db->nState
1dc90 6d 65 6e 74 3e 3d 30 20 26 26 20 64 62 2d 3e 6e  ment>=0 && db->n
1dc91 53 61 76 65 70 6f 69 6e 74 3e 3d 30 20 29 3b 0a  Savepoint>=0 );.
1dc92 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 74 61          db->nSta
1dc93 74 65 6d 65 6e 74 2b 2b 3b 0a 20 20 20 20 20 20  tement++;.      
1dc94 20 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20    p->iStatement 
1dc95 3d 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74  = db->nSavepoint
1dc96 20 2b 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65 6e   + db->nStatemen
1dc97 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
1dc98 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1dc99 65 65 42 65 67 69 6e 53 74 6d 74 28 75 2e 61 73  eeBeginStmt(u.as
1dc9a 2e 70 42 74 2c 20 70 2d 3e 69 53 74 61 74 65 6d  .pBt, p->iStatem
1dc9b 65 6e 74 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ent);..      /* 
1dc9c 53 74 6f 72 65 20 74 68 65 20 63 75 72 72 65 6e  Store the curren
1dc9d 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64  t value of the d
1dc9e 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 73 20  atabase handles 
1dc9f 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61  deferred constra
1dca0 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 75  int.      ** cou
1dca1 6e 74 65 72 2e 20 49 66 20 74 68 65 20 73 74 61  nter. If the sta
1dca2 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1dca3 6f 6e 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  on needs to be r
1dca4 6f 6c 6c 65 64 20 62 61 63 6b 2c 0a 20 20 20 20  olled back,.    
1dca5 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f    ** the value o
1dca6 66 20 74 68 69 73 20 63 6f 75 6e 74 65 72 20 6e  f this counter n
1dca7 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 6f  eeds to be resto
1dca8 72 65 64 20 74 6f 6f 2e 20 20 2a 2f 0a 20 20 20  red too.  */.   
1dca9 20 20 20 70 2d 3e 6e 53 74 6d 74 44 65 66 43 6f     p->nStmtDefCo
1dcaa 6e 73 20 3d 20 64 62 2d 3e 6e 44 65 66 65 72 72  ns = db->nDeferr
1dcab 65 64 43 6f 6e 73 3b 0a 20 20 20 20 7d 0a 20 20  edCons;.    }.  
1dcac 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1dcad 20 4f 70 63 6f 64 65 3a 20 52 65 61 64 43 6f 6f   Opcode: ReadCoo
1dcae 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a  kie P1 P2 P3 * *
1dcaf 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63 6f 6f 6b  .**.** Read cook
1dcb0 69 65 20 6e 75 6d 62 65 72 20 50 33 20 66 72 6f  ie number P3 fro
1dcb1 6d 20 64 61 74 61 62 61 73 65 20 50 31 20 61 6e  m database P1 an
1dcb2 64 20 77 72 69 74 65 20 69 74 20 69 6e 74 6f 20  d write it into 
1dcb3 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20  register P2..** 
1dcb4 50 33 3d 3d 31 20 69 73 20 74 68 65 20 73 63 68  P3==1 is the sch
1dcb5 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20 20 50 33  ema version.  P3
1dcb6 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74 61 62  ==2 is the datab
1dcb7 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 50  ase format..** P
1dcb8 33 3d 3d 33 20 69 73 20 74 68 65 20 72 65 63 6f  3==3 is the reco
1dcb9 6d 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 61  mmended pager ca
1dcba 63 68 65 20 73 69 7a 65 2c 20 61 6e 64 20 73 6f  che size, and so
1dcbb 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d 30 20 69   forth.  P1==0 i
1dcbc 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61  s.** the main da
1dcbd 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
1dcbe 50 31 3d 3d 31 20 69 73 20 74 68 65 20 64 61 74  P1==1 is the dat
1dcbf 61 62 61 73 65 20 66 69 6c 65 20 75 73 65 64 20  abase file used 
1dcc0 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70  to store.** temp
1dcc1 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a 2a  orary tables..**
1dcc2 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62  .** There must b
1dcc3 65 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e  e a read-lock on
1dcc4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28 65   the database (e
1dcc5 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63 74  ither a transact
1dcc6 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73  ion.** must be s
1dcc7 74 61 72 74 65 64 20 6f 72 20 74 68 65 72 65 20  tarted or there 
1dcc8 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20  must be an open 
1dcc9 63 75 72 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a  cursor) before.*
1dcca 2a 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73  * executing this
1dccb 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f   instruction..*/
1dccc 0a 63 61 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f  .case OP_ReadCoo
1dccd 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20  kie: {          
1dcce 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1dccf 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30  release */.#if 0
1dcd0 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1dcd1 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1dcd2 75 2e 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4d  u.at */.  int iM
1dcd3 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  eta;.  int iDb;.
1dcd4 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 0a 23    int iCookie;.#
1dcd5 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1dcd6 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1dcd7 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 0a 20 20 75  nto u.at */..  u
1dcd8 2e 61 74 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70  .at.iDb = pOp->p
1dcd9 31 3b 0a 20 20 75 2e 61 74 2e 69 43 6f 6f 6b 69  1;.  u.at.iCooki
1dcda 65 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61  e = pOp->p3;.  a
1dcdb 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 53  ssert( pOp->p3<S
1dcdc 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d 45  QLITE_N_BTREE_ME
1dcdd 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  TA );.  assert( 
1dcde 75 2e 61 74 2e 69 44 62 3e 3d 30 20 26 26 20 75  u.at.iDb>=0 && u
1dcdf 2e 61 74 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20  .at.iDb<db->nDb 
1dce0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
1dce1 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 5d 2e 70  >aDb[u.at.iDb].p
1dce2 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  Bt!=0 );.  asser
1dce3 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1dce4 20 26 20 28 31 3c 3c 75 2e 61 74 2e 69 44 62 29   & (1<<u.at.iDb)
1dce5 29 21 3d 30 20 29 3b 0a 0a 20 20 73 71 6c 69 74  )!=0 );..  sqlit
1dce6 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 64  e3BtreeGetMeta(d
1dce7 62 2d 3e 61 44 62 5b 75 2e 61 74 2e 69 44 62 5d  b->aDb[u.at.iDb]
1dce8 2e 70 42 74 2c 20 75 2e 61 74 2e 69 43 6f 6f 6b  .pBt, u.at.iCook
1dce9 69 65 2c 20 28 75 33 32 20 2a 29 26 75 2e 61 74  ie, (u32 *)&u.at
1dcea 2e 69 4d 65 74 61 29 3b 0a 20 20 70 4f 75 74 2d  .iMeta);.  pOut-
1dceb 3e 75 2e 69 20 3d 20 75 2e 61 74 2e 69 4d 65 74  >u.i = u.at.iMet
1dcec 61 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  a;.  break;.}../
1dced 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 43 6f 6f  * Opcode: SetCoo
1dcee 6b 69 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a  kie P1 P2 P3 * *
1dcef 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  .**.** Write the
1dcf0 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
1dcf1 73 74 65 72 20 50 33 20 28 69 6e 74 65 72 70 72  ster P3 (interpr
1dcf2 65 74 65 64 20 61 73 20 61 6e 20 69 6e 74 65 67  eted as an integ
1dcf3 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63 6f 6f 6b  er).** into cook
1dcf4 69 65 20 6e 75 6d 62 65 72 20 50 32 20 6f 66 20  ie number P2 of 
1dcf5 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 50 32  database P1.  P2
1dcf6 3d 3d 31 20 69 73 20 74 68 65 20 73 63 68 65 6d  ==1 is the schem
1dcf7 61 20 76 65 72 73 69 6f 6e 2e 20 20 0a 2a 2a 20  a version.  .** 
1dcf8 50 32 3d 3d 32 20 69 73 20 74 68 65 20 64 61 74  P2==2 is the dat
1dcf9 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 20 50 32  abase format. P2
1dcfa 3d 3d 33 20 69 73 20 74 68 65 20 72 65 63 6f 6d  ==3 is the recom
1dcfb 6d 65 6e 64 65 64 20 70 61 67 65 72 20 63 61 63  mended pager cac
1dcfc 68 65 20 0a 2a 2a 20 73 69 7a 65 2c 20 61 6e 64  he .** size, and
1dcfd 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d   so forth.  P1==
1dcfe 30 20 69 73 20 74 68 65 20 6d 61 69 6e 20 64 61  0 is the main da
1dcff 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20  tabase file and 
1dd00 50 31 3d 3d 31 20 69 73 20 74 68 65 20 0a 2a 2a  P1==1 is the .**
1dd01 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75   database file u
1dd02 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 65 6d  sed to store tem
1dd03 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 2a  porary tables..*
1dd04 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63 74 69  *.** A transacti
1dd05 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61 72 74  on must be start
1dd06 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75 74  ed before execut
1dd07 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e  ing this opcode.
1dd08 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 74 43  .*/.case OP_SetC
1dd09 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20 20 2f  ookie: {       /
1dd0a 2a 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20  * in3 */.#if 0  
1dd0b 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1dd0c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1dd0d 61 75 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b  au */.  Db *pDb;
1dd0e 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1dd0f 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1dd10 20 69 6e 74 6f 20 75 2e 61 75 20 2a 2f 0a 20 20   into u.au */.  
1dd11 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c  assert( pOp->p2<
1dd12 53 51 4c 49 54 45 5f 4e 5f 42 54 52 45 45 5f 4d  SQLITE_N_BTREE_M
1dd13 45 54 41 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ETA );.  assert(
1dd14 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1dd15 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29  Op->p1<db->nDb )
1dd16 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
1dd17 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c  btreeMask & (1<<
1dd18 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a  pOp->p1))!=0 );.
1dd19 20 20 75 2e 61 75 2e 70 44 62 20 3d 20 26 64 62    u.au.pDb = &db
1dd1a 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  ->aDb[pOp->p1];.
1dd1b 20 20 61 73 73 65 72 74 28 20 75 2e 61 75 2e 70    assert( u.au.p
1dd1c 44 62 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20  Db->pBt!=0 );.  
1dd1d 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn3 = &aMem[pOp
1dd1e 2d 3e 70 33 5d 3b 0a 20 20 73 71 6c 69 74 65 33  ->p3];.  sqlite3
1dd1f 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
1dd20 79 28 70 49 6e 33 29 3b 0a 20 20 2f 2a 20 53 65  y(pIn3);.  /* Se
1dd21 65 20 6e 6f 74 65 20 61 62 6f 75 74 20 69 6e 64  e note about ind
1dd22 65 78 20 73 68 69 66 74 69 6e 67 20 6f 6e 20 4f  ex shifting on O
1dd23 50 5f 52 65 61 64 43 6f 6f 6b 69 65 20 2a 2f 0a  P_ReadCookie */.
1dd24 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1dd25 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 75 2e  reeUpdateMeta(u.
1dd26 61 75 2e 70 44 62 2d 3e 70 42 74 2c 20 70 4f 70  au.pDb->pBt, pOp
1dd27 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e 33 2d  ->p2, (int)pIn3-
1dd28 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 70 4f 70  >u.i);.  if( pOp
1dd29 2d 3e 70 32 3d 3d 42 54 52 45 45 5f 53 43 48 45  ->p2==BTREE_SCHE
1dd2a 4d 41 5f 56 45 52 53 49 4f 4e 20 29 7b 0a 20 20  MA_VERSION ){.  
1dd2b 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 73 63    /* When the sc
1dd2c 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e  hema cookie chan
1dd2d 67 65 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20  ges, record the 
1dd2e 6e 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72  new cookie inter
1dd2f 6e 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 75 2e 61  nally */.    u.a
1dd30 75 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  u.pDb->pSchema->
1dd31 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20  schema_cookie = 
1dd32 28 69 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a  (int)pIn3->u.i;.
1dd33 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
1dd34 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
1dd35 61 6e 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69  anges;.  }else i
1dd36 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45  f( pOp->p2==BTRE
1dd37 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20 29 7b  E_FILE_FORMAT ){
1dd38 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63  .    /* Record c
1dd39 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69  hanges in the fi
1dd3a 6c 65 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20  le format */.   
1dd3b 20 75 2e 61 75 2e 70 44 62 2d 3e 70 53 63 68 65   u.au.pDb->pSche
1dd3c 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20  ma->file_format 
1dd3d 3d 20 28 75 38 29 70 49 6e 33 2d 3e 75 2e 69 3b  = (u8)pIn3->u.i;
1dd3e 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e  .  }.  if( pOp->
1dd3f 70 31 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20  p1==1 ){.    /* 
1dd40 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70  Invalidate all p
1dd41 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
1dd42 74 73 20 77 68 65 6e 65 76 65 72 20 74 68 65 20  ts whenever the 
1dd43 54 45 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20  TEMP database.  
1dd44 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 63    ** schema is c
1dd45 68 61 6e 67 65 64 2e 20 20 54 69 63 6b 65 74 20  hanged.  Ticket 
1dd46 23 31 36 34 34 20 2a 2f 0a 20 20 20 20 73 71 6c  #1644 */.    sql
1dd47 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
1dd48 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29  edStatements(db)
1dd49 3b 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64  ;.    p->expired
1dd4a 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61   = 0;.  }.  brea
1dd4b 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1dd4c 20 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31   VerifyCookie P1
1dd4d 20 50 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63   P2 *.**.** Chec
1dd4e 6b 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 67  k the value of g
1dd4f 6c 6f 62 61 6c 20 64 61 74 61 62 61 73 65 20 70  lobal database p
1dd50 61 72 61 6d 65 74 65 72 20 6e 75 6d 62 65 72 20  arameter number 
1dd51 30 20 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61  0 (the.** schema
1dd52 20 76 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61   version) and ma
1dd53 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 65 71  ke sure it is eq
1dd54 75 61 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20  ual to P2.  .** 
1dd55 50 31 20 69 73 20 74 68 65 20 64 61 74 61 62 61  P1 is the databa
1dd56 73 65 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20  se number which 
1dd57 69 73 20 30 20 66 6f 72 20 74 68 65 20 6d 61 69  is 0 for the mai
1dd58 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a  n database file.
1dd59 2a 2a 20 61 6e 64 20 31 20 66 6f 72 20 74 68 65  ** and 1 for the
1dd5a 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65   file holding te
1dd5b 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61  mporary tables a
1dd5c 6e 64 20 73 6f 6d 65 20 68 69 67 68 65 72 20 6e  nd some higher n
1dd5d 75 6d 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78  umber.** for aux
1dd5e 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 73  iliary databases
1dd5f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b  ..**.** The cook
1dd60 69 65 20 63 68 61 6e 67 65 73 20 69 74 73 20 76  ie changes its v
1dd61 61 6c 75 65 20 77 68 65 6e 65 76 65 72 20 74 68  alue whenever th
1dd62 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
1dd63 61 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68  a changes..** Th
1dd64 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20  is operation is 
1dd65 75 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 77  used to detect w
1dd66 68 65 6e 20 74 68 61 74 20 74 68 65 20 63 6f 6f  hen that the coo
1dd67 6b 69 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a  kie has changed.
1dd68 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 65 20  ** and that the 
1dd69 63 75 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20  current process 
1dd6a 6e 65 65 64 73 20 74 6f 20 72 65 72 65 61 64 20  needs to reread 
1dd6b 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a  the schema..**.*
1dd6c 2a 20 45 69 74 68 65 72 20 61 20 74 72 61 6e 73  * Either a trans
1dd6d 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20  action needs to 
1dd6e 68 61 76 65 20 62 65 65 6e 20 73 74 61 72 74 65  have been starte
1dd6f 64 20 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20  d or an OP_Open 
1dd70 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65  needs.** to be e
1dd71 78 65 63 75 74 65 64 20 28 74 6f 20 65 73 74 61  xecuted (to esta
1dd72 62 6c 69 73 68 20 61 20 72 65 61 64 20 6c 6f 63  blish a read loc
1dd73 6b 29 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f  k) before this o
1dd74 70 63 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f  pcode is.** invo
1dd75 6b 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ked..*/.case OP_
1dd76 56 65 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a  VerifyCookie: {.
1dd77 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1dd78 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1dd79 69 6e 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20 69  into u.av */.  i
1dd7a 6e 74 20 69 4d 65 74 61 3b 0a 20 20 42 74 72 65  nt iMeta;.  Btre
1dd7b 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f  e *pBt;.#endif /
1dd7c 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1dd7d 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1dd7e 76 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  v */.  assert( p
1dd7f 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1dd80 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  ->p1<db->nDb );.
1dd81 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1dd82 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f  reeMask & (1<<pO
1dd83 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20  p->p1))!=0 );.  
1dd84 75 2e 61 76 2e 70 42 74 20 3d 20 64 62 2d 3e 61  u.av.pBt = db->a
1dd85 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b  Db[pOp->p1].pBt;
1dd86 0a 20 20 69 66 28 20 75 2e 61 76 2e 70 42 74 20  .  if( u.av.pBt 
1dd87 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
1dd88 72 65 65 47 65 74 4d 65 74 61 28 75 2e 61 76 2e  reeGetMeta(u.av.
1dd89 70 42 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d  pBt, BTREE_SCHEM
1dd8a 41 5f 56 45 52 53 49 4f 4e 2c 20 28 75 33 32 20  A_VERSION, (u32 
1dd8b 2a 29 26 75 2e 61 76 2e 69 4d 65 74 61 29 3b 0a  *)&u.av.iMeta);.
1dd8c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61    }else{.    u.a
1dd8d 76 2e 69 4d 65 74 61 20 3d 20 30 3b 0a 20 20 7d  v.iMeta = 0;.  }
1dd8e 0a 20 20 69 66 28 20 75 2e 61 76 2e 69 4d 65 74  .  if( u.av.iMet
1dd8f 61 21 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20  a!=pOp->p2 ){.  
1dd90 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1dd91 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  db, p->zErrMsg);
1dd92 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  .    p->zErrMsg 
1dd93 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
1dd94 70 28 64 62 2c 20 22 64 61 74 61 62 61 73 65 20  p(db, "database 
1dd95 73 63 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67  schema has chang
1dd96 65 64 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20  ed");.    /* If 
1dd97 74 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69  the schema-cooki
1dd98 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  e from the datab
1dd99 61 73 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73  ase file matches
1dd9a 20 74 68 65 20 63 6f 6f 6b 69 65 0a 20 20 20 20   the cookie.    
1dd9b 2a 2a 20 73 74 6f 72 65 64 20 77 69 74 68 20 74  ** stored with t
1dd9c 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70  he in-memory rep
1dd9d 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
1dd9e 68 65 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20  he schema, do.  
1dd9f 20 20 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20    ** not reload 
1dda0 74 68 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20  the schema from 
1dda1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1dda2 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e..    **.    **
1dda3 20 49 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c   If virtual-tabl
1dda4 65 73 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74  es are in use, t
1dda5 68 69 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20  his is not just 
1dda6 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e  an optimization.
1dda7 0a 20 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76  .    ** Often, v
1dda8 2d 74 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68  -tables store th
1dda9 65 69 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65  eir data in othe
1ddaa 72 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c  r SQLite tables,
1ddab 20 77 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72   which.    ** ar
1ddac 65 20 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77  e queried from w
1ddad 69 74 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e  ithin xNext() an
1ddae 64 20 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20  d other v-table 
1ddaf 6d 65 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20  methods using.  
1ddb0 20 20 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75    ** prepared qu
1ddb1 65 72 69 65 73 2e 20 49 66 20 73 75 63 68 20 61  eries. If such a
1ddb2 20 71 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66   query is out-of
1ddb3 2d 64 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74  -date, we do not
1ddb4 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20   want to.    ** 
1ddb5 64 69 73 63 61 72 64 20 74 68 65 20 64 61 74 61  discard the data
1ddb6 62 61 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20  base schema, as 
1ddb7 74 68 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d  the user code im
1ddb8 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20  plementing the. 
1ddb9 20 20 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f     ** v-table wo
1ddba 75 6c 64 20 68 61 76 65 20 74 6f 20 62 65 20 72  uld have to be r
1ddbb 65 61 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c  eady for the sql
1ddbc 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74  ite3_vtab struct
1ddbd 75 72 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a  ure itself.    *
1ddbe 2a 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61  * to be invalida
1ddbf 74 65 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c  ted whenever sql
1ddc0 69 74 65 33 5f 73 74 65 70 28 29 20 69 73 20 63  ite3_step() is c
1ddc1 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69  alled from withi
1ddc2 6e 0a 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62  n.    ** a v-tab
1ddc3 6c 65 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a  le method..    *
1ddc4 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44  /.    if( db->aD
1ddc5 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65  b[pOp->p1].pSche
1ddc6 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
1ddc7 65 21 3d 75 2e 61 76 2e 69 4d 65 74 61 20 29 7b  e!=u.av.iMeta ){
1ddc8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
1ddc9 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
1ddca 61 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a  a(db, pOp->p1);.
1ddcb 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
1ddcc 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
1ddcd 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
1ddce 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1ddcf 53 43 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 62 72  SCHEMA;.  }.  br
1ddd0 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1ddd1 65 3a 20 4f 70 65 6e 52 65 61 64 20 50 31 20 50  e: OpenRead P1 P
1ddd2 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a  2 P3 P4 P5.**.**
1ddd3 20 4f 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e 6c   Open a read-onl
1ddd4 79 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65  y cursor for the
1ddd5 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
1ddd6 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20  whose root page 
1ddd7 69 73 0a 2a 2a 20 50 32 20 69 6e 20 61 20 64 61  is.** P2 in a da
1ddd8 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68  tabase file.  Th
1ddd9 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
1ddda 69 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79  is determined by
1dddb 20 50 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30 20 6d   P3. .** P3==0 m
1dddc 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 64 61  eans the main da
1dddd 74 61 62 61 73 65 2c 20 50 33 3d 3d 31 20 6d 65  tabase, P3==1 me
1ddde 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ans the database
1dddf 20 75 73 65 64 20 66 6f 72 20 0a 2a 2a 20 74 65   used for .** te
1dde0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2c 20  mporary tables, 
1dde1 61 6e 64 20 50 33 3e 31 20 6d 65 61 6e 73 20 75  and P3>1 means u
1dde2 73 65 64 20 74 68 65 20 63 6f 72 72 65 73 70 6f  sed the correspo
1dde3 6e 64 69 6e 67 20 61 74 74 61 63 68 65 64 0a 2a  nding attached.*
1dde4 2a 20 64 61 74 61 62 61 73 65 2e 20 20 47 69 76  * database.  Giv
1dde5 65 20 74 68 65 20 6e 65 77 20 63 75 72 73 6f 72  e the new cursor
1dde6 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f   an identifier o
1dde7 66 20 50 31 2e 20 20 54 68 65 20 50 31 0a 2a 2a  f P1.  The P1.**
1dde8 20 76 61 6c 75 65 73 20 6e 65 65 64 20 6e 6f 74   values need not
1dde9 20 62 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 62   be contiguous b
1ddea 75 74 20 61 6c 6c 20 50 31 20 76 61 6c 75 65 73  ut all P1 values
1ddeb 20 73 68 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c   should be small
1ddec 20 69 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 49 74   integers..** It
1dded 20 69 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72   is an error for
1ddee 20 50 31 20 74 6f 20 62 65 20 6e 65 67 61 74 69   P1 to be negati
1ddef 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 21  ve..**.** If P5!
1ddf0 3d 30 20 74 68 65 6e 20 75 73 65 20 74 68 65 20  =0 then use the 
1ddf1 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73  content of regis
1ddf2 74 65 72 20 50 32 20 61 73 20 74 68 65 20 72 6f  ter P2 as the ro
1ddf3 6f 74 20 70 61 67 65 2c 20 6e 6f 74 0a 2a 2a 20  ot page, not.** 
1ddf4 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50 32 20  the value of P2 
1ddf5 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 54 68  itself..**.** Th
1ddf6 65 72 65 20 77 69 6c 6c 20 62 65 20 61 20 72 65  ere will be a re
1ddf7 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  ad lock on the d
1ddf8 61 74 61 62 61 73 65 20 77 68 65 6e 65 76 65 72  atabase whenever
1ddf9 20 74 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a 20   there is an.** 
1ddfa 6f 70 65 6e 20 63 75 72 73 6f 72 2e 20 20 49 66  open cursor.  If
1ddfb 20 74 68 65 20 64 61 74 61 62 61 73 65 20 77 61   the database wa
1ddfc 73 20 75 6e 6c 6f 63 6b 65 64 20 70 72 69 6f 72  s unlocked prior
1ddfd 20 74 6f 20 74 68 69 73 20 69 6e 73 74 72 75 63   to this instruc
1ddfe 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 20 72  tion.** then a r
1ddff 65 61 64 20 6c 6f 63 6b 20 69 73 20 61 63 71 75  ead lock is acqu
1de00 69 72 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  ired as part of 
1de01 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
1de02 2e 20 20 41 20 72 65 61 64 0a 2a 2a 20 6c 6f 63  .  A read.** loc
1de03 6b 20 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70  k allows other p
1de04 72 6f 63 65 73 73 65 73 20 74 6f 20 72 65 61 64  rocesses to read
1de05 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 75   the database bu
1de06 74 20 70 72 6f 68 69 62 69 74 73 0a 2a 2a 20 61  t prohibits.** a
1de07 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ny other process
1de08 20 66 72 6f 6d 20 6d 6f 64 69 66 79 69 6e 67 20   from modifying 
1de09 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
1de0a 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 0a  he read lock is.
1de0b 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e  ** released when
1de0c 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 65   all cursors are
1de0d 20 63 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69   closed.  If thi
1de0e 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74  s instruction at
1de0f 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 65 74  tempts.** to get
1de10 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 62 75 74   a read lock but
1de11 20 66 61 69 6c 73 2c 20 74 68 65 20 73 63 72 69   fails, the scri
1de12 70 74 20 74 65 72 6d 69 6e 61 74 65 73 20 77 69  pt terminates wi
1de13 74 68 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f  th an.** SQLITE_
1de14 42 55 53 59 20 65 72 72 6f 72 20 63 6f 64 65 2e  BUSY error code.
1de15 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 61  .**.** The P4 va
1de16 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 68 65  lue may be eithe
1de17 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34  r an integer (P4
1de18 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69  _INT32) or a poi
1de19 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 79  nter to.** a Key
1de1a 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 28  Info structure (
1de1b 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 20  P4_KEYINFO). If 
1de1c 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  it is a pointer 
1de1d 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a  to a KeyInfo .**
1de1e 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e   structure, then
1de1f 20 73 61 69 64 20 73 74 72 75 63 74 75 72 65 20   said structure 
1de20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f 6e 74  defines the cont
1de21 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e  ent and collatin
1de22 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6f  g .** sequence o
1de23 66 20 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e  f the index bein
1de24 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 77  g opened. Otherw
1de25 69 73 65 2c 20 69 66 20 50 34 20 69 73 20 61 6e  ise, if P4 is an
1de26 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 6c   integer .** val
1de27 75 65 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f  ue, it is set to
1de28 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
1de29 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61  olumns in the ta
1de2a 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ble..**.** See a
1de2b 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a 2a  lso OpenWrite..*
1de2c 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65  /./* Opcode: Ope
1de2d 6e 57 72 69 74 65 20 50 31 20 50 32 20 50 33 20  nWrite P1 P2 P3 
1de2e 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e  P4 P5.**.** Open
1de2f 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 75   a read/write cu
1de30 72 73 6f 72 20 6e 61 6d 65 64 20 50 31 20 6f 6e  rsor named P1 on
1de31 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e   the table or in
1de32 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 0a 2a  dex whose root.*
1de33 2a 20 70 61 67 65 20 69 73 20 50 32 2e 20 20 4f  * page is P2.  O
1de34 72 20 69 66 20 50 35 21 3d 30 20 75 73 65 20 74  r if P5!=0 use t
1de35 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
1de36 67 69 73 74 65 72 20 50 32 20 74 6f 20 66 69 6e  gister P2 to fin
1de37 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61  d the.** root pa
1de38 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34  ge..**.** The P4
1de39 20 76 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69   value may be ei
1de3a 74 68 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20  ther an integer 
1de3b 28 50 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20  (P4_INT32) or a 
1de3c 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20  pointer to.** a 
1de3d 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
1de3e 65 20 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20  e (P4_KEYINFO). 
1de3f 49 66 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74  If it is a point
1de40 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20  er to a KeyInfo 
1de41 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74  .** structure, t
1de42 68 65 6e 20 73 61 69 64 20 73 74 72 75 63 74 75  hen said structu
1de43 72 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 63  re defines the c
1de44 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61  ontent and colla
1de45 74 69 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63  ting .** sequenc
1de46 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62  e of the index b
1de47 65 69 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68  eing opened. Oth
1de48 65 72 77 69 73 65 2c 20 69 66 20 50 34 20 69 73  erwise, if P4 is
1de49 20 61 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20   an integer .** 
1de4a 76 61 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74  value, it is set
1de4b 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
1de4c 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
1de4d 20 74 61 62 6c 65 2c 20 6f 72 20 74 6f 20 74 68   table, or to th
1de4e 65 0a 2a 2a 20 6c 61 72 67 65 73 74 20 69 6e 64  e.** largest ind
1de4f 65 78 20 6f 66 20 61 6e 79 20 63 6f 6c 75 6d 6e  ex of any column
1de50 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68   of the table th
1de51 61 74 20 69 73 20 61 63 74 75 61 6c 6c 79 20 75  at is actually u
1de52 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  sed..**.** This 
1de53 69 6e 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b  instruction work
1de54 73 20 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e  s just like Open
1de55 52 65 61 64 20 65 78 63 65 70 74 20 74 68 61 74  Read except that
1de56 20 69 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75   it opens the cu
1de57 72 73 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f  rsor.** in read/
1de58 77 72 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72  write mode.  For
1de59 20 61 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20   a given table, 
1de5a 74 68 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65  there can be one
1de5b 20 6f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e   or more read-on
1de5c 6c 79 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72  ly.** cursors or
1de5d 20 61 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77   a single read/w
1de5e 72 69 74 65 20 63 75 72 73 6f 72 20 62 75 74 20  rite cursor but 
1de5f 6e 6f 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20  not both..**.** 
1de60 53 65 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61  See also OpenRea
1de61 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70  d..*/.case OP_Op
1de62 65 6e 52 65 61 64 3a 0a 63 61 73 65 20 4f 50 5f  enRead:.case OP_
1de63 4f 70 65 6e 57 72 69 74 65 3a 20 7b 0a 23 69 66  OpenWrite: {.#if
1de64 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1de65 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1de66 6f 20 75 2e 61 77 20 2a 2f 0a 20 20 69 6e 74 20  o u.aw */.  int 
1de67 6e 46 69 65 6c 64 3b 0a 20 20 4b 65 79 49 6e 66  nField;.  KeyInf
1de68 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69  o *pKeyInfo;.  i
1de69 6e 74 20 70 32 3b 0a 20 20 69 6e 74 20 69 44 62  nt p2;.  int iDb
1de6a 3b 0a 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a  ;.  int wrFlag;.
1de6b 20 20 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56    Btree *pX;.  V
1de6c 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b  dbeCursor *pCur;
1de6d 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64  .  Db *pDb;.#end
1de6e 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1de6f 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1de70 20 75 2e 61 77 20 2a 2f 0a 0a 20 20 69 66 28 20   u.aw */..  if( 
1de71 70 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 20  p->expired ){.  
1de72 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42    rc = SQLITE_AB
1de73 4f 52 54 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a  ORT;.    break;.
1de74 20 20 7d 0a 0a 20 20 75 2e 61 77 2e 6e 46 69 65    }..  u.aw.nFie
1de75 6c 64 20 3d 20 30 3b 0a 20 20 75 2e 61 77 2e 70  ld = 0;.  u.aw.p
1de76 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 0a 20 20 75  KeyInfo = 0;.  u
1de77 2e 61 77 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32  .aw.p2 = pOp->p2
1de78 3b 0a 20 20 75 2e 61 77 2e 69 44 62 20 3d 20 70  ;.  u.aw.iDb = p
1de79 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74  Op->p3;.  assert
1de7a 28 20 75 2e 61 77 2e 69 44 62 3e 3d 30 20 26 26  ( u.aw.iDb>=0 &&
1de7b 20 75 2e 61 77 2e 69 44 62 3c 64 62 2d 3e 6e 44   u.aw.iDb<db->nD
1de7c 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  b );.  assert( (
1de7d 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28  p->btreeMask & (
1de7e 31 3c 3c 75 2e 61 77 2e 69 44 62 29 29 21 3d 30  1<<u.aw.iDb))!=0
1de7f 20 29 3b 0a 20 20 75 2e 61 77 2e 70 44 62 20 3d   );.  u.aw.pDb =
1de80 20 26 64 62 2d 3e 61 44 62 5b 75 2e 61 77 2e 69   &db->aDb[u.aw.i
1de81 44 62 5d 3b 0a 20 20 75 2e 61 77 2e 70 58 20 3d  Db];.  u.aw.pX =
1de82 20 75 2e 61 77 2e 70 44 62 2d 3e 70 42 74 3b 0a   u.aw.pDb->pBt;.
1de83 20 20 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70    assert( u.aw.p
1de84 58 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f  X!=0 );.  if( pO
1de85 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70  p->opcode==OP_Op
1de86 65 6e 57 72 69 74 65 20 29 7b 0a 20 20 20 20 75  enWrite ){.    u
1de87 2e 61 77 2e 77 72 46 6c 61 67 20 3d 20 31 3b 0a  .aw.wrFlag = 1;.
1de88 20 20 20 20 69 66 28 20 75 2e 61 77 2e 70 44 62      if( u.aw.pDb
1de89 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
1de8a 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d 69 6e 57  format < p->minW
1de8b 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 29  riteFileFormat )
1de8c 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69 6e 57 72  {.      p->minWr
1de8d 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20  iteFileFormat = 
1de8e 75 2e 61 77 2e 70 44 62 2d 3e 70 53 63 68 65 6d  u.aw.pDb->pSchem
1de8f 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a  a->file_format;.
1de90 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
1de91 20 20 20 75 2e 61 77 2e 77 72 46 6c 61 67 20 3d     u.aw.wrFlag =
1de92 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f   0;.  }.  if( pO
1de93 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 61 73 73  p->p5 ){.    ass
1de94 65 72 74 28 20 75 2e 61 77 2e 70 32 3e 30 20 29  ert( u.aw.p2>0 )
1de95 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  ;.    assert( u.
1de96 61 77 2e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  aw.p2<=p->nMem )
1de97 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20 26 61 4d  ;.    pIn2 = &aM
1de98 65 6d 5b 75 2e 61 77 2e 70 32 5d 3b 0a 20 20 20  em[u.aw.p2];.   
1de99 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
1de9a 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b  ntegerify(pIn2);
1de9b 0a 20 20 20 20 75 2e 61 77 2e 70 32 20 3d 20 28  .    u.aw.p2 = (
1de9c 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20  int)pIn2->u.i;. 
1de9d 20 20 20 2f 2a 20 54 68 65 20 75 2e 61 77 2e 70     /* The u.aw.p
1de9e 32 20 76 61 6c 75 65 20 61 6c 77 61 79 73 20 63  2 value always c
1de9f 6f 6d 65 73 20 66 72 6f 6d 20 61 20 70 72 69 6f  omes from a prio
1dea0 72 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65  r OP_CreateTable
1dea1 20 6f 70 63 6f 64 65 20 61 6e 64 0a 20 20 20 20   opcode and.    
1dea2 2a 2a 20 74 68 61 74 20 6f 70 63 6f 64 65 20 77  ** that opcode w
1dea3 69 6c 6c 20 61 6c 77 61 79 73 20 73 65 74 20 74  ill always set t
1dea4 68 65 20 75 2e 61 77 2e 70 32 20 76 61 6c 75 65  he u.aw.p2 value
1dea5 20 74 6f 20 32 20 6f 72 20 6d 6f 72 65 20 6f 72   to 2 or more or
1dea6 20 65 6c 73 65 20 66 61 69 6c 2e 0a 20 20 20 20   else fail..    
1dea7 2a 2a 20 49 66 20 74 68 65 72 65 20 77 65 72 65  ** If there were
1dea8 20 61 20 66 61 69 6c 75 72 65 2c 20 74 68 65 20   a failure, the 
1dea9 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1deaa 6e 74 20 77 6f 75 6c 64 20 68 61 76 65 20 68 61  nt would have ha
1deab 6c 74 65 64 0a 20 20 20 20 2a 2a 20 62 65 66 6f  lted.    ** befo
1deac 72 65 20 72 65 61 63 68 69 6e 67 20 74 68 69 73  re reaching this
1dead 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 2a 2f   instruction. */
1deae 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 75  .    if( NEVER(u
1deaf 2e 61 77 2e 70 32 3c 32 29 20 29 20 7b 0a 20 20  .aw.p2<2) ) {.  
1deb0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1deb1 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1deb2 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
1deb3 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1deb4 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70   }.  }.  if( pOp
1deb5 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59  ->p4type==P4_KEY
1deb6 49 4e 46 4f 20 29 7b 0a 20 20 20 20 75 2e 61 77  INFO ){.    u.aw
1deb7 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d  .pKeyInfo = pOp-
1deb8 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  >p4.pKeyInfo;.  
1deb9 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 2d    u.aw.pKeyInfo-
1deba 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62  >enc = ENC(p->db
1debb 29 3b 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69 65  );.    u.aw.nFie
1debc 6c 64 20 3d 20 75 2e 61 77 2e 70 4b 65 79 49 6e  ld = u.aw.pKeyIn
1debd 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b 0a 20 20  fo->nField+1;.  
1debe 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70  }else if( pOp->p
1debf 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20  4type==P4_INT32 
1dec0 29 7b 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69 65  ){.    u.aw.nFie
1dec1 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a  ld = pOp->p4.i;.
1dec2 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4f    }.  assert( pO
1dec3 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e  p->p1>=0 );.  u.
1dec4 61 77 2e 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61  aw.pCur = alloca
1dec5 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d  teCursor(p, pOp-
1dec6 3e 70 31 2c 20 75 2e 61 77 2e 6e 46 69 65 6c 64  >p1, u.aw.nField
1dec7 2c 20 75 2e 61 77 2e 69 44 62 2c 20 31 29 3b 0a  , u.aw.iDb, 1);.
1dec8 20 20 69 66 28 20 75 2e 61 77 2e 70 43 75 72 3d    if( u.aw.pCur=
1dec9 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  =0 ) goto no_mem
1deca 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 6e  ;.  u.aw.pCur->n
1decb 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72 63  ullRow = 1;.  rc
1decc 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
1decd 75 72 73 6f 72 28 75 2e 61 77 2e 70 58 2c 20 75  ursor(u.aw.pX, u
1dece 2e 61 77 2e 70 32 2c 20 75 2e 61 77 2e 77 72 46  .aw.p2, u.aw.wrF
1decf 6c 61 67 2c 20 75 2e 61 77 2e 70 4b 65 79 49 6e  lag, u.aw.pKeyIn
1ded0 66 6f 2c 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70  fo, u.aw.pCur->p
1ded1 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e 61 77 2e  Cursor);.  u.aw.
1ded2 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d  pCur->pKeyInfo =
1ded3 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 3b 0a   u.aw.pKeyInfo;.
1ded4 0a 20 20 2f 2a 20 53 69 6e 63 65 20 69 74 20 70  .  /* Since it p
1ded5 65 72 66 6f 72 6d 73 20 6e 6f 20 6d 65 6d 6f 72  erforms no memor
1ded6 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20  y allocation or 
1ded7 49 4f 2c 20 74 68 65 20 6f 6e 6c 79 20 76 61 6c  IO, the only val
1ded8 75 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 73 71  ues that.  ** sq
1ded9 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
1deda 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 61 72  () may return ar
1dedb 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 61  e SQLITE_EMPTY a
1dedc 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20 20  nd SQLITE_OK..  
1dedd 2a 2a 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20  ** SQLITE_EMPTY 
1dede 69 73 20 6f 6e 6c 79 20 72 65 74 75 72 6e 65 64  is only returned
1dedf 20 77 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67   when attempting
1dee0 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 74 61 62   to open the tab
1dee1 6c 65 0a 20 20 2a 2a 20 72 6f 6f 74 65 64 20 61  le.  ** rooted a
1dee2 74 20 70 61 67 65 20 31 20 6f 66 20 61 20 7a 65  t page 1 of a ze
1dee3 72 6f 2d 62 79 74 65 20 64 61 74 61 62 61 73 65  ro-byte database
1dee4 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
1dee5 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59  rc==SQLITE_EMPTY
1dee6 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   || rc==SQLITE_O
1dee7 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  K );.  if( rc==S
1dee8 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a 20  QLITE_EMPTY ){. 
1dee9 20 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70 43     u.aw.pCur->pC
1deea 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20 72  ursor = 0;.    r
1deeb 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1deec 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
1deed 20 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 61   VdbeCursor.isTa
1deee 62 6c 65 20 61 6e 64 20 69 73 49 6e 64 65 78 20  ble and isIndex 
1deef 76 61 72 69 61 62 6c 65 73 2e 20 50 72 65 76 69  variables. Previ
1def0 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66 0a  ous versions of.
1def1 20 20 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 64    ** SQLite used
1def2 20 74 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65   to check if the
1def3 20 72 6f 6f 74 2d 70 61 67 65 20 66 6c 61 67 73   root-page flags
1def4 20 77 65 72 65 20 73 61 6e 65 20 61 74 20 74 68   were sane at th
1def5 69 73 20 70 6f 69 6e 74 0a 20 20 2a 2a 20 61 6e  is point.  ** an
1def6 64 20 72 65 70 6f 72 74 20 64 61 74 61 62 61 73  d report databas
1def7 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 20  e corruption if 
1def8 74 68 65 79 20 77 65 72 65 20 6e 6f 74 2c 20 62  they were not, b
1def9 75 74 20 74 68 69 73 20 63 68 65 63 6b 20 68 61  ut this check ha
1defa 73 0a 20 20 2a 2a 20 73 69 6e 63 65 20 6d 6f 76  s.  ** since mov
1defb 65 64 20 69 6e 74 6f 20 74 68 65 20 62 74 72 65  ed into the btre
1defc 65 20 6c 61 79 65 72 2e 20 20 2a 2f 0a 20 20 75  e layer.  */.  u
1defd 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54 61 62 6c  .aw.pCur->isTabl
1defe 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 70 65 21  e = pOp->p4type!
1deff 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 75  =P4_KEYINFO;.  u
1df00 2e 61 77 2e 70 43 75 72 2d 3e 69 73 49 6e 64 65  .aw.pCur->isInde
1df01 78 20 3d 20 21 75 2e 61 77 2e 70 43 75 72 2d 3e  x = !u.aw.pCur->
1df02 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b  isTable;.  break
1df03 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1df04 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 50 31  OpenEphemeral P1
1df05 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a   P2 * P4 *.**.**
1df06 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72 73   Open a new curs
1df07 6f 72 20 50 31 20 74 6f 20 61 20 74 72 61 6e 73  or P1 to a trans
1df08 69 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 20 54  ient table..** T
1df09 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c 77  he cursor is alw
1df0a 61 79 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2f  ays opened read/
1df0b 77 72 69 74 65 20 65 76 65 6e 20 69 66 20 0a 2a  write even if .*
1df0c 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  * the main datab
1df0d 61 73 65 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79  ase is read-only
1df0e 2e 20 20 54 68 65 20 74 72 61 6e 73 69 65 6e 74  .  The transient
1df0f 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74   or virtual.** t
1df10 61 62 6c 65 20 69 73 20 64 65 6c 65 74 65 64 20  able is deleted 
1df11 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68  automatically wh
1df12 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  en the cursor is
1df13 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 50   closed..**.** P
1df14 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  2 is the number 
1df15 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
1df16 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
1df17 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 70  .** The cursor p
1df18 6f 69 6e 74 73 20 74 6f 20 61 20 42 54 72 65 65  oints to a BTree
1df19 20 74 61 62 6c 65 20 69 66 20 50 34 3d 3d 30 20   table if P4==0 
1df1a 61 6e 64 20 74 6f 20 61 20 42 54 72 65 65 20 69  and to a BTree i
1df1b 6e 64 65 78 0a 2a 2a 20 69 66 20 50 34 20 69 73  ndex.** if P4 is
1df1c 20 6e 6f 74 20 30 2e 20 20 49 66 20 50 34 20 69   not 0.  If P4 i
1df1d 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20 70  s not NULL, it p
1df1e 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 49 6e  oints to a KeyIn
1df1f 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  fo structure.** 
1df20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68 65  that defines the
1df21 20 66 6f 72 6d 61 74 20 6f 66 20 6b 65 79 73 20   format of keys 
1df22 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a  in the index..**
1df23 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
1df24 77 61 73 20 6f 6e 63 65 20 63 61 6c 6c 65 64 20  was once called 
1df25 4f 70 65 6e 54 65 6d 70 2e 20 20 42 75 74 20 74  OpenTemp.  But t
1df26 68 61 74 20 63 72 65 61 74 65 64 0a 2a 2a 20 63  hat created.** c
1df27 6f 6e 66 75 73 69 6f 6e 20 62 65 63 61 75 73 65  onfusion because
1df28 20 74 68 65 20 74 65 72 6d 20 22 74 65 6d 70 20   the term "temp 
1df29 74 61 62 6c 65 22 2c 20 6d 69 67 68 74 20 72 65  table", might re
1df2a 66 65 72 20 65 69 74 68 65 72 0a 2a 2a 20 74 6f  fer either.** to
1df2b 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 61 74   a TEMP table at
1df2c 20 74 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20   the SQL level, 
1df2d 6f 72 20 74 6f 20 61 20 74 61 62 6c 65 20 6f 70  or to a table op
1df2e 65 6e 65 64 20 62 79 0a 2a 2a 20 74 68 69 73 20  ened by.** this 
1df2f 6f 70 63 6f 64 65 2e 20 20 54 68 65 6e 20 74 68  opcode.  Then th
1df30 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 63 61  is opcode was ca
1df31 6c 6c 20 4f 70 65 6e 56 69 72 74 75 61 6c 2e 20  ll OpenVirtual. 
1df32 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 63 72 65   But.** that cre
1df33 61 74 65 64 20 63 6f 6e 66 75 73 69 6f 6e 20 77  ated confusion w
1df34 69 74 68 20 74 68 65 20 77 68 6f 6c 65 20 76 69  ith the whole vi
1df35 72 74 75 61 6c 2d 74 61 62 6c 65 20 69 64 65 61  rtual-table idea
1df36 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65  ..*/.case OP_Ope
1df37 6e 45 70 68 65 6d 65 72 61 6c 3a 20 7b 0a 23 69  nEphemeral: {.#i
1df38 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1df39 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1df3a 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 56 64 62  to u.ax */.  Vdb
1df3b 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23 65  eCursor *pCx;.#e
1df3c 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1df3d 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1df3e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 73 74 61  to u.ax */.  sta
1df3f 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70  tic const int op
1df40 65 6e 46 6c 61 67 73 20 3d 0a 20 20 20 20 20 20  enFlags =.      
1df41 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
1df42 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 53 51  WRITE |.      SQ
1df43 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
1df44 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f   |.      SQLITE_
1df45 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c  OPEN_EXCLUSIVE |
1df46 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50  .      SQLITE_OP
1df47 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45  EN_DELETEONCLOSE
1df48 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f   |.      SQLITE_
1df49 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44  OPEN_TRANSIENT_D
1df4a 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f  B;..  assert( pO
1df4b 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e  p->p1>=0 );.  u.
1df4c 61 78 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74  ax.pCx = allocat
1df4d 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e  eCursor(p, pOp->
1df4e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31 2c  p1, pOp->p2, -1,
1df4f 20 31 29 3b 0a 20 20 69 66 28 20 75 2e 61 78 2e   1);.  if( u.ax.
1df50 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f  pCx==0 ) goto no
1df51 5f 6d 65 6d 3b 0a 20 20 75 2e 61 78 2e 70 43 78  _mem;.  u.ax.pCx
1df52 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20  ->nullRow = 1;. 
1df53 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1df54 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 30 2c  eeFactory(db, 0,
1df55 20 31 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55   1, SQLITE_DEFAU
1df56 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53 49  LT_TEMP_CACHE_SI
1df57 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 0a 20  ZE, openFlags,. 
1df58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1df59 20 20 20 20 20 20 20 20 20 20 26 75 2e 61 78 2e            &u.ax.
1df5a 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28  pCx->pBt);.  if(
1df5b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1df5c 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1df5d 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e  e3BtreeBeginTran
1df5e 73 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c  s(u.ax.pCx->pBt,
1df5f 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72   1);.  }.  if( r
1df60 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1df61 20 20 20 20 2f 2a 20 49 66 20 61 20 74 72 61 6e      /* If a tran
1df62 73 69 65 6e 74 20 69 6e 64 65 78 20 69 73 20 72  sient index is r
1df63 65 71 75 69 72 65 64 2c 20 63 72 65 61 74 65 20  equired, create 
1df64 69 74 20 62 79 20 63 61 6c 6c 69 6e 67 0a 20 20  it by calling.  
1df65 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65    ** sqlite3Btre
1df66 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 20 77  eCreateTable() w
1df67 69 74 68 20 74 68 65 20 42 54 52 45 45 5f 5a 45  ith the BTREE_ZE
1df68 52 4f 44 41 54 41 20 66 6c 61 67 20 62 65 66 6f  RODATA flag befo
1df69 72 65 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69 6e  re.    ** openin
1df6a 67 20 69 74 2e 20 49 66 20 61 20 74 72 61 6e 73  g it. If a trans
1df6b 69 65 6e 74 20 74 61 62 6c 65 20 69 73 20 72 65  ient table is re
1df6c 71 75 69 72 65 64 2c 20 6a 75 73 74 20 75 73 65  quired, just use
1df6d 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 75 74 6f   the.    ** auto
1df6e 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74 65  matically create
1df6f 64 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f  d table with roo
1df70 74 2d 70 61 67 65 20 31 20 28 61 6e 20 49 4e 54  t-page 1 (an INT
1df71 4b 45 59 20 74 61 62 6c 65 29 2e 0a 20 20 20 20  KEY table)..    
1df72 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  */.    if( pOp->
1df73 70 34 2e 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20  p4.pKeyInfo ){. 
1df74 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20       int pgno;. 
1df75 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1df76 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59  ->p4type==P4_KEY
1df77 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20 20 72 63  INFO );.      rc
1df78 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
1df79 72 65 61 74 65 54 61 62 6c 65 28 75 2e 61 78 2e  reateTable(u.ax.
1df7a 70 43 78 2d 3e 70 42 74 2c 20 26 70 67 6e 6f 2c  pCx->pBt, &pgno,
1df7b 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 29   BTREE_ZERODATA)
1df7c 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
1df7d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1df7e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e       assert( pgn
1df7f 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 2b 31  o==MASTER_ROOT+1
1df80 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   );.        rc =
1df81 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
1df82 73 6f 72 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42  sor(u.ax.pCx->pB
1df83 74 2c 20 70 67 6e 6f 2c 20 31 2c 0a 20 20 20 20  t, pgno, 1,.    
1df84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1df85 20 20 20 20 20 20 20 20 20 20 20 20 28 4b 65 79              (Key
1df86 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c  Info*)pOp->p4.z,
1df87 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43 75 72 73   u.ax.pCx->pCurs
1df88 6f 72 29 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  or);.        u.a
1df89 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 20  x.pCx->pKeyInfo 
1df8a 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e  = pOp->p4.pKeyIn
1df8b 66 6f 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 78  fo;.        u.ax
1df8c 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e  .pCx->pKeyInfo->
1df8d 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29  enc = ENC(p->db)
1df8e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1df8f 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c  u.ax.pCx->isTabl
1df90 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  e = 0;.    }else
1df91 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1df92 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 28  ite3BtreeCursor(
1df93 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20 4d  u.ax.pCx->pBt, M
1df94 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 30  ASTER_ROOT, 1, 0
1df95 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43 75 72  , u.ax.pCx->pCur
1df96 73 6f 72 29 3b 0a 20 20 20 20 20 20 75 2e 61 78  sor);.      u.ax
1df97 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20  .pCx->isTable = 
1df98 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75  1;.    }.  }.  u
1df99 2e 61 78 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78  .ax.pCx->isIndex
1df9a 20 3d 20 21 75 2e 61 78 2e 70 43 78 2d 3e 69 73   = !u.ax.pCx->is
1df9b 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b 0a  Table;.  break;.
1df9c 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70  }../* Opcode: Op
1df9d 65 6e 50 73 65 75 64 6f 20 50 31 20 50 32 20 50  enPseudo P1 P2 P
1df9e 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e  3 * *.**.** Open
1df9f 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 74 68   a new cursor th
1dfa0 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66  at points to a f
1dfa1 61 6b 65 20 74 61 62 6c 65 20 74 68 61 74 20 63  ake table that c
1dfa2 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65  ontains a single
1dfa3 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74 61 2e  .** row of data.
1dfa4 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66    The content of
1dfa5 20 74 68 61 74 20 6f 6e 65 20 72 6f 77 20 69 6e   that one row in
1dfa6 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
1dfa7 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67 69 73 74  memory.** regist
1dfa8 65 72 20 50 32 2e 20 20 49 6e 20 6f 74 68 65 72  er P2.  In other
1dfa9 20 77 6f 72 64 73 2c 20 63 75 72 73 6f 72 20 50   words, cursor P
1dfaa 31 20 62 65 63 6f 6d 65 73 20 61 6e 20 61 6c 69  1 becomes an ali
1dfab 61 73 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20 4d  as for the .** M
1dfac 45 4d 5f 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 20  EM_Blob content 
1dfad 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 72 65 67  contained in reg
1dfae 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ister P2..**.** 
1dfaf 41 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63  A pseudo-table c
1dfb0 72 65 61 74 65 64 20 62 79 20 74 68 69 73 20 6f  reated by this o
1dfb1 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74 6f  pcode is used to
1dfb2 20 68 6f 6c 64 20 74 68 65 20 61 20 73 69 6e 67   hold the a sing
1dfb3 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 74  le.** row output
1dfb4 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72   from the sorter
1dfb5 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 6f 77   so that the row
1dfb6 20 63 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f 73   can be decompos
1dfb7 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76  ed into.** indiv
1dfb8 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75 73  idual columns us
1dfb9 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d  ing the OP_Colum
1dfba 6e 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20 4f  n opcode.  The O
1dfbb 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 0a  P_Column opcode.
1dfbc 2a 2a 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 63  ** is the only c
1dfbd 75 72 73 6f 72 20 6f 70 63 6f 64 65 20 74 68 61  ursor opcode tha
1dfbe 74 20 77 6f 72 6b 73 20 77 69 74 68 20 61 20 70  t works with a p
1dfbf 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a  seudo-table..**.
1dfc0 2a 2a 20 50 33 20 69 73 20 74 68 65 20 6e 75 6d  ** P3 is the num
1dfc1 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e  ber of fields in
1dfc2 20 74 68 65 20 72 65 63 6f 72 64 73 20 74 68 61   the records tha
1dfc3 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65 64  t will be stored
1dfc4 20 62 79 0a 2a 2a 20 74 68 65 20 70 73 65 75 64   by.** the pseud
1dfc5 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65  o-table..*/.case
1dfc6 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20   OP_OpenPseudo: 
1dfc7 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1dfc8 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1dfc9 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a 20  d into u.ay */. 
1dfca 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78   VdbeCursor *pCx
1dfcb 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1dfcc 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1dfcd 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a 0a  d into u.ay */..
1dfce 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1dfcf 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 79 2e 70  1>=0 );.  u.ay.p
1dfd0 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72  Cx = allocateCur
1dfd1 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c 20  sor(p, pOp->p1, 
1dfd2 70 4f 70 2d 3e 70 33 2c 20 2d 31 2c 20 30 29 3b  pOp->p3, -1, 0);
1dfd3 0a 20 20 69 66 28 20 75 2e 61 79 2e 70 43 78 3d  .  if( u.ay.pCx=
1dfd4 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  =0 ) goto no_mem
1dfd5 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 6e 75  ;.  u.ay.pCx->nu
1dfd6 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 75 2e 61  llRow = 1;.  u.a
1dfd7 79 2e 70 43 78 2d 3e 70 73 65 75 64 6f 54 61 62  y.pCx->pseudoTab
1dfd8 6c 65 52 65 67 20 3d 20 70 4f 70 2d 3e 70 32 3b  leReg = pOp->p2;
1dfd9 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 69 73 54  .  u.ay.pCx->isT
1dfda 61 62 6c 65 20 3d 20 31 3b 0a 20 20 75 2e 61 79  able = 1;.  u.ay
1dfdb 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d 20  .pCx->isIndex = 
1dfdc 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  0;.  break;.}../
1dfdd 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 6f 73 65 20  * Opcode: Close 
1dfde 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
1dfdf 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 20   Close a cursor 
1dfe0 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65  previously opene
1dfe1 64 20 61 73 20 50 31 2e 20 20 49 66 20 50 31 20  d as P1.  If P1 
1dfe2 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72 72 65 6e  is not.** curren
1dfe3 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69 73 20 69  tly open, this i
1dfe4 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 20  nstruction is a 
1dfe5 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f  no-op..*/.case O
1dfe6 50 5f 43 6c 6f 73 65 3a 20 7b 0a 20 20 61 73 73  P_Close: {.  ass
1dfe7 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1dfe8 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
1dfe9 75 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74  ursor );.  sqlit
1dfea 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f 72  e3VdbeFreeCursor
1dfeb 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  (p, p->apCsr[pOp
1dfec 2d 3e 70 31 5d 29 3b 0a 20 20 70 2d 3e 61 70 43  ->p1]);.  p->apC
1dfed 73 72 5b 70 4f 70 2d 3e 70 31 5d 20 3d 20 30 3b  sr[pOp->p1] = 0;
1dfee 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1dfef 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 65 20 50  Opcode: SeekGe P
1dff0 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1dff1 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20  ** If cursor P1 
1dff2 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c  refers to an SQL
1dff3 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74   table (B-Tree t
1dff4 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72  hat uses integer
1dff5 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20   keys), .** use 
1dff6 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1dff7 69 73 74 65 72 20 50 33 20 61 73 20 74 68 65 20  ister P3 as the 
1dff8 6b 65 79 2e 20 20 49 66 20 63 75 72 73 6f 72 20  key.  If cursor 
1dff9 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f  P1 refers .** to
1dffa 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74   an SQL index, t
1dffb 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69  hen P3 is the fi
1dffc 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20  rst in an array 
1dffd 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20  of P4 registers 
1dffe 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65  .** that are use
1dfff 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64  d as an unpacked
1e000 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a   index key. .**.
1e001 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75  ** Reposition cu
1e002 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20  rsor P1 so that 
1e003 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
1e004 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79  e smallest entry
1e005 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72 65   that .** is gre
1e006 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
1e007 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76 61  al to the key va
1e008 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 72  lue. If there ar
1e009 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a 2a  e no records .**
1e00a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
1e00b 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65   equal to the ke
1e00c 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20  y and P2 is not 
1e00d 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20  zero, then jump 
1e00e 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  to P2..**.** See
1e00f 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f   also: Found, No
1e010 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74  tFound, Distinct
1e011 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47 74  , SeekLt, SeekGt
1e012 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20 4f  , SeekLe.*/./* O
1e013 70 63 6f 64 65 3a 20 53 65 65 6b 47 74 20 50 31  pcode: SeekGt P1
1e014 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1e015 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72  * If cursor P1 r
1e016 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20  efers to an SQL 
1e017 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68  table (B-Tree th
1e018 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20  at uses integer 
1e019 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74  keys), .** use t
1e01a 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1e01b 73 74 65 72 20 50 33 20 61 73 20 61 20 6b 65 79  ster P3 as a key
1e01c 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72  . If cursor P1 r
1e01d 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e 20  efers .** to an 
1e01e 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e 20  SQL index, then 
1e01f 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74 20  P3 is the first 
1e020 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 50  in an array of P
1e021 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a 20  4 registers .** 
1e022 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73  that are used as
1e023 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64   an unpacked ind
1e024 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20 52  ex key. .**.** R
1e025 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f 72  eposition cursor
1e026 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 74 20   P1 so that  it 
1e027 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 6d  points to the sm
1e028 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 74 68 61  allest entry tha
1e029 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 74 65 72  t .** is greater
1e02a 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76 61   than the key va
1e02b 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61 72  lue. If there ar
1e02c 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 67 72 65  e no records gre
1e02d 61 74 65 72 20 74 68 61 6e 20 0a 2a 2a 20 74 68  ater than .** th
1e02e 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20  e key and P2 is 
1e02f 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a  not zero, then j
1e030 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a  ump to P2..**.**
1e031 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64   See also: Found
1e032 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74  , NotFound, Dist
1e033 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65  inct, SeekLt, Se
1e034 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a  ekGe, SeekLe.*/.
1e035 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 4c  /* Opcode: SeekL
1e036 74 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 20  t P1 P2 P3 P4 * 
1e037 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72  .**.** If cursor
1e038 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e   P1 refers to an
1e039 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72   SQL table (B-Tr
1e03a 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e 74  ee that uses int
1e03b 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20  eger keys), .** 
1e03c 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e  use the value in
1e03d 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20   register P3 as 
1e03e 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72  a key. If cursor
1e03f 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74   P1 refers .** t
1e040 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
1e041 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
1e042 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
1e043 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
1e044 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73   .** that are us
1e045 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65  ed as an unpacke
1e046 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a  d index key. .**
1e047 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63  .** Reposition c
1e048 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
1e049 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74    it points to t
1e04a 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79  he largest entry
1e04b 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73   that .** is les
1e04c 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76  s than the key v
1e04d 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61  alue. If there a
1e04e 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c 65  re no records le
1e04f 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20  ss than .** the 
1e050 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f  key and P2 is no
1e051 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d  t zero, then jum
1e052 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53  p to P2..**.** S
1e053 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20  ee also: Found, 
1e054 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e  NotFound, Distin
1e055 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65 6b  ct, SeekGt, Seek
1e056 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a  Ge, SeekLe.*/./*
1e057 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 4c 65 20   Opcode: SeekLe 
1e058 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
1e059 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31  .** If cursor P1
1e05a 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51   refers to an SQ
1e05b 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20  L table (B-Tree 
1e05c 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65  that uses intege
1e05d 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65  r keys), .** use
1e05e 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1e05f 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20 6b  gister P3 as a k
1e060 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31  ey. If cursor P1
1e061 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61   refers .** to a
1e062 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65  n SQL index, the
1e063 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73  n P3 is the firs
1e064 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66  t in an array of
1e065 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a   P4 registers .*
1e066 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20  * that are used 
1e067 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69  as an unpacked i
1e068 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a  ndex key. .**.**
1e069 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73   Reposition curs
1e06a 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74  or P1 so that it
1e06b 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 6c   points to the l
1e06c 61 72 67 65 73 74 20 65 6e 74 72 79 20 74 68 61  argest entry tha
1e06d 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74 68  t .** is less th
1e06e 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
1e06f 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66  he key value. If
1e070 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65   there are no re
1e071 63 6f 72 64 73 20 0a 2a 2a 20 6c 65 73 73 20 74  cords .** less t
1e072 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
1e073 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69  the key and P2 i
1e074 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e  s not zero, then
1e075 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a   jump to P2..**.
1e076 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75  ** See also: Fou
1e077 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69  nd, NotFound, Di
1e078 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20  stinct, SeekGt, 
1e079 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 74 0a 2a  SeekGe, SeekLt.*
1e07a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 4c 74  /.case OP_SeekLt
1e07b 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  :         /* jum
1e07c 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  p, in3 */.case O
1e07d 50 5f 53 65 65 6b 4c 65 3a 20 20 20 20 20 20 20  P_SeekLe:       
1e07e 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1e07f 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 47 65  /.case OP_SeekGe
1e080 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  :         /* jum
1e081 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  p, in3 */.case O
1e082 50 5f 53 65 65 6b 47 74 3a 20 7b 20 20 20 20 20  P_SeekGt: {     
1e083 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1e084 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1e085 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e086 64 20 69 6e 74 6f 20 75 2e 61 7a 20 2a 2f 0a 20  d into u.az */. 
1e087 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 6e 74 20   int res;.  int 
1e088 6f 63 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  oc;.  VdbeCursor
1e089 20 2a 70 43 3b 0a 20 20 55 6e 70 61 63 6b 65 64   *pC;.  Unpacked
1e08a 52 65 63 6f 72 64 20 72 3b 0a 20 20 69 6e 74 20  Record r;.  int 
1e08b 6e 46 69 65 6c 64 3b 0a 20 20 69 36 34 20 69 4b  nField;.  i64 iK
1e08c 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ey;      /* The 
1e08d 72 6f 77 69 64 20 77 65 20 61 72 65 20 74 6f 20  rowid we are to 
1e08e 73 65 65 6b 20 74 6f 20 2a 2f 0a 23 65 6e 64 69  seek to */.#endi
1e08f 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e090 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e091 75 2e 61 7a 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.az */..  asser
1e092 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1e093 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1e094 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  sor );.  assert(
1e095 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29 3b 0a 20   pOp->p2!=0 );. 
1e096 20 75 2e 61 7a 2e 70 43 20 3d 20 70 2d 3e 61 70   u.az.pC = p->ap
1e097 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e098 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 70 43 21  assert( u.az.pC!
1e099 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1e09a 75 2e 61 7a 2e 70 43 2d 3e 70 73 65 75 64 6f 54  u.az.pC->pseudoT
1e09b 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20  ableReg==0 );.  
1e09c 61 73 73 65 72 74 28 20 4f 50 5f 53 65 65 6b 4c  assert( OP_SeekL
1e09d 65 20 3d 3d 20 4f 50 5f 53 65 65 6b 4c 74 2b 31  e == OP_SeekLt+1
1e09e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 4f 50   );.  assert( OP
1e09f 5f 53 65 65 6b 47 65 20 3d 3d 20 4f 50 5f 53 65  _SeekGe == OP_Se
1e0a0 65 6b 4c 74 2b 32 20 29 3b 0a 20 20 61 73 73 65  ekLt+2 );.  asse
1e0a1 72 74 28 20 4f 50 5f 53 65 65 6b 47 74 20 3d 3d  rt( OP_SeekGt ==
1e0a2 20 4f 50 5f 53 65 65 6b 4c 74 2b 33 20 29 3b 0a   OP_SeekLt+3 );.
1e0a3 20 20 69 66 28 20 75 2e 61 7a 2e 70 43 2d 3e 70    if( u.az.pC->p
1e0a4 43 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20  Cursor!=0 ){.   
1e0a5 20 75 2e 61 7a 2e 6f 63 20 3d 20 70 4f 70 2d 3e   u.az.oc = pOp->
1e0a6 6f 70 63 6f 64 65 3b 0a 20 20 20 20 75 2e 61 7a  opcode;.    u.az
1e0a7 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30  .pC->nullRow = 0
1e0a8 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 70  ;.    if( u.az.p
1e0a9 43 2d 3e 69 73 54 61 62 6c 65 20 29 7b 0a 20 20  C->isTable ){.  
1e0aa 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 74      /* The input
1e0ab 20 76 61 6c 75 65 20 69 6e 20 50 33 20 6d 69 67   value in P3 mig
1e0ac 68 74 20 62 65 20 6f 66 20 61 6e 79 20 74 79 70  ht be of any typ
1e0ad 65 3a 20 69 6e 74 65 67 65 72 2c 20 72 65 61 6c  e: integer, real
1e0ae 2c 20 73 74 72 69 6e 67 2c 0a 20 20 20 20 20 20  , string,.      
1e0af 2a 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e 55 4c 4c  ** blob, or NULL
1e0b0 2e 20 20 42 75 74 20 69 74 20 6e 65 65 64 73 20  .  But it needs 
1e0b1 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72  to be an integer
1e0b2 20 62 65 66 6f 72 65 20 77 65 20 63 61 6e 20 64   before we can d
1e0b3 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73  o.      ** the s
1e0b4 65 65 6b 2c 20 73 6f 20 63 6f 76 65 72 74 20 69  eek, so covert i
1e0b5 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 6e 33  t. */.      pIn3
1e0b6 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33   = &aMem[pOp->p3
1e0b7 5d 3b 0a 20 20 20 20 20 20 61 70 70 6c 79 4e 75  ];.      applyNu
1e0b8 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 49  mericAffinity(pI
1e0b9 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e  n3);.      u.az.
1e0ba 69 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64  iKey = sqlite3Vd
1e0bb 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29  beIntValue(pIn3)
1e0bc 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d  ;.      u.az.pC-
1e0bd 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
1e0be 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  0;..      /* If 
1e0bf 74 68 65 20 50 33 20 76 61 6c 75 65 20 63 6f 75  the P3 value cou
1e0c0 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72  ld not be conver
1e0c1 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65  ted into an inte
1e0c2 67 65 72 20 77 69 74 68 6f 75 74 0a 20 20 20 20  ger without.    
1e0c3 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 69 6e 66    ** loss of inf
1e0c4 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 6e 20 73  ormation, then s
1e0c5 70 65 63 69 61 6c 20 70 72 6f 63 65 73 73 69 6e  pecial processin
1e0c6 67 20 69 73 20 72 65 71 75 69 72 65 64 2e 2e 2e  g is required...
1e0c7 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 28 70   */.      if( (p
1e0c8 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In3->flags & MEM
1e0c9 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20  _Int)==0 ){.    
1e0ca 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d 3e 66      if( (pIn3->f
1e0cb 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29  lags & MEM_Real)
1e0cc 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1e0cd 20 2f 2a 20 49 66 20 74 68 65 20 50 33 20 76 61   /* If the P3 va
1e0ce 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f  lue cannot be co
1e0cf 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 79  nverted into any
1e0d0 20 6b 69 6e 64 20 6f 66 20 61 20 6e 75 6d 62 65   kind of a numbe
1e0d1 72 2c 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  r,.          ** 
1e0d2 74 68 65 6e 20 74 68 65 20 73 65 65 6b 20 69 73  then the seek is
1e0d3 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c 20 73   not possible, s
1e0d4 6f 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a 2f 0a  o jump to P2 */.
1e0d5 20 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 70            pc = p
1e0d6 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1e0d7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1e0d8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f       }.        /
1e0d9 2a 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68  * If we reach th
1e0da 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74  is point, then t
1e0db 68 65 20 50 33 20 76 61 6c 75 65 20 6d 75 73 74  he P3 value must
1e0dc 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67 0a 20   be a floating. 
1e0dd 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 20         ** point 
1e0de 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20  number. */.     
1e0df 20 20 20 61 73 73 65 72 74 28 20 28 70 49 6e 33     assert( (pIn3
1e0e0 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ->flags & MEM_Re
1e0e1 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 20 20 20  al)!=0 );..     
1e0e2 20 20 20 69 66 28 20 75 2e 61 7a 2e 69 4b 65 79     if( u.az.iKey
1e0e3 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34  ==SMALLEST_INT64
1e0e4 20 26 26 20 28 70 49 6e 33 2d 3e 72 3c 28 64 6f   && (pIn3->r<(do
1e0e5 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 7c  uble)u.az.iKey |
1e0e6 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29 7b 0a  | pIn3->r>0) ){.
1e0e7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1e0e8 20 50 33 20 76 61 6c 75 65 20 69 73 20 74 6f 6f   P3 value is too
1e0e9 20 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e 69 74   large in magnit
1e0ea 75 64 65 20 74 6f 20 62 65 20 65 78 70 72 65 73  ude to be expres
1e0eb 73 65 64 20 61 73 20 61 6e 0a 20 20 20 20 20 20  sed as an.      
1e0ec 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20      ** integer. 
1e0ed 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75 2e 61  */.          u.a
1e0ee 7a 2e 72 65 73 20 3d 20 31 3b 0a 20 20 20 20 20  z.res = 1;.     
1e0ef 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72       if( pIn3->r
1e0f0 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <0 ){.          
1e0f1 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3e 3d 4f    if( u.az.oc>=O
1e0f2 50 5f 53 65 65 6b 47 65 20 29 7b 20 20 61 73 73  P_SeekGe ){  ass
1e0f3 65 72 74 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50  ert( u.az.oc==OP
1e0f4 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e 61 7a 2e  _SeekGe || u.az.
1e0f5 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b  oc==OP_SeekGt );
1e0f6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
1e0f7 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1e0f8 46 69 72 73 74 28 75 2e 61 7a 2e 70 43 2d 3e 70  First(u.az.pC->p
1e0f9 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65  Cursor, &u.az.re
1e0fa 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  s);.            
1e0fb 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1e0fc 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  _OK ) goto abort
1e0fd 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1e0fe 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1e0ff 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1e100 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e            if( u.
1e101 61 7a 2e 6f 63 3c 3d 4f 50 5f 53 65 65 6b 4c 65  az.oc<=OP_SeekLe
1e102 20 29 7b 20 20 61 73 73 65 72 74 28 20 75 2e 61   ){  assert( u.a
1e103 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20  z.oc==OP_SeekLt 
1e104 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53  || u.az.oc==OP_S
1e105 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 20 20 20  eekLe );.       
1e106 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1e107 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e 61  te3BtreeLast(u.a
1e108 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26  z.pC->pCursor, &
1e109 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20  u.az.res);.     
1e10a 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
1e10b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
1e10c 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1e10d 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20  rror;.          
1e10e 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
1e10f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 75 2e            if( u.
1e110 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20 20 20  az.res ){.      
1e111 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1e112 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20  p2 - 1;.        
1e113 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72    }.          br
1e114 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  eak;.        }el
1e115 73 65 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d 3d  se if( u.az.oc==
1e116 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c 20 75 2e 61  OP_SeekLt || u.a
1e117 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20  z.oc==OP_SeekGe 
1e118 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
1e119 55 73 65 20 74 68 65 20 63 65 69 6c 69 6e 67 28  Use the ceiling(
1e11a 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f  ) function to co
1e11b 6e 76 65 72 74 20 72 65 61 6c 2d 3e 69 6e 74 20  nvert real->int 
1e11c 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  */.          if(
1e11d 20 70 49 6e 33 2d 3e 72 20 3e 20 28 64 6f 75 62   pIn3->r > (doub
1e11e 6c 65 29 75 2e 61 7a 2e 69 4b 65 79 20 29 20 75  le)u.az.iKey ) u
1e11f 2e 61 7a 2e 69 4b 65 79 2b 2b 3b 0a 20 20 20 20  .az.iKey++;.    
1e120 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e121 20 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20       /* Use the 
1e122 66 6c 6f 6f 72 28 29 20 66 75 6e 63 74 69 6f 6e  floor() function
1e123 20 74 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 6c   to convert real
1e124 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20  ->int */.       
1e125 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e     assert( u.az.
1e126 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 65 20 7c 7c  oc==OP_SeekLe ||
1e127 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65   u.az.oc==OP_See
1e128 6b 47 74 20 29 3b 0a 20 20 20 20 20 20 20 20 20  kGt );.         
1e129 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 3c 20 28   if( pIn3->r < (
1e12a 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79  double)u.az.iKey
1e12b 20 29 20 75 2e 61 7a 2e 69 4b 65 79 2d 2d 3b 0a   ) u.az.iKey--;.
1e12c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1e12d 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  }.      rc = sql
1e12e 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55  ite3BtreeMovetoU
1e12f 6e 70 61 63 6b 65 64 28 75 2e 61 7a 2e 70 43 2d  npacked(u.az.pC-
1e130 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75 36  >pCursor, 0, (u6
1e131 34 29 75 2e 61 7a 2e 69 4b 65 79 2c 20 30 2c 20  4)u.az.iKey, 0, 
1e132 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20  &u.az.res);.    
1e133 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1e134 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
1e135 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1e136 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a  _error;.      }.
1e137 20 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72        if( u.az.r
1e138 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  es==0 ){.       
1e139 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49   u.az.pC->rowidI
1e13a 73 56 61 6c 69 64 20 3d 20 31 3b 0a 20 20 20 20  sValid = 1;.    
1e13b 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 6c 61 73      u.az.pC->las
1e13c 74 52 6f 77 69 64 20 3d 20 75 2e 61 7a 2e 69 4b  tRowid = u.az.iK
1e13d 65 79 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ey;.      }.    
1e13e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 61  }else{.      u.a
1e13f 7a 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e  z.nField = pOp->
1e140 70 34 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65  p4.i;.      asse
1e141 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  rt( pOp->p4type=
1e142 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20  =P4_INT32 );.   
1e143 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e     assert( u.az.
1e144 6e 46 69 65 6c 64 3e 30 20 29 3b 0a 20 20 20 20  nField>0 );.    
1e145 20 20 75 2e 61 7a 2e 72 2e 70 4b 65 79 49 6e 66    u.az.r.pKeyInf
1e146 6f 20 3d 20 75 2e 61 7a 2e 70 43 2d 3e 70 4b 65  o = u.az.pC->pKe
1e147 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 75 2e 61  yInfo;.      u.a
1e148 7a 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75 31  z.r.nField = (u1
1e149 36 29 75 2e 61 7a 2e 6e 46 69 65 6c 64 3b 0a 0a  6)u.az.nField;..
1e14a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78        /* The nex
1e14b 74 20 6c 69 6e 65 20 6f 66 20 63 6f 64 65 20 63  t line of code c
1e14c 6f 6d 70 75 74 65 73 20 61 73 20 66 6f 6c 6c 6f  omputes as follo
1e14d 77 73 2c 20 6f 6e 6c 79 20 66 61 73 74 65 72 3a  ws, only faster:
1e14e 0a 20 20 20 20 20 20 2a 2a 20 20 20 69 66 28 20  .      **   if( 
1e14f 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1e150 47 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f  Gt || u.az.oc==O
1e151 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20  P_SeekLe ){.    
1e152 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a 2e 72 2e    **     u.az.r.
1e153 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44  flags = UNPACKED
1e154 5f 49 4e 43 52 4b 45 59 3b 0a 20 20 20 20 20 20  _INCRKEY;.      
1e155 2a 2a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  **   }else{.    
1e156 20 20 2a 2a 20 20 20 20 20 75 2e 61 7a 2e 72 2e    **     u.az.r.
1e157 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 20  flags = 0;.     
1e158 20 2a 2a 20 20 20 7d 0a 20 20 20 20 20 20 2a 2f   **   }.      */
1e159 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 66 6c  .      u.az.r.fl
1e15a 61 67 73 20 3d 20 28 75 31 36 29 28 55 4e 50 41  ags = (u16)(UNPA
1e15b 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 2a 20 28  CKED_INCRKEY * (
1e15c 31 20 26 20 28 75 2e 61 7a 2e 6f 63 20 2d 20 4f  1 & (u.az.oc - O
1e15d 50 5f 53 65 65 6b 4c 74 29 29 29 3b 0a 20 20 20  P_SeekLt)));.   
1e15e 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e     assert( u.az.
1e15f 6f 63 21 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c  oc!=OP_SeekGt ||
1e160 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 3d 3d 55   u.az.r.flags==U
1e161 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20  NPACKED_INCRKEY 
1e162 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1e163 20 75 2e 61 7a 2e 6f 63 21 3d 4f 50 5f 53 65 65   u.az.oc!=OP_See
1e164 6b 4c 65 20 7c 7c 20 75 2e 61 7a 2e 72 2e 66 6c  kLe || u.az.r.fl
1e165 61 67 73 3d 3d 55 4e 50 41 43 4b 45 44 5f 49 4e  ags==UNPACKED_IN
1e166 43 52 4b 45 59 20 29 3b 0a 20 20 20 20 20 20 61  CRKEY );.      a
1e167 73 73 65 72 74 28 20 75 2e 61 7a 2e 6f 63 21 3d  ssert( u.az.oc!=
1e168 4f 50 5f 53 65 65 6b 47 65 20 7c 7c 20 75 2e 61  OP_SeekGe || u.a
1e169 7a 2e 72 2e 66 6c 61 67 73 3d 3d 30 20 29 3b 0a  z.r.flags==0 );.
1e16a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1e16b 61 7a 2e 6f 63 21 3d 4f 50 5f 53 65 65 6b 4c 74  az.oc!=OP_SeekLt
1e16c 20 7c 7c 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73   || u.az.r.flags
1e16d 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 75 2e  ==0 );..      u.
1e16e 61 7a 2e 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65  az.r.aMem = &aMe
1e16f 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20  m[pOp->p3];.    
1e170 20 20 45 78 70 61 6e 64 42 6c 6f 62 28 75 2e 61    ExpandBlob(u.a
1e171 7a 2e 72 2e 61 4d 65 6d 29 3b 0a 20 20 20 20 20  z.r.aMem);.     
1e172 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e173 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
1e174 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.az.pC->pCurso
1e175 72 2c 20 26 75 2e 61 7a 2e 72 2c 20 30 2c 20 30  r, &u.az.r, 0, 0
1e176 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20  , &u.az.res);.  
1e177 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1e178 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1e179 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1e17a 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
1e17b 7d 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d  }.      u.az.pC-
1e17c 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
1e17d 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 61  0;.    }.    u.a
1e17e 7a 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  z.pC->deferredMo
1e17f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e  veto = 0;.    u.
1e180 61 7a 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  az.pC->cacheStat
1e181 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1e182 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
1e183 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33  TEST.    sqlite3
1e184 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b  _search_count++;
1e185 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20  .#endif.    if( 
1e186 75 2e 61 7a 2e 6f 63 3e 3d 4f 50 5f 53 65 65 6b  u.az.oc>=OP_Seek
1e187 47 65 20 29 7b 20 20 61 73 73 65 72 74 28 20 75  Ge ){  assert( u
1e188 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47  .az.oc==OP_SeekG
1e189 65 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50  e || u.az.oc==OP
1e18a 5f 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20  _SeekGt );.     
1e18b 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 3c 30 20   if( u.az.res<0 
1e18c 7c 7c 20 28 75 2e 61 7a 2e 72 65 73 3d 3d 30 20  || (u.az.res==0 
1e18d 26 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53  && u.az.oc==OP_S
1e18e 65 65 6b 47 74 29 20 29 7b 0a 20 20 20 20 20 20  eekGt) ){.      
1e18f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1e190 72 65 65 4e 65 78 74 28 75 2e 61 7a 2e 70 43 2d  reeNext(u.az.pC-
1e191 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e  >pCursor, &u.az.
1e192 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66  res);.        if
1e193 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1e194 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1e195 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20  _to_error;.     
1e196 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69     u.az.pC->rowi
1e197 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1e198 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1e199 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d 20 30 3b     u.az.res = 0;
1e19a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
1e19b 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
1e19c 28 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65  ( u.az.oc==OP_Se
1e19d 65 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d  ekLt || u.az.oc=
1e19e 3d 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20  =OP_SeekLe );.  
1e19f 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73      if( u.az.res
1e1a0 3e 30 20 7c 7c 20 28 75 2e 61 7a 2e 72 65 73 3d  >0 || (u.az.res=
1e1a1 3d 30 20 26 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f  =0 && u.az.oc==O
1e1a2 50 5f 53 65 65 6b 4c 74 29 20 29 7b 0a 20 20 20  P_SeekLt) ){.   
1e1a3 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1e1a4 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 75  3BtreePrevious(u
1e1a5 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c  .az.pC->pCursor,
1e1a6 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20   &u.az.res);.   
1e1a7 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1e1a8 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62  ITE_OK ) goto ab
1e1a9 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1e1aa 3b 0a 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 70  ;.        u.az.p
1e1ab 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1e1ac 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
1e1ad 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 2e 61  {.        /* u.a
1e1ae 7a 2e 72 65 73 20 6d 69 67 68 74 20 62 65 20 6e  z.res might be n
1e1af 65 67 61 74 69 76 65 20 62 65 63 61 75 73 65 20  egative because 
1e1b0 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
1e1b1 74 79 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20 20  ty.  Check to.  
1e1b2 20 20 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20        ** see if 
1e1b3 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65  this is the case
1e1b4 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
1e1b5 20 20 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d 20       u.az.res = 
1e1b6 73 71 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28  sqlite3BtreeEof(
1e1b7 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.az.pC->pCursor
1e1b8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1e1b9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
1e1ba 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 69 66  ->p2>0 );.    if
1e1bb 28 20 75 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20  ( u.az.res ){.  
1e1bc 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1e1bd 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65   - 1;.    }.  }e
1e1be 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73  lse{.    /* This
1e1bf 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 74   happens when at
1e1c0 74 65 6d 70 74 69 6e 67 20 74 6f 20 6f 70 65 6e  tempting to open
1e1c1 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 73   the sqlite3_mas
1e1c2 74 65 72 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a  ter table.    **
1e1c3 20 66 6f 72 20 72 65 61 64 20 61 63 63 65 73 73   for read access
1e1c4 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
1e1c5 45 4d 50 54 59 2e 20 49 6e 20 74 68 69 73 20 63  EMPTY. In this c
1e1c6 61 73 65 20 61 6c 77 61 79 73 0a 20 20 20 20 2a  ase always.    *
1e1c7 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20  * take the jump 
1e1c8 28 73 69 6e 63 65 20 74 68 65 72 65 20 61 72 65  (since there are
1e1c9 20 6e 6f 20 72 65 63 6f 72 64 73 20 69 6e 20 74   no records in t
1e1ca 68 65 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 2a  he table)..    *
1e1cb 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  /.    pc = pOp->
1e1cc 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72  p2 - 1;.  }.  br
1e1cd 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e1ce 65 3a 20 53 65 65 6b 20 50 31 20 50 32 20 2a 20  e: Seek P1 P2 * 
1e1cf 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20  * *.**.** P1 is 
1e1d0 61 6e 20 6f 70 65 6e 20 74 61 62 6c 65 20 63 75  an open table cu
1e1d1 72 73 6f 72 20 61 6e 64 20 50 32 20 69 73 20 61  rsor and P2 is a
1e1d2 20 72 6f 77 69 64 20 69 6e 74 65 67 65 72 2e 20   rowid integer. 
1e1d3 20 41 72 72 61 6e 67 65 0a 2a 2a 20 66 6f 72 20   Arrange.** for 
1e1d4 50 31 20 74 6f 20 6d 6f 76 65 20 73 6f 20 74 68  P1 to move so th
1e1d5 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  at it points to 
1e1d6 74 68 65 20 72 6f 77 69 64 20 67 69 76 65 6e 20  the rowid given 
1e1d7 62 79 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  by P2..**.** Thi
1e1d8 73 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20  s is actually a 
1e1d9 64 65 66 65 72 72 65 64 20 73 65 65 6b 2e 20 20  deferred seek.  
1e1da 4e 6f 74 68 69 6e 67 20 61 63 74 75 61 6c 6c 79  Nothing actually
1e1db 20 68 61 70 70 65 6e 73 20 75 6e 74 69 6c 0a 2a   happens until.*
1e1dc 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  * the cursor is 
1e1dd 75 73 65 64 20 74 6f 20 72 65 61 64 20 61 20 72  used to read a r
1e1de 65 63 6f 72 64 2e 20 20 54 68 61 74 20 77 61 79  ecord.  That way
1e1df 2c 20 69 66 20 6e 6f 20 72 65 61 64 73 0a 2a 2a  , if no reads.**
1e1e0 20 6f 63 63 75 72 2c 20 6e 6f 20 75 6e 6e 65 63   occur, no unnec
1e1e1 65 73 73 61 72 79 20 49 2f 4f 20 68 61 70 70 65  essary I/O happe
1e1e2 6e 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  ns..*/.case OP_S
1e1e3 65 65 6b 3a 20 7b 20 20 20 20 2f 2a 20 69 6e 32  eek: {    /* in2
1e1e4 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
1e1e5 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e1e6 76 65 64 20 69 6e 74 6f 20 75 2e 62 61 20 2a 2f  ved into u.ba */
1e1e7 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1e1e8 43 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  C;.#endif /* loc
1e1e9 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e1ea 65 64 20 69 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a  ed into u.ba */.
1e1eb 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e1ec 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e1ed 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e1ee 20 75 2e 62 61 2e 70 43 20 3d 20 70 2d 3e 61 70   u.ba.pC = p->ap
1e1ef 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e1f0 61 73 73 65 72 74 28 20 75 2e 62 61 2e 70 43 21  assert( u.ba.pC!
1e1f1 3d 30 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41  =0 );.  if( ALWA
1e1f2 59 53 28 75 2e 62 61 2e 70 43 2d 3e 70 43 75 72  YS(u.ba.pC->pCur
1e1f3 73 6f 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 61  sor!=0) ){.    a
1e1f4 73 73 65 72 74 28 20 75 2e 62 61 2e 70 43 2d 3e  ssert( u.ba.pC->
1e1f5 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 75  isTable );.    u
1e1f6 2e 62 61 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20  .ba.pC->nullRow 
1e1f7 3d 20 30 3b 0a 20 20 20 20 70 49 6e 32 20 3d 20  = 0;.    pIn2 = 
1e1f8 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  &aMem[pOp->p2];.
1e1f9 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e 6d 6f 76      u.ba.pC->mov
1e1fa 65 74 6f 54 61 72 67 65 74 20 3d 20 73 71 6c 69  etoTarget = sqli
1e1fb 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28  te3VdbeIntValue(
1e1fc 70 49 6e 32 29 3b 0a 20 20 20 20 75 2e 62 61 2e  pIn2);.    u.ba.
1e1fd 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1e1fe 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 61 2e 70   = 0;.    u.ba.p
1e1ff 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1e200 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  o = 1;.  }.  bre
1e201 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20 4f 70 63  ak;.}.  ../* Opc
1e202 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31 20 50 32  ode: Found P1 P2
1e203 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49   P3 P4 *.**.** I
1e204 66 20 50 34 3d 3d 30 20 74 68 65 6e 20 72 65 67  f P4==0 then reg
1e205 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20 61  ister P3 holds a
1e206 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65   blob constructe
1e207 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64 2e  d by MakeRecord.
1e208 20 20 49 66 0a 2a 2a 20 50 34 3e 30 20 74 68 65    If.** P4>0 the
1e209 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 69 73  n register P3 is
1e20a 20 74 68 65 20 66 69 72 73 74 20 6f 66 20 50 34   the first of P4
1e20b 20 72 65 67 69 73 74 65 72 73 20 74 68 61 74 20   registers that 
1e20c 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64  form an unpacked
1e20d 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2a 0a 2a  .** record..**.*
1e20e 2a 20 43 75 72 73 6f 72 20 50 31 20 69 73 20 6f  * Cursor P1 is o
1e20f 6e 20 61 6e 20 69 6e 64 65 78 20 62 74 72 65 65  n an index btree
1e210 2e 20 20 49 66 20 74 68 65 20 72 65 63 6f 72 64  .  If the record
1e211 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 50   identified by P
1e212 33 20 61 6e 64 20 50 34 0a 2a 2a 20 69 73 20 61  3 and P4.** is a
1e213 20 70 72 65 66 69 78 20 6f 66 20 61 6e 79 20 65   prefix of any e
1e214 6e 74 72 79 20 69 6e 20 50 31 20 74 68 65 6e 20  ntry in P1 then 
1e215 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74  a jump is made t
1e216 6f 20 50 32 20 61 6e 64 0a 2a 2a 20 50 31 20 69  o P2 and.** P1 i
1e217 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
1e218 61 74 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20  at the matching 
1e219 65 6e 74 72 79 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63  entry..*/./* Opc
1e21a 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20 50 31  ode: NotFound P1
1e21b 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1e21c 2a 20 49 66 20 50 34 3d 3d 30 20 74 68 65 6e 20  * If P4==0 then 
1e21d 72 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64  register P3 hold
1e21e 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75  s a blob constru
1e21f 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f  cted by MakeReco
1e220 72 64 2e 20 20 49 66 0a 2a 2a 20 50 34 3e 30 20  rd.  If.** P4>0 
1e221 74 68 65 6e 20 72 65 67 69 73 74 65 72 20 50 33  then register P3
1e222 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 66   is the first of
1e223 20 50 34 20 72 65 67 69 73 74 65 72 73 20 74 68   P4 registers th
1e224 61 74 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63  at form an unpac
1e225 6b 65 64 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a  ked.** record..*
1e226 2a 20 0a 2a 2a 20 43 75 72 73 6f 72 20 50 31 20  * .** Cursor P1 
1e227 69 73 20 6f 6e 20 61 6e 20 69 6e 64 65 78 20 62  is on an index b
1e228 74 72 65 65 2e 20 20 49 66 20 74 68 65 20 72 65  tree.  If the re
1e229 63 6f 72 64 20 69 64 65 6e 74 69 66 69 65 64 20  cord identified 
1e22a 62 79 20 50 33 20 61 6e 64 20 50 34 0a 2a 2a 20  by P3 and P4.** 
1e22b 69 73 20 6e 6f 74 20 74 68 65 20 70 72 65 66 69  is not the prefi
1e22c 78 20 6f 66 20 61 6e 79 20 65 6e 74 72 79 20 69  x of any entry i
1e22d 6e 20 50 31 20 74 68 65 6e 20 61 20 6a 75 6d 70  n P1 then a jump
1e22e 20 69 73 20 6d 61 64 65 20 74 6f 20 50 32 2e 20   is made to P2. 
1e22f 20 49 66 20 50 31 20 0a 2a 2a 20 64 6f 65 73 20   If P1 .** does 
1e230 63 6f 6e 74 61 69 6e 20 61 6e 20 65 6e 74 72 79  contain an entry
1e231 20 77 68 6f 73 65 20 70 72 65 66 69 78 20 6d 61   whose prefix ma
1e232 74 63 68 65 73 20 74 68 65 20 50 33 2f 50 34 20  tches the P3/P4 
1e233 72 65 63 6f 72 64 20 74 68 65 6e 20 63 6f 6e 74  record then cont
1e234 72 6f 6c 0a 2a 2a 20 66 61 6c 6c 73 20 74 68 72  rol.** falls thr
1e235 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74  ough to the next
1e236 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64   instruction and
1e237 20 50 31 20 69 73 20 6c 65 66 74 20 70 6f 69 6e   P1 is left poin
1e238 74 69 6e 67 20 61 74 20 74 68 65 0a 2a 2a 20 6d  ting at the.** m
1e239 61 74 63 68 69 6e 67 20 65 6e 74 72 79 2e 0a 2a  atching entry..*
1e23a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46  *.** See also: F
1e23b 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c  ound, NotExists,
1e23c 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73   IsUnique.*/.cas
1e23d 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20  e OP_NotFound:  
1e23e 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1e23f 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75  3 */.case OP_Fou
1e240 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  nd: {        /* 
1e241 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66  jump, in3 */.#if
1e242 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e243 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e244 6f 20 75 2e 62 62 20 2a 2f 0a 20 20 69 6e 74 20  o u.bb */.  int 
1e245 61 6c 72 65 61 64 79 45 78 69 73 74 73 3b 0a 20  alreadyExists;. 
1e246 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1e247 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e  .  int res;.  Un
1e248 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49  packedRecord *pI
1e249 64 78 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b 65  dxKey;.  Unpacke
1e24a 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 63 68 61  dRecord r;.  cha
1e24b 72 20 61 54 65 6d 70 52 65 63 5b 52 4f 55 4e 44  r aTempRec[ROUND
1e24c 38 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b 65  8(sizeof(Unpacke
1e24d 64 52 65 63 6f 72 64 29 29 20 2b 20 73 69 7a 65  dRecord)) + size
1e24e 6f 66 28 4d 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a  of(Mem)*3 + 7];.
1e24f 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1e250 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e251 69 6e 74 6f 20 75 2e 62 62 20 2a 2f 0a 0a 23 69  into u.bb */..#i
1e252 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1e253 0a 20 20 73 71 6c 69 74 65 33 5f 66 6f 75 6e 64  .  sqlite3_found
1e254 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
1e255 0a 0a 20 20 75 2e 62 62 2e 61 6c 72 65 61 64 79  ..  u.bb.already
1e256 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 61 73  Exists = 0;.  as
1e257 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e258 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e259 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65  Cursor );.  asse
1e25a 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  rt( pOp->p4type=
1e25b 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 75  =P4_INT32 );.  u
1e25c 2e 62 62 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73  .bb.pC = p->apCs
1e25d 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  r[pOp->p1];.  as
1e25e 73 65 72 74 28 20 75 2e 62 62 2e 70 43 21 3d 30  sert( u.bb.pC!=0
1e25f 20 29 3b 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d   );.  pIn3 = &aM
1e260 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69  em[pOp->p3];.  i
1e261 66 28 20 41 4c 57 41 59 53 28 75 2e 62 62 2e 70  f( ALWAYS(u.bb.p
1e262 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 29  C->pCursor!=0) )
1e263 7b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  {..    assert( u
1e264 2e 62 62 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d  .bb.pC->isTable=
1e265 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4f  =0 );.    if( pO
1e266 70 2d 3e 70 34 2e 69 3e 30 20 29 7b 0a 20 20 20  p->p4.i>0 ){.   
1e267 20 20 20 75 2e 62 62 2e 72 2e 70 4b 65 79 49 6e     u.bb.r.pKeyIn
1e268 66 6f 20 3d 20 75 2e 62 62 2e 70 43 2d 3e 70 4b  fo = u.bb.pC->pK
1e269 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 75 2e  eyInfo;.      u.
1e26a 62 62 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 28 75  bb.r.nField = (u
1e26b 31 36 29 70 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20  16)pOp->p4.i;.  
1e26c 20 20 20 20 75 2e 62 62 2e 72 2e 61 4d 65 6d 20      u.bb.r.aMem 
1e26d 3d 20 70 49 6e 33 3b 0a 20 20 20 20 20 20 75 2e  = pIn3;.      u.
1e26e 62 62 2e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50  bb.r.flags = UNP
1e26f 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54  ACKED_PREFIX_MAT
1e270 43 48 3b 0a 20 20 20 20 20 20 75 2e 62 62 2e 70  CH;.      u.bb.p
1e271 49 64 78 4b 65 79 20 3d 20 26 75 2e 62 62 2e 72  IdxKey = &u.bb.r
1e272 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1e273 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d     assert( pIn3-
1e274 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
1e275 62 20 29 3b 0a 20 20 20 20 20 20 45 78 70 61 6e  b );.      Expan
1e276 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20 20  dBlob(pIn3);.   
1e277 20 20 20 75 2e 62 62 2e 70 49 64 78 4b 65 79 20     u.bb.pIdxKey 
1e278 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  = sqlite3VdbeRec
1e279 6f 72 64 55 6e 70 61 63 6b 28 75 2e 62 62 2e 70  ordUnpack(u.bb.p
1e27a 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20 70 49 6e  C->pKeyInfo, pIn
1e27b 33 2d 3e 6e 2c 20 70 49 6e 33 2d 3e 7a 2c 0a 20  3->n, pIn3->z,. 
1e27c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e27d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e27e 20 20 20 20 20 20 20 75 2e 62 62 2e 61 54 65 6d         u.bb.aTem
1e27f 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 75 2e 62  pRec, sizeof(u.b
1e280 62 2e 61 54 65 6d 70 52 65 63 29 29 3b 0a 20 20  b.aTempRec));.  
1e281 20 20 20 20 69 66 28 20 75 2e 62 62 2e 70 49 64      if( u.bb.pId
1e282 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  xKey==0 ){.     
1e283 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a     goto no_mem;.
1e284 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e        }.      u.
1e285 62 62 2e 70 49 64 78 4b 65 79 2d 3e 66 6c 61 67  bb.pIdxKey->flag
1e286 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52  s |= UNPACKED_PR
1e287 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20 20  EFIX_MATCH;.    
1e288 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
1e289 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
1e28a 61 63 6b 65 64 28 75 2e 62 62 2e 70 43 2d 3e 70  acked(u.bb.pC->p
1e28b 43 75 72 73 6f 72 2c 20 75 2e 62 62 2e 70 49 64  Cursor, u.bb.pId
1e28c 78 4b 65 79 2c 20 30 2c 20 30 2c 20 26 75 2e 62  xKey, 0, 0, &u.b
1e28d 62 2e 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20  b.res);.    if( 
1e28e 70 4f 70 2d 3e 70 34 2e 69 3d 3d 30 20 29 7b 0a  pOp->p4.i==0 ){.
1e28f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1e290 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52  eDeleteUnpackedR
1e291 65 63 6f 72 64 28 75 2e 62 62 2e 70 49 64 78 4b  ecord(u.bb.pIdxK
1e292 65 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ey);.    }.    i
1e293 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1e294 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   ){.      break;
1e295 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 62 2e  .    }.    u.bb.
1e296 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 3d 20  alreadyExists = 
1e297 28 75 2e 62 62 2e 72 65 73 3d 3d 30 29 3b 0a 20  (u.bb.res==0);. 
1e298 20 20 20 75 2e 62 62 2e 70 43 2d 3e 64 65 66 65     u.bb.pC->defe
1e299 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a  rredMoveto = 0;.
1e29a 20 20 20 20 75 2e 62 62 2e 70 43 2d 3e 63 61 63      u.bb.pC->cac
1e29b 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1e29c 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 69 66  _STALE;.  }.  if
1e29d 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
1e29e 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20 69  P_Found ){.    i
1e29f 66 28 20 75 2e 62 62 2e 61 6c 72 65 61 64 79 45  f( u.bb.alreadyE
1e2a0 78 69 73 74 73 20 29 20 70 63 20 3d 20 70 4f 70  xists ) pc = pOp
1e2a1 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73  ->p2 - 1;.  }els
1e2a2 65 7b 0a 20 20 20 20 69 66 28 20 21 75 2e 62 62  e{.    if( !u.bb
1e2a3 2e 61 6c 72 65 61 64 79 45 78 69 73 74 73 20 29  .alreadyExists )
1e2a4 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1e2a5 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  1;.  }.  break;.
1e2a6 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73  }../* Opcode: Is
1e2a7 55 6e 69 71 75 65 20 50 31 20 50 32 20 50 33 20  Unique P1 P2 P3 
1e2a8 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 75 72 73 6f  P4 *.**.** Curso
1e2a9 72 20 50 31 20 69 73 20 6f 70 65 6e 20 6f 6e 20  r P1 is open on 
1e2aa 61 6e 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20  an index b-tree 
1e2ab 2d 20 74 68 61 74 20 69 73 20 74 6f 20 73 61 79  - that is to say
1e2ac 2c 20 61 20 62 74 72 65 65 20 77 68 69 63 68 0a  , a btree which.
1e2ad 2a 2a 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 77  ** no data and w
1e2ae 68 65 72 65 20 74 68 65 20 6b 65 79 20 61 72 65  here the key are
1e2af 20 72 65 63 6f 72 64 73 20 67 65 6e 65 72 61 74   records generat
1e2b0 65 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63  ed by OP_MakeRec
1e2b1 6f 72 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20  ord with.** the 
1e2b2 6c 69 73 74 20 66 69 65 6c 64 20 62 65 69 6e 67  list field being
1e2b3 20 74 68 65 20 69 6e 74 65 67 65 72 20 52 4f 57   the integer ROW
1e2b4 49 44 20 6f 66 20 74 68 65 20 65 6e 74 72 79 20  ID of the entry 
1e2b5 74 68 61 74 20 74 68 65 20 69 6e 64 65 78 0a 2a  that the index.*
1e2b6 2a 20 65 6e 74 72 79 20 72 65 66 65 72 73 20 74  * entry refers t
1e2b7 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 20  o..**.** The P3 
1e2b8 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e  register contain
1e2b9 73 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 63  s an integer rec
1e2ba 6f 72 64 20 6e 75 6d 62 65 72 2e 20 43 61 6c 6c  ord number. Call
1e2bb 20 74 68 69 73 20 72 65 63 6f 72 64 20 0a 2a 2a   this record .**
1e2bc 20 6e 75 6d 62 65 72 20 52 2e 20 52 65 67 69 73   number R. Regis
1e2bd 74 65 72 20 50 34 20 69 73 20 74 68 65 20 66 69  ter P4 is the fi
1e2be 72 73 74 20 69 6e 20 61 20 73 65 74 20 6f 66 20  rst in a set of 
1e2bf 4e 20 63 6f 6e 74 69 67 75 6f 75 73 20 72 65 67  N contiguous reg
1e2c0 69 73 74 65 72 73 0a 2a 2a 20 74 68 61 74 20 6d  isters.** that m
1e2c1 61 6b 65 20 75 70 20 61 6e 20 75 6e 70 61 63 6b  ake up an unpack
1e2c2 65 64 20 69 6e 64 65 78 20 6b 65 79 20 74 68 61  ed index key tha
1e2c3 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 77 69  t can be used wi
1e2c4 74 68 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a  th cursor P1..**
1e2c5 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 20   The value of N 
1e2c6 63 61 6e 20 62 65 20 69 6e 66 65 72 72 65 64 20  can be inferred 
1e2c7 66 72 6f 6d 20 74 68 65 20 63 75 72 73 6f 72 2e  from the cursor.
1e2c8 20 4e 20 69 6e 63 6c 75 64 65 73 20 74 68 65 20   N includes the 
1e2c9 72 6f 77 69 64 0a 2a 2a 20 76 61 6c 75 65 20 61  rowid.** value a
1e2ca 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65  ppended to the e
1e2cb 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  nd of the index 
1e2cc 72 65 63 6f 72 64 2e 20 54 68 69 73 20 72 6f 77  record. This row
1e2cd 69 64 20 76 61 6c 75 65 20 6d 61 79 0a 2a 2a 20  id value may.** 
1e2ce 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68  or may not be th
1e2cf 65 20 73 61 6d 65 20 61 73 20 52 2e 0a 2a 2a 0a  e same as R..**.
1e2d0 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68 65  ** If any of the
1e2d1 20 4e 20 72 65 67 69 73 74 65 72 73 20 62 65 67   N registers beg
1e2d2 69 6e 6e 69 6e 67 20 77 69 74 68 20 72 65 67 69  inning with regi
1e2d3 73 74 65 72 20 50 34 20 63 6f 6e 74 61 69 6e 73  ster P4 contains
1e2d4 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76 61 6c 75 65   a NULL.** value
1e2d5 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65  , jump immediate
1e2d6 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ly to P2..**.** 
1e2d7 4f 74 68 65 72 77 69 73 65 2c 20 74 68 69 73 20  Otherwise, this 
1e2d8 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 68 65 63  instruction chec
1e2d9 6b 73 20 69 66 20 63 75 72 73 6f 72 20 50 31 20  ks if cursor P1 
1e2da 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e 74 72  contains an entr
1e2db 79 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 66  y.** where the f
1e2dc 69 72 73 74 20 28 4e 2d 31 29 20 66 69 65 6c 64  irst (N-1) field
1e2dd 73 20 6d 61 74 63 68 20 62 75 74 20 74 68 65 20  s match but the 
1e2de 72 6f 77 69 64 20 76 61 6c 75 65 20 61 74 20 74  rowid value at t
1e2df 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20 74 68 65  he end.** of the
1e2e0 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20   index entry is 
1e2e1 6e 6f 74 20 52 2e 20 49 66 20 74 68 65 72 65 20  not R. If there 
1e2e2 69 73 20 6e 6f 20 73 75 63 68 20 65 6e 74 72 79  is no such entry
1e2e3 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 0a  , control jumps.
1e2e4 2a 2a 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f  ** to instructio
1e2e5 6e 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c  n P2. Otherwise,
1e2e6 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   the rowid of th
1e2e7 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 69 6e  e conflicting in
1e2e8 64 65 78 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20  dex.** entry is 
1e2e9 63 6f 70 69 65 64 20 74 6f 20 72 65 67 69 73 74  copied to regist
1e2ea 65 72 20 50 33 20 61 6e 64 20 63 6f 6e 74 72 6f  er P3 and contro
1e2eb 6c 20 66 61 6c 6c 73 20 74 68 72 6f 75 67 68 20  l falls through 
1e2ec 74 6f 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 69  to the next.** i
1e2ed 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  nstruction..**.*
1e2ee 2a 20 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46  * See also: NotF
1e2ef 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c  ound, NotExists,
1e2f0 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73 65 20 4f   Found.*/.case O
1e2f1 50 5f 49 73 55 6e 69 71 75 65 3a 20 7b 20 20 20  P_IsUnique: {   
1e2f2 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1e2f3 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  3 */.#if 0  /* l
1e2f4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e2f5 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 63 20 2a  oved into u.bc *
1e2f6 2f 0a 20 20 75 31 36 20 69 69 3b 0a 20 20 56 64  /.  u16 ii;.  Vd
1e2f7 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20  beCursor *pCx;. 
1e2f8 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1e2f9 3b 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 0a  ;.  u16 nField;.
1e2fa 20 20 4d 65 6d 20 2a 61 4d 78 3b 0a 20 20 55 6e    Mem *aMx;.  Un
1e2fb 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b 20  packedRecord r; 
1e2fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e2fd 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65 78   /* B-Tree index
1e2fe 20 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a 20   search key */. 
1e2ff 20 69 36 34 20 52 3b 20 20 20 20 20 20 20 20 20   i64 R;         
1e300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e301 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 73 74 6f      /* Rowid sto
1e302 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
1e303 50 33 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  P3 */.#endif /* 
1e304 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e305 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 63 20  moved into u.bc 
1e306 2a 2f 0a 0a 20 20 70 49 6e 33 20 3d 20 26 61 4d  */..  pIn3 = &aM
1e307 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75  em[pOp->p3];.  u
1e308 2e 62 63 2e 61 4d 78 20 3d 20 26 61 4d 65 6d 5b  .bc.aMx = &aMem[
1e309 70 4f 70 2d 3e 70 34 2e 69 5d 3b 0a 20 20 2f 2a  pOp->p4.i];.  /*
1e30a 20 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65   Assert that the
1e30b 20 76 61 6c 75 65 73 20 6f 66 20 70 61 72 61 6d   values of param
1e30c 65 74 65 72 73 20 50 31 20 61 6e 64 20 50 34 20  eters P1 and P4 
1e30d 61 72 65 20 69 6e 20 72 61 6e 67 65 2e 20 2a 2f  are in range. */
1e30e 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e30f 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32  p4type==P4_INT32
1e310 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1e311 70 2d 3e 70 34 2e 69 3e 30 20 26 26 20 70 4f 70  p->p4.i>0 && pOp
1e312 2d 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d 65 6d 20  ->p4.i<=p->nMem 
1e313 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1e314 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1e315 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1e316 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20  ..  /* Find the 
1e317 69 6e 64 65 78 20 63 75 72 73 6f 72 2e 20 2a 2f  index cursor. */
1e318 0a 20 20 75 2e 62 63 2e 70 43 78 20 3d 20 70 2d  .  u.bc.pCx = p-
1e319 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1e31a 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 63 2e  .  assert( u.bc.
1e31b 70 43 78 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  pCx->deferredMov
1e31c 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 63  eto==0 );.  u.bc
1e31d 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c 74  .pCx->seekResult
1e31e 20 3d 20 30 3b 0a 20 20 75 2e 62 63 2e 70 43 78   = 0;.  u.bc.pCx
1e31f 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1e320 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 75  CACHE_STALE;.  u
1e321 2e 62 63 2e 70 43 72 73 72 20 3d 20 75 2e 62 63  .bc.pCrsr = u.bc
1e322 2e 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 0a  .pCx->pCursor;..
1e323 20 20 2f 2a 20 49 66 20 61 6e 79 20 6f 66 20 74    /* If any of t
1e324 68 65 20 76 61 6c 75 65 73 20 61 72 65 20 4e 55  he values are NU
1e325 4c 4c 2c 20 74 61 6b 65 20 74 68 65 20 6a 75 6d  LL, take the jum
1e326 70 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e 6e 46 69  p. */.  u.bc.nFi
1e327 65 6c 64 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e  eld = u.bc.pCx->
1e328 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64  pKeyInfo->nField
1e329 3b 0a 20 20 66 6f 72 28 75 2e 62 63 2e 69 69 3d  ;.  for(u.bc.ii=
1e32a 30 3b 20 75 2e 62 63 2e 69 69 3c 75 2e 62 63 2e  0; u.bc.ii<u.bc.
1e32b 6e 46 69 65 6c 64 3b 20 75 2e 62 63 2e 69 69 2b  nField; u.bc.ii+
1e32c 2b 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 63  +){.    if( u.bc
1e32d 2e 61 4d 78 5b 75 2e 62 63 2e 69 69 5d 2e 66 6c  .aMx[u.bc.ii].fl
1e32e 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
1e32f 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  {.      pc = pOp
1e330 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20  ->p2 - 1;.      
1e331 75 2e 62 63 2e 70 43 72 73 72 20 3d 20 30 3b 0a  u.bc.pCrsr = 0;.
1e332 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1e333 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28   }.  }.  assert(
1e334 20 28 75 2e 62 63 2e 61 4d 78 5b 75 2e 62 63 2e   (u.bc.aMx[u.bc.
1e335 6e 46 69 65 6c 64 5d 2e 66 6c 61 67 73 20 26 20  nField].flags & 
1e336 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a  MEM_Null)==0 );.
1e337 0a 20 20 69 66 28 20 75 2e 62 63 2e 70 43 72 73  .  if( u.bc.pCrs
1e338 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 50  r!=0 ){.    /* P
1e339 6f 70 75 6c 61 74 65 20 74 68 65 20 69 6e 64 65  opulate the inde
1e33a 78 20 73 65 61 72 63 68 20 6b 65 79 2e 20 2a 2f  x search key. */
1e33b 0a 20 20 20 20 75 2e 62 63 2e 72 2e 70 4b 65 79  .    u.bc.r.pKey
1e33c 49 6e 66 6f 20 3d 20 75 2e 62 63 2e 70 43 78 2d  Info = u.bc.pCx-
1e33d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 75  >pKeyInfo;.    u
1e33e 2e 62 63 2e 72 2e 6e 46 69 65 6c 64 20 3d 20 75  .bc.r.nField = u
1e33f 2e 62 63 2e 6e 46 69 65 6c 64 20 2b 20 31 3b 0a  .bc.nField + 1;.
1e340 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c 61 67 73      u.bc.r.flags
1e341 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46   = UNPACKED_PREF
1e342 49 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 20 75  IX_SEARCH;.    u
1e343 2e 62 63 2e 72 2e 61 4d 65 6d 20 3d 20 75 2e 62  .bc.r.aMem = u.b
1e344 63 2e 61 4d 78 3b 0a 0a 20 20 20 20 2f 2a 20 45  c.aMx;..    /* E
1e345 78 74 72 61 63 74 20 74 68 65 20 76 61 6c 75 65  xtract the value
1e346 20 6f 66 20 75 2e 62 63 2e 52 20 66 72 6f 6d 20   of u.bc.R from 
1e347 72 65 67 69 73 74 65 72 20 50 33 2e 20 2a 2f 0a  register P3. */.
1e348 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1e349 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e  emIntegerify(pIn
1e34a 33 29 3b 0a 20 20 20 20 75 2e 62 63 2e 52 20 3d  3);.    u.bc.R =
1e34b 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 0a 20 20 20   pIn3->u.i;..   
1e34c 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 42   /* Search the B
1e34d 2d 54 72 65 65 20 69 6e 64 65 78 2e 20 49 66 20  -Tree index. If 
1e34e 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20 72  no conflicting r
1e34f 65 63 6f 72 64 20 69 73 20 66 6f 75 6e 64 2c 20  ecord is found, 
1e350 6a 75 6d 70 0a 20 20 20 20 2a 2a 20 74 6f 20 50  jump.    ** to P
1e351 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 63 6f  2. Otherwise, co
1e352 70 79 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20  py the rowid of 
1e353 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67 20  the conflicting 
1e354 72 65 63 6f 72 64 20 74 6f 0a 20 20 20 20 2a 2a  record to.    **
1e355 20 72 65 67 69 73 74 65 72 20 50 33 20 61 6e 64   register P3 and
1e356 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f   fall through to
1e357 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75   the next instru
1e358 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 72  ction.  */.    r
1e359 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1e35a 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75  MovetoUnpacked(u
1e35b 2e 62 63 2e 70 43 72 73 72 2c 20 26 75 2e 62 63  .bc.pCrsr, &u.bc
1e35c 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 62 63 2e  .r, 0, 0, &u.bc.
1e35d 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c 74 29  pCx->seekResult)
1e35e 3b 0a 20 20 20 20 69 66 28 20 28 75 2e 62 63 2e  ;.    if( (u.bc.
1e35f 72 2e 66 6c 61 67 73 20 26 20 55 4e 50 41 43 4b  r.flags & UNPACK
1e360 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52 43 48  ED_PREFIX_SEARCH
1e361 29 20 7c 7c 20 75 2e 62 63 2e 72 2e 72 6f 77 69  ) || u.bc.r.rowi
1e362 64 3d 3d 75 2e 62 63 2e 52 20 29 7b 0a 20 20 20  d==u.bc.R ){.   
1e363 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1e364 2d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  - 1;.    }else{.
1e365 20 20 20 20 20 20 70 49 6e 33 2d 3e 75 2e 69 20        pIn3->u.i 
1e366 3d 20 75 2e 62 63 2e 72 2e 72 6f 77 69 64 3b 0a  = u.bc.r.rowid;.
1e367 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61      }.  }.  brea
1e368 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1e369 20 4e 6f 74 45 78 69 73 74 73 20 50 31 20 50 32   NotExists P1 P2
1e36a 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 55 73   P3 * *.**.** Us
1e36b 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
1e36c 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20   register P3 as 
1e36d 61 20 69 6e 74 65 67 65 72 20 6b 65 79 2e 20 20  a integer key.  
1e36e 49 66 20 61 20 72 65 63 6f 72 64 20 0a 2a 2a 20  If a record .** 
1e36f 77 69 74 68 20 74 68 61 74 20 6b 65 79 20 64 6f  with that key do
1e370 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20  es not exist in 
1e371 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 74 68 65  table of P1, the
1e372 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 0a 2a  n jump to P2. .*
1e373 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20  * If the record 
1e374 64 6f 65 73 20 65 78 69 73 74 2c 20 74 68 65 6e  does exist, then
1e375 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54 68 65   fall thru.  The
1e376 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20   cursor is left 
1e377 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  .** pointing to 
1e378 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 69 74  the record if it
1e379 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54   exists..**.** T
1e37a 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62 65  he difference be
1e37b 74 77 65 65 6e 20 74 68 69 73 20 6f 70 65 72 61  tween this opera
1e37c 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46 6f 75 6e  tion and NotFoun
1e37d 64 20 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a  d is that this.*
1e37e 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61 73 73 75  * operation assu
1e37f 6d 65 73 20 74 68 65 20 6b 65 79 20 69 73 20 61  mes the key is a
1e380 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20 74 68  n integer and th
1e381 61 74 20 50 31 20 69 73 20 61 20 74 61 62 6c 65  at P1 is a table
1e382 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e 6f 74 46   whereas.** NotF
1e383 6f 75 6e 64 20 61 73 73 75 6d 65 73 20 6b 65 79  ound assumes key
1e384 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74   is a blob const
1e385 72 75 63 74 65 64 20 66 72 6f 6d 20 4d 61 6b 65  ructed from Make
1e386 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a 20 50 31  Record and.** P1
1e387 20 69 73 20 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a   is an index..**
1e388 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
1e389 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 49  und, NotFound, I
1e38a 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73 65 20  sUnique.*/.case 
1e38b 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a 20 7b 20  OP_NotExists: { 
1e38c 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20         /* jump, 
1e38d 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  in3 */.#if 0  /*
1e38e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e38f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 64   moved into u.bd
1e390 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72   */.  VdbeCursor
1e391 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72   *pC;.  BtCursor
1e392 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72   *pCrsr;.  int r
1e393 65 73 3b 0a 20 20 75 36 34 20 69 4b 65 79 3b 0a  es;.  u64 iKey;.
1e394 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1e395 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e396 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a 0a 20 20  into u.bd */..  
1e397 70 49 6e 33 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn3 = &aMem[pOp
1e398 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p3];.  assert(
1e399 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn3->flags & M
1e39a 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 61 73 73 65  EM_Int );.  asse
1e39b 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e39c 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e39d 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 64 2e 70  rsor );.  u.bd.p
1e39e 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e39f 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1e3a0 20 75 2e 62 64 2e 70 43 21 3d 30 20 29 3b 0a 20   u.bd.pC!=0 );. 
1e3a1 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43   assert( u.bd.pC
1e3a2 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 61  ->isTable );.  a
1e3a3 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43 2d 3e  ssert( u.bd.pC->
1e3a4 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d  pseudoTableReg==
1e3a5 30 20 29 3b 0a 20 20 75 2e 62 64 2e 70 43 72 73  0 );.  u.bd.pCrs
1e3a6 72 20 3d 20 75 2e 62 64 2e 70 43 2d 3e 70 43 75  r = u.bd.pC->pCu
1e3a7 72 73 6f 72 3b 0a 20 20 69 66 28 20 75 2e 62 64  rsor;.  if( u.bd
1e3a8 2e 70 43 72 73 72 21 3d 30 20 29 7b 0a 20 20 20  .pCrsr!=0 ){.   
1e3a9 20 75 2e 62 64 2e 72 65 73 20 3d 20 30 3b 0a 20   u.bd.res = 0;. 
1e3aa 20 20 20 75 2e 62 64 2e 69 4b 65 79 20 3d 20 70     u.bd.iKey = p
1e3ab 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 72 63  In3->u.i;.    rc
1e3ac 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
1e3ad 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e  ovetoUnpacked(u.
1e3ae 62 64 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62  bd.pCrsr, 0, u.b
1e3af 64 2e 69 4b 65 79 2c 20 30 2c 20 26 75 2e 62 64  d.iKey, 0, &u.bd
1e3b0 2e 72 65 73 29 3b 0a 20 20 20 20 75 2e 62 64 2e  .res);.    u.bd.
1e3b1 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20  pC->lastRowid = 
1e3b2 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 75  pIn3->u.i;.    u
1e3b3 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56  .bd.pC->rowidIsV
1e3b4 61 6c 69 64 20 3d 20 75 2e 62 64 2e 72 65 73 3d  alid = u.bd.res=
1e3b5 3d 30 20 3f 31 3a 30 3b 0a 20 20 20 20 75 2e 62  =0 ?1:0;.    u.b
1e3b6 64 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20  d.pC->nullRow = 
1e3b7 30 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e  0;.    u.bd.pC->
1e3b8 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1e3b9 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 75  CHE_STALE;.    u
1e3ba 2e 62 64 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bd.pC->deferred
1e3bb 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20  Moveto = 0;.    
1e3bc 69 66 28 20 75 2e 62 64 2e 72 65 73 21 3d 30 20  if( u.bd.res!=0 
1e3bd 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
1e3be 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
1e3bf 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70 43   assert( u.bd.pC
1e3c0 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d  ->rowidIsValid==
1e3c1 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75  0 );.    }.    u
1e3c2 2e 62 64 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75  .bd.pC->seekResu
1e3c3 6c 74 20 3d 20 75 2e 62 64 2e 72 65 73 3b 0a 20  lt = u.bd.res;. 
1e3c4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
1e3c5 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e  his happens when
1e3c6 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f   an attempt to o
1e3c7 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f  pen a read curso
1e3c8 72 20 6f 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  r on the.    ** 
1e3c9 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
1e3ca 62 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ble returns SQLI
1e3cb 54 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f  TE_EMPTY..    */
1e3cc 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70  .    pc = pOp->p
1e3cd 32 20 2d 20 31 3b 0a 20 20 20 20 61 73 73 65 72  2 - 1;.    asser
1e3ce 74 28 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69  t( u.bd.pC->rowi
1e3cf 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20  dIsValid==0 );. 
1e3d0 20 20 20 75 2e 62 64 2e 70 43 2d 3e 73 65 65 6b     u.bd.pC->seek
1e3d1 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a  Result = 0;.  }.
1e3d2 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e3d3 70 63 6f 64 65 3a 20 53 65 71 75 65 6e 63 65 20  pcode: Sequence 
1e3d4 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  P1 P2 * * *.**.*
1e3d5 2a 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20  * Find the next 
1e3d6 61 76 61 69 6c 61 62 6c 65 20 73 65 71 75 65 6e  available sequen
1e3d7 63 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 63 75  ce number for cu
1e3d8 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 57 72 69 74  rsor P1..** Writ
1e3d9 65 20 74 68 65 20 73 65 71 75 65 6e 63 65 20 6e  e the sequence n
1e3da 75 6d 62 65 72 20 69 6e 74 6f 20 72 65 67 69 73  umber into regis
1e3db 74 65 72 20 50 32 2e 0a 2a 2a 20 54 68 65 20 73  ter P2..** The s
1e3dc 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 6f  equence number o
1e3dd 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  n the cursor is 
1e3de 69 6e 63 72 65 6d 65 6e 74 65 64 20 61 66 74 65  incremented afte
1e3df 72 20 74 68 69 73 0a 2a 2a 20 69 6e 73 74 72 75  r this.** instru
1e3e0 63 74 69 6f 6e 2e 20 20 0a 2a 2f 0a 63 61 73 65  ction.  .*/.case
1e3e1 20 4f 50 5f 53 65 71 75 65 6e 63 65 3a 20 7b 20   OP_Sequence: { 
1e3e2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74            /* out
1e3e3 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
1e3e4 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e3e5 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1e3e6 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1e3e7 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72  assert( p->apCsr
1e3e8 5b 70 4f 70 2d 3e 70 31 5d 21 3d 30 20 29 3b 0a  [pOp->p1]!=0 );.
1e3e9 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 70 2d    pOut->u.i = p-
1e3ea 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 2d  >apCsr[pOp->p1]-
1e3eb 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a 20 20 62  >seqCount++;.  b
1e3ec 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63  reak;.}.../* Opc
1e3ed 6f 64 65 3a 20 4e 65 77 52 6f 77 69 64 20 50 31  ode: NewRowid P1
1e3ee 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1e3ef 20 47 65 74 20 61 20 6e 65 77 20 69 6e 74 65 67   Get a new integ
1e3f0 65 72 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  er record number
1e3f1 20 28 61 2e 6b 2e 61 20 22 72 6f 77 69 64 22 29   (a.k.a "rowid")
1e3f2 20 75 73 65 64 20 61 73 20 74 68 65 20 6b 65 79   used as the key
1e3f3 20 74 6f 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20   to a table..** 
1e3f4 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  The record numbe
1e3f5 72 20 69 73 20 6e 6f 74 20 70 72 65 76 69 6f 75  r is not previou
1e3f6 73 6c 79 20 75 73 65 64 20 61 73 20 61 20 6b 65  sly used as a ke
1e3f7 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  y in the databas
1e3f8 65 0a 2a 2a 20 74 61 62 6c 65 20 74 68 61 74 20  e.** table that 
1e3f9 63 75 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73  cursor P1 points
1e3fa 20 74 6f 2e 20 20 54 68 65 20 6e 65 77 20 72 65   to.  The new re
1e3fb 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 77  cord number is w
1e3fc 72 69 74 74 65 6e 0a 2a 2a 20 77 72 69 74 74 65  ritten.** writte
1e3fd 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20 50 32  n to register P2
1e3fe 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 33 3e 30 20  ..**.** If P3>0 
1e3ff 74 68 65 6e 20 50 33 20 69 73 20 61 20 72 65 67  then P3 is a reg
1e400 69 73 74 65 72 20 69 6e 20 74 68 65 20 72 6f 6f  ister in the roo
1e401 74 20 66 72 61 6d 65 20 6f 66 20 74 68 69 73 20  t frame of this 
1e402 56 44 42 45 20 74 68 61 74 20 68 6f 6c 64 73 20  VDBE that holds 
1e403 0a 2a 2a 20 74 68 65 20 6c 61 72 67 65 73 74 20  .** the largest 
1e404 70 72 65 76 69 6f 75 73 6c 79 20 67 65 6e 65 72  previously gener
1e405 61 74 65 64 20 72 65 63 6f 72 64 20 6e 75 6d 62  ated record numb
1e406 65 72 2e 20 4e 6f 20 6e 65 77 20 72 65 63 6f 72  er. No new recor
1e407 64 20 6e 75 6d 62 65 72 73 20 61 72 65 0a 2a 2a  d numbers are.**
1e408 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 6c   allowed to be l
1e409 65 73 73 20 74 68 61 6e 20 74 68 69 73 20 76 61  ess than this va
1e40a 6c 75 65 2e 20 57 68 65 6e 20 74 68 69 73 20 76  lue. When this v
1e40b 61 6c 75 65 20 72 65 61 63 68 65 73 20 69 74 73  alue reaches its
1e40c 20 6d 61 78 69 6d 75 6d 2c 20 0a 2a 2a 20 61 20   maximum, .** a 
1e40d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 65 72 72 6f  SQLITE_FULL erro
1e40e 72 20 69 73 20 67 65 6e 65 72 61 74 65 64 2e 20  r is generated. 
1e40f 54 68 65 20 50 33 20 72 65 67 69 73 74 65 72 20  The P3 register 
1e410 69 73 20 75 70 64 61 74 65 64 20 77 69 74 68 20  is updated with 
1e411 74 68 65 20 27 0a 2a 2a 20 67 65 6e 65 72 61 74  the '.** generat
1e412 65 64 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  ed record number
1e413 2e 20 54 68 69 73 20 50 33 20 6d 65 63 68 61 6e  . This P3 mechan
1e414 69 73 6d 20 69 73 20 75 73 65 64 20 74 6f 20 68  ism is used to h
1e415 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  elp implement th
1e416 65 0a 2a 2a 20 41 55 54 4f 49 4e 43 52 45 4d 45  e.** AUTOINCREME
1e417 4e 54 20 66 65 61 74 75 72 65 2e 0a 2a 2f 0a 63  NT feature..*/.c
1e418 61 73 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 3a  ase OP_NewRowid:
1e419 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20   {           /* 
1e41a 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1e41b 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1e41c 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e41d 65 64 20 69 6e 74 6f 20 75 2e 62 65 20 2a 2f 0a  ed into u.be */.
1e41e 20 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20    i64 v;        
1e41f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1e420 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 56  new rowid */.  V
1e421 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20 20  dbeCursor *pC;  
1e422 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
1e423 6f 66 20 74 61 62 6c 65 20 74 6f 20 67 65 74 20  of table to get 
1e424 74 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f  the new rowid */
1e425 0a 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20  .  int res;     
1e426 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
1e427 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  ult of an sqlite
1e428 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a  3BtreeLast() */.
1e429 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20    int cnt;      
1e42a 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e           /* Coun
1e42b 74 65 72 20 74 6f 20 6c 69 6d 69 74 20 74 68 65  ter to limit the
1e42c 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 61 72 63   number of searc
1e42d 68 65 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  hes */.  Mem *pM
1e42e 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
1e42f 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64  /* Register hold
1e430 69 6e 67 20 6c 61 72 67 65 73 74 20 72 6f 77 69  ing largest rowi
1e431 64 20 66 6f 72 20 41 55 54 4f 49 4e 43 52 45 4d  d for AUTOINCREM
1e432 45 4e 54 20 2a 2f 0a 20 20 56 64 62 65 46 72 61  ENT */.  VdbeFra
1e433 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20  me *pFrame;     
1e434 2f 2a 20 52 6f 6f 74 20 66 72 61 6d 65 20 6f 66  /* Root frame of
1e435 20 56 44 42 45 20 2a 2f 0a 23 65 6e 64 69 66 20   VDBE */.#endif 
1e436 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e437 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e438 62 65 20 2a 2f 0a 0a 20 20 75 2e 62 65 2e 76 20  be */..  u.be.v 
1e439 3d 20 30 3b 0a 20 20 75 2e 62 65 2e 72 65 73 20  = 0;.  u.be.res 
1e43a 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70  = 0;.  assert( p
1e43b 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1e43c 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1e43d 29 3b 0a 20 20 75 2e 62 65 2e 70 43 20 3d 20 70  );.  u.be.pC = p
1e43e 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1e43f 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 65  ;.  assert( u.be
1e440 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  .pC!=0 );.  if( 
1e441 4e 45 56 45 52 28 75 2e 62 65 2e 70 43 2d 3e 70  NEVER(u.be.pC->p
1e442 43 75 72 73 6f 72 3d 3d 30 29 20 29 7b 0a 20 20  Cursor==0) ){.  
1e443 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f 20 69 6e    /* The zero in
1e444 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61 62 6f  itialization abo
1e445 76 65 20 69 73 20 61 6c 6c 20 74 68 61 74 20 69  ve is all that i
1e446 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 7d 65  s needed */.  }e
1e447 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  lse{.    /* The 
1e448 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 20 72 65  next rowid or re
1e449 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 64 69 66  cord number (dif
1e44a 66 65 72 65 6e 74 20 74 65 72 6d 73 20 66 6f 72  ferent terms for
1e44b 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 2a 2a   the same.    **
1e44c 20 74 68 69 6e 67 29 20 69 73 20 6f 62 74 61 69   thing) is obtai
1e44d 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d 73 74 65  ned in a two-ste
1e44e 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20  p algorithm..   
1e44f 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 72 73 74   **.    ** First
1e450 20 77 65 20 61 74 74 65 6d 70 74 20 74 6f 20 66   we attempt to f
1e451 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73 74 20  ind the largest 
1e452 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20 61  existing rowid a
1e453 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 20 20 2a  nd add one.    *
1e454 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 75 74 20  * to that.  But 
1e455 69 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 65  if the largest e
1e456 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20 69 73  xisting rowid is
1e457 20 61 6c 72 65 61 64 79 20 74 68 65 20 6d 61 78   already the max
1e458 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 6f 73 69  imum.    ** posi
1e459 74 69 76 65 20 69 6e 74 65 67 65 72 2c 20 77 65  tive integer, we
1e45a 20 68 61 76 65 20 74 6f 20 66 61 6c 6c 20 74 68   have to fall th
1e45b 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73 65 63  rough to the sec
1e45c 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 6f 62 61  ond.    ** proba
1e45d 62 69 6c 69 73 74 69 63 20 61 6c 67 6f 72 69 74  bilistic algorit
1e45e 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  hm.    **.    **
1e45f 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6c 67 6f   The second algo
1e460 72 69 74 68 6d 20 69 73 20 74 6f 20 73 65 6c 65  rithm is to sele
1e461 63 74 20 61 20 72 6f 77 69 64 20 61 74 20 72 61  ct a rowid at ra
1e462 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 69 66 0a  ndom and see if.
1e463 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 65 61 64      ** it alread
1e464 79 20 65 78 69 73 74 73 20 69 6e 20 74 68 65 20  y exists in the 
1e465 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20 64 6f  table.  If it do
1e466 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 77 65  es not exist, we
1e467 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 73 75 63   have.    ** suc
1e468 63 65 65 64 65 64 2e 20 20 49 66 20 74 68 65 20  ceeded.  If the 
1e469 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 64 6f 65  random rowid doe
1e46a 73 20 65 78 69 73 74 2c 20 77 65 20 73 65 6c 65  s exist, we sele
1e46b 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a 20 20 20  ct a new one.   
1e46c 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 67 61 69   ** and try agai
1e46d 6e 2c 20 75 70 20 74 6f 20 31 30 30 20 74 69 6d  n, up to 100 tim
1e46e 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  es..    */.    a
1e46f 73 73 65 72 74 28 20 75 2e 62 65 2e 70 43 2d 3e  ssert( u.be.pC->
1e470 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 75  isTable );.    u
1e471 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a 0a 23 69  .be.cnt = 0;..#i
1e472 66 64 65 66 20 53 51 4c 49 54 45 5f 33 32 42 49  fdef SQLITE_32BI
1e473 54 5f 52 4f 57 49 44 0a 23 20 20 20 64 65 66 69  T_ROWID.#   defi
1e474 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20 30 78 37  ne MAX_ROWID 0x7
1e475 66 66 66 66 66 66 66 0a 23 65 6c 73 65 0a 20 20  fffffff.#else.  
1e476 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 70 69 6c    /* Some compil
1e477 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 61 62 6f  ers complain abo
1e478 75 74 20 63 6f 6e 73 74 61 6e 74 73 20 6f 66 20  ut constants of 
1e479 74 68 65 20 66 6f 72 6d 20 30 78 37 66 66 66 66  the form 0x7ffff
1e47a 66 66 66 66 66 66 66 66 66 66 66 2e 0a 20 20 20  fffffffffff..   
1e47b 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f 6d 70 6c   ** Others compl
1e47c 61 69 6e 20 61 62 6f 75 74 20 30 78 37 66 66 66  ain about 0x7fff
1e47d 66 66 66 66 66 66 66 66 66 66 66 66 66 4c 4c 2e  fffffffffffffLL.
1e47e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
1e47f 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20 20 20 20  macro seems.    
1e480 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68  ** to provide th
1e481 65 20 63 6f 6e 73 74 61 6e 74 20 77 68 69 6c 65  e constant while
1e482 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63 6f 6d 70   making all comp
1e483 69 6c 65 72 73 20 68 61 70 70 79 2e 0a 20 20 20  ilers happy..   
1e484 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e 65 20 4d   */.#   define M
1e485 41 58 5f 52 4f 57 49 44 20 20 28 69 36 34 29 28  AX_ROWID  (i64)(
1e486 20 28 28 28 75 36 34 29 30 78 37 66 66 66 66 66   (((u64)0x7fffff
1e487 66 66 29 3c 3c 33 32 29 20 7c 20 28 75 36 34 29  ff)<<32) | (u64)
1e488 30 78 66 66 66 66 66 66 66 66 20 29 0a 23 65 6e  0xffffffff ).#en
1e489 64 69 66 0a 0a 20 20 20 20 69 66 28 20 21 75 2e  dif..    if( !u.
1e48a 62 65 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d  be.pC->useRandom
1e48b 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 75  Rowid ){.      u
1e48c 2e 62 65 2e 76 20 3d 20 73 71 6c 69 74 65 33 42  .be.v = sqlite3B
1e48d 74 72 65 65 47 65 74 43 61 63 68 65 64 52 6f 77  treeGetCachedRow
1e48e 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72  id(u.be.pC->pCur
1e48f 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  sor);.      if( 
1e490 75 2e 62 65 2e 76 3d 3d 30 20 29 7b 0a 20 20 20  u.be.v==0 ){.   
1e491 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1e492 33 42 74 72 65 65 4c 61 73 74 28 75 2e 62 65 2e  3BtreeLast(u.be.
1e493 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e  pC->pCursor, &u.
1e494 62 65 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20  be.res);.       
1e495 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1e496 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
1e497 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1e498 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  o_error;.       
1e499 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 75   }.        if( u
1e49a 2e 62 65 2e 72 65 73 20 29 7b 0a 20 20 20 20 20  .be.res ){.     
1e49b 20 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 31 3b       u.be.v = 1;
1e49c 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1e49d 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1e49e 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  ( sqlite3BtreeCu
1e49f 72 73 6f 72 49 73 56 61 6c 69 64 28 75 2e 62 65  rsorIsValid(u.be
1e4a0 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 20 29 3b  .pC->pCursor) );
1e4a1 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
1e4a2 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79 53  sqlite3BtreeKeyS
1e4a3 69 7a 65 28 75 2e 62 65 2e 70 43 2d 3e 70 43 75  ize(u.be.pC->pCu
1e4a4 72 73 6f 72 2c 20 26 75 2e 62 65 2e 76 29 3b 0a  rsor, &u.be.v);.
1e4a5 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1e4a6 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1e4a7 29 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 66  );   /* Cannot f
1e4a8 61 69 6c 20 66 6f 6c 6c 6f 77 69 6e 67 20 42 74  ail following Bt
1e4a9 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a 20 20 20  reeLast() */.   
1e4aa 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e         if( u.be.
1e4ab 76 3d 3d 4d 41 58 5f 52 4f 57 49 44 20 29 7b 0a  v==MAX_ROWID ){.
1e4ac 20 20 20 20 20 20 20 20 20 20 20 20 75 2e 62 65              u.be
1e4ad 2e 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f  .pC->useRandomRo
1e4ae 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  wid = 1;.       
1e4af 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1e4b0 20 20 20 20 20 20 75 2e 62 65 2e 76 2b 2b 3b 0a        u.be.v++;.
1e4b1 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1e4b2 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 23      }.      }..#
1e4b3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1e4b4 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
1e4b5 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  .      if( pOp->
1e4b6 70 33 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  p3 ){.        /*
1e4b7 20 41 73 73 65 72 74 20 74 68 61 74 20 50 33 20   Assert that P3 
1e4b8 69 73 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72  is a valid memor
1e4b9 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20  y cell. */.     
1e4ba 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1e4bb 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20  p3>0 );.        
1e4bc 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29 7b  if( p->pFrame ){
1e4bd 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 75  .          for(u
1e4be 2e 62 65 2e 70 46 72 61 6d 65 3d 70 2d 3e 70 46  .be.pFrame=p->pF
1e4bf 72 61 6d 65 3b 20 75 2e 62 65 2e 70 46 72 61 6d  rame; u.be.pFram
1e4c0 65 2d 3e 70 50 61 72 65 6e 74 3b 20 75 2e 62 65  e->pParent; u.be
1e4c1 2e 70 46 72 61 6d 65 3d 75 2e 62 65 2e 70 46 72  .pFrame=u.be.pFr
1e4c2 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20  ame->pParent);. 
1e4c3 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65           /* Asse
1e4c4 72 74 20 74 68 61 74 20 50 33 20 69 73 20 61 20  rt that P3 is a 
1e4c5 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c  valid memory cel
1e4c6 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  l. */.          
1e4c7 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c  assert( pOp->p3<
1e4c8 3d 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 6e 4d  =u.be.pFrame->nM
1e4c9 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  em );.          
1e4ca 75 2e 62 65 2e 70 4d 65 6d 20 3d 20 26 75 2e 62  u.be.pMem = &u.b
1e4cb 65 2e 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70  e.pFrame->aMem[p
1e4cc 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20  Op->p3];.       
1e4cd 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1e4ce 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61 74    /* Assert that
1e4cf 20 50 33 20 69 73 20 61 20 76 61 6c 69 64 20 6d   P3 is a valid m
1e4d0 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a 20  emory cell. */. 
1e4d1 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
1e4d2 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65   pOp->p3<=p->nMe
1e4d3 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 75  m );.          u
1e4d4 2e 62 65 2e 70 4d 65 6d 20 3d 20 26 61 4d 65 6d  .be.pMem = &aMem
1e4d5 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20  [pOp->p3];.     
1e4d6 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 52 45     }..        RE
1e4d7 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1e4d8 2d 3e 70 33 2c 20 75 2e 62 65 2e 70 4d 65 6d 29  ->p3, u.be.pMem)
1e4d9 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1e4da 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
1e4db 66 79 28 75 2e 62 65 2e 70 4d 65 6d 29 3b 0a 20  fy(u.be.pMem);. 
1e4dc 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28         assert( (
1e4dd 75 2e 62 65 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73  u.be.pMem->flags
1e4de 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29   & MEM_Int)!=0 )
1e4df 3b 20 20 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f  ;  /* mem(P3) ho
1e4e0 6c 64 73 20 61 6e 20 69 6e 74 65 67 65 72 20 2a  lds an integer *
1e4e1 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 2e  /.        if( u.
1e4e2 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 3d 3d 4d 41  be.pMem->u.i==MA
1e4e3 58 5f 52 4f 57 49 44 20 7c 7c 20 75 2e 62 65 2e  X_ROWID || u.be.
1e4e4 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77  pC->useRandomRow
1e4e5 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  id ){.          
1e4e6 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c  rc = SQLITE_FULL
1e4e7 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
1e4e8 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1e4e9 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ror;.        }. 
1e4ea 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e         if( u.be.
1e4eb 76 3c 75 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69  v<u.be.pMem->u.i
1e4ec 2b 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  +1 ){.          
1e4ed 75 2e 62 65 2e 76 20 3d 20 75 2e 62 65 2e 70 4d  u.be.v = u.be.pM
1e4ee 65 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20  em->u.i + 1;.   
1e4ef 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 75       }.        u
1e4f0 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20  .be.pMem->u.i = 
1e4f1 75 2e 62 65 2e 76 3b 0a 20 20 20 20 20 20 7d 0a  u.be.v;.      }.
1e4f2 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20 73 71  #endif..      sq
1e4f3 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
1e4f4 68 65 64 52 6f 77 69 64 28 75 2e 62 65 2e 70 43  hedRowid(u.be.pC
1e4f5 2d 3e 70 43 75 72 73 6f 72 2c 20 75 2e 62 65 2e  ->pCursor, u.be.
1e4f6 76 3c 4d 41 58 5f 52 4f 57 49 44 20 3f 20 75 2e  v<MAX_ROWID ? u.
1e4f7 62 65 2e 76 2b 31 20 3a 20 30 29 3b 0a 20 20 20  be.v+1 : 0);.   
1e4f8 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 62 65 2e   }.    if( u.be.
1e4f9 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77  pC->useRandomRow
1e4fa 69 64 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  id ){.      asse
1e4fb 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29  rt( pOp->p3==0 )
1e4fc 3b 20 20 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20  ;  /* We cannot 
1e4fd 62 65 20 69 6e 20 72 61 6e 64 6f 6d 20 72 6f 77  be in random row
1e4fe 69 64 20 6d 6f 64 65 20 69 66 20 74 68 69 73 20  id mode if this 
1e4ff 69 73 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  is.             
1e500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e501 2a 2a 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d  ** an AUTOINCREM
1e502 45 4e 54 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20  ENT table. */.  
1e503 20 20 20 20 75 2e 62 65 2e 76 20 3d 20 64 62 2d      u.be.v = db-
1e504 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 20 20  >lastRowid;.    
1e505 20 20 75 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a    u.be.cnt = 0;.
1e506 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20        do{.      
1e507 20 20 69 66 28 20 75 2e 62 65 2e 63 6e 74 3d 3d    if( u.be.cnt==
1e508 30 20 26 26 20 28 75 2e 62 65 2e 76 26 30 78 66  0 && (u.be.v&0xf
1e509 66 66 66 66 66 29 3d 3d 75 2e 62 65 2e 76 20 29  fffff)==u.be.v )
1e50a 7b 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62 65  {.          u.be
1e50b 2e 76 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65  .v++;.        }e
1e50c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73  lse{.          s
1e50d 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
1e50e 73 28 73 69 7a 65 6f 66 28 75 2e 62 65 2e 76 29  s(sizeof(u.be.v)
1e50f 2c 20 26 75 2e 62 65 2e 76 29 3b 0a 20 20 20 20  , &u.be.v);.    
1e510 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 63        if( u.be.c
1e511 6e 74 3c 35 20 29 20 75 2e 62 65 2e 76 20 26 3d  nt<5 ) u.be.v &=
1e512 20 30 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20   0xffffff;.     
1e513 20 20 20 7d 0a 20 20 20 20 20 20 20 20 72 63 20     }.        rc 
1e514 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
1e515 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62  vetoUnpacked(u.b
1e516 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30  e.pC->pCursor, 0
1e517 2c 20 28 75 36 34 29 75 2e 62 65 2e 76 2c 20 30  , (u64)u.be.v, 0
1e518 2c 20 26 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20  , &u.be.res);.  
1e519 20 20 20 20 20 20 75 2e 62 65 2e 63 6e 74 2b 2b        u.be.cnt++
1e51a 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20  ;.      }while( 
1e51b 75 2e 62 65 2e 63 6e 74 3c 31 30 30 20 26 26 20  u.be.cnt<100 && 
1e51c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1e51d 20 75 2e 62 65 2e 72 65 73 3d 3d 30 20 29 3b 0a   u.be.res==0 );.
1e51e 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1e51f 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e  LITE_OK && u.be.
1e520 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  res==0 ){.      
1e521 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55    rc = SQLITE_FU
1e522 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  LL;.        goto
1e523 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1e524 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ror;.      }.   
1e525 20 7d 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e   }.    u.be.pC->
1e526 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
1e527 3b 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 64  ;.    u.be.pC->d
1e528 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20  eferredMoveto = 
1e529 30 3b 0a 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e  0;.    u.be.pC->
1e52a 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1e52b 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20  CHE_STALE;.  }. 
1e52c 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62   pOut->u.i = u.b
1e52d 65 2e 76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  e.v;.  break;.}.
1e52e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65  ./* Opcode: Inse
1e52f 72 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50  rt P1 P2 P3 P4 P
1e530 35 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e  5.**.** Write an
1e531 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20   entry into the 
1e532 74 61 62 6c 65 20 6f 66 20 63 75 72 73 6f 72 20  table of cursor 
1e533 50 31 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79  P1.  A new entry
1e534 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 69   is.** created i
1e535 66 20 69 74 20 64 6f 65 73 6e 27 74 20 61 6c 72  f it doesn't alr
1e536 65 61 64 79 20 65 78 69 73 74 20 6f 72 20 74 68  eady exist or th
1e537 65 20 64 61 74 61 20 66 6f 72 20 61 6e 20 65 78  e data for an ex
1e538 69 73 74 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20  isting.** entry 
1e539 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20  is overwritten. 
1e53a 20 54 68 65 20 64 61 74 61 20 69 73 20 74 68 65   The data is the
1e53b 20 76 61 6c 75 65 20 4d 45 4d 5f 42 6c 6f 62 20   value MEM_Blob 
1e53c 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1e53d 65 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e  er.** number P2.
1e53e 20 54 68 65 20 6b 65 79 20 69 73 20 73 74 6f 72   The key is stor
1e53f 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ed in register P
1e540 33 2e 20 54 68 65 20 6b 65 79 20 6d 75 73 74 0a  3. The key must.
1e541 2a 2a 20 62 65 20 61 20 4d 45 4d 5f 49 6e 74 2e  ** be a MEM_Int.
1e542 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50  .**.** If the OP
1e543 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61  FLAG_NCHANGE fla
1e544 67 20 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20  g of P5 is set, 
1e545 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61  then the row cha
1e546 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20  nge count is.** 
1e547 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68  incremented (oth
1e548 65 72 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66  erwise not).  If
1e549 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54   the OPFLAG_LAST
1e54a 52 4f 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35  ROWID flag of P5
1e54b 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e   is set,.** then
1e54c 20 72 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64   rowid is stored
1e54d 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20   for subsequent 
1e54e 72 65 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a  return by the.**
1e54f 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e   sqlite3_last_in
1e550 73 65 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e  sert_rowid() fun
1e551 63 74 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65  ction (otherwise
1e552 20 69 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65   it is unmodifie
1e553 64 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  d)..**.** If the
1e554 20 4f 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52   OPFLAG_USESEEKR
1e555 45 53 55 4c 54 20 66 6c 61 67 20 6f 66 20 50 35  ESULT flag of P5
1e556 20 69 73 20 73 65 74 20 61 6e 64 20 69 66 20 74   is set and if t
1e557 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20  he result of.** 
1e558 74 68 65 20 6c 61 73 74 20 73 65 65 6b 20 6f 70  the last seek op
1e559 65 72 61 74 69 6f 6e 20 28 4f 50 5f 4e 6f 74 45  eration (OP_NotE
1e55a 78 69 73 74 73 29 20 77 61 73 20 61 20 73 75 63  xists) was a suc
1e55b 63 65 73 73 2c 20 74 68 65 6e 20 74 68 69 73 0a  cess, then this.
1e55c 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c  ** operation wil
1e55d 6c 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f  l not attempt to
1e55e 20 66 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70   find the approp
1e55f 72 69 61 74 65 20 72 6f 77 20 62 65 66 6f 72 65  riate row before
1e560 20 64 6f 69 6e 67 0a 2a 2a 20 74 68 65 20 69 6e   doing.** the in
1e561 73 65 72 74 20 62 75 74 20 77 69 6c 6c 20 69 6e  sert but will in
1e562 73 74 65 61 64 20 6f 76 65 72 77 72 69 74 65 20  stead overwrite 
1e563 74 68 65 20 72 6f 77 20 74 68 61 74 20 74 68 65  the row that the
1e564 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20 63 75   cursor is.** cu
1e565 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
1e566 20 74 6f 2e 20 20 50 72 65 73 75 6d 61 62 6c 79   to.  Presumably
1e567 2c 20 74 68 65 20 70 72 69 6f 72 20 4f 50 5f 4e  , the prior OP_N
1e568 6f 74 45 78 69 73 74 73 20 6f 70 63 6f 64 65 0a  otExists opcode.
1e569 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 70  ** has already p
1e56a 6f 73 69 74 69 6f 6e 65 64 20 74 68 65 20 63 75  ositioned the cu
1e56b 72 73 6f 72 20 63 6f 72 72 65 63 74 6c 79 2e 20  rsor correctly. 
1e56c 20 54 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69   This is an opti
1e56d 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74  mization.** that
1e56e 20 62 6f 6f 73 74 73 20 70 65 72 66 6f 72 6d 61   boosts performa
1e56f 6e 63 65 20 62 79 20 61 76 6f 69 64 69 6e 67 20  nce by avoiding 
1e570 72 65 64 75 6e 64 61 6e 74 20 73 65 65 6b 73 2e  redundant seeks.
1e571 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50  .**.** If the OP
1e572 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20 66 6c  FLAG_ISUPDATE fl
1e573 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  ag is set, then 
1e574 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 70  this opcode is p
1e575 61 72 74 20 6f 66 20 61 6e 0a 2a 2a 20 55 50 44  art of an.** UPD
1e576 41 54 45 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  ATE operation.  
1e577 4f 74 68 65 72 77 69 73 65 20 28 69 66 20 74 68  Otherwise (if th
1e578 65 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 29  e flag is clear)
1e579 20 74 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64   then this opcod
1e57a 65 0a 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20  e.** is part of 
1e57b 61 6e 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74  an INSERT operat
1e57c 69 6f 6e 2e 20 20 54 68 65 20 64 69 66 66 65 72  ion.  The differ
1e57d 65 6e 63 65 20 69 73 20 6f 6e 6c 79 20 69 6d 70  ence is only imp
1e57e 6f 72 74 61 6e 74 20 74 6f 0a 2a 2a 20 74 68 65  ortant to.** the
1e57f 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a   update hook..**
1e580 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 50 34  .** Parameter P4
1e581 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 20   may point to a 
1e582 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e  string containin
1e583 67 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65  g the table-name
1e584 2c 20 6f 72 0a 2a 2a 20 6d 61 79 20 62 65 20 4e  , or.** may be N
1e585 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f  ULL. If it is no
1e586 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65  t NULL, then the
1e587 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 0a 2a 2a   update-hook .**
1e588 20 28 73 71 6c 69 74 65 33 2e 78 55 70 64 61 74   (sqlite3.xUpdat
1e589 65 43 61 6c 6c 62 61 63 6b 29 20 69 73 20 69 6e  eCallback) is in
1e58a 76 6f 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20  voked following 
1e58b 61 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73  a successful ins
1e58c 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 41 52 4e  ert..**.** (WARN
1e58d 49 4e 47 2f 54 4f 44 4f 3a 20 49 66 20 50 31 20  ING/TODO: If P1 
1e58e 69 73 20 61 20 70 73 65 75 64 6f 2d 63 75 72 73  is a pseudo-curs
1e58f 6f 72 20 61 6e 64 20 50 32 20 69 73 20 64 79 6e  or and P2 is dyn
1e590 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f  amically.** allo
1e591 63 61 74 65 64 2c 20 74 68 65 6e 20 6f 77 6e 65  cated, then owne
1e592 72 73 68 69 70 20 6f 66 20 50 32 20 69 73 20 74  rship of P2 is t
1e593 72 61 6e 73 66 65 72 72 65 64 20 74 6f 20 74 68  ransferred to th
1e594 65 20 70 73 65 75 64 6f 2d 63 75 72 73 6f 72 0a  e pseudo-cursor.
1e595 2a 2a 20 61 6e 64 20 72 65 67 69 73 74 65 72 20  ** and register 
1e596 50 32 20 62 65 63 6f 6d 65 73 20 65 70 68 65 6d  P2 becomes ephem
1e597 65 72 61 6c 2e 20 20 49 66 20 74 68 65 20 63 75  eral.  If the cu
1e598 72 73 6f 72 20 69 73 20 63 68 61 6e 67 65 64 2c  rsor is changed,
1e599 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66   the.** value of
1e59a 20 72 65 67 69 73 74 65 72 20 50 32 20 77 69 6c   register P2 wil
1e59b 6c 20 74 68 65 6e 20 63 68 61 6e 67 65 2e 20 20  l then change.  
1e59c 4d 61 6b 65 20 73 75 72 65 20 74 68 69 73 20 64  Make sure this d
1e59d 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 61 75 73 65  oes not.** cause
1e59e 20 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 29 0a   any problems.).
1e59f 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72  **.** This instr
1e5a0 75 63 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b  uction only work
1e5a1 73 20 6f 6e 20 74 61 62 6c 65 73 2e 20 20 54 68  s on tables.  Th
1e5a2 65 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73  e equivalent ins
1e5a3 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20  truction.** for 
1e5a4 69 6e 64 69 63 65 73 20 69 73 20 4f 50 5f 49 64  indices is OP_Id
1e5a5 78 49 6e 73 65 72 74 2e 0a 2a 2f 0a 2f 2a 20 4f  xInsert..*/./* O
1e5a6 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 49 6e 74  pcode: InsertInt
1e5a7 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
1e5a8 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73  **.** This works
1e5a9 20 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 4f 50   exactly like OP
1e5aa 5f 49 6e 73 65 72 74 20 65 78 63 65 70 74 20 74  _Insert except t
1e5ab 68 61 74 20 74 68 65 20 6b 65 79 20 69 73 20 74  hat the key is t
1e5ac 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61  he.** integer va
1e5ad 6c 75 65 20 50 33 2c 20 6e 6f 74 20 74 68 65 20  lue P3, not the 
1e5ae 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74  value of the int
1e5af 65 67 65 72 20 73 74 6f 72 65 64 20 69 6e 20 72  eger stored in r
1e5b0 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63  egister P3..*/.c
1e5b1 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 0a  ase OP_Insert: .
1e5b2 63 61 73 65 20 4f 50 5f 49 6e 73 65 72 74 49 6e  case OP_InsertIn
1e5b3 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  t: {.#if 0  /* l
1e5b4 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e5b5 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 66 20 2a  oved into u.bf *
1e5b6 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b 20  /.  Mem *pData; 
1e5b7 20 20 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c        /* MEM cel
1e5b8 6c 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 66  l holding data f
1e5b9 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  or the record to
1e5ba 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
1e5bb 20 20 4d 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20    Mem *pKey;    
1e5bc 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20      /* MEM cell 
1e5bd 68 6f 6c 64 69 6e 67 20 6b 65 79 20 20 66 6f 72  holding key  for
1e5be 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1e5bf 20 69 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20   i64 iKey;      
1e5c0 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65     /* The intege
1e5c1 72 20 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66  r ROWID or key f
1e5c2 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  or the record to
1e5c3 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
1e5c4 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1e5c5 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f  ;   /* Cursor to
1e5c6 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
1e5c7 68 20 69 6e 73 65 72 74 20 69 73 20 77 72 69 74  h insert is writ
1e5c8 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65  ten */.  int nZe
1e5c9 72 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ro;        /* Nu
1e5ca 6d 62 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79 74  mber of zero-byt
1e5cb 65 73 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f 0a  es to append */.
1e5cc 20 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74    int seekResult
1e5cd 3b 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66  ;   /* Result of
1e5ce 20 70 72 69 6f 72 20 73 65 65 6b 20 6f 72 20 30   prior seek or 0
1e5cf 20 69 66 20 6e 6f 20 55 53 45 53 45 45 4b 52 45   if no USESEEKRE
1e5d0 53 55 4c 54 20 66 6c 61 67 20 2a 2f 0a 20 20 63  SULT flag */.  c
1e5d1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20  onst char *zDb; 
1e5d2 20 2f 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d   /* database nam
1e5d3 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20  e - used by the 
1e5d4 75 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20  update hook */. 
1e5d5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62   const char *zTb
1e5d6 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65  l; /* Table name
1e5d7 20 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 6f   - used by the o
1e5d8 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20  pdate hook */.  
1e5d9 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20  int op;         
1e5da 20 20 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72 20    /* Opcode for 
1e5db 75 70 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51 4c  update hook: SQL
1e5dc 49 54 45 5f 55 50 44 41 54 45 20 6f 72 20 53 51  ITE_UPDATE or SQ
1e5dd 4c 49 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a 23  LITE_INSERT */.#
1e5de 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1e5df 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e5e0 6e 74 6f 20 75 2e 62 66 20 2a 2f 0a 0a 20 20 75  nto u.bf */..  u
1e5e1 2e 62 66 2e 70 44 61 74 61 20 3d 20 26 61 4d 65  .bf.pData = &aMe
1e5e2 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73  m[pOp->p2];.  as
1e5e3 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e5e4 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e5e5 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 66  Cursor );.  u.bf
1e5e6 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1e5e7 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1e5e8 74 28 20 75 2e 62 66 2e 70 43 21 3d 30 20 29 3b  t( u.bf.pC!=0 );
1e5e9 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e  .  assert( u.bf.
1e5ea 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29  pC->pCursor!=0 )
1e5eb 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 66  ;.  assert( u.bf
1e5ec 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65  .pC->pseudoTable
1e5ed 52 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Reg==0 );.  asse
1e5ee 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e 69 73 54  rt( u.bf.pC->isT
1e5ef 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49 53 54  able );.  REGIST
1e5f0 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32  ER_TRACE(pOp->p2
1e5f1 2c 20 75 2e 62 66 2e 70 44 61 74 61 29 3b 0a 0a  , u.bf.pData);..
1e5f2 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64    if( pOp->opcod
1e5f3 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 20 29 7b 0a  e==OP_Insert ){.
1e5f4 20 20 20 20 75 2e 62 66 2e 70 4b 65 79 20 3d 20      u.bf.pKey = 
1e5f5 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
1e5f6 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66      assert( u.bf
1e5f7 2e 70 4b 65 79 2d 3e 66 6c 61 67 73 20 26 20 4d  .pKey->flags & M
1e5f8 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 52 45  EM_Int );.    RE
1e5f9 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1e5fa 2d 3e 70 33 2c 20 75 2e 62 66 2e 70 4b 65 79 29  ->p3, u.bf.pKey)
1e5fb 3b 0a 20 20 20 20 75 2e 62 66 2e 69 4b 65 79 20  ;.    u.bf.iKey 
1e5fc 3d 20 75 2e 62 66 2e 70 4b 65 79 2d 3e 75 2e 69  = u.bf.pKey->u.i
1e5fd 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
1e5fe 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f  ssert( pOp->opco
1e5ff 64 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 49 6e 74  de==OP_InsertInt
1e600 20 29 3b 0a 20 20 20 20 75 2e 62 66 2e 69 4b 65   );.    u.bf.iKe
1e601 79 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 7d  y = pOp->p3;.  }
1e602 0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20  ..  if( pOp->p5 
1e603 26 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  & OPFLAG_NCHANGE
1e604 20 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b   ) p->nChange++;
1e605 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26  .  if( pOp->p5 &
1e606 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49   OPFLAG_LASTROWI
1e607 44 20 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69  D ) db->lastRowi
1e608 64 20 3d 20 75 2e 62 66 2e 69 4b 65 79 3b 0a 20  d = u.bf.iKey;. 
1e609 20 69 66 28 20 75 2e 62 66 2e 70 44 61 74 61 2d   if( u.bf.pData-
1e60a 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
1e60b 6c 20 29 7b 0a 20 20 20 20 75 2e 62 66 2e 70 44  l ){.    u.bf.pD
1e60c 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20  ata->z = 0;.    
1e60d 75 2e 62 66 2e 70 44 61 74 61 2d 3e 6e 20 3d 20  u.bf.pData->n = 
1e60e 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
1e60f 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 44 61  assert( u.bf.pDa
1e610 74 61 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  ta->flags & (MEM
1e611 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 20 29  _Blob|MEM_Str) )
1e612 3b 0a 20 20 7d 0a 20 20 75 2e 62 66 2e 73 65 65  ;.  }.  u.bf.see
1e613 6b 52 65 73 75 6c 74 20 3d 20 28 28 70 4f 70 2d  kResult = ((pOp-
1e614 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45  >p5 & OPFLAG_USE
1e615 53 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 75 2e  SEEKRESULT) ? u.
1e616 62 66 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c  bf.pC->seekResul
1e617 74 20 3a 20 30 29 3b 0a 20 20 69 66 28 20 75 2e  t : 0);.  if( u.
1e618 62 66 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20  bf.pData->flags 
1e619 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
1e61a 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d 20 75    u.bf.nZero = u
1e61b 2e 62 66 2e 70 44 61 74 61 2d 3e 75 2e 6e 5a 65  .bf.pData->u.nZe
1e61c 72 6f 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ro;.  }else{.   
1e61d 20 75 2e 62 66 2e 6e 5a 65 72 6f 20 3d 20 30 3b   u.bf.nZero = 0;
1e61e 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
1e61f 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77 69  reeSetCachedRowi
1e620 64 28 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73  d(u.bf.pC->pCurs
1e621 6f 72 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20 73  or, 0);.  rc = s
1e622 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72  qlite3BtreeInser
1e623 74 28 75 2e 62 66 2e 70 43 2d 3e 70 43 75 72 73  t(u.bf.pC->pCurs
1e624 6f 72 2c 20 30 2c 20 75 2e 62 66 2e 69 4b 65 79  or, 0, u.bf.iKey
1e625 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1e626 20 20 20 20 20 20 20 20 20 20 20 20 75 2e 62 66              u.bf
1e627 2e 70 44 61 74 61 2d 3e 7a 2c 20 75 2e 62 66 2e  .pData->z, u.bf.
1e628 70 44 61 74 61 2d 3e 6e 2c 20 75 2e 62 66 2e 6e  pData->n, u.bf.n
1e629 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20 20 20 20  Zero,.          
1e62a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e62b 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47  pOp->p5 & OPFLAG
1e62c 5f 41 50 50 45 4e 44 2c 20 75 2e 62 66 2e 73 65  _APPEND, u.bf.se
1e62d 65 6b 52 65 73 75 6c 74 0a 20 20 29 3b 0a 20 20  ekResult.  );.  
1e62e 75 2e 62 66 2e 70 43 2d 3e 72 6f 77 69 64 49 73  u.bf.pC->rowidIs
1e62f 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 75 2e 62  Valid = 0;.  u.b
1e630 66 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  f.pC->deferredMo
1e631 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e 62 66  veto = 0;.  u.bf
1e632 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73  .pC->cacheStatus
1e633 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
1e634 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65  .  /* Invoke the
1e635 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20   update-hook if 
1e636 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69  required. */.  i
1e637 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1e638 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43   && db->xUpdateC
1e639 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e  allback && pOp->
1e63a 70 34 2e 7a 20 29 7b 0a 20 20 20 20 75 2e 62 66  p4.z ){.    u.bf
1e63b 2e 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 75  .zDb = db->aDb[u
1e63c 2e 62 66 2e 70 43 2d 3e 69 44 62 5d 2e 7a 4e 61  .bf.pC->iDb].zNa
1e63d 6d 65 3b 0a 20 20 20 20 75 2e 62 66 2e 7a 54 62  me;.    u.bf.zTb
1e63e 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20  l = pOp->p4.z;. 
1e63f 20 20 20 75 2e 62 66 2e 6f 70 20 3d 20 28 28 70     u.bf.op = ((p
1e640 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f  Op->p5 & OPFLAG_
1e641 49 53 55 50 44 41 54 45 29 20 3f 20 53 51 4c 49  ISUPDATE) ? SQLI
1e642 54 45 5f 55 50 44 41 54 45 20 3a 20 53 51 4c 49  TE_UPDATE : SQLI
1e643 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20 20 20 20  TE_INSERT);.    
1e644 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70 43 2d  assert( u.bf.pC-
1e645 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  >isTable );.    
1e646 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62  db->xUpdateCallb
1e647 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41  ack(db->pUpdateA
1e648 72 67 2c 20 75 2e 62 66 2e 6f 70 2c 20 75 2e 62  rg, u.bf.op, u.b
1e649 66 2e 7a 44 62 2c 20 75 2e 62 66 2e 7a 54 62 6c  f.zDb, u.bf.zTbl
1e64a 2c 20 75 2e 62 66 2e 69 4b 65 79 29 3b 0a 20 20  , u.bf.iKey);.  
1e64b 20 20 61 73 73 65 72 74 28 20 75 2e 62 66 2e 70    assert( u.bf.p
1e64c 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d  C->iDb>=0 );.  }
1e64d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1e64e 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74 65 20 50  Opcode: Delete P
1e64f 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  1 P2 * P4 *.**.*
1e650 2a 20 44 65 6c 65 74 65 20 74 68 65 20 72 65 63  * Delete the rec
1e651 6f 72 64 20 61 74 20 77 68 69 63 68 20 74 68 65  ord at which the
1e652 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 63 75   P1 cursor is cu
1e653 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
1e654 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73  ..**.** The curs
1e655 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20  or will be left 
1e656 70 6f 69 6e 74 69 6e 67 20 61 74 20 65 69 74 68  pointing at eith
1e657 65 72 20 74 68 65 20 6e 65 78 74 20 6f 72 20 74  er the next or t
1e658 68 65 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20 72  he previous.** r
1e659 65 63 6f 72 64 20 69 6e 20 74 68 65 20 74 61 62  ecord in the tab
1e65a 6c 65 2e 20 49 66 20 69 74 20 69 73 20 6c 65 66  le. If it is lef
1e65b 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74 68  t pointing at th
1e65c 65 20 6e 65 78 74 20 72 65 63 6f 72 64 2c 20 74  e next record, t
1e65d 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 78 74 20  hen.** the next 
1e65e 4e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  Next instruction
1e65f 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f 2d 6f 70   will be a no-op
1e660 2e 20 20 48 65 6e 63 65 20 69 74 20 69 73 20 4f  .  Hence it is O
1e661 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a 2a 20 61  K to delete.** a
1e662 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 77 69 74   record from wit
1e663 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c 6f 6f 70  hin an Next loop
1e664 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f  ..**.** If the O
1e665 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c  PFLAG_NCHANGE fl
1e666 61 67 20 6f 66 20 50 32 20 69 73 20 73 65 74 2c  ag of P2 is set,
1e667 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63 68   then the row ch
1e668 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a  ange count is.**
1e669 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f 74   incremented (ot
1e66a 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 0a 2a 2a  herwise not)..**
1e66b 0a 2a 2a 20 50 31 20 6d 75 73 74 20 6e 6f 74 20  .** P1 must not 
1e66c 62 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e  be pseudo-table.
1e66d 20 20 49 74 20 68 61 73 20 74 6f 20 62 65 20 61    It has to be a
1e66e 20 72 65 61 6c 20 74 61 62 6c 65 20 77 69 74 68   real table with
1e66f 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 72 6f 77  .** multiple row
1e670 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69  s..**.** If P4 i
1e671 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
1e672 20 69 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20   it is the name 
1e673 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  of the table tha
1e674 74 20 50 31 20 69 73 0a 2a 2a 20 70 6f 69 6e 74  t P1 is.** point
1e675 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 75 70 64  ing to.  The upd
1e676 61 74 65 20 68 6f 6f 6b 20 77 69 6c 6c 20 62 65  ate hook will be
1e677 20 69 6e 76 6f 6b 65 64 2c 20 69 66 20 69 74 20   invoked, if it 
1e678 65 78 69 73 74 73 2e 0a 2a 2a 20 49 66 20 50 34  exists..** If P4
1e679 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65   is not NULL the
1e67a 6e 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20  n the P1 cursor 
1e67b 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 70  must have been p
1e67c 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a 20 75 73 69  ositioned.** usi
1e67d 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 70  ng OP_NotFound p
1e67e 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67  rior to invoking
1e67f 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 0a 2a 2f   this opcode..*/
1e680 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65 74 65 3a  .case OP_Delete:
1e681 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1e682 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e683 65 64 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a  ed into u.bg */.
1e684 20 20 69 36 34 20 69 4b 65 79 3b 0a 20 20 56 64    i64 iKey;.  Vd
1e685 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65  beCursor *pC;.#e
1e686 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e687 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e688 74 6f 20 75 2e 62 67 20 2a 2f 0a 0a 20 20 75 2e  to u.bg */..  u.
1e689 62 67 2e 69 4b 65 79 20 3d 20 30 3b 0a 20 20 61  bg.iKey = 0;.  a
1e68a 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1e68b 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e  0 && pOp->p1<p->
1e68c 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62  nCursor );.  u.b
1e68d 67 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b  g.pC = p->apCsr[
1e68e 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1e68f 72 74 28 20 75 2e 62 67 2e 70 43 21 3d 30 20 29  rt( u.bg.pC!=0 )
1e690 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 67  ;.  assert( u.bg
1e691 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20  .pC->pCursor!=0 
1e692 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c 69  );  /* Only vali
1e693 64 20 66 6f 72 20 72 65 61 6c 20 74 61 62 6c 65  d for real table
1e694 73 2c 20 6e 6f 20 70 73 65 75 64 6f 74 61 62 6c  s, no pseudotabl
1e695 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  es */..  /* If t
1e696 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 77  he update-hook w
1e697 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20  ill be invoked, 
1e698 73 65 74 20 75 2e 62 67 2e 69 4b 65 79 20 74 6f  set u.bg.iKey to
1e699 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   the rowid of th
1e69a 65 0a 20 20 2a 2a 20 72 6f 77 20 62 65 69 6e 67  e.  ** row being
1e69b 20 64 65 6c 65 74 65 64 2e 0a 20 20 2a 2f 0a 20   deleted..  */. 
1e69c 20 69 66 28 20 64 62 2d 3e 78 55 70 64 61 74 65   if( db->xUpdate
1e69d 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d  Callback && pOp-
1e69e 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 61 73 73  >p4.z ){.    ass
1e69f 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 69 73  ert( u.bg.pC->is
1e6a0 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73  Table );.    ass
1e6a1 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 72 6f  ert( u.bg.pC->ro
1e6a2 77 69 64 49 73 56 61 6c 69 64 20 29 3b 20 20 2f  widIsValid );  /
1e6a3 2a 20 6c 61 73 74 52 6f 77 69 64 20 73 65 74 20  * lastRowid set 
1e6a4 62 79 20 70 72 65 76 69 6f 75 73 20 4f 50 5f 4e  by previous OP_N
1e6a5 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 75  otFound */.    u
1e6a6 2e 62 67 2e 69 4b 65 79 20 3d 20 75 2e 62 67 2e  .bg.iKey = u.bg.
1e6a7 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20  pC->lastRowid;. 
1e6a8 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 4f 50 5f   }..  /* The OP_
1e6a9 44 65 6c 65 74 65 20 6f 70 63 6f 64 65 20 61 6c  Delete opcode al
1e6aa 77 61 79 73 20 66 6f 6c 6c 6f 77 73 20 61 6e 20  ways follows an 
1e6ab 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 20  OP_NotExists or 
1e6ac 4f 50 5f 4c 61 73 74 20 6f 72 0a 20 20 2a 2a 20  OP_Last or.  ** 
1e6ad 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65  OP_Column on the
1e6ae 20 73 61 6d 65 20 74 61 62 6c 65 20 77 69 74 68   same table with
1e6af 6f 75 74 20 61 6e 79 20 69 6e 74 65 72 76 65 6e  out any interven
1e6b0 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 73 20 74  ing operations t
1e6b1 68 61 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 6d  hat.  ** might m
1e6b2 6f 76 65 20 6f 72 20 69 6e 76 61 6c 69 64 61 74  ove or invalidat
1e6b3 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20 48  e the cursor.  H
1e6b4 65 6e 63 65 20 63 75 72 73 6f 72 20 75 2e 62 67  ence cursor u.bg
1e6b5 2e 70 43 20 69 73 20 61 6c 77 61 79 73 20 70 6f  .pC is always po
1e6b6 69 6e 74 69 6e 67 0a 20 20 2a 2a 20 74 6f 20 74  inting.  ** to t
1e6b7 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c  he row to be del
1e6b8 65 74 65 64 20 61 6e 64 20 74 68 65 20 73 71 6c  eted and the sql
1e6b9 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f  ite3VdbeCursorMo
1e6ba 76 65 74 6f 28 29 20 6f 70 65 72 61 74 69 6f 6e  veto() operation
1e6bb 0a 20 20 2a 2a 20 62 65 6c 6f 77 20 69 73 20 61  .  ** below is a
1e6bc 6c 77 61 79 73 20 61 20 6e 6f 2d 6f 70 20 61 6e  lways a no-op an
1e6bd 64 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 20  d cannot fail.  
1e6be 57 65 20 77 69 6c 6c 20 72 75 6e 20 69 74 20 61  We will run it a
1e6bf 6e 79 68 6f 77 2c 20 74 68 6f 75 67 68 2c 0a 20  nyhow, though,. 
1e6c0 20 2a 2a 20 74 6f 20 67 75 61 72 64 20 61 67 61   ** to guard aga
1e6c1 69 6e 73 74 20 66 75 74 75 72 65 20 63 68 61 6e  inst future chan
1e6c2 67 65 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20  ges to the code 
1e6c3 67 65 6e 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 2f  generator..  **/
1e6c4 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e  .  assert( u.bg.
1e6c5 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1e6c6 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20  to==0 );.  rc = 
1e6c7 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
1e6c8 72 4d 6f 76 65 74 6f 28 75 2e 62 67 2e 70 43 29  rMoveto(u.bg.pC)
1e6c9 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63  ;.  if( NEVER(rc
1e6ca 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67  !=SQLITE_OK) ) g
1e6cb 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1e6cc 5f 65 72 72 6f 72 3b 0a 0a 20 20 73 71 6c 69 74  _error;..  sqlit
1e6cd 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64  e3BtreeSetCached
1e6ce 52 6f 77 69 64 28 75 2e 62 67 2e 70 43 2d 3e 70  Rowid(u.bg.pC->p
1e6cf 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63  Cursor, 0);.  rc
1e6d0 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44   = sqlite3BtreeD
1e6d1 65 6c 65 74 65 28 75 2e 62 67 2e 70 43 2d 3e 70  elete(u.bg.pC->p
1e6d2 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e 62 67 2e  Cursor);.  u.bg.
1e6d3 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1e6d4 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 0a  = CACHE_STALE;..
1e6d5 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
1e6d6 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20 72  update-hook if r
1e6d7 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66  equired. */.  if
1e6d8 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1e6d9 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61  && db->xUpdateCa
1e6da 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e 70  llback && pOp->p
1e6db 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  4.z ){.    const
1e6dc 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d   char *zDb = db-
1e6dd 3e 61 44 62 5b 75 2e 62 67 2e 70 43 2d 3e 69 44  >aDb[u.bg.pC->iD
1e6de 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f  b].zName;.    co
1e6df 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 20 3d  nst char *zTbl =
1e6e0 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20   pOp->p4.z;.    
1e6e1 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c 62  db->xUpdateCallb
1e6e2 61 63 6b 28 64 62 2d 3e 70 55 70 64 61 74 65 41  ack(db->pUpdateA
1e6e3 72 67 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54  rg, SQLITE_DELET
1e6e4 45 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 75 2e  E, zDb, zTbl, u.
1e6e5 62 67 2e 69 4b 65 79 29 3b 0a 20 20 20 20 61 73  bg.iKey);.    as
1e6e6 73 65 72 74 28 20 75 2e 62 67 2e 70 43 2d 3e 69  sert( u.bg.pC->i
1e6e7 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 69  Db>=0 );.  }.  i
1e6e8 66 28 20 70 4f 70 2d 3e 70 32 20 26 20 4f 50 46  f( pOp->p2 & OPF
1e6e9 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 20 70 2d  LAG_NCHANGE ) p-
1e6ea 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20 62 72  >nChange++;.  br
1e6eb 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70 63 6f 64 65  eak;.}./* Opcode
1e6ec 3a 20 52 65 73 65 74 43 6f 75 6e 74 20 2a 20 2a  : ResetCount * *
1e6ed 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65   * * *.**.** The
1e6ee 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 68   value of the ch
1e6ef 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20  ange counter is 
1e6f0 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 64 61  copied to the da
1e6f1 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a  tabase handle.**
1e6f2 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1e6f3 28 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62  (returned by sub
1e6f4 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f  sequent calls to
1e6f5 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
1e6f6 28 29 29 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65  ())..** Then the
1e6f7 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c 20 63 68   VMs internal ch
1e6f8 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 72 65 73  ange counter res
1e6f9 65 74 73 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 69  ets to 0..** Thi
1e6fa 73 20 69 73 20 75 73 65 64 20 62 79 20 74 72 69  s is used by tri
1e6fb 67 67 65 72 20 70 72 6f 67 72 61 6d 73 2e 0a 2a  gger programs..*
1e6fc 2f 0a 63 61 73 65 20 4f 50 5f 52 65 73 65 74 43  /.case OP_ResetC
1e6fd 6f 75 6e 74 3a 20 7b 0a 20 20 73 71 6c 69 74 65  ount: {.  sqlite
1e6fe 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
1e6ff 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29 3b  db, p->nChange);
1e700 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20  .  p->nChange = 
1e701 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  0;.  break;.}../
1e702 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 44 61 74  * Opcode: RowDat
1e703 61 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  a P1 P2 * * *.**
1e704 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72  .** Write into r
1e705 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63  egister P2 the c
1e706 6f 6d 70 6c 65 74 65 20 72 6f 77 20 64 61 74 61  omplete row data
1e707 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a   for cursor P1..
1e708 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69  ** There is no i
1e709 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66  nterpretation of
1e70a 20 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20   the data.  .** 
1e70b 49 74 20 69 73 20 6a 75 73 74 20 63 6f 70 69 65  It is just copie
1e70c 64 20 6f 6e 74 6f 20 74 68 65 20 50 32 20 72 65  d onto the P2 re
1e70d 67 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61  gister exactly a
1e70e 73 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e  s .** it is foun
1e70f 64 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  d in the databas
1e710 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e file..**.** If
1e711 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d   the P1 cursor m
1e712 75 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20  ust be pointing 
1e713 74 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28  to a valid row (
1e714 6e 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a  not a NULL row).
1e715 2a 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62  ** of a real tab
1e716 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f  le, not a pseudo
1e717 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20 4f 70  -table..*/./* Op
1e718 63 6f 64 65 3a 20 52 6f 77 4b 65 79 20 50 31 20  code: RowKey P1 
1e719 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57  P2 * * *.**.** W
1e71a 72 69 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74  rite into regist
1e71b 65 72 20 50 32 20 74 68 65 20 63 6f 6d 70 6c 65  er P2 the comple
1e71c 74 65 20 72 6f 77 20 6b 65 79 20 66 6f 72 20 63  te row key for c
1e71d 75 72 73 6f 72 20 50 31 2e 0a 2a 2a 20 54 68 65  ursor P1..** The
1e71e 72 65 20 69 73 20 6e 6f 20 69 6e 74 65 72 70 72  re is no interpr
1e71f 65 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  etation of the d
1e720 61 74 61 2e 20 20 0a 2a 2a 20 54 68 65 20 6b 65  ata.  .** The ke
1e721 79 20 69 73 20 63 6f 70 69 65 64 20 6f 6e 74 6f  y is copied onto
1e722 20 74 68 65 20 50 33 20 72 65 67 69 73 74 65 72   the P3 register
1e723 20 65 78 61 63 74 6c 79 20 61 73 20 0a 2a 2a 20   exactly as .** 
1e724 69 74 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74  it is found in t
1e725 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1e726 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50  ..**.** If the P
1e727 31 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65  1 cursor must be
1e728 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76   pointing to a v
1e729 61 6c 69 64 20 72 6f 77 20 28 6e 6f 74 20 61 20  alid row (not a 
1e72a 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a 20 6f 66 20  NULL row).** of 
1e72b 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e 6f  a real table, no
1e72c 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  t a pseudo-table
1e72d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77  ..*/.case OP_Row
1e72e 4b 65 79 3a 0a 63 61 73 65 20 4f 50 5f 52 6f 77  Key:.case OP_Row
1e72f 44 61 74 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f  Data: {.#if 0  /
1e730 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e731 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e732 68 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f  h */.  VdbeCurso
1e733 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f  r *pC;.  BtCurso
1e734 72 20 2a 70 43 72 73 72 3b 0a 20 20 75 33 32 20  r *pCrsr;.  u32 
1e735 6e 3b 0a 20 20 69 36 34 20 6e 36 34 3b 0a 23 65  n;.  i64 n64;.#e
1e736 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e737 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e738 74 6f 20 75 2e 62 68 20 2a 2f 0a 0a 20 20 70 4f  to u.bh */..  pO
1e739 75 74 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e  ut = &aMem[pOp->
1e73a 70 32 5d 3b 0a 0a 20 20 2f 2a 20 4e 6f 74 65 20  p2];..  /* Note 
1e73b 74 68 61 74 20 52 6f 77 4b 65 79 20 61 6e 64 20  that RowKey and 
1e73c 52 6f 77 44 61 74 61 20 61 72 65 20 72 65 61 6c  RowData are real
1e73d 6c 79 20 65 78 61 63 74 6c 79 20 74 68 65 20 73  ly exactly the s
1e73e 61 6d 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ame instruction 
1e73f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1e740 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1e741 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1e742 0a 20 20 75 2e 62 68 2e 70 43 20 3d 20 70 2d 3e  .  u.bh.pC = p->
1e743 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1e744 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70    assert( u.bh.p
1e745 43 2d 3e 69 73 54 61 62 6c 65 20 7c 7c 20 70 4f  C->isTable || pO
1e746 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f  p->opcode==OP_Ro
1e747 77 4b 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74  wKey );.  assert
1e748 28 20 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64  ( u.bh.pC->isInd
1e749 65 78 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64  ex || pOp->opcod
1e74a 65 3d 3d 4f 50 5f 52 6f 77 44 61 74 61 20 29 3b  e==OP_RowData );
1e74b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e  .  assert( u.bh.
1e74c 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  pC!=0 );.  asser
1e74d 74 28 20 75 2e 62 68 2e 70 43 2d 3e 6e 75 6c 6c  t( u.bh.pC->null
1e74e 52 6f 77 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Row==0 );.  asse
1e74f 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70 73 65  rt( u.bh.pC->pse
1e750 75 64 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29  udoTableReg==0 )
1e751 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68  ;.  assert( u.bh
1e752 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20  .pC->pCursor!=0 
1e753 29 3b 0a 20 20 75 2e 62 68 2e 70 43 72 73 72 20  );.  u.bh.pCrsr 
1e754 3d 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72 73  = u.bh.pC->pCurs
1e755 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  or;.  assert( sq
1e756 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
1e757 49 73 56 61 6c 69 64 28 75 2e 62 68 2e 70 43 72  IsValid(u.bh.pCr
1e758 73 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  sr) );..  /* The
1e759 20 4f 50 5f 52 6f 77 4b 65 79 20 61 6e 64 20 4f   OP_RowKey and O
1e75a 50 5f 52 6f 77 44 61 74 61 20 6f 70 63 6f 64 65  P_RowData opcode
1e75b 73 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 20  s always follow 
1e75c 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72 0a  OP_NotExists or.
1e75d 20 20 2a 2a 20 4f 50 5f 52 65 77 69 6e 64 2f 4f    ** OP_Rewind/O
1e75e 70 5f 4e 65 78 74 20 77 69 74 68 20 6e 6f 20 69  p_Next with no i
1e75f 6e 74 65 72 76 65 6e 69 6e 67 20 69 6e 73 74 72  ntervening instr
1e760 75 63 74 69 6f 6e 73 20 74 68 61 74 20 6d 69 67  uctions that mig
1e761 68 74 20 69 6e 76 61 6c 69 64 61 74 65 0a 20 20  ht invalidate.  
1e762 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20  ** the cursor.  
1e763 48 65 6e 63 65 20 74 68 65 20 66 6f 6c 6c 6f 77  Hence the follow
1e764 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65 43  ing sqlite3VdbeC
1e765 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61  ursorMoveto() ca
1e766 6c 6c 20 69 73 20 61 6c 77 61 79 73 0a 20 20 2a  ll is always.  *
1e767 2a 20 61 20 6e 6f 2d 6f 70 20 61 6e 64 20 63 61  * a no-op and ca
1e768 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 20 42  n never fail.  B
1e769 75 74 20 77 65 20 6c 65 61 76 65 20 69 74 20 69  ut we leave it i
1e76a 6e 20 70 6c 61 63 65 20 61 73 20 61 20 73 61 66  n place as a saf
1e76b 65 74 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ety..  */.  asse
1e76c 72 74 28 20 75 2e 62 68 2e 70 43 2d 3e 64 65 66  rt( u.bh.pC->def
1e76d 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29  erredMoveto==0 )
1e76e 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1e76f 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f  VdbeCursorMoveto
1e770 28 75 2e 62 68 2e 70 43 29 3b 0a 20 20 69 66 28  (u.bh.pC);.  if(
1e771 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54   NEVER(rc!=SQLIT
1e772 45 5f 4f 4b 29 20 29 20 67 6f 74 6f 20 61 62 6f  E_OK) ) goto abo
1e773 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1e774 0a 0a 20 20 69 66 28 20 75 2e 62 68 2e 70 43 2d  ..  if( u.bh.pC-
1e775 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  >isIndex ){.    
1e776 61 73 73 65 72 74 28 20 21 75 2e 62 68 2e 70 43  assert( !u.bh.pC
1e777 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20  ->isTable );.   
1e778 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e779 65 65 4b 65 79 53 69 7a 65 28 75 2e 62 68 2e 70  eeKeySize(u.bh.p
1e77a 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e 36 34 29  Crsr, &u.bh.n64)
1e77b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  ;.    assert( rc
1e77c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20  ==SQLITE_OK );  
1e77d 20 20 2f 2a 20 54 72 75 65 20 62 65 63 61 75 73    /* True becaus
1e77e 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65 74  e of CursorMovet
1e77f 6f 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 20 2a  o() call above *
1e780 2f 0a 20 20 20 20 69 66 28 20 75 2e 62 68 2e 6e  /.    if( u.bh.n
1e781 36 34 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  64>db->aLimit[SQ
1e782 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1e783 48 5d 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  H] ){.      goto
1e784 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a   too_big;.    }.
1e785 20 20 20 20 75 2e 62 68 2e 6e 20 3d 20 28 75 33      u.bh.n = (u3
1e786 32 29 75 2e 62 68 2e 6e 36 34 3b 0a 20 20 7d 65  2)u.bh.n64;.  }e
1e787 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  lse{.    rc = sq
1e788 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69  lite3BtreeDataSi
1e789 7a 65 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 26  ze(u.bh.pCrsr, &
1e78a 75 2e 62 68 2e 6e 29 3b 0a 20 20 20 20 61 73 73  u.bh.n);.    ass
1e78b 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
1e78c 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 44 61 74 61  OK );    /* Data
1e78d 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61  Size() cannot fa
1e78e 69 6c 20 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e  il */.    if( u.
1e78f 62 68 2e 6e 3e 28 75 33 32 29 64 62 2d 3e 61 4c  bh.n>(u32)db->aL
1e790 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1e791 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
1e792 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1e793 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
1e794 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
1e795 72 6f 77 28 70 4f 75 74 2c 20 75 2e 62 68 2e 6e  row(pOut, u.bh.n
1e796 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  , 0) ){.    goto
1e797 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 70   no_mem;.  }.  p
1e798 4f 75 74 2d 3e 6e 20 3d 20 75 2e 62 68 2e 6e 3b  Out->n = u.bh.n;
1e799 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1e79a 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 42 6c 6f 62  g(pOut, MEM_Blob
1e79b 29 3b 0a 20 20 69 66 28 20 75 2e 62 68 2e 70 43  );.  if( u.bh.pC
1e79c 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20  ->isIndex ){.   
1e79d 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e79e 65 65 4b 65 79 28 75 2e 62 68 2e 70 43 72 73 72  eeKey(u.bh.pCrsr
1e79f 2c 20 30 2c 20 75 2e 62 68 2e 6e 2c 20 70 4f 75  , 0, u.bh.n, pOu
1e7a0 74 2d 3e 7a 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  t->z);.  }else{.
1e7a1 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1e7a2 42 74 72 65 65 44 61 74 61 28 75 2e 62 68 2e 70  BtreeData(u.bh.p
1e7a3 43 72 73 72 2c 20 30 2c 20 75 2e 62 68 2e 6e 2c  Crsr, 0, u.bh.n,
1e7a4 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 0a 20   pOut->z);.  }. 
1e7a5 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c   pOut->enc = SQL
1e7a6 49 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e  ITE_UTF8;  /* In
1e7a7 20 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69   case the blob i
1e7a8 73 20 65 76 65 72 20 63 61 73 74 20 74 6f 20 74  s ever cast to t
1e7a9 65 78 74 20 2a 2f 0a 20 20 55 50 44 41 54 45 5f  ext */.  UPDATE_
1e7aa 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75  MAX_BLOBSIZE(pOu
1e7ab 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
1e7ac 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 69 64  /* Opcode: Rowid
1e7ad 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1e7ae 2a 2a 20 53 74 6f 72 65 20 69 6e 20 72 65 67 69  ** Store in regi
1e7af 73 74 65 72 20 50 32 20 61 6e 20 69 6e 74 65 67  ster P2 an integ
1e7b0 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20  er which is the 
1e7b1 6b 65 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65  key of the table
1e7b2 20 65 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 50   entry that.** P
1e7b3 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  1 is currently p
1e7b4 6f 69 6e 74 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 50  oint to..**.** P
1e7b5 31 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  1 can be either 
1e7b6 61 6e 20 6f 72 64 69 6e 61 72 79 20 74 61 62 6c  an ordinary tabl
1e7b7 65 20 6f 72 20 61 20 76 69 72 74 75 61 6c 20 74  e or a virtual t
1e7b8 61 62 6c 65 2e 20 20 54 68 65 72 65 20 75 73 65  able.  There use
1e7b9 64 20 74 6f 0a 2a 2a 20 62 65 20 61 20 73 65 70  d to.** be a sep
1e7ba 61 72 61 74 65 20 4f 50 5f 56 52 6f 77 69 64 20  arate OP_VRowid 
1e7bb 6f 70 63 6f 64 65 20 66 6f 72 20 75 73 65 20 77  opcode for use w
1e7bc 69 74 68 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ith virtual tabl
1e7bd 65 73 2c 20 62 75 74 20 74 68 69 73 0a 2a 2a 20  es, but this.** 
1e7be 6f 6e 65 20 6f 70 63 6f 64 65 20 6e 6f 77 20 77  one opcode now w
1e7bf 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68 20 74 61  orks for both ta
1e7c0 62 6c 65 20 74 79 70 65 73 2e 0a 2a 2f 0a 63 61  ble types..*/.ca
1e7c1 73 65 20 4f 50 5f 52 6f 77 69 64 3a 20 7b 20 20  se OP_Rowid: {  
1e7c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e7c3 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1e7c4 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e */.#if 0  /* l
1e7c5 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e7c6 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 69 20 2a  oved into u.bi *
1e7c7 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1e7c8 70 43 3b 0a 20 20 69 36 34 20 76 3b 0a 20 20 73  pC;.  i64 v;.  s
1e7c9 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
1e7ca 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  ab;.  const sqli
1e7cb 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
1e7cc 75 6c 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  ule;.#endif /* l
1e7cd 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e7ce 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 69 20 2a  oved into u.bi *
1e7cf 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  /..  assert( pOp
1e7d0 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1e7d1 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1e7d2 0a 20 20 75 2e 62 69 2e 70 43 20 3d 20 70 2d 3e  .  u.bi.pC = p->
1e7d3 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1e7d4 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70    assert( u.bi.p
1e7d5 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  C!=0 );.  assert
1e7d6 28 20 75 2e 62 69 2e 70 43 2d 3e 70 73 65 75 64  ( u.bi.pC->pseud
1e7d7 6f 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a  oTableReg==0 );.
1e7d8 20 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 6e    if( u.bi.pC->n
1e7d9 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 70 4f  ullRow ){.    pO
1e7da 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ut->flags = MEM_
1e7db 4e 75 6c 6c 3b 0a 20 20 20 20 62 72 65 61 6b 3b  Null;.    break;
1e7dc 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 62  .  }else if( u.b
1e7dd 69 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  i.pC->deferredMo
1e7de 76 65 74 6f 20 29 7b 0a 20 20 20 20 75 2e 62 69  veto ){.    u.bi
1e7df 2e 76 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 6d 6f  .v = u.bi.pC->mo
1e7e0 76 65 74 6f 54 61 72 67 65 74 3b 0a 23 69 66 6e  vetoTarget;.#ifn
1e7e1 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1e7e2 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 7d  VIRTUALTABLE.  }
1e7e3 65 6c 73 65 20 69 66 28 20 75 2e 62 69 2e 70 43  else if( u.bi.pC
1e7e4 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 7b  ->pVtabCursor ){
1e7e5 0a 20 20 20 20 75 2e 62 69 2e 70 56 74 61 62 20  .    u.bi.pVtab 
1e7e6 3d 20 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62  = u.bi.pC->pVtab
1e7e7 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20  Cursor->pVtab;. 
1e7e8 20 20 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 20     u.bi.pModule 
1e7e9 3d 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e 70 4d  = u.bi.pVtab->pM
1e7ea 6f 64 75 6c 65 3b 0a 20 20 20 20 61 73 73 65 72  odule;.    asser
1e7eb 74 28 20 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d  t( u.bi.pModule-
1e7ec 3e 78 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 69  >xRowid );.    i
1e7ed 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1e7ee 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61  Off(db) ) goto a
1e7ef 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1e7f0 73 65 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 62  se;.    rc = u.b
1e7f1 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69  i.pModule->xRowi
1e7f2 64 28 75 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62  d(u.bi.pC->pVtab
1e7f3 43 75 72 73 6f 72 2c 20 26 75 2e 62 69 2e 76 29  Cursor, &u.bi.v)
1e7f4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
1e7f5 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1e7f6 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72  sg);.    p->zErr
1e7f7 4d 73 67 20 3d 20 75 2e 62 69 2e 70 56 74 61 62  Msg = u.bi.pVtab
1e7f8 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 75  ->zErrMsg;.    u
1e7f9 2e 62 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d  .bi.pVtab->zErrM
1e7fa 73 67 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  sg = 0;.    if( 
1e7fb 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1e7fc 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1e7fd 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1e7fe 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1e7ff 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1e800 4c 45 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20  LE */.  }else{. 
1e801 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e     assert( u.bi.
1e802 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29  pC->pCursor!=0 )
1e803 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1e804 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
1e805 74 6f 28 75 2e 62 69 2e 70 43 29 3b 0a 20 20 20  to(u.bi.pC);.   
1e806 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 61   if( rc ) goto a
1e807 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1e808 72 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 69 2e  r;.    if( u.bi.
1e809 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64  pC->rowidIsValid
1e80a 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 69 2e 76   ){.      u.bi.v
1e80b 20 3d 20 75 2e 62 69 2e 70 43 2d 3e 6c 61 73 74   = u.bi.pC->last
1e80c 52 6f 77 69 64 3b 0a 20 20 20 20 7d 65 6c 73 65  Rowid;.    }else
1e80d 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
1e80e 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
1e80f 28 75 2e 62 69 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.bi.pC->pCurso
1e810 72 2c 20 26 75 2e 62 69 2e 76 29 3b 0a 20 20 20  r, &u.bi.v);.   
1e811 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
1e812 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20  QLITE_OK );  /* 
1e813 41 6c 77 61 79 73 20 73 6f 20 62 65 63 61 75 73  Always so becaus
1e814 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65 74  e of CursorMovet
1e815 6f 28 29 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20  o() above */.   
1e816 20 7d 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75   }.  }.  pOut->u
1e817 2e 69 20 3d 20 75 2e 62 69 2e 76 3b 0a 20 20 62  .i = u.bi.v;.  b
1e818 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1e819 64 65 3a 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a  de: NullRow P1 *
1e81a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76   * * *.**.** Mov
1e81b 65 20 74 68 65 20 63 75 72 73 6f 72 20 50 31 20  e the cursor P1 
1e81c 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20  to a null row.  
1e81d 41 6e 79 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70  Any OP_Column op
1e81e 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74  erations.** that
1e81f 20 6f 63 63 75 72 20 77 68 69 6c 65 20 74 68 65   occur while the
1e820 20 63 75 72 73 6f 72 20 69 73 20 6f 6e 20 74 68   cursor is on th
1e821 65 20 6e 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20  e null row will 
1e822 61 6c 77 61 79 73 0a 2a 2a 20 77 72 69 74 65 20  always.** write 
1e823 61 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20  a NULL..*/.case 
1e824 4f 50 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 23 69  OP_NullRow: {.#i
1e825 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1e826 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e827 74 6f 20 75 2e 62 6a 20 2a 2f 0a 20 20 56 64 62  to u.bj */.  Vdb
1e828 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e  eCursor *pC;.#en
1e829 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e82a 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e82b 6f 20 75 2e 62 6a 20 2a 2f 0a 0a 20 20 61 73 73  o u.bj */..  ass
1e82c 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1e82d 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43  && pOp->p1<p->nC
1e82e 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6a 2e  ursor );.  u.bj.
1e82f 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  pC = p->apCsr[pO
1e830 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  p->p1];.  assert
1e831 28 20 75 2e 62 6a 2e 70 43 21 3d 30 20 29 3b 0a  ( u.bj.pC!=0 );.
1e832 20 20 75 2e 62 6a 2e 70 43 2d 3e 6e 75 6c 6c 52    u.bj.pC->nullR
1e833 6f 77 20 3d 20 31 3b 0a 20 20 75 2e 62 6a 2e 70  ow = 1;.  u.bj.p
1e834 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1e835 3d 20 30 3b 0a 20 20 69 66 28 20 75 2e 62 6a 2e  = 0;.  if( u.bj.
1e836 70 43 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20  pC->pCursor ){. 
1e837 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
1e838 6c 65 61 72 43 75 72 73 6f 72 28 75 2e 62 6a 2e  learCursor(u.bj.
1e839 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20  pC->pCursor);.  
1e83a 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1e83b 20 4f 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 31   Opcode: Last P1
1e83c 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1e83d 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20  The next use of 
1e83e 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c  the Rowid or Col
1e83f 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74  umn or Next inst
1e840 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a  ruction for P1 .
1e841 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f  ** will refer to
1e842 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
1e843 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1e844 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a  table or index..
1e845 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  ** If the table 
1e846 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74  or index is empt
1e847 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e  y and P2>0, then
1e848 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c   jump immediatel
1e849 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50  y to P2..** If P
1e84a 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 65  2 is 0 or if the
1e84b 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1e84c 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61  is not empty, fa
1e84d 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f  ll through.** to
1e84e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
1e84f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63  nstruction..*/.c
1e850 61 73 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20  ase OP_Last: {  
1e851 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
1e852 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1e853 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1e854 20 69 6e 74 6f 20 75 2e 62 6b 20 2a 2f 0a 20 20   into u.bk */.  
1e855 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1e856 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1e857 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23 65  r;.  int res;.#e
1e858 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e859 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e85a 74 6f 20 75 2e 62 6b 20 2a 2f 0a 0a 20 20 61 73  to u.bk */..  as
1e85b 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e85c 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e85d 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6b  Cursor );.  u.bk
1e85e 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1e85f 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1e860 74 28 20 75 2e 62 6b 2e 70 43 21 3d 30 20 29 3b  t( u.bk.pC!=0 );
1e861 0a 20 20 75 2e 62 6b 2e 70 43 72 73 72 20 3d 20  .  u.bk.pCrsr = 
1e862 75 2e 62 6b 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bk.pC->pCursor
1e863 3b 0a 20 20 69 66 28 20 75 2e 62 6b 2e 70 43 72  ;.  if( u.bk.pCr
1e864 73 72 3d 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62  sr==0 ){.    u.b
1e865 6b 2e 72 65 73 20 3d 20 31 3b 0a 20 20 7d 65 6c  k.res = 1;.  }el
1e866 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  se{.    rc = sql
1e867 69 74 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e  ite3BtreeLast(u.
1e868 62 6b 2e 70 43 72 73 72 2c 20 26 75 2e 62 6b 2e  bk.pCrsr, &u.bk.
1e869 72 65 73 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 6b  res);.  }.  u.bk
1e86a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28  .pC->nullRow = (
1e86b 75 38 29 75 2e 62 6b 2e 72 65 73 3b 0a 20 20 75  u8)u.bk.res;.  u
1e86c 2e 62 6b 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bk.pC->deferred
1e86d 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e  Moveto = 0;.  u.
1e86e 62 6b 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  bk.pC->rowidIsVa
1e86f 6c 69 64 20 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e  lid = 0;.  u.bk.
1e870 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1e871 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
1e872 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26   if( pOp->p2>0 &
1e873 26 20 75 2e 62 6b 2e 72 65 73 20 29 7b 0a 20 20  & u.bk.res ){.  
1e874 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1e875 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   1;.  }.  break;
1e876 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  .}.../* Opcode: 
1e877 53 6f 72 74 20 50 31 20 50 32 20 2a 20 2a 20 2a  Sort P1 P2 * * *
1e878 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
1e879 64 65 20 64 6f 65 73 20 65 78 61 63 74 6c 79 20  de does exactly 
1e87a 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61  the same thing a
1e87b 73 20 4f 50 5f 52 65 77 69 6e 64 20 65 78 63 65  s OP_Rewind exce
1e87c 70 74 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e  pt that.** it in
1e87d 63 72 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f  crements an undo
1e87e 63 75 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20  cumented global 
1e87f 76 61 72 69 61 62 6c 65 20 75 73 65 64 20 66 6f  variable used fo
1e880 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a  r testing..**.**
1e881 20 53 6f 72 74 69 6e 67 20 69 73 20 61 63 63 6f   Sorting is acco
1e882 6d 70 6c 69 73 68 65 64 20 62 79 20 77 72 69 74  mplished by writ
1e883 69 6e 67 20 72 65 63 6f 72 64 73 20 69 6e 74 6f  ing records into
1e884 20 61 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78   a sorting index
1e885 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e 64  ,.** then rewind
1e886 69 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20 61  ing that index a
1e887 6e 64 20 70 6c 61 79 69 6e 67 20 69 74 20 62 61  nd playing it ba
1e888 63 6b 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e  ck from beginnin
1e889 67 20 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65  g to.** end.  We
1e88a 20 75 73 65 20 74 68 65 20 4f 50 5f 53 6f 72 74   use the OP_Sort
1e88b 20 6f 70 63 6f 64 65 20 69 6e 73 74 65 61 64 20   opcode instead 
1e88c 6f 66 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20  of OP_Rewind to 
1e88d 64 6f 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64  do the.** rewind
1e88e 69 6e 67 20 73 6f 20 74 68 61 74 20 74 68 65 20  ing so that the 
1e88f 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
1e890 77 69 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e  will be incremen
1e891 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65  ted and.** regre
1e892 73 73 69 6f 6e 20 74 65 73 74 73 20 63 61 6e 20  ssion tests can 
1e893 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65  determine whethe
1e894 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 74  r or not the opt
1e895 69 6d 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72  imizer is.** cor
1e896 72 65 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e  rectly optimizin
1e897 67 20 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a  g out sorts..*/.
1e898 63 61 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20  case OP_Sort: { 
1e899 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a         /* jump *
1e89a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
1e89b 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 73  TEST.  sqlite3_s
1e89c 6f 72 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73  ort_count++;.  s
1e89d 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f  qlite3_search_co
1e89e 75 6e 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20  unt--;.#endif.  
1e89f 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49  p->aCounter[SQLI
1e8a0 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f  TE_STMTSTATUS_SO
1e8a1 52 54 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61  RT-1]++;.  /* Fa
1e8a2 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20  ll through into 
1e8a3 4f 50 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f  OP_Rewind */.}./
1e8a4 2a 20 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e 64  * Opcode: Rewind
1e8a5 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1e8a6 2a 2a 20 54 68 65 20 6e 65 78 74 20 75 73 65 20  ** The next use 
1e8a7 6f 66 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20  of the Rowid or 
1e8a8 43 6f 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69  Column or Next i
1e8a9 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50  nstruction for P
1e8aa 31 20 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72  1 .** will refer
1e8ab 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e   to the first en
1e8ac 74 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62  try in the datab
1e8ad 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  ase table or ind
1e8ae 65 78 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61  ex..** If the ta
1e8af 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20  ble or index is 
1e8b0 65 6d 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20  empty and P2>0, 
1e8b1 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69  then jump immedi
1e8b2 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20  ately to P2..** 
1e8b3 49 66 20 50 32 20 69 73 20 30 20 6f 72 20 69 66  If P2 is 0 or if
1e8b4 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e   the table or in
1e8b5 64 65 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79  dex is not empty
1e8b6 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a  , fall through.*
1e8b7 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * to the followi
1e8b8 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  ng instruction..
1e8b9 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 77 69 6e  */.case OP_Rewin
1e8ba 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  d: {        /* j
1e8bb 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  ump */.#if 0  /*
1e8bc 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e8bd 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c   moved into u.bl
1e8be 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72   */.  VdbeCursor
1e8bf 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72   *pC;.  BtCursor
1e8c0 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72   *pCrsr;.  int r
1e8c1 65 73 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  es;.#endif /* lo
1e8c2 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e8c3 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f  ved into u.bl */
1e8c4 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ..  assert( pOp-
1e8c5 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1e8c6 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1e8c7 20 20 75 2e 62 6c 2e 70 43 20 3d 20 70 2d 3e 61    u.bl.pC = p->a
1e8c8 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
1e8c9 20 61 73 73 65 72 74 28 20 75 2e 62 6c 2e 70 43   assert( u.bl.pC
1e8ca 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 75 2e  !=0 );.  if( (u.
1e8cb 62 6c 2e 70 43 72 73 72 20 3d 20 75 2e 62 6c 2e  bl.pCrsr = u.bl.
1e8cc 70 43 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20  pC->pCursor)!=0 
1e8cd 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1e8ce 74 65 33 42 74 72 65 65 46 69 72 73 74 28 75 2e  te3BtreeFirst(u.
1e8cf 62 6c 2e 70 43 72 73 72 2c 20 26 75 2e 62 6c 2e  bl.pCrsr, &u.bl.
1e8d0 72 65 73 29 3b 0a 20 20 20 20 75 2e 62 6c 2e 70  res);.    u.bl.p
1e8d1 43 2d 3e 61 74 46 69 72 73 74 20 3d 20 75 2e 62  C->atFirst = u.b
1e8d2 6c 2e 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20  l.res==0 ?1:0;. 
1e8d3 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 64 65 66 65     u.bl.pC->defe
1e8d4 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a  rredMoveto = 0;.
1e8d5 20 20 20 20 75 2e 62 6c 2e 70 43 2d 3e 63 61 63      u.bl.pC->cac
1e8d6 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1e8d7 5f 53 54 41 4c 45 3b 0a 20 20 20 20 75 2e 62 6c  _STALE;.    u.bl
1e8d8 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69  .pC->rowidIsVali
1e8d9 64 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  d = 0;.  }else{.
1e8da 20 20 20 20 75 2e 62 6c 2e 72 65 73 20 3d 20 31      u.bl.res = 1
1e8db 3b 0a 20 20 7d 0a 20 20 75 2e 62 6c 2e 70 43 2d  ;.  }.  u.bl.pC-
1e8dc 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75  >nullRow = (u8)u
1e8dd 2e 62 6c 2e 72 65 73 3b 0a 20 20 61 73 73 65 72  .bl.res;.  asser
1e8de 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20  t( pOp->p2>0 && 
1e8df 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29  pOp->p2<p->nOp )
1e8e0 3b 0a 20 20 69 66 28 20 75 2e 62 6c 2e 72 65 73  ;.  if( u.bl.res
1e8e1 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
1e8e2 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20  ->p2 - 1;.  }.  
1e8e3 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1e8e4 6f 64 65 3a 20 4e 65 78 74 20 50 31 20 50 32 20  ode: Next P1 P2 
1e8e5 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61  * * *.**.** Adva
1e8e6 6e 63 65 20 63 75 72 73 6f 72 20 50 31 20 73 6f  nce cursor P1 so
1e8e7 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
1e8e8 74 6f 20 74 68 65 20 6e 65 78 74 20 6b 65 79 2f  to the next key/
1e8e9 64 61 74 61 20 70 61 69 72 20 69 6e 20 69 74 73  data pair in its
1e8ea 0a 2a 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  .** table or ind
1e8eb 65 78 2e 20 20 49 66 20 74 68 65 72 65 20 61 72  ex.  If there ar
1e8ec 65 20 6e 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61  e no more key/va
1e8ed 6c 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66  lue pairs then f
1e8ee 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74  all through.** t
1e8ef 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  o the following 
1e8f0 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75  instruction.  Bu
1e8f1 74 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20  t if the cursor 
1e8f2 61 64 76 61 6e 63 65 20 77 61 73 20 73 75 63 63  advance was succ
1e8f3 65 73 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20  essful,.** jump 
1e8f4 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50  immediately to P
1e8f5 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20  2..**.** The P1 
1e8f6 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66  cursor must be f
1e8f7 6f 72 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c  or a real table,
1e8f8 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61   not a pseudo-ta
1e8f9 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ble..**.** See a
1e8fa 6c 73 6f 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20  lso: Prev.*/./* 
1e8fb 4f 70 63 6f 64 65 3a 20 50 72 65 76 20 50 31 20  Opcode: Prev P1 
1e8fc 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42  P2 * * *.**.** B
1e8fd 61 63 6b 20 75 70 20 63 75 72 73 6f 72 20 50 31  ack up cursor P1
1e8fe 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e   so that it poin
1e8ff 74 73 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f  ts to the previo
1e900 75 73 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72  us key/data pair
1e901 20 69 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65   in its.** table
1e902 20 6f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74   or index.  If t
1e903 68 65 72 65 20 69 73 20 6e 6f 20 70 72 65 76 69  here is no previ
1e904 6f 75 73 20 6b 65 79 2f 76 61 6c 75 65 20 70 61  ous key/value pa
1e905 69 72 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68  irs then fall th
1e906 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20  rough.** to the 
1e907 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75  following instru
1e908 63 74 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74  ction.  But if t
1e909 68 65 20 63 75 72 73 6f 72 20 62 61 63 6b 75 70  he cursor backup
1e90a 20 77 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c   was successful,
1e90b 0a 2a 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61  .** jump immedia
1e90c 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  tely to P2..**.*
1e90d 2a 20 54 68 65 20 50 31 20 63 75 72 73 6f 72 20  * The P1 cursor 
1e90e 6d 75 73 74 20 62 65 20 66 6f 72 20 61 20 72 65  must be for a re
1e90f 61 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20  al table, not a 
1e910 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f  pseudo-table..*/
1e911 0a 63 61 73 65 20 4f 50 5f 50 72 65 76 3a 20 20  .case OP_Prev:  
1e912 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20          /* jump 
1e913 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a  */.case OP_Next:
1e914 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1e915 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  p */.#if 0  /* l
1e916 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e917 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a  oved into u.bm *
1e918 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1e919 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  pC;.  BtCursor *
1e91a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73  pCrsr;.  int res
1e91b 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1e91c 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e91d 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 0a  d into u.bm */..
1e91e 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45    CHECK_FOR_INTE
1e91f 52 52 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28  RRUPT;.  assert(
1e920 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1e921 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  Op->p1<p->nCurso
1e922 72 20 29 3b 0a 20 20 75 2e 62 6d 2e 70 43 20 3d  r );.  u.bm.pC =
1e923 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1e924 31 5d 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70  1];.  if( u.bm.p
1e925 43 3d 3d 30 20 29 7b 0a 20 20 20 20 62 72 65 61  C==0 ){.    brea
1e926 6b 3b 20 20 2f 2a 20 53 65 65 20 74 69 63 6b 65  k;  /* See ticke
1e927 74 20 23 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20  t #2273 */.  }. 
1e928 20 75 2e 62 6d 2e 70 43 72 73 72 20 3d 20 75 2e   u.bm.pCrsr = u.
1e929 62 6d 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a  bm.pC->pCursor;.
1e92a 20 20 69 66 28 20 75 2e 62 6d 2e 70 43 72 73 72    if( u.bm.pCrsr
1e92b 3d 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 6d 2e  ==0 ){.    u.bm.
1e92c 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b  pC->nullRow = 1;
1e92d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
1e92e 20 20 75 2e 62 6d 2e 72 65 73 20 3d 20 31 3b 0a    u.bm.res = 1;.
1e92f 20 20 61 73 73 65 72 74 28 20 75 2e 62 6d 2e 70    assert( u.bm.p
1e930 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1e931 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 70  o==0 );.  rc = p
1e932 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e  Op->opcode==OP_N
1e933 65 78 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72  ext ? sqlite3Btr
1e934 65 65 4e 65 78 74 28 75 2e 62 6d 2e 70 43 72 73  eeNext(u.bm.pCrs
1e935 72 2c 20 26 75 2e 62 6d 2e 72 65 73 29 20 3a 0a  r, &u.bm.res) :.
1e936 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e937 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
1e938 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f  lite3BtreePrevio
1e939 75 73 28 75 2e 62 6d 2e 70 43 72 73 72 2c 20 26  us(u.bm.pCrsr, &
1e93a 75 2e 62 6d 2e 72 65 73 29 3b 0a 20 20 75 2e 62  u.bm.res);.  u.b
1e93b 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20  m.pC->nullRow = 
1e93c 28 75 38 29 75 2e 62 6d 2e 72 65 73 3b 0a 20 20  (u8)u.bm.res;.  
1e93d 75 2e 62 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.bm.pC->cacheSt
1e93e 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1e93f 4c 45 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 72  LE;.  if( u.bm.r
1e940 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20  es==0 ){.    pc 
1e941 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1e942 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29     if( pOp->p5 )
1e943 20 70 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70   p->aCounter[pOp
1e944 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65  ->p5-1]++;.#ifde
1e945 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
1e946 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68    sqlite3_search
1e947 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
1e948 0a 20 20 7d 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e  .  }.  u.bm.pC->
1e949 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30  rowidIsValid = 0
1e94a 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1e94b 20 4f 70 63 6f 64 65 3a 20 49 64 78 49 6e 73 65   Opcode: IdxInse
1e94c 72 74 20 50 31 20 50 32 20 50 33 20 2a 20 50 35  rt P1 P2 P3 * P5
1e94d 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  .**.** Register 
1e94e 50 32 20 68 6f 6c 64 73 20 61 20 53 51 4c 20 69  P2 holds a SQL i
1e94f 6e 64 65 78 20 6b 65 79 20 6d 61 64 65 20 75 73  ndex key made us
1e950 69 6e 67 20 74 68 65 0a 2a 2a 20 4d 61 6b 65 52  ing the.** MakeR
1e951 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f  ecord instructio
1e952 6e 73 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65  ns.  This opcode
1e953 20 77 72 69 74 65 73 20 74 68 61 74 20 6b 65 79   writes that key
1e954 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 69 6e 64  .** into the ind
1e955 65 78 20 50 31 2e 20 20 44 61 74 61 20 66 6f 72  ex P1.  Data for
1e956 20 74 68 65 20 65 6e 74 72 79 20 69 73 20 6e 69   the entry is ni
1e957 6c 2e 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61  l..**.** P3 is a
1e958 20 66 6c 61 67 20 74 68 61 74 20 70 72 6f 76 69   flag that provi
1e959 64 65 73 20 61 20 68 69 6e 74 20 74 6f 20 74 68  des a hint to th
1e95a 65 20 62 2d 74 72 65 65 20 6c 61 79 65 72 20 74  e b-tree layer t
1e95b 68 61 74 20 74 68 69 73 0a 2a 2a 20 69 6e 73 65  hat this.** inse
1e95c 72 74 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20  rt is likely to 
1e95d 62 65 20 61 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a  be an append..**
1e95e 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63  .** This instruc
1e95f 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  tion only works 
1e960 66 6f 72 20 69 6e 64 69 63 65 73 2e 20 20 54 68  for indices.  Th
1e961 65 20 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73  e equivalent ins
1e962 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20  truction.** for 
1e963 74 61 62 6c 65 73 20 69 73 20 4f 50 5f 49 6e 73  tables is OP_Ins
1e964 65 72 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ert..*/.case OP_
1e965 49 64 78 49 6e 73 65 72 74 3a 20 7b 20 20 20 20  IdxInsert: {    
1e966 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69      /* in2 */.#i
1e967 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1e968 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e969 74 6f 20 75 2e 62 6e 20 2a 2f 0a 20 20 56 64 62  to u.bn */.  Vdb
1e96a 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42  eCursor *pC;.  B
1e96b 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a  tCursor *pCrsr;.
1e96c 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 63 6f    int nKey;.  co
1e96d 6e 73 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a  nst char *zKey;.
1e96e 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1e96f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e970 69 6e 74 6f 20 75 2e 62 6e 20 2a 2f 0a 0a 20 20  into u.bn */..  
1e971 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1e972 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d  =0 && pOp->p1<p-
1e973 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e  >nCursor );.  u.
1e974 62 6e 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72  bn.pC = p->apCsr
1e975 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1e976 65 72 74 28 20 75 2e 62 6e 2e 70 43 21 3d 30 20  ert( u.bn.pC!=0 
1e977 29 3b 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65  );.  pIn2 = &aMe
1e978 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73  m[pOp->p2];.  as
1e979 73 65 72 74 28 20 70 49 6e 32 2d 3e 66 6c 61 67  sert( pIn2->flag
1e97a 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a  s & MEM_Blob );.
1e97b 20 20 75 2e 62 6e 2e 70 43 72 73 72 20 3d 20 75    u.bn.pCrsr = u
1e97c 2e 62 6e 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b  .bn.pC->pCursor;
1e97d 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e  .  if( ALWAYS(u.
1e97e 62 6e 2e 70 43 72 73 72 21 3d 30 29 20 29 7b 0a  bn.pCrsr!=0) ){.
1e97f 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 6e      assert( u.bn
1e980 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20  .pC->isTable==0 
1e981 29 3b 0a 20 20 20 20 72 63 20 3d 20 45 78 70 61  );.    rc = Expa
1e982 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20  ndBlob(pIn2);.  
1e983 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1e984 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e 62  _OK ){.      u.b
1e985 6e 2e 6e 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 6e  n.nKey = pIn2->n
1e986 3b 0a 20 20 20 20 20 20 75 2e 62 6e 2e 7a 4b 65  ;.      u.bn.zKe
1e987 79 20 3d 20 70 49 6e 32 2d 3e 7a 3b 0a 20 20 20  y = pIn2->z;.   
1e988 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1e989 74 72 65 65 49 6e 73 65 72 74 28 75 2e 62 6e 2e  treeInsert(u.bn.
1e98a 70 43 72 73 72 2c 20 75 2e 62 6e 2e 7a 4b 65 79  pCrsr, u.bn.zKey
1e98b 2c 20 75 2e 62 6e 2e 6e 4b 65 79 2c 20 22 22 2c  , u.bn.nKey, "",
1e98c 20 30 2c 20 30 2c 20 70 4f 70 2d 3e 70 33 2c 0a   0, 0, pOp->p3,.
1e98d 20 20 20 20 20 20 20 20 20 20 28 28 70 4f 70 2d            ((pOp-
1e98e 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 55 53 45  >p5 & OPFLAG_USE
1e98f 53 45 45 4b 52 45 53 55 4c 54 29 20 3f 20 75 2e  SEEKRESULT) ? u.
1e990 62 6e 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c  bn.pC->seekResul
1e991 74 20 3a 20 30 29 0a 20 20 20 20 20 20 29 3b 0a  t : 0).      );.
1e992 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e        assert( u.
1e993 62 6e 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d  bn.pC->deferredM
1e994 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20  oveto==0 );.    
1e995 20 20 75 2e 62 6e 2e 70 43 2d 3e 63 61 63 68 65    u.bn.pC->cache
1e996 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
1e997 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TALE;.    }.  }.
1e998 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e999 70 63 6f 64 65 3a 20 49 64 78 44 65 6c 65 74 65  pcode: IdxDelete
1e99a 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1e99b 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20  .** The content 
1e99c 6f 66 20 50 33 20 72 65 67 69 73 74 65 72 73 20  of P3 registers 
1e99d 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 69  starting at regi
1e99e 73 74 65 72 20 50 32 20 66 6f 72 6d 0a 2a 2a 20  ster P2 form.** 
1e99f 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65  an unpacked inde
1e9a0 78 20 6b 65 79 2e 20 54 68 69 73 20 6f 70 63 6f  x key. This opco
1e9a1 64 65 20 72 65 6d 6f 76 65 73 20 74 68 61 74 20  de removes that 
1e9a2 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 0a  entry from the .
1e9a3 2a 2a 20 69 6e 64 65 78 20 6f 70 65 6e 65 64 20  ** index opened 
1e9a4 62 79 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2f  by cursor P1..*/
1e9a5 0a 63 61 73 65 20 4f 50 5f 49 64 78 44 65 6c 65  .case OP_IdxDele
1e9a6 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  te: {.#if 0  /* 
1e9a7 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1e9a8 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6f 20  moved into u.bo 
1e9a9 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  */.  VdbeCursor 
1e9aa 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  *pC;.  BtCursor 
1e9ab 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65  *pCrsr;.  int re
1e9ac 73 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63  s;.  UnpackedRec
1e9ad 6f 72 64 20 72 3b 0a 23 65 6e 64 69 66 20 2f 2a  ord r;.#endif /*
1e9ae 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e9af 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6f   moved into u.bo
1e9b0 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
1e9b1 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20 20 61 73  Op->p3>0 );.  as
1e9b2 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1e9b3 26 26 20 70 4f 70 2d 3e 70 32 2b 70 4f 70 2d 3e  && pOp->p2+pOp->
1e9b4 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b  p3<=p->nMem+1 );
1e9b5 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e9b6 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e9b7 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e9b8 20 75 2e 62 6f 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bo.pC = p->ap
1e9b9 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e9ba 61 73 73 65 72 74 28 20 75 2e 62 6f 2e 70 43 21  assert( u.bo.pC!
1e9bb 3d 30 20 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 72  =0 );.  u.bo.pCr
1e9bc 73 72 20 3d 20 75 2e 62 6f 2e 70 43 2d 3e 70 43  sr = u.bo.pC->pC
1e9bd 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57  ursor;.  if( ALW
1e9be 41 59 53 28 75 2e 62 6f 2e 70 43 72 73 72 21 3d  AYS(u.bo.pCrsr!=
1e9bf 30 29 20 29 7b 0a 20 20 20 20 75 2e 62 6f 2e 72  0) ){.    u.bo.r
1e9c0 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 75 2e 62 6f  .pKeyInfo = u.bo
1e9c1 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20  .pC->pKeyInfo;. 
1e9c2 20 20 20 75 2e 62 6f 2e 72 2e 6e 46 69 65 6c 64     u.bo.r.nField
1e9c3 20 3d 20 28 75 31 36 29 70 4f 70 2d 3e 70 33 3b   = (u16)pOp->p3;
1e9c4 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 66 6c 61 67  .    u.bo.r.flag
1e9c5 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 6f 2e  s = 0;.    u.bo.
1e9c6 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70  r.aMem = &aMem[p
1e9c7 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 72 63 20  Op->p2];.    rc 
1e9c8 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
1e9c9 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62  vetoUnpacked(u.b
1e9ca 6f 2e 70 43 72 73 72 2c 20 26 75 2e 62 6f 2e 72  o.pCrsr, &u.bo.r
1e9cb 2c 20 30 2c 20 30 2c 20 26 75 2e 62 6f 2e 72 65  , 0, 0, &u.bo.re
1e9cc 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  s);.    if( rc==
1e9cd 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e 62  SQLITE_OK && u.b
1e9ce 6f 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20  o.res==0 ){.    
1e9cf 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1e9d0 72 65 65 44 65 6c 65 74 65 28 75 2e 62 6f 2e 70  reeDelete(u.bo.p
1e9d1 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Crsr);.    }.   
1e9d2 20 61 73 73 65 72 74 28 20 75 2e 62 6f 2e 70 43   assert( u.bo.pC
1e9d3 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f  ->deferredMoveto
1e9d4 3d 3d 30 20 29 3b 0a 20 20 20 20 75 2e 62 6f 2e  ==0 );.    u.bo.
1e9d5 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1e9d6 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
1e9d7 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1e9d8 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 52 6f 77  * Opcode: IdxRow
1e9d9 69 64 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  id P1 P2 * * *.*
1e9da 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20  *.** Write into 
1e9db 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 20 69  register P2 an i
1e9dc 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73 20  nteger which is 
1e9dd 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69  the last entry i
1e9de 6e 20 74 68 65 20 72 65 63 6f 72 64 20 61 74 0a  n the record at.
1e9df 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  ** the end of th
1e9e0 65 20 69 6e 64 65 78 20 6b 65 79 20 70 6f 69 6e  e index key poin
1e9e1 74 65 64 20 74 6f 20 62 79 20 63 75 72 73 6f 72  ted to by cursor
1e9e2 20 50 31 2e 20 20 54 68 69 73 20 69 6e 74 65 67   P1.  This integ
1e9e3 65 72 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20  er should be.** 
1e9e4 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
1e9e5 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 6f 20   table entry to 
1e9e6 77 68 69 63 68 20 74 68 69 73 20 69 6e 64 65 78  which this index
1e9e7 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 2e 0a 2a   entry points..*
1e9e8 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 52  *.** See also: R
1e9e9 6f 77 69 64 2c 20 4d 61 6b 65 52 65 63 6f 72 64  owid, MakeRecord
1e9ea 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78  ..*/.case OP_Idx
1e9eb 52 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20  Rowid: {        
1e9ec 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1e9ed 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20  erelease */.#if 
1e9ee 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e9ef 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e9f0 20 75 2e 62 70 20 2a 2f 0a 20 20 42 74 43 75 72   u.bp */.  BtCur
1e9f1 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 56 64  sor *pCrsr;.  Vd
1e9f2 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1e9f3 69 36 34 20 72 6f 77 69 64 3b 0a 23 65 6e 64 69  i64 rowid;.#endi
1e9f4 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e9f5 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e9f6 75 2e 62 70 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.bp */..  asser
1e9f7 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1e9f8 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1e9f9 73 6f 72 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43  sor );.  u.bp.pC
1e9fa 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1e9fb 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1e9fc 75 2e 62 70 2e 70 43 21 3d 30 20 29 3b 0a 20 20  u.bp.pC!=0 );.  
1e9fd 75 2e 62 70 2e 70 43 72 73 72 20 3d 20 75 2e 62  u.bp.pCrsr = u.b
1e9fe 70 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  p.pC->pCursor;. 
1e9ff 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
1ea00 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 69 66 28 20 41  EM_Null;.  if( A
1ea01 4c 57 41 59 53 28 75 2e 62 70 2e 70 43 72 73 72  LWAYS(u.bp.pCrsr
1ea02 21 3d 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  !=0) ){.    rc =
1ea03 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73   sqlite3VdbeCurs
1ea04 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 70 2e 70 43  orMoveto(u.bp.pC
1ea05 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52  );.    if( NEVER
1ea06 28 72 63 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (rc) ) goto abor
1ea07 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1ea08 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 70      assert( u.bp
1ea09 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76  .pC->deferredMov
1ea0a 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73  eto==0 );.    as
1ea0b 73 65 72 74 28 20 75 2e 62 70 2e 70 43 2d 3e 69  sert( u.bp.pC->i
1ea0c 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20  sTable==0 );.   
1ea0d 20 69 66 28 20 21 75 2e 62 70 2e 70 43 2d 3e 6e   if( !u.bp.pC->n
1ea0e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20  ullRow ){.      
1ea0f 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1ea10 49 64 78 52 6f 77 69 64 28 64 62 2c 20 75 2e 62  IdxRowid(db, u.b
1ea11 70 2e 70 43 72 73 72 2c 20 26 75 2e 62 70 2e 72  p.pCrsr, &u.bp.r
1ea12 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28  owid);.      if(
1ea13 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1ea14 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61  {.        goto a
1ea15 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1ea16 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  r;.      }.     
1ea17 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62   pOut->u.i = u.b
1ea18 70 2e 72 6f 77 69 64 3b 0a 20 20 20 20 20 20 70  p.rowid;.      p
1ea19 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
1ea1a 5f 49 6e 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  _Int;.    }.  }.
1ea1b 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1ea1c 70 63 6f 64 65 3a 20 49 64 78 47 45 20 50 31 20  pcode: IdxGE P1 
1ea1d 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
1ea1e 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 74 65  * The P4 registe
1ea1f 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69  r values beginni
1ea20 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20  ng with P3 form 
1ea21 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65  an unpacked inde
1ea22 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f  x .** key that o
1ea23 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20  mits the ROWID. 
1ea24 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b 65   Compare this ke
1ea25 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74 20  y value against 
1ea26 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68  the index .** th
1ea27 61 74 20 50 31 20 69 73 20 63 75 72 72 65 6e 74  at P1 is current
1ea28 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20  ly pointing to, 
1ea29 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f 57  ignoring the ROW
1ea2a 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e 64  ID on the P1 ind
1ea2b 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ex..**.** If the
1ea2c 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20   P1 index entry 
1ea2d 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
1ea2e 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
1ea2f 6b 65 79 20 76 61 6c 75 65 0a 2a 2a 20 74 68 65  key value.** the
1ea30 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 4f  n jump to P2.  O
1ea31 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68  therwise fall th
1ea32 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78  rough to the nex
1ea33 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  t instruction..*
1ea34 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f  *.** If P5 is no
1ea35 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20  n-zero then the 
1ea36 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e 63  key value is inc
1ea37 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 70 73  reased by an eps
1ea38 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 6f 72 20 74  ilon .** prior t
1ea39 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  o the comparison
1ea3a 2e 20 20 54 68 69 73 20 6d 61 6b 65 20 74 68 65  .  This make the
1ea3b 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b   opcode work lik
1ea3c 65 20 49 64 78 47 54 20 65 78 63 65 70 74 0a 2a  e IdxGT except.*
1ea3d 2a 20 74 68 61 74 20 69 66 20 74 68 65 20 6b 65  * that if the ke
1ea3e 79 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20  y from register 
1ea3f 50 33 20 69 73 20 61 20 70 72 65 66 69 78 20 6f  P3 is a prefix o
1ea40 66 20 74 68 65 20 6b 65 79 20 69 6e 20 74 68 65  f the key in the
1ea41 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68 65 20   cursor,.** the 
1ea42 72 65 73 75 6c 74 20 69 73 20 66 61 6c 73 65 20  result is false 
1ea43 77 68 65 72 65 61 73 20 69 74 20 77 6f 75 6c 64  whereas it would
1ea44 20 62 65 20 74 72 75 65 20 77 69 74 68 20 49 64   be true with Id
1ea45 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  xGT..*/./* Opcod
1ea46 65 3a 20 49 64 78 4c 54 20 50 31 20 50 32 20 50  e: IdxLT P1 P2 P
1ea47 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 65  3 * P5.**.** The
1ea48 20 50 34 20 72 65 67 69 73 74 65 72 20 76 61 6c   P4 register val
1ea49 75 65 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  ues beginning wi
1ea4a 74 68 20 50 33 20 66 6f 72 6d 20 61 6e 20 75 6e  th P3 form an un
1ea4b 70 61 63 6b 65 64 20 69 6e 64 65 78 20 0a 2a 2a  packed index .**
1ea4c 20 6b 65 79 20 74 68 61 74 20 6f 6d 69 74 73 20   key that omits 
1ea4d 74 68 65 20 52 4f 57 49 44 2e 20 20 43 6f 6d 70  the ROWID.  Comp
1ea4e 61 72 65 20 74 68 69 73 20 6b 65 79 20 76 61 6c  are this key val
1ea4f 75 65 20 61 67 61 69 6e 73 74 20 74 68 65 20 69  ue against the i
1ea50 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74 20 50 31  ndex .** that P1
1ea51 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
1ea52 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67 6e 6f 72  inting to, ignor
1ea53 69 6e 67 20 74 68 65 20 52 4f 57 49 44 20 6f 6e  ing the ROWID on
1ea54 20 74 68 65 20 50 31 20 69 6e 64 65 78 2e 0a 2a   the P1 index..*
1ea55 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 31 20 69  *.** If the P1 i
1ea56 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 6c 65  ndex entry is le
1ea57 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20  ss than the key 
1ea58 76 61 6c 75 65 20 74 68 65 6e 20 6a 75 6d 70 20  value then jump 
1ea59 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 68 65 72 77  to P2..** Otherw
1ea5a 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  ise fall through
1ea5b 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73   to the next ins
1ea5c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  truction..**.** 
1ea5d 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d 7a 65 72  If P5 is non-zer
1ea5e 6f 20 74 68 65 6e 20 74 68 65 20 6b 65 79 20 76  o then the key v
1ea5f 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61 73 65  alue is increase
1ea60 64 20 62 79 20 61 6e 20 65 70 73 69 6c 6f 6e 20  d by an epsilon 
1ea61 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20 74 68 65  prior .** to the
1ea62 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54 68   comparison.  Th
1ea63 69 73 20 6d 61 6b 65 73 20 74 68 65 20 6f 70 63  is makes the opc
1ea64 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65 20 49 64  ode work like Id
1ea65 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  xLE..*/.case OP_
1ea66 49 64 78 4c 54 3a 20 20 20 20 20 20 20 20 20 20  IdxLT:          
1ea67 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 20  /* jump */.case 
1ea68 4f 50 5f 49 64 78 47 45 3a 20 7b 20 20 20 20 20  OP_IdxGE: {     
1ea69 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69     /* jump */.#i
1ea6a 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1ea6b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ea6c 74 6f 20 75 2e 62 71 20 2a 2f 0a 20 20 56 64 62  to u.bq */.  Vdb
1ea6d 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69  eCursor *pC;.  i
1ea6e 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b  nt res;.  Unpack
1ea6f 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64  edRecord r;.#end
1ea70 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1ea71 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1ea72 20 75 2e 62 71 20 2a 2f 0a 0a 20 20 61 73 73 65   u.bq */..  asse
1ea73 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1ea74 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1ea75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 71 2e 70  rsor );.  u.bq.p
1ea76 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1ea77 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1ea78 20 75 2e 62 71 2e 70 43 21 3d 30 20 29 3b 0a 20   u.bq.pC!=0 );. 
1ea79 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 71   if( ALWAYS(u.bq
1ea7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29  .pC->pCursor!=0)
1ea7b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1ea7c 75 2e 62 71 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bq.pC->deferre
1ea7d 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20  dMoveto==0 );.  
1ea7e 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1ea7f 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d  5==0 || pOp->p5=
1ea80 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =1 );.    assert
1ea81 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1ea82 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 75  4_INT32 );.    u
1ea83 2e 62 71 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d  .bq.r.pKeyInfo =
1ea84 20 75 2e 62 71 2e 70 43 2d 3e 70 4b 65 79 49 6e   u.bq.pC->pKeyIn
1ea85 66 6f 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e 6e  fo;.    u.bq.r.n
1ea86 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70  Field = (u16)pOp
1ea87 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 28 20  ->p4.i;.    if( 
1ea88 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 20  pOp->p5 ){.     
1ea89 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d 20   u.bq.r.flags = 
1ea8a 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
1ea8b 20 7c 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f   | UNPACKED_IGNO
1ea8c 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65  RE_ROWID;.    }e
1ea8d 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e  lse{.      u.bq.
1ea8e 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b  r.flags = UNPACK
1ea8f 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b  ED_IGNORE_ROWID;
1ea90 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 71 2e  .    }.    u.bq.
1ea91 72 2e 61 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70  r.aMem = &aMem[p
1ea92 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 72 63 20  Op->p3];.    rc 
1ea93 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  = sqlite3VdbeIdx
1ea94 4b 65 79 43 6f 6d 70 61 72 65 28 75 2e 62 71 2e  KeyCompare(u.bq.
1ea95 70 43 2c 20 26 75 2e 62 71 2e 72 2c 20 26 75 2e  pC, &u.bq.r, &u.
1ea96 62 71 2e 72 65 73 29 3b 0a 20 20 20 20 69 66 28  bq.res);.    if(
1ea97 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1ea98 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 20 20 20  _IdxLT ){.      
1ea99 75 2e 62 71 2e 72 65 73 20 3d 20 2d 75 2e 62 71  u.bq.res = -u.bq
1ea9a 2e 72 65 73 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  .res;.    }else{
1ea9b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1ea9c 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49  Op->opcode==OP_I
1ea9d 64 78 47 45 20 29 3b 0a 20 20 20 20 20 20 75 2e  dxGE );.      u.
1ea9e 62 71 2e 72 65 73 2b 2b 3b 0a 20 20 20 20 7d 0a  bq.res++;.    }.
1ea9f 20 20 20 20 69 66 28 20 75 2e 62 71 2e 72 65 73      if( u.bq.res
1eaa0 3e 30 20 29 7b 0a 20 20 20 20 20 20 70 63 20 3d  >0 ){.      pc =
1eaa1 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20 3b 0a 20   pOp->p2 - 1 ;. 
1eaa2 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b     }.  }.  break
1eaa3 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1eaa4 44 65 73 74 72 6f 79 20 50 31 20 50 32 20 50 33  Destroy P1 P2 P3
1eaa5 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74   * *.**.** Delet
1eaa6 65 20 61 6e 20 65 6e 74 69 72 65 20 64 61 74 61  e an entire data
1eaa7 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e  base table or in
1eaa8 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70  dex whose root p
1eaa9 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  age in the datab
1eaaa 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 73 20 67  ase.** file is g
1eaab 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a 2a 0a 2a  iven by P1..**.*
1eaac 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e  * The table bein
1eaad 67 20 64 65 73 74 72 6f 79 65 64 20 69 73 20 69  g destroyed is i
1eaae 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
1eaaf 61 73 65 20 66 69 6c 65 20 69 66 20 50 33 3d 3d  ase file if P3==
1eab0 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d 3d 31 20  0.  If.** P3==1 
1eab1 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 74  then the table t
1eab2 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69 6e  o be clear is in
1eab3 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64   the auxiliary d
1eab4 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20  atabase file.** 
1eab5 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f 20  that is used to 
1eab6 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72 65  store tables cre
1eab7 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54 45  ate using CREATE
1eab8 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c 45   TEMPORARY TABLE
1eab9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56  ..**.** If AUTOV
1eaba 41 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 64  ACUUM is enabled
1eabb 20 74 68 65 6e 20 69 74 20 69 73 20 70 6f 73 73   then it is poss
1eabc 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74 68 65  ible that anothe
1eabd 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 6d  r root page.** m
1eabe 69 67 68 74 20 62 65 20 6d 6f 76 65 64 20 69 6e  ight be moved in
1eabf 74 6f 20 74 68 65 20 6e 65 77 6c 79 20 64 65 6c  to the newly del
1eac0 65 74 65 64 20 72 6f 6f 74 20 70 61 67 65 20 69  eted root page i
1eac1 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
1eac2 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65  all.** root page
1eac3 73 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 74 20  s contiguous at 
1eac4 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
1eac5 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
1eac6 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a 20 76 61  The former.** va
1eac7 6c 75 65 20 6f 66 20 74 68 65 20 72 6f 6f 74 20  lue of the root 
1eac8 70 61 67 65 20 74 68 61 74 20 6d 6f 76 65 64 20  page that moved 
1eac9 2d 20 69 74 73 20 76 61 6c 75 65 20 62 65 66 6f  - its value befo
1eaca 72 65 20 74 68 65 20 6d 6f 76 65 20 6f 63 63 75  re the move occu
1eacb 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20 73 74 6f  rred -.** is sto
1eacc 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
1eacd 50 32 2e 20 20 49 66 20 6e 6f 20 70 61 67 65 20  P2.  If no page 
1eace 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20 77 61 73  .** movement was
1eacf 20 72 65 71 75 69 72 65 64 20 28 62 65 63 61 75   required (becau
1ead0 73 65 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  se the table bei
1ead1 6e 67 20 64 72 6f 70 70 65 64 20 77 61 73 20 61  ng dropped was a
1ead2 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68 65 20 6c  lready .** the l
1ead3 61 73 74 20 6f 6e 65 20 69 6e 20 74 68 65 20 64  ast one in the d
1ead4 61 74 61 62 61 73 65 29 20 74 68 65 6e 20 61 20  atabase) then a 
1ead5 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20 69  zero is stored i
1ead6 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a  n register P2..*
1ead7 2a 20 49 66 20 41 55 54 4f 56 41 43 55 55 4d 20  * If AUTOVACUUM 
1ead8 69 73 20 64 69 73 61 62 6c 65 64 20 74 68 65 6e  is disabled then
1ead9 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65   a zero is store
1eada 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  d in register P2
1eadb 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f  ..**.** See also
1eadc 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61 73 65 20  : Clear.*/.case 
1eadd 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b 20 20 20  OP_Destroy: {   
1eade 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1eadf 65 61 73 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f  ease */.#if 0  /
1eae0 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1eae1 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1eae2 72 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 6f 76 65  r */.  int iMove
1eae3 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20  d;.  int iCnt;. 
1eae4 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20 20   Vdbe *pVdbe;.  
1eae5 69 6e 74 20 69 44 62 3b 0a 23 65 6e 64 69 66 20  int iDb;.#endif 
1eae6 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1eae7 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1eae8 62 72 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  br */.#ifndef SQ
1eae9 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1eaea 4c 54 41 42 4c 45 0a 20 20 75 2e 62 72 2e 69 43  LTABLE.  u.br.iC
1eaeb 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 75 2e  nt = 0;.  for(u.
1eaec 62 72 2e 70 56 64 62 65 3d 64 62 2d 3e 70 56 64  br.pVdbe=db->pVd
1eaed 62 65 3b 20 75 2e 62 72 2e 70 56 64 62 65 3b 20  be; u.br.pVdbe; 
1eaee 75 2e 62 72 2e 70 56 64 62 65 20 3d 20 75 2e 62  u.br.pVdbe = u.b
1eaef 72 2e 70 56 64 62 65 2d 3e 70 4e 65 78 74 29 7b  r.pVdbe->pNext){
1eaf0 0a 20 20 20 20 69 66 28 20 75 2e 62 72 2e 70 56  .    if( u.br.pV
1eaf1 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  dbe->magic==VDBE
1eaf2 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 75 2e  _MAGIC_RUN && u.
1eaf3 62 72 2e 70 56 64 62 65 2d 3e 69 6e 56 74 61 62  br.pVdbe->inVtab
1eaf4 4d 65 74 68 6f 64 3c 32 20 26 26 20 75 2e 62 72  Method<2 && u.br
1eaf5 2e 70 56 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b  .pVdbe->pc>=0 ){
1eaf6 0a 20 20 20 20 20 20 75 2e 62 72 2e 69 43 6e 74  .      u.br.iCnt
1eaf7 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  ++;.    }.  }.#e
1eaf8 6c 73 65 0a 20 20 75 2e 62 72 2e 69 43 6e 74 20  lse.  u.br.iCnt 
1eaf9 3d 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65  = db->activeVdbe
1eafa 43 6e 74 3b 0a 23 65 6e 64 69 66 0a 20 20 70 4f  Cnt;.#endif.  pO
1eafb 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ut->flags = MEM_
1eafc 4e 75 6c 6c 3b 0a 20 20 69 66 28 20 75 2e 62 72  Null;.  if( u.br
1eafd 2e 69 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 72  .iCnt>1 ){.    r
1eafe 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  c = SQLITE_LOCKE
1eaff 44 3b 0a 20 20 20 20 70 2d 3e 65 72 72 6f 72 41  D;.    p->errorA
1eb00 63 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74  ction = OE_Abort
1eb01 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75  ;.  }else{.    u
1eb02 2e 62 72 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70  .br.iDb = pOp->p
1eb03 33 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  3;.    assert( u
1eb04 2e 62 72 2e 69 43 6e 74 3d 3d 31 20 29 3b 0a 20  .br.iCnt==1 );. 
1eb05 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62     assert( (p->b
1eb06 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 75  treeMask & (1<<u
1eb07 2e 62 72 2e 69 44 62 29 29 21 3d 30 20 29 3b 0a  .br.iDb))!=0 );.
1eb08 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1eb09 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 64  BtreeDropTable(d
1eb0a 62 2d 3e 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d  b->aDb[u.br.iDb]
1eb0b 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31 2c 20 26  .pBt, pOp->p1, &
1eb0c 75 2e 62 72 2e 69 4d 6f 76 65 64 29 3b 0a 20 20  u.br.iMoved);.  
1eb0d 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20    pOut->flags = 
1eb0e 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4f 75  MEM_Int;.    pOu
1eb0f 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 72 2e 69 4d  t->u.i = u.br.iM
1eb10 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 20 53 51  oved;.#ifndef SQ
1eb11 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1eb12 43 55 55 4d 0a 20 20 20 20 69 66 28 20 72 63 3d  CUUM.    if( rc=
1eb13 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e  =SQLITE_OK && u.
1eb14 62 72 2e 69 4d 6f 76 65 64 21 3d 30 20 29 7b 0a  br.iMoved!=0 ){.
1eb15 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6f        sqlite3Roo
1eb16 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62 2d 3e  tPageMoved(&db->
1eb17 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d 2c 20 75  aDb[u.br.iDb], u
1eb18 2e 62 72 2e 69 4d 6f 76 65 64 2c 20 70 4f 70 2d  .br.iMoved, pOp-
1eb19 3e 70 31 29 3b 0a 20 20 20 20 20 20 72 65 73 65  >p1);.      rese
1eb1a 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74 20 3d  tSchemaOnFault =
1eb1b 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66   1;.    }.#endif
1eb1c 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1eb1d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 65 61  ./* Opcode: Clea
1eb1e 72 20 50 31 20 50 32 20 50 33 0a 2a 2a 0a 2a 2a  r P1 P2 P3.**.**
1eb1f 20 44 65 6c 65 74 65 20 61 6c 6c 20 63 6f 6e 74   Delete all cont
1eb20 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61  ents of the data
1eb21 62 61 73 65 20 74 61 62 6c 65 20 6f 72 20 69 6e  base table or in
1eb22 64 65 78 20 77 68 6f 73 65 20 72 6f 6f 74 20 70  dex whose root p
1eb23 61 67 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61  age.** in the da
1eb24 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 67  tabase file is g
1eb25 69 76 65 6e 20 62 79 20 50 31 2e 20 20 42 75 74  iven by P1.  But
1eb26 2c 20 75 6e 6c 69 6b 65 20 44 65 73 74 72 6f 79  , unlike Destroy
1eb27 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72 65 6d 6f  , do not.** remo
1eb28 76 65 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  ve the table or 
1eb29 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65 20 64  index from the d
1eb2a 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
1eb2b 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65  .** The table be
1eb2c 69 6e 67 20 63 6c 65 61 72 20 69 73 20 69 6e 20  ing clear is in 
1eb2d 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1eb2e 65 20 66 69 6c 65 20 69 66 20 50 32 3d 3d 30 2e  e file if P2==0.
1eb2f 20 20 49 66 0a 2a 2a 20 50 32 3d 3d 31 20 74 68    If.** P2==1 th
1eb30 65 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  en the table to 
1eb31 62 65 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74  be clear is in t
1eb32 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  he auxiliary dat
1eb33 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68  abase file.** th
1eb34 61 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  at is used to st
1eb35 6f 72 65 20 74 61 62 6c 65 73 20 63 72 65 61 74  ore tables creat
1eb36 65 20 75 73 69 6e 67 20 43 52 45 41 54 45 20 54  e using CREATE T
1eb37 45 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a  EMPORARY TABLE..
1eb38 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 33 20  **.** If the P3 
1eb39 76 61 6c 75 65 20 69 73 20 6e 6f 6e 2d 7a 65 72  value is non-zer
1eb3a 6f 2c 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c  o, then the tabl
1eb3b 65 20 72 65 66 65 72 72 65 64 20 74 6f 20 6d 75  e referred to mu
1eb3c 73 74 20 62 65 20 61 6e 0a 2a 2a 20 69 6e 74 6b  st be an.** intk
1eb3d 65 79 20 74 61 62 6c 65 20 28 61 6e 20 53 51 4c  ey table (an SQL
1eb3e 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 6e 20 69   table, not an i
1eb3f 6e 64 65 78 29 2e 20 49 6e 20 74 68 69 73 20 63  ndex). In this c
1eb40 61 73 65 20 74 68 65 20 72 6f 77 20 63 68 61 6e  ase the row chan
1eb41 67 65 20 0a 2a 2a 20 63 6f 75 6e 74 20 69 73 20  ge .** count is 
1eb42 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74  incremented by t
1eb43 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
1eb44 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62  s in the table b
1eb45 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a  eing cleared. .*
1eb46 2a 20 49 66 20 50 33 20 69 73 20 67 72 65 61 74  * If P3 is great
1eb47 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68  er than zero, th
1eb48 65 6e 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  en the value sto
1eb49 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
1eb4a 50 33 20 69 73 0a 2a 2a 20 61 6c 73 6f 20 69 6e  P3 is.** also in
1eb4b 63 72 65 6d 65 6e 74 65 64 20 62 79 20 74 68 65  cremented by the
1eb4c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
1eb4d 69 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  in the table bei
1eb4e 6e 67 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a  ng cleared..**.*
1eb4f 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44 65 73 74  * See also: Dest
1eb50 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  roy.*/.case OP_C
1eb51 6c 65 61 72 3a 20 7b 0a 23 69 66 20 30 20 20 2f  lear: {.#if 0  /
1eb52 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1eb53 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1eb54 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 6e  s */.  int nChan
1eb55 67 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f  ge;.#endif /* lo
1eb56 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1eb57 76 65 64 20 69 6e 74 6f 20 75 2e 62 73 20 2a 2f  ved into u.bs */
1eb58 0a 0a 20 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65  ..  u.bs.nChange
1eb59 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
1eb5a 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20  (p->btreeMask & 
1eb5b 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29 21 3d 30  (1<<pOp->p2))!=0
1eb5c 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
1eb5d 65 33 42 74 72 65 65 43 6c 65 61 72 54 61 62 6c  e3BtreeClearTabl
1eb5e 65 28 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62  e(.      db->aDb
1eb5f 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74 2c 20 70  [pOp->p2].pBt, p
1eb60 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d 3e 70 33  Op->p1, (pOp->p3
1eb61 20 3f 20 26 75 2e 62 73 2e 6e 43 68 61 6e 67 65   ? &u.bs.nChange
1eb62 20 3a 20 30 29 0a 20 20 29 3b 0a 20 20 69 66 28   : 0).  );.  if(
1eb63 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20 20 20 20   pOp->p3 ){.    
1eb64 70 2d 3e 6e 43 68 61 6e 67 65 20 2b 3d 20 75 2e  p->nChange += u.
1eb65 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20  bs.nChange;.    
1eb66 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 7b  if( pOp->p3>0 ){
1eb67 0a 20 20 20 20 20 20 61 4d 65 6d 5b 70 4f 70 2d  .      aMem[pOp-
1eb68 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20 75 2e 62 73  >p3].u.i += u.bs
1eb69 2e 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20 7d 0a  .nChange;.    }.
1eb6a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1eb6b 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74  /* Opcode: Creat
1eb6c 65 54 61 62 6c 65 20 50 31 20 50 32 20 2a 20 2a  eTable P1 P2 * *
1eb6d 20 2a 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74   *.**.** Allocat
1eb6e 65 20 61 20 6e 65 77 20 74 61 62 6c 65 20 69 6e  e a new table in
1eb6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1eb70 73 65 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30  se file if P1==0
1eb71 20 6f 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75   or in the.** au
1eb72 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
1eb73 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f   file if P1==1 o
1eb74 72 20 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64  r in an attached
1eb75 20 64 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20   database if.** 
1eb76 50 31 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65  P1>1.  Write the
1eb77 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
1eb78 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  r of the new tab
1eb79 6c 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73  le into.** regis
1eb7a 74 65 72 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68 65  ter P2.**.** The
1eb7b 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
1eb7c 65 65 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20  een a table and 
1eb7d 61 6e 20 69 6e 64 65 78 20 69 73 20 74 68 69 73  an index is this
1eb7e 3a 20 20 41 20 74 61 62 6c 65 20 6d 75 73 74 0a  :  A table must.
1eb7f 2a 2a 20 68 61 76 65 20 61 20 34 2d 62 79 74 65  ** have a 4-byte
1eb80 20 69 6e 74 65 67 65 72 20 6b 65 79 20 61 6e 64   integer key and
1eb81 20 63 61 6e 20 68 61 76 65 20 61 72 62 69 74 72   can have arbitr
1eb82 61 72 79 20 64 61 74 61 2e 20 20 41 6e 20 69 6e  ary data.  An in
1eb83 64 65 78 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72  dex.** has an ar
1eb84 62 69 74 72 61 72 79 20 6b 65 79 20 62 75 74 20  bitrary key but 
1eb85 6e 6f 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53  no data..**.** S
1eb86 65 65 20 61 6c 73 6f 3a 20 43 72 65 61 74 65 49  ee also: CreateI
1eb87 6e 64 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ndex.*/./* Opcod
1eb88 65 3a 20 43 72 65 61 74 65 49 6e 64 65 78 20 50  e: CreateIndex P
1eb89 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1eb8a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
1eb8b 69 6e 64 65 78 20 69 6e 20 74 68 65 20 6d 61 69  index in the mai
1eb8c 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
1eb8d 69 66 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74  if P1==0 or in t
1eb8e 68 65 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20  he.** auxiliary 
1eb8f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66  database file if
1eb90 20 50 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20   P1==1 or in an 
1eb91 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1eb92 65 20 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57  e if.** P1>1.  W
1eb93 72 69 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61  rite the root pa
1eb94 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
1eb95 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a   new table into.
1eb96 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  ** register P2..
1eb97 2a 2a 0a 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65  **.** See docume
1eb98 6e 74 61 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72  ntation on OP_Cr
1eb99 65 61 74 65 54 61 62 6c 65 20 66 6f 72 20 61 64  eateTable for ad
1eb9a 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
1eb9b 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
1eb9c 5f 43 72 65 61 74 65 49 6e 64 65 78 3a 20 20 20  _CreateIndex:   
1eb9d 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32           /* out2
1eb9e 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 63  -prerelease */.c
1eb9f 61 73 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62  ase OP_CreateTab
1eba0 6c 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f  le: {          /
1eba1 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1eba2 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e */.#if 0  /* l
1eba3 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1eba4 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 74 20 2a  oved into u.bt *
1eba5 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20  /.  int pgno;.  
1eba6 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62 20  int flags;.  Db 
1eba7 2a 70 44 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  *pDb;.#endif /* 
1eba8 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1eba9 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 74 20  moved into u.bt 
1ebaa 2a 2f 0a 0a 20 20 75 2e 62 74 2e 70 67 6e 6f 20  */..  u.bt.pgno 
1ebab 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70  = 0;.  assert( p
1ebac 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1ebad 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  ->p1<db->nDb );.
1ebae 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1ebaf 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f  reeMask & (1<<pO
1ebb0 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20  p->p1))!=0 );.  
1ebb1 75 2e 62 74 2e 70 44 62 20 3d 20 26 64 62 2d 3e  u.bt.pDb = &db->
1ebb2 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  aDb[pOp->p1];.  
1ebb3 61 73 73 65 72 74 28 20 75 2e 62 74 2e 70 44 62  assert( u.bt.pDb
1ebb4 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 69 66  ->pBt!=0 );.  if
1ebb5 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
1ebb6 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20 29 7b  P_CreateTable ){
1ebb7 0a 20 20 20 20 2f 2a 20 75 2e 62 74 2e 66 6c 61  .    /* u.bt.fla
1ebb8 67 73 20 3d 20 42 54 52 45 45 5f 49 4e 54 4b 45  gs = BTREE_INTKE
1ebb9 59 3b 20 2a 2f 0a 20 20 20 20 75 2e 62 74 2e 66  Y; */.    u.bt.f
1ebba 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 4c 45 41  lags = BTREE_LEA
1ebbb 46 44 41 54 41 7c 42 54 52 45 45 5f 49 4e 54 4b  FDATA|BTREE_INTK
1ebbc 45 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  EY;.  }else{.   
1ebbd 20 75 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54   u.bt.flags = BT
1ebbe 52 45 45 5f 5a 45 52 4f 44 41 54 41 3b 0a 20 20  REE_ZERODATA;.  
1ebbf 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
1ebc0 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c 65  BtreeCreateTable
1ebc1 28 75 2e 62 74 2e 70 44 62 2d 3e 70 42 74 2c 20  (u.bt.pDb->pBt, 
1ebc2 26 75 2e 62 74 2e 70 67 6e 6f 2c 20 75 2e 62 74  &u.bt.pgno, u.bt
1ebc3 2e 66 6c 61 67 73 29 3b 0a 20 20 70 4f 75 74 2d  .flags);.  pOut-
1ebc4 3e 75 2e 69 20 3d 20 75 2e 62 74 2e 70 67 6e 6f  >u.i = u.bt.pgno
1ebc5 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1ebc6 20 4f 70 63 6f 64 65 3a 20 50 61 72 73 65 53 63   Opcode: ParseSc
1ebc7 68 65 6d 61 20 50 31 20 50 32 20 2a 20 50 34 20  hema P1 P2 * P4 
1ebc8 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 64  *.**.** Read and
1ebc9 20 70 61 72 73 65 20 61 6c 6c 20 65 6e 74 72 69   parse all entri
1ebca 65 73 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 49  es from the SQLI
1ebcb 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20  TE_MASTER table 
1ebcc 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 0a 2a  of database P1.*
1ebcd 2a 20 74 68 61 74 20 6d 61 74 63 68 20 74 68 65  * that match the
1ebce 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 50 34   WHERE clause P4
1ebcf 2e 20 20 50 32 20 69 73 20 74 68 65 20 22 66 6f  .  P2 is the "fo
1ebd0 72 63 65 22 20 66 6c 61 67 2e 20 20 20 41 6c 77  rce" flag.   Alw
1ebd1 61 79 73 20 64 6f 0a 2a 2a 20 74 68 65 20 70 61  ays do.** the pa
1ebd2 72 73 69 6e 67 20 69 66 20 50 32 20 69 73 20 74  rsing if P2 is t
1ebd3 72 75 65 2e 20 20 49 66 20 50 32 20 69 73 20 66  rue.  If P2 is f
1ebd4 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20  alse, then this 
1ebd5 72 6f 75 74 69 6e 65 20 69 73 20 61 0a 2a 2a 20  routine is a.** 
1ebd6 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73 63 68  no-op if the sch
1ebd7 65 6d 61 20 69 73 20 6e 6f 74 20 63 75 72 72 65  ema is not curre
1ebd8 6e 74 6c 79 20 6c 6f 61 64 65 64 2e 20 20 49 6e  ntly loaded.  In
1ebd9 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66   other words, if
1ebda 20 50 32 0a 2a 2a 20 69 73 20 66 61 6c 73 65 2c   P2.** is false,
1ebdb 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54   the SQLITE_MAST
1ebdc 45 52 20 74 61 62 6c 65 20 69 73 20 6f 6e 6c 79  ER table is only
1ebdd 20 70 61 72 73 65 64 20 69 66 20 74 68 65 20 72   parsed if the r
1ebde 65 73 74 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63  est of the.** sc
1ebdf 68 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79 20  hema is already 
1ebe0 6c 6f 61 64 65 64 20 69 6e 74 6f 20 74 68 65 20  loaded into the 
1ebe1 73 79 6d 62 6f 6c 20 74 61 62 6c 65 2e 0a 2a 2a  symbol table..**
1ebe2 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20  .** This opcode 
1ebe3 69 6e 76 6f 6b 65 73 20 74 68 65 20 70 61 72 73  invokes the pars
1ebe4 65 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  er to create a n
1ebe5 65 77 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  ew virtual machi
1ebe6 6e 65 2c 0a 2a 2a 20 74 68 65 6e 20 72 75 6e 73  ne,.** then runs
1ebe7 20 74 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c   the new virtual
1ebe8 20 6d 61 63 68 69 6e 65 2e 20 20 49 74 20 69 73   machine.  It is
1ebe9 20 74 68 75 73 20 61 20 72 65 2d 65 6e 74 72 61   thus a re-entra
1ebea 6e 74 20 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61  nt opcode..*/.ca
1ebeb 73 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  se OP_ParseSchem
1ebec 61 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  a: {.#if 0  /* l
1ebed 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1ebee 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 75 20 2a  oved into u.bu *
1ebef 2f 0a 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 63  /.  int iDb;.  c
1ebf0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74  onst char *zMast
1ebf1 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c  er;.  char *zSql
1ebf2 3b 0a 20 20 49 6e 69 74 44 61 74 61 20 69 6e 69  ;.  InitData ini
1ebf3 74 44 61 74 61 3b 0a 23 65 6e 64 69 66 20 2f 2a  tData;.#endif /*
1ebf4 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1ebf5 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 75   moved into u.bu
1ebf6 20 2a 2f 0a 0a 20 20 75 2e 62 75 2e 69 44 62 20   */..  u.bu.iDb 
1ebf7 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73  = pOp->p1;.  ass
1ebf8 65 72 74 28 20 75 2e 62 75 2e 69 44 62 3e 3d 30  ert( u.bu.iDb>=0
1ebf9 20 26 26 20 75 2e 62 75 2e 69 44 62 3c 64 62 2d   && u.bu.iDb<db-
1ebfa 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  >nDb );..  /* If
1ebfb 20 70 4f 70 2d 3e 70 32 20 69 73 20 30 2c 20 74   pOp->p2 is 0, t
1ebfc 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 20  hen this opcode 
1ebfd 69 73 20 62 65 69 6e 67 20 65 78 65 63 75 74 65  is being execute
1ebfe 64 20 74 6f 20 72 65 61 64 20 61 0a 20 20 2a 2a  d to read a.  **
1ebff 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20 66 6f 72   single row, for
1ec00 20 65 78 61 6d 70 6c 65 20 74 68 65 20 72 6f 77   example the row
1ec01 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74   corresponding t
1ec02 6f 20 61 20 6e 65 77 20 69 6e 64 65 78 0a 20 20  o a new index.  
1ec03 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 74 68  ** created by th
1ec04 69 73 20 56 44 42 45 2c 20 66 72 6f 6d 20 74 68  is VDBE, from th
1ec05 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
1ec06 74 61 62 6c 65 2e 20 49 74 20 6f 6e 6c 79 0a 20  table. It only. 
1ec07 20 2a 2a 20 64 6f 65 73 20 74 68 69 73 20 69 66   ** does this if
1ec08 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
1ec09 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 63 68  ng in-memory sch
1ec0a 65 6d 61 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ema is currently
1ec0b 0a 20 20 2a 2a 20 6c 6f 61 64 65 64 2e 20 4f 74  .  ** loaded. Ot
1ec0c 68 65 72 77 69 73 65 2c 20 74 68 65 20 6e 65 77  herwise, the new
1ec0d 20 69 6e 64 65 78 20 64 65 66 69 6e 69 74 69 6f   index definitio
1ec0e 6e 20 63 61 6e 20 62 65 20 6c 6f 61 64 65 64 20  n can be loaded 
1ec0f 61 6c 6f 6e 67 0a 20 20 2a 2a 20 77 69 74 68 20  along.  ** with 
1ec10 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 20  the rest of the 
1ec11 73 63 68 65 6d 61 20 77 68 65 6e 20 69 74 20 69  schema when it i
1ec12 73 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2a  s required..  **
1ec13 0a 20 20 2a 2a 20 41 6c 74 68 6f 75 67 68 20 74  .  ** Although t
1ec14 68 65 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20  he mutex on the 
1ec15 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20  BtShared object 
1ec16 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73  that corresponds
1ec17 20 74 6f 0a 20 20 2a 2a 20 64 61 74 61 62 61 73   to.  ** databas
1ec18 65 20 75 2e 62 75 2e 69 44 62 20 28 74 68 65 20  e u.bu.iDb (the 
1ec19 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
1ec1a 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  ing the sqlite_m
1ec1b 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a  aster table.  **
1ec1c 20 72 65 61 64 20 62 79 20 74 68 69 73 20 69 6e   read by this in
1ec1d 73 74 72 75 63 74 69 6f 6e 29 20 69 73 20 63 75  struction) is cu
1ec1e 72 72 65 6e 74 6c 79 20 68 65 6c 64 2c 20 69 74  rrently held, it
1ec1f 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 74 6f   is necessary to
1ec20 0a 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65  .  ** obtain the
1ec21 20 6d 75 74 65 78 65 73 20 6f 6e 20 61 6c 6c 20   mutexes on all 
1ec22 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
1ec23 65 73 20 62 65 66 6f 72 65 20 63 68 65 63 6b 69  es before checki
1ec24 6e 67 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 73  ng if.  ** the s
1ec25 63 68 65 6d 61 20 6f 66 20 75 2e 62 75 2e 69 44  chema of u.bu.iD
1ec26 62 20 69 73 20 6c 6f 61 64 65 64 2e 20 54 68 69  b is loaded. Thi
1ec27 73 20 69 73 20 62 65 63 61 75 73 65 2c 20 61 74  s is because, at
1ec28 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a 20 20   the start of.  
1ec29 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65  ** the sqlite3_e
1ec2a 78 65 63 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77  xec() call below
1ec2b 2c 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e  , SQLite will in
1ec2c 76 6f 6b 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65  voke.  ** sqlite
1ec2d 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29  3BtreeEnterAll()
1ec2e 2e 20 49 66 20 61 6c 6c 20 6d 75 74 65 78 65 73  . If all mutexes
1ec2f 20 61 72 65 20 6e 6f 74 20 61 6c 72 65 61 64 79   are not already
1ec30 20 68 65 6c 64 2c 20 74 68 65 0a 20 20 2a 2a 20   held, the.  ** 
1ec31 75 2e 62 75 2e 69 44 62 20 6d 75 74 65 78 20 6d  u.bu.iDb mutex m
1ec32 61 79 20 62 65 20 74 65 6d 70 6f 72 61 72 69 6c  ay be temporaril
1ec33 79 20 72 65 6c 65 61 73 65 64 20 74 6f 20 61 76  y released to av
1ec34 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2e 20 49 66  oid deadlock. If
1ec35 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65  .  ** this happe
1ec36 6e 73 2c 20 74 68 65 6e 20 73 6f 6d 65 20 6f 74  ns, then some ot
1ec37 68 65 72 20 74 68 72 65 61 64 20 6d 61 79 20 64  her thread may d
1ec38 65 6c 65 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d  elete the in-mem
1ec39 6f 72 79 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20  ory.  ** schema 
1ec3a 6f 66 20 64 61 74 61 62 61 73 65 20 75 2e 62 75  of database u.bu
1ec3b 2e 69 44 62 20 62 65 66 6f 72 65 20 74 68 65 20  .iDb before the 
1ec3c 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 72 75  SQL statement ru
1ec3d 6e 73 2e 20 54 68 65 20 73 63 68 65 6d 61 0a 20  ns. The schema. 
1ec3e 20 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20   ** will not be 
1ec3f 72 65 6c 6f 61 64 65 64 20 62 65 63 75 61 73 65  reloaded becuase
1ec40 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75   the db->init.bu
1ec41 73 79 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20  sy flag is set. 
1ec42 54 68 69 73 0a 20 20 2a 2a 20 63 61 6e 20 72 65  This.  ** can re
1ec43 73 75 6c 74 20 69 6e 20 61 20 22 6e 6f 20 73 75  sult in a "no su
1ec44 63 68 20 74 61 62 6c 65 3a 20 73 71 6c 69 74 65  ch table: sqlite
1ec45 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 6d 61 6c  _master" or "mal
1ec46 66 6f 72 6d 65 64 0a 20 20 2a 2a 20 64 61 74 61  formed.  ** data
1ec47 62 61 73 65 20 73 63 68 65 6d 61 22 20 65 72 72  base schema" err
1ec48 6f 72 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65  or being returne
1ec49 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 20  d to the user.. 
1ec4a 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 71   */.  assert( sq
1ec4b 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
1ec4c 75 74 65 78 28 64 62 2d 3e 61 44 62 5b 75 2e 62  utex(db->aDb[u.b
1ec4d 75 2e 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 20  u.iDb].pBt) );. 
1ec4e 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1ec4f 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28  erAll(db);.  if(
1ec50 20 70 4f 70 2d 3e 70 32 20 7c 7c 20 44 62 48 61   pOp->p2 || DbHa
1ec51 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 75 2e  sProperty(db, u.
1ec52 62 75 2e 69 44 62 2c 20 44 42 5f 53 63 68 65 6d  bu.iDb, DB_Schem
1ec53 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20  aLoaded) ){.    
1ec54 75 2e 62 75 2e 7a 4d 61 73 74 65 72 20 3d 20 53  u.bu.zMaster = S
1ec55 43 48 45 4d 41 5f 54 41 42 4c 45 28 75 2e 62 75  CHEMA_TABLE(u.bu
1ec56 2e 69 44 62 29 3b 0a 20 20 20 20 75 2e 62 75 2e  .iDb);.    u.bu.
1ec57 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 64 62  initData.db = db
1ec58 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 44  ;.    u.bu.initD
1ec59 61 74 61 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70  ata.iDb = pOp->p
1ec5a 31 3b 0a 20 20 20 20 75 2e 62 75 2e 69 6e 69 74  1;.    u.bu.init
1ec5b 44 61 74 61 2e 70 7a 45 72 72 4d 73 67 20 3d 20  Data.pzErrMsg = 
1ec5c 26 70 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20  &p->zErrMsg;.   
1ec5d 20 75 2e 62 75 2e 7a 53 71 6c 20 3d 20 73 71 6c   u.bu.zSql = sql
1ec5e 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 0a  ite3MPrintf(db,.
1ec5f 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e         "SELECT n
1ec60 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73  ame, rootpage, s
1ec61 71 6c 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 20  ql FROM '%q'.%s 
1ec62 57 48 45 52 45 20 25 73 22 2c 0a 20 20 20 20 20  WHERE %s",.     
1ec63 20 20 64 62 2d 3e 61 44 62 5b 75 2e 62 75 2e 69    db->aDb[u.bu.i
1ec64 44 62 5d 2e 7a 4e 61 6d 65 2c 20 75 2e 62 75 2e  Db].zName, u.bu.
1ec65 7a 4d 61 73 74 65 72 2c 20 70 4f 70 2d 3e 70 34  zMaster, pOp->p4
1ec66 2e 7a 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62  .z);.    if( u.b
1ec67 75 2e 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20  u.zSql==0 ){.   
1ec68 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
1ec69 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  OMEM;.    }else{
1ec6a 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c  .      (void)sql
1ec6b 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1ec6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1ec6d 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d   db->init.busy==
1ec6e 30 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 69  0 );.      db->i
1ec6f 6e 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a 20 20  nit.busy = 1;.  
1ec70 20 20 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74      u.bu.initDat
1ec71 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  a.rc = SQLITE_OK
1ec72 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1ec73 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
1ec74 64 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  d );.      rc = 
1ec75 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
1ec76 20 75 2e 62 75 2e 7a 53 71 6c 2c 20 73 71 6c 69   u.bu.zSql, sqli
1ec77 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2c  te3InitCallback,
1ec78 20 26 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2c   &u.bu.initData,
1ec79 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   0);.      if( r
1ec7a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c==SQLITE_OK ) r
1ec7b 63 20 3d 20 75 2e 62 75 2e 69 6e 69 74 44 61 74  c = u.bu.initDat
1ec7c 61 2e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69  a.rc;.      sqli
1ec7d 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e  te3DbFree(db, u.
1ec7e 62 75 2e 7a 53 71 6c 29 3b 0a 20 20 20 20 20 20  bu.zSql);.      
1ec7f 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20  db->init.busy = 
1ec80 30 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  0;.      (void)s
1ec81 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1ec82 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  b);.    }.  }.  
1ec83 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1ec84 65 41 6c 6c 28 64 62 29 3b 0a 20 20 69 66 28 20  eAll(db);.  if( 
1ec85 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
1ec86 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f   ){.    goto no_
1ec87 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  mem;.  }.  break
1ec88 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  ;.}..#if !define
1ec89 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e  d(SQLITE_OMIT_AN
1ec8a 41 4c 59 5a 45 29 0a 2f 2a 20 4f 70 63 6f 64 65  ALYZE)./* Opcode
1ec8b 3a 20 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20 50  : LoadAnalysis P
1ec8c 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
1ec8d 52 65 61 64 20 74 68 65 20 73 71 6c 69 74 65 5f  Read the sqlite_
1ec8e 73 74 61 74 31 20 74 61 62 6c 65 20 66 6f 72 20  stat1 table for 
1ec8f 64 61 74 61 62 61 73 65 20 50 31 20 61 6e 64 20  database P1 and 
1ec90 6c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74  load the content
1ec91 0a 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c  .** of that tabl
1ec92 65 20 69 6e 74 6f 20 74 68 65 20 69 6e 74 65 72  e into the inter
1ec93 6e 61 6c 20 69 6e 64 65 78 20 68 61 73 68 20 74  nal index hash t
1ec94 61 62 6c 65 2e 20 20 54 68 69 73 20 77 69 6c 6c  able.  This will
1ec95 20 63 61 75 73 65 0a 2a 2a 20 74 68 65 20 61 6e   cause.** the an
1ec96 61 6c 79 73 69 73 20 74 6f 20 62 65 20 75 73 65  alysis to be use
1ec97 64 20 77 68 65 6e 20 70 72 65 70 61 72 69 6e 67  d when preparing
1ec98 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20   all subsequent 
1ec99 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 63 61 73 65  queries..*/.case
1ec9a 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73   OP_LoadAnalysis
1ec9b 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  : {.  assert( pO
1ec9c 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d  p->p1>=0 && pOp-
1ec9d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  >p1<db->nDb );. 
1ec9e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 6e 61   rc = sqlite3Ana
1ec9f 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 70 4f  lysisLoad(db, pO
1eca0 70 2d 3e 70 31 29 3b 0a 20 20 62 72 65 61 6b 3b  p->p1);.  break;
1eca1 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21    .}.#endif /* !
1eca2 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1eca3 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 20 2a 2f 0a  MIT_ANALYZE) */.
1eca4 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70  ./* Opcode: Drop
1eca5 54 61 62 6c 65 20 50 31 20 2a 20 2a 20 50 34 20  Table P1 * * P4 
1eca6 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74  *.**.** Remove t
1eca7 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d  he internal (in-
1eca8 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72  memory) data str
1eca9 75 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73  uctures that des
1ecaa 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 74 61 62  cribe.** the tab
1ecab 6c 65 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64  le named P4 in d
1ecac 61 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69  atabase P1.  Thi
1ecad 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  s is called afte
1ecae 72 20 61 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20  r a table.** is 
1ecaf 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72  dropped in order
1ecb0 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74   to keep the int
1ecb1 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
1ecb2 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73  tion of the.** s
1ecb3 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74  chema consistent
1ecb4 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e   with what is on
1ecb5 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f   disk..*/.case O
1ecb6 50 5f 44 72 6f 70 54 61 62 6c 65 3a 20 7b 0a 20  P_DropTable: {. 
1ecb7 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e   sqlite3UnlinkAn
1ecb8 64 44 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c  dDeleteTable(db,
1ecb9 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70   pOp->p1, pOp->p
1ecba 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  4.z);.  break;.}
1ecbb 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f  ../* Opcode: Dro
1ecbc 70 49 6e 64 65 78 20 50 31 20 2a 20 2a 20 50 34  pIndex P1 * * P4
1ecbd 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20   *.**.** Remove 
1ecbe 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e  the internal (in
1ecbf 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74  -memory) data st
1ecc0 72 75 63 74 75 72 65 73 20 74 68 61 74 20 64 65  ructures that de
1ecc1 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20 69 6e  scribe.** the in
1ecc2 64 65 78 20 6e 61 6d 65 64 20 50 34 20 69 6e 20  dex named P4 in 
1ecc3 64 61 74 61 62 61 73 65 20 50 31 2e 20 20 54 68  database P1.  Th
1ecc4 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74  is is called aft
1ecc5 65 72 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 69  er an index.** i
1ecc6 73 20 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64  s dropped in ord
1ecc7 65 72 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69  er to keep the i
1ecc8 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e  nternal represen
1ecc9 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  tation of the.**
1ecca 20 73 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65   schema consiste
1eccb 6e 74 20 77 69 74 68 20 77 68 61 74 20 69 73 20  nt with what is 
1eccc 6f 6e 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65  on disk..*/.case
1eccd 20 4f 50 5f 44 72 6f 70 49 6e 64 65 78 3a 20 7b   OP_DropIndex: {
1ecce 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  .  sqlite3Unlink
1eccf 41 6e 64 44 65 6c 65 74 65 49 6e 64 65 78 28 64  AndDeleteIndex(d
1ecd0 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d  b, pOp->p1, pOp-
1ecd1 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b  >p4.z);.  break;
1ecd2 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44  .}../* Opcode: D
1ecd3 72 6f 70 54 72 69 67 67 65 72 20 50 31 20 2a 20  ropTrigger P1 * 
1ecd4 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d  * P4 *.**.** Rem
1ecd5 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ove the internal
1ecd6 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74   (in-memory) dat
1ecd7 61 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61  a structures tha
1ecd8 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68  t describe.** th
1ecd9 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 64 20  e trigger named 
1ecda 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50  P4 in database P
1ecdb 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c  1.  This is call
1ecdc 65 64 20 61 66 74 65 72 20 61 20 74 72 69 67 67  ed after a trigg
1ecdd 65 72 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64  er.** is dropped
1ecde 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65   in order to kee
1ecdf 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72  p the internal r
1ece0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
1ece1 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63   the.** schema c
1ece2 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77  onsistent with w
1ece3 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a  hat is on disk..
1ece4 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54  */.case OP_DropT
1ece5 72 69 67 67 65 72 3a 20 7b 0a 20 20 73 71 6c 69  rigger: {.  sqli
1ece6 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65  te3UnlinkAndDele
1ece7 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 4f  teTrigger(db, pO
1ece8 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a  p->p1, pOp->p4.z
1ece9 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a  );.  break;.}...
1ecea 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1eceb 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48  MIT_INTEGRITY_CH
1ecec 45 43 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  ECK./* Opcode: I
1eced 6e 74 65 67 72 69 74 79 43 6b 20 50 31 20 50 32  ntegrityCk P1 P2
1ecee 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 44   P3 * P5.**.** D
1ecef 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66  o an analysis of
1ecf0 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20 6f   the currently o
1ecf1 70 65 6e 20 64 61 74 61 62 61 73 65 2e 20 20 53  pen database.  S
1ecf2 74 6f 72 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73  tore in.** regis
1ecf3 74 65 72 20 50 31 20 74 68 65 20 74 65 78 74 20  ter P1 the text 
1ecf4 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  of an error mess
1ecf5 61 67 65 20 64 65 73 63 72 69 62 69 6e 67 20 61  age describing a
1ecf6 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20  ny problems..** 
1ecf7 49 66 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61  If no problems a
1ecf8 72 65 20 66 6f 75 6e 64 2c 20 73 74 6f 72 65 20  re found, store 
1ecf9 61 20 4e 55 4c 4c 20 69 6e 20 72 65 67 69 73 74  a NULL in regist
1ecfa 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  er P1..**.** The
1ecfb 20 72 65 67 69 73 74 65 72 20 50 33 20 63 6f 6e   register P3 con
1ecfc 74 61 69 6e 73 20 74 68 65 20 6d 61 78 69 6d 75  tains the maximu
1ecfd 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f  m number of allo
1ecfe 77 65 64 20 65 72 72 6f 72 73 2e 0a 2a 2a 20 41  wed errors..** A
1ecff 74 20 6d 6f 73 74 20 72 65 67 28 50 33 29 20 65  t most reg(P3) e
1ed00 72 72 6f 72 73 20 77 69 6c 6c 20 62 65 20 72 65  rrors will be re
1ed01 70 6f 72 74 65 64 2e 0a 2a 2a 20 49 6e 20 6f 74  ported..** In ot
1ed02 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 61  her words, the a
1ed03 6e 61 6c 79 73 69 73 20 73 74 6f 70 73 20 61 73  nalysis stops as
1ed04 20 73 6f 6f 6e 20 61 73 20 72 65 67 28 50 31 29   soon as reg(P1)
1ed05 20 65 72 72 6f 72 73 20 61 72 65 20 0a 2a 2a 20   errors are .** 
1ed06 73 65 65 6e 2e 20 20 52 65 67 28 50 31 29 20 69  seen.  Reg(P1) i
1ed07 73 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74  s updated with t
1ed08 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  he number of err
1ed09 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 2e 0a 2a  ors remaining..*
1ed0a 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61  *.** The root pa
1ed0b 67 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 6c  ge numbers of al
1ed0c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  l tables in the 
1ed0d 64 61 74 61 62 61 73 65 20 61 72 65 20 69 6e 74  database are int
1ed0e 65 67 65 72 0a 2a 2a 20 73 74 6f 72 65 64 20 69  eger.** stored i
1ed0f 6e 20 72 65 67 28 50 31 29 2c 20 72 65 67 28 50  n reg(P1), reg(P
1ed10 31 2b 31 29 2c 20 72 65 67 28 50 31 2b 32 29 2c  1+1), reg(P1+2),
1ed11 20 2e 2e 2e 2e 20 20 54 68 65 72 65 20 61 72 65   ....  There are
1ed12 20 50 32 20 74 61 62 6c 65 73 0a 2a 2a 20 74 6f   P2 tables.** to
1ed13 74 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35  tal..**.** If P5
1ed14 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68   is not zero, th
1ed15 65 20 63 68 65 63 6b 20 69 73 20 64 6f 6e 65 20  e check is done 
1ed16 6f 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  on the auxiliary
1ed17 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
1ed18 65 2c 20 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20  e, not the main 
1ed19 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
1ed1a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65  *.** This opcode
1ed1b 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c   is used to impl
1ed1c 65 6d 65 6e 74 20 74 68 65 20 69 6e 74 65 67 72  ement the integr
1ed1d 69 74 79 5f 63 68 65 63 6b 20 70 72 61 67 6d 61  ity_check pragma
1ed1e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74  ..*/.case OP_Int
1ed1f 65 67 72 69 74 79 43 6b 3a 20 7b 0a 23 69 66 20  egrityCk: {.#if 
1ed20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1ed21 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1ed22 20 75 2e 62 76 20 2a 2f 0a 20 20 69 6e 74 20 6e   u.bv */.  int n
1ed23 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75  Root;      /* Nu
1ed24 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74  mber of tables t
1ed25 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65  o check.  (Numbe
1ed26 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e  r of root pages.
1ed27 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f 6f  ) */.  int *aRoo
1ed28 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20  t;     /* Array 
1ed29 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d 62  of rootpage numb
1ed2a 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73 20 74  ers for tables t
1ed2b 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a  o be checked */.
1ed2c 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20 20    int j;        
1ed2d 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
1ed2e 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b  r */.  int nErr;
1ed2f 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1ed30 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70 6f 72   of errors repor
1ed31 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ted */.  char *z
1ed32 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74  ;        /* Text
1ed33 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72 65   of the error re
1ed34 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  port */.  Mem *p
1ed35 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52 65 67  nErr;     /* Reg
1ed36 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20 74 72  ister keeping tr
1ed37 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20 72 65  ack of errors re
1ed38 6d 61 69 6e 69 6e 67 20 2a 2f 0a 23 65 6e 64 69  maining */.#endi
1ed39 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1ed3a 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1ed3b 75 2e 62 76 20 2a 2f 0a 0a 20 20 75 2e 62 76 2e  u.bv */..  u.bv.
1ed3c 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70 32 3b  nRoot = pOp->p2;
1ed3d 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 76 2e  .  assert( u.bv.
1ed3e 6e 52 6f 6f 74 3e 30 20 29 3b 0a 20 20 75 2e 62  nRoot>0 );.  u.b
1ed3f 76 2e 61 52 6f 6f 74 20 3d 20 73 71 6c 69 74 65  v.aRoot = sqlite
1ed40 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
1ed41 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 75 2e   sizeof(int)*(u.
1ed42 62 76 2e 6e 52 6f 6f 74 2b 31 29 20 29 3b 0a 20  bv.nRoot+1) );. 
1ed43 20 69 66 28 20 75 2e 62 76 2e 61 52 6f 6f 74 3d   if( u.bv.aRoot=
1ed44 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  =0 ) goto no_mem
1ed45 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1ed46 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33  >p3>0 && pOp->p3
1ed47 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75  <=p->nMem );.  u
1ed48 2e 62 76 2e 70 6e 45 72 72 20 3d 20 26 61 4d 65  .bv.pnErr = &aMe
1ed49 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73  m[pOp->p3];.  as
1ed4a 73 65 72 74 28 20 28 75 2e 62 76 2e 70 6e 45 72  sert( (u.bv.pnEr
1ed4b 72 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  r->flags & MEM_I
1ed4c 6e 74 29 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  nt)!=0 );.  asse
1ed4d 72 74 28 20 28 75 2e 62 76 2e 70 6e 45 72 72 2d  rt( (u.bv.pnErr-
1ed4e 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74  >flags & (MEM_St
1ed4f 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20  r|MEM_Blob))==0 
1ed50 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 61 4d 65  );.  pIn1 = &aMe
1ed51 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f  m[pOp->p1];.  fo
1ed52 72 28 75 2e 62 76 2e 6a 3d 30 3b 20 75 2e 62 76  r(u.bv.j=0; u.bv
1ed53 2e 6a 3c 75 2e 62 76 2e 6e 52 6f 6f 74 3b 20 75  .j<u.bv.nRoot; u
1ed54 2e 62 76 2e 6a 2b 2b 29 7b 0a 20 20 20 20 75 2e  .bv.j++){.    u.
1ed55 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d  bv.aRoot[u.bv.j]
1ed56 20 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33 56   = (int)sqlite3V
1ed57 64 62 65 49 6e 74 56 61 6c 75 65 28 26 70 49 6e  dbeIntValue(&pIn
1ed58 31 5b 75 2e 62 76 2e 6a 5d 29 3b 0a 20 20 7d 0a  1[u.bv.j]);.  }.
1ed59 20 20 75 2e 62 76 2e 61 52 6f 6f 74 5b 75 2e 62    u.bv.aRoot[u.b
1ed5a 76 2e 6a 5d 20 3d 20 30 3b 0a 20 20 61 73 73 65  v.j] = 0;.  asse
1ed5b 72 74 28 20 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e  rt( pOp->p5<db->
1ed5c 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nDb );.  assert(
1ed5d 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26   (p->btreeMask &
1ed5e 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 29 29 21 3d   (1<<pOp->p5))!=
1ed5f 30 20 29 3b 0a 20 20 75 2e 62 76 2e 7a 20 3d 20  0 );.  u.bv.z = 
1ed60 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65  sqlite3BtreeInte
1ed61 67 72 69 74 79 43 68 65 63 6b 28 64 62 2d 3e 61  grityCheck(db->a
1ed62 44 62 5b 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c  Db[pOp->p5].pBt,
1ed63 20 75 2e 62 76 2e 61 52 6f 6f 74 2c 20 75 2e 62   u.bv.aRoot, u.b
1ed64 76 2e 6e 52 6f 6f 74 2c 0a 20 20 20 20 20 20 20  v.nRoot,.       
1ed65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ed66 20 20 20 20 20 20 20 20 20 20 28 69 6e 74 29 75            (int)u
1ed67 2e 62 76 2e 70 6e 45 72 72 2d 3e 75 2e 69 2c 20  .bv.pnErr->u.i, 
1ed68 26 75 2e 62 76 2e 6e 45 72 72 29 3b 0a 20 20 73  &u.bv.nErr);.  s
1ed69 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1ed6a 20 75 2e 62 76 2e 61 52 6f 6f 74 29 3b 0a 20 20   u.bv.aRoot);.  
1ed6b 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 75 2e 69 20  u.bv.pnErr->u.i 
1ed6c 2d 3d 20 75 2e 62 76 2e 6e 45 72 72 3b 0a 20 20  -= u.bv.nErr;.  
1ed6d 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1ed6e 74 4e 75 6c 6c 28 70 49 6e 31 29 3b 0a 20 20 69  tNull(pIn1);.  i
1ed6f 66 28 20 75 2e 62 76 2e 6e 45 72 72 3d 3d 30 20  f( u.bv.nErr==0 
1ed70 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  ){.    assert( u
1ed71 2e 62 76 2e 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65  .bv.z==0 );.  }e
1ed72 6c 73 65 20 69 66 28 20 75 2e 62 76 2e 7a 3d 3d  lse if( u.bv.z==
1ed73 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f  0 ){.    goto no
1ed74 5f 6d 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  _mem;.  }else{. 
1ed75 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1ed76 6d 53 65 74 53 74 72 28 70 49 6e 31 2c 20 75 2e  mSetStr(pIn1, u.
1ed77 62 76 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45  bv.z, -1, SQLITE
1ed78 5f 55 54 46 38 2c 20 73 71 6c 69 74 65 33 5f 66  _UTF8, sqlite3_f
1ed79 72 65 65 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41  ree);.  }.  UPDA
1ed7a 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
1ed7b 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  pIn1);.  sqlite3
1ed7c 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
1ed7d 6e 67 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e  ng(pIn1, encodin
1ed7e 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23  g);.  break;.}.#
1ed7f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1ed80 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
1ed81 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f  HECK */../* Opco
1ed82 64 65 3a 20 52 6f 77 53 65 74 41 64 64 20 50 31  de: RowSetAdd P1
1ed83 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1ed84 49 6e 73 65 72 74 20 74 68 65 20 69 6e 74 65 67  Insert the integ
1ed85 65 72 20 76 61 6c 75 65 20 68 65 6c 64 20 62 79  er value held by
1ed86 20 72 65 67 69 73 74 65 72 20 50 32 20 69 6e 74   register P2 int
1ed87 6f 20 61 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65  o a boolean inde
1ed88 78 0a 2a 2a 20 68 65 6c 64 20 69 6e 20 72 65 67  x.** held in reg
1ed89 69 73 74 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20  ister P1..**.** 
1ed8a 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69  An assertion fai
1ed8b 6c 73 20 69 66 20 50 32 20 69 73 20 6e 6f 74 20  ls if P2 is not 
1ed8c 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63  an integer..*/.c
1ed8d 61 73 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64  ase OP_RowSetAdd
1ed8e 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 69 6e 31  : {       /* in1
1ed8f 2c 20 69 6e 32 20 2a 2f 0a 20 20 70 49 6e 31 20  , in2 */.  pIn1 
1ed90 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d  = &aMem[pOp->p1]
1ed91 3b 0a 20 20 70 49 6e 32 20 3d 20 26 61 4d 65 6d  ;.  pIn2 = &aMem
1ed92 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61 73 73  [pOp->p2];.  ass
1ed93 65 72 74 28 20 28 70 49 6e 32 2d 3e 66 6c 61 67  ert( (pIn2->flag
1ed94 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20  s & MEM_Int)!=0 
1ed95 29 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e  );.  if( (pIn1->
1ed96 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53  flags & MEM_RowS
1ed97 65 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  et)==0 ){.    sq
1ed98 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 52  lite3VdbeMemSetR
1ed99 6f 77 53 65 74 28 70 49 6e 31 29 3b 0a 20 20 20  owSet(pIn1);.   
1ed9a 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
1ed9b 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
1ed9c 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d  =0 ) goto no_mem
1ed9d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  ;.  }.  sqlite3R
1ed9e 6f 77 53 65 74 49 6e 73 65 72 74 28 70 49 6e 31  owSetInsert(pIn1
1ed9f 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 70 49 6e  ->u.pRowSet, pIn
1eda0 32 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 65 61 6b  2->u.i);.  break
1eda1 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1eda2 52 6f 77 53 65 74 52 65 61 64 20 50 31 20 50 32  RowSetRead P1 P2
1eda3 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 45 78   P3 * *.**.** Ex
1eda4 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65  tract the smalle
1eda5 73 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 62 6f  st value from bo
1eda6 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 61  olean index P1 a
1eda7 6e 64 20 70 75 74 20 74 68 61 74 20 76 61 6c 75  nd put that valu
1eda8 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74  e into.** regist
1eda9 65 72 20 50 33 2e 20 20 4f 72 2c 20 69 66 20 62  er P3.  Or, if b
1edaa 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20  oolean index P1 
1edab 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70  is initially emp
1edac 74 79 2c 20 6c 65 61 76 65 20 50 33 0a 2a 2a 20  ty, leave P3.** 
1edad 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 6a 75  unchanged and ju
1edae 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f  mp to instructio
1edaf 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  n P2..*/.case OP
1edb0 5f 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20 20  _RowSetRead: {  
1edb1 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1edb2 31 2c 20 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30  1, out3 */.#if 0
1edb3 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1edb4 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1edb5 75 2e 62 77 20 2a 2f 0a 20 20 69 36 34 20 76 61  u.bw */.  i64 va
1edb6 6c 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  l;.#endif /* loc
1edb7 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1edb8 65 64 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a  ed into u.bw */.
1edb9 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45    CHECK_FOR_INTE
1edba 52 52 55 50 54 3b 0a 20 20 70 49 6e 31 20 3d 20  RRUPT;.  pIn1 = 
1edbb 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  &aMem[pOp->p1];.
1edbc 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1edbd 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
1edbe 3d 3d 30 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65  ==0.   || sqlite
1edbf 33 52 6f 77 53 65 74 4e 65 78 74 28 70 49 6e 31  3RowSetNext(pIn1
1edc0 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 26 75 2e  ->u.pRowSet, &u.
1edc1 62 77 2e 76 61 6c 29 3d 3d 30 0a 20 20 29 7b 0a  bw.val)==0.  ){.
1edc2 20 20 20 20 2f 2a 20 54 68 65 20 62 6f 6f 6c 65      /* The boole
1edc3 61 6e 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74  an index is empt
1edc4 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  y */.    sqlite3
1edc5 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70  VdbeMemSetNull(p
1edc6 49 6e 31 29 3b 0a 20 20 20 20 70 63 20 3d 20 70  In1);.    pc = p
1edc7 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65  Op->p2 - 1;.  }e
1edc8 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 41 20 76 61  lse{.    /* A va
1edc9 6c 75 65 20 77 61 73 20 70 75 6c 6c 65 64 20 66  lue was pulled f
1edca 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 2a 2f  rom the index */
1edcb 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1edcc 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 61 4d 65  MemSetInt64(&aMe
1edcd 6d 5b 70 4f 70 2d 3e 70 33 5d 2c 20 75 2e 62 77  m[pOp->p3], u.bw
1edce 2e 76 61 6c 29 3b 0a 20 20 7d 0a 20 20 62 72 65  .val);.  }.  bre
1edcf 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1edd0 3a 20 52 6f 77 53 65 74 54 65 73 74 20 50 31 20  : RowSetTest P1 
1edd1 50 32 20 50 33 20 50 34 0a 2a 2a 0a 2a 2a 20 52  P2 P3 P4.**.** R
1edd2 65 67 69 73 74 65 72 20 50 33 20 69 73 20 61 73  egister P3 is as
1edd3 73 75 6d 65 64 20 74 6f 20 68 6f 6c 64 20 61 20  sumed to hold a 
1edd4 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76  64-bit integer v
1edd5 61 6c 75 65 2e 20 49 66 20 72 65 67 69 73 74 65  alue. If registe
1edd6 72 20 50 31 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73  r P1.** contains
1edd7 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74   a RowSet object
1edd8 20 61 6e 64 20 74 68 61 74 20 52 6f 77 53 65 74   and that RowSet
1edd9 20 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73   object contains
1edda 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 68 65  .** the value he
1eddb 6c 64 20 69 6e 20 50 33 2c 20 6a 75 6d 70 20 74  ld in P3, jump t
1eddc 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 4f  o register P2. O
1eddd 74 68 65 72 77 69 73 65 2c 20 69 6e 73 65 72 74  therwise, insert
1edde 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20   the.** integer 
1eddf 69 6e 20 50 33 20 69 6e 74 6f 20 74 68 65 20 52  in P3 into the R
1ede0 6f 77 53 65 74 20 61 6e 64 20 63 6f 6e 74 69 6e  owSet and contin
1ede1 75 65 20 6f 6e 20 74 6f 20 74 68 65 0a 2a 2a 20  ue on to the.** 
1ede2 6e 65 78 74 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a  next opcode..**.
1ede3 2a 2a 20 54 68 65 20 52 6f 77 53 65 74 20 6f 62  ** The RowSet ob
1ede4 6a 65 63 74 20 69 73 20 6f 70 74 69 6d 69 7a 65  ject is optimize
1ede5 64 20 66 6f 72 20 74 68 65 20 63 61 73 65 20 77  d for the case w
1ede6 68 65 72 65 20 73 75 63 63 65 73 73 69 76 65 20  here successive 
1ede7 73 65 74 73 0a 2a 2a 20 6f 66 20 69 6e 74 65 67  sets.** of integ
1ede8 65 72 73 2c 20 77 68 65 72 65 20 65 61 63 68 20  ers, where each 
1ede9 73 65 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20  set contains no 
1edea 64 75 70 6c 69 63 61 74 65 73 2e 20 45 61 63 68  duplicates. Each
1edeb 20 73 65 74 0a 2a 2a 20 6f 66 20 76 61 6c 75 65   set.** of value
1edec 73 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20  s is identified 
1eded 62 79 20 61 20 75 6e 69 71 75 65 20 50 34 20 76  by a unique P4 v
1edee 61 6c 75 65 2e 20 54 68 65 20 66 69 72 73 74 20  alue. The first 
1edef 73 65 74 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65  set.** must have
1edf0 20 50 34 3d 3d 30 2c 20 74 68 65 20 66 69 6e 61   P4==0, the fina
1edf1 6c 20 73 65 74 20 50 34 3d 2d 31 2e 20 20 50 34  l set P4=-1.  P4
1edf2 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
1edf3 2d 31 20 6f 72 0a 2a 2a 20 6e 6f 6e 2d 6e 65 67  -1 or.** non-neg
1edf4 61 74 69 76 65 2e 20 20 46 6f 72 20 6e 6f 6e 2d  ative.  For non-
1edf5 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 73 20  negative values 
1edf6 6f 66 20 50 34 20 6f 6e 6c 79 20 74 68 65 20 6c  of P4 only the l
1edf7 6f 77 65 72 20 34 0a 2a 2a 20 62 69 74 73 20 61  ower 4.** bits a
1edf8 72 65 20 73 69 67 6e 69 66 69 63 61 6e 74 2e 0a  re significant..
1edf9 2a 2a 0a 2a 2a 20 54 68 69 73 20 61 6c 6c 6f 77  **.** This allow
1edfa 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 3a  s optimizations:
1edfb 20 28 61 29 20 77 68 65 6e 20 50 34 3d 3d 30 20   (a) when P4==0 
1edfc 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
1edfd 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74 68 65 20   to test.** the 
1edfe 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 20 66 6f  rowset object fo
1edff 72 20 50 33 2c 20 61 73 20 69 74 20 69 73 20 67  r P3, as it is g
1ee00 75 61 72 61 6e 74 65 65 64 20 6e 6f 74 20 74 6f  uaranteed not to
1ee01 20 63 6f 6e 74 61 69 6e 20 69 74 2c 0a 2a 2a 20   contain it,.** 
1ee02 28 62 29 20 77 68 65 6e 20 50 34 3d 3d 2d 31 20  (b) when P4==-1 
1ee03 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
1ee04 20 74 6f 20 69 6e 73 65 72 74 20 74 68 65 20 76   to insert the v
1ee05 61 6c 75 65 2c 20 61 73 20 69 74 20 77 69 6c 6c  alue, as it will
1ee06 0a 2a 2a 20 6e 65 76 65 72 20 62 65 20 74 65 73  .** never be tes
1ee07 74 65 64 20 66 6f 72 2c 20 61 6e 64 20 28 63 29  ted for, and (c)
1ee08 20 77 68 65 6e 20 61 20 76 61 6c 75 65 20 74 68   when a value th
1ee09 61 74 20 69 73 20 70 61 72 74 20 6f 66 20 73 65  at is part of se
1ee0a 74 20 58 20 69 73 0a 2a 2a 20 69 6e 73 65 72 74  t X is.** insert
1ee0b 65 64 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ed, there is no 
1ee0c 6e 65 65 64 20 74 6f 20 73 65 61 72 63 68 20 74  need to search t
1ee0d 6f 20 73 65 65 20 69 66 20 74 68 65 20 73 61 6d  o see if the sam
1ee0e 65 20 76 61 6c 75 65 20 77 61 73 0a 2a 2a 20 70  e value was.** p
1ee0f 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72 74  reviously insert
1ee10 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 73 65  ed as part of se
1ee11 74 20 58 20 28 6f 6e 6c 79 20 69 66 20 69 74 20  t X (only if it 
1ee12 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  was previously.*
1ee13 2a 20 69 6e 73 65 72 74 65 64 20 61 73 20 70 61  * inserted as pa
1ee14 72 74 20 6f 66 20 73 6f 6d 65 20 6f 74 68 65 72  rt of some other
1ee15 20 73 65 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f   set)..*/.case O
1ee16 50 5f 52 6f 77 53 65 74 54 65 73 74 3a 20 7b 20  P_RowSetTest: { 
1ee17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ee18 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31      /* jump, in1
1ee19 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20  , in3 */.#if 0  
1ee1a 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1ee1b 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1ee1c 62 78 20 2a 2f 0a 20 20 69 6e 74 20 69 53 65 74  bx */.  int iSet
1ee1d 3b 0a 20 20 69 6e 74 20 65 78 69 73 74 73 3b 0a  ;.  int exists;.
1ee1e 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1ee1f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1ee20 69 6e 74 6f 20 75 2e 62 78 20 2a 2f 0a 0a 20 20  into u.bx */..  
1ee21 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pIn1 = &aMem[pOp
1ee22 2d 3e 70 31 5d 3b 0a 20 20 70 49 6e 33 20 3d 20  ->p1];.  pIn3 = 
1ee23 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  &aMem[pOp->p3];.
1ee24 20 20 75 2e 62 78 2e 69 53 65 74 20 3d 20 70 4f    u.bx.iSet = pO
1ee25 70 2d 3e 70 34 2e 69 3b 0a 20 20 61 73 73 65 72  p->p4.i;.  asser
1ee26 74 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 26 4d  t( pIn3->flags&M
1ee27 45 4d 5f 49 6e 74 20 29 3b 0a 0a 20 20 2f 2a 20  EM_Int );..  /* 
1ee28 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 74  If there is anyt
1ee29 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20  hing other than 
1ee2a 61 20 72 6f 77 73 65 74 20 6f 62 6a 65 63 74 20  a rowset object 
1ee2b 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50  in memory cell P
1ee2c 31 2c 0a 20 20 2a 2a 20 64 65 6c 65 74 65 20 69  1,.  ** delete i
1ee2d 74 20 6e 6f 77 20 61 6e 64 20 69 6e 69 74 69 61  t now and initia
1ee2e 6c 69 7a 65 20 50 31 20 77 69 74 68 20 61 6e 20  lize P1 with an 
1ee2f 65 6d 70 74 79 20 72 6f 77 73 65 74 0a 20 20 2a  empty rowset.  *
1ee30 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66  /.  if( (pIn1->f
1ee31 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
1ee32 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  t)==0 ){.    sql
1ee33 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f  ite3VdbeMemSetRo
1ee34 77 53 65 74 28 70 49 6e 31 29 3b 0a 20 20 20 20  wSet(pIn1);.    
1ee35 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1ee36 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
1ee37 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b  0 ) goto no_mem;
1ee38 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
1ee39 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1ee3a 49 4e 54 33 32 20 29 3b 0a 20 20 61 73 73 65 72  INT32 );.  asser
1ee3b 74 28 20 75 2e 62 78 2e 69 53 65 74 3d 3d 2d 31  t( u.bx.iSet==-1
1ee3c 20 7c 7c 20 75 2e 62 78 2e 69 53 65 74 3e 3d 30   || u.bx.iSet>=0
1ee3d 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 78 2e 69   );.  if( u.bx.i
1ee3e 53 65 74 20 29 7b 0a 20 20 20 20 75 2e 62 78 2e  Set ){.    u.bx.
1ee3f 65 78 69 73 74 73 20 3d 20 73 71 6c 69 74 65 33  exists = sqlite3
1ee40 52 6f 77 53 65 74 54 65 73 74 28 70 49 6e 31 2d  RowSetTest(pIn1-
1ee41 3e 75 2e 70 52 6f 77 53 65 74 2c 0a 20 20 20 20  >u.pRowSet,.    
1ee42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ee43 20 20 20 20 20 20 20 20 20 20 20 28 75 38 29 28             (u8)(
1ee44 75 2e 62 78 2e 69 53 65 74 3e 3d 30 20 3f 20 75  u.bx.iSet>=0 ? u
1ee45 2e 62 78 2e 69 53 65 74 20 26 20 30 78 66 20 3a  .bx.iSet & 0xf :
1ee46 20 30 78 66 66 29 2c 0a 20 20 20 20 20 20 20 20   0xff),.        
1ee47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ee48 20 20 20 20 20 20 20 70 49 6e 33 2d 3e 75 2e 69         pIn3->u.i
1ee49 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 62 78 2e  );.    if( u.bx.
1ee4a 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20  exists ){.      
1ee4b 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1ee4c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1ee4d 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 75     }.  }.  if( u
1ee4e 2e 62 78 2e 69 53 65 74 3e 3d 30 20 29 7b 0a 20  .bx.iSet>=0 ){. 
1ee4f 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53 65 74     sqlite3RowSet
1ee50 49 6e 73 65 72 74 28 70 49 6e 31 2d 3e 75 2e 70  Insert(pIn1->u.p
1ee51 52 6f 77 53 65 74 2c 20 70 49 6e 33 2d 3e 75 2e  RowSet, pIn3->u.
1ee52 69 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  i);.  }.  break;
1ee53 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  .}...#ifndef SQL
1ee54 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1ee55 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 6f  ../* Opcode: Pro
1ee56 67 72 61 6d 20 50 31 20 50 32 20 50 33 20 50 34  gram P1 P2 P3 P4
1ee57 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65   *.**.** Execute
1ee58 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f   the trigger pro
1ee59 67 72 61 6d 20 70 61 73 73 65 64 20 61 73 20 50  gram passed as P
1ee5a 34 20 28 74 79 70 65 20 50 34 5f 53 55 42 50 52  4 (type P4_SUBPR
1ee5b 4f 47 52 41 4d 29 2e 20 0a 2a 2a 0a 2a 2a 20 50  OGRAM). .**.** P
1ee5c 31 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 61  1 contains the a
1ee5d 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6d 65  ddress of the me
1ee5e 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20 63  mory cell that c
1ee5f 6f 6e 74 61 69 6e 73 20 74 68 65 20 66 69 72 73  ontains the firs
1ee60 74 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 63 65 6c  t memory .** cel
1ee61 6c 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66  l in an array of
1ee62 20 76 61 6c 75 65 73 20 75 73 65 64 20 61 73 20   values used as 
1ee63 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
1ee64 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 50 32   sub-program. P2
1ee65 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 74 68   .** contains th
1ee66 65 20 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d  e address to jum
1ee67 70 20 74 6f 20 69 66 20 74 68 65 20 73 75 62 2d  p to if the sub-
1ee68 70 72 6f 67 72 61 6d 20 74 68 72 6f 77 73 20 61  program throws a
1ee69 6e 20 49 47 4e 4f 52 45 20 0a 2a 2a 20 65 78 63  n IGNORE .** exc
1ee6a 65 70 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65  eption using the
1ee6b 20 52 41 49 53 45 28 29 20 66 75 6e 63 74 69 6f   RAISE() functio
1ee6c 6e 2e 20 52 65 67 69 73 74 65 72 20 50 33 20 63  n. Register P3 c
1ee6d 6f 6e 74 61 69 6e 73 20 74 68 65 20 61 64 64 72  ontains the addr
1ee6e 65 73 73 20 0a 2a 2a 20 6f 66 20 61 20 6d 65 6d  ess .** of a mem
1ee6f 6f 72 79 20 63 65 6c 6c 20 69 6e 20 74 68 69 73  ory cell in this
1ee70 20 28 74 68 65 20 70 61 72 65 6e 74 29 20 56 4d   (the parent) VM
1ee71 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f   that is used to
1ee72 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 0a 2a   allocate the .*
1ee73 2a 20 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65  * memory require
1ee74 64 20 62 79 20 74 68 65 20 73 75 62 2d 76 64 62  d by the sub-vdb
1ee75 65 20 61 74 20 72 75 6e 74 69 6d 65 2e 0a 2a 2a  e at runtime..**
1ee76 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e  .** P4 is a poin
1ee77 74 65 72 20 74 6f 20 74 68 65 20 56 4d 20 63 6f  ter to the VM co
1ee78 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 72 69  ntaining the tri
1ee79 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f  gger program..*/
1ee7a 0a 63 61 73 65 20 4f 50 5f 50 72 6f 67 72 61 6d  .case OP_Program
1ee7b 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1ee7c 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  mp */.#if 0  /* 
1ee7d 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1ee7e 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 79 20  moved into u.by 
1ee7f 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20  */.  int nMem;  
1ee80 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ee81 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79  Number of memory
1ee82 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73   registers for s
1ee83 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  ub-program */.  
1ee84 69 6e 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20  int nByte;      
1ee85 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
1ee86 20 6f 66 20 72 75 6e 74 69 6d 65 20 73 70 61 63   of runtime spac
1ee87 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 73  e required for s
1ee88 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  ub-program */.  
1ee89 4d 65 6d 20 2a 70 52 74 3b 20 20 20 20 20 20 20  Mem *pRt;       
1ee8a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
1ee8b 74 65 72 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ter to allocate 
1ee8c 72 75 6e 74 69 6d 65 20 73 70 61 63 65 20 2a 2f  runtime space */
1ee8d 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20  .  Mem *pMem;   
1ee8e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
1ee8f 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  ed to iterate th
1ee90 72 6f 75 67 68 20 6d 65 6d 6f 72 79 20 63 65 6c  rough memory cel
1ee91 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 45 6e  ls */.  Mem *pEn
1ee92 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
1ee93 2f 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63  /* Last memory c
1ee94 65 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79  ell in new array
1ee95 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 6d 65 20   */.  VdbeFrame 
1ee96 2a 70 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a  *pFrame;      /*
1ee97 20 4e 65 77 20 76 64 62 65 20 66 72 61 6d 65 20   New vdbe frame 
1ee98 74 6f 20 65 78 65 63 75 74 65 20 69 6e 20 2a 2f  to execute in */
1ee99 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70  .  SubProgram *p
1ee9a 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a 20 53 75  Program;   /* Su
1ee9b 62 2d 70 72 6f 67 72 61 6d 20 74 6f 20 65 78 65  b-program to exe
1ee9c 63 75 74 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  cute */.  void *
1ee9d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
1ee9e 20 20 2f 2a 20 54 6f 6b 65 6e 20 69 64 65 6e 74    /* Token ident
1ee9f 69 66 79 69 6e 67 20 74 72 69 67 67 65 72 20 2a  ifying trigger *
1eea0 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  /.#endif /* loca
1eea1 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1eea2 64 20 69 6e 74 6f 20 75 2e 62 79 20 2a 2f 0a 0a  d into u.by */..
1eea3 20 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 20    u.by.pProgram 
1eea4 3d 20 70 4f 70 2d 3e 70 34 2e 70 50 72 6f 67 72  = pOp->p4.pProgr
1eea5 61 6d 3b 0a 20 20 75 2e 62 79 2e 70 52 74 20 3d  am;.  u.by.pRt =
1eea6 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b   &aMem[pOp->p3];
1eea7 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e  .  assert( u.by.
1eea8 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3e 30 20  pProgram->nOp>0 
1eea9 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
1eeaa 70 35 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72  p5 flag is clear
1eeab 2c 20 74 68 65 6e 20 72 65 63 75 72 73 69 76 65  , then recursive
1eeac 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
1eead 72 69 67 67 65 72 73 20 69 73 0a 20 20 2a 2a 20  riggers is.  ** 
1eeae 64 69 73 61 62 6c 65 64 20 66 6f 72 20 62 61 63  disabled for bac
1eeaf 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
1eeb0 6c 69 74 79 20 28 70 35 20 69 73 20 73 65 74 20  lity (p5 is set 
1eeb1 69 66 20 74 68 69 73 20 73 75 62 2d 70 72 6f 67  if this sub-prog
1eeb2 72 61 6d 0a 20 20 2a 2a 20 69 73 20 72 65 61 6c  ram.  ** is real
1eeb3 6c 79 20 61 20 74 72 69 67 67 65 72 2c 20 6e 6f  ly a trigger, no
1eeb4 74 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  t a foreign key 
1eeb5 61 63 74 69 6f 6e 2c 20 61 6e 64 20 74 68 65 20  action, and the 
1eeb6 66 6c 61 67 20 73 65 74 0a 20 20 2a 2a 20 61 6e  flag set.  ** an
1eeb7 64 20 63 6c 65 61 72 65 64 20 62 79 20 74 68 65  d cleared by the
1eeb8 20 22 50 52 41 47 4d 41 20 72 65 63 75 72 73 69   "PRAGMA recursi
1eeb9 76 65 5f 74 72 69 67 67 65 72 73 22 20 63 6f 6d  ve_triggers" com
1eeba 6d 61 6e 64 20 69 73 20 63 6c 65 61 72 29 2e 0a  mand is clear)..
1eebb 20 20 2a 2a 0a 20 20 2a 2a 20 49 74 20 69 73 20    **.  ** It is 
1eebc 72 65 63 75 72 73 69 76 65 20 69 6e 76 6f 63 61  recursive invoca
1eebd 74 69 6f 6e 20 6f 66 20 74 72 69 67 67 65 72 73  tion of triggers
1eebe 2c 20 61 74 20 74 68 65 20 53 51 4c 20 6c 65 76  , at the SQL lev
1eebf 65 6c 2c 20 74 68 61 74 20 69 73 0a 20 20 2a 2a  el, that is.  **
1eec0 20 64 69 73 61 62 6c 65 64 2e 20 49 6e 20 73 6f   disabled. In so
1eec1 6d 65 20 63 61 73 65 73 20 61 20 73 69 6e 67 6c  me cases a singl
1eec2 65 20 74 72 69 67 67 65 72 20 6d 61 79 20 67 65  e trigger may ge
1eec3 6e 65 72 61 74 65 20 6d 6f 72 65 20 74 68 61 6e  nerate more than
1eec4 20 6f 6e 65 0a 20 20 2a 2a 20 53 75 62 50 72 6f   one.  ** SubPro
1eec5 67 72 61 6d 20 28 69 66 20 74 68 65 20 74 72 69  gram (if the tri
1eec6 67 67 65 72 20 6d 61 79 20 62 65 20 65 78 65 63  gger may be exec
1eec7 75 74 65 64 20 77 69 74 68 20 6d 6f 72 65 20 74  uted with more t
1eec8 68 61 6e 20 6f 6e 65 20 64 69 66 66 65 72 65 6e  han one differen
1eec9 74 0a 20 20 2a 2a 20 4f 4e 20 43 4f 4e 46 4c 49  t.  ** ON CONFLI
1eeca 43 54 20 61 6c 67 6f 72 69 74 68 6d 29 2e 20 53  CT algorithm). S
1eecb 75 62 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74  ubProgram struct
1eecc 75 72 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  ures associated 
1eecd 77 69 74 68 20 61 0a 20 20 2a 2a 20 73 69 6e 67  with a.  ** sing
1eece 6c 65 20 74 72 69 67 67 65 72 20 61 6c 6c 20 68  le trigger all h
1eecf 61 76 65 20 74 68 65 20 73 61 6d 65 20 76 61 6c  ave the same val
1eed0 75 65 20 66 6f 72 20 74 68 65 20 53 75 62 50 72  ue for the SubPr
1eed1 6f 67 72 61 6d 2e 74 6f 6b 65 6e 0a 20 20 2a 2a  ogram.token.  **
1eed2 20 76 61 72 69 61 62 6c 65 2e 20 20 2a 2f 0a 20   variable.  */. 
1eed3 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a   if( pOp->p5 ){.
1eed4 20 20 20 20 75 2e 62 79 2e 74 20 3d 20 75 2e 62      u.by.t = u.b
1eed5 79 2e 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b 65  y.pProgram->toke
1eed6 6e 3b 0a 20 20 20 20 66 6f 72 28 75 2e 62 79 2e  n;.    for(u.by.
1eed7 70 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65  pFrame=p->pFrame
1eed8 3b 20 75 2e 62 79 2e 70 46 72 61 6d 65 20 26 26  ; u.by.pFrame &&
1eed9 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 74 6f   u.by.pFrame->to
1eeda 6b 65 6e 21 3d 75 2e 62 79 2e 74 3b 20 75 2e 62  ken!=u.by.t; u.b
1eedb 79 2e 70 46 72 61 6d 65 3d 75 2e 62 79 2e 70 46  y.pFrame=u.by.pF
1eedc 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  rame->pParent);.
1eedd 20 20 20 20 69 66 28 20 75 2e 62 79 2e 70 46 72      if( u.by.pFr
1eede 61 6d 65 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  ame ) break;.  }
1eedf 0a 0a 20 20 69 66 28 20 70 2d 3e 6e 46 72 61 6d  ..  if( p->nFram
1eee0 65 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  e>=db->aLimit[SQ
1eee1 4c 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47  LITE_LIMIT_TRIGG
1eee2 45 52 5f 44 45 50 54 48 5d 20 29 7b 0a 20 20 20  ER_DEPTH] ){.   
1eee3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
1eee4 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  OR;.    sqlite3S
1eee5 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1eee6 72 4d 73 67 2c 20 64 62 2c 20 22 74 6f 6f 20 6d  rMsg, db, "too m
1eee7 61 6e 79 20 6c 65 76 65 6c 73 20 6f 66 20 74 72  any levels of tr
1eee8 69 67 67 65 72 20 72 65 63 75 72 73 69 6f 6e 22  igger recursion"
1eee9 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  );.    break;.  
1eeea 7d 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72  }..  /* Register
1eeeb 20 75 2e 62 79 2e 70 52 74 20 69 73 20 75 73 65   u.by.pRt is use
1eeec 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6d  d to store the m
1eeed 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20 74  emory required t
1eeee 6f 20 73 61 76 65 20 74 68 65 20 73 74 61 74 65  o save the state
1eeef 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72  .  ** of the cur
1eef0 72 65 6e 74 20 70 72 6f 67 72 61 6d 2c 20 61 6e  rent program, an
1eef1 64 20 74 68 65 20 6d 65 6d 6f 72 79 20 72 65 71  d the memory req
1eef2 75 69 72 65 64 20 61 74 20 72 75 6e 74 69 6d 65  uired at runtime
1eef3 20 74 6f 20 65 78 65 63 75 74 65 0a 20 20 2a 2a   to execute.  **
1eef4 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f   the trigger pro
1eef5 67 72 61 6d 2e 20 49 66 20 74 68 69 73 20 74 72  gram. If this tr
1eef6 69 67 67 65 72 20 68 61 73 20 62 65 65 6e 20 66  igger has been f
1eef7 69 72 65 64 20 62 65 66 6f 72 65 2c 20 74 68 65  ired before, the
1eef8 6e 20 75 2e 62 79 2e 70 52 74 0a 20 20 2a 2a 20  n u.by.pRt.  ** 
1eef9 69 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63  is already alloc
1eefa 61 74 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  ated. Otherwise,
1eefb 20 69 74 20 6d 75 73 74 20 62 65 20 69 6e 69 74   it must be init
1eefc 69 61 6c 69 7a 65 64 2e 20 20 2a 2f 0a 20 20 69  ialized.  */.  i
1eefd 66 28 20 28 75 2e 62 79 2e 70 52 74 2d 3e 66 6c  f( (u.by.pRt->fl
1eefe 61 67 73 26 4d 45 4d 5f 46 72 61 6d 65 29 3d 3d  ags&MEM_Frame)==
1eeff 30 20 29 7b 0a 20 20 20 20 2f 2a 20 53 75 62 50  0 ){.    /* SubP
1ef00 72 6f 67 72 61 6d 2e 6e 4d 65 6d 20 69 73 20 73  rogram.nMem is s
1ef01 65 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72  et to the number
1ef02 20 6f 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73   of memory cells
1ef03 20 75 73 65 64 20 62 79 20 74 68 65 0a 20 20 20   used by the.   
1ef04 20 2a 2a 20 70 72 6f 67 72 61 6d 20 73 74 6f 72   ** program stor
1ef05 65 64 20 69 6e 20 53 75 62 50 72 6f 67 72 61 6d  ed in SubProgram
1ef06 2e 61 4f 70 2e 20 41 73 20 77 65 6c 6c 20 61 73  .aOp. As well as
1ef07 20 74 68 65 73 65 2c 20 6f 6e 65 20 6d 65 6d 6f   these, one memo
1ef08 72 79 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 69  ry.    ** cell i
1ef09 73 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 65  s required for e
1ef0a 61 63 68 20 63 75 72 73 6f 72 20 75 73 65 64 20  ach cursor used 
1ef0b 62 79 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20  by the program. 
1ef0c 53 65 74 20 6c 6f 63 61 6c 0a 20 20 20 20 2a 2a  Set local.    **
1ef0d 20 76 61 72 69 61 62 6c 65 20 75 2e 62 79 2e 6e   variable u.by.n
1ef0e 4d 65 6d 20 28 61 6e 64 20 6c 61 74 65 72 2c 20  Mem (and later, 
1ef0f 56 64 62 65 46 72 61 6d 65 2e 6e 43 68 69 6c 64  VdbeFrame.nChild
1ef10 4d 65 6d 29 20 74 6f 20 74 68 69 73 20 76 61 6c  Mem) to this val
1ef11 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 75  ue..    */.    u
1ef12 2e 62 79 2e 6e 4d 65 6d 20 3d 20 75 2e 62 79 2e  .by.nMem = u.by.
1ef13 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 20 2b  pProgram->nMem +
1ef14 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d 3e   u.by.pProgram->
1ef15 6e 43 73 72 3b 0a 20 20 20 20 75 2e 62 79 2e 6e  nCsr;.    u.by.n
1ef16 42 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 73 69  Byte = ROUND8(si
1ef17 7a 65 6f 66 28 56 64 62 65 46 72 61 6d 65 29 29  zeof(VdbeFrame))
1ef18 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2b  .              +
1ef19 20 75 2e 62 79 2e 6e 4d 65 6d 20 2a 20 73 69 7a   u.by.nMem * siz
1ef1a 65 6f 66 28 4d 65 6d 29 0a 20 20 20 20 20 20 20  eof(Mem).       
1ef1b 20 20 20 20 20 20 20 2b 20 75 2e 62 79 2e 70 50         + u.by.pP
1ef1c 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 20 2a 20 73  rogram->nCsr * s
1ef1d 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72  izeof(VdbeCursor
1ef1e 20 2a 29 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46   *);.    u.by.pF
1ef1f 72 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62  rame = sqlite3Db
1ef20 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 75  MallocZero(db, u
1ef21 2e 62 79 2e 6e 42 79 74 65 29 3b 0a 20 20 20 20  .by.nByte);.    
1ef22 69 66 28 20 21 75 2e 62 79 2e 70 46 72 61 6d 65  if( !u.by.pFrame
1ef23 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e   ){.      goto n
1ef24 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  o_mem;.    }.   
1ef25 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1ef26 65 6c 65 61 73 65 28 75 2e 62 79 2e 70 52 74 29  elease(u.by.pRt)
1ef27 3b 0a 20 20 20 20 75 2e 62 79 2e 70 52 74 2d 3e  ;.    u.by.pRt->
1ef28 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 46 72 61 6d  flags = MEM_Fram
1ef29 65 3b 0a 20 20 20 20 75 2e 62 79 2e 70 52 74 2d  e;.    u.by.pRt-
1ef2a 3e 75 2e 70 46 72 61 6d 65 20 3d 20 75 2e 62 79  >u.pFrame = u.by
1ef2b 2e 70 46 72 61 6d 65 3b 0a 0a 20 20 20 20 75 2e  .pFrame;..    u.
1ef2c 62 79 2e 70 46 72 61 6d 65 2d 3e 76 20 3d 20 70  by.pFrame->v = p
1ef2d 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d  ;.    u.by.pFram
1ef2e 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 20 3d 20 75  e->nChildMem = u
1ef2f 2e 62 79 2e 6e 4d 65 6d 3b 0a 20 20 20 20 75 2e  .by.nMem;.    u.
1ef30 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c  by.pFrame->nChil
1ef31 64 43 73 72 20 3d 20 75 2e 62 79 2e 70 50 72 6f  dCsr = u.by.pPro
1ef32 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a 20 20 20 20  gram->nCsr;.    
1ef33 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 70 63 20  u.by.pFrame->pc 
1ef34 3d 20 70 63 3b 0a 20 20 20 20 75 2e 62 79 2e 70  = pc;.    u.by.p
1ef35 46 72 61 6d 65 2d 3e 61 4d 65 6d 20 3d 20 70 2d  Frame->aMem = p-
1ef36 3e 61 4d 65 6d 3b 0a 20 20 20 20 75 2e 62 79 2e  >aMem;.    u.by.
1ef37 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20 3d 20 70  pFrame->nMem = p
1ef38 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 75 2e 62 79  ->nMem;.    u.by
1ef39 2e 70 46 72 61 6d 65 2d 3e 61 70 43 73 72 20 3d  .pFrame->apCsr =
1ef3a 20 70 2d 3e 61 70 43 73 72 3b 0a 20 20 20 20 75   p->apCsr;.    u
1ef3b 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 6e 43 75 72  .by.pFrame->nCur
1ef3c 73 6f 72 20 3d 20 70 2d 3e 6e 43 75 72 73 6f 72  sor = p->nCursor
1ef3d 3b 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d  ;.    u.by.pFram
1ef3e 65 2d 3e 61 4f 70 20 3d 20 70 2d 3e 61 4f 70 3b  e->aOp = p->aOp;
1ef3f 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65  .    u.by.pFrame
1ef40 2d 3e 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a  ->nOp = p->nOp;.
1ef41 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65 2d      u.by.pFrame-
1ef42 3e 74 6f 6b 65 6e 20 3d 20 75 2e 62 79 2e 70 50  >token = u.by.pP
1ef43 72 6f 67 72 61 6d 2d 3e 74 6f 6b 65 6e 3b 0a 0a  rogram->token;..
1ef44 20 20 20 20 75 2e 62 79 2e 70 45 6e 64 20 3d 20      u.by.pEnd = 
1ef45 26 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e  &VdbeFrameMem(u.
1ef46 62 79 2e 70 46 72 61 6d 65 29 5b 75 2e 62 79 2e  by.pFrame)[u.by.
1ef47 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65  pFrame->nChildMe
1ef48 6d 5d 3b 0a 20 20 20 20 66 6f 72 28 75 2e 62 79  m];.    for(u.by
1ef49 2e 70 4d 65 6d 3d 56 64 62 65 46 72 61 6d 65 4d  .pMem=VdbeFrameM
1ef4a 65 6d 28 75 2e 62 79 2e 70 46 72 61 6d 65 29 3b  em(u.by.pFrame);
1ef4b 20 75 2e 62 79 2e 70 4d 65 6d 21 3d 75 2e 62 79   u.by.pMem!=u.by
1ef4c 2e 70 45 6e 64 3b 20 75 2e 62 79 2e 70 4d 65 6d  .pEnd; u.by.pMem
1ef4d 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 62 79 2e  ++){.      u.by.
1ef4e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1ef4f 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 75 2e  M_Null;.      u.
1ef50 62 79 2e 70 4d 65 6d 2d 3e 64 62 20 3d 20 64 62  by.pMem->db = db
1ef51 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1ef52 0a 20 20 20 20 75 2e 62 79 2e 70 46 72 61 6d 65  .    u.by.pFrame
1ef53 20 3d 20 75 2e 62 79 2e 70 52 74 2d 3e 75 2e 70   = u.by.pRt->u.p
1ef54 46 72 61 6d 65 3b 0a 20 20 20 20 61 73 73 65 72  Frame;.    asser
1ef55 74 28 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d  t( u.by.pProgram
1ef56 2d 3e 6e 4d 65 6d 2b 75 2e 62 79 2e 70 50 72 6f  ->nMem+u.by.pPro
1ef57 67 72 61 6d 2d 3e 6e 43 73 72 3d 3d 75 2e 62 79  gram->nCsr==u.by
1ef58 2e 70 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d  .pFrame->nChildM
1ef59 65 6d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  em );.    assert
1ef5a 28 20 75 2e 62 79 2e 70 50 72 6f 67 72 61 6d 2d  ( u.by.pProgram-
1ef5b 3e 6e 43 73 72 3d 3d 75 2e 62 79 2e 70 46 72 61  >nCsr==u.by.pFra
1ef5c 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 20 29 3b  me->nChildCsr );
1ef5d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63 3d  .    assert( pc=
1ef5e 3d 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e 70 63  =u.by.pFrame->pc
1ef5f 20 29 3b 0a 20 20 7d 0a 0a 20 20 70 2d 3e 6e 46   );.  }..  p->nF
1ef60 72 61 6d 65 2b 2b 3b 0a 20 20 75 2e 62 79 2e 70  rame++;.  u.by.p
1ef61 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74 20 3d  Frame->pParent =
1ef62 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 75 2e   p->pFrame;.  u.
1ef63 62 79 2e 70 46 72 61 6d 65 2d 3e 6c 61 73 74 52  by.pFrame->lastR
1ef64 6f 77 69 64 20 3d 20 64 62 2d 3e 6c 61 73 74 52  owid = db->lastR
1ef65 6f 77 69 64 3b 0a 20 20 75 2e 62 79 2e 70 46 72  owid;.  u.by.pFr
1ef66 61 6d 65 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 70  ame->nChange = p
1ef67 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20 20 70 2d 3e  ->nChange;.  p->
1ef68 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70  nChange = 0;.  p
1ef69 2d 3e 70 46 72 61 6d 65 20 3d 20 75 2e 62 79 2e  ->pFrame = u.by.
1ef6a 70 46 72 61 6d 65 3b 0a 20 20 70 2d 3e 61 4d 65  pFrame;.  p->aMe
1ef6b 6d 20 3d 20 61 4d 65 6d 20 3d 20 26 56 64 62 65  m = aMem = &Vdbe
1ef6c 46 72 61 6d 65 4d 65 6d 28 75 2e 62 79 2e 70 46  FrameMem(u.by.pF
1ef6d 72 61 6d 65 29 5b 2d 31 5d 3b 0a 20 20 70 2d 3e  rame)[-1];.  p->
1ef6e 6e 4d 65 6d 20 3d 20 75 2e 62 79 2e 70 46 72 61  nMem = u.by.pFra
1ef6f 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 3b 0a 20  me->nChildMem;. 
1ef70 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20 28 75   p->nCursor = (u
1ef71 31 36 29 75 2e 62 79 2e 70 46 72 61 6d 65 2d 3e  16)u.by.pFrame->
1ef72 6e 43 68 69 6c 64 43 73 72 3b 0a 20 20 70 2d 3e  nChildCsr;.  p->
1ef73 61 70 43 73 72 20 3d 20 28 56 64 62 65 43 75 72  apCsr = (VdbeCur
1ef74 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b 70 2d 3e  sor **)&aMem[p->
1ef75 6e 4d 65 6d 2b 31 5d 3b 0a 20 20 70 2d 3e 61 4f  nMem+1];.  p->aO
1ef76 70 20 3d 20 61 4f 70 20 3d 20 75 2e 62 79 2e 70  p = aOp = u.by.p
1ef77 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 3b 0a 20 20  Program->aOp;.  
1ef78 70 2d 3e 6e 4f 70 20 3d 20 75 2e 62 79 2e 70 50  p->nOp = u.by.pP
1ef79 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3b 0a 20 20 70  rogram->nOp;.  p
1ef7a 63 20 3d 20 2d 31 3b 0a 0a 20 20 62 72 65 61 6b  c = -1;..  break
1ef7b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1ef7c 50 61 72 61 6d 20 50 31 20 50 32 20 2a 20 2a 20  Param P1 P2 * * 
1ef7d 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  *.**.** This opc
1ef7e 6f 64 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72  ode is only ever
1ef7f 20 70 72 65 73 65 6e 74 20 69 6e 20 73 75 62 2d   present in sub-
1ef80 70 72 6f 67 72 61 6d 73 20 63 61 6c 6c 65 64 20  programs called 
1ef81 76 69 61 20 74 68 65 20 0a 2a 2a 20 4f 50 5f 50  via the .** OP_P
1ef82 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69  rogram instructi
1ef83 6f 6e 2e 20 43 6f 70 79 20 61 20 76 61 6c 75 65  on. Copy a value
1ef84 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65   currently store
1ef85 64 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 0a 2a  d in a memory .*
1ef86 2a 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 63 61  * cell of the ca
1ef87 6c 6c 69 6e 67 20 28 70 61 72 65 6e 74 29 20 66  lling (parent) f
1ef88 72 61 6d 65 20 74 6f 20 63 65 6c 6c 20 50 32 20  rame to cell P2 
1ef89 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 66  in the current f
1ef8a 72 61 6d 65 73 20 0a 2a 2a 20 61 64 64 72 65 73  rames .** addres
1ef8b 73 20 73 70 61 63 65 2e 20 54 68 69 73 20 69 73  s space. This is
1ef8c 20 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72   used by trigger
1ef8d 20 70 72 6f 67 72 61 6d 73 20 74 6f 20 61 63 63   programs to acc
1ef8e 65 73 73 20 74 68 65 20 6e 65 77 2e 2a 20 0a 2a  ess the new.* .*
1ef8f 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20 76 61 6c 75  * and old.* valu
1ef90 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 64  es..**.** The ad
1ef91 64 72 65 73 73 20 6f 66 20 74 68 65 20 63 65 6c  dress of the cel
1ef92 6c 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  l in the parent 
1ef93 66 72 61 6d 65 20 69 73 20 64 65 74 65 72 6d 69  frame is determi
1ef94 6e 65 64 20 62 79 20 61 64 64 69 6e 67 0a 2a 2a  ned by adding.**
1ef95 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1ef96 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f  e P1 argument to
1ef97 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1ef98 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f  e P1 argument to
1ef99 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20   the.** calling 
1ef9a 4f 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72  OP_Program instr
1ef9b 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20  uction..*/.case 
1ef9c 4f 50 5f 50 61 72 61 6d 3a 20 7b 20 20 20 20 20  OP_Param: {     
1ef9d 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1ef9e 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20  erelease */.#if 
1ef9f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1efa0 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1efa1 20 75 2e 62 7a 20 2a 2f 0a 20 20 56 64 62 65 46   u.bz */.  VdbeF
1efa2 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20  rame *pFrame;.  
1efa3 4d 65 6d 20 2a 70 49 6e 3b 0a 23 65 6e 64 69 66  Mem *pIn;.#endif
1efa4 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1efa5 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1efa6 2e 62 7a 20 2a 2f 0a 20 20 75 2e 62 7a 2e 70 46  .bz */.  u.bz.pF
1efa7 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65  rame = p->pFrame
1efa8 3b 0a 20 20 75 2e 62 7a 2e 70 49 6e 20 3d 20 26  ;.  u.bz.pIn = &
1efa9 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4d 65  u.bz.pFrame->aMe
1efaa 6d 5b 70 4f 70 2d 3e 70 31 20 2b 20 75 2e 62 7a  m[pOp->p1 + u.bz
1efab 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 5b 75 2e 62  .pFrame->aOp[u.b
1efac 7a 2e 70 46 72 61 6d 65 2d 3e 70 63 5d 2e 70 31  z.pFrame->pc].p1
1efad 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ];.  sqlite3Vdbe
1efae 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70  MemShallowCopy(p
1efaf 4f 75 74 2c 20 75 2e 62 7a 2e 70 49 6e 2c 20 4d  Out, u.bz.pIn, M
1efb0 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 62 72 65  EM_Ephem);.  bre
1efb1 61 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  ak;.}..#endif /*
1efb2 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
1efb3 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
1efb4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1efb5 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
1efb6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6b 43 6f  ./* Opcode: FkCo
1efb7 75 6e 74 65 72 20 50 31 20 50 32 20 2a 20 2a 20  unter P1 P2 * * 
1efb8 2a 0a 2a 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e  *.**.** Incremen
1efb9 74 20 61 20 22 63 6f 6e 73 74 72 61 69 6e 74 20  t a "constraint 
1efba 63 6f 75 6e 74 65 72 22 20 62 79 20 50 32 20 28  counter" by P2 (
1efbb 50 32 20 6d 61 79 20 62 65 20 6e 65 67 61 74 69  P2 may be negati
1efbc 76 65 20 6f 72 20 70 6f 73 69 74 69 76 65 29 2e  ve or positive).
1efbd 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 6f 6e  .** If P1 is non
1efbe 2d 7a 65 72 6f 2c 20 74 68 65 20 64 61 74 61 62  -zero, the datab
1efbf 61 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  ase constraint c
1efc0 6f 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d  ounter is increm
1efc1 65 6e 74 65 64 20 0a 2a 2a 20 28 64 65 66 65 72  ented .** (defer
1efc2 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  red foreign key 
1efc3 63 6f 6e 73 74 72 61 69 6e 74 73 29 2e 20 4f 74  constraints). Ot
1efc4 68 65 72 77 69 73 65 2c 20 69 66 20 50 31 20 69  herwise, if P1 i
1efc5 73 20 7a 65 72 6f 2c 20 74 68 65 20 0a 2a 2a 20  s zero, the .** 
1efc6 73 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 65  statement counte
1efc7 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  r is incremented
1efc8 20 28 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65   (immediate fore
1efc9 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
1efca 6e 74 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  nts)..*/.case OP
1efcb 5f 46 6b 43 6f 75 6e 74 65 72 3a 20 7b 0a 20 20  _FkCounter: {.  
1efcc 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20  if( pOp->p1 ){. 
1efcd 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64     db->nDeferred
1efce 43 6f 6e 73 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b  Cons += pOp->p2;
1efcf 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d  .  }else{.    p-
1efd0 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 20 2b  >nFkConstraint +
1efd1 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 0a 20  = pOp->p2;.  }. 
1efd2 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1efd3 63 6f 64 65 3a 20 46 6b 49 66 5a 65 72 6f 20 50  code: FkIfZero P
1efd4 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1efd5 20 54 68 69 73 20 6f 70 63 6f 64 65 20 74 65 73   This opcode tes
1efd6 74 73 20 69 66 20 61 20 66 6f 72 65 69 67 6e 20  ts if a foreign 
1efd7 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63  key constraint-c
1efd8 6f 75 6e 74 65 72 20 69 73 20 63 75 72 72 65 6e  ounter is curren
1efd9 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 20 49 66 20  tly zero..** If 
1efda 73 6f 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74  so, jump to inst
1efdb 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65  ruction P2. Othe
1efdc 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f  rwise, fall thro
1efdd 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
1efde 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  .** instruction.
1efdf 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20  .**.** If P1 is 
1efe0 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
1efe1 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e  he jump is taken
1efe2 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
1efe3 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e   constraint-coun
1efe4 74 65 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 28  ter.** is zero (
1efe5 74 68 65 20 6f 6e 65 20 74 68 61 74 20 63 6f 75  the one that cou
1efe6 6e 74 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e  nts deferred con
1efe7 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
1efe8 6e 73 29 2e 20 49 66 20 50 31 20 69 73 0a 2a 2a  ns). If P1 is.**
1efe9 20 7a 65 72 6f 2c 20 74 68 65 20 6a 75 6d 70 20   zero, the jump 
1efea 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20  is taken if the 
1efeb 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 73 74 72  statement constr
1efec 61 69 6e 74 2d 63 6f 75 6e 74 65 72 20 69 73 20  aint-counter is 
1efed 7a 65 72 6f 0a 2a 2a 20 28 69 6d 6d 65 64 69 61  zero.** (immedia
1efee 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  te foreign key c
1efef 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74  onstraint violat
1eff0 69 6f 6e 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ions)..*/.case O
1eff1 50 5f 46 6b 49 66 5a 65 72 6f 3a 20 7b 20 20 20  P_FkIfZero: {   
1eff2 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
1eff3 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29  .  if( pOp->p1 )
1eff4 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 44  {.    if( db->nD
1eff5 65 66 65 72 72 65 64 43 6f 6e 73 3d 3d 30 20 29  eferredCons==0 )
1eff6 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b   pc = pOp->p2-1;
1eff7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66  .  }else{.    if
1eff8 28 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69  ( p->nFkConstrai
1eff9 6e 74 3d 3d 30 20 29 20 70 63 20 3d 20 70 4f 70  nt==0 ) pc = pOp
1effa 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72  ->p2-1;.  }.  br
1effb 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1effc 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
1effd 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
1effe 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1efff 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
1f000 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65  REMENT./* Opcode
1f001 3a 20 4d 65 6d 4d 61 78 20 50 31 20 50 32 20 2a  : MemMax P1 P2 *
1f002 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73   * *.**.** P1 is
1f003 20 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74   a register in t
1f004 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66  he root frame of
1f005 20 74 68 69 73 20 56 4d 20 28 74 68 65 20 72 6f   this VM (the ro
1f006 6f 74 20 66 72 61 6d 65 20 69 73 0a 2a 2a 20 64  ot frame is.** d
1f007 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68  ifferent from th
1f008 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 20  e current frame 
1f009 69 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  if this instruct
1f00a 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 65 78 65  ion is being exe
1f00b 63 75 74 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20  cuted.** within 
1f00c 61 20 73 75 62 2d 70 72 6f 67 72 61 6d 29 2e 20  a sub-program). 
1f00d 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  Set the value of
1f00e 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
1f00f 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 0a  the maximum of .
1f010 2a 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 76  ** its current v
1f011 61 6c 75 65 20 61 6e 64 20 74 68 65 20 76 61 6c  alue and the val
1f012 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1f013 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  2..**.** This in
1f014 73 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73  struction throws
1f015 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65   an error if the
1f016 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20   memory cell is 
1f017 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a  not initially.**
1f018 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a   an integer..*/.
1f019 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20  case OP_MemMax: 
1f01a 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20  {        /* in2 
1f01b 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1f01c 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1f01d 65 64 20 69 6e 74 6f 20 75 2e 63 61 20 2a 2f 0a  ed into u.ca */.
1f01e 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 56    Mem *pIn1;.  V
1f01f 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
1f020 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1f021 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1f022 64 20 69 6e 74 6f 20 75 2e 63 61 20 2a 2f 0a 20  d into u.ca */. 
1f023 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29   if( p->pFrame )
1f024 7b 0a 20 20 20 20 66 6f 72 28 75 2e 63 61 2e 70  {.    for(u.ca.p
1f025 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b  Frame=p->pFrame;
1f026 20 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 70 50   u.ca.pFrame->pP
1f027 61 72 65 6e 74 3b 20 75 2e 63 61 2e 70 46 72 61  arent; u.ca.pFra
1f028 6d 65 3d 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e  me=u.ca.pFrame->
1f029 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 75 2e  pParent);.    u.
1f02a 63 61 2e 70 49 6e 31 20 3d 20 26 75 2e 63 61 2e  ca.pIn1 = &u.ca.
1f02b 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70  pFrame->aMem[pOp
1f02c 2d 3e 70 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ->p1];.  }else{.
1f02d 20 20 20 20 75 2e 63 61 2e 70 49 6e 31 20 3d 20      u.ca.pIn1 = 
1f02e 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  &aMem[pOp->p1];.
1f02f 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62    }.  sqlite3Vdb
1f030 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 75  eMemIntegerify(u
1f031 2e 63 61 2e 70 49 6e 31 29 3b 0a 20 20 70 49 6e  .ca.pIn1);.  pIn
1f032 32 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  2 = &aMem[pOp->p
1f033 32 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  2];.  sqlite3Vdb
1f034 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70  eMemIntegerify(p
1f035 49 6e 32 29 3b 0a 20 20 69 66 28 20 75 2e 63 61  In2);.  if( u.ca
1f036 2e 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e 32 2d  .pIn1->u.i<pIn2-
1f037 3e 75 2e 69 29 7b 0a 20 20 20 20 75 2e 63 61 2e  >u.i){.    u.ca.
1f038 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 49 6e 32  pIn1->u.i = pIn2
1f039 2d 3e 75 2e 69 3b 0a 20 20 7d 0a 20 20 62 72 65  ->u.i;.  }.  bre
1f03a 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1f03b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
1f03c 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 2f 2a  INCREMENT */../*
1f03d 20 4f 70 63 6f 64 65 3a 20 49 66 50 6f 73 20 50   Opcode: IfPos P
1f03e 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1f03f 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66   If the value of
1f040 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1f041 31 20 6f 72 20 67 72 65 61 74 65 72 2c 20 6a 75  1 or greater, ju
1f042 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  mp to P2..**.** 
1f043 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f  It is illegal to
1f044 20 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75   use this instru
1f045 63 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73  ction on a regis
1f046 74 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a  ter that does.**
1f047 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20   not contain an 
1f048 69 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73  integer.  An ass
1f049 65 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c  ertion fault wil
1f04a 6c 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20  l result if you 
1f04b 74 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  try..*/.case OP_
1f04c 49 66 50 6f 73 3a 20 7b 20 20 20 20 20 20 20 20  IfPos: {        
1f04d 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a  /* jump, in1 */.
1f04e 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
1f04f 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1f050 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d  t( pIn1->flags&M
1f051 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28 20  EM_Int );.  if( 
1f052 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a 20  pIn1->u.i>0 ){. 
1f053 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1f054 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1f055 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1f056 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20 2a   IfNeg P1 P2 * *
1f057 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20   *.**.** If the 
1f058 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65  value of registe
1f059 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68 61  r P1 is less tha
1f05a 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f 20  n zero, jump to 
1f05b 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  P2. .**.** It is
1f05c 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20   illegal to use 
1f05d 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
1f05e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74   on a register t
1f05f 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20  hat does.** not 
1f060 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67  contain an integ
1f061 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f  er.  An assertio
1f062 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73  n fault will res
1f063 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a  ult if you try..
1f064 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65 67  */.case OP_IfNeg
1f065 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1f066 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 70 49 6e  mp, in1 */.  pIn
1f067 31 20 3d 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70  1 = &aMem[pOp->p
1f068 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49  1];.  assert( pI
1f069 6e 31 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e  n1->flags&MEM_In
1f06a 74 20 29 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d  t );.  if( pIn1-
1f06b 3e 75 2e 69 3c 30 20 29 7b 0a 20 20 20 20 20 70  >u.i<0 ){.     p
1f06c 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1f06d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1f06e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66 5a 65  ./* Opcode: IfZe
1f06f 72 6f 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  ro P1 P2 P3 * *.
1f070 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74  **.** The regist
1f071 65 72 20 50 31 20 6d 75 73 74 20 63 6f 6e 74 61  er P1 must conta
1f072 69 6e 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20  in an integer.  
1f073 41 64 64 20 6c 69 74 65 72 61 6c 20 50 33 20 74  Add literal P3 t
1f074 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69  o the.** value i
1f075 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 20  n register P1.  
1f076 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  If the result is
1f077 20 65 78 61 63 74 6c 79 20 30 2c 20 6a 75 6d 70   exactly 0, jump
1f078 20 74 6f 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49   to P2. .**.** I
1f079 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20  t is illegal to 
1f07a 75 73 65 20 74 68 69 73 20 69 6e 73 74 72 75 63  use this instruc
1f07b 74 69 6f 6e 20 6f 6e 20 61 20 72 65 67 69 73 74  tion on a regist
1f07c 65 72 20 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20  er that does.** 
1f07d 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69  not contain an i
1f07e 6e 74 65 67 65 72 2e 20 20 41 6e 20 61 73 73 65  nteger.  An asse
1f07f 72 74 69 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c  rtion fault will
1f080 20 72 65 73 75 6c 74 20 69 66 20 79 6f 75 20 74   result if you t
1f081 72 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  ry..*/.case OP_I
1f082 66 5a 65 72 6f 3a 20 7b 20 20 20 20 20 20 20 20  fZero: {        
1f083 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a  /* jump, in1 */.
1f084 20 20 70 49 6e 31 20 3d 20 26 61 4d 65 6d 5b 70    pIn1 = &aMem[p
1f085 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1f086 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26 4d  t( pIn1->flags&M
1f087 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 70 49 6e 31  EM_Int );.  pIn1
1f088 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d 3e 70 33  ->u.i += pOp->p3
1f089 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 75 2e  ;.  if( pIn1->u.
1f08a 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 70 63 20  i==0 ){.     pc 
1f08b 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1f08c 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1f08d 2a 20 4f 70 63 6f 64 65 3a 20 41 67 67 53 74 65  * Opcode: AggSte
1f08e 70 20 2a 20 50 32 20 50 33 20 50 34 20 50 35 0a  p * P2 P3 P4 P5.
1f08f 2a 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68  **.** Execute th
1f090 65 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20  e step function 
1f091 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65  for an aggregate
1f092 2e 20 20 54 68 65 0a 2a 2a 20 66 75 6e 63 74 69  .  The.** functi
1f093 6f 6e 20 68 61 73 20 50 35 20 61 72 67 75 6d 65  on has P5 argume
1f094 6e 74 73 2e 20 20 20 50 34 20 69 73 20 61 20 70  nts.   P4 is a p
1f095 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75  ointer to the Fu
1f096 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75  ncDef.** structu
1f097 72 65 20 74 68 61 74 20 73 70 65 63 69 66 69 65  re that specifie
1f098 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20  s the function. 
1f099 20 55 73 65 20 72 65 67 69 73 74 65 72 0a 2a 2a   Use register.**
1f09a 20 50 33 20 61 73 20 74 68 65 20 61 63 63 75 6d   P3 as the accum
1f09b 75 6c 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ulator..**.** Th
1f09c 65 20 50 35 20 61 72 67 75 6d 65 6e 74 73 20 61  e P5 arguments a
1f09d 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65  re taken from re
1f09e 67 69 73 74 65 72 20 50 32 20 61 6e 64 20 69 74  gister P2 and it
1f09f 73 0a 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e  s.** successors.
1f0a0 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 53  .*/.case OP_AggS
1f0a1 74 65 70 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  tep: {.#if 0  /*
1f0a2 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1f0a3 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 62   moved into u.cb
1f0a4 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 69   */.  int n;.  i
1f0a5 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65  nt i;.  Mem *pMe
1f0a6 6d 3b 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a  m;.  Mem *pRec;.
1f0a7 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1f0a8 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65 33  t ctx;.  sqlite3
1f0a9 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b 0a  _value **apVal;.
1f0aa 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1f0ab 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1f0ac 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a 0a 20 20  into u.cb */..  
1f0ad 75 2e 63 62 2e 6e 20 3d 20 70 4f 70 2d 3e 70 35  u.cb.n = pOp->p5
1f0ae 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 62  ;.  assert( u.cb
1f0af 2e 6e 3e 3d 30 20 29 3b 0a 20 20 75 2e 63 62 2e  .n>=0 );.  u.cb.
1f0b0 70 52 65 63 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pRec = &aMem[pOp
1f0b1 2d 3e 70 32 5d 3b 0a 20 20 75 2e 63 62 2e 61 70  ->p2];.  u.cb.ap
1f0b2 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a  Val = p->apArg;.
1f0b3 20 20 61 73 73 65 72 74 28 20 75 2e 63 62 2e 61    assert( u.cb.a
1f0b4 70 56 61 6c 20 7c 7c 20 75 2e 63 62 2e 6e 3d 3d  pVal || u.cb.n==
1f0b5 30 20 29 3b 0a 20 20 66 6f 72 28 75 2e 63 62 2e  0 );.  for(u.cb.
1f0b6 69 3d 30 3b 20 75 2e 63 62 2e 69 3c 75 2e 63 62  i=0; u.cb.i<u.cb
1f0b7 2e 6e 3b 20 75 2e 63 62 2e 69 2b 2b 2c 20 75 2e  .n; u.cb.i++, u.
1f0b8 63 62 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20  cb.pRec++){.    
1f0b9 75 2e 63 62 2e 61 70 56 61 6c 5b 75 2e 63 62 2e  u.cb.apVal[u.cb.
1f0ba 69 5d 20 3d 20 75 2e 63 62 2e 70 52 65 63 3b 0a  i] = u.cb.pRec;.
1f0bb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1f0bc 65 6d 53 74 6f 72 65 54 79 70 65 28 75 2e 63 62  emStoreType(u.cb
1f0bd 2e 70 52 65 63 29 3b 0a 20 20 7d 0a 20 20 75 2e  .pRec);.  }.  u.
1f0be 63 62 2e 63 74 78 2e 70 46 75 6e 63 20 3d 20 70  cb.ctx.pFunc = p
1f0bf 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20  Op->p4.pFunc;.  
1f0c0 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
1f0c1 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  0 && pOp->p3<=p-
1f0c2 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63 62 2e  >nMem );.  u.cb.
1f0c3 63 74 78 2e 70 4d 65 6d 20 3d 20 75 2e 63 62 2e  ctx.pMem = u.cb.
1f0c4 70 4d 65 6d 20 3d 20 26 61 4d 65 6d 5b 70 4f 70  pMem = &aMem[pOp
1f0c5 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 62 2e 70 4d  ->p3];.  u.cb.pM
1f0c6 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 75 2e 63 62 2e  em->n++;.  u.cb.
1f0c7 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45  ctx.s.flags = ME
1f0c8 4d 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 63 62 2e 63  M_Null;.  u.cb.c
1f0c9 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 75 2e  tx.s.z = 0;.  u.
1f0ca 63 62 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63  cb.ctx.s.zMalloc
1f0cb 20 3d 20 30 3b 0a 20 20 75 2e 63 62 2e 63 74 78   = 0;.  u.cb.ctx
1f0cc 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75  .s.xDel = 0;.  u
1f0cd 2e 63 62 2e 63 74 78 2e 73 2e 64 62 20 3d 20 64  .cb.ctx.s.db = d
1f0ce 62 3b 0a 20 20 75 2e 63 62 2e 63 74 78 2e 69 73  b;.  u.cb.ctx.is
1f0cf 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63  Error = 0;.  u.c
1f0d0 62 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b  b.ctx.pColl = 0;
1f0d1 0a 20 20 69 66 28 20 75 2e 63 62 2e 63 74 78 2e  .  if( u.cb.ctx.
1f0d2 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53  pFunc->flags & S
1f0d3 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
1f0d4 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72  OLL ){.    asser
1f0d5 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b  t( pOp>p->aOp );
1f0d6 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
1f0d7 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f  [-1].p4type==P4_
1f0d8 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61  COLLSEQ );.    a
1f0d9 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f  ssert( pOp[-1].o
1f0da 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65  pcode==OP_CollSe
1f0db 71 20 29 3b 0a 20 20 20 20 75 2e 63 62 2e 63 74  q );.    u.cb.ct
1f0dc 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31  x.pColl = pOp[-1
1f0dd 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a  ].p4.pColl;.  }.
1f0de 20 20 28 75 2e 63 62 2e 63 74 78 2e 70 46 75 6e    (u.cb.ctx.pFun
1f0df 63 2d 3e 78 53 74 65 70 29 28 26 75 2e 63 62 2e  c->xStep)(&u.cb.
1f0e0 63 74 78 2c 20 75 2e 63 62 2e 6e 2c 20 75 2e 63  ctx, u.cb.n, u.c
1f0e1 62 2e 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20  b.apVal);.  if( 
1f0e2 75 2e 63 62 2e 63 74 78 2e 69 73 45 72 72 6f 72  u.cb.ctx.isError
1f0e3 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
1f0e4 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1f0e5 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20  rMsg, db, "%s", 
1f0e6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1f0e7 78 74 28 26 75 2e 63 62 2e 63 74 78 2e 73 29 29  xt(&u.cb.ctx.s))
1f0e8 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 62 2e  ;.    rc = u.cb.
1f0e9 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d  ctx.isError;.  }
1f0ea 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1f0eb 6d 52 65 6c 65 61 73 65 28 26 75 2e 63 62 2e 63  mRelease(&u.cb.c
1f0ec 74 78 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  tx.s);.  break;.
1f0ed 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67  }../* Opcode: Ag
1f0ee 67 46 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50  gFinal P1 P2 * P
1f0ef 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74  4 *.**.** Execut
1f0f0 65 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20  e the finalizer 
1f0f1 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20  function for an 
1f0f2 61 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69  aggregate.  P1 i
1f0f3 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20  s.** the memory 
1f0f4 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73  location that is
1f0f5 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
1f0f6 20 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61   for the aggrega
1f0f7 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20  te..**.** P2 is 
1f0f8 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  the number of ar
1f0f9 67 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65  guments that the
1f0fa 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74   step function t
1f0fb 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69  akes and.** P4 i
1f0fc 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
1f0fd 68 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74  he FuncDef for t
1f0fe 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  his function.  T
1f0ff 68 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e  he P2.** argumen
1f100 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79  t is not used by
1f101 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49   this opcode.  I
1f102 74 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20  t is only there 
1f103 74 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a  to disambiguate.
1f104 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  ** functions tha
1f105 74 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69  t can take varyi
1f106 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72  ng numbers of ar
1f107 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a  guments.  The.**
1f108 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20   P4 argument is 
1f109 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20  only needed for 
1f10a 74 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63  the degenerate c
1f10b 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65  ase where.** the
1f10c 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77   step function w
1f10d 61 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c  as not previousl
1f10e 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73  y called..*/.cas
1f10f 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b  e OP_AggFinal: {
1f110 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1f111 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f112 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 20   into u.cc */.  
1f113 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 23 65 6e 64 69  Mem *pMem;.#endi
1f114 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1f115 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1f116 75 2e 63 63 20 2a 2f 0a 20 20 61 73 73 65 72 74  u.cc */.  assert
1f117 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70  ( pOp->p1>0 && p
1f118 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p1<=p->nMem 
1f119 29 3b 0a 20 20 75 2e 63 63 2e 70 4d 65 6d 20 3d  );.  u.cc.pMem =
1f11a 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b   &aMem[pOp->p1];
1f11b 0a 20 20 61 73 73 65 72 74 28 20 28 75 2e 63 63  .  assert( (u.cc
1f11c 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 7e  .pMem->flags & ~
1f11d 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 41 67  (MEM_Null|MEM_Ag
1f11e 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d  g))==0 );.  rc =
1f11f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
1f120 69 6e 61 6c 69 7a 65 28 75 2e 63 63 2e 70 4d 65  inalize(u.cc.pMe
1f121 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63  m, pOp->p4.pFunc
1f122 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20  );.  if( rc ){. 
1f123 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1f124 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1f125 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
1f126 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 75 2e  e3_value_text(u.
1f127 63 63 2e 70 4d 65 6d 29 29 3b 0a 20 20 7d 0a 20  cc.pMem));.  }. 
1f128 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1f129 67 65 45 6e 63 6f 64 69 6e 67 28 75 2e 63 63 2e  geEncoding(u.cc.
1f12a 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  pMem, encoding);
1f12b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
1f12c 4f 42 53 49 5a 45 28 75 2e 63 63 2e 70 4d 65 6d  OBSIZE(u.cc.pMem
1f12d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
1f12e 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 75 2e  VdbeMemTooBig(u.
1f12f 63 63 2e 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20  cc.pMem) ){.    
1f130 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20  goto too_big;.  
1f131 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23  }.  break;.}...#
1f132 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
1f133 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20  TE_OMIT_VACUUM) 
1f134 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
1f135 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 29 0a  TE_OMIT_ATTACH).
1f136 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61 63 75 75  /* Opcode: Vacuu
1f137 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  m * * * * *.**.*
1f138 2a 20 56 61 63 75 75 6d 20 74 68 65 20 65 6e 74  * Vacuum the ent
1f139 69 72 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  ire database.  T
1f13a 68 69 73 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20  his opcode will 
1f13b 63 61 75 73 65 20 6f 74 68 65 72 20 76 69 72 74  cause other virt
1f13c 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65 73 20  ual.** machines 
1f13d 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 61 6e  to be created an
1f13e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61 79 20 6e  d run.  It may n
1f13f 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f  ot be called fro
1f140 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61 20 74 72  m within.** a tr
1f141 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  ansaction..*/.ca
1f142 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a 20 7b 0a  se OP_Vacuum: {.
1f143 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1f144 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74  etyOff(db) ) got
1f145 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1f146 69 73 75 73 65 3b 20 0a 20 20 72 63 20 3d 20 73  isuse; .  rc = s
1f147 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28  qlite3RunVacuum(
1f148 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 29  &p->zErrMsg, db)
1f149 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
1f14a 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f  afetyOn(db) ) go
1f14b 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1f14c 6d 69 73 75 73 65 3b 0a 20 20 62 72 65 61 6b 3b  misuse;.  break;
1f14d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21  .}.#endif..#if !
1f14e 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1f14f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a  MIT_AUTOVACUUM).
1f150 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 63 72 56  /* Opcode: IncrV
1f151 61 63 75 75 6d 20 50 31 20 50 32 20 2a 20 2a 20  acuum P1 P2 * * 
1f152 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20  *.**.** Perform 
1f153 61 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66  a single step of
1f154 20 74 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c   the incremental
1f155 20 76 61 63 75 75 6d 20 70 72 6f 63 65 64 75 72   vacuum procedur
1f156 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50 31 20 64  e on.** the P1 d
1f157 61 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20  atabase. If the 
1f158 76 61 63 75 75 6d 20 68 61 73 20 66 69 6e 69 73  vacuum has finis
1f159 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73  hed, jump to ins
1f15a 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50 32 2e 20  truction.** P2. 
1f15b 4f 74 68 65 72 77 69 73 65 2c 20 66 61 6c 6c 20  Otherwise, fall 
1f15c 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e  through to the n
1f15d 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  ext instruction.
1f15e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 63 72  .*/.case OP_Incr
1f15f 56 61 63 75 75 6d 3a 20 7b 20 20 20 20 20 20 20  Vacuum: {       
1f160 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66 20   /* jump */.#if 
1f161 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1f162 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f163 20 75 2e 63 64 20 2a 2f 0a 20 20 42 74 72 65 65   u.cd */.  Btree
1f164 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f 2a   *pBt;.#endif /*
1f165 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1f166 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 64   moved into u.cd
1f167 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
1f168 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1f169 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  ->p1<db->nDb );.
1f16a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1f16b 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f  reeMask & (1<<pO
1f16c 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20  p->p1))!=0 );.  
1f16d 75 2e 63 64 2e 70 42 74 20 3d 20 64 62 2d 3e 61  u.cd.pBt = db->a
1f16e 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b  Db[pOp->p1].pBt;
1f16f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
1f170 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 75  treeIncrVacuum(u
1f171 2e 63 64 2e 70 42 74 29 3b 0a 20 20 69 66 28 20  .cd.pBt);.  if( 
1f172 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
1f173 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1f174 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 72 63 20  >p2 - 1;.    rc 
1f175 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
1f176 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1f177 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 45  if../* Opcode: E
1f178 78 70 69 72 65 20 50 31 20 2a 20 2a 20 2a 20 2a  xpire P1 * * * *
1f179 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20 70 72 65  .**.** Cause pre
1f17a 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65  compiled stateme
1f17b 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65 20 65 78  nts to become ex
1f17c 70 69 72 65 64 2e 20 41 6e 20 65 78 70 69 72 65  pired. An expire
1f17d 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 66  d statement.** f
1f17e 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65 72 72  ails with an err
1f17f 6f 72 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54  or code of SQLIT
1f180 45 5f 53 43 48 45 4d 41 20 69 66 20 69 74 20 69  E_SCHEMA if it i
1f181 73 20 65 76 65 72 20 65 78 65 63 75 74 65 64 20  s ever executed 
1f182 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69 74 65 33  .** (via sqlite3
1f183 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20 0a 2a 2a  _step())..** .**
1f184 20 49 66 20 50 31 20 69 73 20 30 2c 20 74 68 65   If P1 is 0, the
1f185 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61 74 65 6d  n all SQL statem
1f186 65 6e 74 73 20 62 65 63 6f 6d 65 20 65 78 70 69  ents become expi
1f187 72 65 64 2e 20 49 66 20 50 31 20 69 73 20 6e 6f  red. If P1 is no
1f188 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20  n-zero,.** then 
1f189 6f 6e 6c 79 20 74 68 65 20 63 75 72 72 65 6e 74  only the current
1f18a 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74 61  ly executing sta
1f18b 74 65 6d 65 6e 74 20 69 73 20 61 66 66 65 63 74  tement is affect
1f18c 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ed. .*/.case OP_
1f18d 45 78 70 69 72 65 3a 20 7b 0a 20 20 69 66 28 20  Expire: {.  if( 
1f18e 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20  !pOp->p1 ){.    
1f18f 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
1f190 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
1f191 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  db);.  }else{.  
1f192 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31    p->expired = 1
1f193 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1f194 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1f195 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1f196 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 61  HE./* Opcode: Ta
1f197 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32 20 50 33  bleLock P1 P2 P3
1f198 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f 62 74 61   P4 *.**.** Obta
1f199 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 70  in a lock on a p
1f19a 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e  articular table.
1f19b 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   This instructio
1f19c 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77  n is only used w
1f19d 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68 61 72 65  hen.** the share
1f19e 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 20  d-cache feature 
1f19f 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a 2a 2a 0a  is enabled. .**.
1f1a0 2a 2a 20 50 31 20 69 73 20 74 68 65 20 69 6e 64  ** P1 is the ind
1f1a1 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
1f1a2 73 65 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44  se in sqlite3.aD
1f1a3 62 5b 5d 20 6f 66 20 74 68 65 20 64 61 74 61 62  b[] of the datab
1f1a4 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69 63 68 20  ase.** on which 
1f1a5 74 68 65 20 6c 6f 63 6b 20 69 73 20 61 63 71 75  the lock is acqu
1f1a6 69 72 65 64 2e 20 20 41 20 72 65 61 64 6c 6f 63  ired.  A readloc
1f1a7 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69 66  k is obtained if
1f1a8 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20 61 20 77   P3==0 or.** a w
1f1a9 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 50 33 3d  rite lock if P3=
1f1aa 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 63 6f 6e  =1..**.** P2 con
1f1ab 74 61 69 6e 73 20 74 68 65 20 72 6f 6f 74 2d 70  tains the root-p
1f1ac 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  age of the table
1f1ad 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20   to lock..**.** 
1f1ae 50 34 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f  P4 contains a po
1f1af 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d  inter to the nam
1f1b0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62  e of the table b
1f1b1 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20 54 68 69  eing locked. Thi
1f1b2 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 75 73 65  s is only.** use
1f1b3 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61 6e  d to generate an
1f1b4 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
1f1b5 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f  f the lock canno
1f1b6 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a  t be obtained..*
1f1b7 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62 6c 65 4c  /.case OP_TableL
1f1b8 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69 73 57 72  ock: {.  u8 isWr
1f1b9 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38 29 70 4f  iteLock = (u8)pO
1f1ba 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20 69 73 57  p->p3;.  if( isW
1f1bb 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30 3d 3d 28  riteLock || 0==(
1f1bc 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
1f1bd 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64  _ReadUncommitted
1f1be 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 31 20  ) ){.    int p1 
1f1bf 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20 20 20 20  = pOp->p1; .    
1f1c0 61 73 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26  assert( p1>=0 &&
1f1c1 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20   p1<db->nDb );. 
1f1c2 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62     assert( (p->b
1f1c3 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70  treeMask & (1<<p
1f1c4 31 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  1))!=0 );.    as
1f1c5 73 65 72 74 28 20 69 73 57 72 69 74 65 4c 6f 63  sert( isWriteLoc
1f1c6 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 65 4c  k==0 || isWriteL
1f1c7 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 72 63  ock==1 );.    rc
1f1c8 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c   = sqlite3BtreeL
1f1c9 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e 61 44 62  ockTable(db->aDb
1f1ca 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70  [p1].pBt, pOp->p
1f1cb 32 2c 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b  2, isWriteLock);
1f1cc 0a 20 20 20 20 69 66 28 20 28 72 63 26 30 78 46  .    if( (rc&0xF
1f1cd 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  F)==SQLITE_LOCKE
1f1ce 44 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  D ){.      const
1f1cf 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e   char *z = pOp->
1f1d0 70 34 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69  p4.z;.      sqli
1f1d1 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d  te3SetString(&p-
1f1d2 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 64  >zErrMsg, db, "d
1f1d3 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 69 73  atabase table is
1f1d4 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20 7a 29   locked: %s", z)
1f1d5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72  ;.    }.  }.  br
1f1d6 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1f1d7 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1f1d8 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 23 69  RED_CACHE */..#i
1f1d9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f1da 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
1f1db 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65 67 69 6e  * Opcode: VBegin
1f1dc 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a   * * * P4 *.**.*
1f1dd 2a 20 50 34 20 6d 61 79 20 62 65 20 61 20 70 6f  * P4 may be a po
1f1de 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69  inter to an sqli
1f1df 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
1f1e0 72 65 2e 20 49 66 20 73 6f 2c 20 63 61 6c 6c 20  re. If so, call 
1f1e1 74 68 65 20 0a 2a 2a 20 78 42 65 67 69 6e 20 6d  the .** xBegin m
1f1e2 65 74 68 6f 64 20 66 6f 72 20 74 68 61 74 20 74  ethod for that t
1f1e3 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f  able..**.** Also
1f1e4 2c 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  , whether or not
1f1e5 20 50 34 20 69 73 20 73 65 74 2c 20 63 68 65 63   P4 is set, chec
1f1e6 6b 20 74 68 61 74 20 74 68 69 73 20 69 73 20 6e  k that this is n
1f1e7 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20  ot being called 
1f1e8 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69 6e 20 61  from.** within a
1f1e9 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 61 20 76   callback to a v
1f1ea 69 72 74 75 61 6c 20 74 61 62 6c 65 20 78 53 79  irtual table xSy
1f1eb 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20 49 66 20  nc() method. If 
1f1ec 69 74 20 69 73 2c 20 74 68 65 20 65 72 72 6f 72  it is, the error
1f1ed 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 62 65  .** code will be
1f1ee 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c   set to SQLITE_L
1f1ef 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73 65 20 4f  OCKED..*/.case O
1f1f0 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 23 69 66 20  P_VBegin: {.#if 
1f1f1 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1f1f2 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f1f3 20 75 2e 63 65 20 2a 2f 0a 20 20 56 54 61 62 6c   u.ce */.  VTabl
1f1f4 65 20 2a 70 56 54 61 62 3b 0a 23 65 6e 64 69 66  e *pVTab;.#endif
1f1f5 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1f1f6 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1f1f7 2e 63 65 20 2a 2f 0a 20 20 75 2e 63 65 2e 70 56  .ce */.  u.ce.pV
1f1f8 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56  Tab = pOp->p4.pV
1f1f9 74 61 62 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  tab;.  rc = sqli
1f1fa 74 65 33 56 74 61 62 42 65 67 69 6e 28 64 62 2c  te3VtabBegin(db,
1f1fb 20 75 2e 63 65 2e 70 56 54 61 62 29 3b 0a 20 20   u.ce.pVTab);.  
1f1fc 69 66 28 20 75 2e 63 65 2e 70 56 54 61 62 20 29  if( u.ce.pVTab )
1f1fd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
1f1fe 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1f1ff 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72  sg);.    p->zErr
1f200 4d 73 67 20 3d 20 75 2e 63 65 2e 70 56 54 61 62  Msg = u.ce.pVTab
1f201 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  ->pVtab->zErrMsg
1f202 3b 0a 20 20 20 20 75 2e 63 65 2e 70 56 54 61 62  ;.    u.ce.pVTab
1f203 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  ->pVtab->zErrMsg
1f204 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61   = 0;.  }.  brea
1f205 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
1f206 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1f207 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e  ALTABLE */..#ifn
1f208 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f209 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20  VIRTUALTABLE./* 
1f20a 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 74 65 20  Opcode: VCreate 
1f20b 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  P1 * * P4 *.**.*
1f20c 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 6d 65  * P4 is the name
1f20d 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61   of a virtual ta
1f20e 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20  ble in database 
1f20f 50 31 2e 20 43 61 6c 6c 20 74 68 65 20 78 43 72  P1. Call the xCr
1f210 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 66  eate method.** f
1f211 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a  or that table..*
1f212 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72 65 61 74  /.case OP_VCreat
1f213 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73 71 6c 69  e: {.  rc = sqli
1f214 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74  te3VtabCallCreat
1f215 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70  e(db, pOp->p1, p
1f216 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d 3e 7a 45  Op->p4.z, &p->zE
1f217 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 61 6b 3b  rrMsg);.  break;
1f218 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1f219 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1f21a 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  TABLE */..#ifnde
1f21b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1f21c 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70  RTUALTABLE./* Op
1f21d 63 6f 64 65 3a 20 56 44 65 73 74 72 6f 79 20 50  code: VDestroy P
1f21e 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  1 * * P4 *.**.**
1f21f 20 50 34 20 69 73 20 74 68 65 20 6e 61 6d 65 20   P4 is the name 
1f220 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  of a virtual tab
1f221 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 50  le in database P
1f222 31 2e 20 20 43 61 6c 6c 20 74 68 65 20 78 44 65  1.  Call the xDe
1f223 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20  stroy method.** 
1f224 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a  of that table..*
1f225 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65 73 74 72  /.case OP_VDestr
1f226 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e 56 74 61  oy: {.  p->inVta
1f227 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a 20 20 72  bMethod = 2;.  r
1f228 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43  c = sqlite3VtabC
1f229 61 6c 6c 44 65 73 74 72 6f 79 28 64 62 2c 20 70  allDestroy(db, p
1f22a 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e  Op->p1, pOp->p4.
1f22b 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d  z);.  p->inVtabM
1f22c 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 62 72 65  ethod = 0;.  bre
1f22d 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1f22e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1f22f 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
1f230 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f231 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1f232 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65 6e 20 50   Opcode: VOpen P
1f233 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  1 * * P4 *.**.**
1f234 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
1f235 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61   to a virtual ta
1f236 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73  ble object, an s
1f237 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
1f238 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 69 73 20  cture..** P1 is 
1f239 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e  a cursor number.
1f23a 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 6f 70    This opcode op
1f23b 65 6e 73 20 61 20 63 75 72 73 6f 72 20 74 6f 20  ens a cursor to 
1f23c 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74  the virtual.** t
1f23d 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 73 20  able and stores 
1f23e 74 68 61 74 20 63 75 72 73 6f 72 20 69 6e 20 50  that cursor in P
1f23f 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4f  1..*/.case OP_VO
1f240 70 65 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  pen: {.#if 0  /*
1f241 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1f242 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 66   moved into u.cf
1f243 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72   */.  VdbeCursor
1f244 20 2a 70 43 75 72 3b 0a 20 20 73 71 6c 69 74 65   *pCur;.  sqlite
1f245 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
1f246 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 73 71  VtabCursor;.  sq
1f247 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
1f248 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64  b;.  sqlite3_mod
1f249 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65  ule *pModule;.#e
1f24a 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1f24b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1f24c 74 6f 20 75 2e 63 66 20 2a 2f 0a 0a 20 20 75 2e  to u.cf */..  u.
1f24d 63 66 2e 70 43 75 72 20 3d 20 30 3b 0a 20 20 75  cf.pCur = 0;.  u
1f24e 2e 63 66 2e 70 56 74 61 62 43 75 72 73 6f 72 20  .cf.pVtabCursor 
1f24f 3d 20 30 3b 0a 20 20 75 2e 63 66 2e 70 56 74 61  = 0;.  u.cf.pVta
1f250 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61  b = pOp->p4.pVta
1f251 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 66  b->pVtab;.  u.cf
1f252 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69  .pModule = (sqli
1f253 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 75 2e 63  te3_module *)u.c
1f254 66 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  f.pVtab->pModule
1f255 3b 0a 20 20 61 73 73 65 72 74 28 75 2e 63 66 2e  ;.  assert(u.cf.
1f256 70 56 74 61 62 20 26 26 20 75 2e 63 66 2e 70 4d  pVtab && u.cf.pM
1f257 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20 73 71  odule);.  if( sq
1f258 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1f259 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1f25a 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1f25b 20 72 63 20 3d 20 75 2e 63 66 2e 70 4d 6f 64 75   rc = u.cf.pModu
1f25c 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63 66 2e 70  le->xOpen(u.cf.p
1f25d 56 74 61 62 2c 20 26 75 2e 63 66 2e 70 56 74 61  Vtab, &u.cf.pVta
1f25e 62 43 75 72 73 6f 72 29 3b 0a 20 20 73 71 6c 69  bCursor);.  sqli
1f25f 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1f260 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e  >zErrMsg);.  p->
1f261 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 66 2e 70  zErrMsg = u.cf.p
1f262 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20  Vtab->zErrMsg;. 
1f263 20 75 2e 63 66 2e 70 56 74 61 62 2d 3e 7a 45 72   u.cf.pVtab->zEr
1f264 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20  rMsg = 0;.  if( 
1f265 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1f266 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1f267 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1f268 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d    if( SQLITE_OK=
1f269 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e  =rc ){.    /* In
1f26a 69 74 69 61 6c 69 7a 65 20 73 71 6c 69 74 65 33  itialize sqlite3
1f26b 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73  _vtab_cursor bas
1f26c 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20 20 20 75  e class */.    u
1f26d 2e 63 66 2e 70 56 74 61 62 43 75 72 73 6f 72 2d  .cf.pVtabCursor-
1f26e 3e 70 56 74 61 62 20 3d 20 75 2e 63 66 2e 70 56  >pVtab = u.cf.pV
1f26f 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69  tab;..    /* Ini
1f270 74 69 61 6c 69 73 65 20 76 64 62 65 20 63 75 72  tialise vdbe cur
1f271 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  sor object */.  
1f272 20 20 75 2e 63 66 2e 70 43 75 72 20 3d 20 61 6c    u.cf.pCur = al
1f273 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c 20  locateCursor(p, 
1f274 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d 31 2c 20  pOp->p1, 0, -1, 
1f275 30 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 63 66  0);.    if( u.cf
1f276 2e 70 43 75 72 20 29 7b 0a 20 20 20 20 20 20 75  .pCur ){.      u
1f277 2e 63 66 2e 70 43 75 72 2d 3e 70 56 74 61 62 43  .cf.pCur->pVtabC
1f278 75 72 73 6f 72 20 3d 20 75 2e 63 66 2e 70 56 74  ursor = u.cf.pVt
1f279 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20  abCursor;.      
1f27a 75 2e 63 66 2e 70 43 75 72 2d 3e 70 4d 6f 64 75  u.cf.pCur->pModu
1f27b 6c 65 20 3d 20 75 2e 63 66 2e 70 56 74 61 62 43  le = u.cf.pVtabC
1f27c 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d 3e 70 4d  ursor->pVtab->pM
1f27d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65 6c 73 65  odule;.    }else
1f27e 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
1f27f 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
1f280 20 20 20 20 75 2e 63 66 2e 70 4d 6f 64 75 6c 65      u.cf.pModule
1f281 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 66 2e 70 56  ->xClose(u.cf.pV
1f282 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  tabCursor);.    
1f283 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  }.  }.  break;.}
1f284 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1f285 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
1f286 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BLE */..#ifndef 
1f287 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1f288 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f  UALTABLE./* Opco
1f289 64 65 3a 20 56 46 69 6c 74 65 72 20 50 31 20 50  de: VFilter P1 P
1f28a 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
1f28b 50 31 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f  P1 is a cursor o
1f28c 70 65 6e 65 64 20 75 73 69 6e 67 20 56 4f 70 65  pened using VOpe
1f28d 6e 2e 20 20 50 32 20 69 73 20 61 6e 20 61 64 64  n.  P2 is an add
1f28e 72 65 73 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20  ress to jump to 
1f28f 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c 74 65 72  if.** the filter
1f290 65 64 20 72 65 73 75 6c 74 20 73 65 74 20 69 73  ed result set is
1f291 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 50 34   empty..**.** P4
1f292 20 69 73 20 65 69 74 68 65 72 20 4e 55 4c 4c 20   is either NULL 
1f293 6f 72 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  or a string that
1f294 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 62   was generated b
1f295 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 65 78  y the xBestIndex
1f296 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  .** method of th
1f297 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 69  e module.  The i
1f298 6e 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66  nterpretation of
1f299 20 74 68 65 20 50 34 20 73 74 72 69 6e 67 20 69   the P4 string i
1f29a 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20 74 68 65  s left.** to the
1f29b 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
1f29c 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  tation..**.** Th
1f29d 69 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65  is opcode invoke
1f29e 73 20 74 68 65 20 78 46 69 6c 74 65 72 20 6d 65  s the xFilter me
1f29f 74 68 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74  thod on the virt
1f2a0 75 61 6c 20 74 61 62 6c 65 20 73 70 65 63 69 66  ual table specif
1f2a1 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e 20 20 54  ied.** by P1.  T
1f2a2 68 65 20 69 6e 74 65 67 65 72 20 71 75 65 72 79  he integer query
1f2a3 20 70 6c 61 6e 20 70 61 72 61 6d 65 74 65 72 20   plan parameter 
1f2a4 74 6f 20 78 46 69 6c 74 65 72 20 69 73 20 73 74  to xFilter is st
1f2a5 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
1f2a6 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73 74 65 72  .** P3. Register
1f2a7 20 50 33 2b 31 20 73 74 6f 72 65 73 20 74 68 65   P3+1 stores the
1f2a8 20 61 72 67 63 20 70 61 72 61 6d 65 74 65 72 20   argc parameter 
1f2a9 74 6f 20 62 65 20 70 61 73 73 65 64 20 74 6f 20  to be passed to 
1f2aa 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65 72 20 6d  the.** xFilter m
1f2ab 65 74 68 6f 64 2e 20 52 65 67 69 73 74 65 72 73  ethod. Registers
1f2ac 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61 72 67 63   P3+2..P3+1+argc
1f2ad 20 61 72 65 20 74 68 65 20 61 72 67 63 0a 2a 2a   are the argc.**
1f2ae 20 61 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61   additional para
1f2af 6d 65 74 65 72 73 20 77 68 69 63 68 20 61 72 65  meters which are
1f2b0 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 78 46   passed to.** xF
1f2b1 69 6c 74 65 72 20 61 73 20 61 72 67 76 2e 20 52  ilter as argv. R
1f2b2 65 67 69 73 74 65 72 20 50 33 2b 32 20 62 65 63  egister P3+2 bec
1f2b3 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20 77 68 65  omes argv[0] whe
1f2b4 6e 20 70 61 73 73 65 64 20 74 6f 20 78 46 69 6c  n passed to xFil
1f2b5 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d  ter..**.** A jum
1f2b6 70 20 69 73 20 6d 61 64 65 20 74 6f 20 50 32 20  p is made to P2 
1f2b7 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  if the result se
1f2b8 74 20 61 66 74 65 72 20 66 69 6c 74 65 72 69 6e  t after filterin
1f2b9 67 20 77 6f 75 6c 64 20 62 65 20 65 6d 70 74 79  g would be empty
1f2ba 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 46 69  ..*/.case OP_VFi
1f2bb 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d  lter: {   /* jum
1f2bc 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  p */.#if 0  /* l
1f2bd 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1f2be 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 67 20 2a  oved into u.cg *
1f2bf 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20  /.  int nArg;.  
1f2c0 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 63 6f  int iQuery;.  co
1f2c1 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
1f2c2 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d  le *pModule;.  M
1f2c3 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20 20 4d 65  em *pQuery;.  Me
1f2c4 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73 71 6c 69  m *pArgc;.  sqli
1f2c5 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
1f2c6 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20  *pVtabCursor;.  
1f2c7 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1f2c8 74 61 62 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  tab;.  VdbeCurso
1f2c9 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72  r *pCur;.  int r
1f2ca 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d  es;.  int i;.  M
1f2cb 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23 65 6e 64  em **apArg;.#end
1f2cc 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1f2cd 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f2ce 20 75 2e 63 67 20 2a 2f 0a 0a 20 20 75 2e 63 67   u.cg */..  u.cg
1f2cf 2e 70 51 75 65 72 79 20 3d 20 26 61 4d 65 6d 5b  .pQuery = &aMem[
1f2d0 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 67  pOp->p3];.  u.cg
1f2d1 2e 70 41 72 67 63 20 3d 20 26 75 2e 63 67 2e 70  .pArgc = &u.cg.p
1f2d2 51 75 65 72 79 5b 31 5d 3b 0a 20 20 75 2e 63 67  Query[1];.  u.cg
1f2d3 2e 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72  .pCur = p->apCsr
1f2d4 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 52 45 47  [pOp->p1];.  REG
1f2d5 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1f2d6 3e 70 33 2c 20 75 2e 63 67 2e 70 51 75 65 72 79  >p3, u.cg.pQuery
1f2d7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63  );.  assert( u.c
1f2d8 67 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  g.pCur->pVtabCur
1f2d9 73 6f 72 20 29 3b 0a 20 20 75 2e 63 67 2e 70 56  sor );.  u.cg.pV
1f2da 74 61 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 67  tabCursor = u.cg
1f2db 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73  .pCur->pVtabCurs
1f2dc 6f 72 3b 0a 20 20 75 2e 63 67 2e 70 56 74 61 62  or;.  u.cg.pVtab
1f2dd 20 3d 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72   = u.cg.pVtabCur
1f2de 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e  sor->pVtab;.  u.
1f2df 63 67 2e 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 63  cg.pModule = u.c
1f2e0 67 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  g.pVtab->pModule
1f2e1 3b 0a 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 65  ;..  /* Grab the
1f2e2 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 20 61 6e   index number an
1f2e3 64 20 61 72 67 63 20 70 61 72 61 6d 65 74 65 72  d argc parameter
1f2e4 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28  s */.  assert( (
1f2e5 75 2e 63 67 2e 70 51 75 65 72 79 2d 3e 66 6c 61  u.cg.pQuery->fla
1f2e6 67 73 26 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 26  gs&MEM_Int)!=0 &
1f2e7 26 20 75 2e 63 67 2e 70 41 72 67 63 2d 3e 66 6c  & u.cg.pArgc->fl
1f2e8 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74 20 29 3b 0a  ags==MEM_Int );.
1f2e9 20 20 75 2e 63 67 2e 6e 41 72 67 20 3d 20 28 69    u.cg.nArg = (i
1f2ea 6e 74 29 75 2e 63 67 2e 70 41 72 67 63 2d 3e 75  nt)u.cg.pArgc->u
1f2eb 2e 69 3b 0a 20 20 75 2e 63 67 2e 69 51 75 65 72  .i;.  u.cg.iQuer
1f2ec 79 20 3d 20 28 69 6e 74 29 75 2e 63 67 2e 70 51  y = (int)u.cg.pQ
1f2ed 75 65 72 79 2d 3e 75 2e 69 3b 0a 0a 20 20 2f 2a  uery->u.i;..  /*
1f2ee 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 46 69 6c   Invoke the xFil
1f2ef 74 65 72 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ter method */.  
1f2f0 7b 0a 20 20 20 20 75 2e 63 67 2e 72 65 73 20 3d  {.    u.cg.res =
1f2f1 20 30 3b 0a 20 20 20 20 75 2e 63 67 2e 61 70 41   0;.    u.cg.apA
1f2f2 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20  rg = p->apArg;. 
1f2f3 20 20 20 66 6f 72 28 75 2e 63 67 2e 69 20 3d 20     for(u.cg.i = 
1f2f4 30 3b 20 75 2e 63 67 2e 69 3c 75 2e 63 67 2e 6e  0; u.cg.i<u.cg.n
1f2f5 41 72 67 3b 20 75 2e 63 67 2e 69 2b 2b 29 7b 0a  Arg; u.cg.i++){.
1f2f6 20 20 20 20 20 20 75 2e 63 67 2e 61 70 41 72 67        u.cg.apArg
1f2f7 5b 75 2e 63 67 2e 69 5d 20 3d 20 26 75 2e 63 67  [u.cg.i] = &u.cg
1f2f8 2e 70 41 72 67 63 5b 75 2e 63 67 2e 69 2b 31 5d  .pArgc[u.cg.i+1]
1f2f9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1f2fa 64 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28  dbeMemStoreType(
1f2fb 75 2e 63 67 2e 61 70 41 72 67 5b 75 2e 63 67 2e  u.cg.apArg[u.cg.
1f2fc 69 5d 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  i]);.    }..    
1f2fd 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1f2fe 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20  yOff(db) ) goto 
1f2ff 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1f300 75 73 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74  use;.    p->inVt
1f301 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20  abMethod = 1;.  
1f302 20 20 72 63 20 3d 20 75 2e 63 67 2e 70 4d 6f 64    rc = u.cg.pMod
1f303 75 6c 65 2d 3e 78 46 69 6c 74 65 72 28 75 2e 63  ule->xFilter(u.c
1f304 67 2e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 75  g.pVtabCursor, u
1f305 2e 63 67 2e 69 51 75 65 72 79 2c 20 70 4f 70 2d  .cg.iQuery, pOp-
1f306 3e 70 34 2e 7a 2c 20 75 2e 63 67 2e 6e 41 72 67  >p4.z, u.cg.nArg
1f307 2c 20 75 2e 63 67 2e 61 70 41 72 67 29 3b 0a 20  , u.cg.apArg);. 
1f308 20 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74 68     p->inVtabMeth
1f309 6f 64 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  od = 0;.    sqli
1f30a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1f30b 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70  >zErrMsg);.    p
1f30c 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 67  ->zErrMsg = u.cg
1f30d 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b  .pVtab->zErrMsg;
1f30e 0a 20 20 20 20 75 2e 63 67 2e 70 56 74 61 62 2d  .    u.cg.pVtab-
1f30f 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1f310 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1f311 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 75 2e 63  _OK ){.      u.c
1f312 67 2e 72 65 73 20 3d 20 75 2e 63 67 2e 70 4d 6f  g.res = u.cg.pMo
1f313 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63 67 2e  dule->xEof(u.cg.
1f314 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20  pVtabCursor);.  
1f315 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
1f316 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20  te3SafetyOn(db) 
1f317 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1f318 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 0a 20 20 20  _to_misuse;..   
1f319 20 69 66 28 20 75 2e 63 67 2e 72 65 73 20 29 7b   if( u.cg.res ){
1f31a 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
1f31b 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20  >p2 - 1;.    }. 
1f31c 20 7d 0a 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e   }.  u.cg.pCur->
1f31d 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 0a 20 20  nullRow = 0;..  
1f31e 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1f31f 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1f320 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1f321 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f322 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1f323 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 6f 6c  ./* Opcode: VCol
1f324 75 6d 6e 20 50 31 20 50 32 20 50 33 20 2a 20 2a  umn P1 P2 P3 * *
1f325 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65  .**.** Store the
1f326 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32   value of the P2
1f327 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a  -th column of.**
1f328 20 74 68 65 20 72 6f 77 20 6f 66 20 74 68 65 20   the row of the 
1f329 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 74 68  virtual-table th
1f32a 61 74 20 74 68 65 20 0a 2a 2a 20 50 31 20 63 75  at the .** P1 cu
1f32b 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e 67  rsor is pointing
1f32c 20 74 6f 20 69 6e 74 6f 20 72 65 67 69 73 74 65   to into registe
1f32d 72 20 50 33 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  r P3..*/.case OP
1f32e 5f 56 43 6f 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20  _VColumn: {.#if 
1f32f 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1f330 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f331 20 75 2e 63 68 20 2a 2f 0a 20 20 73 71 6c 69 74   u.ch */.  sqlit
1f332 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1f333 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
1f334 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
1f335 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b 0a 20  .  Mem *pDest;. 
1f336 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1f337 20 73 43 6f 6e 74 65 78 74 3b 0a 23 65 6e 64 69   sContext;.#endi
1f338 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1f339 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1f33a 75 2e 63 68 20 2a 2f 0a 0a 20 20 56 64 62 65 43  u.ch */..  VdbeC
1f33b 75 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d  ursor *pCur = p-
1f33c 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1f33d 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
1f33e 3e 70 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a  >pVtabCursor );.
1f33f 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1f340 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d  3>0 && pOp->p3<=
1f341 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e 63  p->nMem );.  u.c
1f342 68 2e 70 44 65 73 74 20 3d 20 26 61 4d 65 6d 5b  h.pDest = &aMem[
1f343 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20  pOp->p3];.  if( 
1f344 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b  pCur->nullRow ){
1f345 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1f346 4d 65 6d 53 65 74 4e 75 6c 6c 28 75 2e 63 68 2e  MemSetNull(u.ch.
1f347 70 44 65 73 74 29 3b 0a 20 20 20 20 62 72 65 61  pDest);.    brea
1f348 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 68 2e 70 56  k;.  }.  u.ch.pV
1f349 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 74 61  tab = pCur->pVta
1f34a 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a  bCursor->pVtab;.
1f34b 20 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 20 3d    u.ch.pModule =
1f34c 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 70 4d 6f   u.ch.pVtab->pMo
1f34d 64 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20  dule;.  assert( 
1f34e 75 2e 63 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43  u.ch.pModule->xC
1f34f 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d 73 65  olumn );.  memse
1f350 74 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74  t(&u.ch.sContext
1f351 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75 2e 63 68  , 0, sizeof(u.ch
1f352 2e 73 43 6f 6e 74 65 78 74 29 29 3b 0a 0a 20 20  .sContext));..  
1f353 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65  /* The output ce
1f354 6c 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68  ll may already h
1f355 61 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c  ave a buffer all
1f356 6f 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a  ocated. Move.  *
1f357 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f  * the current co
1f358 6e 74 65 6e 74 73 20 74 6f 20 75 2e 63 68 2e 73  ntents to u.ch.s
1f359 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20 69 6e 20  Context.s so in 
1f35a 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66 75  case the user-fu
1f35b 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 63 61 6e 20  nction.  ** can 
1f35c 75 73 65 20 74 68 65 20 61 6c 72 65 61 64 79 20  use the already 
1f35d 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
1f35e 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c 6f   instead of allo
1f35f 63 61 74 69 6e 67 20 61 0a 20 20 2a 2a 20 6e 65  cating a.  ** ne
1f360 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 73 71  w one..  */.  sq
1f361 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
1f362 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e  (&u.ch.sContext.
1f363 73 2c 20 75 2e 63 68 2e 70 44 65 73 74 29 3b 0a  s, u.ch.pDest);.
1f364 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1f365 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e  (&u.ch.sContext.
1f366 73 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20  s, MEM_Null);.. 
1f367 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1f368 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
1f369 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1f36a 73 75 73 65 3b 0a 20 20 72 63 20 3d 20 75 2e 63  suse;.  rc = u.c
1f36b 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75  h.pModule->xColu
1f36c 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 62 43 75  mn(pCur->pVtabCu
1f36d 72 73 6f 72 2c 20 26 75 2e 63 68 2e 73 43 6f 6e  rsor, &u.ch.sCon
1f36e 74 65 78 74 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a  text, pOp->p2);.
1f36f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1f370 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  db, p->zErrMsg);
1f371 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  .  p->zErrMsg = 
1f372 75 2e 63 68 2e 70 56 74 61 62 2d 3e 7a 45 72 72  u.ch.pVtab->zErr
1f373 4d 73 67 3b 0a 20 20 75 2e 63 68 2e 70 56 74 61  Msg;.  u.ch.pVta
1f374 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  b->zErrMsg = 0;.
1f375 20 20 69 66 28 20 75 2e 63 68 2e 73 43 6f 6e 74    if( u.ch.sCont
1f376 65 78 74 2e 69 73 45 72 72 6f 72 20 29 7b 0a 20  ext.isError ){. 
1f377 20 20 20 72 63 20 3d 20 75 2e 63 68 2e 73 43 6f     rc = u.ch.sCo
1f378 6e 74 65 78 74 2e 69 73 45 72 72 6f 72 3b 0a 20  ntext.isError;. 
1f379 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68   }..  /* Copy th
1f37a 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  e result of the 
1f37b 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65 20  function to the 
1f37c 50 33 20 72 65 67 69 73 74 65 72 2e 20 57 65 0a  P3 register. We.
1f37d 20 20 2a 2a 20 64 6f 20 74 68 69 73 20 72 65 67    ** do this reg
1f37e 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68  ardless of wheth
1f37f 65 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72  er or not an err
1f380 6f 72 20 6f 63 63 75 72 72 65 64 20 74 6f 20 65  or occurred to e
1f381 6e 73 75 72 65 20 61 6e 79 0a 20 20 2a 2a 20 64  nsure any.  ** d
1f382 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69 6f  ynamic allocatio
1f383 6e 20 69 6e 20 75 2e 63 68 2e 73 43 6f 6e 74 65  n in u.ch.sConte
1f384 78 74 2e 73 20 28 61 20 4d 65 6d 20 73 74 72 75  xt.s (a Mem stru
1f385 63 74 29 20 69 73 20 20 72 65 6c 65 61 73 65 64  ct) is  released
1f386 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
1f387 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69  VdbeChangeEncodi
1f388 6e 67 28 26 75 2e 63 68 2e 73 43 6f 6e 74 65 78  ng(&u.ch.sContex
1f389 74 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  t.s, encoding);.
1f38a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1f38b 4d 6f 76 65 28 75 2e 63 68 2e 70 44 65 73 74 2c  Move(u.ch.pDest,
1f38c 20 26 75 2e 63 68 2e 73 43 6f 6e 74 65 78 74 2e   &u.ch.sContext.
1f38d 73 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54  s);.  REGISTER_T
1f38e 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e  RACE(pOp->p3, u.
1f38f 63 68 2e 70 44 65 73 74 29 3b 0a 20 20 55 50 44  ch.pDest);.  UPD
1f390 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
1f391 28 75 2e 63 68 2e 70 44 65 73 74 29 3b 0a 0a 20  (u.ch.pDest);.. 
1f392 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1f393 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20  tyOn(db) ){.    
1f394 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1f395 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20  o_misuse;.  }.  
1f396 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
1f397 65 6d 54 6f 6f 42 69 67 28 75 2e 63 68 2e 70 44  emTooBig(u.ch.pD
1f398 65 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  est) ){.    goto
1f399 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20   too_big;.  }.  
1f39a 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1f39b 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1f39c 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1f39d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f39e 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1f39f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4e 65 78  ./* Opcode: VNex
1f3a0 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  t P1 P2 * * *.**
1f3a1 0a 2a 2a 20 41 64 76 61 6e 63 65 20 76 69 72 74  .** Advance virt
1f3a2 75 61 6c 20 74 61 62 6c 65 20 50 31 20 74 6f 20  ual table P1 to 
1f3a3 74 68 65 20 6e 65 78 74 20 72 6f 77 20 69 6e 20  the next row in 
1f3a4 69 74 73 20 72 65 73 75 6c 74 20 73 65 74 20 61  its result set a
1f3a5 6e 64 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 69 6e  nd.** jump to in
1f3a6 73 74 72 75 63 74 69 6f 6e 20 50 32 2e 20 20 4f  struction P2.  O
1f3a7 72 2c 20 69 66 20 74 68 65 20 76 69 72 74 75 61  r, if the virtua
1f3a8 6c 20 74 61 62 6c 65 20 68 61 73 20 72 65 61 63  l table has reac
1f3a9 68 65 64 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f  hed.** the end o
1f3aa 66 20 69 74 73 20 72 65 73 75 6c 74 20 73 65 74  f its result set
1f3ab 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f  , then fall thro
1f3ac 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
1f3ad 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  instruction..*/.
1f3ae 63 61 73 65 20 4f 50 5f 56 4e 65 78 74 3a 20 7b  case OP_VNext: {
1f3af 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69     /* jump */.#i
1f3b0 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1f3b1 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1f3b2 74 6f 20 75 2e 63 69 20 2a 2f 0a 20 20 73 71 6c  to u.ci */.  sql
1f3b3 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
1f3b4 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ;.  const sqlite
1f3b5 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
1f3b6 65 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20  e;.  int res;.  
1f3b7 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75 72  VdbeCursor *pCur
1f3b8 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1f3b9 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1f3ba 64 20 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 0a  d into u.ci */..
1f3bb 20 20 75 2e 63 69 2e 72 65 73 20 3d 20 30 3b 0a    u.ci.res = 0;.
1f3bc 20 20 75 2e 63 69 2e 70 43 75 72 20 3d 20 70 2d    u.ci.pCur = p-
1f3bd 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1f3be 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 69 2e  .  assert( u.ci.
1f3bf 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f  pCur->pVtabCurso
1f3c0 72 20 29 3b 0a 20 20 69 66 28 20 75 2e 63 69 2e  r );.  if( u.ci.
1f3c1 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b  pCur->nullRow ){
1f3c2 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
1f3c3 20 20 75 2e 63 69 2e 70 56 74 61 62 20 3d 20 75    u.ci.pVtab = u
1f3c4 2e 63 69 2e 70 43 75 72 2d 3e 70 56 74 61 62 43  .ci.pCur->pVtabC
1f3c5 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20  ursor->pVtab;.  
1f3c6 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 20 3d 20 75  u.ci.pModule = u
1f3c7 2e 63 69 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  .ci.pVtab->pModu
1f3c8 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  le;.  assert( u.
1f3c9 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78  ci.pModule->xNex
1f3ca 74 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b  t );..  /* Invok
1f3cb 65 20 74 68 65 20 78 4e 65 78 74 28 29 20 6d 65  e the xNext() me
1f3cc 74 68 6f 64 20 6f 66 20 74 68 65 20 6d 6f 64 75  thod of the modu
1f3cd 6c 65 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20  le. There is no 
1f3ce 77 61 79 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  way for the.  **
1f3cf 20 75 6e 64 65 72 6c 79 69 6e 67 20 69 6d 70 6c   underlying impl
1f3d0 65 6d 65 6e 74 61 74 69 6f 6e 20 74 6f 20 72 65  ementation to re
1f3d1 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 66  turn an error if
1f3d2 20 6f 6e 65 20 6f 63 63 75 72 73 20 64 75 72 69   one occurs duri
1f3d3 6e 67 0a 20 20 2a 2a 20 78 4e 65 78 74 28 29 2e  ng.  ** xNext().
1f3d4 20 49 6e 73 74 65 61 64 2c 20 69 66 20 61 6e 20   Instead, if an 
1f3d5 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 72  error occurs, tr
1f3d6 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 28  ue is returned (
1f3d7 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 0a  indicating that.
1f3d8 20 20 2a 2a 20 64 61 74 61 20 69 73 20 61 76 61    ** data is ava
1f3d9 69 6c 61 62 6c 65 29 20 61 6e 64 20 74 68 65 20  ilable) and the 
1f3da 65 72 72 6f 72 20 63 6f 64 65 20 72 65 74 75 72  error code retur
1f3db 6e 65 64 20 77 68 65 6e 20 78 43 6f 6c 75 6d 6e  ned when xColumn
1f3dc 20 6f 72 0a 20 20 2a 2a 20 73 6f 6d 65 20 6f 74   or.  ** some ot
1f3dd 68 65 72 20 6d 65 74 68 6f 64 20 69 73 20 6e 65  her method is ne
1f3de 78 74 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68  xt invoked on th
1f3df 65 20 73 61 76 65 20 76 69 72 74 75 61 6c 20 74  e save virtual t
1f3e0 61 62 6c 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a  able cursor..  *
1f3e1 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
1f3e2 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67  afetyOff(db) ) g
1f3e3 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1f3e4 5f 6d 69 73 75 73 65 3b 0a 20 20 70 2d 3e 69 6e  _misuse;.  p->in
1f3e5 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a  VtabMethod = 1;.
1f3e6 20 20 72 63 20 3d 20 75 2e 63 69 2e 70 4d 6f 64    rc = u.ci.pMod
1f3e7 75 6c 65 2d 3e 78 4e 65 78 74 28 75 2e 63 69 2e  ule->xNext(u.ci.
1f3e8 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f  pCur->pVtabCurso
1f3e9 72 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d  r);.  p->inVtabM
1f3ea 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 73 71 6c  ethod = 0;.  sql
1f3eb 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1f3ec 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d  ->zErrMsg);.  p-
1f3ed 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 69 2e  >zErrMsg = u.ci.
1f3ee 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a  pVtab->zErrMsg;.
1f3ef 20 20 75 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45    u.ci.pVtab->zE
1f3f0 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28  rrMsg = 0;.  if(
1f3f1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1f3f2 7b 0a 20 20 20 20 75 2e 63 69 2e 72 65 73 20 3d  {.    u.ci.res =
1f3f3 20 75 2e 63 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78   u.ci.pModule->x
1f3f4 45 6f 66 28 75 2e 63 69 2e 70 43 75 72 2d 3e 70  Eof(u.ci.pCur->p
1f3f5 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 7d  VtabCursor);.  }
1f3f6 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1f3f7 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74  fetyOn(db) ) got
1f3f8 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1f3f9 69 73 75 73 65 3b 0a 0a 20 20 69 66 28 20 21 75  isuse;..  if( !u
1f3fa 2e 63 69 2e 72 65 73 20 29 7b 0a 20 20 20 20 2f  .ci.res ){.    /
1f3fb 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 64 61  * If there is da
1f3fc 74 61 2c 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a  ta, jump to P2 *
1f3fd 2f 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  /.    pc = pOp->
1f3fe 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72  p2 - 1;.  }.  br
1f3ff 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1f400 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1f401 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69  TUALTABLE */..#i
1f402 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f403 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f  T_VIRTUALTABLE./
1f404 2a 20 4f 70 63 6f 64 65 3a 20 56 52 65 6e 61 6d  * Opcode: VRenam
1f405 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  e P1 * * P4 *.**
1f406 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e  .** P4 is a poin
1f407 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c  ter to a virtual
1f408 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61   table object, a
1f409 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73  n sqlite3_vtab s
1f40a 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69  tructure..** Thi
1f40b 73 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73  s opcode invokes
1f40c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
1f40d 6e 67 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f  ng xRename metho
1f40e 64 2e 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20  d. The value.** 
1f40f 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69  in register P1 i
1f410 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
1f411 7a 4e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 74  zName argument t
1f412 6f 20 74 68 65 20 78 52 65 6e 61 6d 65 20 6d 65  o the xRename me
1f413 74 68 6f 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  thod..*/.case OP
1f414 5f 56 52 65 6e 61 6d 65 3a 20 7b 0a 23 69 66 20  _VRename: {.#if 
1f415 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1f416 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1f417 20 75 2e 63 6a 20 2a 2f 0a 20 20 73 71 6c 69 74   u.cj */.  sqlit
1f418 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1f419 20 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a 23 65    Mem *pName;.#e
1f41a 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1f41b 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1f41c 74 6f 20 75 2e 63 6a 20 2a 2f 0a 0a 20 20 75 2e  to u.cj */..  u.
1f41d 63 6a 2e 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e  cj.pVtab = pOp->
1f41e 70 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b  p4.pVtab->pVtab;
1f41f 0a 20 20 75 2e 63 6a 2e 70 4e 61 6d 65 20 3d 20  .  u.cj.pName = 
1f420 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  &aMem[pOp->p1];.
1f421 20 20 61 73 73 65 72 74 28 20 75 2e 63 6a 2e 70    assert( u.cj.p
1f422 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78  Vtab->pModule->x
1f423 52 65 6e 61 6d 65 20 29 3b 0a 20 20 52 45 47 49  Rename );.  REGI
1f424 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1f425 70 31 2c 20 75 2e 63 6a 2e 70 4e 61 6d 65 29 3b  p1, u.cj.pName);
1f426 0a 20 20 61 73 73 65 72 74 28 20 75 2e 63 6a 2e  .  assert( u.cj.
1f427 70 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 26 20 4d  pName->flags & M
1f428 45 4d 5f 53 74 72 20 29 3b 0a 20 20 69 66 28 20  EM_Str );.  if( 
1f429 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1f42a 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1f42b 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1f42c 0a 20 20 72 63 20 3d 20 75 2e 63 6a 2e 70 56 74  .  rc = u.cj.pVt
1f42d 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65  ab->pModule->xRe
1f42e 6e 61 6d 65 28 75 2e 63 6a 2e 70 56 74 61 62 2c  name(u.cj.pVtab,
1f42f 20 75 2e 63 6a 2e 70 4e 61 6d 65 2d 3e 7a 29 3b   u.cj.pName->z);
1f430 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1f431 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1f432 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d  ;.  p->zErrMsg =
1f433 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e 7a 45 72   u.cj.pVtab->zEr
1f434 72 4d 73 67 3b 0a 20 20 75 2e 63 6a 2e 70 56 74  rMsg;.  u.cj.pVt
1f435 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
1f436 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
1f437 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74  fetyOn(db) ) got
1f438 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1f439 69 73 75 73 65 3b 0a 0a 20 20 62 72 65 61 6b 3b  isuse;..  break;
1f43a 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
1f43b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1f43c 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f  IRTUALTABLE./* O
1f43d 70 63 6f 64 65 3a 20 56 55 70 64 61 74 65 20 50  pcode: VUpdate P
1f43e 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1f43f 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
1f440 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20  er to a virtual 
1f441 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e  table object, an
1f442 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74   sqlite3_vtab st
1f443 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 69 73  ructure..** This
1f444 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20   opcode invokes 
1f445 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
1f446 67 20 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64  g xUpdate method
1f447 2e 20 50 32 20 76 61 6c 75 65 73 0a 2a 2a 20 61  . P2 values.** a
1f448 72 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 6d 65  re contiguous me
1f449 6d 6f 72 79 20 63 65 6c 6c 73 20 73 74 61 72 74  mory cells start
1f44a 69 6e 67 20 61 74 20 50 33 20 74 6f 20 70 61 73  ing at P3 to pas
1f44b 73 20 74 6f 20 74 68 65 20 78 55 70 64 61 74 65  s to the xUpdate
1f44c 20 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 2e   .** invocation.
1f44d 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   The value in re
1f44e 67 69 73 74 65 72 20 28 50 33 2b 50 32 2d 31 29  gister (P3+P2-1)
1f44f 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
1f450 74 68 65 20 0a 2a 2a 20 70 32 74 68 20 65 6c 65  the .** p2th ele
1f451 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 67 76  ment of the argv
1f452 20 61 72 72 61 79 20 70 61 73 73 65 64 20 74 6f   array passed to
1f453 20 78 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20   xUpdate..**.** 
1f454 54 68 65 20 78 55 70 64 61 74 65 20 6d 65 74 68  The xUpdate meth
1f455 6f 64 20 77 69 6c 6c 20 64 6f 20 61 20 44 45 4c  od will do a DEL
1f456 45 54 45 20 6f 72 20 61 6e 20 49 4e 53 45 52 54  ETE or an INSERT
1f457 20 6f 72 20 62 6f 74 68 2e 0a 2a 2a 20 54 68 65   or both..** The
1f458 20 61 72 67 76 5b 30 5d 20 65 6c 65 6d 65 6e 74   argv[0] element
1f459 20 28 77 68 69 63 68 20 63 6f 72 72 65 73 70 6f   (which correspo
1f45a 6e 64 73 20 74 6f 20 6d 65 6d 6f 72 79 20 63 65  nds to memory ce
1f45b 6c 6c 20 50 33 29 0a 2a 2a 20 69 73 20 74 68 65  ll P3).** is the
1f45c 20 72 6f 77 69 64 20 6f 66 20 61 20 72 6f 77 20   rowid of a row 
1f45d 74 6f 20 64 65 6c 65 74 65 2e 20 20 49 66 20 61  to delete.  If a
1f45e 72 67 76 5b 30 5d 20 69 73 20 4e 55 4c 4c 20 74  rgv[0] is NULL t
1f45f 68 65 6e 20 6e 6f 20 0a 2a 2a 20 64 65 6c 65 74  hen no .** delet
1f460 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20 54 68 65  ion occurs.  The
1f461 20 61 72 67 76 5b 31 5d 20 65 6c 65 6d 65 6e 74   argv[1] element
1f462 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66   is the rowid of
1f463 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77   the new .** row
1f464 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20 4e  .  This can be N
1f465 55 4c 4c 20 74 6f 20 68 61 76 65 20 74 68 65 20  ULL to have the 
1f466 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 65  virtual table se
1f467 6c 65 63 74 20 74 68 65 20 6e 65 77 20 0a 2a 2a  lect the new .**
1f468 20 72 6f 77 69 64 20 66 6f 72 20 69 74 73 65 6c   rowid for itsel
1f469 66 2e 20 20 54 68 65 20 73 75 62 73 65 71 75 65  f.  The subseque
1f46a 6e 74 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  nt elements in t
1f46b 68 65 20 61 72 72 61 79 20 61 72 65 20 0a 2a 2a  he array are .**
1f46c 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 63   the values of c
1f46d 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 6e 65  olumns in the ne
1f46e 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  w row..**.** If 
1f46f 50 32 3d 3d 31 20 74 68 65 6e 20 6e 6f 20 69 6e  P2==1 then no in
1f470 73 65 72 74 20 69 73 20 70 65 72 66 6f 72 6d 65  sert is performe
1f471 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74  d.  argv[0] is t
1f472 68 65 20 72 6f 77 69 64 20 6f 66 0a 2a 2a 20 61  he rowid of.** a
1f473 20 72 6f 77 20 74 6f 20 64 65 6c 65 74 65 2e 0a   row to delete..
1f474 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 20 62 6f  **.** P1 is a bo
1f475 6f 6c 65 61 6e 20 66 6c 61 67 2e 20 49 66 20 69  olean flag. If i
1f476 74 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65  t is set to true
1f477 20 61 6e 64 20 74 68 65 20 78 55 70 64 61 74 65   and the xUpdate
1f478 20 63 61 6c 6c 0a 2a 2a 20 69 73 20 73 75 63 63   call.** is succ
1f479 65 73 73 66 75 6c 2c 20 74 68 65 6e 20 74 68 65  essful, then the
1f47a 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
1f47b 62 79 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  by sqlite3_last_
1f47c 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 0a  insert_rowid() .
1f47d 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ** is set to the
1f47e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f   value of the ro
1f47f 77 69 64 20 66 6f 72 20 74 68 65 20 72 6f 77 20  wid for the row 
1f480 6a 75 73 74 20 69 6e 73 65 72 74 65 64 2e 0a 2a  just inserted..*
1f481 2f 0a 63 61 73 65 20 4f 50 5f 56 55 70 64 61 74  /.case OP_VUpdat
1f482 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e: {.#if 0  /* l
1f483 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1f484 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6b 20 2a  oved into u.ck *
1f485 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  /.  sqlite3_vtab
1f486 20 2a 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74   *pVtab;.  sqlit
1f487 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
1f488 6c 65 3b 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a  le;.  int nArg;.
1f489 20 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74    int i;.  sqlit
1f48a 65 5f 69 6e 74 36 34 20 72 6f 77 69 64 3b 0a 20  e_int64 rowid;. 
1f48b 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20   Mem **apArg;.  
1f48c 4d 65 6d 20 2a 70 58 3b 0a 23 65 6e 64 69 66 20  Mem *pX;.#endif 
1f48d 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1f48e 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1f48f 63 6b 20 2a 2f 0a 0a 20 20 75 2e 63 6b 2e 70 56  ck */..  u.ck.pV
1f490 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56  tab = pOp->p4.pV
1f491 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e  tab->pVtab;.  u.
1f492 63 6b 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71  ck.pModule = (sq
1f493 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 75  lite3_module *)u
1f494 2e 63 6b 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  .ck.pVtab->pModu
1f495 6c 65 3b 0a 20 20 75 2e 63 6b 2e 6e 41 72 67 20  le;.  u.ck.nArg 
1f496 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73  = pOp->p2;.  ass
1f497 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
1f498 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a 20 20 69  ==P4_VTAB );.  i
1f499 66 28 20 41 4c 57 41 59 53 28 75 2e 63 6b 2e 70  f( ALWAYS(u.ck.p
1f49a 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65 29  Module->xUpdate)
1f49b 20 29 7b 0a 20 20 20 20 75 2e 63 6b 2e 61 70 41   ){.    u.ck.apA
1f49c 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20  rg = p->apArg;. 
1f49d 20 20 20 75 2e 63 6b 2e 70 58 20 3d 20 26 61 4d     u.ck.pX = &aM
1f49e 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20  em[pOp->p3];.   
1f49f 20 66 6f 72 28 75 2e 63 6b 2e 69 3d 30 3b 20 75   for(u.ck.i=0; u
1f4a0 2e 63 6b 2e 69 3c 75 2e 63 6b 2e 6e 41 72 67 3b  .ck.i<u.ck.nArg;
1f4a1 20 75 2e 63 6b 2e 69 2b 2b 29 7b 0a 20 20 20 20   u.ck.i++){.    
1f4a2 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1f4a3 53 74 6f 72 65 54 79 70 65 28 75 2e 63 6b 2e 70  StoreType(u.ck.p
1f4a4 58 29 3b 0a 20 20 20 20 20 20 75 2e 63 6b 2e 61  X);.      u.ck.a
1f4a5 70 41 72 67 5b 75 2e 63 6b 2e 69 5d 20 3d 20 75  pArg[u.ck.i] = u
1f4a6 2e 63 6b 2e 70 58 3b 0a 20 20 20 20 20 20 75 2e  .ck.pX;.      u.
1f4a7 63 6b 2e 70 58 2b 2b 3b 0a 20 20 20 20 7d 0a 20  ck.pX++;.    }. 
1f4a8 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1f4a9 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1f4aa 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1f4ab 6d 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d  misuse;.    rc =
1f4ac 20 75 2e 63 6b 2e 70 4d 6f 64 75 6c 65 2d 3e 78   u.ck.pModule->x
1f4ad 55 70 64 61 74 65 28 75 2e 63 6b 2e 70 56 74 61  Update(u.ck.pVta
1f4ae 62 2c 20 75 2e 63 6b 2e 6e 41 72 67 2c 20 75 2e  b, u.ck.nArg, u.
1f4af 63 6b 2e 61 70 41 72 67 2c 20 26 75 2e 63 6b 2e  ck.apArg, &u.ck.
1f4b0 72 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69  rowid);.    sqli
1f4b1 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
1f4b2 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70  >zErrMsg);.    p
1f4b3 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 6b  ->zErrMsg = u.ck
1f4b4 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b  .pVtab->zErrMsg;
1f4b5 0a 20 20 20 20 75 2e 63 6b 2e 70 56 74 61 62 2d  .    u.ck.pVtab-
1f4b6 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
1f4b7 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1f4b8 65 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f  etyOn(db) ) goto
1f4b9 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1f4ba 73 75 73 65 3b 0a 20 20 20 20 69 66 28 20 72 63  suse;.    if( rc
1f4bb 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
1f4bc 4f 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 20 20  Op->p1 ){.      
1f4bd 61 73 73 65 72 74 28 20 75 2e 63 6b 2e 6e 41 72  assert( u.ck.nAr
1f4be 67 3e 31 20 26 26 20 75 2e 63 6b 2e 61 70 41 72  g>1 && u.ck.apAr
1f4bf 67 5b 30 5d 20 26 26 20 28 75 2e 63 6b 2e 61 70  g[0] && (u.ck.ap
1f4c0 41 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d 45  Arg[0]->flags&ME
1f4c1 4d 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20 20  M_Null) );.     
1f4c2 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d   db->lastRowid =
1f4c3 20 75 2e 63 6b 2e 72 6f 77 69 64 3b 0a 20 20 20   u.ck.rowid;.   
1f4c4 20 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67   }.    p->nChang
1f4c5 65 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  e++;.  }.  break
1f4c6 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1f4c7 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1f4c8 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64  LTABLE */..#ifnd
1f4c9 65 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ef  SQLITE_OMIT_
1f4ca 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a  PAGER_PRAGMAS./*
1f4cb 20 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f 75   Opcode: Pagecou
1f4cc 6e 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  nt P1 P2 * * *.*
1f4cd 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63  *.** Write the c
1f4ce 75 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66  urrent number of
1f4cf 20 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61   pages in databa
1f4d0 73 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 20  se P1 to memory 
1f4d1 63 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  cell P2..*/.case
1f4d2 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 7b   OP_Pagecount: {
1f4d3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f              /* o
1f4d4 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
1f4d5 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1f4d6 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1f4d7 64 20 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f 0a 20  d into u.cl */. 
1f4d8 20 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 6e   int p1;.  int n
1f4d9 50 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70  Page;.  Pager *p
1f4da 50 61 67 65 72 3b 0a 23 65 6e 64 69 66 20 2f 2a  Pager;.#endif /*
1f4db 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1f4dc 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6c   moved into u.cl
1f4dd 20 2a 2f 0a 0a 20 20 75 2e 63 6c 2e 70 31 20 3d   */..  u.cl.p1 =
1f4de 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 63 6c   pOp->p1;.  u.cl
1f4df 2e 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65  .pPager = sqlite
1f4e0 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e  3BtreePager(db->
1f4e1 61 44 62 5b 75 2e 63 6c 2e 70 31 5d 2e 70 42 74  aDb[u.cl.p1].pBt
1f4e2 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
1f4e3 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
1f4e4 75 2e 63 6c 2e 70 50 61 67 65 72 2c 20 26 75 2e  u.cl.pPager, &u.
1f4e5 63 6c 2e 6e 50 61 67 65 29 3b 0a 20 20 2f 2a 20  cl.nPage);.  /* 
1f4e6 4f 50 5f 50 61 67 65 63 6f 75 6e 74 20 69 73 20  OP_Pagecount is 
1f4e7 61 6c 77 61 79 73 20 63 61 6c 6c 65 64 20 66 72  always called fr
1f4e8 6f 6d 20 77 69 74 68 69 6e 20 61 20 72 65 61 64  om within a read
1f4e9 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54   transaction.  T
1f4ea 68 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 6f 75  he.  ** page cou
1f4eb 6e 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  nt has already b
1f4ec 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79  een successfully
1f4ed 20 72 65 61 64 20 61 6e 64 20 63 61 63 68 65 64   read and cached
1f4ee 2e 20 20 53 6f 20 74 68 65 0a 20 20 2a 2a 20 73  .  So the.  ** s
1f4ef 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
1f4f0 6f 75 6e 74 28 29 20 63 61 6c 6c 20 61 62 6f 76  ount() call abov
1f4f1 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 2a  e cannot fail. *
1f4f2 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 72  /.  if( ALWAYS(r
1f4f3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b  c==SQLITE_OK) ){
1f4f4 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  .    pOut->u.i =
1f4f5 20 75 2e 63 6c 2e 6e 50 61 67 65 3b 0a 20 20 7d   u.cl.nPage;.  }
1f4f6 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1f4f7 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
1f4f8 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a  TE_OMIT_TRACE./*
1f4f9 20 4f 70 63 6f 64 65 3a 20 54 72 61 63 65 20 2a   Opcode: Trace *
1f4fa 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20   * * P4 *.**.** 
1f4fb 49 66 20 74 72 61 63 69 6e 67 20 69 73 20 65 6e  If tracing is en
1f4fc 61 62 6c 65 64 20 28 62 79 20 74 68 65 20 73 71  abled (by the sq
1f4fd 6c 69 74 65 33 5f 74 72 61 63 65 28 29 29 20 69  lite3_trace()) i
1f4fe 6e 74 65 72 66 61 63 65 2c 20 74 68 65 6e 0a 2a  nterface, then.*
1f4ff 2a 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 69  * the UTF-8 stri
1f500 6e 67 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  ng contained in 
1f501 50 34 20 69 73 20 65 6d 69 74 74 65 64 20 6f 6e  P4 is emitted on
1f502 20 74 68 65 20 74 72 61 63 65 20 63 61 6c 6c 62   the trace callb
1f503 61 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ack..*/.case OP_
1f504 54 72 61 63 65 3a 20 7b 0a 23 69 66 20 30 20 20  Trace: {.#if 0  
1f505 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1f506 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1f507 63 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54  cm */.  char *zT
1f508 72 61 63 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  race;.#endif /* 
1f509 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1f50a 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6d 20  moved into u.cm 
1f50b 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 7a 54 72 61 63  */..  u.cm.zTrac
1f50c 65 20 3d 20 28 70 4f 70 2d 3e 70 34 2e 7a 20 3f  e = (pOp->p4.z ?
1f50d 20 70 4f 70 2d 3e 70 34 2e 7a 20 3a 20 70 2d 3e   pOp->p4.z : p->
1f50e 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 75 2e 63  zSql);.  if( u.c
1f50f 6d 2e 7a 54 72 61 63 65 20 29 7b 0a 20 20 20 20  m.zTrace ){.    
1f510 69 66 28 20 64 62 2d 3e 78 54 72 61 63 65 20 29  if( db->xTrace )
1f511 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 20  {.      char *z 
1f512 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 70  = sqlite3VdbeExp
1f513 61 6e 64 53 71 6c 28 70 2c 20 75 2e 63 6d 2e 7a  andSql(p, u.cm.z
1f514 54 72 61 63 65 29 3b 0a 20 20 20 20 20 20 64 62  Trace);.      db
1f515 2d 3e 78 54 72 61 63 65 28 64 62 2d 3e 70 54 72  ->xTrace(db->pTr
1f516 61 63 65 41 72 67 2c 20 7a 29 3b 0a 20 20 20 20  aceArg, z);.    
1f517 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1f518 64 62 2c 20 7a 29 3b 0a 20 20 20 20 7d 0a 23 69  db, z);.    }.#i
1f519 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
1f51a 47 0a 20 20 20 20 69 66 28 20 28 64 62 2d 3e 66  G.    if( (db->f
1f51b 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 53 71  lags & SQLITE_Sq
1f51c 6c 54 72 61 63 65 29 21 3d 30 20 29 7b 0a 20 20  lTrace)!=0 ){.  
1f51d 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
1f51e 50 72 69 6e 74 66 28 22 53 51 4c 2d 74 72 61 63  Printf("SQL-trac
1f51f 65 3a 20 25 73 5c 6e 22 2c 20 75 2e 63 6d 2e 7a  e: %s\n", u.cm.z
1f520 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65  Trace);.    }.#e
1f521 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 44  ndif /* SQLITE_D
1f522 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 20 62 72  EBUG */.  }.  br
1f523 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  eak;.}.#endif...
1f524 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 6f 70 20  /* Opcode: Noop 
1f525 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  * * * * *.**.** 
1f526 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68 69  Do nothing.  Thi
1f527 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73  s instruction is
1f528 20 6f 66 74 65 6e 20 75 73 65 66 75 6c 20 61 73   often useful as
1f529 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 73 74 69   a jump.** desti
1f52a 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a  nation..*/./*.**
1f52b 20 54 68 65 20 6d 61 67 69 63 20 45 78 70 6c 61   The magic Expla
1f52c 69 6e 20 6f 70 63 6f 64 65 20 61 72 65 20 6f 6e  in opcode are on
1f52d 6c 79 20 69 6e 73 65 72 74 65 64 20 77 68 65 6e  ly inserted when
1f52e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28 77 68 69   explain==2 (whi
1f52f 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73 61 79 20  ch.** is to say 
1f530 77 68 65 6e 20 74 68 65 20 45 58 50 4c 41 49 4e  when the EXPLAIN
1f531 20 51 55 45 52 59 20 50 4c 41 4e 20 73 79 6e 74   QUERY PLAN synt
1f532 61 78 20 69 73 20 75 73 65 64 2e 29 0a 2a 2a 20  ax is used.).** 
1f533 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65 63 6f  This opcode reco
1f534 72 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  rds information 
1f535 66 72 6f 6d 20 74 68 65 20 6f 70 74 69 6d 69 7a  from the optimiz
1f536 65 72 2e 20 20 49 74 20 69 73 20 74 68 65 0a 2a  er.  It is the.*
1f537 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 61 20  * the same as a 
1f538 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20 6f 70 63  no-op.  This opc
1f539 6f 64 65 73 6e 65 76 65 72 20 61 70 70 65 61 72  odesnever appear
1f53a 73 20 69 6e 20 61 20 72 65 61 6c 20 56 4d 20 70  s in a real VM p
1f53b 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 66 61 75  rogram..*/.defau
1f53c 6c 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f  lt: {          /
1f53d 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c 6c 79  * This is really
1f53e 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 4f 50 5f   OP_Noop and OP_
1f53f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 62 72 65  Explain */.  bre
1f540 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ak;.}../********
1f541 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f542 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f543 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f544 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f545 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 73  *****.** The cas
1f546 65 73 20 6f 66 20 74 68 65 20 73 77 69 74 63 68  es of the switch
1f547 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65   statement above
1f548 20 74 68 69 73 20 6c 69 6e 65 20 73 68 6f 75 6c   this line shoul
1f549 64 20 61 6c 6c 20 62 65 20 69 6e 64 65 6e 74 65  d all be indente
1f54a 64 0a 2a 2a 20 62 79 20 36 20 73 70 61 63 65 73  d.** by 6 spaces
1f54b 2e 20 20 42 75 74 20 74 68 65 20 6c 65 66 74 2d  .  But the left-
1f54c 6d 6f 73 74 20 36 20 73 70 61 63 65 73 20 68 61  most 6 spaces ha
1f54d 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 20  ve been removed 
1f54e 74 6f 20 69 6d 70 72 6f 76 65 20 74 68 65 0a 2a  to improve the.*
1f54f 2a 20 72 65 61 64 61 62 69 6c 69 74 79 2e 20 20  * readability.  
1f550 46 72 6f 6d 20 74 68 69 73 20 70 6f 69 6e 74 20  From this point 
1f551 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20 6e 6f 72  on down, the nor
1f552 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20  mal indentation 
1f553 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20 72 65 73  rules are.** res
1f554 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tored..*********
1f555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f556 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f557 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f558 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f559 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a 23 69 66  ****/.    }..#if
1f55a 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45  def VDBE_PROFILE
1f55b 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 75 36 34  .    {.      u64
1f55c 20 65 6c 61 70 73 65 64 20 3d 20 73 71 6c 69 74   elapsed = sqlit
1f55d 65 33 48 77 74 69 6d 65 28 29 20 2d 20 73 74 61  e3Hwtime() - sta
1f55e 72 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63  rt;.      pOp->c
1f55f 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70 73 65 64  ycles += elapsed
1f560 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63 6e 74  ;.      pOp->cnt
1f561 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20 20 20 20  ++;.#if 0.      
1f562 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75 74    fprintf(stdout
1f563 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20 65 6c 61  , "%10llu ", ela
1f564 70 73 65 64 29 3b 0a 20 20 20 20 20 20 20 20 73  psed);.        s
1f565 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f  qlite3VdbePrintO
1f566 70 28 73 74 64 6f 75 74 2c 20 6f 72 69 67 50 63  p(stdout, origPc
1f567 2c 20 26 61 4f 70 5b 6f 72 69 67 50 63 5d 29 3b  , &aOp[origPc]);
1f568 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 23 65  .#endif.    }.#e
1f569 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 54 68 65  ndif..    /* The
1f56a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20   following code 
1f56b 61 64 64 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20  adds nothing to 
1f56c 74 68 65 20 61 63 74 75 61 6c 20 66 75 6e 63 74  the actual funct
1f56d 69 6f 6e 61 6c 69 74 79 0a 20 20 20 20 2a 2a 20  ionality.    ** 
1f56e 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20  of the program. 
1f56f 20 49 74 20 69 73 20 6f 6e 6c 79 20 68 65 72 65   It is only here
1f570 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
1f571 20 64 65 62 75 67 67 69 6e 67 2e 0a 20 20 20 20   debugging..    
1f572 2a 2a 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20  ** On the other 
1f573 68 61 6e 64 2c 20 69 74 20 64 6f 65 73 20 62 75  hand, it does bu
1f574 72 6e 20 43 50 55 20 63 79 63 6c 65 73 20 65 76  rn CPU cycles ev
1f575 65 72 79 20 74 69 6d 65 20 74 68 72 6f 75 67 68  ery time through
1f576 0a 20 20 20 20 2a 2a 20 74 68 65 20 65 76 61 6c  .    ** the eval
1f577 75 61 74 6f 72 20 6c 6f 6f 70 2e 20 20 53 6f 20  uator loop.  So 
1f578 77 65 20 63 61 6e 20 6c 65 61 76 65 20 69 74 20  we can leave it 
1f579 6f 75 74 20 77 68 65 6e 20 4e 44 45 42 55 47 20  out when NDEBUG 
1f57a 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20  is defined..    
1f57b 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  */.#ifndef NDEBU
1f57c 47 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 63  G.    assert( pc
1f57d 3e 3d 2d 31 20 26 26 20 70 63 3c 70 2d 3e 6e 4f  >=-1 && pc<p->nO
1f57e 70 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c  p );..#ifdef SQL
1f57f 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 69 66  ITE_DEBUG.    if
1f580 28 20 70 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20  ( p->trace ){.  
1f581 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29 20      if( rc!=0 ) 
1f582 66 70 72 69 6e 74 66 28 70 2d 3e 74 72 61 63 65  fprintf(p->trace
1f583 2c 22 72 63 3d 25 64 5c 6e 22 2c 72 63 29 3b 0a  ,"rc=%d\n",rc);.
1f584 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f        if( pOp->o
1f585 70 66 6c 61 67 73 20 26 20 28 4f 50 46 4c 47 5f  pflags & (OPFLG_
1f586 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 7c  OUT2_PRERELEASE|
1f587 4f 50 46 4c 47 5f 4f 55 54 32 29 20 29 7b 0a 20  OPFLG_OUT2) ){. 
1f588 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 54         registerT
1f589 72 61 63 65 28 70 2d 3e 74 72 61 63 65 2c 20 70  race(p->trace, p
1f58a 4f 70 2d 3e 70 32 2c 20 26 61 4d 65 6d 5b 70 4f  Op->p2, &aMem[pO
1f58b 70 2d 3e 70 32 5d 29 3b 0a 20 20 20 20 20 20 7d  p->p2]);.      }
1f58c 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  .      if( pOp->
1f58d 6f 70 66 6c 61 67 73 20 26 20 4f 50 46 4c 47 5f  opflags & OPFLG_
1f58e 4f 55 54 33 20 29 7b 0a 20 20 20 20 20 20 20 20  OUT3 ){.        
1f58f 72 65 67 69 73 74 65 72 54 72 61 63 65 28 70 2d  registerTrace(p-
1f590 3e 74 72 61 63 65 2c 20 70 4f 70 2d 3e 70 33 2c  >trace, pOp->p3,
1f591 20 26 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 29   &aMem[pOp->p3])
1f592 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1f593 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
1f594 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 69  E_DEBUG */.#endi
1f595 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  f  /* NDEBUG */.
1f596 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 20    }  /* The end 
1f597 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 6c  of the for(;;) l
1f598 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 68  oop the loops th
1f599 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a 2f  rough opcodes */
1f59a 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61  ..  /* If we rea
1f59b 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69  ch this point, i
1f59c 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78 65  t means that exe
1f59d 63 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68  cution is finish
1f59e 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e 20  ed with.  ** an 
1f59f 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b 69  error of some ki
1f5a0 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65 72  nd..  */.vdbe_er
1f5a1 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73 65  ror_halt:.  asse
1f5a2 72 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e 72  rt( rc );.  p->r
1f5a3 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  c = rc;.  sqlite
1f5a4 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20  3VdbeHalt(p);.  
1f5a5 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49  if( rc==SQLITE_I
1f5a6 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d  OERR_NOMEM ) db-
1f5a7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
1f5a8 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  1;.  rc = SQLITE
1f5a9 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 65  _ERROR;.  if( re
1f5aa 73 65 74 53 63 68 65 6d 61 4f 6e 46 61 75 6c 74  setSchemaOnFault
1f5ab 20 29 20 73 71 6c 69 74 65 33 52 65 73 65 74 49   ) sqlite3ResetI
1f5ac 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62  nternalSchema(db
1f5ad 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  , 0);..  /* This
1f5ae 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77 61 79   is the only way
1f5af 20 6f 75 74 20 6f 66 20 74 68 69 73 20 70 72 6f   out of this pro
1f5b0 63 65 64 75 72 65 2e 20 20 57 65 20 68 61 76 65  cedure.  We have
1f5b1 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 61 73 65   to.  ** release
1f5b2 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f 6e 20   the mutexes on 
1f5b3 62 74 72 65 65 73 20 74 68 61 74 20 77 65 72 65  btrees that were
1f5b4 20 61 63 71 75 69 72 65 64 20 61 74 20 74 68 65   acquired at the
1f5b5 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f 0a 76 64  .  ** top. */.vd
1f5b6 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20 73 71 6c  be_return:.  sql
1f5b7 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
1f5b8 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61 4d 75  rayLeave(&p->aMu
1f5b9 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
1f5ba 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  c;..  /* Jump to
1f5bb 20 68 65 72 65 20 69 66 20 61 20 73 74 72 69 6e   here if a strin
1f5bc 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72 67 65 72  g or blob larger
1f5bd 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d 41 58   than SQLITE_MAX
1f5be 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 69 73 20  _LENGTH.  ** is 
1f5bf 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 20 20 2a  encountered..  *
1f5c0 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 73 71 6c  /.too_big:.  sql
1f5c1 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1f5c2 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
1f5c3 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 74  string or blob t
1f5c4 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72 63 20 3d  oo big");.  rc =
1f5c5 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a   SQLITE_TOOBIG;.
1f5c6 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f    goto vdbe_erro
1f5c7 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75  r_halt;..  /* Ju
1f5c8 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 61 20  mp to here if a 
1f5c9 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a  malloc() fails..
1f5ca 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a 20 20 64    */.no_mem:.  d
1f5cb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1f5cc 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  = 1;.  sqlite3Se
1f5cd 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72  tString(&p->zErr
1f5ce 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74 20 6f 66  Msg, db, "out of
1f5cf 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 72 63 20   memory");.  rc 
1f5d0 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1f5d1 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f    goto vdbe_erro
1f5d2 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a 75  r_halt;..  /* Ju
1f5d3 6d 70 20 74 6f 20 68 65 72 65 20 66 6f 72 20 61  mp to here for a
1f5d4 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20  n SQLITE_MISUSE 
1f5d5 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 62 6f 72  error..  */.abor
1f5d6 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3a  t_due_to_misuse:
1f5d7 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d  .  rc = SQLITE_M
1f5d8 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 61 6c 6c  ISUSE;.  /* Fall
1f5d9 20 74 68 72 75 20 69 6e 74 6f 20 61 62 6f 72 74   thru into abort
1f5da 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 20 2a 2f  _due_to_error */
1f5db 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 68  ..  /* Jump to h
1f5dc 65 72 65 20 66 6f 72 20 61 6e 79 20 6f 74 68 65  ere for any othe
1f5dd 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 61 6c 20  r kind of fatal 
1f5de 65 72 72 6f 72 2e 20 20 54 68 65 20 22 72 63 22  error.  The "rc"
1f5df 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20 73   variable.  ** s
1f5e0 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65  hould hold the e
1f5e1 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20 20 2a  rror number..  *
1f5e2 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  /.abort_due_to_e
1f5e3 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74 28 20  rror:.  assert( 
1f5e4 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 29 3b  p->zErrMsg==0 );
1f5e5 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
1f5e6 63 46 61 69 6c 65 64 20 29 20 72 63 20 3d 20 53  cFailed ) rc = S
1f5e7 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 69  QLITE_NOMEM;.  i
1f5e8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f  f( rc!=SQLITE_IO
1f5e9 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ERR_NOMEM ){.   
1f5ea 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1f5eb 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1f5ec 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33  b, "%s", sqlite3
1f5ed 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 7d  ErrStr(rc));.  }
1f5ee 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72  .  goto vdbe_err
1f5ef 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20 4a  or_halt;..  /* J
1f5f0 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20 74  ump to here if t
1f5f1 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  he sqlite3_inter
1f5f2 72 75 70 74 28 29 20 41 50 49 20 73 65 74 73 20  rupt() API sets 
1f5f3 74 68 65 20 69 6e 74 65 72 72 75 70 74 0a 20 20  the interrupt.  
1f5f4 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a 61 62  ** flag..  */.ab
1f5f5 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65 72  ort_due_to_inter
1f5f6 72 75 70 74 3a 0a 20 20 61 73 73 65 72 74 28 20  rupt:.  assert( 
1f5f7 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75  db->u1.isInterru
1f5f8 70 74 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 53  pted );.  rc = S
1f5f9 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b  QLITE_INTERRUPT;
1f5fa 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20  .  p->rc = rc;. 
1f5fb 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1f5fc 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1f5fd 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33  b, "%s", sqlite3
1f5fe 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 67  ErrStr(rc));.  g
1f5ff 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68  oto vdbe_error_h
1f600 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  alt;.}../*******
1f601 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76  ******* End of v
1f602 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dbe.c **********
1f603 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f604 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f605 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1f606 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
1f607 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a  le vdbeblob.c **
1f608 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f60a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
1f60b 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2a 20 54  07 May 1.**.** T
1f60c 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
1f60d 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
1f60e 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
1f60f 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
1f610 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
1f611 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
1f612 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
1f613 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
1f614 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
1f615 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
1f616 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
1f617 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
1f618 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
1f619 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
1f61a 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
1f61b 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
1f61c 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
1f61d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f61e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f61f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f621 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
1f622 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
1f623 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d   code used to im
1f624 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 6d 65 6e  plement incremen
1f625 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e 0a 2a 2f  tal BLOB I/O..*/
1f626 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
1f627 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
1f628 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 73 71 6c  ./*.** Valid sql
1f629 69 74 65 33 5f 62 6c 6f 62 2a 20 68 61 6e 64 6c  ite3_blob* handl
1f62a 65 73 20 70 6f 69 6e 74 20 74 6f 20 49 6e 63 72  es point to Incr
1f62b 62 6c 6f 62 20 73 74 72 75 63 74 75 72 65 73 2e  blob structures.
1f62c 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
1f62d 63 74 20 49 6e 63 72 62 6c 6f 62 20 49 6e 63 72  ct Incrblob Incr
1f62e 62 6c 6f 62 3b 0a 73 74 72 75 63 74 20 49 6e 63  blob;.struct Inc
1f62f 72 62 6c 6f 62 20 7b 0a 20 20 69 6e 74 20 66 6c  rblob {.  int fl
1f630 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
1f631 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 22 66 6c    /* Copy of "fl
1f632 61 67 73 22 20 70 61 73 73 65 64 20 74 6f 20 73  ags" passed to s
1f633 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
1f634 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  () */.  int nByt
1f635 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1f636 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 70 65 6e 20  /* Size of open 
1f637 62 6c 6f 62 2c 20 69 6e 20 62 79 74 65 73 20 2a  blob, in bytes *
1f638 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b  /.  int iOffset;
1f639 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
1f63a 79 74 65 20 6f 66 66 73 65 74 20 6f 66 20 62 6c  yte offset of bl
1f63b 6f 62 20 69 6e 20 63 75 72 73 6f 72 20 64 61 74  ob in cursor dat
1f63c 61 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20  a */.  BtCursor 
1f63d 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20 2f  *pCsr;         /
1f63e 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e  * Cursor pointin
1f63f 67 20 61 74 20 62 6c 6f 62 20 72 6f 77 20 2a 2f  g at blob row */
1f640 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
1f641 2a 70 53 74 6d 74 3b 20 20 20 20 2f 2a 20 53 74  *pStmt;    /* St
1f642 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e 67 20  atement holding 
1f643 63 75 72 73 6f 72 20 6f 70 65 6e 20 2a 2f 0a 20  cursor open */. 
1f644 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
1f645 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1f646 61 73 73 6f 63 69 61 74 65 64 20 64 61 74 61 62  associated datab
1f647 61 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ase */.};../*.**
1f648 20 4f 70 65 6e 20 61 20 62 6c 6f 62 20 68 61 6e   Open a blob han
1f649 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  dle..*/.SQLITE_A
1f64a 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
1f64b 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69  lob_open(.  sqli
1f64c 74 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20  te3* db,        
1f64d 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
1f64e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
1f64f 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1f650 7a 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  zDb,        /* T
1f651 68 65 20 61 74 74 61 63 68 65 64 20 64 61 74 61  he attached data
1f652 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
1f653 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f  the blob */.  co
1f654 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
1f655 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  ,     /* The tab
1f656 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  le containing th
1f657 65 20 62 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73  e blob */.  cons
1f658 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 2c  t char *zColumn,
1f659 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d      /* The colum
1f65a 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  n containing the
1f65b 20 62 6c 6f 62 20 2a 2f 0a 20 20 73 71 6c 69 74   blob */.  sqlit
1f65c 65 5f 69 6e 74 36 34 20 69 52 6f 77 2c 20 20 20  e_int64 iRow,   
1f65d 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 63 6f     /* The row co
1f65e 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 67 6c 6f  ntaining the glo
1f65f 62 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  b */.  int flags
1f660 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
1f661 2a 20 54 72 75 65 20 2d 3e 20 72 65 61 64 2f 77  * True -> read/w
1f662 72 69 74 65 20 61 63 63 65 73 73 2c 20 66 61 6c  rite access, fal
1f663 73 65 20 2d 3e 20 72 65 61 64 2d 6f 6e 6c 79 20  se -> read-only 
1f664 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  */.  sqlite3_blo
1f665 62 20 2a 2a 70 70 42 6c 6f 62 20 20 20 2f 2a 20  b **ppBlob   /* 
1f666 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63 65 73  Handle for acces
1f667 73 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 72 65  sing the blob re
1f668 74 75 72 6e 65 64 20 68 65 72 65 20 2a 2f 0a 29  turned here */.)
1f669 7b 0a 20 20 69 6e 74 20 6e 41 74 74 65 6d 70 74  {.  int nAttempt
1f66a 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c   = 0;.  int iCol
1f66b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1f66c 2f 2a 20 49 6e 64 65 78 20 6f 66 20 7a 43 6f 6c  /* Index of zCol
1f66d 75 6d 6e 20 69 6e 20 72 6f 77 2d 72 65 63 6f 72  umn in row-recor
1f66e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20  d */..  /* This 
1f66f 56 44 42 45 20 70 72 6f 67 72 61 6d 20 73 65 65  VDBE program see
1f670 6b 73 20 61 20 62 74 72 65 65 20 63 75 72 73 6f  ks a btree curso
1f671 72 20 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66  r to the identif
1f672 69 65 64 20 0a 20 20 2a 2a 20 64 62 2f 74 61 62  ied .  ** db/tab
1f673 6c 65 2f 72 6f 77 20 65 6e 74 72 79 2e 20 54 68  le/row entry. Th
1f674 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 75 73 69  e reason for usi
1f675 6e 67 20 61 20 76 64 62 65 20 70 72 6f 67 72 61  ng a vdbe progra
1f676 6d 20 69 6e 73 74 65 61 64 0a 20 20 2a 2a 20 6f  m instead.  ** o
1f677 66 20 77 72 69 74 69 6e 67 20 63 6f 64 65 20 74  f writing code t
1f678 6f 20 75 73 65 20 74 68 65 20 62 2d 74 72 65 65  o use the b-tree
1f679 20 6c 61 79 65 72 20 64 69 72 65 63 74 6c 79 20   layer directly 
1f67a 69 73 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a  is that the.  **
1f67b 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 77 69   vdbe program wi
1f67c 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67  ll take advantag
1f67d 65 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73  e of the various
1f67e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20 20   transaction,.  
1f67f 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20 65  ** locking and e
1f680 72 72 6f 72 20 68 61 6e 64 6c 69 6e 67 20 69 6e  rror handling in
1f681 66 72 61 73 74 72 75 63 74 75 72 65 20 62 75 69  frastructure bui
1f682 6c 74 20 69 6e 74 6f 20 74 68 65 20 76 64 62 65  lt into the vdbe
1f683 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 66 74 65  ..  **.  ** Afte
1f684 72 20 73 65 65 6b 69 6e 67 20 74 68 65 20 63 75  r seeking the cu
1f685 72 73 6f 72 2c 20 74 68 65 20 76 64 62 65 20 65  rsor, the vdbe e
1f686 78 65 63 75 74 65 73 20 61 6e 20 4f 50 5f 52 65  xecutes an OP_Re
1f687 73 75 6c 74 52 6f 77 2e 0a 20 20 2a 2a 20 43 6f  sultRow..  ** Co
1f688 64 65 20 65 78 74 65 72 6e 61 6c 20 74 6f 20 74  de external to t
1f689 68 65 20 56 64 62 65 20 74 68 65 6e 20 22 62 6f  he Vdbe then "bo
1f68a 72 72 6f 77 73 22 20 74 68 65 20 62 2d 74 72 65  rrows" the b-tre
1f68b 65 20 63 75 72 73 6f 72 20 61 6e 64 0a 20 20 2a  e cursor and.  *
1f68c 2a 20 75 73 65 73 20 69 74 20 74 6f 20 69 6d 70  * uses it to imp
1f68d 6c 65 6d 65 6e 74 20 74 68 65 20 62 6c 6f 62 5f  lement the blob_
1f68e 72 65 61 64 28 29 2c 20 62 6c 6f 62 5f 77 72 69  read(), blob_wri
1f68f 74 65 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 62  te() and .  ** b
1f690 6c 6f 62 5f 62 79 74 65 73 28 29 20 66 75 6e 63  lob_bytes() func
1f691 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
1f692 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f   The sqlite3_blo
1f693 62 5f 63 6c 6f 73 65 28 29 20 66 75 6e 63 74 69  b_close() functi
1f694 6f 6e 20 66 69 6e 61 6c 69 7a 65 73 20 74 68 65  on finalizes the
1f695 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2c 0a 20   vdbe program,. 
1f696 20 2a 2a 20 77 68 69 63 68 20 63 6c 6f 73 65 73   ** which closes
1f697 20 74 68 65 20 62 2d 74 72 65 65 20 63 75 72 73   the b-tree curs
1f698 6f 72 20 61 6e 64 20 28 70 6f 73 73 69 62 6c 79  or and (possibly
1f699 29 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 0a 20  ) commits the . 
1f69a 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e   ** transaction.
1f69b 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63  .  */.  static c
1f69c 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
1f69d 6f 70 65 6e 42 6c 6f 62 5b 5d 20 3d 20 7b 0a 20  openBlob[] = {. 
1f69e 20 20 20 7b 4f 50 5f 54 72 61 6e 73 61 63 74 69     {OP_Transacti
1f69f 6f 6e 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20  on, 0, 0, 0},   
1f6a0 20 20 2f 2a 20 30 3a 20 53 74 61 72 74 20 61 20    /* 0: Start a 
1f6a1 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  transaction */. 
1f6a2 20 20 20 7b 4f 50 5f 56 65 72 69 66 79 43 6f 6f     {OP_VerifyCoo
1f6a3 6b 69 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20  kie, 0, 0, 0},  
1f6a4 20 20 2f 2a 20 31 3a 20 43 68 65 63 6b 20 74 68    /* 1: Check th
1f6a5 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  e schema cookie 
1f6a6 2a 2f 0a 20 20 20 20 7b 4f 50 5f 54 61 62 6c 65  */.    {OP_Table
1f6a7 4c 6f 63 6b 2c 20 30 2c 20 30 2c 20 30 7d 2c 20  Lock, 0, 0, 0}, 
1f6a8 20 20 20 20 20 20 2f 2a 20 32 3a 20 41 63 71 75        /* 2: Acqu
1f6a9 69 72 65 20 61 20 72 65 61 64 20 6f 72 20 77 72  ire a read or wr
1f6aa 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20 20  ite lock */..   
1f6ab 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 66   /* One of the f
1f6ac 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73  ollowing two ins
1f6ad 74 72 75 63 74 69 6f 6e 73 20 69 73 20 72 65 70  tructions is rep
1f6ae 6c 61 63 65 64 20 62 79 20 61 6e 20 4f 50 5f 4e  laced by an OP_N
1f6af 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  oop. */.    {OP_
1f6b0 4f 70 65 6e 52 65 61 64 2c 20 30 2c 20 30 2c 20  OpenRead, 0, 0, 
1f6b1 30 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 33 3a  0},        /* 3:
1f6b2 20 4f 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66   Open cursor 0 f
1f6b3 6f 72 20 72 65 61 64 69 6e 67 20 2a 2f 0a 20 20  or reading */.  
1f6b4 20 20 7b 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c    {OP_OpenWrite,
1f6b5 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20   0, 0, 0},      
1f6b6 20 2f 2a 20 34 3a 20 4f 70 65 6e 20 63 75 72 73   /* 4: Open curs
1f6b7 6f 72 20 30 20 66 6f 72 20 72 65 61 64 2f 77 72  or 0 for read/wr
1f6b8 69 74 65 20 2a 2f 0a 0a 20 20 20 20 7b 4f 50 5f  ite */..    {OP_
1f6b9 56 61 72 69 61 62 6c 65 2c 20 31 2c 20 31 2c 20  Variable, 1, 1, 
1f6ba 31 7d 2c 20 20 20 20 20 20 20 20 2f 2a 20 35 3a  1},        /* 5:
1f6bb 20 50 75 73 68 20 74 68 65 20 72 6f 77 69 64 20   Push the rowid 
1f6bc 74 6f 20 74 68 65 20 73 74 61 63 6b 20 2a 2f 0a  to the stack */.
1f6bd 20 20 20 20 7b 4f 50 5f 4e 6f 74 45 78 69 73 74      {OP_NotExist
1f6be 73 2c 20 30 2c 20 39 2c 20 31 7d 2c 20 20 20 20  s, 0, 9, 1},    
1f6bf 20 20 20 2f 2a 20 36 3a 20 53 65 65 6b 20 74 68     /* 6: Seek th
1f6c0 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20  e cursor */.    
1f6c1 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30  {OP_Column, 0, 0
1f6c2 2c 20 31 7d 2c 20 20 20 20 20 20 20 20 20 20 2f  , 1},          /
1f6c3 2a 20 37 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  * 7  */.    {OP_
1f6c4 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 30 2c  ResultRow, 1, 0,
1f6c5 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 38 20   0},       /* 8 
1f6c6 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6c 6f 73   */.    {OP_Clos
1f6c7 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20  e, 0, 0, 0},    
1f6c8 20 20 20 20 20 20 20 2f 2a 20 39 20 20 2a 2f 0a         /* 9  */.
1f6c9 20 20 20 20 7b 4f 50 5f 48 61 6c 74 2c 20 30 2c      {OP_Halt, 0,
1f6ca 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20 20   0, 0},         
1f6cb 20 20 20 2f 2a 20 31 30 20 2a 2f 0a 20 20 7d 3b     /* 10 */.  };
1f6cc 0a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b  ..  Vdbe *v = 0;
1f6cd 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1f6ce 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a 7a  TE_OK;.  char *z
1f6cf 45 72 72 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65  Err = 0;.  Table
1f6d0 20 2a 70 54 61 62 3b 0a 20 20 50 61 72 73 65 20   *pTab;.  Parse 
1f6d1 2a 70 50 61 72 73 65 3b 0a 0a 20 20 2a 70 70 42  *pParse;..  *ppB
1f6d2 6c 6f 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  lob = 0;.  sqlit
1f6d3 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
1f6d4 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 50 61  b->mutex);.  pPa
1f6d5 72 73 65 20 3d 20 73 71 6c 69 74 65 33 53 74 61  rse = sqlite3Sta
1f6d6 63 6b 41 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  ckAllocRaw(db, s
1f6d7 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29 3b  izeof(*pParse));
1f6d8 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30  .  if( pParse==0
1f6d9 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
1f6da 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
1f6db 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75  oto blob_open_ou
1f6dc 74 3b 0a 20 20 7d 0a 20 20 64 6f 20 7b 0a 20 20  t;.  }.  do {.  
1f6dd 20 20 6d 65 6d 73 65 74 28 70 50 61 72 73 65 2c    memset(pParse,
1f6de 20 30 2c 20 73 69 7a 65 6f 66 28 50 61 72 73 65   0, sizeof(Parse
1f6df 29 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  ));.    pParse->
1f6e0 64 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 69 66  db = db;..    if
1f6e1 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1f6e2 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20 73  n(db) ){.      s
1f6e3 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f6e4 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73 71   zErr);.      sq
1f6e5 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64  lite3StackFree(d
1f6e6 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20  b, pParse);.    
1f6e7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1f6e8 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
1f6e9 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
1f6ea 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
1f6eb 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
1f6ec 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62  BtreeEnterAll(db
1f6ed 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71  );.    pTab = sq
1f6ee 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
1f6ef 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 54 61 62  (pParse, 0, zTab
1f6f0 6c 65 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66  le, zDb);.    if
1f6f1 28 20 70 54 61 62 20 26 26 20 49 73 56 69 72 74  ( pTab && IsVirt
1f6f2 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
1f6f3 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20     pTab = 0;.   
1f6f4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1f6f5 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
1f6f6 6f 74 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20  ot open virtual 
1f6f7 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62  table: %s", zTab
1f6f8 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  le);.    }.#ifnd
1f6f9 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1f6fa 49 45 57 0a 20 20 20 20 69 66 28 20 70 54 61 62  IEW.    if( pTab
1f6fb 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   && pTab->pSelec
1f6fc 74 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20  t ){.      pTab 
1f6fd 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
1f6fe 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1f6ff 65 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20  e, "cannot open 
1f700 76 69 65 77 3a 20 25 73 22 2c 20 7a 54 61 62 6c  view: %s", zTabl
1f701 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  e);.    }.#endif
1f702 0a 20 20 20 20 69 66 28 20 21 70 54 61 62 20 29  .    if( !pTab )
1f703 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72  {.      if( pPar
1f704 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20  se->zErrMsg ){. 
1f705 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
1f706 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a  Free(db, zErr);.
1f707 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 70          zErr = p
1f708 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a  Parse->zErrMsg;.
1f709 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
1f70a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
1f70b 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
1f70c 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1f70d 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
1f70e 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
1f70f 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1f710 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
1f711 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f        goto blob_
1f712 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  open_out;.    }.
1f713 0a 20 20 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72  .    /* Now sear
1f714 63 68 20 70 54 61 62 20 66 6f 72 20 74 68 65 20  ch pTab for the 
1f715 65 78 61 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f  exact column. */
1f716 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b  .    for(iCol=0;
1f717 20 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43   iCol < pTab->nC
1f718 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20  ol; iCol++) {.  
1f719 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1f71a 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f  trICmp(pTab->aCo
1f71b 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a  l[iCol].zName, z
1f71c 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20  Column)==0 ){.  
1f71d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1f71e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1f71f 66 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e  f( iCol==pTab->n
1f720 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Col ){.      sql
1f721 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
1f722 45 72 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72  Err);.      zErr
1f723 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
1f724 66 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 63  f(db, "no such c
1f725 6f 6c 75 6d 6e 3a 20 5c 22 25 73 5c 22 22 2c 20  olumn: \"%s\"", 
1f726 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20  zColumn);.      
1f727 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1f728 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  R;.      (void)s
1f729 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1f72a 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  db);.      sqlit
1f72b 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28  e3BtreeLeaveAll(
1f72c 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  db);.      goto 
1f72d 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  blob_open_out;. 
1f72e 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
1f72f 74 68 65 20 76 61 6c 75 65 20 69 73 20 62 65 69  the value is bei
1f730 6e 67 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72  ng opened for wr
1f731 69 74 69 6e 67 2c 20 63 68 65 63 6b 20 74 68 61  iting, check tha
1f732 74 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6c  t the.    ** col
1f733 75 6d 6e 20 69 73 20 6e 6f 74 20 69 6e 64 65 78  umn is not index
1f734 65 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20  ed, and that it 
1f735 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61  is not part of a
1f736 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 0a 20   foreign key. . 
1f737 20 20 20 2a 2a 20 49 74 20 69 73 20 61 67 61 69     ** It is agai
1f738 6e 73 74 20 74 68 65 20 72 75 6c 65 73 20 74 6f  nst the rules to
1f739 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74   open a column t
1f73a 6f 20 77 68 69 63 68 20 65 69 74 68 65 72 20 6f  o which either o
1f73b 66 20 74 68 65 73 65 0a 20 20 20 20 2a 2a 20 64  f these.    ** d
1f73c 65 73 63 72 69 70 74 69 6f 6e 73 20 61 70 70 6c  escriptions appl
1f73d 69 65 73 20 66 6f 72 20 77 72 69 74 69 6e 67 2e  ies for writing.
1f73e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 66 6c 61    */.    if( fla
1f73f 67 73 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73  gs ){.      cons
1f740 74 20 63 68 61 72 20 2a 7a 46 61 75 6c 74 20 3d  t char *zFault =
1f741 20 30 3b 0a 20 20 20 20 20 20 49 6e 64 65 78 20   0;.      Index 
1f742 2a 70 49 64 78 3b 0a 23 69 66 6e 64 65 66 20 53  *pIdx;.#ifndef S
1f743 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
1f744 47 4e 5f 4b 45 59 0a 20 20 20 20 20 20 69 66 28  GN_KEY.      if(
1f745 20 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54   db->flags&SQLIT
1f746 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b  E_ForeignKeys ){
1f747 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63  .        /* Chec
1f748 6b 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d  k that the colum
1f749 6e 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  n is not part of
1f74a 20 61 6e 20 46 4b 20 63 68 69 6c 64 20 6b 65 79   an FK child key
1f74b 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 49 74 0a   definition. It.
1f74c 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f          ** is no
1f74d 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 63  t necessary to c
1f74e 68 65 63 6b 20 69 66 20 69 74 20 69 73 20 70 61  heck if it is pa
1f74f 72 74 20 6f 66 20 61 20 70 61 72 65 6e 74 20 6b  rt of a parent k
1f750 65 79 2c 20 61 73 20 70 61 72 65 6e 74 0a 20 20  ey, as parent.  
1f751 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 63 6f 6c        ** key col
1f752 75 6d 6e 73 20 6d 75 73 74 20 62 65 20 69 6e 64  umns must be ind
1f753 65 78 65 64 2e 20 54 68 65 20 63 68 65 63 6b 20  exed. The check 
1f754 62 65 6c 6f 77 20 77 69 6c 6c 20 70 69 63 6b 20  below will pick 
1f755 75 70 20 74 68 69 73 20 0a 20 20 20 20 20 20 20  up this .       
1f756 20 2a 2a 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20   ** case.  */.  
1f757 20 20 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 65        FKey *pFKe
1f758 79 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70  y;.        for(p
1f759 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b 65 79  FKey=pTab->pFKey
1f75a 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70  ; pFKey; pFKey=p
1f75b 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29  FKey->pNextFrom)
1f75c 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
1f75d 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  j;.          for
1f75e 28 6a 3d 30 3b 20 6a 3c 70 46 4b 65 79 2d 3e 6e  (j=0; j<pFKey->n
1f75f 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Col; j++){.     
1f760 20 20 20 20 20 20 20 69 66 28 20 70 46 4b 65 79         if( pFKey
1f761 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 3d  ->aCol[j].iFrom=
1f762 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20  =iCol ){.       
1f763 20 20 20 20 20 20 20 7a 46 61 75 6c 74 20 3d 20         zFault = 
1f764 22 66 6f 72 65 69 67 6e 20 6b 65 79 22 3b 0a 20  "foreign key";. 
1f765 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1f766 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1f767 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69   }.      }.#endi
1f768 66 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78  f.      for(pIdx
1f769 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
1f76a 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
1f76b 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
1f76c 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66  int j;.        f
1f76d 6f 72 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e  or(j=0; j<pIdx->
1f76e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20  nColumn; j++){. 
1f76f 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64           if( pId
1f770 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d  x->aiColumn[j]==
1f771 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20  iCol ){.        
1f772 20 20 20 20 7a 46 61 75 6c 74 20 3d 20 22 69 6e      zFault = "in
1f773 64 65 78 65 64 22 3b 0a 20 20 20 20 20 20 20 20  dexed";.        
1f774 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1f775 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1f776 7a 46 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  zFault ){.      
1f777 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1f778 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20  db, zErr);.     
1f779 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65     zErr = sqlite
1f77a 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 63 61  3MPrintf(db, "ca
1f77b 6e 6e 6f 74 20 6f 70 65 6e 20 25 73 20 63 6f 6c  nnot open %s col
1f77c 75 6d 6e 20 66 6f 72 20 77 72 69 74 69 6e 67 22  umn for writing"
1f77d 2c 20 7a 46 61 75 6c 74 29 3b 0a 20 20 20 20 20  , zFault);.     
1f77e 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
1f77f 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 28 76  RROR;.        (v
1f780 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
1f781 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20  yOff(db);.      
1f782 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1f783 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  aveAll(db);.    
1f784 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70      goto blob_op
1f785 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  en_out;.      }.
1f786 20 20 20 20 7d 0a 0a 20 20 20 20 76 20 3d 20 73      }..    v = s
1f787 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65  qlite3VdbeCreate
1f788 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 76 20  (db);.    if( v 
1f789 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  ){.      int iDb
1f78a 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
1f78b 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
1f78c 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20  ->pSchema);.    
1f78d 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1f78e 4f 70 4c 69 73 74 28 76 2c 20 73 69 7a 65 6f 66  OpList(v, sizeof
1f78f 28 6f 70 65 6e 42 6c 6f 62 29 2f 73 69 7a 65 6f  (openBlob)/sizeo
1f790 66 28 56 64 62 65 4f 70 4c 69 73 74 29 2c 20 6f  f(VdbeOpList), o
1f791 70 65 6e 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20  penBlob);.      
1f792 66 6c 61 67 73 20 3d 20 21 21 66 6c 61 67 73 3b  flags = !!flags;
1f793 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f794 20 2f 2a 20 66 6c 61 67 73 20 3d 20 28 66 6c 61   /* flags = (fla
1f795 67 73 20 3f 20 31 20 3a 20 30 29 3b 20 2a 2f 0a  gs ? 1 : 0); */.
1f796 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67  .      /* Config
1f797 75 72 65 20 74 68 65 20 4f 50 5f 54 72 61 6e 73  ure the OP_Trans
1f798 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20  action */.      
1f799 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f79a 65 50 31 28 76 2c 20 30 2c 20 69 44 62 29 3b 0a  eP1(v, 0, iDb);.
1f79b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f79c 65 43 68 61 6e 67 65 50 32 28 76 2c 20 30 2c 20  eChangeP2(v, 0, 
1f79d 66 6c 61 67 73 29 3b 0a 0a 20 20 20 20 20 20 2f  flags);..      /
1f79e 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20  * Configure the 
1f79f 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20  OP_VerifyCookie 
1f7a0 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
1f7a1 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
1f7a2 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  1, iDb);.      s
1f7a3 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1f7a4 50 32 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70  P2(v, 1, pTab->p
1f7a5 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
1f7a6 6f 6f 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f  ookie);..      /
1f7a7 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75  * Make sure a mu
1f7a8 74 65 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74  tex is held on t
1f7a9 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61  he table to be a
1f7aa 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20  ccessed */.     
1f7ab 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
1f7ac 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a  Btree(v, iDb); .
1f7ad 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67  .      /* Config
1f7ae 75 72 65 20 74 68 65 20 4f 50 5f 54 61 62 6c 65  ure the OP_Table
1f7af 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74 69 6f 6e  Lock instruction
1f7b0 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
1f7b1 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
1f7b2 20 32 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20   2, iDb);.      
1f7b3 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f7b4 65 50 32 28 76 2c 20 32 2c 20 70 54 61 62 2d 3e  eP2(v, 2, pTab->
1f7b5 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c  tnum);.      sql
1f7b6 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
1f7b7 28 76 2c 20 32 2c 20 66 6c 61 67 73 29 3b 0a 20  (v, 2, flags);. 
1f7b8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f7b9 43 68 61 6e 67 65 50 34 28 76 2c 20 32 2c 20 70  ChangeP4(v, 2, p
1f7ba 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 54  Tab->zName, P4_T
1f7bb 52 41 4e 53 49 45 4e 54 29 3b 0a 0a 20 20 20 20  RANSIENT);..    
1f7bc 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65 69 74 68    /* Remove eith
1f7bd 65 72 20 74 68 65 20 4f 50 5f 4f 70 65 6e 57 72  er the OP_OpenWr
1f7be 69 74 65 20 6f 72 20 4f 70 65 6e 52 65 61 64 2e  ite or OpenRead.
1f7bf 20 53 65 74 20 74 68 65 20 50 32 20 0a 20 20 20   Set the P2 .   
1f7c0 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72 20     ** parameter 
1f7c1 6f 66 20 74 68 65 20 6f 74 68 65 72 20 74 6f 20  of the other to 
1f7c2 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 20 2a 2f 0a  pTab->tnum.  */.
1f7c3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f7c4 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c  eChangeToNoop(v,
1f7c5 20 34 20 2d 20 66 6c 61 67 73 2c 20 31 29 3b 0a   4 - flags, 1);.
1f7c6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f7c7 65 43 68 61 6e 67 65 50 32 28 76 2c 20 33 20 2b  eChangeP2(v, 3 +
1f7c8 20 66 6c 61 67 73 2c 20 70 54 61 62 2d 3e 74 6e   flags, pTab->tn
1f7c9 75 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  um);.      sqlit
1f7ca 65 33 56 64 62 65 43 68 61 6e 67 65 50 33 28 76  e3VdbeChangeP3(v
1f7cb 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 69 44 62  , 3 + flags, iDb
1f7cc 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 6e  );..      /* Con
1f7cd 66 69 67 75 72 65 20 74 68 65 20 6e 75 6d 62 65  figure the numbe
1f7ce 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20 43 6f  r of columns. Co
1f7cf 6e 66 69 67 75 72 65 20 74 68 65 20 63 75 72 73  nfigure the curs
1f7d0 6f 72 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74  or to.      ** t
1f7d1 68 69 6e 6b 20 74 68 61 74 20 74 68 65 20 74 61  hink that the ta
1f7d2 62 6c 65 20 68 61 73 20 6f 6e 65 20 6d 6f 72 65  ble has one more
1f7d3 20 63 6f 6c 75 6d 6e 20 74 68 61 6e 20 69 74 20   column than it 
1f7d4 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 2a 2a 20  really.      ** 
1f7d5 64 6f 65 73 2e 20 41 6e 20 4f 50 5f 43 6f 6c 75  does. An OP_Colu
1f7d6 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74  mn to retrieve t
1f7d7 68 69 73 20 69 6d 61 67 69 6e 61 72 79 20 63 6f  his imaginary co
1f7d8 6c 75 6d 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20  lumn will.      
1f7d9 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  ** always return
1f7da 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 54 68   an SQL NULL. Th
1f7db 69 73 20 69 73 20 75 73 65 66 75 6c 20 62 65 63  is is useful bec
1f7dc 61 75 73 65 20 69 74 20 6d 65 61 6e 73 0a 20 20  ause it means.  
1f7dd 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 20 69 6e      ** we can in
1f7de 76 6f 6b 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74  voke OP_Column t
1f7df 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 76 64  o fill in the vd
1f7e0 62 65 20 63 75 72 73 6f 72 73 20 74 79 70 65 20  be cursors type 
1f7e1 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 6f 66  .      ** and of
1f7e2 66 73 65 74 20 63 61 63 68 65 20 77 69 74 68 6f  fset cache witho
1f7e3 75 74 20 63 61 75 73 69 6e 67 20 61 6e 79 20 49  ut causing any I
1f7e4 4f 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  O..      */.    
1f7e5 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1f7e6 6e 67 65 50 34 28 76 2c 20 33 2b 66 6c 61 67 73  ngeP4(v, 3+flags
1f7e7 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  , SQLITE_INT_TO_
1f7e8 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31  PTR(pTab->nCol+1
1f7e9 29 2c 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20  ),P4_INT32);.   
1f7ea 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1f7eb 61 6e 67 65 50 32 28 76 2c 20 37 2c 20 70 54 61  angeP2(v, 7, pTa
1f7ec 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20  b->nCol);.      
1f7ed 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  if( !db->mallocF
1f7ee 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
1f7ef 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
1f7f0 52 65 61 64 79 28 76 2c 20 31 2c 20 31 2c 20 31  Ready(v, 1, 1, 1
1f7f1 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
1f7f2 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20    }.    }.   .  
1f7f3 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1f7f4 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  aveAll(db);.    
1f7f5 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65  rc = sqlite3Safe
1f7f6 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69  tyOff(db);.    i
1f7f7 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c  f( NEVER(rc!=SQL
1f7f8 49 54 45 5f 4f 4b 29 20 7c 7c 20 64 62 2d 3e 6d  ITE_OK) || db->m
1f7f9 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1f7fa 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1f7fb 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  pen_out;.    }..
1f7fc 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
1f7fd 5f 69 6e 74 36 34 28 28 73 71 6c 69 74 65 33 5f  _int64((sqlite3_
1f7fe 73 74 6d 74 20 2a 29 76 2c 20 31 2c 20 69 52 6f  stmt *)v, 1, iRo
1f7ff 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  w);.    rc = sql
1f800 69 74 65 33 5f 73 74 65 70 28 28 73 71 6c 69 74  ite3_step((sqlit
1f801 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20 20  e3_stmt *)v);.  
1f802 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1f803 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 6e 41  _ROW ){.      nA
1f804 74 74 65 6d 70 74 2b 2b 3b 0a 20 20 20 20 20 20  ttempt++;.      
1f805 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e  rc = sqlite3_fin
1f806 61 6c 69 7a 65 28 28 73 71 6c 69 74 65 33 5f 73  alize((sqlite3_s
1f807 74 6d 74 20 2a 29 76 29 3b 0a 20 20 20 20 20 20  tmt *)v);.      
1f808 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1f809 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a  , zErr);.      z
1f80a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Err = sqlite3MPr
1f80b 69 6e 74 66 28 64 62 2c 20 73 71 6c 69 74 65 33  intf(db, sqlite3
1f80c 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20  _errmsg(db));.  
1f80d 20 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 7d      v = 0;.    }
1f80e 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e 41 74 74  .  } while( nAtt
1f80f 65 6d 70 74 3c 35 20 26 26 20 72 63 3d 3d 53 51  empt<5 && rc==SQ
1f810 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 3b 0a 0a  LITE_SCHEMA );..
1f811 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1f812 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f 2a 20 54  _ROW ){.    /* T
1f813 68 65 20 72 6f 77 2d 72 65 63 6f 72 64 20 68 61  he row-record ha
1f814 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 73 75  s been opened su
1f815 63 63 65 73 73 66 75 6c 6c 79 2e 20 43 68 65 63  ccessfully. Chec
1f816 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a  k that the.    *
1f817 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71 75 65 73  * column in ques
1f818 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 65  tion contains te
1f819 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e 20 49 66  xt or a blob. If
1f81a 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20   it contains.   
1f81b 20 2a 2a 20 74 65 78 74 2c 20 69 74 20 69 73 20   ** text, it is 
1f81c 75 70 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  up to the caller
1f81d 20 74 6f 20 67 65 74 20 74 68 65 20 65 6e 63 6f   to get the enco
1f81e 64 69 6e 67 20 72 69 67 68 74 2e 0a 20 20 20 20  ding right..    
1f81f 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c 6f 62 20  */.    Incrblob 
1f820 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75 33 32 20  *pBlob;.    u32 
1f821 74 79 70 65 20 3d 20 76 2d 3e 61 70 43 73 72 5b  type = v->apCsr[
1f822 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f 6c 5d 3b  0]->aType[iCol];
1f823 0a 0a 20 20 20 20 69 66 28 20 74 79 70 65 3c 31  ..    if( type<1
1f824 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  2 ){.      sqlit
1f825 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
1f826 72 29 3b 0a 20 20 20 20 20 20 7a 45 72 72 20 3d  r);.      zErr =
1f827 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1f828 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e  db, "cannot open
1f829 20 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25   value of type %
1f82a 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79  s",.          ty
1f82b 70 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79  pe==0?"null": ty
1f82c 70 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69  pe==7?"real": "i
1f82d 6e 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b  nteger".      );
1f82e 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1f82f 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
1f830 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f  goto blob_open_o
1f831 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42  ut;.    }.    pB
1f832 6c 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20  lob = (Incrblob 
1f833 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
1f834 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
1f835 28 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20  (Incrblob));.   
1f836 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1f837 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73  ailed ){.      s
1f838 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f839 20 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67   pBlob);.      g
1f83a 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75  oto blob_open_ou
1f83b 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c  t;.    }.    pBl
1f83c 6f 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67  ob->flags = flag
1f83d 73 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43  s;.    pBlob->pC
1f83e 73 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30  sr =  v->apCsr[0
1f83f 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20  ]->pCursor;.    
1f840 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1f841 72 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70  rCursor(pBlob->p
1f842 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Csr);.    sqlite
1f843 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66  3BtreeCacheOverf
1f844 6c 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29  low(pBlob->pCsr)
1f845 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
1f846 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 42  eeLeaveCursor(pB
1f847 6c 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  lob->pCsr);.    
1f848 70 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28  pBlob->pStmt = (
1f849 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76  sqlite3_stmt *)v
1f84a 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66  ;.    pBlob->iOf
1f84b 66 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b  fset = v->apCsr[
1f84c 30 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c  0]->aOffset[iCol
1f84d 5d 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42  ];.    pBlob->nB
1f84e 79 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  yte = sqlite3Vdb
1f84f 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74  eSerialTypeLen(t
1f850 79 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d  ype);.    pBlob-
1f851 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70  >db = db;.    *p
1f852 70 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 33  pBlob = (sqlite3
1f853 5f 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20  _blob *)pBlob;. 
1f854 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
1f855 4b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72  K;.  }else if( r
1f856 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1f857 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f858 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  e(db, zErr);.   
1f859 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d   zErr = sqlite3M
1f85a 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73  Printf(db, "no s
1f85b 75 63 68 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22  uch rowid: %lld"
1f85c 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20  , iRow);.    rc 
1f85d 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
1f85e 20 20 7d 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f    }..blob_open_o
1f85f 75 74 3a 0a 20 20 69 66 28 20 76 20 26 26 20 28  ut:.  if( v && (
1f860 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
1f861 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1f862 64 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  d) ){.    sqlite
1f863 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29  3VdbeFinalize(v)
1f864 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
1f865 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 7a 45 72  rror(db, rc, zEr
1f866 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  r);.  sqlite3DbF
1f867 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20  ree(db, zErr);. 
1f868 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
1f869 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a 20  e(db, pParse);. 
1f86a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
1f86b 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
1f86c 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1f86d 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
1f86e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1f86f 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 62 6c  /*.** Close a bl
1f870 6f 62 20 68 61 6e 64 6c 65 20 74 68 61 74 20 77  ob handle that w
1f871 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 63 72  as previously cr
1f872 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73  eated using.** s
1f873 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
1f874 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ()..*/.SQLITE_AP
1f875 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
1f876 6f 62 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33  ob_close(sqlite3
1f877 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20  _blob *pBlob){. 
1f878 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28   Incrblob *p = (
1f879 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62  Incrblob *)pBlob
1f87a 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  ;.  int rc;.  sq
1f87b 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66  lite3 *db;..  if
1f87c 28 20 70 20 29 7b 0a 20 20 20 20 64 62 20 3d 20  ( p ){.    db = 
1f87d 70 2d 3e 64 62 3b 0a 20 20 20 20 73 71 6c 69 74  p->db;.    sqlit
1f87e 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
1f87f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72  b->mutex);.    r
1f880 63 20 3d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  c = sqlite3_fina
1f881 6c 69 7a 65 28 70 2d 3e 70 53 74 6d 74 29 3b 0a  lize(p->pStmt);.
1f882 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f883 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71  e(db, p);.    sq
1f884 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1f885 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
1f886 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1f887 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
1f888 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1f889 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72  *.** Perform a r
1f88a 65 61 64 20 6f 72 20 77 72 69 74 65 20 6f 70 65  ead or write ope
1f88b 72 61 74 69 6f 6e 20 6f 6e 20 61 20 62 6c 6f 62  ration on a blob
1f88c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
1f88d 6c 6f 62 52 65 61 64 57 72 69 74 65 28 0a 20 20  lobReadWrite(.  
1f88e 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42  sqlite3_blob *pB
1f88f 6c 6f 62 2c 20 0a 20 20 76 6f 69 64 20 2a 7a 2c  lob, .  void *z,
1f890 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20 20 69 6e   .  int n, .  in
1f891 74 20 69 4f 66 66 73 65 74 2c 20 0a 20 20 69 6e  t iOffset, .  in
1f892 74 20 28 2a 78 43 61 6c 6c 29 28 42 74 43 75 72  t (*xCall)(BtCur
1f893 73 6f 72 2a 2c 20 75 33 32 2c 20 75 33 32 2c 20  sor*, u32, u32, 
1f894 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20  void*).){.  int 
1f895 72 63 3b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a  rc;.  Incrblob *
1f896 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29  p = (Incrblob *)
1f897 70 42 6c 6f 62 3b 0a 20 20 56 64 62 65 20 2a 76  pBlob;.  Vdbe *v
1f898 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  ;.  sqlite3 *db;
1f899 0a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ..  if( p==0 ) r
1f89a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
1f89b 55 53 45 3b 0a 20 20 64 62 20 3d 20 70 2d 3e 64  USE;.  db = p->d
1f89c 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  b;.  sqlite3_mut
1f89d 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
1f89e 65 78 29 3b 0a 20 20 76 20 3d 20 28 56 64 62 65  ex);.  v = (Vdbe
1f89f 2a 29 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 69  *)p->pStmt;..  i
1f8a0 66 28 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65  f( n<0 || iOffse
1f8a1 74 3c 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b  t<0 || (iOffset+
1f8a2 6e 29 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20  n)>p->nByte ){. 
1f8a3 20 20 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73     /* Request is
1f8a4 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52   out of range. R
1f8a5 65 74 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e  eturn a transien
1f8a6 74 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 20  t error. */.    
1f8a7 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1f8a8 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  R;.    sqlite3Er
1f8a9 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45  ror(db, SQLITE_E
1f8aa 52 52 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 65 6c  RROR, 0);.  } el
1f8ab 73 65 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20  se if( v==0 ){. 
1f8ac 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69     /* If there i
1f8ad 73 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68  s no statement h
1f8ae 61 6e 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20  andle, then the 
1f8af 62 6c 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a  blob-handle has.
1f8b0 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62      ** already b
1f8b1 65 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 64 2e  een invalidated.
1f8b2 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41   Return SQLITE_A
1f8b3 42 4f 52 54 20 69 6e 20 74 68 69 73 20 63 61 73  BORT in this cas
1f8b4 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63  e..    */.    rc
1f8b5 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b   = SQLITE_ABORT;
1f8b6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
1f8b7 20 43 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72   Call either Btr
1f8b8 65 65 44 61 74 61 28 29 20 6f 72 20 42 74 72 65  eeData() or Btre
1f8b9 65 50 75 74 44 61 74 61 28 29 2e 20 49 66 20 53  ePutData(). If S
1f8ba 51 4c 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20  QLITE_ABORT is. 
1f8bb 20 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20     ** returned, 
1f8bc 63 6c 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61  clean-up the sta
1f8bd 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20  tement handle.. 
1f8be 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
1f8bf 28 20 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b  ( db == v->db );
1f8c0 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1f8c1 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e  eEnterCursor(p->
1f8c2 70 43 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20  pCsr);.    rc = 
1f8c3 78 43 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69  xCall(p->pCsr, i
1f8c4 4f 66 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65  Offset+p->iOffse
1f8c5 74 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71  t, n, z);.    sq
1f8c6 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43  lite3BtreeLeaveC
1f8c7 75 72 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a  ursor(p->pCsr);.
1f8c8 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1f8c9 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20  TE_ABORT ){.    
1f8ca 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e    sqlite3VdbeFin
1f8cb 61 6c 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20  alize(v);.      
1f8cc 70 2d 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20  p->pStmt = 0;.  
1f8cd 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
1f8ce 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b  b->errCode = rc;
1f8cf 0a 20 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72  .      v->rc = r
1f8d0 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  c;.    }.  }.  r
1f8d1 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
1f8d2 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
1f8d3 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
1f8d4 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
1f8d5 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1f8d6 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72  .** Read data fr
1f8d7 6f 6d 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65  om a blob handle
1f8d8 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
1f8d9 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
1f8da 5f 72 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c  _read(sqlite3_bl
1f8db 6f 62 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20  ob *pBlob, void 
1f8dc 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69  *z, int n, int i
1f8dd 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72  Offset){.  retur
1f8de 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28  n blobReadWrite(
1f8df 70 42 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66  pBlob, z, n, iOf
1f8e0 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72  fset, sqlite3Btr
1f8e1 65 65 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  eeData);.}../*.*
1f8e2 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20  * Write data to 
1f8e3 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a  a blob handle..*
1f8e4 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1f8e5 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72   sqlite3_blob_wr
1f8e6 69 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ite(sqlite3_blob
1f8e7 20 2a 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76   *pBlob, const v
1f8e8 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69  oid *z, int n, i
1f8e9 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72  nt iOffset){.  r
1f8ea 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72  eturn blobReadWr
1f8eb 69 74 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64  ite(pBlob, (void
1f8ec 20 2a 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74   *)z, n, iOffset
1f8ed 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75  , sqlite3BtreePu
1f8ee 74 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  tData);.}../*.**
1f8ef 20 51 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61   Query a blob ha
1f8f0 6e 64 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a  ndle for the siz
1f8f1 65 20 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a  e of the data..*
1f8f2 2a 0a 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f  *.** The Incrblo
1f8f3 62 2e 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73  b.nByte field is
1f8f4 20 66 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c   fixed for the l
1f8f5 69 66 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49  ifetime of the I
1f8f6 6e 63 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f  ncrblob.** so no
1f8f7 20 6d 75 74 65 78 20 69 73 20 72 65 71 75 69 72   mutex is requir
1f8f8 65 64 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a  ed for access..*
1f8f9 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1f8fa 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79   sqlite3_blob_by
1f8fb 74 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  tes(sqlite3_blob
1f8fc 20 2a 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72   *pBlob){.  Incr
1f8fd 62 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62  blob *p = (Incrb
1f8fe 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72  lob *)pBlob;.  r
1f8ff 65 74 75 72 6e 20 70 20 3f 20 70 2d 3e 6e 42 79  eturn p ? p->nBy
1f900 74 65 20 3a 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69  te : 0;.}..#endi
1f901 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c  f /* #ifndef SQL
1f902 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
1f903 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  B */../*********
1f904 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
1f905 65 62 6c 6f 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  eblob.c ********
1f906 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f907 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f908 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1f909 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1f90a 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a   journal.c *****
1f90b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f90c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f90d 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
1f90e 20 41 75 67 75 73 74 20 32 32 0a 2a 2a 0a 2a 2a   August 22.**.**
1f90f 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1f910 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1f911 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1f912 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1f913 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1f914 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1f915 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1f916 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1f917 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1f918 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1f919 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1f91a 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1f91b 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1f91c 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1f91d 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1f91e 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1f91f 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1f920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f921 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f922 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f923 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f924 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1f925 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  This file implem
1f926 65 6e 74 73 20 61 20 73 70 65 63 69 61 6c 20 6b  ents a special k
1f927 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 5f 66  ind of sqlite3_f
1f928 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 65 64 0a  ile object used.
1f929 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20  ** by SQLite to 
1f92a 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c 20 66  create journal f
1f92b 69 6c 65 73 20 69 66 20 74 68 65 20 61 74 6f 6d  iles if the atom
1f92c 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69 7a  ic-write optimiz
1f92d 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e 61 62  ation.** is enab
1f92e 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  led..**.** The d
1f92f 69 73 74 69 6e 63 74 69 76 65 20 63 68 61 72 61  istinctive chara
1f930 63 74 65 72 69 73 74 69 63 20 6f 66 20 74 68 69  cteristic of thi
1f931 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 69  s sqlite3_file i
1f932 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 63  s that the.** ac
1f933 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66 69 6c  tual on disk fil
1f934 65 20 69 73 20 63 72 65 61 74 65 64 20 6c 61 7a  e is created laz
1f935 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 20 66 69  ily. When the fi
1f936 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c 0a 2a  le is created,.*
1f937 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 73 70 65  * the caller spe
1f938 63 69 66 69 65 73 20 61 20 62 75 66 66 65 72 20  cifies a buffer 
1f939 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e 2d 6d  size for an in-m
1f93a 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 6f 0a  emory buffer to.
1f93b 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  ** be used to se
1f93c 72 76 69 63 65 20 72 65 61 64 28 29 20 61 6e 64  rvice read() and
1f93d 20 77 72 69 74 65 28 29 20 72 65 71 75 65 73 74   write() request
1f93e 73 2e 20 54 68 65 20 61 63 74 75 61 6c 20 66 69  s. The actual fi
1f93f 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73  le.** on disk is
1f940 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f 72 20   not created or 
1f941 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 69 6c 20  populated until 
1f942 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20  either:.**.**   
1f943 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  1) The in-memory
1f944 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
1f945 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67 65 20  grows too large 
1f946 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61 74 65  for the allocate
1f947 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 66 66 65  d .**      buffe
1f948 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20 54 68  r, or.**   2) Th
1f949 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  e sqlite3Journal
1f94a 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f  Create() functio
1f94b 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  n is called..*/.
1f94c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
1f94d 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54  ABLE_ATOMIC_WRIT
1f94e 45 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75 72  E.../*.** A Jour
1f94f 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20 69  nalFile object i
1f950 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66 20  s a subclass of 
1f951 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73 65  sqlite3_file use
1f952 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f 70  d by.** as an op
1f953 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66  en file handle f
1f954 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  or journal files
1f955 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75 72  ..*/.struct Jour
1f956 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c 69  nalFile {.  sqli
1f957 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
1f958 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 49  pMethod;    /* I
1f959 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a 6f  /O methods on jo
1f95a 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20  urnal files */. 
1f95b 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 20 20   int nBuf;      
1f95c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f95d 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75 66   /* Size of zBuf
1f95e 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  [] in bytes */. 
1f95f 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 20 20   char *zBuf;    
1f960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f961 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 75 66   /* Space to buf
1f962 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69 74  fer journal writ
1f963 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69 7a  es */.  int iSiz
1f964 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1f965 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e          /* Amoun
1f966 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72 72  t of zBuf[] curr
1f967 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20  ently used */.  
1f968 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
1f969 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f96a 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 20 2a  /* xOpen flags *
1f96b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  /.  sqlite3_vfs 
1f96c 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20  *pVfs;          
1f96d 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61 6c      /* The "real
1f96e 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 46 53  " underlying VFS
1f96f 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
1f970 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20 20  le *pReal;      
1f971 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65        /* The "re
1f972 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 66  al" underlying f
1f973 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 2a  ile descriptor *
1f974 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1f975 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20 20  zJournal;       
1f976 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
1f977 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1f978 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74  */.};.typedef st
1f979 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  ruct JournalFile
1f97a 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a 2f   JournalFile;../
1f97b 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 20  *.** If it does 
1f97c 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  not already exis
1f97d 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20 70  ts, create and p
1f97e 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d 64  opulate the on-d
1f97f 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f 72  isk file .** for
1f980 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e 0a   JournalFile p..
1f981 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72  */.static int cr
1f982 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61 6c  eateFile(Journal
1f983 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  File *p){.  int 
1f984 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1f985 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c 20    if( !p->pReal 
1f986 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
1f987 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 73 71  ile *pReal = (sq
1f988 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70 5b  lite3_file *)&p[
1f989 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  1];.    rc = sql
1f98a 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70 56  ite3OsOpen(p->pV
1f98b 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  fs, p->zJournal,
1f98c 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67 73   pReal, p->flags
1f98d 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
1f98e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1f98f 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d 20       p->pReal = 
1f990 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 66 28  pReal;.      if(
1f991 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a 20   p->iSize>0 ){. 
1f992 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 2d         assert(p-
1f993 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66 29  >iSize<=p->nBuf)
1f994 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
1f995 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 2d  qlite3OsWrite(p-
1f996 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66 2c  >pReal, p->zBuf,
1f997 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a 20   p->iSize, 0);. 
1f998 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1f999 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1f99a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65  ./*.** Close the
1f99b 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1f99c 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28 73   int jrnlClose(s
1f99d 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1f99e 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c  d){.  JournalFil
1f99f 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46  e *p = (JournalF
1f9a0 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66  ile *)pJfd;.  if
1f9a1 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20  ( p->pReal ){.  
1f9a2 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65    sqlite3OsClose
1f9a3 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d 0a  (p->pReal);.  }.
1f9a4 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1f9a5 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 75 72  ->zBuf);.  retur
1f9a6 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1f9a7 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20  /*.** Read data 
1f9a8 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a  from the file..*
1f9a9 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e  /.static int jrn
1f9aa 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33  lRead(.  sqlite3
1f9ab 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20  _file *pJfd,    
1f9ac 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
1f9ad 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74  ile from which t
1f9ae 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64  o read */.  void
1f9af 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20   *zBuf,         
1f9b0 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65     /* Put the re
1f9b1 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20  sults here */.  
1f9b2 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1f9b3 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1f9b4 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61   of bytes to rea
1f9b5 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e  d */.  sqlite_in
1f9b6 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a  t64 iOfst     /*
1f9b7 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
1f9b8 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
1f9b9 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
1f9ba 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72  QLITE_OK;.  Jour
1f9bb 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
1f9bc 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
1f9bd 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c  ;.  if( p->pReal
1f9be 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
1f9bf 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70 52  ite3OsRead(p->pR
1f9c0 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74 2c  eal, zBuf, iAmt,
1f9c1 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73 65   iOfst);.  }else
1f9c2 20 69 66 28 20 28 69 41 6d 74 2b 69 4f 66 73 74   if( (iAmt+iOfst
1f9c3 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20  )>p->iSize ){.  
1f9c4 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
1f9c5 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a  ERR_SHORT_READ;.
1f9c6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d    }else{.    mem
1f9c7 63 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e 7a 42  cpy(zBuf, &p->zB
1f9c8 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d 74 29  uf[iOfst], iAmt)
1f9c9 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1f9ca 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  c;.}../*.** Writ
1f9cb 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 66 69  e data to the fi
1f9cc 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
1f9cd 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20 73  t jrnlWrite(.  s
1f9ce 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1f9cf 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75  d,    /* The jou
1f9d0 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 77  rnal file into w
1f9d1 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a 2f  hich to write */
1f9d2 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1f9d3 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61 6b  Buf,      /* Tak
1f9d4 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72 69  e data to be wri
1f9d5 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20 2a  tten from here *
1f9d6 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20 20  /.  int iAmt,   
1f9d7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1f9d8 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f  mber of bytes to
1f9d9 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69   write */.  sqli
1f9da 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20  te_int64 iOfst  
1f9db 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69 74     /* Begin writ
1f9dc 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73  ing at this offs
1f9dd 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65  et into the file
1f9de 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
1f9df 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a  = SQLITE_OK;.  J
1f9e0 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20  ournalFile *p = 
1f9e1 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70  (JournalFile *)p
1f9e2 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70  Jfd;.  if( !p->p
1f9e3 52 65 61 6c 20 26 26 20 28 69 4f 66 73 74 2b 69  Real && (iOfst+i
1f9e4 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 7b 0a  Amt)>p->nBuf ){.
1f9e5 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 65 46      rc = createF
1f9e6 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66  ile(p);.  }.  if
1f9e7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1f9e8 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 52  ){.    if( p->pR
1f9e9 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  eal ){.      rc 
1f9ea 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
1f9eb 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66 2c  (p->pReal, zBuf,
1f9ec 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a 20   iAmt, iOfst);. 
1f9ed 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1f9ee 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75 66 5b  memcpy(&p->zBuf[
1f9ef 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 69 41  iOfst], zBuf, iA
1f9f0 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  mt);.      if( p
1f9f1 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74 2b 69  ->iSize<(iOfst+i
1f9f2 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Amt) ){.        
1f9f3 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f 66 73  p->iSize = (iOfs
1f9f4 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20 20 7d  t+iAmt);.      }
1f9f5 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1f9f6 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1f9f7 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66 69   Truncate the fi
1f9f8 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
1f9f9 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28 73  t jrnlTruncate(s
1f9fa 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1f9fb 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  d, sqlite_int64 
1f9fc 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  size){.  int rc 
1f9fd 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a  = SQLITE_OK;.  J
1f9fe 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20  ournalFile *p = 
1f9ff 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70  (JournalFile *)p
1fa00 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52  Jfd;.  if( p->pR
1fa01 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  eal ){.    rc = 
1fa02 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74  sqlite3OsTruncat
1fa03 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69 7a 65  e(p->pReal, size
1fa04 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  );.  }else if( s
1fa05 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29 7b 0a  ize<p->iSize ){.
1fa06 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20 73      p->iSize = s
1fa07 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ize;.  }.  retur
1fa08 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
1fa09 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f  ync the file..*/
1fa0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c  .static int jrnl
1fa0b 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c  Sync(sqlite3_fil
1fa0c 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 6c 61  e *pJfd, int fla
1fa0d 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  gs){.  int rc;. 
1fa0e 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20   JournalFile *p 
1fa0f 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  = (JournalFile *
1fa10 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e  )pJfd;.  if( p->
1fa11 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20  pReal ){.    rc 
1fa12 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28  = sqlite3OsSync(
1fa13 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 73 29  p->pReal, flags)
1fa14 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
1fa15 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1fa16 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1fa17 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74  }../*.** Query t
1fa18 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  he size of the f
1fa19 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f  ile in bytes..*/
1fa1a 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e 6c  .static int jrnl
1fa1b 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65 33  FileSize(sqlite3
1fa1c 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c  _file *pJfd, sql
1fa1d 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65  ite_int64 *pSize
1fa1e 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1fa1f 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72 6e  LITE_OK;.  Journ
1fa20 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f 75  alFile *p = (Jou
1fa21 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64 3b  rnalFile *)pJfd;
1fa22 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c 20  .  if( p->pReal 
1fa23 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1fa24 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 2d  te3OsFileSize(p-
1fa25 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29 3b 0a  >pReal, pSize);.
1fa26 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 53    }else{.    *pS
1fa27 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e  ize = (sqlite_in
1fa28 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b 0a 20  t64) p->iSize;. 
1fa29 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1fa2a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f  }../*.** Table o
1fa2b 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4a 6f  f methods for Jo
1fa2c 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69 74 65  urnalFile sqlite
1fa2d 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a  3_file object..*
1fa2e 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74 20  /.static struct 
1fa2f 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f  sqlite3_io_metho
1fa30 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65  ds JournalFileMe
1fa31 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20  thods = {.  1,  
1fa32 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56             /* iV
1fa33 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e 6c  ersion */.  jrnl
1fa34 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78 43  Close,     /* xC
1fa35 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52 65  lose */.  jrnlRe
1fa36 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61  ad,      /* xRea
1fa37 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69 74 65  d */.  jrnlWrite
1fa38 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20  ,     /* xWrite 
1fa39 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 61 74  */.  jrnlTruncat
1fa3a 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65  e,  /* xTruncate
1fa3b 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c 20   */.  jrnlSync, 
1fa3c 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f       /* xSync */
1fa3d 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 2c  .  jrnlFileSize,
1fa3e 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20 2a    /* xFileSize *
1fa3f 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
1fa40 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20     /* xLock */. 
1fa41 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1fa42 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20  /* xUnlock */.  
1fa43 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1fa44 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
1fa45 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Lock */.  0,    
1fa46 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c           /* xFil
1fa47 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c  eControl */.  0,
1fa48 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fa49 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a 20  xSectorSize */. 
1fa4a 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
1fa4b 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72 61 63  /* xDeviceCharac
1fa4c 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a  teristics */.};.
1fa4d 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a  ./* .** Open a j
1fa4e 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a  ournal file..*/.
1fa4f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1fa50 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61  nt sqlite3Journa
1fa51 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  lOpen(.  sqlite3
1fa52 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
1fa53 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 74      /* The VFS t
1fa54 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75 61 6c  o use for actual
1fa55 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20 63   file I/O */.  c
1fa56 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1fa57 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ,         /* Nam
1fa58 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
1fa59 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74   file */.  sqlit
1fa5a 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20  e3_file *pJfd,  
1fa5b 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f        /* Preallo
1fa5c 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 69 6c  cated, blank fil
1fa5d 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e  e handle */.  in
1fa5e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
1fa5f 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e           /* Open
1fa60 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69  ing flags */.  i
1fa61 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20 20 20  nt nBuf         
1fa62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
1fa63 65 73 20 62 75 66 66 65 72 65 64 20 62 65 66 6f  es buffered befo
1fa64 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66  re opening the f
1fa65 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75 72  ile */.){.  Jour
1fa66 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
1fa67 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
1fa68 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ;.  memset(p, 0,
1fa69 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
1fa6a 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 69 66  ize(pVfs));.  if
1fa6b 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 20 20  ( nBuf>0 ){.    
1fa6c 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 74 65  p->zBuf = sqlite
1fa6d 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75 66  3MallocZero(nBuf
1fa6e 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 7a  );.    if( !p->z
1fa6f 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72 65 74  Buf ){.      ret
1fa70 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1fa71 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1fa72 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
1fa73 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20  te3OsOpen(pVfs, 
1fa74 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 6c 61  zName, pJfd, fla
1fa75 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70 2d  gs, 0);.  }.  p-
1fa76 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f 75 72  >pMethod = &Jour
1fa77 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 3b 0a  nalFileMethods;.
1fa78 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 75 66    p->nBuf = nBuf
1fa79 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 66  ;.  p->flags = f
1fa7a 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75 72  lags;.  p->zJour
1fa7b 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 70  nal = zName;.  p
1fa7c 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20  ->pVfs = pVfs;. 
1fa7d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1fa7e 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  K;.}../*.** If t
1fa7f 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20 70 6f  he argument p po
1fa80 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 6e 61  ints to a Journa
1fa81 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 2c  lFile structure,
1fa82 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c 79   and the underly
1fa83 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 73 20  ing.** file has 
1fa84 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72 65  not yet been cre
1fa85 61 74 65 64 2c 20 63 72 65 61 74 65 20 69 74 20  ated, create it 
1fa86 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  now..*/.SQLITE_P
1fa87 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1fa88 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28  e3JournalCreate(
1fa89 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 29  sqlite3_file *p)
1fa8a 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 74 68  {.  if( p->pMeth
1fa8b 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 69 6c  ods!=&JournalFil
1fa8c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  eMethods ){.    
1fa8d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1fa8e 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63  ;.  }.  return c
1fa8f 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 72 6e  reateFile((Journ
1fa90 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a 0a  alFile *)p);.}..
1fa91 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /* .** Return th
1fa92 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
1fa93 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74  s required to st
1fa94 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46 69 6c  ore a JournalFil
1fa95 65 20 74 68 61 74 20 75 73 65 73 20 76 66 73 0a  e that uses vfs.
1fa96 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65 61 74  ** pVfs to creat
1fa97 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  e the underlying
1fa98 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e 0a   on-disk files..
1fa99 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1fa9a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75  E int sqlite3Jou
1fa9b 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33  rnalSize(sqlite3
1fa9c 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20 72  _vfs *pVfs){.  r
1fa9d 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 7a 4f  eturn (pVfs->szO
1fa9e 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f 75  sFile+sizeof(Jou
1fa9f 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a 23 65  rnalFile));.}.#e
1faa0 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
1faa1 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a 6f 75  ***** End of jou
1faa2 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  rnal.c *********
1faa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faa4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faa5 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1faa6 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1faa7 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a   memjournal.c **
1faa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faaa 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
1faab 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a 2a 2a   October 7.**.**
1faac 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1faad 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1faae 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1faaf 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1fab0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1fab1 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1fab2 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1fab3 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1fab4 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1fab5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1fab6 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1fab7 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1fab8 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1fab9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1faba 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1fabb 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1fabc 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1fabd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fabe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fabf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1fac2 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1fac3 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 69  ns code use to i
1fac4 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d 6d  mplement an in-m
1fac5 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20 6a  emory rollback j
1fac6 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 69  ournal..** The i
1fac7 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63  n-memory rollbac
1fac8 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73 65  k journal is use
1fac9 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74 72 61  d to journal tra
1faca 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a 2a 2a  nsactions for.**
1facb 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61 74 61   ":memory:" data
1facc 62 61 73 65 73 20 61 6e 64 20 77 68 65 6e 20 74  bases and when t
1facd 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d  he journal_mode=
1face 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 20 69 73  MEMORY pragma is
1facf 20 75 73 65 64 2e 0a 2a 2f 0a 0a 2f 2a 20 46 6f   used..*/../* Fo
1fad0 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73  rward references
1fad1 20 74 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 72   to internal str
1fad2 75 63 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 64  uctures */.typed
1fad3 65 66 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75  ef struct MemJou
1fad4 72 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b  rnal MemJournal;
1fad5 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
1fad6 46 69 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 6f  FilePoint FilePo
1fad7 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  int;.typedef str
1fad8 75 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 69  uct FileChunk Fi
1fad9 6c 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61  leChunk;../* Spa
1fada 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
1fadb 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
1fadc 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  is allocated in 
1fadd 69 6e 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a  increments of.**
1fade 20 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73   this many bytes
1fadf 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65  ..**.** The size
1fae0 20 63 68 6f 73 65 6e 20 69 73 20 61 20 6c 69 74   chosen is a lit
1fae1 74 6c 65 20 6c 65 73 73 20 74 68 61 6e 20 61 20  tle less than a 
1fae2 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 54  power of two.  T
1fae3 68 61 74 20 77 61 79 2c 0a 2a 2a 20 74 68 65 20  hat way,.** the 
1fae4 46 69 6c 65 43 68 75 6e 6b 20 6f 62 6a 65 63 74  FileChunk object
1fae5 20 77 69 6c 6c 20 68 61 76 65 20 61 20 73 69 7a   will have a siz
1fae6 65 20 74 68 61 74 20 61 6c 6d 6f 73 74 20 65 78  e that almost ex
1fae7 61 63 74 6c 79 20 66 69 6c 6c 73 0a 2a 2a 20 61  actly fills.** a
1fae8 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c   power-of-two al
1fae9 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 69 73 20  location.  This 
1faea 6d 69 6d 69 6d 69 7a 65 73 20 77 61 73 74 65 64  mimimizes wasted
1faeb 20 73 70 61 63 65 20 69 6e 20 70 6f 77 65 72 2d   space in power-
1faec 6f 66 2d 74 77 6f 0a 2a 2a 20 6d 65 6d 6f 72 79  of-two.** memory
1faed 20 61 6c 6c 6f 63 61 74 6f 72 73 2e 0a 2a 2f 0a   allocators..*/.
1faee 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f  #define JOURNAL_
1faef 43 48 55 4e 4b 53 49 5a 45 20 28 28 69 6e 74 29  CHUNKSIZE ((int)
1faf0 28 31 30 32 34 2d 73 69 7a 65 6f 66 28 46 69 6c  (1024-sizeof(Fil
1faf1 65 43 68 75 6e 6b 2a 29 29 29 0a 0a 2f 2a 20 4d  eChunk*)))../* M
1faf2 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74 68 65  acro to find the
1faf3 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77 6f 20   minimum of two 
1faf4 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 2e 0a  numeric values..
1faf5 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a 23  */.#ifndef MIN.#
1faf6 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79 29   define MIN(x,y)
1faf7 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a 28 79   ((x)<(y)?(x):(y
1faf8 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  )).#endif../*.**
1faf9 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f   The rollback jo
1fafa 75 72 6e 61 6c 20 69 73 20 63 6f 6d 70 6f 73 65  urnal is compose
1fafb 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69  d of a linked li
1fafc 73 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75  st of these stru
1fafd 63 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63  ctures..*/.struc
1fafe 74 20 46 69 6c 65 43 68 75 6e 6b 20 7b 0a 20 20  t FileChunk {.  
1faff 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 78 74  FileChunk *pNext
1fb00 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1fb01 2f 2a 20 4e 65 78 74 20 63 68 75 6e 6b 20 69 6e  /* Next chunk in
1fb02 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   the journal */.
1fb03 20 20 75 38 20 7a 43 68 75 6e 6b 5b 4a 4f 55 52    u8 zChunk[JOUR
1fb04 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 5d 3b 20  NAL_CHUNKSIZE]; 
1fb05 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20    /* Content of 
1fb06 74 68 69 73 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b  this chunk */.};
1fb07 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
1fb08 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
1fb09 63 74 20 73 65 72 76 65 73 20 61 73 20 61 20 63  ct serves as a c
1fb0a 75 72 73 6f 72 20 69 6e 74 6f 20 74 68 65 20 72  ursor into the r
1fb0b 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
1fb0c 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20 63  .** The cursor c
1fb0d 61 6e 20 62 65 20 65 69 74 68 65 72 20 66 6f 72  an be either for
1fb0e 20 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74   reading or writ
1fb0f 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46  ing..*/.struct F
1fb10 69 6c 65 50 6f 69 6e 74 20 7b 0a 20 20 73 71 6c  ilePoint {.  sql
1fb11 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66 73  ite3_int64 iOffs
1fb12 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  et;          /* 
1fb13 4f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20  Offset from the 
1fb14 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65  beginning of the
1fb15 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65 43   file */.  FileC
1fb16 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 20 20 20  hunk *pChunk;   
1fb17 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70             /* Sp
1fb18 65 63 69 66 69 63 20 63 68 75 6e 6b 20 69 6e 74  ecific chunk int
1fb19 6f 20 77 68 69 63 68 20 63 75 72 73 6f 72 20 70  o which cursor p
1fb1a 6f 69 6e 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  oints */.};../*.
1fb1b 2a 2a 20 54 68 69 73 20 73 75 62 63 6c 61 73 73  ** This subclass
1fb1c 20 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f   is a subclass o
1fb1d 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 20  f sqlite3_file. 
1fb1e 20 45 61 63 68 20 6f 70 65 6e 20 6d 65 6d 6f 72   Each open memor
1fb1f 79 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 69 73 20  y-journal.** is 
1fb20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
1fb21 68 69 73 20 63 6c 61 73 73 2e 0a 2a 2f 0a 73 74  his class..*/.st
1fb22 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ruct MemJournal 
1fb23 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  {.  sqlite3_io_m
1fb24 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b  ethods *pMethod;
1fb25 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 63 6c      /* Parent cl
1fb26 61 73 73 2e 20 4d 55 53 54 20 42 45 20 46 49 52  ass. MUST BE FIR
1fb27 53 54 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e  ST */.  FileChun
1fb28 6b 20 2a 70 46 69 72 73 74 3b 20 20 20 20 20 20  k *pFirst;      
1fb29 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 64 20          /* Head 
1fb2a 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 68 75  of in-memory chu
1fb2b 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 46 69 6c  nk-list */.  Fil
1fb2c 65 50 6f 69 6e 74 20 65 6e 64 70 6f 69 6e 74 3b  ePoint endpoint;
1fb2d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fb2e 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65  Pointer to the e
1fb2f 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a  nd of the file *
1fb30 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 72 65  /.  FilePoint re
1fb31 61 64 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20  adpoint;        
1fb32 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
1fb33 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
1fb34 20 6c 61 73 74 20 78 52 65 61 64 28 29 20 2a 2f   last xRead() */
1fb35 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  .};../*.** Read 
1fb36 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 69 6e  data from the in
1fb37 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20  -memory journal 
1fb38 66 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20 74  file.  This is t
1fb39 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
1fb3a 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71 6c 69  n.** of the sqli
1fb3b 74 65 33 5f 76 66 73 2e 78 52 65 61 64 20 6d 65  te3_vfs.xRead me
1fb3c 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
1fb3d 69 6e 74 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 28  int memjrnlRead(
1fb3e 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
1fb3f 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65  *pJfd,    /* The
1fb40 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 72   journal file fr
1fb41 6f 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61 64  om which to read
1fb42 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66   */.  void *zBuf
1fb43 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
1fb44 50 75 74 20 74 68 65 20 72 65 73 75 6c 74 73 20  Put the results 
1fb45 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41  here */.  int iA
1fb46 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
1fb47 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1fb48 74 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20  tes to read */. 
1fb49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f   sqlite_int64 iO
1fb4a 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e  fst     /* Begin
1fb4b 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73   reading at this
1fb4c 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20   offset */.){.  
1fb4d 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20  MemJournal *p = 
1fb4e 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a  (MemJournal *)pJ
1fb4f 66 64 3b 0a 20 20 75 38 20 2a 7a 4f 75 74 20 3d  fd;.  u8 *zOut =
1fb50 20 7a 42 75 66 3b 0a 20 20 69 6e 74 20 6e 52 65   zBuf;.  int nRe
1fb51 61 64 20 3d 20 69 41 6d 74 3b 0a 20 20 69 6e 74  ad = iAmt;.  int
1fb52 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3b 0a 20   iChunkOffset;. 
1fb53 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43 68 75   FileChunk *pChu
1fb54 6e 6b 3b 0a 0a 20 20 2f 2a 20 53 51 4c 69 74 65  nk;..  /* SQLite
1fb55 20 6e 65 76 65 72 20 74 72 69 65 73 20 74 6f 20   never tries to 
1fb56 72 65 61 64 20 70 61 73 74 20 74 68 65 20 65 6e  read past the en
1fb57 64 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20  d of a rollback 
1fb58 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a  journal file */.
1fb59 20 20 61 73 73 65 72 74 28 20 69 4f 66 73 74 2b    assert( iOfst+
1fb5a 69 41 6d 74 3c 3d 70 2d 3e 65 6e 64 70 6f 69 6e  iAmt<=p->endpoin
1fb5b 74 2e 69 4f 66 66 73 65 74 20 29 3b 0a 0a 20 20  t.iOffset );..  
1fb5c 69 66 28 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74  if( p->readpoint
1fb5d 2e 69 4f 66 66 73 65 74 21 3d 69 4f 66 73 74 20  .iOffset!=iOfst 
1fb5e 7c 7c 20 69 4f 66 73 74 3d 3d 30 20 29 7b 0a 20  || iOfst==0 ){. 
1fb5f 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
1fb60 20 69 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 66   iOff = 0;.    f
1fb61 6f 72 28 70 43 68 75 6e 6b 3d 70 2d 3e 70 46 69  or(pChunk=p->pFi
1fb62 72 73 74 3b 20 0a 20 20 20 20 20 20 20 20 41 4c  rst; .        AL
1fb63 57 41 59 53 28 70 43 68 75 6e 6b 29 20 26 26 20  WAYS(pChunk) && 
1fb64 28 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f 43 48  (iOff+JOURNAL_CH
1fb65 55 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66 73 74 3b  UNKSIZE)<=iOfst;
1fb66 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b 3d  .        pChunk=
1fb67 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 0a 20 20  pChunk->pNext.  
1fb68 20 20 29 7b 0a 20 20 20 20 20 20 69 4f 66 66 20    ){.      iOff 
1fb69 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  += JOURNAL_CHUNK
1fb6a 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  SIZE;.    }.  }e
1fb6b 6c 73 65 7b 0a 20 20 20 20 70 43 68 75 6e 6b 20  lse{.    pChunk 
1fb6c 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 70  = p->readpoint.p
1fb6d 43 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20 20 69 43  Chunk;.  }..  iC
1fb6e 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e  hunkOffset = (in
1fb6f 74 29 28 69 4f 66 73 74 25 4a 4f 55 52 4e 41 4c  t)(iOfst%JOURNAL
1fb70 5f 43 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 64  _CHUNKSIZE);.  d
1fb71 6f 20 7b 0a 20 20 20 20 69 6e 74 20 69 53 70 61  o {.    int iSpa
1fb72 63 65 20 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55  ce = JOURNAL_CHU
1fb73 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f  NKSIZE - iChunkO
1fb74 66 66 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 6e  ffset;.    int n
1fb75 43 6f 70 79 20 3d 20 4d 49 4e 28 6e 52 65 61 64  Copy = MIN(nRead
1fb76 2c 20 28 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  , (JOURNAL_CHUNK
1fb77 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66  SIZE - iChunkOff
1fb78 73 65 74 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70  set));.    memcp
1fb79 79 28 7a 4f 75 74 2c 20 26 70 43 68 75 6e 6b 2d  y(zOut, &pChunk-
1fb7a 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66  >zChunk[iChunkOf
1fb7b 66 73 65 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20  fset], nCopy);. 
1fb7c 20 20 20 7a 4f 75 74 20 2b 3d 20 6e 43 6f 70 79     zOut += nCopy
1fb7d 3b 0a 20 20 20 20 6e 52 65 61 64 20 2d 3d 20 69  ;.    nRead -= i
1fb7e 53 70 61 63 65 3b 0a 20 20 20 20 69 43 68 75 6e  Space;.    iChun
1fb7f 6b 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 7d  kOffset = 0;.  }
1fb80 20 77 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30   while( nRead>=0
1fb81 20 26 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75   && (pChunk=pChu
1fb82 6e 6b 2d 3e 70 4e 65 78 74 29 21 3d 30 20 26 26  nk->pNext)!=0 &&
1fb83 20 6e 52 65 61 64 3e 30 20 29 3b 0a 20 20 70 2d   nRead>0 );.  p-
1fb84 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66 73  >readpoint.iOffs
1fb85 65 74 20 3d 20 69 4f 66 73 74 2b 69 41 6d 74 3b  et = iOfst+iAmt;
1fb86 0a 20 20 70 2d 3e 72 65 61 64 70 6f 69 6e 74 2e  .  p->readpoint.
1fb87 70 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 3b  pChunk = pChunk;
1fb88 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
1fb89 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  E_OK;.}../*.** W
1fb8a 72 69 74 65 20 64 61 74 61 20 74 6f 20 74 68 65  rite data to the
1fb8b 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
1fb8c 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 57 72 69 74   int memjrnlWrit
1fb8d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  e(.  sqlite3_fil
1fb8e 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54  e *pJfd,    /* T
1fb8f 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1fb90 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72  into which to wr
1fb91 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ite */.  const v
1fb92 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20  oid *zBuf,      
1fb93 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20  /* Take data to 
1fb94 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20  be written from 
1fb95 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41  here */.  int iA
1fb96 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
1fb97 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1fb98 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  tes to write */.
1fb99 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
1fb9a 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69  Ofst     /* Begi
1fb9b 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69  n writing at thi
1fb9c 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68  s offset into th
1fb9d 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4d  e file */.){.  M
1fb9e 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28  emJournal *p = (
1fb9f 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66  MemJournal *)pJf
1fba0 64 3b 0a 20 20 69 6e 74 20 6e 57 72 69 74 65 20  d;.  int nWrite 
1fba1 3d 20 69 41 6d 74 3b 0a 20 20 75 38 20 2a 7a 57  = iAmt;.  u8 *zW
1fba2 72 69 74 65 20 3d 20 28 75 38 20 2a 29 7a 42 75  rite = (u8 *)zBu
1fba3 66 3b 0a 0a 20 20 2f 2a 20 41 6e 20 69 6e 2d 6d  f;..  /* An in-m
1fba4 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69  emory journal fi
1fba5 6c 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 65  le should only e
1fba6 76 65 72 20 62 65 20 61 70 70 65 6e 64 65 64 20  ver be appended 
1fba7 74 6f 2e 20 52 61 6e 64 6f 6d 0a 20 20 2a 2a 20  to. Random.  ** 
1fba8 61 63 63 65 73 73 20 77 72 69 74 65 73 20 61 72  access writes ar
1fba9 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 62  e not required b
1fbaa 79 20 73 71 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20  y sqlite..  */. 
1fbab 20 61 73 73 65 72 74 28 20 69 4f 66 73 74 3d 3d   assert( iOfst==
1fbac 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66  p->endpoint.iOff
1fbad 73 65 74 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f  set );.  UNUSED_
1fbae 50 41 52 41 4d 45 54 45 52 28 69 4f 66 73 74 29  PARAMETER(iOfst)
1fbaf 3b 0a 0a 20 20 77 68 69 6c 65 28 20 6e 57 72 69  ;..  while( nWri
1fbb0 74 65 3e 30 20 29 7b 0a 20 20 20 20 46 69 6c 65  te>0 ){.    File
1fbb1 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 20 3d 20  Chunk *pChunk = 
1fbb2 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75  p->endpoint.pChu
1fbb3 6e 6b 3b 0a 20 20 20 20 69 6e 74 20 69 43 68 75  nk;.    int iChu
1fbb4 6e 6b 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 29  nkOffset = (int)
1fbb5 28 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66  (p->endpoint.iOf
1fbb6 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55  fset%JOURNAL_CHU
1fbb7 4e 4b 53 49 5a 45 29 3b 0a 20 20 20 20 69 6e 74  NKSIZE);.    int
1fbb8 20 69 53 70 61 63 65 20 3d 20 4d 49 4e 28 6e 57   iSpace = MIN(nW
1fbb9 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f 43 48  rite, JOURNAL_CH
1fbba 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b  UNKSIZE - iChunk
1fbbb 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20 69 66  Offset);..    if
1fbbc 28 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 3d 3d  ( iChunkOffset==
1fbbd 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 65  0 ){.      /* Ne
1fbbe 77 20 63 68 75 6e 6b 20 69 73 20 72 65 71 75 69  w chunk is requi
1fbbf 72 65 64 20 74 6f 20 65 78 74 65 6e 64 20 74 68  red to extend th
1fbc0 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20  e file. */.     
1fbc1 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e 65 77   FileChunk *pNew
1fbc2 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
1fbc3 63 28 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75  c(sizeof(FileChu
1fbc4 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20  nk));.      if( 
1fbc5 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20  !pNew ){.       
1fbc6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
1fbc7 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  OERR_NOMEM;.    
1fbc8 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e    }.      pNew->
1fbc9 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 20  pNext = 0;.     
1fbca 20 69 66 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20   if( pChunk ){. 
1fbcb 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1fbcc 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 20 20 20  ->pFirst );.    
1fbcd 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70 4e 65 78      pChunk->pNex
1fbce 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20  t = pNew;.      
1fbcf 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
1fbd0 73 73 65 72 74 28 20 21 70 2d 3e 70 46 69 72 73  ssert( !p->pFirs
1fbd1 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e  t );.        p->
1fbd2 70 46 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20  pFirst = pNew;. 
1fbd3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e       }.      p->
1fbd4 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 20  endpoint.pChunk 
1fbd5 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a 0a 20  = pNew;.    }.. 
1fbd6 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 65 6e     memcpy(&p->en
1fbd7 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d 3e 7a  dpoint.pChunk->z
1fbd8 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73  Chunk[iChunkOffs
1fbd9 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20 69 53 70  et], zWrite, iSp
1fbda 61 63 65 29 3b 0a 20 20 20 20 7a 57 72 69 74 65  ace);.    zWrite
1fbdb 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20 20 20   += iSpace;.    
1fbdc 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 61 63 65  nWrite -= iSpace
1fbdd 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f 69 6e  ;.    p->endpoin
1fbde 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69 53 70  t.iOffset += iSp
1fbdf 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ace;.  }..  retu
1fbe0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1fbe1 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20  ./*.** Truncate 
1fbe2 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1fbe3 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 54  tic int memjrnlT
1fbe4 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f  runcate(sqlite3_
1fbe5 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69  file *pJfd, sqli
1fbe6 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a  te_int64 size){.
1fbe7 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20    MemJournal *p 
1fbe8 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29  = (MemJournal *)
1fbe9 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43 68 75 6e  pJfd;.  FileChun
1fbea 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 61 73 73  k *pChunk;.  ass
1fbeb 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b 0a 20 20  ert(size==0);.  
1fbec 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1fbed 28 73 69 7a 65 29 3b 0a 20 20 70 43 68 75 6e 6b  (size);.  pChunk
1fbee 20 3d 20 70 2d 3e 70 46 69 72 73 74 3b 0a 20 20   = p->pFirst;.  
1fbef 77 68 69 6c 65 28 20 70 43 68 75 6e 6b 20 29 7b  while( pChunk ){
1fbf0 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a  .    FileChunk *
1fbf1 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b 3b 0a 20  pTmp = pChunk;. 
1fbf2 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 43 68 75     pChunk = pChu
1fbf3 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73  nk->pNext;.    s
1fbf4 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6d 70  qlite3_free(pTmp
1fbf5 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
1fbf6 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70  MemJournalOpen(p
1fbf7 4a 66 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  Jfd);.  return S
1fbf8 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1fbf9 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66 69 6c  ** Close the fil
1fbfa 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
1fbfb 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 28 73 71   memjrnlClose(sq
1fbfc 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1fbfd 29 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e  ){.  memjrnlTrun
1fbfe 63 61 74 65 28 70 4a 66 64 2c 20 30 29 3b 0a 20  cate(pJfd, 0);. 
1fbff 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1fc00 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e  K;.}.../*.** Syn
1fc01 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  c the file..**.*
1fc02 2a 20 53 79 6e 63 69 6e 67 20 61 6e 20 69 6e 2d  * Syncing an in-
1fc03 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 20 69  memory journal i
1fc04 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 41 6e 64 2c  s a no-op.  And,
1fc05 20 69 6e 20 66 61 63 74 2c 20 74 68 69 73 20 72   in fact, this r
1fc06 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 6e 65 76  outine.** is nev
1fc07 65 72 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 77  er called in a w
1fc08 6f 72 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74  orking implement
1fc09 61 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6d 70  ation.  This imp
1fc0a 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 65  lementation.** e
1fc0b 78 69 73 74 73 20 70 75 72 65 6c 79 20 61 73 20  xists purely as 
1fc0c 61 20 63 6f 6e 74 69 6e 67 65 6e 63 79 2c 20 69  a contingency, i
1fc0d 6e 20 63 61 73 65 20 73 6f 6d 65 20 6d 61 6c 66  n case some malf
1fc0e 75 6e 63 74 69 6f 6e 20 69 6e 20 73 6f 6d 65 20  unction in some 
1fc0f 6f 74 68 65 72 0a 2a 2a 20 70 61 72 74 20 6f 66  other.** part of
1fc10 20 53 51 4c 69 74 65 20 63 61 75 73 65 73 20 53   SQLite causes S
1fc11 79 6e 63 20 74 6f 20 62 65 20 63 61 6c 6c 65 64  ync to be called
1fc12 20 62 79 20 6d 69 73 74 61 6b 65 2e 0a 2a 2f 0a   by mistake..*/.
1fc13 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72  static int memjr
1fc14 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66  nlSync(sqlite3_f
1fc15 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e  ile *NotUsed, in
1fc16 74 20 4e 6f 74 55 73 65 64 32 29 7b 20 20 20 2f  t NotUsed2){   /
1fc17 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 55 4e 55  *NO_TEST*/.  UNU
1fc18 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
1fc19 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
1fc1a 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
1fc1b 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f 54            /*NO_T
1fc1c 45 53 54 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  EST*/.  assert( 
1fc1d 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  0 );            
1fc1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f       /*NO_TEST*/
1fc21 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1fc22 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
1fc23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc25 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 7d 20 20 20  /*NO_TEST*/.}   
1fc26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f             /*NO_
1fc2a 54 45 53 54 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75  TEST*/../*.** Qu
1fc2b 65 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66 20  ery the size of 
1fc2c 74 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74 65  the file in byte
1fc2d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
1fc2e 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a 65   memjrnlFileSize
1fc2f 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1fc30 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  Jfd, sqlite_int6
1fc31 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 4d 65 6d  4 *pSize){.  Mem
1fc32 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65  Journal *p = (Me
1fc33 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b  mJournal *)pJfd;
1fc34 0a 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c  .  *pSize = (sql
1fc35 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 65 6e  ite_int64) p->en
1fc36 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 3b 0a  dpoint.iOffset;.
1fc37 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1fc38 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62  OK;.}../*.** Tab
1fc39 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f  le of methods fo
1fc3a 72 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 73 71 6c  r MemJournal sql
1fc3b 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74  ite3_file object
1fc3c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
1fc3d 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  ct sqlite3_io_me
1fc3e 74 68 6f 64 73 20 4d 65 6d 4a 6f 75 72 6e 61 6c  thods MemJournal
1fc3f 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c  Methods = {.  1,
1fc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fc41 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
1fc42 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20   memjrnlClose,  
1fc43 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a     /* xClose */.
1fc44 20 20 6d 65 6d 6a 72 6e 6c 52 65 61 64 2c 20 20    memjrnlRead,  
1fc45 20 20 20 20 2f 2a 20 78 52 65 61 64 20 2a 2f 0a      /* xRead */.
1fc46 20 20 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 2c 20    memjrnlWrite, 
1fc47 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20 2a 2f      /* xWrite */
1fc48 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61  .  memjrnlTrunca
1fc49 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74  te,  /* xTruncat
1fc4a 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 53 79  e */.  memjrnlSy
1fc4b 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 53 79 6e  nc,      /* xSyn
1fc4c 63 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 46 69  c */.  memjrnlFi
1fc4d 6c 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c  leSize,  /* xFil
1fc4e 65 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20  eSize */.  0,   
1fc4f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fc50 78 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20  xLock */.  0,   
1fc51 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fc52 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20  xUnlock */.  0, 
1fc53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1fc54 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  * xCheckReserved
1fc55 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Lock */.  0,    
1fc56 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
1fc57 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20  FileControl */. 
1fc58 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1fc59 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a     /* xSectorSiz
1fc5a 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20  e */.  0        
1fc5b 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 76           /* xDev
1fc5c 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
1fc5d 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a  cs */.};../* .**
1fc5e 20 4f 70 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20   Open a journal 
1fc5f 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  file..*/.SQLITE_
1fc60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1fc61 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70  ite3MemJournalOp
1fc62 65 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  en(sqlite3_file 
1fc63 2a 70 4a 66 64 29 7b 0a 20 20 4d 65 6d 4a 6f 75  *pJfd){.  MemJou
1fc64 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f  rnal *p = (MemJo
1fc65 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20  urnal *)pJfd;.  
1fc66 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
1fc67 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 29 20  TE_ALIGNMENT(p) 
1fc68 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  );.  memset(p, 0
1fc69 2c 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72  , sqlite3MemJour
1fc6a 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 70 2d  nalSize());.  p-
1fc6b 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4d 65 6d 4a  >pMethod = &MemJ
1fc6c 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d  ournalMethods;.}
1fc6d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1fc6e 72 75 65 20 69 66 20 74 68 65 20 66 69 6c 65 2d  rue if the file-
1fc6f 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61 73  handle passed as
1fc70 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20   an argument is 
1fc71 0a 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  .** an in-memory
1fc72 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c   journal .*/.SQL
1fc73 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1fc74 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72  sqlite3IsMemJour
1fc75 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  nal(sqlite3_file
1fc76 20 2a 70 4a 66 64 29 7b 0a 20 20 72 65 74 75 72   *pJfd){.  retur
1fc77 6e 20 70 4a 66 64 2d 3e 70 4d 65 74 68 6f 64 73  n pJfd->pMethods
1fc78 3d 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74  ==&MemJournalMet
1fc79 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  hods;.}../* .** 
1fc7a 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1fc7b 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69  r of bytes requi
1fc7c 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 4d  red to store a M
1fc7d 65 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 74 20 75  emJournal that u
1fc7e 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20  ses vfs.** pVfs 
1fc7f 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 75 6e  to create the un
1fc80 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b  derlying on-disk
1fc81 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   files..*/.SQLIT
1fc82 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1fc83 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53  lite3MemJournalS
1fc84 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74  ize(void){.  ret
1fc85 75 72 6e 20 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f  urn sizeof(MemJo
1fc86 75 72 6e 61 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  urnal);.}../****
1fc87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1fc88 66 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a  f memjournal.c *
1fc89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1fc8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
1fc8d 20 66 69 6c 65 20 77 61 6c 6b 65 72 2e 63 20 2a   file walker.c *
1fc8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fc90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
1fc91 20 32 30 30 38 20 41 75 67 75 73 74 20 31 36 0a   2008 August 16.
1fc92 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1fc93 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1fc94 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1fc95 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1fc96 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1fc97 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1fc98 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1fc99 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1fc9a 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1fc9b 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1fc9c 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1fc9d 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1fc9e 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1fc9f 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1fca0 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1fca1 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
1fca2 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
1fca3 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1fca4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fca5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fca6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fca7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1fca8 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
1fca9 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73  ains routines us
1fcaa 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20 74  ed for walking t
1fcab 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20 66  he parser tree f
1fcac 6f 72 0a 2a 2a 20 61 6e 20 53 51 4c 20 73 74 61  or.** an SQL sta
1fcad 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a  tement..*/.../*.
1fcae 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65  ** Walk an expre
1fcaf 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 49 6e 76  ssion tree.  Inv
1fcb0 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  oke the callback
1fcb1 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 6e   once for each n
1fcb2 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 65 20 65 78  ode.** of the ex
1fcb3 70 72 65 73 73 69 6f 6e 2c 20 77 68 69 6c 65 20  pression, while 
1fcb4 64 65 63 65 6e 64 69 6e 67 2e 20 20 28 49 6e 20  decending.  (In 
1fcb5 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65  other words, the
1fcb6 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73 20   callback.** is 
1fcb7 69 6e 76 6f 6b 65 64 20 62 65 66 6f 72 65 20 76  invoked before v
1fcb8 69 73 69 74 69 6e 67 20 63 68 69 6c 64 72 65 6e  isiting children
1fcb9 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  .).**.** The ret
1fcba 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74  urn value from t
1fcbb 68 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75  he callback shou
1fcbc 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65  ld be one of the
1fcbd 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61   WRC_*.** consta
1fcbe 6e 74 73 20 74 6f 20 73 70 65 63 69 66 79 20 68  nts to specify h
1fcbf 6f 77 20 74 6f 20 70 72 6f 63 65 65 64 20 77 69  ow to proceed wi
1fcc0 74 68 20 74 68 65 20 77 61 6c 6b 2e 0a 2a 2a 0a  th the walk..**.
1fcc1 2a 2a 20 20 20 20 57 52 43 5f 43 6f 6e 74 69 6e  **    WRC_Contin
1fcc2 75 65 20 20 20 20 20 20 43 6f 6e 74 69 6e 75 65  ue      Continue
1fcc3 20 64 65 73 63 65 6e 64 69 6e 67 20 64 6f 77 6e   descending down
1fcc4 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a   the tree..**.**
1fcc5 20 20 20 20 57 52 43 5f 50 72 75 6e 65 20 20 20      WRC_Prune   
1fcc6 20 20 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 73        Do not des
1fcc7 63 65 6e 64 20 69 6e 74 6f 20 63 68 69 6c 64 20  cend into child 
1fcc8 6e 6f 64 65 73 2e 20 20 42 75 74 20 61 6c 6c 6f  nodes.  But allo
1fcc9 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  w.**            
1fcca 20 20 20 20 20 20 20 20 20 20 74 68 65 20 77 61            the wa
1fccb 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77  lk to continue w
1fccc 69 74 68 20 73 69 62 6c 69 6e 67 20 6e 6f 64 65  ith sibling node
1fccd 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f  s..**.**    WRC_
1fcce 41 62 6f 72 74 20 20 20 20 20 20 20 20 20 44 6f  Abort         Do
1fccf 20 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c 62 61 63   no more callbac
1fcd0 6b 73 2e 20 20 55 6e 77 69 6e 64 20 74 68 65 20  ks.  Unwind the 
1fcd1 73 74 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20 20  stack and.**    
1fcd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcd3 20 20 72 65 74 75 72 6e 20 74 68 65 20 74 6f 70    return the top
1fcd4 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 63 61 6c 6c  -level walk call
1fcd5 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
1fcd6 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68  rn value from th
1fcd7 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 57 52  is routine is WR
1fcd8 43 5f 41 62 6f 72 74 20 74 6f 20 61 62 61 6e 64  C_Abort to aband
1fcd9 6f 6e 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b  on the tree walk
1fcda 0a 2a 2a 20 61 6e 64 20 57 52 43 5f 43 6f 6e 74  .** and WRC_Cont
1fcdb 69 6e 75 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65  inue to continue
1fcdc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1fcdd 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
1fcde 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 20 2a  alkExpr(Walker *
1fcdf 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70  pWalker, Expr *p
1fce0 45 78 70 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Expr){.  int rc;
1fce1 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20  .  if( pExpr==0 
1fce2 29 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e  ) return WRC_Con
1fce3 74 69 6e 75 65 3b 0a 20 20 74 65 73 74 63 61 73  tinue;.  testcas
1fce4 65 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  e( ExprHasProper
1fce5 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b  ty(pExpr, EP_Tok
1fce6 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 74 65 73  enOnly) );.  tes
1fce7 74 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72  tcase( ExprHasPr
1fce8 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1fce9 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 72  _Reduced) );.  r
1fcea 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78  c = pWalker->xEx
1fceb 70 72 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b  prCallback(pWalk
1fcec 65 72 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66  er, pExpr);.  if
1fced 28 20 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e  ( rc==WRC_Contin
1fcee 75 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ue.             
1fcef 20 26 26 20 21 45 78 70 72 48 61 73 41 6e 79 50   && !ExprHasAnyP
1fcf0 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50  roperty(pExpr,EP
1fcf1 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20  _TokenOnly) ){. 
1fcf2 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61     if( sqlite3Wa
1fcf3 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20  lkExpr(pWalker, 
1fcf4 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 20 29 20  pExpr->pLeft) ) 
1fcf5 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1fcf6 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1fcf7 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
1fcf8 72 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  r, pExpr->pRight
1fcf9 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1fcfa 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 45 78  bort;.    if( Ex
1fcfb 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
1fcfc 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
1fcfd 74 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  t) ){.      if( 
1fcfe 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
1fcff 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72  t(pWalker, pExpr
1fd00 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20 29 20 72  ->x.pSelect) ) r
1fd01 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1fd02 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1fd03 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c    if( sqlite3Wal
1fd04 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65  kExprList(pWalke
1fd05 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  r, pExpr->x.pLis
1fd06 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  t) ) return WRC_
1fd07 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  Abort;.    }.  }
1fd08 0a 20 20 72 65 74 75 72 6e 20 72 63 20 26 20 57  .  return rc & W
1fd09 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 0a  RC_Abort;.}../*.
1fd0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57  ** Call sqlite3W
1fd0b 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76  alkExpr() for ev
1fd0c 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ery expression i
1fd0d 6e 20 6c 69 73 74 20 70 20 6f 72 20 75 6e 74 69  n list p or unti
1fd0e 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74 20 72 65  l.** an abort re
1fd0f 71 75 65 73 74 20 69 73 20 73 65 65 6e 2e 0a 2a  quest is seen..*
1fd10 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1fd11 20 69 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b   int sqlite3Walk
1fd12 45 78 70 72 4c 69 73 74 28 57 61 6c 6b 65 72 20  ExprList(Walker 
1fd13 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 4c 69  *pWalker, ExprLi
1fd14 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b  st *p){.  int i;
1fd15 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
1fd16 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
1fd17 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 66    if( p ){.    f
1fd18 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72 2c 20 70  or(i=p->nExpr, p
1fd19 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20  Item=p->a; i>0; 
1fd1a 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i--, pItem++){. 
1fd1b 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1fd1c 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72  WalkExpr(pWalker
1fd1d 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 20  , pItem->pExpr) 
1fd1e 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1fd1f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rt;.    }.  }.  
1fd20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
1fd21 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61  nue;.}../*.** Wa
1fd22 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f  lk all expressio
1fd23 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ns associated wi
1fd24 74 68 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  th SELECT statem
1fd25 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e 6f  ent p.  Do.** no
1fd26 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45 4c  t invoke the SEL
1fd27 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20  ECT callback on 
1fd28 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63 6f  p, but do (of co
1fd29 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a 20  urse) invoke.** 
1fd2a 61 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61 63  any expr callbac
1fd2b 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63 61  ks and SELECT ca
1fd2c 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f 6d  llbacks that com
1fd2d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69 65  e from subquerie
1fd2e 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43  s..** Return WRC
1fd2f 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43 6f  _Abort or WRC_Co
1fd30 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ntinue..*/.SQLIT
1fd31 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1fd32 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45  lite3WalkSelectE
1fd33 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  xpr(Walker *pWal
1fd34 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b  ker, Select *p){
1fd35 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61  .  if( sqlite3Wa
1fd36 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b  lkExprList(pWalk
1fd37 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20 29  er, p->pEList) )
1fd38 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1fd39 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  t;.  if( sqlite3
1fd3a 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65 72  WalkExpr(pWalker
1fd3b 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20 72  , p->pWhere) ) r
1fd3c 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b  eturn WRC_Abort;
1fd3d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61  .  if( sqlite3Wa
1fd3e 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c 6b  lkExprList(pWalk
1fd3f 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 29  er, p->pGroupBy)
1fd40 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1fd41 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ort;.  if( sqlit
1fd42 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b  e3WalkExpr(pWalk
1fd43 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 20  er, p->pHaving) 
1fd44 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1fd45 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rt;.  if( sqlite
1fd46 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57  3WalkExprList(pW
1fd47 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65 72  alker, p->pOrder
1fd48 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  By) ) return WRC
1fd49 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71  _Abort;.  if( sq
1fd4a 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57  lite3WalkExpr(pW
1fd4b 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69 74  alker, p->pLimit
1fd4c 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1fd4d 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  bort;.  if( sqli
1fd4e 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1fd4f 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29  ker, p->pOffset)
1fd50 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1fd51 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57 52  ort;.  return WR
1fd52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f  C_Continue;.}../
1fd53 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70 61  *.** Walk the pa
1fd54 72 73 65 20 74 72 65 65 73 20 61 73 73 6f 63 69  rse trees associ
1fd55 61 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73 75  ated with all su
1fd56 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65 0a  bqueries in the.
1fd57 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  ** FROM clause o
1fd58 66 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  f SELECT stateme
1fd59 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69 6e  nt p.  Do not in
1fd5a 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74 0a  voke the select.
1fd5b 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20 70  ** callback on p
1fd5c 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65 20  , but do invoke 
1fd5d 69 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d 20  it on each FROM 
1fd5e 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79 0a  clause subquery.
1fd5f 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73 75  ** and on any su
1fd60 62 71 75 65 72 69 65 73 20 66 75 72 74 68 65 72  bqueries further
1fd61 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72 65   down in the tre
1fd62 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20 57  e.  Return .** W
1fd63 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f  RC_Abort or WRC_
1fd64 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51 4c  Continue;.*/.SQL
1fd65 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1fd66 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
1fd67 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70 57  tFrom(Walker *pW
1fd68 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70  alker, Select *p
1fd69 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  ){.  SrcList *pS
1fd6a 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  rc;.  int i;.  s
1fd6b 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
1fd6c 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70 53  em *pItem;..  pS
1fd6d 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20  rc = p->pSrc;.  
1fd6e 69 66 28 20 41 4c 57 41 59 53 28 70 53 72 63 29  if( ALWAYS(pSrc)
1fd6f 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53   ){.    for(i=pS
1fd70 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d  rc->nSrc, pItem=
1fd71 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d  pSrc->a; i>0; i-
1fd72 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  -, pItem++){.   
1fd73 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61     if( sqlite3Wa
1fd74 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72  lkSelect(pWalker
1fd75 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74  , pItem->pSelect
1fd76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  ) ){.        ret
1fd77 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1fd78 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1fd79 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f  .  return WRC_Co
1fd7a 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f 2a 0a 2a  ntinue;.} ../*.*
1fd7b 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 57 61  * Call sqlite3Wa
1fd7c 6c 6b 45 78 70 72 28 29 20 66 6f 72 20 65 76 65  lkExpr() for eve
1fd7d 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ry expression in
1fd7e 20 53 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e   Select statemen
1fd7f 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b 65 20 73  t p..** Invoke s
1fd80 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
1fd81 28 29 20 66 6f 72 20 73 75 62 71 75 65 72 69 65  () for subquerie
1fd82 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
1fd83 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f 6e 20 74  ause and.** on t
1fd84 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  he compound sele
1fd85 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e 70 50 72  ct chain, p->pPr
1fd86 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ior..**.** Retur
1fd87 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 75  n WRC_Continue u
1fd88 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63 6f 6e 64  nder normal cond
1fd89 69 74 69 6f 6e 73 2e 20 20 52 65 74 75 72 6e 20  itions.  Return 
1fd8a 57 52 43 5f 41 62 6f 72 74 20 69 66 0a 2a 2a 20  WRC_Abort if.** 
1fd8b 74 68 65 72 65 20 69 73 20 61 6e 20 61 62 6f 72  there is an abor
1fd8c 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a 0a 2a 2a  t request..**.**
1fd8d 20 49 66 20 74 68 65 20 57 61 6c 6b 65 72 20 64   If the Walker d
1fd8e 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 6e 20  oes not have an 
1fd8f 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28  xSelectCallback(
1fd90 29 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  ) then this rout
1fd91 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f  ine.** is a no-o
1fd92 70 20 72 65 74 75 72 6e 69 6e 67 20 57 52 43 5f  p returning WRC_
1fd93 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c  Continue..*/.SQL
1fd94 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1fd95 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
1fd96 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  t(Walker *pWalke
1fd97 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  r, Select *p){. 
1fd98 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70   int rc;.  if( p
1fd99 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65 72 2d 3e  ==0 || pWalker->
1fd9a 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 3d  xSelectCallback=
1fd9b 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  =0 ) return WRC_
1fd9c 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20 3d  Continue;.  rc =
1fd9d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20   WRC_Continue;. 
1fd9e 20 77 68 69 6c 65 28 20 70 20 20 29 7b 0a 20 20   while( p  ){.  
1fd9f 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e    rc = pWalker->
1fda0 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 28  xSelectCallback(
1fda1 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a 20 20 20  pWalker, p);.   
1fda2 20 69 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b   if( rc ) break;
1fda3 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1fda4 57 61 6c 6b 53 65 6c 65 63 74 45 78 70 72 28 70  WalkSelectExpr(p
1fda5 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74  Walker, p) ) ret
1fda6 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1fda7 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 57 61     if( sqlite3Wa
1fda8 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28 70 57 61  lkSelectFrom(pWa
1fda9 6c 6b 65 72 2c 20 70 29 20 29 20 72 65 74 75 72  lker, p) ) retur
1fdaa 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
1fdab 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a   p = p->pPrior;.
1fdac 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20    }.  return rc 
1fdad 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a  & WRC_Abort;.}..
1fdae 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1fdaf 45 6e 64 20 6f 66 20 77 61 6c 6b 65 72 2e 63 20  End of walker.c 
1fdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fdb1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fdb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1fdb3 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1fdb4 42 65 67 69 6e 20 66 69 6c 65 20 72 65 73 6f 6c  Begin file resol
1fdb5 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve.c ***********
1fdb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fdb7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1fdb8 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67 75 73  /*.** 2008 Augus
1fdb9 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 18.**.** The a
1fdba 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
1fdbb 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
1fdbc 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
1fdbd 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
1fdbe 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
1fdbf 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
1fdc0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
1fdc1 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
1fdc2 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
1fdc3 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
1fdc4 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
1fdc5 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
1fdc6 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
1fdc7 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
1fdc8 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
1fdc9 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
1fdca 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
1fdcb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fdcc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fdcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fdce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fdcf 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
1fdd0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75  ile contains rou
1fdd1 74 69 6e 65 73 20 75 73 65 64 20 66 6f 72 20 77  tines used for w
1fdd2 61 6c 6b 69 6e 67 20 74 68 65 20 70 61 72 73 65  alking the parse
1fdd3 72 20 74 72 65 65 20 61 6e 64 0a 2a 2a 20 72 65  r tree and.** re
1fdd4 73 6f 6c 76 65 20 61 6c 6c 20 69 64 65 6e 74 69  solve all identi
1fdd5 66 69 65 72 73 20 62 79 20 61 73 73 6f 63 69 61  fiers by associa
1fdd6 74 69 6e 67 20 74 68 65 6d 20 77 69 74 68 20 61  ting them with a
1fdd7 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 74   particular.** t
1fdd8 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e  able and column.
1fdd9 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20  .*/../*.** Turn 
1fdda 74 68 65 20 70 45 78 70 72 20 65 78 70 72 65 73  the pExpr expres
1fddb 73 69 6f 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69  sion into an ali
1fddc 61 73 20 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d  as for the iCol-
1fddd 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
1fdde 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74 20 69  .** result set i
1fddf 6e 20 70 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  n pEList..**.** 
1fde0 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  If the result se
1fde1 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69  t column is a si
1fde2 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65  mple column refe
1fde3 72 65 6e 63 65 2c 20 74 68 65 6e 20 74 68 69 73  rence, then this
1fde4 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65   routine.** make
1fde5 73 20 61 6e 20 65 78 61 63 74 20 63 6f 70 79 2e  s an exact copy.
1fde6 20 20 42 75 74 20 66 6f 72 20 61 6e 79 20 6f 74    But for any ot
1fde7 68 65 72 20 6b 69 6e 64 20 6f 66 20 65 78 70 72  her kind of expr
1fde8 65 73 73 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20  ession, this.** 
1fde9 72 6f 75 74 69 6e 65 20 6d 61 6b 65 20 61 20 63  routine make a c
1fdea 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73 75 6c  opy of the resul
1fdeb 74 20 73 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20  t set column as 
1fdec 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  the argument to 
1fded 74 68 65 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65  the.** TK_AS ope
1fdee 72 61 74 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41  rator.  The TK_A
1fdef 53 20 6f 70 65 72 61 74 6f 72 20 63 61 75 73 65  S operator cause
1fdf0 73 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  s the expression
1fdf1 20 74 6f 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61   to be.** evalua
1fdf2 74 65 64 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e  ted just once an
1fdf3 64 20 74 68 65 6e 20 72 65 75 73 65 64 20 66 6f  d then reused fo
1fdf4 72 20 65 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a  r each alias..**
1fdf5 0a 2a 2a 20 54 68 65 20 72 65 61 73 6f 6e 20 66  .** The reason f
1fdf6 6f 72 20 73 75 70 70 72 65 73 73 69 6e 67 20 74  or suppressing t
1fdf7 68 65 20 54 4b 5f 41 53 20 74 65 72 6d 20 77 68  he TK_AS term wh
1fdf8 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  en the expressio
1fdf9 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a  n is a simple.**
1fdfa 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63   column referenc
1fdfb 65 20 69 73 20 73 6f 20 74 68 61 74 20 74 68 65  e is so that the
1fdfc 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63   column referenc
1fdfd 65 20 77 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e  e will be recogn
1fdfe 69 7a 65 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c  ized as.** usabl
1fdff 65 20 62 79 20 69 6e 64 69 63 65 73 20 77 69 74  e by indices wit
1fe00 68 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c  hin the WHERE cl
1fe01 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67 20  ause processing 
1fe02 6c 6f 67 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61  logic. .**.** Ha
1fe03 63 6b 3a 20 20 54 68 65 20 54 4b 5f 41 53 20 6f  ck:  The TK_AS o
1fe04 70 65 72 61 74 6f 72 20 69 73 20 69 6e 68 69 62  perator is inhib
1fe05 69 74 65 64 20 69 66 20 7a 54 79 70 65 5b 30 5d  ited if zType[0]
1fe06 3d 3d 27 47 27 2e 20 20 54 68 69 73 20 6d 65 61  =='G'.  This mea
1fe07 6e 73 0a 2a 2a 20 74 68 61 74 20 69 6e 20 61 20  ns.** that in a 
1fe08 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2c  GROUP BY clause,
1fe09 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1fe0a 69 73 20 65 76 61 6c 75 61 74 65 64 20 74 77 69  is evaluated twi
1fe0b 63 65 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a  ce.  Hence:.**.*
1fe0c 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72 61 6e  *     SELECT ran
1fe0d 64 6f 6d 28 29 25 35 20 41 53 20 78 2c 20 63 6f  dom()%5 AS x, co
1fe0e 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61 62 20  unt(*) FROM tab 
1fe0f 47 52 4f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a  GROUP BY x.**.**
1fe10 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74   Is equivalent t
1fe11 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c  o:.**.**     SEL
1fe12 45 43 54 20 72 61 6e 64 6f 6d 28 29 25 35 20 41  ECT random()%5 A
1fe13 53 20 78 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52  S x, count(*) FR
1fe14 4f 4d 20 74 61 62 20 47 52 4f 55 50 20 42 59 20  OM tab GROUP BY 
1fe15 72 61 6e 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a  random()%5.**.**
1fe16 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 72   The result of r
1fe17 61 6e 64 6f 6d 28 29 25 35 20 69 6e 20 74 68 65  andom()%5 in the
1fe18 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
1fe19 20 69 73 20 70 72 6f 62 61 62 6c 79 20 64 69 66   is probably dif
1fe1a 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74  ferent.** from t
1fe1b 68 65 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65  he result in the
1fe1c 20 72 65 73 75 6c 74 2d 73 65 74 2e 20 20 57 65   result-set.  We
1fe1d 20 6d 69 67 68 74 20 66 69 78 20 74 68 69 73 20   might fix this 
1fe1e 73 6f 6d 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20  someday.  Or.** 
1fe1f 74 68 65 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d  then again, we m
1fe20 69 67 68 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73  ight not....*/.s
1fe21 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c  tatic void resol
1fe22 76 65 41 6c 69 61 73 28 0a 20 20 50 61 72 73 65  veAlias(.  Parse
1fe23 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
1fe24 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
1fe25 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
1fe26 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20  st *pEList,     
1fe27 20 2f 2a 20 41 20 72 65 73 75 6c 74 20 73 65 74   /* A result set
1fe28 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20   */.  int iCol, 
1fe29 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1fe2a 41 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  A column in the 
1fe2b 72 65 73 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e  result set.  0..
1fe2c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20  pEList->nExpr-1 
1fe2d 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
1fe2e 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
1fe2f 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 20 69 6e  ransform this in
1fe30 74 6f 20 61 6e 20 61 6c 69 61 73 20 74 6f 20 74  to an alias to t
1fe31 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
1fe32 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1fe33 54 79 70 65 20 20 20 20 20 20 2f 2a 20 22 47 52  Type      /* "GR
1fe34 4f 55 50 22 20 6f 72 20 22 4f 52 44 45 52 22 20  OUP" or "ORDER" 
1fe35 6f 72 20 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78  or "" */.){.  Ex
1fe36 70 72 20 2a 70 4f 72 69 67 3b 20 20 20 20 20 20  pr *pOrig;      
1fe37 20 20 20 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c       /* The iCol
1fe38 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  -th column of th
1fe39 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  e result set */.
1fe3a 20 20 45 78 70 72 20 2a 70 44 75 70 3b 20 20 20    Expr *pDup;   
1fe3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
1fe3c 20 6f 66 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73   of pOrig */.  s
1fe3d 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
1fe3e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
1fe3f 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1fe40 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69   */..  assert( i
1fe41 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70  Col>=0 && iCol<p
1fe42 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a  EList->nExpr );.
1fe43 20 20 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74    pOrig = pEList
1fe44 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b  ->a[iCol].pExpr;
1fe45 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 69 67  .  assert( pOrig
1fe46 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1fe47 20 70 4f 72 69 67 2d 3e 66 6c 61 67 73 20 26 20   pOrig->flags & 
1fe48 45 50 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20  EP_Resolved );. 
1fe49 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
1fe4a 3b 0a 20 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f  ;.  if( pOrig->o
1fe4b 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20  p!=TK_COLUMN && 
1fe4c 7a 54 79 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b  zType[0]!='G' ){
1fe4d 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69  .    pDup = sqli
1fe4e 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
1fe4f 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 44  Orig, 0);.    pD
1fe50 75 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  up = sqlite3PExp
1fe51 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c  r(pParse, TK_AS,
1fe52 20 70 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20   pDup, 0, 0);.  
1fe53 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 29 20    if( pDup==0 ) 
1fe54 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20  return;.    if( 
1fe55 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e  pEList->a[iCol].
1fe56 69 41 6c 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20  iAlias==0 ){.   
1fe57 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f     pEList->a[iCo
1fe58 6c 5d 2e 69 41 6c 69 61 73 20 3d 20 28 75 31 36  l].iAlias = (u16
1fe59 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69  )(++pParse->nAli
1fe5a 61 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  as);.    }.    p
1fe5b 44 75 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45  Dup->iTable = pE
1fe5c 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41  List->a[iCol].iA
1fe5d 6c 69 61 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66  lias;.  }else if
1fe5e 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
1fe5f 79 28 70 4f 72 69 67 2c 20 45 50 5f 49 6e 74 56  y(pOrig, EP_IntV
1fe60 61 6c 75 65 29 20 7c 7c 20 70 4f 72 69 67 2d 3e  alue) || pOrig->
1fe61 75 2e 7a 54 6f 6b 65 6e 3d 3d 30 20 29 7b 0a 20  u.zToken==0 ){. 
1fe62 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
1fe63 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72  3ExprDup(db, pOr
1fe64 69 67 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ig, 0);.    if( 
1fe65 70 44 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  pDup==0 ) return
1fe66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  ;.  }else{.    c
1fe67 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4f  har *zToken = pO
1fe68 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20  rig->u.zToken;. 
1fe69 20 20 20 61 73 73 65 72 74 28 20 7a 54 6f 6b 65     assert( zToke
1fe6a 6e 21 3d 30 20 29 3b 0a 20 20 20 20 70 4f 72 69  n!=0 );.    pOri
1fe6b 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 30 3b  g->u.zToken = 0;
1fe6c 0a 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69  .    pDup = sqli
1fe6d 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
1fe6e 4f 72 69 67 2c 20 30 29 3b 0a 20 20 20 20 70 4f  Orig, 0);.    pO
1fe6f 72 69 67 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20  rig->u.zToken = 
1fe70 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20  zToken;.    if( 
1fe71 70 44 75 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  pDup==0 ) return
1fe72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
1fe73 44 75 70 2d 3e 66 6c 61 67 73 20 26 20 28 45 50  Dup->flags & (EP
1fe74 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65  _Reduced|EP_Toke
1fe75 6e 4f 6e 6c 79 29 29 3d 3d 30 20 29 3b 0a 20 20  nOnly))==0 );.  
1fe76 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c    pDup->flags2 |
1fe77 3d 20 45 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f  = EP2_MallocedTo
1fe78 6b 65 6e 3b 0a 20 20 20 20 70 44 75 70 2d 3e 75  ken;.    pDup->u
1fe79 2e 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65  .zToken = sqlite
1fe7a 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 54  3DbStrDup(db, zT
1fe7b 6f 6b 65 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28  oken);.  }.  if(
1fe7c 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20   pExpr->flags & 
1fe7d 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b  EP_ExpCollate ){
1fe7e 0a 20 20 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c  .    pDup->pColl
1fe7f 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b   = pExpr->pColl;
1fe80 0a 20 20 20 20 70 44 75 70 2d 3e 66 6c 61 67 73  .    pDup->flags
1fe81 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74   |= EP_ExpCollat
1fe82 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 66  e;.  }..  /* Bef
1fe83 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69  ore calling sqli
1fe84 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 29 2c  te3ExprDelete(),
1fe85 20 73 65 74 20 74 68 65 20 45 50 5f 53 74 61 74   set the EP_Stat
1fe86 69 63 20 66 6c 61 67 2e 20 54 68 69 73 20 0a 20  ic flag. This . 
1fe87 20 2a 2a 20 70 72 65 76 65 6e 74 73 20 45 78 70   ** prevents Exp
1fe88 72 44 65 6c 65 74 65 28 29 20 66 72 6f 6d 20 64  rDelete() from d
1fe89 65 6c 65 74 69 6e 67 20 74 68 65 20 45 78 70 72  eleting the Expr
1fe8a 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c   structure itsel
1fe8b 66 2c 0a 20 20 2a 2a 20 61 6c 6c 6f 77 69 6e 67  f,.  ** allowing
1fe8c 20 69 74 20 74 6f 20 62 65 20 72 65 70 6f 70 75   it to be repopu
1fe8d 6c 61 74 65 64 20 62 79 20 74 68 65 20 6d 65 6d  lated by the mem
1fe8e 63 70 79 28 29 20 6f 6e 20 74 68 65 20 66 6f 6c  cpy() on the fol
1fe8f 6c 6f 77 69 6e 67 20 6c 69 6e 65 2e 0a 20 20 2a  lowing line..  *
1fe90 2f 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 65  /.  ExprSetPrope
1fe91 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 53 74  rty(pExpr, EP_St
1fe92 61 74 69 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  atic);.  sqlite3
1fe93 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1fe94 45 78 70 72 29 3b 0a 20 20 6d 65 6d 63 70 79 28  Expr);.  memcpy(
1fe95 70 45 78 70 72 2c 20 70 44 75 70 2c 20 73 69 7a  pExpr, pDup, siz
1fe96 65 6f 66 28 2a 70 45 78 70 72 29 29 3b 0a 20 20  eof(*pExpr));.  
1fe97 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1fe98 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , pDup);.}../*.*
1fe99 2a 20 47 69 76 65 6e 20 74 68 65 20 6e 61 6d 65  * Given the name
1fe9a 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20   of a column of 
1fe9b 74 68 65 20 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f  the form X.Y.Z o
1fe9c 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74 20 5a 2c  r Y.Z or just Z,
1fe9d 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20 74 68 61 74   look up.** that
1fe9e 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73 65 74   name in the set
1fe9f 20 6f 66 20 73 6f 75 72 63 65 20 74 61 62 6c 65   of source table
1fea0 73 20 69 6e 20 70 53 72 63 4c 69 73 74 20 61 6e  s in pSrcList an
1fea1 64 20 6d 61 6b 65 20 74 68 65 20 70 45 78 70 72  d make the pExpr
1fea2 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20   .** expression 
1fea3 6e 6f 64 65 20 72 65 66 65 72 20 62 61 63 6b 20  node refer back 
1fea4 74 6f 20 74 68 61 74 20 73 6f 75 72 63 65 20 63  to that source c
1fea5 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 66 6f 6c 6c  olumn.  The foll
1fea6 6f 77 69 6e 67 20 63 68 61 6e 67 65 73 0a 2a 2a  owing changes.**
1fea7 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 45 78   are made to pEx
1fea8 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70 45 78  pr:.**.**    pEx
1fea9 70 72 2d 3e 69 44 62 20 20 20 20 20 20 20 20 20  pr->iDb         
1feaa 20 20 53 65 74 20 74 68 65 20 69 6e 64 65 78 20    Set the index 
1feab 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 6f 66 20  in db->aDb[] of 
1feac 74 68 65 20 64 61 74 61 62 61 73 65 20 58 0a 2a  the database X.*
1fead 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
1feae 20 20 20 20 20 20 20 20 20 20 28 65 76 65 6e 20            (even 
1feaf 69 66 20 58 20 69 73 20 69 6d 70 6c 69 65 64 29  if X is implied)
1feb0 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69  ..**    pExpr->i
1feb1 54 61 62 6c 65 20 20 20 20 20 20 20 20 53 65 74  Table        Set
1feb2 20 74 6f 20 74 68 65 20 63 75 72 73 6f 72 20 6e   to the cursor n
1feb3 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61  umber for the ta
1feb4 62 6c 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20  ble obtained.** 
1feb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1feb6 20 20 20 20 20 20 20 20 66 72 6f 6d 20 70 53 72          from pSr
1feb7 63 4c 69 73 74 2e 0a 2a 2a 20 20 20 20 70 45 78  cList..**    pEx
1feb8 70 72 2d 3e 70 54 61 62 20 20 20 20 20 20 20 20  pr->pTab        
1feb9 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 68 65 20    Points to the 
1feba 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
1febb 6f 66 20 58 2e 59 20 28 65 76 65 6e 20 69 66 0a  of X.Y (even if.
1febc 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1febd 20 20 20 20 20 20 20 20 20 20 20 58 20 61 6e 64             X and
1febe 2f 6f 72 20 59 20 61 72 65 20 69 6d 70 6c 69 65  /or Y are implie
1febf 64 2e 29 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d  d.).**    pExpr-
1fec0 3e 69 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 53  >iColumn       S
1fec1 65 74 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e  et to the column
1fec2 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e 20 74   number within t
1fec3 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20  he table..**    
1fec4 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 20 20 20  pExpr->op       
1fec5 20 20 20 20 20 53 65 74 20 74 6f 20 54 4b 5f 43       Set to TK_C
1fec6 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20 20 70 45 78  OLUMN..**    pEx
1fec7 70 72 2d 3e 70 4c 65 66 74 20 20 20 20 20 20 20  pr->pLeft       
1fec8 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e    Any expression
1fec9 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f 20   this points to 
1feca 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a 20 20 20  is deleted.**   
1fecb 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 20   pExpr->pRight  
1fecc 20 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73        Any expres
1fecd 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73  sion this points
1fece 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64 2e 0a   to is deleted..
1fecf 2a 2a 0a 2a 2a 20 54 68 65 20 7a 44 62 20 76 61  **.** The zDb va
1fed0 72 69 61 62 6c 65 20 69 73 20 74 68 65 20 6e 61  riable is the na
1fed1 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1fed2 73 65 20 28 74 68 65 20 22 58 22 29 2e 20 20 54  se (the "X").  T
1fed3 68 69 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65  his value may be
1fed4 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67  .** NULL meaning
1fed5 20 74 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66   that name is of
1fed6 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72   the form Y.Z or
1fed7 20 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62   Z.  Any availab
1fed8 6c 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  le database.** c
1fed9 61 6e 20 62 65 20 75 73 65 64 2e 20 20 54 68 65  an be used.  The
1feda 20 7a 54 61 62 6c 65 20 76 61 72 69 61 62 6c 65   zTable variable
1fedb 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
1fedc 74 68 65 20 74 61 62 6c 65 20 28 74 68 65 20 22  the table (the "
1fedd 59 22 29 2e 20 20 54 68 69 73 0a 2a 2a 20 76 61  Y").  This.** va
1fede 6c 75 65 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20  lue can be NULL 
1fedf 69 66 20 7a 44 62 20 69 73 20 61 6c 73 6f 20 4e  if zDb is also N
1fee0 55 4c 4c 2e 20 20 49 66 20 7a 54 61 62 6c 65 20  ULL.  If zTable 
1fee1 69 73 20 4e 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65  is NULL it.** me
1fee2 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 6f 72  ans that the for
1fee3 6d 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73  m of the name is
1fee4 20 5a 20 61 6e 64 20 74 68 61 74 20 63 6f 6c 75   Z and that colu
1fee5 6d 6e 73 20 66 72 6f 6d 20 61 6e 79 20 74 61 62  mns from any tab
1fee6 6c 65 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65  le.** can be use
1fee7 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
1fee8 6e 61 6d 65 20 63 61 6e 6e 6f 74 20 62 65 20 72  name cannot be r
1fee9 65 73 6f 6c 76 65 64 20 75 6e 61 6d 62 69 67 75  esolved unambigu
1feea 6f 75 73 6c 79 2c 20 6c 65 61 76 65 20 61 6e 20  ously, leave an 
1feeb 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a  error message.**
1feec 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72   in pParse and r
1feed 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 2e  eturn WRC_Abort.
1feee 20 20 52 65 74 75 72 6e 20 57 52 43 5f 50 72 75    Return WRC_Pru
1feef 6e 65 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a  ne on success..*
1fef0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f  /.static int loo
1fef1 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65  kupName(.  Parse
1fef2 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
1fef3 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
1fef4 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73  ontext */.  cons
1fef5 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20  t char *zDb,    
1fef6 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1fef7 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
1fef8 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55  ing table, or NU
1fef9 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  LL */.  const ch
1fefa 61 72 20 2a 7a 54 61 62 2c 20 20 20 20 2f 2a 20  ar *zTab,    /* 
1fefb 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f  Name of table co
1fefc 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c  ntaining column,
1fefd 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f   or NULL */.  co
1fefe 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20  nst char *zCol, 
1feff 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
1ff00 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e  e column. */.  N
1ff01 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
1ff02 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20      /* The name 
1ff03 63 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20  context used to 
1ff04 72 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65  resolve the name
1ff05 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
1ff06 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  r          /* Ma
1ff07 6b 65 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64  ke this EXPR nod
1ff08 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73  e point to the s
1ff09 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a  elected column *
1ff0a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  /.){.  int i, j;
1ff0b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1ff0c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
1ff0d 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 20 20    int cnt = 0;  
1ff0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ff0f 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1ff10 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e   matching column
1ff11 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20   names */.  int 
1ff12 63 6e 74 54 61 62 20 3d 20 30 3b 20 20 20 20 20  cntTab = 0;     
1ff13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ff14 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68   Number of match
1ff15 69 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65 73 20  ing table names 
1ff16 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
1ff17 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20   = pParse->db;  
1ff18 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
1ff19 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1ff1a 6e 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  n */.  struct Sr
1ff1b 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  cList_item *pIte
1ff1c 6d 3b 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20  m;       /* Use 
1ff1d 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  for looping over
1ff1e 20 70 53 72 63 4c 69 73 74 20 69 74 65 6d 73 20   pSrcList items 
1ff1f 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
1ff20 69 73 74 5f 69 74 65 6d 20 2a 70 4d 61 74 63 68  ist_item *pMatch
1ff21 20 3d 20 30 3b 20 20 2f 2a 20 54 68 65 20 6d 61   = 0;  /* The ma
1ff22 74 63 68 69 6e 67 20 70 53 72 63 4c 69 73 74 20  tching pSrcList 
1ff23 69 74 65 6d 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  item */.  NameCo
1ff24 6e 74 65 78 74 20 2a 70 54 6f 70 4e 43 20 3d 20  ntext *pTopNC = 
1ff25 70 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20 46  pNC;        /* F
1ff26 69 72 73 74 20 6e 61 6d 65 63 6f 6e 74 65 78 74  irst namecontext
1ff27 20 69 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a   in the list */.
1ff28 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
1ff29 61 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  a = 0;          
1ff2a 20 20 20 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66      /* Schema of
1ff2b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1ff2c 2a 2f 0a 20 20 69 6e 74 20 69 73 54 72 69 67 67  */.  int isTrigg
1ff2d 65 72 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  er = 0;..  asser
1ff2e 74 28 20 70 4e 43 20 29 3b 20 20 20 20 20 2f 2a  t( pNC );     /*
1ff2f 20 74 68 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78   the name contex
1ff30 74 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c  t cannot be NULL
1ff31 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 7a  . */.  assert( z
1ff32 43 6f 6c 20 29 3b 20 20 20 20 2f 2a 20 54 68 65  Col );    /* The
1ff33 20 5a 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e   Z in X.Y.Z cann
1ff34 6f 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ot be NULL */.  
1ff35 61 73 73 65 72 74 28 20 7e 45 78 70 72 48 61 73  assert( ~ExprHas
1ff36 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70  AnyProperty(pExp
1ff37 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c  r, EP_TokenOnly|
1ff38 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 0a  EP_Reduced) );..
1ff39 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
1ff3a 74 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d  the node to no-m
1ff3b 61 74 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d  atch */.  pExpr-
1ff3c 3e 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20  >iTable = -1;.  
1ff3d 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b  pExpr->pTab = 0;
1ff3e 0a 20 20 45 78 70 72 53 65 74 49 72 72 65 64 75  .  ExprSetIrredu
1ff3f 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a 0a 20  cible(pExpr);.. 
1ff40 20 2f 2a 20 53 74 61 72 74 20 61 74 20 74 68 65   /* Start at the
1ff41 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63 6f 6e 74   inner-most cont
1ff42 65 78 74 20 61 6e 64 20 6d 6f 76 65 20 6f 75 74  ext and move out
1ff43 77 61 72 64 20 75 6e 74 69 6c 20 61 20 6d 61 74  ward until a mat
1ff44 63 68 20 69 73 20 66 6f 75 6e 64 20 2a 2f 0a 20  ch is found */. 
1ff45 20 77 68 69 6c 65 28 20 70 4e 43 20 26 26 20 63   while( pNC && c
1ff46 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70  nt==0 ){.    Exp
1ff47 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20  rList *pEList;. 
1ff48 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
1ff49 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63  List = pNC->pSrc
1ff4a 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66 28 20 70  List;..    if( p
1ff4b 53 72 63 4c 69 73 74 20 29 7b 0a 20 20 20 20 20  SrcList ){.     
1ff4c 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d   for(i=0, pItem=
1ff4d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  pSrcList->a; i<p
1ff4e 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  SrcList->nSrc; i
1ff4f 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
1ff50 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
1ff51 62 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  b;.        int i
1ff52 44 62 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c 75  Db;.        Colu
1ff53 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a 20 20 20  mn *pCol;.  .   
1ff54 20 20 20 20 20 70 54 61 62 20 3d 20 70 49 74 65       pTab = pIte
1ff55 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20  m->pTab;.       
1ff56 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d 30   assert( pTab!=0
1ff57 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 21   && pTab->zName!
1ff58 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 44  =0 );.        iD
1ff59 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
1ff5a 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
1ff5b 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
1ff5c 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61       assert( pTa
1ff5d 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a 20 20 20  b->nCol>0 );.   
1ff5e 20 20 20 20 20 69 66 28 20 7a 54 61 62 20 29 7b       if( zTab ){
1ff5f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
1ff60 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a  Item->zAlias ){.
1ff61 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72              char
1ff62 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70 49 74   *zTabName = pIt
1ff63 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20 20  em->zAlias;.    
1ff64 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
1ff65 74 65 33 53 74 72 49 43 6d 70 28 7a 54 61 62 4e  te3StrICmp(zTabN
1ff66 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 29 20  ame, zTab)!=0 ) 
1ff67 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
1ff68 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ff69 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61         char *zTa
1ff6a 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e  bName = pTab->zN
1ff6b 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ame;.           
1ff6c 20 69 66 28 20 4e 45 56 45 52 28 7a 54 61 62 4e   if( NEVER(zTabN
1ff6d 61 6d 65 3d 3d 30 29 20 7c 7c 20 73 71 6c 69 74  ame==0) || sqlit
1ff6e 65 33 53 74 72 49 43 6d 70 28 7a 54 61 62 4e 61  e3StrICmp(zTabNa
1ff6f 6d 65 2c 20 7a 54 61 62 29 21 3d 30 20 29 7b 0a  me, zTab)!=0 ){.
1ff70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
1ff71 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
1ff72 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1ff73 20 20 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20    if( zDb!=0 && 
1ff74 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64  sqlite3StrICmp(d
1ff75 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
1ff76 65 2c 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20  e, zDb)!=0 ){.  
1ff77 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74              cont
1ff78 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20  inue;.          
1ff79 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
1ff7a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1ff7b 20 20 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62    if( 0==(cntTab
1ff7c 2b 2b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ++) ){.         
1ff7d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
1ff7e 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b   pItem->iCursor;
1ff7f 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72  .          pExpr
1ff80 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20  ->pTab = pTab;. 
1ff81 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d 61           pSchema
1ff82 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61   = pTab->pSchema
1ff83 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 61 74  ;.          pMat
1ff84 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20  ch = pItem;.    
1ff85 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f      }.        fo
1ff86 72 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62  r(j=0, pCol=pTab
1ff87 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e  ->aCol; j<pTab->
1ff88 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b  nCol; j++, pCol+
1ff89 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
1ff8a 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1ff8b 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43  (pCol->zName, zC
1ff8c 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ol)==0 ){.      
1ff8d 20 20 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55        IdList *pU
1ff8e 73 69 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20  sing;.          
1ff8f 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20    cnt++;.       
1ff90 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62       pExpr->iTab
1ff91 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72  le = pItem->iCur
1ff92 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20  sor;.           
1ff93 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70   pExpr->pTab = p
1ff94 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Tab;.           
1ff95 20 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b   pMatch = pItem;
1ff96 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53 63  .            pSc
1ff97 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63  hema = pTab->pSc
1ff98 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20  hema;.          
1ff99 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20    /* Substitute 
1ff9a 74 68 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d  the rowid (colum
1ff9b 6e 20 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e  n -1) for the IN
1ff9c 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
1ff9d 59 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  Y */.           
1ff9e 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
1ff9f 3d 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79  = j==pTab->iPKey
1ffa0 20 3f 20 2d 31 20 3a 20 28 69 31 36 29 6a 3b 0a   ? -1 : (i16)j;.
1ffa1 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
1ffa2 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63  i<pSrcList->nSrc
1ffa3 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  -1 ){.          
1ffa4 20 20 20 20 69 66 28 20 70 49 74 65 6d 5b 31 5d      if( pItem[1]
1ffa5 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e  .jointype & JT_N
1ffa6 41 54 55 52 41 4c 20 29 7b 0a 20 20 20 20 20 20  ATURAL ){.      
1ffa7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
1ffa8 74 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75 72  this match occur
1ffa9 72 65 64 20 69 6e 20 74 68 65 20 6c 65 66 74 20  red in the left 
1ffaa 74 61 62 6c 65 20 6f 66 20 61 20 6e 61 74 75 72  table of a natur
1ffab 61 6c 20 6a 6f 69 6e 2c 0a 20 20 20 20 20 20 20  al join,.       
1ffac 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 6e           ** then
1ffad 20 73 6b 69 70 20 74 68 65 20 72 69 67 68 74 20   skip the right 
1ffae 74 61 62 6c 65 20 74 6f 20 61 76 6f 69 64 20 61  table to avoid a
1ffaf 20 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68   duplicate match
1ffb0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
1ffb1 20 20 20 20 70 49 74 65 6d 2b 2b 3b 0a 20 20 20      pItem++;.   
1ffb2 20 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b               i++
1ffb3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1ffb4 7d 65 6c 73 65 20 69 66 28 20 28 70 55 73 69 6e  }else if( (pUsin
1ffb5 67 20 3d 20 70 49 74 65 6d 5b 31 5d 2e 70 55 73  g = pItem[1].pUs
1ffb6 69 6e 67 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ing)!=0 ){.     
1ffb7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66             /* If
1ffb8 20 74 68 69 73 20 6d 61 74 63 68 20 6f 63 63 75   this match occu
1ffb9 72 73 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 74  rs on a column t
1ffba 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 55 53  hat is in the US
1ffbb 49 4e 47 20 63 6c 61 75 73 65 0a 20 20 20 20 20  ING clause.     
1ffbc 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66             ** of
1ffbd 20 61 20 6a 6f 69 6e 2c 20 73 6b 69 70 20 74 68   a join, skip th
1ffbe 65 20 73 65 61 72 63 68 20 6f 66 20 74 68 65 20  e search of the 
1ffbf 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 74  right table of t
1ffc0 68 65 20 6a 6f 69 6e 0a 20 20 20 20 20 20 20 20  he join.        
1ffc1 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 61 76          ** to av
1ffc2 6f 69 64 20 61 20 64 75 70 6c 69 63 61 74 65 20  oid a duplicate 
1ffc3 6d 61 74 63 68 20 74 68 65 72 65 2e 20 2a 2f 0a  match there. */.
1ffc4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ffc5 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 20 20 20  int k;.         
1ffc6 20 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20         for(k=0; 
1ffc7 6b 3c 70 55 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b  k<pUsing->nId; k
1ffc8 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
1ffc9 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
1ffca 65 33 53 74 72 49 43 6d 70 28 70 55 73 69 6e 67  e3StrICmp(pUsing
1ffcb 2d 3e 61 5b 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43  ->a[k].zName, zC
1ffcc 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ol)==0 ){.      
1ffcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49                pI
1ffce 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  tem++;.         
1ffcf 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a             i++;.
1ffd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ffd1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1ffd2 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
1ffd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
1ffd4 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
1ffd5 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
1ffd6 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
1ffd7 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1ffd8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1ffd9 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
1ffda 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
1ffdb 45 52 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20  ER.    /* If we 
1ffdc 68 61 76 65 20 6e 6f 74 20 61 6c 72 65 61 64 79  have not already
1ffdd 20 72 65 73 6f 6c 76 65 64 20 74 68 65 20 6e 61   resolved the na
1ffde 6d 65 2c 20 74 68 65 6e 20 6d 61 79 62 65 20 0a  me, then maybe .
1ffdf 20 20 20 20 2a 2a 20 69 74 20 69 73 20 61 20 6e      ** it is a n
1ffe0 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 74 72  ew.* or old.* tr
1ffe1 69 67 67 65 72 20 61 72 67 75 6d 65 6e 74 20 72  igger argument r
1ffe2 65 66 65 72 65 6e 63 65 0a 20 20 20 20 2a 2f 0a  eference.    */.
1ffe3 20 20 20 20 69 66 28 20 7a 44 62 3d 3d 30 20 26      if( zDb==0 &
1ffe4 26 20 7a 54 61 62 21 3d 30 20 26 26 20 63 6e 74  & zTab!=0 && cnt
1ffe5 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70  ==0 && pParse->p
1ffe6 54 72 69 67 67 65 72 54 61 62 21 3d 30 20 29 7b  TriggerTab!=0 ){
1ffe7 0a 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20  .      int op = 
1ffe8 70 50 61 72 73 65 2d 3e 65 54 72 69 67 67 65 72  pParse->eTrigger
1ffe9 4f 70 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 20  Op;.      Table 
1ffea 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20  *pTab = 0;.     
1ffeb 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b 5f   assert( op==TK_
1ffec 44 45 4c 45 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b  DELETE || op==TK
1ffed 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54  _UPDATE || op==T
1ffee 4b 5f 49 4e 53 45 52 54 20 29 3b 0a 20 20 20 20  K_INSERT );.    
1ffef 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 44 45 4c    if( op!=TK_DEL
1fff0 45 54 45 20 26 26 20 73 71 6c 69 74 65 33 53 74  ETE && sqlite3St
1fff1 72 49 43 6d 70 28 22 6e 65 77 22 2c 7a 54 61 62  rICmp("new",zTab
1fff2 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20 20  ) == 0 ){.      
1fff3 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1fff4 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 54 61  = 1;.        pTa
1fff5 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72 69  b = pParse->pTri
1fff6 67 67 65 72 54 61 62 3b 0a 20 20 20 20 20 20 7d  ggerTab;.      }
1fff7 65 6c 73 65 20 69 66 28 20 6f 70 21 3d 54 4b 5f  else if( op!=TK_
1fff8 49 4e 53 45 52 54 20 26 26 20 73 71 6c 69 74 65  INSERT && sqlite
1fff9 33 53 74 72 49 43 6d 70 28 22 6f 6c 64 22 2c 7a  3StrICmp("old",z
1fffa 54 61 62 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Tab)==0 ){.     
1fffb 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1fffc 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 54   = 0;.        pT
1fffd 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 54 72  ab = pParse->pTr
1fffe 69 67 67 65 72 54 61 62 3b 0a 20 20 20 20 20 20  iggerTab;.      
1ffff 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 61  }..      if( pTa
20000 62 20 29 7b 20 0a 20 20 20 20 20 20 20 20 69 6e  b ){ .        in
20001 74 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  t iCol;.        
20002 70 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e  pSchema = pTab->
20003 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 20 20 20  pSchema;.       
20004 20 63 6e 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20   cntTab++;.     
20005 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73     if( sqlite3Is
20006 52 6f 77 69 64 28 7a 43 6f 6c 29 20 29 7b 0a 20  Rowid(zCol) ){. 
20007 20 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20           iCol = 
20008 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  -1;.        }els
20009 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  e{.          for
2000a 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54  (iCol=0; iCol<pT
2000b 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b  ab->nCol; iCol++
2000c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 43  ){.            C
2000d 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70  olumn *pCol = &p
2000e 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b  Tab->aCol[iCol];
2000f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
20010 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
20011 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f  pCol->zName, zCo
20012 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)==0 ){.       
20013 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3d         if( iCol=
20014 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a  =pTab->iPKey ){.
20015 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20016 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 20  iCol = -1;.     
20017 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
20018 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
20019 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2001a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2001b 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2001c 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20  iCol<pTab->nCol 
2001d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74  ){.          cnt
2001e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ++;.          if
2001f 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20  ( iCol<0 ){.    
20020 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61          pExpr->a
20021 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45  ffinity = SQLITE
20022 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20  _AFF_INTEGER;.  
20023 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
20024 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d  ( pExpr->iTable=
20025 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
20026 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c    testcase( iCol
20027 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20  ==31 );.        
20028 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43      testcase( iC
20029 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20  ol==32 );.      
2002a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6f 6c        pParse->ol
2002b 64 6d 61 73 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d  dmask |= (iCol>=
2002c 33 32 20 3f 20 30 78 66 66 66 66 66 66 66 66 20  32 ? 0xffffffff 
2002d 3a 20 28 28 28 75 33 32 29 31 29 3c 3c 69 43 6f  : (((u32)1)<<iCo
2002e 6c 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  l));.          }
2002f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
20030 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c    testcase( iCol
20031 3d 3d 33 31 20 29 3b 0a 20 20 20 20 20 20 20 20  ==31 );.        
20032 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43      testcase( iC
20033 6f 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20  ol==32 );.      
20034 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 65        pParse->ne
20035 77 6d 61 73 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d  wmask |= (iCol>=
20036 33 32 20 3f 20 30 78 66 66 66 66 66 66 66 66 20  32 ? 0xffffffff 
20037 3a 20 28 28 28 75 33 32 29 31 29 3c 3c 69 43 6f  : (((u32)1)<<iCo
20038 6c 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  l));.          }
20039 0a 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72  .          pExpr
2003a 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 31 36  ->iColumn = (i16
2003b 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20  )iCol;.         
2003c 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70   pExpr->pTab = p
2003d 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 69  Tab;.          i
2003e 73 54 72 69 67 67 65 72 20 3d 20 31 3b 0a 20 20  sTrigger = 1;.  
2003f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
20040 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
20041 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
20042 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f  OMIT_TRIGGER) */
20043 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
20044 50 65 72 68 61 70 73 20 74 68 65 20 6e 61 6d 65  Perhaps the name
20045 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20   is a reference 
20046 74 6f 20 74 68 65 20 52 4f 57 49 44 0a 20 20 20  to the ROWID.   
20047 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d   */.    if( cnt=
20048 3d 30 20 26 26 20 63 6e 74 54 61 62 3d 3d 31 20  =0 && cntTab==1 
20049 26 26 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69  && sqlite3IsRowi
2004a 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20  d(zCol) ){.     
2004b 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20   cnt = 1;.      
2004c 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  pExpr->iColumn =
2004d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72   -1;.      pExpr
2004e 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c  ->affinity = SQL
2004f 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b  ITE_AFF_INTEGER;
20050 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20  .    }..    /*. 
20051 20 20 20 2a 2a 20 49 66 20 74 68 65 20 69 6e 70     ** If the inp
20052 75 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  ut is of the for
20053 6d 20 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20  m Z (not Y.Z or 
20054 58 2e 59 2e 5a 29 20 74 68 65 6e 20 74 68 65 20  X.Y.Z) then the 
20055 6e 61 6d 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69  name Z.    ** mi
20056 67 68 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20  ght refer to an 
20057 72 65 73 75 6c 74 2d 73 65 74 20 61 6c 69 61 73  result-set alias
20058 2e 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c  .  This happens,
20059 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68   for example, wh
2005a 65 6e 0a 20 20 20 20 2a 2a 20 77 65 20 61 72 65  en.    ** we are
2005b 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73   resolving names
2005c 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
2005d 61 75 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ause of the foll
2005e 6f 77 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20  owing command:. 
2005f 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
20060 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78   SELECT a+b AS x
20061 20 46 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52   FROM table WHER
20062 45 20 78 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20  E x<10;.    **. 
20063 20 20 20 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c     ** In cases l
20064 69 6b 65 20 74 68 69 73 2c 20 72 65 70 6c 61 63  ike this, replac
20065 65 20 70 45 78 70 72 20 77 69 74 68 20 61 20 63  e pExpr with a c
20066 6f 70 79 20 6f 66 20 74 68 65 20 65 78 70 72 65  opy of the expre
20067 73 73 69 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a  ssion that.    *
20068 2a 20 66 6f 72 6d 73 20 74 68 65 20 72 65 73 75  * forms the resu
20069 6c 74 20 73 65 74 20 65 6e 74 72 79 20 28 22 61  lt set entry ("a
2006a 2b 62 22 20 69 6e 20 74 68 65 20 65 78 61 6d 70  +b" in the examp
2006b 6c 65 29 20 61 6e 64 20 72 65 74 75 72 6e 20 69  le) and return i
2006c 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20  mmediately..    
2006d 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
2006e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
2006f 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 73 68  he result set sh
20070 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64  ould have alread
20071 79 20 62 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65  y been.    ** re
20072 73 6f 6c 76 65 64 20 62 79 20 74 68 65 20 74 69  solved by the ti
20073 6d 65 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  me the WHERE cla
20074 75 73 65 20 69 73 20 72 65 73 6f 6c 76 65 64 2e  use is resolved.
20075 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
20076 63 6e 74 3d 3d 30 20 26 26 20 28 70 45 4c 69 73  cnt==0 && (pELis
20077 74 20 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29  t = pNC->pEList)
20078 21 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 29  !=0 && zTab==0 )
20079 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  {.      for(j=0;
2007a 20 6a 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72   j<pEList->nExpr
2007b 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
2007c 63 68 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69  char *zAs = pELi
2007d 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a  st->a[j].zName;.
2007e 20 20 20 20 20 20 20 20 69 66 28 20 7a 41 73 21          if( zAs!
2007f 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =0 && sqlite3Str
20080 49 43 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d  ICmp(zAs, zCol)=
20081 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
20082 45 78 70 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20  Expr *pOrig;.   
20083 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
20084 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26  Expr->pLeft==0 &
20085 26 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d  & pExpr->pRight=
20086 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
20087 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 78  assert( pExpr->x
20088 2e 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20 20  .pList==0 );.   
20089 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2008a 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 3d  Expr->x.pSelect=
2008b 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
2008c 70 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e  pOrig = pEList->
2008d 61 5b 6a 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[j].pExpr;.    
2008e 20 20 20 20 20 20 69 66 28 20 21 70 4e 43 2d 3e        if( !pNC->
2008f 61 6c 6c 6f 77 41 67 67 20 26 26 20 45 78 70 72  allowAgg && Expr
20090 48 61 73 50 72 6f 70 65 72 74 79 28 70 4f 72 69  HasProperty(pOri
20091 67 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a 20 20  g, EP_Agg) ){.  
20092 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
20093 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
20094 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61 6c 69  , "misuse of ali
20095 61 73 65 64 20 61 67 67 72 65 67 61 74 65 20 25  ased aggregate %
20096 73 22 2c 20 7a 41 73 29 3b 0a 20 20 20 20 20 20  s", zAs);.      
20097 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
20098 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20  _Abort;.        
20099 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 65    }.          re
2009a 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61 72 73  solveAlias(pPars
2009b 65 2c 20 70 45 4c 69 73 74 2c 20 6a 2c 20 70 45  e, pEList, j, pE
2009c 78 70 72 2c 20 22 22 29 3b 0a 20 20 20 20 20 20  xpr, "");.      
2009d 20 20 20 20 63 6e 74 20 3d 20 31 3b 0a 20 20 20      cnt = 1;.   
2009e 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 3d 20         pMatch = 
2009f 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  0;.          ass
200a0 65 72 74 28 20 7a 54 61 62 3d 3d 30 20 26 26 20  ert( zTab==0 && 
200a1 7a 44 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  zDb==0 );.      
200a2 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e      goto lookupn
200a3 61 6d 65 5f 65 6e 64 3b 0a 20 20 20 20 20 20 20  ame_end;.       
200a4 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20   }.      } .    
200a5 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63  }..    /* Advanc
200a6 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6e 61  e to the next na
200a7 6d 65 20 63 6f 6e 74 65 78 74 2e 20 20 54 68 65  me context.  The
200a8 20 6c 6f 6f 70 20 77 69 6c 6c 20 65 78 69 74 20   loop will exit 
200a9 77 68 65 6e 20 65 69 74 68 65 72 0a 20 20 20 20  when either.    
200aa 2a 2a 20 77 65 20 68 61 76 65 20 61 20 6d 61 74  ** we have a mat
200ab 63 68 20 28 63 6e 74 3e 30 29 20 6f 72 20 77 68  ch (cnt>0) or wh
200ac 65 6e 20 77 65 20 72 75 6e 20 6f 75 74 20 6f 66  en we run out of
200ad 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73 2e 0a   name contexts..
200ae 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63      */.    if( c
200af 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  nt==0 ){.      p
200b0 4e 43 20 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b  NC = pNC->pNext;
200b1 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
200b2 0a 20 20 2a 2a 20 49 66 20 58 20 61 6e 64 20 59  .  ** If X and Y
200b3 20 61 72 65 20 4e 55 4c 4c 20 28 69 6e 20 6f 74   are NULL (in ot
200b4 68 65 72 20 77 6f 72 64 73 20 69 66 20 6f 6e 6c  her words if onl
200b5 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  y the column nam
200b6 65 20 5a 20 69 73 0a 20 20 2a 2a 20 73 75 70 70  e Z is.  ** supp
200b7 6c 69 65 64 29 20 61 6e 64 20 74 68 65 20 76 61  lied) and the va
200b8 6c 75 65 20 6f 66 20 5a 20 69 73 20 65 6e 63 6c  lue of Z is encl
200b9 6f 73 65 64 20 69 6e 20 64 6f 75 62 6c 65 2d 71  osed in double-q
200ba 75 6f 74 65 73 2c 20 74 68 65 6e 0a 20 20 2a 2a  uotes, then.  **
200bb 20 5a 20 69 73 20 61 20 73 74 72 69 6e 67 20 6c   Z is a string l
200bc 69 74 65 72 61 6c 20 69 66 20 69 74 20 64 6f 65  iteral if it doe
200bd 73 6e 27 74 20 6d 61 74 63 68 20 61 6e 79 20 63  sn't match any c
200be 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49 6e  olumn names.  In
200bf 20 74 68 61 74 0a 20 20 2a 2a 20 63 61 73 65 2c   that.  ** case,
200c0 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 74 75   we need to retu
200c1 72 6e 20 72 69 67 68 74 20 61 77 61 79 20 61 6e  rn right away an
200c2 64 20 6e 6f 74 20 6d 61 6b 65 20 61 6e 79 20 63  d not make any c
200c3 68 61 6e 67 65 73 20 74 6f 0a 20 20 2a 2a 20 70  hanges to.  ** p
200c4 45 78 70 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  Expr..  **.  ** 
200c5 42 65 63 61 75 73 65 20 6e 6f 20 72 65 66 65 72  Because no refer
200c6 65 6e 63 65 20 77 61 73 20 6d 61 64 65 20 74 6f  ence was made to
200c7 20 6f 75 74 65 72 20 63 6f 6e 74 65 78 74 73 2c   outer contexts,
200c8 20 74 68 65 20 70 4e 43 2d 3e 6e 52 65 66 0a 20   the pNC->nRef. 
200c9 20 2a 2a 20 66 69 65 6c 64 73 20 61 72 65 20 6e   ** fields are n
200ca 6f 74 20 63 68 61 6e 67 65 64 20 69 6e 20 61 6e  ot changed in an
200cb 79 20 63 6f 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a  y context..  */.
200cc 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26 26 20    if( cnt==0 && 
200cd 7a 54 61 62 3d 3d 30 20 26 26 20 45 78 70 72 48  zTab==0 && ExprH
200ce 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
200cf 2c 45 50 5f 44 62 6c 51 75 6f 74 65 64 29 20 29  ,EP_DblQuoted) )
200d0 7b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20  {.    pExpr->op 
200d1 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20  = TK_STRING;.   
200d2 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30   pExpr->pTab = 0
200d3 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  ;.    return WRC
200d4 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f  _Prune;.  }..  /
200d5 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65  *.  ** cnt==0 me
200d6 61 6e 73 20 74 68 65 72 65 20 77 61 73 20 6e 6f  ans there was no
200d7 74 20 6d 61 74 63 68 2e 20 20 63 6e 74 3e 31 20  t match.  cnt>1 
200d8 6d 65 61 6e 73 20 74 68 65 72 65 20 77 65 72 65  means there were
200d9 20 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72   two or.  ** mor
200da 65 20 6d 61 74 63 68 65 73 2e 20 20 45 69 74 68  e matches.  Eith
200db 65 72 20 77 61 79 2c 20 77 65 20 68 61 76 65 20  er way, we have 
200dc 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20  an error..  */. 
200dd 20 69 66 28 20 63 6e 74 21 3d 31 20 29 7b 0a 20   if( cnt!=1 ){. 
200de 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
200df 45 72 72 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20  Err;.    zErr = 
200e0 63 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20 73 75 63  cnt==0 ? "no suc
200e1 68 20 63 6f 6c 75 6d 6e 22 20 3a 20 22 61 6d 62  h column" : "amb
200e2 69 67 75 6f 75 73 20 63 6f 6c 75 6d 6e 20 6e 61  iguous column na
200e3 6d 65 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62  me";.    if( zDb
200e4 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
200e5 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
200e6 2c 20 22 25 73 3a 20 25 73 2e 25 73 2e 25 73 22  , "%s: %s.%s.%s"
200e7 2c 20 7a 45 72 72 2c 20 7a 44 62 2c 20 7a 54 61  , zErr, zDb, zTa
200e8 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65  b, zCol);.    }e
200e9 6c 73 65 20 69 66 28 20 7a 54 61 62 20 29 7b 0a  lse if( zTab ){.
200ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
200eb 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
200ec 73 3a 20 25 73 2e 25 73 22 2c 20 7a 45 72 72 2c  s: %s.%s", zErr,
200ed 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20   zTab, zCol);.  
200ee 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
200ef 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
200f0 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73 22 2c  Parse, "%s: %s",
200f1 20 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20   zErr, zCol);.  
200f2 20 20 7d 0a 20 20 20 20 70 54 6f 70 4e 43 2d 3e    }.    pTopNC->
200f3 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f  nErr++;.  }..  /
200f4 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20 66 72  * If a column fr
200f5 6f 6d 20 61 20 74 61 62 6c 65 20 69 6e 20 70 53  om a table in pS
200f6 72 63 4c 69 73 74 20 69 73 20 72 65 66 65 72 65  rcList is refere
200f7 6e 63 65 64 2c 20 74 68 65 6e 20 72 65 63 6f 72  nced, then recor
200f8 64 0a 20 20 2a 2a 20 74 68 69 73 20 66 61 63 74  d.  ** this fact
200f9 20 69 6e 20 74 68 65 20 70 53 72 63 4c 69 73 74   in the pSrcList
200fa 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64 20 62 69 74  .a[].colUsed bit
200fb 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20 30 20  mask.  Column 0 
200fc 63 61 75 73 65 73 0a 20 20 2a 2a 20 62 69 74 20  causes.  ** bit 
200fd 30 20 74 6f 20 62 65 20 73 65 74 2e 20 20 43 6f  0 to be set.  Co
200fe 6c 75 6d 6e 20 31 20 73 65 74 73 20 62 69 74 20  lumn 1 sets bit 
200ff 31 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68  1.  And so forth
20100 2e 20 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 63  .  If the.  ** c
20101 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69 73 20  olumn number is 
20102 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65  greater than the
20103 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20   number of bits 
20104 69 6e 20 74 68 65 20 62 69 74 6d 61 73 6b 0a 20  in the bitmask. 
20105 20 2a 2a 20 74 68 65 6e 20 73 65 74 20 74 68 65   ** then set the
20106 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69 74 20   high-order bit 
20107 6f 66 20 74 68 65 20 62 69 74 6d 61 73 6b 2e 0a  of the bitmask..
20108 20 20 2a 2f 0a 20 20 69 66 28 20 70 45 78 70 72    */.  if( pExpr
20109 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20  ->iColumn>=0 && 
2010a 70 4d 61 74 63 68 21 3d 30 20 29 7b 0a 20 20 20  pMatch!=0 ){.   
2010b 20 69 6e 74 20 6e 20 3d 20 70 45 78 70 72 2d 3e   int n = pExpr->
2010c 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 74 65 73  iColumn;.    tes
2010d 74 63 61 73 65 28 20 6e 3d 3d 42 4d 53 2d 31 20  tcase( n==BMS-1 
2010e 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 3d 42 4d  );.    if( n>=BM
2010f 53 20 29 7b 0a 20 20 20 20 20 20 6e 20 3d 20 42  S ){.      n = B
20110 4d 53 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  MS-1;.    }.    
20111 61 73 73 65 72 74 28 20 70 4d 61 74 63 68 2d 3e  assert( pMatch->
20112 69 43 75 72 73 6f 72 3d 3d 70 45 78 70 72 2d 3e  iCursor==pExpr->
20113 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70 4d  iTable );.    pM
20114 61 74 63 68 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d  atch->colUsed |=
20115 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 6e   ((Bitmask)1)<<n
20116 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61  ;.  }..  /* Clea
20117 6e 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 0a  n up and return.
20118 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78    */.  sqlite3Ex
20119 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78  prDelete(db, pEx
2011a 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 70 45  pr->pLeft);.  pE
2011b 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 30 3b 0a  xpr->pLeft = 0;.
2011c 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2011d 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d 3e 70  ete(db, pExpr->p
2011e 52 69 67 68 74 29 3b 0a 20 20 70 45 78 70 72 2d  Right);.  pExpr-
2011f 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a 20 20 70  >pRight = 0;.  p
20120 45 78 70 72 2d 3e 6f 70 20 3d 20 28 69 73 54 72  Expr->op = (isTr
20121 69 67 67 65 72 20 3f 20 54 4b 5f 54 52 49 47 47  igger ? TK_TRIGG
20122 45 52 20 3a 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b  ER : TK_COLUMN);
20123 0a 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a  .lookupname_end:
20124 0a 20 20 69 66 28 20 63 6e 74 3d 3d 31 20 29 7b  .  if( cnt==1 ){
20125 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 43  .    assert( pNC
20126 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  !=0 );.    sqlit
20127 65 33 41 75 74 68 52 65 61 64 28 70 50 61 72 73  e3AuthRead(pPars
20128 65 2c 20 70 45 78 70 72 2c 20 70 53 63 68 65 6d  e, pExpr, pSchem
20129 61 2c 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74  a, pNC->pSrcList
2012a 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d  );.    /* Increm
2012b 65 6e 74 20 74 68 65 20 6e 52 65 66 20 76 61 6c  ent the nRef val
2012c 75 65 20 6f 6e 20 61 6c 6c 20 6e 61 6d 65 20 63  ue on all name c
2012d 6f 6e 74 65 78 74 73 20 66 72 6f 6d 20 54 6f 70  ontexts from Top
2012e 4e 43 20 75 70 20 74 6f 0a 20 20 20 20 2a 2a 20  NC up to.    ** 
2012f 74 68 65 20 70 6f 69 6e 74 20 77 68 65 72 65 20  the point where 
20130 74 68 65 20 6e 61 6d 65 20 6d 61 74 63 68 65 64  the name matched
20131 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 3b 3b 29  . */.    for(;;)
20132 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
20133 70 54 6f 70 4e 43 21 3d 30 20 29 3b 0a 20 20 20  pTopNC!=0 );.   
20134 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b     pTopNC->nRef+
20135 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f  +;.      if( pTo
20136 70 4e 43 3d 3d 70 4e 43 20 29 20 62 72 65 61 6b  pNC==pNC ) break
20137 3b 0a 20 20 20 20 20 20 70 54 6f 70 4e 43 20 3d  ;.      pTopNC =
20138 20 70 54 6f 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a   pTopNC->pNext;.
20139 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
2013a 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 20   WRC_Prune;.  } 
2013b 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72  else {.    retur
2013c 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d  n WRC_Abort;.  }
2013d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
2013e 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  te and return a 
2013f 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78  pointer to an ex
20140 70 72 65 73 73 69 6f 6e 20 74 6f 20 6c 6f 61 64  pression to load
20141 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c   the column iCol
20142 0a 2a 2a 20 66 72 6f 6d 20 64 61 74 61 73 6f 75  .** from datasou
20143 72 63 65 20 69 53 72 63 20 64 61 74 61 73 6f 75  rce iSrc datasou
20144 72 63 65 20 69 6e 20 53 72 63 4c 69 73 74 20 70  rce in SrcList p
20145 53 72 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Src..*/.SQLITE_P
20146 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
20147 69 74 65 33 43 72 65 61 74 65 43 6f 6c 75 6d 6e  ite3CreateColumn
20148 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a 64 62  Expr(sqlite3 *db
20149 2c 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c  , SrcList *pSrc,
2014a 20 69 6e 74 20 69 53 72 63 2c 20 69 6e 74 20 69   int iSrc, int i
2014b 43 6f 6c 29 7b 0a 20 20 45 78 70 72 20 2a 70 20  Col){.  Expr *p 
2014c 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c  = sqlite3ExprAll
2014d 6f 63 28 64 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e  oc(db, TK_COLUMN
2014e 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  , 0, 0);.  if( p
2014f 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53   ){.    struct S
20150 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
20151 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 69 53  em = &pSrc->a[iS
20152 72 63 5d 3b 0a 20 20 20 20 70 2d 3e 70 54 61 62  rc];.    p->pTab
20153 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a   = pItem->pTab;.
20154 20 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20      p->iTable = 
20155 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a  pItem->iCursor;.
20156 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61 62 2d      if( p->pTab-
20157 3e 69 50 4b 65 79 3d 3d 69 43 6f 6c 20 29 7b 0a  >iPKey==iCol ){.
20158 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e        p->iColumn
20159 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65   = -1;.    }else
2015a 7b 0a 20 20 20 20 20 20 70 2d 3e 69 43 6f 6c 75  {.      p->iColu
2015b 6d 6e 20 3d 20 28 79 6e 56 61 72 29 69 43 6f 6c  mn = (ynVar)iCol
2015c 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 63  ;.      pItem->c
2015d 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42 69 74 6d  olUsed |= ((Bitm
2015e 61 73 6b 29 31 29 3c 3c 28 69 43 6f 6c 3e 3d 42  ask)1)<<(iCol>=B
2015f 4d 53 20 3f 20 42 4d 53 2d 31 20 3a 20 69 43 6f  MS ? BMS-1 : iCo
20160 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 45 78  l);.    }.    Ex
20161 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c  prSetProperty(p,
20162 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 20   EP_Resolved);. 
20163 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
20164 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
20165 74 69 6e 65 20 69 73 20 63 61 6c 6c 62 61 63 6b  tine is callback
20166 20 66 6f 72 20 73 71 6c 69 74 65 33 57 61 6c 6b   for sqlite3Walk
20167 45 78 70 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65  Expr()..**.** Re
20168 73 6f 6c 76 65 20 73 79 6d 62 6f 6c 69 63 20 6e  solve symbolic n
20169 61 6d 65 73 20 69 6e 74 6f 20 54 4b 5f 43 4f 4c  ames into TK_COL
2016a 55 4d 4e 20 6f 70 65 72 61 74 6f 72 73 20 66 6f  UMN operators fo
2016b 72 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a  r the current.**
2016c 20 6e 6f 64 65 20 69 6e 20 74 68 65 20 65 78 70   node in the exp
2016d 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52  ression tree.  R
2016e 65 74 75 72 6e 20 30 20 74 6f 20 63 6f 6e 74 69  eturn 0 to conti
2016f 6e 75 65 20 74 68 65 20 73 65 61 72 63 68 20 64  nue the search d
20170 6f 77 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65 20  own.** the tree 
20171 6f 72 20 32 20 74 6f 20 61 62 6f 72 74 20 74 68  or 2 to abort th
20172 65 20 74 72 65 65 20 77 61 6c 6b 2e 0a 2a 2a 0a  e tree walk..**.
20173 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
20174 61 6c 73 6f 20 64 6f 65 73 20 65 72 72 6f 72 20  also does error 
20175 63 68 65 63 6b 69 6e 67 20 61 6e 64 20 6e 61 6d  checking and nam
20176 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 66 6f 72  e resolution for
20177 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  .** function nam
20178 65 73 2e 20 20 54 68 65 20 6f 70 65 72 61 74 6f  es.  The operato
20179 72 20 66 6f 72 20 61 67 67 72 65 67 61 74 65 20  r for aggregate 
2017a 66 75 6e 63 74 69 6f 6e 73 20 69 73 20 63 68 61  functions is cha
2017b 6e 67 65 64 0a 2a 2a 20 74 6f 20 54 4b 5f 41 47  nged.** to TK_AG
2017c 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2f 0a 73  G_FUNCTION..*/.s
2017d 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76  tatic int resolv
2017e 65 45 78 70 72 53 74 65 70 28 57 61 6c 6b 65 72  eExprStep(Walker
2017f 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20   *pWalker, Expr 
20180 2a 70 45 78 70 72 29 7b 0a 20 20 4e 61 6d 65 43  *pExpr){.  NameC
20181 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 0a 20 20 50  ontext *pNC;.  P
20182 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20  arse *pParse;.. 
20183 20 70 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e   pNC = pWalker->
20184 75 2e 70 4e 43 3b 0a 20 20 61 73 73 65 72 74 28  u.pNC;.  assert(
20185 20 70 4e 43 21 3d 30 20 29 3b 0a 20 20 70 50 61   pNC!=0 );.  pPa
20186 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73  rse = pNC->pPars
20187 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  e;.  assert( pPa
20188 72 73 65 3d 3d 70 57 61 6c 6b 65 72 2d 3e 70 50  rse==pWalker->pP
20189 61 72 73 65 20 29 3b 0a 0a 20 20 69 66 28 20 45  arse );..  if( E
2018a 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
2018b 79 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f  y(pExpr, EP_Reso
2018c 6c 76 65 64 29 20 29 20 72 65 74 75 72 6e 20 57  lved) ) return W
2018d 52 43 5f 50 72 75 6e 65 3b 0a 20 20 45 78 70 72  RC_Prune;.  Expr
2018e 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 78 70  SetProperty(pExp
2018f 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29 3b  r, EP_Resolved);
20190 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
20191 20 20 69 66 28 20 70 4e 43 2d 3e 70 53 72 63 4c    if( pNC->pSrcL
20192 69 73 74 20 26 26 20 70 4e 43 2d 3e 70 53 72 63  ist && pNC->pSrc
20193 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3e 30 20 29  List->nAlloc>0 )
20194 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70  {.    SrcList *p
20195 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70  SrcList = pNC->p
20196 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74  SrcList;.    int
20197 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   i;.    for(i=0;
20198 20 69 3c 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   i<pNC->pSrcList
20199 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
2019a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63      assert( pSrc
2019b 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73  List->a[i].iCurs
2019c 6f 72 3e 3d 30 20 26 26 20 70 53 72 63 4c 69 73  or>=0 && pSrcLis
2019d 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3c  t->a[i].iCursor<
2019e 70 50 61 72 73 65 2d 3e 6e 54 61 62 29 3b 0a 20  pParse->nTab);. 
2019f 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
201a0 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d    switch( pExpr-
201a1 3e 6f 70 20 29 7b 0a 0a 23 69 66 20 64 65 66 69  >op ){..#if defi
201a2 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
201a3 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f  E_UPDATE_DELETE_
201a4 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e  LIMIT) && !defin
201a5 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
201a6 55 42 51 55 45 52 59 29 0a 20 20 20 20 2f 2a 20  UBQUERY).    /* 
201a7 54 68 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72  The special oper
201a8 61 74 6f 72 20 54 4b 5f 52 4f 57 20 6d 65 61 6e  ator TK_ROW mean
201a9 73 20 75 73 65 20 74 68 65 20 72 6f 77 69 64 20  s use the rowid 
201aa 66 6f 72 20 74 68 65 20 66 69 72 73 74 0a 20 20  for the first.  
201ab 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 74    ** column in t
201ac 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20  he FROM clause. 
201ad 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
201ae 20 74 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f   the LIMIT and O
201af 52 44 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63  RDER BY.    ** c
201b0 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67  lause processing
201b1 20 6f 6e 20 55 50 44 41 54 45 20 61 6e 64 20 44   on UPDATE and D
201b2 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73  ELETE statements
201b3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
201b4 65 20 54 4b 5f 52 4f 57 3a 20 7b 0a 20 20 20 20  e TK_ROW: {.    
201b5 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c    SrcList *pSrcL
201b6 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c  ist = pNC->pSrcL
201b7 69 73 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63  ist;.      struc
201b8 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
201b9 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 61 73 73  pItem;.      ass
201ba 65 72 74 28 20 70 53 72 63 4c 69 73 74 20 26 26  ert( pSrcList &&
201bb 20 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63 3d   pSrcList->nSrc=
201bc 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 49 74 65  =1 );.      pIte
201bd 6d 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b  m = pSrcList->a;
201be 20 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f   .      pExpr->o
201bf 70 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20  p = TK_COLUMN;. 
201c0 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54 61 62       pExpr->pTab
201c1 20 3d 20 70 49 74 65 6d 2d 3e 70 54 61 62 3b 0a   = pItem->pTab;.
201c2 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61        pExpr->iTa
201c3 62 6c 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75  ble = pItem->iCu
201c4 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 45 78 70  rsor;.      pExp
201c5 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b  r->iColumn = -1;
201c6 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66  .      pExpr->af
201c7 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
201c8 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
201c9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
201ca 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65  #endif /* define
201cb 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
201cc 55 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49  UPDATE_DELETE_LI
201cd 4d 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64  MIT) && !defined
201ce 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42  (SQLITE_OMIT_SUB
201cf 51 55 45 52 59 29 20 2a 2f 0a 0a 20 20 20 20 2f  QUERY) */..    /
201d0 2a 20 41 20 6c 6f 6e 65 20 69 64 65 6e 74 69 66  * A lone identif
201d1 69 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ier is the name 
201d2 6f 66 20 61 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20  of a column..   
201d3 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   */.    case TK_
201d4 49 44 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75  ID: {.      retu
201d5 72 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50  rn lookupName(pP
201d6 61 72 73 65 2c 20 30 2c 20 30 2c 20 70 45 78 70  arse, 0, 0, pExp
201d7 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 70 4e 43  r->u.zToken, pNC
201d8 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a  , pExpr);.    }.
201d9 20 20 0a 20 20 20 20 2f 2a 20 41 20 74 61 62 6c    .    /* A tabl
201da 65 20 6e 61 6d 65 20 61 6e 64 20 63 6f 6c 75 6d  e name and colum
201db 6e 20 6e 61 6d 65 3a 20 20 20 20 20 49 44 2e 49  n name:     ID.I
201dc 44 0a 20 20 20 20 2a 2a 20 4f 72 20 61 20 64 61  D.    ** Or a da
201dd 74 61 62 61 73 65 2c 20 74 61 62 6c 65 20 61 6e  tabase, table an
201de 64 20 63 6f 6c 75 6d 6e 3a 20 20 49 44 2e 49 44  d column:  ID.ID
201df 2e 49 44 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  .ID.    */.    c
201e0 61 73 65 20 54 4b 5f 44 4f 54 3a 20 7b 0a 20 20  ase TK_DOT: {.  
201e1 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
201e2 7a 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 63  zColumn;.      c
201e3 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c  onst char *zTabl
201e4 65 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  e;.      const c
201e5 68 61 72 20 2a 7a 44 62 3b 0a 20 20 20 20 20 20  har *zDb;.      
201e6 45 78 70 72 20 2a 70 52 69 67 68 74 3b 0a 0a 20  Expr *pRight;.. 
201e7 20 20 20 20 20 2f 2a 20 69 66 28 20 70 53 72 63       /* if( pSrc
201e8 4c 69 73 74 3d 3d 30 20 29 20 62 72 65 61 6b 3b  List==0 ) break;
201e9 20 2a 2f 0a 20 20 20 20 20 20 70 52 69 67 68 74   */.      pRight
201ea 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74   = pExpr->pRight
201eb 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69 67  ;.      if( pRig
201ec 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b  ht->op==TK_ID ){
201ed 0a 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20 30  .        zDb = 0
201ee 3b 0a 20 20 20 20 20 20 20 20 7a 54 61 62 6c 65  ;.        zTable
201ef 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d   = pExpr->pLeft-
201f0 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  >u.zToken;.     
201f1 20 20 20 7a 43 6f 6c 75 6d 6e 20 3d 20 70 52 69     zColumn = pRi
201f2 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20  ght->u.zToken;. 
201f3 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
201f4 20 20 20 20 61 73 73 65 72 74 28 20 70 52 69 67      assert( pRig
201f5 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29  ht->op==TK_DOT )
201f6 3b 0a 20 20 20 20 20 20 20 20 7a 44 62 20 3d 20  ;.        zDb = 
201f7 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 75 2e  pExpr->pLeft->u.
201f8 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20  zToken;.        
201f9 7a 54 61 62 6c 65 20 3d 20 70 52 69 67 68 74 2d  zTable = pRight-
201fa 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e  >pLeft->u.zToken
201fb 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d  ;.        zColum
201fc 6e 20 3d 20 70 52 69 67 68 74 2d 3e 70 52 69 67  n = pRight->pRig
201fd 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20  ht->u.zToken;.  
201fe 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
201ff 72 6e 20 6c 6f 6f 6b 75 70 4e 61 6d 65 28 70 50  rn lookupName(pP
20200 61 72 73 65 2c 20 7a 44 62 2c 20 7a 54 61 62 6c  arse, zDb, zTabl
20201 65 2c 20 7a 43 6f 6c 75 6d 6e 2c 20 70 4e 43 2c  e, zColumn, pNC,
20202 20 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 0a   pExpr);.    }..
20203 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 66      /* Resolve f
20204 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a 20 20  unction names.  
20205 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b    */.    case TK
20206 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20 20 20  _CONST_FUNC:.   
20207 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f   case TK_FUNCTIO
20208 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70 72 4c  N: {.      ExprL
20209 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78  ist *pList = pEx
2020a 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 20 20 20 20  pr->x.pList;    
2020b 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20  /* The argument 
2020c 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e  list */.      in
2020d 74 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c  t n = pList ? pL
2020e 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20  ist->nExpr : 0; 
2020f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
20210 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20  arguments */.   
20211 20 20 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66     int no_such_f
20212 75 6e 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  unc = 0;       /
20213 2a 20 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63  * True if no suc
20214 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74  h function exist
20215 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77  s */.      int w
20216 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20  rong_num_args = 
20217 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  0;     /* True i
20218 66 20 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f  f wrong number o
20219 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20  f arguments */. 
2021a 20 20 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20       int is_agg 
2021b 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2021c 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61   /* True if is a
2021d 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  n aggregate func
2021e 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e  tion */.      in
2021f 74 20 61 75 74 68 3b 20 20 20 20 20 20 20 20 20  t auth;         
20220 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74            /* Aut
20221 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73  horization to us
20222 65 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  e the function *
20223 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b  /.      int nId;
20224 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20225 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
20226 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 66   characters in f
20227 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
20228 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
20229 20 2a 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20   *zId;          
2022a 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f    /* The functio
2022b 6e 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20  n name. */.     
2022c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20   FuncDef *pDef; 
2022d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2022e 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
2022f 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a  t the function *
20230 2f 0a 20 20 20 20 20 20 75 38 20 65 6e 63 20 3d  /.      u8 enc =
20231 20 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29   ENC(pParse->db)
20232 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  ;   /* The datab
20233 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a  ase encoding */.
20234 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
20235 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43   pExpr->op==TK_C
20236 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20  ONST_FUNC );.   
20237 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
20238 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
20239 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
2023a 20 29 3b 0a 20 20 20 20 20 20 7a 49 64 20 3d 20   );.      zId = 
2023b 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b  pExpr->u.zToken;
2023c 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 73 71 6c  .      nId = sql
2023d 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64  ite3Strlen30(zId
2023e 29 3b 0a 20 20 20 20 20 20 70 44 65 66 20 3d 20  );.      pDef = 
2023f 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
20240 69 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ion(pParse->db, 
20241 7a 49 64 2c 20 6e 49 64 2c 20 6e 2c 20 65 6e 63  zId, nId, n, enc
20242 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
20243 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pDef==0 ){.     
20244 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65     pDef = sqlite
20245 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50  3FindFunction(pP
20246 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20 6e  arse->db, zId, n
20247 49 64 2c 20 2d 31 2c 20 65 6e 63 2c 20 30 29 3b  Id, -1, enc, 0);
20248 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 44 65  .        if( pDe
20249 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  f==0 ){.        
2024a 20 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63 20 3d    no_such_func =
2024b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   1;.        }els
2024c 65 7b 0a 20 20 20 20 20 20 20 20 20 20 77 72 6f  e{.          wro
2024d 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 31 3b  ng_num_args = 1;
2024e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2024f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
20250 69 73 5f 61 67 67 20 3d 20 70 44 65 66 2d 3e 78  is_agg = pDef->x
20251 46 75 6e 63 3d 3d 30 3b 0a 20 20 20 20 20 20 7d  Func==0;.      }
20252 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
20253 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
20254 4f 4e 0a 20 20 20 20 20 20 69 66 28 20 70 44 65  ON.      if( pDe
20255 66 20 29 7b 0a 20 20 20 20 20 20 20 20 61 75 74  f ){.        aut
20256 68 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43  h = sqlite3AuthC
20257 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
20258 49 54 45 5f 46 55 4e 43 54 49 4f 4e 2c 20 30 2c  ITE_FUNCTION, 0,
20259 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pDef->zName, 0)
2025a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 75  ;.        if( au
2025b 74 68 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  th!=SQLITE_OK ){
2025c 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61  .          if( a
2025d 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59  uth==SQLITE_DENY
2025e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2025f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
20260 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74  pParse, "not aut
20261 68 6f 72 69 7a 65 64 20 74 6f 20 75 73 65 20 66  horized to use f
20262 75 6e 63 74 69 6f 6e 3a 20 25 73 22 2c 0a 20 20  unction: %s",.  
20263 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20264 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20265 20 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a    pDef->zName);.
20266 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 43 2d              pNC-
20267 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20  >nErr++;.       
20268 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70     }.          p
20269 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55  Expr->op = TK_NU
2026a 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  LL;.          re
2026b 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
2026c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2026d 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69  }.#endif.      i
2026e 66 28 20 69 73 5f 61 67 67 20 26 26 20 21 70 4e  f( is_agg && !pN
2026f 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 29 7b 0a 20  C->allowAgg ){. 
20270 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
20271 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
20272 6d 69 73 75 73 65 20 6f 66 20 61 67 67 72 65 67  misuse of aggreg
20273 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a  ate function %.*
20274 73 28 29 22 2c 20 6e 49 64 2c 7a 49 64 29 3b 0a  s()", nId,zId);.
20275 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72          pNC->nEr
20276 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 73 5f  r++;.        is_
20277 61 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  agg = 0;.      }
20278 65 6c 73 65 20 69 66 28 20 6e 6f 5f 73 75 63 68  else if( no_such
20279 5f 66 75 6e 63 20 29 7b 0a 20 20 20 20 20 20 20  _func ){.       
2027a 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2027b 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
2027c 68 20 66 75 6e 63 74 69 6f 6e 3a 20 25 2e 2a 73  h function: %.*s
2027d 22 2c 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20  ", nId, zId);.  
2027e 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b        pNC->nErr+
2027f 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  +;.      }else i
20280 66 28 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67  f( wrong_num_arg
20281 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  s ){.        sql
20282 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
20283 72 73 65 2c 22 77 72 6f 6e 67 20 6e 75 6d 62 65  rse,"wrong numbe
20284 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
20285 6f 20 66 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28  o function %.*s(
20286 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  )",.            
20287 20 6e 49 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20   nId, zId);.    
20288 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b      pNC->nErr++;
20289 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2028a 66 28 20 69 73 5f 61 67 67 20 29 7b 0a 20 20 20  f( is_agg ){.   
2028b 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d       pExpr->op =
2028c 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e   TK_AGG_FUNCTION
2028d 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 68  ;.        pNC->h
2028e 61 73 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20  asAgg = 1;.     
2028f 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73 5f   }.      if( is_
20290 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77  agg ) pNC->allow
20291 41 67 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 73  Agg = 0;.      s
20292 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69  qlite3WalkExprLi
20293 73 74 28 70 57 61 6c 6b 65 72 2c 20 70 4c 69 73  st(pWalker, pLis
20294 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73  t);.      if( is
20295 5f 61 67 67 20 29 20 70 4e 43 2d 3e 61 6c 6c 6f  _agg ) pNC->allo
20296 77 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 20 20  wAgg = 1;.      
20297 2f 2a 20 46 49 58 20 4d 45 3a 20 20 43 6f 6d 70  /* FIX ME:  Comp
20298 75 74 65 20 70 45 78 70 72 2d 3e 61 66 66 69 6e  ute pExpr->affin
20299 69 74 79 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ity based on the
2029a 20 65 78 70 65 63 74 65 64 20 72 65 74 75 72 6e   expected return
2029b 0a 20 20 20 20 20 20 2a 2a 20 74 79 70 65 20 6f  .      ** type o
2029c 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 0a  f the function .
2029d 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
2029e 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
2029f 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
202a0 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
202a1 45 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ERY.    case TK_
202a2 53 45 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65  SELECT:.    case
202a3 20 54 4b 5f 45 58 49 53 54 53 3a 20 20 74 65 73   TK_EXISTS:  tes
202a4 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70  tcase( pExpr->op
202a5 3d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 23  ==TK_EXISTS );.#
202a6 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54  endif.    case T
202a7 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65  K_IN: {.      te
202a8 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
202a9 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 20 20  p==TK_IN );.    
202aa 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
202ab 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
202ac 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
202ad 20 20 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d        int nRef =
202ae 20 70 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e   pNC->nRef;.#ifn
202af 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
202b0 43 48 45 43 4b 0a 20 20 20 20 20 20 20 20 69 66  CHECK.        if
202b1 28 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29  ( pNC->isCheck )
202b2 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
202b3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
202b4 73 65 2c 22 73 75 62 71 75 65 72 69 65 73 20 70  se,"subqueries p
202b5 72 6f 68 69 62 69 74 65 64 20 69 6e 20 43 48 45  rohibited in CHE
202b6 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29  CK constraints")
202b7 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64  ;.        }.#end
202b8 69 66 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  if.        sqlit
202b9 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61  e3WalkSelect(pWa
202ba 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70  lker, pExpr->x.p
202bb 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20  Select);.       
202bc 20 61 73 73 65 72 74 28 20 70 4e 43 2d 3e 6e 52   assert( pNC->nR
202bd 65 66 3e 3d 6e 52 65 66 20 29 3b 0a 20 20 20 20  ef>=nRef );.    
202be 20 20 20 20 69 66 28 20 6e 52 65 66 21 3d 70 4e      if( nRef!=pN
202bf 43 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20 20  C->nRef ){.     
202c0 20 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70       ExprSetProp
202c1 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56  erty(pExpr, EP_V
202c2 61 72 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20  arSelect);.     
202c3 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
202c4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
202c5 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
202c6 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 63 61  MIT_CHECK.    ca
202c7 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a 20  se TK_VARIABLE: 
202c8 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d  {.      if( pNC-
202c9 3e 69 73 43 68 65 63 6b 20 29 7b 0a 20 20 20 20  >isCheck ){.    
202ca 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
202cb 4d 73 67 28 70 50 61 72 73 65 2c 22 70 61 72 61  Msg(pParse,"para
202cc 6d 65 74 65 72 73 20 70 72 6f 68 69 62 69 74 65  meters prohibite
202cd 64 20 69 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74  d in CHECK const
202ce 72 61 69 6e 74 73 22 29 3b 0a 20 20 20 20 20 20  raints");.      
202cf 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
202d0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
202d1 20 20 72 65 74 75 72 6e 20 28 70 50 61 72 73 65    return (pParse
202d2 2d 3e 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65  ->nErr || pParse
202d3 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
202d4 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74 20  ed) ? WRC_Abort 
202d5 3a 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  : WRC_Continue;.
202d6 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73 74 20  }../*.** pEList 
202d7 69 73 20 61 20 6c 69 73 74 20 6f 66 20 65 78 70  is a list of exp
202d8 72 65 73 73 69 6f 6e 73 20 77 68 69 63 68 20 61  ressions which a
202d9 72 65 20 72 65 61 6c 6c 79 20 74 68 65 20 72 65  re really the re
202da 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 0a  sult set of the.
202db 2a 2a 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  ** a SELECT stat
202dc 65 6d 65 6e 74 2e 20 20 70 45 20 69 73 20 61 20  ement.  pE is a 
202dd 74 65 72 6d 20 69 6e 20 61 6e 20 4f 52 44 45 52  term in an ORDER
202de 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20   BY or GROUP BY 
202df 63 6c 61 75 73 65 2e 0a 2a 2a 20 54 68 69 73 20  clause..** This 
202e0 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74  routine checks t
202e1 6f 20 73 65 65 20 69 66 20 70 45 20 69 73 20 61  o see if pE is a
202e2 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69   simple identifi
202e3 65 72 20 77 68 69 63 68 20 63 6f 72 72 65 73 70  er which corresp
202e4 6f 6e 64 73 0a 2a 2a 20 74 6f 20 74 68 65 20 41  onds.** to the A
202e5 53 2d 6e 61 6d 65 20 6f 66 20 6f 6e 65 20 6f 66  S-name of one of
202e6 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68   the terms of th
202e7 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  e expression lis
202e8 74 2e 20 20 49 66 20 69 74 20 69 73 2c 0a 2a 2a  t.  If it is,.**
202e9 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
202ea 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20  turn an integer 
202eb 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20  between 1 and N 
202ec 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e  where N is the n
202ed 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d  umber of.** elem
202ee 65 6e 74 73 20 69 6e 20 70 45 4c 69 73 74 2c 20  ents in pEList, 
202ef 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
202f0 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65 6e   the matching en
202f1 74 72 79 2e 20 20 49 66 20 74 68 65 72 65 20 69  try.  If there i
202f2 73 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68 2c 20 6f  s.** no match, o
202f3 72 20 69 66 20 70 45 20 69 73 20 6e 6f 74 20 61  r if pE is not a
202f4 20 73 69 6d 70 6c 65 20 69 64 65 6e 74 69 66 69   simple identifi
202f5 65 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  er, then this ro
202f6 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 20  utine.** return 
202f7 30 2e 0a 2a 2a 0a 2a 2a 20 70 45 4c 69 73 74 20  0..**.** pEList 
202f8 68 61 73 20 62 65 65 6e 20 72 65 73 6f 6c 76 65  has been resolve
202f9 64 2e 20 20 70 45 20 68 61 73 20 6e 6f 74 2e 0a  d.  pE has not..
202fa 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
202fb 73 6f 6c 76 65 41 73 4e 61 6d 65 28 0a 20 20 50  solveAsName(.  P
202fc 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
202fd 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
202fe 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20 6d  text for error m
202ff 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 45 78 70  essages */.  Exp
20300 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20  rList *pEList,  
20301 2f 2a 20 4c 69 73 74 20 6f 66 20 65 78 70 72 65  /* List of expre
20302 73 73 69 6f 6e 73 20 74 6f 20 73 63 61 6e 20 2a  ssions to scan *
20303 2f 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20  /.  Expr *pE    
20304 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73         /* Expres
20305 73 69 6f 6e 20 77 65 20 61 72 65 20 74 72 79 69  sion we are tryi
20306 6e 67 20 74 6f 20 6d 61 74 63 68 20 2a 2f 0a 29  ng to match */.)
20307 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  {.  int i;      
20308 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
20309 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 55 4e 55  ounter */..  UNU
2030a 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50  SED_PARAMETER(pP
2030b 61 72 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 45  arse);..  if( pE
2030c 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20  ->op==TK_ID ){. 
2030d 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20     char *zCol = 
2030e 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20  pE->u.zToken;.  
2030f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c    for(i=0; i<pEL
20310 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
20311 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41  {.      char *zA
20312 73 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d  s = pEList->a[i]
20313 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66  .zName;.      if
20314 28 20 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69  ( zAs!=0 && sqli
20315 74 65 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20  te3StrICmp(zAs, 
20316 7a 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20  zCol)==0 ){.    
20317 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a      return i+1;.
20318 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
20319 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
2031a 0a 2f 2a 0a 2a 2a 20 70 45 20 69 73 20 61 20 70  ./*.** pE is a p
2031b 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 65 78 70  ointer to an exp
2031c 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 69 73  ression which is
2031d 20 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69   a single term i
2031e 6e 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42  n the.** ORDER B
2031f 59 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20  Y of a compound 
20320 53 45 4c 45 43 54 2e 20 20 54 68 65 20 65 78 70  SELECT.  The exp
20321 72 65 73 73 69 6f 6e 20 68 61 73 20 6e 6f 74 20  ression has not 
20322 62 65 65 6e 0a 2a 2a 20 6e 61 6d 65 20 72 65 73  been.** name res
20323 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20  olved..**.** At 
20324 74 68 65 20 70 6f 69 6e 74 20 74 68 69 73 20 72  the point this r
20325 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
20326 2c 20 77 65 20 61 6c 72 65 61 64 79 20 6b 6e 6f  , we already kno
20327 77 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 4f 52  w that the.** OR
20328 44 45 52 20 42 59 20 74 65 72 6d 20 69 73 20 6e  DER BY term is n
20329 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e  ot an integer in
2032a 64 65 78 20 69 6e 74 6f 20 74 68 65 20 72 65 73  dex into the res
2032b 75 6c 74 20 73 65 74 2e 20 20 54 68 61 74 0a 2a  ult set.  That.*
2032c 2a 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65  * case is handle
2032d 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  d by the calling
2032e 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
2032f 41 74 74 65 6d 70 74 20 74 6f 20 6d 61 74 63 68  Attempt to match
20330 20 70 45 20 61 67 61 69 6e 73 74 20 72 65 73 75   pE against resu
20331 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 69  lt set columns i
20332 6e 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 0a  n the left-most.
20333 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
20334 65 6e 74 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ent.  Return the
20335 20 69 6e 64 65 78 20 69 20 6f 66 20 74 68 65 20   index i of the 
20336 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c  matching column,
20337 0a 2a 2a 20 61 73 20 61 6e 20 69 6e 64 69 63 61  .** as an indica
20338 74 69 6f 6e 20 74 6f 20 74 68 65 20 63 61 6c 6c  tion to the call
20339 65 72 20 74 68 61 74 20 69 74 20 73 68 6f 75 6c  er that it shoul
2033a 64 20 73 6f 72 74 20 62 79 20 74 68 65 20 69 2d  d sort by the i-
2033b 74 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68  th column..** Th
2033c 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75  e left-most colu
2033d 6d 6e 20 69 73 20 31 2e 20 20 49 6e 20 6f 74 68  mn is 1.  In oth
2033e 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 76 61  er words, the va
2033f 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20  lue returned is 
20340 74 68 65 0a 2a 2a 20 73 61 6d 65 20 69 6e 74 65  the.** same inte
20341 67 65 72 20 76 61 6c 75 65 20 74 68 61 74 20 77  ger value that w
20342 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20  ould be used in 
20343 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
20344 74 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a  t to indicate.**
20345 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a   the column..**.
20346 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  ** If there is n
20347 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e 20  o match, return 
20348 30 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66  0.  Return -1 if
20349 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
2034a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2034b 72 65 73 6f 6c 76 65 4f 72 64 65 72 42 79 54 65  resolveOrderByTe
2034c 72 6d 54 6f 45 78 70 72 4c 69 73 74 28 0a 20 20  rmToExprList(.  
2034d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2034e 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
2034f 6e 74 65 78 74 20 66 6f 72 20 65 72 72 6f 72 20  ntext for error 
20350 6d 65 73 73 61 67 65 73 20 2a 2f 0a 20 20 53 65  messages */.  Se
20351 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
20352 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
20353 74 61 74 65 6d 65 6e 74 20 77 69 74 68 20 74 68  tatement with th
20354 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
20355 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 20  e */.  Expr *pE 
20356 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
20357 20 73 70 65 63 69 66 69 63 20 4f 52 44 45 52 20   specific ORDER 
20358 42 59 20 74 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20  BY term */.){.  
20359 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
2035a 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
2035b 65 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  er */.  ExprList
2035c 20 2a 70 45 4c 69 73 74 3b 20 20 2f 2a 20 54 68   *pEList;  /* Th
2035d 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  e columns of the
2035e 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
2035f 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6e 63 3b   NameContext nc;
20360 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74      /* Name cont
20361 65 78 74 20 66 6f 72 20 72 65 73 6f 6c 76 69 6e  ext for resolvin
20362 67 20 70 45 20 2a 2f 0a 0a 20 20 61 73 73 65 72  g pE */..  asser
20363 74 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  t( sqlite3ExprIs
20364 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 29 3d  Integer(pE, &i)=
20365 3d 30 20 29 3b 0a 20 20 70 45 4c 69 73 74 20 3d  =0 );.  pEList =
20366 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
20367 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  ;..  /* Resolve 
20368 61 6c 6c 20 6e 61 6d 65 73 20 69 6e 20 74 68 65  all names in the
20369 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 20 65   ORDER BY term e
2036a 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20  xpression.  */. 
2036b 20 6d 65 6d 73 65 74 28 26 6e 63 2c 20 30 2c 20   memset(&nc, 0, 
2036c 73 69 7a 65 6f 66 28 6e 63 29 29 3b 0a 20 20 6e  sizeof(nc));.  n
2036d 63 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  c.pParse = pPars
2036e 65 3b 0a 20 20 6e 63 2e 70 53 72 63 4c 69 73 74  e;.  nc.pSrcList
2036f 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63   = pSelect->pSrc
20370 3b 0a 20 20 6e 63 2e 70 45 4c 69 73 74 20 3d 20  ;.  nc.pEList = 
20371 70 45 4c 69 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c  pEList;.  nc.all
20372 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20 6e 63 2e  owAgg = 1;.  nc.
20373 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 66 28 20  nErr = 0;.  if( 
20374 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
20375 70 72 4e 61 6d 65 73 28 26 6e 63 2c 20 70 45 29  prNames(&nc, pE)
20376 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
20377 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65  rrorClear(pParse
20378 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
20379 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 72 79 20 74  .  }..  /* Try t
2037a 6f 20 6d 61 74 63 68 20 74 68 65 20 4f 52 44 45  o match the ORDE
2037b 52 20 42 59 20 65 78 70 72 65 73 73 69 6f 6e 20  R BY expression 
2037c 61 67 61 69 6e 73 74 20 61 6e 20 65 78 70 72 65  against an expre
2037d 73 73 69 6f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68  ssion.  ** in th
2037e 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 52  e result set.  R
2037f 65 74 75 72 6e 20 61 6e 20 31 2d 62 61 73 65 64  eturn an 1-based
20380 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61   index of the ma
20381 74 63 68 69 6e 67 0a 20 20 2a 2a 20 72 65 73 75  tching.  ** resu
20382 6c 74 2d 73 65 74 20 65 6e 74 72 79 2e 0a 20 20  lt-set entry..  
20383 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
20384 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  pEList->nExpr; i
20385 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
20386 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28  ite3ExprCompare(
20387 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78  pEList->a[i].pEx
20388 70 72 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20  pr, pE) ){.     
20389 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20   return i+1;.   
2038a 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
2038b 6e 6f 20 6d 61 74 63 68 2c 20 72 65 74 75 72 6e  no match, return
2038c 20 30 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20   0. */.  return 
2038d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  0;.}../*.** Gene
2038e 72 61 74 65 20 61 6e 20 4f 52 44 45 52 20 42 59  rate an ORDER BY
2038f 20 6f 72 20 47 52 4f 55 50 20 42 59 20 74 65 72   or GROUP BY ter
20390 6d 20 6f 75 74 2d 6f 66 2d 72 61 6e 67 65 20 65  m out-of-range e
20391 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rror..*/.static 
20392 76 6f 69 64 20 72 65 73 6f 6c 76 65 4f 75 74 4f  void resolveOutO
20393 66 52 61 6e 67 65 45 72 72 6f 72 28 0a 20 20 50  fRangeError(.  P
20394 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
20395 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 72 72        /* The err
20396 6f 72 20 63 6f 6e 74 65 78 74 20 69 6e 74 6f 20  or context into 
20397 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 74  which to write t
20398 68 65 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f  he error */.  co
20399 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 2c  nst char *zType,
2039a 20 20 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20       /* "ORDER" 
2039b 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f 0a 20 20  or "GROUP" */.  
2039c 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20 20 20  int i,          
2039d 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
2039e 64 65 78 20 28 31 2d 62 61 73 65 64 29 20 6f 66  dex (1-based) of
2039f 20 74 68 65 20 74 65 72 6d 20 6f 75 74 20 6f 66   the term out of
203a0 20 72 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20   range */.  int 
203a1 6d 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20  mx              
203a2 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 70 65     /* Largest pe
203a3 72 6d 69 73 73 69 62 6c 65 20 76 61 6c 75 65 20  rmissible value 
203a4 6f 66 20 69 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  of i */.){.  sql
203a5 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
203a6 72 73 65 2c 20 0a 20 20 20 20 22 25 72 20 25 73  rse, .    "%r %s
203a7 20 42 59 20 74 65 72 6d 20 6f 75 74 20 6f 66 20   BY term out of 
203a8 72 61 6e 67 65 20 2d 20 73 68 6f 75 6c 64 20 62  range - should b
203a9 65 20 22 0a 20 20 20 20 22 62 65 74 77 65 65 6e  e ".    "between
203aa 20 31 20 61 6e 64 20 25 64 22 2c 20 69 2c 20 7a   1 and %d", i, z
203ab 54 79 70 65 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a  Type, mx);.}../*
203ac 0a 2a 2a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20  .** Analyze the 
203ad 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
203ae 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45  in a compound SE
203af 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  LECT statement. 
203b0 20 20 4d 6f 64 69 66 79 0a 2a 2a 20 65 61 63 68    Modify.** each
203b1 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44   term of the ORD
203b2 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  ER BY clause is 
203b3 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67  a constant integ
203b4 65 72 20 62 65 74 77 65 65 6e 20 31 0a 2a 2a 20  er between 1.** 
203b5 61 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73  and N where N is
203b6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
203b7 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 6f  olumns in the co
203b8 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 2e 0a 2a  mpound SELECT..*
203b9 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65  *.** ORDER BY te
203ba 72 6d 73 20 74 68 61 74 20 61 72 65 20 61 6c 72  rms that are alr
203bb 65 61 64 79 20 61 6e 20 69 6e 74 65 67 65 72 20  eady an integer 
203bc 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e 20  between 1 and N 
203bd 61 72 65 0a 2a 2a 20 75 6e 6d 6f 64 69 66 69 65  are.** unmodifie
203be 64 2e 20 20 4f 52 44 45 52 20 42 59 20 74 65 72  d.  ORDER BY ter
203bf 6d 73 20 74 68 61 74 20 61 72 65 20 69 6e 74 65  ms that are inte
203c0 67 65 72 73 20 6f 75 74 73 69 64 65 20 74 68 65  gers outside the
203c1 20 72 61 6e 67 65 20 6f 66 0a 2a 2a 20 31 20 74   range of.** 1 t
203c2 68 72 6f 75 67 68 20 4e 20 67 65 6e 65 72 61 74  hrough N generat
203c3 65 20 61 6e 20 65 72 72 6f 72 2e 20 20 4f 52 44  e an error.  ORD
203c4 45 52 20 42 59 20 74 65 72 6d 73 20 74 68 61 74  ER BY terms that
203c5 20 61 72 65 20 65 78 70 72 65 73 73 69 6f 6e 73   are expressions
203c6 0a 2a 2a 20 61 72 65 20 6d 61 74 63 68 65 64 20  .** are matched 
203c7 61 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73  against result s
203c8 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f  et expressions o
203c9 66 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43  f compound SELEC
203ca 54 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 77  T.** beginning w
203cb 69 74 68 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73  ith the left-mos
203cc 74 20 53 45 4c 45 43 54 20 61 6e 64 20 77 6f 72  t SELECT and wor
203cd 6b 69 6e 67 20 74 6f 77 61 72 64 20 74 68 65 20  king toward the 
203ce 72 69 67 68 74 2e 0a 2a 2a 20 41 74 20 74 68 65  right..** At the
203cf 20 66 69 72 73 74 20 6d 61 74 63 68 2c 20 74 68   first match, th
203d0 65 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65  e ORDER BY expre
203d1 73 73 69 6f 6e 20 69 73 20 74 72 61 6e 73 66 6f  ssion is transfo
203d2 72 6d 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65  rmed into.** the
203d3 20 69 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20   integer column 
203d4 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65  number..**.** Re
203d5 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
203d6 6f 66 20 65 72 72 6f 72 73 20 73 65 65 6e 2e 0a  of errors seen..
203d7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
203d8 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e 64 4f 72 64  solveCompoundOrd
203d9 65 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70  erBy(.  Parse *p
203da 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
203db 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
203dc 2e 20 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d  .  Leave error m
203dd 65 73 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a  essages here */.
203de 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
203df 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53  t       /* The S
203e0 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
203e1 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4f  containing the O
203e2 52 44 45 52 20 42 59 20 2a 2f 0a 29 7b 0a 20 20  RDER BY */.){.  
203e3 69 6e 74 20 69 3b 0a 20 20 45 78 70 72 4c 69 73  int i;.  ExprLis
203e4 74 20 2a 70 4f 72 64 65 72 42 79 3b 0a 20 20 45  t *pOrderBy;.  E
203e5 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
203e6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
203e7 20 20 69 6e 74 20 6d 6f 72 65 54 6f 44 6f 20 3d    int moreToDo =
203e8 20 31 3b 0a 0a 20 20 70 4f 72 64 65 72 42 79 20   1;..  pOrderBy 
203e9 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65  = pSelect->pOrde
203ea 72 42 79 3b 0a 20 20 69 66 28 20 70 4f 72 64 65  rBy;.  if( pOrde
203eb 72 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  rBy==0 ) return 
203ec 30 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  0;.  db = pParse
203ed 2d 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49 54 45  ->db;.#if SQLITE
203ee 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66  _MAX_COLUMN.  if
203ef 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  ( pOrderBy->nExp
203f0 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  r>db->aLimit[SQL
203f1 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
203f2 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
203f3 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
203f4 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73   "too many terms
203f5 20 69 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   in ORDER BY cla
203f6 75 73 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72  use");.    retur
203f7 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  n 1;.  }.#endif.
203f8 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 72    for(i=0; i<pOr
203f9 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b  derBy->nExpr; i+
203fa 2b 29 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79  +){.    pOrderBy
203fb 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b  ->a[i].done = 0;
203fc 0a 20 20 7d 0a 20 20 70 53 65 6c 65 63 74 2d 3e  .  }.  pSelect->
203fd 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 77 68 69  pNext = 0;.  whi
203fe 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72  le( pSelect->pPr
203ff 69 6f 72 20 29 7b 0a 20 20 20 20 70 53 65 6c 65  ior ){.    pSele
20400 63 74 2d 3e 70 50 72 69 6f 72 2d 3e 70 4e 65 78  ct->pPrior->pNex
20401 74 20 3d 20 70 53 65 6c 65 63 74 3b 0a 20 20 20  t = pSelect;.   
20402 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65   pSelect = pSele
20403 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a  ct->pPrior;.  }.
20404 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74    while( pSelect
20405 20 26 26 20 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a   && moreToDo ){.
20406 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
20407 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
20408 0a 20 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20  .    moreToDo = 
20409 30 3b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20  0;.    pEList = 
2040a 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
2040b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c  .    assert( pEL
2040c 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f  ist!=0 );.    fo
2040d 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72  r(i=0, pItem=pOr
2040e 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64  derBy->a; i<pOrd
2040f 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  erBy->nExpr; i++
20410 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
20411 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 3b    int iCol = -1;
20412 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 2c  .      Expr *pE,
20413 20 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69 66   *pDup;.      if
20414 28 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 29 20  ( pItem->done ) 
20415 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
20416 70 45 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70  pE = pItem->pExp
20417 72 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  r;.      if( sql
20418 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65  ite3ExprIsIntege
20419 72 28 70 45 2c 20 26 69 43 6f 6c 29 20 29 7b 0a  r(pE, &iCol) ){.
2041a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
2041b 3c 3d 30 20 7c 7c 20 69 43 6f 6c 3e 70 45 4c 69  <=0 || iCol>pELi
2041c 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
2041d 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f 75         resolveOu
2041e 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70 50  tOfRangeError(pP
2041f 61 72 73 65 2c 20 22 4f 52 44 45 52 22 2c 20 69  arse, "ORDER", i
20420 2b 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  +1, pEList->nExp
20421 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  r);.          re
20422 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20  turn 1;.        
20423 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
20424 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65         iCol = re
20425 73 6f 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72  solveAsName(pPar
20426 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 45 29 3b  se, pEList, pE);
20427 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f  .        if( iCo
20428 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  l==0 ){.        
20429 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
2042a 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 2c 20  ExprDup(db, pE, 
2042b 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  0);.          if
2042c 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
2042d 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  led ){.         
2042e 20 20 20 61 73 73 65 72 74 28 70 44 75 70 29 3b     assert(pDup);
2042f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f  .            iCo
20430 6c 20 3d 20 72 65 73 6f 6c 76 65 4f 72 64 65 72  l = resolveOrder
20431 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73 74  ByTermToExprList
20432 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
20433 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20  , pDup);.       
20434 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73     }.          s
20435 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
20436 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20 20 20  (db, pDup);.    
20437 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
20438 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 30 20 29      if( iCol>0 )
20439 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65  {.        CollSe
2043a 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70  q *pColl = pE->p
2043b 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e  Coll;.        in
2043c 74 20 66 6c 61 67 73 20 3d 20 70 45 2d 3e 66 6c  t flags = pE->fl
2043d 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c  ags & EP_ExpColl
2043e 61 74 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ate;.        sql
2043f 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
20440 62 2c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20  b, pE);.        
20441 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 3d 20 70  pItem->pExpr = p
20442 45 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28  E = sqlite3Expr(
20443 64 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20  db, TK_INTEGER, 
20444 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
20445 70 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  pE==0 ) return 1
20446 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 70 43  ;.        pE->pC
20447 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20  oll = pColl;.   
20448 20 20 20 20 20 70 45 2d 3e 66 6c 61 67 73 20 7c       pE->flags |
20449 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 20 7c 20  = EP_IntValue | 
2044a 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 20 20 70  flags;.        p
2044b 45 2d 3e 75 2e 69 56 61 6c 75 65 20 3d 20 69 43  E->u.iValue = iC
2044c 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65  ol;.        pIte
2044d 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29 69  m->iCol = (u16)i
2044e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74  Col;.        pIt
2044f 65 6d 2d 3e 64 6f 6e 65 20 3d 20 31 3b 0a 20 20  em->done = 1;.  
20450 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20451 20 20 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b     moreToDo = 1;
20452 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
20453 20 20 20 70 53 65 6c 65 63 74 20 3d 20 70 53 65     pSelect = pSe
20454 6c 65 63 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  lect->pNext;.  }
20455 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f  .  for(i=0; i<pO
20456 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69  rderBy->nExpr; i
20457 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 72  ++){.    if( pOr
20458 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65  derBy->a[i].done
20459 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
2045a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2045b 72 73 65 2c 20 22 25 72 20 4f 52 44 45 52 20 42  rse, "%r ORDER B
2045c 59 20 74 65 72 6d 20 64 6f 65 73 20 6e 6f 74 20  Y term does not 
2045d 6d 61 74 63 68 20 61 6e 79 20 22 0a 20 20 20 20  match any ".    
2045e 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 20          "column 
2045f 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
20460 74 22 2c 20 69 2b 31 29 3b 0a 20 20 20 20 20 20  t", i+1);.      
20461 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
20462 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
20463 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 65  }../*.** Check e
20464 76 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65  very term in the
20465 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f   ORDER BY or GRO
20466 55 50 20 42 59 20 63 6c 61 75 73 65 20 70 4f 72  UP BY clause pOr
20467 64 65 72 42 79 20 6f 66 0a 2a 2a 20 74 68 65 20  derBy of.** the 
20468 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
20469 20 70 53 65 6c 65 63 74 2e 20 20 49 66 20 61 6e   pSelect.  If an
2046a 79 20 74 65 72 6d 20 69 73 20 72 65 66 65 72 65  y term is refere
2046b 6e 63 65 20 74 6f 20 61 0a 2a 2a 20 72 65 73 75  nce to a.** resu
2046c 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69 6f  lt set expressio
2046d 6e 20 28 61 73 20 64 65 74 65 72 6d 69 6e 65 64  n (as determined
2046e 20 62 79 20 74 68 65 20 45 78 70 72 4c 69 73 74   by the ExprList
2046f 2e 61 2e 69 43 6f 6c 20 66 69 65 6c 64 29 0a 2a  .a.iCol field).*
20470 2a 20 74 68 65 6e 20 63 6f 6e 76 65 72 74 20 74  * then convert t
20471 68 61 74 20 74 65 72 6d 20 69 6e 74 6f 20 61 20  hat term into a 
20472 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 72 72  copy of the corr
20473 65 73 70 6f 6e 64 69 6e 67 20 72 65 73 75 6c 74  esponding result
20474 20 73 65 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a   set.** column..
20475 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 65 72 72  **.** If any err
20476 6f 72 73 20 61 72 65 20 64 65 74 65 63 74 65 64  ors are detected
20477 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20 6d  , add an error m
20478 65 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65  essage to pParse
20479 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 6e   and.** return n
2047a 6f 6e 2d 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e  on-zero.  Return
2047b 20 7a 65 72 6f 20 69 66 20 6e 6f 20 65 72 72 6f   zero if no erro
2047c 72 73 20 61 72 65 20 73 65 65 6e 2e 0a 2a 2f 0a  rs are seen..*/.
2047d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2047e 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  nt sqlite3Resolv
2047f 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20  eOrderGroupBy(. 
20480 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
20481 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
20482 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c 65 61 76  g context.  Leav
20483 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  e error messages
20484 20 68 65 72 65 20 2a 2f 0a 20 20 53 65 6c 65 63   here */.  Selec
20485 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20  t *pSelect,     
20486 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
20487 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74 61 69 6e  tatement contain
20488 69 6e 67 20 74 68 65 20 63 6c 61 75 73 65 20 2a  ing the clause *
20489 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f  /.  ExprList *pO
2048a 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 54 68 65  rderBy,   /* The
2048b 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f   ORDER BY or GRO
2048c 55 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20  UP BY clause to 
2048d 62 65 20 70 72 6f 63 65 73 73 65 64 20 2a 2f 0a  be processed */.
2048e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2048f 79 70 65 20 20 20 20 20 2f 2a 20 22 4f 52 44 45  ype     /* "ORDE
20490 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20 2a 2f  R" or "GROUP" */
20491 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  .){.  int i;.  s
20492 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
20493 72 73 65 2d 3e 64 62 3b 0a 20 20 45 78 70 72 4c  rse->db;.  ExprL
20494 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73  ist *pEList;.  s
20495 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
20496 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69  tem *pItem;..  i
20497 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 7c  f( pOrderBy==0 |
20498 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  | pParse->db->ma
20499 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
2049a 75 72 6e 20 30 3b 0a 23 69 66 20 53 51 4c 49 54  urn 0;.#if SQLIT
2049b 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20 20 69  E_MAX_COLUMN.  i
2049c 66 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78  f( pOrderBy->nEx
2049d 70 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  pr>db->aLimit[SQ
2049e 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d  LITE_LIMIT_COLUM
2049f 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  N] ){.    sqlite
204a0 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
204a1 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d  , "too many term
204a2 73 20 69 6e 20 25 73 20 42 59 20 63 6c 61 75 73  s in %s BY claus
204a3 65 22 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20  e", zType);.    
204a4 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65  return 1;.  }.#e
204a5 6e 64 69 66 0a 20 20 70 45 4c 69 73 74 20 3d 20  ndif.  pEList = 
204a6 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
204a7 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73  .  assert( pELis
204a8 74 21 3d 30 20 29 3b 20 20 2f 2a 20 73 71 6c 69  t!=0 );  /* sqli
204a9 74 65 33 53 65 6c 65 63 74 4e 65 77 28 29 20 67  te3SelectNew() g
204aa 75 61 72 61 6e 74 65 65 73 20 74 68 69 73 20 2a  uarantees this *
204ab 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74  /.  for(i=0, pIt
204ac 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
204ad 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  i<pOrderBy->nExp
204ae 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  r; i++, pItem++)
204af 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d  {.    if( pItem-
204b0 3e 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69  >iCol ){.      i
204b1 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3e 70  f( pItem->iCol>p
204b2 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a  EList->nExpr ){.
204b3 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65 4f          resolveO
204b4 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28 70  utOfRangeError(p
204b5 50 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69 2b  Parse, zType, i+
204b6 31 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  1, pEList->nExpr
204b7 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
204b8 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
204b9 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 61 73 28     resolveAlias(
204ba 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20  pParse, pEList, 
204bb 70 49 74 65 6d 2d 3e 69 43 6f 6c 2d 31 2c 20 70  pItem->iCol-1, p
204bc 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 7a 54 79  Item->pExpr, zTy
204bd 70 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  pe);.    }.  }. 
204be 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
204bf 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20  .** pOrderBy is 
204c0 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47  an ORDER BY or G
204c1 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69  ROUP BY clause i
204c2 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  n SELECT stateme
204c3 6e 74 20 70 53 65 6c 65 63 74 2e 0a 2a 2a 20 54  nt pSelect..** T
204c4 68 65 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  he Name context 
204c5 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  of the SELECT st
204c6 61 74 65 6d 65 6e 74 20 69 73 20 70 4e 43 2e 20  atement is pNC. 
204c7 20 7a 54 79 70 65 20 69 73 20 65 69 74 68 65 72   zType is either
204c8 0a 2a 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22  .** "ORDER" or "
204c9 47 52 4f 55 50 22 20 64 65 70 65 6e 64 69 6e 67  GROUP" depending
204ca 20 6f 6e 20 77 68 69 63 68 20 74 79 70 65 20 6f   on which type o
204cb 66 20 63 6c 61 75 73 65 20 70 4f 72 64 65 72 42  f clause pOrderB
204cc 79 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  y is..**.** This
204cd 20 72 6f 75 74 69 6e 65 20 72 65 73 6f 6c 76 65   routine resolve
204ce 73 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74  s each term of t
204cf 68 65 20 63 6c 61 75 73 65 20 69 6e 74 6f 20 61  he clause into a
204d0 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  n expression..**
204d1 20 49 66 20 74 68 65 20 6f 72 64 65 72 2d 62 79   If the order-by
204d2 20 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74 65   term is an inte
204d3 67 65 72 20 49 20 62 65 74 77 65 65 6e 20 31 20  ger I between 1 
204d4 61 6e 64 20 4e 20 28 77 68 65 72 65 20 4e 20 69  and N (where N i
204d5 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  s the.** number 
204d6 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
204d7 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
204d8 74 68 65 20 53 45 4c 45 43 54 29 20 74 68 65 6e  the SELECT) then
204d9 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a   the expression.
204da 2a 2a 20 69 6e 20 74 68 65 20 72 65 73 6f 6c 75  ** in the resolu
204db 74 69 6f 6e 20 69 73 20 61 20 63 6f 70 79 20 6f  tion is a copy o
204dc 66 20 74 68 65 20 49 2d 74 68 20 72 65 73 75 6c  f the I-th resul
204dd 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t-set expression
204de 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 6f 72 64  .  If.** the ord
204df 65 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e  er-by term is an
204e0 20 69 64 65 6e 74 69 66 79 20 74 68 61 74 20 63   identify that c
204e1 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68  orresponds to th
204e2 65 20 41 53 2d 6e 61 6d 65 20 6f 66 0a 2a 2a 20  e AS-name of.** 
204e3 61 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70  a result-set exp
204e4 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 20 74 68  ression, then th
204e5 65 20 74 65 72 6d 20 72 65 73 6f 6c 76 65 73 20  e term resolves 
204e6 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  to a copy of the
204e7 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65  .** result-set e
204e8 78 70 72 65 73 73 69 6f 6e 2e 20 20 4f 74 68 65  xpression.  Othe
204e9 72 77 69 73 65 2c 20 74 68 65 20 65 78 70 72 65  rwise, the expre
204ea 73 73 69 6f 6e 20 69 73 20 72 65 73 6f 6c 76 65  ssion is resolve
204eb 64 20 69 6e 0a 2a 2a 20 74 68 65 20 75 73 75 61  d in.** the usua
204ec 6c 20 77 61 79 20 2d 20 75 73 69 6e 67 20 73 71  l way - using sq
204ed 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
204ee 4e 61 6d 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54  Names()..**.** T
204ef 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
204f0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
204f1 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20 65 72  f errors.  If er
204f2 72 6f 72 73 20 6f 63 63 75 72 2c 20 74 68 65 6e  rors occur, then
204f3 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69 61  .** an appropria
204f4 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
204f5 20 6d 69 67 68 74 20 62 65 20 6c 65 66 74 20 69   might be left i
204f6 6e 20 70 50 61 72 73 65 2e 20 20 28 4f 4f 4d 20  n pParse.  (OOM 
204f7 65 72 72 6f 72 73 0a 2a 2a 20 65 78 63 65 70 74  errors.** except
204f8 65 64 2e 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ed.).*/.static i
204f9 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 65 72 47  nt resolveOrderG
204fa 72 6f 75 70 42 79 28 0a 20 20 4e 61 6d 65 43 6f  roupBy(.  NameCo
204fb 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20 20  ntext *pNC,     
204fc 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74  /* The name cont
204fd 65 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43  ext of the SELEC
204fe 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  T statement */. 
204ff 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
20500 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45  ,      /* The SE
20501 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 68  LECT statement h
20502 6f 6c 64 69 6e 67 20 70 4f 72 64 65 72 42 79 20  olding pOrderBy 
20503 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
20504 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a 20 41 6e  OrderBy,   /* An
20505 20 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f   ORDER BY or GRO
20506 55 50 20 42 59 20 63 6c 61 75 73 65 20 74 6f 20  UP BY clause to 
20507 72 65 73 6f 6c 76 65 20 2a 2f 0a 20 20 63 6f 6e  resolve */.  con
20508 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20  st char *zType  
20509 20 20 20 2f 2a 20 45 69 74 68 65 72 20 22 4f 52     /* Either "OR
2050a 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 2c  DER" or "GROUP",
2050b 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20   as appropriate 
2050c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20  */.){.  int i;  
2050d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2050e 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
2050f 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
20510 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  iCol;           
20511 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
20512 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  lumn number */. 
20513 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
20514 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20  _item *pItem;   
20515 2f 2a 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65  /* A term of the
20516 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
20517 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61   */.  Parse *pPa
20518 72 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  rse;            
20519 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2051a 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
2051b 20 6e 52 65 73 75 6c 74 3b 20 20 20 20 20 20 20   nResult;       
2051c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2051d 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69  umber of terms i
2051e 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
2051f 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4f 72 64 65   */..  if( pOrde
20520 72 42 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  rBy==0 ) return 
20521 30 3b 0a 20 20 6e 52 65 73 75 6c 74 20 3d 20 70  0;.  nResult = p
20522 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e  Select->pEList->
20523 6e 45 78 70 72 3b 0a 20 20 70 50 61 72 73 65 20  nExpr;.  pParse 
20524 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20  = pNC->pParse;. 
20525 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d   for(i=0, pItem=
20526 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70  pOrderBy->a; i<p
20527 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20  OrderBy->nExpr; 
20528 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
20529 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 49     Expr *pE = pI
2052a 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  tem->pExpr;.    
2052b 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65 41 73  iCol = resolveAs
2052c 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 53 65  Name(pParse, pSe
2052d 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2c 20 70 45  lect->pEList, pE
2052e 29 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e  );.    if( iCol>
2052f 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  0 ){.      /* If
20530 20 61 6e 20 41 53 2d 6e 61 6d 65 20 6d 61 74 63   an AS-name matc
20531 68 20 69 73 20 66 6f 75 6e 64 2c 20 6d 61 72 6b  h is found, mark
20532 20 74 68 69 73 20 4f 52 44 45 52 20 42 59 20 63   this ORDER BY c
20533 6f 6c 75 6d 6e 20 61 73 20 62 65 69 6e 67 0a 20  olumn as being. 
20534 20 20 20 20 20 2a 2a 20 61 20 63 6f 70 79 20 6f       ** a copy o
20535 66 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65  f the iCol-th re
20536 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 2e  sult-set column.
20537 20 20 54 68 65 20 73 75 62 73 65 71 75 65 6e 74    The subsequent
20538 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20 2a   call to.      *
20539 2a 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  * sqlite3Resolve
2053a 4f 72 64 65 72 47 72 6f 75 70 42 79 28 29 20 77  OrderGroupBy() w
2053b 69 6c 6c 20 63 6f 6e 76 65 72 74 20 74 68 65 20  ill convert the 
2053c 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 61 0a  expression to a.
2053d 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f 66        ** copy of
2053e 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 72 65 73   the iCol-th res
2053f 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69  ult-set expressi
20540 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 74  on. */.      pIt
20541 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36 29  em->iCol = (u16)
20542 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74  iCol;.      cont
20543 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  inue;.    }.    
20544 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49  if( sqlite3ExprI
20545 73 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43  sInteger(pE, &iC
20546 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ol) ){.      /* 
20547 54 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72  The ORDER BY ter
20548 6d 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20  m is an integer 
20549 63 6f 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 6e  constant.  Again
2054a 2c 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e  , set the column
2054b 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72  .      ** number
2054c 20 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33   so that sqlite3
2054d 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75  ResolveOrderGrou
2054e 70 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65  pBy() will conve
2054f 72 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  rt the.      ** 
20550 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20 74 6f  order-by term to
20551 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72   a copy of the r
20552 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73  esult-set expres
20553 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66  sion */.      if
20554 28 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20  ( iCol<1 ){.    
20555 20 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66      resolveOutOf
20556 52 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73  RangeError(pPars
20557 65 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e  e, zType, i+1, n
20558 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20  Result);.       
20559 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
2055a 20 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e   }.      pItem->
2055b 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 43 6f 6c  iCol = (u16)iCol
2055c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  ;.      continue
2055d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2055e 4f 74 68 65 72 77 69 73 65 2c 20 74 72 65 61 74  Otherwise, treat
2055f 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 74 65   the ORDER BY te
20560 72 6d 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72  rm as an ordinar
20561 79 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  y expression */.
20562 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20      pItem->iCol 
20563 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  = 0;.    if( sql
20564 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
20565 61 6d 65 73 28 70 4e 43 2c 20 70 45 29 20 29 7b  ames(pNC, pE) ){
20566 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
20567 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
20568 75 72 6e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  urn sqlite3Resol
20569 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70  veOrderGroupBy(p
2056a 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20  Parse, pSelect, 
2056b 70 4f 72 64 65 72 42 79 2c 20 7a 54 79 70 65 29  pOrderBy, zType)
2056c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c  ;.}../*.** Resol
2056d 76 65 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20  ve names in the 
2056e 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2056f 20 70 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74   p and all of it
20570 73 20 64 65 73 63 65 6e 64 65 6e 74 73 2e 0a 2a  s descendents..*
20571 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73  /.static int res
20572 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 28 57  olveSelectStep(W
20573 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20  alker *pWalker, 
20574 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 4e 61  Select *p){.  Na
20575 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65  meContext *pOute
20576 72 4e 43 3b 20 20 2f 2a 20 43 6f 6e 74 65 78 74  rNC;  /* Context
20577 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
20578 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  his SELECT */.  
20579 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b  NameContext sNC;
2057a 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2057b 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 69 73 20  context of this 
2057c 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20  SELECT */.  int 
2057d 69 73 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20  isCompound;     
2057e 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
2057f 20 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73   is a compound s
20580 65 6c 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  elect */.  int n
20581 43 6f 6d 70 6f 75 6e 64 3b 20 20 20 20 20 20 20  Compound;       
20582 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
20583 63 6f 6d 70 6f 75 6e 64 20 74 65 72 6d 73 20 70  compound terms p
20584 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 20  rocessed so far 
20585 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  */.  Parse *pPar
20586 73 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se;          /* 
20587 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
20588 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
20589 45 4c 69 73 74 3b 20 20 20 20 20 20 20 2f 2a 20  EList;       /* 
2058a 52 65 73 75 6c 74 20 73 65 74 20 65 78 70 72 65  Result set expre
2058b 73 73 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20  ssion list */.  
2058c 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
2058d 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2058e 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70  counter */.  Exp
2058f 72 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 3b  rList *pGroupBy;
20590 20 20 20 20 20 2f 2a 20 54 68 65 20 47 52 4f 55       /* The GROU
20591 50 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  P BY clause */. 
20592 20 53 65 6c 65 63 74 20 2a 70 4c 65 66 74 6d 6f   Select *pLeftmo
20593 73 74 3b 20 20 20 20 20 20 2f 2a 20 4c 65 66 74  st;      /* Left
20594 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 20  -most of SELECT 
20595 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f  of a compound */
20596 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
20597 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
20598 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
20599 6e 20 2a 2f 0a 20 20 0a 0a 20 20 61 73 73 65 72  n */.  ..  asser
2059a 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66 28  t( p!=0 );.  if(
2059b 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53   p->selFlags & S
2059c 46 5f 52 65 73 6f 6c 76 65 64 20 29 7b 0a 20 20  F_Resolved ){.  
2059d 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
2059e 6e 65 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 65 72  ne;.  }.  pOuter
2059f 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e  NC = pWalker->u.
205a0 70 4e 43 3b 0a 20 20 70 50 61 72 73 65 20 3d 20  pNC;.  pParse = 
205a1 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 3b  pWalker->pParse;
205a2 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
205a3 64 62 3b 0a 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c  db;..  /* Normal
205a4 6c 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  ly sqlite3Select
205a5 45 78 70 61 6e 64 28 29 20 77 69 6c 6c 20 62 65  Expand() will be
205a6 20 63 61 6c 6c 65 64 20 66 69 72 73 74 20 61 6e   called first an
205a7 64 20 77 69 6c 6c 20 68 61 76 65 0a 20 20 2a 2a  d will have.  **
205a8 20 61 6c 72 65 61 64 79 20 65 78 70 61 6e 64 65   already expande
205a9 64 20 74 68 69 73 20 53 45 4c 45 43 54 2e 20 20  d this SELECT.  
205aa 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 69 73  However, if this
205ab 20 69 73 20 61 20 73 75 62 71 75 65 72 79 20 77   is a subquery w
205ac 69 74 68 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 78  ithin.  ** an ex
205ad 70 72 65 73 73 69 6f 6e 2c 20 73 71 6c 69 74 65  pression, sqlite
205ae 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
205af 73 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  s() will be call
205b0 65 64 20 77 69 74 68 6f 75 74 20 61 0a 20 20 2a  ed without a.  *
205b1 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  * prior call to 
205b2 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
205b3 61 6e 64 28 29 2e 20 20 57 68 65 6e 20 74 68 61  and().  When tha
205b4 74 20 68 61 70 70 65 6e 73 2c 20 6c 65 74 0a 20  t happens, let. 
205b5 20 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65 63   ** sqlite3Selec
205b6 74 50 72 65 70 28 29 20 64 6f 20 61 6c 6c 20 6f  tPrep() do all o
205b7 66 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67  f the processing
205b8 20 66 6f 72 20 74 68 69 73 20 53 45 4c 45 43 54   for this SELECT
205b9 2e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 53 65  ..  ** sqlite3Se
205ba 6c 65 63 74 50 72 65 70 28 29 20 77 69 6c 6c 20  lectPrep() will 
205bb 69 6e 76 6f 6b 65 20 62 6f 74 68 20 73 71 6c 69  invoke both sqli
205bc 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28  te3SelectExpand(
205bd 29 20 61 6e 64 0a 20 20 2a 2a 20 74 68 69 73 20  ) and.  ** this 
205be 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 65 20 63  routine in the c
205bf 6f 72 72 65 63 74 20 6f 72 64 65 72 2e 0a 20 20  orrect order..  
205c0 2a 2f 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c  */.  if( (p->sel
205c1 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e  Flags & SF_Expan
205c2 64 65 64 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73  ded)==0 ){.    s
205c3 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70  qlite3SelectPrep
205c4 28 70 50 61 72 73 65 2c 20 70 2c 20 70 4f 75 74  (pParse, p, pOut
205c5 65 72 4e 43 29 3b 0a 20 20 20 20 72 65 74 75 72  erNC);.    retur
205c6 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  n (pParse->nErr 
205c7 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
205c8 6c 65 64 29 20 3f 20 57 52 43 5f 41 62 6f 72 74  led) ? WRC_Abort
205c9 20 3a 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20   : WRC_Prune;.  
205ca 7d 0a 0a 20 20 69 73 43 6f 6d 70 6f 75 6e 64 20  }..  isCompound 
205cb 3d 20 70 2d 3e 70 50 72 69 6f 72 21 3d 30 3b 0a  = p->pPrior!=0;.
205cc 20 20 6e 43 6f 6d 70 6f 75 6e 64 20 3d 20 30 3b    nCompound = 0;
205cd 0a 20 20 70 4c 65 66 74 6d 6f 73 74 20 3d 20 70  .  pLeftmost = p
205ce 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a  ;.  while( p ){.
205cf 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e      assert( (p->
205d0 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78  selFlags & SF_Ex
205d1 70 61 6e 64 65 64 29 21 3d 30 20 29 3b 0a 20 20  panded)!=0 );.  
205d2 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73 65    assert( (p->se
205d3 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f  lFlags & SF_Reso
205d4 6c 76 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20  lved)==0 );.    
205d5 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53  p->selFlags |= S
205d6 46 5f 52 65 73 6f 6c 76 65 64 3b 0a 0a 20 20 20  F_Resolved;..   
205d7 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20   /* Resolve the 
205d8 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74  expressions in t
205d9 68 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46  he LIMIT and OFF
205da 53 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68 65  SET clauses. The
205db 73 65 0a 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f  se.    ** are no
205dc 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 72 65 66  t allowed to ref
205dd 65 72 20 74 6f 20 61 6e 79 20 6e 61 6d 65 73 2c  er to any names,
205de 20 73 6f 20 70 61 73 73 20 61 6e 20 65 6d 70 74   so pass an empt
205df 79 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 0a 20  y NameContext.. 
205e0 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65 74     */.    memset
205e1 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66  (&sNC, 0, sizeof
205e2 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e  (sNC));.    sNC.
205e3 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
205e4 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
205e5 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
205e6 28 26 73 4e 43 2c 20 70 2d 3e 70 4c 69 6d 69 74  (&sNC, p->pLimit
205e7 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ||.        sql
205e8 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
205e9 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 4f  ames(&sNC, p->pO
205ea 66 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20  ffset) ){.      
205eb 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
205ec 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
205ed 2a 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f 63  * Set up the loc
205ee 61 6c 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20  al name-context 
205ef 74 6f 20 70 61 73 73 20 74 6f 20 73 71 6c 69 74  to pass to sqlit
205f0 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
205f1 65 73 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 72  es() to.    ** r
205f2 65 73 6f 6c 76 65 20 74 68 65 20 72 65 73 75 6c  esolve the resul
205f3 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t-set expression
205f4 20 6c 69 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20   list..    */.  
205f5 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d    sNC.allowAgg =
205f6 20 31 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63   1;.    sNC.pSrc
205f7 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a  List = p->pSrc;.
205f8 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20      sNC.pNext = 
205f9 70 4f 75 74 65 72 4e 43 3b 0a 20 20 0a 20 20 20  pOuterNC;.  .   
205fa 20 2f 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d 65   /* Resolve name
205fb 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
205fc 73 65 74 2e 20 2a 2f 0a 20 20 20 20 70 45 4c 69  set. */.    pELi
205fd 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a  st = p->pEList;.
205fe 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69      assert( pELi
205ff 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72  st!=0 );.    for
20600 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e  (i=0; i<pEList->
20601 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
20602 20 20 20 45 78 70 72 20 2a 70 58 20 3d 20 70 45     Expr *pX = pE
20603 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  List->a[i].pExpr
20604 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
20605 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
20606 6d 65 73 28 26 73 4e 43 2c 20 70 58 29 20 29 7b  mes(&sNC, pX) ){
20607 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
20608 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20  WRC_Abort;.     
20609 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
2060a 2f 2a 20 52 65 63 75 72 73 69 76 65 6c 79 20 72  /* Recursively r
2060b 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20  esolve names in 
2060c 61 6c 6c 20 73 75 62 71 75 65 72 69 65 73 0a 20  all subqueries. 
2060d 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d     */.    for(i=
2060e 30 3b 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53  0; i<p->pSrc->nS
2060f 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  rc; i++){.      
20610 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
20611 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d  tem *pItem = &p-
20612 3e 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20  >pSrc->a[i];.   
20613 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53     if( pItem->pS
20614 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
20615 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 61   const char *zSa
20616 76 65 64 43 6f 6e 74 65 78 74 20 3d 20 70 50 61  vedContext = pPa
20617 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
20618 74 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  t;.        if( p
20619 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 29 20 70 50  Item->zName ) pP
2061a 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
2061b 78 74 20 3d 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  xt = pItem->zNam
2061c 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e;.        sqlit
2061d 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e  e3ResolveSelectN
2061e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 49 74  ames(pParse, pIt
2061f 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 70 4f 75  em->pSelect, pOu
20620 74 65 72 4e 43 29 3b 0a 20 20 20 20 20 20 20 20  terNC);.        
20621 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
20622 74 65 78 74 20 3d 20 7a 53 61 76 65 64 43 6f 6e  text = zSavedCon
20623 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20 69 66  text;.        if
20624 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
20625 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
20626 65 64 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  ed ) return WRC_
20627 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20  Abort;.      }. 
20628 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49     }.  .    /* I
20629 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 61  f there are no a
2062a 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
2062b 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
2062c 2d 73 65 74 2c 20 61 6e 64 20 6e 6f 20 47 52 4f  -set, and no GRO
2062d 55 50 20 42 59 20 0a 20 20 20 20 2a 2a 20 65 78  UP BY .    ** ex
2062e 70 72 65 73 73 69 6f 6e 2c 20 64 6f 20 6e 6f 74  pression, do not
2062f 20 61 6c 6c 6f 77 20 61 67 67 72 65 67 61 74 65   allow aggregate
20630 73 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20  s in any of the 
20631 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  other expression
20632 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  s..    */.    as
20633 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61  sert( (p->selFla
20634 67 73 20 26 20 53 46 5f 41 67 67 72 65 67 61 74  gs & SF_Aggregat
20635 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70 47 72  e)==0 );.    pGr
20636 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75  oupBy = p->pGrou
20637 70 42 79 3b 0a 20 20 20 20 69 66 28 20 70 47 72  pBy;.    if( pGr
20638 6f 75 70 42 79 20 7c 7c 20 73 4e 43 2e 68 61 73  oupBy || sNC.has
20639 41 67 67 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  Agg ){.      p->
2063a 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 41  selFlags |= SF_A
2063b 67 67 72 65 67 61 74 65 3b 0a 20 20 20 20 7d 65  ggregate;.    }e
2063c 6c 73 65 7b 0a 20 20 20 20 20 20 73 4e 43 2e 61  lse{.      sNC.a
2063d 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20 20  llowAgg = 0;.   
2063e 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66 20   }.  .    /* If 
2063f 61 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20  a HAVING clause 
20640 69 73 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e  is present, then
20641 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   there must be a
20642 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
20643 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
20644 20 70 2d 3e 70 48 61 76 69 6e 67 20 26 26 20 21   p->pHaving && !
20645 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20  pGroupBy ){.    
20646 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
20647 67 28 70 50 61 72 73 65 2c 20 22 61 20 47 52 4f  g(pParse, "a GRO
20648 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  UP BY clause is 
20649 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65 20  required before 
2064a 48 41 56 49 4e 47 22 29 3b 0a 20 20 20 20 20 20  HAVING");.      
2064b 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2064c 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
2064d 2a 20 41 64 64 20 74 68 65 20 65 78 70 72 65 73  * Add the expres
2064e 73 69 6f 6e 20 6c 69 73 74 20 74 6f 20 74 68 65  sion list to the
2064f 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74 20 62 65   name-context be
20650 66 6f 72 65 20 70 61 72 73 69 6e 67 20 74 68 65  fore parsing the
20651 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 65 78  .    ** other ex
20652 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  pressions in the
20653 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
20654 74 2e 20 54 68 69 73 20 69 73 20 73 6f 20 74 68  t. This is so th
20655 61 74 0a 20 20 20 20 2a 2a 20 65 78 70 72 65 73  at.    ** expres
20656 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45  sions in the WHE
20657 52 45 20 63 6c 61 75 73 65 20 28 65 74 63 2e 29  RE clause (etc.)
20658 20 63 61 6e 20 72 65 66 65 72 20 74 6f 20 65 78   can refer to ex
20659 70 72 65 73 73 69 6f 6e 73 20 62 79 0a 20 20 20  pressions by.   
2065a 20 2a 2a 20 61 6c 69 61 73 65 73 20 69 6e 20 74   ** aliases in t
2065b 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 20  he result set.. 
2065c 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4d 69 6e     **.    ** Min
2065d 6f 72 20 70 6f 69 6e 74 3a 20 49 66 20 74 68 69  or point: If thi
2065e 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  s is the case, t
2065f 68 65 6e 20 74 68 65 20 65 78 70 72 65 73 73 69  hen the expressi
20660 6f 6e 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a  on will be.    *
20661 2a 20 72 65 2d 65 76 61 6c 75 61 74 65 64 20 66  * re-evaluated f
20662 6f 72 20 65 61 63 68 20 72 65 66 65 72 65 6e 63  or each referenc
20663 65 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a 2f 0a  e to it..    */.
20664 20 20 20 20 73 4e 43 2e 70 45 4c 69 73 74 20 3d      sNC.pEList =
20665 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20   p->pEList;.    
20666 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  if( sqlite3Resol
20667 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43  veExprNames(&sNC
20668 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 7c 7c 0a  , p->pWhere) ||.
20669 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
2066a 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26  solveExprNames(&
2066b 73 4e 43 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29  sNC, p->pHaving)
2066c 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65  .    ){.      re
2066d 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
2066e 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68      }..    /* Th
2066f 65 20 4f 52 44 45 52 20 42 59 20 61 6e 64 20 47  e ORDER BY and G
20670 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 73 20  ROUP BY clauses 
20671 6d 61 79 20 6e 6f 74 20 72 65 66 65 72 20 74 6f  may not refer to
20672 20 74 65 72 6d 73 20 69 6e 0a 20 20 20 20 2a 2a   terms in.    **
20673 20 6f 75 74 65 72 20 71 75 65 72 69 65 73 20 0a   outer queries .
20674 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70      */.    sNC.p
20675 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 73 4e  Next = 0;.    sN
20676 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a  C.allowAgg = 1;.
20677 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 20  .    /* Process 
20678 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
20679 75 73 65 20 66 6f 72 20 73 69 6e 67 6c 65 74 6f  use for singleto
2067a 6e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  n SELECT stateme
2067b 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20  nts..    ** The 
2067c 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2067d 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 73 20 53 45  for compounds SE
2067e 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
2067f 69 73 20 68 61 6e 64 6c 65 64 0a 20 20 20 20 2a  is handled.    *
20680 2a 20 62 65 6c 6f 77 2c 20 61 66 74 65 72 20 61  * below, after a
20681 6c 6c 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  ll of the result
20682 2d 73 65 74 73 20 66 6f 72 20 61 6c 6c 20 6f 66  -sets for all of
20683 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66   the elements of
20684 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70  .    ** the comp
20685 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72  ound have been r
20686 65 73 6f 6c 76 65 64 2e 0a 20 20 20 20 2a 2f 0a  esolved..    */.
20687 20 20 20 20 69 66 28 20 21 69 73 43 6f 6d 70 6f      if( !isCompo
20688 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 4f 72  und && resolveOr
20689 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43 2c  derGroupBy(&sNC,
2068a 20 70 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c   p, p->pOrderBy,
2068b 20 22 4f 52 44 45 52 22 29 20 29 7b 0a 20 20 20   "ORDER") ){.   
2068c 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
2068d 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ort;.    }.    i
2068e 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2068f 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 72 65 74  led ){.      ret
20690 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
20691 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52     }.  .    /* R
20692 65 73 6f 6c 76 65 20 74 68 65 20 47 52 4f 55 50  esolve the GROUP
20693 20 42 59 20 63 6c 61 75 73 65 2e 20 20 41 74 20   BY clause.  At 
20694 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 6d  the same time, m
20695 61 6b 65 20 73 75 72 65 20 0a 20 20 20 20 2a 2a  ake sure .    **
20696 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c   the GROUP BY cl
20697 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63 6f  ause does not co
20698 6e 74 61 69 6e 20 61 67 67 72 65 67 61 74 65 20  ntain aggregate 
20699 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 20 20 2a  functions..    *
2069a 2f 0a 20 20 20 20 69 66 28 20 70 47 72 6f 75 70  /.    if( pGroup
2069b 42 79 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75  By ){.      stru
2069c 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
2069d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 0a 20 20   *pItem;.    .  
2069e 20 20 20 20 69 66 28 20 72 65 73 6f 6c 76 65 4f      if( resolveO
2069f 72 64 65 72 47 72 6f 75 70 42 79 28 26 73 4e 43  rderGroupBy(&sNC
206a0 2c 20 70 2c 20 70 47 72 6f 75 70 42 79 2c 20 22  , p, pGroupBy, "
206a1 47 52 4f 55 50 22 29 20 7c 7c 20 64 62 2d 3e 6d  GROUP") || db->m
206a2 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
206a3 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52         return WR
206a4 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d  C_Abort;.      }
206a5 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  .      for(i=0, 
206a6 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e  pItem=pGroupBy->
206a7 61 3b 20 69 3c 70 47 72 6f 75 70 42 79 2d 3e 6e  a; i<pGroupBy->n
206a8 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
206a9 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
206aa 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
206ab 28 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 45  (pItem->pExpr, E
206ac 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20  P_Agg) ){.      
206ad 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
206ae 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 67 67  Msg(pParse, "agg
206af 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
206b0 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64   are not allowed
206b1 20 69 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20   in ".          
206b2 20 20 20 20 22 74 68 65 20 47 52 4f 55 50 20 42      "the GROUP B
206b3 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20  Y clause");.    
206b4 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
206b5 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 20 20  _Abort;.        
206b6 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
206b7 0a 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20  .    /* Advance 
206b8 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d  to the next term
206b9 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64   of the compound
206ba 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d 20  .    */.    p = 
206bb 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 6e  p->pPrior;.    n
206bc 43 6f 6d 70 6f 75 6e 64 2b 2b 3b 0a 20 20 7d 0a  Compound++;.  }.
206bd 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68  .  /* Resolve th
206be 65 20 4f 52 44 45 52 20 42 59 20 6f 6e 20 61 20  e ORDER BY on a 
206bf 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20  compound SELECT 
206c0 61 66 74 65 72 20 61 6c 6c 20 74 65 72 6d 73 20  after all terms 
206c1 6f 66 0a 20 20 2a 2a 20 74 68 65 20 63 6f 6d 70  of.  ** the comp
206c2 6f 75 6e 64 20 68 61 76 65 20 62 65 65 6e 20 72  ound have been r
206c3 65 73 6f 6c 76 65 64 2e 0a 20 20 2a 2f 0a 20 20  esolved..  */.  
206c4 69 66 28 20 69 73 43 6f 6d 70 6f 75 6e 64 20 26  if( isCompound &
206c5 26 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75 6e  & resolveCompoun
206c6 64 4f 72 64 65 72 42 79 28 70 50 61 72 73 65 2c  dOrderBy(pParse,
206c7 20 70 4c 65 66 74 6d 6f 73 74 29 20 29 7b 0a 20   pLeftmost) ){. 
206c8 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
206c9 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ort;.  }..  retu
206ca 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 7d 0a  rn WRC_Prune;.}.
206cb 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
206cc 69 6e 65 20 77 61 6c 6b 73 20 61 6e 20 65 78 70  ine walks an exp
206cd 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64  ression tree and
206ce 20 72 65 73 6f 6c 76 65 73 20 72 65 66 65 72 65   resolves refere
206cf 6e 63 65 73 20 74 6f 0a 2a 2a 20 74 61 62 6c 65  nces to.** table
206d0 20 63 6f 6c 75 6d 6e 73 20 61 6e 64 20 72 65 73   columns and res
206d1 75 6c 74 2d 73 65 74 20 63 6f 6c 75 6d 6e 73 2e  ult-set columns.
206d2 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 69    At the same ti
206d3 6d 65 2c 20 64 6f 20 65 72 72 6f 72 0a 2a 2a 20  me, do error.** 
206d4 63 68 65 63 6b 69 6e 67 20 6f 6e 20 66 75 6e 63  checking on func
206d5 74 69 6f 6e 20 75 73 61 67 65 20 61 6e 64 20 73  tion usage and s
206d6 65 74 20 61 20 66 6c 61 67 20 69 66 20 61 6e 79  et a flag if any
206d7 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
206d8 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 73 65 65 6e  ions.** are seen
206d9 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c  ..**.** To resol
206da 76 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73  ve table columns
206db 20 72 65 66 65 72 65 6e 63 65 73 20 77 65 20 6c   references we l
206dc 6f 6f 6b 20 66 6f 72 20 6e 6f 64 65 73 20 28 6f  ook for nodes (o
206dd 72 20 73 75 62 74 72 65 65 73 29 20 6f 66 20 74  r subtrees) of t
206de 68 65 20 0a 2a 2a 20 66 6f 72 6d 20 58 2e 59 2e  he .** form X.Y.
206df 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73 74  Z or Y.Z or just
206e0 20 5a 20 77 68 65 72 65 0a 2a 2a 0a 2a 2a 20 20   Z where.**.**  
206e1 20 20 20 20 58 3a 20 20 20 54 68 65 20 6e 61 6d      X:   The nam
206e2 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e  e of a database.
206e3 20 20 45 78 3a 20 20 22 6d 61 69 6e 22 20 6f 72    Ex:  "main" or
206e4 20 22 74 65 6d 70 22 20 6f 72 0a 2a 2a 20 20 20   "temp" or.**   
206e5 20 20 20 20 20 20 20 20 74 68 65 20 73 79 6d 62          the symb
206e6 6f 6c 69 63 20 6e 61 6d 65 20 61 73 73 69 67 6e  olic name assign
206e7 65 64 20 74 6f 20 61 6e 20 41 54 54 41 43 48 2d  ed to an ATTACH-
206e8 65 64 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  ed database..**.
206e9 2a 2a 20 20 20 20 20 20 59 3a 20 20 20 54 68 65  **      Y:   The
206ea 20 6e 61 6d 65 20 6f 66 20 61 20 74 61 62 6c 65   name of a table
206eb 20 69 6e 20 61 20 46 52 4f 4d 20 63 6c 61 75 73   in a FROM claus
206ec 65 2e 20 20 4f 72 20 69 6e 20 61 20 74 72 69 67  e.  Or in a trig
206ed 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ger.**          
206ee 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70 65 63   one of the spec
206ef 69 61 6c 20 6e 61 6d 65 73 20 22 6f 6c 64 22 20  ial names "old" 
206f0 6f 72 20 22 6e 65 77 22 2e 0a 2a 2a 0a 2a 2a 20  or "new"..**.** 
206f1 20 20 20 20 20 5a 3a 20 20 20 54 68 65 20 6e 61       Z:   The na
206f2 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69  me of a column i
206f3 6e 20 74 61 62 6c 65 20 59 2e 0a 2a 2a 0a 2a 2a  n table Y..**.**
206f4 20 54 68 65 20 6e 6f 64 65 20 61 74 20 74 68 65   The node at the
206f5 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 73 75 62   root of the sub
206f6 74 72 65 65 20 69 73 20 6d 6f 64 69 66 69 65 64  tree is modified
206f7 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
206f8 2a 2a 20 20 20 20 45 78 70 72 2e 6f 70 20 20 20  **    Expr.op   
206f9 20 20 20 20 20 43 68 61 6e 67 65 64 20 74 6f 20       Changed to 
206fa 54 4b 5f 43 4f 4c 55 4d 4e 0a 2a 2a 20 20 20 20  TK_COLUMN.**    
206fb 45 78 70 72 2e 70 54 61 62 20 20 20 20 20 20 50  Expr.pTab      P
206fc 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62  oints to the Tab
206fd 6c 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 58 2e  le object for X.
206fe 59 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 43 6f  Y.**    Expr.iCo
206ff 6c 75 6d 6e 20 20 20 54 68 65 20 63 6f 6c 75 6d  lumn   The colum
20700 6e 20 69 6e 64 65 78 20 69 6e 20 58 2e 59 2e 20  n index in X.Y. 
20701 20 2d 31 20 66 6f 72 20 74 68 65 20 72 6f 77 69   -1 for the rowi
20702 64 2e 0a 2a 2a 20 20 20 20 45 78 70 72 2e 69 54  d..**    Expr.iT
20703 61 62 6c 65 20 20 20 20 54 68 65 20 56 44 42 45  able    The VDBE
20704 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66   cursor number f
20705 6f 72 20 58 2e 59 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  or X.Y.**.**.** 
20706 54 6f 20 72 65 73 6f 6c 76 65 20 72 65 73 75 6c  To resolve resul
20707 74 2d 73 65 74 20 72 65 66 65 72 65 6e 63 65 73  t-set references
20708 2c 20 6c 6f 6f 6b 20 66 6f 72 20 65 78 70 72 65  , look for expre
20709 73 73 69 6f 6e 20 6e 6f 64 65 73 20 6f 66 20 74  ssion nodes of t
2070a 68 65 0a 2a 2a 20 66 6f 72 6d 20 5a 20 28 77 69  he.** form Z (wi
2070b 74 68 20 6e 6f 20 58 20 61 6e 64 20 59 20 70 72  th no X and Y pr
2070c 65 66 69 78 29 20 77 68 65 72 65 20 74 68 65 20  efix) where the 
2070d 5a 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 69  Z matches the ri
2070e 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73 69 7a 65  ght-hand.** size
2070f 20 6f 66 20 61 6e 20 41 53 20 63 6c 61 75 73 65   of an AS clause
20710 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 2d 73   in the result-s
20711 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 2e 20  et of a SELECT. 
20712 20 54 68 65 20 5a 20 65 78 70 72 65 73 73 69 6f   The Z expressio
20713 6e 0a 2a 2a 20 69 73 20 72 65 70 6c 61 63 65 64  n.** is replaced
20714 20 62 79 20 61 20 63 6f 70 79 20 6f 66 20 74 68   by a copy of th
20715 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65  e left-hand side
20716 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 2d 73   of the result-s
20717 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  et expression..*
20718 2a 20 54 61 62 6c 65 2d 6e 61 6d 65 20 61 6e 64  * Table-name and
20719 20 66 75 6e 63 74 69 6f 6e 20 72 65 73 6f 6c 75   function resolu
2071a 74 69 6f 6e 20 6f 63 63 75 72 73 20 6f 6e 20 74  tion occurs on t
2071b 68 65 20 73 75 62 73 74 69 74 75 74 65 64 20 65  he substituted e
2071c 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65  xpression.** tre
2071d 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  e.  For example,
2071e 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   in:.**.**      
2071f 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 2c  SELECT a+b AS x,
20720 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20 74   c+d AS y FROM t
20721 31 20 4f 52 44 45 52 20 42 59 20 78 3b 0a 2a 2a  1 ORDER BY x;.**
20722 0a 2a 2a 20 54 68 65 20 22 78 22 20 74 65 72 6d  .** The "x" term
20723 20 6f 66 20 74 68 65 20 6f 72 64 65 72 20 62 79   of the order by
20724 20 69 73 20 72 65 70 6c 61 63 65 64 20 62 79 20   is replaced by 
20725 22 61 2b 62 22 20 74 6f 20 72 65 6e 64 65 72 3a  "a+b" to render:
20726 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 53 45 4c 45  .**.**      SELE
20727 43 54 20 61 2b 62 20 41 53 20 78 2c 20 63 2b 64  CT a+b AS x, c+d
20728 20 41 53 20 79 20 46 52 4f 4d 20 74 31 20 4f 52   AS y FROM t1 OR
20729 44 45 52 20 42 59 20 61 2b 62 3b 0a 2a 2a 0a 2a  DER BY a+b;.**.*
2072a 2a 20 46 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73  * Function calls
2072b 20 61 72 65 20 63 68 65 63 6b 65 64 20 74 6f 20   are checked to 
2072c 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
2072d 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 0a  he function is .
2072e 2a 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 74  ** defined and t
2072f 68 61 74 20 74 68 65 20 63 6f 72 72 65 63 74 20  hat the correct 
20730 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
20731 6e 74 73 20 61 72 65 20 73 70 65 63 69 66 69 65  nts are specifie
20732 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66 75 6e  d..** If the fun
20733 63 74 69 6f 6e 20 69 73 20 61 6e 20 61 67 67 72  ction is an aggr
20734 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20  egate function, 
20735 74 68 65 6e 20 74 68 65 20 70 4e 43 2d 3e 68 61  then the pNC->ha
20736 73 41 67 67 20 69 73 0a 2a 2a 20 73 65 74 20 61  sAgg is.** set a
20737 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 20 69 73  nd the opcode is
20738 20 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 54 4b   changed from TK
20739 5f 46 55 4e 43 54 49 4f 4e 20 74 6f 20 54 4b 5f  _FUNCTION to TK_
2073a 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e 0a 2a 2a  AGG_FUNCTION..**
2073b 20 49 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f   If an expressio
2073c 6e 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65  n contains aggre
2073d 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 74  gate functions t
2073e 68 65 6e 20 74 68 65 20 45 50 5f 41 67 67 0a 2a  hen the EP_Agg.*
2073f 2a 20 70 72 6f 70 65 72 74 79 20 6f 6e 20 74 68  * property on th
20740 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
20741 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 72  set..**.** An er
20742 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 6c  ror message is l
20743 65 66 74 20 69 6e 20 70 50 61 72 73 65 20 69 66  eft in pParse if
20744 20 61 6e 79 74 68 69 6e 67 20 69 73 20 61 6d 69   anything is ami
20745 73 73 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 0a  ss.  The number.
20746 2a 2a 20 69 66 20 65 72 72 6f 72 73 20 69 73 20  ** if errors is 
20747 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
20748 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
20749 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
2074a 70 72 4e 61 6d 65 73 28 20 0a 20 20 4e 61 6d 65  prNames( .  Name
2074b 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20  Context *pNC,   
2074c 20 20 20 20 2f 2a 20 4e 61 6d 65 73 70 61 63 65      /* Namespace
2074d 20 74 6f 20 72 65 73 6f 6c 76 65 20 65 78 70 72   to resolve expr
2074e 65 73 73 69 6f 6e 73 20 69 6e 2e 20 2a 2f 0a 20  essions in. */. 
2074f 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20   Expr *pExpr    
20750 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
20751 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62 65  expression to be
20752 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 29 7b   analyzed. */.){
20753 0a 20 20 69 6e 74 20 73 61 76 65 64 48 61 73 41  .  int savedHasA
20754 67 67 3b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a  gg;.  Walker w;.
20755 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20  .  if( pExpr==0 
20756 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20  ) return 0;.#if 
20757 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
20758 44 45 50 54 48 3e 30 0a 20 20 7b 0a 20 20 20 20  DEPTH>0.  {.    
20759 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20  Parse *pParse = 
2075a 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20  pNC->pParse;.   
2075b 20 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72   if( sqlite3Expr
2075c 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72  CheckHeight(pPar
2075d 73 65 2c 20 70 45 78 70 72 2d 3e 6e 48 65 69 67  se, pExpr->nHeig
2075e 68 74 2b 70 4e 43 2d 3e 70 50 61 72 73 65 2d 3e  ht+pNC->pParse->
2075f 6e 48 65 69 67 68 74 29 20 29 7b 0a 20 20 20 20  nHeight) ){.    
20760 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
20761 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 48  }.    pParse->nH
20762 65 69 67 68 74 20 2b 3d 20 70 45 78 70 72 2d 3e  eight += pExpr->
20763 6e 48 65 69 67 68 74 3b 0a 20 20 7d 0a 23 65 6e  nHeight;.  }.#en
20764 64 69 66 0a 20 20 73 61 76 65 64 48 61 73 41 67  dif.  savedHasAg
20765 67 20 3d 20 70 4e 43 2d 3e 68 61 73 41 67 67 3b  g = pNC->hasAgg;
20766 0a 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d  .  pNC->hasAgg =
20767 20 30 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c   0;.  w.xExprCal
20768 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45  lback = resolveE
20769 78 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65  xprStep;.  w.xSe
2076a 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72  lectCallback = r
2076b 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70  esolveSelectStep
2076c 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70  ;.  w.pParse = p
2076d 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 77 2e  NC->pParse;.  w.
2076e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20 73  u.pNC = pNC;.  s
2076f 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26  qlite3WalkExpr(&
20770 77 2c 20 70 45 78 70 72 29 3b 0a 23 69 66 20 53  w, pExpr);.#if S
20771 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
20772 45 50 54 48 3e 30 0a 20 20 70 4e 43 2d 3e 70 50  EPTH>0.  pNC->pP
20773 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d  arse->nHeight -=
20774 20 70 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b   pExpr->nHeight;
20775 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70 4e  .#endif.  if( pN
20776 43 2d 3e 6e 45 72 72 3e 30 20 7c 7c 20 77 2e 70  C->nErr>0 || w.p
20777 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 7b  Parse->nErr>0 ){
20778 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70  .    ExprSetProp
20779 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45  erty(pExpr, EP_E
2077a 72 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28  rror);.  }.  if(
2077b 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 29 7b 0a   pNC->hasAgg ){.
2077c 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65      ExprSetPrope
2077d 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 41 67  rty(pExpr, EP_Ag
2077e 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  g);.  }else if( 
2077f 73 61 76 65 64 48 61 73 41 67 67 20 29 7b 0a 20  savedHasAgg ){. 
20780 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 3d     pNC->hasAgg =
20781 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
20782 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
20783 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72 6f 72  (pExpr, EP_Error
20784 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 73  );.}.../*.** Res
20785 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69  olve all names i
20786 6e 20 61 6c 6c 20 65 78 70 72 65 73 73 69 6f 6e  n all expression
20787 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20 61 6e  s of a SELECT an
20788 64 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65 63 65  d in all.** dece
20789 6e 64 65 6e 74 73 20 6f 66 20 74 68 65 20 53 45  ndents of the SE
2078a 4c 45 43 54 2c 20 69 6e 63 6c 75 64 69 6e 67 20  LECT, including 
2078b 63 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 20 6f 66  compounds off of
2078c 20 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a 20 73   p->pPrior,.** s
2078d 75 62 71 75 65 72 69 65 73 20 69 6e 20 65 78 70  ubqueries in exp
2078e 72 65 73 73 69 6f 6e 73 2c 20 61 6e 64 20 73 75  ressions, and su
2078f 62 71 75 65 72 69 65 73 20 75 73 65 64 20 61 73  bqueries used as
20790 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 20   FROM clause.** 
20791 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  terms..**.** See
20792 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
20793 78 70 72 4e 61 6d 65 73 28 29 20 66 6f 72 20 61  xprNames() for a
20794 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20   description of 
20795 74 68 65 20 6b 69 6e 64 73 20 6f 66 0a 2a 2a 20  the kinds of.** 
20796 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e 73 20  transformations 
20797 74 68 61 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a  that occur..**.*
20798 2a 20 41 6c 6c 20 53 45 4c 45 43 54 20 73 74 61  * All SELECT sta
20799 74 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64 20 68  tements should h
2079a 61 76 65 20 62 65 65 6e 20 65 78 70 61 6e 64 65  ave been expande
2079b 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  d using.** sqlit
2079c 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29  e3SelectExpand()
2079d 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69   prior to invoki
2079e 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ng this routine.
2079f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
207a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
207a1 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65  esolveSelectName
207a2 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  s(.  Parse *pPar
207a3 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  se,         /* T
207a4 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
207a5 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
207a6 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
207a7 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
207a8 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65  ement being code
207a9 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74  d. */.  NameCont
207aa 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20 20 2f  ext *pOuterNC  /
207ab 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66  * Name context f
207ac 6f 72 20 70 61 72 65 6e 74 20 53 45 4c 45 43 54  or parent SELECT
207ad 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b   statement */.){
207ae 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a 20 20  .  Walker w;..  
207af 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a  assert( p!=0 );.
207b0 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63    w.xExprCallbac
207b1 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70 72 53  k = resolveExprS
207b2 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74  tep;.  w.xSelect
207b3 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c  Callback = resol
207b4 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a 20 20  veSelectStep;.  
207b5 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  w.pParse = pPars
207b6 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70  e;.  w.u.pNC = p
207b7 4f 75 74 65 72 4e 43 3b 0a 20 20 73 71 6c 69 74  OuterNC;.  sqlit
207b8 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c  e3WalkSelect(&w,
207b9 20 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a   p);.}../*******
207ba 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72  ******* End of r
207bb 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  esolve.c *******
207bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207be 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
207bf 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
207c0 6c 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a  le expr.c ******
207c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207c3 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
207c4 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
207c5 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
207c6 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
207c7 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
207c8 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
207c9 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
207ca 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
207cb 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
207cc 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
207cd 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
207ce 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
207cf 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
207d0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
207d1 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
207d2 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
207d3 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
207d4 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
207d5 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
207d6 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
207d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
207da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
207db 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
207dc 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75 73  ains routines us
207dd 65 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69 6e 67  ed for analyzing
207de 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64   expressions and
207df 0a 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61 74 69  .** for generati
207e0 6e 67 20 56 44 42 45 20 63 6f 64 65 20 74 68 61  ng VDBE code tha
207e1 74 20 65 76 61 6c 75 61 74 65 73 20 65 78 70 72  t evaluates expr
207e2 65 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c 69 74  essions in SQLit
207e3 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  e..*/../*.** Ret
207e4 75 72 6e 20 74 68 65 20 27 61 66 66 69 6e 69 74  urn the 'affinit
207e5 79 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  y' of the expres
207e6 73 69 6f 6e 20 70 45 78 70 72 20 69 66 20 61 6e  sion pExpr if an
207e7 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45 78 70  y..**.** If pExp
207e8 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 61  r is a column, a
207e9 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20   reference to a 
207ea 63 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20 27 41  column via an 'A
207eb 53 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f 72 20  S' alias,.** or 
207ec 61 20 73 75 62 2d 73 65 6c 65 63 74 20 77 69 74  a sub-select wit
207ed 68 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68  h a column as th
207ee 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2c 20  e return value, 
207ef 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61 66 66  then the .** aff
207f0 69 6e 69 74 79 20 6f 66 20 74 68 61 74 20 63 6f  inity of that co
207f1 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e 65 64  lumn is returned
207f2 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30 78 30  . Otherwise, 0x0
207f3 30 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 2a  0 is returned,.*
207f4 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20  * indicating no 
207f5 61 66 66 69 6e 69 74 79 20 66 6f 72 20 74 68 65  affinity for the
207f6 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a   expression..**.
207f7 2a 2a 20 69 2e 65 2e 20 74 68 65 20 57 48 45 52  ** i.e. the WHER
207f8 45 20 63 6c 61 75 73 65 20 65 78 70 72 65 73 73  E clause express
207f9 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 66 6f 6c  sions in the fol
207fa 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  lowing statement
207fb 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20 61 6e  s all.** have an
207fc 20 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a 2a 2a   affinity:.**.**
207fd 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
207fe 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a  (a);.** SELECT *
207ff 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61   FROM t1 WHERE a
20800 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20 41 53  ;.** SELECT a AS
20801 20 62 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   b FROM t1 WHERE
20802 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 2a 20   b;.** SELECT * 
20803 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 28 73  FROM t1 WHERE (s
20804 65 6c 65 63 74 20 61 20 66 72 6f 6d 20 74 31 29  elect a from t1)
20805 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ;.*/.SQLITE_PRIV
20806 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65 33  ATE char sqlite3
20807 45 78 70 72 41 66 66 69 6e 69 74 79 28 45 78 70  ExprAffinity(Exp
20808 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74  r *pExpr){.  int
20809 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b   op = pExpr->op;
2080a 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 45  .  if( op==TK_SE
2080b 4c 45 43 54 20 29 7b 0a 20 20 20 20 61 73 73 65  LECT ){.    asse
2080c 72 74 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73  rt( pExpr->flags
2080d 26 45 50 5f 78 49 73 53 65 6c 65 63 74 20 29 3b  &EP_xIsSelect );
2080e 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
2080f 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28  te3ExprAffinity(
20810 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
20811 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  ->pEList->a[0].p
20812 45 78 70 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64  Expr);.  }.#ifnd
20813 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
20814 41 53 54 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b  AST.  if( op==TK
20815 5f 43 41 53 54 20 29 7b 0a 20 20 20 20 61 73 73  _CAST ){.    ass
20816 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
20817 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
20818 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20  IntValue) );.   
20819 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41   return sqlite3A
2081a 66 66 69 6e 69 74 79 54 79 70 65 28 70 45 78 70  ffinityType(pExp
2081b 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  r->u.zToken);.  
2081c 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 28  }.#endif.  if( (
2081d 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d  op==TK_AGG_COLUM
2081e 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  N || op==TK_COLU
2081f 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45 47  MN || op==TK_REG
20820 49 53 54 45 52 29 20 0a 20 20 20 26 26 20 70 45  ISTER) .   && pE
20821 78 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20 29  xpr->pTab!=0.  )
20822 7b 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f  {.    /* op==TK_
20823 52 45 47 49 53 54 45 52 20 26 26 20 70 45 78 70  REGISTER && pExp
20824 72 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65  r->pTab!=0 happe
20825 6e 73 20 77 68 65 6e 20 70 45 78 70 72 20 77 61  ns when pExpr wa
20826 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20  s originally.   
20827 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20   ** a TK_COLUMN 
20828 62 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73  but was previous
20829 6c 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64  ly evaluated and
2082a 20 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67   cached in a reg
2082b 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  ister */.    int
2082c 20 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c   j = pExpr->iCol
2082d 75 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c 30  umn;.    if( j<0
2082e 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
2082f 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20  _AFF_INTEGER;.  
20830 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
20831 3e 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70 72  >pTab && j<pExpr
20832 2d 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a  ->pTab->nCol );.
20833 20 20 20 20 72 65 74 75 72 6e 20 70 45 78 70 72      return pExpr
20834 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e  ->pTab->aCol[j].
20835 61 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20 20  affinity;.  }.  
20836 72 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61 66  return pExpr->af
20837 66 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  finity;.}../*.**
20838 20 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74 69   Set the collati
20839 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  ng sequence for 
2083a 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72  expression pExpr
2083b 20 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c 61   to be the colla
2083c 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65  ting.** sequence
2083d 20 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65 6e   named by pToken
2083e 2e 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .   Return a poi
2083f 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 76 69  nter to the revi
20840 73 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a  sed expression..
20841 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67  ** The collating
20842 20 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61 72   sequence is mar
20843 6b 65 64 20 61 73 20 22 65 78 70 6c 69 63 69 74  ked as "explicit
20844 22 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f 45  " using the EP_E
20845 78 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c 61  xpCollate.** fla
20846 67 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74 20  g.  An explicit 
20847 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
20848 63 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64 65  ce will override
20849 20 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f 6c   implicit.** col
2084a 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
2084b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2084c 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
2084d 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61 72  3ExprSetColl(Par
2084e 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
2084f 20 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20 2a   *pExpr, Token *
20850 70 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63 68  pCollName){.  ch
20851 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20 20  ar *zColl = 0;  
20852 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 71            /* Deq
20853 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63 6f  uoted name of co
20854 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
20855 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70   */.  CollSeq *p
20856 43 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20  Coll;.  sqlite3 
20857 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
20858 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69  ;.  zColl = sqli
20859 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
2085a 28 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29 3b  (db, pCollName);
2085b 0a 20 20 69 66 28 20 70 45 78 70 72 20 26 26 20  .  if( pExpr && 
2085c 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f  zColl ){.    pCo
2085d 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ll = sqlite3Loca
2085e 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  teCollSeq(pParse
2085f 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66  , zColl);.    if
20860 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20  ( pColl ){.     
20861 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20   pExpr->pColl = 
20862 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 45 78  pColl;.      pEx
20863 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  pr->flags |= EP_
20864 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20  ExpCollate;.    
20865 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  }.  }.  sqlite3D
20866 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c 6c 29  bFree(db, zColl)
20867 3b 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70 72  ;.  return pExpr
20868 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
20869 6e 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f  n the default co
2086a 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2086b 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73   for the express
2086c 69 6f 6e 20 70 45 78 70 72 2e 20 49 66 0a 2a 2a  ion pExpr. If.**
2086d 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 65 66   there is no def
2086e 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 74  ault collation t
2086f 79 70 65 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a  ype, return 0..*
20870 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20871 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65   CollSeq *sqlite
20872 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72  3ExprCollSeq(Par
20873 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
20874 20 2a 70 45 78 70 72 29 7b 0a 20 20 43 6f 6c 6c   *pExpr){.  Coll
20875 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a  Seq *pColl = 0;.
20876 20 20 45 78 70 72 20 2a 70 20 3d 20 70 45 78 70    Expr *p = pExp
20877 72 3b 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41  r;.  while( ALWA
20878 59 53 28 70 29 20 29 7b 0a 20 20 20 20 69 6e 74  YS(p) ){.    int
20879 20 6f 70 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d   op;.    pColl =
2087a 20 70 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 69   p->pColl;.    i
2087b 66 28 20 70 43 6f 6c 6c 20 29 20 62 72 65 61 6b  f( pColl ) break
2087c 3b 0a 20 20 20 20 6f 70 20 3d 20 70 2d 3e 6f 70  ;.    op = p->op
2087d 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 54 61  ;.    if( p->pTa
2087e 62 21 3d 30 20 26 26 20 28 0a 20 20 20 20 20 20  b!=0 && (.      
2087f 20 20 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c    op==TK_AGG_COL
20880 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f  UMN || op==TK_CO
20881 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52  LUMN || op==TK_R
20882 45 47 49 53 54 45 52 20 7c 7c 20 6f 70 3d 3d 54  EGISTER || op==T
20883 4b 5f 54 52 49 47 47 45 52 0a 20 20 20 20 29 29  K_TRIGGER.    ))
20884 7b 0a 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54  {.      /* op==T
20885 4b 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 2d  K_REGISTER && p-
20886 3e 70 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73  >pTab!=0 happens
20887 20 77 68 65 6e 20 70 45 78 70 72 20 77 61 73 20   when pExpr was 
20888 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20  originally.     
20889 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20   ** a TK_COLUMN 
2088a 62 75 74 20 77 61 73 20 70 72 65 76 69 6f 75 73  but was previous
2088b 6c 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64  ly evaluated and
2088c 20 63 61 63 68 65 64 20 69 6e 20 61 20 72 65 67   cached in a reg
2088d 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63  ister */.      c
2088e 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  onst char *zColl
2088f 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20  ;.      int j = 
20890 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  p->iColumn;.    
20891 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20    if( j>=0 ){.  
20892 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64        sqlite3 *d
20893 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
20894 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20          zColl = 
20895 70 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  p->pTab->aCol[j]
20896 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20  .zColl;.        
20897 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46  pColl = sqlite3F
20898 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45  indCollSeq(db, E
20899 4e 43 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30  NC(db), zColl, 0
2089a 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72  );.        pExpr
2089b 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b  ->pColl = pColl;
2089c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
2089d 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2089e 69 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53 54 20  if( op!=TK_CAST 
2089f 26 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55 53 20  && op!=TK_UPLUS 
208a0 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ){.      break;.
208a1 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d      }.    p = p-
208a2 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 69 66  >pLeft;.  }.  if
208a3 28 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f  ( sqlite3CheckCo
208a4 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 43  llSeq(pParse, pC
208a5 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70 43 6f  oll) ){ .    pCo
208a6 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ll = 0;.  }.  re
208a7 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f  turn pColl;.}../
208a8 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 6e  *.** pExpr is an
208a9 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20 63 6f   operand of a co
208aa 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f  mparison operato
208ab 72 2e 20 20 61 66 66 32 20 69 73 20 74 68 65 0a  r.  aff2 is the.
208ac 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74 79  ** type affinity
208ad 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6f 70   of the other op
208ae 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72 6f 75  erand.  This rou
208af 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
208b0 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69 74  .** type affinit
208b1 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65  y that should be
208b2 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 63 6f   used for the co
208b3 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f  mparison operato
208b4 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
208b5 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74 65  VATE char sqlite
208b6 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
208b7 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63 68  (Expr *pExpr, ch
208b8 61 72 20 61 66 66 32 29 7b 0a 20 20 63 68 61 72  ar aff2){.  char
208b9 20 61 66 66 31 20 3d 20 73 71 6c 69 74 65 33 45   aff1 = sqlite3E
208ba 78 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70  xprAffinity(pExp
208bb 72 29 3b 0a 20 20 69 66 28 20 61 66 66 31 20 26  r);.  if( aff1 &
208bc 26 20 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a  & aff2 ){.    /*
208bd 20 42 6f 74 68 20 73 69 64 65 73 20 6f 66 20 74   Both sides of t
208be 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 72  he comparison ar
208bf 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 6f 6e  e columns. If on
208c0 65 20 68 61 73 20 6e 75 6d 65 72 69 63 0a 20 20  e has numeric.  
208c1 20 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c 20 75    ** affinity, u
208c2 73 65 20 74 68 61 74 2e 20 4f 74 68 65 72 77 69  se that. Otherwi
208c3 73 65 20 75 73 65 20 6e 6f 20 61 66 66 69 6e 69  se use no affini
208c4 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ty..    */.    i
208c5 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65  f( sqlite3IsNume
208c6 72 69 63 41 66 66 69 6e 69 74 79 28 61 66 66 31  ricAffinity(aff1
208c7 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 4e 75  ) || sqlite3IsNu
208c8 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66  mericAffinity(af
208c9 66 32 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  f2) ){.      ret
208ca 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  urn SQLITE_AFF_N
208cb 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65 6c 73  UMERIC;.    }els
208cc 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
208cd 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
208ce 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
208cf 66 28 20 21 61 66 66 31 20 26 26 20 21 61 66 66  f( !aff1 && !aff
208d0 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65 69 74  2 ){.    /* Neit
208d1 68 65 72 20 73 69 64 65 20 6f 66 20 74 68 65 20  her side of the 
208d2 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 20  comparison is a 
208d3 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61 72 65  column.  Compare
208d4 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 73 75   the.    ** resu
208d5 6c 74 73 20 64 69 72 65 63 74 6c 79 2e 0a 20 20  lts directly..  
208d6 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20    */.    return 
208d7 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
208d8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
208d9 20 4f 6e 65 20 73 69 64 65 20 69 73 20 61 20 63   One side is a c
208da 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68 65 72  olumn, the other
208db 20 69 73 20 6e 6f 74 2e 20 55 73 65 20 74 68 65   is not. Use the
208dc 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e 69 74   columns affinit
208dd 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74  y. */.    assert
208de 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61 66 66  ( aff1==0 || aff
208df 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75  2==0 );.    retu
208e0 72 6e 20 28 61 66 66 31 20 2b 20 61 66 66 32 29  rn (aff1 + aff2)
208e1 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70  ;.  }.}../*.** p
208e2 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70 61 72  Expr is a compar
208e3 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20  ison operator.  
208e4 52 65 74 75 72 6e 20 74 68 65 20 74 79 70 65 20  Return the type 
208e5 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68  affinity that sh
208e6 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70 6c 69  ould.** be appli
208e7 65 64 20 74 6f 20 62 6f 74 68 20 6f 70 65 72 61  ed to both opera
208e8 6e 64 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69  nds prior to doi
208e9 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  ng the compariso
208ea 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  n..*/.static cha
208eb 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69  r comparisonAffi
208ec 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72  nity(Expr *pExpr
208ed 29 7b 0a 20 20 63 68 61 72 20 61 66 66 3b 0a 20  ){.  char aff;. 
208ee 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
208ef 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70 45 78  op==TK_EQ || pEx
208f0 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 7c 7c  pr->op==TK_IN ||
208f1 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c   pExpr->op==TK_L
208f2 54 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 70  T ||.          p
208f3 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20  Expr->op==TK_GT 
208f4 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  || pExpr->op==TK
208f5 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70  _GE || pExpr->op
208f6 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20 20 20  ==TK_LE ||.     
208f7 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d       pExpr->op==
208f8 54 4b 5f 4e 45 20 7c 7c 20 70 45 78 70 72 2d 3e  TK_NE || pExpr->
208f9 6f 70 3d 3d 54 4b 5f 49 53 20 7c 7c 20 70 45 78  op==TK_IS || pEx
208fa 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 4e 4f 54  pr->op==TK_ISNOT
208fb 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
208fc 78 70 72 2d 3e 70 4c 65 66 74 20 29 3b 0a 20 20  xpr->pLeft );.  
208fd 61 66 66 20 3d 20 73 71 6c 69 74 65 33 45 78 70  aff = sqlite3Exp
208fe 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d  rAffinity(pExpr-
208ff 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 70  >pLeft);.  if( p
20900 45 78 70 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a  Expr->pRight ){.
20901 20 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65      aff = sqlite
20902 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
20903 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20  (pExpr->pRight, 
20904 61 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  aff);.  }else if
20905 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
20906 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53  y(pExpr, EP_xIsS
20907 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 61 66  elect) ){.    af
20908 66 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70 61  f = sqlite3Compa
20909 72 65 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  reAffinity(pExpr
2090a 2d 3e 78 2e 70 53 65 6c 65 63 74 2d 3e 70 45 4c  ->x.pSelect->pEL
2090b 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c  ist->a[0].pExpr,
2090c 20 61 66 66 29 3b 0a 20 20 7d 65 6c 73 65 20 69   aff);.  }else i
2090d 66 28 20 21 61 66 66 20 29 7b 0a 20 20 20 20 61  f( !aff ){.    a
2090e 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
2090f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20 20 72 65 74 75  NONE;.  }.  retu
20910 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn aff;.}../*.**
20911 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70   pExpr is a comp
20912 61 72 69 73 6f 6e 20 65 78 70 72 65 73 73 69 6f  arison expressio
20913 6e 2c 20 65 67 2e 20 27 3d 27 2c 20 27 3c 27 2c  n, eg. '=', '<',
20914 20 49 4e 28 2e 2e 2e 29 20 65 74 63 2e 0a 2a 2a   IN(...) etc..**
20915 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20 69 73   idx_affinity is
20916 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66   the affinity of
20917 20 61 6e 20 69 6e 64 65 78 65 64 20 63 6f 6c 75   an indexed colu
20918 6d 6e 2e 20 52 65 74 75 72 6e 20 74 72 75 65 0a  mn. Return true.
20919 2a 2a 20 69 66 20 74 68 65 20 69 6e 64 65 78 20  ** if the index 
2091a 77 69 74 68 20 61 66 66 69 6e 69 74 79 20 69 64  with affinity id
2091b 78 5f 61 66 66 69 6e 69 74 79 20 6d 61 79 20 62  x_affinity may b
2091c 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  e used to implem
2091d 65 6e 74 0a 2a 2a 20 74 68 65 20 63 6f 6d 70 61  ent.** the compa
2091e 72 69 73 6f 6e 20 69 6e 20 70 45 78 70 72 2e 0a  rison in pExpr..
2091f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20920 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64  E int sqlite3Ind
20921 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70  exAffinityOk(Exp
20922 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69  r *pExpr, char i
20923 64 78 5f 61 66 66 69 6e 69 74 79 29 7b 0a 20 20  dx_affinity){.  
20924 63 68 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61  char aff = compa
20925 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45  risonAffinity(pE
20926 78 70 72 29 3b 0a 20 20 73 77 69 74 63 68 28 20  xpr);.  switch( 
20927 61 66 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20  aff ){.    case 
20928 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3a  SQLITE_AFF_NONE:
20929 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2092a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2092b 5f 41 46 46 5f 54 45 58 54 3a 0a 20 20 20 20 20  _AFF_TEXT:.     
2092c 20 72 65 74 75 72 6e 20 69 64 78 5f 61 66 66 69   return idx_affi
2092d 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
2092e 5f 54 45 58 54 3b 0a 20 20 20 20 64 65 66 61 75  _TEXT;.    defau
2092f 6c 74 3a 0a 20 20 20 20 20 20 72 65 74 75 72 6e  lt:.      return
20930 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69   sqlite3IsNumeri
20931 63 41 66 66 69 6e 69 74 79 28 69 64 78 5f 61 66  cAffinity(idx_af
20932 66 69 6e 69 74 79 29 3b 0a 20 20 7d 0a 7d 0a 0a  finity);.  }.}..
20933 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
20934 20 50 35 20 76 61 6c 75 65 20 74 68 61 74 20 73   P5 value that s
20935 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f  hould be used fo
20936 72 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61  r a binary compa
20937 72 69 73 6f 6e 0a 2a 2a 20 6f 70 63 6f 64 65 20  rison.** opcode 
20938 28 4f 50 5f 45 71 2c 20 4f 50 5f 47 65 20 65 74  (OP_Eq, OP_Ge et
20939 63 2e 29 20 75 73 65 64 20 74 6f 20 63 6f 6d 70  c.) used to comp
2093a 61 72 65 20 70 45 78 70 72 31 20 61 6e 64 20 70  are pExpr1 and p
2093b 45 78 70 72 32 2e 0a 2a 2f 0a 73 74 61 74 69 63  Expr2..*/.static
2093c 20 75 38 20 62 69 6e 61 72 79 43 6f 6d 70 61 72   u8 binaryCompar
2093d 65 50 35 28 45 78 70 72 20 2a 70 45 78 70 72 31  eP5(Expr *pExpr1
2093e 2c 20 45 78 70 72 20 2a 70 45 78 70 72 32 2c 20  , Expr *pExpr2, 
2093f 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b  int jumpIfNull){
20940 0a 20 20 75 38 20 61 66 66 20 3d 20 28 63 68 61  .  u8 aff = (cha
20941 72 29 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  r)sqlite3ExprAff
20942 69 6e 69 74 79 28 70 45 78 70 72 32 29 3b 0a 20  inity(pExpr2);. 
20943 20 61 66 66 20 3d 20 28 75 38 29 73 71 6c 69 74   aff = (u8)sqlit
20944 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74  e3CompareAffinit
20945 79 28 70 45 78 70 72 31 2c 20 61 66 66 29 20 7c  y(pExpr1, aff) |
20946 20 28 75 38 29 6a 75 6d 70 49 66 4e 75 6c 6c 3b   (u8)jumpIfNull;
20947 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d  .  return aff;.}
20948 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
20949 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2094a 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2094b 63 65 20 74 68 61 74 20 73 68 6f 75 6c 64 20 62  ce that should b
2094c 65 20 75 73 65 64 20 62 79 0a 2a 2a 20 61 20 62  e used by.** a b
2094d 69 6e 61 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e  inary comparison
2094e 20 6f 70 65 72 61 74 6f 72 20 63 6f 6d 70 61 72   operator compar
2094f 69 6e 67 20 70 4c 65 66 74 20 61 6e 64 20 70 52  ing pLeft and pR
20950 69 67 68 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ight..**.** If t
20951 68 65 20 6c 65 66 74 20 68 61 6e 64 20 65 78 70  he left hand exp
20952 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63 6f  ression has a co
20953 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
20954 20 74 79 70 65 2c 20 74 68 65 6e 20 69 74 20 69   type, then it i
20955 73 0a 2a 2a 20 75 73 65 64 2e 20 4f 74 68 65 72  s.** used. Other
20956 77 69 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69  wise the collati
20957 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  on sequence for 
20958 74 68 65 20 72 69 67 68 74 20 68 61 6e 64 20 65  the right hand e
20959 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20  xpression.** is 
2095a 75 73 65 64 2c 20 6f 72 20 74 68 65 20 64 65 66  used, or the def
2095b 61 75 6c 74 20 28 42 49 4e 41 52 59 29 20 69 66  ault (BINARY) if
2095c 20 6e 65 69 74 68 65 72 20 65 78 70 72 65 73 73   neither express
2095d 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74  ion has a collat
2095e 69 6e 67 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a  ing.** type..**.
2095f 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 52 69 67  ** Argument pRig
20960 68 74 20 28 62 75 74 20 6e 6f 74 20 70 4c 65 66  ht (but not pLef
20961 74 29 20 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c  t) may be a null
20962 20 70 6f 69 6e 74 65 72 2e 20 49 6e 20 74 68 69   pointer. In thi
20963 73 20 63 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73  s case,.** it is
20964 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65 64 2e   not considered.
20965 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20966 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
20967 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65  te3BinaryCompare
20968 43 6f 6c 6c 53 65 71 28 0a 20 20 50 61 72 73 65  CollSeq(.  Parse
20969 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 45 78 70   *pParse, .  Exp
2096a 72 20 2a 70 4c 65 66 74 2c 20 0a 20 20 45 78 70  r *pLeft, .  Exp
2096b 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20 43  r *pRight.){.  C
2096c 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20  ollSeq *pColl;. 
2096d 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 20 29   assert( pLeft )
2096e 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 66  ;.  if( pLeft->f
2096f 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c  lags & EP_ExpCol
20970 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73 73 65  late ){.    asse
20971 72 74 28 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c  rt( pLeft->pColl
20972 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20   );.    pColl = 
20973 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20  pLeft->pColl;.  
20974 7d 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74  }else if( pRight
20975 20 26 26 20 70 52 69 67 68 74 2d 3e 66 6c 61 67   && pRight->flag
20976 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  s & EP_ExpCollat
20977 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  e ){.    assert(
20978 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29   pRight->pColl )
20979 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52  ;.    pColl = pR
2097a 69 67 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d  ight->pColl;.  }
2097b 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20  else{.    pColl 
2097c 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
2097d 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65  lSeq(pParse, pLe
2097e 66 74 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43  ft);.    if( !pC
2097f 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f  oll ){.      pCo
20980 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
20981 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
20982 70 52 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20  pRight);.    }. 
20983 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c   }.  return pCol
20984 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  l;.}../*.** Gene
20985 72 61 74 65 20 74 68 65 20 6f 70 65 72 61 6e 64  rate the operand
20986 73 20 66 6f 72 20 61 20 63 6f 6d 70 61 72 69 73  s for a comparis
20987 6f 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42  on operation.  B
20988 65 66 6f 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74  efore.** generat
20989 69 6e 67 20 74 68 65 20 63 6f 64 65 20 66 6f 72  ing the code for
2098a 20 65 61 63 68 20 6f 70 65 72 61 6e 64 2c 20 73   each operand, s
2098b 65 74 20 74 68 65 20 45 50 5f 41 6e 79 41 66 66  et the EP_AnyAff
2098c 0a 2a 2a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20  .** flag on the 
2098d 65 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74 68  expression so th
2098e 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 61 62  at it will be ab
2098f 6c 65 20 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20  le to used a.** 
20990 63 61 63 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61  cached column va
20991 6c 75 65 20 74 68 61 74 20 68 61 73 20 70 72 65  lue that has pre
20992 76 69 6f 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e  viously undergon
20993 65 20 61 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79  e an.** affinity
20994 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74   change..*/.stat
20995 69 63 20 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70  ic void codeComp
20996 61 72 65 4f 70 65 72 61 6e 64 73 28 0a 20 20 50  areOperands(.  P
20997 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
20998 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20   /* Parsing and 
20999 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20  code generating 
2099a 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
2099b 72 20 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f  r *pLeft,      /
2099c 2a 20 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61  * The left opera
2099d 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65  nd */.  int *pRe
2099e 67 4c 65 66 74 2c 20 20 20 20 2f 2a 20 52 65 67  gLeft,    /* Reg
2099f 69 73 74 65 72 20 77 68 65 72 65 20 6c 65 66 74  ister where left
209a0 20 6f 70 65 72 61 6e 64 20 69 73 20 73 74 6f 72   operand is stor
209a1 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72  ed */.  int *pFr
209a2 65 65 4c 65 66 74 2c 20 20 20 2f 2a 20 46 72 65  eeLeft,   /* Fre
209a3 65 20 74 68 69 73 20 72 65 67 69 73 74 65 72 20  e this register 
209a4 77 68 65 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45  when done */.  E
209a5 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20  xpr *pRight,    
209a6 20 2f 2a 20 54 68 65 20 72 69 67 68 74 20 6f 70   /* The right op
209a7 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  erand */.  int *
209a8 70 52 65 67 52 69 67 68 74 2c 20 20 20 2f 2a 20  pRegRight,   /* 
209a9 52 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72  Register where r
209aa 69 67 68 74 20 6f 70 65 72 61 6e 64 20 69 73 20  ight operand is 
209ab 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20  stored */.  int 
209ac 2a 70 46 72 65 65 52 69 67 68 74 20 20 20 2f 2a  *pFreeRight   /*
209ad 20 57 72 69 74 65 20 74 65 6d 70 20 72 65 67 69   Write temp regi
209ae 73 74 65 72 20 66 6f 72 20 72 69 67 68 74 20 6f  ster for right o
209af 70 65 72 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a  perand there */.
209b0 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 4c 65 66  ){.  while( pLef
209b1 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20  t->op==TK_UPLUS 
209b2 29 20 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d  ) pLeft = pLeft-
209b3 3e 70 4c 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d  >pLeft;.  pLeft-
209b4 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79  >flags |= EP_Any
209b5 41 66 66 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74  Aff;.  *pRegLeft
209b6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
209b7 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
209b8 4c 65 66 74 2c 20 70 46 72 65 65 4c 65 66 74 29  Left, pFreeLeft)
209b9 3b 0a 20 20 77 68 69 6c 65 28 20 70 52 69 67 68  ;.  while( pRigh
209ba 74 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20  t->op==TK_UPLUS 
209bb 29 20 70 52 69 67 68 74 20 3d 20 70 52 69 67 68  ) pRight = pRigh
209bc 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70 52 69 67  t->pLeft;.  pRig
209bd 68 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  ht->flags |= EP_
209be 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65 67 52  AnyAff;.  *pRegR
209bf 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78  ight = sqlite3Ex
209c0 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
209c1 65 2c 20 70 52 69 67 68 74 2c 20 70 46 72 65 65  e, pRight, pFree
209c2 52 69 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Right);.}../*.**
209c3 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
209c4 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20  or a comparison 
209c5 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61  operator..*/.sta
209c6 74 69 63 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70  tic int codeComp
209c7 61 72 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  are(.  Parse *pP
209c8 61 72 73 65 2c 20 20 20 20 2f 2a 20 54 68 65 20  arse,    /* The 
209c9 70 61 72 73 69 6e 67 20 28 61 6e 64 20 63 6f 64  parsing (and cod
209ca 65 20 67 65 6e 65 72 61 74 69 6e 67 29 20 63 6f  e generating) co
209cb 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20  ntext */.  Expr 
209cc 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20  *pLeft,      /* 
209cd 54 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64  The left operand
209ce 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67   */.  Expr *pRig
209cf 68 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72  ht,     /* The r
209d0 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  ight operand */.
209d1 20 20 69 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20    int opcode,   
209d2 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61      /* The compa
209d3 72 69 73 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a  rison opcode */.
209d4 20 20 69 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69    int in1, int i
209d5 6e 32 2c 20 2f 2a 20 52 65 67 69 73 74 65 72 20  n2, /* Register 
209d6 68 6f 6c 64 69 6e 67 20 6f 70 65 72 61 6e 64 73  holding operands
209d7 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20   */.  int dest, 
209d8 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
209d9 68 65 72 65 20 69 66 20 74 72 75 65 2e 20 20 2a  here if true.  *
209da 2f 0a 20 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75  /.  int jumpIfNu
209db 6c 6c 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65  ll    /* If true
209dc 2c 20 6a 75 6d 70 20 69 66 20 65 69 74 68 65 72  , jump if either
209dd 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
209de 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b   */.){.  int p5;
209df 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 43  .  int addr;.  C
209e0 6f 6c 6c 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70  ollSeq *p4;..  p
209e1 34 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72  4 = sqlite3Binar
209e2 79 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28  yCompareCollSeq(
209e3 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70  pParse, pLeft, p
209e4 52 69 67 68 74 29 3b 0a 20 20 70 35 20 3d 20 62  Right);.  p5 = b
209e5 69 6e 61 72 79 43 6f 6d 70 61 72 65 50 35 28 70  inaryCompareP5(p
209e6 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 6a 75  Left, pRight, ju
209e7 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64  mpIfNull);.  add
209e8 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
209e9 64 64 4f 70 34 28 70 50 61 72 73 65 2d 3e 70 56  ddOp4(pParse->pV
209ea 64 62 65 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32  dbe, opcode, in2
209eb 2c 20 64 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20  , dest, in1,.   
209ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
209ed 20 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70          (void*)p
209ee 34 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a  4, P4_COLLSEQ);.
209ef 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
209f0 6e 67 65 50 35 28 70 50 61 72 73 65 2d 3e 70 56  ngeP5(pParse->pV
209f1 64 62 65 2c 20 28 75 38 29 70 35 29 3b 0a 20 20  dbe, (u8)p5);.  
209f2 69 66 28 20 28 70 35 20 26 20 53 51 4c 49 54 45  if( (p5 & SQLITE
209f3 5f 41 46 46 5f 4d 41 53 4b 29 21 3d 53 51 4c 49  _AFF_MASK)!=SQLI
209f4 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20  TE_AFF_NONE ){. 
209f5 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
209f6 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
209f7 65 28 70 50 61 72 73 65 2c 20 69 6e 31 2c 20 31  e(pParse, in1, 1
209f8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
209f9 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
209fa 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69 6e  hange(pParse, in
209fb 32 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74  2, 1);.  }.  ret
209fc 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 23 69 66  urn addr;.}..#if
209fd 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52   SQLITE_MAX_EXPR
209fe 5f 44 45 50 54 48 3e 30 0a 2f 2a 0a 2a 2a 20 43  _DEPTH>0./*.** C
209ff 68 65 63 6b 20 74 68 61 74 20 61 72 67 75 6d 65  heck that argume
20a00 6e 74 20 6e 48 65 69 67 68 74 20 69 73 20 6c 65  nt nHeight is le
20a01 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
20a02 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a   to the maximum.
20a03 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65  ** expression de
20a04 70 74 68 20 61 6c 6c 6f 77 65 64 2e 20 49 66 20  pth allowed. If 
20a05 69 74 20 69 73 20 6e 6f 74 2c 20 6c 65 61 76 65  it is not, leave
20a06 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
20a07 65 20 69 6e 0a 2a 2a 20 70 50 61 72 73 65 2e 0a  e in.** pParse..
20a08 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20a09 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
20a0a 72 43 68 65 63 6b 48 65 69 67 68 74 28 50 61 72  rCheckHeight(Par
20a0b 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
20a0c 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20  nHeight){.  int 
20a0d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
20a0e 20 20 69 6e 74 20 6d 78 48 65 69 67 68 74 20 3d    int mxHeight =
20a0f 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69   pParse->db->aLi
20a10 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
20a11 5f 45 58 50 52 5f 44 45 50 54 48 5d 3b 0a 20 20  _EXPR_DEPTH];.  
20a12 69 66 28 20 6e 48 65 69 67 68 74 3e 6d 78 48 65  if( nHeight>mxHe
20a13 69 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  ight ){.    sqli
20a14 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
20a15 73 65 2c 20 0a 20 20 20 20 20 20 20 22 45 78 70  se, .       "Exp
20a16 72 65 73 73 69 6f 6e 20 74 72 65 65 20 69 73 20  ression tree is 
20a17 74 6f 6f 20 6c 61 72 67 65 20 28 6d 61 78 69 6d  too large (maxim
20a18 75 6d 20 64 65 70 74 68 20 25 64 29 22 2c 20 6d  um depth %d)", m
20a19 78 48 65 69 67 68 74 0a 20 20 20 20 29 3b 0a 20  xHeight.    );. 
20a1a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
20a1b 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75  RROR;.  }.  retu
20a1c 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 65  rn rc;.}../* The
20a1d 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 72 65 65   following three
20a1e 20 66 75 6e 63 74 69 6f 6e 73 2c 20 68 65 69 67   functions, heig
20a1f 68 74 4f 66 45 78 70 72 28 29 2c 20 68 65 69 67  htOfExpr(), heig
20a20 68 74 4f 66 45 78 70 72 4c 69 73 74 28 29 0a 2a  htOfExprList().*
20a21 2a 20 61 6e 64 20 68 65 69 67 68 74 4f 66 53 65  * and heightOfSe
20a22 6c 65 63 74 28 29 2c 20 61 72 65 20 75 73 65 64  lect(), are used
20a23 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
20a24 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74  e maximum height
20a25 0a 2a 2a 20 6f 66 20 61 6e 79 20 65 78 70 72 65  .** of any expre
20a26 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72  ssion tree refer
20a27 65 6e 63 65 64 20 62 79 20 74 68 65 20 73 74 72  enced by the str
20a28 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61 73  ucture passed as
20a29 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 61 72   the.** first ar
20a2a 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  gument..**.** If
20a2b 20 74 68 69 73 20 6d 61 78 69 6d 75 6d 20 68 65   this maximum he
20a2c 69 67 68 74 20 69 73 20 67 72 65 61 74 65 72 20  ight is greater 
20a2d 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e 74  than the current
20a2e 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 0a 2a   value pointed.*
20a2f 2a 20 74 6f 20 62 79 20 70 6e 48 65 69 67 68 74  * to by pnHeight
20a30 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  , the second par
20a31 61 6d 65 74 65 72 2c 20 74 68 65 6e 20 73 65 74  ameter, then set
20a32 20 2a 70 6e 48 65 69 67 68 74 20 74 6f 20 74 68   *pnHeight to th
20a33 61 74 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2f 0a  at.** value..*/.
20a34 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 69 67  static void heig
20a35 68 74 4f 66 45 78 70 72 28 45 78 70 72 20 2a 70  htOfExpr(Expr *p
20a36 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29  , int *pnHeight)
20a37 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
20a38 20 69 66 28 20 70 2d 3e 6e 48 65 69 67 68 74 3e   if( p->nHeight>
20a39 2a 70 6e 48 65 69 67 68 74 20 29 7b 0a 20 20 20  *pnHeight ){.   
20a3a 20 20 20 2a 70 6e 48 65 69 67 68 74 20 3d 20 70     *pnHeight = p
20a3b 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20 20 20 7d  ->nHeight;.    }
20a3c 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
20a3d 69 64 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c  id heightOfExprL
20a3e 69 73 74 28 45 78 70 72 4c 69 73 74 20 2a 70 2c  ist(ExprList *p,
20a3f 20 69 6e 74 20 2a 70 6e 48 65 69 67 68 74 29 7b   int *pnHeight){
20a40 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
20a41 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
20a42 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70 72 3b 20  =0; i<p->nExpr; 
20a43 69 2b 2b 29 7b 0a 20 20 20 20 20 20 68 65 69 67  i++){.      heig
20a44 68 74 4f 66 45 78 70 72 28 70 2d 3e 61 5b 69 5d  htOfExpr(p->a[i]
20a45 2e 70 45 78 70 72 2c 20 70 6e 48 65 69 67 68 74  .pExpr, pnHeight
20a46 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73  );.    }.  }.}.s
20a47 74 61 74 69 63 20 76 6f 69 64 20 68 65 69 67 68  tatic void heigh
20a48 74 4f 66 53 65 6c 65 63 74 28 53 65 6c 65 63 74  tOfSelect(Select
20a49 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69 67   *p, int *pnHeig
20a4a 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  ht){.  if( p ){.
20a4b 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72      heightOfExpr
20a4c 28 70 2d 3e 70 57 68 65 72 65 2c 20 70 6e 48 65  (p->pWhere, pnHe
20a4d 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68  ight);.    heigh
20a4e 74 4f 66 45 78 70 72 28 70 2d 3e 70 48 61 76 69  tOfExpr(p->pHavi
20a4f 6e 67 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20  ng, pnHeight);. 
20a50 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28     heightOfExpr(
20a51 70 2d 3e 70 4c 69 6d 69 74 2c 20 70 6e 48 65 69  p->pLimit, pnHei
20a52 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74  ght);.    height
20a53 4f 66 45 78 70 72 28 70 2d 3e 70 4f 66 66 73 65  OfExpr(p->pOffse
20a54 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20  t, pnHeight);.  
20a55 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69    heightOfExprLi
20a56 73 74 28 70 2d 3e 70 45 4c 69 73 74 2c 20 70 6e  st(p->pEList, pn
20a57 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69  Height);.    hei
20a58 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d  ghtOfExprList(p-
20a59 3e 70 47 72 6f 75 70 42 79 2c 20 70 6e 48 65 69  >pGroupBy, pnHei
20a5a 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74  ght);.    height
20a5b 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 4f  OfExprList(p->pO
20a5c 72 64 65 72 42 79 2c 20 70 6e 48 65 69 67 68 74  rderBy, pnHeight
20a5d 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 53  );.    heightOfS
20a5e 65 6c 65 63 74 28 70 2d 3e 70 50 72 69 6f 72 2c  elect(p->pPrior,
20a5f 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a   pnHeight);.  }.
20a60 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
20a61 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20 76 61   Expr.nHeight va
20a62 72 69 61 62 6c 65 20 69 6e 20 74 68 65 20 73 74  riable in the st
20a63 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61  ructure passed a
20a64 73 20 61 6e 20 0a 2a 2a 20 61 72 67 75 6d 65 6e  s an .** argumen
20a65 74 2e 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e  t. An expression
20a66 20 77 69 74 68 20 6e 6f 20 63 68 69 6c 64 72 65   with no childre
20a67 6e 2c 20 45 78 70 72 2e 70 4c 69 73 74 20 6f 72  n, Expr.pList or
20a68 20 0a 2a 2a 20 45 78 70 72 2e 70 53 65 6c 65 63   .** Expr.pSelec
20a69 74 20 6d 65 6d 62 65 72 20 68 61 73 20 61 20 68  t member has a h
20a6a 65 69 67 68 74 20 6f 66 20 31 2e 20 41 6e 79 20  eight of 1. Any 
20a6b 6f 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  other expression
20a6c 0a 2a 2a 20 68 61 73 20 61 20 68 65 69 67 68 74  .** has a height
20a6d 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61   equal to the ma
20a6e 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f 66 20  ximum height of 
20a6f 61 6e 79 20 6f 74 68 65 72 20 0a 2a 2a 20 72 65  any other .** re
20a70 66 65 72 65 6e 63 65 64 20 45 78 70 72 20 70 6c  ferenced Expr pl
20a71 75 73 20 6f 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69  us one..*/.stati
20a72 63 20 76 6f 69 64 20 65 78 70 72 53 65 74 48 65  c void exprSetHe
20a73 69 67 68 74 28 45 78 70 72 20 2a 70 29 7b 0a 20  ight(Expr *p){. 
20a74 20 69 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30   int nHeight = 0
20a75 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78 70 72  ;.  heightOfExpr
20a76 28 70 2d 3e 70 4c 65 66 74 2c 20 26 6e 48 65 69  (p->pLeft, &nHei
20a77 67 68 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66  ght);.  heightOf
20a78 45 78 70 72 28 70 2d 3e 70 52 69 67 68 74 2c 20  Expr(p->pRight, 
20a79 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 69 66 28  &nHeight);.  if(
20a7a 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
20a7b 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  (p, EP_xIsSelect
20a7c 29 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f  ) ){.    heightO
20a7d 66 53 65 6c 65 63 74 28 70 2d 3e 78 2e 70 53 65  fSelect(p->x.pSe
20a7e 6c 65 63 74 2c 20 26 6e 48 65 69 67 68 74 29 3b  lect, &nHeight);
20a7f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 65  .  }else{.    he
20a80 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28 70  ightOfExprList(p
20a81 2d 3e 78 2e 70 4c 69 73 74 2c 20 26 6e 48 65 69  ->x.pList, &nHei
20a82 67 68 74 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e  ght);.  }.  p->n
20a83 48 65 69 67 68 74 20 3d 20 6e 48 65 69 67 68 74  Height = nHeight
20a84 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53   + 1;.}../*.** S
20a85 65 74 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69  et the Expr.nHei
20a86 67 68 74 20 76 61 72 69 61 62 6c 65 20 75 73 69  ght variable usi
20a87 6e 67 20 74 68 65 20 65 78 70 72 53 65 74 48 65  ng the exprSetHe
20a88 69 67 68 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e  ight() function.
20a89 20 49 66 0a 2a 2a 20 74 68 65 20 68 65 69 67 68   If.** the heigh
20a8a 74 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  t is greater tha
20a8b 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c  n the maximum al
20a8c 6c 6f 77 65 64 20 65 78 70 72 65 73 73 69 6f 6e  lowed expression
20a8d 20 64 65 70 74 68 2c 0a 2a 2a 20 6c 65 61 76 65   depth,.** leave
20a8e 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 70 50 61   an error in pPa
20a8f 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  rse..*/.SQLITE_P
20a90 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
20a91 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
20a92 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
20a93 45 78 70 72 20 2a 70 29 7b 0a 20 20 65 78 70 72  Expr *p){.  expr
20a94 53 65 74 48 65 69 67 68 74 28 70 29 3b 0a 20 20  SetHeight(p);.  
20a95 73 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b  sqlite3ExprCheck
20a96 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 70  Height(pParse, p
20a97 2d 3e 6e 48 65 69 67 68 74 29 3b 0a 7d 0a 0a 2f  ->nHeight);.}../
20a98 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
20a99 6d 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 6f  maximum height o
20a9a 66 20 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e  f any expression
20a9b 20 74 72 65 65 20 72 65 66 65 72 65 6e 63 65 64   tree referenced
20a9c 0a 2a 2a 20 62 79 20 74 68 65 20 73 65 6c 65 63  .** by the selec
20a9d 74 20 73 74 61 74 65 6d 65 6e 74 20 70 61 73 73  t statement pass
20a9e 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
20a9f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
20aa0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
20aa1 53 65 6c 65 63 74 45 78 70 72 48 65 69 67 68 74  SelectExprHeight
20aa2 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 69  (Select *p){.  i
20aa3 6e 74 20 6e 48 65 69 67 68 74 20 3d 20 30 3b 0a  nt nHeight = 0;.
20aa4 20 20 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74    heightOfSelect
20aa5 28 70 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20  (p, &nHeight);. 
20aa6 20 72 65 74 75 72 6e 20 6e 48 65 69 67 68 74 3b   return nHeight;
20aa7 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  .}.#else.  #defi
20aa8 6e 65 20 65 78 70 72 53 65 74 48 65 69 67 68 74  ne exprSetHeight
20aa9 28 79 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  (y).#endif /* SQ
20aaa 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
20aab 50 54 48 3e 30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  PTH>0 */../*.** 
20aac 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
20aad 74 68 65 20 63 6f 72 65 20 61 6c 6c 6f 63 61 74  the core allocat
20aae 6f 72 20 66 6f 72 20 45 78 70 72 20 6e 6f 64 65  or for Expr node
20aaf 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75  s..**.** Constru
20ab0 63 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 73  ct a new express
20ab1 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65 74  ion node and ret
20ab2 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
20ab3 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a 20   it.  Memory.** 
20ab4 66 6f 72 20 74 68 69 73 20 6e 6f 64 65 20 61 6e  for this node an
20ab5 64 20 66 6f 72 20 74 68 65 20 70 54 6f 6b 65 6e  d for the pToken
20ab6 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 73   argument is a s
20ab7 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  ingle allocation
20ab8 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  .** obtained fro
20ab9 6d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  m sqlite3DbMallo
20aba 63 28 29 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e  c().  The callin
20abb 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73  g function.** is
20abc 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
20abd 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 74 68 65   making sure the
20abe 20 6e 6f 64 65 20 65 76 65 6e 74 75 61 6c 6c 79   node eventually
20abf 20 67 65 74 73 20 66 72 65 65 64 2e 0a 2a 2a 0a   gets freed..**.
20ac0 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20 69 73  ** If dequote is
20ac1 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
20ac2 74 6f 6b 65 6e 20 28 69 66 20 69 74 20 65 78 69  token (if it exi
20ac3 73 74 73 29 20 69 73 20 64 65 71 75 6f 74 65 64  sts) is dequoted
20ac4 2e 0a 2a 2a 20 49 66 20 64 65 71 75 6f 74 65 20  ..** If dequote 
20ac5 69 73 20 66 61 6c 73 65 2c 20 6e 6f 20 64 65 71  is false, no deq
20ac6 75 6f 74 69 6e 67 20 69 73 20 70 65 72 66 6f 72  uoting is perfor
20ac7 6d 61 6e 63 65 2e 20 20 54 68 65 20 64 65 51 75  mance.  The deQu
20ac8 6f 74 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  ote.** parameter
20ac9 20 69 73 20 69 67 6e 6f 72 65 64 20 69 66 20 70   is ignored if p
20aca 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 6f 72  Token is NULL or
20acb 20 69 66 20 74 68 65 20 74 6f 6b 65 6e 20 64 6f   if the token do
20acc 65 73 20 6e 6f 74 0a 2a 2a 20 61 70 70 65 61 72  es not.** appear
20acd 20 74 6f 20 62 65 20 71 75 6f 74 65 64 2e 20 20   to be quoted.  
20ace 49 66 20 74 68 65 20 71 75 6f 74 65 73 20 77 65  If the quotes we
20acf 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  re of the form "
20ad0 2e 2e 2e 22 20 28 64 6f 75 62 6c 65 2d 71 75 6f  ..." (double-quo
20ad1 74 65 73 29 0a 2a 2a 20 74 68 65 6e 20 74 68 65  tes).** then the
20ad2 20 45 50 5f 44 62 6c 51 75 6f 74 65 64 20 66 6c   EP_DblQuoted fl
20ad3 61 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65  ag is set on the
20ad4 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65   expression node
20ad5 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 61 6c 20  ..**.** Special 
20ad6 63 61 73 65 3a 20 20 49 66 20 6f 70 3d 3d 54 4b  case:  If op==TK
20ad7 5f 49 4e 54 45 47 45 52 20 61 6e 64 20 70 54 6f  _INTEGER and pTo
20ad8 6b 65 6e 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  ken points to a 
20ad9 73 74 72 69 6e 67 20 74 68 61 74 0a 2a 2a 20 63  string that.** c
20ada 61 6e 20 62 65 20 74 72 61 6e 73 6c 61 74 65 64  an be translated
20adb 20 69 6e 74 6f 20 61 20 33 32 2d 62 69 74 20 69   into a 32-bit i
20adc 6e 74 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65  nteger, then the
20add 20 74 6f 6b 65 6e 20 69 73 20 6e 6f 74 0a 2a 2a   token is not.**
20ade 20 73 74 6f 72 65 64 20 69 6e 20 75 2e 7a 54 6f   stored in u.zTo
20adf 6b 65 6e 2e 20 20 49 6e 73 74 65 61 64 2c 20 74  ken.  Instead, t
20ae0 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  he integer value
20ae1 73 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  s is written.** 
20ae2 69 6e 74 6f 20 75 2e 69 56 61 6c 75 65 20 61 6e  into u.iValue an
20ae3 64 20 74 68 65 20 45 50 5f 49 6e 74 56 61 6c 75  d the EP_IntValu
20ae4 65 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20  e flag is set.  
20ae5 4e 6f 20 65 78 74 72 61 20 73 74 6f 72 61 67 65  No extra storage
20ae6 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  .** is allocated
20ae7 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 69 6e 74   to hold the int
20ae8 65 67 65 72 20 74 65 78 74 20 61 6e 64 20 74 68  eger text and th
20ae9 65 20 64 65 71 75 6f 74 65 20 66 6c 61 67 20 69  e dequote flag i
20aea 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53 51  s ignored..*/.SQ
20aeb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
20aec 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6c  r *sqlite3ExprAl
20aed 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  loc(.  sqlite3 *
20aee 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  db,            /
20aef 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71 6c  * Handle for sql
20af0 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
20af1 28 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c 29  () (may be null)
20af2 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20   */.  int op,   
20af3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20af4 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63 6f   Expression opco
20af5 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f  de */.  const To
20af6 6b 65 6e 20 2a 70 54 6f 6b 65 6e 2c 20 20 20 20  ken *pToken,    
20af7 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e  /* Token argumen
20af8 74 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c  t.  Might be NUL
20af9 4c 20 2a 2f 0a 20 20 69 6e 74 20 64 65 71 75 6f  L */.  int dequo
20afa 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  te             /
20afb 2a 20 54 72 75 65 20 74 6f 20 64 65 71 75 6f 74  * True to dequot
20afc 65 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a  e */.){.  Expr *
20afd 70 4e 65 77 3b 0a 20 20 69 6e 74 20 6e 45 78 74  pNew;.  int nExt
20afe 72 61 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 56  ra = 0;.  int iV
20aff 61 6c 75 65 20 3d 20 30 3b 0a 0a 20 20 69 66 28  alue = 0;..  if(
20b00 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69   pToken ){.    i
20b01 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 54 45 47 45  f( op!=TK_INTEGE
20b02 52 20 7c 7c 20 70 54 6f 6b 65 6e 2d 3e 7a 3d 3d  R || pToken->z==
20b03 30 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73  0.          || s
20b04 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 70  qlite3GetInt32(p
20b05 54 6f 6b 65 6e 2d 3e 7a 2c 20 26 69 56 61 6c 75  Token->z, &iValu
20b06 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e  e)==0 ){.      n
20b07 45 78 74 72 61 20 3d 20 70 54 6f 6b 65 6e 2d 3e  Extra = pToken->
20b08 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n+1;.    }.  }. 
20b09 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
20b0a 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
20b0b 73 69 7a 65 6f 66 28 45 78 70 72 29 2b 6e 45 78  sizeof(Expr)+nEx
20b0c 74 72 61 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  tra);.  if( pNew
20b0d 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6f 70   ){.    pNew->op
20b0e 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 20 20 70   = (u8)op;.    p
20b0f 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d 31 3b 0a  New->iAgg = -1;.
20b10 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29      if( pToken )
20b11 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 45 78 74  {.      if( nExt
20b12 72 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ra==0 ){.       
20b13 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20   pNew->flags |= 
20b14 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20  EP_IntValue;.   
20b15 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 69 56 61       pNew->u.iVa
20b16 6c 75 65 20 3d 20 69 56 61 6c 75 65 3b 0a 20 20  lue = iValue;.  
20b17 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20b18 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20     int c;.      
20b19 20 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e    pNew->u.zToken
20b1a 20 3d 20 28 63 68 61 72 2a 29 26 70 4e 65 77 5b   = (char*)&pNew[
20b1b 31 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  1];.        memc
20b1c 70 79 28 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65  py(pNew->u.zToke
20b1d 6e 2c 20 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 70 54  n, pToken->z, pT
20b1e 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20 20 20 20 20  oken->n);.      
20b1f 20 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e    pNew->u.zToken
20b20 5b 70 54 6f 6b 65 6e 2d 3e 6e 5d 20 3d 20 30 3b  [pToken->n] = 0;
20b21 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 65 71  .        if( deq
20b22 75 6f 74 65 20 26 26 20 6e 45 78 74 72 61 3e 3d  uote && nExtra>=
20b23 33 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  3 .             
20b24 26 26 20 28 28 63 20 3d 20 70 54 6f 6b 65 6e 2d  && ((c = pToken-
20b25 3e 7a 5b 30 5d 29 3d 3d 27 5c 27 27 20 7c 7c 20  >z[0])=='\'' || 
20b26 63 3d 3d 27 22 27 20 7c 7c 20 63 3d 3d 27 5b 27  c=='"' || c=='['
20b27 20 7c 7c 20 63 3d 3d 27 60 27 29 20 29 7b 0a 20   || c=='`') ){. 
20b28 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
20b29 44 65 71 75 6f 74 65 28 70 4e 65 77 2d 3e 75 2e  Dequote(pNew->u.
20b2a 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20  zToken);.       
20b2b 20 20 20 69 66 28 20 63 3d 3d 27 22 27 20 29 20     if( c=='"' ) 
20b2c 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  pNew->flags |= E
20b2d 50 5f 44 62 6c 51 75 6f 74 65 64 3b 0a 20 20 20  P_DblQuoted;.   
20b2e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
20b2f 20 20 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f     }.#if SQLITE_
20b30 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30  MAX_EXPR_DEPTH>0
20b31 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 48 65 69 67  .    pNew->nHeig
20b32 68 74 20 3d 20 31 3b 0a 23 65 6e 64 69 66 20 20  ht = 1;.#endif  
20b33 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e  .  }.  return pN
20b34 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  ew;.}../*.** All
20b35 6f 63 61 74 65 20 61 20 6e 65 77 20 65 78 70 72  ocate a new expr
20b36 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 72 6f 6d  ession node from
20b37 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74   a zero-terminat
20b38 65 64 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 61  ed token that ha
20b39 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65  s.** already bee
20b3a 6e 20 64 65 71 75 6f 74 65 64 2e 0a 2a 2f 0a 53  n dequoted..*/.S
20b3b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
20b3c 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28  pr *sqlite3Expr(
20b3d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
20b3e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
20b3f 6e 64 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33  ndle for sqlite3
20b40 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28  DbMallocZero() (
20b41 6d 61 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a  may be null) */.
20b42 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
20b43 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
20b44 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a  ression opcode *
20b45 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
20b46 7a 54 6f 6b 65 6e 20 20 20 20 20 20 2f 2a 20 54  zToken      /* T
20b47 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20 20  oken argument.  
20b48 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f  Might be NULL */
20b49 0a 29 7b 0a 20 20 54 6f 6b 65 6e 20 78 3b 0a 20  .){.  Token x;. 
20b4a 20 78 2e 7a 20 3d 20 7a 54 6f 6b 65 6e 3b 0a 20   x.z = zToken;. 
20b4b 20 78 2e 6e 20 3d 20 7a 54 6f 6b 65 6e 20 3f 20   x.n = zToken ? 
20b4c 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
20b4d 7a 54 6f 6b 65 6e 29 20 3a 20 30 3b 0a 20 20 72  zToken) : 0;.  r
20b4e 65 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70  eturn sqlite3Exp
20b4f 72 41 6c 6c 6f 63 28 64 62 2c 20 6f 70 2c 20 26  rAlloc(db, op, &
20b50 78 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  x, 0);.}../*.** 
20b51 41 74 74 61 63 68 20 73 75 62 74 72 65 65 73 20  Attach subtrees 
20b52 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74  pLeft and pRight
20b53 20 74 6f 20 74 68 65 20 45 78 70 72 20 6e 6f 64   to the Expr nod
20b54 65 20 70 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 49  e pRoot..**.** I
20b55 66 20 70 52 6f 6f 74 3d 3d 4e 55 4c 4c 20 74 68  f pRoot==NULL th
20b56 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  at means that a 
20b57 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
20b58 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  n error has occu
20b59 72 72 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 61 74  rred..** In that
20b5a 20 63 61 73 65 2c 20 64 65 6c 65 74 65 20 74 68   case, delete th
20b5b 65 20 73 75 62 74 72 65 65 73 20 70 4c 65 66 74  e subtrees pLeft
20b5c 20 61 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2f 0a   and pRight..*/.
20b5d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
20b5e 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
20b5f 74 74 61 63 68 53 75 62 74 72 65 65 73 28 0a 20  ttachSubtrees(. 
20b60 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20   sqlite3 *db,.  
20b61 45 78 70 72 20 2a 70 52 6f 6f 74 2c 0a 20 20 45  Expr *pRoot,.  E
20b62 78 70 72 20 2a 70 4c 65 66 74 2c 0a 20 20 45 78  xpr *pLeft,.  Ex
20b63 70 72 20 2a 70 52 69 67 68 74 0a 29 7b 0a 20 20  pr *pRight.){.  
20b64 69 66 28 20 70 52 6f 6f 74 3d 3d 30 20 29 7b 0a  if( pRoot==0 ){.
20b65 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
20b66 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
20b67 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
20b68 65 6c 65 74 65 28 64 62 2c 20 70 4c 65 66 74 29  elete(db, pLeft)
20b69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
20b6a 72 44 65 6c 65 74 65 28 64 62 2c 20 70 52 69 67  rDelete(db, pRig
20b6b 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ht);.  }else{.  
20b6c 20 20 69 66 28 20 70 52 69 67 68 74 20 29 7b 0a    if( pRight ){.
20b6d 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70 52 69        pRoot->pRi
20b6e 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a 20 20  ght = pRight;.  
20b6f 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e      if( pRight->
20b70 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
20b71 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20 20 20  llate ){.       
20b72 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20 7c 3d   pRoot->flags |=
20b73 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a   EP_ExpCollate;.
20b74 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 70          pRoot->p
20b75 43 6f 6c 6c 20 3d 20 70 52 69 67 68 74 2d 3e 70  Coll = pRight->p
20b76 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
20b77 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 66    }.    if( pLef
20b78 74 20 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74  t ){.      pRoot
20b79 2d 3e 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b  ->pLeft = pLeft;
20b7a 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 66 74  .      if( pLeft
20b7b 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
20b7c 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 20  Collate ){.     
20b7d 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61 67 73 20     pRoot->flags 
20b7e 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65  |= EP_ExpCollate
20b7f 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d  ;.        pRoot-
20b80 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e  >pColl = pLeft->
20b81 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  pColl;.      }. 
20b82 20 20 20 7d 0a 20 20 20 20 65 78 70 72 53 65 74     }.    exprSet
20b83 48 65 69 67 68 74 28 70 52 6f 6f 74 29 3b 0a 20  Height(pRoot);. 
20b84 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f   }.}../*.** Allo
20b85 63 61 74 65 20 61 20 45 78 70 72 20 6e 6f 64 65  cate a Expr node
20b86 20 77 68 69 63 68 20 6a 6f 69 6e 73 20 61 73 20   which joins as 
20b87 6d 61 6e 79 20 61 73 20 74 77 6f 20 73 75 62 74  many as two subt
20b88 72 65 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20  rees..**.** One 
20b89 6f 72 20 62 6f 74 68 20 6f 66 20 74 68 65 20 73  or both of the s
20b8a 75 62 74 72 65 65 73 20 63 61 6e 20 62 65 20 4e  ubtrees can be N
20b8b 55 4c 4c 2e 20 20 52 65 74 75 72 6e 20 61 20 70  ULL.  Return a p
20b8c 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
20b8d 77 0a 2a 2a 20 45 78 70 72 20 6e 6f 64 65 2e 20  w.** Expr node. 
20b8e 20 4f 72 2c 20 69 66 20 61 6e 20 4f 4f 4d 20 65   Or, if an OOM e
20b8f 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 73 65 74  rror occurs, set
20b90 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c   pParse->db->mal
20b91 6c 6f 63 46 61 69 6c 65 64 2c 0a 2a 2a 20 66 72  locFailed,.** fr
20b92 65 65 20 74 68 65 20 73 75 62 74 72 65 65 73 20  ee the subtrees 
20b93 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  and return NULL.
20b94 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20b95 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
20b96 50 45 78 70 72 28 0a 20 20 50 61 72 73 65 20 2a  PExpr(.  Parse *
20b97 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
20b98 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
20b99 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c  ext */.  int op,
20b9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20b9b 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f   /* Expression o
20b9c 70 63 6f 64 65 20 2a 2f 0a 20 20 45 78 70 72 20  pcode */.  Expr 
20b9d 2a 70 4c 65 66 74 2c 20 20 20 20 20 20 20 20 20  *pLeft,         
20b9e 20 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61     /* Left opera
20b9f 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52  nd */.  Expr *pR
20ba0 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20  ight,           
20ba1 2f 2a 20 52 69 67 68 74 20 6f 70 65 72 61 6e 64  /* Right operand
20ba2 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f 6b 65   */.  const Toke
20ba3 6e 20 2a 70 54 6f 6b 65 6e 20 20 20 20 20 2f 2a  n *pToken     /*
20ba4 20 41 72 67 75 6d 65 6e 74 20 74 6f 6b 65 6e 20   Argument token 
20ba5 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70 20  */.){.  Expr *p 
20ba6 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c 6c  = sqlite3ExprAll
20ba7 6f 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 6f  oc(pParse->db, o
20ba8 70 2c 20 70 54 6f 6b 65 6e 2c 20 31 29 3b 0a 20  p, pToken, 1);. 
20ba9 20 73 71 6c 69 74 65 33 45 78 70 72 41 74 74 61   sqlite3ExprAtta
20baa 63 68 53 75 62 74 72 65 65 73 28 70 50 61 72 73  chSubtrees(pPars
20bab 65 2d 3e 64 62 2c 20 70 2c 20 70 4c 65 66 74 2c  e->db, p, pLeft,
20bac 20 70 52 69 67 68 74 29 3b 0a 20 20 72 65 74 75   pRight);.  retu
20bad 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4a  rn p;.}../*.** J
20bae 6f 69 6e 20 74 77 6f 20 65 78 70 72 65 73 73 69  oin two expressi
20baf 6f 6e 73 20 75 73 69 6e 67 20 61 6e 20 41 4e 44  ons using an AND
20bb0 20 6f 70 65 72 61 74 6f 72 2e 20 20 49 66 20 65   operator.  If e
20bb1 69 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  ither expression
20bb2 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c 20 74 68 65   is.** NULL, the
20bb3 6e 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68  n just return th
20bb4 65 20 6f 74 68 65 72 20 65 78 70 72 65 73 73 69  e other expressi
20bb5 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
20bb6 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69  IVATE Expr *sqli
20bb7 74 65 33 45 78 70 72 41 6e 64 28 73 71 6c 69 74  te3ExprAnd(sqlit
20bb8 65 33 20 2a 64 62 2c 20 45 78 70 72 20 2a 70 4c  e3 *db, Expr *pL
20bb9 65 66 74 2c 20 45 78 70 72 20 2a 70 52 69 67 68  eft, Expr *pRigh
20bba 74 29 7b 0a 20 20 69 66 28 20 70 4c 65 66 74 3d  t){.  if( pLeft=
20bbb 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
20bbc 20 70 52 69 67 68 74 3b 0a 20 20 7d 65 6c 73 65   pRight;.  }else
20bbd 20 69 66 28 20 70 52 69 67 68 74 3d 3d 30 20 29   if( pRight==0 )
20bbe 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 4c 65  {.    return pLe
20bbf 66 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ft;.  }else{.   
20bc0 20 45 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71   Expr *pNew = sq
20bc1 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64  lite3ExprAlloc(d
20bc2 62 2c 20 54 4b 5f 41 4e 44 2c 20 30 2c 20 30 29  b, TK_AND, 0, 0)
20bc3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
20bc4 72 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28  rAttachSubtrees(
20bc5 64 62 2c 20 70 4e 65 77 2c 20 70 4c 65 66 74 2c  db, pNew, pLeft,
20bc6 20 70 52 69 67 68 74 29 3b 0a 20 20 20 20 72 65   pRight);.    re
20bc7 74 75 72 6e 20 70 4e 65 77 3b 0a 20 20 7d 0a 7d  turn pNew;.  }.}
20bc8 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63  ../*.** Construc
20bc9 74 20 61 20 6e 65 77 20 65 78 70 72 65 73 73 69  t a new expressi
20bca 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 66 75  on node for a fu
20bcb 6e 63 74 69 6f 6e 20 77 69 74 68 20 6d 75 6c 74  nction with mult
20bcc 69 70 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  iple.** argument
20bcd 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
20bce 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
20bcf 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50  e3ExprFunction(P
20bd0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
20bd1 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 54  prList *pList, T
20bd2 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20  oken *pToken){. 
20bd3 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 73   Expr *pNew;.  s
20bd4 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
20bd5 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72  rse->db;.  asser
20bd6 74 28 20 70 54 6f 6b 65 6e 20 29 3b 0a 20 20 70  t( pToken );.  p
20bd7 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
20bd8 72 41 6c 6c 6f 63 28 64 62 2c 20 54 4b 5f 46 55  rAlloc(db, TK_FU
20bd9 4e 43 54 49 4f 4e 2c 20 70 54 6f 6b 65 6e 2c 20  NCTION, pToken, 
20bda 31 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d  1);.  if( pNew==
20bdb 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
20bdc 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
20bdd 62 2c 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76  b, pList); /* Av
20bde 6f 69 64 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20  oid memory leak 
20bdf 77 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  when malloc fail
20be0 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  s */.    return 
20be1 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 78  0;.  }.  pNew->x
20be2 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b 0a  .pList = pList;.
20be3 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
20be4 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c  asProperty(pNew,
20be5 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
20be6 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53  ;.  sqlite3ExprS
20be7 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c  etHeight(pParse,
20be8 20 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72 6e   pNew);.  return
20be9 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pNew;.}../*.** 
20bea 41 73 73 69 67 6e 20 61 20 76 61 72 69 61 62 6c  Assign a variabl
20beb 65 20 6e 75 6d 62 65 72 20 74 6f 20 61 6e 20 65  e number to an e
20bec 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 65  xpression that e
20bed 6e 63 6f 64 65 73 20 61 20 77 69 6c 64 63 61 72  ncodes a wildcar
20bee 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67  d.** in the orig
20bef 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65 6d 65  inal SQL stateme
20bf0 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 69 6c 64  nt.  .**.** Wild
20bf1 63 61 72 64 73 20 63 6f 6e 73 69 73 74 69 6e 67  cards consisting
20bf2 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 22 3f 22   of a single "?"
20bf3 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68   are assigned th
20bf4 65 20 6e 65 78 74 20 73 65 71 75 65 6e 74 69 61  e next sequentia
20bf5 6c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e 75  l.** variable nu
20bf6 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64  mber..**.** Wild
20bf7 63 61 72 64 73 20 6f 66 20 74 68 65 20 66 6f 72  cards of the for
20bf8 6d 20 22 3f 6e 6e 6e 22 20 61 72 65 20 61 73 73  m "?nnn" are ass
20bf9 69 67 6e 65 64 20 74 68 65 20 6e 75 6d 62 65 72  igned the number
20bfa 20 22 6e 6e 6e 22 2e 20 20 57 65 20 6d 61 6b 65   "nnn".  We make
20bfb 0a 2a 2a 20 73 75 72 65 20 22 6e 6e 6e 22 20 69  .** sure "nnn" i
20bfc 73 20 6e 6f 74 20 74 6f 6f 20 62 65 20 74 6f 20  s not too be to 
20bfd 61 76 6f 69 64 20 61 20 64 65 6e 69 61 6c 20 6f  avoid a denial o
20bfe 66 20 73 65 72 76 69 63 65 20 61 74 74 61 63 6b  f service attack
20bff 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c   when.** the SQL
20c00 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73   statement comes
20c01 20 66 72 6f 6d 20 61 6e 20 65 78 74 65 72 6e 61   from an externa
20c02 6c 20 73 6f 75 72 63 65 2e 0a 2a 2a 0a 2a 2a 20  l source..**.** 
20c03 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68 65  Wildcards of the
20c04 20 66 6f 72 6d 20 22 3a 61 61 61 22 2c 20 22 40   form ":aaa", "@
20c05 61 61 61 22 2c 20 6f 72 20 22 24 61 61 61 22 20  aaa", or "$aaa" 
20c06 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65  are assigned the
20c07 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20   same number.** 
20c08 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  as the previous 
20c09 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
20c0a 73 61 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20  same wildcard.  
20c0b 4f 72 20 69 66 20 74 68 69 73 20 69 73 20 74 68  Or if this is th
20c0c 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61  e first.** insta
20c0d 6e 63 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63  nce of the wildc
20c0e 61 72 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65  ard, the next se
20c0f 71 75 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65  quenial variable
20c10 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73   number is.** as
20c11 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  signed..*/.SQLIT
20c12 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
20c13 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e  qlite3ExprAssign
20c14 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20  VarNumber(Parse 
20c15 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
20c16 45 78 70 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  Expr){.  sqlite3
20c17 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
20c18 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b;.  const char 
20c19 2a 7a 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72  *z;..  if( pExpr
20c1a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
20c1b 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
20c1c 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70  AnyProperty(pExp
20c1d 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 7c 45  r, EP_IntValue|E
20c1e 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b  P_Reduced|EP_Tok
20c1f 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20 7a 20 3d  enOnly) );.  z =
20c20 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
20c21 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30  ;.  assert( z!=0
20c22 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 5b   );.  assert( z[
20c23 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 7a  0]!=0 );.  if( z
20c24 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  [1]==0 ){.    /*
20c25 20 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65   Wildcard of the
20c26 20 66 6f 72 6d 20 22 3f 22 2e 20 20 41 73 73 69   form "?".  Assi
20c27 67 6e 20 74 68 65 20 6e 65 78 74 20 76 61 72 69  gn the next vari
20c28 61 62 6c 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20  able number */. 
20c29 20 20 20 61 73 73 65 72 74 28 20 7a 5b 30 5d 3d     assert( z[0]=
20c2a 3d 27 3f 27 20 29 3b 0a 20 20 20 20 70 45 78 70  ='?' );.    pExp
20c2b 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 28 79 6e  r->iColumn = (yn
20c2c 56 61 72 29 28 2b 2b 70 50 61 72 73 65 2d 3e 6e  Var)(++pParse->n
20c2d 56 61 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Var);.  }else if
20c2e 28 20 7a 5b 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20  ( z[0]=='?' ){. 
20c2f 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f     /* Wildcard o
20c30 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e  f the form "?nnn
20c31 22 2e 20 20 43 6f 6e 76 65 72 74 20 22 6e 6e 6e  ".  Convert "nnn
20c32 22 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  " to an integer 
20c33 61 6e 64 0a 20 20 20 20 2a 2a 20 75 73 65 20 69  and.    ** use i
20c34 74 20 61 73 20 74 68 65 20 76 61 72 69 61 62 6c  t as the variabl
20c35 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  e number */.    
20c36 69 6e 74 20 69 20 3d 20 61 74 6f 69 28 28 63 68  int i = atoi((ch
20c37 61 72 2a 29 26 7a 5b 31 5d 29 3b 0a 20 20 20 20  ar*)&z[1]);.    
20c38 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  pExpr->iColumn =
20c39 20 28 79 6e 56 61 72 29 69 3b 0a 20 20 20 20 74   (ynVar)i;.    t
20c3a 65 73 74 63 61 73 65 28 20 69 3d 3d 30 20 29 3b  estcase( i==0 );
20c3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69  .    testcase( i
20c3c 3d 3d 31 20 29 3b 0a 20 20 20 20 74 65 73 74 63  ==1 );.    testc
20c3d 61 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d  ase( i==db->aLim
20c3e 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
20c3f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d  VARIABLE_NUMBER]
20c40 2d 31 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  -1 );.    testca
20c41 73 65 28 20 69 3d 3d 64 62 2d 3e 61 4c 69 6d 69  se( i==db->aLimi
20c42 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  t[SQLITE_LIMIT_V
20c43 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20  ARIABLE_NUMBER] 
20c44 29 3b 0a 20 20 20 20 69 66 28 20 69 3c 31 20 7c  );.    if( i<1 |
20c45 7c 20 69 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  | i>db->aLimit[S
20c46 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49  QLITE_LIMIT_VARI
20c47 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b 0a  ABLE_NUMBER] ){.
20c48 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
20c49 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76  orMsg(pParse, "v
20c4a 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 6d  ariable number m
20c4b 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 3f  ust be between ?
20c4c 31 20 61 6e 64 20 3f 25 64 22 2c 0a 20 20 20 20  1 and ?%d",.    
20c4d 20 20 20 20 20 20 64 62 2d 3e 61 4c 69 6d 69 74        db->aLimit
20c4e 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41  [SQLITE_LIMIT_VA
20c4f 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 29 3b  RIABLE_NUMBER]);
20c50 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
20c51 3e 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 29 7b  >pParse->nVar ){
20c52 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
20c53 56 61 72 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20  Var = i;.    }. 
20c54 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 57   }else{.    /* W
20c55 69 6c 64 63 61 72 64 73 20 6c 69 6b 65 20 22 3a  ildcards like ":
20c56 61 61 61 22 2c 20 22 24 61 61 61 22 20 6f 72 20  aaa", "$aaa" or 
20c57 22 40 61 61 61 22 2e 20 20 52 65 75 73 65 20 74  "@aaa".  Reuse t
20c58 68 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c 65  he same variable
20c59 0a 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 61  .    ** number a
20c5a 73 20 74 68 65 20 70 72 69 6f 72 20 61 70 70 65  s the prior appe
20c5b 61 72 61 6e 63 65 20 6f 66 20 74 68 65 20 73 61  arance of the sa
20c5c 6d 65 20 6e 61 6d 65 2c 20 6f 72 20 69 66 20 74  me name, or if t
20c5d 68 65 20 6e 61 6d 65 0a 20 20 20 20 2a 2a 20 68  he name.    ** h
20c5e 61 73 20 6e 65 76 65 72 20 61 70 70 65 61 72 65  as never appeare
20c5f 64 20 62 65 66 6f 72 65 2c 20 72 65 75 73 65 20  d before, reuse 
20c60 74 68 65 20 73 61 6d 65 20 76 61 72 69 61 62 6c  the same variabl
20c61 65 20 6e 75 6d 62 65 72 0a 20 20 20 20 2a 2f 0a  e number.    */.
20c62 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 75      int i;.    u
20c63 33 32 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 73 71  32 n;.    n = sq
20c64 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29  lite3Strlen30(z)
20c65 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
20c66 3c 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70  <pParse->nVarExp
20c67 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45  r; i++){.      E
20c68 78 70 72 20 2a 70 45 20 3d 20 70 50 61 72 73 65  xpr *pE = pParse
20c69 2d 3e 61 70 56 61 72 45 78 70 72 5b 69 5d 3b 0a  ->apVarExpr[i];.
20c6a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
20c6b 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  !=0 );.      if(
20c6c 20 6d 65 6d 63 6d 70 28 70 45 2d 3e 75 2e 7a 54   memcmp(pE->u.zT
20c6d 6f 6b 65 6e 2c 20 7a 2c 20 6e 29 3d 3d 30 20 26  oken, z, n)==0 &
20c6e 26 20 70 45 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 6e  & pE->u.zToken[n
20c6f 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ]==0 ){.        
20c70 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  pExpr->iColumn =
20c71 20 70 45 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20   pE->iColumn;.  
20c72 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
20c73 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
20c74 66 28 20 69 3e 3d 70 50 61 72 73 65 2d 3e 6e 56  f( i>=pParse->nV
20c75 61 72 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20  arExpr ){.      
20c76 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  pExpr->iColumn =
20c77 20 28 79 6e 56 61 72 29 28 2b 2b 70 50 61 72 73   (ynVar)(++pPars
20c78 65 2d 3e 6e 56 61 72 29 3b 0a 20 20 20 20 20 20  e->nVar);.      
20c79 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72  if( pParse->nVar
20c7a 45 78 70 72 3e 3d 70 50 61 72 73 65 2d 3e 6e 56  Expr>=pParse->nV
20c7b 61 72 45 78 70 72 41 6c 6c 6f 63 2d 31 20 29 7b  arExprAlloc-1 ){
20c7c 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
20c7d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b  >nVarExprAlloc +
20c7e 3d 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78  = pParse->nVarEx
20c7f 70 72 41 6c 6c 6f 63 20 2b 20 31 30 3b 0a 20 20  prAlloc + 10;.  
20c80 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70        pParse->ap
20c81 56 61 72 45 78 70 72 20 3d 0a 20 20 20 20 20 20  VarExpr =.      
20c82 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 52        sqlite3DbR
20c83 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 0a 20 20  eallocOrFree(.  
20c84 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2c 0a              db,.
20c85 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50                pP
20c86 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 2c  arse->apVarExpr,
20c87 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
20c88 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41  Parse->nVarExprA
20c89 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 50 61 72  lloc*sizeof(pPar
20c8a 73 65 2d 3e 61 70 56 61 72 45 78 70 72 5b 30 5d  se->apVarExpr[0]
20c8b 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 3b  ).            );
20c8c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
20c8d 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
20c8e 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  iled ){.        
20c8f 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
20c90 61 70 56 61 72 45 78 70 72 21 3d 30 20 29 3b 0a  apVarExpr!=0 );.
20c91 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
20c92 61 70 56 61 72 45 78 70 72 5b 70 50 61 72 73 65  apVarExpr[pParse
20c93 2d 3e 6e 56 61 72 45 78 70 72 2b 2b 5d 20 3d 20  ->nVarExpr++] = 
20c94 70 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20  pExpr;.      }. 
20c95 20 20 20 7d 0a 20 20 7d 20 0a 20 20 69 66 28 20     }.  } .  if( 
20c96 21 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 26 26  !pParse->nErr &&
20c97 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3e 64 62   pParse->nVar>db
20c98 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
20c99 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
20c9a 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 73 71  UMBER] ){.    sq
20c9b 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
20c9c 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
20c9d 53 51 4c 20 76 61 72 69 61 62 6c 65 73 22 29 3b  SQL variables");
20c9e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
20c9f 63 75 72 73 69 76 65 6c 79 20 64 65 6c 65 74 65  cursively delete
20ca0 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
20ca1 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ree..*/.SQLITE_P
20ca2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
20ca3 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 73 71  te3ExprDelete(sq
20ca4 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20  lite3 *db, Expr 
20ca5 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20  *p){.  if( p==0 
20ca6 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
20ca7 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
20ca8 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e 4f  rty(p, EP_TokenO
20ca9 6e 6c 79 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  nly) ){.    sqli
20caa 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
20cab 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20  , p->pLeft);.   
20cac 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
20cad 74 65 28 64 62 2c 20 70 2d 3e 70 52 69 67 68 74  te(db, p->pRight
20cae 29 3b 0a 20 20 20 20 69 66 28 20 21 45 78 70 72  );.    if( !Expr
20caf 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45  HasProperty(p, E
20cb0 50 5f 52 65 64 75 63 65 64 29 20 26 26 20 28 70  P_Reduced) && (p
20cb1 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 4d  ->flags2 & EP2_M
20cb2 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 21 3d 30  allocedToken)!=0
20cb3 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
20cb4 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 75  3DbFree(db, p->u
20cb5 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a  .zToken);.    }.
20cb6 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50      if( ExprHasP
20cb7 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49  roperty(p, EP_xI
20cb8 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
20cb9 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
20cba 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 78 2e 70  elete(db, p->x.p
20cbb 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 7d 65 6c  Select);.    }el
20cbc 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
20cbd 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
20cbe 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 74 29 3b  db, p->x.pList);
20cbf 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
20cc0 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
20cc1 79 28 70 2c 20 45 50 5f 53 74 61 74 69 63 29 20  y(p, EP_Static) 
20cc2 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  ){.    sqlite3Db
20cc3 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d  Free(db, p);.  }
20cc4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
20cc5 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
20cc6 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  ytes allocated f
20cc7 6f 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  or the expressio
20cc8 6e 20 73 74 72 75 63 74 75 72 65 20 0a 2a 2a 20  n structure .** 
20cc9 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
20cca 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68  rst argument. Th
20ccb 69 73 20 69 73 20 61 6c 77 61 79 73 20 6f 6e 65  is is always one
20ccc 20 6f 66 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a   of EXPR_FULLSIZ
20ccd 45 2c 0a 2a 2a 20 45 58 50 52 5f 52 45 44 55 43  E,.** EXPR_REDUC
20cce 45 44 53 49 5a 45 20 6f 72 20 45 58 50 52 5f 54  EDSIZE or EXPR_T
20ccf 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 0a 2a 2f  OKENONLYSIZE..*/
20cd0 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72  .static int expr
20cd1 53 74 72 75 63 74 53 69 7a 65 28 45 78 70 72 20  StructSize(Expr 
20cd2 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 70 72 48  *p){.  if( ExprH
20cd3 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  asProperty(p, EP
20cd4 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 20 72 65  _TokenOnly) ) re
20cd5 74 75 72 6e 20 45 58 50 52 5f 54 4f 4b 45 4e 4f  turn EXPR_TOKENO
20cd6 4e 4c 59 53 49 5a 45 3b 0a 20 20 69 66 28 20 45  NLYSIZE;.  if( E
20cd7 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20cd8 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 20  , EP_Reduced) ) 
20cd9 72 65 74 75 72 6e 20 45 58 50 52 5f 52 45 44 55  return EXPR_REDU
20cda 43 45 44 53 49 5a 45 3b 0a 20 20 72 65 74 75 72  CEDSIZE;.  retur
20cdb 6e 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 3b  n EXPR_FULLSIZE;
20cdc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 75  .}../*.** The du
20cdd 70 65 64 45 78 70 72 2a 53 69 7a 65 28 29 20 72  pedExpr*Size() r
20cde 6f 75 74 69 6e 65 73 20 65 61 63 68 20 72 65 74  outines each ret
20cdf 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
20ce0 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64  f bytes required
20ce1 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20 61 20 63  .** to store a c
20ce2 6f 70 79 20 6f 66 20 61 6e 20 65 78 70 72 65 73  opy of an expres
20ce3 73 69 6f 6e 20 6f 72 20 65 78 70 72 65 73 73 69  sion or expressi
20ce4 6f 6e 20 74 72 65 65 2e 20 20 54 68 65 79 20 64  on tree.  They d
20ce5 69 66 66 65 72 20 69 6e 0a 2a 2a 20 68 6f 77 20  iffer in.** how 
20ce6 6d 75 63 68 20 6f 66 20 74 68 65 20 74 72 65 65  much of the tree
20ce7 20 69 73 20 6d 65 61 73 75 72 65 64 2e 0a 2a 2a   is measured..**
20ce8 0a 2a 2a 20 20 20 20 20 64 75 70 65 64 45 78 70  .**     dupedExp
20ce9 72 53 74 72 75 63 74 53 69 7a 65 28 29 20 20 20  rStructSize()   
20cea 20 20 53 69 7a 65 20 6f 66 20 6f 6e 6c 79 20 74    Size of only t
20ceb 68 65 20 45 78 70 72 20 73 74 72 75 63 74 75 72  he Expr structur
20cec 65 20 0a 2a 2a 20 20 20 20 20 64 75 70 65 64 45  e .**     dupedE
20ced 78 70 72 4e 6f 64 65 53 69 7a 65 28 29 20 20 20  xprNodeSize()   
20cee 20 20 20 20 53 69 7a 65 20 6f 66 20 45 78 70 72      Size of Expr
20cef 20 2b 20 73 70 61 63 65 20 66 6f 72 20 74 6f 6b   + space for tok
20cf0 65 6e 0a 2a 2a 20 20 20 20 20 64 75 70 65 64 45  en.**     dupedE
20cf1 78 70 72 53 69 7a 65 28 29 20 20 20 20 20 20 20  xprSize()       
20cf2 20 20 20 20 45 78 70 72 20 2b 20 74 6f 6b 65 6e      Expr + token
20cf3 20 2b 20 73 75 62 74 72 65 65 20 63 6f 6d 70 6f   + subtree compo
20cf4 6e 65 6e 74 73 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  nents.**.*******
20cf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20cf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20cf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20cf8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20cf9 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  ****.**.** The d
20cfa 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69  upedExprStructSi
20cfb 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 72 65  ze() function re
20cfc 74 75 72 6e 73 20 74 77 6f 20 76 61 6c 75 65 73  turns two values
20cfd 20 4f 52 2d 65 64 20 74 6f 67 65 74 68 65 72 3a   OR-ed together:
20cfe 20 20 0a 2a 2a 20 28 31 29 20 74 68 65 20 73 70    .** (1) the sp
20cff 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72  ace required for
20d00 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45   a copy of the E
20d01 78 70 72 20 73 74 72 75 63 74 75 72 65 20 6f 6e  xpr structure on
20d02 6c 79 20 61 6e 64 20 0a 2a 2a 20 28 32 29 20 74  ly and .** (2) t
20d03 68 65 20 45 50 5f 78 78 78 20 66 6c 61 67 73 20  he EP_xxx flags 
20d04 74 68 61 74 20 69 6e 64 69 63 61 74 65 20 77 68  that indicate wh
20d05 61 74 20 74 68 65 20 73 74 72 75 63 74 75 72 65  at the structure
20d06 20 73 69 7a 65 20 73 68 6f 75 6c 64 20 62 65 2e   size should be.
20d07 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
20d08 61 6c 75 65 73 20 69 73 20 61 6c 77 61 79 73 20  alues is always 
20d09 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20 20 20  one of:.**.**   
20d0a 20 20 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45     EXPR_FULLSIZE
20d0b 0a 2a 2a 20 20 20 20 20 20 45 58 50 52 5f 52 45  .**      EXPR_RE
20d0c 44 55 43 45 44 53 49 5a 45 20 20 20 7c 20 45 50  DUCEDSIZE   | EP
20d0d 5f 52 65 64 75 63 65 64 0a 2a 2a 20 20 20 20 20  _Reduced.**     
20d0e 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53   EXPR_TOKENONLYS
20d0f 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  IZE | EP_TokenOn
20d10 6c 79 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 69 7a  ly.**.** The siz
20d11 65 20 6f 66 20 74 68 65 20 73 74 72 75 63 74 75  e of the structu
20d12 72 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 20  re can be found 
20d13 62 79 20 6d 61 73 6b 69 6e 67 20 74 68 65 20 72  by masking the r
20d14 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f  eturn value.** o
20d15 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  f this routine w
20d16 69 74 68 20 30 78 66 66 66 2e 20 20 54 68 65 20  ith 0xfff.  The 
20d17 66 6c 61 67 73 20 63 61 6e 20 62 65 20 66 6f 75  flags can be fou
20d18 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 20 74 68  nd by masking th
20d19 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75  e.** return valu
20d1a 65 20 77 69 74 68 20 45 50 5f 52 65 64 75 63 65  e with EP_Reduce
20d1b 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 2e 0a  d|EP_TokenOnly..
20d1c 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
20d1d 77 69 74 68 20 66 6c 61 67 73 3d 3d 45 58 50 52  with flags==EXPR
20d1e 44 55 50 5f 52 45 44 55 43 45 2c 20 74 68 69 73  DUP_REDUCE, this
20d1f 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 73 20   routines works 
20d20 6f 6e 20 66 75 6c 6c 2d 73 69 7a 65 0a 2a 2a 20  on full-size.** 
20d21 28 75 6e 72 65 64 75 63 65 64 29 20 45 78 70 72  (unreduced) Expr
20d22 20 6f 62 6a 65 63 74 73 20 61 73 20 74 68 65 79   objects as they
20d23 20 6f 72 20 6f 72 69 67 69 6e 61 6c 6c 79 20 63   or originally c
20d24 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20 74 68  onstructed by th
20d25 65 20 70 61 72 73 65 72 2e 0a 2a 2a 20 44 75 72  e parser..** Dur
20d26 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 20 61  ing expression a
20d27 6e 61 6c 79 73 69 73 2c 20 65 78 74 72 61 20 69  nalysis, extra i
20d28 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 63 6f  nformation is co
20d29 6d 70 75 74 65 64 20 61 6e 64 20 6d 6f 76 65 64  mputed and moved
20d2a 20 69 6e 74 6f 0a 2a 2a 20 6c 61 74 65 72 20 70   into.** later p
20d2b 61 72 74 73 20 6f 66 20 74 65 68 20 45 78 70 72  arts of teh Expr
20d2c 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 61 74   object and that
20d2d 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69   extra informati
20d2e 6f 6e 20 6d 69 67 68 74 20 67 65 74 20 63 68 6f  on might get cho
20d2f 70 70 65 64 0a 2a 2a 20 6f 66 66 20 69 66 20 74  pped.** off if t
20d30 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
20d31 20 72 65 64 75 63 65 64 2e 20 20 4e 6f 74 65 20   reduced.  Note 
20d32 61 6c 73 6f 20 74 68 61 74 20 69 74 20 64 6f 65  also that it doe
20d33 73 20 6e 6f 74 20 77 6f 72 6b 20 74 6f 0a 2a 2a  s not work to.**
20d34 20 6d 61 6b 65 20 61 20 45 58 50 52 44 55 50 5f   make a EXPRDUP_
20d35 52 45 44 55 43 45 20 63 6f 70 79 20 6f 66 20 61  REDUCE copy of a
20d36 20 72 65 64 75 63 65 64 20 65 78 70 72 65 73 73   reduced express
20d37 69 6f 6e 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79  ion.  It is only
20d38 20 6c 65 67 61 6c 0a 2a 2a 20 74 6f 20 72 65 64   legal.** to red
20d39 75 63 65 20 61 20 70 72 69 73 74 69 6e 65 20 65  uce a pristine e
20d3a 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 66  xpression tree f
20d3b 72 6f 6d 20 74 68 65 20 70 61 72 73 65 72 2e 20  rom the parser. 
20d3c 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
20d3d 69 6f 6e 0a 2a 2a 20 6f 66 20 64 75 70 65 64 45  ion.** of dupedE
20d3e 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 29 20  xprStructSize() 
20d3f 63 6f 6e 74 61 69 6e 20 6d 75 6c 74 69 70 6c 65  contain multiple
20d40 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
20d41 65 6e 74 73 20 74 68 61 74 20 61 74 74 65 6d 70  ents that attemp
20d42 74 0a 2a 2a 20 74 6f 20 65 6e 66 6f 72 63 65 20  t.** to enforce 
20d43 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e  this constraint.
20d44 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
20d45 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69  upedExprStructSi
20d46 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  ze(Expr *p, int 
20d47 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 6e 53  flags){.  int nS
20d48 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 66  ize;.  assert( f
20d49 6c 61 67 73 3d 3d 45 58 50 52 44 55 50 5f 52 45  lags==EXPRDUP_RE
20d4a 44 55 43 45 20 7c 7c 20 66 6c 61 67 73 3d 3d 30  DUCE || flags==0
20d4b 20 29 3b 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20   ); /* Only one 
20d4c 66 6c 61 67 20 76 61 6c 75 65 20 61 6c 6c 6f 77  flag value allow
20d4d 65 64 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 28  ed */.  if( 0==(
20d4e 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 45  flags&EXPRDUP_RE
20d4f 44 55 43 45 29 20 29 7b 0a 20 20 20 20 6e 53 69  DUCE) ){.    nSi
20d50 7a 65 20 3d 20 45 58 50 52 5f 46 55 4c 4c 53 49  ze = EXPR_FULLSI
20d51 5a 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ZE;.  }else{.   
20d52 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
20d53 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20  sAnyProperty(p, 
20d54 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f  EP_TokenOnly|EP_
20d55 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20  Reduced) );.    
20d56 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
20d57 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 46  Property(p, EP_F
20d58 72 6f 6d 4a 6f 69 6e 29 20 29 3b 20 0a 20 20 20  romJoin) ); .   
20d59 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61   assert( (p->fla
20d5a 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c 6f 63  gs2 & EP2_Malloc
20d5b 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 3b 0a 20  edToken)==0 );. 
20d5c 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66     assert( (p->f
20d5d 6c 61 67 73 32 20 26 20 45 50 32 5f 49 72 72 65  lags2 & EP2_Irre
20d5e 64 75 63 69 62 6c 65 29 3d 3d 30 20 29 3b 0a 20  ducible)==0 );. 
20d5f 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 20     if( p->pLeft 
20d60 7c 7c 20 70 2d 3e 70 52 69 67 68 74 20 7c 7c 20  || p->pRight || 
20d61 70 2d 3e 70 43 6f 6c 6c 20 7c 7c 20 70 2d 3e 78  p->pColl || p->x
20d62 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20  .pList ){.      
20d63 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 52 45 44  nSize = EXPR_RED
20d64 55 43 45 44 53 49 5a 45 20 7c 20 45 50 5f 52 65  UCEDSIZE | EP_Re
20d65 64 75 63 65 64 3b 0a 20 20 20 20 7d 65 6c 73 65  duced;.    }else
20d66 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20 3d 20  {.      nSize = 
20d67 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49  EXPR_TOKENONLYSI
20d68 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  ZE | EP_TokenOnl
20d69 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  y;.    }.  }.  r
20d6a 65 74 75 72 6e 20 6e 53 69 7a 65 3b 0a 7d 0a 0a  eturn nSize;.}..
20d6b 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
20d6c 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  ion returns the 
20d6d 73 70 61 63 65 20 69 6e 20 62 79 74 65 73 20 72  space in bytes r
20d6e 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65  equired to store
20d6f 20 74 68 65 20 63 6f 70 79 20 0a 2a 2a 20 6f 66   the copy .** of
20d70 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74   the Expr struct
20d71 75 72 65 20 61 6e 64 20 61 20 63 6f 70 79 20 6f  ure and a copy o
20d72 66 20 74 68 65 20 45 78 70 72 2e 75 2e 7a 54 6f  f the Expr.u.zTo
20d73 6b 65 6e 20 73 74 72 69 6e 67 20 28 69 66 20 74  ken string (if t
20d74 68 61 74 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73  hat.** string is
20d75 20 64 65 66 69 6e 65 64 2e 29 0a 2a 2f 0a 73 74   defined.).*/.st
20d76 61 74 69 63 20 69 6e 74 20 64 75 70 65 64 45 78  atic int dupedEx
20d77 70 72 4e 6f 64 65 53 69 7a 65 28 45 78 70 72 20  prNodeSize(Expr 
20d78 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  *p, int flags){.
20d79 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20 64 75    int nByte = du
20d7a 70 65 64 45 78 70 72 53 74 72 75 63 74 53 69 7a  pedExprStructSiz
20d7b 65 28 70 2c 20 66 6c 61 67 73 29 20 26 20 30 78  e(p, flags) & 0x
20d7c 66 66 66 3b 0a 20 20 69 66 28 20 21 45 78 70 72  fff;.  if( !Expr
20d7d 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45  HasProperty(p, E
20d7e 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26 20 70  P_IntValue) && p
20d7f 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20  ->u.zToken ){.  
20d80 20 20 6e 42 79 74 65 20 2b 3d 20 73 71 6c 69 74    nByte += sqlit
20d81 65 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e 75 2e  e3Strlen30(p->u.
20d82 7a 54 6f 6b 65 6e 29 2b 31 3b 0a 20 20 7d 0a 20  zToken)+1;.  }. 
20d83 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e   return ROUND8(n
20d84 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Byte);.}../*.** 
20d85 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
20d86 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69  r of bytes requi
20d87 72 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  red to create a 
20d88 64 75 70 6c 69 63 61 74 65 20 6f 66 20 74 68 65  duplicate of the
20d89 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20   .** expression 
20d8a 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66 69  passed as the fi
20d8b 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54 68  rst argument. Th
20d8c 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
20d8d 74 20 69 73 20 61 0a 2a 2a 20 6d 61 73 6b 20 63  t is a.** mask c
20d8e 6f 6e 74 61 69 6e 69 6e 67 20 45 58 50 52 44 55  ontaining EXPRDU
20d8f 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a 2a 2a 0a  P_XXX flags..**.
20d90 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  ** The value ret
20d91 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 73 20 73  urned includes s
20d92 70 61 63 65 20 74 6f 20 63 72 65 61 74 65 20 61  pace to create a
20d93 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45 78 70   copy of the Exp
20d94 72 20 73 74 72 75 63 74 0a 2a 2a 20 69 74 73 65  r struct.** itse
20d95 6c 66 20 61 6e 64 20 74 68 65 20 62 75 66 66 65  lf and the buffe
20d96 72 20 72 65 66 65 72 72 65 64 20 74 6f 20 62 79  r referred to by
20d97 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65 6e 2c 20   Expr.u.zToken, 
20d98 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66  if any..**.** If
20d99 20 74 68 65 20 45 58 50 52 44 55 50 5f 52 45 44   the EXPRDUP_RED
20d9a 55 43 45 20 66 6c 61 67 20 69 73 20 73 65 74 2c  UCE flag is set,
20d9b 20 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e   then the return
20d9c 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65 73 20   value includes 
20d9d 0a 2a 2a 20 73 70 61 63 65 20 74 6f 20 64 75 70  .** space to dup
20d9e 6c 69 63 61 74 65 20 61 6c 6c 20 45 78 70 72 20  licate all Expr 
20d9f 6e 6f 64 65 73 20 69 6e 20 74 68 65 20 74 72 65  nodes in the tre
20da0 65 20 66 6f 72 6d 65 64 20 62 79 20 45 78 70 72  e formed by Expr
20da1 2e 70 4c 65 66 74 20 0a 2a 2a 20 61 6e 64 20 45  .pLeft .** and E
20da2 78 70 72 2e 70 52 69 67 68 74 20 76 61 72 69 61  xpr.pRight varia
20da3 62 6c 65 73 20 28 62 75 74 20 6e 6f 74 20 66 6f  bles (but not fo
20da4 72 20 61 6e 79 20 73 74 72 75 63 74 75 72 65 73  r any structures
20da5 20 70 6f 69 6e 74 65 64 20 74 6f 20 6f 72 20 0a   pointed to or .
20da6 2a 2a 20 64 65 73 63 65 6e 64 65 64 20 66 72 6f  ** descended fro
20da7 6d 20 74 68 65 20 45 78 70 72 2e 78 2e 70 4c 69  m the Expr.x.pLi
20da8 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70 53 65  st or Expr.x.pSe
20da9 6c 65 63 74 20 76 61 72 69 61 62 6c 65 73 29 2e  lect variables).
20daa 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
20dab 75 70 65 64 45 78 70 72 53 69 7a 65 28 45 78 70  upedExprSize(Exp
20dac 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29  r *p, int flags)
20dad 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20  {.  int nByte = 
20dae 30 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  0;.  if( p ){.  
20daf 20 20 6e 42 79 74 65 20 3d 20 64 75 70 65 64 45    nByte = dupedE
20db0 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c 20 66  xprNodeSize(p, f
20db1 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 66  lags);.    if( f
20db2 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52 45 44  lags&EXPRDUP_RED
20db3 55 43 45 20 29 7b 0a 20 20 20 20 20 20 6e 42 79  UCE ){.      nBy
20db4 74 65 20 2b 3d 20 64 75 70 65 64 45 78 70 72 53  te += dupedExprS
20db5 69 7a 65 28 70 2d 3e 70 4c 65 66 74 2c 20 66 6c  ize(p->pLeft, fl
20db6 61 67 73 29 20 2b 20 64 75 70 65 64 45 78 70 72  ags) + dupedExpr
20db7 53 69 7a 65 28 70 2d 3e 70 52 69 67 68 74 2c 20  Size(p->pRight, 
20db8 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20  flags);.    }.  
20db9 7d 0a 20 20 72 65 74 75 72 6e 20 6e 42 79 74 65  }.  return nByte
20dba 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
20dbb 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 69 6d 69  function is simi
20dbc 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33 45 78  lar to sqlite3Ex
20dbd 70 72 44 75 70 28 29 2c 20 65 78 63 65 70 74 20  prDup(), except 
20dbe 74 68 61 74 20 69 66 20 70 7a 42 75 66 66 65 72  that if pzBuffer
20dbf 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c   .** is not NULL
20dc0 20 74 68 65 6e 20 2a 70 7a 42 75 66 66 65 72 20   then *pzBuffer 
20dc1 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 70 6f  is assumed to po
20dc2 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20  int to a buffer 
20dc3 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 0a 2a 2a  large enough .**
20dc4 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f   to store the co
20dc5 70 79 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  py of expression
20dc6 20 70 2c 20 74 68 65 20 63 6f 70 69 65 73 20 6f   p, the copies o
20dc7 66 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 0a 2a 2a  f p->u.zToken.**
20dc8 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29   (if applicable)
20dc9 2c 20 61 6e 64 20 74 68 65 20 63 6f 70 69 65 73  , and the copies
20dca 20 6f 66 20 74 68 65 20 70 2d 3e 70 4c 65 66 74   of the p->pLeft
20dcb 20 61 6e 64 20 70 2d 3e 70 52 69 67 68 74 20 65   and p->pRight e
20dcc 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a 2a 20 69  xpressions,.** i
20dcd 66 20 61 6e 79 2e 20 42 65 66 6f 72 65 20 72 65  f any. Before re
20dce 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 42 75 66 66  turning, *pzBuff
20dcf 65 72 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  er is set to the
20dd0 20 66 69 72 73 74 20 62 79 74 65 20 70 61 73 73   first byte pass
20dd1 65 64 20 74 68 65 0a 2a 2a 20 70 6f 72 74 69 6f  ed the.** portio
20dd2 6e 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20  n of the buffer 
20dd3 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 79 20 74  copied into by t
20dd4 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  his function..*/
20dd5 0a 73 74 61 74 69 63 20 45 78 70 72 20 2a 65 78  .static Expr *ex
20dd6 70 72 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  prDup(sqlite3 *d
20dd7 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  b, Expr *p, int 
20dd8 66 6c 61 67 73 2c 20 75 38 20 2a 2a 70 7a 42 75  flags, u8 **pzBu
20dd9 66 66 65 72 29 7b 0a 20 20 45 78 70 72 20 2a 70  ffer){.  Expr *p
20dda 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20 20 20  New = 0;        
20ddb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20ddc 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e   Value to return
20ddd 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b 0a 20   */.  if( p ){. 
20dde 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 52     const int isR
20ddf 65 64 75 63 65 64 20 3d 20 28 66 6c 61 67 73 26  educed = (flags&
20de0 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b  EXPRDUP_REDUCE);
20de1 0a 20 20 20 20 75 38 20 2a 7a 41 6c 6c 6f 63 3b  .    u8 *zAlloc;
20de2 0a 20 20 20 20 75 33 32 20 73 74 61 74 69 63 46  .    u32 staticF
20de3 6c 61 67 20 3d 20 30 3b 0a 0a 20 20 20 20 61 73  lag = 0;..    as
20de4 73 65 72 74 28 20 70 7a 42 75 66 66 65 72 3d 3d  sert( pzBuffer==
20de5 30 20 7c 7c 20 69 73 52 65 64 75 63 65 64 20 29  0 || isReduced )
20de6 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65  ;..    /* Figure
20de7 20 6f 75 74 20 77 68 65 72 65 20 74 6f 20 77 72   out where to wr
20de8 69 74 65 20 74 68 65 20 6e 65 77 20 45 78 70 72  ite the new Expr
20de9 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20   structure. */. 
20dea 20 20 20 69 66 28 20 70 7a 42 75 66 66 65 72 20     if( pzBuffer 
20deb 29 7b 0a 20 20 20 20 20 20 7a 41 6c 6c 6f 63 20  ){.      zAlloc 
20dec 3d 20 2a 70 7a 42 75 66 66 65 72 3b 0a 20 20 20  = *pzBuffer;.   
20ded 20 20 20 73 74 61 74 69 63 46 6c 61 67 20 3d 20     staticFlag = 
20dee 45 50 5f 53 74 61 74 69 63 3b 0a 20 20 20 20 7d  EP_Static;.    }
20def 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 41 6c 6c  else{.      zAll
20df0 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  oc = sqlite3DbMa
20df1 6c 6c 6f 63 52 61 77 28 64 62 2c 20 64 75 70 65  llocRaw(db, dupe
20df2 64 45 78 70 72 53 69 7a 65 28 70 2c 20 66 6c 61  dExprSize(p, fla
20df3 67 73 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  gs));.    }.    
20df4 70 4e 65 77 20 3d 20 28 45 78 70 72 20 2a 29 7a  pNew = (Expr *)z
20df5 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 69 66 28 20  Alloc;..    if( 
20df6 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 2f 2a  pNew ){.      /*
20df7 20 53 65 74 20 6e 4e 65 77 53 69 7a 65 20 74 6f   Set nNewSize to
20df8 20 74 68 65 20 73 69 7a 65 20 61 6c 6c 6f 63 61   the size alloca
20df9 74 65 64 20 66 6f 72 20 74 68 65 20 73 74 72 75  ted for the stru
20dfa 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f  cture pointed to
20dfb 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 70 4e 65  .      ** by pNe
20dfc 77 2e 20 54 68 69 73 20 69 73 20 65 69 74 68 65  w. This is eithe
20dfd 72 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a 45 2c  r EXPR_FULLSIZE,
20dfe 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a   EXPR_REDUCEDSIZ
20dff 45 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 45 58  E or.      ** EX
20e00 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45  PR_TOKENONLYSIZE
20e01 2e 20 6e 54 6f 6b 65 6e 20 69 73 20 73 65 74 20  . nToken is set 
20e02 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
20e03 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 0a   bytes consumed.
20e04 20 20 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20        ** by the 
20e05 63 6f 70 79 20 6f 66 20 74 68 65 20 70 2d 3e 75  copy of the p->u
20e06 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28  .zToken string (
20e07 69 66 20 61 6e 79 29 2e 0a 20 20 20 20 20 20 2a  if any)..      *
20e08 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e  /.      const un
20e09 73 69 67 6e 65 64 20 6e 53 74 72 75 63 74 53 69  signed nStructSi
20e0a 7a 65 20 3d 20 64 75 70 65 64 45 78 70 72 53 74  ze = dupedExprSt
20e0b 72 75 63 74 53 69 7a 65 28 70 2c 20 66 6c 61 67  ructSize(p, flag
20e0c 73 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  s);.      const 
20e0d 69 6e 74 20 6e 4e 65 77 53 69 7a 65 20 3d 20 6e  int nNewSize = n
20e0e 53 74 72 75 63 74 53 69 7a 65 20 26 20 30 78 66  StructSize & 0xf
20e0f 66 66 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 54  ff;.      int nT
20e10 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 69 66 28 20  oken;.      if( 
20e11 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
20e12 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  (p, EP_IntValue)
20e13 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20   && p->u.zToken 
20e14 29 7b 0a 20 20 20 20 20 20 20 20 6e 54 6f 6b 65  ){.        nToke
20e15 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
20e16 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 29  n30(p->u.zToken)
20e17 20 2b 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73   + 1;.      }els
20e18 65 7b 0a 20 20 20 20 20 20 20 20 6e 54 6f 6b 65  e{.        nToke
20e19 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  n = 0;.      }. 
20e1a 20 20 20 20 20 69 66 28 20 69 73 52 65 64 75 63       if( isReduc
20e1b 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  ed ){.        as
20e1c 73 65 72 74 28 20 45 78 70 72 48 61 73 50 72 6f  sert( ExprHasPro
20e1d 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65 64 75  perty(p, EP_Redu
20e1e 63 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ced)==0 );.     
20e1f 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c 6f 63     memcpy(zAlloc
20e20 2c 20 70 2c 20 6e 4e 65 77 53 69 7a 65 29 3b 0a  , p, nNewSize);.
20e21 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
20e22 20 20 20 20 20 69 6e 74 20 6e 53 69 7a 65 20 3d       int nSize =
20e23 20 65 78 70 72 53 74 72 75 63 74 53 69 7a 65 28   exprStructSize(
20e24 70 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  p);.        memc
20e25 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20 6e 53  py(zAlloc, p, nS
20e26 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 6d 65  ize);.        me
20e27 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b 6e 53 69  mset(&zAlloc[nSi
20e28 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f 46 55 4c  ze], 0, EXPR_FUL
20e29 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b 0a 20 20  LSIZE-nSize);.  
20e2a 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
20e2b 53 65 74 20 74 68 65 20 45 50 5f 52 65 64 75 63  Set the EP_Reduc
20e2c 65 64 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  ed, EP_TokenOnly
20e2d 2c 20 61 6e 64 20 45 50 5f 53 74 61 74 69 63 20  , and EP_Static 
20e2e 66 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61 74  flags appropriat
20e2f 65 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20 70 4e  ely. */.      pN
20e30 65 77 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 45  ew->flags &= ~(E
20e31 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b  P_Reduced|EP_Tok
20e32 65 6e 4f 6e 6c 79 7c 45 50 5f 53 74 61 74 69 63  enOnly|EP_Static
20e33 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66  );.      pNew->f
20e34 6c 61 67 73 20 7c 3d 20 6e 53 74 72 75 63 74 53  lags |= nStructS
20e35 69 7a 65 20 26 20 28 45 50 5f 52 65 64 75 63 65  ize & (EP_Reduce
20e36 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 3b  d|EP_TokenOnly);
20e37 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61  .      pNew->fla
20e38 67 73 20 7c 3d 20 73 74 61 74 69 63 46 6c 61 67  gs |= staticFlag
20e39 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 70 79  ;..      /* Copy
20e3a 20 74 68 65 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e   the p->u.zToken
20e3b 20 73 74 72 69 6e 67 2c 20 69 66 20 61 6e 79 2e   string, if any.
20e3c 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 54   */.      if( nT
20e3d 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20  oken ){.        
20e3e 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70  char *zToken = p
20e3f 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20  New->u.zToken = 
20e40 28 63 68 61 72 2a 29 26 7a 41 6c 6c 6f 63 5b 6e  (char*)&zAlloc[n
20e41 4e 65 77 53 69 7a 65 5d 3b 0a 20 20 20 20 20 20  NewSize];.      
20e42 20 20 6d 65 6d 63 70 79 28 7a 54 6f 6b 65 6e 2c    memcpy(zToken,
20e43 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 6e 54   p->u.zToken, nT
20e44 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  oken);.      }..
20e45 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 28 70        if( 0==((p
20e46 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d 3e 66 6c  ->flags|pNew->fl
20e47 61 67 73 29 20 26 20 45 50 5f 54 6f 6b 65 6e 4f  ags) & EP_TokenO
20e48 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20  nly) ){.        
20e49 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70  /* Fill in the p
20e4a 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74 20 6f  New->x.pSelect o
20e4b 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20  r pNew->x.pList 
20e4c 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 20 20 20 20  member. */.     
20e4d 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
20e4e 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73  operty(p, EP_xIs
20e4f 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20  Select) ){.     
20e50 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 53 65       pNew->x.pSe
20e51 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65  lect = sqlite3Se
20e52 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 78  lectDup(db, p->x
20e53 2e 70 53 65 6c 65 63 74 2c 20 69 73 52 65 64 75  .pSelect, isRedu
20e54 63 65 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  ced);.        }e
20e55 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70  lse{.          p
20e56 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 73  New->x.pList = s
20e57 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75  qlite3ExprListDu
20e58 70 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 74  p(db, p->x.pList
20e59 2c 20 69 73 52 65 64 75 63 65 64 29 3b 0a 20 20  , isReduced);.  
20e5a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
20e5b 0a 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69  .      /* Fill i
20e5c 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 61 6e  n pNew->pLeft an
20e5d 64 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 2e 20  d pNew->pRight. 
20e5e 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 45 78 70  */.      if( Exp
20e5f 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
20e60 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 65 64  pNew, EP_Reduced
20e61 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29  |EP_TokenOnly) )
20e62 7b 0a 20 20 20 20 20 20 20 20 7a 41 6c 6c 6f 63  {.        zAlloc
20e63 20 2b 3d 20 64 75 70 65 64 45 78 70 72 4e 6f 64   += dupedExprNod
20e64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 3b  eSize(p, flags);
20e65 0a 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70  .        if( Exp
20e66 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65  rHasProperty(pNe
20e67 77 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20 29  w, EP_Reduced) )
20e68 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77  {.          pNew
20e69 2d 3e 70 4c 65 66 74 20 3d 20 65 78 70 72 44 75  ->pLeft = exprDu
20e6a 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20  p(db, p->pLeft, 
20e6b 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20  EXPRDUP_REDUCE, 
20e6c 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20 20 20  &zAlloc);.      
20e6d 20 20 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74      pNew->pRight
20e6e 20 3d 20 65 78 70 72 44 75 70 28 64 62 2c 20 70   = exprDup(db, p
20e6f 2d 3e 70 52 69 67 68 74 2c 20 45 58 50 52 44 55  ->pRight, EXPRDU
20e70 50 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c 6c 6f  P_REDUCE, &zAllo
20e71 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  c);.        }.  
20e72 20 20 20 20 20 20 69 66 28 20 70 7a 42 75 66 66        if( pzBuff
20e73 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  er ){.          
20e74 2a 70 7a 42 75 66 66 65 72 20 3d 20 7a 41 6c 6c  *pzBuffer = zAll
20e75 6f 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  oc;.        }.  
20e76 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20e77 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 32 20     pNew->flags2 
20e78 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  = 0;.        if(
20e79 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70   !ExprHasAnyProp
20e7a 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65 6e  erty(p, EP_Token
20e7b 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20 20  Only) ){.       
20e7c 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d     pNew->pLeft =
20e7d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
20e7e 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20 30 29  db, p->pLeft, 0)
20e7f 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65 77  ;.          pNew
20e80 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74  ->pRight = sqlit
20e81 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d  e3ExprDup(db, p-
20e82 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20  >pRight, 0);.   
20e83 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
20e84 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
20e85 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
20e86 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
20e87 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e 65  group of routine
20e88 73 20 6d 61 6b 65 20 64 65 65 70 20 63 6f 70 69  s make deep copi
20e89 65 73 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e  es of expression
20e8a 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  s,.** expression
20e8b 20 6c 69 73 74 73 2c 20 49 44 20 6c 69 73 74 73   lists, ID lists
20e8c 2c 20 61 6e 64 20 73 65 6c 65 63 74 20 73 74 61  , and select sta
20e8d 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20 63 6f  tements.  The co
20e8e 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 64  pies can.** be d
20e8f 65 6c 65 74 65 64 20 28 62 79 20 62 65 69 6e 67  eleted (by being
20e90 20 70 61 73 73 65 64 20 74 6f 20 74 68 65 69 72   passed to their
20e91 20 72 65 73 70 65 63 74 69 76 65 20 2e 2e 2e 44   respective ...D
20e92 65 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65 73  elete() routines
20e93 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20 65 66 66  ).** without eff
20e94 65 63 74 69 6e 67 20 74 68 65 20 6f 72 69 67 69  ecting the origi
20e95 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nals..**.** The 
20e96 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c  expression list,
20e97 20 49 44 2c 20 61 6e 64 20 73 6f 75 72 63 65 20   ID, and source 
20e98 6c 69 73 74 73 20 72 65 74 75 72 6e 20 62 79 20  lists return by 
20e99 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
20e9a 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  up(),.** sqlite3
20e9b 49 64 4c 69 73 74 44 75 70 28 29 2c 20 61 6e 64  IdListDup(), and
20e9c 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
20e9d 75 70 28 29 20 63 61 6e 20 6e 6f 74 20 62 65 20  up() can not be 
20e9e 66 75 72 74 68 65 72 20 65 78 70 61 6e 64 65 64  further expanded
20e9f 20 0a 2a 2a 20 62 79 20 73 75 62 73 65 71 75 65   .** by subseque
20ea0 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  nt calls to sqli
20ea1 74 65 2a 4c 69 73 74 41 70 70 65 6e 64 28 29 20  te*ListAppend() 
20ea2 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20  routines..**.** 
20ea3 41 6e 79 20 74 61 62 6c 65 73 20 74 68 61 74 20  Any tables that 
20ea4 74 68 65 20 53 72 63 4c 69 73 74 20 6d 69 67 68  the SrcList migh
20ea5 74 20 70 6f 69 6e 74 20 74 6f 20 61 72 65 20 6e  t point to are n
20ea6 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e 0a 2a  ot duplicated..*
20ea7 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 70  *.** The flags p
20ea8 61 72 61 6d 65 74 65 72 20 63 6f 6e 74 61 69 6e  arameter contain
20ea9 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  s a combination 
20eaa 6f 66 20 74 68 65 20 45 58 50 52 44 55 50 5f 58  of the EXPRDUP_X
20eab 58 58 20 66 6c 61 67 73 2e 0a 2a 2a 20 49 66 20  XX flags..** If 
20eac 74 68 65 20 45 58 50 52 44 55 50 5f 52 45 44 55  the EXPRDUP_REDU
20ead 43 45 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  CE flag is set, 
20eae 74 68 65 6e 20 74 68 65 20 73 74 72 75 63 74 75  then the structu
20eaf 72 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61  re returned is a
20eb0 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 76 65  .** truncated ve
20eb1 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 75 73 75  rsion of the usu
20eb2 61 6c 20 45 78 70 72 20 73 74 72 75 63 74 75 72  al Expr structur
20eb3 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 73  e that will be s
20eb4 74 6f 72 65 64 20 61 73 0a 2a 2a 20 70 61 72 74  tored as.** part
20eb5 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   of the in-memor
20eb6 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  y representation
20eb7 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
20eb8 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a 53 51 4c 49   schema..*/.SQLI
20eb9 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
20eba 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28  *sqlite3ExprDup(
20ebb 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
20ebc 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29  r *p, int flags)
20ebd 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 44  {.  return exprD
20ebe 75 70 28 64 62 2c 20 70 2c 20 66 6c 61 67 73 2c  up(db, p, flags,
20ebf 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52   0);.}.SQLITE_PR
20ec0 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a  IVATE ExprList *
20ec1 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
20ec2 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
20ec3 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74  ExprList *p, int
20ec4 20 66 6c 61 67 73 29 7b 0a 20 20 45 78 70 72 4c   flags){.  ExprL
20ec5 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 73 74 72  ist *pNew;.  str
20ec6 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
20ec7 6d 20 2a 70 49 74 65 6d 2c 20 2a 70 4f 6c 64 49  m *pItem, *pOldI
20ec8 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  tem;.  int i;.  
20ec9 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
20eca 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71  n 0;.  pNew = sq
20ecb 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
20ecc 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65  (db, sizeof(*pNe
20ecd 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  w) );.  if( pNew
20ece 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
20ecf 20 20 70 4e 65 77 2d 3e 69 45 43 75 72 73 6f 72    pNew->iECursor
20ed0 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 45   = 0;.  pNew->nE
20ed1 78 70 72 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c  xpr = pNew->nAll
20ed2 6f 63 20 3d 20 70 2d 3e 6e 45 78 70 72 3b 0a 20  oc = p->nExpr;. 
20ed3 20 70 4e 65 77 2d 3e 61 20 3d 20 70 49 74 65 6d   pNew->a = pItem
20ed4 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
20ed5 6f 63 52 61 77 28 64 62 2c 20 20 70 2d 3e 6e 45  ocRaw(db,  p->nE
20ed6 78 70 72 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b  xpr*sizeof(p->a[
20ed7 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 49 74  0]) );.  if( pIt
20ed8 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  em==0 ){.    sql
20ed9 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
20eda 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  New);.    return
20edb 20 30 3b 0a 20 20 7d 20 0a 20 20 70 4f 6c 64 49   0;.  } .  pOldI
20edc 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20 20 66 6f  tem = p->a;.  fo
20edd 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45 78 70  r(i=0; i<p->nExp
20ede 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 2c  r; i++, pItem++,
20edf 20 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b 0a 20 20   pOldItem++){.  
20ee0 20 20 45 78 70 72 20 2a 70 4f 6c 64 45 78 70 72    Expr *pOldExpr
20ee1 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 45 78   = pOldItem->pEx
20ee2 70 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70  pr;.    pItem->p
20ee3 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78  Expr = sqlite3Ex
20ee4 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 45 78  prDup(db, pOldEx
20ee5 70 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20  pr, flags);.    
20ee6 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73  pItem->zName = s
20ee7 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
20ee8 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61  b, pOldItem->zNa
20ee9 6d 65 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  me);.    pItem->
20eea 7a 53 70 61 6e 20 3d 20 73 71 6c 69 74 65 33 44  zSpan = sqlite3D
20eeb 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  bStrDup(db, pOld
20eec 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20  Item->zSpan);.  
20eed 20 20 70 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64    pItem->sortOrd
20eee 65 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 73  er = pOldItem->s
20eef 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 70 49  ortOrder;.    pI
20ef0 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20  tem->done = 0;. 
20ef1 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d     pItem->iCol =
20ef2 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 6f 6c 3b   pOldItem->iCol;
20ef3 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69  .    pItem->iAli
20ef4 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69  as = pOldItem->i
20ef5 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 72 65 74  Alias;.  }.  ret
20ef6 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
20ef7 2a 2a 20 49 66 20 63 75 72 73 6f 72 73 2c 20 74  ** If cursors, t
20ef8 72 69 67 67 65 72 73 2c 20 76 69 65 77 73 20 61  riggers, views a
20ef9 6e 64 20 73 75 62 71 75 65 72 69 65 73 20 61 72  nd subqueries ar
20efa 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64 20 66 72  e all omitted fr
20efb 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2c  om.** the build,
20efc 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68   then none of th
20efd 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
20efe 69 6e 65 73 2c 20 65 78 63 65 70 74 20 66 6f 72  ines, except for
20eff 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65   .** sqlite3Sele
20f00 63 74 44 75 70 28 29 2c 20 63 61 6e 20 62 65 20  ctDup(), can be 
20f01 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74 65 33 53  called. sqlite3S
20f02 65 6c 65 63 74 44 75 70 28 29 20 69 73 20 73 6f  electDup() is so
20f03 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61 6c 6c 65  metimes.** calle
20f04 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 61 72  d with a NULL ar
20f05 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69 66 20 21  gument..*/.#if !
20f06 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
20f07 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
20f08 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
20f09 54 5f 54 52 49 47 47 45 52 29 20 5c 0a 20 7c 7c  T_TRIGGER) \. ||
20f0a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
20f0b 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a  _OMIT_SUBQUERY).
20f0c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
20f0d 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
20f0e 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65  rcListDup(sqlite
20f0f 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a  3 *db, SrcList *
20f10 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  p, int flags){. 
20f11 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77 3b 0a   SrcList *pNew;.
20f12 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
20f13 42 79 74 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30  Byte;.  if( p==0
20f14 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e   ) return 0;.  n
20f15 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70  Byte = sizeof(*p
20f16 29 20 2b 20 28 70 2d 3e 6e 53 72 63 3e 30 20 3f  ) + (p->nSrc>0 ?
20f17 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d 29   sizeof(p->a[0])
20f18 20 2a 20 28 70 2d 3e 6e 53 72 63 2d 31 29 20 3a   * (p->nSrc-1) :
20f19 20 30 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71   0);.  pNew = sq
20f1a 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
20f1b 28 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20  (db, nByte );.  
20f1c 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65  if( pNew==0 ) re
20f1d 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  turn 0;.  pNew->
20f1e 6e 53 72 63 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c  nSrc = pNew->nAl
20f1f 6c 6f 63 20 3d 20 70 2d 3e 6e 53 72 63 3b 0a 20  loc = p->nSrc;. 
20f20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
20f21 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  Src; i++){.    s
20f22 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
20f23 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26  em *pNewItem = &
20f24 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  pNew->a[i];.    
20f25 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
20f26 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20  tem *pOldItem = 
20f27 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 54 61  &p->a[i];.    Ta
20f28 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 70  ble *pTab;.    p
20f29 4e 65 77 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  NewItem->zDataba
20f2a 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  se = sqlite3DbSt
20f2b 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  rDup(db, pOldIte
20f2c 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  m->zDatabase);. 
20f2d 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e 61     pNewItem->zNa
20f2e 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
20f2f 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65  rDup(db, pOldIte
20f30 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 70  m->zName);.    p
20f31 4e 65 77 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20  NewItem->zAlias 
20f32 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
20f33 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
20f34 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 70 4e 65  zAlias);.    pNe
20f35 77 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20  wItem->jointype 
20f36 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6a 6f 69 6e  = pOldItem->join
20f37 74 79 70 65 3b 0a 20 20 20 20 70 4e 65 77 49 74  type;.    pNewIt
20f38 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70 4f  em->iCursor = pO
20f39 6c 64 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b  ldItem->iCursor;
20f3a 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69  .    pNewItem->i
20f3b 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 70 4f 6c  sPopulated = pOl
20f3c 64 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74  dItem->isPopulat
20f3d 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  ed;.    pNewItem
20f3e 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 6c 69 74  ->zIndex = sqlit
20f3f 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
20f40 4f 6c 64 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 29  OldItem->zIndex)
20f41 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
20f42 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 70 4f 6c  notIndexed = pOl
20f43 64 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65  dItem->notIndexe
20f44 64 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  d;.    pNewItem-
20f45 3e 70 49 6e 64 65 78 20 3d 20 70 4f 6c 64 49 74  >pIndex = pOldIt
20f46 65 6d 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20  em->pIndex;.    
20f47 70 54 61 62 20 3d 20 70 4e 65 77 49 74 65 6d 2d  pTab = pNewItem-
20f48 3e 70 54 61 62 20 3d 20 70 4f 6c 64 49 74 65 6d  >pTab = pOldItem
20f49 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20  ->pTab;.    if( 
20f4a 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 70 54  pTab ){.      pT
20f4b 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  ab->nRef++;.    
20f4c 7d 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  }.    pNewItem->
20f4d 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
20f4e 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
20f4f 4f 6c 64 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74  OldItem->pSelect
20f50 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 4e  , flags);.    pN
20f51 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71  ewItem->pOn = sq
20f52 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
20f53 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 4f 6e 2c 20   pOldItem->pOn, 
20f54 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 4e 65 77  flags);.    pNew
20f55 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d 20 73  Item->pUsing = s
20f56 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28  qlite3IdListDup(
20f57 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 55  db, pOldItem->pU
20f58 73 69 6e 67 29 3b 0a 20 20 20 20 70 4e 65 77 49  sing);.    pNewI
20f59 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 20 3d 20 70  tem->colUsed = p
20f5a 4f 6c 64 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64  OldItem->colUsed
20f5b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
20f5c 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  New;.}.SQLITE_PR
20f5d 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71  IVATE IdList *sq
20f5e 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 73  lite3IdListDup(s
20f5f 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69  qlite3 *db, IdLi
20f60 73 74 20 2a 70 29 7b 0a 20 20 49 64 4c 69 73 74  st *p){.  IdList
20f61 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b   *pNew;.  int i;
20f62 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
20f63 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d  turn 0;.  pNew =
20f64 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
20f65 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  Raw(db, sizeof(*
20f66 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 20 70  pNew) );.  if( p
20f67 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  New==0 ) return 
20f68 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 49 64 20 3d  0;.  pNew->nId =
20f69 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20   pNew->nAlloc = 
20f6a 70 2d 3e 6e 49 64 3b 0a 20 20 70 4e 65 77 2d 3e  p->nId;.  pNew->
20f6b 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  a = sqlite3DbMal
20f6c 6c 6f 63 52 61 77 28 64 62 2c 20 70 2d 3e 6e 49  locRaw(db, p->nI
20f6d 64 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30 5d  d*sizeof(p->a[0]
20f6e 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d  ) );.  if( pNew-
20f6f 3e 61 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  >a==0 ){.    sql
20f70 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
20f71 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  New);.    return
20f72 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d   0;.  }.  for(i=
20f73 30 3b 20 69 3c 70 2d 3e 6e 49 64 3b 20 69 2b 2b  0; i<p->nId; i++
20f74 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 49 64  ){.    struct Id
20f75 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49  List_item *pNewI
20f76 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69  tem = &pNew->a[i
20f77 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20 49 64  ];.    struct Id
20f78 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49  List_item *pOldI
20f79 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a  tem = &p->a[i];.
20f7a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 4e      pNewItem->zN
20f7b 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
20f7c 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74  trDup(db, pOldIt
20f7d 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  em->zName);.    
20f7e 70 4e 65 77 49 74 65 6d 2d 3e 69 64 78 20 3d 20  pNewItem->idx = 
20f7f 70 4f 6c 64 49 74 65 6d 2d 3e 69 64 78 3b 0a 20  pOldItem->idx;. 
20f80 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
20f81 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
20f82 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74  TE Select *sqlit
20f83 65 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69  e3SelectDup(sqli
20f84 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20  te3 *db, Select 
20f85 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  *p, int flags){.
20f86 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 3b 0a    Select *pNew;.
20f87 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
20f88 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20  urn 0;.  pNew = 
20f89 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
20f8a 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  aw(db, sizeof(*p
20f8b 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d  ) );.  if( pNew=
20f8c 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
20f8d 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20   pNew->pEList = 
20f8e 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
20f8f 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74  up(db, p->pEList
20f90 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77  , flags);.  pNew
20f91 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33  ->pSrc = sqlite3
20f92 53 72 63 4c 69 73 74 44 75 70 28 64 62 2c 20 70  SrcListDup(db, p
20f93 2d 3e 70 53 72 63 2c 20 66 6c 61 67 73 29 3b 0a  ->pSrc, flags);.
20f94 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d    pNew->pWhere =
20f95 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
20f96 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 66  db, p->pWhere, f
20f97 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70  lags);.  pNew->p
20f98 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74 65  GroupBy = sqlite
20f99 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
20f9a 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20 66 6c   p->pGroupBy, fl
20f9b 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 48  ags);.  pNew->pH
20f9c 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 45  aving = sqlite3E
20f9d 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 48  xprDup(db, p->pH
20f9e 61 76 69 6e 67 2c 20 66 6c 61 67 73 29 3b 0a 20  aving, flags);. 
20f9f 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 79 20   pNew->pOrderBy 
20fa0 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
20fa1 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 72 64  tDup(db, p->pOrd
20fa2 65 72 42 79 2c 20 66 6c 61 67 73 29 3b 0a 20 20  erBy, flags);.  
20fa3 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d 3e 6f 70  pNew->op = p->op
20fa4 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72 69 6f 72  ;.  pNew->pPrior
20fa5 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
20fa6 44 75 70 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f  Dup(db, p->pPrio
20fa7 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65  r, flags);.  pNe
20fa8 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69  w->pLimit = sqli
20fa9 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
20faa 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c 61 67 73 29  ->pLimit, flags)
20fab 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65  ;.  pNew->pOffse
20fac 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  t = sqlite3ExprD
20fad 75 70 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65  up(db, p->pOffse
20fae 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65  t, flags);.  pNe
20faf 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  w->iLimit = 0;. 
20fb0 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d   pNew->iOffset =
20fb1 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46   0;.  pNew->selF
20fb2 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c 46 6c 61  lags = p->selFla
20fb3 67 73 20 26 20 7e 53 46 5f 55 73 65 73 45 70 68  gs & ~SF_UsesEph
20fb4 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65 77 2d 3e  emeral;.  pNew->
20fb5 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a  pRightmost = 0;.
20fb6 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e    pNew->addrOpen
20fb7 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20  Ephm[0] = -1;.  
20fb8 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70  pNew->addrOpenEp
20fb9 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e  hm[1] = -1;.  pN
20fba 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d  ew->addrOpenEphm
20fbb 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75  [2] = -1;.  retu
20fbc 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65  rn pNew;.}.#else
20fbd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20fbe 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53  Select *sqlite3S
20fbf 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33  electDup(sqlite3
20fc0 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 2c   *db, Select *p,
20fc1 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 61   int flags){.  a
20fc2 73 73 65 72 74 28 20 70 3d 3d 30 20 29 3b 0a 20  ssert( p==0 );. 
20fc3 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
20fc4 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  dif.../*.** Add 
20fc5 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f  a new element to
20fc6 20 74 68 65 20 65 6e 64 20 6f 66 20 61 6e 20 65   the end of an e
20fc7 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20  xpression list. 
20fc8 20 49 66 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20   If pList is.** 
20fc9 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c 4c 2c 20  initially NULL, 
20fca 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65  then create a ne
20fcb 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  w expression lis
20fcc 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65  t..**.** If a me
20fcd 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
20fce 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68  error occurs, th
20fcf 65 20 65 6e 74 69 72 65 20 6c 69 73 74 20 69 73  e entire list is
20fd0 20 66 72 65 65 64 20 61 6e 64 0a 2a 2a 20 4e 55   freed and.** NU
20fd1 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  LL is returned. 
20fd2 20 49 66 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 73 20   If non-NULL is 
20fd3 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 69  returned, then i
20fd4 74 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a  t is guaranteed.
20fd5 2a 2a 20 74 68 61 74 20 74 68 65 20 6e 65 77 20  ** that the new 
20fd6 65 6e 74 72 79 20 77 61 73 20 73 75 63 63 65 73  entry was succes
20fd7 73 66 75 6c 6c 79 20 61 70 70 65 6e 64 65 64 2e  sfully appended.
20fd8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20fd9 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71 6c  TE ExprList *sql
20fda 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
20fdb 6e 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  nd(.  Parse *pPa
20fdc 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
20fdd 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
20fde 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
20fdf 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 2f 2a  pList,        /*
20fe0 20 4c 69 73 74 20 74 6f 20 77 68 69 63 68 20 74   List to which t
20fe1 6f 20 61 70 70 65 6e 64 2e 20 4d 69 67 68 74 20  o append. Might 
20fe2 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45 78 70  be NULL */.  Exp
20fe3 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20  r *pExpr        
20fe4 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
20fe5 6f 6e 20 74 6f 20 62 65 20 61 70 70 65 6e 64 65  on to be appende
20fe6 64 2e 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c  d. Might be NULL
20fe7 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
20fe8 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
20fe9 62 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  b;.  if( pList==
20fea 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d  0 ){.    pList =
20feb 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
20fec 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
20fed 45 78 70 72 4c 69 73 74 29 20 29 3b 0a 20 20 20  ExprList) );.   
20fee 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b   if( pList==0 ){
20fef 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d  .      goto no_m
20ff0 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  em;.    }.    as
20ff1 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 41 6c  sert( pList->nAl
20ff2 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 7d 0a 20 20  loc==0 );.  }.  
20ff3 69 66 28 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f  if( pList->nAllo
20ff4 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20  c<=pList->nExpr 
20ff5 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78  ){.    struct Ex
20ff6 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 3b 0a  prList_item *a;.
20ff7 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73      int n = pLis
20ff8 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 34 3b  t->nAlloc*2 + 4;
20ff9 0a 20 20 20 20 61 20 3d 20 73 71 6c 69 74 65 33  .    a = sqlite3
20ffa 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 4c  DbRealloc(db, pL
20ffb 69 73 74 2d 3e 61 2c 20 6e 2a 73 69 7a 65 6f 66  ist->a, n*sizeof
20ffc 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 29 3b 0a  (pList->a[0]));.
20ffd 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29 7b 0a      if( a==0 ){.
20ffe 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65        goto no_me
20fff 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 69  m;.    }.    pLi
21000 73 74 2d 3e 61 20 3d 20 61 3b 0a 20 20 20 20 70  st->a = a;.    p
21001 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73  List->nAlloc = s
21002 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
21003 7a 65 28 64 62 2c 20 61 29 2f 73 69 7a 65 6f 66  ze(db, a)/sizeof
21004 28 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20 61 73  (a[0]);.  }.  as
21005 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d  sert( pList->a!=
21006 30 20 29 3b 0a 20 20 69 66 28 20 31 20 29 7b 0a  0 );.  if( 1 ){.
21007 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
21008 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
21009 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73  = &pList->a[pLis
2100a 74 2d 3e 6e 45 78 70 72 2b 2b 5d 3b 0a 20 20 20  t->nExpr++];.   
2100b 20 6d 65 6d 73 65 74 28 70 49 74 65 6d 2c 20 30   memset(pItem, 0
2100c 2c 20 73 69 7a 65 6f 66 28 2a 70 49 74 65 6d 29  , sizeof(*pItem)
2100d 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 45  );.    pItem->pE
2100e 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 7d  xpr = pExpr;.  }
2100f 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b  .  return pList;
21010 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20 20 0a 20  ..no_mem:     . 
21011 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61 6b 69 6e   /* Avoid leakin
21012 67 20 6d 65 6d 6f 72 79 20 69 66 20 6d 61 6c 6c  g memory if mall
21013 6f 63 20 68 61 73 20 66 61 69 6c 65 64 2e 20 2a  oc has failed. *
21014 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  /.  sqlite3ExprD
21015 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 29  elete(db, pExpr)
21016 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
21017 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c  istDelete(db, pL
21018 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  ist);.  return 0
21019 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
2101a 68 65 20 45 78 70 72 4c 69 73 74 2e 61 5b 5d 2e  he ExprList.a[].
2101b 7a 4e 61 6d 65 20 65 6c 65 6d 65 6e 74 20 6f 66  zName element of
2101c 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
2101d 6c 79 20 61 64 64 65 64 20 69 74 65 6d 0a 2a 2a  ly added item.**
2101e 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   on the expressi
2101f 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 70  on list..**.** p
21020 4c 69 73 74 20 6d 69 67 68 74 20 62 65 20 4e 55  List might be NU
21021 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 6e 20  LL following an 
21022 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75 74 20  OOM error.  But 
21023 70 4e 61 6d 65 20 73 68 6f 75 6c 64 20 6e 65 76  pName should nev
21024 65 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2e 20 20  er be.** NULL.  
21025 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  If a memory allo
21026 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74 68  cation fails, th
21027 65 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61  e pParse->db->ma
21028 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 0a  llocFailed flag.
21029 2a 2a 20 69 73 20 73 65 74 2e 0a 2a 2f 0a 53 51  ** is set..*/.SQ
2102a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2102b 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  d sqlite3ExprLis
2102c 74 53 65 74 4e 61 6d 65 28 0a 20 20 50 61 72 73  tSetName(.  Pars
2102d 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2102e 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2102f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
21030 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20  List *pList,    
21031 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77      /* List to w
21032 68 69 63 68 20 74 6f 20 61 64 64 20 74 68 65 20  hich to add the 
21033 73 70 61 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  span. */.  Token
21034 20 2a 70 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *pName,        
21035 20 20 20 2f 2a 20 4e 61 6d 65 20 74 6f 20 62 65     /* Name to be
21036 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   added */.  int 
21037 64 65 71 75 6f 74 65 20 20 20 20 20 20 20 20 20  dequote         
21038 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 63      /* True to c
21039 61 75 73 65 20 74 68 65 20 6e 61 6d 65 20 74 6f  ause the name to
2103a 20 62 65 20 64 65 71 75 6f 74 65 64 20 2a 2f 0a   be dequoted */.
2103b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69  ){.  assert( pLi
2103c 73 74 21 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d  st!=0 || pParse-
2103d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
2103e 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4c  d!=0 );.  if( pL
2103f 69 73 74 20 29 7b 0a 20 20 20 20 73 74 72 75 63  ist ){.    struc
21040 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
21041 2a 70 49 74 65 6d 3b 0a 20 20 20 20 61 73 73 65  *pItem;.    asse
21042 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  rt( pList->nExpr
21043 3e 30 20 29 3b 0a 20 20 20 20 70 49 74 65 6d 20  >0 );.    pItem 
21044 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73  = &pList->a[pLis
21045 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20  t->nExpr-1];.   
21046 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e   assert( pItem->
21047 7a 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20  zName==0 );.    
21048 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73  pItem->zName = s
21049 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
2104a 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e 61 6d  pParse->db, pNam
2104b 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e 29 3b  e->z, pName->n);
2104c 0a 20 20 20 20 69 66 28 20 64 65 71 75 6f 74 65  .    if( dequote
2104d 20 26 26 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65   && pItem->zName
2104e 20 29 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74   ) sqlite3Dequot
2104f 65 28 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b  e(pItem->zName);
21050 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  .  }.}../*.** Se
21051 74 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61  t the ExprList.a
21052 5b 5d 2e 7a 53 70 61 6e 20 65 6c 65 6d 65 6e 74  [].zSpan element
21053 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
21054 65 6e 74 6c 79 20 61 64 64 65 64 20 69 74 65 6d  ently added item
21055 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78 70 72 65  .** on the expre
21056 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a  ssion list..**.*
21057 2a 20 70 4c 69 73 74 20 6d 69 67 68 74 20 62 65  * pList might be
21058 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20   NULL following 
21059 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42  an OOM error.  B
2105a 75 74 20 70 53 70 61 6e 20 73 68 6f 75 6c 64 20  ut pSpan should 
2105b 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c  never be.** NULL
2105c 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61  .  If a memory a
2105d 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
2105e 20 74 68 65 20 70 50 61 72 73 65 2d 3e 64 62 2d   the pParse->db-
2105f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c  >mallocFailed fl
21060 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e 0a 2a 2f  ag.** is set..*/
21061 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
21062 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
21063 4c 69 73 74 53 65 74 53 70 61 6e 28 0a 20 20 50  ListSetSpan(.  P
21064 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
21065 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
21066 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
21067 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
21068 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74         /* List t
21069 6f 20 77 68 69 63 68 20 74 6f 20 61 64 64 20 74  o which to add t
2106a 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 20 45 78  he span. */.  Ex
2106b 70 72 53 70 61 6e 20 2a 70 53 70 61 6e 20 20 20  prSpan *pSpan   
2106c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 61        /* The spa
2106d 6e 20 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f  n to be added */
2106e 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
2106f 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
21070 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21    assert( pList!
21071 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  =0 || db->malloc
21072 46 61 69 6c 65 64 21 3d 30 20 29 3b 0a 20 20 69  Failed!=0 );.  i
21073 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  f( pList ){.    
21074 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
21075 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70  item *pItem = &p
21076 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e  List->a[pList->n
21077 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20 61 73 73  Expr-1];.    ass
21078 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70  ert( pList->nExp
21079 72 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  r>0 );.    asser
2107a 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
2107b 6c 65 64 20 7c 7c 20 70 49 74 65 6d 2d 3e 70 45  led || pItem->pE
2107c 78 70 72 3d 3d 70 53 70 61 6e 2d 3e 70 45 78 70  xpr==pSpan->pExp
2107d 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  r );.    sqlite3
2107e 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d  DbFree(db, pItem
2107f 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 20 20 70 49  ->zSpan);.    pI
21080 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c  tem->zSpan = sql
21081 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62  ite3DbStrNDup(db
21082 2c 20 28 63 68 61 72 2a 29 70 53 70 61 6e 2d 3e  , (char*)pSpan->
21083 7a 53 74 61 72 74 2c 0a 20 20 20 20 20 20 20 20  zStart,.        
21084 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21085 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74              (int
21086 29 28 70 53 70 61 6e 2d 3e 7a 45 6e 64 20 2d 20  )(pSpan->zEnd - 
21087 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 29 29 3b  pSpan->zStart));
21088 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  .  }.}../*.** If
21089 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2108a 6c 69 73 74 20 70 45 4c 69 73 74 20 63 6f 6e 74  list pEList cont
2108b 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20 69  ains more than i
2108c 4c 69 6d 69 74 20 65 6c 65 6d 65 6e 74 73 2c 0a  Limit elements,.
2108d 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f  ** leave an erro
2108e 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61  r message in pPa
2108f 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  rse..*/.SQLITE_P
21090 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
21091 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b  te3ExprListCheck
21092 4c 65 6e 67 74 68 28 0a 20 20 50 61 72 73 65 20  Length(.  Parse 
21093 2a 70 50 61 72 73 65 2c 0a 20 20 45 78 70 72 4c  *pParse,.  ExprL
21094 69 73 74 20 2a 70 45 4c 69 73 74 2c 0a 20 20 63  ist *pEList,.  c
21095 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62 6a 65  onst char *zObje
21096 63 74 0a 29 7b 0a 20 20 69 6e 74 20 6d 78 20 3d  ct.){.  int mx =
21097 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69   pParse->db->aLi
21098 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
21099 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65 73 74  _COLUMN];.  test
2109a 63 61 73 65 28 20 70 45 4c 69 73 74 20 26 26 20  case( pEList && 
2109b 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d  pEList->nExpr==m
2109c 78 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  x );.  testcase(
2109d 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c 69 73   pEList && pELis
2109e 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31 20 29  t->nExpr==mx+1 )
2109f 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 26  ;.  if( pEList &
210a0 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e  & pEList->nExpr>
210a1 6d 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  mx ){.    sqlite
210a2 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
210a3 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75  , "too many colu
210a4 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a 4f 62 6a  mns in %s", zObj
210a5 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ect);.  }.}../*.
210a6 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74  ** Delete an ent
210a7 69 72 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  ire expression l
210a8 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ist..*/.SQLITE_P
210a9 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
210aa 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
210ab 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45  e(sqlite3 *db, E
210ac 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  xprList *pList){
210ad 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
210ae 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
210af 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20 70   *pItem;.  if( p
210b0 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  List==0 ) return
210b1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73  ;.  assert( pLis
210b2 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 70 4c 69 73  t->a!=0 || (pLis
210b3 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 26 26 20 70  t->nExpr==0 && p
210b4 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d 30 29  List->nAlloc==0)
210b5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c   );.  assert( pL
210b6 69 73 74 2d 3e 6e 45 78 70 72 3c 3d 70 4c 69 73  ist->nExpr<=pLis
210b7 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 66  t->nAlloc );.  f
210b8 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  or(pItem=pList->
210b9 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  a, i=0; i<pList-
210ba 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  >nExpr; i++, pIt
210bb 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74  em++){.    sqlit
210bc 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
210bd 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a   pItem->pExpr);.
210be 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
210bf 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61  e(db, pItem->zNa
210c0 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  me);.    sqlite3
210c1 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d  DbFree(db, pItem
210c2 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 7d 0a 20 20  ->zSpan);.  }.  
210c3 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
210c4 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73  , pList->a);.  s
210c5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
210c6 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   pList);.}../*.*
210c7 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73  * These routines
210c8 20 61 72 65 20 57 61 6c 6b 65 72 20 63 61 6c 6c   are Walker call
210c9 62 61 63 6b 73 2e 20 20 57 61 6c 6b 65 72 2e 75  backs.  Walker.u
210ca 2e 70 69 20 69 73 20 61 20 70 6f 69 6e 74 65 72  .pi is a pointer
210cb 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e 74 65 67 65  .** to an intege
210cc 72 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e  r.  These routin
210cd 65 73 20 61 72 65 20 63 68 65 63 6b 69 6e 67 20  es are checking 
210ce 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  an expression to
210cf 20 73 65 65 0a 2a 2a 20 69 66 20 69 74 20 69 73   see.** if it is
210d0 20 61 20 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65   a constant.  Se
210d1 74 20 2a 57 61 6c 6b 65 72 2e 75 2e 70 69 20 74  t *Walker.u.pi t
210d2 6f 20 30 20 69 66 20 74 68 65 20 65 78 70 72 65  o 0 if the expre
210d3 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20  ssion is.** not 
210d4 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20  constant..**.** 
210d5 54 68 65 73 65 20 63 61 6c 6c 62 61 63 6b 20 72  These callback r
210d6 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65 64  outines are used
210d7 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68   to implement th
210d8 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
210d9 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  **     sqlite3Ex
210da 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 29 0a 2a  prIsConstant().*
210db 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  *     sqlite3Exp
210dc 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f  rIsConstantNotJo
210dd 69 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  in().**     sqli
210de 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
210df 74 4f 72 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a  tOrFunction().**
210e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65  .*/.static int e
210e1 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e  xprNodeIsConstan
210e2 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  t(Walker *pWalke
210e3 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  r, Expr *pExpr){
210e4 0a 0a 20 20 2f 2a 20 49 66 20 70 57 61 6c 6b 65  ..  /* If pWalke
210e5 72 2d 3e 75 2e 69 20 69 73 20 33 20 74 68 65 6e  r->u.i is 3 then
210e6 20 61 6e 79 20 74 65 72 6d 20 6f 66 20 74 68 65   any term of the
210e7 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
210e8 20 63 6f 6d 65 73 20 66 72 6f 6d 0a 20 20 2a 2a   comes from.  **
210e9 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47   the ON or USING
210ea 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a 6f   clauses of a jo
210eb 69 6e 20 64 69 73 71 75 61 6c 69 66 69 65 73 20  in disqualifies 
210ec 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 20  the expression. 
210ed 20 2a 2a 20 66 72 6f 6d 20 62 65 69 6e 67 20 63   ** from being c
210ee 6f 6e 73 69 64 65 72 65 64 20 63 6f 6e 73 74 61  onsidered consta
210ef 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 57 61  nt. */.  if( pWa
210f0 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33 20 26 26 20  lker->u.i==3 && 
210f1 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
210f2 74 79 28 70 45 78 70 72 2c 20 45 50 5f 46 72 6f  ty(pExpr, EP_Fro
210f3 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 70 57  mJoin) ){.    pW
210f4 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a  alker->u.i = 0;.
210f5 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
210f6 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 73 77 69  bort;.  }..  swi
210f7 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
210f8 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65  {.    /* Conside
210f9 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 62  r functions to b
210fa 65 20 63 6f 6e 73 74 61 6e 74 20 69 66 20 61 6c  e constant if al
210fb 6c 20 74 68 65 69 72 20 61 72 67 75 6d 65 6e 74  l their argument
210fc 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 0a 20  s are constant. 
210fd 20 20 20 2a 2a 20 61 6e 64 20 70 57 61 6c 6b 65     ** and pWalke
210fe 72 2d 3e 75 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20  r->u.i==2 */.   
210ff 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f   case TK_FUNCTIO
21100 4e 3a 0a 20 20 20 20 20 20 69 66 28 20 70 57 61  N:.      if( pWa
21101 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 29 20 72  lker->u.i==2 ) r
21102 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 2f  eturn 0;.      /
21103 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a  * Fall through *
21104 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 44  /.    case TK_ID
21105 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f  :.    case TK_CO
21106 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 54  LUMN:.    case T
21107 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a  K_AGG_FUNCTION:.
21108 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
21109 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 20 20 74 65  COLUMN:.      te
2110a 73 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f  stcase( pExpr->o
2110b 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a 20 20 20 20  p==TK_ID );.    
2110c 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
2110d 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  r->op==TK_COLUMN
2110e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2110f 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
21110 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29  K_AGG_FUNCTION )
21111 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21112 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
21113 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  AGG_COLUMN );.  
21114 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69      pWalker->u.i
21115 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75   = 0;.      retu
21116 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
21117 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
21118 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72   testcase( pExpr
21119 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20  ->op==TK_SELECT 
2111a 29 3b 20 2f 2a 20 73 65 6c 65 63 74 4e 6f 64 65  ); /* selectNode
2111b 49 73 43 6f 6e 73 74 61 6e 74 20 77 69 6c 6c 20  IsConstant will 
2111c 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a 20 20 20 20  disallow */.    
2111d 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
2111e 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53  r->op==TK_EXISTS
2111f 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74 4e 6f 64   ); /* selectNod
21120 65 49 73 43 6f 6e 73 74 61 6e 74 20 77 69 6c 6c  eIsConstant will
21121 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a 20 20 20   disallow */.   
21122 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f     return WRC_Co
21123 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74  ntinue;.  }.}.st
21124 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 4e  atic int selectN
21125 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57 61  odeIsConstant(Wa
21126 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53  lker *pWalker, S
21127 65 6c 65 63 74 20 2a 4e 6f 74 55 73 65 64 29 7b  elect *NotUsed){
21128 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
21129 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
2112a 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30  pWalker->u.i = 0
2112b 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ;.  return WRC_A
2112c 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69  bort;.}.static i
2112d 6e 74 20 65 78 70 72 49 73 43 6f 6e 73 74 28 45  nt exprIsConst(E
2112e 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 6e 69 74  xpr *p, int init
2112f 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b 65 72 20  Flag){.  Walker 
21130 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20 69 6e 69  w;.  w.u.i = ini
21131 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45 78 70 72  tFlag;.  w.xExpr
21132 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70 72 4e  Callback = exprN
21133 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20  odeIsConstant;. 
21134 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
21135 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f 64 65 49  ck = selectNodeI
21136 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 73 71 6c  sConstant;.  sql
21137 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77 2c  ite3WalkExpr(&w,
21138 20 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 77 2e   p);.  return w.
21139 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61  u.i;.}../*.** Wa
2113a 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  lk an expression
2113b 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31   tree.  Return 1
2113c 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
2113d 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a  on is constant.*
2113e 2a 20 61 6e 64 20 30 20 69 66 20 69 74 20 69 6e  * and 0 if it in
2113f 76 6f 6c 76 65 73 20 76 61 72 69 61 62 6c 65 73  volves variables
21140 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c   or function cal
21141 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  ls..**.** For th
21142 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68  e purposes of th
21143 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64  is function, a d
21144 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72  ouble-quoted str
21145 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a  ing (ex: "abc").
21146 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ** is considered
21147 20 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20   a variable but 
21148 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20  a single-quoted 
21149 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63  string (ex: 'abc
2114a 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74  ') is.** a const
2114b 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ant..*/.SQLITE_P
2114c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2114d 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
2114e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 65 74  (Expr *p){.  ret
2114f 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 74 28  urn exprIsConst(
21150 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  p, 1);.}../*.** 
21151 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69  Walk an expressi
21152 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e  on tree.  Return
21153 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 73   1 if the expres
21154 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74  sion is constant
21155 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  .** that does no
21156 20 6f 72 69 67 69 6e 61 74 65 20 66 72 6f 6d 20   originate from 
21157 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20  the ON or USING 
21158 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a 6f 69  clauses of a joi
21159 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 69  n..** Return 0 i
2115a 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 20 76 61  f it involves va
2115b 72 69 61 62 6c 65 73 20 6f 72 20 66 75 6e 63 74  riables or funct
2115c 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20 74 65 72  ion calls or ter
2115d 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e  ms from.** an ON
2115e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65   or USING clause
2115f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
21160 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
21161 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74  xprIsConstantNot
21162 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20  Join(Expr *p){. 
21163 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f   return exprIsCo
21164 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a  nst(p, 3);.}../*
21165 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72  .** Walk an expr
21166 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65  ession tree.  Re
21167 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 65 78  turn 1 if the ex
21168 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73  pression is cons
21169 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 66 75 6e  tant.** or a fun
2116a 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 74 68 20  ction call with 
2116b 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d 65 6e  constant argumen
2116c 74 73 2e 20 20 52 65 74 75 72 6e 20 61 6e 64 20  ts.  Return and 
2116d 30 20 69 66 20 74 68 65 72 65 0a 2a 2a 20 61 72  0 if there.** ar
2116e 65 20 61 6e 79 20 76 61 72 69 61 62 6c 65 73 2e  e any variables.
2116f 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70  .**.** For the p
21170 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20  urposes of this 
21171 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62  function, a doub
21172 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le-quoted string
21173 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20   (ex: "abc").** 
21174 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20  is considered a 
21175 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 20 73  variable but a s
21176 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72  ingle-quoted str
21177 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29 20  ing (ex: 'abc') 
21178 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74  is.** a constant
21179 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2117a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
2117b 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46  xprIsConstantOrF
2117c 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 2a 70 29  unction(Expr *p)
2117d 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49  {.  return exprI
2117e 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b 0a 7d 0a  sConst(p, 2);.}.
2117f 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78  ./*.** If the ex
21180 70 72 65 73 73 69 6f 6e 20 70 20 63 6f 64 65 73  pression p codes
21181 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65   a constant inte
21182 67 65 72 20 74 68 61 74 20 69 73 20 73 6d 61 6c  ger that is smal
21183 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f 20 66  l enough.** to f
21184 69 74 20 69 6e 20 61 20 33 32 2d 62 69 74 20 69  it in a 32-bit i
21185 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 31  nteger, return 1
21186 20 61 6e 64 20 70 75 74 20 74 68 65 20 76 61 6c   and put the val
21187 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ue of the intege
21188 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c 75 65 2e  r.** in *pValue.
21189 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73    If the express
2118a 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e  ion is not an in
2118b 74 65 67 65 72 20 6f 72 20 69 66 20 69 74 20 69  teger or if it i
2118c 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 74 6f 20  s too big.** to 
2118d 66 69 74 20 69 6e 20 61 20 73 69 67 6e 65 64 20  fit in a signed 
2118e 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 2c 20  32-bit integer, 
2118f 72 65 74 75 72 6e 20 30 20 61 6e 64 20 6c 65 61  return 0 and lea
21190 76 65 20 2a 70 56 61 6c 75 65 20 75 6e 63 68 61  ve *pValue uncha
21191 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nged..*/.SQLITE_
21192 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
21193 74 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72  te3ExprIsInteger
21194 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70  (Expr *p, int *p
21195 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20 72 63  Value){.  int rc
21196 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e 66   = 0;.  if( p->f
21197 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56 61 6c  lags & EP_IntVal
21198 75 65 20 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75  ue ){.    *pValu
21199 65 20 3d 20 70 2d 3e 75 2e 69 56 61 6c 75 65 3b  e = p->u.iValue;
2119a 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
2119b 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e   }.  switch( p->
2119c 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
2119d 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20  K_INTEGER: {.   
2119e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 47     rc = sqlite3G
2119f 65 74 49 6e 74 33 32 28 70 2d 3e 75 2e 7a 54 6f  etInt32(p->u.zTo
211a0 6b 65 6e 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20  ken, pValue);.  
211a1 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
211a2 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
211a3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
211a4 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20   TK_UPLUS: {.   
211a5 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 45     rc = sqlite3E
211a6 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d 3e  xprIsInteger(p->
211a7 70 4c 65 66 74 2c 20 70 56 61 6c 75 65 29 3b 0a  pLeft, pValue);.
211a8 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
211a9 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55   }.    case TK_U
211aa 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20 20 69  MINUS: {.      i
211ab 6e 74 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20  nt v;.      if( 
211ac 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74  sqlite3ExprIsInt
211ad 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c 20 26  eger(p->pLeft, &
211ae 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70  v) ){.        *p
211af 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20  Value = -v;.    
211b0 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20      rc = 1;.    
211b1 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
211b2 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
211b3 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20  lt: break;.  }. 
211b4 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 61   if( rc ){.    a
211b5 73 73 65 72 74 28 20 45 78 70 72 48 61 73 41 6e  ssert( ExprHasAn
211b6 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  yProperty(p, EP_
211b7 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e  Reduced|EP_Token
211b8 4f 6e 6c 79 29 0a 20 20 20 20 20 20 20 20 20 20  Only).          
211b9 20 20 20 20 20 7c 7c 20 28 70 2d 3e 66 6c 61 67       || (p->flag
211ba 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c 6f 63 65  s2 & EP2_Malloce
211bb 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 3b 0a 20 20  dToken)==0 );.  
211bc 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49 4e 54    p->op = TK_INT
211bd 45 47 45 52 3b 0a 20 20 20 20 70 2d 3e 66 6c 61  EGER;.    p->fla
211be 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75  gs |= EP_IntValu
211bf 65 3b 0a 20 20 20 20 70 2d 3e 75 2e 69 56 61 6c  e;.    p->u.iVal
211c0 75 65 20 3d 20 2a 70 56 61 6c 75 65 3b 0a 20 20  ue = *pValue;.  
211c1 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
211c2 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 46  ../*.** Return F
211c3 41 4c 53 45 20 69 66 20 74 68 65 72 65 20 69 73  ALSE if there is
211c4 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20   no chance that 
211c5 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63  the expression c
211c6 61 6e 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  an be NULL..**.*
211c7 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73  * If the express
211c8 69 6f 6e 20 6d 69 67 68 74 20 62 65 20 4e 55 4c  ion might be NUL
211c9 4c 20 6f 72 20 69 66 20 74 68 65 20 65 78 70 72  L or if the expr
211ca 65 73 73 69 6f 6e 20 69 73 20 74 6f 6f 20 63 6f  ession is too co
211cb 6d 70 6c 65 78 0a 2a 2a 20 74 6f 20 74 65 6c 6c  mplex.** to tell
211cc 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 0a   return TRUE.  .
211cd 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
211ce 6e 65 20 69 73 20 75 73 65 64 20 61 73 20 61 6e  ne is used as an
211cf 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74   optimization, t
211d0 6f 20 73 6b 69 70 20 4f 50 5f 49 73 4e 75 6c 6c  o skip OP_IsNull
211d1 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 77 68 65 6e   opcodes.** when
211d2 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 61 20   we know that a 
211d3 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65 20  value cannot be 
211d4 4e 55 4c 4c 2e 20 20 48 65 6e 63 65 2c 20 61 20  NULL.  Hence, a 
211d5 66 61 6c 73 65 20 70 6f 73 69 74 69 76 65 0a 2a  false positive.*
211d6 2a 20 28 72 65 74 75 72 6e 69 6e 67 20 54 52 55  * (returning TRU
211d7 45 20 77 68 65 6e 20 69 6e 20 66 61 63 74 20 74  E when in fact t
211d8 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63 61  he expression ca
211d9 6e 20 6e 65 76 65 72 20 62 65 20 4e 55 4c 4c 29  n never be NULL)
211da 20 6d 69 67 68 74 0a 2a 2a 20 62 65 20 61 20 73   might.** be a s
211db 6d 61 6c 6c 20 70 65 72 66 6f 72 6d 61 6e 63 65  mall performance
211dc 20 68 69 74 20 62 75 74 20 69 73 20 6f 74 68 65   hit but is othe
211dd 72 77 69 73 65 20 68 61 72 6d 6c 65 73 73 2e 20  rwise harmless. 
211de 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a   On the other.**
211df 20 68 61 6e 64 2c 20 61 20 66 61 6c 73 65 20 6e   hand, a false n
211e0 65 67 61 74 69 76 65 20 28 72 65 74 75 72 6e 69  egative (returni
211e1 6e 67 20 46 41 4c 53 45 20 77 68 65 6e 20 74 68  ng FALSE when th
211e2 65 20 72 65 73 75 6c 74 20 63 6f 75 6c 64 20 62  e result could b
211e3 65 20 4e 55 4c 4c 29 0a 2a 2a 20 77 69 6c 6c 20  e NULL).** will 
211e4 6c 69 6b 65 6c 79 20 72 65 73 75 6c 74 20 69 6e  likely result in
211e5 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 61 6e   an incorrect an
211e6 73 77 65 72 2e 20 20 53 6f 20 77 68 65 6e 20 69  swer.  So when i
211e7 6e 20 64 6f 75 62 74 2c 20 72 65 74 75 72 6e 0a  n doubt, return.
211e8 2a 2a 20 54 52 55 45 2e 0a 2a 2f 0a 53 51 4c 49  ** TRUE..*/.SQLI
211e9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
211ea 71 6c 69 74 65 33 45 78 70 72 43 61 6e 42 65 4e  qlite3ExprCanBeN
211eb 75 6c 6c 28 63 6f 6e 73 74 20 45 78 70 72 20 2a  ull(const Expr *
211ec 70 29 7b 0a 20 20 75 38 20 6f 70 3b 0a 20 20 77  p){.  u8 op;.  w
211ed 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  hile( p->op==TK_
211ee 55 50 4c 55 53 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  UPLUS || p->op==
211ef 54 4b 5f 55 4d 49 4e 55 53 20 29 7b 20 70 20 3d  TK_UMINUS ){ p =
211f0 20 70 2d 3e 70 4c 65 66 74 3b 20 7d 0a 20 20 6f   p->pLeft; }.  o
211f1 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20 69 66 28  p = p->op;.  if(
211f2 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52   op==TK_REGISTER
211f3 20 29 20 6f 70 20 3d 20 70 2d 3e 6f 70 32 3b 0a   ) op = p->op2;.
211f4 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
211f5 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45      case TK_INTE
211f6 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  GER:.    case TK
211f7 5f 53 54 52 49 4e 47 3a 0a 20 20 20 20 63 61 73  _STRING:.    cas
211f8 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20  e TK_FLOAT:.    
211f9 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a 0a 20 20  case TK_BLOB:.  
211fa 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
211fb 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
211fc 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d   return 1;.  }.}
211fd 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
211fe 20 61 6e 20 4f 50 5f 49 73 4e 75 6c 6c 20 69 6e   an OP_IsNull in
211ff 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 74  struction that t
21200 65 73 74 73 20 72 65 67 69 73 74 65 72 20 69 52  ests register iR
21201 65 67 20 61 6e 64 20 6a 75 6d 70 73 0a 2a 2a 20  eg and jumps.** 
21202 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69 44 65 73  to location iDes
21203 74 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69  t if the value i
21204 6e 20 69 52 65 67 20 69 73 20 4e 55 4c 4c 2e 20  n iReg is NULL. 
21205 20 54 68 65 20 76 61 6c 75 65 20 69 6e 20 69 52   The value in iR
21206 65 67 20 0a 2a 2a 20 77 61 73 20 63 6f 6d 70 75  eg .** was compu
21207 74 65 64 20 62 79 20 70 45 78 70 72 2e 20 20 49  ted by pExpr.  I
21208 66 20 77 65 20 63 61 6e 20 6c 6f 6f 6b 20 61 74  f we can look at
21209 20 70 45 78 70 72 20 61 74 20 63 6f 6d 70 69 6c   pExpr at compil
2120a 65 2d 74 69 6d 65 20 61 6e 64 0a 2a 2a 20 64 65  e-time and.** de
2120b 74 65 72 6d 69 6e 65 20 74 68 61 74 20 69 74 20  termine that it 
2120c 63 61 6e 20 6e 65 76 65 72 20 67 65 6e 65 72 61  can never genera
2120d 74 65 20 61 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  te a NULL, then 
2120e 74 68 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 6f 70  the OP_IsNull op
2120f 65 72 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e 20 62  eration.** can b
21210 65 20 6f 6d 69 74 74 65 64 2e 0a 2a 2f 0a 53 51  e omitted..*/.SQ
21211 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
21212 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  d sqlite3ExprCod
21213 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 0a 20 20 56  eIsNullJump(.  V
21214 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20 20  dbe *v,         
21215 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 75     /* The VDBE u
21216 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
21217 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 45 78 70  n */.  const Exp
21218 72 20 2a 70 45 78 70 72 2c 20 20 2f 2a 20 4f 6e  r *pExpr,  /* On
21219 6c 79 20 67 65 6e 65 72 61 74 65 20 4f 50 5f 49  ly generate OP_I
2121a 73 4e 75 6c 6c 20 69 66 20 74 68 69 73 20 65 78  sNull if this ex
2121b 70 72 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 2a  pr can be NULL *
2121c 2f 0a 20 20 69 6e 74 20 69 52 65 67 2c 20 20 20  /.  int iReg,   
2121d 20 20 20 20 20 20 20 20 2f 2a 20 54 65 73 74 20          /* Test 
2121e 74 68 65 20 76 61 6c 75 65 20 69 6e 20 74 68 69  the value in thi
2121f 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 4e  s register for N
21220 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 44 65  ULL */.  int iDe
21221 73 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  st           /* 
21222 4a 75 6d 70 20 68 65 72 65 20 69 66 20 74 68 65  Jump here if the
21223 20 76 61 6c 75 65 20 69 73 20 6e 75 6c 6c 20 2a   value is null *
21224 2f 0a 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74  /.){.  if( sqlit
21225 65 33 45 78 70 72 43 61 6e 42 65 4e 75 6c 6c 28  e3ExprCanBeNull(
21226 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 73 71  pExpr) ){.    sq
21227 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21228 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 69 52  v, OP_IsNull, iR
21229 65 67 2c 20 69 44 65 73 74 29 3b 0a 20 20 7d 0a  eg, iDest);.  }.
2122a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2122b 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76 65  TRUE if the give
2122c 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  n expression is 
2122d 61 20 63 6f 6e 73 74 61 6e 74 20 77 68 69 63 68  a constant which
2122e 20 77 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 6e 63   would be.** unc
2122f 68 61 6e 67 65 64 20 62 79 20 4f 50 5f 41 66 66  hanged by OP_Aff
21230 69 6e 69 74 79 20 77 69 74 68 20 74 68 65 20 61  inity with the a
21231 66 66 69 6e 69 74 79 20 67 69 76 65 6e 20 69 6e  ffinity given in
21232 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 61   the second.** a
21233 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  rgument..**.** T
21234 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
21235 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
21236 20 69 66 20 74 68 65 20 4f 50 5f 41 66 66 69 6e   if the OP_Affin
21237 69 74 79 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a  ity operation.**
21238 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64 2e   can be omitted.
21239 20 20 57 68 65 6e 20 69 6e 20 64 6f 75 62 74 20    When in doubt 
2123a 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 20 20 41  return FALSE.  A
2123b 20 66 61 6c 73 65 20 6e 65 67 61 74 69 76 65 0a   false negative.
2123c 2a 2a 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20  ** is harmless. 
2123d 20 41 20 66 61 6c 73 65 20 70 6f 73 69 74 69 76   A false positiv
2123e 65 2c 20 68 6f 77 65 76 65 72 2c 20 63 61 6e 20  e, however, can 
2123f 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 77 72  result in the wr
21240 6f 6e 67 0a 2a 2a 20 61 6e 73 77 65 72 2e 0a 2a  ong.** answer..*
21241 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21242 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
21243 4e 65 65 64 73 4e 6f 41 66 66 69 6e 69 74 79 43  NeedsNoAffinityC
21244 68 61 6e 67 65 28 63 6f 6e 73 74 20 45 78 70 72  hange(const Expr
21245 20 2a 70 2c 20 63 68 61 72 20 61 66 66 29 7b 0a   *p, char aff){.
21246 20 20 75 38 20 6f 70 3b 0a 20 20 69 66 28 20 61    u8 op;.  if( a
21247 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
21248 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ONE ) return 1;.
21249 20 20 77 68 69 6c 65 28 20 70 2d 3e 6f 70 3d 3d    while( p->op==
2124a 54 4b 5f 55 50 4c 55 53 20 7c 7c 20 70 2d 3e 6f  TK_UPLUS || p->o
2124b 70 3d 3d 54 4b 5f 55 4d 49 4e 55 53 20 29 7b 20  p==TK_UMINUS ){ 
2124c 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 20 7d 0a  p = p->pLeft; }.
2124d 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20 20    op = p->op;.  
2124e 69 66 28 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  if( op==TK_REGIS
2124f 54 45 52 20 29 20 6f 70 20 3d 20 70 2d 3e 6f 70  TER ) op = p->op
21250 32 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20  2;.  switch( op 
21251 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  ){.    case TK_I
21252 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20  NTEGER: {.      
21253 72 65 74 75 72 6e 20 61 66 66 3d 3d 53 51 4c 49  return aff==SQLI
21254 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 7c  TE_AFF_INTEGER |
21255 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46  | aff==SQLITE_AF
21256 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d  F_NUMERIC;.    }
21257 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f  .    case TK_FLO
21258 41 54 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75  AT: {.      retu
21259 72 6e 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41  rn aff==SQLITE_A
2125a 46 46 5f 52 45 41 4c 20 7c 7c 20 61 66 66 3d 3d  FF_REAL || aff==
2125b 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
2125c 49 43 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  IC;.    }.    ca
2125d 73 65 20 54 4b 5f 53 54 52 49 4e 47 3a 20 7b 0a  se TK_STRING: {.
2125e 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 66 66        return aff
2125f 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  ==SQLITE_AFF_TEX
21260 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  T;.    }.    cas
21261 65 20 54 4b 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20  e TK_BLOB: {.   
21262 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
21263 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43   }.    case TK_C
21264 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 61  OLUMN: {.      a
21265 73 73 65 72 74 28 20 70 2d 3e 69 54 61 62 6c 65  ssert( p->iTable
21266 3e 3d 30 20 29 3b 20 20 2f 2a 20 70 20 63 61 6e  >=0 );  /* p can
21267 6e 6f 74 20 62 65 20 70 61 72 74 20 6f 66 20 61  not be part of a
21268 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e   CHECK constrain
21269 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72  t */.      retur
2126a 6e 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 0a 20  n p->iColumn<0. 
2126b 20 20 20 20 20 20 20 20 20 26 26 20 28 61 66 66           && (aff
2126c 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  ==SQLITE_AFF_INT
2126d 45 47 45 52 20 7c 7c 20 61 66 66 3d 3d 53 51 4c  EGER || aff==SQL
2126e 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 29  ITE_AFF_NUMERIC)
2126f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
21270 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 65 74  ult: {.      ret
21271 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
21272 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
21273 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76   TRUE if the giv
21274 65 6e 20 73 74 72 69 6e 67 20 69 73 20 61 20 72  en string is a r
21275 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ow-id column nam
21276 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
21277 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
21278 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 68  IsRowid(const ch
21279 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20 73 71  ar *z){.  if( sq
2127a 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
2127b 22 5f 52 4f 57 49 44 5f 22 29 3d 3d 30 20 29 20  "_ROWID_")==0 ) 
2127c 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20  return 1;.  if( 
2127d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2127e 2c 20 22 52 4f 57 49 44 22 29 3d 3d 30 20 29 20  , "ROWID")==0 ) 
2127f 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20  return 1;.  if( 
21280 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
21281 2c 20 22 4f 49 44 22 29 3d 3d 30 20 29 20 72 65  , "OID")==0 ) re
21282 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e  turn 1;.  return
21283 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   0;.}../*.** Ret
21284 75 72 6e 20 74 72 75 65 20 69 66 20 77 65 20 61  urn true if we a
21285 72 65 20 61 62 6c 65 20 74 6f 20 74 68 65 20 49  re able to the I
21286 4e 20 6f 70 65 72 61 74 6f 72 20 6f 70 74 69 6d  N operator optim
21287 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 0a 2a 2a 20  ization on a.** 
21288 71 75 65 72 79 20 6f 66 20 74 68 65 20 66 6f 72  query of the for
21289 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 78 20  m.**.**       x 
2128a 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 0a  IN (SELECT ...).
2128b 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 74 68 65 20  **.** Where the 
2128c 53 45 4c 45 43 54 2e 2e 2e 20 63 6c 61 75 73 65  SELECT... clause
2128d 20 69 73 20 61 73 20 73 70 65 63 69 66 69 65 64   is as specified
2128e 20 62 79 20 74 68 65 20 70 61 72 61 6d 65 74 65   by the paramete
2128f 72 20 74 6f 20 74 68 69 73 0a 2a 2a 20 72 6f 75  r to this.** rou
21290 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tine..**.** The 
21291 53 65 6c 65 63 74 20 6f 62 6a 65 63 74 20 70 61  Select object pa
21292 73 73 65 64 20 69 6e 20 68 61 73 20 61 6c 72 65  ssed in has alre
21293 61 64 79 20 62 65 65 6e 20 70 72 65 70 72 6f 63  ady been preproc
21294 65 73 73 65 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20  essed and no.** 
21295 65 72 72 6f 72 73 20 68 61 76 65 20 62 65 65 6e  errors have been
21296 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 23 69 66 6e 64   found..*/.#ifnd
21297 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
21298 55 42 51 55 45 52 59 0a 73 74 61 74 69 63 20 69  UBQUERY.static i
21299 6e 74 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f  nt isCandidateFo
2129a 72 49 6e 4f 70 74 28 53 65 6c 65 63 74 20 2a 70  rInOpt(Select *p
2129b 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  ){.  SrcList *pS
2129c 72 63 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  rc;.  ExprList *
2129d 70 45 4c 69 73 74 3b 0a 20 20 54 61 62 6c 65 20  pEList;.  Table 
2129e 2a 70 54 61 62 3b 0a 20 20 69 66 28 20 70 3d 3d  *pTab;.  if( p==
2129f 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  0 ) return 0;   
212a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
212a1 2f 2a 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  /* right-hand si
212a2 64 65 20 6f 66 20 49 4e 20 69 73 20 53 45 4c 45  de of IN is SELE
212a3 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  CT */.  if( p->p
212a4 50 72 69 6f 72 20 29 20 72 65 74 75 72 6e 20 30  Prior ) return 0
212a5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
212a6 2a 20 4e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64  * Not a compound
212a7 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28   SELECT */.  if(
212a8 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28   p->selFlags & (
212a9 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41  SF_Distinct|SF_A
212aa 67 67 72 65 67 61 74 65 29 20 29 7b 0a 20 20 20  ggregate) ){.   
212ab 20 74 65 73 74 63 61 73 65 28 20 28 70 2d 3e 73   testcase( (p->s
212ac 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69  elFlags & (SF_Di
212ad 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67  stinct|SF_Aggreg
212ae 61 74 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e  ate))==SF_Distin
212af 63 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  ct );.    testca
212b0 73 65 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73  se( (p->selFlags
212b1 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c   & (SF_Distinct|
212b2 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 3d 3d  SF_Aggregate))==
212b3 53 46 5f 41 67 67 72 65 67 61 74 65 20 29 3b 0a  SF_Aggregate );.
212b4 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a      return 0; /*
212b5 20 4e 6f 20 44 49 53 54 49 4e 43 54 20 6b 65 79   No DISTINCT key
212b6 77 6f 72 64 20 61 6e 64 20 6e 6f 20 61 67 67 72  word and no aggr
212b7 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
212b8 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  */.  }.  assert(
212b9 20 70 2d 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20   p->pGroupBy==0 
212ba 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
212bb 2f 2a 20 48 61 73 20 6e 6f 20 47 52 4f 55 50 20  /* Has no GROUP 
212bc 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  BY clause */.  i
212bd 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72  f( p->pLimit ) r
212be 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
212bf 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20        /* Has no 
212c0 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a  LIMIT clause */.
212c1 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 66    assert( p->pOf
212c2 66 73 65 74 3d 3d 30 20 29 3b 20 20 20 20 20 20  fset==0 );      
212c3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 4c           /* No L
212c4 49 4d 49 54 20 6d 65 61 6e 73 20 6e 6f 20 4f 46  IMIT means no OF
212c5 46 53 45 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d  FSET */.  if( p-
212c6 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 72 6e  >pWhere ) return
212c7 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
212c8 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45 52 45   /* Has no WHERE
212c9 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70 53 72   clause */.  pSr
212ca 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61  c = p->pSrc;.  a
212cb 73 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29  ssert( pSrc!=0 )
212cc 3b 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53  ;.  if( pSrc->nS
212cd 72 63 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30  rc!=1 ) return 0
212ce 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69  ;          /* Si
212cf 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 46 52 4f  ngle term in FRO
212d0 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66  M clause */.  if
212d1 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65  ( pSrc->a[0].pSe
212d2 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b  lect ) return 0;
212d3 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 69 73 20       /* FROM is 
212d4 6e 6f 74 20 61 20 73 75 62 71 75 65 72 79 20 6f  not a subquery o
212d5 72 20 76 69 65 77 20 2a 2f 0a 20 20 70 54 61 62  r view */.  pTab
212d6 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54   = pSrc->a[0].pT
212d7 61 62 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28  ab;.  if( NEVER(
212d8 70 54 61 62 3d 3d 30 29 20 29 20 72 65 74 75 72  pTab==0) ) retur
212d9 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70  n 0;.  assert( p
212da 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20  Tab->pSelect==0 
212db 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  );            /*
212dc 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20   FROM clause is 
212dd 6e 6f 74 20 61 20 76 69 65 77 20 2a 2f 0a 20 20  not a view */.  
212de 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
212df 61 62 29 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ab) ) return 0; 
212e0 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63         /* FROM c
212e1 6c 61 75 73 65 20 6e 6f 74 20 61 20 76 69 72 74  lause not a virt
212e2 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 70  ual table */.  p
212e3 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  EList = p->pELis
212e4 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  t;.  if( pEList-
212e5 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75  >nExpr!=1 ) retu
212e6 72 6e 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f  rn 0;       /* O
212e7 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  ne column in the
212e8 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
212e9 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30   if( pEList->a[0
212ea 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ].pExpr->op!=TK_
212eb 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 20  COLUMN ) return 
212ec 30 3b 20 2f 2a 20 52 65 73 75 6c 74 20 69 73 20  0; /* Result is 
212ed 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 72 65  a column */.  re
212ee 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66  turn 1;.}.#endif
212ef 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
212f0 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a  SUBQUERY */../*.
212f1 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
212f2 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
212f3 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
212f4 66 20 74 68 65 20 49 4e 20 28 2e 2e 2e 29 20 6f  f the IN (...) o
212f5 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 49 74 27 73  perator..** It's
212f6 20 6a 6f 62 20 69 73 20 74 6f 20 66 69 6e 64 20   job is to find 
212f7 6f 72 20 63 72 65 61 74 65 20 61 20 62 2d 74 72  or create a b-tr
212f8 65 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ee structure tha
212f9 74 20 6d 61 79 20 62 65 20 75 73 65 64 0a 2a 2a  t may be used.**
212fa 20 65 69 74 68 65 72 20 74 6f 20 74 65 73 74 20   either to test 
212fb 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 6f  for membership o
212fc 66 20 74 68 65 20 28 2e 2e 2e 29 20 73 65 74 20  f the (...) set 
212fd 6f 72 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  or to iterate th
212fe 72 6f 75 67 68 0a 2a 2a 20 69 74 73 20 6d 65 6d  rough.** its mem
212ff 62 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 20 64  bers, skipping d
21300 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a  uplicates..**.**
21301 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   The index of th
21302 65 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20  e cursor opened 
21303 6f 6e 20 74 68 65 20 62 2d 74 72 65 65 20 28 64  on the b-tree (d
21304 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c 20 64  atabase table, d
21305 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 0a 2a  atabase index .*
21306 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c 20 74 61  * or ephermal ta
21307 62 6c 65 29 20 69 73 20 73 74 6f 72 65 64 20 69  ble) is stored i
21308 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20 62 65 66  n pX->iTable bef
21309 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ore this functio
2130a 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 68  n returns..** Th
2130b 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65  e returned value
2130c 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
2130d 6e 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20  n indicates the 
2130e 62 2d 74 72 65 65 20 74 79 70 65 2c 20 61 73 20  b-tree type, as 
2130f 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
21310 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20   IN_INDEX_ROWID 
21311 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73  - The cursor was
21312 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74   opened on a dat
21313 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20  abase table..** 
21314 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58    IN_INDEX_INDEX
21315 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61   - The cursor wa
21316 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61  s opened on a da
21317 74 61 62 61 73 65 20 69 6e 64 65 78 2e 0a 2a 2a  tabase index..**
21318 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20     IN_INDEX_EPH 
21319 2d 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 77  -   The cursor w
2131a 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 73  as opened on a s
2131b 70 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65 64  pecially created
2131c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
2131d 20 20 20 20 20 20 20 20 20 20 20 70 6f 70 75 6c             popul
2131e 61 74 65 64 20 65 70 68 65 72 65 6d 61 6c 20 74  ated epheremal t
2131f 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65  able..**.** An e
21320 78 69 73 74 69 6e 67 20 62 2d 74 72 65 65 20 6d  xisting b-tree m
21321 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20  ay only be used 
21322 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 69 73  if the SELECT is
21323 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 0a 2a   of the simple.*
21324 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  * form:.**.**   
21325 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75 6d 6e    SELECT <column
21326 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e 0a 2a  > FROM <table>.*
21327 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f  *.** If the prNo
21328 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72  tFound parameter
21329 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20   is 0, then the 
2132a 62 2d 74 72 65 65 20 77 69 6c 6c 20 62 65 20 75  b-tree will be u
2132b 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 0a 2a  sed to iterate.*
2132c 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 73 65  * through the se
2132d 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70  t members, skipp
2132e 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 63 61 74  ing any duplicat
2132f 65 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  es. In this case
21330 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 6d 61 6c   an.** epheremal
21331 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 75   table must be u
21332 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65 20 73  sed unless the s
21333 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 6d 6e 3e  elected <column>
21334 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a 2a   is guaranteed.*
21335 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 65 20 2d  * to be unique -
21336 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 20   either because 
21337 69 74 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52  it is an INTEGER
21338 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
21339 69 74 0a 2a 2a 20 68 61 73 20 61 20 55 4e 49 51  it.** has a UNIQ
2133a 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72  UE constraint or
2133b 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e 0a 2a   UNIQUE index..*
2133c 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f  *.** If the prNo
2133d 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72  tFound parameter
2133e 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20   is not 0, then 
2133f 74 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20  the b-tree will 
21340 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20  be used .** for 
21341 66 61 73 74 20 73 65 74 20 6d 65 6d 62 65 72 73  fast set members
21342 68 69 70 20 74 65 73 74 73 2e 20 49 6e 20 74 68  hip tests. In th
21343 69 73 20 63 61 73 65 20 61 6e 20 65 70 68 65 72  is case an epher
21344 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20  emal table must 
21345 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 6e 6c 65  .** be used unle
21346 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 61  ss <column> is a
21347 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  n INTEGER PRIMAR
21348 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 6e 64 65  Y KEY or an inde
21349 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20 66 6f 75  x can .** be fou
2134a 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 6d 6e 3e  nd with <column>
2134b 20 61 73 20 69 74 73 20 6c 65 66 74 2d 6d 6f 73   as its left-mos
2134c 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20  t column..**.** 
2134d 57 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20  When the b-tree 
2134e 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f  is being used fo
2134f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73  r membership tes
21350 74 73 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  ts, the calling 
21351 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6e 65 65 64  function.** need
21352 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65  s to know whethe
21353 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73 74 72  r or not the str
21354 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20  ucture contains 
21355 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a 20  an SQL NULL .** 
21356 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74  value in order t
21357 6f 20 63 6f 72 72 65 63 74 6c 79 20 65 76 61 6c  o correctly eval
21358 75 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73  uate expressions
21359 20 6c 69 6b 65 20 22 58 20 49 4e 20 28 59 2c 20   like "X IN (Y, 
2135a 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65  Z)"..** If there
2135b 20 69 73 20 61 6e 79 20 63 68 61 6e 63 65 20 74   is any chance t
2135c 68 61 74 20 74 68 65 20 28 2e 2e 2e 29 20 6d 69  hat the (...) mi
2135d 67 68 74 20 63 6f 6e 74 61 69 6e 20 61 20 4e 55  ght contain a NU
2135e 4c 4c 20 76 61 6c 75 65 20 61 74 0a 2a 2a 20 72  LL value at.** r
2135f 75 6e 74 69 6d 65 2c 20 74 68 65 6e 20 61 20 72  untime, then a r
21360 65 67 69 73 74 65 72 20 69 73 20 61 6c 6c 6f 63  egister is alloc
21361 61 74 65 64 20 61 6e 64 20 74 68 65 20 72 65 67  ated and the reg
21362 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77 72 69  ister number wri
21363 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e 6f  tten.** to *prNo
21364 74 46 6f 75 6e 64 2e 20 49 66 20 74 68 65 72 65  tFound. If there
21365 20 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68   is no chance th
21366 61 74 20 74 68 65 20 28 2e 2e 2e 29 20 63 6f 6e  at the (...) con
21367 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 4c 4c 20  tains a.** NULL 
21368 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 72 4e  value, then *prN
21369 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 66 74 20  otFound is left 
2136a 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a  unchanged..**.**
2136b 20 49 66 20 61 20 72 65 67 69 73 74 65 72 20 69   If a register i
2136c 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  s allocated and 
2136d 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 73 74 6f  its location sto
2136e 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 46 6f 75  red in *prNotFou
2136f 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 74 73 20  nd, then.** its 
21370 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 69 73  initial value is
21371 20 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65 20 28   NULL.  If the (
21372 2e 2e 2e 29 20 64 6f 65 73 20 6e 6f 74 20 72 65  ...) does not re
21373 6d 61 69 6e 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a  main constant.**
21374 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f   for the duratio
21375 6e 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 28  n of the query (
21376 69 2e 65 2e 20 74 68 65 20 53 45 4c 45 43 54 20  i.e. the SELECT 
21377 77 69 74 68 69 6e 20 74 68 65 20 28 2e 2e 2e 29  within the (...)
21378 0a 2a 2a 20 69 73 20 61 20 63 6f 72 72 65 6c 61  .** is a correla
21379 74 65 64 20 73 75 62 71 75 65 72 79 29 20 74 68  ted subquery) th
2137a 65 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  en the value of 
2137b 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65  the allocated re
2137c 67 69 73 74 65 72 20 69 73 0a 2a 2a 20 72 65 73  gister is.** res
2137d 65 74 20 74 6f 20 4e 55 4c 4c 20 65 61 63 68 20  et to NULL each 
2137e 74 69 6d 65 20 74 68 65 20 73 75 62 71 75 65 72  time the subquer
2137f 79 20 69 73 20 72 65 72 75 6e 2e 20 54 68 69 73  y is rerun. This
21380 20 61 6c 6c 6f 77 73 20 74 68 65 0a 2a 2a 20 63   allows the.** c
21381 61 6c 6c 65 72 20 74 6f 20 75 73 65 20 76 64 62  aller to use vdb
21382 65 20 63 6f 64 65 20 65 71 75 69 76 61 6c 65 6e  e code equivalen
21383 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  t to the followi
21384 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20  ng:.**.**   if( 
21385 72 65 67 69 73 74 65 72 3d 3d 4e 55 4c 4c 20 29  register==NULL )
21386 7b 0a 2a 2a 20 20 20 20 20 68 61 73 5f 6e 75 6c  {.**     has_nul
21387 6c 20 3d 20 3c 74 65 73 74 20 69 66 20 64 61 74  l = <test if dat
21388 61 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  a structure cont
21389 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 20 20  ains null>.**   
2138a 20 20 72 65 67 69 73 74 65 72 20 3d 20 31 0a 2a    register = 1.*
2138b 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20 69 6e 20 6f  *   }.**.** in o
2138c 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 72 75  rder to avoid ru
2138d 6e 6e 69 6e 67 20 74 68 65 20 3c 74 65 73 74 20  nning the <test 
2138e 69 66 20 64 61 74 61 20 73 74 72 75 63 74 75 72  if data structur
2138f 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e  e contains null>
21390 0a 2a 2a 20 74 65 73 74 20 6d 6f 72 65 20 6f 66  .** test more of
21391 74 65 6e 20 74 68 61 6e 20 69 73 20 6e 65 63 65  ten than is nece
21392 73 73 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ssary..*/.#ifnde
21393 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
21394 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50 52  BQUERY.SQLITE_PR
21395 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
21396 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 50 61 72  3FindInIndex(Par
21397 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
21398 20 2a 70 58 2c 20 69 6e 74 20 2a 70 72 4e 6f 74   *pX, int *prNot
21399 46 6f 75 6e 64 29 7b 0a 20 20 53 65 6c 65 63 74  Found){.  Select
2139a 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   *p;            
2139b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2139c 2f 2a 20 53 45 4c 45 43 54 20 74 6f 20 74 68 65  /* SELECT to the
2139d 20 72 69 67 68 74 20 6f 66 20 49 4e 20 6f 70 65   right of IN ope
2139e 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 65  rator */.  int e
2139f 54 79 70 65 20 3d 20 30 3b 20 20 20 20 20 20 20  Type = 0;       
213a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213a1 20 2f 2a 20 54 79 70 65 20 6f 66 20 52 48 53 20   /* Type of RHS 
213a2 74 61 62 6c 65 2e 20 49 4e 5f 49 4e 44 45 58 5f  table. IN_INDEX_
213a3 2a 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 20  * */.  int iTab 
213a4 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
213a5 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
213a6 43 75 72 73 6f 72 20 6f 66 20 74 68 65 20 52 48  Cursor of the RH
213a7 53 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  S table */.  int
213a8 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d 20   mustBeUnique = 
213a9 28 70 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30 29 3b  (prNotFound==0);
213aa 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 52 48     /* True if RH
213ab 53 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65  S must be unique
213ac 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
213ad 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a  X->op==TK_IN );.
213ae 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73  .  /* Check to s
213af 65 65 20 69 66 20 61 6e 20 65 78 69 73 74 69 6e  ee if an existin
213b0 67 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  g table or index
213b1 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 0a   can be used to.
213b2 20 20 2a 2a 20 73 61 74 69 73 66 79 20 74 68 65    ** satisfy the
213b3 20 71 75 65 72 79 2e 20 20 54 68 69 73 20 69 73   query.  This is
213b4 20 70 72 65 66 65 72 61 62 6c 65 20 74 6f 20 67   preferable to g
213b5 65 6e 65 72 61 74 69 6e 67 20 61 20 6e 65 77 20  enerating a new 
213b6 0a 20 20 2a 2a 20 65 70 68 65 6d 65 72 61 6c 20  .  ** ephemeral 
213b7 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 20  table..  */.  p 
213b8 3d 20 28 45 78 70 72 48 61 73 50 72 6f 70 65 72  = (ExprHasProper
213b9 74 79 28 70 58 2c 20 45 50 5f 78 49 73 53 65 6c  ty(pX, EP_xIsSel
213ba 65 63 74 29 20 3f 20 70 58 2d 3e 78 2e 70 53 65  ect) ? pX->x.pSe
213bb 6c 65 63 74 20 3a 20 30 29 3b 0a 20 20 69 66 28  lect : 0);.  if(
213bc 20 41 4c 57 41 59 53 28 70 50 61 72 73 65 2d 3e   ALWAYS(pParse->
213bd 6e 45 72 72 3d 3d 30 29 20 26 26 20 69 73 43 61  nErr==0) && isCa
213be 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 74 28  ndidateForInOpt(
213bf 70 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  p) ){.    sqlite
213c0 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
213c1 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
213c2 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
213c3 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 45  nection */.    E
213c4 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 2d 3e  xpr *pExpr = p->
213c5 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
213c6 70 72 3b 20 20 20 2f 2a 20 45 78 70 72 65 73 73  pr;   /* Express
213c7 69 6f 6e 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a  ion <column> */.
213c8 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70      int iCol = p
213c9 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20  Expr->iColumn;  
213ca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
213cb 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 3c 63  dex of column <c
213cc 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 20 20 56 64  olumn> */.    Vd
213cd 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
213ce 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 20  etVdbe(pParse); 
213cf 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
213d0 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20 63 6f  machine being co
213d1 64 65 64 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65  ded */.    Table
213d2 20 2a 70 54 61 62 20 3d 20 70 2d 3e 70 53 72 63   *pTab = p->pSrc
213d3 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b 20 20 20 20  ->a[0].pTab;    
213d4 20 20 2f 2a 20 54 61 62 6c 65 20 3c 74 61 62 6c    /* Table <tabl
213d5 65 3e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  e>. */.    int i
213d6 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
213d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
213d8 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 64    /* Database id
213d9 78 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20  x for pTab */.  
213da 20 0a 20 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e   .    /* Code an
213db 20 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65   OP_VerifyCookie
213dc 20 61 6e 64 20 4f 50 5f 54 61 62 6c 65 4c 6f 63   and OP_TableLoc
213dd 6b 20 66 6f 72 20 3c 74 61 62 6c 65 3e 2e 20 2a  k for <table>. *
213de 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69  /.    iDb = sqli
213df 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
213e0 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
213e1 6d 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ma);.    sqlite3
213e2 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61  CodeVerifySchema
213e3 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
213e4 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c     sqlite3TableL
213e5 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ock(pParse, iDb,
213e6 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20   pTab->tnum, 0, 
213e7 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20  pTab->zName);.. 
213e8 20 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74     /* This funct
213e9 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c  ion is only call
213ea 65 64 20 66 72 6f 6d 20 74 77 6f 20 70 6c 61 63  ed from two plac
213eb 65 73 2e 20 49 6e 20 62 6f 74 68 20 63 61 73 65  es. In both case
213ec 73 20 74 68 65 20 76 64 62 65 0a 20 20 20 20 2a  s the vdbe.    *
213ed 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  * has already be
213ee 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2e 20 53 6f  en allocated. So
213ef 20 61 73 73 75 6d 65 20 73 71 6c 69 74 65 33 47   assume sqlite3G
213f0 65 74 56 64 62 65 28 29 20 69 73 20 61 6c 77 61  etVdbe() is alwa
213f1 79 73 0a 20 20 20 20 2a 2a 20 73 75 63 63 65 73  ys.    ** succes
213f2 73 66 75 6c 20 68 65 72 65 2e 0a 20 20 20 20 2a  sful here..    *
213f3 2f 0a 20 20 20 20 61 73 73 65 72 74 28 76 29 3b  /.    assert(v);
213f4 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20  .    if( iCol<0 
213f5 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65  ){.      int iMe
213f6 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  m = ++pParse->nM
213f7 65 6d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 41  em;.      int iA
213f8 64 64 72 3b 0a 0a 20 20 20 20 20 20 69 41 64 64  ddr;..      iAdd
213f9 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
213fa 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp1(v, OP_If, 
213fb 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c  iMem);.      sql
213fc 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
213fd 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
213fe 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 20 20 73   iMem);..      s
213ff 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
21400 70 50 61 72 73 65 2c 20 69 54 61 62 2c 20 69 44  pParse, iTab, iD
21401 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e  b, pTab, OP_Open
21402 52 65 61 64 29 3b 0a 20 20 20 20 20 20 65 54 79  Read);.      eTy
21403 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f  pe = IN_INDEX_RO
21404 57 49 44 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69  WID;..      sqli
21405 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
21406 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 20 7d  v, iAddr);.    }
21407 65 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65  else{.      Inde
21408 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
21409 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2140a 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72   /* Iterator var
2140b 69 61 62 6c 65 20 2a 2f 0a 0a 20 20 20 20 20 20  iable */..      
2140c 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  /* The collation
2140d 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 20 62   sequence used b
2140e 79 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  y the comparison
2140f 2e 20 49 66 20 61 6e 20 69 6e 64 65 78 20 69 73  . If an index is
21410 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 65 20   to.      ** be 
21411 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66  used in place of
21412 20 61 20 74 65 6d 70 2d 74 61 62 6c 65 2c 20 69   a temp-table, i
21413 74 20 6d 75 73 74 20 62 65 20 6f 72 64 65 72 65  t must be ordere
21414 64 20 61 63 63 6f 72 64 69 6e 67 0a 20 20 20 20  d according.    
21415 20 20 2a 2a 20 74 6f 20 74 68 69 73 20 63 6f 6c    ** to this col
21416 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e  lation sequence.
21417 20 20 2a 2f 0a 20 20 20 20 20 20 43 6f 6c 6c 53    */.      CollS
21418 65 71 20 2a 70 52 65 71 20 3d 20 73 71 6c 69 74  eq *pReq = sqlit
21419 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43  e3BinaryCompareC
2141a 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
2141b 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 29  X->pLeft, pExpr)
2141c 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63  ;..      /* Chec
2141d 6b 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e  k that the affin
2141e 69 74 79 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ity that will be
2141f 20 75 73 65 64 20 74 6f 20 70 65 72 66 6f 72 6d   used to perform
21420 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 63   the .      ** c
21421 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 68 65  omparison is the
21422 20 73 61 6d 65 20 61 73 20 74 68 65 20 61 66 66   same as the aff
21423 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c  inity of the col
21424 75 6d 6e 2e 20 49 66 0a 20 20 20 20 20 20 2a 2a  umn. If.      **
21425 20 69 74 20 69 73 20 6e 6f 74 2c 20 69 74 20 69   it is not, it i
21426 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74  s not possible t
21427 6f 20 75 73 65 20 61 6e 79 20 69 6e 64 65 78 2e  o use any index.
21428 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
21429 63 68 61 72 20 61 66 66 20 3d 20 63 6f 6d 70 61  char aff = compa
2142a 72 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 58  risonAffinity(pX
2142b 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66  );.      int aff
2142c 69 6e 69 74 79 5f 6f 6b 20 3d 20 28 70 54 61 62  inity_ok = (pTab
2142d 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 61 66 66  ->aCol[iCol].aff
2142e 69 6e 69 74 79 3d 3d 61 66 66 7c 7c 61 66 66 3d  inity==aff||aff=
2142f 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45  =SQLITE_AFF_NONE
21430 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 70 49  );..      for(pI
21431 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
21432 20 70 49 64 78 20 26 26 20 65 54 79 70 65 3d 3d   pIdx && eType==
21433 30 20 26 26 20 61 66 66 69 6e 69 74 79 5f 6f 6b  0 && affinity_ok
21434 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
21435 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  xt){.        if(
21436 20 28 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e   (pIdx->aiColumn
21437 5b 30 5d 3d 3d 69 43 6f 6c 29 0a 20 20 20 20 20  [0]==iCol).     
21438 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 69      && sqlite3Fi
21439 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e  ndCollSeq(db, EN
2143a 43 28 64 62 29 2c 20 70 49 64 78 2d 3e 61 7a 43  C(db), pIdx->azC
2143b 6f 6c 6c 5b 30 5d 2c 20 30 29 3d 3d 70 52 65 71  oll[0], 0)==pReq
2143c 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 21 6d  .         && (!m
2143d 75 73 74 42 65 55 6e 69 71 75 65 20 7c 7c 20 28  ustBeUnique || (
2143e 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31  pIdx->nColumn==1
2143f 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f   && pIdx->onErro
21440 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20 20  r!=OE_None)).   
21441 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
21442 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70    int iMem = ++p
21443 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
21444 20 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 72         int iAddr
21445 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72  ;.          char
21446 20 2a 70 4b 65 79 3b 0a 20 20 0a 20 20 20 20 20   *pKey;.  .     
21447 20 20 20 20 20 70 4b 65 79 20 3d 20 28 63 68 61       pKey = (cha
21448 72 20 2a 29 73 71 6c 69 74 65 33 49 6e 64 65 78  r *)sqlite3Index
21449 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20  Keyinfo(pParse, 
2144a 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 20  pIdx);.         
2144b 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33   iAddr = sqlite3
2144c 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2144d 5f 49 66 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20  _If, iMem);.    
2144e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2144f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
21450 74 65 67 65 72 2c 20 31 2c 20 69 4d 65 6d 29 3b  teger, 1, iMem);
21451 0a 20 20 0a 20 20 20 20 20 20 20 20 20 20 73 71  .  .          sq
21452 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
21453 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20  v, OP_OpenRead, 
21454 69 54 61 62 2c 20 70 49 64 78 2d 3e 74 6e 75 6d  iTab, pIdx->tnum
21455 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20  , iDb,.         
21456 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21457 20 20 20 20 20 20 70 4b 65 79 2c 50 34 5f 4b 45        pKey,P4_KE
21458 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
21459 20 20 20 20 20 20 20 20 20 20 56 64 62 65 43 6f            VdbeCo
2145a 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20  mment((v, "%s", 
2145b 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20  pIdx->zName));. 
2145c 20 20 20 20 20 20 20 20 20 65 54 79 70 65 20 3d           eType =
2145d 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 3b   IN_INDEX_INDEX;
2145e 0a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ..          sqli
2145f 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
21460 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 20 20  v, iAddr);.     
21461 20 20 20 20 20 69 66 28 20 70 72 4e 6f 74 46 6f       if( prNotFo
21462 75 6e 64 20 26 26 20 21 70 54 61 62 2d 3e 61 43  und && !pTab->aC
21463 6f 6c 5b 69 43 6f 6c 5d 2e 6e 6f 74 4e 75 6c 6c  ol[iCol].notNull
21464 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
21465 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b  *prNotFound = ++
21466 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
21467 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
21468 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
21469 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 65 54 79  }.  }..  if( eTy
2146a 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  pe==0 ){.    /* 
2146b 43 6f 75 6c 64 20 6e 6f 74 20 66 6f 75 6e 64 20  Could not found 
2146c 61 6e 20 65 78 69 73 74 69 6e 67 20 74 61 62 6c  an existing tabl
2146d 65 20 6f 72 20 69 6e 64 65 78 20 74 6f 20 75 73  e or index to us
2146e 65 20 61 73 20 74 68 65 20 52 48 53 20 62 2d 74  e as the RHS b-t
2146f 72 65 65 2e 0a 20 20 20 20 2a 2a 20 57 65 20 77  ree..    ** We w
21470 69 6c 6c 20 68 61 76 65 20 74 6f 20 67 65 6e 65  ill have to gene
21471 72 61 74 65 20 61 6e 20 65 70 68 65 6d 65 72 61  rate an ephemera
21472 6c 20 74 61 62 6c 65 20 74 6f 20 64 6f 20 74 68  l table to do th
21473 65 20 6a 6f 62 2e 0a 20 20 20 20 2a 2f 0a 20 20  e job..    */.  
21474 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75    int rMayHaveNu
21475 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 65 54 79 70  ll = 0;.    eTyp
21476 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48  e = IN_INDEX_EPH
21477 3b 0a 20 20 20 20 69 66 28 20 70 72 4e 6f 74 46  ;.    if( prNotF
21478 6f 75 6e 64 20 29 7b 0a 20 20 20 20 20 20 2a 70  ound ){.      *p
21479 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 72 4d 61 79  rNotFound = rMay
2147a 48 61 76 65 4e 75 6c 6c 20 3d 20 2b 2b 70 50 61  HaveNull = ++pPa
2147b 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d  rse->nMem;.    }
2147c 65 6c 73 65 20 69 66 28 20 70 58 2d 3e 70 4c 65  else if( pX->pLe
2147d 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3c 30 20 26 26  ft->iColumn<0 &&
2147e 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70   !ExprHasAnyProp
2147f 65 72 74 79 28 70 58 2c 20 45 50 5f 78 49 73 53  erty(pX, EP_xIsS
21480 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20  elect) ){.      
21481 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58  eType = IN_INDEX
21482 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20  _ROWID;.    }.  
21483 20 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62    sqlite3CodeSub
21484 73 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  select(pParse, p
21485 58 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c  X, rMayHaveNull,
21486 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58   eType==IN_INDEX
21487 5f 52 4f 57 49 44 29 3b 0a 20 20 7d 65 6c 73 65  _ROWID);.  }else
21488 7b 0a 20 20 20 20 70 58 2d 3e 69 54 61 62 6c 65  {.    pX->iTable
21489 20 3d 20 69 54 61 62 3b 0a 20 20 7d 0a 20 20 72   = iTab;.  }.  r
2148a 65 74 75 72 6e 20 65 54 79 70 65 3b 0a 7d 0a 23  eturn eType;.}.#
2148b 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  endif../*.** Gen
2148c 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 73  erate code for s
2148d 63 61 6c 61 72 20 73 75 62 71 75 65 72 69 65 73  calar subqueries
2148e 20 75 73 65 64 20 61 73 20 61 6e 20 65 78 70 72   used as an expr
2148f 65 73 73 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e  ession.** and IN
21490 20 6f 70 65 72 61 74 6f 72 73 2e 20 20 45 78 61   operators.  Exa
21491 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  mples:.**.**    
21492 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f 4d 20   (SELECT a FROM 
21493 62 29 20 20 20 20 20 20 20 20 20 20 2d 2d 20 73  b)          -- s
21494 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20 20 45  ubquery.**     E
21495 58 49 53 54 53 20 28 53 45 4c 45 43 54 20 61 20  XISTS (SELECT a 
21496 46 52 4f 4d 20 62 29 20 20 20 2d 2d 20 45 58 49  FROM b)   -- EXI
21497 53 54 53 20 73 75 62 71 75 65 72 79 0a 2a 2a 20  STS subquery.** 
21498 20 20 20 20 78 20 49 4e 20 28 34 2c 35 2c 31 31      x IN (4,5,11
21499 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d  )              -
2149a 2d 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69  - IN operator wi
2149b 74 68 20 6c 69 73 74 20 6f 6e 20 72 69 67 68 74  th list on right
2149c 2d 68 61 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20  -hand side.**   
2149d 20 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 61    x IN (SELECT a
2149e 20 46 52 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20   FROM b)     -- 
2149f 49 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68  IN operator with
214a0 20 73 75 62 71 75 65 72 79 20 6f 6e 20 74 68 65   subquery on the
214a1 20 72 69 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65   right.**.** The
214a2 20 70 45 78 70 72 20 70 61 72 61 6d 65 74 65 72   pExpr parameter
214a3 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65   describes the e
214a4 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63  xpression that c
214a5 6f 6e 74 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a  ontains the IN.*
214a6 2a 20 6f 70 65 72 61 74 6f 72 20 6f 72 20 73 75  * operator or su
214a7 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66  bquery..**.** If
214a8 20 70 61 72 61 6d 65 74 65 72 20 69 73 52 6f 77   parameter isRow
214a9 69 64 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  id is non-zero, 
214aa 74 68 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  then expression 
214ab 70 45 78 70 72 20 69 73 20 67 75 61 72 61 6e 74  pExpr is guarant
214ac 65 65 64 0a 2a 2a 20 74 6f 20 62 65 20 6f 66 20  eed.** to be of 
214ad 74 68 65 20 66 6f 72 6d 20 22 3c 72 6f 77 69 64  the form "<rowid
214ae 3e 20 49 4e 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c  > IN (?, ?, ?)",
214af 20 77 68 65 72 65 20 3c 72 6f 77 69 64 3e 20 69   where <rowid> i
214b0 73 20 61 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a  s a reference.**
214b1 20 74 6f 20 73 6f 6d 65 20 69 6e 74 65 67 65 72   to some integer
214b2 20 6b 65 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 61   key column of a
214b3 20 74 61 62 6c 65 20 42 2d 54 72 65 65 2e 20 49   table B-Tree. I
214b4 6e 20 74 68 69 73 20 63 61 73 65 2c 20 75 73 65  n this case, use
214b5 20 61 6e 0a 2a 2a 20 69 6e 74 6b 65 79 20 42 2d   an.** intkey B-
214b6 54 72 65 65 20 74 6f 20 73 74 6f 72 65 20 74 68  Tree to store th
214b7 65 20 73 65 74 20 6f 66 20 49 4e 28 2e 2e 2e 29  e set of IN(...)
214b8 20 76 61 6c 75 65 73 20 69 6e 73 74 65 61 64 20   values instead 
214b9 6f 66 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20  of the usual.** 
214ba 28 73 6c 6f 77 65 72 29 20 76 61 72 69 61 62 6c  (slower) variabl
214bb 65 20 6c 65 6e 67 74 68 20 6b 65 79 73 20 42 2d  e length keys B-
214bc 54 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72  Tree..**.** If r
214bd 4d 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 6e  MayHaveNull is n
214be 6f 6e 2d 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65  on-zero, that me
214bf 61 6e 73 20 74 68 61 74 20 74 68 65 20 6f 70 65  ans that the ope
214c0 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 0a  ration is an IN.
214c1 2a 2a 20 28 6e 6f 74 20 61 20 53 45 4c 45 43 54  ** (not a SELECT
214c2 20 6f 72 20 45 58 49 53 54 53 29 20 61 6e 64 20   or EXISTS) and 
214c3 74 68 61 74 20 74 68 65 20 52 48 53 20 6d 69 67  that the RHS mig
214c4 68 74 20 63 6f 6e 74 61 69 6e 73 20 4e 55 4c 4c  ht contains NULL
214c5 73 2e 0a 2a 2a 20 46 75 72 74 68 65 72 6d 6f 72  s..** Furthermor
214c6 65 2c 20 74 68 65 20 49 4e 20 69 73 20 69 6e 20  e, the IN is in 
214c7 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 61  a WHERE clause a
214c8 6e 64 20 74 68 61 74 20 77 65 20 72 65 61 6c 6c  nd that we reall
214c9 79 20 77 61 6e 74 0a 2a 2a 20 74 6f 20 69 74 65  y want.** to ite
214ca 72 61 74 65 20 6f 76 65 72 20 74 68 65 20 52 48  rate over the RH
214cb 53 20 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72  S of the IN oper
214cc 61 74 6f 72 20 69 6e 20 6f 72 64 65 72 20 74 6f  ator in order to
214cd 20 71 75 69 63 6b 6c 79 20 6c 6f 63 61 74 65 0a   quickly locate.
214ce 2a 2a 20 61 6c 6c 20 63 6f 72 72 65 73 70 6f 6e  ** all correspon
214cf 64 69 6e 67 20 4c 48 53 20 65 6c 65 6d 65 6e 74  ding LHS element
214d0 73 2e 20 20 41 6c 6c 20 74 68 69 73 20 72 6f 75  s.  All this rou
214d1 74 69 6e 65 20 64 6f 65 73 20 69 73 20 69 6e 69  tine does is ini
214d2 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 72  tialize.** the r
214d3 65 67 69 73 74 65 72 20 67 69 76 65 6e 20 62 79  egister given by
214d4 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 74 6f   rMayHaveNull to
214d5 20 4e 55 4c 4c 2e 20 20 43 61 6c 6c 69 6e 67 20   NULL.  Calling 
214d6 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c 20 74 61  routines will ta
214d7 6b 65 0a 2a 2a 20 63 61 72 65 20 6f 66 20 63 68  ke.** care of ch
214d8 61 6e 67 69 6e 67 20 74 68 69 73 20 72 65 67 69  anging this regi
214d9 73 74 65 72 20 76 61 6c 75 65 20 74 6f 20 6e 6f  ster value to no
214da 6e 2d 4e 55 4c 4c 20 69 66 20 74 68 65 20 52 48  n-NULL if the RH
214db 53 20 69 73 20 4e 55 4c 4c 2d 66 72 65 65 2e 0a  S is NULL-free..
214dc 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 79 48 61 76  **.** If rMayHav
214dd 65 4e 75 6c 6c 20 69 73 20 7a 65 72 6f 2c 20 74  eNull is zero, t
214de 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74  hat means that t
214df 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 62  he subquery is b
214e0 65 69 6e 67 20 75 73 65 64 0a 2a 2a 20 66 6f 72  eing used.** for
214e1 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73 74   membership test
214e2 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68 65 72 65  ing only.  There
214e3 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69   is no need to i
214e4 6e 69 74 69 61 6c 69 7a 65 20 61 6e 79 0a 2a 2a  nitialize any.**
214e5 20 72 65 67 69 73 74 65 72 73 20 74 6f 20 69 6e   registers to in
214e6 64 69 63 61 74 65 20 74 68 65 20 70 72 65 73 65  dicate the prese
214e7 6e 73 65 20 6f 72 20 61 62 73 65 6e 63 65 20 6f  nse or absence o
214e8 66 20 4e 55 4c 4c 73 20 6f 6e 20 74 68 65 20 52  f NULLs on the R
214e9 48 53 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20  HS..**.** For a 
214ea 53 45 4c 45 43 54 20 6f 72 20 45 58 49 53 54 53  SELECT or EXISTS
214eb 20 6f 70 65 72 61 74 6f 72 2c 20 72 65 74 75 72   operator, retur
214ec 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20 74  n the register t
214ed 68 61 74 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a  hat holds the.**
214ee 20 72 65 73 75 6c 74 2e 20 20 46 6f 72 20 49 4e   result.  For IN
214ef 20 6f 70 65 72 61 74 6f 72 73 20 6f 72 20 69 66   operators or if
214f0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
214f1 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  , the return val
214f2 75 65 20 69 73 20 30 2e 0a 2a 2f 0a 23 69 66 6e  ue is 0..*/.#ifn
214f3 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
214f4 53 55 42 51 55 45 52 59 0a 53 51 4c 49 54 45 5f  SUBQUERY.SQLITE_
214f5 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
214f6 74 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74  te3CodeSubselect
214f7 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
214f8 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e,          /* P
214f9 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
214fa 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c  /.  Expr *pExpr,
214fb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
214fc 68 65 20 49 4e 2c 20 53 45 4c 45 43 54 2c 20 6f  he IN, SELECT, o
214fd 72 20 45 58 49 53 54 53 20 6f 70 65 72 61 74 6f  r EXISTS operato
214fe 72 20 2a 2f 0a 20 20 69 6e 74 20 72 4d 61 79 48  r */.  int rMayH
214ff 61 76 65 4e 75 6c 6c 2c 20 20 20 20 20 20 20 2f  aveNull,       /
21500 2a 20 52 65 67 69 73 74 65 72 20 74 68 61 74 20  * Register that 
21501 72 65 63 6f 72 64 73 20 77 68 65 74 68 65 72 20  records whether 
21502 4e 55 4c 4c 73 20 65 78 69 73 74 20 69 6e 20 52  NULLs exist in R
21503 48 53 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f  HS */.  int isRo
21504 77 69 64 20 20 20 20 20 20 20 20 20 20 20 20 20  wid             
21505 2f 2a 20 49 66 20 74 72 75 65 2c 20 4c 48 53 20  /* If true, LHS 
21506 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 69  of IN operator i
21507 73 20 61 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a  s a rowid */.){.
21508 20 20 69 6e 74 20 74 65 73 74 41 64 64 72 20 3d    int testAddr =
21509 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2150a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
2150b 2d 74 69 6d 65 20 74 65 73 74 20 61 64 64 72 65  -time test addre
2150c 73 73 20 2a 2f 0a 20 20 69 6e 74 20 72 52 65 67  ss */.  int rReg
2150d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2150e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2150f 2f 2a 20 52 65 67 69 73 74 65 72 20 73 74 6f 72  /* Register stor
21510 69 6e 67 20 72 65 73 75 6c 74 69 6e 67 20 2a 2f  ing resulting */
21511 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c  .  Vdbe *v = sql
21512 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
21513 73 65 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  se);.  if( NEVER
21514 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20  (v==0) ) return 
21515 30 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  0;.  sqlite3Expr
21516 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65  CachePush(pParse
21517 29 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f  );..  /* This co
21518 64 65 20 6d 75 73 74 20 62 65 20 72 75 6e 20 69  de must be run i
21519 6e 20 69 74 73 20 65 6e 74 69 72 65 74 79 20 65  n its entirety e
2151a 76 65 72 79 20 74 69 6d 65 20 69 74 20 69 73 20  very time it is 
2151b 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20 2a 2a  encountered.  **
2151c 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66   if any of the f
2151d 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75 65  ollowing is true
2151e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a  :.  **.  **    *
2151f 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e 64    The right-hand
21520 20 73 69 64 65 20 69 73 20 61 20 63 6f 72 72 65   side is a corre
21521 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 0a 20  lated subquery. 
21522 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72 69   **    *  The ri
21523 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73  ght-hand side is
21524 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c   an expression l
21525 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76  ist containing v
21526 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20 20 20  ariables.  **   
21527 20 2a 20 20 57 65 20 61 72 65 20 69 6e 73 69 64   *  We are insid
21528 65 20 61 20 74 72 69 67 67 65 72 0a 20 20 2a 2a  e a trigger.  **
21529 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66 20  .  ** If all of 
2152a 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 66 61  the above are fa
2152b 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63 61 6e  lse, then we can
2152c 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 6a   run this code j
2152d 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 73 61  ust once.  ** sa
2152e 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 2c 20  ve the results, 
2152f 61 6e 64 20 72 65 75 73 65 20 74 68 65 20 73 61  and reuse the sa
21530 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73 75 62  me result on sub
21531 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69  sequent invocati
21532 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ons..  */.  if( 
21533 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
21534 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56 61  rty(pExpr, EP_Va
21535 72 53 65 6c 65 63 74 29 20 26 26 20 21 70 50 61  rSelect) && !pPa
21536 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
21537 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20   ){.    int mem 
21538 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
21539 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2153a 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
2153b 2c 20 6d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74  , mem);.    test
2153c 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  Addr = sqlite3Vd
2153d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2153e 6e 74 65 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b  nteger, 1, mem);
2153f 0a 20 20 20 20 61 73 73 65 72 74 28 20 74 65 73  .    assert( tes
21540 74 41 64 64 72 3e 30 20 7c 7c 20 70 50 61 72 73  tAddr>0 || pPars
21541 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
21542 6c 65 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77  led );.  }..  sw
21543 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20  itch( pExpr->op 
21544 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49  ){.    case TK_I
21545 4e 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20  N: {.      char 
21546 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20  affinity;.      
21547 4b 65 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b  KeyInfo keyInfo;
21548 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b  .      int addr;
21549 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2154a 73 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68  ss of OP_OpenEph
2154b 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69  emeral instructi
2154c 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  on */.      Expr
2154d 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d   *pLeft = pExpr-
2154e 3e 70 4c 65 66 74 3b 0a 0a 20 20 20 20 20 20 69  >pLeft;..      i
2154f 66 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20  f( rMayHaveNull 
21550 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
21551 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21552 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79  OP_Null, 0, rMay
21553 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  HaveNull);.     
21554 20 7d 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69   }..      affini
21555 74 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ty = sqlite3Expr
21556 41 66 66 69 6e 69 74 79 28 70 4c 65 66 74 29 3b  Affinity(pLeft);
21557 0a 0a 20 20 20 20 20 20 2f 2a 20 57 68 65 74 68  ..      /* Wheth
21558 65 72 20 74 68 69 73 20 69 73 20 61 6e 20 27 78  er this is an 'x
21559 20 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20   IN(SELECT...)' 
2155a 6f 72 20 61 6e 20 27 78 20 49 4e 28 3c 65 78 70  or an 'x IN(<exp
2155b 72 6c 69 73 74 3e 29 27 0a 20 20 20 20 20 20 2a  rlist>)'.      *
2155c 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 74 20  * expression it 
2155d 69 73 20 68 61 6e 64 6c 65 64 20 74 68 65 20 73  is handled the s
2155e 61 6d 65 20 77 61 79 2e 20 20 41 6e 20 65 70 68  ame way.  An eph
2155f 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 69 73 20  emeral table is 
21560 0a 20 20 20 20 20 20 2a 2a 20 66 69 6c 6c 65 64  .      ** filled
21561 20 77 69 74 68 20 73 69 6e 67 6c 65 2d 66 69 65   with single-fie
21562 6c 64 20 69 6e 64 65 78 20 6b 65 79 73 20 72 65  ld index keys re
21563 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 72  presenting the r
21564 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20  esults.      ** 
21565 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54 20  from the SELECT 
21566 6f 72 20 74 68 65 20 3c 65 78 70 72 6c 69 73 74  or the <exprlist
21567 3e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  >..      **.    
21568 20 20 2a 2a 20 49 66 20 74 68 65 20 27 78 27 20    ** If the 'x' 
21569 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
2156a 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 2c 20 6f 72  column value, or
2156b 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 0a 20   the SELECT.... 
2156c 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e       ** statemen
2156d 74 20 72 65 74 75 72 6e 73 20 61 20 63 6f 6c 75  t returns a colu
2156e 6d 6e 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74  mn value, then t
2156f 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74  he affinity of t
21570 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6c  hat.      ** col
21571 75 6d 6e 20 69 73 20 75 73 65 64 20 74 6f 20 62  umn is used to b
21572 75 69 6c 64 20 74 68 65 20 69 6e 64 65 78 20 6b  uild the index k
21573 65 79 73 2e 20 49 66 20 62 6f 74 68 20 27 78 27  eys. If both 'x'
21574 20 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 2a   and the.      *
21575 2a 20 53 45 4c 45 43 54 2e 2e 2e 20 73 74 61 74  * SELECT... stat
21576 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c 75 6d 6e  ement are column
21577 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 20  s, then numeric 
21578 61 66 66 69 6e 69 74 79 20 69 73 20 75 73 65 64  affinity is used
21579 0a 20 20 20 20 20 20 2a 2a 20 69 66 20 65 69 74  .      ** if eit
2157a 68 65 72 20 63 6f 6c 75 6d 6e 20 68 61 73 20 4e  her column has N
2157b 55 4d 45 52 49 43 20 6f 72 20 49 4e 54 45 47 45  UMERIC or INTEGE
2157c 52 20 61 66 66 69 6e 69 74 79 2e 20 49 66 20 6e  R affinity. If n
2157d 65 69 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20  either.      ** 
2157e 27 78 27 20 6e 6f 72 20 74 68 65 20 53 45 4c 45  'x' nor the SELE
2157f 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20  CT... statement 
21580 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65  are columns, the
21581 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69  n numeric affini
21582 74 79 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 75  ty.      ** is u
21583 73 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  sed..      */.  
21584 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
21585 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  e = pParse->nTab
21586 2b 2b 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d  ++;.      addr =
21587 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21588 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p2(v, OP_OpenEph
21589 65 6d 65 72 61 6c 2c 20 70 45 78 70 72 2d 3e 69  emeral, pExpr->i
2158a 54 61 62 6c 65 2c 20 21 69 73 52 6f 77 69 64 29  Table, !isRowid)
2158b 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26  ;.      memset(&
2158c 6b 65 79 49 6e 66 6f 2c 20 30 2c 20 73 69 7a 65  keyInfo, 0, size
2158d 6f 66 28 6b 65 79 49 6e 66 6f 29 29 3b 0a 20 20  of(keyInfo));.  
2158e 20 20 20 20 6b 65 79 49 6e 66 6f 2e 6e 46 69 65      keyInfo.nFie
2158f 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 69  ld = 1;..      i
21590 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
21591 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
21592 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20  Select) ){.     
21593 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20 20 20     /* Case 1:   
21594 20 20 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43    expr IN (SELEC
21595 54 20 2e 2e 2e 29 0a 20 20 20 20 20 20 20 20 2a  T ...).        *
21596 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 47 65 6e  *.        ** Gen
21597 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72  erate code to wr
21598 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ite the results 
21599 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 69 6e  of the select in
2159a 74 6f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  to the temporary
2159b 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c  .        ** tabl
2159c 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  e allocated and 
2159d 6f 70 65 6e 65 64 20 61 62 6f 76 65 2e 0a 20 20  opened above..  
2159e 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2159f 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
215a0 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69  ;.        ExprLi
215a1 73 74 20 2a 70 45 4c 69 73 74 3b 0a 0a 20 20 20  st *pEList;..   
215a2 20 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73       assert( !is
215a3 52 6f 77 69 64 20 29 3b 0a 20 20 20 20 20 20 20  Rowid );.       
215a4 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
215a5 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52  stInit(&dest, SR
215a6 54 5f 53 65 74 2c 20 70 45 78 70 72 2d 3e 69 54  T_Set, pExpr->iT
215a7 61 62 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 64  able);.        d
215a8 65 73 74 2e 61 66 66 69 6e 69 74 79 20 3d 20 28  est.affinity = (
215a9 75 38 29 61 66 66 69 6e 69 74 79 3b 0a 20 20 20  u8)affinity;.   
215aa 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 45       assert( (pE
215ab 78 70 72 2d 3e 69 54 61 62 6c 65 26 30 78 30 30  xpr->iTable&0x00
215ac 30 30 46 46 46 46 29 3d 3d 70 45 78 70 72 2d 3e  00FFFF)==pExpr->
215ad 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20  iTable );.      
215ae 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 65 6c    if( sqlite3Sel
215af 65 63 74 28 70 50 61 72 73 65 2c 20 70 45 78 70  ect(pParse, pExp
215b0 72 2d 3e 78 2e 70 53 65 6c 65 63 74 2c 20 26 64  r->x.pSelect, &d
215b1 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  est) ){.        
215b2 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
215b3 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 45      }.        pE
215b4 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e  List = pExpr->x.
215b5 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
215b6 0a 20 20 20 20 20 20 20 20 69 66 28 20 41 4c 57  .        if( ALW
215b7 41 59 53 28 70 45 4c 69 73 74 21 3d 30 20 26 26  AYS(pEList!=0 &&
215b8 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 30   pEList->nExpr>0
215b9 29 20 29 7b 20 0a 20 20 20 20 20 20 20 20 20 20  ) ){ .          
215ba 6b 65 79 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d  keyInfo.aColl[0]
215bb 20 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79   = sqlite3Binary
215bc 43 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70  CompareCollSeq(p
215bd 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
215be 65 66 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  eft,.           
215bf 20 20 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e     pEList->a[0].
215c0 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20  pExpr);.        
215c1 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  }.      }else if
215c2 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  ( pExpr->x.pList
215c3 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  !=0 ){.        /
215c4 2a 20 43 61 73 65 20 32 3a 20 20 20 20 20 65 78  * Case 2:     ex
215c5 70 72 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29  pr IN (exprlist)
215c6 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
215c7 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20      ** For each 
215c8 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c  expression, buil
215c9 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66  d an index key f
215ca 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69  rom the evaluati
215cb 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a  on and.        *
215cc 2a 20 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68  * store it in th
215cd 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
215ce 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20  e. If <expr> is 
215cf 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75  a column, then u
215d0 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  se.        ** th
215d1 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e  at columns affin
215d2 69 74 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e  ity when buildin
215d3 67 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66  g index keys. If
215d4 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20   <expr> is not. 
215d5 20 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75         ** a colu
215d6 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20  mn, use numeric 
215d7 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20  affinity..      
215d8 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
215d9 20 69 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72   i;.        Expr
215da 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45  List *pList = pE
215db 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20  xpr->x.pList;.  
215dc 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70        struct Exp
215dd 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
215de 6d 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  m;.        int r
215df 31 2c 20 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20  1, r2, r3;..    
215e0 20 20 20 20 69 66 28 20 21 61 66 66 69 6e 69 74      if( !affinit
215e1 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  y ){.          a
215e2 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45  ffinity = SQLITE
215e3 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20  _AFF_NONE;.     
215e4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6b 65 79     }.        key
215e5 49 6e 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20  Info.aColl[0] = 
215e6 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
215e7 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  eq(pParse, pExpr
215e8 2d 3e 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20  ->pLeft);..     
215e9 20 20 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75     /* Loop throu
215ea 67 68 20 65 61 63 68 20 65 78 70 72 65 73 73 69  gh each expressi
215eb 6f 6e 20 69 6e 20 3c 65 78 70 72 6c 69 73 74 3e  on in <exprlist>
215ec 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 31 20  . */.        r1 
215ed 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
215ee 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
215ef 20 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65       r2 = sqlite
215f0 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
215f1 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  se);.        sql
215f2 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
215f3 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 32  , OP_Null, 0, r2
215f4 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  );.        for(i
215f5 3d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70  =pList->nExpr, p
215f6 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69  Item=pList->a; i
215f7 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b  >0; i--, pItem++
215f8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70  ){.          Exp
215f9 72 20 2a 70 45 32 20 3d 20 70 49 74 65 6d 2d 3e  r *pE2 = pItem->
215fa 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20  pExpr;.         
215fb 20 69 6e 74 20 69 56 61 6c 54 6f 49 6e 73 3b 0a   int iValToIns;.
215fc 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66  .          /* If
215fd 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
215fe 69 73 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20  is not constant 
215ff 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 6e 65 65  then we will nee
21600 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 2a  d to.          *
21601 2a 20 64 69 73 61 62 6c 65 20 74 68 65 20 74 65  * disable the te
21602 73 74 20 74 68 61 74 20 77 61 73 20 67 65 6e 65  st that was gene
21603 72 61 74 65 64 20 61 62 6f 76 65 20 74 68 61 74  rated above that
21604 20 6d 61 6b 65 73 20 73 75 72 65 0a 20 20 20 20   makes sure.    
21605 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 63 6f        ** this co
21606 64 65 20 6f 6e 6c 79 20 65 78 65 63 75 74 65 73  de only executes
21607 20 6f 6e 63 65 2e 20 20 42 65 63 61 75 73 65 20   once.  Because 
21608 66 6f 72 20 61 20 6e 6f 6e 2d 63 6f 6e 73 74 61  for a non-consta
21609 6e 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  nt.          ** 
2160a 65 78 70 72 65 73 73 69 6f 6e 20 77 65 20 6e 65  expression we ne
2160b 65 64 20 74 6f 20 72 65 72 75 6e 20 74 68 69 73  ed to rerun this
2160c 20 63 6f 64 65 20 65 61 63 68 20 74 69 6d 65 2e   code each time.
2160d 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
2160e 20 20 20 20 20 20 20 20 69 66 28 20 74 65 73 74          if( test
2160f 41 64 64 72 20 26 26 20 21 73 71 6c 69 74 65 33  Addr && !sqlite3
21610 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70  ExprIsConstant(p
21611 45 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  E2) ){.         
21612 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
21613 61 6e 67 65 54 6f 4e 6f 6f 70 28 76 2c 20 74 65  angeToNoop(v, te
21614 73 74 41 64 64 72 2d 31 2c 20 32 29 3b 0a 20 20  stAddr-1, 2);.  
21615 20 20 20 20 20 20 20 20 20 20 74 65 73 74 41 64            testAd
21616 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  dr = 0;.        
21617 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f    }..          /
21618 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65 20 65  * Evaluate the e
21619 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 69 6e  xpression and in
2161a 73 65 72 74 20 69 74 20 69 6e 74 6f 20 74 68 65  sert it into the
2161b 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20   temp table */. 
2161c 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73 52           if( isR
2161d 6f 77 69 64 20 26 26 20 73 71 6c 69 74 65 33 45  owid && sqlite3E
2161e 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 32  xprIsInteger(pE2
2161f 2c 20 26 69 56 61 6c 54 6f 49 6e 73 29 20 29 7b  , &iValToIns) ){
21620 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
21621 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
21622 2c 20 4f 50 5f 49 6e 73 65 72 74 49 6e 74 2c 20  , OP_InsertInt, 
21623 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72  pExpr->iTable, r
21624 32 2c 20 69 56 61 6c 54 6f 49 6e 73 29 3b 0a 20  2, iValToIns);. 
21625 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
21626 20 20 20 20 20 20 20 20 20 20 20 20 72 33 20 3d              r3 =
21627 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
21628 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70  Target(pParse, p
21629 45 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20  E2, r1);.       
2162a 20 20 20 20 20 69 66 28 20 69 73 52 6f 77 69 64       if( isRowid
2162b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2162c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2162d 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65  Op2(v, OP_MustBe
2162e 49 6e 74 2c 20 72 33 2c 0a 20 20 20 20 20 20 20  Int, r3,.       
2162f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21630 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
21631 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
21632 76 29 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20  v)+2);.         
21633 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21634 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
21635 65 72 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62  ert, pExpr->iTab
21636 6c 65 2c 20 72 32 2c 20 72 33 29 3b 0a 20 20 20  le, r2, r3);.   
21637 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
21638 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
21639 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2163a 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
2163b 2c 20 72 33 2c 20 31 2c 20 72 32 2c 20 26 61 66  , r3, 1, r2, &af
2163c 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20  finity, 1);.    
2163d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2163e 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
2163f 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
21640 20 72 33 2c 20 31 29 3b 0a 20 20 20 20 20 20 20   r3, 1);.       
21641 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21642 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
21643 64 78 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d  dxInsert, pExpr-
21644 3e 69 54 61 62 6c 65 2c 20 72 32 29 3b 0a 20 20  >iTable, r2);.  
21645 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
21646 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
21647 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
21648 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
21649 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
2164a 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
2164b 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2164c 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a  e, r2);.      }.
2164d 20 20 20 20 20 20 69 66 28 20 21 69 73 52 6f 77        if( !isRow
2164e 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  id ){.        sq
2164f 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
21650 34 28 76 2c 20 61 64 64 72 2c 20 28 76 6f 69 64  4(v, addr, (void
21651 20 2a 29 26 6b 65 79 49 6e 66 6f 2c 20 50 34 5f   *)&keyInfo, P4_
21652 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20  KEYINFO);.      
21653 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
21654 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 54     }..    case T
21655 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61  K_EXISTS:.    ca
21656 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20  se TK_SELECT:.  
21657 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
21658 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 68 61     /* If this ha
21659 73 20 74 6f 20 62 65 20 61 20 73 63 61 6c 61 72  s to be a scalar
2165a 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65 72 61   SELECT.  Genera
2165b 74 65 20 63 6f 64 65 20 74 6f 20 70 75 74 20 74  te code to put t
2165c 68 65 0a 20 20 20 20 20 20 2a 2a 20 76 61 6c 75  he.      ** valu
2165d 65 20 6f 66 20 74 68 69 73 20 73 65 6c 65 63 74  e of this select
2165e 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c   in a memory cel
2165f 6c 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68 65  l and record the
21660 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 2a 2a   number.      **
21661 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63   of the memory c
21662 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d 6e 2e 20  ell in iColumn. 
21663 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 45   If this is an E
21664 58 49 53 54 53 2c 20 77 72 69 74 65 0a 20 20 20  XISTS, write.   
21665 20 20 20 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72     ** an integer
21666 20 30 20 28 6e 6f 74 20 65 78 69 73 74 73 29 20   0 (not exists) 
21667 6f 72 20 31 20 28 65 78 69 73 74 73 29 20 69 6e  or 1 (exists) in
21668 74 6f 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  to a memory cell
21669 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65  .      ** and re
2166a 63 6f 72 64 20 74 68 61 74 20 6d 65 6d 6f 72 79  cord that memory
2166b 20 63 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d 6e   cell in iColumn
2166c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2166d 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 54 6f   static const To
2166e 6b 65 6e 20 6f 6e 65 20 3d 20 7b 20 22 31 22 2c  ken one = { "1",
2166f 20 31 20 7d 3b 20 20 2f 2a 20 54 6f 6b 65 6e 20   1 };  /* Token 
21670 66 6f 72 20 6c 69 74 65 72 61 6c 20 76 61 6c 75  for literal valu
21671 65 20 31 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c  e 1 */.      Sel
21672 65 63 74 20 2a 70 53 65 6c 3b 20 20 20 20 20 20  ect *pSel;      
21673 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21674 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61     /* SELECT sta
21675 74 65 6d 65 6e 74 20 74 6f 20 65 6e 63 6f 64 65  tement to encode
21676 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74   */.      Select
21677 44 65 73 74 20 64 65 73 74 3b 20 20 20 20 20 20  Dest dest;      
21678 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21679 2f 2a 20 48 6f 77 20 74 6f 20 64 65 61 6c 20 77  /* How to deal w
2167a 69 74 68 20 53 45 4c 45 43 74 20 72 65 73 75 6c  ith SELECt resul
2167b 74 20 2a 2f 0a 0a 20 20 20 20 20 20 74 65 73 74  t */..      test
2167c 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
2167d 3d 54 4b 5f 45 58 49 53 54 53 20 29 3b 0a 20 20  =TK_EXISTS );.  
2167e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
2167f 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45  xpr->op==TK_SELE
21680 43 54 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  CT );.      asse
21681 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  rt( pExpr->op==T
21682 4b 5f 45 58 49 53 54 53 20 7c 7c 20 70 45 78 70  K_EXISTS || pExp
21683 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54  r->op==TK_SELECT
21684 20 29 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72   );..      asser
21685 74 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  t( ExprHasProper
21686 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73  ty(pExpr, EP_xIs
21687 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20  Select) );.     
21688 20 70 53 65 6c 20 3d 20 70 45 78 70 72 2d 3e 78   pSel = pExpr->x
21689 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20  .pSelect;.      
2168a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
2168b 74 49 6e 69 74 28 26 64 65 73 74 2c 20 30 2c 20  tInit(&dest, 0, 
2168c 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b  ++pParse->nMem);
2168d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72  .      if( pExpr
2168e 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20  ->op==TK_SELECT 
2168f 29 7b 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e  ){.        dest.
21690 65 44 65 73 74 20 3d 20 53 52 54 5f 4d 65 6d 3b  eDest = SRT_Mem;
21691 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21692 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21693 5f 4e 75 6c 6c 2c 20 30 2c 20 64 65 73 74 2e 69  _Null, 0, dest.i
21694 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20 20 56  Parm);.        V
21695 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
21696 49 6e 69 74 20 73 75 62 71 75 65 72 79 20 72 65  Init subquery re
21697 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d  sult"));.      }
21698 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64 65  else{.        de
21699 73 74 2e 65 44 65 73 74 20 3d 20 53 52 54 5f 45  st.eDest = SRT_E
2169a 78 69 73 74 73 3b 0a 20 20 20 20 20 20 20 20 73  xists;.        s
2169b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2169c 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2169d 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a  0, dest.iParm);.
2169e 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d          VdbeComm
2169f 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 45 58  ent((v, "Init EX
216a0 49 53 54 53 20 72 65 73 75 6c 74 22 29 29 3b 0a  ISTS result"));.
216a1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
216a2 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
216a3 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c  pParse->db, pSel
216a4 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20  ->pLimit);.     
216a5 20 70 53 65 6c 2d 3e 70 4c 69 6d 69 74 20 3d 20   pSel->pLimit = 
216a6 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
216a7 72 73 65 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c  rse, TK_INTEGER,
216a8 20 30 2c 20 30 2c 20 26 6f 6e 65 29 3b 0a 20 20   0, 0, &one);.  
216a9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
216aa 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53  elect(pParse, pS
216ab 65 6c 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20  el, &dest) ){.  
216ac 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
216ad 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 52        }.      rR
216ae 65 67 20 3d 20 64 65 73 74 2e 69 50 61 72 6d 3b  eg = dest.iParm;
216af 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 49 72  .      ExprSetIr
216b0 72 65 64 75 63 69 62 6c 65 28 70 45 78 70 72 29  reducible(pExpr)
216b1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
216b2 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
216b3 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20 20 20  testAddr ){.    
216b4 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
216b5 65 72 65 28 76 2c 20 74 65 73 74 41 64 64 72 2d  ere(v, testAddr-
216b6 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  1);.  }.  sqlite
216b7 33 45 78 70 72 43 61 63 68 65 50 6f 70 28 70 50  3ExprCachePop(pP
216b8 61 72 73 65 2c 20 31 29 3b 0a 0a 20 20 72 65 74  arse, 1);..  ret
216b9 75 72 6e 20 72 52 65 67 3b 0a 7d 0a 23 65 6e 64  urn rReg;.}.#end
216ba 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
216bb 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 23  T_SUBQUERY */..#
216bc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
216bd 49 54 5f 53 55 42 51 55 45 52 59 0a 2f 2a 0a 2a  IT_SUBQUERY./*.*
216be 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
216bf 66 6f 72 20 61 6e 20 49 4e 20 65 78 70 72 65 73  for an IN expres
216c0 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  sion..**.**     
216c1 20 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e   x IN (SELECT ..
216c2 2e 29 0a 2a 2a 20 20 20 20 20 20 78 20 49 4e 20  .).**      x IN 
216c3 28 76 61 6c 75 65 2c 20 76 61 6c 75 65 2c 20 2e  (value, value, .
216c4 2e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65  ..).**.** The le
216c5 66 74 2d 68 61 6e 64 20 73 69 64 65 20 28 4c 48  ft-hand side (LH
216c6 53 29 20 69 73 20 61 20 73 63 61 6c 61 72 20 65  S) is a scalar e
216c7 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68 65 20  xpression.  The 
216c8 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
216c9 28 52 48 53 29 0a 2a 2a 20 69 73 20 61 6e 20 61  (RHS).** is an a
216ca 72 72 61 79 20 6f 66 20 7a 65 72 6f 20 6f 72 20  rray of zero or 
216cb 6d 6f 72 65 20 76 61 6c 75 65 73 2e 20 20 54 68  more values.  Th
216cc 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
216cd 74 72 75 65 20 69 66 20 74 68 65 20 4c 48 53 20  true if the LHS 
216ce 69 73 0a 2a 2a 20 63 6f 6e 74 61 69 6e 65 64 20  is.** contained 
216cf 77 69 74 68 69 6e 20 74 68 65 20 52 48 53 2e 20  within the RHS. 
216d0 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   The value of th
216d1 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
216d2 75 6e 6b 6e 6f 77 6e 20 28 4e 55 4c 4c 29 0a 2a  unknown (NULL).*
216d3 2a 20 69 66 20 74 68 65 20 4c 48 53 20 69 73 20  * if the LHS is 
216d4 4e 55 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 4c  NULL or if the L
216d5 48 53 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69  HS is not contai
216d6 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 52  ned within the R
216d7 48 53 20 61 6e 64 20 74 68 65 0a 2a 2a 20 52 48  HS and the.** RH
216d8 53 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f  S contains one o
216d9 72 20 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c 75  r more NULL valu
216da 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  es..**.** This r
216db 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
216dc 20 63 6f 64 65 20 77 69 6c 6c 20 6a 75 6d 70 20   code will jump 
216dd 74 6f 20 64 65 73 74 49 66 46 61 6c 73 65 20 69  to destIfFalse i
216de 66 20 74 68 65 20 4c 48 53 20 69 73 20 6e 6f 74  f the LHS is not
216df 20 0a 2a 2a 20 63 6f 6e 74 61 69 6e 65 64 20 77   .** contained w
216e0 69 74 68 69 6e 20 74 68 65 20 52 48 53 2e 20 20  ithin the RHS.  
216e1 49 66 20 64 75 65 20 74 6f 20 4e 55 4c 4c 73 20  If due to NULLs 
216e2 77 65 20 63 61 6e 6e 6f 74 20 64 65 74 65 72 6d  we cannot determ
216e3 69 6e 65 20 69 66 20 74 68 65 20 4c 48 53 0a 2a  ine if the LHS.*
216e4 2a 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69  * is contained i
216e5 6e 20 74 68 65 20 52 48 53 20 74 68 65 6e 20 6a  n the RHS then j
216e6 75 6d 70 20 74 6f 20 64 65 73 74 49 66 4e 75 6c  ump to destIfNul
216e7 6c 2e 20 20 49 66 20 74 68 65 20 4c 48 53 20 69  l.  If the LHS i
216e8 73 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20 77  s contained.** w
216e9 69 74 68 69 6e 20 74 68 65 20 52 48 53 20 74 68  ithin the RHS th
216ea 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 2e  en fall through.
216eb 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
216ec 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 49  sqlite3ExprCodeI
216ed 4e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  N(.  Parse *pPar
216ee 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61  se,        /* Pa
216ef 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67  rsing and code g
216f0 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78  enerating contex
216f1 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  t */.  Expr *pEx
216f2 70 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  pr,          /* 
216f3 54 68 65 20 49 4e 20 65 78 70 72 65 73 73 69 6f  The IN expressio
216f4 6e 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 49  n */.  int destI
216f5 66 46 61 6c 73 65 2c 20 20 20 20 20 20 2f 2a 20  fFalse,      /* 
216f6 4a 75 6d 70 20 68 65 72 65 20 69 66 20 4c 48 53  Jump here if LHS
216f7 20 69 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65   is not containe
216f8 64 20 69 6e 20 74 68 65 20 52 48 53 20 2a 2f 0a  d in the RHS */.
216f9 20 20 69 6e 74 20 64 65 73 74 49 66 4e 75 6c 6c    int destIfNull
216fa 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
216fb 68 65 72 65 20 69 66 20 74 68 65 20 72 65 73 75  here if the resu
216fc 6c 74 73 20 61 72 65 20 75 6e 6b 6e 6f 77 6e 20  lts are unknown 
216fd 64 75 65 20 74 6f 20 4e 55 4c 4c 73 20 2a 2f 0a  due to NULLs */.
216fe 29 7b 0a 20 20 69 6e 74 20 72 52 68 73 48 61 73  ){.  int rRhsHas
216ff 4e 75 6c 6c 20 3d 20 30 3b 20 20 2f 2a 20 52 65  Null = 0;  /* Re
21700 67 69 73 74 65 72 20 74 68 61 74 20 69 73 20 74  gister that is t
21701 72 75 65 20 69 66 20 52 48 53 20 63 6f 6e 74 61  rue if RHS conta
21702 69 6e 73 20 4e 55 4c 4c 20 76 61 6c 75 65 73 20  ins NULL values 
21703 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69  */.  char affini
21704 74 79 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ty;        /* Co
21705 6d 70 61 72 69 73 6f 6e 20 61 66 66 69 6e 69 74  mparison affinit
21706 79 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 69 6e  y to use */.  in
21707 74 20 65 54 79 70 65 3b 20 20 20 20 20 20 20 20  t eType;        
21708 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74      /* Type of t
21709 68 65 20 52 48 53 20 2a 2f 0a 20 20 69 6e 74 20  he RHS */.  int 
2170a 72 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  r1;             
2170b 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 75    /* Temporary u
2170c 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  se register */. 
2170d 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
2170e 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d         /* Statem
2170f 65 6e 74 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  ent under constr
21710 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20  uction */..  /* 
21711 43 6f 6d 70 75 74 65 20 74 68 65 20 52 48 53 2e  Compute the RHS.
21712 20 20 20 41 66 74 65 72 20 74 68 69 73 20 73 74     After this st
21713 65 70 2c 20 74 68 65 20 74 61 62 6c 65 20 77 69  ep, the table wi
21714 74 68 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 70  th cursor.  ** p
21715 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 77 69 6c  Expr->iTable wil
21716 6c 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76  l contains the v
21717 61 6c 75 65 73 20 74 68 61 74 20 6d 61 6b 65 20  alues that make 
21718 75 70 20 74 68 65 20 52 48 53 2e 0a 20 20 2a 2f  up the RHS..  */
21719 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  .  v = pParse->p
2171a 56 64 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20  Vdbe;.  assert( 
2171b 76 21 3d 30 20 29 3b 20 20 20 20 20 20 20 2f 2a  v!=0 );       /*
2171c 20 4f 4f 4d 20 64 65 74 65 63 74 65 64 20 70 72   OOM detected pr
2171d 69 6f 72 20 74 6f 20 74 68 69 73 20 72 6f 75 74  ior to this rout
2171e 69 6e 65 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f  ine */.  VdbeNoo
2171f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 62 65  pComment((v, "be
21720 67 69 6e 20 49 4e 20 65 78 70 72 22 29 29 3b 0a  gin IN expr"));.
21721 20 20 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65    eType = sqlite
21722 33 46 69 6e 64 49 6e 49 6e 64 65 78 28 70 50 61  3FindInIndex(pPa
21723 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 52 68  rse, pExpr, &rRh
21724 73 48 61 73 4e 75 6c 6c 29 3b 0a 0a 20 20 2f 2a  sHasNull);..  /*
21725 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20   Figure out the 
21726 61 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20  affinity to use 
21727 74 6f 20 63 72 65 61 74 65 20 61 20 6b 65 79 20  to create a key 
21728 66 72 6f 6d 20 74 68 65 20 72 65 73 75 6c 74 73  from the results
21729 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 65 78 70  .  ** of the exp
2172a 72 65 73 73 69 6f 6e 2e 20 61 66 66 69 6e 69 74  ression. affinit
2172b 79 53 74 72 20 73 74 6f 72 65 73 20 61 20 73 74  yStr stores a st
2172c 61 74 69 63 20 73 74 72 69 6e 67 20 73 75 69 74  atic string suit
2172d 61 62 6c 65 20 66 6f 72 0a 20 20 2a 2a 20 50 34  able for.  ** P4
2172e 20 6f 66 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72   of OP_MakeRecor
2172f 64 2e 0a 20 20 2a 2f 0a 20 20 61 66 66 69 6e 69  d..  */.  affini
21730 74 79 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41  ty = comparisonA
21731 66 66 69 6e 69 74 79 28 70 45 78 70 72 29 3b 0a  ffinity(pExpr);.
21732 0a 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 4c  .  /* Code the L
21733 48 53 2c 20 74 68 65 20 3c 65 78 70 72 3e 20 66  HS, the <expr> f
21734 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49 4e 20 28  rom "<expr> IN (
21735 2e 2e 2e 29 22 2e 0a 20 20 2a 2f 0a 20 20 73 71  ...)"..  */.  sq
21736 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75  lite3ExprCachePu
21737 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 72 31  sh(pParse);.  r1
21738 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
21739 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2173a 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2173b 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
2173c 4c 65 66 74 2c 20 72 31 29 3b 0a 20 20 73 71 6c  Left, r1);.  sql
2173d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2173e 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c  , OP_IsNull, r1,
2173f 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 0a 0a   destIfNull);...
21740 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e 5f    if( eType==IN_
21741 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a 20  INDEX_ROWID ){. 
21742 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61     /* In this ca
21743 73 65 2c 20 74 68 65 20 52 48 53 20 69 73 20 74  se, the RHS is t
21744 68 65 20 52 4f 57 49 44 20 6f 66 20 74 61 62 6c  he ROWID of tabl
21745 65 20 62 2d 74 72 65 65 0a 20 20 20 20 2a 2f 0a  e b-tree.    */.
21746 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21747 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73 74  ddOp2(v, OP_Must
21748 42 65 49 6e 74 2c 20 72 31 2c 20 64 65 73 74 49  BeInt, r1, destI
21749 66 46 61 6c 73 65 29 3b 0a 20 20 20 20 73 71 6c  fFalse);.    sql
2174a 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2174b 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20  , OP_NotExists, 
2174c 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 64  pExpr->iTable, d
2174d 65 73 74 49 66 46 61 6c 73 65 2c 20 72 31 29 3b  estIfFalse, r1);
2174e 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
2174f 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74   In this case, t
21750 68 65 20 52 48 53 20 69 73 20 61 6e 20 69 6e 64  he RHS is an ind
21751 65 78 20 62 2d 74 72 65 65 2e 0a 20 20 20 20 2a  ex b-tree..    *
21752 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
21753 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 41 66  eAddOp4(v, OP_Af
21754 66 69 6e 69 74 79 2c 20 72 31 2c 20 31 2c 20 30  finity, r1, 1, 0
21755 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31 29 3b  , &affinity, 1);
21756 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ..    /* If the 
21757 73 65 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74  set membership t
21758 65 73 74 20 66 61 69 6c 73 2c 20 74 68 65 6e 20  est fails, then 
21759 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
2175a 65 20 0a 20 20 20 20 2a 2a 20 22 78 20 49 4e 20  e .    ** "x IN 
2175b 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 73 69 6f  (...)" expressio
2175c 6e 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  n must be either
2175d 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49 66 20 74   0 or NULL. If t
2175e 68 65 20 73 65 74 0a 20 20 20 20 2a 2a 20 63 6f  he set.    ** co
2175f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76  ntains no NULL v
21760 61 6c 75 65 73 2c 20 74 68 65 6e 20 74 68 65 20  alues, then the 
21761 72 65 73 75 6c 74 20 69 73 20 30 2e 20 49 66 20  result is 0. If 
21762 74 68 65 20 73 65 74 20 0a 20 20 20 20 2a 2a 20  the set .    ** 
21763 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20  contains one or 
21764 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c 75 65 73  more NULL values
21765 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  , then the resul
21766 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  t of the.    ** 
21767 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6c  expression is al
21768 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a  so NULL..    */.
21769 20 20 20 20 69 66 28 20 72 52 68 73 48 61 73 4e      if( rRhsHasN
2176a 75 6c 6c 3d 3d 30 20 7c 7c 20 64 65 73 74 49 66  ull==0 || destIf
2176b 46 61 6c 73 65 3d 3d 64 65 73 74 49 66 4e 75 6c  False==destIfNul
2176c 6c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  l ){.      /* Th
2176d 69 73 20 62 72 61 6e 63 68 20 72 75 6e 73 20 69  is branch runs i
2176e 66 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 61 74  f it is known at
2176f 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20 74 68   compile time th
21770 61 74 20 74 68 65 20 52 48 53 0a 20 20 20 20 20  at the RHS.     
21771 20 2a 2a 20 63 61 6e 6e 6f 74 20 63 6f 6e 74 61   ** cannot conta
21772 69 6e 20 4e 55 4c 4c 20 76 61 6c 75 65 73 2e 20  in NULL values. 
21773 54 68 69 73 20 68 61 70 70 65 6e 73 20 61 73 20  This happens as 
21774 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 20  the result.     
21775 20 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 20 4e 55   ** of a "NOT NU
21776 4c 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  LL" constraint i
21777 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  n the database s
21778 63 68 65 6d 61 2e 0a 20 20 20 20 20 20 2a 2a 0a  chema..      **.
21779 20 20 20 20 20 20 2a 2a 20 41 6c 73 6f 20 72 75        ** Also ru
2177a 6e 20 74 68 69 73 20 62 72 61 6e 63 68 20 69 66  n this branch if
2177b 20 4e 55 4c 4c 20 69 73 20 65 71 75 69 76 61 6c   NULL is equival
2177c 65 6e 74 20 74 6f 20 46 41 4c 53 45 0a 20 20 20  ent to FALSE.   
2177d 20 20 20 2a 2a 20 66 6f 72 20 74 68 69 73 20 70     ** for this p
2177e 61 72 74 69 63 75 6c 61 72 20 49 4e 20 6f 70 65  articular IN ope
2177f 72 61 74 6f 72 2e 0a 20 20 20 20 20 20 2a 2f 0a  rator..      */.
21780 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21781 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50  eAddOp4Int(v, OP
21782 5f 4e 6f 74 46 6f 75 6e 64 2c 20 70 45 78 70 72  _NotFound, pExpr
21783 2d 3e 69 54 61 62 6c 65 2c 20 64 65 73 74 49 66  ->iTable, destIf
21784 46 61 6c 73 65 2c 20 72 31 2c 20 31 29 3b 0a 0a  False, r1, 1);..
21785 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
21786 20 2f 2a 20 49 6e 20 74 68 69 73 20 62 72 61 6e   /* In this bran
21787 63 68 2c 20 74 68 65 20 52 48 53 20 6f 66 20 74  ch, the RHS of t
21788 68 65 20 49 4e 20 6d 69 67 68 74 20 63 6f 6e 74  he IN might cont
21789 61 69 6e 20 61 20 4e 55 4c 4c 20 61 6e 64 0a 20  ain a NULL and. 
2178a 20 20 20 20 20 2a 2a 20 74 68 65 20 70 72 65 73       ** the pres
2178b 65 6e 63 65 20 6f 66 20 61 20 4e 55 4c 4c 20 6f  ence of a NULL o
2178c 6e 20 74 68 65 20 52 48 53 20 6d 61 6b 65 73 20  n the RHS makes 
2178d 61 20 64 69 66 66 65 72 65 6e 63 65 20 69 6e 20  a difference in 
2178e 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 75 74  the.      ** out
2178f 63 6f 6d 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  come..      */. 
21790 20 20 20 20 20 69 6e 74 20 6a 31 2c 20 6a 32 2c       int j1, j2,
21791 20 6a 33 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46   j3;..      /* F
21792 69 72 73 74 20 63 68 65 63 6b 20 74 6f 20 73 65  irst check to se
21793 65 20 69 66 20 74 68 65 20 4c 48 53 20 69 73 20  e if the LHS is 
21794 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
21795 20 52 48 53 2e 20 20 49 66 20 73 6f 2c 0a 20 20   RHS.  If so,.  
21796 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20      ** then the 
21797 70 72 65 73 65 6e 63 65 20 6f 66 20 4e 55 4c 4c  presence of NULL
21798 73 20 69 6e 20 74 68 65 20 52 48 53 20 64 6f 65  s in the RHS doe
21799 73 20 6e 6f 74 20 6d 61 74 74 65 72 2c 20 73 6f  s not matter, so
2179a 20 6a 75 6d 70 0a 20 20 20 20 20 20 2a 2a 20 6f   jump.      ** o
2179b 76 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 63  ver all of the c
2179c 6f 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ode that follows
2179d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
2179e 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
2179f 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f 50  eAddOp4Int(v, OP
217a0 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e 69  _Found, pExpr->i
217a1 54 61 62 6c 65 2c 20 30 2c 20 72 31 2c 20 31 29  Table, 0, r1, 1)
217a2 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 48 65 72 65  ;..      /* Here
217a3 20 77 65 20 62 65 67 69 6e 20 67 65 6e 65 72 61   we begin genera
217a4 74 69 6e 67 20 63 6f 64 65 20 74 68 61 74 20 72  ting code that r
217a5 75 6e 73 20 69 66 20 74 68 65 20 4c 48 53 20 69  uns if the LHS i
217a6 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 63  s not.      ** c
217a7 6f 6e 74 61 69 6e 65 64 20 77 69 74 68 69 6e 20  ontained within 
217a8 74 68 65 20 52 48 53 2e 20 20 47 65 6e 65 72 61  the RHS.  Genera
217a9 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f  te additional co
217aa 64 65 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a  de that.      **
217ab 20 74 65 73 74 73 20 74 68 65 20 52 48 53 20 66   tests the RHS f
217ac 6f 72 20 4e 55 4c 4c 73 2e 20 20 49 66 20 74 68  or NULLs.  If th
217ad 65 20 52 48 53 20 63 6f 6e 74 61 69 6e 73 20 61  e RHS contains a
217ae 20 4e 55 4c 4c 20 74 68 65 6e 0a 20 20 20 20 20   NULL then.     
217af 20 2a 2a 20 6a 75 6d 70 20 74 6f 20 64 65 73 74   ** jump to dest
217b0 49 66 4e 75 6c 6c 2e 20 20 49 66 20 74 68 65 72  IfNull.  If ther
217b1 65 20 61 72 65 20 6e 6f 20 4e 55 4c 4c 73 20 69  e are no NULLs i
217b2 6e 20 74 68 65 20 52 48 53 20 74 68 65 6e 0a 20  n the RHS then. 
217b3 20 20 20 20 20 2a 2a 20 6a 75 6d 70 20 74 6f 20       ** jump to 
217b4 64 65 73 74 49 66 46 61 6c 73 65 2e 0a 20 20 20  destIfFalse..   
217b5 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a 32 20 3d     */.      j2 =
217b6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
217b7 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c  p1(v, OP_NotNull
217b8 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c 29 3b 0a  , rRhsHasNull);.
217b9 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74        j3 = sqlit
217ba 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74 28  e3VdbeAddOp4Int(
217bb 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78  v, OP_Found, pEx
217bc 70 72 2d 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72  pr->iTable, 0, r
217bd 52 68 73 48 61 73 4e 75 6c 6c 2c 20 31 29 3b 0a  RhsHasNull, 1);.
217be 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
217bf 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
217c0 74 65 67 65 72 2c 20 2d 31 2c 20 72 52 68 73 48  teger, -1, rRhsH
217c1 61 73 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73  asNull);.      s
217c2 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
217c3 72 65 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 20  re(v, j3);.     
217c4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
217c5 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
217c6 20 72 52 68 73 48 61 73 4e 75 6c 6c 2c 20 31 29   rRhsHasNull, 1)
217c7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
217c8 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
217c9 32 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4a 75  2);..      /* Ju
217ca 6d 70 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70  mp to the approp
217cb 72 69 61 74 65 20 74 61 72 67 65 74 20 64 65 70  riate target dep
217cc 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
217cd 72 20 6f 72 20 6e 6f 74 0a 20 20 20 20 20 20 2a  r or not.      *
217ce 2a 20 74 68 65 20 52 48 53 20 63 6f 6e 74 61 69  * the RHS contai
217cf 6e 73 20 61 20 4e 55 4c 4c 0a 20 20 20 20 20 20  ns a NULL.      
217d0 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
217d1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
217d2 5f 49 66 2c 20 72 52 68 73 48 61 73 4e 75 6c 6c  _If, rRhsHasNull
217d3 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a 20  , destIfNull);. 
217d4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
217d5 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
217d6 6f 2c 20 30 2c 20 64 65 73 74 49 66 46 61 6c 73  o, 0, destIfFals
217d7 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68  e);..      /* Th
217d8 65 20 4f 50 5f 46 6f 75 6e 64 20 61 74 20 74 68  e OP_Found at th
217d9 65 20 74 6f 70 20 6f 66 20 74 68 69 73 20 62 72  e top of this br
217da 61 6e 63 68 20 6a 75 6d 70 73 20 68 65 72 65 20  anch jumps here 
217db 77 68 65 6e 20 74 72 75 65 2c 20 0a 20 20 20 20  when true, .    
217dc 20 20 2a 2a 20 63 61 75 73 69 6e 67 20 74 68 65    ** causing the
217dd 20 6f 76 65 72 61 6c 6c 20 49 4e 20 65 78 70 72   overall IN expr
217de 65 73 73 69 6f 6e 20 65 76 61 6c 75 61 74 69 6f  ession evaluatio
217df 6e 20 74 6f 20 66 61 6c 6c 20 74 68 72 6f 75 67  n to fall throug
217e0 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  h..      */.    
217e1 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
217e2 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20  pHere(v, j1);.  
217e3 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
217e4 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
217e5 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 73  pParse, r1);.  s
217e6 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
217e7 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20  op(pParse, 1);. 
217e8 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
217e9 20 22 65 6e 64 20 49 4e 20 65 78 70 72 22 29 29   "end IN expr"))
217ea 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
217eb 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
217ec 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 75 70  RY */../*.** Dup
217ed 6c 69 63 61 74 65 20 61 6e 20 38 2d 62 79 74 65  licate an 8-byte
217ee 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61 74 69 63   value.*/.static
217ef 20 63 68 61 72 20 2a 64 75 70 38 62 79 74 65 73   char *dup8bytes
217f0 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74 20  (Vdbe *v, const 
217f1 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20 63 68 61  char *in){.  cha
217f2 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69 74 65 33  r *out = sqlite3
217f3 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69  DbMallocRaw(sqli
217f4 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 38 29  te3VdbeDb(v), 8)
217f5 3b 0a 20 20 69 66 28 20 6f 75 74 20 29 7b 0a 20  ;.  if( out ){. 
217f6 20 20 20 6d 65 6d 63 70 79 28 6f 75 74 2c 20 69     memcpy(out, i
217f7 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20 72 65 74  n, 8);.  }.  ret
217f8 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn out;.}../*.*
217f9 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 69 6e  * Generate an in
217fa 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 77  struction that w
217fb 69 6c 6c 20 70 75 74 20 74 68 65 20 66 6c 6f 61  ill put the floa
217fc 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76 61  ting point.** va
217fd 6c 75 65 20 64 65 73 63 72 69 62 65 64 20 62 79  lue described by
217fe 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20   z[0..n-1] into 
217ff 72 65 67 69 73 74 65 72 20 69 4d 65 6d 2e 0a 2a  register iMem..*
21800 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 74 72  *.** The z[] str
21801 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 62 6c  ing will probabl
21802 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d 74 65  y not be zero-te
21803 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 20 74  rminated.  But t
21804 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72  he .** z[n] char
21805 61 63 74 65 72 20 69 73 20 67 75 61 72 61 6e 74  acter is guarant
21806 65 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 74 68  eed to be someth
21807 69 6e 67 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  ing that does no
21808 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74  t look.** like t
21809 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20  he continuation 
2180a 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e 0a 2a  of the number..*
2180b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
2180c 64 65 52 65 61 6c 28 56 64 62 65 20 2a 76 2c 20  deReal(Vdbe *v, 
2180d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
2180e 6e 74 20 6e 65 67 61 74 65 46 6c 61 67 2c 20 69  nt negateFlag, i
2180f 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28 20  nt iMem){.  if( 
21810 41 4c 57 41 59 53 28 7a 21 3d 30 29 20 29 7b 0a  ALWAYS(z!=0) ){.
21811 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75 65      double value
21812 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a  ;.    char *zV;.
21813 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28      sqlite3AtoF(
21814 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20  z, &value);.    
21815 61 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33  assert( !sqlite3
21816 49 73 4e 61 4e 28 76 61 6c 75 65 29 20 29 3b 20  IsNaN(value) ); 
21817 2f 2a 20 54 68 65 20 6e 65 77 20 41 74 6f 46 20  /* The new AtoF 
21818 6e 65 76 65 72 20 72 65 74 75 72 6e 73 20 4e 61  never returns Na
21819 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 67  N */.    if( neg
2181a 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75 65 20  ateFlag ) value 
2181b 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 7a 56  = -value;.    zV
2181c 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c 20   = dup8bytes(v, 
2181d 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b 0a  (char*)&value);.
2181e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2181f 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 65 61 6c  ddOp4(v, OP_Real
21820 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56  , 0, iMem, 0, zV
21821 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20 20 7d 0a  , P4_REAL);.  }.
21822 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  }.../*.** Genera
21823 74 65 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f  te an instructio
21824 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 75 74 20  n that will put 
21825 74 68 65 20 69 6e 74 65 67 65 72 20 64 65 73 63  the integer desc
21826 72 69 62 65 20 62 79 0a 2a 2a 20 74 65 78 74 20  ribe by.** text 
21827 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72  z[0..n-1] into r
21828 65 67 69 73 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a  egister iMem..**
21829 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 74 72 69  .** The z[] stri
2182a 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 62 6c 79  ng will probably
2182b 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d 74 65 72   not be zero-ter
2182c 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 20 74 68  minated.  But th
2182d 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72 61  e .** z[n] chara
2182e 63 74 65 72 20 69 73 20 67 75 61 72 61 6e 74 65  cter is guarante
2182f 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 74 68 69  ed to be somethi
21830 6e 67 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74  ng that does not
21831 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68   look.** like th
21832 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f  e continuation o
21833 66 20 74 68 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f  f the number..*/
21834 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
21835 65 49 6e 74 65 67 65 72 28 56 64 62 65 20 2a 76  eInteger(Vdbe *v
21836 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69  , Expr *pExpr, i
21837 6e 74 20 6e 65 67 46 6c 61 67 2c 20 69 6e 74 20  nt negFlag, int 
21838 69 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 45 78  iMem){.  if( pEx
21839 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 49  pr->flags & EP_I
2183a 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 69  ntValue ){.    i
2183b 6e 74 20 69 20 3d 20 70 45 78 70 72 2d 3e 75 2e  nt i = pExpr->u.
2183c 69 56 61 6c 75 65 3b 0a 20 20 20 20 69 66 28 20  iValue;.    if( 
2183d 6e 65 67 46 6c 61 67 20 29 20 69 20 3d 20 2d 69  negFlag ) i = -i
2183e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2183f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
21840 74 65 67 65 72 2c 20 69 2c 20 69 4d 65 6d 29 3b  teger, i, iMem);
21841 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f  .  }else{.    co
21842 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 45  nst char *z = pE
21843 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20  xpr->u.zToken;. 
21844 20 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30 20     assert( z!=0 
21845 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  );.    if( sqlit
21846 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28 7a  e3FitsIn64Bits(z
21847 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b 0a 20 20  , negFlag) ){.  
21848 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20      i64 value;. 
21849 20 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a 20       char *zV;. 
2184a 20 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 69       sqlite3Atoi
2184b 36 34 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20  64(z, &value);. 
2184c 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61 67       if( negFlag
2184d 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c 75   ) value = -valu
2184e 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64 75  e;.      zV = du
2184f 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61 72  p8bytes(v, (char
21850 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 20  *)&value);.     
21851 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21852 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c 20  p4(v, OP_Int64, 
21853 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c 20  0, iMem, 0, zV, 
21854 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 20 20 7d  P4_INT64);.    }
21855 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 64 65  else{.      code
21856 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 65 67 46 6c  Real(v, z, negFl
21857 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d  ag, iMem);.    }
21858 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  .  }.}../*.** Cl
21859 65 61 72 20 61 20 63 61 63 68 65 20 65 6e 74 72  ear a cache entr
2185a 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  y..*/.static voi
2185b 64 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61  d cacheEntryClea
2185c 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  r(Parse *pParse,
2185d 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68   struct yColCach
2185e 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  e *p){.  if( p->
2185f 74 65 6d 70 52 65 67 20 29 7b 0a 20 20 20 20 69  tempReg ){.    i
21860 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70  f( pParse->nTemp
21861 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70 50  Reg<ArraySize(pP
21862 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29 20  arse->aTempReg) 
21863 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ){.      pParse-
21864 3e 61 54 65 6d 70 52 65 67 5b 70 50 61 72 73 65  >aTempReg[pParse
21865 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d 20  ->nTempReg++] = 
21866 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20  p->iReg;.    }. 
21867 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20     p->tempReg = 
21868 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  0;.  }.}.../*.**
21869 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65 20 63   Record in the c
2186a 6f 6c 75 6d 6e 20 63 61 63 68 65 20 74 68 61 74  olumn cache that
2186b 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f   a particular co
2186c 6c 75 6d 6e 20 66 72 6f 6d 20 61 0a 2a 2a 20 70  lumn from a.** p
2186d 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20  articular table 
2186e 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 70  is stored in a p
2186f 61 72 74 69 63 75 6c 61 72 20 72 65 67 69 73 74  articular regist
21870 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
21871 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21872 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65  e3ExprCacheStore
21873 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
21874 69 6e 74 20 69 54 61 62 2c 20 69 6e 74 20 69 43  int iTab, int iC
21875 6f 6c 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20  ol, int iReg){. 
21876 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d 69   int i;.  int mi
21877 6e 4c 72 75 3b 0a 20 20 69 6e 74 20 69 64 78 4c  nLru;.  int idxL
21878 72 75 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f  ru;.  struct yCo
21879 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 61 73  lCache *p;..  as
2187a 73 65 72 74 28 20 69 52 65 67 3e 30 20 29 3b 20  sert( iReg>0 ); 
2187b 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6e 75 6d   /* Register num
2187c 62 65 72 73 20 61 72 65 20 61 6c 77 61 79 73 20  bers are always 
2187d 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20 61 73  positive */.  as
2187e 73 65 72 74 28 20 69 43 6f 6c 3e 3d 2d 31 20 26  sert( iCol>=-1 &
2187f 26 20 69 43 6f 6c 3c 33 32 37 36 38 20 29 3b 20  & iCol<32768 ); 
21880 20 2f 2a 20 46 69 6e 69 74 65 20 63 6f 6c 75 6d   /* Finite colum
21881 6e 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 0a 20 20  n numbers */..  
21882 2f 2a 20 46 69 72 73 74 20 72 65 70 6c 61 63 65  /* First replace
21883 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 65 6e   any existing en
21884 74 72 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  try */.  for(i=0
21885 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c  , p=pParse->aCol
21886 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f  Cache; i<SQLITE_
21887 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c  N_COLCACHE; i++,
21888 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70   p++){.    if( p
21889 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e 69 54 61  ->iReg && p->iTa
2188a 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 2d 3e  ble==iTab && p->
2188b 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 20 29 7b  iColumn==iCol ){
2188c 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72  .      cacheEntr
2188d 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70  yClear(pParse, p
2188e 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 65 76  );.      p->iLev
2188f 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61  el = pParse->iCa
21890 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 20 20  cheLevel;.      
21891 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b 0a  p->iReg = iReg;.
21892 20 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e        p->affChan
21893 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  ge = 0;.      p-
21894 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69  >lru = pParse->i
21895 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20  CacheCnt++;.    
21896 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
21897 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20 61    }..  /* Find a
21898 6e 20 65 6d 70 74 79 20 73 6c 6f 74 20 61 6e 64  n empty slot and
21899 20 72 65 70 6c 61 63 65 20 69 74 20 2a 2f 0a 20   replace it */. 
2189a 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
2189b 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
2189c 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
2189d 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
2189e 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d     if( p->iReg==
2189f 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4c  0 ){.      p->iL
218a0 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69  evel = pParse->i
218a1 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20  CacheLevel;.    
218a2 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54    p->iTable = iT
218a3 61 62 3b 0a 20 20 20 20 20 20 70 2d 3e 69 43 6f  ab;.      p->iCo
218a4 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  lumn = iCol;.   
218a5 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65     p->iReg = iRe
218a6 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 43  g;.      p->affC
218a7 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 20  hange = 0;.     
218a8 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30 3b   p->tempReg = 0;
218a9 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d 20  .      p->lru = 
218aa 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43 6e  pParse->iCacheCn
218ab 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72  t++;.      retur
218ac 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
218ad 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 6c  /* Replace the l
218ae 61 73 74 20 72 65 63 65 6e 74 6c 79 20 75 73 65  ast recently use
218af 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72 75 20 3d 20  d */.  minLru = 
218b0 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 69 64  0x7fffffff;.  id
218b1 78 4c 72 75 20 3d 20 2d 31 3b 0a 20 20 66 6f 72  xLru = -1;.  for
218b2 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
218b3 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
218b4 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
218b5 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
218b6 66 28 20 70 2d 3e 6c 72 75 3c 6d 69 6e 4c 72 75  f( p->lru<minLru
218b7 20 29 7b 0a 20 20 20 20 20 20 69 64 78 4c 72 75   ){.      idxLru
218b8 20 3d 20 69 3b 0a 20 20 20 20 20 20 6d 69 6e 4c   = i;.      minL
218b9 72 75 20 3d 20 70 2d 3e 6c 72 75 3b 0a 20 20 20  ru = p->lru;.   
218ba 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 41 4c 57   }.  }.  if( ALW
218bb 41 59 53 28 69 64 78 4c 72 75 3e 3d 30 29 20 29  AYS(idxLru>=0) )
218bc 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61 72 73  {.    p = &pPars
218bd 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 64 78  e->aColCache[idx
218be 4c 72 75 5d 3b 0a 20 20 20 20 70 2d 3e 69 4c 65  Lru];.    p->iLe
218bf 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43  vel = pParse->iC
218c0 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 70  acheLevel;.    p
218c1 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 3b  ->iTable = iTab;
218c2 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20  .    p->iColumn 
218c3 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e 69  = iCol;.    p->i
218c4 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20 20  Reg = iReg;.    
218c5 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 30  p->affChange = 0
218c6 3b 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67  ;.    p->tempReg
218c7 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 6c 72 75   = 0;.    p->lru
218c8 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68   = pParse->iCach
218c9 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 72 65 74 75  eCnt++;.    retu
218ca 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn;.  }.}../*.**
218cb 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 20 61   Indicate that a
218cc 20 72 65 67 69 73 74 65 72 20 69 73 20 62 65 69   register is bei
218cd 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20  ng overwritten. 
218ce 20 50 75 72 67 65 20 74 68 65 20 72 65 67 69 73   Purge the regis
218cf 74 65 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ter.** from the 
218d0 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 2a 2f  column cache..*/
218d1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
218d2 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
218d3 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72 73  CacheRemove(Pars
218d4 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
218d5 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  Reg){.  int i;. 
218d6 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68   struct yColCach
218d7 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  e *p;.  for(i=0,
218d8 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   p=pParse->aColC
218d9 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e  ache; i<SQLITE_N
218da 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20  _COLCACHE; i++, 
218db 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  p++){.    if( p-
218dc 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20  >iReg==iReg ){. 
218dd 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43       cacheEntryC
218de 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b  lear(pParse, p);
218df 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d  .      p->iReg =
218e0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a   0;.    }.  }.}.
218e1 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72 20  ./*.** Remember 
218e2 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6c 75  the current colu
218e3 6d 6e 20 63 61 63 68 65 20 63 6f 6e 74 65 78 74  mn cache context
218e4 2e 20 20 41 6e 79 20 6e 65 77 20 65 6e 74 72 69  .  Any new entri
218e5 65 73 20 61 64 64 65 64 0a 2a 2a 20 61 64 64 65  es added.** adde
218e6 64 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20  d to the column 
218e7 63 61 63 68 65 20 61 66 74 65 72 20 74 68 69 73  cache after this
218e8 20 63 61 6c 6c 20 61 72 65 20 72 65 6d 6f 76 65   call are remove
218e9 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 63 6f  d when the.** co
218ea 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 70 20  rresponding pop 
218eb 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  occurs..*/.SQLIT
218ec 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
218ed 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
218ee 75 73 68 28 50 61 72 73 65 20 2a 70 50 61 72 73  ush(Parse *pPars
218ef 65 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 69 43  e){.  pParse->iC
218f0 61 63 68 65 4c 65 76 65 6c 2b 2b 3b 0a 7d 0a 0a  acheLevel++;.}..
218f1 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 66 72 6f  /*.** Remove fro
218f2 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63  m the column cac
218f3 68 65 20 61 6e 79 20 65 6e 74 72 69 65 73 20 74  he any entries t
218f4 68 61 74 20 77 65 72 65 20 61 64 64 65 64 20 73  hat were added s
218f5 69 6e 63 65 20 74 68 65 0a 2a 2a 20 74 68 65 20  ince the.** the 
218f6 70 72 65 76 69 6f 75 73 20 4e 20 50 75 73 68 20  previous N Push 
218f7 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 49 6e 20  operations.  In 
218f8 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 73  other words, res
218f9 74 6f 72 65 20 74 68 65 20 63 61 63 68 65 0a 2a  tore the cache.*
218fa 2a 20 74 6f 20 74 68 65 20 73 74 61 74 65 20 69  * to the state i
218fb 74 20 77 61 73 20 69 6e 20 4e 20 50 75 73 68 65  t was in N Pushe
218fc 73 20 61 67 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45  s ago..*/.SQLITE
218fd 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
218fe 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f  lite3ExprCachePo
218ff 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  p(Parse *pParse,
21900 20 69 6e 74 20 4e 29 7b 0a 20 20 69 6e 74 20 69   int N){.  int i
21901 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43  ;.  struct yColC
21902 61 63 68 65 20 2a 70 3b 0a 20 20 61 73 73 65 72  ache *p;.  asser
21903 74 28 20 4e 3e 30 20 29 3b 0a 20 20 61 73 73 65  t( N>0 );.  asse
21904 72 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63  rt( pParse->iCac
21905 68 65 4c 65 76 65 6c 3e 3d 4e 20 29 3b 0a 20 20  heLevel>=N );.  
21906 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65  pParse->iCacheLe
21907 76 65 6c 20 2d 3d 20 4e 3b 0a 20 20 66 6f 72 28  vel -= N;.  for(
21908 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
21909 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
2190a 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
2190b 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, p++){.    if
2190c 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e  ( p->iReg && p->
2190d 69 4c 65 76 65 6c 3e 70 50 61 72 73 65 2d 3e 69  iLevel>pParse->i
2190e 43 61 63 68 65 4c 65 76 65 6c 20 29 7b 0a 20 20  CacheLevel ){.  
2190f 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c      cacheEntryCl
21910 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a  ear(pParse, p);.
21911 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20        p->iReg = 
21912 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
21913 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61 63  /*.** When a cac
21914 68 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65  hed column is re
21915 75 73 65 64 2c 20 6d 61 6b 65 20 73 75 72 65 20  used, make sure 
21916 74 68 61 74 20 69 74 73 20 72 65 67 69 73 74 65  that its registe
21917 72 20 69 73 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67 65  r is.** no longe
21918 72 20 61 76 61 69 6c 61 62 6c 65 20 61 73 20 61  r available as a
21919 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 2e 20   temp register. 
2191a 20 74 69 63 6b 65 74 20 23 33 38 37 39 3a 20 20   ticket #3879:  
2191b 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20 72 65 67  that same.** reg
2191c 69 73 74 65 72 20 6d 69 67 68 74 20 62 65 20 69  ister might be i
2191d 6e 20 74 68 65 20 63 61 63 68 65 20 69 6e 20 6d  n the cache in m
2191e 75 6c 74 69 70 6c 65 20 70 6c 61 63 65 73 2c 20  ultiple places, 
2191f 73 6f 20 62 65 20 73 75 72 65 20 74 6f 0a 2a 2a  so be sure to.**
21920 20 67 65 74 20 74 68 65 6d 20 61 6c 6c 2e 0a 2a   get them all..*
21921 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
21922 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 69  lite3ExprCachePi
21923 6e 52 65 67 69 73 74 65 72 28 50 61 72 73 65 20  nRegister(Parse 
21924 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65  *pParse, int iRe
21925 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  g){.  int i;.  s
21926 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65 20  truct yColCache 
21927 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  *p;.  for(i=0, p
21928 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63  =pParse->aColCac
21929 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f 43  he; i<SQLITE_N_C
2192a 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70 2b  OLCACHE; i++, p+
2192b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69  +){.    if( p->i
2192c 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20 20 20  Reg==iReg ){.   
2192d 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20     p->tempReg = 
2192e 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  0;.    }.  }.}..
2192f 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
21930 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 78  ode that will ex
21931 74 72 61 63 74 20 74 68 65 20 69 43 6f 6c 75 6d  tract the iColum
21932 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d  n-th column from
21933 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 61  .** table pTab a
21934 6e 64 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c  nd store the col
21935 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 61 20 72  umn value in a r
21936 65 67 69 73 74 65 72 2e 20 20 41 6e 20 65 66 66  egister.  An eff
21937 6f 72 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74  ort.** is made t
21938 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75  o store the colu
21939 6d 6e 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  mn value in regi
2193a 73 74 65 72 20 69 52 65 67 2c 20 62 75 74 20 74  ster iReg, but t
2193b 68 69 73 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 75  his is.** not gu
2193c 61 72 61 6e 74 65 65 64 2e 20 20 54 68 65 20 6c  aranteed.  The l
2193d 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63  ocation of the c
2193e 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20 72  olumn value is r
2193f 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eturned..**.** T
21940 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e 20  here must be an 
21941 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 6f 20 70  open cursor to p
21942 54 61 62 20 69 6e 20 69 54 61 62 6c 65 20 77 68  Tab in iTable wh
21943 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  en this routine.
21944 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 49  ** is called.  I
21945 66 20 69 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e  f iColumn<0 then
21946 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61 74   code is generat
21947 65 64 20 74 68 61 74 20 65 78 74 72 61 63 74 73  ed that extracts
21948 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a   the rowid..**.*
21949 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  * This routine m
2194a 69 67 68 74 20 61 74 74 65 6d 70 74 20 74 6f 20  ight attempt to 
2194b 72 65 75 73 65 20 74 68 65 20 76 61 6c 75 65 20  reuse the value 
2194c 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68  of the column th
2194d 61 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64  at.** has alread
2194e 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e  y been loaded in
2194f 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20 20  to a register.  
21950 54 68 65 20 76 61 6c 75 65 20 77 69 6c 6c 20 61  The value will a
21951 6c 77 61 79 73 0a 2a 2a 20 62 65 20 75 73 65 64  lways.** be used
21952 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20 75   if it has not u
21953 6e 64 65 72 67 6f 6e 65 20 61 6e 79 20 61 66 66  ndergone any aff
21954 69 6e 69 74 79 20 63 68 61 6e 67 65 73 2e 20 20  inity changes.  
21955 42 75 74 20 69 66 0a 2a 2a 20 61 6e 20 61 66 66  But if.** an aff
21956 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73  inity change has
21957 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20   occurred, then 
21958 74 68 65 20 63 61 63 68 65 64 20 76 61 6c 75 65  the cached value
21959 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a   will only be.**
2195a 20 75 73 65 64 20 69 66 20 61 6c 6c 6f 77 41 66   used if allowAf
2195b 66 43 68 6e 67 20 69 73 20 74 72 75 65 2e 0a 2a  fChng is true..*
2195c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2195d 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
2195e 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 0a 20  CodeGetColumn(. 
2195f 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
21960 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64    /* Parsing and
21961 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67   code generating
21962 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
21963 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f  ble *pTab,     /
21964 2a 20 44 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  * Description of
21965 20 74 68 65 20 74 61 62 6c 65 20 77 65 20 61 72   the table we ar
21966 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a  e reading from *
21967 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c  /.  int iColumn,
21968 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
21969 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d   the table colum
2196a 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c  n */.  int iTabl
2196b 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  e,      /* The c
2196c 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74  ursor pointing t
2196d 6f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  o the table */. 
2196e 20 69 6e 74 20 69 52 65 67 2c 20 20 20 20 20 20   int iReg,      
2196f 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c    /* Store resul
21970 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  ts here */.  int
21971 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 2f 2a   allowAffChng /*
21972 20 54 72 75 65 20 69 66 20 70 72 69 6f 72 20 61   True if prior a
21973 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73 20  ffinity changes 
21974 61 72 65 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56  are OK */.){.  V
21975 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
21976 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b  >pVdbe;.  int i;
21977 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61  .  struct yColCa
21978 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69  che *p;..  for(i
21979 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43  =0, p=pParse->aC
2197a 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54  olCache; i<SQLIT
2197b 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b  E_N_COLCACHE; i+
2197c 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, p++){.    if(
2197d 20 70 2d 3e 69 52 65 67 3e 30 20 26 26 20 70 2d   p->iReg>0 && p-
2197e 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20  >iTable==iTable 
2197f 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69  && p->iColumn==i
21980 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 20  Column.         
21981 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 68 61    && (!p->affCha
21982 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 66 43  nge || allowAffC
21983 68 6e 67 29 20 29 7b 0a 20 20 20 20 20 20 70 2d  hng) ){.      p-
21984 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e 69  >lru = pParse->i
21985 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20  CacheCnt++;.    
21986 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
21987 68 65 50 69 6e 52 65 67 69 73 74 65 72 28 70 50  hePinRegister(pP
21988 61 72 73 65 2c 20 70 2d 3e 69 52 65 67 29 3b 0a  arse, p->iReg);.
21989 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e        return p->
2198a 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 20  iReg;.    }.  } 
2198b 20 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30   .  assert( v!=0
2198c 20 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75 6d   );.  if( iColum
2198d 6e 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  n<0 ){.    sqlit
2198e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2198f 4f 50 5f 52 6f 77 69 64 2c 20 69 54 61 62 6c 65  OP_Rowid, iTable
21990 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65  , iReg);.  }else
21991 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61 62   if( ALWAYS(pTab
21992 21 3d 30 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  !=0) ){.    int 
21993 6f 70 20 3d 20 49 73 56 69 72 74 75 61 6c 28 70  op = IsVirtual(p
21994 54 61 62 29 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d  Tab) ? OP_VColum
21995 6e 20 3a 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20  n : OP_Column;. 
21996 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21997 64 4f 70 33 28 76 2c 20 6f 70 2c 20 69 54 61 62  dOp3(v, op, iTab
21998 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65  le, iColumn, iRe
21999 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43  g);.    sqlite3C
2199a 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20  olumnDefault(v, 
2199b 70 54 61 62 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69  pTab, iColumn, i
2199c 52 65 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Reg);.  }.  sqli
2199d 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72  te3ExprCacheStor
2199e 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 6c 65  e(pParse, iTable
2199f 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29  , iColumn, iReg)
219a0 3b 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67 3b  ;.  return iReg;
219a1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
219a2 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  all column cache
219a3 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c   entries..*/.SQL
219a4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
219a5 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
219a6 65 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50  eClear(Parse *pP
219a7 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  arse){.  int i;.
219a8 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63    struct yColCac
219a9 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d  he *p;..  for(i=
219aa 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f  0, p=pParse->aCo
219ab 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45  lCache; i<SQLITE
219ac 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b  _N_COLCACHE; i++
219ad 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  , p++){.    if( 
219ae 70 2d 3e 69 52 65 67 20 29 7b 0a 20 20 20 20 20  p->iReg ){.     
219af 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61 72   cacheEntryClear
219b0 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20  (pParse, p);.   
219b1 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b 0a     p->iReg = 0;.
219b2 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
219b3 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66 61  ** Record the fa
219b4 63 74 20 74 68 61 74 20 61 6e 20 61 66 66 69 6e  ct that an affin
219b5 69 74 79 20 63 68 61 6e 67 65 20 68 61 73 20 6f  ity change has o
219b6 63 63 75 72 72 65 64 20 6f 6e 20 69 43 6f 75 6e  ccurred on iCoun
219b7 74 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20 73  t.** registers s
219b8 74 61 72 74 69 6e 67 20 77 69 74 68 20 69 53 74  tarting with iSt
219b9 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  art..*/.SQLITE_P
219ba 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
219bb 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
219bc 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72 73 65  nityChange(Parse
219bd 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 53   *pParse, int iS
219be 74 61 72 74 2c 20 69 6e 74 20 69 43 6f 75 6e 74  tart, int iCount
219bf 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d 20  ){.  int iEnd = 
219c0 69 53 74 61 72 74 20 2b 20 69 43 6f 75 6e 74 20  iStart + iCount 
219c1 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  - 1;.  int i;.  
219c2 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
219c3 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20   *p;.  for(i=0, 
219c4 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
219c5 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
219c6 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
219c7 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 20 3d  ++){.    int r =
219c8 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69 66   p->iReg;.    if
219c9 28 20 72 3e 3d 69 53 74 61 72 74 20 26 26 20 72  ( r>=iStart && r
219ca 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 20 20 20 20  <=iEnd ){.      
219cb 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20 31  p->affChange = 1
219cc 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
219cd 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
219ce 64 65 20 74 6f 20 6d 6f 76 65 20 63 6f 6e 74 65  de to move conte
219cf 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72  nt from register
219d0 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b  s iFrom...iFrom+
219d1 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 72 20 74  nReg-1.** over t
219d2 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67 2d  o iTo..iTo+nReg-
219d3 31 2e 20 4b 65 65 70 20 74 68 65 20 63 6f 6c 75  1. Keep the colu
219d4 6d 6e 20 63 61 63 68 65 20 75 70 2d 74 6f 2d 64  mn cache up-to-d
219d5 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ate..*/.SQLITE_P
219d6 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
219d7 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
219d8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
219d9 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54  nt iFrom, int iT
219da 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20  o, int nReg){.  
219db 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
219dc 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20  yColCache *p;.  
219dd 69 66 28 20 4e 45 56 45 52 28 69 46 72 6f 6d 3d  if( NEVER(iFrom=
219de 3d 69 54 6f 29 20 29 20 72 65 74 75 72 6e 3b 0a  =iTo) ) return;.
219df 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
219e0 4f 70 33 28 70 50 61 72 73 65 2d 3e 70 56 64 62  Op3(pParse->pVdb
219e1 65 2c 20 4f 50 5f 4d 6f 76 65 2c 20 69 46 72 6f  e, OP_Move, iFro
219e2 6d 2c 20 69 54 6f 2c 20 6e 52 65 67 29 3b 0a 20  m, iTo, nReg);. 
219e3 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61 72   for(i=0, p=pPar
219e4 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69  se->aColCache; i
219e5 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43  <SQLITE_N_COLCAC
219e6 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20  HE; i++, p++){. 
219e7 20 20 20 69 6e 74 20 78 20 3d 20 70 2d 3e 69 52     int x = p->iR
219e8 65 67 3b 0a 20 20 20 20 69 66 28 20 78 3e 3d 69  eg;.    if( x>=i
219e9 46 72 6f 6d 20 26 26 20 78 3c 69 46 72 6f 6d 2b  From && x<iFrom+
219ea 6e 52 65 67 20 29 7b 0a 20 20 20 20 20 20 70 2d  nReg ){.      p-
219eb 3e 69 52 65 67 20 2b 3d 20 69 54 6f 2d 69 46 72  >iReg += iTo-iFr
219ec 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  om;.    }.  }.}.
219ed 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
219ee 63 6f 64 65 20 74 6f 20 63 6f 70 79 20 63 6f 6e  code to copy con
219ef 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 74  tent from regist
219f0 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f  ers iFrom...iFro
219f1 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 72  m+nReg-1.** over
219f2 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65   to iTo..iTo+nRe
219f3 67 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  g-1..*/.SQLITE_P
219f4 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
219f5 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 28  te3ExprCodeCopy(
219f6 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
219f7 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54  nt iFrom, int iT
219f8 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20  o, int nReg){.  
219f9 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 4e 45 56  int i;.  if( NEV
219fa 45 52 28 69 46 72 6f 6d 3d 3d 69 54 6f 29 20 29  ER(iFrom==iTo) )
219fb 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69   return;.  for(i
219fc 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b 2b 29  =0; i<nReg; i++)
219fd 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
219fe 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e  eAddOp2(pParse->
219ff 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c 20  pVdbe, OP_Copy, 
21a00 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 29 3b  iFrom+i, iTo+i);
21a01 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
21a02 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 79  turn true if any
21a03 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65   register in the
21a04 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69 54   range iFrom..iT
21a05 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a 2a  o (inclusive).**
21a06 20 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74   is used as part
21a07 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63   of the column c
21a08 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ache..*/.static 
21a09 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e  int usedAsColumn
21a0a 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61  Cache(Parse *pPa
21a0b 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20  rse, int iFrom, 
21a0c 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74 20  int iTo){.  int 
21a0d 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  i;.  struct yCol
21a0e 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28  Cache *p;.  for(
21a0f 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
21a10 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
21a11 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
21a12 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 6e  ++, p++){.    in
21a13 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a 20  t r = p->iReg;. 
21a14 20 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d 20     if( r>=iFrom 
21a15 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74 75  && r<=iTo ) retu
21a16 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 1;.  }.  retu
21a17 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  rn 0;.}../*.** I
21a18 66 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74 72  f the last instr
21a19 75 63 74 69 6f 6e 20 63 6f 64 65 64 20 69 73 20  uction coded is 
21a1a 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 63 6f 70  an ephemeral cop
21a1b 79 20 6f 66 20 61 6e 79 20 6f 66 0a 2a 2a 20 74  y of any of.** t
21a1c 68 65 20 72 65 67 69 73 74 65 72 73 20 69 6e 20  he registers in 
21a1d 74 68 65 20 6e 52 65 67 20 72 65 67 69 73 74 65  the nReg registe
21a1e 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  rs beginning wit
21a1f 68 20 69 52 65 67 2c 20 74 68 65 6e 0a 2a 2a 20  h iReg, then.** 
21a20 63 6f 6e 76 65 72 74 20 74 68 65 20 6c 61 73 74  convert the last
21a21 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 72 6f   instruction fro
21a22 6d 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20 4f 50  m OP_SCopy to OP
21a23 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  _Copy..*/.SQLITE
21a24 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
21a25 6c 69 74 65 33 45 78 70 72 48 61 72 64 43 6f 70  lite3ExprHardCop
21a26 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  y(Parse *pParse,
21a27 20 69 6e 74 20 69 52 65 67 2c 20 69 6e 74 20 6e   int iReg, int n
21a28 52 65 67 29 7b 0a 20 20 56 64 62 65 4f 70 20 2a  Reg){.  VdbeOp *
21a29 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  pOp;.  Vdbe *v;.
21a2a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
21a2b 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
21a2c 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 76 20 3d 20  led==0 );.  v = 
21a2d 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
21a2e 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
21a2f 0a 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33  .  pOp = sqlite3
21a30 56 64 62 65 47 65 74 4f 70 28 76 2c 20 2d 31 29  VdbeGetOp(v, -1)
21a31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 21  ;.  assert( pOp!
21a32 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d  =0 );.  if( pOp-
21a33 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 43 6f 70  >opcode==OP_SCop
21a34 79 20 26 26 20 70 4f 70 2d 3e 70 31 3e 3d 69 52  y && pOp->p1>=iR
21a35 65 67 20 26 26 20 70 4f 70 2d 3e 70 31 3c 69 52  eg && pOp->p1<iR
21a36 65 67 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20 70  eg+nReg ){.    p
21a37 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f  Op->opcode = OP_
21a38 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  Copy;.  }.}../*.
21a39 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
21a3a 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 76 61   to store the va
21a3b 6c 75 65 20 6f 66 20 74 68 65 20 69 41 6c 69 61  lue of the iAlia
21a3c 73 2d 74 68 20 61 6c 69 61 73 20 69 6e 20 72 65  s-th alias in re
21a3d 67 69 73 74 65 72 0a 2a 2a 20 74 61 72 67 65 74  gister.** target
21a3e 2e 20 20 54 68 65 20 66 69 72 73 74 20 74 69 6d  .  The first tim
21a3f 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  e this is called
21a40 2c 20 70 45 78 70 72 20 69 73 20 65 76 61 6c 75  , pExpr is evalu
21a41 61 74 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 0a  ated to compute.
21a42 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
21a43 74 68 65 20 61 6c 69 61 73 2e 20 20 54 68 65 20  the alias.  The 
21a44 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20  value is stored 
21a45 69 6e 20 61 6e 20 61 75 78 69 6c 69 61 72 79 20  in an auxiliary 
21a46 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64 20  register.** and 
21a47 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  the number of th
21a48 61 74 20 72 65 67 69 73 74 65 72 20 69 73 20 72  at register is r
21a49 65 74 75 72 6e 65 64 2e 20 20 4f 6e 20 73 75 62  eturned.  On sub
21a4a 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 2c 0a 2a  sequent calls,.*
21a4b 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e  * the register n
21a4c 75 6d 62 65 72 20 69 73 20 72 65 74 75 72 6e 65  umber is returne
21a4d 64 20 77 69 74 68 6f 75 74 20 67 65 6e 65 72 61  d without genera
21a4e 74 69 6e 67 20 61 6e 79 20 63 6f 64 65 2e 0a 2a  ting any code..*
21a4f 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69  *.** Note that i
21a50 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73  n order for this
21a51 20 74 6f 20 77 6f 72 6b 2c 20 63 6f 64 65 20 6d   to work, code m
21a52 75 73 74 20 62 65 20 67 65 6e 65 72 61 74 65 64  ust be generated
21a53 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   in the.** same 
21a54 6f 72 64 65 72 20 74 68 61 74 20 69 74 20 69 73  order that it is
21a55 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a 0a 2a 2a   executed..**.**
21a56 20 41 6c 69 61 73 65 73 20 61 72 65 20 6e 75 6d   Aliases are num
21a57 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20 77  bered starting w
21a58 69 74 68 20 31 2e 20 20 53 6f 20 69 41 6c 69 61  ith 1.  So iAlia
21a59 73 20 69 73 20 69 6e 20 74 68 65 20 72 61 6e 67  s is in the rang
21a5a 65 0a 2a 2a 20 6f 66 20 31 20 74 6f 20 70 50 61  e.** of 1 to pPa
21a5b 72 73 65 2d 3e 6e 41 6c 69 61 73 20 69 6e 63 6c  rse->nAlias incl
21a5c 75 73 69 76 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 70  usive.  .**.** p
21a5d 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41  Parse->aAlias[iA
21a5e 6c 69 61 73 2d 31 5d 20 72 65 63 6f 72 64 73 20  lias-1] records 
21a5f 74 68 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d  the register num
21a60 62 65 72 20 77 68 65 72 65 20 74 68 65 20 76 61  ber where the va
21a61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 69 41  lue.** of the iA
21a62 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69 73  lias-th alias is
21a63 20 73 74 6f 72 65 64 2e 20 20 49 66 20 7a 65 72   stored.  If zer
21a64 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  o, that means th
21a65 61 74 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20  at the.** alias 
21a66 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
21a67 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2f 0a 73 74   computed..*/.st
21a68 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c 69  atic int codeAli
21a69 61 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  as(Parse *pParse
21a6a 2c 20 69 6e 74 20 69 41 6c 69 61 73 2c 20 45 78  , int iAlias, Ex
21a6b 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74  pr *pExpr, int t
21a6c 61 72 67 65 74 29 7b 0a 23 69 66 20 30 0a 20 20  arget){.#if 0.  
21a6d 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
21a6e 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
21a6f 69 52 65 67 3b 0a 20 20 69 66 28 20 70 50 61 72  iReg;.  if( pPar
21a70 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 3c  se->nAliasAlloc<
21a71 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29  pParse->nAlias )
21a72 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 41  {.    pParse->aA
21a73 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62  lias = sqlite3Db
21a74 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64 62  ReallocOrFree(db
21a75 2c 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73  , pParse->aAlias
21a76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
21a77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21a78 20 20 20 73 69 7a 65 6f 66 28 70 50 61 72 73 65     sizeof(pParse
21a79 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 2a 70 50 61  ->aAlias[0])*pPa
21a7a 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20  rse->nAlias );. 
21a7b 20 20 20 74 65 73 74 63 61 73 65 28 20 64 62 2d     testcase( db-
21a7c 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26  >mallocFailed &&
21a7d 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41   pParse->nAliasA
21a7e 6c 6c 6f 63 3e 30 20 29 3b 0a 20 20 20 20 69 66  lloc>0 );.    if
21a7f 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
21a80 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  ed ) return 0;. 
21a81 20 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73     memset(&pPars
21a82 65 2d 3e 61 41 6c 69 61 73 5b 70 50 61 72 73 65  e->aAlias[pParse
21a83 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 5d 2c 20  ->nAliasAlloc], 
21a84 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 70  0,.           (p
21a85 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 2d 70 50  Parse->nAlias-pP
21a86 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f  arse->nAliasAllo
21a87 63 29 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65  c)*sizeof(pParse
21a88 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 29 3b 0a 20  ->aAlias[0]));. 
21a89 20 20 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61     pParse->nAlia
21a8a 73 41 6c 6c 6f 63 20 3d 20 70 50 61 72 73 65 2d  sAlloc = pParse-
21a8b 3e 6e 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 61  >nAlias;.  }.  a
21a8c 73 73 65 72 74 28 20 69 41 6c 69 61 73 3e 30 20  ssert( iAlias>0 
21a8d 26 26 20 69 41 6c 69 61 73 3c 3d 70 50 61 72 73  && iAlias<=pPars
21a8e 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 69  e->nAlias );.  i
21a8f 52 65 67 20 3d 20 70 50 61 72 73 65 2d 3e 61 41  Reg = pParse->aA
21a90 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 3b 0a  lias[iAlias-1];.
21a91 20 20 69 66 28 20 69 52 65 67 3d 3d 30 20 29 7b    if( iReg==0 ){
21a92 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d  .    if( pParse-
21a93 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e 30 20 29  >iCacheLevel>0 )
21a94 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20 73  {.      iReg = s
21a95 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
21a96 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
21a97 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  pr, target);.   
21a98 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 52   }else{.      iR
21a99 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  eg = ++pParse->n
21a9a 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Mem;.      sqlit
21a9b 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
21a9c 65 2c 20 70 45 78 70 72 2c 20 69 52 65 67 29 3b  e, pExpr, iReg);
21a9d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61  .      pParse->a
21a9e 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20  Alias[iAlias-1] 
21a9f 3d 20 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20  = iReg;.    }.  
21aa0 7d 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67 3b  }.  return iReg;
21aa1 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f  .#else.  UNUSED_
21aa2 50 41 52 41 4d 45 54 45 52 28 69 41 6c 69 61 73  PARAMETER(iAlias
21aa3 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
21aa4 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65  te3ExprCodeTarge
21aa5 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  t(pParse, pExpr,
21aa6 20 74 61 72 67 65 74 29 3b 0a 23 65 6e 64 69 66   target);.#endif
21aa7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
21aa8 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 65  te code into the
21aa9 20 63 75 72 72 65 6e 74 20 56 64 62 65 20 74 6f   current Vdbe to
21aaa 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 67 69   evaluate the gi
21aab 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  ven.** expressio
21aac 6e 2e 20 20 41 74 74 65 6d 70 74 20 74 6f 20 73  n.  Attempt to s
21aad 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
21aae 20 69 6e 20 72 65 67 69 73 74 65 72 20 22 74 61   in register "ta
21aaf 72 67 65 74 22 2e 0a 2a 2a 20 52 65 74 75 72 6e  rget"..** Return
21ab0 20 74 68 65 20 72 65 67 69 73 74 65 72 20 77 68   the register wh
21ab1 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  ere results are 
21ab2 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 69  stored..**.** Wi
21ab3 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  th this routine,
21ab4 20 74 68 65 72 65 20 69 73 20 6e 6f 20 67 75 61   there is no gua
21ab5 72 61 6e 74 65 65 20 74 68 61 74 20 72 65 73 75  rantee that resu
21ab6 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 73  lts will.** be s
21ab7 74 6f 72 65 64 20 69 6e 20 74 61 72 67 65 74 2e  tored in target.
21ab8 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 69 67    The result mig
21ab9 68 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20  ht be stored in 
21aba 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 72 65  some other.** re
21abb 67 69 73 74 65 72 20 69 66 20 69 74 20 69 73 20  gister if it is 
21abc 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64 6f  convenient to do
21abd 20 73 6f 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e   so.  The callin
21abe 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75  g function.** mu
21abf 73 74 20 63 68 65 63 6b 20 74 68 65 20 72 65 74  st check the ret
21ac0 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 6d 6f 76  urn code and mov
21ac1 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 74 6f  e the results to
21ac2 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a 20   the desired.** 
21ac3 72 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c  register..*/.SQL
21ac4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
21ac5 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
21ac6 61 72 67 65 74 28 50 61 72 73 65 20 2a 70 50 61  arget(Parse *pPa
21ac7 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
21ac8 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20  , int target){. 
21ac9 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
21aca 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 54 68  e->pVdbe;  /* Th
21acb 65 20 56 4d 20 75 6e 64 65 72 20 63 6f 6e 73 74  e VM under const
21acc 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  ruction */.  int
21acd 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   op;            
21ace 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70         /* The op
21acf 63 6f 64 65 20 62 65 69 6e 67 20 63 6f 64 65 64  code being coded
21ad0 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 52 65 67 20   */.  int inReg 
21ad1 3d 20 74 61 72 67 65 74 3b 20 20 20 20 20 20 20  = target;       
21ad2 2f 2a 20 52 65 73 75 6c 74 73 20 73 74 6f 72 65  /* Results store
21ad3 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 6e  d in register in
21ad4 52 65 67 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  Reg */.  int reg
21ad5 46 72 65 65 31 20 3d 20 30 3b 20 20 20 20 20 20  Free1 = 0;      
21ad6 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72     /* If non-zer
21ad7 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d 70  o free this temp
21ad8 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20 2a  orary register *
21ad9 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 32  /.  int regFree2
21ada 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
21adb 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65   If non-zero fre
21adc 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79  e this temporary
21add 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69   register */.  i
21ade 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 2c 20 72  nt r1, r2, r3, r
21adf 34 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69  4;       /* Vari
21ae0 6f 75 73 20 72 65 67 69 73 74 65 72 20 6e 75 6d  ous register num
21ae1 62 65 72 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  bers */.  sqlite
21ae2 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
21ae3 64 62 3b 20 2f 2a 20 54 68 65 20 64 61 74 61 62  db; /* The datab
21ae4 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
21ae5 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 74 61 72  /..  assert( tar
21ae6 67 65 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c  get>0 && target<
21ae7 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b  =pParse->nMem );
21ae8 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20  .  if( v==0 ){. 
21ae9 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73     assert( pPars
21aea 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
21aeb 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 75 72  led );.    retur
21aec 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  n 0;.  }..  if( 
21aed 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20  pExpr==0 ){.    
21aee 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20 20  op = TK_NULL;.  
21aef 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 70 20 3d 20  }else{.    op = 
21af0 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 7d 0a 20  pExpr->op;.  }. 
21af1 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20   switch( op ){. 
21af2 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43     case TK_AGG_C
21af3 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 41  OLUMN: {.      A
21af4 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
21af5 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e   = pExpr->pAggIn
21af6 66 6f 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74  fo;.      struct
21af7 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43   AggInfo_col *pC
21af8 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e  ol = &pAggInfo->
21af9 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 41 67 67  aCol[pExpr->iAgg
21afa 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 41  ];.      if( !pA
21afb 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f  ggInfo->directMo
21afc 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  de ){.        as
21afd 73 65 72 74 28 20 70 43 6f 6c 2d 3e 69 4d 65 6d  sert( pCol->iMem
21afe 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69 6e  >0 );.        in
21aff 52 65 67 20 3d 20 70 43 6f 6c 2d 3e 69 4d 65 6d  Reg = pCol->iMem
21b00 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
21b01 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
21b02 20 70 41 67 67 49 6e 66 6f 2d 3e 75 73 65 53 6f   pAggInfo->useSo
21b03 72 74 69 6e 67 49 64 78 20 29 7b 0a 20 20 20 20  rtingIdx ){.    
21b04 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21b05 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
21b06 6d 6e 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 73 6f  mn, pAggInfo->so
21b07 72 74 69 6e 67 49 64 78 2c 0a 20 20 20 20 20 20  rtingIdx,.      
21b08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b09 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53          pCol->iS
21b0a 6f 72 74 65 72 43 6f 6c 75 6d 6e 2c 20 74 61 72  orterColumn, tar
21b0b 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72  get);.        br
21b0c 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
21b0d 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c     /* Otherwise,
21b0e 20 66 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20   fall thru into 
21b0f 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 63 61  the TK_COLUMN ca
21b10 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  se */.    }.    
21b11 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20  case TK_COLUMN: 
21b12 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  {.      if( pExp
21b13 72 2d 3e 69 54 61 62 6c 65 3c 30 20 29 7b 0a 20  r->iTable<0 ){. 
21b14 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 6f         /* This o
21b15 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65 6e  nly happens when
21b16 20 63 6f 64 69 6e 67 20 63 68 65 63 6b 20 63 6f   coding check co
21b17 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 20  nstraints */.   
21b18 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
21b19 72 73 65 2d 3e 63 6b 42 61 73 65 3e 30 20 29 3b  rse->ckBase>0 );
21b1a 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d  .        inReg =
21b1b 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
21b1c 2b 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73 65  + pParse->ckBase
21b1d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
21b1e 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
21b1f 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26   (pExpr->flags &
21b20 20 45 50 5f 41 6e 79 41 66 66 29 21 3d 30 20 29   EP_AnyAff)!=0 )
21b21 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20  ;.        inReg 
21b22 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
21b23 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73  eGetColumn(pPars
21b24 65 2c 20 70 45 78 70 72 2d 3e 70 54 61 62 2c 0a  e, pExpr->pTab,.
21b25 20 20 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 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c   pExpr->iColumn,
21b28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20   pExpr->iTable, 
21b29 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20 20  target,.        
21b2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b2b 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
21b2c 66 6c 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66  flags & EP_AnyAf
21b2d 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  f);.      }.    
21b2e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
21b2f 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 47     case TK_INTEG
21b30 45 52 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64 65  ER: {.      code
21b31 49 6e 74 65 67 65 72 28 76 2c 20 70 45 78 70 72  Integer(v, pExpr
21b32 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 0, target);.  
21b33 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21b34 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c 4f  .    case TK_FLO
21b35 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65  AT: {.      asse
21b36 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
21b37 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49  erty(pExpr, EP_I
21b38 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20  ntValue) );.    
21b39 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 70 45    codeReal(v, pE
21b3a 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 30  xpr->u.zToken, 0
21b3b 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21b3c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21b3d 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47    case TK_STRING
21b3e 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
21b3f 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
21b40 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74  ty(pExpr, EP_Int
21b41 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20  Value) );.      
21b42 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21b43 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
21b44 20 30 2c 20 74 61 72 67 65 74 2c 20 30 2c 20 70   0, target, 0, p
21b45 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20  Expr->u.zToken, 
21b46 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  0);.      break;
21b47 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
21b48 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20  TK_NULL: {.     
21b49 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21b4a 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
21b4b 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21b4c 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69   break;.    }.#i
21b4d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
21b4e 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20  T_BLOB_LITERAL. 
21b4f 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a     case TK_BLOB:
21b50 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a   {.      int n;.
21b51 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
21b52 20 2a 7a 3b 0a 20 20 20 20 20 20 63 68 61 72 20   *z;.      char 
21b53 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 20 20 61 73  *zBlob;.      as
21b54 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
21b55 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
21b56 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20  _IntValue) );.  
21b57 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
21b58 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d  r->u.zToken[0]==
21b59 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75 2e  'x' || pExpr->u.
21b5a 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20 29  zToken[0]=='X' )
21b5b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21b5c 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b  pExpr->u.zToken[
21b5d 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20  1]=='\'' );.    
21b5e 20 20 7a 20 3d 20 26 70 45 78 70 72 2d 3e 75 2e    z = &pExpr->u.
21b5f 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a 20 20 20 20 20  zToken[2];.     
21b60 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
21b61 65 6e 33 30 28 7a 29 20 2d 20 31 3b 0a 20 20 20  en30(z) - 1;.   
21b62 20 20 20 61 73 73 65 72 74 28 20 7a 5b 6e 5d 3d     assert( z[n]=
21b63 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20 7a  ='\'' );.      z
21b64 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 48 65  Blob = sqlite3He
21b65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 56  xToBlob(sqlite3V
21b66 64 62 65 44 62 28 76 29 2c 20 7a 2c 20 6e 29 3b  dbeDb(v), z, n);
21b67 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
21b68 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 42  beAddOp4(v, OP_B
21b69 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72 67 65 74  lob, n/2, target
21b6a 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50 34 5f 44  , 0, zBlob, P4_D
21b6b 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 62  YNAMIC);.      b
21b6c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
21b6d 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 56  if.    case TK_V
21b6e 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 20 20 20  ARIABLE: {.     
21b6f 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20   VdbeOp *pOp;.  
21b70 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
21b71 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
21b72 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pr, EP_IntValue)
21b73 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21b74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  ( pExpr->u.zToke
21b75 6e 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73  n!=0 );.      as
21b76 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a  sert( pExpr->u.z
21b77 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29 3b 0a 20  Token[0]!=0 );. 
21b78 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
21b79 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 30 0a 20  u.zToken[1]==0. 
21b7a 20 20 20 20 20 20 20 20 26 26 20 28 70 4f 70 20          && (pOp 
21b7b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65 74  = sqlite3VdbeGet
21b7c 4f 70 28 76 2c 20 2d 31 29 29 2d 3e 6f 70 63 6f  Op(v, -1))->opco
21b7d 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65 0a  de==OP_Variable.
21b7e 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d           && pOp-
21b7f 3e 70 31 2b 70 4f 70 2d 3e 70 33 3d 3d 70 45 78  >p1+pOp->p3==pEx
21b80 70 72 2d 3e 69 43 6f 6c 75 6d 6e 0a 20 20 20 20  pr->iColumn.    
21b81 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 32 2b       && pOp->p2+
21b82 70 4f 70 2d 3e 70 33 3d 3d 74 61 72 67 65 74 0a  pOp->p3==target.
21b83 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70 2d           && pOp-
21b84 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20 20 20 20 29  >p4.z==0.      )
21b85 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
21b86 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e 73  the previous ins
21b87 74 72 75 63 74 69 6f 6e 20 77 61 73 20 61 20 63  truction was a c
21b88 6f 70 79 20 6f 66 20 74 68 65 20 70 72 65 76 69  opy of the previ
21b89 6f 75 73 20 75 6e 6e 61 6d 65 64 0a 20 20 20 20  ous unnamed.    
21b8a 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72      ** parameter
21b8b 20 69 6e 74 6f 20 74 68 65 20 70 72 65 76 69 6f   into the previo
21b8c 75 73 20 72 65 67 69 73 74 65 72 2c 20 74 68 65  us register, the
21b8d 6e 20 73 69 6d 70 6c 79 20 69 6e 63 72 65 6d 65  n simply increme
21b8e 6e 74 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  nt the.        *
21b8f 2a 20 72 65 70 65 61 74 20 63 6f 75 6e 74 20 6f  * repeat count o
21b90 6e 20 74 68 65 20 70 72 69 6f 72 20 69 6e 73 74  n the prior inst
21b91 72 75 63 74 69 6f 6e 20 72 61 74 68 65 72 20 74  ruction rather t
21b92 68 61 6e 20 6d 61 6b 69 6e 67 20 61 20 6e 65 77  han making a new
21b93 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73 74  .        ** inst
21b94 72 75 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20  ruction..       
21b95 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f 70 2d   */.        pOp-
21b96 3e 70 33 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c  >p3++;.      }el
21b97 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
21b98 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
21b99 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 70 45   OP_Variable, pE
21b9a 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 74 61  xpr->iColumn, ta
21b9b 72 67 65 74 2c 20 31 29 3b 0a 20 20 20 20 20 20  rget, 1);.      
21b9c 20 20 69 66 28 20 70 45 78 70 72 2d 3e 75 2e 7a    if( pExpr->u.z
21b9d 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20 29 7b 0a 20  Token[1]!=0 ){. 
21b9e 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
21b9f 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
21ba0 2d 31 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f  -1, pExpr->u.zTo
21ba1 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ken, 0);.       
21ba2 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
21ba3 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21ba4 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54    case TK_REGIST
21ba5 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52 65  ER: {.      inRe
21ba6 67 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  g = pExpr->iTabl
21ba7 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  e;.      break;.
21ba8 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21ba9 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 20 20 69 6e  K_AS: {.      in
21baa 52 65 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28  Reg = codeAlias(
21bab 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 69  pParse, pExpr->i
21bac 54 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Table, pExpr->pL
21bad 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  eft, target);.  
21bae 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21baf 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
21bb0 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20 20 63 61  OMIT_CAST.    ca
21bb1 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b 0a 20 20  se TK_CAST: {.  
21bb2 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
21bb3 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 20  ns of the form: 
21bb4 20 20 43 41 53 54 28 70 4c 65 66 74 20 41 53 20    CAST(pLeft AS 
21bb5 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20 20 20 20  token) */.      
21bb6 69 6e 74 20 61 66 66 2c 20 74 6f 5f 6f 70 3b 0a  int aff, to_op;.
21bb7 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71        inReg = sq
21bb8 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72  lite3ExprCodeTar
21bb9 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70  get(pParse, pExp
21bba 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74  r->pLeft, target
21bbb 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21bbc 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
21bbd 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56  y(pExpr, EP_IntV
21bbe 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 61  alue) );.      a
21bbf 66 66 20 3d 20 73 71 6c 69 74 65 33 41 66 66 69  ff = sqlite3Affi
21bc0 6e 69 74 79 54 79 70 65 28 70 45 78 70 72 2d 3e  nityType(pExpr->
21bc1 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  u.zToken);.     
21bc2 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 2d 20 53   to_op = aff - S
21bc3 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 2b  QLITE_AFF_TEXT +
21bc4 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 20 20 20   OP_ToText;.    
21bc5 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d    assert( to_op=
21bc6 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 7c 7c  =OP_ToText    ||
21bc7 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46   aff!=SQLITE_AFF
21bc8 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 20 20 20  _TEXT    );.    
21bc9 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d    assert( to_op=
21bca 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 7c 7c  =OP_ToBlob    ||
21bcb 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46   aff!=SQLITE_AFF
21bcc 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 20 20 20  _NONE    );.    
21bcd 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d    assert( to_op=
21bce 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 7c 7c  =OP_ToNumeric ||
21bcf 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46   aff!=SQLITE_AFF
21bd0 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 20  _NUMERIC );.    
21bd1 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d    assert( to_op=
21bd2 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 7c 7c  =OP_ToInt     ||
21bd3 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46   aff!=SQLITE_AFF
21bd4 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20 20  _INTEGER );.    
21bd5 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70 3d    assert( to_op=
21bd6 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 7c 7c  =OP_ToReal    ||
21bd7 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46 46   aff!=SQLITE_AFF
21bd8 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 20 20 20  _REAL    );.    
21bd9 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f    testcase( to_o
21bda 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 29 3b 0a  p==OP_ToText );.
21bdb 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21bdc 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f 62  to_op==OP_ToBlob
21bdd 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21bde 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f  se( to_op==OP_To
21bdf 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 20 20 20  Numeric );.     
21be0 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f 70   testcase( to_op
21be1 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b 0a 20 20  ==OP_ToInt );.  
21be2 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f      testcase( to
21be3 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 29  _op==OP_ToReal )
21be4 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e 52 65  ;.      if( inRe
21be5 67 21 3d 74 61 72 67 65 74 20 29 7b 0a 20 20 20  g!=target ){.   
21be6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21be7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f  AddOp2(v, OP_SCo
21be8 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67 65  py, inReg, targe
21be9 74 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65  t);.        inRe
21bea 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20 20  g = target;.    
21beb 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
21bec 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 74  3VdbeAddOp1(v, t
21bed 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b 0a 20 20  o_op, inReg);.  
21bee 20 20 20 20 74 65 73 74 63 61 73 65 28 20 75 73      testcase( us
21bef 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65 28  edAsColumnCache(
21bf0 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 69  pParse, inReg, i
21bf1 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 20 20 73  nReg) );.      s
21bf2 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
21bf3 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
21bf4 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 31 29 3b  arse, inReg, 1);
21bf5 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21bf6 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
21bf7 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a  LITE_OMIT_CAST *
21bf8 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54  /.    case TK_LT
21bf9 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45  :.    case TK_LE
21bfa 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54  :.    case TK_GT
21bfb 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45  :.    case TK_GE
21bfc 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45  :.    case TK_NE
21bfd 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51  :.    case TK_EQ
21bfe 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
21bff 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20 29  ( TK_LT==OP_Lt )
21c00 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21c01 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b 0a  TK_LE==OP_Le );.
21c02 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
21c03 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20  _GT==OP_Gt );.  
21c04 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47      assert( TK_G
21c05 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20 20  E==OP_Ge );.    
21c06 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51 3d    assert( TK_EQ=
21c07 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20 20  =OP_Eq );.      
21c08 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d 4f  assert( TK_NE==O
21c09 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74 65  P_Ne );.      te
21c0a 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c  stcase( op==TK_L
21c0b 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
21c0c 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29  ase( op==TK_LE )
21c0d 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21c0e 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20  ( op==TK_GT );. 
21c0f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21c10 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20 20  p==TK_GE );.    
21c11 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21c12 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 74  TK_EQ );.      t
21c13 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21c14 4e 45 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65  NE );.      code
21c15 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73 28  CompareOperands(
21c16 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
21c17 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67 46  Left, &r1, &regF
21c18 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20 20  ree1,.          
21c19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c1a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70          pExpr->p
21c1b 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65 67  Right, &r2, &reg
21c1c 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63 6f  Free2);.      co
21c1d 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65  deCompare(pParse
21c1e 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
21c1f 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f  pExpr->pRight, o
21c20 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  p,.             
21c21 20 20 20 20 20 72 31 2c 20 72 32 2c 20 69 6e 52       r1, r2, inR
21c22 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52 45  eg, SQLITE_STORE
21c23 50 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  P2);.      testc
21c24 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
21c25 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21c26 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20  se( regFree2==0 
21c27 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21c28 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21c29 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73 65 20 54  K_IS:.    case T
21c2a 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20 20 20 20  K_ISNOT: {.     
21c2b 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21c2c 4b 5f 49 53 20 29 3b 0a 20 20 20 20 20 20 74 65  K_IS );.      te
21c2d 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49  stcase( op==TK_I
21c2e 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 63 6f  SNOT );.      co
21c2f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64  deCompareOperand
21c30 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  s(pParse, pExpr-
21c31 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65  >pLeft, &r1, &re
21c32 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20  gFree1,.        
21c33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c34 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
21c35 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72  >pRight, &r2, &r
21c36 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
21c37 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b 5f 49 53 29  op = (op==TK_IS)
21c38 20 3f 20 54 4b 5f 45 51 20 3a 20 54 4b 5f 4e 45   ? TK_EQ : TK_NE
21c39 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
21c3a 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  are(pParse, pExp
21c3b 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d  r->pLeft, pExpr-
21c3c 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20  >pRight, op,.   
21c3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
21c3e 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 53 51  1, r2, inReg, SQ
21c3f 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 7c 20 53  LITE_STOREP2 | S
21c40 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20  QLITE_NULLEQ);. 
21c41 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
21c42 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
21c43 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
21c44 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20  gFree2==0 );.   
21c45 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21c46 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a      case TK_AND:
21c47 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a  .    case TK_OR:
21c48 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 50 4c 55  .    case TK_PLU
21c49 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  S:.    case TK_S
21c4a 54 41 52 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  TAR:.    case TK
21c4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63 61 73 65  _MINUS:.    case
21c4c 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 63 61 73   TK_REM:.    cas
21c4d 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a 20 20 20  e TK_BITAND:.   
21c4e 20 63 61 73 65 20 54 4b 5f 42 49 54 4f 52 3a 0a   case TK_BITOR:.
21c4f 20 20 20 20 63 61 73 65 20 54 4b 5f 53 4c 41 53      case TK_SLAS
21c50 48 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c  H:.    case TK_L
21c51 53 48 49 46 54 3a 0a 20 20 20 20 63 61 73 65 20  SHIFT:.    case 
21c52 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20 20 20 20  TK_RSHIFT: .    
21c53 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41 54 3a 20  case TK_CONCAT: 
21c54 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
21c55 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e 64 20 29  TK_AND==OP_And )
21c56 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21c57 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 29 3b 0a  TK_OR==OP_Or );.
21c58 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
21c59 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 20 29 3b  _PLUS==OP_Add );
21c5a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21c5b 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 75 62 74  K_MINUS==OP_Subt
21c5c 72 61 63 74 20 29 3b 0a 20 20 20 20 20 20 61 73  ract );.      as
21c5d 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d 3d 4f 50  sert( TK_REM==OP
21c5e 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b 0a 20 20  _Remainder );.  
21c5f 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 42      assert( TK_B
21c60 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 41 6e 64  ITAND==OP_BitAnd
21c61 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21c62 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f 50 5f 42  ( TK_BITOR==OP_B
21c63 69 74 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 73  itOr );.      as
21c64 73 65 72 74 28 20 54 4b 5f 53 4c 41 53 48 3d 3d  sert( TK_SLASH==
21c65 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a 20 20 20  OP_Divide );.   
21c66 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 53     assert( TK_LS
21c67 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 4c 65  HIFT==OP_ShiftLe
21c68 66 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ft );.      asse
21c69 72 74 28 20 54 4b 5f 52 53 48 49 46 54 3d 3d 4f  rt( TK_RSHIFT==O
21c6a 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b 0a  P_ShiftRight );.
21c6b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
21c6c 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 6f 6e 63  _CONCAT==OP_Conc
21c6d 61 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  at );.      test
21c6e 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 41 4e 44  case( op==TK_AND
21c6f 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21c70 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52 20 29 3b  se( op==TK_OR );
21c71 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21c72 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20 29 3b 0a   op==TK_PLUS );.
21c73 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21c74 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20 29 3b 0a  op==TK_MINUS );.
21c75 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21c76 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b 0a 20 20  op==TK_REM );.  
21c77 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
21c78 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 29 3b 0a 20  ==TK_BITAND );. 
21c79 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21c7a 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 29 3b 0a 20  p==TK_BITOR );. 
21c7b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21c7c 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 29 3b 0a 20  p==TK_SLASH );. 
21c7d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
21c7e 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 20 29 3b 0a  p==TK_LSHIFT );.
21c7f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21c80 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 54 20 29 3b  op==TK_RSHIFT );
21c81 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21c82 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41 54 20 29   op==TK_CONCAT )
21c83 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ;.      r1 = sql
21c84 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
21c85 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21c86 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31  pLeft, &regFree1
21c87 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73 71  );.      r2 = sq
21c88 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
21c89 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  p(pParse, pExpr-
21c8a 3e 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65  >pRight, &regFre
21c8b 65 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  e2);.      sqlit
21c8c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
21c8d 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74 61 72 67  op, r2, r1, targ
21c8e 65 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  et);.      testc
21c8f 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
21c90 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21c91 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20  se( regFree2==0 
21c92 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
21c93 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21c94 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20  K_UMINUS: {.    
21c95 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20    Expr *pLeft = 
21c96 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
21c97 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66      assert( pLef
21c98 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  t );.      if( p
21c99 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f  Left->op==TK_FLO
21c9a 41 54 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  AT ){.        as
21c9b 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
21c9c 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
21c9d 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20  _IntValue) );.  
21c9e 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28 76        codeReal(v
21c9f 2c 20 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65  , pLeft->u.zToke
21ca0 6e 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20  n, 1, target);. 
21ca1 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
21ca2 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54  Left->op==TK_INT
21ca3 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20  EGER ){.        
21ca4 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20 70  codeInteger(v, p
21ca5 4c 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74 29  Left, 1, target)
21ca6 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
21ca7 20 20 20 20 20 20 20 72 65 67 46 72 65 65 31 20         regFree1 
21ca8 3d 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65  = r1 = sqlite3Ge
21ca9 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
21caa 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
21cab 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
21cac 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 31  P_Integer, 0, r1
21cad 29 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20  );.        r2 = 
21cae 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
21caf 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
21cb0 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
21cb1 65 65 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ee2);.        sq
21cb2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21cb3 76 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20  v, OP_Subtract, 
21cb4 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29 3b  r2, r1, target);
21cb5 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
21cb6 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29  e( regFree2==0 )
21cb7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
21cb8 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b 0a  inReg = target;.
21cb9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21cba 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42   }.    case TK_B
21cbb 49 54 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65 20  ITNOT:.    case 
21cbc 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20  TK_NOT: {.      
21cbd 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4e 4f  assert( TK_BITNO
21cbe 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b 0a  T==OP_BitNot );.
21cbf 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
21cc0 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b 0a  _NOT==OP_Not );.
21cc1 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21cc2 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 20 29 3b  op==TK_BITNOT );
21cc3 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21cc4 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a 20   op==TK_NOT );. 
21cc5 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
21cc6 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
21cc7 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
21cc8 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a  ft, &regFree1);.
21cc9 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21cca 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
21ccb 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72       inReg = tar
21ccc 67 65 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  get;.      sqlit
21ccd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21cce 6f 70 2c 20 72 31 2c 20 69 6e 52 65 67 29 3b 0a  op, r1, inReg);.
21ccf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21cd0 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49   }.    case TK_I
21cd1 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20  SNULL:.    case 
21cd2 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20  TK_NOTNULL: {.  
21cd3 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20      int addr;.  
21cd4 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49      assert( TK_I
21cd5 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c  SNULL==OP_IsNull
21cd6 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21cd7 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50  ( TK_NOTNULL==OP
21cd8 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  _NotNull );.    
21cd9 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21cda 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20  TK_ISNULL );.   
21cdb 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21cdc 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20  =TK_NOTNULL );. 
21cdd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21cde 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
21cdf 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74 29  eger, 1, target)
21ce0 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ;.      r1 = sql
21ce1 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
21ce2 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21ce3 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31  pLeft, &regFree1
21ce4 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21ce5 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
21ce6 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
21ce7 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
21ce8 28 76 2c 20 6f 70 2c 20 72 31 29 3b 0a 20 20 20  (v, op, r1);.   
21ce9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21cea 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
21ceb 6d 2c 20 74 61 72 67 65 74 2c 20 2d 31 29 3b 0a  m, target, -1);.
21cec 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21ced 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
21cee 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  r);.      break;
21cef 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
21cf0 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a  TK_AGG_FUNCTION:
21cf1 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66 6f   {.      AggInfo
21cf2 20 2a 70 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d   *pInfo = pExpr-
21cf3 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20  >pAggInfo;.     
21cf4 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29 7b   if( pInfo==0 ){
21cf5 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
21cf6 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
21cf7 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56  y(pExpr, EP_IntV
21cf8 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 20  alue) );.       
21cf9 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
21cfa 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73 65  (pParse, "misuse
21cfb 20 6f 66 20 61 67 67 72 65 67 61 74 65 3a 20 25   of aggregate: %
21cfc 73 28 29 22 2c 20 70 45 78 70 72 2d 3e 75 2e 7a  s()", pExpr->u.z
21cfd 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 65  Token);.      }e
21cfe 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 52  lse{.        inR
21cff 65 67 20 3d 20 70 49 6e 66 6f 2d 3e 61 46 75 6e  eg = pInfo->aFun
21d00 63 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 2e 69  c[pExpr->iAgg].i
21d01 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Mem;.      }.   
21d02 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21d03 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53      case TK_CONS
21d04 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73 65  T_FUNC:.    case
21d05 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a   TK_FUNCTION: {.
21d06 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a        ExprList *
21d07 70 46 61 72 67 3b 20 20 20 20 20 20 20 2f 2a 20  pFarg;       /* 
21d08 4c 69 73 74 20 6f 66 20 66 75 6e 63 74 69 6f 6e  List of function
21d09 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
21d0a 20 20 20 20 69 6e 74 20 6e 46 61 72 67 3b 20 20      int nFarg;  
21d0b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
21d0c 6d 62 65 72 20 6f 66 20 66 75 6e 63 74 69 6f 6e  mber of function
21d0d 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20   arguments */.  
21d0e 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65      FuncDef *pDe
21d0f 66 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  f;         /* Th
21d10 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  e function defin
21d11 69 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f 0a  ition object */.
21d12 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20        int nId;  
21d13 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21d14 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66 75  Length of the fu
21d15 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 69 6e 20 62  nction name in b
21d16 79 74 65 73 20 2a 2f 0a 20 20 20 20 20 20 63 6f  ytes */.      co
21d17 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20 20  nst char *zId;  
21d18 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63       /* The func
21d19 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  tion name */.   
21d1a 20 20 20 69 6e 74 20 63 6f 6e 73 74 4d 61 73 6b     int constMask
21d1b 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4d 61 73   = 0;     /* Mas
21d1c 6b 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61 72  k of function ar
21d1d 67 75 6d 65 6e 74 73 20 74 68 61 74 20 61 72 65  guments that are
21d1e 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 20 20 20   constant */.   
21d1f 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
21d20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
21d21 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20  p counter */.   
21d22 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28     u8 enc = ENC(
21d23 64 62 29 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  db);      /* The
21d24 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75   text encoding u
21d25 73 65 64 20 62 79 20 74 68 69 73 20 64 61 74 61  sed by this data
21d26 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 6f  base */.      Co
21d27 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30  llSeq *pColl = 0
21d28 3b 20 20 20 20 2f 2a 20 41 20 63 6f 6c 6c 61 74  ;    /* A collat
21d29 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ing sequence */.
21d2a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
21d2b 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
21d2c 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
21d2d 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 74 65  ect) );.      te
21d2e 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43  stcase( op==TK_C
21d2f 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20 20  ONST_FUNC );.   
21d30 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21d31 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 3b 0a  =TK_FUNCTION );.
21d32 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61        if( ExprHa
21d33 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78  sAnyProperty(pEx
21d34 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  pr, EP_TokenOnly
21d35 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 61  ) ){.        pFa
21d36 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65  rg = 0;.      }e
21d37 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 46 61  lse{.        pFa
21d38 72 67 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  rg = pExpr->x.pL
21d39 69 73 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ist;.      }.   
21d3a 20 20 20 6e 46 61 72 67 20 3d 20 70 46 61 72 67     nFarg = pFarg
21d3b 20 3f 20 70 46 61 72 67 2d 3e 6e 45 78 70 72 20   ? pFarg->nExpr 
21d3c 3a 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72  : 0;.      asser
21d3d 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
21d3e 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e  rty(pExpr, EP_In
21d3f 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20  tValue) );.     
21d40 20 7a 49 64 20 3d 20 70 45 78 70 72 2d 3e 75 2e   zId = pExpr->u.
21d41 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e 49  zToken;.      nI
21d42 64 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  d = sqlite3Strle
21d43 6e 33 30 28 7a 49 64 29 3b 0a 20 20 20 20 20 20  n30(zId);.      
21d44 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69  pDef = sqlite3Fi
21d45 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  ndFunction(db, z
21d46 49 64 2c 20 6e 49 64 2c 20 6e 46 61 72 67 2c 20  Id, nId, nFarg, 
21d47 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  enc, 0);.      i
21d48 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20  f( pDef==0 ){.  
21d49 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
21d4a 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75  orMsg(pParse, "u
21d4b 6e 6b 6e 6f 77 6e 20 66 75 6e 63 74 69 6f 6e 3a  nknown function:
21d4c 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 20 7a   %.*s()", nId, z
21d4d 49 64 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  Id);.        bre
21d4e 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  ak;.      }..   
21d4f 20 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 61 20     /* Attempt a 
21d50 64 69 72 65 63 74 20 69 6d 70 6c 65 6d 65 6e 74  direct implement
21d51 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 69  ation of the bui
21d52 6c 74 2d 69 6e 20 43 4f 41 4c 45 53 43 45 28 29  lt-in COALESCE()
21d53 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 49 46   and.      ** IF
21d54 4e 55 4c 4c 28 29 20 66 75 6e 63 74 69 6f 6e 73  NULL() functions
21d55 2e 20 20 54 68 69 73 20 61 76 6f 69 64 73 20 75  .  This avoids u
21d56 6e 6e 65 63 65 73 73 61 72 79 20 65 76 61 6c 61  nnecessary evala
21d57 74 69 6f 6e 20 6f 66 0a 20 20 20 20 20 20 2a 2a  tion of.      **
21d58 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 74 20   arguments past 
21d59 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 4e 55  the first non-NU
21d5a 4c 4c 20 61 72 67 75 6d 65 6e 74 2e 0a 20 20 20  LL argument..   
21d5b 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
21d5c 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51  pDef->flags & SQ
21d5d 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53  LITE_FUNC_COALES
21d5e 43 45 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  CE ){.        in
21d5f 74 20 65 6e 64 43 6f 61 6c 65 73 63 65 20 3d 20  t endCoalesce = 
21d60 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
21d61 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20  abel(v);.       
21d62 20 61 73 73 65 72 74 28 20 6e 46 61 72 67 3e 3d   assert( nFarg>=
21d63 32 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  2 );.        sql
21d64 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
21d65 72 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b 30 5d  rse, pFarg->a[0]
21d66 2e 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b  .pExpr, target);
21d67 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 31  .        for(i=1
21d68 3b 20 69 3c 6e 46 61 72 67 3b 20 69 2b 2b 29 7b  ; i<nFarg; i++){
21d69 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
21d6a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21d6b 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 74 61 72 67  OP_NotNull, targ
21d6c 65 74 2c 20 65 6e 64 43 6f 61 6c 65 73 63 65 29  et, endCoalesce)
21d6d 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
21d6e 74 65 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f  te3ExprCacheRemo
21d6f 76 65 28 70 50 61 72 73 65 2c 20 74 61 72 67 65  ve(pParse, targe
21d70 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  t);.          sq
21d71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50 75  lite3ExprCachePu
21d72 73 68 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  sh(pParse);.    
21d73 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21d74 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 46  rCode(pParse, pF
21d75 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c  arg->a[i].pExpr,
21d76 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
21d77 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
21d78 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20  achePop(pParse, 
21d79 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  1);.        }.  
21d7a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21d7b 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
21d7c 20 65 6e 64 43 6f 61 6c 65 73 63 65 29 3b 0a 20   endCoalesce);. 
21d7d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
21d7e 20 20 20 20 7d 0a 0a 0a 20 20 20 20 20 20 69 66      }...      if
21d7f 28 20 70 46 61 72 67 20 29 7b 0a 20 20 20 20 20  ( pFarg ){.     
21d80 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47     r1 = sqlite3G
21d81 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
21d82 73 65 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20  se, nFarg);.    
21d83 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
21d84 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65 29  achePush(pParse)
21d85 3b 20 20 20 20 20 2f 2a 20 54 69 63 6b 65 74 20  ;     /* Ticket 
21d86 32 65 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a  2ea2425d34be */.
21d87 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
21d88 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
21d89 70 50 61 72 73 65 2c 20 70 46 61 72 67 2c 20 72  pParse, pFarg, r
21d8a 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73  1, 1);.        s
21d8b 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
21d8c 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 20 20  op(pParse, 1);  
21d8d 20 2f 2a 20 54 69 63 6b 65 74 20 32 65 61 32 34   /* Ticket 2ea24
21d8e 32 35 64 33 34 62 65 20 2a 2f 0a 20 20 20 20 20  25d34be */.     
21d8f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
21d90 72 31 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  r1 = 0;.      }.
21d91 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
21d92 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
21d93 0a 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62  .      /* Possib
21d94 6c 79 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20  ly overload the 
21d95 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20  function if the 
21d96 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
21d97 73 0a 20 20 20 20 20 20 2a 2a 20 61 20 76 69 72  s.      ** a vir
21d98 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6c 75 6d  tual table colum
21d99 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  n..      **.    
21d9a 20 20 2a 2a 20 46 6f 72 20 69 6e 66 69 78 20 66    ** For infix f
21d9b 75 6e 63 74 69 6f 6e 73 20 28 4c 49 4b 45 2c 20  unctions (LIKE, 
21d9c 47 4c 4f 42 2c 20 52 45 47 45 58 50 2c 20 61 6e  GLOB, REGEXP, an
21d9d 64 20 4d 41 54 43 48 29 20 75 73 65 20 74 68 65  d MATCH) use the
21d9e 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64  .      ** second
21d9f 20 61 72 67 75 6d 65 6e 74 2c 20 6e 6f 74 20 74   argument, not t
21da0 68 65 20 66 69 72 73 74 2c 20 61 73 20 74 68 65  he first, as the
21da1 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 65 73   argument to tes
21da2 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 73 65  t to.      ** se
21da3 65 20 69 66 20 69 74 20 69 73 20 61 20 63 6f 6c  e if it is a col
21da4 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c  umn in a virtual
21da5 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73   table.  This is
21da6 20 64 6f 6e 65 20 62 65 63 61 75 73 65 0a 20 20   done because.  
21da7 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 66 74 20      ** the left 
21da8 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 66 69 78  operand of infix
21da9 20 66 75 6e 63 74 69 6f 6e 73 20 28 74 68 65 20   functions (the 
21daa 6f 70 65 72 61 6e 64 20 77 65 20 77 61 6e 74 20  operand we want 
21dab 74 6f 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74  to.      ** cont
21dac 72 6f 6c 20 6f 76 65 72 6c 6f 61 64 69 6e 67 29  rol overloading)
21dad 20 65 6e 64 73 20 75 70 20 61 73 20 74 68 65 20   ends up as the 
21dae 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
21daf 74 6f 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  to the.      ** 
21db0 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 65  function.  The e
21db1 78 70 72 65 73 73 69 6f 6e 20 22 41 20 67 6c 6f  xpression "A glo
21db2 62 20 42 22 20 69 73 20 65 71 75 69 76 61 6c 65  b B" is equivale
21db3 6e 74 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20  nt to .      ** 
21db4 22 67 6c 6f 62 28 42 2c 41 29 2e 20 20 57 65 20  "glob(B,A).  We 
21db5 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20  want to use the 
21db6 41 20 69 6e 20 22 41 20 67 6c 6f 62 20 42 22 20  A in "A glob B" 
21db7 74 6f 20 74 65 73 74 0a 20 20 20 20 20 20 2a 2a  to test.      **
21db8 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 6f 76   for function ov
21db9 65 72 6c 6f 61 64 69 6e 67 2e 20 20 42 75 74 20  erloading.  But 
21dba 77 65 20 75 73 65 20 74 68 65 20 42 20 74 65 72  we use the B ter
21dbb 6d 20 69 6e 20 22 67 6c 6f 62 28 42 2c 41 29 22  m in "glob(B,A)"
21dbc 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
21dbd 20 69 66 28 20 6e 46 61 72 67 3e 3d 32 20 26 26   if( nFarg>=2 &&
21dbe 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26   (pExpr->flags &
21dbf 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 29 20 29   EP_InfixFunc) )
21dc0 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d  {.        pDef =
21dc1 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72   sqlite3VtabOver
21dc2 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c  loadFunction(db,
21dc3 20 70 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46   pDef, nFarg, pF
21dc4 61 72 67 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 29  arg->a[1].pExpr)
21dc5 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
21dc6 28 20 6e 46 61 72 67 3e 30 20 29 7b 0a 20 20 20  ( nFarg>0 ){.   
21dc7 20 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69       pDef = sqli
21dc8 74 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46  te3VtabOverloadF
21dc9 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 44 65 66  unction(db, pDef
21dca 2c 20 6e 46 61 72 67 2c 20 70 46 61 72 67 2d 3e  , nFarg, pFarg->
21dcb 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  a[0].pExpr);.   
21dcc 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
21dcd 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 46 61    for(i=0; i<nFa
21dce 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  rg; i++){.      
21dcf 20 20 69 66 28 20 69 3c 33 32 20 26 26 20 73 71    if( i<32 && sq
21dd0 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
21dd1 61 6e 74 28 70 46 61 72 67 2d 3e 61 5b 69 5d 2e  ant(pFarg->a[i].
21dd2 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
21dd3 20 20 20 20 63 6f 6e 73 74 4d 61 73 6b 20 7c 3d      constMask |=
21dd4 20 28 31 3c 3c 69 29 3b 0a 20 20 20 20 20 20 20   (1<<i);.       
21dd5 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 28   }.        if( (
21dd6 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51  pDef->flags & SQ
21dd7 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f  LITE_FUNC_NEEDCO
21dd8 4c 4c 29 21 3d 30 20 26 26 20 21 70 43 6f 6c 6c  LL)!=0 && !pColl
21dd9 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43   ){.          pC
21dda 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70  oll = sqlite3Exp
21ddb 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  rCollSeq(pParse,
21ddc 20 70 46 61 72 67 2d 3e 61 5b 69 5d 2e 70 45 78   pFarg->a[i].pEx
21ddd 70 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pr);.        }. 
21dde 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
21ddf 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53   pDef->flags & S
21de0 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
21de1 4f 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 69  OLL ){.        i
21de2 66 28 20 21 70 43 6f 6c 6c 20 29 20 70 43 6f 6c  f( !pColl ) pCol
21de3 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c  l = db->pDfltCol
21de4 6c 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69  l; .        sqli
21de5 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
21de6 20 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20   OP_CollSeq, 0, 
21de7 30 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43  0, 0, (char *)pC
21de8 6f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29  oll, P4_COLLSEQ)
21de9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
21dea 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21deb 34 28 76 2c 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  4(v, OP_Function
21dec 2c 20 63 6f 6e 73 74 4d 61 73 6b 2c 20 72 31 2c  , constMask, r1,
21ded 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20   target,.       
21dee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21def 20 28 63 68 61 72 2a 29 70 44 65 66 2c 20 50 34   (char*)pDef, P4
21df0 5f 46 55 4e 43 44 45 46 29 3b 0a 20 20 20 20 20  _FUNCDEF);.     
21df1 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
21df2 67 65 50 35 28 76 2c 20 28 75 38 29 6e 46 61 72  geP5(v, (u8)nFar
21df3 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 46  g);.      if( nF
21df4 61 72 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73  arg ){.        s
21df5 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
21df6 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  pRange(pParse, r
21df7 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20 20  1, nFarg);.     
21df8 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
21df9 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
21dfa 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
21dfb 72 31 2c 20 6e 46 61 72 67 29 3b 0a 20 20 20 20  r1, nFarg);.    
21dfc 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
21dfd 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
21dfe 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20  IT_SUBQUERY.    
21dff 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a 0a  case TK_EXISTS:.
21e00 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45      case TK_SELE
21e01 43 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  CT: {.      test
21e02 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 58 49  case( op==TK_EXI
21e03 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73  STS );.      tes
21e04 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 45  tcase( op==TK_SE
21e05 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20 69 6e  LECT );.      in
21e06 52 65 67 20 3d 20 73 71 6c 69 74 65 33 43 6f 64  Reg = sqlite3Cod
21e07 65 53 75 62 73 65 6c 65 63 74 28 70 50 61 72 73  eSubselect(pPars
21e08 65 2c 20 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  e, pExpr, 0, 0);
21e09 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21e0a 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21e0b 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  IN: {.      int 
21e0c 64 65 73 74 49 66 46 61 6c 73 65 20 3d 20 73 71  destIfFalse = sq
21e0d 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
21e0e 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69 6e 74  el(v);.      int
21e0f 20 64 65 73 74 49 66 4e 75 6c 6c 20 3d 20 73 71   destIfNull = sq
21e10 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
21e11 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c  el(v);.      sql
21e12 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
21e13 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61  , OP_Null, 0, ta
21e14 72 67 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  rget);.      sql
21e15 69 74 65 33 45 78 70 72 43 6f 64 65 49 4e 28 70  ite3ExprCodeIN(p
21e16 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 64 65  Parse, pExpr, de
21e17 73 74 49 66 46 61 6c 73 65 2c 20 64 65 73 74 49  stIfFalse, destI
21e18 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71  fNull);.      sq
21e19 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21e1a 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
21e1b 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21e1c 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
21e1d 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 65 73 74  lveLabel(v, dest
21e1e 49 66 46 61 6c 73 65 29 3b 0a 20 20 20 20 20 20  IfFalse);.      
21e1f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21e20 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
21e21 74 61 72 67 65 74 2c 20 30 29 3b 0a 20 20 20 20  target, 0);.    
21e22 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
21e23 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 65 73  olveLabel(v, des
21e24 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  tIfNull);.      
21e25 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
21e26 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
21e27 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a  IT_SUBQUERY */..
21e28 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20  .    /*.    **  
21e29 20 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e    x BETWEEN y AN
21e2a 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  D z.    **.    *
21e2b 2a 20 54 68 69 73 20 69 73 20 65 71 75 69 76 61  * This is equiva
21e2c 6c 65 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 0a 20  lent to.    **. 
21e2d 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e     **    x>=y AN
21e2e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a 0a 20 20  D x<=z.    **.  
21e2f 20 20 2a 2a 20 58 20 69 73 20 73 74 6f 72 65 64    ** X is stored
21e30 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65 66 74   in pExpr->pLeft
21e31 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 20 73 74  ..    ** Y is st
21e32 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70  ored in pExpr->p
21e33 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
21e34 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 73 20 73 74  ..    ** Z is st
21e35 6f 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70  ored in pExpr->p
21e36 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72  List->a[1].pExpr
21e37 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
21e38 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a  e TK_BETWEEN: {.
21e39 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66        Expr *pLef
21e3a 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  t = pExpr->pLeft
21e3b 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45  ;.      struct E
21e3c 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c  xprList_item *pL
21e3d 49 74 65 6d 20 3d 20 70 45 78 70 72 2d 3e 78 2e  Item = pExpr->x.
21e3e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20  pList->a;.      
21e3f 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70  Expr *pRight = p
21e40 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20  LItem->pExpr;.. 
21e41 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
21e42 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c  Operands(pParse,
21e43 20 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65   pLeft, &r1, &re
21e44 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20  gFree1,.        
21e45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e46 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74            pRight
21e47 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32  , &r2, &regFree2
21e48 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21e49 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
21e4a 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21e4b 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b  ( regFree2==0 );
21e4c 0a 20 20 20 20 20 20 72 33 20 3d 20 73 71 6c 69  .      r3 = sqli
21e4d 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
21e4e 61 72 73 65 29 3b 0a 20 20 20 20 20 20 72 34 20  arse);.      r4 
21e4f 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
21e50 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
21e51 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70     codeCompare(p
21e52 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52  Parse, pLeft, pR
21e53 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a 20 20 20  ight, OP_Ge,.   
21e54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
21e55 31 2c 20 72 32 2c 20 72 33 2c 20 53 51 4c 49 54  1, r2, r3, SQLIT
21e56 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20  E_STOREP2);.    
21e57 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 20 20 20    pLItem++;.    
21e58 20 20 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65    pRight = pLIte
21e59 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  m->pExpr;.      
21e5a 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
21e5b 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
21e5c 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 72  gFree2);.      r
21e5d 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  2 = sqlite3ExprC
21e5e 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
21e5f 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 65  pRight, &regFree
21e60 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  2);.      testca
21e61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20  se( regFree2==0 
21e62 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
21e63 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65  pare(pParse, pLe
21e64 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 4c  ft, pRight, OP_L
21e65 65 2c 20 72 31 2c 20 72 32 2c 20 72 34 2c 20 53  e, r1, r2, r4, S
21e66 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a  QLITE_STOREP2);.
21e67 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21e68 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 6e  eAddOp3(v, OP_An
21e69 64 2c 20 72 33 2c 20 72 34 2c 20 74 61 72 67 65  d, r3, r4, targe
21e6a 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
21e6b 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
21e6c 70 50 61 72 73 65 2c 20 72 33 29 3b 0a 20 20 20  pParse, r3);.   
21e6d 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
21e6e 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
21e6f 20 72 34 29 3b 0a 20 20 20 20 20 20 62 72 65 61   r4);.      brea
21e70 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21e71 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20  e TK_UPLUS: {.  
21e72 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69      inReg = sqli
21e73 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65  te3ExprCodeTarge
21e74 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  t(pParse, pExpr-
21e75 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b  >pLeft, target);
21e76 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21e77 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 54 4b    }..    case TK
21e78 5f 54 52 49 47 47 45 52 3a 20 7b 0a 20 20 20 20  _TRIGGER: {.    
21e79 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 70 63 6f    /* If the opco
21e7a 64 65 20 69 73 20 54 4b 5f 54 52 49 47 47 45 52  de is TK_TRIGGER
21e7b 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65  , then the expre
21e7c 73 73 69 6f 6e 20 69 73 20 61 20 72 65 66 65 72  ssion is a refer
21e7d 65 6e 63 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f  ence.      ** to
21e7e 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65   a column in the
21e7f 20 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20   new.* or old.* 
21e80 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 61 76  pseudo-tables av
21e81 61 69 6c 61 62 6c 65 20 74 6f 0a 20 20 20 20 20  ailable to.     
21e82 20 2a 2a 20 74 72 69 67 67 65 72 20 70 72 6f 67   ** trigger prog
21e83 72 61 6d 73 2e 20 49 6e 20 74 68 69 73 20 63 61  rams. In this ca
21e84 73 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69  se Expr.iTable i
21e85 73 20 73 65 74 20 74 6f 20 31 20 66 6f 72 20 74  s set to 1 for t
21e86 68 65 0a 20 20 20 20 20 20 2a 2a 20 6e 65 77 2e  he.      ** new.
21e87 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20  * pseudo-table, 
21e88 6f 72 20 30 20 66 6f 72 20 74 68 65 20 6f 6c 64  or 0 for the old
21e89 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e  .* pseudo-table.
21e8a 20 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 0a 20 20   Expr.iColumn.  
21e8b 20 20 20 20 2a 2a 20 69 73 20 73 65 74 20 74 6f      ** is set to
21e8c 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74   the column of t
21e8d 68 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20  he pseudo-table 
21e8e 74 6f 20 72 65 61 64 2c 20 6f 72 20 74 6f 20 2d  to read, or to -
21e8f 31 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65  1 to.      ** re
21e90 61 64 20 74 68 65 20 72 6f 77 69 64 20 66 69 65  ad the rowid fie
21e91 6c 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ld..      **.   
21e92 20 20 20 2a 2a 20 54 68 65 20 65 78 70 72 65 73     ** The expres
21e93 73 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e  sion is implemen
21e94 74 65 64 20 75 73 69 6e 67 20 61 6e 20 4f 50 5f  ted using an OP_
21e95 50 61 72 61 6d 20 6f 70 63 6f 64 65 2e 20 54 68  Param opcode. Th
21e96 65 20 70 31 0a 20 20 20 20 20 20 2a 2a 20 70 61  e p1.      ** pa
21e97 72 61 6d 65 74 65 72 20 69 73 20 73 65 74 20 74  rameter is set t
21e98 6f 20 30 20 66 6f 72 20 61 6e 20 6f 6c 64 2e 72  o 0 for an old.r
21e99 6f 77 69 64 20 72 65 66 65 72 65 6e 63 65 2c 20  owid reference, 
21e9a 6f 72 20 74 6f 20 28 69 2b 31 29 0a 20 20 20 20  or to (i+1).    
21e9b 20 20 2a 2a 20 74 6f 20 72 65 66 65 72 65 6e 63    ** to referenc
21e9c 65 20 61 6e 6f 74 68 65 72 20 63 6f 6c 75 6d 6e  e another column
21e9d 20 6f 66 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73   of the old.* ps
21e9e 65 75 64 6f 2d 74 61 62 6c 65 2c 20 77 68 65 72  eudo-table, wher
21e9f 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 20 69 73  e .      ** i is
21ea0 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
21ea1 65 20 63 6f 6c 75 6d 6e 2e 20 46 6f 72 20 61 20  e column. For a 
21ea2 6e 65 77 2e 72 6f 77 69 64 20 72 65 66 65 72 65  new.rowid refere
21ea3 6e 63 65 2c 20 70 31 20 69 73 0a 20 20 20 20 20  nce, p1 is.     
21ea4 20 2a 2a 20 73 65 74 20 74 6f 20 28 6e 2b 31 29   ** set to (n+1)
21ea5 2c 20 77 68 65 72 65 20 6e 20 69 73 20 74 68 65  , where n is the
21ea6 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
21ea7 6e 73 20 69 6e 20 65 61 63 68 20 70 73 65 75 64  ns in each pseud
21ea8 6f 2d 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a  o-table..      *
21ea9 2a 20 46 6f 72 20 61 20 72 65 66 65 72 65 6e 63  * For a referenc
21eaa 65 20 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 63  e to any other c
21eab 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 6e 65 77  olumn in the new
21eac 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c  .* pseudo-table,
21ead 20 70 31 0a 20 20 20 20 20 20 2a 2a 20 69 73 20   p1.      ** is 
21eae 73 65 74 20 74 6f 20 28 6e 2b 32 2b 69 29 2c 20  set to (n+2+i), 
21eaf 77 68 65 72 65 20 6e 20 61 6e 64 20 69 20 61 72  where n and i ar
21eb0 65 20 61 73 20 64 65 66 69 6e 65 64 20 70 72 65  e as defined pre
21eb1 76 69 6f 75 73 6c 79 2e 20 46 6f 72 0a 20 20 20  viously. For.   
21eb2 20 20 20 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69     ** example, i
21eb3 66 20 74 68 65 20 74 61 62 6c 65 20 6f 6e 20 77  f the table on w
21eb4 68 69 63 68 20 74 72 69 67 67 65 72 73 20 61 72  hich triggers ar
21eb5 65 20 62 65 69 6e 67 20 66 69 72 65 64 20 69 73  e being fired is
21eb6 0a 20 20 20 20 20 20 2a 2a 20 64 65 63 6c 61 72  .      ** declar
21eb7 65 64 20 61 73 3a 0a 20 20 20 20 20 20 2a 2a 0a  ed as:.      **.
21eb8 20 20 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54        **   CREAT
21eb9 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 29  E TABLE t1(a, b)
21eba 3b 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ;.      **.     
21ebb 20 2a 2a 20 54 68 65 6e 20 70 31 20 69 73 20 69   ** Then p1 is i
21ebc 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 66 6f  nterpreted as fo
21ebd 6c 6c 6f 77 73 3a 0a 20 20 20 20 20 20 2a 2a 0a  llows:.      **.
21ebe 20 20 20 20 20 20 2a 2a 20 20 20 70 31 3d 3d 30        **   p1==0
21ebf 20 20 20 2d 3e 20 20 20 20 6f 6c 64 2e 72 6f 77     ->    old.row
21ec0 69 64 20 20 20 20 20 70 31 3d 3d 33 20 20 20 2d  id     p1==3   -
21ec1 3e 20 20 20 20 6e 65 77 2e 72 6f 77 69 64 0a 20  >    new.rowid. 
21ec2 20 20 20 20 20 2a 2a 20 20 20 70 31 3d 3d 31 20       **   p1==1 
21ec3 20 20 2d 3e 20 20 20 20 6f 6c 64 2e 61 20 20 20    ->    old.a   
21ec4 20 20 20 20 20 20 70 31 3d 3d 34 20 20 20 2d 3e        p1==4   ->
21ec5 20 20 20 20 6e 65 77 2e 61 0a 20 20 20 20 20 20      new.a.      
21ec6 2a 2a 20 20 20 70 31 3d 3d 32 20 20 20 2d 3e 20  **   p1==2   -> 
21ec7 20 20 20 6f 6c 64 2e 62 20 20 20 20 20 20 20 20     old.b        
21ec8 20 70 31 3d 3d 35 20 20 20 2d 3e 20 20 20 20 6e   p1==5   ->    n
21ec9 65 77 2e 62 20 20 20 20 20 20 20 0a 20 20 20 20  ew.b       .    
21eca 20 20 2a 2f 0a 20 20 20 20 20 20 54 61 62 6c 65    */.      Table
21ecb 20 2a 70 54 61 62 20 3d 20 70 45 78 70 72 2d 3e   *pTab = pExpr->
21ecc 70 54 61 62 3b 0a 20 20 20 20 20 20 69 6e 74 20  pTab;.      int 
21ecd 70 31 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62  p1 = pExpr->iTab
21ece 6c 65 20 2a 20 28 70 54 61 62 2d 3e 6e 43 6f 6c  le * (pTab->nCol
21ecf 2b 31 29 20 2b 20 31 20 2b 20 70 45 78 70 72 2d  +1) + 1 + pExpr-
21ed0 3e 69 43 6f 6c 75 6d 6e 3b 0a 0a 20 20 20 20 20  >iColumn;..     
21ed1 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
21ed2 69 54 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 45 78  iTable==0 || pEx
21ed3 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 31 20 29 3b  pr->iTable==1 );
21ed4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
21ed5 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 2d  Expr->iColumn>=-
21ed6 31 20 26 26 20 70 45 78 70 72 2d 3e 69 43 6f 6c  1 && pExpr->iCol
21ed7 75 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  umn<pTab->nCol )
21ed8 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
21ed9 70 54 61 62 2d 3e 69 50 4b 65 79 3c 30 20 7c 7c  pTab->iPKey<0 ||
21eda 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21   pExpr->iColumn!
21edb 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 3b 0a  =pTab->iPKey );.
21edc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 31        assert( p1
21edd 3e 3d 30 20 26 26 20 70 31 3c 28 70 54 61 62 2d  >=0 && p1<(pTab-
21ede 3e 6e 43 6f 6c 2a 32 2b 32 29 20 29 3b 0a 0a 20  >nCol*2+2) );.. 
21edf 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21ee0 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 50 61 72  AddOp2(v, OP_Par
21ee1 61 6d 2c 20 70 31 2c 20 74 61 72 67 65 74 29 3b  am, p1, target);
21ee2 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
21ee3 6e 74 28 28 76 2c 20 22 25 73 2e 25 73 20 2d 3e  nt((v, "%s.%s ->
21ee4 20 24 25 64 22 2c 0a 20 20 20 20 20 20 20 20 28   $%d",.        (
21ee5 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3f 20  pExpr->iTable ? 
21ee6 22 6e 65 77 22 20 3a 20 22 6f 6c 64 22 29 2c 0a  "new" : "old"),.
21ee7 20 20 20 20 20 20 20 20 28 70 45 78 70 72 2d 3e          (pExpr->
21ee8 69 43 6f 6c 75 6d 6e 3c 30 20 3f 20 22 72 6f 77  iColumn<0 ? "row
21ee9 69 64 22 20 3a 20 70 45 78 70 72 2d 3e 70 54 61  id" : pExpr->pTa
21eea 62 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69  b->aCol[pExpr->i
21eeb 43 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d 65 29 2c 0a  Column].zName),.
21eec 20 20 20 20 20 20 20 20 74 61 72 67 65 74 0a 20          target. 
21eed 20 20 20 20 20 29 29 3b 0a 0a 20 20 20 20 20 20       ));..      
21eee 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e  /* If the column
21eef 20 68 61 73 20 52 45 41 4c 20 61 66 66 69 6e 69   has REAL affini
21ef0 74 79 2c 20 69 74 20 6d 61 79 20 63 75 72 72 65  ty, it may curre
21ef1 6e 74 6c 79 20 62 65 20 73 74 6f 72 65 64 20 61  ntly be stored a
21ef2 73 20 61 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e  s an.      ** in
21ef3 74 65 67 65 72 2e 20 55 73 65 20 4f 50 5f 52 65  teger. Use OP_Re
21ef4 61 6c 41 66 66 69 6e 69 74 79 20 74 6f 20 6d 61  alAffinity to ma
21ef5 6b 65 20 73 75 72 65 20 69 74 20 69 73 20 72 65  ke sure it is re
21ef6 61 6c 6c 79 20 72 65 61 6c 2e 20 20 2a 2f 0a 20  ally real.  */. 
21ef7 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e       if( pExpr->
21ef8 69 43 6f 6c 75 6d 6e 3e 3d 30 20 0a 20 20 20 20  iColumn>=0 .    
21ef9 20 20 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c     && pTab->aCol
21efa 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d  [pExpr->iColumn]
21efb 2e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54  .affinity==SQLIT
21efc 45 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20  E_AFF_REAL.     
21efd 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
21efe 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
21eff 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79   OP_RealAffinity
21f00 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21f01 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
21f02 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 0a 20      }...    /*. 
21f03 20 20 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20     ** Form A:.  
21f04 20 20 2a 2a 20 20 20 43 41 53 45 20 78 20 57 48    **   CASE x WH
21f05 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48  EN e1 THEN r1 WH
21f06 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e  EN e2 THEN r2 ..
21f07 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72  . WHEN eN THEN r
21f08 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20  N ELSE y END.   
21f09 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20   **.    ** Form 
21f0a 42 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45  B:.    **   CASE
21f0b 20 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31   WHEN e1 THEN r1
21f0c 20 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32   WHEN e2 THEN r2
21f0d 20 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45   ... WHEN eN THE
21f0e 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a  N rN ELSE y END.
21f0f 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f      **.    ** Fo
21f10 72 6d 20 41 20 69 73 20 63 61 6e 20 62 65 20 74  rm A is can be t
21f11 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20  ransformed into 
21f12 74 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66  the equivalent f
21f13 6f 72 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73  orm B as follows
21f14 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20  :.    **   CASE 
21f15 57 48 45 4e 20 78 3d 65 31 20 54 48 45 4e 20 72  WHEN x=e1 THEN r
21f16 31 20 57 48 45 4e 20 78 3d 65 32 20 54 48 45 4e  1 WHEN x=e2 THEN
21f17 20 72 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20   r2 ....    **  
21f18 20 20 20 20 20 20 57 48 45 4e 20 78 3d 65 4e 20        WHEN x=eN 
21f19 54 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45  THEN rN ELSE y E
21f1a 4e 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  ND.    **.    **
21f1b 20 58 20 28 69 66 20 69 74 20 65 78 69 73 74 73   X (if it exists
21f1c 29 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70  ) is in pExpr->p
21f1d 4c 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69  Left..    ** Y i
21f1e 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 52 69 67  s in pExpr->pRig
21f1f 68 74 2e 20 20 54 68 65 20 59 20 69 73 20 61 6c  ht.  The Y is al
21f20 73 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66  so optional.  If
21f21 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 20 20 20   there is no.   
21f22 20 2a 2a 20 45 4c 53 45 20 63 6c 61 75 73 65 20   ** ELSE clause 
21f23 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 74 65 72  and no other ter
21f24 6d 20 6d 61 74 63 68 65 73 2c 20 74 68 65 6e 20  m matches, then 
21f25 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
21f26 65 0a 20 20 20 20 2a 2a 20 65 78 70 72 73 73 69  e.    ** exprssi
21f27 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20  on is NULL..    
21f28 2a 2a 20 45 69 20 69 73 20 69 6e 20 70 45 78 70  ** Ei is in pExp
21f29 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d  r->pList->a[i*2]
21f2a 20 61 6e 64 20 52 69 20 69 73 20 70 45 78 70 72   and Ri is pExpr
21f2b 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31  ->pList->a[i*2+1
21f2c 5d 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  ]..    **.    **
21f2d 20 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   The result of t
21f2e 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
21f2f 20 74 68 65 20 52 69 20 66 6f 72 20 74 68 65 20   the Ri for the 
21f30 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 45  first matching E
21f31 69 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20  i,.    ** or if 
21f32 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63  there is no matc
21f33 68 69 6e 67 20 45 69 2c 20 74 68 65 20 45 4c 53  hing Ei, the ELS
21f34 45 20 74 65 72 6d 20 59 2c 20 6f 72 20 69 66 20  E term Y, or if 
21f35 74 68 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20  there is.    ** 
21f36 6e 6f 20 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55  no ELSE term, NU
21f37 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64  LL..    */.    d
21f38 65 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28 20  efault: assert( 
21f39 6f 70 3d 3d 54 4b 5f 43 41 53 45 20 29 3b 20 7b  op==TK_CASE ); {
21f3a 0a 20 20 20 20 20 20 69 6e 74 20 65 6e 64 4c 61  .      int endLa
21f3b 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  bel;            
21f3c 20 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f           /* GOTO
21f3d 20 6c 61 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f   label for end o
21f3e 66 20 43 41 53 45 20 73 74 6d 74 20 2a 2f 0a 20  f CASE stmt */. 
21f3f 20 20 20 20 20 69 6e 74 20 6e 65 78 74 43 61 73       int nextCas
21f40 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
21f41 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c         /* GOTO l
21f42 61 62 65 6c 20 66 6f 72 20 6e 65 78 74 20 57 48  abel for next WH
21f43 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20  EN clause */.   
21f44 20 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20     int nExpr;   
21f45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f46 20 20 20 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65       /* 2x numbe
21f47 72 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20  r of WHEN terms 
21f48 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20  */.      int i; 
21f49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f4a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
21f4b 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
21f4c 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45      ExprList *pE
21f4d 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  List;           
21f4e 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
21f4f 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20   WHEN terms */. 
21f50 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72       struct Expr
21f51 4c 69 73 74 5f 69 74 65 6d 20 2a 61 4c 69 73 74  List_item *aList
21f52 65 6c 65 6d 3b 20 20 2f 2a 20 41 72 72 61 79 20  elem;  /* Array 
21f53 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f  of WHEN terms */
21f54 0a 20 20 20 20 20 20 45 78 70 72 20 6f 70 43 6f  .      Expr opCo
21f55 6d 70 61 72 65 3b 20 20 20 20 20 20 20 20 20 20  mpare;          
21f56 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
21f57 58 3d 3d 45 69 20 65 78 70 72 65 73 73 69 6f 6e  X==Ei expression
21f58 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 63   */.      Expr c
21f59 61 63 68 65 58 3b 20 20 20 20 20 20 20 20 20 20  acheX;          
21f5a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
21f5b 61 63 68 65 64 20 65 78 70 72 65 73 73 69 6f 6e  ached expression
21f5c 20 58 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72   X */.      Expr
21f5d 20 2a 70 58 3b 20 20 20 20 20 20 20 20 20 20 20   *pX;           
21f5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21f5f 20 54 68 65 20 58 20 65 78 70 72 65 73 73 69 6f   The X expressio
21f60 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20  n */.      Expr 
21f61 2a 70 54 65 73 74 20 3d 20 30 3b 20 20 20 20 20  *pTest = 0;     
21f62 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21f63 58 3d 3d 45 69 20 28 66 6f 72 6d 20 41 29 20 6f  X==Ei (form A) o
21f64 72 20 6a 75 73 74 20 45 69 20 28 66 6f 72 6d 20  r just Ei (form 
21f65 42 29 20 2a 2f 0a 20 20 20 20 20 20 56 56 41 5f  B) */.      VVA_
21f66 4f 4e 4c 59 28 20 69 6e 74 20 69 43 61 63 68 65  ONLY( int iCache
21f67 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e  Level = pParse->
21f68 69 43 61 63 68 65 4c 65 76 65 6c 3b 20 29 0a 0a  iCacheLevel; )..
21f69 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
21f6a 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
21f6b 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
21f6c 63 74 29 20 26 26 20 70 45 78 70 72 2d 3e 78 2e  ct) && pExpr->x.
21f6d 70 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20 61  pList );.      a
21f6e 73 73 65 72 74 28 28 70 45 78 70 72 2d 3e 78 2e  ssert((pExpr->x.
21f6f 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 25 20 32  pList->nExpr % 2
21f70 29 20 3d 3d 20 30 29 3b 0a 20 20 20 20 20 20 61  ) == 0);.      a
21f71 73 73 65 72 74 28 70 45 78 70 72 2d 3e 78 2e 70  ssert(pExpr->x.p
21f72 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3e 20 30 29  List->nExpr > 0)
21f73 3b 0a 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d  ;.      pEList =
21f74 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b   pExpr->x.pList;
21f75 0a 20 20 20 20 20 20 61 4c 69 73 74 65 6c 65 6d  .      aListelem
21f76 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20   = pEList->a;.  
21f77 20 20 20 20 6e 45 78 70 72 20 3d 20 70 45 4c 69      nExpr = pELi
21f78 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20  st->nExpr;.     
21f79 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71 6c 69   endLabel = sqli
21f7a 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
21f7b 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  (v);.      if( (
21f7c 70 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  pX = pExpr->pLef
21f7d 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  t)!=0 ){.       
21f7e 20 63 61 63 68 65 58 20 3d 20 2a 70 58 3b 0a 20   cacheX = *pX;. 
21f7f 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
21f80 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55   pX->op==TK_COLU
21f81 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65  MN );.        te
21f82 73 74 63 61 73 65 28 20 70 58 2d 3e 6f 70 3d 3d  stcase( pX->op==
21f83 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20  TK_REGISTER );. 
21f84 20 20 20 20 20 20 20 63 61 63 68 65 58 2e 69 54         cacheX.iT
21f85 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78  able = sqlite3Ex
21f86 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
21f87 65 2c 20 70 58 2c 20 26 72 65 67 46 72 65 65 31  e, pX, &regFree1
21f88 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
21f89 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
21f8a 20 29 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68   );.        cach
21f8b 65 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53  eX.op = TK_REGIS
21f8c 54 45 52 3b 0a 20 20 20 20 20 20 20 20 6f 70 43  TER;.        opC
21f8d 6f 6d 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45  ompare.op = TK_E
21f8e 51 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d  Q;.        opCom
21f8f 70 61 72 65 2e 70 4c 65 66 74 20 3d 20 26 63 61  pare.pLeft = &ca
21f90 63 68 65 58 3b 0a 20 20 20 20 20 20 20 20 70 54  cheX;.        pT
21f91 65 73 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65  est = &opCompare
21f92 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
21f93 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72  for(i=0; i<nExpr
21f94 3b 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20  ; i=i+2){.      
21f95 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
21f96 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a  hePush(pParse);.
21f97 20 20 20 20 20 20 20 20 69 66 28 20 70 58 20 29          if( pX )
21f98 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  {.          asse
21f99 72 74 28 20 70 54 65 73 74 21 3d 30 20 29 3b 0a  rt( pTest!=0 );.
21f9a 20 20 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70            opComp
21f9b 61 72 65 2e 70 52 69 67 68 74 20 3d 20 61 4c 69  are.pRight = aLi
21f9c 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b  stelem[i].pExpr;
21f9d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
21f9e 20 20 20 20 20 20 20 20 20 20 70 54 65 73 74 20            pTest 
21f9f 3d 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70  = aListelem[i].p
21fa0 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Expr;.        }.
21fa1 20 20 20 20 20 20 20 20 6e 65 78 74 43 61 73 65          nextCase
21fa2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
21fa3 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
21fa4 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54      testcase( pT
21fa5 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  est->op==TK_COLU
21fa6 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  MN );.        sq
21fa7 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
21fa8 28 70 50 61 72 73 65 2c 20 70 54 65 73 74 2c 20  (pParse, pTest, 
21fa9 6e 65 78 74 43 61 73 65 2c 20 53 51 4c 49 54 45  nextCase, SQLITE
21faa 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
21fab 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21fac 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70  aListelem[i+1].p
21fad 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Expr->op==TK_COL
21fae 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74  UMN );.        t
21faf 65 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 6c  estcase( aListel
21fb0 65 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f  em[i+1].pExpr->o
21fb1 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29  p==TK_REGISTER )
21fb2 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
21fb3 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
21fb4 2c 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d  , aListelem[i+1]
21fb5 2e 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b  .pExpr, target);
21fb6 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21fb7 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21fb8 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4c 61 62  _Goto, 0, endLab
21fb9 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  el);.        sql
21fba 69 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70  ite3ExprCachePop
21fbb 28 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 20  (pParse, 1);.   
21fbc 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21fbd 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
21fbe 6e 65 78 74 43 61 73 65 29 3b 0a 20 20 20 20 20  nextCase);.     
21fbf 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78   }.      if( pEx
21fc0 70 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20  pr->pRight ){.  
21fc1 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21fc2 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73  rCachePush(pPars
21fc3 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
21fc4 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
21fc5 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  se, pExpr->pRigh
21fc6 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  t, target);.    
21fc7 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
21fc8 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20  achePop(pParse, 
21fc9 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  1);.      }else{
21fca 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21fcb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21fcc 5f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74  _Null, 0, target
21fcd 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
21fce 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c   assert( db->mal
21fcf 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61  locFailed || pPa
21fd0 72 73 65 2d 3e 6e 45 72 72 3e 30 20 0a 20 20 20  rse->nErr>0 .   
21fd1 20 20 20 20 20 20 20 20 7c 7c 20 70 50 61 72 73          || pPars
21fd2 65 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3d 3d  e->iCacheLevel==
21fd3 69 43 61 63 68 65 4c 65 76 65 6c 20 29 3b 0a 20  iCacheLevel );. 
21fd4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21fd5 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
21fd6 65 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20  endLabel);.     
21fd7 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69   break;.    }.#i
21fd8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
21fd9 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 63 61  T_TRIGGER.    ca
21fda 73 65 20 54 4b 5f 52 41 49 53 45 3a 20 7b 0a 20  se TK_RAISE: {. 
21fdb 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
21fdc 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45  pr->affinity==OE
21fdd 5f 52 6f 6c 6c 62 61 63 6b 20 0a 20 20 20 20 20  _Rollback .     
21fde 20 20 20 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e        || pExpr->
21fdf 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 41 62 6f  affinity==OE_Abo
21fe0 72 74 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c  rt.           ||
21fe1 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79   pExpr->affinity
21fe2 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20 20  ==OE_Fail.      
21fe3 20 20 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61       || pExpr->a
21fe4 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f  ffinity==OE_Igno
21fe5 72 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  re.      );.    
21fe6 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 70    if( !pParse->p
21fe7 54 72 69 67 67 65 72 54 61 62 20 29 7b 0a 20 20  TriggerTab ){.  
21fe8 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
21fe9 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20  orMsg(pParse,.  
21fea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21feb 20 20 20 20 20 22 52 41 49 53 45 28 29 20 6d 61       "RAISE() ma
21fec 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77  y only be used w
21fed 69 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 2d  ithin a trigger-
21fee 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20 20 20  program");.     
21fef 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
21ff0 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
21ff1 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d  Expr->affinity==
21ff2 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20 20 20  OE_Abort ){.    
21ff3 20 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62      sqlite3MayAb
21ff4 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20 20  ort(pParse);.   
21ff5 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
21ff6 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
21ff7 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e  rty(pExpr, EP_In
21ff8 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20  tValue) );.     
21ff9 20 69 66 28 20 70 45 78 70 72 2d 3e 61 66 66 69   if( pExpr->affi
21ffa 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20  nity==OE_Ignore 
21ffb 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
21ffc 65 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20  e3VdbeAddOp4(.  
21ffd 20 20 20 20 20 20 20 20 20 20 76 2c 20 4f 50 5f            v, OP_
21ffe 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c  Halt, SQLITE_OK,
21fff 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 30 2c 20 70   OE_Ignore, 0, p
22000 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 30  Expr->u.zToken,0
22001 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
22002 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 48          sqlite3H
22003 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 70 50  altConstraint(pP
22004 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 61 66 66  arse, pExpr->aff
22005 69 6e 69 74 79 2c 20 70 45 78 70 72 2d 3e 75 2e  inity, pExpr->u.
22006 7a 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20  zToken, 0);.    
22007 20 20 7d 0a 0a 20 20 20 20 20 20 62 72 65 61 6b    }..      break
22008 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
22009 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65   }.  sqlite3Rele
2200a 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2200b 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a 20 20  e, regFree1);.  
2200c 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2200d 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
2200e 67 46 72 65 65 32 29 3b 0a 20 20 72 65 74 75 72  gFree2);.  retur
2200f 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a  n inReg;.}../*.*
22010 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
22011 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6e 20 65  to evaluate an e
22012 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 73 74  xpression and st
22013 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a  ore the results.
22014 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74  ** into a regist
22015 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  er.  Return the 
22016 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20  register number 
22017 77 68 65 72 65 20 74 68 65 20 72 65 73 75 6c 74  where the result
22018 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 2e  s.** are stored.
22019 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  .**.** If the re
2201a 67 69 73 74 65 72 20 69 73 20 61 20 74 65 6d 70  gister is a temp
2201b 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20 74  orary register t
2201c 68 61 74 20 63 61 6e 20 62 65 20 64 65 61 6c 6c  hat can be deall
2201d 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20  ocated,.** then 
2201e 77 72 69 74 65 20 69 74 73 20 6e 75 6d 62 65 72  write its number
2201f 20 69 6e 74 6f 20 2a 70 52 65 67 2e 20 20 49 66   into *pReg.  If
22020 20 74 68 65 20 72 65 73 75 6c 74 20 72 65 67 69   the result regi
22021 73 74 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 61  ster is not.** a
22022 20 74 65 6d 70 6f 72 61 72 79 2c 20 74 68 65 6e   temporary, then
22023 20 73 65 74 20 2a 70 52 65 67 20 74 6f 20 7a 65   set *pReg to ze
22024 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ro..*/.SQLITE_PR
22025 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
22026 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61  3ExprCodeTemp(Pa
22027 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
22028 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70  r *pExpr, int *p
22029 52 65 67 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d  Reg){.  int r1 =
2202a 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2202b 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e  eg(pParse);.  in
2202c 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78  t r2 = sqlite3Ex
2202d 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
2202e 72 73 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b  rse, pExpr, r1);
2202f 0a 20 20 69 66 28 20 72 32 3d 3d 72 31 20 29 7b  .  if( r2==r1 ){
22030 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 72 31 3b  .    *pReg = r1;
22031 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
22032 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
22033 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
22034 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 30 3b 0a  .    *pReg = 0;.
22035 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 32 3b    }.  return r2;
22036 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
22037 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
22038 6c 20 65 76 61 6c 75 61 74 65 20 65 78 70 72 65  l evaluate expre
22039 73 73 69 6f 6e 20 70 45 78 70 72 20 61 6e 64 20  ssion pExpr and 
2203a 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73  store the.** res
2203b 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72  ults in register
2203c 20 74 61 72 67 65 74 2e 20 20 54 68 65 20 72 65   target.  The re
2203d 73 75 6c 74 73 20 61 72 65 20 67 75 61 72 61 6e  sults are guaran
2203e 74 65 65 64 20 74 6f 20 61 70 70 65 61 72 0a 2a  teed to appear.*
2203f 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 61  * in register ta
22040 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rget..*/.SQLITE_
22041 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
22042 74 65 33 45 78 70 72 43 6f 64 65 28 50 61 72 73  te3ExprCode(Pars
22043 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
22044 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67  *pExpr, int targ
22045 65 74 29 7b 0a 20 20 69 6e 74 20 69 6e 52 65 67  et){.  int inReg
22046 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 74 61 72  ;..  assert( tar
22047 67 65 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c  get>0 && target<
22048 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b  =pParse->nMem );
22049 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74  .  inReg = sqlit
2204a 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
2204b 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
2204c 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72  target);.  asser
2204d 74 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65  t( pParse->pVdbe
2204e 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e   || pParse->db->
2204f 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
22050 20 20 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72    if( inReg!=tar
22051 67 65 74 20 26 26 20 70 50 61 72 73 65 2d 3e 70  get && pParse->p
22052 56 64 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  Vdbe ){.    sqli
22053 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 70 50  te3VdbeAddOp2(pP
22054 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f  arse->pVdbe, OP_
22055 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61  SCopy, inReg, ta
22056 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74  rget);.  }.  ret
22057 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f  urn target;.}../
22058 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
22059 64 65 20 74 68 61 74 20 65 76 61 6c 75 74 65 73  de that evalutes
2205a 20 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65   the given expre
2205b 73 73 69 6f 6e 20 61 6e 64 20 70 75 74 73 20 74  ssion and puts t
2205c 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20  he result.** in 
2205d 72 65 67 69 73 74 65 72 20 74 61 72 67 65 74 2e  register target.
2205e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65  .**.** Also make
2205f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65   a copy of the e
22060 78 70 72 65 73 73 69 6f 6e 20 72 65 73 75 6c 74  xpression result
22061 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65 72 20 22  s into another "
22062 63 61 63 68 65 22 20 72 65 67 69 73 74 65 72 0a  cache" register.
22063 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68  ** and modify th
22064 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73 6f 20  e expression so 
22065 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 74 69  that the next ti
22066 6d 65 20 69 74 20 69 73 20 65 76 61 6c 75 61 74  me it is evaluat
22067 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c  ed,.** the resul
22068 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  t is a copy of t
22069 68 65 20 63 61 63 68 65 20 72 65 67 69 73 74 65  he cache registe
2206a 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  r..**.** This ro
2206b 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
2206c 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  r expressions th
2206d 61 74 20 61 72 65 20 75 73 65 64 20 6d 75 6c 74  at are used mult
2206e 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65 73 2e 20  iple .** times. 
2206f 20 54 68 65 79 20 61 72 65 20 65 76 61 6c 75 61   They are evalua
22070 74 65 64 20 6f 6e 63 65 20 61 6e 64 20 74 68 65  ted once and the
22071 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
22072 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 72  expression.** ar
22073 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c  e reused..*/.SQL
22074 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
22075 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41  sqlite3ExprCodeA
22076 6e 64 43 61 63 68 65 28 50 61 72 73 65 20 2a 70  ndCache(Parse *p
22077 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
22078 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b  pr, int target){
22079 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
2207a 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
2207b 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e 52 65 67  t inReg;.  inReg
2207c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2207d 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
2207e 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73  , target);.  ass
2207f 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b  ert( target>0 );
22080 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69  .  /* This routi
22081 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72  ne is called for
22082 20 74 65 72 6d 73 20 74 6f 20 49 4e 53 45 52 54   terms to INSERT
22083 20 6f 72 20 55 50 44 41 54 45 2e 20 20 41 6e 64   or UPDATE.  And
22084 20 74 68 65 20 6f 6e 6c 79 0a 20 20 2a 2a 20 6f   the only.  ** o
22085 74 68 65 72 20 70 6c 61 63 65 20 77 68 65 72 65  ther place where
22086 20 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e   expressions can
22087 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e   be converted in
22088 74 6f 20 54 4b 5f 52 45 47 49 53 54 45 52 20 69  to TK_REGISTER i
22089 73 0a 20 20 2a 2a 20 69 6e 20 57 48 45 52 45 20  s.  ** in WHERE 
2208a 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
2208b 67 2e 20 20 53 6f 20 61 73 20 63 75 72 72 65 6e  g.  So as curren
2208c 74 6c 79 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2c  tly implemented,
2208d 20 74 68 65 72 65 20 69 73 0a 20 20 2a 2a 20 6e   there is.  ** n
2208e 6f 20 77 61 79 20 66 6f 72 20 61 20 54 4b 5f 52  o way for a TK_R
2208f 45 47 49 53 54 45 52 20 74 6f 20 65 78 69 73 74  EGISTER to exist
22090 20 68 65 72 65 2e 20 20 42 75 74 20 69 74 20 73   here.  But it s
22091 65 65 6d 73 20 70 72 75 64 65 6e 74 20 74 6f 0a  eems prudent to.
22092 20 20 2a 2a 20 6b 65 65 70 20 74 68 65 20 41 4c    ** keep the AL
22093 57 41 59 53 28 29 20 69 6e 20 63 61 73 65 20 74  WAYS() in case t
22094 68 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62  he conditions ab
22095 6f 76 65 20 63 68 61 6e 67 65 20 77 69 74 68 20  ove change with 
22096 66 75 74 75 72 65 0a 20 20 2a 2a 20 6d 6f 64 69  future.  ** modi
22097 66 69 63 61 74 69 6f 6e 73 20 6f 72 20 65 6e 68  fications or enh
22098 61 6e 63 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20  ancements. */.  
22099 69 66 28 20 41 4c 57 41 59 53 28 70 45 78 70 72  if( ALWAYS(pExpr
2209a 2d 3e 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45  ->op!=TK_REGISTE
2209b 52 29 20 29 7b 20 20 0a 20 20 20 20 69 6e 74 20  R) ){  .    int 
2209c 69 4d 65 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d  iMem;.    iMem =
2209d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2209e 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2209f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70  AddOp2(v, OP_Cop
220a0 79 2c 20 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b  y, inReg, iMem);
220a1 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62  .    pExpr->iTab
220a2 6c 65 20 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70  le = iMem;.    p
220a3 45 78 70 72 2d 3e 6f 70 32 20 3d 20 70 45 78 70  Expr->op2 = pExp
220a4 72 2d 3e 6f 70 3b 0a 20 20 20 20 70 45 78 70 72  r->op;.    pExpr
220a5 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54  ->op = TK_REGIST
220a6 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ER;.  }.  return
220a7 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   inReg;.}../*.**
220a8 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
220a9 70 45 78 70 72 20 69 73 20 61 6e 20 63 6f 6e 73  pExpr is an cons
220aa 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 20  tant expression 
220ab 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 69  that is appropri
220ac 61 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63 74 6f  ate.** for facto
220ad 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f  ring out of a lo
220ae 6f 70 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65  op.  Appropriate
220af 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65   expressions are
220b0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e  :.**.**    *  An
220b1 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  y expression tha
220b2 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 74  t evaluates to t
220b3 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63 6f 64  wo or more opcod
220b4 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  es..**.**    *  
220b5 41 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  Any OP_Integer, 
220b6 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74 72 69  OP_Real, OP_Stri
220b7 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50 5f  ng, OP_Blob, OP_
220b8 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20 20  Null, .**       
220b9 6f 72 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 74  or OP_Variable t
220ba 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  hat does not nee
220bb 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69  d to be placed i
220bc 6e 20 61 20 0a 2a 2a 20 20 20 20 20 20 20 73 70  n a .**       sp
220bd 65 63 69 66 69 63 20 72 65 67 69 73 74 65 72 2e  ecific register.
220be 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20  .**.** There is 
220bf 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61 63 74  no point in fact
220c0 6f 72 69 6e 67 20 6f 75 74 20 73 69 6e 67 6c 65  oring out single
220c1 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e  -instruction con
220c2 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 73 73  stant.** express
220c3 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74  ions that need t
220c4 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61  o be placed in a
220c5 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 69   particular regi
220c6 73 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20 63 6f  ster.  .** We co
220c7 75 6c 64 20 66 61 63 74 6f 72 20 74 68 65 6d 20  uld factor them 
220c8 6f 75 74 2c 20 62 75 74 20 74 68 65 6e 20 77 65  out, but then we
220c9 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 61 64   would end up ad
220ca 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53 43  ding an.** OP_SC
220cb 6f 70 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  opy instruction 
220cc 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75  to move the valu
220cd 65 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 72 65  e into the corre
220ce 63 74 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6c  ct register.** l
220cf 61 74 65 72 2e 20 20 57 65 20 6d 69 67 68 74 20  ater.  We might 
220d0 61 73 20 77 65 6c 6c 20 6a 75 73 74 20 75 73 65  as well just use
220d1 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69 6e   the original in
220d2 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a  struction and.**
220d3 20 61 76 6f 69 64 20 74 68 65 20 4f 50 5f 53 43   avoid the OP_SC
220d4 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  opy..*/.static i
220d5 6e 74 20 69 73 41 70 70 72 6f 70 72 69 61 74 65  nt isAppropriate
220d6 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 78 70  ForFactoring(Exp
220d7 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 73 71  r *p){.  if( !sq
220d8 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
220d9 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 7b  antNotJoin(p) ){
220da 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
220db 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e 74  /* Only constant
220dc 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65   expressions are
220dd 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
220de 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a 20 20   factoring */.  
220df 7d 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67  }.  if( (p->flag
220e0 73 20 26 20 45 50 5f 46 69 78 65 64 44 65 73 74  s & EP_FixedDest
220e1 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
220e2 72 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20 63 6f  rn 1;  /* Any co
220e3 6e 73 74 61 6e 74 20 77 69 74 68 6f 75 74 20 61  nstant without a
220e4 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 69   fixed destinati
220e5 6f 6e 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  on is appropriat
220e6 65 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c 65  e */.  }.  while
220e7 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55  ( p->op==TK_UPLU
220e8 53 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74  S ) p = p->pLeft
220e9 3b 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f  ;.  switch( p->o
220ea 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  p ){.#ifndef SQL
220eb 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49  ITE_OMIT_BLOB_LI
220ec 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 54  TERAL.    case T
220ed 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a 20  K_BLOB:.#endif. 
220ee 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41     case TK_VARIA
220ef 42 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  BLE:.    case TK
220f0 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61  _INTEGER:.    ca
220f1 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20  se TK_FLOAT:.   
220f2 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a 20   case TK_NULL:. 
220f3 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e     case TK_STRIN
220f4 47 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  G: {.      testc
220f5 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 42  ase( p->op==TK_B
220f6 4c 4f 42 20 29 3b 0a 20 20 20 20 20 20 74 65 73  LOB );.      tes
220f7 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b  tcase( p->op==TK
220f8 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a 20 20 20  _VARIABLE );.   
220f9 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e     testcase( p->
220fa 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29  op==TK_INTEGER )
220fb 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
220fc 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41  ( p->op==TK_FLOA
220fd 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  T );.      testc
220fe 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e  ase( p->op==TK_N
220ff 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ULL );.      tes
22100 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b  tcase( p->op==TK
22101 5f 53 54 52 49 4e 47 20 29 3b 0a 20 20 20 20 20  _STRING );.     
22102 20 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73 74 72   /* Single-instr
22103 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 73  uction constants
22104 20 77 69 74 68 20 61 20 66 69 78 65 64 20 64 65   with a fixed de
22105 73 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a 20 20  stination are.  
22106 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 64 6f      ** better do
22107 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49 66 20  ne in-line.  If 
22108 77 65 20 66 61 63 74 6f 72 20 74 68 65 6d 2c 20  we factor them, 
22109 74 68 65 79 20 77 69 6c 6c 20 6a 75 73 74 20 65  they will just e
2210a 6e 64 0a 20 20 20 20 20 20 2a 2a 20 75 70 20 67  nd.      ** up g
2210b 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f 50 5f  enerating an OP_
2210c 53 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20 74 68  SCopy to move th
2210d 65 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 64  e value to the d
2210e 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20  estination.     
2210f 20 2a 2a 20 72 65 67 69 73 74 65 72 2e 20 2a 2f   ** register. */
22110 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
22111 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
22112 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20  TK_UMINUS: {.   
22113 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 2d     if( p->pLeft-
22114 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c  >op==TK_FLOAT ||
22115 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54   p->pLeft->op==T
22116 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20  K_INTEGER ){.   
22117 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
22118 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
22119 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
2211a 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62  fault: {.      b
2211b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
2211c 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
2211d 2a 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73  *.** If pExpr is
2211e 20 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72   a constant expr
2211f 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61  ession that is a
22120 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a  ppropriate for.*
22121 2a 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 20  * factoring out 
22122 6f 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e 20  of a loop, then 
22123 65 76 61 6c 75 61 74 65 20 74 68 65 20 65 78 70  evaluate the exp
22124 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f 20  ression.** into 
22125 61 20 72 65 67 69 73 74 65 72 20 61 6e 64 20 63  a register and c
22126 6f 6e 76 65 72 74 20 74 68 65 20 65 78 70 72 65  onvert the expre
22127 73 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f  ssion into a TK_
22128 52 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70 72  REGISTER.** expr
22129 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  ession..*/.stati
2212a 63 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74 45  c int evalConstE
2212b 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  xpr(Walker *pWal
2212c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72  ker, Expr *pExpr
2212d 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  ){.  Parse *pPar
2212e 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50  se = pWalker->pP
2212f 61 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28 20  arse;.  switch( 
22130 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20  pExpr->op ){.   
22131 20 63 61 73 65 20 54 4b 5f 49 4e 3a 0a 20 20 20   case TK_IN:.   
22132 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 45   case TK_REGISTE
22133 52 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72  R: {.      retur
22134 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20  n WRC_Prune;.   
22135 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46   }.    case TK_F
22136 55 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73  UNCTION:.    cas
22137 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  e TK_AGG_FUNCTIO
22138 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43  N:.    case TK_C
22139 4f 4e 53 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 20  ONST_FUNC: {.   
2213a 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d 65     /* The argume
2213b 6e 74 73 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  nts to a functio
2213c 6e 20 68 61 76 65 20 61 20 66 69 78 65 64 20 64  n have a fixed d
2213d 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 20  estination..    
2213e 20 20 2a 2a 20 4d 61 72 6b 20 74 68 65 6d 20 74    ** Mark them t
2213f 68 69 73 20 77 61 79 20 74 6f 20 61 76 6f 69 64  his way to avoid
22140 20 67 65 6e 65 72 61 74 65 64 20 75 6e 6e 65 65   generated unnee
22141 64 65 64 20 4f 50 5f 53 43 6f 70 79 0a 20 20 20  ded OP_SCopy.   
22142 20 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f     ** instructio
22143 6e 73 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ns. .      */.  
22144 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
22145 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  ist = pExpr->x.p
22146 4c 69 73 74 3b 0a 20 20 20 20 20 20 61 73 73 65  List;.      asse
22147 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
22148 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78  erty(pExpr, EP_x
22149 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
2214a 20 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a     if( pList ){.
2214b 20 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20          int i = 
2214c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20  pList->nExpr;.  
2214d 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70        struct Exp
2214e 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
2214f 6d 20 3d 20 70 4c 69 73 74 2d 3e 61 3b 0a 20 20  m = pList->a;.  
22150 20 20 20 20 20 20 66 6f 72 28 3b 20 69 3e 30 3b        for(; i>0;
22151 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i--, pItem++){.
22152 20 20 20 20 20 20 20 20 20 20 69 66 28 20 41 4c            if( AL
22153 57 41 59 53 28 70 49 74 65 6d 2d 3e 70 45 78 70  WAYS(pItem->pExp
22154 72 29 20 29 20 70 49 74 65 6d 2d 3e 70 45 78 70  r) ) pItem->pExp
22155 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 46  r->flags |= EP_F
22156 69 78 65 64 44 65 73 74 3b 0a 20 20 20 20 20 20  ixedDest;.      
22157 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
22158 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
22159 20 7d 0a 20 20 69 66 28 20 69 73 41 70 70 72 6f   }.  if( isAppro
2215a 70 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72 69  priateForFactori
2215b 6e 67 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20  ng(pExpr) ){.   
2215c 20 69 6e 74 20 72 31 20 3d 20 2b 2b 70 50 61 72   int r1 = ++pPar
2215d 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69 6e  se->nMem;.    in
2215e 74 20 72 32 3b 0a 20 20 20 20 72 32 20 3d 20 73  t r2;.    r2 = s
2215f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
22160 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
22161 70 72 2c 20 72 31 29 3b 0a 20 20 20 20 69 66 28  pr, r1);.    if(
22162 20 4e 45 56 45 52 28 72 31 21 3d 72 32 29 20 29   NEVER(r1!=r2) )
22163 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
22164 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
22165 31 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f  1);.    pExpr->o
22166 70 32 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a  p2 = pExpr->op;.
22167 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
22168 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20  TK_REGISTER;.   
22169 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
2216a 20 72 32 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   r2;.    return 
2216b 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20  WRC_Prune;.  }. 
2216c 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
2216d 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  inue;.}../*.** P
2216e 72 65 65 76 61 6c 75 61 74 65 20 63 6f 6e 73 74  reevaluate const
2216f 61 6e 74 20 73 75 62 65 78 70 72 65 73 73 69 6f  ant subexpressio
22170 6e 73 20 77 69 74 68 69 6e 20 70 45 78 70 72 20  ns within pExpr 
22171 61 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a  and store the.**
22172 20 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69   results in regi
22173 73 74 65 72 73 2e 20 20 4d 6f 64 69 66 79 20 70  sters.  Modify p
22174 45 78 70 72 20 73 6f 20 74 68 61 74 20 74 68 65  Expr so that the
22175 20 63 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 70   constant subexp
22176 72 65 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 54  resions.** are T
22177 4b 5f 52 45 47 49 53 54 45 52 20 6f 70 63 6f 64  K_REGISTER opcod
22178 65 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f  es that refer to
22179 20 74 68 65 20 70 72 65 63 6f 6d 70 75 74 65 64   the precomputed
2217a 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49   values..*/.SQLI
2217b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2217c 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43  sqlite3ExprCodeC
2217d 6f 6e 73 74 61 6e 74 73 28 50 61 72 73 65 20 2a  onstants(Parse *
2217e 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
2217f 78 70 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77  xpr){.  Walker w
22180 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
22181 61 63 6b 20 3d 20 65 76 61 6c 43 6f 6e 73 74 45  ack = evalConstE
22182 78 70 72 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74  xpr;.  w.xSelect
22183 43 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20  Callback = 0;.  
22184 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  w.pParse = pPars
22185 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b  e;.  sqlite3Walk
22186 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b  Expr(&w, pExpr);
22187 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  .}.../*.** Gener
22188 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 70 75  ate code that pu
22189 73 68 65 73 20 74 68 65 20 76 61 6c 75 65 20 6f  shes the value o
2218a 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20  f every element 
2218b 6f 66 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20  of the given.** 
2218c 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20  expression list 
2218d 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63 65 20  into a sequence 
2218e 6f 66 20 72 65 67 69 73 74 65 72 73 20 62 65 67  of registers beg
2218f 69 6e 6e 69 6e 67 20 61 74 20 74 61 72 67 65 74  inning at target
22190 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ..**.** Return t
22191 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65  he number of ele
22192 6d 65 6e 74 73 20 65 76 61 6c 75 61 74 65 64 2e  ments evaluated.
22193 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
22194 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
22195 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 0a  prCodeExprList(.
22196 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
22197 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
22198 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
22199 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20  rList *pList,   
2219a 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
2219b 6e 20 6c 69 73 74 20 74 6f 20 62 65 20 63 6f 64  n list to be cod
2219c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 74 61 72 67  ed */.  int targ
2219d 65 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 68  et,        /* Wh
2219e 65 72 65 20 74 6f 20 77 72 69 74 65 20 72 65 73  ere to write res
2219f 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 64 6f  ults */.  int do
221a0 48 61 72 64 43 6f 70 79 20 20 20 20 20 2f 2a 20  HardCopy     /* 
221a1 4d 61 6b 65 20 61 20 68 61 72 64 20 63 6f 70 79  Make a hard copy
221a2 20 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e   of every elemen
221a3 74 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74  t */.){.  struct
221a4 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
221a5 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 2c 20  pItem;.  int i, 
221a6 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69  n;.  assert( pLi
221a7 73 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  st!=0 );.  asser
221a8 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20  t( target>0 );. 
221a9 20 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70   n = pList->nExp
221aa 72 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70  r;.  for(pItem=p
221ab 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c  List->a, i=0; i<
221ac 6e 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  n; i++, pItem++)
221ad 7b 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d  {.    if( pItem-
221ae 3e 69 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20  >iAlias ){.     
221af 20 69 6e 74 20 69 52 65 67 20 3d 20 63 6f 64 65   int iReg = code
221b0 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 49  Alias(pParse, pI
221b1 74 65 6d 2d 3e 69 41 6c 69 61 73 2c 20 70 49 74  tem->iAlias, pIt
221b2 65 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65  em->pExpr, targe
221b3 74 2b 69 29 3b 0a 20 20 20 20 20 20 56 64 62 65  t+i);.      Vdbe
221b4 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
221b5 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
221b6 20 20 20 20 69 66 28 20 69 52 65 67 21 3d 74 61      if( iReg!=ta
221b7 72 67 65 74 2b 69 20 29 7b 0a 20 20 20 20 20 20  rget+i ){.      
221b8 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
221b9 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c  Op2(v, OP_SCopy,
221ba 20 69 52 65 67 2c 20 74 61 72 67 65 74 2b 69 29   iReg, target+i)
221bb 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
221bc 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
221bd 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
221be 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c  e, pItem->pExpr,
221bf 20 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20   target+i);.    
221c0 7d 0a 20 20 20 20 69 66 28 20 64 6f 48 61 72 64  }.    if( doHard
221c1 43 6f 70 79 20 26 26 20 21 70 50 61 72 73 65 2d  Copy && !pParse-
221c2 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
221c3 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  d ){.      sqlit
221c4 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 70  e3ExprHardCopy(p
221c5 50 61 72 73 65 2c 20 74 61 72 67 65 74 2c 20 6e  Parse, target, n
221c6 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
221c7 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn n;.}../*.*
221c8 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
221c9 66 6f 72 20 61 20 42 45 54 57 45 45 4e 20 6f 70  for a BETWEEN op
221ca 65 72 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 20 20  erator..**.**   
221cb 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44   x BETWEEN y AND
221cc 20 7a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 62 6f   z.**.** The abo
221cd 76 65 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74  ve is equivalent
221ce 20 74 6f 20 0a 2a 2a 0a 2a 2a 20 20 20 20 78 3e   to .**.**    x>
221cf 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 2a 2a 0a 2a  =y AND x<=z.**.*
221d0 2a 20 43 6f 64 65 20 69 74 20 61 73 20 73 75 63  * Code it as suc
221d1 68 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74  h, taking care t
221d2 6f 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20  o do the common 
221d3 73 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  subexpression.**
221d4 20 65 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66   elementation of
221d5 20 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f   x..*/.static vo
221d6 69 64 20 65 78 70 72 43 6f 64 65 42 65 74 77 65  id exprCodeBetwe
221d7 65 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  en(.  Parse *pPa
221d8 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69  rse,    /* Parsi
221d9 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  ng and code gene
221da 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
221db 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c  /.  Expr *pExpr,
221dc 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 45 54        /* The BET
221dd 57 45 45 4e 20 65 78 70 72 65 73 73 69 6f 6e 20  WEEN expression 
221de 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20  */.  int dest,  
221df 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
221e0 65 72 65 20 69 66 20 74 68 65 20 6a 75 6d 70 20  ere if the jump 
221e1 69 73 20 74 61 6b 65 6e 20 2a 2f 0a 20 20 69 6e  is taken */.  in
221e2 74 20 6a 75 6d 70 49 66 54 72 75 65 2c 20 20 20  t jumpIfTrue,   
221e3 2f 2a 20 54 61 6b 65 20 74 68 65 20 6a 75 6d 70  /* Take the jump
221e4 20 69 66 20 74 68 65 20 42 45 54 57 45 45 4e 20   if the BETWEEN 
221e5 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74  is true */.  int
221e6 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 20 20 20 2f   jumpIfNull    /
221e7 2a 20 54 61 6b 65 20 74 68 65 20 6a 75 6d 70 20  * Take the jump 
221e8 69 66 20 74 68 65 20 42 45 54 57 45 45 4e 20 69  if the BETWEEN i
221e9 73 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 45  s NULL */.){.  E
221ea 78 70 72 20 65 78 70 72 41 6e 64 3b 20 20 20 20  xpr exprAnd;    
221eb 20 2f 2a 20 54 68 65 20 41 4e 44 20 6f 70 65 72   /* The AND oper
221ec 61 74 6f 72 20 69 6e 20 20 78 3e 3d 79 20 41 4e  ator in  x>=y AN
221ed 44 20 78 3c 3d 7a 20 20 2a 2f 0a 20 20 45 78 70  D x<=z  */.  Exp
221ee 72 20 63 6f 6d 70 4c 65 66 74 3b 20 20 20 20 2f  r compLeft;    /
221ef 2a 20 54 68 65 20 20 78 3e 3d 79 20 20 74 65 72  * The  x>=y  ter
221f0 6d 20 2a 2f 0a 20 20 45 78 70 72 20 63 6f 6d 70  m */.  Expr comp
221f1 52 69 67 68 74 3b 20 20 20 2f 2a 20 54 68 65 20  Right;   /* The 
221f2 20 78 3c 3d 7a 20 20 74 65 72 6d 20 2a 2f 0a 20   x<=z  term */. 
221f3 20 45 78 70 72 20 65 78 70 72 58 3b 20 20 20 20   Expr exprX;    
221f4 20 20 20 2f 2a 20 54 68 65 20 20 78 20 20 73 75     /* The  x  su
221f5 62 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20  bexpression */. 
221f6 20 69 6e 74 20 72 65 67 46 72 65 65 31 20 3d 20   int regFree1 = 
221f7 30 3b 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20  0; /* Temporary 
221f8 75 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  use register */.
221f9 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72  .  assert( !Expr
221fa 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
221fb 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
221fc 20 29 3b 0a 20 20 65 78 70 72 58 20 3d 20 2a 70   );.  exprX = *p
221fd 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 65  Expr->pLeft;.  e
221fe 78 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b 5f 41  xprAnd.op = TK_A
221ff 4e 44 3b 0a 20 20 65 78 70 72 41 6e 64 2e 70 4c  ND;.  exprAnd.pL
22200 65 66 74 20 3d 20 26 63 6f 6d 70 4c 65 66 74 3b  eft = &compLeft;
22201 0a 20 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68  .  exprAnd.pRigh
22202 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a  t = &compRight;.
22203 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d 20    compLeft.op = 
22204 54 4b 5f 47 45 3b 0a 20 20 63 6f 6d 70 4c 65 66  TK_GE;.  compLef
22205 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70 72 58  t.pLeft = &exprX
22206 3b 0a 20 20 63 6f 6d 70 4c 65 66 74 2e 70 52 69  ;.  compLeft.pRi
22207 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  ght = pExpr->x.p
22208 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
22209 3b 0a 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70  ;.  compRight.op
2220a 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 63 6f 6d 70   = TK_LE;.  comp
2220b 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 65  Right.pLeft = &e
2220c 78 70 72 58 3b 0a 20 20 63 6f 6d 70 52 69 67 68  xprX;.  compRigh
2220d 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70 72  t.pRight = pExpr
2220e 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  ->x.pList->a[1].
2220f 70 45 78 70 72 3b 0a 20 20 65 78 70 72 58 2e 69  pExpr;.  exprX.i
22210 54 61 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45  Table = sqlite3E
22211 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
22212 73 65 2c 20 26 65 78 70 72 58 2c 20 26 72 65 67  se, &exprX, &reg
22213 46 72 65 65 31 29 3b 0a 20 20 65 78 70 72 58 2e  Free1);.  exprX.
22214 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52  op = TK_REGISTER
22215 3b 0a 20 20 69 66 28 20 6a 75 6d 70 49 66 54 72  ;.  if( jumpIfTr
22216 75 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ue ){.    sqlite
22217 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
22218 73 65 2c 20 26 65 78 70 72 41 6e 64 2c 20 64 65  se, &exprAnd, de
22219 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
2221a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
2221b 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
2221c 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 41 6e  (pParse, &exprAn
2221d 64 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  d, dest, jumpIfN
2221e 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  ull);.  }.  sqli
2221f 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
22220 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65  g(pParse, regFre
22221 65 31 29 3b 0a 0a 20 20 2f 2a 20 45 6e 73 75 72  e1);..  /* Ensur
22222 65 20 61 64 65 71 75 61 74 65 20 74 65 73 74 20  e adequate test 
22223 63 6f 76 65 72 61 67 65 20 2a 2f 0a 20 20 74 65  coverage */.  te
22224 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 54 72  stcase( jumpIfTr
22225 75 65 3d 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e  ue==0 && jumpIfN
22226 75 6c 6c 3d 3d 30 20 26 26 20 72 65 67 46 72 65  ull==0 && regFre
22227 65 31 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63  e1==0 );.  testc
22228 61 73 65 28 20 6a 75 6d 70 49 66 54 72 75 65 3d  ase( jumpIfTrue=
22229 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c 6c  =0 && jumpIfNull
2222a 3d 3d 30 20 26 26 20 72 65 67 46 72 65 65 31 21  ==0 && regFree1!
2222b 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  =0 );.  testcase
2222c 28 20 6a 75 6d 70 49 66 54 72 75 65 3d 3d 30 20  ( jumpIfTrue==0 
2222d 26 26 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30  && jumpIfNull!=0
2222e 20 26 26 20 72 65 67 46 72 65 65 31 3d 3d 30 20   && regFree1==0 
2222f 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6a  );.  testcase( j
22230 75 6d 70 49 66 54 72 75 65 3d 3d 30 20 26 26 20  umpIfTrue==0 && 
22231 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 20 26 26  jumpIfNull!=0 &&
22232 20 72 65 67 46 72 65 65 31 21 3d 30 20 29 3b 0a   regFree1!=0 );.
22233 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
22234 49 66 54 72 75 65 21 3d 30 20 26 26 20 6a 75 6d  IfTrue!=0 && jum
22235 70 49 66 4e 75 6c 6c 3d 3d 30 20 26 26 20 72 65  pIfNull==0 && re
22236 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 74  gFree1==0 );.  t
22237 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 54  estcase( jumpIfT
22238 72 75 65 21 3d 30 20 26 26 20 6a 75 6d 70 49 66  rue!=0 && jumpIf
22239 4e 75 6c 6c 3d 3d 30 20 26 26 20 72 65 67 46 72  Null==0 && regFr
2223a 65 65 31 21 3d 30 20 29 3b 0a 20 20 74 65 73 74  ee1!=0 );.  test
2223b 63 61 73 65 28 20 6a 75 6d 70 49 66 54 72 75 65  case( jumpIfTrue
2223c 21 3d 30 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c  !=0 && jumpIfNul
2223d 6c 21 3d 30 20 26 26 20 72 65 67 46 72 65 65 31  l!=0 && regFree1
2223e 3d 3d 30 20 29 3b 0a 20 20 74 65 73 74 63 61 73  ==0 );.  testcas
2223f 65 28 20 6a 75 6d 70 49 66 54 72 75 65 21 3d 30  e( jumpIfTrue!=0
22240 20 26 26 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d   && jumpIfNull!=
22241 30 20 26 26 20 72 65 67 46 72 65 65 31 21 3d 30  0 && regFree1!=0
22242 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   );.}../*.** Gen
22243 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61  erate code for a
22244 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73   boolean express
22245 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 20  ion such that a 
22246 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20  jump is made.** 
22247 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65  to the label "de
22248 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72 65  st" if the expre
22249 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20 62 75  ssion is true bu
2224a 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 63  t execution.** c
2224b 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67 68  ontinues straigh
2224c 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65 78  t thru if the ex
2224d 70 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c 73  pression is fals
2224e 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
2224f 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75  expression evalu
22250 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65  ates to NULL (ne
22251 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66  ither true nor f
22252 61 6c 73 65 29 2c 20 74 68 65 6e 0a 2a 2a 20 74  alse), then.** t
22253 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20  ake the jump if 
22254 74 68 65 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 66  the jumpIfNull f
22255 6c 61 67 20 69 73 20 53 51 4c 49 54 45 5f 4a 55  lag is SQLITE_JU
22256 4d 50 49 46 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20  MPIFNULL..**.** 
22257 54 68 69 73 20 63 6f 64 65 20 64 65 70 65 6e 64  This code depend
22258 73 20 6f 6e 20 74 68 65 20 66 61 63 74 20 74 68  s on the fact th
22259 61 74 20 63 65 72 74 61 69 6e 20 74 6f 6b 65 6e  at certain token
2225a 20 76 61 6c 75 65 73 20 28 65 78 3a 20 54 4b 5f   values (ex: TK_
2225b 45 51 29 0a 2a 2a 20 61 72 65 20 74 68 65 20 73  EQ).** are the s
2225c 61 6d 65 20 61 73 20 6f 70 63 6f 64 65 20 76 61  ame as opcode va
2225d 6c 75 65 73 20 28 65 78 3a 20 4f 50 5f 45 71 29  lues (ex: OP_Eq)
2225e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
2225f 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
22260 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20  g.** operation. 
22261 20 53 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e 74   Special comment
22262 73 20 69 6e 20 76 64 62 65 2e 63 20 61 6e 64 20  s in vdbe.c and 
22263 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77  the mkopcodeh.aw
22264 6b 20 73 63 72 69 70 74 20 69 6e 0a 2a 2a 20 74  k script in.** t
22265 68 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73 20  he make process 
22266 63 61 75 73 65 20 74 68 65 73 65 20 76 61 6c 75  cause these valu
22267 65 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41 73  es to align.  As
22268 73 65 72 74 28 29 73 20 69 6e 20 74 68 65 20 63  sert()s in the c
22269 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77 20 76 65 72  ode.** below ver
2226a 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75 6d  ify that the num
2226b 62 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65 64  bers are aligned
2226c 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a 53   correctly..*/.S
2226d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2226e 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49 66  id sqlite3ExprIf
2226f 54 72 75 65 28 50 61 72 73 65 20 2a 70 50 61 72  True(Parse *pPar
22270 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c  se, Expr *pExpr,
22271 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20 6a   int dest, int j
22272 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56 64  umpIfNull){.  Vd
22273 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e  be *v = pParse->
22274 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70 20  pVdbe;.  int op 
22275 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72  = 0;.  int regFr
22276 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  ee1 = 0;.  int r
22277 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20 69  egFree2 = 0;.  i
22278 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61 73  nt r1, r2;..  as
22279 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  sert( jumpIfNull
2227a 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  ==SQLITE_JUMPIFN
2227b 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c  ULL || jumpIfNul
2227c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 4e 45  l==0 );.  if( NE
2227d 56 45 52 28 76 3d 3d 30 29 20 29 20 20 20 20 20  VER(v==0) )     
2227e 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 78 69 73  return;  /* Exis
2227f 74 61 6e 63 65 20 6f 66 20 56 44 42 45 20 63 68  tance of VDBE ch
22280 65 63 6b 65 64 20 62 79 20 63 61 6c 6c 65 72 20  ecked by caller 
22281 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  */.  if( NEVER(p
22282 45 78 70 72 3d 3d 30 29 20 29 20 72 65 74 75 72  Expr==0) ) retur
22283 6e 3b 20 20 2f 2a 20 4e 6f 20 77 61 79 20 74 68  n;  /* No way th
22284 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 2a 2f  is can happen */
22285 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f  .  op = pExpr->o
22286 70 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20  p;.  switch( op 
22287 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  ){.    case TK_A
22288 4e 44 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ND: {.      int 
22289 64 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  d2 = sqlite3Vdbe
2228a 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
2228b 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
2228c 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
2228d 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2228e 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65  CachePush(pParse
2228f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
22290 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
22291 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
22292 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75 6c 6c 5e  , d2,jumpIfNull^
22293 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
22294 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  L);.      sqlite
22295 33 45 78 70 72 49 66 54 72 75 65 28 70 50 61 72  3ExprIfTrue(pPar
22296 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  se, pExpr->pRigh
22297 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  t, dest, jumpIfN
22298 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ull);.      sqli
22299 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2229a 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20  bel(v, d2);.    
2229b 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2229c 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29  hePop(pParse, 1)
2229d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2229e 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
2229f 5f 4f 52 3a 20 7b 0a 20 20 20 20 20 20 74 65 73  _OR: {.      tes
222a0 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
222a1 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71  l==0 );.      sq
222a2 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
222a3 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
222a4 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  Left, dest, jump
222a5 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 73  IfNull);.      s
222a6 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65  qlite3ExprIfTrue
222a7 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
222a8 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75  pRight, dest, ju
222a9 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
222aa 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
222ab 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b    case TK_NOT: {
222ac 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
222ad 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
222ae 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
222af 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
222b0 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
222b1 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
222b2 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  l);.      break;
222b3 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
222b4 54 4b 5f 4c 54 3a 0a 20 20 20 20 63 61 73 65 20  TK_LT:.    case 
222b5 54 4b 5f 4c 45 3a 0a 20 20 20 20 63 61 73 65 20  TK_LE:.    case 
222b6 54 4b 5f 47 54 3a 0a 20 20 20 20 63 61 73 65 20  TK_GT:.    case 
222b7 54 4b 5f 47 45 3a 0a 20 20 20 20 63 61 73 65 20  TK_GE:.    case 
222b8 54 4b 5f 4e 45 3a 0a 20 20 20 20 63 61 73 65 20  TK_NE:.    case 
222b9 54 4b 5f 45 51 3a 20 7b 0a 20 20 20 20 20 20 61  TK_EQ: {.      a
222ba 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50  ssert( TK_LT==OP
222bb 5f 4c 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _Lt );.      ass
222bc 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c  ert( TK_LE==OP_L
222bd 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  e );.      asser
222be 74 28 20 54 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20  t( TK_GT==OP_Gt 
222bf 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
222c0 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47 65 20 29 3b   TK_GE==OP_Ge );
222c1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
222c2 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20  K_EQ==OP_Eq );. 
222c3 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
222c4 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b 0a 20 20 20  NE==OP_Ne );.   
222c5 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
222c6 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20  =TK_LT );.      
222c7 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
222c8 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _LE );.      tes
222c9 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54  tcase( op==TK_GT
222ca 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
222cb 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b  se( op==TK_GE );
222cc 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
222cd 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20   op==TK_EQ );.  
222ce 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
222cf 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20  ==TK_NE );.     
222d0 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
222d1 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
222d2 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
222d3 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45  rands(pParse, pE
222d4 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c  xpr->pLeft, &r1,
222d5 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
222d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
222d8 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32  xpr->pRight, &r2
222d9 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
222da 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
222db 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
222dc 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Left, pExpr->pRi
222dd 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20  ght, op,.       
222de 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72             r1, r
222df 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  2, dest, jumpIfN
222e0 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ull);.      test
222e1 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
222e2 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
222e3 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
222e4 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
222e5 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
222e6 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73 65 20  TK_IS:.    case 
222e7 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20 20 20  TK_ISNOT: {.    
222e8 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
222e9 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20 20 20 74  TK_IS );.      t
222ea 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
222eb 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 63  ISNOT );.      c
222ec 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
222ed 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ds(pParse, pExpr
222ee 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72  ->pLeft, &r1, &r
222ef 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20  egFree1,.       
222f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222f1 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
222f2 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26  ->pRight, &r2, &
222f3 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
222f4 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b 5f 49 53   op = (op==TK_IS
222f5 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54 4b 5f 4e  ) ? TK_EQ : TK_N
222f6 45 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  E;.      codeCom
222f7 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78  pare(pParse, pEx
222f8 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72  pr->pLeft, pExpr
222f9 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20  ->pRight, op,.  
222fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222fb 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 53 51  r1, r2, dest, SQ
222fc 4c 49 54 45 5f 4e 55 4c 4c 45 51 29 3b 0a 20 20  LITE_NULLEQ);.  
222fd 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
222fe 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
222ff 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
22300 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20  Free2==0 );.    
22301 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
22302 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c     case TK_ISNUL
22303 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  L:.    case TK_N
22304 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  OTNULL: {.      
22305 61 73 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c  assert( TK_ISNUL
22306 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a  L==OP_IsNull );.
22307 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
22308 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74  _NOTNULL==OP_Not
22309 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65  Null );.      te
2230a 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49  stcase( op==TK_I
2230b 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74  SNULL );.      t
2230c 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2230d 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20  NOTNULL );.     
2230e 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r1 = sqlite3Exp
2230f 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
22310 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
22311 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20  &regFree1);.    
22312 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22313 4f 70 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64  Op2(v, op, r1, d
22314 65 73 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74  est);.      test
22315 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
22316 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
22317 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
22318 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20   TK_BETWEEN: {. 
22319 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
2231a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
2231b 20 20 20 20 20 20 65 78 70 72 43 6f 64 65 42 65        exprCodeBe
2231c 74 77 65 65 6e 28 70 50 61 72 73 65 2c 20 70 45  tween(pParse, pE
2231d 78 70 72 2c 20 64 65 73 74 2c 20 31 2c 20 6a 75  xpr, dest, 1, ju
2231e 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
2231f 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
22320 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a    case TK_IN: {.
22321 20 20 20 20 20 20 69 6e 74 20 64 65 73 74 49 66        int destIf
22322 46 61 6c 73 65 20 3d 20 73 71 6c 69 74 65 33 56  False = sqlite3V
22323 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
22324 0a 20 20 20 20 20 20 69 6e 74 20 64 65 73 74 49  .      int destI
22325 66 4e 75 6c 6c 20 3d 20 6a 75 6d 70 49 66 4e 75  fNull = jumpIfNu
22326 6c 6c 20 3f 20 64 65 73 74 20 3a 20 64 65 73 74  ll ? dest : dest
22327 49 66 46 61 6c 73 65 3b 0a 20 20 20 20 20 20 73  IfFalse;.      s
22328 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 49 4e  qlite3ExprCodeIN
22329 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
2232a 64 65 73 74 49 66 46 61 6c 73 65 2c 20 64 65 73  destIfFalse, des
2232b 74 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  tIfNull);.      
2232c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2232d 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2232e 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20 73 71   dest);.      sq
2232f 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
22330 4c 61 62 65 6c 28 76 2c 20 64 65 73 74 49 66 46  Label(v, destIfF
22331 61 6c 73 65 29 3b 0a 20 20 20 20 20 20 62 72 65  alse);.      bre
22332 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
22333 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72  fault: {.      r
22334 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
22335 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
22336 70 45 78 70 72 2c 20 26 72 65 67 46 72 65 65 31  pExpr, &regFree1
22337 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
22338 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
22339 5f 49 66 2c 20 72 31 2c 20 64 65 73 74 2c 20 6a  _If, r1, dest, j
2233a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30 29 3b 0a 20  umpIfNull!=0);. 
2233b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
2233c 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
2233d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75      testcase( ju
2233e 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20  mpIfNull==0 );. 
2233f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
22340 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52  }.  }.  sqlite3R
22341 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
22342 61 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b  arse, regFree1);
22343 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
22344 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
22345 20 72 65 67 46 72 65 65 32 29 3b 20 20 0a 7d 0a   regFree2);  .}.
22346 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
22347 63 6f 64 65 20 66 6f 72 20 61 20 62 6f 6f 6c 65  code for a boole
22348 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 75  an expression su
22349 63 68 20 74 68 61 74 20 61 20 6a 75 6d 70 20 69  ch that a jump i
2234a 73 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 74 68 65  s made.** to the
2234b 20 6c 61 62 65 6c 20 22 64 65 73 74 22 20 69 66   label "dest" if
2234c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2234d 69 73 20 66 61 6c 73 65 20 62 75 74 20 65 78 65  is false but exe
2234e 63 75 74 69 6f 6e 0a 2a 2a 20 63 6f 6e 74 69 6e  cution.** contin
2234f 75 65 73 20 73 74 72 61 69 67 68 74 20 74 68 72  ues straight thr
22350 75 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73  u if the express
22351 69 6f 6e 20 69 73 20 74 72 75 65 2e 0a 2a 2a 0a  ion is true..**.
22352 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
22353 73 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20 74  sion evaluates t
22354 6f 20 4e 55 4c 4c 20 28 6e 65 69 74 68 65 72 20  o NULL (neither 
22355 74 72 75 65 20 6e 6f 72 20 66 61 6c 73 65 29 20  true nor false) 
22356 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 69 66 20  then.** jump if 
22357 6a 75 6d 70 49 66 4e 75 6c 6c 20 69 73 20 53 51  jumpIfNull is SQ
22358 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20  LITE_JUMPIFNULL 
22359 6f 72 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20  or fall through 
2235a 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c 0a 2a 2a  if jumpIfNull.**
2235b 20 69 73 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45   is 0..*/.SQLITE
2235c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2235d 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
2235e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2235f 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
22360 20 64 65 73 74 2c 20 69 6e 74 20 6a 75 6d 70 49   dest, int jumpI
22361 66 4e 75 6c 6c 29 7b 0a 20 20 56 64 62 65 20 2a  fNull){.  Vdbe *
22362 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
22363 65 3b 0a 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b  e;.  int op = 0;
22364 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20  .  int regFree1 
22365 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46 72  = 0;.  int regFr
22366 65 65 32 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  ee2 = 0;.  int r
22367 31 2c 20 72 32 3b 0a 0a 20 20 61 73 73 65 72 74  1, r2;..  assert
22368 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 53 51  ( jumpIfNull==SQ
22369 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20  LITE_JUMPIFNULL 
2236a 7c 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  || jumpIfNull==0
2236b 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28   );.  if( NEVER(
2236c 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 20  v==0) ) return; 
2236d 2f 2a 20 45 78 69 73 74 61 6e 63 65 20 6f 66 20  /* Existance of 
2236e 56 44 42 45 20 63 68 65 63 6b 65 64 20 62 79 20  VDBE checked by 
2236f 63 61 6c 6c 65 72 20 2a 2f 0a 20 20 69 66 28 20  caller */.  if( 
22370 70 45 78 70 72 3d 3d 30 20 29 20 20 20 20 72 65  pExpr==0 )    re
22371 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  turn;..  /* The 
22372 76 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2d 3e  value of pExpr->
22373 6f 70 20 61 6e 64 20 6f 70 20 61 72 65 20 72 65  op and op are re
22374 6c 61 74 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  lated as follows
22375 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
22376 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20 20 20    pExpr->op     
22377 20 20 20 20 20 20 20 6f 70 0a 20 20 2a 2a 20 20         op.  **  
22378 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20       ---------  
22379 20 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d          --------
2237a 2d 2d 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b  --.  **       TK
2237b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20 20 20  _ISNULL         
2237c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 0a 20 20 2a 2a   OP_NotNull.  **
2237d 20 20 20 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c         TK_NOTNUL
2237e 4c 20 20 20 20 20 20 20 20 20 4f 50 5f 49 73 4e  L         OP_IsN
2237f 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  ull.  **       T
22380 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20 20 20  K_NE            
22381 20 20 4f 50 5f 45 71 0a 20 20 2a 2a 20 20 20 20    OP_Eq.  **    
22382 20 20 20 54 4b 5f 45 51 20 20 20 20 20 20 20 20     TK_EQ        
22383 20 20 20 20 20 20 4f 50 5f 4e 65 0a 20 20 2a 2a        OP_Ne.  **
22384 20 20 20 20 20 20 20 54 4b 5f 47 54 20 20 20 20         TK_GT    
22385 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 65 0a            OP_Le.
22386 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c 45    **       TK_LE
22387 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
22388 5f 47 74 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  _Gt.  **       T
22389 4b 5f 47 45 20 20 20 20 20 20 20 20 20 20 20 20  K_GE            
2238a 20 20 4f 50 5f 4c 74 0a 20 20 2a 2a 20 20 20 20    OP_Lt.  **    
2238b 20 20 20 54 4b 5f 4c 54 20 20 20 20 20 20 20 20     TK_LT        
2238c 20 20 20 20 20 20 4f 50 5f 47 65 0a 20 20 2a 2a        OP_Ge.  **
2238d 0a 20 20 2a 2a 20 46 6f 72 20 6f 74 68 65 72 20  .  ** For other 
2238e 76 61 6c 75 65 73 20 6f 66 20 70 45 78 70 72 2d  values of pExpr-
2238f 3e 6f 70 2c 20 6f 70 20 69 73 20 75 6e 64 65 66  >op, op is undef
22390 69 6e 65 64 20 61 6e 64 20 75 6e 75 73 65 64 2e  ined and unused.
22391 0a 20 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  .  ** The value 
22392 6f 66 20 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63  of TK_ and OP_ c
22393 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 61 72 72  onstants are arr
22394 61 6e 67 65 64 20 73 75 63 68 20 74 68 61 74 20  anged such that 
22395 77 65 0a 20 20 2a 2a 20 63 61 6e 20 63 6f 6d 70  we.  ** can comp
22396 75 74 65 20 74 68 65 20 6d 61 70 70 69 6e 67 20  ute the mapping 
22397 61 62 6f 76 65 20 75 73 69 6e 67 20 74 68 65 20  above using the 
22398 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73  following expres
22399 73 69 6f 6e 2e 0a 20 20 2a 2a 20 41 73 73 65 72  sion..  ** Asser
2239a 74 28 29 73 20 76 65 72 69 66 79 20 74 68 61 74  t()s verify that
2239b 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e   the computation
2239c 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a   is correct..  *
2239d 2f 0a 20 20 6f 70 20 3d 20 28 28 70 45 78 70 72  /.  op = ((pExpr
2239e 2d 3e 6f 70 2b 28 54 4b 5f 49 53 4e 55 4c 4c 26  ->op+(TK_ISNULL&
2239f 31 29 29 5e 31 29 2d 28 54 4b 5f 49 53 4e 55 4c  1))^1)-(TK_ISNUL
223a0 4c 26 31 29 3b 0a 0a 20 20 2f 2a 20 56 65 72 69  L&1);..  /* Veri
223a1 66 79 20 63 6f 72 72 65 63 74 20 61 6c 69 67 6e  fy correct align
223a2 6d 65 6e 74 20 6f 66 20 54 4b 5f 20 61 6e 64 20  ment of TK_ and 
223a3 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 0a 20 20  OP_ constants.  
223a4 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  */.  assert( pEx
223a5 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49 53 4e 55 4c  pr->op!=TK_ISNUL
223a6 4c 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 6f 74 4e  L || op==OP_NotN
223a7 75 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ull );.  assert(
223a8 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e   pExpr->op!=TK_N
223a9 4f 54 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50  OTNULL || op==OP
223aa 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 61 73 73  _IsNull );.  ass
223ab 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
223ac 54 4b 5f 4e 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f  TK_NE || op==OP_
223ad 45 71 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Eq );.  assert( 
223ae 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 45 51  pExpr->op!=TK_EQ
223af 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4e 65 20 29 3b   || op==OP_Ne );
223b0 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72  .  assert( pExpr
223b1 2d 3e 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20 6f  ->op!=TK_LT || o
223b2 70 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 61 73  p==OP_Ge );.  as
223b3 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
223b4 3d 54 4b 5f 4c 45 20 7c 7c 20 6f 70 3d 3d 4f 50  =TK_LE || op==OP
223b5 5f 47 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _Gt );.  assert(
223b6 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47   pExpr->op!=TK_G
223b7 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 65 20 29  T || op==OP_Le )
223b8 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70  ;.  assert( pExp
223b9 72 2d 3e 6f 70 21 3d 54 4b 5f 47 45 20 7c 7c 20  r->op!=TK_GE || 
223ba 6f 70 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 0a 20 20  op==OP_Lt );..  
223bb 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f  switch( pExpr->o
223bc 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b  p ){.    case TK
223bd 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20 74 65  _AND: {.      te
223be 73 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75  stcase( jumpIfNu
223bf 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  ll==0 );.      s
223c0 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
223c1 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
223c2 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75  >pLeft, dest, ju
223c3 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
223c4 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61   sqlite3ExprIfFa
223c5 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  lse(pParse, pExp
223c6 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c  r->pRight, dest,
223c7 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
223c8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
223c9 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a  .    case TK_OR:
223ca 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20   {.      int d2 
223cb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
223cc 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
223cd 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
223ce 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
223cf 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
223d0 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a  hePush(pParse);.
223d1 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
223d2 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
223d3 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 32  pExpr->pLeft, d2
223d4 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 5e 53 51 4c  , jumpIfNull^SQL
223d5 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
223d6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
223d7 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
223d8 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
223d9 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
223da 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  l);.      sqlite
223db 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
223dc 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20 20 20  l(v, d2);.      
223dd 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
223de 50 6f 70 28 70 50 61 72 73 65 2c 20 31 29 3b 0a  Pop(pParse, 1);.
223df 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
223e0 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e   }.    case TK_N
223e1 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  OT: {.      test
223e2 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
223e3 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
223e4 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
223e5 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
223e6 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49  eft, dest, jumpI
223e7 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72  fNull);.      br
223e8 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
223e9 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63  ase TK_LT:.    c
223ea 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63  ase TK_LE:.    c
223eb 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63  ase TK_GT:.    c
223ec 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63  ase TK_GE:.    c
223ed 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63  ase TK_NE:.    c
223ee 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20  ase TK_EQ: {.   
223ef 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
223f0 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20  =TK_LT );.      
223f1 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
223f2 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _LE );.      tes
223f3 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54  tcase( op==TK_GT
223f4 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
223f5 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b  se( op==TK_GE );
223f6 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
223f7 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20   op==TK_EQ );.  
223f8 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
223f9 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20  ==TK_NE );.     
223fa 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
223fb 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
223fc 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
223fd 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45  rands(pParse, pE
223fe 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c  xpr->pLeft, &r1,
223ff 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
22400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22401 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
22402 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32  xpr->pRight, &r2
22403 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
22404 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
22405 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
22406 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Left, pExpr->pRi
22407 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20  ght, op,.       
22408 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72             r1, r
22409 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  2, dest, jumpIfN
2240a 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ull);.      test
2240b 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
2240c 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
2240d 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
2240e 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
2240f 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
22410 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73 65 20  TK_IS:.    case 
22411 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20 20 20  TK_ISNOT: {.    
22412 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
22413 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 20 29 3b 0a  r->op==TK_IS );.
22414 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
22415 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53  pExpr->op==TK_IS
22416 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 63 6f 64  NOT );.      cod
22417 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73  eCompareOperands
22418 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
22419 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67  pLeft, &r1, &reg
2241a 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20  Free1,.         
2241b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2241c 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
2241d 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65  pRight, &r2, &re
2241e 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 6f  gFree2);.      o
2241f 70 20 3d 20 28 70 45 78 70 72 2d 3e 6f 70 3d 3d  p = (pExpr->op==
22420 54 4b 5f 49 53 29 20 3f 20 54 4b 5f 4e 45 20 3a  TK_IS) ? TK_NE :
22421 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20 20 63 6f   TK_EQ;.      co
22422 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65  deCompare(pParse
22423 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
22424 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f  pExpr->pRight, o
22425 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  p,.             
22426 20 20 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73       r1, r2, des
22427 74 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51  t, SQLITE_NULLEQ
22428 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
22429 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
2242a 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2242b 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b  ( regFree2==0 );
2242c 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2242d 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
2242e 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65  ISNULL:.    case
2242f 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20   TK_NOTNULL: {. 
22430 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
22431 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a  p==TK_ISNULL );.
22432 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
22433 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29  op==TK_NOTNULL )
22434 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ;.      r1 = sql
22435 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
22436 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
22437 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65 31  pLeft, &regFree1
22438 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
22439 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 6f 70  VdbeAddOp2(v, op
2243a 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a 20 20 20  , r1, dest);.   
2243b 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
2243c 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
2243d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2243e 20 20 20 63 61 73 65 20 54 4b 5f 42 45 54 57 45     case TK_BETWE
2243f 45 4e 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  EN: {.      test
22440 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
22441 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65 78 70  ==0 );.      exp
22442 72 43 6f 64 65 42 65 74 77 65 65 6e 28 70 50 61  rCodeBetween(pPa
22443 72 73 65 2c 20 70 45 78 70 72 2c 20 64 65 73 74  rse, pExpr, dest
22444 2c 20 30 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  , 0, jumpIfNull)
22445 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
22446 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
22447 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  _IN: {.      if(
22448 20 6a 75 6d 70 49 66 4e 75 6c 6c 20 29 7b 0a 20   jumpIfNull ){. 
22449 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2244a 70 72 43 6f 64 65 49 4e 28 70 50 61 72 73 65 2c  prCodeIN(pParse,
2244b 20 70 45 78 70 72 2c 20 64 65 73 74 2c 20 64 65   pExpr, dest, de
2244c 73 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  st);.      }else
2244d 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 64 65  {.        int de
2244e 73 74 49 66 4e 75 6c 6c 20 3d 20 73 71 6c 69 74  stIfNull = sqlit
2244f 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
22450 76 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  v);.        sqli
22451 74 65 33 45 78 70 72 43 6f 64 65 49 4e 28 70 50  te3ExprCodeIN(pP
22452 61 72 73 65 2c 20 70 45 78 70 72 2c 20 64 65 73  arse, pExpr, des
22453 74 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a  t, destIfNull);.
22454 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
22455 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
22456 76 2c 20 64 65 73 74 49 66 4e 75 6c 6c 29 3b 0a  v, destIfNull);.
22457 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
22458 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
22459 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
2245a 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r1 = sqlite3Expr
2245b 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
2245c 20 70 45 78 70 72 2c 20 26 72 65 67 46 72 65 65   pExpr, &regFree
2245d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2245e 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2245f 50 5f 49 66 4e 6f 74 2c 20 72 31 2c 20 64 65 73  P_IfNot, r1, des
22460 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21 3d 30  t, jumpIfNull!=0
22461 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
22462 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
22463 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
22464 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  ( jumpIfNull==0 
22465 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
22466 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
22467 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
22468 67 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65  g(pParse, regFre
22469 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  e1);.  sqlite3Re
2246a 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2246b 72 73 65 2c 20 72 65 67 46 72 65 65 32 29 3b 0a  rse, regFree2);.
2246c 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 64 65  }../*.** Do a de
2246d 65 70 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 66  ep comparison of
2246e 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 20   two expression 
2246f 74 72 65 65 73 2e 20 20 52 65 74 75 72 6e 20 54  trees.  Return T
22470 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29 0a 2a  RUE (non-zero).*
22471 2a 20 69 66 20 74 68 65 79 20 61 72 65 20 69 64  * if they are id
22472 65 6e 74 69 63 61 6c 20 61 6e 64 20 72 65 74 75  entical and retu
22473 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68 65 79  rn FALSE if they
22474 20 64 69 66 66 65 72 20 69 6e 20 61 6e 79 20 77   differ in any w
22475 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69  ay..**.** Someti
22476 6d 65 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  mes this routine
22477 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 46 41 4c   will return FAL
22478 53 45 20 65 76 65 6e 20 69 66 20 74 68 65 20 74  SE even if the t
22479 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a  wo expressions.*
2247a 2a 20 72 65 61 6c 6c 79 20 61 72 65 20 65 71 75  * really are equ
2247b 69 76 61 6c 65 6e 74 2e 20 20 49 66 20 77 65 20  ivalent.  If we 
2247c 63 61 6e 6e 6f 74 20 70 72 6f 76 65 20 74 68 61  cannot prove tha
2247d 74 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  t the expression
2247e 73 20 61 72 65 0a 2a 2a 20 69 64 65 6e 74 69 63  s are.** identic
2247f 61 6c 2c 20 77 65 20 72 65 74 75 72 6e 20 46 41  al, we return FA
22480 4c 53 45 20 6a 75 73 74 20 74 6f 20 62 65 20 73  LSE just to be s
22481 61 66 65 2e 20 20 53 6f 20 69 66 20 74 68 69 73  afe.  So if this
22482 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75   routine.** retu
22483 72 6e 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20  rns false, then 
22484 79 6f 75 20 64 6f 20 6e 6f 74 20 72 65 61 6c 6c  you do not reall
22485 79 20 6b 6e 6f 77 20 66 6f 72 20 63 65 72 74 61  y know for certa
22486 69 6e 20 69 66 20 74 68 65 20 74 77 6f 0a 2a 2a  in if the two.**
22487 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65   expressions are
22488 20 74 68 65 20 73 61 6d 65 2e 20 20 42 75 74 20   the same.  But 
22489 69 66 20 79 6f 75 20 67 65 74 20 61 20 54 52 55  if you get a TRU
2248a 45 20 72 65 74 75 72 6e 2c 20 74 68 65 6e 20 79  E return, then y
2248b 6f 75 0a 2a 2a 20 63 61 6e 20 62 65 20 73 75 72  ou.** can be sur
2248c 65 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  e the expression
2248d 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20  s are the same. 
2248e 20 49 6e 20 74 68 65 20 70 6c 61 63 65 73 20 77   In the places w
2248f 68 65 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75  here.** this rou
22490 74 69 6e 65 20 69 73 20 75 73 65 64 2c 20 69 74  tine is used, it
22491 20 64 6f 65 73 20 6e 6f 74 20 68 75 72 74 20 74   does not hurt t
22492 6f 20 67 65 74 20 61 6e 20 65 78 74 72 61 20 46  o get an extra F
22493 41 4c 53 45 20 2d 20 74 68 61 74 0a 2a 2a 20 6a  ALSE - that.** j
22494 75 73 74 20 6d 69 67 68 74 20 72 65 73 75 6c 74  ust might result
22495 20 69 6e 20 73 6f 6d 65 20 73 6c 69 67 68 74 6c   in some slightl
22496 79 20 73 6c 6f 77 65 72 20 63 6f 64 65 2e 20 20  y slower code.  
22497 42 75 74 20 72 65 74 75 72 6e 69 6e 67 0a 2a 2a  But returning.**
22498 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 54 52   an incorrect TR
22499 55 45 20 63 6f 75 6c 64 20 6c 65 61 64 20 74 6f  UE could lead to
2249a 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a   a malfunction..
2249b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2249c 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
2249d 72 43 6f 6d 70 61 72 65 28 45 78 70 72 20 2a 70  rCompare(Expr *p
2249e 41 2c 20 45 78 70 72 20 2a 70 42 29 7b 0a 20 20  A, Expr *pB){.  
2249f 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 41 3d  int i;.  if( pA=
224a0 3d 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a 20 20 20  =0||pB==0 ){.   
224a1 20 72 65 74 75 72 6e 20 70 42 3d 3d 70 41 3b 0a   return pB==pA;.
224a2 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 45    }.  assert( !E
224a3 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
224a4 79 28 70 41 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  y(pA, EP_TokenOn
224a5 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29  ly|EP_Reduced) )
224a6 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45 78 70  ;.  assert( !Exp
224a7 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
224a8 70 42 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  pB, EP_TokenOnly
224a9 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a  |EP_Reduced) );.
224aa 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
224ab 70 65 72 74 79 28 70 41 2c 20 45 50 5f 78 49 73  perty(pA, EP_xIs
224ac 53 65 6c 65 63 74 29 20 7c 7c 20 45 78 70 72 48  Select) || ExprH
224ad 61 73 50 72 6f 70 65 72 74 79 28 70 42 2c 20 45  asProperty(pB, E
224ae 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a  P_xIsSelect) ){.
224af 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
224b0 7d 0a 20 20 69 66 28 20 28 70 41 2d 3e 66 6c 61  }.  if( (pA->fla
224b1 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63 74  gs & EP_Distinct
224b2 29 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20 26 20  )!=(pB->flags & 
224b3 45 50 5f 44 69 73 74 69 6e 63 74 29 20 29 20 72  EP_Distinct) ) r
224b4 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70  eturn 0;.  if( p
224b5 41 2d 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20  A->op!=pB->op ) 
224b6 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
224b7 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70  !sqlite3ExprComp
224b8 61 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c 20 70  are(pA->pLeft, p
224b9 42 2d 3e 70 4c 65 66 74 29 20 29 20 72 65 74 75  B->pLeft) ) retu
224ba 72 6e 20 30 3b 0a 20 20 69 66 28 20 21 73 71 6c  rn 0;.  if( !sql
224bb 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28  ite3ExprCompare(
224bc 70 41 2d 3e 70 52 69 67 68 74 2c 20 70 42 2d 3e  pA->pRight, pB->
224bd 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
224be 20 30 3b 0a 0a 20 20 69 66 28 20 70 41 2d 3e 78   0;..  if( pA->x
224bf 2e 70 4c 69 73 74 20 26 26 20 70 42 2d 3e 78 2e  .pList && pB->x.
224c0 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 69 66 28  pList ){.    if(
224c1 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45   pA->x.pList->nE
224c2 78 70 72 21 3d 70 42 2d 3e 78 2e 70 4c 69 73 74  xpr!=pB->x.pList
224c3 2d 3e 6e 45 78 70 72 20 29 20 72 65 74 75 72 6e  ->nExpr ) return
224c4 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   0;.    for(i=0;
224c5 20 69 3c 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e   i<pA->x.pList->
224c6 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
224c7 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 41 20     Expr *pExprA 
224c8 3d 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61  = pA->x.pList->a
224c9 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
224ca 20 45 78 70 72 20 2a 70 45 78 70 72 42 20 3d 20   Expr *pExprB = 
224cb 70 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 69  pB->x.pList->a[i
224cc 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69  ].pExpr;.      i
224cd 66 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43  f( !sqlite3ExprC
224ce 6f 6d 70 61 72 65 28 70 45 78 70 72 41 2c 20 70  ompare(pExprA, p
224cf 45 78 70 72 42 29 20 29 20 72 65 74 75 72 6e 20  ExprB) ) return 
224d0 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  0;.    }.  }else
224d1 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74   if( pA->x.pList
224d2 20 7c 7c 20 70 42 2d 3e 78 2e 70 4c 69 73 74 20   || pB->x.pList 
224d3 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
224d4 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 41 2d 3e  .  }..  if( pA->
224d5 69 54 61 62 6c 65 21 3d 70 42 2d 3e 69 54 61 62  iTable!=pB->iTab
224d6 6c 65 20 7c 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d  le || pA->iColum
224d7 6e 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29  n!=pB->iColumn )
224d8 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
224d9 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
224da 28 70 41 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  (pA, EP_IntValue
224db 29 20 29 7b 0a 20 20 20 20 69 66 28 20 21 45 78  ) ){.    if( !Ex
224dc 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42  prHasProperty(pB
224dd 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 7c  , EP_IntValue) |
224de 7c 20 70 41 2d 3e 75 2e 69 56 61 6c 75 65 21 3d  | pA->u.iValue!=
224df 70 42 2d 3e 75 2e 69 56 61 6c 75 65 20 29 7b 0a  pB->u.iValue ){.
224e0 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
224e1 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
224e2 28 20 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c  ( pA->op!=TK_COL
224e3 55 4d 4e 20 26 26 20 70 41 2d 3e 75 2e 7a 54 6f  UMN && pA->u.zTo
224e4 6b 65 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 45  ken ){.    if( E
224e5 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
224e6 42 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20  B, EP_IntValue) 
224e7 7c 7c 20 4e 45 56 45 52 28 70 42 2d 3e 75 2e 7a  || NEVER(pB->u.z
224e8 54 6f 6b 65 6e 3d 3d 30 29 20 29 20 72 65 74 75  Token==0) ) retu
224e9 72 6e 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71  rn 0;.    if( sq
224ea 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 41 2d  lite3StrICmp(pA-
224eb 3e 75 2e 7a 54 6f 6b 65 6e 2c 70 42 2d 3e 75 2e  >u.zToken,pB->u.
224ec 7a 54 6f 6b 65 6e 29 21 3d 30 20 29 7b 0a 20 20  zToken)!=0 ){.  
224ed 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
224ee 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
224ef 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64   1;.}.../*.** Ad
224f0 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20  d a new element 
224f1 74 6f 20 74 68 65 20 70 41 67 67 49 6e 66 6f 2d  to the pAggInfo-
224f2 3e 61 43 6f 6c 5b 5d 20 61 72 72 61 79 2e 20 20  >aCol[] array.  
224f3 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65 78  Return the index
224f4 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 65   of.** the new e
224f5 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75 72 6e 20  lement.  Return 
224f6 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65  a negative numbe
224f7 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  r if malloc fail
224f8 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
224f9 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d   addAggInfoColum
224fa 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 41  n(sqlite3 *db, A
224fb 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a  ggInfo *pInfo){.
224fc 20 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e 66 6f    int i;.  pInfo
224fd 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33  ->aCol = sqlite3
224fe 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20  ArrayAllocate(. 
224ff 20 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20        db,.      
22500 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a 20 20   pInfo->aCol,.  
22501 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49 6e 66       sizeof(pInf
22502 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20 20 20  o->aCol[0]),.   
22503 20 20 20 20 33 2c 0a 20 20 20 20 20 20 20 26 70      3,.       &p
22504 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c 0a 20  Info->nColumn,. 
22505 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e 43        &pInfo->nC
22506 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20 20 20  olumnAlloc,.    
22507 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 72 65 74     &i.  );.  ret
22508 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a 2f 2a  urn i;.}    ../*
22509 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c  .** Add a new el
2250a 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67  ement to the pAg
2250b 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 61  gInfo->aFunc[] a
2250c 72 72 61 79 2e 20 20 52 65 74 75 72 6e 20 74 68  rray.  Return th
2250d 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68  e index of.** th
2250e 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20  e new element.  
2250f 52 65 74 75 72 6e 20 61 20 6e 65 67 61 74 69 76  Return a negativ
22510 65 20 6e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c  e number if mall
22511 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61  oc fails..*/.sta
22512 74 69 63 20 69 6e 74 20 61 64 64 41 67 67 49 6e  tic int addAggIn
22513 66 6f 46 75 6e 63 28 73 71 6c 69 74 65 33 20 2a  foFunc(sqlite3 *
22514 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e  db, AggInfo *pIn
22515 66 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  fo){.  int i;.  
22516 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d 20 73  pInfo->aFunc = s
22517 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63  qlite3ArrayAlloc
22518 61 74 65 28 0a 20 20 20 20 20 20 20 64 62 2c 20  ate(.       db, 
22519 0a 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61  .       pInfo->a
2251a 46 75 6e 63 2c 0a 20 20 20 20 20 20 20 73 69 7a  Func,.       siz
2251b 65 6f 66 28 70 49 6e 66 6f 2d 3e 61 46 75 6e 63  eof(pInfo->aFunc
2251c 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 33 2c 0a  [0]),.       3,.
2251d 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e 6e         &pInfo->n
2251e 46 75 6e 63 2c 0a 20 20 20 20 20 20 20 26 70 49  Func,.       &pI
2251f 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c 6c 6f 63 2c  nfo->nFuncAlloc,
22520 0a 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a  .       &i.  );.
22521 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20    return i;.}   
22522 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73   ../*.** This is
22523 20 74 68 65 20 78 45 78 70 72 43 61 6c 6c 62 61   the xExprCallba
22524 63 6b 20 66 6f 72 20 61 20 74 72 65 65 20 77 61  ck for a tree wa
22525 6c 6b 65 72 2e 20 20 49 74 20 69 73 20 75 73 65  lker.  It is use
22526 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  d to.** implemen
22527 74 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61  t sqlite3ExprAna
22528 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 29  lyzeAggregates()
22529 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 45 78  .  See sqlite3Ex
2252a 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  prAnalyzeAggrega
2252b 74 65 73 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74  tes.** for addit
2252c 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
2252d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
2252e 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74   analyzeAggregat
2252f 65 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  e(Walker *pWalke
22530 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b  r, Expr *pExpr){
22531 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65  .  int i;.  Name
22532 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70  Context *pNC = p
22533 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20  Walker->u.pNC;. 
22534 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d   Parse *pParse =
22535 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20   pNC->pParse;.  
22536 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73  SrcList *pSrcLis
22537 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73  t = pNC->pSrcLis
22538 74 3b 0a 20 20 41 67 67 49 6e 66 6f 20 2a 70 41  t;.  AggInfo *pA
22539 67 67 49 6e 66 6f 20 3d 20 70 4e 43 2d 3e 70 41  ggInfo = pNC->pA
2253a 67 67 49 6e 66 6f 3b 0a 0a 20 20 73 77 69 74 63  ggInfo;..  switc
2253b 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a  h( pExpr->op ){.
2253c 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
2253d 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65  COLUMN:.    case
2253e 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20   TK_COLUMN: {.  
2253f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
22540 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f  xpr->op==TK_AGG_
22541 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20  COLUMN );.      
22542 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d  testcase( pExpr-
22543 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29  >op==TK_COLUMN )
22544 3b 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b  ;.      /* Check
22545 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 63   to see if the c
22546 6f 6c 75 6d 6e 20 69 73 20 69 6e 20 6f 6e 65 20  olumn is in one 
22547 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e  of the tables in
22548 20 74 68 65 20 46 52 4f 4d 0a 20 20 20 20 20 20   the FROM.      
22549 2a 2a 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  ** clause of the
2254a 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79   aggregate query
2254b 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c   */.      if( AL
2254c 57 41 59 53 28 70 53 72 63 4c 69 73 74 21 3d 30  WAYS(pSrcList!=0
2254d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74 72  ) ){.        str
2254e 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2254f 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69   *pItem = pSrcLi
22550 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 66  st->a;.        f
22551 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63 4c 69  or(i=0; i<pSrcLi
22552 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70  st->nSrc; i++, p
22553 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
22554 20 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66     struct AggInf
22555 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a 20 20 20  o_col *pCol;.   
22556 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
22557 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
22558 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b  ty(pExpr, EP_Tok
22559 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65  enOnly|EP_Reduce
2255a 64 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  d) );.          
2255b 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  if( pExpr->iTabl
2255c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f  e==pItem->iCurso
2255d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  r ){.           
2255e 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20   /* If we reach 
2255f 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d  this point, it m
22560 65 61 6e 73 20 74 68 61 74 20 70 45 78 70 72 20  eans that pExpr 
22561 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c  refers to a tabl
22562 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  e.            **
22563 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20   that is in the 
22564 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
22565 68 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65  he aggregate que
22566 72 79 2e 20 20 0a 20 20 20 20 20 20 20 20 20 20  ry.  .          
22567 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20    **.           
22568 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72   ** Make an entr
22569 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e  y for the column
2256a 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43   in pAggInfo->aC
2256b 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a 20 20  ol[] if there.  
2256c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20            ** is 
2256d 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20 74 68 65  not an entry the
2256e 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20  re already..    
2256f 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
22570 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20         int k;.  
22571 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d            pCol =
22572 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b   pAggInfo->aCol;
22573 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72  .            for
22574 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f  (k=0; k<pAggInfo
22575 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20  ->nColumn; k++, 
22576 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20  pCol++){.       
22577 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d         if( pCol-
22578 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e  >iTable==pExpr->
22579 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20  iTable &&.      
2257a 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
2257b 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72  ->iColumn==pExpr
2257c 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->iColumn ){.   
2257d 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
2257e 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
2257f 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
22580 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  }.            if
22581 28 20 28 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e  ( (k>=pAggInfo->
22582 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20  nColumn).       
22583 20 20 20 20 20 20 26 26 20 28 6b 20 3d 20 61 64        && (k = ad
22584 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70  dAggInfoColumn(p
22585 50 61 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49  Parse->db, pAggI
22586 6e 66 6f 29 29 3e 3d 30 20 0a 20 20 20 20 20 20  nfo))>=0 .      
22587 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
22588 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70         pCol = &p
22589 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d  AggInfo->aCol[k]
2258a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2258b 70 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78  pCol->pTab = pEx
2258c 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20  pr->pTab;.      
2258d 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54          pCol->iT
2258e 61 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 69 54  able = pExpr->iT
2258f 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20  able;.          
22590 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d      pCol->iColum
22591 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  n = pExpr->iColu
22592 6d 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  mn;.            
22593 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b    pCol->iMem = +
22594 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
22595 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
22596 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  l->iSorterColumn
22597 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20   = -1;.         
22598 20 20 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72       pCol->pExpr
22599 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20   = pExpr;.      
2259a 20 20 20 20 20 20 20 20 69 66 28 20 70 41 67 67          if( pAgg
2259b 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29  Info->pGroupBy )
2259c 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2259d 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20    int j, n;.    
2259e 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72              Expr
2259f 4c 69 73 74 20 2a 70 47 42 20 3d 20 70 41 67 67  List *pGB = pAgg
225a0 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a  Info->pGroupBy;.
225a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
225a2 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
225a3 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47  item *pTerm = pG
225a4 42 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 20 20  B->a;.          
225a5 20 20 20 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e        n = pGB->n
225a6 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Expr;.          
225a7 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
225a8 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b  <n; j++, pTerm++
225a9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
225aa 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20       Expr *pE = 
225ab 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
225ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
225ad 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43  if( pE->op==TK_C
225ae 4f 4c 55 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61  OLUMN && pE->iTa
225af 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62  ble==pExpr->iTab
225b0 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  le &&.          
225b1 20 20 20 20 20 20 20 20 20 20 20 20 70 45 2d 3e              pE->
225b2 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e  iColumn==pExpr->
225b3 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20  iColumn ){.     
225b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
225b5 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75  Col->iSorterColu
225b6 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20  mn = j;.        
225b7 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
225b8 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
225b9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
225ba 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
225bb 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
225bc 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d         if( pCol-
225bd 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30  >iSorterColumn<0
225be 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
225bf 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65      pCol->iSorte
225c0 72 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e  rColumn = pAggIn
225c1 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75  fo->nSortingColu
225c2 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  mn++;.          
225c3 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
225c4 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
225c5 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 77 20  /* There is now 
225c6 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 70 45 78  an entry for pEx
225c7 70 72 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e  pr in pAggInfo->
225c8 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20  aCol[] (either. 
225c9 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65             ** be
225ca 63 61 75 73 65 20 69 74 20 77 61 73 20 74 68 65  cause it was the
225cb 72 65 20 62 65 66 6f 72 65 20 6f 72 20 62 65 63  re before or bec
225cc 61 75 73 65 20 77 65 20 6a 75 73 74 20 63 72 65  ause we just cre
225cd 61 74 65 64 20 69 74 29 2e 0a 20 20 20 20 20 20  ated it)..      
225ce 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74        ** Convert
225cf 20 74 68 65 20 70 45 78 70 72 20 74 6f 20 62 65   the pExpr to be
225d0 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e   a TK_AGG_COLUMN
225d1 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20 74 68   referring to th
225d2 61 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  at.            *
225d3 2a 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c  * pAggInfo->aCol
225d4 5b 5d 20 65 6e 74 72 79 2e 0a 20 20 20 20 20 20  [] entry..      
225d5 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
225d6 20 20 20 20 20 45 78 70 72 53 65 74 49 72 72 65       ExprSetIrre
225d7 64 75 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a  ducible(pExpr);.
225d8 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70              pExp
225d9 72 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20 70 41  r->pAggInfo = pA
225da 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20  ggInfo;.        
225db 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
225dc 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3b 0a 20  TK_AGG_COLUMN;. 
225dd 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
225de 2d 3e 69 41 67 67 20 3d 20 28 69 31 36 29 6b 3b  ->iAgg = (i16)k;
225df 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
225e0 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 20  ak;.          } 
225e1 2f 2a 20 65 6e 64 69 66 20 70 45 78 70 72 2d 3e  /* endif pExpr->
225e2 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69  iTable==pItem->i
225e3 43 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20 20  Cursor */.      
225e4 20 20 7d 20 2f 2a 20 65 6e 64 20 6c 6f 6f 70 20    } /* end loop 
225e5 6f 76 65 72 20 70 53 72 63 4c 69 73 74 20 2a 2f  over pSrcList */
225e6 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
225e7 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
225e8 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
225e9 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a  TK_AGG_FUNCTION:
225ea 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20   {.      /* The 
225eb 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20 74  pNC->nDepth==0 t
225ec 65 73 74 20 63 61 75 73 65 73 20 61 67 67 72 65  est causes aggre
225ed 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69  gate functions i
225ee 6e 20 73 75 62 71 75 65 72 69 65 73 0a 20 20 20  n subqueries.   
225ef 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 67 6e 6f     ** to be igno
225f0 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  red */.      if(
225f1 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30 20   pNC->nDepth==0 
225f2 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68  ){.        /* Ch
225f3 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 70 45  eck to see if pE
225f4 78 70 72 20 69 73 20 61 20 64 75 70 6c 69 63 61  xpr is a duplica
225f5 74 65 20 6f 66 20 61 6e 6f 74 68 65 72 20 61 67  te of another ag
225f6 67 72 65 67 61 74 65 20 0a 20 20 20 20 20 20 20  gregate .       
225f7 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 68 61   ** function tha
225f8 74 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  t is already in 
225f9 74 68 65 20 70 41 67 67 49 6e 66 6f 20 73 74 72  the pAggInfo str
225fa 75 63 74 75 72 65 0a 20 20 20 20 20 20 20 20 2a  ucture.        *
225fb 2f 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74  /.        struct
225fc 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70   AggInfo_func *p
225fd 49 74 65 6d 20 3d 20 70 41 67 67 49 6e 66 6f 2d  Item = pAggInfo-
225fe 3e 61 46 75 6e 63 3b 0a 20 20 20 20 20 20 20 20  >aFunc;.        
225ff 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67 67 49  for(i=0; i<pAggI
22600 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c  nfo->nFunc; i++,
22601 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
22602 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
22603 45 78 70 72 43 6f 6d 70 61 72 65 28 70 49 74 65  ExprCompare(pIte
22604 6d 2d 3e 70 45 78 70 72 2c 20 70 45 78 70 72 29  m->pExpr, pExpr)
22605 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
22606 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
22607 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
22608 20 20 20 20 20 69 66 28 20 69 3e 3d 70 41 67 67       if( i>=pAgg
22609 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b 0a 20  Info->nFunc ){. 
2260a 20 20 20 20 20 20 20 20 20 2f 2a 20 70 45 78 70           /* pExp
2260b 72 20 69 73 20 6f 72 69 67 69 6e 61 6c 2e 20 20  r is original.  
2260c 4d 61 6b 65 20 61 20 6e 65 77 20 65 6e 74 72 79  Make a new entry
2260d 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46   in pAggInfo->aF
2260e 75 6e 63 5b 5d 0a 20 20 20 20 20 20 20 20 20 20  unc[].          
2260f 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75 38 20  */.          u8 
22610 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65  enc = ENC(pParse
22611 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20  ->db);.         
22612 20 69 20 3d 20 61 64 64 41 67 67 49 6e 66 6f 46   i = addAggInfoF
22613 75 6e 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  unc(pParse->db, 
22614 70 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20  pAggInfo);.     
22615 20 20 20 20 20 69 66 28 20 69 3e 3d 30 20 29 7b       if( i>=0 ){
22616 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
22617 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
22618 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
22619 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20  xIsSelect) );.  
2261a 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 20            pItem 
2261b 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75  = &pAggInfo->aFu
2261c 6e 63 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20  nc[i];.         
2261d 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20     pItem->pExpr 
2261e 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  = pExpr;.       
2261f 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4d 65 6d       pItem->iMem
22620 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
22621 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  m;.            a
22622 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
22623 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
22624 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
22625 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d             pItem
22626 2d 3e 70 46 75 6e 63 20 3d 20 73 71 6c 69 74 65  ->pFunc = sqlite
22627 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70 50  3FindFunction(pP
22628 61 72 73 65 2d 3e 64 62 2c 0a 20 20 20 20 20 20  arse->db,.      
22629 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
2262a 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 73 71  pr->u.zToken, sq
2262b 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 45  lite3Strlen30(pE
2262c 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2c 0a  xpr->u.zToken),.
2262d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2262e 20 20 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73     pExpr->x.pLis
2262f 74 20 3f 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t ? pExpr->x.pLi
22630 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20 65  st->nExpr : 0, e
22631 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  nc, 0);.        
22632 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66      if( pExpr->f
22633 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e  lags & EP_Distin
22634 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ct ){.          
22635 20 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74      pItem->iDist
22636 69 6e 63 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e  inct = pParse->n
22637 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  Tab++;.         
22638 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
22639 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69          pItem->i
2263a 44 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20  Distinct = -1;. 
2263b 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2263c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2263d 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d 61   }.        /* Ma
2263e 6b 65 20 70 45 78 70 72 20 70 6f 69 6e 74 20 74  ke pExpr point t
2263f 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
22640 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e  e pAggInfo->aFun
22641 63 5b 5d 20 65 6e 74 72 79 0a 20 20 20 20 20 20  c[] entry.      
22642 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73    */.        ass
22643 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79  ert( !ExprHasAny
22644 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
22645 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f  EP_TokenOnly|EP_
22646 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20  Reduced) );.    
22647 20 20 20 20 45 78 70 72 53 65 74 49 72 72 65 64      ExprSetIrred
22648 75 63 69 62 6c 65 28 70 45 78 70 72 29 3b 0a 20  ucible(pExpr);. 
22649 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 41         pExpr->iA
2264a 67 67 20 3d 20 28 69 31 36 29 69 3b 0a 20 20 20  gg = (i16)i;.   
2264b 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67       pExpr->pAgg
2264c 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b  Info = pAggInfo;
2264d 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2264e 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20  WRC_Prune;.     
2264f 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
22650 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
22651 75 65 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  ue;.}.static int
22652 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74   analyzeAggregat
22653 65 73 49 6e 53 65 6c 65 63 74 28 57 61 6c 6b 65  esInSelect(Walke
22654 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
22655 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20  ct *pSelect){.  
22656 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
22657 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e   = pWalker->u.pN
22658 43 3b 0a 20 20 69 66 28 20 70 4e 43 2d 3e 6e 44  C;.  if( pNC->nD
22659 65 70 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 70  epth==0 ){.    p
2265a 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b 3b 0a 20 20  NC->nDepth++;.  
2265b 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c    sqlite3WalkSel
2265c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 53 65  ect(pWalker, pSe
2265d 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e 43 2d 3e  lect);.    pNC->
2265e 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 20 20 72 65  nDepth--;.    re
2265f 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a  turn WRC_Prune;.
22660 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
22661 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  urn WRC_Continue
22662 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
22663 6e 61 6c 79 7a 65 20 74 68 65 20 67 69 76 65 6e  nalyze the given
22664 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b   expression look
22665 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74  ing for aggregat
22666 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 0a  e functions and.
22667 2a 2a 20 66 6f 72 20 76 61 72 69 61 62 6c 65 73  ** for variables
22668 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65   that need to be
22669 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 50   added to the pP
2266a 61 72 73 65 2d 3e 61 41 67 67 5b 5d 20 61 72 72  arse->aAgg[] arr
2266b 61 79 2e 0a 2a 2a 20 4d 61 6b 65 20 61 64 64 69  ay..** Make addi
2266c 74 69 6f 6e 61 6c 20 65 6e 74 72 69 65 73 20 74  tional entries t
2266d 6f 20 74 68 65 20 70 50 61 72 73 65 2d 3e 61 41  o the pParse->aA
2266e 67 67 5b 5d 20 61 72 72 61 79 20 61 73 20 6e 65  gg[] array as ne
2266f 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54  cessary..**.** T
22670 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
22671 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65  ld only be calle
22672 64 20 61 66 74 65 72 20 74 68 65 20 65 78 70 72  d after the expr
22673 65 73 73 69 6f 6e 20 68 61 73 20 62 65 65 6e 0a  ession has been.
22674 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 62 79 20 73  ** analyzed by s
22675 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
22676 72 4e 61 6d 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c  rNames()..*/.SQL
22677 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
22678 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c   sqlite3ExprAnal
22679 79 7a 65 41 67 67 72 65 67 61 74 65 73 28 4e 61  yzeAggregates(Na
2267a 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20  meContext *pNC, 
2267b 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20  Expr *pExpr){.  
2267c 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 45  Walker w;.  w.xE
2267d 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 61 6e  xprCallback = an
2267e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 3b 0a  alyzeAggregate;.
2267f 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62    w.xSelectCallb
22680 61 63 6b 20 3d 20 61 6e 61 6c 79 7a 65 41 67 67  ack = analyzeAgg
22681 72 65 67 61 74 65 73 49 6e 53 65 6c 65 63 74 3b  regatesInSelect;
22682 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e 43  .  w.u.pNC = pNC
22683 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 43 2d  ;.  assert( pNC-
22684 3e 70 53 72 63 4c 69 73 74 21 3d 30 20 29 3b 0a  >pSrcList!=0 );.
22685 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70    sqlite3WalkExp
22686 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a  r(&w, pExpr);.}.
22687 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69  ./*.** Call sqli
22688 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
22689 67 72 65 67 61 74 65 73 28 29 20 66 6f 72 20 65  gregates() for e
2268a 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  very expression 
2268b 69 6e 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73  in an.** express
2268c 69 6f 6e 20 6c 69 73 74 2e 20 20 52 65 74 75 72  ion list.  Retur
2268d 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
2268e 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  errors..**.** If
2268f 20 61 6e 20 65 72 72 6f 72 20 69 73 20 66 6f 75   an error is fou
22690 6e 64 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73  nd, the analysis
22691 20 69 73 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a   is cut short..*
22692 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
22693 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
22694 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28  rAnalyzeAggList(
22695 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
22696 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73  , ExprList *pLis
22697 74 29 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70  t){.  struct Exp
22698 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
22699 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  m;.  int i;.  if
2269a 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66  ( pList ){.    f
2269b 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  or(pItem=pList->
2269c 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  a, i=0; i<pList-
2269d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  >nExpr; i++, pIt
2269e 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c  em++){.      sql
2269f 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
226a0 67 67 72 65 67 61 74 65 73 28 70 4e 43 2c 20 70  ggregates(pNC, p
226a1 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20  Item->pExpr);.  
226a2 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
226a3 20 41 6c 6c 6f 63 61 74 65 20 61 20 73 69 6e 67   Allocate a sing
226a4 6c 65 20 6e 65 77 20 72 65 67 69 73 74 65 72 20  le new register 
226a5 66 6f 72 20 75 73 65 20 74 6f 20 68 6f 6c 64 20  for use to hold 
226a6 73 6f 6d 65 20 69 6e 74 65 72 6d 65 64 69 61 74  some intermediat
226a7 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 53 51 4c  e result..*/.SQL
226a8 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
226a9 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
226aa 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  g(Parse *pParse)
226ab 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  {.  if( pParse->
226ac 6e 54 65 6d 70 52 65 67 3d 3d 30 20 29 7b 0a 20  nTempReg==0 ){. 
226ad 20 20 20 72 65 74 75 72 6e 20 2b 2b 70 50 61 72     return ++pPar
226ae 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20  se->nMem;.  }.  
226af 72 65 74 75 72 6e 20 70 50 61 72 73 65 2d 3e 61  return pParse->a
226b0 54 65 6d 70 52 65 67 5b 2d 2d 70 50 61 72 73 65  TempReg[--pParse
226b1 2d 3e 6e 54 65 6d 70 52 65 67 5d 3b 0a 7d 0a 0a  ->nTempReg];.}..
226b2 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  /*.** Deallocate
226b3 20 61 20 72 65 67 69 73 74 65 72 2c 20 6d 61 6b   a register, mak
226b4 69 6e 67 20 61 76 61 69 6c 61 62 6c 65 20 66 6f  ing available fo
226b5 72 20 72 65 75 73 65 20 66 6f 72 20 73 6f 6d 65  r reuse for some
226b6 20 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70 6f 73   other.** purpos
226b7 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 72 65  e..**.** If a re
226b8 67 69 73 74 65 72 20 69 73 20 63 75 72 72 65 6e  gister is curren
226b9 74 6c 79 20 62 65 69 6e 67 20 75 73 65 64 20 62  tly being used b
226ba 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63  y the column cac
226bb 68 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  he, then.** the 
226bc 64 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 64  dallocation is d
226bd 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20 74 68  eferred until th
226be 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 6c  e column cache l
226bf 69 6e 65 20 74 68 61 74 20 75 73 65 73 0a 2a 2a  ine that uses.**
226c0 20 74 68 65 20 72 65 67 69 73 74 65 72 20 62 65   the register be
226c1 63 6f 6d 65 73 20 73 74 61 6c 65 2e 0a 2a 2f 0a  comes stale..*/.
226c2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
226c3 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61  oid sqlite3Relea
226c4 73 65 54 65 6d 70 52 65 67 28 50 61 72 73 65 20  seTempReg(Parse 
226c5 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65  *pParse, int iRe
226c6 67 29 7b 0a 20 20 69 66 28 20 69 52 65 67 20 26  g){.  if( iReg &
226c7 26 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52  & pParse->nTempR
226c8 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70 50 61  eg<ArraySize(pPa
226c9 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29 20 29  rse->aTempReg) )
226ca 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
226cb 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68   struct yColCach
226cc 65 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 69 3d  e *p;.    for(i=
226cd 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f  0, p=pParse->aCo
226ce 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45  lCache; i<SQLITE
226cf 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b  _N_COLCACHE; i++
226d0 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  , p++){.      if
226d1 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20  ( p->iReg==iReg 
226d2 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74 65  ){.        p->te
226d3 6d 70 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 20  mpReg = 1;.     
226d4 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
226d5 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61   }.    }.    pPa
226d6 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50  rse->aTempReg[pP
226d7 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b  arse->nTempReg++
226d8 5d 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a  ] = iReg;.  }.}.
226d9 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
226da 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 20 61 20  or deallocate a 
226db 62 6c 6f 63 6b 20 6f 66 20 6e 52 65 67 20 63 6f  block of nReg co
226dc 6e 73 65 63 75 74 69 76 65 20 72 65 67 69 73 74  nsecutive regist
226dd 65 72 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ers.*/.SQLITE_PR
226de 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
226df 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 50 61  3GetTempRange(Pa
226e0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
226e1 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 2c   nReg){.  int i,
226e2 20 6e 3b 0a 20 20 69 20 3d 20 70 50 61 72 73 65   n;.  i = pParse
226e3 2d 3e 69 52 61 6e 67 65 52 65 67 3b 0a 20 20 6e  ->iRangeReg;.  n
226e4 20 3d 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67   = pParse->nRang
226e5 65 52 65 67 3b 0a 20 20 69 66 28 20 6e 52 65 67  eReg;.  if( nReg
226e6 3c 3d 6e 20 26 26 20 21 75 73 65 64 41 73 43 6f  <=n && !usedAsCo
226e7 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65  lumnCache(pParse
226e8 2c 20 69 2c 20 69 2b 6e 2d 31 29 20 29 7b 0a 20  , i, i+n-1) ){. 
226e9 20 20 20 70 50 61 72 73 65 2d 3e 69 52 61 6e 67     pParse->iRang
226ea 65 52 65 67 20 2b 3d 20 6e 52 65 67 3b 0a 20 20  eReg += nReg;.  
226eb 20 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65    pParse->nRange
226ec 52 65 67 20 2d 3d 20 6e 52 65 67 3b 0a 20 20 7d  Reg -= nReg;.  }
226ed 65 6c 73 65 7b 0a 20 20 20 20 69 20 3d 20 70 50  else{.    i = pP
226ee 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20  arse->nMem+1;.  
226ef 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
226f0 3d 20 6e 52 65 67 3b 0a 20 20 7d 0a 20 20 72 65  = nReg;.  }.  re
226f1 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54 45  turn i;.}.SQLITE
226f2 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
226f3 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
226f4 52 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50 61  Range(Parse *pPa
226f5 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c 20 69  rse, int iReg, i
226f6 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 66 28 20  nt nReg){.  if( 
226f7 6e 52 65 67 3e 70 50 61 72 73 65 2d 3e 6e 52 61  nReg>pParse->nRa
226f8 6e 67 65 52 65 67 20 29 7b 0a 20 20 20 20 70 50  ngeReg ){.    pP
226f9 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20  arse->nRangeReg 
226fa 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70 50 61 72  = nReg;.    pPar
226fb 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 20 3d 20  se->iRangeReg = 
226fc 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a  iReg;.  }.}../**
226fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
226fe 20 6f 66 20 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a   of expr.c *****
226ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22701 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
22702 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
22703 69 6e 20 66 69 6c 65 20 61 6c 74 65 72 2e 63 20  in file alter.c 
22704 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22705 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22706 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
22707 2a 2a 20 32 30 30 35 20 46 65 62 72 75 61 72 79  ** 2005 February
22708 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
22709 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
2270a 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
2270b 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
2270c 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
2270d 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
2270e 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
2270f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
22710 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
22711 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
22712 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
22713 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
22714 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
22715 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
22716 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
22717 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
22718 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
22719 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
2271a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2271b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2271c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2271d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2271e 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
2271f 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20  contains C code 
22720 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 75 73  routines that us
22721 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 56  ed to generate V
22722 44 42 45 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74  DBE code.** that
22723 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
22724 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d  ALTER TABLE comm
22725 61 6e 64 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  and..*/../*.** T
22726 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
22727 66 69 6c 65 20 6f 6e 6c 79 20 65 78 69 73 74 73  file only exists
22728 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74 20 6f   if we are not o
22729 6d 69 74 74 69 6e 67 20 74 68 65 0a 2a 2a 20 41  mitting the.** A
2272a 4c 54 45 52 20 54 41 42 4c 45 20 6c 6f 67 69 63  LTER TABLE logic
2272b 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c 64 2e   from the build.
2272c 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
2272d 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42  TE_OMIT_ALTERTAB
2272e 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  LE.../*.** This 
2272f 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
22730 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61 74 65   by SQL generate
22731 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
22732 68 65 20 0a 2a 2a 20 41 4c 54 45 52 20 54 41 42  he .** ALTER TAB
22733 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20  LE command. The 
22734 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
22735 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 61 20  s the text of a 
22736 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 0a  CREATE TABLE or.
22737 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  ** CREATE INDEX 
22738 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 73 65 63  command. The sec
22739 6f 6e 64 20 69 73 20 61 20 74 61 62 6c 65 20 6e  ond is a table n
2273a 61 6d 65 2e 20 54 68 65 20 74 61 62 6c 65 20 6e  ame. The table n
2273b 61 6d 65 20 69 6e 20 0a 2a 2a 20 74 68 65 20 43  ame in .** the C
2273c 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20 43  REATE TABLE or C
2273d 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74  REATE INDEX stat
2273e 65 6d 65 6e 74 20 69 73 20 72 65 70 6c 61 63 65  ement is replace
2273f 64 20 77 69 74 68 20 74 68 65 20 74 68 69 72 64  d with the third
22740 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e 64  .** argument and
22741 20 74 68 65 20 72 65 73 75 6c 74 20 72 65 74 75   the result retu
22742 72 6e 65 64 2e 20 45 78 61 6d 70 6c 65 73 3a 0a  rned. Examples:.
22743 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e  **.** sqlite_ren
22744 61 6d 65 5f 74 61 62 6c 65 28 27 43 52 45 41 54  ame_table('CREAT
22745 45 20 54 41 42 4c 45 20 61 62 63 28 61 2c 20 62  E TABLE abc(a, b
22746 2c 20 63 29 27 2c 20 27 64 65 66 27 29 0a 2a 2a  , c)', 'def').**
22747 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20       -> 'CREATE 
22748 54 41 42 4c 45 20 64 65 66 28 61 2c 20 62 2c 20  TABLE def(a, b, 
22749 63 29 27 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65  c)'.**.** sqlite
2274a 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43  _rename_table('C
2274b 52 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e  REATE INDEX i ON
2274c 20 61 62 63 28 61 29 27 2c 20 27 64 65 66 27 29   abc(a)', 'def')
2274d 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41  .**     -> 'CREA
2274e 54 45 20 49 4e 44 45 58 20 69 20 4f 4e 20 64 65  TE INDEX i ON de
2274f 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2f 0a 73  f(a, b, c)'.*/.s
22750 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e 61 6d  tatic void renam
22751 65 54 61 62 6c 65 46 75 6e 63 28 0a 20 20 73 71  eTableFunc(.  sq
22752 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
22753 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f  ontext,.  int No
22754 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33  tUsed,.  sqlite3
22755 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
22756 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
22757 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d 20 73   const *zSql = s
22758 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
22759 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 75 6e  t(argv[0]);.  un
2275a 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
2275b 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 3d 20  t *zTableName = 
2275c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2275d 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a 20 20  xt(argv[1]);..  
2275e 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54 6f 6b  int token;.  Tok
2275f 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 75 6e 73 69  en tname;.  unsi
22760 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
22761 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a 20 20  *zCsr = zSql;.  
22762 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 63  int len = 0;.  c
22763 68 61 72 20 2a 7a 52 65 74 3b 0a 0a 20 20 73 71  har *zRet;..  sq
22764 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
22765 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
22766 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
22767 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
22768 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20  TER(NotUsed);.. 
22769 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69 70 6c   /* The principl
2276a 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61 74 65  e used to locate
2276b 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
2276c 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41  in the CREATE TA
2276d 42 4c 45 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d  BLE .  ** statem
2276e 65 6e 74 20 69 73 20 74 68 61 74 20 74 68 65 20  ent is that the 
2276f 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 74 68  table name is th
22770 65 20 66 69 72 73 74 20 6e 6f 6e 2d 73 70 61 63  e first non-spac
22771 65 20 74 6f 6b 65 6e 20 74 68 61 74 0a 20 20 2a  e token that.  *
22772 2a 20 69 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  * is immediately
22773 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 54   followed by a T
22774 4b 5f 4c 50 20 6f 72 20 54 4b 5f 55 53 49 4e 47  K_LP or TK_USING
22775 20 74 6f 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69   token..  */.  i
22776 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64  f( zSql ){.    d
22777 6f 20 7b 0a 20 20 20 20 20 20 69 66 28 20 21 2a  o {.      if( !*
22778 7a 43 73 72 20 29 7b 0a 20 20 20 20 20 20 20 20  zCsr ){.        
22779 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e  /* Ran out of in
2277a 70 75 74 20 62 65 66 6f 72 65 20 66 69 6e 64 69  put before findi
2277b 6e 67 20 61 6e 20 6f 70 65 6e 69 6e 67 20 62 72  ng an opening br
2277c 61 63 6b 65 74 2e 20 52 65 74 75 72 6e 20 4e 55  acket. Return NU
2277d 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  LL. */.        r
2277e 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a  eturn;.      }..
2277f 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74        /* Store t
22780 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43  he token that zC
22781 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20  sr points to in 
22782 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20  tname. */.      
22783 74 6e 61 6d 65 2e 7a 20 3d 20 28 63 68 61 72 2a  tname.z = (char*
22784 29 7a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e 61  )zCsr;.      tna
22785 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20  me.n = len;..   
22786 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43     /* Advance zC
22787 73 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74  sr to the next t
22788 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 74  oken. Store that
22789 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 27   token type in '
2278a 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a  token',.      **
2278b 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 20   and its length 
2278c 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 20  in 'len' (to be 
2278d 75 73 65 64 20 6e 65 78 74 20 69 74 65 72 61 74  used next iterat
2278e 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70  ion of this loop
2278f 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
22790 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a    do {.        z
22791 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20  Csr += len;.    
22792 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65      len = sqlite
22793 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20  3GetToken(zCsr, 
22794 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d  &token);.      }
22795 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54   while( token==T
22796 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 20 20 20  K_SPACE );.     
22797 20 61 73 73 65 72 74 28 20 6c 65 6e 3e 30 20 29   assert( len>0 )
22798 3b 0a 20 20 20 20 7d 20 77 68 69 6c 65 28 20 74  ;.    } while( t
22799 6f 6b 65 6e 21 3d 54 4b 5f 4c 50 20 26 26 20 74  oken!=TK_LP && t
2279a 6f 6b 65 6e 21 3d 54 4b 5f 55 53 49 4e 47 20 29  oken!=TK_USING )
2279b 3b 0a 0a 20 20 20 20 7a 52 65 74 20 3d 20 73 71  ;..    zRet = sq
2279c 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2279d 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25 73 22 2c   "%.*s\"%w\"%s",
2279e 20 28 28 75 38 2a 29 74 6e 61 6d 65 2e 7a 29 20   ((u8*)tname.z) 
2279f 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20  - zSql, zSql, . 
227a0 20 20 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65        zTableName
227a1 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e  , tname.z+tname.
227a2 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  n);.    sqlite3_
227a3 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
227a4 65 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53  ext, zRet, -1, S
227a5 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a  QLITE_DYNAMIC);.
227a6 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
227a7 73 20 43 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  s C function imp
227a8 6c 65 6d 65 6e 74 73 20 61 6e 20 53 51 4c 20 75  lements an SQL u
227a9 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  ser function tha
227aa 74 20 69 73 20 75 73 65 64 20 62 79 20 53 51 4c  t is used by SQL
227ab 20 63 6f 64 65 0a 2a 2a 20 67 65 6e 65 72 61 74   code.** generat
227ac 65 64 20 62 79 20 74 68 65 20 41 4c 54 45 52 20  ed by the ALTER 
227ad 54 41 42 4c 45 20 2e 2e 2e 20 52 45 4e 41 4d 45  TABLE ... RENAME
227ae 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 6d 6f 64 69   command to modi
227af 66 79 20 74 68 65 20 64 65 66 69 6e 69 74 69 6f  fy the definitio
227b0 6e 0a 2a 2a 20 6f 66 20 61 6e 79 20 66 6f 72 65  n.** of any fore
227b1 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
227b2 6e 74 73 20 74 68 61 74 20 75 73 65 20 74 68 65  nts that use the
227b3 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72 65 6e   table being ren
227b4 61 6d 65 64 20 61 73 20 74 68 65 20 0a 2a 2a 20  amed as the .** 
227b5 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 49 74  parent table. It
227b6 20 69 73 20 70 61 73 73 65 64 20 74 68 72 65 65   is passed three
227b7 20 61 72 67 75 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a   arguments:.**.*
227b8 2a 20 20 20 31 29 20 54 68 65 20 63 6f 6d 70 6c  *   1) The compl
227b9 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  ete text of the 
227ba 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
227bb 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 6d 6f 64  tement being mod
227bc 69 66 69 65 64 2c 0a 2a 2a 20 20 20 32 29 20 54  ified,.**   2) T
227bd 68 65 20 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74  he old name of t
227be 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72  he table being r
227bf 65 6e 61 6d 65 64 2c 20 61 6e 64 0a 2a 2a 20 20  enamed, and.**  
227c0 20 33 29 20 54 68 65 20 6e 65 77 20 6e 61 6d 65   3) The new name
227c1 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   of the table be
227c2 69 6e 67 20 72 65 6e 61 6d 65 64 2e 0a 2a 2a 0a  ing renamed..**.
227c3 2a 2a 20 49 74 20 72 65 74 75 72 6e 73 20 74 68  ** It returns th
227c4 65 20 6e 65 77 20 43 52 45 41 54 45 20 54 41 42  e new CREATE TAB
227c5 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 46 6f  LE statement. Fo
227c6 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  r example:.**.**
227c7 20 20 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65     sqlite_rename
227c8 5f 70 61 72 65 6e 74 28 27 43 52 45 41 54 45 20  _parent('CREATE 
227c9 54 41 42 4c 45 20 74 31 28 61 20 52 45 46 45 52  TABLE t1(a REFER
227ca 45 4e 43 45 53 20 74 32 29 27 2c 20 27 74 32 27  ENCES t2)', 't2'
227cb 2c 20 27 74 33 27 29 0a 2a 2a 20 20 20 20 20 20  , 't3').**      
227cc 20 2d 3e 20 27 43 52 45 41 54 45 20 54 41 42 4c   -> 'CREATE TABL
227cd 45 20 74 31 28 61 20 52 45 46 45 52 45 4e 43 45  E t1(a REFERENCE
227ce 53 20 74 33 29 27 0a 2a 2f 0a 23 69 66 6e 64 65  S t3)'.*/.#ifnde
227cf 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
227d0 52 45 49 47 4e 5f 4b 45 59 0a 73 74 61 74 69 63  REIGN_KEY.static
227d1 20 76 6f 69 64 20 72 65 6e 61 6d 65 50 61 72 65   void renamePare
227d2 6e 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  ntFunc(.  sqlite
227d3 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
227d4 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
227d5 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
227d6 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 73  ue **argv.){.  s
227d7 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
227d8 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
227d9 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
227da 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 70 75 74  .  char *zOutput
227db 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52   = 0;.  char *zR
227dc 65 73 75 6c 74 3b 0a 20 20 75 6e 73 69 67 6e 65  esult;.  unsigne
227dd 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 49  d char const *zI
227de 6e 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76  nput = sqlite3_v
227df 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
227e0 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  ]);.  unsigned c
227e1 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 6c 64 20  har const *zOld 
227e2 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
227e3 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
227e4 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
227e5 6f 6e 73 74 20 2a 7a 4e 65 77 20 3d 20 73 71 6c  onst *zNew = sql
227e6 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
227e7 61 72 67 76 5b 32 5d 29 3b 0a 0a 20 20 75 6e 73  argv[2]);..  uns
227e8 69 67 6e 65 64 20 63 6f 6e 73 74 20 63 68 61 72  igned const char
227e9 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   *z;         /* 
227ea 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b 65 6e  Pointer to token
227eb 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20   */.  int n;    
227ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
227ed 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
227ee 6f 66 20 74 6f 6b 65 6e 20 7a 20 2a 2f 0a 20 20  of token z */.  
227ef 69 6e 74 20 74 6f 6b 65 6e 3b 20 20 20 20 20 20  int token;      
227f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
227f1 2f 2a 20 54 79 70 65 20 6f 66 20 74 6f 6b 65 6e  /* Type of token
227f2 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41   */..  UNUSED_PA
227f3 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
227f4 3b 0a 20 20 66 6f 72 28 7a 3d 7a 49 6e 70 75 74  ;.  for(z=zInput
227f5 3b 20 2a 7a 3b 20 7a 3d 7a 2b 6e 29 7b 0a 20 20  ; *z; z=z+n){.  
227f6 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74    n = sqlite3Get
227f7 54 6f 6b 65 6e 28 7a 2c 20 26 74 6f 6b 65 6e 29  Token(z, &token)
227f8 3b 0a 20 20 20 20 69 66 28 20 74 6f 6b 65 6e 3d  ;.    if( token=
227f9 3d 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 20 29  =TK_REFERENCES )
227fa 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 50  {.      char *zP
227fb 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 64 6f 20  arent;.      do 
227fc 7b 0a 20 20 20 20 20 20 20 20 7a 20 2b 3d 20 6e  {.        z += n
227fd 3b 0a 20 20 20 20 20 20 20 20 6e 20 3d 20 73 71  ;.        n = sq
227fe 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 2c  lite3GetToken(z,
227ff 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20   &token);.      
22800 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54  }while( token==T
22801 4b 5f 53 50 41 43 45 20 29 3b 0a 0a 20 20 20 20  K_SPACE );..    
22802 20 20 7a 50 61 72 65 6e 74 20 3d 20 73 71 6c 69    zParent = sqli
22803 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
22804 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a   (const char *)z
22805 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , n);.      if( 
22806 7a 50 61 72 65 6e 74 3d 3d 30 20 29 20 62 72 65  zParent==0 ) bre
22807 61 6b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ak;.      sqlite
22808 33 44 65 71 75 6f 74 65 28 7a 50 61 72 65 6e 74  3Dequote(zParent
22809 29 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d  );.      if( 0==
2280a 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 28  sqlite3StrICmp((
2280b 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a 4f 6c  const char *)zOl
2280c 64 2c 20 7a 50 61 72 65 6e 74 29 20 29 7b 0a 20  d, zParent) ){. 
2280d 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 75         char *zOu
2280e 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
2280f 74 66 28 64 62 2c 20 22 25 73 25 2e 2a 73 5c 22  tf(db, "%s%.*s\"
22810 25 77 5c 22 22 2c 20 0a 20 20 20 20 20 20 20 20  %w\"", .        
22811 20 20 20 20 28 7a 4f 75 74 70 75 74 3f 7a 4f 75      (zOutput?zOu
22812 74 70 75 74 3a 22 22 29 2c 20 7a 2d 7a 49 6e 70  tput:""), z-zInp
22813 75 74 2c 20 7a 49 6e 70 75 74 2c 20 28 63 6f 6e  ut, zInput, (con
22814 73 74 20 63 68 61 72 20 2a 29 7a 4e 65 77 0a 20  st char *)zNew. 
22815 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
22816 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
22817 64 62 2c 20 7a 4f 75 74 70 75 74 29 3b 0a 20 20  db, zOutput);.  
22818 20 20 20 20 20 20 7a 4f 75 74 70 75 74 20 3d 20        zOutput = 
22819 7a 4f 75 74 3b 0a 20 20 20 20 20 20 20 20 7a 49  zOut;.        zI
2281a 6e 70 75 74 20 3d 20 26 7a 5b 6e 5d 3b 0a 20 20  nput = &z[n];.  
2281b 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2281c 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 50  te3DbFree(db, zP
2281d 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20  arent);.    }.  
2281e 7d 0a 0a 20 20 7a 52 65 73 75 6c 74 20 3d 20 73  }..  zResult = s
2281f 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
22820 2c 20 22 25 73 25 73 22 2c 20 28 7a 4f 75 74 70  , "%s%s", (zOutp
22821 75 74 3f 7a 4f 75 74 70 75 74 3a 22 22 29 2c 20  ut?zOutput:""), 
22822 7a 49 6e 70 75 74 29 2c 20 0a 20 20 73 71 6c 69  zInput), .  sqli
22823 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
22824 63 6f 6e 74 65 78 74 2c 20 7a 52 65 73 75 6c 74  context, zResult
22825 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44 59 4e  , -1, SQLITE_DYN
22826 41 4d 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33  AMIC);.  sqlite3
22827 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 75 74 70  DbFree(db, zOutp
22828 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23  ut);.}.#endif..#
22829 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2282a 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a 20 54 68  IT_TRIGGER./* Th
2282b 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
2282c 73 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72  sed by SQL gener
2282d 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ated to implemen
2282e 74 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54  t the.** ALTER T
2282f 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68  ABLE command. Th
22830 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
22831 20 69 73 20 74 68 65 20 74 65 78 74 20 6f 66 20   is the text of 
22832 61 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52  a CREATE TRIGGER
22833 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20   .** statement. 
22834 54 68 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20  The second is a 
22835 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68 65 20  table name. The 
22836 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68  table name in th
22837 65 20 43 52 45 41 54 45 20 0a 2a 2a 20 54 52 49  e CREATE .** TRI
22838 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 20 69  GGER statement i
22839 73 20 72 65 70 6c 61 63 65 64 20 77 69 74 68 20  s replaced with 
2283a 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  the third argume
2283b 6e 74 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c  nt and the resul
2283c 74 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20  t .** returned. 
2283d 54 68 69 73 20 69 73 20 61 6e 61 6c 61 67 6f 75  This is analagou
2283e 73 20 74 6f 20 72 65 6e 61 6d 65 54 61 62 6c 65  s to renameTable
2283f 46 75 6e 63 28 29 20 61 62 6f 76 65 2c 20 65 78  Func() above, ex
22840 63 65 70 74 20 66 6f 72 20 43 52 45 41 54 45 0a  cept for CREATE.
22841 2a 2a 20 54 52 49 47 47 45 52 2c 20 6e 6f 74 20  ** TRIGGER, not 
22842 43 52 45 41 54 45 20 49 4e 44 45 58 20 61 6e 64  CREATE INDEX and
22843 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e 0a 2a   CREATE TABLE..*
22844 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
22845 6e 61 6d 65 54 72 69 67 67 65 72 46 75 6e 63 28  nameTriggerFunc(
22846 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
22847 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
22848 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
22849 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
2284a 67 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  gv.){.  unsigned
2284b 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71   char const *zSq
2284c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
2284d 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
2284e 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
2284f 20 63 6f 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61   const *zTableNa
22850 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  me = sqlite3_val
22851 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
22852 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a  ;..  int token;.
22853 20 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20    Token tname;. 
22854 20 69 6e 74 20 64 69 73 74 20 3d 20 33 3b 0a 20   int dist = 3;. 
22855 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63   unsigned char c
22856 6f 6e 73 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71  onst *zCsr = zSq
22857 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30  l;.  int len = 0
22858 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a  ;.  char *zRet;.
22859 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2285a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
2285b 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
2285c 74 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41  t);..  UNUSED_PA
2285d 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
2285e 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e  ;..  /* The prin
2285f 63 69 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f  ciple used to lo
22860 63 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e  cate the table n
22861 61 6d 65 20 69 6e 20 74 68 65 20 43 52 45 41 54  ame in the CREAT
22862 45 20 54 52 49 47 47 45 52 20 0a 20 20 2a 2a 20  E TRIGGER .  ** 
22863 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 61  statement is tha
22864 74 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65  t the table name
22865 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74 6f   is the first to
22866 6b 65 6e 20 74 68 61 74 20 69 73 20 69 6d 6d 65  ken that is imme
22867 64 69 61 74 65 64 6c 79 0a 20 20 2a 2a 20 70 72  diatedly.  ** pr
22868 65 63 65 64 65 64 20 62 79 20 65 69 74 68 65 72  eceded by either
22869 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b 5f 44 4f 54   TK_ON or TK_DOT
2286a 20 61 6e 64 20 69 6d 6d 65 64 69 61 74 65 64 6c   and immediatedl
2286b 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6f 6e  y followed by on
2286c 65 0a 20 20 2a 2a 20 6f 66 20 54 4b 5f 57 48 45  e.  ** of TK_WHE
2286d 4e 2c 20 54 4b 5f 42 45 47 49 4e 20 6f 72 20 54  N, TK_BEGIN or T
2286e 4b 5f 46 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66  K_FOR..  */.  if
2286f 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 64 6f  ( zSql ){.    do
22870 20 7b 0a 0a 20 20 20 20 20 20 69 66 28 20 21 2a   {..      if( !*
22871 7a 43 73 72 20 29 7b 0a 20 20 20 20 20 20 20 20  zCsr ){.        
22872 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20 69 6e  /* Ran out of in
22873 70 75 74 20 62 65 66 6f 72 65 20 66 69 6e 64 69  put before findi
22874 6e 67 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  ng the table nam
22875 65 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20  e. Return NULL. 
22876 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
22877 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  n;.      }..    
22878 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20 74    /* Store the t
22879 6f 6b 65 6e 20 74 68 61 74 20 7a 43 73 72 20 70  oken that zCsr p
2287a 6f 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d  oints to in tnam
2287b 65 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d  e. */.      tnam
2287c 65 2e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 43 73  e.z = (char*)zCs
2287d 72 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 6e  r;.      tname.n
2287e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20 20 2f   = len;..      /
2287f 2a 20 41 64 76 61 6e 63 65 20 7a 43 73 72 20 74  * Advance zCsr t
22880 6f 20 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e  o the next token
22881 2e 20 53 74 6f 72 65 20 74 68 61 74 20 74 6f 6b  . Store that tok
22882 65 6e 20 74 79 70 65 20 69 6e 20 27 74 6f 6b 65  en type in 'toke
22883 6e 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64  n',.      ** and
22884 20 69 74 73 20 6c 65 6e 67 74 68 20 69 6e 20 27   its length in '
22885 6c 65 6e 27 20 28 74 6f 20 62 65 20 75 73 65 64  len' (to be used
22886 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20   next iteration 
22887 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e 0a 20  of this loop).. 
22888 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 64 6f       */.      do
22889 20 7b 0a 20 20 20 20 20 20 20 20 7a 43 73 72 20   {.        zCsr 
2288a 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20  += len;.        
2288b 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74  len = sqlite3Get
2288c 54 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74 6f 6b  Token(zCsr, &tok
2288d 65 6e 29 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c  en);.      }whil
2288e 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41  e( token==TK_SPA
2288f 43 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  CE );.      asse
22890 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20  rt( len>0 );..  
22891 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65 20      /* Variable 
22892 27 64 69 73 74 27 20 73 74 6f 72 65 73 20 74 68  'dist' stores th
22893 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  e number of toke
22894 6e 73 20 72 65 61 64 20 73 69 6e 63 65 20 74 68  ns read since th
22895 65 20 6d 6f 73 74 0a 20 20 20 20 20 20 2a 2a 20  e most.      ** 
22896 72 65 63 65 6e 74 20 54 4b 5f 44 4f 54 20 6f 72  recent TK_DOT or
22897 20 54 4b 5f 4f 4e 2e 20 54 68 69 73 20 6d 65 61   TK_ON. This mea
22898 6e 73 20 74 68 61 74 20 77 68 65 6e 20 61 20 57  ns that when a W
22899 48 45 4e 2c 20 46 4f 52 20 6f 72 20 42 45 47 49  HEN, FOR or BEGI
2289a 4e 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65  N .      ** toke
2289b 6e 20 69 73 20 72 65 61 64 20 61 6e 64 20 27 64  n is read and 'd
2289c 69 73 74 27 20 65 71 75 61 6c 73 20 32 2c 20 74  ist' equals 2, t
2289d 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 74 61  he condition sta
2289e 74 65 64 20 61 62 6f 76 65 0a 20 20 20 20 20 20  ted above.      
2289f 2a 2a 20 74 6f 20 62 65 20 6d 65 74 2e 0a 20 20  ** to be met..  
228a0 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
228a1 4e 6f 74 65 20 74 68 61 74 20 4f 4e 20 63 61 6e  Note that ON can
228a2 6e 6f 74 20 62 65 20 61 20 64 61 74 61 62 61 73  not be a databas
228a3 65 2c 20 74 61 62 6c 65 20 6f 72 20 63 6f 6c 75  e, table or colu
228a4 6d 6e 20 6e 61 6d 65 2c 20 73 6f 0a 20 20 20 20  mn name, so.    
228a5 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f    ** there is no
228a6 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79 20 61   need to worry a
228a7 62 6f 75 74 20 73 79 6e 74 61 78 20 6c 69 6b 65  bout syntax like
228a8 20 0a 20 20 20 20 20 20 2a 2a 20 22 43 52 45 41   .      ** "CREA
228a9 54 45 20 54 52 49 47 47 45 52 20 2e 2e 2e 20 4f  TE TRIGGER ... O
228aa 4e 20 4f 4e 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e  N ON.ON BEGIN ..
228ab 2e 22 20 65 74 63 2e 0a 20 20 20 20 20 20 2a 2f  ." etc..      */
228ac 0a 20 20 20 20 20 20 64 69 73 74 2b 2b 3b 0a 20  .      dist++;. 
228ad 20 20 20 20 20 69 66 28 20 74 6f 6b 65 6e 3d 3d       if( token==
228ae 54 4b 5f 44 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d  TK_DOT || token=
228af 3d 54 4b 5f 4f 4e 20 29 7b 0a 20 20 20 20 20 20  =TK_ON ){.      
228b0 20 20 64 69 73 74 20 3d 20 30 3b 0a 20 20 20 20    dist = 0;.    
228b1 20 20 7d 0a 20 20 20 20 7d 20 77 68 69 6c 65 28    }.    } while(
228b2 20 64 69 73 74 21 3d 32 20 7c 7c 20 28 74 6f 6b   dist!=2 || (tok
228b3 65 6e 21 3d 54 4b 5f 57 48 45 4e 20 26 26 20 74  en!=TK_WHEN && t
228b4 6f 6b 65 6e 21 3d 54 4b 5f 46 4f 52 20 26 26 20  oken!=TK_FOR && 
228b5 74 6f 6b 65 6e 21 3d 54 4b 5f 42 45 47 49 4e 29  token!=TK_BEGIN)
228b6 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 56 61 72 69   );..    /* Vari
228b7 61 62 6c 65 20 74 6e 61 6d 65 20 6e 6f 77 20 63  able tname now c
228b8 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 6f 6b 65  ontains the toke
228b9 6e 20 74 68 61 74 20 69 73 20 74 68 65 20 6f 6c  n that is the ol
228ba 64 20 74 61 62 6c 65 2d 6e 61 6d 65 0a 20 20 20  d table-name.   
228bb 20 2a 2a 20 69 6e 20 74 68 65 20 43 52 45 41 54   ** in the CREAT
228bc 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d  E TRIGGER statem
228bd 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ent..    */.    
228be 7a 52 65 74 20 3d 20 73 71 6c 69 74 65 33 4d 50  zRet = sqlite3MP
228bf 72 69 6e 74 66 28 64 62 2c 20 22 25 2e 2a 73 5c  rintf(db, "%.*s\
228c0 22 25 77 5c 22 25 73 22 2c 20 28 28 75 38 2a 29  "%w\"%s", ((u8*)
228c1 74 6e 61 6d 65 2e 7a 29 20 2d 20 7a 53 71 6c 2c  tname.z) - zSql,
228c2 20 7a 53 71 6c 2c 20 0a 20 20 20 20 20 20 20 7a   zSql, .       z
228c3 54 61 62 6c 65 4e 61 6d 65 2c 20 74 6e 61 6d 65  TableName, tname
228c4 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20 20 20  .z+tname.n);.   
228c5 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
228c6 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52  text(context, zR
228c7 65 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44  et, -1, SQLITE_D
228c8 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23  YNAMIC);.  }.}.#
228c9 65 6e 64 69 66 20 20 20 2f 2a 20 21 53 51 4c 49  endif   /* !SQLI
228ca 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20  TE_OMIT_TRIGGER 
228cb 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  */../*.** Regist
228cc 65 72 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63  er built-in func
228cd 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 68 65  tions used to he
228ce 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 41 4c 54  lp implement ALT
228cf 45 52 20 54 41 42 4c 45 0a 2a 2f 0a 53 51 4c 49  ER TABLE.*/.SQLI
228d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
228d1 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63  sqlite3AlterFunc
228d2 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64  tions(sqlite3 *d
228d3 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65  b){.  sqlite3Cre
228d4 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c  ateFunc(db, "sql
228d5 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  ite_rename_table
228d6 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46  ", 2, SQLITE_UTF
228d7 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  8, 0,.          
228d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
228d9 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63 2c 20  enameTableFunc, 
228da 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53  0, 0);.#ifndef S
228db 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
228dc 45 52 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61  ER.  sqlite3Crea
228dd 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69  teFunc(db, "sqli
228de 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67 65  te_rename_trigge
228df 72 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55 54  r", 2, SQLITE_UT
228e0 46 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  F8, 0,.         
228e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
228e2 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46 75 6e  renameTriggerFun
228e3 63 2c 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66  c, 0, 0);.#endif
228e4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
228e5 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
228e6 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  .  sqlite3Create
228e7 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69 74 65  Func(db, "sqlite
228e8 5f 72 65 6e 61 6d 65 5f 70 61 72 65 6e 74 22 2c  _rename_parent",
228e9 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   3, SQLITE_UTF8,
228ea 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
228eb 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6e               ren
228ec 61 6d 65 50 61 72 65 6e 74 46 75 6e 63 2c 20 30  ameParentFunc, 0
228ed 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  , 0);.#endif.}..
228ee 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
228ef 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 63  ion is used to c
228f0 72 65 61 74 65 20 74 68 65 20 74 65 78 74 20 6f  reate the text o
228f1 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66  f expressions of
228f2 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a   the form:.**.**
228f3 20 20 20 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e     name=<constan
228f4 74 31 3e 20 4f 52 20 6e 61 6d 65 3d 3c 63 6f 6e  t1> OR name=<con
228f5 73 74 61 6e 74 32 3e 20 4f 52 20 2e 2e 2e 0a 2a  stant2> OR ....*
228f6 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74  *.** If argument
228f7 20 7a 57 68 65 72 65 20 69 73 20 4e 55 4c 4c 2c   zWhere is NULL,
228f8 20 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20   then a pointer 
228f9 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e  string containin
228fa 67 20 74 68 65 20 74 65 78 74 20 0a 2a 2a 20 22  g the text .** "
228fb 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e 74 3e 22  name=<constant>"
228fc 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 77 68   is returned, wh
228fd 65 72 65 20 3c 63 6f 6e 73 74 61 6e 74 3e 20 69  ere <constant> i
228fe 73 20 74 68 65 20 71 75 6f 74 65 64 20 76 65 72  s the quoted ver
228ff 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 73  sion.** of the s
22900 74 72 69 6e 67 20 70 61 73 73 65 64 20 61 73 20  tring passed as 
22901 61 72 67 75 6d 65 6e 74 20 7a 43 6f 6e 73 74 61  argument zConsta
22902 6e 74 2e 20 54 68 65 20 72 65 74 75 72 6e 65 64  nt. The returned
22903 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 61 6c   buffer is.** al
22904 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 73 71  located using sq
22905 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2e  lite3DbMalloc().
22906 20 49 74 20 69 73 20 74 68 65 20 72 65 73 70 6f   It is the respo
22907 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65  nsibility of the
22908 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20 65 6e  .** caller to en
22909 73 75 72 65 20 74 68 61 74 20 69 74 20 69 73 20  sure that it is 
2290a 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64  eventually freed
2290b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d  ..**.** If argum
2290c 65 6e 74 20 7a 57 68 65 72 65 20 69 73 20 6e 6f  ent zWhere is no
2290d 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65  t NULL, then the
2290e 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64   string returned
2290f 20 69 73 20 0a 2a 2a 20 22 3c 77 68 65 72 65 3e   is .** "<where>
22910 20 4f 52 20 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61   OR name=<consta
22911 6e 74 3e 22 2c 20 77 68 65 72 65 20 3c 77 68 65  nt>", where <whe
22912 72 65 3e 20 69 73 20 74 68 65 20 63 6f 6e 74 65  re> is the conte
22913 6e 74 73 20 6f 66 20 7a 57 68 65 72 65 2e 0a 2a  nts of zWhere..*
22914 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 7a  * In this case z
22915 57 68 65 72 65 20 69 73 20 70 61 73 73 65 64 20  Where is passed 
22916 74 6f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  to sqlite3DbFree
22917 28 29 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  () before return
22918 69 6e 67 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74  ing..** .*/.stat
22919 69 63 20 63 68 61 72 20 2a 77 68 65 72 65 4f 72  ic char *whereOr
2291a 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 64 62  Name(sqlite3 *db
2291b 2c 20 63 68 61 72 20 2a 7a 57 68 65 72 65 2c 20  , char *zWhere, 
2291c 63 68 61 72 20 2a 7a 43 6f 6e 73 74 61 6e 74 29  char *zConstant)
2291d 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a  {.  char *zNew;.
2291e 20 20 69 66 28 20 21 7a 57 68 65 72 65 20 29 7b    if( !zWhere ){
2291f 0a 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69  .    zNew = sqli
22920 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
22921 6e 61 6d 65 3d 25 51 22 2c 20 7a 43 6f 6e 73 74  name=%Q", zConst
22922 61 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ant);.  }else{. 
22923 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65     zNew = sqlite
22924 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
22925 20 4f 52 20 6e 61 6d 65 3d 25 51 22 2c 20 7a 57   OR name=%Q", zW
22926 68 65 72 65 2c 20 7a 43 6f 6e 73 74 61 6e 74 29  here, zConstant)
22927 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
22928 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65 29 3b  ree(db, zWhere);
22929 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e  .  }.  return zN
2292a 65 77 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69  ew;.}..#if !defi
2292b 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2292c 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20  FOREIGN_KEY) && 
2292d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2292e 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 2f 2a  OMIT_TRIGGER)./*
2292f 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65  .** Generate the
22930 20 74 65 78 74 20 6f 66 20 61 20 57 48 45 52 45   text of a WHERE
22931 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63   expression whic
22932 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  h can be used to
22933 20 73 65 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74   select all.** t
22934 61 62 6c 65 73 20 74 68 61 74 20 68 61 76 65 20  ables that have 
22935 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
22936 74 72 61 69 6e 74 73 20 74 68 61 74 20 72 65 66  traints that ref
22937 65 72 20 74 6f 20 74 61 62 6c 65 20 70 54 61 62  er to table pTab
22938 20 28 69 2e 65 2e 0a 2a 2a 20 63 6f 6e 73 74 72   (i.e..** constr
22939 61 69 6e 74 73 20 66 6f 72 20 77 68 69 63 68 20  aints for which 
2293a 70 54 61 62 20 69 73 20 74 68 65 20 70 61 72 65  pTab is the pare
2293b 6e 74 20 74 61 62 6c 65 29 20 66 72 6f 6d 20 74  nt table) from t
2293c 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
2293d 0a 2a 2a 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  .** table..*/.st
2293e 61 74 69 63 20 63 68 61 72 20 2a 77 68 65 72 65  atic char *where
2293f 46 6f 72 65 69 67 6e 4b 65 79 73 28 50 61 72 73  ForeignKeys(Pars
22940 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65  e *pParse, Table
22941 20 2a 70 54 61 62 29 7b 0a 20 20 46 4b 65 79 20   *pTab){.  FKey 
22942 2a 70 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 65  *p;.  char *zWhe
22943 72 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d  re = 0;.  for(p=
22944 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e  sqlite3FkReferen
22945 63 65 73 28 70 54 61 62 29 3b 20 70 3b 20 70 3d  ces(pTab); p; p=
22946 70 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20  p->pNextTo){.   
22947 20 7a 57 68 65 72 65 20 3d 20 77 68 65 72 65 4f   zWhere = whereO
22948 72 4e 61 6d 65 28 70 50 61 72 73 65 2d 3e 64 62  rName(pParse->db
22949 2c 20 7a 57 68 65 72 65 2c 20 70 2d 3e 70 46 72  , zWhere, p->pFr
2294a 6f 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a  om->zName);.  }.
2294b 20 20 72 65 74 75 72 6e 20 7a 57 68 65 72 65 3b    return zWhere;
2294c 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
2294d 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65   Generate the te
2294e 78 74 20 6f 66 20 61 20 57 48 45 52 45 20 65 78  xt of a WHERE ex
2294f 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63  pression which c
22950 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73 65  an be used to se
22951 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70  lect all.** temp
22952 6f 72 61 72 79 20 74 72 69 67 67 65 72 73 20 6f  orary triggers o
22953 6e 20 74 61 62 6c 65 20 70 54 61 62 20 66 72 6f  n table pTab fro
22954 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d  m the sqlite_tem
22955 70 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20  p_master table. 
22956 49 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62  If.** table pTab
22957 20 68 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72   has no temporar
22958 79 20 74 72 69 67 67 65 72 73 2c 20 6f 72 20 69  y triggers, or i
22959 73 20 69 74 73 65 6c 66 20 73 74 6f 72 65 64 20  s itself stored 
2295a 69 6e 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f  in the .** tempo
2295b 72 61 72 79 20 64 61 74 61 62 61 73 65 2c 20 4e  rary database, N
2295c 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
2295d 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
2295e 2a 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65  *whereTempTrigge
2295f 72 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  rs(Parse *pParse
22960 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
22961 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67    Trigger *pTrig
22962 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65  ;.  char *zWhere
22963 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53 63   = 0;.  const Sc
22964 68 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65 6d  hema *pTempSchem
22965 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  a = pParse->db->
22966 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20  aDb[1].pSchema; 
22967 2f 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65 6d  /* Temp db schem
22968 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68  a */..  /* If th
22969 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 6c  e table is not l
2296a 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20 74 65  ocated in the te
2296b 6d 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68 20  mp-db (in which 
2296c 63 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20 20  case NULL is .  
2296d 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f 6f  ** returned, loo
2296e 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61  p through the ta
2296f 62 6c 65 73 20 6c 69 73 74 20 6f 66 20 74 72 69  bles list of tri
22970 67 67 65 72 73 2e 20 46 6f 72 20 65 61 63 68 20  ggers. For each 
22971 74 72 69 67 67 65 72 0a 20 20 2a 2a 20 74 68 61  trigger.  ** tha
22972 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
22973 20 74 68 65 20 74 65 6d 70 2d 64 62 20 73 63 68   the temp-db sch
22974 65 6d 61 2c 20 61 64 64 20 61 20 63 6c 61 75 73  ema, add a claus
22975 65 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 0a  e to the WHERE .
22976 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
22977 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 69  being built up i
22978 6e 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a 20  n zWhere..  */. 
22979 20 69 66 28 20 70 54 61 62 2d 3e 70 53 63 68 65   if( pTab->pSche
2297a 6d 61 21 3d 70 54 65 6d 70 53 63 68 65 6d 61 20  ma!=pTempSchema 
2297b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
2297c 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2297d 0a 20 20 20 20 66 6f 72 28 70 54 72 69 67 3d 73  .    for(pTrig=s
2297e 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73  qlite3TriggerLis
2297f 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  t(pParse, pTab);
22980 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54   pTrig; pTrig=pT
22981 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  rig->pNext){.   
22982 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70 53     if( pTrig->pS
22983 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68 65  chema==pTempSche
22984 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 57  ma ){.        zW
22985 68 65 72 65 20 3d 20 77 68 65 72 65 4f 72 4e 61  here = whereOrNa
22986 6d 65 28 64 62 2c 20 7a 57 68 65 72 65 2c 20 70  me(db, zWhere, p
22987 54 72 69 67 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Trig->zName);.  
22988 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
22989 20 20 72 65 74 75 72 6e 20 7a 57 68 65 72 65 3b    return zWhere;
2298a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
2298b 74 65 20 63 6f 64 65 20 74 6f 20 64 72 6f 70 20  te code to drop 
2298c 61 6e 64 20 72 65 6c 6f 61 64 20 74 68 65 20 69  and reload the i
2298d 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e  nternal represen
2298e 74 61 74 69 6f 6e 20 6f 66 20 74 61 62 6c 65 0a  tation of table.
2298f 2a 2a 20 70 54 61 62 20 66 72 6f 6d 20 74 68 65  ** pTab from the
22990 20 64 61 74 61 62 61 73 65 2c 20 69 6e 63 6c 75   database, inclu
22991 64 69 6e 67 20 74 72 69 67 67 65 72 73 20 61 6e  ding triggers an
22992 64 20 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67  d temporary trig
22993 67 65 72 73 2e 0a 2a 2a 20 41 72 67 75 6d 65 6e  gers..** Argumen
22994 74 20 7a 4e 61 6d 65 20 69 73 20 74 68 65 20 6e  t zName is the n
22995 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
22996 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
22997 20 73 63 68 65 6d 61 20 61 74 0a 2a 2a 20 74 68   schema at.** th
22998 65 20 74 69 6d 65 20 74 68 65 20 67 65 6e 65 72  e time the gener
22999 61 74 65 64 20 63 6f 64 65 20 69 73 20 65 78 65  ated code is exe
2299a 63 75 74 65 64 2e 20 54 68 69 73 20 63 61 6e 20  cuted. This can 
2299b 62 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f  be different fro
2299c 6d 0a 2a 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  m.** pTab->zName
2299d 20 69 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   if this functio
2299e 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65  n is being calle
2299f 64 20 74 6f 20 63 6f 64 65 20 70 61 72 74 20 6f  d to code part o
229a0 66 20 61 6e 20 0a 2a 2a 20 22 41 4c 54 45 52 20  f an .** "ALTER 
229a1 54 41 42 4c 45 20 52 45 4e 41 4d 45 20 54 4f 22  TABLE RENAME TO"
229a2 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73   statement..*/.s
229a3 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c 6f 61  tatic void reloa
229a4 64 54 61 62 6c 65 53 63 68 65 6d 61 28 50 61 72  dTableSchema(Par
229a5 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
229a6 65 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74 20 63  e *pTab, const c
229a7 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56  har *zName){.  V
229a8 64 62 65 20 2a 76 3b 0a 20 20 63 68 61 72 20 2a  dbe *v;.  char *
229a9 7a 57 68 65 72 65 3b 0a 20 20 69 6e 74 20 69 44  zWhere;.  int iD
229aa 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
229ab 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
229ac 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
229ad 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 23 69 66  ning pTab */.#if
229ae 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
229af 5f 54 52 49 47 47 45 52 0a 20 20 54 72 69 67 67  _TRIGGER.  Trigg
229b0 65 72 20 2a 70 54 72 69 67 3b 0a 23 65 6e 64 69  er *pTrig;.#endi
229b1 66 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  f..  v = sqlite3
229b2 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
229b3 0a 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d 3d  .  if( NEVER(v==
229b4 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  0) ) return;.  a
229b5 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
229b6 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
229b7 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29  es(pParse->db) )
229b8 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
229b9 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
229ba 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
229bb 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73  >pSchema);.  ass
229bc 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a  ert( iDb>=0 );..
229bd 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
229be 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a  MIT_TRIGGER.  /*
229bf 20 44 72 6f 70 20 61 6e 79 20 74 61 62 6c 65 20   Drop any table 
229c0 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20 74 68  triggers from th
229c1 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  e internal schem
229c2 61 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 54 72 69  a. */.  for(pTri
229c3 67 3d 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  g=sqlite3Trigger
229c4 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  List(pParse, pTa
229c5 62 29 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67  b); pTrig; pTrig
229c6 3d 70 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a  =pTrig->pNext){.
229c7 20 20 20 20 69 6e 74 20 69 54 72 69 67 44 62 20      int iTrigDb 
229c8 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
229c9 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
229ca 62 2c 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d  b, pTrig->pSchem
229cb 61 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  a);.    assert( 
229cc 69 54 72 69 67 44 62 3d 3d 69 44 62 20 7c 7c 20  iTrigDb==iDb || 
229cd 69 54 72 69 67 44 62 3d 3d 31 20 29 3b 0a 20 20  iTrigDb==1 );.  
229ce 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
229cf 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72  Op4(v, OP_DropTr
229d0 69 67 67 65 72 2c 20 69 54 72 69 67 44 62 2c 20  igger, iTrigDb, 
229d1 30 2c 20 30 2c 20 70 54 72 69 67 2d 3e 7a 4e 61  0, 0, pTrig->zNa
229d2 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e 64  me, 0);.  }.#end
229d3 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20 74 68  if..  /* Drop th
229d4 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65  e table and inde
229d5 78 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72  x from the inter
229d6 6e 61 6c 20 73 63 68 65 6d 61 2e 20 20 2a 2f 0a  nal schema.  */.
229d7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
229d8 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61  Op4(v, OP_DropTa
229d9 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20  ble, iDb, 0, 0, 
229da 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  pTab->zName, 0);
229db 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68  ..  /* Reload th
229dc 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 20 61  e table, index a
229dd 6e 64 20 70 65 72 6d 61 6e 65 6e 74 20 74 72 69  nd permanent tri
229de 67 67 65 72 20 73 63 68 65 6d 61 73 2e 20 2a 2f  gger schemas. */
229df 0a 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c 69  .  zWhere = sqli
229e0 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73  te3MPrintf(pPars
229e1 65 2d 3e 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65  e->db, "tbl_name
229e2 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  =%Q", zName);.  
229e3 69 66 28 20 21 7a 57 68 65 72 65 20 29 20 72 65  if( !zWhere ) re
229e4 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56  turn;.  sqlite3V
229e5 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
229e6 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62  ParseSchema, iDb
229e7 2c 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20  , 0, 0, zWhere, 
229e8 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 0a 23 69  P4_DYNAMIC);..#i
229e9 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
229ea 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 4e  T_TRIGGER.  /* N
229eb 6f 77 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65  ow, if the table
229ec 20 69 73 20 6e 6f 74 20 73 74 6f 72 65 64 20 69   is not stored i
229ed 6e 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62  n the temp datab
229ee 61 73 65 2c 20 72 65 6c 6f 61 64 20 61 6e 79 20  ase, reload any 
229ef 74 65 6d 70 20 0a 20 20 2a 2a 20 74 72 69 67 67  temp .  ** trigg
229f0 65 72 73 2e 20 44 6f 6e 27 74 20 75 73 65 20 49  ers. Don't use I
229f1 4e 28 2e 2e 2e 29 20 69 6e 20 63 61 73 65 20 53  N(...) in case S
229f2 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
229f3 45 52 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20  ERY is defined. 
229f4 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68  .  */.  if( (zWh
229f5 65 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69  ere=whereTempTri
229f6 67 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54  ggers(pParse, pT
229f7 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73  ab))!=0 ){.    s
229f8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
229f9 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65  (v, OP_ParseSche
229fa 6d 61 2c 20 31 2c 20 30 2c 20 30 2c 20 7a 57 68  ma, 1, 0, 0, zWh
229fb 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29  ere, P4_DYNAMIC)
229fc 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  ;.  }.#endif.}..
229fd 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
229fe 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
229ff 20 74 68 65 20 22 41 4c 54 45 52 20 54 41 42 4c   the "ALTER TABL
22a00 45 20 78 78 78 20 52 45 4e 41 4d 45 20 54 4f 20  E xxx RENAME TO 
22a01 79 79 79 22 20 0a 2a 2a 20 63 6f 6d 6d 61 6e 64  yyy" .** command
22a02 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  . .*/.SQLITE_PRI
22a03 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
22a04 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c  3AlterRenameTabl
22a05 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
22a06 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  se,            /
22a07 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74  * Parser context
22a08 2e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  . */.  SrcList *
22a09 70 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20  pSrc,           
22a0a 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f   /* The table to
22a0b 20 72 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 54 6f   rename. */.  To
22a0c 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20  ken *pName      
22a0d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e          /* The n
22a0e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a  ew table name. *
22a0f 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20  /.){.  int iDb; 
22a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22a11 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 68 61   /* Database tha
22a12 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  t contains the t
22a13 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  able */.  char *
22a14 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  zDb;            
22a15 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64      /* Name of d
22a16 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20  atabase iDb */. 
22a17 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
22a18 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
22a19 62 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 6d 65  ble being rename
22a1a 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61  d */.  char *zNa
22a1b 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  me = 0;         
22a1c 20 2f 2a 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61   /* NULL-termina
22a1d 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70  ted version of p
22a1e 4e 61 6d 65 20 2a 2f 20 0a 20 20 73 71 6c 69 74  Name */ .  sqlit
22a1f 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
22a20 3e 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73 65  >db; /* Database
22a21 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
22a22 20 69 6e 74 20 6e 54 61 62 4e 61 6d 65 3b 20 20   int nTabName;  
22a23 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
22a24 6d 62 65 72 20 6f 66 20 55 54 46 2d 38 20 63 68  mber of UTF-8 ch
22a25 61 72 61 63 74 65 72 73 20 69 6e 20 7a 54 61 62  aracters in zTab
22a26 4e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  Name */.  const 
22a27 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 3b 20  char *zTabName; 
22a28 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20      /* Original 
22a29 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
22a2a 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 0a  e */.  Vdbe *v;.
22a2b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22a2c 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 63 68  MIT_TRIGGER.  ch
22a2d 61 72 20 2a 7a 57 68 65 72 65 20 3d 20 30 3b 20  ar *zWhere = 0; 
22a2e 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65          /* Where
22a2f 20 63 6c 61 75 73 65 20 74 6f 20 6c 6f 63 61 74   clause to locat
22a30 65 20 74 65 6d 70 20 74 72 69 67 67 65 72 73 20  e temp triggers 
22a31 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56 54 61 62  */.#endif.  VTab
22a32 6c 65 20 2a 70 56 54 61 62 20 3d 20 30 3b 20 20  le *pVTab = 0;  
22a33 20 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 7a 65 72        /* Non-zer
22a34 6f 20 69 66 20 74 68 69 73 20 69 73 20 61 20 76  o if this is a v
22a35 2d 74 61 62 20 77 69 74 68 20 61 6e 20 78 52 65  -tab with an xRe
22a36 6e 61 6d 65 28 29 20 2a 2f 0a 20 20 0a 20 20 69  name() */.  .  i
22a37 66 28 20 4e 45 56 45 52 28 64 62 2d 3e 6d 61 6c  f( NEVER(db->mal
22a38 6c 6f 63 46 61 69 6c 65 64 29 20 29 20 67 6f 74  locFailed) ) got
22a39 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
22a3a 62 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ble;.  assert( p
22a3b 53 72 63 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  Src->nSrc==1 );.
22a3c 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
22a3d 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
22a3e 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62  texes(pParse->db
22a3f 29 20 29 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73  ) );..  pTab = s
22a40 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
22a41 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72  e(pParse, 0, pSr
22a42 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70  c->a[0].zName, p
22a43 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62  Src->a[0].zDatab
22a44 61 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61  ase);.  if( !pTa
22a45 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65  b ) goto exit_re
22a46 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 69 44  name_table;.  iD
22a47 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
22a48 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
22a49 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
22a4a 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62 2d  ma);.  zDb = db-
22a4b 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
22a4c 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e 55 4c  ..  /* Get a NUL
22a4d 4c 20 74 65 72 6d 69 6e 61 74 65 64 20 76 65 72  L terminated ver
22a4e 73 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77 20  sion of the new 
22a4f 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f 0a 20  table name. */. 
22a50 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
22a51 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
22a52 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  , pName);.  if( 
22a53 21 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20 65 78  !zName ) goto ex
22a54 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b  it_rename_table;
22a55 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
22a56 74 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  t a table or ind
22a57 65 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d 65 27  ex named 'zName'
22a58 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65 61 64   does not alread
22a59 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 69 6e 20  y exist.  ** in 
22a5a 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 49 66  database iDb. If
22a5b 20 73 6f 2c 20 74 68 69 73 20 69 73 20 61 6e 20   so, this is an 
22a5c 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  error..  */.  if
22a5d 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  ( sqlite3FindTab
22a5e 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44  le(db, zName, zD
22a5f 62 29 20 7c 7c 20 73 71 6c 69 74 65 33 46 69 6e  b) || sqlite3Fin
22a60 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65  dIndex(db, zName
22a61 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 73 71  , zDb) ){.    sq
22a62 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
22a63 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22  arse, .        "
22a64 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79  there is already
22a65 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 6f   another table o
22a66 72 20 69 6e 64 65 78 20 77 69 74 68 20 74 68 69  r index with thi
22a67 73 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a 4e 61  s name: %s", zNa
22a68 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  me);.    goto ex
22a69 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b  it_rename_table;
22a6a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
22a6b 73 75 72 65 20 69 74 20 69 73 20 6e 6f 74 20 61  sure it is not a
22a6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 62 65   system table be
22a6d 69 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f 72 20  ing altered, or 
22a6e 61 20 72 65 73 65 72 76 65 64 20 6e 61 6d 65 0a  a reserved name.
22a6f 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 74 61    ** that the ta
22a70 62 6c 65 20 69 73 20 62 65 69 6e 67 20 72 65 6e  ble is being ren
22a71 61 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a 20 20  amed to..  */.  
22a72 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 6c 65  if( sqlite3Strle
22a73 6e 33 30 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  n30(pTab->zName)
22a74 3e 36 20 0a 20 20 20 26 26 20 30 3d 3d 73 71 6c  >6 .   && 0==sql
22a75 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61  ite3StrNICmp(pTa
22a76 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74  b->zName, "sqlit
22a77 65 5f 22 2c 20 37 29 0a 20 20 29 7b 0a 20 20 20  e_", 7).  ){.   
22a78 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
22a79 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
22a7a 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c  %s may not be al
22a7b 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e  tered", pTab->zN
22a7c 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ame);.    goto e
22a7d 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
22a7e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49  ;.  }.  if( SQLI
22a7f 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68  TE_OK!=sqlite3Ch
22a80 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50  eckObjectName(pP
22a81 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a  arse, zName) ){.
22a82 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65      goto exit_re
22a83 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  name_table;.  }.
22a84 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22a85 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66 28 20  OMIT_VIEW.  if( 
22a86 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b  pTab->pSelect ){
22a87 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
22a88 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69  rMsg(pParse, "vi
22a89 65 77 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65  ew %s may not be
22a8a 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d   altered", pTab-
22a8b 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
22a8c 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
22a8d 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ble;.  }.#endif.
22a8e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22a8f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
22a90 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74  ON.  /* Invoke t
22a91 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  he authorization
22a92 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20   callback. */.  
22a93 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
22a94 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
22a95 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c  ITE_ALTER_TABLE,
22a96 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d   zDb, pTab->zNam
22a97 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74  e, 0) ){.    got
22a98 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61  o exit_rename_ta
22a99 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ble;.  }.#endif.
22a9a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22a9b 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
22a9c 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  E.  if( sqlite3V
22a9d 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
22a9e 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20  s(pParse, pTab) 
22a9f 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  ){.    goto exit
22aa0 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20  _rename_table;. 
22aa1 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72 74 75   }.  if( IsVirtu
22aa2 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
22aa3 70 56 54 61 62 20 3d 20 73 71 6c 69 74 65 33 47  pVTab = sqlite3G
22aa4 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61  etVTable(db, pTa
22aa5 62 29 3b 0a 20 20 20 20 69 66 28 20 70 56 54 61  b);.    if( pVTa
22aa6 62 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c  b->pVtab->pModul
22aa7 65 2d 3e 78 52 65 6e 61 6d 65 3d 3d 30 20 29 7b  e->xRename==0 ){
22aa8 0a 20 20 20 20 20 20 70 56 54 61 62 20 3d 20 30  .      pVTab = 0
22aa9 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
22aaa 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61  if..  /* Begin a
22aab 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
22aac 20 63 6f 64 65 20 74 68 65 20 56 65 72 69 66 79   code the Verify
22aad 43 6f 6f 6b 69 65 20 66 6f 72 20 64 61 74 61 62  Cookie for datab
22aae 61 73 65 20 69 44 62 2e 20 0a 20 20 2a 2a 20 54  ase iDb. .  ** T
22aaf 68 65 6e 20 6d 6f 64 69 66 79 20 74 68 65 20 73  hen modify the s
22ab0 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 28 73 69  chema cookie (si
22ab1 6e 63 65 20 74 68 65 20 41 4c 54 45 52 20 54 41  nce the ALTER TA
22ab2 42 4c 45 20 6d 6f 64 69 66 69 65 73 20 74 68 65  BLE modifies the
22ab3 0a 20 20 2a 2a 20 73 63 68 65 6d 61 29 2e 20 4f  .  ** schema). O
22ab4 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20  pen a statement 
22ab5 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74  transaction if t
22ab6 68 65 20 74 61 62 6c 65 20 69 73 20 61 20 76 69  he table is a vi
22ab7 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62 6c 65  rtual.  ** table
22ab8 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c  ..  */.  v = sql
22ab9 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
22aba 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20  se);.  if( v==0 
22abb 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  ){.    goto exit
22abc 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20  _rename_table;. 
22abd 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69   }.  sqlite3Begi
22abe 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
22abf 70 50 61 72 73 65 2c 20 70 56 54 61 62 21 3d 30  pParse, pVTab!=0
22ac0 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65  , iDb);.  sqlite
22ac1 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50  3ChangeCookie(pP
22ac2 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a 20 20 2f  arse, iDb);..  /
22ac3 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 76  * If this is a v
22ac4 69 72 74 75 61 6c 20 74 61 62 6c 65 2c 20 69 6e  irtual table, in
22ac5 76 6f 6b 65 20 74 68 65 20 78 52 65 6e 61 6d 65  voke the xRename
22ac6 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 66 0a 20  () function if. 
22ac7 20 2a 2a 20 6f 6e 65 20 69 73 20 64 65 66 69 6e   ** one is defin
22ac8 65 64 2e 20 54 68 65 20 78 52 65 6e 61 6d 65 28  ed. The xRename(
22ac9 29 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 20  ) callback will 
22aca 6d 6f 64 69 66 79 20 74 68 65 20 6e 61 6d 65 73  modify the names
22acb 0a 20 20 2a 2a 20 6f 66 20 61 6e 79 20 72 65 73  .  ** of any res
22acc 6f 75 72 63 65 73 20 75 73 65 64 20 62 79 20 74  ources used by t
22acd 68 65 20 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65  he v-table imple
22ace 6d 65 6e 74 61 74 69 6f 6e 20 28 69 6e 63 6c 75  mentation (inclu
22acf 64 69 6e 67 20 6f 74 68 65 72 0a 20 20 2a 2a 20  ding other.  ** 
22ad0 53 51 4c 69 74 65 20 74 61 62 6c 65 73 29 20 74  SQLite tables) t
22ad1 68 61 74 20 61 72 65 20 69 64 65 6e 74 69 66 69  hat are identifi
22ad2 65 64 20 62 79 20 74 68 65 20 6e 61 6d 65 20 6f  ed by the name o
22ad3 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  f the virtual ta
22ad4 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  ble..  */.#ifnde
22ad5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
22ad6 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
22ad7 20 70 56 54 61 62 20 29 7b 0a 20 20 20 20 69 6e   pVTab ){.    in
22ad8 74 20 69 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  t i = ++pParse->
22ad9 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
22ada 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
22adb 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 69 2c  P_String8, 0, i,
22adc 20 30 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20   0, zName, 0);. 
22add 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22ade 64 4f 70 34 28 76 2c 20 4f 50 5f 56 52 65 6e 61  dOp4(v, OP_VRena
22adf 6d 65 2c 20 69 2c 20 30 2c 20 30 2c 28 63 6f 6e  me, i, 0, 0,(con
22ae0 73 74 20 63 68 61 72 2a 29 70 56 54 61 62 2c 20  st char*)pVTab, 
22ae1 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 73 71  P4_VTAB);.    sq
22ae2 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50  lite3MayAbort(pP
22ae3 61 72 73 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  arse);.  }.#endi
22ae4 66 0a 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f  f..  /* figure o
22ae5 75 74 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d  ut how many UTF-
22ae6 38 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  8 characters are
22ae7 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a   in zName */.  z
22ae8 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e  TabName = pTab->
22ae9 7a 4e 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d  zName;.  nTabNam
22aea 65 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43  e = sqlite3Utf8C
22aeb 68 61 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c  harLen(zTabName,
22aec 20 2d 31 29 3b 0a 0a 23 69 66 20 21 64 65 66 69   -1);..#if !defi
22aed 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
22aee 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26 26 20  FOREIGN_KEY) && 
22aef 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
22af0 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20  OMIT_TRIGGER).  
22af1 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51  if( db->flags&SQ
22af2 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73  LITE_ForeignKeys
22af3 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 6f   ){.    /* If fo
22af4 72 65 69 67 6e 2d 6b 65 79 20 73 75 70 70 6f 72  reign-key suppor
22af5 74 20 69 73 20 65 6e 61 62 6c 65 64 2c 20 72 65  t is enabled, re
22af6 77 72 69 74 65 20 74 68 65 20 43 52 45 41 54 45  write the CREATE
22af7 20 54 41 42 4c 45 20 0a 20 20 20 20 2a 2a 20 73   TABLE .    ** s
22af8 74 61 74 65 6d 65 6e 74 73 20 63 6f 72 72 65 73  tatements corres
22af9 70 6f 6e 64 69 6e 67 20 74 6f 20 61 6c 6c 20 63  ponding to all c
22afa 68 69 6c 64 20 74 61 62 6c 65 73 20 6f 66 20 66  hild tables of f
22afb 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
22afc 72 61 69 6e 74 73 0a 20 20 20 20 2a 2a 20 66 6f  raints.    ** fo
22afd 72 20 77 68 69 63 68 20 74 68 65 20 72 65 6e 61  r which the rena
22afe 6d 65 64 20 74 61 62 6c 65 20 69 73 20 74 68 65  med table is the
22aff 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 20   parent table.  
22b00 2a 2f 0a 20 20 20 20 69 66 28 20 28 7a 57 68 65  */.    if( (zWhe
22b01 72 65 3d 77 68 65 72 65 46 6f 72 65 69 67 6e 4b  re=whereForeignK
22b02 65 79 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  eys(pParse, pTab
22b03 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ))!=0 ){.      s
22b04 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
22b05 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  e(pParse, .     
22b06 20 20 20 20 20 22 55 50 44 41 54 45 20 73 71 6c       "UPDATE sql
22b07 69 74 65 5f 6d 61 73 74 65 72 20 53 45 54 20 22  ite_master SET "
22b08 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22  .              "
22b09 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e  sql = sqlite_ren
22b0a 61 6d 65 5f 70 61 72 65 6e 74 28 73 71 6c 2c 20  ame_parent(sql, 
22b0b 25 51 2c 20 25 51 29 20 22 0a 20 20 20 20 20 20  %Q, %Q) ".      
22b0c 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20 25          "WHERE %
22b0d 73 3b 22 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 7a  s;", zTabName, z
22b0e 4e 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b 0a 20  Name, zWhere);. 
22b0f 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
22b10 65 65 28 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a  ee(db, zWhere);.
22b11 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
22b12 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68  ..  /* Modify th
22b13 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
22b14 74 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 65  table to use the
22b15 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e   new table name.
22b16 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65 73   */.  sqlite3Nes
22b17 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
22b18 0a 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25  .      "UPDATE %
22b19 51 2e 25 73 20 53 45 54 20 22 0a 23 69 66 64 65  Q.%s SET ".#ifde
22b1a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
22b1b 49 47 47 45 52 0a 20 20 20 20 20 20 20 20 20 20  IGGER.          
22b1c 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65  "sql = sqlite_re
22b1d 6e 61 6d 65 5f 74 61 62 6c 65 28 73 71 6c 2c 20  name_table(sql, 
22b1e 25 51 29 2c 20 22 0a 23 65 6c 73 65 0a 20 20 20  %Q), ".#else.   
22b1f 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 43 41         "sql = CA
22b20 53 45 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  SE ".           
22b21 20 22 57 48 45 4e 20 74 79 70 65 20 3d 20 27 74   "WHEN type = 't
22b22 72 69 67 67 65 72 27 20 54 48 45 4e 20 73 71 6c  rigger' THEN sql
22b23 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67 67  ite_rename_trigg
22b24 65 72 28 73 71 6c 2c 20 25 51 29 22 0a 20 20 20  er(sql, %Q)".   
22b25 20 20 20 20 20 20 20 20 20 22 45 4c 53 45 20 73           "ELSE s
22b26 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62  qlite_rename_tab
22b27 6c 65 28 73 71 6c 2c 20 25 51 29 20 45 4e 44 2c  le(sql, %Q) END,
22b28 20 22 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   ".#endif.      
22b29 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20      "tbl_name = 
22b2a 25 51 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20  %Q, ".          
22b2b 22 6e 61 6d 65 20 3d 20 43 41 53 45 20 22 0a 20  "name = CASE ". 
22b2c 20 20 20 20 20 20 20 20 20 20 20 22 57 48 45 4e             "WHEN
22b2d 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20 54 48   type='table' TH
22b2e 45 4e 20 25 51 20 22 0a 20 20 20 20 20 20 20 20  EN %Q ".        
22b2f 20 20 20 20 22 57 48 45 4e 20 6e 61 6d 65 20 4c      "WHEN name L
22b30 49 4b 45 20 27 73 71 6c 69 74 65 5f 61 75 74 6f  IKE 'sqlite_auto
22b31 69 6e 64 65 78 25 25 27 20 41 4e 44 20 74 79 70  index%%' AND typ
22b32 65 3d 27 69 6e 64 65 78 27 20 54 48 45 4e 20 22  e='index' THEN "
22b33 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 22 27  .             "'
22b34 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78  sqlite_autoindex
22b35 5f 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62 73  _' || %Q || subs
22b36 74 72 28 6e 61 6d 65 2c 25 64 2b 31 38 29 20 22  tr(name,%d+18) "
22b37 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c  .            "EL
22b38 53 45 20 6e 61 6d 65 20 45 4e 44 20 22 0a 20 20  SE name END ".  
22b39 20 20 20 20 22 57 48 45 52 45 20 74 62 6c 5f 6e      "WHERE tbl_n
22b3a 61 6d 65 3d 25 51 20 41 4e 44 20 22 0a 20 20 20  ame=%Q AND ".   
22b3b 20 20 20 20 20 20 20 22 28 74 79 70 65 3d 27 74         "(type='t
22b3c 61 62 6c 65 27 20 4f 52 20 74 79 70 65 3d 27 69  able' OR type='i
22b3d 6e 64 65 78 27 20 4f 52 20 74 79 70 65 3d 27 74  ndex' OR type='t
22b3e 72 69 67 67 65 72 27 29 3b 22 2c 20 0a 20 20 20  rigger');", .   
22b3f 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54     zDb, SCHEMA_T
22b40 41 42 4c 45 28 69 44 62 29 2c 20 7a 4e 61 6d 65  ABLE(iDb), zName
22b41 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20  , zName, zName, 
22b42 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22b43 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
22b44 20 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e 64 69 66     zName,.#endif
22b45 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20 6e 54  .      zName, nT
22b46 61 62 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65  abName, zTabName
22b47 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  .  );..#ifndef S
22b48 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
22b49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a 20 49 66  NCREMENT.  /* If
22b4a 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75   the sqlite_sequ
22b4b 65 6e 63 65 20 74 61 62 6c 65 20 65 78 69 73 74  ence table exist
22b4c 73 20 69 6e 20 74 68 69 73 20 64 61 74 61 62 61  s in this databa
22b4d 73 65 2c 20 74 68 65 6e 20 75 70 64 61 74 65 20  se, then update 
22b4e 0a 20 20 2a 2a 20 69 74 20 77 69 74 68 20 74 68  .  ** it with th
22b4f 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65  e new table name
22b50 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
22b51 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
22b52 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  , "sqlite_sequen
22b53 63 65 22 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20  ce", zDb) ){.   
22b54 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
22b55 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
22b56 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 25 77      "UPDATE \"%w
22b57 5c 22 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  \".sqlite_sequen
22b58 63 65 20 73 65 74 20 6e 61 6d 65 20 3d 20 25 51  ce set name = %Q
22b59 20 57 48 45 52 45 20 6e 61 6d 65 20 3d 20 25 51   WHERE name = %Q
22b5a 22 2c 0a 20 20 20 20 20 20 20 20 7a 44 62 2c 20  ",.        zDb, 
22b5b 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61  zName, pTab->zNa
22b5c 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  me);.  }.#endif.
22b5d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
22b5e 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f  OMIT_TRIGGER.  /
22b5f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 54  * If there are T
22b60 45 4d 50 20 74 72 69 67 67 65 72 73 20 6f 6e 20  EMP triggers on 
22b61 74 68 69 73 20 74 61 62 6c 65 2c 20 6d 6f 64 69  this table, modi
22b62 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 74 65  fy the sqlite_te
22b63 6d 70 5f 6d 61 73 74 65 72 0a 20 20 2a 2a 20 74  mp_master.  ** t
22b64 61 62 6c 65 2e 20 44 6f 6e 27 74 20 64 6f 20 74  able. Don't do t
22b65 68 69 73 20 69 66 20 74 68 65 20 74 61 62 6c 65  his if the table
22b66 20 62 65 69 6e 67 20 41 4c 54 45 52 65 64 20 69   being ALTERed i
22b67 73 20 69 74 73 65 6c 66 20 6c 6f 63 61 74 65 64  s itself located
22b68 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 74 65 6d   in.  ** the tem
22b69 70 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f  p database..  */
22b6a 0a 20 20 69 66 28 20 28 7a 57 68 65 72 65 3d 77  .  if( (zWhere=w
22b6b 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73  hereTempTriggers
22b6c 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 29 21  (pParse, pTab))!
22b6d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
22b6e 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
22b6f 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 22 55  rse, .        "U
22b70 50 44 41 54 45 20 73 71 6c 69 74 65 5f 74 65 6d  PDATE sqlite_tem
22b71 70 5f 6d 61 73 74 65 72 20 53 45 54 20 22 0a 20  p_master SET ". 
22b72 20 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20             "sql 
22b73 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f  = sqlite_rename_
22b74 74 72 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29  trigger(sql, %Q)
22b75 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  , ".            
22b76 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51 20 22  "tbl_name = %Q "
22b77 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57 48  .            "WH
22b78 45 52 45 20 25 73 3b 22 2c 20 7a 4e 61 6d 65 2c  ERE %s;", zName,
22b79 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b   zName, zWhere);
22b7a 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
22b7b 65 65 28 64 62 2c 20 7a 57 68 65 72 65 29 3b 0a  ee(db, zWhere);.
22b7c 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20    }.#endif..#if 
22b7d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
22b7e 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
22b7f 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
22b80 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
22b81 52 29 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61  R).  if( db->fla
22b82 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67  gs&SQLITE_Foreig
22b83 6e 4b 65 79 73 20 29 7b 0a 20 20 20 20 46 4b 65  nKeys ){.    FKe
22b84 79 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d  y *p;.    for(p=
22b85 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e  sqlite3FkReferen
22b86 63 65 73 28 70 54 61 62 29 3b 20 70 3b 20 70 3d  ces(pTab); p; p=
22b87 70 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20  p->pNextTo){.   
22b88 20 20 20 54 61 62 6c 65 20 2a 70 46 72 6f 6d 20     Table *pFrom 
22b89 3d 20 70 2d 3e 70 46 72 6f 6d 3b 0a 20 20 20 20  = p->pFrom;.    
22b8a 20 20 69 66 28 20 70 46 72 6f 6d 21 3d 70 54 61    if( pFrom!=pTa
22b8b 62 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c  b ){.        rel
22b8c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70  oadTableSchema(p
22b8d 50 61 72 73 65 2c 20 70 2d 3e 70 46 72 6f 6d 2c  Parse, p->pFrom,
22b8e 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a   pFrom->zName);.
22b8f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
22b90 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 44  }.#endif..  /* D
22b91 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64 20 74  rop and reload t
22b92 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c  he internal tabl
22b93 65 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 72  e schema. */.  r
22b94 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61  eloadTableSchema
22b95 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 7a  (pParse, pTab, z
22b96 4e 61 6d 65 29 3b 0a 0a 65 78 69 74 5f 72 65 6e  Name);..exit_ren
22b97 61 6d 65 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c  ame_table:.  sql
22b98 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
22b99 65 28 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 73  e(db, pSrc);.  s
22b9a 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
22b9b 20 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f 2a 0a   zName);.}.../*.
22b9c 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
22b9d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
22b9e 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75  e file format nu
22b9f 6d 62 65 72 20 69 73 20 61 74 20 6c 65 61 73 74  mber is at least
22ba0 20 6d 69 6e 46 6f 72 6d 61 74 2e 0a 2a 2a 20 54   minFormat..** T
22ba1 68 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  he generated cod
22ba2 65 20 77 69 6c 6c 20 69 6e 63 72 65 61 73 65 20  e will increase 
22ba3 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  the file format 
22ba4 6e 75 6d 62 65 72 20 69 66 20 6e 65 63 65 73 73  number if necess
22ba5 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ary..*/.SQLITE_P
22ba6 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
22ba7 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f  te3MinimumFileFo
22ba8 72 6d 61 74 28 50 61 72 73 65 20 2a 70 50 61 72  rmat(Parse *pPar
22ba9 73 65 2c 20 69 6e 74 20 69 44 62 2c 20 69 6e 74  se, int iDb, int
22baa 20 6d 69 6e 46 6f 72 6d 61 74 29 7b 0a 20 20 56   minFormat){.  V
22bab 64 62 65 20 2a 76 3b 0a 20 20 76 20 3d 20 73 71  dbe *v;.  v = sq
22bac 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
22bad 72 73 65 29 3b 0a 20 20 2f 2a 20 54 68 65 20 56  rse);.  /* The V
22bae 44 42 45 20 73 68 6f 75 6c 64 20 68 61 76 65 20  DBE should have 
22baf 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62  been allocated b
22bb0 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69  efore this routi
22bb1 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20  ne is called..  
22bb2 2a 2a 20 49 66 20 74 68 61 74 20 61 6c 6c 6f 63  ** If that alloc
22bb3 61 74 69 6f 6e 20 66 61 69 6c 65 64 2c 20 77 65  ation failed, we
22bb4 20 77 6f 75 6c 64 20 68 61 76 65 20 71 75 69 74   would have quit
22bb5 20 62 65 66 6f 72 65 20 72 65 61 63 68 69 6e 67   before reaching
22bb6 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69 6e 74   this.  ** point
22bb7 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41 59 53   */.  if( ALWAYS
22bb8 28 76 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  (v) ){.    int r
22bb9 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
22bba 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
22bbb 20 20 20 69 6e 74 20 72 32 20 3d 20 73 71 6c 69     int r2 = sqli
22bbc 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
22bbd 61 72 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 6a  arse);.    int j
22bbe 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  1;.    sqlite3Vd
22bbf 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52  beAddOp3(v, OP_R
22bc0 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20  eadCookie, iDb, 
22bc1 72 31 2c 20 42 54 52 45 45 5f 46 49 4c 45 5f 46  r1, BTREE_FILE_F
22bc2 4f 52 4d 41 54 29 3b 0a 20 20 20 20 73 71 6c 69  ORMAT);.    sqli
22bc3 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
22bc4 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71  (v, iDb);.    sq
22bc5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22bc6 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6d  v, OP_Integer, m
22bc7 69 6e 46 6f 72 6d 61 74 2c 20 72 32 29 3b 0a 20  inFormat, r2);. 
22bc8 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56     j1 = sqlite3V
22bc9 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
22bca 47 65 2c 20 72 32 2c 20 30 2c 20 72 31 29 3b 0a  Ge, r2, 0, r1);.
22bcb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22bcc 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43  ddOp3(v, OP_SetC
22bcd 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54 52 45  ookie, iDb, BTRE
22bce 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2c 20 72  E_FILE_FORMAT, r
22bcf 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  2);.    sqlite3V
22bd0 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
22bd1 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52  1);.    sqlite3R
22bd2 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
22bd3 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 73  arse, r1);.    s
22bd4 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
22bd5 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 29  pReg(pParse, r2)
22bd6 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
22bd7 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
22bd8 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61 6e 20  called after an 
22bd9 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e  "ALTER TABLE ...
22bda 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 0a   ADD" statement.
22bdb 2a 2a 20 68 61 73 20 62 65 65 6e 20 70 61 72 73  ** has been pars
22bdc 65 64 2e 20 41 72 67 75 6d 65 6e 74 20 70 43 6f  ed. Argument pCo
22bdd 6c 44 65 66 20 63 6f 6e 74 61 69 6e 73 20 74 68  lDef contains th
22bde 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 6e 65  e text of the ne
22bdf 77 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65 66 69  w.** column defi
22be0 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  nition..**.** Th
22be1 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
22be2 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  e pParse->pNewTa
22be3 62 6c 65 20 77 61 73 20 65 78 74 65 6e 64 65 64  ble was extended
22be4 20 74 6f 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 74   to include.** t
22be5 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 64 75  he new column du
22be6 72 69 6e 67 20 70 61 72 73 69 6e 67 2e 0a 2a 2f  ring parsing..*/
22be7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22be8 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
22be9 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e  rFinishAddColumn
22bea 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
22beb 54 6f 6b 65 6e 20 2a 70 43 6f 6c 44 65 66 29 7b  Token *pColDef){
22bec 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 20  .  Table *pNew; 
22bed 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
22bee 43 6f 70 79 20 6f 66 20 70 50 61 72 73 65 2d 3e  Copy of pParse->
22bef 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20 20 54  pNewTable */.  T
22bf0 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20  able *pTab;     
22bf1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
22bf2 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 20  e being altered 
22bf3 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
22bf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22bf5 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65  * Database numbe
22bf6 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
22bf7 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20  r *zDb;         
22bf8 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d   /* Database nam
22bf9 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
22bfa 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 20 20  r *zTab;        
22bfb 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a   /* Table name *
22bfc 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20  /.  char *zCol; 
22bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
22bfe 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64   Null-terminated
22bff 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69   column definiti
22c00 6f 6e 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a  on */.  Column *
22c01 70 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  pCol;           
22c02 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 63 6f 6c    /* The new col
22c03 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  umn */.  Expr *p
22c04 44 66 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20  Dflt;           
22c05 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61     /* Default va
22c06 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20  lue for the new 
22c07 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 73 71 6c 69  column */.  sqli
22c08 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
22c09 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
22c0a 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
22c0b 3b 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 70 50 61  ; */..  db = pPa
22c0c 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  rse->db;.  if( p
22c0d 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
22c0e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
22c0f 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4e 65 77  ) return;.  pNew
22c10 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
22c11 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 20  able;.  assert( 
22c12 70 4e 65 77 20 29 3b 0a 0a 20 20 61 73 73 65 72  pNew );..  asser
22c13 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
22c14 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 64  oldsAllMutexes(d
22c15 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71  b) );.  iDb = sq
22c16 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
22c17 65 78 28 64 62 2c 20 70 4e 65 77 2d 3e 70 53 63  ex(db, pNew->pSc
22c18 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64  hema);.  zDb = d
22c19 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
22c1a 65 3b 0a 20 20 7a 54 61 62 20 3d 20 26 70 4e 65  e;.  zTab = &pNe
22c1b 77 2d 3e 7a 4e 61 6d 65 5b 31 36 5d 3b 20 20 2f  w->zName[16];  /
22c1c 2a 20 53 6b 69 70 20 74 68 65 20 22 73 71 6c 69  * Skip the "sqli
22c1d 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 20 70 72  te_altertab_" pr
22c1e 65 66 69 78 20 6f 6e 20 74 68 65 20 6e 61 6d 65  efix on the name
22c1f 20 2a 2f 0a 20 20 70 43 6f 6c 20 3d 20 26 70 4e   */.  pCol = &pN
22c20 65 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77 2d 3e 6e  ew->aCol[pNew->n
22c21 43 6f 6c 2d 31 5d 3b 0a 20 20 70 44 66 6c 74 20  Col-1];.  pDflt 
22c22 3d 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 3b 0a 20  = pCol->pDflt;. 
22c23 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46   pTab = sqlite3F
22c24 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61  indTable(db, zTa
22c25 62 2c 20 7a 44 62 29 3b 0a 20 20 61 73 73 65 72  b, zDb);.  asser
22c26 74 28 20 70 54 61 62 20 29 3b 0a 0a 23 69 66 6e  t( pTab );..#ifn
22c27 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22c28 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
22c29 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75  /* Invoke the au
22c2a 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c  thorization call
22c2b 62 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73  back. */.  if( s
22c2c 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
22c2d 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41  pParse, SQLITE_A
22c2e 4c 54 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c  LTER_TABLE, zDb,
22c2f 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29   pTab->zName, 0)
22c30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
22c31 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
22c32 20 49 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   If the default 
22c33 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65  value for the ne
22c34 77 20 63 6f 6c 75 6d 6e 20 77 61 73 20 73 70 65  w column was spe
22c35 63 69 66 69 65 64 20 77 69 74 68 20 61 20 0a 20  cified with a . 
22c36 20 2a 2a 20 6c 69 74 65 72 61 6c 20 4e 55 4c 4c   ** literal NULL
22c37 2c 20 74 68 65 6e 20 73 65 74 20 70 44 66 6c 74  , then set pDflt
22c38 20 74 6f 20 30 2e 20 54 68 69 73 20 73 69 6d 70   to 0. This simp
22c39 6c 69 66 69 65 73 20 63 68 65 63 6b 69 6e 67 0a  lifies checking.
22c3a 20 20 2a 2a 20 66 6f 72 20 61 6e 20 53 51 4c 20    ** for an SQL 
22c3b 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 62 65 6c  NULL default bel
22c3c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ow..  */.  if( p
22c3d 44 66 6c 74 20 26 26 20 70 44 66 6c 74 2d 3e 6f  Dflt && pDflt->o
22c3e 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20  p==TK_NULL ){.  
22c3f 20 20 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d    pDflt = 0;.  }
22c40 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
22c41 74 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e  t the new column
22c42 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65   is not specifie
22c43 64 20 61 73 20 50 52 49 4d 41 52 59 20 4b 45 59  d as PRIMARY KEY
22c44 20 6f 72 20 55 4e 49 51 55 45 2e 0a 20 20 2a 2a   or UNIQUE..  **
22c45 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 4e   If there is a N
22c46 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
22c47 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 64 65 66  nt, then the def
22c48 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74  ault value for t
22c49 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6d  he.  ** column m
22c4a 75 73 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e  ust not be NULL.
22c4b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c  .  */.  if( pCol
22c4c 2d 3e 69 73 50 72 69 6d 4b 65 79 20 29 7b 0a 20  ->isPrimKey ){. 
22c4d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
22c4e 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e  sg(pParse, "Cann
22c4f 6f 74 20 61 64 64 20 61 20 50 52 49 4d 41 52 59  ot add a PRIMARY
22c50 20 4b 45 59 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20   KEY column");. 
22c51 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
22c52 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 6e 64 65   if( pNew->pInde
22c53 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  x ){.    sqlite3
22c54 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
22c55 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 55   "Cannot add a U
22c56 4e 49 51 55 45 20 63 6f 6c 75 6d 6e 22 29 3b 0a  NIQUE column");.
22c57 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
22c58 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73    if( (db->flags
22c59 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b  &SQLITE_ForeignK
22c5a 65 79 73 29 20 26 26 20 70 4e 65 77 2d 3e 70 46  eys) && pNew->pF
22c5b 4b 65 79 20 26 26 20 70 44 66 6c 74 20 29 7b 0a  Key && pDflt ){.
22c5c 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
22c5d 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20  Msg(pParse, .   
22c5e 20 20 20 20 20 22 43 61 6e 6e 6f 74 20 61 64 64       "Cannot add
22c5f 20 61 20 52 45 46 45 52 45 4e 43 45 53 20 63 6f   a REFERENCES co
22c60 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d 4e 55  lumn with non-NU
22c61 4c 4c 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  LL default value
22c62 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ");.    return;.
22c63 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e    }.  if( pCol->
22c64 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 44 66 6c  notNull && !pDfl
22c65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
22c66 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
22c67 20 0a 20 20 20 20 20 20 20 20 22 43 61 6e 6e 6f   .        "Canno
22c68 74 20 61 64 64 20 61 20 4e 4f 54 20 4e 55 4c 4c  t add a NOT NULL
22c69 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 64 65 66   column with def
22c6a 61 75 6c 74 20 76 61 6c 75 65 20 4e 55 4c 4c 22  ault value NULL"
22c6b 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
22c6c 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20   }..  /* Ensure 
22c6d 74 68 65 20 64 65 66 61 75 6c 74 20 65 78 70 72  the default expr
22c6e 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d 65 74 68  ession is someth
22c6f 69 6e 67 20 74 68 61 74 20 73 71 6c 69 74 65 33  ing that sqlite3
22c70 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 29 0a  ValueFromExpr().
22c71 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64 6c 65 20    ** can handle 
22c72 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52 52 45 4e  (i.e. not CURREN
22c73 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a 20 20 2a  T_TIME etc.).  *
22c74 2f 0a 20 20 69 66 28 20 70 44 66 6c 74 20 29 7b  /.  if( pDflt ){
22c75 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  .    sqlite3_val
22c76 75 65 20 2a 70 56 61 6c 3b 0a 20 20 20 20 69 66  ue *pVal;.    if
22c77 28 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  ( sqlite3ValueFr
22c78 6f 6d 45 78 70 72 28 64 62 2c 20 70 44 66 6c 74  omExpr(db, pDflt
22c79 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
22c7a 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2c 20  QLITE_AFF_NONE, 
22c7b 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20  &pVal) ){.      
22c7c 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
22c7d 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75   = 1;.      retu
22c7e 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  rn;.    }.    if
22c7f 28 20 21 70 56 61 6c 20 29 7b 0a 20 20 20 20 20  ( !pVal ){.     
22c80 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
22c81 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74  (pParse, "Cannot
22c82 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20 77 69   add a column wi
22c83 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e 74 20  th non-constant 
22c84 64 65 66 61 75 6c 74 22 29 3b 0a 20 20 20 20 20  default");.     
22c85 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
22c86 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46     sqlite3ValueF
22c87 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 0a  ree(pVal);.  }..
22c88 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65 20    /* Modify the 
22c89 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
22c8a 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 7a 43 6f  tement. */.  zCo
22c8b 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  l = sqlite3DbStr
22c8c 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29  NDup(db, (char*)
22c8d 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70 43 6f 6c  pColDef->z, pCol
22c8e 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66 28 20 7a  Def->n);.  if( z
22c8f 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61 72 20  Col ){.    char 
22c90 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c 5b 70 43  *zEnd = &zCol[pC
22c91 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a 20 20 20  olDef->n-1];.   
22c92 20 77 68 69 6c 65 28 20 7a 45 6e 64 3e 7a 43 6f   while( zEnd>zCo
22c93 6c 20 26 26 20 28 2a 7a 45 6e 64 3d 3d 27 3b 27  l && (*zEnd==';'
22c94 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 73 70 61   || sqlite3Isspa
22c95 63 65 28 2a 7a 45 6e 64 29 29 20 29 7b 0a 20 20  ce(*zEnd)) ){.  
22c96 20 20 20 20 2a 7a 45 6e 64 2d 2d 20 3d 20 27 5c      *zEnd-- = '\
22c97 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  0';.    }.    sq
22c98 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
22c99 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
22c9a 20 20 22 55 50 44 41 54 45 20 5c 22 25 77 5c 22    "UPDATE \"%w\"
22c9b 2e 25 73 20 53 45 54 20 22 0a 20 20 20 20 20 20  .%s SET ".      
22c9c 20 20 20 20 22 73 71 6c 20 3d 20 73 75 62 73 74      "sql = subst
22c9d 72 28 73 71 6c 2c 31 2c 25 64 29 20 7c 7c 20 27  r(sql,1,%d) || '
22c9e 2c 20 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75 62  , ' || %Q || sub
22c9f 73 74 72 28 73 71 6c 2c 25 64 29 20 22 0a 20 20  str(sql,%d) ".  
22ca0 20 20 20 20 20 20 22 57 48 45 52 45 20 74 79 70        "WHERE typ
22ca1 65 20 3d 20 27 74 61 62 6c 65 27 20 41 4e 44 20  e = 'table' AND 
22ca2 6e 61 6d 65 20 3d 20 25 51 22 2c 20 0a 20 20 20  name = %Q", .   
22ca3 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41 5f 54     zDb, SCHEMA_T
22ca4 41 42 4c 45 28 69 44 62 29 2c 20 70 4e 65 77 2d  ABLE(iDb), pNew-
22ca5 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 2c 20 7a  >addColOffset, z
22ca6 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61 64 64 43 6f  Col, pNew->addCo
22ca7 6c 4f 66 66 73 65 74 2b 31 2c 0a 20 20 20 20 20  lOffset+1,.     
22ca8 20 7a 54 61 62 0a 20 20 20 20 29 3b 0a 20 20 20   zTab.    );.   
22ca9 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
22caa 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 0a 20  b, zCol);.  }.. 
22cab 20 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75   /* If the defau
22cac 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  lt value of the 
22cad 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55  new column is NU
22cae 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65  LL, then set the
22caf 20 66 69 6c 65 0a 20 20 2a 2a 20 66 6f 72 6d 61   file.  ** forma
22cb0 74 20 74 6f 20 32 2e 20 49 66 20 74 68 65 20 64  t to 2. If the d
22cb1 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20  efault value of 
22cb2 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69  the new column i
22cb3 73 20 6e 6f 74 20 4e 55 4c 4c 2c 0a 20 20 2a 2a  s not NULL,.  **
22cb4 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74   the file format
22cb5 20 62 65 63 6f 6d 65 73 20 33 2e 0a 20 20 2a 2f   becomes 3..  */
22cb6 0a 20 20 73 71 6c 69 74 65 33 4d 69 6e 69 6d 75  .  sqlite3Minimu
22cb7 6d 46 69 6c 65 46 6f 72 6d 61 74 28 70 50 61 72  mFileFormat(pPar
22cb8 73 65 2c 20 69 44 62 2c 20 70 44 66 6c 74 20 3f  se, iDb, pDflt ?
22cb9 20 33 20 3a 20 32 29 3b 0a 0a 20 20 2f 2a 20 52   3 : 2);..  /* R
22cba 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61  eload the schema
22cbb 20 6f 66 20 74 68 65 20 6d 6f 64 69 66 69 65 64   of the modified
22cbc 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 72 65 6c   table. */.  rel
22cbd 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 70  oadTableSchema(p
22cbe 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 54 61  Parse, pTab, pTa
22cbf 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a  b->zName);.}../*
22cc0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
22cc1 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  n is called by t
22cc2 68 65 20 70 61 72 73 65 72 20 61 66 74 65 72 20  he parser after 
22cc3 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 20 69  the table-name i
22cc4 6e 0a 2a 2a 20 61 6e 20 22 41 4c 54 45 52 20 54  n.** an "ALTER T
22cc5 41 42 4c 45 20 3c 74 61 62 6c 65 2d 6e 61 6d 65  ABLE <table-name
22cc6 3e 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74  > ADD" statement
22cc7 20 69 73 20 70 61 72 73 65 64 2e 20 41 72 67 75   is parsed. Argu
22cc8 6d 65 6e 74 20 0a 2a 2a 20 70 53 72 63 20 69 73  ment .** pSrc is
22cc9 20 74 68 65 20 66 75 6c 6c 2d 6e 61 6d 65 20 6f   the full-name o
22cca 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  f the table bein
22ccb 67 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a  g altered..**.**
22ccc 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61   This routine ma
22ccd 6b 65 73 20 61 20 28 70 61 72 74 69 61 6c 29 20  kes a (partial) 
22cce 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61 62 6c  copy of the Tabl
22ccf 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 66  e structure.** f
22cd0 6f 72 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  or the table bei
22cd1 6e 67 20 61 6c 74 65 72 65 64 20 61 6e 64 20 73  ng altered and s
22cd2 65 74 73 20 50 61 72 73 65 2e 70 4e 65 77 54 61  ets Parse.pNewTa
22cd3 62 6c 65 20 74 6f 20 70 6f 69 6e 74 0a 2a 2a 20  ble to point.** 
22cd4 74 6f 20 69 74 2e 20 52 6f 75 74 69 6e 65 73 20  to it. Routines 
22cd5 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
22cd6 72 73 65 72 20 61 73 20 74 68 65 20 63 6f 6c 75  rser as the colu
22cd7 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 0a 2a 2a  mn definition.**
22cd8 20 69 73 20 70 61 72 73 65 64 20 28 69 2e 65 2e   is parsed (i.e.
22cd9 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d   sqlite3AddColum
22cda 6e 28 29 29 20 61 64 64 20 74 68 65 20 6e 65 77  n()) add the new
22cdb 20 43 6f 6c 75 6d 6e 20 64 61 74 61 20 74 6f 20   Column data to 
22cdc 0a 2a 2a 20 74 68 65 20 63 6f 70 79 2e 20 54 68  .** the copy. Th
22cdd 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61  e copy of the Ta
22cde 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73  ble structure is
22cdf 20 64 65 6c 65 74 65 64 20 62 79 20 74 6f 6b 65   deleted by toke
22ce0 6e 69 7a 65 2e 63 20 0a 2a 2a 20 61 66 74 65 72  nize.c .** after
22ce1 20 70 61 72 73 69 6e 67 20 69 73 20 66 69 6e 69   parsing is fini
22ce2 73 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f 75 74  shed..**.** Rout
22ce3 69 6e 65 20 73 71 6c 69 74 65 33 41 6c 74 65 72  ine sqlite3Alter
22ce4 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28  FinishAddColumn(
22ce5 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64  ) will be called
22ce6 20 74 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20   to complete.** 
22ce7 63 6f 64 69 6e 67 20 74 68 65 20 22 41 4c 54 45  coding the "ALTE
22ce8 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22  R TABLE ... ADD"
22ce9 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
22cea 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
22ceb 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 42  id sqlite3AlterB
22cec 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50 61  eginAddColumn(Pa
22ced 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63  rse *pParse, Src
22cee 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20 20 54  List *pSrc){.  T
22cef 61 62 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 54 61  able *pNew;.  Ta
22cf0 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62  ble *pTab;.  Vdb
22cf1 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b  e *v;.  int iDb;
22cf2 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
22cf3 6e 41 6c 6c 6f 63 3b 0a 20 20 73 71 6c 69 74 65  nAlloc;.  sqlite
22cf4 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
22cf5 64 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b 20 75  db;..  /* Look u
22cf6 70 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  p the table bein
22cf7 67 20 61 6c 74 65 72 65 64 2e 20 2a 2f 0a 20 20  g altered. */.  
22cf8 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
22cf9 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  pNewTable==0 );.
22cfa 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
22cfb 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
22cfc 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20 20 69  texes(db) );.  i
22cfd 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
22cfe 6c 65 64 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  led ) goto exit_
22cff 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e  begin_add_column
22d00 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ;.  pTab = sqlit
22d01 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50  e3LocateTable(pP
22d02 61 72 73 65 2c 20 30 2c 20 70 53 72 63 2d 3e 61  arse, 0, pSrc->a
22d03 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72 63 2d  [0].zName, pSrc-
22d04 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29  >a[0].zDatabase)
22d05 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20 29 20  ;.  if( !pTab ) 
22d06 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f  goto exit_begin_
22d07 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23 69 66  add_column;..#if
22d08 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22d09 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
22d0a 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
22d0b 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ab) ){.    sqlit
22d0c 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
22d0d 65 2c 20 22 76 69 72 74 75 61 6c 20 74 61 62 6c  e, "virtual tabl
22d0e 65 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 6c  es may not be al
22d0f 74 65 72 65 64 22 29 3b 0a 20 20 20 20 67 6f 74  tered");.    got
22d10 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  o exit_begin_add
22d11 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 23 65 6e  _column;.  }.#en
22d12 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  dif..  /* Make s
22d13 75 72 65 20 74 68 69 73 20 69 73 20 6e 6f 74 20  ure this is not 
22d14 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 41 4c  an attempt to AL
22d15 54 45 52 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20  TER a view. */. 
22d16 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65   if( pTab->pSele
22d17 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ct ){.    sqlite
22d18 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
22d19 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61 20  , "Cannot add a 
22d1a 63 6f 6c 75 6d 6e 20 74 6f 20 61 20 76 69 65 77  column to a view
22d1b 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  ");.    goto exi
22d1c 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75  t_begin_add_colu
22d1d 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  mn;.  }..  asser
22d1e 74 28 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f  t( pTab->addColO
22d1f 66 66 73 65 74 3e 30 20 29 3b 0a 20 20 69 44 62  ffset>0 );.  iDb
22d20 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
22d21 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62  ToIndex(db, pTab
22d22 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f  ->pSchema);..  /
22d23 2a 20 50 75 74 20 61 20 63 6f 70 79 20 6f 66 20  * Put a copy of 
22d24 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
22d25 20 69 6e 20 50 61 72 73 65 2e 70 4e 65 77 54 61   in Parse.pNewTa
22d26 62 6c 65 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  ble for the.  **
22d27 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d   sqlite3AddColum
22d28 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64  n() function and
22d29 20 66 72 69 65 6e 64 73 20 74 6f 20 6d 6f 64 69   friends to modi
22d2a 66 79 2e 20 20 42 75 74 20 6d 6f 64 69 66 79 0a  fy.  But modify.
22d2b 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20 62 79    ** the name by
22d2c 20 61 64 64 69 6e 67 20 61 6e 20 22 73 71 6c 69   adding an "sqli
22d2d 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 20 70 72  te_altertab_" pr
22d2e 65 66 69 78 2e 20 20 42 79 20 61 64 64 69 6e 67  efix.  By adding
22d2f 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65 66 69   this.  ** prefi
22d30 78 2c 20 77 65 20 69 6e 73 75 72 65 20 74 68 61  x, we insure tha
22d31 74 20 74 68 65 20 6e 61 6d 65 20 77 69 6c 6c 20  t the name will 
22d32 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68  not collide with
22d33 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20 20 2a   an existing.  *
22d34 2a 20 74 61 62 6c 65 20 62 65 63 61 75 73 65 20  * table because 
22d35 75 73 65 72 20 74 61 62 6c 65 20 61 72 65 20 6e  user table are n
22d36 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 68 61  ot allowed to ha
22d37 76 65 20 74 68 65 20 22 73 71 6c 69 74 65 5f 22  ve the "sqlite_"
22d38 0a 20 20 2a 2a 20 70 72 65 66 69 78 20 6f 6e 20  .  ** prefix on 
22d39 74 68 65 69 72 20 6e 61 6d 65 2e 0a 20 20 2a 2f  their name..  */
22d3a 0a 20 20 70 4e 65 77 20 3d 20 28 54 61 62 6c 65  .  pNew = (Table
22d3b 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  *)sqlite3DbMallo
22d3c 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
22d3d 28 54 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20  (Table));.  if( 
22d3e 21 70 4e 65 77 20 29 20 67 6f 74 6f 20 65 78 69  !pNew ) goto exi
22d3f 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75  t_begin_add_colu
22d40 6d 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 4e  mn;.  pParse->pN
22d41 65 77 54 61 62 6c 65 20 3d 20 70 4e 65 77 3b 0a  ewTable = pNew;.
22d42 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31    pNew->nRef = 1
22d43 3b 0a 20 20 70 4e 65 77 2d 3e 64 62 4d 65 6d 20  ;.  pNew->dbMem 
22d44 3d 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3b 0a 20  = pTab->dbMem;. 
22d45 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70 54   pNew->nCol = pT
22d46 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73 65  ab->nCol;.  asse
22d47 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e 30  rt( pNew->nCol>0
22d48 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20 28   );.  nAlloc = (
22d49 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29 2f  ((pNew->nCol-1)/
22d4a 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 65 72  8)*8)+8;.  asser
22d4b 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77 2d  t( nAlloc>=pNew-
22d4c 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63 25  >nCol && nAlloc%
22d4d 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d 70  8==0 && nAlloc-p
22d4e 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a 20  New->nCol<8 );. 
22d4f 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28 43   pNew->aCol = (C
22d50 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 44 62  olumn*)sqlite3Db
22d51 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
22d52 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e 41  izeof(Column)*nA
22d53 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d 3e 7a  lloc);.  pNew->z
22d54 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  Name = sqlite3MP
22d55 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74  rintf(db, "sqlit
22d56 65 5f 61 6c 74 65 72 74 61 62 5f 25 73 22 2c 20  e_altertab_%s", 
22d57 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
22d58 69 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f 6c 20  if( !pNew->aCol 
22d59 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d 65 20  || !pNew->zName 
22d5a 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  ){.    db->mallo
22d5b 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
22d5c 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e   goto exit_begin
22d5d 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d  _add_column;.  }
22d5e 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e  .  memcpy(pNew->
22d5f 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  aCol, pTab->aCol
22d60 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29  , sizeof(Column)
22d61 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a 20 20  *pNew->nCol);.  
22d62 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65 77 2d  for(i=0; i<pNew-
22d63 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
22d64 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20   Column *pCol = 
22d65 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a  &pNew->aCol[i];.
22d66 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20      pCol->zName 
22d67 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
22d68 70 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d  p(db, pCol->zNam
22d69 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 43  e);.    pCol->zC
22d6a 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f  oll = 0;.    pCo
22d6b 6c 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20  l->zType = 0;.  
22d6c 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20    pCol->pDflt = 
22d6d 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 44 66  0;.    pCol->zDf
22d6e 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4e  lt = 0;.  }.  pN
22d6f 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62  ew->pSchema = db
22d70 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
22d71 6d 61 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 43  ma;.  pNew->addC
22d72 6f 6c 4f 66 66 73 65 74 20 3d 20 70 54 61 62 2d  olOffset = pTab-
22d73 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b 0a 20  >addColOffset;. 
22d74 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b   pNew->nRef = 1;
22d75 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61 20 74  ..  /* Begin a t
22d76 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 69  ransaction and i
22d77 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73 63 68  ncrement the sch
22d78 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 2a 2f 0a  ema cookie.  */.
22d79 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
22d7a 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
22d7b 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20  rse, 0, iDb);.  
22d7c 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
22d7d 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
22d7e 28 20 21 76 20 29 20 67 6f 74 6f 20 65 78 69 74  ( !v ) goto exit
22d7f 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d  _begin_add_colum
22d80 6e 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 61 6e  n;.  sqlite3Chan
22d81 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c  geCookie(pParse,
22d82 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f 62 65 67   iDb);..exit_beg
22d83 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3a 0a 20  in_add_column:. 
22d84 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
22d85 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b  elete(db, pSrc);
22d86 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e  .  return;.}.#en
22d87 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41  dif  /* SQLITE_A
22d88 4c 54 45 52 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f  LTER_TABLE */../
22d89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
22d8a 6e 64 20 6f 66 20 61 6c 74 65 72 2e 63 20 2a 2a  nd of alter.c **
22d8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
22d8e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
22d8f 65 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c 79 7a  egin file analyz
22d90 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
22d91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22d92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
22d93 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 6c 79 20 38  *.** 2005 July 8
22d94 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
22d95 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
22d96 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
22d97 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
22d98 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
22d99 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
22d9a 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
22d9b 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
22d9c 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
22d9d 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
22d9e 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
22d9f 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
22da0 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
22da1 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
22da2 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
22da3 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
22da4 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
22da5 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
22da6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22da7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22da8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22da9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
22daa 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
22dab 74 61 69 6e 73 20 63 6f 64 65 20 61 73 73 6f 63  tains code assoc
22dac 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 41  iated with the A
22dad 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a  NALYZE command..
22dae 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
22daf 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a  E_OMIT_ANALYZE..
22db0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
22db1 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64  ne generates cod
22db2 65 20 74 68 61 74 20 6f 70 65 6e 73 20 74 68 65  e that opens the
22db3 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
22db4 62 6c 65 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69  ble for.** writi
22db5 6e 67 20 77 69 74 68 20 63 75 72 73 6f 72 20 69  ng with cursor i
22db6 53 74 61 74 43 75 72 2e 20 49 66 20 74 68 65 20  StatCur. If the 
22db7 6c 69 62 72 61 72 79 20 77 61 73 20 62 75 69 6c  library was buil
22db8 74 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 53 51  t with the.** SQ
22db9 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54  LITE_ENABLE_STAT
22dba 32 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 2c  2 macro defined,
22dbb 20 74 68 65 6e 20 74 68 65 20 73 71 6c 69 74 65   then the sqlite
22dbc 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69 73 0a  _stat2 table is.
22dbd 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72 20 77 72  ** opened for wr
22dbe 69 74 69 6e 67 20 75 73 69 6e 67 20 63 75 72 73  iting using curs
22dbf 6f 72 20 28 69 53 74 61 74 43 75 72 2b 31 29 0a  or (iStatCur+1).
22dc0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 71 6c  **.** If the sql
22dc1 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 73  ite_stat1 tables
22dc2 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69 6f   does not previo
22dc3 75 73 6c 79 20 65 78 69 73 74 2c 20 69 74 20 69  usly exist, it i
22dc4 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 20 53 69  s created..** Si
22dc5 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 65 20  milarly, if the 
22dc6 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62  sqlite_stat2 tab
22dc7 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  le does not exis
22dc8 74 20 61 6e 64 20 74 68 65 20 6c 69 62 72 61 72  t and the librar
22dc9 79 0a 2a 2a 20 69 73 20 63 6f 6d 70 69 6c 65 64  y.** is compiled
22dca 20 77 69 74 68 20 53 51 4c 49 54 45 5f 45 4e 41   with SQLITE_ENA
22dcb 42 4c 45 5f 53 54 41 54 32 20 64 65 66 69 6e 65  BLE_STAT2 define
22dcc 64 2c 20 69 74 20 69 73 20 63 72 65 61 74 65 64  d, it is created
22dcd 2e 20 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e  . .**.** Argumen
22dce 74 20 7a 57 68 65 72 65 20 6d 61 79 20 62 65 20  t zWhere may be 
22dcf 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62  a pointer to a b
22dd0 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
22dd1 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2c 0a 2a   a table name,.*
22dd2 2a 20 6f 72 20 69 74 20 6d 61 79 20 62 65 20 61  * or it may be a
22dd3 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 49   NULL pointer. I
22dd4 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  f it is not NULL
22dd5 2c 20 74 68 65 6e 20 61 6c 6c 20 65 6e 74 72 69  , then all entri
22dd6 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71 6c  es in.** the sql
22dd7 69 74 65 5f 73 74 61 74 31 20 61 6e 64 20 28 69  ite_stat1 and (i
22dd8 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 73 71  f applicable) sq
22dd9 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c 65  lite_stat2 table
22dda 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  s associated.** 
22ddb 77 69 74 68 20 74 68 65 20 6e 61 6d 65 64 20 74  with the named t
22ddc 61 62 6c 65 20 61 72 65 20 64 65 6c 65 74 65 64  able are deleted
22ddd 2e 20 49 66 20 7a 57 68 65 72 65 3d 3d 30 2c 20  . If zWhere==0, 
22dde 74 68 65 6e 20 63 6f 64 65 20 69 73 20 67 65 6e  then code is gen
22ddf 65 72 61 74 65 64 0a 2a 2a 20 74 6f 20 64 65 6c  erated.** to del
22de0 65 74 65 20 61 6c 6c 20 73 74 61 74 20 74 61 62  ete all stat tab
22de1 6c 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73  le entries..*/.s
22de2 74 61 74 69 63 20 76 6f 69 64 20 6f 70 65 6e 53  tatic void openS
22de3 74 61 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73  tatTable(.  Pars
22de4 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
22de5 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
22de6 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
22de7 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  iDb,            
22de8 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
22de9 61 73 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69  ase we are looki
22dea 6e 67 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ng in */.  int i
22deb 53 74 61 74 43 75 72 2c 20 20 20 20 20 20 20 20  StatCur,        
22dec 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 73     /* Open the s
22ded 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
22dee 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72  e on this cursor
22def 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
22df0 20 2a 7a 57 68 65 72 65 20 20 20 20 20 20 2f 2a   *zWhere      /*
22df1 20 44 65 6c 65 74 65 20 65 6e 74 72 69 65 73 20   Delete entries 
22df2 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
22df3 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b  this table */.){
22df4 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 63 74  .  static struct
22df5 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61   {.    const cha
22df6 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f  r *zName;.    co
22df7 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 73 3b  nst char *zCols;
22df8 0a 20 20 7d 20 61 54 61 62 6c 65 5b 5d 20 3d 20  .  } aTable[] = 
22df9 7b 0a 20 20 20 20 7b 20 22 73 71 6c 69 74 65 5f  {.    { "sqlite_
22dfa 73 74 61 74 31 22 2c 20 22 74 62 6c 2c 69 64 78  stat1", "tbl,idx
22dfb 2c 73 74 61 74 22 20 7d 2c 0a 23 69 66 64 65 66  ,stat" },.#ifdef
22dfc 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
22dfd 54 41 54 32 0a 20 20 20 20 7b 20 22 73 71 6c 69  TAT2.    { "sqli
22dfe 74 65 5f 73 74 61 74 32 22 2c 20 22 74 62 6c 2c  te_stat2", "tbl,
22dff 69 64 78 2c 73 61 6d 70 6c 65 6e 6f 2c 73 61 6d  idx,sampleno,sam
22e00 70 6c 65 22 20 7d 2c 0a 23 65 6e 64 69 66 0a 20  ple" },.#endif. 
22e01 20 7d 3b 0a 0a 20 20 69 6e 74 20 61 52 6f 6f 74   };..  int aRoot
22e02 5b 5d 20 3d 20 7b 30 2c 20 30 7d 3b 0a 20 20 75  [] = {0, 0};.  u
22e03 38 20 61 43 72 65 61 74 65 54 62 6c 5b 5d 20 3d  8 aCreateTbl[] =
22e04 20 7b 30 2c 20 30 7d 3b 0a 0a 20 20 69 6e 74 20   {0, 0};..  int 
22e05 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  i;.  sqlite3 *db
22e06 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
22e07 20 44 62 20 2a 70 44 62 3b 0a 20 20 56 64 62 65   Db *pDb;.  Vdbe
22e08 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
22e09 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
22e0a 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72  if( v==0 ) retur
22e0b 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  n;.  assert( sql
22e0c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
22e0d 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a  lMutexes(db) );.
22e0e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
22e0f 33 56 64 62 65 44 62 28 76 29 3d 3d 64 62 20 29  3VdbeDb(v)==db )
22e10 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61  ;.  pDb = &db->a
22e11 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 66 6f 72 28  Db[iDb];..  for(
22e12 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65  i=0; i<ArraySize
22e13 28 61 54 61 62 6c 65 29 3b 20 69 2b 2b 29 7b 0a  (aTable); i++){.
22e14 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
22e15 7a 54 61 62 20 3d 20 61 54 61 62 6c 65 5b 69 5d  zTab = aTable[i]
22e16 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 54 61 62 6c  .zName;.    Tabl
22e17 65 20 2a 70 53 74 61 74 3b 0a 20 20 20 20 69 66  e *pStat;.    if
22e18 28 20 28 70 53 74 61 74 20 3d 20 73 71 6c 69 74  ( (pStat = sqlit
22e19 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
22e1a 7a 54 61 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65  zTab, pDb->zName
22e1b 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  ))==0 ){.      /
22e1c 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61  * The sqlite_sta
22e1d 74 5b 31 32 5d 20 74 61 62 6c 65 20 64 6f 65 73  t[12] table does
22e1e 20 6e 6f 74 20 65 78 69 73 74 2e 20 43 72 65 61   not exist. Crea
22e1f 74 65 20 69 74 2e 20 4e 6f 74 65 20 74 68 61 74  te it. Note that
22e20 20 61 20 0a 20 20 20 20 20 20 2a 2a 20 73 69 64   a .      ** sid
22e21 65 2d 65 66 66 65 63 74 20 6f 66 20 74 68 65 20  e-effect of the 
22e22 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
22e23 74 65 6d 65 6e 74 20 69 73 20 74 6f 20 6c 65 61  tement is to lea
22e24 76 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20  ve the rootpage 
22e25 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  .      ** of the
22e26 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 72 65   new table in re
22e27 67 69 73 74 65 72 20 70 50 61 72 73 65 2d 3e 72  gister pParse->r
22e28 65 67 52 6f 6f 74 2e 20 54 68 69 73 20 69 73 20  egRoot. This is 
22e29 69 6d 70 6f 72 74 61 6e 74 20 0a 20 20 20 20 20  important .     
22e2a 20 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65 20   ** because the 
22e2b 4f 70 65 6e 57 72 69 74 65 20 6f 70 63 6f 64 65  OpenWrite opcode
22e2c 20 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20 6e   below will be n
22e2d 65 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20 20  eeding it. */.  
22e2e 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
22e2f 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
22e30 20 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45           "CREATE
22e31 20 54 41 42 4c 45 20 25 51 2e 25 73 28 25 73 29   TABLE %Q.%s(%s)
22e32 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  ", pDb->zName, z
22e33 54 61 62 2c 20 61 54 61 62 6c 65 5b 69 5d 2e 7a  Tab, aTable[i].z
22e34 43 6f 6c 73 0a 20 20 20 20 20 20 29 3b 0a 20 20  Cols.      );.  
22e35 20 20 20 20 61 52 6f 6f 74 5b 69 5d 20 3d 20 70      aRoot[i] = p
22e36 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 3b 0a  Parse->regRoot;.
22e37 20 20 20 20 20 20 61 43 72 65 61 74 65 54 62 6c        aCreateTbl
22e38 5b 69 5d 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c  [i] = 1;.    }el
22e39 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  se{.      /* The
22e3a 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65   table already e
22e3b 78 69 73 74 73 2e 20 49 66 20 7a 57 68 65 72 65  xists. If zWhere
22e3c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 64 65   is not NULL, de
22e3d 6c 65 74 65 20 61 6c 6c 20 65 6e 74 72 69 65 73  lete all entries
22e3e 20 0a 20 20 20 20 20 20 2a 2a 20 61 73 73 6f 63   .      ** assoc
22e3f 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74  iated with the t
22e40 61 62 6c 65 20 7a 57 68 65 72 65 2e 20 49 66 20  able zWhere. If 
22e41 7a 57 68 65 72 65 20 69 73 20 4e 55 4c 4c 2c 20  zWhere is NULL, 
22e42 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20 20  delete the.     
22e43 20 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74 65   ** entire conte
22e44 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  nts of the table
22e45 2e 20 2a 2f 0a 20 20 20 20 20 20 61 52 6f 6f 74  . */.      aRoot
22e46 5b 69 5d 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75  [i] = pStat->tnu
22e47 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  m;.      sqlite3
22e48 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65  TableLock(pParse
22e49 2c 20 69 44 62 2c 20 61 52 6f 6f 74 5b 69 5d 2c  , iDb, aRoot[i],
22e4a 20 31 2c 20 7a 54 61 62 29 3b 0a 20 20 20 20 20   1, zTab);.     
22e4b 20 69 66 28 20 7a 57 68 65 72 65 20 29 7b 0a 20   if( zWhere ){. 
22e4c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65         sqlite3Ne
22e4d 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
22e4e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 22 44 45  ,.           "DE
22e4f 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20  LETE FROM %Q.%s 
22e50 57 48 45 52 45 20 74 62 6c 3d 25 51 22 2c 20 70  WHERE tbl=%Q", p
22e51 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 54 61 62 2c  Db->zName, zTab,
22e52 20 7a 57 68 65 72 65 0a 20 20 20 20 20 20 20 20   zWhere.        
22e53 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
22e54 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
22e55 71 6c 69 74 65 5f 73 74 61 74 5b 31 32 5d 20 74  qlite_stat[12] t
22e56 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69  able already exi
22e57 73 74 73 2e 20 20 44 65 6c 65 74 65 20 61 6c 6c  sts.  Delete all
22e58 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20 20 20 20   rows. */.      
22e59 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22e5a 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72 2c  Op2(v, OP_Clear,
22e5b 20 61 52 6f 6f 74 5b 69 5d 2c 20 69 44 62 29 3b   aRoot[i], iDb);
22e5c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
22e5d 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68   }..  /* Open th
22e5e 65 20 73 71 6c 69 74 65 5f 73 74 61 74 5b 31 32  e sqlite_stat[12
22e5f 5d 20 74 61 62 6c 65 73 20 66 6f 72 20 77 72 69  ] tables for wri
22e60 74 69 6e 67 2e 20 2a 2f 0a 20 20 66 6f 72 28 69  ting. */.  for(i
22e61 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
22e62 61 54 61 62 6c 65 29 3b 20 69 2b 2b 29 7b 0a 20  aTable); i++){. 
22e63 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22e64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  dOp3(v, OP_OpenW
22e65 72 69 74 65 2c 20 69 53 74 61 74 43 75 72 2b 69  rite, iStatCur+i
22e66 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 69 44 62 29  , aRoot[i], iDb)
22e67 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
22e68 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
22e69 20 28 63 68 61 72 20 2a 29 33 2c 20 50 34 5f 49   (char *)3, P4_I
22e6a 4e 54 33 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  NT32);.    sqlit
22e6b 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
22e6c 2c 20 61 43 72 65 61 74 65 54 62 6c 5b 69 5d 29  , aCreateTbl[i])
22e6d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
22e6e 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
22e6f 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f  do an analysis o
22e70 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73  f all indices as
22e71 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a  sociated with.**
22e72 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e   a single table.
22e73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
22e74 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28  analyzeOneTable(
22e75 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
22e76 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f  ,   /* Parser co
22e77 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
22e78 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 54   *pTab,     /* T
22e79 61 62 6c 65 20 77 68 6f 73 65 20 69 6e 64 69 63  able whose indic
22e7a 65 73 20 61 72 65 20 74 6f 20 62 65 20 61 6e 61  es are to be ana
22e7b 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  lyzed */.  int i
22e7c 53 74 61 74 43 75 72 2c 20 20 20 20 2f 2a 20 49  StatCur,    /* I
22e7d 6e 64 65 78 20 6f 66 20 56 64 62 65 43 75 72 73  ndex of VdbeCurs
22e7e 6f 72 20 74 68 61 74 20 77 72 69 74 65 73 20 74  or that writes t
22e7f 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
22e80 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
22e81 4d 65 6d 20 20 20 20 20 20 20 20 20 2f 2a 20 41  Mem         /* A
22e82 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20  vailable memory 
22e83 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67 69 6e 20  locations begin 
22e84 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  here */.){.  sql
22e85 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
22e86 65 2d 3e 64 62 3b 20 20 20 20 2f 2a 20 44 61 74  e->db;    /* Dat
22e87 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
22e88 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
22e89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22e8a 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f 20 62 65  * An index to be
22e8b 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a  ing analyzed */.
22e8c 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20    int iIdxCur;  
22e8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
22e8e 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f 6e  * Cursor open on
22e8f 20 69 6e 64 65 78 20 62 65 69 6e 67 20 61 6e 61   index being ana
22e90 6c 79 7a 65 64 20 2a 2f 0a 20 20 56 64 62 65 20  lyzed */.  Vdbe 
22e91 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
22e92 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
22e93 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 62  irtual machine b
22e94 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 2a 2f  eing built up */
22e95 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
22e96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22e97 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
22e98 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c 6f  */.  int topOfLo
22e99 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  op;             
22e9a 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20    /* The top of 
22e9b 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  the loop */.  in
22e9c 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20 20  t endOfLoop;    
22e9d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
22e9e 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f  e end of the loo
22e9f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b  p */.  int addr;
22ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22ea1 20 20 20 20 2f 2a 20 54 68 65 20 61 64 64 72 65      /* The addre
22ea2 73 73 20 6f 66 20 61 6e 20 69 6e 73 74 72 75 63  ss of an instruc
22ea3 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44  tion */.  int iD
22ea4 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
22ea5 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
22ea6 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  of database cont
22ea7 61 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20  aining pTab */. 
22ea8 20 69 6e 74 20 72 65 67 54 61 62 6e 61 6d 65 20   int regTabname 
22ea9 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 2f 2a  = iMem++;     /*
22eaa 20 52 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69   Register contai
22eab 6e 69 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65 20  ning table name 
22eac 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 64 78 6e  */.  int regIdxn
22ead 61 6d 65 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20  ame = iMem++;   
22eae 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 63 6f    /* Register co
22eaf 6e 74 61 69 6e 69 6e 67 20 69 6e 64 65 78 20 6e  ntaining index n
22eb0 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ame */.  int reg
22eb1 53 61 6d 70 6c 65 6e 6f 20 3d 20 69 4d 65 6d 2b  Sampleno = iMem+
22eb2 2b 3b 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65  +;    /* Registe
22eb3 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 65 78  r containing nex
22eb4 74 20 73 61 6d 70 6c 65 20 6e 75 6d 62 65 72 20  t sample number 
22eb5 2a 2f 0a 20 20 69 6e 74 20 72 65 67 43 6f 6c 20  */.  int regCol 
22eb6 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20  = iMem++;       
22eb7 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20    /* Content of 
22eb8 61 20 63 6f 6c 75 6d 6e 20 61 6e 61 6c 79 7a 65  a column analyze
22eb9 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  d table */.  int
22eba 20 72 65 67 52 65 63 20 3d 20 69 4d 65 6d 2b 2b   regRec = iMem++
22ebb 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67  ;         /* Reg
22ebc 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63 6f  ister holding co
22ebd 6d 70 6c 65 74 65 64 20 72 65 63 6f 72 64 20 2a  mpleted record *
22ebe 2f 0a 20 20 69 6e 74 20 72 65 67 54 65 6d 70 20  /.  int regTemp 
22ebf 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20  = iMem++;       
22ec0 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 75 73   /* Temporary us
22ec1 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  e register */.  
22ec2 69 6e 74 20 72 65 67 52 6f 77 69 64 20 3d 20 69  int regRowid = i
22ec3 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20  Mem++;       /* 
22ec4 52 6f 77 69 64 20 66 6f 72 20 74 68 65 20 69 6e  Rowid for the in
22ec5 73 65 72 74 65 64 20 72 65 63 6f 72 64 20 2a 2f  serted record */
22ec6 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
22ec7 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20 20 69  ENABLE_STAT2.  i
22ec8 6e 74 20 72 65 67 54 65 6d 70 32 20 3d 20 69 4d  nt regTemp2 = iM
22ec9 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20 54  em++;       /* T
22eca 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65 67  emporary use reg
22ecb 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72  ister */.  int r
22ecc 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 20 3d 20  egSamplerecno = 
22ecd 69 4d 65 6d 2b 2b 3b 20 2f 2a 20 49 6e 64 65 78  iMem++; /* Index
22ece 20 6f 66 20 6e 65 78 74 20 73 61 6d 70 6c 65 20   of next sample 
22ecf 74 6f 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  to record */.  i
22ed0 6e 74 20 72 65 67 52 65 63 6e 6f 20 3d 20 69 4d  nt regRecno = iM
22ed1 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20 43  em++;       /* C
22ed2 75 72 72 65 6e 74 20 73 61 6d 70 6c 65 20 69 6e  urrent sample in
22ed3 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  dex */.  int reg
22ed4 4c 61 73 74 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20  Last = iMem++;  
22ed5 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
22ed6 66 20 6c 61 73 74 20 73 61 6d 70 6c 65 20 74 6f  f last sample to
22ed7 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74   record */.  int
22ed8 20 72 65 67 46 69 72 73 74 20 3d 20 69 4d 65 6d   regFirst = iMem
22ed9 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  ++;       /* Ind
22eda 65 78 20 6f 66 20 66 69 72 73 74 20 73 61 6d 70  ex of first samp
22edb 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 2a 2f 0a  le to record */.
22edc 23 65 6e 64 69 66 0a 0a 20 20 76 20 3d 20 73 71  #endif..  v = sq
22edd 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
22ede 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30  rse);.  if( v==0
22edf 20 7c 7c 20 4e 45 56 45 52 28 70 54 61 62 3d 3d   || NEVER(pTab==
22ee0 30 29 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64  0) || pTab->pInd
22ee1 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ex==0 ){.    /* 
22ee2 44 6f 20 6e 6f 20 61 6e 61 6c 79 73 69 73 20 66  Do no analysis f
22ee3 6f 72 20 74 61 62 6c 65 73 20 74 68 61 74 20 68  or tables that h
22ee4 61 76 65 20 6e 6f 20 69 6e 64 69 63 65 73 20 2a  ave no indices *
22ee5 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  /.    return;.  
22ee6 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  }.  assert( sqli
22ee7 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
22ee8 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20  Mutexes(db) );. 
22ee9 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
22eea 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
22eeb 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
22eec 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
22eed 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   );.#ifndef SQLI
22eee 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
22eef 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69  ATION.  if( sqli
22ef0 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
22ef1 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4e 41 4c  rse, SQLITE_ANAL
22ef2 59 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  YZE, pTab->zName
22ef3 2c 20 30 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61  , 0,.      db->a
22ef4 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20 29 20  Db[iDb].zName ) 
22ef5 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
22ef6 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
22ef7 45 73 74 61 62 6c 69 73 68 20 61 20 72 65 61 64  Establish a read
22ef8 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62  -lock on the tab
22ef9 6c 65 20 61 74 20 74 68 65 20 73 68 61 72 65 64  le at the shared
22efa 2d 63 61 63 68 65 20 6c 65 76 65 6c 2e 20 2a 2f  -cache level. */
22efb 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c  .  sqlite3TableL
22efc 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ock(pParse, iDb,
22efd 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20   pTab->tnum, 0, 
22efe 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20  pTab->zName);.. 
22eff 20 69 49 64 78 43 75 72 20 3d 20 70 50 61 72 73   iIdxCur = pPars
22f00 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72  e->nTab++;.  for
22f01 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
22f02 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
22f03 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
22f04 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70 49 64 78   int nCol = pIdx
22f05 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 4b  ->nColumn;.    K
22f06 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73  eyInfo *pKey = s
22f07 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e  qlite3IndexKeyin
22f08 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78 29  fo(pParse, pIdx)
22f09 3b 0a 0a 20 20 20 20 69 66 28 20 69 4d 65 6d 2b  ;..    if( iMem+
22f0a 31 2b 28 6e 43 6f 6c 2a 32 29 3e 70 50 61 72 73  1+(nCol*2)>pPars
22f0b 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20 20 20 20 20  e->nMem ){.     
22f0c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
22f0d 69 4d 65 6d 2b 31 2b 28 6e 43 6f 6c 2a 32 29 3b  iMem+1+(nCol*2);
22f0e 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f  .    }..    /* O
22f0f 70 65 6e 20 61 20 63 75 72 73 6f 72 20 74 6f 20  pen a cursor to 
22f10 74 68 65 20 69 6e 64 65 78 20 74 6f 20 62 65 20  the index to be 
22f11 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 20 20 20  analyzed. */.   
22f12 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d 73 71   assert( iDb==sq
22f13 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
22f14 65 78 28 64 62 2c 20 70 49 64 78 2d 3e 70 53 63  ex(db, pIdx->pSc
22f15 68 65 6d 61 29 20 29 3b 0a 20 20 20 20 73 71 6c  hema) );.    sql
22f16 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
22f17 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69  , OP_OpenRead, i
22f18 49 64 78 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e  IdxCur, pIdx->tn
22f19 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20  um, iDb,.       
22f1a 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c 20 50   (char *)pKey, P
22f1b 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
22f1c 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  F);.    VdbeComm
22f1d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49  ent((v, "%s", pI
22f1e 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20  dx->zName));..  
22f1f 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68    /* Populate th
22f20 65 20 72 65 67 69 73 74 65 72 73 20 63 6f 6e 74  e registers cont
22f21 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65  aining the table
22f22 20 61 6e 64 20 69 6e 64 65 78 20 6e 61 6d 65 73   and index names
22f23 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 61  . */.    if( pTa
22f24 62 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 64 78 20  b->pIndex==pIdx 
22f25 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
22f26 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
22f27 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67  _String8, 0, reg
22f28 54 61 62 6e 61 6d 65 2c 20 30 2c 20 70 54 61 62  Tabname, 0, pTab
22f29 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
22f2a 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
22f2b 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
22f2c 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 49 64  tring8, 0, regId
22f2d 78 6e 61 6d 65 2c 20 30 2c 20 70 49 64 78 2d 3e  xname, 0, pIdx->
22f2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 23 69 66 64  zName, 0);..#ifd
22f2f 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
22f30 5f 53 54 41 54 32 0a 0a 20 20 20 20 2f 2a 20 49  _STAT2..    /* I
22f31 66 20 74 68 69 73 20 69 74 65 72 61 74 69 6f 6e  f this iteration
22f32 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 73 20   of the loop is 
22f33 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20  generating code 
22f34 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 0a 20  to analyze the. 
22f35 20 20 20 2a 2a 20 66 69 72 73 74 20 69 6e 64 65     ** first inde
22f36 78 20 69 6e 20 74 68 65 20 70 54 61 62 2d 3e 70  x in the pTab->p
22f37 49 6e 64 65 78 20 6c 69 73 74 2c 20 74 68 65 6e  Index list, then
22f38 20 72 65 67 69 73 74 65 72 20 72 65 67 4c 61 73   register regLas
22f39 74 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e 6f 74  t has.    ** not
22f3a 20 62 65 65 6e 20 70 6f 70 75 6c 61 74 65 64 2e   been populated.
22f3b 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 70 6f   In this case po
22f3c 70 75 6c 61 74 65 20 69 74 20 6e 6f 77 2e 20 20  pulate it now.  
22f3d 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  */.    if( pTab-
22f3e 3e 70 49 6e 64 65 78 3d 3d 70 49 64 78 20 29 7b  >pIndex==pIdx ){
22f3f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22f40 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
22f41 6e 74 65 67 65 72 2c 20 53 51 4c 49 54 45 5f 49  nteger, SQLITE_I
22f42 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2c 20 72 65  NDEX_SAMPLES, re
22f43 67 53 61 6d 70 6c 65 72 65 63 6e 6f 29 3b 0a 20  gSamplerecno);. 
22f44 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22f45 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
22f46 65 67 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e 44  eger, SQLITE_IND
22f47 45 58 5f 53 41 4d 50 4c 45 53 2a 32 2d 31 2c 20  EX_SAMPLES*2-1, 
22f48 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20  regTemp);.      
22f49 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22f4a 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
22f4b 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41   SQLITE_INDEX_SA
22f4c 4d 50 4c 45 53 2a 32 2c 20 72 65 67 54 65 6d 70  MPLES*2, regTemp
22f4d 32 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74  2);..      sqlit
22f4e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
22f4f 4f 50 5f 43 6f 75 6e 74 2c 20 69 49 64 78 43 75  OP_Count, iIdxCu
22f50 72 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20 20 20  r, regLast);.   
22f51 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22f52 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
22f53 20 30 2c 20 72 65 67 46 69 72 73 74 29 3b 0a 20   0, regFirst);. 
22f54 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
22f55 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
22f56 20 4f 50 5f 4c 74 2c 20 72 65 67 53 61 6d 70 6c   OP_Lt, regSampl
22f57 65 72 65 63 6e 6f 2c 20 30 2c 20 72 65 67 4c 61  erecno, 0, regLa
22f58 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  st);.      sqlit
22f59 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
22f5a 4f 50 5f 44 69 76 69 64 65 2c 20 72 65 67 54 65  OP_Divide, regTe
22f5b 6d 70 32 2c 20 72 65 67 4c 61 73 74 2c 20 72 65  mp2, regLast, re
22f5c 67 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20 73  gFirst);.      s
22f5d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22f5e 28 76 2c 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 2c  (v, OP_Multiply,
22f5f 20 72 65 67 4c 61 73 74 2c 20 72 65 67 54 65 6d   regLast, regTem
22f60 70 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20 20 20  p, regLast);.   
22f61 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22f62 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
22f63 6d 2c 20 72 65 67 4c 61 73 74 2c 20 53 51 4c 49  m, regLast, SQLI
22f64 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53  TE_INDEX_SAMPLES
22f65 2a 32 2d 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  *2-2);.      sql
22f66 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22f67 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 20 72 65  , OP_Divide,  re
22f68 67 54 65 6d 70 32 2c 20 72 65 67 4c 61 73 74 2c  gTemp2, regLast,
22f69 20 72 65 67 4c 61 73 74 29 3b 0a 20 20 20 20 20   regLast);.     
22f6a 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
22f6b 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 20  Here(v, addr);. 
22f6c 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 5a 65 72     }..    /* Zer
22f6d 6f 20 74 68 65 20 72 65 67 53 61 6d 70 6c 65 6e  o the regSamplen
22f6e 6f 20 61 6e 64 20 72 65 67 52 65 63 6e 6f 20 72  o and regRecno r
22f6f 65 67 69 73 74 65 72 73 2e 20 2a 2f 0a 20 20 20  egisters. */.   
22f70 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22f71 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
22f72 2c 20 30 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f  , 0, regSampleno
22f73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22f74 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
22f75 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52 65  nteger, 0, regRe
22f76 63 6e 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  cno);.    sqlite
22f77 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22f78 50 5f 43 6f 70 79 2c 20 72 65 67 46 69 72 73 74  P_Copy, regFirst
22f79 2c 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f  , regSamplerecno
22f7a 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f  );.#endif..    /
22f7b 2a 20 54 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d  * The block of m
22f7c 65 6d 6f 72 79 20 63 65 6c 6c 73 20 69 6e 69 74  emory cells init
22f7d 69 61 6c 69 7a 65 64 20 68 65 72 65 20 69 73 20  ialized here is 
22f7e 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 2e  used as follows.
22f7f 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
22f80 20 20 69 4d 65 6d 3a 20 20 20 20 20 20 20 20 20    iMem:         
22f81 20 20 20 20 20 20 20 0a 20 20 20 20 2a 2a 20 20         .    **  
22f82 20 20 20 20 20 20 54 68 65 20 74 6f 74 61 6c 20        The total 
22f83 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69  number of rows i
22f84 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20  n the table..   
22f85 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69 4d   **.    **    iM
22f86 65 6d 2b 31 20 2e 2e 20 69 4d 65 6d 2b 6e 43 6f  em+1 .. iMem+nCo
22f87 6c 3a 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  l: .    **      
22f88 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73 74    Number of dist
22f89 69 6e 63 74 20 65 6e 74 72 69 65 73 20 69 6e 20  inct entries in 
22f8a 69 6e 64 65 78 20 63 6f 6e 73 69 64 65 72 69 6e  index considerin
22f8b 67 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20  g the .    **   
22f8c 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74 20 4e       left-most N
22f8d 20 63 6f 6c 75 6d 6e 73 20 6f 6e 6c 79 2c 20 77   columns only, w
22f8e 68 65 72 65 20 4e 20 69 73 20 62 65 74 77 65 65  here N is betwee
22f8f 6e 20 31 20 61 6e 64 20 6e 43 6f 6c 2c 20 0a 20  n 1 and nCol, . 
22f90 20 20 20 2a 2a 20 20 20 20 20 20 20 20 69 6e 63     **        inc
22f91 6c 75 73 69 76 65 2e 0a 20 20 20 20 2a 2a 0a 20  lusive..    **. 
22f92 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d 2b 6e 43     **    iMem+nC
22f93 6f 6c 2b 31 20 2e 2e 20 4d 65 6d 2b 32 2a 6e 43  ol+1 .. Mem+2*nC
22f94 6f 6c 3a 20 20 0a 20 20 20 20 2a 2a 20 20 20 20  ol:  .    **    
22f95 20 20 20 20 50 72 65 76 69 6f 75 73 20 76 61 6c      Previous val
22f96 75 65 20 6f 66 20 69 6e 64 65 78 65 64 20 63 6f  ue of indexed co
22f97 6c 75 6d 6e 73 2c 20 66 72 6f 6d 20 6c 65 66 74  lumns, from left
22f98 20 74 6f 20 72 69 67 68 74 2e 0a 20 20 20 20 2a   to right..    *
22f99 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c 73 20 69  *.    ** Cells i
22f9a 4d 65 6d 20 74 68 72 6f 75 67 68 20 69 4d 65 6d  Mem through iMem
22f9b 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69 74 69 61  +nCol are initia
22f9c 6c 69 7a 65 64 20 74 6f 20 30 2e 20 54 68 65 20  lized to 0. The 
22f9d 6f 74 68 65 72 73 20 61 72 65 20 0a 20 20 20 20  others are .    
22f9e 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  ** initialized t
22f9f 6f 20 63 6f 6e 74 61 69 6e 20 61 6e 20 53 51 4c  o contain an SQL
22fa0 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20   NULL..    */.  
22fa1 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 6e 43    for(i=0; i<=nC
22fa2 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
22fa3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22fa4 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
22fa5 20 30 2c 20 69 4d 65 6d 2b 69 29 3b 0a 20 20 20   0, iMem+i);.   
22fa6 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
22fa7 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
22fa8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22fa9 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
22faa 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b 69  , 0, iMem+nCol+i
22fab 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  +1);.    }..    
22fac 2f 2a 20 53 74 61 72 74 20 74 68 65 20 61 6e 61  /* Start the ana
22fad 6c 79 73 69 73 20 6c 6f 6f 70 2e 20 54 68 69 73  lysis loop. This
22fae 20 6c 6f 6f 70 20 72 75 6e 73 20 74 68 72 6f 75   loop runs throu
22faf 67 68 20 61 6c 6c 20 74 68 65 20 65 6e 74 72 69  gh all the entri
22fb0 65 73 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65  es in.    ** the
22fb1 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2e 20 20   index b-tree.  
22fb2 2a 2f 0a 20 20 20 20 65 6e 64 4f 66 4c 6f 6f 70  */.    endOfLoop
22fb3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
22fb4 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
22fb5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22fb6 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  2(v, OP_Rewind, 
22fb7 69 49 64 78 43 75 72 2c 20 65 6e 64 4f 66 4c 6f  iIdxCur, endOfLo
22fb8 6f 70 29 3b 0a 20 20 20 20 74 6f 70 4f 66 4c 6f  op);.    topOfLo
22fb9 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  op = sqlite3Vdbe
22fba 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
22fbb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22fbc 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
22fbd 6d 6d 2c 20 69 4d 65 6d 2c 20 31 29 3b 0a 0a 20  mm, iMem, 1);.. 
22fbe 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
22fbf 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
22fc0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22fc1 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
22fc2 69 49 64 78 43 75 72 2c 20 69 2c 20 72 65 67 43  iIdxCur, i, regC
22fc3 6f 6c 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  ol);.#ifdef SQLI
22fc4 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a  TE_ENABLE_STAT2.
22fc5 20 20 20 20 20 20 69 66 28 20 69 3d 3d 30 20 29        if( i==0 )
22fc6 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68 65  {.        /* Che
22fc7 63 6b 20 69 66 20 74 68 65 20 72 65 63 6f 72 64  ck if the record
22fc8 20 74 68 61 74 20 63 75 72 73 6f 72 20 69 49 64   that cursor iId
22fc9 78 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20 63  xCur points to c
22fca 6f 6e 74 61 69 6e 73 20 61 0a 20 20 20 20 20 20  ontains a.      
22fcb 20 20 2a 2a 20 76 61 6c 75 65 20 74 68 61 74 20    ** value that 
22fcc 73 68 6f 75 6c 64 20 62 65 20 73 74 6f 72 65 64  should be stored
22fcd 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 73   in the sqlite_s
22fce 74 61 74 32 20 74 61 62 6c 65 2e 20 49 66 20 73  tat2 table. If s
22fcf 6f 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 74  o,.        ** st
22fd0 6f 72 65 20 69 74 2e 20 20 2a 2f 0a 20 20 20 20  ore it.  */.    
22fd1 20 20 20 20 69 6e 74 20 6e 65 20 3d 20 73 71 6c      int ne = sql
22fd2 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22fd3 2c 20 4f 50 5f 4e 65 2c 20 72 65 67 52 65 63 6e  , OP_Ne, regRecn
22fd4 6f 2c 20 30 2c 20 72 65 67 53 61 6d 70 6c 65 72  o, 0, regSampler
22fd5 65 63 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 61  ecno);.        a
22fd6 73 73 65 72 74 28 20 72 65 67 54 61 62 6e 61 6d  ssert( regTabnam
22fd7 65 2b 31 3d 3d 72 65 67 49 64 78 6e 61 6d 65 20  e+1==regIdxname 
22fd8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26  .             &&
22fd9 20 72 65 67 54 61 62 6e 61 6d 65 2b 32 3d 3d 72   regTabname+2==r
22fda 65 67 53 61 6d 70 6c 65 6e 6f 0a 20 20 20 20 20  egSampleno.     
22fdb 20 20 20 20 20 20 20 20 26 26 20 72 65 67 54 61          && regTa
22fdc 62 6e 61 6d 65 2b 33 3d 3d 72 65 67 43 6f 6c 0a  bname+3==regCol.
22fdd 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
22fde 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
22fdf 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45  angeP5(v, SQLITE
22fe0 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
22fe1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22fe2 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61  eAddOp4(v, OP_Ma
22fe3 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 54 61 62  keRecord, regTab
22fe4 6e 61 6d 65 2c 20 34 2c 20 72 65 67 52 65 63 2c  name, 4, regRec,
22fe5 20 22 61 61 61 62 22 2c 20 30 29 3b 0a 20 20 20   "aaab", 0);.   
22fe6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22fe7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
22fe8 52 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72 2b  Rowid, iStatCur+
22fe9 31 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  1, regRowid);.  
22fea 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22feb 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
22fec 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2b 31  sert, iStatCur+1
22fed 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77  , regRec, regRow
22fee 69 64 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  id);..        /*
22fef 20 43 61 6c 63 75 6c 61 74 65 20 6e 65 77 20 76   Calculate new v
22ff0 61 6c 75 65 73 20 66 6f 72 20 72 65 67 53 61 6d  alues for regSam
22ff1 70 6c 65 72 65 63 6e 6f 20 61 6e 64 20 72 65 67  plerecno and reg
22ff2 53 61 6d 70 6c 65 6e 6f 2e 0a 20 20 20 20 20 20  Sampleno..      
22ff3 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
22ff4 20 20 73 61 6d 70 6c 65 6e 6f 20 3d 20 73 61 6d    sampleno = sam
22ff5 70 6c 65 6e 6f 20 2b 20 31 0a 20 20 20 20 20 20  pleno + 1.      
22ff6 20 20 2a 2a 20 20 20 73 61 6d 70 6c 65 72 65 63    **   samplerec
22ff7 6e 6f 20 3d 20 73 61 6d 70 6c 65 72 65 63 6e 6f  no = samplerecno
22ff8 2b 28 72 65 6d 61 69 6e 69 6e 67 20 72 65 63 6f  +(remaining reco
22ff9 72 64 73 29 2f 28 72 65 6d 61 69 6e 69 6e 67 20  rds)/(remaining 
22ffa 73 61 6d 70 6c 65 73 29 0a 20 20 20 20 20 20 20  samples).       
22ffb 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
22ffc 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22ffd 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 53   OP_AddImm, regS
22ffe 61 6d 70 6c 65 6e 6f 2c 20 31 29 3b 0a 20 20 20  ampleno, 1);.   
22fff 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23000 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62  AddOp3(v, OP_Sub
23001 74 72 61 63 74 2c 20 72 65 67 52 65 63 6e 6f 2c  tract, regRecno,
23002 20 72 65 67 4c 61 73 74 2c 20 72 65 67 54 65 6d   regLast, regTem
23003 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  p);.        sqli
23004 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
23005 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 54   OP_AddImm, regT
23006 65 6d 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  emp, -1);.      
23007 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23008 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
23009 72 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  r, SQLITE_INDEX_
2300a 53 41 4d 50 4c 45 53 2c 20 72 65 67 54 65 6d 70  SAMPLES, regTemp
2300b 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  2);.        sqli
2300c 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2300d 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 72 65   OP_Subtract, re
2300e 67 53 61 6d 70 6c 65 6e 6f 2c 20 72 65 67 54 65  gSampleno, regTe
2300f 6d 70 32 2c 20 72 65 67 54 65 6d 70 32 29 3b 0a  mp2, regTemp2);.
23010 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
23011 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
23012 44 69 76 69 64 65 2c 20 72 65 67 54 65 6d 70 32  Divide, regTemp2
23013 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65  , regTemp, regTe
23014 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  mp);.        sql
23015 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
23016 2c 20 4f 50 5f 41 64 64 2c 20 72 65 67 53 61 6d  , OP_Add, regSam
23017 70 6c 65 72 65 63 6e 6f 2c 20 72 65 67 54 65 6d  plerecno, regTem
23018 70 2c 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e  p, regSamplerecn
23019 6f 29 3b 0a 0a 20 20 20 20 20 20 20 20 73 71 6c  o);..        sql
2301a 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2301b 28 76 2c 20 6e 65 29 3b 0a 20 20 20 20 20 20 20  (v, ne);.       
2301c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2301d 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
2301e 20 72 65 67 52 65 63 6e 6f 2c 20 31 29 3b 0a 20   regRecno, 1);. 
2301f 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20       }.#endif.. 
23020 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23021 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c  AddOp3(v, OP_Ne,
23022 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65 6d   regCol, 0, iMem
23023 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20  +nCol+i+1);.    
23024 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20 61    /**** TODO:  a
23025 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  dd collating seq
23026 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20 20  uence *****/.   
23027 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
23028 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45  angeP5(v, SQLITE
23029 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
2302a 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e    }.    if( db->
2302b 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2302c 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61        /* If a ma
2302d 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 68 61 73  lloc failure has
2302e 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20   occurred, then 
2302f 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
23030 65 20 65 78 70 72 65 73 73 69 6f 6e 20 0a 20 20  e expression .  
23031 20 20 20 20 2a 2a 20 70 61 73 73 65 64 20 61 73      ** passed as
23032 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
23033 6d 65 6e 74 20 74 6f 20 74 68 65 20 63 61 6c 6c  ment to the call
23034 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65 4a   to sqlite3VdbeJ
23035 75 6d 70 48 65 72 65 28 29 20 0a 20 20 20 20 20  umpHere() .     
23036 20 2a 2a 20 62 65 6c 6f 77 20 6d 61 79 20 62 65   ** below may be
23037 20 6e 65 67 61 74 69 76 65 2e 20 57 68 69 63 68   negative. Which
23038 20 63 61 75 73 65 73 20 61 6e 20 61 73 73 65 72   causes an asser
23039 74 28 29 20 74 6f 20 66 61 69 6c 20 28 6f 72 20  t() to fail (or 
2303a 61 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 75 74 2d  an.      ** out-
2303b 6f 66 2d 62 6f 75 6e 64 73 20 77 72 69 74 65 20  of-bounds write 
2303c 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  if SQLITE_DEBUG 
2303d 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2e  is not defined).
2303e 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72    */.      retur
2303f 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  n;.    }.    sql
23040 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
23041 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65 6e  , OP_Goto, 0, en
23042 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 66 6f  dOfLoop);.    fo
23043 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
23044 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
23045 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
23046 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  , sqlite3VdbeCur
23047 72 65 6e 74 41 64 64 72 28 76 29 2d 28 6e 43 6f  rentAddr(v)-(nCo
23048 6c 2a 32 29 29 3b 0a 20 20 20 20 20 20 73 71 6c  l*2));.      sql
23049 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2304a 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d 65  , OP_AddImm, iMe
2304b 6d 2b 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20  m+i+1, 1);.     
2304c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2304d 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
2304e 20 69 49 64 78 43 75 72 2c 20 69 2c 20 69 4d 65   iIdxCur, i, iMe
2304f 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20  m+nCol+i+1);.   
23050 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 6f   }..    /* End o
23051 66 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 6c  f the analysis l
23052 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69  oop. */.    sqli
23053 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
23054 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70  bel(v, endOfLoop
23055 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23056 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
23057 65 78 74 2c 20 69 49 64 78 43 75 72 2c 20 74 6f  ext, iIdxCur, to
23058 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73 71  pOfLoop);.    sq
23059 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2305a 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49 64  v, OP_Close, iId
2305b 78 43 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 53  xCur);..    /* S
2305c 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  tore the results
2305d 20 69 6e 20 73 71 6c 69 74 65 5f 73 74 61 74 31   in sqlite_stat1
2305e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
2305f 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20  The result is a 
23060 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68  single row of th
23061 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
23062 61 62 6c 65 2e 20 20 54 68 65 20 66 69 72 73 74  able.  The first
23063 0a 20 20 20 20 2a 2a 20 74 77 6f 20 63 6f 6c 75  .    ** two colu
23064 6d 6e 73 20 61 72 65 20 74 68 65 20 6e 61 6d 65  mns are the name
23065 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 61  s of the table a
23066 6e 64 20 69 6e 64 65 78 2e 20 20 54 68 65 20 74  nd index.  The t
23067 68 69 72 64 20 63 6f 6c 75 6d 6e 0a 20 20 20 20  hird column.    
23068 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 63  ** is a string c
23069 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69 73  omposed of a lis
2306a 74 20 6f 66 20 69 6e 74 65 67 65 72 20 73 74 61  t of integer sta
2306b 74 69 73 74 69 63 73 20 61 62 6f 75 74 20 74 68  tistics about th
2306c 65 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 2e 20  e.    ** index. 
2306d 20 54 68 65 20 66 69 72 73 74 20 69 6e 74 65 67   The first integ
2306e 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20 69  er in the list i
2306f 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  s the total numb
23070 65 72 20 6f 66 20 65 6e 74 72 69 65 73 0a 20 20  er of entries.  
23071 20 20 2a 2a 20 69 6e 20 74 68 65 20 69 6e 64 65    ** in the inde
23072 78 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65  x.  There is one
23073 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 74 65   additional inte
23074 67 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20  ger in the list 
23075 66 6f 72 20 65 61 63 68 0a 20 20 20 20 2a 2a 20  for each.    ** 
23076 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61  column of the ta
23077 62 6c 65 2e 20 20 54 68 69 73 20 61 64 64 69 74  ble.  This addit
23078 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20 69 73  ional integer is
23079 20 61 20 67 75 65 73 73 20 6f 66 20 68 6f 77 20   a guess of how 
2307a 6d 61 6e 79 0a 20 20 20 20 2a 2a 20 72 6f 77 73  many.    ** rows
2307b 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68   of the table th
2307c 65 20 69 6e 64 65 78 20 77 69 6c 6c 20 73 65 6c  e index will sel
2307d 65 63 74 2e 20 20 49 66 20 44 20 69 73 20 74 68  ect.  If D is th
2307e 65 20 63 6f 75 6e 74 20 6f 66 20 64 69 73 74 69  e count of disti
2307f 6e 63 74 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65  nct.    ** value
23080 73 20 61 6e 64 20 4b 20 69 73 20 74 68 65 20 74  s and K is the t
23081 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72  otal number of r
23082 6f 77 73 2c 20 74 68 65 6e 20 74 68 65 20 69 6e  ows, then the in
23083 74 65 67 65 72 20 69 73 20 63 6f 6d 70 75 74 65  teger is compute
23084 64 0a 20 20 20 20 2a 2a 20 61 73 3a 0a 20 20 20  d.    ** as:.   
23085 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
23086 20 20 49 20 3d 20 28 4b 2b 44 2d 31 29 2f 44 0a    I = (K+D-1)/D.
23087 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66      **.    ** If
23088 20 4b 3d 3d 30 20 74 68 65 6e 20 6e 6f 20 65 6e   K==0 then no en
23089 74 72 79 20 69 73 20 6d 61 64 65 20 69 6e 74 6f  try is made into
2308a 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
2308b 31 20 74 61 62 6c 65 2e 20 20 0a 20 20 20 20 2a  1 table.  .    *
2308c 2a 20 49 66 20 4b 3e 30 20 74 68 65 6e 20 69 74  * If K>0 then it
2308d 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 63   is always the c
2308e 61 73 65 20 74 68 65 20 44 3e 30 20 73 6f 20 64  ase the D>0 so d
2308f 69 76 69 73 69 6f 6e 20 62 79 20 7a 65 72 6f 0a  ivision by zero.
23090 20 20 20 20 2a 2a 20 69 73 20 6e 65 76 65 72 20      ** is never 
23091 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20 20 2a 2f  possible..    */
23092 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
23093 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
23094 20 4f 50 5f 49 66 4e 6f 74 2c 20 69 4d 65 6d 29   OP_IfNot, iMem)
23095 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23096 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
23097 6f 70 79 2c 20 69 4d 65 6d 2c 20 72 65 67 53 61  opy, iMem, regSa
23098 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20 20 66 6f 72  mpleno);.    for
23099 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
2309a 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
2309b 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2309c 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65  P_String8, 0, re
2309d 67 54 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20 30  gTemp, 0, " ", 0
2309e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2309f 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
230a0 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70  _Concat, regTemp
230a1 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 72  , regSampleno, r
230a2 65 67 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20  egSampleno);.   
230a3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
230a4 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c 20  dOp3(v, OP_Add, 
230a5 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20  iMem, iMem+i+1, 
230a6 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20  regTemp);.      
230a7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
230a8 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
230a9 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b 0a 20 20  regTemp, -1);.  
230aa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
230ab 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 69 76 69  ddOp3(v, OP_Divi
230ac 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 72 65  de, iMem+i+1, re
230ad 67 54 65 6d 70 2c 20 72 65 67 54 65 6d 70 29 3b  gTemp, regTemp);
230ae 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
230af 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 54  beAddOp1(v, OP_T
230b0 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70 29 3b 0a  oInt, regTemp);.
230b1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
230b2 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
230b3 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c 20 72  ncat, regTemp, r
230b4 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 72 65 67 53  egSampleno, regS
230b5 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20 20 7d 0a  ampleno);.    }.
230b6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
230b7 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp4(v, OP_Make
230b8 52 65 63 6f 72 64 2c 20 72 65 67 54 61 62 6e 61  Record, regTabna
230b9 6d 65 2c 20 33 2c 20 72 65 67 52 65 63 2c 20 22  me, 3, regRec, "
230ba 61 61 61 22 2c 20 30 29 3b 0a 20 20 20 20 73 71  aaa", 0);.    sq
230bb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
230bc 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
230bd 69 53 74 61 74 43 75 72 2c 20 72 65 67 52 6f 77  iStatCur, regRow
230be 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
230bf 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
230c0 5f 49 6e 73 65 72 74 2c 20 69 53 74 61 74 43 75  _Insert, iStatCu
230c1 72 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52 6f  r, regRec, regRo
230c2 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  wid);.    sqlite
230c3 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
230c4 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b   OPFLAG_APPEND);
230c5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
230c6 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
230c7 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
230c8 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
230c9 61 74 20 77 69 6c 6c 20 63 61 75 73 65 20 74 68  at will cause th
230ca 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e  e most recent in
230cb 64 65 78 20 61 6e 61 6c 79 73 69 73 20 74 6f 0a  dex analysis to.
230cc 2a 2a 20 62 65 20 6c 61 6f 64 65 64 20 69 6e 74  ** be laoded int
230cd 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20  o internal hash 
230ce 74 61 62 6c 65 73 20 77 68 65 72 65 20 69 73 20  tables where is 
230cf 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a  can be used..*/.
230d0 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61 64  static void load
230d1 41 6e 61 6c 79 73 69 73 28 50 61 72 73 65 20 2a  Analysis(Parse *
230d2 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29  pParse, int iDb)
230d3 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71  {.  Vdbe *v = sq
230d4 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
230d5 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b  rse);.  if( v ){
230d6 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
230d7 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4c 6f 61  AddOp1(v, OP_Loa
230d8 64 41 6e 61 6c 79 73 69 73 2c 20 69 44 62 29 3b  dAnalysis, iDb);
230d9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
230da 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
230db 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e 61 6c   will do an anal
230dc 79 73 69 73 20 6f 66 20 61 6e 20 65 6e 74 69 72  ysis of an entir
230dd 65 20 64 61 74 61 62 61 73 65 0a 2a 2f 0a 73 74  e database.*/.st
230de 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c 79 7a  atic void analyz
230df 65 44 61 74 61 62 61 73 65 28 50 61 72 73 65 20  eDatabase(Parse 
230e0 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62  *pParse, int iDb
230e1 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
230e2 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
230e3 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
230e4 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
230e5 70 53 63 68 65 6d 61 3b 20 20 20 20 2f 2a 20 53  pSchema;    /* S
230e6 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61 73  chema of databas
230e7 65 20 69 44 62 20 2a 2f 0a 20 20 48 61 73 68 45  e iDb */.  HashE
230e8 6c 65 6d 20 2a 6b 3b 0a 20 20 69 6e 74 20 69 53  lem *k;.  int iS
230e9 74 61 74 43 75 72 3b 0a 20 20 69 6e 74 20 69 4d  tatCur;.  int iM
230ea 65 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 65  em;..  sqlite3Be
230eb 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
230ec 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
230ed 29 3b 0a 20 20 69 53 74 61 74 43 75 72 20 3d 20  );.  iStatCur = 
230ee 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20  pParse->nTab;.  
230ef 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 2b 3d 20  pParse->nTab += 
230f0 32 3b 0a 20 20 6f 70 65 6e 53 74 61 74 54 61 62  2;.  openStatTab
230f1 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  le(pParse, iDb, 
230f2 69 53 74 61 74 43 75 72 2c 20 30 29 3b 0a 20 20  iStatCur, 0);.  
230f3 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iMem = pParse->n
230f4 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28 6b 3d 73  Mem+1;.  for(k=s
230f5 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
230f6 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
230f7 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 48 61  ); k; k=sqliteHa
230f8 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20 20 20  shNext(k)){.    
230f9 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 28 54  Table *pTab = (T
230fa 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73 68  able*)sqliteHash
230fb 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 61 6e 61  Data(k);.    ana
230fc 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50 61  lyzeOneTable(pPa
230fd 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61 74  rse, pTab, iStat
230fe 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 0a  Cur, iMem);.  }.
230ff 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 70    loadAnalysis(p
23100 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a 0a  Parse, iDb);.}..
23101 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
23102 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 64 6f  ode that will do
23103 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20   an analysis of 
23104 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69  a single table i
23105 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 2e  n.** a database.
23106 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
23107 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 50 61 72  analyzeTable(Par
23108 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
23109 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74 20  e *pTab){.  int 
2310a 69 44 62 3b 0a 20 20 69 6e 74 20 69 53 74 61 74  iDb;.  int iStat
2310b 43 75 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  Cur;..  assert( 
2310c 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 61 73 73  pTab!=0 );.  ass
2310d 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
2310e 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
2310f 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a  (pParse->db) );.
23110 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
23111 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
23112 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70  rse->db, pTab->p
23113 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74  Schema);.  sqlit
23114 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
23115 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c  ation(pParse, 0,
23116 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 43 75   iDb);.  iStatCu
23117 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
23118 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  ;.  pParse->nTab
23119 20 2b 3d 20 32 3b 0a 20 20 6f 70 65 6e 53 74 61   += 2;.  openSta
2311a 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  tTable(pParse, i
2311b 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 54  Db, iStatCur, pT
2311c 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61 6e  ab->zName);.  an
2311d 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50  alyzeOneTable(pP
2311e 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61  arse, pTab, iSta
2311f 74 43 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d  tCur, pParse->nM
23120 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e 61  em+1);.  loadAna
23121 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20 69 44  lysis(pParse, iD
23122 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  b);.}../*.** Gen
23123 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74  erate code for t
23124 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  he ANALYZE comma
23125 6e 64 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  nd.  The parser 
23126 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
23127 6e 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72 65  ne.** when it re
23128 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41 4c  cognizes an ANAL
23129 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a  YZE command..**.
2312a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a  **        ANALYZ
2312b 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
2312c 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20               -- 
2312d 31 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c  1.**        ANAL
2312e 59 5a 45 20 20 3c 64 61 74 61 62 61 73 65 3e 20  YZE  <database> 
2312f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
23130 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e  - 2.**        AN
23131 41 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 61 73  ALYZE  ?<databas
23132 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20  e>.?<tablename> 
23133 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d   -- 3.**.** Form
23134 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e   1 causes all in
23135 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74  dices in all att
23136 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20  ached databases 
23137 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 0a  to be analyzed..
23138 2a 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79 7a  ** Form 2 analyz
23139 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 74  es all indices t
2313a 68 65 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61  he single databa
2313b 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f 72  se named..** For
2313c 6d 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c  m 3 analyzes all
2313d 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61   indices associa
2313e 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d  ted with the nam
2313f 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  ed table..*/.SQL
23140 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
23141 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28   sqlite3Analyze(
23142 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
23143 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f  oken *pName1, To
23144 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20  ken *pName2){.  
23145 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
23146 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
23147 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  iDb;.  int i;.  
23148 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a 20  char *z, *zDb;. 
23149 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
2314a 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d  Token *pTableNam
2314b 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68  e;..  /* Read th
2314c 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
2314d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  a. If an error o
2314e 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20  ccurs, leave an 
2314f 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20  error message.  
23150 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70  ** and code in p
23151 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e  Parse and return
23152 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65   NULL. */.  asse
23153 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  rt( sqlite3Btree
23154 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
23155 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20  pParse->db) );. 
23156 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
23157 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
23158 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  a(pParse) ){.   
23159 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
2315a 61 73 73 65 72 74 28 20 70 4e 61 6d 65 32 21 3d  assert( pName2!=
2315b 30 20 7c 7c 20 70 4e 61 6d 65 31 3d 3d 30 20 29  0 || pName1==0 )
2315c 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d  ;.  if( pName1==
2315d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d  0 ){.    /* Form
2315e 20 31 3a 20 20 41 6e 61 6c 79 7a 65 20 65 76 65   1:  Analyze eve
2315f 72 79 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 66  rything */.    f
23160 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
23161 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  b; i++){.      i
23162 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e  f( i==1 ) contin
23163 75 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 61  ue;  /* Do not a
23164 6e 61 6c 79 7a 65 20 74 68 65 20 54 45 4d 50 20  nalyze the TEMP 
23165 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20  database */.    
23166 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62 61 73    analyzeDatabas
23167 65 28 70 50 61 72 73 65 2c 20 69 29 3b 0a 20 20  e(pParse, i);.  
23168 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
23169 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 29 7b 0a  pName2->n==0 ){.
2316a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32 3a 20 20      /* Form 2:  
2316b 41 6e 61 6c 79 7a 65 20 74 68 65 20 64 61 74 61  Analyze the data
2316c 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6e 61  base or table na
2316d 6d 65 64 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d  med */.    iDb =
2316e 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 64   sqlite3FindDb(d
2316f 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20  b, pName1);.    
23170 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20  if( iDb>=0 ){.  
23171 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62      analyzeDatab
23172 61 73 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  ase(pParse, iDb)
23173 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
23174 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61     z = sqlite3Na
23175 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
23176 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 20 69  pName1);.      i
23177 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  f( z ){.        
23178 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f  pTab = sqlite3Lo
23179 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
2317a 2c 20 30 2c 20 7a 2c 20 30 29 3b 0a 20 20 20 20  , 0, z, 0);.    
2317b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2317c 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20  e(db, z);.      
2317d 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
2317e 20 20 20 20 20 20 20 20 61 6e 61 6c 79 7a 65 54          analyzeT
2317f 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61  able(pParse, pTa
23180 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  b);.        }.  
23181 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
23182 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d  lse{.    /* Form
23183 20 33 3a 20 41 6e 61 6c 79 7a 65 20 74 68 65 20   3: Analyze the 
23184 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 20  fully qualified 
23185 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  table name */.  
23186 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 54    iDb = sqlite3T
23187 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73  woPartName(pPars
23188 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65  e, pName1, pName
23189 32 2c 20 26 70 54 61 62 6c 65 4e 61 6d 65 29 3b  2, &pTableName);
2318a 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d 30 20  .    if( iDb>=0 
2318b 29 7b 0a 20 20 20 20 20 20 7a 44 62 20 3d 20 64  ){.      zDb = d
2318c 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2318d 65 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c  e;.      z = sql
2318e 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
2318f 6e 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d 65  n(db, pTableName
23190 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29  );.      if( z )
23191 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d  {.        pTab =
23192 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
23193 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a  ble(pParse, 0, z
23194 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 20  , zDb);.        
23195 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
23196 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , z);.        if
23197 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20  ( pTab ){.      
23198 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65      analyzeTable
23199 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
2319a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2319b 7d 0a 20 20 20 20 7d 20 20 20 0a 20 20 7d 0a 7d  }.    }   .  }.}
2319c 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 64 20 74 6f 20  ../*.** Used to 
2319d 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  pass information
2319e 20 66 72 6f 6d 20 74 68 65 20 61 6e 61 6c 79 7a   from the analyz
2319f 65 72 20 72 65 61 64 65 72 20 74 68 72 6f 75 67  er reader throug
231a0 68 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c 6c  h to the.** call
231a1 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f  back routine..*/
231a2 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
231a3 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 61 6e 61  analysisInfo ana
231a4 6c 79 73 69 73 49 6e 66 6f 3b 0a 73 74 72 75 63  lysisInfo;.struc
231a5 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 7b  t analysisInfo {
231a6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
231a7 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
231a8 61 74 61 62 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a  atabase;.};../*.
231a9 2a 2a 20 54 68 69 73 20 63 61 6c 6c 62 61 63 6b  ** This callback
231aa 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65   is invoked once
231ab 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20   for each index 
231ac 77 68 65 6e 20 72 65 61 64 69 6e 67 20 74 68 65  when reading the
231ad 0a 2a 2a 20 73 71 6c 69 74 65 5f 73 74 61 74 31  .** sqlite_stat1
231ae 20 74 61 62 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a 20   table.  .**.** 
231af 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61      argv[0] = na
231b0 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 0a  me of the index.
231b1 2a 2a 20 20 20 20 20 61 72 67 76 5b 31 5d 20 3d  **     argv[1] =
231b2 20 72 65 73 75 6c 74 73 20 6f 66 20 61 6e 61 6c   results of anal
231b3 79 73 69 73 20 2d 20 6f 6e 20 69 6e 74 65 67 65  ysis - on intege
231b4 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  r for each colum
231b5 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n.*/.static int 
231b6 61 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 28 76  analysisLoader(v
231b7 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74 20  oid *pData, int 
231b8 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67  argc, char **arg
231b9 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74 55 73 65  v, char **NotUse
231ba 64 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49 6e  d){.  analysisIn
231bb 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 28 61 6e 61  fo *pInfo = (ana
231bc 6c 79 73 69 73 49 6e 66 6f 2a 29 70 44 61 74 61  lysisInfo*)pData
231bd 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65  ;.  Index *pInde
231be 78 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20  x;.  int i, c;. 
231bf 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 76 3b   unsigned int v;
231c0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
231c1 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ;..  assert( arg
231c2 63 3d 3d 32 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==2 );.  UNUSED
231c3 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
231c4 73 65 64 2c 20 61 72 67 63 29 3b 0a 0a 20 20 69  sed, argc);..  i
231c5 66 28 20 61 72 67 76 3d 3d 30 20 7c 7c 20 61 72  f( argv==0 || ar
231c6 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20 61 72 67 76  gv[0]==0 || argv
231c7 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  [1]==0 ){.    re
231c8 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49  turn 0;.  }.  pI
231c9 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69  ndex = sqlite3Fi
231ca 6e 64 49 6e 64 65 78 28 70 49 6e 66 6f 2d 3e 64  ndIndex(pInfo->d
231cb 62 2c 20 61 72 67 76 5b 30 5d 2c 20 70 49 6e 66  b, argv[0], pInf
231cc 6f 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  o->zDatabase);. 
231cd 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29   if( pIndex==0 )
231ce 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
231cf 20 20 7d 0a 20 20 7a 20 3d 20 61 72 67 76 5b 31    }.  z = argv[1
231d0 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 2a 7a  ];.  for(i=0; *z
231d1 20 26 26 20 69 3c 3d 70 49 6e 64 65 78 2d 3e 6e   && i<=pIndex->n
231d2 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
231d3 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 77 68 69    v = 0;.    whi
231d4 6c 65 28 20 28 63 3d 7a 5b 30 5d 29 3e 3d 27 30  le( (c=z[0])>='0
231d5 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20  ' && c<='9' ){. 
231d6 20 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20       v = v*10 + 
231d7 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a  c - '0';.      z
231d8 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49  ++;.    }.    pI
231d9 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 5b 69  ndex->aiRowEst[i
231da 5d 20 3d 20 76 3b 0a 20 20 20 20 69 66 28 20 2a  ] = v;.    if( *
231db 7a 3d 3d 27 20 27 20 29 20 7a 2b 2b 3b 0a 20 20  z==' ' ) z++;.  
231dc 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
231dd 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 49 6e  ./*.** If the In
231de 64 65 78 2e 61 53 61 6d 70 6c 65 20 76 61 72 69  dex.aSample vari
231df 61 62 6c 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  able is not NULL
231e0 2c 20 64 65 6c 65 74 65 20 74 68 65 20 61 53 61  , delete the aSa
231e1 6d 70 6c 65 5b 5d 20 61 72 72 61 79 0a 2a 2a 20  mple[] array.** 
231e2 61 6e 64 20 69 74 73 20 63 6f 6e 74 65 6e 74 73  and its contents
231e3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
231e4 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
231e5 44 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70 6c  DeleteIndexSampl
231e6 65 73 28 49 6e 64 65 78 20 2a 70 49 64 78 29 7b  es(Index *pIdx){
231e7 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
231e8 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20 20 69 66  NABLE_STAT2.  if
231e9 28 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 20  ( pIdx->aSample 
231ea 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20  ){.    int j;.  
231eb 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4d 65 6d    sqlite3 *dbMem
231ec 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d   = pIdx->pTable-
231ed 3e 64 62 4d 65 6d 3b 0a 20 20 20 20 66 6f 72 28  >dbMem;.    for(
231ee 6a 3d 30 3b 20 6a 3c 53 51 4c 49 54 45 5f 49 4e  j=0; j<SQLITE_IN
231ef 44 45 58 5f 53 41 4d 50 4c 45 53 3b 20 6a 2b 2b  DEX_SAMPLES; j++
231f0 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 53 61  ){.      IndexSa
231f1 6d 70 6c 65 20 2a 70 20 3d 20 26 70 49 64 78 2d  mple *p = &pIdx-
231f2 3e 61 53 61 6d 70 6c 65 5b 6a 5d 3b 0a 20 20 20  >aSample[j];.   
231f3 20 20 20 69 66 28 20 70 2d 3e 65 54 79 70 65 3d     if( p->eType=
231f4 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c 20  =SQLITE_TEXT || 
231f5 70 2d 3e 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  p->eType==SQLITE
231f6 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20 20  _BLOB ){.       
231f7 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
231f8 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 4d  Idx->pTable->dbM
231f9 65 6d 2c 20 70 2d 3e 75 2e 7a 29 3b 0a 20 20 20  em, p->u.z);.   
231fa 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
231fb 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 4d  qlite3DbFree(dbM
231fc 65 6d 2c 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c  em, pIdx->aSampl
231fd 65 29 3b 0a 20 20 20 20 70 49 64 78 2d 3e 61 53  e);.    pIdx->aS
231fe 61 6d 70 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a 23  ample = 0;.  }.#
231ff 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41  else.  UNUSED_PA
23200 52 41 4d 45 54 45 52 28 70 49 64 78 29 3b 0a 23  RAMETER(pIdx);.#
23201 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  endif.}../*.** L
23202 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  oad the content 
23203 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  of the sqlite_st
23204 61 74 31 20 61 6e 64 20 73 71 6c 69 74 65 5f 73  at1 and sqlite_s
23205 74 61 74 32 20 74 61 62 6c 65 73 2e 20 54 68 65  tat2 tables. The
23206 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  .** contents of 
23207 73 71 6c 69 74 65 5f 73 74 61 74 31 20 61 72 65  sqlite_stat1 are
23208 20 75 73 65 64 20 74 6f 20 70 6f 70 75 6c 61 74   used to populat
23209 65 20 74 68 65 20 49 6e 64 65 78 2e 61 69 52 6f  e the Index.aiRo
2320a 77 45 73 74 5b 5d 0a 2a 2a 20 61 72 72 61 79 73  wEst[].** arrays
2320b 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  . The contents o
2320c 66 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 61  f sqlite_stat2 a
2320d 72 65 20 75 73 65 64 20 74 6f 20 70 6f 70 75 6c  re used to popul
2320e 61 74 65 20 74 68 65 0a 2a 2a 20 49 6e 64 65 78  ate the.** Index
2320f 2e 61 53 61 6d 70 6c 65 5b 5d 20 61 72 72 61 79  .aSample[] array
23210 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  s..**.** If the 
23211 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
23212 6c 65 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e  le is not presen
23213 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  t in the databas
23214 65 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 0a  e, SQLITE_ERROR.
23215 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  ** is returned. 
23216 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 65 76  In this case, ev
23217 65 6e 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  en if SQLITE_ENA
23218 42 4c 45 5f 53 54 41 54 32 20 77 61 73 20 64 65  BLE_STAT2 was de
23219 66 69 6e 65 64 20 0a 2a 2a 20 64 75 72 69 6e 67  fined .** during
2321a 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e 64   compilation and
2321b 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
2321c 32 20 74 61 62 6c 65 20 69 73 20 70 72 65 73 65  2 table is prese
2321d 6e 74 2c 20 6e 6f 20 64 61 74 61 20 69 73 20 0a  nt, no data is .
2321e 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 69 74 2e  ** read from it.
2321f 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  .**.** If SQLITE
23220 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 77 61  _ENABLE_STAT2 wa
23221 73 20 64 65 66 69 6e 65 64 20 64 75 72 69 6e 67  s defined during
23222 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e 64   compilation and
23223 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 5f   the .** sqlite_
23224 73 74 61 74 32 20 74 61 62 6c 65 20 69 73 20 6e  stat2 table is n
23225 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68  ot present in th
23226 65 20 64 61 74 61 62 61 73 65 2c 20 53 51 4c 49  e database, SQLI
23227 54 45 5f 45 52 52 4f 52 20 69 73 0a 2a 2a 20 72  TE_ERROR is.** r
23228 65 74 75 72 6e 65 64 2e 20 48 6f 77 65 76 65 72  eturned. However
23229 2c 20 69 6e 20 74 68 69 73 20 63 61 73 65 2c 20  , in this case, 
2322a 64 61 74 61 20 69 73 20 72 65 61 64 20 66 72 6f  data is read fro
2322b 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  m the sqlite_sta
2322c 74 31 0a 2a 2a 20 74 61 62 6c 65 20 28 69 66 20  t1.** table (if 
2322d 69 74 20 69 73 20 70 72 65 73 65 6e 74 29 20 62  it is present) b
2322e 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
2322f 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f 4d  .**.** If an OOM
23230 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74   error occurs, t
23231 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77  his function alw
23232 61 79 73 20 73 65 74 73 20 64 62 2d 3e 6d 61 6c  ays sets db->mal
23233 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a 2a 20 54 68  locFailed..** Th
23234 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68 65 20  is means if the 
23235 63 61 6c 6c 65 72 20 64 6f 65 73 20 6e 6f 74 20  caller does not 
23236 63 61 72 65 20 61 62 6f 75 74 20 6f 74 68 65 72  care about other
23237 20 65 72 72 6f 72 73 2c 20 74 68 65 20 72 65 74   errors, the ret
23238 75 72 6e 0a 2a 2a 20 63 6f 64 65 20 6d 61 79 20  urn.** code may 
23239 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a 53  be ignored..*/.S
2323a 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2323b 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69  t sqlite3Analysi
2323c 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 20 2a 64  sLoad(sqlite3 *d
2323d 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 61  b, int iDb){.  a
2323e 6e 61 6c 79 73 69 73 49 6e 66 6f 20 73 49 6e 66  nalysisInfo sInf
2323f 6f 3b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 69  o;.  HashElem *i
23240 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a  ;.  char *zSql;.
23241 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
23242 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
23243 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
23244 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b  assert( db->aDb[
23245 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20  iDb].pBt!=0 );. 
23246 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
23247 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
23248 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
23249 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 72  ) );..  /* Clear
2324a 20 61 6e 79 20 70 72 69 6f 72 20 73 74 61 74 69   any prior stati
2324b 73 74 69 63 73 20 2a 2f 0a 20 20 66 6f 72 28 69  stics */.  for(i
2324c 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
2324d 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  (&db->aDb[iDb].p
2324e 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29  Schema->idxHash)
2324f 3b 69 3b 69 3d 73 71 6c 69 74 65 48 61 73 68 4e  ;i;i=sqliteHashN
23250 65 78 74 28 69 29 29 7b 0a 20 20 20 20 49 6e 64  ext(i)){.    Ind
23251 65 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74  ex *pIdx = sqlit
23252 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20  eHashData(i);.  
23253 20 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74    sqlite3Default
23254 52 6f 77 45 73 74 28 70 49 64 78 29 3b 0a 20 20  RowEst(pIdx);.  
23255 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49    sqlite3DeleteI
23256 6e 64 65 78 53 61 6d 70 6c 65 73 28 70 49 64 78  ndexSamples(pIdx
23257 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65  );.  }..  /* Che
23258 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
23259 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
2325a 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 2a 2f   table exists */
2325b 0a 20 20 73 49 6e 66 6f 2e 64 62 20 3d 20 64 62  .  sInfo.db = db
2325c 3b 0a 20 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62  ;.  sInfo.zDatab
2325d 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  ase = db->aDb[iD
2325e 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20  b].zName;.  if( 
2325f 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
23260 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61  (db, "sqlite_sta
23261 74 31 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61  t1", sInfo.zData
23262 62 61 73 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  base)==0 ){.    
23263 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
23264 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c  ROR;.  }..  /* L
23265 6f 61 64 20 6e 65 77 20 73 74 61 74 69 73 74 69  oad new statisti
23266 63 73 20 6f 75 74 20 6f 66 20 74 68 65 20 73 71  cs out of the sq
23267 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
23268 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c   */.  zSql = sql
23269 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2326a 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 69  .      "SELECT i
2326b 64 78 2c 20 73 74 61 74 20 46 52 4f 4d 20 25 51  dx, stat FROM %Q
2326c 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20  .sqlite_stat1", 
2326d 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 29  sInfo.zDatabase)
2326e 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30 20  ;.  if( zSql==0 
2326f 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
23270 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73  TE_NOMEM;.  }els
23271 65 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c  e{.    (void)sql
23272 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
23273 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
23274 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71  te3_exec(db, zSq
23275 6c 2c 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64 65  l, analysisLoade
23276 72 2c 20 26 73 49 6e 66 6f 2c 20 30 29 3b 0a 20  r, &sInfo, 0);. 
23277 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
23278 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
23279 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2327a 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a  db, zSql);.  }..
2327b 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 73  .  /* Load the s
2327c 74 61 74 69 73 74 69 63 73 20 66 72 6f 6d 20 74  tatistics from t
2327d 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20  he sqlite_stat2 
2327e 74 61 62 6c 65 2e 20 2a 2f 0a 23 69 66 64 65 66  table. */.#ifdef
2327f 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53   SQLITE_ENABLE_S
23280 54 41 54 32 0a 20 20 69 66 28 20 72 63 3d 3d 53  TAT2.  if( rc==S
23281 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 73 71 6c  QLITE_OK && !sql
23282 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
23283 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 32 22  , "sqlite_stat2"
23284 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73  , sInfo.zDatabas
23285 65 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  e) ){.    rc = S
23286 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
23287 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
23288 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
23289 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20  te3_stmt *pStmt 
2328a 3d 20 30 3b 0a 0a 20 20 20 20 7a 53 71 6c 20 3d  = 0;..    zSql =
2328b 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2328c 64 62 2c 20 0a 20 20 20 20 20 20 20 20 22 53 45  db, .        "SE
2328d 4c 45 43 54 20 69 64 78 2c 73 61 6d 70 6c 65 6e  LECT idx,samplen
2328e 6f 2c 73 61 6d 70 6c 65 20 46 52 4f 4d 20 25 51  o,sample FROM %Q
2328f 2e 73 71 6c 69 74 65 5f 73 74 61 74 32 22 2c 20  .sqlite_stat2", 
23290 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65 29  sInfo.zDatabase)
23291 3b 0a 20 20 20 20 69 66 28 20 21 7a 53 71 6c 20  ;.    if( !zSql 
23292 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
23293 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
23294 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 76 6f  }else{.      (vo
23295 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
23296 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 72  Off(db);.      r
23297 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70  c = sqlite3_prep
23298 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31  are(db, zSql, -1
23299 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  , &pStmt, 0);.  
2329a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
2329b 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
2329c 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2329d 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20  ee(db, zSql);.  
2329e 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d    }..    if( rc=
2329f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
232a0 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
232a1 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
232a2 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71 6c        while( sql
232a3 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29  ite3_step(pStmt)
232a4 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
232a5 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 49          char *zI
232a6 6e 64 65 78 20 3d 20 28 63 68 61 72 20 2a 29 73  ndex = (char *)s
232a7 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
232a8 78 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  xt(pStmt, 0);.  
232a9 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64        Index *pId
232aa 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  x = sqlite3FindI
232ab 6e 64 65 78 28 64 62 2c 20 7a 49 6e 64 65 78 2c  ndex(db, zIndex,
232ac 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65   sInfo.zDatabase
232ad 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
232ae 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Idx ){.         
232af 20 69 6e 74 20 69 53 61 6d 70 6c 65 20 3d 20 73   int iSample = s
232b0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
232b1 74 28 70 53 74 6d 74 2c 20 31 29 3b 0a 20 20 20  t(pStmt, 1);.   
232b2 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a         sqlite3 *
232b3 64 62 4d 65 6d 20 3d 20 70 49 64 78 2d 3e 70 54  dbMem = pIdx->pT
232b4 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 20  able->dbMem;.   
232b5 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64         assert( d
232b6 62 4d 65 6d 3d 3d 64 62 20 7c 7c 20 64 62 4d 65  bMem==db || dbMe
232b7 6d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  m==0 );.        
232b8 20 20 69 66 28 20 69 53 61 6d 70 6c 65 3c 53 51    if( iSample<SQ
232b9 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c  LITE_INDEX_SAMPL
232ba 45 53 20 26 26 20 69 53 61 6d 70 6c 65 3e 3d 30  ES && iSample>=0
232bb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
232bc 69 6e 74 20 65 54 79 70 65 20 3d 20 73 71 6c 69  int eType = sqli
232bd 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
232be 70 53 74 6d 74 2c 20 32 29 3b 0a 0a 20 20 20 20  pStmt, 2);..    
232bf 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
232c0 2d 3e 61 53 61 6d 70 6c 65 3d 3d 30 20 29 7b 0a  ->aSample==0 ){.
232c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74                st
232c2 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 73  atic const int s
232c3 7a 20 3d 20 73 69 7a 65 6f 66 28 49 6e 64 65 78  z = sizeof(Index
232c4 53 61 6d 70 6c 65 29 2a 53 51 4c 49 54 45 5f 49  Sample)*SQLITE_I
232c5 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 0a 20 20  NDEX_SAMPLES;.  
232c6 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64 78              pIdx
232c7 2d 3e 61 53 61 6d 70 6c 65 20 3d 20 28 49 6e 64  ->aSample = (Ind
232c8 65 78 53 61 6d 70 6c 65 20 2a 29 73 71 6c 69 74  exSample *)sqlit
232c9 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
232ca 62 4d 65 6d 2c 20 73 7a 29 3b 0a 20 20 20 20 20  bMem, sz);.     
232cb 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64           if( pId
232cc 78 2d 3e 61 53 61 6d 70 6c 65 3d 3d 30 20 29 7b  x->aSample==0 ){
232cd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
232ce 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
232cf 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  d = 1;.         
232d0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
232d1 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
232d2 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20            }..   
232d3 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
232d4 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 20 29   pIdx->aSample )
232d5 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 0a  ;.            {.
232d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49 6e                In
232d7 64 65 78 53 61 6d 70 6c 65 20 2a 70 53 61 6d 70  dexSample *pSamp
232d8 6c 65 20 3d 20 26 70 49 64 78 2d 3e 61 53 61 6d  le = &pIdx->aSam
232d9 70 6c 65 5b 69 53 61 6d 70 6c 65 5d 3b 0a 20 20  ple[iSample];.  
232da 20 20 20 20 20 20 20 20 20 20 20 20 70 53 61 6d              pSam
232db 70 6c 65 2d 3e 65 54 79 70 65 20 3d 20 28 75 38  ple->eType = (u8
232dc 29 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20  )eType;.        
232dd 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65 3d        if( eType=
232de 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20  =SQLITE_INTEGER 
232df 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  || eType==SQLITE
232e0 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20  _FLOAT ){.      
232e1 20 20 20 20 20 20 20 20 20 20 70 53 61 6d 70 6c            pSampl
232e2 65 2d 3e 75 2e 72 20 3d 20 73 71 6c 69 74 65 33  e->u.r = sqlite3
232e3 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 70  _column_double(p
232e4 53 74 6d 74 2c 20 32 29 3b 0a 20 20 20 20 20 20  Stmt, 2);.      
232e5 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
232e6 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  ( eType==SQLITE_
232e7 54 45 58 54 20 7c 7c 20 65 54 79 70 65 3d 3d 53  TEXT || eType==S
232e8 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20  QLITE_BLOB ){.  
232e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
232ea 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63  nst char *z = (c
232eb 6f 6e 73 74 20 63 68 61 72 20 2a 29 28 0a 20 20  onst char *)(.  
232ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
232ed 20 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54 45    (eType==SQLITE
232ee 5f 42 4c 4f 42 29 20 3f 0a 20 20 20 20 20 20 20  _BLOB) ?.       
232ef 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
232f0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
232f1 28 70 53 74 6d 74 2c 20 32 29 3a 0a 20 20 20 20  (pStmt, 2):.    
232f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
232f3 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
232f4 65 78 74 28 70 53 74 6d 74 2c 20 32 29 0a 20 20  ext(pStmt, 2).  
232f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 3b                );
232f6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
232f7 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33   int n = sqlite3
232f8 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53  _column_bytes(pS
232f9 74 6d 74 2c 20 32 29 3b 0a 20 20 20 20 20 20 20  tmt, 2);.       
232fa 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e 32           if( n>2
232fb 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  4 ){.           
232fc 20 20 20 20 20 20 20 6e 20 3d 20 32 34 3b 0a 20         n = 24;. 
232fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
232fe 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
232ff 20 70 53 61 6d 70 6c 65 2d 3e 6e 42 79 74 65 20   pSample->nByte 
23300 3d 20 28 75 38 29 6e 3b 0a 20 20 20 20 20 20 20  = (u8)n;.       
23301 20 20 20 20 20 20 20 20 20 70 53 61 6d 70 6c 65           pSample
23302 2d 3e 75 2e 7a 20 3d 20 73 71 6c 69 74 65 33 44  ->u.z = sqlite3D
23303 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 4d 65 6d  bMallocRaw(dbMem
23304 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , n);.          
23305 20 20 20 20 20 20 69 66 28 20 70 53 61 6d 70 6c        if( pSampl
23306 65 2d 3e 75 2e 7a 20 29 7b 0a 20 20 20 20 20 20  e->u.z ){.      
23307 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63              memc
23308 70 79 28 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a 2c  py(pSample->u.z,
23309 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20   z, n);.        
2330a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2330b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2330c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2330d 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  d = 1;.         
2330e 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2330f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23310 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
23311 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  }.            }.
23312 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
23313 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
23314 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
23315 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
23316 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73 71  ;.      (void)sq
23317 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
23318 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  );.    }.  }.#en
23319 64 69 66 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  dif..  if( rc==S
2331a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
2331b 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
2331c 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  led = 1;.  }.  r
2331d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65  eturn rc;.}...#e
2331e 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2331f 4d 49 54 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a  MIT_ANALYZE */..
23320 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
23321 45 6e 64 20 6f 66 20 61 6e 61 6c 79 7a 65 2e 63  End of analyze.c
23322 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
23323 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23324 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
23325 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
23326 42 65 67 69 6e 20 66 69 6c 65 20 61 74 74 61 63  Begin file attac
23327 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.c ************
23328 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23329 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2332a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c  /*.** 2003 April
2332b 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   6.**.** The aut
2332c 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
2332d 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
2332e 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
2332f 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
23330 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
23331 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
23332 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
23333 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
23334 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
23335 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
23336 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
23337 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
23338 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
23339 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
2333a 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
2333b 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
2333c 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
2333d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2333e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2333f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23341 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
23342 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
23343 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
23344 68 65 20 41 54 54 41 43 48 20 61 6e 64 20 44 45  he ATTACH and DE
23345 54 41 43 48 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a  TACH commands..*
23346 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
23347 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 0a 2f 2a  E_OMIT_ATTACH./*
23348 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6e 20 65  .** Resolve an e
23349 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77  xpression that w
2334a 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 41 54  as part of an AT
2334b 54 41 43 48 20 6f 72 20 44 45 54 41 43 48 20 73  TACH or DETACH s
2334c 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 0a 2a  tatement. This.*
2334d 2a 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69  * is slightly di
2334e 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 72 65 73  fferent from res
2334f 6f 6c 76 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20  olving a normal 
23350 53 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 2c 20  SQL expression, 
23351 62 65 63 61 75 73 65 20 73 69 6d 70 6c 65 0a 2a  because simple.*
23352 2a 20 69 64 65 6e 74 69 66 69 65 72 73 20 61 72  * identifiers ar
23353 65 20 74 72 65 61 74 65 64 20 61 73 20 73 74 72  e treated as str
23354 69 6e 67 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62  ings, not possib
23355 6c 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  le column names 
23356 6f 72 20 61 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a  or aliases..**.*
23357 2a 20 69 2e 65 2e 20 69 66 20 74 68 65 20 70 61  * i.e. if the pa
23358 72 73 65 72 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a  rser sees:.**.**
23359 20 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41       ATTACH DATA
2335a 42 41 53 45 20 61 62 63 20 41 53 20 64 65 66 0a  BASE abc AS def.
2335b 2a 2a 0a 2a 2a 20 69 74 20 74 72 65 61 74 73 20  **.** it treats 
2335c 74 68 65 20 74 77 6f 20 65 78 70 72 65 73 73 69  the two expressi
2335d 6f 6e 73 20 61 73 20 6c 69 74 65 72 61 6c 20 73  ons as literal s
2335e 74 72 69 6e 67 73 20 27 61 62 63 27 20 61 6e 64  trings 'abc' and
2335f 20 27 64 65 66 27 20 69 6e 73 74 65 61 64 20 6f   'def' instead o
23360 66 0a 2a 2a 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72  f.** looking for
23361 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
23362 73 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a  same name..**.**
23363 20 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69   This only appli
23364 65 73 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e  es to the root n
23365 6f 64 65 20 6f 66 20 70 45 78 70 72 2c 20 73 6f  ode of pExpr, so
23366 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 3a 0a   the statement:.
23367 2a 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48  **.**     ATTACH
23368 20 44 41 54 41 42 41 53 45 20 61 62 63 7c 7c 64   DATABASE abc||d
23369 65 66 20 41 53 20 27 64 62 32 27 0a 2a 2a 0a 2a  ef AS 'db2'.**.*
2336a 2a 20 77 69 6c 6c 20 66 61 69 6c 20 62 65 63 61  * will fail beca
2336b 75 73 65 20 6e 65 69 74 68 65 72 20 61 62 63 20  use neither abc 
2336c 6f 72 20 64 65 66 20 63 61 6e 20 62 65 20 72 65  or def can be re
2336d 73 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  solved..*/.stati
2336e 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 74 74  c int resolveAtt
2336f 61 63 68 45 78 70 72 28 4e 61 6d 65 43 6f 6e 74  achExpr(NameCont
23370 65 78 74 20 2a 70 4e 61 6d 65 2c 20 45 78 70 72  ext *pName, Expr
23371 20 2a 70 45 78 70 72 29 0a 7b 0a 20 20 69 6e 74   *pExpr).{.  int
23372 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
23373 0a 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a  .  if( pExpr ){.
23374 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f      if( pExpr->o
23375 70 21 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20  p!=TK_ID ){.    
23376 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65    rc = sqlite3Re
23377 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70  solveExprNames(p
23378 4e 61 6d 65 2c 20 70 45 78 70 72 29 3b 0a 20 20  Name, pExpr);.  
23379 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
2337a 54 45 5f 4f 4b 20 26 26 20 21 73 71 6c 69 74 65  TE_OK && !sqlite
2337b 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28  3ExprIsConstant(
2337c 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
2337d 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2337e 67 28 70 4e 61 6d 65 2d 3e 70 50 61 72 73 65 2c  g(pName->pParse,
2337f 20 22 69 6e 76 61 6c 69 64 20 6e 61 6d 65 3a 20   "invalid name: 
23380 5c 22 25 73 5c 22 22 2c 20 70 45 78 70 72 2d 3e  \"%s\"", pExpr->
23381 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  u.zToken);.     
23382 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
23383 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a  _ERROR;.      }.
23384 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23385 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f   pExpr->op = TK_
23386 53 54 52 49 4e 47 3b 0a 20 20 20 20 7d 0a 20 20  STRING;.    }.  
23387 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
23388 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 75  ../*.** An SQL u
23389 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65 67  ser-function reg
2338a 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20 74 68  istered to do th
2338b 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 41 54 54  e work of an ATT
2338c 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  ACH statement. T
2338d 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72 67 75  he.** three argu
2338e 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75 6e  ments to the fun
2338f 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65 63  ction come direc
23390 74 6c 79 20 66 72 6f 6d 20 61 6e 20 61 74 74 61  tly from an atta
23391 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a  ch statement:.**
23392 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20 44  .**     ATTACH D
23393 41 54 41 42 41 53 45 20 78 20 41 53 20 79 20 4b  ATABASE x AS y K
23394 45 59 20 7a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  EY z.**.**     S
23395 45 4c 45 43 54 20 73 71 6c 69 74 65 5f 61 74 74  ELECT sqlite_att
23396 61 63 68 28 78 2c 20 79 2c 20 7a 29 0a 2a 2a 0a  ach(x, y, z).**.
23397 2a 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6f 6e  ** If the option
23398 61 6c 20 22 4b 45 59 20 7a 22 20 73 79 6e 74 61  al "KEY z" synta
23399 78 20 69 73 20 6f 6d 69 74 74 65 64 2c 20 61 6e  x is omitted, an
2339a 20 53 51 4c 20 4e 55 4c 4c 20 69 73 20 70 61 73   SQL NULL is pas
2339b 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 74 68  sed as the.** th
2339c 69 72 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  ird argument..*/
2339d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 74 74  .static void att
2339e 61 63 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  achFunc(.  sqlit
2339f 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
233a0 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
233a1 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
233a2 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
233a3 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20  int i;.  int rc 
233a4 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  = 0;.  sqlite3 *
233a5 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
233a6 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
233a7 6f 6e 74 65 78 74 29 3b 0a 20 20 63 6f 6e 73 74  ontext);.  const
233a8 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
233a9 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
233aa 65 3b 0a 20 20 44 62 20 2a 61 4e 65 77 3b 0a 20  e;.  Db *aNew;. 
233ab 20 63 68 61 72 20 2a 7a 45 72 72 44 79 6e 20 3d   char *zErrDyn =
233ac 20 30 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41   0;..  UNUSED_PA
233ad 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
233ae 3b 0a 0a 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f  ;..  zFile = (co
233af 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
233b0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
233b1 67 76 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20  gv[0]);.  zName 
233b2 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
233b3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
233b4 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69  xt(argv[1]);.  i
233b5 66 28 20 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46  f( zFile==0 ) zF
233b6 69 6c 65 20 3d 20 22 22 3b 0a 20 20 69 66 28 20  ile = "";.  if( 
233b7 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65  zName==0 ) zName
233b8 20 3d 20 22 22 3b 0a 0a 20 20 2f 2a 20 43 68 65   = "";..  /* Che
233b9 63 6b 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f  ck for the follo
233ba 77 69 6e 67 20 65 72 72 6f 72 73 3a 0a 20 20 2a  wing errors:.  *
233bb 2a 0a 20 20 2a 2a 20 20 20 20 20 2a 20 54 6f 6f  *.  **     * Too
233bc 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64 20 64   many attached d
233bd 61 74 61 62 61 73 65 73 2c 0a 20 20 2a 2a 20 20  atabases,.  **  
233be 20 20 20 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e     * Transaction
233bf 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 0a   currently open.
233c0 20 20 2a 2a 20 20 20 20 20 2a 20 53 70 65 63 69    **     * Speci
233c1 66 69 65 64 20 64 61 74 61 62 61 73 65 20 6e 61  fied database na
233c2 6d 65 20 61 6c 72 65 61 64 79 20 62 65 69 6e 67  me already being
233c3 20 75 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66   used..  */.  if
233c4 28 20 64 62 2d 3e 6e 44 62 3e 3d 64 62 2d 3e 61  ( db->nDb>=db->a
233c5 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
233c6 49 54 5f 41 54 54 41 43 48 45 44 5d 2b 32 20 29  IT_ATTACHED]+2 )
233c7 7b 0a 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20  {.    zErrDyn = 
233c8 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
233c9 62 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 61 74 74  b, "too many att
233ca 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20  ached databases 
233cb 2d 20 6d 61 78 20 25 64 22 2c 20 0a 20 20 20 20  - max %d", .    
233cc 20 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c    db->aLimit[SQL
233cd 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48  ITE_LIMIT_ATTACH
233ce 45 44 5d 0a 20 20 20 20 29 3b 0a 20 20 20 20 67  ED].    );.    g
233cf 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72  oto attach_error
233d0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62 2d  ;.  }.  if( !db-
233d1 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
233d2 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c     zErrDyn = sql
233d3 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
233d4 22 63 61 6e 6e 6f 74 20 41 54 54 41 43 48 20 64  "cannot ATTACH d
233d5 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20 74  atabase within t
233d6 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20  ransaction");.  
233d7 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72    goto attach_er
233d8 72 6f 72 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  ror;.  }.  for(i
233d9 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
233da 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a  ++){.    char *z
233db 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e   = db->aDb[i].zN
233dc 61 6d 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ame;.    assert(
233dd 20 7a 20 26 26 20 7a 4e 61 6d 65 20 29 3b 0a 20   z && zName );. 
233de 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
233df 72 49 43 6d 70 28 7a 2c 20 7a 4e 61 6d 65 29 3d  rICmp(z, zName)=
233e0 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 45 72 72  =0 ){.      zErr
233e1 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Dyn = sqlite3MPr
233e2 69 6e 74 66 28 64 62 2c 20 22 64 61 74 61 62 61  intf(db, "databa
233e3 73 65 20 25 73 20 69 73 20 61 6c 72 65 61 64 79  se %s is already
233e4 20 69 6e 20 75 73 65 22 2c 20 7a 4e 61 6d 65 29   in use", zName)
233e5 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 74 74  ;.      goto att
233e6 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  ach_error;.    }
233e7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
233e8 61 74 65 20 74 68 65 20 6e 65 77 20 65 6e 74 72  ate the new entr
233e9 79 20 69 6e 20 74 68 65 20 64 62 2d 3e 61 44 62  y in the db->aDb
233ea 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 69 6e 69  [] array and ini
233eb 74 69 61 6c 69 73 65 20 74 68 65 20 73 63 68 65  tialise the sche
233ec 6d 61 0a 20 20 2a 2a 20 68 61 73 68 20 74 61 62  ma.  ** hash tab
233ed 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  les..  */.  if( 
233ee 64 62 2d 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62  db->aDb==db->aDb
233ef 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 61 4e  Static ){.    aN
233f0 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
233f1 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65  llocRaw(db, size
233f2 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33  of(db->aDb[0])*3
233f3 20 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77   );.    if( aNew
233f4 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
233f5 20 20 6d 65 6d 63 70 79 28 61 4e 65 77 2c 20 64    memcpy(aNew, d
233f6 62 2d 3e 61 44 62 2c 20 73 69 7a 65 6f 66 28 64  b->aDb, sizeof(d
233f7 62 2d 3e 61 44 62 5b 30 5d 29 2a 32 29 3b 0a 20  b->aDb[0])*2);. 
233f8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 4e 65 77   }else{.    aNew
233f9 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
233fa 6c 6f 63 28 64 62 2c 20 64 62 2d 3e 61 44 62 2c  loc(db, db->aDb,
233fb 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b   sizeof(db->aDb[
233fc 30 5d 29 2a 28 64 62 2d 3e 6e 44 62 2b 31 29 20  0])*(db->nDb+1) 
233fd 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d  );.    if( aNew=
233fe 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d  =0 ) return;.  }
233ff 0a 20 20 64 62 2d 3e 61 44 62 20 3d 20 61 4e 65  .  db->aDb = aNe
23400 77 3b 0a 20 20 61 4e 65 77 20 3d 20 26 64 62 2d  w;.  aNew = &db-
23401 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 5d 3b 0a 20  >aDb[db->nDb];. 
23402 20 6d 65 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c   memset(aNew, 0,
23403 20 73 69 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b   sizeof(*aNew));
23404 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20  ..  /* Open the 
23405 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
23406 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 73  f the btree is s
23407 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e  uccessfully open
23408 65 64 2c 20 75 73 65 0a 20 20 2a 2a 20 69 74 20  ed, use.  ** it 
23409 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 64 61  to obtain the da
2340a 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 41  tabase schema. A
2340b 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
2340c 20 73 63 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a   schema may.  **
2340d 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 69   or may not be i
2340e 6e 69 74 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f  nitialised..  */
2340f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
23410 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20  treeFactory(db, 
23411 7a 46 69 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45  zFile, 0, SQLITE
23412 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
23413 49 5a 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20  IZE,.           
23414 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23415 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20  db->openFlags | 
23416 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
23417 5f 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20  _DB,.           
23418 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23419 26 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 64  &aNew->pBt);.  d
2341a 62 2d 3e 6e 44 62 2b 2b 3b 0a 20 20 69 66 28 20  b->nDb++;.  if( 
2341b 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54  rc==SQLITE_CONST
2341c 52 41 49 4e 54 20 29 7b 0a 20 20 20 20 72 63 20  RAINT ){.    rc 
2341d 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2341e 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71      zErrDyn = sq
2341f 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
23420 20 22 64 61 74 61 62 61 73 65 20 69 73 20 61 6c   "database is al
23421 72 65 61 64 79 20 61 74 74 61 63 68 65 64 22 29  ready attached")
23422 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63  ;.  }else if( rc
23423 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
23424 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72     Pager *pPager
23425 3b 0a 20 20 20 20 61 4e 65 77 2d 3e 70 53 63 68  ;.    aNew->pSch
23426 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63 68  ema = sqlite3Sch
23427 65 6d 61 47 65 74 28 64 62 2c 20 61 4e 65 77 2d  emaGet(db, aNew-
23428 3e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 20 21  >pBt);.    if( !
23429 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 29 7b  aNew->pSchema ){
2342a 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
2342b 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65  TE_NOMEM;.    }e
2342c 6c 73 65 20 69 66 28 20 61 4e 65 77 2d 3e 70 53  lse if( aNew->pS
2342d 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
2342e 61 74 20 26 26 20 61 4e 65 77 2d 3e 70 53 63 68  at && aNew->pSch
2342f 65 6d 61 2d 3e 65 6e 63 21 3d 45 4e 43 28 64 62  ema->enc!=ENC(db
23430 29 20 29 7b 0a 20 20 20 20 20 20 7a 45 72 72 44  ) ){.      zErrD
23431 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  yn = sqlite3MPri
23432 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20  ntf(db, .       
23433 20 22 61 74 74 61 63 68 65 64 20 64 61 74 61 62   "attached datab
23434 61 73 65 73 20 6d 75 73 74 20 75 73 65 20 74 68  ases must use th
23435 65 20 73 61 6d 65 20 74 65 78 74 20 65 6e 63 6f  e same text enco
23436 64 69 6e 67 20 61 73 20 6d 61 69 6e 20 64 61 74  ding as main dat
23437 61 62 61 73 65 22 29 3b 0a 20 20 20 20 20 20 72  abase");.      r
23438 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
23439 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67  ;.    }.    pPag
2343a 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
2343b 65 50 61 67 65 72 28 61 4e 65 77 2d 3e 70 42 74  ePager(aNew->pBt
2343c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  );.    sqlite3Pa
2343d 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70  gerLockingMode(p
2343e 50 61 67 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4c  Pager, db->dfltL
2343f 6f 63 6b 4d 6f 64 65 29 3b 0a 20 20 20 20 73 71  ockMode);.    sq
23440 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
23441 6c 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 64 62  lMode(pPager, db
23442 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64  ->dfltJournalMod
23443 65 29 3b 0a 20 20 7d 0a 20 20 61 4e 65 77 2d 3e  e);.  }.  aNew->
23444 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
23445 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d  bStrDup(db, zNam
23446 65 29 3b 0a 20 20 61 4e 65 77 2d 3e 73 61 66 65  e);.  aNew->safe
23447 74 79 5f 6c 65 76 65 6c 20 3d 20 33 3b 0a 0a 23  ty_level = 3;..#
23448 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  if SQLITE_HAS_CO
23449 44 45 43 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  DEC.  if( rc==SQ
2344a 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65  LITE_OK ){.    e
2344b 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65  xtern int sqlite
2344c 33 43 6f 64 65 63 41 74 74 61 63 68 28 73 71 6c  3CodecAttach(sql
2344d 69 74 65 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73  ite3*, int, cons
2344e 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 20  t void*, int);. 
2344f 20 20 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73     extern void s
23450 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65  qlite3CodecGetKe
23451 79 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c  y(sqlite3*, int,
23452 20 76 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b 0a   void**, int*);.
23453 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20      int nKey;.  
23454 20 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20 20    char *zKey;.  
23455 20 20 69 6e 74 20 74 20 3d 20 73 71 6c 69 74 65    int t = sqlite
23456 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
23457 76 5b 32 5d 29 3b 0a 20 20 20 20 73 77 69 74 63  v[2]);.    switc
23458 68 28 20 74 20 29 7b 0a 20 20 20 20 20 20 63 61  h( t ){.      ca
23459 73 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  se SQLITE_INTEGE
2345a 52 3a 0a 20 20 20 20 20 20 63 61 73 65 20 53 51  R:.      case SQ
2345b 4c 49 54 45 5f 46 4c 4f 41 54 3a 0a 20 20 20 20  LITE_FLOAT:.    
2345c 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71      zErrDyn = sq
2345d 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2345e 2c 20 22 49 6e 76 61 6c 69 64 20 6b 65 79 20 76  , "Invalid key v
2345f 61 6c 75 65 22 29 3b 0a 20 20 20 20 20 20 20 20  alue");.        
23460 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
23461 52 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  R;.        break
23462 3b 0a 20 20 20 20 20 20 20 20 0a 20 20 20 20 20  ;.        .     
23463 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58   case SQLITE_TEX
23464 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20 53 51  T:.      case SQ
23465 4c 49 54 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 20  LITE_BLOB:.     
23466 20 20 20 6e 4b 65 79 20 3d 20 73 71 6c 69 74 65     nKey = sqlite
23467 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
23468 67 76 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20  gv[2]);.        
23469 7a 4b 65 79 20 3d 20 28 63 68 61 72 20 2a 29 73  zKey = (char *)s
2346a 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
2346b 62 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20  b(argv[2]);.    
2346c 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2346d 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c 20  CodecAttach(db, 
2346e 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c  db->nDb-1, zKey,
2346f 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20   nKey);.        
23470 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 63 61  break;..      ca
23471 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 0a  se SQLITE_NULL:.
23472 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 6b 65          /* No ke
23473 79 20 73 70 65 63 69 66 69 65 64 2e 20 20 55 73  y specified.  Us
23474 65 20 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 74  e the key from t
23475 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
23476 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
23477 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28 64  te3CodecGetKey(d
23478 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26 7a  b, 0, (void**)&z
23479 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20 20  Key, &nKey);.   
2347a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2347b 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c  3CodecAttach(db,
2347c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79   db->nDb-1, zKey
2347d 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20  , nKey);.       
2347e 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2347f 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
23480 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 6f  f the file was o
23481 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c  pened successful
23482 6c 79 2c 20 72 65 61 64 20 74 68 65 20 73 63 68  ly, read the sch
23483 65 6d 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20  ema for the new 
23484 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49  database..  ** I
23485 66 20 74 68 69 73 20 66 61 69 6c 73 2c 20 6f 72  f this fails, or
23486 20 69 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20   if opening the 
23487 66 69 6c 65 20 66 61 69 6c 65 64 2c 20 74 68 65  file failed, the
23488 6e 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  n close the file
23489 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76   and .  ** remov
2348a 65 20 74 68 65 20 65 6e 74 72 79 20 66 72 6f 6d  e the entry from
2348b 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61   the db->aDb[] a
2348c 72 72 61 79 2e 20 69 2e 65 2e 20 70 75 74 20 65  rray. i.e. put e
2348d 76 65 72 79 74 68 69 6e 67 20 62 61 63 6b 20 74  verything back t
2348e 68 65 20 77 61 79 0a 20 20 2a 2a 20 77 65 20 66  he way.  ** we f
2348f 6f 75 6e 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20  ound it..  */.  
23490 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
23491 4b 20 29 7b 0a 20 20 20 20 28 76 6f 69 64 29 73  K ){.    (void)s
23492 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
23493 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  b);.    sqlite3B
23494 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29  treeEnterAll(db)
23495 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
23496 65 33 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72  e3Init(db, &zErr
23497 44 79 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Dyn);.    sqlite
23498 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
23499 62 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71  b);.    (void)sq
2349a 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
2349b 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  b);.  }.  if( rc
2349c 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20   ){.    int iDb 
2349d 3d 20 64 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20  = db->nDb - 1;. 
2349e 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d     assert( iDb>=
2349f 32 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  2 );.    if( db-
234a0 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b  >aDb[iDb].pBt ){
234a1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
234a2 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62  reeClose(db->aDb
234a3 5b 69 44 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20  [iDb].pBt);.    
234a4 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70    db->aDb[iDb].p
234a5 42 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 62  Bt = 0;.      db
234a6 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
234a7 6d 61 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ma = 0;.    }.  
234a8 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
234a9 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
234aa 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62   0);.    db->nDb
234ab 20 3d 20 69 44 62 3b 0a 20 20 20 20 69 66 28 20   = iDb;.    if( 
234ac 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
234ad 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49   || rc==SQLITE_I
234ae 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  OERR_NOMEM ){.  
234af 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
234b0 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
234b1 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
234b2 2c 20 7a 45 72 72 44 79 6e 29 3b 0a 20 20 20 20  , zErrDyn);.    
234b3 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69    zErrDyn = sqli
234b4 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
234b5 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b  out of memory");
234b6 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a  .    }else if( z
234b7 45 72 72 44 79 6e 3d 3d 30 20 29 7b 0a 20 20 20  ErrDyn==0 ){.   
234b8 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c     zErrDyn = sql
234b9 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
234ba 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  "unable to open 
234bb 64 61 74 61 62 61 73 65 3a 20 25 73 22 2c 20 7a  database: %s", z
234bc 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  File);.    }.   
234bd 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72 72   goto attach_err
234be 6f 72 3b 0a 20 20 7d 0a 20 20 0a 20 20 72 65 74  or;.  }.  .  ret
234bf 75 72 6e 3b 0a 0a 61 74 74 61 63 68 5f 65 72 72  urn;..attach_err
234c0 6f 72 3a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20  or:.  /* Return 
234c1 61 6e 20 65 72 72 6f 72 20 69 66 20 77 65 20 67  an error if we g
234c2 65 74 20 68 65 72 65 20 2a 2f 0a 20 20 69 66 28  et here */.  if(
234c3 20 7a 45 72 72 44 79 6e 20 29 7b 0a 20 20 20 20   zErrDyn ){.    
234c4 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
234c5 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45  rror(context, zE
234c6 72 72 44 79 6e 2c 20 2d 31 29 3b 0a 20 20 20 20  rrDyn, -1);.    
234c7 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
234c8 2c 20 7a 45 72 72 44 79 6e 29 3b 0a 20 20 7d 0a  , zErrDyn);.  }.
234c9 20 20 69 66 28 20 72 63 20 29 20 73 71 6c 69 74    if( rc ) sqlit
234ca 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
234cb 63 6f 64 65 28 63 6f 6e 74 65 78 74 2c 20 72 63  code(context, rc
234cc 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53  );.}../*.** An S
234cd 51 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e  QL user-function
234ce 20 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64   registered to d
234cf 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e  o the work of an
234d0 20 44 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e   DETACH statemen
234d1 74 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20  t. The.** three 
234d2 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
234d3 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64   function come d
234d4 69 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 20 64  irectly from a d
234d5 65 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74 3a  etach statement:
234d6 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43  .**.**     DETAC
234d7 48 20 44 41 54 41 42 41 53 45 20 78 0a 2a 2a 0a  H DATABASE x.**.
234d8 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 73 71  **     SELECT sq
234d9 6c 69 74 65 5f 64 65 74 61 63 68 28 78 29 0a 2a  lite_detach(x).*
234da 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65  /.static void de
234db 74 61 63 68 46 75 6e 63 28 0a 20 20 73 71 6c 69  tachFunc(.  sqli
234dc 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
234dd 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55  text,.  int NotU
234de 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  sed,.  sqlite3_v
234df 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
234e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
234e1 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  me = (const char
234e2 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
234e3 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
234e4 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
234e5 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
234e6 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
234e7 74 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 44  t);.  int i;.  D
234e8 62 20 2a 70 44 62 20 3d 20 30 3b 0a 20 20 63 68  b *pDb = 0;.  ch
234e9 61 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20  ar zErr[128];.. 
234ea 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
234eb 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69  R(NotUsed);..  i
234ec 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e  f( zName==0 ) zN
234ed 61 6d 65 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28  ame = "";.  for(
234ee 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
234ef 69 2b 2b 29 7b 0a 20 20 20 20 70 44 62 20 3d 20  i++){.    pDb = 
234f0 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20  &db->aDb[i];.   
234f1 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30   if( pDb->pBt==0
234f2 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
234f3 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
234f4 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20  Cmp(pDb->zName, 
234f5 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61  zName)==0 ) brea
234f6 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3e  k;.  }..  if( i>
234f7 3d 64 62 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20  =db->nDb ){.    
234f8 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
234f9 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45  (sizeof(zErr),zE
234fa 72 72 2c 20 22 6e 6f 20 73 75 63 68 20 64 61 74  rr, "no such dat
234fb 61 62 61 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d  abase: %s", zNam
234fc 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74  e);.    goto det
234fd 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20  ach_error;.  }. 
234fe 20 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20   if( i<2 ){.    
234ff 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
23500 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45  (sizeof(zErr),zE
23501 72 72 2c 20 22 63 61 6e 6e 6f 74 20 64 65 74 61  rr, "cannot deta
23502 63 68 20 64 61 74 61 62 61 73 65 20 25 73 22 2c  ch database %s",
23503 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74   zName);.    got
23504 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a  o detach_error;.
23505 20 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61    }.  if( !db->a
23506 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20  utoCommit ){.   
23507 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
23508 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20  f(sizeof(zErr), 
23509 7a 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20  zErr,.          
2350a 20 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e             "cann
2350b 6f 74 20 44 45 54 41 43 48 20 64 61 74 61 62 61  ot DETACH databa
2350c 73 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61  se within transa
2350d 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74  ction");.    got
2350e 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a  o detach_error;.
2350f 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
23510 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72  3BtreeIsInReadTr
23511 61 6e 73 28 70 44 62 2d 3e 70 42 74 29 20 7c 7c  ans(pDb->pBt) ||
23512 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
23513 6e 42 61 63 6b 75 70 28 70 44 62 2d 3e 70 42 74  nBackup(pDb->pBt
23514 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
23515 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
23516 28 7a 45 72 72 29 2c 7a 45 72 72 2c 20 22 64 61  (zErr),zErr, "da
23517 74 61 62 61 73 65 20 25 73 20 69 73 20 6c 6f 63  tabase %s is loc
23518 6b 65 64 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  ked", zName);.  
23519 20 20 67 6f 74 6f 20 64 65 74 61 63 68 5f 65 72    goto detach_er
2351a 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  ror;.  }..  sqli
2351b 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 70 44  te3BtreeClose(pD
2351c 62 2d 3e 70 42 74 29 3b 0a 20 20 70 44 62 2d 3e  b->pBt);.  pDb->
2351d 70 42 74 20 3d 20 30 3b 0a 20 20 70 44 62 2d 3e  pBt = 0;.  pDb->
2351e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 73  pSchema = 0;.  s
2351f 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
23520 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
23521 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 64 65 74  ;.  return;..det
23522 61 63 68 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c  ach_error:.  sql
23523 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
23524 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c  r(context, zErr,
23525 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   -1);.}../*.** T
23526 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 67 65  his procedure ge
23527 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64  nerates VDBE cod
23528 65 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 69  e for a single i
23529 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 65 69 74  nvocation of eit
2352a 68 65 72 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74  her the.** sqlit
2352b 65 5f 64 65 74 61 63 68 28 29 20 6f 72 20 73 71  e_detach() or sq
2352c 6c 69 74 65 5f 61 74 74 61 63 68 28 29 20 53 51  lite_attach() SQ
2352d 4c 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73  L user functions
2352e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2352f 20 63 6f 64 65 41 74 74 61 63 68 28 0a 20 20 50   codeAttach(.  P
23530 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
23531 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65      /* The parse
23532 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69  r context */.  i
23533 6e 74 20 74 79 70 65 2c 20 20 20 20 20 20 20 20  nt type,        
23534 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20 53 51      /* Either SQ
23535 4c 49 54 45 5f 41 54 54 41 43 48 20 6f 72 20 53  LITE_ATTACH or S
23536 51 4c 49 54 45 5f 44 45 54 41 43 48 20 2a 2f 0a  QLITE_DETACH */.
23537 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63    FuncDef *pFunc
23538 2c 20 20 20 20 20 20 2f 2a 20 46 75 6e 63 44 65  ,      /* FuncDe
23539 66 20 77 72 61 70 70 65 72 20 66 6f 72 20 64 65  f wrapper for de
2353a 74 61 63 68 46 75 6e 63 28 29 20 6f 72 20 61 74  tachFunc() or at
2353b 74 61 63 68 46 75 6e 63 28 29 20 2a 2f 0a 20 20  tachFunc() */.  
2353c 45 78 70 72 20 2a 70 41 75 74 68 41 72 67 2c 20  Expr *pAuthArg, 
2353d 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
2353e 6f 6e 20 74 6f 20 70 61 73 73 20 74 6f 20 61 75  on to pass to au
2353f 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c  thorization call
23540 62 61 63 6b 20 2a 2f 0a 20 20 45 78 70 72 20 2a  back */.  Expr *
23541 70 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f  pFilename,     /
23542 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
23543 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 45 78 70  se file */.  Exp
23544 72 20 2a 70 44 62 6e 61 6d 65 2c 20 20 20 20 20  r *pDbname,     
23545 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
23546 20 64 61 74 61 62 61 73 65 20 74 6f 20 75 73 65   database to use
23547 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20   internally */. 
23548 20 45 78 70 72 20 2a 70 4b 65 79 20 20 20 20 20   Expr *pKey     
23549 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2354a 65 20 6b 65 79 20 66 6f 72 20 65 6e 63 72 79 70  e key for encryp
2354b 74 69 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 20 2a  tion extension *
2354c 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
2354d 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 61   NameContext sNa
2354e 6d 65 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20  me;.  Vdbe *v;. 
2354f 20 73 71 6c 69 74 65 33 2a 20 64 62 20 3d 20 70   sqlite3* db = p
23550 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
23551 20 72 65 67 41 72 67 73 3b 0a 0a 20 20 6d 65 6d   regArgs;..  mem
23552 73 65 74 28 26 73 4e 61 6d 65 2c 20 30 2c 20 73  set(&sName, 0, s
23553 69 7a 65 6f 66 28 4e 61 6d 65 43 6f 6e 74 65 78  izeof(NameContex
23554 74 29 29 3b 0a 20 20 73 4e 61 6d 65 2e 70 50 61  t));.  sName.pPa
23555 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 0a 20  rse = pParse;.. 
23556 20 69 66 28 20 0a 20 20 20 20 20 20 53 51 4c 49   if( .      SQLI
23557 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73  TE_OK!=(rc = res
23558 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 26  olveAttachExpr(&
23559 73 4e 61 6d 65 2c 20 70 46 69 6c 65 6e 61 6d 65  sName, pFilename
2355a 29 29 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c 49  )) ||.      SQLI
2355b 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73  TE_OK!=(rc = res
2355c 6f 6c 76 65 41 74 74 61 63 68 45 78 70 72 28 26  olveAttachExpr(&
2355d 73 4e 61 6d 65 2c 20 70 44 62 6e 61 6d 65 29 29  sName, pDbname))
2355e 20 7c 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45   ||.      SQLITE
2355f 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c  _OK!=(rc = resol
23560 76 65 41 74 74 61 63 68 45 78 70 72 28 26 73 4e  veAttachExpr(&sN
23561 61 6d 65 2c 20 70 4b 65 79 29 29 0a 20 20 29 7b  ame, pKey)).  ){
23562 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72  .    pParse->nEr
23563 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74  r++;.    goto at
23564 74 61 63 68 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 23  tach_end;.  }..#
23565 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
23566 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
23567 0a 20 20 69 66 28 20 70 41 75 74 68 41 72 67 20  .  if( pAuthArg 
23568 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 41 75  ){.    char *zAu
23569 74 68 41 72 67 20 3d 20 70 41 75 74 68 41 72 67  thArg = pAuthArg
2356a 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  ->u.zToken;.    
2356b 69 66 28 20 4e 45 56 45 52 28 7a 41 75 74 68 41  if( NEVER(zAuthA
2356c 72 67 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20  rg==0) ){.      
2356d 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b  goto attach_end;
2356e 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
2356f 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
23570 28 70 50 61 72 73 65 2c 20 74 79 70 65 2c 20 7a  (pParse, type, z
23571 41 75 74 68 41 72 67 2c 20 30 2c 20 30 29 3b 0a  AuthArg, 0, 0);.
23572 20 20 20 20 69 66 28 72 63 21 3d 53 51 4c 49 54      if(rc!=SQLIT
23573 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
23574 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20  to attach_end;. 
23575 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20     }.  }.#endif 
23576 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  /* SQLITE_OMIT_A
23577 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a  UTHORIZATION */.
23578 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
23579 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2357a 20 20 72 65 67 41 72 67 73 20 3d 20 73 71 6c 69    regArgs = sqli
2357b 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
2357c 70 50 61 72 73 65 2c 20 34 29 3b 0a 20 20 73 71  pParse, 4);.  sq
2357d 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2357e 61 72 73 65 2c 20 70 46 69 6c 65 6e 61 6d 65 2c  arse, pFilename,
2357f 20 72 65 67 41 72 67 73 29 3b 0a 20 20 73 71 6c   regArgs);.  sql
23580 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61  ite3ExprCode(pPa
23581 72 73 65 2c 20 70 44 62 6e 61 6d 65 2c 20 72 65  rse, pDbname, re
23582 67 41 72 67 73 2b 31 29 3b 0a 20 20 73 71 6c 69  gArgs+1);.  sqli
23583 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
23584 73 65 2c 20 70 4b 65 79 2c 20 72 65 67 41 72 67  se, pKey, regArg
23585 73 2b 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  s+2);..  assert(
23586 20 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63   v || db->malloc
23587 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
23588 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  v ){.    sqlite3
23589 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2358a 5f 46 75 6e 63 74 69 6f 6e 2c 20 30 2c 20 72 65  _Function, 0, re
2358b 67 41 72 67 73 2b 33 2d 70 46 75 6e 63 2d 3e 6e  gArgs+3-pFunc->n
2358c 41 72 67 2c 20 72 65 67 41 72 67 73 2b 33 29 3b  Arg, regArgs+3);
2358d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 75  .    assert( pFu
2358e 6e 63 2d 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20  nc->nArg==-1 || 
2358f 28 70 46 75 6e 63 2d 3e 6e 41 72 67 26 30 78 66  (pFunc->nArg&0xf
23590 66 29 3d 3d 70 46 75 6e 63 2d 3e 6e 41 72 67 20  f)==pFunc->nArg 
23591 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23592 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75  beChangeP5(v, (u
23593 38 29 28 70 46 75 6e 63 2d 3e 6e 41 72 67 29 29  8)(pFunc->nArg))
23594 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23595 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
23596 20 28 63 68 61 72 20 2a 29 70 46 75 6e 63 2c 20   (char *)pFunc, 
23597 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 0a 20 20  P4_FUNCDEF);..  
23598 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f    /* Code an OP_
23599 45 78 70 69 72 65 2e 20 46 6f 72 20 61 6e 20 41  Expire. For an A
2359a 54 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2c  TTACH statement,
2359b 20 73 65 74 20 50 31 20 74 6f 20 74 72 75 65 20   set P1 to true 
2359c 28 65 78 70 69 72 65 20 74 68 69 73 0a 20 20 20  (expire this.   
2359d 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e   ** statement on
2359e 6c 79 29 2e 20 46 6f 72 20 44 45 54 41 43 48 2c  ly). For DETACH,
2359f 20 73 65 74 20 69 74 20 74 6f 20 66 61 6c 73 65   set it to false
235a0 20 28 65 78 70 69 72 65 20 61 6c 6c 20 65 78 69   (expire all exi
235a1 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20 73 74 61  sting.    ** sta
235a2 74 65 6d 65 6e 74 73 29 2e 0a 20 20 20 20 2a 2f  tements)..    */
235a3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
235a4 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70  AddOp1(v, OP_Exp
235a5 69 72 65 2c 20 28 74 79 70 65 3d 3d 53 51 4c 49  ire, (type==SQLI
235a6 54 45 5f 41 54 54 41 43 48 29 29 3b 0a 20 20 7d  TE_ATTACH));.  }
235a7 0a 20 20 0a 61 74 74 61 63 68 5f 65 6e 64 3a 0a  .  .attach_end:.
235a8 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
235a9 65 74 65 28 64 62 2c 20 70 46 69 6c 65 6e 61 6d  ete(db, pFilenam
235aa 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  e);.  sqlite3Exp
235ab 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44 62 6e  rDelete(db, pDbn
235ac 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ame);.  sqlite3E
235ad 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4b  xprDelete(db, pK
235ae 65 79 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61  ey);.}../*.** Ca
235af 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
235b0 65 72 20 74 6f 20 63 6f 6d 70 69 6c 65 20 61 20  er to compile a 
235b1 44 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74  DETACH statement
235b2 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41  ..**.**     DETA
235b3 43 48 20 70 44 62 6e 61 6d 65 0a 2a 2f 0a 53 51  CH pDbname.*/.SQ
235b4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
235b5 64 20 73 71 6c 69 74 65 33 44 65 74 61 63 68 28  d sqlite3Detach(
235b6 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
235b7 78 70 72 20 2a 70 44 62 6e 61 6d 65 29 7b 0a 20  xpr *pDbname){. 
235b8 20 73 74 61 74 69 63 20 46 75 6e 63 44 65 66 20   static FuncDef 
235b9 64 65 74 61 63 68 5f 66 75 6e 63 20 3d 20 7b 0a  detach_func = {.
235ba 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20      1,          
235bb 20 20 20 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f        /* nArg */
235bc 0a 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38  .    SQLITE_UTF8
235bd 2c 20 20 20 20 20 20 2f 2a 20 69 50 72 65 66 45  ,      /* iPrefE
235be 6e 63 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  nc */.    0,    
235bf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
235c0 6c 61 67 73 20 2a 2f 0a 20 20 20 20 30 2c 20 20  lags */.    0,  
235c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
235c2 20 70 55 73 65 72 44 61 74 61 20 2a 2f 0a 20 20   pUserData */.  
235c3 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
235c4 20 20 20 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a      /* pNext */.
235c5 20 20 20 20 64 65 74 61 63 68 46 75 6e 63 2c 20      detachFunc, 
235c6 20 20 20 20 20 20 2f 2a 20 78 46 75 6e 63 20 2a        /* xFunc *
235c7 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
235c8 20 20 20 20 20 20 20 20 2f 2a 20 78 53 74 65 70          /* xStep
235c9 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
235ca 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
235cb 6e 61 6c 69 7a 65 20 2a 2f 0a 20 20 20 20 22 73  nalize */.    "s
235cc 71 6c 69 74 65 5f 64 65 74 61 63 68 22 2c 20 20  qlite_detach",  
235cd 2f 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  /* zName */.    
235ce 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
235cf 20 20 2f 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20    /* pHash */.  
235d0 7d 3b 0a 20 20 63 6f 64 65 41 74 74 61 63 68 28  };.  codeAttach(
235d1 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44  pParse, SQLITE_D
235d2 45 54 41 43 48 2c 20 26 64 65 74 61 63 68 5f 66  ETACH, &detach_f
235d3 75 6e 63 2c 20 70 44 62 6e 61 6d 65 2c 20 30 2c  unc, pDbname, 0,
235d4 20 30 2c 20 70 44 62 6e 61 6d 65 29 3b 0a 7d 0a   0, pDbname);.}.
235d5 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79  ./*.** Called by
235d6 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 63   the parser to c
235d7 6f 6d 70 69 6c 65 20 61 6e 20 41 54 54 41 43 48  ompile an ATTACH
235d8 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
235d9 2a 20 20 20 20 20 41 54 54 41 43 48 20 70 20 41  *     ATTACH p A
235da 53 20 70 44 62 6e 61 6d 65 20 4b 45 59 20 70 4b  S pDbname KEY pK
235db 65 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ey.*/.SQLITE_PRI
235dc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
235dd 33 41 74 74 61 63 68 28 50 61 72 73 65 20 2a 70  3Attach(Parse *p
235de 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 2c 20  Parse, Expr *p, 
235df 45 78 70 72 20 2a 70 44 62 6e 61 6d 65 2c 20 45  Expr *pDbname, E
235e0 78 70 72 20 2a 70 4b 65 79 29 7b 0a 20 20 73 74  xpr *pKey){.  st
235e1 61 74 69 63 20 46 75 6e 63 44 65 66 20 61 74 74  atic FuncDef att
235e2 61 63 68 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20  ach_func = {.   
235e3 20 33 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   3,             
235e4 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20     /* nArg */.  
235e5 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20    SQLITE_UTF8,  
235e6 20 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20      /* iPrefEnc 
235e7 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
235e8 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67           /* flag
235e9 73 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  s */.    0,     
235ea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 55             /* pU
235eb 73 65 72 44 61 74 61 20 2a 2f 0a 20 20 20 20 30  serData */.    0
235ec 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
235ed 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20   /* pNext */.   
235ee 20 61 74 74 61 63 68 46 75 6e 63 2c 20 20 20 20   attachFunc,    
235ef 20 20 20 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20     /* xFunc */. 
235f0 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
235f1 20 20 20 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f       /* xStep */
235f2 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
235f3 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c         /* xFinal
235f4 69 7a 65 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69  ize */.    "sqli
235f5 74 65 5f 61 74 74 61 63 68 22 2c 20 20 2f 2a 20  te_attach",  /* 
235f6 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 20 20  zName */.    0  
235f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
235f8 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20 7d 3b 0a  * pHash */.  };.
235f9 20 20 63 6f 64 65 41 74 74 61 63 68 28 70 50 61    codeAttach(pPa
235fa 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 54 54 41  rse, SQLITE_ATTA
235fb 43 48 2c 20 26 61 74 74 61 63 68 5f 66 75 6e 63  CH, &attach_func
235fc 2c 20 70 2c 20 70 2c 20 70 44 62 6e 61 6d 65 2c  , p, p, pDbname,
235fd 20 70 4b 65 79 29 3b 0a 7d 0a 23 65 6e 64 69 66   pKey);.}.#endif
235fe 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
235ff 41 54 54 41 43 48 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ATTACH */../*.**
23600 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 44 62   Initialize a Db
23601 46 69 78 65 72 20 73 74 72 75 63 74 75 72 65 2e  Fixer structure.
23602 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d    This routine m
23603 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 70 72  ust be called pr
23604 69 6f 72 0a 2a 2a 20 74 6f 20 70 61 73 73 69 6e  ior.** to passin
23605 67 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  g the structure 
23606 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 71  to one of the sq
23607 6c 69 74 65 46 69 78 41 41 41 41 28 29 20 72 6f  liteFixAAAA() ro
23608 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a  utines below..**
23609 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76  .** The return v
2360a 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 77  alue indicates w
2360b 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 66 69  hether or not fi
2360c 78 61 74 69 6f 6e 20 69 73 20 72 65 71 75 69 72  xation is requir
2360d 65 64 2e 20 20 54 52 55 45 0a 2a 2a 20 6d 65 61  ed.  TRUE.** mea
2360e 6e 73 20 77 65 20 64 6f 20 6e 65 65 64 20 74 6f  ns we do need to
2360f 20 66 69 78 20 74 68 65 20 64 61 74 61 62 61 73   fix the databas
23610 65 20 72 65 66 65 72 65 6e 63 65 73 2c 20 46 41  e references, FA
23611 4c 53 45 20 6d 65 61 6e 73 20 77 65 20 64 6f 20  LSE means we do 
23612 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  not..*/.SQLITE_P
23613 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
23614 65 33 46 69 78 49 6e 69 74 28 0a 20 20 44 62 46  e3FixInit(.  DbF
23615 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20  ixer *pFix,     
23616 20 2f 2a 20 54 68 65 20 66 69 78 65 72 20 74 6f   /* The fixer to
23617 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   be initialized 
23618 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  */.  Parse *pPar
23619 73 65 2c 20 20 20 20 20 20 2f 2a 20 45 72 72 6f  se,      /* Erro
2361a 72 20 6d 65 73 73 61 67 65 73 20 77 69 6c 6c 20  r messages will 
2361b 62 65 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  be written here 
2361c 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  */.  int iDb,   
2361d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
2361e 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
2361f 20 74 68 61 74 20 6d 75 73 74 20 62 65 20 75 73   that must be us
23620 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
23621 61 72 20 2a 7a 54 79 70 65 2c 20 20 2f 2a 20 22  ar *zType,  /* "
23622 76 69 65 77 22 2c 20 22 74 72 69 67 67 65 72 22  view", "trigger"
23623 2c 20 6f 72 20 22 69 6e 64 65 78 22 20 2a 2f 0a  , or "index" */.
23624 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70    const Token *p
23625 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  Name  /* Name of
23626 20 74 68 65 20 76 69 65 77 2c 20 74 72 69 67 67   the view, trigg
23627 65 72 2c 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a  er, or index */.
23628 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
23629 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 69  ;..  if( NEVER(i
2362a 44 62 3c 30 29 20 7c 7c 20 69 44 62 3d 3d 31 20  Db<0) || iDb==1 
2362b 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 64 62  ) return 0;.  db
2362c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2362d 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6e 44 62   assert( db->nDb
2362e 3e 69 44 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e  >iDb );.  pFix->
2362f 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
23630 0a 20 20 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64  .  pFix->zDb = d
23631 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
23632 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 54 79 70 65  e;.  pFix->zType
23633 20 3d 20 7a 54 79 70 65 3b 0a 20 20 70 46 69 78   = zType;.  pFix
23634 2d 3e 70 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b  ->pName = pName;
23635 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
23636 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
23637 69 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74 69  ing set of routi
23638 6e 65 73 20 77 61 6c 6b 20 74 68 72 6f 75 67 68  nes walk through
23639 20 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20   the parse tree 
2363a 61 6e 64 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20  and assign.** a 
2363b 73 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73  specific databas
2363c 65 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72  e to all table r
2363d 65 66 65 72 65 6e 63 65 73 20 77 68 65 72 65 20  eferences where 
2363e 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
2363f 65 0a 2a 2a 20 77 61 73 20 6c 65 66 74 20 75 6e  e.** was left un
23640 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
23641 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74   original SQL st
23642 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46  atement.  The pF
23643 69 78 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ix structure.** 
23644 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20 69  must have been i
23645 6e 69 74 69 61 6c 69 7a 65 64 20 62 79 20 61 20  nitialized by a 
23646 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71  prior call to sq
23647 6c 69 74 65 33 46 69 78 49 6e 69 74 28 29 2e 0a  lite3FixInit()..
23648 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
23649 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  ines are used to
2364a 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
2364b 61 6e 20 69 6e 64 65 78 2c 20 74 72 69 67 67 65  an index, trigge
2364c 72 2c 20 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e  r, or.** view in
2364d 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 64 6f   one database do
2364e 65 73 20 6e 6f 74 20 72 65 66 65 72 20 74 6f 20  es not refer to 
2364f 6f 62 6a 65 63 74 73 20 69 6e 20 61 20 64 69 66  objects in a dif
23650 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2e  ferent database.
23651 0a 2a 2a 20 28 45 78 63 65 70 74 69 6f 6e 3a 20  .** (Exception: 
23652 69 6e 64 69 63 65 73 2c 20 74 72 69 67 67 65 72  indices, trigger
23653 73 2c 20 61 6e 64 20 76 69 65 77 73 20 69 6e 20  s, and views in 
23654 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73  the TEMP databas
23655 65 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64  e are.** allowed
23656 20 74 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79   to refer to any
23657 74 68 69 6e 67 2e 29 20 20 49 66 20 61 20 72 65  thing.)  If a re
23658 66 65 72 65 6e 63 65 20 69 73 20 65 78 70 6c 69  ference is expli
23659 63 69 74 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f  citly made.** to
2365a 20 61 6e 20 6f 62 6a 65 63 74 20 69 6e 20 61 20   an object in a 
2365b 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62 61  different databa
2365c 73 65 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  se, an error mes
2365d 73 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f  sage is added to
2365e 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 7a 45 72 72  .** pParse->zErr
2365f 4d 73 67 20 61 6e 64 20 74 68 65 73 65 20 72 6f  Msg and these ro
23660 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f  utines return no
23661 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 65 76 65 72  n-zero.  If ever
23662 79 74 68 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73  ything.** checks
23663 20 6f 75 74 2c 20 74 68 65 73 65 20 72 6f 75 74   out, these rout
23664 69 6e 65 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a  ines return 0..*
23665 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23666 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53   int sqlite3FixS
23667 72 63 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65  rcList(.  DbFixe
23668 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f  r *pFix,       /
23669 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
2366a 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53   fixation */.  S
2366b 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20  rcList *pList   
2366c 20 20 20 20 2f 2a 20 54 68 65 20 53 6f 75 72 63      /* The Sourc
2366d 65 20 6c 69 73 74 20 74 6f 20 63 68 65 63 6b 20  e list to check 
2366e 61 6e 64 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b  and modify */.){
2366f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73  .  int i;.  cons
23670 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73  t char *zDb;.  s
23671 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
23672 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66  em *pItem;..  if
23673 28 20 4e 45 56 45 52 28 70 4c 69 73 74 3d 3d 30  ( NEVER(pList==0
23674 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
23675 7a 44 62 20 3d 20 70 46 69 78 2d 3e 7a 44 62 3b  zDb = pFix->zDb;
23676 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65  .  for(i=0, pIte
23677 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c  m=pList->a; i<pL
23678 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20  ist->nSrc; i++, 
23679 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66  pItem++){.    if
2367a 28 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  ( pItem->zDataba
2367b 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  se==0 ){.      p
2367c 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20  Item->zDatabase 
2367d 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
2367e 70 28 70 46 69 78 2d 3e 70 50 61 72 73 65 2d 3e  p(pFix->pParse->
2367f 64 62 2c 20 7a 44 62 29 3b 0a 20 20 20 20 7d 65  db, zDb);.    }e
23680 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53  lse if( sqlite3S
23681 74 72 49 43 6d 70 28 70 49 74 65 6d 2d 3e 7a 44  trICmp(pItem->zD
23682 61 74 61 62 61 73 65 2c 7a 44 62 29 21 3d 30 20  atabase,zDb)!=0 
23683 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
23684 45 72 72 6f 72 4d 73 67 28 70 46 69 78 2d 3e 70  ErrorMsg(pFix->p
23685 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20  Parse,.         
23686 22 25 73 20 25 54 20 63 61 6e 6e 6f 74 20 72 65  "%s %T cannot re
23687 66 65 72 65 6e 63 65 20 6f 62 6a 65 63 74 73 20  ference objects 
23688 69 6e 20 64 61 74 61 62 61 73 65 20 25 73 22 2c  in database %s",
23689 0a 20 20 20 20 20 20 20 20 20 70 46 69 78 2d 3e  .         pFix->
2368a 7a 54 79 70 65 2c 20 70 46 69 78 2d 3e 70 4e 61  zType, pFix->pNa
2368b 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61  me, pItem->zData
2368c 62 61 73 65 29 3b 0a 20 20 20 20 20 20 72 65 74  base);.      ret
2368d 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 23 69 66  urn 1;.    }.#if
2368e 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2368f 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21  _OMIT_VIEW) || !
23690 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
23691 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20  MIT_TRIGGER).   
23692 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 53   if( sqlite3FixS
23693 65 6c 65 63 74 28 70 46 69 78 2c 20 70 49 74 65  elect(pFix, pIte
23694 6d 2d 3e 70 53 65 6c 65 63 74 29 20 29 20 72 65  m->pSelect) ) re
23695 74 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20  turn 1;.    if( 
23696 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70  sqlite3FixExpr(p
23697 46 69 78 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29  Fix, pItem->pOn)
23698 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e   ) return 1;.#en
23699 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  dif.  }.  return
2369a 20 30 3b 0a 7d 0a 23 69 66 20 21 64 65 66 69 6e   0;.}.#if !defin
2369b 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2369c 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  IEW) || !defined
2369d 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
2369e 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52 49  GGER).SQLITE_PRI
2369f 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
236a0 46 69 78 53 65 6c 65 63 74 28 0a 20 20 44 62 46  FixSelect(.  DbF
236a1 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20  ixer *pFix,     
236a2 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20    /* Context of 
236a3 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a  the fixation */.
236a4 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
236a5 74 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45  t      /* The SE
236a6 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74  LECT statement t
236a7 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e  o be fixed to on
236a8 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b  e database */.){
236a9 0a 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63  .  while( pSelec
236aa 74 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  t ){.    if( sql
236ab 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28  ite3FixExprList(
236ac 70 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70  pFix, pSelect->p
236ad 45 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20 20  EList) ){.      
236ae 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
236af 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
236b0 69 78 53 72 63 4c 69 73 74 28 70 46 69 78 2c 20  ixSrcList(pFix, 
236b1 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 29 20 29  pSelect->pSrc) )
236b2 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
236b3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
236b4 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70  sqlite3FixExpr(p
236b5 46 69 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 57  Fix, pSelect->pW
236b6 68 65 72 65 29 20 29 7b 0a 20 20 20 20 20 20 72  here) ){.      r
236b7 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
236b8 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
236b9 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 65 6c  xExpr(pFix, pSel
236ba 65 63 74 2d 3e 70 48 61 76 69 6e 67 29 20 29 7b  ect->pHaving) ){
236bb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
236bc 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 65 6c 65  .    }.    pSele
236bd 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50  ct = pSelect->pP
236be 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rior;.  }.  retu
236bf 72 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  rn 0;.}.SQLITE_P
236c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
236c1 65 33 46 69 78 45 78 70 72 28 0a 20 20 44 62 46  e3FixExpr(.  DbF
236c2 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20  ixer *pFix,     
236c3 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68  /* Context of th
236c4 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20  e fixation */.  
236c5 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20  Expr *pExpr     
236c6 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
236c7 73 69 6f 6e 20 74 6f 20 62 65 20 66 69 78 65 64  sion to be fixed
236c8 20 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65   to one database
236c9 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20   */.){.  while( 
236ca 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28  pExpr ){.    if(
236cb 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65   ExprHasAnyPrope
236cc 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54 6f  rty(pExpr, EP_To
236cd 6b 65 6e 4f 6e 6c 79 29 20 29 20 62 72 65 61 6b  kenOnly) ) break
236ce 3b 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61  ;.    if( ExprHa
236cf 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
236d0 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
236d1 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
236d2 74 65 33 46 69 78 53 65 6c 65 63 74 28 70 46 69  te3FixSelect(pFi
236d3 78 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c  x, pExpr->x.pSel
236d4 65 63 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b  ect) ) return 1;
236d5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
236d6 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
236d7 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70  ExprList(pFix, p
236d8 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29 20 29  Expr->x.pList) )
236d9 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
236da 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
236db 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 45  FixExpr(pFix, pE
236dc 78 70 72 2d 3e 70 52 69 67 68 74 29 20 29 7b 0a  xpr->pRight) ){.
236dd 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
236de 20 20 20 20 7d 0a 20 20 20 20 70 45 78 70 72 20      }.    pExpr 
236df 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a  = pExpr->pLeft;.
236e0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
236e1 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
236e2 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45   int sqlite3FixE
236e3 78 70 72 4c 69 73 74 28 0a 20 20 44 62 46 69 78  xprList(.  DbFix
236e4 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a  er *pFix,     /*
236e5 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   Context of the 
236e6 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78  fixation */.  Ex
236e7 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 20 20  prList *pList   
236e8 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
236e9 6f 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20 74  on to be fixed t
236ea 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a  o one database *
236eb 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
236ec 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
236ed 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69  item *pItem;.  i
236ee 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
236ef 74 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 3d  turn 0;.  for(i=
236f0 30 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e  0, pItem=pList->
236f1 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  a; i<pList->nExp
236f2 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  r; i++, pItem++)
236f3 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
236f4 33 46 69 78 45 78 70 72 28 70 46 69 78 2c 20 70  3FixExpr(pFix, p
236f5 49 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 7b 0a  Item->pExpr) ){.
236f6 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
236f7 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
236f8 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  rn 0;.}.#endif..
236f9 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
236fa 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c 49  MIT_TRIGGER.SQLI
236fb 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
236fc 71 6c 69 74 65 33 46 69 78 54 72 69 67 67 65 72  qlite3FixTrigger
236fd 53 74 65 70 28 0a 20 20 44 62 46 69 78 65 72 20  Step(.  DbFixer 
236fe 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f  *pFix,     /* Co
236ff 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78  ntext of the fix
23700 61 74 69 6f 6e 20 2a 2f 0a 20 20 54 72 69 67 67  ation */.  Trigg
23701 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 2f 2a  erStep *pStep /*
23702 20 54 68 65 20 74 72 69 67 67 65 72 20 73 74 65   The trigger ste
23703 70 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f 6e  p be fixed to on
23704 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b  e database */.){
23705 0a 20 20 77 68 69 6c 65 28 20 70 53 74 65 70 20  .  while( pStep 
23706 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
23707 65 33 46 69 78 53 65 6c 65 63 74 28 70 46 69 78  e3FixSelect(pFix
23708 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 74  , pStep->pSelect
23709 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
2370a 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  n 1;.    }.    i
2370b 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
2370c 72 28 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70  r(pFix, pStep->p
2370d 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 20 20  Where) ){.      
2370e 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
2370f 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
23710 69 78 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c  ixExprList(pFix,
23711 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73   pStep->pExprLis
23712 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  t) ){.      retu
23713 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
23714 70 53 74 65 70 20 3d 20 70 53 74 65 70 2d 3e 70  pStep = pStep->p
23715 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Next;.  }.  retu
23716 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  rn 0;.}.#endif..
23717 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
23718 45 6e 64 20 6f 66 20 61 74 74 61 63 68 2e 63 20  End of attach.c 
23719 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2371a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2371b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2371c 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2371d 42 65 67 69 6e 20 66 69 6c 65 20 61 75 74 68 2e  Begin file auth.
2371e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2371f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
23721 2f 2a 0a 2a 2a 20 32 30 30 33 20 4a 61 6e 75 61  /*.** 2003 Janua
23722 72 79 20 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ry 11.**.** The 
23723 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
23724 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
23725 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
23726 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
23727 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
23728 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
23729 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2372a 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2372b 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2372c 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2372d 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2372e 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2372f 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
23730 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
23731 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
23732 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
23733 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
23734 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23735 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23736 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23737 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23738 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
23739 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
2373a 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
2373b 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 65  t the sqlite3_se
2373c 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 0a 2a  t_authorizer().*
2373d 2a 20 41 50 49 2e 20 20 54 68 69 73 20 66 61 63  * API.  This fac
2373e 69 6c 69 74 79 20 69 73 20 61 6e 20 6f 70 74 69  ility is an opti
2373f 6f 6e 61 6c 20 66 65 61 74 75 72 65 20 6f 66 20  onal feature of 
23740 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20 45 6d  the library.  Em
23741 62 65 64 64 65 64 0a 2a 2a 20 73 79 73 74 65 6d  bedded.** system
23742 73 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 6e 65  s that do not ne
23743 65 64 20 74 68 69 73 20 66 61 63 69 6c 69 74 79  ed this facility
23744 20 6d 61 79 20 6f 6d 69 74 20 69 74 20 62 79 20   may omit it by 
23745 72 65 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 74  recompiling.** t
23746 68 65 20 6c 69 62 72 61 72 79 20 77 69 74 68 20  he library with 
23747 2d 44 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  -DSQLITE_OMIT_AU
23748 54 48 4f 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a 2f  THORIZATION=1.*/
23749 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74  ../*.** All of t
2374a 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
2374b 66 69 6c 65 20 6d 61 79 20 62 65 20 6f 6d 69 74  file may be omit
2374c 74 65 64 20 62 79 20 64 65 66 69 6e 69 6e 67 20  ted by defining 
2374d 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d 61 63 72  a single.** macr
2374e 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  o..*/.#ifndef SQ
2374f 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
23750 49 5a 41 54 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53  IZATION../*.** S
23751 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68 65 20  et or clear the 
23752 61 63 63 65 73 73 20 61 75 74 68 6f 72 69 7a 61  access authoriza
23753 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  tion function..*
23754 2a 0a 2a 2a 20 54 68 65 20 61 63 63 65 73 73 20  *.** The access 
23755 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75  authorization fu
23756 6e 63 74 69 6f 6e 20 69 73 20 62 65 20 63 61 6c  nction is be cal
23757 6c 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 63  led during the c
23758 6f 6d 70 69 6c 61 74 69 6f 6e 0a 2a 2a 20 70 68  ompilation.** ph
23759 61 73 65 20 74 6f 20 76 65 72 69 66 79 20 74 68  ase to verify th
2375a 61 74 20 74 68 65 20 75 73 65 72 20 68 61 73 20  at the user has 
2375b 72 65 61 64 20 61 6e 64 2f 6f 72 20 77 72 69 74  read and/or writ
2375c 65 20 61 63 63 65 73 73 20 70 65 72 6d 69 73 73  e access permiss
2375d 69 6f 6e 20 6f 6e 0a 2a 2a 20 76 61 72 69 6f 75  ion on.** variou
2375e 73 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20  s fields of the 
2375f 64 61 74 61 62 61 73 65 2e 20 20 54 68 65 20 66  database.  The f
23760 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
23761 20 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69   the auth functi
23762 6f 6e 0a 2a 2a 20 69 73 20 61 20 63 6f 70 79 20  on.** is a copy 
23763 6f 66 20 74 68 65 20 33 72 64 20 61 72 67 75 6d  of the 3rd argum
23764 65 6e 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74  ent to this rout
23765 69 6e 65 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  ine.  The second
23766 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20   argument.** to 
23767 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f  the auth functio
23768 6e 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73  n is one of thes
23769 65 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a  e constants:.**.
2376a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
2376b 43 52 45 41 54 45 5f 49 4e 44 45 58 0a 2a 2a 20  CREATE_INDEX.** 
2376c 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45        SQLITE_CRE
2376d 41 54 45 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20  ATE_TABLE.**    
2376e 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45     SQLITE_CREATE
2376f 5f 54 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a 20 20  _TEMP_INDEX.**  
23770 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41       SQLITE_CREA
23771 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a  TE_TEMP_TABLE.**
23772 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
23773 45 41 54 45 5f 54 45 4d 50 5f 54 52 49 47 47 45  EATE_TEMP_TRIGGE
23774 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  R.**       SQLIT
23775 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49  E_CREATE_TEMP_VI
23776 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  EW.**       SQLI
23777 54 45 5f 43 52 45 41 54 45 5f 54 52 49 47 47 45  TE_CREATE_TRIGGE
23778 52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  R.**       SQLIT
23779 45 5f 43 52 45 41 54 45 5f 56 49 45 57 0a 2a 2a  E_CREATE_VIEW.**
2377a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45         SQLITE_DE
2377b 4c 45 54 45 0a 2a 2a 20 20 20 20 20 20 20 53 51  LETE.**       SQ
2377c 4c 49 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 0a  LITE_DROP_INDEX.
2377d 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f  **       SQLITE_
2377e 44 52 4f 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20  DROP_TABLE.**   
2377f 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f      SQLITE_DROP_
23780 54 45 4d 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20  TEMP_INDEX.**   
23781 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f      SQLITE_DROP_
23782 54 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20  TEMP_TABLE.**   
23783 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f      SQLITE_DROP_
23784 54 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a 20  TEMP_TRIGGER.** 
23785 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f        SQLITE_DRO
23786 50 5f 54 45 4d 50 5f 56 49 45 57 0a 2a 2a 20 20  P_TEMP_VIEW.**  
23787 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
23788 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20  _TRIGGER.**     
23789 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49    SQLITE_DROP_VI
2378a 45 57 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49  EW.**       SQLI
2378b 54 45 5f 49 4e 53 45 52 54 0a 2a 2a 20 20 20 20  TE_INSERT.**    
2378c 20 20 20 53 51 4c 49 54 45 5f 50 52 41 47 4d 41     SQLITE_PRAGMA
2378d 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
2378e 5f 52 45 41 44 0a 2a 2a 20 20 20 20 20 20 20 53  _READ.**       S
2378f 51 4c 49 54 45 5f 53 45 4c 45 43 54 0a 2a 2a 20  QLITE_SELECT.** 
23790 20 20 20 20 20 20 53 51 4c 49 54 45 5f 54 52 41        SQLITE_TRA
23791 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 20 20 20  NSACTION.**     
23792 20 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 0a    SQLITE_UPDATE.
23793 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20  **.** The third 
23794 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d  and fourth argum
23795 65 6e 74 73 20 74 6f 20 74 68 65 20 61 75 74 68  ents to the auth
23796 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68   function are th
23797 65 20 6e 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65  e name of.** the
23798 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 63   table and the c
23799 6f 6c 75 6d 6e 20 74 68 61 74 20 61 72 65 20 62  olumn that are b
2379a 65 69 6e 67 20 61 63 63 65 73 73 65 64 2e 20 20  eing accessed.  
2379b 54 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f  The auth functio
2379c 6e 0a 2a 2a 20 73 68 6f 75 6c 64 20 72 65 74 75  n.** should retu
2379d 72 6e 20 65 69 74 68 65 72 20 53 51 4c 49 54 45  rn either SQLITE
2379e 5f 4f 4b 2c 20 53 51 4c 49 54 45 5f 44 45 4e 59  _OK, SQLITE_DENY
2379f 2c 20 6f 72 20 53 51 4c 49 54 45 5f 49 47 4e 4f  , or SQLITE_IGNO
237a0 52 45 2e 20 20 49 66 0a 2a 2a 20 53 51 4c 49 54  RE.  If.** SQLIT
237a1 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
237a2 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
237a3 61 63 63 65 73 73 20 69 73 20 61 6c 6c 6f 77 65  access is allowe
237a4 64 2e 20 20 53 51 4c 49 54 45 5f 44 45 4e 59 0a  d.  SQLITE_DENY.
237a5 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ** means that th
237a6 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
237a7 77 69 6c 6c 20 6e 65 76 65 72 2d 72 75 6e 20 2d  will never-run -
237a8 20 74 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65   the sqlite3_exe
237a9 63 28 29 20 63 61 6c 6c 0a 2a 2a 20 77 69 6c 6c  c() call.** will
237aa 20 72 65 74 75 72 6e 20 77 69 74 68 20 61 6e 20   return with an 
237ab 65 72 72 6f 72 2e 20 20 53 51 4c 49 54 45 5f 49  error.  SQLITE_I
237ac 47 4e 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74  GNORE means that
237ad 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
237ae 6e 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 72 75 6e  nt.** should run
237af 20 62 75 74 20 61 74 74 65 6d 70 74 73 20 74 6f   but attempts to
237b0 20 72 65 61 64 20 74 68 65 20 73 70 65 63 69 66   read the specif
237b1 69 65 64 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20  ied column will 
237b2 72 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 61  return NULL.** a
237b3 6e 64 20 61 74 74 65 6d 70 74 73 20 74 6f 20 77  nd attempts to w
237b4 72 69 74 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20  rite the column 
237b5 77 69 6c 6c 20 62 65 20 69 67 6e 6f 72 65 64 2e  will be ignored.
237b6 0a 2a 2a 0a 2a 2a 20 53 65 74 74 69 6e 67 20 74  .**.** Setting t
237b7 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e  he auth function
237b8 20 74 6f 20 4e 55 4c 4c 20 64 69 73 61 62 6c 65   to NULL disable
237b9 73 20 74 68 69 73 20 68 6f 6f 6b 2e 20 20 54 68  s this hook.  Th
237ba 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 73 65 74  e default.** set
237bb 74 69 6e 67 20 6f 66 20 74 68 65 20 61 75 74 68  ting of the auth
237bc 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55 4c   function is NUL
237bd 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  L..*/.SQLITE_API
237be 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74   int sqlite3_set
237bf 5f 61 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73  _authorizer(.  s
237c0 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 69 6e  qlite3 *db,.  in
237c1 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a  t (*xAuth)(void*
237c2 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
237c3 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
237c4 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
237c5 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 70  har*),.  void *p
237c6 41 72 67 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33  Arg.){.  sqlite3
237c7 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
237c8 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 78  >mutex);.  db->x
237c9 41 75 74 68 20 3d 20 78 41 75 74 68 3b 0a 20 20  Auth = xAuth;.  
237ca 64 62 2d 3e 70 41 75 74 68 41 72 67 20 3d 20 70  db->pAuthArg = p
237cb 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  Arg;.  sqlite3Ex
237cc 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
237cd 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 73 71  ements(db);.  sq
237ce 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
237cf 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
237d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
237d1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
237d2 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
237d3 65 20 69 6e 74 6f 20 70 50 61 72 73 65 2d 3e 7a  e into pParse->z
237d4 45 72 72 4d 73 67 20 74 68 61 74 20 65 78 70 6c  ErrMsg that expl
237d5 61 69 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a  ains that the.**
237d6 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20 61   user-supplied a
237d7 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e  uthorization fun
237d8 63 74 69 6f 6e 20 72 65 74 75 72 6e 65 64 20 61  ction returned a
237d9 6e 20 69 6c 6c 65 67 61 6c 20 76 61 6c 75 65 2e  n illegal value.
237da 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
237db 73 71 6c 69 74 65 41 75 74 68 42 61 64 52 65 74  sqliteAuthBadRet
237dc 75 72 6e 43 6f 64 65 28 50 61 72 73 65 20 2a 70  urnCode(Parse *p
237dd 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
237de 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
237df 2c 20 22 61 75 74 68 6f 72 69 7a 65 72 20 6d 61  , "authorizer ma
237e0 6c 66 75 6e 63 74 69 6f 6e 22 29 3b 0a 20 20 70  lfunction");.  p
237e1 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
237e2 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a  TE_ERROR;.}../*.
237e3 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75  ** Invoke the au
237e4 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c  thorization call
237e5 62 61 63 6b 20 66 6f 72 20 70 65 72 6d 69 73 73  back for permiss
237e6 69 6f 6e 20 74 6f 20 72 65 61 64 20 63 6f 6c 75  ion to read colu
237e7 6d 6e 20 7a 43 6f 6c 20 66 72 6f 6d 0a 2a 2a 20  mn zCol from.** 
237e8 74 61 62 6c 65 20 7a 54 61 62 20 69 6e 20 64 61  table zTab in da
237e9 74 61 62 61 73 65 20 7a 44 62 2e 20 54 68 69 73  tabase zDb. This
237ea 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65   function assume
237eb 73 20 74 68 61 74 20 61 6e 20 61 75 74 68 6f 72  s that an author
237ec 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62  ization.** callb
237ed 61 63 6b 20 68 61 73 20 62 65 65 6e 20 72 65 67  ack has been reg
237ee 69 73 74 65 72 65 64 20 28 69 2e 65 2e 20 74 68  istered (i.e. th
237ef 61 74 20 73 71 6c 69 74 65 33 2e 78 41 75 74 68  at sqlite3.xAuth
237f0 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 2e 0a 2a   is not NULL)..*
237f1 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 49  *.** If SQLITE_I
237f2 47 4e 4f 52 45 20 69 73 20 72 65 74 75 72 6e 65  GNORE is returne
237f3 64 20 61 6e 64 20 70 45 78 70 72 20 69 73 20 6e  d and pExpr is n
237f4 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 45  ot NULL, then pE
237f5 78 70 72 20 69 73 20 63 68 61 6e 67 65 64 0a 2a  xpr is changed.*
237f6 2a 20 74 6f 20 61 6e 20 53 51 4c 20 4e 55 4c 4c  * to an SQL NULL
237f7 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4f 74 68   expression. Oth
237f8 65 72 77 69 73 65 2c 20 69 66 20 70 45 78 70 72  erwise, if pExpr
237f9 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53   is NULL, then S
237fa 51 4c 49 54 45 5f 49 47 4e 4f 52 45 0a 2a 2a 20  QLITE_IGNORE.** 
237fb 69 73 20 74 72 65 61 74 65 64 20 61 73 20 53 51  is treated as SQ
237fc 4c 49 54 45 5f 44 45 4e 59 2e 20 49 6e 20 74 68  LITE_DENY. In th
237fd 69 73 20 63 61 73 65 20 61 6e 20 65 72 72 6f 72  is case an error
237fe 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50 61 72   is left in pPar
237ff 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
23800 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
23801 33 41 75 74 68 52 65 61 64 43 6f 6c 28 0a 20 20  3AuthReadCol(.  
23802 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
23803 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23804 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
23805 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ntext */.  const
23806 20 63 68 61 72 20 2a 7a 54 61 62 2c 20 20 20 20   char *zTab,    
23807 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
23808 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f  ble name */.  co
23809 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 2c 20  nst char *zCol, 
2380a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2380b 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a   Column name */.
2380c 20 20 69 6e 74 20 69 44 62 20 20 20 20 20 20 20    int iDb       
2380d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2380e 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f    /* Index of co
2380f 6e 74 61 69 6e 69 6e 67 20 64 61 74 61 62 61 73  ntaining databas
23810 65 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  e. */.){.  sqlit
23811 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
23812 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61  >db;       /* Da
23813 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
23814 0a 20 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64  .  char *zDb = d
23815 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
23816 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 61 74  e; /* Name of at
23817 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20  tached database 
23818 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
23819 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2381a 20 20 20 20 20 2f 2a 20 41 75 74 68 20 63 61 6c       /* Auth cal
2381b 6c 62 61 63 6b 20 72 65 74 75 72 6e 20 63 6f 64  lback return cod
2381c 65 20 2a 2f 0a 0a 20 20 72 63 20 3d 20 64 62 2d  e */..  rc = db-
2381d 3e 78 41 75 74 68 28 64 62 2d 3e 70 41 75 74 68  >xAuth(db->pAuth
2381e 41 72 67 2c 20 53 51 4c 49 54 45 5f 52 45 41 44  Arg, SQLITE_READ
2381f 2c 20 7a 54 61 62 2c 7a 43 6f 6c 2c 7a 44 62 2c  , zTab,zCol,zDb,
23820 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e  pParse->zAuthCon
23821 74 65 78 74 29 3b 0a 20 20 69 66 28 20 72 63 3d  text);.  if( rc=
23822 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a  =SQLITE_DENY ){.
23823 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e      if( db->nDb>
23824 32 20 7c 7c 20 69 44 62 21 3d 30 20 29 7b 0a 20  2 || iDb!=0 ){. 
23825 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
23826 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 63  rMsg(pParse, "ac
23827 63 65 73 73 20 74 6f 20 25 73 2e 25 73 2e 25 73  cess to %s.%s.%s
23828 20 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c   is prohibited",
23829 7a 44 62 2c 7a 54 61 62 2c 7a 43 6f 6c 29 3b 0a  zDb,zTab,zCol);.
2382a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2382b 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2382c 28 70 50 61 72 73 65 2c 20 22 61 63 63 65 73 73  (pParse, "access
2382d 20 74 6f 20 25 73 2e 25 73 20 69 73 20 70 72 6f   to %s.%s is pro
2382e 68 69 62 69 74 65 64 22 2c 20 7a 54 61 62 2c 20  hibited", zTab, 
2382f 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20  zCol);.    }.   
23830 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51   pParse->rc = SQ
23831 4c 49 54 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c  LITE_AUTH;.  }el
23832 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  se if( rc!=SQLIT
23833 45 5f 49 47 4e 4f 52 45 20 26 26 20 72 63 21 3d  E_IGNORE && rc!=
23834 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
23835 20 73 71 6c 69 74 65 41 75 74 68 42 61 64 52 65   sqliteAuthBadRe
23836 74 75 72 6e 43 6f 64 65 28 70 50 61 72 73 65 29  turnCode(pParse)
23837 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
23838 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
23839 70 45 78 70 72 20 73 68 6f 75 6c 64 20 62 65 20  pExpr should be 
2383a 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72  a TK_COLUMN expr
2383b 65 73 73 69 6f 6e 2e 20 20 54 68 65 20 74 61 62  ession.  The tab
2383c 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f 0a 2a  le referred to.*
2383d 2a 20 69 73 20 69 6e 20 70 54 61 62 4c 69 73 74  * is in pTabList
2383e 20 6f 72 20 65 6c 73 65 20 69 74 20 69 73 20 74   or else it is t
2383f 68 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61  he NEW or OLD ta
23840 62 6c 65 20 6f 66 20 61 20 74 72 69 67 67 65 72  ble of a trigger
23841 2e 20 20 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  .  .** Check to 
23842 73 65 65 20 69 66 20 69 74 20 69 73 20 4f 4b 20  see if it is OK 
23843 74 6f 20 72 65 61 64 20 74 68 69 73 20 70 61 72  to read this par
23844 74 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 2e 0a  ticular column..
23845 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 61 75 74  **.** If the aut
23846 68 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  h function retur
23847 6e 73 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  ns SQLITE_IGNORE
23848 2c 20 63 68 61 6e 67 65 20 74 68 65 20 54 4b 5f  , change the TK_
23849 43 4f 4c 55 4d 4e 20 0a 2a 2a 20 69 6e 73 74 72  COLUMN .** instr
2384a 75 63 74 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b  uction into a TK
2384b 5f 4e 55 4c 4c 2e 20 20 49 66 20 74 68 65 20 61  _NULL.  If the a
2384c 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  uth function ret
2384d 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 45 4e 59  urns SQLITE_DENY
2384e 2c 0a 2a 2a 20 74 68 65 6e 20 67 65 6e 65 72 61  ,.** then genera
2384f 74 65 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  te an error..*/.
23850 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
23851 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 52  oid sqlite3AuthR
23852 65 61 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ead(.  Parse *pP
23853 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
23854 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65  The parser conte
23855 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45  xt */.  Expr *pE
23856 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  xpr,          /*
23857 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
23858 74 6f 20 63 68 65 63 6b 20 61 75 74 68 6f 72 69  to check authori
23859 7a 61 74 69 6f 6e 20 6f 6e 20 2a 2f 0a 20 20 53  zation on */.  S
2385a 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 2c 20  chema *pSchema, 
2385b 20 20 20 20 20 2f 2a 20 54 68 65 20 73 63 68 65       /* The sche
2385c 6d 61 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ma of the expres
2385d 73 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c 69 73  sion */.  SrcLis
2385e 74 20 2a 70 54 61 62 4c 69 73 74 20 20 20 20 20  t *pTabList     
2385f 2f 2a 20 41 6c 6c 20 74 61 62 6c 65 20 74 68 61  /* All table tha
23860 74 20 70 45 78 70 72 20 6d 69 67 68 74 20 72 65  t pExpr might re
23861 66 65 72 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73  fer to */.){.  s
23862 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
23863 72 73 65 2d 3e 64 62 3b 0a 20 20 54 61 62 6c 65  rse->db;.  Table
23864 20 2a 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20   *pTab = 0;     
23865 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65   /* The table be
23866 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f  ing read */.  co
23867 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20  nst char *zCol; 
23868 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
23869 68 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  he column of the
2386a 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
2386b 69 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  iSrc;           
2386c 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 54    /* Index in pT
2386d 61 62 4c 69 73 74 2d 3e 61 5b 5d 20 6f 66 20 74  abList->a[] of t
2386e 61 62 6c 65 20 62 65 69 6e 67 20 72 65 61 64 20  able being read 
2386f 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
23870 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
23871 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64  e index of the d
23872 61 74 61 62 61 73 65 20 74 68 65 20 65 78 70 72  atabase the expr
23873 65 73 73 69 6f 6e 20 72 65 66 65 72 73 20 74 6f  ession refers to
23874 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 20   */.  int iCol; 
23875 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
23876 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69  ndex of column i
23877 6e 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 69 66  n table */..  if
23878 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20 29  ( db->xAuth==0 )
23879 20 72 65 74 75 72 6e 3b 0a 20 20 69 44 62 20 3d   return;.  iDb =
2387a 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2387b 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
2387c 2c 20 70 53 63 68 65 6d 61 29 3b 0a 20 20 69 66  , pSchema);.  if
2387d 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20 20 2f  ( iDb<0 ){.    /
2387e 2a 20 41 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  * An attempt to 
2387f 72 65 61 64 20 61 20 63 6f 6c 75 6d 6e 20 6f 75  read a column ou
23880 74 20 6f 66 20 61 20 73 75 62 71 75 65 72 79 20  t of a subquery 
23881 6f 72 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20  or other.    ** 
23882 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e  temporary table.
23883 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   */.    return;.
23884 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70    }..  assert( p
23885 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Expr->op==TK_COL
23886 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70  UMN || pExpr->op
23887 3d 3d 54 4b 5f 54 52 49 47 47 45 52 20 29 3b 0a  ==TK_TRIGGER );.
23888 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d    if( pExpr->op=
23889 3d 54 4b 5f 54 52 49 47 47 45 52 20 29 7b 0a 20  =TK_TRIGGER ){. 
2388a 20 20 20 70 54 61 62 20 3d 20 70 50 61 72 73 65     pTab = pParse
2388b 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3b 0a 20  ->pTriggerTab;. 
2388c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
2388d 72 74 28 20 70 54 61 62 4c 69 73 74 20 29 3b 0a  rt( pTabList );.
2388e 20 20 20 20 66 6f 72 28 69 53 72 63 3d 30 3b 20      for(iSrc=0; 
2388f 41 4c 57 41 59 53 28 69 53 72 63 3c 70 54 61 62  ALWAYS(iSrc<pTab
23890 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 20 69 53 72  List->nSrc); iSr
23891 63 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  c++){.      if( 
23892 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70  pExpr->iTable==p
23893 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63 5d  TabList->a[iSrc]
23894 2e 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20  .iCursor ){.    
23895 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c      pTab = pTabL
23896 69 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 70 54 61  ist->a[iSrc].pTa
23897 62 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  b;.        break
23898 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
23899 20 20 7d 0a 20 20 69 43 6f 6c 20 3d 20 70 45 78    }.  iCol = pEx
2389a 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 69  pr->iColumn;.  i
2389b 66 28 20 4e 45 56 45 52 28 70 54 61 62 3d 3d 30  f( NEVER(pTab==0
2389c 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69  ) ) return;..  i
2389d 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20  f( iCol>=0 ){.  
2389e 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3c 70    assert( iCol<p
2389f 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20  Tab->nCol );.   
238a0 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43   zCol = pTab->aC
238a1 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a  ol[iCol].zName;.
238a2 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 61 62    }else if( pTab
238a3 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b 0a 20 20  ->iPKey>=0 ){.  
238a4 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
238a5 69 50 4b 65 79 3c 70 54 61 62 2d 3e 6e 43 6f 6c  iPKey<pTab->nCol
238a6 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 70   );.    zCol = p
238a7 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e  Tab->aCol[pTab->
238a8 69 50 4b 65 79 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  iPKey].zName;.  
238a9 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 43 6f 6c 20  }else{.    zCol 
238aa 3d 20 22 52 4f 57 49 44 22 3b 0a 20 20 7d 0a 20  = "ROWID";.  }. 
238ab 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
238ac 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
238ad 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 49  ;.  if( SQLITE_I
238ae 47 4e 4f 52 45 3d 3d 73 71 6c 69 74 65 33 41 75  GNORE==sqlite3Au
238af 74 68 52 65 61 64 43 6f 6c 28 70 50 61 72 73 65  thReadCol(pParse
238b0 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  , pTab->zName, z
238b1 43 6f 6c 2c 20 69 44 62 29 20 29 7b 0a 20 20 20  Col, iDb) ){.   
238b2 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f   pExpr->op = TK_
238b3 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  NULL;.  }.}../*.
238b4 2a 2a 20 44 6f 20 61 6e 20 61 75 74 68 6f 72 69  ** Do an authori
238b5 7a 61 74 69 6f 6e 20 63 68 65 63 6b 20 75 73 69  zation check usi
238b6 6e 67 20 74 68 65 20 63 6f 64 65 20 61 6e 64 20  ng the code and 
238b7 61 72 67 75 6d 65 6e 74 73 20 67 69 76 65 6e 2e  arguments given.
238b8 20 20 52 65 74 75 72 6e 0a 2a 2a 20 65 69 74 68    Return.** eith
238b9 65 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28 7a 65  er SQLITE_OK (ze
238ba 72 6f 29 20 6f 72 20 53 51 4c 49 54 45 5f 49 47  ro) or SQLITE_IG
238bb 4e 4f 52 45 20 6f 72 20 53 51 4c 49 54 45 5f 44  NORE or SQLITE_D
238bc 45 4e 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f  ENY.  If SQLITE_
238bd 44 45 4e 59 0a 2a 2a 20 69 73 20 72 65 74 75 72  DENY.** is retur
238be 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 65 72  ned, then the er
238bf 72 6f 72 20 63 6f 75 6e 74 20 61 6e 64 20 65 72  ror count and er
238c0 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70  ror message in p
238c1 50 61 72 73 65 20 61 72 65 0a 2a 2a 20 6d 6f 64  Parse are.** mod
238c2 69 66 69 65 64 20 61 70 70 72 6f 70 72 69 61 74  ified appropriat
238c3 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ely..*/.SQLITE_P
238c4 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
238c5 65 33 41 75 74 68 43 68 65 63 6b 28 0a 20 20 50  e3AuthCheck(.  P
238c6 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a 20 20  arse *pParse,.  
238c7 69 6e 74 20 63 6f 64 65 2c 0a 20 20 63 6f 6e 73  int code,.  cons
238c8 74 20 63 68 61 72 20 2a 7a 41 72 67 31 2c 0a 20  t char *zArg1,. 
238c9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72   const char *zAr
238ca 67 32 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  g2,.  const char
238cb 20 2a 7a 41 72 67 33 0a 29 7b 0a 20 20 73 71 6c   *zArg3.){.  sql
238cc 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
238cd 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 63 3b  e->db;.  int rc;
238ce 0a 0a 20 20 2f 2a 20 44 6f 6e 27 74 20 64 6f 20  ..  /* Don't do 
238cf 61 6e 79 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  any authorizatio
238d0 6e 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 20  n checks if the 
238d1 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 69 74  database is init
238d2 69 61 6c 69 73 69 6e 67 0a 20 20 2a 2a 20 6f 72  ialising.  ** or
238d3 20 69 66 20 74 68 65 20 70 61 72 73 65 72 20 69   if the parser i
238d4 73 20 62 65 69 6e 67 20 69 6e 76 6f 6b 65 64 20  s being invoked 
238d5 66 72 6f 6d 20 77 69 74 68 69 6e 20 73 71 6c 69  from within sqli
238d6 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
238d7 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ..  */.  if( db-
238d8 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 49 4e  >init.busy || IN
238d9 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b  _DECLARE_VTAB ){
238da 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
238db 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66  TE_OK;.  }..  if
238dc 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30 20 29  ( db->xAuth==0 )
238dd 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
238de 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63  ITE_OK;.  }.  rc
238df 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d   = db->xAuth(db-
238e0 3e 70 41 75 74 68 41 72 67 2c 20 63 6f 64 65 2c  >pAuthArg, code,
238e1 20 7a 41 72 67 31 2c 20 7a 41 72 67 32 2c 20 7a   zArg1, zArg2, z
238e2 41 72 67 33 2c 20 70 50 61 72 73 65 2d 3e 7a 41  Arg3, pParse->zA
238e3 75 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69  uthContext);.  i
238e4 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 45  f( rc==SQLITE_DE
238e5 4e 59 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  NY ){.    sqlite
238e6 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
238e7 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65  , "not authorize
238e8 64 22 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  d");.    pParse-
238e9 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 55 54  >rc = SQLITE_AUT
238ea 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72  H;.  }else if( r
238eb 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c!=SQLITE_OK && 
238ec 72 63 21 3d 53 51 4c 49 54 45 5f 49 47 4e 4f 52  rc!=SQLITE_IGNOR
238ed 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  E ){.    rc = SQ
238ee 4c 49 54 45 5f 44 45 4e 59 3b 0a 20 20 20 20 73  LITE_DENY;.    s
238ef 71 6c 69 74 65 41 75 74 68 42 61 64 52 65 74 75  qliteAuthBadRetu
238f0 72 6e 43 6f 64 65 28 70 50 61 72 73 65 29 3b 0a  rnCode(pParse);.
238f1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
238f2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 75 73 68 20 61  .}../*.** Push a
238f3 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  n authorization 
238f4 63 6f 6e 74 65 78 74 2e 20 20 41 66 74 65 72 20  context.  After 
238f5 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
238f6 63 61 6c 6c 65 64 2c 20 74 68 65 0a 2a 2a 20 7a  called, the.** z
238f7 41 72 67 33 20 61 72 67 75 6d 65 6e 74 20 74 6f  Arg3 argument to
238f8 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63   authorization c
238f9 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62 65  allbacks will be
238fa 20 7a 43 6f 6e 74 65 78 74 20 75 6e 74 69 6c 0a   zContext until.
238fb 2a 2a 20 70 6f 70 70 65 64 2e 20 20 4f 72 20 69  ** popped.  Or i
238fc 66 20 70 50 61 72 73 65 3d 3d 30 2c 20 74 68 69  f pParse==0, thi
238fd 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
238fe 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  o-op..*/.SQLITE_
238ff 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
23900 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50  ite3AuthContextP
23901 75 73 68 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ush(.  Parse *pP
23902 61 72 73 65 2c 0a 20 20 41 75 74 68 43 6f 6e 74  arse,.  AuthCont
23903 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20 0a  ext *pContext, .
23904 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
23905 6f 6e 74 65 78 74 0a 29 7b 0a 20 20 61 73 73 65  ontext.){.  asse
23906 72 74 28 20 70 50 61 72 73 65 20 29 3b 0a 20 20  rt( pParse );.  
23907 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65  pContext->pParse
23908 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70 43 6f   = pParse;.  pCo
23909 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74  ntext->zAuthCont
2390a 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a 41  ext = pParse->zA
2390b 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 70 50  uthContext;.  pP
2390c 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
2390d 78 74 20 3d 20 7a 43 6f 6e 74 65 78 74 3b 0a 7d  xt = zContext;.}
2390e 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 20 61 6e 20 61  ../*.** Pop an a
2390f 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e  uthorization con
23910 74 65 78 74 20 74 68 61 74 20 77 61 73 20 70 72  text that was pr
23911 65 76 69 6f 75 73 6c 79 20 70 75 73 68 65 64 0a  eviously pushed.
23912 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 41 75 74  ** by sqlite3Aut
23913 68 43 6f 6e 74 65 78 74 50 75 73 68 0a 2a 2f 0a  hContextPush.*/.
23914 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
23915 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 68 43  oid sqlite3AuthC
23916 6f 6e 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f  ontextPop(AuthCo
23917 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 29  ntext *pContext)
23918 7b 0a 20 20 69 66 28 20 70 43 6f 6e 74 65 78 74  {.  if( pContext
23919 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 20 20  ->pParse ){.    
2391a 70 43 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65  pContext->pParse
2391b 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d  ->zAuthContext =
2391c 20 70 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68   pContext->zAuth
2391d 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 70 43 6f  Context;.    pCo
2391e 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 20 3d 20  ntext->pParse = 
2391f 30 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66  0;.  }.}..#endif
23920 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
23921 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f  AUTHORIZATION */
23922 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
23923 2a 20 45 6e 64 20 6f 66 20 61 75 74 68 2e 63 20  * End of auth.c 
23924 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23925 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23926 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23927 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
23928 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62 75 69  * Begin file bui
23929 6c 64 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ld.c ***********
2392a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2392b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2392c 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
2392d 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
2392e 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
2392f 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
23930 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
23931 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
23932 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
23933 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
23934 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
23935 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
23936 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
23937 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
23938 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
23939 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
2393a 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
2393b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
2393c 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
2393d 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
2393e 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
2393f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23941 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23942 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23943 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
23944 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43   file contains C
23945 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74   code routines t
23946 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62  hat are called b
23947 79 20 74 68 65 20 53 51 4c 69 74 65 20 70 61 72  y the SQLite par
23948 73 65 72 0a 2a 2a 20 77 68 65 6e 20 73 79 6e 74  ser.** when synt
23949 61 78 20 72 75 6c 65 73 20 61 72 65 20 72 65 64  ax rules are red
2394a 75 63 65 64 2e 20 20 54 68 65 20 72 6f 75 74 69  uced.  The routi
2394b 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  nes in this file
2394c 20 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 66   handle the.** f
2394d 6f 6c 6c 6f 77 69 6e 67 20 6b 69 6e 64 73 20 6f  ollowing kinds o
2394e 66 20 53 51 4c 20 73 79 6e 74 61 78 3a 0a 2a 2a  f SQL syntax:.**
2394f 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54  .**     CREATE T
23950 41 42 4c 45 0a 2a 2a 20 20 20 20 20 44 52 4f 50  ABLE.**     DROP
23951 20 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 43 52   TABLE.**     CR
23952 45 41 54 45 20 49 4e 44 45 58 0a 2a 2a 20 20 20  EATE INDEX.**   
23953 20 20 44 52 4f 50 20 49 4e 44 45 58 0a 2a 2a 20    DROP INDEX.** 
23954 20 20 20 20 63 72 65 61 74 69 6e 67 20 49 44 20      creating ID 
23955 6c 69 73 74 73 0a 2a 2a 20 20 20 20 20 42 45 47  lists.**     BEG
23956 49 4e 20 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a  IN TRANSACTION.*
23957 2a 20 20 20 20 20 43 4f 4d 4d 49 54 0a 2a 2a 20  *     COMMIT.** 
23958 20 20 20 20 52 4f 4c 4c 42 41 43 4b 0a 2a 2f 0a      ROLLBACK.*/.
23959 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2395a 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ine is called wh
2395b 65 6e 20 61 20 6e 65 77 20 53 51 4c 20 73 74 61  en a new SQL sta
2395c 74 65 6d 65 6e 74 20 69 73 20 62 65 67 69 6e 6e  tement is beginn
2395d 69 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 70 61 72  ing to.** be par
2395e 73 65 64 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65  sed.  Initialize
2395f 20 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75   the pParse stru
23960 63 74 75 72 65 20 61 73 20 6e 65 65 64 65 64 2e  cture as needed.
23961 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23962 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
23963 65 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 20  eginParse(Parse 
23964 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 65 78 70  *pParse, int exp
23965 6c 61 69 6e 46 6c 61 67 29 7b 0a 20 20 70 50 61  lainFlag){.  pPa
23966 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 3d 20 28  rse->explain = (
23967 75 38 29 65 78 70 6c 61 69 6e 46 6c 61 67 3b 0a  u8)explainFlag;.
23968 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d    pParse->nVar =
23969 20 30 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53   0;.}..#ifndef S
2396a 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
2396b 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 54 68  D_CACHE./*.** Th
2396c 65 20 54 61 62 6c 65 4c 6f 63 6b 20 73 74 72 75  e TableLock stru
2396d 63 74 75 72 65 20 69 73 20 6f 6e 6c 79 20 75 73  cture is only us
2396e 65 64 20 62 79 20 74 68 65 20 73 71 6c 69 74 65  ed by the sqlite
2396f 33 54 61 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64  3TableLock() and
23970 0a 2a 2a 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63  .** codeTableLoc
23971 6b 73 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  ks() functions..
23972 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 4c  */.struct TableL
23973 6f 63 6b 20 7b 0a 20 20 69 6e 74 20 69 44 62 3b  ock {.  int iDb;
23974 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23975 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
23976 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c  taining the tabl
23977 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a  e to be locked *
23978 2f 0a 20 20 69 6e 74 20 69 54 61 62 3b 20 20 20  /.  int iTab;   
23979 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2397a 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65  root page of the
2397b 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63   table to be loc
2397c 6b 65 64 20 2a 2f 0a 20 20 75 38 20 69 73 57 72  ked */.  u8 isWr
2397d 69 74 65 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a  iteLock;      /*
2397e 20 54 72 75 65 20 66 6f 72 20 77 72 69 74 65 20   True for write 
2397f 6c 6f 63 6b 2e 20 20 46 61 6c 73 65 20 66 6f 72  lock.  False for
23980 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a   a read lock */.
23981 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
23982 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  ame;   /* Name o
23983 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d  f the table */.}
23984 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20  ;../*.** Record 
23985 74 68 65 20 66 61 63 74 20 74 68 61 74 20 77 65  the fact that we
23986 20 77 61 6e 74 20 74 6f 20 6c 6f 63 6b 20 61 20   want to lock a 
23987 74 61 62 6c 65 20 61 74 20 72 75 6e 2d 74 69 6d  table at run-tim
23988 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  e.  .**.** The t
23989 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65  able to be locke
2398a 64 20 68 61 73 20 72 6f 6f 74 20 70 61 67 65 20  d has root page 
2398b 69 54 61 62 20 61 6e 64 20 69 73 20 66 6f 75 6e  iTab and is foun
2398c 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44  d in database iD
2398d 62 2e 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20  b..** A read or 
2398e 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 63 61 6e  a write lock can
2398f 20 62 65 20 74 61 6b 65 6e 20 64 65 70 65 6e 64   be taken depend
23990 69 6e 67 20 6f 6e 20 69 73 57 72 69 74 65 6c 6f  ing on isWritelo
23991 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ck..**.** This r
23992 6f 75 74 69 6e 65 20 6a 75 73 74 20 72 65 63 6f  outine just reco
23993 72 64 73 20 74 68 65 20 66 61 63 74 20 74 68 61  rds the fact tha
23994 74 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 65  t the lock is de
23995 73 69 72 65 64 2e 20 20 54 68 65 0a 2a 2a 20 63  sired.  The.** c
23996 6f 64 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 20  ode to make the 
23997 6c 6f 63 6b 20 6f 63 63 75 72 20 69 73 20 67 65  lock occur is ge
23998 6e 65 72 61 74 65 64 20 62 79 20 61 20 6c 61 74  nerated by a lat
23999 65 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 63 6f  er call to.** co
2399a 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20 77  deTableLocks() w
2399b 68 69 63 68 20 6f 63 63 75 72 73 20 64 75 72 69  hich occurs duri
2399c 6e 67 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  ng sqlite3Finish
2399d 43 6f 64 69 6e 67 28 29 2e 0a 2a 2f 0a 53 51 4c  Coding()..*/.SQL
2399e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2399f 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
239a0 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  k(.  Parse *pPar
239a1 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
239a2 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
239a3 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20  int iDb,        
239a4 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74     /* Index of t
239a5 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  he database cont
239a6 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65  aining the table
239a7 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e   to lock */.  in
239a8 74 20 69 54 61 62 2c 20 20 20 20 20 20 20 20 20  t iTab,         
239a9 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6e 75   /* Root page nu
239aa 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c  mber of the tabl
239ab 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a  e to be locked *
239ac 2f 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f  /.  u8 isWriteLo
239ad 63 6b 2c 20 20 20 20 2f 2a 20 54 72 75 65 20 66  ck,    /* True f
239ae 6f 72 20 61 20 77 72 69 74 65 20 6c 6f 63 6b 20  or a write lock 
239af 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
239b0 2a 7a 4e 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 20  *zName  /* Name 
239b1 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
239b2 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 29 7b 0a  be locked */.){.
239b3 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
239b4 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  el = sqlite3Pars
239b5 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
239b6 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  );.  int i;.  in
239b7 74 20 6e 42 79 74 65 73 3b 0a 20 20 54 61 62 6c  t nBytes;.  Tabl
239b8 65 4c 6f 63 6b 20 2a 70 3b 0a 20 20 61 73 73 65  eLock *p;.  asse
239b9 72 74 28 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 20  rt( iDb>=0 );.. 
239ba 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 70   for(i=0; i<pTop
239bb 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63  level->nTableLoc
239bc 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d  k; i++){.    p =
239bd 20 26 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61   &pToplevel->aTa
239be 62 6c 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20  bleLock[i];.    
239bf 69 66 28 20 70 2d 3e 69 44 62 3d 3d 69 44 62 20  if( p->iDb==iDb 
239c0 26 26 20 70 2d 3e 69 54 61 62 3d 3d 69 54 61 62  && p->iTab==iTab
239c1 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 57   ){.      p->isW
239c2 72 69 74 65 4c 6f 63 6b 20 3d 20 28 70 2d 3e 69  riteLock = (p->i
239c3 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 69 73  sWriteLock || is
239c4 57 72 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20  WriteLock);.    
239c5 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
239c6 20 20 7d 0a 0a 20 20 6e 42 79 74 65 73 20 3d 20    }..  nBytes = 
239c7 73 69 7a 65 6f 66 28 54 61 62 6c 65 4c 6f 63 6b  sizeof(TableLock
239c8 29 20 2a 20 28 70 54 6f 70 6c 65 76 65 6c 2d 3e  ) * (pToplevel->
239c9 6e 54 61 62 6c 65 4c 6f 63 6b 2b 31 29 3b 0a 20  nTableLock+1);. 
239ca 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62   pToplevel->aTab
239cb 6c 65 4c 6f 63 6b 20 3d 0a 20 20 20 20 20 20 73  leLock =.      s
239cc 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f  qlite3DbReallocO
239cd 72 46 72 65 65 28 70 54 6f 70 6c 65 76 65 6c 2d  rFree(pToplevel-
239ce 3e 64 62 2c 20 70 54 6f 70 6c 65 76 65 6c 2d 3e  >db, pToplevel->
239cf 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e 42 79 74  aTableLock, nByt
239d0 65 73 29 3b 0a 20 20 69 66 28 20 70 54 6f 70 6c  es);.  if( pTopl
239d1 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  evel->aTableLock
239d2 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 54 6f   ){.    p = &pTo
239d3 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f  plevel->aTableLo
239d4 63 6b 5b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54  ck[pToplevel->nT
239d5 61 62 6c 65 4c 6f 63 6b 2b 2b 5d 3b 0a 20 20 20  ableLock++];.   
239d6 20 70 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20   p->iDb = iDb;. 
239d7 20 20 20 70 2d 3e 69 54 61 62 20 3d 20 69 54 61     p->iTab = iTa
239d8 62 3b 0a 20 20 20 20 70 2d 3e 69 73 57 72 69 74  b;.    p->isWrit
239d9 65 4c 6f 63 6b 20 3d 20 69 73 57 72 69 74 65 4c  eLock = isWriteL
239da 6f 63 6b 3b 0a 20 20 20 20 70 2d 3e 7a 4e 61 6d  ock;.    p->zNam
239db 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c  e = zName;.  }el
239dc 73 65 7b 0a 20 20 20 20 70 54 6f 70 6c 65 76 65  se{.    pTopleve
239dd 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 20 3d 20  l->nTableLock = 
239de 30 3b 0a 20 20 20 20 70 54 6f 70 6c 65 76 65 6c  0;.    pToplevel
239df 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
239e0 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ed = 1;.  }.}../
239e1 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f  *.** Code an OP_
239e2 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 75  TableLock instru
239e3 63 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74  ction for each t
239e4 61 62 6c 65 20 6c 6f 63 6b 65 64 20 62 79 20 74  able locked by t
239e5 68 65 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  he.** statement 
239e6 28 63 6f 6e 66 69 67 75 72 65 64 20 62 79 20 63  (configured by c
239e7 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 54  alls to sqlite3T
239e8 61 62 6c 65 4c 6f 63 6b 28 29 29 2e 0a 2a 2f 0a  ableLock())..*/.
239e9 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65  static void code
239ea 54 61 62 6c 65 4c 6f 63 6b 73 28 50 61 72 73 65  TableLocks(Parse
239eb 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74   *pParse){.  int
239ec 20 69 3b 0a 20 20 56 64 62 65 20 2a 70 56 64 62   i;.  Vdbe *pVdb
239ed 65 3b 20 0a 0a 20 20 70 56 64 62 65 20 3d 20 73  e; ..  pVdbe = s
239ee 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
239ef 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28  arse);.  assert(
239f0 20 70 56 64 62 65 21 3d 30 20 29 3b 20 2f 2a 20   pVdbe!=0 ); /* 
239f1 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 20 63  sqlite3GetVdbe c
239f2 61 6e 6e 6f 74 20 66 61 69 6c 3a 20 56 44 42 45  annot fail: VDBE
239f3 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74   already allocat
239f4 65 64 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 3d 30  ed */..  for(i=0
239f5 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62  ; i<pParse->nTab
239f6 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20  leLock; i++){.  
239f7 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d    TableLock *p =
239f8 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65   &pParse->aTable
239f9 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 6e 74  Lock[i];.    int
239fa 20 70 31 20 3d 20 70 2d 3e 69 44 62 3b 0a 20 20   p1 = p->iDb;.  
239fb 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
239fc 4f 70 34 28 70 56 64 62 65 2c 20 4f 50 5f 54 61  Op4(pVdbe, OP_Ta
239fd 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e  bleLock, p1, p->
239fe 69 54 61 62 2c 20 70 2d 3e 69 73 57 72 69 74 65  iTab, p->isWrite
239ff 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20  Lock,.          
23a00 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 7a              p->z
23a01 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
23a02 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20  ;.  }.}.#else.  
23a03 23 64 65 66 69 6e 65 20 63 6f 64 65 54 61 62 6c  #define codeTabl
23a04 65 4c 6f 63 6b 73 28 78 29 0a 23 65 6e 64 69 66  eLocks(x).#endif
23a05 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
23a06 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 61  tine is called a
23a07 66 74 65 72 20 61 20 73 69 6e 67 6c 65 20 53 51  fter a single SQ
23a08 4c 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  L statement has 
23a09 62 65 65 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61  been.** parsed a
23a0a 6e 64 20 61 20 56 44 42 45 20 70 72 6f 67 72 61  nd a VDBE progra
23a0b 6d 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 61  m to execute tha
23a0c 74 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  t statement has 
23a0d 62 65 65 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64  been.** prepared
23a0e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
23a0f 70 75 74 73 20 74 68 65 20 66 69 6e 69 73 68 69  puts the finishi
23a10 6e 67 20 74 6f 75 63 68 65 73 20 6f 6e 20 74 68  ng touches on th
23a11 65 0a 2a 2a 20 56 44 42 45 20 70 72 6f 67 72 61  e.** VDBE progra
23a12 6d 20 61 6e 64 20 72 65 73 65 74 73 20 74 68 65  m and resets the
23a13 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72   pParse structur
23a14 65 20 66 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a  e for the next.*
23a15 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e  * parse..**.** N
23a16 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e 20 65  ote that if an e
23a17 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 69  rror occurred, i
23a18 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 63  t might be the c
23a19 61 73 65 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56  ase that.** no V
23a1a 44 42 45 20 63 6f 64 65 20 77 61 73 20 67 65 6e  DBE code was gen
23a1b 65 72 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  erated..*/.SQLIT
23a1c 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23a1d 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69  qlite3FinishCodi
23a1e 6e 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ng(Parse *pParse
23a1f 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
23a20 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20  ;.  Vdbe *v;..  
23a21 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
23a22 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
23a23 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
23a24 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
23a25 6e 65 73 74 65 64 20 29 20 72 65 74 75 72 6e 3b  nested ) return;
23a26 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
23a27 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  Err ) return;.. 
23a28 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 67 65 6e   /* Begin by gen
23a29 65 72 61 74 69 6e 67 20 73 6f 6d 65 20 74 65 72  erating some ter
23a2a 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 20 61 74  mination code at
23a2b 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a   the end of the.
23a2c 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 72 61    ** vdbe progra
23a2d 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c  m.  */.  v = sql
23a2e 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
23a2f 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  se);.  assert( !
23a30 70 50 61 72 73 65 2d 3e 69 73 4d 75 6c 74 69 57  pParse->isMultiW
23a31 72 69 74 65 20 0a 20 20 20 20 20 20 20 7c 7c 20  rite .       || 
23a32 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65 72  sqlite3VdbeAsser
23a33 74 4d 61 79 41 62 6f 72 74 28 76 2c 20 70 50 61  tMayAbort(v, pPa
23a34 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29 29 3b  rse->mayAbort));
23a35 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20  .  if( v ){.    
23a36 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23a37 30 28 76 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a 0a  0(v, OP_Halt);..
23a38 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6f 6b 69      /* The cooki
23a39 65 20 6d 61 73 6b 20 63 6f 6e 74 61 69 6e 73 20  e mask contains 
23a3a 6f 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68  one bit for each
23a3b 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f   database file o
23a3c 70 65 6e 2e 0a 20 20 20 20 2a 2a 20 28 42 69 74  pen..    ** (Bit
23a3d 20 30 20 69 73 20 66 6f 72 20 6d 61 69 6e 2c 20   0 is for main, 
23a3e 62 69 74 20 31 20 69 73 20 66 6f 72 20 74 65 6d  bit 1 is for tem
23a3f 70 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e  p, and so forth.
23a40 29 20 20 42 69 74 73 20 61 72 65 0a 20 20 20 20  )  Bits are.    
23a41 2a 2a 20 73 65 74 20 66 6f 72 20 65 61 63 68 20  ** set for each 
23a42 64 61 74 61 62 61 73 65 20 74 68 61 74 20 69 73  database that is
23a43 20 75 73 65 64 2e 20 20 47 65 6e 65 72 61 74 65   used.  Generate
23a44 20 63 6f 64 65 20 74 6f 20 73 74 61 72 74 20 61   code to start a
23a45 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  .    ** transact
23a46 69 6f 6e 20 6f 6e 20 65 61 63 68 20 75 73 65 64  ion on each used
23a47 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 6f   database and to
23a48 20 76 65 72 69 66 79 20 74 68 65 20 73 63 68 65   verify the sche
23a49 6d 61 20 63 6f 6f 6b 69 65 0a 20 20 20 20 2a 2a  ma cookie.    **
23a4a 20 6f 6e 20 65 61 63 68 20 75 73 65 64 20 64 61   on each used da
23a4b 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20  tabase..    */. 
23a4c 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63     if( pParse->c
23a4d 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20 29 7b 0a 20  ookieGoto>0 ){. 
23a4e 20 20 20 20 20 75 33 32 20 6d 61 73 6b 3b 0a 20       u32 mask;. 
23a4f 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20       int iDb;.  
23a50 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
23a51 75 6d 70 48 65 72 65 28 76 2c 20 70 50 61 72 73  umpHere(v, pPars
23a52 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 2d 31 29  e->cookieGoto-1)
23a53 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 44 62 3d  ;.      for(iDb=
23a54 30 2c 20 6d 61 73 6b 3d 31 3b 20 69 44 62 3c 64  0, mask=1; iDb<d
23a55 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b 3c 3c 3d 31  b->nDb; mask<<=1
23a56 2c 20 69 44 62 2b 2b 29 7b 0a 20 20 20 20 20 20  , iDb++){.      
23a57 20 20 69 66 28 20 28 6d 61 73 6b 20 26 20 70 50    if( (mask & pP
23a58 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b  arse->cookieMask
23a59 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  )==0 ) continue;
23a5a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
23a5b 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
23a5c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 73   iDb);.        s
23a5d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23a5e 28 76 2c 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f  (v,OP_Transactio
23a5f 6e 2c 20 69 44 62 2c 20 28 6d 61 73 6b 20 26 20  n, iDb, (mask & 
23a60 70 50 61 72 73 65 2d 3e 77 72 69 74 65 4d 61 73  pParse->writeMas
23a61 6b 29 21 3d 30 29 3b 0a 20 20 20 20 20 20 20 20  k)!=0);.        
23a62 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  if( db->init.bus
23a63 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  y==0 ){.        
23a64 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23a65 4f 70 32 28 76 2c 4f 50 5f 56 65 72 69 66 79 43  Op2(v,OP_VerifyC
23a66 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 70 50 61 72  ookie, iDb, pPar
23a67 73 65 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75 65 5b  se->cookieValue[
23a68 69 44 62 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d  iDb]);.        }
23a69 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  .      }.#ifndef
23a6a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
23a6b 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20  TUALTABLE.      
23a6c 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b  {.        int i;
23a6d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
23a6e 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56 74 61  ; i<pParse->nVta
23a6f 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20  bLock; i++){.   
23a70 20 20 20 20 20 20 20 63 68 61 72 20 2a 76 74 61         char *vta
23a71 62 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  b = (char *)sqli
23a72 74 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c  te3GetVTable(db,
23a73 20 70 50 61 72 73 65 2d 3e 61 70 56 74 61 62 4c   pParse->apVtabL
23a74 6f 63 6b 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20  ock[i]);.       
23a75 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23a76 64 4f 70 34 28 76 2c 20 4f 50 5f 56 42 65 67 69  dOp4(v, OP_VBegi
23a77 6e 2c 20 30 2c 20 30 2c 20 30 2c 20 76 74 61 62  n, 0, 0, 0, vtab
23a78 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20  , P4_VTAB);.    
23a79 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 50      }.        pP
23a7a 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 20  arse->nVtabLock 
23a7b 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  = 0;.      }.#en
23a7c 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 6e  dif..      /* On
23a7d 63 65 20 61 6c 6c 20 74 68 65 20 63 6f 6f 6b 69  ce all the cooki
23a7e 65 73 20 68 61 76 65 20 62 65 65 6e 20 76 65 72  es have been ver
23a7f 69 66 69 65 64 20 61 6e 64 20 74 72 61 6e 73 61  ified and transa
23a80 63 74 69 6f 6e 73 20 6f 70 65 6e 65 64 2c 20 0a  ctions opened, .
23a81 20 20 20 20 20 20 2a 2a 20 6f 62 74 61 69 6e 20        ** obtain 
23a82 74 68 65 20 72 65 71 75 69 72 65 64 20 74 61 62  the required tab
23a83 6c 65 2d 6c 6f 63 6b 73 2e 20 54 68 69 73 20 69  le-locks. This i
23a84 73 20 61 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73  s a no-op unless
23a85 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 73   the .      ** s
23a86 68 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74  hared-cache feat
23a87 75 72 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a  ure is enabled..
23a88 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63        */.      c
23a89 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 70 50  odeTableLocks(pP
23a8a 61 72 73 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  arse);..      /*
23a8b 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 79 20   Initialize any 
23a8c 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 64 61  AUTOINCREMENT da
23a8d 74 61 20 73 74 72 75 63 74 75 72 65 73 20 72 65  ta structures re
23a8e 71 75 69 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f  quired..      */
23a8f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 75  .      sqlite3Au
23a90 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e  toincrementBegin
23a91 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 20  (pParse);..     
23a92 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 6a 75 6d   /* Finally, jum
23a93 70 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65  p back to the be
23a94 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 65  ginning of the e
23a95 78 65 63 75 74 61 62 6c 65 20 63 6f 64 65 2e 20  xecutable code. 
23a96 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
23a97 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
23a98 5f 47 6f 74 6f 2c 20 30 2c 20 70 50 61 72 73 65  _Goto, 0, pParse
23a99 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 29 3b 0a 20  ->cookieGoto);. 
23a9a 20 20 20 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20     }.  }...  /* 
23a9b 47 65 74 20 74 68 65 20 56 44 42 45 20 70 72 6f  Get the VDBE pro
23a9c 67 72 61 6d 20 72 65 61 64 79 20 66 6f 72 20 65  gram ready for e
23a9d 78 65 63 75 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20  xecution.  */.  
23a9e 69 66 28 20 76 20 26 26 20 41 4c 57 41 59 53 28  if( v && ALWAYS(
23a9f 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 29  pParse->nErr==0)
23aa0 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46   && !db->mallocF
23aa1 61 69 6c 65 64 20 29 7b 0a 23 69 66 64 65 66 20  ailed ){.#ifdef 
23aa2 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
23aa3 20 46 49 4c 45 20 2a 74 72 61 63 65 20 3d 20 28   FILE *trace = (
23aa4 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
23aa5 54 45 5f 56 64 62 65 54 72 61 63 65 29 21 3d 30  TE_VdbeTrace)!=0
23aa6 20 3f 20 73 74 64 6f 75 74 20 3a 20 30 3b 0a 20   ? stdout : 0;. 
23aa7 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 54 72     sqlite3VdbeTr
23aa8 61 63 65 28 76 2c 20 74 72 61 63 65 29 3b 0a 23  ace(v, trace);.#
23aa9 65 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74  endif.    assert
23aaa 28 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65  ( pParse->iCache
23aab 4c 65 76 65 6c 3d 3d 30 20 29 3b 20 20 2f 2a 20  Level==0 );  /* 
23aac 44 69 73 61 62 6c 65 73 20 61 6e 64 20 72 65 2d  Disables and re-
23aad 65 6e 61 62 6c 65 73 20 6d 61 74 63 68 20 2a 2f  enables match */
23aae 0a 20 20 20 20 2f 2a 20 41 20 6d 69 6e 69 6d 75  .    /* A minimu
23aaf 6d 20 6f 66 20 6f 6e 65 20 63 75 72 73 6f 72 20  m of one cursor 
23ab0 69 73 20 72 65 71 75 69 72 65 64 20 69 66 20 61  is required if a
23ab1 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 69 73 20  utoincrement is 
23ab2 75 73 65 64 0a 20 20 20 20 2a 20 20 53 65 65 20  used.    *  See 
23ab3 74 69 63 6b 65 74 20 5b 61 36 39 36 33 37 39 63  ticket [a696379c
23ab4 31 66 30 38 38 36 36 5d 20 2a 2f 0a 20 20 20 20  1f08866] */.    
23ab5 69 66 28 20 70 50 61 72 73 65 2d 3e 70 41 69 6e  if( pParse->pAin
23ab6 63 21 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e  c!=0 && pParse->
23ab7 6e 54 61 62 3d 3d 30 20 29 20 70 50 61 72 73 65  nTab==0 ) pParse
23ab8 2d 3e 6e 54 61 62 20 3d 20 31 3b 0a 20 20 20 20  ->nTab = 1;.    
23ab9 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52  sqlite3VdbeMakeR
23aba 65 61 64 79 28 76 2c 20 70 50 61 72 73 65 2d 3e  eady(v, pParse->
23abb 6e 56 61 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d  nVar, pParse->nM
23abc 65 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  em,.            
23abd 20 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61               pPa
23abe 72 73 65 2d 3e 6e 54 61 62 2c 20 70 50 61 72 73  rse->nTab, pPars
23abf 65 2d 3e 6e 4d 61 78 41 72 67 2c 20 70 50 61 72  e->nMaxArg, pPar
23ac0 73 65 2d 3e 65 78 70 6c 61 69 6e 2c 0a 20 20 20  se->explain,.   
23ac1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23ac2 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 69 73        pParse->is
23ac3 4d 75 6c 74 69 57 72 69 74 65 20 26 26 20 70 50  MultiWrite && pP
23ac4 61 72 73 65 2d 3e 6d 61 79 41 62 6f 72 74 29 3b  arse->mayAbort);
23ac5 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
23ac6 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  = SQLITE_DONE;. 
23ac7 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61     pParse->colNa
23ac8 6d 65 73 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65  mesSet = 0;.  }e
23ac9 6c 73 65 20 69 66 28 20 70 50 61 72 73 65 2d 3e  lse if( pParse->
23aca 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
23acb 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
23acc 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
23acd 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54    }.  pParse->nT
23ace 61 62 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65  ab = 0;.  pParse
23acf 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 50  ->nMem = 0;.  pP
23ad0 61 72 73 65 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a  arse->nSet = 0;.
23ad1 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d    pParse->nVar =
23ad2 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f   0;.  pParse->co
23ad3 6f 6b 69 65 4d 61 73 6b 20 3d 20 30 3b 0a 20 20  okieMask = 0;.  
23ad4 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f  pParse->cookieGo
23ad5 74 6f 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  to = 0;.}../*.**
23ad6 20 52 75 6e 20 74 68 65 20 70 61 72 73 65 72 20   Run the parser 
23ad7 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74  and code generat
23ad8 6f 72 20 72 65 63 75 72 73 69 76 65 6c 79 20 69  or recursively i
23ad9 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72  n order to gener
23ada 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20  ate.** code for 
23adb 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
23adc 74 20 67 69 76 65 6e 20 6f 6e 74 6f 20 74 68 65  t given onto the
23add 20 65 6e 64 20 6f 66 20 74 68 65 20 70 50 61 72   end of the pPar
23ade 73 65 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 63 75  se context.** cu
23adf 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f  rrently under co
23ae0 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 57 68 65  nstruction.  Whe
23ae1 6e 20 74 68 65 20 70 61 72 73 65 72 20 69 73 20  n the parser is 
23ae2 72 75 6e 20 72 65 63 75 72 73 69 76 65 6c 79 0a  run recursively.
23ae3 2a 2a 20 74 68 69 73 20 77 61 79 2c 20 74 68 65  ** this way, the
23ae4 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 69   final OP_Halt i
23ae5 73 20 6e 6f 74 20 61 70 70 65 6e 64 65 64 20 61  s not appended a
23ae6 6e 64 20 6f 74 68 65 72 20 69 6e 69 74 69 61 6c  nd other initial
23ae7 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 66  ization.** and f
23ae8 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 73 74 65 70  inalization step
23ae9 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20 62 65  s are omitted be
23aea 63 61 75 73 65 20 74 68 6f 73 65 20 61 72 65 20  cause those are 
23aeb 68 61 6e 64 6c 69 6e 67 20 62 79 20 74 68 65 0a  handling by the.
23aec 2a 2a 20 6f 75 74 65 72 6d 6f 73 74 20 70 61 72  ** outermost par
23aed 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 65  ser..**.** Not e
23aee 76 65 72 79 74 68 69 6e 67 20 69 73 20 6e 65 73  verything is nes
23aef 74 61 62 6c 65 2e 20 20 54 68 69 73 20 66 61 63  table.  This fac
23af0 69 6c 69 74 79 20 69 73 20 64 65 73 69 67 6e 65  ility is designe
23af1 64 20 74 6f 20 70 65 72 6d 69 74 0a 2a 2a 20 49  d to permit.** I
23af2 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 61  NSERT, UPDATE, a
23af3 6e 64 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74  nd DELETE operat
23af4 69 6f 6e 73 20 61 67 61 69 6e 73 74 20 53 51 4c  ions against SQL
23af5 49 54 45 5f 4d 41 53 54 45 52 2e 20 20 55 73 65  ITE_MASTER.  Use
23af6 0a 2a 2a 20 63 61 72 65 20 69 66 20 79 6f 75 20  .** care if you 
23af7 64 65 63 69 64 65 20 74 6f 20 74 72 79 20 74 6f  decide to try to
23af8 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e   use this routin
23af9 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72  e for some other
23afa 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2f 0a 53 51   purposes..*/.SQ
23afb 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
23afc 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50  d sqlite3NestedP
23afd 61 72 73 65 28 50 61 72 73 65 20 2a 70 50 61 72  arse(Parse *pPar
23afe 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
23aff 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
23b00 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63   va_list ap;.  c
23b01 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 63 68 61  har *zSql;.  cha
23b02 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  r *zErrMsg = 0;.
23b03 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23b04 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 20 64 65  pParse->db;.# de
23b05 66 69 6e 65 20 53 41 56 45 5f 53 5a 20 20 28 73  fine SAVE_SZ  (s
23b06 69 7a 65 6f 66 28 50 61 72 73 65 29 20 2d 20 6f  izeof(Parse) - o
23b07 66 66 73 65 74 6f 66 28 50 61 72 73 65 2c 6e 56  ffsetof(Parse,nV
23b08 61 72 29 29 0a 20 20 63 68 61 72 20 73 61 76 65  ar)).  char save
23b09 42 75 66 5b 53 41 56 45 5f 53 5a 5d 3b 0a 0a 20  Buf[SAVE_SZ];.. 
23b0a 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
23b0b 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  r ) return;.  as
23b0c 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 65  sert( pParse->ne
23b0d 73 74 65 64 3c 31 30 20 29 3b 20 20 2f 2a 20 4e  sted<10 );  /* N
23b0e 65 73 74 69 6e 67 20 73 68 6f 75 6c 64 20 6f 6e  esting should on
23b0f 6c 79 20 62 65 20 6f 66 20 6c 69 6d 69 74 65 64  ly be of limited
23b10 20 64 65 70 74 68 20 2a 2f 0a 20 20 76 61 5f 73   depth */.  va_s
23b11 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
23b12 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69  );.  zSql = sqli
23b13 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20  te3VMPrintf(db, 
23b14 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20  zFormat, ap);.  
23b15 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 69 66  va_end(ap);.  if
23b16 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20  ( zSql==0 ){.   
23b17 20 72 65 74 75 72 6e 3b 20 20 20 2f 2a 20 41 20   return;   /* A 
23b18 6d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65  malloc must have
23b19 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 7d 0a 20   failed */.  }. 
23b1a 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 2b   pParse->nested+
23b1b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28 73 61 76 65  +;.  memcpy(save
23b1c 42 75 66 2c 20 26 70 50 61 72 73 65 2d 3e 6e 56  Buf, &pParse->nV
23b1d 61 72 2c 20 53 41 56 45 5f 53 5a 29 3b 0a 20 20  ar, SAVE_SZ);.  
23b1e 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65 2d 3e  memset(&pParse->
23b1f 6e 56 61 72 2c 20 30 2c 20 53 41 56 45 5f 53 5a  nVar, 0, SAVE_SZ
23b20 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 75 6e 50  );.  sqlite3RunP
23b21 61 72 73 65 72 28 70 50 61 72 73 65 2c 20 7a 53  arser(pParse, zS
23b22 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20  ql, &zErrMsg);. 
23b23 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
23b24 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73  b, zErrMsg);.  s
23b25 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
23b26 20 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d 63 70 79   zSql);.  memcpy
23b27 28 26 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20  (&pParse->nVar, 
23b28 73 61 76 65 42 75 66 2c 20 53 41 56 45 5f 53 5a  saveBuf, SAVE_SZ
23b29 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73  );.  pParse->nes
23b2a 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ted--;.}../*.** 
23b2b 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65  Locate the in-me
23b2c 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 20 74  mory structure t
23b2d 68 61 74 20 64 65 73 63 72 69 62 65 73 20 61 20  hat describes a 
23b2e 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62  particular datab
23b2f 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76  ase.** table giv
23b30 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  en the name of t
23b31 68 61 74 20 74 61 62 6c 65 20 61 6e 64 20 28 6f  hat table and (o
23b32 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e  ptionally) the n
23b33 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61  ame of the.** da
23b34 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
23b35 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65  g the table.  Re
23b36 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74  turn NULL if not
23b37 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66   found..**.** If
23b38 20 7a 44 61 74 61 62 61 73 65 20 69 73 20 30 2c   zDatabase is 0,
23b39 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 20 61   all databases a
23b3a 72 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20  re searched for 
23b3b 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 74 68  the table and th
23b3c 65 0a 2a 2a 20 66 69 72 73 74 20 6d 61 74 63 68  e.** first match
23b3d 69 6e 67 20 74 61 62 6c 65 20 69 73 20 72 65 74  ing table is ret
23b3e 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68 65 63  urned.  (No chec
23b3f 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c 69 63 61  king for duplica
23b40 74 65 20 74 61 62 6c 65 0a 2a 2a 20 6e 61 6d 65  te table.** name
23b41 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54 68 65  s is done.)  The
23b42 20 73 65 61 72 63 68 20 6f 72 64 65 72 20 69 73   search order is
23b43 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74 68 65   TEMP first, the
23b44 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61 6e 79  n MAIN, then any
23b45 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61  .** auxiliary da
23b46 74 61 62 61 73 65 73 20 61 64 64 65 64 20 75 73  tabases added us
23b47 69 6e 67 20 74 68 65 20 41 54 54 41 43 48 20 63  ing the ATTACH c
23b48 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53 65  ommand..**.** Se
23b49 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 4c 6f  e also sqlite3Lo
23b4a 63 61 74 65 54 61 62 6c 65 28 29 2e 0a 2a 2f 0a  cateTable()..*/.
23b4b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
23b4c 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46 69 6e  able *sqlite3Fin
23b4d 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a  dTable(sqlite3 *
23b4e 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
23b4f 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61  zName, const cha
23b50 72 20 2a 7a 44 61 74 61 62 61 73 65 29 7b 0a 20  r *zDatabase){. 
23b51 20 54 61 62 6c 65 20 2a 70 20 3d 20 30 3b 0a 20   Table *p = 0;. 
23b52 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4e   int i;.  int nN
23b53 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 28 20 7a  ame;.  assert( z
23b54 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 6e 4e 61  Name!=0 );.  nNa
23b55 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  me = sqlite3Strl
23b56 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 66  en30(zName);.  f
23b57 6f 72 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42  or(i=OMIT_TEMPDB
23b58 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
23b59 29 7b 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28  ){.    int j = (
23b5a 69 3c 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20  i<2) ? i^1 : i; 
23b5b 20 20 2f 2a 20 53 65 61 72 63 68 20 54 45 4d 50    /* Search TEMP
23b5c 20 62 65 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a   before MAIN */.
23b5d 20 20 20 20 69 66 28 20 7a 44 61 74 61 62 61 73      if( zDatabas
23b5e 65 21 3d 30 20 26 26 20 73 71 6c 69 74 65 33 53  e!=0 && sqlite3S
23b5f 74 72 49 43 6d 70 28 7a 44 61 74 61 62 61 73 65  trICmp(zDatabase
23b60 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61  , db->aDb[j].zNa
23b61 6d 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  me) ) continue;.
23b62 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48      p = sqlite3H
23b63 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 44 62  ashFind(&db->aDb
23b64 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c  [j].pSchema->tbl
23b65 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Hash, zName, nNa
23b66 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29  me);.    if( p )
23b67 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65   break;.  }.  re
23b68 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
23b69 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d 6d   Locate the in-m
23b6a 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65 20  emory structure 
23b6b 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 61  that describes a
23b6c 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61   particular data
23b6d 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69  base.** table gi
23b6e 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ven the name of 
23b6f 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 20 28  that table and (
23b70 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20  optionally) the 
23b71 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64  name of the.** d
23b72 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
23b73 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52  ng the table.  R
23b74 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f  eturn NULL if no
23b75 74 20 66 6f 75 6e 64 2e 20 20 41 6c 73 6f 20 6c  t found.  Also l
23b76 65 61 76 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72  eave an.** error
23b77 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72   message in pPar
23b78 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a  se->zErrMsg..**.
23b79 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63  ** The differenc
23b7a 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20 72  e between this r
23b7b 6f 75 74 69 6e 65 20 61 6e 64 20 73 71 6c 69 74  outine and sqlit
23b7c 65 33 46 69 6e 64 54 61 62 6c 65 28 29 20 69 73  e3FindTable() is
23b7d 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f   that this.** ro
23b7e 75 74 69 6e 65 20 6c 65 61 76 65 73 20 61 6e 20  utine leaves an 
23b7f 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
23b80 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
23b81 20 77 68 65 72 65 0a 2a 2a 20 73 71 6c 69 74 65   where.** sqlite
23b82 33 46 69 6e 64 54 61 62 6c 65 28 29 20 64 6f 65  3FindTable() doe
23b83 73 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  s not..*/.SQLITE
23b84 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a  _PRIVATE Table *
23b85 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
23b86 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  le(.  Parse *pPa
23b87 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
23b88 63 6f 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68  context in which
23b89 20 74 6f 20 72 65 70 6f 72 74 20 65 72 72 6f 72   to report error
23b8a 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 65  s */.  int isVie
23b8b 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  w,            /*
23b8c 20 54 72 75 65 20 69 66 20 6c 6f 6f 6b 69 6e 67   True if looking
23b8d 20 66 6f 72 20 61 20 56 49 45 57 20 72 61 74 68   for a VIEW rath
23b8e 65 72 20 74 68 61 6e 20 61 20 54 41 42 4c 45 20  er than a TABLE 
23b8f 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
23b90 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e  *zName,     /* N
23b91 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
23b92 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20   we are looking 
23b93 66 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  for */.  const c
23b94 68 61 72 20 2a 7a 44 62 61 73 65 20 20 20 20 20  har *zDbase     
23b95 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64  /* Name of the d
23b96 61 74 61 62 61 73 65 2e 20 20 4d 69 67 68 74 20  atabase.  Might 
23b97 62 65 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20  be NULL */.){.  
23b98 54 61 62 6c 65 20 2a 70 3b 0a 0a 20 20 2f 2a 20  Table *p;..  /* 
23b99 52 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73  Read the databas
23b9a 65 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20  e schema. If an 
23b9b 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65  error occurs, le
23b9c 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  ave an error mes
23b9d 73 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f  sage.  ** and co
23b9e 64 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64  de in pParse and
23b9f 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f   return NULL. */
23ba0 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
23ba1 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  !=sqlite3ReadSch
23ba2 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
23ba3 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
23ba4 0a 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46  ..  p = sqlite3F
23ba5 69 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65 2d  indTable(pParse-
23ba6 3e 64 62 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 61  >db, zName, zDba
23ba7 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  se);.  if( p==0 
23ba8 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  ){.    const cha
23ba9 72 20 2a 7a 4d 73 67 20 3d 20 69 73 56 69 65 77  r *zMsg = isView
23baa 20 3f 20 22 6e 6f 20 73 75 63 68 20 76 69 65 77   ? "no such view
23bab 22 20 3a 20 22 6e 6f 20 73 75 63 68 20 74 61 62  " : "no such tab
23bac 6c 65 22 3b 0a 20 20 20 20 69 66 28 20 7a 44 62  le";.    if( zDb
23bad 61 73 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ase ){.      sql
23bae 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
23baf 72 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 22  rse, "%s: %s.%s"
23bb0 2c 20 7a 4d 73 67 2c 20 7a 44 62 61 73 65 2c 20  , zMsg, zDbase, 
23bb1 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73  zName);.    }els
23bb2 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
23bb3 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
23bb4 20 22 25 73 3a 20 25 73 22 2c 20 7a 4d 73 67 2c   "%s: %s", zMsg,
23bb5 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20   zName);.    }. 
23bb6 20 20 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b     pParse->check
23bb7 53 63 68 65 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a  Schema = 1;.  }.
23bb8 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
23bb9 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 74 68 65 20  *.** Locate the 
23bba 69 6e 2d 6d 65 6d 6f 72 79 20 73 74 72 75 63 74  in-memory struct
23bbb 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69 62  ure that describ
23bbc 65 73 20 0a 2a 2a 20 61 20 70 61 72 74 69 63 75  es .** a particu
23bbd 6c 61 72 20 69 6e 64 65 78 20 67 69 76 65 6e 20  lar index given 
23bbe 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74  the name of that
23bbf 20 69 6e 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68   index.** and th
23bc0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  e name of the da
23bc1 74 61 62 61 73 65 20 74 68 61 74 20 63 6f 6e 74  tabase that cont
23bc2 61 69 6e 73 20 74 68 65 20 69 6e 64 65 78 2e 0a  ains the index..
23bc3 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69  ** Return NULL i
23bc4 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a  f not found..**.
23bc5 2a 2a 20 49 66 20 7a 44 61 74 61 62 61 73 65 20  ** If zDatabase 
23bc6 69 73 20 30 2c 20 61 6c 6c 20 64 61 74 61 62 61  is 0, all databa
23bc7 73 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64  ses are searched
23bc8 20 66 6f 72 20 74 68 65 0a 2a 2a 20 74 61 62 6c   for the.** tabl
23bc9 65 20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20  e and the first 
23bca 6d 61 74 63 68 69 6e 67 20 69 6e 64 65 78 20 69  matching index i
23bcb 73 20 72 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f  s returned.  (No
23bcc 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72   checking.** for
23bcd 20 64 75 70 6c 69 63 61 74 65 20 69 6e 64 65 78   duplicate index
23bce 20 6e 61 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29   names is done.)
23bcf 20 20 54 68 65 20 73 65 61 72 63 68 20 6f 72 64    The search ord
23bd0 65 72 20 69 73 0a 2a 2a 20 54 45 4d 50 20 66 69  er is.** TEMP fi
23bd1 72 73 74 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20  rst, then MAIN, 
23bd2 74 68 65 6e 20 61 6e 79 20 61 75 78 69 6c 69 61  then any auxilia
23bd3 72 79 20 64 61 74 61 62 61 73 65 73 20 61 64 64  ry databases add
23bd4 65 64 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  ed.** using the 
23bd5 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a  ATTACH command..
23bd6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23bd7 45 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33  E Index *sqlite3
23bd8 46 69 6e 64 49 6e 64 65 78 28 73 71 6c 69 74 65  FindIndex(sqlite
23bd9 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
23bda 72 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20  r *zName, const 
23bdb 63 68 61 72 20 2a 7a 44 62 29 7b 0a 20 20 49 6e  char *zDb){.  In
23bdc 64 65 78 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e  dex *p = 0;.  in
23bdd 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  t i;.  int nName
23bde 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
23bdf 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72  30(zName);.  for
23be0 28 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20  (i=OMIT_TEMPDB; 
23be1 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
23be2 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c  .    int j = (i<
23be3 32 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f  2) ? i^1 : i;  /
23be4 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65  * Search TEMP be
23be5 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20  fore MAIN */.   
23be6 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
23be7 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53   = db->aDb[j].pS
23be8 63 68 65 6d 61 3b 0a 20 20 20 20 61 73 73 65 72  chema;.    asser
23be9 74 28 20 70 53 63 68 65 6d 61 20 29 3b 0a 20 20  t( pSchema );.  
23bea 20 20 69 66 28 20 7a 44 62 20 26 26 20 73 71 6c    if( zDb && sql
23beb 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 62 2c  ite3StrICmp(zDb,
23bec 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d   db->aDb[j].zNam
23bed 65 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  e) ) continue;. 
23bee 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61     p = sqlite3Ha
23bef 73 68 46 69 6e 64 28 26 70 53 63 68 65 6d 61 2d  shFind(&pSchema-
23bf0 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c  >idxHash, zName,
23bf1 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28   nName);.    if(
23bf2 20 70 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a   p ) break;.  }.
23bf3 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
23bf4 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d 20 74 68 65  *.** Reclaim the
23bf5 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20   memory used by 
23bf6 61 6e 20 69 6e 64 65 78 0a 2a 2f 0a 73 74 61 74  an index.*/.stat
23bf7 69 63 20 76 6f 69 64 20 66 72 65 65 49 6e 64 65  ic void freeInde
23bf8 78 28 49 6e 64 65 78 20 2a 70 29 7b 0a 20 20 73  x(Index *p){.  s
23bf9 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
23bfa 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 23  pTable->dbMem;.#
23bfb 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
23bfc 49 54 5f 41 4e 41 4c 59 5a 45 0a 20 20 73 71 6c  IT_ANALYZE.  sql
23bfd 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78 53  ite3DeleteIndexS
23bfe 61 6d 70 6c 65 73 28 70 29 3b 0a 23 65 6e 64 69  amples(p);.#endi
23bff 66 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  f.  sqlite3DbFre
23c00 65 28 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66  e(db, p->zColAff
23c01 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
23c02 65 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a  ee(db, p);.}../*
23c03 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 67  .** Remove the g
23c04 69 76 65 6e 20 69 6e 64 65 78 20 66 72 6f 6d 20  iven index from 
23c05 74 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74  the index hash t
23c06 61 62 6c 65 2c 20 61 6e 64 20 66 72 65 65 0a 2a  able, and free.*
23c07 2a 20 69 74 73 20 6d 65 6d 6f 72 79 20 73 74 72  * its memory str
23c08 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  uctures..**.** T
23c09 68 65 20 69 6e 64 65 78 20 69 73 20 72 65 6d 6f  he index is remo
23c0a 76 65 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74  ved from the dat
23c0b 61 62 61 73 65 20 68 61 73 68 20 74 61 62 6c 65  abase hash table
23c0c 73 20 62 75 74 0a 2a 2a 20 69 74 20 69 73 20 6e  s but.** it is n
23c0d 6f 74 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d  ot unlinked from
23c0e 20 74 68 65 20 54 61 62 6c 65 20 74 68 61 74 20   the Table that 
23c0f 69 74 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 55  it indexes..** U
23c10 6e 6c 69 6e 6b 69 6e 67 20 66 72 6f 6d 20 74 68  nlinking from th
23c11 65 20 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20  e Table must be 
23c12 64 6f 6e 65 20 62 79 20 74 68 65 20 63 61 6c 6c  done by the call
23c13 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  ing function..*/
23c14 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
23c15 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78 28  ite3DeleteIndex(
23c16 49 6e 64 65 78 20 2a 70 29 7b 0a 20 20 49 6e 64  Index *p){.  Ind
23c17 65 78 20 2a 70 4f 6c 64 3b 0a 20 20 63 6f 6e 73  ex *pOld;.  cons
23c18 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20  t char *zName = 
23c19 70 2d 3e 7a 4e 61 6d 65 3b 0a 0a 20 20 70 4f 6c  p->zName;..  pOl
23c1a 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  d = sqlite3HashI
23c1b 6e 73 65 72 74 28 26 70 2d 3e 70 53 63 68 65 6d  nsert(&p->pSchem
23c1c 61 2d 3e 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d  a->idxHash, zNam
23c1d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
23c1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
23c1f 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
23c20 61 6d 65 29 2c 20 30 29 3b 0a 20 20 61 73 73 65  ame), 0);.  asse
23c21 72 74 28 20 70 4f 6c 64 3d 3d 30 20 7c 7c 20 70  rt( pOld==0 || p
23c22 4f 6c 64 3d 3d 70 20 29 3b 0a 20 20 66 72 65 65  Old==p );.  free
23c23 49 6e 64 65 78 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  Index(p);.}../*.
23c24 2a 2a 20 46 6f 72 20 74 68 65 20 69 6e 64 65 78  ** For the index
23c25 20 63 61 6c 6c 65 64 20 7a 49 64 78 4e 61 6d 65   called zIdxName
23c26 20 77 68 69 63 68 20 69 73 20 66 6f 75 6e 64 20   which is found 
23c27 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
23c28 69 44 62 2c 0a 2a 2a 20 75 6e 6c 69 6b 65 20 74  iDb,.** unlike t
23c29 68 61 74 20 69 6e 64 65 78 20 66 72 6f 6d 20 69  hat index from i
23c2a 74 73 20 54 61 62 6c 65 20 74 68 65 6e 20 72 65  ts Table then re
23c2b 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 66  move the index f
23c2c 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78  rom.** the index
23c2d 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20   hash table and 
23c2e 66 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  free all memory 
23c2f 73 74 72 75 63 74 75 72 65 73 20 61 73 73 6f 63  structures assoc
23c30 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  iated.** with th
23c31 65 20 69 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49  e index..*/.SQLI
23c32 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
23c33 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64  sqlite3UnlinkAnd
23c34 44 65 6c 65 74 65 49 6e 64 65 78 28 73 71 6c 69  DeleteIndex(sqli
23c35 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
23c36 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49  , const char *zI
23c37 64 78 4e 61 6d 65 29 7b 0a 20 20 49 6e 64 65 78  dxName){.  Index
23c38 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20   *pIndex;.  int 
23c39 6c 65 6e 3b 0a 20 20 48 61 73 68 20 2a 70 48 61  len;.  Hash *pHa
23c3a 73 68 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  sh = &db->aDb[iD
23c3b 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48  b].pSchema->idxH
23c3c 61 73 68 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73 71  ash;..  len = sq
23c3d 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 49  lite3Strlen30(zI
23c3e 64 78 4e 61 6d 65 29 3b 0a 20 20 70 49 6e 64 65  dxName);.  pInde
23c3f 78 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  x = sqlite3HashI
23c40 6e 73 65 72 74 28 70 48 61 73 68 2c 20 7a 49 64  nsert(pHash, zId
23c41 78 4e 61 6d 65 2c 20 6c 65 6e 2c 20 30 29 3b 0a  xName, len, 0);.
23c42 20 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a    if( pIndex ){.
23c43 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e      if( pIndex->
23c44 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d  pTable->pIndex==
23c45 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20  pIndex ){.      
23c46 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e  pIndex->pTable->
23c47 70 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 2d  pIndex = pIndex-
23c48 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73  >pNext;.    }els
23c49 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a  e{.      Index *
23c4a 70 3b 0a 20 20 20 20 20 20 2f 2a 20 4a 75 73 74  p;.      /* Just
23c4b 69 66 69 63 61 74 69 6f 6e 20 6f 66 20 41 4c 57  ification of ALW
23c4c 41 59 53 28 29 3b 20 20 54 68 65 20 69 6e 64 65  AYS();  The inde
23c4d 78 20 6d 75 73 74 20 62 65 20 6f 6e 20 74 68 65  x must be on the
23c4e 20 6c 69 73 74 20 6f 66 0a 20 20 20 20 20 20 2a   list of.      *
23c4f 2a 20 69 6e 64 69 63 65 73 2e 20 2a 2f 0a 20 20  * indices. */.  
23c50 20 20 20 20 70 20 3d 20 70 49 6e 64 65 78 2d 3e      p = pIndex->
23c51 70 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 0a  pTable->pIndex;.
23c52 20 20 20 20 20 20 77 68 69 6c 65 28 20 41 4c 57        while( ALW
23c53 41 59 53 28 70 29 20 26 26 20 70 2d 3e 70 4e 65  AYS(p) && p->pNe
23c54 78 74 21 3d 70 49 6e 64 65 78 20 29 7b 20 70 20  xt!=pIndex ){ p 
23c55 3d 20 70 2d 3e 70 4e 65 78 74 3b 20 7d 0a 20 20  = p->pNext; }.  
23c56 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 70      if( ALWAYS(p
23c57 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 49   && p->pNext==pI
23c58 6e 64 65 78 29 20 29 7b 0a 20 20 20 20 20 20 20  ndex) ){.       
23c59 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64   p->pNext = pInd
23c5a 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  ex->pNext;.     
23c5b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 65   }.    }.    fre
23c5c 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29 3b 0a  eIndex(pIndex);.
23c5d 20 20 7d 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20    }.  db->flags 
23c5e 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  |= SQLITE_Intern
23c5f 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  Changes;.}../*.*
23c60 2a 20 45 72 61 73 65 20 61 6c 6c 20 73 63 68 65  * Erase all sche
23c61 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ma information f
23c62 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  rom the in-memor
23c63 79 20 68 61 73 68 20 74 61 62 6c 65 73 20 6f 66  y hash tables of
23c64 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64 61 74  .** a single dat
23c65 61 62 61 73 65 2e 20 20 54 68 69 73 20 72 6f 75  abase.  This rou
23c66 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74  tine is called t
23c67 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79  o reclaim memory
23c68 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 65 20 64  .** before the d
23c69 61 74 61 62 61 73 65 20 63 6c 6f 73 65 73 2e 20  atabase closes. 
23c6a 20 49 74 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c   It is also call
23c6b 65 64 20 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c  ed during a roll
23c6c 62 61 63 6b 0a 2a 2a 20 69 66 20 74 68 65 72 65  back.** if there
23c6d 20 77 65 72 65 20 73 63 68 65 6d 61 20 63 68 61   were schema cha
23c6e 6e 67 65 73 20 64 75 72 69 6e 67 20 74 68 65 20  nges during the 
23c6f 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 69  transaction or i
23c70 66 20 61 0a 2a 2a 20 73 63 68 65 6d 61 2d 63 6f  f a.** schema-co
23c71 6f 6b 69 65 20 6d 69 73 6d 61 74 63 68 20 6f 63  okie mismatch oc
23c72 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  curs..**.** If i
23c73 44 62 3d 3d 30 20 74 68 65 6e 20 72 65 73 65 74  Db==0 then reset
23c74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63   the internal sc
23c75 68 65 6d 61 20 74 61 62 6c 65 73 20 66 6f 72 20  hema tables for 
23c76 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  all database.** 
23c77 66 69 6c 65 73 2e 20 20 49 66 20 69 44 62 3e 3d  files.  If iDb>=
23c78 31 20 74 68 65 6e 20 72 65 73 65 74 20 74 68 65  1 then reset the
23c79 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61   internal schema
23c7a 20 66 6f 72 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a   for only the.**
23c7b 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 69 6e 64   single file ind
23c7c 69 63 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  icated..*/.SQLIT
23c7d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23c7e 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
23c7f 6e 61 6c 53 63 68 65 6d 61 28 73 71 6c 69 74 65  nalSchema(sqlite
23c80 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 29 7b  3 *db, int iDb){
23c81 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 61  .  int i, j;.  a
23c82 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
23c83 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
23c84 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20 29 7b  .  if( iDb==0 ){
23c85 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
23c86 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20  eEnterAll(db);. 
23c87 20 7d 0a 20 20 66 6f 72 28 69 3d 69 44 62 3b 20   }.  for(i=iDb; 
23c88 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
23c89 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26  .    Db *pDb = &
23c8a 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20  db->aDb[i];.    
23c8b 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  if( pDb->pSchema
23c8c 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
23c8d 28 69 3d 3d 31 20 7c 7c 20 28 70 44 62 2d 3e 70  (i==1 || (pDb->p
23c8e 42 74 20 26 26 20 73 71 6c 69 74 65 33 42 74 72  Bt && sqlite3Btr
23c8f 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 44 62  eeHoldsMutex(pDb
23c90 2d 3e 70 42 74 29 29 29 3b 0a 20 20 20 20 20 20  ->pBt)));.      
23c91 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65  sqlite3SchemaFre
23c92 65 28 70 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b  e(pDb->pSchema);
23c93 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
23c94 44 62 3e 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  Db>0 ) return;. 
23c95 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62   }.  assert( iDb
23c96 3d 3d 30 20 29 3b 0a 20 20 64 62 2d 3e 66 6c 61  ==0 );.  db->fla
23c97 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e  gs &= ~SQLITE_In
23c98 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 73  ternChanges;.  s
23c99 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
23c9a 4c 69 73 74 28 64 62 29 3b 0a 20 20 73 71 6c 69  List(db);.  sqli
23c9b 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c  te3BtreeLeaveAll
23c9c 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f  (db);..  /* If o
23c9d 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
23c9e 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  e auxiliary data
23c9f 62 61 73 65 20 66 69 6c 65 73 20 68 61 73 20 62  base files has b
23ca0 65 65 6e 20 63 6c 6f 73 65 64 2c 0a 20 20 2a 2a  een closed,.  **
23ca1 20 74 68 65 6e 20 72 65 6d 6f 76 65 20 74 68 65   then remove the
23ca2 6d 20 66 72 6f 6d 20 74 68 65 20 61 75 78 69 6c  m from the auxil
23ca3 69 61 72 79 20 64 61 74 61 62 61 73 65 20 6c 69  iary database li
23ca4 73 74 2e 20 20 57 65 20 74 61 6b 65 20 74 68 65  st.  We take the
23ca5 0a 20 20 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74  .  ** opportunit
23ca6 79 20 74 6f 20 64 6f 20 74 68 69 73 20 68 65 72  y to do this her
23ca7 65 20 73 69 6e 63 65 20 77 65 20 68 61 76 65 20  e since we have 
23ca8 6a 75 73 74 20 64 65 6c 65 74 65 64 20 61 6c 6c  just deleted all
23ca9 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 73 63 68   of the.  ** sch
23caa 65 6d 61 20 68 61 73 68 20 74 61 62 6c 65 73 20  ema hash tables 
23cab 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 64 6f  and therefore do
23cac 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 6d 61 6b   not have to mak
23cad 65 20 61 6e 79 20 63 68 61 6e 67 65 73 0a 20 20  e any changes.  
23cae 2a 2a 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 6f  ** to any of tho
23caf 73 65 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a  se tables..  */.
23cb0 20 20 66 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c 64    for(i=j=2; i<d
23cb1 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
23cb2 20 20 73 74 72 75 63 74 20 44 62 20 2a 70 44 62    struct Db *pDb
23cb3 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a   = &db->aDb[i];.
23cb4 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 42 74      if( pDb->pBt
23cb5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
23cb6 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
23cb7 44 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  Db->zName);.    
23cb8 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 20 3d 20 30    pDb->zName = 0
23cb9 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  ;.      continue
23cba 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
23cbb 6a 3c 69 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  j<i ){.      db-
23cbc 3e 61 44 62 5b 6a 5d 20 3d 20 64 62 2d 3e 61 44  >aDb[j] = db->aD
23cbd 62 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  b[i];.    }.    
23cbe 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  j++;.  }.  memse
23cbf 74 28 26 64 62 2d 3e 61 44 62 5b 6a 5d 2c 20 30  t(&db->aDb[j], 0
23cc0 2c 20 28 64 62 2d 3e 6e 44 62 2d 6a 29 2a 73 69  , (db->nDb-j)*si
23cc1 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 6a 5d 29  zeof(db->aDb[j])
23cc2 29 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20 6a  );.  db->nDb = j
23cc3 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3c  ;.  if( db->nDb<
23cc4 3d 32 20 26 26 20 64 62 2d 3e 61 44 62 21 3d 64  =2 && db->aDb!=d
23cc5 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29 7b 0a  b->aDbStatic ){.
23cc6 20 20 20 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61      memcpy(db->a
23cc7 44 62 53 74 61 74 69 63 2c 20 64 62 2d 3e 61 44  DbStatic, db->aD
23cc8 62 2c 20 32 2a 73 69 7a 65 6f 66 28 64 62 2d 3e  b, 2*sizeof(db->
23cc9 61 44 62 5b 30 5d 29 29 3b 0a 20 20 20 20 73 71  aDb[0]));.    sq
23cca 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
23ccb 64 62 2d 3e 61 44 62 29 3b 0a 20 20 20 20 64 62  db->aDb);.    db
23ccc 2d 3e 61 44 62 20 3d 20 64 62 2d 3e 61 44 62 53  ->aDb = db->aDbS
23ccd 74 61 74 69 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  tatic;.  }.}../*
23cce 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
23ccf 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
23cd0 61 20 63 6f 6d 6d 69 74 20 6f 63 63 75 72 73 2e  a commit occurs.
23cd1 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
23cd2 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
23cd3 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68 61  ommitInternalCha
23cd4 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62  nges(sqlite3 *db
23cd5 29 7b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  ){.  db->flags &
23cd6 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e  = ~SQLITE_Intern
23cd7 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a  Changes;.}../*.*
23cd8 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6c 75  * Clear the colu
23cd9 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20 61 20  mn names from a 
23cda 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2e 0a 2a  table or view..*
23cdb 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
23cdc 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e  liteResetColumnN
23cdd 61 6d 65 73 28 54 61 62 6c 65 20 2a 70 54 61 62  ames(Table *pTab
23cde 6c 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  le){.  int i;.  
23cdf 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20  Column *pCol;.  
23ce0 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 54  sqlite3 *db = pT
23ce1 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 74  able->dbMem;.  t
23ce2 65 73 74 63 61 73 65 28 20 64 62 3d 3d 30 20 29  estcase( db==0 )
23ce3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ;.  assert( pTab
23ce4 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28  le!=0 );.  if( (
23ce5 70 43 6f 6c 20 3d 20 70 54 61 62 6c 65 2d 3e 61  pCol = pTable->a
23ce6 43 6f 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 66  Col)!=0 ){.    f
23ce7 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 6c 65  or(i=0; i<pTable
23ce8 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  ->nCol; i++, pCo
23ce9 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  l++){.      sqli
23cea 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
23ceb 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ol->zName);.    
23cec 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
23ced 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44  ete(db, pCol->pD
23cee 66 6c 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  flt);.      sqli
23cef 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
23cf0 6f 6c 2d 3e 7a 44 66 6c 74 29 3b 0a 20 20 20 20  ol->zDflt);.    
23cf1 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23cf2 64 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29  db, pCol->zType)
23cf3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
23cf4 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e  bFree(db, pCol->
23cf5 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20  zColl);.    }.  
23cf6 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23cf7 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c  db, pTable->aCol
23cf8 29 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d  );.  }.  pTable-
23cf9 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61  >aCol = 0;.  pTa
23cfa 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d  ble->nCol = 0;.}
23cfb 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74  ../*.** Remove t
23cfc 68 65 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73  he memory data s
23cfd 74 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69  tructures associ
23cfe 61 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69  ated with the gi
23cff 76 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e  ven.** Table.  N
23d00 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61  o changes are ma
23d01 64 65 20 74 6f 20 64 69 73 6b 20 62 79 20 74 68  de to disk by th
23d02 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a  is routine..**.*
23d03 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a  * This routine j
23d04 75 73 74 20 64 65 6c 65 74 65 73 20 74 68 65 20  ust deletes the 
23d05 64 61 74 61 20 73 74 72 75 63 74 75 72 65 2e 20  data structure. 
23d06 20 49 74 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c   It does not unl
23d07 69 6e 6b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65  ink.** the table
23d08 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
23d09 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61  from the hash ta
23d0a 62 6c 65 2e 20 20 42 75 74 20 69 74 20 64 6f 65  ble.  But it doe
23d0b 73 20 64 65 73 74 72 6f 79 0a 2a 2a 20 6d 65 6d  s destroy.** mem
23d0c 6f 72 79 20 73 74 72 75 63 74 75 72 65 73 20 6f  ory structures o
23d0d 66 20 74 68 65 20 69 6e 64 69 63 65 73 20 61 6e  f the indices an
23d0e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61  d foreign keys a
23d0f 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a  ssociated with .
23d10 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f  ** the table..*/
23d11 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23d12 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
23d13 74 65 54 61 62 6c 65 28 54 61 62 6c 65 20 2a 70  teTable(Table *p
23d14 54 61 62 6c 65 29 7b 0a 20 20 49 6e 64 65 78 20  Table){.  Index 
23d15 2a 70 49 6e 64 65 78 2c 20 2a 70 4e 65 78 74 3b  *pIndex, *pNext;
23d16 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
23d17 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30  .  if( pTable==0
23d18 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20   ) return;.  db 
23d19 3d 20 70 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b  = pTable->dbMem;
23d1a 0a 20 20 74 65 73 74 63 61 73 65 28 20 64 62 3d  .  testcase( db=
23d1b 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e  =0 );..  /* Do n
23d1c 6f 74 20 64 65 6c 65 74 65 20 74 68 65 20 74 61  ot delete the ta
23d1d 62 6c 65 20 75 6e 74 69 6c 20 74 68 65 20 72 65  ble until the re
23d1e 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 72 65  ference count re
23d1f 61 63 68 65 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20  aches zero. */. 
23d20 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 2d 2d 3b   pTable->nRef--;
23d21 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e  .  if( pTable->n
23d22 52 65 66 3e 30 20 29 7b 0a 20 20 20 20 72 65 74  Ref>0 ){.    ret
23d23 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  urn;.  }.  asser
23d24 74 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3d  t( pTable->nRef=
23d25 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65  =0 );..  /* Dele
23d26 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61  te all indices a
23d27 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
23d28 68 69 73 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20  his table.  */. 
23d29 20 66 6f 72 28 70 49 6e 64 65 78 20 3d 20 70 54   for(pIndex = pT
23d2a 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 20 70 49  able->pIndex; pI
23d2b 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 4e 65  ndex; pIndex=pNe
23d2c 78 74 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d  xt){.    pNext =
23d2d 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a   pIndex->pNext;.
23d2e 20 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 64      assert( pInd
23d2f 65 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61  ex->pSchema==pTa
23d30 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a  ble->pSchema );.
23d31 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
23d32 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29 3b 0a  eIndex(pIndex);.
23d33 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65    }..  /* Delete
23d34 20 61 6e 79 20 66 6f 72 65 69 67 6e 20 6b 65 79   any foreign key
23d35 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68  s attached to th
23d36 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 73  is table. */.  s
23d37 71 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28 70  qlite3FkDelete(p
23d38 54 61 62 6c 65 29 3b 0a 0a 20 20 2f 2a 20 44 65  Table);..  /* De
23d39 6c 65 74 65 20 74 68 65 20 54 61 62 6c 65 20 73  lete the Table s
23d3a 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e  tructure itself.
23d3b 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 52 65  .  */.  sqliteRe
23d3c 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  setColumnNames(p
23d3d 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65  Table);.  sqlite
23d3e 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62  3DbFree(db, pTab
23d3f 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71  le->zName);.  sq
23d40 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
23d41 70 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29  pTable->zColAff)
23d42 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
23d43 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62  tDelete(db, pTab
23d44 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 23 69  le->pSelect);.#i
23d45 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
23d46 54 5f 43 48 45 43 4b 0a 20 20 73 71 6c 69 74 65  T_CHECK.  sqlite
23d47 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
23d48 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b  pTable->pCheck);
23d49 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65  .#endif.  sqlite
23d4a 33 56 74 61 62 43 6c 65 61 72 28 70 54 61 62 6c  3VtabClear(pTabl
23d4b 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  e);.  sqlite3DbF
23d4c 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 29 3b  ree(db, pTable);
23d4d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b  .}../*.** Unlink
23d4e 20 74 68 65 20 67 69 76 65 6e 20 74 61 62 6c 65   the given table
23d4f 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74   from the hash t
23d50 61 62 6c 65 73 20 61 6e 64 20 74 68 65 20 64 65  ables and the de
23d51 6c 65 74 65 20 74 68 65 0a 2a 2a 20 74 61 62 6c  lete the.** tabl
23d52 65 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68  e structure with
23d53 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73   all its indices
23d54 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b 65 79   and foreign key
23d55 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
23d56 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23d57 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
23d58 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64  Table(sqlite3 *d
23d59 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73  b, int iDb, cons
23d5a 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65  t char *zTabName
23d5b 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20  ){.  Table *p;. 
23d5c 20 44 62 20 2a 70 44 62 3b 0a 0a 20 20 61 73 73   Db *pDb;..  ass
23d5d 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20  ert( db!=0 );.  
23d5e 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26  assert( iDb>=0 &
23d5f 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  & iDb<db->nDb );
23d60 0a 20 20 61 73 73 65 72 74 28 20 7a 54 61 62 4e  .  assert( zTabN
23d61 61 6d 65 20 26 26 20 7a 54 61 62 4e 61 6d 65 5b  ame && zTabName[
23d62 30 5d 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64  0] );.  pDb = &d
23d63 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 70  b->aDb[iDb];.  p
23d64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e   = sqlite3HashIn
23d65 73 65 72 74 28 26 70 44 62 2d 3e 70 53 63 68 65  sert(&pDb->pSche
23d66 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 54 61  ma->tblHash, zTa
23d67 62 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  bName,.         
23d68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
23d69 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
23d6a 54 61 62 4e 61 6d 65 29 2c 30 29 3b 0a 20 20 73  TabName),0);.  s
23d6b 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
23d6c 65 28 70 29 3b 0a 20 20 64 62 2d 3e 66 6c 61 67  e(p);.  db->flag
23d6d 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65  s |= SQLITE_Inte
23d6e 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a  rnChanges;.}../*
23d6f 0a 2a 2a 20 47 69 76 65 6e 20 61 20 74 6f 6b 65  .** Given a toke
23d70 6e 2c 20 72 65 74 75 72 6e 20 61 20 73 74 72 69  n, return a stri
23d71 6e 67 20 74 68 61 74 20 63 6f 6e 73 69 73 74 73  ng that consists
23d72 20 6f 66 20 74 68 65 20 74 65 78 74 20 6f 66 20   of the text of 
23d73 74 68 61 74 0a 2a 2a 20 74 6f 6b 65 6e 2e 20 20  that.** token.  
23d74 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  Space to hold th
23d75 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  e returned strin
23d76 67 0a 2a 2a 20 69 73 20 6f 62 74 61 69 6e 65 64  g.** is obtained
23d77 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c   from sqliteMall
23d78 6f 63 28 29 20 61 6e 64 20 6d 75 73 74 20 62 65  oc() and must be
23d79 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63 61   freed by the ca
23d7a 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f  lling.** functio
23d7b 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 71 75 6f  n..**.** Any quo
23d7c 74 61 74 69 6f 6e 20 6d 61 72 6b 73 20 28 65 78  tation marks (ex
23d7d 3a 20 20 22 6e 61 6d 65 22 2c 20 27 6e 61 6d 65  :  "name", 'name
23d7e 27 2c 20 5b 6e 61 6d 65 5d 2c 20 6f 72 20 60 6e  ', [name], or `n
23d7f 61 6d 65 60 29 20 74 68 61 74 0a 2a 2a 20 73 75  ame`) that.** su
23d80 72 72 6f 75 6e 64 20 74 68 65 20 62 6f 64 79 20  rround the body 
23d81 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 61 72 65  of the token are
23d82 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20   removed..**.** 
23d83 54 6f 6b 65 6e 73 20 61 72 65 20 6f 66 74 65 6e  Tokens are often
23d84 20 6a 75 73 74 20 70 6f 69 6e 74 65 72 73 20 69   just pointers i
23d85 6e 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  nto the original
23d86 20 53 51 4c 20 74 65 78 74 20 61 6e 64 20 73 6f   SQL text and so
23d87 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 5c 30 30 30  .** are not \000
23d88 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20   terminated and 
23d89 61 72 65 20 6e 6f 74 20 70 65 72 73 69 73 74 65  are not persiste
23d8a 6e 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65  nt.  The returne
23d8b 64 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 5c  d string.** is \
23d8c 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64 20 61  000 terminated a
23d8d 6e 64 20 69 73 20 70 65 72 73 69 73 74 65 6e 74  nd is persistent
23d8e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23d8f 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
23d90 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 73  3NameFromToken(s
23d91 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65  qlite3 *db, Toke
23d92 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61  n *pName){.  cha
23d93 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20  r *zName;.  if( 
23d94 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61  pName ){.    zNa
23d95 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
23d96 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a  rNDup(db, (char*
23d97 29 70 4e 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65  )pName->z, pName
23d98 2d 3e 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ->n);.    sqlite
23d99 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d 65 29 3b  3Dequote(zName);
23d9a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4e  .  }else{.    zN
23d9b 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  ame = 0;.  }.  r
23d9c 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a  eturn zName;.}..
23d9d 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68 65 20 73  /*.** Open the s
23d9e 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
23d9f 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 64 61 74  le stored in dat
23da0 61 62 61 73 65 20 6e 75 6d 62 65 72 20 69 44 62  abase number iDb
23da1 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67 2e   for.** writing.
23da2 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6f 70   The table is op
23da3 65 6e 65 64 20 75 73 69 6e 67 20 63 75 72 73 6f  ened using curso
23da4 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  r 0..*/.SQLITE_P
23da5 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
23da6 74 65 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62  te3OpenMasterTab
23da7 6c 65 28 50 61 72 73 65 20 2a 70 2c 20 69 6e 74  le(Parse *p, int
23da8 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76   iDb){.  Vdbe *v
23da9 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
23daa 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 54  e(p);.  sqlite3T
23dab 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 44 62 2c  ableLock(p, iDb,
23dac 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c   MASTER_ROOT, 1,
23dad 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
23dae 62 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  b));.  sqlite3Vd
23daf 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f  beAddOp3(v, OP_O
23db0 70 65 6e 57 72 69 74 65 2c 20 30 2c 20 4d 41 53  penWrite, 0, MAS
23db1 54 45 52 5f 52 4f 4f 54 2c 20 69 44 62 29 3b 0a  TER_ROOT, iDb);.
23db2 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
23db3 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68  ngeP4(v, -1, (ch
23db4 61 72 20 2a 29 35 2c 20 50 34 5f 49 4e 54 33 32  ar *)5, P4_INT32
23db5 29 3b 20 20 2f 2a 20 35 20 63 6f 6c 75 6d 6e 20  );  /* 5 column 
23db6 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70  table */.  if( p
23db7 2d 3e 6e 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20  ->nTab==0 ){.   
23db8 20 70 2d 3e 6e 54 61 62 20 3d 20 31 3b 0a 20 20   p->nTab = 1;.  
23db9 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d  }.}../*.** Param
23dba 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f 69 6e 74  eter zName point
23dbb 73 20 74 6f 20 61 20 6e 75 6c 2d 74 65 72 6d 69  s to a nul-termi
23dbc 6e 61 74 65 64 20 62 75 66 66 65 72 20 63 6f 6e  nated buffer con
23dbd 74 61 69 6e 69 6e 67 20 74 68 65 20 6e 61 6d 65  taining the name
23dbe 0a 2a 2a 20 6f 66 20 61 20 64 61 74 61 62 61 73  .** of a databas
23dbf 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70  e ("main", "temp
23dc0 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 20 6f 66  " or the name of
23dc1 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 62 29   an attached db)
23dc2 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69  . This.** functi
23dc3 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 69  on returns the i
23dc4 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 6d 65  ndex of the name
23dc5 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 64 62  d database in db
23dc6 2d 3e 61 44 62 5b 5d 2c 20 6f 72 0a 2a 2a 20 2d  ->aDb[], or.** -
23dc7 31 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 64  1 if the named d
23dc8 62 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  b cannot be foun
23dc9 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
23dca 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
23dcb 46 69 6e 64 44 62 4e 61 6d 65 28 73 71 6c 69 74  FindDbName(sqlit
23dcc 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
23dcd 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e  ar *zName){.  in
23dce 74 20 69 20 3d 20 2d 31 3b 20 20 20 20 20 20 20  t i = -1;       
23dcf 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75    /* Database nu
23dd0 6d 62 65 72 20 2a 2f 0a 20 20 69 66 28 20 7a 4e  mber */.  if( zN
23dd1 61 6d 65 20 29 7b 0a 20 20 20 20 44 62 20 2a 70  ame ){.    Db *p
23dd2 44 62 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20  Db;.    int n = 
23dd3 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
23dd4 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66 6f 72 28  zName);.    for(
23dd5 69 3d 28 64 62 2d 3e 6e 44 62 2d 31 29 2c 20 70  i=(db->nDb-1), p
23dd6 44 62 3d 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 20  Db=&db->aDb[i]; 
23dd7 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 44 62 2d 2d  i>=0; i--, pDb--
23dd8 29 7b 0a 20 20 20 20 20 20 69 66 28 20 28 21 4f  ){.      if( (!O
23dd9 4d 49 54 5f 54 45 4d 50 44 42 20 7c 7c 20 69 21  MIT_TEMPDB || i!
23dda 3d 31 20 29 20 26 26 20 6e 3d 3d 73 71 6c 69 74  =1 ) && n==sqlit
23ddb 65 33 53 74 72 6c 65 6e 33 30 28 70 44 62 2d 3e  e3Strlen30(pDb->
23ddc 7a 4e 61 6d 65 29 20 26 26 20 0a 20 20 20 20 20  zName) && .     
23ddd 20 20 20 20 20 30 3d 3d 73 71 6c 69 74 65 33 53       0==sqlite3S
23dde 74 72 49 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d  trICmp(pDb->zNam
23ddf 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  e, zName) ){.   
23de0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
23de1 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
23de2 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a  return i;.}../*.
23de3 2a 2a 20 54 68 65 20 74 6f 6b 65 6e 20 2a 70 4e  ** The token *pN
23de4 61 6d 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ame contains the
23de5 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61 62   name of a datab
23de6 61 73 65 20 28 65 69 74 68 65 72 20 22 6d 61 69  ase (either "mai
23de7 6e 22 20 6f 72 0a 2a 2a 20 22 74 65 6d 70 22 20  n" or.** "temp" 
23de8 6f 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61  or the name of a
23de9 6e 20 61 74 74 61 63 68 65 64 20 64 62 29 2e 20  n attached db). 
23dea 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
23deb 75 72 6e 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65  urns the.** inde
23dec 78 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64  x of the named d
23ded 61 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61  atabase in db->a
23dee 44 62 5b 5d 2c 20 6f 72 20 2d 31 20 69 66 20 74  Db[], or -1 if t
23def 68 65 20 6e 61 6d 65 64 20 64 62 20 0a 2a 2a 20  he named db .** 
23df0 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a  does not exist..
23df1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23df2 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e  E int sqlite3Fin
23df3 64 44 62 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  dDb(sqlite3 *db,
23df4 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a   Token *pName){.
23df5 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
23df6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23df7 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
23df8 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63  se number */.  c
23df9 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
23dfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23dfb 20 20 20 20 2f 2a 20 4e 61 6d 65 20 77 65 20 61      /* Name we a
23dfc 72 65 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72  re searching for
23dfd 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71   */.  zName = sq
23dfe 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
23dff 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20  en(db, pName);. 
23e00 20 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64   i = sqlite3Find
23e01 44 62 4e 61 6d 65 28 64 62 2c 20 7a 4e 61 6d 65  DbName(db, zName
23e02 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
23e03 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20  ee(db, zName);. 
23e04 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a   return i;.}../*
23e05 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69   The table or vi
23e06 65 77 20 6f 72 20 74 72 69 67 67 65 72 20 6e 61  ew or trigger na
23e07 6d 65 20 69 73 20 70 61 73 73 65 64 20 74 6f 20  me is passed to 
23e08 74 68 69 73 20 72 6f 75 74 69 6e 65 20 76 69 61  this routine via
23e09 20 74 6f 6b 65 6e 73 0a 2a 2a 20 70 4e 61 6d 65   tokens.** pName
23e0a 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49 66  1 and pName2. If
23e0b 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
23e0c 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69 66  was fully qualif
23e0d 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  ied, for example
23e0e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54  :.**.** CREATE T
23e0f 41 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e 2e  ABLE xxx.yyy (..
23e10 2e 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65 6e 20  .);.** .** Then 
23e11 70 4e 61 6d 65 31 20 69 73 20 73 65 74 20 74 6f  pName1 is set to
23e12 20 22 78 78 78 22 20 61 6e 64 20 70 4e 61 6d 65   "xxx" and pName
23e13 32 20 22 79 79 79 22 2e 20 4f 6e 20 74 68 65 20  2 "yyy". On the 
23e14 6f 74 68 65 72 20 68 61 6e 64 20 69 66 0a 2a 2a  other hand if.**
23e15 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
23e16 69 73 20 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61  is not fully qua
23e17 6c 69 66 69 65 64 2c 20 69 2e 65 2e 3a 0a 2a 2a  lified, i.e.:.**
23e18 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45  .** CREATE TABLE
23e19 20 79 79 79 28 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a   yyy(...);.**.**
23e1a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20   Then pName1 is 
23e1b 73 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e 64  set to "yyy" and
23e1c 20 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a 2a   pName2 is ""..*
23e1d 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
23e1e 65 20 73 65 74 73 20 74 68 65 20 2a 70 70 55 6e  e sets the *ppUn
23e1f 71 75 61 6c 20 70 6f 69 6e 74 65 72 20 74 6f 20  qual pointer to 
23e20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f 6b  point at the tok
23e21 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 2a 2a  en (pName1 or.**
23e22 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20 73 74   pName2) that st
23e23 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61 6c 69  ores the unquali
23e24 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d 65 2e  fied table name.
23e25 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74    The index of t
23e26 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 22  he.** database "
23e27 78 78 78 22 20 69 73 20 72 65 74 75 72 6e 65 64  xxx" is returned
23e28 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23e29 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54  ATE int sqlite3T
23e2a 77 6f 50 61 72 74 4e 61 6d 65 28 0a 20 20 50 61  woPartName(.  Pa
23e2b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
23e2c 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e 64    /* Parsing and
23e2d 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e 67   code generating
23e2e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f   context */.  To
23e2f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20  ken *pName1,    
23e30 20 20 2f 2a 20 54 68 65 20 22 78 78 78 22 20 69    /* The "xxx" i
23e31 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78 78 2e  n the name "xxx.
23e32 79 79 79 22 20 6f 72 20 22 78 78 78 22 20 2a 2f  yyy" or "xxx" */
23e33 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32  .  Token *pName2
23e34 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 79  ,      /* The "y
23e35 79 79 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20  yy" in the name 
23e36 22 78 78 78 2e 79 79 79 22 20 2a 2f 0a 20 20 54  "xxx.yyy" */.  T
23e37 6f 6b 65 6e 20 2a 2a 70 55 6e 71 75 61 6c 20 20  oken **pUnqual  
23e38 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
23e39 75 6e 71 75 61 6c 69 66 69 65 64 20 6f 62 6a 65  unqualified obje
23e3a 63 74 20 6e 61 6d 65 20 68 65 72 65 20 2a 2f 0a  ct name here */.
23e3b 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  ){.  int iDb;   
23e3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23e3d 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 6f 6c   /* Database hol
23e3e 64 69 6e 67 20 74 68 65 20 6f 62 6a 65 63 74 20  ding the object 
23e3f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
23e40 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
23e41 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 4e 61    if( ALWAYS(pNa
23e42 6d 65 32 21 3d 30 29 20 26 26 20 70 4e 61 6d 65  me2!=0) && pName
23e43 32 2d 3e 6e 3e 30 20 29 7b 0a 20 20 20 20 69 66  2->n>0 ){.    if
23e44 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20  ( db->init.busy 
23e45 29 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ) {.      sqlite
23e46 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
23e47 2c 20 22 63 6f 72 72 75 70 74 20 64 61 74 61 62  , "corrupt datab
23e48 61 73 65 22 29 3b 0a 20 20 20 20 20 20 70 50 61  ase");.      pPa
23e49 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20  rse->nErr++;.   
23e4a 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20     return -1;.  
23e4b 20 20 7d 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c    }.    *pUnqual
23e4c 20 3d 20 70 4e 61 6d 65 32 3b 0a 20 20 20 20 69   = pName2;.    i
23e4d 44 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  Db = sqlite3Find
23e4e 44 62 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a  Db(db, pName1);.
23e4f 20 20 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b      if( iDb<0 ){
23e50 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
23e51 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23e52 75 6e 6b 6e 6f 77 6e 20 64 61 74 61 62 61 73 65  unknown database
23e53 20 25 54 22 2c 20 70 4e 61 6d 65 31 29 3b 0a 20   %T", pName1);. 
23e54 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72       pParse->nEr
23e55 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72  r++;.      retur
23e56 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  n -1;.    }.  }e
23e57 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
23e58 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 30   db->init.iDb==0
23e59 20 7c 7c 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73   || db->init.bus
23e5a 79 20 29 3b 0a 20 20 20 20 69 44 62 20 3d 20 64  y );.    iDb = d
23e5b 62 2d 3e 69 6e 69 74 2e 69 44 62 3b 0a 20 20 20  b->init.iDb;.   
23e5c 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d   *pUnqual = pNam
23e5d 65 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  e1;.  }.  return
23e5e 20 69 44 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   iDb;.}../*.** T
23e5f 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
23e60 73 65 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20  sed to check if 
23e61 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e 67  the UTF-8 string
23e62 20 7a 4e 61 6d 65 20 69 73 20 61 20 6c 65 67 61   zName is a lega
23e63 6c 0a 2a 2a 20 75 6e 71 75 61 6c 69 66 69 65 64  l.** unqualified
23e64 20 6e 61 6d 65 20 66 6f 72 20 61 20 6e 65 77 20   name for a new 
23e65 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 28 74  schema object (t
23e66 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20 76 69 65  able, index, vie
23e67 77 20 6f 72 0a 2a 2a 20 74 72 69 67 67 65 72 29  w or.** trigger)
23e68 2e 20 41 6c 6c 20 6e 61 6d 65 73 20 61 72 65 20  . All names are 
23e69 6c 65 67 61 6c 20 65 78 63 65 70 74 20 74 68 6f  legal except tho
23e6a 73 65 20 74 68 61 74 20 62 65 67 69 6e 20 77 69  se that begin wi
23e6b 74 68 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a  th the string.**
23e6c 20 22 73 71 6c 69 74 65 5f 22 20 28 69 6e 20 75   "sqlite_" (in u
23e6d 70 70 65 72 2c 20 6c 6f 77 65 72 20 6f 72 20 6d  pper, lower or m
23e6e 69 78 65 64 20 63 61 73 65 29 2e 20 54 68 69 73  ixed case). This
23e6f 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65 20   portion of the 
23e70 6e 61 6d 65 73 70 61 63 65 0a 2a 2a 20 69 73 20  namespace.** is 
23e71 72 65 73 65 72 76 65 64 20 66 6f 72 20 69 6e 74  reserved for int
23e72 65 72 6e 61 6c 20 75 73 65 2e 0a 2a 2f 0a 53 51  ernal use..*/.SQ
23e73 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
23e74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a   sqlite3CheckObj
23e75 65 63 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 70  ectName(Parse *p
23e76 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
23e77 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 66 28  r *zName){.  if(
23e78 20 21 70 50 61 72 73 65 2d 3e 64 62 2d 3e 69 6e   !pParse->db->in
23e79 69 74 2e 62 75 73 79 20 26 26 20 70 50 61 72 73  it.busy && pPars
23e7a 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 0a 20 20  e->nested==0 .  
23e7b 20 20 20 20 20 20 20 20 26 26 20 28 70 50 61 72          && (pPar
23e7c 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  se->db->flags & 
23e7d 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65  SQLITE_WriteSche
23e7e 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20  ma)==0.         
23e7f 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74   && 0==sqlite3St
23e80 72 4e 49 43 6d 70 28 7a 4e 61 6d 65 2c 20 22 73  rNICmp(zName, "s
23e81 71 6c 69 74 65 5f 22 2c 20 37 29 20 29 7b 0a 20  qlite_", 7) ){. 
23e82 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
23e83 73 67 28 70 50 61 72 73 65 2c 20 22 6f 62 6a 65  sg(pParse, "obje
23e84 63 74 20 6e 61 6d 65 20 72 65 73 65 72 76 65 64  ct name reserved
23e85 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
23e86 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  e: %s", zName);.
23e87 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
23e88 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72  E_ERROR;.  }.  r
23e89 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
23e8a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20  .}../*.** Begin 
23e8b 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20 6e  constructing a n
23e8c 65 77 20 74 61 62 6c 65 20 72 65 70 72 65 73 65  ew table represe
23e8d 6e 74 61 74 69 6f 6e 20 69 6e 20 6d 65 6d 6f 72  ntation in memor
23e8e 79 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 74  y.  This is.** t
23e8f 68 65 20 66 69 72 73 74 20 6f 66 20 73 65 76 65  he first of seve
23e90 72 61 6c 20 61 63 74 69 6f 6e 20 72 6f 75 74 69  ral action routi
23e91 6e 65 73 20 74 68 61 74 20 67 65 74 20 63 61 6c  nes that get cal
23e92 6c 65 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 0a  led in response.
23e93 2a 2a 20 74 6f 20 61 20 43 52 45 41 54 45 20 54  ** to a CREATE T
23e94 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  ABLE statement. 
23e95 20 49 6e 20 70 61 72 74 69 63 75 6c 61 72 2c 20   In particular, 
23e96 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
23e97 63 61 6c 6c 65 64 0a 2a 2a 20 61 66 74 65 72 20  called.** after 
23e98 73 65 65 69 6e 67 20 74 6f 6b 65 6e 73 20 22 43  seeing tokens "C
23e99 52 45 41 54 45 22 20 61 6e 64 20 22 54 41 42 4c  REATE" and "TABL
23e9a 45 22 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65  E" and the table
23e9b 20 6e 61 6d 65 2e 20 54 68 65 20 69 73 54 65 6d   name. The isTem
23e9c 70 0a 2a 2a 20 66 6c 61 67 20 69 73 20 74 72 75  p.** flag is tru
23e9d 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 73  e if the table s
23e9e 68 6f 75 6c 64 20 62 65 20 73 74 6f 72 65 64 20  hould be stored 
23e9f 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79  in the auxiliary
23ea0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
23ea1 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 20  e instead of in 
23ea2 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
23ea3 65 20 66 69 6c 65 2e 20 20 54 68 69 73 20 69 73  e file.  This is
23ea4 20 6e 6f 72 6d 61 6c 6c 79 20 74 68 65 20 63 61   normally the ca
23ea5 73 65 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 22  se.** when the "
23ea6 54 45 4d 50 22 20 6f 72 20 22 54 45 4d 50 4f 52  TEMP" or "TEMPOR
23ea7 41 52 59 22 20 6b 65 79 77 6f 72 64 20 6f 63 63  ARY" keyword occ
23ea8 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 0a 2a  urs in between.*
23ea9 2a 20 43 52 45 41 54 45 20 61 6e 64 20 54 41 42  * CREATE and TAB
23eaa 4c 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  LE..**.** The ne
23eab 77 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 69  w table record i
23eac 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e  s initialized an
23ead 64 20 70 75 74 20 69 6e 20 70 50 61 72 73 65 2d  d put in pParse-
23eae 3e 70 4e 65 77 54 61 62 6c 65 2e 0a 2a 2a 20 41  >pNewTable..** A
23eaf 73 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 43 52  s more of the CR
23eb0 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
23eb1 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2c 20  ment is parsed, 
23eb2 61 64 64 69 74 69 6f 6e 61 6c 20 61 63 74 69 6f  additional actio
23eb3 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 69  n.** routines wi
23eb4 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20  ll be called to 
23eb5 61 64 64 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61  add more informa
23eb6 74 69 6f 6e 20 74 6f 20 74 68 69 73 20 72 65 63  tion to this rec
23eb7 6f 72 64 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65  ord..** At the e
23eb8 6e 64 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  nd of the CREATE
23eb9 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
23eba 2c 20 74 68 65 20 73 71 6c 69 74 65 33 45 6e 64  , the sqlite3End
23ebb 54 61 62 6c 65 28 29 20 72 6f 75 74 69 6e 65 0a  Table() routine.
23ebc 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ** is called to 
23ebd 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 63 6f 6e  complete the con
23ebe 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65  struction of the
23ebf 20 6e 65 77 20 74 61 62 6c 65 20 72 65 63 6f 72   new table recor
23ec0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
23ec1 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
23ec2 33 53 74 61 72 74 54 61 62 6c 65 28 0a 20 20 50  3StartTable(.  P
23ec3 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
23ec4 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
23ec5 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  t */.  Token *pN
23ec6 61 6d 65 31 2c 20 20 20 2f 2a 20 46 69 72 73 74  ame1,   /* First
23ec7 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d   part of the nam
23ec8 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f  e of the table o
23ec9 72 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65  r view */.  Toke
23eca 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 2f 2a 20  n *pName2,   /* 
23ecb 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 74  Second part of t
23ecc 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
23ecd 61 62 6c 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a  able or view */.
23ece 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20 20 20    int isTemp,   
23ecf 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
23ed0 69 73 20 69 73 20 61 20 54 45 4d 50 20 74 61 62  is is a TEMP tab
23ed1 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69  le */.  int isVi
23ed2 65 77 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ew,      /* True
23ed3 20 69 66 20 74 68 69 73 20 69 73 20 61 20 56 49   if this is a VI
23ed4 45 57 20 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69  EW */.  int isVi
23ed5 72 74 75 61 6c 2c 20 20 20 2f 2a 20 54 72 75 65  rtual,   /* True
23ed6 20 69 66 20 74 68 69 73 20 69 73 20 61 20 56 49   if this is a VI
23ed7 52 54 55 41 4c 20 74 61 62 6c 65 20 2a 2f 0a 20  RTUAL table */. 
23ed8 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20   int noErr      
23ed9 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20    /* Do nothing 
23eda 69 66 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79  if table already
23edb 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20   exists */.){.  
23edc 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b 0a 20  Table *pTable;. 
23edd 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30   char *zName = 0
23ede 3b 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  ; /* The name of
23edf 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 2a   the new table *
23ee0 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
23ee1 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
23ee2 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69  Vdbe *v;.  int i
23ee3 44 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 44  Db;         /* D
23ee4 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 74  atabase number t
23ee5 6f 20 63 72 65 61 74 65 20 74 68 65 20 74 61 62  o create the tab
23ee6 6c 65 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  le in */.  Token
23ee7 20 2a 70 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 55   *pName;    /* U
23ee8 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 20  nqualified name 
23ee9 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
23eea 63 72 65 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20  create */..  /* 
23eeb 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65  The table or vie
23eec 77 20 6e 61 6d 65 20 74 6f 20 63 72 65 61 74 65  w name to create
23eed 20 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 68   is passed to th
23eee 69 73 20 72 6f 75 74 69 6e 65 20 76 69 61 20 74  is routine via t
23eef 6f 6b 65 6e 73 0a 20 20 2a 2a 20 70 4e 61 6d 65  okens.  ** pName
23ef0 31 20 61 6e 64 20 70 4e 61 6d 65 32 2e 20 49 66  1 and pName2. If
23ef1 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
23ef2 77 61 73 20 66 75 6c 6c 79 20 71 75 61 6c 69 66  was fully qualif
23ef3 69 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  ied, for example
23ef4 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 41  :.  **.  ** CREA
23ef5 54 45 20 54 41 42 4c 45 20 78 78 78 2e 79 79 79  TE TABLE xxx.yyy
23ef6 20 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 20 0a 20 20   (...);.  ** .  
23ef7 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69  ** Then pName1 i
23ef8 73 20 73 65 74 20 74 6f 20 22 78 78 78 22 20 61  s set to "xxx" a
23ef9 6e 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22 2e  nd pName2 "yyy".
23efa 20 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61   On the other ha
23efb 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 74  nd if.  ** the t
23efc 61 62 6c 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74  able name is not
23efd 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64   fully qualified
23efe 2c 20 69 2e 65 2e 3a 0a 20 20 2a 2a 0a 20 20 2a  , i.e.:.  **.  *
23eff 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 79  * CREATE TABLE y
23f00 79 79 28 2e 2e 2e 29 3b 0a 20 20 2a 2a 0a 20 20  yy(...);.  **.  
23f01 2a 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69  ** Then pName1 i
23f02 73 20 73 65 74 20 74 6f 20 22 79 79 79 22 20 61  s set to "yyy" a
23f03 6e 64 20 70 4e 61 6d 65 32 20 69 73 20 22 22 2e  nd pName2 is "".
23f04 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63  .  **.  ** The c
23f05 61 6c 6c 20 62 65 6c 6f 77 20 73 65 74 73 20 74  all below sets t
23f06 68 65 20 70 4e 61 6d 65 20 70 6f 69 6e 74 65 72  he pName pointer
23f07 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
23f08 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f   token (pName1 o
23f09 72 0a 20 20 2a 2a 20 70 4e 61 6d 65 32 29 20 74  r.  ** pName2) t
23f0a 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 75  hat stores the u
23f0b 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65  nqualified table
23f0c 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 72 69 61   name. The varia
23f0d 62 6c 65 20 69 44 62 20 69 73 0a 20 20 2a 2a 20  ble iDb is.  ** 
23f0e 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65 78  set to the index
23f0f 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
23f10 20 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20   that the table 
23f11 6f 72 20 76 69 65 77 20 69 73 20 74 6f 20 62 65  or view is to be
23f12 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e  .  ** created in
23f13 2e 0a 20 20 2a 2f 0a 20 20 69 44 62 20 3d 20 73  ..  */.  iDb = s
23f14 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
23f15 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31  e(pParse, pName1
23f16 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65  , pName2, &pName
23f17 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29  );.  if( iDb<0 )
23f18 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 21   return;.  if( !
23f19 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
23f1a 73 54 65 6d 70 20 26 26 20 69 44 62 3e 31 20 29  sTemp && iDb>1 )
23f1b 7b 0a 20 20 20 20 2f 2a 20 49 66 20 63 72 65 61  {.    /* If crea
23f1c 74 69 6e 67 20 61 20 74 65 6d 70 20 74 61 62 6c  ting a temp tabl
23f1d 65 2c 20 74 68 65 20 6e 61 6d 65 20 6d 61 79 20  e, the name may 
23f1e 6e 6f 74 20 62 65 20 71 75 61 6c 69 66 69 65 64  not be qualified
23f1f 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45   */.    sqlite3E
23f20 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
23f21 22 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65  "temporary table
23f22 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 75 6e   name must be un
23f23 71 75 61 6c 69 66 69 65 64 22 29 3b 0a 20 20 20  qualified");.   
23f24 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
23f25 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20  f( !OMIT_TEMPDB 
23f26 26 26 20 69 73 54 65 6d 70 20 29 20 69 44 62 20  && isTemp ) iDb 
23f27 3d 20 31 3b 0a 0a 20 20 70 50 61 72 73 65 2d 3e  = 1;..  pParse->
23f28 73 4e 61 6d 65 54 6f 6b 65 6e 20 3d 20 2a 70 4e  sNameToken = *pN
23f29 61 6d 65 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 73  ame;.  zName = s
23f2a 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
23f2b 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ken(db, pName);.
23f2c 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29    if( zName==0 )
23f2d 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 53   return;.  if( S
23f2e 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
23f2f 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65  3CheckObjectName
23f30 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20  (pParse, zName) 
23f31 29 7b 0a 20 20 20 20 67 6f 74 6f 20 62 65 67 69  ){.    goto begi
23f32 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20  n_table_error;. 
23f33 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69   }.  if( db->ini
23f34 74 2e 69 44 62 3d 3d 31 20 29 20 69 73 54 65 6d  t.iDb==1 ) isTem
23f35 70 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53  p = 1;.#ifndef S
23f36 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
23f37 52 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 72  RIZATION.  asser
23f38 74 28 20 28 69 73 54 65 6d 70 20 26 20 31 29 3d  t( (isTemp & 1)=
23f39 3d 69 73 54 65 6d 70 20 29 3b 0a 20 20 7b 0a 20  =isTemp );.  {. 
23f3a 20 20 20 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20     int code;.   
23f3b 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d   char *zDb = db-
23f3c 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b  >aDb[iDb].zName;
23f3d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
23f3e 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
23f3f 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c  , SQLITE_INSERT,
23f40 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 73   SCHEMA_TABLE(is
23f41 54 65 6d 70 29 2c 20 30 2c 20 7a 44 62 29 20 29  Temp), 0, zDb) )
23f42 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67  {.      goto beg
23f43 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
23f44 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73      }.    if( is
23f45 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 69 66  View ){.      if
23f46 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
23f47 26 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20  & isTemp ){.    
23f48 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
23f49 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49  E_CREATE_TEMP_VI
23f4a 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  EW;.      }else{
23f4b 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
23f4c 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 49  SQLITE_CREATE_VI
23f4d 45 57 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  EW;.      }.    
23f4e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
23f4f 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26   !OMIT_TEMPDB &&
23f50 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 20   isTemp ){.     
23f51 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45     code = SQLITE
23f52 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41 42  _CREATE_TEMP_TAB
23f53 4c 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  LE;.      }else{
23f54 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
23f55 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41  SQLITE_CREATE_TA
23f56 42 4c 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  BLE;.      }.   
23f57 20 7d 0a 20 20 20 20 69 66 28 20 21 69 73 56 69   }.    if( !isVi
23f58 72 74 75 61 6c 20 26 26 20 73 71 6c 69 74 65 33  rtual && sqlite3
23f59 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
23f5a 2c 20 63 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 30  , code, zName, 0
23f5b 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20  , zDb) ){.      
23f5c 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65  goto begin_table
23f5d 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20  _error;.    }.  
23f5e 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d  }.#endif..  /* M
23f5f 61 6b 65 20 73 75 72 65 20 74 68 65 20 6e 65 77  ake sure the new
23f60 20 74 61 62 6c 65 20 6e 61 6d 65 20 64 6f 65 73   table name does
23f61 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69 74   not collide wit
23f62 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20 20  h an existing.  
23f63 2a 2a 20 69 6e 64 65 78 20 6f 72 20 74 61 62 6c  ** index or tabl
23f64 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 73 61  e name in the sa
23f65 6d 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 73  me database.  Is
23f66 73 75 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  sue an error mes
23f67 73 61 67 65 20 69 66 0a 20 20 2a 2a 20 69 74 20  sage if.  ** it 
23f68 64 6f 65 73 2e 20 54 68 65 20 65 78 63 65 70 74  does. The except
23f69 69 6f 6e 20 69 73 20 69 66 20 74 68 65 20 73 74  ion is if the st
23f6a 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 70 61  atement being pa
23f6b 72 73 65 64 20 77 61 73 20 70 61 73 73 65 64 0a  rsed was passed.
23f6c 20 20 2a 2a 20 74 6f 20 61 6e 20 73 71 6c 69 74    ** to an sqlit
23f6d 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28  e3_declare_vtab(
23f6e 29 20 63 61 6c 6c 2e 20 49 6e 20 74 68 61 74 20  ) call. In that 
23f6f 63 61 73 65 20 6f 6e 6c 79 20 74 68 65 20 63 6f  case only the co
23f70 6c 75 6d 6e 20 6e 61 6d 65 73 0a 20 20 2a 2a 20  lumn names.  ** 
23f71 61 6e 64 20 74 79 70 65 73 20 77 69 6c 6c 20 62  and types will b
23f72 65 20 75 73 65 64 2c 20 73 6f 20 74 68 65 72 65  e used, so there
23f73 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74   is no need to t
23f74 65 73 74 20 66 6f 72 20 6e 61 6d 65 73 70 61 63  est for namespac
23f75 65 0a 20 20 2a 2a 20 63 6f 6c 6c 69 73 69 6f 6e  e.  ** collision
23f76 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 49  s..  */.  if( !I
23f77 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
23f78 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  {.    if( SQLITE
23f79 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
23f7a 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
23f7b 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67  {.      goto beg
23f7c 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
23f7d 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62 6c 65      }.    pTable
23f7e 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
23f7f 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 64  ble(db, zName, d
23f80 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
23f81 65 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  e);.    if( pTab
23f82 6c 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  le ){.      if( 
23f83 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20  !noErr ){.      
23f84 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
23f85 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65  g(pParse, "table
23f86 20 25 54 20 61 6c 72 65 61 64 79 20 65 78 69 73   %T already exis
23f87 74 73 22 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20  ts", pName);.   
23f88 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20     }.      goto 
23f89 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f  begin_table_erro
23f8a 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  r;.    }.    if(
23f8b 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65   sqlite3FindInde
23f8c 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 30 29 21  x(db, zName, 0)!
23f8d 3d 30 20 26 26 20 28 69 44 62 3d 3d 30 20 7c 7c  =0 && (iDb==0 ||
23f8e 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 29   !db->init.busy)
23f8f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
23f90 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
23f91 2c 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65  , "there is alre
23f92 61 64 79 20 61 6e 20 69 6e 64 65 78 20 6e 61 6d  ady an index nam
23f93 65 64 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  ed %s", zName);.
23f94 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e        goto begin
23f95 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20  _table_error;.  
23f96 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 54 61 62 6c    }.  }..  pTabl
23f97 65 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  e = sqlite3DbMal
23f98 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
23f99 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69 66  of(Table));.  if
23f9a 28 20 70 54 61 62 6c 65 3d 3d 30 20 29 7b 0a 20  ( pTable==0 ){. 
23f9b 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
23f9c 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 70 50 61  led = 1;.    pPa
23f9d 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
23f9e 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 70 50 61 72  _NOMEM;.    pPar
23f9f 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20  se->nErr++;.    
23fa0 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65  goto begin_table
23fa1 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 54  _error;.  }.  pT
23fa2 61 62 6c 65 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e  able->zName = zN
23fa3 61 6d 65 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 69  ame;.  pTable->i
23fa4 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 70 54 61  PKey = -1;.  pTa
23fa5 62 6c 65 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64  ble->pSchema = d
23fa6 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
23fa7 65 6d 61 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 6e  ema;.  pTable->n
23fa8 52 65 66 20 3d 20 31 3b 0a 20 20 70 54 61 62 6c  Ref = 1;.  pTabl
23fa9 65 2d 3e 64 62 4d 65 6d 20 3d 20 30 3b 0a 20 20  e->dbMem = 0;.  
23faa 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
23fab 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  pNewTable==0 );.
23fac 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61    pParse->pNewTa
23fad 62 6c 65 20 3d 20 70 54 61 62 6c 65 3b 0a 0a 20  ble = pTable;.. 
23fae 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74   /* If this is t
23faf 68 65 20 6d 61 67 69 63 20 73 71 6c 69 74 65 5f  he magic sqlite_
23fb0 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 75  sequence table u
23fb1 73 65 64 20 62 79 20 61 75 74 6f 69 6e 63 72 65  sed by autoincre
23fb2 6d 65 6e 74 2c 0a 20 20 2a 2a 20 74 68 65 6e 20  ment,.  ** then 
23fb3 72 65 63 6f 72 64 20 61 20 70 6f 69 6e 74 65 72  record a pointer
23fb4 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20 69   to this table i
23fb5 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  n the main datab
23fb6 61 73 65 20 73 74 72 75 63 74 75 72 65 0a 20 20  ase structure.  
23fb7 2a 2a 20 73 6f 20 74 68 61 74 20 49 4e 53 45 52  ** so that INSER
23fb8 54 20 63 61 6e 20 66 69 6e 64 20 74 68 65 20 74  T can find the t
23fb9 61 62 6c 65 20 65 61 73 69 6c 79 2e 0a 20 20 2a  able easily..  *
23fba 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
23fbb 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
23fbc 45 4e 54 0a 20 20 69 66 28 20 21 70 50 61 72 73  ENT.  if( !pPars
23fbd 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 73 74 72  e->nested && str
23fbe 63 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69  cmp(zName, "sqli
23fbf 74 65 5f 73 65 71 75 65 6e 63 65 22 29 3d 3d 30  te_sequence")==0
23fc0 20 29 7b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e   ){.    pTable->
23fc1 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62  pSchema->pSeqTab
23fc2 20 3d 20 70 54 61 62 6c 65 3b 0a 20 20 7d 0a 23   = pTable;.  }.#
23fc3 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69  endif..  /* Begi
23fc4 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 74 68 65  n generating the
23fc5 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
23fc6 69 6e 73 65 72 74 20 74 68 65 20 74 61 62 6c 65  insert the table
23fc7 20 72 65 63 6f 72 64 20 69 6e 74 6f 0a 20 20 2a   record into.  *
23fc8 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53  * the SQLITE_MAS
23fc9 54 45 52 20 74 61 62 6c 65 2e 20 20 4e 6f 74 65  TER table.  Note
23fca 20 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 74   in particular t
23fcb 68 61 74 20 77 65 20 6d 75 73 74 20 67 6f 20 61  hat we must go a
23fcc 68 65 61 64 0a 20 20 2a 2a 20 61 6e 64 20 61 6c  head.  ** and al
23fcd 6c 6f 63 61 74 65 20 74 68 65 20 72 65 63 6f 72  locate the recor
23fce 64 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  d number for the
23fcf 20 74 61 62 6c 65 20 65 6e 74 72 79 20 6e 6f 77   table entry now
23fd0 2e 20 20 42 65 66 6f 72 65 20 61 6e 79 0a 20 20  .  Before any.  
23fd1 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f  ** PRIMARY KEY o
23fd2 72 20 55 4e 49 51 55 45 20 6b 65 79 77 6f 72 64  r UNIQUE keyword
23fd3 73 20 61 72 65 20 70 61 72 73 65 64 2e 20 20 54  s are parsed.  T
23fd4 68 6f 73 65 20 6b 65 79 77 6f 72 64 73 20 77 69  hose keywords wi
23fd5 6c 6c 20 63 61 75 73 65 0a 20 20 2a 2a 20 69 6e  ll cause.  ** in
23fd6 64 69 63 65 73 20 74 6f 20 62 65 20 63 72 65 61  dices to be crea
23fd7 74 65 64 20 61 6e 64 20 74 68 65 20 74 61 62 6c  ted and the tabl
23fd8 65 20 72 65 63 6f 72 64 20 6d 75 73 74 20 63 6f  e record must co
23fd9 6d 65 20 62 65 66 6f 72 65 20 74 68 65 20 0a 20  me before the . 
23fda 20 2a 2a 20 69 6e 64 69 63 65 73 2e 20 20 48 65   ** indices.  He
23fdb 6e 63 65 2c 20 74 68 65 20 72 65 63 6f 72 64 20  nce, the record 
23fdc 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74  number for the t
23fdd 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61 6c 6c  able must be all
23fde 6f 63 61 74 65 64 0a 20 20 2a 2a 20 6e 6f 77 2e  ocated.  ** now.
23fdf 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d  .  */.  if( !db-
23fe0 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 28 76  >init.busy && (v
23fe1 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
23fe2 65 28 70 50 61 72 73 65 29 29 21 3d 30 20 29 7b  e(pParse))!=0 ){
23fe3 0a 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20  .    int j1;.   
23fe4 20 69 6e 74 20 66 69 6c 65 46 6f 72 6d 61 74 3b   int fileFormat;
23fe5 0a 20 20 20 20 69 6e 74 20 72 65 67 31 2c 20 72  .    int reg1, r
23fe6 65 67 32 2c 20 72 65 67 33 3b 0a 20 20 20 20 73  eg2, reg3;.    s
23fe7 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
23fe8 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
23fe9 2c 20 30 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e  , 0, iDb);..#ifn
23fea 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23feb 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
23fec 20 69 66 28 20 69 73 56 69 72 74 75 61 6c 20 29   if( isVirtual )
23fed 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
23fee 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f  dbeAddOp0(v, OP_
23fef 56 42 65 67 69 6e 29 3b 0a 20 20 20 20 7d 0a 23  VBegin);.    }.#
23ff0 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49 66  endif..    /* If
23ff1 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74   the file format
23ff2 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67 20 69 6e   and encoding in
23ff3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
23ff4 76 65 20 6e 6f 74 20 62 65 65 6e 20 73 65 74 2c  ve not been set,
23ff5 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 74 68 65   .    ** set the
23ff6 6d 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20  m now..    */.  
23ff7 20 20 72 65 67 31 20 3d 20 70 50 61 72 73 65 2d    reg1 = pParse-
23ff8 3e 72 65 67 52 6f 77 69 64 20 3d 20 2b 2b 70 50  >regRowid = ++pP
23ff9 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
23ffa 72 65 67 32 20 3d 20 70 50 61 72 73 65 2d 3e 72  reg2 = pParse->r
23ffb 65 67 52 6f 6f 74 20 3d 20 2b 2b 70 50 61 72 73  egRoot = ++pPars
23ffc 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67  e->nMem;.    reg
23ffd 33 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  3 = ++pParse->nM
23ffe 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  em;.    sqlite3V
23fff 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
24000 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  ReadCookie, iDb,
24001 20 72 65 67 33 2c 20 42 54 52 45 45 5f 46 49 4c   reg3, BTREE_FIL
24002 45 5f 46 4f 52 4d 41 54 29 3b 0a 20 20 20 20 73  E_FORMAT);.    s
24003 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
24004 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20  ree(v, iDb);.   
24005 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
24006 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
24007 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 66 69 6c  , reg3);.    fil
24008 65 46 6f 72 6d 61 74 20 3d 20 28 64 62 2d 3e 66  eFormat = (db->f
24009 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4c 65  lags & SQLITE_Le
2400a 67 61 63 79 46 69 6c 65 46 6d 74 29 21 3d 30 20  gacyFileFmt)!=0 
2400b 3f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ?.              
2400c 20 20 20 20 31 20 3a 20 53 51 4c 49 54 45 5f 4d      1 : SQLITE_M
2400d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 3b 0a  AX_FILE_FORMAT;.
2400e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2400f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
24010 67 65 72 2c 20 66 69 6c 65 46 6f 72 6d 61 74 2c  ger, fileFormat,
24011 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69   reg3);.    sqli
24012 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
24013 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69   OP_SetCookie, i
24014 44 62 2c 20 42 54 52 45 45 5f 46 49 4c 45 5f 46  Db, BTREE_FILE_F
24015 4f 52 4d 41 54 2c 20 72 65 67 33 29 3b 0a 20 20  ORMAT, reg3);.  
24016 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24017 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
24018 72 2c 20 45 4e 43 28 64 62 29 2c 20 72 65 67 33  r, ENC(db), reg3
24019 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2401a 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53  beAddOp3(v, OP_S
2401b 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42  etCookie, iDb, B
2401c 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49  TREE_TEXT_ENCODI
2401d 4e 47 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73  NG, reg3);.    s
2401e 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
2401f 72 65 28 76 2c 20 6a 31 29 3b 0a 0a 20 20 20 20  re(v, j1);..    
24020 2f 2a 20 54 68 69 73 20 6a 75 73 74 20 63 72 65  /* This just cre
24021 61 74 65 73 20 61 20 70 6c 61 63 65 2d 68 6f 6c  ates a place-hol
24022 64 65 72 20 72 65 63 6f 72 64 20 69 6e 20 74 68  der record in th
24023 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
24024 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 54 68  table..    ** Th
24025 65 20 72 65 63 6f 72 64 20 63 72 65 61 74 65 64  e record created
24026 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69   does not contai
24027 6e 20 61 6e 79 74 68 69 6e 67 20 79 65 74 2e 20  n anything yet. 
24028 20 49 74 20 77 69 6c 6c 20 62 65 20 72 65 70 6c   It will be repl
24029 61 63 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 74  aced.    ** by t
2402a 68 65 20 72 65 61 6c 20 65 6e 74 72 79 20 69 6e  he real entry in
2402b 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
2402c 61 74 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62  at sqlite3EndTab
2402d 6c 65 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  le()..    **.   
2402e 20 2a 2a 20 54 68 65 20 72 6f 77 69 64 20 66 6f   ** The rowid fo
2402f 72 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20  r the new entry 
24030 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67 69 73  is left in regis
24031 74 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52  ter pParse->regR
24032 6f 77 69 64 2e 0a 20 20 20 20 2a 2a 20 54 68 65  owid..    ** The
24033 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
24034 72 20 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62  r of the new tab
24035 6c 65 20 69 73 20 6c 65 66 74 20 69 6e 20 72 65  le is left in re
24036 67 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f  g pParse->regRoo
24037 74 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f  t..    ** The ro
24038 77 69 64 20 61 6e 64 20 72 6f 6f 74 20 70 61 67  wid and root pag
24039 65 20 6e 75 6d 62 65 72 20 76 61 6c 75 65 73 20  e number values 
2403a 61 72 65 20 6e 65 65 64 65 64 20 62 79 20 74 68  are needed by th
2403b 65 20 63 6f 64 65 20 74 68 61 74 0a 20 20 20 20  e code that.    
2403c 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62  ** sqlite3EndTab
2403d 6c 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74 65  le will generate
2403e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 20 21 64 65  ..    */.#if !de
2403f 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
24040 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69  T_VIEW) || !defi
24041 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
24042 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a 20 20  VIRTUALTABLE).  
24043 20 20 69 66 28 20 69 73 56 69 65 77 20 7c 7c 20    if( isView || 
24044 69 73 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20  isVirtual ){.   
24045 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24046 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
24047 65 72 2c 20 30 2c 20 72 65 67 32 29 3b 0a 20 20  er, 0, reg2);.  
24048 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
24049 20 20 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74     {.      sqlit
2404a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2404b 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 2c 20  OP_CreateTable, 
2404c 69 44 62 2c 20 72 65 67 32 29 3b 0a 20 20 20 20  iDb, reg2);.    
2404d 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65  }.    sqlite3Ope
2404e 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 70 50 61  nMasterTable(pPa
2404f 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  rse, iDb);.    s
24050 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24051 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c  (v, OP_NewRowid,
24052 20 30 2c 20 72 65 67 31 29 3b 0a 20 20 20 20 73   0, reg1);.    s
24053 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24054 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
24055 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74  reg3);.    sqlit
24056 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
24057 4f 50 5f 49 6e 73 65 72 74 2c 20 30 2c 20 72 65  OP_Insert, 0, re
24058 67 33 2c 20 72 65 67 31 29 3b 0a 20 20 20 20 73  g3, reg1);.    s
24059 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2405a 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50  P5(v, OPFLAG_APP
2405b 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  END);.    sqlite
2405c 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
2405d 50 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 0a 20  P_Close);.  }.. 
2405e 20 2f 2a 20 4e 6f 72 6d 61 6c 20 28 6e 6f 6e 2d   /* Normal (non-
2405f 65 72 72 6f 72 29 20 72 65 74 75 72 6e 2e 20 2a  error) return. *
24060 2f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f  /.  return;..  /
24061 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
24062 63 75 72 73 2c 20 77 65 20 6a 75 6d 70 20 68 65  curs, we jump he
24063 72 65 20 2a 2f 0a 62 65 67 69 6e 5f 74 61 62 6c  re */.begin_tabl
24064 65 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74  e_error:.  sqlit
24065 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61  e3DbFree(db, zNa
24066 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d  me);.  return;.}
24067 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63  ../*.** This mac
24068 72 6f 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  ro is used to co
24069 6d 70 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67  mpare two string
2406a 73 20 69 6e 20 61 20 63 61 73 65 2d 69 6e 73 65  s in a case-inse
2406b 6e 73 69 74 69 76 65 20 6d 61 6e 6e 65 72 2e 0a  nsitive manner..
2406c 2a 2a 20 49 74 20 69 73 20 73 6c 69 67 68 74 6c  ** It is slightl
2406d 79 20 66 61 73 74 65 72 20 74 68 61 6e 20 63 61  y faster than ca
2406e 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 53 74 72  lling sqlite3Str
2406f 49 43 6d 70 28 29 20 64 69 72 65 63 74 6c 79 2c  ICmp() directly,
24070 20 62 75 74 0a 2a 2a 20 70 72 6f 64 75 63 65 73   but.** produces
24071 20 6c 61 72 67 65 72 20 63 6f 64 65 2e 0a 2a 2a   larger code..**
24072 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 54 68 69  .** WARNING: Thi
24073 73 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74 20 63  s macro is not c
24074 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 74  ompatible with t
24075 68 65 20 73 74 72 63 6d 70 28 29 20 66 61 6d 69  he strcmp() fami
24076 6c 79 2e 20 49 74 0a 2a 2a 20 72 65 74 75 72 6e  ly. It.** return
24077 73 20 74 72 75 65 20 69 66 20 74 68 65 20 74 77  s true if the tw
24078 6f 20 73 74 72 69 6e 67 73 20 61 72 65 20 65 71  o strings are eq
24079 75 61 6c 2c 20 6f 74 68 65 72 77 69 73 65 20 66  ual, otherwise f
2407a 61 6c 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  alse..*/.#define
2407b 20 53 54 52 49 43 4d 50 28 78 2c 20 79 29 20 28   STRICMP(x, y) (
2407c 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  \.sqlite3UpperTo
2407d 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64  Lower[*(unsigned
2407e 20 63 68 61 72 20 2a 29 28 78 29 5d 3d 3d 20 20   char *)(x)]==  
2407f 20 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72 54   \.sqlite3UpperT
24080 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65  oLower[*(unsigne
24081 64 20 63 68 61 72 20 2a 29 28 79 29 5d 20 20 20  d char *)(y)]   
24082 20 20 5c 0a 26 26 20 73 71 6c 69 74 65 33 53 74    \.&& sqlite3St
24083 72 49 43 6d 70 28 28 78 29 2b 31 2c 28 79 29 2b  rICmp((x)+1,(y)+
24084 31 29 3d 3d 30 20 29 0a 0a 2f 2a 0a 2a 2a 20 41  1)==0 )../*.** A
24085 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20  dd a new column 
24086 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  to the table cur
24087 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e  rently being con
24088 73 74 72 75 63 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  structed..**.** 
24089 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
2408a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   this routine on
2408b 63 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  ce for each colu
2408c 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a  mn declaration.*
2408d 2a 20 69 6e 20 61 20 43 52 45 41 54 45 20 54 41  * in a CREATE TA
2408e 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  BLE statement.  
2408f 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
24090 65 28 29 20 67 65 74 73 20 63 61 6c 6c 65 64 0a  e() gets called.
24091 2a 2a 20 66 69 72 73 74 20 74 6f 20 67 65 74 20  ** first to get 
24092 74 68 69 6e 67 73 20 67 6f 69 6e 67 2e 20 20 54  things going.  T
24093 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
24094 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 65   is called for e
24095 61 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a  ach.** column..*
24096 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24097 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
24098 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 70 50  Column(Parse *pP
24099 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61  arse, Token *pNa
2409a 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  me){.  Table *p;
2409b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72  .  int i;.  char
2409c 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70   *z;.  Column *p
2409d 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Col;.  sqlite3 *
2409e 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2409f 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72  .  if( (p = pPar
240a0 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d  se->pNewTable)==
240a1 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66 20  0 ) return;.#if 
240a2 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
240a3 4e 0a 20 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 2b  N.  if( p->nCol+
240a4 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  1>db->aLimit[SQL
240a5 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e  ITE_LIMIT_COLUMN
240a6 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
240a7 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
240a8 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d   "too many colum
240a9 6e 73 20 6f 6e 20 25 73 22 2c 20 70 2d 3e 7a 4e  ns on %s", p->zN
240aa 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ame);.    return
240ab 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7a  ;.  }.#endif.  z
240ac 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
240ad 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d  omToken(db, pNam
240ae 65 29 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29  e);.  if( z==0 )
240af 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69   return;.  for(i
240b0 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69  =0; i<p->nCol; i
240b1 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 53 54 52  ++){.    if( STR
240b2 49 43 4d 50 28 7a 2c 20 70 2d 3e 61 43 6f 6c 5b  ICMP(z, p->aCol[
240b3 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  i].zName) ){.   
240b4 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
240b5 73 67 28 70 50 61 72 73 65 2c 20 22 64 75 70 6c  sg(pParse, "dupl
240b6 69 63 61 74 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  icate column nam
240b7 65 3a 20 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20  e: %s", z);.    
240b8 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
240b9 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72 65  db, z);.      re
240ba 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
240bb 20 20 69 66 28 20 28 70 2d 3e 6e 43 6f 6c 20 26    if( (p->nCol &
240bc 20 30 78 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20   0x7)==0 ){.    
240bd 43 6f 6c 75 6d 6e 20 2a 61 4e 65 77 3b 0a 20 20  Column *aNew;.  
240be 20 20 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    aNew = sqlite3
240bf 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 70 2d 3e  DbRealloc(db,p->
240c0 61 43 6f 6c 2c 28 70 2d 3e 6e 43 6f 6c 2b 38 29  aCol,(p->nCol+8)
240c1 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b  *sizeof(p->aCol[
240c2 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 61 4e  0]));.    if( aN
240c3 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ew==0 ){.      s
240c4 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
240c5 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72   z);.      retur
240c6 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  n;.    }.    p->
240c7 61 43 6f 6c 20 3d 20 61 4e 65 77 3b 0a 20 20 7d  aCol = aNew;.  }
240c8 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43  .  pCol = &p->aC
240c9 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20 6d  ol[p->nCol];.  m
240ca 65 6d 73 65 74 28 70 43 6f 6c 2c 20 30 2c 20 73  emset(pCol, 0, s
240cb 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d  izeof(p->aCol[0]
240cc 29 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d  ));.  pCol->zNam
240cd 65 20 3d 20 7a 3b 0a 20 0a 20 20 2f 2a 20 49 66  e = z;. .  /* If
240ce 20 74 68 65 72 65 20 69 73 20 6e 6f 20 74 79 70   there is no typ
240cf 65 20 73 70 65 63 69 66 69 65 64 2c 20 63 6f 6c  e specified, col
240d0 75 6d 6e 73 20 68 61 76 65 20 74 68 65 20 64 65  umns have the de
240d1 66 61 75 6c 74 20 61 66 66 69 6e 69 74 79 0a 20  fault affinity. 
240d2 20 2a 2a 20 27 4e 4f 4e 45 27 2e 20 49 66 20 74   ** 'NONE'. If t
240d3 68 65 72 65 20 69 73 20 61 20 74 79 70 65 20 73  here is a type s
240d4 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20 73  pecified, then s
240d5 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54  qlite3AddColumnT
240d6 79 70 65 28 29 20 77 69 6c 6c 0a 20 20 2a 2a 20  ype() will.  ** 
240d7 62 65 20 63 61 6c 6c 65 64 20 6e 65 78 74 20 74  be called next t
240d8 6f 20 73 65 74 20 70 43 6f 6c 2d 3e 61 66 66 69  o set pCol->affi
240d9 6e 69 74 79 20 63 6f 72 72 65 63 74 6c 79 2e 0a  nity correctly..
240da 20 20 2a 2f 0a 20 20 70 43 6f 6c 2d 3e 61 66 66    */.  pCol->aff
240db 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41  inity = SQLITE_A
240dc 46 46 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 6e 43  FF_NONE;.  p->nC
240dd 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ol++;.}../*.** T
240de 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
240df 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
240e0 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65  ser while in the
240e1 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61   middle of.** pa
240e2 72 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54  rsing a CREATE T
240e3 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  ABLE statement. 
240e4 20 41 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f   A "NOT NULL" co
240e5 6e 73 74 72 61 69 6e 74 20 68 61 73 0a 2a 2a 20  nstraint has.** 
240e6 62 65 65 6e 20 73 65 65 6e 20 6f 6e 20 61 20 63  been seen on a c
240e7 6f 6c 75 6d 6e 2e 20 20 54 68 69 73 20 72 6f 75  olumn.  This rou
240e8 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 6e 6f  tine sets the no
240e9 74 4e 75 6c 6c 20 66 6c 61 67 20 6f 6e 0a 2a 2a  tNull flag on.**
240ea 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 75 72 72   the column curr
240eb 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73  ently under cons
240ec 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  truction..*/.SQL
240ed 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
240ee 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75   sqlite3AddNotNu
240ef 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ll(Parse *pParse
240f0 2c 20 69 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a  , int onError){.
240f1 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 70 20    Table *p;.  p 
240f2 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
240f3 62 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  ble;.  if( p==0 
240f4 7c 7c 20 4e 45 56 45 52 28 70 2d 3e 6e 43 6f 6c  || NEVER(p->nCol
240f5 3c 31 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  <1) ) return;.  
240f6 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d  p->aCol[p->nCol-
240f7 31 5d 2e 6e 6f 74 4e 75 6c 6c 20 3d 20 28 75 38  1].notNull = (u8
240f8 29 6f 6e 45 72 72 6f 72 3b 0a 7d 0a 0a 2f 2a 0a  )onError;.}../*.
240f9 2a 2a 20 53 63 61 6e 20 74 68 65 20 63 6f 6c 75  ** Scan the colu
240fa 6d 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a 54 79  mn type name zTy
240fb 70 65 20 28 6c 65 6e 67 74 68 20 6e 54 79 70 65  pe (length nType
240fc 29 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65  ) and return the
240fd 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20 61  .** associated a
240fe 66 66 69 6e 69 74 79 20 74 79 70 65 2e 0a 2a 2a  ffinity type..**
240ff 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
24100 20 64 6f 65 73 20 61 20 63 61 73 65 2d 69 6e 64   does a case-ind
24101 65 70 65 6e 64 65 6e 74 20 73 65 61 72 63 68 20  ependent search 
24102 6f 66 20 7a 54 79 70 65 20 66 6f 72 20 74 68 65  of zType for the
24103 20 0a 2a 2a 20 73 75 62 73 74 72 69 6e 67 73 20   .** substrings 
24104 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
24105 20 74 61 62 6c 65 2e 20 49 66 20 6f 6e 65 20 6f   table. If one o
24106 66 20 74 68 65 20 73 75 62 73 74 72 69 6e 67 73  f the substrings
24107 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 74 68   is.** found, th
24108 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
24109 61 66 66 69 6e 69 74 79 20 69 73 20 72 65 74 75  affinity is retu
2410a 72 6e 65 64 2e 20 49 66 20 7a 54 79 70 65 20 63  rned. If zType c
2410b 6f 6e 74 61 69 6e 73 0a 2a 2a 20 6d 6f 72 65 20  ontains.** more 
2410c 74 68 61 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  than one of the 
2410d 73 75 62 73 74 72 69 6e 67 73 2c 20 65 6e 74 72  substrings, entr
2410e 69 65 73 20 74 6f 77 61 72 64 20 74 68 65 20 74  ies toward the t
2410f 6f 70 20 6f 66 20 0a 2a 2a 20 74 68 65 20 74 61  op of .** the ta
24110 62 6c 65 20 74 61 6b 65 20 70 72 69 6f 72 69 74  ble take priorit
24111 79 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  y. For example, 
24112 69 66 20 7a 54 79 70 65 20 69 73 20 27 42 4c 4f  if zType is 'BLO
24113 42 49 4e 54 27 2c 20 0a 2a 2a 20 53 51 4c 49 54  BINT', .** SQLIT
24114 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 69 73  E_AFF_INTEGER is
24115 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
24116 20 53 75 62 73 74 72 69 6e 67 20 20 20 20 20 7c   Substring     |
24117 20 41 66 66 69 6e 69 74 79 0a 2a 2a 20 2d 2d 2d   Affinity.** ---
24118 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
24119 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
2411a 20 27 49 4e 54 27 20 20 20 20 20 20 20 20 20 7c   'INT'         |
2411b 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
2411c 47 45 52 0a 2a 2a 20 27 43 48 41 52 27 20 20 20  GER.** 'CHAR'   
2411d 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46       | SQLITE_AF
2411e 46 5f 54 45 58 54 0a 2a 2a 20 27 43 4c 4f 42 27  F_TEXT.** 'CLOB'
2411f 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45          | SQLITE
24120 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 54 45  _AFF_TEXT.** 'TE
24121 58 54 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c  XT'        | SQL
24122 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20  ITE_AFF_TEXT.** 
24123 27 42 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20  'BLOB'        | 
24124 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a  SQLITE_AFF_NONE.
24125 2a 2a 20 27 52 45 41 4c 27 20 20 20 20 20 20 20  ** 'REAL'       
24126 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45   | SQLITE_AFF_RE
24127 41 4c 0a 2a 2a 20 27 46 4c 4f 41 27 20 20 20 20  AL.** 'FLOA'    
24128 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46      | SQLITE_AFF
24129 5f 52 45 41 4c 0a 2a 2a 20 27 44 4f 55 42 27 20  _REAL.** 'DOUB' 
2412a 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
2412b 41 46 46 5f 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 49  AFF_REAL.**.** I
2412c 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 73 75  f none of the su
2412d 62 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65 20  bstrings in the 
2412e 61 62 6f 76 65 20 74 61 62 6c 65 20 61 72 65 20  above table are 
2412f 66 6f 75 6e 64 2c 0a 2a 2a 20 53 51 4c 49 54 45  found,.** SQLITE
24130 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 69 73 20  _AFF_NUMERIC is 
24131 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
24132 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
24133 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69 74 79   sqlite3Affinity
24134 54 79 70 65 28 63 6f 6e 73 74 20 63 68 61 72 20  Type(const char 
24135 2a 7a 49 6e 29 7b 0a 20 20 75 33 32 20 68 20 3d  *zIn){.  u32 h =
24136 20 30 3b 0a 20 20 63 68 61 72 20 61 66 66 20 3d   0;.  char aff =
24137 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45   SQLITE_AFF_NUME
24138 52 49 43 3b 0a 0a 20 20 69 66 28 20 7a 49 6e 20  RIC;..  if( zIn 
24139 29 20 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20  ) while( zIn[0] 
2413a 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 38  ){.    h = (h<<8
2413b 29 20 2b 20 73 71 6c 69 74 65 33 55 70 70 65 72  ) + sqlite3Upper
2413c 54 6f 4c 6f 77 65 72 5b 28 2a 7a 49 6e 29 26 30  ToLower[(*zIn)&0
2413d 78 66 66 5d 3b 0a 20 20 20 20 7a 49 6e 2b 2b 3b  xff];.    zIn++;
2413e 0a 20 20 20 20 69 66 28 20 68 3d 3d 28 28 27 63  .    if( h==(('c
2413f 27 3c 3c 32 34 29 2b 28 27 68 27 3c 3c 31 36 29  '<<24)+('h'<<16)
24140 2b 28 27 61 27 3c 3c 38 29 2b 27 72 27 29 20 29  +('a'<<8)+'r') )
24141 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {             /*
24142 20 43 48 41 52 20 2a 2f 0a 20 20 20 20 20 20 61   CHAR */.      a
24143 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
24144 54 45 58 54 3b 20 0a 20 20 20 20 7d 65 6c 73 65  TEXT; .    }else
24145 20 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32   if( h==(('c'<<2
24146 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f  4)+('l'<<16)+('o
24147 27 3c 3c 38 29 2b 27 62 27 29 20 29 7b 20 20 20  '<<8)+'b') ){   
24148 20 20 20 20 2f 2a 20 43 4c 4f 42 20 2a 2f 0a 20      /* CLOB */. 
24149 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54       aff = SQLIT
2414a 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20  E_AFF_TEXT;.    
2414b 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27  }else if( h==(('
2414c 74 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36  t'<<24)+('e'<<16
2414d 29 2b 28 27 78 27 3c 3c 38 29 2b 27 74 27 29 20  )+('x'<<8)+'t') 
2414e 29 7b 20 20 20 20 20 20 20 2f 2a 20 54 45 58 54  ){       /* TEXT
2414f 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20   */.      aff = 
24150 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b  SQLITE_AFF_TEXT;
24151 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68  .    }else if( h
24152 3d 3d 28 28 27 62 27 3c 3c 32 34 29 2b 28 27 6c  ==(('b'<<24)+('l
24153 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b  '<<16)+('o'<<8)+
24154 27 62 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a  'b')          /*
24155 20 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 20   BLOB */.       
24156 20 26 26 20 28 61 66 66 3d 3d 53 51 4c 49 54 45   && (aff==SQLITE
24157 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 7c 20  _AFF_NUMERIC || 
24158 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  aff==SQLITE_AFF_
24159 52 45 41 4c 29 20 29 7b 0a 20 20 20 20 20 20 61  REAL) ){.      a
2415a 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ff = SQLITE_AFF_
2415b 4e 4f 4e 45 3b 0a 23 69 66 6e 64 65 66 20 53 51  NONE;.#ifndef SQ
2415c 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
2415d 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 7d 65 6c  NG_POINT.    }el
2415e 73 65 20 69 66 28 20 68 3d 3d 28 28 27 72 27 3c  se if( h==(('r'<
2415f 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28  <24)+('e'<<16)+(
24160 27 61 27 3c 3c 38 29 2b 27 6c 27 29 20 20 20 20  'a'<<8)+'l')    
24161 20 20 20 20 20 20 2f 2a 20 52 45 41 4c 20 2a 2f        /* REAL */
24162 0a 20 20 20 20 20 20 20 20 26 26 20 61 66 66 3d  .        && aff=
24163 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45  =SQLITE_AFF_NUME
24164 52 49 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66  RIC ){.      aff
24165 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45   = SQLITE_AFF_RE
24166 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  AL;.    }else if
24167 28 20 68 3d 3d 28 28 27 66 27 3c 3c 32 34 29 2b  ( h==(('f'<<24)+
24168 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c  ('l'<<16)+('o'<<
24169 38 29 2b 27 61 27 29 20 20 20 20 20 20 20 20 20  8)+'a')         
2416a 20 2f 2a 20 46 4c 4f 41 20 2a 2f 0a 20 20 20 20   /* FLOA */.    
2416b 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49      && aff==SQLI
2416c 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29  TE_AFF_NUMERIC )
2416d 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51  {.      aff = SQ
2416e 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20  LITE_AFF_REAL;. 
2416f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d     }else if( h==
24170 28 28 27 64 27 3c 3c 32 34 29 2b 28 27 6f 27 3c  (('d'<<24)+('o'<
24171 3c 31 36 29 2b 28 27 75 27 3c 3c 38 29 2b 27 62  <16)+('u'<<8)+'b
24172 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  ')          /* D
24173 4f 55 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 26  OUB */.        &
24174 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46  & aff==SQLITE_AF
24175 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20 20 20  F_NUMERIC ){.   
24176 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f     aff = SQLITE_
24177 41 46 46 5f 52 45 41 4c 3b 0a 23 65 6e 64 69 66  AFF_REAL;.#endif
24178 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28  .    }else if( (
24179 68 26 30 78 30 30 46 46 46 46 46 46 29 3d 3d 28  h&0x00FFFFFF)==(
2417a 28 27 69 27 3c 3c 31 36 29 2b 28 27 6e 27 3c 3c  ('i'<<16)+('n'<<
2417b 38 29 2b 27 74 27 29 20 29 7b 20 20 20 20 2f 2a  8)+'t') ){    /*
2417c 20 49 4e 54 20 2a 2f 0a 20 20 20 20 20 20 61 66   INT */.      af
2417d 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49  f = SQLITE_AFF_I
2417e 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 62 72  NTEGER;.      br
2417f 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  eak;.    }.  }..
24180 20 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a    return aff;.}.
24181 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
24182 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ine is called by
24183 20 74 68 65 20 70 61 72 73 65 72 20 77 68 69 6c   the parser whil
24184 65 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  e in the middle 
24185 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67 20 61 20  of.** parsing a 
24186 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
24187 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69  tement.  The pFi
24188 72 73 74 20 74 6f 6b 65 6e 20 69 73 20 74 68 65  rst token is the
24189 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e 20   first.** token 
2418a 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63 65 20  in the sequence 
2418b 6f 66 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 64  of tokens that d
2418c 65 73 63 72 69 62 65 20 74 68 65 20 74 79 70 65  escribe the type
2418d 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d   of the.** colum
2418e 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65  n currently unde
2418f 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20  r construction. 
24190 20 20 70 4c 61 73 74 20 69 73 20 74 68 65 20 6c    pLast is the l
24191 61 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20  ast token.** in 
24192 74 68 65 20 73 65 71 75 65 6e 63 65 2e 20 20 55  the sequence.  U
24193 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74  se this informat
24194 69 6f 6e 20 74 6f 20 63 6f 6e 73 74 72 75 63 74  ion to construct
24195 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61   a string.** tha
24196 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74  t contains the t
24197 79 70 65 6e 61 6d 65 20 6f 66 20 74 68 65 20 63  ypename of the c
24198 6f 6c 75 6d 6e 20 61 6e 64 20 73 74 6f 72 65 20  olumn and store 
24199 74 68 61 74 20 73 74 72 69 6e 67 0a 2a 2a 20 69  that string.** i
2419a 6e 20 7a 54 79 70 65 2e 0a 2a 2f 20 0a 53 51 4c  n zType..*/ .SQL
2419b 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2419c 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d   sqlite3AddColum
2419d 6e 54 79 70 65 28 50 61 72 73 65 20 2a 70 50 61  nType(Parse *pPa
2419e 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54 79 70  rse, Token *pTyp
2419f 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a  e){.  Table *p;.
241a0 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a    Column *pCol;.
241a1 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  .  p = pParse->p
241a2 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20  NewTable;.  if( 
241a3 70 3d 3d 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d  p==0 || NEVER(p-
241a4 3e 6e 43 6f 6c 3c 31 29 20 29 20 72 65 74 75 72  >nCol<1) ) retur
241a5 6e 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e  n;.  pCol = &p->
241a6 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 3b  aCol[p->nCol-1];
241a7 0a 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d  .  assert( pCol-
241a8 3e 7a 54 79 70 65 3d 3d 30 20 29 3b 0a 20 20 70  >zType==0 );.  p
241a9 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73 71 6c  Col->zType = sql
241aa 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
241ab 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  n(pParse->db, pT
241ac 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 61 66  ype);.  pCol->af
241ad 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33  finity = sqlite3
241ae 41 66 66 69 6e 69 74 79 54 79 70 65 28 70 43 6f  AffinityType(pCo
241af 6c 2d 3e 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a  l->zType);.}../*
241b0 0a 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73 69  .** The expressi
241b1 6f 6e 20 69 73 20 74 68 65 20 64 65 66 61 75 6c  on is the defaul
241b2 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
241b3 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64  most recently ad
241b4 64 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66  ded column.** of
241b5 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65   the table curre
241b6 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
241b7 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44  ruction..**.** D
241b8 65 66 61 75 6c 74 20 76 61 6c 75 65 20 65 78 70  efault value exp
241b9 72 65 73 73 69 6f 6e 73 20 6d 75 73 74 20 62 65  ressions must be
241ba 20 63 6f 6e 73 74 61 6e 74 2e 20 20 52 61 69 73   constant.  Rais
241bb 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 69  e an exception i
241bc 66 20 74 68 69 73 0a 2a 2a 20 69 73 20 6e 6f 74  f this.** is not
241bd 20 74 68 65 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a   the case..**.**
241be 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
241bf 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
241c0 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74  arser while in t
241c1 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20  he middle of.** 
241c2 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 45  parsing a CREATE
241c3 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
241c4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
241c5 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
241c6 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28  AddDefaultValue(
241c7 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
241c8 78 70 72 53 70 61 6e 20 2a 70 53 70 61 6e 29 7b  xprSpan *pSpan){
241c9 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43  .  Table *p;.  C
241ca 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73  olumn *pCol;.  s
241cb 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
241cc 72 73 65 2d 3e 64 62 3b 0a 20 20 70 20 3d 20 70  rse->db;.  p = p
241cd 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
241ce 3b 0a 20 20 69 66 28 20 70 21 3d 30 20 29 7b 0a  ;.  if( p!=0 ){.
241cf 20 20 20 20 70 43 6f 6c 20 3d 20 26 28 70 2d 3e      pCol = &(p->
241d0 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29  aCol[p->nCol-1])
241d1 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74  ;.    if( !sqlit
241d2 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
241d3 4f 72 46 75 6e 63 74 69 6f 6e 28 70 53 70 61 6e  OrFunction(pSpan
241d4 2d 3e 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ->pExpr) ){.    
241d5 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
241d6 67 28 70 50 61 72 73 65 2c 20 22 64 65 66 61 75  g(pParse, "defau
241d7 6c 74 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75  lt value of colu
241d8 6d 6e 20 5b 25 73 5d 20 69 73 20 6e 6f 74 20 63  mn [%s] is not c
241d9 6f 6e 73 74 61 6e 74 22 2c 0a 20 20 20 20 20 20  onstant",.      
241da 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29      pCol->zName)
241db 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
241dc 20 20 20 2f 2a 20 41 20 63 6f 70 79 20 6f 66 20     /* A copy of 
241dd 70 45 78 70 72 20 69 73 20 75 73 65 64 20 69 6e  pExpr is used in
241de 73 74 65 61 64 20 6f 66 20 74 68 65 20 6f 72 69  stead of the ori
241df 67 69 6e 61 6c 2c 20 61 73 20 70 45 78 70 72 20  ginal, as pExpr 
241e0 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 2a  contains.      *
241e1 2a 20 74 6f 6b 65 6e 73 20 74 68 61 74 20 70 6f  * tokens that po
241e2 69 6e 74 20 74 6f 20 76 6f 6c 61 74 69 6c 65 20  int to volatile 
241e3 6d 65 6d 6f 72 79 2e 20 54 68 65 20 27 73 70 61  memory. The 'spa
241e4 6e 27 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  n' of the expres
241e5 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73  sion.      ** is
241e6 20 72 65 71 75 69 72 65 64 20 62 79 20 70 72 61   required by pra
241e7 67 6d 61 20 74 61 62 6c 65 5f 69 6e 66 6f 2e 0a  gma table_info..
241e8 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
241e9 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
241ea 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74  (db, pCol->pDflt
241eb 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70  );.      pCol->p
241ec 44 66 6c 74 20 3d 20 73 71 6c 69 74 65 33 45 78  Dflt = sqlite3Ex
241ed 70 72 44 75 70 28 64 62 2c 20 70 53 70 61 6e 2d  prDup(db, pSpan-
241ee 3e 70 45 78 70 72 2c 20 45 58 50 52 44 55 50 5f  >pExpr, EXPRDUP_
241ef 52 45 44 55 43 45 29 3b 0a 20 20 20 20 20 20 73  REDUCE);.      s
241f0 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
241f1 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 29 3b 0a 20   pCol->zDflt);. 
241f2 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74       pCol->zDflt
241f3 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
241f4 44 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70  Dup(db, (char*)p
241f5 53 70 61 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20 20  Span->zStart,.  
241f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
241f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
241f8 20 20 20 28 69 6e 74 29 28 70 53 70 61 6e 2d 3e     (int)(pSpan->
241f9 7a 45 6e 64 20 2d 20 70 53 70 61 6e 2d 3e 7a 53  zEnd - pSpan->zS
241fa 74 61 72 74 29 29 3b 0a 20 20 20 20 7d 0a 20 20  tart));.    }.  
241fb 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  }.  sqlite3ExprD
241fc 65 6c 65 74 65 28 64 62 2c 20 70 53 70 61 6e 2d  elete(db, pSpan-
241fd 3e 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  >pExpr);.}../*.*
241fe 2a 20 44 65 73 69 67 6e 61 74 65 20 74 68 65 20  * Designate the 
241ff 50 52 49 4d 41 52 59 20 4b 45 59 20 66 6f 72 20  PRIMARY KEY for 
24200 74 68 65 20 74 61 62 6c 65 2e 20 20 70 4c 69 73  the table.  pLis
24201 74 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6e  t is a list of n
24202 61 6d 65 73 20 0a 2a 2a 20 6f 66 20 63 6f 6c 75  ames .** of colu
24203 6d 6e 73 20 74 68 61 74 20 66 6f 72 6d 20 74 68  mns that form th
24204 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20  e primary key.  
24205 49 66 20 70 4c 69 73 74 20 69 73 20 4e 55 4c 4c  If pList is NULL
24206 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 6f  , then the.** mo
24207 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65  st recently adde
24208 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  d column of the 
24209 74 61 62 6c 65 20 69 73 20 74 68 65 20 70 72 69  table is the pri
2420a 6d 61 72 79 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20  mary key..**.** 
2420b 41 20 74 61 62 6c 65 20 63 61 6e 20 68 61 76 65  A table can have
2420c 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 70 72 69   at most one pri
2420d 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 20 74 68  mary key.  If th
2420e 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20  e table already 
2420f 68 61 73 0a 2a 2a 20 61 20 70 72 69 6d 61 72 79  has.** a primary
24210 20 6b 65 79 20 28 61 6e 64 20 74 68 69 73 20 69   key (and this i
24211 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72 69  s the second pri
24212 6d 61 72 79 20 6b 65 79 29 20 74 68 65 6e 20 63  mary key) then c
24213 72 65 61 74 65 20 61 6e 0a 2a 2a 20 65 72 72 6f  reate an.** erro
24214 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  r..**.** If the 
24215 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20 6f  PRIMARY KEY is o
24216 6e 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  n a single colum
24217 6e 20 77 68 6f 73 65 20 64 61 74 61 74 79 70 65  n whose datatype
24218 20 69 73 20 49 4e 54 45 47 45 52 2c 0a 2a 2a 20   is INTEGER,.** 
24219 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 74 72 79  then we will try
2421a 20 74 6f 20 75 73 65 20 74 68 61 74 20 63 6f 6c   to use that col
2421b 75 6d 6e 20 61 73 20 74 68 65 20 72 6f 77 69 64  umn as the rowid
2421c 2e 20 20 53 65 74 20 74 68 65 20 54 61 62 6c 65  .  Set the Table
2421d 2e 69 50 4b 65 79 0a 2a 2a 20 66 69 65 6c 64 20  .iPKey.** field 
2421e 6f 66 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64  of the table und
2421f 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20  er construction 
24220 74 6f 20 62 65 20 74 68 65 20 69 6e 64 65 78 20  to be the index 
24221 6f 66 20 74 68 65 0a 2a 2a 20 49 4e 54 45 47 45  of the.** INTEGE
24222 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f  R PRIMARY KEY co
24223 6c 75 6d 6e 2e 20 20 54 61 62 6c 65 2e 69 50 4b  lumn.  Table.iPK
24224 65 79 20 69 73 20 73 65 74 20 74 6f 20 2d 31 20  ey is set to -1 
24225 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e  if there is.** n
24226 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  o INTEGER PRIMAR
24227 59 20 4b 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  Y KEY..**.** If 
24228 74 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 61  the key is not a
24229 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  n INTEGER PRIMAR
2422a 59 20 4b 45 59 2c 20 74 68 65 6e 20 63 72 65 61  Y KEY, then crea
2422b 74 65 20 61 20 75 6e 69 71 75 65 0a 2a 2a 20 69  te a unique.** i
2422c 6e 64 65 78 20 66 6f 72 20 74 68 65 20 6b 65 79  ndex for the key
2422d 2e 20 20 4e 6f 20 69 6e 64 65 78 20 69 73 20 63  .  No index is c
2422e 72 65 61 74 65 64 20 66 6f 72 20 49 4e 54 45 47  reated for INTEG
2422f 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 73 2e  ER PRIMARY KEYs.
24230 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
24231 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
24232 64 64 50 72 69 6d 61 72 79 4b 65 79 28 0a 20 20  ddPrimaryKey(.  
24233 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
24234 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
24235 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
24236 73 74 20 2a 70 4c 69 73 74 2c 20 20 2f 2a 20 4c  st *pList,  /* L
24237 69 73 74 20 6f 66 20 66 69 65 6c 64 20 6e 61 6d  ist of field nam
24238 65 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  es to be indexed
24239 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f   */.  int onErro
2423a 72 2c 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20  r,      /* What 
2423b 74 6f 20 64 6f 20 77 69 74 68 20 61 20 75 6e 69  to do with a uni
2423c 71 75 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74  queness conflict
2423d 20 2a 2f 0a 20 20 69 6e 74 20 61 75 74 6f 49 6e   */.  int autoIn
2423e 63 2c 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  c,      /* True 
2423f 69 66 20 74 68 65 20 41 55 54 4f 49 4e 43 52 45  if the AUTOINCRE
24240 4d 45 4e 54 20 6b 65 79 77 6f 72 64 20 69 73 20  MENT keyword is 
24241 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  present */.  int
24242 20 73 6f 72 74 4f 72 64 65 72 20 20 20 20 20 2f   sortOrder     /
24243 2a 20 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20  * SQLITE_SO_ASC 
24244 6f 72 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53  or SQLITE_SO_DES
24245 43 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20  C */.){.  Table 
24246 2a 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e  *pTab = pParse->
24247 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 63 68 61  pNewTable;.  cha
24248 72 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20  r *zType = 0;.  
24249 69 6e 74 20 69 43 6f 6c 20 3d 20 2d 31 2c 20 69  int iCol = -1, i
2424a 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  ;.  if( pTab==0 
2424b 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54  || IN_DECLARE_VT
2424c 41 42 20 29 20 67 6f 74 6f 20 70 72 69 6d 61 72  AB ) goto primar
2424d 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20 69 66  y_key_exit;.  if
2424e 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73  ( pTab->tabFlags
2424f 20 26 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79   & TF_HasPrimary
24250 4b 65 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Key ){.    sqlit
24251 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
24252 65 2c 20 0a 20 20 20 20 20 20 22 74 61 62 6c 65  e, .      "table
24253 20 5c 22 25 73 5c 22 20 68 61 73 20 6d 6f 72 65   \"%s\" has more
24254 20 74 68 61 6e 20 6f 6e 65 20 70 72 69 6d 61 72   than one primar
24255 79 20 6b 65 79 22 2c 20 70 54 61 62 2d 3e 7a 4e  y key", pTab->zN
24256 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 70  ame);.    goto p
24257 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b  rimary_key_exit;
24258 0a 20 20 7d 0a 20 20 70 54 61 62 2d 3e 74 61 62  .  }.  pTab->tab
24259 46 6c 61 67 73 20 7c 3d 20 54 46 5f 48 61 73 50  Flags |= TF_HasP
2425a 72 69 6d 61 72 79 4b 65 79 3b 0a 20 20 69 66 28  rimaryKey;.  if(
2425b 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20   pList==0 ){.   
2425c 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43   iCol = pTab->nC
2425d 6f 6c 20 2d 20 31 3b 0a 20 20 20 20 70 54 61 62  ol - 1;.    pTab
2425e 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50  ->aCol[iCol].isP
2425f 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 7d 65  rimKey = 1;.  }e
24260 6c 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  lse{.    for(i=0
24261 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72  ; i<pList->nExpr
24262 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f  ; i++){.      fo
24263 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70  r(iCol=0; iCol<p
24264 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b  Tab->nCol; iCol+
24265 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
24266 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
24267 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  List->a[i].zName
24268 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f  , pTab->aCol[iCo
24269 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  l].zName)==0 ){.
2426a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
2426b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2426c 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f   }.      if( iCo
2426d 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a  l<pTab->nCol ){.
2426e 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43          pTab->aC
2426f 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72 69 6d 4b  ol[iCol].isPrimK
24270 65 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ey = 1;.      }.
24271 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 4c      }.    if( pL
24272 69 73 74 2d 3e 6e 45 78 70 72 3e 31 20 29 20 69  ist->nExpr>1 ) i
24273 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20  Col = -1;.  }.  
24274 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69  if( iCol>=0 && i
24275 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  Col<pTab->nCol )
24276 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54  {.    zType = pT
24277 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
24278 54 79 70 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Type;.  }.  if( 
24279 7a 54 79 70 65 20 26 26 20 73 71 6c 69 74 65 33  zType && sqlite3
2427a 53 74 72 49 43 6d 70 28 7a 54 79 70 65 2c 20 22  StrICmp(zType, "
2427b 49 4e 54 45 47 45 52 22 29 3d 3d 30 0a 20 20 20  INTEGER")==0.   
2427c 20 20 20 20 20 26 26 20 73 6f 72 74 4f 72 64 65       && sortOrde
2427d 72 3d 3d 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43  r==SQLITE_SO_ASC
2427e 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 69 50   ){.    pTab->iP
2427f 4b 65 79 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20  Key = iCol;.    
24280 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 20 3d 20  pTab->keyConf = 
24281 28 75 38 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 20  (u8)onError;.   
24282 20 61 73 73 65 72 74 28 20 61 75 74 6f 49 6e 63   assert( autoInc
24283 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e 63 3d 3d  ==0 || autoInc==
24284 31 20 29 3b 0a 20 20 20 20 70 54 61 62 2d 3e 74  1 );.    pTab->t
24285 61 62 46 6c 61 67 73 20 7c 3d 20 61 75 74 6f 49  abFlags |= autoI
24286 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d  nc*TF_Autoincrem
24287 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ent;.  }else if(
24288 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23 69 66 6e   autoInc ){.#ifn
24289 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2428a 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20  AUTOINCREMENT.  
2428b 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2428c 67 28 70 50 61 72 73 65 2c 20 22 41 55 54 4f 49  g(pParse, "AUTOI
2428d 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f 6e 6c 79  NCREMENT is only
2428e 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61 6e 20 22   allowed on an "
2428f 0a 20 20 20 20 20 20 20 22 49 4e 54 45 47 45 52  .       "INTEGER
24290 20 50 52 49 4d 41 52 59 20 4b 45 59 22 29 3b 0a   PRIMARY KEY");.
24291 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a  #endif.  }else{.
24292 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20      Index *p;.  
24293 20 20 70 20 3d 20 73 71 6c 69 74 65 33 43 72 65    p = sqlite3Cre
24294 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c  ateIndex(pParse,
24295 20 30 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74 2c   0, 0, 0, pList,
24296 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c 20   onError, 0, 0, 
24297 73 6f 72 74 4f 72 64 65 72 2c 20 30 29 3b 0a 20  sortOrder, 0);. 
24298 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20     if( p ){.    
24299 20 20 70 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d    p->autoIndex =
2429a 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c   2;.    }.    pL
2429b 69 73 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 70 72  ist = 0;.  }..pr
2429c 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3a 0a  imary_key_exit:.
2429d 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2429e 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
2429f 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 65  db, pList);.  re
242a0 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  turn;.}../*.** A
242a1 64 64 20 61 20 6e 65 77 20 43 48 45 43 4b 20 63  dd a new CHECK c
242a2 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 74 68 65  onstraint to the
242a3 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79   table currently
242a4 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
242a5 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
242a6 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
242a7 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74  te3AddCheckConst
242a8 72 61 69 6e 74 28 0a 20 20 50 61 72 73 65 20 2a  raint(.  Parse *
242a9 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61  pParse,    /* Pa
242aa 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
242ab 0a 20 20 45 78 70 72 20 2a 70 43 68 65 63 6b 45  .  Expr *pCheckE
242ac 78 70 72 20 20 2f 2a 20 54 68 65 20 63 68 65 63  xpr  /* The chec
242ad 6b 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  k expression */.
242ae 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
242af 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23   = pParse->db;.#
242b0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
242b1 49 54 5f 43 48 45 43 4b 0a 20 20 54 61 62 6c 65  IT_CHECK.  Table
242b2 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73 65 2d   *pTab = pParse-
242b3 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66  >pNewTable;.  if
242b4 28 20 70 54 61 62 20 26 26 20 21 49 4e 5f 44 45  ( pTab && !IN_DE
242b5 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20  CLARE_VTAB ){.  
242b6 20 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 20 3d    pTab->pCheck =
242b7 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28   sqlite3ExprAnd(
242b8 64 62 2c 20 70 54 61 62 2d 3e 70 43 68 65 63 6b  db, pTab->pCheck
242b9 2c 20 70 43 68 65 63 6b 45 78 70 72 29 3b 0a 20  , pCheckExpr);. 
242ba 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
242bb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  {.    sqlite3Exp
242bc 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 68 65  rDelete(db, pChe
242bd 63 6b 45 78 70 72 29 3b 0a 20 20 7d 0a 7d 0a 0a  ckExpr);.  }.}..
242be 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f  /*.** Set the co
242bf 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e  llation function
242c0 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
242c1 65 6e 74 6c 79 20 70 61 72 73 65 64 20 74 61 62  ently parsed tab
242c2 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20  le column.** to 
242c3 74 68 65 20 43 6f 6c 6c 53 65 71 20 67 69 76 65  the CollSeq give
242c4 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
242c5 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
242c6 33 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 28  3AddCollateType(
242c7 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
242c8 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20  oken *pToken){. 
242c9 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74   Table *p;.  int
242ca 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c   i;.  char *zCol
242cb 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
242cc 2f 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d 65  /* Dequoted name
242cd 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65   of collation se
242ce 71 75 65 6e 63 65 20 2a 2f 0a 20 20 73 71 6c 69  quence */.  sqli
242cf 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20  te3 *db;..  if( 
242d0 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  (p = pParse->pNe
242d1 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74  wTable)==0 ) ret
242d2 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43  urn;.  i = p->nC
242d3 6f 6c 2d 31 3b 0a 20 20 64 62 20 3d 20 70 50 61  ol-1;.  db = pPa
242d4 72 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c  rse->db;.  zColl
242d5 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
242d6 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b  omToken(db, pTok
242d7 65 6e 29 3b 0a 20 20 69 66 28 20 21 7a 43 6f 6c  en);.  if( !zCol
242d8 6c 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69  l ) return;..  i
242d9 66 28 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  f( sqlite3Locate
242da 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
242db 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 49 6e  zColl) ){.    In
242dc 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 70  dex *pIdx;.    p
242dd 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 20  ->aCol[i].zColl 
242de 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 0a 20 20 20 20  = zColl;.  .    
242df 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e  /* If the column
242e0 20 69 73 20 64 65 63 6c 61 72 65 64 20 61 73 20   is declared as 
242e1 22 3c 6e 61 6d 65 3e 20 50 52 49 4d 41 52 59 20  "<name> PRIMARY 
242e2 4b 45 59 20 43 4f 4c 4c 41 54 45 20 3c 74 79 70  KEY COLLATE <typ
242e3 65 3e 22 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e  e>",.    ** then
242e4 20 61 6e 20 69 6e 64 65 78 20 6d 61 79 20 68 61   an index may ha
242e5 76 65 20 62 65 65 6e 20 63 72 65 61 74 65 64 20  ve been created 
242e6 6f 6e 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 62  on this column b
242e7 65 66 6f 72 65 20 74 68 65 0a 20 20 20 20 2a 2a  efore the.    **
242e8 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20   collation type 
242e9 77 61 73 20 61 64 64 65 64 2e 20 43 6f 72 72 65  was added. Corre
242ea 63 74 20 74 68 69 73 20 69 66 20 69 74 20 69 73  ct this if it is
242eb 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20 20 2a   the case..    *
242ec 2f 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70  /.    for(pIdx=p
242ed 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
242ee 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
242ef 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
242f0 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d   pIdx->nColumn==
242f1 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  1 );.      if( p
242f2 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d  Idx->aiColumn[0]
242f3 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==i ){.        p
242f4 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 20 3d  Idx->azColl[0] =
242f5 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c   p->aCol[i].zCol
242f6 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  l;.      }.    }
242f7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
242f8 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
242f9 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  zColl);.  }.}../
242fa 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
242fb 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 63  on returns the c
242fc 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
242fd 65 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 6e  e for database n
242fe 61 74 69 76 65 20 74 65 78 74 0a 2a 2a 20 65 6e  ative text.** en
242ff 63 6f 64 69 6e 67 20 69 64 65 6e 74 69 66 69 65  coding identifie
24300 64 20 62 79 20 74 68 65 20 73 74 72 69 6e 67 20  d by the string 
24301 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e  zName, length nN
24302 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ame..**.** If th
24303 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 6c 6c  e requested coll
24304 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69  ation sequence i
24305 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c  s not available,
24306 20 6f 72 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c   or not availabl
24307 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74 61  e.** in the data
24308 62 61 73 65 20 6e 61 74 69 76 65 20 65 6e 63 6f  base native enco
24309 64 69 6e 67 2c 20 74 68 65 20 63 6f 6c 6c 61 74  ding, the collat
2430a 69 6f 6e 20 66 61 63 74 6f 72 79 20 69 73 20 69  ion factory is i
2430b 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20 72 65 71  nvoked to.** req
2430c 75 65 73 74 20 69 74 2e 20 49 66 20 74 68 65 20  uest it. If the 
2430d 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72  collation factor
2430e 79 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6c  y does not suppl
2430f 79 20 73 75 63 68 20 61 20 73 65 71 75 65 6e 63  y such a sequenc
24310 65 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 65  e,.** and the se
24311 71 75 65 6e 63 65 20 69 73 20 61 76 61 69 6c 61  quence is availa
24312 62 6c 65 20 69 6e 20 61 6e 6f 74 68 65 72 20 74  ble in another t
24313 65 78 74 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68  ext encoding, th
24314 65 6e 20 74 68 61 74 20 69 73 0a 2a 2a 20 72 65  en that is.** re
24315 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a  turned instead..
24316 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 76 65 72 73  **.** If no vers
24317 69 6f 6e 73 20 6f 66 20 74 68 65 20 72 65 71 75  ions of the requ
24318 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 73  ested collations
24319 20 73 65 71 75 65 6e 63 65 20 61 72 65 20 61 76   sequence are av
2431a 61 69 6c 61 62 6c 65 2c 20 6f 72 0a 2a 2a 20 61  ailable, or.** a
2431b 6e 6f 74 68 65 72 20 65 72 72 6f 72 20 6f 63 63  nother error occ
2431c 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74  urs, NULL is ret
2431d 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72  urned and an err
2431e 6f 72 20 6d 65 73 73 61 67 65 20 77 72 69 74 74  or message writt
2431f 65 6e 20 69 6e 74 6f 0a 2a 2a 20 70 50 61 72 73  en into.** pPars
24320 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
24321 75 74 69 6e 65 20 69 73 20 61 20 77 72 61 70 70  utine is a wrapp
24322 65 72 20 61 72 6f 75 6e 64 20 73 71 6c 69 74 65  er around sqlite
24323 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 29 2e 20  3FindCollSeq(). 
24324 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   This routine.**
24325 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 6c   invokes the col
24326 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 69  lation factory i
24327 66 20 74 68 65 20 6e 61 6d 65 64 20 63 6f 6c 6c  f the named coll
24328 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20  ation cannot be 
24329 66 6f 75 6e 64 0a 2a 2a 20 61 6e 64 20 67 65 6e  found.** and gen
2432a 65 72 61 74 65 73 20 61 6e 20 65 72 72 6f 72 20  erates an error 
2432b 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53  message..**.** S
2432c 65 65 20 61 6c 73 6f 3a 20 73 71 6c 69 74 65 33  ee also: sqlite3
2432d 46 69 6e 64 43 6f 6c 6c 53 65 71 28 29 2c 20 73  FindCollSeq(), s
2432e 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71  qlite3GetCollSeq
2432f 28 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ().*/.SQLITE_PRI
24330 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71  VATE CollSeq *sq
24331 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53  lite3LocateCollS
24332 65 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  eq(Parse *pParse
24333 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
24334 61 6d 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  ame){.  sqlite3 
24335 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
24336 3b 0a 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43  ;.  u8 enc = ENC
24337 28 64 62 29 3b 0a 20 20 75 38 20 69 6e 69 74 62  (db);.  u8 initb
24338 75 73 79 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 62  usy = db->init.b
24339 75 73 79 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  usy;.  CollSeq *
2433a 70 43 6f 6c 6c 3b 0a 0a 20 20 70 43 6f 6c 6c 20  pColl;..  pColl 
2433b 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c  = sqlite3FindCol
2433c 6c 53 65 71 28 64 62 2c 20 65 6e 63 2c 20 7a 4e  lSeq(db, enc, zN
2433d 61 6d 65 2c 20 69 6e 69 74 62 75 73 79 29 3b 0a  ame, initbusy);.
2433e 20 20 69 66 28 20 21 69 6e 69 74 62 75 73 79 20    if( !initbusy 
2433f 26 26 20 28 21 70 43 6f 6c 6c 20 7c 7c 20 21 70  && (!pColl || !p
24340 43 6f 6c 6c 2d 3e 78 43 6d 70 29 20 29 7b 0a 20  Coll->xCmp) ){. 
24341 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
24342 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c  e3GetCollSeq(db,
24343 20 65 6e 63 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61   enc, pColl, zNa
24344 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43  me);.    if( !pC
24345 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  oll ){.      sql
24346 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
24347 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f  rse, "no such co
24348 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
24349 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  : %s", zName);. 
2434a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
2434b 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a  rn pColl;.}.../*
2434c 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
2434d 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 63 72  e that will incr
2434e 65 6d 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61  ement the schema
2434f 20 63 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54   cookie..**.** T
24350 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
24351 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65   is used to dete
24352 72 6d 69 6e 65 20 77 68 65 6e 20 74 68 65 20 73  rmine when the s
24353 63 68 65 6d 61 20 66 6f 72 20 74 68 65 0a 2a 2a  chema for the.**
24354 20 64 61 74 61 62 61 73 65 20 63 68 61 6e 67 65   database change
24355 73 2e 20 20 41 66 74 65 72 20 65 61 63 68 20 73  s.  After each s
24356 63 68 65 6d 61 20 63 68 61 6e 67 65 2c 20 74 68  chema change, th
24357 65 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 0a 2a  e cookie value.*
24358 2a 20 63 68 61 6e 67 65 73 2e 20 20 57 68 65 6e  * changes.  When
24359 20 61 20 70 72 6f 63 65 73 73 20 66 69 72 73 74   a process first
2435a 20 72 65 61 64 73 20 74 68 65 20 73 63 68 65 6d   reads the schem
2435b 61 20 69 74 20 72 65 63 6f 72 64 73 20 74 68 65  a it records the
2435c 0a 2a 2a 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65  .** cookie.  The
2435d 72 65 61 66 74 65 72 2c 20 77 68 65 6e 65 76 65  reafter, wheneve
2435e 72 20 69 74 20 67 6f 65 73 20 74 6f 20 61 63 63  r it goes to acc
2435f 65 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ess the database
24360 2c 0a 2a 2a 20 69 74 20 63 68 65 63 6b 73 20 74  ,.** it checks t
24361 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b  he cookie to mak
24362 65 20 73 75 72 65 20 74 68 65 20 73 63 68 65 6d  e sure the schem
24363 61 20 68 61 73 20 6e 6f 74 20 63 68 61 6e 67 65  a has not change
24364 64 0a 2a 2a 20 73 69 6e 63 65 20 69 74 20 77 61  d.** since it wa
24365 73 20 6c 61 73 74 20 72 65 61 64 2e 0a 2a 2a 0a  s last read..**.
24366 2a 2a 20 54 68 69 73 20 70 6c 61 6e 20 69 73 20  ** This plan is 
24367 6e 6f 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62  not completely b
24368 75 6c 6c 65 74 2d 70 72 6f 6f 66 2e 20 20 49 74  ullet-proof.  It
24369 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72   is possible for
2436a 0a 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 74  .** the schema t
2436b 6f 20 63 68 61 6e 67 65 20 6d 75 6c 74 69 70 6c  o change multipl
2436c 65 20 74 69 6d 65 73 20 61 6e 64 20 66 6f 72 20  e times and for 
2436d 74 68 65 20 63 6f 6f 6b 69 65 20 74 6f 20 62 65  the cookie to be
2436e 0a 2a 2a 20 73 65 74 20 62 61 63 6b 20 74 6f 20  .** set back to 
2436f 70 72 69 6f 72 20 76 61 6c 75 65 2e 20 20 42 75  prior value.  Bu
24370 74 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  t schema changes
24371 20 61 72 65 20 69 6e 66 72 65 71 75 65 6e 74 0a   are infrequent.
24372 2a 2a 20 61 6e 64 20 74 68 65 20 70 72 6f 62 61  ** and the proba
24373 62 69 6c 69 74 79 20 6f 66 20 68 69 74 74 69 6e  bility of hittin
24374 67 20 74 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69  g the same cooki
24375 65 20 76 61 6c 75 65 20 69 73 20 6f 6e 6c 79 0a  e value is only.
24376 2a 2a 20 31 20 63 68 61 6e 63 65 20 69 6e 20 32  ** 1 chance in 2
24377 5e 33 32 2e 20 20 53 6f 20 77 65 27 72 65 20 73  ^32.  So we're s
24378 61 66 65 20 65 6e 6f 75 67 68 2e 0a 2a 2f 0a 53  afe enough..*/.S
24379 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2437a 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65  id sqlite3Change
2437b 43 6f 6f 6b 69 65 28 50 61 72 73 65 20 2a 70 50  Cookie(Parse *pP
2437c 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a  arse, int iDb){.
2437d 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
2437e 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2437f 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 20  rse);.  sqlite3 
24380 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
24381 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  ;.  Vdbe *v = pP
24382 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73  arse->pVdbe;.  s
24383 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24384 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
24385 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
24386 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
24387 6b 69 65 2b 31 2c 20 72 31 29 3b 0a 20 20 73 71  kie+1, r1);.  sq
24388 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
24389 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c  v, OP_SetCookie,
2438a 20 69 44 62 2c 20 42 54 52 45 45 5f 53 43 48 45   iDb, BTREE_SCHE
2438b 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 72 31 29 3b  MA_VERSION, r1);
2438c 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2438d 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2438e 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d   r1);.}../*.** M
2438f 65 61 73 75 72 65 20 74 68 65 20 6e 75 6d 62 65  easure the numbe
24390 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
24391 6e 65 65 64 65 64 20 74 6f 20 6f 75 74 70 75 74  needed to output
24392 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 69 64   the given.** id
24393 65 6e 74 69 66 69 65 72 2e 20 20 54 68 65 20 6e  entifier.  The n
24394 75 6d 62 65 72 20 72 65 74 75 72 6e 65 64 20 69  umber returned i
24395 6e 63 6c 75 64 65 73 20 61 6e 79 20 71 75 6f 74  ncludes any quot
24396 65 73 20 75 73 65 64 0a 2a 2a 20 62 75 74 20 64  es used.** but d
24397 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20  oes not include 
24398 74 68 65 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61  the null termina
24399 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  tor..**.** The e
2439a 73 74 69 6d 61 74 65 20 69 73 20 63 6f 6e 73 65  stimate is conse
2439b 72 76 61 74 69 76 65 2e 20 20 49 74 20 6d 69 67  rvative.  It mig
2439c 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68 61  ht be larger tha
2439d 74 20 77 68 61 74 20 69 73 0a 2a 2a 20 72 65 61  t what is.** rea
2439e 6c 6c 79 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73  lly needed..*/.s
2439f 74 61 74 69 63 20 69 6e 74 20 69 64 65 6e 74 4c  tatic int identL
243a0 65 6e 67 74 68 28 63 6f 6e 73 74 20 63 68 61 72  ength(const char
243a1 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20   *z){.  int n;. 
243a2 20 66 6f 72 28 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b   for(n=0; *z; n+
243a3 2b 2c 20 7a 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, z++){.    if(
243a4 20 2a 7a 3d 3d 27 22 27 20 29 7b 20 6e 2b 2b 3b   *z=='"' ){ n++;
243a5 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
243a6 6e 20 2b 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n + 2;.}../*.** 
243a7 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
243a8 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ter is a pointer
243a9 20 74 6f 20 61 6e 20 6f 75 74 70 75 74 20 62 75   to an output bu
243aa 66 66 65 72 2e 20 54 68 65 20 73 65 63 6f 6e 64  ffer. The second
243ab 20 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69   .** parameter i
243ac 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
243ad 6e 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 63  n integer that c
243ae 6f 6e 74 61 69 6e 73 20 74 68 65 20 6f 66 66 73  ontains the offs
243af 65 74 20 61 74 0a 2a 2a 20 77 68 69 63 68 20 74  et at.** which t
243b0 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 74 68 65  o write into the
243b1 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e 20   output buffer. 
243b2 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f  This function co
243b3 70 69 65 73 20 74 68 65 0a 2a 2a 20 6e 75 6c 2d  pies the.** nul-
243b4 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
243b5 67 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  g pointed to by 
243b6 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d 65  the third parame
243b7 74 65 72 2c 20 7a 53 69 67 6e 65 64 49 64 65 6e  ter, zSignedIden
243b8 74 2c 0a 2a 2a 20 74 6f 20 74 68 65 20 73 70 65  t,.** to the spe
243b9 63 69 66 69 65 64 20 6f 66 66 73 65 74 20 69 6e  cified offset in
243ba 20 74 68 65 20 62 75 66 66 65 72 20 61 6e 64 20   the buffer and 
243bb 75 70 64 61 74 65 73 20 2a 70 49 64 78 20 74 6f  updates *pIdx to
243bc 20 72 65 66 65 72 0a 2a 2a 20 74 6f 20 74 68 65   refer.** to the
243bd 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65   first byte afte
243be 72 20 74 68 65 20 6c 61 73 74 20 62 79 74 65 20  r the last byte 
243bf 77 72 69 74 74 65 6e 20 62 65 66 6f 72 65 20 72  written before r
243c0 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a  eturning..** .**
243c1 20 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 7a   If the string z
243c2 53 69 67 6e 65 64 49 64 65 6e 74 20 63 6f 6e 73  SignedIdent cons
243c3 69 73 74 73 20 65 6e 74 69 72 65 6c 79 20 6f 66  ists entirely of
243c4 20 61 6c 70 68 61 2d 6e 75 6d 65 72 69 63 0a 2a   alpha-numeric.*
243c5 2a 20 63 68 61 72 61 63 74 65 72 73 2c 20 64 6f  * characters, do
243c6 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74  es not begin wit
243c7 68 20 61 20 64 69 67 69 74 20 61 6e 64 20 69 73  h a digit and is
243c8 20 6e 6f 74 20 61 6e 20 53 51 4c 20 6b 65 79 77   not an SQL keyw
243c9 6f 72 64 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20  ord,.** then it 
243ca 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68 65  is copied to the
243cb 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 65   output buffer e
243cc 78 61 63 74 6c 79 20 61 73 20 69 74 20 69 73 2e  xactly as it is.
243cd 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20 69   Otherwise,.** i
243ce 74 20 69 73 20 71 75 6f 74 65 64 20 75 73 69 6e  t is quoted usin
243cf 67 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2e  g double-quotes.
243d0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
243d1 69 64 65 6e 74 50 75 74 28 63 68 61 72 20 2a 7a  identPut(char *z
243d2 2c 20 69 6e 74 20 2a 70 49 64 78 2c 20 63 68 61  , int *pIdx, cha
243d3 72 20 2a 7a 53 69 67 6e 65 64 49 64 65 6e 74 29  r *zSignedIdent)
243d4 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
243d5 72 20 2a 7a 49 64 65 6e 74 20 3d 20 28 75 6e 73  r *zIdent = (uns
243d6 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 53 69 67  igned char*)zSig
243d7 6e 65 64 49 64 65 6e 74 3b 0a 20 20 69 6e 74 20  nedIdent;.  int 
243d8 69 2c 20 6a 2c 20 6e 65 65 64 51 75 6f 74 65 3b  i, j, needQuote;
243d9 0a 20 20 69 20 3d 20 2a 70 49 64 78 3b 0a 0a 20  .  i = *pIdx;.. 
243da 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e 74   for(j=0; zIdent
243db 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69  [j]; j++){.    i
243dc 66 28 20 21 73 71 6c 69 74 65 33 49 73 61 6c 6e  f( !sqlite3Isaln
243dd 75 6d 28 7a 49 64 65 6e 74 5b 6a 5d 29 20 26 26  um(zIdent[j]) &&
243de 20 7a 49 64 65 6e 74 5b 6a 5d 21 3d 27 5f 27 20   zIdent[j]!='_' 
243df 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 6e  ) break;.  }.  n
243e0 65 65 64 51 75 6f 74 65 20 3d 20 73 71 6c 69 74  eedQuote = sqlit
243e1 65 33 49 73 64 69 67 69 74 28 7a 49 64 65 6e 74  e3Isdigit(zIdent
243e2 5b 30 5d 29 20 7c 7c 20 73 71 6c 69 74 65 33 4b  [0]) || sqlite3K
243e3 65 79 77 6f 72 64 43 6f 64 65 28 7a 49 64 65 6e  eywordCode(zIden
243e4 74 2c 20 6a 29 21 3d 54 4b 5f 49 44 3b 0a 20 20  t, j)!=TK_ID;.  
243e5 69 66 28 20 21 6e 65 65 64 51 75 6f 74 65 20 29  if( !needQuote )
243e6 7b 0a 20 20 20 20 6e 65 65 64 51 75 6f 74 65 20  {.    needQuote 
243e7 3d 20 7a 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 7d  = zIdent[j];.  }
243e8 0a 0a 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74  ..  if( needQuot
243e9 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27  e ) z[i++] = '"'
243ea 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64  ;.  for(j=0; zId
243eb 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20  ent[j]; j++){.  
243ec 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a 49 64 65 6e    z[i++] = zIden
243ed 74 5b 6a 5d 3b 0a 20 20 20 20 69 66 28 20 7a 49  t[j];.    if( zI
243ee 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27 20 29 20 7a  dent[j]=='"' ) z
243ef 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7d  [i++] = '"';.  }
243f0 0a 20 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65  .  if( needQuote
243f1 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b   ) z[i++] = '"';
243f2 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a  .  z[i] = 0;.  *
243f3 70 49 64 78 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a  pIdx = i;.}../*.
243f4 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 20 43 52  ** Generate a CR
243f5 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
243f6 6d 65 6e 74 20 61 70 70 72 6f 70 72 69 61 74 65  ment appropriate
243f7 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 0a 2a   for the given.*
243f8 2a 20 74 61 62 6c 65 2e 20 20 4d 65 6d 6f 72 79  * table.  Memory
243f9 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 74 65 78   to hold the tex
243fa 74 20 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65  t of the stateme
243fb 6e 74 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a  nt is obtained.*
243fc 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c  * from sqliteMal
243fd 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73 74 20 62  loc() and must b
243fe 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63  e freed by the c
243ff 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e  alling function.
24400 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
24401 2a 63 72 65 61 74 65 54 61 62 6c 65 53 74 6d 74  *createTableStmt
24402 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61  (sqlite3 *db, Ta
24403 62 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69  ble *p){.  int i
24404 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a  , k, n;.  char *
24405 7a 53 74 6d 74 3b 0a 20 20 63 68 61 72 20 2a 7a  zStmt;.  char *z
24406 53 65 70 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45  Sep, *zSep2, *zE
24407 6e 64 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43  nd;.  Column *pC
24408 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 66  ol;.  n = 0;.  f
24409 6f 72 28 70 43 6f 6c 20 3d 20 70 2d 3e 61 43 6f  or(pCol = p->aCo
2440a 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f  l, i=0; i<p->nCo
2440b 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b  l; i++, pCol++){
2440c 0a 20 20 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c  .    n += identL
2440d 65 6e 67 74 68 28 70 43 6f 6c 2d 3e 7a 4e 61 6d  ength(pCol->zNam
2440e 65 29 20 2b 20 35 3b 0a 20 20 7d 0a 20 20 6e 20  e) + 5;.  }.  n 
2440f 2b 3d 20 69 64 65 6e 74 4c 65 6e 67 74 68 28 70  += identLength(p
24410 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ->zName);.  if( 
24411 6e 3c 35 30 20 29 7b 20 0a 20 20 20 20 7a 53 65  n<50 ){ .    zSe
24412 70 20 3d 20 22 22 3b 0a 20 20 20 20 7a 53 65 70  p = "";.    zSep
24413 32 20 3d 20 22 2c 22 3b 0a 20 20 20 20 7a 45 6e  2 = ",";.    zEn
24414 64 20 3d 20 22 29 22 3b 0a 20 20 7d 65 6c 73 65  d = ")";.  }else
24415 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 5c 6e  {.    zSep = "\n
24416 20 20 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d    ";.    zSep2 =
24417 20 22 2c 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 45   ",\n  ";.    zE
24418 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a 20 20 7d 0a  nd = "\n)";.  }.
24419 20 20 6e 20 2b 3d 20 33 35 20 2b 20 36 2a 70 2d    n += 35 + 6*p-
2441a 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74 6d 74 20 3d  >nCol;.  zStmt =
2441b 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20   sqlite3Malloc( 
2441c 6e 20 29 3b 0a 20 20 69 66 28 20 7a 53 74 6d 74  n );.  if( zStmt
2441d 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  ==0 ){.    db->m
2441e 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2441f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
24420 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70   }.  sqlite3_snp
24421 72 69 6e 74 66 28 6e 2c 20 7a 53 74 6d 74 2c 20  rintf(n, zStmt, 
24422 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 22 29  "CREATE TABLE ")
24423 3b 0a 20 20 6b 20 3d 20 73 71 6c 69 74 65 33 53  ;.  k = sqlite3S
24424 74 72 6c 65 6e 33 30 28 7a 53 74 6d 74 29 3b 0a  trlen30(zStmt);.
24425 20 20 69 64 65 6e 74 50 75 74 28 7a 53 74 6d 74    identPut(zStmt
24426 2c 20 26 6b 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b  , &k, p->zName);
24427 0a 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20  .  zStmt[k++] = 
24428 27 28 27 3b 0a 20 20 66 6f 72 28 70 43 6f 6c 3d  '(';.  for(pCol=
24429 70 2d 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c  p->aCol, i=0; i<
2442a 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43  p->nCol; i++, pC
2442b 6f 6c 2b 2b 29 7b 0a 20 20 20 20 73 74 61 74 69  ol++){.    stati
2442c 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63  c const char * c
2442d 6f 6e 73 74 20 61 7a 54 79 70 65 5b 5d 20 3d 20  onst azType[] = 
2442e 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  {.        /* SQL
2442f 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20 20  ITE_AFF_TEXT    
24430 2a 2f 20 22 20 54 45 58 54 22 2c 0a 20 20 20 20  */ " TEXT",.    
24431 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46      /* SQLITE_AF
24432 46 5f 4e 4f 4e 45 20 20 20 20 2a 2f 20 22 22 2c  F_NONE    */ "",
24433 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49  .        /* SQLI
24434 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 2a  TE_AFF_NUMERIC *
24435 2f 20 22 20 4e 55 4d 22 2c 0a 20 20 20 20 20 20  / " NUM",.      
24436 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f    /* SQLITE_AFF_
24437 49 4e 54 45 47 45 52 20 2a 2f 20 22 20 49 4e 54  INTEGER */ " INT
24438 22 2c 0a 20 20 20 20 20 20 20 20 2f 2a 20 53 51  ",.        /* SQ
24439 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20  LITE_AFF_REAL   
2443a 20 2a 2f 20 22 20 52 45 41 4c 22 0a 20 20 20 20   */ " REAL".    
2443b 7d 3b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a  };.    int len;.
2443c 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2443d 7a 54 79 70 65 3b 0a 0a 20 20 20 20 73 71 6c 69  zType;..    sqli
2443e 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b  te3_snprintf(n-k
2443f 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20 7a 53 65  , &zStmt[k], zSe
24440 70 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c  p);.    k += sql
24441 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 53  ite3Strlen30(&zS
24442 74 6d 74 5b 6b 5d 29 3b 0a 20 20 20 20 7a 53 65  tmt[k]);.    zSe
24443 70 20 3d 20 7a 53 65 70 32 3b 0a 20 20 20 20 69  p = zSep2;.    i
24444 64 65 6e 74 50 75 74 28 7a 53 74 6d 74 2c 20 26  dentPut(zStmt, &
24445 6b 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b  k, pCol->zName);
24446 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f  .    assert( pCo
24447 6c 2d 3e 61 66 66 69 6e 69 74 79 2d 53 51 4c 49  l->affinity-SQLI
24448 54 45 5f 41 46 46 5f 54 45 58 54 20 3e 3d 20 30  TE_AFF_TEXT >= 0
24449 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2444a 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2d 53  pCol->affinity-S
2444b 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 3c  QLITE_AFF_TEXT <
2444c 20 73 69 7a 65 6f 66 28 61 7a 54 79 70 65 29 2f   sizeof(azType)/
2444d 73 69 7a 65 6f 66 28 61 7a 54 79 70 65 5b 30 5d  sizeof(azType[0]
2444e 29 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  ) );.    testcas
2444f 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  e( pCol->affinit
24450 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  y==SQLITE_AFF_TE
24451 58 54 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  XT );.    testca
24452 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  se( pCol->affini
24453 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ty==SQLITE_AFF_N
24454 4f 4e 45 20 29 3b 0a 20 20 20 20 74 65 73 74 63  ONE );.    testc
24455 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e  ase( pCol->affin
24456 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
24457 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20 20 74  NUMERIC );.    t
24458 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61  estcase( pCol->a
24459 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
2445a 41 46 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20  AFF_INTEGER );. 
2445b 20 20 20 74 65 73 74 63 61 73 65 28 20 70 43 6f     testcase( pCo
2445c 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  l->affinity==SQL
2445d 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 29 3b 0a  ITE_AFF_REAL );.
2445e 20 20 20 20 0a 20 20 20 20 7a 54 79 70 65 20 3d      .    zType =
2445f 20 61 7a 54 79 70 65 5b 70 43 6f 6c 2d 3e 61 66   azType[pCol->af
24460 66 69 6e 69 74 79 20 2d 20 53 51 4c 49 54 45 5f  finity - SQLITE_
24461 41 46 46 5f 54 45 58 54 5d 3b 0a 20 20 20 20 6c  AFF_TEXT];.    l
24462 65 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  en = sqlite3Strl
24463 65 6e 33 30 28 7a 54 79 70 65 29 3b 0a 20 20 20  en30(zType);.   
24464 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 61   assert( pCol->a
24465 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f  ffinity==SQLITE_
24466 41 46 46 5f 4e 4f 4e 45 20 0a 20 20 20 20 20 20  AFF_NONE .      
24467 20 20 20 20 20 20 7c 7c 20 70 43 6f 6c 2d 3e 61        || pCol->a
24468 66 66 69 6e 69 74 79 3d 3d 73 71 6c 69 74 65 33  ffinity==sqlite3
24469 41 66 66 69 6e 69 74 79 54 79 70 65 28 7a 54 79  AffinityType(zTy
2446a 70 65 29 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70  pe) );.    memcp
2446b 79 28 26 7a 53 74 6d 74 5b 6b 5d 2c 20 7a 54 79  y(&zStmt[k], zTy
2446c 70 65 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 6b 20  pe, len);.    k 
2446d 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 61 73 73 65  += len;.    asse
2446e 72 74 28 20 6b 3c 3d 6e 20 29 3b 0a 20 20 7d 0a  rt( k<=n );.  }.
2446f 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
24470 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b  tf(n-k, &zStmt[k
24471 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29 3b 0a  ], "%s", zEnd);.
24472 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74 3b 0a    return zStmt;.
24473 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
24474 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
24475 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20 66 69  to report the fi
24476 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74 65 72  nal ")" that ter
24477 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43 52 45  minates.** a CRE
24478 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
24479 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ent..**.** The t
2447a 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74  able structure t
2447b 68 61 74 20 6f 74 68 65 72 20 61 63 74 69 6f 6e  hat other action
2447c 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20 62   routines have b
2447d 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a 2a 2a 20  een building.** 
2447e 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  is added to the 
2447f 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74 61  internal hash ta
24480 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e 67 20 6e  bles, assuming n
24481 6f 20 65 72 72 6f 72 73 20 68 61 76 65 0a 2a 2a  o errors have.**
24482 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a 2a   occurred..**.**
24483 20 41 6e 20 65 6e 74 72 79 20 66 6f 72 20 74 68   An entry for th
24484 65 20 74 61 62 6c 65 20 69 73 20 6d 61 64 65 20  e table is made 
24485 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 74 61  in the master ta
24486 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 75 6e 6c  ble on disk, unl
24487 65 73 73 0a 2a 2a 20 74 68 69 73 20 69 73 20 61  ess.** this is a
24488 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
24489 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73   or db->init.bus
2448a 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64 62 2d 3e  y==1.  When db->
2448b 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a 2a 2a 20  init.busy==1.** 
2448c 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20  it means we are 
2448d 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c 69  reading the sqli
2448e 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
2448f 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 0a  because we just.
24490 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20  ** connected to 
24491 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20  the database or 
24492 62 65 63 61 75 73 65 20 74 68 65 20 73 71 6c 69  because the sqli
24493 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
24494 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74 6c 79 20  has.** recently 
24495 63 68 61 6e 67 65 64 2c 20 73 6f 20 74 68 65 20  changed, so the 
24496 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73 20 74  entry for this t
24497 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69  able already exi
24498 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71  sts in.** the sq
24499 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
2449a 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61  e.  We do not wa
2449b 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69 74 20  nt to create it 
2449c 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  again..**.** If 
2449d 74 68 65 20 70 53 65 6c 65 63 74 20 61 72 67 75  the pSelect argu
2449e 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ment is not NULL
2449f 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
244a0 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
244a1 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72  was called to cr
244a2 65 61 74 65 20 61 20 74 61 62 6c 65 20 67 65 6e  eate a table gen
244a3 65 72 61 74 65 64 20 66 72 6f 6d 20 61 20 0a 2a  erated from a .*
244a4 2a 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20  * "CREATE TABLE 
244a5 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 20 2e 2e  ... AS SELECT ..
244a6 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54  ." statement.  T
244a7 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
244a8 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 74 61  of.** the new ta
244a9 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63 68 20 74  ble will match t
244aa 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
244ab 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a   the SELECT..*/.
244ac 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
244ad 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 54 61  oid sqlite3EndTa
244ae 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ble(.  Parse *pP
244af 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
244b0 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
244b1 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43 6f 6e  */.  Token *pCon
244b2 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  s,           /* 
244b3 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e 20 61 66  The ',' token af
244b4 74 65 72 20 74 68 65 20 6c 61 73 74 20 63 6f 6c  ter the last col
244b5 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a 20 20 54  umn defn. */.  T
244b6 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20 20  oken *pEnd,     
244b7 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69         /* The fi
244b8 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e 20 69 6e  nal ')' token in
244b9 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c   the CREATE TABL
244ba 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  E */.  Select *p
244bb 53 65 6c 65 63 74 20 20 20 20 20 20 20 20 20 2f  Select         /
244bc 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d 20 61 20  * Select from a 
244bd 22 43 52 45 41 54 45 20 2e 2e 2e 20 41 53 20 53  "CREATE ... AS S
244be 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20 20 54  ELECT" */.){.  T
244bf 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71 6c 69 74  able *p;.  sqlit
244c0 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
244c1 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  >db;.  int iDb;.
244c2 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d 30 20  .  if( (pEnd==0 
244c3 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29 20 7c  && pSelect==0) |
244c4 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
244c5 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
244c6 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 50 61 72  ;.  }.  p = pPar
244c7 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
244c8 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
244c9 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21  rn;..  assert( !
244ca 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c  db->init.busy ||
244cb 20 21 70 53 65 6c 65 63 74 20 29 3b 0a 0a 20 20   !pSelect );..  
244cc 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
244cd 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
244ce 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23 69 66  ->pSchema);..#if
244cf 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
244d0 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52 65 73 6f  _CHECK.  /* Reso
244d1 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c  lve names in all
244d2 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e   CHECK constrain
244d3 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20  t expressions.. 
244d4 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 43 68   */.  if( p->pCh
244d5 65 63 6b 20 29 7b 0a 20 20 20 20 53 72 63 4c 69  eck ){.    SrcLi
244d6 73 74 20 73 53 72 63 3b 20 20 20 20 20 20 20 20  st sSrc;        
244d7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 61             /* Fa
244d8 6b 65 20 53 72 63 4c 69 73 74 20 66 6f 72 20 70  ke SrcList for p
244d9 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
244da 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74   */.    NameCont
244db 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20 20 20  ext sNC;        
244dc 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
244dd 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 50 61 72  context for pPar
244de 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f  se->pNewTable */
244df 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e  ..    memset(&sN
244e0 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
244e1 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  ));.    memset(&
244e2 73 53 72 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28  sSrc, 0, sizeof(
244e3 73 53 72 63 29 29 3b 0a 20 20 20 20 73 53 72 63  sSrc));.    sSrc
244e4 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 20 73  .nSrc = 1;.    s
244e5 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d 65 20 3d  Src.a[0].zName =
244e6 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 73   p->zName;.    s
244e7 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62 20 3d 20  Src.a[0].pTab = 
244e8 70 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b 30 5d  p;.    sSrc.a[0]
244e9 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20  .iCursor = -1;. 
244ea 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20     sNC.pParse = 
244eb 70 50 61 72 73 65 3b 0a 20 20 20 20 73 4e 43 2e  pParse;.    sNC.
244ec 70 53 72 63 4c 69 73 74 20 3d 20 26 73 53 72 63  pSrcList = &sSrc
244ed 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43 68 65 63  ;.    sNC.isChec
244ee 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 73  k = 1;.    if( s
244ef 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
244f0 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e  rNames(&sNC, p->
244f1 70 43 68 65 63 6b 29 20 29 7b 0a 20 20 20 20 20  pCheck) ){.     
244f2 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
244f3 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65   }.#endif /* !de
244f4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
244f5 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20 20 2f  T_CHECK) */..  /
244f6 2a 20 49 66 20 74 68 65 20 64 62 2d 3e 69 6e 69  * If the db->ini
244f7 74 2e 62 75 73 79 20 69 73 20 31 20 69 74 20 6d  t.busy is 1 it m
244f8 65 61 6e 73 20 77 65 20 61 72 65 20 72 65 61 64  eans we are read
244f9 69 6e 67 20 74 68 65 20 53 51 4c 20 6f 66 66 20  ing the SQL off 
244fa 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c 69 74 65  the.  ** "sqlite
244fb 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 73 71 6c  _master" or "sql
244fc 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22  ite_temp_master"
244fd 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20 64 69   table on the di
244fe 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64 6f 20 6e  sk..  ** So do n
244ff 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68 65 20  ot write to the 
24500 64 69 73 6b 20 61 67 61 69 6e 2e 20 20 45 78 74  disk again.  Ext
24501 72 61 63 74 20 74 68 65 20 72 6f 6f 74 20 70 61  ract the root pa
24502 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 66  ge number.  ** f
24503 6f 72 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f  or the table fro
24504 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 6e  m the db->init.n
24505 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e 20 20 28  ewTnum field.  (
24506 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 0a  The page number.
24507 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61 76 65    ** should have
24508 20 62 65 65 6e 20 70 75 74 20 74 68 65 72 65 20   been put there 
24509 62 79 20 74 68 65 20 73 71 6c 69 74 65 4f 70 65  by the sqliteOpe
2450a 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29 0a 20 20  nCb routine.).  
2450b 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69  */.  if( db->ini
2450c 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 70 2d  t.busy ){.    p-
2450d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74  >tnum = db->init
2450e 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d 0a 0a 20  .newTnum;.  }.. 
2450f 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e 69 74 69   /* If not initi
24510 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e 20 63 72  alizing, then cr
24511 65 61 74 65 20 61 20 72 65 63 6f 72 64 20 66 6f  eate a record fo
24512 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 0a  r the new table.
24513 20 20 2a 2a 20 69 6e 20 74 68 65 20 53 51 4c 49    ** in the SQLI
24514 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20  TE_MASTER table 
24515 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
24516 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  **.  ** If th
24517 69 73 20 69 73 20 61 20 54 45 4d 50 4f 52 41 52  is is a TEMPORAR
24518 59 20 74 61 62 6c 65 2c 20 77 72 69 74 65 20 74  Y table, write t
24519 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68  he entry into th
2451a 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a  e auxiliary.  **
2451b 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66   file instead of
2451c 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 64   into the main d
2451d 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20  atabase file..  
2451e 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  */.  if( !db->in
2451f 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 69  it.busy ){.    i
24520 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62 65 20 2a  nt n;.    Vdbe *
24521 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79  v;.    char *zTy
24522 70 65 3b 20 20 20 20 2f 2a 20 22 76 69 65 77 22  pe;    /* "view"
24523 20 6f 72 20 22 74 61 62 6c 65 22 20 2a 2f 0a 20   or "table" */. 
24524 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65 32 3b     char *zType2;
24525 20 20 20 2f 2a 20 22 56 49 45 57 22 20 6f 72 20     /* "VIEW" or 
24526 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20 20 20 63  "TABLE" */.    c
24527 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20 20 20 2f  har *zStmt;    /
24528 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 43 52  * Text of the CR
24529 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20 43 52  EATE TABLE or CR
2452a 45 41 54 45 20 56 49 45 57 20 73 74 61 74 65 6d  EATE VIEW statem
2452b 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76 20 3d 20  ent */..    v = 
2452c 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2452d 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20  Parse);.    if( 
2452e 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65  NEVER(v==0) ) re
2452f 74 75 72 6e 3b 0a 0a 20 20 20 20 73 71 6c 69 74  turn;..    sqlit
24530 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
24531 4f 50 5f 43 6c 6f 73 65 2c 20 30 29 3b 0a 0a 20  OP_Close, 0);.. 
24532 20 20 20 2f 2a 20 0a 20 20 20 20 2a 2a 20 49 6e     /* .    ** In
24533 69 74 69 61 6c 69 7a 65 20 7a 54 79 70 65 20 66  itialize zType f
24534 6f 72 20 74 68 65 20 6e 65 77 20 76 69 65 77 20  or the new view 
24535 6f 72 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f  or table..    */
24536 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 53 65 6c  .    if( p->pSel
24537 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ect==0 ){.      
24538 2f 2a 20 41 20 72 65 67 75 6c 61 72 20 74 61 62  /* A regular tab
24539 6c 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70  le */.      zTyp
2453a 65 20 3d 20 22 74 61 62 6c 65 22 3b 0a 20 20 20  e = "table";.   
2453b 20 20 20 7a 54 79 70 65 32 20 3d 20 22 54 41 42     zType2 = "TAB
2453c 4c 45 22 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  LE";.#ifndef SQL
2453d 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20  ITE_OMIT_VIEW.  
2453e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
2453f 2a 20 41 20 76 69 65 77 20 2a 2f 0a 20 20 20 20  * A view */.    
24540 20 20 7a 54 79 70 65 20 3d 20 22 76 69 65 77 22    zType = "view"
24541 3b 0a 20 20 20 20 20 20 7a 54 79 70 65 32 20 3d  ;.      zType2 =
24542 20 22 56 49 45 57 22 3b 0a 23 65 6e 64 69 66 0a   "VIEW";.#endif.
24543 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66      }..    /* If
24544 20 74 68 69 73 20 69 73 20 61 20 43 52 45 41 54   this is a CREAT
24545 45 20 54 41 42 4c 45 20 78 78 20 41 53 20 53 45  E TABLE xx AS SE
24546 4c 45 43 54 20 2e 2e 2e 2c 20 65 78 65 63 75 74  LECT ..., execut
24547 65 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20  e the SELECT.   
24548 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 6f   ** statement to
24549 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e 65   populate the ne
2454a 77 20 74 61 62 6c 65 2e 20 54 68 65 20 72 6f 6f  w table. The roo
2454b 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  t-page number fo
2454c 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 65 77  r the.    ** new
2454d 20 74 61 62 6c 65 20 69 73 20 69 6e 20 72 65 67   table is in reg
2454e 69 73 74 65 72 20 70 50 61 72 73 65 2d 3e 72 65  ister pParse->re
2454f 67 52 6f 6f 74 2e 0a 20 20 20 20 2a 2a 0a 20 20  gRoot..    **.  
24550 20 20 2a 2a 20 4f 6e 63 65 20 74 68 65 20 53 45    ** Once the SE
24551 4c 45 43 54 20 68 61 73 20 62 65 65 6e 20 63 6f  LECT has been co
24552 64 65 64 20 62 79 20 73 71 6c 69 74 65 33 53 65  ded by sqlite3Se
24553 6c 65 63 74 28 29 2c 20 69 74 20 69 73 20 69 6e  lect(), it is in
24554 20 61 0a 20 20 20 20 2a 2a 20 73 75 69 74 61 62   a.    ** suitab
24555 6c 65 20 73 74 61 74 65 20 74 6f 20 71 75 65 72  le state to quer
24556 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e  y for the column
24557 20 6e 61 6d 65 73 20 61 6e 64 20 74 79 70 65 73   names and types
24558 20 74 6f 20 62 65 20 75 73 65 64 0a 20 20 20 20   to be used.    
24559 2a 2a 20 62 79 20 74 68 65 20 6e 65 77 20 74 61  ** by the new ta
2455a 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ble..    **.    
2455b 2a 2a 20 41 20 73 68 61 72 65 64 2d 63 61 63 68  ** A shared-cach
2455c 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73 20  e write-lock is 
2455d 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
2455e 77 72 69 74 65 20 74 6f 20 74 68 65 20 6e 65 77  write to the new
2455f 20 74 61 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 61   table,.    ** a
24560 73 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 20  s a schema-lock 
24561 6d 75 73 74 20 68 61 76 65 20 61 6c 72 65 61 64  must have alread
24562 79 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20  y been obtained 
24563 74 6f 20 63 72 65 61 74 65 20 69 74 2e 20 53 69  to create it. Si
24564 6e 63 65 0a 20 20 20 20 2a 2a 20 61 20 73 63 68  nce.    ** a sch
24565 65 6d 61 2d 6c 6f 63 6b 20 65 78 63 6c 75 64 65  ema-lock exclude
24566 73 20 61 6c 6c 20 6f 74 68 65 72 20 64 61 74 61  s all other data
24567 62 61 73 65 20 75 73 65 72 73 2c 20 74 68 65 20  base users, the 
24568 77 72 69 74 65 2d 6c 6f 63 6b 20 77 6f 75 6c 64  write-lock would
24569 0a 20 20 20 20 2a 2a 20 62 65 20 72 65 64 75 6e  .    ** be redun
2456a 64 61 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  dant..    */.   
2456b 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a   if( pSelect ){.
2456c 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74        SelectDest
2456d 20 64 65 73 74 3b 0a 20 20 20 20 20 20 54 61 62   dest;.      Tab
2456e 6c 65 20 2a 70 53 65 6c 54 61 62 3b 0a 0a 20 20  le *pSelTab;..  
2456f 20 20 20 20 61 73 73 65 72 74 28 70 50 61 72 73      assert(pPars
24570 65 2d 3e 6e 54 61 62 3d 3d 31 29 3b 0a 20 20 20  e->nTab==1);.   
24571 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24572 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  dOp3(v, OP_OpenW
24573 72 69 74 65 2c 20 31 2c 20 70 50 61 72 73 65 2d  rite, 1, pParse-
24574 3e 72 65 67 52 6f 6f 74 2c 20 69 44 62 29 3b 0a  >regRoot, iDb);.
24575 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24576 65 43 68 61 6e 67 65 50 35 28 76 2c 20 31 29 3b  eChangeP5(v, 1);
24577 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  .      pParse->n
24578 54 61 62 20 3d 20 32 3b 0a 20 20 20 20 20 20 73  Tab = 2;.      s
24579 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
2457a 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f  Init(&dest, SRT_
2457b 54 61 62 6c 65 2c 20 31 29 3b 0a 20 20 20 20 20  Table, 1);.     
2457c 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
2457d 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20  Parse, pSelect, 
2457e 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 73 71  &dest);.      sq
2457f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
24580 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 31 29 3b  v, OP_Close, 1);
24581 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73  .      if( pPars
24582 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20  e->nErr==0 ){.  
24583 20 20 20 20 20 20 70 53 65 6c 54 61 62 20 3d 20        pSelTab = 
24584 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65 74  sqlite3ResultSet
24585 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  OfSelect(pParse,
24586 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20   pSelect);.     
24587 20 20 20 69 66 28 20 70 53 65 6c 54 61 62 3d 3d     if( pSelTab==
24588 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
24589 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
2458a 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  Col==0 );.      
2458b 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c    p->nCol = pSel
2458c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20  Tab->nCol;.     
2458d 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 70 53 65     p->aCol = pSe
2458e 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20  lTab->aCol;.    
2458f 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f      pSelTab->nCo
24590 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  l = 0;.        p
24591 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30  SelTab->aCol = 0
24592 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
24593 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 53 65  3DeleteTable(pSe
24594 6c 54 61 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20  lTab);.      }. 
24595 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d     }..    /* Com
24596 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65 74  pute the complet
24597 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52  e text of the CR
24598 45 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a  EATE statement *
24599 2f 0a 20 20 20 20 69 66 28 20 70 53 65 6c 65 63  /.    if( pSelec
2459a 74 20 29 7b 0a 20 20 20 20 20 20 7a 53 74 6d 74  t ){.      zStmt
2459b 20 3d 20 63 72 65 61 74 65 54 61 62 6c 65 53 74   = createTableSt
2459c 6d 74 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d  mt(db, p);.    }
2459d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 20 3d 20  else{.      n = 
2459e 28 69 6e 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20  (int)(pEnd->z - 
2459f 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
245a0 65 6e 2e 7a 29 20 2b 20 31 3b 0a 20 20 20 20 20  en.z) + 1;.     
245a1 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33   zStmt = sqlite3
245a2 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20  MPrintf(db, .   
245a3 20 20 20 20 20 20 20 22 43 52 45 41 54 45 20 25         "CREATE %
245a4 73 20 25 2e 2a 73 22 2c 20 7a 54 79 70 65 32 2c  s %.*s", zType2,
245a5 20 6e 2c 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d   n, pParse->sNam
245a6 65 54 6f 6b 65 6e 2e 7a 0a 20 20 20 20 20 20 29  eToken.z.      )
245a7 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
245a8 41 20 73 6c 6f 74 20 66 6f 72 20 74 68 65 20 72  A slot for the r
245a9 65 63 6f 72 64 20 68 61 73 20 61 6c 72 65 61 64  ecord has alread
245aa 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  y been allocated
245ab 20 69 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20   in the .    ** 
245ac 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61  SQLITE_MASTER ta
245ad 62 6c 65 2e 20 20 57 65 20 6a 75 73 74 20 6e 65  ble.  We just ne
245ae 65 64 20 74 6f 20 75 70 64 61 74 65 20 74 68 61  ed to update tha
245af 74 20 73 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0a  t slot with all.
245b0 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72      ** the infor
245b1 6d 61 74 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c  mation we've col
245b2 6c 65 63 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  lected..    */. 
245b3 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
245b4 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
245b5 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e 25      "UPDATE %Q.%
245b6 73 20 22 0a 20 20 20 20 20 20 20 20 20 22 53 45  s ".         "SE
245b7 54 20 74 79 70 65 3d 27 25 73 27 2c 20 6e 61 6d  T type='%s', nam
245b8 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 6d 65 3d 25  e=%Q, tbl_name=%
245b9 51 2c 20 72 6f 6f 74 70 61 67 65 3d 23 25 64 2c  Q, rootpage=#%d,
245ba 20 73 71 6c 3d 25 51 20 22 0a 20 20 20 20 20 20   sql=%Q ".      
245bb 20 22 57 48 45 52 45 20 72 6f 77 69 64 3d 23 25   "WHERE rowid=#%
245bc 64 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 44  d",.      db->aD
245bd 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43  b[iDb].zName, SC
245be 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c  HEMA_TABLE(iDb),
245bf 0a 20 20 20 20 20 20 7a 54 79 70 65 2c 0a 20 20  .      zType,.  
245c0 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20      p->zName,.  
245c1 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20      p->zName,.  
245c2 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67 52      pParse->regR
245c3 6f 6f 74 2c 0a 20 20 20 20 20 20 7a 53 74 6d 74  oot,.      zStmt
245c4 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ,.      pParse->
245c5 72 65 67 52 6f 77 69 64 0a 20 20 20 20 29 3b 0a  regRowid.    );.
245c6 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
245c7 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 20 20  e(db, zStmt);.  
245c8 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43    sqlite3ChangeC
245c9 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44  ookie(pParse, iD
245ca 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  b);..#ifndef SQL
245cb 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
245cc 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 43 68  REMENT.    /* Ch
245cd 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65  eck to see if we
245ce 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65 20   need to create 
245cf 61 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  an sqlite_sequen
245d0 63 65 20 74 61 62 6c 65 20 66 6f 72 0a 20 20 20  ce table for.   
245d1 20 2a 2a 20 6b 65 65 70 69 6e 67 20 74 72 61 63   ** keeping trac
245d2 6b 20 6f 66 20 61 75 74 6f 69 6e 63 72 65 6d 65  k of autoincreme
245d3 6e 74 20 6b 65 79 73 2e 0a 20 20 20 20 2a 2f 0a  nt keys..    */.
245d4 20 20 20 20 69 66 28 20 70 2d 3e 74 61 62 46 6c      if( p->tabFl
245d5 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63  ags & TF_Autoinc
245d6 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  rement ){.      
245d7 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61  Db *pDb = &db->a
245d8 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 20 20 69  Db[iDb];.      i
245d9 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  f( pDb->pSchema-
245da 3e 70 53 65 71 54 61 62 3d 3d 30 20 29 7b 0a 20  >pSeqTab==0 ){. 
245db 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65         sqlite3Ne
245dc 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
245dd 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43 52 45  ,.          "CRE
245de 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73 71 6c  ATE TABLE %Q.sql
245df 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e 61 6d  ite_sequence(nam
245e0 65 2c 73 65 71 29 22 2c 0a 20 20 20 20 20 20 20  e,seq)",.       
245e1 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 0a 20 20     pDb->zName.  
245e2 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d        );.      }
245e3 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
245e4 20 20 20 2f 2a 20 52 65 70 61 72 73 65 20 65 76     /* Reparse ev
245e5 65 72 79 74 68 69 6e 67 20 74 6f 20 75 70 64 61  erything to upda
245e6 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20  te our internal 
245e7 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
245e8 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
245e9 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
245ea 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c  arseSchema, iDb,
245eb 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 73   0, 0,.        s
245ec 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
245ed 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 27 25 71 27  , "tbl_name='%q'
245ee 22 2c 70 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f  ",p->zName), P4_
245ef 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 0a 0a  DYNAMIC);.  }...
245f0 20 20 2f 2a 20 41 64 64 20 74 68 65 20 74 61 62    /* Add the tab
245f1 6c 65 20 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d  le to the in-mem
245f2 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69  ory representati
245f3 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  on of the databa
245f4 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  se..  */.  if( d
245f5 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a  b->init.busy ){.
245f6 20 20 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b      Table *pOld;
245f7 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63  .    Schema *pSc
245f8 68 65 6d 61 20 3d 20 70 2d 3e 70 53 63 68 65 6d  hema = p->pSchem
245f9 61 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20 73 71  a;.    pOld = sq
245fa 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
245fb 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  &pSchema->tblHas
245fc 68 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  h, p->zName,.   
245fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
245fe 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
245ff 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e 7a 4e 61  3Strlen30(p->zNa
24600 6d 65 29 2c 70 29 3b 0a 20 20 20 20 69 66 28 20  me),p);.    if( 
24601 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 61 73  pOld ){.      as
24602 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64 20 29 3b  sert( p==pOld );
24603 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74    /* Malloc must
24604 20 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73   have failed ins
24605 69 64 65 20 48 61 73 68 49 6e 73 65 72 74 28 29  ide HashInsert()
24606 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   */.      db->ma
24607 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
24608 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
24609 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e    }.    pParse->
2460a 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20  pNewTable = 0;. 
2460b 20 20 20 64 62 2d 3e 6e 54 61 62 6c 65 2b 2b 3b     db->nTable++;
2460c 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  .    db->flags |
2460d 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43  = SQLITE_InternC
2460e 68 61 6e 67 65 73 3b 0a 0a 23 69 66 6e 64 65 66  hanges;..#ifndef
2460f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54   SQLITE_OMIT_ALT
24610 45 52 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20  ERTABLE.    if( 
24611 21 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  !p->pSelect ){. 
24612 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
24613 2a 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20  *zName = (const 
24614 63 68 61 72 20 2a 29 70 50 61 72 73 65 2d 3e 73  char *)pParse->s
24615 4e 61 6d 65 54 6f 6b 65 6e 2e 7a 3b 0a 20 20 20  NameToken.z;.   
24616 20 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20     int nName;.  
24617 20 20 20 20 61 73 73 65 72 74 28 20 21 70 53 65      assert( !pSe
24618 6c 65 63 74 20 26 26 20 70 43 6f 6e 73 20 26 26  lect && pCons &&
24619 20 70 45 6e 64 20 29 3b 0a 20 20 20 20 20 20 69   pEnd );.      i
2461a 66 28 20 70 43 6f 6e 73 2d 3e 7a 3d 3d 30 20 29  f( pCons->z==0 )
2461b 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6e 73 20  {.        pCons 
2461c 3d 20 70 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  = pEnd;.      }.
2461d 20 20 20 20 20 20 6e 4e 61 6d 65 20 3d 20 28 69        nName = (i
2461e 6e 74 29 28 28 63 6f 6e 73 74 20 63 68 61 72 20  nt)((const char 
2461f 2a 29 70 43 6f 6e 73 2d 3e 7a 20 2d 20 7a 4e 61  *)pCons->z - zNa
24620 6d 65 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64  me);.      p->ad
24621 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 31 33 20  dColOffset = 13 
24622 2b 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61  + sqlite3Utf8Cha
24623 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  rLen(zName, nNam
24624 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  e);.    }.#endif
24625 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  .  }.}..#ifndef 
24626 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
24627 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65  ./*.** The parse
24628 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
24629 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  tine in order to
2462a 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 56 49   create a new VI
2462b 45 57 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  EW.*/.SQLITE_PRI
2462c 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2462d 33 43 72 65 61 74 65 56 69 65 77 28 0a 20 20 50  3CreateView(.  P
2462e 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2462f 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67    /* The parsing
24630 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f   context */.  To
24631 6b 65 6e 20 2a 70 42 65 67 69 6e 2c 20 20 20 20  ken *pBegin,    
24632 20 2f 2a 20 54 68 65 20 43 52 45 41 54 45 20 74   /* The CREATE t
24633 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73  oken that begins
24634 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 2a   the statement *
24635 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
24636 31 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f  1,     /* The to
24637 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73 20 74  ken that holds t
24638 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 76  he name of the v
24639 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  iew */.  Token *
2463a 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f 2a 20 54  pName2,     /* T
2463b 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f  he token that ho
2463c 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  lds the name of 
2463d 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 53 65  the view */.  Se
2463e 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20  lect *pSelect,  
2463f 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61   /* A SELECT sta
24640 74 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c  tement that will
24641 20 62 65 63 6f 6d 65 20 74 68 65 20 6e 65 77 20   become the new 
24642 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73  view */.  int is
24643 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f 2a 20  Temp,        /* 
24644 54 52 55 45 20 66 6f 72 20 61 20 54 45 4d 50 4f  TRUE for a TEMPO
24645 52 41 52 59 20 76 69 65 77 20 2a 2f 0a 20 20 69  RARY view */.  i
24646 6e 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20  nt noErr        
24647 20 20 2f 2a 20 53 75 70 70 72 65 73 73 20 65 72    /* Suppress er
24648 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 66 20  ror messages if 
24649 56 49 45 57 20 61 6c 72 65 61 64 79 20 65 78 69  VIEW already exi
2464a 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  sts */.){.  Tabl
2464b 65 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20  e *p;.  int n;. 
2464c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a   const char *z;.
2464d 20 20 54 6f 6b 65 6e 20 73 45 6e 64 3b 0a 20 20    Token sEnd;.  
2464e 44 62 46 69 78 65 72 20 73 46 69 78 3b 0a 20 20  DbFixer sFix;.  
2464f 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b 0a 20 20  Token *pName;.  
24650 69 6e 74 20 69 44 62 3b 0a 20 20 73 71 6c 69 74  int iDb;.  sqlit
24651 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
24652 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72  >db;..  if( pPar
24653 73 65 2d 3e 6e 56 61 72 3e 30 20 29 7b 0a 20 20  se->nVar>0 ){.  
24654 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
24655 67 28 70 50 61 72 73 65 2c 20 22 70 61 72 61 6d  g(pParse, "param
24656 65 74 65 72 73 20 61 72 65 20 6e 6f 74 20 61 6c  eters are not al
24657 6c 6f 77 65 64 20 69 6e 20 76 69 65 77 73 22 29  lowed in views")
24658 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
24659 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53  ectDelete(db, pS
2465a 65 6c 65 63 74 29 3b 0a 20 20 20 20 72 65 74 75  elect);.    retu
2465b 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  rn;.  }.  sqlite
2465c 33 53 74 61 72 74 54 61 62 6c 65 28 70 50 61 72  3StartTable(pPar
2465d 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
2465e 65 32 2c 20 69 73 54 65 6d 70 2c 20 31 2c 20 30  e2, isTemp, 1, 0
2465f 2c 20 6e 6f 45 72 72 29 3b 0a 20 20 70 20 3d 20  , noErr);.  p = 
24660 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
24661 65 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b  e;.  if( p==0 ){
24662 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65  .    sqlite3Sele
24663 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65  ctDelete(db, pSe
24664 6c 65 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72  lect);.    retur
24665 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
24666 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30   pParse->nErr==0
24667 20 29 3b 20 2f 2a 20 49 66 20 73 71 6c 69 74 65   ); /* If sqlite
24668 33 53 74 61 72 74 54 61 62 6c 65 20 72 65 74 75  3StartTable retu
24669 72 6e 20 6e 6f 6e 2d 4e 55 4c 4c 20 74 68 65 6e  rn non-NULL then
2466a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2466b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
2466c 20 74 68 65 72 65 20 63 6f 75 6c 64 20 6e 6f 74   there could not
2466d 20 68 61 76 65 20 62 65 65 6e 20 61 6e 20 65 72   have been an er
2466e 72 6f 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ror */.  sqlite3
2466f 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72  TwoPartName(pPar
24670 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
24671 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69  e2, &pName);.  i
24672 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
24673 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d  maToIndex(db, p-
24674 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28  >pSchema);.  if(
24675 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28   sqlite3FixInit(
24676 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69  &sFix, pParse, i
24677 44 62 2c 20 22 76 69 65 77 22 2c 20 70 4e 61 6d  Db, "view", pNam
24678 65 29 0a 20 20 20 20 26 26 20 73 71 6c 69 74 65  e).    && sqlite
24679 33 46 69 78 53 65 6c 65 63 74 28 26 73 46 69 78  3FixSelect(&sFix
2467a 2c 20 70 53 65 6c 65 63 74 29 0a 20 20 29 7b 0a  , pSelect).  ){.
2467b 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2467c 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c  tDelete(db, pSel
2467d 65 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ect);.    return
2467e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ;.  }..  /* Make
2467f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65   a copy of the e
24680 6e 74 69 72 65 20 53 45 4c 45 43 54 20 73 74 61  ntire SELECT sta
24681 74 65 6d 65 6e 74 20 74 68 61 74 20 64 65 66 69  tement that defi
24682 6e 65 73 20 74 68 65 20 76 69 65 77 2e 0a 20 20  nes the view..  
24683 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20 66 6f 72  ** This will for
24684 63 65 20 61 6c 6c 20 74 68 65 20 45 78 70 72 2e  ce all the Expr.
24685 74 6f 6b 65 6e 2e 7a 20 76 61 6c 75 65 73 20 74  token.z values t
24686 6f 20 62 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79  o be dynamically
24687 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  .  ** allocated 
24688 72 61 74 68 65 72 20 74 68 61 6e 20 70 6f 69 6e  rather than poin
24689 74 20 74 6f 20 74 68 65 20 69 6e 70 75 74 20 73  t to the input s
2468a 74 72 69 6e 67 20 2d 20 77 68 69 63 68 20 6d 65  tring - which me
2468b 61 6e 73 20 74 68 61 74 0a 20 20 2a 2a 20 74 68  ans that.  ** th
2468c 65 79 20 77 69 6c 6c 20 70 65 72 73 69 73 74 20  ey will persist 
2468d 61 66 74 65 72 20 74 68 65 20 63 75 72 72 65 6e  after the curren
2468e 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29  t sqlite3_exec()
2468f 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 20   call returns.. 
24690 20 2a 2f 0a 20 20 70 2d 3e 70 53 65 6c 65 63 74   */.  p->pSelect
24691 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
24692 44 75 70 28 64 62 2c 20 70 53 65 6c 65 63 74 2c  Dup(db, pSelect,
24693 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
24694 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
24695 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c  tDelete(db, pSel
24696 65 63 74 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  ect);.  if( db->
24697 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
24698 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
24699 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
2469a 62 75 73 79 20 29 7b 0a 20 20 20 20 73 71 6c 69  busy ){.    sqli
2469b 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  te3ViewGetColumn
2469c 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 29  Names(pParse, p)
2469d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61  ;.  }..  /* Loca
2469e 74 65 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  te the end of th
2469f 65 20 43 52 45 41 54 45 20 56 49 45 57 20 73 74  e CREATE VIEW st
246a0 61 74 65 6d 65 6e 74 2e 20 20 4d 61 6b 65 20 73  atement.  Make s
246a1 45 6e 64 20 70 6f 69 6e 74 20 74 6f 0a 20 20 2a  End point to.  *
246a2 2a 20 74 68 65 20 65 6e 64 2e 0a 20 20 2a 2f 0a  * the end..  */.
246a3 20 20 73 45 6e 64 20 3d 20 70 50 61 72 73 65 2d    sEnd = pParse-
246a4 3e 73 4c 61 73 74 54 6f 6b 65 6e 3b 0a 20 20 69  >sLastToken;.  i
246a5 66 28 20 41 4c 57 41 59 53 28 73 45 6e 64 2e 7a  f( ALWAYS(sEnd.z
246a6 5b 30 5d 21 3d 30 29 20 26 26 20 73 45 6e 64 2e  [0]!=0) && sEnd.
246a7 7a 5b 30 5d 21 3d 27 3b 27 20 29 7b 0a 20 20 20  z[0]!=';' ){.   
246a8 20 73 45 6e 64 2e 7a 20 2b 3d 20 73 45 6e 64 2e   sEnd.z += sEnd.
246a9 6e 3b 0a 20 20 7d 0a 20 20 73 45 6e 64 2e 6e 20  n;.  }.  sEnd.n 
246aa 3d 20 30 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29  = 0;.  n = (int)
246ab 28 73 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69 6e  (sEnd.z - pBegin
246ac 2d 3e 7a 29 3b 0a 20 20 7a 20 3d 20 70 42 65 67  ->z);.  z = pBeg
246ad 69 6e 2d 3e 7a 3b 0a 20 20 77 68 69 6c 65 28 20  in->z;.  while( 
246ae 41 4c 57 41 59 53 28 6e 3e 30 29 20 26 26 20 73  ALWAYS(n>0) && s
246af 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a 5b  qlite3Isspace(z[
246b0 6e 2d 31 5d 29 20 29 7b 20 6e 2d 2d 3b 20 7d 0a  n-1]) ){ n--; }.
246b1 20 20 73 45 6e 64 2e 7a 20 3d 20 26 7a 5b 6e 2d    sEnd.z = &z[n-
246b2 31 5d 3b 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 31  1];.  sEnd.n = 1
246b3 3b 0a 0a 20 20 2f 2a 20 55 73 65 20 73 71 6c 69  ;..  /* Use sqli
246b4 74 65 33 45 6e 64 54 61 62 6c 65 28 29 20 74 6f  te3EndTable() to
246b5 20 61 64 64 20 74 68 65 20 76 69 65 77 20 74 6f   add the view to
246b6 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54   the SQLITE_MAST
246b7 45 52 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71  ER table */.  sq
246b8 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50  lite3EndTable(pP
246b9 61 72 73 65 2c 20 30 2c 20 26 73 45 6e 64 2c 20  arse, 0, &sEnd, 
246ba 30 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  0);.  return;.}.
246bb 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
246bc 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 23  _OMIT_VIEW */..#
246bd 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
246be 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c  TE_OMIT_VIEW) ||
246bf 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
246c0 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
246c1 4c 45 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61  LE)./*.** The Ta
246c2 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 70 54  ble structure pT
246c3 61 62 6c 65 20 69 73 20 72 65 61 6c 6c 79 20 61  able is really a
246c4 20 56 49 45 57 2e 20 20 46 69 6c 6c 20 69 6e 20   VIEW.  Fill in 
246c5 74 68 65 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a 20  the names of.** 
246c6 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74  the columns of t
246c7 68 65 20 76 69 65 77 20 69 6e 20 74 68 65 20 70  he view in the p
246c8 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 2e  Table structure.
246c9 20 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d    Return the num
246ca 62 65 72 0a 2a 2a 20 6f 66 20 65 72 72 6f 72 73  ber.** of errors
246cb 2e 20 20 49 66 20 61 6e 20 65 72 72 6f 72 20 69  .  If an error i
246cc 73 20 73 65 65 6e 20 6c 65 61 76 65 20 61 6e 20  s seen leave an 
246cd 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
246ce 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
246cf 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
246d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
246d1 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
246d2 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  s(Parse *pParse,
246d3 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b   Table *pTable){
246d4 0a 20 20 54 61 62 6c 65 20 2a 70 53 65 6c 54 61  .  Table *pSelTa
246d5 62 3b 20 20 20 2f 2a 20 41 20 66 61 6b 65 20 74  b;   /* A fake t
246d6 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20  able from which 
246d7 77 65 20 67 65 74 20 74 68 65 20 72 65 73 75 6c  we get the resul
246d8 74 20 73 65 74 20 2a 2f 0a 20 20 53 65 6c 65 63  t set */.  Selec
246d9 74 20 2a 70 53 65 6c 3b 20 20 20 20 20 2f 2a 20  t *pSel;     /* 
246da 43 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45  Copy of the SELE
246db 43 54 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  CT that implemen
246dc 74 73 20 74 68 65 20 76 69 65 77 20 2a 2f 0a 20  ts the view */. 
246dd 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b 20 20   int nErr = 0;  
246de 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
246df 65 72 72 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72  errors encounter
246e0 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20  ed */.  int n;  
246e1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d            /* Tem
246e2 70 6f 72 61 72 69 6c 79 20 68 6f 6c 64 73 20 74  porarily holds t
246e3 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 75 72  he number of cur
246e4 73 6f 72 73 20 61 73 73 69 67 6e 65 64 20 2a 2f  sors assigned */
246e5 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
246e6 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 2f 2a   pParse->db;  /*
246e7 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
246e8 74 69 6f 6e 20 66 6f 72 20 6d 61 6c 6c 6f 63 20  tion for malloc 
246e9 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20  errors */.  int 
246ea 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69  (*xAuth)(void*,i
246eb 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  nt,const char*,c
246ec 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
246ed 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
246ee 72 2a 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  r*);..  assert( 
246ef 70 54 61 62 6c 65 20 29 3b 0a 0a 23 69 66 6e 64  pTable );..#ifnd
246f0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
246f1 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66  IRTUALTABLE.  if
246f2 28 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c  ( sqlite3VtabCal
246f3 6c 43 6f 6e 6e 65 63 74 28 70 50 61 72 73 65 2c  lConnect(pParse,
246f4 20 70 54 61 62 6c 65 29 20 29 7b 0a 20 20 20 20   pTable) ){.    
246f5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
246f6 52 4f 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49  ROR;.  }.  if( I
246f7 73 56 69 72 74 75 61 6c 28 70 54 61 62 6c 65 29  sVirtual(pTable)
246f8 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e   ) return 0;.#en
246f9 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
246fa 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20  ITE_OMIT_VIEW.  
246fb 2f 2a 20 41 20 70 6f 73 69 74 69 76 65 20 6e 43  /* A positive nC
246fc 6f 6c 20 6d 65 61 6e 73 20 74 68 65 20 63 6f 6c  ol means the col
246fd 75 6d 6e 73 20 6e 61 6d 65 73 20 66 6f 72 20 74  umns names for t
246fe 68 69 73 20 76 69 65 77 20 61 72 65 0a 20 20 2a  his view are.  *
246ff 2a 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 6e 2e  * already known.
24700 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  .  */.  if( pTab
24701 6c 65 2d 3e 6e 43 6f 6c 3e 30 20 29 20 72 65 74  le->nCol>0 ) ret
24702 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 41 20 6e  urn 0;..  /* A n
24703 65 67 61 74 69 76 65 20 6e 43 6f 6c 20 69 73 20  egative nCol is 
24704 61 20 73 70 65 63 69 61 6c 20 6d 61 72 6b 65 72  a special marker
24705 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20 77 65   meaning that we
24706 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 0a 20   are currently. 
24707 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20 63 6f   ** trying to co
24708 6d 70 75 74 65 20 74 68 65 20 63 6f 6c 75 6d 6e  mpute the column
24709 20 6e 61 6d 65 73 2e 20 20 49 66 20 77 65 20 65   names.  If we e
2470a 6e 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e  nter this routin
2470b 65 20 77 69 74 68 0a 20 20 2a 2a 20 61 20 6e 65  e with.  ** a ne
2470c 67 61 74 69 76 65 20 6e 43 6f 6c 2c 20 69 74 20  gative nCol, it 
2470d 6d 65 61 6e 73 20 74 77 6f 20 6f 72 20 6d 6f 72  means two or mor
2470e 65 20 76 69 65 77 73 20 66 6f 72 6d 20 61 20 6c  e views form a l
2470f 6f 6f 70 2c 20 6c 69 6b 65 20 74 68 69 73 3a 0a  oop, like this:.
24710 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 43 52    **.  **     CR
24711 45 41 54 45 20 56 49 45 57 20 6f 6e 65 20 41 53  EATE VIEW one AS
24712 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
24713 77 6f 3b 0a 20 20 2a 2a 20 20 20 20 20 43 52 45  wo;.  **     CRE
24714 41 54 45 20 56 49 45 57 20 74 77 6f 20 41 53 20  ATE VIEW two AS 
24715 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 6f 6e  SELECT * FROM on
24716 65 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 63 74  e;.  **.  ** Act
24717 75 61 6c 6c 79 2c 20 74 68 65 20 65 72 72 6f 72  ually, the error
24718 20 61 62 6f 76 65 20 69 73 20 6e 6f 77 20 63 61   above is now ca
24719 75 67 68 74 20 70 72 69 6f 72 20 74 6f 20 72 65  ught prior to re
2471a 61 63 68 69 6e 67 20 74 68 69 73 20 70 6f 69 6e  aching this poin
2471b 74 2e 0a 20 20 2a 2a 20 42 75 74 20 74 68 65 20  t..  ** But the 
2471c 66 6f 6c 6c 6f 77 69 6e 67 20 74 65 73 74 20 69  following test i
2471d 73 20 73 74 69 6c 6c 20 69 6d 70 6f 72 74 61 6e  s still importan
2471e 74 20 61 73 20 69 74 20 64 6f 65 73 20 63 6f 6d  t as it does com
2471f 65 20 75 70 0a 20 20 2a 2a 20 69 6e 20 74 68 65  e up.  ** in the
24720 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 2a 2a   following:.  **
24721 20 0a 20 20 2a 2a 20 20 20 20 20 43 52 45 41 54   .  **     CREAT
24722 45 20 54 41 42 4c 45 20 6d 61 69 6e 2e 65 78 31  E TABLE main.ex1
24723 28 61 29 3b 0a 20 20 2a 2a 20 20 20 20 20 43 52  (a);.  **     CR
24724 45 41 54 45 20 54 45 4d 50 20 56 49 45 57 20 65  EATE TEMP VIEW e
24725 78 31 20 41 53 20 53 45 4c 45 43 54 20 61 20 46  x1 AS SELECT a F
24726 52 4f 4d 20 65 78 31 3b 0a 20 20 2a 2a 20 20 20  ROM ex1;.  **   
24727 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
24728 74 65 6d 70 2e 65 78 31 3b 0a 20 20 2a 2f 0a 20  temp.ex1;.  */. 
24729 20 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f   if( pTable->nCo
2472a 6c 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  l<0 ){.    sqlit
2472b 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2472c 65 2c 20 22 76 69 65 77 20 25 73 20 69 73 20 63  e, "view %s is c
2472d 69 72 63 75 6c 61 72 6c 79 20 64 65 66 69 6e 65  ircularly define
2472e 64 22 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d  d", pTable->zNam
2472f 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e);.    return 1
24730 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
24731 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 3d 30 20  pTable->nCol>=0 
24732 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 67  );..  /* If we g
24733 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20  et this far, it 
24734 6d 65 61 6e 73 20 77 65 20 6e 65 65 64 20 74 6f  means we need to
24735 20 63 6f 6d 70 75 74 65 20 74 68 65 20 74 61 62   compute the tab
24736 6c 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2a 20 4e  le names..  ** N
24737 6f 74 65 20 74 68 61 74 20 74 68 65 20 63 61 6c  ote that the cal
24738 6c 20 74 6f 20 73 71 6c 69 74 65 33 52 65 73 75  l to sqlite3Resu
24739 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 29 20  ltSetOfSelect() 
2473a 77 69 6c 6c 20 65 78 70 61 6e 64 20 61 6e 79 0a  will expand any.
2473b 20 20 2a 2a 20 22 2a 22 20 65 6c 65 6d 65 6e 74    ** "*" element
2473c 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 73  s in the results
2473d 20 73 65 74 20 6f 66 20 74 68 65 20 76 69 65 77   set of the view
2473e 20 61 6e 64 20 77 69 6c 6c 20 61 73 73 69 67 6e   and will assign
2473f 20 63 75 72 73 6f 72 73 0a 20 20 2a 2a 20 74 6f   cursors.  ** to
24740 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66   the elements of
24741 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
24742 2e 20 20 42 75 74 20 77 65 20 64 6f 20 6e 6f 74  .  But we do not
24743 20 77 61 6e 74 20 74 68 65 73 65 20 63 68 61 6e   want these chan
24744 67 65 73 0a 20 20 2a 2a 20 74 6f 20 62 65 20 70  ges.  ** to be p
24745 65 72 6d 61 6e 65 6e 74 2e 20 20 53 6f 20 74 68  ermanent.  So th
24746 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e 20 69 73  e computation is
24747 20 64 6f 6e 65 20 6f 6e 20 61 20 63 6f 70 79 20   done on a copy 
24748 6f 66 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20  of the SELECT.  
24749 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  ** statement tha
2474a 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 76 69  t defines the vi
2474b 65 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ew..  */.  asser
2474c 74 28 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65  t( pTable->pSele
2474d 63 74 20 29 3b 0a 20 20 70 53 65 6c 20 3d 20 73  ct );.  pSel = s
2474e 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
2474f 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c  db, pTable->pSel
24750 65 63 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ect, 0);.  if( p
24751 53 65 6c 20 29 7b 0a 20 20 20 20 75 38 20 65 6e  Sel ){.    u8 en
24752 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 20 3d 20  ableLookaside = 
24753 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
24754 6e 61 62 6c 65 64 3b 0a 20 20 20 20 6e 20 3d 20  nabled;.    n = 
24755 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20  pParse->nTab;.  
24756 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
24757 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50  AssignCursors(pP
24758 61 72 73 65 2c 20 70 53 65 6c 2d 3e 70 53 72 63  arse, pSel->pSrc
24759 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e  );.    pTable->n
2475a 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20 20 64 62  Col = -1;.    db
2475b 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61  ->lookaside.bEna
2475c 62 6c 65 64 20 3d 20 30 3b 0a 23 69 66 6e 64 65  bled = 0;.#ifnde
2475d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
2475e 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
2475f 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 41 75 74  xAuth = db->xAut
24760 68 3b 0a 20 20 20 20 64 62 2d 3e 78 41 75 74 68  h;.    db->xAuth
24761 20 3d 20 30 3b 0a 20 20 20 20 70 53 65 6c 54 61   = 0;.    pSelTa
24762 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c  b = sqlite3Resul
24763 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61  tSetOfSelect(pPa
24764 72 73 65 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20  rse, pSel);.    
24765 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74  db->xAuth = xAut
24766 68 3b 0a 23 65 6c 73 65 0a 20 20 20 20 70 53 65  h;.#else.    pSe
24767 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65  lTab = sqlite3Re
24768 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28  sultSetOfSelect(
24769 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a 23  pParse, pSel);.#
2476a 65 6e 64 69 66 0a 20 20 20 20 64 62 2d 3e 6c 6f  endif.    db->lo
2476b 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
2476c 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69   = enableLookasi
2476d 64 65 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  de;.    pParse->
2476e 6e 54 61 62 20 3d 20 6e 3b 0a 20 20 20 20 69 66  nTab = n;.    if
2476f 28 20 70 53 65 6c 54 61 62 20 29 7b 0a 20 20 20  ( pSelTab ){.   
24770 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c     assert( pTabl
24771 65 2d 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20  e->aCol==0 );.  
24772 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c      pTable->nCol
24773 20 3d 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c   = pSelTab->nCol
24774 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e  ;.      pTable->
24775 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e  aCol = pSelTab->
24776 61 43 6f 6c 3b 0a 20 20 20 20 20 20 70 53 65 6c  aCol;.      pSel
24777 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20  Tab->nCol = 0;. 
24778 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 61 43       pSelTab->aC
24779 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  ol = 0;.      sq
2477a 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
2477b 28 70 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 20  (pSelTab);.     
2477c 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61   pTable->pSchema
2477d 2d 3e 66 6c 61 67 73 20 7c 3d 20 44 42 5f 55 6e  ->flags |= DB_Un
2477e 72 65 73 65 74 56 69 65 77 73 3b 0a 20 20 20 20  resetViews;.    
2477f 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61  }else{.      pTa
24780 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20  ble->nCol = 0;. 
24781 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20       nErr++;.   
24782 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65   }.    sqlite3Se
24783 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
24784 53 65 6c 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  Sel);.  } else {
24785 0a 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 7d  .    nErr++;.  }
24786 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
24787 45 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 20  E_OMIT_VIEW */. 
24788 20 72 65 74 75 72 6e 20 6e 45 72 72 3b 20 20 0a   return nErr;  .
24789 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
2478a 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2478b 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e  _VIEW) || !defin
2478c 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2478d 49 52 54 55 41 4c 54 41 42 4c 45 29 20 2a 2f 0a  IRTUALTABLE) */.
2478e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2478f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a 0a 2a 2a 20  OMIT_VIEW./*.** 
24790 43 6c 65 61 72 20 74 68 65 20 63 6f 6c 75 6d 6e  Clear the column
24791 20 6e 61 6d 65 73 20 66 72 6f 6d 20 65 76 65 72   names from ever
24792 79 20 56 49 45 57 20 69 6e 20 64 61 74 61 62 61  y VIEW in databa
24793 73 65 20 69 64 78 2e 0a 2a 2f 0a 73 74 61 74 69  se idx..*/.stati
24794 63 20 76 6f 69 64 20 73 71 6c 69 74 65 56 69 65  c void sqliteVie
24795 77 52 65 73 65 74 41 6c 6c 28 73 71 6c 69 74 65  wResetAll(sqlite
24796 33 20 2a 64 62 2c 20 69 6e 74 20 69 64 78 29 7b  3 *db, int idx){
24797 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a  .  HashElem *i;.
24798 20 20 69 66 28 20 21 44 62 48 61 73 50 72 6f 70    if( !DbHasProp
24799 65 72 74 79 28 64 62 2c 20 69 64 78 2c 20 44 42  erty(db, idx, DB
2479a 5f 55 6e 72 65 73 65 74 56 69 65 77 73 29 20 29  _UnresetViews) )
2479b 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69   return;.  for(i
2479c 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
2479d 28 26 64 62 2d 3e 61 44 62 5b 69 64 78 5d 2e 70  (&db->aDb[idx].p
2479e 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29  Schema->tblHash)
2479f 3b 20 69 3b 69 3d 73 71 6c 69 74 65 48 61 73 68  ; i;i=sqliteHash
247a0 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 54 61  Next(i)){.    Ta
247a1 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69  ble *pTab = sqli
247a2 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20  teHashData(i);. 
247a3 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65     if( pTab->pSe
247a4 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 73 71  lect ){.      sq
247a5 6c 69 74 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e  liteResetColumnN
247a6 61 6d 65 73 28 70 54 61 62 29 3b 0a 20 20 20 20  ames(pTab);.    
247a7 7d 0a 20 20 7d 0a 20 20 44 62 43 6c 65 61 72 50  }.  }.  DbClearP
247a8 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78 2c  roperty(db, idx,
247a9 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73   DB_UnresetViews
247aa 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66  );.}.#else.# def
247ab 69 6e 65 20 73 71 6c 69 74 65 56 69 65 77 52 65  ine sqliteViewRe
247ac 73 65 74 41 6c 6c 28 41 2c 42 29 0a 23 65 6e 64  setAll(A,B).#end
247ad 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
247ae 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  T_VIEW */../*.**
247af 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
247b0 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
247b1 56 44 42 45 20 74 6f 20 61 64 6a 75 73 74 20 74  VDBE to adjust t
247b2 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65  he internal sche
247b3 6d 61 0a 2a 2a 20 75 73 65 64 20 62 79 20 53 51  ma.** used by SQ
247b4 4c 69 74 65 20 77 68 65 6e 20 74 68 65 20 62 74  Lite when the bt
247b5 72 65 65 20 6c 61 79 65 72 20 6d 6f 76 65 73 20  ree layer moves 
247b6 61 20 74 61 62 6c 65 20 72 6f 6f 74 20 70 61 67  a table root pag
247b7 65 2e 20 54 68 65 0a 2a 2a 20 72 6f 6f 74 2d 70  e. The.** root-p
247b8 61 67 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6f  age of a table o
247b9 72 20 69 6e 64 65 78 20 69 6e 20 64 61 74 61 62  r index in datab
247ba 61 73 65 20 69 44 62 20 68 61 73 20 63 68 61 6e  ase iDb has chan
247bb 67 65 64 20 66 72 6f 6d 20 69 46 72 6f 6d 0a 2a  ged from iFrom.*
247bc 2a 20 74 6f 20 69 54 6f 2e 0a 2a 2a 0a 2a 2a 20  * to iTo..**.** 
247bd 54 69 63 6b 65 74 20 23 31 37 32 38 3a 20 20 54  Ticket #1728:  T
247be 68 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c 65 20  he symbol table 
247bf 6d 69 67 68 74 20 73 74 69 6c 6c 20 63 6f 6e 74  might still cont
247c0 61 69 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a  ain information.
247c1 2a 2a 20 6f 6e 20 74 61 62 6c 65 73 20 61 6e 64  ** on tables and
247c2 2f 6f 72 20 69 6e 64 69 63 65 73 20 74 68 61 74  /or indices that
247c3 20 61 72 65 20 74 68 65 20 70 72 6f 63 65 73 73   are the process
247c4 20 6f 66 20 62 65 69 6e 67 20 64 65 6c 65 74 65   of being delete
247c5 64 2e 0a 2a 2a 20 49 66 20 79 6f 75 20 61 72 65  d..** If you are
247c6 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e 65 20 6f 66   unlucky, one of
247c7 20 74 68 6f 73 65 20 64 65 6c 65 74 65 64 20 69   those deleted i
247c8 6e 64 69 63 65 73 20 6f 72 20 74 61 62 6c 65 73  ndices or tables
247c9 20 6d 69 67 68 74 0a 2a 2a 20 68 61 76 65 20 74   might.** have t
247ca 68 65 20 73 61 6d 65 20 72 6f 6f 74 70 61 67 65  he same rootpage
247cb 20 6e 75 6d 62 65 72 20 61 73 20 74 68 65 20 72   number as the r
247cc 65 61 6c 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  eal table or ind
247cd 65 78 20 74 68 61 74 20 69 73 0a 2a 2a 20 62 65  ex that is.** be
247ce 69 6e 67 20 6d 6f 76 65 64 2e 20 20 53 6f 20 77  ing moved.  So w
247cf 65 20 63 61 6e 6e 6f 74 20 73 74 6f 70 20 73 65  e cannot stop se
247d0 61 72 63 68 69 6e 67 20 61 66 74 65 72 20 74 68  arching after th
247d1 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 0a 2a  e first match .*
247d2 2a 20 62 65 63 61 75 73 65 20 74 68 65 20 66 69  * because the fi
247d3 72 73 74 20 6d 61 74 63 68 20 6d 69 67 68 74 20  rst match might 
247d4 62 65 20 66 6f 72 20 6f 6e 65 20 6f 66 20 74 68  be for one of th
247d5 65 20 64 65 6c 65 74 65 64 20 69 6e 64 69 63 65  e deleted indice
247d6 73 0a 2a 2a 20 6f 72 20 74 61 62 6c 65 73 20 61  s.** or tables a
247d7 6e 64 20 6e 6f 74 20 74 68 65 20 74 61 62 6c 65  nd not the table
247d8 2f 69 6e 64 65 78 20 74 68 61 74 20 69 73 20 61  /index that is a
247d9 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 20 6d 6f  ctually being mo
247da 76 65 64 2e 0a 2a 2a 20 57 65 20 6d 75 73 74 20  ved..** We must 
247db 63 6f 6e 74 69 6e 75 65 20 6c 6f 6f 70 69 6e 67  continue looping
247dc 20 75 6e 74 69 6c 20 61 6c 6c 20 74 61 62 6c 65   until all table
247dd 73 20 61 6e 64 20 69 6e 64 69 63 65 73 20 77 69  s and indices wi
247de 74 68 0a 2a 2a 20 72 6f 6f 74 70 61 67 65 3d 3d  th.** rootpage==
247df 69 46 72 6f 6d 20 68 61 76 65 20 62 65 65 6e 20  iFrom have been 
247e0 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 68 61 76  converted to hav
247e1 65 20 61 20 72 6f 6f 74 70 61 67 65 20 6f 66 20  e a rootpage of 
247e2 69 54 6f 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  iTo.** in order 
247e3 74 6f 20 62 65 20 63 65 72 74 61 69 6e 20 74 68  to be certain th
247e4 61 74 20 77 65 20 67 6f 74 20 74 68 65 20 72 69  at we got the ri
247e5 67 68 74 20 6f 6e 65 2e 0a 2a 2f 0a 23 69 66 6e  ght one..*/.#ifn
247e6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
247e7 41 55 54 4f 56 41 43 55 55 4d 0a 53 51 4c 49 54  AUTOVACUUM.SQLIT
247e8 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
247e9 71 6c 69 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f  qlite3RootPageMo
247ea 76 65 64 28 44 62 20 2a 70 44 62 2c 20 69 6e 74  ved(Db *pDb, int
247eb 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 29   iFrom, int iTo)
247ec 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 45  {.  HashElem *pE
247ed 6c 65 6d 3b 0a 20 20 48 61 73 68 20 2a 70 48 61  lem;.  Hash *pHa
247ee 73 68 3b 0a 0a 20 20 70 48 61 73 68 20 3d 20 26  sh;..  pHash = &
247ef 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62  pDb->pSchema->tb
247f0 6c 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 45 6c  lHash;.  for(pEl
247f1 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  em=sqliteHashFir
247f2 73 74 28 70 48 61 73 68 29 3b 20 70 45 6c 65 6d  st(pHash); pElem
247f3 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61  ; pElem=sqliteHa
247f4 73 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a  shNext(pElem)){.
247f5 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
247f6 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  = sqliteHashData
247f7 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20 69 66 28  (pElem);.    if(
247f8 20 70 54 61 62 2d 3e 74 6e 75 6d 3d 3d 69 46 72   pTab->tnum==iFr
247f9 6f 6d 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62  om ){.      pTab
247fa 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f 3b 0a 20 20  ->tnum = iTo;.  
247fb 20 20 7d 0a 20 20 7d 0a 20 20 70 48 61 73 68 20    }.  }.  pHash 
247fc 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  = &pDb->pSchema-
247fd 3e 69 64 78 48 61 73 68 3b 0a 20 20 66 6f 72 28  >idxHash;.  for(
247fe 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68  pElem=sqliteHash
247ff 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70 45  First(pHash); pE
24800 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74  lem; pElem=sqlit
24801 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d 29  eHashNext(pElem)
24802 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  ){.    Index *pI
24803 64 78 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44  dx = sqliteHashD
24804 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20  ata(pElem);.    
24805 69 66 28 20 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d  if( pIdx->tnum==
24806 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20 70  iFrom ){.      p
24807 49 64 78 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f 3b  Idx->tnum = iTo;
24808 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e  .    }.  }.}.#en
24809 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  dif../*.** Write
2480a 20 63 6f 64 65 20 74 6f 20 65 72 61 73 65 20 74   code to erase t
2480b 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f  he table with ro
2480c 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 66  ot-page iTable f
2480d 72 6f 6d 20 64 61 74 61 62 61 73 65 20 69 44 62  rom database iDb
2480e 2e 0a 2a 2a 20 41 6c 73 6f 20 77 72 69 74 65 20  ..** Also write 
2480f 63 6f 64 65 20 74 6f 20 6d 6f 64 69 66 79 20 74  code to modify t
24810 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
24811 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 74 65 72   table and inter
24812 6e 61 6c 20 73 63 68 65 6d 61 0a 2a 2a 20 69 66  nal schema.** if
24813 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20   a root-page of 
24814 61 6e 6f 74 68 65 72 20 74 61 62 6c 65 20 69 73  another table is
24815 20 6d 6f 76 65 64 20 62 79 20 74 68 65 20 62 74   moved by the bt
24816 72 65 65 2d 6c 61 79 65 72 20 77 68 69 6c 73 74  ree-layer whilst
24817 0a 2a 2a 20 65 72 61 73 69 6e 67 20 69 54 61 62  .** erasing iTab
24818 6c 65 20 28 74 68 69 73 20 63 61 6e 20 68 61 70  le (this can hap
24819 70 65 6e 20 77 69 74 68 20 61 6e 20 61 75 74 6f  pen with an auto
2481a 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65  -vacuum database
2481b 29 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f  )..*/ .static vo
2481c 69 64 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61  id destroyRootPa
2481d 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ge(Parse *pParse
2481e 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e  , int iTable, in
2481f 74 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a  t iDb){.  Vdbe *
24820 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
24821 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e  be(pParse);.  in
24822 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65  t r1 = sqlite3Ge
24823 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
24824 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
24825 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 65 73 74  ddOp3(v, OP_Dest
24826 72 6f 79 2c 20 69 54 61 62 6c 65 2c 20 72 31 2c  roy, iTable, r1,
24827 20 69 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33   iDb);.  sqlite3
24828 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65 29  MayAbort(pParse)
24829 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2482a 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
2482b 0a 20 20 2f 2a 20 4f 50 5f 44 65 73 74 72 6f 79  .  /* OP_Destroy
2482c 20 73 74 6f 72 65 73 20 61 6e 20 69 6e 20 69 6e   stores an in in
2482d 74 65 67 65 72 20 72 31 2e 20 49 66 20 74 68 69  teger r1. If thi
2482e 73 20 69 6e 74 65 67 65 72 0a 20 20 2a 2a 20 69  s integer.  ** i
2482f 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
24830 20 69 74 20 69 73 20 74 68 65 20 72 6f 6f 74 20   it is the root 
24831 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61  page number of a
24832 20 74 61 62 6c 65 20 6d 6f 76 65 64 20 74 6f 0a   table moved to.
24833 20 20 2a 2a 20 6c 6f 63 61 74 69 6f 6e 20 69 54    ** location iT
24834 61 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  able. The follow
24835 69 6e 67 20 63 6f 64 65 20 6d 6f 64 69 66 69 65  ing code modifie
24836 73 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  s the sqlite_mas
24837 74 65 72 20 74 61 62 6c 65 20 74 6f 0a 20 20 2a  ter table to.  *
24838 2a 20 72 65 66 6c 65 63 74 20 74 68 69 73 2e 0a  * reflect this..
24839 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 22 23    **.  ** The "#
2483a 4e 4e 4e 22 20 69 6e 20 74 68 65 20 53 51 4c 20  NNN" in the SQL 
2483b 69 73 20 61 20 73 70 65 63 69 61 6c 20 63 6f 6e  is a special con
2483c 73 74 61 6e 74 20 74 68 61 74 20 6d 65 61 6e 73  stant that means
2483d 20 77 68 61 74 65 76 65 72 20 76 61 6c 75 65 0a   whatever value.
2483e 20 20 2a 2a 20 69 73 20 69 6e 20 72 65 67 69 73    ** is in regis
2483f 74 65 72 20 4e 4e 4e 2e 20 20 53 65 65 20 67 72  ter NNN.  See gr
24840 61 6d 6d 61 72 20 72 75 6c 65 73 20 61 73 73 6f  ammar rules asso
24841 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
24842 54 4b 5f 52 45 47 49 53 54 45 52 0a 20 20 2a 2a  TK_REGISTER.  **
24843 20 74 6f 6b 65 6e 20 66 6f 72 20 61 64 64 69 74   token for addit
24844 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
24845 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  n..  */.  sqlite
24846 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
24847 72 73 65 2c 20 0a 20 20 20 20 20 22 55 50 44 41  rse, .     "UPDA
24848 54 45 20 25 51 2e 25 73 20 53 45 54 20 72 6f 6f  TE %Q.%s SET roo
24849 74 70 61 67 65 3d 25 64 20 57 48 45 52 45 20 23  tpage=%d WHERE #
2484a 25 64 20 41 4e 44 20 72 6f 6f 74 70 61 67 65 3d  %d AND rootpage=
2484b 23 25 64 22 2c 0a 20 20 20 20 20 70 50 61 72 73  #%d",.     pPars
2484c 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  e->db->aDb[iDb].
2484d 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41  zName, SCHEMA_TA
2484e 42 4c 45 28 69 44 62 29 2c 20 69 54 61 62 6c 65  BLE(iDb), iTable
2484f 2c 20 72 31 2c 20 72 31 29 3b 0a 23 65 6e 64 69  , r1, r1);.#endi
24850 66 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  f.  sqlite3Relea
24851 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
24852 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  , r1);.}../*.** 
24853 57 72 69 74 65 20 56 44 42 45 20 63 6f 64 65 20  Write VDBE code 
24854 74 6f 20 65 72 61 73 65 20 74 61 62 6c 65 20 70  to erase table p
24855 54 61 62 20 61 6e 64 20 61 6c 6c 20 61 73 73 6f  Tab and all asso
24856 63 69 61 74 65 64 20 69 6e 64 69 63 65 73 20 6f  ciated indices o
24857 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43 6f 64 65 20  n disk..** Code 
24858 74 6f 20 75 70 64 61 74 65 20 74 68 65 20 73 71  to update the sq
24859 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
2485a 65 73 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20  es and internal 
2485b 73 63 68 65 6d 61 20 64 65 66 69 6e 69 74 69 6f  schema definitio
2485c 6e 73 0a 2a 2a 20 69 6e 20 63 61 73 65 20 61 20  ns.** in case a 
2485d 72 6f 6f 74 2d 70 61 67 65 20 62 65 6c 6f 6e 67  root-page belong
2485e 69 6e 67 20 74 6f 20 61 6e 6f 74 68 65 72 20 74  ing to another t
2485f 61 62 6c 65 20 69 73 20 6d 6f 76 65 64 20 62 79  able is moved by
24860 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65 72   the btree layer
24861 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 61 64 64 65  .** is also adde
24862 64 20 28 74 68 69 73 20 63 61 6e 20 68 61 70 70  d (this can happ
24863 65 6e 20 77 69 74 68 20 61 6e 20 61 75 74 6f 2d  en with an auto-
24864 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65 29  vacuum database)
24865 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
24866 20 64 65 73 74 72 6f 79 54 61 62 6c 65 28 50 61   destroyTable(Pa
24867 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
24868 6c 65 20 2a 70 54 61 62 29 7b 0a 23 69 66 64 65  le *pTab){.#ifde
24869 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
2486a 54 4f 56 41 43 55 55 4d 0a 20 20 49 6e 64 65 78  TOVACUUM.  Index
2486b 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20 69 44   *pIdx;.  int iD
2486c 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
2486d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
2486e 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
2486f 6d 61 29 3b 0a 20 20 64 65 73 74 72 6f 79 52 6f  ma);.  destroyRo
24870 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c 20 70  otPage(pParse, p
24871 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b  Tab->tnum, iDb);
24872 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62  .  for(pIdx=pTab
24873 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
24874 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
24875 29 7b 0a 20 20 20 20 64 65 73 74 72 6f 79 52 6f  ){.    destroyRo
24876 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c 20 70  otPage(pParse, p
24877 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b  Idx->tnum, iDb);
24878 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20  .  }.#else.  /* 
24879 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
2487a 6d 61 79 20 62 65 20 61 75 74 6f 2d 76 61 63 75  may be auto-vacu
2487b 75 6d 20 63 61 70 61 62 6c 65 20 28 69 66 20 53  um capable (if S
2487c 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
2487d 41 43 55 55 4d 0a 20 20 2a 2a 20 69 73 20 6e 6f  ACUUM.  ** is no
2487e 74 20 64 65 66 69 6e 65 64 29 2c 20 74 68 65 6e  t defined), then
2487f 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   it is important
24880 20 74 6f 20 63 61 6c 6c 20 4f 50 5f 44 65 73 74   to call OP_Dest
24881 72 6f 79 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20  roy on the.  ** 
24882 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 20  table and index 
24883 72 6f 6f 74 2d 70 61 67 65 73 20 69 6e 20 6f 72  root-pages in or
24884 64 65 72 2c 20 73 74 61 72 74 69 6e 67 20 77 69  der, starting wi
24885 74 68 20 74 68 65 20 6e 75 6d 65 72 69 63 61 6c  th the numerical
24886 6c 79 20 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74  ly .  ** largest
24887 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65   root-page numbe
24888 72 2e 20 54 68 69 73 20 67 75 61 72 61 6e 74 65  r. This guarante
24889 65 73 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20  es that none of 
2488a 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 73 0a 20  the root-pages. 
2488b 20 2a 2a 20 74 6f 20 62 65 20 64 65 73 74 72 6f   ** to be destro
2488c 79 65 64 20 69 73 20 72 65 6c 6f 63 61 74 65 64  yed is relocated
2488d 20 62 79 20 61 6e 20 65 61 72 6c 69 65 72 20 4f   by an earlier O
2488e 50 5f 44 65 73 74 72 6f 79 2e 20 69 2e 65 2e 20  P_Destroy. i.e. 
2488f 69 66 20 74 68 65 0a 20 20 2a 2a 20 66 6f 6c 6c  if the.  ** foll
24890 6f 77 69 6e 67 20 77 65 72 65 20 63 6f 64 65 64  owing were coded
24891 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 50 5f 44  :.  **.  ** OP_D
24892 65 73 74 72 6f 79 20 34 20 30 0a 20 20 2a 2a 20  estroy 4 0.  ** 
24893 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 74  ....  ** OP_Dest
24894 72 6f 79 20 35 20 30 0a 20 20 2a 2a 0a 20 20 2a  roy 5 0.  **.  *
24895 2a 20 61 6e 64 20 72 6f 6f 74 20 70 61 67 65 20  * and root page 
24896 35 20 68 61 70 70 65 6e 65 64 20 74 6f 20 62 65  5 happened to be
24897 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f   the largest roo
24898 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20 69 6e  t-page number in
24899 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61   the.  ** databa
2489a 73 65 2c 20 74 68 65 6e 20 72 6f 6f 74 20 70 61  se, then root pa
2489b 67 65 20 35 20 77 6f 75 6c 64 20 62 65 20 6d 6f  ge 5 would be mo
2489c 76 65 64 20 74 6f 20 70 61 67 65 20 34 20 62 79  ved to page 4 by
2489d 20 74 68 65 20 0a 20 20 2a 2a 20 22 4f 50 5f 44   the .  ** "OP_D
2489e 65 73 74 72 6f 79 20 34 20 30 22 20 6f 70 63 6f  estroy 4 0" opco
2489f 64 65 2e 20 54 68 65 20 73 75 62 73 65 71 75 65  de. The subseque
248a0 6e 74 20 22 4f 50 5f 44 65 73 74 72 6f 79 20 35  nt "OP_Destroy 5
248a1 20 30 22 20 77 6f 75 6c 64 20 68 69 74 0a 20 20   0" would hit.  
248a2 2a 2a 20 61 20 66 72 65 65 2d 6c 69 73 74 20 70  ** a free-list p
248a3 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  age..  */.  int 
248a4 69 54 61 62 20 3d 20 70 54 61 62 2d 3e 74 6e 75  iTab = pTab->tnu
248a5 6d 3b 0a 20 20 69 6e 74 20 69 44 65 73 74 72 6f  m;.  int iDestro
248a6 79 65 64 20 3d 20 30 3b 0a 0a 20 20 77 68 69 6c  yed = 0;..  whil
248a7 65 28 20 31 20 29 7b 0a 20 20 20 20 49 6e 64 65  e( 1 ){.    Inde
248a8 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 69 6e 74  x *pIdx;.    int
248a9 20 69 4c 61 72 67 65 73 74 20 3d 20 30 3b 0a 0a   iLargest = 0;..
248aa 20 20 20 20 69 66 28 20 69 44 65 73 74 72 6f 79      if( iDestroy
248ab 65 64 3d 3d 30 20 7c 7c 20 69 54 61 62 3c 69 44  ed==0 || iTab<iD
248ac 65 73 74 72 6f 79 65 64 20 29 7b 0a 20 20 20 20  estroyed ){.    
248ad 20 20 69 4c 61 72 67 65 73 74 20 3d 20 69 54 61    iLargest = iTa
248ae 62 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  b;.    }.    for
248af 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
248b0 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
248b1 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
248b2 20 20 20 69 6e 74 20 69 49 64 78 20 3d 20 70 49     int iIdx = pI
248b3 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20  dx->tnum;.      
248b4 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70 53  assert( pIdx->pS
248b5 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63  chema==pTab->pSc
248b6 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20 69 66  hema );.      if
248b7 28 20 28 69 44 65 73 74 72 6f 79 65 64 3d 3d 30  ( (iDestroyed==0
248b8 20 7c 7c 20 28 69 49 64 78 3c 69 44 65 73 74 72   || (iIdx<iDestr
248b9 6f 79 65 64 29 29 20 26 26 20 69 49 64 78 3e 69  oyed)) && iIdx>i
248ba 4c 61 72 67 65 73 74 20 29 7b 0a 20 20 20 20 20  Largest ){.     
248bb 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20 69 49     iLargest = iI
248bc 64 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  dx;.      }.    
248bd 7d 0a 20 20 20 20 69 66 28 20 69 4c 61 72 67 65  }.    if( iLarge
248be 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  st==0 ){.      r
248bf 65 74 75 72 6e 3b 0a 20 20 20 20 7d 65 6c 73 65  eturn;.    }else
248c0 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 20  {.      int iDb 
248c1 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
248c2 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
248c3 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
248c4 29 3b 0a 20 20 20 20 20 20 64 65 73 74 72 6f 79  );.      destroy
248c5 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c  RootPage(pParse,
248c6 20 69 4c 61 72 67 65 73 74 2c 20 69 44 62 29 3b   iLargest, iDb);
248c7 0a 20 20 20 20 20 20 69 44 65 73 74 72 6f 79 65  .      iDestroye
248c8 64 20 3d 20 69 4c 61 72 67 65 73 74 3b 0a 20 20  d = iLargest;.  
248c9 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d    }.  }.#endif.}
248ca 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
248cb 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74  tine is called t
248cc 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66  o do the work of
248cd 20 61 20 44 52 4f 50 20 54 41 42 4c 45 20 73 74   a DROP TABLE st
248ce 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 70 4e 61 6d  atement..** pNam
248cf 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  e is the name of
248d0 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
248d1 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51 4c   dropped..*/.SQL
248d2 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
248d3 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c   sqlite3DropTabl
248d4 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
248d5 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c   SrcList *pName,
248d6 20 69 6e 74 20 69 73 56 69 65 77 2c 20 69 6e 74   int isView, int
248d7 20 6e 6f 45 72 72 29 7b 0a 20 20 54 61 62 6c 65   noErr){.  Table
248d8 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65 20 2a   *pTab;.  Vdbe *
248d9 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  v;.  sqlite3 *db
248da 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
248db 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69 66 28   int iDb;..  if(
248dc 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
248dd 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78  d ){.    goto ex
248de 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20  it_drop_table;. 
248df 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61   }.  assert( pPa
248e0 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 0a  rse->nErr==0 );.
248e1 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 2d    assert( pName-
248e2 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 70 54  >nSrc==1 );.  pT
248e3 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
248e4 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  teTable(pParse, 
248e5 69 73 56 69 65 77 2c 20 0a 20 20 20 20 20 20 20  isView, .       
248e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
248e7 20 20 20 20 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d       pName->a[0]
248e8 2e 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61  .zName, pName->a
248e9 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a  [0].zDatabase);.
248ea 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  .  if( pTab==0 )
248eb 7b 0a 20 20 20 20 69 66 28 20 6e 6f 45 72 72 20  {.    if( noErr 
248ec 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
248ed 45 72 72 6f 72 43 6c 65 61 72 28 70 50 61 72 73  ErrorClear(pPars
248ee 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f  e);.    }.    go
248ef 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
248f0 6c 65 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20  le;.  }.  iDb = 
248f1 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
248f2 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70  ndex(db, pTab->p
248f3 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73 65 72  Schema);.  asser
248f4 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44 62  t( iDb>=0 && iDb
248f5 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f  <db->nDb );..  /
248f6 2a 20 49 66 20 70 54 61 62 20 69 73 20 61 20 76  * If pTab is a v
248f7 69 72 74 75 61 6c 20 74 61 62 6c 65 2c 20 63 61  irtual table, ca
248f8 6c 6c 20 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e  ll ViewGetColumn
248f9 4e 61 6d 65 73 28 29 20 74 6f 20 65 6e 73 75 72  Names() to ensur
248fa 65 0a 20 20 2a 2a 20 69 74 20 69 73 20 69 6e 69  e.  ** it is ini
248fb 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20  tialized..  */. 
248fc 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
248fd 54 61 62 29 20 26 26 20 73 71 6c 69 74 65 33 56  Tab) && sqlite3V
248fe 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
248ff 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20  s(pParse, pTab) 
24900 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  ){.    goto exit
24901 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d  _drop_table;.  }
24902 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
24903 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
24904 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63  ON.  {.    int c
24905 6f 64 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ode;.    const c
24906 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45  har *zTab = SCHE
24907 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20  MA_TABLE(iDb);. 
24908 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
24909 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
2490a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e  ].zName;.    con
2490b 73 74 20 63 68 61 72 20 2a 7a 41 72 67 32 20 3d  st char *zArg2 =
2490c 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   0;.    if( sqli
2490d 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
2490e 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45  rse, SQLITE_DELE
2490f 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62  TE, zTab, 0, zDb
24910 29 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65  )){.      goto e
24911 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a  xit_drop_table;.
24912 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 73      }.    if( is
24913 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 69 66  View ){.      if
24914 28 20 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26  ( !OMIT_TEMPDB &
24915 26 20 69 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20  & iDb==1 ){.    
24916 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
24917 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57  E_DROP_TEMP_VIEW
24918 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
24919 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51         code = SQ
2491a 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 3b 0a  LITE_DROP_VIEW;.
2491b 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20        }.#ifndef 
2491c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2491d 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c  UALTABLE.    }el
2491e 73 65 20 69 66 28 20 49 73 56 69 72 74 75 61 6c  se if( IsVirtual
2491f 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
24920 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
24921 4f 50 5f 56 54 41 42 4c 45 3b 0a 20 20 20 20 20  OP_VTABLE;.     
24922 20 7a 41 72 67 32 20 3d 20 73 71 6c 69 74 65 33   zArg2 = sqlite3
24923 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54  GetVTable(db, pT
24924 61 62 29 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65  ab)->pMod->zName
24925 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c  ;.#endif.    }el
24926 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f  se{.      if( !O
24927 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44  MIT_TEMPDB && iD
24928 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  b==1 ){.        
24929 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
2492a 4f 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20  OP_TEMP_TABLE;. 
2492b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2492c 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
2492d 45 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20  E_DROP_TABLE;.  
2492e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2492f 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43  if( sqlite3AuthC
24930 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64  heck(pParse, cod
24931 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  e, pTab->zName, 
24932 7a 41 72 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20  zArg2, zDb) ){. 
24933 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64       goto exit_d
24934 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d  rop_table;.    }
24935 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
24936 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
24937 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
24938 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c   pTab->zName, 0,
24939 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67   zDb) ){.      g
2493a 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
2493b 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  ble;.    }.  }.#
2493c 65 6e 64 69 66 0a 20 20 69 66 28 20 73 71 6c 69  endif.  if( sqli
2493d 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62  te3StrNICmp(pTab
2493e 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65  ->zName, "sqlite
2493f 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20  _", 7)==0 ){.   
24940 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
24941 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
24942 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 64 72  %s may not be dr
24943 6f 70 70 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e  opped", pTab->zN
24944 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ame);.    goto e
24945 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a  xit_drop_table;.
24946 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
24947 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20  ITE_OMIT_VIEW.  
24948 2f 2a 20 45 6e 73 75 72 65 20 44 52 4f 50 20 54  /* Ensure DROP T
24949 41 42 4c 45 20 69 73 20 6e 6f 74 20 75 73 65 64  ABLE is not used
2494a 20 6f 6e 20 61 20 76 69 65 77 2c 20 61 6e 64 20   on a view, and 
2494b 44 52 4f 50 20 56 49 45 57 20 69 73 20 6e 6f 74  DROP VIEW is not
2494c 20 75 73 65 64 0a 20 20 2a 2a 20 6f 6e 20 61 20   used.  ** on a 
2494d 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  table..  */.  if
2494e 28 20 69 73 56 69 65 77 20 26 26 20 70 54 61 62  ( isView && pTab
2494f 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a  ->pSelect==0 ){.
24950 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24951 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 73 65  Msg(pParse, "use
24952 20 44 52 4f 50 20 54 41 42 4c 45 20 74 6f 20 64   DROP TABLE to d
24953 65 6c 65 74 65 20 74 61 62 6c 65 20 25 73 22 2c  elete table %s",
24954 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
24955 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
24956 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69  p_table;.  }.  i
24957 66 28 20 21 69 73 56 69 65 77 20 26 26 20 70 54  f( !isView && pT
24958 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  ab->pSelect ){. 
24959 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2495a 73 67 28 70 50 61 72 73 65 2c 20 22 75 73 65 20  sg(pParse, "use 
2495b 44 52 4f 50 20 56 49 45 57 20 74 6f 20 64 65 6c  DROP VIEW to del
2495c 65 74 65 20 76 69 65 77 20 25 73 22 2c 20 70 54  ete view %s", pT
2495d 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
2495e 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74  goto exit_drop_t
2495f 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  able;.  }.#endif
24960 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
24961 63 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74  code to remove t
24962 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68  he table from th
24963 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20  e master table. 
24964 20 2a 2a 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a   ** on disk..  *
24965 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
24966 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
24967 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 54    if( v ){.    T
24968 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
24969 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20  ;.    Db *pDb = 
2496a 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20  &db->aDb[iDb];. 
2496b 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
2496c 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
2496d 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a  arse, 1, iDb);..
2496e 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2496f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
24970 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75  .    if( IsVirtu
24971 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
24972 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24973 4f 70 30 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e  Op0(v, OP_VBegin
24974 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
24975 20 20 20 20 73 71 6c 69 74 65 33 46 6b 44 72 6f      sqlite3FkDro
24976 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  pTable(pParse, p
24977 4e 61 6d 65 2c 20 70 54 61 62 29 3b 0a 0a 20 20  Name, pTab);..  
24978 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 74 72    /* Drop all tr
24979 69 67 67 65 72 73 20 61 73 73 6f 63 69 61 74 65  iggers associate
2497a 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c 65  d with the table
2497b 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20   being dropped. 
2497c 43 6f 64 65 0a 20 20 20 20 2a 2a 20 69 73 20 67  Code.    ** is g
2497d 65 6e 65 72 61 74 65 64 20 74 6f 20 72 65 6d 6f  enerated to remo
2497e 76 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20  ve entries from 
2497f 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e  sqlite_master an
24980 64 2f 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69  d/or.    ** sqli
24981 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 20 69  te_temp_master i
24982 66 20 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20  f required..    
24983 2a 2f 0a 20 20 20 20 70 54 72 69 67 67 65 72 20  */.    pTrigger 
24984 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
24985 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  List(pParse, pTa
24986 62 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70  b);.    while( p
24987 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20  Trigger ){.     
24988 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65   assert( pTrigge
24989 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62  r->pSchema==pTab
2498a 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a 20 20  ->pSchema || .  
2498b 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72          pTrigger
2498c 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61  ->pSchema==db->a
2498d 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b  Db[1].pSchema );
2498e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 72  .      sqlite3Dr
2498f 6f 70 54 72 69 67 67 65 72 50 74 72 28 70 50 61  opTriggerPtr(pPa
24990 72 73 65 2c 20 70 54 72 69 67 67 65 72 29 3b 0a  rse, pTrigger);.
24991 20 20 20 20 20 20 70 54 72 69 67 67 65 72 20 3d        pTrigger =
24992 20 70 54 72 69 67 67 65 72 2d 3e 70 4e 65 78 74   pTrigger->pNext
24993 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66  ;.    }..#ifndef
24994 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
24995 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f  OINCREMENT.    /
24996 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20 65 6e 74  * Remove any ent
24997 72 69 65 73 20 6f 66 20 74 68 65 20 73 71 6c 69  ries of the sqli
24998 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
24999 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
2499a 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 61 62  h.    ** the tab
2499b 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65 64  le being dropped
2499c 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20 62  . This is done b
2499d 65 66 6f 72 65 20 74 68 65 20 74 61 62 6c 65 20  efore the table 
2499e 69 73 20 64 72 6f 70 70 65 64 0a 20 20 20 20 2a  is dropped.    *
2499f 2a 20 61 74 20 74 68 65 20 62 74 72 65 65 20 6c  * at the btree l
249a0 65 76 65 6c 2c 20 69 6e 20 63 61 73 65 20 74 68  evel, in case th
249a1 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
249a2 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20 74 6f  e table needs to
249a3 0a 20 20 20 20 2a 2a 20 6d 6f 76 65 20 61 73 20  .    ** move as 
249a4 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  a result of the 
249a5 64 72 6f 70 20 28 63 61 6e 20 68 61 70 70 65 6e  drop (can happen
249a6 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   in auto-vacuum 
249a7 6d 6f 64 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  mode)..    */.  
249a8 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62 46    if( pTab->tabF
249a9 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e  lags & TF_Autoin
249aa 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20  crement ){.     
249ab 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
249ac 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
249ad 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
249ae 20 25 73 2e 73 71 6c 69 74 65 5f 73 65 71 75 65   %s.sqlite_seque
249af 6e 63 65 20 57 48 45 52 45 20 6e 61 6d 65 3d 25  nce WHERE name=%
249b0 51 22 2c 0a 20 20 20 20 20 20 20 20 70 44 62 2d  Q",.        pDb-
249b1 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e  >zName, pTab->zN
249b2 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  ame.      );.   
249b3 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f   }.#endif..    /
249b4 2a 20 44 72 6f 70 20 61 6c 6c 20 53 51 4c 49 54  * Drop all SQLIT
249b5 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 61  E_MASTER table a
249b6 6e 64 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73  nd index entries
249b7 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74   that refer to t
249b8 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e  he.    ** table.
249b9 20 54 68 65 20 70 72 6f 67 72 61 6d 20 6e 61 6d   The program nam
249ba 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20  e loops through 
249bb 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65  the master table
249bc 20 61 6e 64 20 64 65 6c 65 74 65 73 0a 20 20 20   and deletes.   
249bd 20 2a 2a 20 65 76 65 72 79 20 72 6f 77 20 74 68   ** every row th
249be 61 74 20 72 65 66 65 72 73 20 74 6f 20 61 20 74  at refers to a t
249bf 61 62 6c 65 20 6f 66 20 74 68 65 20 73 61 6d 65  able of the same
249c0 20 6e 61 6d 65 20 61 73 20 74 68 65 20 6f 6e 65   name as the one
249c1 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 64 72   being.    ** dr
249c2 6f 70 70 65 64 2e 20 54 72 69 67 67 65 72 73 20  opped. Triggers 
249c3 61 72 65 20 68 61 6e 64 6c 65 64 20 73 65 70 65  are handled sepe
249c4 72 61 74 65 6c 79 20 62 65 63 61 75 73 65 20 61  rately because a
249c5 20 74 72 69 67 67 65 72 20 63 61 6e 20 62 65 0a   trigger can be.
249c6 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20 69      ** created i
249c7 6e 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62  n the temp datab
249c8 61 73 65 20 74 68 61 74 20 72 65 66 65 72 73 20  ase that refers 
249c9 74 6f 20 61 20 74 61 62 6c 65 20 69 6e 20 61 6e  to a table in an
249ca 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 64 61 74  other.    ** dat
249cb 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  abase..    */.  
249cc 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
249cd 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20  arse(pParse, .  
249ce 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52        "DELETE FR
249cf 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 20 74  OM %Q.%s WHERE t
249d0 62 6c 5f 6e 61 6d 65 3d 25 51 20 61 6e 64 20 74  bl_name=%Q and t
249d1 79 70 65 21 3d 27 74 72 69 67 67 65 72 27 22 2c  ype!='trigger'",
249d2 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e  .        pDb->zN
249d3 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ame, SCHEMA_TABL
249d4 45 28 69 44 62 29 2c 20 70 54 61 62 2d 3e 7a 4e  E(iDb), pTab->zN
249d5 61 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72  ame);..    /* Dr
249d6 6f 70 20 61 6e 79 20 73 74 61 74 69 73 74 69 63  op any statistic
249d7 73 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74  s from the sqlit
249d8 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2c 20 69  e_stat1 table, i
249d9 66 20 69 74 20 65 78 69 73 74 73 20 2a 2f 0a 20  f it exists */. 
249da 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
249db 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c  ndTable(db, "sql
249dc 69 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e  ite_stat1", db->
249dd 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20  aDb[iDb].zName) 
249de 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
249df 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
249e0 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c  se,.        "DEL
249e1 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69  ETE FROM %Q.sqli
249e2 74 65 5f 73 74 61 74 31 20 57 48 45 52 45 20 74  te_stat1 WHERE t
249e3 62 6c 3d 25 51 22 2c 20 70 44 62 2d 3e 7a 4e 61  bl=%Q", pDb->zNa
249e4 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a  me, pTab->zName.
249e5 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a        );.    }..
249e6 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20      if( !isView 
249e7 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54  && !IsVirtual(pT
249e8 61 62 29 20 29 7b 0a 20 20 20 20 20 20 64 65 73  ab) ){.      des
249e9 74 72 6f 79 54 61 62 6c 65 28 70 50 61 72 73 65  troyTable(pParse
249ea 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a 0a  , pTab);.    }..
249eb 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68      /* Remove th
249ec 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 66 72  e table entry fr
249ed 6f 6d 20 53 51 4c 69 74 65 27 73 20 69 6e 74 65  om SQLite's inte
249ee 72 6e 61 6c 20 73 63 68 65 6d 61 20 61 6e 64 20  rnal schema and 
249ef 6d 6f 64 69 66 79 0a 20 20 20 20 2a 2a 20 74 68  modify.    ** th
249f0 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e  e schema cookie.
249f1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
249f2 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
249f3 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
249f4 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
249f5 5f 56 44 65 73 74 72 6f 79 2c 20 69 44 62 2c 20  _VDestroy, iDb, 
249f6 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  0, 0, pTab->zNam
249f7 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20  e, 0);.    }.   
249f8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
249f9 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62  p4(v, OP_DropTab
249fa 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70  le, iDb, 0, 0, p
249fb 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  Tab->zName, 0);.
249fc 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67      sqlite3Chang
249fd 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
249fe 69 44 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  iDb);.  }.  sqli
249ff 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 64  teViewResetAll(d
24a00 62 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f 64  b, iDb);..exit_d
24a01 72 6f 70 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c  rop_table:.  sql
24a02 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
24a03 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a  e(db, pName);.}.
24a04 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
24a05 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ine is called to
24a06 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 66 6f   create a new fo
24a07 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 68 65  reign key on the
24a08 20 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72 65 6e   table.** curren
24a09 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
24a0a 75 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d 43 6f  uction.  pFromCo
24a0b 6c 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69  l determines whi
24a0c 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e  ch columns.** in
24a0d 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62   the current tab
24a0e 6c 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  le point to the 
24a0f 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20 49 66  foreign key.  If
24a10 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74 68 65   pFromCol==0 the
24a11 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74 68 65  n.** connect the
24a12 20 6b 65 79 20 74 6f 20 74 68 65 20 6c 61 73 74   key to the last
24a13 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74 65 64   column inserted
24a14 2e 20 20 70 54 6f 20 69 73 20 74 68 65 20 6e 61  .  pTo is the na
24a15 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62  me of.** the tab
24a16 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f 2e 20  le referred to. 
24a17 20 70 54 6f 43 6f 6c 20 69 73 20 61 20 6c 69 73   pToCol is a lis
24a18 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74  t of tables in t
24a19 68 65 20 6f 74 68 65 72 0a 2a 2a 20 70 54 6f 20  he other.** pTo 
24a1a 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 66  table that the f
24a1b 6f 72 65 69 67 6e 20 6b 65 79 20 70 6f 69 6e 74  oreign key point
24a1c 73 20 74 6f 2e 20 20 66 6c 61 67 73 20 63 6f 6e  s to.  flags con
24a1d 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a 20 69 6e 66  tains all.** inf
24a1e 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
24a1f 68 65 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f  he conflict reso
24a20 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d  lution algorithm
24a21 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 69  s specified.** i
24a22 6e 20 74 68 65 20 4f 4e 20 44 45 4c 45 54 45 2c  n the ON DELETE,
24a23 20 4f 4e 20 55 50 44 41 54 45 20 61 6e 64 20 4f   ON UPDATE and O
24a24 4e 20 49 4e 53 45 52 54 20 63 6c 61 75 73 65 73  N INSERT clauses
24a25 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 4b 65 79 20  ..**.** An FKey 
24a26 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72 65  structure is cre
24a27 61 74 65 64 20 61 6e 64 20 61 64 64 65 64 20 74  ated and added t
24a28 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72  o the table curr
24a29 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64 65 72 20 63  ently.** under c
24a2a 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74  onstruction in t
24a2b 68 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  he pParse->pNewT
24a2c 61 62 6c 65 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a  able field..**.*
24a2d 2a 20 54 68 65 20 66 6f 72 65 69 67 6e 20 6b 65  * The foreign ke
24a2e 79 20 69 73 20 73 65 74 20 66 6f 72 20 49 4d 4d  y is set for IMM
24a2f 45 44 49 41 54 45 20 70 72 6f 63 65 73 73 69 6e  EDIATE processin
24a30 67 2e 20 20 41 20 73 75 62 73 65 71 75 65 6e 74  g.  A subsequent
24a31 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69   call.** to sqli
24a32 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b  te3DeferForeignK
24a33 65 79 28 29 20 6d 69 67 68 74 20 63 68 61 6e 67  ey() might chang
24a34 65 20 74 68 69 73 20 74 6f 20 44 45 46 45 52 52  e this to DEFERR
24a35 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ED..*/.SQLITE_PR
24a36 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
24a37 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b  e3CreateForeignK
24a38 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ey(.  Parse *pPa
24a39 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61  rse,       /* Pa
24a3a 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
24a3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46 72  .  ExprList *pFr
24a3c 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 6d  omCol,  /* Colum
24a3d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  ns in this table
24a3e 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f   that point to o
24a3f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ther table */.  
24a40 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20 20 20 20  Token *pTo,     
24a41 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
24a42 74 68 65 20 6f 74 68 65 72 20 74 61 62 6c 65 20  the other table 
24a43 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
24a44 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f 6c  ToCol,    /* Col
24a45 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f 74 68 65  umns in the othe
24a46 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  r table */.  int
24a47 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 20 20   flags          
24a48 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65    /* Conflict re
24a49 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74  solution algorit
24a4a 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  hms. */.){.  sql
24a4b 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
24a4c 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 53  e->db;.#ifndef S
24a4d 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
24a4e 47 4e 5f 4b 45 59 0a 20 20 46 4b 65 79 20 2a 70  GN_KEY.  FKey *p
24a4f 46 4b 65 79 20 3d 20 30 3b 0a 20 20 46 4b 65 79  FKey = 0;.  FKey
24a50 20 2a 70 4e 65 78 74 54 6f 3b 0a 20 20 54 61 62   *pNextTo;.  Tab
24a51 6c 65 20 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e  le *p = pParse->
24a52 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74  pNewTable;.  int
24a53 20 6e 42 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b   nByte;.  int i;
24a54 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63  .  int nCol;.  c
24a55 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72  har *z;..  asser
24a56 74 28 20 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69  t( pTo!=0 );.  i
24a57 66 28 20 70 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45  f( p==0 || IN_DE
24a58 43 4c 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74  CLARE_VTAB ) got
24a59 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20  o fk_end;.  if( 
24a5a 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20  pFromCol==0 ){. 
24a5b 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d     int iCol = p-
24a5c 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28  >nCol-1;.    if(
24a5d 20 4e 45 56 45 52 28 69 43 6f 6c 3c 30 29 20 29   NEVER(iCol<0) )
24a5e 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20   goto fk_end;.  
24a5f 20 20 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20    if( pToCol && 
24a60 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31  pToCol->nExpr!=1
24a61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
24a62 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
24a63 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f  , "foreign key o
24a64 6e 20 25 73 22 0a 20 20 20 20 20 20 20 20 20 22  n %s".         "
24a65 20 73 68 6f 75 6c 64 20 72 65 66 65 72 65 6e 63   should referenc
24a66 65 20 6f 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d  e only one colum
24a67 6e 20 6f 66 20 74 61 62 6c 65 20 25 54 22 2c 0a  n of table %T",.
24a68 20 20 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c           p->aCol
24a69 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54  [iCol].zName, pT
24a6a 6f 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66  o);.      goto f
24a6b 6b 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20  k_end;.    }.   
24a6c 20 6e 43 6f 6c 20 3d 20 31 3b 0a 20 20 7d 65 6c   nCol = 1;.  }el
24a6d 73 65 20 69 66 28 20 70 54 6f 43 6f 6c 20 26 26  se if( pToCol &&
24a6e 20 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d   pToCol->nExpr!=
24a6f 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 20  pFromCol->nExpr 
24a70 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
24a71 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20  rorMsg(pParse,. 
24a72 20 20 20 20 20 20 20 22 6e 75 6d 62 65 72 20 6f         "number o
24a73 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72  f columns in for
24a74 65 69 67 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f  eign key does no
24a75 74 20 6d 61 74 63 68 20 74 68 65 20 6e 75 6d 62  t match the numb
24a76 65 72 20 6f 66 20 22 0a 20 20 20 20 20 20 20 20  er of ".        
24a77 22 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20  "columns in the 
24a78 72 65 66 65 72 65 6e 63 65 64 20 74 61 62 6c 65  referenced table
24a79 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f  ");.    goto fk_
24a7a 65 6e 64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  end;.  }else{.  
24a7b 20 20 6e 43 6f 6c 20 3d 20 70 46 72 6f 6d 43 6f    nCol = pFromCo
24a7c 6c 2d 3e 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20  l->nExpr;.  }.  
24a7d 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a  nByte = sizeof(*
24a7e 70 46 4b 65 79 29 20 2b 20 28 6e 43 6f 6c 2d 31  pFKey) + (nCol-1
24a7f 29 2a 73 69 7a 65 6f 66 28 70 46 4b 65 79 2d 3e  )*sizeof(pFKey->
24a80 61 43 6f 6c 5b 30 5d 29 20 2b 20 70 54 6f 2d 3e  aCol[0]) + pTo->
24a81 6e 20 2b 20 31 3b 0a 20 20 69 66 28 20 70 54 6f  n + 1;.  if( pTo
24a82 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  Col ){.    for(i
24a83 3d 30 3b 20 69 3c 70 54 6f 43 6f 6c 2d 3e 6e 45  =0; i<pToCol->nE
24a84 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
24a85 20 6e 42 79 74 65 20 2b 3d 20 73 71 6c 69 74 65   nByte += sqlite
24a86 33 53 74 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c  3Strlen30(pToCol
24a87 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20  ->a[i].zName) + 
24a88 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  1;.    }.  }.  p
24a89 46 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 44 62  FKey = sqlite3Db
24a8a 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e  MallocZero(db, n
24a8b 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 70 46  Byte );.  if( pF
24a8c 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f  Key==0 ){.    go
24a8d 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20  to fk_end;.  }. 
24a8e 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 20 3d 20   pFKey->pFrom = 
24a8f 70 3b 0a 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78  p;.  pFKey->pNex
24a90 74 46 72 6f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79  tFrom = p->pFKey
24a91 3b 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 26  ;.  z = (char*)&
24a92 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6e 43 6f 6c  pFKey->aCol[nCol
24a93 5d 3b 0a 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 20  ];.  pFKey->zTo 
24a94 3d 20 7a 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c  = z;.  memcpy(z,
24a95 20 70 54 6f 2d 3e 7a 2c 20 70 54 6f 2d 3e 6e 29   pTo->z, pTo->n)
24a96 3b 0a 20 20 7a 5b 70 54 6f 2d 3e 6e 5d 20 3d 20  ;.  z[pTo->n] = 
24a97 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 71 75  0;.  sqlite3Dequ
24a98 6f 74 65 28 7a 29 3b 0a 20 20 7a 20 2b 3d 20 70  ote(z);.  z += p
24a99 54 6f 2d 3e 6e 2b 31 3b 0a 20 20 70 46 4b 65 79  To->n+1;.  pFKey
24a9a 2d 3e 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20  ->nCol = nCol;. 
24a9b 20 69 66 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30   if( pFromCol==0
24a9c 20 29 7b 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61   ){.    pFKey->a
24a9d 43 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70  Col[0].iFrom = p
24a9e 2d 3e 6e 43 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73  ->nCol-1;.  }els
24a9f 65 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  e{.    for(i=0; 
24aa0 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
24aa1 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20      int j;.     
24aa2 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e   for(j=0; j<p->n
24aa3 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Col; j++){.     
24aa4 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
24aa5 72 49 43 6d 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d  rICmp(p->aCol[j]
24aa6 2e 7a 4e 61 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c  .zName, pFromCol
24aa7 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30  ->a[i].zName)==0
24aa8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 46   ){.          pF
24aa9 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72  Key->aCol[i].iFr
24aaa 6f 6d 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20  om = j;.        
24aab 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
24aac 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
24aad 20 69 66 28 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20   if( j>=p->nCol 
24aae 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
24aaf 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
24ab0 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 22 75  e, .          "u
24ab1 6e 6b 6e 6f 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22  nknown column \"
24ab2 25 73 5c 22 20 69 6e 20 66 6f 72 65 69 67 6e 20  %s\" in foreign 
24ab3 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 22 2c  key definition",
24ab4 20 0a 20 20 20 20 20 20 20 20 20 20 70 46 72 6f   .          pFro
24ab5 6d 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  mCol->a[i].zName
24ab6 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  );.        goto 
24ab7 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  fk_end;.      }.
24ab8 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
24ab9 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f  pToCol ){.    fo
24aba 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
24abb 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ++){.      int n
24abc 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
24abd 33 30 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e  30(pToCol->a[i].
24abe 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 46  zName);.      pF
24abf 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f  Key->aCol[i].zCo
24ac0 6c 20 3d 20 7a 3b 0a 20 20 20 20 20 20 6d 65 6d  l = z;.      mem
24ac1 63 70 79 28 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61  cpy(z, pToCol->a
24ac2 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20  [i].zName, n);. 
24ac3 20 20 20 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20       z[n] = 0;. 
24ac4 20 20 20 20 20 7a 20 2b 3d 20 6e 2b 31 3b 0a 20       z += n+1;. 
24ac5 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79     }.  }.  pFKey
24ac6 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d 20 30  ->isDeferred = 0
24ac7 3b 0a 20 20 70 46 4b 65 79 2d 3e 61 41 63 74 69  ;.  pFKey->aActi
24ac8 6f 6e 5b 30 5d 20 3d 20 28 75 38 29 28 66 6c 61  on[0] = (u8)(fla
24ac9 67 73 20 26 20 30 78 66 66 29 3b 20 20 20 20 20  gs & 0xff);     
24aca 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20 44 45 4c         /* ON DEL
24acb 45 54 45 20 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ETE action */.  
24acc 70 46 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 31  pFKey->aAction[1
24acd 5d 20 3d 20 28 75 38 29 28 28 66 6c 61 67 73 20  ] = (u8)((flags 
24ace 3e 3e 20 38 20 29 20 26 20 30 78 66 66 29 3b 20  >> 8 ) & 0xff); 
24acf 20 20 20 2f 2a 20 4f 4e 20 55 50 44 41 54 45 20     /* ON UPDATE 
24ad0 61 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 70 4e 65  action */..  pNe
24ad1 78 74 54 6f 20 3d 20 28 46 4b 65 79 20 2a 29 73  xtTo = (FKey *)s
24ad2 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
24ad3 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6b  (&p->pSchema->fk
24ad4 65 79 48 61 73 68 2c 20 0a 20 20 20 20 20 20 70  eyHash, .      p
24ad5 46 4b 65 79 2d 3e 7a 54 6f 2c 20 73 71 6c 69 74  FKey->zTo, sqlit
24ad6 65 33 53 74 72 6c 65 6e 33 30 28 70 46 4b 65 79  e3Strlen30(pFKey
24ad7 2d 3e 7a 54 6f 29 2c 20 28 76 6f 69 64 20 2a 29  ->zTo), (void *)
24ad8 70 46 4b 65 79 0a 20 20 29 3b 0a 20 20 69 66 28  pFKey.  );.  if(
24ad9 20 70 4e 65 78 74 54 6f 3d 3d 70 46 4b 65 79 20   pNextTo==pFKey 
24ada 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  ){.    db->mallo
24adb 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
24adc 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20   goto fk_end;.  
24add 7d 0a 20 20 69 66 28 20 70 4e 65 78 74 54 6f 20  }.  if( pNextTo 
24ade 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
24adf 4e 65 78 74 54 6f 2d 3e 70 50 72 65 76 54 6f 3d  NextTo->pPrevTo=
24ae0 3d 30 20 29 3b 0a 20 20 20 20 70 46 4b 65 79 2d  =0 );.    pFKey-
24ae1 3e 70 4e 65 78 74 54 6f 20 3d 20 70 4e 65 78 74  >pNextTo = pNext
24ae2 54 6f 3b 0a 20 20 20 20 70 4e 65 78 74 54 6f 2d  To;.    pNextTo-
24ae3 3e 70 50 72 65 76 54 6f 20 3d 20 70 46 4b 65 79  >pPrevTo = pFKey
24ae4 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 69 6e 6b  ;.  }..  /* Link
24ae5 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
24ae6 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 61 73   to the table as
24ae7 20 74 68 65 20 6c 61 73 74 20 73 74 65 70 2e 0a   the last step..
24ae8 20 20 2a 2f 0a 20 20 70 2d 3e 70 46 4b 65 79 20    */.  p->pFKey 
24ae9 3d 20 70 46 4b 65 79 3b 0a 20 20 70 46 4b 65 79  = pFKey;.  pFKey
24aea 20 3d 20 30 3b 0a 0a 66 6b 5f 65 6e 64 3a 0a 20   = 0;..fk_end:. 
24aeb 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
24aec 62 2c 20 70 46 4b 65 79 29 3b 0a 23 65 6e 64 69  b, pFKey);.#endi
24aed 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
24aee 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
24aef 4e 5f 4b 45 59 29 20 2a 2f 0a 20 20 73 71 6c 69  N_KEY) */.  sqli
24af0 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
24af1 65 28 64 62 2c 20 70 46 72 6f 6d 43 6f 6c 29 3b  e(db, pFromCol);
24af2 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
24af3 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 6f  stDelete(db, pTo
24af4 43 6f 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  Col);.}../*.** T
24af5 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
24af6 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4e  alled when an IN
24af7 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54  ITIALLY IMMEDIAT
24af8 45 20 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 44  E or INITIALLY D
24af9 45 46 45 52 52 45 44 0a 2a 2a 20 63 6c 61 75 73  EFERRED.** claus
24afa 65 20 69 73 20 73 65 65 6e 20 61 73 20 70 61 72  e is seen as par
24afb 74 20 6f 66 20 61 20 66 6f 72 65 69 67 6e 20 6b  t of a foreign k
24afc 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 20  ey definition.  
24afd 54 68 65 20 69 73 44 65 66 65 72 72 65 64 0a 2a  The isDeferred.*
24afe 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 31  * parameter is 1
24aff 20 66 6f 72 20 49 4e 49 54 49 41 4c 4c 59 20 44   for INITIALLY D
24b00 45 46 45 52 52 45 44 20 61 6e 64 20 30 20 66 6f  EFERRED and 0 fo
24b01 72 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45  r INITIALLY IMME
24b02 44 49 41 54 45 2e 0a 2a 2a 20 54 68 65 20 62 65  DIATE..** The be
24b03 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20 6d 6f  havior of the mo
24b04 73 74 20 72 65 63 65 6e 74 6c 79 20 63 72 65 61  st recently crea
24b05 74 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ted foreign key 
24b06 69 73 20 61 64 6a 75 73 74 65 64 0a 2a 2a 20 61  is adjusted.** a
24b07 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2f 0a 53  ccordingly..*/.S
24b08 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
24b09 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72 46  id sqlite3DeferF
24b0a 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65 20  oreignKey(Parse 
24b0b 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 73 44  *pParse, int isD
24b0c 65 66 65 72 72 65 64 29 7b 0a 23 69 66 6e 64 65  eferred){.#ifnde
24b0d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
24b0e 52 45 49 47 4e 5f 4b 45 59 0a 20 20 54 61 62 6c  REIGN_KEY.  Tabl
24b0f 65 20 2a 70 54 61 62 3b 0a 20 20 46 4b 65 79 20  e *pTab;.  FKey 
24b10 2a 70 46 4b 65 79 3b 0a 20 20 69 66 28 20 28 70  *pFKey;.  if( (p
24b11 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  Tab = pParse->pN
24b12 65 77 54 61 62 6c 65 29 3d 3d 30 20 7c 7c 20 28  ewTable)==0 || (
24b13 70 46 4b 65 79 20 3d 20 70 54 61 62 2d 3e 70 46  pFKey = pTab->pF
24b14 4b 65 79 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  Key)==0 ) return
24b15 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73 44 65  ;.  assert( isDe
24b16 66 65 72 72 65 64 3d 3d 30 20 7c 7c 20 69 73 44  ferred==0 || isD
24b17 65 66 65 72 72 65 64 3d 3d 31 20 29 3b 20 2f 2a  eferred==1 ); /*
24b18 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32 31 39   EV: R-30323-219
24b19 31 37 20 2a 2f 0a 20 20 70 46 4b 65 79 2d 3e 69  17 */.  pFKey->i
24b1a 73 44 65 66 65 72 72 65 64 20 3d 20 28 75 38 29  sDeferred = (u8)
24b1b 69 73 44 65 66 65 72 72 65 64 3b 0a 23 65 6e 64  isDeferred;.#end
24b1c 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  if.}../*.** Gene
24b1d 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
24b1e 69 6c 6c 20 65 72 61 73 65 20 61 6e 64 20 72 65  ill erase and re
24b1f 66 69 6c 6c 20 69 6e 64 65 78 20 2a 70 49 64 78  fill index *pIdx
24b20 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 75 73  .  This is.** us
24b21 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  ed to initialize
24b22 20 61 20 6e 65 77 6c 79 20 63 72 65 61 74 65 64   a newly created
24b23 20 69 6e 64 65 78 20 6f 72 20 74 6f 20 72 65 63   index or to rec
24b24 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 63 6f  ompute the.** co
24b25 6e 74 65 6e 74 20 6f 66 20 61 6e 20 69 6e 64 65  ntent of an inde
24b26 78 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f  x in response to
24b27 20 61 20 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61   a REINDEX comma
24b28 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66 20 6d 65 6d  nd..**.** if mem
24b29 52 6f 6f 74 50 61 67 65 20 69 73 20 6e 6f 74 20  RootPage is not 
24b2a 6e 65 67 61 74 69 76 65 2c 20 69 74 20 6d 65 61  negative, it mea
24b2b 6e 73 20 74 68 61 74 20 74 68 65 20 69 6e 64 65  ns that the inde
24b2c 78 20 69 73 20 6e 65 77 6c 79 0a 2a 2a 20 63 72  x is newly.** cr
24b2d 65 61 74 65 64 2e 20 20 54 68 65 20 72 65 67 69  eated.  The regi
24b2e 73 74 65 72 20 73 70 65 63 69 66 69 65 64 20 62  ster specified b
24b2f 79 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 63 6f  y memRootPage co
24b30 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 72 6f  ntains the.** ro
24b31 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
24b32 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 49 66  f the index.  If
24b33 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20   memRootPage is 
24b34 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 0a 2a  negative, then.*
24b35 2a 20 74 68 65 20 69 6e 64 65 78 20 61 6c 72 65  * the index alre
24b36 61 64 79 20 65 78 69 73 74 73 20 61 6e 64 20 6d  ady exists and m
24b37 75 73 74 20 62 65 20 63 6c 65 61 72 65 64 20 62  ust be cleared b
24b38 65 66 6f 72 65 20 62 65 69 6e 67 20 72 65 66 69  efore being refi
24b39 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20  lled and.** the 
24b3a 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
24b3b 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 69 73   of the index is
24b3c 20 74 61 6b 65 6e 20 66 72 6f 6d 20 70 49 6e 64   taken from pInd
24b3d 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f 0a 73 74 61  ex->tnum..*/.sta
24b3e 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
24b3f 52 65 66 69 6c 6c 49 6e 64 65 78 28 50 61 72 73  RefillIndex(Pars
24b40 65 20 2a 70 50 61 72 73 65 2c 20 49 6e 64 65 78  e *pParse, Index
24b41 20 2a 70 49 6e 64 65 78 2c 20 69 6e 74 20 6d 65   *pIndex, int me
24b42 6d 52 6f 6f 74 50 61 67 65 29 7b 0a 20 20 54 61  mRootPage){.  Ta
24b43 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64  ble *pTab = pInd
24b44 65 78 2d 3e 70 54 61 62 6c 65 3b 20 20 2f 2a 20  ex->pTable;  /* 
24b45 54 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69  The table that i
24b46 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69  s indexed */.  i
24b47 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 73 65  nt iTab = pParse
24b48 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 2f 2a  ->nTab++;     /*
24b49 20 42 74 72 65 65 20 63 75 72 73 6f 72 20 75 73   Btree cursor us
24b4a 65 64 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20  ed for pTab */. 
24b4b 20 69 6e 74 20 69 49 64 78 20 3d 20 70 50 61 72   int iIdx = pPar
24b4c 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20  se->nTab++;     
24b4d 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f 72 20  /* Btree cursor 
24b4e 75 73 65 64 20 66 6f 72 20 70 49 6e 64 65 78 20  used for pIndex 
24b4f 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 31 3b 20  */.  int addr1; 
24b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b51 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
24b52 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f  f top of loop */
24b53 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20  .  int tnum;    
24b54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b55 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f    /* Root page o
24b56 66 20 69 6e 64 65 78 20 2a 2f 0a 20 20 56 64 62  f index */.  Vdb
24b57 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
24b58 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47              /* G
24b59 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74  enerate code int
24b5a 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 6d  o this virtual m
24b5b 61 63 68 69 6e 65 20 2a 2f 0a 20 20 4b 65 79 49  achine */.  KeyI
24b5c 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20 20 20 20  nfo *pKey;      
24b5d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65             /* Ke
24b5e 79 49 6e 66 6f 20 66 6f 72 20 69 6e 64 65 78 20  yInfo for index 
24b5f 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 64 78 4b  */.  int regIdxK
24b60 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ey;             
24b61 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 73      /* Registers
24b62 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
24b63 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69  index key */.  i
24b64 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 20 20 20  nt regRecord;   
24b65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24b66 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
24b67 67 20 61 73 73 65 6d 62 6c 69 65 64 20 69 6e 64  g assemblied ind
24b68 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73  ex record */.  s
24b69 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
24b6a 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 2f 2a  rse->db;      /*
24b6b 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
24b6c 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
24b6d 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
24b6e 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
24b6f 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61   pIndex->pSchema
24b70 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
24b71 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
24b72 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69  ATION.  if( sqli
24b73 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
24b74 72 73 65 2c 20 53 51 4c 49 54 45 5f 52 45 49 4e  rse, SQLITE_REIN
24b75 44 45 58 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61  DEX, pIndex->zNa
24b76 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20 64 62 2d  me, 0,.      db-
24b77 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20  >aDb[iDb].zName 
24b78 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
24b79 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
24b7a 2a 20 52 65 71 75 69 72 65 20 61 20 77 72 69 74  * Require a writ
24b7b 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61  e-lock on the ta
24b7c 62 6c 65 20 74 6f 20 70 65 72 66 6f 72 6d 20 74  ble to perform t
24b7d 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f  his operation */
24b7e 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c  .  sqlite3TableL
24b7f 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ock(pParse, iDb,
24b80 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 31 2c 20   pTab->tnum, 1, 
24b81 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20  pTab->zName);.. 
24b82 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
24b83 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
24b84 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
24b85 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f 6f 74 50  ;.  if( memRootP
24b86 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 74 6e  age>=0 ){.    tn
24b87 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50 61 67 65  um = memRootPage
24b88 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 74  ;.  }else{.    t
24b89 6e 75 6d 20 3d 20 70 49 6e 64 65 78 2d 3e 74 6e  num = pIndex->tn
24b8a 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  um;.    sqlite3V
24b8b 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24b8c 43 6c 65 61 72 2c 20 74 6e 75 6d 2c 20 69 44 62  Clear, tnum, iDb
24b8d 29 3b 0a 20 20 7d 0a 20 20 70 4b 65 79 20 3d 20  );.  }.  pKey = 
24b8e 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
24b8f 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 6e 64  nfo(pParse, pInd
24b90 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ex);.  sqlite3Vd
24b91 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
24b92 70 65 6e 57 72 69 74 65 2c 20 69 49 64 78 2c 20  penWrite, iIdx, 
24b93 74 6e 75 6d 2c 20 69 44 62 2c 20 0a 20 20 20 20  tnum, iDb, .    
24b94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b95 28 63 68 61 72 20 2a 29 70 4b 65 79 2c 20 50 34  (char *)pKey, P4
24b96 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
24b97 29 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f 6f 74  );.  if( memRoot
24b98 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20 20 73  Page>=0 ){.    s
24b99 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
24b9a 50 35 28 76 2c 20 31 29 3b 0a 20 20 7d 0a 20 20  P5(v, 1);.  }.  
24b9b 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
24b9c 28 70 50 61 72 73 65 2c 20 69 54 61 62 2c 20 69  (pParse, iTab, i
24b9d 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65  Db, pTab, OP_Ope
24b9e 6e 52 65 61 64 29 3b 0a 20 20 61 64 64 72 31 20  nRead);.  addr1 
24b9f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
24ba0 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op2(v, OP_Rewind
24ba1 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20 72 65  , iTab, 0);.  re
24ba2 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 65  gRecord = sqlite
24ba3 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
24ba4 73 65 29 3b 0a 20 20 72 65 67 49 64 78 4b 65 79  se);.  regIdxKey
24ba5 20 3d 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61   = sqlite3Genera
24ba6 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73  teIndexKey(pPars
24ba7 65 2c 20 70 49 6e 64 65 78 2c 20 69 54 61 62 2c  e, pIndex, iTab,
24ba8 20 72 65 67 52 65 63 6f 72 64 2c 20 31 29 3b 0a   regRecord, 1);.
24ba9 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 6f 6e    if( pIndex->on
24baa 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29  Error!=OE_None )
24bab 7b 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20  {.    const int 
24bac 72 65 67 52 6f 77 69 64 20 3d 20 72 65 67 49 64  regRowid = regId
24bad 78 4b 65 79 20 2b 20 70 49 6e 64 65 78 2d 3e 6e  xKey + pIndex->n
24bae 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 63 6f 6e 73  Column;.    cons
24baf 74 20 69 6e 74 20 6a 32 20 3d 20 73 71 6c 69 74  t int j2 = sqlit
24bb0 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
24bb1 72 28 76 29 20 2b 20 32 3b 0a 20 20 20 20 76 6f  r(v) + 2;.    vo
24bb2 69 64 20 2a 20 63 6f 6e 73 74 20 70 52 65 67 4b  id * const pRegK
24bb3 65 79 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 5f  ey = SQLITE_INT_
24bb4 54 4f 5f 50 54 52 28 72 65 67 49 64 78 4b 65 79  TO_PTR(regIdxKey
24bb5 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72  );..    /* The r
24bb6 65 67 69 73 74 65 72 73 20 61 63 63 65 73 73 65  egisters accesse
24bb7 64 20 62 79 20 74 68 65 20 4f 50 5f 49 73 55 6e  d by the OP_IsUn
24bb8 69 71 75 65 20 6f 70 63 6f 64 65 20 77 65 72 65  ique opcode were
24bb9 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 2a   allocated.    *
24bba 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 47  * using sqlite3G
24bbb 65 74 54 65 6d 70 52 61 6e 67 65 28 29 20 69 6e  etTempRange() in
24bbc 73 69 64 65 20 6f 66 20 74 68 65 20 73 71 6c 69  side of the sqli
24bbd 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78  te3GenerateIndex
24bbe 4b 65 79 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c  Key().    ** cal
24bbf 6c 20 61 62 6f 76 65 2e 20 4a 75 73 74 20 62 65  l above. Just be
24bc0 66 6f 72 65 20 74 68 61 74 20 66 75 6e 63 74 69  fore that functi
24bc1 6f 6e 20 77 61 73 20 66 72 65 65 64 20 74 68 65  on was freed the
24bc2 79 20 77 65 72 65 20 72 65 6c 65 61 73 65 64 0a  y were released.
24bc3 20 20 20 20 2a 2a 20 28 6d 61 64 65 20 61 76 61      ** (made ava
24bc4 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 63 6f  ilable to the co
24bc5 6d 70 69 6c 65 72 20 66 6f 72 20 72 65 75 73 65  mpiler for reuse
24bc6 29 20 75 73 69 6e 67 20 0a 20 20 20 20 2a 2a 20  ) using .    ** 
24bc7 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
24bc8 6d 70 52 61 6e 67 65 28 29 2e 20 53 6f 20 69 6e  mpRange(). So in
24bc9 20 73 6f 6d 65 20 77 61 79 73 20 68 61 76 69 6e   some ways havin
24bca 67 20 74 68 65 20 4f 50 5f 49 73 55 6e 69 71 75  g the OP_IsUniqu
24bcb 65 0a 20 20 20 20 2a 2a 20 6f 70 63 6f 64 65 20  e.    ** opcode 
24bcc 75 73 65 20 74 68 65 20 76 61 6c 75 65 73 20 73  use the values s
24bcd 74 6f 72 65 64 20 77 69 74 68 69 6e 20 73 65 65  tored within see
24bce 6d 73 20 64 61 6e 67 65 72 6f 75 73 2e 20 48 6f  ms dangerous. Ho
24bcf 77 65 76 65 72 2c 20 73 69 6e 63 65 0a 20 20 20  wever, since.   
24bd0 20 2a 2a 20 77 65 20 63 61 6e 20 62 65 20 73 75   ** we can be su
24bd1 72 65 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72  re that no other
24bd2 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 73 20   temp registers 
24bd3 68 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61  have been alloca
24bd4 74 65 64 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65  ted.    ** since
24bd5 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
24bd6 65 6d 70 52 61 6e 67 65 28 29 20 77 61 73 20 63  empRange() was c
24bd7 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 73 61 66  alled, it is saf
24bd8 65 20 74 6f 20 64 6f 20 73 6f 2e 0a 20 20 20 20  e to do so..    
24bd9 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
24bda 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49  beAddOp4(v, OP_I
24bdb 73 55 6e 69 71 75 65 2c 20 69 49 64 78 2c 20 6a  sUnique, iIdx, j
24bdc 32 2c 20 72 65 67 52 6f 77 69 64 2c 20 70 52 65  2, regRowid, pRe
24bdd 67 4b 65 79 2c 20 50 34 5f 49 4e 54 33 32 29 3b  gKey, P4_INT32);
24bde 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74  .    sqlite3Halt
24bdf 43 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 20 20  Constraint(.    
24be0 20 20 20 20 70 50 61 72 73 65 2c 20 4f 45 5f 41      pParse, OE_A
24be1 62 6f 72 74 2c 20 22 69 6e 64 65 78 65 64 20 63  bort, "indexed c
24be2 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75  olumns are not u
24be3 6e 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49  nique", P4_STATI
24be4 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  C);.  }.  sqlite
24be5 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
24be6 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64  P_IdxInsert, iId
24be7 78 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20  x, regRecord);. 
24be8 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
24be9 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 55  geP5(v, OPFLAG_U
24bea 53 45 53 45 45 4b 52 45 53 55 4c 54 29 3b 0a 20  SESEEKRESULT);. 
24beb 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
24bec 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
24bed 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c  egRecord);.  sql
24bee 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24bef 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c  , OP_Next, iTab,
24bf0 20 61 64 64 72 31 2b 31 29 3b 0a 20 20 73 71 6c   addr1+1);.  sql
24bf1 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
24bf2 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 73 71  (v, addr1);.  sq
24bf3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
24bf4 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 54 61  v, OP_Close, iTa
24bf5 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  b);.  sqlite3Vdb
24bf6 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
24bf7 6f 73 65 2c 20 69 49 64 78 29 3b 0a 7d 0a 0a 2f  ose, iIdx);.}../
24bf8 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
24bf9 77 20 69 6e 64 65 78 20 66 6f 72 20 61 6e 20 53  w index for an S
24bfa 51 4c 20 74 61 62 6c 65 2e 20 20 70 4e 61 6d 65  QL table.  pName
24bfb 31 2e 70 4e 61 6d 65 32 20 69 73 20 74 68 65 20  1.pName2 is the 
24bfc 6e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65  name of the inde
24bfd 78 20 0a 2a 2a 20 61 6e 64 20 70 54 62 6c 4c 69  x .** and pTblLi
24bfe 73 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  st is the name o
24bff 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
24c00 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65   is to be indexe
24c01 64 2e 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a  d.  Both will .*
24c02 2a 20 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20  * be NULL for a 
24c03 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 61  primary key or a
24c04 6e 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 20  n index that is 
24c05 63 72 65 61 74 65 64 20 74 6f 20 73 61 74 69 73  created to satis
24c06 66 79 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 63  fy a.** UNIQUE c
24c07 6f 6e 73 74 72 61 69 6e 74 2e 20 20 49 66 20 70  onstraint.  If p
24c08 54 61 62 6c 65 20 61 6e 64 20 70 49 6e 64 65 78  Table and pIndex
24c09 20 61 72 65 20 4e 55 4c 4c 2c 20 75 73 65 20 70   are NULL, use p
24c0a 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
24c0b 0a 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65  .** as the table
24c0c 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20   to be indexed. 
24c0d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
24c0e 6c 65 20 69 73 20 61 20 74 61 62 6c 65 20 74 68  le is a table th
24c0f 61 74 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74  at is.** current
24c10 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75  ly being constru
24c11 63 74 65 64 20 62 79 20 61 20 43 52 45 41 54 45  cted by a CREATE
24c12 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
24c13 2e 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73  ..**.** pList is
24c14 20 61 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d   a list of colum
24c15 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  ns to be indexed
24c16 2e 20 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 65  .  pList will be
24c17 20 4e 55 4c 4c 20 69 66 20 74 68 69 73 0a 2a 2a   NULL if this.**
24c18 20 69 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65   is a primary ke
24c19 79 20 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e 73  y or unique-cons
24c1a 74 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d 6f  traint on the mo
24c1b 73 74 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d 6e  st recent column
24c1c 20 61 64 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65   added.** to the
24c1d 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79   table currently
24c1e 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
24c1f 69 6f 6e 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20  ion.  .**.** If 
24c20 74 68 65 20 69 6e 64 65 78 20 69 73 20 63 72 65  the index is cre
24c21 61 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c  ated successfull
24c22 79 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  y, return a poin
24c23 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 49  ter to the new I
24c24 6e 64 65 78 0a 2a 2a 20 73 74 72 75 63 74 75 72  ndex.** structur
24c25 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  e. This is used 
24c26 62 79 20 73 71 6c 69 74 65 33 41 64 64 50 72 69  by sqlite3AddPri
24c27 6d 61 72 79 4b 65 79 28 29 20 74 6f 20 6d 61 72  maryKey() to mar
24c28 6b 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20 61  k the index.** a
24c29 73 20 74 68 65 20 74 61 62 6c 65 73 20 70 72 69  s the tables pri
24c2a 6d 61 72 79 20 6b 65 79 20 28 49 6e 64 65 78 2e  mary key (Index.
24c2b 61 75 74 6f 49 6e 64 65 78 3d 3d 32 29 2e 0a 2a  autoIndex==2)..*
24c2c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24c2d 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 43   Index *sqlite3C
24c2e 72 65 61 74 65 49 6e 64 65 78 28 0a 20 20 50 61  reateIndex(.  Pa
24c2f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
24c30 20 2f 2a 20 41 6c 6c 20 69 6e 66 6f 72 6d 61 74   /* All informat
24c31 69 6f 6e 20 61 62 6f 75 74 20 74 68 69 73 20 70  ion about this p
24c32 61 72 73 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  arse */.  Token 
24c33 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20  *pName1,     /* 
24c34 46 69 72 73 74 20 70 61 72 74 20 6f 66 20 69 6e  First part of in
24c35 64 65 78 20 6e 61 6d 65 2e 20 4d 61 79 20 62 65  dex name. May be
24c36 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e   NULL */.  Token
24c37 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 2f 2a   *pName2,     /*
24c38 20 53 65 63 6f 6e 64 20 70 61 72 74 20 6f 66 20   Second part of 
24c39 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d 61 79 20  index name. May 
24c3a 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 53 72 63  be NULL */.  Src
24c3b 4c 69 73 74 20 2a 70 54 62 6c 4e 61 6d 65 2c 20  List *pTblName, 
24c3c 2f 2a 20 54 61 62 6c 65 20 74 6f 20 69 6e 64 65  /* Table to inde
24c3d 78 2e 20 55 73 65 20 70 50 61 72 73 65 2d 3e 70  x. Use pParse->p
24c3e 4e 65 77 54 61 62 6c 65 20 69 66 20 30 20 2a 2f  NewTable if 0 */
24c3f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
24c40 73 74 2c 20 20 20 2f 2a 20 41 20 6c 69 73 74 20  st,   /* A list 
24c41 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65  of columns to be
24c42 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e   indexed */.  in
24c43 74 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20  t onError,      
24c44 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 2c 20 4f 45   /* OE_Abort, OE
24c45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f 52 65 70 6c  _Ignore, OE_Repl
24c46 61 63 65 2c 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20  ace, or OE_None 
24c47 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 53 74 61  */.  Token *pSta
24c48 72 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 43  rt,     /* The C
24c49 52 45 41 54 45 20 74 6f 6b 65 6e 20 74 68 61 74  REATE token that
24c4a 20 62 65 67 69 6e 73 20 74 68 69 73 20 73 74 61   begins this sta
24c4b 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65  tement */.  Toke
24c4c 6e 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 2f  n *pEnd,       /
24c4d 2a 20 54 68 65 20 22 29 22 20 74 68 61 74 20 63  * The ")" that c
24c4e 6c 6f 73 65 73 20 74 68 65 20 43 52 45 41 54 45  loses the CREATE
24c4f 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
24c50 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72   */.  int sortOr
24c51 64 65 72 2c 20 20 20 20 20 2f 2a 20 53 6f 72 74  der,     /* Sort
24c52 20 6f 72 64 65 72 20 6f 66 20 70 72 69 6d 61 72   order of primar
24c53 79 20 6b 65 79 20 77 68 65 6e 20 70 4c 69 73 74  y key when pList
24c54 3d 3d 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20  ==NULL */.  int 
24c55 69 66 4e 6f 74 45 78 69 73 74 20 20 20 20 20 2f  ifNotExist     /
24c56 2a 20 4f 6d 69 74 20 65 72 72 6f 72 20 69 66 20  * Omit error if 
24c57 69 6e 64 65 78 20 61 6c 72 65 61 64 79 20 65 78  index already ex
24c58 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 49 6e 64  ists */.){.  Ind
24c59 65 78 20 2a 70 52 65 74 20 3d 20 30 3b 20 20 20  ex *pRet = 0;   
24c5a 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
24c5b 72 65 74 75 72 6e 20 2a 2f 0a 20 20 54 61 62 6c  return */.  Tabl
24c5c 65 20 2a 70 54 61 62 20 3d 20 30 3b 20 20 20 20  e *pTab = 0;    
24c5d 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20   /* Table to be 
24c5e 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 49 6e 64  indexed */.  Ind
24c5f 65 78 20 2a 70 49 6e 64 65 78 20 3d 20 30 3b 20  ex *pIndex = 0; 
24c60 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 74    /* The index t
24c61 6f 20 62 65 20 63 72 65 61 74 65 64 20 2a 2f 0a  o be created */.
24c62 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20    char *zName = 
24c63 30 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  0;     /* Name o
24c64 66 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20  f the index */. 
24c65 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20   int nName;     
24c66 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
24c67 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  of characters in
24c68 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20   zName */.  int 
24c69 69 2c 20 6a 3b 0a 20 20 54 6f 6b 65 6e 20 6e 75  i, j;.  Token nu
24c6a 6c 6c 49 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  llId;        /* 
24c6b 46 61 6b 65 20 74 6f 6b 65 6e 20 66 6f 72 20 61  Fake token for a
24c6c 6e 20 65 6d 70 74 79 20 49 44 20 6c 69 73 74 20  n empty ID list 
24c6d 2a 2f 0a 20 20 44 62 46 69 78 65 72 20 73 46 69  */.  DbFixer sFi
24c6e 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72  x;        /* For
24c6f 20 61 73 73 69 67 6e 69 6e 67 20 64 61 74 61 62   assigning datab
24c70 61 73 65 20 6e 61 6d 65 73 20 74 6f 20 70 54 61  ase names to pTa
24c71 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72  ble */.  int sor
24c72 74 4f 72 64 65 72 4d 61 73 6b 3b 20 20 20 2f 2a  tOrderMask;   /*
24c73 20 31 20 74 6f 20 68 6f 6e 6f 72 20 44 45 53 43   1 to honor DESC
24c74 20 69 6e 20 69 6e 64 65 78 2e 20 20 30 20 74 6f   in index.  0 to
24c75 20 69 67 6e 6f 72 65 2e 20 2a 2f 0a 20 20 73 71   ignore. */.  sq
24c76 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
24c77 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44  se->db;.  Db *pD
24c78 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
24c79 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20 74  * The specific t
24c7a 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
24c7b 74 68 65 20 69 6e 64 65 78 65 64 20 64 61 74 61  the indexed data
24c7c 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44  base */.  int iD
24c7d 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b;             /
24c7e 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * Index of the d
24c7f 61 74 61 62 61 73 65 20 74 68 61 74 20 69 73 20  atabase that is 
24c80 62 65 69 6e 67 20 77 72 69 74 74 65 6e 20 2a 2f  being written */
24c81 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20  .  Token *pName 
24c82 3d 20 30 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61  = 0;    /* Unqua
24c83 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f 66 20 74  lified name of t
24c84 68 65 20 69 6e 64 65 78 20 74 6f 20 63 72 65 61  he index to crea
24c85 74 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45  te */.  struct E
24c86 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c  xprList_item *pL
24c87 69 73 74 49 74 65 6d 3b 20 2f 2a 20 46 6f 72 20  istItem; /* For 
24c88 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 4c 69  looping over pLi
24c89 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  st */.  int nCol
24c8a 3b 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 20 3d  ;.  int nExtra =
24c8b 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 45 78 74   0;.  char *zExt
24c8c 72 61 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  ra;..  assert( p
24c8d 53 74 61 72 74 3d 3d 30 20 7c 7c 20 70 45 6e 64  Start==0 || pEnd
24c8e 21 3d 30 20 29 3b 20 2f 2a 20 70 45 6e 64 20 6d  !=0 ); /* pEnd m
24c8f 75 73 74 20 62 65 20 6e 6f 6e 2d 4e 55 4c 4c 20  ust be non-NULL 
24c90 69 66 20 70 53 74 61 72 74 20 69 73 20 2a 2f 0a  if pStart is */.
24c91 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
24c92 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 20 20 20 20  ->nErr==0 );    
24c93 20 20 2f 2a 20 4e 65 76 65 72 20 63 61 6c 6c 65    /* Never calle
24c94 64 20 77 69 74 68 20 70 72 69 6f 72 20 65 72 72  d with prior err
24c95 6f 72 73 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ors */.  if( db-
24c96 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
24c97 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42   IN_DECLARE_VTAB
24c98 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   ){.    goto exi
24c99 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
24c9a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45    }.  if( SQLITE
24c9b 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
24c9c 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
24c9d 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
24c9e 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
24c9f 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e  }..  /*.  ** Fin
24ca0 64 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  d the table that
24ca1 20 69 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65   is to be indexe
24ca2 64 2e 20 20 52 65 74 75 72 6e 20 65 61 72 6c 79  d.  Return early
24ca3 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20   if not found.. 
24ca4 20 2a 2f 0a 20 20 69 66 28 20 70 54 62 6c 4e 61   */.  if( pTblNa
24ca5 6d 65 21 3d 30 20 29 7b 0a 0a 20 20 20 20 2f 2a  me!=0 ){..    /*
24ca6 20 55 73 65 20 74 68 65 20 74 77 6f 2d 70 61 72   Use the two-par
24ca7 74 20 69 6e 64 65 78 20 6e 61 6d 65 20 74 6f 20  t index name to 
24ca8 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 61  determine the da
24ca9 74 61 62 61 73 65 20 0a 20 20 20 20 2a 2a 20 74  tabase .    ** t
24caa 6f 20 73 65 61 72 63 68 20 66 6f 72 20 74 68 65  o search for the
24cab 20 74 61 62 6c 65 2e 20 27 46 69 78 27 20 74 68   table. 'Fix' th
24cac 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 74 6f 20  e table name to 
24cad 74 68 69 73 20 64 62 0a 20 20 20 20 2a 2a 20 62  this db.    ** b
24cae 65 66 6f 72 65 20 6c 6f 6f 6b 69 6e 67 20 75 70  efore looking up
24caf 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20   the table..    
24cb0 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
24cb1 4e 61 6d 65 31 20 26 26 20 70 4e 61 6d 65 32 20  Name1 && pName2 
24cb2 29 3b 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c  );.    iDb = sql
24cb3 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28  ite3TwoPartName(
24cb4 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20  pParse, pName1, 
24cb5 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b  pName2, &pName);
24cb6 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20 29  .    if( iDb<0 )
24cb7 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
24cb8 65 5f 69 6e 64 65 78 3b 0a 0a 23 69 66 6e 64 65  e_index;..#ifnde
24cb9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45  f SQLITE_OMIT_TE
24cba 4d 50 44 42 0a 20 20 20 20 2f 2a 20 49 66 20 74  MPDB.    /* If t
24cbb 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 20 77 61  he index name wa
24cbc 73 20 75 6e 71 75 61 6c 69 66 69 65 64 2c 20 63  s unqualified, c
24cbd 68 65 63 6b 20 69 66 20 74 68 65 20 74 68 65 20  heck if the the 
24cbe 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20  table.    ** is 
24cbf 61 20 74 65 6d 70 20 74 61 62 6c 65 2e 20 49 66  a temp table. If
24cc0 20 73 6f 2c 20 73 65 74 20 74 68 65 20 64 61 74   so, set the dat
24cc1 61 62 61 73 65 20 74 6f 20 31 2e 20 44 6f 20 6e  abase to 1. Do n
24cc2 6f 74 20 64 6f 20 74 68 69 73 0a 20 20 20 20 2a  ot do this.    *
24cc3 2a 20 69 66 20 69 6e 69 74 69 61 6c 69 73 69 6e  * if initialisin
24cc4 67 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68  g a database sch
24cc5 65 6d 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ema..    */.    
24cc6 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75  if( !db->init.bu
24cc7 73 79 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62  sy ){.      pTab
24cc8 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
24cc9 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20  tLookup(pParse, 
24cca 70 54 62 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20  pTblName);.     
24ccb 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d   if( pName2->n==
24ccc 30 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61  0 && pTab && pTa
24ccd 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e  b->pSchema==db->
24cce 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29  aDb[1].pSchema )
24ccf 7b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20  {.        iDb = 
24cd0 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
24cd1 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28  .#endif..    if(
24cd2 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28   sqlite3FixInit(
24cd3 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69  &sFix, pParse, i
24cd4 44 62 2c 20 22 69 6e 64 65 78 22 2c 20 70 4e 61  Db, "index", pNa
24cd5 6d 65 29 20 26 26 0a 20 20 20 20 20 20 20 20 73  me) &&.        s
24cd6 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74  qlite3FixSrcList
24cd7 28 26 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d 65  (&sFix, pTblName
24cd8 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f  ).    ){.      /
24cd9 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 70 61  * Because the pa
24cda 72 73 65 72 20 63 6f 6e 73 74 72 75 63 74 73 20  rser constructs 
24cdb 70 54 62 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 20  pTblName from a 
24cdc 73 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65  single identifie
24cdd 72 2c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  r,.      ** sqli
24cde 74 65 33 46 69 78 53 72 63 4c 69 73 74 20 63 61  te3FixSrcList ca
24cdf 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 2a 2f  n never fail. */
24ce0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29  .      assert(0)
24ce1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62  ;.    }.    pTab
24ce2 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
24ce3 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
24ce4 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e   pTblName->a[0].
24ce5 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 20  zName, .        
24ce6 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a  pTblName->a[0].z
24ce7 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 69  Database);.    i
24ce8 66 28 20 21 70 54 61 62 20 7c 7c 20 64 62 2d 3e  f( !pTab || db->
24ce9 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
24cea 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
24ceb 69 6e 64 65 78 3b 0a 20 20 20 20 61 73 73 65 72  index;.    asser
24cec 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t( db->aDb[iDb].
24ced 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70  pSchema==pTab->p
24cee 53 63 68 65 6d 61 20 29 3b 0a 20 20 7d 65 6c 73  Schema );.  }els
24cef 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
24cf0 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70  Name==0 );.    p
24cf1 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  Tab = pParse->pN
24cf2 65 77 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 28  ewTable;.    if(
24cf3 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78   !pTab ) goto ex
24cf4 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
24cf5 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
24cf6 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
24cf7 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
24cf8 61 29 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d 20  a);.  }.  pDb = 
24cf9 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a  &db->aDb[iDb];..
24cfa 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d    assert( pTab!=
24cfb 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
24cfc 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29  Parse->nErr==0 )
24cfd 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
24cfe 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e  trNICmp(pTab->zN
24cff 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20  ame, "sqlite_", 
24d00 37 29 3d 3d 30 20 0a 20 20 20 20 20 20 20 26 26  7)==0 .       &&
24d01 20 6d 65 6d 63 6d 70 28 26 70 54 61 62 2d 3e 7a   memcmp(&pTab->z
24d02 4e 61 6d 65 5b 37 5d 2c 22 61 6c 74 65 72 74 61  Name[7],"alterta
24d03 62 5f 22 2c 39 29 21 3d 30 20 29 7b 0a 20 20 20  b_",9)!=0 ){.   
24d04 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
24d05 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
24d06 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e  %s may not be in
24d07 64 65 78 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e  dexed", pTab->zN
24d08 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ame);.    goto e
24d09 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24d0a 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  ;.  }.#ifndef SQ
24d0b 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20  LITE_OMIT_VIEW. 
24d0c 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65   if( pTab->pSele
24d0d 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ct ){.    sqlite
24d0e 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
24d0f 2c 20 22 76 69 65 77 73 20 6d 61 79 20 6e 6f 74  , "views may not
24d10 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b 0a 20   be indexed");. 
24d11 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
24d12 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23  ate_index;.  }.#
24d13 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51  endif.#ifndef SQ
24d14 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
24d15 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49 73 56  LTABLE.  if( IsV
24d16 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
24d17 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24d18 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76 69 72  Msg(pParse, "vir
24d19 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 61 79 20  tual tables may 
24d1a 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 29  not be indexed")
24d1b 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
24d1c 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
24d1d 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20  }.#endif..  /*. 
24d1e 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e 61 6d   ** Find the nam
24d1f 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20  e of the index. 
24d20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 72 65   Make sure there
24d21 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
24d22 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20 69 6e 64  another.  ** ind
24d23 65 78 20 6f 72 20 74 61 62 6c 65 20 77 69 74 68  ex or table with
24d24 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20   the same name. 
24d25 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65   .  **.  ** Exce
24d26 70 74 69 6f 6e 3a 20 20 49 66 20 77 65 20 61 72  ption:  If we ar
24d27 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6e 61  e reading the na
24d28 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e 65 6e 74  mes of permanent
24d29 20 69 6e 64 69 63 65 73 20 66 72 6f 6d 20 74 68   indices from th
24d2a 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61  e.  ** sqlite_ma
24d2b 73 74 65 72 20 74 61 62 6c 65 20 28 62 65 63 61  ster table (beca
24d2c 75 73 65 20 73 6f 6d 65 20 6f 74 68 65 72 20 70  use some other p
24d2d 72 6f 63 65 73 73 20 63 68 61 6e 67 65 64 20 74  rocess changed t
24d2e 68 65 20 73 63 68 65 6d 61 29 20 61 6e 64 0a 20  he schema) and. 
24d2f 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65 20 69   ** one of the i
24d30 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f 6c 6c 69  ndex names colli
24d31 64 65 73 20 77 69 74 68 20 74 68 65 20 6e 61 6d  des with the nam
24d32 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61 72 79  e of a temporary
24d33 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a 2a 20 69   table or.  ** i
24d34 6e 64 65 78 2c 20 74 68 65 6e 20 77 65 20 77 69  ndex, then we wi
24d35 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 70  ll continue to p
24d36 72 6f 63 65 73 73 20 74 68 69 73 20 69 6e 64 65  rocess this inde
24d37 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  x..  **.  ** If 
24d38 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65 61 6e  pName==0 it mean
24d39 73 20 74 68 61 74 20 77 65 20 61 72 65 0a 20 20  s that we are.  
24d3a 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20  ** dealing with 
24d3b 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 72  a primary key or
24d3c 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69   UNIQUE constrai
24d3d 6e 74 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  nt.  We have to 
24d3e 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20 2a 2a 20  invent our.  ** 
24d3f 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20  own name..  */. 
24d40 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20   if( pName ){.  
24d41 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
24d42 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
24d43 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69  b, pName);.    i
24d44 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 67 6f  f( zName==0 ) go
24d45 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24d46 6e 64 65 78 3b 0a 20 20 20 20 69 66 28 20 53 51  ndex;.    if( SQ
24d47 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
24d48 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28  CheckObjectName(
24d49 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29  pParse, zName) )
24d4a 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
24d4b 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
24d4c 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64      }.    if( !d
24d4d 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a  b->init.busy ){.
24d4e 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
24d4f 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
24d50 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29 7b 0a 20  Name, 0)!=0 ){. 
24d51 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
24d52 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
24d53 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79  there is already
24d54 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 25   a table named %
24d55 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
24d56 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72      goto exit_cr
24d57 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20  eate_index;.    
24d58 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
24d59 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64  ( sqlite3FindInd
24d5a 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 44  ex(db, zName, pD
24d5b 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 29 7b 0a  b->zName)!=0 ){.
24d5c 20 20 20 20 20 20 69 66 28 20 21 69 66 4e 6f 74        if( !ifNot
24d5d 45 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20  Exist ){.       
24d5e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
24d5f 28 70 50 61 72 73 65 2c 20 22 69 6e 64 65 78 20  (pParse, "index 
24d60 25 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  %s already exist
24d61 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
24d62 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65    }.      goto e
24d63 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24d64 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
24d65 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20  .    int n;.    
24d66 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 3b 0a 20 20  Index *pLoop;.  
24d67 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 54 61 62    for(pLoop=pTab
24d68 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d 31 3b 20 70  ->pIndex, n=1; p
24d69 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f  Loop; pLoop=pLoo
24d6a 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b 2b 29 7b 7d  p->pNext, n++){}
24d6b 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c  .    zName = sql
24d6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
24d6d 22 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65  "sqlite_autoinde
24d6e 78 5f 25 73 5f 25 64 22 2c 20 70 54 61 62 2d 3e  x_%s_%d", pTab->
24d6f 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 69  zName, n);.    i
24d70 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20  f( zName==0 ){. 
24d71 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
24d72 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
24d73 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65   }.  }..  /* Che
24d74 63 6b 20 66 6f 72 20 61 75 74 68 6f 72 69 7a 61  ck for authoriza
24d75 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20 61  tion to create a
24d76 6e 20 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a 23 69  n index..  */.#i
24d77 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
24d78 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
24d79 20 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68    {.    const ch
24d7a 61 72 20 2a 7a 44 62 20 3d 20 70 44 62 2d 3e 7a  ar *zDb = pDb->z
24d7b 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20 73 71  Name;.    if( sq
24d7c 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
24d7d 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e  Parse, SQLITE_IN
24d7e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54 41 42  SERT, SCHEMA_TAB
24d7f 4c 45 28 69 44 62 29 2c 20 30 2c 20 7a 44 62 29  LE(iDb), 0, zDb)
24d80 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   ){.      goto e
24d81 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24d82 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20 3d 20  ;.    }.    i = 
24d83 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e  SQLITE_CREATE_IN
24d84 44 45 58 3b 0a 20 20 20 20 69 66 28 20 21 4f 4d  DEX;.    if( !OM
24d85 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62  IT_TEMPDB && iDb
24d86 3d 3d 31 20 29 20 69 20 3d 20 53 51 4c 49 54 45  ==1 ) i = SQLITE
24d87 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44  _CREATE_TEMP_IND
24d88 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  EX;.    if( sqli
24d89 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
24d8a 72 73 65 2c 20 69 2c 20 7a 4e 61 6d 65 2c 20 70  rse, i, zName, p
24d8b 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29  Tab->zName, zDb)
24d8c 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   ){.      goto e
24d8d 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24d8e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
24d8f 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 4c 69 73  if..  /* If pLis
24d90 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e 73 20 74  t==0, it means t
24d91 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73 20  his routine was 
24d92 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b 65 20 61  called to make a
24d93 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65   primary.  ** ke
24d94 79 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 61 73  y out of the las
24d95 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64 20 74  t column added t
24d96 6f 20 74 68 65 20 74 61 62 6c 65 20 75 6e 64 65  o the table unde
24d97 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  r construction..
24d98 20 20 2a 2a 20 53 6f 20 63 72 65 61 74 65 20 61    ** So create a
24d99 20 66 61 6b 65 20 6c 69 73 74 20 74 6f 20 73 69   fake list to si
24d9a 6d 75 6c 61 74 65 20 74 68 69 73 2e 0a 20 20 2a  mulate this..  *
24d9b 2f 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  /.  if( pList==0
24d9c 20 29 7b 0a 20 20 20 20 6e 75 6c 6c 49 64 2e 7a   ){.    nullId.z
24d9d 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54   = pTab->aCol[pT
24d9e 61 62 2d 3e 6e 43 6f 6c 2d 31 5d 2e 7a 4e 61 6d  ab->nCol-1].zNam
24d9f 65 3b 0a 20 20 20 20 6e 75 6c 6c 49 64 2e 6e 20  e;.    nullId.n 
24da0 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
24da1 30 28 28 63 68 61 72 2a 29 6e 75 6c 6c 49 64 2e  0((char*)nullId.
24da2 7a 29 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20  z);.    pList = 
24da3 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
24da4 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c  ppend(pParse, 0,
24da5 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69   0);.    if( pLi
24da6 73 74 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69  st==0 ) goto exi
24da7 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
24da8 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c      sqlite3ExprL
24da9 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73  istSetName(pPars
24daa 65 2c 20 70 4c 69 73 74 2c 20 26 6e 75 6c 6c 49  e, pList, &nullI
24dab 64 2c 20 30 29 3b 0a 20 20 20 20 70 4c 69 73 74  d, 0);.    pList
24dac 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72  ->a[0].sortOrder
24dad 20 3d 20 28 75 38 29 73 6f 72 74 4f 72 64 65 72   = (u8)sortOrder
24dae 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75  ;.  }..  /* Figu
24daf 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
24db0 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61  bytes of space a
24db1 72 65 20 72 65 71 75 69 72 65 64 20 74 6f 20 73  re required to s
24db2 74 6f 72 65 20 65 78 70 6c 69 63 69 74 6c 79 0a  tore explicitly.
24db3 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 63    ** specified c
24db4 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
24db5 65 20 6e 61 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20  e names..  */.  
24db6 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74  for(i=0; i<pList
24db7 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
24db8 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d     Expr *pExpr =
24db9 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
24dba 70 72 3b 0a 20 20 20 20 69 66 28 20 70 45 78 70  pr;.    if( pExp
24dbb 72 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53  r ){.      CollS
24dbc 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 45 78 70  eq *pColl = pExp
24dbd 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20  r->pColl;.      
24dbe 2f 2a 20 45 69 74 68 65 72 20 70 43 6f 6c 6c 21  /* Either pColl!
24dbf 3d 30 20 6f 72 20 74 68 65 72 65 20 77 61 73 20  =0 or there was 
24dc0 61 6e 20 4f 4f 4d 20 66 61 69 6c 75 72 65 2e 20  an OOM failure. 
24dc1 20 42 75 74 20 69 66 20 61 6e 20 4f 4f 4d 0a 20   But if an OOM. 
24dc2 20 20 20 20 20 2a 2a 20 66 61 69 6c 75 72 65 20       ** failure 
24dc3 77 65 20 68 61 76 65 20 71 75 69 74 20 62 65 66  we have quit bef
24dc4 6f 72 65 20 72 65 61 63 68 69 6e 67 20 74 68 69  ore reaching thi
24dc5 73 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20  s point. */.    
24dc6 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 43 6f    if( ALWAYS(pCo
24dc7 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  ll) ){.        n
24dc8 45 78 74 72 61 20 2b 3d 20 28 31 20 2b 20 73 71  Extra += (1 + sq
24dc9 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 43  lite3Strlen30(pC
24dca 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20  oll->zName));.  
24dcb 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
24dcc 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f  .  /* .  ** Allo
24dcd 63 61 74 65 20 74 68 65 20 69 6e 64 65 78 20 73  cate the index s
24dce 74 72 75 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a  tructure. .  */.
24dcf 20 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    nName = sqlite
24dd0 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
24dd1 3b 0a 20 20 6e 43 6f 6c 20 3d 20 70 4c 69 73 74  ;.  nCol = pList
24dd2 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 64 65  ->nExpr;.  pInde
24dd3 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  x = sqlite3DbMal
24dd4 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20 20 20  locZero(db, .   
24dd5 20 20 20 73 69 7a 65 6f 66 28 49 6e 64 65 78 29     sizeof(Index)
24dd6 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   +              
24dd7 2f 2a 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  /* Index structu
24dd8 72 65 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a  re  */.      siz
24dd9 65 6f 66 28 69 6e 74 29 2a 6e 43 6f 6c 20 2b 20  eof(int)*nCol + 
24dda 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
24ddb 65 78 2e 61 69 43 6f 6c 75 6d 6e 20 20 20 2a 2f  ex.aiColumn   */
24ddc 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 69 6e  .      sizeof(in
24ddd 74 29 2a 28 6e 43 6f 6c 2b 31 29 20 2b 20 20 20  t)*(nCol+1) +   
24dde 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 69 52      /* Index.aiR
24ddf 6f 77 45 73 74 20 20 20 2a 2f 0a 20 20 20 20 20  owEst   */.     
24de0 20 73 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a   sizeof(char *)*
24de1 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20 20 2f 2a  nCol +        /*
24de2 20 49 6e 64 65 78 2e 61 7a 43 6f 6c 6c 20 20 20   Index.azColl   
24de3 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f    */.      sizeo
24de4 66 28 75 38 29 2a 6e 43 6f 6c 20 2b 20 20 20 20  f(u8)*nCol +    
24de5 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
24de6 2e 61 53 6f 72 74 4f 72 64 65 72 20 2a 2f 0a 20  .aSortOrder */. 
24de7 20 20 20 20 20 6e 4e 61 6d 65 20 2b 20 31 20 2b       nName + 1 +
24de8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24de9 20 20 2f 2a 20 49 6e 64 65 78 2e 7a 4e 61 6d 65    /* Index.zName
24dea 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6e        */.      n
24deb 45 78 74 72 61 20 20 20 20 20 20 20 20 20 20 20  Extra           
24dec 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
24ded 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
24dee 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 29 3b 0a  e names */.  );.
24def 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
24df0 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
24df1 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24df2 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70 49 6e 64  ndex;.  }.  pInd
24df3 65 78 2d 3e 61 7a 43 6f 6c 6c 20 3d 20 28 63 68  ex->azColl = (ch
24df4 61 72 2a 2a 29 28 26 70 49 6e 64 65 78 5b 31 5d  ar**)(&pIndex[1]
24df5 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 69 43  );.  pIndex->aiC
24df6 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74 20 2a 29 28  olumn = (int *)(
24df7 26 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b  &pIndex->azColl[
24df8 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78  nCol]);.  pIndex
24df9 2d 3e 61 69 52 6f 77 45 73 74 20 3d 20 28 75 6e  ->aiRowEst = (un
24dfa 73 69 67 6e 65 64 20 2a 29 28 26 70 49 6e 64 65  signed *)(&pInde
24dfb 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 43 6f 6c  x->aiColumn[nCol
24dfc 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61 53  ]);.  pIndex->aS
24dfd 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 20 2a  ortOrder = (u8 *
24dfe 29 28 26 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77  )(&pIndex->aiRow
24dff 45 73 74 5b 6e 43 6f 6c 2b 31 5d 29 3b 0a 20 20  Est[nCol+1]);.  
24e00 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 20 3d 20  pIndex->zName = 
24e01 28 63 68 61 72 20 2a 29 28 26 70 49 6e 64 65 78  (char *)(&pIndex
24e02 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e 43 6f  ->aSortOrder[nCo
24e03 6c 5d 29 3b 0a 20 20 7a 45 78 74 72 61 20 3d 20  l]);.  zExtra = 
24e04 28 63 68 61 72 20 2a 29 28 26 70 49 6e 64 65 78  (char *)(&pIndex
24e05 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 2b 31 5d  ->zName[nName+1]
24e06 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 49 6e 64  );.  memcpy(pInd
24e07 65 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  ex->zName, zName
24e08 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 70 49  , nName+1);.  pI
24e09 6e 64 65 78 2d 3e 70 54 61 62 6c 65 20 3d 20 70  ndex->pTable = p
24e0a 54 61 62 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6e  Tab;.  pIndex->n
24e0b 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73 74 2d 3e  Column = pList->
24e0c 6e 45 78 70 72 3b 0a 20 20 70 49 6e 64 65 78 2d  nExpr;.  pIndex-
24e0d 3e 6f 6e 45 72 72 6f 72 20 3d 20 28 75 38 29 6f  >onError = (u8)o
24e0e 6e 45 72 72 6f 72 3b 0a 20 20 70 49 6e 64 65 78  nError;.  pIndex
24e0f 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d 20 28 75  ->autoIndex = (u
24e10 38 29 28 70 4e 61 6d 65 3d 3d 30 29 3b 0a 20 20  8)(pName==0);.  
24e11 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 20  pIndex->pSchema 
24e12 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
24e13 53 63 68 65 6d 61 3b 0a 0a 20 20 2f 2a 20 43 68  Schema;..  /* Ch
24e14 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77 65  eck to see if we
24e15 20 73 68 6f 75 6c 64 20 68 6f 6e 6f 72 20 44 45   should honor DE
24e16 53 43 20 72 65 71 75 65 73 74 73 20 6f 6e 20 69  SC requests on i
24e17 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 0a 20 20 2a  ndex columns.  *
24e18 2f 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63  /.  if( pDb->pSc
24e19 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61  hema->file_forma
24e1a 74 3e 3d 34 20 29 7b 0a 20 20 20 20 73 6f 72 74  t>=4 ){.    sort
24e1b 4f 72 64 65 72 4d 61 73 6b 20 3d 20 2d 31 3b 20  OrderMask = -1; 
24e1c 20 20 2f 2a 20 48 6f 6e 6f 72 20 44 45 53 43 20    /* Honor DESC 
24e1d 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  */.  }else{.    
24e1e 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20  sortOrderMask = 
24e1f 30 3b 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20  0;    /* Ignore 
24e20 44 45 53 43 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f  DESC */.  }..  /
24e21 2a 20 53 63 61 6e 20 74 68 65 20 6e 61 6d 65 73  * Scan the names
24e22 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20   of the columns 
24e23 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
24e24 62 65 20 69 6e 64 65 78 65 64 20 61 6e 64 0a 20  be indexed and. 
24e25 20 2a 2a 20 6c 6f 61 64 20 74 68 65 20 63 6f 6c   ** load the col
24e26 75 6d 6e 20 69 6e 64 69 63 65 73 20 69 6e 74 6f  umn indices into
24e27 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63   the Index struc
24e28 74 75 72 65 2e 20 20 52 65 70 6f 72 74 20 61 6e  ture.  Report an
24e29 20 65 72 72 6f 72 0a 20 20 2a 2a 20 69 66 20 61   error.  ** if a
24e2a 6e 79 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74  ny column is not
24e2b 20 66 6f 75 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a   found..  **.  *
24e2c 2a 20 54 4f 44 4f 3a 20 20 41 64 64 20 61 20 74  * TODO:  Add a t
24e2d 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  est to make sure
24e2e 20 74 68 61 74 20 74 68 65 20 73 61 6d 65 20 63   that the same c
24e2f 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 6e 61 6d  olumn is not nam
24e30 65 64 0a 20 20 2a 2a 20 6d 6f 72 65 20 74 68 61  ed.  ** more tha
24e31 6e 20 6f 6e 63 65 20 77 69 74 68 69 6e 20 74 68  n once within th
24e32 65 20 73 61 6d 65 20 69 6e 64 65 78 2e 20 20 4f  e same index.  O
24e33 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 69 6e  nly the first in
24e34 73 74 61 6e 63 65 20 6f 66 0a 20 20 2a 2a 20 74  stance of.  ** t
24e35 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 65  he column will e
24e36 76 65 72 20 62 65 20 75 73 65 64 20 62 79 20 74  ver be used by t
24e37 68 65 20 6f 70 74 69 6d 69 7a 65 72 2e 20 20 4e  he optimizer.  N
24e38 6f 74 65 20 74 68 61 74 20 75 73 69 6e 67 20 74  ote that using t
24e39 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20 63 6f 6c  he.  ** same col
24e3a 75 6d 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  umn more than on
24e3b 63 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20  ce cannot be an 
24e3c 65 72 72 6f 72 20 62 65 63 61 75 73 65 20 74 68  error because th
24e3d 61 74 20 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 62  at would .  ** b
24e3e 72 65 61 6b 20 62 61 63 6b 77 61 72 64 73 20 63  reak backwards c
24e3f 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2d 20 69  ompatibility - i
24e40 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 20  t needs to be a 
24e41 77 61 72 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20  warning..  */.  
24e42 66 6f 72 28 69 3d 30 2c 20 70 4c 69 73 74 49 74  for(i=0, pListIt
24e43 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  em=pList->a; i<p
24e44 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
24e45 2c 20 70 4c 69 73 74 49 74 65 6d 2b 2b 29 7b 0a  , pListItem++){.
24e46 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
24e47 7a 43 6f 6c 4e 61 6d 65 20 3d 20 70 4c 69 73 74  zColName = pList
24e48 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20  Item->zName;.   
24e49 20 43 6f 6c 75 6d 6e 20 2a 70 54 61 62 43 6f 6c   Column *pTabCol
24e4a 3b 0a 20 20 20 20 69 6e 74 20 72 65 71 75 65 73  ;.    int reques
24e4b 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20  tedSortOrder;.  
24e4c 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20    char *zColl;  
24e4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24e4e 20 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65   /* Collation se
24e4f 71 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 0a  quence name */..
24e50 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 54 61      for(j=0, pTa
24e51 62 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b  bCol=pTab->aCol;
24e52 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a   j<pTab->nCol; j
24e53 2b 2b 2c 20 70 54 61 62 43 6f 6c 2b 2b 29 7b 0a  ++, pTabCol++){.
24e54 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
24e55 33 53 74 72 49 43 6d 70 28 7a 43 6f 6c 4e 61 6d  3StrICmp(zColNam
24e56 65 2c 20 70 54 61 62 43 6f 6c 2d 3e 7a 4e 61 6d  e, pTabCol->zNam
24e57 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  e)==0 ) break;. 
24e58 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d     }.    if( j>=
24e59 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pTab->nCol ){.  
24e5a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24e5b 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62  Msg(pParse, "tab
24e5c 6c 65 20 25 73 20 68 61 73 20 6e 6f 20 63 6f 6c  le %s has no col
24e5d 75 6d 6e 20 6e 61 6d 65 64 20 25 73 22 2c 0a 20  umn named %s",. 
24e5e 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61         pTab->zNa
24e5f 6d 65 2c 20 7a 43 6f 6c 4e 61 6d 65 29 3b 0a 20  me, zColName);. 
24e60 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
24e61 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
24e62 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61   }.    pIndex->a
24e63 69 43 6f 6c 75 6d 6e 5b 69 5d 20 3d 20 6a 3b 0a  iColumn[i] = j;.
24e64 20 20 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61      /* Justifica
24e65 74 69 6f 6e 20 6f 66 20 74 68 65 20 41 4c 57 41  tion of the ALWA
24e66 59 53 28 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45  YS(pListItem->pE
24e67 78 70 72 2d 3e 70 43 6f 6c 6c 29 3a 20 20 42 65  xpr->pColl):  Be
24e68 63 61 75 73 65 20 6f 66 0a 20 20 20 20 2a 2a 20  cause of.    ** 
24e69 74 68 65 20 77 61 79 20 74 68 65 20 22 69 64 78  the way the "idx
24e6a 6c 69 73 74 22 20 6e 6f 6e 2d 74 65 72 6d 69 6e  list" non-termin
24e6b 61 6c 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65  al is constructe
24e6c 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 2c  d by the parser,
24e6d 0a 20 20 20 20 2a 2a 20 69 66 20 70 4c 69 73 74  .    ** if pList
24e6e 49 74 65 6d 2d 3e 70 45 78 70 72 20 69 73 20 6e  Item->pExpr is n
24e6f 6f 74 20 6e 75 6c 6c 20 74 68 65 6e 20 65 69 74  ot null then eit
24e70 68 65 72 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70  her pListItem->p
24e71 45 78 70 72 2d 3e 70 43 6f 6c 6c 0a 20 20 20 20  Expr->pColl.    
24e72 2a 2a 20 6d 75 73 74 20 65 78 69 73 74 20 6f 72  ** must exist or
24e73 20 65 6c 73 65 20 74 68 65 72 65 20 6d 75 73 74   else there must
24e74 20 68 61 76 65 20 62 65 65 6e 20 61 6e 20 4f 4f   have been an OO
24e75 4d 20 65 72 72 6f 72 2e 20 20 42 75 74 20 69 66  M error.  But if
24e76 20 74 68 65 72 65 0a 20 20 20 20 2a 2a 20 77 61   there.    ** wa
24e77 73 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2c 20  s an OOM error, 
24e78 77 65 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 72  we would never r
24e79 65 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2e  each this point.
24e7a 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c 69 73   */.    if( pLis
24e7b 74 49 74 65 6d 2d 3e 70 45 78 70 72 20 26 26 20  tItem->pExpr && 
24e7c 41 4c 57 41 59 53 28 70 4c 69 73 74 49 74 65 6d  ALWAYS(pListItem
24e7d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 29 20  ->pExpr->pColl) 
24e7e 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 6f  ){.      int nCo
24e7f 6c 6c 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20  ll;.      zColl 
24e80 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78  = pListItem->pEx
24e81 70 72 2d 3e 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  pr->pColl->zName
24e82 3b 0a 20 20 20 20 20 20 6e 43 6f 6c 6c 20 3d 20  ;.      nColl = 
24e83 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
24e84 7a 43 6f 6c 6c 29 20 2b 20 31 3b 0a 20 20 20 20  zColl) + 1;.    
24e85 20 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61    assert( nExtra
24e86 3e 3d 6e 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20  >=nColl );.     
24e87 20 6d 65 6d 63 70 79 28 7a 45 78 74 72 61 2c 20   memcpy(zExtra, 
24e88 7a 43 6f 6c 6c 2c 20 6e 43 6f 6c 6c 29 3b 0a 20  zColl, nColl);. 
24e89 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 7a 45 78       zColl = zEx
24e8a 74 72 61 3b 0a 20 20 20 20 20 20 7a 45 78 74 72  tra;.      zExtr
24e8b 61 20 2b 3d 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20  a += nColl;.    
24e8c 20 20 6e 45 78 74 72 61 20 2d 3d 20 6e 43 6f 6c    nExtra -= nCol
24e8d 6c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  l;.    }else{.  
24e8e 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 54 61 62      zColl = pTab
24e8f 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b  ->aCol[j].zColl;
24e90 0a 20 20 20 20 20 20 69 66 28 20 21 7a 43 6f 6c  .      if( !zCol
24e91 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f  l ){.        zCo
24e92 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
24e93 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  ll->zName;.     
24e94 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
24e95 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
24e96 26 26 20 21 73 71 6c 69 74 65 33 4c 6f 63 61 74  && !sqlite3Locat
24e97 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
24e98 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20   zColl) ){.     
24e99 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
24e9a 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20  e_index;.    }. 
24e9b 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c     pIndex->azCol
24e9c 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20  l[i] = zColl;.  
24e9d 20 20 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f    requestedSortO
24e9e 72 64 65 72 20 3d 20 70 4c 69 73 74 49 74 65 6d  rder = pListItem
24e9f 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26 20 73 6f  ->sortOrder & so
24ea0 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a 20 20 20  rtOrderMask;.   
24ea1 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72   pIndex->aSortOr
24ea2 64 65 72 5b 69 5d 20 3d 20 28 75 38 29 72 65 71  der[i] = (u8)req
24ea3 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65 72 3b  uestedSortOrder;
24ea4 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65  .  }.  sqlite3De
24ea5 66 61 75 6c 74 52 6f 77 45 73 74 28 70 49 6e 64  faultRowEst(pInd
24ea6 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62  ex);..  if( pTab
24ea7 3d 3d 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  ==pParse->pNewTa
24ea8 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ble ){.    /* Th
24ea9 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 62  is routine has b
24eaa 65 65 6e 20 63 61 6c 6c 65 64 20 74 6f 20 63 72  een called to cr
24eab 65 61 74 65 20 61 6e 20 61 75 74 6f 6d 61 74 69  eate an automati
24eac 63 20 69 6e 64 65 78 20 61 73 20 61 0a 20 20 20  c index as a.   
24ead 20 2a 2a 20 72 65 73 75 6c 74 20 6f 66 20 61 20   ** result of a 
24eae 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55  PRIMARY KEY or U
24eaf 4e 49 51 55 45 20 63 6c 61 75 73 65 20 6f 6e 20  NIQUE clause on 
24eb0 61 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74  a column definit
24eb1 69 6f 6e 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61  ion, or.    ** a
24eb2 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
24eb3 55 4e 49 51 55 45 20 63 6c 61 75 73 65 20 66 6f  UNIQUE clause fo
24eb4 6c 6c 6f 77 69 6e 67 20 74 68 65 20 63 6f 6c 75  llowing the colu
24eb5 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a  mn definitions..
24eb6 20 20 20 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20      ** i.e. one 
24eb7 6f 66 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  of:.    **.    *
24eb8 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74  * CREATE TABLE t
24eb9 28 78 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20  (x PRIMARY KEY, 
24eba 79 29 3b 0a 20 20 20 20 2a 2a 20 43 52 45 41 54  y);.    ** CREAT
24ebb 45 20 54 41 42 4c 45 20 74 28 78 2c 20 79 2c 20  E TABLE t(x, y, 
24ebc 55 4e 49 51 55 45 28 78 2c 20 79 29 29 3b 0a 20  UNIQUE(x, y));. 
24ebd 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 69 74     **.    ** Eit
24ebe 68 65 72 20 77 61 79 2c 20 63 68 65 63 6b 20 74  her way, check t
24ebf 6f 20 73 65 65 20 69 66 20 74 68 65 20 74 61 62  o see if the tab
24ec0 6c 65 20 61 6c 72 65 61 64 79 20 68 61 73 20 73  le already has s
24ec1 75 63 68 20 61 6e 20 69 6e 64 65 78 2e 20 49 66  uch an index. If
24ec2 0a 20 20 20 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27  .    ** so, don'
24ec3 74 20 62 6f 74 68 65 72 20 63 72 65 61 74 69 6e  t bother creatin
24ec4 67 20 74 68 69 73 20 6f 6e 65 2e 20 54 68 69 73  g this one. This
24ec5 20 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74 6f   only applies to
24ec6 0a 20 20 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69  .    ** automati
24ec7 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 69 6e  cally created in
24ec8 64 69 63 65 73 2e 20 55 73 65 72 73 20 63 61 6e  dices. Users can
24ec9 20 64 6f 20 61 73 20 74 68 65 79 20 77 69 73 68   do as they wish
24eca 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 65 78 70   with.    ** exp
24ecb 6c 69 63 69 74 20 69 6e 64 69 63 65 73 2e 0a 20  licit indices.. 
24ecc 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 77 6f     **.    ** Two
24ecd 20 55 4e 49 51 55 45 20 6f 72 20 50 52 49 4d 41   UNIQUE or PRIMA
24ece 52 59 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e  RY KEY constrain
24ecf 74 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65  ts are considere
24ed0 64 20 65 71 75 69 76 61 6c 65 6e 74 0a 20 20 20  d equivalent.   
24ed1 20 2a 2a 20 28 61 6e 64 20 74 68 75 73 20 73 75   ** (and thus su
24ed2 70 70 72 65 73 73 69 6e 67 20 74 68 65 20 73 65  ppressing the se
24ed3 63 6f 6e 64 20 6f 6e 65 29 20 65 76 65 6e 20 69  cond one) even i
24ed4 66 20 74 68 65 79 20 68 61 76 65 20 64 69 66 66  f they have diff
24ed5 65 72 65 6e 74 0a 20 20 20 20 2a 2a 20 73 6f 72  erent.    ** sor
24ed6 74 20 6f 72 64 65 72 73 2e 0a 20 20 20 20 2a 2a  t orders..    **
24ed7 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 72 65  .    ** If there
24ed8 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 20 63   are different c
24ed9 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
24eda 65 73 20 6f 72 20 69 66 20 74 68 65 20 63 6f 6c  es or if the col
24edb 75 6d 6e 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74  umns of.    ** t
24edc 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 63  he constraint oc
24edd 63 75 72 20 69 6e 20 64 69 66 66 65 72 65 6e 74  cur in different
24ede 20 6f 72 64 65 72 73 2c 20 74 68 65 6e 20 74 68   orders, then th
24edf 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72  e constraints ar
24ee0 65 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64 65  e.    ** conside
24ee1 72 65 64 20 64 69 73 74 69 6e 63 74 20 61 6e 64  red distinct and
24ee2 20 62 6f 74 68 20 72 65 73 75 6c 74 20 69 6e 20   both result in 
24ee3 73 65 70 61 72 61 74 65 20 69 6e 64 69 63 65 73  separate indices
24ee4 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 64  ..    */.    Ind
24ee5 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 66 6f  ex *pIdx;.    fo
24ee6 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
24ee7 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
24ee8 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
24ee9 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20      int k;.     
24eea 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 6f   assert( pIdx->o
24eeb 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20  nError!=OE_None 
24eec 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
24eed 20 70 49 64 78 2d 3e 61 75 74 6f 49 6e 64 65 78   pIdx->autoIndex
24eee 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
24eef 28 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f  ( pIndex->onErro
24ef0 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 0a 20  r!=OE_None );.. 
24ef1 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6e       if( pIdx->n
24ef2 43 6f 6c 75 6d 6e 21 3d 70 49 6e 64 65 78 2d 3e  Column!=pIndex->
24ef3 6e 43 6f 6c 75 6d 6e 20 29 20 63 6f 6e 74 69 6e  nColumn ) contin
24ef4 75 65 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d  ue;.      for(k=
24ef5 30 3b 20 6b 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75  0; k<pIdx->nColu
24ef6 6d 6e 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20  mn; k++){.      
24ef7 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31    const char *z1
24ef8 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  ;.        const 
24ef9 63 68 61 72 20 2a 7a 32 3b 0a 20 20 20 20 20 20  char *z2;.      
24efa 20 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f    if( pIdx->aiCo
24efb 6c 75 6d 6e 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d  lumn[k]!=pIndex-
24efc 3e 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62  >aiColumn[k] ) b
24efd 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 31  reak;.        z1
24efe 20 3d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b   = pIdx->azColl[
24eff 6b 5d 3b 0a 20 20 20 20 20 20 20 20 7a 32 20 3d  k];.        z2 =
24f00 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b   pIndex->azColl[
24f01 6b 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  k];.        if( 
24f02 7a 31 21 3d 7a 32 20 26 26 20 73 71 6c 69 74 65  z1!=z2 && sqlite
24f03 33 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29  3StrICmp(z1, z2)
24f04 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
24f05 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 70  }.      if( k==p
24f06 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a  Idx->nColumn ){.
24f07 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78          if( pIdx
24f08 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e 64 65  ->onError!=pInde
24f09 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20  x->onError ){.  
24f0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
24f0b 63 6f 6e 73 74 72 61 69 6e 74 20 63 72 65 61 74  constraint creat
24f0c 65 73 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65  es the same inde
24f0d 78 20 61 73 20 61 20 70 72 65 76 69 6f 75 73 0a  x as a previous.
24f0e 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e            ** con
24f0f 73 74 72 61 69 6e 74 20 73 70 65 63 69 66 69 65  straint specifie
24f10 64 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74  d somewhere in t
24f11 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  he CREATE TABLE 
24f12 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20  statement..     
24f13 20 20 20 20 20 2a 2a 20 48 6f 77 65 76 65 72 20       ** However 
24f14 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  the ON CONFLICT 
24f15 63 6c 61 75 73 65 73 20 61 72 65 20 64 69 66 66  clauses are diff
24f16 65 72 65 6e 74 2e 20 49 66 20 62 6f 74 68 20 74  erent. If both t
24f17 68 69 73 20 0a 20 20 20 20 20 20 20 20 20 20 2a  his .          *
24f18 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64  * constraint and
24f19 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 71   the previous eq
24f1a 75 69 76 61 6c 65 6e 74 20 63 6f 6e 73 74 72 61  uivalent constra
24f1b 69 6e 74 20 68 61 76 65 20 65 78 70 6c 69 63 69  int have explici
24f1c 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 4f  t.          ** O
24f1d 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73  N CONFLICT claus
24f1e 65 73 20 74 68 69 73 20 69 73 20 61 6e 20 65 72  es this is an er
24f1f 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ror. Otherwise, 
24f20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 20 20  use the.        
24f21 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20    ** explicitly 
24f22 73 70 65 63 69 66 69 65 64 20 62 65 68 61 76 69  specified behavi
24f23 6f 75 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65  our for the inde
24f24 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  x..          */.
24f25 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 28            if( !(
24f26 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f  pIdx->onError==O
24f27 45 5f 44 65 66 61 75 6c 74 20 7c 7c 20 70 49 6e  E_Default || pIn
24f28 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45  dex->onError==OE
24f29 5f 44 65 66 61 75 6c 74 29 20 29 7b 0a 20 20 20  _Default) ){.   
24f2a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
24f2b 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
24f2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
24f2d 20 20 22 63 6f 6e 66 6c 69 63 74 69 6e 67 20 4f    "conflicting O
24f2e 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73  N CONFLICT claus
24f2f 65 73 20 73 70 65 63 69 66 69 65 64 22 2c 20 30  es specified", 0
24f30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
24f31 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64           if( pId
24f32 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  x->onError==OE_D
24f33 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
24f34 20 20 20 20 20 20 70 49 64 78 2d 3e 6f 6e 45 72        pIdx->onEr
24f35 72 6f 72 20 3d 20 70 49 6e 64 65 78 2d 3e 6f 6e  ror = pIndex->on
24f36 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20 20  Error;.         
24f37 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
24f38 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
24f39 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
24f3a 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
24f3b 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65    /* Link the ne
24f3c 77 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72  w Index structur
24f3d 65 20 74 6f 20 69 74 73 20 74 61 62 6c 65 20 61  e to its table a
24f3e 6e 64 20 74 6f 20 74 68 65 20 6f 74 68 65 72 0a  nd to the other.
24f3f 20 20 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64    ** in-memory d
24f40 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72  atabase structur
24f41 65 73 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20  es. .  */.  if( 
24f42 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b  db->init.busy ){
24f43 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20  .    Index *p;. 
24f44 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61     p = sqlite3Ha
24f45 73 68 49 6e 73 65 72 74 28 26 70 49 6e 64 65 78  shInsert(&pIndex
24f46 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61  ->pSchema->idxHa
24f47 73 68 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  sh, .           
24f48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
24f49 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 73 71  Index->zName, sq
24f4a 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 49  lite3Strlen30(pI
24f4b 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2c 0a 20 20  ndex->zName),.  
24f4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24f4d 20 20 20 20 20 20 20 20 70 49 6e 64 65 78 29 3b          pIndex);
24f4e 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20  .    if( p ){.  
24f4f 20 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70      assert( p==p
24f50 49 6e 64 65 78 20 29 3b 20 20 2f 2a 20 4d 61 6c  Index );  /* Mal
24f51 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61  loc must have fa
24f52 69 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20 64 62  iled */.      db
24f53 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
24f54 20 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65   1;.      goto e
24f55 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
24f56 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e  ;.    }.    db->
24f57 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f  flags |= SQLITE_
24f58 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20  InternChanges;. 
24f59 20 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21     if( pTblName!
24f5a 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64  =0 ){.      pInd
24f5b 65 78 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69  ex->tnum = db->i
24f5c 6e 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 20  nit.newTnum;.   
24f5d 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
24f5e 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  the db->init.bus
24f5f 79 20 69 73 20 30 20 74 68 65 6e 20 63 72 65 61  y is 0 then crea
24f60 74 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20  te the index on 
24f61 64 69 73 6b 2e 20 20 54 68 69 73 0a 20 20 2a 2a  disk.  This.  **
24f62 20 69 6e 76 6f 6c 76 65 73 20 77 72 69 74 69 6e   involves writin
24f63 67 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f  g the index into
24f64 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c   the master tabl
24f65 65 20 61 6e 64 20 66 69 6c 6c 69 6e 67 20 69 6e  e and filling in
24f66 20 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20   the.  ** index 
24f67 77 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74  with the current
24f68 20 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74 73 2e   table contents.
24f69 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64  .  **.  ** The d
24f6a 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20  b->init.busy is 
24f6b 30 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20  0 when the user 
24f6c 66 69 72 73 74 20 65 6e 74 65 72 73 20 61 20 43  first enters a C
24f6d 52 45 41 54 45 20 49 4e 44 45 58 20 0a 20 20 2a  REATE INDEX .  *
24f6e 2a 20 63 6f 6d 6d 61 6e 64 2e 20 20 64 62 2d 3e  * command.  db->
24f6f 69 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 77  init.busy is 1 w
24f70 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 69  hen a database i
24f71 73 20 6f 70 65 6e 65 64 20 61 6e 64 20 0a 20 20  s opened and .  
24f72 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  ** CREATE INDEX 
24f73 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 72  statements are r
24f74 65 61 64 20 6f 75 74 20 6f 66 20 74 68 65 20 6d  ead out of the m
24f75 61 73 74 65 72 20 74 61 62 6c 65 2e 20 20 49 6e  aster table.  In
24f76 0a 20 20 2a 2a 20 74 68 65 20 6c 61 74 74 65 72  .  ** the latter
24f77 20 63 61 73 65 20 74 68 65 20 69 6e 64 65 78 20   case the index 
24f78 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 6f  already exists o
24f79 6e 20 64 69 73 6b 2c 20 77 68 69 63 68 20 69 73  n disk, which is
24f7a 20 77 68 79 0a 20 20 2a 2a 20 77 65 20 64 6f 6e   why.  ** we don
24f7b 27 74 20 77 61 6e 74 20 74 6f 20 72 65 63 72 65  't want to recre
24f7c 61 74 65 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a  ate it..  **.  *
24f7d 2a 20 49 66 20 70 54 62 6c 4e 61 6d 65 3d 3d 30  * If pTblName==0
24f7e 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69   it means this i
24f7f 6e 64 65 78 20 69 73 20 67 65 6e 65 72 61 74 65  ndex is generate
24f80 64 20 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b  d as a primary k
24f81 65 79 0a 20 20 2a 2a 20 6f 72 20 55 4e 49 51 55  ey.  ** or UNIQU
24f82 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20  E constraint of 
24f83 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
24f84 74 61 74 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65  tatement.  Since
24f85 20 74 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20   the table.  ** 
24f86 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 63 72  has just been cr
24f87 65 61 74 65 64 2c 20 69 74 20 63 6f 6e 74 61 69  eated, it contai
24f88 6e 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 74  ns no data and t
24f89 68 65 20 69 6e 64 65 78 20 69 6e 69 74 69 61 6c  he index initial
24f8a 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65  ization.  ** ste
24f8b 70 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64  p can be skipped
24f8c 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 7b 20 2f  ..  */.  else{ /
24f8d 2a 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62  * if( db->init.b
24f8e 75 73 79 3d 3d 30 20 29 20 2a 2f 0a 20 20 20 20  usy==0 ) */.    
24f8f 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61  Vdbe *v;.    cha
24f90 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 69 6e  r *zStmt;.    in
24f91 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73  t iMem = ++pPars
24f92 65 2d 3e 6e 4d 65 6d 3b 0a 0a 20 20 20 20 76 20  e->nMem;..    v 
24f93 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
24f94 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66  (pParse);.    if
24f95 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78  ( v==0 ) goto ex
24f96 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
24f97 0a 0a 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65  ...    /* Create
24f98 20 74 68 65 20 72 6f 6f 74 70 61 67 65 20 66 6f   the rootpage fo
24f99 72 20 74 68 65 20 69 6e 64 65 78 0a 20 20 20 20  r the index.    
24f9a 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  */.    sqlite3Be
24f9b 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
24f9c 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44 62  n(pParse, 1, iDb
24f9d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
24f9e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
24f9f 72 65 61 74 65 49 6e 64 65 78 2c 20 69 44 62 2c  reateIndex, iDb,
24fa0 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 2f 2a 20   iMem);..    /* 
24fa1 47 61 74 68 65 72 20 74 68 65 20 63 6f 6d 70 6c  Gather the compl
24fa2 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  ete text of the 
24fa3 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61  CREATE INDEX sta
24fa4 74 65 6d 65 6e 74 20 69 6e 74 6f 0a 20 20 20 20  tement into.    
24fa5 2a 2a 20 74 68 65 20 7a 53 74 6d 74 20 76 61 72  ** the zStmt var
24fa6 69 61 62 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20  iable.    */.   
24fa7 20 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20   if( pStart ){. 
24fa8 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 6e       assert( pEn
24fa9 64 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 2f 2a  d!=0 );.      /*
24faa 20 41 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 77   A named index w
24fab 69 74 68 20 61 6e 20 65 78 70 6c 69 63 69 74 20  ith an explicit 
24fac 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61  CREATE INDEX sta
24fad 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20  tement */.      
24fae 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d  zStmt = sqlite3M
24faf 50 72 69 6e 74 66 28 64 62 2c 20 22 43 52 45 41  Printf(db, "CREA
24fb0 54 45 25 73 20 49 4e 44 45 58 20 25 2e 2a 73 22  TE%s INDEX %.*s"
24fb1 2c 0a 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f  ,.        onErro
24fb2 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 3f 20 22 22 20  r==OE_None ? "" 
24fb3 3a 20 22 20 55 4e 49 51 55 45 22 2c 0a 20 20 20  : " UNIQUE",.   
24fb4 20 20 20 20 20 70 45 6e 64 2d 3e 7a 20 2d 20 70       pEnd->z - p
24fb5 4e 61 6d 65 2d 3e 7a 20 2b 20 31 2c 0a 20 20 20  Name->z + 1,.   
24fb6 20 20 20 20 20 70 4e 61 6d 65 2d 3e 7a 29 3b 0a       pName->z);.
24fb7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
24fb8 20 2f 2a 20 41 6e 20 61 75 74 6f 6d 61 74 69 63   /* An automatic
24fb9 20 69 6e 64 65 78 20 63 72 65 61 74 65 64 20 62   index created b
24fba 79 20 61 20 50 52 49 4d 41 52 59 20 4b 45 59 20  y a PRIMARY KEY 
24fbb 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  or UNIQUE constr
24fbc 61 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 2f 2a  aint */.      /*
24fbd 20 7a 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33   zStmt = sqlite3
24fbe 4d 50 72 69 6e 74 66 28 22 22 29 3b 20 2a 2f 0a  MPrintf(""); */.
24fbf 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20 30 3b        zStmt = 0;
24fc0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
24fc1 64 64 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 73  dd an entry in s
24fc2 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 66 6f 72  qlite_master for
24fc3 20 74 68 69 73 20 69 6e 64 65 78 0a 20 20 20 20   this index.    
24fc4 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65  */.    sqlite3Ne
24fc5 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
24fc6 2c 20 0a 20 20 20 20 20 20 20 20 22 49 4e 53 45  , .        "INSE
24fc7 52 54 20 49 4e 54 4f 20 25 51 2e 25 73 20 56 41  RT INTO %Q.%s VA
24fc8 4c 55 45 53 28 27 69 6e 64 65 78 27 2c 25 51 2c  LUES('index',%Q,
24fc9 25 51 2c 23 25 64 2c 25 51 29 3b 22 2c 0a 20 20  %Q,#%d,%Q);",.  
24fca 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
24fcb 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41  b].zName, SCHEMA
24fcc 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20  _TABLE(iDb),.   
24fcd 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61       pIndex->zNa
24fce 6d 65 2c 0a 20 20 20 20 20 20 20 20 70 54 61 62  me,.        pTab
24fcf 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  ->zName,.       
24fd0 20 69 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 7a   iMem,.        z
24fd1 53 74 6d 74 0a 20 20 20 20 29 3b 0a 20 20 20 20  Stmt.    );.    
24fd2 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
24fd3 2c 20 7a 53 74 6d 74 29 3b 0a 0a 20 20 20 20 2f  , zStmt);..    /
24fd4 2a 20 46 69 6c 6c 20 74 68 65 20 69 6e 64 65 78  * Fill the index
24fd5 20 77 69 74 68 20 64 61 74 61 20 61 6e 64 20 72   with data and r
24fd6 65 70 61 72 73 65 20 74 68 65 20 73 63 68 65 6d  eparse the schem
24fd7 61 2e 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78  a. Code an OP_Ex
24fd8 70 69 72 65 0a 20 20 20 20 2a 2a 20 74 6f 20 69  pire.    ** to i
24fd9 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72  nvalidate all pr
24fda 65 2d 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65  e-compiled state
24fdb 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  ments..    */.  
24fdc 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 20 29    if( pTblName )
24fdd 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
24fde 65 66 69 6c 6c 49 6e 64 65 78 28 70 50 61 72 73  efillIndex(pPars
24fdf 65 2c 20 70 49 6e 64 65 78 2c 20 69 4d 65 6d 29  e, pIndex, iMem)
24fe0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43  ;.      sqlite3C
24fe1 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72  hangeCookie(pPar
24fe2 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  se, iDb);.      
24fe3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24fe4 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68  4(v, OP_ParseSch
24fe5 65 6d 61 2c 20 69 44 62 2c 20 30 2c 20 30 2c 0a  ema, iDb, 0, 0,.
24fe6 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
24fe7 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d  MPrintf(db, "nam
24fe8 65 3d 27 25 71 27 22 2c 20 70 49 6e 64 65 78 2d  e='%q'", pIndex-
24fe9 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41  >zName), P4_DYNA
24fea 4d 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  MIC);.      sqli
24feb 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
24fec 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 29 3b 0a   OP_Expire, 0);.
24fed 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
24fee 57 68 65 6e 20 61 64 64 69 6e 67 20 61 6e 20 69  When adding an i
24fef 6e 64 65 78 20 74 6f 20 74 68 65 20 6c 69 73 74  ndex to the list
24ff0 20 6f 66 20 69 6e 64 69 63 65 73 20 66 6f 72 20   of indices for 
24ff1 61 20 74 61 62 6c 65 2c 20 6d 61 6b 65 0a 20 20  a table, make.  
24ff2 2a 2a 20 73 75 72 65 20 61 6c 6c 20 69 6e 64 69  ** sure all indi
24ff3 63 65 73 20 6c 61 62 65 6c 65 64 20 4f 45 5f 52  ces labeled OE_R
24ff4 65 70 6c 61 63 65 20 63 6f 6d 65 20 61 66 74 65  eplace come afte
24ff5 72 20 61 6c 6c 20 74 68 6f 73 65 20 6c 61 62 65  r all those labe
24ff6 6c 65 64 0a 20 20 2a 2a 20 4f 45 5f 49 67 6e 6f  led.  ** OE_Igno
24ff7 72 65 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63  re.  This is nec
24ff8 65 73 73 61 72 79 20 66 6f 72 20 74 68 65 20 63  essary for the c
24ff9 6f 72 72 65 63 74 20 63 6f 6e 73 74 72 61 69 6e  orrect constrain
24ffa 74 20 63 68 65 63 6b 0a 20 20 2a 2a 20 70 72 6f  t check.  ** pro
24ffb 63 65 73 73 69 6e 67 20 28 69 6e 20 73 71 6c 69  cessing (in sqli
24ffc 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74  te3GenerateConst
24ffd 72 61 69 6e 74 43 68 65 63 6b 73 28 29 29 20 61  raintChecks()) a
24ffe 73 20 70 61 72 74 20 6f 66 0a 20 20 2a 2a 20 55  s part of.  ** U
24fff 50 44 41 54 45 20 61 6e 64 20 49 4e 53 45 52 54  PDATE and INSERT
25000 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 0a 20   statements.  . 
25001 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e   */.  if( db->in
25002 69 74 2e 62 75 73 79 20 7c 7c 20 70 54 62 6c 4e  it.busy || pTblN
25003 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  ame==0 ){.    if
25004 28 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65  ( onError!=OE_Re
25005 70 6c 61 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70  place || pTab->p
25006 49 6e 64 65 78 3d 3d 30 0a 20 20 20 20 20 20 20  Index==0.       
25007 20 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65    || pTab->pInde
25008 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  x->onError==OE_R
25009 65 70 6c 61 63 65 29 7b 0a 20 20 20 20 20 20 70  eplace){.      p
2500a 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70  Index->pNext = p
2500b 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20  Tab->pIndex;.   
2500c 20 20 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 20     pTab->pIndex 
2500d 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d 65  = pIndex;.    }e
2500e 6c 73 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 78  lse{.      Index
2500f 20 2a 70 4f 74 68 65 72 20 3d 20 70 54 61 62 2d   *pOther = pTab-
25010 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20 77  >pIndex;.      w
25011 68 69 6c 65 28 20 70 4f 74 68 65 72 2d 3e 70 4e  hile( pOther->pN
25012 65 78 74 20 26 26 20 70 4f 74 68 65 72 2d 3e 70  ext && pOther->p
25013 4e 65 78 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  Next->onError!=O
25014 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20  E_Replace ){.   
25015 20 20 20 20 20 70 4f 74 68 65 72 20 3d 20 70 4f       pOther = pO
25016 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ther->pNext;.   
25017 20 20 20 7d 0a 20 20 20 20 20 20 70 49 6e 64 65     }.      pInde
25018 78 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68 65  x->pNext = pOthe
25019 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  r->pNext;.      
2501a 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20  pOther->pNext = 
2501b 70 49 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20  pIndex;.    }.  
2501c 20 20 70 52 65 74 20 3d 20 70 49 6e 64 65 78 3b    pRet = pIndex;
2501d 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20 30 3b  .    pIndex = 0;
2501e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e  .  }..  /* Clean
2501f 20 75 70 20 62 65 66 6f 72 65 20 65 78 69 74 69   up before exiti
25020 6e 67 20 2a 2f 0a 65 78 69 74 5f 63 72 65 61 74  ng */.exit_creat
25021 65 5f 69 6e 64 65 78 3a 0a 20 20 69 66 28 20 70  e_index:.  if( p
25022 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71 6c  Index ){.    sql
25023 69 74 65 33 5f 66 72 65 65 28 70 49 6e 64 65 78  ite3_free(pIndex
25024 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20 20 20 20  ->zColAff);.    
25025 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
25026 2c 20 70 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20  , pIndex);.  }. 
25027 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
25028 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74  Delete(db, pList
25029 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c  );.  sqlite3SrcL
2502a 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54  istDelete(db, pT
2502b 62 6c 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  blName);.  sqlit
2502c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61  e3DbFree(db, zNa
2502d 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52  me);.  return pR
2502e 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c  et;.}../*.** Fil
2502f 6c 20 74 68 65 20 49 6e 64 65 78 2e 61 69 52 6f  l the Index.aiRo
25030 77 45 73 74 5b 5d 20 61 72 72 61 79 20 77 69 74  wEst[] array wit
25031 68 20 64 65 66 61 75 6c 74 20 69 6e 66 6f 72 6d  h default inform
25032 61 74 69 6f 6e 20 2d 20 69 6e 66 6f 72 6d 61 74  ation - informat
25033 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 75 73 65  ion.** to be use
25034 64 20 77 68 65 6e 20 77 65 20 68 61 76 65 20 6e  d when we have n
25035 6f 74 20 72 75 6e 20 74 68 65 20 41 4e 41 4c 59  ot run the ANALY
25036 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a  ZE command..**.*
25037 2a 20 61 69 52 6f 77 45 73 74 5b 30 5d 20 69 73  * aiRowEst[0] is
25038 20 73 75 70 70 6f 73 65 20 74 6f 20 63 6f 6e 74   suppose to cont
25039 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ain the number o
2503a 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68  f elements in th
2503b 65 20 69 6e 64 65 78 2e 0a 2a 2a 20 53 69 6e 63  e index..** Sinc
2503c 65 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77  e we do not know
2503d 2c 20 67 75 65 73 73 20 31 20 6d 69 6c 6c 69 6f  , guess 1 millio
2503e 6e 2e 20 20 61 69 52 6f 77 45 73 74 5b 31 5d 20  n.  aiRowEst[1] 
2503f 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f  is an estimate o
25040 66 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  f the.** number 
25041 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74  of rows in the t
25042 61 62 6c 65 20 74 68 61 74 20 6d 61 74 63 68 20  able that match 
25043 61 6e 79 20 70 61 72 74 69 63 75 6c 61 72 20 76  any particular v
25044 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66  alue of the.** f
25045 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  irst column of t
25046 68 65 20 69 6e 64 65 78 2e 20 20 61 69 52 6f 77  he index.  aiRow
25047 45 73 74 5b 32 5d 20 69 73 20 61 6e 20 65 73 74  Est[2] is an est
25048 69 6d 61 74 65 20 6f 66 20 74 68 65 20 6e 75 6d  imate of the num
25049 62 65 72 0a 2a 2a 20 6f 66 20 72 6f 77 73 20 74  ber.** of rows t
2504a 68 61 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61  hat match any pa
2504b 72 74 69 63 75 6c 61 72 20 63 6f 6d 62 69 6e 69  rticular combini
2504c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 69 72  ation of the fir
2504d 73 74 20 32 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  st 2 columns.** 
2504e 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 41  of the index.  A
2504f 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49 74  nd so forth.  It
25050 20 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 20   must always be 
25051 74 68 65 20 63 61 73 65 20 74 68 61 74 0a 2a 0a  the case that.*.
25052 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69 52  **           aiR
25053 6f 77 45 73 74 5b 4e 5d 3c 3d 61 69 52 6f 77 45  owEst[N]<=aiRowE
25054 73 74 5b 4e 2d 31 5d 0a 2a 2a 20 20 20 20 20 20  st[N-1].**      
25055 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d       aiRowEst[N]
25056 3e 3d 31 0a 2a 2a 0a 2a 2a 20 41 70 61 72 74 20  >=1.**.** Apart 
25057 66 72 6f 6d 20 74 68 61 74 2c 20 77 65 20 68 61  from that, we ha
25058 76 65 20 6c 69 74 74 6c 65 20 74 6f 20 67 6f 20  ve little to go 
25059 6f 6e 20 62 65 73 69 64 65 73 20 69 6e 74 75 69  on besides intui
2505a 74 69 6f 6e 20 61 73 20 74 6f 0a 2a 2a 20 68 6f  tion as to.** ho
2505b 77 20 61 69 52 6f 77 45 73 74 5b 5d 20 73 68 6f  w aiRowEst[] sho
2505c 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 69 7a  uld be initializ
2505d 65 64 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 73  ed.  The numbers
2505e 20 67 65 6e 65 72 61 74 65 64 20 68 65 72 65 0a   generated here.
2505f 2a 2a 20 61 72 65 20 62 61 73 65 64 20 6f 6e 20  ** are based on 
25060 74 79 70 69 63 61 6c 20 76 61 6c 75 65 73 20 66  typical values f
25061 6f 75 6e 64 20 69 6e 20 61 63 74 75 61 6c 20 69  ound in actual i
25062 6e 64 69 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ndices..*/.SQLIT
25063 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
25064 71 6c 69 74 65 33 44 65 66 61 75 6c 74 52 6f 77  qlite3DefaultRow
25065 45 73 74 28 49 6e 64 65 78 20 2a 70 49 64 78 29  Est(Index *pIdx)
25066 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 2a 61 20  {.  unsigned *a 
25067 3d 20 70 49 64 78 2d 3e 61 69 52 6f 77 45 73 74  = pIdx->aiRowEst
25068 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73  ;.  int i;.  ass
25069 65 72 74 28 20 61 21 3d 30 20 29 3b 0a 20 20 61  ert( a!=0 );.  a
2506a 5b 30 5d 20 3d 20 31 30 30 30 30 30 30 3b 0a 20  [0] = 1000000;. 
2506b 20 66 6f 72 28 69 3d 70 49 64 78 2d 3e 6e 43 6f   for(i=pIdx->nCo
2506c 6c 75 6d 6e 3b 20 69 3e 3d 35 3b 20 69 2d 2d 29  lumn; i>=5; i--)
2506d 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20 35 3b 0a  {.    a[i] = 5;.
2506e 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 69 3e 3d    }.  while( i>=
2506f 31 20 29 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20  1 ){.    a[i] = 
25070 31 31 20 2d 20 69 3b 0a 20 20 20 20 69 2d 2d 3b  11 - i;.    i--;
25071 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78 2d  .  }.  if( pIdx-
25072 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
25073 65 20 29 7b 0a 20 20 20 20 61 5b 70 49 64 78 2d  e ){.    a[pIdx-
25074 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20 31 3b 0a 20  >nColumn] = 1;. 
25075 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
25076 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 72   routine will dr
25077 6f 70 20 61 6e 20 65 78 69 73 74 69 6e 67 20 6e  op an existing n
25078 61 6d 65 64 20 69 6e 64 65 78 2e 20 20 54 68 69  amed index.  Thi
25079 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6d 70  s routine.** imp
2507a 6c 65 6d 65 6e 74 73 20 74 68 65 20 44 52 4f 50  lements the DROP
2507b 20 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74   INDEX statement
2507c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2507d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2507e 44 72 6f 70 49 6e 64 65 78 28 50 61 72 73 65 20  DropIndex(Parse 
2507f 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73 74  *pParse, SrcList
25080 20 2a 70 4e 61 6d 65 2c 20 69 6e 74 20 69 66 45   *pName, int ifE
25081 78 69 73 74 73 29 7b 0a 20 20 49 6e 64 65 78 20  xists){.  Index 
25082 2a 70 49 6e 64 65 78 3b 0a 20 20 56 64 62 65 20  *pIndex;.  Vdbe 
25083 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *v;.  sqlite3 *d
25084 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
25085 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 61 73    int iDb;..  as
25086 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 45  sert( pParse->nE
25087 72 72 3d 3d 30 20 29 3b 20 20 20 2f 2a 20 4e 65  rr==0 );   /* Ne
25088 76 65 72 20 63 61 6c 6c 65 64 20 77 69 74 68 20  ver called with 
25089 70 72 69 6f 72 20 65 72 72 6f 72 73 20 2a 2f 0a  prior errors */.
2508a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2508b 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f  Failed ){.    go
2508c 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64  to exit_drop_ind
2508d 65 78 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  ex;.  }.  assert
2508e 28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31  ( pName->nSrc==1
2508f 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45   );.  if( SQLITE
25090 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64  _OK!=sqlite3Read
25091 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
25092 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
25093 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a  drop_index;.  }.
25094 20 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74    pIndex = sqlit
25095 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20  e3FindIndex(db, 
25096 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d  pName->a[0].zNam
25097 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a  e, pName->a[0].z
25098 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28  Database);.  if(
25099 20 70 49 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20   pIndex==0 ){.  
2509a 20 20 69 66 28 20 21 69 66 45 78 69 73 74 73 20    if( !ifExists 
2509b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2509c 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2509d 20 22 6e 6f 20 73 75 63 68 20 69 6e 64 65 78 3a   "no such index:
2509e 20 25 53 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b   %S", pName, 0);
2509f 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
250a0 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d  e->checkSchema =
250a1 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69   1;.    goto exi
250a2 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20  t_drop_index;.  
250a3 7d 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e  }.  if( pIndex->
250a4 61 75 74 6f 49 6e 64 65 78 20 29 7b 0a 20 20 20  autoIndex ){.   
250a5 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
250a6 28 70 50 61 72 73 65 2c 20 22 69 6e 64 65 78 20  (pParse, "index 
250a7 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
250a8 55 4e 49 51 55 45 20 22 0a 20 20 20 20 20 20 22  UNIQUE ".      "
250a9 6f 72 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63  or PRIMARY KEY c
250aa 6f 6e 73 74 72 61 69 6e 74 20 63 61 6e 6e 6f 74  onstraint cannot
250ab 20 62 65 20 64 72 6f 70 70 65 64 22 2c 20 30 29   be dropped", 0)
250ac 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  ;.    goto exit_
250ad 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a  drop_index;.  }.
250ae 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
250af 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
250b0 20 70 49 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61   pIndex->pSchema
250b1 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
250b2 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
250b3 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74  TION.  {.    int
250b4 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
250b5 52 4f 50 5f 49 4e 44 45 58 3b 0a 20 20 20 20 54  ROP_INDEX;.    T
250b6 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 6e  able *pTab = pIn
250b7 64 65 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20  dex->pTable;.   
250b8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
250b9 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
250ba 7a 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74  zName;.    const
250bb 20 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43   char *zTab = SC
250bc 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b  HEMA_TABLE(iDb);
250bd 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
250be 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
250bf 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
250c0 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29   zTab, 0, zDb) )
250c1 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69  {.      goto exi
250c2 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20  t_drop_index;.  
250c3 20 20 7d 0a 20 20 20 20 69 66 28 20 21 4f 4d 49    }.    if( !OMI
250c4 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 20  T_TEMPDB && iDb 
250c5 29 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  ) code = SQLITE_
250c6 44 52 4f 50 5f 54 45 4d 50 5f 49 4e 44 45 58 3b  DROP_TEMP_INDEX;
250c7 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
250c8 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
250c9 2c 20 63 6f 64 65 2c 20 70 49 6e 64 65 78 2d 3e  , code, pIndex->
250ca 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61  zName, pTab->zNa
250cb 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20  me, zDb) ){.    
250cc 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
250cd 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20  _index;.    }.  
250ce 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47  }.#endif..  /* G
250cf 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
250d0 72 65 6d 6f 76 65 20 74 68 65 20 69 6e 64 65 78  remove the index
250d1 20 61 6e 64 20 66 72 6f 6d 20 74 68 65 20 6d 61   and from the ma
250d2 73 74 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ster table */.  
250d3 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
250d4 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
250d5 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( v ){.    sqlit
250d6 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72  e3BeginWriteOper
250d7 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c  ation(pParse, 1,
250d8 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74   iDb);.    sqlit
250d9 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50  e3NestedParse(pP
250da 61 72 73 65 2c 0a 20 20 20 20 20 20 20 22 44 45  arse,.       "DE
250db 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73 20  LETE FROM %Q.%s 
250dc 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a  WHERE name=%Q",.
250dd 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69         db->aDb[i
250de 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d  Db].zName, SCHEM
250df 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20  A_TABLE(iDb),.  
250e0 20 20 20 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61       pIndex->zNa
250e1 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66  me.    );.    if
250e2 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  ( sqlite3FindTab
250e3 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73  le(db, "sqlite_s
250e4 74 61 74 31 22 2c 20 64 62 2d 3e 61 44 62 5b 69  tat1", db->aDb[i
250e5 44 62 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20  Db].zName) ){.  
250e6 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
250e7 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
250e8 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46         "DELETE F
250e9 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74  ROM %Q.sqlite_st
250ea 61 74 31 20 57 48 45 52 45 20 69 64 78 3d 25 51  at1 WHERE idx=%Q
250eb 22 2c 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61  ",.        db->a
250ec 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 70  Db[iDb].zName, p
250ed 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20  Index->zName.   
250ee 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20     );.    }.    
250ef 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f  sqlite3ChangeCoo
250f0 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  kie(pParse, iDb)
250f1 3b 0a 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f  ;.    destroyRoo
250f2 74 50 61 67 65 28 70 50 61 72 73 65 2c 20 70 49  tPage(pParse, pI
250f3 6e 64 65 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  ndex->tnum, iDb)
250f4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
250f5 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72  eAddOp4(v, OP_Dr
250f6 6f 70 49 6e 64 65 78 2c 20 69 44 62 2c 20 30 2c  opIndex, iDb, 0,
250f7 20 30 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d   0, pIndex->zNam
250f8 65 2c 20 30 29 3b 0a 20 20 7d 0a 0a 65 78 69 74  e, 0);.  }..exit
250f9 5f 64 72 6f 70 5f 69 6e 64 65 78 3a 0a 20 20 73  _drop_index:.  s
250fa 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
250fb 65 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ete(db, pName);.
250fc 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41 72 72 61 79 20  }../*.** pArray 
250fd 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
250fe 61 6e 20 61 72 72 61 79 20 6f 66 20 6f 62 6a 65  an array of obje
250ff 63 74 73 2e 20 20 45 61 63 68 20 6f 62 6a 65 63  cts.  Each objec
25100 74 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72 72 61  t in the.** arra
25101 79 20 69 73 20 73 7a 45 6e 74 72 79 20 62 79 74  y is szEntry byt
25102 65 73 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 69  es in size.  Thi
25103 73 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61  s routine alloca
25104 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6f 62 6a  tes a new.** obj
25105 65 63 74 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f  ect on the end o
25106 66 20 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a  f the array..**.
25107 2a 2a 20 2a 70 6e 45 6e 74 72 79 20 69 73 20 74  ** *pnEntry is t
25108 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
25109 72 69 65 73 20 61 6c 72 65 61 64 79 20 69 6e 20  ries already in 
2510a 75 73 65 2e 20 20 2a 70 6e 41 6c 6c 6f 63 20 69  use.  *pnAlloc i
2510b 73 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75  s.** the previou
2510c 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 69  sly allocated si
2510d 7a 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 2e  ze of the array.
2510e 20 20 69 6e 69 74 53 69 7a 65 20 69 73 20 74 68    initSize is th
2510f 65 0a 2a 2a 20 73 75 67 67 65 73 74 65 64 20 69  e.** suggested i
25110 6e 69 74 69 61 6c 20 61 72 72 61 79 20 73 69 7a  nitial array siz
25111 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a  e allocation..**
25112 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 6f 66  .** The index of
25113 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69   the new entry i
25114 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70  s returned in *p
25115 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  Idx..**.** This 
25116 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
25117 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
25118 20 61 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74   array of object
25119 73 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 69 67 68  s.  This.** migh
2511a 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73  t be the same as
2511b 20 74 68 65 20 70 41 72 72 61 79 20 70 61 72 61   the pArray para
2511c 6d 65 74 65 72 20 6f 72 20 69 74 20 6d 69 67 68  meter or it migh
2511d 74 20 62 65 20 61 20 64 69 66 66 65 72 65 6e 74  t be a different
2511e 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 66 20 74  .** pointer if t
2511f 68 65 20 61 72 72 61 79 20 77 61 73 20 72 65 73  he array was res
25120 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ized..*/.SQLITE_
25121 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
25122 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61  lite3ArrayAlloca
25123 74 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  te(.  sqlite3 *d
25124 62 2c 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65  b,      /* Conne
25125 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20  ction to notify 
25126 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  of malloc failur
25127 65 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  es */.  void *pA
25128 72 72 61 79 2c 20 20 20 20 20 2f 2a 20 41 72 72  rray,     /* Arr
25129 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20  ay of objects.  
2512a 4d 69 67 68 74 20 62 65 20 72 65 61 6c 6c 6f 63  Might be realloc
2512b 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  ated */.  int sz
2512c 45 6e 74 72 79 2c 20 20 20 20 20 20 2f 2a 20 53  Entry,      /* S
2512d 69 7a 65 20 6f 66 20 65 61 63 68 20 6f 62 6a 65  ize of each obje
2512e 63 74 20 69 6e 20 74 68 65 20 61 72 72 61 79 20  ct in the array 
2512f 2a 2f 0a 20 20 69 6e 74 20 69 6e 69 74 53 69 7a  */.  int initSiz
25130 65 2c 20 20 20 20 20 2f 2a 20 53 75 67 67 65 73  e,     /* Sugges
25131 74 65 64 20 69 6e 69 74 69 61 6c 20 61 6c 6c 6f  ted initial allo
25132 63 61 74 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65  cation, in eleme
25133 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  nts */.  int *pn
25134 45 6e 74 72 79 2c 20 20 20 20 20 2f 2a 20 4e 75  Entry,     /* Nu
25135 6d 62 65 72 20 6f 66 20 6f 62 6a 65 63 74 73 20  mber of objects 
25136 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65  currently in use
25137 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 41 6c 6c   */.  int *pnAll
25138 6f 63 2c 20 20 20 20 20 2f 2a 20 43 75 72 72 65  oc,     /* Curre
25139 6e 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61  nt size of the a
2513a 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 65 6c  llocation, in el
2513b 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  ements */.  int 
2513c 2a 70 49 64 78 20 20 20 20 20 20 20 20 20 2f 2a  *pIdx         /*
2513d 20 57 72 69 74 65 20 74 68 65 20 69 6e 64 65 78   Write the index
2513e 20 6f 66 20 61 20 6e 65 77 20 73 6c 6f 74 20 68   of a new slot h
2513f 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72  ere */.){.  char
25140 20 2a 7a 3b 0a 20 20 69 66 28 20 2a 70 6e 45 6e   *z;.  if( *pnEn
25141 74 72 79 20 3e 3d 20 2a 70 6e 41 6c 6c 6f 63 20  try >= *pnAlloc 
25142 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4e 65  ){.    void *pNe
25143 77 3b 0a 20 20 20 20 69 6e 74 20 6e 65 77 53 69  w;.    int newSi
25144 7a 65 3b 0a 20 20 20 20 6e 65 77 53 69 7a 65 20  ze;.    newSize 
25145 3d 20 28 2a 70 6e 41 6c 6c 6f 63 29 2a 32 20 2b  = (*pnAlloc)*2 +
25146 20 69 6e 69 74 53 69 7a 65 3b 0a 20 20 20 20 70   initSize;.    p
25147 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52  New = sqlite3DbR
25148 65 61 6c 6c 6f 63 28 64 62 2c 20 70 41 72 72 61  ealloc(db, pArra
25149 79 2c 20 6e 65 77 53 69 7a 65 2a 73 7a 45 6e 74  y, newSize*szEnt
2514a 72 79 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65  ry);.    if( pNe
2514b 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70  w==0 ){.      *p
2514c 49 64 78 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  Idx = -1;.      
2514d 72 65 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 20  return pArray;. 
2514e 20 20 20 7d 0a 20 20 20 20 2a 70 6e 41 6c 6c 6f     }.    *pnAllo
2514f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  c = sqlite3DbMal
25150 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70 4e 65 77  locSize(db, pNew
25151 29 2f 73 7a 45 6e 74 72 79 3b 0a 20 20 20 20 70  )/szEntry;.    p
25152 41 72 72 61 79 20 3d 20 70 4e 65 77 3b 0a 20 20  Array = pNew;.  
25153 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70  }.  z = (char*)p
25154 41 72 72 61 79 3b 0a 20 20 6d 65 6d 73 65 74 28  Array;.  memset(
25155 26 7a 5b 2a 70 6e 45 6e 74 72 79 20 2a 20 73 7a  &z[*pnEntry * sz
25156 45 6e 74 72 79 5d 2c 20 30 2c 20 73 7a 45 6e 74  Entry], 0, szEnt
25157 72 79 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20 2a  ry);.  *pIdx = *
25158 70 6e 45 6e 74 72 79 3b 0a 20 20 2b 2b 2a 70 6e  pnEntry;.  ++*pn
25159 45 6e 74 72 79 3b 0a 20 20 72 65 74 75 72 6e 20  Entry;.  return 
2515a 70 41 72 72 61 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pArray;.}../*.**
2515b 20 41 70 70 65 6e 64 20 61 20 6e 65 77 20 65 6c   Append a new el
2515c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 67 69 76  ement to the giv
2515d 65 6e 20 49 64 4c 69 73 74 2e 20 20 43 72 65 61  en IdList.  Crea
2515e 74 65 20 61 20 6e 65 77 20 49 64 4c 69 73 74 20  te a new IdList 
2515f 69 66 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 0a 2a  if.** need be..*
25160 2a 0a 2a 2a 20 41 20 6e 65 77 20 49 64 4c 69 73  *.** A new IdLis
25161 74 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f  t is returned, o
25162 72 20 4e 55 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63  r NULL if malloc
25163 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c  () fails..*/.SQL
25164 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69  ITE_PRIVATE IdLi
25165 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73  st *sqlite3IdLis
25166 74 41 70 70 65 6e 64 28 73 71 6c 69 74 65 33 20  tAppend(sqlite3 
25167 2a 64 62 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69  *db, IdList *pLi
25168 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65  st, Token *pToke
25169 6e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  n){.  int i;.  i
2516a 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20  f( pList==0 ){. 
2516b 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74     pList = sqlit
2516c 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2516d 62 2c 20 73 69 7a 65 6f 66 28 49 64 4c 69 73 74  b, sizeof(IdList
2516e 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69  ) );.    if( pLi
2516f 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  st==0 ) return 0
25170 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c  ;.    pList->nAl
25171 6c 6f 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70  loc = 0;.  }.  p
25172 4c 69 73 74 2d 3e 61 20 3d 20 73 71 6c 69 74 65  List->a = sqlite
25173 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a  3ArrayAllocate(.
25174 20 20 20 20 20 20 64 62 2c 0a 20 20 20 20 20 20        db,.      
25175 70 4c 69 73 74 2d 3e 61 2c 0a 20 20 20 20 20 20  pList->a,.      
25176 73 69 7a 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b  sizeof(pList->a[
25177 30 5d 29 2c 0a 20 20 20 20 20 20 35 2c 0a 20 20  0]),.      5,.  
25178 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e 49 64 2c      &pList->nId,
25179 0a 20 20 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e  .      &pList->n
2517a 41 6c 6c 6f 63 2c 0a 20 20 20 20 20 20 26 69 0a  Alloc,.      &i.
2517b 20 20 29 3b 0a 20 20 69 66 28 20 69 3c 30 20 29    );.  if( i<0 )
2517c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c  {.    sqlite3IdL
2517d 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c  istDelete(db, pL
2517e 69 73 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ist);.    return
2517f 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d   0;.  }.  pList-
25180 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71  >a[i].zName = sq
25181 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
25182 65 6e 28 64 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a  en(db, pToken);.
25183 20 20 72 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a    return pList;.
25184 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
25185 61 6e 20 49 64 4c 69 73 74 2e 0a 2a 2f 0a 53 51  an IdList..*/.SQ
25186 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
25187 64 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44  d sqlite3IdListD
25188 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64  elete(sqlite3 *d
25189 62 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74  b, IdList *pList
2518a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
2518b 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74  ( pList==0 ) ret
2518c 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  urn;.  for(i=0; 
2518d 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b  i<pList->nId; i+
2518e 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  +){.    sqlite3D
2518f 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 2d  bFree(db, pList-
25190 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  >a[i].zName);.  
25191 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
25192 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b  e(db, pList->a);
25193 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
25194 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a  (db, pList);.}..
25195 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
25196 20 69 6e 64 65 78 20 69 6e 20 70 4c 69 73 74 20   index in pList 
25197 6f 66 20 74 68 65 20 69 64 65 6e 74 69 66 69 65  of the identifie
25198 72 20 6e 61 6d 65 64 20 7a 49 64 2e 20 20 52 65  r named zId.  Re
25199 74 75 72 6e 20 2d 31 0a 2a 2a 20 69 66 20 6e 6f  turn -1.** if no
2519a 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49  t found..*/.SQLI
2519b 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2519c 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65  qlite3IdListInde
2519d 78 28 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c  x(IdList *pList,
2519e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2519f 6d 65 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  me){.  int i;.  
251a0 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72  if( pList==0 ) r
251a1 65 74 75 72 6e 20 2d 31 3b 0a 20 20 66 6f 72 28  eturn -1;.  for(
251a2 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49  i=0; i<pList->nI
251a3 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  d; i++){.    if(
251a4 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
251a5 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  pList->a[i].zNam
251a6 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 72  e, zName)==0 ) r
251a7 65 74 75 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72  eturn i;.  }.  r
251a8 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a  eturn -1;.}../*.
251a9 2a 2a 20 45 78 70 61 6e 64 20 74 68 65 20 73 70  ** Expand the sp
251aa 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ace allocated fo
251ab 72 20 74 68 65 20 67 69 76 65 6e 20 53 72 63 4c  r the given SrcL
251ac 69 73 74 20 6f 62 6a 65 63 74 20 62 79 0a 2a 2a  ist object by.**
251ad 20 63 72 65 61 74 69 6e 67 20 6e 45 78 74 72 61   creating nExtra
251ae 20 6e 65 77 20 73 6c 6f 74 73 20 62 65 67 69 6e   new slots begin
251af 6e 69 6e 67 20 61 74 20 69 53 74 61 72 74 2e 20  ning at iStart. 
251b0 20 69 53 74 61 72 74 20 69 73 20 7a 65 72 6f 20   iStart is zero 
251b1 62 61 73 65 64 2e 0a 2a 2a 20 4e 65 77 20 73 6c  based..** New sl
251b2 6f 74 73 20 61 72 65 20 7a 65 72 6f 65 64 2e 0a  ots are zeroed..
251b3 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c  **.** For exampl
251b4 65 2c 20 73 75 70 70 6f 73 65 20 61 20 53 72 63  e, suppose a Src
251b5 4c 69 73 74 20 69 6e 69 74 69 61 6c 6c 79 20 63  List initially c
251b6 6f 6e 74 61 69 6e 73 20 74 77 6f 20 65 6e 74 72  ontains two entr
251b7 69 65 73 3a 20 41 2c 42 2e 0a 2a 2a 20 54 6f 20  ies: A,B..** To 
251b8 61 70 70 65 6e 64 20 33 20 6e 65 77 20 65 6e 74  append 3 new ent
251b9 72 69 65 73 20 6f 6e 74 6f 20 74 68 65 20 65 6e  ries onto the en
251ba 64 2c 20 64 6f 20 74 68 69 73 3a 0a 2a 2a 0a 2a  d, do this:.**.*
251bb 2a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c  *    sqlite3SrcL
251bc 69 73 74 45 6e 6c 61 72 67 65 28 64 62 2c 20 70  istEnlarge(db, p
251bd 53 72 63 6c 69 73 74 2c 20 33 2c 20 32 29 3b 0a  Srclist, 3, 2);.
251be 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20  **.** After the 
251bf 63 61 6c 6c 20 61 62 6f 76 65 20 69 74 20 77 6f  call above it wo
251c0 75 6c 64 20 63 6f 6e 74 61 69 6e 3a 20 20 41 2c  uld contain:  A,
251c1 20 42 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69   B, nil, nil, ni
251c2 6c 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 53 74  l..** If the iSt
251c3 61 72 74 20 61 72 67 75 6d 65 6e 74 20 68 61 64  art argument had
251c4 20 62 65 65 6e 20 31 20 69 6e 73 74 65 61 64 20   been 1 instead 
251c5 6f 66 20 32 2c 20 74 68 65 6e 20 74 68 65 20 72  of 2, then the r
251c6 65 73 75 6c 74 0a 2a 2a 20 77 6f 75 6c 64 20 68  esult.** would h
251c7 61 76 65 20 62 65 65 6e 3a 20 20 41 2c 20 6e 69  ave been:  A, ni
251c8 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 42 2e 20  l, nil, nil, B. 
251c9 20 54 6f 20 70 72 65 70 65 6e 64 20 74 68 65 20   To prepend the 
251ca 6e 65 77 20 73 6c 6f 74 73 2c 0a 2a 2a 20 74 68  new slots,.** th
251cb 65 20 69 53 74 61 72 74 20 76 61 6c 75 65 20 77  e iStart value w
251cc 6f 75 6c 64 20 62 65 20 30 2e 20 20 54 68 65 20  ould be 0.  The 
251cd 72 65 73 75 6c 74 20 74 68 65 6e 20 77 6f 75 6c  result then woul
251ce 64 0a 2a 2a 20 62 65 3a 20 6e 69 6c 2c 20 6e 69  d.** be: nil, ni
251cf 6c 2c 20 6e 69 6c 2c 20 41 2c 20 42 2e 0a 2a 2a  l, nil, A, B..**
251d0 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20  .** If a memory 
251d1 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73  allocation fails
251d2 20 74 68 65 20 53 72 63 4c 69 73 74 20 69 73 20   the SrcList is 
251d3 75 6e 63 68 61 6e 67 65 64 2e 20 20 54 68 65 0a  unchanged.  The.
251d4 2a 2a 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ** db->mallocFai
251d5 6c 65 64 20 66 6c 61 67 20 77 69 6c 6c 20 62 65  led flag will be
251d6 20 73 65 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2f   set to true..*/
251d7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
251d8 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33  SrcList *sqlite3
251d9 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 0a  SrcListEnlarge(.
251da 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
251db 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
251dc 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e   connection to n
251dd 6f 74 69 66 79 20 6f 66 20 4f 4f 4d 20 65 72 72  otify of OOM err
251de 6f 72 73 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ors */.  SrcList
251df 20 2a 70 53 72 63 2c 20 20 20 20 20 2f 2a 20 54   *pSrc,     /* T
251e0 68 65 20 53 72 63 4c 69 73 74 20 74 6f 20 62 65  he SrcList to be
251e1 20 65 6e 6c 61 72 67 65 64 20 2a 2f 0a 20 20 69   enlarged */.  i
251e2 6e 74 20 6e 45 78 74 72 61 2c 20 20 20 20 20 20  nt nExtra,      
251e3 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e    /* Number of n
251e4 65 77 20 73 6c 6f 74 73 20 74 6f 20 61 64 64 20  ew slots to add 
251e5 74 6f 20 70 53 72 63 2d 3e 61 5b 5d 20 2a 2f 0a  to pSrc->a[] */.
251e6 20 20 69 6e 74 20 69 53 74 61 72 74 20 20 20 20    int iStart    
251e7 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
251e8 20 70 53 72 63 2d 3e 61 5b 5d 20 6f 66 20 66 69   pSrc->a[] of fi
251e9 72 73 74 20 6e 65 77 20 73 6c 6f 74 20 2a 2f 0a  rst new slot */.
251ea 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 2f  ){.  int i;..  /
251eb 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  * Sanity checkin
251ec 67 20 6f 6e 20 63 61 6c 6c 69 6e 67 20 70 61 72  g on calling par
251ed 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73  ameters */.  ass
251ee 65 72 74 28 20 69 53 74 61 72 74 3e 3d 30 20 29  ert( iStart>=0 )
251ef 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 45 78 74  ;.  assert( nExt
251f0 72 61 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72  ra>=1 );.  asser
251f1 74 28 20 70 53 72 63 21 3d 30 20 29 3b 0a 20 20  t( pSrc!=0 );.  
251f2 61 73 73 65 72 74 28 20 69 53 74 61 72 74 3c 3d  assert( iStart<=
251f3 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 0a 20  pSrc->nSrc );.. 
251f4 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 64 64   /* Allocate add
251f5 69 74 69 6f 6e 61 6c 20 73 70 61 63 65 20 69 66  itional space if
251f6 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 69 66 28   needed */.  if(
251f7 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e 45 78 74   pSrc->nSrc+nExt
251f8 72 61 3e 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20  ra>pSrc->nAlloc 
251f9 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a  ){.    SrcList *
251fa 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e 41  pNew;.    int nA
251fb 6c 6c 6f 63 20 3d 20 70 53 72 63 2d 3e 6e 53 72  lloc = pSrc->nSr
251fc 63 2b 6e 45 78 74 72 61 3b 0a 20 20 20 20 69 6e  c+nExtra;.    in
251fd 74 20 6e 47 6f 74 3b 0a 20 20 20 20 70 4e 65 77  t nGot;.    pNew
251fe 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
251ff 6c 6f 63 28 64 62 2c 20 70 53 72 63 2c 0a 20 20  loc(db, pSrc,.  
25200 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69 7a               siz
25201 65 6f 66 28 2a 70 53 72 63 29 20 2b 20 28 6e 41  eof(*pSrc) + (nA
25202 6c 6c 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70  lloc-1)*sizeof(p
25203 53 72 63 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20  Src->a[0]) );.  
25204 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
25205 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
25206 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
25207 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
25208 70 53 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  pSrc;.    }.    
25209 70 53 72 63 20 3d 20 70 4e 65 77 3b 0a 20 20 20  pSrc = pNew;.   
2520a 20 6e 47 6f 74 20 3d 20 28 73 71 6c 69 74 65 33   nGot = (sqlite3
2520b 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c  DbMallocSize(db,
2520c 20 70 4e 65 77 29 20 2d 20 73 69 7a 65 6f 66 28   pNew) - sizeof(
2520d 2a 70 53 72 63 29 29 2f 73 69 7a 65 6f 66 28 70  *pSrc))/sizeof(p
2520e 53 72 63 2d 3e 61 5b 30 5d 29 2b 31 3b 0a 20 20  Src->a[0])+1;.  
2520f 20 20 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 3d    pSrc->nAlloc =
25210 20 28 75 31 36 29 6e 47 6f 74 3b 0a 20 20 7d 0a   (u16)nGot;.  }.
25211 0a 20 20 2f 2a 20 4d 6f 76 65 20 65 78 69 73 74  .  /* Move exist
25212 69 6e 67 20 73 6c 6f 74 73 20 74 68 61 74 20 63  ing slots that c
25213 6f 6d 65 20 61 66 74 65 72 20 74 68 65 20 6e 65  ome after the ne
25214 77 6c 79 20 69 6e 73 65 72 74 65 64 20 73 6c 6f  wly inserted slo
25215 74 73 0a 20 20 2a 2a 20 6f 75 74 20 6f 66 20 74  ts.  ** out of t
25216 68 65 20 77 61 79 20 2a 2f 0a 20 20 66 6f 72 28  he way */.  for(
25217 69 3d 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b 20  i=pSrc->nSrc-1; 
25218 69 3e 3d 69 53 74 61 72 74 3b 20 69 2d 2d 29 7b  i>=iStart; i--){
25219 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b 6e  .    pSrc->a[i+n
2521a 45 78 74 72 61 5d 20 3d 20 70 53 72 63 2d 3e 61  Extra] = pSrc->a
2521b 5b 69 5d 3b 0a 20 20 7d 0a 20 20 70 53 72 63 2d  [i];.  }.  pSrc-
2521c 3e 6e 53 72 63 20 2b 3d 20 28 69 31 36 29 6e 45  >nSrc += (i16)nE
2521d 78 74 72 61 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f  xtra;..  /* Zero
2521e 20 74 68 65 20 6e 65 77 6c 79 20 61 6c 6c 6f 63   the newly alloc
2521f 61 74 65 64 20 73 6c 6f 74 73 20 2a 2f 0a 20 20  ated slots */.  
25220 6d 65 6d 73 65 74 28 26 70 53 72 63 2d 3e 61 5b  memset(&pSrc->a[
25221 69 53 74 61 72 74 5d 2c 20 30 2c 20 73 69 7a 65  iStart], 0, size
25222 6f 66 28 70 53 72 63 2d 3e 61 5b 30 5d 29 2a 6e  of(pSrc->a[0])*n
25223 45 78 74 72 61 29 3b 0a 20 20 66 6f 72 28 69 3d  Extra);.  for(i=
25224 69 53 74 61 72 74 3b 20 69 3c 69 53 74 61 72 74  iStart; i<iStart
25225 2b 6e 45 78 74 72 61 3b 20 69 2b 2b 29 7b 0a 20  +nExtra; i++){. 
25226 20 20 20 70 53 72 63 2d 3e 61 5b 69 5d 2e 69 43     pSrc->a[i].iC
25227 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 7d 0a  ursor = -1;.  }.
25228 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70  .  /* Return a p
25229 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e  ointer to the en
2522a 6c 61 72 67 65 64 20 53 72 63 4c 69 73 74 20 2a  larged SrcList *
2522b 2f 0a 20 20 72 65 74 75 72 6e 20 70 53 72 63 3b  /.  return pSrc;
2522c 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e  .}.../*.** Appen
2522d 64 20 61 20 6e 65 77 20 74 61 62 6c 65 20 6e 61  d a new table na
2522e 6d 65 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20  me to the given 
2522f 53 72 63 4c 69 73 74 2e 20 20 43 72 65 61 74 65  SrcList.  Create
25230 20 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 69   a new SrcList i
25231 66 0a 2a 2a 20 6e 65 65 64 20 62 65 2e 20 20 41  f.** need be.  A
25232 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 63 72   new entry is cr
25233 65 61 74 65 64 20 69 6e 20 74 68 65 20 53 72 63  eated in the Src
25234 4c 69 73 74 20 65 76 65 6e 20 69 66 20 70 54 61  List even if pTa
25235 62 6c 65 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ble is NULL..**.
25236 2a 2a 20 41 20 53 72 63 4c 69 73 74 20 69 73 20  ** A SrcList is 
25237 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e 55 4c  returned, or NUL
25238 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  L if there is an
25239 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 54 68 65   OOM error.  The
2523a 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 53 72 63   returned.** Src
2523b 4c 69 73 74 20 6d 69 67 68 74 20 62 65 20 74 68  List might be th
2523c 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 53 72  e same as the Sr
2523d 63 4c 69 73 74 20 74 68 61 74 20 77 61 73 20 69  cList that was i
2523e 6e 70 75 74 20 6f 72 20 69 74 20 6d 69 67 68 74  nput or it might
2523f 20 62 65 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e 65   be.** a new one
25240 2e 20 20 49 66 20 61 6e 20 4f 4f 4d 20 65 72 72  .  If an OOM err
25241 6f 72 20 64 6f 65 73 20 6f 63 63 75 72 73 2c 20  or does occurs, 
25242 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 76  then the prior v
25243 61 6c 75 65 20 6f 66 20 70 4c 69 73 74 0a 2a 2a  alue of pList.**
25244 20 74 68 61 74 20 69 73 20 69 6e 70 75 74 20 74   that is input t
25245 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  o this routine i
25246 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
25247 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  freed..**.** If 
25248 70 44 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74  pDatabase is not
25249 20 6e 75 6c 6c 2c 20 69 74 20 6d 65 61 6e 73 20   null, it means 
2524a 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 68  that the table h
2524b 61 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 0a 2a  as an optional.*
2524c 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  * database name 
2524d 70 72 65 66 69 78 2e 20 20 4c 69 6b 65 20 74 68  prefix.  Like th
2524e 69 73 3a 20 20 22 64 61 74 61 62 61 73 65 2e 74  is:  "database.t
2524f 61 62 6c 65 22 2e 20 20 54 68 65 20 70 44 61 74  able".  The pDat
25250 61 62 61 73 65 0a 2a 2a 20 70 6f 69 6e 74 73 20  abase.** points 
25251 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  to the table nam
25252 65 20 61 6e 64 20 74 68 65 20 70 54 61 62 6c 65  e and the pTable
25253 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 64   points to the d
25254 61 74 61 62 61 73 65 20 6e 61 6d 65 2e 0a 2a 2a  atabase name..**
25255 20 54 68 65 20 53 72 63 4c 69 73 74 2e 61 5b 5d   The SrcList.a[]
25256 2e 7a 4e 61 6d 65 20 66 69 65 6c 64 20 69 73 20  .zName field is 
25257 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20  filled with the 
25258 74 61 62 6c 65 20 6e 61 6d 65 20 77 68 69 63 68  table name which
25259 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6d 65 20 66   might.** come f
2525a 72 6f 6d 20 70 54 61 62 6c 65 20 28 69 66 20 70  rom pTable (if p
2525b 44 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c 4c  Database is NULL
2525c 29 20 6f 72 20 66 72 6f 6d 20 70 44 61 74 61 62  ) or from pDatab
2525d 61 73 65 2e 20 20 0a 2a 2a 20 53 72 63 4c 69 73  ase.  .** SrcLis
2525e 74 2e 61 5b 5d 2e 7a 44 61 74 61 62 61 73 65 20  t.a[].zDatabase 
2525f 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68 20 74  is filled with t
25260 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
25261 20 66 72 6f 6d 20 70 54 61 62 6c 65 2c 0a 2a 2a   from pTable,.**
25262 20 6f 72 20 77 69 74 68 20 4e 55 4c 4c 20 69 66   or with NULL if
25263 20 6e 6f 20 64 61 74 61 62 61 73 65 20 69 73 20   no database is 
25264 73 70 65 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a  specified..**.**
25265 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
25266 20 69 66 20 63 61 6c 6c 20 6c 69 6b 65 20 74 68   if call like th
25267 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  is:.**.**       
25268 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
25269 41 70 70 65 6e 64 28 44 2c 41 2c 42 2c 30 29 3b  Append(D,A,B,0);
2526a 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 42 20 69 73  .**.** Then B is
2526b 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e   a table name an
2526c 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  d the database n
2526d 61 6d 65 20 69 73 20 75 6e 73 70 65 63 69 66 69  ame is unspecifi
2526e 65 64 2e 20 20 49 66 20 63 61 6c 6c 65 64 0a 2a  ed.  If called.*
2526f 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  * like this:.**.
25270 2a 2a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  **         sqlit
25271 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
25272 44 2c 41 2c 42 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20  D,A,B,C);.**.** 
25273 54 68 65 6e 20 43 20 69 73 20 74 68 65 20 74 61  Then C is the ta
25274 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 42 20 69  ble name and B i
25275 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e  s the database n
25276 61 6d 65 2e 20 20 49 66 20 43 20 69 73 20 64 65  ame.  If C is de
25277 66 69 6e 65 64 0a 2a 2a 20 74 68 65 6e 20 73 6f  fined.** then so
25278 20 69 73 20 42 2e 20 20 49 6e 20 6f 74 68 65 72   is B.  In other
25279 20 77 6f 72 64 73 2c 20 77 65 20 6e 65 76 65 72   words, we never
2527a 20 68 61 76 65 20 61 20 63 61 73 65 20 77 68 65   have a case whe
2527b 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  re:.**.**       
2527c 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
2527d 41 70 70 65 6e 64 28 44 2c 41 2c 30 2c 43 29 3b  Append(D,A,0,C);
2527e 0a 2a 2a 0a 2a 2a 20 42 6f 74 68 20 70 54 61 62  .**.** Both pTab
2527f 6c 65 20 61 6e 64 20 70 44 61 74 61 62 61 73 65  le and pDatabase
25280 20 61 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20   are assumed to 
25281 62 65 20 71 75 6f 74 65 64 2e 20 20 54 68 65 79  be quoted.  They
25282 20 61 72 65 20 64 65 71 75 6f 74 65 64 0a 2a 2a   are dequoted.**
25283 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 61 64   before being ad
25284 64 65 64 20 74 6f 20 74 68 65 20 53 72 63 4c 69  ded to the SrcLi
25285 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  st..*/.SQLITE_PR
25286 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
25287 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
25288 65 6e 64 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  end(.  sqlite3 *
25289 64 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  db,        /* Co
2528a 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69  nnection to noti
2528b 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69  fy of malloc fai
2528c 6c 75 72 65 73 20 2a 2f 0a 20 20 53 72 63 4c 69  lures */.  SrcLi
2528d 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 2f  st *pList,     /
2528e 2a 20 41 70 70 65 6e 64 20 74 6f 20 74 68 69 73  * Append to this
2528f 20 53 72 63 4c 69 73 74 2e 20 4e 55 4c 4c 20 63   SrcList. NULL c
25290 72 65 61 74 65 73 20 61 20 6e 65 77 20 53 72 63  reates a new Src
25291 4c 69 73 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  List */.  Token 
25292 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a  *pTable,      /*
25293 20 54 61 62 6c 65 20 74 6f 20 61 70 70 65 6e 64   Table to append
25294 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61   */.  Token *pDa
25295 74 61 62 61 73 65 20 20 20 20 2f 2a 20 44 61 74  tabase    /* Dat
25296 61 62 61 73 65 20 6f 66 20 74 68 65 20 74 61 62  abase of the tab
25297 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63  le */.){.  struc
25298 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a  t SrcList_item *
25299 70 49 74 65 6d 3b 0a 20 20 61 73 73 65 72 74 28  pItem;.  assert(
2529a 20 70 44 61 74 61 62 61 73 65 3d 3d 30 20 7c 7c   pDatabase==0 ||
2529b 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 20 20 2f   pTable!=0 );  /
2529c 2a 20 43 61 6e 6e 6f 74 20 68 61 76 65 20 43 20  * Cannot have C 
2529d 77 69 74 68 6f 75 74 20 42 20 2a 2f 0a 20 20 69  without B */.  i
2529e 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20  f( pList==0 ){. 
2529f 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74     pList = sqlit
252a0 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
252a1 62 2c 20 73 69 7a 65 6f 66 28 53 72 63 4c 69 73  b, sizeof(SrcLis
252a2 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  t) );.    if( pL
252a3 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ist==0 ) return 
252a4 30 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41  0;.    pList->nA
252a5 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  lloc = 1;.  }.  
252a6 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 53  pList = sqlite3S
252a7 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62  rcListEnlarge(db
252a8 2c 20 70 4c 69 73 74 2c 20 31 2c 20 70 4c 69 73  , pList, 1, pLis
252a9 74 2d 3e 6e 53 72 63 29 3b 0a 20 20 69 66 28 20  t->nSrc);.  if( 
252aa 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
252ab 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
252ac 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
252ad 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 72 65 74   pList);.    ret
252ae 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49 74  urn 0;.  }.  pIt
252af 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70  em = &pList->a[p
252b0 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20  List->nSrc-1];. 
252b1 20 69 66 28 20 70 44 61 74 61 62 61 73 65 20 26   if( pDatabase &
252b2 26 20 70 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d  & pDatabase->z==
252b3 30 20 29 7b 0a 20 20 20 20 70 44 61 74 61 62 61  0 ){.    pDataba
252b4 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  se = 0;.  }.  if
252b5 28 20 70 44 61 74 61 62 61 73 65 20 29 7b 0a 20  ( pDatabase ){. 
252b6 20 20 20 54 6f 6b 65 6e 20 2a 70 54 65 6d 70 20     Token *pTemp 
252b7 3d 20 70 44 61 74 61 62 61 73 65 3b 0a 20 20 20  = pDatabase;.   
252b8 20 70 44 61 74 61 62 61 73 65 20 3d 20 70 54 61   pDatabase = pTa
252b9 62 6c 65 3b 0a 20 20 20 20 70 54 61 62 6c 65 20  ble;.    pTable 
252ba 3d 20 70 54 65 6d 70 3b 0a 20 20 7d 0a 20 20 70  = pTemp;.  }.  p
252bb 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71  Item->zName = sq
252bc 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
252bd 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a  en(db, pTable);.
252be 20 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61    pItem->zDataba
252bf 73 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  se = sqlite3Name
252c0 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44  FromToken(db, pD
252c1 61 74 61 62 61 73 65 29 3b 0a 20 20 72 65 74 75  atabase);.  retu
252c2 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a  rn pList;.}../*.
252c3 2a 2a 20 41 73 73 69 67 6e 20 56 64 62 65 43 75  ** Assign VdbeCu
252c4 72 73 6f 72 20 69 6e 64 65 78 20 6e 75 6d 62 65  rsor index numbe
252c5 72 73 20 74 6f 20 61 6c 6c 20 74 61 62 6c 65 73  rs to all tables
252c6 20 69 6e 20 61 20 53 72 63 4c 69 73 74 0a 2a 2f   in a SrcList.*/
252c7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
252c8 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c  void sqlite3SrcL
252c9 69 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73  istAssignCursors
252ca 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
252cb 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b  SrcList *pList){
252cc 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75  .  int i;.  stru
252cd 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
252ce 2a 70 49 74 65 6d 3b 0a 20 20 61 73 73 65 72 74  *pItem;.  assert
252cf 28 70 4c 69 73 74 20 7c 7c 20 70 50 61 72 73 65  (pList || pParse
252d0 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
252d1 65 64 20 29 3b 0a 20 20 69 66 28 20 70 4c 69 73  ed );.  if( pLis
252d2 74 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t ){.    for(i=0
252d3 2c 20 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  , pItem=pList->a
252d4 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b  ; i<pList->nSrc;
252d5 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i++, pItem++){.
252d6 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d        if( pItem-
252d7 3e 69 43 75 72 73 6f 72 3e 3d 30 20 29 20 62 72  >iCursor>=0 ) br
252d8 65 61 6b 3b 0a 20 20 20 20 20 20 70 49 74 65 6d  eak;.      pItem
252d9 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70 50 61 72  ->iCursor = pPar
252da 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
252db 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 70 53 65    if( pItem->pSe
252dc 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20  lect ){.        
252dd 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73  sqlite3SrcListAs
252de 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72  signCursors(pPar
252df 73 65 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65  se, pItem->pSele
252e0 63 74 2d 3e 70 53 72 63 29 3b 0a 20 20 20 20 20  ct->pSrc);.     
252e1 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a   }.    }.  }.}..
252e2 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20  /*.** Delete an 
252e3 65 6e 74 69 72 65 20 53 72 63 4c 69 73 74 20 69  entire SrcList i
252e4 6e 63 6c 75 64 69 6e 67 20 61 6c 6c 20 69 74 73  ncluding all its
252e5 20 73 75 62 73 74 72 75 63 74 75 72 65 2e 0a 2a   substructure..*
252e6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
252e7 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72 63   void sqlite3Src
252e8 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74  ListDelete(sqlit
252e9 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 20  e3 *db, SrcList 
252ea 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69  *pList){.  int i
252eb 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  ;.  struct SrcLi
252ec 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
252ed 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
252ee 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70   return;.  for(p
252ef 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69  Item=pList->a, i
252f0 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72  =0; i<pList->nSr
252f1 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  c; i++, pItem++)
252f2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
252f3 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a  ree(db, pItem->z
252f4 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 73  Database);.    s
252f5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
252f6 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a   pItem->zName);.
252f7 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
252f8 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c  e(db, pItem->zAl
252f9 69 61 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ias);.    sqlite
252fa 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65  3DbFree(db, pIte
252fb 6d 2d 3e 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20  m->zIndex);.    
252fc 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62  sqlite3DeleteTab
252fd 6c 65 28 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b  le(pItem->pTab);
252fe 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65  .    sqlite3Sele
252ff 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74  ctDelete(db, pIt
25300 65 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  em->pSelect);.  
25301 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
25302 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70  ete(db, pItem->p
25303 4f 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  On);.    sqlite3
25304 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  IdListDelete(db,
25305 20 70 49 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b   pItem->pUsing);
25306 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
25307 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74 29 3b  Free(db, pList);
25308 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
25309 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
2530a 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74   by the parser t
2530b 6f 20 61 64 64 20 61 20 6e 65 77 20 74 65 72 6d  o add a new term
2530c 20 74 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f   to the.** end o
2530d 66 20 61 20 67 72 6f 77 69 6e 67 20 46 52 4f 4d  f a growing FROM
2530e 20 63 6c 61 75 73 65 2e 20 20 54 68 65 20 22 70   clause.  The "p
2530f 22 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  " parameter is t
25310 68 65 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68  he part of.** th
25311 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 68  e FROM clause th
25312 61 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  at has already b
25313 65 65 6e 20 63 6f 6e 73 74 72 75 63 74 65 64 2e  een constructed.
25314 20 20 22 70 22 20 69 73 20 4e 55 4c 4c 0a 2a 2a    "p" is NULL.**
25315 20 69 66 20 74 68 69 73 20 69 73 20 74 68 65 20   if this is the 
25316 66 69 72 73 74 20 74 65 72 6d 20 6f 66 20 74 68  first term of th
25317 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20  e FROM clause.  
25318 70 54 61 62 6c 65 20 61 6e 64 20 70 44 61 74 61  pTable and pData
25319 62 61 73 65 0a 2a 2a 20 61 72 65 20 74 68 65 20  base.** are the 
2531a 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
2531b 65 20 61 6e 64 20 64 61 74 61 62 61 73 65 20 6e  e and database n
2531c 61 6d 65 64 20 69 6e 20 74 68 65 20 46 52 4f 4d  amed in the FROM
2531d 20 63 6c 61 75 73 65 20 74 65 72 6d 2e 0a 2a 2a   clause term..**
2531e 20 70 44 61 74 61 62 61 73 65 20 69 73 20 4e 55   pDatabase is NU
2531f 4c 4c 20 69 66 20 74 68 65 20 64 61 74 61 62 61  LL if the databa
25320 73 65 20 6e 61 6d 65 20 71 75 61 6c 69 66 69 65  se name qualifie
25321 72 20 69 73 20 6d 69 73 73 69 6e 67 20 2d 20 74  r is missing - t
25322 68 65 0a 2a 2a 20 75 73 75 61 6c 20 63 61 73 65  he.** usual case
25323 2e 20 20 49 66 20 74 68 65 20 74 65 72 6d 20 68  .  If the term h
25324 61 73 20 61 20 61 6c 69 61 73 2c 20 74 68 65 6e  as a alias, then
25325 20 70 41 6c 69 61 73 20 70 6f 69 6e 74 73 20 74   pAlias points t
25326 6f 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73 20 74  o the.** alias t
25327 6f 6b 65 6e 2e 20 20 49 66 20 74 68 65 20 74 65  oken.  If the te
25328 72 6d 20 69 73 20 61 20 73 75 62 71 75 65 72 79  rm is a subquery
25329 2c 20 74 68 65 6e 20 70 53 75 62 71 75 65 72 79  , then pSubquery
2532a 20 69 73 20 74 68 65 0a 2a 2a 20 53 45 4c 45 43   is the.** SELEC
2532b 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  T statement that
2532c 20 74 68 65 20 73 75 62 71 75 65 72 79 20 65 6e   the subquery en
2532d 63 6f 64 65 73 2e 20 20 54 68 65 20 70 54 61 62  codes.  The pTab
2532e 6c 65 20 61 6e 64 0a 2a 2a 20 70 44 61 74 61 62  le and.** pDatab
2532f 61 73 65 20 70 61 72 61 6d 65 74 65 72 73 20 61  ase parameters a
25330 72 65 20 4e 55 4c 4c 20 66 6f 72 20 73 75 62 71  re NULL for subq
25331 75 65 72 69 65 73 2e 20 20 54 68 65 20 70 4f 6e  ueries.  The pOn
25332 20 61 6e 64 20 70 55 73 69 6e 67 0a 2a 2a 20 70   and pUsing.** p
25333 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 74 68  arameters are th
25334 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
25335 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c   ON and USING cl
25336 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  auses..**.** Ret
25337 75 72 6e 20 61 20 6e 65 77 20 53 72 63 4c 69 73  urn a new SrcLis
25338 74 20 77 68 69 63 68 20 65 6e 63 6f 64 65 73 20  t which encodes 
25339 69 73 20 74 68 65 20 46 52 4f 4d 20 77 69 74 68  is the FROM with
2533a 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74 65 72 6d   the new.** term
2533b 20 61 64 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54   added..*/.SQLIT
2533c 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73  E_PRIVATE SrcLis
2533d 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73  t *sqlite3SrcLis
2533e 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28  tAppendFromTerm(
2533f 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
25340 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
25341 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
25342 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 20  .  SrcList *p,  
25343 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
25344 65 20 6c 65 66 74 20 70 61 72 74 20 6f 66 20 74  e left part of t
25345 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61  he FROM clause a
25346 6c 72 65 61 64 79 20 73 65 65 6e 20 2a 2f 0a 20  lready seen */. 
25347 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20   Token *pTable, 
25348 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
25349 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
2534a 20 61 64 64 20 74 6f 20 74 68 65 20 46 52 4f 4d   add to the FROM
2534b 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 54 6f 6b   clause */.  Tok
2534c 65 6e 20 2a 70 44 61 74 61 62 61 73 65 2c 20 20  en *pDatabase,  
2534d 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2534e 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
2534f 74 61 69 6e 69 6e 67 20 70 54 61 62 6c 65 20 2a  taining pTable *
25350 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 41 6c 69 61  /.  Token *pAlia
25351 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  s,          /* T
25352 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
25353 64 65 20 6f 66 20 74 68 65 20 41 53 20 73 75 62  de of the AS sub
25354 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20  expression */.  
25355 53 65 6c 65 63 74 20 2a 70 53 75 62 71 75 65 72  Select *pSubquer
25356 79 2c 20 20 20 20 20 20 2f 2a 20 41 20 73 75 62  y,      /* A sub
25357 71 75 65 72 79 20 75 73 65 64 20 69 6e 20 70 6c  query used in pl
25358 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6e  ace of a table n
25359 61 6d 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ame */.  Expr *p
2535a 4f 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  On,             
2535b 20 2f 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 73   /* The ON claus
2535c 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20  e of a join */. 
2535d 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 20   IdList *pUsing 
2535e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2535f 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20  USING clause of 
25360 61 20 6a 6f 69 6e 20 2a 2f 0a 29 7b 0a 20 20 73  a join */.){.  s
25361 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
25362 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 73 71 6c  em *pItem;.  sql
25363 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
25364 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 21 70 20  e->db;.  if( !p 
25365 26 26 20 28 70 4f 6e 20 7c 7c 20 70 55 73 69 6e  && (pOn || pUsin
25366 67 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  g) ){.    sqlite
25367 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
25368 2c 20 22 61 20 4a 4f 49 4e 20 63 6c 61 75 73 65  , "a JOIN clause
25369 20 69 73 20 72 65 71 75 69 72 65 64 20 62 65 66   is required bef
2536a 6f 72 65 20 25 73 22 2c 20 0a 20 20 20 20 20 20  ore %s", .      
2536b 28 70 4f 6e 20 3f 20 22 4f 4e 22 20 3a 20 22 55  (pOn ? "ON" : "U
2536c 53 49 4e 47 22 29 0a 20 20 20 20 29 3b 0a 20 20  SING").    );.  
2536d 20 20 67 6f 74 6f 20 61 70 70 65 6e 64 5f 66 72    goto append_fr
2536e 6f 6d 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20  om_error;.  }.  
2536f 70 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  p = sqlite3SrcLi
25370 73 74 41 70 70 65 6e 64 28 64 62 2c 20 70 2c 20  stAppend(db, p, 
25371 70 54 61 62 6c 65 2c 20 70 44 61 74 61 62 61 73  pTable, pDatabas
25372 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c  e);.  if( p==0 |
25373 7c 20 4e 45 56 45 52 28 70 2d 3e 6e 53 72 63 3d  | NEVER(p->nSrc=
25374 3d 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  =0) ){.    goto 
25375 61 70 70 65 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f  append_from_erro
25376 72 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20 3d  r;.  }.  pItem =
25377 20 26 70 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31   &p->a[p->nSrc-1
25378 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 41 6c  ];.  assert( pAl
25379 69 61 73 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  ias!=0 );.  if( 
2537a 70 41 6c 69 61 73 2d 3e 6e 20 29 7b 0a 20 20 20  pAlias->n ){.   
2537b 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d   pItem->zAlias =
2537c 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
2537d 54 6f 6b 65 6e 28 64 62 2c 20 70 41 6c 69 61 73  Token(db, pAlias
2537e 29 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e  );.  }.  pItem->
2537f 70 53 65 6c 65 63 74 20 3d 20 70 53 75 62 71 75  pSelect = pSubqu
25380 65 72 79 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 4f  ery;.  pItem->pO
25381 6e 20 3d 20 70 4f 6e 3b 0a 20 20 70 49 74 65 6d  n = pOn;.  pItem
25382 2d 3e 70 55 73 69 6e 67 20 3d 20 70 55 73 69 6e  ->pUsing = pUsin
25383 67 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a  g;.  return p;..
25384 20 61 70 70 65 6e 64 5f 66 72 6f 6d 5f 65 72 72   append_from_err
25385 6f 72 3a 0a 20 20 61 73 73 65 72 74 28 20 70 3d  or:.  assert( p=
25386 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  =0 );.  sqlite3E
25387 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4f  xprDelete(db, pO
25388 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c  n);.  sqlite3IdL
25389 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 55  istDelete(db, pU
2538a 73 69 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65 33  sing);.  sqlite3
2538b 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
2538c 20 70 53 75 62 71 75 65 72 79 29 3b 0a 20 20 72   pSubquery);.  r
2538d 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
2538e 2a 20 41 64 64 20 61 6e 20 49 4e 44 45 58 45 44  * Add an INDEXED
2538f 20 42 59 20 6f 72 20 4e 4f 54 20 49 4e 44 45 58   BY or NOT INDEX
25390 45 44 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65  ED clause to the
25391 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 61   most recently a
25392 64 64 65 64 20 0a 2a 2a 20 65 6c 65 6d 65 6e 74  dded .** element
25393 20 6f 66 20 74 68 65 20 73 6f 75 72 63 65 2d 6c   of the source-l
25394 69 73 74 20 70 61 73 73 65 64 20 61 73 20 74 68  ist passed as th
25395 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
25396 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
25397 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
25398 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42  3SrcListIndexedB
25399 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  y(Parse *pParse,
2539a 20 53 72 63 4c 69 73 74 20 2a 70 2c 20 54 6f 6b   SrcList *p, Tok
2539b 65 6e 20 2a 70 49 6e 64 65 78 65 64 42 79 29 7b  en *pIndexedBy){
2539c 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 64 65  .  assert( pInde
2539d 78 65 64 42 79 21 3d 30 20 29 3b 0a 20 20 69 66  xedBy!=0 );.  if
2539e 28 20 70 20 26 26 20 41 4c 57 41 59 53 28 70 2d  ( p && ALWAYS(p-
2539f 3e 6e 53 72 63 3e 30 29 20 29 7b 0a 20 20 20 20  >nSrc>0) ){.    
253a0 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
253a1 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d  tem *pItem = &p-
253a2 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20  >a[p->nSrc-1];. 
253a3 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d     assert( pItem
253a4 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20  ->notIndexed==0 
253a5 26 26 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78  && pItem->zIndex
253a6 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70  ==0 );.    if( p
253a7 49 6e 64 65 78 65 64 42 79 2d 3e 6e 3d 3d 31 20  IndexedBy->n==1 
253a8 26 26 20 21 70 49 6e 64 65 78 65 64 42 79 2d 3e  && !pIndexedBy->
253a9 7a 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20  z ){.      /* A 
253aa 22 4e 4f 54 20 49 4e 44 45 58 45 44 22 20 63 6c  "NOT INDEXED" cl
253ab 61 75 73 65 20 77 61 73 20 73 75 70 70 6c 69 65  ause was supplie
253ac 64 2e 20 53 65 65 20 70 61 72 73 65 2e 79 20 0a  d. See parse.y .
253ad 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 75        ** constru
253ae 63 74 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 22  ct "indexed_opt"
253af 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f   for details. */
253b0 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 6e 6f  .      pItem->no
253b1 74 49 6e 64 65 78 65 64 20 3d 20 31 3b 0a 20 20  tIndexed = 1;.  
253b2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
253b3 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73  Item->zIndex = s
253b4 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
253b5 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ken(pParse->db, 
253b6 70 49 6e 64 65 78 65 64 42 79 29 3b 0a 20 20 20  pIndexedBy);.   
253b7 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
253b8 57 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 75 70  When building up
253b9 20 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69   a FROM clause i
253ba 6e 20 74 68 65 20 70 61 72 73 65 72 2c 20 74 68  n the parser, th
253bb 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a  e join operator.
253bc 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20  ** is initially 
253bd 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20  attached to the 
253be 6c 65 66 74 20 6f 70 65 72 61 6e 64 2e 20 20 42  left operand.  B
253bf 75 74 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65  ut the code gene
253c0 72 61 74 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73  rator.** expects
253c1 20 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74   the join operat
253c2 6f 72 20 74 6f 20 62 65 20 6f 6e 20 74 68 65 20  or to be on the 
253c3 72 69 67 68 74 20 6f 70 65 72 61 6e 64 2e 20 20  right operand.  
253c4 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  This routine.** 
253c5 53 68 69 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20  Shifts all join 
253c6 6f 70 65 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c  operators from l
253c7 65 66 74 20 74 6f 20 72 69 67 68 74 20 66 6f 72  eft to right for
253c8 20 61 6e 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a   an entire FROM.
253c9 2a 2a 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  ** clause..**.**
253ca 20 45 78 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73   Example: Suppos
253cb 65 20 74 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69  e the join is li
253cc 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
253cd 20 20 20 20 20 20 20 20 20 41 20 6e 61 74 75 72           A natur
253ce 61 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a  al cross join B.
253cf 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 65 72 61 74  **.** The operat
253d0 6f 72 20 69 73 20 22 6e 61 74 75 72 61 6c 20 63  or is "natural c
253d1 72 6f 73 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65  ross join".  The
253d2 20 41 20 61 6e 64 20 42 20 6f 70 65 72 61 6e 64   A and B operand
253d3 73 20 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20  s are stored.** 
253d4 69 6e 20 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70  in p->a[0] and p
253d5 2d 3e 61 5b 31 5d 2c 20 72 65 73 70 65 63 74 69  ->a[1], respecti
253d6 76 65 6c 79 2e 20 20 54 68 65 20 70 61 72 73 65  vely.  The parse
253d7 72 20 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72  r initially stor
253d8 65 73 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74  es the.** operat
253d9 6f 72 20 77 69 74 68 20 41 2e 20 20 54 68 69 73  or with A.  This
253da 20 72 6f 75 74 69 6e 65 20 73 68 69 66 74 73 20   routine shifts 
253db 74 68 61 74 20 6f 70 65 72 61 74 6f 72 20 6f 76  that operator ov
253dc 65 72 20 74 6f 20 42 2e 0a 2a 2f 0a 53 51 4c 49  er to B..*/.SQLI
253dd 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
253de 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68  sqlite3SrcListSh
253df 69 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c  iftJoinType(SrcL
253e0 69 73 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  ist *p){.  if( p
253e1 20 26 26 20 70 2d 3e 61 20 29 7b 0a 20 20 20 20   && p->a ){.    
253e2 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69  int i;.    for(i
253e3 3d 70 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b  =p->nSrc-1; i>0;
253e4 20 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e   i--){.      p->
253e5 61 5b 69 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20  a[i].jointype = 
253e6 70 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79  p->a[i-1].jointy
253e7 70 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  pe;.    }.    p-
253e8 3e 61 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d  >a[0].jointype =
253e9 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
253ea 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63   Begin a transac
253eb 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tion.*/.SQLITE_P
253ec 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
253ed 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74  te3BeginTransact
253ee 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  ion(Parse *pPars
253ef 65 2c 20 69 6e 74 20 74 79 70 65 29 7b 0a 20 20  e, int type){.  
253f0 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56  sqlite3 *db;.  V
253f1 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b  dbe *v;.  int i;
253f2 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ..  assert( pPar
253f3 73 65 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20  se!=0 );.  db = 
253f4 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
253f5 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 2f  sert( db!=0 );./
253f6 2a 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 30  *  if( db->aDb[0
253f7 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72  ].pBt==0 ) retur
253f8 6e 3b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  n; */.  if( sqli
253f9 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
253fa 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  rse, SQLITE_TRAN
253fb 53 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e 22  SACTION, "BEGIN"
253fc 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 72  , 0, 0) ){.    r
253fd 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d  eturn;.  }.  v =
253fe 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
253ff 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 21  pParse);.  if( !
25400 76 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  v ) return;.  if
25401 28 20 74 79 70 65 21 3d 54 4b 5f 44 45 46 45 52  ( type!=TK_DEFER
25402 52 45 44 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  RED ){.    for(i
25403 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
25404 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
25405 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25406 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20  OP_Transaction, 
25407 69 2c 20 28 74 79 70 65 3d 3d 54 4b 5f 45 58 43  i, (type==TK_EXC
25408 4c 55 53 49 56 45 29 2b 31 29 3b 0a 20 20 20 20  LUSIVE)+1);.    
25409 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
2540a 73 42 74 72 65 65 28 76 2c 20 69 29 3b 0a 20 20  sBtree(v, i);.  
2540b 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
2540c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2540d 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 30 2c  P_AutoCommit, 0,
2540e 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   0);.}../*.** Co
2540f 6d 6d 69 74 20 61 20 74 72 61 6e 73 61 63 74 69  mmit a transacti
25410 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  on.*/.SQLITE_PRI
25411 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
25412 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69  3CommitTransacti
25413 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  on(Parse *pParse
25414 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
25415 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20  ;.  Vdbe *v;..  
25416 61 73 73 65 72 74 28 20 70 50 61 72 73 65 21 3d  assert( pParse!=
25417 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 50 61 72  0 );.  db = pPar
25418 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74  se->db;.  assert
25419 28 20 64 62 21 3d 30 20 29 3b 0a 2f 2a 20 20 69  ( db!=0 );./*  i
2541a 66 28 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42  f( db->aDb[0].pB
2541b 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 20 2a  t==0 ) return; *
2541c 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  /.  if( sqlite3A
2541d 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2541e 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54   SQLITE_TRANSACT
2541f 49 4f 4e 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 30  ION, "COMMIT", 0
25420 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , 0) ){.    retu
25421 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d 20 73 71  rn;.  }.  v = sq
25422 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
25423 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b  rse);.  if( v ){
25424 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
25425 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74  AddOp2(v, OP_Aut
25426 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 30 29 3b 0a  oCommit, 1, 0);.
25427 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c    }.}../*.** Rol
25428 6c 62 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74  lback a transact
25429 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ion.*/.SQLITE_PR
2542a 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2542b 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61  e3RollbackTransa
2542c 63 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61  ction(Parse *pPa
2542d 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  rse){.  sqlite3 
2542e 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  *db;.  Vdbe *v;.
2542f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
25430 65 21 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70  e!=0 );.  db = p
25431 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73  Parse->db;.  ass
25432 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a 2f 2a  ert( db!=0 );./*
25433 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 30 5d    if( db->aDb[0]
25434 2e 70 42 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  .pBt==0 ) return
25435 3b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74  ; */.  if( sqlit
25436 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
25437 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  se, SQLITE_TRANS
25438 41 43 54 49 4f 4e 2c 20 22 52 4f 4c 4c 42 41 43  ACTION, "ROLLBAC
25439 4b 22 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20  K", 0, 0) ){.   
2543a 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76   return;.  }.  v
2543b 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
2543c 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
2543d 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   v ){.    sqlite
2543e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2543f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c  P_AutoCommit, 1,
25440 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   1);.  }.}../*.*
25441 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
25442 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
25443 20 70 61 72 73 65 72 20 77 68 65 6e 20 69 74 20   parser when it 
25444 70 61 72 73 65 73 20 61 20 63 6f 6d 6d 61 6e 64  parses a command
25445 20 74 6f 20 63 72 65 61 74 65 2c 0a 2a 2a 20 72   to create,.** r
25446 65 6c 65 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61  elease or rollba
25447 63 6b 20 61 6e 20 53 51 4c 20 73 61 76 65 70 6f  ck an SQL savepo
25448 69 6e 74 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  int. .*/.SQLITE_
25449 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2544a 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 50 61  ite3Savepoint(Pa
2544b 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
2544c 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d   op, Token *pNam
2544d 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d  e){.  char *zNam
2544e 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
2544f 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d  romToken(pParse-
25450 3e 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69  >db, pName);.  i
25451 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  f( zName ){.    
25452 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
25453 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
25454 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
25455 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
25456 49 4f 4e 0a 20 20 20 20 73 74 61 74 69 63 20 63  ION.    static c
25457 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a 5b 5d 20  onst char *az[] 
25458 3d 20 7b 20 22 42 45 47 49 4e 22 2c 20 22 52 45  = { "BEGIN", "RE
25459 4c 45 41 53 45 22 2c 20 22 52 4f 4c 4c 42 41 43  LEASE", "ROLLBAC
2545a 4b 22 20 7d 3b 0a 20 20 20 20 61 73 73 65 72 74  K" };.    assert
2545b 28 20 21 53 41 56 45 50 4f 49 4e 54 5f 42 45 47  ( !SAVEPOINT_BEG
2545c 49 4e 20 26 26 20 53 41 56 45 50 4f 49 4e 54 5f  IN && SAVEPOINT_
2545d 52 45 4c 45 41 53 45 3d 3d 31 20 26 26 20 53 41  RELEASE==1 && SA
2545e 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b  VEPOINT_ROLLBACK
2545f 3d 3d 32 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20  ==2 );.#endif.  
25460 20 20 69 66 28 20 21 76 20 7c 7c 20 73 71 6c 69    if( !v || sqli
25461 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
25462 72 73 65 2c 20 53 51 4c 49 54 45 5f 53 41 56 45  rse, SQLITE_SAVE
25463 50 4f 49 4e 54 2c 20 61 7a 5b 6f 70 5d 2c 20 7a  POINT, az[op], z
25464 4e 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Name, 0) ){.    
25465 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
25466 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d  pParse->db, zNam
25467 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
25468 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
25469 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2546a 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 2c 20 6f   OP_Savepoint, o
2546b 70 2c 20 30 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20  p, 0, 0, zName, 
2546c 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d  P4_DYNAMIC);.  }
2546d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73  .}../*.** Make s
2546e 75 72 65 20 74 68 65 20 54 45 4d 50 20 64 61 74  ure the TEMP dat
2546f 61 62 61 73 65 20 69 73 20 6f 70 65 6e 20 61 6e  abase is open an
25470 64 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20  d available for 
25471 75 73 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20  use.  Return.** 
25472 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72  the number of er
25473 72 6f 72 73 2e 20 20 4c 65 61 76 65 20 61 6e 79  rors.  Leave any
25474 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
25475 69 6e 20 74 68 65 20 70 50 61 72 73 65 20 73 74  in the pParse st
25476 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49  ructure..*/.SQLI
25477 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
25478 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61  qlite3OpenTempDa
25479 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 70 50  tabase(Parse *pP
2547a 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33  arse){.  sqlite3
2547b 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2547c 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 44 62  b;.  if( db->aDb
2547d 5b 31 5d 2e 70 42 74 3d 3d 30 20 26 26 20 21 70  [1].pBt==0 && !p
2547e 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29  Parse->explain )
2547f 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
25480 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
25481 6e 74 20 66 6c 61 67 73 20 3d 20 0a 20 20 20 20  nt flags = .    
25482 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
25483 4e 5f 52 45 41 44 57 52 49 54 45 20 7c 0a 20 20  N_READWRITE |.  
25484 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
25485 50 45 4e 5f 43 52 45 41 54 45 20 7c 0a 20 20 20  PEN_CREATE |.   
25486 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
25487 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20  EN_EXCLUSIVE |. 
25488 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
25489 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
2548a 53 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53  SE |.          S
2548b 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
2548c 44 42 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73 71  DB;..    rc = sq
2548d 6c 69 74 65 33 42 74 72 65 65 46 61 63 74 6f 72  lite3BtreeFactor
2548e 79 28 64 62 2c 20 30 2c 20 30 2c 20 53 51 4c 49  y(db, 0, 0, SQLI
2548f 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45  TE_DEFAULT_CACHE
25490 5f 53 49 5a 45 2c 20 66 6c 61 67 73 2c 0a 20 20  _SIZE, flags,.  
25491 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25492 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
25493 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 3b  db->aDb[1].pBt);
25494 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
25495 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
25496 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
25497 70 50 61 72 73 65 2c 20 22 75 6e 61 62 6c 65 20  pParse, "unable 
25498 74 6f 20 6f 70 65 6e 20 61 20 74 65 6d 70 6f 72  to open a tempor
25499 61 72 79 20 64 61 74 61 62 61 73 65 20 22 0a 20  ary database ". 
2549a 20 20 20 20 20 20 20 22 66 69 6c 65 20 66 6f 72         "file for
2549b 20 73 74 6f 72 69 6e 67 20 74 65 6d 70 6f 72 61   storing tempora
2549c 72 79 20 74 61 62 6c 65 73 22 29 3b 0a 20 20 20  ry tables");.   
2549d 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
2549e 72 63 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  rc;.      return
2549f 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73   1;.    }.    as
254a0 73 65 72 74 28 20 28 64 62 2d 3e 66 6c 61 67 73  sert( (db->flags
254a1 20 26 20 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e   & SQLITE_InTran
254a2 73 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 75 74  s)==0 || db->aut
254a3 6f 43 6f 6d 6d 69 74 20 29 3b 0a 20 20 20 20 61  oCommit );.    a
254a4 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 31  ssert( db->aDb[1
254a5 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20  ].pSchema );.   
254a6 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
254a7 72 6e 61 6c 4d 6f 64 65 28 73 71 6c 69 74 65 33  rnalMode(sqlite3
254a8 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61  BtreePager(db->a
254a9 44 62 5b 31 5d 2e 70 42 74 29 2c 0a 20 20 20 20  Db[1].pBt),.    
254aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
254ab 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66 6c 74          db->dflt
254ac 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20  JournalMode);.  
254ad 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
254ae 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
254af 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 77  VDBE code that w
254b0 69 6c 6c 20 76 65 72 69 66 79 20 74 68 65 20 73  ill verify the s
254b1 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 61 6e 64  chema cookie and
254b2 20 73 74 61 72 74 0a 2a 2a 20 61 20 72 65 61 64   start.** a read
254b3 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72  -transaction for
254b4 20 61 6c 6c 20 6e 61 6d 65 64 20 64 61 74 61 62   all named datab
254b5 61 73 65 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a  ase files..**.**
254b6 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   It is important
254b7 20 74 68 61 74 20 61 6c 6c 20 73 63 68 65 6d 61   that all schema
254b8 20 63 6f 6f 6b 69 65 73 20 62 65 20 76 65 72 69   cookies be veri
254b9 66 69 65 64 20 61 6e 64 20 61 6c 6c 0a 2a 2a 20  fied and all.** 
254ba 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e  read transaction
254bb 73 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66  s be started bef
254bc 6f 72 65 20 61 6e 79 74 68 69 6e 67 20 65 6c 73  ore anything els
254bd 65 20 68 61 70 70 65 6e 73 20 69 6e 0a 2a 2a 20  e happens in.** 
254be 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d  the VDBE program
254bf 2e 20 20 42 75 74 20 74 68 69 73 20 72 6f 75 74  .  But this rout
254c0 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65  ine can be calle
254c1 64 20 61 66 74 65 72 20 6d 75 63 68 20 6f 74 68  d after much oth
254c2 65 72 0a 2a 2a 20 63 6f 64 65 20 68 61 73 20 62  er.** code has b
254c3 65 65 6e 20 67 65 6e 65 72 61 74 65 64 2e 20 20  een generated.  
254c4 53 6f 20 68 65 72 65 20 69 73 20 77 68 61 74 20  So here is what 
254c5 77 65 20 64 6f 3a 0a 2a 2a 0a 2a 2a 20 54 68 65  we do:.**.** The
254c6 20 66 69 72 73 74 20 74 69 6d 65 20 74 68 69 73   first time this
254c7 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
254c8 65 64 2c 20 77 65 20 63 6f 64 65 20 61 6e 20 4f  ed, we code an O
254c9 50 5f 47 6f 74 6f 20 74 68 61 74 0a 2a 2a 20 77  P_Goto that.** w
254ca 69 6c 6c 20 6a 75 6d 70 20 74 6f 20 61 20 73 75  ill jump to a su
254cb 62 72 6f 75 74 69 6e 65 20 61 74 20 74 68 65 20  broutine at the 
254cc 65 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72  end of the progr
254cd 61 6d 2e 20 20 54 68 65 6e 20 77 65 0a 2a 2a 20  am.  Then we.** 
254ce 72 65 63 6f 72 64 20 65 76 65 72 79 20 64 61 74  record every dat
254cf 61 62 61 73 65 20 74 68 61 74 20 6e 65 65 64 73  abase that needs
254d0 20 69 74 73 20 73 63 68 65 6d 61 20 76 65 72 69   its schema veri
254d1 66 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 70  fied in the.** p
254d2 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73  Parse->cookieMas
254d3 6b 20 66 69 65 6c 64 2e 20 20 4c 61 74 65 72 2c  k field.  Later,
254d4 20 61 66 74 65 72 20 61 6c 6c 20 6f 74 68 65 72   after all other
254d5 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e 0a 2a   code has been.*
254d6 2a 20 67 65 6e 65 72 61 74 65 64 2c 20 74 68 65  * generated, the
254d7 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74   subroutine that
254d8 20 64 6f 65 73 20 74 68 65 20 63 6f 6f 6b 69 65   does the cookie
254d9 20 76 65 72 69 66 69 63 61 74 69 6f 6e 73 20 61   verifications a
254da 6e 64 0a 2a 2a 20 73 74 61 72 74 73 20 74 68 65  nd.** starts the
254db 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 77 69   transactions wi
254dc 6c 6c 20 62 65 20 63 6f 64 65 64 20 61 6e 64 20  ll be coded and 
254dd 74 68 65 20 4f 50 5f 47 6f 74 6f 20 50 32 20 76  the OP_Goto P2 v
254de 61 6c 75 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  alue.** will be 
254df 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f  made to point to
254e0 20 74 68 61 74 20 73 75 62 72 6f 75 74 69 6e 65   that subroutine
254e1 2e 20 20 54 68 65 20 67 65 6e 65 72 61 74 69 6f  .  The generatio
254e2 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6f 6b  n of the.** cook
254e3 69 65 20 76 65 72 69 66 69 63 61 74 69 6f 6e 20  ie verification 
254e4 73 75 62 72 6f 75 74 69 6e 65 20 63 6f 64 65 20  subroutine code 
254e5 68 61 70 70 65 6e 73 20 69 6e 20 73 71 6c 69 74  happens in sqlit
254e6 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 29  e3FinishCoding()
254e7 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 44 62 3c 30  ..**.** If iDb<0
254e8 20 74 68 65 6e 20 63 6f 64 65 20 74 68 65 20 4f   then code the O
254e9 50 5f 47 6f 74 6f 20 6f 6e 6c 79 20 2d 20 64 6f  P_Goto only - do
254ea 6e 27 74 20 73 65 74 20 66 6c 61 67 20 74 6f 20  n't set flag to 
254eb 76 65 72 69 66 79 20 74 68 65 0a 2a 2a 20 73 63  verify the.** sc
254ec 68 65 6d 61 20 6f 6e 20 61 6e 79 20 64 61 74 61  hema on any data
254ed 62 61 73 65 73 2e 20 20 54 68 69 73 20 63 61 6e  bases.  This can
254ee 20 62 65 20 75 73 65 64 20 74 6f 20 70 6f 73 69   be used to posi
254ef 74 69 6f 6e 20 74 68 65 20 4f 50 5f 47 6f 74 6f  tion the OP_Goto
254f0 0a 2a 2a 20 65 61 72 6c 79 20 69 6e 20 74 68 65  .** early in the
254f1 20 63 6f 64 65 2c 20 62 65 66 6f 72 65 20 77 65   code, before we
254f2 20 6b 6e 6f 77 20 69 66 20 61 6e 79 20 64 61 74   know if any dat
254f3 61 62 61 73 65 20 74 61 62 6c 65 73 20 77 69 6c  abase tables wil
254f4 6c 20 62 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51  l be used..*/.SQ
254f5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
254f6 64 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72  d sqlite3CodeVer
254f7 69 66 79 53 63 68 65 6d 61 28 50 61 72 73 65 20  ifySchema(Parse 
254f8 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62  *pParse, int iDb
254f9 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70  ){.  Parse *pTop
254fa 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50  level = sqlite3P
254fb 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61  arseToplevel(pPa
254fc 72 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 6f  rse);..  if( pTo
254fd 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 47 6f  plevel->cookieGo
254fe 74 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 56 64 62  to==0 ){.    Vdb
254ff 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
25500 74 56 64 62 65 28 70 54 6f 70 6c 65 76 65 6c 29  tVdbe(pToplevel)
25501 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20 29  ;.    if( v==0 )
25502 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 54 68 69   return;  /* Thi
25503 73 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 69  s only happens i
25504 66 20 74 68 65 72 65 20 77 61 73 20 61 20 70 72  f there was a pr
25505 69 6f 72 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20  ior error */.   
25506 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b   pToplevel->cook
25507 69 65 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33  ieGoto = sqlite3
25508 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
25509 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 2b 31 3b 0a  _Goto, 0, 0)+1;.
2550a 20 20 7d 0a 20 20 69 66 28 20 69 44 62 3e 3d 30    }.  if( iDb>=0
2550b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20   ){.    sqlite3 
2550c 2a 64 62 20 3d 20 70 54 6f 70 6c 65 76 65 6c 2d  *db = pToplevel-
2550d 3e 64 62 3b 0a 20 20 20 20 69 6e 74 20 6d 61 73  >db;.    int mas
2550e 6b 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  k;..    assert( 
2550f 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
25510 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61     assert( db->a
25511 44 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 7c  Db[iDb].pBt!=0 |
25512 7c 20 69 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20  | iDb==1 );.    
25513 61 73 73 65 72 74 28 20 69 44 62 3c 53 51 4c 49  assert( iDb<SQLI
25514 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b  TE_MAX_ATTACHED+
25515 32 20 29 3b 0a 20 20 20 20 6d 61 73 6b 20 3d 20  2 );.    mask = 
25516 31 3c 3c 69 44 62 3b 0a 20 20 20 20 69 66 28 20  1<<iDb;.    if( 
25517 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b  (pToplevel->cook
25518 69 65 4d 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d  ieMask & mask)==
25519 30 20 29 7b 0a 20 20 20 20 20 20 70 54 6f 70 6c  0 ){.      pTopl
2551a 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b  evel->cookieMask
2551b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20   |= mask;.      
2551c 70 54 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69  pToplevel->cooki
2551d 65 56 61 6c 75 65 5b 69 44 62 5d 20 3d 20 64 62  eValue[iDb] = db
2551e 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
2551f 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
25520 65 3b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d  e;.      if( !OM
25521 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62  IT_TEMPDB && iDb
25522 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==1 ){.        s
25523 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61  qlite3OpenTempDa
25524 74 61 62 61 73 65 28 70 54 6f 70 6c 65 76 65 6c  tabase(pToplevel
25525 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
25526 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
25527 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64 65  nerate VDBE code
25528 20 74 68 61 74 20 70 72 65 70 61 72 65 73 20 66   that prepares f
25529 6f 72 20 64 6f 69 6e 67 20 61 6e 20 6f 70 65 72  or doing an oper
2552a 61 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 6d 69  ation that.** mi
2552b 67 68 74 20 63 68 61 6e 67 65 20 74 68 65 20 64  ght change the d
2552c 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  atabase..**.** T
2552d 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72  his routine star
2552e 74 73 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63  ts a new transac
2552f 74 69 6f 6e 20 69 66 20 77 65 20 61 72 65 20 6e  tion if we are n
25530 6f 74 20 61 6c 72 65 61 64 79 20 77 69 74 68 69  ot already withi
25531 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69  n.** a transacti
25532 6f 6e 2e 20 20 49 66 20 77 65 20 61 72 65 20 61  on.  If we are a
25533 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 61 20  lready within a 
25534 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
25535 6e 20 61 20 63 68 65 63 6b 70 6f 69 6e 74 0a 2a  n a checkpoint.*
25536 2a 20 69 73 20 73 65 74 20 69 66 20 74 68 65 20  * is set if the 
25537 73 65 74 53 74 61 74 65 6d 65 6e 74 20 70 61 72  setStatement par
25538 61 6d 65 74 65 72 20 69 73 20 74 72 75 65 2e 20  ameter is true. 
25539 20 41 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68   A checkpoint sh
2553a 6f 75 6c 64 0a 2a 2a 20 62 65 20 73 65 74 20 66  ould.** be set f
2553b 6f 72 20 6f 70 65 72 61 74 69 6f 6e 73 20 74 68  or operations th
2553c 61 74 20 6d 69 67 68 74 20 66 61 69 6c 20 28 64  at might fail (d
2553d 75 65 20 74 6f 20 61 20 63 6f 6e 73 74 72 61 69  ue to a constrai
2553e 6e 74 29 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74  nt) part of.** t
2553f 68 65 20 77 61 79 20 74 68 72 6f 75 67 68 20 61  he way through a
25540 6e 64 20 77 68 69 63 68 20 77 69 6c 6c 20 6e 65  nd which will ne
25541 65 64 20 74 6f 20 75 6e 64 6f 20 73 6f 6d 65 20  ed to undo some 
25542 77 72 69 74 65 73 20 77 69 74 68 6f 75 74 20 68  writes without h
25543 61 76 69 6e 67 20 74 6f 0a 2a 2a 20 72 6f 6c 6c  aving to.** roll
25544 62 61 63 6b 20 74 68 65 20 77 68 6f 6c 65 20 74  back the whole t
25545 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 46 6f 72  ransaction.  For
25546 20 6f 70 65 72 61 74 69 6f 6e 73 20 77 68 65 72   operations wher
25547 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74  e all constraint
25548 73 0a 2a 2a 20 63 61 6e 20 62 65 20 63 68 65 63  s.** can be chec
25549 6b 65 64 20 62 65 66 6f 72 65 20 61 6e 79 20 63  ked before any c
2554a 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20  hanges are made 
2554b 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  to the database,
2554c 20 69 74 20 69 73 20 6e 65 76 65 72 0a 2a 2a 20   it is never.** 
2554d 6e 65 63 65 73 73 61 72 79 20 74 6f 20 75 6e 64  necessary to und
2554e 6f 20 61 20 77 72 69 74 65 20 61 6e 64 20 74 68  o a write and th
2554f 65 20 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f  e checkpoint sho
25550 75 6c 64 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a  uld not be set..
25551 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
25552 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
25553 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
25554 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
25555 20 69 6e 74 20 73 65 74 53 74 61 74 65 6d 65 6e   int setStatemen
25556 74 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 50  t, int iDb){.  P
25557 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20  arse *pToplevel 
25558 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  = sqlite3ParseTo
25559 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a  plevel(pParse);.
2555a 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
2555b 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
2555c 2c 20 69 44 62 29 3b 0a 20 20 70 54 6f 70 6c 65  , iDb);.  pTople
2555d 76 65 6c 2d 3e 77 72 69 74 65 4d 61 73 6b 20 7c  vel->writeMask |
2555e 3d 20 31 3c 3c 69 44 62 3b 0a 20 20 70 54 6f 70  = 1<<iDb;.  pTop
2555f 6c 65 76 65 6c 2d 3e 69 73 4d 75 6c 74 69 57 72  level->isMultiWr
25560 69 74 65 20 7c 3d 20 73 65 74 53 74 61 74 65 6d  ite |= setStatem
25561 65 6e 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ent;.}../*.** In
25562 64 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20  dicate that the 
25563 73 74 61 74 65 6d 65 6e 74 20 63 75 72 72 65 6e  statement curren
25564 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  tly under constr
25565 75 63 74 69 6f 6e 20 6d 69 67 68 74 20 77 72 69  uction might wri
25566 74 65 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20  te.** more than 
25567 6f 6e 65 20 65 6e 74 72 79 20 28 65 78 61 6d 70  one entry (examp
25568 6c 65 3a 20 64 65 6c 65 74 69 6e 67 20 6f 6e 65  le: deleting one
25569 20 72 6f 77 20 74 68 65 6e 20 69 6e 73 65 72 74   row then insert
2556a 69 6e 67 20 61 6e 6f 74 68 65 72 2c 0a 2a 2a 20  ing another,.** 
2556b 69 6e 73 65 72 74 69 6e 67 20 6d 75 6c 74 69 70  inserting multip
2556c 6c 65 20 72 6f 77 73 20 69 6e 20 61 20 74 61 62  le rows in a tab
2556d 6c 65 2c 20 6f 72 20 69 6e 73 65 72 74 69 6e 67  le, or inserting
2556e 20 61 20 72 6f 77 20 61 6e 64 20 69 6e 64 65 78   a row and index
2556f 20 65 6e 74 72 69 65 73 2e 29 0a 2a 2a 20 49 66   entries.).** If
25570 20 61 6e 20 61 62 6f 72 74 20 6f 63 63 75 72 73   an abort occurs
25571 20 61 66 74 65 72 20 73 6f 6d 65 20 6f 66 20 74   after some of t
25572 68 65 73 65 20 77 72 69 74 65 73 20 68 61 76 65  hese writes have
25573 20 63 6f 6d 70 6c 65 74 65 64 2c 20 74 68 65 6e   completed, then
25574 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 6e   it will.** be n
25575 65 63 65 73 73 61 72 79 20 74 6f 20 75 6e 64 6f  ecessary to undo
25576 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 64 20 77   the completed w
25577 72 69 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rites..*/.SQLITE
25578 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
25579 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28  lite3MultiWrite(
2557a 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
2557b 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
2557c 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  el = sqlite3Pars
2557d 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
2557e 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e  );.  pToplevel->
2557f 69 73 4d 75 6c 74 69 57 72 69 74 65 20 3d 20 31  isMultiWrite = 1
25580 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20  ;.}../* .** The 
25581 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20 63  code generator c
25582 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
25583 65 20 69 66 20 69 73 20 64 69 73 63 6f 76 65 72  e if is discover
25584 73 20 74 68 61 74 20 69 74 20 69 73 0a 2a 2a 20  s that it is.** 
25585 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 62 6f 72  possible to abor
25586 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20 70 72  t a statement pr
25587 69 6f 72 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f  ior to completio
25588 6e 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20  n.  In order to 
25589 0a 2a 2a 20 70 65 72 66 6f 72 6d 20 74 68 69 73  .** perform this
2558a 20 61 62 6f 72 74 20 77 69 74 68 6f 75 74 20 63   abort without c
2558b 6f 72 72 75 70 74 69 6e 67 20 74 68 65 20 64 61  orrupting the da
2558c 74 61 62 61 73 65 2c 20 77 65 20 6e 65 65 64 20  tabase, we need 
2558d 74 6f 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20  to make.** sure 
2558e 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d 65  that the stateme
2558f 6e 74 20 69 73 20 70 72 6f 74 65 63 74 65 64 20  nt is protected 
25590 62 79 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  by a statement t
25591 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  ransaction..**.*
25592 2a 20 54 65 63 68 6e 69 63 61 6c 6c 79 2c 20 77  * Technically, w
25593 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73  e only need to s
25594 65 74 20 74 68 65 20 6d 61 79 41 62 6f 72 74 20  et the mayAbort 
25595 66 6c 61 67 20 69 66 20 74 68 65 0a 2a 2a 20 69  flag if the.** i
25596 73 4d 75 6c 74 69 57 72 69 74 65 20 66 6c 61 67  sMultiWrite flag
25597 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20   was previously 
25598 73 65 74 2e 20 20 54 68 65 72 65 20 69 73 20 61  set.  There is a
25599 20 74 69 6d 65 20 64 65 70 65 6e 64 65 6e 63 79   time dependency
2559a 0a 2a 2a 20 73 75 63 68 20 74 68 61 74 20 74 68  .** such that th
2559b 65 20 61 62 6f 72 74 20 6d 75 73 74 20 6f 63 63  e abort must occ
2559c 75 72 20 61 66 74 65 72 20 74 68 65 20 6d 75 6c  ur after the mul
2559d 74 69 77 72 69 74 65 2e 20 20 54 68 69 73 20 6d  tiwrite.  This m
2559e 61 6b 65 73 0a 2a 2a 20 73 6f 6d 65 20 73 74 61  akes.** some sta
2559f 74 65 6d 65 6e 74 73 20 69 6e 76 6f 6c 76 69 6e  tements involvin
255a0 67 20 74 68 65 20 52 45 50 4c 41 43 45 20 63 6f  g the REPLACE co
255a1 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f  nflict resolutio
255a2 6e 20 61 6c 67 6f 72 69 74 68 6d 0a 2a 2a 20 67  n algorithm.** g
255a3 6f 20 61 20 6c 69 74 74 6c 65 20 66 61 73 74 65  o a little faste
255a4 72 2e 20 20 42 75 74 20 74 61 6b 69 6e 67 20 61  r.  But taking a
255a5 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 69 73  dvantage of this
255a6 20 74 69 6d 65 20 64 65 70 65 6e 64 65 6e 63 79   time dependency
255a7 0a 2a 2a 20 6d 61 6b 65 73 20 69 74 20 6d 6f 72  .** makes it mor
255a8 65 20 64 69 66 66 69 63 75 6c 74 20 74 6f 20 70  e difficult to p
255a9 72 6f 76 65 20 74 68 61 74 20 74 68 65 20 63 6f  rove that the co
255aa 64 65 20 69 73 20 63 6f 72 72 65 63 74 20 28 69  de is correct (i
255ab 6e 20 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72  n .** particular
255ac 2c 20 69 74 20 70 72 65 76 65 6e 74 73 20 75 73  , it prevents us
255ad 20 66 72 6f 6d 20 77 72 69 74 69 6e 67 20 61 6e   from writing an
255ae 20 65 66 66 65 63 74 69 76 65 0a 2a 2a 20 69 6d   effective.** im
255af 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
255b0 73 71 6c 69 74 65 33 41 73 73 65 72 74 4d 61 79  sqlite3AssertMay
255b1 41 62 6f 72 74 28 29 29 20 61 6e 64 20 73 6f 20  Abort()) and so 
255b2 77 65 20 68 61 76 65 20 63 68 6f 73 65 6e 0a 2a  we have chosen.*
255b3 2a 20 74 6f 20 74 61 6b 65 20 74 68 65 20 73 61  * to take the sa
255b4 66 65 20 72 6f 75 74 65 20 61 6e 64 20 73 6b 69  fe route and ski
255b5 70 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69  p the optimizati
255b6 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
255b7 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
255b8 65 33 4d 61 79 41 62 6f 72 74 28 50 61 72 73 65  e3MayAbort(Parse
255b9 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 50 61 72   *pParse){.  Par
255ba 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20  se *pToplevel = 
255bb 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c  sqlite3ParseTopl
255bc 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20  evel(pParse);.  
255bd 70 54 6f 70 6c 65 76 65 6c 2d 3e 6d 61 79 41 62  pToplevel->mayAb
255be 6f 72 74 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  ort = 1;.}../*.*
255bf 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 48 61 6c  * Code an OP_Hal
255c0 74 20 74 68 61 74 20 63 61 75 73 65 73 20 74 68  t that causes th
255c1 65 20 76 64 62 65 20 74 6f 20 72 65 74 75 72 6e  e vdbe to return
255c2 20 61 6e 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54   an SQLITE_CONST
255c3 52 41 49 4e 54 0a 2a 2a 20 65 72 72 6f 72 2e 20  RAINT.** error. 
255c4 54 68 65 20 6f 6e 45 72 72 6f 72 20 70 61 72 61  The onError para
255c5 6d 65 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73  meter determines
255c6 20 77 68 69 63 68 20 28 69 66 20 61 6e 79 29 20   which (if any) 
255c7 6f 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  of the statement
255c8 0a 2a 2a 20 61 6e 64 2f 6f 72 20 63 75 72 72 65  .** and/or curre
255c9 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
255ca 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  s rolled back..*
255cb 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
255cc 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 6c   void sqlite3Hal
255cd 74 43 6f 6e 73 74 72 61 69 6e 74 28 50 61 72 73  tConstraint(Pars
255ce 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f  e *pParse, int o
255cf 6e 45 72 72 6f 72 2c 20 63 68 61 72 20 2a 70 34  nError, char *p4
255d0 2c 20 69 6e 74 20 70 34 74 79 70 65 29 7b 0a 20  , int p4type){. 
255d1 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
255d2 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
255d3 29 3b 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72  );.  if( onError
255d4 3d 3d 4f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20  ==OE_Abort ){.  
255d5 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72    sqlite3MayAbor
255d6 74 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20  t(pParse);.  }. 
255d7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
255d8 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p4(v, OP_Halt, S
255d9 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
255da 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 70 34  , onError, 0, p4
255db 2c 20 70 34 74 79 70 65 29 3b 0a 7d 0a 0a 2f 2a  , p4type);.}../*
255dc 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
255dd 20 69 66 20 70 49 6e 64 65 78 20 75 73 65 73 20   if pIndex uses 
255de 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  the collating se
255df 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 52  quence pColl.  R
255e0 65 74 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66  eturn.** true if
255e1 20 69 74 20 64 6f 65 73 20 61 6e 64 20 66 61 6c   it does and fal
255e2 73 65 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f  se if it does no
255e3 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  t..*/.#ifndef SQ
255e4 4c 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45  LITE_OMIT_REINDE
255e5 58 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c  X.static int col
255e6 6c 61 74 69 6f 6e 4d 61 74 63 68 28 63 6f 6e 73  lationMatch(cons
255e7 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20 49  t char *zColl, I
255e8 6e 64 65 78 20 2a 70 49 6e 64 65 78 29 7b 0a 20  ndex *pIndex){. 
255e9 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
255ea 28 20 7a 43 6f 6c 6c 21 3d 30 20 29 3b 0a 20 20  ( zColl!=0 );.  
255eb 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65  for(i=0; i<pInde
255ec 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  x->nColumn; i++)
255ed 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
255ee 20 2a 7a 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a   *z = pIndex->az
255ef 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 61 73 73  Coll[i];.    ass
255f0 65 72 74 28 20 7a 21 3d 30 20 29 3b 0a 20 20 20  ert( z!=0 );.   
255f1 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53   if( 0==sqlite3S
255f2 74 72 49 43 6d 70 28 7a 2c 20 7a 43 6f 6c 6c 29  trICmp(z, zColl)
255f3 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
255f4 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
255f5 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64  return 0;.}.#end
255f6 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70  if../*.** Recomp
255f7 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  ute all indices 
255f8 6f 66 20 70 54 61 62 20 74 68 61 74 20 75 73 65  of pTab that use
255f9 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
255fa 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 0a 2a  equence pColl..*
255fb 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68  * If pColl==0 th
255fc 65 6e 20 72 65 63 6f 6d 70 75 74 65 20 61 6c 6c  en recompute all
255fd 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62   indices of pTab
255fe 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
255ff 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58  ITE_OMIT_REINDEX
25600 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 69  .static void rei
25601 6e 64 65 78 54 61 62 6c 65 28 50 61 72 73 65 20  ndexTable(Parse 
25602 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
25603 70 54 61 62 2c 20 63 68 61 72 20 63 6f 6e 73 74  pTab, char const
25604 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 49 6e 64 65   *zColl){.  Inde
25605 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20  x *pIndex;      
25606 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e          /* An in
25607 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  dex associated w
25608 69 74 68 20 70 54 61 62 20 2a 2f 0a 0a 20 20 66  ith pTab */..  f
25609 6f 72 28 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e  or(pIndex=pTab->
2560a 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20  pIndex; pIndex; 
2560b 70 49 6e 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70  pIndex=pIndex->p
2560c 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 7a  Next){.    if( z
2560d 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f 6c 6c 61  Coll==0 || colla
2560e 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f 6c 6c 2c  tionMatch(zColl,
2560f 20 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20   pIndex) ){.    
25610 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69    int iDb = sqli
25611 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
25612 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
25613 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
25614 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
25615 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
25616 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20  arse, 0, iDb);. 
25617 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 66 69       sqlite3Refi
25618 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  llIndex(pParse, 
25619 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20  pIndex, -1);.   
2561a 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a   }.  }.}.#endif.
2561b 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65  ./*.** Recompute
2561c 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   all indices of 
2561d 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 6c  all tables in al
2561e 6c 20 64 61 74 61 62 61 73 65 73 20 77 68 65 72  l databases wher
2561f 65 20 74 68 65 0a 2a 2a 20 69 6e 64 69 63 65 73  e the.** indices
25620 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69   use the collati
25621 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c  ng sequence pCol
25622 6c 2e 20 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20  l.  If pColl==0 
25623 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 0a 2a  then recompute.*
25624 2a 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 65 76  * all indices ev
25625 65 72 79 77 68 65 72 65 2e 0a 2a 2f 0a 23 69 66  erywhere..*/.#if
25626 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
25627 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20  _REINDEX.static 
25628 76 6f 69 64 20 72 65 69 6e 64 65 78 44 61 74 61  void reindexData
25629 62 61 73 65 73 28 50 61 72 73 65 20 2a 70 50 61  bases(Parse *pPa
2562a 72 73 65 2c 20 63 68 61 72 20 63 6f 6e 73 74 20  rse, char const 
2562b 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44 62 20 2a 70  *zColl){.  Db *p
2562c 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
2562d 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67         /* A sing
2562e 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  le database */. 
2562f 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
25630 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25631 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 64  The database ind
25632 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73  ex number */.  s
25633 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
25634 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68  rse->db;   /* Th
25635 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
25636 63 74 69 6f 6e 20 2a 2f 0a 20 20 48 61 73 68 45  ction */.  HashE
25637 6c 65 6d 20 2a 6b 3b 20 20 20 20 20 20 20 20 20  lem *k;         
25638 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
25639 6f 70 69 6e 67 20 6f 76 65 72 20 74 61 62 6c 65  oping over table
2563a 73 20 69 6e 20 70 44 62 20 2a 2f 0a 20 20 54 61  s in pDb */.  Ta
2563b 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
2563c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74            /* A t
2563d 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61  able in the data
2563e 62 61 73 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69  base */..  for(i
2563f 44 62 3d 30 2c 20 70 44 62 3d 64 62 2d 3e 61 44  Db=0, pDb=db->aD
25640 62 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20  b; iDb<db->nDb; 
25641 69 44 62 2b 2b 2c 20 70 44 62 2b 2b 29 7b 0a 20  iDb++, pDb++){. 
25642 20 20 20 61 73 73 65 72 74 28 20 70 44 62 21 3d     assert( pDb!=
25643 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 73  0 );.    for(k=s
25644 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
25645 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62  pDb->pSchema->tb
25646 6c 48 61 73 68 29 3b 20 20 6b 3b 20 6b 3d 73 71  lHash);  k; k=sq
25647 6c 69 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29  liteHashNext(k))
25648 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 28  {.      pTab = (
25649 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73  Table*)sqliteHas
2564a 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 20 20  hData(k);.      
2564b 72 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61  reindexTable(pPa
2564c 72 73 65 2c 20 70 54 61 62 2c 20 7a 43 6f 6c 6c  rse, pTab, zColl
2564d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23  );.    }.  }.}.#
2564e 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  endif../*.** Gen
2564f 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74  erate code for t
25650 68 65 20 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61  he REINDEX comma
25651 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  nd..**.**       
25652 20 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20   REINDEX        
25653 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25654 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20      -- 1.**     
25655 20 20 20 52 45 49 4e 44 45 58 20 20 3c 63 6f 6c     REINDEX  <col
25656 6c 61 74 69 6f 6e 3e 20 20 20 20 20 20 20 20 20  lation>         
25657 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20        -- 2.**   
25658 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3f 3c       REINDEX  ?<
25659 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c  database>.?<tabl
2565a 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 20  ename>  -- 3.** 
2565b 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20         REINDEX  
2565c 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c 69 6e  ?<database>.?<in
2565d 64 65 78 6e 61 6d 65 3e 20 20 2d 2d 20 34 0a 2a  dexname>  -- 4.*
2565e 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73  *.** Form 1 caus
2565f 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69  es all indices i
25660 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64  n all attached d
25661 61 74 61 62 61 73 65 73 20 74 6f 20 62 65 20 72  atabases to be r
25662 65 62 75 69 6c 74 2e 0a 2a 2a 20 46 6f 72 6d 20  ebuilt..** Form 
25663 32 20 72 65 62 75 69 6c 64 73 20 61 6c 6c 20 69  2 rebuilds all i
25664 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 64 61  ndices in all da
25665 74 61 62 61 73 65 73 20 74 68 61 74 20 75 73 65  tabases that use
25666 20 74 68 65 20 6e 61 6d 65 64 0a 2a 2a 20 63 6f   the named.** co
25667 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
25668 2e 20 20 46 6f 72 6d 73 20 33 20 61 6e 64 20 34  .  Forms 3 and 4
25669 20 72 65 62 75 69 6c 64 20 74 68 65 20 6e 61 6d   rebuild the nam
2566a 65 64 20 69 6e 64 65 78 20 6f 72 20 61 6c 6c 0a  ed index or all.
2566b 2a 2a 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63  ** indices assoc
2566c 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e  iated with the n
2566d 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23  amed table..*/.#
2566e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2566f 49 54 5f 52 45 49 4e 44 45 58 0a 53 51 4c 49 54  IT_REINDEX.SQLIT
25670 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
25671 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 61  qlite3Reindex(Pa
25672 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
25673 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65  en *pName1, Toke
25674 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 43 6f  n *pName2){.  Co
25675 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20  llSeq *pColl;   
25676 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
25677 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
25678 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 2c  to be reindexed,
25679 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68   or NULL */.  ch
2567a 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20  ar *z;          
2567b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
2567c 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20  e of a table or 
2567d 69 6e 64 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 74  index */.  const
2567e 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20   char *zDb;     
2567f 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
25680 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  f the database *
25681 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
25682 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25683 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68  /* A table in th
25684 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
25685 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20  Index *pIndex;  
25686 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
25687 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74  n index associat
25688 65 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a  ed with pTab */.
25689 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
2568a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2568b 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e   The database in
2568c 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  dex number */.  
2568d 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2568e 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54  arse->db;   /* T
2568f 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
25690 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65  ection */.  Toke
25691 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b 20 20 20 20  n *pObjName;    
25692 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
25693 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  of the table or 
25694 69 6e 64 65 78 20 74 6f 20 62 65 20 72 65 69 6e  index to be rein
25695 64 65 78 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52  dexed */..  /* R
25696 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65  ead the database
25697 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65   schema. If an e
25698 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61  rror occurs, lea
25699 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
2569a 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64  age.  ** and cod
2569b 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20  e in pParse and 
2569c 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a  return NULL. */.
2569d 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
2569e 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
2569f 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
256a0 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
256a1 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29   if( pName1==0 )
256a2 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 44 61 74  {.    reindexDat
256a3 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 30  abases(pParse, 0
256a4 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
256a5 20 7d 65 6c 73 65 20 69 66 28 20 4e 45 56 45 52   }else if( NEVER
256a6 28 70 4e 61 6d 65 32 3d 3d 30 29 20 7c 7c 20 70  (pName2==0) || p
256a7 4e 61 6d 65 32 2d 3e 7a 3d 3d 30 20 29 7b 0a 20  Name2->z==0 ){. 
256a8 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a     char *zColl;.
256a9 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d      assert( pNam
256aa 65 31 2d 3e 7a 20 29 3b 0a 20 20 20 20 7a 43 6f  e1->z );.    zCo
256ab 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65  ll = sqlite3Name
256ac 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65  FromToken(pParse
256ad 2d 3e 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20  ->db, pName1);. 
256ae 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 20     if( !zColl ) 
256af 72 65 74 75 72 6e 3b 0a 20 20 20 20 70 43 6f 6c  return;.    pCol
256b0 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
256b1 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64  ollSeq(db, ENC(d
256b2 62 29 2c 20 7a 43 6f 6c 6c 2c 20 30 29 3b 0a 20  b), zColl, 0);. 
256b3 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a     if( pColl ){.
256b4 20 20 20 20 20 20 72 65 69 6e 64 65 78 44 61 74        reindexDat
256b5 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 7a  abases(pParse, z
256b6 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c  Coll);.      sql
256b7 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
256b8 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 72 65 74  Coll);.      ret
256b9 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  urn;.    }.    s
256ba 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
256bb 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69   zColl);.  }.  i
256bc 44 62 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50  Db = sqlite3TwoP
256bd 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  artName(pParse, 
256be 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20  pName1, pName2, 
256bf 26 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66  &pObjName);.  if
256c0 28 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e  ( iDb<0 ) return
256c1 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e  ;.  z = sqlite3N
256c2 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
256c3 20 70 4f 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66   pObjName);.  if
256c4 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( z==0 ) return;
256c5 0a 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  .  zDb = db->aDb
256c6 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70  [iDb].zName;.  p
256c7 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Tab = sqlite3Fin
256c8 64 54 61 62 6c 65 28 64 62 2c 20 7a 2c 20 7a 44  dTable(db, z, zD
256c9 62 29 3b 0a 20 20 69 66 28 20 70 54 61 62 20 29  b);.  if( pTab )
256ca 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 54 61 62  {.    reindexTab
256cb 6c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  le(pParse, pTab,
256cc 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
256cd 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20  DbFree(db, z);. 
256ce 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
256cf 20 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65   pIndex = sqlite
256d0 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a  3FindIndex(db, z
256d1 2c 20 7a 44 62 29 3b 0a 20 20 73 71 6c 69 74 65  , zDb);.  sqlite
256d2 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a  3DbFree(db, z);.
256d3 20 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a    if( pIndex ){.
256d4 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
256d5 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
256d6 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a  Parse, 0, iDb);.
256d7 20 20 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c      sqlite3Refil
256d8 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  lIndex(pParse, p
256d9 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20  Index, -1);.    
256da 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71  return;.  }.  sq
256db 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
256dc 61 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f  arse, "unable to
256dd 20 69 64 65 6e 74 69 66 79 20 74 68 65 20 6f 62   identify the ob
256de 6a 65 63 74 20 74 6f 20 62 65 20 72 65 69 6e 64  ject to be reind
256df 65 78 65 64 22 29 3b 0a 7d 0a 23 65 6e 64 69 66  exed");.}.#endif
256e0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
256e1 20 64 79 6e 61 6d 69 63 6c 79 20 61 6c 6c 6f 63   dynamicly alloc
256e2 61 74 65 64 20 4b 65 79 49 6e 66 6f 20 73 74 72  ated KeyInfo str
256e3 75 63 74 75 72 65 20 74 68 61 74 20 63 61 6e 20  ucture that can 
256e4 62 65 20 75 73 65 64 0a 2a 2a 20 77 69 74 68 20  be used.** with 
256e5 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f  OP_OpenRead or O
256e6 50 5f 4f 70 65 6e 57 72 69 74 65 20 74 6f 20 61  P_OpenWrite to a
256e7 63 63 65 73 73 20 64 61 74 61 62 61 73 65 20 69  ccess database i
256e8 6e 64 65 78 20 70 49 64 78 2e 0a 2a 2a 0a 2a 2a  ndex pIdx..**.**
256e9 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
256ea 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
256eb 20 6e 65 77 20 73 74 72 75 63 74 75 72 65 20 69   new structure i
256ec 73 20 72 65 74 75 72 6e 65 64 2e 20 49 6e 20 74  s returned. In t
256ed 68 69 73 20 63 61 73 65 0a 2a 2a 20 74 68 65 20  his case.** the 
256ee 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e  caller is respon
256ef 73 69 62 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e  sible for callin
256f0 67 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  g sqlite3DbFree(
256f1 64 62 2c 20 29 20 6f 6e 20 74 68 65 20 72 65 74  db, ) on the ret
256f2 75 72 6e 65 64 20 0a 2a 2a 20 70 6f 69 6e 74 65  urned .** pointe
256f3 72 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  r. If an error o
256f4 63 63 75 72 73 20 28 6f 75 74 20 6f 66 20 6d 65  ccurs (out of me
256f5 6d 6f 72 79 20 6f 72 20 6d 69 73 73 69 6e 67 20  mory or missing 
256f6 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 73 65  collation .** se
256f7 71 75 65 6e 63 65 29 2c 20 4e 55 4c 4c 20 69 73  quence), NULL is
256f8 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68   returned and th
256f9 65 20 73 74 61 74 65 20 6f 66 20 70 50 61 72 73  e state of pPars
256fa 65 20 75 70 64 61 74 65 64 20 74 6f 20 72 65 66  e updated to ref
256fb 6c 65 63 74 0a 2a 2a 20 74 68 65 20 65 72 72 6f  lect.** the erro
256fc 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
256fd 56 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71  VATE KeyInfo *sq
256fe 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66  lite3IndexKeyinf
256ff 6f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  o(Parse *pParse,
25700 20 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20   Index *pIdx){. 
25701 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43   int i;.  int nC
25702 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  ol = pIdx->nColu
25703 6d 6e 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73  mn;.  int nBytes
25704 20 3d 20 73 69 7a 65 6f 66 28 4b 65 79 49 6e 66   = sizeof(KeyInf
25705 6f 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 69  o) + (nCol-1)*si
25706 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b  zeof(CollSeq*) +
25707 20 6e 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33   nCol;.  sqlite3
25708 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
25709 62 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  b;.  KeyInfo *pK
2570a 65 79 20 3d 20 28 4b 65 79 49 6e 66 6f 20 2a 29  ey = (KeyInfo *)
2570b 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
2570c 65 72 6f 28 64 62 2c 20 6e 42 79 74 65 73 29 3b  ero(db, nBytes);
2570d 0a 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a  ..  if( pKey ){.
2570e 20 20 20 20 70 4b 65 79 2d 3e 64 62 20 3d 20 70      pKey->db = p
2570f 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70  Parse->db;.    p
25710 4b 65 79 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  Key->aSortOrder 
25711 3d 20 28 75 38 20 2a 29 26 28 70 4b 65 79 2d 3e  = (u8 *)&(pKey->
25712 61 43 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20  aColl[nCol]);.  
25713 20 20 61 73 73 65 72 74 28 20 26 70 4b 65 79 2d    assert( &pKey-
25714 3e 61 53 6f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c  >aSortOrder[nCol
25715 5d 3d 3d 26 28 28 28 75 38 20 2a 29 70 4b 65 79  ]==&(((u8 *)pKey
25716 29 5b 6e 42 79 74 65 73 5d 29 20 29 3b 0a 20 20  )[nBytes]) );.  
25717 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
25718 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63  l; i++){.      c
25719 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 70 49 64  har *zColl = pId
2571a 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20  x->azColl[i];.  
2571b 20 20 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c      assert( zCol
2571c 6c 20 29 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d  l );.      pKey-
2571d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 73 71 6c 69  >aColl[i] = sqli
2571e 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71  te3LocateCollSeq
2571f 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b  (pParse, zColl);
25720 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f  .      pKey->aSo
25721 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 64  rtOrder[i] = pId
25722 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  x->aSortOrder[i]
25723 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4b 65 79  ;.    }.    pKey
25724 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29  ->nField = (u16)
25725 6e 43 6f 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  nCol;.  }..  if(
25726 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b   pParse->nErr ){
25727 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
25728 65 65 28 64 62 2c 20 70 4b 65 79 29 3b 0a 20 20  ee(db, pKey);.  
25729 20 20 70 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a    pKey = 0;.  }.
2572a 20 20 72 65 74 75 72 6e 20 70 4b 65 79 3b 0a 7d    return pKey;.}
2572b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2572c 2a 20 45 6e 64 20 6f 66 20 62 75 69 6c 64 2e 63  * End of build.c
2572d 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2572e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2572f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25730 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
25731 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 63 61 6c  * Begin file cal
25732 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  lback.c ********
25733 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25734 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25735 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79  /./*.** 2005 May
25736 20 32 33 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61   23 .**.** The a
25737 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
25738 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
25739 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
2573a 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
2573b 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2573c 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2573d 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2573e 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2573f 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
25740 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
25741 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
25742 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
25743 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
25744 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
25745 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
25746 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
25747 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
25748 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25749 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2574a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2574b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2574c 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
2574d 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e  ile contains fun
2574e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 61  ctions used to a
2574f 63 63 65 73 73 20 74 68 65 20 69 6e 74 65 72 6e  ccess the intern
25750 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 0a 2a  al hash tables.*
25751 2a 20 6f 66 20 75 73 65 72 20 64 65 66 69 6e 65  * of user define
25752 64 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20  d functions and 
25753 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
25754 63 65 73 2e 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20  ces..*/.../*.** 
25755 49 6e 76 6f 6b 65 20 74 68 65 20 27 63 6f 6c 6c  Invoke the 'coll
25756 61 74 69 6f 6e 20 6e 65 65 64 65 64 27 20 63 61  ation needed' ca
25757 6c 6c 62 61 63 6b 20 74 6f 20 72 65 71 75 65 73  llback to reques
25758 74 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  t a collation se
25759 71 75 65 6e 63 65 0a 2a 2a 20 69 6e 20 74 68 65  quence.** in the
2575a 20 65 6e 63 6f 64 69 6e 67 20 65 6e 63 20 6f 66   encoding enc of
2575b 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e   name zName, len
2575c 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2f 0a 73 74  gth nName..*/.st
2575d 61 74 69 63 20 76 6f 69 64 20 63 61 6c 6c 43 6f  atic void callCo
2575e 6c 6c 4e 65 65 64 65 64 28 73 71 6c 69 74 65 33  llNeeded(sqlite3
2575f 20 2a 64 62 2c 20 69 6e 74 20 65 6e 63 2c 20 63   *db, int enc, c
25760 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
25761 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  ){.  assert( !db
25762 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 7c 7c  ->xCollNeeded ||
25763 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65   !db->xCollNeede
25764 64 31 36 20 29 3b 0a 20 20 69 66 28 20 64 62 2d  d16 );.  if( db-
25765 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 29 7b 0a  >xCollNeeded ){.
25766 20 20 20 20 63 68 61 72 20 2a 7a 45 78 74 65 72      char *zExter
25767 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53  nal = sqlite3DbS
25768 74 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29  trDup(db, zName)
25769 3b 0a 20 20 20 20 69 66 28 20 21 7a 45 78 74 65  ;.    if( !zExte
2576a 72 6e 61 6c 20 29 20 72 65 74 75 72 6e 3b 0a 20  rnal ) return;. 
2576b 20 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64     db->xCollNeed
2576c 65 64 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64  ed(db->pCollNeed
2576d 65 64 41 72 67 2c 20 64 62 2c 20 65 6e 63 2c 20  edArg, db, enc, 
2576e 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20  zExternal);.    
2576f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
25770 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20  , zExternal);.  
25771 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
25772 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 69 66  _OMIT_UTF16.  if
25773 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65  ( db->xCollNeede
25774 64 31 36 20 29 7b 0a 20 20 20 20 63 68 61 72 20  d16 ){.    char 
25775 63 6f 6e 73 74 20 2a 7a 45 78 74 65 72 6e 61 6c  const *zExternal
25776 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  ;.    sqlite3_va
25777 6c 75 65 20 2a 70 54 6d 70 20 3d 20 73 71 6c 69  lue *pTmp = sqli
25778 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62 29 3b  te3ValueNew(db);
25779 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
2577a 65 53 65 74 53 74 72 28 70 54 6d 70 2c 20 2d 31  eSetStr(pTmp, -1
2577b 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f  , zName, SQLITE_
2577c 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41  UTF8, SQLITE_STA
2577d 54 49 43 29 3b 0a 20 20 20 20 7a 45 78 74 65 72  TIC);.    zExter
2577e 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 56 61 6c  nal = sqlite3Val
2577f 75 65 54 65 78 74 28 70 54 6d 70 2c 20 53 51 4c  ueText(pTmp, SQL
25780 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29  ITE_UTF16NATIVE)
25781 3b 0a 20 20 20 20 69 66 28 20 7a 45 78 74 65 72  ;.    if( zExter
25782 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 64 62 2d  nal ){.      db-
25783 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31 36 28 64  >xCollNeeded16(d
25784 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72  b->pCollNeededAr
25785 67 2c 20 64 62 2c 20 28 69 6e 74 29 45 4e 43 28  g, db, (int)ENC(
25786 64 62 29 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b  db), zExternal);
25787 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
25788 65 33 56 61 6c 75 65 46 72 65 65 28 70 54 6d 70  e3ValueFree(pTmp
25789 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a  );.  }.#endif.}.
2578a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2578b 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 66  ine is called if
2578c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66   the collation f
2578d 61 63 74 6f 72 79 20 66 61 69 6c 73 20 74 6f 20  actory fails to 
2578e 64 65 6c 69 76 65 72 20 61 0a 2a 2a 20 63 6f 6c  deliver a.** col
2578f 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  lation function 
25790 69 6e 20 74 68 65 20 62 65 73 74 20 65 6e 63 6f  in the best enco
25791 64 69 6e 67 20 62 75 74 20 74 68 65 72 65 20 6d  ding but there m
25792 61 79 20 62 65 20 6f 74 68 65 72 20 76 65 72 73  ay be other vers
25793 69 6f 6e 73 0a 2a 2a 20 6f 66 20 74 68 69 73 20  ions.** of this 
25794 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69  collation functi
25795 6f 6e 20 28 66 6f 72 20 6f 74 68 65 72 20 74 65  on (for other te
25796 78 74 20 65 6e 63 6f 64 69 6e 67 73 29 20 61 76  xt encodings) av
25797 61 69 6c 61 62 6c 65 2e 20 55 73 65 20 6f 6e 65  ailable. Use one
25798 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 69 6e 73  .** of these ins
25799 74 65 61 64 20 69 66 20 74 68 65 79 20 65 78 69  tead if they exi
2579a 73 74 2e 20 41 76 6f 69 64 20 61 20 55 54 46 2d  st. Avoid a UTF-
2579b 38 20 3c 2d 3e 20 55 54 46 2d 31 36 20 63 6f 6e  8 <-> UTF-16 con
2579c 76 65 72 73 69 6f 6e 20 69 66 0a 2a 2a 20 70 6f  version if.** po
2579d 73 73 69 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  ssible..*/.stati
2579e 63 20 69 6e 74 20 73 79 6e 74 68 43 6f 6c 6c 53  c int synthCollS
2579f 65 71 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  eq(sqlite3 *db, 
257a0 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b  CollSeq *pColl){
257a1 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c  .  CollSeq *pCol
257a2 6c 32 3b 0a 20 20 63 68 61 72 20 2a 7a 20 3d 20  l2;.  char *z = 
257a3 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  pColl->zName;.  
257a4 69 6e 74 20 69 3b 0a 20 20 73 74 61 74 69 63 20  int i;.  static 
257a5 63 6f 6e 73 74 20 75 38 20 61 45 6e 63 5b 5d 20  const u8 aEnc[] 
257a6 3d 20 7b 20 53 51 4c 49 54 45 5f 55 54 46 31 36  = { SQLITE_UTF16
257a7 42 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  BE, SQLITE_UTF16
257a8 4c 45 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 20  LE, SQLITE_UTF8 
257a9 7d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  };.  for(i=0; i<
257aa 33 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 43 6f  3; i++){.    pCo
257ab 6c 6c 32 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  ll2 = sqlite3Fin
257ac 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 61 45 6e  dCollSeq(db, aEn
257ad 63 5b 69 5d 2c 20 7a 2c 20 30 29 3b 0a 20 20 20  c[i], z, 0);.   
257ae 20 69 66 28 20 70 43 6f 6c 6c 32 2d 3e 78 43 6d   if( pColl2->xCm
257af 70 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6d 65  p!=0 ){.      me
257b0 6d 63 70 79 28 70 43 6f 6c 6c 2c 20 70 43 6f 6c  mcpy(pColl, pCol
257b1 6c 32 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 6c 53  l2, sizeof(CollS
257b2 65 71 29 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c  eq));.      pCol
257b3 6c 2d 3e 78 44 65 6c 20 3d 20 30 3b 20 20 20 20  l->xDel = 0;    
257b4 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63       /* Do not c
257b5 6f 70 79 20 74 68 65 20 64 65 73 74 72 75 63 74  opy the destruct
257b6 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  or */.      retu
257b7 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
257b8 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
257b9 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d   SQLITE_ERROR;.}
257ba 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
257bb 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73  ction is respons
257bc 69 62 6c 65 20 66 6f 72 20 69 6e 76 6f 6b 69 6e  ible for invokin
257bd 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  g the collation 
257be 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b  factory callback
257bf 0a 2a 2a 20 6f 72 20 73 75 62 73 74 69 74 75 74  .** or substitut
257c0 69 6e 67 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20  ing a collation 
257c1 73 65 71 75 65 6e 63 65 20 6f 66 20 61 20 64 69  sequence of a di
257c2 66 66 65 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67  fferent encoding
257c3 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 72 65 71   when the.** req
257c4 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e  uested collation
257c5 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e 6f 74   sequence is not
257c6 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
257c7 65 20 64 65 73 69 72 65 64 20 65 6e 63 6f 64 69  e desired encodi
257c8 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 69 74  ng..** .** If it
257c9 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
257ca 65 6e 20 70 43 6f 6c 6c 20 6d 75 73 74 20 70 6f  en pColl must po
257cb 69 6e 74 20 74 6f 20 74 68 65 20 64 61 74 61 62  int to the datab
257cc 61 73 65 20 6e 61 74 69 76 65 20 65 6e 63 6f 64  ase native encod
257cd 69 6e 67 20 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f  ing .** collatio
257ce 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74 68 20  n sequence with 
257cf 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67  name zName, leng
257d0 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  th nName..**.** 
257d1 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
257d2 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 63   is either the c
257d3 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
257d4 65 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e 20  e to be used in 
257d5 64 61 74 61 62 61 73 65 0a 2a 2a 20 64 62 20 66  database.** db f
257d6 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70  or collation typ
257d7 65 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65  e name zName, le
257d8 6e 67 74 68 20 6e 4e 61 6d 65 2c 20 6f 72 20 4e  ngth nName, or N
257d9 55 4c 4c 2c 20 69 66 20 6e 6f 20 63 6f 6c 6c 61  ULL, if no colla
257da 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65  tion.** sequence
257db 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2e 0a 2a   can be found..*
257dc 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 73  *.** See also: s
257dd 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c  qlite3LocateColl
257de 53 65 71 28 29 2c 20 73 71 6c 69 74 65 33 46 69  Seq(), sqlite3Fi
257df 6e 64 43 6f 6c 6c 53 65 71 28 29 0a 2a 2f 0a 53  ndCollSeq().*/.S
257e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f  QLITE_PRIVATE Co
257e1 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65  llSeq *sqlite3Ge
257e2 74 43 6f 6c 6c 53 65 71 28 0a 20 20 73 71 6c 69  tCollSeq(.  sqli
257e3 74 65 33 2a 20 64 62 2c 20 20 20 20 20 20 20 20  te3* db,        
257e4 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
257e5 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
257e6 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20    u8 enc,       
257e7 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
257e8 65 73 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 20  esired encoding 
257e9 66 6f 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  for the collatin
257ea 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20  g sequence */.  
257eb 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 2c 20  CollSeq *pColl, 
257ec 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69        /* Collati
257ed 6e 67 20 73 65 71 75 65 6e 63 65 20 77 69 74 68  ng sequence with
257ee 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67   native encoding
257ef 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63  , or NULL */.  c
257f0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
257f1 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e       /* Collatin
257f2 67 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20  g sequence name 
257f3 2a 2f 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  */.){.  CollSeq 
257f4 2a 70 3b 0a 0a 20 20 70 20 3d 20 70 43 6f 6c 6c  *p;..  p = pColl
257f5 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20  ;.  if( !p ){.  
257f6 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e    p = sqlite3Fin
257f7 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63  dCollSeq(db, enc
257f8 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d  , zName, 0);.  }
257f9 0a 20 20 69 66 28 20 21 70 20 7c 7c 20 21 70 2d  .  if( !p || !p-
257fa 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 2f 2a 20  >xCmp ){.    /* 
257fb 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  No collation seq
257fc 75 65 6e 63 65 20 6f 66 20 74 68 69 73 20 74 79  uence of this ty
257fd 70 65 20 66 6f 72 20 74 68 69 73 20 65 6e 63 6f  pe for this enco
257fe 64 69 6e 67 20 69 73 20 72 65 67 69 73 74 65 72  ding is register
257ff 65 64 2e 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 20  ed..    ** Call 
25800 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61  the collation fa
25801 63 74 6f 72 79 20 74 6f 20 73 65 65 20 69 66 20  ctory to see if 
25802 69 74 20 63 61 6e 20 73 75 70 70 6c 79 20 75 73  it can supply us
25803 20 77 69 74 68 20 6f 6e 65 2e 0a 20 20 20 20 2a   with one..    *
25804 2f 0a 20 20 20 20 63 61 6c 6c 43 6f 6c 6c 4e 65  /.    callCollNe
25805 65 64 65 64 28 64 62 2c 20 65 6e 63 2c 20 7a 4e  eded(db, enc, zN
25806 61 6d 65 29 3b 0a 20 20 20 20 70 20 3d 20 73 71  ame);.    p = sq
25807 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
25808 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c  (db, enc, zName,
25809 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70   0);.  }.  if( p
2580a 20 26 26 20 21 70 2d 3e 78 43 6d 70 20 26 26 20   && !p->xCmp && 
2580b 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28 64 62 2c  synthCollSeq(db,
2580c 20 70 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 30   p) ){.    p = 0
2580d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
2580e 21 70 20 7c 7c 20 70 2d 3e 78 43 6d 70 20 29 3b  !p || p->xCmp );
2580f 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
25810 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
25811 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20  ne is called on 
25812 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  a collation sequ
25813 65 6e 63 65 20 62 65 66 6f 72 65 20 69 74 20 69  ence before it i
25814 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 68 65  s used to.** che
25815 63 6b 20 74 68 61 74 20 69 74 20 69 73 20 64 65  ck that it is de
25816 66 69 6e 65 64 2e 20 41 6e 20 75 6e 64 65 66 69  fined. An undefi
25817 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ned collation se
25818 71 75 65 6e 63 65 20 65 78 69 73 74 73 20 77 68  quence exists wh
25819 65 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65  en.** a database
2581a 20 69 73 20 6c 6f 61 64 65 64 20 74 68 61 74 20   is loaded that 
2581b 63 6f 6e 74 61 69 6e 73 20 72 65 66 65 72 65 6e  contains referen
2581c 63 65 73 20 74 6f 20 63 6f 6c 6c 61 74 69 6f 6e  ces to collation
2581d 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 74 68   sequences.** th
2581e 61 74 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e  at have not been
2581f 20 64 65 66 69 6e 65 64 20 62 79 20 73 71 6c 69   defined by sqli
25820 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
25821 74 69 6f 6e 28 29 20 65 74 63 2e 0a 2a 2a 0a 2a  tion() etc..**.*
25822 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 74  * If required, t
25823 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c  his routine call
25824 73 20 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e  s the 'collation
25825 20 6e 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63   needed' callbac
25826 6b 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20  k to.** request 
25827 61 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  a definition of 
25828 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  the collating se
25829 71 75 65 6e 63 65 2e 20 49 66 20 74 68 69 73 20  quence. If this 
2582a 64 6f 65 73 6e 27 74 20 77 6f 72 6b 2c 20 0a 2a  doesn't work, .*
2582b 2a 20 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20  * an equivalent 
2582c 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2582d 63 65 20 74 68 61 74 20 75 73 65 73 20 61 20 74  ce that uses a t
2582e 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 64 69 66  ext encoding dif
2582f 66 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74  ferent.** from t
25830 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
25831 20 69 73 20 73 75 62 73 74 69 74 75 74 65 64 2c   is substituted,
25832 20 69 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c   if one is avail
25833 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
25834 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
25835 74 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28  te3CheckCollSeq(
25836 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 43  Parse *pParse, C
25837 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a  ollSeq *pColl){.
25838 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20    if( pColl ){. 
25839 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2583a 4e 61 6d 65 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e  Name = pColl->zN
2583b 61 6d 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ame;.    sqlite3
2583c 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2583d 62 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  b;.    CollSeq *
2583e 70 20 3d 20 73 71 6c 69 74 65 33 47 65 74 43 6f  p = sqlite3GetCo
2583f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62  llSeq(db, ENC(db
25840 29 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 29  ), pColl, zName)
25841 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b 0a  ;.    if( !p ){.
25842 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
25843 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
25844 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74 69 6f 6e  o such collation
25845 20 73 65 71 75 65 6e 63 65 3a 20 25 73 22 2c 20   sequence: %s", 
25846 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 50  zName);.      pP
25847 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
25848 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
25849 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20  E_ERROR;.    }. 
2584a 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 43     assert( p==pC
2584b 6f 6c 6c 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74  oll );.  }.  ret
2584c 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2584d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65  ..../*.** Locate
2584e 20 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65   and return an e
2584f 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 64 62  ntry from the db
25850 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73 68 20 74  .aCollSeq hash t
25851 61 62 6c 65 2e 20 49 66 20 74 68 65 20 65 6e 74  able. If the ent
25852 72 79 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  ry.** specified 
25853 62 79 20 7a 4e 61 6d 65 20 61 6e 64 20 6e 4e 61  by zName and nNa
25854 6d 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20  me is not found 
25855 61 6e 64 20 70 61 72 61 6d 65 74 65 72 20 27 63  and parameter 'c
25856 72 65 61 74 65 27 20 69 73 0a 2a 2a 20 74 72 75  reate' is.** tru
25857 65 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61  e, then create a
25858 20 6e 65 77 20 65 6e 74 72 79 2e 20 4f 74 68 65   new entry. Othe
25859 72 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c  rwise return NUL
2585a 4c 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 70 6f  L..**.** Each po
2585b 69 6e 74 65 72 20 73 74 6f 72 65 64 20 69 6e 20  inter stored in 
2585c 74 68 65 20 73 71 6c 69 74 65 33 2e 61 43 6f 6c  the sqlite3.aCol
2585d 6c 53 65 71 20 68 61 73 68 20 74 61 62 6c 65 20  lSeq hash table 
2585e 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 2a 2a 20 61  contains an.** a
2585f 72 72 61 79 20 6f 66 20 74 68 72 65 65 20 43 6f  rray of three Co
25860 6c 6c 53 65 71 20 73 74 72 75 63 74 75 72 65 73  llSeq structures
25861 2e 20 54 68 65 20 66 69 72 73 74 20 69 73 20 74  . The first is t
25862 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
25863 75 65 6e 63 65 0a 2a 2a 20 70 72 65 66 66 65 72  uence.** preffer
25864 72 65 64 20 66 6f 72 20 55 54 46 2d 38 2c 20 74  red for UTF-8, t
25865 68 65 20 73 65 63 6f 6e 64 20 55 54 46 2d 31 36  he second UTF-16
25866 6c 65 2c 20 61 6e 64 20 74 68 65 20 74 68 69 72  le, and the thir
25867 64 20 55 54 46 2d 31 36 62 65 2e 0a 2a 2a 0a 2a  d UTF-16be..**.*
25868 2a 20 53 74 6f 72 65 64 20 69 6d 6d 65 64 69 61  * Stored immedia
25869 74 65 6c 79 20 61 66 74 65 72 20 74 68 65 20 74  tely after the t
2586a 68 72 65 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  hree collation s
2586b 65 71 75 65 6e 63 65 73 20 69 73 20 61 20 63 6f  equences is a co
2586c 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c  py of.** the col
2586d 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2586e 6e 61 6d 65 2e 20 41 20 70 6f 69 6e 74 65 72 20  name. A pointer 
2586f 74 6f 20 74 68 69 73 20 73 74 72 69 6e 67 20 69  to this string i
25870 73 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 65  s stored in.** e
25871 61 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ach collation se
25872 71 75 65 6e 63 65 20 73 74 72 75 63 74 75 72 65  quence structure
25873 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c  ..*/.static Coll
25874 53 65 71 20 2a 66 69 6e 64 43 6f 6c 6c 53 65 71  Seq *findCollSeq
25875 45 6e 74 72 79 28 0a 20 20 73 71 6c 69 74 65 33  Entry(.  sqlite3
25876 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
25877 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
25878 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ction */.  const
25879 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
2587a 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
2587b 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2587c 63 65 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61  ce */.  int crea
2587d 74 65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  te            /*
2587e 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 65 6e   Create a new en
2587f 74 72 79 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  try if true */.)
25880 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
25881 6c 6c 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20  ll;.  int nName 
25882 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
25883 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c  0(zName);.  pCol
25884 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  l = sqlite3HashF
25885 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  ind(&db->aCollSe
25886 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  q, zName, nName)
25887 3b 0a 0a 20 20 69 66 28 20 30 3d 3d 70 43 6f 6c  ;..  if( 0==pCol
25888 6c 20 26 26 20 63 72 65 61 74 65 20 29 7b 0a 20  l && create ){. 
25889 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
2588a 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2588b 62 2c 20 33 2a 73 69 7a 65 6f 66 28 2a 70 43 6f  b, 3*sizeof(*pCo
2588c 6c 6c 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 20  ll) + nName + 1 
2588d 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6c 6c  );.    if( pColl
2588e 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65   ){.      CollSe
2588f 71 20 2a 70 44 65 6c 20 3d 20 30 3b 0a 20 20 20  q *pDel = 0;.   
25890 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d     pColl[0].zNam
25891 65 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f 6c  e = (char*)&pCol
25892 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c  l[3];.      pCol
25893 6c 5b 30 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54  l[0].enc = SQLIT
25894 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20 70 43  E_UTF8;.      pC
25895 6f 6c 6c 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 28  oll[1].zName = (
25896 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b  char*)&pColl[3];
25897 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e  .      pColl[1].
25898 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
25899 31 36 4c 45 3b 0a 20 20 20 20 20 20 70 43 6f 6c  16LE;.      pCol
2589a 6c 5b 32 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68  l[2].zName = (ch
2589b 61 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20  ar*)&pColl[3];. 
2589c 20 20 20 20 20 70 43 6f 6c 6c 5b 32 5d 2e 65 6e       pColl[2].en
2589d 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36  c = SQLITE_UTF16
2589e 42 45 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  BE;.      memcpy
2589f 28 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c  (pColl[0].zName,
258a0 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a   zName, nName);.
258a1 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a        pColl[0].z
258a2 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b  Name[nName] = 0;
258a3 0a 20 20 20 20 20 20 70 44 65 6c 20 3d 20 73 71  .      pDel = sq
258a4 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
258a5 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 70  &db->aCollSeq, p
258a6 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 6e  Coll[0].zName, n
258a7 4e 61 6d 65 2c 20 70 43 6f 6c 6c 29 3b 0a 0a 20  Name, pColl);.. 
258a8 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c       /* If a mal
258a9 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20 6f 63  loc() failure oc
258aa 63 75 72 72 65 64 20 69 6e 20 73 71 6c 69 74 65  curred in sqlite
258ab 33 48 61 73 68 49 6e 73 65 72 74 28 29 2c 20 69  3HashInsert(), i
258ac 74 20 77 69 6c 6c 20 0a 20 20 20 20 20 20 2a 2a  t will .      **
258ad 20 72 65 74 75 72 6e 20 74 68 65 20 70 43 6f 6c   return the pCol
258ae 6c 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20  l pointer to be 
258af 64 65 6c 65 74 65 64 20 28 62 65 63 61 75 73 65  deleted (because
258b0 20 69 74 20 77 61 73 6e 27 74 20 61 64 64 65 64   it wasn't added
258b1 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65  .      ** to the
258b2 20 68 61 73 68 20 74 61 62 6c 65 29 2e 0a 20 20   hash table)..  
258b3 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73      */.      ass
258b4 65 72 74 28 20 70 44 65 6c 3d 3d 30 20 7c 7c 20  ert( pDel==0 || 
258b5 70 44 65 6c 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20  pDel==pColl );. 
258b6 20 20 20 20 20 69 66 28 20 70 44 65 6c 21 3d 30       if( pDel!=0
258b7 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e   ){.        db->
258b8 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
258b9 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
258ba 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 65 6c  3DbFree(db, pDel
258bb 29 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  );.        pColl
258bc 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
258bd 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
258be 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pColl;.}../*.**
258bf 20 50 61 72 61 6d 65 74 65 72 20 7a 4e 61 6d 65   Parameter zName
258c0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 55 54 46   points to a UTF
258c1 2d 38 20 65 6e 63 6f 64 65 64 20 73 74 72 69 6e  -8 encoded strin
258c2 67 20 6e 4e 61 6d 65 20 62 79 74 65 73 20 6c 6f  g nName bytes lo
258c3 6e 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ng..** Return th
258c4 65 20 43 6f 6c 6c 53 65 71 2a 20 70 6f 69 6e 74  e CollSeq* point
258c5 65 72 20 66 6f 72 20 74 68 65 20 63 6f 6c 6c 61  er for the colla
258c6 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
258c7 6d 65 64 20 7a 4e 61 6d 65 0a 2a 2a 20 66 6f 72  med zName.** for
258c8 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 27 65   the encoding 'e
258c9 6e 63 27 20 66 72 6f 6d 20 74 68 65 20 64 61 74  nc' from the dat
258ca 61 62 61 73 65 20 27 64 62 27 2e 0a 2a 2a 0a 2a  abase 'db'..**.*
258cb 2a 20 49 66 20 74 68 65 20 65 6e 74 72 79 20 73  * If the entry s
258cc 70 65 63 69 66 69 65 64 20 69 73 20 6e 6f 74 20  pecified is not 
258cd 66 6f 75 6e 64 20 61 6e 64 20 27 63 72 65 61 74  found and 'creat
258ce 65 27 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  e' is true, then
258cf 20 63 72 65 61 74 65 20 61 0a 2a 2a 20 6e 65 77   create a.** new
258d0 20 65 6e 74 72 79 2e 20 20 4f 74 68 65 72 77 69   entry.  Otherwi
258d1 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  se return NULL..
258d2 2a 2a 0a 2a 2a 20 41 20 73 65 70 61 72 61 74 65  **.** A separate
258d3 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69 74 65   function sqlite
258d4 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29  3LocateCollSeq()
258d5 20 69 73 20 61 20 77 72 61 70 70 65 72 20 61 72   is a wrapper ar
258d6 6f 75 6e 64 0a 2a 2a 20 74 68 69 73 20 72 6f 75  ound.** this rou
258d7 74 69 6e 65 2e 20 20 73 71 6c 69 74 65 33 4c 6f  tine.  sqlite3Lo
258d8 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 6e  cateCollSeq() in
258d9 76 6f 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74  vokes the collat
258da 69 6f 6e 20 66 61 63 74 6f 72 79 0a 2a 2a 20 69  ion factory.** i
258db 66 20 6e 65 63 65 73 73 61 72 79 20 61 6e 64 20  f necessary and 
258dc 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72  generates an err
258dd 6f 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68  or message if th
258de 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
258df 65 6e 63 65 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62  ence.** cannot b
258e0 65 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53  e found..**.** S
258e1 65 65 20 61 6c 73 6f 3a 20 73 71 6c 69 74 65 33  ee also: sqlite3
258e2 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 2c  LocateCollSeq(),
258e3 20 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53   sqlite3GetCollS
258e4 65 71 28 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  eq().*/.SQLITE_P
258e5 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
258e6 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
258e7 65 71 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  eq(.  sqlite3 *d
258e8 62 2c 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20 63  b,.  u8 enc,.  c
258e9 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
258ea 2c 0a 20 20 69 6e 74 20 63 72 65 61 74 65 0a 29  ,.  int create.)
258eb 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
258ec 6c 6c 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20  ll;.  if( zName 
258ed 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 66  ){.    pColl = f
258ee 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28  indCollSeqEntry(
258ef 64 62 2c 20 7a 4e 61 6d 65 2c 20 63 72 65 61 74  db, zName, creat
258f0 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e);.  }else{.   
258f1 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66   pColl = db->pDf
258f2 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a 20 20 61 73  ltColl;.  }.  as
258f3 73 65 72 74 28 20 53 51 4c 49 54 45 5f 55 54 46  sert( SQLITE_UTF
258f4 38 3d 3d 31 20 26 26 20 53 51 4c 49 54 45 5f 55  8==1 && SQLITE_U
258f5 54 46 31 36 4c 45 3d 3d 32 20 26 26 20 53 51 4c  TF16LE==2 && SQL
258f6 49 54 45 5f 55 54 46 31 36 42 45 3d 3d 33 20 29  ITE_UTF16BE==3 )
258f7 3b 0a 20 20 61 73 73 65 72 74 28 20 65 6e 63 3e  ;.  assert( enc>
258f8 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26 20  =SQLITE_UTF8 && 
258f9 65 6e 63 3c 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc<=SQLITE_UTF1
258fa 36 42 45 20 29 3b 0a 20 20 69 66 28 20 70 43 6f  6BE );.  if( pCo
258fb 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b 3d 20 65 6e  ll ) pColl += en
258fc 63 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 70 43  c-1;.  return pC
258fd 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 20 44 75 72 69 6e  oll;.}../* Durin
258fe 67 20 74 68 65 20 73 65 61 72 63 68 20 66 6f 72  g the search for
258ff 20 74 68 65 20 62 65 73 74 20 66 75 6e 63 74 69   the best functi
25900 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20 74  on definition, t
25901 68 69 73 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a  his procedure.**
25902 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 74 65   is called to te
25903 73 74 20 68 6f 77 20 77 65 6c 6c 20 74 68 65 20  st how well the 
25904 66 75 6e 63 74 69 6f 6e 20 70 61 73 73 65 64 20  function passed 
25905 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
25906 75 6d 65 6e 74 0a 2a 2a 20 6d 61 74 63 68 65 73  ument.** matches
25907 20 74 68 65 20 72 65 71 75 65 73 74 20 66 6f 72   the request for
25908 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
25909 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73 20   nArg arguments 
2590a 69 6e 20 61 20 73 79 73 74 65 6d 0a 2a 2a 20 74  in a system.** t
2590b 68 61 74 20 75 73 65 73 20 65 6e 63 6f 64 69 6e  hat uses encodin
2590c 67 20 65 6e 63 2e 20 54 68 65 20 76 61 6c 75 65  g enc. The value
2590d 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63 61   returned indica
2590e 74 65 73 20 68 6f 77 20 77 65 6c 6c 20 74 68 65  tes how well the
2590f 0a 2a 2a 20 72 65 71 75 65 73 74 20 69 73 20 6d  .** request is m
25910 61 74 63 68 65 64 2e 20 41 20 68 69 67 68 65 72  atched. A higher
25911 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73   value indicates
25912 20 61 20 62 65 74 74 65 72 20 6d 61 74 63 68 2e   a better match.
25913 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
25914 6e 65 64 20 76 61 6c 75 65 20 69 73 20 61 6c 77  ned value is alw
25915 61 79 73 20 62 65 74 77 65 65 6e 20 30 20 61 6e  ays between 0 an
25916 64 20 36 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  d 6, as follows:
25917 0a 2a 2a 0a 2a 2a 20 30 3a 20 4e 6f 74 20 61 20  .**.** 0: Not a 
25918 6d 61 74 63 68 2c 20 6f 72 20 69 66 20 6e 41 72  match, or if nAr
25919 67 3c 30 20 61 6e 64 20 74 68 65 20 66 75 6e 63  g<0 and the func
2591a 74 69 6f 6e 20 69 73 20 68 61 73 20 6e 6f 20 69  tion is has no i
2591b 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a  mplementation..*
2591c 2a 20 31 3a 20 41 20 76 61 72 69 61 62 6c 65 20  * 1: A variable 
2591d 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63 74 69  arguments functi
2591e 6f 6e 20 74 68 61 74 20 70 72 65 66 65 72 73 20  on that prefers 
2591f 55 54 46 2d 38 20 77 68 65 6e 20 61 20 55 54 46  UTF-8 when a UTF
25920 2d 31 36 0a 2a 2a 20 20 20 20 65 6e 63 6f 64 69  -16.**    encodi
25921 6e 67 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ng is requested,
25922 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a   or vice versa..
25923 2a 2a 20 32 3a 20 41 20 76 61 72 69 61 62 6c 65  ** 2: A variable
25924 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63 74   arguments funct
25925 69 6f 6e 20 74 68 61 74 20 75 73 65 73 20 55 54  ion that uses UT
25926 46 2d 31 36 42 45 20 77 68 65 6e 20 55 54 46 2d  F-16BE when UTF-
25927 31 36 4c 45 20 69 73 0a 2a 2a 20 20 20 20 72 65  16LE is.**    re
25928 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65  quested, or vice
25929 20 76 65 72 73 61 2e 0a 2a 2a 20 33 3a 20 41 20   versa..** 3: A 
2592a 76 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65 6e  variable argumen
2592b 74 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 69 6e  ts function usin
2592c 67 20 74 68 65 20 73 61 6d 65 20 74 65 78 74 20  g the same text 
2592d 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 20 34 3a 20  encoding..** 4: 
2592e 41 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  A function with 
2592f 74 68 65 20 65 78 61 63 74 20 6e 75 6d 62 65 72  the exact number
25930 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 72 65   of arguments re
25931 71 75 65 73 74 65 64 20 74 68 61 74 0a 2a 2a 20  quested that.** 
25932 20 20 20 70 72 65 66 65 72 73 20 55 54 46 2d 38     prefers UTF-8
25933 20 77 68 65 6e 20 61 20 55 54 46 2d 31 36 20 65   when a UTF-16 e
25934 6e 63 6f 64 69 6e 67 20 69 73 20 72 65 71 75 65  ncoding is reque
25935 73 74 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65  sted, or vice ve
25936 72 73 61 2e 0a 2a 2a 20 35 3a 20 41 20 66 75 6e  rsa..** 5: A fun
25937 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20 65  ction with the e
25938 78 61 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61  xact number of a
25939 72 67 75 6d 65 6e 74 73 20 72 65 71 75 65 73 74  rguments request
2593a 65 64 20 74 68 61 74 0a 2a 2a 20 20 20 20 70 72  ed that.**    pr
2593b 65 66 65 72 73 20 55 54 46 2d 31 36 4c 45 20 77  efers UTF-16LE w
2593c 68 65 6e 20 55 54 46 2d 31 36 42 45 20 69 73 20  hen UTF-16BE is 
2593d 72 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69  requested, or vi
2593e 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20 36 3a 20  ce versa..** 6: 
2593f 41 6e 20 65 78 61 63 74 20 6d 61 74 63 68 2e 0a  An exact match..
25940 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  **.*/.static int
25941 20 6d 61 74 63 68 51 75 61 6c 69 74 79 28 46 75   matchQuality(Fu
25942 6e 63 44 65 66 20 2a 70 2c 20 69 6e 74 20 6e 41  ncDef *p, int nA
25943 72 67 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69  rg, u8 enc){.  i
25944 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b 0a 20 20  nt match = 0;.  
25945 69 66 28 20 70 2d 3e 6e 41 72 67 3d 3d 2d 31 20  if( p->nArg==-1 
25946 7c 7c 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67  || p->nArg==nArg
25947 20 0a 20 20 20 7c 7c 20 28 6e 41 72 67 3d 3d 2d   .   || (nArg==-
25948 31 20 26 26 20 28 70 2d 3e 78 46 75 6e 63 21 3d  1 && (p->xFunc!=
25949 30 20 7c 7c 20 70 2d 3e 78 53 74 65 70 21 3d 30  0 || p->xStep!=0
2594a 29 29 0a 20 20 29 7b 0a 20 20 20 20 6d 61 74 63  )).  ){.    matc
2594b 68 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 70  h = 1;.    if( p
2594c 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c 20  ->nArg==nArg || 
2594d 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 20  nArg==-1 ){.    
2594e 20 20 6d 61 74 63 68 20 3d 20 34 3b 0a 20 20 20    match = 4;.   
2594f 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 3d 3d   }.    if( enc==
25950 70 2d 3e 69 50 72 65 66 45 6e 63 20 29 7b 0a 20  p->iPrefEnc ){. 
25951 20 20 20 20 20 6d 61 74 63 68 20 2b 3d 20 32 3b       match += 2;
25952 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 20  .    }.    else 
25953 69 66 28 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45  if( (enc==SQLITE
25954 5f 55 54 46 31 36 4c 45 20 26 26 20 70 2d 3e 69  _UTF16LE && p->i
25955 50 72 65 66 45 6e 63 3d 3d 53 51 4c 49 54 45 5f  PrefEnc==SQLITE_
25956 55 54 46 31 36 42 45 29 20 7c 7c 0a 20 20 20 20  UTF16BE) ||.    
25957 20 20 20 20 20 20 20 20 20 28 65 6e 63 3d 3d 53           (enc==S
25958 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 26 26  QLITE_UTF16BE &&
25959 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53 51   p->iPrefEnc==SQ
2595a 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 20 29 7b  LITE_UTF16LE) ){
2595b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b 3d 20  .      match += 
2595c 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
2595d 65 74 75 72 6e 20 6d 61 74 63 68 3b 0a 7d 0a 0a  eturn match;.}..
2595e 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 61 20 46  /*.** Search a F
2595f 75 6e 63 44 65 66 48 61 73 68 20 66 6f 72 20 61  uncDefHash for a
25960 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
25961 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 20 20  he given name.  
25962 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e  Return.** a poin
25963 74 65 72 20 74 6f 20 74 68 65 20 6d 61 74 63 68  ter to the match
25964 69 6e 67 20 46 75 6e 63 44 65 66 20 69 66 20 66  ing FuncDef if f
25965 6f 75 6e 64 2c 20 6f 72 20 30 20 69 66 20 74 68  ound, or 0 if th
25966 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e  ere is no match.
25967 0a 2a 2f 0a 73 74 61 74 69 63 20 46 75 6e 63 44  .*/.static FuncD
25968 65 66 20 2a 66 75 6e 63 74 69 6f 6e 53 65 61 72  ef *functionSear
25969 63 68 28 0a 20 20 46 75 6e 63 44 65 66 48 61 73  ch(.  FuncDefHas
2596a 68 20 2a 70 48 61 73 68 2c 20 20 2f 2a 20 48 61  h *pHash,  /* Ha
2596b 73 68 20 74 61 62 6c 65 20 74 6f 20 73 65 61 72  sh table to sear
2596c 63 68 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20  ch */.  int h,  
2596d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2596e 48 61 73 68 20 6f 66 20 74 68 65 20 6e 61 6d 65  Hash of the name
2596f 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
25970 20 2a 7a 46 75 6e 63 2c 20 20 20 2f 2a 20 4e 61   *zFunc,   /* Na
25971 6d 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 2a  me of function *
25972 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 20 20 20  /.  int nFunc   
25973 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
25974 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 7a  er of bytes in z
25975 46 75 6e 63 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e  Func */.){.  Fun
25976 63 44 65 66 20 2a 70 3b 0a 20 20 66 6f 72 28 70  cDef *p;.  for(p
25977 3d 70 48 61 73 68 2d 3e 61 5b 68 5d 3b 20 70 3b  =pHash->a[h]; p;
25978 20 70 3d 70 2d 3e 70 48 61 73 68 29 7b 0a 20 20   p=p->pHash){.  
25979 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2597a 4e 49 43 6d 70 28 70 2d 3e 7a 4e 61 6d 65 2c 20  NICmp(p->zName, 
2597b 7a 46 75 6e 63 2c 20 6e 46 75 6e 63 29 3d 3d 30  zFunc, nFunc)==0
2597c 20 26 26 20 70 2d 3e 7a 4e 61 6d 65 5b 6e 46 75   && p->zName[nFu
2597d 6e 63 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  nc]==0 ){.      
2597e 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 7d 0a  return p;.    }.
2597f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
25980 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
25981 61 20 6e 65 77 20 46 75 6e 63 44 65 66 20 69 6e  a new FuncDef in
25982 74 6f 20 61 20 46 75 6e 63 44 65 66 48 61 73 68  to a FuncDefHash
25983 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a   hash table..*/.
25984 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
25985 6f 69 64 20 73 71 6c 69 74 65 33 46 75 6e 63 44  oid sqlite3FuncD
25986 65 66 49 6e 73 65 72 74 28 0a 20 20 46 75 6e 63  efInsert(.  Func
25987 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 2c 20  DefHash *pHash, 
25988 20 2f 2a 20 54 68 65 20 68 61 73 68 20 74 61 62   /* The hash tab
25989 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 6f  le into which to
2598a 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 46 75 6e   insert */.  Fun
2598b 63 44 65 66 20 2a 70 44 65 66 20 20 20 20 20 20  cDef *pDef      
2598c 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f    /* The functio
2598d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 74 6f 20  n definition to 
2598e 69 6e 73 65 72 74 20 2a 2f 0a 29 7b 0a 20 20 46  insert */.){.  F
2598f 75 6e 63 44 65 66 20 2a 70 4f 74 68 65 72 3b 0a  uncDef *pOther;.
25990 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71    int nName = sq
25991 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44  lite3Strlen30(pD
25992 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 75 38  ef->zName);.  u8
25993 20 63 31 20 3d 20 28 75 38 29 70 44 65 66 2d 3e   c1 = (u8)pDef->
25994 7a 4e 61 6d 65 5b 30 5d 3b 0a 20 20 69 6e 74 20  zName[0];.  int 
25995 68 20 3d 20 28 73 71 6c 69 74 65 33 55 70 70 65  h = (sqlite3Uppe
25996 72 54 6f 4c 6f 77 65 72 5b 63 31 5d 20 2b 20 6e  rToLower[c1] + n
25997 4e 61 6d 65 29 20 25 20 41 72 72 61 79 53 69 7a  Name) % ArraySiz
25998 65 28 70 48 61 73 68 2d 3e 61 29 3b 0a 20 20 70  e(pHash->a);.  p
25999 4f 74 68 65 72 20 3d 20 66 75 6e 63 74 69 6f 6e  Other = function
2599a 53 65 61 72 63 68 28 70 48 61 73 68 2c 20 68 2c  Search(pHash, h,
2599b 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e   pDef->zName, nN
2599c 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4f 74 68  ame);.  if( pOth
2599d 65 72 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  er ){.    assert
2599e 28 20 70 4f 74 68 65 72 21 3d 70 44 65 66 20 26  ( pOther!=pDef &
2599f 26 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 21  & pOther->pNext!
259a0 3d 70 44 65 66 20 29 3b 0a 20 20 20 20 70 44 65  =pDef );.    pDe
259a1 66 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 74 68 65  f->pNext = pOthe
259a2 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70 4f  r->pNext;.    pO
259a3 74 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20 70 44  ther->pNext = pD
259a4 65 66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ef;.  }else{.   
259a5 20 70 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20 30   pDef->pNext = 0
259a6 3b 0a 20 20 20 20 70 44 65 66 2d 3e 70 48 61 73  ;.    pDef->pHas
259a7 68 20 3d 20 70 48 61 73 68 2d 3e 61 5b 68 5d 3b  h = pHash->a[h];
259a8 0a 20 20 20 20 70 48 61 73 68 2d 3e 61 5b 68 5d  .    pHash->a[h]
259a9 20 3d 20 70 44 65 66 3b 0a 20 20 7d 0a 7d 0a 20   = pDef;.  }.}. 
259aa 20 0a 20 20 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61   .  ../*.** Loca
259ab 74 65 20 61 20 75 73 65 72 20 66 75 6e 63 74 69  te a user functi
259ac 6f 6e 20 67 69 76 65 6e 20 61 20 6e 61 6d 65 2c  on given a name,
259ad 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67   a number of arg
259ae 75 6d 65 6e 74 73 20 61 6e 64 20 61 20 66 6c 61  uments and a fla
259af 67 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20  g.** indicating 
259b0 77 68 65 74 68 65 72 20 74 68 65 20 66 75 6e 63  whether the func
259b1 74 69 6f 6e 20 70 72 65 66 65 72 73 20 55 54 46  tion prefers UTF
259b2 2d 31 36 20 6f 76 65 72 20 55 54 46 2d 38 2e 20  -16 over UTF-8. 
259b3 20 52 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69   Return a.** poi
259b4 6e 74 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63  nter to the Func
259b5 44 65 66 20 73 74 72 75 63 74 75 72 65 20 74 68  Def structure th
259b6 61 74 20 64 65 66 69 6e 65 73 20 74 68 61 74 20  at defines that 
259b7 66 75 6e 63 74 69 6f 6e 2c 20 6f 72 20 72 65 74  function, or ret
259b8 75 72 6e 0a 2a 2a 20 4e 55 4c 4c 20 69 66 20 74  urn.** NULL if t
259b9 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73  he function does
259ba 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a 0a 2a   not exist..**.*
259bb 2a 20 49 66 20 74 68 65 20 63 72 65 61 74 65 46  * If the createF
259bc 6c 61 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20  lag argument is 
259bd 74 72 75 65 2c 20 74 68 65 6e 20 61 20 6e 65 77  true, then a new
259be 20 28 62 6c 61 6e 6b 29 20 46 75 6e 63 44 65 66   (blank) FuncDef
259bf 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73  .** structure is
259c0 20 63 72 65 61 74 65 64 20 61 6e 64 20 6c 69 6b   created and lik
259c1 65 64 20 69 6e 74 6f 20 74 68 65 20 22 64 62 22  ed into the "db"
259c2 20 73 74 72 75 63 74 75 72 65 20 69 66 20 61 0a   structure if a.
259c3 2a 2a 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 66  ** no matching f
259c4 75 6e 63 74 69 6f 6e 20 70 72 65 76 69 6f 75 73  unction previous
259c5 6c 79 20 65 78 69 73 74 65 64 2e 20 20 57 68 65  ly existed.  Whe
259c6 6e 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20  n createFlag is 
259c7 74 72 75 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20  true.** and the 
259c8 6e 41 72 67 20 70 61 72 61 6d 65 74 65 72 20 69  nArg parameter i
259c9 73 20 2d 31 2c 20 74 68 65 6e 20 6f 6e 6c 79 20  s -1, then only 
259ca 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  a function that 
259cb 61 63 63 65 70 74 73 0a 2a 2a 20 61 6e 79 20 6e  accepts.** any n
259cc 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
259cd 74 73 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  ts will be retur
259ce 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 72  ned..**.** If cr
259cf 65 61 74 65 46 6c 61 67 20 69 73 20 66 61 6c 73  eateFlag is fals
259d0 65 20 61 6e 64 20 6e 41 72 67 20 69 73 20 2d 31  e and nArg is -1
259d1 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74  , then the first
259d2 20 76 61 6c 69 64 0a 2a 2a 20 66 75 6e 63 74 69   valid.** functi
259d3 6f 6e 20 66 6f 75 6e 64 20 69 73 20 72 65 74 75  on found is retu
259d4 72 6e 65 64 2e 20 20 41 20 66 75 6e 63 74 69 6f  rned.  A functio
259d5 6e 20 69 73 20 76 61 6c 69 64 20 69 66 20 65 69  n is valid if ei
259d6 74 68 65 72 20 78 46 75 6e 63 0a 2a 2a 20 6f 72  ther xFunc.** or
259d7 20 78 53 74 65 70 20 69 73 20 6e 6f 6e 2d 7a 65   xStep is non-ze
259d8 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 63 72 65  ro..**.** If cre
259d9 61 74 65 46 6c 61 67 20 69 73 20 66 61 6c 73 65  ateFlag is false
259da 2c 20 74 68 65 6e 20 61 20 66 75 6e 63 74 69 6f  , then a functio
259db 6e 20 77 69 74 68 20 74 68 65 20 72 65 71 75 69  n with the requi
259dc 72 65 64 20 6e 61 6d 65 20 61 6e 64 0a 2a 2a 20  red name and.** 
259dd 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
259de 6e 74 73 20 6d 61 79 20 62 65 20 72 65 74 75 72  nts may be retur
259df 6e 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 20  ned even if the 
259e0 65 54 65 78 74 52 65 70 20 66 6c 61 67 20 64 6f  eTextRep flag do
259e1 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68 20  es not.** match 
259e2 74 68 61 74 20 72 65 71 75 65 73 74 65 64 2e 0a  that requested..
259e3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
259e4 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74  E FuncDef *sqlit
259e5 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 0a  e3FindFunction(.
259e6 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
259e7 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70 65 6e 20       /* An open 
259e8 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f  database */.  co
259e9 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
259ea 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
259eb 66 75 6e 63 74 69 6f 6e 2e 20 20 4e 6f 74 20 6e  function.  Not n
259ec 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 2a  ull-terminated *
259ed 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 20 20  /.  int nName,  
259ee 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
259ef 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69   of characters i
259f0 6e 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  n the name */.  
259f1 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20  int nArg,       
259f2 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
259f3 61 72 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d  arguments.  -1 m
259f4 65 61 6e 73 20 61 6e 79 20 6e 75 6d 62 65 72 20  eans any number 
259f5 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20  */.  u8 enc,    
259f6 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65          /* Prefe
259f7 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69  rred text encodi
259f8 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61  ng */.  int crea
259f9 74 65 46 6c 61 67 20 20 20 20 20 2f 2a 20 43 72  teFlag     /* Cr
259fa 65 61 74 65 20 6e 65 77 20 65 6e 74 72 79 20 69  eate new entry i
259fb 66 20 74 72 75 65 20 61 6e 64 20 64 6f 65 73 20  f true and does 
259fc 6e 6f 74 20 6f 74 68 65 72 77 69 73 65 20 65 78  not otherwise ex
259fd 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 46 75 6e 63  ist */.){.  Func
259fe 44 65 66 20 2a 70 3b 20 20 20 20 20 20 20 20 20  Def *p;         
259ff 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
25a00 61 62 6c 65 20 2a 2f 0a 20 20 46 75 6e 63 44 65  able */.  FuncDe
25a01 66 20 2a 70 42 65 73 74 20 3d 20 30 3b 20 2f 2a  f *pBest = 0; /*
25a02 20 42 65 73 74 20 6d 61 74 63 68 20 66 6f 75 6e   Best match foun
25a03 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e  d so far */.  in
25a04 74 20 62 65 73 74 53 63 6f 72 65 20 3d 20 30 3b  t bestScore = 0;
25a05 20 20 2f 2a 20 53 63 6f 72 65 20 6f 66 20 62 65    /* Score of be
25a06 73 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 69 6e  st match */.  in
25a07 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  t h;            
25a08 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20    /* Hash value 
25a09 2a 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 65  */...  assert( e
25a0a 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc==SQLITE_UTF8 
25a0b 7c 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  || enc==SQLITE_U
25a0c 54 46 31 36 4c 45 20 7c 7c 20 65 6e 63 3d 3d 53  TF16LE || enc==S
25a0d 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b  QLITE_UTF16BE );
25a0e 0a 20 20 68 20 3d 20 28 73 71 6c 69 74 65 33 55  .  h = (sqlite3U
25a0f 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29  pperToLower[(u8)
25a10 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20 6e 4e 61 6d  zName[0]] + nNam
25a11 65 29 20 25 20 41 72 72 61 79 53 69 7a 65 28 64  e) % ArraySize(d
25a12 62 2d 3e 61 46 75 6e 63 2e 61 29 3b 0a 0a 20 20  b->aFunc.a);..  
25a13 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 20  /* First search 
25a14 66 6f 72 20 61 20 6d 61 74 63 68 20 61 6d 6f 6e  for a match amon
25a15 67 73 74 20 74 68 65 20 61 70 70 6c 69 63 61 74  gst the applicat
25a16 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
25a17 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 20  tions..  */.  p 
25a18 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68  = functionSearch
25a19 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20 68 2c 20  (&db->aFunc, h, 
25a1a 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20  zName, nName);. 
25a1b 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
25a1c 20 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d 61 74   int score = mat
25a1d 63 68 51 75 61 6c 69 74 79 28 70 2c 20 6e 41 72  chQuality(p, nAr
25a1e 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20 69 66 28  g, enc);.    if(
25a1f 20 73 63 6f 72 65 3e 62 65 73 74 53 63 6f 72 65   score>bestScore
25a20 20 29 7b 0a 20 20 20 20 20 20 70 42 65 73 74 20   ){.      pBest 
25a21 3d 20 70 3b 0a 20 20 20 20 20 20 62 65 73 74 53  = p;.      bestS
25a22 63 6f 72 65 20 3d 20 73 63 6f 72 65 3b 0a 20 20  core = score;.  
25a23 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70    }.    p = p->p
25a24 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Next;.  }..  /* 
25a25 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66  If no match is f
25a26 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 74 68 65  ound, search the
25a27 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69   built-in functi
25a28 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45  ons..  **.  ** E
25a29 78 63 65 70 74 2c 20 69 66 20 63 72 65 61 74 65  xcept, if create
25a2a 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68  Flag is true, th
25a2b 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  at means that we
25a2c 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 0a 20   are trying to. 
25a2d 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6e 65   ** install a ne
25a2e 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20 57 68 61  w function.  Wha
25a2f 74 65 76 65 72 20 46 75 6e 63 44 65 66 20 73 74  tever FuncDef st
25a30 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72  ructure is retur
25a31 6e 65 64 20 77 69 6c 6c 0a 20 20 2a 2a 20 68 61  ned will.  ** ha
25a32 76 65 20 66 69 65 6c 64 73 20 6f 76 65 72 77 72  ve fields overwr
25a33 69 74 74 65 6e 20 77 69 74 68 20 6e 65 77 20 69  itten with new i
25a34 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f  nformation appro
25a35 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 0a 20  priate for the. 
25a36 20 2a 2a 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e   ** new function
25a37 2e 20 20 42 75 74 20 74 68 65 20 46 75 6e 63 44  .  But the FuncD
25a38 65 66 73 20 66 6f 72 20 62 75 69 6c 74 2d 69 6e  efs for built-in
25a39 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 72   functions are r
25a3a 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53  ead-only..  ** S
25a3b 6f 20 77 65 20 6d 75 73 74 20 6e 6f 74 20 73 65  o we must not se
25a3c 61 72 63 68 20 66 6f 72 20 62 75 69 6c 74 2d 69  arch for built-i
25a3d 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67  ns when creating
25a3e 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e   a new function.
25a3f 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 21 63 72  .  */ .  if( !cr
25a40 65 61 74 65 46 6c 61 67 20 26 26 20 21 70 42 65  eateFlag && !pBe
25a41 73 74 20 29 7b 0a 20 20 20 20 46 75 6e 63 44 65  st ){.    FuncDe
25a42 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26  fHash *pHash = &
25a43 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61  GLOBAL(FuncDefHa
25a44 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  sh, sqlite3Globa
25a45 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 20  lFunctions);.   
25a46 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61   p = functionSea
25a47 72 63 68 28 70 48 61 73 68 2c 20 68 2c 20 7a 4e  rch(pHash, h, zN
25a48 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
25a49 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
25a4a 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d     int score = m
25a4b 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c 20 6e  atchQuality(p, n
25a4c 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20 20  Arg, enc);.     
25a4d 20 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74 53   if( score>bestS
25a4e 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20  core ){.        
25a4f 70 42 65 73 74 20 3d 20 70 3b 0a 20 20 20 20 20  pBest = p;.     
25a50 20 20 20 62 65 73 74 53 63 6f 72 65 20 3d 20 73     bestScore = s
25a51 63 6f 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  core;.      }.  
25a52 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74      p = p->pNext
25a53 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
25a54 2a 20 49 66 20 74 68 65 20 63 72 65 61 74 65 46  * If the createF
25a55 6c 61 67 20 70 61 72 61 6d 65 74 65 72 20 69 73  lag parameter is
25a56 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 73 65   true and the se
25a57 61 72 63 68 20 64 69 64 20 6e 6f 74 20 72 65 76  arch did not rev
25a58 65 61 6c 20 61 6e 0a 20 20 2a 2a 20 65 78 61 63  eal an.  ** exac
25a59 74 20 6d 61 74 63 68 20 66 6f 72 20 74 68 65 20  t match for the 
25a5a 6e 61 6d 65 2c 20 6e 75 6d 62 65 72 20 6f 66 20  name, number of 
25a5b 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 65 6e  arguments and en
25a5c 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 61 64 64  coding, then add
25a5d 20 61 0a 20 20 2a 2a 20 6e 65 77 20 65 6e 74 72   a.  ** new entr
25a5e 79 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  y to the hash ta
25a5f 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 69  ble and return i
25a60 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 72  t..  */.  if( cr
25a61 65 61 74 65 46 6c 61 67 20 26 26 20 28 62 65 73  eateFlag && (bes
25a62 74 53 63 6f 72 65 3c 36 20 7c 7c 20 70 42 65 73  tScore<6 || pBes
25a63 74 2d 3e 6e 41 72 67 21 3d 6e 41 72 67 29 20 26  t->nArg!=nArg) &
25a64 26 20 0a 20 20 20 20 20 20 28 70 42 65 73 74 20  & .      (pBest 
25a65 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
25a66 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
25a67 28 2a 70 42 65 73 74 29 2b 6e 4e 61 6d 65 2b 31  (*pBest)+nName+1
25a68 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 42 65  ))!=0 ){.    pBe
25a69 73 74 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  st->zName = (cha
25a6a 72 20 2a 29 26 70 42 65 73 74 5b 31 5d 3b 0a 20  r *)&pBest[1];. 
25a6b 20 20 20 70 42 65 73 74 2d 3e 6e 41 72 67 20 3d     pBest->nArg =
25a6c 20 28 75 31 36 29 6e 41 72 67 3b 0a 20 20 20 20   (u16)nArg;.    
25a6d 70 42 65 73 74 2d 3e 69 50 72 65 66 45 6e 63 20  pBest->iPrefEnc 
25a6e 3d 20 65 6e 63 3b 0a 20 20 20 20 6d 65 6d 63 70  = enc;.    memcp
25a6f 79 28 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20  y(pBest->zName, 
25a70 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20  zName, nName);. 
25a71 20 20 20 70 42 65 73 74 2d 3e 7a 4e 61 6d 65 5b     pBest->zName[
25a72 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20  nName] = 0;.    
25a73 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e  sqlite3FuncDefIn
25a74 73 65 72 74 28 26 64 62 2d 3e 61 46 75 6e 63 2c  sert(&db->aFunc,
25a75 20 70 42 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20   pBest);.  }..  
25a76 69 66 28 20 70 42 65 73 74 20 26 26 20 28 70 42  if( pBest && (pB
25a77 65 73 74 2d 3e 78 53 74 65 70 20 7c 7c 20 70 42  est->xStep || pB
25a78 65 73 74 2d 3e 78 46 75 6e 63 20 7c 7c 20 63 72  est->xFunc || cr
25a79 65 61 74 65 46 6c 61 67 29 20 29 7b 0a 20 20 20  eateFlag) ){.   
25a7a 20 72 65 74 75 72 6e 20 70 42 65 73 74 3b 0a 20   return pBest;. 
25a7b 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
25a7c 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c  ../*.** Free all
25a7d 20 72 65 73 6f 75 72 63 65 73 20 68 65 6c 64 20   resources held 
25a7e 62 79 20 74 68 65 20 73 63 68 65 6d 61 20 73 74  by the schema st
25a7f 72 75 63 74 75 72 65 2e 20 54 68 65 20 76 6f 69  ructure. The voi
25a80 64 2a 20 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e  d* argument poin
25a81 74 73 0a 2a 2a 20 61 74 20 61 20 53 63 68 65 6d  ts.** at a Schem
25a82 61 20 73 74 72 75 63 74 2e 20 54 68 69 73 20 66  a struct. This f
25a83 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  unction does not
25a84 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46   call sqlite3DbF
25a85 72 65 65 28 64 62 2c 20 29 20 6f 6e 20 74 68 65  ree(db, ) on the
25a86 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 74 73   .** pointer its
25a87 65 6c 66 2c 20 69 74 20 6a 75 73 74 20 63 6c 65  elf, it just cle
25a88 61 6e 73 20 75 70 20 73 75 62 73 69 64 75 61 72  ans up subsiduar
25a89 79 20 72 65 73 6f 75 72 63 65 73 20 28 69 2e 65  y resources (i.e
25a8a 2e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 2a  . the contents.*
25a8b 2a 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20  * of the schema 
25a8c 68 61 73 68 20 74 61 62 6c 65 73 29 2e 0a 2a 2a  hash tables)..**
25a8d 0a 2a 2a 20 54 68 65 20 53 63 68 65 6d 61 2e 63  .** The Schema.c
25a8e 61 63 68 65 5f 73 69 7a 65 20 76 61 72 69 61 62  ache_size variab
25a8f 6c 65 20 69 73 20 6e 6f 74 20 63 6c 65 61 72 65  le is not cleare
25a90 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
25a91 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
25a92 33 53 63 68 65 6d 61 46 72 65 65 28 76 6f 69 64  3SchemaFree(void
25a93 20 2a 70 29 7b 0a 20 20 48 61 73 68 20 74 65 6d   *p){.  Hash tem
25a94 70 31 3b 0a 20 20 48 61 73 68 20 74 65 6d 70 32  p1;.  Hash temp2
25a95 3b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 45  ;.  HashElem *pE
25a96 6c 65 6d 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70  lem;.  Schema *p
25a97 53 63 68 65 6d 61 20 3d 20 28 53 63 68 65 6d 61  Schema = (Schema
25a98 20 2a 29 70 3b 0a 0a 20 20 74 65 6d 70 31 20 3d   *)p;..  temp1 =
25a99 20 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73   pSchema->tblHas
25a9a 68 3b 0a 20 20 74 65 6d 70 32 20 3d 20 70 53 63  h;.  temp2 = pSc
25a9b 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 3b 0a  hema->trigHash;.
25a9c 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
25a9d 74 28 26 70 53 63 68 65 6d 61 2d 3e 74 72 69 67  t(&pSchema->trig
25a9e 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74 65 33  Hash);.  sqlite3
25a9f 48 61 73 68 43 6c 65 61 72 28 26 70 53 63 68 65  HashClear(&pSche
25aa0 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b 0a 20 20  ma->idxHash);.  
25aa1 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65  for(pElem=sqlite
25aa2 48 61 73 68 46 69 72 73 74 28 26 74 65 6d 70 32  HashFirst(&temp2
25aa3 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d  ); pElem; pElem=
25aa4 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
25aa5 45 6c 65 6d 29 29 7b 0a 20 20 20 20 73 71 6c 69  Elem)){.    sqli
25aa6 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
25aa7 28 30 2c 20 28 54 72 69 67 67 65 72 2a 29 73 71  (0, (Trigger*)sq
25aa8 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c  liteHashData(pEl
25aa9 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  em));.  }.  sqli
25aaa 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 74 65  te3HashClear(&te
25aab 6d 70 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 48  mp2);.  sqlite3H
25aac 61 73 68 49 6e 69 74 28 26 70 53 63 68 65 6d 61  ashInit(&pSchema
25aad 2d 3e 74 62 6c 48 61 73 68 29 3b 0a 20 20 66 6f  ->tblHash);.  fo
25aae 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61  r(pElem=sqliteHa
25aaf 73 68 46 69 72 73 74 28 26 74 65 6d 70 31 29 3b  shFirst(&temp1);
25ab0 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71   pElem; pElem=sq
25ab1 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c  liteHashNext(pEl
25ab2 65 6d 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20  em)){.    Table 
25ab3 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61  *pTab = sqliteHa
25ab4 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20  shData(pElem);. 
25ab5 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d     assert( pTab-
25ab6 3e 64 62 4d 65 6d 3d 3d 30 20 29 3b 0a 20 20 20  >dbMem==0 );.   
25ab7 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
25ab8 62 6c 65 28 70 54 61 62 29 3b 0a 20 20 7d 0a 20  ble(pTab);.  }. 
25ab9 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
25aba 72 28 26 74 65 6d 70 31 29 3b 0a 20 20 73 71 6c  r(&temp1);.  sql
25abb 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 70  ite3HashClear(&p
25abc 53 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68  Schema->fkeyHash
25abd 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d 3e 70 53  );.  pSchema->pS
25abe 65 71 54 61 62 20 3d 20 30 3b 0a 20 20 70 53 63  eqTab = 0;.  pSc
25abf 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  hema->flags &= ~
25ac0 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 3b  DB_SchemaLoaded;
25ac1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61  .}../*.** Find a
25ac2 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 63  nd return the sc
25ac3 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20  hema associated 
25ac4 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 43  with a BTree.  C
25ac5 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6f  reate.** a new o
25ac6 6e 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  ne if necessary.
25ac7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
25ac8 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74  TE Schema *sqlit
25ac9 65 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69  e3SchemaGet(sqli
25aca 74 65 33 20 2a 64 62 2c 20 42 74 72 65 65 20 2a  te3 *db, Btree *
25acb 70 42 74 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a  pBt){.  Schema *
25acc 20 70 3b 0a 20 20 69 66 28 20 70 42 74 20 29 7b   p;.  if( pBt ){
25acd 0a 20 20 20 20 70 20 3d 20 28 53 63 68 65 6d 61  .    p = (Schema
25ace 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 53   *)sqlite3BtreeS
25acf 63 68 65 6d 61 28 70 42 74 2c 20 73 69 7a 65 6f  chema(pBt, sizeo
25ad0 66 28 53 63 68 65 6d 61 29 2c 20 73 71 6c 69 74  f(Schema), sqlit
25ad1 65 33 53 63 68 65 6d 61 46 72 65 65 29 3b 0a 20  e3SchemaFree);. 
25ad2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20   }else{.    p = 
25ad3 28 53 63 68 65 6d 61 20 2a 29 73 71 6c 69 74 65  (Schema *)sqlite
25ad4 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65  3MallocZero(size
25ad5 6f 66 28 53 63 68 65 6d 61 29 29 3b 0a 20 20 7d  of(Schema));.  }
25ad6 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20  .  if( !p ){.   
25ad7 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
25ad8 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69  d = 1;.  }else i
25ad9 66 20 28 20 30 3d 3d 70 2d 3e 66 69 6c 65 5f 66  f ( 0==p->file_f
25ada 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 73 71 6c  ormat ){.    sql
25adb 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d  ite3HashInit(&p-
25adc 3e 74 62 6c 48 61 73 68 29 3b 0a 20 20 20 20 73  >tblHash);.    s
25add 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
25ade 70 2d 3e 69 64 78 48 61 73 68 29 3b 0a 20 20 20  p->idxHash);.   
25adf 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74   sqlite3HashInit
25ae0 28 26 70 2d 3e 74 72 69 67 48 61 73 68 29 3b 0a  (&p->trigHash);.
25ae1 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
25ae2 6e 69 74 28 26 70 2d 3e 66 6b 65 79 48 61 73 68  nit(&p->fkeyHash
25ae3 29 3b 0a 20 20 20 20 70 2d 3e 65 6e 63 20 3d 20  );.    p->enc = 
25ae4 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 7d  SQLITE_UTF8;.  }
25ae5 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
25ae6 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
25ae7 45 6e 64 20 6f 66 20 63 61 6c 6c 62 61 63 6b 2e  End of callback.
25ae8 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
25ae9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25aea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
25aeb 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
25aec 42 65 67 69 6e 20 66 69 6c 65 20 64 65 6c 65 74  Begin file delet
25aed 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
25aee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25aef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
25af0 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
25af1 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
25af2 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
25af3 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
25af4 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
25af5 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
25af6 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
25af7 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
25af8 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
25af9 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
25afa 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
25afb 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
25afc 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
25afd 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
25afe 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
25aff 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
25b00 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
25b01 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
25b02 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
25b03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25b04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25b05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25b06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25b07 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
25b08 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ile contains C c
25b09 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ode routines tha
25b0a 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20  t are called by 
25b0b 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 69 6e  the parser.** in
25b0c 20 6f 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61   order to genera
25b0d 74 65 20 63 6f 64 65 20 66 6f 72 20 44 45 4c 45  te code for DELE
25b0e 54 45 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e  TE FROM statemen
25b0f 74 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  ts..*/../*.** Lo
25b10 6f 6b 20 75 70 20 65 76 65 72 79 20 74 61 62 6c  ok up every tabl
25b11 65 20 74 68 61 74 20 69 73 20 6e 61 6d 65 64 20  e that is named 
25b12 69 6e 20 70 53 72 63 2e 20 20 49 66 20 61 6e 79  in pSrc.  If any
25b13 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 66 6f   table is not fo
25b14 75 6e 64 2c 0a 2a 2a 20 61 64 64 20 61 6e 20 65  und,.** add an e
25b15 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20  rror message to 
25b16 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20  pParse->zErrMsg 
25b17 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  and return NULL.
25b18 20 20 49 66 20 61 6c 6c 20 74 61 62 6c 65 73 0a    If all tables.
25b19 2a 2a 20 61 72 65 20 66 6f 75 6e 64 2c 20 72 65  ** are found, re
25b1a 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
25b1b 6f 20 74 68 65 20 6c 61 73 74 20 74 61 62 6c 65  o the last table
25b1c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
25b1d 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74  ATE Table *sqlit
25b1e 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28  e3SrcListLookup(
25b1f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
25b20 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20  rcList *pSrc){. 
25b21 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
25b22 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53  item *pItem = pS
25b23 72 63 2d 3e 61 3b 0a 20 20 54 61 62 6c 65 20 2a  rc->a;.  Table *
25b24 70 54 61 62 3b 0a 20 20 61 73 73 65 72 74 28 20  pTab;.  assert( 
25b25 70 49 74 65 6d 20 26 26 20 70 53 72 63 2d 3e 6e  pItem && pSrc->n
25b26 53 72 63 3d 3d 31 20 29 3b 0a 20 20 70 54 61 62  Src==1 );.  pTab
25b27 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
25b28 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
25b29 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70   pItem->zName, p
25b2a 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29  Item->zDatabase)
25b2b 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74  ;.  sqlite3Delet
25b2c 65 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e 70 54  eTable(pItem->pT
25b2d 61 62 29 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 54  ab);.  pItem->pT
25b2e 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 69 66 28  ab = pTab;.  if(
25b2f 20 70 54 61 62 20 29 7b 0a 20 20 20 20 70 54 61   pTab ){.    pTa
25b30 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20  b->nRef++;.  }. 
25b31 20 69 66 28 20 73 71 6c 69 74 65 33 49 6e 64 65   if( sqlite3Inde
25b32 78 65 64 42 79 4c 6f 6f 6b 75 70 28 70 50 61 72  xedByLookup(pPar
25b33 73 65 2c 20 70 49 74 65 6d 29 20 29 7b 0a 20 20  se, pItem) ){.  
25b34 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 7d 0a    pTab = 0;.  }.
25b35 20 20 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d    return pTab;.}
25b36 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  ../*.** Check to
25b37 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 67   make sure the g
25b38 69 76 65 6e 20 74 61 62 6c 65 20 69 73 20 77 72  iven table is wr
25b39 69 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20 69  itable.  If it i
25b3a 73 20 6e 6f 74 0a 2a 2a 20 77 72 69 74 61 62 6c  s not.** writabl
25b3b 65 2c 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65  e, generate an e
25b3c 72 72 6f 72 20 6d 65 73 73 61 67 65 20 61 6e 64  rror message and
25b3d 20 72 65 74 75 72 6e 20 31 2e 20 20 49 66 20 69   return 1.  If i
25b3e 74 20 69 73 0a 2a 2a 20 77 72 69 74 61 62 6c 65  t is.** writable
25b3f 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2f 0a 53 51   return 0;.*/.SQ
25b40 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
25b41 20 73 71 6c 69 74 65 33 49 73 52 65 61 64 4f 6e   sqlite3IsReadOn
25b42 6c 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ly(Parse *pParse
25b43 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 69  , Table *pTab, i
25b44 6e 74 20 76 69 65 77 4f 6b 29 7b 0a 20 20 2f 2a  nt viewOk){.  /*
25b45 20 41 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20   A table is not 
25b46 77 72 69 74 61 62 6c 65 20 75 6e 64 65 72 20 74  writable under t
25b47 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 69 72  he following cir
25b48 63 75 6d 73 74 61 6e 63 65 73 3a 0a 20 20 2a 2a  cumstances:.  **
25b49 0a 20 20 2a 2a 20 20 20 31 29 20 49 74 20 69 73  .  **   1) It is
25b4a 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
25b4b 20 61 6e 64 20 6e 6f 20 69 6d 70 6c 65 6d 65 6e   and no implemen
25b4c 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 78 55  tation of the xU
25b4d 70 64 61 74 65 20 6d 65 74 68 6f 64 0a 20 20 2a  pdate method.  *
25b4e 2a 20 20 20 20 20 20 68 61 73 20 62 65 65 6e 20  *      has been 
25b4f 70 72 6f 76 69 64 65 64 2c 20 6f 72 0a 20 20 2a  provided, or.  *
25b50 2a 20 20 20 32 29 20 49 74 20 69 73 20 61 20 73  *   2) It is a s
25b51 79 73 74 65 6d 20 74 61 62 6c 65 20 28 69 2e 65  ystem table (i.e
25b52 2e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 29  . sqlite_master)
25b53 2c 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 6e  , this call is n
25b54 6f 74 0a 20 20 2a 2a 20 20 20 20 20 20 70 61 72  ot.  **      par
25b55 74 20 6f 66 20 61 20 6e 65 73 74 65 64 20 70 61  t of a nested pa
25b56 72 73 65 20 61 6e 64 20 77 72 69 74 61 62 6c 65  rse and writable
25b57 5f 73 63 68 65 6d 61 20 70 72 61 67 6d 61 20 68  _schema pragma h
25b58 61 73 20 6e 6f 74 20 0a 20 20 2a 2a 20 20 20 20  as not .  **    
25b59 20 20 62 65 65 6e 20 73 70 65 63 69 66 69 65 64    been specified
25b5a 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 65  ..  **.  ** In e
25b5b 69 74 68 65 72 20 63 61 73 65 20 6c 65 61 76 65  ither case leave
25b5c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
25b5d 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20  e in pParse and 
25b5e 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e  return non-zero.
25b5f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 20 49 73  .  */.  if( ( Is
25b60 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 0a 20  Virtual(pTab) . 
25b61 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 47 65      && sqlite3Ge
25b62 74 56 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e  tVTable(pParse->
25b63 64 62 2c 20 70 54 61 62 29 2d 3e 70 4d 6f 64 2d  db, pTab)->pMod-
25b64 3e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74  >pModule->xUpdat
25b65 65 3d 3d 30 20 29 0a 20 20 20 7c 7c 20 28 20 28  e==0 ).   || ( (
25b66 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
25b67 20 54 46 5f 52 65 61 64 6f 6e 6c 79 29 21 3d 30   TF_Readonly)!=0
25b68 0a 20 20 20 20 20 26 26 20 28 70 50 61 72 73 65  .     && (pParse
25b69 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
25b6a 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61  LITE_WriteSchema
25b6b 29 3d 3d 30 0a 20 20 20 20 20 26 26 20 70 50 61  )==0.     && pPa
25b6c 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
25b6d 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  .  ){.    sqlite
25b6e 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
25b6f 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20  , "table %s may 
25b70 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65 64 22  not be modified"
25b71 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
25b72 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
25b73 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
25b74 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66  E_OMIT_VIEW.  if
25b75 28 20 21 76 69 65 77 4f 6b 20 26 26 20 70 54 61  ( !viewOk && pTa
25b76 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  b->pSelect ){.  
25b77 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
25b78 67 28 70 50 61 72 73 65 2c 22 63 61 6e 6e 6f 74  g(pParse,"cannot
25b79 20 6d 6f 64 69 66 79 20 25 73 20 62 65 63 61 75   modify %s becau
25b7a 73 65 20 69 74 20 69 73 20 61 20 76 69 65 77 22  se it is a view"
25b7b 2c 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  ,pTab->zName);. 
25b7c 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
25b7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
25b7e 20 30 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64 65 66   0;.}...#if !def
25b7f 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
25b80 5f 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e  _VIEW) && !defin
25b81 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
25b82 52 49 47 47 45 52 29 0a 2f 2a 0a 2a 2a 20 45 76  RIGGER)./*.** Ev
25b83 61 6c 75 61 74 65 20 61 20 76 69 65 77 20 61 6e  aluate a view an
25b84 64 20 73 74 6f 72 65 20 69 74 73 20 72 65 73 75  d store its resu
25b85 6c 74 20 69 6e 20 61 6e 20 65 70 68 65 6d 65 72  lt in an ephemer
25b86 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 0a 2a  al table.  The.*
25b87 2a 20 70 57 68 65 72 65 20 61 72 67 75 6d 65 6e  * pWhere argumen
25b88 74 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61 6c  t is an optional
25b89 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 68   WHERE clause th
25b8a 61 74 20 72 65 73 74 72 69 63 74 73 20 74 68 65  at restricts the
25b8b 0a 2a 2a 20 73 65 74 20 6f 66 20 72 6f 77 73 20  .** set of rows 
25b8c 69 6e 20 74 68 65 20 76 69 65 77 20 74 68 61 74  in the view that
25b8d 20 61 72 65 20 74 6f 20 62 65 20 61 64 64 65 64   are to be added
25b8e 20 74 6f 20 74 68 65 20 65 70 68 65 6d 65 72 61   to the ephemera
25b8f 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  l table..*/.SQLI
25b90 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
25b91 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61 6c 69  sqlite3Materiali
25b92 7a 65 56 69 65 77 28 0a 20 20 50 61 72 73 65 20  zeView(.  Parse 
25b93 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
25b94 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
25b95 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 56  t */.  Table *pV
25b96 69 65 77 2c 20 20 20 20 20 20 20 20 2f 2a 20 56  iew,        /* V
25b97 69 65 77 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a  iew definition *
25b98 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65  /.  Expr *pWhere
25b99 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69  ,        /* Opti
25b9a 6f 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73  onal WHERE claus
25b9b 65 20 74 6f 20 62 65 20 61 64 64 65 64 20 2a 2f  e to be added */
25b9c 0a 20 20 69 6e 74 20 69 43 75 72 20 20 20 20 20  .  int iCur     
25b9d 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
25b9e 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 65 70 68  r number for eph
25b9f 65 6d 65 72 69 61 6c 20 74 61 62 6c 65 20 2a 2f  emerial table */
25ba0 0a 29 7b 0a 20 20 53 65 6c 65 63 74 44 65 73 74  .){.  SelectDest
25ba1 20 64 65 73 74 3b 0a 20 20 53 65 6c 65 63 74 20   dest;.  Select 
25ba2 2a 70 44 75 70 3b 0a 20 20 73 71 6c 69 74 65 33  *pDup;.  sqlite3
25ba3 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
25ba4 62 3b 0a 0a 20 20 70 44 75 70 20 3d 20 73 71 6c  b;..  pDup = sql
25ba5 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62  ite3SelectDup(db
25ba6 2c 20 70 56 69 65 77 2d 3e 70 53 65 6c 65 63 74  , pView->pSelect
25ba7 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 57 68 65  , 0);.  if( pWhe
25ba8 72 65 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73  re ){.    SrcLis
25ba9 74 20 2a 70 46 72 6f 6d 3b 0a 20 20 20 20 0a 20  t *pFrom;.    . 
25baa 20 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69     pWhere = sqli
25bab 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
25bac 57 68 65 72 65 2c 20 30 29 3b 0a 20 20 20 20 70  Where, 0);.    p
25bad 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 53 72  From = sqlite3Sr
25bae 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20  cListAppend(db, 
25baf 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 0, 0);.    if
25bb0 28 20 70 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  ( pFrom ){.     
25bb1 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e   assert( pFrom->
25bb2 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20  nSrc==1 );.     
25bb3 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 7a 41 6c   pFrom->a[0].zAl
25bb4 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ias = sqlite3DbS
25bb5 74 72 44 75 70 28 64 62 2c 20 70 56 69 65 77 2d  trDup(db, pView-
25bb6 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70  >zName);.      p
25bb7 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65  From->a[0].pSele
25bb8 63 74 20 3d 20 70 44 75 70 3b 0a 20 20 20 20 20  ct = pDup;.     
25bb9 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e   assert( pFrom->
25bba 61 5b 30 5d 2e 70 4f 6e 3d 3d 30 20 29 3b 0a 20  a[0].pOn==0 );. 
25bbb 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72       assert( pFr
25bbc 6f 6d 2d 3e 61 5b 30 5d 2e 70 55 73 69 6e 67 3d  om->a[0].pUsing=
25bbd 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  =0 );.    }else{
25bbe 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
25bbf 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
25bc0 44 75 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Dup);.    }.    
25bc1 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 53 65  pDup = sqlite3Se
25bc2 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65 2c 20  lectNew(pParse, 
25bc3 30 2c 20 70 46 72 6f 6d 2c 20 70 57 68 65 72 65  0, pFrom, pWhere
25bc4 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
25bc5 20 30 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74   0);.  }.  sqlit
25bc6 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
25bc7 28 26 64 65 73 74 2c 20 53 52 54 5f 45 70 68 65  (&dest, SRT_Ephe
25bc8 6d 54 61 62 2c 20 69 43 75 72 29 3b 0a 20 20 73  mTab, iCur);.  s
25bc9 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
25bca 72 73 65 2c 20 70 44 75 70 2c 20 26 64 65 73 74  rse, pDup, &dest
25bcb 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
25bcc 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75  ctDelete(db, pDu
25bcd 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  p);.}.#endif /* 
25bce 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
25bcf 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20 21 64  OMIT_VIEW) && !d
25bd0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
25bd1 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a  IT_TRIGGER) */..
25bd2 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
25bd3 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45  TE_ENABLE_UPDATE
25bd4 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26  _DELETE_LIMIT) &
25bd5 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
25bd6 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
25bd7 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
25bd8 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
25bd9 65 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ee to implement 
25bda 74 68 65 20 57 48 45 52 45 2c 20 4f 52 44 45 52  the WHERE, ORDER
25bdb 20 42 59 2c 0a 2a 2a 20 61 6e 64 20 4c 49 4d 49   BY,.** and LIMI
25bdc 54 2f 4f 46 46 53 45 54 20 70 6f 72 74 69 6f 6e  T/OFFSET portion
25bdd 20 6f 66 20 44 45 4c 45 54 45 20 61 6e 64 20 55   of DELETE and U
25bde 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73  PDATE statements
25bdf 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45  ..**.**     DELE
25be0 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78  TE FROM table_wx
25be1 79 7a 20 57 48 45 52 45 20 61 3c 35 20 4f 52 44  yz WHERE a<5 ORD
25be2 45 52 20 42 59 20 61 20 4c 49 4d 49 54 20 31 3b  ER BY a LIMIT 1;
25be3 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
25be4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
25be5 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
25be6 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20 20  __________/.**  
25be7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25be8 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 69               pLi
25be9 6d 69 74 57 68 65 72 65 20 28 70 49 6e 43 6c 61  mitWhere (pInCla
25bea 75 73 65 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  use).*/.SQLITE_P
25beb 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
25bec 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65 28 0a  ite3LimitWhere(.
25bed 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
25bee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25bef 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
25bf0 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73  text */.  SrcLis
25bf1 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20  t *pSrc,        
25bf2 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52         /* the FR
25bf3 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20 77 68 69  OM clause -- whi
25bf4 63 68 20 74 61 62 6c 65 73 20 74 6f 20 73 63 61  ch tables to sca
25bf5 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  n */.  Expr *pWh
25bf6 65 72 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ere,            
25bf7 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
25bf8 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65   clause.  May be
25bf9 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 4c   null */.  ExprL
25bfa 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20  ist *pOrderBy,  
25bfb 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f          /* The O
25bfc 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20  RDER BY clause. 
25bfd 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a   May be null */.
25bfe 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20    Expr *pLimit, 
25bff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
25c00 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75  * The LIMIT clau
25c01 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c  se.  May be null
25c02 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66   */.  Expr *pOff
25c03 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
25c04 20 20 20 2f 2a 20 54 68 65 20 4f 46 46 53 45 54     /* The OFFSET
25c05 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65   clause.  May be
25c06 20 6e 75 6c 6c 20 2a 2f 0a 20 20 63 68 61 72 20   null */.  char 
25c07 2a 7a 53 74 6d 74 54 79 70 65 20 20 20 20 20 20  *zStmtType      
25c08 20 20 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65          /* Eithe
25c09 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 41  r DELETE or UPDA
25c0a 54 45 2e 20 20 46 6f 72 20 65 72 72 6f 72 20 6d  TE.  For error m
25c0b 65 73 73 61 67 65 73 2e 20 2a 2f 0a 29 7b 0a 20  essages. */.){. 
25c0c 20 45 78 70 72 20 2a 70 57 68 65 72 65 52 6f 77   Expr *pWhereRow
25c0d 69 64 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 2f 2a  id = NULL;    /*
25c0e 20 57 48 45 52 45 20 72 6f 77 69 64 20 2e 2e 20   WHERE rowid .. 
25c0f 2a 2f 0a 20 20 45 78 70 72 20 2a 70 49 6e 43 6c  */.  Expr *pInCl
25c10 61 75 73 65 20 3d 20 4e 55 4c 4c 3b 20 20 20 20  ause = NULL;    
25c11 20 20 2f 2a 20 57 48 45 52 45 20 72 6f 77 69 64    /* WHERE rowid
25c12 20 49 4e 20 28 20 73 65 6c 65 63 74 20 29 20 2a   IN ( select ) *
25c13 2f 0a 20 20 45 78 70 72 20 2a 70 53 65 6c 65 63  /.  Expr *pSelec
25c14 74 52 6f 77 69 64 20 3d 20 4e 55 4c 4c 3b 20 20  tRowid = NULL;  
25c15 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f 77 69 64   /* SELECT rowid
25c16 20 2e 2e 2e 20 2a 2f 0a 20 20 45 78 70 72 4c 69   ... */.  ExprLi
25c17 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 4e 55 4c  st *pEList = NUL
25c18 4c 3b 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73  L;     /* Expres
25c19 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e 74 61 6e  sion list contan
25c1a 69 6e 67 20 6f 6e 6c 79 20 70 53 65 6c 65 63 74  ing only pSelect
25c1b 52 6f 77 69 64 20 2a 2f 0a 20 20 53 72 63 4c 69  Rowid */.  SrcLi
25c1c 73 74 20 2a 70 53 65 6c 65 63 74 53 72 63 20 3d  st *pSelectSrc =
25c1d 20 4e 55 4c 4c 3b 20 20 2f 2a 20 53 45 4c 45 43   NULL;  /* SELEC
25c1e 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 78 20 2e  T rowid FROM x .
25c1f 2e 2e 20 28 64 75 70 20 6f 66 20 70 53 72 63 29  .. (dup of pSrc)
25c20 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
25c21 65 6c 65 63 74 20 3d 20 4e 55 4c 4c 3b 20 20 20  elect = NULL;   
25c22 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 53     /* Complete S
25c23 45 4c 45 43 54 20 74 72 65 65 20 2a 2f 0a 0a 20  ELECT tree */.. 
25c24 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74   /* Check that t
25c25 68 65 72 65 20 69 73 6e 27 74 20 61 6e 20 4f 52  here isn't an OR
25c26 44 45 52 20 42 59 20 77 69 74 68 6f 75 74 20 61  DER BY without a
25c27 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a 20   LIMIT clause.. 
25c28 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64 65 72   */.  if( pOrder
25c29 42 79 20 26 26 20 28 70 4c 69 6d 69 74 20 3d 3d  By && (pLimit ==
25c2a 20 30 29 20 29 20 7b 0a 20 20 20 20 73 71 6c 69   0) ) {.    sqli
25c2b 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
25c2c 73 65 2c 20 22 4f 52 44 45 52 20 42 59 20 77 69  se, "ORDER BY wi
25c2d 74 68 6f 75 74 20 4c 49 4d 49 54 20 6f 6e 20 25  thout LIMIT on %
25c2e 73 22 2c 20 7a 53 74 6d 74 54 79 70 65 29 3b 0a  s", zStmtType);.
25c2f 20 20 20 20 70 50 61 72 73 65 2d 3e 70 61 72 73      pParse->pars
25c30 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 20 20  eError = 1;.    
25c31 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65  goto limit_where
25c32 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a  _cleanup_2;.  }.
25c33 0a 20 20 2f 2a 20 57 65 20 6f 6e 6c 79 20 6e 65  .  /* We only ne
25c34 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61  ed to generate a
25c35 20 73 65 6c 65 63 74 20 65 78 70 72 65 73 73 69   select expressi
25c36 6f 6e 20 69 66 20 74 68 65 72 65 0a 20 20 2a 2a  on if there.  **
25c37 20 69 73 20 61 20 6c 69 6d 69 74 2f 6f 66 66 73   is a limit/offs
25c38 65 74 20 74 65 72 6d 20 74 6f 20 65 6e 66 6f 72  et term to enfor
25c39 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ce..  */.  if( p
25c3a 4c 69 6d 69 74 20 3d 3d 20 30 20 29 20 7b 0a 20  Limit == 0 ) {. 
25c3b 20 20 20 2f 2a 20 69 66 20 70 4c 69 6d 69 74 20     /* if pLimit 
25c3c 69 73 20 6e 75 6c 6c 2c 20 70 4f 66 66 73 65 74  is null, pOffset
25c3d 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20   will always be 
25c3e 6e 75 6c 6c 20 61 73 20 77 65 6c 6c 2e 20 2a 2f  null as well. */
25c3f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 66  .    assert( pOf
25c40 66 73 65 74 20 3d 3d 20 30 20 29 3b 0a 20 20 20  fset == 0 );.   
25c41 20 72 65 74 75 72 6e 20 70 57 68 65 72 65 3b 0a   return pWhere;.
25c42 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61    }..  /* Genera
25c43 74 65 20 61 20 73 65 6c 65 63 74 20 65 78 70 72  te a select expr
25c44 65 73 73 69 6f 6e 20 74 72 65 65 20 74 6f 20 65  ession tree to e
25c45 6e 66 6f 72 63 65 20 74 68 65 20 6c 69 6d 69 74  nforce the limit
25c46 2f 6f 66 66 73 65 74 20 0a 20 20 2a 2a 20 74 65  /offset .  ** te
25c47 72 6d 20 66 6f 72 20 74 68 65 20 44 45 4c 45 54  rm for the DELET
25c48 45 20 6f 72 20 55 50 44 41 54 45 20 73 74 61 74  E or UPDATE stat
25c49 65 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78 61 6d  ement.  For exam
25c4a 70 6c 65 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45  ple:.  **   DELE
25c4b 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20  TE FROM table_a 
25c4c 57 48 45 52 45 20 63 6f 6c 31 3d 31 20 4f 52 44  WHERE col1=1 ORD
25c4d 45 52 20 42 59 20 63 6f 6c 32 20 4c 49 4d 49 54  ER BY col2 LIMIT
25c4e 20 31 20 4f 46 46 53 45 54 20 31 0a 20 20 2a 2a   1 OFFSET 1.  **
25c4f 20 62 65 63 6f 6d 65 73 3a 0a 20 20 2a 2a 20 20   becomes:.  **  
25c50 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62   DELETE FROM tab
25c51 6c 65 5f 61 20 57 48 45 52 45 20 72 6f 77 69 64  le_a WHERE rowid
25c52 20 49 4e 20 28 20 0a 20 20 2a 2a 20 20 20 20 20   IN ( .  **     
25c53 53 45 4c 45 43 54 20 72 6f 77 69 64 20 46 52 4f  SELECT rowid FRO
25c54 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20  M table_a WHERE 
25c55 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20 42 59 20  col1=1 ORDER BY 
25c56 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20 4f 46 46  col2 LIMIT 1 OFF
25c57 53 45 54 20 31 0a 20 20 2a 2a 20 20 20 29 3b 0a  SET 1.  **   );.
25c58 20 20 2a 2f 0a 0a 20 20 70 53 65 6c 65 63 74 52    */..  pSelectR
25c59 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 50 45  owid = sqlite3PE
25c5a 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52  xpr(pParse, TK_R
25c5b 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  OW, 0, 0, 0);.  
25c5c 69 66 28 20 70 53 65 6c 65 63 74 52 6f 77 69 64  if( pSelectRowid
25c5d 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d   == 0 ) goto lim
25c5e 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70  it_where_cleanup
25c5f 5f 32 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 73  _2;.  pEList = s
25c60 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
25c61 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20  pend(pParse, 0, 
25c62 70 53 65 6c 65 63 74 52 6f 77 69 64 29 3b 0a 20  pSelectRowid);. 
25c63 20 69 66 28 20 70 45 4c 69 73 74 20 3d 3d 20 30   if( pEList == 0
25c64 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68   ) goto limit_wh
25c65 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 0a  ere_cleanup_2;..
25c66 20 20 2f 2a 20 64 75 70 6c 69 63 61 74 65 20 74    /* duplicate t
25c67 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61  he FROM clause a
25c68 73 20 69 74 20 69 73 20 6e 65 65 64 65 64 20 62  s it is needed b
25c69 79 20 62 6f 74 68 20 74 68 65 20 44 45 4c 45 54  y both the DELET
25c6a 45 2f 55 50 44 41 54 45 20 74 72 65 65 0a 20 20  E/UPDATE tree.  
25c6b 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c 45 43  ** and the SELEC
25c6c 54 20 73 75 62 74 72 65 65 2e 20 2a 2f 0a 20 20  T subtree. */.  
25c6d 70 53 65 6c 65 63 74 53 72 63 20 3d 20 73 71 6c  pSelectSrc = sql
25c6e 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 70  ite3SrcListDup(p
25c6f 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 2c  Parse->db, pSrc,
25c70 20 30 29 3b 0a 20 20 69 66 28 20 70 53 65 6c 65   0);.  if( pSele
25c71 63 74 53 72 63 20 3d 3d 20 30 20 29 20 7b 0a 20  ctSrc == 0 ) {. 
25c72 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
25c73 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  stDelete(pParse-
25c74 3e 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  >db, pEList);.  
25c75 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65    goto limit_whe
25c76 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20  re_cleanup_2;.  
25c77 7d 0a 0a 20 20 2f 2a 20 67 65 6e 65 72 61 74 65  }..  /* generate
25c78 20 74 68 65 20 53 45 4c 45 43 54 20 65 78 70 72   the SELECT expr
25c79 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 2a 2f 0a  ession tree. */.
25c7a 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69    pSelect = sqli
25c7b 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61  te3SelectNew(pPa
25c7c 72 73 65 2c 70 45 4c 69 73 74 2c 70 53 65 6c 65  rse,pEList,pSele
25c7d 63 74 53 72 63 2c 70 57 68 65 72 65 2c 30 2c 30  ctSrc,pWhere,0,0
25c7e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
25c7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
25c80 4f 72 64 65 72 42 79 2c 30 2c 70 4c 69 6d 69 74  OrderBy,0,pLimit
25c81 2c 70 4f 66 66 73 65 74 29 3b 0a 20 20 69 66 28  ,pOffset);.  if(
25c82 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 20 29 20   pSelect == 0 ) 
25c83 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20  return 0;..  /* 
25c84 6e 6f 77 20 67 65 6e 65 72 61 74 65 20 74 68 65  now generate the
25c85 20 6e 65 77 20 57 48 45 52 45 20 72 6f 77 69 64   new WHERE rowid
25c86 20 49 4e 20 63 6c 61 75 73 65 20 66 6f 72 20 74   IN clause for t
25c87 68 65 20 44 45 4c 45 54 45 2f 55 44 50 41 54 45  he DELETE/UDPATE
25c88 20 2a 2f 0a 20 20 70 57 68 65 72 65 52 6f 77 69   */.  pWhereRowi
25c89 64 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  d = sqlite3PExpr
25c8a 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 4f 57 2c  (pParse, TK_ROW,
25c8b 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28   0, 0, 0);.  if(
25c8c 20 70 57 68 65 72 65 52 6f 77 69 64 20 3d 3d 20   pWhereRowid == 
25c8d 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77  0 ) goto limit_w
25c8e 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31 3b 0a  here_cleanup_1;.
25c8f 20 20 70 49 6e 43 6c 61 75 73 65 20 3d 20 73 71    pInClause = sq
25c90 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
25c91 65 2c 20 54 4b 5f 49 4e 2c 20 70 57 68 65 72 65  e, TK_IN, pWhere
25c92 52 6f 77 69 64 2c 20 30 2c 20 30 29 3b 0a 20 20  Rowid, 0, 0);.  
25c93 69 66 28 20 70 49 6e 43 6c 61 75 73 65 20 3d 3d  if( pInClause ==
25c94 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f   0 ) goto limit_
25c95 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31 3b  where_cleanup_1;
25c96 0a 0a 20 20 70 49 6e 43 6c 61 75 73 65 2d 3e 78  ..  pInClause->x
25c97 2e 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65  .pSelect = pSele
25c98 63 74 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65 2d  ct;.  pInClause-
25c99 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 78 49 73  >flags |= EP_xIs
25c9a 53 65 6c 65 63 74 3b 0a 20 20 73 71 6c 69 74 65  Select;.  sqlite
25c9b 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70  3ExprSetHeight(p
25c9c 50 61 72 73 65 2c 20 70 49 6e 43 6c 61 75 73 65  Parse, pInClause
25c9d 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 49 6e 43  );.  return pInC
25c9e 6c 61 75 73 65 3b 0a 0a 20 20 2f 2a 20 73 6f 6d  lause;..  /* som
25c9f 65 74 68 69 6e 67 20 77 65 6e 74 20 77 72 6f 6e  ething went wron
25ca0 67 2e 20 63 6c 65 61 6e 20 75 70 20 61 6e 79 74  g. clean up anyt
25ca1 68 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64 2e 20  hing allocated. 
25ca2 2a 2f 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63  */.limit_where_c
25ca3 6c 65 61 6e 75 70 5f 31 3a 0a 20 20 73 71 6c 69  leanup_1:.  sqli
25ca4 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
25ca5 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 65 6c  pParse->db, pSel
25ca6 65 63 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  ect);.  return 0
25ca7 3b 0a 0a 6c 69 6d 69 74 5f 77 68 65 72 65 5f 63  ;..limit_where_c
25ca8 6c 65 61 6e 75 70 5f 32 3a 0a 20 20 73 71 6c 69  leanup_2:.  sqli
25ca9 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70 50  te3ExprDelete(pP
25caa 61 72 73 65 2d 3e 64 62 2c 20 70 57 68 65 72 65  arse->db, pWhere
25cab 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
25cac 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
25cad 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72 42 79 29  e->db, pOrderBy)
25cae 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ;.  sqlite3ExprD
25caf 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
25cb0 2c 20 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71 6c  , pLimit);.  sql
25cb1 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
25cb2 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 66 66 73  Parse->db, pOffs
25cb3 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  et);.  return 0;
25cb4 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66  .}.#endif /* def
25cb5 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
25cb6 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45  LE_UPDATE_DELETE
25cb7 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69  _LIMIT) && !defi
25cb8 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
25cb9 53 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a 2f 2a  SUBQUERY) */../*
25cba 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
25cbb 65 20 66 6f 72 20 61 20 44 45 4c 45 54 45 20 46  e for a DELETE F
25cbc 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ROM statement..*
25cbd 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20  *.**     DELETE 
25cbe 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20  FROM table_wxyz 
25cbf 57 48 45 52 45 20 61 3c 35 20 41 4e 44 20 62 20  WHERE a<5 AND b 
25cc0 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20  NOT NULL;.**    
25cc1 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f               \__
25cc2 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f  ______/       \_
25cc3 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f  _______________/
25cc4 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
25cc5 20 20 20 20 20 70 54 61 62 4c 69 73 74 20 20 20       pTabList   
25cc6 20 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72             pWher
25cc7 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e.*/.SQLITE_PRIV
25cc8 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
25cc9 44 65 6c 65 74 65 46 72 6f 6d 28 0a 20 20 50 61  DeleteFrom(.  Pa
25cca 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
25ccb 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
25ccc 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
25ccd 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
25cce 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61  t,     /* The ta
25ccf 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 77  ble from which w
25cd0 65 20 73 68 6f 75 6c 64 20 64 65 6c 65 74 65 20  e should delete 
25cd1 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78 70 72  things */.  Expr
25cd2 20 2a 70 57 68 65 72 65 20 20 20 20 20 20 20 20   *pWhere        
25cd3 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
25cd4 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20  clause.  May be 
25cd5 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 56 64 62  null */.){.  Vdb
25cd6 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
25cd7 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75      /* The virtu
25cd8 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69  al database engi
25cd9 6e 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  ne */.  Table *p
25cda 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Tab;           /
25cdb 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72 6f 6d  * The table from
25cdc 20 77 68 69 63 68 20 72 65 63 6f 72 64 73 20 77   which records w
25cdd 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 20 2a  ill be deleted *
25cde 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
25cdf 7a 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61  zDb;       /* Na
25ce0 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 68  me of database h
25ce1 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20  olding pTab */. 
25ce2 20 69 6e 74 20 65 6e 64 2c 20 61 64 64 72 20 3d   int end, addr =
25ce3 20 30 3b 20 20 20 20 20 2f 2a 20 41 20 63 6f 75   0;     /* A cou
25ce4 70 6c 65 20 61 64 64 72 65 73 73 65 73 20 6f 66  ple addresses of
25ce5 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20   generated code 
25ce6 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
25ce7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
25ce8 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
25ce9 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e   WhereInfo *pWIn
25cea 66 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72  fo;     /* Infor
25ceb 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
25cec 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
25ced 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
25cee 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
25cef 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69 6e   looping over in
25cf0 64 69 63 65 73 20 6f 66 20 74 68 65 20 74 61 62  dices of the tab
25cf1 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  le */.  int iCur
25cf2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
25cf3 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75  * VDBE Cursor nu
25cf4 6d 62 65 72 20 66 6f 72 20 70 54 61 62 20 2a 2f  mber for pTab */
25cf5 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
25cf6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 69            /* Mai
25cf7 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63  n database struc
25cf8 74 75 72 65 20 2a 2f 0a 20 20 41 75 74 68 43 6f  ture */.  AuthCo
25cf9 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b 20  ntext sContext; 
25cfa 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f   /* Authorizatio
25cfb 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 4e  n context */.  N
25cfc 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20  ameContext sNC; 
25cfd 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f        /* Name co
25cfe 6e 74 65 78 74 20 74 6f 20 72 65 73 6f 6c 76 65  ntext to resolve
25cff 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
25d00 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
25d01 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
25d02 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72 20 2a  atabase number *
25d03 2f 0a 20 20 69 6e 74 20 6d 65 6d 43 6e 74 20 3d  /.  int memCnt =
25d04 20 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 4d 65   -1;       /* Me
25d05 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 66  mory cell used f
25d06 6f 72 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 69  or change counti
25d07 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 72 63 61 75  ng */.  int rcau
25d08 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  th;            /
25d09 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e 65 64  * Value returned
25d0a 20 62 79 20 61 75 74 68 6f 72 69 7a 61 74 69 6f   by authorizatio
25d0b 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 0a 23  n callback */..#
25d0c 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
25d0d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74  IT_TRIGGER.  int
25d0e 20 69 73 56 69 65 77 3b 20 20 20 20 20 20 20 20   isView;        
25d0f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
25d10 65 20 69 66 20 61 74 74 65 6d 70 74 69 6e 67 20  e if attempting 
25d11 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 61  to delete from a
25d12 20 76 69 65 77 20 2a 2f 0a 20 20 54 72 69 67 67   view */.  Trigg
25d13 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20  er *pTrigger;   
25d14 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20          /* List 
25d15 6f 66 20 74 61 62 6c 65 20 74 72 69 67 67 65 72  of table trigger
25d16 73 2c 20 69 66 20 72 65 71 75 69 72 65 64 20 2a  s, if required *
25d17 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 6d 65 6d 73  /.#endif..  mems
25d18 65 74 28 26 73 43 6f 6e 74 65 78 74 2c 20 30 2c  et(&sContext, 0,
25d19 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74 65 78 74   sizeof(sContext
25d1a 29 29 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73  ));.  db = pPars
25d1b 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61  e->db;.  if( pPa
25d1c 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
25d1d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
25d1e 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
25d1f 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
25d20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61   }.  assert( pTa
25d21 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29  bList->nSrc==1 )
25d22 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  ;..  /* Locate t
25d23 68 65 20 74 61 62 6c 65 20 77 68 69 63 68 20 77  he table which w
25d24 65 20 77 61 6e 74 20 74 6f 20 64 65 6c 65 74 65  e want to delete
25d25 2e 20 20 54 68 69 73 20 74 61 62 6c 65 20 68 61  .  This table ha
25d26 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 75 74  s to be.  ** put
25d27 20 69 6e 20 61 6e 20 53 72 63 4c 69 73 74 20 73   in an SrcList s
25d28 74 72 75 63 74 75 72 65 20 62 65 63 61 75 73 65  tructure because
25d29 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 73 75 62   some of the sub
25d2a 72 6f 75 74 69 6e 65 73 20 77 65 0a 20 20 2a 2a  routines we.  **
25d2b 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 69 6e 67   will be calling
25d2c 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f   are designed to
25d2d 20 77 6f 72 6b 20 77 69 74 68 20 6d 75 6c 74 69   work with multi
25d2e 70 6c 65 20 74 61 62 6c 65 73 20 61 6e 64 20 65  ple tables and e
25d2f 78 70 65 63 74 0a 20 20 2a 2a 20 61 6e 20 53 72  xpect.  ** an Sr
25d30 63 4c 69 73 74 2a 20 70 61 72 61 6d 65 74 65 72  cList* parameter
25d31 20 69 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74   instead of just
25d32 20 61 20 54 61 62 6c 65 2a 20 70 61 72 61 6d 65   a Table* parame
25d33 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62  ter..  */.  pTab
25d34 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
25d35 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20  tLookup(pParse, 
25d36 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28  pTabList);.  if(
25d37 20 70 54 61 62 3d 3d 30 20 29 20 20 67 6f 74 6f   pTab==0 )  goto
25d38 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
25d39 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46 69 67 75  anup;..  /* Figu
25d3a 72 65 20 6f 75 74 20 69 66 20 77 65 20 68 61 76  re out if we hav
25d3b 65 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 61  e any triggers a
25d3c 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  nd if the table 
25d3d 62 65 69 6e 67 0a 20 20 2a 2a 20 64 65 6c 65 74  being.  ** delet
25d3e 65 64 20 66 72 6f 6d 20 69 73 20 61 20 76 69 65  ed from is a vie
25d3f 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  w.  */.#ifndef S
25d40 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
25d41 45 52 0a 20 20 70 54 72 69 67 67 65 72 20 3d 20  ER.  pTrigger = 
25d42 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45  sqlite3TriggersE
25d43 78 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  xist(pParse, pTa
25d44 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c  b, TK_DELETE, 0,
25d45 20 30 29 3b 0a 20 20 69 73 56 69 65 77 20 3d 20   0);.  isView = 
25d46 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30  pTab->pSelect!=0
25d47 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ;.#else.# define
25d48 20 70 54 72 69 67 67 65 72 20 30 0a 23 20 64 65   pTrigger 0.# de
25d49 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65  fine isView 0.#e
25d4a 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
25d4b 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75  TE_OMIT_VIEW.# u
25d4c 6e 64 65 66 20 69 73 56 69 65 77 0a 23 20 64 65  ndef isView.# de
25d4d 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65  fine isView 0.#e
25d4e 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 54  ndif..  /* If pT
25d4f 61 62 20 69 73 20 72 65 61 6c 6c 79 20 61 20 76  ab is really a v
25d50 69 65 77 2c 20 6d 61 6b 65 20 73 75 72 65 20 69  iew, make sure i
25d51 74 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74 69  t has been initi
25d52 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  alized..  */.  i
25d53 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65  f( sqlite3ViewGe
25d54 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  tColumnNames(pPa
25d55 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20  rse, pTab) ){.  
25d56 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72    goto delete_fr
25d57 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  om_cleanup;.  }.
25d58 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73  .  if( sqlite3Is
25d59 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c  ReadOnly(pParse,
25d5a 20 70 54 61 62 2c 20 28 70 54 72 69 67 67 65 72   pTab, (pTrigger
25d5b 3f 31 3a 30 29 29 20 29 7b 0a 20 20 20 20 67 6f  ?1:0)) ){.    go
25d5c 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63  to delete_from_c
25d5d 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 44  leanup;.  }.  iD
25d5e 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
25d5f 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
25d60 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61  b->pSchema);.  a
25d61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e  ssert( iDb<db->n
25d62 44 62 20 29 3b 0a 20 20 7a 44 62 20 3d 20 64 62  Db );.  zDb = db
25d63 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
25d64 3b 0a 20 20 72 63 61 75 74 68 20 3d 20 73 71 6c  ;.  rcauth = sql
25d65 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
25d66 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c  arse, SQLITE_DEL
25d67 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ETE, pTab->zName
25d68 2c 20 30 2c 20 7a 44 62 29 3b 0a 20 20 61 73 73  , 0, zDb);.  ass
25d69 65 72 74 28 20 72 63 61 75 74 68 3d 3d 53 51 4c  ert( rcauth==SQL
25d6a 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 61 75 74 68  ITE_OK || rcauth
25d6b 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 7c 7c  ==SQLITE_DENY ||
25d6c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f   rcauth==SQLITE_
25d6d 49 47 4e 4f 52 45 20 29 3b 0a 20 20 69 66 28 20  IGNORE );.  if( 
25d6e 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44  rcauth==SQLITE_D
25d6f 45 4e 59 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ENY ){.    goto 
25d70 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61  delete_from_clea
25d71 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  nup;.  }.  asser
25d72 74 28 21 69 73 56 69 65 77 20 7c 7c 20 70 54 72  t(!isView || pTr
25d73 69 67 67 65 72 29 3b 0a 0a 20 20 2f 2a 20 41 73  igger);..  /* As
25d74 73 69 67 6e 20 20 63 75 72 73 6f 72 20 6e 75 6d  sign  cursor num
25d75 62 65 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ber to the table
25d76 20 61 6e 64 20 61 6c 6c 20 69 74 73 20 69 6e 64   and all its ind
25d77 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  ices..  */.  ass
25d78 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e  ert( pTabList->n
25d79 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69 43 75 72  Src==1 );.  iCur
25d7a 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30   = pTabList->a[0
25d7b 5d 2e 69 43 75 72 73 6f 72 20 3d 20 70 50 61 72  ].iCursor = pPar
25d7c 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f  se->nTab++;.  fo
25d7d 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
25d7e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
25d7f 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
25d80 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b    pParse->nTab++
25d81 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 61 72  ;.  }..  /* Star
25d82 74 20 74 68 65 20 76 69 65 77 20 63 6f 6e 74 65  t the view conte
25d83 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  xt.  */.  if( is
25d84 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71 6c 69  View ){.    sqli
25d85 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75  te3AuthContextPu
25d86 73 68 28 70 50 61 72 73 65 2c 20 26 73 43 6f 6e  sh(pParse, &sCon
25d87 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  text, pTab->zNam
25d88 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65  e);.  }..  /* Be
25d89 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63  gin generating c
25d8a 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  ode..  */.  v = 
25d8b 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
25d8c 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d  Parse);.  if( v=
25d8d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64  =0 ){.    goto d
25d8e 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e  elete_from_clean
25d8f 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  up;.  }.  if( pP
25d90 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20  arse->nested==0 
25d91 29 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75  ) sqlite3VdbeCou
25d92 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20 20  ntChanges(v);.  
25d93 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
25d94 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
25d95 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 20 20 2f  e, 1, iDb);..  /
25d96 2a 20 49 66 20 77 65 20 61 72 65 20 74 72 79 69  * If we are tryi
25d97 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f  ng to delete fro
25d98 6d 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a  m a view, realiz
25d99 65 20 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f  e that view into
25d9a 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61  .  ** a ephemera
25d9b 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69  l table..  */.#i
25d9c 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
25d9d 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20  E_OMIT_VIEW) && 
25d9e 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
25d9f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20  OMIT_TRIGGER).  
25da0 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20  if( isView ){.  
25da1 20 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61    sqlite3Materia
25da2 6c 69 7a 65 56 69 65 77 28 70 50 61 72 73 65 2c  lizeView(pParse,
25da3 20 70 54 61 62 2c 20 70 57 68 65 72 65 2c 20 69   pTab, pWhere, i
25da4 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Cur);.  }.#endif
25da5 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74  ..  /* Resolve t
25da6 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
25da7 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  in the WHERE cla
25da8 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73  use..  */.  mems
25da9 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65  et(&sNC, 0, size
25daa 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e  of(sNC));.  sNC.
25dab 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
25dac 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20  .  sNC.pSrcList 
25dad 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 69 66  = pTabList;.  if
25dae 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
25daf 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
25db0 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20 67  pWhere) ){.    g
25db1 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f  oto delete_from_
25db2 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
25db3 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
25db4 65 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68 65  e counter of the
25db5 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
25db6 64 65 6c 65 74 65 64 2c 20 69 66 0a 20 20 2a 2a  deleted, if.  **
25db7 20 77 65 20 61 72 65 20 63 6f 75 6e 74 69 6e 67   we are counting
25db8 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20 69 66   rows..  */.  if
25db9 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ( db->flags & SQ
25dba 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29  LITE_CountRows )
25dbb 7b 0a 20 20 20 20 6d 65 6d 43 6e 74 20 3d 20 2b  {.    memCnt = +
25dbc 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
25dbd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25dbe 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
25dbf 65 72 2c 20 30 2c 20 6d 65 6d 43 6e 74 29 3b 0a  er, 0, memCnt);.
25dc0 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
25dc1 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54  ITE_OMIT_TRUNCAT
25dc2 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20  E_OPTIMIZATION. 
25dc3 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65   /* Special case
25dc4 3a 20 41 20 44 45 4c 45 54 45 20 77 69 74 68 6f  : A DELETE witho
25dc5 75 74 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  ut a WHERE claus
25dc6 65 20 64 65 6c 65 74 65 73 20 65 76 65 72 79 74  e deletes everyt
25dc7 68 69 6e 67 2e 0a 20 20 2a 2a 20 49 74 20 69 73  hing..  ** It is
25dc8 20 65 61 73 69 65 72 20 6a 75 73 74 20 74 6f 20   easier just to 
25dc9 65 72 61 73 65 20 74 68 65 20 77 68 6f 6c 65 20  erase the whole 
25dca 74 61 62 6c 65 2e 20 50 72 69 6f 72 20 74 6f 20  table. Prior to 
25dcb 76 65 72 73 69 6f 6e 20 33 2e 36 2e 35 2c 0a 20  version 3.6.5,. 
25dcc 20 2a 2a 20 74 68 69 73 20 6f 70 74 69 6d 69 7a   ** this optimiz
25dcd 61 74 69 6f 6e 20 63 61 75 73 65 64 20 74 68 65  ation caused the
25dce 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f 75 6e   row change coun
25dcf 74 20 28 74 68 65 20 76 61 6c 75 65 20 72 65 74  t (the value ret
25dd0 75 72 6e 65 64 20 62 79 20 0a 20 20 2a 2a 20 41  urned by .  ** A
25dd1 50 49 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69  PI function sqli
25dd2 74 65 33 5f 63 6f 75 6e 74 5f 63 68 61 6e 67 65  te3_count_change
25dd3 73 29 20 74 6f 20 62 65 20 73 65 74 20 69 6e 63  s) to be set inc
25dd4 6f 72 72 65 63 74 6c 79 2e 20 20 2a 2f 0a 20 20  orrectly.  */.  
25dd5 69 66 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49  if( rcauth==SQLI
25dd6 54 45 5f 4f 4b 20 26 26 20 70 57 68 65 72 65 3d  TE_OK && pWhere=
25dd7 3d 30 20 26 26 20 21 70 54 72 69 67 67 65 72 20  =0 && !pTrigger 
25dd8 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54  && !IsVirtual(pT
25dd9 61 62 29 20 0a 20 20 20 26 26 20 30 3d 3d 73 71  ab) .   && 0==sq
25dda 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28  lite3FkRequired(
25ddb 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c  pParse, pTab, 0,
25ddc 20 30 29 0a 20 20 29 7b 0a 20 20 20 20 61 73 73   0).  ){.    ass
25ddd 65 72 74 28 20 21 69 73 56 69 65 77 20 29 3b 0a  ert( !isView );.
25dde 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25ddf 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6c 65 61  ddOp4(v, OP_Clea
25de0 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69  r, pTab->tnum, i
25de1 44 62 2c 20 6d 65 6d 43 6e 74 2c 0a 20 20 20 20  Db, memCnt,.    
25de2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25de3 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50    pTab->zName, P
25de4 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66  4_STATIC);.    f
25de5 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
25de6 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
25de7 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
25de8 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64       assert( pId
25de9 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62  x->pSchema==pTab
25dea 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20  ->pSchema );.   
25deb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25dec 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72  dOp2(v, OP_Clear
25ded 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44  , pIdx->tnum, iD
25dee 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  b);.    }.  }els
25def 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
25df0 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41 54 45  TE_OMIT_TRUNCATE
25df1 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f  _OPTIMIZATION */
25df2 0a 20 20 2f 2a 20 54 68 65 20 75 73 75 61 6c 20  .  /* The usual 
25df3 63 61 73 65 3a 20 54 68 65 72 65 20 69 73 20 61  case: There is a
25df4 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 73 6f   WHERE clause so
25df5 20 77 65 20 68 61 76 65 20 74 6f 20 73 63 61 6e   we have to scan
25df6 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20 74 68   through.  ** th
25df7 65 20 74 61 62 6c 65 20 61 6e 64 20 70 69 63 6b  e table and pick
25df8 20 77 68 69 63 68 20 72 65 63 6f 72 64 73 20 74   which records t
25df9 6f 20 64 65 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20  o delete..  */. 
25dfa 20 7b 0a 20 20 20 20 69 6e 74 20 69 52 6f 77 53   {.    int iRowS
25dfb 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  et = ++pParse->n
25dfc 4d 65 6d 3b 20 20 20 2f 2a 20 52 65 67 69 73 74  Mem;   /* Regist
25dfd 65 72 20 66 6f 72 20 72 6f 77 73 65 74 20 6f 66  er for rowset of
25dfe 20 72 6f 77 73 20 74 6f 20 64 65 6c 65 74 65 20   rows to delete 
25dff 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52 6f 77 69  */.    int iRowi
25e00 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  d = ++pParse->nM
25e01 65 6d 3b 20 20 20 20 2f 2a 20 55 73 65 64 20 66  em;    /* Used f
25e02 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f 77 69 64  or storing rowid
25e03 20 76 61 6c 75 65 73 2e 20 2a 2f 0a 20 20 20 20   values. */.    
25e04 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20  int regRowid;   
25e05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25e06 2f 2a 20 41 63 74 75 61 6c 20 72 65 67 69 73 74  /* Actual regist
25e07 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f  er containing ro
25e08 77 69 64 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  wids */..    /* 
25e09 43 6f 6c 6c 65 63 74 20 72 6f 77 69 64 73 20 6f  Collect rowids o
25e0a 66 20 65 76 65 72 79 20 72 6f 77 20 74 6f 20 62  f every row to b
25e0b 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a  e deleted..    *
25e0c 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
25e0d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
25e0e 6c 6c 2c 20 30 2c 20 69 52 6f 77 53 65 74 29 3b  ll, 0, iRowSet);
25e0f 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71  .    pWInfo = sq
25e10 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
25e11 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
25e12 2c 20 70 57 68 65 72 65 2c 30 2c 57 48 45 52 45  , pWhere,0,WHERE
25e13 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3b  _DUPLICATES_OK);
25e14 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d  .    if( pWInfo=
25e15 3d 30 20 29 20 67 6f 74 6f 20 64 65 6c 65 74 65  =0 ) goto delete
25e16 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
25e17 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71     regRowid = sq
25e18 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74  lite3ExprCodeGet
25e19 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70  Column(pParse, p
25e1a 54 61 62 2c 20 2d 31 2c 20 69 43 75 72 2c 20 69  Tab, -1, iCur, i
25e1b 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 73  Rowid, 0);.    s
25e1c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
25e1d 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 41 64 64  (v, OP_RowSetAdd
25e1e 2c 20 69 52 6f 77 53 65 74 2c 20 72 65 67 52 6f  , iRowSet, regRo
25e1f 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20 64 62  wid);.    if( db
25e20 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
25e21 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20  _CountRows ){.  
25e22 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25e23 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
25e24 6d 6d 2c 20 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a  mm, memCnt, 1);.
25e25 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
25e26 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
25e27 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74  );..    /* Delet
25e28 65 20 65 76 65 72 79 20 69 74 65 6d 20 77 68 6f  e every item who
25e29 73 65 20 6b 65 79 20 77 61 73 20 77 72 69 74 74  se key was writt
25e2a 65 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20 64  en to the list d
25e2b 75 72 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a  uring the.    **
25e2c 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 2e 20   database scan. 
25e2d 20 57 65 20 68 61 76 65 20 74 6f 20 64 65 6c 65   We have to dele
25e2e 74 65 20 69 74 65 6d 73 20 61 66 74 65 72 20 74  te items after t
25e2f 68 65 20 73 63 61 6e 20 69 73 20 63 6f 6d 70 6c  he scan is compl
25e30 65 74 65 0a 20 20 20 20 2a 2a 20 62 65 63 61 75  ete.    ** becau
25e31 73 65 20 64 65 6c 65 74 69 6e 67 20 61 6e 20 69  se deleting an i
25e32 74 65 6d 20 63 61 6e 20 63 68 61 6e 67 65 20 74  tem can change t
25e33 68 65 20 73 63 61 6e 20 6f 72 64 65 72 2e 20 20  he scan order.  
25e34 2a 2f 0a 20 20 20 20 65 6e 64 20 3d 20 73 71 6c  */.    end = sql
25e35 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
25e36 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 6e  l(v);..    /* Un
25e37 6c 65 73 73 20 74 68 69 73 20 69 73 20 61 20 76  less this is a v
25e38 69 65 77 2c 20 6f 70 65 6e 20 63 75 72 73 6f 72  iew, open cursor
25e39 73 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  s for the table 
25e3a 77 65 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 64  we are .    ** d
25e3b 65 6c 65 74 69 6e 67 20 66 72 6f 6d 20 61 6e 64  eleting from and
25e3c 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73   all its indices
25e3d 2e 20 49 66 20 74 68 69 73 20 69 73 20 61 20 76  . If this is a v
25e3e 69 65 77 2c 20 74 68 65 6e 20 74 68 65 0a 20 20  iew, then the.  
25e3f 20 20 2a 2a 20 6f 6e 6c 79 20 65 66 66 65 63 74    ** only effect
25e40 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   this statement 
25e41 68 61 73 20 69 73 20 74 6f 20 66 69 72 65 20 74  has is to fire t
25e42 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20 0a 20  he INSTEAD OF . 
25e43 20 20 20 2a 2a 20 74 72 69 67 67 65 72 73 2e 20     ** triggers. 
25e44 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56   */.    if( !isV
25e45 69 65 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  iew ){.      sql
25e46 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64  ite3OpenTableAnd
25e47 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20  Indices(pParse, 
25e48 70 54 61 62 2c 20 69 43 75 72 2c 20 4f 50 5f 4f  pTab, iCur, OP_O
25e49 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 7d  penWrite);.    }
25e4a 0a 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  ..    addr = sql
25e4b 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
25e4c 2c 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 2c  , OP_RowSetRead,
25e4d 20 69 52 6f 77 53 65 74 2c 20 65 6e 64 2c 20 69   iRowSet, end, i
25e4e 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20  Rowid);..    /* 
25e4f 44 65 6c 65 74 65 20 74 68 65 20 72 6f 77 20 2a  Delete the row *
25e50 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
25e51 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
25e52 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56 69 72  LE.    if( IsVir
25e53 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
25e54 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
25e55 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63  pVTab = (const c
25e56 68 61 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74  har *)sqlite3Get
25e57 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29  VTable(db, pTab)
25e58 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25e59 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28  tabMakeWritable(
25e5a 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20  pParse, pTab);. 
25e5b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25e5c 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70  AddOp4(v, OP_VUp
25e5d 64 61 74 65 2c 20 30 2c 20 31 2c 20 69 52 6f 77  date, 0, 1, iRow
25e5e 69 64 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54  id, pVTab, P4_VT
25e5f 41 42 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  AB);.      sqlit
25e60 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73  e3MayAbort(pPars
25e61 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65  e);.    }else.#e
25e62 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20  ndif.    {.     
25e63 20 69 6e 74 20 63 6f 75 6e 74 20 3d 20 28 70 50   int count = (pP
25e64 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 29  arse->nested==0)
25e65 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20  ;    /* True to 
25e66 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 2a 2f  count changes */
25e67 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65  .      sqlite3Ge
25e68 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28  nerateRowDelete(
25e69 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43  pParse, pTab, iC
25e6a 75 72 2c 20 69 52 6f 77 69 64 2c 20 63 6f 75 6e  ur, iRowid, coun
25e6b 74 2c 20 70 54 72 69 67 67 65 72 2c 20 4f 45 5f  t, pTrigger, OE_
25e6c 44 65 66 61 75 6c 74 29 3b 0a 20 20 20 20 7d 0a  Default);.    }.
25e6d 0a 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74  .    /* End of t
25e6e 68 65 20 64 65 6c 65 74 65 20 6c 6f 6f 70 20 2a  he delete loop *
25e6f 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
25e70 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
25e71 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a 20 20  to, 0, addr);.  
25e72 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
25e73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64  olveLabel(v, end
25e74 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65  );..    /* Close
25e75 20 74 68 65 20 63 75 72 73 6f 72 73 20 6f 70 65   the cursors ope
25e76 6e 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 61  n on the table a
25e77 6e 64 20 69 74 73 20 69 6e 64 65 78 65 73 2e 20  nd its indexes. 
25e78 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56 69  */.    if( !isVi
25e79 65 77 20 26 26 20 21 49 73 56 69 72 74 75 61 6c  ew && !IsVirtual
25e7a 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
25e7b 66 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d 70 54  for(i=1, pIdx=pT
25e7c 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
25e7d 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49 64 78  ; i++, pIdx=pIdx
25e7e 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
25e7f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25e80 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
25e81 20 69 43 75 72 20 2b 20 69 2c 20 70 49 64 78 2d   iCur + i, pIdx-
25e82 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20 7d 0a  >tnum);.      }.
25e83 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25e84 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
25e85 6f 73 65 2c 20 69 43 75 72 29 3b 0a 20 20 20 20  ose, iCur);.    
25e86 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61  }.  }..  /* Upda
25e87 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65  te the sqlite_se
25e88 71 75 65 6e 63 65 20 74 61 62 6c 65 20 62 79 20  quence table by 
25e89 73 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 6e 74  storing the cont
25e8a 65 6e 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  ent of the.  ** 
25e8b 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 63 6f  maximum rowid co
25e8c 75 6e 74 65 72 20 76 61 6c 75 65 73 20 72 65 63  unter values rec
25e8d 6f 72 64 65 64 20 77 68 69 6c 65 20 69 6e 73 65  orded while inse
25e8e 72 74 69 6e 67 20 69 6e 74 6f 0a 20 20 2a 2a 20  rting into.  ** 
25e8f 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74 61  autoincrement ta
25e90 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  bles..  */.  if(
25e91 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
25e92 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54  =0 && pParse->pT
25e93 72 69 67 67 65 72 54 61 62 3d 3d 30 20 29 7b 0a  riggerTab==0 ){.
25e94 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 6f 69      sqlite3Autoi
25e95 6e 63 72 65 6d 65 6e 74 45 6e 64 28 70 50 61 72  ncrementEnd(pPar
25e96 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  se);.  }..  /* R
25e97 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
25e98 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 65   of rows that we
25e99 72 65 20 64 65 6c 65 74 65 64 2e 20 49 66 20 74  re deleted. If t
25e9a 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 0a  his routine is .
25e9b 20 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67 20    ** generating 
25e9c 63 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66 20  code because of 
25e9d 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
25e9e 33 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c 20  3NestedParse(), 
25e9f 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76 6f  do not.  ** invo
25ea0 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  ke the callback 
25ea1 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  function..  */. 
25ea2 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26   if( (db->flags&
25ea3 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
25ea4 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e 6e 65  ) && !pParse->ne
25ea5 73 74 65 64 20 26 26 20 21 70 50 61 72 73 65 2d  sted && !pParse-
25ea6 3e 70 54 72 69 67 67 65 72 54 61 62 20 29 7b 0a  >pTriggerTab ){.
25ea7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25ea8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
25ea9 6c 74 52 6f 77 2c 20 6d 65 6d 43 6e 74 2c 20 31  ltRow, memCnt, 1
25eaa 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
25eab 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
25eac 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
25ead 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
25eae 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
25eaf 2c 20 22 72 6f 77 73 20 64 65 6c 65 74 65 64 22  , "rows deleted"
25eb0 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
25eb1 3b 0a 20 20 7d 0a 0a 64 65 6c 65 74 65 5f 66 72  ;.  }..delete_fr
25eb2 6f 6d 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71  om_cleanup:.  sq
25eb3 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
25eb4 50 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b 0a  Pop(&sContext);.
25eb5 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
25eb6 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 4c  Delete(db, pTabL
25eb7 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  ist);.  sqlite3E
25eb8 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57  xprDelete(db, pW
25eb9 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b  here);.  return;
25eba 0a 7d 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  .}./* Make sure 
25ebb 22 69 73 56 69 65 77 22 20 61 6e 64 20 6f 74 68  "isView" and oth
25ebc 65 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65  er macros define
25ebd 64 20 61 62 6f 76 65 20 61 72 65 20 75 6e 64 65  d above are unde
25ebe 66 69 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  fined. Otherwise
25ebf 0a 2a 2a 20 74 68 65 6c 79 20 6d 61 79 20 69 6e  .** thely may in
25ec0 74 65 72 66 65 72 65 20 77 69 74 68 20 63 6f 6d  terfere with com
25ec1 70 69 6c 61 74 69 6f 6e 20 6f 66 20 6f 74 68 65  pilation of othe
25ec2 72 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74  r functions in t
25ec3 68 69 73 20 66 69 6c 65 0a 2a 2a 20 28 6f 72 20  his file.** (or 
25ec4 69 6e 20 61 6e 6f 74 68 65 72 20 66 69 6c 65 2c  in another file,
25ec5 20 69 66 20 74 68 69 73 20 66 69 6c 65 20 62 65   if this file be
25ec6 63 6f 6d 65 73 20 70 61 72 74 20 6f 66 20 74 68  comes part of th
25ec7 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 29 2e  e amalgamation).
25ec8 20 20 2a 2f 0a 23 69 66 64 65 66 20 69 73 56 69    */.#ifdef isVi
25ec9 65 77 0a 20 23 75 6e 64 65 66 20 69 73 56 69 65  ew. #undef isVie
25eca 77 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  w.#endif.#ifdef 
25ecb 70 54 72 69 67 67 65 72 0a 20 23 75 6e 64 65 66  pTrigger. #undef
25ecc 20 70 54 72 69 67 67 65 72 0a 23 65 6e 64 69 66   pTrigger.#endif
25ecd 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
25ece 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56  tine generates V
25ecf 44 42 45 20 63 6f 64 65 20 74 68 61 74 20 63 61  DBE code that ca
25ed0 75 73 65 73 20 61 20 73 69 6e 67 6c 65 20 72 6f  uses a single ro
25ed1 77 20 6f 66 20 61 0a 2a 2a 20 73 69 6e 67 6c 65  w of a.** single
25ed2 20 74 61 62 6c 65 20 74 6f 20 62 65 20 64 65 6c   table to be del
25ed3 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eted..**.** The 
25ed4 56 44 42 45 20 6d 75 73 74 20 62 65 20 69 6e 20  VDBE must be in 
25ed5 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 61  a particular sta
25ed6 74 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  te when this rou
25ed7 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
25ed8 2a 2a 20 54 68 65 73 65 20 61 72 65 20 74 68 65  ** These are the
25ed9 20 72 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   requirements:.*
25eda 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20 72 65 61  *.**   1.  A rea
25edb 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70  d/write cursor p
25edc 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54 61 62 2c  ointing to pTab,
25edd 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61   the table conta
25ede 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a  ining the row.**
25edf 20 20 20 20 20 20 20 74 6f 20 62 65 20 64 65 6c         to be del
25ee0 65 74 65 64 2c 20 6d 75 73 74 20 62 65 20 6f 70  eted, must be op
25ee1 65 6e 65 64 20 61 73 20 63 75 72 73 6f 72 20 6e  ened as cursor n
25ee2 75 6d 62 65 72 20 24 69 43 75 72 2e 0a 2a 2a 0a  umber $iCur..**.
25ee3 2a 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72  **   2.  Read/wr
25ee4 69 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20  ite cursors for 
25ee5 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70  all indices of p
25ee6 54 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e  Tab must be open
25ee7 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72   as.**       cur
25ee8 73 6f 72 20 6e 75 6d 62 65 72 20 62 61 73 65 2b  sor number base+
25ee9 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69  i for the i-th i
25eea 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e  ndex..**.**   3.
25eeb 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d    The record num
25eec 62 65 72 20 6f 66 20 74 68 65 20 72 6f 77 20 74  ber of the row t
25eed 6f 20 62 65 20 64 65 6c 65 74 65 64 20 6d 75 73  o be deleted mus
25eee 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e 0a 2a  t be stored in.*
25eef 2a 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 63  *       memory c
25ef0 65 6c 6c 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a  ell iRowid..**.*
25ef1 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67  * This routine g
25ef2 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f  enerates code to
25ef3 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74 68 65   remove both the
25ef4 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20 61 6e   table record an
25ef5 64 20 61 6c 6c 20 0a 2a 2a 20 69 6e 64 65 78 20  d all .** index 
25ef6 65 6e 74 72 69 65 73 20 74 68 61 74 20 70 6f 69  entries that poi
25ef7 6e 74 20 74 6f 20 74 68 61 74 20 72 65 63 6f 72  nt to that recor
25ef8 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
25ef9 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
25efa 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65  3GenerateRowDele
25efb 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  te(.  Parse *pPa
25efc 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
25efd 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
25efe 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
25eff 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e      /* Table con
25f00 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 20  taining the row 
25f01 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f  to be deleted */
25f02 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20  .  int iCur,    
25f03 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
25f04 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74  number for the t
25f05 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52  able */.  int iR
25f06 6f 77 69 64 2c 20 20 20 20 20 20 20 20 2f 2a 20  owid,        /* 
25f07 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74  Memory cell that
25f08 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f   contains the ro
25f09 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  wid to delete */
25f0a 0a 20 20 69 6e 74 20 63 6f 75 6e 74 2c 20 20 20  .  int count,   
25f0b 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d        /* If non-
25f0c 7a 65 72 6f 2c 20 69 6e 63 72 65 6d 65 6e 74 20  zero, increment 
25f0d 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63  the row change c
25f0e 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 54 72 69 67  ounter */.  Trig
25f0f 67 65 72 20 2a 70 54 72 69 67 67 65 72 2c 20 2f  ger *pTrigger, /
25f10 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65  * List of trigge
25f11 72 73 20 74 6f 20 28 70 6f 74 65 6e 74 69 61 6c  rs to (potential
25f12 6c 79 29 20 66 69 72 65 20 2a 2f 0a 20 20 69 6e  ly) fire */.  in
25f13 74 20 6f 6e 63 6f 6e 66 20 20 20 20 20 20 20 20  t onconf        
25f14 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20 43   /* Default ON C
25f15 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 66  ONFLICT policy f
25f16 6f 72 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 29  or triggers */.)
25f17 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
25f18 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20 20  arse->pVdbe;    
25f19 20 20 20 20 2f 2a 20 56 64 62 65 20 2a 2f 0a 20      /* Vdbe */. 
25f1a 20 69 6e 74 20 69 4f 6c 64 20 3d 20 30 3b 20 20   int iOld = 0;  
25f1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25f1c 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74   /* First regist
25f1d 65 72 20 69 6e 20 4f 4c 44 2e 2a 20 61 72 72 61  er in OLD.* arra
25f1e 79 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 61 62 65  y */.  int iLabe
25f1f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
25f20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20         /* Label 
25f21 72 65 73 6f 6c 76 65 64 20 74 6f 20 65 6e 64 20  resolved to end 
25f22 6f 66 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  of generated cod
25f23 65 20 2a 2f 0a 0a 20 20 2f 2a 20 56 64 62 65 20  e */..  /* Vdbe 
25f24 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  is guaranteed to
25f25 20 68 61 76 65 20 62 65 65 6e 20 61 6c 6c 6f 63   have been alloc
25f26 61 74 65 64 20 62 79 20 74 68 69 73 20 73 74 61  ated by this sta
25f27 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ge. */.  assert(
25f28 20 76 20 29 3b 0a 0a 20 20 2f 2a 20 53 65 65 6b   v );..  /* Seek
25f29 20 63 75 72 73 6f 72 20 69 43 75 72 20 74 6f 20   cursor iCur to 
25f2a 74 68 65 20 72 6f 77 20 74 6f 20 64 65 6c 65 74  the row to delet
25f2b 65 2e 20 49 66 20 74 68 69 73 20 72 6f 77 20 6e  e. If this row n
25f2c 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74 73 20  o longer exists 
25f2d 0a 20 20 2a 2a 20 28 74 68 69 73 20 63 61 6e 20  .  ** (this can 
25f2e 68 61 70 70 65 6e 20 69 66 20 61 20 74 72 69 67  happen if a trig
25f2f 67 65 72 20 70 72 6f 67 72 61 6d 20 68 61 73 20  ger program has 
25f30 61 6c 72 65 61 64 79 20 64 65 6c 65 74 65 64 20  already deleted 
25f31 69 74 29 2c 20 64 6f 0a 20 20 2a 2a 20 6e 6f 74  it), do.  ** not
25f32 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 6c 65   attempt to dele
25f33 74 65 20 69 74 20 6f 72 20 66 69 72 65 20 61 6e  te it or fire an
25f34 79 20 44 45 4c 45 54 45 20 74 72 69 67 67 65 72  y DELETE trigger
25f35 73 2e 20 20 2a 2f 0a 20 20 69 4c 61 62 65 6c 20  s.  */.  iLabel 
25f36 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
25f37 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 73 71 6c  eLabel(v);.  sql
25f38 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
25f39 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20  , OP_NotExists, 
25f3a 69 43 75 72 2c 20 69 4c 61 62 65 6c 2c 20 69 52  iCur, iLabel, iR
25f3b 6f 77 69 64 29 3b 0a 20 0a 20 20 2f 2a 20 49 66  owid);. .  /* If
25f3c 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 74   there are any t
25f3d 72 69 67 67 65 72 73 20 74 6f 20 66 69 72 65 2c  riggers to fire,
25f3e 20 61 6c 6c 6f 63 61 74 65 20 61 20 72 61 6e 67   allocate a rang
25f3f 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 74  e of registers t
25f40 6f 0a 20 20 2a 2a 20 75 73 65 20 66 6f 72 20 74  o.  ** use for t
25f41 68 65 20 6f 6c 64 2e 2a 20 72 65 66 65 72 65 6e  he old.* referen
25f42 63 65 73 20 69 6e 20 74 68 65 20 74 72 69 67 67  ces in the trigg
25f43 65 72 73 2e 20 20 2a 2f 0a 20 20 69 66 28 20 73  ers.  */.  if( s
25f44 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64  qlite3FkRequired
25f45 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30  (pParse, pTab, 0
25f46 2c 20 30 29 20 7c 7c 20 70 54 72 69 67 67 65 72  , 0) || pTrigger
25f47 20 29 7b 0a 20 20 20 20 75 33 32 20 6d 61 73 6b   ){.    u32 mask
25f48 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
25f49 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
25f4a 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e 73 20 69   OLD.* columns i
25f4b 6e 20 75 73 65 20 2a 2f 0a 20 20 20 20 69 6e 74  n use */.    int
25f4c 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   iCol;          
25f4d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
25f4e 65 72 61 74 6f 72 20 75 73 65 64 20 77 68 69 6c  erator used whil
25f4f 65 20 70 6f 70 75 6c 61 74 69 6e 67 20 4f 4c 44  e populating OLD
25f50 2e 2a 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 54 4f  .* */..    /* TO
25f51 44 4f 3a 20 43 6f 75 6c 64 20 75 73 65 20 74 65  DO: Could use te
25f52 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72  mporary register
25f53 73 20 68 65 72 65 2e 20 41 6c 73 6f 20 63 6f 75  s here. Also cou
25f54 6c 64 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20  ld attempt to.  
25f55 20 20 2a 2a 20 61 76 6f 69 64 20 63 6f 70 79 69    ** avoid copyi
25f56 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  ng the contents 
25f57 6f 66 20 74 68 65 20 72 6f 77 69 64 20 72 65 67  of the rowid reg
25f58 69 73 74 65 72 2e 20 20 2a 2f 0a 20 20 20 20 6d  ister.  */.    m
25f59 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 54 72 69  ask = sqlite3Tri
25f5a 67 67 65 72 43 6f 6c 6d 61 73 6b 28 0a 20 20 20  ggerColmask(.   
25f5b 20 20 20 20 20 70 50 61 72 73 65 2c 20 70 54 72       pParse, pTr
25f5c 69 67 67 65 72 2c 20 30 2c 20 30 2c 20 54 52 49  igger, 0, 0, TRI
25f5d 47 47 45 52 5f 42 45 46 4f 52 45 7c 54 52 49 47  GGER_BEFORE|TRIG
25f5e 47 45 52 5f 41 46 54 45 52 2c 20 70 54 61 62 2c  GER_AFTER, pTab,
25f5f 20 6f 6e 63 6f 6e 66 0a 20 20 20 20 29 3b 0a 20   onconf.    );. 
25f60 20 20 20 6d 61 73 6b 20 7c 3d 20 73 71 6c 69 74     mask |= sqlit
25f61 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 70 50 61 72  e3FkOldmask(pPar
25f62 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 69  se, pTab);.    i
25f63 4f 6c 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Old = pParse->nM
25f64 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61 72 73 65  em+1;.    pParse
25f65 2d 3e 6e 4d 65 6d 20 2b 3d 20 28 31 20 2b 20 70  ->nMem += (1 + p
25f66 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 0a 20 20 20  Tab->nCol);..   
25f67 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65   /* Populate the
25f68 20 4f 4c 44 2e 2a 20 70 73 65 75 64 6f 2d 74 61   OLD.* pseudo-ta
25f69 62 6c 65 20 72 65 67 69 73 74 65 72 20 61 72 72  ble register arr
25f6a 61 79 2e 20 54 68 65 73 65 20 76 61 6c 75 65 73  ay. These values
25f6b 20 77 69 6c 6c 20 62 65 20 0a 20 20 20 20 2a 2a   will be .    **
25f6c 20 75 73 65 64 20 62 79 20 61 6e 79 20 42 45 46   used by any BEF
25f6d 4f 52 45 20 61 6e 64 20 41 46 54 45 52 20 74 72  ORE and AFTER tr
25f6e 69 67 67 65 72 73 20 74 68 61 74 20 65 78 69 73  iggers that exis
25f6f 74 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  t.  */.    sqlit
25f70 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25f71 4f 50 5f 43 6f 70 79 2c 20 69 52 6f 77 69 64 2c  OP_Copy, iRowid,
25f72 20 69 4f 6c 64 29 3b 0a 20 20 20 20 66 6f 72 28   iOld);.    for(
25f73 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61  iCol=0; iCol<pTa
25f74 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29  b->nCol; iCol++)
25f75 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 61 73 6b  {.      if( mask
25f76 3d 3d 30 78 66 66 66 66 66 66 66 66 20 7c 7c 20  ==0xffffffff || 
25f77 6d 61 73 6b 26 28 31 3c 3c 69 43 6f 6c 29 20 29  mask&(1<<iCol) )
25f78 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 54  {.        int iT
25f79 61 72 67 65 74 20 3d 20 69 4f 6c 64 20 2b 20 69  arget = iOld + i
25f7a 43 6f 6c 20 2b 20 31 3b 0a 20 20 20 20 20 20 20  Col + 1;.       
25f7b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25f7c 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
25f7d 20 69 43 75 72 2c 20 69 43 6f 6c 2c 20 69 54 61   iCur, iCol, iTa
25f7e 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73  rget);.        s
25f7f 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61  qlite3ColumnDefa
25f80 75 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 43 6f  ult(v, pTab, iCo
25f81 6c 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20 20  l, iTarget);.   
25f82 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
25f83 2f 2a 20 49 6e 76 6f 6b 65 20 42 45 46 4f 52 45  /* Invoke BEFORE
25f84 20 44 45 4c 45 54 45 20 74 72 69 67 67 65 72 20   DELETE trigger 
25f85 70 72 6f 67 72 61 6d 73 2e 20 2a 2f 0a 20 20 20  programs. */.   
25f86 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
25f87 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70  rigger(pParse, p
25f88 54 72 69 67 67 65 72 2c 20 0a 20 20 20 20 20 20  Trigger, .      
25f89 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20    TK_DELETE, 0, 
25f8a 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20  TRIGGER_BEFORE, 
25f8b 70 54 61 62 2c 20 69 4f 6c 64 2c 20 6f 6e 63 6f  pTab, iOld, onco
25f8c 6e 66 2c 20 69 4c 61 62 65 6c 0a 20 20 20 20 29  nf, iLabel.    )
25f8d 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65 6b 20 74  ;..    /* Seek t
25f8e 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65  he cursor to the
25f8f 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74   row to be delet
25f90 65 64 20 61 67 61 69 6e 2e 20 49 74 20 6d 61 79  ed again. It may
25f91 20 62 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20   be that.    ** 
25f92 74 68 65 20 42 45 46 4f 52 45 20 74 72 69 67 67  the BEFORE trigg
25f93 65 72 73 20 63 6f 64 65 64 20 61 62 6f 76 65 20  ers coded above 
25f94 68 61 76 65 20 61 6c 72 65 61 64 79 20 72 65 6d  have already rem
25f95 6f 76 65 64 20 74 68 65 20 72 6f 77 0a 20 20 20  oved the row.   
25f96 20 2a 2a 20 62 65 69 6e 67 20 64 65 6c 65 74 65   ** being delete
25f97 64 2e 20 44 6f 20 6e 6f 74 20 61 74 74 65 6d 70  d. Do not attemp
25f98 74 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20  t to delete the 
25f99 72 6f 77 20 61 20 73 65 63 6f 6e 64 20 74 69 6d  row a second tim
25f9a 65 2c 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 64  e, and .    ** d
25f9b 6f 20 6e 6f 74 20 66 69 72 65 20 41 46 54 45 52  o not fire AFTER
25f9c 20 74 72 69 67 67 65 72 73 2e 20 20 2a 2f 0a 20   triggers.  */. 
25f9d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25f9e 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
25f9f 69 73 74 73 2c 20 69 43 75 72 2c 20 69 4c 61 62  ists, iCur, iLab
25fa0 65 6c 2c 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20  el, iRowid);..  
25fa1 20 20 2f 2a 20 44 6f 20 46 4b 20 70 72 6f 63 65    /* Do FK proce
25fa2 73 73 69 6e 67 2e 20 54 68 69 73 20 63 61 6c 6c  ssing. This call
25fa3 20 63 68 65 63 6b 73 20 74 68 61 74 20 61 6e 79   checks that any
25fa4 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   FK constraints 
25fa5 74 68 61 74 0a 20 20 20 20 2a 2a 20 72 65 66 65  that.    ** refe
25fa6 72 20 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20  r to this table 
25fa7 28 69 2e 65 2e 20 63 6f 6e 73 74 72 61 69 6e 74  (i.e. constraint
25fa8 73 20 61 74 74 61 63 68 65 64 20 74 6f 20 6f 74  s attached to ot
25fa9 68 65 72 20 74 61 62 6c 65 73 29 20 0a 20 20 20  her tables) .   
25faa 20 2a 2a 20 61 72 65 20 6e 6f 74 20 76 69 6f 6c   ** are not viol
25fab 61 74 65 64 20 62 79 20 64 65 6c 65 74 69 6e 67  ated by deleting
25fac 20 74 68 69 73 20 72 6f 77 2e 20 20 2a 2f 0a 20   this row.  */. 
25fad 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63     sqlite3FkChec
25fae 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  k(pParse, pTab, 
25faf 69 4f 6c 64 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20  iOld, 0);.  }.. 
25fb0 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 69   /* Delete the i
25fb1 6e 64 65 78 20 61 6e 64 20 74 61 62 6c 65 20 65  ndex and table e
25fb2 6e 74 72 69 65 73 2e 20 53 6b 69 70 20 74 68 69  ntries. Skip thi
25fb3 73 20 73 74 65 70 20 69 66 20 70 54 61 62 20 69  s step if pTab i
25fb4 73 20 72 65 61 6c 6c 79 0a 20 20 2a 2a 20 61 20  s really.  ** a 
25fb5 76 69 65 77 20 28 69 6e 20 77 68 69 63 68 20 63  view (in which c
25fb6 61 73 65 20 74 68 65 20 6f 6e 6c 79 20 65 66 66  ase the only eff
25fb7 65 63 74 20 6f 66 20 74 68 65 20 44 45 4c 45 54  ect of the DELET
25fb8 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74  E statement is t
25fb9 6f 0a 20 20 2a 2a 20 66 69 72 65 20 74 68 65 20  o.  ** fire the 
25fba 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67  INSTEAD OF trigg
25fbb 65 72 73 29 2e 20 20 2a 2f 20 0a 20 20 69 66 28  ers).  */ .  if(
25fbc 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d   pTab->pSelect==
25fbd 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
25fbe 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78  GenerateRowIndex
25fbf 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70  Delete(pParse, p
25fc0 54 61 62 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20  Tab, iCur, 0);. 
25fc1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25fc2 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65 6c 65 74  dOp2(v, OP_Delet
25fc3 65 2c 20 69 43 75 72 2c 20 28 63 6f 75 6e 74 3f  e, iCur, (count?
25fc4 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 3a 30  OPFLAG_NCHANGE:0
25fc5 29 29 3b 0a 20 20 20 20 69 66 28 20 63 6f 75 6e  ));.    if( coun
25fc6 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
25fc7 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
25fc8 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  , -1, pTab->zNam
25fc9 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
25fca 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44     }.  }..  /* D
25fcb 6f 20 61 6e 79 20 4f 4e 20 43 41 53 43 41 44 45  o any ON CASCADE
25fcc 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72 20 53 45  , SET NULL or SE
25fcd 54 20 44 45 46 41 55 4c 54 20 6f 70 65 72 61 74  T DEFAULT operat
25fce 69 6f 6e 73 20 72 65 71 75 69 72 65 64 20 74 6f  ions required to
25fcf 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 72 6f 77  .  ** handle row
25fd0 73 20 28 70 6f 73 73 69 62 6c 79 20 69 6e 20 6f  s (possibly in o
25fd1 74 68 65 72 20 74 61 62 6c 65 73 29 20 74 68 61  ther tables) tha
25fd2 74 20 72 65 66 65 72 20 76 69 61 20 61 20 66 6f  t refer via a fo
25fd3 72 65 69 67 6e 20 6b 65 79 0a 20 20 2a 2a 20 74  reign key.  ** t
25fd4 6f 20 74 68 65 20 72 6f 77 20 6a 75 73 74 20 64  o the row just d
25fd5 65 6c 65 74 65 64 2e 20 2a 2f 20 0a 20 20 73 71  eleted. */ .  sq
25fd6 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 70  lite3FkActions(p
25fd7 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 2c 20  Parse, pTab, 0, 
25fd8 69 4f 6c 64 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76  iOld);..  /* Inv
25fd9 6f 6b 65 20 41 46 54 45 52 20 44 45 4c 45 54 45  oke AFTER DELETE
25fda 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
25fdb 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43  s. */.  sqlite3C
25fdc 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
25fdd 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20  arse, pTrigger, 
25fde 0a 20 20 20 20 20 20 54 4b 5f 44 45 4c 45 54 45  .      TK_DELETE
25fdf 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54  , 0, TRIGGER_AFT
25fe0 45 52 2c 20 70 54 61 62 2c 20 69 4f 6c 64 2c 20  ER, pTab, iOld, 
25fe1 6f 6e 63 6f 6e 66 2c 20 69 4c 61 62 65 6c 0a 20  onconf, iLabel. 
25fe2 20 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68   );..  /* Jump h
25fe3 65 72 65 20 69 66 20 74 68 65 20 72 6f 77 20 68  ere if the row h
25fe4 61 64 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  ad already been 
25fe5 64 65 6c 65 74 65 64 20 62 65 66 6f 72 65 20 61  deleted before a
25fe6 6e 79 20 42 45 46 4f 52 45 0a 20 20 2a 2a 20 74  ny BEFORE.  ** t
25fe7 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 73 20  rigger programs 
25fe8 77 65 72 65 20 69 6e 76 6f 6b 65 64 2e 20 4f 72  were invoked. Or
25fe9 20 69 66 20 61 20 74 72 69 67 67 65 72 20 70 72   if a trigger pr
25fea 6f 67 72 61 6d 20 74 68 72 6f 77 73 20 61 20 0a  ogram throws a .
25feb 20 20 2a 2a 20 52 41 49 53 45 28 49 47 4e 4f 52    ** RAISE(IGNOR
25fec 45 29 20 65 78 63 65 70 74 69 6f 6e 2e 20 20 2a  E) exception.  *
25fed 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  /.  sqlite3VdbeR
25fee 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
25fef 4c 61 62 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Label);.}../*.**
25ff0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
25ff1 6e 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64  nerates VDBE cod
25ff2 65 20 74 68 61 74 20 63 61 75 73 65 73 20 74 68  e that causes th
25ff3 65 20 64 65 6c 65 74 69 6f 6e 20 6f 66 20 61 6c  e deletion of al
25ff4 6c 0a 2a 2a 20 69 6e 64 65 78 20 65 6e 74 72 69  l.** index entri
25ff5 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  es associated wi
25ff6 74 68 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  th a single row 
25ff7 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  of a single tabl
25ff8 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42  e..**.** The VDB
25ff9 45 20 6d 75 73 74 20 62 65 20 69 6e 20 61 20 70  E must be in a p
25ffa 61 72 74 69 63 75 6c 61 72 20 73 74 61 74 65 20  articular state 
25ffb 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  when this routin
25ffc 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20  e is called..** 
25ffd 54 68 65 73 65 20 61 72 65 20 74 68 65 20 72 65  These are the re
25ffe 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a  quirements:.**.*
25fff 2a 20 20 20 31 2e 20 20 41 20 72 65 61 64 2f 77  *   1.  A read/w
26000 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e  rite cursor poin
26001 74 69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74 68  ting to pTab, th
26002 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  e table containi
26003 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20 20  ng the row.**   
26004 20 20 20 20 74 6f 20 62 65 20 64 65 6c 65 74 65      to be delete
26005 64 2c 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65  d, must be opene
26006 64 20 61 73 20 63 75 72 73 6f 72 20 6e 75 6d 62  d as cursor numb
26007 65 72 20 22 69 43 75 72 22 2e 0a 2a 2a 0a 2a 2a  er "iCur"..**.**
26008 20 20 20 32 2e 20 20 52 65 61 64 2f 77 72 69 74     2.  Read/writ
26009 65 20 63 75 72 73 6f 72 73 20 66 6f 72 20 61 6c  e cursors for al
2600a 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61  l indices of pTa
2600b 62 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 61  b must be open a
2600c 73 0a 2a 2a 20 20 20 20 20 20 20 63 75 72 73 6f  s.**       curso
2600d 72 20 6e 75 6d 62 65 72 20 69 43 75 72 2b 69 20  r number iCur+i 
2600e 66 6f 72 20 74 68 65 20 69 2d 74 68 20 69 6e 64  for the i-th ind
2600f 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20  ex..**.**   3.  
26010 54 68 65 20 22 69 43 75 72 22 20 63 75 72 73 6f  The "iCur" curso
26011 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74 69  r must be pointi
26012 6e 67 20 74 6f 20 74 68 65 20 72 6f 77 20 74 68  ng to the row th
26013 61 74 20 69 73 20 74 6f 20 62 65 0a 2a 2a 20 20  at is to be.**  
26014 20 20 20 20 20 64 65 6c 65 74 65 64 2e 0a 2a 2f       deleted..*/
26015 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
26016 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65  void sqlite3Gene
26017 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65  rateRowIndexDele
26018 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  te(.  Parse *pPa
26019 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73  rse,     /* Pars
2601a 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ing and code gen
2601b 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20  erating context 
2601c 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2601d 2c 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65  ,       /* Table
2601e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2601f 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65  row to be delete
26020 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c  d */.  int iCur,
26021 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
26022 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
26023 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
26024 74 20 2a 61 52 65 67 49 64 78 20 20 20 20 20 20  t *aRegIdx      
26025 20 2f 2a 20 4f 6e 6c 79 20 64 65 6c 65 74 65 20   /* Only delete 
26026 69 66 20 61 52 65 67 49 64 78 21 3d 30 20 26 26  if aRegIdx!=0 &&
26027 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20 2a 2f   aRegIdx[i]>0 */
26028 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 49  .){.  int i;.  I
26029 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e  ndex *pIdx;.  in
2602a 74 20 72 31 3b 0a 0a 20 20 66 6f 72 28 69 3d 31  t r1;..  for(i=1
2602b 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
2602c 64 65 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20  dex; pIdx; i++, 
2602d 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2602e 29 7b 0a 20 20 20 20 69 66 28 20 61 52 65 67 49  ){.    if( aRegI
2602f 64 78 21 3d 30 20 26 26 20 61 52 65 67 49 64 78  dx!=0 && aRegIdx
26030 5b 69 2d 31 5d 3d 3d 30 20 29 20 63 6f 6e 74 69  [i-1]==0 ) conti
26031 6e 75 65 3b 0a 20 20 20 20 72 31 20 3d 20 73 71  nue;.    r1 = sq
26032 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64  lite3GenerateInd
26033 65 78 4b 65 79 28 70 50 61 72 73 65 2c 20 70 49  exKey(pParse, pI
26034 64 78 2c 20 69 43 75 72 2c 20 30 2c 20 30 29 3b  dx, iCur, 0, 0);
26035 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
26036 41 64 64 4f 70 33 28 70 50 61 72 73 65 2d 3e 70  AddOp3(pParse->p
26037 56 64 62 65 2c 20 4f 50 5f 49 64 78 44 65 6c 65  Vdbe, OP_IdxDele
26038 74 65 2c 20 69 43 75 72 2b 69 2c 20 72 31 2c 70  te, iCur+i, r1,p
26039 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b  Idx->nColumn+1);
2603a 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
2603b 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
2603c 20 77 69 6c 6c 20 61 73 73 65 6d 62 6c 65 20 61   will assemble a
2603d 6e 20 69 6e 64 65 78 20 6b 65 79 20 61 6e 64 20  n index key and 
2603e 70 75 74 20 69 74 20 69 6e 20 72 65 67 69 73 74  put it in regist
2603f 65 72 0a 2a 2a 20 72 65 67 4f 75 74 2e 20 20 54  er.** regOut.  T
26040 68 65 20 6b 65 79 20 77 69 74 68 20 62 65 20 66  he key with be f
26041 6f 72 20 69 6e 64 65 78 20 70 49 64 78 20 77 68  or index pIdx wh
26042 69 63 68 20 69 73 20 61 6e 20 69 6e 64 65 78 20  ich is an index 
26043 6f 6e 20 70 54 61 62 2e 0a 2a 2a 20 69 43 75 72  on pTab..** iCur
26044 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
26045 20 61 20 63 75 72 73 6f 72 20 6f 70 65 6e 20 6f   a cursor open o
26046 6e 20 74 68 65 20 70 54 61 62 20 74 61 62 6c 65  n the pTab table
26047 20 61 6e 64 20 70 6f 69 6e 74 69 6e 67 20 74 6f   and pointing to
26048 0a 2a 2a 20 74 68 65 20 65 6e 74 72 79 20 74 68  .** the entry th
26049 61 74 20 6e 65 65 64 73 20 69 6e 64 65 78 69 6e  at needs indexin
2604a 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  g..**.** Return 
2604b 61 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65  a register numbe
2604c 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 66  r which is the f
2604d 69 72 73 74 20 69 6e 20 61 20 62 6c 6f 63 6b 20  irst in a block 
2604e 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20  of.** registers 
2604f 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 65  that holds the e
26050 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 69  lements of the i
26051 6e 64 65 78 20 6b 65 79 2e 20 20 54 68 65 0a 2a  ndex key.  The.*
26052 2a 20 62 6c 6f 63 6b 20 6f 66 20 72 65 67 69 73  * block of regis
26053 74 65 72 73 20 68 61 73 20 61 6c 72 65 61 64 79  ters has already
26054 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61 74 65   been deallocate
26055 64 20 62 79 20 74 68 65 20 74 69 6d 65 0a 2a 2a  d by the time.**
26056 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
26057 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  turns..*/.SQLITE
26058 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
26059 69 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65  ite3GenerateInde
2605a 78 4b 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70  xKey(.  Parse *p
2605b 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61  Parse,     /* Pa
2605c 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2605d 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20  .  Index *pIdx, 
2605e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64        /* The ind
2605f 65 78 20 66 6f 72 20 77 68 69 63 68 20 74 6f 20  ex for which to 
26060 67 65 6e 65 72 61 74 65 20 61 20 6b 65 79 20 2a  generate a key *
26061 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20  /.  int iCur,   
26062 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
26063 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
26064 70 49 64 78 2d 3e 70 54 61 62 6c 65 20 74 61 62  pIdx->pTable tab
26065 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f  le */.  int regO
26066 75 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72  ut,        /* Wr
26067 69 74 65 20 74 68 65 20 6e 65 77 20 69 6e 64 65  ite the new inde
26068 78 20 6b 65 79 20 74 6f 20 74 68 69 73 20 72 65  x key to this re
26069 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  gister */.  int 
2606a 64 6f 4d 61 6b 65 52 65 63 20 20 20 20 20 20 2f  doMakeRec      /
2606b 2a 20 52 75 6e 20 74 68 65 20 4f 50 5f 4d 61 6b  * Run the OP_Mak
2606c 65 52 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74  eRecord instruct
2606d 69 6f 6e 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  ion if true */.)
2606e 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
2606f 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
26070 6e 74 20 6a 3b 0a 20 20 54 61 62 6c 65 20 2a 70  nt j;.  Table *p
26071 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62  Tab = pIdx->pTab
26072 6c 65 3b 0a 20 20 69 6e 74 20 72 65 67 42 61 73  le;.  int regBas
26073 65 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 0a  e;.  int nCol;..
26074 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e    nCol = pIdx->n
26075 43 6f 6c 75 6d 6e 3b 0a 20 20 72 65 67 42 61 73  Column;.  regBas
26076 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  e = sqlite3GetTe
26077 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
26078 6e 43 6f 6c 2b 31 29 3b 0a 20 20 73 71 6c 69 74  nCol+1);.  sqlit
26079 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2607a 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c 20  OP_Rowid, iCur, 
2607b 72 65 67 42 61 73 65 2b 6e 43 6f 6c 29 3b 0a 20  regBase+nCol);. 
2607c 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f 6c   for(j=0; j<nCol
2607d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  ; j++){.    int 
2607e 69 64 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f  idx = pIdx->aiCo
2607f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 69 66 28  lumn[j];.    if(
26080 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b 65   idx==pTab->iPKe
26081 79 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  y ){.      sqlit
26082 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26083 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 42 61 73  OP_SCopy, regBas
26084 65 2b 6e 43 6f 6c 2c 20 72 65 67 42 61 73 65 2b  e+nCol, regBase+
26085 6a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  j);.    }else{. 
26086 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26087 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
26088 75 6d 6e 2c 20 69 43 75 72 2c 20 69 64 78 2c 20  umn, iCur, idx, 
26089 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20  regBase+j);.    
2608a 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44    sqlite3ColumnD
2608b 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20  efault(v, pTab, 
2608c 69 64 78 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a  idx, -1);.    }.
2608d 20 20 7d 0a 20 20 69 66 28 20 64 6f 4d 61 6b 65    }.  if( doMake
2608e 52 65 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Rec ){.    sqlit
2608f 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
26090 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72  OP_MakeRecord, r
26091 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31 2c 20  egBase, nCol+1, 
26092 72 65 67 4f 75 74 29 3b 0a 20 20 20 20 73 71 6c  regOut);.    sql
26093 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
26094 28 76 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33 49  (v, -1, sqlite3I
26095 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28  ndexAffinityStr(
26096 76 2c 20 70 49 64 78 29 2c 20 30 29 3b 0a 20 20  v, pIdx), 0);.  
26097 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
26098 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
26099 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65  (pParse, regBase
2609a 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20 7d 0a 20  , nCol+1);.  }. 
2609b 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2609c 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
2609d 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31   regBase, nCol+1
2609e 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 67 42  );.  return regB
2609f 61 73 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ase;.}../*******
260a0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 64  ******* End of d
260a1 65 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  elete.c ********
260a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
260a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
260a4 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
260a5 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
260a6 6c 65 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a  le func.c ******
260a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
260a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
260a9 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
260aa 30 32 20 46 65 62 72 75 61 72 79 20 32 33 0a 2a  02 February 23.*
260ab 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
260ac 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
260ad 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
260ae 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
260af 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
260b0 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
260b1 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
260b2 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
260b3 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
260b4 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
260b5 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
260b6 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
260b7 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
260b8 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
260b9 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
260ba 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
260bb 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
260bc 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
260bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
260be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
260bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
260c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
260c1 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
260c2 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
260c3 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
260c4 6e 74 20 76 61 72 69 6f 75 73 20 53 51 4c 0a 2a  nt various SQL.*
260c5 2a 20 66 75 6e 63 74 69 6f 6e 73 20 6f 66 20 53  * functions of S
260c6 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  QLite.  .**.** T
260c7 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65  here is only one
260c8 20 65 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c   exported symbol
260c9 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20   in this file - 
260ca 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  the function.** 
260cb 73 71 6c 69 74 65 52 65 67 69 73 74 65 72 42 75  sqliteRegisterBu
260cc 69 6c 64 69 6e 46 75 6e 63 74 69 6f 6e 73 28 29  ildinFunctions()
260cd 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f   found at the bo
260ce 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65  ttom of the file
260cf 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63  ..** All other c
260d0 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f  ode has file sco
260d1 70 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  pe..*/../*.** Re
260d2 74 75 72 6e 20 74 68 65 20 63 6f 6c 6c 61 74 69  turn the collati
260d3 6e 67 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 6f  ng function asso
260d4 63 69 61 74 65 64 20 77 69 74 68 20 61 20 66 75  ciated with a fu
260d5 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
260d6 63 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  c CollSeq *sqlit
260d7 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71  e3GetFuncCollSeq
260d8 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
260d9 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 72 65   *context){.  re
260da 74 75 72 6e 20 63 6f 6e 74 65 78 74 2d 3e 70 43  turn context->pC
260db 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  oll;.}../*.** Im
260dc 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
260dd 74 68 65 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74  the non-aggregat
260de 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28  e min() and max(
260df 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73  ) functions.*/.s
260e0 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d 61  tatic void minma
260e1 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  xFunc(.  sqlite3
260e2 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
260e3 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
260e4 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
260e5 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69  *argv.){.  int i
260e6 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 20 20 20  ;.  int mask;   
260e7 20 2f 2a 20 30 20 66 6f 72 20 6d 69 6e 28 29 20   /* 0 for min() 
260e8 6f 72 20 30 78 66 66 66 66 66 66 66 66 20 66 6f  or 0xffffffff fo
260e9 72 20 6d 61 78 28 29 20 2a 2f 0a 20 20 69 6e 74  r max() */.  int
260ea 20 69 42 65 73 74 3b 0a 20 20 43 6f 6c 6c 53 65   iBest;.  CollSe
260eb 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 61 73 73  q *pColl;..  ass
260ec 65 72 74 28 20 61 72 67 63 3e 31 20 29 3b 0a 20  ert( argc>1 );. 
260ed 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 5f   mask = sqlite3_
260ee 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
260ef 74 29 3d 3d 30 20 3f 20 30 20 3a 20 2d 31 3b 0a  t)==0 ? 0 : -1;.
260f0 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
260f1 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28  3GetFuncCollSeq(
260f2 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65  context);.  asse
260f3 72 74 28 20 70 43 6f 6c 6c 20 29 3b 0a 20 20 61  rt( pColl );.  a
260f4 73 73 65 72 74 28 20 6d 61 73 6b 3d 3d 2d 31 20  ssert( mask==-1 
260f5 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20  || mask==0 );.  
260f6 69 42 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28  iBest = 0;.  if(
260f7 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
260f8 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
260f9 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75  LITE_NULL ) retu
260fa 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69  rn;.  for(i=1; i
260fb 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  <argc; i++){.   
260fc 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
260fd 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29  ue_type(argv[i])
260fe 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20  ==SQLITE_NULL ) 
260ff 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20  return;.    if( 
26100 28 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61  (sqlite3MemCompa
26101 72 65 28 61 72 67 76 5b 69 42 65 73 74 5d 2c 20  re(argv[iBest], 
26102 61 72 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c 29 5e  argv[i], pColl)^
26103 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a 20 20 20 20  mask)>=0 ){.    
26104 20 20 74 65 73 74 63 61 73 65 28 20 6d 61 73 6b    testcase( mask
26105 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 42 65  ==0 );.      iBe
26106 73 74 20 3d 20 69 3b 0a 20 20 20 20 7d 0a 20 20  st = i;.    }.  
26107 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  }.  sqlite3_resu
26108 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74  lt_value(context
26109 2c 20 61 72 67 76 5b 69 42 65 73 74 5d 29 3b 0a  , argv[iBest]);.
2610a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2610b 74 68 65 20 74 79 70 65 20 6f 66 20 74 68 65 20  the type of the 
2610c 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61  argument..*/.sta
2610d 74 69 63 20 76 6f 69 64 20 74 79 70 65 6f 66 46  tic void typeofF
2610e 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
2610f 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
26110 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
26111 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
26112 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73  **argv.){.  cons
26113 74 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 0a 20  t char *z = 0;. 
26114 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
26115 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 73 77  R(NotUsed);.  sw
26116 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61  itch( sqlite3_va
26117 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
26118 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  ) ){.    case SQ
26119 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 20 7a 20  LITE_INTEGER: z 
2611a 3d 20 22 69 6e 74 65 67 65 72 22 3b 20 62 72 65  = "integer"; bre
2611b 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ak;.    case SQL
2611c 49 54 45 5f 54 45 58 54 3a 20 20 20 20 7a 20 3d  ITE_TEXT:    z =
2611d 20 22 74 65 78 74 22 3b 20 20 20 20 62 72 65 61   "text";    brea
2611e 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
2611f 54 45 5f 46 4c 4f 41 54 3a 20 20 20 7a 20 3d 20  TE_FLOAT:   z = 
26120 22 72 65 61 6c 22 3b 20 20 20 20 62 72 65 61 6b  "real";    break
26121 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ;.    case SQLIT
26122 45 5f 42 4c 4f 42 3a 20 20 20 20 7a 20 3d 20 22  E_BLOB:    z = "
26123 62 6c 6f 62 22 3b 20 20 20 20 62 72 65 61 6b 3b  blob";    break;
26124 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20  .    default:   
26125 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20 22 6e            z = "n
26126 75 6c 6c 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a  ull";    break;.
26127 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65    }.  sqlite3_re
26128 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
26129 74 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45  t, z, -1, SQLITE
2612a 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 0a 2f 2a  _STATIC);.}.../*
2612b 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
2612c 6f 6e 20 6f 66 20 74 68 65 20 6c 65 6e 67 74 68  on of the length
2612d 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f 0a 73  () function.*/.s
2612e 74 61 74 69 63 20 76 6f 69 64 20 6c 65 6e 67 74  tatic void lengt
2612f 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  hFunc(.  sqlite3
26130 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
26131 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
26132 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
26133 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6c  *argv.){.  int l
26134 65 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61  en;..  assert( a
26135 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53  rgc==1 );.  UNUS
26136 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
26137 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71  c);.  switch( sq
26138 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
26139 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20  (argv[0]) ){.   
2613a 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f   case SQLITE_BLO
2613b 42 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  B:.    case SQLI
2613c 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20  TE_INTEGER:.    
2613d 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41  case SQLITE_FLOA
2613e 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  T: {.      sqlit
2613f 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f  e3_result_int(co
26140 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f 76  ntext, sqlite3_v
26141 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
26142 30 5d 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61  0]));.      brea
26143 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
26144 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b  e SQLITE_TEXT: {
26145 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 6e 73  .      const uns
26146 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 20 3d 20  igned char *z = 
26147 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
26148 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  xt(argv[0]);.   
26149 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65     if( z==0 ) re
2614a 74 75 72 6e 3b 0a 20 20 20 20 20 20 6c 65 6e 20  turn;.      len 
2614b 3d 20 30 3b 0a 20 20 20 20 20 20 77 68 69 6c 65  = 0;.      while
2614c 28 20 2a 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  ( *z ){.        
2614d 6c 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 53  len++;.        S
2614e 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
2614f 7a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  z);.      }.    
26150 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
26151 5f 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 6c 65  _int(context, le
26152 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  n);.      break;
26153 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
26154 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  lt: {.      sqli
26155 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28  te3_result_null(
26156 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  context);.      
26157 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
26158 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
26159 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
2615a 61 62 73 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a  abs() function.*
2615b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 62  /.static void ab
2615c 73 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f  sFunc(sqlite3_co
2615d 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
2615e 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
2615f 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
26160 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
26161 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =1 );.  UNUSED_P
26162 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a  ARAMETER(argc);.
26163 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65    switch( sqlite
26164 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
26165 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73  v[0]) ){.    cas
26166 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  e SQLITE_INTEGER
26167 3a 20 7b 0a 20 20 20 20 20 20 69 36 34 20 69 56  : {.      i64 iV
26168 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
26169 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d  ue_int64(argv[0]
2616a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 56 61  );.      if( iVa
2616b 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  l<0 ){.        i
2616c 66 28 20 28 69 56 61 6c 3c 3c 31 29 3d 3d 30 20  f( (iVal<<1)==0 
2616d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
2616e 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
2616f 72 28 63 6f 6e 74 65 78 74 2c 20 22 69 6e 74 65  r(context, "inte
26170 67 65 72 20 6f 76 65 72 66 6c 6f 77 22 2c 20 2d  ger overflow", -
26171 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  1);.          re
26172 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a  turn;.        }.
26173 20 20 20 20 20 20 20 20 69 56 61 6c 20 3d 20 2d          iVal = -
26174 69 56 61 6c 3b 0a 20 20 20 20 20 20 7d 20 0a 20  iVal;.      } . 
26175 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
26176 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78  ult_int64(contex
26177 74 2c 20 69 56 61 6c 29 3b 0a 20 20 20 20 20 20  t, iVal);.      
26178 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
26179 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c   case SQLITE_NUL
2617a 4c 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  L: {.      sqlit
2617b 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 63  e3_result_null(c
2617c 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 62  ontext);.      b
2617d 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
2617e 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
2617f 20 64 6f 75 62 6c 65 20 72 56 61 6c 20 3d 20 73   double rVal = s
26180 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
26181 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ble(argv[0]);.  
26182 20 20 20 20 69 66 28 20 72 56 61 6c 3c 30 20 29      if( rVal<0 )
26183 20 72 56 61 6c 20 3d 20 2d 72 56 61 6c 3b 0a 20   rVal = -rVal;. 
26184 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
26185 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65  ult_double(conte
26186 78 74 2c 20 72 56 61 6c 29 3b 0a 20 20 20 20 20  xt, rVal);.     
26187 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
26188 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
26189 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
2618a 20 73 75 62 73 74 72 28 29 20 66 75 6e 63 74 69   substr() functi
2618b 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 73 74 72  on..**.** substr
2618c 28 78 2c 70 31 2c 70 32 29 20 20 72 65 74 75 72  (x,p1,p2)  retur
2618d 6e 73 20 70 32 20 63 68 61 72 61 63 74 65 72 73  ns p2 characters
2618e 20 6f 66 20 78 5b 5d 20 62 65 67 69 6e 6e 69 6e   of x[] beginnin
2618f 67 20 77 69 74 68 20 70 31 2e 0a 2a 2a 20 70 31  g with p1..** p1
26190 20 69 73 20 31 2d 69 6e 64 65 78 65 64 2e 20 20   is 1-indexed.  
26191 53 6f 20 73 75 62 73 74 72 28 78 2c 31 2c 31 29  So substr(x,1,1)
26192 20 72 65 74 75 72 6e 73 20 74 68 65 20 66 69 72   returns the fir
26193 73 74 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20  st character.** 
26194 6f 66 20 78 2e 20 20 49 66 20 78 20 69 73 20 74  of x.  If x is t
26195 65 78 74 2c 20 74 68 65 6e 20 77 65 20 61 63 74  ext, then we act
26196 75 61 6c 6c 79 20 63 6f 75 6e 74 20 55 54 46 2d  ually count UTF-
26197 38 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a  8 characters..**
26198 20 49 66 20 78 20 69 73 20 61 20 62 6c 6f 62 2c   If x is a blob,
26199 20 74 68 65 6e 20 77 65 20 63 6f 75 6e 74 20 62   then we count b
2619a 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70  ytes..**.** If p
2619b 31 20 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74  1 is negative, t
2619c 68 65 6e 20 77 65 20 62 65 67 69 6e 20 61 62 73  hen we begin abs
2619d 28 70 31 29 20 66 72 6f 6d 20 74 68 65 20 65 6e  (p1) from the en
2619e 64 20 6f 66 20 78 5b 5d 2e 0a 2a 2a 0a 2a 2a 20  d of x[]..**.** 
2619f 49 66 20 70 32 20 69 73 20 6e 65 67 61 74 69 76  If p2 is negativ
261a0 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20 70 32  e, return the p2
261a1 20 63 68 61 72 61 63 74 65 72 73 20 70 72 65 63   characters prec
261a2 65 65 64 69 6e 67 20 70 31 2e 0a 2a 2f 0a 73 74  eeding p1..*/.st
261a3 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 72  atic void substr
261a4 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
261a5 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
261a6 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
261a7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
261a8 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
261a9 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
261aa 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  ;.  const unsign
261ab 65 64 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69  ed char *z2;.  i
261ac 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20 70 30  nt len;.  int p0
261ad 74 79 70 65 3b 0a 20 20 69 36 34 20 70 31 2c 20  type;.  i64 p1, 
261ae 70 32 3b 0a 20 20 69 6e 74 20 6e 65 67 50 32 20  p2;.  int negP2 
261af 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
261b0 61 72 67 63 3d 3d 33 20 7c 7c 20 61 72 67 63 3d  argc==3 || argc=
261b1 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  =2 );.  if( sqli
261b2 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
261b3 72 67 76 5b 31 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[1])==SQLITE_
261b4 4e 55 4c 4c 0a 20 20 20 7c 7c 20 28 61 72 67 63  NULL.   || (argc
261b5 3d 3d 33 20 26 26 20 73 71 6c 69 74 65 33 5f 76  ==3 && sqlite3_v
261b6 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 32  alue_type(argv[2
261b7 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 29  ])==SQLITE_NULL)
261b8 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  .  ){.    return
261b9 3b 0a 20 20 7d 0a 20 20 70 30 74 79 70 65 20 3d  ;.  }.  p0type =
261ba 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
261bb 79 70 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ype(argv[0]);.  
261bc 70 31 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  p1 = sqlite3_val
261bd 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b  ue_int(argv[1]);
261be 0a 20 20 69 66 28 20 70 30 74 79 70 65 3d 3d 53  .  if( p0type==S
261bf 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20  QLITE_BLOB ){.  
261c0 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f    len = sqlite3_
261c1 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
261c2 5b 30 5d 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71  [0]);.    z = sq
261c3 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
261c4 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69  (argv[0]);.    i
261c5 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( z==0 ) return
261c6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 65  ;.    assert( le
261c7 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  n==sqlite3_value
261c8 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 20  _bytes(argv[0]) 
261c9 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
261ca 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  z = sqlite3_valu
261cb 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
261cc 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20  .    if( z==0 ) 
261cd 72 65 74 75 72 6e 3b 0a 20 20 20 20 6c 65 6e 20  return;.    len 
261ce 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 31 3c  = 0;.    if( p1<
261cf 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 7a  0 ){.      for(z
261d0 32 3d 7a 3b 20 2a 7a 32 3b 20 6c 65 6e 2b 2b 29  2=z; *z2; len++)
261d1 7b 0a 20 20 20 20 20 20 20 20 53 51 4c 49 54 45  {.        SQLITE
261d2 5f 53 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a  _SKIP_UTF8(z2);.
261d3 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
261d4 7d 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 33 20  }.  if( argc==3 
261d5 29 7b 0a 20 20 20 20 70 32 20 3d 20 73 71 6c 69  ){.    p2 = sqli
261d6 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72  te3_value_int(ar
261d7 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20  gv[2]);.    if( 
261d8 70 32 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 32  p2<0 ){.      p2
261d9 20 3d 20 2d 70 32 3b 0a 20 20 20 20 20 20 6e 65   = -p2;.      ne
261da 67 50 32 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  gP2 = 1;.    }. 
261db 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 32 20 3d   }else{.    p2 =
261dc 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
261dd 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
261de 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  xt)->aLimit[SQLI
261df 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
261e0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 31 3c 30  ;.  }.  if( p1<0
261e1 20 29 7b 0a 20 20 20 20 70 31 20 2b 3d 20 6c 65   ){.    p1 += le
261e2 6e 3b 0a 20 20 20 20 69 66 28 20 70 31 3c 30 20  n;.    if( p1<0 
261e3 29 7b 0a 20 20 20 20 20 20 70 32 20 2b 3d 20 70  ){.      p2 += p
261e4 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32 3c  1;.      if( p2<
261e5 30 20 29 20 70 32 20 3d 20 30 3b 0a 20 20 20 20  0 ) p2 = 0;.    
261e6 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d 0a    p1 = 0;.    }.
261e7 20 20 7d 65 6c 73 65 20 69 66 28 20 70 31 3e 30    }else if( p1>0
261e8 20 29 7b 0a 20 20 20 20 70 31 2d 2d 3b 0a 20 20   ){.    p1--;.  
261e9 7d 65 6c 73 65 20 69 66 28 20 70 32 3e 30 20 29  }else if( p2>0 )
261ea 7b 0a 20 20 20 20 70 32 2d 2d 3b 0a 20 20 7d 0a  {.    p2--;.  }.
261eb 20 20 69 66 28 20 6e 65 67 50 32 20 29 7b 0a 20    if( negP2 ){. 
261ec 20 20 20 70 31 20 2d 3d 20 70 32 3b 0a 20 20 20     p1 -= p2;.   
261ed 20 69 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20   if( p1<0 ){.   
261ee 20 20 20 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20     p2 += p1;.   
261ef 20 20 20 70 31 20 3d 20 30 3b 0a 20 20 20 20 7d     p1 = 0;.    }
261f0 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
261f1 31 3e 3d 30 20 26 26 20 70 32 3e 3d 30 20 29 3b  1>=0 && p2>=0 );
261f2 0a 20 20 69 66 28 20 70 30 74 79 70 65 21 3d 53  .  if( p0type!=S
261f3 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20  QLITE_BLOB ){.  
261f4 20 20 77 68 69 6c 65 28 20 2a 7a 20 26 26 20 70    while( *z && p
261f5 31 20 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54  1 ){.      SQLIT
261f6 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a  E_SKIP_UTF8(z);.
261f7 20 20 20 20 20 20 70 31 2d 2d 3b 0a 20 20 20 20        p1--;.    
261f8 7d 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20  }.    for(z2=z; 
261f9 2a 7a 32 20 26 26 20 70 32 3b 20 70 32 2d 2d 29  *z2 && p2; p2--)
261fa 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53  {.      SQLITE_S
261fb 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20 20  KIP_UTF8(z2);.  
261fc 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
261fd 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
261fe 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 28  ext, (char*)z, (
261ff 69 6e 74 29 28 7a 32 2d 7a 29 2c 20 53 51 4c 49  int)(z2-z), SQLI
26200 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
26201 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
26202 70 31 2b 70 32 3e 6c 65 6e 20 29 7b 0a 20 20 20  p1+p2>len ){.   
26203 20 20 20 70 32 20 3d 20 6c 65 6e 2d 70 31 3b 0a     p2 = len-p1;.
26204 20 20 20 20 20 20 69 66 28 20 70 32 3c 30 20 29        if( p2<0 )
26205 20 70 32 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20   p2 = 0;.    }. 
26206 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
26207 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20  t_blob(context, 
26208 28 63 68 61 72 2a 29 26 7a 5b 70 31 5d 2c 20 28  (char*)&z[p1], (
26209 69 6e 74 29 70 32 2c 20 53 51 4c 49 54 45 5f 54  int)p2, SQLITE_T
2620a 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d  RANSIENT);.  }.}
2620b 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
2620c 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 6f  tation of the ro
2620d 75 6e 64 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a  und() function.*
2620e 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
2620f 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
26210 4f 49 4e 54 0a 73 74 61 74 69 63 20 76 6f 69 64  OINT.static void
26211 20 72 6f 75 6e 64 46 75 6e 63 28 73 71 6c 69 74   roundFunc(sqlit
26212 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
26213 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
26214 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
26215 72 67 76 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20  rgv){.  int n = 
26216 30 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20  0;.  double r;. 
26217 20 63 68 61 72 20 2a 7a 42 75 66 3b 0a 20 20 61   char *zBuf;.  a
26218 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c  ssert( argc==1 |
26219 7c 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69  | argc==2 );.  i
2621a 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20  f( argc==2 ){.  
2621b 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c    if( SQLITE_NUL
2621c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  L==sqlite3_value
2621d 5f 74 79 70 65 28 61 72 67 76 5b 31 5d 29 20 29  _type(argv[1]) )
2621e 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 6e 20 3d   return;.    n =
2621f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
26220 6e 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20  nt(argv[1]);.   
26221 20 69 66 28 20 6e 3e 33 30 20 29 20 6e 20 3d 20   if( n>30 ) n = 
26222 33 30 3b 0a 20 20 20 20 69 66 28 20 6e 3c 30 20  30;.    if( n<0 
26223 29 20 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  ) n = 0;.  }.  i
26224 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
26225 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d  _type(argv[0])==
26226 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65  SQLITE_NULL ) re
26227 74 75 72 6e 3b 0a 20 20 72 20 3d 20 73 71 6c 69  turn;.  r = sqli
26228 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65  te3_value_double
26229 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 42 75  (argv[0]);.  zBu
2622a 66 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  f = sqlite3_mpri
2622b 6e 74 66 28 22 25 2e 2a 66 22 2c 6e 2c 72 29 3b  ntf("%.*f",n,r);
2622c 0a 20 20 69 66 28 20 7a 42 75 66 3d 3d 30 20 29  .  if( zBuf==0 )
2622d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
2622e 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
2622f 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 65 6c  (context);.  }el
26230 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41  se{.    sqlite3A
26231 74 6f 46 28 7a 42 75 66 2c 20 26 72 29 3b 0a 20  toF(zBuf, &r);. 
26232 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
26233 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74  zBuf);.    sqlit
26234 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65  e3_result_double
26235 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a 20 20  (context, r);.  
26236 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  }.}.#endif../*.*
26237 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65  * Allocate nByte
26238 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20   bytes of space 
26239 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 6d 61  using sqlite3_ma
2623a 6c 6c 6f 63 28 29 2e 20 49 66 20 74 68 65 0a 2a  lloc(). If the.*
2623b 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69  * allocation fai
2623c 6c 73 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  ls, call sqlite3
2623d 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f  _result_error_no
2623e 6d 65 6d 28 29 20 74 6f 20 6e 6f 74 69 66 79 0a  mem() to notify.
2623f 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
26240 68 61 6e 64 6c 65 20 74 68 61 74 20 6d 61 6c 6c  handle that mall
26241 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65 64 20  oc() has failed 
26242 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e  and return NULL.
26243 0a 2a 2a 20 49 66 20 6e 42 79 74 65 20 69 73 20  .** If nByte is 
26244 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
26245 6d 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6f  maximum string o
26246 72 20 62 6c 6f 62 20 6c 65 6e 67 74 68 2c 20 74  r blob length, t
26247 68 65 6e 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20  hen.** raise an 
26248 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 78  SQLITE_TOOBIG ex
26249 63 65 70 74 69 6f 6e 20 61 6e 64 20 72 65 74 75  ception and retu
2624a 72 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74  rn NULL..*/.stat
2624b 69 63 20 76 6f 69 64 20 2a 63 6f 6e 74 65 78 74  ic void *context
2624c 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 63  Malloc(sqlite3_c
2624d 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
2624e 20 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 63   i64 nByte){.  c
2624f 68 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 65  har *z;.  sqlite
26250 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
26251 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
26252 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73  e(context);.  as
26253 73 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 3b  sert( nByte>0 );
26254 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 42 79  .  testcase( nBy
26255 74 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  te==db->aLimit[S
26256 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
26257 54 48 5d 20 29 3b 0a 20 20 74 65 73 74 63 61 73  TH] );.  testcas
26258 65 28 20 6e 42 79 74 65 3d 3d 64 62 2d 3e 61 4c  e( nByte==db->aL
26259 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
2625a 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20  T_LENGTH]+1 );. 
2625b 20 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61   if( nByte>db->a
2625c 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
2625d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
2625e 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2625f 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f  _error_toobig(co
26260 6e 74 65 78 74 29 3b 0a 20 20 20 20 7a 20 3d 20  ntext);.    z = 
26261 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
26262 7a 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  z = sqlite3Mallo
26263 63 28 28 69 6e 74 29 6e 42 79 74 65 29 3b 0a 20  c((int)nByte);. 
26264 20 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20     if( !z ){.   
26265 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
26266 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
26267 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20  ntext);.    }.  
26268 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a  }.  return z;.}.
26269 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
2626a 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 75 70 70  ation of the upp
2626b 65 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 29  er() and lower()
2626c 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a   SQL functions..
2626d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
2626e 70 70 65 72 46 75 6e 63 28 73 71 6c 69 74 65 33  pperFunc(sqlite3
2626f 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
26270 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
26271 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
26272 76 29 7b 0a 20 20 63 68 61 72 20 2a 7a 31 3b 0a  v){.  char *z1;.
26273 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32    const char *z2
26274 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  ;.  int i, n;.  
26275 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
26276 28 61 72 67 63 29 3b 0a 20 20 7a 32 20 3d 20 28  (argc);.  z2 = (
26277 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
26278 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
26279 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33  );.  n = sqlite3
2627a 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
2627b 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69  v[0]);.  /* Veri
2627c 66 79 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c  fy that the call
2627d 20 74 6f 20 5f 62 79 74 65 73 28 29 20 64 6f 65   to _bytes() doe
2627e 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65  s not invalidate
2627f 20 74 68 65 20 5f 74 65 78 74 28 29 20 70 6f 69   the _text() poi
26280 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74  nter */.  assert
26281 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c  ( z2==(char*)sql
26282 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
26283 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66  argv[0]) );.  if
26284 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d  ( z2 ){.    z1 =
26285 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63   contextMalloc(c
26286 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29  ontext, ((i64)n)
26287 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 20  +1);.    if( z1 
26288 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
26289 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20  z1, z2, n+1);.  
2628a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b      for(i=0; z1[
2628b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  i]; i++){.      
2628c 20 20 7a 31 5b 69 5d 20 3d 20 28 63 68 61 72 29    z1[i] = (char)
2628d 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 7a  sqlite3Toupper(z
2628e 31 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  1[i]);.      }. 
2628f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
26290 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
26291 2c 20 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65  , z1, -1, sqlite
26292 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20  3_free);.    }. 
26293 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
26294 20 6c 6f 77 65 72 46 75 6e 63 28 73 71 6c 69 74   lowerFunc(sqlit
26295 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
26296 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
26297 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
26298 72 67 76 29 7b 0a 20 20 75 38 20 2a 7a 31 3b 0a  rgv){.  u8 *z1;.
26299 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32    const char *z2
2629a 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  ;.  int i, n;.  
2629b 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
2629c 28 61 72 67 63 29 3b 0a 20 20 7a 32 20 3d 20 28  (argc);.  z2 = (
2629d 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
2629e 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
2629f 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33  );.  n = sqlite3
262a0 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
262a1 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69  v[0]);.  /* Veri
262a2 66 79 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c  fy that the call
262a3 20 74 6f 20 5f 62 79 74 65 73 28 29 20 64 6f 65   to _bytes() doe
262a4 73 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65  s not invalidate
262a5 20 74 68 65 20 5f 74 65 78 74 28 29 20 70 6f 69   the _text() poi
262a6 6e 74 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74  nter */.  assert
262a7 28 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c  ( z2==(char*)sql
262a8 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
262a9 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66  argv[0]) );.  if
262aa 28 20 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d  ( z2 ){.    z1 =
262ab 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63   contextMalloc(c
262ac 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29  ontext, ((i64)n)
262ad 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 20  +1);.    if( z1 
262ae 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
262af 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20  z1, z2, n+1);.  
262b0 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b      for(i=0; z1[
262b1 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  i]; i++){.      
262b2 20 20 7a 31 5b 69 5d 20 3d 20 73 71 6c 69 74 65    z1[i] = sqlite
262b3 33 54 6f 6c 6f 77 65 72 28 7a 31 5b 69 5d 29 3b  3Tolower(z1[i]);
262b4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
262b5 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
262b6 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  xt(context, (cha
262b7 72 20 2a 29 7a 31 2c 20 2d 31 2c 20 73 71 6c 69  r *)z1, -1, sqli
262b8 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d  te3_free);.    }
262b9 0a 20 20 7d 0a 7d 0a 0a 0a 23 69 66 20 30 20 20  .  }.}...#if 0  
262ba 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
262bb 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e 20   is never used. 
262bc 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 4f 41  */./*.** The COA
262bd 4c 45 53 43 45 28 29 20 61 6e 64 20 49 46 4e 55  LESCE() and IFNU
262be 4c 4c 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 75  LL() functions u
262bf 73 65 64 20 74 6f 20 62 65 20 69 6d 70 6c 65 6d  sed to be implem
262c0 65 6e 74 65 64 20 61 73 20 73 68 6f 77 6e 0a 2a  ented as shown.*
262c1 2a 20 68 65 72 65 2e 20 20 42 75 74 20 6e 6f 77  * here.  But now
262c2 20 74 68 65 79 20 61 72 65 20 69 6d 70 6c 65 6d   they are implem
262c3 65 6e 74 65 64 20 61 73 20 56 44 42 45 20 63 6f  ented as VDBE co
262c4 64 65 20 73 6f 20 74 68 61 74 20 75 6e 75 73 65  de so that unuse
262c5 64 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 64  d arguments.** d
262c6 6f 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 62 65  o not have to be
262c7 20 63 6f 6d 70 75 74 65 64 2e 20 20 54 68 69 73   computed.  This
262c8 20 6c 65 67 61 63 79 20 69 6d 70 6c 65 6d 65 6e   legacy implemen
262c9 74 61 74 69 6f 6e 20 69 73 20 72 65 74 61 69 6e  tation is retain
262ca 65 64 20 61 73 0a 2a 2a 20 63 6f 6d 6d 65 6e 74  ed as.** comment
262cb 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ..*/./*.** Imple
262cc 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
262cd 20 49 46 4e 55 4c 4c 28 29 2c 20 4e 56 4c 28 29   IFNULL(), NVL()
262ce 2c 20 61 6e 64 20 43 4f 41 4c 45 53 43 45 28 29  , and COALESCE()
262cf 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 0a 2a 2a   functions.  .**
262d0 20 41 6c 6c 20 74 68 72 65 65 20 64 6f 20 74 68   All three do th
262d1 65 20 73 61 6d 65 20 74 68 69 6e 67 2e 20 20 54  e same thing.  T
262d2 68 65 79 20 72 65 74 75 72 6e 20 74 68 65 20 66  hey return the f
262d3 69 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a  irst non-NULL.**
262d4 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74   argument..*/.st
262d5 61 74 69 63 20 76 6f 69 64 20 69 66 6e 75 6c 6c  atic void ifnull
262d6 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
262d7 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
262d8 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
262d9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
262da 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  argv.){.  int i;
262db 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 61 72  .  for(i=0; i<ar
262dc 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  gc; i++){.    if
262dd 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21 3d 73  ( SQLITE_NULL!=s
262de 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
262df 65 28 61 72 67 76 5b 69 5d 29 20 29 7b 0a 20 20  e(argv[i]) ){.  
262e0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
262e1 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65 78 74  lt_value(context
262e2 2c 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20  , argv[i]);.    
262e3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
262e4 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e   }.}.#endif /* N
262e5 4f 54 20 55 53 45 44 20 2a 2f 0a 23 64 65 66 69  OT USED */.#defi
262e6 6e 65 20 69 66 6e 75 6c 6c 46 75 6e 63 20 76 65  ne ifnullFunc ve
262e7 72 73 69 6f 6e 46 75 6e 63 20 20 20 2f 2a 20 53  rsionFunc   /* S
262e8 75 62 73 74 69 74 75 74 65 20 66 75 6e 63 74 69  ubstitute functi
262e9 6f 6e 20 2d 20 6e 65 76 65 72 20 63 61 6c 6c 65  on - never calle
262ea 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  d */../*.** Impl
262eb 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61  ementation of ra
262ec 6e 64 6f 6d 28 29 2e 20 20 52 65 74 75 72 6e 20  ndom().  Return 
262ed 61 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67 65 72  a random integer
262ee 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  .  .*/.static vo
262ef 69 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28 0a 20  id randomFunc(. 
262f0 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
262f1 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
262f2 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
262f3 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
262f4 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65  sed2.){.  sqlite
262f5 5f 69 6e 74 36 34 20 72 3b 0a 20 20 55 4e 55 53  _int64 r;.  UNUS
262f6 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
262f7 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
262f8 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64  ;.  sqlite3_rand
262f9 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 29  omness(sizeof(r)
262fa 2c 20 26 72 29 3b 0a 20 20 69 66 28 20 72 3c 30  , &r);.  if( r<0
262fb 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 6e 65   ){.    /* We ne
262fc 65 64 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20  ed to prevent a 
262fd 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 6f 66  random number of
262fe 20 30 78 38 30 30 30 30 30 30 30 30 30 30 30 30   0x8000000000000
262ff 30 30 30 20 0a 20 20 20 20 2a 2a 20 28 6f 72 20  000 .    ** (or 
26300 2d 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37  -922337203685477
26301 35 38 30 38 29 20 73 69 6e 63 65 20 77 68 65 6e  5808) since when
26302 20 79 6f 75 20 64 6f 20 61 62 73 28 29 20 6f 66   you do abs() of
26303 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 6e 75 6d   that.    ** num
26304 62 65 72 20 6f 66 20 79 6f 75 20 67 65 74 20 74  ber of you get t
26305 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 62 61  he same value ba
26306 63 6b 20 61 67 61 69 6e 2e 20 20 54 6f 20 64 6f  ck again.  To do
26307 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 6e 20   this.    ** in 
26308 61 20 77 61 79 20 74 68 61 74 20 69 73 20 74 65  a way that is te
26309 73 74 61 62 6c 65 2c 20 6d 61 73 6b 20 74 68 65  stable, mask the
2630a 20 73 69 67 6e 20 62 69 74 20 6f 66 66 20 6f 66   sign bit off of
2630b 20 6e 65 67 61 74 69 76 65 0a 20 20 20 20 2a 2a   negative.    **
2630c 20 76 61 6c 75 65 73 2c 20 72 65 73 75 6c 74 69   values, resulti
2630d 6e 67 20 69 6e 20 61 20 70 6f 73 69 74 69 76 65  ng in a positive
2630e 20 76 61 6c 75 65 2e 20 20 54 68 65 6e 20 74 61   value.  Then ta
2630f 6b 65 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 32  ke the .    ** 2
26310 73 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 6f 66 20  s complement of 
26311 74 68 61 74 20 70 6f 73 69 74 69 76 65 20 76 61  that positive va
26312 6c 75 65 2e 20 20 54 68 65 20 65 6e 64 20 72 65  lue.  The end re
26313 73 75 6c 74 20 63 61 6e 0a 20 20 20 20 2a 2a 20  sult can.    ** 
26314 74 68 65 72 65 66 6f 72 65 20 62 65 20 6e 6f 20  therefore be no 
26315 6c 65 73 73 20 74 68 61 6e 20 2d 39 32 32 33 33  less than -92233
26316 37 32 30 33 36 38 35 34 37 37 35 38 30 37 2e 0a  72036854775807..
26317 20 20 20 20 2a 2f 0a 20 20 20 20 72 20 3d 20 2d      */.    r = -
26318 28 72 20 5e 20 28 28 28 73 71 6c 69 74 65 33 5f  (r ^ (((sqlite3_
26319 69 6e 74 36 34 29 31 29 3c 3c 36 33 29 29 3b 0a  int64)1)<<63));.
2631a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65    }.  sqlite3_re
2631b 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65  sult_int64(conte
2631c 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  xt, r);.}../*.**
2631d 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
2631e 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 4e 29  of randomblob(N)
2631f 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61 6e 64  .  Return a rand
26320 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68 61 74 20  om blob.** that 
26321 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67 2e  is N bytes long.
26322 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
26323 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20 20 73 71  randomBlob(.  sq
26324 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
26325 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
26326 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
26327 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
26328 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67 6e 65  int n;.  unsigne
26329 64 20 63 68 61 72 20 2a 70 3b 0a 20 20 61 73 73  d char *p;.  ass
2632a 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a  ert( argc==1 );.
2632b 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
2632c 45 52 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20  ER(argc);.  n = 
2632d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
2632e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66  t(argv[0]);.  if
2632f 28 20 6e 3c 31 20 29 7b 0a 20 20 20 20 6e 20 3d  ( n<1 ){.    n =
26330 20 31 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 63 6f   1;.  }.  p = co
26331 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74  ntextMalloc(cont
26332 65 78 74 2c 20 6e 29 3b 0a 20 20 69 66 28 20 70  ext, n);.  if( p
26333 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
26334 72 61 6e 64 6f 6d 6e 65 73 73 28 6e 2c 20 70 29  randomness(n, p)
26335 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
26336 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65 78  sult_blob(contex
26337 74 2c 20 28 63 68 61 72 2a 29 70 2c 20 6e 2c 20  t, (char*)p, n, 
26338 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20  sqlite3_free);. 
26339 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
2633a 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
2633b 65 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  e last_insert_ro
2633c 77 69 64 28 29 20 53 51 4c 20 66 75 6e 63 74 69  wid() SQL functi
2633d 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 0a  on.  The return.
2633e 2a 2a 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  ** value is the 
2633f 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69  same as the sqli
26340 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f  te3_last_insert_
26341 72 6f 77 69 64 28 29 20 41 50 49 20 66 75 6e 63  rowid() API func
26342 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
26343 76 6f 69 64 20 6c 61 73 74 5f 69 6e 73 65 72 74  void last_insert
26344 5f 72 6f 77 69 64 28 0a 20 20 73 71 6c 69 74 65  _rowid(.  sqlite
26345 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
26346 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55 73  xt, .  int NotUs
26347 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76  ed, .  sqlite3_v
26348 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a  alue **NotUsed2.
26349 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
2634a 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
2634b 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
2634c 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f  text);.  UNUSED_
2634d 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
2634e 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
2634f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
26350 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 73  int64(context, s
26351 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
26352 72 74 5f 72 6f 77 69 64 28 64 62 29 29 3b 0a 7d  rt_rowid(db));.}
26353 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
26354 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68  tation of the ch
26355 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63  anges() SQL func
26356 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72  tion.  The retur
26357 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a 2a  n value is the.*
26358 2a 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71  * same as the sq
26359 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20  lite3_changes() 
2635a 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  API function..*/
2635b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 61  .static void cha
2635c 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 65 33 5f  nges(.  sqlite3_
2635d 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2635e 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c  ,.  int NotUsed,
2635f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
26360 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20   **NotUsed2.){. 
26361 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
26362 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
26363 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
26364 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
26365 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
26366 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c  NotUsed2);.  sql
26367 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
26368 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33  context, sqlite3
26369 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a 7d  _changes(db));.}
2636a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
2636b 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 6f  tation of the to
2636c 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 53 51  tal_changes() SQ
2636d 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  L function.  The
2636e 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
2636f 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20  .** the same as 
26370 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61  the sqlite3_tota
26371 6c 5f 63 68 61 6e 67 65 73 28 29 20 41 50 49 20  l_changes() API 
26372 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
26373 74 69 63 20 76 6f 69 64 20 74 6f 74 61 6c 5f 63  tic void total_c
26374 68 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 65  hanges(.  sqlite
26375 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
26376 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
26377 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
26378 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
26379 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2637a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2637b 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
2637c 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  xt);.  UNUSED_PA
2637d 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
2637e 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73  , NotUsed2);.  s
2637f 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
26380 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74  t(context, sqlit
26381 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
26382 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  (db));.}../*.** 
26383 41 20 73 74 72 75 63 74 75 72 65 20 64 65 66 69  A structure defi
26384 6e 69 6e 67 20 68 6f 77 20 74 6f 20 64 6f 20 47  ning how to do G
26385 4c 4f 42 2d 73 74 79 6c 65 20 63 6f 6d 70 61 72  LOB-style compar
26386 69 73 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  isons..*/.struct
26387 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 7b 0a 20   compareInfo {. 
26388 20 75 38 20 6d 61 74 63 68 41 6c 6c 3b 0a 20 20   u8 matchAll;.  
26389 75 38 20 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75  u8 matchOne;.  u
2638a 38 20 6d 61 74 63 68 53 65 74 3b 0a 20 20 75 38  8 matchSet;.  u8
2638b 20 6e 6f 43 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a 0a   noCase;.};../*.
2638c 2a 2a 20 46 6f 72 20 4c 49 4b 45 20 61 6e 64 20  ** For LIKE and 
2638d 47 4c 4f 42 20 6d 61 74 63 68 69 6e 67 20 6f 6e  GLOB matching on
2638e 20 45 42 43 44 49 43 20 6d 61 63 68 69 6e 65 73   EBCDIC machines
2638f 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20 65 76  , assume that ev
26390 65 72 79 0a 2a 2a 20 63 68 61 72 61 63 74 65 72  ery.** character
26391 20 69 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20   is exactly one 
26392 62 79 74 65 20 69 6e 20 73 69 7a 65 2e 20 20 41  byte in size.  A
26393 6c 73 6f 2c 20 61 6c 6c 20 63 68 61 72 61 63 74  lso, all charact
26394 65 72 73 20 61 72 65 0a 2a 2a 20 61 62 6c 65 20  ers are.** able 
26395 74 6f 20 70 61 72 74 69 63 69 70 61 74 65 20 69  to participate i
26396 6e 20 75 70 70 65 72 2d 63 61 73 65 2d 74 6f 2d  n upper-case-to-
26397 6c 6f 77 65 72 2d 63 61 73 65 20 6d 61 70 70 69  lower-case mappi
26398 6e 67 73 20 69 6e 20 45 42 43 44 49 43 0a 2a 2a  ngs in EBCDIC.**
26399 20 77 68 65 72 65 61 73 20 6f 6e 6c 79 20 63 68   whereas only ch
2639a 61 72 61 63 74 65 72 73 20 6c 65 73 73 20 74 68  aracters less th
2639b 61 6e 20 30 78 38 30 20 64 6f 20 69 6e 20 41 53  an 0x80 do in AS
2639c 43 49 49 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  CII..*/.#if defi
2639d 6e 65 64 28 53 51 4c 49 54 45 5f 45 42 43 44 49  ned(SQLITE_EBCDI
2639e 43 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  C).# define sqli
2639f 74 65 33 55 74 66 38 52 65 61 64 28 41 2c 43 29  te3Utf8Read(A,C)
263a0 20 20 20 20 28 2a 28 41 2b 2b 29 29 0a 23 20 64      (*(A++)).# d
263a1 65 66 69 6e 65 20 47 6c 6f 67 55 70 70 65 72 54  efine GlogUpperT
263a2 6f 4c 6f 77 65 72 28 41 29 20 20 20 20 20 41 20  oLower(A)     A 
263a3 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  = sqlite3UpperTo
263a4 4c 6f 77 65 72 5b 41 5d 0a 23 65 6c 73 65 0a 23  Lower[A].#else.#
263a5 20 64 65 66 69 6e 65 20 47 6c 6f 67 55 70 70 65   define GlogUppe
263a6 72 54 6f 4c 6f 77 65 72 28 41 29 20 20 20 20 20  rToLower(A)     
263a7 69 66 28 20 41 3c 30 78 38 30 20 29 7b 20 41 20  if( A<0x80 ){ A 
263a8 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f  = sqlite3UpperTo
263a9 4c 6f 77 65 72 5b 41 5d 3b 20 7d 0a 23 65 6e 64  Lower[A]; }.#end
263aa 69 66 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  if..static const
263ab 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49   struct compareI
263ac 6e 66 6f 20 67 6c 6f 62 49 6e 66 6f 20 3d 20 7b  nfo globInfo = {
263ad 20 27 2a 27 2c 20 27 3f 27 2c 20 27 5b 27 2c 20   '*', '?', '[', 
263ae 30 20 7d 3b 0a 2f 2a 20 54 68 65 20 63 6f 72 72  0 };./* The corr
263af 65 63 74 20 53 51 4c 2d 39 32 20 62 65 68 61 76  ect SQL-92 behav
263b0 69 6f 72 20 69 73 20 66 6f 72 20 74 68 65 20 4c  ior is for the L
263b1 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  IKE operator to 
263b2 69 67 6e 6f 72 65 0a 2a 2a 20 63 61 73 65 2e 20  ignore.** case. 
263b3 20 54 68 75 73 20 20 27 61 27 20 4c 49 4b 45 20   Thus  'a' LIKE 
263b4 27 41 27 20 77 6f 75 6c 64 20 62 65 20 74 72 75  'A' would be tru
263b5 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  e. */.static con
263b6 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72  st struct compar
263b7 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f 4e 6f  eInfo likeInfoNo
263b8 72 6d 20 3d 20 7b 20 27 25 27 2c 20 27 5f 27 2c  rm = { '%', '_',
263b9 20 20 20 30 2c 20 31 20 7d 3b 0a 2f 2a 20 49 66     0, 1 };./* If
263ba 20 53 51 4c 49 54 45 5f 43 41 53 45 5f 53 45 4e   SQLITE_CASE_SEN
263bb 53 49 54 49 56 45 5f 4c 49 4b 45 20 69 73 20 64  SITIVE_LIKE is d
263bc 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
263bd 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 0a 2a   LIKE operator.*
263be 2a 20 69 73 20 63 61 73 65 20 73 65 6e 73 69 74  * is case sensit
263bf 69 76 65 20 63 61 75 73 69 6e 67 20 27 61 27 20  ive causing 'a' 
263c0 4c 49 4b 45 20 27 41 27 20 74 6f 20 62 65 20 66  LIKE 'A' to be f
263c1 61 6c 73 65 20 2a 2f 0a 73 74 61 74 69 63 20 63  alse */.static c
263c2 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70  onst struct comp
263c3 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f  areInfo likeInfo
263c4 41 6c 74 20 3d 20 7b 20 27 25 27 2c 20 27 5f 27  Alt = { '%', '_'
263c5 2c 20 20 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 0a  ,   0, 0 };../*.
263c6 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20 55  ** Compare two U
263c7 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 6f 72  TF-8 strings for
263c8 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 65 20   equality where 
263c9 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67  the first string
263ca 20 63 61 6e 0a 2a 2a 20 70 6f 74 65 6e 74 69 61   can.** potentia
263cb 6c 6c 79 20 62 65 20 61 20 22 67 6c 6f 62 22 20  lly be a "glob" 
263cc 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 52 65 74  expression.  Ret
263cd 75 72 6e 20 74 72 75 65 20 28 31 29 20 69 66 20  urn true (1) if 
263ce 74 68 65 79 0a 2a 2a 20 61 72 65 20 74 68 65 20  they.** are the 
263cf 73 61 6d 65 20 61 6e 64 20 66 61 6c 73 65 20 28  same and false (
263d0 30 29 20 69 66 20 74 68 65 79 20 61 72 65 20 64  0) if they are d
263d1 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  ifferent..**.** 
263d2 47 6c 6f 62 62 69 6e 67 20 72 75 6c 65 73 3a 0a  Globbing rules:.
263d3 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 2a 27 20 20  **.**      '*'  
263d4 20 20 20 20 20 4d 61 74 63 68 65 73 20 61 6e 79       Matches any
263d5 20 73 65 71 75 65 6e 63 65 20 6f 66 20 7a 65 72   sequence of zer
263d6 6f 20 6f 72 20 6d 6f 72 65 20 63 68 61 72 61 63  o or more charac
263d7 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ters..**.**     
263d8 20 27 3f 27 20 20 20 20 20 20 20 4d 61 74 63 68   '?'       Match
263d9 65 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63  es exactly one c
263da 68 61 72 61 63 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  haracter..**.** 
263db 20 20 20 20 5b 2e 2e 2e 5d 20 20 20 20 20 20 4d      [...]      M
263dc 61 74 63 68 65 73 20 6f 6e 65 20 63 68 61 72 61  atches one chara
263dd 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20 65 6e  cter from the en
263de 63 6c 6f 73 65 64 20 6c 69 73 74 20 6f 66 0a 2a  closed list of.*
263df 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
263e0 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a   characters..**.
263e1 2a 2a 20 20 20 20 20 5b 5e 2e 2e 2e 5d 20 20 20  **     [^...]   
263e2 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 63 68    Matches one ch
263e3 61 72 61 63 74 65 72 20 6e 6f 74 20 69 6e 20 74  aracter not in t
263e4 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74  he enclosed list
263e5 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 65  ..**.** With the
263e6 20 5b 2e 2e 2e 5d 20 61 6e 64 20 5b 5e 2e 2e 2e   [...] and [^...
263e7 5d 20 6d 61 74 63 68 69 6e 67 2c 20 61 20 27 5d  ] matching, a ']
263e8 27 20 63 68 61 72 61 63 74 65 72 20 63 61 6e 20  ' character can 
263e9 62 65 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69  be included.** i
263ea 6e 20 74 68 65 20 6c 69 73 74 20 62 79 20 6d 61  n the list by ma
263eb 6b 69 6e 67 20 69 74 20 74 68 65 20 66 69 72 73  king it the firs
263ec 74 20 63 68 61 72 61 63 74 65 72 20 61 66 74 65  t character afte
263ed 72 20 27 5b 27 20 6f 72 20 27 5e 27 2e 20 20 41  r '[' or '^'.  A
263ee 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 63 68 61  .** range of cha
263ef 72 61 63 74 65 72 73 20 63 61 6e 20 62 65 20 73  racters can be s
263f0 70 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 27  pecified using '
263f1 2d 27 2e 20 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a  -'.  Example:.**
263f2 20 22 5b 61 2d 7a 5d 22 20 6d 61 74 63 68 65 73   "[a-z]" matches
263f3 20 61 6e 79 20 73 69 6e 67 6c 65 20 6c 6f 77 65   any single lowe
263f4 72 2d 63 61 73 65 20 6c 65 74 74 65 72 2e 20 20  r-case letter.  
263f5 54 6f 20 6d 61 74 63 68 20 61 20 27 2d 27 2c 20  To match a '-', 
263f6 6d 61 6b 65 0a 2a 2a 20 69 74 20 74 68 65 20 6c  make.** it the l
263f7 61 73 74 20 63 68 61 72 61 63 74 65 72 20 69 6e  ast character in
263f8 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a   the list..**.**
263f9 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
263fa 20 75 73 75 61 6c 6c 79 20 71 75 69 63 6b 2c 20   usually quick, 
263fb 62 75 74 20 63 61 6e 20 62 65 20 4e 2a 2a 32 20  but can be N**2 
263fc 69 6e 20 74 68 65 20 77 6f 72 73 74 20 63 61 73  in the worst cas
263fd 65 2e 0a 2a 2a 0a 2a 2a 20 48 69 6e 74 73 3a 20  e..**.** Hints: 
263fe 74 6f 20 6d 61 74 63 68 20 27 2a 27 20 6f 72 20  to match '*' or 
263ff 27 3f 27 2c 20 70 75 74 20 74 68 65 6d 20 69 6e  '?', put them in
26400 20 22 5b 5d 22 2e 20 20 4c 69 6b 65 20 74 68 69   "[]".  Like thi
26401 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
26402 20 61 62 63 5b 2a 5d 78 79 7a 20 20 20 20 20 20   abc[*]xyz      
26403 20 20 4d 61 74 63 68 65 73 20 22 61 62 63 2a 78    Matches "abc*x
26404 79 7a 22 20 6f 6e 6c 79 0a 2a 2f 0a 73 74 61 74  yz" only.*/.stat
26405 69 63 20 69 6e 74 20 70 61 74 74 65 72 6e 43 6f  ic int patternCo
26406 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 20 75  mpare(.  const u
26407 38 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 20 20  8 *zPattern,    
26408 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
26409 20 67 6c 6f 62 20 70 61 74 74 65 72 6e 20 2a 2f   glob pattern */
2640a 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 53 74  .  const u8 *zSt
2640b 72 69 6e 67 2c 20 20 20 20 20 20 20 20 20 20 20  ring,           
2640c 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e      /* The strin
2640d 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61  g to compare aga
2640e 69 6e 73 74 20 74 68 65 20 67 6c 6f 62 20 2a 2f  inst the glob */
2640f 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20  .  const struct 
26410 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e  compareInfo *pIn
26411 66 6f 2c 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69  fo, /* Informati
26412 6f 6e 20 61 62 6f 75 74 20 68 6f 77 20 74 6f 20  on about how to 
26413 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 65 20 2a  do the compare *
26414 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 65 73  /.  const int es
26415 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c               
26416 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61       /* The esca
26417 70 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  pe character */.
26418 29 7b 0a 20 20 69 6e 74 20 63 2c 20 63 32 3b 0a  ){.  int c, c2;.
26419 20 20 69 6e 74 20 69 6e 76 65 72 74 3b 0a 20 20    int invert;.  
2641a 69 6e 74 20 73 65 65 6e 3b 0a 20 20 75 38 20 6d  int seen;.  u8 m
2641b 61 74 63 68 4f 6e 65 20 3d 20 70 49 6e 66 6f 2d  atchOne = pInfo-
2641c 3e 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75 38 20  >matchOne;.  u8 
2641d 6d 61 74 63 68 41 6c 6c 20 3d 20 70 49 6e 66 6f  matchAll = pInfo
2641e 2d 3e 6d 61 74 63 68 41 6c 6c 3b 0a 20 20 75 38  ->matchAll;.  u8
2641f 20 6d 61 74 63 68 53 65 74 20 3d 20 70 49 6e 66   matchSet = pInf
26420 6f 2d 3e 6d 61 74 63 68 53 65 74 3b 0a 20 20 75  o->matchSet;.  u
26421 38 20 6e 6f 43 61 73 65 20 3d 20 70 49 6e 66 6f  8 noCase = pInfo
26422 2d 3e 6e 6f 43 61 73 65 3b 20 0a 20 20 69 6e 74  ->noCase; .  int
26423 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b   prevEscape = 0;
26424 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
26425 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 61  the previous cha
26426 72 61 63 74 65 72 20 77 61 73 20 27 65 73 63 61  racter was 'esca
26427 70 65 27 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28  pe' */..  while(
26428 20 28 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66   (c = sqlite3Utf
26429 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c 26  8Read(zPattern,&
2642a 7a 50 61 74 74 65 72 6e 29 29 21 3d 30 20 29 7b  zPattern))!=0 ){
2642b 0a 20 20 20 20 69 66 28 20 21 70 72 65 76 45 73  .    if( !prevEs
2642c 63 61 70 65 20 26 26 20 63 3d 3d 6d 61 74 63 68  cape && c==match
2642d 41 6c 6c 20 29 7b 0a 20 20 20 20 20 20 77 68 69  All ){.      whi
2642e 6c 65 28 20 28 63 3d 73 71 6c 69 74 65 33 55 74  le( (c=sqlite3Ut
2642f 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c  f8Read(zPattern,
26430 26 7a 50 61 74 74 65 72 6e 29 29 20 3d 3d 20 6d  &zPattern)) == m
26431 61 74 63 68 41 6c 6c 0a 20 20 20 20 20 20 20 20  atchAll.        
26432 20 20 20 20 20 20 20 7c 7c 20 63 20 3d 3d 20 6d         || c == m
26433 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20  atchOne ){.     
26434 20 20 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 4f     if( c==matchO
26435 6e 65 20 26 26 20 73 71 6c 69 74 65 33 55 74 66  ne && sqlite3Utf
26436 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26  8Read(zString, &
26437 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20  zString)==0 ){. 
26438 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
26439 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
2643a 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63     }.      if( c
2643b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
2643c 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
2643d 65 6c 73 65 20 69 66 28 20 63 3d 3d 65 73 63 20  else if( c==esc 
2643e 29 7b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73  ){.        c = s
2643f 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
26440 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65  Pattern, &zPatte
26441 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  rn);.        if(
26442 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   c==0 ){.       
26443 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
26444 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
26445 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63 68 53  se if( c==matchS
26446 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  et ){.        as
26447 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29 3b 20  sert( esc==0 ); 
26448 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
26449 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b  is GLOB, not LIK
2644a 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73  E */.        ass
2644b 65 72 74 28 20 6d 61 74 63 68 53 65 74 3c 30 78  ert( matchSet<0x
2644c 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20 69 73  80 );  /* '[' is
2644d 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63   a single-byte c
2644e 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20  haracter */.    
2644f 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53 74 72      while( *zStr
26450 69 6e 67 20 26 26 20 70 61 74 74 65 72 6e 43 6f  ing && patternCo
26451 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72 6e 5b  mpare(&zPattern[
26452 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 49 6e 66  -1],zString,pInf
26453 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a 20 20 20  o,esc)==0 ){.   
26454 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b         SQLITE_SK
26455 49 50 5f 55 54 46 38 28 7a 53 74 72 69 6e 67 29  IP_UTF8(zString)
26456 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
26457 20 20 20 20 72 65 74 75 72 6e 20 2a 7a 53 74 72      return *zStr
26458 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20 20 7d 0a  ing!=0;.      }.
26459 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 32        while( (c2
2645a 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
2645b 61 64 28 7a 53 74 72 69 6e 67 2c 26 7a 53 74 72  ad(zString,&zStr
2645c 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  ing))!=0 ){.    
2645d 20 20 20 20 69 66 28 20 6e 6f 43 61 73 65 20 29      if( noCase )
2645e 7b 0a 20 20 20 20 20 20 20 20 20 20 47 6c 6f 67  {.          Glog
2645f 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29  UpperToLower(c2)
26460 3b 0a 20 20 20 20 20 20 20 20 20 20 47 6c 6f 67  ;.          Glog
26461 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 29 3b  UpperToLower(c);
26462 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
26463 28 20 63 32 20 21 3d 20 30 20 26 26 20 63 32 20  ( c2 != 0 && c2 
26464 21 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20 20  != c ){.        
26465 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33      c2 = sqlite3
26466 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67  Utf8Read(zString
26467 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20  , &zString);.   
26468 20 20 20 20 20 20 20 20 20 47 6c 6f 67 55 70 70           GlogUpp
26469 65 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b 0a 20  erToLower(c2);. 
2646a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2646b 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2646c 20 20 20 20 77 68 69 6c 65 28 20 63 32 20 21 3d      while( c2 !=
2646d 20 30 20 26 26 20 63 32 20 21 3d 20 63 20 29 7b   0 && c2 != c ){
2646e 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 32 20  .            c2 
2646f 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
26470 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74 72  d(zString, &zStr
26471 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ing);.          
26472 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
26473 20 20 20 20 69 66 28 20 63 32 3d 3d 30 20 29 20      if( c2==0 ) 
26474 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
26475 20 20 69 66 28 20 70 61 74 74 65 72 6e 43 6f 6d    if( patternCom
26476 70 61 72 65 28 7a 50 61 74 74 65 72 6e 2c 7a 53  pare(zPattern,zS
26477 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73 63 29  tring,pInfo,esc)
26478 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
26479 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
2647a 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  n 0;.    }else i
2647b 66 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26  f( !prevEscape &
2647c 26 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20 29 7b  & c==matchOne ){
2647d 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
2647e 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69  e3Utf8Read(zStri
2647f 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30  ng, &zString)==0
26480 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
26481 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
26482 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d    }else if( c==m
26483 61 74 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20  atchSet ){.     
26484 20 69 6e 74 20 70 72 69 6f 72 5f 63 20 3d 20 30   int prior_c = 0
26485 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
26486 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 2f 2a 20  esc==0 );    /* 
26487 54 68 69 73 20 6f 6e 6c 79 20 6f 63 63 75 72 73  This only occurs
26488 20 66 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c   for GLOB, not L
26489 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 73 65 65  IKE */.      see
2648a 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 76  n = 0;.      inv
2648b 65 72 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63  ert = 0;.      c
2648c 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
2648d 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74  ad(zString, &zSt
2648e 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28  ring);.      if(
2648f 20 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   c==0 ) return 0
26490 3b 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c  ;.      c2 = sql
26491 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
26492 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e  ttern, &zPattern
26493 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63 32 3d  );.      if( c2=
26494 3d 27 5e 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='^' ){.        
26495 69 6e 76 65 72 74 20 3d 20 31 3b 0a 20 20 20 20  invert = 1;.    
26496 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33      c2 = sqlite3
26497 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
26498 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20  n, &zPattern);. 
26499 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2649a 20 63 32 3d 3d 27 5d 27 20 29 7b 0a 20 20 20 20   c2==']' ){.    
2649b 20 20 20 20 69 66 28 20 63 3d 3d 27 5d 27 20 29      if( c==']' )
2649c 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20   seen = 1;.     
2649d 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55     c2 = sqlite3U
2649e 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e  tf8Read(zPattern
2649f 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a 20 20  , &zPattern);.  
264a0 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68 69 6c      }.      whil
264a1 65 28 20 63 32 20 26 26 20 63 32 21 3d 27 5d 27  e( c2 && c2!=']'
264a2 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
264a3 63 32 3d 3d 27 2d 27 20 26 26 20 7a 50 61 74 74  c2=='-' && zPatt
264a4 65 72 6e 5b 30 5d 21 3d 27 5d 27 20 26 26 20 7a  ern[0]!=']' && z
264a5 50 61 74 74 65 72 6e 5b 30 5d 21 3d 30 20 26 26  Pattern[0]!=0 &&
264a6 20 70 72 69 6f 72 5f 63 3e 30 20 29 7b 0a 20 20   prior_c>0 ){.  
264a7 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c          c2 = sql
264a8 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
264a9 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e  ttern, &zPattern
264aa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
264ab 20 63 3e 3d 70 72 69 6f 72 5f 63 20 26 26 20 63   c>=prior_c && c
264ac 3c 3d 63 32 20 29 20 73 65 65 6e 20 3d 20 31 3b  <=c2 ) seen = 1;
264ad 0a 20 20 20 20 20 20 20 20 20 20 70 72 69 6f 72  .          prior
264ae 5f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  _c = 0;.        
264af 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
264b0 20 69 66 28 20 63 3d 3d 63 32 20 29 7b 0a 20 20   if( c==c2 ){.  
264b1 20 20 20 20 20 20 20 20 20 20 73 65 65 6e 20 3d            seen =
264b2 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   1;.          }.
264b3 20 20 20 20 20 20 20 20 20 20 70 72 69 6f 72 5f            prior_
264b4 63 20 3d 20 63 32 3b 0a 20 20 20 20 20 20 20 20  c = c2;.        
264b5 7d 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20 73  }.        c2 = s
264b6 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
264b7 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65  Pattern, &zPatte
264b8 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rn);.      }.   
264b9 20 20 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20     if( c2==0 || 
264ba 28 73 65 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d  (seen ^ invert)=
264bb 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
264bc 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
264bd 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73      }else if( es
264be 63 3d 3d 63 20 26 26 20 21 70 72 65 76 45 73 63  c==c && !prevEsc
264bf 61 70 65 20 29 7b 0a 20 20 20 20 20 20 70 72 65  ape ){.      pre
264c0 76 45 73 63 61 70 65 20 3d 20 31 3b 0a 20 20 20  vEscape = 1;.   
264c1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 32   }else{.      c2
264c2 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
264c3 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53 74  ad(zString, &zSt
264c4 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66 28  ring);.      if(
264c5 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20 20   noCase ){.     
264c6 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f     GlogUpperToLo
264c7 77 65 72 28 63 29 3b 0a 20 20 20 20 20 20 20 20  wer(c);.        
264c8 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72  GlogUpperToLower
264c9 28 63 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  (c2);.      }.  
264ca 20 20 20 20 69 66 28 20 63 21 3d 63 32 20 29 7b      if( c!=c2 ){
264cb 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
264cc 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
264cd 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30 3b   prevEscape = 0;
264ce 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
264cf 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d 30 3b  urn *zString==0;
264d0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20  .}../*.** Count 
264d1 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  the number of ti
264d2 6d 65 73 20 74 68 61 74 20 74 68 65 20 4c 49 4b  mes that the LIK
264d3 45 20 6f 70 65 72 61 74 6f 72 20 28 6f 72 20 47  E operator (or G
264d4 4c 4f 42 20 77 68 69 63 68 20 69 73 0a 2a 2a 20  LOB which is.** 
264d5 6a 75 73 74 20 61 20 76 61 72 69 61 74 69 6f 6e  just a variation
264d6 20 6f 66 20 4c 49 4b 45 29 20 67 65 74 73 20 63   of LIKE) gets c
264d7 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 73 20  alled.  This is 
264d8 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
264d9 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66  .** only..*/.#if
264da 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
264db 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
264dc 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f 75 6e  qlite3_like_coun
264dd 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a  t = 0;.#endif...
264de 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
264df 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b 65  tion of the like
264e0 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  () SQL function.
264e1 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20    This function 
264e2 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68  implements.** th
264e3 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45 20  e build-in LIKE 
264e4 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 66  operator.  The f
264e5 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
264e6 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   the function is
264e7 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e 20   the.** pattern 
264e8 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61  and the second a
264e9 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73  rgument is the s
264ea 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65 20  tring.  So, the 
264eb 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a  SQL statements:.
264ec 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c 49  **.**       A LI
264ed 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d  KE B.**.** is im
264ee 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69 6b  plemented as lik
264ef 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  e(B,A)..**.** Th
264f0 69 73 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e  is same function
264f1 20 28 77 69 74 68 20 61 20 64 69 66 66 65 72 65   (with a differe
264f2 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 73  nt compareInfo s
264f3 74 72 75 63 74 75 72 65 29 20 63 6f 6d 70 75 74  tructure) comput
264f4 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f 42 20 6f  es.** the GLOB o
264f5 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74  perator..*/.stat
264f6 69 63 20 76 6f 69 64 20 6c 69 6b 65 46 75 6e 63  ic void likeFunc
264f7 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
264f8 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20  ext *context, . 
264f9 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 71   int argc, .  sq
264fa 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
264fb 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e  gv.){.  const un
264fc 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 2c  signed char *zA,
264fd 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65 73 63 61   *zB;.  int esca
264fe 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 50  pe = 0;.  int nP
264ff 61 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  at;.  sqlite3 *d
26500 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  b = sqlite3_cont
26501 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f  ext_db_handle(co
26502 6e 74 65 78 74 29 3b 0a 0a 20 20 7a 42 20 3d 20  ntext);..  zB = 
26503 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
26504 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a  xt(argv[0]);.  z
26505 41 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  A = sqlite3_valu
26506 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
26507 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68 65  ..  /* Limit the
26508 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 4c   length of the L
26509 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74  IKE or GLOB patt
2650a 65 72 6e 20 74 6f 20 61 76 6f 69 64 20 70 72 6f  ern to avoid pro
2650b 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20 64 65  blems.  ** of de
2650c 65 70 20 72 65 63 75 72 73 69 6f 6e 20 61 6e 64  ep recursion and
2650d 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20 69 6e   N*N behavior in
2650e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28   patternCompare(
2650f 29 2e 0a 20 20 2a 2f 0a 20 20 6e 50 61 74 20 3d  )..  */.  nPat =
26510 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
26511 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20  ytes(argv[0]);. 
26512 20 74 65 73 74 63 61 73 65 28 20 6e 50 61 74 3d   testcase( nPat=
26513 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  =db->aLimit[SQLI
26514 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50 41  TE_LIMIT_LIKE_PA
26515 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 20 29 3b  TTERN_LENGTH] );
26516 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 61  .  testcase( nPa
26517 74 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  t==db->aLimit[SQ
26518 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f  LITE_LIMIT_LIKE_
26519 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 2b  PATTERN_LENGTH]+
2651a 31 20 29 3b 0a 20 20 69 66 28 20 6e 50 61 74 20  1 );.  if( nPat 
2651b 3e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  > db->aLimit[SQL
2651c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f 50  ITE_LIMIT_LIKE_P
2651d 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 20 29  ATTERN_LENGTH] )
2651e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
2651f 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65  sult_error(conte
26520 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47 4c 4f  xt, "LIKE or GLO
26521 42 20 70 61 74 74 65 72 6e 20 74 6f 6f 20 63 6f  B pattern too co
26522 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a 20 20 20  mplex", -1);.   
26523 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61   return;.  }.  a
26524 73 73 65 72 74 28 20 7a 42 3d 3d 73 71 6c 69 74  ssert( zB==sqlit
26525 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
26526 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 45 6e  gv[0]) );  /* En
26527 63 6f 64 69 6e 67 20 64 69 64 20 6e 6f 74 20 63  coding did not c
26528 68 61 6e 67 65 20 2a 2f 0a 0a 20 20 69 66 28 20  hange */..  if( 
26529 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 2f  argc==3 ){.    /
2652a 2a 20 54 68 65 20 65 73 63 61 70 65 20 63 68 61  * The escape cha
2652b 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 6d 75  racter string mu
2652c 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 61 20  st consist of a 
2652d 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61  single UTF-8 cha
2652e 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20 4f  racter..    ** O
2652f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e  therwise, return
26530 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a   an error..    *
26531 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69  /.    const unsi
26532 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 73 63 20  gned char *zEsc 
26533 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
26534 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20  text(argv[2]);. 
26535 20 20 20 69 66 28 20 7a 45 73 63 3d 3d 30 20 29     if( zEsc==0 )
26536 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28   return;.    if(
26537 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61 72   sqlite3Utf8Char
26538 4c 65 6e 28 28 63 68 61 72 2a 29 7a 45 73 63 2c  Len((char*)zEsc,
26539 20 2d 31 29 21 3d 31 20 29 7b 0a 20 20 20 20 20   -1)!=1 ){.     
2653a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2653b 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 0a  error(context, .
2653c 20 20 20 20 20 20 20 20 20 20 22 45 53 43 41 50            "ESCAP
2653d 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75 73  E expression mus
2653e 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63 68  t be a single ch
2653f 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b 0a 20  aracter", -1);. 
26540 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
26541 20 7d 0a 20 20 20 20 65 73 63 61 70 65 20 3d 20   }.    escape = 
26542 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28  sqlite3Utf8Read(
26543 7a 45 73 63 2c 20 26 7a 45 73 63 29 3b 0a 20 20  zEsc, &zEsc);.  
26544 7d 0a 20 20 69 66 28 20 7a 41 20 26 26 20 7a 42  }.  if( zA && zB
26545 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 63   ){.    struct c
26546 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66  ompareInfo *pInf
26547 6f 20 3d 20 73 71 6c 69 74 65 33 5f 75 73 65 72  o = sqlite3_user
26548 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a  _data(context);.
26549 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
2654a 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6c  ST.    sqlite3_l
2654b 69 6b 65 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e  ike_count++;.#en
2654c 64 69 66 0a 20 20 20 20 0a 20 20 20 20 73 71 6c  dif.    .    sql
2654d 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
2654e 63 6f 6e 74 65 78 74 2c 20 70 61 74 74 65 72 6e  context, pattern
2654f 43 6f 6d 70 61 72 65 28 7a 42 2c 20 7a 41 2c 20  Compare(zB, zA, 
26550 70 49 6e 66 6f 2c 20 65 73 63 61 70 65 29 29 3b  pInfo, escape));
26551 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  .  }.}../*.** Im
26552 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
26553 74 68 65 20 4e 55 4c 4c 49 46 28 78 2c 79 29 20  the NULLIF(x,y) 
26554 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72  function.  The r
26555 65 73 75 6c 74 20 69 73 20 74 68 65 20 66 69 72  esult is the fir
26556 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69  st.** argument i
26557 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 20  f the arguments 
26558 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e 20 20  are different.  
26559 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  The result is NU
2655a 4c 4c 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67  LL if the.** arg
2655b 75 6d 65 6e 74 73 20 61 72 65 20 65 71 75 61 6c  uments are equal
2655c 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 2e 0a   to each other..
2655d 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
2655e 75 6c 6c 69 66 46 75 6e 63 28 0a 20 20 73 71 6c  ullifFunc(.  sql
2655f 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
26560 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
26561 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
26562 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
26563 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
26564 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e   = sqlite3GetFun
26565 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74  cCollSeq(context
26566 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
26567 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
26568 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d 65 6d    if( sqlite3Mem
26569 43 6f 6d 70 61 72 65 28 61 72 67 76 5b 30 5d 2c  Compare(argv[0],
2656a 20 61 72 67 76 5b 31 5d 2c 20 70 43 6f 6c 6c 29   argv[1], pColl)
2656b 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  !=0 ){.    sqlit
2656c 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
2656d 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d  context, argv[0]
2656e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2656f 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
26570 66 20 74 68 65 20 73 71 6c 69 74 65 5f 76 65 72  f the sqlite_ver
26571 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 2e  sion() function.
26572 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20    The result is 
26573 74 68 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f  the version.** o
26574 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  f the SQLite lib
26575 72 61 72 79 20 74 68 61 74 20 69 73 20 72 75 6e  rary that is run
26576 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ning..*/.static 
26577 76 6f 69 64 20 76 65 72 73 69 6f 6e 46 75 6e 63  void versionFunc
26578 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
26579 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
2657a 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
2657b 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
2657c 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55  otUsed2.){.  UNU
2657d 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
2657e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
2657f 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  );.  sqlite3_res
26580 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
26581 2c 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  , sqlite3_versio
26582 6e 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54  n, -1, SQLITE_ST
26583 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ATIC);.}../*.** 
26584 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
26585 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 6f 75  f the sqlite_sou
26586 72 63 65 5f 69 64 28 29 20 66 75 6e 63 74 69 6f  rce_id() functio
26587 6e 2e 20 54 68 65 20 72 65 73 75 6c 74 20 69 73  n. The result is
26588 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61   a string.** tha
26589 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65  t identifies the
2658a 20 70 61 72 74 69 63 75 6c 61 72 20 76 65 72 73   particular vers
2658b 69 6f 6e 20 6f 66 20 74 68 65 20 73 6f 75 72 63  ion of the sourc
2658c 65 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 62  e code used to b
2658d 75 69 6c 64 0a 2a 2a 20 53 51 4c 69 74 65 2e 0a  uild.** SQLite..
2658e 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
2658f 6f 75 72 63 65 69 64 46 75 6e 63 28 0a 20 20 73  ourceidFunc(.  s
26590 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
26591 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
26592 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
26593 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65  3_value **NotUse
26594 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  d2.){.  UNUSED_P
26595 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
26596 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20  d, NotUsed2);.  
26597 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
26598 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 53 51 4c  ext(context, SQL
26599 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 2c 20 2d  ITE_SOURCE_ID, -
2659a 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
2659b 29 3b 0a 7d 0a 0a 2f 2a 20 41 72 72 61 79 20 66  );.}../* Array f
2659c 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20 66 72  or converting fr
2659d 6f 6d 20 68 61 6c 66 2d 62 79 74 65 73 20 28 6e  om half-bytes (n
2659e 79 62 62 6c 65 73 29 20 69 6e 74 6f 20 41 53 43  ybbles) into ASC
2659f 49 49 20 68 65 78 0a 2a 2a 20 64 69 67 69 74 73  II hex.** digits
265a0 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  . */.static cons
265a1 74 20 63 68 61 72 20 68 65 78 64 69 67 69 74 73  t char hexdigits
265a2 5b 5d 20 3d 20 7b 0a 20 20 27 30 27 2c 20 27 31  [] = {.  '0', '1
265a3 27 2c 20 27 32 27 2c 20 27 33 27 2c 20 27 34 27  ', '2', '3', '4'
265a4 2c 20 27 35 27 2c 20 27 36 27 2c 20 27 37 27 2c  , '5', '6', '7',
265a5 0a 20 20 27 38 27 2c 20 27 39 27 2c 20 27 41 27  .  '8', '9', 'A'
265a6 2c 20 27 42 27 2c 20 27 43 27 2c 20 27 44 27 2c  , 'B', 'C', 'D',
265a7 20 27 45 27 2c 20 27 46 27 20 0a 7d 3b 0a 0a 2f   'E', 'F' .};../
265a8 2a 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  *.** EXPERIMENTA
265a9 4c 20 2d 20 54 68 69 73 20 69 73 20 6e 6f 74 20  L - This is not 
265aa 61 6e 20 6f 66 66 69 63 69 61 6c 20 66 75 6e 63  an official func
265ab 74 69 6f 6e 2e 20 20 54 68 65 20 69 6e 74 65 72  tion.  The inter
265ac 66 61 63 65 20 6d 61 79 0a 2a 2a 20 63 68 61 6e  face may.** chan
265ad 67 65 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69  ge.  This functi
265ae 6f 6e 20 6d 61 79 20 64 69 73 61 70 70 65 61 72  on may disappear
265af 2e 20 20 44 6f 20 6e 6f 74 20 77 72 69 74 65 20  .  Do not write 
265b0 63 6f 64 65 20 74 68 61 74 20 64 65 70 65 6e 64  code that depend
265b1 73 0a 2a 2a 20 6f 6e 20 74 68 69 73 20 66 75 6e  s.** on this fun
265b2 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70  ction..**.** Imp
265b3 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
265b4 68 65 20 51 55 4f 54 45 28 29 20 66 75 6e 63 74  he QUOTE() funct
265b5 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74  ion.  This funct
265b6 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
265b7 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 20  le.** argument. 
265b8 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   If the argument
265b9 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 74 68 65   is numeric, the
265ba 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
265bb 20 74 68 65 20 73 61 6d 65 20 61 73 0a 2a 2a 20   the same as.** 
265bc 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 20 20 49  the argument.  I
265bd 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69  f the argument i
265be 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 74 75  s NULL, the retu
265bf 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  rn value is the 
265c0 73 74 72 69 6e 67 0a 2a 2a 20 22 4e 55 4c 4c 22  string.** "NULL"
265c1 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68  .  Otherwise, th
265c2 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 65 6e  e argument is en
265c3 63 6c 6f 73 65 64 20 69 6e 20 73 69 6e 67 6c 65  closed in single
265c4 20 71 75 6f 74 65 73 20 77 69 74 68 0a 2a 2a 20   quotes with.** 
265c5 73 69 6e 67 6c 65 2d 71 75 6f 74 65 20 65 73 63  single-quote esc
265c6 61 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  apes..*/.static 
265c7 76 6f 69 64 20 71 75 6f 74 65 46 75 6e 63 28 73  void quoteFunc(s
265c8 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
265c9 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67  context, int arg
265ca 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  c, sqlite3_value
265cb 20 2a 2a 61 72 67 76 29 7b 0a 20 20 61 73 73 65   **argv){.  asse
265cc 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20  rt( argc==1 );. 
265cd 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
265ce 52 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63  R(argc);.  switc
265cf 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  h( sqlite3_value
265d0 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29  _type(argv[0]) )
265d1 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
265d2 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63  E_INTEGER:.    c
265d3 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  ase SQLITE_FLOAT
265d4 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
265d5 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63  3_result_value(c
265d6 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29  ontext, argv[0])
265d7 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
265d8 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
265d9 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20  LITE_BLOB: {.   
265da 20 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20 3d     char *zText =
265db 20 30 3b 0a 20 20 20 20 20 20 63 68 61 72 20 63   0;.      char c
265dc 6f 6e 73 74 20 2a 7a 42 6c 6f 62 20 3d 20 73 71  onst *zBlob = sq
265dd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
265de 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20  (argv[0]);.     
265df 20 69 6e 74 20 6e 42 6c 6f 62 20 3d 20 73 71 6c   int nBlob = sql
265e0 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
265e1 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20  (argv[0]);.     
265e2 20 61 73 73 65 72 74 28 20 7a 42 6c 6f 62 3d 3d   assert( zBlob==
265e3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
265e4 6f 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 2f  ob(argv[0]) ); /
265e5 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68  * No encoding ch
265e6 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54  ange */.      zT
265e7 65 78 74 20 3d 20 28 63 68 61 72 20 2a 29 63 6f  ext = (char *)co
265e8 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74  ntextMalloc(cont
265e9 65 78 74 2c 20 28 32 2a 28 69 36 34 29 6e 42 6c  ext, (2*(i64)nBl
265ea 6f 62 29 2b 34 29 3b 20 0a 20 20 20 20 20 20 69  ob)+4); .      i
265eb 66 28 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20  f( zText ){.    
265ec 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
265ed 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 42     for(i=0; i<nB
265ee 6c 6f 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  lob; i++){.     
265ef 20 20 20 20 20 7a 54 65 78 74 5b 28 69 2a 32 29       zText[(i*2)
265f0 2b 32 5d 20 3d 20 68 65 78 64 69 67 69 74 73 5b  +2] = hexdigits[
265f1 28 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 29 26 30 78  (zBlob[i]>>4)&0x
265f2 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  0F];.          z
265f3 54 65 78 74 5b 28 69 2a 32 29 2b 33 5d 20 3d 20  Text[(i*2)+3] = 
265f4 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c 6f 62  hexdigits[(zBlob
265f5 5b 69 5d 29 26 30 78 30 46 5d 3b 0a 20 20 20 20  [i])&0x0F];.    
265f6 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a 54      }.        zT
265f7 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d  ext[(nBlob*2)+2]
265f8 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20   = '\'';.       
265f9 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29   zText[(nBlob*2)
265fa 2b 33 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  +3] = '\0';.    
265fb 20 20 20 20 7a 54 65 78 74 5b 30 5d 20 3d 20 27      zText[0] = '
265fc 58 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 78  X';.        zTex
265fd 74 5b 31 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20  t[1] = '\'';.   
265fe 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
265ff 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
26600 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 53 51 4c  , zText, -1, SQL
26601 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
26602 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
26603 66 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20 20  free(zText);.   
26604 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
26605 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
26606 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 7b 0a   SQLITE_TEXT: {.
26607 20 20 20 20 20 20 69 6e 74 20 69 2c 6a 3b 0a 20        int i,j;. 
26608 20 20 20 20 20 75 36 34 20 6e 3b 0a 20 20 20 20       u64 n;.    
26609 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
2660a 20 63 68 61 72 20 2a 7a 41 72 67 20 3d 20 73 71   char *zArg = sq
2660b 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2660c 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20  (argv[0]);.     
2660d 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20 20 20   char *z;..     
2660e 20 69 66 28 20 7a 41 72 67 3d 3d 30 20 29 20 72   if( zArg==0 ) r
2660f 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 66 6f 72  eturn;.      for
26610 28 69 3d 30 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b  (i=0, n=0; zArg[
26611 69 5d 3b 20 69 2b 2b 29 7b 20 69 66 28 20 7a 41  i]; i++){ if( zA
26612 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b  rg[i]=='\'' ) n+
26613 2b 3b 20 7d 0a 20 20 20 20 20 20 7a 20 3d 20 63  +; }.      z = c
26614 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e  ontextMalloc(con
26615 74 65 78 74 2c 20 28 28 69 36 34 29 69 29 2b 28  text, ((i64)i)+(
26616 28 69 36 34 29 6e 29 2b 33 29 3b 0a 20 20 20 20  (i64)n)+3);.    
26617 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20    if( z ){.     
26618 20 20 20 7a 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a     z[0] = '\'';.
26619 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c          for(i=0,
2661a 20 6a 3d 31 3b 20 7a 41 72 67 5b 69 5d 3b 20 69   j=1; zArg[i]; i
2661b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  ++){.          z
2661c 5b 6a 2b 2b 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b  [j++] = zArg[i];
2661d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a  .          if( z
2661e 41 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a  Arg[i]=='\'' ){.
2661f 20 20 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b              z[j+
26620 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20  +] = '\'';.     
26621 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
26622 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20  .        z[j++] 
26623 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20  = '\'';.        
26624 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  z[j] = 0;.      
26625 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
26626 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
26627 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66 72 65  , j, sqlite3_fre
26628 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
26629 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2662a 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
2662b 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
2662c 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
2662d 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[0])==SQLITE_
2662e 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 73 71  NULL );.      sq
2662f 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
26630 74 28 63 6f 6e 74 65 78 74 2c 20 22 4e 55 4c 4c  t(context, "NULL
26631 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41  ", 4, SQLITE_STA
26632 54 49 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61  TIC);.      brea
26633 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  k;.    }.  }.}..
26634 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 78 28 29 20  /*.** The hex() 
26635 66 75 6e 63 74 69 6f 6e 2e 20 20 49 6e 74 65 72  function.  Inter
26636 70 72 65 74 20 74 68 65 20 61 72 67 75 6d 65 6e  pret the argumen
26637 74 20 61 73 20 61 20 62 6c 6f 62 2e 20 20 52 65  t as a blob.  Re
26638 74 75 72 6e 0a 2a 2a 20 61 20 68 65 78 61 64 65  turn.** a hexade
26639 63 69 6d 61 6c 20 72 65 6e 64 65 72 69 6e 67 20  cimal rendering 
2663a 61 73 20 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74  as text..*/.stat
2663b 69 63 20 76 6f 69 64 20 68 65 78 46 75 6e 63 28  ic void hexFunc(
2663c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
2663d 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
2663e 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
2663f 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
26640 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20  ){.  int i, n;. 
26641 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
26642 63 68 61 72 20 2a 70 42 6c 6f 62 3b 0a 20 20 63  char *pBlob;.  c
26643 68 61 72 20 2a 7a 48 65 78 2c 20 2a 7a 3b 0a 20  har *zHex, *z;. 
26644 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31   assert( argc==1
26645 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   );.  UNUSED_PAR
26646 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
26647 70 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f  pBlob = sqlite3_
26648 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b  value_blob(argv[
26649 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74  0]);.  n = sqlit
2664a 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
2664b 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72  rgv[0]);.  asser
2664c 74 28 20 70 42 6c 6f 62 3d 3d 73 71 6c 69 74 65  t( pBlob==sqlite
2664d 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
2664e 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f 20  v[0]) );  /* No 
2664f 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65 20  encoding change 
26650 2a 2f 0a 20 20 7a 20 3d 20 7a 48 65 78 20 3d 20  */.  z = zHex = 
26651 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
26652 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2a  ntext, ((i64)n)*
26653 32 20 2b 20 31 29 3b 0a 20 20 69 66 28 20 7a 48  2 + 1);.  if( zH
26654 65 78 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  ex ){.    for(i=
26655 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70 42 6c  0; i<n; i++, pBl
26656 6f 62 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e 73  ob++){.      uns
26657 69 67 6e 65 64 20 63 68 61 72 20 63 20 3d 20 2a  igned char c = *
26658 70 42 6c 6f 62 3b 0a 20 20 20 20 20 20 2a 28 7a  pBlob;.      *(z
26659 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74 73 5b  ++) = hexdigits[
2665a 28 63 3e 3e 34 29 26 30 78 66 5d 3b 0a 20 20 20  (c>>4)&0xf];.   
2665b 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64     *(z++) = hexd
2665c 69 67 69 74 73 5b 63 26 30 78 66 5d 3b 0a 20 20  igits[c&0xf];.  
2665d 20 20 7d 0a 20 20 20 20 2a 7a 20 3d 20 30 3b 0a    }.    *z = 0;.
2665e 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
2665f 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
26660 20 7a 48 65 78 2c 20 6e 2a 32 2c 20 73 71 6c 69   zHex, n*2, sqli
26661 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d  te3_free);.  }.}
26662 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 7a 65 72 6f  ../*.** The zero
26663 62 6c 6f 62 28 4e 29 20 66 75 6e 63 74 69 6f 6e  blob(N) function
26664 20 72 65 74 75 72 6e 73 20 61 20 7a 65 72 6f 2d   returns a zero-
26665 66 69 6c 6c 65 64 20 62 6c 6f 62 20 6f 66 20 73  filled blob of s
26666 69 7a 65 20 4e 20 62 79 74 65 73 2e 0a 2a 2f 0a  ize N bytes..*/.
26667 73 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f  static void zero
26668 62 6c 6f 62 46 75 6e 63 28 0a 20 20 73 71 6c 69  blobFunc(.  sqli
26669 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
2666a 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
2666b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
2666c 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 36  e **argv.){.  i6
2666d 34 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  4 n;.  sqlite3 *
2666e 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
2666f 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
26670 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65 72  ontext);.  asser
26671 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20  t( argc==1 );.  
26672 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
26673 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73 71  (argc);.  n = sq
26674 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
26675 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 74 65  4(argv[0]);.  te
26676 73 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d 3e 61  stcase( n==db->a
26677 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
26678 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20  IT_LENGTH] );.  
26679 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d  testcase( n==db-
2667a 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
2667b 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31 20 29  IMIT_LENGTH]+1 )
2667c 3b 0a 20 20 69 66 28 20 6e 3e 64 62 2d 3e 61 4c  ;.  if( n>db->aL
2667d 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
2667e 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
2667f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
26680 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e  error_toobig(con
26681 74 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  text);.  }else{.
26682 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
26683 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e 74  lt_zeroblob(cont
26684 65 78 74 2c 20 28 69 6e 74 29 6e 29 3b 0a 20 20  ext, (int)n);.  
26685 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72  }.}../*.** The r
26686 65 70 6c 61 63 65 28 29 20 66 75 6e 63 74 69 6f  eplace() functio
26687 6e 2e 20 20 54 68 72 65 65 20 61 72 67 75 6d 65  n.  Three argume
26688 6e 74 73 20 61 72 65 20 61 6c 6c 20 73 74 72 69  nts are all stri
26689 6e 67 73 3a 20 63 61 6c 6c 0a 2a 2a 20 74 68 65  ngs: call.** the
2668a 6d 20 41 2c 20 42 2c 20 61 6e 64 20 43 2e 20 54  m A, B, and C. T
2668b 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 73  he result is als
2668c 6f 20 61 20 73 74 72 69 6e 67 20 77 68 69 63 68  o a string which
2668d 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20 66   is derived.** f
2668e 72 6f 6d 20 41 20 62 79 20 72 65 70 6c 61 63 69  rom A by replaci
2668f 6e 67 20 65 76 65 72 79 20 6f 63 63 75 72 61 6e  ng every occuran
26690 63 65 20 6f 66 20 42 20 77 69 74 68 20 43 2e 20  ce of B with C. 
26691 20 54 68 65 20 6d 61 74 63 68 0a 2a 2a 20 6d 75   The match.** mu
26692 73 74 20 62 65 20 65 78 61 63 74 2e 20 20 43 6f  st be exact.  Co
26693 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
26694 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64 2e 0a  s are not used..
26695 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
26696 65 70 6c 61 63 65 46 75 6e 63 28 0a 20 20 73 71  eplaceFunc(.  sq
26697 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
26698 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
26699 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
2669a 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
2669b 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2669c 68 61 72 20 2a 7a 53 74 72 3b 20 20 20 20 20 20  har *zStr;      
2669d 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 74 20 73    /* The input s
2669e 74 72 69 6e 67 20 41 20 2a 2f 0a 20 20 63 6f 6e  tring A */.  con
2669f 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
266a0 20 2a 7a 50 61 74 74 65 72 6e 3b 20 20 20 20 2f   *zPattern;    /
266a1 2a 20 54 68 65 20 70 61 74 74 65 72 6e 20 73 74  * The pattern st
266a2 72 69 6e 67 20 42 20 2a 2f 0a 20 20 63 6f 6e 73  ring B */.  cons
266a3 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
266a4 2a 7a 52 65 70 3b 20 20 20 20 20 20 20 20 2f 2a  *zRep;        /*
266a5 20 54 68 65 20 72 65 70 6c 61 63 65 6d 65 6e 74   The replacement
266a6 20 73 74 72 69 6e 67 20 43 20 2a 2f 0a 20 20 75   string C */.  u
266a7 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f  nsigned char *zO
266a8 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
266a9 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20 2a   /* The output *
266aa 2f 0a 20 20 69 6e 74 20 6e 53 74 72 3b 20 20 20  /.  int nStr;   
266ab 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
266ac 53 69 7a 65 20 6f 66 20 7a 53 74 72 20 2a 2f 0a  Size of zStr */.
266ad 20 20 69 6e 74 20 6e 50 61 74 74 65 72 6e 3b 20    int nPattern; 
266ae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
266af 7a 65 20 6f 66 20 7a 50 61 74 74 65 72 6e 20 2a  ze of zPattern *
266b0 2f 0a 20 20 69 6e 74 20 6e 52 65 70 3b 20 20 20  /.  int nRep;   
266b1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
266b2 53 69 7a 65 20 6f 66 20 7a 52 65 70 20 2a 2f 0a  Size of zRep */.
266b3 20 20 69 36 34 20 6e 4f 75 74 3b 20 20 20 20 20    i64 nOut;     
266b4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
266b5 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 7a 4f  ximum size of zO
266b6 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 6f 70  ut */.  int loop
266b7 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20  Limit;          
266b8 20 2f 2a 20 4c 61 73 74 20 7a 53 74 72 5b 5d 20   /* Last zStr[] 
266b9 74 68 61 74 20 6d 69 67 68 74 20 6d 61 74 63 68  that might match
266ba 20 7a 50 61 74 74 65 72 6e 5b 5d 20 2a 2f 0a 20   zPattern[] */. 
266bb 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
266bc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
266bd 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 0a 20  p counters */.. 
266be 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 33   assert( argc==3
266bf 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   );.  UNUSED_PAR
266c0 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
266c1 7a 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 76  zStr = sqlite3_v
266c2 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
266c3 5d 29 3b 0a 20 20 69 66 28 20 7a 53 74 72 3d 3d  ]);.  if( zStr==
266c4 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 53  0 ) return;.  nS
266c5 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  tr = sqlite3_val
266c6 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
266c7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 74  );.  assert( zSt
266c8 72 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r==sqlite3_value
266c9 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29  _text(argv[0]) )
266ca 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e  ;  /* No encodin
266cb 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 50  g change */.  zP
266cc 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 33  attern = sqlite3
266cd 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
266ce 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 50 61 74  [1]);.  if( zPat
266cf 74 65 72 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61  tern==0 ){.    a
266d0 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76  ssert( sqlite3_v
266d1 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31  alue_type(argv[1
266d2 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 0a  ])==SQLITE_NULL.
266d3 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 73              || s
266d4 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
266d5 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
266d6 29 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  )->mallocFailed 
266d7 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
266d8 20 7d 0a 20 20 69 66 28 20 7a 50 61 74 74 65 72   }.  if( zPatter
266d9 6e 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 61  n[0]==0 ){.    a
266da 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 76  ssert( sqlite3_v
266db 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31  alue_type(argv[1
266dc 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])!=SQLITE_NULL 
266dd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
266de 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74  esult_value(cont
266df 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20  ext, argv[0]);. 
266e0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
266e1 20 6e 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69   nPattern = sqli
266e2 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
266e3 61 72 67 76 5b 31 5d 29 3b 0a 20 20 61 73 73 65  argv[1]);.  asse
266e4 72 74 28 20 7a 50 61 74 74 65 72 6e 3d 3d 73 71  rt( zPattern==sq
266e5 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
266e6 28 61 72 67 76 5b 31 5d 29 20 29 3b 20 20 2f 2a  (argv[1]) );  /*
266e7 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61   No encoding cha
266e8 6e 67 65 20 2a 2f 0a 20 20 7a 52 65 70 20 3d 20  nge */.  zRep = 
266e9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
266ea 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69  xt(argv[2]);.  i
266eb 66 28 20 7a 52 65 70 3d 3d 30 20 29 20 72 65 74  f( zRep==0 ) ret
266ec 75 72 6e 3b 0a 20 20 6e 52 65 70 20 3d 20 73 71  urn;.  nRep = sq
266ed 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
266ee 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 61 73  s(argv[2]);.  as
266ef 73 65 72 74 28 20 7a 52 65 70 3d 3d 73 71 6c 69  sert( zRep==sqli
266f0 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
266f1 72 67 76 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f 75  rgv[2]) );.  nOu
266f2 74 20 3d 20 6e 53 74 72 20 2b 20 31 3b 0a 20 20  t = nStr + 1;.  
266f3 61 73 73 65 72 74 28 20 6e 4f 75 74 3c 53 51 4c  assert( nOut<SQL
266f4 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29  ITE_MAX_LENGTH )
266f5 3b 0a 20 20 7a 4f 75 74 20 3d 20 63 6f 6e 74 65  ;.  zOut = conte
266f6 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74  xtMalloc(context
266f7 2c 20 28 69 36 34 29 6e 4f 75 74 29 3b 0a 20 20  , (i64)nOut);.  
266f8 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20  if( zOut==0 ){. 
266f9 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
266fa 20 6c 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74   loopLimit = nSt
266fb 72 20 2d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0a  r - nPattern;  .
266fc 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d    for(i=j=0; i<=
266fd 6c 6f 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b  loopLimit; i++){
266fe 0a 20 20 20 20 69 66 28 20 7a 53 74 72 5b 69 5d  .    if( zStr[i]
266ff 21 3d 7a 50 61 74 74 65 72 6e 5b 30 5d 20 7c 7c  !=zPattern[0] ||
26700 20 6d 65 6d 63 6d 70 28 26 7a 53 74 72 5b 69 5d   memcmp(&zStr[i]
26701 2c 20 7a 50 61 74 74 65 72 6e 2c 20 6e 50 61 74  , zPattern, nPat
26702 74 65 72 6e 29 20 29 7b 0a 20 20 20 20 20 20 7a  tern) ){.      z
26703 4f 75 74 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b  Out[j++] = zStr[
26704 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  i];.    }else{. 
26705 20 20 20 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20       u8 *zOld;. 
26706 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62       sqlite3 *db
26707 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
26708 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
26709 74 65 78 74 29 3b 0a 20 20 20 20 20 20 6e 4f 75  text);.      nOu
2670a 74 20 2b 3d 20 6e 52 65 70 20 2d 20 6e 50 61 74  t += nRep - nPat
2670b 74 65 72 6e 3b 0a 20 20 20 20 20 20 74 65 73 74  tern;.      test
2670c 63 61 73 65 28 20 6e 4f 75 74 2d 31 3d 3d 64 62  case( nOut-1==db
2670d 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
2670e 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b  LIMIT_LENGTH] );
2670f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
26710 20 6e 4f 75 74 2d 32 3d 3d 64 62 2d 3e 61 4c 69   nOut-2==db->aLi
26711 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
26712 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20 20 20  _LENGTH] );.    
26713 20 20 69 66 28 20 6e 4f 75 74 2d 31 3e 64 62 2d    if( nOut-1>db-
26714 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
26715 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
26716 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
26717 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f  result_error_too
26718 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  big(context);.  
26719 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2671a 72 65 65 28 64 62 2c 20 7a 4f 75 74 29 3b 0a 20  ree(db, zOut);. 
2671b 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
2671c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 4f 6c       }.      zOl
2671d 64 20 3d 20 7a 4f 75 74 3b 0a 20 20 20 20 20 20  d = zOut;.      
2671e 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 72  zOut = sqlite3_r
2671f 65 61 6c 6c 6f 63 28 7a 4f 75 74 2c 20 28 69 6e  ealloc(zOut, (in
26720 74 29 6e 4f 75 74 29 3b 0a 20 20 20 20 20 20 69  t)nOut);.      i
26721 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20  f( zOut==0 ){.  
26722 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
26723 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
26724 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20  (context);.     
26725 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
26726 28 64 62 2c 20 7a 4f 6c 64 29 3b 0a 20 20 20 20  (db, zOld);.    
26727 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
26728 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63 70 79    }.      memcpy
26729 28 26 7a 4f 75 74 5b 6a 5d 2c 20 7a 52 65 70 2c  (&zOut[j], zRep,
2672a 20 6e 52 65 70 29 3b 0a 20 20 20 20 20 20 6a 20   nRep);.      j 
2672b 2b 3d 20 6e 52 65 70 3b 0a 20 20 20 20 20 20 69  += nRep;.      i
2672c 20 2b 3d 20 6e 50 61 74 74 65 72 6e 2d 31 3b 0a   += nPattern-1;.
2672d 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65      }.  }.  asse
2672e 72 74 28 20 6a 2b 6e 53 74 72 2d 69 2b 31 3d 3d  rt( j+nStr-i+1==
2672f 6e 4f 75 74 20 29 3b 0a 20 20 6d 65 6d 63 70 79  nOut );.  memcpy
26730 28 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53 74 72  (&zOut[j], &zStr
26731 5b 69 5d 2c 20 6e 53 74 72 2d 69 29 3b 0a 20 20  [i], nStr-i);.  
26732 6a 20 2b 3d 20 6e 53 74 72 20 2d 20 69 3b 0a 20  j += nStr - i;. 
26733 20 61 73 73 65 72 74 28 20 6a 3c 3d 6e 4f 75 74   assert( j<=nOut
26734 20 29 3b 0a 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20   );.  zOut[j] = 
26735 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  0;.  sqlite3_res
26736 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
26737 2c 20 28 63 68 61 72 2a 29 7a 4f 75 74 2c 20 6a  , (char*)zOut, j
26738 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b  , sqlite3_free);
26739 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
2673a 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
2673b 54 52 49 4d 28 29 2c 20 4c 54 52 49 4d 28 29 2c  TRIM(), LTRIM(),
2673c 20 61 6e 64 20 52 54 52 49 4d 28 29 20 66 75 6e   and RTRIM() fun
2673d 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 75  ctions..** The u
2673e 73 65 72 64 61 74 61 20 69 73 20 30 78 31 20 66  serdata is 0x1 f
2673f 6f 72 20 6c 65 66 74 20 74 72 69 6d 2c 20 30 78  or left trim, 0x
26740 32 20 66 6f 72 20 72 69 67 68 74 20 74 72 69 6d  2 for right trim
26741 2c 20 30 78 33 20 66 6f 72 20 62 6f 74 68 2e 0a  , 0x3 for both..
26742 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74  */.static void t
26743 72 69 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  rimFunc(.  sqlit
26744 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
26745 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
26746 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
26747 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e   **argv.){.  con
26748 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
26749 20 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 2f   *zIn;         /
2674a 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20 2a  * Input string *
2674b 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  /.  const unsign
2674c 65 64 20 63 68 61 72 20 2a 7a 43 68 61 72 53 65  ed char *zCharSe
2674d 74 3b 20 20 20 20 2f 2a 20 53 65 74 20 6f 66 20  t;    /* Set of 
2674e 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 74 72  characters to tr
2674f 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 3b  im */.  int nIn;
26750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26751 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
26752 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
26753 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 66  input */.  int f
26754 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
26755 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26756 31 3a 20 74 72 69 6d 6c 65 66 74 20 20 32 3a 20  1: trimleft  2: 
26757 74 72 69 6d 72 69 67 68 74 20 20 33 3a 20 74 72  trimright  3: tr
26758 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20  im */.  int i;  
26759 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2675a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
2675b 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75  p counter */.  u
2675c 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 4c  nsigned char *aL
2675d 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  en = 0;         
2675e 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 65 61   /* Length of ea
2675f 63 68 20 63 68 61 72 61 63 74 65 72 20 69 6e 20  ch character in 
26760 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20 75 6e  zCharSet */.  un
26761 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a 61 7a  signed char **az
26762 43 68 61 72 20 3d 20 30 3b 20 20 20 20 20 20 20  Char = 0;       
26763 2f 2a 20 49 6e 64 69 76 69 64 75 61 6c 20 63 68  /* Individual ch
26764 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68 61  aracters in zCha
26765 72 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  rSet */.  int nC
26766 68 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  har;            
26767 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
26768 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
26769 65 72 73 20 69 6e 20 7a 43 68 61 72 53 65 74 20  ers in zCharSet 
2676a 2a 2f 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  */..  if( sqlite
2676b 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
2676c 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  v[0])==SQLITE_NU
2676d 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  LL ){.    return
2676e 3b 0a 20 20 7d 0a 20 20 7a 49 6e 20 3d 20 73 71  ;.  }.  zIn = sq
2676f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
26770 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28  (argv[0]);.  if(
26771 20 7a 49 6e 3d 3d 30 20 29 20 72 65 74 75 72 6e   zIn==0 ) return
26772 3b 0a 20 20 6e 49 6e 20 3d 20 73 71 6c 69 74 65  ;.  nIn = sqlite
26773 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72  3_value_bytes(ar
26774 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65 72 74  gv[0]);.  assert
26775 28 20 7a 49 6e 3d 3d 73 71 6c 69 74 65 33 5f 76  ( zIn==sqlite3_v
26776 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
26777 5d 29 20 29 3b 0a 20 20 69 66 28 20 61 72 67 63  ]) );.  if( argc
26778 3d 3d 31 20 29 7b 0a 20 20 20 20 73 74 61 74 69  ==1 ){.    stati
26779 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  c const unsigned
2677a 20 63 68 61 72 20 6c 65 6e 4f 6e 65 5b 5d 20 3d   char lenOne[] =
2677b 20 7b 20 31 20 7d 3b 0a 20 20 20 20 73 74 61 74   { 1 };.    stat
2677c 69 63 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  ic unsigned char
2677d 20 2a 20 63 6f 6e 73 74 20 61 7a 4f 6e 65 5b 5d   * const azOne[]
2677e 20 3d 20 7b 20 28 75 38 2a 29 22 20 22 20 7d 3b   = { (u8*)" " };
2677f 0a 20 20 20 20 6e 43 68 61 72 20 3d 20 31 3b 0a  .    nChar = 1;.
26780 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 38 2a 29      aLen = (u8*)
26781 6c 65 6e 4f 6e 65 3b 0a 20 20 20 20 61 7a 43 68  lenOne;.    azCh
26782 61 72 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  ar = (unsigned c
26783 68 61 72 20 2a 2a 29 61 7a 4f 6e 65 3b 0a 20 20  har **)azOne;.  
26784 20 20 7a 43 68 61 72 53 65 74 20 3d 20 30 3b 0a    zCharSet = 0;.
26785 20 20 7d 65 6c 73 65 20 69 66 28 20 28 7a 43 68    }else if( (zCh
26786 61 72 53 65 74 20 3d 20 73 71 6c 69 74 65 33 5f  arSet = sqlite3_
26787 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
26788 31 5d 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  1]))==0 ){.    r
26789 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a  eturn;.  }else{.
2678a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e      const unsign
2678b 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20  ed char *z;.    
2678c 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74 2c 20  for(z=zCharSet, 
2678d 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e 43 68  nChar=0; *z; nCh
2678e 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20 53 51 4c  ar++){.      SQL
2678f 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29  ITE_SKIP_UTF8(z)
26790 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
26791 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20 20 20 20  nChar>0 ){.     
26792 20 61 7a 43 68 61 72 20 3d 20 63 6f 6e 74 65 78   azChar = contex
26793 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
26794 20 28 28 69 36 34 29 6e 43 68 61 72 29 2a 28 73   ((i64)nChar)*(s
26795 69 7a 65 6f 66 28 63 68 61 72 2a 29 2b 31 29 29  izeof(char*)+1))
26796 3b 0a 20 20 20 20 20 20 69 66 28 20 61 7a 43 68  ;.      if( azCh
26797 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ar==0 ){.       
26798 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
26799 0a 20 20 20 20 20 20 61 4c 65 6e 20 3d 20 28 75  .      aLen = (u
2679a 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 26 61  nsigned char*)&a
2679b 7a 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a 20 20  zChar[nChar];.  
2679c 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53      for(z=zCharS
2679d 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b  et, nChar=0; *z;
2679e 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20   nChar++){.     
2679f 20 20 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d     azChar[nChar]
267a0 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61   = (unsigned cha
267a1 72 20 2a 29 7a 3b 0a 20 20 20 20 20 20 20 20 53  r *)z;.        S
267a2 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
267a3 7a 29 3b 0a 20 20 20 20 20 20 20 20 61 4c 65 6e  z);.        aLen
267a4 5b 6e 43 68 61 72 5d 20 3d 20 28 75 38 29 28 7a  [nChar] = (u8)(z
267a5 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d   - azChar[nChar]
267a6 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
267a7 0a 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 72  .  }.  if( nChar
267a8 3e 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20  >0 ){.    flags 
267a9 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f  = SQLITE_PTR_TO_
267aa 49 4e 54 28 73 71 6c 69 74 65 33 5f 75 73 65 72  INT(sqlite3_user
267ab 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 29 3b  _data(context));
267ac 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26  .    if( flags &
267ad 20 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c   1 ){.      whil
267ae 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20  e( nIn>0 ){.    
267af 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b      int len = 0;
267b0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
267b1 3b 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b 29 7b  ; i<nChar; i++){
267b2 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 20 3d  .          len =
267b3 20 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20 20 20   aLen[i];.      
267b4 20 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e 49 6e      if( len<=nIn
267b5 20 26 26 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20   && memcmp(zIn, 
267b6 61 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d  azChar[i], len)=
267b7 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
267b8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
267b9 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65  ( i>=nChar ) bre
267ba 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e 20  ak;.        zIn 
267bb 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20  += len;.        
267bc 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20  nIn -= len;.    
267bd 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
267be 28 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a 20  ( flags & 2 ){. 
267bf 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e       while( nIn>
267c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  0 ){.        int
267c1 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20   len = 0;.      
267c2 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 68    for(i=0; i<nCh
267c3 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ar; i++){.      
267c4 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69      len = aLen[i
267c5 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ];.          if(
267c6 20 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d   len<=nIn && mem
267c7 63 6d 70 28 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e  cmp(&zIn[nIn-len
267c8 5d 2c 61 7a 43 68 61 72 5b 69 5d 2c 6c 65 6e 29  ],azChar[i],len)
267c9 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
267ca 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
267cb 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72  f( i>=nChar ) br
267cc 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 6e 49 6e  eak;.        nIn
267cd 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 7d   -= len;.      }
267ce 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 7a  .    }.    if( z
267cf 43 68 61 72 53 65 74 20 29 7b 0a 20 20 20 20 20  CharSet ){.     
267d0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a   sqlite3_free(az
267d1 43 68 61 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Char);.    }.  }
267d2 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
267d3 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
267d4 28 63 68 61 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c  (char*)zIn, nIn,
267d5 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
267d6 54 29 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53  T);.}...#ifdef S
267d7 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a  QLITE_SOUNDEX./*
267d8 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20  .** Compute the 
267d9 73 6f 75 6e 64 65 78 20 65 6e 63 6f 64 69 6e 67  soundex encoding
267da 20 6f 66 20 61 20 77 6f 72 64 2e 0a 2a 2f 0a 73   of a word..*/.s
267db 74 61 74 69 63 20 76 6f 69 64 20 73 6f 75 6e 64  tatic void sound
267dc 65 78 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  exFunc(.  sqlite
267dd 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
267de 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
267df 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
267e0 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 68 61 72  **argv.){.  char
267e1 20 7a 52 65 73 75 6c 74 5b 38 5d 3b 0a 20 20 63   zResult[8];.  c
267e2 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 3b 0a 20 20  onst u8 *zIn;.  
267e3 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 61 74  int i, j;.  stat
267e4 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
267e5 64 20 63 68 61 72 20 69 43 6f 64 65 5b 5d 20 3d  d char iCode[] =
267e6 20 7b 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20   {.    0, 0, 0, 
267e7 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
267e8 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
267e9 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c   0, 0,.    0, 0,
267ea 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
267eb 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
267ec 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30  , 0, 0, 0,.    0
267ed 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
267ee 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
267ef 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20  0, 0, 0, 0, 0,. 
267f0 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30     0, 0, 0, 0, 0
267f1 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
267f2 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
267f3 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c 20  0,.    0, 0, 1, 
267f4 32 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c 20 30  2, 3, 0, 1, 2, 0
267f5 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20 35 2c  , 0, 2, 2, 4, 5,
267f6 20 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20 32 2c   5, 0,.    1, 2,
267f7 20 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20   6, 2, 3, 0, 1, 
267f8 30 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c 20 30  0, 2, 0, 2, 0, 0
267f9 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30  , 0, 0, 0,.    0
267fa 2c 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20 30 2c  , 0, 1, 2, 3, 0,
267fb 20 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32 2c 20   1, 2, 0, 0, 2, 
267fc 32 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c 0a 20  2, 4, 5, 5, 0,. 
267fd 20 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c 20 33     1, 2, 6, 2, 3
267fe 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20 30 2c  , 0, 1, 0, 2, 0,
267ff 20 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   2, 0, 0, 0, 0, 
26800 30 2c 0a 20 20 7d 3b 0a 20 20 61 73 73 65 72 74  0,.  };.  assert
26801 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 7a  ( argc==1 );.  z
26802 49 6e 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65  In = (u8*)sqlite
26803 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
26804 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 49 6e  v[0]);.  if( zIn
26805 3d 3d 30 20 29 20 7a 49 6e 20 3d 20 28 75 38 2a  ==0 ) zIn = (u8*
26806 29 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  )"";.  for(i=0; 
26807 7a 49 6e 5b 69 5d 20 26 26 20 21 73 71 6c 69 74  zIn[i] && !sqlit
26808 65 33 49 73 61 6c 70 68 61 28 7a 49 6e 5b 69 5d  e3Isalpha(zIn[i]
26809 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66 28 20  ); i++){}.  if( 
2680a 7a 49 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 75 38  zIn[i] ){.    u8
2680b 20 70 72 65 76 63 6f 64 65 20 3d 20 69 43 6f 64   prevcode = iCod
2680c 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a  e[zIn[i]&0x7f];.
2680d 20 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d 20 3d      zResult[0] =
2680e 20 73 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28   sqlite3Toupper(
2680f 7a 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72  zIn[i]);.    for
26810 28 6a 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e  (j=1; j<4 && zIn
26811 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  [i]; i++){.     
26812 20 69 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64   int code = iCod
26813 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a  e[zIn[i]&0x7f];.
26814 20 20 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30        if( code>0
26815 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
26816 63 6f 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29  code!=prevcode )
26817 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 76  {.          prev
26818 63 6f 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20  code = code;.   
26819 20 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a         zResult[j
2681a 2b 2b 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27  ++] = code + '0'
2681b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2681c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2681d 20 70 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20   prevcode = 0;. 
2681e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2681f 20 77 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20   while( j<4 ){. 
26820 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b       zResult[j++
26821 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20  ] = '0';.    }. 
26822 20 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20     zResult[j] = 
26823 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  0;.    sqlite3_r
26824 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
26825 78 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20  xt, zResult, 4, 
26826 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
26827 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
26828 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
26829 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 3f 30  ext(context, "?0
2682a 30 30 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53  00", 4, SQLITE_S
2682b 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65  TATIC);.  }.}.#e
2682c 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
2682d 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
2682e 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41  XTENSION./*.** A
2682f 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c   function that l
26830 6f 61 64 73 20 61 20 73 68 61 72 65 64 2d 6c 69  oads a shared-li
26831 62 72 61 72 79 20 65 78 74 65 6e 73 69 6f 6e 20  brary extension 
26832 74 68 65 6e 20 72 65 74 75 72 6e 73 20 4e 55 4c  then returns NUL
26833 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  L..*/.static voi
26834 64 20 6c 6f 61 64 45 78 74 28 73 71 6c 69 74 65  d loadExt(sqlite
26835 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
26836 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
26837 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
26838 67 76 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  gv){.  const cha
26839 72 20 2a 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73  r *zFile = (cons
2683a 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
2683b 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
2683c 5b 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  [0]);.  const ch
2683d 61 72 20 2a 7a 50 72 6f 63 3b 0a 20 20 73 71 6c  ar *zProc;.  sql
2683e 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
2683f 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
26840 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ndle(context);. 
26841 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d   char *zErrMsg =
26842 20 30 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d   0;..  if( argc=
26843 3d 32 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20  =2 ){.    zProc 
26844 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
26845 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
26846 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d  xt(argv[1]);.  }
26847 65 6c 73 65 7b 0a 20 20 20 20 7a 50 72 6f 63 20  else{.    zProc 
26848 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a  = 0;.  }.  if( z
26849 46 69 6c 65 20 26 26 20 73 71 6c 69 74 65 33 5f  File && sqlite3_
2684a 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 64  load_extension(d
2684b 62 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c  b, zFile, zProc,
2684c 20 26 7a 45 72 72 4d 73 67 29 20 29 7b 0a 20 20   &zErrMsg) ){.  
2684d 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2684e 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
2684f 7a 45 72 72 4d 73 67 2c 20 2d 31 29 3b 0a 20 20  zErrMsg, -1);.  
26850 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
26851 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23  ErrMsg);.  }.}.#
26852 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e  endif.../*.** An
26853 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
26854 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
26855 74 75 72 65 20 68 6f 6c 64 73 20 74 68 65 20 63  ture holds the c
26856 6f 6e 74 65 78 74 20 6f 66 20 61 0a 2a 2a 20 73  ontext of a.** s
26857 75 6d 28 29 20 6f 72 20 61 76 67 28 29 20 61 67  um() or avg() ag
26858 67 72 65 67 61 74 65 20 63 6f 6d 70 75 74 61 74  gregate computat
26859 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ion..*/.typedef 
2685a 73 74 72 75 63 74 20 53 75 6d 43 74 78 20 53 75  struct SumCtx Su
2685b 6d 43 74 78 3b 0a 73 74 72 75 63 74 20 53 75 6d  mCtx;.struct Sum
2685c 43 74 78 20 7b 0a 20 20 64 6f 75 62 6c 65 20 72  Ctx {.  double r
2685d 53 75 6d 3b 20 20 20 20 20 20 2f 2a 20 46 6c 6f  Sum;      /* Flo
2685e 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 6d 20  ating point sum 
2685f 2a 2f 0a 20 20 69 36 34 20 69 53 75 6d 3b 20 20  */.  i64 iSum;  
26860 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65         /* Intege
26861 72 20 73 75 6d 20 2a 2f 20 20 20 0a 20 20 69 36  r sum */   .  i6
26862 34 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20  4 cnt;          
26863 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65  /* Number of ele
26864 6d 65 6e 74 73 20 73 75 6d 6d 65 64 20 2a 2f 0a  ments summed */.
26865 20 20 75 38 20 6f 76 65 72 66 6c 6f 77 3b 20 20    u8 overflow;  
26866 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
26867 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20  nteger overflow 
26868 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 61 70 70  seen */.  u8 app
26869 72 6f 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  rox;        /* T
2686a 72 75 65 20 69 66 20 6e 6f 6e 2d 69 6e 74 65 67  rue if non-integ
2686b 65 72 20 76 61 6c 75 65 20 77 61 73 20 69 6e 70  er value was inp
2686c 75 74 20 74 6f 20 74 68 65 20 73 75 6d 20 2a 2f  ut to the sum */
2686d 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69  .};../*.** Routi
2686e 6e 65 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70  nes used to comp
2686f 75 74 65 20 74 68 65 20 73 75 6d 2c 20 61 76 65  ute the sum, ave
26870 72 61 67 65 2c 20 61 6e 64 20 74 6f 74 61 6c 2e  rage, and total.
26871 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 55 4d 28 29  .**.** The SUM()
26872 20 66 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77   function follow
26873 73 20 74 68 65 20 28 62 72 6f 6b 65 6e 29 20 53  s the (broken) S
26874 51 4c 20 73 74 61 6e 64 61 72 64 20 77 68 69 63  QL standard whic
26875 68 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20  h means.** that 
26876 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20  it returns NULL 
26877 69 66 20 69 74 20 73 75 6d 73 20 6f 76 65 72 20  if it sums over 
26878 6e 6f 20 69 6e 70 75 74 73 2e 20 20 54 4f 54 41  no inputs.  TOTA
26879 4c 20 72 65 74 75 72 6e 73 0a 2a 2a 20 30 2e 30  L returns.** 0.0
2687a 20 69 6e 20 74 68 61 74 20 63 61 73 65 2e 20 20   in that case.  
2687b 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20 54 4f 54  In addition, TOT
2687c 41 4c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  AL always return
2687d 73 20 61 20 66 6c 6f 61 74 20 77 68 65 72 65 0a  s a float where.
2687e 2a 2a 20 53 55 4d 20 6d 69 67 68 74 20 72 65 74  ** SUM might ret
2687f 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 69  urn an integer i
26880 66 20 69 74 20 6e 65 76 65 72 20 65 6e 63 6f 75  f it never encou
26881 6e 74 65 72 73 20 61 20 66 6c 6f 61 74 69 6e 67  nters a floating
26882 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e   point.** value.
26883 20 20 54 4f 54 41 4c 20 6e 65 76 65 72 20 66 61    TOTAL never fa
26884 69 6c 73 2c 20 62 75 74 20 53 55 4d 20 6d 69 67  ils, but SUM mig
26885 68 74 20 74 68 72 6f 75 67 68 20 61 6e 20 65 78  ht through an ex
26886 63 65 70 74 69 6f 6e 20 69 66 0a 2a 2a 20 69 74  ception if.** it
26887 20 6f 76 65 72 66 6c 6f 77 73 20 61 6e 20 69 6e   overflows an in
26888 74 65 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  teger..*/.static
26889 20 76 6f 69 64 20 73 75 6d 53 74 65 70 28 73 71   void sumStep(sq
2688a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2688b 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63  ontext, int argc
2688c 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
2688d 2a 2a 61 72 67 76 29 7b 0a 20 20 53 75 6d 43 74  **argv){.  SumCt
2688e 78 20 2a 70 3b 0a 20 20 69 6e 74 20 74 79 70 65  x *p;.  int type
2688f 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ;.  assert( argc
26890 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f  ==1 );.  UNUSED_
26891 50 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b  PARAMETER(argc);
26892 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61  .  p = sqlite3_a
26893 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74  ggregate_context
26894 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66  (context, sizeof
26895 28 2a 70 29 29 3b 0a 20 20 74 79 70 65 20 3d 20  (*p));.  type = 
26896 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75  sqlite3_value_nu
26897 6d 65 72 69 63 5f 74 79 70 65 28 61 72 67 76 5b  meric_type(argv[
26898 30 5d 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20  0]);.  if( p && 
26899 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 4e 55 4c  type!=SQLITE_NUL
2689a 4c 20 29 7b 0a 20 20 20 20 70 2d 3e 63 6e 74 2b  L ){.    p->cnt+
2689b 2b 3b 0a 20 20 20 20 69 66 28 20 74 79 70 65 3d  +;.    if( type=
2689c 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20  =SQLITE_INTEGER 
2689d 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76 20 3d  ){.      i64 v =
2689e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
2689f 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20  nt64(argv[0]);. 
268a0 20 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d 20       p->rSum += 
268a1 76 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70 2d  v;.      if( (p-
268a2 3e 61 70 70 72 6f 78 7c 70 2d 3e 6f 76 65 72 66  >approx|p->overf
268a3 6c 6f 77 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  low)==0 ){.     
268a4 20 20 20 69 36 34 20 69 4e 65 77 53 75 6d 20 3d     i64 iNewSum =
268a5 20 70 2d 3e 69 53 75 6d 20 2b 20 76 3b 0a 20 20   p->iSum + v;.  
268a6 20 20 20 20 20 20 69 6e 74 20 73 31 20 3d 20 28        int s1 = (
268a7 69 6e 74 29 28 70 2d 3e 69 53 75 6d 20 3e 3e 20  int)(p->iSum >> 
268a8 28 73 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31  (sizeof(i64)*8-1
268a9 29 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  ));.        int 
268aa 73 32 20 3d 20 28 69 6e 74 29 28 76 20 20 20 20  s2 = (int)(v    
268ab 20 20 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36     >> (sizeof(i6
268ac 34 29 2a 38 2d 31 29 29 3b 0a 20 20 20 20 20 20  4)*8-1));.      
268ad 20 20 69 6e 74 20 73 33 20 3d 20 28 69 6e 74 29    int s3 = (int)
268ae 28 69 4e 65 77 53 75 6d 20 3e 3e 20 28 73 69 7a  (iNewSum >> (siz
268af 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 29 3b 0a  eof(i64)*8-1));.
268b0 20 20 20 20 20 20 20 20 70 2d 3e 6f 76 65 72 66          p->overf
268b1 6c 6f 77 20 3d 20 28 28 73 31 26 73 32 26 7e 73  low = ((s1&s2&~s
268b2 33 29 20 7c 20 28 7e 73 31 26 7e 73 32 26 73 33  3) | (~s1&~s2&s3
268b3 29 29 3f 31 3a 30 3b 0a 20 20 20 20 20 20 20 20  ))?1:0;.        
268b4 70 2d 3e 69 53 75 6d 20 3d 20 69 4e 65 77 53 75  p->iSum = iNewSu
268b5 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  m;.      }.    }
268b6 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d 3e 72  else{.      p->r
268b7 53 75 6d 20 2b 3d 20 73 71 6c 69 74 65 33 5f 76  Sum += sqlite3_v
268b8 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76  alue_double(argv
268b9 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  [0]);.      p->a
268ba 70 70 72 6f 78 20 3d 20 31 3b 0a 20 20 20 20 7d  pprox = 1;.    }
268bb 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
268bc 69 64 20 73 75 6d 46 69 6e 61 6c 69 7a 65 28 73  id sumFinalize(s
268bd 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
268be 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43  context){.  SumC
268bf 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  tx *p;.  p = sql
268c0 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
268c1 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
268c2 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70  0);.  if( p && p
268c3 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20 20 69  ->cnt>0 ){.    i
268c4 66 28 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 29  f( p->overflow )
268c5 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
268c6 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
268c7 74 65 78 74 2c 22 69 6e 74 65 67 65 72 20 6f 76  text,"integer ov
268c8 65 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a 20 20 20  erflow",-1);.   
268c9 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 61 70   }else if( p->ap
268ca 70 72 6f 78 20 29 7b 0a 20 20 20 20 20 20 73 71  prox ){.      sq
268cb 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
268cc 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 2d 3e  ble(context, p->
268cd 72 53 75 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65  rSum);.    }else
268ce 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
268cf 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e  result_int64(con
268d0 74 65 78 74 2c 20 70 2d 3e 69 53 75 6d 29 3b 0a  text, p->iSum);.
268d1 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74      }.  }.}.stat
268d2 69 63 20 76 6f 69 64 20 61 76 67 46 69 6e 61 6c  ic void avgFinal
268d3 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ize(sqlite3_cont
268d4 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20  ext *context){. 
268d5 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20   SumCtx *p;.  p 
268d6 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  = sqlite3_aggreg
268d7 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74  ate_context(cont
268d8 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ext, 0);.  if( p
268d9 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a   && p->cnt>0 ){.
268da 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
268db 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78  lt_double(contex
268dc 74 2c 20 70 2d 3e 72 53 75 6d 2f 28 64 6f 75 62  t, p->rSum/(doub
268dd 6c 65 29 70 2d 3e 63 6e 74 29 3b 0a 20 20 7d 0a  le)p->cnt);.  }.
268de 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f  }.static void to
268df 74 61 6c 46 69 6e 61 6c 69 7a 65 28 73 71 6c 69  talFinalize(sqli
268e0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
268e1 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74 78 20  text){.  SumCtx 
268e2 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65  *p;.  p = sqlite
268e3 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
268e4 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30 29 3b  ext(context, 0);
268e5 0a 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20  .  /* (double)0 
268e6 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54  In case of SQLIT
268e7 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
268e8 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 73 71  POINT... */.  sq
268e9 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75  lite3_result_dou
268ea 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f  ble(context, p ?
268eb 20 70 2d 3e 72 53 75 6d 20 3a 20 28 64 6f 75 62   p->rSum : (doub
268ec 6c 65 29 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  le)0);.}../*.** 
268ed 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  The following st
268ee 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74 72  ructure keeps tr
268ef 61 63 6b 20 6f 66 20 73 74 61 74 65 20 69 6e 66  ack of state inf
268f0 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  ormation for the
268f1 0a 2a 2a 20 63 6f 75 6e 74 28 29 20 61 67 67 72  .** count() aggr
268f2 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a  egate function..
268f3 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
268f4 74 20 43 6f 75 6e 74 43 74 78 20 43 6f 75 6e 74  t CountCtx Count
268f5 43 74 78 3b 0a 73 74 72 75 63 74 20 43 6f 75 6e  Ctx;.struct Coun
268f6 74 43 74 78 20 7b 0a 20 20 69 36 34 20 6e 3b 0a  tCtx {.  i64 n;.
268f7 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e  };../*.** Routin
268f8 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  es to implement 
268f9 74 68 65 20 63 6f 75 6e 74 28 29 20 61 67 67 72  the count() aggr
268fa 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 2e 0a  egate function..
268fb 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
268fc 6f 75 6e 74 53 74 65 70 28 73 71 6c 69 74 65 33  ountStep(sqlite3
268fd 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
268fe 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  t, int argc, sql
268ff 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
26900 76 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78 20 2a  v){.  CountCtx *
26901 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  p;.  p = sqlite3
26902 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
26903 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65  xt(context, size
26904 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20 28  of(*p));.  if( (
26905 61 72 67 63 3d 3d 30 20 7c 7c 20 53 51 4c 49 54  argc==0 || SQLIT
26906 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f  E_NULL!=sqlite3_
26907 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
26908 30 5d 29 29 20 26 26 20 70 20 29 7b 0a 20 20 20  0])) && p ){.   
26909 20 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a 0a 23 69   p->n++;.  }..#i
2690a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2690b 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20 2f  T_DEPRECATED.  /
2690c 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 61 67  * The sqlite3_ag
2690d 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 29 20  gregate_count() 
2690e 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 70 72  function is depr
2690f 65 63 61 74 65 64 2e 20 20 42 75 74 20 6a 75 73  ecated.  But jus
26910 74 20 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a 20 73  t to make.  ** s
26911 75 72 65 20 69 74 20 73 74 69 6c 6c 20 6f 70 65  ure it still ope
26912 72 61 74 65 73 20 63 6f 72 72 65 63 74 6c 79 2c  rates correctly,
26913 20 76 65 72 69 66 79 20 74 68 61 74 20 69 74 73   verify that its
26914 20 63 6f 75 6e 74 20 61 67 72 65 65 73 20 77 69   count agrees wi
26915 74 68 20 6f 75 72 20 0a 20 20 2a 2a 20 69 6e 74  th our .  ** int
26916 65 72 6e 61 6c 20 63 6f 75 6e 74 20 77 68 65 6e  ernal count when
26917 20 75 73 69 6e 67 20 63 6f 75 6e 74 28 2a 29 20   using count(*) 
26918 61 6e 64 20 77 68 65 6e 20 74 68 65 20 74 6f 74  and when the tot
26919 61 6c 20 63 6f 75 6e 74 20 63 61 6e 20 62 65 0a  al count can be.
2691a 20 20 2a 2a 20 65 78 70 72 65 73 73 65 64 20 61    ** expressed a
2691b 73 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  s a 32-bit integ
2691c 65 72 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  er. */.  assert(
2691d 20 61 72 67 63 3d 3d 31 20 7c 7c 20 70 3d 3d 30   argc==1 || p==0
2691e 20 7c 7c 20 70 2d 3e 6e 3e 30 78 37 66 66 66 66   || p->n>0x7ffff
2691f 66 66 66 0a 20 20 20 20 20 20 20 20 20 20 7c 7c  fff.          ||
26920 20 70 2d 3e 6e 3d 3d 73 71 6c 69 74 65 33 5f 61   p->n==sqlite3_a
26921 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 63  ggregate_count(c
26922 6f 6e 74 65 78 74 29 20 29 3b 0a 23 65 6e 64 69  ontext) );.#endi
26923 66 0a 7d 20 20 20 0a 73 74 61 74 69 63 20 76 6f  f.}   .static vo
26924 69 64 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65  id countFinalize
26925 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
26926 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 43 6f   *context){.  Co
26927 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d  untCtx *p;.  p =
26928 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
26929 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
2692a 78 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65  xt, 0);.  sqlite
2692b 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63  3_result_int64(c
2692c 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d 3e 6e  ontext, p ? p->n
2692d 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   : 0);.}../*.** 
2692e 52 6f 75 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c  Routines to impl
2692f 65 6d 65 6e 74 20 6d 69 6e 28 29 20 61 6e 64 20  ement min() and 
26930 6d 61 78 28 29 20 61 67 67 72 65 67 61 74 65 20  max() aggregate 
26931 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  functions..*/.st
26932 61 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78  atic void minmax
26933 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33 5f  Step(.  sqlite3_
26934 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
26935 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  , .  int NotUsed
26936 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  , .  sqlite3_val
26937 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 4d  ue **argv.){.  M
26938 65 6d 20 2a 70 41 72 67 20 20 3d 20 28 4d 65 6d  em *pArg  = (Mem
26939 20 2a 29 61 72 67 76 5b 30 5d 3b 0a 20 20 4d 65   *)argv[0];.  Me
2693a 6d 20 2a 70 42 65 73 74 3b 0a 20 20 55 4e 55 53  m *pBest;.  UNUS
2693b 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
2693c 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20 73 71  Used);..  if( sq
2693d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
2693e 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54  (argv[0])==SQLIT
2693f 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b  E_NULL ) return;
26940 0a 20 20 70 42 65 73 74 20 3d 20 28 4d 65 6d 20  .  pBest = (Mem 
26941 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67  *)sqlite3_aggreg
26942 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74  ate_context(cont
26943 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 42 65  ext, sizeof(*pBe
26944 73 74 29 29 3b 0a 20 20 69 66 28 20 21 70 42 65  st));.  if( !pBe
26945 73 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20  st ) return;..  
26946 69 66 28 20 70 42 65 73 74 2d 3e 66 6c 61 67 73  if( pBest->flags
26947 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 61 78 3b   ){.    int max;
26948 0a 20 20 20 20 69 6e 74 20 63 6d 70 3b 0a 20 20  .    int cmp;.  
26949 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2694a 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e   = sqlite3GetFun
2694b 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74  cCollSeq(context
2694c 29 3b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 73  );.    /* This s
2694d 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  tep function is 
2694e 75 73 65 64 20 66 6f 72 20 62 6f 74 68 20 74 68  used for both th
2694f 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78 28  e min() and max(
26950 29 20 61 67 67 72 65 67 61 74 65 73 2c 0a 20 20  ) aggregates,.  
26951 20 20 2a 2a 20 74 68 65 20 6f 6e 6c 79 20 64 69    ** the only di
26952 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e  fference between
26953 20 74 68 65 20 74 77 6f 20 62 65 69 6e 67 20 74   the two being t
26954 68 61 74 20 74 68 65 20 73 65 6e 73 65 20 6f 66  hat the sense of
26955 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 6d 70   the.    ** comp
26956 61 72 69 73 6f 6e 20 69 73 20 69 6e 76 65 72 74  arison is invert
26957 65 64 2e 20 46 6f 72 20 74 68 65 20 6d 61 78 28  ed. For the max(
26958 29 20 61 67 67 72 65 67 61 74 65 2c 20 74 68 65  ) aggregate, the
26959 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  .    ** sqlite3_
2695a 75 73 65 72 5f 64 61 74 61 28 29 20 66 75 6e 63  user_data() func
2695b 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 28 76 6f  tion returns (vo
2695c 69 64 20 2a 29 2d 31 2e 20 46 6f 72 20 6d 69 6e  id *)-1. For min
2695d 28 29 20 69 74 0a 20 20 20 20 2a 2a 20 72 65 74  () it.    ** ret
2695e 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 64 62 2c  urns (void *)db,
2695f 20 77 68 65 72 65 20 64 62 20 69 73 20 74 68 65   where db is the
26960 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61   sqlite3* databa
26961 73 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20 20 20  se pointer..    
26962 2a 2a 20 54 68 65 72 65 66 6f 72 65 20 74 68 65  ** Therefore the
26963 20 6e 65 78 74 20 73 74 61 74 65 6d 65 6e 74 20   next statement 
26964 73 65 74 73 20 76 61 72 69 61 62 6c 65 20 27 6d  sets variable 'm
26965 61 78 27 20 74 6f 20 31 20 66 6f 72 20 74 68 65  ax' to 1 for the
26966 20 6d 61 78 28 29 0a 20 20 20 20 2a 2a 20 61 67   max().    ** ag
26967 67 72 65 67 61 74 65 2c 20 6f 72 20 30 20 66 6f  gregate, or 0 fo
26968 72 20 6d 69 6e 28 29 2e 0a 20 20 20 20 2a 2f 0a  r min()..    */.
26969 20 20 20 20 6d 61 78 20 3d 20 73 71 6c 69 74 65      max = sqlite
2696a 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74  3_user_data(cont
2696b 65 78 74 29 21 3d 30 3b 0a 20 20 20 20 63 6d 70  ext)!=0;.    cmp
2696c 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d   = sqlite3MemCom
2696d 70 61 72 65 28 70 42 65 73 74 2c 20 70 41 72 67  pare(pBest, pArg
2696e 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66  , pColl);.    if
2696f 28 20 28 6d 61 78 20 26 26 20 63 6d 70 3c 30 29  ( (max && cmp<0)
26970 20 7c 7c 20 28 21 6d 61 78 20 26 26 20 63 6d 70   || (!max && cmp
26971 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  >0) ){.      sql
26972 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28  ite3VdbeMemCopy(
26973 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20 20  pBest, pArg);.  
26974 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
26975 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43   sqlite3VdbeMemC
26976 6f 70 79 28 70 42 65 73 74 2c 20 70 41 72 67 29  opy(pBest, pArg)
26977 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
26978 6f 69 64 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69  oid minMaxFinali
26979 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ze(sqlite3_conte
2697a 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20  xt *context){.  
2697b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
2697c 52 65 73 3b 0a 20 20 70 52 65 73 20 3d 20 28 73  Res;.  pRes = (s
2697d 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 29 73  qlite3_value *)s
2697e 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
2697f 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74  _context(context
26980 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 52 65 73  , 0);.  if( pRes
26981 20 29 7b 0a 20 20 20 20 69 66 28 20 41 4c 57 41   ){.    if( ALWA
26982 59 53 28 70 52 65 73 2d 3e 66 6c 61 67 73 29 20  YS(pRes->flags) 
26983 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
26984 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f  _result_value(co
26985 6e 74 65 78 74 2c 20 70 52 65 73 29 3b 0a 20 20  ntext, pRes);.  
26986 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
26987 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 52  dbeMemRelease(pR
26988 65 73 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  es);.  }.}../*.*
26989 2a 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74 28 45  * group_concat(E
2698a 58 50 52 2c 20 3f 53 45 50 41 52 41 54 4f 52 3f  XPR, ?SEPARATOR?
2698b 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ).*/.static void
2698c 20 67 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70   groupConcatStep
2698d 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
2698e 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
2698f 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
26990 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
26991 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  .){.  const char
26992 20 2a 7a 56 61 6c 3b 0a 20 20 53 74 72 41 63 63   *zVal;.  StrAcc
26993 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20 63 6f  um *pAccum;.  co
26994 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70 3b 0a  nst char *zSep;.
26995 20 20 69 6e 74 20 6e 56 61 6c 2c 20 6e 53 65 70    int nVal, nSep
26996 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ;.  assert( argc
26997 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29  ==1 || argc==2 )
26998 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ;.  if( sqlite3_
26999 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
2699a 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c  0])==SQLITE_NULL
2699b 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 41 63   ) return;.  pAc
2699c 63 75 6d 20 3d 20 28 53 74 72 41 63 63 75 6d 2a  cum = (StrAccum*
2699d 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  )sqlite3_aggrega
2699e 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
2699f 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 41 63 63  xt, sizeof(*pAcc
269a0 75 6d 29 29 3b 0a 0a 20 20 69 66 28 20 70 41 63  um));..  if( pAc
269a1 63 75 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  cum ){.    sqlit
269a2 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
269a3 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
269a4 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  le(context);.   
269a5 20 69 6e 74 20 66 69 72 73 74 54 65 72 6d 20 3d   int firstTerm =
269a6 20 70 41 63 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c   pAccum->useMall
269a7 6f 63 3d 3d 30 3b 0a 20 20 20 20 70 41 63 63 75  oc==0;.    pAccu
269a8 6d 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31  m->useMalloc = 1
269a9 3b 0a 20 20 20 20 70 41 63 63 75 6d 2d 3e 6d 78  ;.    pAccum->mx
269aa 41 6c 6c 6f 63 20 3d 20 64 62 2d 3e 61 4c 69 6d  Alloc = db->aLim
269ab 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
269ac 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 20 69 66 28  LENGTH];.    if(
269ad 20 21 66 69 72 73 74 54 65 72 6d 20 29 7b 0a 20   !firstTerm ){. 
269ae 20 20 20 20 20 69 66 28 20 61 72 67 63 3d 3d 32       if( argc==2
269af 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 53 65 70   ){.        zSep
269b0 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65   = (char*)sqlite
269b1 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
269b2 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 6e  v[1]);.        n
269b3 53 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Sep = sqlite3_va
269b4 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31  lue_bytes(argv[1
269b5 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ]);.      }else{
269b6 0a 20 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20  .        zSep = 
269b7 22 2c 22 3b 0a 20 20 20 20 20 20 20 20 6e 53 65  ",";.        nSe
269b8 70 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  p = 1;.      }. 
269b9 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
269ba 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
269bb 6d 2c 20 7a 53 65 70 2c 20 6e 53 65 70 29 3b 0a  m, zSep, nSep);.
269bc 20 20 20 20 7d 0a 20 20 20 20 7a 56 61 6c 20 3d      }.    zVal =
269bd 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f   (char*)sqlite3_
269be 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
269bf 30 5d 29 3b 0a 20 20 20 20 6e 56 61 6c 20 3d 20  0]);.    nVal = 
269c0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
269c1 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
269c2 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
269c3 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20  mAppend(pAccum, 
269c4 7a 56 61 6c 2c 20 6e 56 61 6c 29 3b 0a 20 20 7d  zVal, nVal);.  }
269c5 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67  .}.static void g
269c6 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69  roupConcatFinali
269c7 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ze(sqlite3_conte
269c8 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20  xt *context){.  
269c9 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d  StrAccum *pAccum
269ca 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20 73 71 6c  ;.  pAccum = sql
269cb 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
269cc 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
269cd 30 29 3b 0a 20 20 69 66 28 20 70 41 63 63 75 6d  0);.  if( pAccum
269ce 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41 63 63   ){.    if( pAcc
269cf 75 6d 2d 3e 74 6f 6f 42 69 67 20 29 7b 0a 20 20  um->tooBig ){.  
269d0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
269d1 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
269d2 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d 65  context);.    }e
269d3 6c 73 65 20 69 66 28 20 70 41 63 63 75 6d 2d 3e  lse if( pAccum->
269d4 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
269d5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
269d6 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
269d7 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d  (context);.    }
269d8 65 6c 73 65 7b 20 20 20 20 0a 20 20 20 20 20 20  else{    .      
269d9 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
269da 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c  ext(context, sql
269db 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69  ite3StrAccumFini
269dc 73 68 28 70 41 63 63 75 6d 29 2c 20 2d 31 2c 20  sh(pAccum), -1, 
269dd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
269de 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
269df 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a  e3_free);.    }.
269e0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
269e1 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73  s function regis
269e2 74 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68 65  tered all of the
269e3 20 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69 6f   above C functio
269e4 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75 6e  ns as SQL.** fun
269e5 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73 68  ctions.  This sh
269e6 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c 79  ould be the only
269e7 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73   routine in this
269e8 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65 78   file with.** ex
269e9 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a  ternal linkage..
269ea 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
269eb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
269ec 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e  gisterBuiltinFun
269ed 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a  ctions(sqlite3 *
269ee 64 62 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  db){.#ifndef SQL
269ef 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41  ITE_OMIT_ALTERTA
269f0 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 41 6c 74  BLE.  sqlite3Alt
269f1 65 72 46 75 6e 63 74 69 6f 6e 73 28 64 62 29 3b  erFunctions(db);
269f2 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21 64  .#endif.  if( !d
269f3 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
269f4 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
269f5 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
269f6 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 4d  _function(db, "M
269f7 41 54 43 48 22 2c 20 32 29 3b 0a 20 20 20 20 61  ATCH", 2);.    a
269f8 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
269f9 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53  E_NOMEM || rc==S
269fa 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
269fb 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
269fc 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62  OMEM ){.      db
269fd 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
269fe 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a   1;.    }.  }.}.
269ff 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 4c  ./*.** Set the L
26a00 49 4b 45 4f 50 54 20 66 6c 61 67 20 6f 6e 20 74  IKEOPT flag on t
26a01 68 65 20 32 2d 61 72 67 75 6d 65 6e 74 20 66 75  he 2-argument fu
26a02 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
26a03 67 69 76 65 6e 20 6e 61 6d 65 2e 0a 2a 2f 0a 73  given name..*/.s
26a04 74 61 74 69 63 20 76 6f 69 64 20 73 65 74 4c 69  tatic void setLi
26a05 6b 65 4f 70 74 46 6c 61 67 28 73 71 6c 69 74 65  keOptFlag(sqlite
26a06 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
26a07 72 20 2a 7a 4e 61 6d 65 2c 20 75 38 20 66 6c 61  r *zName, u8 fla
26a08 67 56 61 6c 29 7b 0a 20 20 46 75 6e 63 44 65 66  gVal){.  FuncDef
26a09 20 2a 70 44 65 66 3b 0a 20 20 70 44 65 66 20 3d   *pDef;.  pDef =
26a0a 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
26a0b 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  tion(db, zName, 
26a0c 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
26a0d 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20 20  zName),.        
26a0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a0f 20 20 20 20 20 32 2c 20 53 51 4c 49 54 45 5f 55       2, SQLITE_U
26a10 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 41  TF8, 0);.  if( A
26a11 4c 57 41 59 53 28 70 44 65 66 29 20 29 7b 0a 20  LWAYS(pDef) ){. 
26a12 20 20 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 3d     pDef->flags =
26a13 20 66 6c 61 67 56 61 6c 3b 0a 20 20 7d 0a 7d 0a   flagVal;.  }.}.
26a14 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
26a15 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 4c 49 4b  the built-in LIK
26a16 45 20 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74  E and GLOB funct
26a17 69 6f 6e 73 2e 20 20 54 68 65 20 63 61 73 65 53  ions.  The caseS
26a18 65 6e 73 69 74 69 76 65 0a 2a 2a 20 70 61 72 61  ensitive.** para
26a19 6d 65 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73  meter determines
26a1a 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
26a1b 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  the LIKE operato
26a1c 72 20 69 73 20 63 61 73 65 0a 2a 2a 20 73 65 6e  r is case.** sen
26a1d 73 69 74 69 76 65 2e 20 20 47 4c 4f 42 20 69 73  sitive.  GLOB is
26a1e 20 61 6c 77 61 79 73 20 63 61 73 65 20 73 65 6e   always case sen
26a1f 73 69 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54  sitive..*/.SQLIT
26a20 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
26a21 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69  qlite3RegisterLi
26a22 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69  keFunctions(sqli
26a23 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 63 61 73  te3 *db, int cas
26a24 65 53 65 6e 73 69 74 69 76 65 29 7b 0a 20 20 73  eSensitive){.  s
26a25 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66  truct compareInf
26a26 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 69 66 28 20  o *pInfo;.  if( 
26a27 63 61 73 65 53 65 6e 73 69 74 69 76 65 20 29 7b  caseSensitive ){
26a28 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 28 73 74  .    pInfo = (st
26a29 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  ruct compareInfo
26a2a 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 3b 0a  *)&likeInfoAlt;.
26a2b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e    }else{.    pIn
26a2c 66 6f 20 3d 20 28 73 74 72 75 63 74 20 63 6f 6d  fo = (struct com
26a2d 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49  pareInfo*)&likeI
26a2e 6e 66 6f 4e 6f 72 6d 3b 0a 20 20 7d 0a 20 20 73  nfoNorm;.  }.  s
26a2f 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
26a30 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 32 2c 20  (db, "like", 2, 
26a31 53 51 4c 49 54 45 5f 41 4e 59 2c 20 70 49 6e 66  SQLITE_ANY, pInf
26a32 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20  o, likeFunc, 0, 
26a33 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 72 65  0);.  sqlite3Cre
26a34 61 74 65 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b  ateFunc(db, "lik
26a35 65 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 41 4e  e", 3, SQLITE_AN
26a36 59 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75  Y, pInfo, likeFu
26a37 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c  nc, 0, 0);.  sql
26a38 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
26a39 62 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20 53 51  b, "glob", 2, SQ
26a3a 4c 49 54 45 5f 41 4e 59 2c 20 0a 20 20 20 20 20  LITE_ANY, .     
26a3b 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65   (struct compare
26a3c 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f 2c  Info*)&globInfo,
26a3d 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 30 29 3b   likeFunc, 0,0);
26a3e 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c 61  .  setLikeOptFla
26a3f 67 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 53 51  g(db, "glob", SQ
26a40 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c  LITE_FUNC_LIKE |
26a41 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53   SQLITE_FUNC_CAS
26a42 45 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74  E);.  setLikeOpt
26a43 46 6c 61 67 28 64 62 2c 20 22 6c 69 6b 65 22 2c  Flag(db, "like",
26a44 20 0a 20 20 20 20 20 20 63 61 73 65 53 65 6e 73   .      caseSens
26a45 69 74 69 76 65 20 3f 20 28 53 51 4c 49 54 45 5f  itive ? (SQLITE_
26a46 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c 49  FUNC_LIKE | SQLI
26a47 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 20 3a 20  TE_FUNC_CASE) : 
26a48 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
26a49 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70  );.}../*.** pExp
26a4a 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 65  r points to an e
26a4b 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68 20  xpression which 
26a4c 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e  implements a fun
26a4d 63 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 69 74  ction.  If.** it
26a4e 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20   is appropriate 
26a4f 74 6f 20 61 70 70 6c 79 20 74 68 65 20 4c 49 4b  to apply the LIK
26a50 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 74  E optimization t
26a51 6f 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 0a  o that function.
26a52 2a 2a 20 74 68 65 6e 20 73 65 74 20 61 57 63 5b  ** then set aWc[
26a53 30 5d 20 74 68 72 6f 75 67 68 20 61 57 63 5b 32  0] through aWc[2
26a54 5d 20 74 6f 20 74 68 65 20 77 69 6c 64 63 61 72  ] to the wildcar
26a55 64 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64  d characters and
26a56 0a 2a 2a 20 72 65 74 75 72 6e 20 54 52 55 45 2e  .** return TRUE.
26a57 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f    If the functio
26a58 6e 20 69 73 20 6e 6f 74 20 61 20 4c 49 4b 45 2d  n is not a LIKE-
26a59 73 74 79 6c 65 20 66 75 6e 63 74 69 6f 6e 20 74  style function t
26a5a 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 46 41  hen.** return FA
26a5b 4c 53 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  LSE..*/.SQLITE_P
26a5c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
26a5d 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e  e3IsLikeFunction
26a5e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78  (sqlite3 *db, Ex
26a5f 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a  pr *pExpr, int *
26a60 70 49 73 4e 6f 63 61 73 65 2c 20 63 68 61 72 20  pIsNocase, char 
26a61 2a 61 57 63 29 7b 0a 20 20 46 75 6e 63 44 65 66  *aWc){.  FuncDef
26a62 20 2a 70 44 65 66 3b 0a 20 20 69 66 28 20 70 45   *pDef;.  if( pE
26a63 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43  xpr->op!=TK_FUNC
26a64 54 49 4f 4e 20 0a 20 20 20 7c 7c 20 21 70 45 78  TION .   || !pEx
26a65 70 72 2d 3e 78 2e 70 4c 69 73 74 20 0a 20 20 20  pr->x.pList .   
26a66 7c 7c 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  || pExpr->x.pLis
26a67 74 2d 3e 6e 45 78 70 72 21 3d 32 0a 20 20 29 7b  t->nExpr!=2.  ){
26a68 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
26a69 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 45 78   }.  assert( !Ex
26a6a 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
26a6b 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
26a6c 74 29 20 29 3b 0a 20 20 70 44 65 66 20 3d 20 73  t) );.  pDef = s
26a6d 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
26a6e 6f 6e 28 64 62 2c 20 70 45 78 70 72 2d 3e 75 2e  on(db, pExpr->u.
26a6f 7a 54 6f 6b 65 6e 2c 20 0a 20 20 20 20 20 20 20  zToken, .       
26a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a71 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
26a72 6c 65 6e 33 30 28 70 45 78 70 72 2d 3e 75 2e 7a  len30(pExpr->u.z
26a73 54 6f 6b 65 6e 29 2c 0a 20 20 20 20 20 20 20 20  Token),.        
26a74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26a75 20 20 20 20 20 32 2c 20 53 51 4c 49 54 45 5f 55       2, SQLITE_U
26a76 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 4e  TF8, 0);.  if( N
26a77 45 56 45 52 28 70 44 65 66 3d 3d 30 29 20 7c 7c  EVER(pDef==0) ||
26a78 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20   (pDef->flags & 
26a79 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
26a7a 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
26a7b 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
26a7c 54 68 65 20 6d 65 6d 63 70 79 28 29 20 73 74 61  The memcpy() sta
26a7d 74 65 6d 65 6e 74 20 61 73 73 75 6d 65 73 20 74  tement assumes t
26a7e 68 61 74 20 74 68 65 20 77 69 6c 64 63 61 72 64  hat the wildcard
26a7f 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 0a   characters are.
26a80 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74    ** the first t
26a81 68 72 65 65 20 73 74 61 74 65 6d 65 6e 74 73 20  hree statements 
26a82 69 6e 20 74 68 65 20 63 6f 6d 70 61 72 65 49 6e  in the compareIn
26a83 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  fo structure.  T
26a84 68 65 0a 20 20 2a 2a 20 61 73 73 65 72 74 73 28  he.  ** asserts(
26a85 29 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 76 65  ) that follow ve
26a86 72 69 66 79 20 74 68 61 74 20 61 73 73 75 6d 70  rify that assump
26a87 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 63  tion.  */.  memc
26a88 70 79 28 61 57 63 2c 20 70 44 65 66 2d 3e 70 55  py(aWc, pDef->pU
26a89 73 65 72 44 61 74 61 2c 20 33 29 3b 0a 20 20 61  serData, 3);.  a
26a8a 73 73 65 72 74 28 20 28 63 68 61 72 2a 29 26 6c  ssert( (char*)&l
26a8b 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 3d 20 28 63  ikeInfoAlt == (c
26a8c 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c  har*)&likeInfoAl
26a8d 74 2e 6d 61 74 63 68 41 6c 6c 20 29 3b 0a 20 20  t.matchAll );.  
26a8e 61 73 73 65 72 74 28 20 26 28 28 63 68 61 72 2a  assert( &((char*
26a8f 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 31  )&likeInfoAlt)[1
26a90 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b  ] == (char*)&lik
26a91 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 4f 6e  eInfoAlt.matchOn
26a92 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26  e );.  assert( &
26a93 28 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66  ((char*)&likeInf
26a94 6f 41 6c 74 29 5b 32 5d 20 3d 3d 20 28 63 68 61  oAlt)[2] == (cha
26a95 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e  r*)&likeInfoAlt.
26a96 6d 61 74 63 68 53 65 74 20 29 3b 0a 20 20 2a 70  matchSet );.  *p
26a97 49 73 4e 6f 63 61 73 65 20 3d 20 28 70 44 65 66  IsNocase = (pDef
26a98 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
26a99 5f 46 55 4e 43 5f 43 41 53 45 29 3d 3d 30 3b 0a  _FUNC_CASE)==0;.
26a9a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
26a9b 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c 20 6f 66 20  *.** All all of 
26a9c 74 68 65 20 46 75 6e 63 44 65 66 20 73 74 72 75  the FuncDef stru
26a9d 63 74 75 72 65 73 20 69 6e 20 74 68 65 20 61 42  ctures in the aB
26a9e 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 61 72 72  uiltinFunc[] arr
26a9f 61 79 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20 74  ay above.** to t
26aa0 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69  he global functi
26aa1 6f 6e 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20  on hash table.  
26aa2 54 68 69 73 20 6f 63 63 75 72 73 20 61 74 20 73  This occurs at s
26aa3 74 61 72 74 2d 74 69 6d 65 20 28 61 73 0a 2a 2a  tart-time (as.**
26aa4 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 20 6f   a consequence o
26aa5 66 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  f calling sqlite
26aa6 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29 2e  3_initialize()).
26aa7 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74 68 69  .**.** After thi
26aa8 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 0a 2a  s routine runs.*
26aa9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
26aaa 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
26aab 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63 74  isterGlobalFunct
26aac 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20 2f 2a  ions(void){.  /*
26aad 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  ** The follow
26aae 69 6e 67 20 61 72 72 61 79 20 68 6f 6c 64 73 20  ing array holds 
26aaf 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75 72  FuncDef structur
26ab0 65 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68  es for all of th
26ab1 65 20 66 75 6e 63 74 69 6f 6e 73 0a 20 20 2a 2a  e functions.  **
26ab2 20 64 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73   defined in this
26ab3 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   file..  **.  **
26ab4 20 54 68 65 20 61 72 72 61 79 20 63 61 6e 6e 6f   The array canno
26ab5 74 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 73 69  t be constant si
26ab6 6e 63 65 20 63 68 61 6e 67 65 73 20 61 72 65 20  nce changes are 
26ab7 6d 61 64 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a  made to the.  **
26ab8 20 46 75 6e 63 44 65 66 2e 70 48 61 73 68 20 65   FuncDef.pHash e
26ab9 6c 65 6d 65 6e 74 73 20 61 74 20 73 74 61 72 74  lements at start
26aba 2d 74 69 6d 65 2e 20 20 54 68 65 20 65 6c 65 6d  -time.  The elem
26abb 65 6e 74 73 20 6f 66 20 74 68 69 73 20 61 72 72  ents of this arr
26abc 61 79 0a 20 20 2a 2a 20 61 72 65 20 72 65 61 64  ay.  ** are read
26abd 2d 6f 6e 6c 79 20 61 66 74 65 72 20 69 6e 69 74  -only after init
26abe 69 61 6c 69 7a 61 74 69 6f 6e 20 69 73 20 63 6f  ialization is co
26abf 6d 70 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20 20 73  mplete..  */.  s
26ac0 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
26ac1 20 46 75 6e 63 44 65 66 20 61 42 75 69 6c 74 69   FuncDef aBuilti
26ac2 6e 46 75 6e 63 5b 5d 20 3d 20 7b 0a 20 20 20 20  nFunc[] = {.    
26ac3 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69 6d 2c 20  FUNCTION(ltrim, 
26ac4 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
26ac5 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20  1, 0, trimFunc  
26ac6 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
26ac7 4e 43 54 49 4f 4e 28 6c 74 72 69 6d 2c 20 20 20  NCTION(ltrim,   
26ac8 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 31 2c             2, 1,
26ac9 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20   0, trimFunc    
26aca 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26acb 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20 20 20 20  TION(rtrim,     
26acc 20 20 20 20 20 20 20 20 20 31 2c 20 32 2c 20 30           1, 2, 0
26acd 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20  , trimFunc      
26ace 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
26acf 4f 4e 28 72 74 72 69 6d 2c 20 20 20 20 20 20 20  ON(rtrim,       
26ad0 20 20 20 20 20 20 20 32 2c 20 32 2c 20 30 2c 20         2, 2, 0, 
26ad1 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20  trimFunc        
26ad2 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
26ad3 28 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20  (trim,          
26ad4 20 20 20 20 20 31 2c 20 33 2c 20 30 2c 20 74 72       1, 3, 0, tr
26ad5 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29  imFunc         )
26ad6 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74  ,.    FUNCTION(t
26ad7 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  rim,            
26ad8 20 20 20 32 2c 20 33 2c 20 30 2c 20 74 72 69 6d     2, 3, 0, trim
26ad9 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a  Func         ),.
26ada 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 69 6e      FUNCTION(min
26adb 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26adc 2d 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78  -1, 0, 1, minmax
26add 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20  Func       ),.  
26ade 20 20 46 55 4e 43 54 49 4f 4e 28 6d 69 6e 2c 20    FUNCTION(min, 
26adf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
26ae0 2c 20 30 2c 20 31 2c 20 30 20 20 20 20 20 20 20  , 0, 1, 0       
26ae1 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
26ae2 41 47 47 52 45 47 41 54 45 28 6d 69 6e 2c 20 20  AGGREGATE(min,  
26ae3 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
26ae4 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70  0, 1, minmaxStep
26ae5 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e  ,      minMaxFin
26ae6 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20 46 55 4e  alize ),.    FUN
26ae7 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20 20 20 20  CTION(max,      
26ae8 20 20 20 20 20 20 20 20 20 2d 31 2c 20 31 2c 20           -1, 1, 
26ae9 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20 20  1, minmaxFunc   
26aea 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
26aeb 49 4f 4e 28 6d 61 78 2c 20 20 20 20 20 20 20 20  ION(max,        
26aec 20 20 20 20 20 20 20 20 30 2c 20 31 2c 20 31 2c          0, 1, 1,
26aed 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
26aee 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41    ),.    AGGREGA
26aef 54 45 28 6d 61 78 2c 20 20 20 20 20 20 20 20 20  TE(max,         
26af0 20 20 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 6d        1, 1, 1, m
26af1 69 6e 6d 61 78 53 74 65 70 2c 20 20 20 20 20 20  inmaxStep,      
26af2 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 29  minMaxFinalize )
26af3 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74  ,.    FUNCTION(t
26af4 79 70 65 6f 66 2c 20 20 20 20 20 20 20 20 20 20  ypeof,          
26af5 20 20 20 31 2c 20 30 2c 20 30 2c 20 74 79 70 65     1, 0, 0, type
26af6 6f 66 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a  ofFunc       ),.
26af7 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 65 6e      FUNCTION(len
26af8 67 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 20  gth,            
26af9 20 31 2c 20 30 2c 20 30 2c 20 6c 65 6e 67 74 68   1, 0, 0, length
26afa 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20  Func       ),.  
26afb 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73 74    FUNCTION(subst
26afc 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 32  r,             2
26afd 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46 75  , 0, 0, substrFu
26afe 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  nc       ),.    
26aff 46 55 4e 43 54 49 4f 4e 28 73 75 62 73 74 72 2c  FUNCTION(substr,
26b00 20 20 20 20 20 20 20 20 20 20 20 20 20 33 2c 20               3, 
26b01 30 2c 20 30 2c 20 73 75 62 73 74 72 46 75 6e 63  0, 0, substrFunc
26b02 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
26b03 4e 43 54 49 4f 4e 28 61 62 73 2c 20 20 20 20 20  NCTION(abs,     
26b04 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
26b05 20 30 2c 20 61 62 73 46 75 6e 63 20 20 20 20 20   0, absFunc     
26b06 20 20 20 20 20 29 2c 0a 23 69 66 6e 64 65 66 20       ),.#ifndef 
26b07 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41  SQLITE_OMIT_FLOA
26b08 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 46  TING_POINT.    F
26b09 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20  UNCTION(round,  
26b0a 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
26b0b 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20  , 0, roundFunc  
26b0c 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
26b0d 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20 20 20  CTION(round,    
26b0e 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20            2, 0, 
26b0f 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20 20 20  0, roundFunc    
26b10 20 20 20 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20      ),.#endif.  
26b11 20 20 46 55 4e 43 54 49 4f 4e 28 75 70 70 65 72    FUNCTION(upper
26b12 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31  ,              1
26b13 2c 20 30 2c 20 30 2c 20 75 70 70 65 72 46 75 6e  , 0, 0, upperFun
26b14 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  c        ),.    
26b15 46 55 4e 43 54 49 4f 4e 28 6c 6f 77 65 72 2c 20  FUNCTION(lower, 
26b16 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
26b17 30 2c 20 30 2c 20 6c 6f 77 65 72 46 75 6e 63 20  0, 0, lowerFunc 
26b18 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
26b19 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c  NCTION(coalesce,
26b1a 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c             1, 0,
26b1b 20 30 2c 20 30 20 20 20 20 20 20 20 20 20 20 20   0, 0           
26b1c 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26b1d 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20  TION(coalesce,  
26b1e 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30           0, 0, 0
26b1f 2c 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20  , 0             
26b20 20 20 20 29 2c 0a 2f 2a 20 20 46 55 4e 43 54 49     ),./*  FUNCTI
26b21 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20  ON(coalesce,    
26b22 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20        -1, 0, 0, 
26b23 69 66 6e 75 6c 6c 46 75 6e 63 20 20 20 20 20 20  ifnullFunc      
26b24 20 29 2c 20 2a 2f 0a 20 20 20 20 7b 2d 31 2c 53   ), */.    {-1,S
26b25 51 4c 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54  QLITE_UTF8,SQLIT
26b26 45 5f 46 55 4e 43 5f 43 4f 41 4c 45 53 43 45 2c  E_FUNC_COALESCE,
26b27 30 2c 30 2c 69 66 6e 75 6c 6c 46 75 6e 63 2c 30  0,0,ifnullFunc,0
26b28 2c 30 2c 22 63 6f 61 6c 65 73 63 65 22 2c 30 7d  ,0,"coalesce",0}
26b29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 68  ,.    FUNCTION(h
26b2a 65 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ex,             
26b2b 20 20 20 31 2c 20 30 2c 20 30 2c 20 68 65 78 46     1, 0, 0, hexF
26b2c 75 6e 63 20 20 20 20 20 20 20 20 20 20 29 2c 0a  unc          ),.
26b2d 2f 2a 20 20 46 55 4e 43 54 49 4f 4e 28 69 66 6e  /*  FUNCTION(ifn
26b2e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  ull,            
26b2f 20 32 2c 20 30 2c 20 30 2c 20 69 66 6e 75 6c 6c   2, 0, 0, ifnull
26b30 46 75 6e 63 20 20 20 20 20 20 20 29 2c 20 2a 2f  Func       ), */
26b31 0a 20 20 20 20 7b 32 2c 53 51 4c 49 54 45 5f 55  .    {2,SQLITE_U
26b32 54 46 38 2c 53 51 4c 49 54 45 5f 46 55 4e 43 5f  TF8,SQLITE_FUNC_
26b33 43 4f 41 4c 45 53 43 45 2c 30 2c 30 2c 69 66 6e  COALESCE,0,0,ifn
26b34 75 6c 6c 46 75 6e 63 2c 30 2c 30 2c 22 69 66 6e  ullFunc,0,0,"ifn
26b35 75 6c 6c 22 2c 30 7d 2c 0a 20 20 20 20 46 55 4e  ull",0},.    FUN
26b36 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 2c 20 20 20  CTION(random,   
26b37 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20            0, 0, 
26b38 30 2c 20 72 61 6e 64 6f 6d 46 75 6e 63 20 20 20  0, randomFunc   
26b39 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
26b3a 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c 6f 62 2c 20  ION(randomblob, 
26b3b 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
26b3c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20 20 20 20 20   randomBlob     
26b3d 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
26b3e 4e 28 6e 75 6c 6c 69 66 2c 20 20 20 20 20 20 20  N(nullif,       
26b3f 20 20 20 20 20 20 32 2c 20 30 2c 20 31 2c 20 6e        2, 0, 1, n
26b40 75 6c 6c 69 66 46 75 6e 63 20 20 20 20 20 20 20  ullifFunc       
26b41 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
26b42 73 71 6c 69 74 65 5f 76 65 72 73 69 6f 6e 2c 20  sqlite_version, 
26b43 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 76 65 72      0, 0, 0, ver
26b44 73 69 6f 6e 46 75 6e 63 20 20 20 20 20 20 29 2c  sionFunc      ),
26b45 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 71  .    FUNCTION(sq
26b46 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 2c 20  lite_source_id, 
26b47 20 20 30 2c 20 30 2c 20 30 2c 20 73 6f 75 72 63    0, 0, 0, sourc
26b48 65 69 64 46 75 6e 63 20 20 20 20 20 29 2c 0a 20  eidFunc     ),. 
26b49 20 20 20 46 55 4e 43 54 49 4f 4e 28 71 75 6f 74     FUNCTION(quot
26b4a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
26b4b 31 2c 20 30 2c 20 30 2c 20 71 75 6f 74 65 46 75  1, 0, 0, quoteFu
26b4c 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  nc        ),.   
26b4d 20 46 55 4e 43 54 49 4f 4e 28 6c 61 73 74 5f 69   FUNCTION(last_i
26b4e 6e 73 65 72 74 5f 72 6f 77 69 64 2c 20 20 30 2c  nsert_rowid,  0,
26b4f 20 30 2c 20 30 2c 20 6c 61 73 74 5f 69 6e 73 65   0, 0, last_inse
26b50 72 74 5f 72 6f 77 69 64 29 2c 0a 20 20 20 20 46  rt_rowid),.    F
26b51 55 4e 43 54 49 4f 4e 28 63 68 61 6e 67 65 73 2c  UNCTION(changes,
26b52 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30              0, 0
26b53 2c 20 30 2c 20 63 68 61 6e 67 65 73 20 20 20 20  , 0, changes    
26b54 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
26b55 43 54 49 4f 4e 28 74 6f 74 61 6c 5f 63 68 61 6e  CTION(total_chan
26b56 67 65 73 2c 20 20 20 20 20 20 30 2c 20 30 2c 20  ges,      0, 0, 
26b57 30 2c 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  0, total_changes
26b58 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
26b59 49 4f 4e 28 72 65 70 6c 61 63 65 2c 20 20 20 20  ION(replace,    
26b5a 20 20 20 20 20 20 20 20 33 2c 20 30 2c 20 30 2c          3, 0, 0,
26b5b 20 72 65 70 6c 61 63 65 46 75 6e 63 20 20 20 20   replaceFunc    
26b5c 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
26b5d 4e 28 7a 65 72 6f 62 6c 6f 62 2c 20 20 20 20 20  N(zeroblob,     
26b5e 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 7a        1, 0, 0, z
26b5f 65 72 6f 62 6c 6f 62 46 75 6e 63 20 20 20 20 20  eroblobFunc     
26b60 29 2c 0a 20 20 23 69 66 64 65 66 20 53 51 4c 49  ),.  #ifdef SQLI
26b61 54 45 5f 53 4f 55 4e 44 45 58 0a 20 20 20 20 46  TE_SOUNDEX.    F
26b62 55 4e 43 54 49 4f 4e 28 73 6f 75 6e 64 65 78 2c  UNCTION(soundex,
26b63 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
26b64 2c 20 30 2c 20 73 6f 75 6e 64 65 78 46 75 6e 63  , 0, soundexFunc
26b65 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e 64 69        ),.  #endi
26b66 66 0a 20 20 23 69 66 6e 64 65 66 20 53 51 4c 49  f.  #ifndef SQLI
26b67 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
26b68 45 4e 53 49 4f 4e 0a 20 20 20 20 46 55 4e 43 54  ENSION.    FUNCT
26b69 49 4f 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 69  ION(load_extensi
26b6a 6f 6e 2c 20 20 20 20 20 31 2c 20 30 2c 20 30 2c  on,     1, 0, 0,
26b6b 20 6c 6f 61 64 45 78 74 20 20 20 20 20 20 20 20   loadExt        
26b6c 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
26b6d 4e 28 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  N(load_extension
26b6e 2c 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 6c  ,     2, 0, 0, l
26b6f 6f 61 64 45 78 74 20 20 20 20 20 20 20 20 20 20  oadExt          
26b70 29 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20 20 20  ),.  #endif.    
26b71 41 47 47 52 45 47 41 54 45 28 73 75 6d 2c 20 20  AGGREGATE(sum,  
26b72 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
26b73 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c 20 20  0, 0, sumStep,  
26b74 20 20 20 20 20 20 20 73 75 6d 46 69 6e 61 6c 69         sumFinali
26b75 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47  ze    ),.    AGG
26b76 52 45 47 41 54 45 28 74 6f 74 61 6c 2c 20 20 20  REGATE(total,   
26b77 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
26b78 30 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20  0, sumStep,     
26b79 20 20 20 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a      totalFinaliz
26b7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52  e    ),.    AGGR
26b7b 45 47 41 54 45 28 61 76 67 2c 20 20 20 20 20 20  EGATE(avg,      
26b7c 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
26b7d 2c 20 73 75 6d 53 74 65 70 2c 20 20 20 20 20 20  , sumStep,      
26b7e 20 20 20 61 76 67 46 69 6e 61 6c 69 7a 65 20 20     avgFinalize  
26b7f 20 20 29 2c 0a 20 2f 2a 20 41 47 47 52 45 47 41    ),. /* AGGREGA
26b80 54 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 20  TE(count,       
26b81 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63        0, 0, 0, c
26b82 6f 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 20  ountStep,       
26b83 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29  countFinalize  )
26b84 2c 20 2a 2f 0a 20 20 20 20 7b 30 2c 53 51 4c 49  , */.    {0,SQLI
26b85 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 46  TE_UTF8,SQLITE_F
26b86 55 4e 43 5f 43 4f 55 4e 54 2c 30 2c 30 2c 30 2c  UNC_COUNT,0,0,0,
26b87 63 6f 75 6e 74 53 74 65 70 2c 63 6f 75 6e 74 46  countStep,countF
26b88 69 6e 61 6c 69 7a 65 2c 22 63 6f 75 6e 74 22 2c  inalize,"count",
26b89 30 7d 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54  0},.    AGGREGAT
26b8a 45 28 63 6f 75 6e 74 2c 20 20 20 20 20 20 20 20  E(count,        
26b8b 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 63 6f       1, 0, 0, co
26b8c 75 6e 74 53 74 65 70 2c 20 20 20 20 20 20 20 63  untStep,       c
26b8d 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 20 20 29 2c  ountFinalize  ),
26b8e 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28 67  .    AGGREGATE(g
26b8f 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 20 20 20  roup_concat,    
26b90 20 20 31 2c 20 30 2c 20 30 2c 20 67 72 6f 75 70    1, 0, 0, group
26b91 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 72 6f 75  ConcatStep, grou
26b92 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65 29  pConcatFinalize)
26b93 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28  ,.    AGGREGATE(
26b94 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c 20 20 20  group_concat,   
26b95 20 20 20 32 2c 20 30 2c 20 30 2c 20 67 72 6f 75     2, 0, 0, grou
26b96 70 43 6f 6e 63 61 74 53 74 65 70 2c 20 67 72 6f  pConcatStep, gro
26b97 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c 69 7a 65  upConcatFinalize
26b98 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b 45 46 55  ),.  .    LIKEFU
26b99 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26 67 6c 6f  NC(glob, 2, &glo
26b9a 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45 5f 46 55  bInfo, SQLITE_FU
26b9b 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46  NC_LIKE|SQLITE_F
26b9c 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20 23 69 66  UNC_CASE),.  #if
26b9d 64 65 66 20 53 51 4c 49 54 45 5f 43 41 53 45 5f  def SQLITE_CASE_
26b9e 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 0a 20  SENSITIVE_LIKE. 
26b9f 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65     LIKEFUNC(like
26ba0 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 6f 41 6c  , 2, &likeInfoAl
26ba1 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c  t, SQLITE_FUNC_L
26ba2 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f  IKE|SQLITE_FUNC_
26ba3 43 41 53 45 29 2c 0a 20 20 20 20 4c 49 4b 45 46  CASE),.    LIKEF
26ba4 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20 26 6c 69  UNC(like, 3, &li
26ba5 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 4c 49 54  keInfoAlt, SQLIT
26ba6 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49  E_FUNC_LIKE|SQLI
26ba7 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20  TE_FUNC_CASE),. 
26ba8 20 23 65 6c 73 65 0a 20 20 20 20 4c 49 4b 45 46   #else.    LIKEF
26ba9 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 26 6c 69  UNC(like, 2, &li
26baa 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53 51 4c 49  keInfoNorm, SQLI
26bab 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 2c 0a 20  TE_FUNC_LIKE),. 
26bac 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b 65     LIKEFUNC(like
26bad 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e 6f  , 3, &likeInfoNo
26bae 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  rm, SQLITE_FUNC_
26baf 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64 69 66 0a  LIKE),.  #endif.
26bb0 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69 3b 0a 20    };..  int i;. 
26bb1 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48   FuncDefHash *pH
26bb2 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75  ash = &GLOBAL(Fu
26bb3 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74  ncDefHash, sqlit
26bb4 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e  e3GlobalFunction
26bb5 73 29 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a 61  s);.  FuncDef *a
26bb6 46 75 6e 63 20 3d 20 28 46 75 6e 63 44 65 66 2a  Func = (FuncDef*
26bb7 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66  )&GLOBAL(FuncDef
26bb8 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e 63 29 3b  , aBuiltinFunc);
26bb9 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41  ..  for(i=0; i<A
26bba 72 72 61 79 53 69 7a 65 28 61 42 75 69 6c 74 69  rraySize(aBuilti
26bbb 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b 0a 20 20  nFunc); i++){.  
26bbc 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66    sqlite3FuncDef
26bbd 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26 61  Insert(pHash, &a
26bbe 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20  Func[i]);.  }.  
26bbf 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 44  sqlite3RegisterD
26bc0 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73  ateTimeFunctions
26bc1 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ();.}../********
26bc2 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 75  ****** End of fu
26bc3 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nc.c ***********
26bc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bc6 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
26bc7 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
26bc8 65 20 66 6b 65 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e fkey.c *******
26bc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bcb 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 0a 2a 2a 20  *****/./*.**.** 
26bcc 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
26bcd 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
26bce 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
26bcf 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
26bd0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
26bd1 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
26bd2 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
26bd3 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
26bd4 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
26bd5 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
26bd6 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
26bd7 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
26bd8 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
26bd9 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
26bda 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
26bdb 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
26bdc 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
26bdd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26bdf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26be1 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
26be2 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
26be3 6f 64 65 20 75 73 65 64 20 62 79 20 74 68 65 20  ode used by the 
26be4 63 6f 6d 70 69 6c 65 72 20 74 6f 20 61 64 64 20  compiler to add 
26be5 66 6f 72 65 69 67 6e 20 6b 65 79 0a 2a 2a 20 73  foreign key.** s
26be6 75 70 70 6f 72 74 20 74 6f 20 63 6f 6d 70 69 6c  upport to compil
26be7 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
26be8 73 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  s..*/..#ifndef S
26be9 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
26bea 47 4e 5f 4b 45 59 0a 23 69 66 6e 64 65 66 20 53  GN_KEY.#ifndef S
26beb 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
26bec 45 52 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72 72  ER../*.** Deferr
26bed 65 64 20 61 6e 64 20 49 6d 6d 65 64 69 61 74 65  ed and Immediate
26bee 20 46 4b 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d   FKs.** --------
26bef 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26bf0 2d 2d 0a 2a 2a 0a 2a 2a 20 46 6f 72 65 69 67 6e  --.**.** Foreign
26bf1 20 6b 65 79 73 20 69 6e 20 53 51 4c 69 74 65 20   keys in SQLite 
26bf2 63 6f 6d 65 20 69 6e 20 74 77 6f 20 66 6c 61 76  come in two flav
26bf3 6f 75 72 73 3a 20 64 65 66 65 72 72 65 64 20 61  ours: deferred a
26bf4 6e 64 20 69 6d 6d 65 64 69 61 74 65 2e 0a 2a 2a  nd immediate..**
26bf5 20 49 66 20 61 6e 20 69 6d 6d 65 64 69 61 74 65   If an immediate
26bf6 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
26bf7 73 74 72 61 69 6e 74 20 69 73 20 76 69 6f 6c 61  straint is viola
26bf8 74 65 64 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53  ted, SQLITE_CONS
26bf9 54 52 41 49 4e 54 0a 2a 2a 20 69 73 20 72 65 74  TRAINT.** is ret
26bfa 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 63 75  urned and the cu
26bfb 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 20  rrent statement 
26bfc 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c  transaction roll
26bfd 65 64 20 62 61 63 6b 2e 20 49 66 20 61 20 0a 2a  ed back. If a .*
26bfe 2a 20 64 65 66 65 72 72 65 64 20 66 6f 72 65 69  * deferred forei
26bff 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
26c00 74 20 69 73 20 76 69 6f 6c 61 74 65 64 2c 20 6e  t is violated, n
26c01 6f 20 61 63 74 69 6f 6e 20 69 73 20 74 61 6b 65  o action is take
26c02 6e 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  n .** immediatel
26c03 79 2e 20 48 6f 77 65 76 65 72 20 69 66 20 74 68  y. However if th
26c04 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 61 74  e application at
26c05 74 65 6d 70 74 73 20 74 6f 20 63 6f 6d 6d 69 74  tempts to commit
26c06 20 74 68 65 20 0a 2a 2a 20 74 72 61 6e 73 61 63   the .** transac
26c07 74 69 6f 6e 20 62 65 66 6f 72 65 20 66 69 78 69  tion before fixi
26c08 6e 67 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  ng the constrain
26c09 74 20 76 69 6f 6c 61 74 69 6f 6e 2c 20 74 68 65  t violation, the
26c0a 20 61 74 74 65 6d 70 74 20 66 61 69 6c 73 2e 0a   attempt fails..
26c0b 2a 2a 0a 2a 2a 20 44 65 66 65 72 72 65 64 20 63  **.** Deferred c
26c0c 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 69  onstraints are i
26c0d 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67  mplemented using
26c0e 20 61 20 73 69 6d 70 6c 65 20 63 6f 75 6e 74 65   a simple counte
26c0f 72 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20  r associated.** 
26c10 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
26c11 65 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 63 6f  e handle. The co
26c12 75 6e 74 65 72 20 69 73 20 73 65 74 20 74 6f 20  unter is set to 
26c13 7a 65 72 6f 20 65 61 63 68 20 74 69 6d 65 20 61  zero each time a
26c14 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72   .** database tr
26c15 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65  ansaction is ope
26c16 6e 65 64 2e 20 45 61 63 68 20 74 69 6d 65 20 61  ned. Each time a
26c17 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78   statement is ex
26c18 65 63 75 74 65 64 20 0a 2a 2a 20 74 68 61 74 20  ecuted .** that 
26c19 63 61 75 73 65 73 20 61 20 66 6f 72 65 69 67 6e  causes a foreign
26c1a 20 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e 2c 20   key violation, 
26c1b 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 69  the counter is i
26c1c 6e 63 72 65 6d 65 6e 74 65 64 2e 20 45 61 63 68  ncremented. Each
26c1d 0a 2a 2a 20 74 69 6d 65 20 61 20 73 74 61 74 65  .** time a state
26c1e 6d 65 6e 74 20 69 73 20 65 78 65 63 75 74 65 64  ment is executed
26c1f 20 74 68 61 74 20 72 65 6d 6f 76 65 73 20 61 6e   that removes an
26c20 20 65 78 69 73 74 69 6e 67 20 76 69 6f 6c 61 74   existing violat
26c21 69 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20  ion from.** the 
26c22 64 61 74 61 62 61 73 65 2c 20 74 68 65 20 63 6f  database, the co
26c23 75 6e 74 65 72 20 69 73 20 64 65 63 72 65 6d 65  unter is decreme
26c24 6e 74 65 64 2e 20 57 68 65 6e 20 74 68 65 20 74  nted. When the t
26c25 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a  ransaction is.**
26c26 20 63 6f 6d 6d 69 74 74 65 64 2c 20 74 68 65 20   committed, the 
26c27 63 6f 6d 6d 69 74 20 66 61 69 6c 73 20 69 66 20  commit fails if 
26c28 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
26c29 65 20 6f 66 20 74 68 65 20 63 6f 75 6e 74 65 72  e of the counter
26c2a 20 69 73 0a 2a 2a 20 67 72 65 61 74 65 72 20 74   is.** greater t
26c2b 68 61 6e 20 7a 65 72 6f 2e 20 54 68 69 73 20 73  han zero. This s
26c2c 63 68 65 6d 65 20 68 61 73 20 74 77 6f 20 62 69  cheme has two bi
26c2d 67 20 64 72 61 77 62 61 63 6b 73 3a 0a 2a 2a 0a  g drawbacks:.**.
26c2e 2a 2a 20 20 20 2a 20 57 68 65 6e 20 61 20 63 6f  **   * When a co
26c2f 6d 6d 69 74 20 66 61 69 6c 73 20 64 75 65 20 74  mmit fails due t
26c30 6f 20 61 20 64 65 66 65 72 72 65 64 20 66 6f 72  o a deferred for
26c31 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
26c32 69 6e 74 2c 20 0a 2a 2a 20 20 20 20 20 74 68 65  int, .**     the
26c33 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74 6f 20  re is no way to 
26c34 74 65 6c 6c 20 77 68 69 63 68 20 66 6f 72 65 69  tell which forei
26c35 67 6e 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73  gn constraint is
26c36 20 6e 6f 74 20 73 61 74 69 73 66 69 65 64 2c 0a   not satisfied,.
26c37 2a 2a 20 20 20 20 20 6f 72 20 77 68 69 63 68 20  **     or which 
26c38 72 6f 77 20 69 74 20 69 73 20 6e 6f 74 20 73 61  row it is not sa
26c39 74 69 73 66 69 65 64 20 66 6f 72 2e 0a 2a 2a 0a  tisfied for..**.
26c3a 2a 2a 20 20 20 2a 20 49 66 20 74 68 65 20 64 61  **   * If the da
26c3b 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 73 20  tabase contains 
26c3c 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 69 6f 6c  foreign key viol
26c3d 61 74 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 20  ations when the 
26c3e 0a 2a 2a 20 20 20 20 20 74 72 61 6e 73 61 63 74  .**     transact
26c3f 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2c 20 74  ion is opened, t
26c40 68 69 73 20 6d 61 79 20 63 61 75 73 65 20 74 68  his may cause th
26c41 65 20 6d 65 63 68 61 6e 69 73 6d 20 74 6f 20 6d  e mechanism to m
26c42 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  alfunction..**.*
26c43 2a 20 44 65 73 70 69 74 65 20 74 68 65 73 65 20  * Despite these 
26c44 70 72 6f 62 6c 65 6d 73 2c 20 74 68 69 73 20 61  problems, this a
26c45 70 70 72 6f 61 63 68 20 69 73 20 61 64 6f 70 74  pproach is adopt
26c46 65 64 20 61 73 20 69 74 20 73 65 65 6d 73 20 73  ed as it seems s
26c47 69 6d 70 6c 65 72 0a 2a 2a 20 74 68 61 6e 20 74  impler.** than t
26c48 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65 73 2e  he alternatives.
26c49 0a 2a 2a 0a 2a 2a 20 49 4e 53 45 52 54 20 6f 70  .**.** INSERT op
26c4a 65 72 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20  erations:.**.** 
26c4b 20 20 49 2e 31 29 20 46 6f 72 20 65 61 63 68 20    I.1) For each 
26c4c 46 4b 20 66 6f 72 20 77 68 69 63 68 20 74 68 65  FK for which the
26c4d 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 63 68   table is the ch
26c4e 69 6c 64 20 74 61 62 6c 65 2c 20 73 65 61 72 63  ild table, searc
26c4f 68 0a 2a 2a 20 20 20 20 20 20 20 20 74 68 65 20  h.**        the 
26c50 70 61 72 65 6e 74 20 74 61 62 6c 65 20 66 6f 72  parent table for
26c51 20 61 20 6d 61 74 63 68 2e 20 49 66 20 6e 6f 6e   a match. If non
26c52 65 20 69 73 20 66 6f 75 6e 64 20 69 6e 63 72 65  e is found incre
26c53 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 20 20 20 20  ment the.**     
26c54 20 20 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f     constraint co
26c55 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 49  unter..**.**   I
26c56 2e 32 29 20 46 6f 72 20 65 61 63 68 20 46 4b 20  .2) For each FK 
26c57 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 74 61  for which the ta
26c58 62 6c 65 20 69 73 20 74 68 65 20 70 61 72 65 6e  ble is the paren
26c59 74 20 74 61 62 6c 65 2c 20 0a 2a 2a 20 20 20 20  t table, .**    
26c5a 20 20 20 20 73 65 61 72 63 68 20 74 68 65 20 63      search the c
26c5b 68 69 6c 64 20 74 61 62 6c 65 20 66 6f 72 20 72  hild table for r
26c5c 6f 77 73 20 74 68 61 74 20 63 6f 72 72 65 73 70  ows that corresp
26c5d 6f 6e 64 20 74 6f 20 74 68 65 20 6e 65 77 0a 2a  ond to the new.*
26c5e 2a 20 20 20 20 20 20 20 20 72 6f 77 20 69 6e 20  *        row in 
26c5f 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
26c60 2e 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  . Decrement the 
26c61 63 6f 75 6e 74 65 72 20 66 6f 72 20 65 61 63 68  counter for each
26c62 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 66   row.**        f
26c63 6f 75 6e 64 20 28 61 73 20 74 68 65 20 63 6f 6e  ound (as the con
26c64 73 74 72 61 69 6e 74 20 69 73 20 6e 6f 77 20 73  straint is now s
26c65 61 74 69 73 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a  atisfied)..**.**
26c66 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f   DELETE operatio
26c67 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 44 2e 31 29  ns:.**.**   D.1)
26c68 20 46 6f 72 20 65 61 63 68 20 46 4b 20 66 6f 72   For each FK for
26c69 20 77 68 69 63 68 20 74 68 65 20 74 61 62 6c 65   which the table
26c6a 20 69 73 20 74 68 65 20 63 68 69 6c 64 20 74 61   is the child ta
26c6b 62 6c 65 2c 20 0a 2a 2a 20 20 20 20 20 20 20 20  ble, .**        
26c6c 73 65 61 72 63 68 20 74 68 65 20 70 61 72 65 6e  search the paren
26c6d 74 20 74 61 62 6c 65 20 66 6f 72 20 61 20 72 6f  t table for a ro
26c6e 77 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e  w that correspon
26c6f 64 73 20 74 6f 20 74 68 65 20 0a 2a 2a 20 20 20  ds to the .**   
26c70 20 20 20 20 20 64 65 6c 65 74 65 64 20 72 6f 77       deleted row
26c71 20 69 6e 20 74 68 65 20 63 68 69 6c 64 20 74 61   in the child ta
26c72 62 6c 65 2e 20 49 66 20 73 75 63 68 20 61 20 72  ble. If such a r
26c73 6f 77 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c  ow is not found,
26c74 20 0a 2a 2a 20 20 20 20 20 20 20 20 64 65 63 72   .**        decr
26c75 65 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 65  ement the counte
26c76 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 2e 32 29 20  r..**.**   D.2) 
26c77 46 6f 72 20 65 61 63 68 20 46 4b 20 66 6f 72 20  For each FK for 
26c78 77 68 69 63 68 20 74 68 65 20 74 61 62 6c 65 20  which the table 
26c79 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 74 61  is the parent ta
26c7a 62 6c 65 2c 20 73 65 61 72 63 68 20 0a 2a 2a 20  ble, search .** 
26c7b 20 20 20 20 20 20 20 74 68 65 20 63 68 69 6c 64         the child
26c7c 20 74 61 62 6c 65 20 66 6f 72 20 72 6f 77 73 20   table for rows 
26c7d 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 20  that correspond 
26c7e 74 6f 20 74 68 65 20 64 65 6c 65 74 65 64 20 72  to the deleted r
26c7f 6f 77 20 0a 2a 2a 20 20 20 20 20 20 20 20 69 6e  ow .**        in
26c80 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
26c81 65 2e 20 46 6f 72 20 65 61 63 68 20 66 6f 75 6e  e. For each foun
26c82 64 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  d increment the 
26c83 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 55  counter..**.** U
26c84 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e 73  PDATE operations
26c85 3a 0a 2a 2a 0a 2a 2a 20 20 20 41 6e 20 55 50 44  :.**.**   An UPD
26c86 41 54 45 20 63 6f 6d 6d 61 6e 64 20 72 65 71 75  ATE command requ
26c87 69 72 65 73 20 74 68 61 74 20 61 6c 6c 20 34 20  ires that all 4 
26c88 73 74 65 70 73 20 61 62 6f 76 65 20 61 72 65 20  steps above are 
26c89 74 61 6b 65 6e 2c 20 62 75 74 20 6f 6e 6c 79 0a  taken, but only.
26c8a 2a 2a 20 20 20 66 6f 72 20 46 4b 20 63 6f 6e 73  **   for FK cons
26c8b 74 72 61 69 6e 74 73 20 66 6f 72 20 77 68 69 63  traints for whic
26c8c 68 20 74 68 65 20 61 66 66 65 63 74 65 64 20 63  h the affected c
26c8d 6f 6c 75 6d 6e 73 20 61 72 65 20 61 63 74 75 61  olumns are actua
26c8e 6c 6c 79 20 0a 2a 2a 20 20 20 6d 6f 64 69 66 69  lly .**   modifi
26c8f 65 64 20 28 76 61 6c 75 65 73 20 6d 75 73 74 20  ed (values must 
26c90 62 65 20 63 6f 6d 70 61 72 65 64 20 61 74 20 72  be compared at r
26c91 75 6e 74 69 6d 65 29 2e 0a 2a 2a 0a 2a 2a 20 4e  untime)..**.** N
26c92 6f 74 65 20 74 68 61 74 20 49 2e 31 20 61 6e 64  ote that I.1 and
26c93 20 44 2e 31 20 61 72 65 20 76 65 72 79 20 73 69   D.1 are very si
26c94 6d 69 6c 61 72 20 6f 70 65 72 61 74 69 6f 6e 73  milar operations
26c95 2c 20 61 73 20 61 72 65 20 49 2e 32 20 61 6e 64  , as are I.2 and
26c96 20 44 2e 32 2e 0a 2a 2a 20 54 68 69 73 20 73 69   D.2..** This si
26c97 6d 70 6c 69 66 69 65 73 20 74 68 65 20 69 6d 70  mplifies the imp
26c98 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 20 62 69  lementation a bi
26c99 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65  t..**.** For the
26c9a 20 70 75 72 70 6f 73 65 73 20 6f 66 20 69 6d 6d   purposes of imm
26c9b 65 64 69 61 74 65 20 46 4b 20 63 6f 6e 73 74 72  ediate FK constr
26c9c 61 69 6e 74 73 2c 20 74 68 65 20 4f 52 20 52 45  aints, the OR RE
26c9d 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63 74 0a 2a  PLACE conflict.*
26c9e 2a 20 72 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20  * resolution is 
26c9f 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 64 65  considered to de
26ca0 6c 65 74 65 20 72 6f 77 73 20 62 65 66 6f 72 65  lete rows before
26ca1 20 74 68 65 20 6e 65 77 20 72 6f 77 20 69 73 20   the new row is 
26ca2 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 20 49 66 20  inserted..** If 
26ca3 61 20 64 65 6c 65 74 65 20 63 61 75 73 65 64 20  a delete caused 
26ca4 62 79 20 4f 52 20 52 45 50 4c 41 43 45 20 76 69  by OR REPLACE vi
26ca5 6f 6c 61 74 65 73 20 61 6e 20 46 4b 20 63 6f 6e  olates an FK con
26ca6 73 74 72 61 69 6e 74 2c 20 61 6e 20 65 78 63 65  straint, an exce
26ca7 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 74 68 72 6f  ption.** is thro
26ca8 77 6e 2c 20 65 76 65 6e 20 69 66 20 74 68 65 20  wn, even if the 
26ca9 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 77 6f  FK constraint wo
26caa 75 6c 64 20 62 65 20 73 61 74 69 73 66 69 65 64  uld be satisfied
26cab 20 61 66 74 65 72 20 74 68 65 20 6e 65 77 20 0a   after the new .
26cac 2a 2a 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74  ** row is insert
26cad 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6d 6d 65 64 69  ed..**.** Immedi
26cae 61 74 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ate constraints 
26caf 61 72 65 20 75 73 75 61 6c 6c 79 20 68 61 6e 64  are usually hand
26cb0 6c 65 64 20 73 69 6d 69 6c 61 72 6c 79 2e 20 54  led similarly. T
26cb1 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e  he only differen
26cb2 63 65 20 0a 2a 2a 20 69 73 20 74 68 61 74 20 74  ce .** is that t
26cb3 68 65 20 63 6f 75 6e 74 65 72 20 75 73 65 64 20  he counter used 
26cb4 69 73 20 73 74 6f 72 65 64 20 61 73 20 70 61 72  is stored as par
26cb5 74 20 6f 66 20 65 61 63 68 20 69 6e 64 69 76 69  t of each indivi
26cb6 64 75 61 6c 20 73 74 61 74 65 6d 65 6e 74 0a 2a  dual statement.*
26cb7 2a 20 6f 62 6a 65 63 74 20 28 73 74 72 75 63 74  * object (struct
26cb8 20 56 64 62 65 29 2e 20 49 66 2c 20 61 66 74 65   Vdbe). If, afte
26cb9 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  r the statement 
26cba 68 61 73 20 72 75 6e 2c 20 69 74 73 20 69 6d 6d  has run, its imm
26cbb 65 64 69 61 74 65 0a 2a 2a 20 63 6f 6e 73 74 72  ediate.** constr
26cbc 61 69 6e 74 20 63 6f 75 6e 74 65 72 20 69 73 20  aint counter is 
26cbd 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72  greater than zer
26cbe 6f 2c 20 69 74 20 72 65 74 75 72 6e 73 20 53 51  o, it returns SQ
26cbf 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 0a  LITE_CONSTRAINT.
26cc0 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74 65  ** and the state
26cc1 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
26cc2 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   is rolled back.
26cc3 20 41 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 73   An exception is
26cc4 20 61 6e 20 49 4e 53 45 52 54 0a 2a 2a 20 73 74   an INSERT.** st
26cc5 61 74 65 6d 65 6e 74 20 74 68 61 74 20 69 6e 73  atement that ins
26cc6 65 72 74 73 20 61 20 73 69 6e 67 6c 65 20 72 6f  erts a single ro
26cc7 77 20 6f 6e 6c 79 20 28 6e 6f 20 74 72 69 67 67  w only (no trigg
26cc8 65 72 73 29 2e 20 49 6e 20 74 68 69 73 20 63 61  ers). In this ca
26cc9 73 65 2c 0a 2a 2a 20 69 6e 73 74 65 61 64 20 6f  se,.** instead o
26cca 66 20 75 73 69 6e 67 20 61 20 63 6f 75 6e 74 65  f using a counte
26ccb 72 2c 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20  r, an exception 
26ccc 69 73 20 74 68 72 6f 77 6e 20 69 6d 6d 65 64 69  is thrown immedi
26ccd 61 74 65 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20  ately if the.** 
26cce 49 4e 53 45 52 54 20 76 69 6f 6c 61 74 65 73 20  INSERT violates 
26ccf 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  a foreign key co
26cd0 6e 73 74 72 61 69 6e 74 2e 20 54 68 69 73 20 69  nstraint. This i
26cd1 73 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 73  s necessary as s
26cd2 75 63 68 0a 2a 2a 20 61 6e 20 49 4e 53 45 52 54  uch.** an INSERT
26cd3 20 64 6f 65 73 20 6e 6f 74 20 6f 70 65 6e 20 61   does not open a
26cd4 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
26cd5 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 4f  action..**.** TO
26cd6 44 4f 3a 20 48 6f 77 20 73 68 6f 75 6c 64 20 64  DO: How should d
26cd7 72 6f 70 70 69 6e 67 20 61 20 74 61 62 6c 65 20  ropping a table 
26cd8 62 65 20 68 61 6e 64 6c 65 64 3f 20 48 6f 77 20  be handled? How 
26cd9 73 68 6f 75 6c 64 20 72 65 6e 61 6d 69 6e 67 20  should renaming 
26cda 61 20 0a 2a 2a 20 74 61 62 6c 65 20 62 65 20 68  a .** table be h
26cdb 61 6e 64 6c 65 64 3f 0a 2a 2a 0a 2a 2a 0a 2a 2a  andled?.**.**.**
26cdc 20 51 75 65 72 79 20 41 50 49 20 4e 6f 74 65 73   Query API Notes
26cdd 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .** ------------
26cde 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65  ---.**.** Before
26cdf 20 63 6f 64 69 6e 67 20 61 6e 20 55 50 44 41 54   coding an UPDAT
26ce0 45 20 6f 72 20 44 45 4c 45 54 45 20 72 6f 77 20  E or DELETE row 
26ce1 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 63  operation, the c
26ce2 6f 64 65 2d 67 65 6e 65 72 61 74 6f 72 0a 2a 2a  ode-generator.**
26ce3 20 66 6f 72 20 74 68 6f 73 65 20 74 77 6f 20 6f   for those two o
26ce4 70 65 72 61 74 69 6f 6e 73 20 6e 65 65 64 73 20  perations needs 
26ce5 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20  to know whether 
26ce6 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 65 72 61  or not the opera
26ce7 74 69 6f 6e 0a 2a 2a 20 72 65 71 75 69 72 65 73  tion.** requires
26ce8 20 61 6e 79 20 46 4b 20 70 72 6f 63 65 73 73 69   any FK processi
26ce9 6e 67 20 61 6e 64 2c 20 69 66 20 73 6f 2c 20 77  ng and, if so, w
26cea 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  hich columns of 
26ceb 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20  the original.** 
26cec 72 6f 77 20 61 72 65 20 72 65 71 75 69 72 65 64  row are required
26ced 20 62 79 20 74 68 65 20 46 4b 20 70 72 6f 63 65   by the FK proce
26cee 73 73 69 6e 67 20 56 44 42 45 20 63 6f 64 65 20  ssing VDBE code 
26cef 28 69 2e 65 2e 20 69 66 20 46 4b 73 20 77 65 72  (i.e. if FKs wer
26cf0 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64  e.** implemented
26cf1 20 75 73 69 6e 67 20 74 72 69 67 67 65 72 73 2c   using triggers,
26cf2 20 77 68 69 63 68 20 6f 66 20 74 68 65 20 6f 6c   which of the ol
26cf3 64 2e 2a 20 63 6f 6c 75 6d 6e 73 20 77 6f 75 6c  d.* columns woul
26cf4 64 20 62 65 20 0a 2a 2a 20 61 63 63 65 73 73 65  d be .** accesse
26cf5 64 29 2e 20 4e 6f 20 69 6e 66 6f 72 6d 61 74 69  d). No informati
26cf6 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 20 62  on is required b
26cf7 79 20 74 68 65 20 63 6f 64 65 2d 67 65 6e 65 72  y the code-gener
26cf8 61 74 6f 72 20 62 65 66 6f 72 65 0a 2a 2a 20 63  ator before.** c
26cf9 6f 64 69 6e 67 20 61 6e 20 49 4e 53 45 52 54 20  oding an INSERT 
26cfa 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 66  operation. The f
26cfb 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 62 79  unctions used by
26cfc 20 74 68 65 20 55 50 44 41 54 45 2f 44 45 4c 45   the UPDATE/DELE
26cfd 54 45 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6f 6e  TE.** generation
26cfe 20 63 6f 64 65 20 74 6f 20 71 75 65 72 79 20 66   code to query f
26cff 6f 72 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74  or this informat
26d00 69 6f 6e 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20  ion are:.**.**  
26d01 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72   sqlite3FkRequir
26d02 65 64 28 29 20 2d 20 54 65 73 74 20 74 6f 20 73  ed() - Test to s
26d03 65 65 20 69 66 20 46 4b 20 70 72 6f 63 65 73 73  ee if FK process
26d04 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 2e  ing is required.
26d05 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46 6b 4f  .**   sqlite3FkO
26d06 6c 64 6d 61 73 6b 28 29 20 20 2d 20 51 75 65 72  ldmask()  - Quer
26d07 79 20 66 6f 72 20 74 68 65 20 73 65 74 20 6f 66  y for the set of
26d08 20 72 65 71 75 69 72 65 64 20 6f 6c 64 2e 2a 20   required old.* 
26d09 63 6f 6c 75 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 0a 2a  columns..**.**.*
26d0a 2a 20 45 78 74 65 72 6e 61 6c 6c 79 20 61 63 63  * Externally acc
26d0b 65 73 73 69 62 6c 65 20 6d 6f 64 75 6c 65 20 66  essible module f
26d0c 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 2d 2d 2d 2d  unctions.** ----
26d0d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26d0e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26d0f 2d 2d 0a 2a 2a 0a 2a 2a 20 20 20 73 71 6c 69 74  --.**.**   sqlit
26d10 65 33 46 6b 43 68 65 63 6b 28 29 20 20 20 20 2d  e3FkCheck()    -
26d11 20 43 68 65 63 6b 20 66 6f 72 20 66 6f 72 65 69   Check for forei
26d12 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e  gn key violation
26d13 73 2e 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 46  s..**   sqlite3F
26d14 6b 41 63 74 69 6f 6e 73 28 29 20 20 2d 20 43 6f  kActions()  - Co
26d15 64 65 20 74 72 69 67 67 65 72 73 20 66 6f 72 20  de triggers for 
26d16 4f 4e 20 55 50 44 41 54 45 2f 4f 4e 20 44 45 4c  ON UPDATE/ON DEL
26d17 45 54 45 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a 20  ETE actions..** 
26d18 20 20 73 71 6c 69 74 65 33 46 6b 44 65 6c 65 74    sqlite3FkDelet
26d19 65 28 29 20 20 20 2d 20 44 65 6c 65 74 65 20 61  e()   - Delete a
26d1a 6e 20 46 4b 65 79 20 73 74 72 75 63 74 75 72 65  n FKey structure
26d1b 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 44 42 45  ..*/../*.** VDBE
26d1c 20 43 61 6c 6c 69 6e 67 20 43 6f 6e 76 65 6e 74   Calling Convent
26d1d 69 6f 6e 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ion.** ---------
26d1e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a  --------------.*
26d1f 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a  *.** Example:.**
26d20 0a 2a 2a 20 20 20 46 6f 72 20 74 68 65 20 66 6f  .**   For the fo
26d21 6c 6c 6f 77 69 6e 67 20 49 4e 53 45 52 54 20 73  llowing INSERT s
26d22 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20  tatement:.**.** 
26d23 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
26d24 20 74 31 28 61 2c 20 62 20 49 4e 54 45 47 45 52   t1(a, b INTEGER
26d25 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 63 29   PRIMARY KEY, c)
26d26 3b 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54 20  ;.**     INSERT 
26d27 49 4e 54 4f 20 74 31 20 56 41 4c 55 45 53 28 31  INTO t1 VALUES(1
26d28 2c 20 32 2c 20 33 2e 31 29 3b 0a 2a 2a 0a 2a 2a  , 2, 3.1);.**.**
26d29 20 20 20 52 65 67 69 73 74 65 72 20 28 78 29 3a     Register (x):
26d2a 20 20 20 20 20 20 20 20 32 20 20 20 20 28 74 79          2    (ty
26d2b 70 65 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20 20  pe integer).**  
26d2c 20 52 65 67 69 73 74 65 72 20 28 78 2b 31 29 3a   Register (x+1):
26d2d 20 20 20 20 20 20 31 20 20 20 20 28 74 79 70 65        1    (type
26d2e 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20 20 20 52   integer).**   R
26d2f 65 67 69 73 74 65 72 20 28 78 2b 32 29 3a 20 20  egister (x+2):  
26d30 20 20 20 20 4e 55 4c 4c 20 28 74 79 70 65 20 4e      NULL (type N
26d31 55 4c 4c 29 0a 2a 2a 20 20 20 52 65 67 69 73 74  ULL).**   Regist
26d32 65 72 20 28 78 2b 33 29 3a 20 20 20 20 20 20 33  er (x+3):      3
26d33 2e 31 20 20 28 74 79 70 65 20 72 65 61 6c 29 0a  .1  (type real).
26d34 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 66 6f 72 65  */../*.** A fore
26d35 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
26d36 6e 74 20 72 65 71 75 69 72 65 73 20 74 68 61 74  nt requires that
26d37 20 74 68 65 20 6b 65 79 20 63 6f 6c 75 6d 6e 73   the key columns
26d38 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 0a 2a   in the parent.*
26d39 2a 20 74 61 62 6c 65 20 61 72 65 20 63 6f 6c 6c  * table are coll
26d3a 65 63 74 69 76 65 6c 79 20 73 75 62 6a 65 63 74  ectively subject
26d3b 20 74 6f 20 61 20 55 4e 49 51 55 45 20 6f 72 20   to a UNIQUE or 
26d3c 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73  PRIMARY KEY cons
26d3d 74 72 61 69 6e 74 2e 0a 2a 2a 20 47 69 76 65 6e  traint..** Given
26d3e 20 74 68 61 74 20 70 50 61 72 65 6e 74 20 69 73   that pParent is
26d3f 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
26d40 65 20 66 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65  e for foreign ke
26d41 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 46 4b  y constraint pFK
26d42 65 79 2c 20 0a 2a 2a 20 73 65 61 72 63 68 20 74  ey, .** search t
26d43 68 65 20 73 63 68 65 6d 61 20 61 20 75 6e 69 71  he schema a uniq
26d44 75 65 20 69 6e 64 65 78 20 6f 6e 20 74 68 65 20  ue index on the 
26d45 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d  parent key colum
26d46 6e 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  ns. .**.** If su
26d47 63 63 65 73 73 66 75 6c 2c 20 7a 65 72 6f 20 69  ccessful, zero i
26d48 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74  s returned. If t
26d49 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 69 73  he parent key is
26d4a 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   an INTEGER PRIM
26d4b 41 52 59 20 0a 2a 2a 20 4b 45 59 20 63 6f 6c 75  ARY .** KEY colu
26d4c 6d 6e 2c 20 74 68 65 6e 20 6f 75 74 70 75 74 20  mn, then output 
26d4d 76 61 72 69 61 62 6c 65 20 2a 70 70 49 64 78 20  variable *ppIdx 
26d4e 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20  is set to NULL. 
26d4f 4f 74 68 65 72 77 69 73 65 2c 20 2a 70 70 49 64  Otherwise, *ppId
26d50 78 20 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20  x .** is set to 
26d51 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 75 6e 69  point to the uni
26d52 71 75 65 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 0a  que index. .** .
26d53 2a 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e 74  ** If the parent
26d54 20 6b 65 79 20 63 6f 6e 73 69 73 74 73 20 6f 66   key consists of
26d55 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e   a single column
26d56 20 28 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65   (the foreign ke
26d57 79 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20  y constraint.** 
26d58 69 73 20 6e 6f 74 20 61 20 63 6f 6d 70 6f 73 69  is not a composi
26d59 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 29 2c  te foreign key),
26d5a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
26d5b 20 2a 70 61 69 43 6f 6c 20 69 73 20 73 65 74 20   *paiCol is set 
26d5c 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20 4f 74 68 65  to NULL..** Othe
26d5d 72 77 69 73 65 2c 20 69 74 20 69 73 20 73 65 74  rwise, it is set
26d5e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20   to point to an 
26d5f 61 6c 6c 6f 63 61 74 65 64 20 61 72 72 61 79 20  allocated array 
26d60 6f 66 20 73 69 7a 65 20 4e 2c 20 77 68 65 72 65  of size N, where
26d61 0a 2a 2a 20 4e 20 69 73 20 74 68 65 20 6e 75 6d  .** N is the num
26d62 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
26d63 6e 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  n the parent key
26d64 2e 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d  . The first elem
26d65 65 6e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61 72  ent of the.** ar
26d66 72 61 79 20 69 73 20 74 68 65 20 69 6e 64 65 78  ray is the index
26d67 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 74 61   of the child ta
26d68 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  ble column that 
26d69 69 73 20 6d 61 70 70 65 64 20 62 79 20 74 68 65  is mapped by the
26d6a 20 46 4b 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e   FK.** constrain
26d6b 74 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20  t to the parent 
26d6c 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 73 74 6f  table column sto
26d6d 72 65 64 20 69 6e 20 74 68 65 20 6c 65 66 74 2d  red in the left-
26d6e 6d 6f 73 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f  most column.** o
26d6f 66 20 69 6e 64 65 78 20 2a 70 70 49 64 78 2e 20  f index *ppIdx. 
26d70 54 68 65 20 73 65 63 6f 6e 64 20 65 6c 65 6d 65  The second eleme
26d71 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 79 20  nt of the array 
26d72 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
26d73 74 68 65 0a 2a 2a 20 63 68 69 6c 64 20 74 61 62  the.** child tab
26d74 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 63  le column that c
26d75 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68  orresponds to th
26d76 65 20 73 65 63 6f 6e 64 20 6c 65 66 74 2d 6d 6f  e second left-mo
26d77 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 0a 2a 2a 20  st column of.** 
26d78 2a 70 70 49 64 78 2c 20 61 6e 64 20 73 6f 20 6f  *ppIdx, and so o
26d79 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  n..**.** If the 
26d7a 72 65 71 75 69 72 65 64 20 69 6e 64 65 78 20 63  required index c
26d7b 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20  annot be found, 
26d7c 65 69 74 68 65 72 20 62 65 63 61 75 73 65 3a 0a  either because:.
26d7d 2a 2a 0a 2a 2a 20 20 20 31 29 20 54 68 65 20 6e  **.**   1) The n
26d7e 61 6d 65 64 20 70 61 72 65 6e 74 20 6b 65 79 20  amed parent key 
26d7f 63 6f 6c 75 6d 6e 73 20 64 6f 20 6e 6f 74 20 65  columns do not e
26d80 78 69 73 74 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20  xist, or.**.**  
26d81 20 32 29 20 54 68 65 20 6e 61 6d 65 64 20 70 61   2) The named pa
26d82 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73  rent key columns
26d83 20 64 6f 20 65 78 69 73 74 2c 20 62 75 74 20 61   do exist, but a
26d84 72 65 20 6e 6f 74 20 73 75 62 6a 65 63 74 20 74  re not subject t
26d85 6f 20 61 0a 2a 2a 20 20 20 20 20 20 55 4e 49 51  o a.**      UNIQ
26d86 55 45 20 6f 72 20 50 52 49 4d 41 52 59 20 4b 45  UE or PRIMARY KE
26d87 59 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6f 72  Y constraint, or
26d88 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 4e 6f 20 70  .**.**   3) No p
26d89 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
26d8a 73 20 77 65 72 65 20 70 72 6f 76 69 64 65 64 20  s were provided 
26d8b 65 78 70 6c 69 63 69 74 6c 79 20 61 73 20 70 61  explicitly as pa
26d8c 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20  rt of the.**    
26d8d 20 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65    foreign key de
26d8e 66 69 6e 69 74 69 6f 6e 2c 20 61 6e 64 20 74 68  finition, and th
26d8f 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 64  e parent table d
26d90 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 61 0a 2a  oes not have a.*
26d91 2a 20 20 20 20 20 20 50 52 49 4d 41 52 59 20 4b  *      PRIMARY K
26d92 45 59 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 34  EY, or.**.**   4
26d93 29 20 4e 6f 20 70 61 72 65 6e 74 20 6b 65 79 20  ) No parent key 
26d94 63 6f 6c 75 6d 6e 73 20 77 65 72 65 20 70 72 6f  columns were pro
26d95 76 69 64 65 64 20 65 78 70 6c 69 63 69 74 6c 79  vided explicitly
26d96 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a   as part of the.
26d97 2a 2a 20 20 20 20 20 20 66 6f 72 65 69 67 6e 20  **      foreign 
26d98 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 2c 20  key definition, 
26d99 61 6e 64 20 74 68 65 20 50 52 49 4d 41 52 59 20  and the PRIMARY 
26d9a 4b 45 59 20 6f 66 20 74 68 65 20 70 61 72 65 6e  KEY of the paren
26d9b 74 20 74 61 62 6c 65 20 0a 2a 2a 20 20 20 20 20  t table .**     
26d9c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 61   consists of a a
26d9d 20 64 69 66 66 65 72 65 6e 74 20 6e 75 6d 62 65   different numbe
26d9e 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20  r of columns to 
26d9f 74 68 65 20 63 68 69 6c 64 20 6b 65 79 20 69 6e  the child key in
26da0 20 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 63 68   .**      the ch
26da1 69 6c 64 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ild table..**.**
26da2 20 74 68 65 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69   then non-zero i
26da3 73 20 72 65 74 75 72 6e 65 64 2c 20 61 6e 64 20  s returned, and 
26da4 61 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d  a "foreign key m
26da5 69 73 6d 61 74 63 68 22 20 65 72 72 6f 72 20 6c  ismatch" error l
26da6 6f 61 64 65 64 0a 2a 2a 20 69 6e 74 6f 20 70 50  oaded.** into pP
26da7 61 72 73 65 2e 20 49 66 20 61 6e 20 4f 4f 4d 20  arse. If an OOM 
26da8 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6e 6f  error occurs, no
26da9 6e 2d 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e  n-zero is return
26daa 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 70 50  ed and the.** pP
26dab 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
26dac 46 61 69 6c 65 64 20 66 6c 61 67 20 69 73 20 73  Failed flag is s
26dad 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  et..*/.static in
26dae 74 20 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64 65  t locateFkeyInde
26daf 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  x(.  Parse *pPar
26db0 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
26db1 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f       /* Parse co
26db2 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20 61  ntext to store a
26db3 6e 79 20 65 72 72 6f 72 20 69 6e 20 2a 2f 0a 20  ny error in */. 
26db4 20 54 61 62 6c 65 20 2a 70 50 61 72 65 6e 74 2c   Table *pParent,
26db5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26db6 20 2f 2a 20 50 61 72 65 6e 74 20 74 61 62 6c 65   /* Parent table
26db7 20 6f 66 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e   of FK constrain
26db8 74 20 70 46 4b 65 79 20 2a 2f 0a 20 20 46 4b 65  t pFKey */.  FKe
26db9 79 20 2a 70 46 4b 65 79 2c 20 20 20 20 20 20 20  y *pFKey,       
26dba 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26dbb 46 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f 20 66  Foreign key to f
26dbc 69 6e 64 20 69 6e 64 65 78 20 66 6f 72 20 2a 2f  ind index for */
26dbd 0a 20 20 49 6e 64 65 78 20 2a 2a 70 70 49 64 78  .  Index **ppIdx
26dbe 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26dbf 20 20 20 2f 2a 20 4f 55 54 3a 20 55 6e 69 71 75     /* OUT: Uniqu
26dc0 65 20 69 6e 64 65 78 20 6f 6e 20 70 61 72 65 6e  e index on paren
26dc1 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  t table */.  int
26dc2 20 2a 2a 70 61 69 43 6f 6c 20 20 20 20 20 20 20   **paiCol       
26dc3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26dc4 4f 55 54 3a 20 4d 61 70 20 6f 66 20 69 6e 64 65  OUT: Map of inde
26dc5 78 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 46 4b  x columns in pFK
26dc6 65 79 20 2a 2f 0a 29 7b 0a 20 20 49 6e 64 65 78  ey */.){.  Index
26dc7 20 2a 70 49 64 78 20 3d 20 30 3b 20 20 20 20 20   *pIdx = 0;     
26dc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26dc9 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
26dca 6e 20 76 69 61 20 2a 70 70 49 64 78 20 2a 2f 0a  n via *ppIdx */.
26dcb 20 20 69 6e 74 20 2a 61 69 43 6f 6c 20 3d 20 30    int *aiCol = 0
26dcc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26dcd 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
26dce 6f 20 72 65 74 75 72 6e 20 76 69 61 20 2a 70 61  o return via *pa
26dcf 69 43 6f 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  iCol */.  int nC
26dd0 6f 6c 20 3d 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c  ol = pFKey->nCol
26dd1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
26dd2 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
26dd3 6e 73 20 69 6e 20 70 61 72 65 6e 74 20 6b 65 79  ns in parent key
26dd4 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4b 65 79   */.  char *zKey
26dd5 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30   = pFKey->aCol[0
26dd6 5d 2e 7a 43 6f 6c 3b 20 20 20 2f 2a 20 4e 61 6d  ].zCol;   /* Nam
26dd7 65 20 6f 66 20 6c 65 66 74 2d 6d 6f 73 74 20 70  e of left-most p
26dd8 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
26dd9 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 63 61   */..  /* The ca
26dda 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69  ller is responsi
26ddb 62 6c 65 20 66 6f 72 20 7a 65 72 6f 69 6e 67 20  ble for zeroing 
26ddc 6f 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72  output parameter
26ddd 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  s. */.  assert( 
26dde 70 70 49 64 78 20 26 26 20 2a 70 70 49 64 78 3d  ppIdx && *ppIdx=
26ddf 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
26de0 21 70 61 69 43 6f 6c 20 7c 7c 20 2a 70 61 69 43  !paiCol || *paiC
26de1 6f 6c 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  ol==0 );.  asser
26de2 74 28 20 70 50 61 72 73 65 20 29 3b 0a 0a 20 20  t( pParse );..  
26de3 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
26de4 6e 6f 6e 2d 63 6f 6d 70 6f 73 69 74 65 20 28 73  non-composite (s
26de5 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 29 20 66 6f  ingle column) fo
26de6 72 65 69 67 6e 20 6b 65 79 2c 20 63 68 65 63 6b  reign key, check
26de7 20 69 66 20 69 74 20 0a 20 20 2a 2a 20 6d 61 70   if it .  ** map
26de8 73 20 74 6f 20 74 68 65 20 49 4e 54 45 47 45 52  s to the INTEGER
26de9 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 66 20   PRIMARY KEY of 
26dea 74 61 62 6c 65 20 70 50 61 72 65 6e 74 2e 20 49  table pParent. I
26deb 66 20 73 6f 2c 20 6c 65 61 76 65 20 2a 70 70 49  f so, leave *ppI
26dec 64 78 20 0a 20 20 2a 2a 20 61 6e 64 20 2a 70 61  dx .  ** and *pa
26ded 69 43 6f 6c 20 73 65 74 20 74 6f 20 7a 65 72 6f  iCol set to zero
26dee 20 61 6e 64 20 72 65 74 75 72 6e 20 65 61 72 6c   and return earl
26def 79 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 74  y. .  **.  ** Ot
26df0 68 65 72 77 69 73 65 2c 20 66 6f 72 20 61 20 63  herwise, for a c
26df1 6f 6d 70 6f 73 69 74 65 20 66 6f 72 65 69 67 6e  omposite foreign
26df2 20 6b 65 79 20 28 6d 6f 72 65 20 74 68 61 6e 20   key (more than 
26df3 6f 6e 65 20 63 6f 6c 75 6d 6e 29 2c 20 61 6c 6c  one column), all
26df4 6f 63 61 74 65 0a 20 20 2a 2a 20 73 70 61 63 65  ocate.  ** space
26df5 20 66 6f 72 20 74 68 65 20 61 69 43 6f 6c 20 61   for the aiCol a
26df6 72 72 61 79 20 28 72 65 74 75 72 6e 65 64 20 76  rray (returned v
26df7 69 61 20 6f 75 74 70 75 74 20 70 61 72 61 6d 65  ia output parame
26df8 74 65 72 20 2a 70 61 69 43 6f 6c 29 2e 0a 20 20  ter *paiCol)..  
26df9 2a 2a 20 4e 6f 6e 2d 63 6f 6d 70 6f 73 69 74 65  ** Non-composite
26dfa 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 64 6f   foreign keys do
26dfb 20 6e 6f 74 20 72 65 71 75 69 72 65 20 74 68 65   not require the
26dfc 20 61 69 43 6f 6c 20 61 72 72 61 79 2e 0a 20 20   aiCol array..  
26dfd 2a 2f 0a 20 20 69 66 28 20 6e 43 6f 6c 3d 3d 31  */.  if( nCol==1
26dfe 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 46   ){.    /* The F
26dff 4b 20 6d 61 70 73 20 74 6f 20 74 68 65 20 49 50  K maps to the IP
26e00 4b 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20  K if any of the 
26e01 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72  following are tr
26e02 75 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ue:.    **.    *
26e03 2a 20 20 20 31 29 20 54 68 65 72 65 20 69 73 20  *   1) There is 
26e04 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  an INTEGER PRIMA
26e05 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 61 6e  RY KEY column an
26e06 64 20 74 68 65 20 46 4b 20 69 73 20 69 6d 70 6c  d the FK is impl
26e07 69 63 69 74 6c 79 20 0a 20 20 20 20 2a 2a 20 20  icitly .    **  
26e08 20 20 20 20 6d 61 70 70 65 64 20 74 6f 20 74 68      mapped to th
26e09 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f 66  e primary key of
26e0a 20 74 61 62 6c 65 20 70 50 61 72 65 6e 74 2c 20   table pParent, 
26e0b 6f 72 0a 20 20 20 20 2a 2a 20 20 20 32 29 20 54  or.    **   2) T
26e0c 68 65 20 46 4b 20 69 73 20 65 78 70 6c 69 63 69  he FK is explici
26e0d 74 6c 79 20 6d 61 70 70 65 64 20 74 6f 20 61 20  tly mapped to a 
26e0e 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64 20  column declared 
26e0f 61 73 20 49 4e 54 45 47 45 52 0a 20 20 20 20 2a  as INTEGER.    *
26e10 2a 20 20 20 20 20 20 50 52 49 4d 41 52 59 20 4b  *      PRIMARY K
26e11 45 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  EY..    */.    i
26e12 66 28 20 70 50 61 72 65 6e 74 2d 3e 69 50 4b 65  f( pParent->iPKe
26e13 79 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  y>=0 ){.      if
26e14 28 20 21 7a 4b 65 79 20 29 20 72 65 74 75 72 6e  ( !zKey ) return
26e15 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 21 73   0;.      if( !s
26e16 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 50  qlite3StrICmp(pP
26e17 61 72 65 6e 74 2d 3e 61 43 6f 6c 5b 70 50 61 72  arent->aCol[pPar
26e18 65 6e 74 2d 3e 69 50 4b 65 79 5d 2e 7a 4e 61 6d  ent->iPKey].zNam
26e19 65 2c 20 7a 4b 65 79 29 20 29 20 72 65 74 75 72  e, zKey) ) retur
26e1a 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  n 0;.    }.  }el
26e1b 73 65 20 69 66 28 20 70 61 69 43 6f 6c 20 29 7b  se if( paiCol ){
26e1c 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f  .    assert( nCo
26e1d 6c 3e 31 20 29 3b 0a 20 20 20 20 61 69 43 6f 6c  l>1 );.    aiCol
26e1e 20 3d 20 28 69 6e 74 20 2a 29 73 71 6c 69 74 65   = (int *)sqlite
26e1f 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 70 50 61  3DbMallocRaw(pPa
26e20 72 73 65 2d 3e 64 62 2c 20 6e 43 6f 6c 2a 73 69  rse->db, nCol*si
26e21 7a 65 6f 66 28 69 6e 74 29 29 3b 0a 20 20 20 20  zeof(int));.    
26e22 69 66 28 20 21 61 69 43 6f 6c 20 29 20 72 65 74  if( !aiCol ) ret
26e23 75 72 6e 20 31 3b 0a 20 20 20 20 2a 70 61 69 43  urn 1;.    *paiC
26e24 6f 6c 20 3d 20 61 69 43 6f 6c 3b 0a 20 20 7d 0a  ol = aiCol;.  }.
26e25 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 50 61 72  .  for(pIdx=pPar
26e26 65 6e 74 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  ent->pIndex; pId
26e27 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
26e28 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49  ext){.    if( pI
26e29 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 6e 43 6f  dx->nColumn==nCo
26e2a 6c 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72  l && pIdx->onErr
26e2b 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 20 0a  or!=OE_None ){ .
26e2c 20 20 20 20 20 20 2f 2a 20 70 49 64 78 20 69 73        /* pIdx is
26e2d 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 20   a UNIQUE index 
26e2e 28 6f 72 20 61 20 50 52 49 4d 41 52 59 20 4b 45  (or a PRIMARY KE
26e2f 59 29 20 61 6e 64 20 68 61 73 20 74 68 65 20 72  Y) and has the r
26e30 69 67 68 74 20 6e 75 6d 62 65 72 0a 20 20 20 20  ight number.    
26e31 20 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e    ** of columns.
26e32 20 49 66 20 65 61 63 68 20 69 6e 64 65 78 65 64   If each indexed
26e33 20 63 6f 6c 75 6d 6e 20 63 6f 72 72 65 73 70 6f   column correspo
26e34 6e 64 73 20 74 6f 20 61 20 66 6f 72 65 69 67 6e  nds to a foreign
26e35 20 6b 65 79 0a 20 20 20 20 20 20 2a 2a 20 63 6f   key.      ** co
26e36 6c 75 6d 6e 20 6f 66 20 70 46 4b 65 79 2c 20 74  lumn of pFKey, t
26e37 68 65 6e 20 74 68 69 73 20 69 6e 64 65 78 20 69  hen this index i
26e38 73 20 61 20 77 69 6e 6e 65 72 2e 20 20 2a 2f 0a  s a winner.  */.
26e39 0a 20 20 20 20 20 20 69 66 28 20 7a 4b 65 79 3d  .      if( zKey=
26e3a 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
26e3b 20 49 66 20 7a 4b 65 79 20 69 73 20 4e 55 4c 4c   If zKey is NULL
26e3c 2c 20 74 68 65 6e 20 74 68 69 73 20 66 6f 72 65  , then this fore
26e3d 69 67 6e 20 6b 65 79 20 69 73 20 69 6d 70 6c 69  ign key is impli
26e3e 63 69 74 6c 79 20 6d 61 70 70 65 64 20 74 6f 20  citly mapped to 
26e3f 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
26e40 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 66 20 74  PRIMARY KEY of t
26e41 61 62 6c 65 20 70 50 61 72 65 6e 74 2e 20 54 68  able pParent. Th
26e42 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e  e PRIMARY KEY in
26e43 64 65 78 20 6d 61 79 20 62 65 20 0a 20 20 20 20  dex may be .    
26e44 20 20 20 20 2a 2a 20 69 64 65 6e 74 69 66 69 65      ** identifie
26e45 64 20 62 79 20 74 68 65 20 74 65 73 74 20 28 49  d by the test (I
26e46 6e 64 65 78 2e 61 75 74 6f 49 6e 64 65 78 3d 3d  ndex.autoIndex==
26e47 32 29 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  2).  */.        
26e48 69 66 28 20 70 49 64 78 2d 3e 61 75 74 6f 49 6e  if( pIdx->autoIn
26e49 64 65 78 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20  dex==2 ){.      
26e4a 20 20 20 20 69 66 28 20 61 69 43 6f 6c 20 29 7b      if( aiCol ){
26e4b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
26e4c 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   i;.            
26e4d 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
26e4e 20 69 2b 2b 29 20 61 69 43 6f 6c 5b 69 5d 20 3d   i++) aiCol[i] =
26e4f 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e   pFKey->aCol[i].
26e50 69 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20 20  iFrom;.         
26e51 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65   }.          bre
26e52 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
26e53 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
26e54 20 20 20 2f 2a 20 49 66 20 7a 4b 65 79 20 69 73     /* If zKey is
26e55 20 6e 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 6e 20   non-NULL, then 
26e56 74 68 69 73 20 66 6f 72 65 69 67 6e 20 6b 65 79  this foreign key
26e57 20 77 61 73 20 64 65 63 6c 61 72 65 64 20 74 6f   was declared to
26e58 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 70 20  .        ** map 
26e59 74 6f 20 61 6e 20 65 78 70 6c 69 63 69 74 20 6c  to an explicit l
26e5a 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ist of columns i
26e5b 6e 20 74 61 62 6c 65 20 70 50 61 72 65 6e 74 2e  n table pParent.
26e5c 20 43 68 65 63 6b 20 69 66 20 74 68 69 73 0a 20   Check if this. 
26e5d 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 78 20         ** index 
26e5e 6d 61 74 63 68 65 73 20 74 68 6f 73 65 20 63 6f  matches those co
26e5f 6c 75 6d 6e 73 2e 20 41 6c 73 6f 2c 20 63 68 65  lumns. Also, che
26e60 63 6b 20 74 68 61 74 20 74 68 65 20 69 6e 64 65  ck that the inde
26e61 78 20 75 73 65 73 0a 20 20 20 20 20 20 20 20 2a  x uses.        *
26e62 2a 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f  * the default co
26e63 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
26e64 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  s for each colum
26e65 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e  n. */.        in
26e66 74 20 69 2c 20 6a 3b 0a 20 20 20 20 20 20 20 20  t i, j;.        
26e67 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
26e68 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
26e69 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 49 64 78   int iCol = pIdx
26e6a 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 20 20  ->aiColumn[i];  
26e6b 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
26e6c 6f 6c 75 6d 6e 20 69 6e 20 70 61 72 65 6e 74 20  olumn in parent 
26e6d 74 62 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  tbl */.         
26e6e 20 63 68 61 72 20 2a 7a 44 66 6c 74 43 6f 6c 6c   char *zDfltColl
26e6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26e70 20 20 20 2f 2a 20 44 65 66 2e 20 63 6f 6c 6c 61     /* Def. colla
26e71 74 69 6f 6e 20 66 6f 72 20 63 6f 6c 75 6d 6e 20  tion for column 
26e72 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 68 61  */.          cha
26e73 72 20 2a 7a 49 64 78 43 6f 6c 3b 20 20 20 20 20  r *zIdxCol;     
26e74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26e75 2a 20 4e 61 6d 65 20 6f 66 20 69 6e 64 65 78 65  * Name of indexe
26e76 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 0a 20 20 20  d column */..   
26e77 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
26e78 20 69 6e 64 65 78 20 75 73 65 73 20 61 20 63 6f   index uses a co
26e79 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
26e7a 20 74 68 61 74 20 69 73 20 64 69 66 66 65 72 65   that is differe
26e7b 6e 74 20 66 72 6f 6d 0a 20 20 20 20 20 20 20 20  nt from.        
26e7c 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74    ** the default
26e7d 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
26e7e 6e 63 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  nce for the colu
26e7f 6d 6e 2c 20 74 68 69 73 20 69 6e 64 65 78 20 69  mn, this index i
26e80 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 75  s.          ** u
26e81 6e 75 73 61 62 6c 65 2e 20 42 61 69 6c 20 6f 75  nusable. Bail ou
26e82 74 20 65 61 72 6c 79 20 69 6e 20 74 68 69 73 20  t early in this 
26e83 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20  case.  */.      
26e84 20 20 20 20 7a 44 66 6c 74 43 6f 6c 6c 20 3d 20      zDfltColl = 
26e85 70 50 61 72 65 6e 74 2d 3e 61 43 6f 6c 5b 69 43  pParent->aCol[iC
26e86 6f 6c 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20  ol].zColl;.     
26e87 20 20 20 20 20 69 66 28 20 21 7a 44 66 6c 74 43       if( !zDfltC
26e88 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20  oll ){.         
26e89 20 20 20 7a 44 66 6c 74 43 6f 6c 6c 20 3d 20 22     zDfltColl = "
26e8a 42 49 4e 41 52 59 22 3b 0a 20 20 20 20 20 20 20  BINARY";.       
26e8b 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
26e8c 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
26e8d 70 28 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69  p(pIdx->azColl[i
26e8e 5d 2c 20 7a 44 66 6c 74 43 6f 6c 6c 29 20 29 20  ], zDfltColl) ) 
26e8f 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20 20 20  break;..        
26e90 20 20 7a 49 64 78 43 6f 6c 20 3d 20 70 50 61 72    zIdxCol = pPar
26e91 65 6e 74 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  ent->aCol[iCol].
26e92 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20  zName;.         
26e93 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f 6c   for(j=0; j<nCol
26e94 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
26e95 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
26e96 74 72 49 43 6d 70 28 70 46 4b 65 79 2d 3e 61 43  trICmp(pFKey->aC
26e97 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 2c 20 7a 49 64 78  ol[j].zCol, zIdx
26e98 43 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Col)==0 ){.     
26e99 20 20 20 20 20 20 20 20 20 69 66 28 20 61 69 43           if( aiC
26e9a 6f 6c 20 29 20 61 69 43 6f 6c 5b 69 5d 20 3d 20  ol ) aiCol[i] = 
26e9b 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69  pFKey->aCol[j].i
26e9c 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20 20 20 20  From;.          
26e9d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
26e9e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
26e9f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
26ea0 66 28 20 6a 3d 3d 6e 43 6f 6c 20 29 20 62 72 65  f( j==nCol ) bre
26ea1 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
26ea2 20 20 20 20 20 20 69 66 28 20 69 3d 3d 6e 43 6f        if( i==nCo
26ea3 6c 20 29 20 62 72 65 61 6b 3b 20 20 20 20 20 20  l ) break;      
26ea4 2f 2a 20 70 49 64 78 20 69 73 20 75 73 61 62 6c  /* pIdx is usabl
26ea5 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  e */.      }.   
26ea6 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70   }.  }..  if( !p
26ea7 49 64 78 20 29 7b 0a 20 20 20 20 69 66 28 20 21  Idx ){.    if( !
26ea8 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 54  pParse->disableT
26ea9 72 69 67 67 65 72 73 20 29 7b 0a 20 20 20 20 20  riggers ){.     
26eaa 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
26eab 28 70 50 61 72 73 65 2c 20 22 66 6f 72 65 69 67  (pParse, "foreig
26eac 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68 22 29  n key mismatch")
26ead 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
26eae 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65  te3DbFree(pParse
26eaf 2d 3e 64 62 2c 20 61 69 43 6f 6c 29 3b 0a 20 20  ->db, aiCol);.  
26eb0 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
26eb1 0a 20 20 2a 70 70 49 64 78 20 3d 20 70 49 64 78  .  *ppIdx = pIdx
26eb2 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  ;.  return 0;.}.
26eb3 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
26eb4 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77  tion is called w
26eb5 68 65 6e 20 61 20 72 6f 77 20 69 73 20 69 6e 73  hen a row is ins
26eb6 65 72 74 65 64 20 69 6e 74 6f 20 6f 72 20 64 65  erted into or de
26eb7 6c 65 74 65 64 20 66 72 6f 6d 20 74 68 65 20 0a  leted from the .
26eb8 2a 2a 20 63 68 69 6c 64 20 74 61 62 6c 65 20 6f  ** child table o
26eb9 66 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  f foreign key co
26eba 6e 73 74 72 61 69 6e 74 20 70 46 4b 65 79 2e 20  nstraint pFKey. 
26ebb 49 66 20 61 6e 20 53 51 4c 20 55 50 44 41 54 45  If an SQL UPDATE
26ebc 20 69 73 20 65 78 65 63 75 74 65 64 20 0a 2a 2a   is executed .**
26ebd 20 6f 6e 20 74 68 65 20 63 68 69 6c 64 20 74 61   on the child ta
26ebe 62 6c 65 20 6f 66 20 70 46 4b 65 79 2c 20 74 68  ble of pFKey, th
26ebf 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  is function is i
26ec0 6e 76 6f 6b 65 64 20 74 77 69 63 65 20 66 6f 72  nvoked twice for
26ec1 20 65 61 63 68 20 72 6f 77 0a 2a 2a 20 61 66 66   each row.** aff
26ec2 65 63 74 65 64 20 2d 20 6f 6e 63 65 20 74 6f 20  ected - once to 
26ec3 22 64 65 6c 65 74 65 22 20 74 68 65 20 6f 6c 64  "delete" the old
26ec4 20 72 6f 77 2c 20 61 6e 64 20 74 68 65 6e 20 61   row, and then a
26ec5 67 61 69 6e 20 74 6f 20 22 69 6e 73 65 72 74 22  gain to "insert"
26ec6 20 74 68 65 0a 2a 2a 20 6e 65 77 20 72 6f 77 2e   the.** new row.
26ec7 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 74 69 6d 65  .**.** Each time
26ec8 20 69 74 20 69 73 20 63 61 6c 6c 65 64 2c 20 74   it is called, t
26ec9 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 67 65 6e  his function gen
26eca 65 72 61 74 65 73 20 56 44 42 45 20 63 6f 64 65  erates VDBE code
26ecb 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 0a 2a   to locate the.*
26ecc 2a 20 72 6f 77 20 69 6e 20 74 68 65 20 70 61 72  * row in the par
26ecd 65 6e 74 20 74 61 62 6c 65 20 74 68 61 74 20 63  ent table that c
26ece 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68  orresponds to th
26ecf 65 20 72 6f 77 20 62 65 69 6e 67 20 69 6e 73 65  e row being inse
26ed0 72 74 65 64 20 69 6e 74 6f 20 0a 2a 2a 20 6f 72  rted into .** or
26ed1 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 74 68   deleted from th
26ed2 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 20 49  e child table. I
26ed3 66 20 74 68 65 20 70 61 72 65 6e 74 20 72 6f 77  f the parent row
26ed4 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 6e   can be found, n
26ed5 6f 20 0a 2a 2a 20 73 70 65 63 69 61 6c 20 61 63  o .** special ac
26ed6 74 69 6f 6e 20 69 73 20 74 61 6b 65 6e 2e 20 4f  tion is taken. O
26ed7 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
26ed8 20 70 61 72 65 6e 74 20 72 6f 77 20 63 61 6e 20   parent row can 
26ed9 2a 6e 6f 74 2a 20 62 65 0a 2a 2a 20 66 6f 75 6e  *not* be.** foun
26eda 64 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  d in the parent 
26edb 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f  table:.**.**   O
26edc 70 65 72 61 74 69 6f 6e 20 7c 20 46 4b 20 74 79  peration | FK ty
26edd 70 65 20 20 20 7c 20 41 63 74 69 6f 6e 20 74 61  pe   | Action ta
26ede 6b 65 6e 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ken.**   -------
26edf 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26ee0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26ee1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26ee2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26ee3 2d 2d 2d 0a 2a 2a 20 20 20 49 4e 53 45 52 54 20  ---.**   INSERT 
26ee4 20 20 20 20 20 69 6d 6d 65 64 69 61 74 65 20 20       immediate  
26ee5 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 22   Increment the "
26ee6 69 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74 72  immediate constr
26ee7 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a  aint counter"..*
26ee8 2a 0a 2a 2a 20 20 20 44 45 4c 45 54 45 20 20 20  *.**   DELETE   
26ee9 20 20 20 69 6d 6d 65 64 69 61 74 65 20 20 20 44     immediate   D
26eea 65 63 72 65 6d 65 6e 74 20 74 68 65 20 22 69 6d  ecrement the "im
26eeb 6d 65 64 69 61 74 65 20 63 6f 6e 73 74 72 61 69  mediate constrai
26eec 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a  nt counter"..**.
26eed 2a 2a 20 20 20 49 4e 53 45 52 54 20 20 20 20 20  **   INSERT     
26eee 20 64 65 66 65 72 72 65 64 20 20 20 20 49 6e 63   deferred    Inc
26eef 72 65 6d 65 6e 74 20 74 68 65 20 22 64 65 66 65  rement the "defe
26ef0 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20  rred constraint 
26ef1 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a 2a 20  counter"..**.** 
26ef2 20 20 44 45 4c 45 54 45 20 20 20 20 20 20 64 65    DELETE      de
26ef3 66 65 72 72 65 64 20 20 20 20 44 65 63 72 65 6d  ferred    Decrem
26ef4 65 6e 74 20 74 68 65 20 22 64 65 66 65 72 72 65  ent the "deferre
26ef5 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75  d constraint cou
26ef6 6e 74 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  nter"..**.** The
26ef7 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72  se operations ar
26ef8 65 20 69 64 65 6e 74 69 66 69 65 64 20 69 6e 20  e identified in 
26ef9 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 74 20 74  the comment at t
26efa 68 65 20 74 6f 70 20 6f 66 20 74 68 69 73 20 66  he top of this f
26efb 69 6c 65 20 0a 2a 2a 20 28 66 6b 65 79 2e 63 29  ile .** (fkey.c)
26efc 20 61 73 20 22 49 2e 31 22 20 61 6e 64 20 22 44   as "I.1" and "D
26efd 2e 31 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  .1"..*/.static v
26efe 6f 69 64 20 66 6b 4c 6f 6f 6b 75 70 50 61 72 65  oid fkLookupPare
26eff 6e 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  nt(.  Parse *pPa
26f00 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
26f01 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  arse context */.
26f02 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20    int iDb,      
26f03 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
26f04 20 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 75   of database hou
26f05 73 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 54  sing pTab */.  T
26f06 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
26f07 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 74       /* Parent t
26f08 61 62 6c 65 20 6f 66 20 46 4b 20 70 46 4b 65 79  able of FK pFKey
26f09 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
26f0a 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  x,          /* U
26f0b 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e 20 70  nique index on p
26f0c 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
26f0d 73 20 69 6e 20 70 54 61 62 20 2a 2f 0a 20 20 46  s in pTab */.  F
26f0e 4b 65 79 20 2a 70 46 4b 65 79 2c 20 20 20 20 20  Key *pFKey,     
26f0f 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20       /* Foreign 
26f10 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a  key constraint *
26f11 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c 2c 20  /.  int *aiCol, 
26f12 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70            /* Map
26f13 20 66 72 6f 6d 20 70 61 72 65 6e 74 20 6b 65 79   from parent key
26f14 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 69 6c   columns to chil
26f15 64 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20  d table columns 
26f16 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  */.  int regData
26f17 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  ,          /* Ad
26f18 64 72 65 73 73 20 6f 66 20 61 72 72 61 79 20 63  dress of array c
26f19 6f 6e 74 61 69 6e 69 6e 67 20 63 68 69 6c 64 20  ontaining child 
26f1a 74 61 62 6c 65 20 72 6f 77 20 2a 2f 0a 20 20 69  table row */.  i
26f1b 6e 74 20 6e 49 6e 63 72 2c 20 20 20 20 20 20 20  nt nIncr,       
26f1c 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e       /* Incremen
26f1d 74 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75  t constraint cou
26f1e 6e 74 65 72 20 62 79 20 74 68 69 73 20 2a 2f 0a  nter by this */.
26f1f 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65 20 20    int isIgnore  
26f20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72          /* If tr
26f21 75 65 2c 20 70 72 65 74 65 6e 64 20 70 54 61 62  ue, pretend pTab
26f22 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 4e 55   contains all NU
26f23 4c 4c 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a  LL values */.){.
26f24 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
26f25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f26 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
26f27 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
26f28 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20   */.  Vdbe *v = 
26f29 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
26f2a 50 61 72 73 65 29 3b 20 20 20 20 20 20 20 20 20  Parse);         
26f2b 2f 2a 20 56 64 62 65 20 74 6f 20 61 64 64 20 63  /* Vdbe to add c
26f2c 6f 64 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  ode to */.  int 
26f2d 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iCur = pParse->n
26f2e 54 61 62 20 2d 20 31 3b 20 20 20 20 20 20 20 20  Tab - 1;        
26f2f 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
26f30 6e 75 6d 62 65 72 20 74 6f 20 75 73 65 20 2a 2f  number to use */
26f31 0a 20 20 69 6e 74 20 69 4f 6b 20 3d 20 73 71 6c  .  int iOk = sql
26f32 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
26f33 6c 28 76 29 3b 20 20 20 20 20 20 20 20 2f 2a 20  l(v);        /* 
26f34 6a 75 6d 70 20 68 65 72 65 20 69 66 20 70 61 72  jump here if par
26f35 65 6e 74 20 6b 65 79 20 66 6f 75 6e 64 20 2a 2f  ent key found */
26f36 0a 0a 20 20 2f 2a 20 49 66 20 6e 49 6e 63 72 20  ..  /* If nIncr 
26f37 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72  is less than zer
26f38 6f 2c 20 74 68 65 6e 20 63 68 65 63 6b 20 61 74  o, then check at
26f39 20 72 75 6e 74 69 6d 65 20 69 66 20 74 68 65 72   runtime if ther
26f3a 65 20 61 72 65 20 61 6e 79 0a 20 20 2a 2a 20 6f  e are any.  ** o
26f3b 75 74 73 74 61 6e 64 69 6e 67 20 63 6f 6e 73 74  utstanding const
26f3c 72 61 69 6e 74 73 20 74 6f 20 72 65 73 6f 6c 76  raints to resolv
26f3d 65 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 20  e. If there are 
26f3e 6e 6f 74 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  not, there is no
26f3f 20 6e 65 65 64 0a 20 20 2a 2a 20 74 6f 20 63 68   need.  ** to ch
26f40 65 63 6b 20 69 66 20 64 65 6c 65 74 69 6e 67 20  eck if deleting 
26f41 74 68 69 73 20 72 6f 77 20 72 65 73 6f 6c 76 65  this row resolve
26f42 73 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e  s any outstandin
26f43 67 20 76 69 6f 6c 61 74 69 6f 6e 73 2e 0a 20 20  g violations..  
26f44 2a 2a 0a 20 20 2a 2a 20 43 68 65 63 6b 20 69 66  **.  ** Check if
26f45 20 61 6e 79 20 6f 66 20 74 68 65 20 6b 65 79 20   any of the key 
26f46 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63  columns in the c
26f47 68 69 6c 64 20 74 61 62 6c 65 20 72 6f 77 20 61  hild table row a
26f48 72 65 20 4e 55 4c 4c 2e 20 49 66 20 0a 20 20 2a  re NULL. If .  *
26f49 2a 20 61 6e 79 20 61 72 65 2c 20 74 68 65 6e 20  * any are, then 
26f4a 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  the constraint i
26f4b 73 20 63 6f 6e 73 69 64 65 72 65 64 20 73 61 74  s considered sat
26f4c 69 73 66 69 65 64 2e 20 4e 6f 20 6e 65 65 64 20  isfied. No need 
26f4d 74 6f 20 0a 20 20 2a 2a 20 73 65 61 72 63 68 20  to .  ** search 
26f4e 66 6f 72 20 61 20 6d 61 74 63 68 69 6e 67 20 72  for a matching r
26f4f 6f 77 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74  ow in the parent
26f50 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 66   table.  */.  if
26f51 28 20 6e 49 6e 63 72 3c 30 20 29 7b 0a 20 20 20  ( nIncr<0 ){.   
26f52 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26f53 70 32 28 76 2c 20 4f 50 5f 46 6b 49 66 5a 65 72  p2(v, OP_FkIfZer
26f54 6f 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65  o, pFKey->isDefe
26f55 72 72 65 64 2c 20 69 4f 6b 29 3b 0a 20 20 7d 0a  rred, iOk);.  }.
26f56 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 4b    for(i=0; i<pFK
26f57 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ey->nCol; i++){.
26f58 20 20 20 20 69 6e 74 20 69 52 65 67 20 3d 20 61      int iReg = a
26f59 69 43 6f 6c 5b 69 5d 20 2b 20 72 65 67 44 61 74  iCol[i] + regDat
26f5a 61 20 2b 20 31 3b 0a 20 20 20 20 73 71 6c 69 74  a + 1;.    sqlit
26f5b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26f5c 4f 50 5f 49 73 4e 75 6c 6c 2c 20 69 52 65 67 2c  OP_IsNull, iReg,
26f5d 20 69 4f 6b 29 3b 0a 20 20 7d 0a 0a 20 20 69 66   iOk);.  }..  if
26f5e 28 20 69 73 49 67 6e 6f 72 65 3d 3d 30 20 29 7b  ( isIgnore==0 ){
26f5f 0a 20 20 20 20 69 66 28 20 70 49 64 78 3d 3d 30  .    if( pIdx==0
26f60 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
26f61 70 49 64 78 20 69 73 20 4e 55 4c 4c 2c 20 74 68  pIdx is NULL, th
26f62 65 6e 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65  en the parent ke
26f63 79 20 69 73 20 74 68 65 20 49 4e 54 45 47 45 52  y is the INTEGER
26f64 20 50 52 49 4d 41 52 59 20 4b 45 59 0a 20 20 20   PRIMARY KEY.   
26f65 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66 20     ** column of 
26f66 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
26f67 20 28 74 61 62 6c 65 20 70 54 61 62 29 2e 20 20   (table pTab).  
26f68 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 75  */.      int iMu
26f69 73 74 42 65 49 6e 74 3b 20 20 20 20 20 20 20 20  stBeInt;        
26f6a 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
26f6b 73 20 6f 66 20 4d 75 73 74 42 65 49 6e 74 20 69  s of MustBeInt i
26f6c 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20  nstruction */.  
26f6d 20 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 20      int regTemp 
26f6e 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
26f6f 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 0a  Reg(pParse);.  .
26f70 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20        /* Invoke 
26f71 4d 75 73 74 42 65 49 6e 74 20 74 6f 20 63 6f 65  MustBeInt to coe
26f72 72 63 65 20 74 68 65 20 63 68 69 6c 64 20 6b 65  rce the child ke
26f73 79 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e  y value to an in
26f74 74 65 67 65 72 20 28 69 2e 65 2e 20 0a 20 20 20  teger (i.e. .   
26f75 20 20 20 2a 2a 20 61 70 70 6c 79 20 74 68 65 20     ** apply the 
26f76 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20  affinity of the 
26f77 70 61 72 65 6e 74 20 6b 65 79 29 2e 20 49 66 20  parent key). If 
26f78 74 68 69 73 20 66 61 69 6c 73 2c 20 74 68 65 6e  this fails, then
26f79 20 74 68 65 72 65 0a 20 20 20 20 20 20 2a 2a 20   there.      ** 
26f7a 69 73 20 6e 6f 20 6d 61 74 63 68 69 6e 67 20 70  is no matching p
26f7b 61 72 65 6e 74 20 6b 65 79 2e 20 42 65 66 6f 72  arent key. Befor
26f7c 65 20 75 73 69 6e 67 20 4d 75 73 74 42 65 49 6e  e using MustBeIn
26f7d 74 2c 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f  t, make a copy o
26f7e 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76  f.      ** the v
26f7f 61 6c 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  alue. Otherwise,
26f80 20 74 68 65 20 76 61 6c 75 65 20 69 6e 73 65 72   the value inser
26f81 74 65 64 20 69 6e 74 6f 20 74 68 65 20 63 68 69  ted into the chi
26f82 6c 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 0a 20 20  ld key column.  
26f83 20 20 20 20 2a 2a 20 77 69 6c 6c 20 68 61 76 65      ** will have
26f84 20 49 4e 54 45 47 45 52 20 61 66 66 69 6e 69 74   INTEGER affinit
26f85 79 20 61 70 70 6c 69 65 64 20 74 6f 20 69 74 2c  y applied to it,
26f86 20 77 68 69 63 68 20 6d 61 79 20 6e 6f 74 20 62   which may not b
26f87 65 20 63 6f 72 72 65 63 74 2e 20 20 2a 2f 0a 20  e correct.  */. 
26f88 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26f89 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f  AddOp2(v, OP_SCo
26f8a 70 79 2c 20 61 69 43 6f 6c 5b 30 5d 2b 31 2b 72  py, aiCol[0]+1+r
26f8b 65 67 44 61 74 61 2c 20 72 65 67 54 65 6d 70 29  egData, regTemp)
26f8c 3b 0a 20 20 20 20 20 20 69 4d 75 73 74 42 65 49  ;.      iMustBeI
26f8d 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  nt = sqlite3Vdbe
26f8e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d 75 73  AddOp2(v, OP_Mus
26f8f 74 42 65 49 6e 74 2c 20 72 65 67 54 65 6d 70 2c  tBeInt, regTemp,
26f90 20 30 29 3b 0a 20 20 0a 20 20 20 20 20 20 2f 2a   0);.  .      /*
26f91 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20 74   If the parent t
26f92 61 62 6c 65 20 69 73 20 74 68 65 20 73 61 6d 65  able is the same
26f93 20 61 73 20 74 68 65 20 63 68 69 6c 64 20 74 61   as the child ta
26f94 62 6c 65 2c 20 61 6e 64 20 77 65 20 61 72 65 20  ble, and we are 
26f95 61 62 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20 74  about.      ** t
26f96 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  o increment the 
26f97 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74  constraint-count
26f98 65 72 20 28 69 2e 65 2e 20 74 68 69 73 20 69 73  er (i.e. this is
26f99 20 61 6e 20 49 4e 53 45 52 54 20 6f 70 65 72 61   an INSERT opera
26f9a 74 69 6f 6e 29 2c 0a 20 20 20 20 20 20 2a 2a 20  tion),.      ** 
26f9b 74 68 65 6e 20 63 68 65 63 6b 20 69 66 20 74 68  then check if th
26f9c 65 20 72 6f 77 20 62 65 69 6e 67 20 69 6e 73 65  e row being inse
26f9d 72 74 65 64 20 6d 61 74 63 68 65 73 20 69 74 73  rted matches its
26f9e 65 6c 66 2e 20 49 66 20 73 6f 2c 20 64 6f 20 6e  elf. If so, do n
26f9f 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 63 72  ot.      ** incr
26fa0 65 6d 65 6e 74 20 74 68 65 20 63 6f 6e 73 74 72  ement the constr
26fa1 61 69 6e 74 2d 63 6f 75 6e 74 65 72 2e 20 20 2a  aint-counter.  *
26fa2 2f 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  /.      if( pTab
26fa3 3d 3d 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 20 26  ==pFKey->pFrom &
26fa4 26 20 6e 49 6e 63 72 3d 3d 31 20 29 7b 0a 20 20  & nIncr==1 ){.  
26fa5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26fa6 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 45 71  eAddOp3(v, OP_Eq
26fa7 2c 20 72 65 67 44 61 74 61 2c 20 69 4f 6b 2c 20  , regData, iOk, 
26fa8 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20  regTemp);.      
26fa9 7d 0a 20 20 0a 20 20 20 20 20 20 73 71 6c 69 74  }.  .      sqlit
26faa 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
26fab 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c 20 70  se, iCur, iDb, p
26fac 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64  Tab, OP_OpenRead
26fad 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
26fae 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
26faf 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72  _NotExists, iCur
26fb0 2c 20 30 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20  , 0, regTemp);. 
26fb1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26fb2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
26fb3 6f 2c 20 30 2c 20 69 4f 6b 29 3b 0a 20 20 20 20  o, 0, iOk);.    
26fb4 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
26fb5 70 48 65 72 65 28 76 2c 20 73 71 6c 69 74 65 33  pHere(v, sqlite3
26fb6 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
26fb7 76 29 2d 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  v)-2);.      sql
26fb8 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
26fb9 28 76 2c 20 69 4d 75 73 74 42 65 49 6e 74 29 3b  (v, iMustBeInt);
26fba 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
26fbb 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
26fbc 72 73 65 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20  rse, regTemp);. 
26fbd 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
26fbe 69 6e 74 20 6e 43 6f 6c 20 3d 20 70 46 4b 65 79  int nCol = pFKey
26fbf 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 69 6e  ->nCol;.      in
26fc0 74 20 72 65 67 54 65 6d 70 20 3d 20 73 71 6c 69  t regTemp = sqli
26fc1 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
26fc2 70 50 61 72 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20  pParse, nCol);. 
26fc3 20 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 20       int regRec 
26fc4 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
26fc5 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
26fc6 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
26fc7 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
26fc8 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
26fc9 49 64 78 29 3b 0a 20 20 0a 20 20 20 20 20 20 73  Idx);.  .      s
26fca 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
26fcb 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c  (v, OP_OpenRead,
26fcc 20 69 43 75 72 2c 20 70 49 64 78 2d 3e 74 6e 75   iCur, pIdx->tnu
26fcd 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  m, iDb);.      s
26fce 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
26fcf 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 2a  P4(v, -1, (char*
26fd0 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46  )pKey, P4_KEYINF
26fd1 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20  O_HANDOFF);.    
26fd2 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
26fd3 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; i++){.       
26fd4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26fd5 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
26fd6 61 69 43 6f 6c 5b 69 5d 2b 31 2b 72 65 67 44 61  aiCol[i]+1+regDa
26fd7 74 61 2c 20 72 65 67 54 65 6d 70 2b 69 29 3b 0a  ta, regTemp+i);.
26fd8 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20        }.  .     
26fd9 20 2f 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e   /* If the paren
26fda 74 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 73  t table is the s
26fdb 61 6d 65 20 61 73 20 74 68 65 20 63 68 69 6c 64  ame as the child
26fdc 20 74 61 62 6c 65 2c 20 61 6e 64 20 77 65 20 61   table, and we a
26fdd 72 65 20 61 62 6f 75 74 0a 20 20 20 20 20 20 2a  re about.      *
26fde 2a 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74  * to increment t
26fdf 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f  he constraint-co
26fe0 75 6e 74 65 72 20 28 69 2e 65 2e 20 74 68 69 73  unter (i.e. this
26fe1 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 6f 70   is an INSERT op
26fe2 65 72 61 74 69 6f 6e 29 2c 0a 20 20 20 20 20 20  eration),.      
26fe3 2a 2a 20 74 68 65 6e 20 63 68 65 63 6b 20 69 66  ** then check if
26fe4 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 69   the row being i
26fe5 6e 73 65 72 74 65 64 20 6d 61 74 63 68 65 73 20  nserted matches 
26fe6 69 74 73 65 6c 66 2e 20 49 66 20 73 6f 2c 20 64  itself. If so, d
26fe7 6f 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 69  o not.      ** i
26fe8 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 6e  ncrement the con
26fe9 73 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 2e  straint-counter.
26fea 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70    */.      if( p
26feb 54 61 62 3d 3d 70 46 4b 65 79 2d 3e 70 46 72 6f  Tab==pFKey->pFro
26fec 6d 20 26 26 20 6e 49 6e 63 72 3d 3d 31 20 29 7b  m && nIncr==1 ){
26fed 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 4a 75  .        int iJu
26fee 6d 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  mp = sqlite3Vdbe
26fef 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 20 2b  CurrentAddr(v) +
26ff0 20 6e 43 6f 6c 20 2b 20 31 3b 0a 20 20 20 20 20   nCol + 1;.     
26ff1 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43     for(i=0; i<nC
26ff2 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
26ff3 20 20 20 20 69 6e 74 20 69 43 68 69 6c 64 20 3d      int iChild =
26ff4 20 61 69 43 6f 6c 5b 69 5d 2b 31 2b 72 65 67 44   aiCol[i]+1+regD
26ff5 61 74 61 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ata;.          i
26ff6 6e 74 20 69 50 61 72 65 6e 74 20 3d 20 70 49 64  nt iParent = pId
26ff7 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 2b 31  x->aiColumn[i]+1
26ff8 2b 72 65 67 44 61 74 61 3b 0a 20 20 20 20 20 20  +regData;.      
26ff9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26ffa 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c 20  ddOp3(v, OP_Ne, 
26ffb 69 43 68 69 6c 64 2c 20 69 4a 75 6d 70 2c 20 69  iChild, iJump, i
26ffc 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 20  Parent);.       
26ffd 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
26ffe 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
26fff 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 4f 6b 29  OP_Goto, 0, iOk)
27000 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20  ;.      }.  .   
27001 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27002 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp3(v, OP_MakeR
27003 65 63 6f 72 64 2c 20 72 65 67 54 65 6d 70 2c 20  ecord, regTemp, 
27004 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0a 20  nCol, regRec);. 
27005 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
27006 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
27007 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69  sqlite3IndexAffi
27008 6e 69 74 79 53 74 72 28 76 2c 20 70 49 64 78 29  nityStr(v, pIdx)
27009 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
2700a 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74  te3VdbeAddOp4Int
2700b 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 69 43  (v, OP_Found, iC
2700c 75 72 2c 20 69 4f 6b 2c 20 72 65 67 52 65 63 2c  ur, iOk, regRec,
2700d 20 30 29 3b 0a 20 20 0a 20 20 20 20 20 20 73 71   0);.  .      sq
2700e 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2700f 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52  Reg(pParse, regR
27010 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ec);.      sqlit
27011 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
27012 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 54 65  ge(pParse, regTe
27013 6d 70 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 7d  mp, nCol);.    }
27014 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 46 4b  .  }..  if( !pFK
27015 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20 26  ey->isDeferred &
27016 26 20 21 70 50 61 72 73 65 2d 3e 70 54 6f 70 6c  & !pParse->pTopl
27017 65 76 65 6c 20 26 26 20 21 70 50 61 72 73 65 2d  evel && !pParse-
27018 3e 69 73 4d 75 6c 74 69 57 72 69 74 65 20 29 7b  >isMultiWrite ){
27019 0a 20 20 20 20 2f 2a 20 53 70 65 63 69 61 6c 20  .    /* Special 
2701a 63 61 73 65 3a 20 49 66 20 74 68 69 73 20 69 73  case: If this is
2701b 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65   an INSERT state
2701c 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c 20 69  ment that will i
2701d 6e 73 65 72 74 20 65 78 61 63 74 6c 79 0a 20 20  nsert exactly.  
2701e 20 20 2a 2a 20 6f 6e 65 20 72 6f 77 20 69 6e 74    ** one row int
2701f 6f 20 74 68 65 20 74 61 62 6c 65 2c 20 72 61 69  o the table, rai
27020 73 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20  se a constraint 
27021 69 6d 6d 65 64 69 61 74 65 6c 79 20 69 6e 73 74  immediately inst
27022 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a 20 69 6e  ead of.    ** in
27023 63 72 65 6d 65 6e 74 69 6e 67 20 61 20 63 6f 75  crementing a cou
27024 6e 74 65 72 2e 20 54 68 69 73 20 69 73 20 6e 65  nter. This is ne
27025 63 65 73 73 61 72 79 20 61 73 20 74 68 65 20 56  cessary as the V
27026 4d 20 63 6f 64 65 20 69 73 20 62 65 69 6e 67 0a  M code is being.
27027 20 20 20 20 2a 2a 20 67 65 6e 65 72 61 74 65 64      ** generated
27028 20 66 6f 72 20 77 69 6c 6c 20 6e 6f 74 20 6f 70   for will not op
27029 65 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  en a statement t
2702a 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 2a 2f 0a  ransaction.  */.
2702b 20 20 20 20 61 73 73 65 72 74 28 20 6e 49 6e 63      assert( nInc
2702c 72 3d 3d 31 20 29 3b 0a 20 20 20 20 73 71 6c 69  r==1 );.    sqli
2702d 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e  te3HaltConstrain
2702e 74 28 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  t(.        pPars
2702f 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 22 66 6f  e, OE_Abort, "fo
27030 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
27031 61 69 6e 74 20 66 61 69 6c 65 64 22 2c 20 50 34  aint failed", P4
27032 5f 53 54 41 54 49 43 0a 20 20 20 20 29 3b 0a 20  _STATIC.    );. 
27033 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
27034 6e 49 6e 63 72 3e 30 20 26 26 20 70 46 4b 65 79  nIncr>0 && pFKey
27035 2d 3e 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20  ->isDeferred==0 
27036 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
27037 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50  ParseToplevel(pP
27038 61 72 73 65 29 2d 3e 6d 61 79 41 62 6f 72 74 20  arse)->mayAbort 
27039 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 1;.    }.    s
2703a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2703b 28 76 2c 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72  (v, OP_FkCounter
2703c 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72  , pFKey->isDefer
2703d 72 65 64 2c 20 6e 49 6e 63 72 29 3b 0a 20 20 7d  red, nIncr);.  }
2703e 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  ..  sqlite3VdbeR
2703f 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69  esolveLabel(v, i
27040 4f 6b 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  Ok);.  sqlite3Vd
27041 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
27042 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a 7d 0a 0a  lose, iCur);.}..
27043 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
27044 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f  ion is called to
27045 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 65   generate code e
27046 78 65 63 75 74 65 64 20 77 68 65 6e 20 61 20 72  xecuted when a r
27047 6f 77 20 69 73 20 64 65 6c 65 74 65 64 0a 2a 2a  ow is deleted.**
27048 20 66 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74   from the parent
27049 20 74 61 62 6c 65 20 6f 66 20 66 6f 72 65 69 67   table of foreig
2704a 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
2704b 20 70 46 4b 65 79 20 61 6e 64 2c 20 69 66 20 70   pFKey and, if p
2704c 46 4b 65 79 20 69 73 20 0a 2a 2a 20 64 65 66 65  FKey is .** defe
2704d 72 72 65 64 2c 20 77 68 65 6e 20 61 20 72 6f 77  rred, when a row
2704e 20 69 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74   is inserted int
2704f 6f 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  o the same table
27050 2e 20 57 68 65 6e 20 67 65 6e 65 72 61 74 69 6e  . When generatin
27051 67 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20 61 6e  g.** code for an
27052 20 53 51 4c 20 55 50 44 41 54 45 20 6f 70 65 72   SQL UPDATE oper
27053 61 74 69 6f 6e 2c 20 74 68 69 73 20 66 75 6e 63  ation, this func
27054 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c 6c  tion may be call
27055 65 64 20 74 77 69 63 65 20 2d 0a 2a 2a 20 6f 6e  ed twice -.** on
27056 63 65 20 74 6f 20 22 64 65 6c 65 74 65 22 20 74  ce to "delete" t
27057 68 65 20 6f 6c 64 20 72 6f 77 20 61 6e 64 20 6f  he old row and o
27058 6e 63 65 20 74 6f 20 22 69 6e 73 65 72 74 22 20  nce to "insert" 
27059 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a  the new row..**.
2705a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  ** The code gene
2705b 72 61 74 65 64 20 62 79 20 74 68 69 73 20 66 75  rated by this fu
2705c 6e 63 74 69 6f 6e 20 73 63 61 6e 73 20 74 68 72  nction scans thr
2705d 6f 75 67 68 20 74 68 65 20 72 6f 77 73 20 69 6e  ough the rows in
2705e 20 74 68 65 20 63 68 69 6c 64 0a 2a 2a 20 74 61   the child.** ta
2705f 62 6c 65 20 74 68 61 74 20 63 6f 72 72 65 73 70  ble that corresp
27060 6f 6e 64 20 74 6f 20 74 68 65 20 70 61 72 65 6e  ond to the paren
27061 74 20 74 61 62 6c 65 20 72 6f 77 20 62 65 69 6e  t table row bein
27062 67 20 64 65 6c 65 74 65 64 20 6f 72 20 69 6e 73  g deleted or ins
27063 65 72 74 65 64 2e 0a 2a 2a 20 46 6f 72 20 65 61  erted..** For ea
27064 63 68 20 63 68 69 6c 64 20 72 6f 77 20 66 6f 75  ch child row fou
27065 6e 64 2c 20 6f 6e 65 20 6f 66 20 74 68 65 20 66  nd, one of the f
27066 6f 6c 6c 6f 77 69 6e 67 20 61 63 74 69 6f 6e 73  ollowing actions
27067 20 69 73 20 74 61 6b 65 6e 3a 0a 2a 2a 0a 2a 2a   is taken:.**.**
27068 20 20 20 4f 70 65 72 61 74 69 6f 6e 20 7c 20 46     Operation | F
27069 4b 20 74 79 70 65 20 20 20 7c 20 41 63 74 69 6f  K type   | Actio
2706a 6e 20 74 61 6b 65 6e 0a 2a 2a 20 20 20 2d 2d 2d  n taken.**   ---
2706b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2706c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2706d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2706e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2706f 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 44 45 4c  -------.**   DEL
27070 45 54 45 20 20 20 20 20 20 69 6d 6d 65 64 69 61  ETE      immedia
27071 74 65 20 20 20 49 6e 63 72 65 6d 65 6e 74 20 74  te   Increment t
27072 68 65 20 22 69 6d 6d 65 64 69 61 74 65 20 63 6f  he "immediate co
27073 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72  nstraint counter
27074 22 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  "..**           
27075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27076 4f 72 2c 20 69 66 20 74 68 65 20 4f 4e 20 28 55  Or, if the ON (U
27077 50 44 41 54 45 7c 44 45 4c 45 54 45 29 20 61 63  PDATE|DELETE) ac
27078 74 69 6f 6e 20 69 73 20 52 45 53 54 52 49 43 54  tion is RESTRICT
27079 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ,.**            
2707a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
2707b 68 72 6f 77 20 61 20 22 66 6f 72 65 69 67 6e 20  hrow a "foreign 
2707c 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66  key constraint f
2707d 61 69 6c 65 64 22 20 65 78 63 65 70 74 69 6f 6e  ailed" exception
2707e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45 52 54  ..**.**   INSERT
2707f 20 20 20 20 20 20 69 6d 6d 65 64 69 61 74 65 20        immediate 
27080 20 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20    Decrement the 
27081 22 69 6d 6d 65 64 69 61 74 65 20 63 6f 6e 73 74  "immediate const
27082 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a  raint counter"..
27083 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45 54 45 20 20  **.**   DELETE  
27084 20 20 20 20 64 65 66 65 72 72 65 64 20 20 20 20      deferred    
27085 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 22 64  Increment the "d
27086 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
27087 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 20  nt counter"..** 
27088 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27089 20 20 20 20 20 20 20 20 20 20 4f 72 2c 20 69 66            Or, if
2708a 20 74 68 65 20 4f 4e 20 28 55 50 44 41 54 45 7c   the ON (UPDATE|
2708b 44 45 4c 45 54 45 29 20 61 63 74 69 6f 6e 20 69  DELETE) action i
2708c 73 20 52 45 53 54 52 49 43 54 2c 0a 2a 2a 20 20  s RESTRICT,.**  
2708d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2708e 20 20 20 20 20 20 20 20 20 74 68 72 6f 77 20 61           throw a
2708f 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   "foreign key co
27090 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22  nstraint failed"
27091 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a   exception..**.*
27092 2a 20 20 20 49 4e 53 45 52 54 20 20 20 20 20 20  *   INSERT      
27093 64 65 66 65 72 72 65 64 20 20 20 20 44 65 63 72  deferred    Decr
27094 65 6d 65 6e 74 20 74 68 65 20 22 64 65 66 65 72  ement the "defer
27095 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  red constraint c
27096 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 54  ounter"..**.** T
27097 68 65 73 65 20 6f 70 65 72 61 74 69 6f 6e 73 20  hese operations 
27098 61 72 65 20 69 64 65 6e 74 69 66 69 65 64 20 69  are identified i
27099 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61 74  n the comment at
2709a 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 69 73   the top of this
2709b 20 66 69 6c 65 20 0a 2a 2a 20 28 66 6b 65 79 2e   file .** (fkey.
2709c 63 29 20 61 73 20 22 49 2e 32 22 20 61 6e 64 20  c) as "I.2" and 
2709d 22 44 2e 32 22 2e 0a 2a 2f 0a 73 74 61 74 69 63  "D.2"..*/.static
2709e 20 76 6f 69 64 20 66 6b 53 63 61 6e 43 68 69 6c   void fkScanChil
2709f 64 72 65 6e 28 0a 20 20 50 61 72 73 65 20 2a 70  dren(.  Parse *p
270a0 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
270a1 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
270a2 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
270a3 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20  cList *pSrc,    
270a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
270a5 20 53 72 63 4c 69 73 74 20 63 6f 6e 74 61 69 6e   SrcList contain
270a6 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f  ing the table to
270a7 20 73 63 61 6e 20 2a 2f 0a 20 20 54 61 62 6c 65   scan */.  Table
270a8 20 2a 70 54 61 62 2c 0a 20 20 49 6e 64 65 78 20   *pTab,.  Index 
270a9 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20 20 20  *pIdx,          
270aa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
270ab 65 69 67 6e 20 6b 65 79 20 69 6e 64 65 78 20 2a  eign key index *
270ac 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c  /.  FKey *pFKey,
270ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270ae 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b      /* Foreign k
270af 65 79 20 72 65 6c 61 74 69 6f 6e 73 68 69 70 20  ey relationship 
270b0 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43 6f 6c 2c  */.  int *aiCol,
270b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270b2 20 20 20 20 20 2f 2a 20 4d 61 70 20 66 72 6f 6d       /* Map from
270b3 20 70 49 64 78 20 63 6f 6c 73 20 74 6f 20 63 68   pIdx cols to ch
270b4 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c 73 20 2a  ild table cols *
270b5 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 2c  /.  int regData,
270b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270b7 20 20 20 20 2f 2a 20 52 65 66 65 72 65 6e 63 65      /* Reference
270b8 64 20 74 61 62 6c 65 20 64 61 74 61 20 73 74 61  d table data sta
270b9 72 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  rts here */.  in
270ba 74 20 6e 49 6e 63 72 20 20 20 20 20 20 20 20 20  t nIncr         
270bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
270bc 20 41 6d 6f 75 6e 74 20 74 6f 20 69 6e 63 72 65   Amount to incre
270bd 6d 65 6e 74 20 64 65 66 65 72 72 65 64 20 63 6f  ment deferred co
270be 75 6e 74 65 72 20 62 79 20 2a 2f 0a 29 7b 0a 20  unter by */.){. 
270bf 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
270c0 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20  Parse->db;      
270c1 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
270c2 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  dle */.  int i; 
270c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270c4 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
270c5 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
270c6 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 20  .  Expr *pWhere 
270c7 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
270c8 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75     /* WHERE clau
270c9 73 65 20 74 6f 20 73 63 61 6e 20 77 69 74 68 20  se to scan with 
270ca 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  */.  NameContext
270cb 20 73 4e 61 6d 65 43 6f 6e 74 65 78 74 3b 20 20   sNameContext;  
270cc 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
270cd 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65 20  used to resolve 
270ce 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
270cf 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57 49    WhereInfo *pWI
270d0 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  nfo;            
270d1 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 75 73 65    /* Context use
270d2 64 20 62 79 20 73 71 6c 69 74 65 33 57 68 65 72  d by sqlite3Wher
270d3 65 58 58 58 28 29 20 2a 2f 0a 20 20 69 6e 74 20  eXXX() */.  int 
270d4 69 46 6b 49 66 5a 65 72 6f 20 3d 20 30 3b 20 20  iFkIfZero = 0;  
270d5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
270d6 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 46 6b 49  ddress of OP_FkI
270d7 66 5a 65 72 6f 20 2a 2f 0a 20 20 56 64 62 65 20  fZero */.  Vdbe 
270d8 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
270d9 64 62 65 28 70 50 61 72 73 65 29 3b 0a 0a 20 20  dbe(pParse);..  
270da 61 73 73 65 72 74 28 20 21 70 49 64 78 20 7c 7c  assert( !pIdx ||
270db 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3d 3d 70   pIdx->pTable==p
270dc 54 61 62 20 29 3b 0a 0a 20 20 69 66 28 20 6e 49  Tab );..  if( nI
270dd 6e 63 72 3c 30 20 29 7b 0a 20 20 20 20 69 46 6b  ncr<0 ){.    iFk
270de 49 66 5a 65 72 6f 20 3d 20 73 71 6c 69 74 65 33  IfZero = sqlite3
270df 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
270e0 5f 46 6b 49 66 5a 65 72 6f 2c 20 70 46 4b 65 79  _FkIfZero, pFKey
270e1 2d 3e 69 73 44 65 66 65 72 72 65 64 2c 20 30 29  ->isDeferred, 0)
270e2 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61  ;.  }..  /* Crea
270e3 74 65 20 61 6e 20 45 78 70 72 20 6f 62 6a 65 63  te an Expr objec
270e4 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 61  t representing a
270e5 6e 20 53 51 4c 20 65 78 70 72 65 73 73 69 6f 6e  n SQL expression
270e6 20 6c 69 6b 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a   like:.  **.  **
270e7 20 20 20 3c 70 61 72 65 6e 74 2d 6b 65 79 31 3e     <parent-key1>
270e8 20 3d 20 3c 63 68 69 6c 64 2d 6b 65 79 31 3e 20   = <child-key1> 
270e9 41 4e 44 20 3c 70 61 72 65 6e 74 2d 6b 65 79 32  AND <parent-key2
270ea 3e 20 3d 20 3c 63 68 69 6c 64 2d 6b 65 79 32 3e  > = <child-key2>
270eb 20 2e 2e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54   ....  **.  ** T
270ec 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
270ed 75 65 6e 63 65 20 75 73 65 64 20 66 6f 72 20 74  uence used for t
270ee 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 73 68  he comparison sh
270ef 6f 75 6c 64 20 62 65 20 74 68 61 74 20 6f 66 0a  ould be that of.
270f0 20 20 2a 2a 20 74 68 65 20 70 61 72 65 6e 74 20    ** the parent 
270f1 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e 20 54 68 65  key columns. The
270f2 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65   affinity of the
270f3 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
270f4 6d 6e 20 73 68 6f 75 6c 64 0a 20 20 2a 2a 20 62  mn should.  ** b
270f5 65 20 61 70 70 6c 69 65 64 20 74 6f 20 65 61 63  e applied to eac
270f6 68 20 63 68 69 6c 64 20 6b 65 79 20 76 61 6c 75  h child key valu
270f7 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 6f 6d  e before the com
270f8 70 61 72 69 73 6f 6e 20 74 61 6b 65 73 20 70 6c  parison takes pl
270f9 61 63 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  ace..  */.  for(
270fa 69 3d 30 3b 20 69 3c 70 46 4b 65 79 2d 3e 6e 43  i=0; i<pFKey->nC
270fb 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78  ol; i++){.    Ex
270fc 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20  pr *pLeft;      
270fd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
270fe 61 6c 75 65 20 66 72 6f 6d 20 70 61 72 65 6e 74  alue from parent
270ff 20 74 61 62 6c 65 20 72 6f 77 20 2a 2f 0a 20 20   table row */.  
27100 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20    Expr *pRight; 
27101 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27102 2f 2a 20 43 6f 6c 75 6d 6e 20 72 65 66 20 74 6f  /* Column ref to
27103 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a   child table */.
27104 20 20 20 20 45 78 70 72 20 2a 70 45 71 3b 20 20      Expr *pEq;  
27105 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27106 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
27107 28 70 4c 65 66 74 20 3d 20 70 52 69 67 68 74 29  (pLeft = pRight)
27108 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c   */.    int iCol
27109 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2710a 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
2710b 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63 68 69 6c  f column in chil
2710c 64 20 74 61 62 6c 65 20 2a 2f 20 0a 20 20 20 20  d table */ .    
2710d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c  const char *zCol
2710e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
2710f 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20   Name of column 
27110 69 6e 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a  in child table *
27111 2f 0a 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 73  /..    pLeft = s
27112 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54  qlite3Expr(db, T
27113 4b 5f 52 45 47 49 53 54 45 52 2c 20 30 29 3b 0a  K_REGISTER, 0);.
27114 20 20 20 20 69 66 28 20 70 4c 65 66 74 20 29 7b      if( pLeft ){
27115 0a 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 68  .      /* Set th
27116 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
27117 65 6e 63 65 20 61 6e 64 20 61 66 66 69 6e 69 74  ence and affinit
27118 79 20 6f 66 20 74 68 65 20 4c 48 53 20 6f 66 20  y of the LHS of 
27119 65 61 63 68 20 54 4b 5f 45 51 0a 20 20 20 20 20  each TK_EQ.     
2711a 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74   ** expression t
2711b 6f 20 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79  o the parent key
2711c 20 63 6f 6c 75 6d 6e 20 64 65 66 61 75 6c 74 73   column defaults
2711d 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  .  */.      if( 
2711e 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20  pIdx ){.        
2711f 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20  Column *pCol;.  
27120 20 20 20 20 20 20 69 43 6f 6c 20 3d 20 70 49 64        iCol = pId
27121 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a  x->aiColumn[i];.
27122 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26          pCol = &
27123 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 61 43  pIdx->pTable->aC
27124 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20  ol[iCol];.      
27125 20 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 20    pLeft->iTable 
27126 3d 20 72 65 67 44 61 74 61 2b 69 43 6f 6c 2b 31  = regData+iCol+1
27127 3b 0a 20 20 20 20 20 20 20 20 70 4c 65 66 74 2d  ;.        pLeft-
27128 3e 61 66 66 69 6e 69 74 79 20 3d 20 70 43 6f 6c  >affinity = pCol
27129 2d 3e 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20  ->affinity;.    
2712a 20 20 20 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c      pLeft->pColl
2712b 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
2712c 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2712d 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b 0a 20 20  pCol->zColl);.  
2712e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2712f 20 20 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65     pLeft->iTable
27130 20 3d 20 72 65 67 44 61 74 61 3b 0a 20 20 20 20   = regData;.    
27131 20 20 20 20 70 4c 65 66 74 2d 3e 61 66 66 69 6e      pLeft->affin
27132 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  ity = SQLITE_AFF
27133 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20  _INTEGER;.      
27134 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 43 6f 6c  }.    }.    iCol
27135 20 3d 20 61 69 43 6f 6c 20 3f 20 61 69 43 6f 6c   = aiCol ? aiCol
27136 5b 69 5d 20 3a 20 70 46 4b 65 79 2d 3e 61 43 6f  [i] : pFKey->aCo
27137 6c 5b 30 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20  l[0].iFrom;.    
27138 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20  assert( iCol>=0 
27139 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 70 46  );.    zCol = pF
2713a 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e 61 43 6f 6c  Key->pFrom->aCol
2713b 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  [iCol].zName;.  
2713c 20 20 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74    pRight = sqlit
2713d 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 44  e3Expr(db, TK_ID
2713e 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 70 45 71  , zCol);.    pEq
2713f 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
27140 70 50 61 72 73 65 2c 20 54 4b 5f 45 51 2c 20 70  pParse, TK_EQ, p
27141 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29  Left, pRight, 0)
27142 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 73  ;.    pWhere = s
27143 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64 62  qlite3ExprAnd(db
27144 2c 20 70 57 68 65 72 65 2c 20 70 45 71 29 3b 0a  , pWhere, pEq);.
27145 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
27146 20 63 68 69 6c 64 20 74 61 62 6c 65 20 69 73 20   child table is 
27147 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
27148 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 20 61 6e  parent table, an
27149 64 20 74 68 69 73 20 73 63 61 6e 0a 20 20 2a 2a  d this scan.  **
2714a 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61 63 65   is taking place
2714b 20 61 73 20 70 61 72 74 20 6f 66 20 61 20 44 45   as part of a DE
2714c 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e 20 28  LETE operation (
2714d 6f 70 65 72 61 74 69 6f 6e 20 44 2e 32 29 2c 20  operation D.2), 
2714e 6f 6d 69 74 20 74 68 65 0a 20 20 2a 2a 20 72 6f  omit the.  ** ro
2714f 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20  w being deleted 
27150 66 72 6f 6d 20 74 68 65 20 73 63 61 6e 20 62 79  from the scan by
27151 20 61 64 64 69 6e 67 20 28 24 72 6f 77 69 64 20   adding ($rowid 
27152 21 3d 20 72 6f 77 69 64 29 20 74 6f 20 74 68 65  != rowid) to the
27153 20 57 48 45 52 45 20 0a 20 20 2a 2a 20 63 6c 61   WHERE .  ** cla
27154 75 73 65 2c 20 77 68 65 72 65 20 24 72 6f 77 69  use, where $rowi
27155 64 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f  d is the rowid o
27156 66 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20  f the row being 
27157 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0a 20 20 69  deleted.  */.  i
27158 66 28 20 70 54 61 62 3d 3d 70 46 4b 65 79 2d 3e  f( pTab==pFKey->
27159 70 46 72 6f 6d 20 26 26 20 6e 49 6e 63 72 3e 30  pFrom && nIncr>0
2715a 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45   ){.    Expr *pE
2715b 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q;              
2715c 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73        /* Express
2715d 69 6f 6e 20 28 70 4c 65 66 74 20 3d 20 70 52 69  ion (pLeft = pRi
2715e 67 68 74 29 20 2a 2f 0a 20 20 20 20 45 78 70 72  ght) */.    Expr
2715f 20 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 20 20   *pLeft;        
27160 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
27161 75 65 20 66 72 6f 6d 20 70 61 72 65 6e 74 20 74  ue from parent t
27162 61 62 6c 65 20 72 6f 77 20 2a 2f 0a 20 20 20 20  able row */.    
27163 45 78 70 72 20 2a 70 52 69 67 68 74 3b 20 20 20  Expr *pRight;   
27164 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27165 20 43 6f 6c 75 6d 6e 20 72 65 66 20 74 6f 20 63   Column ref to c
27166 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hild table */.  
27167 20 20 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65    pLeft = sqlite
27168 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 52 45 47  3Expr(db, TK_REG
27169 49 53 54 45 52 2c 20 30 29 3b 0a 20 20 20 20 70  ISTER, 0);.    p
2716a 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45  Right = sqlite3E
2716b 78 70 72 28 64 62 2c 20 54 4b 5f 43 4f 4c 55 4d  xpr(db, TK_COLUM
2716c 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70  N, 0);.    if( p
2716d 4c 65 66 74 20 26 26 20 70 52 69 67 68 74 20 29  Left && pRight )
2716e 7b 0a 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 69  {.      pLeft->i
2716f 54 61 62 6c 65 20 3d 20 72 65 67 44 61 74 61 3b  Table = regData;
27170 0a 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 61 66  .      pLeft->af
27171 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
27172 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
27173 20 20 20 70 52 69 67 68 74 2d 3e 69 54 61 62 6c     pRight->iTabl
27174 65 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 69  e = pSrc->a[0].i
27175 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 70 52  Cursor;.      pR
27176 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20  ight->iColumn = 
27177 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 45  -1;.    }.    pE
27178 71 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  q = sqlite3PExpr
27179 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 45 2c 20  (pParse, TK_NE, 
2717a 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 30  pLeft, pRight, 0
2717b 29 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20  );.    pWhere = 
2717c 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64  sqlite3ExprAnd(d
2717d 62 2c 20 70 57 68 65 72 65 2c 20 70 45 71 29 3b  b, pWhere, pEq);
2717e 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c  .  }..  /* Resol
2717f 76 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  ve the reference
27180 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  s in the WHERE c
27181 6c 61 75 73 65 2e 20 2a 2f 0a 20 20 6d 65 6d 73  lause. */.  mems
27182 65 74 28 26 73 4e 61 6d 65 43 6f 6e 74 65 78 74  et(&sNameContext
27183 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65  , 0, sizeof(Name
27184 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73 4e 61  Context));.  sNa
27185 6d 65 43 6f 6e 74 65 78 74 2e 70 53 72 63 4c 69  meContext.pSrcLi
27186 73 74 20 3d 20 70 53 72 63 3b 0a 20 20 73 4e 61  st = pSrc;.  sNa
27187 6d 65 43 6f 6e 74 65 78 74 2e 70 50 61 72 73 65  meContext.pParse
27188 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c   = pParse;.  sql
27189 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
2718a 61 6d 65 73 28 26 73 4e 61 6d 65 43 6f 6e 74 65  ames(&sNameConte
2718b 78 74 2c 20 70 57 68 65 72 65 29 3b 0a 0a 20 20  xt, pWhere);..  
2718c 2f 2a 20 43 72 65 61 74 65 20 56 44 42 45 20 74  /* Create VDBE t
2718d 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  o loop through t
2718e 68 65 20 65 6e 74 72 69 65 73 20 69 6e 20 70 53  he entries in pS
2718f 72 63 20 74 68 61 74 20 6d 61 74 63 68 20 74 68  rc that match th
27190 65 20 57 48 45 52 45 0a 20 20 2a 2a 20 63 6c 61  e WHERE.  ** cla
27191 75 73 65 2e 20 49 66 20 74 68 65 20 63 6f 6e 73  use. If the cons
27192 74 72 61 69 6e 74 20 69 73 20 6e 6f 74 20 64 65  traint is not de
27193 66 65 72 72 65 64 2c 20 74 68 72 6f 77 20 61 6e  ferred, throw an
27194 20 65 78 63 65 70 74 69 6f 6e 20 66 6f 72 0a 20   exception for. 
27195 20 2a 2a 20 65 61 63 68 20 72 6f 77 20 66 6f 75   ** each row fou
27196 6e 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66  nd. Otherwise, f
27197 6f 72 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73  or deferred cons
27198 74 72 61 69 6e 74 73 2c 20 69 6e 63 72 65 6d 65  traints, increme
27199 6e 74 20 74 68 65 0a 20 20 2a 2a 20 64 65 66 65  nt the.  ** defe
2719a 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 20  rred constraint 
2719b 63 6f 75 6e 74 65 72 20 62 79 20 6e 49 6e 63 72  counter by nIncr
2719c 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 73 65   for each row se
2719d 6c 65 63 74 65 64 2e 20 20 2a 2f 0a 20 20 70 57  lected.  */.  pW
2719e 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68  Info = sqlite3Wh
2719f 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c  ereBegin(pParse,
271a0 20 70 53 72 63 2c 20 70 57 68 65 72 65 2c 20 30   pSrc, pWhere, 0
271a1 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e 49 6e 63  , 0);.  if( nInc
271a2 72 3e 30 20 26 26 20 70 46 4b 65 79 2d 3e 69 73  r>0 && pFKey->is
271a3 44 65 66 65 72 72 65 64 3d 3d 30 20 29 7b 0a 20  Deferred==0 ){. 
271a4 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 54     sqlite3ParseT
271a5 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 2d  oplevel(pParse)-
271a6 3e 6d 61 79 41 62 6f 72 74 20 3d 20 31 3b 0a 20  >mayAbort = 1;. 
271a7 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
271a8 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 43  AddOp2(v, OP_FkC
271a9 6f 75 6e 74 65 72 2c 20 70 46 4b 65 79 2d 3e 69  ounter, pFKey->i
271aa 73 44 65 66 65 72 72 65 64 2c 20 6e 49 6e 63 72  sDeferred, nIncr
271ab 29 3b 0a 20 20 69 66 28 20 70 57 49 6e 66 6f 20  );.  if( pWInfo 
271ac 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 57 68  ){.    sqlite3Wh
271ad 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
271ae 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20    }..  /* Clean 
271af 75 70 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  up the WHERE cla
271b0 75 73 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20  use constructed 
271b1 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 73 71 6c 69  above. */.  sqli
271b2 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
271b3 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 69 66 28  , pWhere);.  if(
271b4 20 69 46 6b 49 66 5a 65 72 6f 20 29 7b 0a 20 20   iFkIfZero ){.  
271b5 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
271b6 70 48 65 72 65 28 76 2c 20 69 46 6b 49 66 5a 65  pHere(v, iFkIfZe
271b7 72 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ro);.  }.}../*.*
271b8 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
271b9 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
271ba 72 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66  r to the head of
271bb 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
271bc 66 20 46 4b 0a 2a 2a 20 63 6f 6e 73 74 72 61 69  f FK.** constrai
271bd 6e 74 73 20 66 6f 72 20 77 68 69 63 68 20 74 61  nts for which ta
271be 62 6c 65 20 70 54 61 62 20 69 73 20 74 68 65 20  ble pTab is the 
271bf 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 46 6f  parent table. Fo
271c0 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 67 69  r example,.** gi
271c1 76 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ven the followin
271c2 67 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20  g schema:.**.** 
271c3 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74    CREATE TABLE t
271c4 31 28 61 20 50 52 49 4d 41 52 59 20 4b 45 59 29  1(a PRIMARY KEY)
271c5 3b 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41  ;.**   CREATE TA
271c6 42 4c 45 20 74 32 28 62 20 52 45 46 45 52 45 4e  BLE t2(b REFEREN
271c7 43 45 53 20 74 31 28 61 29 3b 0a 2a 2a 0a 2a 2a  CES t1(a);.**.**
271c8 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 66 75   Calling this fu
271c9 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 61 62 6c  nction with tabl
271ca 65 20 22 74 31 22 20 61 73 20 61 6e 20 61 72 67  e "t1" as an arg
271cb 75 6d 65 6e 74 20 72 65 74 75 72 6e 73 20 61 20  ument returns a 
271cc 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68  pointer.** to th
271cd 65 20 46 4b 65 79 20 73 74 72 75 63 74 75 72 65  e FKey structure
271ce 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
271cf 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  e foreign key co
271d0 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 61 62 6c  nstraint on tabl
271d1 65 0a 2a 2a 20 22 74 32 22 2e 20 43 61 6c 6c 69  e.** "t2". Calli
271d2 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ng this function
271d3 20 77 69 74 68 20 22 74 32 22 20 61 73 20 74 68   with "t2" as th
271d4 65 20 61 72 67 75 6d 65 6e 74 20 77 6f 75 6c 64  e argument would
271d5 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c   return a.** NUL
271d6 4c 20 70 6f 69 6e 74 65 72 20 28 61 73 20 74 68  L pointer (as th
271d7 65 72 65 20 61 72 65 20 6e 6f 20 46 4b 20 63 6f  ere are no FK co
271d8 6e 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77 68  nstraints for wh
271d9 69 63 68 20 74 32 20 69 73 20 74 68 65 20 70 61  ich t2 is the pa
271da 72 65 6e 74 0a 2a 2a 20 74 61 62 6c 65 29 2e 0a  rent.** table)..
271db 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
271dc 45 20 46 4b 65 79 20 2a 73 71 6c 69 74 65 33 46  E FKey *sqlite3F
271dd 6b 52 65 66 65 72 65 6e 63 65 73 28 54 61 62 6c  kReferences(Tabl
271de 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74 20  e *pTab){.  int 
271df 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
271e0 74 72 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e  trlen30(pTab->zN
271e1 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  ame);.  return (
271e2 46 4b 65 79 20 2a 29 73 71 6c 69 74 65 33 48 61  FKey *)sqlite3Ha
271e3 73 68 46 69 6e 64 28 26 70 54 61 62 2d 3e 70 53  shFind(&pTab->pS
271e4 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61 73 68 2c  chema->fkeyHash,
271e5 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 4e   pTab->zName, nN
271e6 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ame);.}../*.** T
271e7 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
271e8 6e 74 20 69 73 20 61 20 54 72 69 67 67 65 72 20  nt is a Trigger 
271e9 73 74 72 75 63 74 75 72 65 20 61 6c 6c 6f 63 61  structure alloca
271ea 74 65 64 20 62 79 20 74 68 65 20 0a 2a 2a 20 66  ted by the .** f
271eb 6b 41 63 74 69 6f 6e 54 72 69 67 67 65 72 28 29  kActionTrigger()
271ec 20 72 6f 75 74 69 6e 65 2e 20 54 68 69 73 20 66   routine. This f
271ed 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 73 20  unction deletes 
271ee 74 68 65 20 54 72 69 67 67 65 72 20 73 74 72 75  the Trigger stru
271ef 63 74 75 72 65 0a 2a 2a 20 61 6e 64 20 61 6c 6c  cture.** and all
271f0 20 6f 66 20 69 74 73 20 73 75 62 2d 63 6f 6d 70   of its sub-comp
271f1 6f 6e 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  onents..**.** Th
271f2 65 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74  e Trigger struct
271f3 75 72 65 20 6f 72 20 61 6e 79 20 6f 66 20 69 74  ure or any of it
271f4 73 20 73 75 62 2d 63 6f 6d 70 6f 6e 65 6e 74 73  s sub-components
271f5 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 65   may be allocate
271f6 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 6c 6f  d from.** the lo
271f7 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 62  okaside buffer b
271f8 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 64 61 74 61  elonging to data
271f9 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62 4d 65  base handle dbMe
271fa 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  m..*/.static voi
271fb 64 20 66 6b 54 72 69 67 67 65 72 44 65 6c 65 74  d fkTriggerDelet
271fc 65 28 73 71 6c 69 74 65 33 20 2a 64 62 4d 65 6d  e(sqlite3 *dbMem
271fd 2c 20 54 72 69 67 67 65 72 20 2a 70 29 7b 0a 20  , Trigger *p){. 
271fe 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 54 72   if( p ){.    Tr
271ff 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70  iggerStep *pStep
27200 20 3d 20 70 2d 3e 73 74 65 70 5f 6c 69 73 74 3b   = p->step_list;
27201 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
27202 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c 20 70 53  Delete(dbMem, pS
27203 74 65 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  tep->pWhere);.  
27204 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
27205 74 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c 20 70  tDelete(dbMem, p
27206 53 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29  Step->pExprList)
27207 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
27208 65 63 74 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c  ectDelete(dbMem,
27209 20 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 29   pStep->pSelect)
2720a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
2720b 72 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c 20 70  rDelete(dbMem, p
2720c 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 20 20 73 71  ->pWhen);.    sq
2720d 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 4d 65  lite3DbFree(dbMe
2720e 6d 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  m, p);.  }.}../*
2720f 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
27210 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 67  n is called to g
27211 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
27212 74 20 72 75 6e 73 20 77 68 65 6e 20 74 61 62 6c  t runs when tabl
27213 65 20 70 54 61 62 20 69 73 0a 2a 2a 20 62 65 69  e pTab is.** bei
27214 6e 67 20 64 72 6f 70 70 65 64 20 66 72 6f 6d 20  ng dropped from 
27215 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 54 68  the database. Th
27216 65 20 53 72 63 4c 69 73 74 20 70 61 73 73 65 64  e SrcList passed
27217 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
27218 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68  rgument.** to th
27219 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74  is function cont
2721a 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 65 6e  ains a single en
2721b 74 72 79 20 67 75 61 72 61 6e 74 65 65 64 20 74  try guaranteed t
2721c 6f 20 72 65 73 6f 6c 76 65 20 74 6f 0a 2a 2a 20  o resolve to.** 
2721d 74 61 62 6c 65 20 70 54 61 62 2e 0a 2a 2a 0a 2a  table pTab..**.*
2721e 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 6e 6f 20 63  * Normally, no c
2721f 6f 64 65 20 69 73 20 72 65 71 75 69 72 65 64 2e  ode is required.
27220 20 48 6f 77 65 76 65 72 2c 20 69 66 20 65 69 74   However, if eit
27221 68 65 72 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29 20  her.**.**   (a) 
27222 54 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65  The table is the
27223 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 6f 66   parent table of
27224 20 61 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74   a FK constraint
27225 2c 20 6f 72 0a 2a 2a 20 20 20 28 62 29 20 54 68  , or.**   (b) Th
27226 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 63  e table is the c
27227 68 69 6c 64 20 74 61 62 6c 65 20 6f 66 20 61 20  hild table of a 
27228 64 65 66 65 72 72 65 64 20 46 4b 20 63 6f 6e 73  deferred FK cons
27229 74 72 61 69 6e 74 20 61 6e 64 20 69 74 20 69 73  traint and it is
2722a 0a 2a 2a 20 20 20 20 20 20 20 64 65 74 65 72 6d  .**       determ
2722b 69 6e 65 64 20 61 74 20 72 75 6e 74 69 6d 65 20  ined at runtime 
2722c 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 6f  that there are o
2722d 75 74 73 74 61 6e 64 69 6e 67 20 64 65 66 65 72  utstanding defer
2722e 72 65 64 20 46 4b 20 0a 2a 2a 20 20 20 20 20 20  red FK .**      
2722f 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c   constraint viol
27230 61 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 64 61  ations in the da
27231 74 61 62 61 73 65 2c 0a 2a 2a 0a 2a 2a 20 74 68  tabase,.**.** th
27232 65 6e 20 74 68 65 20 65 71 75 69 76 61 6c 65 6e  en the equivalen
27233 74 20 6f 66 20 22 44 45 4c 45 54 45 20 46 52 4f  t of "DELETE FRO
27234 4d 20 3c 74 62 6c 3e 22 20 69 73 20 65 78 65 63  M <tbl>" is exec
27235 75 74 65 64 20 62 65 66 6f 72 65 20 64 72 6f 70  uted before drop
27236 70 69 6e 67 0a 2a 2a 20 74 68 65 20 74 61 62 6c  ping.** the tabl
27237 65 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  e from the datab
27238 61 73 65 2e 20 54 72 69 67 67 65 72 73 20 61 72  ase. Triggers ar
27239 65 20 64 69 73 61 62 6c 65 64 20 77 68 69 6c 65  e disabled while
2723a 20 72 75 6e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a   running this.**
2723b 20 44 45 4c 45 54 45 2c 20 62 75 74 20 66 6f 72   DELETE, but for
2723c 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73  eign key actions
2723d 20 61 72 65 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c   are not..*/.SQL
2723e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2723f 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54 61   sqlite3FkDropTa
27240 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ble(Parse *pPars
27241 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d  e, SrcList *pNam
27242 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b  e, Table *pTab){
27243 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
27244 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
27245 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51  f( (db->flags&SQ
27246 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73  LITE_ForeignKeys
27247 29 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28  ) && !IsVirtual(
27248 70 54 61 62 29 20 26 26 20 21 70 54 61 62 2d 3e  pTab) && !pTab->
27249 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 69  pSelect ){.    i
2724a 6e 74 20 69 53 6b 69 70 20 3d 20 30 3b 0a 20 20  nt iSkip = 0;.  
2724b 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
2724c 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2724d 65 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28  e);..    assert(
2724e 20 76 20 29 3b 20 20 20 20 20 20 20 20 20 20 20   v );           
2724f 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 68         /* VDBE h
27250 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
27251 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 20  allocated */.   
27252 20 69 66 28 20 73 71 6c 69 74 65 33 46 6b 52 65   if( sqlite3FkRe
27253 66 65 72 65 6e 63 65 73 28 70 54 61 62 29 3d 3d  ferences(pTab)==
27254 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65  0 ){.      /* Se
27255 61 72 63 68 20 66 6f 72 20 61 20 64 65 66 65 72  arch for a defer
27256 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  red foreign key 
27257 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f 72 20 77  constraint for w
27258 68 69 63 68 20 74 68 69 73 20 74 61 62 6c 65 0a  hich this table.
27259 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65 20        ** is the 
2725a 63 68 69 6c 64 20 74 61 62 6c 65 2e 20 49 66 20  child table. If 
2725b 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  one cannot be fo
2725c 75 6e 64 2c 20 72 65 74 75 72 6e 20 77 69 74 68  und, return with
2725d 6f 75 74 20 0a 20 20 20 20 20 20 2a 2a 20 67 65  out .      ** ge
2725e 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 56 44 42  nerating any VDB
2725f 45 20 63 6f 64 65 2e 20 49 66 20 6f 6e 65 20 63  E code. If one c
27260 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20 74 68 65  an be found, the
27261 6e 20 6a 75 6d 70 20 6f 76 65 72 0a 20 20 20 20  n jump over.    
27262 20 20 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20    ** the entire 
27263 44 45 4c 45 54 45 20 69 66 20 74 68 65 72 65 20  DELETE if there 
27264 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69  are no outstandi
27265 6e 67 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73  ng deferred cons
27266 74 72 61 69 6e 74 73 0a 20 20 20 20 20 20 2a 2a  traints.      **
27267 20 77 68 65 6e 20 74 68 69 73 20 73 74 61 74 65   when this state
27268 6d 65 6e 74 20 69 73 20 72 75 6e 2e 20 20 2a 2f  ment is run.  */
27269 0a 20 20 20 20 20 20 46 4b 65 79 20 2a 70 3b 0a  .      FKey *p;.
2726a 20 20 20 20 20 20 66 6f 72 28 70 3d 70 54 61 62        for(p=pTab
2726b 2d 3e 70 46 4b 65 79 3b 20 70 3b 20 70 3d 70 2d  ->pFKey; p; p=p-
2726c 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20 20  >pNextFrom){.   
2726d 20 20 20 20 20 69 66 28 20 70 2d 3e 69 73 44 65       if( p->isDe
2726e 66 65 72 72 65 64 20 29 20 62 72 65 61 6b 3b 0a  ferred ) break;.
2726f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
27270 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a 20  ( !p ) return;. 
27271 20 20 20 20 20 69 53 6b 69 70 20 3d 20 73 71 6c       iSkip = sql
27272 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
27273 6c 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  l(v);.      sqli
27274 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27275 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 2c 20 31 2c   OP_FkIfZero, 1,
27276 20 69 53 6b 69 70 29 3b 0a 20 20 20 20 7d 0a 0a   iSkip);.    }..
27277 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61      pParse->disa
27278 62 6c 65 54 72 69 67 67 65 72 73 20 3d 20 31 3b  bleTriggers = 1;
27279 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
2727a 74 65 46 72 6f 6d 28 70 50 61 72 73 65 2c 20 73  teFrom(pParse, s
2727b 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70  qlite3SrcListDup
2727c 28 64 62 2c 20 70 4e 61 6d 65 2c 20 30 29 2c 20  (db, pName, 0), 
2727d 30 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  0);.    pParse->
2727e 64 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 20  disableTriggers 
2727f 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20  = 0;..    /* If 
27280 74 68 65 20 44 45 4c 45 54 45 20 68 61 73 20 67  the DELETE has g
27281 65 6e 65 72 61 74 65 64 20 69 6d 6d 65 64 69 61  enerated immedia
27282 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  te foreign key c
27283 6f 6e 73 74 72 61 69 6e 74 20 0a 20 20 20 20 2a  onstraint .    *
27284 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c 20 68 61  * violations, ha
27285 6c 74 20 74 68 65 20 56 44 42 45 20 61 6e 64 20  lt the VDBE and 
27286 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
27287 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 62  at this point, b
27288 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20 61 6e 79  efore.    ** any
27289 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 74   modifications t
2728a 6f 20 74 68 65 20 73 63 68 65 6d 61 20 61 72 65  o the schema are
2728b 20 6d 61 64 65 2e 20 54 68 69 73 20 69 73 20 62   made. This is b
2728c 65 63 61 75 73 65 20 73 74 61 74 65 6d 65 6e 74  ecause statement
2728d 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74  .    ** transact
2728e 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 62 6c  ions are not abl
2728f 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 73 63  e to rollback sc
27290 68 65 6d 61 20 63 68 61 6e 67 65 73 2e 20 20 2a  hema changes.  *
27291 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
27292 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 6b  eAddOp2(v, OP_Fk
27293 49 66 5a 65 72 6f 2c 20 30 2c 20 73 71 6c 69 74  IfZero, 0, sqlit
27294 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
27295 72 28 76 29 2b 32 29 3b 0a 20 20 20 20 73 71 6c  r(v)+2);.    sql
27296 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69  ite3HaltConstrai
27297 6e 74 28 0a 20 20 20 20 20 20 20 20 70 50 61 72  nt(.        pPar
27298 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 22 66  se, OE_Abort, "f
27299 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
2729a 72 61 69 6e 74 20 66 61 69 6c 65 64 22 2c 20 50  raint failed", P
2729b 34 5f 53 54 41 54 49 43 0a 20 20 20 20 29 3b 0a  4_STATIC.    );.
2729c 0a 20 20 20 20 69 66 28 20 69 53 6b 69 70 20 29  .    if( iSkip )
2729d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2729e 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2729f 76 2c 20 69 53 6b 69 70 29 3b 0a 20 20 20 20 7d  v, iSkip);.    }
272a0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
272a1 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
272a2 61 6c 6c 65 64 20 77 68 65 6e 20 69 6e 73 65 72  alled when inser
272a3 74 69 6e 67 2c 20 64 65 6c 65 74 69 6e 67 20 6f  ting, deleting o
272a4 72 20 75 70 64 61 74 69 6e 67 20 61 20 72 6f 77  r updating a row
272a5 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61   of.** table pTa
272a6 62 20 74 6f 20 67 65 6e 65 72 61 74 65 20 56 44  b to generate VD
272a7 42 45 20 63 6f 64 65 20 74 6f 20 70 65 72 66 6f  BE code to perfo
272a8 72 6d 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  rm foreign key c
272a9 6f 6e 73 74 72 61 69 6e 74 20 0a 2a 2a 20 70 72  onstraint .** pr
272aa 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68 65  ocessing for the
272ab 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a   operation..**.*
272ac 2a 20 46 6f 72 20 61 20 44 45 4c 45 54 45 20 6f  * For a DELETE o
272ad 70 65 72 61 74 69 6f 6e 2c 20 70 61 72 61 6d 65  peration, parame
272ae 74 65 72 20 72 65 67 4f 6c 64 20 69 73 20 70 61  ter regOld is pa
272af 73 73 65 64 20 74 68 65 20 69 6e 64 65 78 20 6f  ssed the index o
272b0 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 72  f the.** first r
272b1 65 67 69 73 74 65 72 20 69 6e 20 61 6e 20 61 72  egister in an ar
272b2 72 61 79 20 6f 66 20 28 70 54 61 62 2d 3e 6e 43  ray of (pTab->nC
272b3 6f 6c 2b 31 29 20 72 65 67 69 73 74 65 72 73 20  ol+1) registers 
272b4 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 0a 2a  containing the.*
272b5 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72  * rowid of the r
272b6 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64  ow being deleted
272b7 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 65 61  , followed by ea
272b8 63 68 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ch of the column
272b9 20 76 61 6c 75 65 73 0a 2a 2a 20 6f 66 20 74 68   values.** of th
272ba 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65  e row being dele
272bb 74 65 64 2c 20 66 72 6f 6d 20 6c 65 66 74 20 74  ted, from left t
272bc 6f 20 72 69 67 68 74 2e 20 50 61 72 61 6d 65 74  o right. Paramet
272bd 65 72 20 72 65 67 4e 65 77 20 69 73 20 70 61 73  er regNew is pas
272be 73 65 64 0a 2a 2a 20 7a 65 72 6f 20 69 6e 20 74  sed.** zero in t
272bf 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
272c0 46 6f 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 70  For an INSERT op
272c1 65 72 61 74 69 6f 6e 2c 20 72 65 67 4f 6c 64 20  eration, regOld 
272c2 69 73 20 70 61 73 73 65 64 20 7a 65 72 6f 20 61  is passed zero a
272c3 6e 64 20 72 65 67 4e 65 77 20 69 73 20 70 61 73  nd regNew is pas
272c4 73 65 64 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  sed the.** first
272c5 20 72 65 67 69 73 74 65 72 20 6f 66 20 61 6e 20   register of an 
272c6 61 72 72 61 79 20 6f 66 20 28 70 54 61 62 2d 3e  array of (pTab->
272c7 6e 43 6f 6c 2b 31 29 20 72 65 67 69 73 74 65 72  nCol+1) register
272c8 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  s containing the
272c9 20 6e 65 77 0a 2a 2a 20 72 6f 77 20 64 61 74 61   new.** row data
272ca 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 55  ..**.** For an U
272cb 50 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e 2c  PDATE operation,
272cc 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
272cd 73 20 63 61 6c 6c 65 64 20 74 77 69 63 65 2e 20  s called twice. 
272ce 4f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74  Once before.** t
272cf 68 65 20 6f 72 69 67 69 6e 61 6c 20 72 65 63 6f  he original reco
272d0 72 64 20 69 73 20 64 65 6c 65 74 65 64 20 66 72  rd is deleted fr
272d1 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 75 73 69  om the table usi
272d2 6e 67 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 63  ng the calling c
272d3 6f 6e 76 65 6e 74 69 6f 6e 0a 2a 2a 20 64 65 73  onvention.** des
272d4 63 72 69 62 65 64 20 66 6f 72 20 44 45 4c 45 54  cribed for DELET
272d5 45 2e 20 54 68 65 6e 20 61 67 61 69 6e 20 61 66  E. Then again af
272d6 74 65 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  ter the original
272d7 20 72 65 63 6f 72 64 20 69 73 20 64 65 6c 65 74   record is delet
272d8 65 64 0a 2a 2a 20 62 75 74 20 62 65 66 6f 72 65  ed.** but before
272d9 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
272da 69 73 20 69 6e 73 65 72 74 65 64 20 75 73 69 6e  is inserted usin
272db 67 20 74 68 65 20 49 4e 53 45 52 54 20 63 6f 6e  g the INSERT con
272dc 76 65 6e 74 69 6f 6e 2e 20 0a 2a 2f 0a 53 51 4c  vention. .*/.SQL
272dd 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
272de 20 73 71 6c 69 74 65 33 46 6b 43 68 65 63 6b 28   sqlite3FkCheck(
272df 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
272e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
272e1 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
272e2 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
272e3 70 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20  pTab,           
272e4 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 20           /* Row 
272e5 69 73 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64  is being deleted
272e6 20 66 72 6f 6d 20 74 68 69 73 20 74 61 62 6c 65   from this table
272e7 20 2a 2f 20 0a 20 20 69 6e 74 20 72 65 67 4f 6c   */ .  int regOl
272e8 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
272e9 20 20 20 20 20 20 20 2f 2a 20 50 72 65 76 69 6f         /* Previo
272ea 75 73 20 72 6f 77 20 64 61 74 61 20 69 73 20 73  us row data is s
272eb 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20  tored here */.  
272ec 69 6e 74 20 72 65 67 4e 65 77 20 20 20 20 20 20  int regNew      
272ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
272ee 2f 2a 20 4e 65 77 20 72 6f 77 20 64 61 74 61 20  /* New row data 
272ef 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a  is stored here *
272f0 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
272f1 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
272f2 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
272f3 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 56  se handle */.  V
272f4 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
272f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
272f6 2a 20 56 4d 20 74 6f 20 77 72 69 74 65 20 63 6f  * VM to write co
272f7 64 65 20 74 6f 20 2a 2f 0a 20 20 46 4b 65 79 20  de to */.  FKey 
272f8 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20  *pFKey;         
272f9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
272fa 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  ed to iterate th
272fb 72 6f 75 67 68 20 46 4b 73 20 2a 2f 0a 20 20 69  rough FKs */.  i
272fc 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
272fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
272fe 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62  * Index of datab
272ff 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70  ase containing p
27300 54 61 62 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  Tab */.  const c
27301 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
27302 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
27303 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e   of database con
27304 74 61 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a  taining pTab */.
27305 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65 45 72    int isIgnoreEr
27306 72 6f 72 73 20 3d 20 70 50 61 72 73 65 2d 3e 64  rors = pParse->d
27307 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 3b 0a  isableTriggers;.
27308 0a 20 20 2f 2a 20 45 78 61 63 74 6c 79 20 6f 6e  .  /* Exactly on
27309 65 20 6f 66 20 72 65 67 4f 6c 64 20 61 6e 64 20  e of regOld and 
2730a 72 65 67 4e 65 77 20 73 68 6f 75 6c 64 20 62 65  regNew should be
2730b 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 2a 2f 0a 20 20   non-zero. */.  
2730c 61 73 73 65 72 74 28 20 28 72 65 67 4f 6c 64 3d  assert( (regOld=
2730d 3d 30 29 21 3d 28 72 65 67 4e 65 77 3d 3d 30 29  =0)!=(regNew==0)
2730e 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 66 6f 72   );..  /* If for
2730f 65 69 67 6e 2d 6b 65 79 73 20 61 72 65 20 64 69  eign-keys are di
27310 73 61 62 6c 65 64 2c 20 74 68 69 73 20 66 75 6e  sabled, this fun
27311 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
27312 2e 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e  . */.  if( (db->
27313 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72  flags&SQLITE_For
27314 65 69 67 6e 4b 65 79 73 29 3d 3d 30 20 29 20 72  eignKeys)==0 ) r
27315 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20 73 71  eturn;..  v = sq
27316 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
27317 72 73 65 29 3b 0a 20 20 69 44 62 20 3d 20 73 71  rse);.  iDb = sq
27318 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
27319 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  ex(db, pTab->pSc
2731a 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64  hema);.  zDb = d
2731b 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2731c 65 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74 68  e;..  /* Loop th
2731d 72 6f 75 67 68 20 61 6c 6c 20 74 68 65 20 66 6f  rough all the fo
2731e 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
2731f 61 69 6e 74 73 20 66 6f 72 20 77 68 69 63 68 20  aints for which 
27320 70 54 61 62 20 69 73 20 74 68 65 0a 20 20 2a 2a  pTab is the.  **
27321 20 63 68 69 6c 64 20 74 61 62 6c 65 20 28 74 68   child table (th
27322 65 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65  e table that the
27323 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66   foreign key def
27324 69 6e 69 74 69 6f 6e 20 69 73 20 70 61 72 74 20  inition is part 
27325 6f 66 29 2e 20 20 2a 2f 0a 20 20 66 6f 72 28 70  of).  */.  for(p
27326 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46 4b 65 79  FKey=pTab->pFKey
27327 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70  ; pFKey; pFKey=p
27328 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 29  FKey->pNextFrom)
27329 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 6f  {.    Table *pTo
2732a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2732b 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20 74 61      /* Parent ta
2732c 62 6c 65 20 6f 66 20 66 6f 72 65 69 67 6e 20 6b  ble of foreign k
2732d 65 79 20 70 46 4b 65 79 20 2a 2f 0a 20 20 20 20  ey pFKey */.    
2732e 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b  Index *pIdx = 0;
2732f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27330 20 49 6e 64 65 78 20 6f 6e 20 6b 65 79 20 63 6f   Index on key co
27331 6c 75 6d 6e 73 20 69 6e 20 70 54 6f 20 2a 2f 0a  lumns in pTo */.
27332 20 20 20 20 69 6e 74 20 2a 61 69 46 72 65 65 20      int *aiFree 
27333 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 2a 61 69  = 0;.    int *ai
27334 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20 69 43 6f  Col;.    int iCo
27335 6c 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  l;.    int i;.  
27336 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65 20 3d    int isIgnore =
27337 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64   0;..    /* Find
27338 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c   the parent tabl
27339 65 20 6f 66 20 74 68 69 73 20 66 6f 72 65 69 67  e of this foreig
2733a 6e 20 6b 65 79 2e 20 41 6c 73 6f 20 66 69 6e 64  n key. Also find
2733b 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65 78 20   a unique index 
2733c 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 70  .    ** on the p
2733d 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
2733e 73 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  s in the parent 
2733f 74 61 62 6c 65 2e 20 49 66 20 65 69 74 68 65 72  table. If either
27340 20 6f 66 20 74 68 65 73 65 20 0a 20 20 20 20 2a   of these .    *
27341 2a 20 73 63 68 65 6d 61 20 69 74 65 6d 73 20 63  * schema items c
27342 61 6e 6e 6f 74 20 62 65 20 6c 6f 63 61 74 65 64  annot be located
27343 2c 20 73 65 74 20 61 6e 20 65 72 72 6f 72 20 69  , set an error i
27344 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74  n pParse and ret
27345 75 72 6e 20 0a 20 20 20 20 2a 2a 20 65 61 72 6c  urn .    ** earl
27346 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  y.  */.    if( p
27347 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 54 72  Parse->disableTr
27348 69 67 67 65 72 73 20 29 7b 0a 20 20 20 20 20 20  iggers ){.      
27349 70 54 6f 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  pTo = sqlite3Fin
2734a 64 54 61 62 6c 65 28 64 62 2c 20 70 46 4b 65 79  dTable(db, pFKey
2734b 2d 3e 7a 54 6f 2c 20 7a 44 62 29 3b 0a 20 20 20  ->zTo, zDb);.   
2734c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54   }else{.      pT
2734d 6f 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  o = sqlite3Locat
2734e 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30  eTable(pParse, 0
2734f 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 7a 44  , pFKey->zTo, zD
27350 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  b);.    }.    if
27351 28 20 21 70 54 6f 20 7c 7c 20 6c 6f 63 61 74 65  ( !pTo || locate
27352 46 6b 65 79 49 6e 64 65 78 28 70 50 61 72 73 65  FkeyIndex(pParse
27353 2c 20 70 54 6f 2c 20 70 46 4b 65 79 2c 20 26 70  , pTo, pFKey, &p
27354 49 64 78 2c 20 26 61 69 46 72 65 65 29 20 29 7b  Idx, &aiFree) ){
27355 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 49 67  .      if( !isIg
27356 6e 6f 72 65 45 72 72 6f 72 73 20 7c 7c 20 64 62  noreErrors || db
27357 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
27358 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 63   return;.      c
27359 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20  ontinue;.    }. 
2735a 20 20 20 61 73 73 65 72 74 28 20 70 46 4b 65 79     assert( pFKey
2735b 2d 3e 6e 43 6f 6c 3d 3d 31 20 7c 7c 20 28 61 69  ->nCol==1 || (ai
2735c 46 72 65 65 20 26 26 20 70 49 64 78 29 20 29 3b  Free && pIdx) );
2735d 0a 0a 20 20 20 20 69 66 28 20 61 69 46 72 65 65  ..    if( aiFree
2735e 20 29 7b 0a 20 20 20 20 20 20 61 69 43 6f 6c 20   ){.      aiCol 
2735f 3d 20 61 69 46 72 65 65 3b 0a 20 20 20 20 7d 65  = aiFree;.    }e
27360 6c 73 65 7b 0a 20 20 20 20 20 20 69 43 6f 6c 20  lse{.      iCol 
27361 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d  = pFKey->aCol[0]
27362 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20 20 61 69  .iFrom;.      ai
27363 43 6f 6c 20 3d 20 26 69 43 6f 6c 3b 0a 20 20 20  Col = &iCol;.   
27364 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20   }.    for(i=0; 
27365 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69  i<pFKey->nCol; i
27366 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61  ++){.      if( a
27367 69 43 6f 6c 5b 69 5d 3d 3d 70 54 61 62 2d 3e 69  iCol[i]==pTab->i
27368 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  PKey ){.        
27369 61 69 43 6f 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20  aiCol[i] = -1;. 
2736a 20 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53       }.#ifndef S
2736b 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
2736c 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20 20 2f  RIZATION.      /
2736d 2a 20 52 65 71 75 65 73 74 20 70 65 72 6d 69 73  * Request permis
2736e 73 69 6f 6e 20 74 6f 20 72 65 61 64 20 74 68 65  sion to read the
2736f 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
27370 6d 6e 73 2e 20 49 66 20 74 68 65 20 0a 20 20 20  mns. If the .   
27371 20 20 20 2a 2a 20 61 75 74 68 6f 72 69 7a 61 74     ** authorizat
27372 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  ion callback ret
27373 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 47 4e 4f  urns SQLITE_IGNO
27374 52 45 2c 20 62 65 68 61 76 65 20 61 73 20 69 66  RE, behave as if
27375 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 76 61   any.      ** va
27376 6c 75 65 73 20 72 65 61 64 20 66 72 6f 6d 20 74  lues read from t
27377 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  he parent table 
27378 61 72 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20  are NULL. */.   
27379 20 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74 68     if( db->xAuth
2737a 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
2737b 72 63 61 75 74 68 3b 0a 20 20 20 20 20 20 20 20  rcauth;.        
2737c 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 54 6f  char *zCol = pTo
2737d 2d 3e 61 43 6f 6c 5b 70 49 64 78 20 3f 20 70 49  ->aCol[pIdx ? pI
2737e 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20  dx->aiColumn[i] 
2737f 3a 20 70 54 6f 2d 3e 69 50 4b 65 79 5d 2e 7a 4e  : pTo->iPKey].zN
27380 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 72 63 61  ame;.        rca
27381 75 74 68 20 3d 20 73 71 6c 69 74 65 33 41 75 74  uth = sqlite3Aut
27382 68 52 65 61 64 43 6f 6c 28 70 50 61 72 73 65 2c  hReadCol(pParse,
27383 20 70 54 6f 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f   pTo->zName, zCo
27384 6c 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20  l, iDb);.       
27385 20 69 73 49 67 6e 6f 72 65 20 3d 20 28 72 63 61   isIgnore = (rca
27386 75 74 68 3d 3d 53 51 4c 49 54 45 5f 49 47 4e 4f  uth==SQLITE_IGNO
27387 52 45 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  RE);.      }.#en
27388 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  dif.    }..    /
27389 2a 20 54 61 6b 65 20 61 20 73 68 61 72 65 64 2d  * Take a shared-
2738a 63 61 63 68 65 20 61 64 76 69 73 6f 72 79 20 72  cache advisory r
2738b 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead-lock on the 
2738c 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20 41 6c  parent table. Al
2738d 6c 6f 63 61 74 65 20 0a 20 20 20 20 2a 2a 20 61  locate .    ** a
2738e 20 63 75 72 73 6f 72 20 74 6f 20 75 73 65 20 74   cursor to use t
2738f 6f 20 73 65 61 72 63 68 20 74 68 65 20 75 6e 69  o search the uni
27390 71 75 65 20 69 6e 64 65 78 20 6f 6e 20 74 68 65  que index on the
27391 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75   parent key colu
27392 6d 6e 73 20 0a 20 20 20 20 2a 2a 20 69 6e 20 74  mns .    ** in t
27393 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e  he parent table.
27394 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
27395 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65  TableLock(pParse
27396 2c 20 69 44 62 2c 20 70 54 6f 2d 3e 74 6e 75 6d  , iDb, pTo->tnum
27397 2c 20 30 2c 20 70 54 6f 2d 3e 7a 4e 61 6d 65 29  , 0, pTo->zName)
27398 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  ;.    pParse->nT
27399 61 62 2b 2b 3b 0a 0a 20 20 20 20 69 66 28 20 72  ab++;..    if( r
2739a 65 67 4f 6c 64 21 3d 30 20 29 7b 0a 20 20 20 20  egOld!=0 ){.    
2739b 20 20 2f 2a 20 41 20 72 6f 77 20 69 73 20 62 65    /* A row is be
2739c 69 6e 67 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d  ing removed from
2739d 20 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65   the child table
2739e 2e 20 53 65 61 72 63 68 20 66 6f 72 20 74 68 65  . Search for the
2739f 20 70 61 72 65 6e 74 2e 0a 20 20 20 20 20 20 2a   parent..      *
273a0 2a 20 49 66 20 74 68 65 20 70 61 72 65 6e 74 20  * If the parent 
273a1 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20  does not exist, 
273a2 72 65 6d 6f 76 69 6e 67 20 74 68 65 20 63 68 69  removing the chi
273a3 6c 64 20 72 6f 77 20 72 65 73 6f 6c 76 65 73 20  ld row resolves 
273a4 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 6f 75 74  an .      ** out
273a5 73 74 61 6e 64 69 6e 67 20 66 6f 72 65 69 67 6e  standing foreign
273a6 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20   key constraint 
273a7 76 69 6f 6c 61 74 69 6f 6e 2e 20 2a 2f 0a 20 20  violation. */.  
273a8 20 20 20 20 66 6b 4c 6f 6f 6b 75 70 50 61 72 65      fkLookupPare
273a9 6e 74 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20  nt(pParse, iDb, 
273aa 70 54 6f 2c 20 70 49 64 78 2c 20 70 46 4b 65 79  pTo, pIdx, pFKey
273ab 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4f 6c 64 2c  , aiCol, regOld,
273ac 20 2d 31 2c 69 73 49 67 6e 6f 72 65 29 3b 0a 20   -1,isIgnore);. 
273ad 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 65 67     }.    if( reg
273ae 4e 65 77 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  New!=0 ){.      
273af 2f 2a 20 41 20 72 6f 77 20 69 73 20 62 65 69 6e  /* A row is bein
273b0 67 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 63  g added to the c
273b1 68 69 6c 64 20 74 61 62 6c 65 2e 20 49 66 20 61  hild table. If a
273b2 20 70 61 72 65 6e 74 20 72 6f 77 20 63 61 6e 6e   parent row cann
273b3 6f 74 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 66  ot.      ** be f
273b4 6f 75 6e 64 2c 20 61 64 64 69 6e 67 20 74 68 65  ound, adding the
273b5 20 63 68 69 6c 64 20 72 6f 77 20 68 61 73 20 76   child row has v
273b6 69 6f 6c 61 74 65 64 20 74 68 65 20 46 4b 20 63  iolated the FK c
273b7 6f 6e 73 74 72 61 69 6e 74 2e 20 2a 2f 20 0a 20  onstraint. */ . 
273b8 20 20 20 20 20 66 6b 4c 6f 6f 6b 75 70 50 61 72       fkLookupPar
273b9 65 6e 74 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ent(pParse, iDb,
273ba 20 70 54 6f 2c 20 70 49 64 78 2c 20 70 46 4b 65   pTo, pIdx, pFKe
273bb 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4e 65 77  y, aiCol, regNew
273bc 2c 20 2b 31 2c 69 73 49 67 6e 6f 72 65 29 3b 0a  , +1,isIgnore);.
273bd 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
273be 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 69 46  e3DbFree(db, aiF
273bf 72 65 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ree);.  }..  /* 
273c0 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 61 6c 6c  Loop through all
273c1 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
273c2 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 68 61   constraints tha
273c3 74 20 72 65 66 65 72 20 74 6f 20 74 68 69 73 20  t refer to this 
273c4 74 61 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 70  table */.  for(p
273c5 46 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 46 6b  FKey = sqlite3Fk
273c6 52 65 66 65 72 65 6e 63 65 73 28 70 54 61 62 29  References(pTab)
273c7 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d 70  ; pFKey; pFKey=p
273c8 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a  FKey->pNextTo){.
273c9 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20      Index *pIdx 
273ca 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
273cb 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79    /* Foreign key
273cc 20 69 6e 64 65 78 20 66 6f 72 20 70 46 4b 65 79   index for pFKey
273cd 20 2a 2f 0a 20 20 20 20 53 72 63 4c 69 73 74 20   */.    SrcList 
273ce 2a 70 53 72 63 3b 0a 20 20 20 20 69 6e 74 20 2a  *pSrc;.    int *
273cf 61 69 43 6f 6c 20 3d 20 30 3b 0a 0a 20 20 20 20  aiCol = 0;..    
273d0 69 66 28 20 21 70 46 4b 65 79 2d 3e 69 73 44 65  if( !pFKey->isDe
273d1 66 65 72 72 65 64 20 26 26 20 21 70 50 61 72 73  ferred && !pPars
273d2 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 26 26 20  e->pToplevel && 
273d3 21 70 50 61 72 73 65 2d 3e 69 73 4d 75 6c 74 69  !pParse->isMulti
273d4 57 72 69 74 65 20 29 7b 0a 20 20 20 20 20 20 61  Write ){.      a
273d5 73 73 65 72 74 28 20 72 65 67 4f 6c 64 3d 3d 30  ssert( regOld==0
273d6 20 26 26 20 72 65 67 4e 65 77 21 3d 30 20 29 3b   && regNew!=0 );
273d7 0a 20 20 20 20 20 20 2f 2a 20 49 6e 73 65 72 74  .      /* Insert
273d8 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  ing a single row
273d9 20 69 6e 74 6f 20 61 20 70 61 72 65 6e 74 20 74   into a parent t
273da 61 62 6c 65 20 63 61 6e 6e 6f 74 20 63 61 75 73  able cannot caus
273db 65 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 0a 20  e an immediate. 
273dc 20 20 20 20 20 2a 2a 20 66 6f 72 65 69 67 6e 20       ** foreign 
273dd 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e 2e 20 53  key violation. S
273de 6f 20 64 6f 20 6e 6f 74 68 69 6e 67 20 69 6e 20  o do nothing in 
273df 74 68 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20  this case.  */. 
273e0 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
273e1 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 6c 6f     }..    if( lo
273e2 63 61 74 65 46 6b 65 79 49 6e 64 65 78 28 70 50  cateFkeyIndex(pP
273e3 61 72 73 65 2c 20 70 54 61 62 2c 20 70 46 4b 65  arse, pTab, pFKe
273e4 79 2c 20 26 70 49 64 78 2c 20 26 61 69 43 6f 6c  y, &pIdx, &aiCol
273e5 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  ) ){.      if( !
273e6 69 73 49 67 6e 6f 72 65 45 72 72 6f 72 73 20 7c  isIgnoreErrors |
273e7 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
273e8 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  ed ) return;.   
273e9 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
273ea 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 61   }.    assert( a
273eb 69 43 6f 6c 20 7c 7c 20 70 46 4b 65 79 2d 3e 6e  iCol || pFKey->n
273ec 43 6f 6c 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 2f  Col==1 );..    /
273ed 2a 20 43 72 65 61 74 65 20 61 20 53 72 63 4c 69  * Create a SrcLi
273ee 73 74 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  st structure con
273ef 74 61 69 6e 69 6e 67 20 61 20 73 69 6e 67 6c 65  taining a single
273f0 20 74 61 62 6c 65 20 28 74 68 65 20 74 61 62 6c   table (the tabl
273f1 65 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 66 6f  e .    ** the fo
273f2 72 65 69 67 6e 20 6b 65 79 20 74 68 61 74 20 72  reign key that r
273f3 65 66 65 72 73 20 74 6f 20 74 68 69 73 20 74 61  efers to this ta
273f4 62 6c 65 20 69 73 20 61 74 74 61 63 68 65 64 20  ble is attached 
273f5 74 6f 29 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a  to). This.    **
273f6 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72   is required for
273f7 20 74 68 65 20 73 71 6c 69 74 65 33 57 68 65 72   the sqlite3Wher
273f8 65 58 58 58 28 29 20 69 6e 74 65 72 66 61 63 65  eXXX() interface
273f9 2e 20 20 2a 2f 0a 20 20 20 20 70 53 72 63 20 3d  .  */.    pSrc =
273fa 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
273fb 70 70 65 6e 64 28 64 62 2c 20 30 2c 20 30 2c 20  ppend(db, 0, 0, 
273fc 30 29 3b 0a 20 20 20 20 69 66 28 20 70 53 72 63  0);.    if( pSrc
273fd 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74   ){.      struct
273fe 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
273ff 49 74 65 6d 20 3d 20 70 53 72 63 2d 3e 61 3b 0a  Item = pSrc->a;.
27400 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 54 61        pItem->pTa
27401 62 20 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d  b = pFKey->pFrom
27402 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a  ;.      pItem->z
27403 4e 61 6d 65 20 3d 20 70 46 4b 65 79 2d 3e 70 46  Name = pFKey->pF
27404 72 6f 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  rom->zName;.    
27405 20 20 70 49 74 65 6d 2d 3e 70 54 61 62 2d 3e 6e    pItem->pTab->n
27406 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20 70 49 74  Ref++;.      pIt
27407 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20 70 50  em->iCursor = pP
27408 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
27409 0a 20 20 20 20 20 20 69 66 28 20 72 65 67 4e 65  .      if( regNe
2740a 77 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  w!=0 ){.        
2740b 66 6b 53 63 61 6e 43 68 69 6c 64 72 65 6e 28 70  fkScanChildren(p
2740c 50 61 72 73 65 2c 20 70 53 72 63 2c 20 70 54 61  Parse, pSrc, pTa
2740d 62 2c 20 70 49 64 78 2c 20 70 46 4b 65 79 2c 20  b, pIdx, pFKey, 
2740e 61 69 43 6f 6c 2c 20 72 65 67 4e 65 77 2c 20 2d  aiCol, regNew, -
2740f 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1);.      }.    
27410 20 20 69 66 28 20 72 65 67 4f 6c 64 21 3d 30 20    if( regOld!=0 
27411 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
27412 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53 54   there is a REST
27413 52 49 43 54 20 61 63 74 69 6f 6e 20 63 6f 6e 66  RICT action conf
27414 69 67 75 72 65 64 20 66 6f 72 20 74 68 65 20 63  igured for the c
27415 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69 6f 6e  urrent operation
27416 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74  .        ** on t
27417 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20  he parent table 
27418 6f 66 20 74 68 69 73 20 46 4b 2c 20 74 68 65 6e  of this FK, then
27419 20 74 68 72 6f 77 20 61 6e 20 65 78 63 65 70 74   throw an except
2741a 69 6f 6e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ion .        ** 
2741b 69 6d 6d 65 64 69 61 74 65 6c 79 20 69 66 20 74  immediately if t
2741c 68 65 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74  he FK constraint
2741d 20 69 73 20 76 69 6f 6c 61 74 65 64 2c 20 65 76   is violated, ev
2741e 65 6e 20 69 66 20 74 68 69 73 20 69 73 20 61 0a  en if this is a.
2741f 20 20 20 20 20 20 20 20 2a 2a 20 64 65 66 65 72          ** defer
27420 72 65 64 20 74 72 69 67 67 65 72 2e 20 54 68 61  red trigger. Tha
27421 74 27 73 20 77 68 61 74 20 52 45 53 54 52 49 43  t's what RESTRIC
27422 54 20 6d 65 61 6e 73 2e 20 54 6f 20 64 65 66 65  T means. To defe
27423 72 20 63 68 65 63 6b 69 6e 67 0a 20 20 20 20 20  r checking.     
27424 20 20 20 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72     ** the constr
27425 61 69 6e 74 2c 20 74 68 65 20 46 4b 20 73 68 6f  aint, the FK sho
27426 75 6c 64 20 73 70 65 63 69 66 79 20 4e 4f 20 41  uld specify NO A
27427 43 54 49 4f 4e 20 28 72 65 70 72 65 73 65 6e 74  CTION (represent
27428 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 75 73  ed.        ** us
27429 69 6e 67 20 4f 45 5f 4e 6f 6e 65 29 2e 20 4e 4f  ing OE_None). NO
2742a 20 41 43 54 49 4f 4e 20 69 73 20 74 68 65 20 64   ACTION is the d
2742b 65 66 61 75 6c 74 2e 20 20 2a 2f 0a 20 20 20 20  efault.  */.    
2742c 20 20 20 20 66 6b 53 63 61 6e 43 68 69 6c 64 72      fkScanChildr
2742d 65 6e 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c  en(pParse, pSrc,
2742e 20 70 54 61 62 2c 20 70 49 64 78 2c 20 70 46 4b   pTab, pIdx, pFK
2742f 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4f 6c  ey, aiCol, regOl
27430 64 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  d, 1);.      }. 
27431 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d       pItem->zNam
27432 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c  e = 0;.      sql
27433 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
27434 65 28 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 20  e(db, pSrc);.   
27435 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
27436 46 72 65 65 28 64 62 2c 20 61 69 43 6f 6c 29 3b  Free(db, aiCol);
27437 0a 20 20 7d 0a 7d 0a 0a 23 64 65 66 69 6e 65 20  .  }.}..#define 
27438 43 4f 4c 55 4d 4e 5f 4d 41 53 4b 28 78 29 20 28  COLUMN_MASK(x) (
27439 28 28 78 29 3e 33 31 29 20 3f 20 30 78 66 66 66  ((x)>31) ? 0xfff
2743a 66 66 66 66 66 20 3a 20 28 28 75 33 32 29 31 3c  fffff : ((u32)1<
2743b 3c 28 78 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  <(x)))../*.** Th
2743c 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
2743d 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 67 65 6e  alled before gen
2743e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 74 6f 20  erating code to 
2743f 75 70 64 61 74 65 20 6f 72 20 64 65 6c 65 74 65  update or delete
27440 20 61 20 0a 2a 2a 20 72 6f 77 20 63 6f 6e 74 61   a .** row conta
27441 69 6e 65 64 20 69 6e 20 74 61 62 6c 65 20 70 54  ined in table pT
27442 61 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ab..*/.SQLITE_PR
27443 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
27444 33 46 6b 4f 6c 64 6d 61 73 6b 28 0a 20 20 50 61  3FkOldmask(.  Pa
27445 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
27446 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27447 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
27448 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  /.  Table *pTab 
27449 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2744a 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69      /* Table bei
2744b 6e 67 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 29  ng modified */.)
2744c 7b 0a 20 20 75 33 32 20 6d 61 73 6b 20 3d 20 30  {.  u32 mask = 0
2744d 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
2744e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
2744f 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b 0a  _ForeignKeys ){.
27450 20 20 20 20 46 4b 65 79 20 2a 70 3b 0a 20 20 20      FKey *p;.   
27451 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
27452 70 3d 70 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70  p=pTab->pFKey; p
27453 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 46 72 6f 6d  ; p=p->pNextFrom
27454 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  ){.      for(i=0
27455 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  ; i<p->nCol; i++
27456 29 20 6d 61 73 6b 20 7c 3d 20 43 4f 4c 55 4d 4e  ) mask |= COLUMN
27457 5f 4d 41 53 4b 28 70 2d 3e 61 43 6f 6c 5b 69 5d  _MASK(p->aCol[i]
27458 2e 69 46 72 6f 6d 29 3b 0a 20 20 20 20 7d 0a 20  .iFrom);.    }. 
27459 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 33     for(p=sqlite3
2745a 46 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54 61  FkReferences(pTa
2745b 62 29 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  b); p; p=p->pNex
2745c 74 54 6f 29 7b 0a 20 20 20 20 20 20 49 6e 64 65  tTo){.      Inde
2745d 78 20 2a 70 49 64 78 20 3d 20 30 3b 0a 20 20 20  x *pIdx = 0;.   
2745e 20 20 20 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64     locateFkeyInd
2745f 65 78 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ex(pParse, pTab,
27460 20 70 2c 20 26 70 49 64 78 2c 20 30 29 3b 0a 20   p, &pIdx, 0);. 
27461 20 20 20 20 20 69 66 28 20 70 49 64 78 20 29 7b       if( pIdx ){
27462 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
27463 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; i<pIdx->nColum
27464 6e 3b 20 69 2b 2b 29 20 6d 61 73 6b 20 7c 3d 20  n; i++) mask |= 
27465 43 4f 4c 55 4d 4e 5f 4d 41 53 4b 28 70 49 64 78  COLUMN_MASK(pIdx
27466 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a  ->aiColumn[i]);.
27467 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
27468 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b  }.  return mask;
27469 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
2746a 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
2746b 64 20 62 65 66 6f 72 65 20 67 65 6e 65 72 61 74  d before generat
2746c 69 6e 67 20 63 6f 64 65 20 74 6f 20 75 70 64 61  ing code to upda
2746d 74 65 20 6f 72 20 64 65 6c 65 74 65 20 61 20 0a  te or delete a .
2746e 2a 2a 20 72 6f 77 20 63 6f 6e 74 61 69 6e 65 64  ** row contained
2746f 20 69 6e 20 74 61 62 6c 65 20 70 54 61 62 2e 20   in table pTab. 
27470 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  If the operation
27471 20 69 73 20 61 20 44 45 4c 45 54 45 2c 20 74 68   is a DELETE, th
27472 65 6e 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  en.** parameter 
27473 61 43 68 61 6e 67 65 20 69 73 20 70 61 73 73 65  aChange is passe
27474 64 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 2e 20  d a NULL value. 
27475 46 6f 72 20 61 6e 20 55 50 44 41 54 45 2c 20 61  For an UPDATE, a
27476 43 68 61 6e 67 65 20 70 6f 69 6e 74 73 0a 2a 2a  Change points.**
27477 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20   to an array of 
27478 73 69 7a 65 20 4e 2c 20 77 68 65 72 65 20 4e 20  size N, where N 
27479 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2747a 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 61 62 6c   columns in tabl
2747b 65 20 70 54 61 62 2e 0a 2a 2a 20 49 66 20 74 68  e pTab..** If th
2747c 65 20 69 27 74 68 20 63 6f 6c 75 6d 6e 20 69 73  e i'th column is
2747d 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20 62 79   not modified by
2747e 20 74 68 65 20 55 50 44 41 54 45 2c 20 74 68 65   the UPDATE, the
2747f 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
27480 69 6e 67 20 0a 2a 2a 20 65 6e 74 72 79 20 69 6e  ing .** entry in
27481 20 74 68 65 20 61 43 68 61 6e 67 65 5b 5d 20 61   the aChange[] a
27482 72 72 61 79 20 69 73 20 73 65 74 20 74 6f 20 2d  rray is set to -
27483 31 2e 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e  1. If the column
27484 20 69 73 20 6d 6f 64 69 66 69 65 64 2c 0a 2a 2a   is modified,.**
27485 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 30 20   the value is 0 
27486 6f 72 20 67 72 65 61 74 65 72 2e 20 50 61 72 61  or greater. Para
27487 6d 65 74 65 72 20 63 68 6e 67 52 6f 77 69 64 20  meter chngRowid 
27488 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69  is set to true i
27489 66 20 74 68 65 0a 2a 2a 20 55 50 44 41 54 45 20  f the.** UPDATE 
2748a 73 74 61 74 65 6d 65 6e 74 20 6d 6f 64 69 66 69  statement modifi
2748b 65 73 20 74 68 65 20 72 6f 77 69 64 20 66 69 65  es the rowid fie
2748c 6c 64 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65  lds of the table
2748d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 66  ..**.** If any f
2748e 6f 72 65 69 67 6e 20 6b 65 79 20 70 72 6f 63 65  oreign key proce
2748f 73 73 69 6e 67 20 77 69 6c 6c 20 62 65 20 72 65  ssing will be re
27490 71 75 69 72 65 64 2c 20 74 68 69 73 20 66 75 6e  quired, this fun
27491 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 0a 2a 2a  ction returns.**
27492 20 74 72 75 65 2e 20 49 66 20 74 68 65 72 65 20   true. If there 
27493 69 73 20 6e 6f 20 66 6f 72 65 69 67 6e 20 6b 65  is no foreign ke
27494 79 20 72 65 6c 61 74 65 64 20 70 72 6f 63 65 73  y related proces
27495 73 69 6e 67 2c 20 74 68 69 73 20 66 75 6e 63 74  sing, this funct
27496 69 6f 6e 20 0a 2a 2a 20 72 65 74 75 72 6e 73 20  ion .** returns 
27497 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  false..*/.SQLITE
27498 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
27499 69 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 0a  ite3FkRequired(.
2749a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2749b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2749c 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65    /* Parse conte
2749d 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  xt */.  Table *p
2749e 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  Tab,            
2749f 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
274a0 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20   being modified 
274a1 2a 2f 0a 20 20 69 6e 74 20 2a 61 43 68 61 6e 67  */.  int *aChang
274a2 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
274a3 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 4e 55 4c 4c       /* Non-NULL
274a4 20 66 6f 72 20 55 50 44 41 54 45 20 6f 70 65 72   for UPDATE oper
274a5 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ations */.  int 
274a6 63 68 6e 67 52 6f 77 69 64 20 20 20 20 20 20 20  chngRowid       
274a7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
274a8 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 20 74  rue for UPDATE t
274a9 68 61 74 20 61 66 66 65 63 74 73 20 72 6f 77 69  hat affects rowi
274aa 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 50  d */.){.  if( pP
274ab 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26  arse->db->flags&
274ac 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
274ad 79 73 20 29 7b 0a 20 20 20 20 69 66 28 20 21 61  ys ){.    if( !a
274ae 43 68 61 6e 67 65 20 29 7b 0a 20 20 20 20 20 20  Change ){.      
274af 2f 2a 20 41 20 44 45 4c 45 54 45 20 6f 70 65 72  /* A DELETE oper
274b0 61 74 69 6f 6e 2e 20 46 6f 72 65 69 67 6e 20 6b  ation. Foreign k
274b1 65 79 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73  ey processing is
274b2 20 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65   required if the
274b3 20 0a 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65   .      ** table
274b4 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 69 73 20   in question is 
274b5 65 69 74 68 65 72 20 74 68 65 20 63 68 69 6c 64  either the child
274b6 20 6f 72 20 70 61 72 65 6e 74 20 74 61 62 6c 65   or parent table
274b7 20 66 6f 72 20 61 6e 79 20 0a 20 20 20 20 20 20   for any .      
274b8 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  ** foreign key c
274b9 6f 6e 73 74 72 61 69 6e 74 2e 20 20 2a 2f 0a 20  onstraint.  */. 
274ba 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c       return (sql
274bb 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
274bc 28 70 54 61 62 29 20 7c 7c 20 70 54 61 62 2d 3e  (pTab) || pTab->
274bd 70 46 4b 65 79 29 3b 0a 20 20 20 20 7d 65 6c 73  pFKey);.    }els
274be 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73  e{.      /* This
274bf 20 69 73 20 61 6e 20 55 50 44 41 54 45 2e 20 46   is an UPDATE. F
274c0 6f 72 65 69 67 6e 20 6b 65 79 20 70 72 6f 63 65  oreign key proce
274c1 73 73 69 6e 67 20 69 73 20 6f 6e 6c 79 20 72 65  ssing is only re
274c2 71 75 69 72 65 64 20 69 66 20 74 68 65 0a 20 20  quired if the.  
274c3 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e      ** operation
274c4 20 6d 6f 64 69 66 69 65 73 20 6f 6e 65 20 6f 72   modifies one or
274c5 20 6d 6f 72 65 20 63 68 69 6c 64 20 6f 72 20 70   more child or p
274c6 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
274c7 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  s. */.      int 
274c8 69 3b 0a 20 20 20 20 20 20 46 4b 65 79 20 2a 70  i;.      FKey *p
274c9 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63  ;..      /* Chec
274ca 6b 20 69 66 20 61 6e 79 20 63 68 69 6c 64 20 6b  k if any child k
274cb 65 79 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 62  ey columns are b
274cc 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20 2a  eing modified. *
274cd 2f 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 70 54  /.      for(p=pT
274ce 61 62 2d 3e 70 46 4b 65 79 3b 20 70 3b 20 70 3d  ab->pFKey; p; p=
274cf 70 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20  p->pNextFrom){. 
274d0 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
274d1 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  i<p->nCol; i++){
274d2 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
274d3 43 68 69 6c 64 4b 65 79 20 3d 20 70 2d 3e 61 43  ChildKey = p->aC
274d4 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20  ol[i].iFrom;.   
274d5 20 20 20 20 20 20 20 69 66 28 20 61 43 68 61 6e         if( aChan
274d6 67 65 5b 69 43 68 69 6c 64 4b 65 79 5d 3e 3d 30  ge[iChildKey]>=0
274d7 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
274d8 20 20 20 20 20 20 20 69 66 28 20 69 43 68 69 6c         if( iChil
274d9 64 4b 65 79 3d 3d 70 54 61 62 2d 3e 69 50 4b 65  dKey==pTab->iPKe
274da 79 20 26 26 20 63 68 6e 67 52 6f 77 69 64 20 29  y && chngRowid )
274db 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
274dc 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20     }.      }..  
274dd 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20      /* Check if 
274de 61 6e 79 20 70 61 72 65 6e 74 20 6b 65 79 20 63  any parent key c
274df 6f 6c 75 6d 6e 73 20 61 72 65 20 62 65 69 6e 67  olumns are being
274e0 20 6d 6f 64 69 66 69 65 64 2e 20 2a 2f 0a 20 20   modified. */.  
274e1 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69 74 65      for(p=sqlite
274e2 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54  3FkReferences(pT
274e3 61 62 29 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  ab); p; p=p->pNe
274e4 78 74 54 6f 29 7b 0a 20 20 20 20 20 20 20 20 66  xtTo){.        f
274e5 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f  or(i=0; i<p->nCo
274e6 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; i++){.       
274e7 20 20 20 63 68 61 72 20 2a 7a 4b 65 79 20 3d 20     char *zKey = 
274e8 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 3b  p->aCol[i].zCol;
274e9 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69  .          int i
274ea 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20 20 66  Key;.          f
274eb 6f 72 28 69 4b 65 79 3d 30 3b 20 69 4b 65 79 3c  or(iKey=0; iKey<
274ec 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 4b 65 79  pTab->nCol; iKey
274ed 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
274ee 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20   Column *pCol = 
274ef 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 4b 65 79  &pTab->aCol[iKey
274f0 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ];.            i
274f1 66 28 20 28 7a 4b 65 79 20 3f 20 21 73 71 6c 69  f( (zKey ? !sqli
274f2 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d  te3StrICmp(pCol-
274f3 3e 7a 4e 61 6d 65 2c 20 7a 4b 65 79 29 20 3a 20  >zName, zKey) : 
274f4 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 29  pCol->isPrimKey)
274f5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
274f6 20 20 69 66 28 20 61 43 68 61 6e 67 65 5b 69 4b    if( aChange[iK
274f7 65 79 5d 3e 3d 30 20 29 20 72 65 74 75 72 6e 20  ey]>=0 ) return 
274f8 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1;.             
274f9 20 69 66 28 20 69 4b 65 79 3d 3d 70 54 61 62 2d   if( iKey==pTab-
274fa 3e 69 50 4b 65 79 20 26 26 20 63 68 6e 67 52 6f  >iPKey && chngRo
274fb 77 69 64 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  wid ) return 1;.
274fc 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
274fd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
274fe 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
274ff 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
27500 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
27501 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
27502 65 64 20 77 68 65 6e 20 61 6e 20 55 50 44 41 54  ed when an UPDAT
27503 45 20 6f 72 20 44 45 4c 45 54 45 20 6f 70 65 72  E or DELETE oper
27504 61 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 0a  ation is being .
27505 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 74  ** compiled on t
27506 61 62 6c 65 20 70 54 61 62 2c 20 77 68 69 63 68  able pTab, which
27507 20 69 73 20 74 68 65 20 70 61 72 65 6e 74 20 74   is the parent t
27508 61 62 6c 65 20 6f 66 20 66 6f 72 65 69 67 6e 2d  able of foreign-
27509 6b 65 79 20 70 46 4b 65 79 2e 0a 2a 2a 20 49 66  key pFKey..** If
2750a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70 65   the current ope
2750b 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 55 50 44  ration is an UPD
2750c 41 54 45 2c 20 74 68 65 6e 20 74 68 65 20 70 43  ATE, then the pC
2750d 68 61 6e 67 65 73 20 70 61 72 61 6d 65 74 65 72  hanges parameter
2750e 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 61 20   is.** passed a 
2750f 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6c  pointer to the l
27510 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 62  ist of columns b
27511 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20 49  eing modified. I
27512 66 20 69 74 20 69 73 20 61 0a 2a 2a 20 44 45 4c  f it is a.** DEL
27513 45 54 45 2c 20 70 43 68 61 6e 67 65 73 20 69 73  ETE, pChanges is
27514 20 70 61 73 73 65 64 20 61 20 4e 55 4c 4c 20 70   passed a NULL p
27515 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 74  ointer..**.** It
27516 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
27517 65 72 20 74 6f 20 61 20 54 72 69 67 67 65 72 20  er to a Trigger 
27518 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
27519 6e 69 6e 67 20 61 20 74 72 69 67 67 65 72 0a 2a  ning a trigger.*
2751a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20  * equivalent to 
2751b 74 68 65 20 4f 4e 20 55 50 44 41 54 45 20 6f 72  the ON UPDATE or
2751c 20 4f 4e 20 44 45 4c 45 54 45 20 61 63 74 69 6f   ON DELETE actio
2751d 6e 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70  n specified by p
2751e 46 4b 65 79 2e 0a 2a 2a 20 49 66 20 74 68 65 20  FKey..** If the 
2751f 61 63 74 69 6f 6e 20 69 73 20 22 4e 4f 20 41 43  action is "NO AC
27520 54 49 4f 4e 22 20 6f 72 20 22 52 45 53 54 52 49  TION" or "RESTRI
27521 43 54 22 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c  CT", then a NULL
27522 20 70 6f 69 6e 74 65 72 20 69 73 0a 2a 2a 20 72   pointer is.** r
27523 65 74 75 72 6e 65 64 20 28 74 68 65 73 65 20 61  eturned (these a
27524 63 74 69 6f 6e 73 20 72 65 71 75 69 72 65 20 6e  ctions require n
27525 6f 20 73 70 65 63 69 61 6c 20 68 61 6e 64 6c 69  o special handli
27526 6e 67 20 62 79 20 74 68 65 20 74 72 69 67 67 65  ng by the trigge
27527 72 73 0a 2a 2a 20 73 75 62 2d 73 79 73 74 65 6d  rs.** sub-system
27528 2c 20 63 6f 64 65 20 66 6f 72 20 74 68 65 6d 20  , code for them 
27529 69 73 20 63 72 65 61 74 65 64 20 62 79 20 66 6b  is created by fk
2752a 53 63 61 6e 43 68 69 6c 64 72 65 6e 28 29 29 2e  ScanChildren()).
2752b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  .**.** For examp
2752c 6c 65 2c 20 69 66 20 70 46 4b 65 79 20 69 73 20  le, if pFKey is 
2752d 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  the foreign key 
2752e 61 6e 64 20 70 54 61 62 20 69 73 20 74 61 62 6c  and pTab is tabl
2752f 65 20 22 70 22 20 69 6e 20 0a 2a 2a 20 74 68 65  e "p" in .** the
27530 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63 68 65 6d   following schem
27531 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 43 52 45 41 54  a:.**.**   CREAT
27532 45 20 54 41 42 4c 45 20 70 28 70 6b 20 50 52 49  E TABLE p(pk PRI
27533 4d 41 52 59 20 4b 45 59 29 3b 0a 2a 2a 20 20 20  MARY KEY);.**   
27534 43 52 45 41 54 45 20 54 41 42 4c 45 20 63 28 63  CREATE TABLE c(c
27535 6b 20 52 45 46 45 52 45 4e 43 45 53 20 70 20 4f  k REFERENCES p O
27536 4e 20 44 45 4c 45 54 45 20 43 41 53 43 41 44 45  N DELETE CASCADE
27537 29 3b 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68  );.**.** then th
27538 65 20 72 65 74 75 72 6e 65 64 20 74 72 69 67 67  e returned trigg
27539 65 72 20 73 74 72 75 63 74 75 72 65 20 69 73 20  er structure is 
2753a 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a  equivalent to:.*
2753b 2a 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 52  *.**   CREATE TR
2753c 49 47 47 45 52 20 2e 2e 2e 20 44 45 4c 45 54 45  IGGER ... DELETE
2753d 20 4f 4e 20 70 20 42 45 47 49 4e 0a 2a 2a 20 20   ON p BEGIN.**  
2753e 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 63     DELETE FROM c
2753f 20 57 48 45 52 45 20 63 6b 20 3d 20 6f 6c 64 2e   WHERE ck = old.
27540 70 6b 3b 0a 2a 2a 20 20 20 45 4e 44 3b 0a 2a 2a  pk;.**   END;.**
27541 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
27542 20 70 6f 69 6e 74 65 72 20 69 73 20 63 61 63 68   pointer is cach
27543 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
27544 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 62  e foreign key ob
27545 6a 65 63 74 2e 20 49 74 0a 2a 2a 20 69 73 20 65  ject. It.** is e
27546 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64 20  ventually freed 
27547 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72  along with the r
27548 65 73 74 20 6f 66 20 74 68 65 20 66 6f 72 65 69  est of the forei
27549 67 6e 20 6b 65 79 20 6f 62 6a 65 63 74 20 62 79  gn key object by
2754a 20 0a 2a 2a 20 73 71 6c 69 74 65 33 46 6b 44 65   .** sqlite3FkDe
2754b 6c 65 74 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  lete()..*/.stati
2754c 63 20 54 72 69 67 67 65 72 20 2a 66 6b 41 63 74  c Trigger *fkAct
2754d 69 6f 6e 54 72 69 67 67 65 72 28 0a 20 20 50 61  ionTrigger(.  Pa
2754e 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2754f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27550 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
27551 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
27552 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27553 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69      /* Table bei
27554 6e 67 20 75 70 64 61 74 65 64 20 6f 72 20 64 65  ng updated or de
27555 6c 65 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20  leted from */.  
27556 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20 20 20 20  FKey *pFKey,    
27557 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27558 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 74  /* Foreign key t
27559 6f 20 67 65 74 20 61 63 74 69 6f 6e 20 66 6f 72  o get action for
2755a 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2755b 70 43 68 61 6e 67 65 73 20 20 20 20 20 20 20 20  pChanges        
2755c 20 20 20 20 20 20 2f 2a 20 43 68 61 6e 67 65 2d        /* Change-
2755d 6c 69 73 74 20 66 6f 72 20 55 50 44 41 54 45 2c  list for UPDATE,
2755e 20 4e 55 4c 4c 20 66 6f 72 20 44 45 4c 45 54 45   NULL for DELETE
2755f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
27560 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
27561 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61  b;       /* Data
27562 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  base handle */. 
27563 20 69 6e 74 20 61 63 74 69 6f 6e 3b 20 20 20 20   int action;    
27564 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27565 20 2f 2a 20 4f 6e 65 20 6f 66 20 4f 45 5f 4e 6f   /* One of OE_No
27566 6e 65 2c 20 4f 45 5f 43 61 73 63 61 64 65 20 65  ne, OE_Cascade e
27567 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  tc. */.  Trigger
27568 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20   *pTrigger;     
27569 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 69 67           /* Trig
2756a 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e 20 74  ger definition t
2756b 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e  o return */.  in
2756c 74 20 69 41 63 74 69 6f 6e 20 3d 20 28 70 43 68  t iAction = (pCh
2756d 61 6e 67 65 73 21 3d 30 29 3b 20 20 20 20 2f 2a  anges!=0);    /*
2756e 20 31 20 66 6f 72 20 55 50 44 41 54 45 2c 20 30   1 for UPDATE, 0
2756f 20 66 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a 0a   for DELETE */..
27570 20 20 61 63 74 69 6f 6e 20 3d 20 70 46 4b 65 79    action = pFKey
27571 2d 3e 61 41 63 74 69 6f 6e 5b 69 41 63 74 69 6f  ->aAction[iActio
27572 6e 5d 3b 0a 20 20 70 54 72 69 67 67 65 72 20 3d  n];.  pTrigger =
27573 20 70 46 4b 65 79 2d 3e 61 70 54 72 69 67 67 65   pFKey->apTrigge
27574 72 5b 69 41 63 74 69 6f 6e 5d 3b 0a 0a 20 20 69  r[iAction];..  i
27575 66 28 20 61 63 74 69 6f 6e 21 3d 4f 45 5f 4e 6f  f( action!=OE_No
27576 6e 65 20 26 26 20 21 70 54 72 69 67 67 65 72 20  ne && !pTrigger 
27577 29 7b 0a 20 20 20 20 75 38 20 65 6e 61 62 6c 65  ){.    u8 enable
27578 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20  Lookaside;      
27579 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20       /* Copy of 
2757a 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
2757b 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 20 20 63 68  nabled */.    ch
2757c 61 72 20 63 6f 6e 73 74 20 2a 7a 46 72 6f 6d 3b  ar const *zFrom;
2757d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2757e 61 6d 65 20 6f 66 20 63 68 69 6c 64 20 74 61 62  ame of child tab
2757f 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 46  le */.    int nF
27580 72 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  rom;            
27581 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
27582 68 20 69 6e 20 62 79 74 65 73 20 6f 66 20 7a 46  h in bytes of zF
27583 72 6f 6d 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78  rom */.    Index
27584 20 2a 70 49 64 78 20 3d 20 30 3b 20 20 20 20 20   *pIdx = 0;     
27585 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65           /* Pare
27586 6e 74 20 6b 65 79 20 69 6e 64 65 78 20 66 6f 72  nt key index for
27587 20 74 68 69 73 20 46 4b 20 2a 2f 0a 20 20 20 20   this FK */.    
27588 69 6e 74 20 2a 61 69 43 6f 6c 20 3d 20 30 3b 20  int *aiCol = 0; 
27589 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2758a 20 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c   child table col
2758b 73 20 2d 3e 20 70 61 72 65 6e 74 20 6b 65 79 20  s -> parent key 
2758c 63 6f 6c 73 20 2a 2f 0a 20 20 20 20 54 72 69 67  cols */.    Trig
2758d 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 3d  gerStep *pStep =
2758e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 69   0;        /* Fi
2758f 72 73 74 20 28 6f 6e 6c 79 29 20 73 74 65 70 20  rst (only) step 
27590 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  of trigger progr
27591 61 6d 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a  am */.    Expr *
27592 70 57 68 65 72 65 20 3d 20 30 3b 20 20 20 20 20  pWhere = 0;     
27593 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45          /* WHERE
27594 20 63 6c 61 75 73 65 20 6f 66 20 74 72 69 67 67   clause of trigg
27595 65 72 20 73 74 65 70 20 2a 2f 0a 20 20 20 20 45  er step */.    E
27596 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
27597 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
27598 43 68 61 6e 67 65 73 20 6c 69 73 74 20 69 66 20  Changes list if 
27599 4f 4e 20 55 50 44 41 54 45 20 43 41 53 43 41 44  ON UPDATE CASCAD
2759a 45 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20  E */.    Select 
2759b 2a 70 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20  *pSelect = 0;   
2759c 20 20 20 20 20 20 20 2f 2a 20 49 66 20 52 45 53         /* If RES
2759d 54 52 49 43 54 2c 20 22 53 45 4c 45 43 54 20 52  TRICT, "SELECT R
2759e 41 49 53 45 28 2e 2e 2e 29 22 20 2a 2f 0a 20 20  AISE(...)" */.  
2759f 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
275a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275a1 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
275a2 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70 72  able */.    Expr
275a3 20 2a 70 57 68 65 6e 20 3d 20 30 3b 20 20 20 20   *pWhen = 0;    
275a4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45            /* WHE
275a5 4e 20 63 6c 61 75 73 65 20 66 6f 72 20 74 68 65  N clause for the
275a6 20 74 72 69 67 67 65 72 20 2a 2f 0a 0a 20 20 20   trigger */..   
275a7 20 69 66 28 20 6c 6f 63 61 74 65 46 6b 65 79 49   if( locateFkeyI
275a8 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 54 61  ndex(pParse, pTa
275a9 62 2c 20 70 46 4b 65 79 2c 20 26 70 49 64 78 2c  b, pFKey, &pIdx,
275aa 20 26 61 69 43 6f 6c 29 20 29 20 72 65 74 75 72   &aiCol) ) retur
275ab 6e 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28  n 0;.    assert(
275ac 20 61 69 43 6f 6c 20 7c 7c 20 70 46 4b 65 79 2d   aiCol || pFKey-
275ad 3e 6e 43 6f 6c 3d 3d 31 20 29 3b 0a 0a 20 20 20  >nCol==1 );..   
275ae 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 46 4b 65   for(i=0; i<pFKe
275af 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  y->nCol; i++){. 
275b0 20 20 20 20 20 54 6f 6b 65 6e 20 74 4f 6c 64 20       Token tOld 
275b1 3d 20 7b 20 22 6f 6c 64 22 2c 20 33 20 7d 3b 20  = { "old", 3 }; 
275b2 20 2f 2a 20 4c 69 74 65 72 61 6c 20 22 6f 6c 64   /* Literal "old
275b3 22 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20  " token */.     
275b4 20 54 6f 6b 65 6e 20 74 4e 65 77 20 3d 20 7b 20   Token tNew = { 
275b5 22 6e 65 77 22 2c 20 33 20 7d 3b 20 20 2f 2a 20  "new", 3 };  /* 
275b6 4c 69 74 65 72 61 6c 20 22 6e 65 77 22 20 74 6f  Literal "new" to
275b7 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 54 6f 6b  ken */.      Tok
275b8 65 6e 20 74 46 72 6f 6d 43 6f 6c 3b 20 20 20 20  en tFromCol;    
275b9 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
275ba 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63 68   of column in ch
275bb 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  ild table */.   
275bc 20 20 20 54 6f 6b 65 6e 20 74 54 6f 43 6f 6c 3b     Token tToCol;
275bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
275be 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e  * Name of column
275bf 20 69 6e 20 70 61 72 65 6e 74 20 74 61 62 6c 65   in parent table
275c0 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 46   */.      int iF
275c1 72 6f 6d 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  romCol;         
275c2 20 20 20 20 20 20 2f 2a 20 49 64 78 20 6f 66 20        /* Idx of 
275c3 63 6f 6c 75 6d 6e 20 69 6e 20 63 68 69 6c 64 20  column in child 
275c4 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 45  table */.      E
275c5 78 70 72 20 2a 70 45 71 3b 20 20 20 20 20 20 20  xpr *pEq;       
275c6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 46             /* tF
275c7 72 6f 6d 43 6f 6c 20 3d 20 4f 4c 44 2e 74 54 6f  romCol = OLD.tTo
275c8 43 6f 6c 20 2a 2f 0a 0a 20 20 20 20 20 20 69 46  Col */..      iF
275c9 72 6f 6d 43 6f 6c 20 3d 20 61 69 43 6f 6c 20 3f  romCol = aiCol ?
275ca 20 61 69 43 6f 6c 5b 69 5d 20 3a 20 70 46 4b 65   aiCol[i] : pFKe
275cb 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f 6d  y->aCol[0].iFrom
275cc 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
275cd 69 46 72 6f 6d 43 6f 6c 3e 3d 30 20 29 3b 0a 20  iFromCol>=0 );. 
275ce 20 20 20 20 20 74 54 6f 43 6f 6c 2e 7a 20 3d 20       tToCol.z = 
275cf 70 49 64 78 20 3f 20 70 54 61 62 2d 3e 61 43 6f  pIdx ? pTab->aCo
275d0 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  l[pIdx->aiColumn
275d1 5b 69 5d 5d 2e 7a 4e 61 6d 65 20 3a 20 22 6f 69  [i]].zName : "oi
275d2 64 22 3b 0a 20 20 20 20 20 20 74 46 72 6f 6d 43  d";.      tFromC
275d3 6f 6c 2e 7a 20 3d 20 70 46 4b 65 79 2d 3e 70 46  ol.z = pFKey->pF
275d4 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 46 72 6f 6d 43  rom->aCol[iFromC
275d5 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 20 20  ol].zName;..    
275d6 20 20 74 54 6f 43 6f 6c 2e 6e 20 3d 20 73 71 6c    tToCol.n = sql
275d7 69 74 65 33 53 74 72 6c 65 6e 33 30 28 74 54 6f  ite3Strlen30(tTo
275d8 43 6f 6c 2e 7a 29 3b 0a 20 20 20 20 20 20 74 46  Col.z);.      tF
275d9 72 6f 6d 43 6f 6c 2e 6e 20 3d 20 73 71 6c 69 74  romCol.n = sqlit
275da 65 33 53 74 72 6c 65 6e 33 30 28 74 46 72 6f 6d  e3Strlen30(tFrom
275db 43 6f 6c 2e 7a 29 3b 0a 0a 20 20 20 20 20 20 2f  Col.z);..      /
275dc 2a 20 43 72 65 61 74 65 20 74 68 65 20 65 78 70  * Create the exp
275dd 72 65 73 73 69 6f 6e 20 22 4f 4c 44 2e 7a 54 6f  ression "OLD.zTo
275de 43 6f 6c 20 3d 20 7a 46 72 6f 6d 43 6f 6c 22 2e  Col = zFromCol".
275df 20 49 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   It is important
275e0 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 74  .      ** that t
275e1 68 65 20 22 4f 4c 44 2e 7a 54 6f 43 6f 6c 22 20  he "OLD.zToCol" 
275e2 74 65 72 6d 20 69 73 20 6f 6e 20 74 68 65 20 4c  term is on the L
275e3 48 53 20 6f 66 20 74 68 65 20 3d 20 6f 70 65 72  HS of the = oper
275e4 61 74 6f 72 2c 20 73 6f 0a 20 20 20 20 20 20 2a  ator, so.      *
275e5 2a 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e  * that the affin
275e6 69 74 79 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f  ity and collatio
275e7 6e 20 73 65 71 75 65 6e 63 65 20 61 73 73 6f 63  n sequence assoc
275e8 69 61 74 65 64 20 77 69 74 68 20 74 68 65 0a 20  iated with the. 
275e9 20 20 20 20 20 2a 2a 20 70 61 72 65 6e 74 20 74       ** parent t
275ea 61 62 6c 65 20 61 72 65 20 75 73 65 64 20 66 6f  able are used fo
275eb 72 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  r the comparison
275ec 2e 20 2a 2f 0a 20 20 20 20 20 20 70 45 71 20 3d  . */.      pEq =
275ed 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
275ee 61 72 73 65 2c 20 54 4b 5f 45 51 2c 0a 20 20 20  arse, TK_EQ,.   
275ef 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
275f0 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
275f1 4f 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  OT, .           
275f2 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
275f3 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20  arse, TK_ID, 0, 
275f4 30 2c 20 26 74 4f 6c 64 29 2c 0a 20 20 20 20 20  0, &tOld),.     
275f5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
275f6 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
275f7 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f 43 6f 6c  D, 0, 0, &tToCol
275f8 29 0a 20 20 20 20 20 20 20 20 20 20 2c 20 30 29  ).          , 0)
275f9 2c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ,.          sqli
275fa 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
275fb 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74   TK_ID, 0, 0, &t
275fc 46 72 6f 6d 43 6f 6c 29 0a 20 20 20 20 20 20 2c  FromCol).      ,
275fd 20 30 29 3b 0a 20 20 20 20 20 20 70 57 68 65 72   0);.      pWher
275fe 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  e = sqlite3ExprA
275ff 6e 64 28 64 62 2c 20 70 57 68 65 72 65 2c 20 70  nd(db, pWhere, p
27600 45 71 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 46  Eq);..      /* F
27601 6f 72 20 4f 4e 20 55 50 44 41 54 45 2c 20 63 6f  or ON UPDATE, co
27602 6e 73 74 72 75 63 74 20 74 68 65 20 6e 65 78 74  nstruct the next
27603 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45   term of the WHE
27604 4e 20 63 6c 61 75 73 65 2e 0a 20 20 20 20 20 20  N clause..      
27605 2a 2a 20 54 68 65 20 66 69 6e 61 6c 20 57 48 45  ** The final WHE
27606 4e 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 62 65  N clause will be
27607 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 20 20   like this:.    
27608 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20    **.      **   
27609 20 57 48 45 4e 20 4e 4f 54 28 6f 6c 64 2e 63 6f   WHEN NOT(old.co
2760a 6c 31 20 49 53 20 6e 65 77 2e 63 6f 6c 31 20 41  l1 IS new.col1 A
2760b 4e 44 20 2e 2e 2e 20 41 4e 44 20 6f 6c 64 2e 63  ND ... AND old.c
2760c 6f 6c 4e 20 49 53 20 6e 65 77 2e 63 6f 6c 4e 29  olN IS new.colN)
2760d 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2760e 69 66 28 20 70 43 68 61 6e 67 65 73 20 29 7b 0a  if( pChanges ){.
2760f 20 20 20 20 20 20 20 20 70 45 71 20 3d 20 73 71          pEq = sq
27610 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
27611 65 2c 20 54 4b 5f 49 53 2c 0a 20 20 20 20 20 20  e, TK_IS,.      
27612 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78        sqlite3PEx
27613 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f  pr(pParse, TK_DO
27614 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  T, .            
27615 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70    sqlite3PExpr(p
27616 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
27617 20 30 2c 20 26 74 4f 6c 64 29 2c 0a 20 20 20 20   0, &tOld),.    
27618 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27619 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2761a 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f  K_ID, 0, 0, &tTo
2761b 43 6f 6c 29 2c 0a 20 20 20 20 20 20 20 20 20 20  Col),.          
2761c 20 20 20 20 30 29 2c 0a 20 20 20 20 20 20 20 20      0),.        
2761d 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70 72      sqlite3PExpr
2761e 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c  (pParse, TK_DOT,
2761f 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
27620 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
27621 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
27622 2c 20 26 74 4e 65 77 29 2c 0a 20 20 20 20 20 20  , &tNew),.      
27623 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50          sqlite3P
27624 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
27625 49 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f 43 6f  ID, 0, 0, &tToCo
27626 6c 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  l),.            
27627 20 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20    0),.          
27628 20 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 57    0);.        pW
27629 68 65 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70  hen = sqlite3Exp
2762a 72 41 6e 64 28 64 62 2c 20 70 57 68 65 6e 2c 20  rAnd(db, pWhen, 
2762b 70 45 71 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  pEq);.      }.  
2762c 0a 20 20 20 20 20 20 69 66 28 20 61 63 74 69 6f  .      if( actio
2762d 6e 21 3d 4f 45 5f 52 65 73 74 72 69 63 74 20 26  n!=OE_Restrict &
2762e 26 20 28 61 63 74 69 6f 6e 21 3d 4f 45 5f 43 61  & (action!=OE_Ca
2762f 73 63 61 64 65 20 7c 7c 20 70 43 68 61 6e 67 65  scade || pChange
27630 73 29 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78  s) ){.        Ex
27631 70 72 20 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20  pr *pNew;.      
27632 20 20 69 66 28 20 61 63 74 69 6f 6e 3d 3d 4f 45    if( action==OE
27633 5f 43 61 73 63 61 64 65 20 29 7b 0a 20 20 20 20  _Cascade ){.    
27634 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
27635 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
27636 2c 20 54 4b 5f 44 4f 54 2c 20 0a 20 20 20 20 20  , TK_DOT, .     
27637 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
27638 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
27639 44 2c 20 30 2c 20 30 2c 20 26 74 4e 65 77 29 2c  D, 0, 0, &tNew),
2763a 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2763b 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2763c 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
2763d 74 54 6f 43 6f 6c 29 0a 20 20 20 20 20 20 20 20  tToCol).        
2763e 20 20 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20    , 0);.        
2763f 7d 65 6c 73 65 20 69 66 28 20 61 63 74 69 6f 6e  }else if( action
27640 3d 3d 4f 45 5f 53 65 74 44 66 6c 74 20 29 7b 0a  ==OE_SetDflt ){.
27641 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20 2a            Expr *
27642 70 44 66 6c 74 20 3d 20 70 46 4b 65 79 2d 3e 70  pDflt = pFKey->p
27643 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 46 72 6f 6d  From->aCol[iFrom
27644 43 6f 6c 5d 2e 70 44 66 6c 74 3b 0a 20 20 20 20  Col].pDflt;.    
27645 20 20 20 20 20 20 69 66 28 20 70 44 66 6c 74 20        if( pDflt 
27646 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  ){.            p
27647 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
27648 72 44 75 70 28 64 62 2c 20 70 44 66 6c 74 2c 20  rDup(db, pDflt, 
27649 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  0);.          }e
2764a 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
2764b 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 50   pNew = sqlite3P
2764c 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2764d 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  NULL, 0, 0, 0);.
2764e 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2764f 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27650 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
27651 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
27652 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20   TK_NULL, 0, 0, 
27653 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0);.        }.  
27654 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71        pList = sq
27655 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
27656 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4c 69 73  end(pParse, pLis
27657 74 2c 20 70 4e 65 77 29 3b 0a 20 20 20 20 20 20  t, pNew);.      
27658 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
27659 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  tSetName(pParse,
2765a 20 70 4c 69 73 74 2c 20 26 74 46 72 6f 6d 43 6f   pList, &tFromCo
2765b 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  l, 0);.      }. 
2765c 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2765d 44 62 46 72 65 65 28 64 62 2c 20 61 69 43 6f 6c  DbFree(db, aiCol
2765e 29 3b 0a 0a 20 20 20 20 7a 46 72 6f 6d 20 3d 20  );..    zFrom = 
2765f 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e 7a 4e  pFKey->pFrom->zN
27660 61 6d 65 3b 0a 20 20 20 20 6e 46 72 6f 6d 20 3d  ame;.    nFrom =
27661 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
27662 28 7a 46 72 6f 6d 29 3b 0a 0a 20 20 20 20 69 66  (zFrom);..    if
27663 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f 52 65 73  ( action==OE_Res
27664 74 72 69 63 74 20 29 7b 0a 20 20 20 20 20 20 54  trict ){.      T
27665 6f 6b 65 6e 20 74 46 72 6f 6d 3b 0a 20 20 20 20  oken tFrom;.    
27666 20 20 45 78 70 72 20 2a 70 52 61 69 73 65 3b 20    Expr *pRaise; 
27667 0a 0a 20 20 20 20 20 20 74 46 72 6f 6d 2e 7a 20  ..      tFrom.z 
27668 3d 20 7a 46 72 6f 6d 3b 0a 20 20 20 20 20 20 74  = zFrom;.      t
27669 46 72 6f 6d 2e 6e 20 3d 20 6e 46 72 6f 6d 3b 0a  From.n = nFrom;.
2766a 20 20 20 20 20 20 70 52 61 69 73 65 20 3d 20 73        pRaise = s
2766b 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54  qlite3Expr(db, T
2766c 4b 5f 52 41 49 53 45 2c 20 22 66 6f 72 65 69 67  K_RAISE, "foreig
2766d 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
2766e 20 66 61 69 6c 65 64 22 29 3b 0a 20 20 20 20 20   failed");.     
2766f 20 69 66 28 20 70 52 61 69 73 65 20 29 7b 0a 20   if( pRaise ){. 
27670 20 20 20 20 20 20 20 70 52 61 69 73 65 2d 3e 61         pRaise->a
27671 66 66 69 6e 69 74 79 20 3d 20 4f 45 5f 41 62 6f  ffinity = OE_Abo
27672 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rt;.      }.    
27673 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69    pSelect = sqli
27674 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61  te3SelectNew(pPa
27675 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20  rse, .          
27676 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
27677 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c  ppend(pParse, 0,
27678 20 70 52 61 69 73 65 29 2c 0a 20 20 20 20 20 20   pRaise),.      
27679 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69      sqlite3SrcLi
2767a 73 74 41 70 70 65 6e 64 28 64 62 2c 20 30 2c 20  stAppend(db, 0, 
2767b 26 74 46 72 6f 6d 2c 20 30 29 2c 0a 20 20 20 20  &tFrom, 0),.    
2767c 20 20 20 20 20 20 70 57 68 65 72 65 2c 0a 20 20        pWhere,.  
2767d 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30 2c          0, 0, 0,
2767e 20 30 2c 20 30 2c 20 30 0a 20 20 20 20 20 20 29   0, 0, 0.      )
2767f 3b 0a 20 20 20 20 20 20 70 57 68 65 72 65 20 3d  ;.      pWhere =
27680 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   0;.    }..    /
27681 2a 20 49 6e 20 74 68 65 20 63 75 72 72 65 6e 74  * In the current
27682 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c   implementation,
27683 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3d 3d 30 20   pTab->dbMem==0 
27684 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 65  for all tables e
27685 78 63 65 70 74 0a 20 20 20 20 2a 2a 20 66 6f 72  xcept.    ** for
27686 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
27687 73 20 75 73 65 64 20 74 6f 20 64 65 73 63 72 69  s used to descri
27688 62 65 20 73 75 62 71 75 65 72 69 65 73 2e 20 20  be subqueries.  
27689 41 6e 64 20 74 65 6d 70 6f 72 61 72 79 0a 20 20  And temporary.  
2768a 20 20 2a 2a 20 74 61 62 6c 65 73 20 64 6f 20 6e    ** tables do n
2768b 6f 74 20 68 61 76 65 20 66 6f 72 65 69 67 6e 20  ot have foreign 
2768c 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  key constraints.
2768d 20 20 48 65 6e 63 65 2c 20 70 54 61 62 2d 3e 64    Hence, pTab->d
2768e 62 4d 65 6d 0a 20 20 20 20 2a 2a 20 73 68 6f 75  bMem.    ** shou
2768f 6c 64 20 61 6c 77 61 79 73 20 62 65 20 30 20 74  ld always be 0 t
27690 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  here..    */.   
27691 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65   enableLookaside
27692 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
27693 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 20 20 64  .bEnabled;.    d
27694 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e  b->lookaside.bEn
27695 61 62 6c 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20  abled = 0;..    
27696 70 54 72 69 67 67 65 72 20 3d 20 28 54 72 69 67  pTrigger = (Trig
27697 67 65 72 20 2a 29 73 71 6c 69 74 65 33 44 62 4d  ger *)sqlite3DbM
27698 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20  allocZero(db, . 
27699 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 54 72         sizeof(Tr
2769a 69 67 67 65 72 29 20 2b 20 20 20 20 20 20 20 20  igger) +        
2769b 20 2f 2a 20 73 74 72 75 63 74 20 54 72 69 67 67   /* struct Trigg
2769c 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 69  er */.        si
2769d 7a 65 6f 66 28 54 72 69 67 67 65 72 53 74 65 70  zeof(TriggerStep
2769e 29 20 2b 20 20 20 20 20 2f 2a 20 53 69 6e 67 6c  ) +     /* Singl
2769f 65 20 73 74 65 70 20 69 6e 20 74 72 69 67 67 65  e step in trigge
276a0 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20  r program */.   
276a1 20 20 20 20 20 6e 46 72 6f 6d 20 2b 20 31 20 20       nFrom + 1  
276a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
276a3 2a 20 53 70 61 63 65 20 66 6f 72 20 70 53 74 65  * Space for pSte
276a4 70 2d 3e 74 61 72 67 65 74 2e 7a 20 2a 2f 0a 20  p->target.z */. 
276a5 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54     );.    if( pT
276a6 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20  rigger ){.      
276a7 70 53 74 65 70 20 3d 20 70 54 72 69 67 67 65 72  pStep = pTrigger
276a8 2d 3e 73 74 65 70 5f 6c 69 73 74 20 3d 20 28 54  ->step_list = (T
276a9 72 69 67 67 65 72 53 74 65 70 20 2a 29 26 70 54  riggerStep *)&pT
276aa 72 69 67 67 65 72 5b 31 5d 3b 0a 20 20 20 20 20  rigger[1];.     
276ab 20 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a   pStep->target.z
276ac 20 3d 20 28 63 68 61 72 20 2a 29 26 70 53 74 65   = (char *)&pSte
276ad 70 5b 31 5d 3b 0a 20 20 20 20 20 20 70 53 74 65  p[1];.      pSte
276ae 70 2d 3e 74 61 72 67 65 74 2e 6e 20 3d 20 6e 46  p->target.n = nF
276af 72 6f 6d 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  rom;.      memcp
276b0 79 28 28 63 68 61 72 20 2a 29 70 53 74 65 70 2d  y((char *)pStep-
276b1 3e 74 61 72 67 65 74 2e 7a 2c 20 7a 46 72 6f 6d  >target.z, zFrom
276b2 2c 20 6e 46 72 6f 6d 29 3b 0a 20 20 0a 20 20 20  , nFrom);.  .   
276b3 20 20 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65     pStep->pWhere
276b4 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
276b5 70 28 64 62 2c 20 70 57 68 65 72 65 2c 20 45 58  p(db, pWhere, EX
276b6 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20  PRDUP_REDUCE);. 
276b7 20 20 20 20 20 70 53 74 65 70 2d 3e 70 45 78 70       pStep->pExp
276b8 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  rList = sqlite3E
276b9 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
276ba 4c 69 73 74 2c 20 45 58 50 52 44 55 50 5f 52 45  List, EXPRDUP_RE
276bb 44 55 43 45 29 3b 0a 20 20 20 20 20 20 70 53 74  DUCE);.      pSt
276bc 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71  ep->pSelect = sq
276bd 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 64  lite3SelectDup(d
276be 62 2c 20 70 53 65 6c 65 63 74 2c 20 45 58 50 52  b, pSelect, EXPR
276bf 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
276c0 20 20 20 69 66 28 20 70 57 68 65 6e 20 29 7b 0a     if( pWhen ){.
276c1 20 20 20 20 20 20 20 20 70 57 68 65 6e 20 3d 20          pWhen = 
276c2 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
276c3 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 70 57 68  rse, TK_NOT, pWh
276c4 65 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20  en, 0, 0);.     
276c5 20 20 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68     pTrigger->pWh
276c6 65 6e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  en = sqlite3Expr
276c7 44 75 70 28 64 62 2c 20 70 57 68 65 6e 2c 20 45  Dup(db, pWhen, E
276c8 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
276c9 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
276ca 20 20 20 2f 2a 20 52 65 2d 65 6e 61 62 6c 65 20     /* Re-enable 
276cb 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  the lookaside bu
276cc 66 66 65 72 2c 20 69 66 20 69 74 20 77 61 73 20  ffer, if it was 
276cd 64 69 73 61 62 6c 65 64 20 65 61 72 6c 69 65 72  disabled earlier
276ce 2e 20 2a 2f 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f  . */.    db->loo
276cf 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20  kaside.bEnabled 
276d0 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64  = enableLookasid
276d1 65 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 45  e;..    sqlite3E
276d2 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57  xprDelete(db, pW
276d3 68 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  here);.    sqlit
276d4 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
276d5 20 70 57 68 65 6e 29 3b 0a 20 20 20 20 73 71 6c   pWhen);.    sql
276d6 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
276d7 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  te(db, pList);. 
276d8 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
276d9 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65  Delete(db, pSele
276da 63 74 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  ct);.    if( db-
276db 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 31  >mallocFailed==1
276dc 20 29 7b 0a 20 20 20 20 20 20 66 6b 54 72 69 67   ){.      fkTrig
276dd 67 65 72 44 65 6c 65 74 65 28 64 62 2c 20 70 54  gerDelete(db, pT
276de 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20 20 72  rigger);.      r
276df 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 0a  eturn 0;.    }..
276e0 20 20 20 20 73 77 69 74 63 68 28 20 61 63 74 69      switch( acti
276e1 6f 6e 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  on ){.      case
276e2 20 4f 45 5f 52 65 73 74 72 69 63 74 3a 0a 20 20   OE_Restrict:.  
276e3 20 20 20 20 20 20 70 53 74 65 70 2d 3e 6f 70 20        pStep->op 
276e4 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 20 0a 20 20  = TK_SELECT; .  
276e5 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
276e6 20 20 20 63 61 73 65 20 4f 45 5f 43 61 73 63 61     case OE_Casca
276e7 64 65 3a 20 0a 20 20 20 20 20 20 20 20 69 66 28  de: .        if(
276e8 20 21 70 43 68 61 6e 67 65 73 20 29 7b 20 0a 20   !pChanges ){ . 
276e9 20 20 20 20 20 20 20 20 20 70 53 74 65 70 2d 3e           pStep->
276ea 6f 70 20 3d 20 54 4b 5f 44 45 4c 45 54 45 3b 20  op = TK_DELETE; 
276eb 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
276ec 3b 20 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ; .        }.   
276ed 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20     default:.    
276ee 20 20 20 20 70 53 74 65 70 2d 3e 6f 70 20 3d 20      pStep->op = 
276ef 54 4b 5f 55 50 44 41 54 45 3b 0a 20 20 20 20 7d  TK_UPDATE;.    }
276f0 0a 20 20 20 20 70 53 74 65 70 2d 3e 70 54 72 69  .    pStep->pTri
276f1 67 20 3d 20 70 54 72 69 67 67 65 72 3b 0a 20 20  g = pTrigger;.  
276f2 20 20 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68    pTrigger->pSch
276f3 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68  ema = pTab->pSch
276f4 65 6d 61 3b 0a 20 20 20 20 70 54 72 69 67 67 65  ema;.    pTrigge
276f5 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 3d 20  r->pTabSchema = 
276f6 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20  pTab->pSchema;. 
276f7 20 20 20 70 46 4b 65 79 2d 3e 61 70 54 72 69 67     pFKey->apTrig
276f8 67 65 72 5b 69 41 63 74 69 6f 6e 5d 20 3d 20 70  ger[iAction] = p
276f9 54 72 69 67 67 65 72 3b 0a 20 20 20 20 70 54 72  Trigger;.    pTr
276fa 69 67 67 65 72 2d 3e 6f 70 20 3d 20 28 70 43 68  igger->op = (pCh
276fb 61 6e 67 65 73 20 3f 20 54 4b 5f 55 50 44 41 54  anges ? TK_UPDAT
276fc 45 20 3a 20 54 4b 5f 44 45 4c 45 54 45 29 3b 0a  E : TK_DELETE);.
276fd 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 54    }..  return pT
276fe 72 69 67 67 65 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rigger;.}../*.**
276ff 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
27700 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 64 65  s called when de
27701 6c 65 74 69 6e 67 20 6f 72 20 75 70 64 61 74 69  leting or updati
27702 6e 67 20 61 20 72 6f 77 20 74 6f 20 69 6d 70 6c  ng a row to impl
27703 65 6d 65 6e 74 0a 2a 2a 20 61 6e 79 20 72 65 71  ement.** any req
27704 75 69 72 65 64 20 43 41 53 43 41 44 45 2c 20 53  uired CASCADE, S
27705 45 54 20 4e 55 4c 4c 20 6f 72 20 53 45 54 20 44  ET NULL or SET D
27706 45 46 41 55 4c 54 20 61 63 74 69 6f 6e 73 2e 0a  EFAULT actions..
27707 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
27708 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b  E void sqlite3Fk
27709 41 63 74 69 6f 6e 73 28 0a 20 20 50 61 72 73 65  Actions(.  Parse
2770a 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2770b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2770c 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
2770d 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2770e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2770f 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67 20   /* Table being 
27710 75 70 64 61 74 65 64 20 6f 72 20 64 65 6c 65 74  updated or delet
27711 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 45 78 70  ed from */.  Exp
27712 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c  rList *pChanges,
27713 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27714 43 68 61 6e 67 65 2d 6c 69 73 74 20 66 6f 72 20  Change-list for 
27715 55 50 44 41 54 45 2c 20 4e 55 4c 4c 20 66 6f 72  UPDATE, NULL for
27716 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 69 6e 74   DELETE */.  int
27717 20 72 65 67 4f 6c 64 20 20 20 20 20 20 20 20 20   regOld         
27718 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27719 41 64 64 72 65 73 73 20 6f 66 20 61 72 72 61 79  Address of array
2771a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f 6c 64 20   containing old 
2771b 72 6f 77 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20 49  row */.){.  /* I
2771c 66 20 66 6f 72 65 69 67 6e 2d 6b 65 79 20 73 75  f foreign-key su
2771d 70 70 6f 72 74 20 69 73 20 65 6e 61 62 6c 65 64  pport is enabled
2771e 2c 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  , iterate throug
2771f 68 20 61 6c 6c 20 46 4b 73 20 74 68 61 74 20 0a  h all FKs that .
27720 20 20 2a 2a 20 72 65 66 65 72 20 74 6f 20 74 61    ** refer to ta
27721 62 6c 65 20 70 54 61 62 2e 20 49 66 20 74 68 65  ble pTab. If the
27722 72 65 20 69 73 20 61 6e 20 61 63 74 69 6f 6e 20  re is an action 
27723 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
27724 74 68 65 20 46 4b 20 0a 20 20 2a 2a 20 66 6f 72  the FK .  ** for
27725 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20   this operation 
27726 28 65 69 74 68 65 72 20 75 70 64 61 74 65 20 6f  (either update o
27727 72 20 64 65 6c 65 74 65 29 2c 20 69 6e 76 6f 6b  r delete), invok
27728 65 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64  e the associated
27729 20 0a 20 20 2a 2a 20 74 72 69 67 67 65 72 20 73   .  ** trigger s
2772a 75 62 2d 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a  ub-program.  */.
2772b 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62    if( pParse->db
2772c 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46  ->flags&SQLITE_F
2772d 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b 0a 20 20  oreignKeys ){.  
2772e 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 3b 20 20    FKey *pFKey;  
2772f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27730 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
27731 61 62 6c 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28  able */.    for(
27732 70 46 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 46  pFKey = sqlite3F
27733 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54 61 62  kReferences(pTab
27734 29 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65 79 3d  ); pFKey; pFKey=
27735 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 29 7b  pFKey->pNextTo){
27736 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 20 2a  .      Trigger *
27737 70 41 63 74 69 6f 6e 20 3d 20 66 6b 41 63 74 69  pAction = fkActi
27738 6f 6e 54 72 69 67 67 65 72 28 70 50 61 72 73 65  onTrigger(pParse
27739 2c 20 70 54 61 62 2c 20 70 46 4b 65 79 2c 20 70  , pTab, pFKey, p
2773a 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 20 20  Changes);.      
2773b 69 66 28 20 70 41 63 74 69 6f 6e 20 29 7b 0a 20  if( pAction ){. 
2773c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f         sqlite3Co
2773d 64 65 52 6f 77 54 72 69 67 67 65 72 44 69 72 65  deRowTriggerDire
2773e 63 74 28 70 50 61 72 73 65 2c 20 70 41 63 74 69  ct(pParse, pActi
2773f 6f 6e 2c 20 70 54 61 62 2c 20 72 65 67 4f 6c 64  on, pTab, regOld
27740 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 30 29 3b 0a  , OE_Abort, 0);.
27741 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
27742 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 69  }.}..#endif /* i
27743 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
27744 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a 2f 2a  T_TRIGGER */../*
27745 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d  .** Free all mem
27746 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  ory associated w
27747 69 74 68 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ith foreign key 
27748 64 65 66 69 6e 69 74 69 6f 6e 73 20 61 74 74 61  definitions atta
27749 63 68 65 64 20 74 6f 0a 2a 2a 20 74 61 62 6c 65  ched to.** table
2774a 20 70 54 61 62 2e 20 52 65 6d 6f 76 65 20 74 68   pTab. Remove th
2774b 65 20 64 65 6c 65 74 65 64 20 66 6f 72 65 69 67  e deleted foreig
2774c 6e 20 6b 65 79 73 20 66 72 6f 6d 20 74 68 65 20  n keys from the 
2774d 53 63 68 65 6d 61 2e 66 6b 65 79 48 61 73 68 0a  Schema.fkeyHash.
2774e 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a  ** hash table..*
2774f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
27750 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 44   void sqlite3FkD
27751 65 6c 65 74 65 28 54 61 62 6c 65 20 2a 70 54 61  elete(Table *pTa
27752 62 29 7b 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65  b){.  FKey *pFKe
27753 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
27754 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
27755 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
27756 46 4b 65 79 20 2a 70 4e 65 78 74 3b 20 20 20 20  FKey *pNext;    
27757 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27758 2f 2a 20 43 6f 70 79 20 6f 66 20 70 46 4b 65 79  /* Copy of pFKey
27759 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 2a 2f 0a 0a  ->pNextFrom */..
2775a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62    for(pFKey=pTab
2775b 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20  ->pFKey; pFKey; 
2775c 70 46 4b 65 79 3d 70 4e 65 78 74 29 7b 0a 0a 20  pFKey=pNext){.. 
2775d 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65     /* Remove the
2775e 20 46 4b 20 66 72 6f 6d 20 74 68 65 20 66 6b 65   FK from the fke
2775f 79 48 61 73 68 20 68 61 73 68 20 74 61 62 6c 65  yHash hash table
27760 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 46 4b  . */.    if( pFK
27761 65 79 2d 3e 70 50 72 65 76 54 6f 20 29 7b 0a 20  ey->pPrevTo ){. 
27762 20 20 20 20 20 70 46 4b 65 79 2d 3e 70 50 72 65       pFKey->pPre
27763 76 54 6f 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 70  vTo->pNextTo = p
27764 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 3b 0a 20  FKey->pNextTo;. 
27765 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27766 76 6f 69 64 20 2a 64 61 74 61 20 3d 20 28 76 6f  void *data = (vo
27767 69 64 20 2a 29 70 46 4b 65 79 2d 3e 70 4e 65 78  id *)pFKey->pNex
27768 74 54 6f 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74  tTo;.      const
27769 20 63 68 61 72 20 2a 7a 20 3d 20 28 64 61 74 61   char *z = (data
2776a 20 3f 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54   ? pFKey->pNextT
2776b 6f 2d 3e 7a 54 6f 20 3a 20 70 46 4b 65 79 2d 3e  o->zTo : pFKey->
2776c 7a 54 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  zTo);.      sqli
2776d 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70  te3HashInsert(&p
2776e 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6b  Tab->pSchema->fk
2776f 65 79 48 61 73 68 2c 20 7a 2c 20 73 71 6c 69 74  eyHash, z, sqlit
27770 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 2c 20 64  e3Strlen30(z), d
27771 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ata);.    }.    
27772 69 66 28 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74  if( pFKey->pNext
27773 54 6f 20 29 7b 0a 20 20 20 20 20 20 70 46 4b 65  To ){.      pFKe
27774 79 2d 3e 70 4e 65 78 74 54 6f 2d 3e 70 50 72 65  y->pNextTo->pPre
27775 76 54 6f 20 3d 20 70 46 4b 65 79 2d 3e 70 50 72  vTo = pFKey->pPr
27776 65 76 54 6f 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  evTo;.    }..   
27777 20 2f 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 74   /* Delete any t
27778 72 69 67 67 65 72 73 20 63 72 65 61 74 65 64 20  riggers created 
27779 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 63 74  to implement act
2777a 69 6f 6e 73 20 66 6f 72 20 74 68 69 73 20 46 4b  ions for this FK
2777b 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  . */.#ifndef SQL
2777c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2777d 0a 20 20 20 20 66 6b 54 72 69 67 67 65 72 44 65  .    fkTriggerDe
2777e 6c 65 74 65 28 70 54 61 62 2d 3e 64 62 4d 65 6d  lete(pTab->dbMem
2777f 2c 20 70 46 4b 65 79 2d 3e 61 70 54 72 69 67 67  , pFKey->apTrigg
27780 65 72 5b 30 5d 29 3b 0a 20 20 20 20 66 6b 54 72  er[0]);.    fkTr
27781 69 67 67 65 72 44 65 6c 65 74 65 28 70 54 61 62  iggerDelete(pTab
27782 2d 3e 64 62 4d 65 6d 2c 20 70 46 4b 65 79 2d 3e  ->dbMem, pFKey->
27783 61 70 54 72 69 67 67 65 72 5b 31 5d 29 3b 0a 23  apTrigger[1]);.#
27784 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 45 56  endif..    /* EV
27785 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37 20  : R-30323-21917 
27786 45 61 63 68 20 66 6f 72 65 69 67 6e 20 6b 65 79  Each foreign key
27787 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 53   constraint in S
27788 51 4c 69 74 65 20 69 73 0a 20 20 20 20 2a 2a 20  QLite is.    ** 
27789 63 6c 61 73 73 69 66 69 65 64 20 61 73 20 65 69  classified as ei
2778a 74 68 65 72 20 69 6d 6d 65 64 69 61 74 65 20 6f  ther immediate o
2778b 72 20 64 65 66 65 72 72 65 64 2e 0a 20 20 20 20  r deferred..    
2778c 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
2778d 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64  FKey->isDeferred
2778e 3d 3d 30 20 7c 7c 20 70 46 4b 65 79 2d 3e 69 73  ==0 || pFKey->is
2778f 44 65 66 65 72 72 65 64 3d 3d 31 20 29 3b 0a 0a  Deferred==1 );..
27790 20 20 20 20 70 4e 65 78 74 20 3d 20 70 46 4b 65      pNext = pFKe
27791 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b 0a 20 20  y->pNextFrom;.  
27792 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
27793 70 54 61 62 2d 3e 64 62 4d 65 6d 2c 20 70 46 4b  pTab->dbMem, pFK
27794 65 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  ey);.  }.}.#endi
27795 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51 4c 49  f /* ifndef SQLI
27796 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
27797 4b 45 59 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  KEY */../*******
27798 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
27799 6b 65 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  key.c **********
2779a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2779b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2779c 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2779d 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2779e 6c 65 20 69 6e 73 65 72 74 2e 63 20 2a 2a 2a 2a  le insert.c ****
2779f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277a1 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
277a2 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
277a3 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
277a4 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
277a5 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
277a6 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
277a7 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
277a8 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
277a9 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
277aa 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
277ab 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
277ac 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
277ad 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
277ae 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
277af 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
277b0 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
277b1 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
277b2 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
277b3 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
277b4 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
277b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
277b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
277b9 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
277ba 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74  ains C code rout
277bb 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61  ines that are ca
277bc 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
277bd 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20  er.** to handle 
277be 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74  INSERT statement
277bf 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a  s in SQLite..*/.
277c0 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
277c1 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 6f  code that will o
277c2 70 65 6e 20 61 20 74 61 62 6c 65 20 66 6f 72 20  pen a table for 
277c3 72 65 61 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  reading..*/.SQLI
277c4 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
277c5 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
277c6 28 0a 20 20 50 61 72 73 65 20 2a 70 2c 20 20 20  (.  Parse *p,   
277c7 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
277c8 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56  code into this V
277c9 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75  DBE */.  int iCu
277ca 72 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  r,       /* The 
277cb 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  cursor number of
277cc 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
277cd 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20  int iDb,        
277ce 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
277cf 69 6e 64 65 78 20 69 6e 20 73 71 6c 69 74 65 33  index in sqlite3
277d0 2e 61 44 62 5b 5d 20 2a 2f 0a 20 20 54 61 62 6c  .aDb[] */.  Tabl
277d1 65 20 2a 70 54 61 62 2c 20 20 20 20 2f 2a 20 54  e *pTab,    /* T
277d2 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6f  he table to be o
277d3 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f  pened */.  int o
277d4 70 63 6f 64 65 20 20 20 20 20 20 2f 2a 20 4f 50  pcode      /* OP
277d5 5f 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f  _OpenRead or OP_
277d6 4f 70 65 6e 57 72 69 74 65 20 2a 2f 0a 29 7b 0a  OpenWrite */.){.
277d7 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 66 28    Vdbe *v;.  if(
277d8 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
277d9 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 76 20 3d   ) return;.  v =
277da 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
277db 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70  p);.  assert( op
277dc 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69  code==OP_OpenWri
277dd 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50  te || opcode==OP
277de 5f 4f 70 65 6e 52 65 61 64 20 29 3b 0a 20 20 73  _OpenRead );.  s
277df 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28  qlite3TableLock(
277e0 70 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e  p, iDb, pTab->tn
277e1 75 6d 2c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f  um, (opcode==OP_
277e2 4f 70 65 6e 57 72 69 74 65 29 3f 31 3a 30 2c 20  OpenWrite)?1:0, 
277e3 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
277e4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
277e5 33 28 76 2c 20 6f 70 63 6f 64 65 2c 20 69 43 75  3(v, opcode, iCu
277e6 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69  r, pTab->tnum, i
277e7 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  Db);.  sqlite3Vd
277e8 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
277e9 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  , SQLITE_INT_TO_
277ea 50 54 52 28 70 54 61 62 2d 3e 6e 43 6f 6c 29 2c  PTR(pTab->nCol),
277eb 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 56 64   P4_INT32);.  Vd
277ec 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25  beComment((v, "%
277ed 73 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  s", pTab->zName)
277ee 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
277ef 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
277f0 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  the column affin
277f1 69 74 79 20 73 74 72 69 6e 67 20 61 73 73 6f 63  ity string assoc
277f2 69 61 74 65 64 20 77 69 74 68 20 69 6e 64 65 78  iated with index
277f3 0a 2a 2a 20 70 49 64 78 2e 20 41 20 63 6f 6c 75  .** pIdx. A colu
277f4 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  mn affinity stri
277f5 6e 67 20 68 61 73 20 6f 6e 65 20 63 68 61 72 61  ng has one chara
277f6 63 74 65 72 20 66 6f 72 20 65 61 63 68 20 63 6f  cter for each co
277f7 6c 75 6d 6e 20 69 6e 20 0a 2a 2a 20 74 68 65 20  lumn in .** the 
277f8 74 61 62 6c 65 2c 20 61 63 63 6f 72 64 69 6e 67  table, according
277f9 20 74 6f 20 74 68 65 20 61 66 66 69 6e 69 74 79   to the affinity
277fa 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 3a 0a   of the column:.
277fb 2a 2a 0a 2a 2a 20 20 43 68 61 72 61 63 74 65 72  **.**  Character
277fc 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66        Column aff
277fd 69 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d  inity.**  ------
277fe 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
277ff 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27  --------.**  'a'
27800 20 20 20 20 20 20 20 20 20 20 20 20 54 45 58 54              TEXT
27801 0a 2a 2a 20 20 27 62 27 20 20 20 20 20 20 20 20  .**  'b'        
27802 20 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27      NONE.**  'c'
27803 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4d 45              NUME
27804 52 49 43 0a 2a 2a 20 20 27 64 27 20 20 20 20 20  RIC.**  'd'     
27805 20 20 20 20 20 20 20 49 4e 54 45 47 45 52 0a 2a         INTEGER.*
27806 2a 20 20 27 65 27 20 20 20 20 20 20 20 20 20 20  *  'e'          
27807 20 20 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20    REAL.**.** An 
27808 65 78 74 72 61 20 27 62 27 20 69 73 20 61 70 70  extra 'b' is app
27809 65 6e 64 65 64 20 74 6f 20 74 68 65 20 65 6e 64  ended to the end
2780a 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 74   of the string t
2780b 6f 20 63 6f 76 65 72 20 74 68 65 0a 2a 2a 20 72  o cover the.** r
2780c 6f 77 69 64 20 74 68 61 74 20 61 70 70 65 61 72  owid that appear
2780d 73 20 61 73 20 74 68 65 20 6c 61 73 74 20 63 6f  s as the last co
2780e 6c 75 6d 6e 20 69 6e 20 65 76 65 72 79 20 69 6e  lumn in every in
2780f 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72  dex..**.** Memor
27810 79 20 66 6f 72 20 74 68 65 20 62 75 66 66 65 72  y for the buffer
27811 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
27812 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 61 66 66  column index aff
27813 69 6e 69 74 79 20 73 74 72 69 6e 67 0a 2a 2a 20  inity string.** 
27814 69 73 20 6d 61 6e 61 67 65 64 20 61 6c 6f 6e 67  is managed along
27815 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20 6f   with the rest o
27816 66 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75  f the Index stru
27817 63 74 75 72 65 2e 20 49 74 20 77 69 6c 6c 20 62  cture. It will b
27818 65 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20 77 68  e.** released wh
27819 65 6e 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  en sqlite3Delete
2781a 49 6e 64 65 78 28 29 20 69 73 20 63 61 6c 6c 65  Index() is calle
2781b 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
2781c 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
2781d 2a 73 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66  *sqlite3IndexAff
2781e 69 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 76  inityStr(Vdbe *v
2781f 2c 20 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a  , Index *pIdx){.
27820 20 20 69 66 28 20 21 70 49 64 78 2d 3e 7a 43 6f    if( !pIdx->zCo
27821 6c 41 66 66 20 29 7b 0a 20 20 20 20 2f 2a 20 54  lAff ){.    /* T
27822 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 20  he first time a 
27823 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  column affinity 
27824 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70 61 72  string for a par
27825 74 69 63 75 6c 61 72 20 69 6e 64 65 78 20 69 73  ticular index is
27826 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72 65 64  .    ** required
27827 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63 61 74 65  , it is allocate
27828 64 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20  d and populated 
27829 68 65 72 65 2e 20 49 74 20 69 73 20 74 68 65 6e  here. It is then
2782a 20 73 74 6f 72 65 64 20 61 73 0a 20 20 20 20 2a   stored as.    *
2782b 2a 20 61 20 6d 65 6d 62 65 72 20 6f 66 20 74 68  * a member of th
2782c 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72  e Index structur
2782d 65 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e 74  e for subsequent
2782e 20 75 73 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20   use..    **.   
2782f 20 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 61   ** The column a
27830 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 77  ffinity string w
27831 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 62  ill eventually b
27832 65 20 64 65 6c 65 74 65 64 20 62 79 0a 20 20 20  e deleted by.   
27833 20 2a 2a 20 73 71 6c 69 74 65 44 65 6c 65 74 65   ** sqliteDelete
27834 49 6e 64 65 78 28 29 20 77 68 65 6e 20 74 68 65  Index() when the
27835 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65   Index structure
27836 20 69 74 73 65 6c 66 20 69 73 20 63 6c 65 61 6e   itself is clean
27837 65 64 0a 20 20 20 20 2a 2a 20 75 70 2e 0a 20 20  ed.    ** up..  
27838 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 3b 0a    */.    int n;.
27839 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
2783a 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 3b 0a  = pIdx->pTable;.
2783b 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
2783c 3d 20 73 71 6c 69 74 65 33 56 64 62 65 44 62 28  = sqlite3VdbeDb(
2783d 76 29 3b 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43  v);.    pIdx->zC
2783e 6f 6c 41 66 66 20 3d 20 28 63 68 61 72 20 2a 29  olAff = (char *)
2783f 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 49  sqlite3Malloc(pI
27840 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a  dx->nColumn+2);.
27841 20 20 20 20 69 66 28 20 21 70 49 64 78 2d 3e 7a      if( !pIdx->z
27842 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 20 20  ColAff ){.      
27843 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
27844 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75   = 1;.      retu
27845 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 0;.    }.    
27846 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70 49 64 78 2d  for(n=0; n<pIdx-
27847 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b 2b 29 7b 0a  >nColumn; n++){.
27848 20 20 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c        pIdx->zCol
27849 41 66 66 5b 6e 5d 20 3d 20 70 54 61 62 2d 3e 61  Aff[n] = pTab->a
2784a 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75  Col[pIdx->aiColu
2784b 6d 6e 5b 6e 5d 5d 2e 61 66 66 69 6e 69 74 79 3b  mn[n]].affinity;
2784c 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 64 78 2d  .    }.    pIdx-
2784d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b 2b 5d 20 3d 20  >zColAff[n++] = 
2784e 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b  SQLITE_AFF_NONE;
2784f 0a 20 20 20 20 70 49 64 78 2d 3e 7a 43 6f 6c 41  .    pIdx->zColA
27850 66 66 5b 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20  ff[n] = 0;.  }. 
27851 0a 20 20 72 65 74 75 72 6e 20 70 49 64 78 2d 3e  .  return pIdx->
27852 7a 43 6f 6c 41 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a  zColAff;.}../*.*
27853 2a 20 53 65 74 20 50 34 20 6f 66 20 74 68 65 20  * Set P4 of the 
27854 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e  most recently in
27855 73 65 72 74 65 64 20 6f 70 63 6f 64 65 20 74 6f  serted opcode to
27856 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69   a column affini
27857 74 79 0a 2a 2a 20 73 74 72 69 6e 67 20 66 6f 72  ty.** string for
27858 20 74 61 62 6c 65 20 70 54 61 62 2e 20 41 20 63   table pTab. A c
27859 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73  olumn affinity s
2785a 74 72 69 6e 67 20 68 61 73 20 6f 6e 65 20 63 68  tring has one ch
2785b 61 72 61 63 74 65 72 0a 2a 2a 20 66 6f 72 20 65  aracter.** for e
2785c 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  ach column index
2785d 65 64 20 62 79 20 74 68 65 20 69 6e 64 65 78 2c  ed by the index,
2785e 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   according to th
2785f 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68  e affinity of th
27860 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a  e.** column:.**.
27861 2a 2a 20 20 43 68 61 72 61 63 74 65 72 20 20 20  **  Character   
27862 20 20 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69     Column affini
27863 74 79 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ty.**  ---------
27864 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27865 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20  -----.**  'a'   
27866 20 20 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a           TEXT.**
27867 20 20 27 62 27 20 20 20 20 20 20 20 20 20 20 20    'b'           
27868 20 4e 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20   NONE.**  'c'   
27869 20 20 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43           NUMERIC
2786a 0a 2a 2a 20 20 27 64 27 20 20 20 20 20 20 20 20  .**  'd'        
2786b 20 20 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20      INTEGER.**  
2786c 27 65 27 20 20 20 20 20 20 20 20 20 20 20 20 52  'e'            R
2786d 45 41 4c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  EAL.*/.SQLITE_PR
2786e 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2786f 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79 53  e3TableAffinityS
27870 74 72 28 56 64 62 65 20 2a 76 2c 20 54 61 62 6c  tr(Vdbe *v, Tabl
27871 65 20 2a 70 54 61 62 29 7b 0a 20 20 2f 2a 20 54  e *pTab){.  /* T
27872 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61 20  he first time a 
27873 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  column affinity 
27874 73 74 72 69 6e 67 20 66 6f 72 20 61 20 70 61 72  string for a par
27875 74 69 63 75 6c 61 72 20 74 61 62 6c 65 0a 20 20  ticular table.  
27876 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2c 20  ** is required, 
27877 69 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20  it is allocated 
27878 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 68 65  and populated he
27879 72 65 2e 20 49 74 20 69 73 20 74 68 65 6e 20 0a  re. It is then .
2787a 20 20 2a 2a 20 73 74 6f 72 65 64 20 61 73 20 61    ** stored as a
2787b 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 54   member of the T
2787c 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 66  able structure f
2787d 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 75 73  or subsequent us
2787e 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  e..  **.  ** The
2787f 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
27880 20 73 74 72 69 6e 67 20 77 69 6c 6c 20 65 76 65   string will eve
27881 6e 74 75 61 6c 6c 79 20 62 65 20 64 65 6c 65 74  ntually be delet
27882 65 64 20 62 79 0a 20 20 2a 2a 20 73 71 6c 69 74  ed by.  ** sqlit
27883 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 29 20  e3DeleteTable() 
27884 77 68 65 6e 20 74 68 65 20 54 61 62 6c 65 20 73  when the Table s
27885 74 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 20  tructure itself 
27886 69 73 20 63 6c 65 61 6e 65 64 20 75 70 2e 0a 20  is cleaned up.. 
27887 20 2a 2f 0a 20 20 69 66 28 20 21 70 54 61 62 2d   */.  if( !pTab-
27888 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20  >zColAff ){.    
27889 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66 3b 0a 20  char *zColAff;. 
2788a 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 73 71     int i;.    sq
2788b 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
2788c 74 65 33 56 64 62 65 44 62 28 76 29 3b 0a 0a 20  te3VdbeDb(v);.. 
2788d 20 20 20 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68     zColAff = (ch
2788e 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  ar *)sqlite3Mall
2788f 6f 63 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29  oc(pTab->nCol+1)
27890 3b 0a 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 41  ;.    if( !zColA
27891 66 66 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  ff ){.      db->
27892 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
27893 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
27894 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69      }..    for(i
27895 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
27896 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 43  ; i++){.      zC
27897 6f 6c 41 66 66 5b 69 5d 20 3d 20 70 54 61 62 2d  olAff[i] = pTab-
27898 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74  >aCol[i].affinit
27899 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43 6f  y;.    }.    zCo
2789a 6c 41 66 66 5b 70 54 61 62 2d 3e 6e 43 6f 6c 5d  lAff[pTab->nCol]
2789b 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 20 20 70 54   = '\0';..    pT
2789c 61 62 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 7a 43  ab->zColAff = zC
2789d 6f 6c 41 66 66 3b 0a 20 20 7d 0a 0a 20 20 73 71  olAff;.  }..  sq
2789e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2789f 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a  4(v, -1, pTab->z
278a0 43 6f 6c 41 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f  ColAff, 0);.}../
278a1 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e 2d  *.** Return non-
278a2 7a 65 72 6f 20 69 66 20 74 68 65 20 74 61 62 6c  zero if the tabl
278a3 65 20 70 54 61 62 20 69 6e 20 64 61 74 61 62 61  e pTab in databa
278a4 73 65 20 69 44 62 20 6f 72 20 61 6e 79 20 6f 66  se iDb or any of
278a5 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20   its indices.** 
278a6 68 61 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64  have been opened
278a7 20 61 74 20 61 6e 79 20 70 6f 69 6e 74 20 69 6e   at any point in
278a8 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
278a9 6d 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 6c  m beginning at l
278aa 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 53 74 61 72  ocation.** iStar
278ab 74 41 64 64 72 20 74 68 72 6f 75 67 68 74 20 74  tAddr throught t
278ac 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 72  he end of the pr
278ad 6f 67 72 61 6d 2e 20 20 54 68 69 73 20 69 73 20  ogram.  This is 
278ae 75 73 65 64 20 74 6f 20 73 65 65 20 69 66 20 0a  used to see if .
278af 2a 2a 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6f  ** a statement o
278b0 66 20 74 68 65 20 66 6f 72 6d 20 20 22 49 4e 53  f the form  "INS
278b1 45 52 54 20 49 4e 54 4f 20 3c 69 44 62 2c 20 70  ERT INTO <iDb, p
278b2 54 61 62 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 22  Tab> SELECT ..."
278b3 20 63 61 6e 20 0a 2a 2a 20 72 75 6e 20 77 69 74   can .** run wit
278b4 68 6f 75 74 20 75 73 69 6e 67 20 74 65 6d 70 6f  hout using tempo
278b5 72 61 72 79 20 74 61 62 6c 65 20 66 6f 72 20 74  rary table for t
278b6 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  he results of th
278b7 65 20 53 45 4c 45 43 54 2e 20 0a 2a 2f 0a 73 74  e SELECT. .*/.st
278b8 61 74 69 63 20 69 6e 74 20 72 65 61 64 73 54 61  atic int readsTa
278b9 62 6c 65 28 50 61 72 73 65 20 2a 70 2c 20 69 6e  ble(Parse *p, in
278ba 74 20 69 53 74 61 72 74 41 64 64 72 2c 20 69 6e  t iStartAddr, in
278bb 74 20 69 44 62 2c 20 54 61 62 6c 65 20 2a 70 54  t iDb, Table *pT
278bc 61 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  ab){.  Vdbe *v =
278bd 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
278be 70 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  p);.  int i;.  i
278bf 6e 74 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 65  nt iEnd = sqlite
278c0 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
278c1 28 76 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  (v);.#ifndef SQL
278c2 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
278c3 54 41 42 4c 45 0a 20 20 56 54 61 62 6c 65 20 2a  TABLE.  VTable *
278c4 70 56 54 61 62 20 3d 20 49 73 56 69 72 74 75 61  pVTab = IsVirtua
278c5 6c 28 70 54 61 62 29 20 3f 20 73 71 6c 69 74 65  l(pTab) ? sqlite
278c6 33 47 65 74 56 54 61 62 6c 65 28 70 2d 3e 64 62  3GetVTable(p->db
278c7 2c 20 70 54 61 62 29 20 3a 20 30 3b 0a 23 65 6e  , pTab) : 0;.#en
278c8 64 69 66 0a 0a 20 20 66 6f 72 28 69 3d 69 53 74  dif..  for(i=iSt
278c9 61 72 74 41 64 64 72 3b 20 69 3c 69 45 6e 64 3b  artAddr; i<iEnd;
278ca 20 69 2b 2b 29 7b 0a 20 20 20 20 56 64 62 65 4f   i++){.    VdbeO
278cb 70 20 2a 70 4f 70 20 3d 20 73 71 6c 69 74 65 33  p *pOp = sqlite3
278cc 56 64 62 65 47 65 74 4f 70 28 76 2c 20 69 29 3b  VdbeGetOp(v, i);
278cd 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
278ce 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70  !=0 );.    if( p
278cf 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f  Op->opcode==OP_O
278d0 70 65 6e 52 65 61 64 20 26 26 20 70 4f 70 2d 3e  penRead && pOp->
278d1 70 33 3d 3d 69 44 62 20 29 7b 0a 20 20 20 20 20  p3==iDb ){.     
278d2 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 0a   Index *pIndex;.
278d3 20 20 20 20 20 20 69 6e 74 20 74 6e 75 6d 20 3d        int tnum =
278d4 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 20 20 20 20   pOp->p2;.      
278d5 69 66 28 20 74 6e 75 6d 3d 3d 70 54 61 62 2d 3e  if( tnum==pTab->
278d6 74 6e 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  tnum ){.        
278d7 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
278d8 7d 0a 20 20 20 20 20 20 66 6f 72 28 70 49 6e 64  }.      for(pInd
278d9 65 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  ex=pTab->pIndex;
278da 20 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3d   pIndex; pIndex=
278db 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 29 7b 0a  pIndex->pNext){.
278dc 20 20 20 20 20 20 20 20 69 66 28 20 74 6e 75 6d          if( tnum
278dd 3d 3d 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 20 29  ==pIndex->tnum )
278de 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
278df 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rn 1;.        }.
278e0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69        }.    }.#i
278e1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
278e2 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
278e3 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f     if( pOp->opco
278e4 64 65 3d 3d 4f 50 5f 56 4f 70 65 6e 20 26 26 20  de==OP_VOpen && 
278e5 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3d 3d 70  pOp->p4.pVtab==p
278e6 56 54 61 62 20 29 7b 0a 20 20 20 20 20 20 61 73  VTab ){.      as
278e7 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 70 56  sert( pOp->p4.pV
278e8 74 61 62 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  tab!=0 );.      
278e9 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
278ea 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a  ype==P4_VTAB );.
278eb 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
278ec 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
278ed 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
278ee 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
278ef 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e  MIT_AUTOINCREMEN
278f0 54 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 6f  T./*.** Locate o
278f1 72 20 63 72 65 61 74 65 20 61 6e 20 41 75 74 6f  r create an Auto
278f2 69 6e 63 49 6e 66 6f 20 73 74 72 75 63 74 75 72  incInfo structur
278f3 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
278f4 68 20 74 61 62 6c 65 20 70 54 61 62 0a 2a 2a 20  h table pTab.** 
278f5 77 68 69 63 68 20 69 73 20 69 6e 20 64 61 74 61  which is in data
278f6 62 61 73 65 20 69 44 62 2e 20 20 52 65 74 75 72  base iDb.  Retur
278f7 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e  n the register n
278f8 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 72 65  umber for the re
278f9 67 69 73 74 65 72 0a 2a 2a 20 74 68 61 74 20 68  gister.** that h
278fa 6f 6c 64 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  olds the maximum
278fb 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68   rowid..**.** Th
278fc 65 72 65 20 69 73 20 61 74 20 6d 6f 73 74 20 6f  ere is at most o
278fd 6e 65 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 73  ne AutoincInfo s
278fe 74 72 75 63 74 75 72 65 20 70 65 72 20 74 61 62  tructure per tab
278ff 6c 65 20 65 76 65 6e 20 69 66 20 74 68 65 0a 2a  le even if the.*
27900 2a 20 73 61 6d 65 20 74 61 62 6c 65 20 69 73 20  * same table is 
27901 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 65 64 20  autoincremented 
27902 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 64  multiple times d
27903 75 65 20 74 6f 20 69 6e 73 65 72 74 73 20 77 69  ue to inserts wi
27904 74 68 69 6e 0a 2a 2a 20 74 72 69 67 67 65 72 73  thin.** triggers
27905 2e 20 20 41 20 6e 65 77 20 41 75 74 6f 69 6e 63  .  A new Autoinc
27906 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69  Info structure i
27907 73 20 63 72 65 61 74 65 64 20 69 66 20 74 68 69  s created if thi
27908 73 20 69 73 20 74 68 65 0a 2a 2a 20 66 69 72 73  s is the.** firs
27909 74 20 75 73 65 20 6f 66 20 74 61 62 6c 65 20 70  t use of table p
2790a 54 61 62 2e 20 20 4f 6e 20 32 6e 64 20 61 6e 64  Tab.  On 2nd and
2790b 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 73   subsequent uses
2790c 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a  , the original.*
2790d 2a 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74  * AutoincInfo st
2790e 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64 2e  ructure is used.
2790f 0a 2a 2a 0a 2a 2a 20 54 68 72 65 65 20 6d 65 6d  .**.** Three mem
27910 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20 61 72  ory locations ar
27911 65 20 61 6c 6c 6f 63 61 74 65 64 3a 0a 2a 2a 0a  e allocated:.**.
27912 2a 2a 20 20 20 28 31 29 20 20 52 65 67 69 73 74  **   (1)  Regist
27913 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e  er to hold the n
27914 61 6d 65 20 6f 66 20 74 68 65 20 70 54 61 62 20  ame of the pTab 
27915 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 28 32 29 20  table..**   (2) 
27916 20 52 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c   Register to hol
27917 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 52 4f  d the maximum RO
27918 57 49 44 20 6f 66 20 70 54 61 62 2e 0a 2a 2a 20  WID of pTab..** 
27919 20 20 28 33 29 20 20 52 65 67 69 73 74 65 72 20    (3)  Register 
2791a 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f 77 69  to hold the rowi
2791b 64 20 69 6e 20 73 71 6c 69 74 65 5f 73 65 71 75  d in sqlite_sequ
2791c 65 6e 63 65 20 6f 66 20 70 54 61 62 0a 2a 2a 0a  ence of pTab.**.
2791d 2a 2a 20 54 68 65 20 32 6e 64 20 72 65 67 69 73  ** The 2nd regis
2791e 74 65 72 20 69 73 20 74 68 65 20 6f 6e 65 20 74  ter is the one t
2791f 68 61 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e  hat is returned.
27920 20 20 54 68 61 74 20 69 73 20 61 6c 6c 20 74 68    That is all th
27921 65 0a 2a 2a 20 69 6e 73 65 72 74 20 72 6f 75 74  e.** insert rout
27922 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 6b 6e 6f  ine needs to kno
27923 77 20 61 62 6f 75 74 2e 0a 2a 2f 0a 73 74 61 74  w about..*/.stat
27924 69 63 20 69 6e 74 20 61 75 74 6f 49 6e 63 42 65  ic int autoIncBe
27925 67 69 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  gin(.  Parse *pP
27926 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61  arse,      /* Pa
27927 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
27928 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20  .  int iDb,     
27929 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2792a 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
2792b 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a  holding pTab */.
2792c 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 20 20    Table *pTab   
2792d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
2792e 6c 65 20 77 65 20 61 72 65 20 77 72 69 74 69 6e  le we are writin
2792f 67 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  g to */.){.  int
27930 20 6d 65 6d 49 64 20 3d 20 30 3b 20 20 20 20 20   memId = 0;     
27931 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
27932 64 69 6e 67 20 6d 61 78 69 6d 75 6d 20 72 6f 77  ding maximum row
27933 69 64 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62  id */.  if( pTab
27934 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
27935 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b  Autoincrement ){
27936 0a 20 20 20 20 50 61 72 73 65 20 2a 70 54 6f 70  .    Parse *pTop
27937 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50  level = sqlite3P
27938 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61  arseToplevel(pPa
27939 72 73 65 29 3b 0a 20 20 20 20 41 75 74 6f 69 6e  rse);.    Autoin
2793a 63 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 0a 20  cInfo *pInfo;.. 
2793b 20 20 20 70 49 6e 66 6f 20 3d 20 70 54 6f 70 6c     pInfo = pTopl
2793c 65 76 65 6c 2d 3e 70 41 69 6e 63 3b 0a 20 20 20  evel->pAinc;.   
2793d 20 77 68 69 6c 65 28 20 70 49 6e 66 6f 20 26 26   while( pInfo &&
2793e 20 70 49 6e 66 6f 2d 3e 70 54 61 62 21 3d 70 54   pInfo->pTab!=pT
2793f 61 62 20 29 7b 20 70 49 6e 66 6f 20 3d 20 70 49  ab ){ pInfo = pI
27940 6e 66 6f 2d 3e 70 4e 65 78 74 3b 20 7d 0a 20 20  nfo->pNext; }.  
27941 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29    if( pInfo==0 )
27942 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 20 3d 20  {.      pInfo = 
27943 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
27944 61 77 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73  aw(pParse->db, s
27945 69 7a 65 6f 66 28 2a 70 49 6e 66 6f 29 29 3b 0a  izeof(*pInfo));.
27946 20 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f 3d        if( pInfo=
27947 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
27948 20 20 20 20 20 70 49 6e 66 6f 2d 3e 70 4e 65 78       pInfo->pNex
27949 74 20 3d 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 70  t = pToplevel->p
2794a 41 69 6e 63 3b 0a 20 20 20 20 20 20 70 54 6f 70  Ainc;.      pTop
2794b 6c 65 76 65 6c 2d 3e 70 41 69 6e 63 20 3d 20 70  level->pAinc = p
2794c 49 6e 66 6f 3b 0a 20 20 20 20 20 20 70 49 6e 66  Info;.      pInf
2794d 6f 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a  o->pTab = pTab;.
2794e 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69 44 62        pInfo->iDb
2794f 20 3d 20 69 44 62 3b 0a 20 20 20 20 20 20 70 54   = iDb;.      pT
27950 6f 70 6c 65 76 65 6c 2d 3e 6e 4d 65 6d 2b 2b 3b  oplevel->nMem++;
27951 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27952 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f    /* Register to
27953 20 68 6f 6c 64 20 6e 61 6d 65 20 6f 66 20 74 61   hold name of ta
27954 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 70 49 6e  ble */.      pIn
27955 66 6f 2d 3e 72 65 67 43 74 72 20 3d 20 2b 2b 70  fo->regCtr = ++p
27956 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d 65 6d 3b 20  Toplevel->nMem; 
27957 20 2f 2a 20 4d 61 78 20 72 6f 77 69 64 20 72 65   /* Max rowid re
27958 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  gister */.      
27959 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d 65 6d 2b  pToplevel->nMem+
2795a 2b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  +;              
2795b 20 20 20 20 2f 2a 20 52 6f 77 69 64 20 69 6e 20      /* Rowid in 
2795c 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
2795d 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d  */.    }.    mem
2795e 49 64 20 3d 20 70 49 6e 66 6f 2d 3e 72 65 67 43  Id = pInfo->regC
2795f 74 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  tr;.  }.  return
27960 20 6d 65 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   memId;.}../*.**
27961 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65   This routine ge
27962 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74 68 61  nerates code tha
27963 74 20 77 69 6c 6c 20 69 6e 69 74 69 61 6c 69 7a  t will initializ
27964 65 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a 20  e all of the.** 
27965 72 65 67 69 73 74 65 72 20 75 73 65 64 20 62 79  register used by
27966 20 74 68 65 20 61 75 74 6f 69 6e 63 72 65 6d 65   the autoincreme
27967 6e 74 20 74 72 61 63 6b 65 72 2e 20 20 0a 2a 2f  nt tracker.  .*/
27968 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
27969 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75 74 6f  void sqlite3Auto
2796a 69 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e 28 50  incrementBegin(P
2796b 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
2796c 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 3b   AutoincInfo *p;
2796d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2796e 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
2796f 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e   an AUTOINCREMEN
27970 54 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  T */.  sqlite3 *
27971 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
27972 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
27973 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
27974 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20    Db *pDb;      
27975 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27976 44 61 74 61 62 61 73 65 20 6f 6e 6c 79 20 61 75  Database only au
27977 74 6f 69 6e 63 20 74 61 62 6c 65 20 2a 2f 0a 20  toinc table */. 
27978 20 69 6e 74 20 6d 65 6d 49 64 3b 20 20 20 20 20   int memId;     
27979 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2797a 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
2797b 6d 61 78 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  max rowid */.  i
2797c 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20 20 20  nt addr;        
2797d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 56            /* A V
2797e 44 42 45 20 61 64 64 72 65 73 73 20 2a 2f 0a 20  DBE address */. 
2797f 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
27980 65 2d 3e 70 56 64 62 65 3b 20 20 20 2f 2a 20 56  e->pVdbe;   /* V
27981 44 42 45 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  DBE under constr
27982 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20  uction */..  /* 
27983 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
27984 6e 65 76 65 72 20 63 61 6c 6c 65 64 20 64 75 72  never called dur
27985 69 6e 67 20 74 72 69 67 67 65 72 2d 67 65 6e 65  ing trigger-gene
27986 72 61 74 69 6f 6e 2e 20 20 49 74 20 69 73 0a 20  ration.  It is. 
27987 20 2a 2a 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20   ** only called 
27988 66 72 6f 6d 20 74 68 65 20 74 6f 70 2d 6c 65 76  from the top-lev
27989 65 6c 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  el */.  assert( 
2798a 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
2798b 54 61 62 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Tab==0 );.  asse
2798c 72 74 28 20 70 50 61 72 73 65 3d 3d 73 71 6c 69  rt( pParse==sqli
2798d 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
2798e 28 70 50 61 72 73 65 29 20 29 3b 0a 0a 20 20 61  (pParse) );..  a
2798f 73 73 65 72 74 28 20 76 20 29 3b 20 20 20 2f 2a  ssert( v );   /*
27990 20 57 65 20 66 61 69 6c 65 64 20 6c 6f 6e 67 20   We failed long 
27991 61 67 6f 20 69 66 20 74 68 69 73 20 69 73 20 6e  ago if this is n
27992 6f 74 20 73 6f 20 2a 2f 0a 20 20 66 6f 72 28 70  ot so */.  for(p
27993 20 3d 20 70 50 61 72 73 65 2d 3e 70 41 69 6e 63   = pParse->pAinc
27994 3b 20 70 3b 20 70 20 3d 20 70 2d 3e 70 4e 65 78  ; p; p = p->pNex
27995 74 29 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 64  t){.    pDb = &d
27996 62 2d 3e 61 44 62 5b 70 2d 3e 69 44 62 5d 3b 0a  b->aDb[p->iDb];.
27997 20 20 20 20 6d 65 6d 49 64 20 3d 20 70 2d 3e 72      memId = p->r
27998 65 67 43 74 72 3b 0a 20 20 20 20 73 71 6c 69 74  egCtr;.    sqlit
27999 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72  e3OpenTable(pPar
2799a 73 65 2c 20 30 2c 20 70 2d 3e 69 44 62 2c 20 70  se, 0, p->iDb, p
2799b 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65  Db->pSchema->pSe
2799c 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  qTab, OP_OpenRea
2799d 64 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73  d);.    addr = s
2799e 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2799f 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71  tAddr(v);.    sq
279a0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
279a1 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
279a2 2c 20 6d 65 6d 49 64 2d 31 2c 20 30 2c 20 70 2d  , memId-1, 0, p-
279a3 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29  >pTab->zName, 0)
279a4 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
279a5 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
279a6 77 69 6e 64 2c 20 30 2c 20 61 64 64 72 2b 39 29  wind, 0, addr+9)
279a7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
279a8 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
279a9 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 6d 65 6d 49  lumn, 0, 0, memI
279aa 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  d);.    sqlite3V
279ab 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
279ac 4e 65 2c 20 6d 65 6d 49 64 2d 31 2c 20 61 64 64  Ne, memId-1, add
279ad 72 2b 37 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20  r+7, memId);.   
279ae 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
279af 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f 4a  geP5(v, SQLITE_J
279b0 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
279b1 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
279b2 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 30  2(v, OP_Rowid, 0
279b3 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20  , memId+1);.    
279b4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
279b5 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
279b6 30 2c 20 31 2c 20 6d 65 6d 49 64 29 3b 0a 20 20  0, 1, memId);.  
279b7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
279b8 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
279b9 30 2c 20 61 64 64 72 2b 39 29 3b 0a 20 20 20 20  0, addr+9);.    
279ba 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
279bb 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 30 2c  2(v, OP_Next, 0,
279bc 20 61 64 64 72 2b 32 29 3b 0a 20 20 20 20 73 71   addr+2);.    sq
279bd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
279be 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
279bf 2c 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71  , memId);.    sq
279c0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
279c1 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20  v, OP_Close);.  
279c2 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74  }.}../*.** Updat
279c3 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f  e the maximum ro
279c4 77 69 64 20 66 6f 72 20 61 6e 20 61 75 74 6f 69  wid for an autoi
279c5 6e 63 72 65 6d 65 6e 74 20 63 61 6c 63 75 6c 61  ncrement calcula
279c6 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tion..**.** This
279c7 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20   routine should 
279c8 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74  be called when t
279c9 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74  he top of the st
279ca 61 63 6b 20 68 6f 6c 64 73 20 61 0a 2a 2a 20 6e  ack holds a.** n
279cb 65 77 20 72 6f 77 69 64 20 74 68 61 74 20 69 73  ew rowid that is
279cc 20 61 62 6f 75 74 20 74 6f 20 62 65 20 69 6e 73   about to be ins
279cd 65 72 74 65 64 2e 20 20 49 66 20 74 68 61 74 20  erted.  If that 
279ce 6e 65 77 20 72 6f 77 69 64 20 69 73 0a 2a 2a 20  new rowid is.** 
279cf 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
279d0 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 69 6e  maximum rowid in
279d1 20 74 68 65 20 6d 65 6d 49 64 20 6d 65 6d 6f 72   the memId memor
279d2 79 20 63 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65  y cell, then the
279d3 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  .** memory cell 
279d4 69 73 20 75 70 64 61 74 65 64 2e 20 20 54 68 65  is updated.  The
279d5 20 73 74 61 63 6b 20 69 73 20 75 6e 63 68 61 6e   stack is unchan
279d6 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ged..*/.static v
279d7 6f 69 64 20 61 75 74 6f 49 6e 63 53 74 65 70 28  oid autoIncStep(
279d8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
279d9 6e 74 20 6d 65 6d 49 64 2c 20 69 6e 74 20 72 65  nt memId, int re
279da 67 52 6f 77 69 64 29 7b 0a 20 20 69 66 28 20 6d  gRowid){.  if( m
279db 65 6d 49 64 3e 30 20 29 7b 0a 20 20 20 20 73 71  emId>0 ){.    sq
279dc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
279dd 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f  pParse->pVdbe, O
279de 50 5f 4d 65 6d 4d 61 78 2c 20 6d 65 6d 49 64 2c  P_MemMax, memId,
279df 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 0a   regRowid);.  }.
279e0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
279e1 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20  utine generates 
279e2 74 68 65 20 63 6f 64 65 20 6e 65 65 64 65 64 20  the code needed 
279e3 74 6f 20 77 72 69 74 65 20 61 75 74 6f 69 6e 63  to write autoinc
279e4 72 65 6d 65 6e 74 0a 2a 2a 20 6d 61 78 69 6d 75  rement.** maximu
279e5 6d 20 72 6f 77 69 64 20 76 61 6c 75 65 73 20 62  m rowid values b
279e6 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  ack into the sql
279e7 69 74 65 5f 73 65 71 75 65 6e 63 65 20 72 65 67  ite_sequence reg
279e8 69 73 74 65 72 2e 0a 2a 2a 20 45 76 65 72 79 20  ister..** Every 
279e9 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 6d  statement that m
279ea 69 67 68 74 20 64 6f 20 61 6e 20 49 4e 53 45 52  ight do an INSER
279eb 54 20 69 6e 74 6f 20 61 6e 20 61 75 74 6f 69 6e  T into an autoin
279ec 63 72 65 6d 65 6e 74 0a 2a 2a 20 74 61 62 6c 65  crement.** table
279ed 20 28 65 69 74 68 65 72 20 64 69 72 65 63 74 6c   (either directl
279ee 79 20 6f 72 20 74 68 72 6f 75 67 68 20 74 72 69  y or through tri
279ef 67 67 65 72 73 29 20 6e 65 65 64 73 20 74 6f 20  ggers) needs to 
279f0 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75  call this.** rou
279f1 74 69 6e 65 20 6a 75 73 74 20 62 65 66 6f 72 65  tine just before
279f2 20 74 68 65 20 22 65 78 69 74 22 20 63 6f 64 65   the "exit" code
279f3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
279f4 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
279f5 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64  AutoincrementEnd
279f6 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
279f7 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a  .  AutoincInfo *
279f8 70 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  p;.  Vdbe *v = p
279f9 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
279fa 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
279fb 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73  arse->db;..  ass
279fc 65 72 74 28 20 76 20 29 3b 0a 20 20 66 6f 72 28  ert( v );.  for(
279fd 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 41 69 6e  p = pParse->pAin
279fe 63 3b 20 70 3b 20 70 20 3d 20 70 2d 3e 70 4e 65  c; p; p = p->pNe
279ff 78 74 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62  xt){.    Db *pDb
27a00 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 2d 3e 69   = &db->aDb[p->i
27a01 44 62 5d 3b 0a 20 20 20 20 69 6e 74 20 6a 31 2c  Db];.    int j1,
27a02 20 6a 32 2c 20 6a 33 2c 20 6a 34 2c 20 6a 35 3b   j2, j3, j4, j5;
27a03 0a 20 20 20 20 69 6e 74 20 69 52 65 63 3b 0a 20  .    int iRec;. 
27a04 20 20 20 69 6e 74 20 6d 65 6d 49 64 20 3d 20 70     int memId = p
27a05 2d 3e 72 65 67 43 74 72 3b 0a 0a 20 20 20 20 69  ->regCtr;..    i
27a06 52 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74  Rec = sqlite3Get
27a07 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
27a08 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e  .    sqlite3Open
27a09 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
27a0a 20 70 2d 3e 69 44 62 2c 20 70 44 62 2d 3e 70 53   p->iDb, pDb->pS
27a0b 63 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 2c 20  chema->pSeqTab, 
27a0c 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20  OP_OpenWrite);. 
27a0d 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56     j1 = sqlite3V
27a0e 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
27a0f 4e 6f 74 4e 75 6c 6c 2c 20 6d 65 6d 49 64 2b 31  NotNull, memId+1
27a10 29 3b 0a 20 20 20 20 6a 32 20 3d 20 73 71 6c 69  );.    j2 = sqli
27a11 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
27a12 20 4f 50 5f 52 65 77 69 6e 64 29 3b 0a 20 20 20   OP_Rewind);.   
27a13 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j3 = sqlite3Vdb
27a14 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
27a15 6c 75 6d 6e 2c 20 30 2c 20 30 2c 20 69 52 65 63  lumn, 0, 0, iRec
27a16 29 3b 0a 20 20 20 20 6a 34 20 3d 20 73 71 6c 69  );.    j4 = sqli
27a17 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
27a18 20 4f 50 5f 45 71 2c 20 6d 65 6d 49 64 2d 31 2c   OP_Eq, memId-1,
27a19 20 30 2c 20 69 52 65 63 29 3b 0a 20 20 20 20 73   0, iRec);.    s
27a1a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27a1b 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 30 2c 20  (v, OP_Next, 0, 
27a1c 6a 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j3);.    sqlite3
27a1d 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
27a1e 6a 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j2);.    sqlite3
27a1f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27a20 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c 20 6d 65  _NewRowid, 0, me
27a21 6d 49 64 2b 31 29 3b 0a 20 20 20 20 6a 35 20 3d  mId+1);.    j5 =
27a22 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27a23 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a  p0(v, OP_Goto);.
27a24 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
27a25 75 6d 70 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a  umpHere(v, j4);.
27a26 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27a27 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69  ddOp2(v, OP_Rowi
27a28 64 2c 20 30 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a  d, 0, memId+1);.
27a29 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
27a2a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a  umpHere(v, j1);.
27a2b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
27a2c 75 6d 70 48 65 72 65 28 76 2c 20 6a 35 29 3b 0a  umpHere(v, j5);.
27a2d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27a2e 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
27a2f 52 65 63 6f 72 64 2c 20 6d 65 6d 49 64 2d 31 2c  Record, memId-1,
27a30 20 32 2c 20 69 52 65 63 29 3b 0a 20 20 20 20 73   2, iRec);.    s
27a31 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
27a32 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 30  (v, OP_Insert, 0
27a33 2c 20 69 52 65 63 2c 20 6d 65 6d 49 64 2b 31 29  , iRec, memId+1)
27a34 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27a35 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46  eChangeP5(v, OPF
27a36 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20  LAG_APPEND);.   
27a37 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27a38 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 29 3b  p0(v, OP_Close);
27a39 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
27a3a 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
27a3b 65 2c 20 69 52 65 63 29 3b 0a 20 20 7d 0a 7d 0a  e, iRec);.  }.}.
27a3c 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49 66 20 53  #else./*.** If S
27a3d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
27a3e 4e 43 52 45 4d 45 4e 54 20 69 73 20 64 65 66 69  NCREMENT is defi
27a3f 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 74 68  ned, then the th
27a40 72 65 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  ree routines.** 
27a41 61 62 6f 76 65 20 61 72 65 20 61 6c 6c 20 6e 6f  above are all no
27a42 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66 69 6e 65  -ops.*/.# define
27a43 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 41 2c   autoIncBegin(A,
27a44 42 2c 43 29 20 28 30 29 0a 23 20 64 65 66 69 6e  B,C) (0).# defin
27a45 65 20 61 75 74 6f 49 6e 63 53 74 65 70 28 41 2c  e autoIncStep(A,
27a46 42 2c 43 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53  B,C).#endif /* S
27a47 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
27a48 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f 2a  NCREMENT */.../*
27a49 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
27a4a 74 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69  tion */.static i
27a4b 6e 74 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74  nt xferOptimizat
27a4c 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
27a4d 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
27a4e 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
27a4f 2f 0a 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74  /.  Table *pDest
27a50 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
27a51 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e   table we are in
27a52 73 65 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a  serting into */.
27a53 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
27a54 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c  t,      /* A SEL
27a55 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ECT statement to
27a56 20 75 73 65 20 61 73 20 74 68 65 20 64 61 74 61   use as the data
27a57 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74   source */.  int
27a58 20 6f 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20   onError,       
27a59 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e     /* How to han
27a5a 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65  dle constraint e
27a5b 72 72 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69  rrors */.  int i
27a5c 44 62 44 65 73 74 20 20 20 20 20 20 20 20 20 20  DbDest          
27a5d 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
27a5e 20 6f 66 20 70 44 65 73 74 20 2a 2f 0a 29 3b 0a   of pDest */.);.
27a5f 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
27a60 69 6e 65 20 69 73 20 63 61 6c 6c 20 74 6f 20 68  ine is call to h
27a61 61 6e 64 6c 65 20 53 51 4c 20 6f 66 20 74 68 65  andle SQL of the
27a62 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73   following forms
27a63 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 69 6e 73 65 72  :.**.**    inser
27a64 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44  t into TABLE (ID
27a65 4c 49 53 54 29 20 76 61 6c 75 65 73 28 45 58 50  LIST) values(EXP
27a66 52 4c 49 53 54 29 0a 2a 2a 20 20 20 20 69 6e 73  RLIST).**    ins
27a67 65 72 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28  ert into TABLE (
27a68 49 44 4c 49 53 54 29 20 73 65 6c 65 63 74 0a 2a  IDLIST) select.*
27a69 2a 0a 2a 2a 20 54 68 65 20 49 44 4c 49 53 54 20  *.** The IDLIST 
27a6a 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 61  following the ta
27a6b 62 6c 65 20 6e 61 6d 65 20 69 73 20 61 6c 77 61  ble name is alwa
27a6c 79 73 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66  ys optional.  If
27a6d 20 6f 6d 69 74 74 65 64 2c 0a 2a 2a 20 74 68 65   omitted,.** the
27a6e 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  n a list of all 
27a6f 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65 20  columns for the 
27a70 74 61 62 6c 65 20 69 73 20 73 75 62 73 74 69 74  table is substit
27a71 75 74 65 64 2e 20 20 54 68 65 20 49 44 4c 49 53  uted.  The IDLIS
27a72 54 0a 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20  T.** appears in 
27a73 74 68 65 20 70 43 6f 6c 75 6d 6e 20 70 61 72 61  the pColumn para
27a74 6d 65 74 65 72 2e 20 20 70 43 6f 6c 75 6d 6e 20  meter.  pColumn 
27a75 69 73 20 4e 55 4c 4c 20 69 66 20 49 44 4c 49 53  is NULL if IDLIS
27a76 54 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a  T is omitted..**
27a77 0a 2a 2a 20 54 68 65 20 70 4c 69 73 74 20 70 61  .** The pList pa
27a78 72 61 6d 65 74 65 72 20 68 6f 6c 64 73 20 45 58  rameter holds EX
27a79 50 52 4c 49 53 54 20 69 6e 20 74 68 65 20 66 69  PRLIST in the fi
27a7a 72 73 74 20 66 6f 72 6d 20 6f 66 20 74 68 65 20  rst form of the 
27a7b 49 4e 53 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d  INSERT.** statem
27a7c 65 6e 74 20 61 62 6f 76 65 2c 20 61 6e 64 20 70  ent above, and p
27a7d 53 65 6c 65 63 74 20 69 73 20 4e 55 4c 4c 2e 20  Select is NULL. 
27a7e 20 46 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20   For the second 
27a7f 66 6f 72 6d 2c 20 70 4c 69 73 74 20 69 73 0a 2a  form, pList is.*
27a80 2a 20 4e 55 4c 4c 20 61 6e 64 20 70 53 65 6c 65  * NULL and pSele
27a81 63 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ct is a pointer 
27a82 74 6f 20 74 68 65 20 73 65 6c 65 63 74 20 73 74  to the select st
27a83 61 74 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20  atement used to 
27a84 67 65 6e 65 72 61 74 65 0a 2a 2a 20 64 61 74 61  generate.** data
27a85 20 66 6f 72 20 74 68 65 20 69 6e 73 65 72 74 2e   for the insert.
27a86 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
27a87 67 65 6e 65 72 61 74 65 64 20 66 6f 6c 6c 6f 77  generated follow
27a88 73 20 6f 6e 65 20 6f 66 20 66 6f 75 72 20 74 65  s one of four te
27a89 6d 70 6c 61 74 65 73 2e 20 20 46 6f 72 20 61 20  mplates.  For a 
27a8a 73 69 6d 70 6c 65 0a 2a 2a 20 73 65 6c 65 63 74  simple.** select
27a8b 20 77 69 74 68 20 64 61 74 61 20 63 6f 6d 69 6e   with data comin
27a8c 67 20 66 72 6f 6d 20 61 20 56 41 4c 55 45 53 20  g from a VALUES 
27a8d 63 6c 61 75 73 65 2c 20 74 68 65 20 63 6f 64 65  clause, the code
27a8e 20 65 78 65 63 75 74 65 73 0a 2a 2a 20 6f 6e 63   executes.** onc
27a8f 65 20 73 74 72 61 69 67 68 74 20 64 6f 77 6e 20  e straight down 
27a90 74 68 72 6f 75 67 68 2e 20 20 50 73 65 75 64 6f  through.  Pseudo
27a91 2d 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 28 77  -code follows (w
27a92 65 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 74  e call this.** t
27a93 68 65 20 22 31 73 74 20 74 65 6d 70 6c 61 74 65  he "1st template
27a94 22 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  "):.**.**       
27a95 20 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72    open write cur
27a96 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61  sor to <table> a
27a97 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a  nd its indices.*
27a98 2a 20 20 20 20 20 20 20 20 20 70 75 74 73 20 56  *         puts V
27a99 41 4c 55 45 53 20 63 6c 61 75 73 65 20 65 78 70  ALUES clause exp
27a9a 72 65 73 73 69 6f 6e 73 20 6f 6e 74 6f 20 74 68  ressions onto th
27a9b 65 20 73 74 61 63 6b 0a 2a 2a 20 20 20 20 20 20  e stack.**      
27a9c 20 20 20 77 72 69 74 65 20 74 68 65 20 72 65 73     write the res
27a9d 75 6c 74 69 6e 67 20 72 65 63 6f 72 64 20 69 6e  ulting record in
27a9e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20  to <table>.**   
27a9f 20 20 20 20 20 20 63 6c 65 61 6e 75 70 0a 2a 2a        cleanup.**
27aa0 0a 2a 2a 20 54 68 65 20 74 68 72 65 65 20 72 65  .** The three re
27aa1 6d 61 69 6e 69 6e 67 20 74 65 6d 70 6c 61 74 65  maining template
27aa2 73 20 61 73 73 75 6d 65 20 74 68 65 20 73 74 61  s assume the sta
27aa3 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65  tement is of the
27aa4 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 49 4e   form.**.**   IN
27aa5 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65  SERT INTO <table
27aa6 3e 20 53 45 4c 45 43 54 20 2e 2e 2e 0a 2a 2a 0a  > SELECT ....**.
27aa7 2a 2a 20 49 66 20 74 68 65 20 53 45 4c 45 43 54  ** If the SELECT
27aa8 20 63 6c 61 75 73 65 20 69 73 20 6f 66 20 74 68   clause is of th
27aa9 65 20 72 65 73 74 72 69 63 74 65 64 20 66 6f 72  e restricted for
27aaa 6d 20 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  m "SELECT * FROM
27aab 20 3c 74 61 62 6c 65 32 3e 22 20 2d 0a 2a 2a 20   <table2>" -.** 
27aac 69 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69  in other words i
27aad 66 20 74 68 65 20 53 45 4c 45 43 54 20 70 75 6c  f the SELECT pul
27aae 6c 73 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66  ls all columns f
27aaf 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62  rom a single tab
27ab0 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 72 65 20  le.** and there 
27ab1 69 73 20 6e 6f 20 57 48 45 52 45 20 6f 72 20 4c  is no WHERE or L
27ab2 49 4d 49 54 20 6f 72 20 47 52 4f 55 50 20 42 59  IMIT or GROUP BY
27ab3 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61   or ORDER BY cla
27ab4 75 73 65 73 2c 20 61 6e 64 0a 2a 2a 20 69 66 20  uses, and.** if 
27ab5 3c 74 61 62 6c 65 32 3e 20 61 6e 64 20 3c 74 61  <table2> and <ta
27ab6 62 6c 65 31 3e 20 61 72 65 20 64 69 73 74 69 6e  ble1> are distin
27ab7 63 74 20 74 61 62 6c 65 73 20 62 75 74 20 68 61  ct tables but ha
27ab8 76 65 20 69 64 65 6e 74 69 63 61 6c 0a 2a 2a 20  ve identical.** 
27ab9 73 63 68 65 6d 61 73 2c 20 69 6e 63 6c 75 64 69  schemas, includi
27aba 6e 67 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 20  ng all the same 
27abb 69 6e 64 69 63 65 73 2c 20 74 68 65 6e 20 61 20  indices, then a 
27abc 73 70 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61  special optimiza
27abd 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b  tion.** is invok
27abe 65 64 20 74 68 61 74 20 63 6f 70 69 65 73 20 72  ed that copies r
27abf 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20  aw records from 
27ac0 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f  <table2> over to
27ac1 20 3c 74 61 62 6c 65 31 3e 2e 0a 2a 2a 20 53 65   <table1>..** Se
27ac2 65 20 74 68 65 20 78 66 65 72 4f 70 74 69 6d 69  e the xferOptimi
27ac3 7a 61 74 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f  zation() functio
27ac4 6e 20 66 6f 72 20 74 68 65 20 69 6d 70 6c 65 6d  n for the implem
27ac5 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  entation of this
27ac6 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 2e 20 20 54  .** template.  T
27ac7 68 69 73 20 69 73 20 74 68 65 20 32 6e 64 20 74  his is the 2nd t
27ac8 65 6d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20  emplate..**.**  
27ac9 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 77 72         open a wr
27aca 69 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74  ite cursor to <t
27acb 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20  able>.**        
27acc 20 6f 70 65 6e 20 72 65 61 64 20 63 75 72 73 6f   open read curso
27acd 72 20 6f 6e 20 3c 74 61 62 6c 65 32 3e 0a 2a 2a  r on <table2>.**
27ace 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65           transfe
27acf 72 20 61 6c 6c 20 72 65 63 6f 72 64 73 20 69 6e  r all records in
27ad0 20 3c 74 61 62 6c 65 32 3e 20 6f 76 65 72 20 74   <table2> over t
27ad1 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20  o <table>.**    
27ad2 20 20 20 20 20 63 6c 6f 73 65 20 63 75 72 73 6f       close curso
27ad3 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 6f  rs.**         fo
27ad4 72 65 61 63 68 20 69 6e 64 65 78 20 6f 6e 20 3c  reach index on <
27ad5 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20  table>.**       
27ad6 20 20 20 20 6f 70 65 6e 20 61 20 77 72 69 74 65      open a write
27ad7 20 63 75 72 73 6f 72 20 6f 6e 20 74 68 65 20 3c   cursor on the <
27ad8 74 61 62 6c 65 3e 20 69 6e 64 65 78 0a 2a 2a 20  table> index.** 
27ad9 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61            open a
27ada 20 72 65 61 64 20 63 75 72 73 6f 72 20 6f 6e 20   read cursor on 
27adb 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
27adc 67 20 3c 74 61 62 6c 65 32 3e 20 69 6e 64 65 78  g <table2> index
27add 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 72  .**           tr
27ade 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72  ansfer all recor
27adf 64 73 20 66 72 6f 6d 20 74 68 65 20 72 65 61 64  ds from the read
27ae0 20 74 6f 20 74 68 65 20 77 72 69 74 65 20 63 75   to the write cu
27ae1 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  rsors.**        
27ae2 20 20 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73     close cursors
27ae3 0a 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20  .**         end 
27ae4 66 6f 72 65 61 63 68 0a 2a 2a 0a 2a 2a 20 54 68  foreach.**.** Th
27ae5 65 20 33 72 64 20 74 65 6d 70 6c 61 74 65 20 69  e 3rd template i
27ae6 73 20 66 6f 72 20 77 68 65 6e 20 74 68 65 20 73  s for when the s
27ae7 65 63 6f 6e 64 20 74 65 6d 70 6c 61 74 65 20 64  econd template d
27ae8 6f 65 73 20 6e 6f 74 20 61 70 70 6c 79 0a 2a 2a  oes not apply.**
27ae9 20 61 6e 64 20 74 68 65 20 53 45 4c 45 43 54 20   and the SELECT 
27aea 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20  clause does not 
27aeb 72 65 61 64 20 66 72 6f 6d 20 3c 74 61 62 6c 65  read from <table
27aec 3e 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a  > at any time..*
27aed 2a 20 54 68 65 20 67 65 6e 65 72 61 74 65 64 20  * The generated 
27aee 63 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 74 68 69  code follows thi
27aef 73 20 74 65 6d 70 6c 61 74 65 3a 0a 2a 2a 0a 2a  s template:.**.*
27af0 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d  *         EOF <-
27af1 20 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 58 20   0.**         X 
27af2 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20 20 20 20  <- A.**         
27af3 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41  goto B.**      A
27af4 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65 20  : setup for the 
27af5 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
27af6 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20    loop over the 
27af7 72 6f 77 73 20 69 6e 20 74 68 65 20 53 45 4c 45  rows in the SELE
27af8 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  CT.**           
27af9 6c 6f 61 64 20 76 61 6c 75 65 73 20 69 6e 74 6f  load values into
27afa 20 72 65 67 69 73 74 65 72 73 20 52 2e 2e 52 2b   registers R..R+
27afb 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79  n.**           y
27afc 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20  ield X.**       
27afd 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20    end loop.**   
27afe 20 20 20 20 20 20 63 6c 65 61 6e 75 70 20 61 66        cleanup af
27aff 74 65 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a  ter the SELECT.*
27b00 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d  *         EOF <-
27b01 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 79 69   1.**         yi
27b02 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20  eld X.**        
27b03 20 67 6f 74 6f 20 41 0a 2a 2a 20 20 20 20 20 20   goto A.**      
27b04 42 3a 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75  B: open write cu
27b05 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20  rsor to <table> 
27b06 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a  and its indices.
27b07 2a 2a 20 20 20 20 20 20 43 3a 20 79 69 65 6c 64  **      C: yield
27b08 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 66   X.**         if
27b09 20 45 4f 46 20 67 6f 74 6f 20 44 0a 2a 2a 20 20   EOF goto D.**  
27b0a 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 74 68         insert th
27b0b 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74 20  e select result 
27b0c 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72 6f  into <table> fro
27b0d 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20  m R..R+n.**     
27b0e 20 20 20 20 67 6f 74 6f 20 43 0a 2a 2a 20 20 20      goto C.**   
27b0f 20 20 20 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2a     D: cleanup.**
27b10 0a 2a 2a 20 54 68 65 20 34 74 68 20 74 65 6d 70  .** The 4th temp
27b11 6c 61 74 65 20 69 73 20 75 73 65 64 20 69 66 20  late is used if 
27b12 74 68 65 20 69 6e 73 65 72 74 20 73 74 61 74 65  the insert state
27b13 6d 65 6e 74 20 74 61 6b 65 73 20 69 74 73 0a 2a  ment takes its.*
27b14 2a 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 61 20  * values from a 
27b15 53 45 4c 45 43 54 20 62 75 74 20 74 68 65 20 64  SELECT but the d
27b16 61 74 61 20 69 73 20 62 65 69 6e 67 20 69 6e 73  ata is being ins
27b17 65 72 74 65 64 20 69 6e 74 6f 20 61 20 74 61 62  erted into a tab
27b18 6c 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 6c  le.** that is al
27b19 73 6f 20 72 65 61 64 20 61 73 20 70 61 72 74 20  so read as part 
27b1a 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 20 20  of the SELECT.  
27b1b 49 6e 20 74 68 65 20 74 68 69 72 64 20 66 6f 72  In the third for
27b1c 6d 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f  m,.** we have to
27b1d 20 75 73 65 20 61 20 69 6e 74 65 72 6d 65 64 69   use a intermedi
27b1e 61 74 65 20 74 61 62 6c 65 20 74 6f 20 73 74 6f  ate table to sto
27b1f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f  re the results o
27b20 66 0a 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 2e  f.** the select.
27b21 20 20 54 68 65 20 74 65 6d 70 6c 61 74 65 20 69    The template i
27b22 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
27b23 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c  **         EOF <
27b24 2d 20 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 58  - 0.**         X
27b25 20 3c 2d 20 41 0a 2a 2a 20 20 20 20 20 20 20 20   <- A.**        
27b26 20 67 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20   goto B.**      
27b27 41 3a 20 73 65 74 75 70 20 66 6f 72 20 74 68 65  A: setup for the
27b28 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
27b29 20 20 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65     loop over the
27b2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 53   tables in the S
27b2b 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  ELECT.**        
27b2c 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e     load value in
27b2d 74 6f 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52  to register R..R
27b2e 2b 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  +n.**           
27b2f 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20  yield X.**      
27b30 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20     end loop.**  
27b31 20 20 20 20 20 20 20 63 6c 65 61 6e 75 70 20 61         cleanup a
27b32 66 74 65 72 20 74 68 65 20 53 45 4c 45 43 54 0a  fter the SELECT.
27b33 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c  **         EOF <
27b34 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 79  - 1.**         y
27b35 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20  ield X.**       
27b36 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a 2a 2a 20    halt-error.** 
27b37 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d       B: open tem
27b38 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20  p table.**      
27b39 4c 3a 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20  L: yield X.**   
27b3a 20 20 20 20 20 20 69 66 20 45 4f 46 20 67 6f 74        if EOF got
27b3b 6f 20 4d 0a 2a 2a 20 20 20 20 20 20 20 20 20 69  o M.**         i
27b3c 6e 73 65 72 74 20 72 6f 77 20 66 72 6f 6d 20 52  nsert row from R
27b3d 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20  ..R+n into temp 
27b3e 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20  table.**        
27b3f 20 67 6f 74 6f 20 4c 0a 2a 2a 20 20 20 20 20 20   goto L.**      
27b40 4d 3a 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75  M: open write cu
27b41 72 73 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20  rsor to <table> 
27b42 61 6e 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a  and its indices.
27b43 2a 2a 20 20 20 20 20 20 20 20 20 72 65 77 69 6e  **         rewin
27b44 64 20 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20  d temp table.** 
27b45 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65       C: loop ove
27b46 72 20 72 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d  r rows of interm
27b47 65 64 69 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20  ediate table.** 
27b48 20 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66            transf
27b49 65 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69  er values form i
27b4a 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
27b4b 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a  e into <table>.*
27b4c 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f  *         end lo
27b4d 6f 70 0a 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c  op.**      D: cl
27b4e 65 61 6e 75 70 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eanup.*/.SQLITE_
27b4f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
27b50 69 74 65 33 49 6e 73 65 72 74 28 0a 20 20 50 61  ite3Insert(.  Pa
27b51 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
27b52 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f      /* Parser co
27b53 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
27b54 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20  st *pTabList,   
27b55 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c   /* Name of tabl
27b56 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20  e into which we 
27b57 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f  are inserting */
27b58 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
27b59 73 74 2c 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  st,      /* List
27b5a 20 6f 66 20 76 61 6c 75 65 73 20 74 6f 20 62 65   of values to be
27b5b 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 53   inserted */.  S
27b5c 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20  elect *pSelect, 
27b5d 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54       /* A SELECT
27b5e 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73   statement to us
27b5f 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73 6f  e as the data so
27b60 75 72 63 65 20 2a 2f 0a 20 20 49 64 4c 69 73 74  urce */.  IdList
27b61 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20   *pColumn,      
27b62 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  /* Column names 
27b63 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
27b64 20 49 44 4c 49 53 54 2e 20 2a 2f 0a 20 20 69 6e   IDLIST. */.  in
27b65 74 20 6f 6e 45 72 72 6f 72 20 20 20 20 20 20 20  t onError       
27b66 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61      /* How to ha
27b67 6e 64 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20  ndle constraint 
27b68 65 72 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20 20 73  errors */.){.  s
27b69 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
27b6a 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e       /* The main
27b6b 20 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74   database struct
27b6c 75 72 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ure */.  Table *
27b6d 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f  pTab;          /
27b6e 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20 69  * The table to i
27b6f 6e 73 65 72 74 20 69 6e 74 6f 2e 20 20 61 6b 61  nsert into.  aka
27b70 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 68 61 72   TABLE */.  char
27b71 20 2a 7a 54 61 62 3b 20 20 20 20 20 20 20 20 20   *zTab;         
27b72 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
27b73 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
27b74 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
27b75 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ng */.  const ch
27b76 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 2f 2a  ar *zDb;      /*
27b77 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
27b78 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68  abase holding th
27b79 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  is table */.  in
27b7a 74 20 69 2c 20 6a 2c 20 69 64 78 3b 20 20 20 20  t i, j, idx;    
27b7b 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
27b7c 74 65 72 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a  ters */.  Vdbe *
27b7d 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
27b7e 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
27b7f 20 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75   into this virtu
27b80 61 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20  al machine */.  
27b81 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20  Index *pIdx;    
27b82 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f        /* For loo
27b83 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65  ping over indice
27b84 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  s of the table *
27b85 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
27b86 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
27b87 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
27b88 6e 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20  n the data */.  
27b89 69 6e 74 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b  int nHidden = 0;
27b8a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
27b8b 6f 66 20 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e  of hidden column
27b8c 73 20 69 66 20 54 41 42 4c 45 20 69 73 20 76 69  s if TABLE is vi
27b8d 72 74 75 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 62  rtual */.  int b
27b8e 61 73 65 43 75 72 20 3d 20 30 3b 20 20 20 20 20  aseCur = 0;     
27b8f 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20   /* VDBE Cursor 
27b90 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 61 62 20  number for pTab 
27b91 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 43 6f 6c 75  */.  int keyColu
27b92 6d 6e 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 43 6f  mn = -1;   /* Co
27b93 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68 65  lumn that is the
27b94 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
27b95 20 4b 45 59 20 2a 2f 0a 20 20 69 6e 74 20 65 6e   KEY */.  int en
27b96 64 4f 66 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20  dOfLoop;        
27b97 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68 65  /* Label for the
27b98 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 73 65   end of the inse
27b99 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20  rtion loop */.  
27b9a 69 6e 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65  int useTempTable
27b9b 20 3d 20 30 3b 20 2f 2a 20 53 74 6f 72 65 20 53   = 0; /* Store S
27b9c 45 4c 45 43 54 20 72 65 73 75 6c 74 73 20 69 6e  ELECT results in
27b9d 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61   intermediate ta
27b9e 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63  ble */.  int src
27b9f 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  Tab = 0;       /
27ba0 2a 20 44 61 74 61 20 63 6f 6d 65 73 20 66 72 6f  * Data comes fro
27ba1 6d 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79  m this temporary
27ba2 20 63 75 72 73 6f 72 20 69 66 20 3e 3d 30 20 2a   cursor if >=0 *
27ba3 2f 0a 20 20 69 6e 74 20 61 64 64 72 49 6e 73 54  /.  int addrInsT
27ba4 6f 70 20 3d 20 30 3b 20 20 20 2f 2a 20 4a 75 6d  op = 0;   /* Jum
27ba5 70 20 74 6f 20 6c 61 62 65 6c 20 22 44 22 20 2a  p to label "D" *
27ba6 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74  /.  int addrCont
27ba7 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 6f 70   = 0;     /* Top
27ba8 20 6f 66 20 69 6e 73 65 72 74 20 6c 6f 6f 70 2e   of insert loop.
27ba9 20 4c 61 62 65 6c 20 22 43 22 20 69 6e 20 74 65   Label "C" in te
27baa 6d 70 6c 61 74 65 73 20 33 20 61 6e 64 20 34 20  mplates 3 and 4 
27bab 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c  */.  int addrSel
27bac 65 63 74 20 3d 20 30 3b 20 20 20 2f 2a 20 41 64  ect = 0;   /* Ad
27bad 64 72 65 73 73 20 6f 66 20 63 6f 72 6f 75 74 69  dress of corouti
27bae 6e 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ne that implemen
27baf 74 73 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f  ts the SELECT */
27bb0 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
27bb1 73 74 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74  st;      /* Dest
27bb2 69 6e 61 74 69 6f 6e 20 66 6f 72 20 53 45 4c 45  ination for SELE
27bb3 43 54 20 6f 6e 20 72 68 73 20 6f 66 20 49 4e 53  CT on rhs of INS
27bb4 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ERT */.  int iDb
27bb5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
27bb6 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61 62  * Index of datab
27bb7 61 73 65 20 68 6f 6c 64 69 6e 67 20 54 41 42 4c  ase holding TABL
27bb8 45 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20  E */.  Db *pDb; 
27bb9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
27bba 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
27bbb 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 62 65  taining table be
27bbc 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74  ing inserted int
27bbd 6f 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e  o */.  int appen
27bbe 64 46 6c 61 67 20 3d 20 30 3b 20 20 20 2f 2a 20  dFlag = 0;   /* 
27bbf 54 72 75 65 20 69 66 20 74 68 65 20 69 6e 73 65  True if the inse
27bc0 72 74 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20  rt is likely to 
27bc1 62 65 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a  be an append */.
27bc2 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61  .  /* Register a
27bc3 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20  llocations */.  
27bc4 69 6e 74 20 72 65 67 46 72 6f 6d 53 65 6c 65 63  int regFromSelec
27bc5 74 20 3d 20 30 3b 2f 2a 20 42 61 73 65 20 72 65  t = 0;/* Base re
27bc6 67 69 73 74 65 72 20 66 6f 72 20 64 61 74 61 20  gister for data 
27bc7 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 53 45 4c 45  coming from SELE
27bc8 43 54 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41  CT */.  int regA
27bc9 75 74 6f 69 6e 63 20 3d 20 30 3b 20 20 20 2f 2a  utoinc = 0;   /*
27bca 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
27bcb 67 20 74 68 65 20 41 55 54 4f 49 4e 43 52 45 4d  g the AUTOINCREM
27bcc 45 4e 54 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  ENT counter */. 
27bcd 20 69 6e 74 20 72 65 67 52 6f 77 43 6f 75 6e 74   int regRowCount
27bce 20 3d 20 30 3b 20 20 2f 2a 20 4d 65 6d 6f 72 79   = 0;  /* Memory
27bcf 20 63 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 74   cell used for t
27bd0 68 65 20 72 6f 77 20 63 6f 75 6e 74 65 72 20 2a  he row counter *
27bd1 2f 0a 20 20 69 6e 74 20 72 65 67 49 6e 73 3b 20  /.  int regIns; 
27bd2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f            /* Blo
27bd3 63 6b 20 6f 66 20 72 65 67 73 20 68 6f 6c 64 69  ck of regs holdi
27bd4 6e 67 20 72 6f 77 69 64 2b 64 61 74 61 20 62 65  ng rowid+data be
27bd5 69 6e 67 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a  ing inserted */.
27bd6 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20    int regRowid; 
27bd7 20 20 20 20 20 20 20 20 2f 2a 20 72 65 67 69 73          /* regis
27bd8 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 69 6e 73  ters holding ins
27bd9 65 72 74 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69  ert rowid */.  i
27bda 6e 74 20 72 65 67 44 61 74 61 3b 20 20 20 20 20  nt regData;     
27bdb 20 20 20 20 20 2f 2a 20 72 65 67 69 73 74 65 72       /* register
27bdc 20 68 6f 6c 64 69 6e 67 20 66 69 72 73 74 20 63   holding first c
27bdd 6f 6c 75 6d 6e 20 74 6f 20 69 6e 73 65 72 74 20  olumn to insert 
27bde 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 63 6f  */.  int regReco
27bdf 72 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 48 6f  rd;        /* Ho
27be0 6c 64 73 20 74 68 65 20 61 73 73 65 6d 62 6c 69  lds the assembli
27be1 65 64 20 72 6f 77 20 72 65 63 6f 72 64 20 2a 2f  ed row record */
27be2 0a 20 20 69 6e 74 20 72 65 67 45 6f 66 20 3d 20  .  int regEof = 
27be3 30 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  0;       /* Regi
27be4 73 74 65 72 20 72 65 63 6f 72 64 69 6e 67 20 65  ster recording e
27be5 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 64 61 74  nd of SELECT dat
27be6 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67  a */.  int *aReg
27be7 49 64 78 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  Idx = 0;     /* 
27be8 4f 6e 65 20 72 65 67 69 73 74 65 72 20 61 6c 6c  One register all
27be9 6f 63 61 74 65 64 20 74 6f 20 65 61 63 68 20 69  ocated to each i
27bea 6e 64 65 78 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ndex */..#ifndef
27beb 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
27bec 47 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65  GGER.  int isVie
27bed 77 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  w;              
27bee 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 74     /* True if at
27bef 74 65 6d 70 74 69 6e 67 20 74 6f 20 69 6e 73 65  tempting to inse
27bf0 72 74 20 69 6e 74 6f 20 61 20 76 69 65 77 20 2a  rt into a view *
27bf1 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  /.  Trigger *pTr
27bf2 69 67 67 65 72 3b 20 20 20 20 20 20 20 20 20 20  igger;          
27bf3 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67  /* List of trigg
27bf4 65 72 73 20 6f 6e 20 70 54 61 62 2c 20 69 66 20  ers on pTab, if 
27bf5 72 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 6e  required */.  in
27bf6 74 20 74 6d 61 73 6b 3b 20 20 20 20 20 20 20 20  t tmask;        
27bf7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73            /* Mas
27bf8 6b 20 6f 66 20 74 72 69 67 67 65 72 20 74 69 6d  k of trigger tim
27bf9 65 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20  es */.#endif..  
27bfa 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
27bfb 0a 20 20 6d 65 6d 73 65 74 28 26 64 65 73 74 2c  .  memset(&dest,
27bfc 20 30 2c 20 73 69 7a 65 6f 66 28 64 65 73 74 29   0, sizeof(dest)
27bfd 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  );.  if( pParse-
27bfe 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
27bff 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
27c00 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
27c01 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
27c02 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65  Locate the table
27c03 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 77   into which we w
27c04 69 6c 6c 20 62 65 20 69 6e 73 65 72 74 69 6e 67  ill be inserting
27c05 20 6e 65 77 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   new information
27c06 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
27c07 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d   pTabList->nSrc=
27c08 3d 31 20 29 3b 0a 20 20 7a 54 61 62 20 3d 20 70  =1 );.  zTab = p
27c09 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 7a 4e  TabList->a[0].zN
27c0a 61 6d 65 3b 0a 20 20 69 66 28 20 4e 45 56 45 52  ame;.  if( NEVER
27c0b 28 7a 54 61 62 3d 3d 30 29 20 29 20 67 6f 74 6f  (zTab==0) ) goto
27c0c 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
27c0d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
27c0e 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70  3SrcListLookup(p
27c0f 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29  Parse, pTabList)
27c10 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  ;.  if( pTab==0 
27c11 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65  ){.    goto inse
27c12 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  rt_cleanup;.  }.
27c13 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
27c14 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
27c15 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
27c16 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3c 64  .  assert( iDb<d
27c17 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 70 44 62 20  b->nDb );.  pDb 
27c18 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b  = &db->aDb[iDb];
27c19 0a 20 20 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e  .  zDb = pDb->zN
27c1a 61 6d 65 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ame;.  if( sqlit
27c1b 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
27c1c 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52  se, SQLITE_INSER
27c1d 54 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  T, pTab->zName, 
27c1e 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 67  0, zDb) ){.    g
27c1f 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e  oto insert_clean
27c20 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69  up;.  }..  /* Fi
27c21 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 68  gure out if we h
27c22 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72 73  ave any triggers
27c23 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c   and if the tabl
27c24 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 69 6e 73  e being.  ** ins
27c25 65 72 74 65 64 20 69 6e 74 6f 20 69 73 20 61 20  erted into is a 
27c26 76 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  view.  */.#ifnde
27c27 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
27c28 49 47 47 45 52 0a 20 20 70 54 72 69 67 67 65 72  IGGER.  pTrigger
27c29 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
27c2a 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
27c2b 70 54 61 62 2c 20 54 4b 5f 49 4e 53 45 52 54 2c  pTab, TK_INSERT,
27c2c 20 30 2c 20 26 74 6d 61 73 6b 29 3b 0a 20 20 69   0, &tmask);.  i
27c2d 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e 70 53  sView = pTab->pS
27c2e 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73 65 0a  elect!=0;.#else.
27c2f 23 20 64 65 66 69 6e 65 20 70 54 72 69 67 67 65  # define pTrigge
27c30 72 20 30 0a 23 20 64 65 66 69 6e 65 20 74 6d 61  r 0.# define tma
27c31 73 6b 20 30 0a 23 20 64 65 66 69 6e 65 20 69 73  sk 0.# define is
27c32 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23 69  View 0.#endif.#i
27c33 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
27c34 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69 73  _VIEW.# undef is
27c35 56 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69 73  View.# define is
27c36 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 20 20  View 0.#endif.  
27c37 61 73 73 65 72 74 28 20 28 70 54 72 69 67 67 65  assert( (pTrigge
27c38 72 20 26 26 20 74 6d 61 73 6b 29 20 7c 7c 20 28  r && tmask) || (
27c39 70 54 72 69 67 67 65 72 3d 3d 30 20 26 26 20 74  pTrigger==0 && t
27c3a 6d 61 73 6b 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f  mask==0) );..  /
27c3b 2a 20 49 66 20 70 54 61 62 20 69 73 20 72 65 61  * If pTab is rea
27c3c 6c 6c 79 20 61 20 76 69 65 77 2c 20 6d 61 6b 65  lly a view, make
27c3d 20 73 75 72 65 20 69 74 20 68 61 73 20 62 65 65   sure it has bee
27c3e 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20  n initialized.. 
27c3f 20 2a 2a 20 56 69 65 77 47 65 74 43 6f 6c 75 6d   ** ViewGetColum
27c40 6e 4e 61 6d 65 73 28 29 20 69 73 20 61 20 6e 6f  nNames() is a no
27c41 2d 6f 70 20 69 66 20 70 54 61 62 20 69 73 20 6e  -op if pTab is n
27c42 6f 74 20 61 20 76 69 65 77 20 28 6f 72 20 76 69  ot a view (or vi
27c43 72 74 75 61 6c 20 0a 20 20 2a 2a 20 6d 6f 64 75  rtual .  ** modu
27c44 6c 65 20 74 61 62 6c 65 29 2e 0a 20 20 2a 2f 0a  le table)..  */.
27c45 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65    if( sqlite3Vie
27c46 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
27c47 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b  pParse, pTab) ){
27c48 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
27c49 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
27c4a 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 61 74 3a   /* Ensure that:
27c4b 0a 20 20 2a 20 20 28 61 29 20 74 68 65 20 74 61  .  *  (a) the ta
27c4c 62 6c 65 20 69 73 20 6e 6f 74 20 72 65 61 64 2d  ble is not read-
27c4d 6f 6e 6c 79 2c 20 0a 20 20 2a 20 20 28 62 29 20  only, .  *  (b) 
27c4e 74 68 61 74 20 69 66 20 69 74 20 69 73 20 61 20  that if it is a 
27c4f 76 69 65 77 20 74 68 65 6e 20 4f 4e 20 49 4e 53  view then ON INS
27c50 45 52 54 20 74 72 69 67 67 65 72 73 20 65 78 69  ERT triggers exi
27c51 73 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  st.  */.  if( sq
27c52 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28  lite3IsReadOnly(
27c53 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 74 6d  pParse, pTab, tm
27c54 61 73 6b 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ask) ){.    goto
27c55 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
27c56 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
27c57 61 74 65 20 61 20 56 44 42 45 0a 20 20 2a 2f 0a  ate a VDBE.  */.
27c58 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
27c59 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
27c5a 69 66 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20  if( v==0 ) goto 
27c5b 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
27c5c 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
27c5d 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69 74 65  sted==0 ) sqlite
27c5e 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e 67 65  3VdbeCountChange
27c5f 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  s(v);.  sqlite3B
27c60 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
27c61 6f 6e 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65  on(pParse, pSele
27c62 63 74 20 7c 7c 20 70 54 72 69 67 67 65 72 2c 20  ct || pTrigger, 
27c63 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  iDb);..#ifndef S
27c64 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f  QLITE_OMIT_XFER_
27c65 4f 50 54 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  OPT.  /* If the 
27c66 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20  statement is of 
27c67 74 68 65 20 66 6f 72 6d 0a 20 20 2a 2a 0a 20 20  the form.  **.  
27c68 2a 2a 20 20 20 20 20 20 20 49 4e 53 45 52 54 20  **       INSERT 
27c69 49 4e 54 4f 20 3c 74 61 62 6c 65 31 3e 20 53 45  INTO <table1> SE
27c6a 4c 45 43 54 20 2a 20 46 52 4f 4d 20 3c 74 61 62  LECT * FROM <tab
27c6b 6c 65 32 3e 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20  le2>;.  **.  ** 
27c6c 54 68 65 6e 20 73 70 65 63 69 61 6c 20 6f 70 74  Then special opt
27c6d 69 6d 69 7a 61 74 69 6f 6e 73 20 63 61 6e 20 62  imizations can b
27c6e 65 20 61 70 70 6c 69 65 64 20 74 68 61 74 20 6d  e applied that m
27c6f 61 6b 65 20 74 68 65 20 74 72 61 6e 73 66 65 72  ake the transfer
27c70 0a 20 20 2a 2a 20 76 65 72 79 20 66 61 73 74 20  .  ** very fast 
27c71 61 6e 64 20 77 68 69 63 68 20 72 65 64 75 63 65  and which reduce
27c72 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 6f   fragmentation o
27c73 66 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a  f indices..  **.
27c74 20 20 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65    ** This is the
27c75 20 32 6e 64 20 74 65 6d 70 6c 61 74 65 2e 0a 20   2nd template.. 
27c76 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
27c77 6e 3d 3d 30 20 26 26 20 78 66 65 72 4f 70 74 69  n==0 && xferOpti
27c78 6d 69 7a 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  mization(pParse,
27c79 20 70 54 61 62 2c 20 70 53 65 6c 65 63 74 2c 20   pTab, pSelect, 
27c7a 6f 6e 45 72 72 6f 72 2c 20 69 44 62 29 20 29 7b  onError, iDb) ){
27c7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70 54  .    assert( !pT
27c7c 72 69 67 67 65 72 20 29 3b 0a 20 20 20 20 61 73  rigger );.    as
27c7d 73 65 72 74 28 20 70 4c 69 73 74 3d 3d 30 20 29  sert( pList==0 )
27c7e 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72  ;.    goto inser
27c7f 74 5f 65 6e 64 3b 0a 20 20 7d 0a 23 65 6e 64 69  t_end;.  }.#endi
27c80 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
27c81 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a 20 20  _XFER_OPT */..  
27c82 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
27c83 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74   AUTOINCREMENT t
27c84 61 62 6c 65 2c 20 6c 6f 6f 6b 20 75 70 20 74 68  able, look up th
27c85 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65  e sequence numbe
27c86 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71  r in the.  ** sq
27c87 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
27c88 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 20 69 74  ble and store it
27c89 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20   in memory cell 
27c8a 72 65 67 41 75 74 6f 69 6e 63 2e 0a 20 20 2a 2f  regAutoinc..  */
27c8b 0a 20 20 72 65 67 41 75 74 6f 69 6e 63 20 3d 20  .  regAutoinc = 
27c8c 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 70 50 61  autoIncBegin(pPa
27c8d 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 29 3b  rse, iDb, pTab);
27c8e 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75  ..  /* Figure ou
27c8f 74 20 68 6f 77 20 6d 61 6e 79 20 63 6f 6c 75 6d  t how many colum
27c90 6e 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 73  ns of data are s
27c91 75 70 70 6c 69 65 64 2e 20 20 49 66 20 74 68 65  upplied.  If the
27c92 20 64 61 74 61 0a 20 20 2a 2a 20 69 73 20 63 6f   data.  ** is co
27c93 6d 69 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45  ming from a SELE
27c94 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  CT statement, th
27c95 65 6e 20 67 65 6e 65 72 61 74 65 20 61 20 63 6f  en generate a co
27c96 2d 72 6f 75 74 69 6e 65 20 74 68 61 74 0a 20 20  -routine that.  
27c97 2a 2a 20 70 72 6f 64 75 63 65 73 20 61 20 73 69  ** produces a si
27c98 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20  ngle row of the 
27c99 53 45 4c 45 43 54 20 6f 6e 20 65 61 63 68 20 69  SELECT on each i
27c9a 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 0a  nvocation.  The.
27c9b 20 20 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 20    ** co-routine 
27c9c 69 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65  is the common he
27c9d 61 64 65 72 20 74 6f 20 74 68 65 20 33 72 64 20  ader to the 3rd 
27c9e 61 6e 64 20 34 74 68 20 74 65 6d 70 6c 61 74 65  and 4th template
27c9f 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  s..  */.  if( pS
27ca0 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20  elect ){.    /* 
27ca1 44 61 74 61 20 69 73 20 63 6f 6d 69 6e 67 20 66  Data is coming f
27ca2 72 6f 6d 20 61 20 53 45 4c 45 43 54 2e 20 20 47  rom a SELECT.  G
27ca3 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
27ca4 69 6d 70 6c 65 6d 65 6e 74 20 74 68 61 74 20 53  implement that S
27ca5 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 61 73 20  ELECT.    ** as 
27ca6 61 20 63 6f 2d 72 6f 75 74 69 6e 65 2e 20 20 54  a co-routine.  T
27ca7 68 65 20 63 6f 64 65 20 69 73 20 63 6f 6d 6d 6f  he code is commo
27ca8 6e 20 74 6f 20 62 6f 74 68 20 74 68 65 20 33 72  n to both the 3r
27ca9 64 20 61 6e 64 20 34 74 68 0a 20 20 20 20 2a 2a  d and 4th.    **
27caa 20 74 65 6d 70 6c 61 74 65 73 3a 0a 20 20 20 20   templates:.    
27cab 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
27cac 20 20 45 4f 46 20 3c 2d 20 30 0a 20 20 20 20 2a    EOF <- 0.    *
27cad 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d 20 41  *         X <- A
27cae 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
27caf 67 6f 74 6f 20 42 0a 20 20 20 20 2a 2a 20 20 20  goto B.    **   
27cb0 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20     A: setup for 
27cb1 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a  the SELECT.    *
27cb2 2a 20 20 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f  *         loop o
27cb3 76 65 72 20 74 68 65 20 74 61 62 6c 65 73 20 69  ver the tables i
27cb4 6e 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20  n the SELECT.   
27cb5 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6c 6f   **           lo
27cb6 61 64 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65  ad value into re
27cb7 67 69 73 74 65 72 20 52 2e 2e 52 2b 6e 0a 20 20  gister R..R+n.  
27cb8 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79    **           y
27cb9 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20 20 20  ield X.    **   
27cba 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20        end loop. 
27cbb 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6c     **         cl
27cbc 65 61 6e 75 70 20 61 66 74 65 72 20 74 68 65 20  eanup after the 
27cbd 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 20 20  SELECT.    **   
27cbe 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31 0a 20        EOF <- 1. 
27cbf 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 79 69     **         yi
27cc0 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20  eld X.    **    
27cc1 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a       halt-error.
27cc2 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e      **.    ** On
27cc3 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e   each invocation
27cc4 20 6f 66 20 74 68 65 20 63 6f 2d 72 6f 75 74 69   of the co-routi
27cc5 6e 65 2c 20 69 74 20 70 75 74 73 20 61 20 73 69  ne, it puts a si
27cc6 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 0a  ngle row of the.
27cc7 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 72 65      ** SELECT re
27cc8 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74  sult into regist
27cc9 65 72 73 20 64 65 73 74 2e 69 4d 65 6d 2e 2e 2e  ers dest.iMem...
27cca 64 65 73 74 2e 69 4d 65 6d 2b 64 65 73 74 2e 6e  dest.iMem+dest.n
27ccb 4d 65 6d 2d 31 2e 0a 20 20 20 20 2a 2a 20 28 54  Mem-1..    ** (T
27ccc 68 65 73 65 20 6f 75 74 70 75 74 20 72 65 67 69  hese output regi
27ccd 73 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 63 61  sters are alloca
27cce 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 53 65  ted by sqlite3Se
27ccf 6c 65 63 74 28 29 2e 29 20 20 57 68 65 6e 0a 20  lect().)  When. 
27cd0 20 20 20 2a 2a 20 74 68 65 20 53 45 4c 45 43 54     ** the SELECT
27cd1 20 63 6f 6d 70 6c 65 74 65 73 2c 20 69 74 20 73   completes, it s
27cd2 65 74 73 20 74 68 65 20 45 4f 46 20 66 6c 61 67  ets the EOF flag
27cd3 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 45 6f   stored in regEo
27cd4 66 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  f..    */.    in
27cd5 74 20 72 63 2c 20 6a 31 3b 0a 0a 20 20 20 20 72  t rc, j1;..    r
27cd6 65 67 45 6f 66 20 3d 20 2b 2b 70 50 61 72 73 65  egEof = ++pParse
27cd7 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  ->nMem;.    sqli
27cd8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27cd9 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
27cda 72 65 67 45 6f 66 29 3b 20 20 20 20 20 20 2f 2a  regEof);      /*
27cdb 20 45 4f 46 20 3c 2d 20 30 20 2a 2f 0a 20 20 20   EOF <- 0 */.   
27cdc 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
27cdd 20 22 53 45 4c 45 43 54 20 65 6f 66 20 66 6c 61   "SELECT eof fla
27cde 67 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  g"));.    sqlite
27cdf 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
27ce0 26 64 65 73 74 2c 20 53 52 54 5f 43 6f 72 6f 75  &dest, SRT_Corou
27ce1 74 69 6e 65 2c 20 2b 2b 70 50 61 72 73 65 2d 3e  tine, ++pParse->
27ce2 6e 4d 65 6d 29 3b 0a 20 20 20 20 61 64 64 72 53  nMem);.    addrS
27ce3 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 56  elect = sqlite3V
27ce4 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
27ce5 29 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  )+2;.    sqlite3
27ce6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27ce7 5f 49 6e 74 65 67 65 72 2c 20 61 64 64 72 53 65  _Integer, addrSe
27ce8 6c 65 63 74 2d 31 2c 20 64 65 73 74 2e 69 50 61  lect-1, dest.iPa
27ce9 72 6d 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71  rm);.    j1 = sq
27cea 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27ceb 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30  v, OP_Goto, 0, 0
27cec 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
27ced 6e 74 28 28 76 2c 20 22 4a 75 6d 70 20 6f 76 65  nt((v, "Jump ove
27cee 72 20 53 45 4c 45 43 54 20 63 6f 72 6f 75 74 69  r SELECT corouti
27cef 6e 65 22 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 52  ne"));..    /* R
27cf0 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72 65  esolve the expre
27cf1 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45  ssions in the SE
27cf2 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61  LECT statement a
27cf3 6e 64 20 65 78 65 63 75 74 65 20 69 74 2e 20 2a  nd execute it. *
27cf4 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
27cf5 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
27cf6 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29   pSelect, &dest)
27cf7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
27cf8 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 7c 7c  arse->nErr==0 ||
27cf9 20 72 63 20 29 3b 0a 20 20 20 20 69 66 28 20 72   rc );.    if( r
27cfa 63 20 7c 7c 20 4e 45 56 45 52 28 70 50 61 72 73  c || NEVER(pPars
27cfb 65 2d 3e 6e 45 72 72 29 20 7c 7c 20 64 62 2d 3e  e->nErr) || db->
27cfc 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
27cfd 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72        goto inser
27cfe 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d  t_cleanup;.    }
27cff 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27d00 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
27d01 65 67 65 72 2c 20 31 2c 20 72 65 67 45 6f 66 29  eger, 1, regEof)
27d02 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 45 4f 46  ;         /* EOF
27d03 20 3c 2d 20 31 20 2a 2f 0a 20 20 20 20 73 71 6c   <- 1 */.    sql
27d04 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
27d05 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73 74  , OP_Yield, dest
27d06 2e 69 50 61 72 6d 29 3b 20 20 20 2f 2a 20 79 69  .iParm);   /* yi
27d07 65 6c 64 20 58 20 2a 2f 0a 20 20 20 20 73 71 6c  eld X */.    sql
27d08 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27d09 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54  , OP_Halt, SQLIT
27d0a 45 5f 49 4e 54 45 52 4e 41 4c 2c 20 4f 45 5f 41  E_INTERNAL, OE_A
27d0b 62 6f 72 74 29 3b 0a 20 20 20 20 56 64 62 65 43  bort);.    VdbeC
27d0c 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e 64 20  omment((v, "End 
27d0d 6f 66 20 53 45 4c 45 43 54 20 63 6f 72 6f 75 74  of SELECT corout
27d0e 69 6e 65 22 29 29 3b 0a 20 20 20 20 73 71 6c 69  ine"));.    sqli
27d0f 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
27d10 76 2c 20 6a 31 29 3b 20 20 20 20 20 20 20 20 20  v, j1);         
27d11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27d12 20 2f 2a 20 6c 61 62 65 6c 20 42 3a 20 2a 2f 0a   /* label B: */.
27d13 0a 20 20 20 20 72 65 67 46 72 6f 6d 53 65 6c 65  .    regFromSele
27d14 63 74 20 3d 20 64 65 73 74 2e 69 4d 65 6d 3b 0a  ct = dest.iMem;.
27d15 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c      assert( pSel
27d16 65 63 74 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20  ect->pEList );. 
27d17 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 53 65     nColumn = pSe
27d18 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  lect->pEList->nE
27d19 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  xpr;.    assert(
27d1a 20 64 65 73 74 2e 6e 4d 65 6d 3d 3d 6e 43 6f 6c   dest.nMem==nCol
27d1b 75 6d 6e 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53  umn );..    /* S
27d1c 65 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  et useTempTable 
27d1d 74 6f 20 54 52 55 45 20 69 66 20 74 68 65 20 72  to TRUE if the r
27d1e 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53 45 4c  esult of the SEL
27d1f 45 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20  ECT statement.  
27d20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 77    ** should be w
27d21 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 74 65  ritten into a te
27d22 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 28 74  mporary table (t
27d23 65 6d 70 6c 61 74 65 20 34 29 2e 20 20 53 65 74  emplate 4).  Set
27d24 20 74 6f 0a 20 20 20 20 2a 2a 20 46 41 4c 53 45   to.    ** FALSE
27d25 20 69 66 20 65 61 63 68 2a 20 72 6f 77 20 6f 66   if each* row of
27d26 20 74 68 65 20 53 45 4c 45 43 54 20 63 61 6e 20   the SELECT can 
27d27 62 65 20 77 72 69 74 74 65 6e 20 64 69 72 65 63  be written direc
27d28 74 6c 79 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20  tly into.    ** 
27d29 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
27d2a 74 61 62 6c 65 20 28 74 65 6d 70 6c 61 74 65 20  table (template 
27d2b 33 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  3)..    **.    *
27d2c 2a 20 41 20 74 65 6d 70 20 74 61 62 6c 65 20 6d  * A temp table m
27d2d 75 73 74 20 62 65 20 75 73 65 64 20 69 66 20 74  ust be used if t
27d2e 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 75  he table being u
27d2f 70 64 61 74 65 64 20 69 73 20 61 6c 73 6f 20 6f  pdated is also o
27d30 6e 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  ne.    ** of the
27d31 20 74 61 62 6c 65 73 20 62 65 69 6e 67 20 72 65   tables being re
27d32 61 64 20 62 79 20 74 68 65 20 53 45 4c 45 43 54  ad by the SELECT
27d33 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 6c 73   statement.  Als
27d34 6f 20 75 73 65 20 61 20 0a 20 20 20 20 2a 2a 20  o use a .    ** 
27d35 74 65 6d 70 20 74 61 62 6c 65 20 69 6e 20 74 68  temp table in th
27d36 65 20 63 61 73 65 20 6f 66 20 72 6f 77 20 74 72  e case of row tr
27d37 69 67 67 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20  iggers..    */. 
27d38 20 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20     if( pTrigger 
27d39 7c 7c 20 72 65 61 64 73 54 61 62 6c 65 28 70 50  || readsTable(pP
27d3a 61 72 73 65 2c 20 61 64 64 72 53 65 6c 65 63 74  arse, addrSelect
27d3b 2c 20 69 44 62 2c 20 70 54 61 62 29 20 29 7b 0a  , iDb, pTab) ){.
27d3c 20 20 20 20 20 20 75 73 65 54 65 6d 70 54 61 62        useTempTab
27d3d 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20  le = 1;.    }.. 
27d3e 20 20 20 69 66 28 20 75 73 65 54 65 6d 70 54 61     if( useTempTa
27d3f 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ble ){.      /* 
27d40 49 6e 76 6f 6b 65 20 74 68 65 20 63 6f 72 6f 75  Invoke the corou
27d41 74 69 6e 65 20 74 6f 20 65 78 74 72 61 63 74 20  tine to extract 
27d42 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
27d43 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20   the SELECT.    
27d44 20 20 2a 2a 20 61 6e 64 20 61 64 64 20 69 74 20    ** and add it 
27d45 74 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74  to a transient t
27d46 61 62 6c 65 20 73 72 63 54 61 62 2e 20 20 54 68  able srcTab.  Th
27d47 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  e code generated
27d48 0a 20 20 20 20 20 20 2a 2a 20 68 65 72 65 20 69  .      ** here i
27d49 73 20 66 72 6f 6d 20 74 68 65 20 34 74 68 20 74  s from the 4th t
27d4a 65 6d 70 6c 61 74 65 3a 0a 20 20 20 20 20 20 2a  emplate:.      *
27d4b 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20  *.      **      
27d4c 42 3a 20 6f 70 65 6e 20 74 65 6d 70 20 74 61 62  B: open temp tab
27d4d 6c 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  le.      **     
27d4e 20 4c 3a 20 79 69 65 6c 64 20 58 0a 20 20 20 20   L: yield X.    
27d4f 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20    **         if 
27d50 45 4f 46 20 67 6f 74 6f 20 4d 0a 20 20 20 20 20  EOF goto M.     
27d51 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65   **         inse
27d52 72 74 20 72 6f 77 20 66 72 6f 6d 20 52 2e 2e 52  rt row from R..R
27d53 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20 74 61 62  +n into temp tab
27d54 6c 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  le.      **     
27d55 20 20 20 20 67 6f 74 6f 20 4c 0a 20 20 20 20 20      goto L.     
27d56 20 2a 2a 20 20 20 20 20 20 4d 3a 20 2e 2e 2e 0a   **      M: ....
27d57 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
27d58 6e 74 20 72 65 67 52 65 63 3b 20 20 20 20 20 20  nt regRec;      
27d59 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
27d5a 74 6f 20 68 6f 6c 64 20 70 61 63 6b 65 64 20 72  to hold packed r
27d5b 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69  ecord */.      i
27d5c 6e 74 20 72 65 67 54 65 6d 70 52 6f 77 69 64 3b  nt regTempRowid;
27d5d 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
27d5e 74 6f 20 68 6f 6c 64 20 74 65 6d 70 20 74 61 62  to hold temp tab
27d5f 6c 65 20 52 4f 57 49 44 20 2a 2f 0a 20 20 20 20  le ROWID */.    
27d60 20 20 69 6e 74 20 61 64 64 72 54 6f 70 3b 20 20    int addrTop;  
27d61 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20         /* Label 
27d62 22 4c 22 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  "L" */.      int
27d63 20 61 64 64 72 49 66 3b 20 20 20 20 20 20 20 20   addrIf;        
27d64 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
27d65 6a 75 6d 70 20 74 6f 20 4d 20 2a 2f 0a 0a 20 20  jump to M */..  
27d66 20 20 20 20 73 72 63 54 61 62 20 3d 20 70 50 61      srcTab = pPa
27d67 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
27d68 20 20 20 72 65 67 52 65 63 20 3d 20 73 71 6c 69     regRec = sqli
27d69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
27d6a 61 72 73 65 29 3b 0a 20 20 20 20 20 20 72 65 67  arse);.      reg
27d6b 54 65 6d 70 52 6f 77 69 64 20 3d 20 73 71 6c 69  TempRowid = sqli
27d6c 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
27d6d 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  arse);.      sql
27d6e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27d6f 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
27d70 61 6c 2c 20 73 72 63 54 61 62 2c 20 6e 43 6f 6c  al, srcTab, nCol
27d71 75 6d 6e 29 3b 0a 20 20 20 20 20 20 61 64 64 72  umn);.      addr
27d72 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Top = sqlite3Vdb
27d73 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69  eAddOp1(v, OP_Yi
27d74 65 6c 64 2c 20 64 65 73 74 2e 69 50 61 72 6d 29  eld, dest.iParm)
27d75 3b 0a 20 20 20 20 20 20 61 64 64 72 49 66 20 3d  ;.      addrIf =
27d76 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27d77 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67  p1(v, OP_If, reg
27d78 45 6f 66 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Eof);.      sqli
27d79 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
27d7a 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
27d7b 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 2c 20 6e  regFromSelect, n
27d7c 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 65 63 29 3b  Column, regRec);
27d7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
27d7e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
27d7f 65 77 52 6f 77 69 64 2c 20 73 72 63 54 61 62 2c  ewRowid, srcTab,
27d80 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29 3b 0a   regTempRowid);.
27d81 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27d82 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
27d83 73 65 72 74 2c 20 73 72 63 54 61 62 2c 20 72 65  sert, srcTab, re
27d84 67 52 65 63 2c 20 72 65 67 54 65 6d 70 52 6f 77  gRec, regTempRow
27d85 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
27d86 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27d87 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72  OP_Goto, 0, addr
27d88 54 6f 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Top);.      sqli
27d89 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
27d8a 76 2c 20 61 64 64 72 49 66 29 3b 0a 20 20 20 20  v, addrIf);.    
27d8b 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
27d8c 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
27d8d 72 65 67 52 65 63 29 3b 0a 20 20 20 20 20 20 73  regRec);.      s
27d8e 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
27d8f 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
27d90 54 65 6d 70 52 6f 77 69 64 29 3b 0a 20 20 20 20  TempRowid);.    
27d91 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  }.  }else{.    /
27d92 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 63 61  * This is the ca
27d93 73 65 20 69 66 20 74 68 65 20 64 61 74 61 20 66  se if the data f
27d94 6f 72 20 74 68 65 20 49 4e 53 45 52 54 20 69 73  or the INSERT is
27d95 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56   coming from a V
27d96 41 4c 55 45 53 0a 20 20 20 20 2a 2a 20 63 6c 61  ALUES.    ** cla
27d97 75 73 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 4e  use.    */.    N
27d98 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a  ameContext sNC;.
27d99 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c      memset(&sNC,
27d9a 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
27d9b 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65  ;.    sNC.pParse
27d9c 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73   = pParse;.    s
27d9d 72 63 54 61 62 20 3d 20 2d 31 3b 0a 20 20 20 20  rcTab = -1;.    
27d9e 61 73 73 65 72 74 28 20 75 73 65 54 65 6d 70 54  assert( useTempT
27d9f 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 6e  able==0 );.    n
27da0 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69 73 74 20 3f  Column = pList ?
27da1 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20   pList->nExpr : 
27da2 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
27da3 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  i<nColumn; i++){
27da4 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
27da5 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
27da6 65 73 28 26 73 4e 43 2c 20 70 4c 69 73 74 2d 3e  es(&sNC, pList->
27da7 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20  a[i].pExpr) ){. 
27da8 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65         goto inse
27da9 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  rt_cleanup;.    
27daa 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
27dab 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
27dac 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
27dad 6d 6e 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63  mns in the sourc
27dae 65 20 64 61 74 61 20 6d 61 74 63 68 65 73 20 74  e data matches t
27daf 68 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 6f  he number.  ** o
27db0 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20  f columns to be 
27db1 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 74 68  inserted into th
27db2 65 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  e table..  */.  
27db3 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
27db4 61 62 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ab) ){.    for(i
27db5 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
27db6 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 48  ; i++){.      nH
27db7 69 64 64 65 6e 20 2b 3d 20 28 49 73 48 69 64 64  idden += (IsHidd
27db8 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e  enColumn(&pTab->
27db9 61 43 6f 6c 5b 69 5d 29 20 3f 20 31 20 3a 20 30  aCol[i]) ? 1 : 0
27dba 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  );.    }.  }.  i
27dbb 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26  f( pColumn==0 &&
27dbc 20 6e 43 6f 6c 75 6d 6e 20 26 26 20 6e 43 6f 6c   nColumn && nCol
27dbd 75 6d 6e 21 3d 28 70 54 61 62 2d 3e 6e 43 6f 6c  umn!=(pTab->nCol
27dbe 2d 6e 48 69 64 64 65 6e 29 20 29 7b 0a 20 20 20  -nHidden) ){.   
27dbf 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
27dc0 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
27dc1 20 22 74 61 62 6c 65 20 25 53 20 68 61 73 20 25   "table %S has %
27dc2 64 20 63 6f 6c 75 6d 6e 73 20 62 75 74 20 25 64  d columns but %d
27dc3 20 76 61 6c 75 65 73 20 77 65 72 65 20 73 75 70   values were sup
27dc4 70 6c 69 65 64 22 2c 0a 20 20 20 20 20 20 20 70  plied",.       p
27dc5 54 61 62 4c 69 73 74 2c 20 30 2c 20 70 54 61 62  TabList, 0, pTab
27dc6 2d 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e 2c 20  ->nCol-nHidden, 
27dc7 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 67 6f  nColumn);.    go
27dc8 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
27dc9 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f  p;.  }.  if( pCo
27dca 6c 75 6d 6e 21 3d 30 20 26 26 20 6e 43 6f 6c 75  lumn!=0 && nColu
27dcb 6d 6e 21 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64  mn!=pColumn->nId
27dcc 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
27dcd 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
27dce 22 25 64 20 76 61 6c 75 65 73 20 66 6f 72 20 25  "%d values for %
27dcf 64 20 63 6f 6c 75 6d 6e 73 22 2c 20 6e 43 6f 6c  d columns", nCol
27dd0 75 6d 6e 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 6e 49  umn, pColumn->nI
27dd1 64 29 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73  d);.    goto ins
27dd2 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ert_cleanup;.  }
27dd3 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 49 4e  ..  /* If the IN
27dd4 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 20 69  SERT statement i
27dd5 6e 63 6c 75 64 65 64 20 61 6e 20 49 44 4c 49 53  ncluded an IDLIS
27dd6 54 20 74 65 72 6d 2c 20 74 68 65 6e 20 6d 61 6b  T term, then mak
27dd7 65 20 73 75 72 65 0a 20 20 2a 2a 20 61 6c 6c 20  e sure.  ** all 
27dd8 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
27dd9 49 44 4c 49 53 54 20 72 65 61 6c 6c 79 20 61 72  IDLIST really ar
27dda 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  e columns of the
27ddb 20 74 61 62 6c 65 20 61 6e 64 20 0a 20 20 2a 2a   table and .  **
27ddc 20 72 65 6d 65 6d 62 65 72 20 74 68 65 20 63 6f   remember the co
27ddd 6c 75 6d 6e 20 69 6e 64 69 63 65 73 2e 0a 20 20  lumn indices..  
27dde 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 74  **.  ** If the t
27ddf 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45  able has an INTE
27de0 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
27de1 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 61 74 20  column and that 
27de2 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 69 73 20 6e  column.  ** is n
27de3 61 6d 65 64 20 69 6e 20 74 68 65 20 49 44 4c 49  amed in the IDLI
27de4 53 54 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 20  ST, then record 
27de5 69 6e 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e  in the keyColumn
27de6 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a 20 74   variable.  ** t
27de7 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 49 44  he index into ID
27de8 4c 49 53 54 20 6f 66 20 74 68 65 20 70 72 69 6d  LIST of the prim
27de9 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d 6e 2e 20  ary key column. 
27dea 20 6b 65 79 43 6f 6c 75 6d 6e 20 69 73 0a 20 20   keyColumn is.  
27deb 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  ** the index of 
27dec 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20  the primary key 
27ded 61 73 20 69 74 20 61 70 70 65 61 72 73 20 69 6e  as it appears in
27dee 20 49 44 4c 49 53 54 2c 20 6e 6f 74 20 61 73 0a   IDLIST, not as.
27def 20 20 2a 2a 20 69 73 20 61 70 70 65 61 72 73 20    ** is appears 
27df0 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  in the original 
27df1 74 61 62 6c 65 2e 20 20 28 54 68 65 20 69 6e 64  table.  (The ind
27df2 65 78 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72  ex of the primar
27df3 79 0a 20 20 2a 2a 20 6b 65 79 20 69 6e 20 74 68  y.  ** key in th
27df4 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62 6c 65  e original table
27df5 20 69 73 20 70 54 61 62 2d 3e 69 50 4b 65 79 2e   is pTab->iPKey.
27df6 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f  ).  */.  if( pCo
27df7 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 66 6f 72 28  lumn ){.    for(
27df8 69 3d 30 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e  i=0; i<pColumn->
27df9 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  nId; i++){.     
27dfa 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69   pColumn->a[i].i
27dfb 64 78 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  dx = -1;.    }. 
27dfc 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43     for(i=0; i<pC
27dfd 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29  olumn->nId; i++)
27dfe 7b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  {.      for(j=0;
27dff 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a   j<pTab->nCol; j
27e00 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
27e01 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
27e02 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e  pColumn->a[i].zN
27e03 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ame, pTab->aCol[
27e04 6a 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  j].zName)==0 ){.
27e05 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 75 6d            pColum
27e06 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20 3d 20 6a 3b  n->a[i].idx = j;
27e07 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6a  .          if( j
27e08 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b  ==pTab->iPKey ){
27e09 0a 20 20 20 20 20 20 20 20 20 20 20 20 6b 65 79  .            key
27e0a 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20 20 20 20  Column = i;.    
27e0b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
27e0c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
27e0d 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
27e0e 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43   if( j>=pTab->nC
27e0f 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ol ){.        if
27e10 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64  ( sqlite3IsRowid
27e11 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a  (pColumn->a[i].z
27e12 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20  Name) ){.       
27e13 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69     keyColumn = i
27e14 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
27e15 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
27e16 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
27e17 65 2c 20 22 74 61 62 6c 65 20 25 53 20 68 61 73  e, "table %S has
27e18 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64   no column named
27e19 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %s",.          
27e1a 20 20 20 20 70 54 61 62 4c 69 73 74 2c 20 30 2c      pTabList, 0,
27e1b 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a   pColumn->a[i].z
27e1c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Name);.         
27e1d 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b   pParse->nErr++;
27e1e 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
27e1f 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
27e20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
27e21 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
27e22 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
27e23 20 49 44 4c 49 53 54 20 74 65 72 6d 20 62 75 74   IDLIST term but
27e24 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61   the table has a
27e25 6e 20 69 6e 74 65 67 65 72 20 70 72 69 6d 61 72  n integer primar
27e26 79 0a 20 20 2a 2a 20 6b 65 79 2c 20 74 68 65 20  y.  ** key, the 
27e27 73 65 74 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d  set the keyColum
27e28 6e 20 76 61 72 69 61 62 6c 65 20 74 6f 20 74 68  n variable to th
27e29 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 63 6f  e primary key co
27e2a 6c 75 6d 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20  lumn index.  ** 
27e2b 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  in the original 
27e2c 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e  table definition
27e2d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f  ..  */.  if( pCo
27e2e 6c 75 6d 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c 75  lumn==0 && nColu
27e2f 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 6b 65 79 43  mn>0 ){.    keyC
27e30 6f 6c 75 6d 6e 20 3d 20 70 54 61 62 2d 3e 69 50  olumn = pTab->iP
27e31 4b 65 79 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20  Key;.  }.    .  
27e32 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
27e33 65 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20  e count of rows 
27e34 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 0a 20  to be inserted. 
27e35 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c   */.  if( db->fl
27e36 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75  ags & SQLITE_Cou
27e37 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20 72 65  ntRows ){.    re
27e38 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50  gRowCount = ++pP
27e39 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
27e3a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27e3b 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
27e3c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 29   0, regRowCount)
27e3d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
27e3e 68 69 73 20 69 73 20 6e 6f 74 20 61 20 76 69 65  his is not a vie
27e3f 77 2c 20 6f 70 65 6e 20 74 68 65 20 74 61 62 6c  w, open the tabl
27e40 65 20 61 6e 64 20 61 6e 64 20 61 6c 6c 20 69 6e  e and and all in
27e41 64 69 63 65 73 20 2a 2f 0a 20 20 69 66 28 20 21  dices */.  if( !
27e42 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 69 6e  isView ){.    in
27e43 74 20 6e 49 64 78 3b 0a 0a 20 20 20 20 62 61 73  t nIdx;..    bas
27e44 65 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  eCur = pParse->n
27e45 54 61 62 3b 0a 20 20 20 20 6e 49 64 78 20 3d 20  Tab;.    nIdx = 
27e46 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
27e47 41 6e 64 49 6e 64 69 63 65 73 28 70 50 61 72 73  AndIndices(pPars
27e48 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75 72  e, pTab, baseCur
27e49 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3b  , OP_OpenWrite);
27e4a 0a 20 20 20 20 61 52 65 67 49 64 78 20 3d 20 73  .    aRegIdx = s
27e4b 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
27e4c 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74  w(db, sizeof(int
27e4d 29 2a 28 6e 49 64 78 2b 31 29 29 3b 0a 20 20 20  )*(nIdx+1));.   
27e4e 20 69 66 28 20 61 52 65 67 49 64 78 3d 3d 30 20   if( aRegIdx==0 
27e4f 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 69 6e  ){.      goto in
27e50 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  sert_cleanup;.  
27e51 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
27e52 20 69 3c 6e 49 64 78 3b 20 69 2b 2b 29 7b 0a 20   i<nIdx; i++){. 
27e53 20 20 20 20 20 61 52 65 67 49 64 78 5b 69 5d 20       aRegIdx[i] 
27e54 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
27e55 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
27e56 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 74 6f  * This is the to
27e57 70 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 69 6e  p of the main in
27e58 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a  sertion loop */.
27e59 20 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62    if( useTempTab
27e5a 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  le ){.    /* Thi
27e5b 73 20 62 6c 6f 63 6b 20 63 6f 64 65 73 20 74 68  s block codes th
27e5c 65 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e  e top of loop on
27e5d 6c 79 2e 20 20 54 68 65 20 63 6f 6d 70 6c 65 74  ly.  The complet
27e5e 65 20 6c 6f 6f 70 20 69 73 20 74 68 65 0a 20 20  e loop is the.  
27e5f 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 70    ** following p
27e60 73 65 75 64 6f 63 6f 64 65 20 28 74 65 6d 70 6c  seudocode (templ
27e61 61 74 65 20 34 29 3a 0a 20 20 20 20 2a 2a 0a 20  ate 4):.    **. 
27e62 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 72 65     **         re
27e63 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c 65 0a  wind temp table.
27e64 20 20 20 20 2a 2a 20 20 20 20 20 20 43 3a 20 6c      **      C: l
27e65 6f 6f 70 20 6f 76 65 72 20 72 6f 77 73 20 6f 66  oop over rows of
27e66 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61   intermediate ta
27e67 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ble.    **      
27e68 20 20 20 20 20 74 72 61 6e 73 66 65 72 20 76 61       transfer va
27e69 6c 75 65 73 20 66 6f 72 6d 20 69 6e 74 65 72 6d  lues form interm
27e6a 65 64 69 61 74 65 20 74 61 62 6c 65 20 69 6e 74  ediate table int
27e6b 6f 20 3c 74 61 62 6c 65 3e 0a 20 20 20 20 2a 2a  o <table>.    **
27e6c 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f           end loo
27e6d 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 44 3a  p.    **      D:
27e6e 20 2e 2e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   ....    */.    
27e6f 61 64 64 72 49 6e 73 54 6f 70 20 3d 20 73 71 6c  addrInsTop = sql
27e70 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
27e71 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 73 72 63  , OP_Rewind, src
27e72 54 61 62 29 3b 0a 20 20 20 20 61 64 64 72 43 6f  Tab);.    addrCo
27e73 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  nt = sqlite3Vdbe
27e74 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
27e75 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c    }else if( pSel
27e76 65 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ect ){.    /* Th
27e77 69 73 20 62 6c 6f 63 6b 20 63 6f 64 65 73 20 74  is block codes t
27e78 68 65 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 6f  he top of loop o
27e79 6e 6c 79 2e 20 20 54 68 65 20 63 6f 6d 70 6c 65  nly.  The comple
27e7a 74 65 20 6c 6f 6f 70 20 69 73 20 74 68 65 0a 20  te loop is the. 
27e7b 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20     ** following 
27e7c 70 73 65 75 64 6f 63 6f 64 65 20 28 74 65 6d 70  pseudocode (temp
27e7d 6c 61 74 65 20 33 29 3a 0a 20 20 20 20 2a 2a 0a  late 3):.    **.
27e7e 20 20 20 20 2a 2a 20 20 20 20 20 20 43 3a 20 79      **      C: y
27e7f 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20 20 20  ield X.    **   
27e80 20 20 20 20 20 20 69 66 20 45 4f 46 20 67 6f 74        if EOF got
27e81 6f 20 44 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  o D.    **      
27e82 20 20 20 69 6e 73 65 72 74 20 74 68 65 20 73 65     insert the se
27e83 6c 65 63 74 20 72 65 73 75 6c 74 20 69 6e 74 6f  lect result into
27e84 20 3c 74 61 62 6c 65 3e 20 66 72 6f 6d 20 52 2e   <table> from R.
27e85 2e 52 2b 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20  .R+n.    **     
27e86 20 20 20 20 67 6f 74 6f 20 43 0a 20 20 20 20 2a      goto C.    *
27e87 2a 20 20 20 20 20 20 44 3a 20 2e 2e 2e 0a 20 20  *      D: ....  
27e88 20 20 2a 2f 0a 20 20 20 20 61 64 64 72 43 6f 6e    */.    addrCon
27e89 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  t = sqlite3VdbeA
27e8a 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
27e8b 64 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a  d, dest.iParm);.
27e8c 20 20 20 20 61 64 64 72 49 6e 73 54 6f 70 20 3d      addrInsTop =
27e8d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27e8e 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67  p1(v, OP_If, reg
27e8f 45 6f 66 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Eof);.  }..  /* 
27e90 41 6c 6c 6f 63 61 74 65 20 72 65 67 69 73 74 65  Allocate registe
27e91 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74  rs for holding t
27e92 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
27e93 6e 65 77 20 72 6f 77 2c 0a 20 20 2a 2a 20 74 68  new row,.  ** th
27e94 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
27e95 20 6e 65 77 20 72 6f 77 2c 20 61 6e 64 20 74 68   new row, and th
27e96 65 20 61 73 73 65 6d 62 6c 69 65 64 20 72 6f 77  e assemblied row
27e97 20 72 65 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20   record..  */.  
27e98 72 65 67 52 65 63 6f 72 64 20 3d 20 2b 2b 70 50  regRecord = ++pP
27e99 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65  arse->nMem;.  re
27e9a 67 52 6f 77 69 64 20 3d 20 72 65 67 49 6e 73 20  gRowid = regIns 
27e9b 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31  = pParse->nMem+1
27e9c 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ;.  pParse->nMem
27e9d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 2b   += pTab->nCol +
27e9e 20 31 3b 0a 20 20 69 66 28 20 49 73 56 69 72 74   1;.  if( IsVirt
27e9f 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
27ea0 20 72 65 67 52 6f 77 69 64 2b 2b 3b 0a 20 20 20   regRowid++;.   
27ea1 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b   pParse->nMem++;
27ea2 0a 20 20 7d 0a 20 20 72 65 67 44 61 74 61 20 3d  .  }.  regData =
27ea3 20 72 65 67 52 6f 77 69 64 2b 31 3b 0a 0a 20 20   regRowid+1;..  
27ea4 2f 2a 20 52 75 6e 20 74 68 65 20 42 45 46 4f 52  /* Run the BEFOR
27ea5 45 20 61 6e 64 20 49 4e 53 54 45 41 44 20 4f 46  E and INSTEAD OF
27ea6 20 74 72 69 67 67 65 72 73 2c 20 69 66 20 74 68   triggers, if th
27ea7 65 72 65 20 61 72 65 20 61 6e 79 0a 20 20 2a 2f  ere are any.  */
27ea8 0a 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73  .  endOfLoop = s
27ea9 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
27eaa 62 65 6c 28 76 29 3b 0a 20 20 69 66 28 20 74 6d  bel(v);.  if( tm
27eab 61 73 6b 20 26 20 54 52 49 47 47 45 52 5f 42 45  ask & TRIGGER_BE
27eac 46 4f 52 45 20 29 7b 0a 20 20 20 20 69 6e 74 20  FORE ){.    int 
27ead 72 65 67 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65  regCols = sqlite
27eae 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
27eaf 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  arse, pTab->nCol
27eb0 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 62 75 69  +1);..    /* bui
27eb1 6c 64 20 74 68 65 20 4e 45 57 2e 2a 20 72 65 66  ld the NEW.* ref
27eb2 65 72 65 6e 63 65 20 72 6f 77 2e 20 20 4e 6f 74  erence row.  Not
27eb3 65 20 74 68 61 74 20 69 66 20 74 68 65 72 65 20  e that if there 
27eb4 69 73 20 61 6e 20 49 4e 54 45 47 45 52 0a 20 20  is an INTEGER.  
27eb5 20 20 2a 2a 20 50 52 49 4d 41 52 59 20 4b 45 59    ** PRIMARY KEY
27eb6 20 69 6e 74 6f 20 77 68 69 63 68 20 61 20 4e 55   into which a NU
27eb7 4c 4c 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65  LL is being inse
27eb8 72 74 65 64 2c 20 74 68 61 74 20 4e 55 4c 4c 20  rted, that NULL 
27eb9 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a 20 74  will be.    ** t
27eba 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61  ranslated into a
27ebb 20 75 6e 69 71 75 65 20 49 44 20 66 6f 72 20 74   unique ID for t
27ebc 68 65 20 72 6f 77 2e 20 20 42 75 74 20 6f 6e 20  he row.  But on 
27ebd 61 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72  a BEFORE trigger
27ebe 2c 0a 20 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e  ,.    ** we do n
27ebf 6f 74 20 6b 6e 6f 77 20 77 68 61 74 20 74 68 65  ot know what the
27ec0 20 75 6e 69 71 75 65 20 49 44 20 77 69 6c 6c 20   unique ID will 
27ec1 62 65 20 28 62 65 63 61 75 73 65 20 74 68 65 20  be (because the 
27ec2 69 6e 73 65 72 74 20 68 61 73 0a 20 20 20 20 2a  insert has.    *
27ec3 2a 20 6e 6f 74 20 68 61 70 70 65 6e 65 64 20 79  * not happened y
27ec4 65 74 29 20 73 6f 20 77 65 20 73 75 62 73 74 69  et) so we substi
27ec5 74 75 74 65 20 61 20 72 6f 77 69 64 20 6f 66 20  tute a rowid of 
27ec6 2d 31 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  -1.    */.    if
27ec7 28 20 6b 65 79 43 6f 6c 75 6d 6e 3c 30 20 29 7b  ( keyColumn<0 ){
27ec8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
27ec9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
27eca 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 65 67 43  nteger, -1, regC
27ecb 6f 6c 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ols);.    }else{
27ecc 0a 20 20 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20  .      int j1;. 
27ecd 20 20 20 20 20 69 66 28 20 75 73 65 54 65 6d 70       if( useTemp
27ece 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20  Table ){.       
27ecf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27ed0 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
27ed1 20 73 72 63 54 61 62 2c 20 6b 65 79 43 6f 6c 75   srcTab, keyColu
27ed2 6d 6e 2c 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20  mn, regCols);.  
27ed3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27ed4 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c 65     assert( pSele
27ed5 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 4f 74 68  ct==0 );  /* Oth
27ed6 65 72 77 69 73 65 20 75 73 65 54 65 6d 70 54 61  erwise useTempTa
27ed7 62 6c 65 20 69 73 20 74 72 75 65 20 2a 2f 0a 20  ble is true */. 
27ed8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
27ed9 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
27eda 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d  List->a[keyColum
27edb 6e 5d 2e 70 45 78 70 72 2c 20 72 65 67 43 6f 6c  n].pExpr, regCol
27edc 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  s);.      }.    
27edd 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
27ede 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e  beAddOp1(v, OP_N
27edf 6f 74 4e 75 6c 6c 2c 20 72 65 67 43 6f 6c 73 29  otNull, regCols)
27ee0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
27ee1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27ee2 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72 65 67  Integer, -1, reg
27ee3 43 6f 6c 73 29 3b 0a 20 20 20 20 20 20 73 71 6c  Cols);.      sql
27ee4 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
27ee5 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 73  (v, j1);.      s
27ee6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
27ee7 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74  (v, OP_MustBeInt
27ee8 2c 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20  , regCols);.    
27ee9 7d 0a 0a 20 20 20 20 2f 2a 20 43 61 6e 6e 6f 74  }..    /* Cannot
27eea 20 68 61 76 65 20 74 72 69 67 67 65 72 73 20 6f   have triggers o
27eeb 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  n a virtual tabl
27eec 65 2e 20 49 66 20 69 74 20 77 65 72 65 20 70 6f  e. If it were po
27eed 73 73 69 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 74  ssible,.    ** t
27eee 68 69 73 20 62 6c 6f 63 6b 20 77 6f 75 6c 64 20  his block would 
27eef 68 61 76 65 20 74 6f 20 61 63 63 6f 75 6e 74 20  have to account 
27ef0 66 6f 72 20 68 69 64 64 65 6e 20 63 6f 6c 75 6d  for hidden colum
27ef1 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  n..    */.    as
27ef2 73 65 72 74 28 20 21 49 73 56 69 72 74 75 61 6c  sert( !IsVirtual
27ef3 28 70 54 61 62 29 20 29 3b 0a 0a 20 20 20 20 2f  (pTab) );..    /
27ef4 2a 20 43 72 65 61 74 65 20 74 68 65 20 6e 65 77  * Create the new
27ef5 20 63 6f 6c 75 6d 6e 20 64 61 74 61 0a 20 20 20   column data.   
27ef6 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   */.    for(i=0;
27ef7 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
27ef8 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  ++){.      if( p
27ef9 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20  Column==0 ){.   
27efa 20 20 20 20 20 6a 20 3d 20 69 3b 0a 20 20 20 20       j = i;.    
27efb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
27efc 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c   for(j=0; j<pCol
27efd 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a  umn->nId; j++){.
27efe 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43            if( pC
27eff 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d  olumn->a[j].idx=
27f00 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =i ) break;.    
27f01 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
27f02 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 20      if( pColumn 
27f03 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e  && j>=pColumn->n
27f04 49 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  Id ){.        sq
27f05 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
27f06 61 72 73 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  arse, pTab->aCol
27f07 5b 69 5d 2e 70 44 66 6c 74 2c 20 72 65 67 43 6f  [i].pDflt, regCo
27f08 6c 73 2b 69 2b 31 29 3b 0a 20 20 20 20 20 20 7d  ls+i+1);.      }
27f09 65 6c 73 65 20 69 66 28 20 75 73 65 54 65 6d 70  else if( useTemp
27f0a 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20  Table ){.       
27f0b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27f0c 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
27f0d 20 73 72 63 54 61 62 2c 20 6a 2c 20 72 65 67 43   srcTab, j, regC
27f0e 6f 6c 73 2b 69 2b 31 29 3b 20 0a 20 20 20 20 20  ols+i+1); .     
27f0f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
27f10 61 73 73 65 72 74 28 20 70 53 65 6c 65 63 74 3d  assert( pSelect=
27f11 3d 30 20 29 3b 20 2f 2a 20 4f 74 68 65 72 77 69  =0 ); /* Otherwi
27f12 73 65 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  se useTempTable 
27f13 69 73 20 74 72 75 65 20 2a 2f 0a 20 20 20 20 20  is true */.     
27f14 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
27f15 64 65 41 6e 64 43 61 63 68 65 28 70 50 61 72 73  deAndCache(pPars
27f16 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70  e, pList->a[j].p
27f17 45 78 70 72 2c 20 72 65 67 43 6f 6c 73 2b 69 2b  Expr, regCols+i+
27f18 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1);.      }.    
27f19 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  }..    /* If thi
27f1a 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 6f  s is an INSERT o
27f1b 6e 20 61 20 76 69 65 77 20 77 69 74 68 20 61 6e  n a view with an
27f1c 20 49 4e 53 54 45 41 44 20 4f 46 20 49 4e 53 45   INSTEAD OF INSE
27f1d 52 54 20 74 72 69 67 67 65 72 2c 0a 20 20 20 20  RT trigger,.    
27f1e 2a 2a 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70  ** do not attemp
27f1f 74 20 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e  t any conversion
27f20 73 20 62 65 66 6f 72 65 20 61 73 73 65 6d 62 6c  s before assembl
27f21 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 2e 0a  ing the record..
27f22 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20 69      ** If this i
27f23 73 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20  s a real table, 
27f24 61 74 74 65 6d 70 74 20 63 6f 6e 76 65 72 73 69  attempt conversi
27f25 6f 6e 73 20 61 73 20 72 65 71 75 69 72 65 64 20  ons as required 
27f26 62 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 61  by the.    ** ta
27f27 62 6c 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  ble column affin
27f28 69 74 69 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  ities..    */.  
27f29 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b    if( !isView ){
27f2a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
27f2b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
27f2c 66 66 69 6e 69 74 79 2c 20 72 65 67 43 6f 6c 73  ffinity, regCols
27f2d 2b 31 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b  +1, pTab->nCol);
27f2e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54 61  .      sqlite3Ta
27f2f 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 76  bleAffinityStr(v
27f30 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a 0a  , pTab);.    }..
27f31 20 20 20 20 2f 2a 20 46 69 72 65 20 42 45 46 4f      /* Fire BEFO
27f32 52 45 20 6f 72 20 49 4e 53 54 45 41 44 20 4f 46  RE or INSTEAD OF
27f33 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20   triggers */.   
27f34 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
27f35 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 70  rigger(pParse, p
27f36 54 72 69 67 67 65 72 2c 20 54 4b 5f 49 4e 53 45  Trigger, TK_INSE
27f37 52 54 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 42  RT, 0, TRIGGER_B
27f38 45 46 4f 52 45 2c 20 0a 20 20 20 20 20 20 20 20  EFORE, .        
27f39 70 54 61 62 2c 20 72 65 67 43 6f 6c 73 2d 70 54  pTab, regCols-pT
27f3a 61 62 2d 3e 6e 43 6f 6c 2d 31 2c 20 6f 6e 45 72  ab->nCol-1, onEr
27f3b 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b  ror, endOfLoop);
27f3c 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  ..    sqlite3Rel
27f3d 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
27f3e 61 72 73 65 2c 20 72 65 67 43 6f 6c 73 2c 20 70  arse, regCols, p
27f3f 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20 20  Tab->nCol+1);.  
27f40 7d 0a 0a 20 20 2f 2a 20 50 75 73 68 20 74 68 65  }..  /* Push the
27f41 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66   record number f
27f42 6f 72 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79  or the new entry
27f43 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e   onto the stack.
27f44 20 20 54 68 65 0a 20 20 2a 2a 20 72 65 63 6f 72    The.  ** recor
27f45 64 20 6e 75 6d 62 65 72 20 69 73 20 61 20 72 61  d number is a ra
27f46 6e 64 6f 6d 6c 79 20 67 65 6e 65 72 61 74 65 20  ndomly generate 
27f47 69 6e 74 65 67 65 72 20 63 72 65 61 74 65 64 20  integer created 
27f48 62 79 20 4e 65 77 52 6f 77 69 64 0a 20 20 2a 2a  by NewRowid.  **
27f49 20 65 78 63 65 70 74 20 77 68 65 6e 20 74 68 65   except when the
27f4a 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e   table has an IN
27f4b 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
27f4c 59 20 63 6f 6c 75 6d 6e 2c 20 69 6e 20 77 68 69  Y column, in whi
27f4d 63 68 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65  ch.  ** case the
27f4e 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69   record number i
27f4f 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  s the same as th
27f50 61 74 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2f  at column. .  */
27f51 0a 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29  .  if( !isView )
27f52 7b 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74  {.    if( IsVirt
27f53 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
27f54 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 20 74 68     /* The row th
27f55 61 74 20 74 68 65 20 56 55 70 64 61 74 65 20 6f  at the VUpdate o
27f56 70 63 6f 64 65 20 77 69 6c 6c 20 64 65 6c 65 74  pcode will delet
27f57 65 3a 20 6e 6f 6e 65 20 2a 2f 0a 20 20 20 20 20  e: none */.     
27f58 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27f59 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
27f5a 2c 20 72 65 67 49 6e 73 29 3b 0a 20 20 20 20 7d  , regIns);.    }
27f5b 0a 20 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75  .    if( keyColu
27f5c 6d 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  mn>=0 ){.      i
27f5d 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20  f( useTempTable 
27f5e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
27f5f 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
27f60 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61  OP_Column, srcTa
27f61 62 2c 20 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65  b, keyColumn, re
27f62 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 7d  gRowid);.      }
27f63 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 74  else if( pSelect
27f64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
27f65 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27f66 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 46 72   OP_SCopy, regFr
27f67 6f 6d 53 65 6c 65 63 74 2b 6b 65 79 43 6f 6c 75  omSelect+keyColu
27f68 6d 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  mn, regRowid);. 
27f69 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
27f6a 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b      VdbeOp *pOp;
27f6b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27f6c 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
27f6d 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c   pList->a[keyCol
27f6e 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72 65 67 52  umn].pExpr, regR
27f6f 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20 20 70  owid);.        p
27f70 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  Op = sqlite3Vdbe
27f71 47 65 74 4f 70 28 76 2c 20 2d 31 29 3b 0a 20 20  GetOp(v, -1);.  
27f72 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53        if( ALWAYS
27f73 28 70 4f 70 29 20 26 26 20 70 4f 70 2d 3e 6f 70  (pOp) && pOp->op
27f74 63 6f 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 20 26 26  code==OP_Null &&
27f75 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62   !IsVirtual(pTab
27f76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  ) ){.          a
27f77 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20  ppendFlag = 1;. 
27f78 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70           pOp->op
27f79 63 6f 64 65 20 3d 20 4f 50 5f 4e 65 77 52 6f 77  code = OP_NewRow
27f7a 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f  id;.          pO
27f7b 70 2d 3e 70 31 20 3d 20 62 61 73 65 43 75 72 3b  p->p1 = baseCur;
27f7c 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e  .          pOp->
27f7d 70 32 20 3d 20 72 65 67 52 6f 77 69 64 3b 0a 20  p2 = regRowid;. 
27f7e 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70 33           pOp->p3
27f7f 20 3d 20 72 65 67 41 75 74 6f 69 6e 63 3b 0a 20   = regAutoinc;. 
27f80 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
27f81 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
27f82 20 50 52 49 4d 41 52 59 20 4b 45 59 20 65 78 70   PRIMARY KEY exp
27f83 72 65 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c  ression is NULL,
27f84 20 74 68 65 6e 20 75 73 65 20 4f 50 5f 4e 65 77   then use OP_New
27f85 52 6f 77 69 64 0a 20 20 20 20 20 20 2a 2a 20 74  Rowid.      ** t
27f86 6f 20 67 65 6e 65 72 61 74 65 20 61 20 75 6e 69  o generate a uni
27f87 71 75 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20  que primary key 
27f88 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f 0a  value..      */.
27f89 20 20 20 20 20 20 69 66 28 20 21 61 70 70 65 6e        if( !appen
27f8a 64 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20  dFlag ){.       
27f8b 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 20 20 20   int j1;.       
27f8c 20 69 66 28 20 21 49 73 56 69 72 74 75 61 6c 28   if( !IsVirtual(
27f8d 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20  pTab) ){.       
27f8e 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56     j1 = sqlite3V
27f8f 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
27f90 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69  NotNull, regRowi
27f91 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  d);.          sq
27f92 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
27f93 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
27f94 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f 77 69  baseCur, regRowi
27f95 64 2c 20 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a  d, regAutoinc);.
27f96 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27f97 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
27f98 20 6a 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 65   j1);.        }e
27f99 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6a  lse{.          j
27f9a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  1 = sqlite3VdbeC
27f9b 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
27f9c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27f9d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27f9e 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69  _IsNull, regRowi
27f9f 64 2c 20 6a 31 2b 32 29 3b 0a 20 20 20 20 20 20  d, j1+2);.      
27fa0 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
27fa1 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
27fa2 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72   OP_MustBeInt, r
27fa3 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  egRowid);.      
27fa4 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  }.    }else if( 
27fa5 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
27fa6 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
27fa7 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27fa8 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77  _Null, 0, regRow
27fa9 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  id);.    }else{.
27faa 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27fab 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65  eAddOp3(v, OP_Ne
27fac 77 52 6f 77 69 64 2c 20 62 61 73 65 43 75 72 2c  wRowid, baseCur,
27fad 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67 41 75   regRowid, regAu
27fae 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20 20 61 70  toinc);.      ap
27faf 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20 20  pendFlag = 1;.  
27fb0 20 20 7d 0a 20 20 20 20 61 75 74 6f 49 6e 63 53    }.    autoIncS
27fb1 74 65 70 28 70 50 61 72 73 65 2c 20 72 65 67 41  tep(pParse, regA
27fb2 75 74 6f 69 6e 63 2c 20 72 65 67 52 6f 77 69 64  utoinc, regRowid
27fb3 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 75 73 68 20  );..    /* Push 
27fb4 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2c 20  onto the stack, 
27fb5 64 61 74 61 20 66 6f 72 20 61 6c 6c 20 63 6f 6c  data for all col
27fb6 75 6d 6e 73 20 6f 66 20 74 68 65 20 6e 65 77 20  umns of the new 
27fb7 65 6e 74 72 79 2c 20 62 65 67 69 6e 6e 69 6e 67  entry, beginning
27fb8 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74 68 65  .    ** with the
27fb9 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 2e 0a 20   first column.. 
27fba 20 20 20 2a 2f 0a 20 20 20 20 6e 48 69 64 64 65     */.    nHidde
27fbb 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69  n = 0;.    for(i
27fbc 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
27fbd 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e  ; i++){.      in
27fbe 74 20 69 52 65 67 53 74 6f 72 65 20 3d 20 72 65  t iRegStore = re
27fbf 67 52 6f 77 69 64 2b 31 2b 69 3b 0a 20 20 20 20  gRowid+1+i;.    
27fc0 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69    if( i==pTab->i
27fc1 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20  PKey ){.        
27fc2 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  /* The value of 
27fc3 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d  the INTEGER PRIM
27fc4 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 69  ARY KEY column i
27fc5 73 20 61 6c 77 61 79 73 20 61 20 4e 55 4c 4c 2e  s always a NULL.
27fc6 0a 20 20 20 20 20 20 20 20 2a 2a 20 57 68 65 6e  .        ** When
27fc7 65 76 65 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e  ever this column
27fc8 20 69 73 20 72 65 61 64 2c 20 74 68 65 20 72 65   is read, the re
27fc9 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c  cord number will
27fca 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64 0a   be substituted.
27fcb 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 69 74          ** in it
27fcc 73 20 70 6c 61 63 65 2e 20 20 53 6f 20 77 69 6c  s place.  So wil
27fcd 6c 20 66 69 6c 6c 20 74 68 69 73 20 63 6f 6c 75  l fill this colu
27fce 6d 6e 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 74  mn with a NULL t
27fcf 6f 20 61 76 6f 69 64 0a 20 20 20 20 20 20 20 20  o avoid.        
27fd0 2a 2a 20 74 61 6b 69 6e 67 20 75 70 20 64 61 74  ** taking up dat
27fd1 61 20 73 70 61 63 65 20 77 69 74 68 20 69 6e 66  a space with inf
27fd2 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 77 69  ormation that wi
27fd3 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73 65 64  ll never be used
27fd4 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c  . */.        sql
27fd5 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27fd6 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69 52  , OP_Null, 0, iR
27fd7 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20  egStore);.      
27fd8 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
27fd9 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43    }.      if( pC
27fda 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  olumn==0 ){.    
27fdb 20 20 20 20 69 66 28 20 49 73 48 69 64 64 65 6e      if( IsHidden
27fdc 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43  Column(&pTab->aC
27fdd 6f 6c 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20  ol[i]) ){.      
27fde 20 20 20 20 61 73 73 65 72 74 28 20 49 73 56 69      assert( IsVi
27fdf 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 20  rtual(pTab) );. 
27fe0 20 20 20 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b           j = -1;
27fe1 0a 20 20 20 20 20 20 20 20 20 20 6e 48 69 64 64  .          nHidd
27fe2 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65  en++;.        }e
27fe3 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 6a  lse{.          j
27fe4 20 3d 20 69 20 2d 20 6e 48 69 64 64 65 6e 3b 0a   = i - nHidden;.
27fe5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
27fe6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66  }else{.        f
27fe7 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c 75 6d  or(j=0; j<pColum
27fe8 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20  n->nId; j++){.  
27fe9 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c          if( pCol
27fea 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d 3d 69  umn->a[j].idx==i
27feb 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
27fec 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
27fed 20 20 69 66 28 20 6a 3c 30 20 7c 7c 20 6e 43 6f    if( j<0 || nCo
27fee 6c 75 6d 6e 3d 3d 30 20 7c 7c 20 28 70 43 6f 6c  lumn==0 || (pCol
27fef 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75 6d  umn && j>=pColum
27ff0 6e 2d 3e 6e 49 64 29 20 29 7b 0a 20 20 20 20 20  n->nId) ){.     
27ff1 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
27ff2 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  de(pParse, pTab-
27ff3 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20  >aCol[i].pDflt, 
27ff4 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20  iRegStore);.    
27ff5 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65 54    }else if( useT
27ff6 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
27ff7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27ff8 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
27ff9 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20 69  mn, srcTab, j, i
27ffa 52 65 67 53 74 6f 72 65 29 3b 20 0a 20 20 20 20  RegStore); .    
27ffb 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c    }else if( pSel
27ffc 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ect ){.        s
27ffd 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27ffe 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65  (v, OP_SCopy, re
27fff 67 46 72 6f 6d 53 65 6c 65 63 74 2b 6a 2c 20 69  gFromSelect+j, i
28000 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20  RegStore);.     
28001 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
28002 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
28003 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61  pParse, pList->a
28004 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 52 65 67 53  [j].pExpr, iRegS
28005 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  tore);.      }. 
28006 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 6e     }..    /* Gen
28007 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63 68  erate code to ch
28008 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  eck constraints 
28009 61 6e 64 20 67 65 6e 65 72 61 74 65 20 69 6e 64  and generate ind
2800a 65 78 20 6b 65 79 73 20 61 6e 64 0a 20 20 20 20  ex keys and.    
2800b 2a 2a 20 64 6f 20 74 68 65 20 69 6e 73 65 72 74  ** do the insert
2800c 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e  ion..    */.#ifn
2800d 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2800e 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
2800f 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
28010 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 63 6f  Tab) ){.      co
28011 6e 73 74 20 63 68 61 72 20 2a 70 56 54 61 62 20  nst char *pVTab 
28012 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
28013 73 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65  sqlite3GetVTable
28014 28 64 62 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  (db, pTab);.    
28015 20 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b    sqlite3VtabMak
28016 65 57 72 69 74 61 62 6c 65 28 70 50 61 72 73 65  eWritable(pParse
28017 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 73  , pTab);.      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 56 55 70 64 61 74 65 2c 20  (v, OP_VUpdate, 
2801a 31 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c  1, pTab->nCol+2,
2801b 20 72 65 67 49 6e 73 2c 20 70 56 54 61 62 2c 20   regIns, pVTab, 
2801c 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 20 20  P4_VTAB);.      
2801d 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28  sqlite3MayAbort(
2801e 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 65 6c  pParse);.    }el
2801f 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a  se.#endif.    {.
28020 20 20 20 20 20 20 69 6e 74 20 69 73 52 65 70 6c        int isRepl
28021 61 63 65 3b 20 20 20 20 2f 2a 20 53 65 74 20 74  ace;    /* Set t
28022 6f 20 74 72 75 65 20 69 66 20 63 6f 6e 73 74 72  o true if constr
28023 61 69 6e 74 73 20 6d 61 79 20 63 61 75 73 65 20  aints may cause 
28024 61 20 72 65 70 6c 61 63 65 20 2a 2f 0a 20 20 20  a replace */.   
28025 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61     sqlite3Genera
28026 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
28027 6b 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ks(pParse, pTab,
28028 20 62 61 73 65 43 75 72 2c 20 72 65 67 49 6e 73   baseCur, regIns
28029 2c 20 61 52 65 67 49 64 78 2c 0a 20 20 20 20 20  , aRegIdx,.     
2802a 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d       keyColumn>=
2802b 30 2c 20 30 2c 20 6f 6e 45 72 72 6f 72 2c 20 65  0, 0, onError, e
2802c 6e 64 4f 66 4c 6f 6f 70 2c 20 26 69 73 52 65 70  ndOfLoop, &isRep
2802d 6c 61 63 65 0a 20 20 20 20 20 20 29 3b 0a 20 20  lace.      );.  
2802e 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68 65      sqlite3FkChe
2802f 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ck(pParse, pTab,
28030 20 30 2c 20 72 65 67 49 6e 73 29 3b 0a 20 20 20   0, regIns);.   
28031 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65     sqlite3Comple
28032 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20 20  teInsertion(.   
28033 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20 70         pParse, p
28034 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72 65  Tab, baseCur, re
28035 67 49 6e 73 2c 20 61 52 65 67 49 64 78 2c 20 30  gIns, aRegIdx, 0
28036 2c 20 61 70 70 65 6e 64 46 6c 61 67 2c 20 69 73  , appendFlag, is
28037 52 65 70 6c 61 63 65 3d 3d 30 0a 20 20 20 20 20  Replace==0.     
28038 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   );.    }.  }.. 
28039 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 63   /* Update the c
2803a 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 74 68 61  ount of rows tha
2803b 74 20 61 72 65 20 69 6e 73 65 72 74 65 64 0a 20  t are inserted. 
2803c 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66   */.  if( (db->f
2803d 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f  lags & SQLITE_Co
2803e 75 6e 74 52 6f 77 73 29 21 3d 30 20 29 7b 0a 20  untRows)!=0 ){. 
2803f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
28040 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d  dOp2(v, OP_AddIm
28041 6d 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 2c 20  m, regRowCount, 
28042 31 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  1);.  }..  if( p
28043 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 2f  Trigger ){.    /
28044 2a 20 43 6f 64 65 20 41 46 54 45 52 20 74 72 69  * Code AFTER tri
28045 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 73 71 6c  ggers */.    sql
28046 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
28047 65 72 28 70 50 61 72 73 65 2c 20 70 54 72 69 67  er(pParse, pTrig
28048 67 65 72 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20  ger, TK_INSERT, 
28049 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52  0, TRIGGER_AFTER
2804a 2c 20 0a 20 20 20 20 20 20 20 20 70 54 61 62 2c  , .        pTab,
2804b 20 72 65 67 44 61 74 61 2d 32 2d 70 54 61 62 2d   regData-2-pTab-
2804c 3e 6e 43 6f 6c 2c 20 6f 6e 45 72 72 6f 72 2c 20  >nCol, onError, 
2804d 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 7d 0a  endOfLoop);.  }.
2804e 0a 20 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d  .  /* The bottom
2804f 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 69 6e 73   of the main ins
28050 65 72 74 69 6f 6e 20 6c 6f 6f 70 2c 20 69 66 20  ertion loop, if 
28051 74 68 65 20 64 61 74 61 20 73 6f 75 72 63 65 0a  the data source.
28052 20 20 2a 2a 20 69 73 20 61 20 53 45 4c 45 43 54    ** is a SELECT
28053 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f   statement..  */
28054 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
28055 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e  solveLabel(v, en
28056 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 69 66 28 20  dOfLoop);.  if( 
28057 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a  useTempTable ){.
28058 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28059 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
2805a 2c 20 73 72 63 54 61 62 2c 20 61 64 64 72 43 6f  , srcTab, addrCo
2805b 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nt);.    sqlite3
2805c 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2805d 61 64 64 72 49 6e 73 54 6f 70 29 3b 0a 20 20 20  addrInsTop);.   
2805e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2805f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p1(v, OP_Close, 
28060 73 72 63 54 61 62 29 3b 0a 20 20 7d 65 6c 73 65  srcTab);.  }else
28061 20 69 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a   if( pSelect ){.
28062 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
28063 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
28064 2c 20 30 2c 20 61 64 64 72 43 6f 6e 74 29 3b 0a  , 0, addrCont);.
28065 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
28066 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 49  umpHere(v, addrI
28067 6e 73 54 6f 70 29 3b 0a 20 20 7d 0a 0a 20 20 69  nsTop);.  }..  i
28068 66 28 20 21 49 73 56 69 72 74 75 61 6c 28 70 54  f( !IsVirtual(pT
28069 61 62 29 20 26 26 20 21 69 73 56 69 65 77 20 29  ab) && !isView )
2806a 7b 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20 61  {.    /* Close a
2806b 6c 6c 20 74 61 62 6c 65 73 20 6f 70 65 6e 65 64  ll tables opened
2806c 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
2806d 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2806e 43 6c 6f 73 65 2c 20 62 61 73 65 43 75 72 29 3b  Close, baseCur);
2806f 0a 20 20 20 20 66 6f 72 28 69 64 78 3d 31 2c 20  .    for(idx=1, 
28070 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
28071 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
28072 64 78 2d 3e 70 4e 65 78 74 2c 20 69 64 78 2b 2b  dx->pNext, idx++
28073 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
28074 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
28075 5f 43 6c 6f 73 65 2c 20 69 64 78 2b 62 61 73 65  _Close, idx+base
28076 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Cur);.    }.  }.
28077 0a 69 6e 73 65 72 74 5f 65 6e 64 3a 0a 20 20 2f  .insert_end:.  /
28078 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 71 6c  * Update the sql
28079 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
2807a 6c 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74 68  le by storing th
2807b 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65  e content of the
2807c 0a 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 72 6f  .  ** maximum ro
2807d 77 69 64 20 63 6f 75 6e 74 65 72 20 76 61 6c 75  wid counter valu
2807e 65 73 20 72 65 63 6f 72 64 65 64 20 77 68 69 6c  es recorded whil
2807f 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f  e inserting into
28080 0a 20 20 2a 2a 20 61 75 74 6f 69 6e 63 72 65 6d  .  ** autoincrem
28081 65 6e 74 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f  ent tables..  */
28082 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
28083 65 73 74 65 64 3d 3d 30 20 26 26 20 70 50 61 72  ested==0 && pPar
28084 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 3d  se->pTriggerTab=
28085 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
28086 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e  3AutoincrementEn
28087 64 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a  d(pParse);.  }..
28088 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e    /*.  ** Return
28089 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
2808a 6f 77 73 20 69 6e 73 65 72 74 65 64 2e 20 49 66  ows inserted. If
2808b 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
2808c 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e   .  ** generatin
2808d 67 20 63 6f 64 65 20 62 65 63 61 75 73 65 20 6f  g code because o
2808e 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  f a call to sqli
2808f 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 29  te3NestedParse()
28090 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e  , do not.  ** in
28091 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63  voke the callbac
28092 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f  k function..  */
28093 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67  .  if( (db->flag
28094 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  s&SQLITE_CountRo
28095 77 73 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e  ws) && !pParse->
28096 6e 65 73 74 65 64 20 26 26 20 21 70 50 61 72 73  nested && !pPars
28097 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 29  e->pTriggerTab )
28098 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
28099 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
2809a 73 75 6c 74 52 6f 77 2c 20 72 65 67 52 6f 77 43  sultRow, regRowC
2809b 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73 71  ount, 1);.    sq
2809c 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
2809d 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73  ols(v, 1);.    s
2809e 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2809f 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
280a0 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20 69  ME_NAME, "rows i
280a1 6e 73 65 72 74 65 64 22 2c 20 53 51 4c 49 54 45  nserted", SQLITE
280a2 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 69  _STATIC);.  }..i
280a3 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3a 0a 20  nsert_cleanup:. 
280a4 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44   sqlite3SrcListD
280a5 65 6c 65 74 65 28 64 62 2c 20 70 54 61 62 4c 69  elete(db, pTabLi
280a6 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  st);.  sqlite3Ex
280a7 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
280a8 20 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74   pList);.  sqlit
280a9 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
280aa 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 73  b, pSelect);.  s
280ab 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
280ac 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 29 3b  te(db, pColumn);
280ad 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
280ae 28 64 62 2c 20 61 52 65 67 49 64 78 29 3b 0a 7d  (db, aRegIdx);.}
280af 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22  ../* Make sure "
280b0 69 73 56 69 65 77 22 20 61 6e 64 20 6f 74 68 65  isView" and othe
280b1 72 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64  r macros defined
280b2 20 61 62 6f 76 65 20 61 72 65 20 75 6e 64 65 66   above are undef
280b3 69 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 0a  ined. Otherwise.
280b4 2a 2a 20 74 68 65 6c 79 20 6d 61 79 20 69 6e 74  ** thely may int
280b5 65 72 66 65 72 65 20 77 69 74 68 20 63 6f 6d 70  erfere with comp
280b6 69 6c 61 74 69 6f 6e 20 6f 66 20 6f 74 68 65 72  ilation of other
280b7 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68   functions in th
280b8 69 73 20 66 69 6c 65 0a 2a 2a 20 28 6f 72 20 69  is file.** (or i
280b9 6e 20 61 6e 6f 74 68 65 72 20 66 69 6c 65 2c 20  n another file, 
280ba 69 66 20 74 68 69 73 20 66 69 6c 65 20 62 65 63  if this file bec
280bb 6f 6d 65 73 20 70 61 72 74 20 6f 66 20 74 68 65  omes part of the
280bc 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 29 2e 20   amalgamation). 
280bd 20 2a 2f 0a 23 69 66 64 65 66 20 69 73 56 69 65   */.#ifdef isVie
280be 77 0a 20 23 75 6e 64 65 66 20 69 73 56 69 65 77  w. #undef isView
280bf 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 70  .#endif.#ifdef p
280c0 54 72 69 67 67 65 72 0a 20 23 75 6e 64 65 66 20  Trigger. #undef 
280c1 70 54 72 69 67 67 65 72 0a 23 65 6e 64 69 66 0a  pTrigger.#endif.
280c2 23 69 66 64 65 66 20 74 6d 61 73 6b 0a 20 23 75  #ifdef tmask. #u
280c3 6e 64 65 66 20 74 6d 61 73 6b 0a 23 65 6e 64 69  ndef tmask.#endi
280c4 66 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  f.../*.** Genera
280c5 74 65 20 63 6f 64 65 20 74 6f 20 64 6f 20 63 6f  te code to do co
280c6 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 73 20  nstraint checks 
280c7 70 72 69 6f 72 20 74 6f 20 61 6e 20 49 4e 53 45  prior to an INSE
280c8 52 54 20 6f 72 20 61 6e 20 55 50 44 41 54 45 2e  RT or an UPDATE.
280c9 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74  .**.** The input
280ca 20 69 73 20 61 20 72 61 6e 67 65 20 6f 66 20 63   is a range of c
280cb 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 69 73  onsecutive regis
280cc 74 65 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ters as follows:
280cd 0a 2a 2a 0a 2a 2a 20 20 20 20 31 2e 20 20 54 68  .**.**    1.  Th
280ce 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72  e rowid of the r
280cf 6f 77 20 61 66 74 65 72 20 74 68 65 20 75 70 64  ow after the upd
280d0 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 32 2e  ate..**.**    2.
280d1 20 20 54 68 65 20 64 61 74 61 20 69 6e 20 74 68    The data in th
280d2 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f  e first column o
280d3 66 20 74 68 65 20 65 6e 74 72 79 20 61 66 74 65  f the entry afte
280d4 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a 2a 2a  r the update..**
280d5 0a 2a 2a 20 20 20 20 69 2e 20 20 44 61 74 61 20  .**    i.  Data 
280d6 66 72 6f 6d 20 6d 69 64 64 6c 65 20 63 6f 6c 75  from middle colu
280d7 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  mns....**.**    
280d8 4e 2e 20 20 54 68 65 20 64 61 74 61 20 69 6e 20  N.  The data in 
280d9 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20  the last column 
280da 6f 66 20 74 68 65 20 65 6e 74 72 79 20 61 66 74  of the entry aft
280db 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a 2a  er the update..*
280dc 2a 0a 2a 2a 20 54 68 65 20 72 65 67 52 6f 77 69  *.** The regRowi
280dd 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  d parameter is t
280de 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
280df 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e  register contain
280e0 69 6e 67 20 28 31 29 2e 0a 2a 2a 0a 2a 2a 20 49  ing (1)..**.** I
280e1 66 20 69 73 55 70 64 61 74 65 20 69 73 20 74 72  f isUpdate is tr
280e2 75 65 20 61 6e 64 20 72 6f 77 69 64 43 68 6e 67  ue and rowidChng
280e3 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68   is non-zero, th
280e4 65 6e 20 72 6f 77 69 64 43 68 6e 67 20 63 6f 6e  en rowidChng con
280e5 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20 61 64 64  tains.** the add
280e6 72 65 73 73 20 6f 66 20 61 20 72 65 67 69 73 74  ress of a regist
280e7 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  er containing th
280e8 65 20 72 6f 77 69 64 20 62 65 66 6f 72 65 20 74  e rowid before t
280e9 68 65 20 75 70 64 61 74 65 20 74 61 6b 65 73 0a  he update takes.
280ea 2a 2a 20 70 6c 61 63 65 2e 20 69 73 55 70 64 61  ** place. isUpda
280eb 74 65 20 69 73 20 74 72 75 65 20 66 6f 72 20 55  te is true for U
280ec 50 44 41 54 45 73 20 61 6e 64 20 66 61 6c 73 65  PDATEs and false
280ed 20 66 6f 72 20 49 4e 53 45 52 54 73 2e 20 49 66   for INSERTs. If
280ee 20 69 73 55 70 64 61 74 65 0a 2a 2a 20 69 73 20   isUpdate.** is 
280ef 66 61 6c 73 65 2c 20 69 6e 64 69 63 61 74 69 6e  false, indicatin
280f0 67 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74  g an INSERT stat
280f1 65 6d 65 6e 74 2c 20 74 68 65 6e 20 61 20 6e 6f  ement, then a no
280f2 6e 2d 7a 65 72 6f 20 72 6f 77 69 64 43 68 6e 67  n-zero rowidChng
280f3 20 0a 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 74   .** indicates t
280f4 68 61 74 20 74 68 65 20 72 6f 77 69 64 20 77 61  hat the rowid wa
280f5 73 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65  s explicitly spe
280f6 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20 6f  cified as part o
280f7 66 20 74 68 65 0a 2a 2a 20 49 4e 53 45 52 54 20  f the.** INSERT 
280f8 73 74 61 74 65 6d 65 6e 74 2e 20 49 66 20 72 6f  statement. If ro
280f9 77 69 64 43 68 6e 67 20 69 73 20 66 61 6c 73 65  widChng is false
280fa 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
280fb 20 74 68 65 20 72 6f 77 69 64 20 69 73 0a 2a 2a   the rowid is.**
280fc 20 63 6f 6d 70 75 74 65 64 20 61 75 74 6f 6d 61   computed automa
280fd 74 69 63 61 6c 6c 79 20 69 6e 20 61 6e 20 69 6e  tically in an in
280fe 73 65 72 74 20 6f 72 20 74 68 61 74 20 74 68 65  sert or that the
280ff 20 72 6f 77 69 64 20 76 61 6c 75 65 20 69 73 20   rowid value is 
28100 6e 6f 74 20 0a 2a 2a 20 6d 6f 64 69 66 69 65 64  not .** modified
28101 20 62 79 20 61 6e 20 75 70 64 61 74 65 2e 0a 2a   by an update..*
28102 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65  *.** The code ge
28103 6e 65 72 61 74 65 64 20 62 79 20 74 68 69 73 20  nerated by this 
28104 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 20 6e 65  routine store ne
28105 77 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20  w index entries 
28106 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72  into.** register
28107 73 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20  s identified by 
28108 61 52 65 67 49 64 78 5b 5d 2e 20 20 4e 6f 20 69  aRegIdx[].  No i
28109 6e 64 65 78 20 65 6e 74 72 79 20 69 73 20 63 72  ndex entry is cr
2810a 65 61 74 65 64 20 66 6f 72 0a 2a 2a 20 69 6e 64  eated for.** ind
2810b 69 63 65 73 20 77 68 65 72 65 20 61 52 65 67 49  ices where aRegI
2810c 64 78 5b 69 5d 3d 3d 30 2e 20 20 54 68 65 20 6f  dx[i]==0.  The o
2810d 72 64 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20  rder of indices 
2810e 69 6e 20 61 52 65 67 49 64 78 5b 5d 20 69 73 0a  in aRegIdx[] is.
2810f 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  ** the same as t
28110 68 65 20 6f 72 64 65 72 20 6f 66 20 69 6e 64 69  he order of indi
28111 63 65 73 20 6f 6e 20 74 68 65 20 6c 69 6e 6b 65  ces on the linke
28112 64 20 6c 69 73 74 20 6f 66 20 69 6e 64 69 63 65  d list of indice
28113 73 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 74 6f  s.** attached to
28114 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   the table..**.*
28115 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
28116 6c 73 6f 20 67 65 6e 65 72 61 74 65 73 20 63 6f  lso generates co
28117 64 65 20 74 6f 20 63 68 65 63 6b 20 63 6f 6e 73  de to check cons
28118 74 72 61 69 6e 74 73 2e 20 20 4e 4f 54 20 4e 55  traints.  NOT NU
28119 4c 4c 2c 0a 2a 2a 20 43 48 45 43 4b 2c 20 61 6e  LL,.** CHECK, an
2811a 64 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61  d UNIQUE constra
2811b 69 6e 74 73 20 61 72 65 20 61 6c 6c 20 63 68 65  ints are all che
2811c 63 6b 65 64 2e 20 20 49 66 20 61 20 63 6f 6e 73  cked.  If a cons
2811d 74 72 61 69 6e 74 20 66 61 69 6c 73 2c 0a 2a 2a  traint fails,.**
2811e 20 74 68 65 6e 20 74 68 65 20 61 70 70 72 6f 70   then the approp
2811f 72 69 61 74 65 20 61 63 74 69 6f 6e 20 69 73 20  riate action is 
28120 70 65 72 66 6f 72 6d 65 64 2e 20 20 54 68 65 72  performed.  Ther
28121 65 20 61 72 65 20 66 69 76 65 20 70 6f 73 73 69  e are five possi
28122 62 6c 65 0a 2a 2a 20 61 63 74 69 6f 6e 73 3a 20  ble.** actions: 
28123 52 4f 4c 4c 42 41 43 4b 2c 20 41 42 4f 52 54 2c  ROLLBACK, ABORT,
28124 20 46 41 49 4c 2c 20 52 45 50 4c 41 43 45 2c 20   FAIL, REPLACE, 
28125 61 6e 64 20 49 47 4e 4f 52 45 2e 0a 2a 2a 0a 2a  and IGNORE..**.*
28126 2a 20 20 43 6f 6e 73 74 72 61 69 6e 74 20 74 79  *  Constraint ty
28127 70 65 20 20 41 63 74 69 6f 6e 20 20 20 20 20 20  pe  Action      
28128 20 57 68 61 74 20 48 61 70 70 65 6e 73 0a 2a 2a   What Happens.**
28129 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
2812a 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20  -  ----------   
2812b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2812c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2812d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 61 6e 79  --------.**  any
2812e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 52 4f                RO
2812f 4c 4c 42 41 43 4b 20 20 20 20 20 54 68 65 20 63  LLBACK     The c
28130 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
28131 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63  on is rolled bac
28132 6b 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  k and.**        
28133 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28134 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28135 65 78 65 63 28 29 20 72 65 74 75 72 6e 73 20 69  exec() returns i
28136 6d 6d 65 64 69 61 74 65 6c 79 20 77 69 74 68 20  mmediately with 
28137 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  a.**            
28138 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28139 20 20 20 20 72 65 74 75 72 6e 20 63 6f 64 65 20      return code 
2813a 6f 66 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52  of SQLITE_CONSTR
2813b 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79  AINT..**.**  any
2813c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 41 42                AB
2813d 4f 52 54 20 20 20 20 20 20 20 20 42 61 63 6b 20  ORT        Back 
2813e 6f 75 74 20 63 68 61 6e 67 65 73 20 66 72 6f 6d  out changes from
2813f 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6d   the current com
28140 6d 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  mand.**         
28141 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28142 20 20 20 20 20 20 20 6f 6e 6c 79 20 28 64 6f 20         only (do 
28143 6e 6f 74 20 64 6f 20 61 20 63 6f 6d 70 6c 65 74  not do a complet
28144 65 20 72 6f 6c 6c 62 61 63 6b 29 20 74 68 65 6e  e rollback) then
28145 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
28146 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28147 20 20 20 63 61 75 73 65 20 73 71 6c 69 74 65 33     cause sqlite3
28148 5f 65 78 65 63 28 29 20 74 6f 20 72 65 74 75 72  _exec() to retur
28149 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a  n immediately.**
2814a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2814b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2814c 77 69 74 68 20 53 51 4c 49 54 45 5f 43 4f 4e 53  with SQLITE_CONS
2814d 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a 20 20 61  TRAINT..**.**  a
2814e 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ny              
2814f 46 41 49 4c 20 20 20 20 20 20 20 20 20 53 71 6c  FAIL         Sql
28150 69 74 65 5f 65 78 65 63 28 29 20 72 65 74 75 72  ite_exec() retur
28151 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 77  ns immediately w
28152 69 74 68 20 61 0a 2a 2a 20 20 20 20 20 20 20 20  ith a.**        
28153 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28154 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 63          return c
28155 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 43 4f  ode of SQLITE_CO
28156 4e 53 54 52 41 49 4e 54 2e 20 20 54 68 65 0a 2a  NSTRAINT.  The.*
28157 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
28158 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28159 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
2815a 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  not rolled back 
2815b 61 6e 64 20 61 6e 79 0a 2a 2a 20 20 20 20 20 20  and any.**      
2815c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2815d 20 20 20 20 20 20 20 20 20 20 70 72 69 6f 72 20            prior 
2815e 63 68 61 6e 67 65 73 20 61 72 65 20 72 65 74 61  changes are reta
2815f 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 61 6e 79  ined..**.**  any
28160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49 47                IG
28161 4e 4f 52 45 20 20 20 20 20 20 20 54 68 65 20 72  NORE       The r
28162 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 61 6e 64  ecord number and
28163 20 64 61 74 61 20 69 73 20 70 6f 70 70 65 64 20   data is popped 
28164 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20  from.**         
28165 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28166 20 20 20 20 20 20 20 74 68 65 20 73 74 61 63 6b         the stack
28167 20 61 6e 64 20 74 68 65 72 65 20 69 73 20 61 6e   and there is an
28168 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d 70 0a   immediate jump.
28169 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
2816a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2816b 20 20 74 6f 20 6c 61 62 65 6c 20 69 67 6e 6f 72    to label ignor
2816c 65 44 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 20 4e 4f  eDest..**.**  NO
2816d 54 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 52  T NULL         R
2816e 45 50 4c 41 43 45 20 20 20 20 20 20 54 68 65 20  EPLACE      The 
2816f 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 72 65  NULL value is re
28170 70 6c 61 63 65 20 62 79 20 74 68 65 20 64 65 66  place by the def
28171 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  ault.**         
28172 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28173 20 20 20 20 20 20 20 76 61 6c 75 65 20 66 6f 72         value for
28174 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 20 20 49   that column.  I
28175 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  f the default va
28176 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  lue.**          
28177 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28178 20 20 20 20 20 20 69 73 20 4e 55 4c 4c 2c 20 74        is NULL, t
28179 68 65 20 61 63 74 69 6f 6e 20 69 73 20 74 68 65  he action is the
2817a 20 73 61 6d 65 20 61 73 20 41 42 4f 52 54 2e 0a   same as ABORT..
2817b 2a 2a 0a 2a 2a 20 20 55 4e 49 51 55 45 20 20 20  **.**  UNIQUE   
2817c 20 20 20 20 20 20 20 20 52 45 50 4c 41 43 45 20          REPLACE 
2817d 20 20 20 20 20 54 68 65 20 6f 74 68 65 72 20 72       The other r
2817e 6f 77 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74  ow that conflict
2817f 73 20 77 69 74 68 20 74 68 65 20 72 6f 77 0a 2a  s with the row.*
28180 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
28181 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28182 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20   being inserted 
28183 69 73 20 72 65 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a  is removed..**.*
28184 2a 20 20 43 48 45 43 4b 20 20 20 20 20 20 20 20  *  CHECK        
28185 20 20 20 20 52 45 50 4c 41 43 45 20 20 20 20 20      REPLACE     
28186 20 49 6c 6c 65 67 61 6c 2e 20 20 54 68 65 20 72   Illegal.  The r
28187 65 73 75 6c 74 73 20 69 6e 20 61 6e 20 65 78 63  esults in an exc
28188 65 70 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68  eption..**.** Wh
28189 69 63 68 20 61 63 74 69 6f 6e 20 74 6f 20 74 61  ich action to ta
2818a 6b 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65 64  ke is determined
2818b 20 62 79 20 74 68 65 20 6f 76 65 72 72 69 64 65   by the override
2818c 45 72 72 6f 72 20 70 61 72 61 6d 65 74 65 72 2e  Error parameter.
2818d 0a 2a 2a 20 4f 72 20 69 66 20 6f 76 65 72 72 69  .** Or if overri
2818e 64 65 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  deError==OE_Defa
2818f 75 6c 74 2c 20 74 68 65 6e 20 74 68 65 20 70 50  ult, then the pP
28190 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 20 70 61  arse->onError pa
28191 72 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 75 73  rameter.** is us
28192 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72 73  ed.  Or if pPars
28193 65 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  e->onError==OE_D
28194 65 66 61 75 6c 74 20 74 68 65 6e 20 74 68 65 20  efault then the 
28195 6f 6e 45 72 72 6f 72 20 76 61 6c 75 65 0a 2a 2a  onError value.**
28196 20 66 6f 72 20 74 68 65 20 63 6f 6e 73 74 72 61   for the constra
28197 69 6e 74 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a  int is used..**.
28198 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 72  ** The calling r
28199 6f 75 74 69 6e 65 20 6d 75 73 74 20 6f 70 65 6e  outine must open
2819a 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63 75   a read/write cu
2819b 72 73 6f 72 20 66 6f 72 20 70 54 61 62 20 77 69  rsor for pTab wi
2819c 74 68 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d  th.** cursor num
2819d 62 65 72 20 22 62 61 73 65 43 75 72 22 2e 20 20  ber "baseCur".  
2819e 41 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 70  All indices of p
2819f 54 61 62 20 6d 75 73 74 20 61 6c 73 6f 20 68 61  Tab must also ha
281a0 76 65 20 6f 70 65 6e 0a 2a 2a 20 72 65 61 64 2f  ve open.** read/
281a1 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 77 69  write cursors wi
281a2 74 68 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  th cursor number
281a3 20 62 61 73 65 43 75 72 2b 69 20 66 6f 72 20 74   baseCur+i for t
281a4 68 65 20 69 2d 74 68 20 63 75 72 73 6f 72 2e 0a  he i-th cursor..
281a5 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 74 68  ** Except, if th
281a6 65 72 65 20 69 73 20 6e 6f 20 70 6f 73 73 69 62  ere is no possib
281a7 69 6c 69 74 79 20 6f 66 20 61 20 52 45 50 4c 41  ility of a REPLA
281a8 43 45 20 61 63 74 69 6f 6e 20 74 68 65 6e 0a 2a  CE action then.*
281a9 2a 20 63 75 72 73 6f 72 73 20 64 6f 20 6e 6f 74  * cursors do not
281aa 20 6e 65 65 64 20 74 6f 20 62 65 20 6f 70 65 6e   need to be open
281ab 20 66 6f 72 20 69 6e 64 69 63 65 73 20 77 68 65   for indices whe
281ac 72 65 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30  re aRegIdx[i]==0
281ad 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
281ae 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
281af 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69  GenerateConstrai
281b0 6e 74 43 68 65 63 6b 73 28 0a 20 20 50 61 72 73  ntChecks(.  Pars
281b1 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
281b2 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
281b3 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
281b4 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 2f   *pTab,        /
281b5 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f  * the table into
281b6 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69 6e   which we are in
281b7 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  serting */.  int
281b8 20 62 61 73 65 43 75 72 2c 20 20 20 20 20 20 20   baseCur,       
281b9 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20 72   /* Index of a r
281ba 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72  ead/write cursor
281bb 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70 54 61   pointing at pTa
281bc 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f  b */.  int regRo
281bd 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20 49 6e  wid,       /* In
281be 64 65 78 20 6f 66 20 74 68 65 20 72 61 6e 67 65  dex of the range
281bf 20 6f 66 20 69 6e 70 75 74 20 72 65 67 69 73 74   of input regist
281c0 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52  ers */.  int *aR
281c1 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20  egIdx,       /* 
281c2 52 65 67 69 73 74 65 72 20 75 73 65 64 20 62 79  Register used by
281c3 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20 30 20   each index.  0 
281c4 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69 63  for unused indic
281c5 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 6f 77 69  es */.  int rowi
281c6 64 43 68 6e 67 2c 20 20 20 20 20 20 2f 2a 20 54  dChng,      /* T
281c7 72 75 65 20 69 66 20 74 68 65 20 72 6f 77 69 64  rue if the rowid
281c8 20 6d 69 67 68 74 20 63 6f 6c 6c 69 64 65 20 77   might collide w
281c9 69 74 68 20 65 78 69 73 74 69 6e 67 20 65 6e 74  ith existing ent
281ca 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55 70  ry */.  int isUp
281cb 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  date,       /* T
281cc 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c 20  rue for UPDATE, 
281cd 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 54  False for INSERT
281ce 20 2a 2f 0a 20 20 69 6e 74 20 6f 76 65 72 72 69   */.  int overri
281cf 64 65 45 72 72 6f 72 2c 20 20 2f 2a 20 4f 76 65  deError,  /* Ove
281d0 72 72 69 64 65 20 6f 6e 45 72 72 6f 72 20 74 6f  rride onError to
281d1 20 74 68 69 73 20 69 66 20 6e 6f 74 20 4f 45 5f   this if not OE_
281d2 44 65 66 61 75 6c 74 20 2a 2f 0a 20 20 69 6e 74  Default */.  int
281d3 20 69 67 6e 6f 72 65 44 65 73 74 2c 20 20 20 20   ignoreDest,    
281d4 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 69 73   /* Jump to this
281d5 20 6c 61 62 65 6c 20 6f 6e 20 61 6e 20 4f 45 5f   label on an OE_
281d6 49 67 6e 6f 72 65 20 72 65 73 6f 6c 75 74 69 6f  Ignore resolutio
281d7 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 4d 61  n */.  int *pbMa
281d8 79 52 65 70 6c 61 63 65 20 20 20 2f 2a 20 4f 55  yReplace   /* OU
281d9 54 3a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69  T: Set to true i
281da 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 6d 61 79  f constraint may
281db 20 63 61 75 73 65 20 61 20 72 65 70 6c 61 63 65   cause a replace
281dc 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20   */.){.  int i; 
281dd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
281de 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  loop counter */.
281df 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
281e0 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 75 6e        /* VDBE un
281e1 64 65 72 20 63 6f 6e 73 74 72 75 74 69 6f 6e 20  der constrution 
281e2 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20  */.  int nCol;  
281e3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
281e4 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f  er of columns */
281e5 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 3b 20  .  int onError; 
281e6 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69         /* Confli
281e7 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 73 74  ct resolution st
281e8 72 61 74 65 67 79 20 2a 2f 0a 20 20 69 6e 74 20  rategy */.  int 
281e9 6a 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  j1;             
281ea 2f 2a 20 41 64 64 72 65 73 73 73 20 6f 66 20 6a  /* Addresss of j
281eb 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ump instruction 
281ec 2a 2f 0a 20 20 69 6e 74 20 6a 32 20 3d 20 30 2c  */.  int j2 = 0,
281ed 20 6a 33 3b 20 20 20 20 20 2f 2a 20 41 64 64 72   j3;     /* Addr
281ee 65 73 73 65 73 20 6f 66 20 6a 75 6d 70 20 69 6e  esses of jump in
281ef 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20  structions */.  
281f0 69 6e 74 20 72 65 67 44 61 74 61 3b 20 20 20 20  int regData;    
281f1 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
281f2 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 72 73 74  containing first
281f3 20 64 61 74 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   data column */.
281f4 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20 20    int iCur;     
281f5 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63        /* Table c
281f6 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 2a 2f 0a  ursor number */.
281f7 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
281f8 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
281f9 72 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20  r to one of the 
281fa 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74  indices */.  int
281fb 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 30   seenReplace = 0
281fc 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 52 45 50  ; /* True if REP
281fd 4c 41 43 45 20 69 73 20 75 73 65 64 20 74 6f 20  LACE is used to 
281fe 72 65 73 6f 6c 76 65 20 49 4e 54 20 50 4b 20 63  resolve INT PK c
281ff 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74  onflict */.  int
28200 20 72 65 67 4f 6c 64 52 6f 77 69 64 20 3d 20 28   regOldRowid = (
28201 72 6f 77 69 64 43 68 6e 67 20 26 26 20 69 73 55  rowidChng && isU
28202 70 64 61 74 65 29 20 3f 20 72 6f 77 69 64 43 68  pdate) ? rowidCh
28203 6e 67 20 3a 20 72 65 67 52 6f 77 69 64 3b 0a 0a  ng : regRowid;..
28204 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
28205 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
28206 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b 0a  assert( v!=0 );.
28207 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
28208 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f  pSelect==0 );  /
28209 2a 20 54 68 69 73 20 74 61 62 6c 65 20 69 73 20  * This table is 
2820a 6e 6f 74 20 61 20 56 49 45 57 20 2a 2f 0a 20 20  not a VIEW */.  
2820b 6e 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f  nCol = pTab->nCo
2820c 6c 3b 0a 20 20 72 65 67 44 61 74 61 20 3d 20 72  l;.  regData = r
2820d 65 67 52 6f 77 69 64 20 2b 20 31 3b 0a 0a 20 20  egRowid + 1;..  
2820e 2f 2a 20 54 65 73 74 20 61 6c 6c 20 4e 4f 54 20  /* Test all NOT 
2820f 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73  NULL constraints
28210 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
28211 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
28212 20 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d      if( i==pTab-
28213 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  >iPKey ){.      
28214 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a  continue;.    }.
28215 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 70 54      onError = pT
28216 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e  ab->aCol[i].notN
28217 75 6c 6c 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45  ull;.    if( onE
28218 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 20  rror==OE_None ) 
28219 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66  continue;.    if
2821a 28 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21  ( overrideError!
2821b 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
2821c 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f       onError = o
2821d 76 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20 20  verrideError;.  
2821e 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45 72    }else if( onEr
2821f 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror==OE_Default 
28220 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
28221 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   = OE_Abort;.   
28222 20 7d 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72   }.    if( onErr
28223 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 26  or==OE_Replace &
28224 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e  & pTab->aCol[i].
28225 70 44 66 6c 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pDflt==0 ){.    
28226 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41    onError = OE_A
28227 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  bort;.    }.    
28228 61 73 73 65 72 74 28 20 6f 6e 45 72 72 6f 72 3d  assert( onError=
28229 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20  =OE_Rollback || 
2822a 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72  onError==OE_Abor
2822b 74 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45  t || onError==OE
2822c 5f 46 61 69 6c 0a 20 20 20 20 20 20 20 20 7c 7c  _Fail.        ||
2822d 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e   onError==OE_Ign
2822e 6f 72 65 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d  ore || onError==
2822f 4f 45 5f 52 65 70 6c 61 63 65 20 29 3b 0a 20 20  OE_Replace );.  
28230 20 20 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f    switch( onErro
28231 72 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  r ){.      case 
28232 4f 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20  OE_Abort:.      
28233 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72    sqlite3MayAbor
28234 74 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  t(pParse);.     
28235 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63   case OE_Rollbac
28236 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45  k:.      case OE
28237 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20  _Fail: {.       
28238 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a 20 20 20   char *zMsg;.   
28239 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65       j1 = sqlite
2823a 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2823b 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 2c 0a 20 20  P_HaltIfNull,.  
2823c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2823d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2823e 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
2823f 54 2c 20 6f 6e 45 72 72 6f 72 2c 20 72 65 67 44  T, onError, regD
28240 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20 20 20  ata+i);.        
28241 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50  zMsg = sqlite3MP
28242 72 69 6e 74 66 28 70 50 61 72 73 65 2d 3e 64 62  rintf(pParse->db
28243 2c 20 22 25 73 2e 25 73 20 6d 61 79 20 6e 6f 74  , "%s.%s may not
28244 20 62 65 20 4e 55 4c 4c 22 2c 0a 20 20 20 20 20   be NULL",.     
28245 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28246 20 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a           pTab->z
28247 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f 6c  Name, pTab->aCol
28248 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  [i].zName);.    
28249 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2824a 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 7a  hangeP4(v, -1, z
2824b 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29  Msg, P4_DYNAMIC)
2824c 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
2824d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
2824e 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b  ase OE_Ignore: {
2824f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28250 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28251 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 44 61 74 61  _IsNull, regData
28252 2b 69 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b  +i, ignoreDest);
28253 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
28254 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65        }.      de
28255 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
28256 20 61 73 73 65 72 74 28 20 6f 6e 45 72 72 6f 72   assert( onError
28257 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 3b 0a  ==OE_Replace );.
28258 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c          j1 = sql
28259 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2825a 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65  , OP_NotNull, re
2825b 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20  gData+i);.      
2825c 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2825d 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d 3e  e(pParse, pTab->
2825e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20 72  aCol[i].pDflt, r
2825f 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20  egData+i);.     
28260 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
28261 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20  mpHere(v, j1);. 
28262 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
28263 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
28264 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20 43  .  /* Test all C
28265 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73  HECK constraints
28266 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
28267 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a  LITE_OMIT_CHECK.
28268 20 20 69 66 28 20 70 54 61 62 2d 3e 70 43 68 65    if( pTab->pChe
28269 63 6b 20 26 26 20 28 70 50 61 72 73 65 2d 3e 64  ck && (pParse->d
2826a 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
2826b 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 29 3d  E_IgnoreChecks)=
2826c 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 61 6c  =0 ){.    int al
2826d 6c 4f 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62  lOk = sqlite3Vdb
2826e 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
2826f 20 20 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73     pParse->ckBas
28270 65 20 3d 20 72 65 67 44 61 74 61 3b 0a 20 20 20  e = regData;.   
28271 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72   sqlite3ExprIfTr
28272 75 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  ue(pParse, pTab-
28273 3e 70 43 68 65 63 6b 2c 20 61 6c 6c 4f 6b 2c 20  >pCheck, allOk, 
28274 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
28275 4c 29 3b 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20  L);.    onError 
28276 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21  = overrideError!
28277 3d 4f 45 5f 44 65 66 61 75 6c 74 20 3f 20 6f 76  =OE_Default ? ov
28278 65 72 72 69 64 65 45 72 72 6f 72 20 3a 20 4f 45  errideError : OE
28279 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20  _Abort;.    if( 
2827a 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f  onError==OE_Igno
2827b 72 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  re ){.      sqli
2827c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2827d 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e   OP_Goto, 0, ign
2827e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20 7d 65  oreDest);.    }e
2827f 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
28280 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74  e3HaltConstraint
28281 28 70 50 61 72 73 65 2c 20 6f 6e 45 72 72 6f 72  (pParse, onError
28282 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  , 0, 0);.    }. 
28283 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
28284 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 6c  solveLabel(v, al
28285 6c 4f 6b 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  lOk);.  }.#endif
28286 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
28287 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 29 20  ITE_OMIT_CHECK) 
28288 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68  */..  /* If we h
28289 61 76 65 20 61 6e 20 49 4e 54 45 47 45 52 20 50  ave an INTEGER P
2828a 52 49 4d 41 52 59 20 4b 45 59 2c 20 6d 61 6b 65  RIMARY KEY, make
2828b 20 73 75 72 65 20 74 68 65 20 70 72 69 6d 61 72   sure the primar
2828c 79 20 6b 65 79 0a 20 20 2a 2a 20 6f 66 20 74 68  y key.  ** of th
2828d 65 20 6e 65 77 20 72 65 63 6f 72 64 20 64 6f 65  e new record doe
2828e 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79  s not previously
2828f 20 65 78 69 73 74 2e 20 20 45 78 63 65 70 74 2c   exist.  Except,
28290 20 69 66 20 74 68 69 73 0a 20 20 2a 2a 20 69 73   if this.  ** is
28291 20 61 6e 20 55 50 44 41 54 45 20 61 6e 64 20 74   an UPDATE and t
28292 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69  he primary key i
28293 73 20 6e 6f 74 20 63 68 61 6e 67 69 6e 67 2c 20  s not changing, 
28294 74 68 61 74 20 69 73 20 4f 4b 2e 0a 20 20 2a 2f  that is OK..  */
28295 0a 20 20 69 66 28 20 72 6f 77 69 64 43 68 6e 67  .  if( rowidChng
28296 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20   ){.    onError 
28297 3d 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 3b  = pTab->keyConf;
28298 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64  .    if( overrid
28299 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75  eError!=OE_Defau
2829a 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72  lt ){.      onEr
2829b 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72  ror = overrideEr
2829c 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ror;.    }else i
2829d 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  f( onError==OE_D
2829e 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
2829f 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f  onError = OE_Abo
282a0 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20  rt;.    }.    . 
282a1 20 20 20 69 66 28 20 69 73 55 70 64 61 74 65 20     if( isUpdate 
282a2 29 7b 0a 20 20 20 20 20 20 6a 32 20 3d 20 73 71  ){.      j2 = sq
282a3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
282a4 76 2c 20 4f 50 5f 45 71 2c 20 72 65 67 52 6f 77  v, OP_Eq, regRow
282a5 69 64 2c 20 30 2c 20 72 6f 77 69 64 43 68 6e 67  id, 0, rowidChng
282a6 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 33 20  );.    }.    j3 
282a7 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
282a8 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
282a9 73 74 73 2c 20 62 61 73 65 43 75 72 2c 20 30 2c  sts, baseCur, 0,
282aa 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
282ab 73 77 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20  switch( onError 
282ac 29 7b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74  ){.      default
282ad 3a 20 7b 0a 20 20 20 20 20 20 20 20 6f 6e 45 72  : {.        onEr
282ae 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a  ror = OE_Abort;.
282af 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20          /* Fall 
282b0 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 6e 65  thru into the ne
282b1 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 20  xt case */.     
282b2 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45   }.      case OE
282b3 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20  _Rollback:.     
282b4 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a   case OE_Abort:.
282b5 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61        case OE_Fa
282b6 69 6c 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  il: {.        sq
282b7 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61  lite3HaltConstra
282b8 69 6e 74 28 0a 20 20 20 20 20 20 20 20 20 20 70  int(.          p
282b9 50 61 72 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20  Parse, onError, 
282ba 22 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73  "PRIMARY KEY mus
282bb 74 20 62 65 20 75 6e 69 71 75 65 22 2c 20 50 34  t be unique", P4
282bc 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
282bd 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
282be 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52  .      case OE_R
282bf 65 70 6c 61 63 65 3a 20 7b 0a 20 20 20 20 20 20  eplace: {.      
282c0 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
282c1 65 20 44 45 4c 45 54 45 20 74 72 69 67 67 65 72  e DELETE trigger
282c2 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 20  s on this table 
282c3 61 6e 64 20 74 68 65 0a 20 20 20 20 20 20 20 20  and the.        
282c4 2a 2a 20 72 65 63 75 72 73 69 76 65 2d 74 72 69  ** recursive-tri
282c5 67 67 65 72 73 20 66 6c 61 67 20 69 73 20 73 65  ggers flag is se
282c6 74 2c 20 63 61 6c 6c 20 47 65 6e 65 72 61 74 65  t, call Generate
282c7 52 6f 77 44 65 6c 65 74 65 28 29 20 74 6f 0a 20  RowDelete() to. 
282c8 20 20 20 20 20 20 20 2a 2a 20 72 65 6d 6f 76 65         ** remove
282c9 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67   the conflicting
282ca 20 72 6f 77 20 66 72 6f 6d 20 74 68 65 20 74 68   row from the th
282cb 65 20 74 61 62 6c 65 2e 20 54 68 69 73 20 77 69  e table. This wi
282cc 6c 6c 20 66 69 72 65 0a 20 20 20 20 20 20 20 20  ll fire.        
282cd 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72 73 20  ** the triggers 
282ce 61 6e 64 20 72 65 6d 6f 76 65 20 62 6f 74 68 20  and remove both 
282cf 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e  the table and in
282d0 64 65 78 20 62 2d 74 72 65 65 20 65 6e 74 72 69  dex b-tree entri
282d1 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  es..        **. 
282d2 20 20 20 20 20 20 20 2a 2a 20 4f 74 68 65 72 77         ** Otherw
282d3 69 73 65 2c 20 69 66 20 74 68 65 72 65 20 61 72  ise, if there ar
282d4 65 20 6e 6f 20 74 72 69 67 67 65 72 73 20 6f 72  e no triggers or
282d5 20 74 68 65 20 72 65 63 75 72 73 69 76 65 2d 74   the recursive-t
282d6 72 69 67 67 65 72 73 0a 20 20 20 20 20 20 20 20  riggers.        
282d7 2a 2a 20 66 6c 61 67 20 69 73 20 6e 6f 74 20 73  ** flag is not s
282d8 65 74 2c 20 63 61 6c 6c 20 47 65 6e 65 72 61 74  et, call Generat
282d9 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28  eRowIndexDelete(
282da 29 2e 20 54 68 69 73 20 72 65 6d 6f 76 65 73 0a  ). This removes.
282db 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 69          ** the i
282dc 6e 64 65 78 20 62 2d 74 72 65 65 20 65 6e 74 72  ndex b-tree entr
282dd 69 65 73 20 6f 6e 6c 79 2e 20 54 68 65 20 74 61  ies only. The ta
282de 62 6c 65 20 62 2d 74 72 65 65 20 65 6e 74 72 79  ble b-tree entry
282df 20 77 69 6c 6c 20 62 65 20 0a 20 20 20 20 20 20   will be .      
282e0 20 20 2a 2a 20 72 65 70 6c 61 63 65 64 20 62 79    ** replaced by
282e1 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 77   the new entry w
282e2 68 65 6e 20 69 74 20 69 73 20 69 6e 73 65 72 74  hen it is insert
282e3 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  ed.  */.        
282e4 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
282e5 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69  r = 0;.        i
282e6 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66  f( pParse->db->f
282e7 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65 63 54  lags&SQLITE_RecT
282e8 72 69 67 67 65 72 73 20 29 7b 0a 20 20 20 20 20  riggers ){.     
282e9 20 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20       pTrigger = 
282ea 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45  sqlite3TriggersE
282eb 78 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61  xist(pParse, pTa
282ec 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c  b, TK_DELETE, 0,
282ed 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   0);.        }. 
282ee 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 75         sqlite3Mu
282ef 6c 74 69 57 72 69 74 65 28 70 50 61 72 73 65 29  ltiWrite(pParse)
282f0 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54  ;.        if( pT
282f1 72 69 67 67 65 72 20 7c 7c 20 73 71 6c 69 74 65  rigger || sqlite
282f2 33 46 6b 52 65 71 75 69 72 65 64 28 70 50 61 72  3FkRequired(pPar
282f3 73 65 2c 20 70 54 61 62 2c 20 30 2c 20 30 29 20  se, pTab, 0, 0) 
282f4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
282f5 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44  ite3GenerateRowD
282f6 65 6c 65 74 65 28 0a 20 20 20 20 20 20 20 20 20  elete(.         
282f7 20 20 20 20 20 70 50 61 72 73 65 2c 20 70 54 61       pParse, pTa
282f8 62 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67 52  b, baseCur, regR
282f9 6f 77 69 64 2c 20 30 2c 20 70 54 72 69 67 67 65  owid, 0, pTrigge
282fa 72 2c 20 4f 45 5f 52 65 70 6c 61 63 65 0a 20 20  r, OE_Replace.  
282fb 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
282fc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
282fd 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72      sqlite3Gener
282fe 61 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74  ateRowIndexDelet
282ff 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
28300 62 61 73 65 43 75 72 2c 20 30 29 3b 0a 20 20 20  baseCur, 0);.   
28301 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
28302 65 65 6e 52 65 70 6c 61 63 65 20 3d 20 31 3b 0a  eenReplace = 1;.
28303 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
28304 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
28305 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20  e OE_Ignore: {. 
28306 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73         assert( s
28307 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b  eenReplace==0 );
28308 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28309 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2830a 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65  _Goto, 0, ignore
2830b 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62  Dest);.        b
2830c 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2830d 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
2830e 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
2830f 33 29 3b 0a 20 20 20 20 69 66 28 20 69 73 55 70  3);.    if( isUp
28310 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 73 71  date ){.      sq
28311 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
28312 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 7d 0a  e(v, j2);.    }.
28313 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61    }..  /* Test a
28314 6c 6c 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  ll UNIQUE constr
28315 61 69 6e 74 73 20 62 79 20 63 72 65 61 74 69 6e  aints by creatin
28316 67 20 65 6e 74 72 69 65 73 20 66 6f 72 20 65 61  g entries for ea
28317 63 68 20 55 4e 49 51 55 45 0a 20 20 2a 2a 20 69  ch UNIQUE.  ** i
28318 6e 64 65 78 20 61 6e 64 20 6d 61 6b 69 6e 67 20  ndex and making 
28319 73 75 72 65 20 74 68 61 74 20 64 75 70 6c 69 63  sure that duplic
2831a 61 74 65 20 65 6e 74 72 69 65 73 20 64 6f 20 6e  ate entries do n
2831b 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ot already exist
2831c 2e 0a 20 20 2a 2a 20 41 64 64 20 74 68 65 20 6e  ..  ** Add the n
2831d 65 77 20 72 65 63 6f 72 64 73 20 74 6f 20 74 68  ew records to th
2831e 65 20 69 6e 64 69 63 65 73 20 61 73 20 77 65 20  e indices as we 
2831f 67 6f 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  go..  */.  for(i
28320 43 75 72 3d 30 2c 20 70 49 64 78 3d 70 54 61 62  Cur=0, pIdx=pTab
28321 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
28322 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
28323 2c 20 69 43 75 72 2b 2b 29 7b 0a 20 20 20 20 69  , iCur++){.    i
28324 6e 74 20 72 65 67 49 64 78 3b 0a 20 20 20 20 69  nt regIdx;.    i
28325 6e 74 20 72 65 67 52 3b 0a 0a 20 20 20 20 69 66  nt regR;..    if
28326 28 20 61 52 65 67 49 64 78 5b 69 43 75 72 5d 3d  ( aRegIdx[iCur]=
28327 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20  =0 ) continue;  
28328 2f 2a 20 53 6b 69 70 20 75 6e 75 73 65 64 20 69  /* Skip unused i
28329 6e 64 69 63 65 73 20 2a 2f 0a 0a 20 20 20 20 2f  ndices */..    /
2832a 2a 20 43 72 65 61 74 65 20 61 20 6b 65 79 20 66  * Create a key f
2832b 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65  or accessing the
2832c 20 69 6e 64 65 78 20 65 6e 74 72 79 20 2a 2f 0a   index entry */.
2832d 20 20 20 20 72 65 67 49 64 78 20 3d 20 73 71 6c      regIdx = sql
2832e 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
2832f 28 70 50 61 72 73 65 2c 20 70 49 64 78 2d 3e 6e  (pParse, pIdx->n
28330 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 20 20 20 20 66  Column+1);.    f
28331 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e  or(i=0; i<pIdx->
28332 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
28333 20 20 20 20 20 69 6e 74 20 69 64 78 20 3d 20 70       int idx = p
28334 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
28335 3b 0a 20 20 20 20 20 20 69 66 28 20 69 64 78 3d  ;.      if( idx=
28336 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a  =pTab->iPKey ){.
28337 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
28338 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
28339 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69 64 2c  SCopy, regRowid,
2833a 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20 20 20   regIdx+i);.    
2833b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2833c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2833d 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
2833e 72 65 67 44 61 74 61 2b 69 64 78 2c 20 72 65 67  regData+idx, reg
2833f 49 64 78 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a  Idx+i);.      }.
28340 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
28341 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
28342 50 5f 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69  P_SCopy, regRowi
28343 64 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20  d, regIdx+i);.  
28344 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28345 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
28346 63 6f 72 64 2c 20 72 65 67 49 64 78 2c 20 70 49  cord, regIdx, pI
28347 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 2c 20 61  dx->nColumn+1, a
28348 52 65 67 49 64 78 5b 69 43 75 72 5d 29 3b 0a 20  RegIdx[iCur]);. 
28349 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2834a 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 73 71  angeP4(v, -1, sq
2834b 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69  lite3IndexAffini
2834c 74 79 53 74 72 28 76 2c 20 70 49 64 78 29 2c 20  tyStr(v, pIdx), 
2834d 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  0);.    sqlite3E
2834e 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
2834f 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
28350 65 67 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f  egIdx, pIdx->nCo
28351 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a  lumn+1);..    /*
28352 20 46 69 6e 64 20 6f 75 74 20 77 68 61 74 20 61   Find out what a
28353 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 69 6e  ction to take in
28354 20 63 61 73 65 20 74 68 65 72 65 20 69 73 20 61   case there is a
28355 6e 20 69 6e 64 65 78 69 6e 67 20 63 6f 6e 66 6c  n indexing confl
28356 69 63 74 20 2a 2f 0a 20 20 20 20 6f 6e 45 72 72  ict */.    onErr
28357 6f 72 20 3d 20 70 49 64 78 2d 3e 6f 6e 45 72 72  or = pIdx->onErr
28358 6f 72 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72  or;.    if( onEr
28359 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 20  ror==OE_None ){ 
2835a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
2835b 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70  leaseTempRange(p
2835c 50 61 72 73 65 2c 20 72 65 67 49 64 78 2c 20 70  Parse, regIdx, p
2835d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b  Idx->nColumn+1);
2835e 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
2835f 20 20 2f 2a 20 70 49 64 78 20 69 73 20 6e 6f 74    /* pIdx is not
28360 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 20   a UNIQUE index 
28361 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  */.    }.    if(
28362 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 21 3d   overrideError!=
28363 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20  OE_Default ){.  
28364 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76      onError = ov
28365 65 72 72 69 64 65 45 72 72 6f 72 3b 0a 20 20 20  errideError;.   
28366 20 7d 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72   }else if( onErr
28367 6f 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29  or==OE_Default )
28368 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20  {.      onError 
28369 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20  = OE_Abort;.    
2836a 7d 0a 20 20 20 20 69 66 28 20 73 65 65 6e 52 65  }.    if( seenRe
2836b 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20 69  place ){.      i
2836c 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49  f( onError==OE_I
2836d 67 6e 6f 72 65 20 29 20 6f 6e 45 72 72 6f 72 20  gnore ) onError 
2836e 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 0a 20 20  = OE_Replace;.  
2836f 20 20 20 20 65 6c 73 65 20 69 66 28 20 6f 6e 45      else if( onE
28370 72 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 20 29 20  rror==OE_Fail ) 
28371 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f  onError = OE_Abo
28372 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20  rt;.    }.    . 
28373 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73     /* Check to s
28374 65 65 20 69 66 20 74 68 65 20 6e 65 77 20 69 6e  ee if the new in
28375 64 65 78 20 65 6e 74 72 79 20 77 69 6c 6c 20 62  dex entry will b
28376 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20 20 20  e unique */.    
28377 72 65 67 52 20 3d 20 73 71 6c 69 74 65 33 47 65  regR = sqlite3Ge
28378 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
28379 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2837a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
2837b 6f 70 79 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  opy, regOldRowid
2837c 2c 20 72 65 67 52 29 3b 0a 20 20 20 20 6a 33 20  , regR);.    j3 
2837d 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2837e 4f 70 34 28 76 2c 20 4f 50 5f 49 73 55 6e 69 71  Op4(v, OP_IsUniq
2837f 75 65 2c 20 62 61 73 65 43 75 72 2b 69 43 75 72  ue, baseCur+iCur
28380 2b 31 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  +1, 0,.         
28381 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28382 20 20 72 65 67 52 2c 20 53 51 4c 49 54 45 5f 49    regR, SQLITE_I
28383 4e 54 5f 54 4f 5f 50 54 52 28 72 65 67 49 64 78  NT_TO_PTR(regIdx
28384 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
28385 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 34                P4
28386 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 73 71 6c  _INT32);.    sql
28387 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
28388 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
28389 49 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  Idx, pIdx->nColu
2838a 6d 6e 2b 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 47  mn+1);..    /* G
2838b 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
2838c 74 20 65 78 65 63 75 74 65 73 20 69 66 20 74 68  t executes if th
2838d 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72  e new index entr
2838e 79 20 69 73 20 6e 6f 74 20 75 6e 69 71 75 65 20  y is not unique 
2838f 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f  */.    assert( o
28390 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62  nError==OE_Rollb
28391 61 63 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d  ack || onError==
28392 4f 45 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72  OE_Abort || onEr
28393 72 6f 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20  ror==OE_Fail.   
28394 20 20 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d       || onError=
28395 3d 4f 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e  =OE_Ignore || on
28396 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
28397 65 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28  e );.    switch(
28398 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20   onError ){.    
28399 20 20 63 61 73 65 20 4f 45 5f 52 6f 6c 6c 62 61    case OE_Rollba
2839a 63 6b 3a 0a 20 20 20 20 20 20 63 61 73 65 20 4f  ck:.      case O
2839b 45 5f 41 62 6f 72 74 3a 0a 20 20 20 20 20 20 63  E_Abort:.      c
2839c 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20  ase OE_Fail: {. 
2839d 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20         int j;.  
2839e 20 20 20 20 20 20 53 74 72 41 63 63 75 6d 20 65        StrAccum e
2839f 72 72 4d 73 67 3b 0a 20 20 20 20 20 20 20 20 63  rrMsg;.        c
283a0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70 3b  onst char *zSep;
283a1 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
283a2 45 72 72 3b 0a 0a 20 20 20 20 20 20 20 20 73 71  Err;..        sq
283a3 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69  lite3StrAccumIni
283a4 74 28 26 65 72 72 4d 73 67 2c 20 30 2c 20 30 2c  t(&errMsg, 0, 0,
283a5 20 32 30 30 29 3b 0a 20 20 20 20 20 20 20 20 65   200);.        e
283a6 72 72 4d 73 67 2e 64 62 20 3d 20 70 50 61 72 73  rrMsg.db = pPars
283a7 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 7a  e->db;.        z
283a8 53 65 70 20 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c  Sep = pIdx->nCol
283a9 75 6d 6e 3e 31 20 3f 20 22 63 6f 6c 75 6d 6e 73  umn>1 ? "columns
283aa 20 22 20 3a 20 22 63 6f 6c 75 6d 6e 20 22 3b 0a   " : "column ";.
283ab 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
283ac 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   j<pIdx->nColumn
283ad 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
283ae 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 70    char *zCol = p
283af 54 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e  Tab->aCol[pIdx->
283b0 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 5d 2e 7a 4e 61  aiColumn[j]].zNa
283b1 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  me;.          sq
283b2 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
283b3 65 6e 64 28 26 65 72 72 4d 73 67 2c 20 7a 53 65  end(&errMsg, zSe
283b4 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  p, -1);.        
283b5 20 20 7a 53 65 70 20 3d 20 22 2c 20 22 3b 0a 20    zSep = ", ";. 
283b6 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
283b7 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26  StrAccumAppend(&
283b8 65 72 72 4d 73 67 2c 20 7a 43 6f 6c 2c 20 2d 31  errMsg, zCol, -1
283b9 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
283ba 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
283bb 63 63 75 6d 41 70 70 65 6e 64 28 26 65 72 72 4d  ccumAppend(&errM
283bc 73 67 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  sg,.            
283bd 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20  pIdx->nColumn>1 
283be 3f 20 22 20 61 72 65 20 6e 6f 74 20 75 6e 69 71  ? " are not uniq
283bf 75 65 22 20 3a 20 22 20 69 73 20 6e 6f 74 20 75  ue" : " is not u
283c0 6e 69 71 75 65 22 2c 20 2d 31 29 3b 0a 20 20 20  nique", -1);.   
283c1 20 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69       zErr = sqli
283c2 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73  te3StrAccumFinis
283c3 68 28 26 65 72 72 4d 73 67 29 3b 0a 20 20 20 20  h(&errMsg);.    
283c4 20 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43      sqlite3HaltC
283c5 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73 65  onstraint(pParse
283c6 2c 20 6f 6e 45 72 72 6f 72 2c 20 7a 45 72 72 2c  , onError, zErr,
283c7 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
283c8 69 74 65 33 44 62 46 72 65 65 28 65 72 72 4d 73  ite3DbFree(errMs
283c9 67 2e 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  g.db, zErr);.   
283ca 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
283cb 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f    }.      case O
283cc 45 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 20  E_Ignore: {.    
283cd 20 20 20 20 61 73 73 65 72 74 28 20 73 65 65 6e      assert( seen
283ce 52 65 70 6c 61 63 65 3d 3d 30 20 29 3b 0a 20 20  Replace==0 );.  
283cf 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
283d0 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
283d1 74 6f 2c 20 30 2c 20 69 67 6e 6f 72 65 44 65 73  to, 0, ignoreDes
283d2 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  t);.        brea
283d3 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
283d4 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
283d5 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 54 72      Trigger *pTr
283d6 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 20 20 20  igger = 0;.     
283d7 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72 72     assert( onErr
283d8 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29  or==OE_Replace )
283d9 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
283da 33 4d 75 6c 74 69 57 72 69 74 65 28 70 50 61 72  3MultiWrite(pPar
283db 73 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  se);.        if(
283dc 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61   pParse->db->fla
283dd 67 73 26 53 51 4c 49 54 45 5f 52 65 63 54 72 69  gs&SQLITE_RecTri
283de 67 67 65 72 73 20 29 7b 0a 20 20 20 20 20 20 20  ggers ){.       
283df 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 73 71     pTrigger = sq
283e0 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69  lite3TriggersExi
283e1 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  st(pParse, pTab,
283e2 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 2c 20 30   TK_DELETE, 0, 0
283e3 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
283e4 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65       sqlite3Gene
283e5 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 0a 20  rateRowDelete(. 
283e6 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
283e7 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75 72  e, pTab, baseCur
283e8 2c 20 72 65 67 52 2c 20 30 2c 20 70 54 72 69 67  , regR, 0, pTrig
283e9 67 65 72 2c 20 4f 45 5f 52 65 70 6c 61 63 65 0a  ger, OE_Replace.
283ea 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
283eb 20 20 20 73 65 65 6e 52 65 70 6c 61 63 65 20 3d     seenReplace =
283ec 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   1;.        brea
283ed 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
283ee 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
283ef 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b  JumpHere(v, j3);
283f0 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
283f1 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
283f2 65 2c 20 72 65 67 52 29 3b 0a 20 20 7d 0a 20 20  e, regR);.  }.  
283f3 0a 20 20 69 66 28 20 70 62 4d 61 79 52 65 70 6c  .  if( pbMayRepl
283f4 61 63 65 20 29 7b 0a 20 20 20 20 2a 70 62 4d 61  ace ){.    *pbMa
283f5 79 52 65 70 6c 61 63 65 20 3d 20 73 65 65 6e 52  yReplace = seenR
283f6 65 70 6c 61 63 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f  eplace;.  }.}../
283f7 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
283f8 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65  e generates code
283f9 20 74 6f 20 66 69 6e 69 73 68 20 74 68 65 20 49   to finish the I
283fa 4e 53 45 52 54 20 6f 72 20 55 50 44 41 54 45 20  NSERT or UPDATE 
283fb 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 74 68 61  operation.** tha
283fc 74 20 77 61 73 20 73 74 61 72 74 65 64 20 62 79  t was started by
283fd 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f   a prior call to
283fe 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
283ff 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73  ConstraintChecks
28400 2e 0a 2a 2a 20 41 20 63 6f 6e 73 65 63 75 74 69  ..** A consecuti
28401 76 65 20 72 61 6e 67 65 20 6f 66 20 72 65 67 69  ve range of regi
28402 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61  sters starting a
28403 74 20 72 65 67 52 6f 77 69 64 20 63 6f 6e 74 61  t regRowid conta
28404 69 6e 73 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64  ins the.** rowid
28405 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e 74   and the content
28406 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 2e   to be inserted.
28407 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  .**.** The argum
28408 65 6e 74 73 20 74 6f 20 74 68 69 73 20 72 6f 75  ents to this rou
28409 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20 74  tine should be t
2840a 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 66  he same as the f
2840b 69 72 73 74 20 73 69 78 0a 2a 2a 20 61 72 67 75  irst six.** argu
2840c 6d 65 6e 74 73 20 74 6f 20 73 71 6c 69 74 65 33  ments to sqlite3
2840d 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69  GenerateConstrai
2840e 6e 74 43 68 65 63 6b 73 2e 0a 2a 2f 0a 53 51 4c  ntChecks..*/.SQL
2840f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
28410 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65   sqlite3Complete
28411 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20 50 61 72  Insertion(.  Par
28412 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
28413 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
28414 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
28415 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
28416 2f 2a 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74  /* the table int
28417 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69  o which we are i
28418 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 69 6e  nserting */.  in
28419 74 20 62 61 73 65 43 75 72 2c 20 20 20 20 20 20  t baseCur,      
2841a 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 61 20    /* Index of a 
2841b 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
2841c 72 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 70 54  r pointing at pT
2841d 61 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52  ab */.  int regR
2841e 6f 77 69 64 2c 20 20 20 20 20 20 20 2f 2a 20 52  owid,       /* R
2841f 61 6e 67 65 20 6f 66 20 63 6f 6e 74 65 6e 74 20  ange of content 
28420 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64  */.  int *aRegId
28421 78 2c 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  x,       /* Regi
28422 73 74 65 72 20 75 73 65 64 20 62 79 20 65 61 63  ster used by eac
28423 68 20 69 6e 64 65 78 2e 20 20 30 20 66 6f 72 20  h index.  0 for 
28424 75 6e 75 73 65 64 20 69 6e 64 69 63 65 73 20 2a  unused indices *
28425 2f 0a 20 20 69 6e 74 20 69 73 55 70 64 61 74 65  /.  int isUpdate
28426 2c 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  ,       /* True 
28427 66 6f 72 20 55 50 44 41 54 45 2c 20 46 61 6c 73  for UPDATE, Fals
28428 65 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f 0a  e for INSERT */.
28429 20 20 69 6e 74 20 61 70 70 65 6e 64 42 69 61 73    int appendBias
2842a 2c 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ,     /* True if
2842b 20 74 68 69 73 20 69 73 20 6c 69 6b 65 6c 79 20   this is likely 
2842c 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e 64 20  to be an append 
2842d 2a 2f 0a 20 20 69 6e 74 20 75 73 65 53 65 65 6b  */.  int useSeek
2842e 52 65 73 75 6c 74 20 20 20 2f 2a 20 54 72 75 65  Result   /* True
2842f 20 74 6f 20 73 65 74 20 74 68 65 20 55 53 45 53   to set the USES
28430 45 45 4b 52 45 53 55 4c 54 20 66 6c 61 67 20 6f  EEKRESULT flag o
28431 6e 20 4f 50 5f 5b 49 64 78 5d 49 6e 73 65 72 74  n OP_[Idx]Insert
28432 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
28433 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74    Vdbe *v;.  int
28434 20 6e 49 64 78 3b 0a 20 20 49 6e 64 65 78 20 2a   nIdx;.  Index *
28435 70 49 64 78 3b 0a 20 20 75 38 20 70 69 6b 5f 66  pIdx;.  u8 pik_f
28436 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72 65 67 44  lags;.  int regD
28437 61 74 61 3b 0a 20 20 69 6e 74 20 72 65 67 52 65  ata;.  int regRe
28438 63 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  c;..  v = sqlite
28439 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
2843a 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30  ;.  assert( v!=0
2843b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
2843c 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29  ab->pSelect==0 )
2843d 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62 6c 65  ;  /* This table
2843e 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57 20 2a   is not a VIEW *
2843f 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d 30 2c 20  /.  for(nIdx=0, 
28440 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
28441 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
28442 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49 64 78 2b  dx->pNext, nIdx+
28443 2b 29 7b 7d 0a 20 20 66 6f 72 28 69 3d 6e 49 64  +){}.  for(i=nId
28444 78 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b  x-1; i>=0; i--){
28445 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64 78  .    if( aRegIdx
28446 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  [i]==0 ) continu
28447 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  e;.    sqlite3Vd
28448 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
28449 64 78 49 6e 73 65 72 74 2c 20 62 61 73 65 43 75  dxInsert, baseCu
2844a 72 2b 69 2b 31 2c 20 61 52 65 67 49 64 78 5b 69  r+i+1, aRegIdx[i
2844b 5d 29 3b 0a 20 20 20 20 69 66 28 20 75 73 65 53  ]);.    if( useS
2844c 65 65 6b 52 65 73 75 6c 74 20 29 7b 0a 20 20 20  eekResult ){.   
2844d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2844e 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
2844f 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 3b  _USESEEKRESULT);
28450 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 67  .    }.  }.  reg
28451 44 61 74 61 20 3d 20 72 65 67 52 6f 77 69 64 20  Data = regRowid 
28452 2b 20 31 3b 0a 20 20 72 65 67 52 65 63 20 3d 20  + 1;.  regRec = 
28453 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
28454 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c  g(pParse);.  sql
28455 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
28456 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
28457 20 72 65 67 44 61 74 61 2c 20 70 54 61 62 2d 3e   regData, pTab->
28458 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0a 20  nCol, regRec);. 
28459 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66   sqlite3TableAff
2845a 69 6e 69 74 79 53 74 72 28 76 2c 20 70 54 61 62  inityStr(v, pTab
2845b 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2845c 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
2845d 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 44  nge(pParse, regD
2845e 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  ata, pTab->nCol)
2845f 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
28460 6e 65 73 74 65 64 20 29 7b 0a 20 20 20 20 70 69  nested ){.    pi
28461 6b 5f 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 7d  k_flags = 0;.  }
28462 65 6c 73 65 7b 0a 20 20 20 20 70 69 6b 5f 66 6c  else{.    pik_fl
28463 61 67 73 20 3d 20 4f 50 46 4c 41 47 5f 4e 43 48  ags = OPFLAG_NCH
28464 41 4e 47 45 3b 0a 20 20 20 20 70 69 6b 5f 66 6c  ANGE;.    pik_fl
28465 61 67 73 20 7c 3d 20 28 69 73 55 70 64 61 74 65  ags |= (isUpdate
28466 3f 4f 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45  ?OPFLAG_ISUPDATE
28467 3a 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49  :OPFLAG_LASTROWI
28468 44 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 61 70  D);.  }.  if( ap
28469 70 65 6e 64 42 69 61 73 20 29 7b 0a 20 20 20 20  pendBias ){.    
2846a 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46  pik_flags |= OPF
2846b 4c 41 47 5f 41 50 50 45 4e 44 3b 0a 20 20 7d 0a  LAG_APPEND;.  }.
2846c 20 20 69 66 28 20 75 73 65 53 65 65 6b 52 65 73    if( useSeekRes
2846d 75 6c 74 20 29 7b 0a 20 20 20 20 70 69 6b 5f 66  ult ){.    pik_f
2846e 6c 61 67 73 20 7c 3d 20 4f 50 46 4c 41 47 5f 55  lags |= OPFLAG_U
2846f 53 45 53 45 45 4b 52 45 53 55 4c 54 3b 0a 20 20  SESEEKRESULT;.  
28470 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  }.  sqlite3VdbeA
28471 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
28472 72 74 2c 20 62 61 73 65 43 75 72 2c 20 72 65 67  rt, baseCur, reg
28473 52 65 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  Rec, regRowid);.
28474 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 6e    if( !pParse->n
28475 65 73 74 65 64 20 29 7b 0a 20 20 20 20 73 71 6c  ested ){.    sql
28476 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
28477 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 4e  (v, -1, pTab->zN
28478 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ame, P4_STATIC);
28479 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
2847a 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 70 69  beChangeP5(v, pi
2847b 6b 5f 66 6c 61 67 73 29 3b 0a 7d 0a 0a 2f 2a 0a  k_flags);.}../*.
2847c 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
2847d 20 74 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20   that will open 
2847e 63 75 72 73 6f 72 73 20 66 6f 72 20 61 20 74 61  cursors for a ta
2847f 62 6c 65 20 61 6e 64 20 66 6f 72 20 61 6c 6c 0a  ble and for all.
28480 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66 20 74 68  ** indices of th
28481 61 74 20 74 61 62 6c 65 2e 20 20 54 68 65 20 22  at table.  The "
28482 62 61 73 65 43 75 72 22 20 70 61 72 61 6d 65 74  baseCur" paramet
28483 65 72 20 69 73 20 74 68 65 20 63 75 72 73 6f 72  er is the cursor
28484 20 6e 75 6d 62 65 72 20 75 73 65 64 0a 2a 2a 20   number used.** 
28485 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 20  for the table.  
28486 49 6e 64 69 63 65 73 20 61 72 65 20 6f 70 65 6e  Indices are open
28487 65 64 20 6f 6e 20 73 75 62 73 65 71 75 65 6e 74  ed on subsequent
28488 20 63 75 72 73 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20   cursors..**.** 
28489 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
2848a 72 20 6f 66 20 69 6e 64 69 63 65 73 20 6f 6e 20  r of indices on 
2848b 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  the table..*/.SQ
2848c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2848d 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c   sqlite3OpenTabl
2848e 65 41 6e 64 49 6e 64 69 63 65 73 28 0a 20 20 50  eAndIndices(.  P
2848f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
28490 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
28491 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  xt */.  Table *p
28492 54 61 62 2c 20 20 20 20 20 2f 2a 20 54 61 62 6c  Tab,     /* Tabl
28493 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a  e to be opened *
28494 2f 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72 2c  /.  int baseCur,
28495 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
28496 75 6d 62 65 72 20 61 73 73 69 67 6e 65 64 20 74  umber assigned t
28497 6f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  o the table */. 
28498 20 69 6e 74 20 6f 70 20 20 20 20 20 20 20 20 20   int op         
28499 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e 52 65 61 64    /* OP_OpenRead
2849a 20 6f 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65   or OP_OpenWrite
2849b 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
2849c 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 49 6e 64    int iDb;.  Ind
2849d 65 78 20 2a 70 49 64 78 3b 0a 20 20 56 64 62 65  ex *pIdx;.  Vdbe
2849e 20 2a 76 3b 0a 0a 20 20 69 66 28 20 49 73 56 69   *v;..  if( IsVi
2849f 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65  rtual(pTab) ) re
284a0 74 75 72 6e 20 30 3b 0a 20 20 69 44 62 20 3d 20  turn 0;.  iDb = 
284a1 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
284a2 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
284a3 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
284a4 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
284a5 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
284a6 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
284a7 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  .  sqlite3OpenTa
284a8 62 6c 65 28 70 50 61 72 73 65 2c 20 62 61 73 65  ble(pParse, base
284a9 43 75 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20  Cur, iDb, pTab, 
284aa 6f 70 29 3b 0a 20 20 66 6f 72 28 69 3d 31 2c 20  op);.  for(i=1, 
284ab 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
284ac 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
284ad 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b  dx->pNext, i++){
284ae 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  .    KeyInfo *pK
284af 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
284b0 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
284b1 20 70 49 64 78 29 3b 0a 20 20 20 20 61 73 73 65   pIdx);.    asse
284b2 72 74 28 20 70 49 64 78 2d 3e 70 53 63 68 65 6d  rt( pIdx->pSchem
284b3 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
284b4 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
284b5 64 62 65 41 64 64 4f 70 34 28 76 2c 20 6f 70 2c  dbeAddOp4(v, op,
284b6 20 69 2b 62 61 73 65 43 75 72 2c 20 70 49 64 78   i+baseCur, pIdx
284b7 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20  ->tnum, iDb,.   
284b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284b9 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20     (char*)pKey, 
284ba 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
284bb 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  FF);.    VdbeCom
284bc 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70  ment((v, "%s", p
284bd 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20  Idx->zName));.  
284be 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  }.  if( pParse->
284bf 6e 54 61 62 3c 62 61 73 65 43 75 72 2b 69 20 29  nTab<baseCur+i )
284c0 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  {.    pParse->nT
284c1 61 62 20 3d 20 62 61 73 65 43 75 72 2b 69 3b 0a  ab = baseCur+i;.
284c2 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 2d 31    }.  return i-1
284c3 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c  ;.}...#ifdef SQL
284c4 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54  ITE_TEST./*.** T
284c5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f  he following glo
284c6 62 61 6c 20 76 61 72 69 61 62 6c 65 20 69 73 20  bal variable is 
284c7 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65 6e  incremented when
284c8 65 76 65 72 20 74 68 65 0a 2a 2a 20 74 72 61 6e  ever the.** tran
284c9 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  sfer optimizatio
284ca 6e 20 69 73 20 75 73 65 64 2e 20 20 54 68 69 73  n is used.  This
284cb 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73   is used for tes
284cc 74 69 6e 67 0a 2a 2a 20 70 75 72 70 6f 73 65 73  ting.** purposes
284cd 20 6f 6e 6c 79 20 2d 20 74 6f 20 6d 61 6b 65 20   only - to make 
284ce 73 75 72 65 20 74 68 65 20 74 72 61 6e 73 66 65  sure the transfe
284cf 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 72  r optimization r
284d0 65 61 6c 6c 79 0a 2a 2a 20 69 73 20 68 61 70 70  eally.** is happ
284d1 65 6e 69 6e 67 20 77 68 65 6e 20 69 74 20 69 73  ening when it is
284d2 20 73 75 70 70 6f 73 65 20 74 6f 2e 0a 2a 2f 0a   suppose to..*/.
284d3 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
284d4 71 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63  qlite3_xferopt_c
284d5 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  ount;.#endif /* 
284d6 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a  SQLITE_TEST */..
284d7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
284d8 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 0a 2f 2a  OMIT_XFER_OPT./*
284d9 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 63 6f 6c  .** Check to col
284da 6c 61 74 69 6f 6e 20 6e 61 6d 65 73 20 74 6f 20  lation names to 
284db 73 65 65 20 69 66 20 74 68 65 79 20 61 72 65 20  see if they are 
284dc 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2f 0a 73  compatible..*/.s
284dd 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 43 6f  tatic int xferCo
284de 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74 69 6f  mpatibleCollatio
284df 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31  n(const char *z1
284e0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32  , const char *z2
284e1 29 7b 0a 20 20 69 66 28 20 7a 31 3d 3d 30 20 29  ){.  if( z1==0 )
284e2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 32 3d  {.    return z2=
284e3 3d 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 32  =0;.  }.  if( z2
284e4 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
284e5 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 0;.  }.  retur
284e6 6e 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  n sqlite3StrICmp
284e7 28 7a 31 2c 20 7a 32 29 3d 3d 30 3b 0a 7d 0a 0a  (z1, z2)==0;.}..
284e8 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
284e9 73 65 65 20 69 66 20 69 6e 64 65 78 20 70 53 72  see if index pSr
284ea 63 20 69 73 20 63 6f 6d 70 61 74 69 62 6c 65 20  c is compatible 
284eb 61 73 20 61 20 73 6f 75 72 63 65 20 6f 66 20 64  as a source of d
284ec 61 74 61 0a 2a 2a 20 66 6f 72 20 69 6e 64 65 78  ata.** for index
284ed 20 70 44 65 73 74 20 69 6e 20 61 6e 20 69 6e 73   pDest in an ins
284ee 65 72 74 20 74 72 61 6e 73 66 65 72 20 6f 70 74  ert transfer opt
284ef 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20  imization.  The 
284f0 72 75 6c 65 73 0a 2a 2a 20 66 6f 72 20 61 20 63  rules.** for a c
284f1 6f 6d 70 61 74 69 62 6c 65 20 69 6e 64 65 78 3a  ompatible index:
284f2 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 20 54 68  .**.**    *   Th
284f3 65 20 69 6e 64 65 78 20 69 73 20 6f 76 65 72 20  e index is over 
284f4 74 68 65 20 73 61 6d 65 20 73 65 74 20 6f 66 20  the same set of 
284f5 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20  columns.**    * 
284f6 20 20 54 68 65 20 73 61 6d 65 20 44 45 53 43 20    The same DESC 
284f7 61 6e 64 20 41 53 43 20 6d 61 72 6b 69 6e 67 73  and ASC markings
284f8 20 6f 63 63 75 72 73 20 6f 6e 20 61 6c 6c 20 63   occurs on all c
284f9 6f 6c 75 6d 6e 73 0a 2a 2a 20 20 20 20 2a 20 20  olumns.**    *  
284fa 20 54 68 65 20 73 61 6d 65 20 6f 6e 45 72 72 6f   The same onErro
284fb 72 20 70 72 6f 63 65 73 73 69 6e 67 20 28 4f 45  r processing (OE
284fc 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f 72  _Abort, OE_Ignor
284fd 65 2c 20 65 74 63 29 0a 2a 2a 20 20 20 20 2a 20  e, etc).**    * 
284fe 20 20 54 68 65 20 73 61 6d 65 20 63 6f 6c 6c 61    The same colla
284ff 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f 6e  ting sequence on
28500 20 65 61 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a   each column.*/.
28501 73 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 43  static int xferC
28502 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28 49  ompatibleIndex(I
28503 6e 64 65 78 20 2a 70 44 65 73 74 2c 20 49 6e 64  ndex *pDest, Ind
28504 65 78 20 2a 70 53 72 63 29 7b 0a 20 20 69 6e 74  ex *pSrc){.  int
28505 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   i;.  assert( pD
28506 65 73 74 20 26 26 20 70 53 72 63 20 29 3b 0a 20  est && pSrc );. 
28507 20 61 73 73 65 72 74 28 20 70 44 65 73 74 2d 3e   assert( pDest->
28508 70 54 61 62 6c 65 21 3d 70 53 72 63 2d 3e 70 54  pTable!=pSrc->pT
28509 61 62 6c 65 20 29 3b 0a 20 20 69 66 28 20 70 44  able );.  if( pD
2850a 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 70 53  est->nColumn!=pS
2850b 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  rc->nColumn ){. 
2850c 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
2850d 2a 20 44 69 66 66 65 72 65 6e 74 20 6e 75 6d 62  * Different numb
2850e 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f  er of columns */
2850f 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74  .  }.  if( pDest
28510 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 53 72 63 2d  ->onError!=pSrc-
28511 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20  >onError ){.    
28512 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44  return 0;   /* D
28513 69 66 66 65 72 65 6e 74 20 63 6f 6e 66 6c 69 63  ifferent conflic
28514 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 73 74 72  t resolution str
28515 61 74 65 67 69 65 73 20 2a 2f 0a 20 20 7d 0a 20  ategies */.  }. 
28516 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63   for(i=0; i<pSrc
28517 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
28518 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61  .    if( pSrc->a
28519 69 43 6f 6c 75 6d 6e 5b 69 5d 21 3d 70 44 65 73  iColumn[i]!=pDes
2851a 74 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 20 29  t->aiColumn[i] )
2851b 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
2851c 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
2851d 20 63 6f 6c 75 6d 6e 73 20 69 6e 64 65 78 65 64   columns indexed
2851e 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
2851f 28 20 70 53 72 63 2d 3e 61 53 6f 72 74 4f 72 64  ( pSrc->aSortOrd
28520 65 72 5b 69 5d 21 3d 70 44 65 73 74 2d 3e 61 53  er[i]!=pDest->aS
28521 6f 72 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20  ortOrder[i] ){. 
28522 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
28523 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 73 6f   /* Different so
28524 72 74 20 6f 72 64 65 72 73 20 2a 2f 0a 20 20 20  rt orders */.   
28525 20 7d 0a 20 20 20 20 69 66 28 20 21 78 66 65 72   }.    if( !xfer
28526 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61 74  CompatibleCollat
28527 69 6f 6e 28 70 53 72 63 2d 3e 61 7a 43 6f 6c 6c  ion(pSrc->azColl
28528 5b 69 5d 2c 70 44 65 73 74 2d 3e 61 7a 43 6f 6c  [i],pDest->azCol
28529 6c 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72  l[i]) ){.      r
2852a 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69  eturn 0;   /* Di
2852b 66 66 65 72 65 6e 74 20 63 6f 6c 6c 61 74 69 6e  fferent collatin
2852c 67 20 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20  g sequences */. 
2852d 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
2852e 66 20 6e 6f 20 74 65 73 74 20 61 62 6f 76 65 20  f no test above 
2852f 66 61 69 6c 73 20 74 68 65 6e 20 74 68 65 20 69  fails then the i
28530 6e 64 69 63 65 73 20 6d 75 73 74 20 62 65 20 63  ndices must be c
28531 6f 6d 70 61 74 69 62 6c 65 20 2a 2f 0a 20 20 72  ompatible */.  r
28532 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
28533 2a 20 41 74 74 65 6d 70 74 20 74 68 65 20 74 72  * Attempt the tr
28534 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74  ansfer optimizat
28535 69 6f 6e 20 6f 6e 20 49 4e 53 45 52 54 73 20 6f  ion on INSERTs o
28536 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
28537 20 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f       INSERT INTO
28538 20 74 61 62 31 20 53 45 4c 45 43 54 20 2a 20 46   tab1 SELECT * F
28539 52 4f 4d 20 74 61 62 32 3b 0a 2a 2a 0a 2a 2a 20  ROM tab2;.**.** 
2853a 54 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f  This optimizatio
2853b 6e 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70  n is only attemp
2853c 74 65 64 20 69 66 0a 2a 2a 0a 2a 2a 20 20 20 20  ted if.**.**    
2853d 28 31 29 20 20 74 61 62 31 20 61 6e 64 20 74 61  (1)  tab1 and ta
2853e 62 32 20 68 61 76 65 20 69 64 65 6e 74 69 63 61  b2 have identica
2853f 6c 20 73 63 68 65 6d 61 73 20 69 6e 63 6c 75 64  l schemas includ
28540 69 6e 67 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 20  ing all the.**  
28541 20 20 20 20 20 20 20 73 61 6d 65 20 69 6e 64 69         same indi
28542 63 65 73 20 61 6e 64 20 63 6f 6e 73 74 72 61 69  ces and constrai
28543 6e 74 73 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29  nts.**.**    (2)
28544 20 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20    tab1 and tab2 
28545 61 72 65 20 64 69 66 66 65 72 65 6e 74 20 74 61  are different ta
28546 62 6c 65 73 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33  bles.**.**    (3
28547 29 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 65  )  There must be
28548 20 6e 6f 20 74 72 69 67 67 65 72 73 20 6f 6e 20   no triggers on 
28549 74 61 62 31 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34  tab1.**.**    (4
2854a 29 20 20 54 68 65 20 72 65 73 75 6c 74 20 73 65  )  The result se
2854b 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  t of the SELECT 
2854c 73 74 61 74 65 6d 65 6e 74 20 69 73 20 22 2a 22  statement is "*"
2854d 0a 2a 2a 0a 2a 2a 20 20 20 20 28 35 29 20 20 54  .**.**    (5)  T
2854e 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2854f 65 6e 74 20 68 61 73 20 6e 6f 20 57 48 45 52 45  ent has no WHERE
28550 2c 20 48 41 56 49 4e 47 2c 20 4f 52 44 45 52 20  , HAVING, ORDER 
28551 42 59 2c 20 47 52 4f 55 50 20 42 59 2c 0a 2a 2a  BY, GROUP BY,.**
28552 20 20 20 20 20 20 20 20 20 6f 72 20 4c 49 4d 49           or LIMI
28553 54 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20  T clause..**.** 
28554 20 20 20 28 36 29 20 20 54 68 65 20 53 45 4c 45     (6)  The SELE
28555 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  CT statement is 
28556 61 20 73 69 6d 70 6c 65 20 28 6e 6f 74 20 61 20  a simple (not a 
28557 63 6f 6d 70 6f 75 6e 64 29 20 73 65 6c 65 63 74  compound) select
28558 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
28559 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 74   contains only t
2855a 61 62 32 20 69 6e 20 69 74 73 20 46 52 4f 4d 20  ab2 in its FROM 
2855b 63 6c 61 75 73 65 0a 2a 2a 0a 2a 2a 20 54 68 69  clause.**.** Thi
2855c 73 20 6d 65 74 68 6f 64 20 66 6f 72 20 69 6d 70  s method for imp
2855d 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 49 4e  lementing the IN
2855e 53 45 52 54 20 74 72 61 6e 73 66 65 72 73 20 72  SERT transfers r
2855f 61 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 0a  aw records from.
28560 2a 2a 20 74 61 62 32 20 6f 76 65 72 20 74 6f 20  ** tab2 over to 
28561 74 61 62 31 2e 20 20 54 68 65 20 63 6f 6c 75 6d  tab1.  The colum
28562 6e 73 20 61 72 65 20 6e 6f 74 20 64 65 63 6f 64  ns are not decod
28563 65 64 2e 20 20 52 61 77 20 72 65 63 6f 72 64 73  ed.  Raw records
28564 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64   from.** the ind
28565 69 63 65 73 20 6f 66 20 74 61 62 32 20 61 72 65  ices of tab2 are
28566 20 74 72 61 6e 73 66 65 72 65 64 20 74 6f 20 74   transfered to t
28567 61 62 31 20 61 73 20 77 65 6c 6c 2e 20 20 49 6e  ab1 as well.  In
28568 20 73 6f 20 64 6f 69 6e 67 2c 0a 2a 2a 20 74 68   so doing,.** th
28569 65 20 72 65 73 75 6c 74 69 6e 67 20 74 61 62 31  e resulting tab1
2856a 20 68 61 73 20 6d 75 63 68 20 6c 65 73 73 20 66   has much less f
2856b 72 61 67 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a  ragmentation..**
2856c 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2856d 20 72 65 74 75 72 6e 73 20 54 52 55 45 20 69 66   returns TRUE if
2856e 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f   the optimizatio
2856f 6e 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e 20  n is attempted. 
28570 20 49 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68   If any.** of th
28571 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f  e conditions abo
28572 76 65 20 66 61 69 6c 20 73 6f 20 74 68 61 74 20  ve fail so that 
28573 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
28574 20 73 68 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62   should not.** b
28575 65 20 61 74 74 65 6d 70 74 65 64 2c 20 74 68 65  e attempted, the
28576 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  n this routine r
28577 65 74 75 72 6e 73 20 46 41 4c 53 45 2e 0a 2a 2f  eturns FALSE..*/
28578 0a 73 74 61 74 69 63 20 69 6e 74 20 78 66 65 72  .static int xfer
28579 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 0a 20 20  Optimization(.  
2857a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2857b 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20        /* Parser 
2857c 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
2857d 6c 65 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20  le *pDest,      
2857e 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
2857f 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67  we are inserting
28580 20 69 6e 74 6f 20 2a 2f 0a 20 20 53 65 6c 65 63   into */.  Selec
28581 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20  t *pSelect,     
28582 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61   /* A SELECT sta
28583 74 65 6d 65 6e 74 20 74 6f 20 75 73 65 20 61 73  tement to use as
28584 20 74 68 65 20 64 61 74 61 20 73 6f 75 72 63 65   the data source
28585 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f   */.  int onErro
28586 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48  r,          /* H
28587 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e  ow to handle con
28588 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a  straint errors *
28589 2f 0a 20 20 69 6e 74 20 69 44 62 44 65 73 74 20  /.  int iDbDest 
2858a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2858b 20 64 61 74 61 62 61 73 65 20 6f 66 20 70 44 65   database of pDe
2858c 73 74 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 4c  st */.){.  ExprL
2858d 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20  ist *pEList;    
2858e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2858f 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
28590 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20   the SELECT */. 
28591 20 54 61 62 6c 65 20 2a 70 53 72 63 3b 20 20 20   Table *pSrc;   
28592 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28593 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69    /* The table i
28594 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
28595 65 20 6f 66 20 53 45 4c 45 43 54 20 2a 2f 0a 20  e of SELECT */. 
28596 20 49 6e 64 65 78 20 2a 70 53 72 63 49 64 78 2c   Index *pSrcIdx,
28597 20 2a 70 44 65 73 74 49 64 78 3b 20 20 20 20 20   *pDestIdx;     
28598 20 20 2f 2a 20 53 6f 75 72 63 65 20 61 6e 64 20    /* Source and 
28599 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 6e 64 69  destination indi
2859a 63 65 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  ces */.  struct 
2859b 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
2859c 74 65 6d 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20  tem;      /* An 
2859d 65 6c 65 6d 65 6e 74 20 6f 66 20 70 53 65 6c 65  element of pSele
2859e 63 74 2d 3e 70 53 72 63 20 2a 2f 0a 20 20 69 6e  ct->pSrc */.  in
2859f 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
285a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
285a1 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
285a2 2f 0a 20 20 69 6e 74 20 69 44 62 53 72 63 3b 20  /.  int iDbSrc; 
285a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
285a4 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
285a5 62 61 73 65 20 6f 66 20 70 53 72 63 20 2a 2f 0a  base of pSrc */.
285a6 20 20 69 6e 74 20 69 53 72 63 2c 20 69 44 65 73    int iSrc, iDes
285a7 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
285a8 20 20 20 2f 2a 20 43 75 72 73 6f 72 73 20 66 72     /* Cursors fr
285a9 6f 6d 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65  om source and de
285aa 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69  stination */.  i
285ab 6e 74 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b  nt addr1, addr2;
285ac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
285ad 2f 2a 20 4c 6f 6f 70 20 61 64 64 72 65 73 73 65  /* Loop addresse
285ae 73 20 2a 2f 0a 20 20 69 6e 74 20 65 6d 70 74 79  s */.  int empty
285af 44 65 73 74 54 65 73 74 3b 20 20 20 20 20 20 20  DestTest;       
285b0 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
285b1 73 73 20 6f 66 20 74 65 73 74 20 66 6f 72 20 65  ss of test for e
285b2 6d 70 74 79 20 70 44 65 73 74 20 2a 2f 0a 20 20  mpty pDest */.  
285b3 69 6e 74 20 65 6d 70 74 79 53 72 63 54 65 73 74  int emptySrcTest
285b4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
285b5 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
285b6 65 73 74 20 66 6f 72 20 65 6d 70 74 79 20 70 53  est for empty pS
285b7 72 63 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b  rc */.  Vdbe *v;
285b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
285b9 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
285ba 56 44 42 45 20 77 65 20 61 72 65 20 62 75 69 6c  VDBE we are buil
285bb 64 69 6e 67 20 2a 2f 0a 20 20 4b 65 79 49 6e 66  ding */.  KeyInf
285bc 6f 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20  o *pKey;        
285bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65             /* Ke
285be 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  y information fo
285bf 72 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20  r an index */.  
285c0 69 6e 74 20 72 65 67 41 75 74 6f 69 6e 63 3b 20  int regAutoinc; 
285c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
285c2 20 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67 69 73   /* Memory regis
285c3 74 65 72 20 75 73 65 64 20 62 79 20 41 55 54 4f  ter used by AUTO
285c4 49 4e 43 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73  INC */.  int des
285c5 74 48 61 73 55 6e 69 71 75 65 49 64 78 20 3d 20  tHasUniqueIdx = 
285c6 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  0;        /* Tru
285c7 65 20 69 66 20 70 44 65 73 74 20 68 61 73 20 61  e if pDest has a
285c8 20 55 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f   UNIQUE index */
285c9 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 2c 20  .  int regData, 
285ca 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20  regRowid;       
285cb 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 73      /* Registers
285cc 20 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 61 6e   holding data an
285cd 64 20 72 6f 77 69 64 20 2a 2f 0a 0a 20 20 69 66  d rowid */..  if
285ce 28 20 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a  ( pSelect==0 ){.
285cf 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
285d0 2f 2a 20 4d 75 73 74 20 62 65 20 6f 66 20 74 68  /* Must be of th
285d1 65 20 66 6f 72 6d 20 20 49 4e 53 45 52 54 20 49  e form  INSERT I
285d2 4e 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e  NTO ... SELECT .
285d3 2e 2e 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  .. */.  }.  if( 
285d4 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69  sqlite3TriggerLi
285d5 73 74 28 70 50 61 72 73 65 2c 20 70 44 65 73 74  st(pParse, pDest
285d6 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
285d7 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d 75 73  0;   /* tab1 mus
285d8 74 20 6e 6f 74 20 68 61 76 65 20 74 72 69 67 67  t not have trigg
285d9 65 72 73 20 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64  ers */.  }.#ifnd
285da 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
285db 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66  IRTUALTABLE.  if
285dc 28 20 70 44 65 73 74 2d 3e 74 61 62 46 6c 61 67  ( pDest->tabFlag
285dd 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c 20 29  s & TF_Virtual )
285de 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
285df 20 20 2f 2a 20 74 61 62 31 20 6d 75 73 74 20 6e    /* tab1 must n
285e0 6f 74 20 62 65 20 61 20 76 69 72 74 75 61 6c 20  ot be a virtual 
285e1 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e  table */.  }.#en
285e2 64 69 66 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f  dif.  if( onErro
285e3 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b  r==OE_Default ){
285e4 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f  .    onError = O
285e5 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 20 20 69  E_Abort;.  }.  i
285e6 66 28 20 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 41  f( onError!=OE_A
285e7 62 6f 72 74 20 26 26 20 6f 6e 45 72 72 6f 72 21  bort && onError!
285e8 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a  =OE_Rollback ){.
285e9 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
285ea 2f 2a 20 43 61 6e 6e 6f 74 20 64 6f 20 4f 52 20  /* Cannot do OR 
285eb 52 45 50 4c 41 43 45 20 6f 72 20 4f 52 20 49 47  REPLACE or OR IG
285ec 4e 4f 52 45 20 6f 72 20 4f 52 20 46 41 49 4c 20  NORE or OR FAIL 
285ed 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  */.  }.  assert(
285ee 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 29 3b 20  pSelect->pSrc); 
285ef 20 20 2f 2a 20 61 6c 6c 6f 63 61 74 65 64 20 65    /* allocated e
285f0 76 65 6e 20 69 66 20 74 68 65 72 65 20 69 73 20  ven if there is 
285f1 6e 6f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  no FROM clause *
285f2 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d  /.  if( pSelect-
285f3 3e 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29  >pSrc->nSrc!=1 )
285f4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
285f5 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
285f6 20 6d 75 73 74 20 68 61 76 65 20 65 78 61 63 74   must have exact
285f7 6c 79 20 6f 6e 65 20 74 65 72 6d 20 2a 2f 0a 20  ly one term */. 
285f8 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74   }.  if( pSelect
285f9 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65  ->pSrc->a[0].pSe
285fa 6c 65 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75  lect ){.    retu
285fb 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20  rn 0;   /* FROM 
285fc 63 6c 61 75 73 65 20 63 61 6e 6e 6f 74 20 63 6f  clause cannot co
285fd 6e 74 61 69 6e 20 61 20 73 75 62 71 75 65 72 79  ntain a subquery
285fe 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53   */.  }.  if( pS
285ff 65 6c 65 63 74 2d 3e 70 57 68 65 72 65 20 29 7b  elect->pWhere ){
28600 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
28601 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e   /* SELECT may n
28602 6f 74 20 68 61 76 65 20 61 20 57 48 45 52 45 20  ot have a WHERE 
28603 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20  clause */.  }.  
28604 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72  if( pSelect->pOr
28605 64 65 72 42 79 20 29 7b 0a 20 20 20 20 72 65 74  derBy ){.    ret
28606 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45  urn 0;   /* SELE
28607 43 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20  CT may not have 
28608 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
28609 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 2f 2a 20 44  se */.  }.  /* D
2860a 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 74 65  o not need to te
2860b 73 74 20 66 6f 72 20 61 20 48 41 56 49 4e 47 20  st for a HAVING 
2860c 63 6c 61 75 73 65 2e 20 20 49 66 20 48 41 56 49  clause.  If HAVI
2860d 4e 47 20 69 73 20 70 72 65 73 65 6e 74 20 62 75  NG is present bu
2860e 74 0a 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20  t.  ** there is 
2860f 6e 6f 20 4f 52 44 45 52 20 42 59 2c 20 77 65 20  no ORDER BY, we 
28610 77 69 6c 6c 20 67 65 74 20 61 6e 20 65 72 72 6f  will get an erro
28611 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c  r. */.  if( pSel
28612 65 63 74 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b  ect->pGroupBy ){
28613 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
28614 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e   /* SELECT may n
28615 6f 74 20 68 61 76 65 20 61 20 47 52 4f 55 50 20  ot have a GROUP 
28616 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d  BY clause */.  }
28617 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e  .  if( pSelect->
28618 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 72 65  pLimit ){.    re
28619 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c  turn 0;   /* SEL
2861a 45 43 54 20 6d 61 79 20 6e 6f 74 20 68 61 76 65  ECT may not have
2861b 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20   a LIMIT clause 
2861c 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  */.  }.  assert(
2861d 20 70 53 65 6c 65 63 74 2d 3e 70 4f 66 66 73 65   pSelect->pOffse
2861e 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 4d 75 73 74  t==0 );  /* Must
2861f 20 62 65 20 73 6f 20 69 66 20 70 4c 69 6d 69 74   be so if pLimit
28620 3d 3d 30 20 2a 2f 0a 20 20 69 66 28 20 70 53 65  ==0 */.  if( pSe
28621 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a  lect->pPrior ){.
28622 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
28623 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f  /* SELECT may no
28624 74 20 62 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20  t be a compound 
28625 71 75 65 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69  query */.  }.  i
28626 66 28 20 70 53 65 6c 65 63 74 2d 3e 73 65 6c 46  f( pSelect->selF
28627 6c 61 67 73 20 26 20 53 46 5f 44 69 73 74 69 6e  lags & SF_Distin
28628 63 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ct ){.    return
28629 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20   0;   /* SELECT 
2862a 6d 61 79 20 6e 6f 74 20 62 65 20 44 49 53 54 49  may not be DISTI
2862b 4e 43 54 20 2a 2f 0a 20 20 7d 0a 20 20 70 45 4c  NCT */.  }.  pEL
2862c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ist = pSelect->p
2862d 45 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28  EList;.  assert(
2862e 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20   pEList!=0 );.  
2862f 69 66 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  if( pEList->nExp
28630 72 21 3d 31 20 29 7b 0a 20 20 20 20 72 65 74 75  r!=1 ){.    retu
28631 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 72  rn 0;   /* The r
28632 65 73 75 6c 74 20 73 65 74 20 6d 75 73 74 20 68  esult set must h
28633 61 76 65 20 65 78 61 63 74 6c 79 20 6f 6e 65 20  ave exactly one 
28634 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 7d 0a 20 20  column */.  }.  
28635 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 2d 3e  assert( pEList->
28636 61 5b 30 5d 2e 70 45 78 70 72 20 29 3b 0a 20 20  a[0].pExpr );.  
28637 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d  if( pEList->a[0]
28638 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41  .pExpr->op!=TK_A
28639 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  LL ){.    return
2863a 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 72 65 73   0;   /* The res
2863b 75 6c 74 20 73 65 74 20 6d 75 73 74 20 62 65 20  ult set must be 
2863c 74 68 65 20 73 70 65 63 69 61 6c 20 6f 70 65 72  the special oper
2863d 61 74 6f 72 20 22 2a 22 20 2a 2f 0a 20 20 7d 0a  ator "*" */.  }.
2863e 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f  .  /* At this po
2863f 69 6e 74 20 77 65 20 68 61 76 65 20 65 73 74 61  int we have esta
28640 62 6c 69 73 68 65 64 20 74 68 61 74 20 74 68 65  blished that the
28641 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66   statement is of
28642 20 74 68 65 0a 20 20 2a 2a 20 63 6f 72 72 65 63   the.  ** correc
28643 74 20 73 79 6e 74 61 63 74 69 63 20 66 6f 72 6d  t syntactic form
28644 20 74 6f 20 70 61 72 74 69 63 69 70 61 74 65 20   to participate 
28645 69 6e 20 74 68 69 73 20 6f 70 74 69 6d 69 7a 61  in this optimiza
28646 74 69 6f 6e 2e 20 20 4e 6f 77 0a 20 20 2a 2a 20  tion.  Now.  ** 
28647 77 65 20 68 61 76 65 20 74 6f 20 63 68 65 63 6b  we have to check
28648 20 74 68 65 20 73 65 6d 61 6e 74 69 63 73 2e 0a   the semantics..
28649 20 20 2a 2f 0a 20 20 70 49 74 65 6d 20 3d 20 70    */.  pItem = p
2864a 53 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 61 3b  Select->pSrc->a;
2864b 0a 20 20 70 53 72 63 20 3d 20 73 71 6c 69 74 65  .  pSrc = sqlite
2864c 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
2864d 72 73 65 2c 20 30 2c 20 70 49 74 65 6d 2d 3e 7a  rse, 0, pItem->z
2864e 4e 61 6d 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61  Name, pItem->zDa
2864f 74 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70  tabase);.  if( p
28650 53 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  Src==0 ){.    re
28651 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f  turn 0;   /* FRO
28652 4d 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f  M clause does no
28653 74 20 63 6f 6e 74 61 69 6e 20 61 20 72 65 61 6c  t contain a real
28654 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7d 0a 20 20   table */.  }.  
28655 69 66 28 20 70 53 72 63 3d 3d 70 44 65 73 74 20  if( pSrc==pDest 
28656 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
28657 20 20 20 2f 2a 20 74 61 62 31 20 61 6e 64 20 74     /* tab1 and t
28658 61 62 32 20 6d 61 79 20 6e 6f 74 20 62 65 20 74  ab2 may not be t
28659 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f  he same table */
2865a 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
2865b 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2865c 54 41 42 4c 45 0a 20 20 69 66 28 20 70 53 72 63  TABLE.  if( pSrc
2865d 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
2865e 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 72  Virtual ){.    r
2865f 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61  eturn 0;   /* ta
28660 62 32 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 61  b2 must not be a
28661 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
28662 2f 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69  /.  }.#endif.  i
28663 66 28 20 70 53 72 63 2d 3e 70 53 65 6c 65 63 74  f( pSrc->pSelect
28664 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
28665 3b 20 20 20 2f 2a 20 74 61 62 32 20 6d 61 79 20  ;   /* tab2 may 
28666 6e 6f 74 20 62 65 20 61 20 76 69 65 77 20 2a 2f  not be a view */
28667 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74  .  }.  if( pDest
28668 2d 3e 6e 43 6f 6c 21 3d 70 53 72 63 2d 3e 6e 43  ->nCol!=pSrc->nC
28669 6f 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ol ){.    return
2866a 20 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20   0;   /* Number 
2866b 6f 66 20 63 6f 6c 75 6d 6e 73 20 6d 75 73 74 20  of columns must 
2866c 62 65 20 74 68 65 20 73 61 6d 65 20 69 6e 20 74  be the same in t
2866d 61 62 31 20 61 6e 64 20 74 61 62 32 20 2a 2f 0a  ab1 and tab2 */.
2866e 20 20 7d 0a 20 20 69 66 28 20 70 44 65 73 74 2d    }.  if( pDest-
2866f 3e 69 50 4b 65 79 21 3d 70 53 72 63 2d 3e 69 50  >iPKey!=pSrc->iP
28670 4b 65 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72  Key ){.    retur
28671 6e 20 30 3b 20 20 20 2f 2a 20 42 6f 74 68 20 74  n 0;   /* Both t
28672 61 62 6c 65 73 20 6d 75 73 74 20 68 61 76 65 20  ables must have 
28673 74 68 65 20 73 61 6d 65 20 49 4e 54 45 47 45 52  the same INTEGER
28674 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a   PRIMARY KEY */.
28675 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
28676 3c 70 44 65 73 74 2d 3e 6e 43 6f 6c 3b 20 69 2b  <pDest->nCol; i+
28677 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 44 65 73  +){.    if( pDes
28678 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e  t->aCol[i].affin
28679 69 74 79 21 3d 70 53 72 63 2d 3e 61 43 6f 6c 5b  ity!=pSrc->aCol[
2867a 69 5d 2e 61 66 66 69 6e 69 74 79 20 29 7b 0a 20  i].affinity ){. 
2867b 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20       return 0;  
2867c 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 6d 75    /* Affinity mu
2867d 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 6f  st be the same o
2867e 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f  n all columns */
2867f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
28680 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f  xferCompatibleCo
28681 6c 6c 61 74 69 6f 6e 28 70 44 65 73 74 2d 3e 61  llation(pDest->a
28682 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 2c 20 70 53  Col[i].zColl, pS
28683 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c  rc->aCol[i].zCol
28684 6c 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  l) ){.      retu
28685 72 6e 20 30 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c  rn 0;    /* Coll
28686 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6d  ating sequence m
28687 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65 20  ust be the same 
28688 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a  on all columns *
28689 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  /.    }.    if( 
2868a 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e  pDest->aCol[i].n
2868b 6f 74 4e 75 6c 6c 20 26 26 20 21 70 53 72 63 2d  otNull && !pSrc-
2868c 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c  >aCol[i].notNull
2868d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2868e 20 30 3b 20 20 20 20 2f 2a 20 74 61 62 32 20 6d   0;    /* tab2 m
2868f 75 73 74 20 62 65 20 4e 4f 54 20 4e 55 4c 4c 20  ust be NOT NULL 
28690 69 66 20 74 61 62 31 20 69 73 20 2a 2f 0a 20 20  if tab1 is */.  
28691 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 70 44    }.  }.  for(pD
28692 65 73 74 49 64 78 3d 70 44 65 73 74 2d 3e 70 49  estIdx=pDest->pI
28693 6e 64 65 78 3b 20 70 44 65 73 74 49 64 78 3b 20  ndex; pDestIdx; 
28694 70 44 65 73 74 49 64 78 3d 70 44 65 73 74 49 64  pDestIdx=pDestId
28695 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  x->pNext){.    i
28696 66 28 20 70 44 65 73 74 49 64 78 2d 3e 6f 6e 45  f( pDestIdx->onE
28697 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b  rror!=OE_None ){
28698 0a 20 20 20 20 20 20 64 65 73 74 48 61 73 55 6e  .      destHasUn
28699 69 71 75 65 49 64 78 20 3d 20 31 3b 0a 20 20 20  iqueIdx = 1;.   
2869a 20 7d 0a 20 20 20 20 66 6f 72 28 70 53 72 63 49   }.    for(pSrcI
2869b 64 78 3d 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b  dx=pSrc->pIndex;
2869c 20 70 53 72 63 49 64 78 3b 20 70 53 72 63 49 64   pSrcIdx; pSrcId
2869d 78 3d 70 53 72 63 49 64 78 2d 3e 70 4e 65 78 74  x=pSrcIdx->pNext
2869e 29 7b 0a 20 20 20 20 20 20 69 66 28 20 78 66 65  ){.      if( xfe
2869f 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e 64 65 78  rCompatibleIndex
286a0 28 70 44 65 73 74 49 64 78 2c 20 70 53 72 63 49  (pDestIdx, pSrcI
286a1 64 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  dx) ) break;.   
286a2 20 7d 0a 20 20 20 20 69 66 28 20 70 53 72 63 49   }.    if( pSrcI
286a3 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  dx==0 ){.      r
286a4 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 70  eturn 0;    /* p
286a5 44 65 73 74 49 64 78 20 68 61 73 20 6e 6f 20 63  DestIdx has no c
286a6 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 64  orresponding ind
286a7 65 78 20 69 6e 20 70 53 72 63 20 2a 2f 0a 20 20  ex in pSrc */.  
286a8 20 20 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20    }.  }.#ifndef 
286a9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43  SQLITE_OMIT_CHEC
286aa 4b 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 70  K.  if( pDest->p
286ab 43 68 65 63 6b 20 26 26 20 21 73 71 6c 69 74 65  Check && !sqlite
286ac 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 53 72  3ExprCompare(pSr
286ad 63 2d 3e 70 43 68 65 63 6b 2c 20 70 44 65 73 74  c->pCheck, pDest
286ae 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20 20  ->pCheck) ){.   
286af 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
286b0 54 61 62 6c 65 73 20 68 61 76 65 20 64 69 66 66  Tables have diff
286b1 65 72 65 6e 74 20 43 48 45 43 4b 20 63 6f 6e 73  erent CHECK cons
286b2 74 72 61 69 6e 74 73 2e 20 20 54 69 63 6b 65 74  traints.  Ticket
286b3 20 23 32 32 35 32 20 2a 2f 0a 20 20 7d 0a 23 65   #2252 */.  }.#e
286b4 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65  ndif..  /* If we
286b5 20 67 65 74 20 74 68 69 73 20 66 61 72 2c 20 69   get this far, i
286b6 74 20 6d 65 61 6e 73 20 65 69 74 68 65 72 3a 0a  t means either:.
286b7 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20    **.  **    *  
286b8 20 57 65 20 63 61 6e 20 61 6c 77 61 79 73 20 64   We can always d
286b9 6f 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 69  o the transfer i
286ba 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74  f the table cont
286bb 61 69 6e 73 20 61 6e 0a 20 20 2a 2a 20 20 20 20  ains an.  **    
286bc 20 20 20 20 61 6e 20 69 6e 74 65 67 65 72 20 70      an integer p
286bd 72 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a 0a  rimary key.  **.
286be 20 20 2a 2a 20 20 20 20 2a 20 20 20 57 65 20 63    **    *   We c
286bf 61 6e 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79  an conditionally
286c0 20 64 6f 20 74 68 65 20 74 72 61 6e 73 66 65 72   do the transfer
286c1 20 69 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   if the destinat
286c2 69 6f 6e 0a 20 20 2a 2a 20 20 20 20 20 20 20 20  ion.  **        
286c3 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a  table is empty..
286c4 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49    */.#ifdef SQLI
286c5 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65  TE_TEST.  sqlite
286c6 33 5f 78 66 65 72 6f 70 74 5f 63 6f 75 6e 74 2b  3_xferopt_count+
286c7 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 69 44 62 53  +;.#endif.  iDbS
286c8 72 63 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  rc = sqlite3Sche
286c9 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
286ca 2d 3e 64 62 2c 20 70 53 72 63 2d 3e 70 53 63 68  ->db, pSrc->pSch
286cb 65 6d 61 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69  ema);.  v = sqli
286cc 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
286cd 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 6f 64  e);.  sqlite3Cod
286ce 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
286cf 61 72 73 65 2c 20 69 44 62 53 72 63 29 3b 0a 20  arse, iDbSrc);. 
286d0 20 69 53 72 63 20 3d 20 70 50 61 72 73 65 2d 3e   iSrc = pParse->
286d1 6e 54 61 62 2b 2b 3b 0a 20 20 69 44 65 73 74 20  nTab++;.  iDest 
286d2 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
286d3 3b 0a 20 20 72 65 67 41 75 74 6f 69 6e 63 20 3d  ;.  regAutoinc =
286d4 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 70 50   autoIncBegin(pP
286d5 61 72 73 65 2c 20 69 44 62 44 65 73 74 2c 20 70  arse, iDbDest, p
286d6 44 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  Dest);.  sqlite3
286d7 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
286d8 2c 20 69 44 65 73 74 2c 20 69 44 62 44 65 73 74  , iDest, iDbDest
286d9 2c 20 70 44 65 73 74 2c 20 4f 50 5f 4f 70 65 6e  , pDest, OP_Open
286da 57 72 69 74 65 29 3b 0a 20 20 69 66 28 20 28 70  Write);.  if( (p
286db 44 65 73 74 2d 3e 69 50 4b 65 79 3c 30 20 26 26  Dest->iPKey<0 &&
286dc 20 70 44 65 73 74 2d 3e 70 49 6e 64 65 78 21 3d   pDest->pIndex!=
286dd 30 29 20 7c 7c 20 64 65 73 74 48 61 73 55 6e 69  0) || destHasUni
286de 71 75 65 49 64 78 20 29 7b 0a 20 20 20 20 2f 2a  queIdx ){.    /*
286df 20 49 66 20 74 61 62 6c 65 73 20 64 6f 20 6e 6f   If tables do no
286e0 74 20 68 61 76 65 20 61 6e 20 49 4e 54 45 47 45  t have an INTEGE
286e1 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 61 6e  R PRIMARY KEY an
286e2 64 20 74 68 65 72 65 0a 20 20 20 20 2a 2a 20 61  d there.    ** a
286e3 72 65 20 69 6e 64 69 63 65 73 20 74 6f 20 62 65  re indices to be
286e4 20 63 6f 70 69 65 64 20 61 6e 64 20 74 68 65 20   copied and the 
286e5 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73 20 6e  destination is n
286e6 6f 74 20 65 6d 70 74 79 2c 0a 20 20 20 20 2a 2a  ot empty,.    **
286e7 20 77 65 20 68 61 76 65 20 74 6f 20 64 69 73 61   we have to disa
286e8 6c 6c 6f 77 20 74 68 65 20 74 72 61 6e 73 66 65  llow the transfe
286e9 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 62  r optimization b
286ea 65 63 61 75 73 65 20 74 68 65 0a 20 20 20 20 2a  ecause the.    *
286eb 2a 20 74 68 65 20 72 6f 77 69 64 73 20 6d 69 67  * the rowids mig
286ec 68 74 20 63 68 61 6e 67 65 20 77 68 69 63 68 20  ht change which 
286ed 77 69 6c 6c 20 6d 65 73 73 20 75 70 20 69 6e 64  will mess up ind
286ee 65 78 69 6e 67 2e 0a 20 20 20 20 2a 2a 0a 20 20  exing..    **.  
286ef 20 20 2a 2a 20 4f 72 20 69 66 20 74 68 65 20 64    ** Or if the d
286f0 65 73 74 69 6e 61 74 69 6f 6e 20 68 61 73 20 61  estination has a
286f1 20 55 4e 49 51 55 45 20 69 6e 64 65 78 20 61 6e   UNIQUE index an
286f2 64 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 0a  d is not empty,.
286f3 20 20 20 20 2a 2a 20 77 65 20 61 6c 73 6f 20 64      ** we also d
286f4 69 73 61 6c 6c 6f 77 20 74 68 65 20 74 72 61 6e  isallow the tran
286f5 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  sfer optimizatio
286f6 6e 20 62 65 63 61 75 73 65 20 77 65 20 63 61 6e  n because we can
286f7 6e 6f 74 0a 20 20 20 20 2a 2a 20 69 6e 73 75 72  not.    ** insur
286f8 65 20 74 68 61 74 20 61 6c 6c 20 65 6e 74 72 69  e that all entri
286f9 65 73 20 69 6e 20 74 68 65 20 75 6e 69 6f 6e 20  es in the union 
286fa 6f 66 20 44 45 53 54 20 61 6e 64 20 53 52 43 20  of DEST and SRC 
286fb 77 69 6c 6c 20 62 65 0a 20 20 20 20 2a 2a 20 75  will be.    ** u
286fc 6e 69 71 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  nique..    */.  
286fd 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
286fe 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
286ff 50 5f 52 65 77 69 6e 64 2c 20 69 44 65 73 74 2c  P_Rewind, iDest,
28700 20 30 29 3b 0a 20 20 20 20 65 6d 70 74 79 44 65   0);.    emptyDe
28701 73 74 54 65 73 74 20 3d 20 73 71 6c 69 74 65 33  stTest = sqlite3
28702 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
28703 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a 20 20  _Goto, 0, 0);.  
28704 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
28705 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
28706 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 65 6d  .  }else{.    em
28707 70 74 79 44 65 73 74 54 65 73 74 20 3d 20 30 3b  ptyDestTest = 0;
28708 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 70  .  }.  sqlite3Op
28709 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  enTable(pParse, 
2870a 69 53 72 63 2c 20 69 44 62 53 72 63 2c 20 70 53  iSrc, iDbSrc, pS
2870b 72 63 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29  rc, OP_OpenRead)
2870c 3b 0a 20 20 65 6d 70 74 79 53 72 63 54 65 73 74  ;.  emptySrcTest
2870d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2870e 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
2870f 64 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20 72  d, iSrc, 0);.  r
28710 65 67 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33  egData = sqlite3
28711 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
28712 65 29 3b 0a 20 20 72 65 67 52 6f 77 69 64 20 3d  e);.  regRowid =
28713 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
28714 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  eg(pParse);.  if
28715 28 20 70 44 65 73 74 2d 3e 69 50 4b 65 79 3e 3d  ( pDest->iPKey>=
28716 30 20 29 7b 0a 20 20 20 20 61 64 64 72 31 20 3d  0 ){.    addr1 =
28717 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28718 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
28719 69 53 72 63 2c 20 72 65 67 52 6f 77 69 64 29 3b  iSrc, regRowid);
2871a 0a 20 20 20 20 61 64 64 72 32 20 3d 20 73 71 6c  .    addr2 = sql
2871b 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2871c 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20  , OP_NotExists, 
2871d 69 44 65 73 74 2c 20 30 2c 20 72 65 67 52 6f 77  iDest, 0, regRow
2871e 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
2871f 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 0a  HaltConstraint(.
28720 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 20          pParse, 
28721 6f 6e 45 72 72 6f 72 2c 20 22 50 52 49 4d 41 52  onError, "PRIMAR
28722 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 75 6e  Y KEY must be un
28723 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43  ique", P4_STATIC
28724 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
28725 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
28726 64 72 32 29 3b 0a 20 20 20 20 61 75 74 6f 49 6e  dr2);.    autoIn
28727 63 53 74 65 70 28 70 50 61 72 73 65 2c 20 72 65  cStep(pParse, re
28728 67 41 75 74 6f 69 6e 63 2c 20 72 65 67 52 6f 77  gAutoinc, regRow
28729 69 64 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  id);.  }else if(
2872a 20 70 44 65 73 74 2d 3e 70 49 6e 64 65 78 3d 3d   pDest->pIndex==
2872b 30 20 29 7b 0a 20 20 20 20 61 64 64 72 31 20 3d  0 ){.    addr1 =
2872c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2872d 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
2872e 64 2c 20 69 44 65 73 74 2c 20 72 65 67 52 6f 77  d, iDest, regRow
2872f 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  id);.  }else{.  
28730 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
28731 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
28732 50 5f 52 6f 77 69 64 2c 20 69 53 72 63 2c 20 72  P_Rowid, iSrc, r
28733 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 61 73  egRowid);.    as
28734 73 65 72 74 28 20 28 70 44 65 73 74 2d 3e 74 61  sert( (pDest->ta
28735 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f  bFlags & TF_Auto
28736 69 6e 63 72 65 6d 65 6e 74 29 3d 3d 30 20 29 3b  increment)==0 );
28737 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
28738 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
28739 6f 77 44 61 74 61 2c 20 69 53 72 63 2c 20 72 65  owData, iSrc, re
2873a 67 44 61 74 61 29 3b 0a 20 20 73 71 6c 69 74 65  gData);.  sqlite
2873b 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2873c 50 5f 49 6e 73 65 72 74 2c 20 69 44 65 73 74 2c  P_Insert, iDest,
2873d 20 72 65 67 44 61 74 61 2c 20 72 65 67 52 6f 77   regData, regRow
2873e 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  id);.  sqlite3Vd
2873f 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
28740 46 4c 41 47 5f 4e 43 48 41 4e 47 45 7c 4f 50 46  FLAG_NCHANGE|OPF
28741 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 7c 4f 50  LAG_LASTROWID|OP
28742 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20  FLAG_APPEND);.  
28743 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
28744 65 50 34 28 76 2c 20 2d 31 2c 20 70 44 65 73 74  eP4(v, -1, pDest
28745 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 73  ->zName, 0);.  s
28746 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28747 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 53 72  (v, OP_Next, iSr
28748 63 2c 20 61 64 64 72 31 29 3b 0a 20 20 66 6f 72  c, addr1);.  for
28749 28 70 44 65 73 74 49 64 78 3d 70 44 65 73 74 2d  (pDestIdx=pDest-
2874a 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74 49 64  >pIndex; pDestId
2874b 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44 65 73  x; pDestIdx=pDes
2874c 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  tIdx->pNext){.  
2874d 20 20 66 6f 72 28 70 53 72 63 49 64 78 3d 70 53    for(pSrcIdx=pS
2874e 72 63 2d 3e 70 49 6e 64 65 78 3b 20 41 4c 57 41  rc->pIndex; ALWA
2874f 59 53 28 70 53 72 63 49 64 78 29 3b 20 70 53 72  YS(pSrcIdx); pSr
28750 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e 70 4e  cIdx=pSrcIdx->pN
28751 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ext){.      if( 
28752 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e  xferCompatibleIn
28753 64 65 78 28 70 44 65 73 74 49 64 78 2c 20 70 53  dex(pDestIdx, pS
28754 72 63 49 64 78 29 20 29 20 62 72 65 61 6b 3b 0a  rcIdx) ) break;.
28755 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
28756 28 20 70 53 72 63 49 64 78 20 29 3b 0a 20 20 20  ( pSrcIdx );.   
28757 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28758 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
28759 69 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71  iSrc, 0);.    sq
2875a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2875b 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65  v, OP_Close, iDe
2875c 73 74 2c 20 30 29 3b 0a 20 20 20 20 70 4b 65 79  st, 0);.    pKey
2875d 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
2875e 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
2875f 53 72 63 49 64 78 29 3b 0a 20 20 20 20 73 71 6c  SrcIdx);.    sql
28760 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
28761 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69  , OP_OpenRead, i
28762 53 72 63 2c 20 70 53 72 63 49 64 78 2d 3e 74 6e  Src, pSrcIdx->tn
28763 75 6d 2c 20 69 44 62 53 72 63 2c 0a 20 20 20 20  um, iDbSrc,.    
28764 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28765 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50    (char*)pKey, P
28766 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
28767 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d  F);.    VdbeComm
28768 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 53  ent((v, "%s", pS
28769 72 63 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a  rcIdx->zName));.
2876a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c 69 74      pKey = sqlit
2876b 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70  e3IndexKeyinfo(p
2876c 50 61 72 73 65 2c 20 70 44 65 73 74 49 64 78 29  Parse, pDestIdx)
2876d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2876e 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70  eAddOp4(v, OP_Op
2876f 65 6e 57 72 69 74 65 2c 20 69 44 65 73 74 2c 20  enWrite, iDest, 
28770 70 44 65 73 74 49 64 78 2d 3e 74 6e 75 6d 2c 20  pDestIdx->tnum, 
28771 69 44 62 44 65 73 74 2c 0a 20 20 20 20 20 20 20  iDbDest,.       
28772 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
28773 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b  char*)pKey, P4_K
28774 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
28775 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
28776 28 28 76 2c 20 22 25 73 22 2c 20 70 44 65 73 74  ((v, "%s", pDest
28777 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20  Idx->zName));.  
28778 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
28779 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2877a 50 5f 52 65 77 69 6e 64 2c 20 69 53 72 63 2c 20  P_Rewind, iSrc, 
2877b 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
2877c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2877d 52 6f 77 4b 65 79 2c 20 69 53 72 63 2c 20 72 65  RowKey, iSrc, re
2877e 67 44 61 74 61 29 3b 0a 20 20 20 20 73 71 6c 69  gData);.    sqli
2877f 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
28780 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69   OP_IdxInsert, i
28781 44 65 73 74 2c 20 72 65 67 44 61 74 61 2c 20 31  Dest, regData, 1
28782 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
28783 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
28784 65 78 74 2c 20 69 53 72 63 2c 20 61 64 64 72 31  ext, iSrc, addr1
28785 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +1);.    sqlite3
28786 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
28787 61 64 64 72 31 29 3b 0a 20 20 7d 0a 20 20 73 71  addr1);.  }.  sq
28788 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
28789 65 28 76 2c 20 65 6d 70 74 79 53 72 63 54 65 73  e(v, emptySrcTes
2878a 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  t);.  sqlite3Rel
2878b 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
2878c 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  se, regRowid);. 
2878d 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
2878e 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
2878f 65 67 44 61 74 61 29 3b 0a 20 20 73 71 6c 69 74  egData);.  sqlit
28790 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28791 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63 2c 20  OP_Close, iSrc, 
28792 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  0);.  sqlite3Vdb
28793 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
28794 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a  ose, iDest, 0);.
28795 20 20 69 66 28 20 65 6d 70 74 79 44 65 73 74 54    if( emptyDestT
28796 65 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  est ){.    sqlit
28797 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28798 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f  OP_Halt, SQLITE_
28799 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  OK, 0);.    sqli
2879a 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2879b 76 2c 20 65 6d 70 74 79 44 65 73 74 54 65 73 74  v, emptyDestTest
2879c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2879d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
2879e 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29 3b  lose, iDest, 0);
2879f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
287a0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
287a1 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  rn 1;.  }.}.#end
287a2 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
287a3 54 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a 2f  T_XFER_OPT */../
287a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
287a5 6e 64 20 6f 66 20 69 6e 73 65 72 74 2e 63 20 2a  nd of insert.c *
287a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
287a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
287a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
287a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
287aa 65 67 69 6e 20 66 69 6c 65 20 6c 65 67 61 63 79  egin file legacy
287ab 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
287ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
287ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
287ae 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
287af 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
287b0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
287b1 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
287b2 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
287b3 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
287b4 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
287b5 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
287b6 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
287b7 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
287b8 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
287b9 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
287ba 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
287bb 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
287bc 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
287bd 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
287be 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
287bf 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
287c0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
287c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
287c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
287c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
287c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
287c5 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e 20 66 69  *****.** Main fi
287c6 6c 65 20 66 6f 72 20 74 68 65 20 53 51 4c 69 74  le for the SQLit
287c7 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20  e library.  The 
287c8 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73  routines in this
287c9 20 66 69 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65   file.** impleme
287ca 6e 74 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 65  nt the programme
287cb 72 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  r interface to t
287cc 68 65 20 6c 69 62 72 61 72 79 2e 20 20 52 6f 75  he library.  Rou
287cd 74 69 6e 65 73 20 69 6e 0a 2a 2a 20 6f 74 68 65  tines in.** othe
287ce 72 20 66 69 6c 65 73 20 61 72 65 20 66 6f 72 20  r files are for 
287cf 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 62 79 20  internal use by 
287d0 53 51 4c 69 74 65 20 61 6e 64 20 73 68 6f 75 6c  SQLite and shoul
287d1 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63 63 65  d not be.** acce
287d2 73 73 65 64 20 62 79 20 75 73 65 72 73 20 6f 66  ssed by users of
287d3 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f   the library..*/
287d4 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65  .../*.** Execute
287d5 20 53 51 4c 20 63 6f 64 65 2e 20 20 52 65 74 75   SQL code.  Retu
287d6 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 53 51  rn one of the SQ
287d7 4c 49 54 45 5f 20 73 75 63 63 65 73 73 2f 66 61  LITE_ success/fa
287d8 69 6c 75 72 65 0a 2a 2a 20 63 6f 64 65 73 2e 20  ilure.** codes. 
287d9 20 41 6c 73 6f 20 77 72 69 74 65 20 61 6e 20 65   Also write an e
287da 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74  rror message int
287db 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
287dc 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63  d from.** malloc
287dd 28 29 20 61 6e 64 20 6d 61 6b 65 20 2a 70 7a 45  () and make *pzE
287de 72 72 4d 73 67 20 70 6f 69 6e 74 20 74 6f 20 74  rrMsg point to t
287df 68 61 74 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a  hat message..**.
287e0 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 69 73  ** If the SQL is
287e1 20 61 20 71 75 65 72 79 2c 20 74 68 65 6e 20 66   a query, then f
287e2 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e 20 74  or each row in t
287e3 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a  he query result.
287e4 2a 2a 20 74 68 65 20 78 43 61 6c 6c 62 61 63 6b  ** the xCallback
287e5 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  () function is c
287e6 61 6c 6c 65 64 2e 20 20 70 41 72 67 20 62 65 63  alled.  pArg bec
287e7 6f 6d 65 73 20 74 68 65 20 66 69 72 73 74 0a 2a  omes the first.*
287e8 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 43  * argument to xC
287e9 61 6c 6c 62 61 63 6b 28 29 2e 20 20 49 66 20 78  allback().  If x
287ea 43 61 6c 6c 62 61 63 6b 3d 4e 55 4c 4c 20 74 68  Callback=NULL th
287eb 65 6e 20 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a  en no callback.*
287ec 2a 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 65 76  * is invoked, ev
287ed 65 6e 20 66 6f 72 20 71 75 65 72 69 65 73 2e 0a  en for queries..
287ee 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
287ef 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 0a  t sqlite3_exec(.
287f0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
287f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
287f2 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 6e   The database on
287f3 20 77 68 69 63 68 20 74 68 65 20 53 51 4c 20 65   which the SQL e
287f4 78 65 63 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e  xecutes */.  con
287f5 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20  st char *zSql,  
287f6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
287f7 53 51 4c 20 74 6f 20 62 65 20 65 78 65 63 75 74  SQL to be execut
287f8 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
287f9 63 61 6c 6c 62 61 63 6b 20 78 43 61 6c 6c 62 61  callback xCallba
287fa 63 6b 2c 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  ck, /* Invoke th
287fb 69 73 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74  is callback rout
287fc 69 6e 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ine */.  void *p
287fd 41 72 67 2c 20 20 20 20 20 20 20 20 20 20 20 20  Arg,            
287fe 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72       /* First ar
287ff 67 75 6d 65 6e 74 20 74 6f 20 78 43 61 6c 6c 62  gument to xCallb
28800 61 63 6b 28 29 20 2a 2f 0a 20 20 63 68 61 72 20  ack() */.  char 
28801 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20  **pzErrMsg      
28802 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
28803 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68  error messages h
28804 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
28805 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
28806 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
28807 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73  n code */.  cons
28808 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 6f 76 65  t char *zLeftove
28809 72 3b 20 20 20 20 20 20 2f 2a 20 54 61 69 6c 20  r;      /* Tail 
2880a 6f 66 20 75 6e 70 72 6f 63 65 73 73 65 64 20 53  of unprocessed S
2880b 51 4c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  QL */.  sqlite3_
2880c 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b  stmt *pStmt = 0;
2880d 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 72 65      /* The curre
2880e 6e 74 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  nt SQL statement
2880f 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43   */.  char **azC
28810 6f 6c 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ols = 0;        
28811 20 20 2f 2a 20 4e 61 6d 65 73 20 6f 66 20 72 65    /* Names of re
28812 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a  sult columns */.
28813 20 20 69 6e 74 20 6e 52 65 74 72 79 20 3d 20 30    int nRetry = 0
28814 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
28815 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 74 72 79   Number of retry
28816 20 61 74 74 65 6d 70 74 73 20 2a 2f 0a 20 20 69   attempts */.  i
28817 6e 74 20 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69  nt callbackIsIni
28818 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  t;         /* Tr
28819 75 65 20 69 66 20 63 61 6c 6c 62 61 63 6b 20 64  ue if callback d
2881a 61 74 61 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  ata is initializ
2881b 65 64 20 2a 2f 0a 0a 20 20 69 66 28 20 7a 53 71  ed */..  if( zSq
2881c 6c 3d 3d 30 20 29 20 7a 53 71 6c 20 3d 20 22 22  l==0 ) zSql = ""
2881d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
2881e 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2881f 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 72  ex);.  sqlite3Er
28820 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  ror(db, SQLITE_O
28821 4b 2c 20 30 29 3b 0a 20 20 77 68 69 6c 65 28 20  K, 0);.  while( 
28822 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  (rc==SQLITE_OK |
28823 7c 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43  | (rc==SQLITE_SC
28824 48 45 4d 41 20 26 26 20 28 2b 2b 6e 52 65 74 72  HEMA && (++nRetr
28825 79 29 3c 32 29 29 20 26 26 20 7a 53 71 6c 5b 30  y)<2)) && zSql[0
28826 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 43 6f  ] ){.    int nCo
28827 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a 2a 61 7a  l;.    char **az
28828 56 61 6c 73 20 3d 20 30 3b 0a 0a 20 20 20 20 70  Vals = 0;..    p
28829 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 72 63  Stmt = 0;.    rc
2882a 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61   = sqlite3_prepa
2882b 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  re(db, zSql, -1,
2882c 20 26 70 53 74 6d 74 2c 20 26 7a 4c 65 66 74 6f   &pStmt, &zLefto
2882d 76 65 72 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ver);.    assert
2882e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2882f 7c 7c 20 70 53 74 6d 74 3d 3d 30 20 29 3b 0a 20  || pStmt==0 );. 
28830 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
28831 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 63 6f  E_OK ){.      co
28832 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20  ntinue;.    }.  
28833 20 20 69 66 28 20 21 70 53 74 6d 74 20 29 7b 0a    if( !pStmt ){.
28834 20 20 20 20 20 20 2f 2a 20 74 68 69 73 20 68 61        /* this ha
28835 70 70 65 6e 73 20 66 6f 72 20 61 20 63 6f 6d 6d  ppens for a comm
28836 65 6e 74 20 6f 72 20 77 68 69 74 65 2d 73 70 61  ent or white-spa
28837 63 65 20 2a 2f 0a 20 20 20 20 20 20 7a 53 71 6c  ce */.      zSql
28838 20 3d 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20   = zLeftover;.  
28839 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
2883a 20 20 7d 0a 0a 20 20 20 20 63 61 6c 6c 62 61 63    }..    callbac
2883b 6b 49 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20  kIsInit = 0;.   
2883c 20 6e 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f   nCol = sqlite3_
2883d 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74  column_count(pSt
2883e 6d 74 29 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28  mt);..    while(
2883f 20 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20   1 ){.      int 
28840 69 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  i;.      rc = sq
28841 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
28842 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76  );..      /* Inv
28843 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  oke the callback
28844 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 72 65 71   function if req
28845 75 69 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 69  uired */.      i
28846 66 28 20 78 43 61 6c 6c 62 61 63 6b 20 26 26 20  f( xCallback && 
28847 28 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 72 63 20  (SQLITE_ROW==rc 
28848 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 53  || .          (S
28849 51 4c 49 54 45 5f 44 4f 4e 45 3d 3d 72 63 20 26  QLITE_DONE==rc &
2884a 26 20 21 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69  & !callbackIsIni
2884b 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t.              
2884c 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
2884d 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
2884e 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 29 29 20  _NullCallback)) 
2884f 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ){.        if( !
28850 63 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 20 29  callbackIsInit )
28851 7b 0a 20 20 20 20 20 20 20 20 20 20 61 7a 43 6f  {.          azCo
28852 6c 73 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ls = sqlite3DbMa
28853 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 32 2a 6e  llocZero(db, 2*n
28854 43 6f 6c 2a 73 69 7a 65 6f 66 28 63 6f 6e 73 74  Col*sizeof(const
28855 20 63 68 61 72 2a 29 20 2b 20 31 29 3b 0a 20 20   char*) + 1);.  
28856 20 20 20 20 20 20 20 20 69 66 28 20 61 7a 43 6f          if( azCo
28857 6c 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ls==0 ){.       
28858 20 20 20 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f       goto exec_o
28859 75 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ut;.          }.
2885a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
2885b 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
2885c 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 43  .            azC
2885d 6f 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a  ols[i] = (char *
2885e 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
2885f 6e 61 6d 65 28 70 53 74 6d 74 2c 20 69 29 3b 0a  name(pStmt, i);.
28860 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
28861 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
28862 4e 61 6d 65 28 29 20 69 6e 73 74 61 6c 6c 73 20  Name() installs 
28863 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 73 20  column names as 
28864 55 54 46 38 0a 20 20 20 20 20 20 20 20 20 20 20  UTF8.           
28865 20 2a 2a 20 73 74 72 69 6e 67 73 20 73 6f 20 74   ** strings so t
28866 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66  here is no way f
28867 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  or sqlite3_colum
28868 6e 5f 6e 61 6d 65 28 29 20 74 6f 20 66 61 69 6c  n_name() to fail
28869 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  . */.           
2886a 20 61 73 73 65 72 74 28 20 61 7a 43 6f 6c 73 5b   assert( azCols[
2886b 69 5d 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  i]!=0 );.       
2886c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63     }.          c
2886d 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 20 3d 20  allbackIsInit = 
2886e 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
2886f 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
28870 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
28871 20 20 20 20 20 61 7a 56 61 6c 73 20 3d 20 26 61       azVals = &a
28872 7a 43 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a 20 20 20  zCols[nCol];.   
28873 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20         for(i=0; 
28874 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
28875 20 20 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73            azVals
28876 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a 29 73 71  [i] = (char *)sq
28877 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
28878 74 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20  t(pStmt, i);.   
28879 20 20 20 20 20 20 20 20 20 69 66 28 20 21 61 7a           if( !az
2887a 56 61 6c 73 5b 69 5d 20 26 26 20 73 71 6c 69 74  Vals[i] && sqlit
2887b 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 70  e3_column_type(p
2887c 53 74 6d 74 2c 20 69 29 21 3d 53 51 4c 49 54 45  Stmt, i)!=SQLITE
2887d 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20  _NULL ){.       
2887e 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f         db->mallo
2887f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
28880 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
28881 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20  exec_out;.      
28882 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
28883 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
28884 20 20 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62        if( xCallb
28885 61 63 6b 28 70 41 72 67 2c 20 6e 43 6f 6c 2c 20  ack(pArg, nCol, 
28886 61 7a 56 61 6c 73 2c 20 61 7a 43 6f 6c 73 29 20  azVals, azCols) 
28887 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
28888 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a  = SQLITE_ABORT;.
28889 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2888a 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56  3VdbeFinalize((V
2888b 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20 20  dbe *)pStmt);.  
2888c 20 20 20 20 20 20 20 20 70 53 74 6d 74 20 3d 20          pStmt = 
2888d 30 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  0;.          sql
2888e 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
2888f 4c 49 54 45 5f 41 42 4f 52 54 2c 20 30 29 3b 0a  LITE_ABORT, 0);.
28890 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
28891 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  xec_out;.       
28892 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20   }.      }..    
28893 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
28894 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 20  _ROW ){.        
28895 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
28896 46 69 6e 61 6c 69 7a 65 28 28 56 64 62 65 20 2a  Finalize((Vdbe *
28897 29 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 20  )pStmt);.       
28898 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20   pStmt = 0;.    
28899 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2889a 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a 20 20 20  TE_SCHEMA ){.   
2889b 20 20 20 20 20 20 20 6e 52 65 74 72 79 20 3d 20         nRetry = 
2889c 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 53 71  0;.          zSq
2889d 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20  l = zLeftover;. 
2889e 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
2889f 73 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 7a  sqlite3Isspace(z
288a0 53 71 6c 5b 30 5d 29 20 29 20 7a 53 71 6c 2b 2b  Sql[0]) ) zSql++
288a1 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
288a2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
288a3 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71   }.    }..    sq
288a4 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
288a5 61 7a 43 6f 6c 73 29 3b 0a 20 20 20 20 61 7a 43  azCols);.    azC
288a6 6f 6c 73 20 3d 20 30 3b 0a 20 20 7d 0a 0a 65 78  ols = 0;.  }..ex
288a7 65 63 5f 6f 75 74 3a 0a 20 20 69 66 28 20 70 53  ec_out:.  if( pS
288a8 74 6d 74 20 29 20 73 71 6c 69 74 65 33 56 64 62  tmt ) sqlite3Vdb
288a9 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62 65 20  eFinalize((Vdbe 
288aa 2a 29 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69  *)pStmt);.  sqli
288ab 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 7a  te3DbFree(db, az
288ac 43 6f 6c 73 29 3b 0a 0a 20 20 72 63 20 3d 20 73  Cols);..  rc = s
288ad 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
288ae 2c 20 72 63 29 3b 0a 20 20 69 66 28 20 72 63 21  , rc);.  if( rc!
288af 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 41 4c  =SQLITE_OK && AL
288b0 57 41 59 53 28 72 63 3d 3d 73 71 6c 69 74 65 33  WAYS(rc==sqlite3
288b1 5f 65 72 72 63 6f 64 65 28 64 62 29 29 20 26 26  _errcode(db)) &&
288b2 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20   pzErrMsg ){.   
288b3 20 69 6e 74 20 6e 45 72 72 4d 73 67 20 3d 20 31   int nErrMsg = 1
288b4 20 2b 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   + sqlite3Strlen
288b5 33 30 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  30(sqlite3_errms
288b6 67 28 64 62 29 29 3b 0a 20 20 20 20 2a 70 7a 45  g(db));.    *pzE
288b7 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d  rrMsg = sqlite3M
288b8 61 6c 6c 6f 63 28 6e 45 72 72 4d 73 67 29 3b 0a  alloc(nErrMsg);.
288b9 20 20 20 20 69 66 28 20 2a 70 7a 45 72 72 4d 73      if( *pzErrMs
288ba 67 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  g ){.      memcp
288bb 79 28 2a 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c  y(*pzErrMsg, sql
288bc 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 2c  ite3_errmsg(db),
288bd 20 6e 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 7d   nErrMsg);.    }
288be 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
288bf 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
288c0 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
288c1 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d  r(db, SQLITE_NOM
288c2 45 4d 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  EM, 0);.    }.  
288c3 7d 65 6c 73 65 20 69 66 28 20 70 7a 45 72 72 4d  }else if( pzErrM
288c4 73 67 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72  sg ){.    *pzErr
288c5 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  Msg = 0;.  }..  
288c6 61 73 73 65 72 74 28 20 28 72 63 26 64 62 2d 3e  assert( (rc&db->
288c7 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a  errMask)==rc );.
288c8 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
288c9 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
288ca 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
288cb 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
288cc 2a 20 45 6e 64 20 6f 66 20 6c 65 67 61 63 79 2e  * End of legacy.
288cd 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
288ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288d0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
288d1 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c 6f 61  * Begin file loa
288d2 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  dext.c *********
288d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288d5 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e  /./*.** 2006 Jun
288d6 65 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  e 7.**.** The au
288d7 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
288d8 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
288d9 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
288da 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
288db 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
288dc 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
288dd 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
288de 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
288df 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
288e0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
288e1 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
288e2 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
288e3 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
288e4 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
288e5 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
288e6 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
288e7 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
288e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
288ec 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
288ed 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
288ee 65 64 20 74 6f 20 64 79 6e 61 6d 69 63 61 6c 6c  ed to dynamicall
288ef 79 20 6c 6f 61 64 20 65 78 74 65 6e 73 69 6f 6e  y load extension
288f0 73 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 53 51  s into.** the SQ
288f1 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2f  Lite library..*/
288f2 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
288f3 5f 43 4f 52 45 0a 20 20 23 64 65 66 69 6e 65 20  _CORE.  #define 
288f4 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 20 20 2f  SQLITE_CORE 1  /
288f5 2a 20 44 69 73 61 62 6c 65 20 74 68 65 20 41 50  * Disable the AP
288f6 49 20 72 65 64 65 66 69 6e 69 74 69 6f 6e 20 69  I redefinition i
288f7 6e 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 2a  n sqlite3ext.h *
288f8 2f 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a 2a  /.#endif./******
288f9 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
288fa 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 69 6e   sqlite3ext.h in
288fb 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6c   the middle of l
288fc 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  oadext.c *******
288fd 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
288fe 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
288ff 69 6c 65 20 73 71 6c 69 74 65 33 65 78 74 2e 68  ile sqlite3ext.h
28900 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
28901 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28902 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
28903 30 30 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a  006 June 7.**.**
28904 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
28905 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
28906 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
28907 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
28908 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
28909 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2890a 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2890b 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2890c 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
2890d 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
2890e 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
2890f 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
28910 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
28911 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
28912 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
28913 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
28914 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
28915 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28916 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28917 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28918 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28919 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
2891a 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65  s header file de
2891b 66 69 6e 65 73 20 74 68 65 20 53 51 4c 69 74 65  fines the SQLite
2891c 20 69 6e 74 65 72 66 61 63 65 20 66 6f 72 20 75   interface for u
2891d 73 65 20 62 79 0a 2a 2a 20 73 68 61 72 65 64 20  se by.** shared 
2891e 6c 69 62 72 61 72 69 65 73 20 74 68 61 74 20 77  libraries that w
2891f 61 6e 74 20 74 6f 20 62 65 20 69 6d 70 6f 72 74  ant to be import
28920 65 64 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73  ed as extensions
28921 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 53 51 4c 69   into.** an SQLi
28922 74 65 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 68  te instance.  Sh
28923 61 72 65 64 20 6c 69 62 72 61 72 69 65 73 20 74  ared libraries t
28924 68 61 74 20 69 6e 74 65 6e 64 20 74 6f 20 62 65  hat intend to be
28925 20 6c 6f 61 64 65 64 0a 2a 2a 20 61 73 20 65 78   loaded.** as ex
28926 74 65 6e 73 69 6f 6e 73 20 62 79 20 53 51 4c 69  tensions by SQLi
28927 74 65 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75  te should #inclu
28928 64 65 20 74 68 69 73 20 66 69 6c 65 20 69 6e 73  de this file ins
28929 74 65 61 64 20 6f 66 20 0a 2a 2a 20 73 71 6c 69  tead of .** sqli
2892a 74 65 33 2e 68 2e 0a 2a 2f 0a 23 69 66 6e 64 65  te3.h..*/.#ifnde
2892b 66 20 5f 53 51 4c 49 54 45 33 45 58 54 5f 48 5f  f _SQLITE3EXT_H_
2892c 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54 45  .#define _SQLITE
2892d 33 45 58 54 5f 48 5f 0a 0a 74 79 70 65 64 65 66  3EXT_H_..typedef
2892e 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
2892f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73 71 6c  api_routines sql
28930 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
28931 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  s;../*.** The fo
28932 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
28933 65 20 68 6f 6c 64 73 20 70 6f 69 6e 74 65 72 73  e holds pointers
28934 20 74 6f 20 61 6c 6c 20 6f 66 20 74 68 65 20 53   to all of the S
28935 51 4c 69 74 65 20 41 50 49 0a 2a 2a 20 72 6f 75  QLite API.** rou
28936 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 57 41 52  tines..**.** WAR
28937 4e 49 4e 47 3a 20 20 49 6e 20 6f 72 64 65 72 20  NING:  In order 
28938 74 6f 20 6d 61 69 6e 74 61 69 6e 20 62 61 63 6b  to maintain back
28939 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
2893a 69 74 79 2c 20 61 64 64 20 6e 65 77 0a 2a 2a 20  ity, add new.** 
2893b 69 6e 74 65 72 66 61 63 65 73 20 74 6f 20 74 68  interfaces to th
2893c 65 20 65 6e 64 20 6f 66 20 74 68 69 73 20 73 74  e end of this st
2893d 72 75 63 74 75 72 65 20 6f 6e 6c 79 2e 20 20 49  ructure only.  I
2893e 66 20 79 6f 75 20 69 6e 73 65 72 74 20 6e 65 77  f you insert new
2893f 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 69  .** interfaces i
28940 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
28941 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2c 20  this structure, 
28942 74 68 65 6e 20 6f 6c 64 65 72 20 64 69 66 66 65  then older diffe
28943 72 65 6e 74 0a 2a 2a 20 76 65 72 73 69 6f 6e 73  rent.** versions
28944 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c 6c 20   of SQLite will 
28945 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 6c  not be able to l
28946 6f 61 64 20 65 61 63 68 20 6f 74 68 65 72 73 27  oad each others'
28947 20 73 68 61 72 65 64 0a 2a 2a 20 6c 69 62 72 61   shared.** libra
28948 72 69 65 73 21 0a 2a 2f 0a 73 74 72 75 63 74 20  ries!.*/.struct 
28949 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
2894a 69 6e 65 73 20 7b 0a 20 20 76 6f 69 64 20 2a 20  ines {.  void * 
2894b 28 2a 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  (*aggregate_cont
2894c 65 78 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ext)(sqlite3_con
2894d 74 65 78 74 2a 2c 69 6e 74 20 6e 42 79 74 65 73  text*,int nBytes
2894e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 61 67 67 72  );.  int  (*aggr
2894f 65 67 61 74 65 5f 63 6f 75 6e 74 29 28 73 71 6c  egate_count)(sql
28950 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
28951 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 62 6c    int  (*bind_bl
28952 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ob)(sqlite3_stmt
28953 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
28954 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28 2a 29 28  *,int n,void(*)(
28955 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20  void*));.  int  
28956 28 2a 62 69 6e 64 5f 64 6f 75 62 6c 65 29 28 73  (*bind_double)(s
28957 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
28958 2c 64 6f 75 62 6c 65 29 3b 0a 20 20 69 6e 74 20  ,double);.  int 
28959 20 28 2a 62 69 6e 64 5f 69 6e 74 29 28 73 71 6c   (*bind_int)(sql
2895a 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 69  ite3_stmt*,int,i
2895b 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  nt);.  int  (*bi
2895c 6e 64 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65  nd_int64)(sqlite
2895d 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 73 71 6c 69  3_stmt*,int,sqli
2895e 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 69 6e 74  te_int64);.  int
2895f 20 20 28 2a 62 69 6e 64 5f 6e 75 6c 6c 29 28 73    (*bind_null)(s
28960 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
28961 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64  );.  int  (*bind
28962 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
28963 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  )(sqlite3_stmt*)
28964 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f  ;.  int  (*bind_
28965 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 29  parameter_index)
28966 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 63  (sqlite3_stmt*,c
28967 6f 6e 73 74 20 63 68 61 72 2a 7a 4e 61 6d 65 29  onst char*zName)
28968 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
28969 20 28 2a 62 69 6e 64 5f 70 61 72 61 6d 65 74 65   (*bind_paramete
2896a 72 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f  r_name)(sqlite3_
2896b 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e  stmt*,int);.  in
2896c 74 20 20 28 2a 62 69 6e 64 5f 74 65 78 74 29 28  t  (*bind_text)(
2896d 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
2896e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  t,const char*,in
2896f 74 20 6e 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  t n,void(*)(void
28970 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  *));.  int  (*bi
28971 6e 64 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74  nd_text16)(sqlit
28972 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e  e3_stmt*,int,con
28973 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69  st void*,int,voi
28974 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
28975 69 6e 74 20 20 28 2a 62 69 6e 64 5f 76 61 6c 75  int  (*bind_valu
28976 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
28977 2c 69 6e 74 2c 63 6f 6e 73 74 20 73 71 6c 69 74  ,int,const sqlit
28978 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e  e3_value*);.  in
28979 74 20 20 28 2a 62 75 73 79 5f 68 61 6e 64 6c 65  t  (*busy_handle
2897a 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28  r)(sqlite3*,int(
2897b 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 76 6f  *)(void*,int),vo
2897c 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  id*);.  int  (*b
2897d 75 73 79 5f 74 69 6d 65 6f 75 74 29 28 73 71 6c  usy_timeout)(sql
2897e 69 74 65 33 2a 2c 69 6e 74 20 6d 73 29 3b 0a 20  ite3*,int ms);. 
2897f 20 69 6e 74 20 20 28 2a 63 68 61 6e 67 65 73 29   int  (*changes)
28980 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e  (sqlite3*);.  in
28981 74 20 20 28 2a 63 6c 6f 73 65 29 28 73 71 6c 69  t  (*close)(sqli
28982 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  te3*);.  int  (*
28983 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
28984 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a  )(sqlite3*,void*
28985 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73  ,void(*)(void*,s
28986 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78  qlite3*,int eTex
28987 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a  tRep,const char*
28988 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c  ));.  int  (*col
28989 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 29  lation_needed16)
2898a 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c  (sqlite3*,void*,
2898b 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71  void(*)(void*,sq
2898c 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74  lite3*,int eText
2898d 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  Rep,const void*)
2898e 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
2898f 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 29  * (*column_blob)
28990 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
28991 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20  nt iCol);.  int 
28992 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 29   (*column_bytes)
28993 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
28994 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20  nt iCol);.  int 
28995 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31   (*column_bytes1
28996 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  6)(sqlite3_stmt*
28997 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e  ,int iCol);.  in
28998 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 63 6f 75 6e  t  (*column_coun
28999 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  t)(sqlite3_stmt*
2899a 70 53 74 6d 74 29 3b 0a 20 20 63 6f 6e 73 74 20  pStmt);.  const 
2899b 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  char * (*column_
2899c 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 29 28 73  database_name)(s
2899d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2899e 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
2899f 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  * (*column_datab
289a0 61 73 65 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69  ase_name16)(sqli
289a1 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
289a2 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28    const char * (
289a3 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  *column_decltype
289a4 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
289a5 69 6e 74 20 69 29 3b 0a 20 20 63 6f 6e 73 74 20  int i);.  const 
289a6 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f  void * (*column_
289a7 64 65 63 6c 74 79 70 65 31 36 29 28 73 71 6c 69  decltype16)(sqli
289a8 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
289a9 20 20 64 6f 75 62 6c 65 20 20 28 2a 63 6f 6c 75    double  (*colu
289aa 6d 6e 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  mn_double)(sqlit
289ab 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f  e3_stmt*,int iCo
289ac 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c  l);.  int  (*col
289ad 75 6d 6e 5f 69 6e 74 29 28 73 71 6c 69 74 65 33  umn_int)(sqlite3
289ae 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29  _stmt*,int iCol)
289af 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
289b0 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34    (*column_int64
289b1 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
289b2 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e  int iCol);.  con
289b3 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75  st char * (*colu
289b4 6d 6e 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33  mn_name)(sqlite3
289b5 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63  _stmt*,int);.  c
289b6 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f  onst void * (*co
289b7 6c 75 6d 6e 5f 6e 61 6d 65 31 36 29 28 73 71 6c  lumn_name16)(sql
289b8 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
289b9 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
289ba 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  (*column_origin_
289bb 6e 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74  name)(sqlite3_st
289bc 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73  mt*,int);.  cons
289bd 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d  t void * (*colum
289be 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 29  n_origin_name16)
289bf 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
289c0 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  nt);.  const cha
289c1 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62  r * (*column_tab
289c2 6c 65 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65 33  le_name)(sqlite3
289c3 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63  _stmt*,int);.  c
289c4 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f  onst void * (*co
289c5 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31  lumn_table_name1
289c6 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  6)(sqlite3_stmt*
289c7 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 75  ,int);.  const u
289c8 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 20 28  nsigned char * (
289c9 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 29 28 73 71  *column_text)(sq
289ca 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
289cb 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73 74 20 76  iCol);.  const v
289cc 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74  oid * (*column_t
289cd 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 73  ext16)(sqlite3_s
289ce 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
289cf 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f    int  (*column_
289d0 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 73 74  type)(sqlite3_st
289d1 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
289d2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20   sqlite3_value* 
289d3 28 2a 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 29 28  (*column_value)(
289d4 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
289d5 74 20 69 43 6f 6c 29 3b 0a 20 20 76 6f 69 64 20  t iCol);.  void 
289d6 2a 20 28 2a 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 29  * (*commit_hook)
289d7 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29  (sqlite3*,int(*)
289d8 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a  (void*),void*);.
289d9 20 20 69 6e 74 20 20 28 2a 63 6f 6d 70 6c 65 74    int  (*complet
289da 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 73 71  e)(const char*sq
289db 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6d  l);.  int  (*com
289dc 70 6c 65 74 65 31 36 29 28 63 6f 6e 73 74 20 76  plete16)(const v
289dd 6f 69 64 2a 73 71 6c 29 3b 0a 20 20 69 6e 74 20  oid*sql);.  int 
289de 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74   (*create_collat
289df 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  ion)(sqlite3*,co
289e0 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f  nst char*,int,vo
289e1 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a  id*,int(*)(void*
289e2 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
289e3 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
289e4 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65  ));.  int  (*cre
289e5 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 29  ate_collation16)
289e6 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
289e7 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c  void*,int,void*,
289e8 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
289e9 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
289ea 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a  ,const void*));.
289eb 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f    int  (*create_
289ec 66 75 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65  function)(sqlite
289ed 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  3*,const char*,i
289ee 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69  nt,int,void*,voi
289ef 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74  d (*xFunc)(sqlit
289f0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
289f1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
289f2 2c 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73  ,void (*xStep)(s
289f3 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
289f4 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
289f5 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 46 69 6e  e**),void (*xFin
289f6 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  al)(sqlite3_cont
289f7 65 78 74 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28  ext*));.  int  (
289f8 2a 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  *create_function
289f9 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  16)(sqlite3*,con
289fa 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  st void*,int,int
289fb 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 78 46  ,void*,void (*xF
289fc 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
289fd 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
289fe 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20  3_value**),void 
289ff 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33  (*xStep)(sqlite3
28a00 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
28a01 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76  lite3_value**),v
28a02 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71  oid (*xFinal)(sq
28a03 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 29  lite3_context*))
28a04 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74 65  ;.  int (*create
28a05 5f 6d 6f 64 75 6c 65 29 28 73 71 6c 69 74 65 33  _module)(sqlite3
28a06 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
28a07 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
28a08 6c 65 2a 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e  le*,void*);.  in
28a09 74 20 20 28 2a 64 61 74 61 5f 63 6f 75 6e 74 29  t  (*data_count)
28a0a 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53  (sqlite3_stmt*pS
28a0b 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 20  tmt);.  sqlite3 
28a0c 2a 20 28 2a 64 62 5f 68 61 6e 64 6c 65 29 28 73  * (*db_handle)(s
28a0d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20  qlite3_stmt*);. 
28a0e 20 69 6e 74 20 28 2a 64 65 63 6c 61 72 65 5f 76   int (*declare_v
28a0f 74 61 62 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  tab)(sqlite3*,co
28a10 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e  nst char*);.  in
28a11 74 20 20 28 2a 65 6e 61 62 6c 65 5f 73 68 61 72  t  (*enable_shar
28a12 65 64 5f 63 61 63 68 65 29 28 69 6e 74 29 3b 0a  ed_cache)(int);.
28a13 20 20 69 6e 74 20 20 28 2a 65 72 72 63 6f 64 65    int  (*errcode
28a14 29 28 73 71 6c 69 74 65 33 2a 64 62 29 3b 0a 20  )(sqlite3*db);. 
28a15 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a   const char * (*
28a16 65 72 72 6d 73 67 29 28 73 71 6c 69 74 65 33 2a  errmsg)(sqlite3*
28a17 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
28a18 2a 20 28 2a 65 72 72 6d 73 67 31 36 29 28 73 71  * (*errmsg16)(sq
28a19 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20 20  lite3*);.  int  
28a1a 28 2a 65 78 65 63 29 28 73 71 6c 69 74 65 33 2a  (*exec)(sqlite3*
28a1b 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c  ,const char*,sql
28a1c 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 2c 76 6f  ite3_callback,vo
28a1d 69 64 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20 20 69  id*,char**);.  i
28a1e 6e 74 20 20 28 2a 65 78 70 69 72 65 64 29 28 73  nt  (*expired)(s
28a1f 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20  qlite3_stmt*);. 
28a20 20 69 6e 74 20 20 28 2a 66 69 6e 61 6c 69 7a 65   int  (*finalize
28a21 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70  )(sqlite3_stmt*p
28a22 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64 20 20 28  Stmt);.  void  (
28a23 2a 66 72 65 65 29 28 76 6f 69 64 2a 29 3b 0a 20  *free)(void*);. 
28a24 20 76 6f 69 64 20 20 28 2a 66 72 65 65 5f 74 61   void  (*free_ta
28a25 62 6c 65 29 28 63 68 61 72 2a 2a 72 65 73 75 6c  ble)(char**resul
28a26 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 67 65 74  t);.  int  (*get
28a27 5f 61 75 74 6f 63 6f 6d 6d 69 74 29 28 73 71 6c  _autocommit)(sql
28a28 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69 64 20 2a  ite3*);.  void *
28a29 20 28 2a 67 65 74 5f 61 75 78 64 61 74 61 29 28   (*get_auxdata)(
28a2a 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
28a2b 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ,int);.  int  (*
28a2c 67 65 74 5f 74 61 62 6c 65 29 28 73 71 6c 69 74  get_table)(sqlit
28a2d 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
28a2e 63 68 61 72 2a 2a 2a 2c 69 6e 74 2a 2c 69 6e 74  char***,int*,int
28a2f 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74  *,char**);.  int
28a30 20 20 28 2a 67 6c 6f 62 61 6c 5f 72 65 63 6f 76    (*global_recov
28a31 65 72 29 28 76 6f 69 64 29 3b 0a 20 20 76 6f 69  er)(void);.  voi
28a32 64 20 20 28 2a 69 6e 74 65 72 72 75 70 74 78 29  d  (*interruptx)
28a33 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 73 71  (sqlite3*);.  sq
28a34 6c 69 74 65 5f 69 6e 74 36 34 20 20 28 2a 6c 61  lite_int64  (*la
28a35 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 29  st_insert_rowid)
28a36 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 63 6f  (sqlite3*);.  co
28a37 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 6c 69 62  nst char * (*lib
28a38 76 65 72 73 69 6f 6e 29 28 76 6f 69 64 29 3b 0a  version)(void);.
28a39 20 20 69 6e 74 20 20 28 2a 6c 69 62 76 65 72 73    int  (*libvers
28a3a 69 6f 6e 5f 6e 75 6d 62 65 72 29 28 76 6f 69 64  ion_number)(void
28a3b 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 6d 61 6c  );.  void *(*mal
28a3c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 63 68 61  loc)(int);.  cha
28a3d 72 20 2a 20 28 2a 6d 70 72 69 6e 74 66 29 28 63  r * (*mprintf)(c
28a3e 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b  onst char*,...);
28a3f 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65 6e 29 28  .  int  (*open)(
28a40 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69  const char*,sqli
28a41 74 65 33 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28  te3**);.  int  (
28a42 2a 6f 70 65 6e 31 36 29 28 63 6f 6e 73 74 20 76  *open16)(const v
28a43 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a 2a 29 3b  oid*,sqlite3**);
28a44 0a 20 20 69 6e 74 20 20 28 2a 70 72 65 70 61 72  .  int  (*prepar
28a45 65 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  e)(sqlite3*,cons
28a46 74 20 63 68 61 72 2a 2c 69 6e 74 2c 73 71 6c 69  t char*,int,sqli
28a47 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74  te3_stmt**,const
28a48 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20   char**);.  int 
28a49 20 28 2a 70 72 65 70 61 72 65 31 36 29 28 73 71   (*prepare16)(sq
28a4a 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69  lite3*,const voi
28a4b 64 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73  d*,int,sqlite3_s
28a4c 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f 69 64  tmt**,const void
28a4d 2a 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  **);.  void * (*
28a4e 70 72 6f 66 69 6c 65 29 28 73 71 6c 69 74 65 33  profile)(sqlite3
28a4f 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c  *,void(*)(void*,
28a50 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69  const char*,sqli
28a51 74 65 5f 75 69 6e 74 36 34 29 2c 76 6f 69 64 2a  te_uint64),void*
28a52 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 70 72 6f  );.  void  (*pro
28a53 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 29 28 73  gress_handler)(s
28a54 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 69 6e 74 28  qlite3*,int,int(
28a55 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29  *)(void*),void*)
28a56 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 72 65 61 6c  ;.  void *(*real
28a57 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74 29 3b  loc)(void*,int);
28a58 0a 20 20 69 6e 74 20 20 28 2a 72 65 73 65 74 29  .  int  (*reset)
28a59 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53  (sqlite3_stmt*pS
28a5a 74 6d 74 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  tmt);.  void  (*
28a5b 72 65 73 75 6c 74 5f 62 6c 6f 62 29 28 73 71 6c  result_blob)(sql
28a5c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f  ite3_context*,co
28a5d 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f  nst void*,int,vo
28a5e 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20  id(*)(void*));. 
28a5f 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f   void  (*result_
28a60 64 6f 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f  double)(sqlite3_
28a61 63 6f 6e 74 65 78 74 2a 2c 64 6f 75 62 6c 65 29  context*,double)
28a62 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
28a63 6c 74 5f 65 72 72 6f 72 29 28 73 71 6c 69 74 65  lt_error)(sqlite
28a64 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74  3_context*,const
28a65 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 20 20 76   char*,int);.  v
28a66 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 65 72  oid  (*result_er
28a67 72 6f 72 31 36 29 28 73 71 6c 69 74 65 33 5f 63  ror16)(sqlite3_c
28a68 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f  ontext*,const vo
28a69 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64  id*,int);.  void
28a6a 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e 74 29 28    (*result_int)(
28a6b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
28a6c 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 20 28  ,int);.  void  (
28a6d 2a 72 65 73 75 6c 74 5f 69 6e 74 36 34 29 28 73  *result_int64)(s
28a6e 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
28a6f 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20  sqlite_int64);. 
28a70 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f   void  (*result_
28a71 6e 75 6c 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f  null)(sqlite3_co
28a72 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f 69 64 20  ntext*);.  void 
28a73 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 29 28   (*result_text)(
28a74 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
28a75 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
28a76 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
28a77 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
28a78 6c 74 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74  lt_text16)(sqlit
28a79 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
28a7a 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64  t void*,int,void
28a7b 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76  (*)(void*));.  v
28a7c 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65  oid  (*result_te
28a7d 78 74 31 36 62 65 29 28 73 71 6c 69 74 65 33 5f  xt16be)(sqlite3_
28a7e 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76  context*,const v
28a7f 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29  oid*,int,void(*)
28a80 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64  (void*));.  void
28a81 20 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 31    (*result_text1
28a82 36 6c 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  6le)(sqlite3_con
28a83 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64  text*,const void
28a84 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f  *,int,void(*)(vo
28a85 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28  id*));.  void  (
28a86 2a 72 65 73 75 6c 74 5f 76 61 6c 75 65 29 28 73  *result_value)(s
28a87 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
28a88 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
28a89 0a 20 20 76 6f 69 64 20 2a 20 28 2a 72 6f 6c 6c  .  void * (*roll
28a8a 62 61 63 6b 5f 68 6f 6f 6b 29 28 73 71 6c 69 74  back_hook)(sqlit
28a8b 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  e3*,void(*)(void
28a8c 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74  *),void*);.  int
28a8d 20 20 28 2a 73 65 74 5f 61 75 74 68 6f 72 69 7a    (*set_authoriz
28a8e 65 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  er)(sqlite3*,int
28a8f 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  (*)(void*,int,co
28a90 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
28a91 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
28a92 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 76  *,const char*),v
28a93 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28  oid*);.  void  (
28a94 2a 73 65 74 5f 61 75 78 64 61 74 61 29 28 73 71  *set_auxdata)(sq
28a95 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
28a96 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a  nt,void*,void (*
28a97 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 63 68 61  )(void*));.  cha
28a98 72 20 2a 20 28 2a 73 6e 70 72 69 6e 74 66 29 28  r * (*snprintf)(
28a99 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e 73 74 20  int,char*,const 
28a9a 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 20 20 69 6e  char*,...);.  in
28a9b 74 20 20 28 2a 73 74 65 70 29 28 73 71 6c 69 74  t  (*step)(sqlit
28a9c 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74  e3_stmt*);.  int
28a9d 20 20 28 2a 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e    (*table_column
28a9e 5f 6d 65 74 61 64 61 74 61 29 28 73 71 6c 69 74  _metadata)(sqlit
28a9f 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  e3*,const char*,
28aa0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
28aa1 74 20 63 68 61 72 2a 2c 63 68 61 72 20 63 6f 6e  t char*,char con
28aa2 73 74 2a 2a 2c 63 68 61 72 20 63 6f 6e 73 74 2a  st**,char const*
28aa3 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a  *,int*,int*,int*
28aa4 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 74 68 72  );.  void  (*thr
28aa5 65 61 64 5f 63 6c 65 61 6e 75 70 29 28 76 6f 69  ead_cleanup)(voi
28aa6 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a 74 6f 74  d);.  int  (*tot
28aa7 61 6c 5f 63 68 61 6e 67 65 73 29 28 73 71 6c 69  al_changes)(sqli
28aa8 74 65 33 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20  te3*);.  void * 
28aa9 28 2a 74 72 61 63 65 29 28 73 71 6c 69 74 65 33  (*trace)(sqlite3
28aaa 2a 2c 76 6f 69 64 28 2a 78 54 72 61 63 65 29 28  *,void(*xTrace)(
28aab 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
28aac 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74  *),void*);.  int
28aad 20 20 28 2a 74 72 61 6e 73 66 65 72 5f 62 69 6e    (*transfer_bin
28aae 64 69 6e 67 73 29 28 73 71 6c 69 74 65 33 5f 73  dings)(sqlite3_s
28aaf 74 6d 74 2a 2c 73 71 6c 69 74 65 33 5f 73 74 6d  tmt*,sqlite3_stm
28ab0 74 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  t*);.  void * (*
28ab1 75 70 64 61 74 65 5f 68 6f 6f 6b 29 28 73 71 6c  update_hook)(sql
28ab2 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f  ite3*,void(*)(vo
28ab3 69 64 2a 2c 69 6e 74 20 2c 63 68 61 72 20 63 6f  id*,int ,char co
28ab4 6e 73 74 2a 2c 63 68 61 72 20 63 6f 6e 73 74 2a  nst*,char const*
28ab5 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 2c 76  ,sqlite_int64),v
28ab6 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20  oid*);.  void * 
28ab7 28 2a 75 73 65 72 5f 64 61 74 61 29 28 73 71 6c  (*user_data)(sql
28ab8 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a  ite3_context*);.
28ab9 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28    const void * (
28aba 2a 76 61 6c 75 65 5f 62 6c 6f 62 29 28 73 71 6c  *value_blob)(sql
28abb 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
28abc 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 62 79 74  int  (*value_byt
28abd 65 73 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  es)(sqlite3_valu
28abe 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61  e*);.  int  (*va
28abf 6c 75 65 5f 62 79 74 65 73 31 36 29 28 73 71 6c  lue_bytes16)(sql
28ac0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
28ac1 64 6f 75 62 6c 65 20 20 28 2a 76 61 6c 75 65 5f  double  (*value_
28ac2 64 6f 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f  double)(sqlite3_
28ac3 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20  value*);.  int  
28ac4 28 2a 76 61 6c 75 65 5f 69 6e 74 29 28 73 71 6c  (*value_int)(sql
28ac5 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
28ac6 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28 2a  sqlite_int64  (*
28ac7 76 61 6c 75 65 5f 69 6e 74 36 34 29 28 73 71 6c  value_int64)(sql
28ac8 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20  ite3_value*);.  
28ac9 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f 6e 75 6d  int  (*value_num
28aca 65 72 69 63 5f 74 79 70 65 29 28 73 71 6c 69 74  eric_type)(sqlit
28acb 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f  e3_value*);.  co
28acc 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
28acd 72 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78 74  r * (*value_text
28ace 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
28acf 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
28ad0 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78 74 31 36  * (*value_text16
28ad1 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
28ad2 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
28ad3 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78 74 31 36  * (*value_text16
28ad4 62 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  be)(sqlite3_valu
28ad5 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69  e*);.  const voi
28ad6 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65 78 74  d * (*value_text
28ad7 31 36 6c 65 29 28 73 71 6c 69 74 65 33 5f 76 61  16le)(sqlite3_va
28ad8 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  lue*);.  int  (*
28ad9 76 61 6c 75 65 5f 74 79 70 65 29 28 73 71 6c 69  value_type)(sqli
28ada 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 63  te3_value*);.  c
28adb 68 61 72 20 2a 28 2a 76 6d 70 72 69 6e 74 66 29  har *(*vmprintf)
28adc 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 76 61 5f  (const char*,va_
28add 6c 69 73 74 29 3b 0a 20 20 2f 2a 20 41 64 64 65  list);.  /* Adde
28ade 64 20 3f 3f 3f 20 2a 2f 0a 20 20 69 6e 74 20 28  d ??? */.  int (
28adf 2a 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  *overload_functi
28ae0 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f  on)(sqlite3*, co
28ae1 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e  nst char *zFuncN
28ae2 61 6d 65 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a  ame, int nArg);.
28ae3 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 33 2e    /* Added by 3.
28ae4 33 2e 31 33 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  3.13 */.  int (*
28ae5 70 72 65 70 61 72 65 5f 76 32 29 28 73 71 6c 69  prepare_v2)(sqli
28ae6 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
28ae7 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d  ,int,sqlite3_stm
28ae8 74 2a 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2a  t**,const char**
28ae9 29 3b 0a 20 20 69 6e 74 20 28 2a 70 72 65 70 61  );.  int (*prepa
28aea 72 65 31 36 5f 76 32 29 28 73 71 6c 69 74 65 33  re16_v2)(sqlite3
28aeb 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
28aec 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a  t,sqlite3_stmt**
28aed 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2a 29 3b 0a  ,const void**);.
28aee 20 20 69 6e 74 20 28 2a 63 6c 65 61 72 5f 62 69    int (*clear_bi
28aef 6e 64 69 6e 67 73 29 28 73 71 6c 69 74 65 33 5f  ndings)(sqlite3_
28af0 73 74 6d 74 2a 29 3b 0a 20 20 2f 2a 20 41 64 64  stmt*);.  /* Add
28af1 65 64 20 62 79 20 33 2e 34 2e 31 20 2a 2f 0a 20  ed by 3.4.1 */. 
28af2 20 69 6e 74 20 28 2a 63 72 65 61 74 65 5f 6d 6f   int (*create_mo
28af3 64 75 6c 65 5f 76 32 29 28 73 71 6c 69 74 65 33  dule_v2)(sqlite3
28af4 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
28af5 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
28af6 6c 65 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28  le*,void*,void (
28af7 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20  *xDestroy)(void 
28af8 2a 29 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20  *));.  /* Added 
28af9 62 79 20 33 2e 35 2e 30 20 2a 2f 0a 20 20 69 6e  by 3.5.0 */.  in
28afa 74 20 28 2a 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f  t (*bind_zeroblo
28afb 62 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  b)(sqlite3_stmt*
28afc 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  ,int,int);.  int
28afd 20 28 2a 62 6c 6f 62 5f 62 79 74 65 73 29 28 73   (*blob_bytes)(s
28afe 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 29 3b 0a 20  qlite3_blob*);. 
28aff 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 63 6c 6f 73   int (*blob_clos
28b00 65 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a  e)(sqlite3_blob*
28b01 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f  );.  int (*blob_
28b02 6f 70 65 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63  open)(sqlite3*,c
28b03 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
28b04 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
28b05 72 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  r*,sqlite3_int64
28b06 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 62 6c 6f  ,int,sqlite3_blo
28b07 62 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c  b**);.  int (*bl
28b08 6f 62 5f 72 65 61 64 29 28 73 71 6c 69 74 65 33  ob_read)(sqlite3
28b09 5f 62 6c 6f 62 2a 2c 76 6f 69 64 2a 2c 69 6e 74  _blob*,void*,int
28b0a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 62  ,int);.  int (*b
28b0b 6c 6f 62 5f 77 72 69 74 65 29 28 73 71 6c 69 74  lob_write)(sqlit
28b0c 65 33 5f 62 6c 6f 62 2a 2c 63 6f 6e 73 74 20 76  e3_blob*,const v
28b0d 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20  oid*,int,int);. 
28b0e 20 69 6e 74 20 28 2a 63 72 65 61 74 65 5f 63 6f   int (*create_co
28b0f 6c 6c 61 74 69 6f 6e 5f 76 32 29 28 73 71 6c 69  llation_v2)(sqli
28b10 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
28b11 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a  ,int,void*,int(*
28b12 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
28b13 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
28b14 74 20 76 6f 69 64 2a 29 2c 76 6f 69 64 28 2a 29  t void*),void(*)
28b15 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20  (void*));.  int 
28b16 28 2a 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 29 28  (*file_control)(
28b17 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
28b18 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b  har*,int,void*);
28b19 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
28b1a 20 28 2a 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61   (*memory_highwa
28b1b 74 65 72 29 28 69 6e 74 29 3b 0a 20 20 73 71 6c  ter)(int);.  sql
28b1c 69 74 65 33 5f 69 6e 74 36 34 20 28 2a 6d 65 6d  ite3_int64 (*mem
28b1d 6f 72 79 5f 75 73 65 64 29 28 76 6f 69 64 29 3b  ory_used)(void);
28b1e 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
28b1f 20 2a 28 2a 6d 75 74 65 78 5f 61 6c 6c 6f 63 29   *(*mutex_alloc)
28b20 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a  (int);.  void (*
28b21 6d 75 74 65 78 5f 65 6e 74 65 72 29 28 73 71 6c  mutex_enter)(sql
28b22 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20  ite3_mutex*);.  
28b23 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f 66 72 65  void (*mutex_fre
28b24 65 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  e)(sqlite3_mutex
28b25 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74  *);.  void (*mut
28b26 65 78 5f 6c 65 61 76 65 29 28 73 71 6c 69 74 65  ex_leave)(sqlite
28b27 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 69 6e 74  3_mutex*);.  int
28b28 20 28 2a 6d 75 74 65 78 5f 74 72 79 29 28 73 71   (*mutex_try)(sq
28b29 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20  lite3_mutex*);. 
28b2a 20 69 6e 74 20 28 2a 6f 70 65 6e 5f 76 32 29 28   int (*open_v2)(
28b2b 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71 6c 69  const char*,sqli
28b2c 74 65 33 2a 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  te3**,int,const 
28b2d 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  char*);.  int (*
28b2e 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 29 28  release_memory)(
28b2f 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72  int);.  void (*r
28b30 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
28b31 6d 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  m)(sqlite3_conte
28b32 78 74 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72  xt*);.  void (*r
28b33 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
28b34 69 67 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ig)(sqlite3_cont
28b35 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 73  ext*);.  int (*s
28b36 6c 65 65 70 29 28 69 6e 74 29 3b 0a 20 20 76 6f  leep)(int);.  vo
28b37 69 64 20 28 2a 73 6f 66 74 5f 68 65 61 70 5f 6c  id (*soft_heap_l
28b38 69 6d 69 74 29 28 69 6e 74 29 3b 0a 20 20 73 71  imit)(int);.  sq
28b39 6c 69 74 65 33 5f 76 66 73 20 2a 28 2a 76 66 73  lite3_vfs *(*vfs
28b3a 5f 66 69 6e 64 29 28 63 6f 6e 73 74 20 63 68 61  _find)(const cha
28b3b 72 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 76 66 73  r*);.  int (*vfs
28b3c 5f 72 65 67 69 73 74 65 72 29 28 73 71 6c 69 74  _register)(sqlit
28b3d 65 33 5f 76 66 73 2a 2c 69 6e 74 29 3b 0a 20 20  e3_vfs*,int);.  
28b3e 69 6e 74 20 28 2a 76 66 73 5f 75 6e 72 65 67 69  int (*vfs_unregi
28b3f 73 74 65 72 29 28 73 71 6c 69 74 65 33 5f 76 66  ster)(sqlite3_vf
28b40 73 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 74 68  s*);.  int (*xth
28b41 72 65 61 64 73 61 66 65 29 28 76 6f 69 64 29 3b  readsafe)(void);
28b42 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c 74  .  void (*result
28b43 5f 7a 65 72 6f 62 6c 6f 62 29 28 73 71 6c 69 74  _zeroblob)(sqlit
28b44 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29  e3_context*,int)
28b45 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c  ;.  void (*resul
28b46 74 5f 65 72 72 6f 72 5f 63 6f 64 65 29 28 73 71  t_error_code)(sq
28b47 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
28b48 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 74 65 73  nt);.  int (*tes
28b49 74 5f 63 6f 6e 74 72 6f 6c 29 28 69 6e 74 2c 20  t_control)(int, 
28b4a 2e 2e 2e 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72  ...);.  void (*r
28b4b 61 6e 64 6f 6d 6e 65 73 73 29 28 69 6e 74 2c 76  andomness)(int,v
28b4c 6f 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33  oid*);.  sqlite3
28b4d 20 2a 28 2a 63 6f 6e 74 65 78 74 5f 64 62 5f 68   *(*context_db_h
28b4e 61 6e 64 6c 65 29 28 73 71 6c 69 74 65 33 5f 63  andle)(sqlite3_c
28b4f 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74 20  ontext*);.  int 
28b50 28 2a 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c  (*extended_resul
28b51 74 5f 63 6f 64 65 73 29 28 73 71 6c 69 74 65 33  t_codes)(sqlite3
28b52 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a  *,int);.  int (*
28b53 6c 69 6d 69 74 29 28 73 71 6c 69 74 65 33 2a 2c  limit)(sqlite3*,
28b54 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 73 71 6c 69  int,int);.  sqli
28b55 74 65 33 5f 73 74 6d 74 20 2a 28 2a 6e 65 78 74  te3_stmt *(*next
28b56 5f 73 74 6d 74 29 28 73 71 6c 69 74 65 33 2a 2c  _stmt)(sqlite3*,
28b57 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
28b58 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 28 2a    const char *(*
28b59 73 71 6c 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  sql)(sqlite3_stm
28b5a 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 73 74 61  t*);.  int (*sta
28b5b 74 75 73 29 28 69 6e 74 2c 69 6e 74 2a 2c 69 6e  tus)(int,int*,in
28b5c 74 2a 2c 69 6e 74 29 3b 0a 7d 3b 0a 0a 2f 2a 0a  t*,int);.};../*.
28b5d 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
28b5e 20 6d 61 63 72 6f 73 20 72 65 64 65 66 69 6e 65   macros redefine
28b5f 20 74 68 65 20 41 50 49 20 72 6f 75 74 69 6e 65   the API routine
28b60 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 61  s so that they a
28b61 72 65 0a 2a 2a 20 72 65 64 69 72 65 63 74 65 64  re.** redirected
28b62 20 74 68 72 6f 75 67 68 74 20 74 68 65 20 67 6c   throught the gl
28b63 6f 62 61 6c 20 73 71 6c 69 74 65 33 5f 61 70 69  obal sqlite3_api
28b64 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a   structure..**.*
28b65 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69  * This header fi
28b66 6c 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20  le is also used 
28b67 62 79 20 74 68 65 20 6c 6f 61 64 65 78 74 2e 63  by the loadext.c
28b68 20 73 6f 75 72 63 65 20 66 69 6c 65 0a 2a 2a 20   source file.** 
28b69 28 70 61 72 74 20 6f 66 20 74 68 65 20 6d 61 69  (part of the mai
28b6a 6e 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  n SQLite library
28b6b 20 2d 20 6e 6f 74 20 61 6e 20 65 78 74 65 6e 73   - not an extens
28b6c 69 6f 6e 29 20 73 6f 20 74 68 61 74 0a 2a 2a 20  ion) so that.** 
28b6d 69 74 20 63 61 6e 20 67 65 74 20 61 63 63 65 73  it can get acces
28b6e 73 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  s to the sqlite3
28b6f 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73 74  _api_routines st
28b70 72 75 63 74 75 72 65 0a 2a 2a 20 64 65 66 69 6e  ructure.** defin
28b71 69 74 69 6f 6e 2e 20 20 42 75 74 20 74 68 65 20  ition.  But the 
28b72 6d 61 69 6e 20 6c 69 62 72 61 72 79 20 64 6f 65  main library doe
28b73 73 20 6e 6f 74 20 77 61 6e 74 20 74 6f 20 72 65  s not want to re
28b74 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 41 50  define.** the AP
28b75 49 2e 20 20 53 6f 20 74 68 65 20 72 65 64 65 66  I.  So the redef
28b76 69 6e 69 74 69 6f 6e 20 6d 61 63 72 6f 73 20 61  inition macros a
28b77 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64 20 69 66  re only valid if
28b78 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 43   the.** SQLITE_C
28b79 4f 52 45 20 6d 61 63 72 6f 73 20 69 73 20 75 6e  ORE macros is un
28b7a 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e  defined..*/.#ifn
28b7b 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a  def SQLITE_CORE.
28b7c 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28b7d 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
28b7e 74 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  t      sqlite3_a
28b7f 70 69 2d 3e 61 67 67 72 65 67 61 74 65 5f 63 6f  pi->aggregate_co
28b80 6e 74 65 78 74 0a 23 69 66 6e 64 65 66 20 53 51  ntext.#ifndef SQ
28b81 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
28b82 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71 6c  ATED.#define sql
28b83 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
28b84 6f 75 6e 74 20 20 20 20 20 20 20 20 73 71 6c 69  ount        sqli
28b85 74 65 33 5f 61 70 69 2d 3e 61 67 67 72 65 67 61  te3_api->aggrega
28b86 74 65 5f 63 6f 75 6e 74 0a 23 65 6e 64 69 66 0a  te_count.#endif.
28b87 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28b88 62 69 6e 64 5f 62 6c 6f 62 20 20 20 20 20 20 20  bind_blob       
28b89 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28b8a 70 69 2d 3e 62 69 6e 64 5f 62 6c 6f 62 0a 23 64  pi->bind_blob.#d
28b8b 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
28b8c 6e 64 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20  nd_double       
28b8d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28b8e 2d 3e 62 69 6e 64 5f 64 6f 75 62 6c 65 0a 23 64  ->bind_double.#d
28b8f 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
28b90 6e 64 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20  nd_int          
28b91 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28b92 2d 3e 62 69 6e 64 5f 69 6e 74 0a 23 64 65 66 69  ->bind_int.#defi
28b93 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
28b94 69 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 20  int64           
28b95 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
28b96 69 6e 64 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e  ind_int64.#defin
28b97 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e  e sqlite3_bind_n
28b98 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  ull             
28b99 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
28b9a 6e 64 5f 6e 75 6c 6c 0a 23 64 65 66 69 6e 65 20  nd_null.#define 
28b9b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
28b9c 61 6d 65 74 65 72 5f 63 6f 75 6e 74 20 20 20 73  ameter_count   s
28b9d 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64  qlite3_api->bind
28b9e 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
28b9f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28ba0 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
28ba1 69 6e 64 65 78 20 20 20 73 71 6c 69 74 65 33 5f  index   sqlite3_
28ba2 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72 61 6d 65  api->bind_parame
28ba3 74 65 72 5f 69 6e 64 65 78 0a 23 64 65 66 69 6e  ter_index.#defin
28ba4 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70  e sqlite3_bind_p
28ba5 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 20 20 20  arameter_name   
28ba6 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
28ba7 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
28ba8 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28ba9 33 5f 62 69 6e 64 5f 74 65 78 74 20 20 20 20 20  3_bind_text     
28baa 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28bab 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65 78 74 0a  _api->bind_text.
28bac 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28bad 62 69 6e 64 5f 74 65 78 74 31 36 20 20 20 20 20  bind_text16     
28bae 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28baf 70 69 2d 3e 62 69 6e 64 5f 74 65 78 74 31 36 0a  pi->bind_text16.
28bb0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28bb1 62 69 6e 64 5f 76 61 6c 75 65 20 20 20 20 20 20  bind_value      
28bb2 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28bb3 70 69 2d 3e 62 69 6e 64 5f 76 61 6c 75 65 0a 23  pi->bind_value.#
28bb4 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
28bb5 75 73 79 5f 68 61 6e 64 6c 65 72 20 20 20 20 20  usy_handler     
28bb6 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28bb7 69 2d 3e 62 75 73 79 5f 68 61 6e 64 6c 65 72 0a  i->busy_handler.
28bb8 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28bb9 62 75 73 79 5f 74 69 6d 65 6f 75 74 20 20 20 20  busy_timeout    
28bba 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28bbb 70 69 2d 3e 62 75 73 79 5f 74 69 6d 65 6f 75 74  pi->busy_timeout
28bbc 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28bbd 5f 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 20  _changes        
28bbe 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28bbf 61 70 69 2d 3e 63 68 61 6e 67 65 73 0a 23 64 65  api->changes.#de
28bc0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6c 6f  fine sqlite3_clo
28bc1 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  se              
28bc2 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28bc3 3e 63 6c 6f 73 65 0a 23 64 65 66 69 6e 65 20 73  >close.#define s
28bc4 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
28bc5 5f 6e 65 65 64 65 64 20 20 20 20 20 20 20 73 71  _needed       sq
28bc6 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 6c 61  lite3_api->colla
28bc7 74 69 6f 6e 5f 6e 65 65 64 65 64 0a 23 64 65 66  tion_needed.#def
28bc8 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  ine sqlite3_coll
28bc9 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 20 20  ation_needed16  
28bca 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28bcb 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
28bcc 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
28bcd 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 20 20  e3_column_blob  
28bce 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28bcf 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 6c  3_api->column_bl
28bd0 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ob.#define sqlit
28bd1 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 20  e3_column_bytes 
28bd2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28bd3 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79  3_api->column_by
28bd4 74 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  tes.#define sqli
28bd5 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
28bd6 31 36 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  16         sqlit
28bd7 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62  e3_api->column_b
28bd8 79 74 65 73 31 36 0a 23 64 65 66 69 6e 65 20 73  ytes16.#define s
28bd9 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f  qlite3_column_co
28bda 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 73 71  unt           sq
28bdb 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
28bdc 6e 5f 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20  n_count.#define 
28bdd 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
28bde 61 74 61 62 61 73 65 5f 6e 61 6d 65 20 20 20 73  atabase_name   s
28bdf 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
28be0 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
28be1 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28be2 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
28be3 5f 6e 61 6d 65 31 36 20 73 71 6c 69 74 65 33 5f  _name16 sqlite3_
28be4 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 61 74 61  api->column_data
28be5 62 61 73 65 5f 6e 61 6d 65 31 36 0a 23 64 65 66  base_name16.#def
28be6 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28be7 6d 6e 5f 64 65 63 6c 74 79 70 65 20 20 20 20 20  mn_decltype     
28be8 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28be9 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 0a  column_decltype.
28bea 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28beb 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31  column_decltype1
28bec 36 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  6      sqlite3_a
28bed 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  pi->column_declt
28bee 79 70 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ype16.#define sq
28bef 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75  lite3_column_dou
28bf0 62 6c 65 20 20 20 20 20 20 20 20 20 20 73 71 6c  ble          sql
28bf1 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
28bf2 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e 65 20  _double.#define 
28bf3 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
28bf4 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73  nt             s
28bf5 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
28bf6 6d 6e 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73  mn_int.#define s
28bf7 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
28bf8 74 36 34 20 20 20 20 20 20 20 20 20 20 20 73 71  t64           sq
28bf9 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
28bfa 6e 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20  n_int64.#define 
28bfb 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
28bfc 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 73  ame            s
28bfd 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
28bfe 6d 6e 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20  mn_name.#define 
28bff 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
28c00 61 6d 65 31 36 20 20 20 20 20 20 20 20 20 20 73  ame16          s
28c01 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
28c02 6d 6e 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e  mn_name16.#defin
28c03 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28c04 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20 20 20 20  _origin_name    
28c05 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
28c06 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
28c07 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c08 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
28c09 61 6d 65 31 36 20 20 20 73 71 6c 69 74 65 33 5f  ame16   sqlite3_
28c0a 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67  api->column_orig
28c0b 69 6e 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e  in_name16.#defin
28c0c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28c0d 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 20 20 20 20  _table_name     
28c0e 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
28c0f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 0a  lumn_table_name.
28c10 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28c11 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
28c12 65 31 36 20 20 20 20 73 71 6c 69 74 65 33 5f 61  e16    sqlite3_a
28c13 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  pi->column_table
28c14 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20  _name16.#define 
28c15 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
28c16 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 73  ext            s
28c17 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
28c18 6d 6e 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20  mn_text.#define 
28c19 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
28c1a 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20 73  ext16          s
28c1b 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
28c1c 6d 6e 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e  mn_text16.#defin
28c1d 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28c1e 5f 74 79 70 65 20 20 20 20 20 20 20 20 20 20 20  _type           
28c1f 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
28c20 6c 75 6d 6e 5f 74 79 70 65 0a 23 64 65 66 69 6e  lumn_type.#defin
28c21 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28c22 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20  _value          
28c23 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
28c24 6c 75 6d 6e 5f 76 61 6c 75 65 0a 23 64 65 66 69  lumn_value.#defi
28c25 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69  ne sqlite3_commi
28c26 74 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20 20 20  t_hook          
28c27 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28c28 6f 6d 6d 69 74 5f 68 6f 6f 6b 0a 23 64 65 66 69  ommit_hook.#defi
28c29 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  ne sqlite3_compl
28c2a 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ete             
28c2b 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28c2c 6f 6d 70 6c 65 74 65 0a 23 64 65 66 69 6e 65 20  omplete.#define 
28c2d 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
28c2e 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20 73  16             s
28c2f 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 70  qlite3_api->comp
28c30 6c 65 74 65 31 36 0a 23 64 65 66 69 6e 65 20 73  lete16.#define s
28c31 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
28c32 6c 6c 61 74 69 6f 6e 20 20 20 20 20 20 20 73 71  llation       sq
28c33 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74  lite3_api->creat
28c34 65 5f 63 6f 6c 6c 61 74 69 6f 6e 0a 23 64 65 66  e_collation.#def
28c35 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ine sqlite3_crea
28c36 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 20 20  te_collation16  
28c37 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28c38 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
28c39 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
28c3a 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
28c3b 6f 6e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  on        sqlite
28c3c 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 66 75  3_api->create_fu
28c3d 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20 73  nction.#define s
28c3e 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
28c3f 6e 63 74 69 6f 6e 31 36 20 20 20 20 20 20 73 71  nction16      sq
28c40 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74  lite3_api->creat
28c41 65 5f 66 75 6e 63 74 69 6f 6e 31 36 0a 23 64 65  e_function16.#de
28c42 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65  fine sqlite3_cre
28c43 61 74 65 5f 6d 6f 64 75 6c 65 20 20 20 20 20 20  ate_module      
28c44 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28c45 3e 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 0a 23  >create_module.#
28c46 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
28c47 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 20  reate_module_v2 
28c48 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28c49 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  i->create_module
28c4a 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  _v2.#define sqli
28c4b 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 20 20  te3_data_count  
28c4c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28c4d 65 33 5f 61 70 69 2d 3e 64 61 74 61 5f 63 6f 75  e3_api->data_cou
28c4e 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  nt.#define sqlit
28c4f 65 33 5f 64 62 5f 68 61 6e 64 6c 65 20 20 20 20  e3_db_handle    
28c50 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28c51 33 5f 61 70 69 2d 3e 64 62 5f 68 61 6e 64 6c 65  3_api->db_handle
28c52 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c53 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 20 20 20  _declare_vtab   
28c54 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28c55 61 70 69 2d 3e 64 65 63 6c 61 72 65 5f 76 74 61  api->declare_vta
28c56 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  b.#define sqlite
28c57 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  3_enable_shared_
28c58 63 61 63 68 65 20 20 20 20 73 71 6c 69 74 65 33  cache    sqlite3
28c59 5f 61 70 69 2d 3e 65 6e 61 62 6c 65 5f 73 68 61  _api->enable_sha
28c5a 72 65 64 5f 63 61 63 68 65 0a 23 64 65 66 69 6e  red_cache.#defin
28c5b 65 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  e sqlite3_errcod
28c5c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
28c5d 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72   sqlite3_api->er
28c5e 72 63 6f 64 65 0a 23 64 65 66 69 6e 65 20 73 71  rcode.#define sq
28c5f 6c 69 74 65 33 5f 65 72 72 6d 73 67 20 20 20 20  lite3_errmsg    
28c60 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28c61 69 74 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67  ite3_api->errmsg
28c62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c63 5f 65 72 72 6d 73 67 31 36 20 20 20 20 20 20 20  _errmsg16       
28c64 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28c65 61 70 69 2d 3e 65 72 72 6d 73 67 31 36 0a 23 64  api->errmsg16.#d
28c66 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 78  efine sqlite3_ex
28c67 65 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ec              
28c68 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28c69 2d 3e 65 78 65 63 0a 23 69 66 6e 64 65 66 20 53  ->exec.#ifndef S
28c6a 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
28c6b 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71  CATED.#define sq
28c6c 6c 69 74 65 33 5f 65 78 70 69 72 65 64 20 20 20  lite3_expired   
28c6d 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28c6e 69 74 65 33 5f 61 70 69 2d 3e 65 78 70 69 72 65  ite3_api->expire
28c6f 64 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65  d.#endif.#define
28c70 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
28c71 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
28c72 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 69 6e  sqlite3_api->fin
28c73 61 6c 69 7a 65 0a 23 64 65 66 69 6e 65 20 73 71  alize.#define sq
28c74 6c 69 74 65 33 5f 66 72 65 65 20 20 20 20 20 20  lite3_free      
28c75 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28c76 69 74 65 33 5f 61 70 69 2d 3e 66 72 65 65 0a 23  ite3_api->free.#
28c77 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66  define sqlite3_f
28c78 72 65 65 5f 74 61 62 6c 65 20 20 20 20 20 20 20  ree_table       
28c79 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28c7a 69 2d 3e 66 72 65 65 5f 74 61 62 6c 65 0a 23 64  i->free_table.#d
28c7b 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65  efine sqlite3_ge
28c7c 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 20 20 20 20  t_autocommit    
28c7d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28c7e 2d 3e 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  ->get_autocommit
28c7f 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c80 5f 67 65 74 5f 61 75 78 64 61 74 61 20 20 20 20  _get_auxdata    
28c81 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28c82 61 70 69 2d 3e 67 65 74 5f 61 75 78 64 61 74 61  api->get_auxdata
28c83 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28c84 5f 67 65 74 5f 74 61 62 6c 65 20 20 20 20 20 20  _get_table      
28c85 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28c86 61 70 69 2d 3e 67 65 74 5f 74 61 62 6c 65 0a 23  api->get_table.#
28c87 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
28c88 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 23 64  IT_DEPRECATED.#d
28c89 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 6c  efine sqlite3_gl
28c8a 6f 62 61 6c 5f 72 65 63 6f 76 65 72 20 20 20 20  obal_recover    
28c8b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28c8c 2d 3e 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72  ->global_recover
28c8d 0a 23 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20  .#endif.#define 
28c8e 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
28c8f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  t              s
28c90 71 6c 69 74 65 33 5f 61 70 69 2d 3e 69 6e 74 65  qlite3_api->inte
28c91 72 72 75 70 74 78 0a 23 64 65 66 69 6e 65 20 73  rruptx.#define s
28c92 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
28c93 72 74 5f 72 6f 77 69 64 20 20 20 20 20 20 73 71  rt_rowid      sq
28c94 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 61 73 74 5f  lite3_api->last_
28c95 69 6e 73 65 72 74 5f 72 6f 77 69 64 0a 23 64 65  insert_rowid.#de
28c96 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69 62  fine sqlite3_lib
28c97 76 65 72 73 69 6f 6e 20 20 20 20 20 20 20 20 20  version         
28c98 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28c99 3e 6c 69 62 76 65 72 73 69 6f 6e 0a 23 64 65 66  >libversion.#def
28c9a 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76  ine sqlite3_libv
28c9b 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 20 20 20  ersion_number   
28c9c 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28c9d 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
28c9e 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
28c9f 33 5f 6d 61 6c 6c 6f 63 20 20 20 20 20 20 20 20  3_malloc        
28ca0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28ca1 5f 61 70 69 2d 3e 6d 61 6c 6c 6f 63 0a 23 64 65  _api->malloc.#de
28ca2 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 70 72  fine sqlite3_mpr
28ca3 69 6e 74 66 20 20 20 20 20 20 20 20 20 20 20 20  intf            
28ca4 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28ca5 3e 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65  >mprintf.#define
28ca6 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 20 20 20   sqlite3_open   
28ca7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28ca8 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65  sqlite3_api->ope
28ca9 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  n.#define sqlite
28caa 33 5f 6f 70 65 6e 31 36 20 20 20 20 20 20 20 20  3_open16        
28cab 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28cac 5f 61 70 69 2d 3e 6f 70 65 6e 31 36 0a 23 64 65  _api->open16.#de
28cad 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65  fine sqlite3_pre
28cae 70 61 72 65 20 20 20 20 20 20 20 20 20 20 20 20  pare            
28caf 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28cb0 3e 70 72 65 70 61 72 65 0a 23 64 65 66 69 6e 65  >prepare.#define
28cb1 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
28cb2 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20  16              
28cb3 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65  sqlite3_api->pre
28cb4 70 61 72 65 31 36 0a 23 64 65 66 69 6e 65 20 73  pare16.#define s
28cb5 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
28cb6 32 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  2             sq
28cb7 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61  lite3_api->prepa
28cb8 72 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71  re_v2.#define sq
28cb9 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
28cba 76 32 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  v2           sql
28cbb 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72  ite3_api->prepar
28cbc 65 31 36 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  e16_v2.#define s
28cbd 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 20 20  qlite3_profile  
28cbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
28cbf 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 66 69  lite3_api->profi
28cc0 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
28cc1 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64  e3_progress_hand
28cc2 6c 65 72 20 20 20 20 20 20 20 73 71 6c 69 74 65  ler       sqlite
28cc3 33 5f 61 70 69 2d 3e 70 72 6f 67 72 65 73 73 5f  3_api->progress_
28cc4 68 61 6e 64 6c 65 72 0a 23 64 65 66 69 6e 65 20  handler.#define 
28cc5 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 20  sqlite3_realloc 
28cc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
28cc7 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 61 6c  qlite3_api->real
28cc8 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  loc.#define sqli
28cc9 74 65 33 5f 72 65 73 65 74 20 20 20 20 20 20 20  te3_reset       
28cca 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28ccb 65 33 5f 61 70 69 2d 3e 72 65 73 65 74 0a 23 64  e3_api->reset.#d
28ccc 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
28ccd 73 75 6c 74 5f 62 6c 6f 62 20 20 20 20 20 20 20  sult_blob       
28cce 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28ccf 2d 3e 72 65 73 75 6c 74 5f 62 6c 6f 62 0a 23 64  ->result_blob.#d
28cd0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
28cd1 73 75 6c 74 5f 64 6f 75 62 6c 65 20 20 20 20 20  sult_double     
28cd2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28cd3 2d 3e 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 0a  ->result_double.
28cd4 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28cd5 72 65 73 75 6c 74 5f 65 72 72 6f 72 20 20 20 20  result_error    
28cd6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28cd7 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72  pi->result_error
28cd8 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28cd9 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 20  _result_error16 
28cda 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28cdb 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f  api->result_erro
28cdc 72 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  r16.#define sqli
28cdd 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20 20  te3_result_int  
28cde 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28cdf 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 69  e3_api->result_i
28ce0 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  nt.#define sqlit
28ce1 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 20  e3_result_int64 
28ce2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28ce3 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 69 6e  3_api->result_in
28ce4 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t64.#define sqli
28ce5 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 20  te3_result_null 
28ce6 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28ce7 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 6e  e3_api->result_n
28ce8 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ull.#define sqli
28ce9 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 20  te3_result_text 
28cea 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28ceb 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74  e3_api->result_t
28cec 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ext.#define sqli
28ced 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
28cee 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  6          sqlit
28cef 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74  e3_api->result_t
28cf0 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ext16.#define sq
28cf1 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
28cf2 74 31 36 62 65 20 20 20 20 20 20 20 20 73 71 6c  t16be        sql
28cf3 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
28cf4 5f 74 65 78 74 31 36 62 65 0a 23 64 65 66 69 6e  _text16be.#defin
28cf5 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
28cf6 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20  _text16le       
28cf7 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
28cf8 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 0a 23 64  sult_text16le.#d
28cf9 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
28cfa 73 75 6c 74 5f 76 61 6c 75 65 20 20 20 20 20 20  sult_value      
28cfb 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28cfc 2d 3e 72 65 73 75 6c 74 5f 76 61 6c 75 65 0a 23  ->result_value.#
28cfd 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
28cfe 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 20 20 20 20  ollback_hook    
28cff 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28d00 69 2d 3e 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  i->rollback_hook
28d01 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28d02 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 20  _set_authorizer 
28d03 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28d04 61 70 69 2d 3e 73 65 74 5f 61 75 74 68 6f 72 69  api->set_authori
28d05 7a 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  zer.#define sqli
28d06 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 20  te3_set_auxdata 
28d07 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28d08 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75 78 64  e3_api->set_auxd
28d09 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ata.#define sqli
28d0a 74 65 33 5f 73 6e 70 72 69 6e 74 66 20 20 20 20  te3_snprintf    
28d0b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28d0c 65 33 5f 61 70 69 2d 3e 73 6e 70 72 69 6e 74 66  e3_api->snprintf
28d0d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28d0e 5f 73 74 65 70 20 20 20 20 20 20 20 20 20 20 20  _step           
28d0f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28d10 61 70 69 2d 3e 73 74 65 70 0a 23 64 65 66 69 6e  api->step.#defin
28d11 65 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f  e sqlite3_table_
28d12 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 20  column_metadata 
28d13 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 61   sqlite3_api->ta
28d14 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64  ble_column_metad
28d15 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ata.#define sqli
28d16 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e  te3_thread_clean
28d17 75 70 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  up         sqlit
28d18 65 33 5f 61 70 69 2d 3e 74 68 72 65 61 64 5f 63  e3_api->thread_c
28d19 6c 65 61 6e 75 70 0a 23 64 65 66 69 6e 65 20 73  leanup.#define s
28d1a 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61  qlite3_total_cha
28d1b 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 73 71  nges          sq
28d1c 6c 69 74 65 33 5f 61 70 69 2d 3e 74 6f 74 61 6c  lite3_api->total
28d1d 5f 63 68 61 6e 67 65 73 0a 23 64 65 66 69 6e 65  _changes.#define
28d1e 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 20 20   sqlite3_trace  
28d1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28d20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 72 61  sqlite3_api->tra
28d21 63 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ce.#ifndef SQLIT
28d22 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
28d23 44 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  D.#define sqlite
28d24 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69  3_transfer_bindi
28d25 6e 67 73 20 20 20 20 20 20 73 71 6c 69 74 65 33  ngs      sqlite3
28d26 5f 61 70 69 2d 3e 74 72 61 6e 73 66 65 72 5f 62  _api->transfer_b
28d27 69 6e 64 69 6e 67 73 0a 23 65 6e 64 69 66 0a 23  indings.#endif.#
28d28 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 75  define sqlite3_u
28d29 70 64 61 74 65 5f 68 6f 6f 6b 20 20 20 20 20 20  pdate_hook      
28d2a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28d2b 69 2d 3e 75 70 64 61 74 65 5f 68 6f 6f 6b 0a 23  i->update_hook.#
28d2c 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 75  define sqlite3_u
28d2d 73 65 72 5f 64 61 74 61 20 20 20 20 20 20 20 20  ser_data        
28d2e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28d2f 69 2d 3e 75 73 65 72 5f 64 61 74 61 0a 23 64 65  i->user_data.#de
28d30 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
28d31 75 65 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20  ue_blob         
28d32 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28d33 3e 76 61 6c 75 65 5f 62 6c 6f 62 0a 23 64 65 66  >value_blob.#def
28d34 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ine sqlite3_valu
28d35 65 5f 62 79 74 65 73 20 20 20 20 20 20 20 20 20  e_bytes         
28d36 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28d37 76 61 6c 75 65 5f 62 79 74 65 73 0a 23 64 65 66  value_bytes.#def
28d38 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ine sqlite3_valu
28d39 65 5f 62 79 74 65 73 31 36 20 20 20 20 20 20 20  e_bytes16       
28d3a 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28d3b 76 61 6c 75 65 5f 62 79 74 65 73 31 36 0a 23 64  value_bytes16.#d
28d3c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
28d3d 6c 75 65 5f 64 6f 75 62 6c 65 20 20 20 20 20 20  lue_double      
28d3e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28d3f 2d 3e 76 61 6c 75 65 5f 64 6f 75 62 6c 65 0a 23  ->value_double.#
28d40 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
28d41 61 6c 75 65 5f 69 6e 74 20 20 20 20 20 20 20 20  alue_int        
28d42 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28d43 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 0a 23 64 65  i->value_int.#de
28d44 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
28d45 75 65 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20  ue_int64        
28d46 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28d47 3e 76 61 6c 75 65 5f 69 6e 74 36 34 0a 23 64 65  >value_int64.#de
28d48 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c  fine sqlite3_val
28d49 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 20  ue_numeric_type 
28d4a 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28d4b 3e 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  >value_numeric_t
28d4c 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ype.#define sqli
28d4d 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 20 20  te3_value_text  
28d4e 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28d4f 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65  e3_api->value_te
28d50 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  xt.#define sqlit
28d51 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 20  e3_value_text16 
28d52 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28d53 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78  3_api->value_tex
28d54 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t16.#define sqli
28d55 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
28d56 62 65 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  be         sqlit
28d57 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65  e3_api->value_te
28d58 78 74 31 36 62 65 0a 23 64 65 66 69 6e 65 20 73  xt16be.#define s
28d59 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
28d5a 74 31 36 6c 65 20 20 20 20 20 20 20 20 20 73 71  t16le         sq
28d5b 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
28d5c 5f 74 65 78 74 31 36 6c 65 0a 23 64 65 66 69 6e  _text16le.#defin
28d5d 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
28d5e 74 79 70 65 20 20 20 20 20 20 20 20 20 20 20 20  type            
28d5f 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
28d60 6c 75 65 5f 74 79 70 65 0a 23 64 65 66 69 6e 65  lue_type.#define
28d61 20 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74   sqlite3_vmprint
28d62 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f               
28d63 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 6d 70  sqlite3_api->vmp
28d64 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71  rintf.#define sq
28d65 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66  lite3_overload_f
28d66 75 6e 63 74 69 6f 6e 20 20 20 20 20 20 73 71 6c  unction      sql
28d67 69 74 65 33 5f 61 70 69 2d 3e 6f 76 65 72 6c 6f  ite3_api->overlo
28d68 61 64 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65 66  ad_function.#def
28d69 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
28d6a 61 72 65 5f 76 32 20 20 20 20 20 20 20 20 20 20  are_v2          
28d6b 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28d6c 70 72 65 70 61 72 65 5f 76 32 0a 23 64 65 66 69  prepare_v2.#defi
28d6d 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ne sqlite3_prepa
28d6e 72 65 31 36 5f 76 32 20 20 20 20 20 20 20 20 20  re16_v2         
28d6f 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70    sqlite3_api->p
28d70 72 65 70 61 72 65 31 36 5f 76 32 0a 23 64 65 66  repare16_v2.#def
28d71 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6c 65 61  ine sqlite3_clea
28d72 72 5f 62 69 6e 64 69 6e 67 73 20 20 20 20 20 20  r_bindings      
28d73 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28d74 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 0a 23  clear_bindings.#
28d75 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
28d76 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20 20 20 20  ind_zeroblob    
28d77 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28d78 69 2d 3e 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  i->bind_zeroblob
28d79 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28d7a 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20 20 20 20  _blob_bytes     
28d7b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28d7c 61 70 69 2d 3e 62 6c 6f 62 5f 62 79 74 65 73 0a  api->blob_bytes.
28d7d 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28d7e 62 6c 6f 62 5f 63 6c 6f 73 65 20 20 20 20 20 20  blob_close      
28d7f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28d80 70 69 2d 3e 62 6c 6f 62 5f 63 6c 6f 73 65 0a 23  pi->blob_close.#
28d81 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
28d82 6c 6f 62 5f 6f 70 65 6e 20 20 20 20 20 20 20 20  lob_open        
28d83 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28d84 69 2d 3e 62 6c 6f 62 5f 6f 70 65 6e 0a 23 64 65  i->blob_open.#de
28d85 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
28d86 62 5f 72 65 61 64 20 20 20 20 20 20 20 20 20 20  b_read          
28d87 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28d88 3e 62 6c 6f 62 5f 72 65 61 64 0a 23 64 65 66 69  >blob_read.#defi
28d89 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
28d8a 77 72 69 74 65 20 20 20 20 20 20 20 20 20 20 20  write           
28d8b 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
28d8c 6c 6f 62 5f 77 72 69 74 65 0a 23 64 65 66 69 6e  lob_write.#defin
28d8d 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
28d8e 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 20 20 20  _collation_v2   
28d8f 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72   sqlite3_api->cr
28d90 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
28d91 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  2.#define sqlite
28d92 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 20 20  3_file_control  
28d93 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28d94 5f 61 70 69 2d 3e 66 69 6c 65 5f 63 6f 6e 74 72  _api->file_contr
28d95 6f 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ol.#define sqlit
28d96 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  e3_memory_highwa
28d97 74 65 72 20 20 20 20 20 20 20 73 71 6c 69 74 65  ter       sqlite
28d98 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79 5f 68 69  3_api->memory_hi
28d99 67 68 77 61 74 65 72 0a 23 64 65 66 69 6e 65 20  ghwater.#define 
28d9a 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75  sqlite3_memory_u
28d9b 73 65 64 20 20 20 20 20 20 20 20 20 20 20 20 73  sed            s
28d9c 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f  qlite3_api->memo
28d9d 72 79 5f 75 73 65 64 0a 23 64 65 66 69 6e 65 20  ry_used.#define 
28d9e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
28d9f 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20 73  loc            s
28da0 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65  qlite3_api->mute
28da1 78 5f 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20  x_alloc.#define 
28da2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
28da3 74 65 72 20 20 20 20 20 20 20 20 20 20 20 20 73  ter            s
28da4 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65  qlite3_api->mute
28da5 78 5f 65 6e 74 65 72 0a 23 64 65 66 69 6e 65 20  x_enter.#define 
28da6 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72  sqlite3_mutex_fr
28da7 65 65 20 20 20 20 20 20 20 20 20 20 20 20 20 73  ee             s
28da8 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65  qlite3_api->mute
28da9 78 5f 66 72 65 65 0a 23 64 65 66 69 6e 65 20 73  x_free.#define s
28daa 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
28dab 76 65 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ve            sq
28dac 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78  lite3_api->mutex
28dad 5f 6c 65 61 76 65 0a 23 64 65 66 69 6e 65 20 73  _leave.#define s
28dae 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
28daf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
28db0 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78  lite3_api->mutex
28db1 5f 74 72 79 0a 23 64 65 66 69 6e 65 20 73 71 6c  _try.#define sql
28db2 69 74 65 33 5f 6f 70 65 6e 5f 76 32 20 20 20 20  ite3_open_v2    
28db3 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28db4 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 5f 76 32  te3_api->open_v2
28db5 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28db6 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 20  _release_memory 
28db7 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28db8 61 70 69 2d 3e 72 65 6c 65 61 73 65 5f 6d 65 6d  api->release_mem
28db9 6f 72 79 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ory.#define sqli
28dba 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
28dbb 5f 6e 6f 6d 65 6d 20 20 20 20 20 73 71 6c 69 74  _nomem     sqlit
28dbc 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65  e3_api->result_e
28dbd 72 72 6f 72 5f 6e 6f 6d 65 6d 0a 23 64 65 66 69  rror_nomem.#defi
28dbe 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ne sqlite3_resul
28dbf 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 20 20  t_error_toobig  
28dc0 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
28dc1 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
28dc2 69 67 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ig.#define sqlit
28dc3 65 33 5f 73 6c 65 65 70 20 20 20 20 20 20 20 20  e3_sleep        
28dc4 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28dc5 33 5f 61 70 69 2d 3e 73 6c 65 65 70 0a 23 64 65  3_api->sleep.#de
28dc6 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 6f 66  fine sqlite3_sof
28dc7 74 5f 68 65 61 70 5f 6c 69 6d 69 74 20 20 20 20  t_heap_limit    
28dc8 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28dc9 3e 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  >soft_heap_limit
28dca 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28dcb 5f 76 66 73 5f 66 69 6e 64 20 20 20 20 20 20 20  _vfs_find       
28dcc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28dcd 61 70 69 2d 3e 76 66 73 5f 66 69 6e 64 0a 23 64  api->vfs_find.#d
28dce 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 66  efine sqlite3_vf
28dcf 73 5f 72 65 67 69 73 74 65 72 20 20 20 20 20 20  s_register      
28dd0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28dd1 2d 3e 76 66 73 5f 72 65 67 69 73 74 65 72 0a 23  ->vfs_register.#
28dd2 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
28dd3 66 73 5f 75 6e 72 65 67 69 73 74 65 72 20 20 20  fs_unregister   
28dd4 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28dd5 69 2d 3e 76 66 73 5f 75 6e 72 65 67 69 73 74 65  i->vfs_unregiste
28dd6 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  r.#define sqlite
28dd7 33 5f 74 68 72 65 61 64 73 61 66 65 20 20 20 20  3_threadsafe    
28dd8 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28dd9 5f 61 70 69 2d 3e 78 74 68 72 65 61 64 73 61 66  _api->xthreadsaf
28dda 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28ddb 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f  3_result_zeroblo
28ddc 62 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  b        sqlite3
28ddd 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 7a 65 72  _api->result_zer
28dde 6f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71  oblob.#define sq
28ddf 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
28de0 6f 72 5f 63 6f 64 65 20 20 20 20 20 20 73 71 6c  or_code      sql
28de1 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
28de2 5f 65 72 72 6f 72 5f 63 6f 64 65 0a 23 64 65 66  _error_code.#def
28de3 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74  ine sqlite3_test
28de4 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20 20 20 20  _control        
28de5 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28de6 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 0a 23 64 65  test_control.#de
28de7 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 61 6e  fine sqlite3_ran
28de8 64 6f 6d 6e 65 73 73 20 20 20 20 20 20 20 20 20  domness         
28de9 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28dea 3e 72 61 6e 64 6f 6d 6e 65 73 73 0a 23 64 65 66  >randomness.#def
28deb 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ine sqlite3_cont
28dec 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 20 20 20  ext_db_handle   
28ded 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28dee 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
28def 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28df0 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c  3_extended_resul
28df1 74 5f 63 6f 64 65 73 20 20 73 71 6c 69 74 65 33  t_codes  sqlite3
28df2 5f 61 70 69 2d 3e 65 78 74 65 6e 64 65 64 5f 72  _api->extended_r
28df3 65 73 75 6c 74 5f 63 6f 64 65 73 0a 23 64 65 66  esult_codes.#def
28df4 69 6e 65 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69  ine sqlite3_limi
28df5 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
28df6 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28df7 6c 69 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71  limit.#define sq
28df8 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 20  lite3_next_stmt 
28df9 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28dfa 69 74 65 33 5f 61 70 69 2d 3e 6e 65 78 74 5f 73  ite3_api->next_s
28dfb 74 6d 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  tmt.#define sqli
28dfc 74 65 33 5f 73 71 6c 20 20 20 20 20 20 20 20 20  te3_sql         
28dfd 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28dfe 65 33 5f 61 70 69 2d 3e 73 71 6c 0a 23 64 65 66  e3_api->sql.#def
28dff 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74  ine sqlite3_stat
28e00 75 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  us              
28e01 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28e02 73 74 61 74 75 73 0a 23 65 6e 64 69 66 20 2f 2a  status.#endif /*
28e03 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 2a 2f 0a   SQLITE_CORE */.
28e04 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
28e05 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 20  EXTENSION_INIT1 
28e06 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65      const sqlite
28e07 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a  3_api_routines *
28e08 73 71 6c 69 74 65 33 5f 61 70 69 20 3d 20 30 3b  sqlite3_api = 0;
28e09 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
28e0a 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28  EXTENSION_INIT2(
28e0b 76 29 20 20 73 71 6c 69 74 65 33 5f 61 70 69 20  v)  sqlite3_api 
28e0c 3d 20 76 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  = v;..#endif /* 
28e0d 5f 53 51 4c 49 54 45 33 45 58 54 5f 48 5f 20 2a  _SQLITE3EXT_H_ *
28e0e 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
28e0f 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65  ** End of sqlite
28e10 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  3ext.h *********
28e11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28e12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28e13 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
28e14 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
28e15 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
28e16 69 6e 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a  in loadext.c ***
28e17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28e18 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
28e19 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
28e1a 45 4e 53 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 6f  ENSION../*.** So
28e1b 6d 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20  me API routines 
28e1c 61 72 65 20 6f 6d 69 74 74 65 64 20 77 68 65 6e  are omitted when
28e1d 20 76 61 72 69 6f 75 73 20 66 65 61 74 75 72 65   various feature
28e1e 73 20 61 72 65 0a 2a 2a 20 65 78 63 6c 75 64 65  s are.** exclude
28e1f 64 20 66 72 6f 6d 20 61 20 62 75 69 6c 64 20 6f  d from a build o
28e20 66 20 53 51 4c 69 74 65 2e 20 20 53 75 62 73 74  f SQLite.  Subst
28e21 69 74 75 74 65 20 61 20 4e 55 4c 4c 20 70 6f 69  itute a NULL poi
28e22 6e 74 65 72 0a 2a 2a 20 66 6f 72 20 61 6e 79 20  nter.** for any 
28e23 6d 69 73 73 69 6e 67 20 41 50 49 73 2e 0a 2a 2f  missing APIs..*/
28e24 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
28e25 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
28e26 54 41 44 41 54 41 0a 23 20 64 65 66 69 6e 65 20  TADATA.# define 
28e27 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
28e28 61 74 61 62 61 73 65 5f 6e 61 6d 65 20 20 20 30  atabase_name   0
28e29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28e2a 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  3_column_databas
28e2b 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20 64 65 66  e_name16 0.# def
28e2c 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28e2d 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20 20 20  mn_table_name   
28e2e 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28e2f 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
28e30 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20 30 0a 23  le_name16    0.#
28e31 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28e32 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
28e33 6d 65 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e  me     0.# defin
28e34 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28e35 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 20 20  _origin_name16  
28e36 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
28e37 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e  te3_table_column
28e38 5f 6d 65 74 61 64 61 74 61 20 20 30 0a 23 65 6e  _metadata  0.#en
28e39 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
28e3a 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
28e3b 41 54 49 4f 4e 0a 23 20 64 65 66 69 6e 65 20 73  ATION.# define s
28e3c 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
28e3d 72 69 7a 65 72 20 20 20 20 20 20 20 20 20 30 0a  rizer         0.
28e3e 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
28e3f 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
28e40 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28e41 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 20 20 20  3_bind_text16   
28e42 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
28e43 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  ine sqlite3_coll
28e44 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 20 20  ation_needed16  
28e45 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28e46 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63  lite3_column_dec
28e47 6c 74 79 70 65 31 36 20 20 20 20 20 20 30 0a 23  ltype16      0.#
28e48 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28e49 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 20 20 20  column_name16   
28e4a 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
28e4b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28e4c 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20  _text16         
28e4d 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
28e4e 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 20  te3_complete16  
28e4f 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64             0.# d
28e50 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
28e51 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36  eate_collation16
28e52 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
28e53 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
28e54 75 6e 63 74 69 6f 6e 31 36 20 20 20 20 20 20 30  unction16      0
28e55 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28e56 33 5f 65 72 72 6d 73 67 31 36 20 20 20 20 20 20  3_errmsg16      
28e57 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
28e58 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ine sqlite3_open
28e59 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20 20  16              
28e5a 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28e5b 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 20  lite3_prepare16 
28e5c 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23               0.#
28e5d 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28e5e 70 72 65 70 61 72 65 31 36 5f 76 32 20 20 20 20  prepare16_v2    
28e5f 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
28e60 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
28e61 5f 65 72 72 6f 72 31 36 20 20 20 20 20 20 20 20  _error16        
28e62 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
28e63 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
28e64 36 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64  6          0.# d
28e65 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
28e66 73 75 6c 74 5f 74 65 78 74 31 36 62 65 20 20 20  sult_text16be   
28e67 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
28e68 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
28e69 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20 30  ext16le        0
28e6a 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28e6b 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 20 20  3_value_text16  
28e6c 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
28e6d 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ine sqlite3_valu
28e6e 65 5f 74 65 78 74 31 36 62 65 20 20 20 20 20 20  e_text16be      
28e6f 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28e70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
28e71 31 36 6c 65 20 20 20 20 20 20 20 20 20 30 0a 23  16le         0.#
28e72 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28e73 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
28e74 6e 61 6d 65 31 36 20 30 0a 23 20 64 65 66 69 6e  name16 0.# defin
28e75 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28e76 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20  _table_name16   
28e77 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
28e78 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
28e79 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a 23 65 6e  n_name16   0.#en
28e7a 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
28e7b 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45  TE_OMIT_COMPLETE
28e7c 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28e7d 33 5f 63 6f 6d 70 6c 65 74 65 20 30 0a 23 20 64  3_complete 0.# d
28e7e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
28e7f 6d 70 6c 65 74 65 31 36 20 30 0a 23 65 6e 64 69  mplete16 0.#endi
28e80 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
28e81 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43  _OMIT_PROGRESS_C
28e82 41 4c 4c 42 41 43 4b 0a 23 20 64 65 66 69 6e 65  ALLBACK.# define
28e83 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73   sqlite3_progres
28e84 73 5f 68 61 6e 64 6c 65 72 20 30 0a 23 65 6e 64  s_handler 0.#end
28e85 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
28e86 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
28e87 42 4c 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  BLE.# define sql
28e88 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
28e89 6c 65 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71  le 0.# define sq
28e8a 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
28e8b 75 6c 65 5f 76 32 20 30 0a 23 20 64 65 66 69 6e  ule_v2 0.# defin
28e8c 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  e sqlite3_declar
28e8d 65 5f 76 74 61 62 20 30 0a 23 65 6e 64 69 66 0a  e_vtab 0.#endif.
28e8e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
28e8f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
28e90 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28e91 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  3_enable_shared_
28e92 63 61 63 68 65 20 30 0a 23 65 6e 64 69 66 0a 0a  cache 0.#endif..
28e93 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
28e94 49 54 5f 54 52 41 43 45 0a 23 20 64 65 66 69 6e  IT_TRACE.# defin
28e95 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c  e sqlite3_profil
28e96 65 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69  e       0.# defi
28e97 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65  ne sqlite3_trace
28e98 20 20 20 20 20 20 20 20 20 30 0a 23 65 6e 64 69           0.#endi
28e99 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
28e9a 5f 4f 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 0a  _OMIT_GET_TABLE.
28e9b 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
28e9c 5f 66 72 65 65 5f 74 61 62 6c 65 20 20 20 20 30  _free_table    0
28e9d 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28e9e 33 5f 67 65 74 5f 74 61 62 6c 65 20 20 20 20 20  3_get_table     
28e9f 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  0.#endif..#ifdef
28ea0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
28ea1 52 42 4c 4f 42 0a 23 64 65 66 69 6e 65 20 73 71  RBLOB.#define sq
28ea2 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62  lite3_bind_zerob
28ea3 6c 6f 62 20 20 30 0a 23 64 65 66 69 6e 65 20 73  lob  0.#define s
28ea4 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65  qlite3_blob_byte
28ea5 73 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20  s     0.#define 
28ea6 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
28ea7 73 65 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65  se     0.#define
28ea8 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70   sqlite3_blob_op
28ea9 65 6e 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e  en      0.#defin
28eaa 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72  e sqlite3_blob_r
28eab 65 61 64 20 20 20 20 20 20 30 0a 23 64 65 66 69  ead      0.#defi
28eac 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
28ead 77 72 69 74 65 20 20 20 20 20 30 0a 23 65 6e 64  write     0.#end
28eae 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  if../*.** The fo
28eaf 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
28eb0 65 20 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74  e contains point
28eb1 65 72 73 20 74 6f 20 61 6c 6c 20 53 51 4c 69 74  ers to all SQLit
28eb2 65 20 41 50 49 20 72 6f 75 74 69 6e 65 73 2e 0a  e API routines..
28eb3 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** A pointer to 
28eb4 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
28eb5 73 20 70 61 73 73 65 64 20 69 6e 74 6f 20 65 78  s passed into ex
28eb6 74 65 6e 73 69 6f 6e 73 20 77 68 65 6e 20 74 68  tensions when th
28eb7 65 79 20 61 72 65 0a 2a 2a 20 6c 6f 61 64 65 64  ey are.** loaded
28eb8 20 73 6f 20 74 68 61 74 20 74 68 65 20 65 78 74   so that the ext
28eb9 65 6e 73 69 6f 6e 20 63 61 6e 20 6d 61 6b 65 20  ension can make 
28eba 63 61 6c 6c 73 20 62 61 63 6b 20 69 6e 74 6f 20  calls back into 
28ebb 74 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20 6c 69  the SQLite.** li
28ebc 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  brary..**.** Whe
28ebd 6e 20 61 64 64 69 6e 67 20 6e 65 77 20 41 50 49  n adding new API
28ebe 73 2c 20 61 64 64 20 74 68 65 6d 20 74 6f 20 74  s, add them to t
28ebf 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 69  he bottom of thi
28ec0 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69  s structure.** i
28ec1 6e 20 6f 72 64 65 72 20 74 6f 20 70 72 65 73 65  n order to prese
28ec2 72 76 65 20 62 61 63 6b 77 61 72 64 73 20 63 6f  rve backwards co
28ec3 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a  mpatibility..**.
28ec4 2a 2a 20 45 78 74 65 6e 73 69 6f 6e 73 20 74 68  ** Extensions th
28ec5 61 74 20 75 73 65 20 6e 65 77 65 72 20 41 50 49  at use newer API
28ec6 73 20 73 68 6f 75 6c 64 20 66 69 72 73 74 20 63  s should first c
28ec7 61 6c 6c 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74  all the.** sqlit
28ec8 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75  e3_libversion_nu
28ec9 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b 65 20 73  mber() to make s
28eca 75 72 65 20 74 68 61 74 20 74 68 65 20 41 50 49  ure that the API
28ecb 20 74 68 65 79 0a 2a 2a 20 69 6e 74 65 6e 64 20   they.** intend 
28ecc 74 6f 20 75 73 65 20 69 73 20 73 75 70 70 6f 72  to use is suppor
28ecd 74 65 64 20 62 79 20 74 68 65 20 6c 69 62 72 61  ted by the libra
28ece 72 79 2e 20 20 45 78 74 65 6e 73 69 6f 6e 73 20  ry.  Extensions 
28ecf 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 63  should.** also c
28ed0 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72  heck to make sur
28ed1 65 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74  e that the point
28ed2 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  er to the functi
28ed3 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 4e 55 4c  on is.** not NUL
28ed4 4c 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67  L before calling
28ed5 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63   it..*/.static c
28ed6 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69  onst sqlite3_api
28ed7 5f 72 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65  _routines sqlite
28ed8 33 41 70 69 73 20 3d 20 7b 0a 20 20 73 71 6c 69  3Apis = {.  sqli
28ed9 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
28eda 6e 74 65 78 74 2c 0a 23 69 66 6e 64 65 66 20 53  ntext,.#ifndef S
28edb 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
28edc 43 41 54 45 44 0a 20 20 73 71 6c 69 74 65 33 5f  CATED.  sqlite3_
28edd 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 2c  aggregate_count,
28ede 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64  .#else.  0,.#end
28edf 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  if.  sqlite3_bin
28ee0 64 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65  d_blob,.  sqlite
28ee1 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 2c 0a 20  3_bind_double,. 
28ee2 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
28ee3 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  t,.  sqlite3_bin
28ee4 64 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74  d_int64,.  sqlit
28ee5 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 2c 0a 20 20  e3_bind_null,.  
28ee6 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
28ee7 61 6d 65 74 65 72 5f 63 6f 75 6e 74 2c 0a 20 20  ameter_count,.  
28ee8 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
28ee9 61 6d 65 74 65 72 5f 69 6e 64 65 78 2c 0a 20 20  ameter_index,.  
28eea 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
28eeb 61 6d 65 74 65 72 5f 6e 61 6d 65 2c 0a 20 20 73  ameter_name,.  s
28eec 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
28eed 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
28eee 5f 74 65 78 74 31 36 2c 0a 20 20 73 71 6c 69 74  _text16,.  sqlit
28eef 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 2c 0a 20  e3_bind_value,. 
28ef0 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61   sqlite3_busy_ha
28ef1 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69 74 65 33  ndler,.  sqlite3
28ef2 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 2c 0a 20  _busy_timeout,. 
28ef3 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
28ef4 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73  ,.  sqlite3_clos
28ef5 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  e,.  sqlite3_col
28ef6 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 2c 0a 20  lation_needed,. 
28ef7 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
28ef8 6f 6e 5f 6e 65 65 64 65 64 31 36 2c 0a 20 20 73  on_needed16,.  s
28ef9 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
28efa 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ob,.  sqlite3_co
28efb 6c 75 6d 6e 5f 62 79 74 65 73 2c 0a 20 20 73 71  lumn_bytes,.  sq
28efc 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
28efd 65 73 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f  es16,.  sqlite3_
28efe 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 2c 0a 20 20  column_count,.  
28eff 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
28f00 61 74 61 62 61 73 65 5f 6e 61 6d 65 2c 0a 20 20  atabase_name,.  
28f01 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
28f02 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 2c 0a  atabase_name16,.
28f03 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28f04 5f 64 65 63 6c 74 79 70 65 2c 0a 20 20 73 71 6c  _decltype,.  sql
28f05 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c  ite3_column_decl
28f06 74 79 70 65 31 36 2c 0a 20 20 73 71 6c 69 74 65  type16,.  sqlite
28f07 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 2c  3_column_double,
28f08 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
28f09 6e 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33  n_int,.  sqlite3
28f0a 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 2c 0a 20  _column_int64,. 
28f0b 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28f0c 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  name,.  sqlite3_
28f0d 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 2c 0a 20  column_name16,. 
28f0e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28f0f 6f 72 69 67 69 6e 5f 6e 61 6d 65 2c 0a 20 20 73  origin_name,.  s
28f10 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
28f11 69 67 69 6e 5f 6e 61 6d 65 31 36 2c 0a 20 20 73  igin_name16,.  s
28f12 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61  qlite3_column_ta
28f13 62 6c 65 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69  ble_name,.  sqli
28f14 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
28f15 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74  _name16,.  sqlit
28f16 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 2c 0a  e3_column_text,.
28f17 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28f18 5f 74 65 78 74 31 36 2c 0a 20 20 73 71 6c 69 74  _text16,.  sqlit
28f19 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 2c 0a  e3_column_type,.
28f1a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28f1b 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65  _value,.  sqlite
28f1c 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 2c 0a 20  3_commit_hook,. 
28f1d 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
28f1e 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d  e,.  sqlite3_com
28f1f 70 6c 65 74 65 31 36 2c 0a 20 20 73 71 6c 69 74  plete16,.  sqlit
28f20 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
28f21 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ion,.  sqlite3_c
28f22 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
28f23 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65  6,.  sqlite3_cre
28f24 61 74 65 5f 66 75 6e 63 74 69 6f 6e 2c 0a 20 20  ate_function,.  
28f25 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
28f26 75 6e 63 74 69 6f 6e 31 36 2c 0a 20 20 73 71 6c  unction16,.  sql
28f27 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
28f28 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 61  le,.  sqlite3_da
28f29 74 61 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69  ta_count,.  sqli
28f2a 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 2c 0a 20  te3_db_handle,. 
28f2b 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
28f2c 5f 76 74 61 62 2c 0a 20 20 73 71 6c 69 74 65 33  _vtab,.  sqlite3
28f2d 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  _enable_shared_c
28f2e 61 63 68 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ache,.  sqlite3_
28f2f 65 72 72 63 6f 64 65 2c 0a 20 20 73 71 6c 69 74  errcode,.  sqlit
28f30 65 33 5f 65 72 72 6d 73 67 2c 0a 20 20 73 71 6c  e3_errmsg,.  sql
28f31 69 74 65 33 5f 65 72 72 6d 73 67 31 36 2c 0a 20  ite3_errmsg16,. 
28f32 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2c 0a 23   sqlite3_exec,.#
28f33 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
28f34 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20  IT_DEPRECATED.  
28f35 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65 64 2c  sqlite3_expired,
28f36 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64  .#else.  0,.#end
28f37 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e  if.  sqlite3_fin
28f38 61 6c 69 7a 65 2c 0a 20 20 73 71 6c 69 74 65 33  alize,.  sqlite3
28f39 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69 74 65 33  _free,.  sqlite3
28f3a 5f 66 72 65 65 5f 74 61 62 6c 65 2c 0a 20 20 73  _free_table,.  s
28f3b 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63  qlite3_get_autoc
28f3c 6f 6d 6d 69 74 2c 0a 20 20 73 71 6c 69 74 65 33  ommit,.  sqlite3
28f3d 5f 67 65 74 5f 61 75 78 64 61 74 61 2c 0a 20 20  _get_auxdata,.  
28f3e 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
28f3f 65 2c 0a 20 20 30 2c 20 20 20 20 20 2f 2a 20 57  e,.  0,     /* W
28f40 61 73 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61  as sqlite3_globa
28f41 6c 5f 72 65 63 6f 76 65 72 28 29 2c 20 62 75 74  l_recover(), but
28f42 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 20 69   that function i
28f43 73 20 64 65 70 72 65 63 61 74 65 64 20 2a 2f 0a  s deprecated */.
28f44 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72    sqlite3_interr
28f45 75 70 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c  upt,.  sqlite3_l
28f46 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
28f47 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 62 76  ,.  sqlite3_libv
28f48 65 72 73 69 6f 6e 2c 0a 20 20 73 71 6c 69 74 65  ersion,.  sqlite
28f49 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
28f4a 62 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ber,.  sqlite3_m
28f4b 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33  alloc,.  sqlite3
28f4c 5f 6d 70 72 69 6e 74 66 2c 0a 20 20 73 71 6c 69  _mprintf,.  sqli
28f4d 74 65 33 5f 6f 70 65 6e 2c 0a 20 20 73 71 6c 69  te3_open,.  sqli
28f4e 74 65 33 5f 6f 70 65 6e 31 36 2c 0a 20 20 73 71  te3_open16,.  sq
28f4f 6c 69 74 65 33 5f 70 72 65 70 61 72 65 2c 0a 20  lite3_prepare,. 
28f50 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
28f51 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72  16,.  sqlite3_pr
28f52 6f 66 69 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  ofile,.  sqlite3
28f53 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65  _progress_handle
28f54 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 61  r,.  sqlite3_rea
28f55 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  lloc,.  sqlite3_
28f56 72 65 73 65 74 2c 0a 20 20 73 71 6c 69 74 65 33  reset,.  sqlite3
28f57 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 2c 0a 20 20  _result_blob,.  
28f58 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
28f59 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33  ouble,.  sqlite3
28f5a 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 2c 0a 20  _result_error,. 
28f5b 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28f5c 65 72 72 6f 72 31 36 2c 0a 20 20 73 71 6c 69 74  error16,.  sqlit
28f5d 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 2c 0a 20  e3_result_int,. 
28f5e 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28f5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33  int64,.  sqlite3
28f60 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 2c 0a 20 20  _result_null,.  
28f61 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
28f62 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  ext,.  sqlite3_r
28f63 65 73 75 6c 74 5f 74 65 78 74 31 36 2c 0a 20 20  esult_text16,.  
28f64 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
28f65 65 78 74 31 36 62 65 2c 0a 20 20 73 71 6c 69 74  ext16be,.  sqlit
28f66 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
28f67 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  le,.  sqlite3_re
28f68 73 75 6c 74 5f 76 61 6c 75 65 2c 0a 20 20 73 71  sult_value,.  sq
28f69 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68  lite3_rollback_h
28f6a 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73  ook,.  sqlite3_s
28f6b 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 2c 0a 20  et_authorizer,. 
28f6c 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78   sqlite3_set_aux
28f6d 64 61 74 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f  data,.  sqlite3_
28f6e 73 6e 70 72 69 6e 74 66 2c 0a 20 20 73 71 6c 69  snprintf,.  sqli
28f6f 74 65 33 5f 73 74 65 70 2c 0a 20 20 73 71 6c 69  te3_step,.  sqli
28f70 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e  te3_table_column
28f71 5f 6d 65 74 61 64 61 74 61 2c 0a 23 69 66 6e 64  _metadata,.#ifnd
28f72 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
28f73 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69  EPRECATED.  sqli
28f74 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e  te3_thread_clean
28f75 75 70 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a 23  up,.#else.  0,.#
28f76 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f  endif.  sqlite3_
28f77 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 2c 0a 20  total_changes,. 
28f78 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 2c 0a   sqlite3_trace,.
28f79 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28f7a 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20  MIT_DEPRECATED. 
28f7b 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65   sqlite3_transfe
28f7c 72 5f 62 69 6e 64 69 6e 67 73 2c 0a 23 65 6c 73  r_bindings,.#els
28f7d 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20 20  e.  0,.#endif.  
28f7e 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68  sqlite3_update_h
28f7f 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 75  ook,.  sqlite3_u
28f80 73 65 72 5f 64 61 74 61 2c 0a 20 20 73 71 6c 69  ser_data,.  sqli
28f81 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 2c 0a  te3_value_blob,.
28f82 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
28f83 62 79 74 65 73 2c 0a 20 20 73 71 6c 69 74 65 33  bytes,.  sqlite3
28f84 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36 2c 0a  _value_bytes16,.
28f85 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
28f86 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65  double,.  sqlite
28f87 33 5f 76 61 6c 75 65 5f 69 6e 74 2c 0a 20 20 73  3_value_int,.  s
28f88 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
28f89 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  64,.  sqlite3_va
28f8a 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  lue_numeric_type
28f8b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
28f8c 65 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65  e_text,.  sqlite
28f8d 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 0a  3_value_text16,.
28f8e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
28f8f 74 65 78 74 31 36 62 65 2c 0a 20 20 73 71 6c 69  text16be,.  sqli
28f90 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
28f91 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  le,.  sqlite3_va
28f92 6c 75 65 5f 74 79 70 65 2c 0a 20 20 73 71 6c 69  lue_type,.  sqli
28f93 74 65 33 5f 76 6d 70 72 69 6e 74 66 2c 0a 20 20  te3_vmprintf,.  
28f94 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6f 72 69 67  /*.  ** The orig
28f95 69 6e 61 6c 20 41 50 49 20 73 65 74 20 65 6e 64  inal API set end
28f96 73 20 68 65 72 65 2e 20 20 41 6c 6c 20 65 78 74  s here.  All ext
28f97 65 6e 73 69 6f 6e 73 20 63 61 6e 20 63 61 6c 6c  ensions can call
28f98 20 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65   any.  ** of the
28f99 20 41 50 49 73 20 61 62 6f 76 65 20 70 72 6f 76   APIs above prov
28f9a 69 64 65 64 20 74 68 61 74 20 74 68 65 20 70 6f  ided that the po
28f9b 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c  inter is not NUL
28f9c 4c 2e 20 20 42 75 74 0a 20 20 2a 2a 20 62 65 66  L.  But.  ** bef
28f9d 6f 72 65 20 63 61 6c 6c 69 6e 67 20 41 50 49 73  ore calling APIs
28f9e 20 74 68 61 74 20 66 6f 6c 6c 6f 77 2c 20 65 78   that follow, ex
28f9f 74 65 6e 73 69 6f 6e 20 73 68 6f 75 6c 64 20 63  tension should c
28fa0 68 65 63 6b 20 74 68 65 0a 20 20 2a 2a 20 73 71  heck the.  ** sq
28fa1 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
28fa2 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20 6d 61 6b  _number() to mak
28fa3 65 20 73 75 72 65 20 74 68 65 79 20 61 72 65 20  e sure they are 
28fa4 64 65 61 6c 69 6e 67 20 77 69 74 68 0a 20 20 2a  dealing with.  *
28fa5 2a 20 61 20 6c 69 62 72 61 72 79 20 74 68 61 74  * a library that
28fa6 20 69 73 20 6e 65 77 20 65 6e 6f 75 67 68 20 74   is new enough t
28fa7 6f 20 73 75 70 70 6f 72 74 20 74 68 61 74 20 41  o support that A
28fa8 50 49 2e 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  PI..  **********
28fa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28faa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28fab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28fac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
28fad 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6f    */.  sqlite3_o
28fae 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
28faf 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64  ,..  /*.  ** Add
28fb0 65 64 20 61 66 74 65 72 20 33 2e 33 2e 31 33 0a  ed after 3.3.13.
28fb1 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70    */.  sqlite3_p
28fb2 72 65 70 61 72 65 5f 76 32 2c 0a 20 20 73 71 6c  repare_v2,.  sql
28fb3 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
28fb4 32 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6c 65  2,.  sqlite3_cle
28fb5 61 72 5f 62 69 6e 64 69 6e 67 73 2c 0a 0a 20 20  ar_bindings,..  
28fb6 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f  /*.  ** Added fo
28fb7 72 20 33 2e 34 2e 31 0a 20 20 2a 2f 0a 20 20 73  r 3.4.1.  */.  s
28fb8 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
28fb9 64 75 6c 65 5f 76 32 2c 0a 0a 20 20 2f 2a 0a 20  dule_v2,..  /*. 
28fba 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e   ** Added for 3.
28fbb 35 2e 30 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  5.0.  */.  sqlit
28fbc 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  e3_bind_zeroblob
28fbd 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ,.  sqlite3_blob
28fbe 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69 74 65  _bytes,.  sqlite
28fbf 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 2c 0a 20 20  3_blob_close,.  
28fc0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
28fc1 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  n,.  sqlite3_blo
28fc2 62 5f 72 65 61 64 2c 0a 20 20 73 71 6c 69 74 65  b_read,.  sqlite
28fc3 33 5f 62 6c 6f 62 5f 77 72 69 74 65 2c 0a 20 20  3_blob_write,.  
28fc4 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
28fc5 6f 6c 6c 61 74 69 6f 6e 5f 76 32 2c 0a 20 20 73  ollation_v2,.  s
28fc6 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
28fc7 72 6f 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rol,.  sqlite3_m
28fc8 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 2c  emory_highwater,
28fc9 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  .  sqlite3_memor
28fca 79 5f 75 73 65 64 2c 0a 23 69 66 64 65 66 20 53  y_used,.#ifdef S
28fcb 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54  QLITE_MUTEX_OMIT
28fcc 0a 20 20 30 2c 20 0a 20 20 30 2c 20 0a 20 20 30  .  0, .  0, .  0
28fcd 2c 0a 20 20 30 2c 0a 20 20 30 2c 0a 23 65 6c 73  ,.  0,.  0,.#els
28fce 65 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  e.  sqlite3_mute
28fcf 78 5f 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69 74  x_alloc,.  sqlit
28fd0 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 2c 0a  e3_mutex_enter,.
28fd1 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
28fd2 66 72 65 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  free,.  sqlite3_
28fd3 6d 75 74 65 78 5f 6c 65 61 76 65 2c 0a 20 20 73  mutex_leave,.  s
28fd4 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
28fd5 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74  ,.#endif.  sqlit
28fd6 65 33 5f 6f 70 65 6e 5f 76 32 2c 0a 20 20 73 71  e3_open_v2,.  sq
28fd7 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
28fd8 6d 6f 72 79 2c 0a 20 20 73 71 6c 69 74 65 33 5f  mory,.  sqlite3_
28fd9 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
28fda 65 6d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  em,.  sqlite3_re
28fdb 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
28fdc 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6c 65  g,.  sqlite3_sle
28fdd 65 70 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f  ep,.  sqlite3_so
28fde 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 2c 0a 20  ft_heap_limit,. 
28fdf 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e   sqlite3_vfs_fin
28fe0 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  d,.  sqlite3_vfs
28fe1 5f 72 65 67 69 73 74 65 72 2c 0a 20 20 73 71 6c  _register,.  sql
28fe2 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73  ite3_vfs_unregis
28fe3 74 65 72 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ter,..  /*.  ** 
28fe4 41 64 64 65 64 20 66 6f 72 20 33 2e 35 2e 38 0a  Added for 3.5.8.
28fe5 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74    */.  sqlite3_t
28fe6 68 72 65 61 64 73 61 66 65 2c 0a 20 20 73 71 6c  hreadsafe,.  sql
28fe7 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f  ite3_result_zero
28fe8 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f  blob,.  sqlite3_
28fe9 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64  result_error_cod
28fea 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 65 73  e,.  sqlite3_tes
28feb 74 5f 63 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c  t_control,.  sql
28fec 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 2c  ite3_randomness,
28fed 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
28fee 78 74 5f 64 62 5f 68 61 6e 64 6c 65 2c 0a 0a 20  xt_db_handle,.. 
28fef 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66   /*.  ** Added f
28ff0 6f 72 20 33 2e 36 2e 30 0a 20 20 2a 2f 0a 20 20  or 3.6.0.  */.  
28ff1 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
28ff2 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 2c 0a 20  _result_codes,. 
28ff3 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 2c 0a   sqlite3_limit,.
28ff4 20 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73    sqlite3_next_s
28ff5 74 6d 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73  tmt,.  sqlite3_s
28ff6 71 6c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  ql,.  sqlite3_st
28ff7 61 74 75 73 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  atus,.};../*.** 
28ff8 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61 64 20  Attempt to load 
28ff9 61 6e 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73  an SQLite extens
28ffa 69 6f 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74  ion library cont
28ffb 61 69 6e 65 64 20 69 6e 20 74 68 65 20 66 69 6c  ained in the fil
28ffc 65 0a 2a 2a 20 7a 46 69 6c 65 2e 20 20 54 68 65  e.** zFile.  The
28ffd 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69 73 20   entry point is 
28ffe 7a 50 72 6f 63 2e 20 20 7a 50 72 6f 63 20 6d 61  zProc.  zProc ma
28fff 79 20 62 65 20 30 20 69 6e 20 77 68 69 63 68 20  y be 0 in which 
29000 63 61 73 65 20 61 0a 2a 2a 20 64 65 66 61 75 6c  case a.** defaul
29001 74 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 6e 61  t entry point na
29002 6d 65 20 28 73 71 6c 69 74 65 33 5f 65 78 74 65  me (sqlite3_exte
29003 6e 73 69 6f 6e 5f 69 6e 69 74 29 20 69 73 20 75  nsion_init) is u
29004 73 65 64 2e 20 20 55 73 65 0a 2a 2a 20 6f 66 20  sed.  Use.** of 
29005 74 68 65 20 64 65 66 61 75 6c 74 20 6e 61 6d 65  the default name
29006 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 2e   is recommended.
29007 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
29008 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
29009 73 73 20 61 6e 64 20 53 51 4c 49 54 45 5f 45 52  ss and SQLITE_ER
2900a 52 4f 52 20 69 66 20 73 6f 6d 65 74 68 69 6e 67  ROR if something
2900b 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a   goes wrong..**.
2900c 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
2900d 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72 72 4d  ccurs and pzErrM
2900e 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65  sg is not 0, the
2900f 6e 20 66 69 6c 6c 20 2a 70 7a 45 72 72 4d 73 67  n fill *pzErrMsg
29010 20 77 69 74 68 20 0a 2a 2a 20 65 72 72 6f 72 20   with .** error 
29011 6d 65 73 73 61 67 65 20 74 65 78 74 2e 20 20 54  message text.  T
29012 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
29013 69 6f 6e 20 73 68 6f 75 6c 64 20 66 72 65 65 20  ion should free 
29014 74 68 69 73 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62  this memory.** b
29015 79 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  y calling sqlite
29016 33 44 62 46 72 65 65 28 64 62 2c 20 29 2e 0a 2a  3DbFree(db, )..*
29017 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
29018 69 74 65 33 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ite3LoadExtensio
29019 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n(.  sqlite3 *db
2901a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  ,          /* Lo
2901b 61 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e  ad the extension
2901c 20 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62   into this datab
2901d 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2901e 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
2901f 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d  zFile,    /* Nam
29020 65 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20  e of the shared 
29021 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69  library containi
29022 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a  ng extension */.
29023 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
29024 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79  roc,    /* Entry
29025 20 70 6f 69 6e 74 2e 20 20 55 73 65 20 22 73 71   point.  Use "sq
29026 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f  lite3_extension_
29027 69 6e 69 74 22 20 69 66 20 30 20 2a 2f 0a 20 20  init" if 0 */.  
29028 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20  char **pzErrMsg 
29029 20 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72        /* Put err
2902a 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20  or message here 
2902b 69 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20  if not 0 */.){. 
2902c 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
2902d 66 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20  fs = db->pVfs;. 
2902e 20 76 6f 69 64 20 2a 68 61 6e 64 6c 65 3b 0a 20   void *handle;. 
2902f 20 69 6e 74 20 28 2a 78 49 6e 69 74 29 28 73 71   int (*xInit)(sq
29030 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f  lite3*,char**,co
29031 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
29032 72 6f 75 74 69 6e 65 73 2a 29 3b 0a 20 20 63 68  routines*);.  ch
29033 61 72 20 2a 7a 45 72 72 6d 73 67 20 3d 20 30 3b  ar *zErrmsg = 0;
29034 0a 20 20 76 6f 69 64 20 2a 2a 61 48 61 6e 64 6c  .  void **aHandl
29035 65 3b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e  e;.  const int n
29036 4d 73 67 20 3d 20 33 30 30 3b 0a 0a 20 20 69 66  Msg = 300;..  if
29037 28 20 70 7a 45 72 72 4d 73 67 20 29 20 2a 70 7a  ( pzErrMsg ) *pz
29038 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 2f  ErrMsg = 0;..  /
29039 2a 20 54 69 63 6b 65 74 20 23 31 38 36 33 2e 20  * Ticket #1863. 
2903a 20 54 6f 20 61 76 6f 69 64 20 61 20 63 72 65 61   To avoid a crea
2903b 74 69 6e 67 20 73 65 63 75 72 69 74 79 20 70 72  ting security pr
2903c 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 6c 64 65 72  oblems for older
2903d 0a 20 20 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  .  ** applicatio
2903e 6e 73 20 74 68 61 74 20 72 65 6c 69 6e 6b 20 61  ns that relink a
2903f 67 61 69 6e 73 74 20 6e 65 77 65 72 20 76 65 72  gainst newer ver
29040 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2c  sions of SQLite,
29041 20 74 68 65 0a 20 20 2a 2a 20 61 62 69 6c 69 74   the.  ** abilit
29042 79 20 74 6f 20 72 75 6e 20 6c 6f 61 64 5f 65 78  y to run load_ex
29043 74 65 6e 73 69 6f 6e 20 69 73 20 74 75 72 6e 65  tension is turne
29044 64 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c 74  d off by default
29045 2e 20 20 4f 6e 65 0a 20 20 2a 2a 20 6d 75 73 74  .  One.  ** must
29046 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 65 6e   call sqlite3_en
29047 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73  able_load_extens
29048 69 6f 6e 28 29 20 74 6f 20 74 75 72 6e 20 6f 6e  ion() to turn on
29049 20 65 78 74 65 6e 73 69 6f 6e 0a 20 20 2a 2a 20   extension.  ** 
2904a 6c 6f 61 64 69 6e 67 2e 20 20 4f 74 68 65 72 77  loading.  Otherw
2904b 69 73 65 20 79 6f 75 20 67 65 74 20 74 68 65 20  ise you get the 
2904c 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 2e  following error.
2904d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d  .  */.  if( (db-
2904e 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
2904f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 29 3d 3d  LoadExtension)==
29050 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45  0 ){.    if( pzE
29051 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 2a  rrMsg ){.      *
29052 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
29053 65 33 5f 6d 70 72 69 6e 74 66 28 22 6e 6f 74 20  e3_mprintf("not 
29054 61 75 74 68 6f 72 69 7a 65 64 22 29 3b 0a 20 20  authorized");.  
29055 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53    }.    return S
29056 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
29057 0a 0a 20 20 69 66 28 20 7a 50 72 6f 63 3d 3d 30  ..  if( zProc==0
29058 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20   ){.    zProc = 
29059 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69  "sqlite3_extensi
2905a 6f 6e 5f 69 6e 69 74 22 3b 0a 20 20 7d 0a 0a 20  on_init";.  }.. 
2905b 20 68 61 6e 64 6c 65 20 3d 20 73 71 6c 69 74 65   handle = sqlite
2905c 33 4f 73 44 6c 4f 70 65 6e 28 70 56 66 73 2c 20  3OsDlOpen(pVfs, 
2905d 7a 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 68 61  zFile);.  if( ha
2905e 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ndle==0 ){.    i
2905f 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  f( pzErrMsg ){. 
29060 20 20 20 20 20 7a 45 72 72 6d 73 67 20 3d 20 73       zErrmsg = s
29061 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63  qlite3StackAlloc
29062 5a 65 72 6f 28 64 62 2c 20 6e 4d 73 67 29 3b 0a  Zero(db, nMsg);.
29063 20 20 20 20 20 20 69 66 28 20 7a 45 72 72 6d 73        if( zErrms
29064 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  g ){.        sql
29065 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4d  ite3_snprintf(nM
29066 73 67 2c 20 7a 45 72 72 6d 73 67 2c 20 0a 20 20  sg, zErrmsg, .  
29067 20 20 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c            "unabl
29068 65 20 74 6f 20 6f 70 65 6e 20 73 68 61 72 65 64  e to open shared
29069 20 6c 69 62 72 61 72 79 20 5b 25 73 5d 22 2c 20   library [%s]", 
2906a 7a 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 20 20  zFile);.        
2906b 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72  sqlite3OsDlError
2906c 28 70 56 66 73 2c 20 6e 4d 73 67 2d 31 2c 20 7a  (pVfs, nMsg-1, z
2906d 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 20  Errmsg);.       
2906e 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c   *pzErrMsg = sql
2906f 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c 20  ite3DbStrDup(0, 
29070 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20  zErrmsg);.      
29071 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72    sqlite3StackFr
29072 65 65 28 64 62 2c 20 7a 45 72 72 6d 73 67 29 3b  ee(db, zErrmsg);
29073 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
29074 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
29075 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 78 49  _ERROR;.  }.  xI
29076 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29 28 73 71  nit = (int(*)(sq
29077 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f  lite3*,char**,co
29078 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  nst sqlite3_api_
29079 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20 20 20  routines*)).    
2907a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2907b 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 70 56  qlite3OsDlSym(pV
2907c 66 73 2c 20 68 61 6e 64 6c 65 2c 20 7a 50 72 6f  fs, handle, zPro
2907d 63 29 3b 0a 20 20 69 66 28 20 78 49 6e 69 74 3d  c);.  if( xInit=
2907e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a  =0 ){.    if( pz
2907f 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
29080 7a 45 72 72 6d 73 67 20 3d 20 73 71 6c 69 74 65  zErrmsg = sqlite
29081 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28  3StackAllocZero(
29082 64 62 2c 20 6e 4d 73 67 29 3b 0a 20 20 20 20 20  db, nMsg);.     
29083 20 69 66 28 20 7a 45 72 72 6d 73 67 20 29 7b 0a   if( zErrmsg ){.
29084 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
29085 73 6e 70 72 69 6e 74 66 28 6e 4d 73 67 2c 20 7a  snprintf(nMsg, z
29086 45 72 72 6d 73 67 2c 0a 20 20 20 20 20 20 20 20  Errmsg,.        
29087 20 20 20 20 22 6e 6f 20 65 6e 74 72 79 20 70 6f      "no entry po
29088 69 6e 74 20 5b 25 73 5d 20 69 6e 20 73 68 61 72  int [%s] in shar
29089 65 64 20 6c 69 62 72 61 72 79 20 5b 25 73 5d 22  ed library [%s]"
2908a 2c 20 7a 50 72 6f 63 2c 7a 46 69 6c 65 29 3b 0a  , zProc,zFile);.
2908b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
2908c 73 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 6e  sDlError(pVfs, n
2908d 4d 73 67 2d 31 2c 20 7a 45 72 72 6d 73 67 29 3b  Msg-1, zErrmsg);
2908e 0a 20 20 20 20 20 20 20 20 2a 70 7a 45 72 72 4d  .        *pzErrM
2908f 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  sg = sqlite3DbSt
29090 72 44 75 70 28 30 2c 20 7a 45 72 72 6d 73 67 29  rDup(0, zErrmsg)
29091 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29092 33 53 74 61 63 6b 46 72 65 65 28 64 62 2c 20 7a  3StackFree(db, z
29093 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 7d  Errmsg);.      }
29094 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
29095 44 6c 43 6c 6f 73 65 28 70 56 66 73 2c 20 68 61  DlClose(pVfs, ha
29096 6e 64 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ndle);.    }.   
29097 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
29098 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66  RROR;.  }else if
29099 28 20 78 49 6e 69 74 28 64 62 2c 20 26 7a 45 72  ( xInit(db, &zEr
2909a 72 6d 73 67 2c 20 26 73 71 6c 69 74 65 33 41 70  rmsg, &sqlite3Ap
2909b 69 73 29 20 29 7b 0a 20 20 20 20 69 66 28 20 70  is) ){.    if( p
2909c 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20  zErrMsg ){.     
2909d 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c   *pzErrMsg = sql
2909e 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 65 72  ite3_mprintf("er
2909f 72 6f 72 20 64 75 72 69 6e 67 20 69 6e 69 74 69  ror during initi
290a0 61 6c 69 7a 61 74 69 6f 6e 3a 20 25 73 22 2c 20  alization: %s", 
290a1 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 7d 0a  zErrmsg);.    }.
290a2 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
290a3 28 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 73  (zErrmsg);.    s
290a4 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28  qlite3OsDlClose(
290a5 70 56 66 73 2c 20 68 61 6e 64 6c 65 29 3b 0a 20  pVfs, handle);. 
290a6 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
290a7 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f  _ERROR;.  }..  /
290a8 2a 20 41 70 70 65 6e 64 20 74 68 65 20 6e 65 77  * Append the new
290a9 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
290aa 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20 64 62  handle to the db
290ab 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 20 61 72 72  ->aExtension arr
290ac 61 79 2e 20 2a 2f 0a 20 20 61 48 61 6e 64 6c 65  ay. */.  aHandle
290ad 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
290ae 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
290af 66 28 68 61 6e 64 6c 65 29 2a 28 64 62 2d 3e 6e  f(handle)*(db->n
290b0 45 78 74 65 6e 73 69 6f 6e 2b 31 29 29 3b 0a 20  Extension+1));. 
290b1 20 69 66 28 20 61 48 61 6e 64 6c 65 3d 3d 30 20   if( aHandle==0 
290b2 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
290b3 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
290b4 20 20 69 66 28 20 64 62 2d 3e 6e 45 78 74 65 6e    if( db->nExten
290b5 73 69 6f 6e 3e 30 20 29 7b 0a 20 20 20 20 6d 65  sion>0 ){.    me
290b6 6d 63 70 79 28 61 48 61 6e 64 6c 65 2c 20 64 62  mcpy(aHandle, db
290b7 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 2c 20 73 69  ->aExtension, si
290b8 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a 64 62 2d  zeof(handle)*db-
290b9 3e 6e 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 20 20  >nExtension);.  
290ba 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
290bb 65 28 64 62 2c 20 64 62 2d 3e 61 45 78 74 65 6e  e(db, db->aExten
290bc 73 69 6f 6e 29 3b 0a 20 20 64 62 2d 3e 61 45 78  sion);.  db->aEx
290bd 74 65 6e 73 69 6f 6e 20 3d 20 61 48 61 6e 64 6c  tension = aHandl
290be 65 3b 0a 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e  e;..  db->aExten
290bf 73 69 6f 6e 5b 64 62 2d 3e 6e 45 78 74 65 6e 73  sion[db->nExtens
290c0 69 6f 6e 2b 2b 5d 20 3d 20 68 61 6e 64 6c 65 3b  ion++] = handle;
290c1 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
290c2 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  _OK;.}.SQLITE_AP
290c3 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f  I int sqlite3_lo
290c4 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20  ad_extension(.  
290c5 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
290c6 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68        /* Load th
290c7 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f  e extension into
290c8 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63   this database c
290c9 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63  onnection */.  c
290ca 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
290cb 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ,    /* Name of 
290cc 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61  the shared libra
290cd 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78  ry containing ex
290ce 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e  tension */.  con
290cf 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20  st char *zProc, 
290d0 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e     /* Entry poin
290d1 74 2e 20 20 55 73 65 20 22 73 71 6c 69 74 65 33  t.  Use "sqlite3
290d2 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22  _extension_init"
290d3 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72 20   if 0 */.  char 
290d4 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20 20  **pzErrMsg      
290d5 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d 65   /* Put error me
290d6 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e 6f  ssage here if no
290d7 74 20 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  t 0 */.){.  int 
290d8 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rc;.  sqlite3_mu
290d9 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
290da 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  tex);.  rc = sql
290db 69 74 65 33 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ite3LoadExtensio
290dc 6e 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a 50 72  n(db, zFile, zPr
290dd 6f 63 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20  oc, pzErrMsg);. 
290de 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
290df 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
290e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
290e1 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
290e2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
290e3 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20  /*.** Call this 
290e4 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65  routine when the
290e5 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
290e6 74 69 6f 6e 20 69 73 20 63 6c 6f 73 69 6e 67 20  tion is closing 
290e7 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 63  in order.** to c
290e8 6c 65 61 6e 20 75 70 20 6c 6f 61 64 65 64 20 65  lean up loaded e
290e9 78 74 65 6e 73 69 6f 6e 73 0a 2a 2f 0a 53 51 4c  xtensions.*/.SQL
290ea 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
290eb 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74   sqlite3CloseExt
290ec 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  ensions(sqlite3 
290ed 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
290ee 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
290ef 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
290f0 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28  mutex) );.  for(
290f1 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 45 78 74 65  i=0; i<db->nExte
290f2 6e 73 69 6f 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  nsion; i++){.   
290f3 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73   sqlite3OsDlClos
290f4 65 28 64 62 2d 3e 70 56 66 73 2c 20 64 62 2d 3e  e(db->pVfs, db->
290f5 61 45 78 74 65 6e 73 69 6f 6e 5b 69 5d 29 3b 0a  aExtension[i]);.
290f6 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46    }.  sqlite3DbF
290f7 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 45 78 74  ree(db, db->aExt
290f8 65 6e 73 69 6f 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ension);.}../*.*
290f9 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61  * Enable or disa
290fa 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  ble extension lo
290fb 61 64 69 6e 67 2e 20 20 45 78 74 65 6e 73 69 6f  ading.  Extensio
290fc 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 64 69 73  n loading is dis
290fd 61 62 6c 65 64 20 62 79 0a 2a 2a 20 64 65 66 61  abled by.** defa
290fe 75 6c 74 20 73 6f 20 61 73 20 6e 6f 74 20 74 6f  ult so as not to
290ff 20 6f 70 65 6e 20 73 65 63 75 72 69 74 79 20 68   open security h
29100 6f 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20 61 70  oles in older ap
29101 70 6c 69 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53  plications..*/.S
29102 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
29103 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61  lite3_enable_loa
29104 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69  d_extension(sqli
29105 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f  te3 *db, int ono
29106 66 66 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ff){.  sqlite3_m
29107 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
29108 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f 6e 6f  utex);.  if( ono
29109 66 66 20 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c  ff ){.    db->fl
2910a 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4c 6f  ags |= SQLITE_Lo
2910b 61 64 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d  adExtension;.  }
2910c 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 66 6c  else{.    db->fl
2910d 61 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c  ags &= ~SQLITE_L
2910e 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20  oadExtension;.  
2910f 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
29110 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
29111 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  x);.  return SQL
29112 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69  ITE_OK;.}..#endi
29113 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
29114 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20  _LOAD_EXTENSION 
29115 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 75  */../*.** The au
29116 74 6f 2d 65 78 74 65 6e 73 69 6f 6e 20 63 6f 64  to-extension cod
29117 65 20 61 64 64 65 64 20 72 65 67 61 72 64 6c 65  e added regardle
29118 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72  ss of whether or
29119 20 6e 6f 74 20 65 78 74 65 6e 73 69 6f 6e 0a 2a   not extension.*
2911a 2a 20 6c 6f 61 64 69 6e 67 20 69 73 20 73 75 70  * loading is sup
2911b 70 6f 72 74 65 64 2e 20 20 57 65 20 6e 65 65 64  ported.  We need
2911c 20 61 20 64 75 6d 6d 79 20 73 71 6c 69 74 65 33   a dummy sqlite3
2911d 41 70 69 73 20 70 6f 69 6e 74 65 72 20 66 6f 72  Apis pointer for
2911e 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65 20 69 66   that.** code if
2911f 20 72 65 67 75 6c 61 72 20 65 78 74 65 6e 73 69   regular extensi
29120 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6e 6f  on loading is no
29121 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 54 68  t available.  Th
29122 69 73 20 69 73 20 74 68 61 74 0a 2a 2a 20 64 75  is is that.** du
29123 6d 6d 79 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a  mmy pointer..*/.
29124 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
29125 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
29126 4e 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  N.static const s
29127 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69  qlite3_api_routi
29128 6e 65 73 20 73 71 6c 69 74 65 33 41 70 69 73 20  nes sqlite3Apis 
29129 3d 20 7b 20 30 20 7d 3b 0a 23 65 6e 64 69 66 0a  = { 0 };.#endif.
2912a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
2912b 6f 77 69 6e 67 20 6f 62 6a 65 63 74 20 68 6f 6c  owing object hol
2912c 64 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61  ds the list of a
2912d 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 6c 6f 61  utomatically loa
2912e 64 65 64 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e  ded.** extension
2912f 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 69  s..**.** This li
29130 73 74 20 69 73 20 73 68 61 72 65 64 20 61 63 72  st is shared acr
29131 6f 73 73 20 74 68 72 65 61 64 73 2e 20 20 54 68  oss threads.  Th
29132 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
29133 54 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20  TATIC_MASTER.** 
29134 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68 65  mutex must be he
29135 6c 64 20 77 68 69 6c 65 20 61 63 63 65 73 73 69  ld while accessi
29136 6e 67 20 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2f  ng this list..*/
29137 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
29138 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69  sqlite3AutoExtLi
29139 73 74 20 73 71 6c 69 74 65 33 41 75 74 6f 45 78  st sqlite3AutoEx
2913a 74 4c 69 73 74 3b 0a 73 74 61 74 69 63 20 53 51  tList;.static SQ
2913b 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
2913c 73 71 6c 69 74 65 33 41 75 74 6f 45 78 74 4c 69  sqlite3AutoExtLi
2913d 73 74 20 7b 0a 20 20 69 6e 74 20 6e 45 78 74 3b  st {.  int nExt;
2913e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2913f 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
29140 65 73 20 69 6e 20 61 45 78 74 5b 5d 20 2a 2f 20  es in aExt[] */ 
29141 20 20 20 20 20 20 20 20 20 0a 20 20 76 6f 69 64           .  void
29142 20 28 2a 2a 61 45 78 74 29 28 76 6f 69 64 29 3b   (**aExt)(void);
29143 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74     /* Pointers t
29144 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  o the extension 
29145 69 6e 69 74 20 66 75 6e 63 74 69 6f 6e 73 20 2a  init functions *
29146 2f 0a 7d 20 73 71 6c 69 74 65 33 41 75 74 6f 65  /.} sqlite3Autoe
29147 78 74 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a  xt = { 0, 0 };..
29148 2f 2a 20 54 68 65 20 22 77 73 64 41 75 74 6f 65  /* The "wsdAutoe
29149 78 74 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72  xt" macro will r
2914a 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 61 75  esolve to the au
2914b 74 6f 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 73  toextension.** s
2914c 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66  tate vector.  If
2914d 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63   writable static
2914e 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f   data is unsuppo
2914f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67  rted on the targ
29150 65 74 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74  et,.** we have t
29151 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61  o locate the sta
29152 74 65 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e  te vector at run
29153 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d  -time.  In the m
29154 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61  ore common.** ca
29155 73 65 20 77 68 65 72 65 20 77 72 69 74 61 62 6c  se where writabl
29156 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73  e static data is
29157 20 73 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53   supported, wsdS
29158 74 61 74 20 63 61 6e 20 72 65 66 65 72 20 64 69  tat can refer di
29159 72 65 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65  rectly.** to the
2915a 20 22 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74   "sqlite3Autoext
2915b 22 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 64  " state vector d
2915c 65 63 6c 61 72 65 64 20 61 62 6f 76 65 2e 0a 2a  eclared above..*
2915d 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
2915e 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65 66 69 6e  OMIT_WSD.# defin
2915f 65 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74  e wsdAutoextInit
29160 20 5c 0a 20 20 73 71 6c 69 74 65 33 41 75 74 6f   \.  sqlite3Auto
29161 45 78 74 4c 69 73 74 20 2a 78 20 3d 20 26 47 4c  ExtList *x = &GL
29162 4f 42 41 4c 28 73 71 6c 69 74 65 33 41 75 74 6f  OBAL(sqlite3Auto
29163 45 78 74 4c 69 73 74 2c 73 71 6c 69 74 65 33 41  ExtList,sqlite3A
29164 75 74 6f 65 78 74 29 0a 23 20 64 65 66 69 6e 65  utoext).# define
29165 20 77 73 64 41 75 74 6f 65 78 74 20 78 5b 30 5d   wsdAutoext x[0]
29166 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
29167 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 0a 23  wsdAutoextInit.#
29168 20 64 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65   define wsdAutoe
29169 78 74 20 73 71 6c 69 74 65 33 41 75 74 6f 65 78  xt sqlite3Autoex
2916a 74 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  t.#endif.../*.**
2916b 20 52 65 67 69 73 74 65 72 20 61 20 73 74 61 74   Register a stat
2916c 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 78  ically linked ex
2916d 74 65 6e 73 69 6f 6e 20 74 68 61 74 20 69 73 20  tension that is 
2916e 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a  automatically.**
2916f 20 6c 6f 61 64 65 64 20 62 79 20 65 76 65 72 79   loaded by every
29170 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f   new database co
29171 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nnection..*/.SQL
29172 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
29173 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69  te3_auto_extensi
29174 6f 6e 28 76 6f 69 64 20 28 2a 78 49 6e 69 74 29  on(void (*xInit)
29175 28 76 6f 69 64 29 29 7b 0a 20 20 69 6e 74 20 72  (void)){.  int r
29176 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23  c = SQLITE_OK;.#
29177 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
29178 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63  IT_AUTOINIT.  rc
29179 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69   = sqlite3_initi
2917a 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72  alize();.  if( r
2917b 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
2917c 72 63 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64  rc;.  }else.#end
2917d 69 66 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69  if.  {.    int i
2917e 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ;.#if SQLITE_THR
2917f 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69  EADSAFE.    sqli
29180 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
29181 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
29182 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
29183 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
29184 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 77 73 64  ;.#endif.    wsd
29185 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 20  AutoextInit;.   
29186 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
29187 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20  nter(mutex);.   
29188 20 66 6f 72 28 69 3d 30 3b 20 69 3c 77 73 64 41   for(i=0; i<wsdA
29189 75 74 6f 65 78 74 2e 6e 45 78 74 3b 20 69 2b 2b  utoext.nExt; i++
2918a 29 7b 0a 20 20 20 20 20 20 69 66 28 20 77 73 64  ){.      if( wsd
2918b 41 75 74 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3d  Autoext.aExt[i]=
2918c 3d 78 49 6e 69 74 20 29 20 62 72 65 61 6b 3b 0a  =xInit ) break;.
2918d 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3d      }.    if( i=
2918e 3d 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74  =wsdAutoext.nExt
2918f 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 42   ){.      int nB
29190 79 74 65 20 3d 20 28 77 73 64 41 75 74 6f 65 78  yte = (wsdAutoex
29191 74 2e 6e 45 78 74 2b 31 29 2a 73 69 7a 65 6f 66  t.nExt+1)*sizeof
29192 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74  (wsdAutoext.aExt
29193 5b 30 5d 29 3b 0a 20 20 20 20 20 20 76 6f 69 64  [0]);.      void
29194 20 28 2a 2a 61 4e 65 77 29 28 76 6f 69 64 29 3b   (**aNew)(void);
29195 0a 20 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71  .      aNew = sq
29196 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 77 73  lite3_realloc(ws
29197 64 41 75 74 6f 65 78 74 2e 61 45 78 74 2c 20 6e  dAutoext.aExt, n
29198 42 79 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Byte);.      if(
29199 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   aNew==0 ){.    
2919a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
2919b 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c  NOMEM;.      }el
2919c 73 65 7b 0a 20 20 20 20 20 20 20 20 77 73 64 41  se{.        wsdA
2919d 75 74 6f 65 78 74 2e 61 45 78 74 20 3d 20 61 4e  utoext.aExt = aN
2919e 65 77 3b 0a 20 20 20 20 20 20 20 20 77 73 64 41  ew;.        wsdA
2919f 75 74 6f 65 78 74 2e 61 45 78 74 5b 77 73 64 41  utoext.aExt[wsdA
291a0 75 74 6f 65 78 74 2e 6e 45 78 74 5d 20 3d 20 78  utoext.nExt] = x
291a1 49 6e 69 74 3b 0a 20 20 20 20 20 20 20 20 77 73  Init;.        ws
291a2 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 2b 2b 3b  dAutoext.nExt++;
291a3 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
291a4 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
291a5 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20  _leave(mutex);. 
291a6 20 20 20 61 73 73 65 72 74 28 20 28 72 63 26 30     assert( (rc&0
291a7 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20 20 20  xff)==rc );.    
291a8 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d  return rc;.  }.}
291a9 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68  ../*.** Reset th
291aa 65 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  e automatic exte
291ab 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d 65  nsion loading me
291ac 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 53 51 4c 49  chanism..*/.SQLI
291ad 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
291ae 74 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65  te3_reset_auto_e
291af 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 7b 0a  xtension(void){.
291b0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
291b1 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69  MIT_AUTOINIT.  i
291b2 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  f( sqlite3_initi
291b3 61 6c 69 7a 65 28 29 3d 3d 53 51 4c 49 54 45 5f  alize()==SQLITE_
291b4 4f 4b 20 29 0a 23 65 6e 64 69 66 0a 20 20 7b 0a  OK ).#endif.  {.
291b5 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
291b6 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65  DSAFE.    sqlite
291b7 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d  3_mutex *mutex =
291b8 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
291b9 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
291ba 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
291bb 23 65 6e 64 69 66 0a 20 20 20 20 77 73 64 41 75  #endif.    wsdAu
291bc 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 20 20 73  toextInit;.    s
291bd 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
291be 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 73  er(mutex);.    s
291bf 71 6c 69 74 65 33 5f 66 72 65 65 28 77 73 64 41  qlite3_free(wsdA
291c0 75 74 6f 65 78 74 2e 61 45 78 74 29 3b 0a 20 20  utoext.aExt);.  
291c1 20 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78    wsdAutoext.aEx
291c2 74 20 3d 20 30 3b 0a 20 20 20 20 77 73 64 41 75  t = 0;.    wsdAu
291c3 74 6f 65 78 74 2e 6e 45 78 74 20 3d 20 30 3b 0a  toext.nExt = 0;.
291c4 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
291c5 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
291c6 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61    }.}../*.** Loa
291c7 64 20 61 6c 6c 20 61 75 74 6f 6d 61 74 69 63 20  d all automatic 
291c8 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a  extensions..**.*
291c9 2a 20 49 66 20 61 6e 79 74 68 69 6e 67 20 67 6f  * If anything go
291ca 65 73 20 77 72 6f 6e 67 2c 20 73 65 74 20 61 6e  es wrong, set an
291cb 20 65 72 72 6f 72 20 69 6e 20 74 68 65 20 64 61   error in the da
291cc 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
291cd 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
291ce 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
291cf 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69  3AutoLoadExtensi
291d0 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ons(sqlite3 *db)
291d1 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
291d2 20 67 6f 20 3d 20 31 3b 0a 20 20 69 6e 74 20 28   go = 1;.  int (
291d3 2a 78 49 6e 69 74 29 28 73 71 6c 69 74 65 33 2a  *xInit)(sqlite3*
291d4 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71  ,char**,const sq
291d5 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
291d6 65 73 2a 29 3b 0a 0a 20 20 77 73 64 41 75 74 6f  es*);..  wsdAuto
291d7 65 78 74 49 6e 69 74 3b 0a 20 20 69 66 28 20 77  extInit;.  if( w
291d8 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 3d 3d  sdAutoext.nExt==
291d9 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d 6d  0 ){.    /* Comm
291da 6f 6e 20 63 61 73 65 3a 20 65 61 72 6c 79 20 6f  on case: early o
291db 75 74 20 77 69 74 68 6f 75 74 20 65 76 65 72 79  ut without every
291dc 20 68 61 76 69 6e 67 20 74 6f 20 61 63 71 75 69   having to acqui
291dd 72 65 20 61 20 6d 75 74 65 78 20 2a 2f 0a 20 20  re a mutex */.  
291de 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
291df 66 6f 72 28 69 3d 30 3b 20 67 6f 3b 20 69 2b 2b  for(i=0; go; i++
291e0 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72  ){.    char *zEr
291e1 72 6d 73 67 3b 0a 23 69 66 20 53 51 4c 49 54 45  rmsg;.#if SQLITE
291e2 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20  _THREADSAFE.    
291e3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
291e4 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75  utex = sqlite3Mu
291e5 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
291e6 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
291e7 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  TER);.#endif.   
291e8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
291e9 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 20  nter(mutex);.   
291ea 20 69 66 28 20 69 3e 3d 77 73 64 41 75 74 6f 65   if( i>=wsdAutoe
291eb 78 74 2e 6e 45 78 74 20 29 7b 0a 20 20 20 20 20  xt.nExt ){.     
291ec 20 78 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20   xInit = 0;.    
291ed 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 65    go = 0;.    }e
291ee 6c 73 65 7b 0a 20 20 20 20 20 20 78 49 6e 69 74  lse{.      xInit
291ef 20 3d 20 28 69 6e 74 28 2a 29 28 73 71 6c 69 74   = (int(*)(sqlit
291f0 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74  e3*,char**,const
291f1 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
291f2 74 69 6e 65 73 2a 29 29 0a 20 20 20 20 20 20 20  tines*)).       
291f3 20 20 20 20 20 20 20 77 73 64 41 75 74 6f 65 78         wsdAutoex
291f4 74 2e 61 45 78 74 5b 69 5d 3b 0a 20 20 20 20 7d  t.aExt[i];.    }
291f5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
291f6 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b  ex_leave(mutex);
291f7 0a 20 20 20 20 7a 45 72 72 6d 73 67 20 3d 20 30  .    zErrmsg = 0
291f8 3b 0a 20 20 20 20 69 66 28 20 78 49 6e 69 74 20  ;.    if( xInit 
291f9 26 26 20 78 49 6e 69 74 28 64 62 2c 20 26 7a 45  && xInit(db, &zE
291fa 72 72 6d 73 67 2c 20 26 73 71 6c 69 74 65 33 41  rrmsg, &sqlite3A
291fb 70 69 73 29 20 29 7b 0a 20 20 20 20 20 20 73 71  pis) ){.      sq
291fc 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
291fd 51 4c 49 54 45 5f 45 52 52 4f 52 2c 0a 20 20 20  QLITE_ERROR,.   
291fe 20 20 20 20 20 20 20 20 20 22 61 75 74 6f 6d 61           "automa
291ff 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f  tic extension lo
29200 61 64 69 6e 67 20 66 61 69 6c 65 64 3a 20 25 73  ading failed: %s
29201 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  ", zErrmsg);.   
29202 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 7d     go = 0;.    }
29203 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
29204 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20 20 7d 0a  e(zErrmsg);.  }.
29205 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
29206 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f 61 64 65 78  ** End of loadex
29207 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
29208 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29209 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2920a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2920b 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 72  ** Begin file pr
2920c 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  agma.c *********
2920d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2920e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2920f 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41 70  */./*.** 2003 Ap
29210 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ril 6.**.** The 
29211 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
29212 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
29213 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
29214 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
29215 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
29216 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
29217 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
29218 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
29219 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2921a 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2921b 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2921c 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2921d 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2921e 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2921f 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
29220 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
29221 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
29222 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29223 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29224 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29225 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29226 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
29227 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
29228 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
29229 74 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f 6d  t the PRAGMA com
2922a 6d 61 6e 64 2e 0a 2a 2f 0a 0a 2f 2a 20 49 67 6e  mand..*/../* Ign
2922b 6f 72 65 20 74 68 69 73 20 77 68 6f 6c 65 20 66  ore this whole f
2922c 69 6c 65 20 69 66 20 70 72 61 67 6d 61 73 20 61  ile if pragmas a
2922d 72 65 20 64 69 73 61 62 6c 65 64 0a 2a 2f 0a 23  re disabled.*/.#
2922e 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
2922f 54 45 5f 4f 4d 49 54 5f 50 52 41 47 4d 41 29 0a  TE_OMIT_PRAGMA).
29230 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74  ./*.** Interpret
29231 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e   the given strin
29232 67 20 61 73 20 61 20 73 61 66 65 74 79 20 6c 65  g as a safety le
29233 76 65 6c 2e 20 20 52 65 74 75 72 6e 20 30 20 66  vel.  Return 0 f
29234 6f 72 20 4f 46 46 2c 0a 2a 2a 20 31 20 66 6f 72  or OFF,.** 1 for
29235 20 4f 4e 20 6f 72 20 4e 4f 52 4d 41 4c 20 61 6e   ON or NORMAL an
29236 64 20 32 20 66 6f 72 20 46 55 4c 4c 2e 20 20 52  d 2 for FULL.  R
29237 65 74 75 72 6e 20 31 20 66 6f 72 20 61 6e 20 65  eturn 1 for an e
29238 6d 70 74 79 20 6f 72 20 0a 2a 2a 20 75 6e 72 65  mpty or .** unre
29239 63 6f 67 6e 69 7a 65 64 20 73 74 72 69 6e 67 20  cognized string 
2923a 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
2923b 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 76 61  Note that the va
2923c 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 61 72  lues returned ar
2923d 65 20 6f 6e 65 20 6c 65 73 73 20 74 68 61 74 20  e one less that 
2923e 74 68 65 20 76 61 6c 75 65 73 20 74 68 61 74 0a  the values that.
2923f 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73  ** should be pas
29240 73 65 64 20 69 6e 74 6f 20 73 71 6c 69 74 65 33  sed into sqlite3
29241 42 74 72 65 65 53 65 74 53 61 66 65 74 79 4c 65  BtreeSetSafetyLe
29242 76 65 6c 28 29 2e 20 20 54 68 65 20 69 73 20 64  vel().  The is d
29243 6f 6e 65 0a 2a 2a 20 74 6f 20 73 75 70 70 6f 72  one.** to suppor
29244 74 20 6c 65 67 61 63 79 20 53 51 4c 20 63 6f 64  t legacy SQL cod
29245 65 2e 20 20 54 68 65 20 73 61 66 65 74 79 20 6c  e.  The safety l
29246 65 76 65 6c 20 75 73 65 64 20 74 6f 20 62 65 20  evel used to be 
29247 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 61 6e 64 20 6f  boolean.** and o
29248 6c 64 65 72 20 73 63 72 69 70 74 73 20 6d 61 79  lder scripts may
29249 20 68 61 76 65 20 75 73 65 64 20 6e 75 6d 62 65   have used numbe
2924a 72 73 20 30 20 66 6f 72 20 4f 46 46 20 61 6e 64  rs 0 for OFF and
2924b 20 31 20 66 6f 72 20 4f 4e 2e 0a 2a 2f 0a 73 74   1 for ON..*/.st
2924c 61 74 69 63 20 75 38 20 67 65 74 53 61 66 65 74  atic u8 getSafet
2924d 79 4c 65 76 65 6c 28 63 6f 6e 73 74 20 63 68 61  yLevel(const cha
2924e 72 20 2a 7a 29 7b 0a 20 20 20 20 20 20 20 20 20  r *z){.         
2924f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29250 20 20 20 20 2f 2a 20 31 32 33 34 35 36 37 38 39      /* 123456789
29251 20 31 32 33 34 35 36 37 38 39 20 2a 2f 0a 20 20   123456789 */.  
29252 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
29253 72 20 7a 54 65 78 74 5b 5d 20 3d 20 22 6f 6e 6f  r zText[] = "ono
29254 66 66 61 6c 73 65 79 65 73 74 72 75 65 66 75 6c  ffalseyestrueful
29255 6c 22 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  l";.  static con
29256 73 74 20 75 38 20 69 4f 66 66 73 65 74 5b 5d 20  st u8 iOffset[] 
29257 3d 20 7b 30 2c 20 31 2c 20 32 2c 20 34 2c 20 39  = {0, 1, 2, 4, 9
29258 2c 20 31 32 2c 20 31 36 7d 3b 0a 20 20 73 74 61  , 12, 16};.  sta
29259 74 69 63 20 63 6f 6e 73 74 20 75 38 20 69 4c 65  tic const u8 iLe
2925a 6e 67 74 68 5b 5d 20 3d 20 7b 32 2c 20 32 2c 20  ngth[] = {2, 2, 
2925b 33 2c 20 35 2c 20 33 2c 20 34 2c 20 34 7d 3b 0a  3, 5, 3, 4, 4};.
2925c 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2925d 38 20 69 56 61 6c 75 65 5b 5d 20 3d 20 20 7b 31  8 iValue[] =  {1
2925e 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 31 2c  , 0, 0, 0, 1, 1,
2925f 20 32 7d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b   2};.  int i, n;
29260 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73  .  if( sqlite3Is
29261 64 69 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20  digit(*z) ){.   
29262 20 72 65 74 75 72 6e 20 28 75 38 29 61 74 6f 69   return (u8)atoi
29263 28 7a 29 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 73  (z);.  }.  n = s
29264 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
29265 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
29266 41 72 72 61 79 53 69 7a 65 28 69 4c 65 6e 67 74  ArraySize(iLengt
29267 68 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  h); i++){.    if
29268 28 20 69 4c 65 6e 67 74 68 5b 69 5d 3d 3d 6e 20  ( iLength[i]==n 
29269 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
2926a 6d 70 28 26 7a 54 65 78 74 5b 69 4f 66 66 73 65  mp(&zText[iOffse
2926b 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b  t[i]],z,n)==0 ){
2926c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 56  .      return iV
2926d 61 6c 75 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20  alue[i];.    }. 
2926e 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d   }.  return 1;.}
2926f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  ../*.** Interpre
29270 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  t the given stri
29271 6e 67 20 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20  ng as a boolean 
29272 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  value..*/.static
29273 20 75 38 20 67 65 74 42 6f 6f 6c 65 61 6e 28 63   u8 getBoolean(c
29274 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
29275 20 72 65 74 75 72 6e 20 67 65 74 53 61 66 65 74   return getSafet
29276 79 4c 65 76 65 6c 28 7a 29 26 31 3b 0a 7d 0a 0a  yLevel(z)&1;.}..
29277 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  /*.** Interpret 
29278 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67  the given string
29279 20 61 73 20 61 20 6c 6f 63 6b 69 6e 67 20 6d 6f   as a locking mo
2927a 64 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61  de value..*/.sta
2927b 74 69 63 20 69 6e 74 20 67 65 74 4c 6f 63 6b 69  tic int getLocki
2927c 6e 67 4d 6f 64 65 28 63 6f 6e 73 74 20 63 68 61  ngMode(const cha
2927d 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20 7a 20 29  r *z){.  if( z )
2927e 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c  {.    if( 0==sql
2927f 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22  ite3StrICmp(z, "
29280 65 78 63 6c 75 73 69 76 65 22 29 20 29 20 72 65  exclusive") ) re
29281 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49  turn PAGER_LOCKI
29282 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45  NGMODE_EXCLUSIVE
29283 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c  ;.    if( 0==sql
29284 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22  ite3StrICmp(z, "
29285 6e 6f 72 6d 61 6c 22 29 20 29 20 72 65 74 75 72  normal") ) retur
29286 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  n PAGER_LOCKINGM
29287 4f 44 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d 0a  ODE_NORMAL;.  }.
29288 20 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c    return PAGER_L
29289 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59  OCKINGMODE_QUERY
2928a 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
2928b 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
2928c 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70  UUM./*.** Interp
2928d 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73 74  ret the given st
2928e 72 69 6e 67 20 61 73 20 61 6e 20 61 75 74 6f 2d  ring as an auto-
2928f 76 61 63 75 75 6d 20 6d 6f 64 65 20 76 61 6c 75  vacuum mode valu
29290 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  e..**.** The fol
29291 6c 6f 77 69 6e 67 20 73 74 72 69 6e 67 73 2c 20  lowing strings, 
29292 22 6e 6f 6e 65 22 2c 20 22 66 75 6c 6c 22 20 61  "none", "full" a
29293 6e 64 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22  nd "incremental"
29294 20 61 72 65 20 0a 2a 2a 20 61 63 63 65 70 74 61   are .** accepta
29295 62 6c 65 2c 20 61 73 20 61 72 65 20 74 68 65 69  ble, as are thei
29296 72 20 6e 75 6d 65 72 69 63 20 65 71 75 69 76 61  r numeric equiva
29297 6c 65 6e 74 73 3a 20 30 2c 20 31 20 61 6e 64 20  lents: 0, 1 and 
29298 32 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 0a  2 respectively..
29299 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
2929a 74 41 75 74 6f 56 61 63 75 75 6d 28 63 6f 6e 73  tAutoVacuum(cons
2929b 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 6e  t char *z){.  in
2929c 74 20 69 3b 0a 20 20 69 66 28 20 30 3d 3d 73 71  t i;.  if( 0==sq
2929d 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
2929e 22 6e 6f 6e 65 22 29 20 29 20 72 65 74 75 72 6e  "none") ) return
2929f 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
292a0 4d 5f 4e 4f 4e 45 3b 0a 20 20 69 66 28 20 30 3d  M_NONE;.  if( 0=
292a1 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  =sqlite3StrICmp(
292a2 7a 2c 20 22 66 75 6c 6c 22 29 20 29 20 72 65 74  z, "full") ) ret
292a3 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41  urn BTREE_AUTOVA
292a4 43 55 55 4d 5f 46 55 4c 4c 3b 0a 20 20 69 66 28  CUUM_FULL;.  if(
292a5 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
292a6 6d 70 28 7a 2c 20 22 69 6e 63 72 65 6d 65 6e 74  mp(z, "increment
292a7 61 6c 22 29 20 29 20 72 65 74 75 72 6e 20 42 54  al") ) return BT
292a8 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49  REE_AUTOVACUUM_I
292a9 4e 43 52 3b 0a 20 20 69 20 3d 20 61 74 6f 69 28  NCR;.  i = atoi(
292aa 7a 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 75 38  z);.  return (u8
292ab 29 28 28 69 3e 3d 30 26 26 69 3c 3d 32 29 3f 69  )((i>=0&&i<=2)?i
292ac 3a 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  :0);.}.#endif /*
292ad 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f   ifndef SQLITE_O
292ae 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a  MIT_AUTOVACUUM *
292af 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
292b0 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
292b1 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  GMAS./*.** Inter
292b2 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73  pret the given s
292b3 74 72 69 6e 67 20 61 73 20 61 20 74 65 6d 70 20  tring as a temp 
292b4 64 62 20 6c 6f 63 61 74 69 6f 6e 2e 20 52 65 74  db location. Ret
292b5 75 72 6e 20 31 20 66 6f 72 20 66 69 6c 65 0a 2a  urn 1 for file.*
292b6 2a 20 62 61 63 6b 65 64 20 74 65 6d 70 6f 72 61  * backed tempora
292b7 72 79 20 64 61 74 61 62 61 73 65 73 2c 20 32 20  ry databases, 2 
292b8 66 6f 72 20 74 68 65 20 52 65 64 2d 42 6c 61 63  for the Red-Blac
292b9 6b 20 74 72 65 65 20 69 6e 20 6d 65 6d 6f 72 79  k tree in memory
292ba 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 61 6e 64   database.** and
292bb 20 30 20 74 6f 20 75 73 65 20 74 68 65 20 63 6f   0 to use the co
292bc 6d 70 69 6c 65 2d 74 69 6d 65 20 64 65 66 61 75  mpile-time defau
292bd 6c 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  lt..*/.static in
292be 74 20 67 65 74 54 65 6d 70 53 74 6f 72 65 28 63  t getTempStore(c
292bf 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
292c0 20 69 66 28 20 7a 5b 30 5d 3e 3d 27 30 27 20 26   if( z[0]>='0' &
292c1 26 20 7a 5b 30 5d 3c 3d 27 32 27 20 29 7b 0a 20  & z[0]<='2' ){. 
292c2 20 20 20 72 65 74 75 72 6e 20 7a 5b 30 5d 20 2d     return z[0] -
292c3 20 27 30 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66   '0';.  }else if
292c4 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
292c5 28 7a 2c 20 22 66 69 6c 65 22 29 3d 3d 30 20 29  (z, "file")==0 )
292c6 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
292c7 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
292c8 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 6d  te3StrICmp(z, "m
292c9 65 6d 6f 72 79 22 29 3d 3d 30 20 29 7b 0a 20 20  emory")==0 ){.  
292ca 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 65    return 2;.  }e
292cb 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
292cc 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  0;.  }.}.#endif 
292cd 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45 52 5f  /* SQLITE_PAGER_
292ce 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e  PRAGMAS */..#ifn
292cf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
292d0 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a  PAGER_PRAGMAS./*
292d1 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 74  .** Invalidate t
292d2 65 6d 70 20 73 74 6f 72 61 67 65 2c 20 65 69 74  emp storage, eit
292d3 68 65 72 20 77 68 65 6e 20 74 68 65 20 74 65 6d  her when the tem
292d4 70 20 73 74 6f 72 61 67 65 20 69 73 20 63 68 61  p storage is cha
292d5 6e 67 65 64 0a 2a 2a 20 66 72 6f 6d 20 64 65 66  nged.** from def
292d6 61 75 6c 74 2c 20 6f 72 20 77 68 65 6e 20 27 66  ault, or when 'f
292d7 69 6c 65 27 20 61 6e 64 20 74 68 65 20 74 65 6d  ile' and the tem
292d8 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72  p_store_director
292d9 79 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a 2f  y has changed.*/
292da 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 76 61  .static int inva
292db 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67  lidateTempStorag
292dc 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  e(Parse *pParse)
292dd 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
292de 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
292df 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  if( db->aDb[1].p
292e0 42 74 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  Bt!=0 ){.    if(
292e1 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74   !db->autoCommit
292e2 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65   || sqlite3Btree
292e3 49 73 49 6e 52 65 61 64 54 72 61 6e 73 28 64 62  IsInReadTrans(db
292e4 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 20 29 7b  ->aDb[1].pBt) ){
292e5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
292e6 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
292e7 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67  temporary storag
292e8 65 20 63 61 6e 6e 6f 74 20 62 65 20 63 68 61 6e  e cannot be chan
292e9 67 65 64 20 22 0a 20 20 20 20 20 20 20 20 22 66  ged ".        "f
292ea 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 74 72 61  rom within a tra
292eb 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20  nsaction");.    
292ec 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
292ed 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20  ERROR;.    }.   
292ee 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
292ef 73 65 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42  se(db->aDb[1].pB
292f0 74 29 3b 0a 20 20 20 20 64 62 2d 3e 61 44 62 5b  t);.    db->aDb[
292f1 31 5d 2e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20  1].pBt = 0;.    
292f2 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
292f3 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30  rnalSchema(db, 0
292f4 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
292f5 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
292f6 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41  dif /* SQLITE_PA
292f7 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a  GER_PRAGMAS */..
292f8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
292f9 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
292fa 53 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54  S./*.** If the T
292fb 45 4d 50 20 64 61 74 61 62 61 73 65 20 69 73 20  EMP database is 
292fc 6f 70 65 6e 2c 20 63 6c 6f 73 65 20 69 74 20 61  open, close it a
292fd 6e 64 20 6d 61 72 6b 20 74 68 65 20 64 61 74 61  nd mark the data
292fe 62 61 73 65 20 73 63 68 65 6d 61 0a 2a 2a 20 61  base schema.** a
292ff 73 20 6e 65 65 64 69 6e 67 20 72 65 6c 6f 61 64  s needing reload
29300 69 6e 67 2e 20 20 54 68 69 73 20 6d 75 73 74 20  ing.  This must 
29301 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 75 73 69  be done when usi
29302 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45  ng the SQLITE_TE
29303 4d 50 5f 53 54 4f 52 45 0a 2a 2a 20 6f 72 20 44  MP_STORE.** or D
29304 45 46 41 55 4c 54 5f 54 45 4d 50 5f 53 54 4f 52  EFAULT_TEMP_STOR
29305 45 20 70 72 61 67 6d 61 73 2e 0a 2a 2f 0a 73 74  E pragmas..*/.st
29306 61 74 69 63 20 69 6e 74 20 63 68 61 6e 67 65 54  atic int changeT
29307 65 6d 70 53 74 6f 72 61 67 65 28 50 61 72 73 65  empStorage(Parse
29308 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
29309 63 68 61 72 20 2a 7a 53 74 6f 72 61 67 65 54 79  char *zStorageTy
2930a 70 65 29 7b 0a 20 20 69 6e 74 20 74 73 20 3d 20  pe){.  int ts = 
2930b 67 65 74 54 65 6d 70 53 74 6f 72 65 28 7a 53 74  getTempStore(zSt
2930c 6f 72 61 67 65 54 79 70 65 29 3b 0a 20 20 73 71  orageType);.  sq
2930d 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2930e 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62  se->db;.  if( db
2930f 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d 3d 74 73  ->temp_store==ts
29310 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
29311 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 6e 76 61 6c  _OK;.  if( inval
29312 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65  idateTempStorage
29313 28 20 70 50 61 72 73 65 20 29 20 21 3d 20 53 51  ( pParse ) != SQ
29314 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
29315 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
29316 4f 52 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 74 65  OR;.  }.  db->te
29317 6d 70 5f 73 74 6f 72 65 20 3d 20 28 75 38 29 74  mp_store = (u8)t
29318 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  s;.  return SQLI
29319 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20  TE_OK;.}.#endif 
2931a 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45 52 5f  /* SQLITE_PAGER_
2931b 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a  PRAGMAS */../*.*
2931c 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2931d 74 6f 20 72 65 74 75 72 6e 20 61 20 73 69 6e 67  to return a sing
2931e 6c 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  le integer value
2931f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
29320 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74   returnSingleInt
29321 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
29322 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 61 62  const char *zLab
29323 65 6c 2c 20 69 36 34 20 76 61 6c 75 65 29 7b 0a  el, i64 value){.
29324 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69    Vdbe *v = sqli
29325 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
29326 65 29 3b 0a 20 20 69 6e 74 20 6d 65 6d 20 3d 20  e);.  int mem = 
29327 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
29328 20 20 69 36 34 20 2a 70 49 36 34 20 3d 20 73 71    i64 *pI64 = sq
29329 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
2932a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a  (pParse->db, siz
2932b 65 6f 66 28 76 61 6c 75 65 29 29 3b 0a 20 20 69  eof(value));.  i
2932c 66 28 20 70 49 36 34 20 29 7b 0a 20 20 20 20 6d  f( pI64 ){.    m
2932d 65 6d 63 70 79 28 70 49 36 34 2c 20 26 76 61 6c  emcpy(pI64, &val
2932e 75 65 2c 20 73 69 7a 65 6f 66 28 76 61 6c 75 65  ue, sizeof(value
2932f 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ));.  }.  sqlite
29330 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
29331 50 5f 49 6e 74 36 34 2c 20 30 2c 20 6d 65 6d 2c  P_Int64, 0, mem,
29332 20 30 2c 20 28 63 68 61 72 2a 29 70 49 36 34 2c   0, (char*)pI64,
29333 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 73 71   P4_INT64);.  sq
29334 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
29335 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 73 71 6c  ols(v, 1);.  sql
29336 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
29337 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
29338 5f 4e 41 4d 45 2c 20 7a 4c 61 62 65 6c 2c 20 53  _NAME, zLabel, S
29339 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2933a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2933b 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
2933c 6f 77 2c 20 6d 65 6d 2c 20 31 29 3b 0a 7d 0a 0a  ow, mem, 1);.}..
2933d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2933e 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53  MIT_FLAG_PRAGMAS
2933f 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
29340 73 65 65 20 69 66 20 7a 52 69 67 68 74 20 61 6e  see if zRight an
29341 64 20 7a 4c 65 66 74 20 72 65 66 65 72 20 74 6f  d zLeft refer to
29342 20 61 20 70 72 61 67 6d 61 20 74 68 61 74 20 71   a pragma that q
29343 75 65 72 69 65 73 0a 2a 2a 20 6f 72 20 63 68 61  ueries.** or cha
29344 6e 67 65 73 20 6f 6e 65 20 6f 66 20 74 68 65 20  nges one of the 
29345 66 6c 61 67 73 20 69 6e 20 64 62 2d 3e 66 6c 61  flags in db->fla
29346 67 73 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66  gs.  Return 1 if
29347 20 73 6f 20 61 6e 64 20 30 20 69 66 20 6e 6f 74   so and 0 if not
29348 2e 0a 2a 2a 20 41 6c 73 6f 2c 20 69 6d 70 6c 65  ..** Also, imple
29349 6d 65 6e 74 20 74 68 65 20 70 72 61 67 6d 61 2e  ment the pragma.
2934a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
2934b 6c 61 67 50 72 61 67 6d 61 28 50 61 72 73 65 20  lagPragma(Parse 
2934c 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63  *pParse, const c
2934d 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73  har *zLeft, cons
2934e 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29 7b  t char *zRight){
2934f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
29350 73 74 72 75 63 74 20 73 50 72 61 67 6d 61 54 79  struct sPragmaTy
29351 70 65 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  pe {.    const c
29352 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 2f 2a 20  har *zName;  /* 
29353 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72 61 67  Name of the prag
29354 6d 61 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d 61  ma */.    int ma
29355 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  sk;           /*
29356 20 4d 61 73 6b 20 66 6f 72 20 74 68 65 20 64 62   Mask for the db
29357 2d 3e 66 6c 61 67 73 20 76 61 6c 75 65 20 2a 2f  ->flags value */
29358 0a 20 20 7d 20 61 50 72 61 67 6d 61 5b 5d 20 3d  .  } aPragma[] =
29359 20 7b 0a 20 20 20 20 7b 20 22 66 75 6c 6c 5f 63   {.    { "full_c
2935a 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20 20  olumn_names",   
2935b 20 20 20 20 20 53 51 4c 49 54 45 5f 46 75 6c 6c       SQLITE_Full
2935c 43 6f 6c 4e 61 6d 65 73 20 20 7d 2c 0a 20 20 20  ColNames  },.   
2935d 20 7b 20 22 73 68 6f 72 74 5f 63 6f 6c 75 6d 6e   { "short_column
2935e 5f 6e 61 6d 65 73 22 2c 20 20 20 20 20 20 20 53  _names",       S
2935f 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61  QLITE_ShortColNa
29360 6d 65 73 20 7d 2c 0a 20 20 20 20 7b 20 22 63 6f  mes },.    { "co
29361 75 6e 74 5f 63 68 61 6e 67 65 73 22 2c 20 20 20  unt_changes",   
29362 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
29363 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 7d 2c  CountRows     },
29364 0a 20 20 20 20 7b 20 22 65 6d 70 74 79 5f 72 65  .    { "empty_re
29365 73 75 6c 74 5f 63 61 6c 6c 62 61 63 6b 73 22 2c  sult_callbacks",
29366 20 20 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61     SQLITE_NullCa
29367 6c 6c 62 61 63 6b 20 20 7d 2c 0a 20 20 20 20 7b  llback  },.    {
29368 20 22 6c 65 67 61 63 79 5f 66 69 6c 65 5f 66 6f   "legacy_file_fo
29369 72 6d 61 74 22 2c 20 20 20 20 20 20 20 53 51 4c  rmat",       SQL
2936a 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46 6d  ITE_LegacyFileFm
2936b 74 20 7d 2c 0a 20 20 20 20 7b 20 22 66 75 6c 6c  t },.    { "full
2936c 66 73 79 6e 63 22 2c 20 20 20 20 20 20 20 20 20  fsync",         
2936d 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46 75         SQLITE_Fu
2936e 6c 6c 46 53 79 6e 63 20 20 20 20 20 7d 2c 0a 20  llFSync     },. 
2936f 20 20 20 7b 20 22 72 65 76 65 72 73 65 5f 75 6e     { "reverse_un
29370 6f 72 64 65 72 65 64 5f 73 65 6c 65 63 74 73 22  ordered_selects"
29371 2c 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73 65  , SQLITE_Reverse
29372 4f 72 64 65 72 20 20 7d 2c 0a 23 69 66 64 65 66  Order  },.#ifdef
29373 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20   SQLITE_DEBUG.  
29374 20 20 7b 20 22 73 71 6c 5f 74 72 61 63 65 22 2c    { "sql_trace",
29375 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29376 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20  SQLITE_SqlTrace 
29377 20 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76       },.    { "v
29378 64 62 65 5f 6c 69 73 74 69 6e 67 22 2c 20 20 20  dbe_listing",   
29379 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2937a 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20 7d  _VdbeListing   }
2937b 2c 0a 20 20 20 20 7b 20 22 76 64 62 65 5f 74 72  ,.    { "vdbe_tr
2937c 61 63 65 22 2c 20 20 20 20 20 20 20 20 20 20 20  ace",           
2937d 20 20 20 20 53 51 4c 49 54 45 5f 56 64 62 65 54      SQLITE_VdbeT
2937e 72 61 63 65 20 20 20 20 20 7d 2c 0a 23 65 6e 64  race     },.#end
2937f 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
29380 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20  E_OMIT_CHECK.   
29381 20 7b 20 22 69 67 6e 6f 72 65 5f 63 68 65 63 6b   { "ignore_check
29382 5f 63 6f 6e 73 74 72 61 69 6e 74 73 22 2c 20 53  _constraints", S
29383 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63  QLITE_IgnoreChec
29384 6b 73 20 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20  ks  },.#endif.  
29385 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
29386 6e 67 20 69 73 20 56 45 52 59 20 65 78 70 65 72  ng is VERY exper
29387 69 6d 65 6e 74 61 6c 20 2a 2f 0a 20 20 20 20 7b  imental */.    {
29388 20 22 77 72 69 74 61 62 6c 65 5f 73 63 68 65 6d   "writable_schem
29389 61 22 2c 20 20 20 20 20 20 20 20 20 20 53 51 4c  a",          SQL
2938a 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 7c  ITE_WriteSchema|
2938b 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d  SQLITE_RecoveryM
2938c 6f 64 65 20 7d 2c 0a 20 20 20 20 7b 20 22 6f 6d  ode },.    { "om
2938d 69 74 5f 72 65 61 64 6c 6f 63 6b 22 2c 20 20 20  it_readlock",   
2938e 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
2938f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 7d 2c  NoReadlock    },
29390 0a 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 4d  ..    /* TODO: M
29391 61 79 62 65 20 69 74 20 73 68 6f 75 6c 64 6e 27  aybe it shouldn'
29392 74 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f  t be possible to
29393 20 63 68 61 6e 67 65 20 74 68 65 20 52 65 61 64   change the Read
29394 55 6e 63 6f 6d 6d 69 74 74 65 64 0a 20 20 20 20  Uncommitted.    
29395 2a 2a 20 66 6c 61 67 20 69 66 20 74 68 65 72 65  ** flag if there
29396 20 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 20   are any active 
29397 73 74 61 74 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20  statements. */. 
29398 20 20 20 7b 20 22 72 65 61 64 5f 75 6e 63 6f 6d     { "read_uncom
29399 6d 69 74 74 65 64 22 2c 20 20 20 20 20 20 20 20  mitted",        
2939a 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f   SQLITE_ReadUnco
2939b 6d 6d 69 74 74 65 64 20 7d 2c 0a 20 20 20 20 7b  mmitted },.    {
2939c 20 22 72 65 63 75 72 73 69 76 65 5f 74 72 69 67   "recursive_trig
2939d 67 65 72 73 22 2c 20 20 20 20 20 20 20 53 51 4c  gers",       SQL
2939e 49 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 20  ITE_RecTriggers 
2939f 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  },..    /* This 
293a0 66 6c 61 67 20 6d 61 79 20 6f 6e 6c 79 20 62 65  flag may only be
293a1 20 73 65 74 20 69 66 20 62 6f 74 68 20 66 6f 72   set if both for
293a2 65 69 67 6e 2d 6b 65 79 20 61 6e 64 20 74 72 69  eign-key and tri
293a3 67 67 65 72 20 73 75 70 70 6f 72 74 0a 20 20 20  gger support.   
293a4 20 2a 2a 20 61 72 65 20 70 72 65 73 65 6e 74 20   ** are present 
293a5 69 6e 20 74 68 65 20 62 75 69 6c 64 2e 20 20 2a  in the build.  *
293a6 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
293a7 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
293a8 47 4e 5f 4b 45 59 29 20 26 26 20 21 64 65 66 69  GN_KEY) && !defi
293a9 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
293aa 54 52 49 47 47 45 52 29 0a 20 20 20 20 7b 20 22  TRIGGER).    { "
293ab 66 6f 72 65 69 67 6e 5f 6b 65 79 73 22 2c 20 20  foreign_keys",  
293ac 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
293ad 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 7d 2c  E_ForeignKeys },
293ae 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20 69  .#endif.  };.  i
293af 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 73 74  nt i;.  const st
293b0 72 75 63 74 20 73 50 72 61 67 6d 61 54 79 70 65  ruct sPragmaType
293b1 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20   *p;.  for(i=0, 
293b2 70 3d 61 50 72 61 67 6d 61 3b 20 69 3c 41 72 72  p=aPragma; i<Arr
293b3 61 79 53 69 7a 65 28 61 50 72 61 67 6d 61 29 3b  aySize(aPragma);
293b4 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20   i++, p++){.    
293b5 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
293b6 6d 70 28 7a 4c 65 66 74 2c 20 70 2d 3e 7a 4e 61  mp(zLeft, p->zNa
293b7 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
293b8 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
293b9 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 20 20  arse->db;.      
293ba 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 20 20 76  Vdbe *v;.      v
293bb 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
293bc 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  e(pParse);.     
293bd 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
293be 20 20 2f 2a 20 41 6c 72 65 61 64 79 20 61 6c 6c    /* Already all
293bf 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69 74 65  ocated by sqlite
293c0 33 50 72 61 67 6d 61 28 29 20 2a 2f 0a 20 20 20  3Pragma() */.   
293c1 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 76 29     if( ALWAYS(v)
293c2 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
293c3 7a 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20  zRight==0 ){.   
293c4 20 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e         returnSin
293c5 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 70  gleInt(pParse, p
293c6 2d 3e 7a 4e 61 6d 65 2c 20 28 64 62 2d 3e 66 6c  ->zName, (db->fl
293c7 61 67 73 20 26 20 70 2d 3e 6d 61 73 6b 29 21 3d  ags & p->mask)!=
293c8 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  0 );.        }el
293c9 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  se{.          in
293ca 74 20 6d 61 73 6b 20 3d 20 70 2d 3e 6d 61 73 6b  t mask = p->mask
293cb 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  ;          /* Ma
293cc 73 6b 20 6f 66 20 62 69 74 73 20 74 6f 20 73 65  sk of bits to se
293cd 74 20 6f 72 20 63 6c 65 61 72 2e 20 2a 2f 0a 20  t or clear. */. 
293ce 20 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d           if( db-
293cf 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29  >autoCommit==0 )
293d0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {.            /*
293d1 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 73 75 70   Foreign key sup
293d2 70 6f 72 74 20 6d 61 79 20 6e 6f 74 20 62 65 20  port may not be 
293d3 65 6e 61 62 6c 65 64 20 6f 72 20 64 69 73 61 62  enabled or disab
293d4 6c 65 64 20 77 68 69 6c 65 20 6e 6f 74 0a 20 20  led while not.  
293d5 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20            ** in 
293d6 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 6d 6f 64 65  auto-commit mode
293d7 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  .  */.          
293d8 20 20 6d 61 73 6b 20 26 3d 20 7e 28 53 51 4c 49    mask &= ~(SQLI
293d9 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 29 3b  TE_ForeignKeys);
293da 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  .          }..  
293db 20 20 20 20 20 20 20 20 69 66 28 20 67 65 74 42          if( getB
293dc 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74 29 20 29  oolean(zRight) )
293dd 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62  {.            db
293de 2d 3e 66 6c 61 67 73 20 7c 3d 20 6d 61 73 6b 3b  ->flags |= mask;
293df 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
293e0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62  {.            db
293e1 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 6d 61 73 6b  ->flags &= ~mask
293e2 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20  ;.          }.. 
293e3 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6e 79           /* Many
293e4 20 6f 66 20 74 68 65 20 66 6c 61 67 2d 70 72 61   of the flag-pra
293e5 67 6d 61 73 20 6d 6f 64 69 66 79 20 74 68 65 20  gmas modify the 
293e6 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62  code generated b
293e7 79 20 74 68 65 20 53 51 4c 20 0a 20 20 20 20 20  y the SQL .     
293e8 20 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 72       ** compiler
293e9 20 28 65 67 2e 20 63 6f 75 6e 74 5f 63 68 61 6e   (eg. count_chan
293ea 67 65 73 29 2e 20 53 6f 20 61 64 64 20 61 6e 20  ges). So add an 
293eb 6f 70 63 6f 64 65 20 74 6f 20 65 78 70 69 72 65  opcode to expire
293ec 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 2a   all.          *
293ed 2a 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73  * compiled SQL s
293ee 74 61 74 65 6d 65 6e 74 73 20 61 66 74 65 72 20  tatements after 
293ef 6d 6f 64 69 66 79 69 6e 67 20 61 20 70 72 61 67  modifying a prag
293f0 6d 61 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20  ma value..      
293f1 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
293f2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
293f3 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c  p2(v, OP_Expire,
293f4 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
293f5 7d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  }.      }..     
293f6 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
293f7 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
293f8 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
293f9 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52  ITE_OMIT_FLAG_PR
293fa 41 47 4d 41 53 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  AGMAS */../*.** 
293fb 52 65 74 75 72 6e 20 61 20 68 75 6d 61 6e 2d 72  Return a human-r
293fc 65 61 64 61 62 6c 65 20 6e 61 6d 65 20 66 6f 72  eadable name for
293fd 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65   a constraint re
293fe 73 6f 6c 75 74 69 6f 6e 20 61 63 74 69 6f 6e 2e  solution action.
293ff 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
29400 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
29401 4b 45 59 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  KEY.static const
29402 20 63 68 61 72 20 2a 61 63 74 69 6f 6e 4e 61 6d   char *actionNam
29403 65 28 75 38 20 61 63 74 69 6f 6e 29 7b 0a 20 20  e(u8 action){.  
29404 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
29405 65 3b 0a 20 20 73 77 69 74 63 68 28 20 61 63 74  e;.  switch( act
29406 69 6f 6e 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ion ){.    case 
29407 4f 45 5f 53 65 74 4e 75 6c 6c 3a 20 20 7a 4e 61  OE_SetNull:  zNa
29408 6d 65 20 3d 20 22 53 45 54 20 4e 55 4c 4c 22 3b  me = "SET NULL";
29409 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2940a 20 20 20 63 61 73 65 20 4f 45 5f 53 65 74 44 66     case OE_SetDf
2940b 6c 74 3a 20 20 7a 4e 61 6d 65 20 3d 20 22 53 45  lt:  zName = "SE
2940c 54 20 44 45 46 41 55 4c 54 22 3b 20 20 20 20 20  T DEFAULT";     
2940d 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
2940e 4f 45 5f 43 61 73 63 61 64 65 3a 20 20 7a 4e 61  OE_Cascade:  zNa
2940f 6d 65 20 3d 20 22 43 41 53 43 41 44 45 22 3b 20  me = "CASCADE"; 
29410 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
29411 20 20 20 63 61 73 65 20 4f 45 5f 52 65 73 74 72     case OE_Restr
29412 69 63 74 3a 20 7a 4e 61 6d 65 20 3d 20 22 52 45  ict: zName = "RE
29413 53 54 52 49 43 54 22 3b 20 20 20 20 20 20 20 20  STRICT";        
29414 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75  break;.    defau
29415 6c 74 3a 20 20 20 20 20 20 20 20 20 20 7a 4e 61  lt:          zNa
29416 6d 65 20 3d 20 22 4e 4f 20 41 43 54 49 4f 4e 22  me = "NO ACTION"
29417 3b 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  ;  .            
29418 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
29419 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f 4e 6f 6e  ( action==OE_Non
2941a 65 20 29 3b 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  e ); break;.  }.
2941b 20 20 72 65 74 75 72 6e 20 7a 4e 61 6d 65 3b 0a    return zName;.
2941c 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
2941d 50 72 6f 63 65 73 73 20 61 20 70 72 61 67 6d 61  Process a pragma
2941e 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a   statement.  .**
2941f 0a 2a 2a 20 50 72 61 67 6d 61 73 20 61 72 65 20  .** Pragmas are 
29420 6f 66 20 74 68 69 73 20 66 6f 72 6d 3a 0a 2a 2a  of this form:.**
29421 0a 2a 2a 20 20 20 20 20 20 50 52 41 47 4d 41 20  .**      PRAGMA 
29422 5b 64 61 74 61 62 61 73 65 2e 5d 69 64 20 5b 3d  [database.]id [=
29423 20 76 61 6c 75 65 5d 0a 2a 2a 0a 2a 2a 20 54 68   value].**.** Th
29424 65 20 69 64 65 6e 74 69 66 69 65 72 20 6d 69 67  e identifier mig
29425 68 74 20 61 6c 73 6f 20 62 65 20 61 20 73 74 72  ht also be a str
29426 69 6e 67 2e 20 20 54 68 65 20 76 61 6c 75 65 20  ing.  The value 
29427 69 73 20 61 20 73 74 72 69 6e 67 2c 20 61 6e 64  is a string, and
29428 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65 72 2c 20  .** identifier, 
29429 6f 72 20 61 20 6e 75 6d 62 65 72 2e 20 20 49 66  or a number.  If
2942a 20 6d 69 6e 75 73 46 6c 61 67 20 69 73 20 74 72   minusFlag is tr
2942b 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  ue, then the val
2942c 75 65 20 69 73 0a 2a 2a 20 61 20 6e 75 6d 62 65  ue is.** a numbe
2942d 72 20 74 68 61 74 20 77 61 73 20 70 72 65 63 65  r that was prece
2942e 64 65 64 20 62 79 20 61 20 6d 69 6e 75 73 20 73  ded by a minus s
2942f 69 67 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ign..**.** If th
29430 65 20 6c 65 66 74 20 73 69 64 65 20 69 73 20 22  e left side is "
29431 64 61 74 61 62 61 73 65 2e 69 64 22 20 74 68 65  database.id" the
29432 6e 20 70 49 64 31 20 69 73 20 74 68 65 20 64 61  n pId1 is the da
29433 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 61  tabase name.** a
29434 6e 64 20 70 49 64 32 20 69 73 20 74 68 65 20 69  nd pId2 is the i
29435 64 2e 20 20 49 66 20 74 68 65 20 6c 65 66 74 20  d.  If the left 
29436 73 69 64 65 20 69 73 20 6a 75 73 74 20 22 69 64  side is just "id
29437 22 20 74 68 65 6e 20 70 49 64 31 20 69 73 20 74  " then pId1 is t
29438 68 65 0a 2a 2a 20 69 64 20 61 6e 64 20 70 49 64  he.** id and pId
29439 32 20 69 73 20 61 6e 79 20 65 6d 70 74 79 20 73  2 is any empty s
2943a 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tring..*/.SQLITE
2943b 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2943c 6c 69 74 65 33 50 72 61 67 6d 61 28 0a 20 20 50  lite3Pragma(.  P
2943d 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20  arse *pParse, . 
2943e 20 54 6f 6b 65 6e 20 2a 70 49 64 31 2c 20 20 20   Token *pId1,   
2943f 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61       /* First pa
29440 72 74 20 6f 66 20 5b 64 61 74 61 62 61 73 65 2e  rt of [database.
29441 5d 69 64 20 66 69 65 6c 64 20 2a 2f 0a 20 20 54  ]id field */.  T
29442 6f 6b 65 6e 20 2a 70 49 64 32 2c 20 20 20 20 20  oken *pId2,     
29443 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72     /* Second par
29444 74 20 6f 66 20 5b 64 61 74 61 62 61 73 65 2e 5d  t of [database.]
29445 69 64 20 66 69 65 6c 64 2c 20 6f 72 20 4e 55 4c  id field, or NUL
29446 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 56  L */.  Token *pV
29447 61 6c 75 65 2c 20 20 20 20 20 20 2f 2a 20 54 6f  alue,      /* To
29448 6b 65 6e 20 66 6f 72 20 3c 76 61 6c 75 65 3e 2c  ken for <value>,
29449 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e   or NULL */.  in
2944a 74 20 6d 69 6e 75 73 46 6c 61 67 20 20 20 20 20  t minusFlag     
2944b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 27    /* True if a '
2944c 2d 27 20 73 69 67 6e 20 70 72 65 63 65 64 65 64  -' sign preceded
2944d 20 3c 76 61 6c 75 65 3e 20 2a 2f 0a 29 7b 0a 20   <value> */.){. 
2944e 20 63 68 61 72 20 2a 7a 4c 65 66 74 20 3d 20 30   char *zLeft = 0
2944f 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74  ;       /* Nul-t
29450 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20  erminated UTF-8 
29451 73 74 72 69 6e 67 20 3c 69 64 3e 20 2a 2f 0a 20  string <id> */. 
29452 20 63 68 61 72 20 2a 7a 52 69 67 68 74 20 3d 20   char *zRight = 
29453 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6c 2d 74  0;      /* Nul-t
29454 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20  erminated UTF-8 
29455 73 74 72 69 6e 67 20 3c 76 61 6c 75 65 3e 2c 20  string <value>, 
29456 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e  or NULL */.  con
29457 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 30  st char *zDb = 0
29458 3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  ;   /* The datab
29459 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f  ase name */.  To
2945a 6b 65 6e 20 2a 70 49 64 3b 20 20 20 20 20 20 20  ken *pId;       
2945b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
2945c 74 6f 20 3c 69 64 3e 20 74 6f 6b 65 6e 20 2a 2f  to <id> token */
2945d 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
2945e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2945f 61 62 61 73 65 20 69 6e 64 65 78 20 66 6f 72 20  abase index for 
29460 3c 64 61 74 61 62 61 73 65 3e 20 2a 2f 0a 20 20  <database> */.  
29461 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
29462 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a  arse->db;.  Db *
29463 70 44 62 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d  pDb;.  Vdbe *v =
29464 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 3d   pParse->pVdbe =
29465 20 73 71 6c 69 74 65 33 56 64 62 65 43 72 65 61   sqlite3VdbeCrea
29466 74 65 28 64 62 29 3b 0a 20 20 69 66 28 20 76 3d  te(db);.  if( v=
29467 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  =0 ) return;.  p
29468 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b  Parse->nMem = 2;
29469 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 70 72 65 74  ..  /* Interpret
2946a 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 2e 5d   the [database.]
2946b 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 72 61   part of the pra
2946c 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e 20 69  gma statement. i
2946d 44 62 20 69 73 20 74 68 65 0a 20 20 2a 2a 20 69  Db is the.  ** i
2946e 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
2946f 62 61 73 65 20 74 68 69 73 20 70 72 61 67 6d 61  base this pragma
29470 20 69 73 20 62 65 69 6e 67 20 61 70 70 6c 69 65   is being applie
29471 64 20 74 6f 20 69 6e 20 64 62 2e 61 44 62 5b 5d  d to in db.aDb[]
29472 2e 20 2a 2f 0a 20 20 69 44 62 20 3d 20 73 71 6c  . */.  iDb = sql
29473 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28  ite3TwoPartName(
29474 70 50 61 72 73 65 2c 20 70 49 64 31 2c 20 70 49  pParse, pId1, pI
29475 64 32 2c 20 26 70 49 64 29 3b 0a 20 20 69 66 28  d2, &pId);.  if(
29476 20 69 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b   iDb<0 ) return;
29477 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  .  pDb = &db->aD
29478 62 5b 69 44 62 5d 3b 0a 0a 20 20 2f 2a 20 49 66  b[iDb];..  /* If
29479 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61   the temp databa
2947a 73 65 20 68 61 73 20 62 65 65 6e 20 65 78 70 6c  se has been expl
2947b 69 63 69 74 6c 79 20 6e 61 6d 65 64 20 61 73 20  icitly named as 
2947c 70 61 72 74 20 6f 66 20 74 68 65 20 0a 20 20 2a  part of the .  *
2947d 2a 20 70 72 61 67 6d 61 2c 20 6d 61 6b 65 20 73  * pragma, make s
2947e 75 72 65 20 69 74 20 69 73 20 6f 70 65 6e 2e 20  ure it is open. 
2947f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 44 62 3d  .  */.  if( iDb=
29480 3d 31 20 26 26 20 73 71 6c 69 74 65 33 4f 70 65  =1 && sqlite3Ope
29481 6e 54 65 6d 70 44 61 74 61 62 61 73 65 28 70 50  nTempDatabase(pP
29482 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74  arse) ){.    ret
29483 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 7a 4c 65 66  urn;.  }..  zLef
29484 74 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  t = sqlite3NameF
29485 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 49 64  romToken(db, pId
29486 29 3b 0a 20 20 69 66 28 20 21 7a 4c 65 66 74 20  );.  if( !zLeft 
29487 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
29488 6d 69 6e 75 73 46 6c 61 67 20 29 7b 0a 20 20 20  minusFlag ){.   
29489 20 7a 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65   zRight = sqlite
2948a 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 2d 25  3MPrintf(db, "-%
2948b 54 22 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 7d  T", pValue);.  }
2948c 65 6c 73 65 7b 0a 20 20 20 20 7a 52 69 67 68 74  else{.    zRight
2948d 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
2948e 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 56 61 6c  omToken(db, pVal
2948f 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  ue);.  }..  asse
29490 72 74 28 20 70 49 64 32 20 29 3b 0a 20 20 7a 44  rt( pId2 );.  zD
29491 62 20 3d 20 70 49 64 32 2d 3e 6e 3e 30 20 3f 20  b = pId2->n>0 ? 
29492 70 44 62 2d 3e 7a 4e 61 6d 65 20 3a 20 30 3b 0a  pDb->zName : 0;.
29493 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
29494 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
29495 51 4c 49 54 45 5f 50 52 41 47 4d 41 2c 20 7a 4c  QLITE_PRAGMA, zL
29496 65 66 74 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62  eft, zRight, zDb
29497 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 70 72  ) ){.    goto pr
29498 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 7d 0a 20 0a  agma_out;.  }. .
29499 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2949a 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
2949b 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  S.  /*.  **  PRA
2949c 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 64  GMA [database.]d
2949d 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a  efault_cache_siz
2949e 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  e.  **  PRAGMA [
2949f 64 61 74 61 62 61 73 65 2e 5d 64 65 66 61 75 6c  database.]defaul
294a0 74 5f 63 61 63 68 65 5f 73 69 7a 65 3d 4e 0a 20  t_cache_size=N. 
294a1 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72   **.  ** The fir
294a2 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20  st form reports 
294a3 74 68 65 20 63 75 72 72 65 6e 74 20 70 65 72 73  the current pers
294a4 69 73 74 65 6e 74 20 73 65 74 74 69 6e 67 20 66  istent setting f
294a5 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 67 65  or the.  ** page
294a6 20 63 61 63 68 65 20 73 69 7a 65 2e 20 20 54 68   cache size.  Th
294a7 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
294a8 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   is the maximum 
294a9 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70  number of.  ** p
294aa 61 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65  ages in the page
294ab 20 63 61 63 68 65 2e 20 20 54 68 65 20 73 65 63   cache.  The sec
294ac 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 62 6f  ond form sets bo
294ad 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20  th the current. 
294ae 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 73   ** page cache s
294af 69 7a 65 20 76 61 6c 75 65 20 61 6e 64 20 74 68  ize value and th
294b0 65 20 70 65 72 73 69 73 74 65 6e 74 20 70 61 67  e persistent pag
294b1 65 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c  e cache size val
294b2 75 65 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69  ue.  ** stored i
294b3 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
294b4 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ile..  **.  ** T
294b5 68 65 20 64 65 66 61 75 6c 74 20 63 61 63 68 65  he default cache
294b6 20 73 69 7a 65 20 69 73 20 73 74 6f 72 65 64 20   size is stored 
294b7 69 6e 20 6d 65 74 61 2d 76 61 6c 75 65 20 32 20  in meta-value 2 
294b8 6f 66 20 70 61 67 65 20 31 20 6f 66 20 74 68 65  of page 1 of the
294b9 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66  .  ** database f
294ba 69 6c 65 2e 20 20 54 68 65 20 63 61 63 68 65 20  ile.  The cache 
294bb 73 69 7a 65 20 69 73 20 61 63 74 75 61 6c 6c 79  size is actually
294bc 20 74 68 65 20 61 62 73 6f 6c 75 74 65 20 76 61   the absolute va
294bd 6c 75 65 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73  lue of.  ** this
294be 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
294bf 2e 20 20 54 68 65 20 73 69 67 6e 20 6f 66 20 6d  .  The sign of m
294c0 65 74 61 2d 76 61 6c 75 65 20 32 20 64 65 74 65  eta-value 2 dete
294c1 72 6d 69 6e 65 73 20 74 68 65 0a 20 20 2a 2a 20  rmines the.  ** 
294c2 73 79 6e 63 68 72 6f 6e 6f 75 73 20 73 65 74 74  synchronous sett
294c3 69 6e 67 2e 20 20 41 20 6e 65 67 61 74 69 76 65  ing.  A negative
294c4 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 73 79 6e   value means syn
294c5 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 0a  chronous is off.
294c6 20 20 2a 2a 20 61 6e 64 20 61 20 70 6f 73 69 74    ** and a posit
294c7 69 76 65 20 76 61 6c 75 65 20 6d 65 61 6e 73 20  ive value means 
294c8 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f  synchronous is o
294c9 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  n..  */.  if( sq
294ca 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
294cb 66 74 2c 22 64 65 66 61 75 6c 74 5f 63 61 63 68  ft,"default_cach
294cc 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20  e_size")==0 ){. 
294cd 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
294ce 56 64 62 65 4f 70 4c 69 73 74 20 67 65 74 43 61  VdbeOpList getCa
294cf 63 68 65 53 69 7a 65 5b 5d 20 3d 20 7b 0a 20 20  cheSize[] = {.  
294d0 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63      { OP_Transac
294d1 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 20 20 20 20  tion, 0, 0,     
294d2 20 20 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20     0},          
294d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
294d4 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f  * 0 */.      { O
294d5 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 30  P_ReadCookie,  0
294d6 2c 20 31 2c 20 20 20 20 20 20 20 20 42 54 52 45  , 1,        BTRE
294d7 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f  E_DEFAULT_CACHE_
294d8 53 49 5a 45 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a  SIZE},  /* 1 */.
294d9 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f 73        { OP_IfPos
294da 2c 20 20 20 20 20 20 20 31 2c 20 37 2c 20 20 20  ,       1, 7,   
294db 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b       0},.      {
294dc 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20   OP_Integer,    
294dd 20 30 2c 20 32 2c 20 20 20 20 20 20 20 20 30 7d   0, 2,        0}
294de 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 75 62  ,.      { OP_Sub
294df 74 72 61 63 74 2c 20 20 20 20 31 2c 20 32 2c 20  tract,    1, 2, 
294e0 20 20 20 20 20 20 20 31 7d 2c 0a 20 20 20 20 20         1},.     
294e1 20 7b 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20 20   { OP_IfPos,    
294e2 20 20 20 31 2c 20 37 2c 20 20 20 20 20 20 20 20     1, 7,        
294e3 30 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49  0},.      { OP_I
294e4 6e 74 65 67 65 72 2c 20 20 20 20 20 30 2c 20 31  nteger,     0, 1
294e5 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20  ,        0},    
294e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
294e7 20 20 20 20 20 2f 2a 20 36 20 2a 2f 0a 20 20 20       /* 6 */.   
294e8 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f     { OP_ResultRo
294e9 77 2c 20 20 20 31 2c 20 31 2c 20 20 20 20 20 20  w,   1, 1,      
294ea 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a 20 20 20    0},.    };.   
294eb 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 69   int addr;.    i
294ec 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
294ed 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
294ee 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
294ef 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55      sqlite3VdbeU
294f0 73 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29  sesBtree(v, iDb)
294f1 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68  ;.    if( !zRigh
294f2 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
294f3 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
294f4 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  (v, 1);.      sq
294f5 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
294f6 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
294f7 45 5f 4e 41 4d 45 2c 20 22 63 61 63 68 65 5f 73  E_NAME, "cache_s
294f8 69 7a 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ize", SQLITE_STA
294f9 54 49 43 29 3b 0a 20 20 20 20 20 20 70 50 61 72  TIC);.      pPar
294fa 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 32 3b 0a 20  se->nMem += 2;. 
294fb 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
294fc 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
294fd 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 67 65  (v, ArraySize(ge
294fe 74 43 61 63 68 65 53 69 7a 65 29 2c 20 67 65 74  tCacheSize), get
294ff 43 61 63 68 65 53 69 7a 65 29 3b 0a 20 20 20 20  CacheSize);.    
29500 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
29501 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69  ngeP1(v, addr, i
29502 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
29503 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
29504 2c 20 61 64 64 72 2b 31 2c 20 69 44 62 29 3b 0a  , addr+1, iDb);.
29505 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29506 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64  eChangeP1(v, add
29507 72 2b 36 2c 20 53 51 4c 49 54 45 5f 44 45 46 41  r+6, SQLITE_DEFA
29508 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 29 3b  ULT_CACHE_SIZE);
29509 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2950a 20 20 69 6e 74 20 73 69 7a 65 20 3d 20 61 74 6f    int size = ato
2950b 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  i(zRight);.     
2950c 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20 73 69   if( size<0 ) si
2950d 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20  ze = -size;.    
2950e 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
2950f 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
29510 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20  rse, 0, iDb);.  
29511 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29512 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
29513 67 65 72 2c 20 73 69 7a 65 2c 20 31 29 3b 0a 20  ger, size, 1);. 
29514 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29515 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61  AddOp3(v, OP_Rea
29516 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 32 2c  dCookie, iDb, 2,
29517 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43   BTREE_DEFAULT_C
29518 41 43 48 45 5f 53 49 5a 45 29 3b 0a 20 20 20 20  ACHE_SIZE);.    
29519 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
2951a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2951b 5f 49 66 50 6f 73 2c 20 32 2c 20 30 29 3b 0a 20  _IfPos, 2, 0);. 
2951c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2951d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2951e 65 67 65 72 2c 20 2d 73 69 7a 65 2c 20 31 29 3b  eger, -size, 1);
2951f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29520 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
29521 64 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  dr);.      sqlit
29522 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
29523 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44  OP_SetCookie, iD
29524 62 2c 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54  b, BTREE_DEFAULT
29525 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 31 29 3b  _CACHE_SIZE, 1);
29526 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 53 63 68  .      pDb->pSch
29527 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20  ema->cache_size 
29528 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 73 71  = size;.      sq
29529 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63  lite3BtreeSetCac
2952a 68 65 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c  heSize(pDb->pBt,
2952b 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63   pDb->pSchema->c
2952c 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20  ache_size);.    
2952d 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a  }.  }else..  /*.
2952e 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
2952f 74 61 62 61 73 65 2e 5d 70 61 67 65 5f 73 69 7a  tabase.]page_siz
29530 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  e.  **  PRAGMA [
29531 64 61 74 61 62 61 73 65 2e 5d 70 61 67 65 5f 73  database.]page_s
29532 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ize=N.  **.  ** 
29533 54 68 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72  The first form r
29534 65 70 6f 72 74 73 20 74 68 65 20 63 75 72 72 65  eports the curre
29535 6e 74 20 73 65 74 74 69 6e 67 20 66 6f 72 20 74  nt setting for t
29536 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  he.  ** database
29537 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 62 79   page size in by
29538 74 65 73 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  tes.  The second
29539 20 66 6f 72 6d 20 73 65 74 73 20 74 68 65 0a 20   form sets the. 
2953a 20 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67   ** database pag
2953b 65 20 73 69 7a 65 20 76 61 6c 75 65 2e 20 20 54  e size value.  T
2953c 68 65 20 76 61 6c 75 65 20 63 61 6e 20 6f 6e 6c  he value can onl
2953d 79 20 62 65 20 73 65 74 20 69 66 0a 20 20 2a 2a  y be set if.  **
2953e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
2953f 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63  s not yet been c
29540 72 65 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  reated..  */.  i
29541 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
29542 70 28 7a 4c 65 66 74 2c 22 70 61 67 65 5f 73 69  p(zLeft,"page_si
29543 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42  ze")==0 ){.    B
29544 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62 2d  tree *pBt = pDb-
29545 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74  >pBt;.    assert
29546 28 20 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20  ( pBt!=0 );.    
29547 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20  if( !zRight ){. 
29548 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20       int size = 
29549 41 4c 57 41 59 53 28 70 42 74 29 20 3f 20 73 71  ALWAYS(pBt) ? sq
2954a 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
2954b 65 53 69 7a 65 28 70 42 74 29 20 3a 20 30 3b 0a  eSize(pBt) : 0;.
2954c 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67        returnSing
2954d 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 70  leInt(pParse, "p
2954e 61 67 65 5f 73 69 7a 65 22 2c 20 73 69 7a 65 29  age_size", size)
2954f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
29550 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 61 79     /* Malloc may
29551 20 66 61 69 6c 20 77 68 65 6e 20 73 65 74 74 69   fail when setti
29552 6e 67 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  ng the page-size
29553 2c 20 61 73 20 74 68 65 72 65 20 69 73 20 61 6e  , as there is an
29554 20 69 6e 74 65 72 6e 61 6c 0a 20 20 20 20 20 20   internal.      
29555 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74 20 74  ** buffer that t
29556 68 65 20 70 61 67 65 72 20 6d 6f 64 75 6c 65 20  he pager module 
29557 72 65 73 69 7a 65 73 20 75 73 69 6e 67 20 73 71  resizes using sq
29558 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e  lite3_realloc().
29559 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2955a 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65  db->nextPagesize
2955b 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b   = atoi(zRight);
2955c 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
2955d 45 5f 4e 4f 4d 45 4d 3d 3d 73 71 6c 69 74 65 33  E_NOMEM==sqlite3
2955e 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65  BtreeSetPageSize
2955f 28 70 42 74 2c 20 64 62 2d 3e 6e 65 78 74 50 61  (pBt, db->nextPa
29560 67 65 73 69 7a 65 2c 20 2d 31 2c 20 30 29 20 29  gesize, -1, 0) )
29561 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61  {.        db->ma
29562 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
29563 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
29564 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }else..  /*.  **
29565 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
29566 73 65 2e 5d 6d 61 78 5f 70 61 67 65 5f 63 6f 75  se.]max_page_cou
29567 6e 74 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  nt.  **  PRAGMA 
29568 5b 64 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70  [database.]max_p
29569 61 67 65 5f 63 6f 75 6e 74 3d 4e 0a 20 20 2a 2a  age_count=N.  **
2956a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
2956b 66 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65  form reports the
2956c 20 63 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67   current setting
2956d 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61   for the.  ** ma
2956e 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
2956f 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
29570 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65  abase file.  The
29571 20 0a 20 20 2a 2a 20 73 65 63 6f 6e 64 20 66 6f   .  ** second fo
29572 72 6d 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63  rm attempts to c
29573 68 61 6e 67 65 20 74 68 69 73 20 73 65 74 74 69  hange this setti
29574 6e 67 2e 20 20 42 6f 74 68 0a 20 20 2a 2a 20 66  ng.  Both.  ** f
29575 6f 72 6d 73 20 72 65 74 75 72 6e 20 74 68 65 20  orms return the 
29576 63 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67 2e  current setting.
29577 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
29578 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
29579 2c 22 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74  ,"max_page_count
2957a 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72  ")==0 ){.    Btr
2957b 65 65 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70  ee *pBt = pDb->p
2957c 42 74 3b 0a 20 20 20 20 69 6e 74 20 6e 65 77 4d  Bt;.    int newM
2957d 61 78 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65  ax = 0;.    asse
2957e 72 74 28 20 70 42 74 21 3d 30 20 29 3b 0a 20 20  rt( pBt!=0 );.  
2957f 20 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a    if( zRight ){.
29580 20 20 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 61        newMax = a
29581 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20  toi(zRight);.   
29582 20 7d 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59   }.    if( ALWAY
29583 53 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  S(pBt) ){.      
29584 6e 65 77 4d 61 78 20 3d 20 73 71 6c 69 74 65 33  newMax = sqlite3
29585 42 74 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e  BtreeMaxPageCoun
29586 74 28 70 42 74 2c 20 6e 65 77 4d 61 78 29 3b 0a  t(pBt, newMax);.
29587 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e      }.    return
29588 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65  SingleInt(pParse
29589 2c 20 22 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e  , "max_page_coun
2958a 74 22 2c 20 6e 65 77 4d 61 78 29 3b 0a 20 20 7d  t", newMax);.  }
2958b 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  else..  /*.  ** 
2958c 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
2958d 65 2e 5d 70 61 67 65 5f 63 6f 75 6e 74 0a 20 20  e.]page_count.  
2958e 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74  **.  ** Return t
2958f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
29590 65 73 20 69 6e 20 74 68 65 20 73 70 65 63 69 66  es in the specif
29591 69 65 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20  ied database..  
29592 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
29593 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70  StrICmp(zLeft,"p
29594 61 67 65 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 29  age_count")==0 )
29595 7b 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 0a  {.    int iReg;.
29596 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
29597 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
29598 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f  ) ) goto pragma_
29599 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  out;.    sqlite3
2959a 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61  CodeVerifySchema
2959b 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
2959c 20 20 20 69 52 65 67 20 3d 20 2b 2b 70 50 61 72     iReg = ++pPar
2959d 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71  se->nMem;.    sq
2959e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2959f 76 2c 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 2c  v, OP_Pagecount,
295a0 20 69 44 62 2c 20 69 52 65 67 29 3b 0a 20 20 20   iDb, iReg);.   
295a1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
295a2 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52  p2(v, OP_ResultR
295a3 6f 77 2c 20 69 52 65 67 2c 20 31 29 3b 0a 20 20  ow, iReg, 1);.  
295a4 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
295a5 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
295a6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
295a7 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
295a8 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 61  OLNAME_NAME, "pa
295a9 67 65 5f 63 6f 75 6e 74 22 2c 20 53 51 4c 49 54  ge_count", SQLIT
295aa 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 65 6c  E_STATIC);.  }el
295ab 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  se..  /*.  **  P
295ac 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
295ad 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 0a 20 20  ]locking_mode.  
295ae 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
295af 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f  base.]locking_mo
295b0 64 65 20 3d 20 28 6e 6f 72 6d 61 6c 7c 65 78 63  de = (normal|exc
295b1 6c 75 73 69 76 65 29 0a 20 20 2a 2f 0a 20 20 69  lusive).  */.  i
295b2 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
295b3 70 28 7a 4c 65 66 74 2c 22 6c 6f 63 6b 69 6e 67  p(zLeft,"locking
295b4 5f 6d 6f 64 65 22 29 3d 3d 30 20 29 7b 0a 20 20  _mode")==0 ){.  
295b5 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52    const char *zR
295b6 65 74 20 3d 20 22 6e 6f 72 6d 61 6c 22 3b 0a 20  et = "normal";. 
295b7 20 20 20 69 6e 74 20 65 4d 6f 64 65 20 3d 20 67     int eMode = g
295b8 65 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 7a 52  etLockingMode(zR
295b9 69 67 68 74 29 3b 0a 0a 20 20 20 20 69 66 28 20  ight);..    if( 
295ba 70 49 64 32 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d  pId2->n==0 && eM
295bb 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ode==PAGER_LOCKI
295bc 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 29 7b 0a  NGMODE_QUERY ){.
295bd 20 20 20 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20        /* Simple 
295be 22 50 52 41 47 4d 41 20 6c 6f 63 6b 69 6e 67 5f  "PRAGMA locking_
295bf 6d 6f 64 65 3b 22 20 73 74 61 74 65 6d 65 6e 74  mode;" statement
295c0 2e 20 54 68 69 73 20 69 73 20 61 20 71 75 65 72  . This is a quer
295c1 79 20 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74  y for.      ** t
295c2 68 65 20 63 75 72 72 65 6e 74 20 64 65 66 61 75  he current defau
295c3 6c 74 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20  lt locking mode 
295c4 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 64 69  (which may be di
295c5 66 66 65 72 65 6e 74 20 74 6f 0a 20 20 20 20 20  fferent to.     
295c6 20 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d   ** the locking-
295c7 6d 6f 64 65 20 6f 66 20 74 68 65 20 6d 61 69 6e  mode of the main
295c8 20 64 61 74 61 62 61 73 65 29 2e 0a 20 20 20 20   database)..    
295c9 20 20 2a 2f 0a 20 20 20 20 20 20 65 4d 6f 64 65    */.      eMode
295ca 20 3d 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d   = db->dfltLockM
295cb 6f 64 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ode;.    }else{.
295cc 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50 61        Pager *pPa
295cd 67 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ger;.      if( p
295ce 49 64 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20  Id2->n==0 ){.   
295cf 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 6e 64       /* This ind
295d0 69 63 61 74 65 73 20 74 68 61 74 20 6e 6f 20 64  icates that no d
295d1 61 74 61 62 61 73 65 20 6e 61 6d 65 20 77 61 73  atabase name was
295d2 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61   specified as pa
295d3 72 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66  rt.        ** of
295d4 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d   the PRAGMA comm
295d5 61 6e 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  and. In this cas
295d6 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f  e the locking-mo
295d7 64 65 20 6d 75 73 74 20 62 65 0a 20 20 20 20 20  de must be.     
295d8 20 20 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c     ** set on all
295d9 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
295da 73 65 73 2c 20 61 73 20 77 65 6c 6c 20 61 73 20  ses, as well as 
295db 74 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65  the main db file
295dc 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  ..        **.   
295dd 20 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68       ** Also, th
295de 65 20 73 71 6c 69 74 65 33 2e 64 66 6c 74 4c 6f  e sqlite3.dfltLo
295df 63 6b 4d 6f 64 65 20 76 61 72 69 61 62 6c 65 20  ckMode variable 
295e0 69 73 20 73 65 74 20 73 6f 20 74 68 61 74 0a 20  is set so that. 
295e1 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 20 73 75         ** any su
295e2 62 73 65 71 75 65 6e 74 6c 79 20 61 74 74 61 63  bsequently attac
295e3 68 65 64 20 64 61 74 61 62 61 73 65 73 20 61 6c  hed databases al
295e4 73 6f 20 75 73 65 20 74 68 65 20 73 70 65 63 69  so use the speci
295e5 66 69 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  fied.        ** 
295e6 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 2e 0a 20 20  locking mode..  
295e7 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
295e8 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20   int ii;.       
295e9 20 61 73 73 65 72 74 28 70 44 62 3d 3d 26 64 62   assert(pDb==&db
295ea 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20 20 20 20 20  ->aDb[0]);.     
295eb 20 20 20 66 6f 72 28 69 69 3d 32 3b 20 69 69 3c     for(ii=2; ii<
295ec 64 62 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a  db->nDb; ii++){.
295ed 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
295ee 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
295ef 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 69 69 5d  ager(db->aDb[ii]
295f0 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20 20  .pBt);.         
295f1 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
295f2 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c  kingMode(pPager,
295f3 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 20 20 20   eMode);.       
295f4 20 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 64   }.        db->d
295f5 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20 28 75  fltLockMode = (u
295f6 38 29 65 4d 6f 64 65 3b 0a 20 20 20 20 20 20 7d  8)eMode;.      }
295f7 0a 20 20 20 20 20 20 70 50 61 67 65 72 20 3d 20  .      pPager = 
295f8 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
295f9 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20  r(pDb->pBt);.   
295fa 20 20 20 65 4d 6f 64 65 20 3d 20 73 71 6c 69 74     eMode = sqlit
295fb 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
295fc 64 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65  de(pPager, eMode
295fd 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73  );.    }..    as
295fe 73 65 72 74 28 65 4d 6f 64 65 3d 3d 50 41 47 45  sert(eMode==PAGE
295ff 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f  R_LOCKINGMODE_NO
29600 52 4d 41 4c 7c 7c 65 4d 6f 64 65 3d 3d 50 41 47  RMAL||eMode==PAG
29601 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45  ER_LOCKINGMODE_E
29602 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 20 20 69  XCLUSIVE);.    i
29603 66 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  f( eMode==PAGER_
29604 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c  LOCKINGMODE_EXCL
29605 55 53 49 56 45 20 29 7b 0a 20 20 20 20 20 20 7a  USIVE ){.      z
29606 52 65 74 20 3d 20 22 65 78 63 6c 75 73 69 76 65  Ret = "exclusive
29607 22 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  ";.    }.    sql
29608 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
29609 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71  ls(v, 1);.    sq
2960a 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2960b 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
2960c 45 5f 4e 41 4d 45 2c 20 22 6c 6f 63 6b 69 6e 67  E_NAME, "locking
2960d 5f 6d 6f 64 65 22 2c 20 53 51 4c 49 54 45 5f 53  _mode", SQLITE_S
2960e 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  TATIC);.    sqli
2960f 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
29610 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
29611 31 2c 20 30 2c 20 7a 52 65 74 2c 20 30 29 3b 0a  1, 0, zRet, 0);.
29612 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29613 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
29614 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20  ltRow, 1, 1);.  
29615 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }else..  /*.  **
29616 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
29617 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  se.]journal_mode
29618 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
29619 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c  atabase.]journal
2961a 5f 6d 6f 64 65 20 3d 20 28 64 65 6c 65 74 65 7c  _mode = (delete|
2961b 70 65 72 73 69 73 74 7c 6f 66 66 7c 74 72 75 6e  persist|off|trun
2961c 63 61 74 65 7c 6d 65 6d 6f 72 79 29 0a 20 20 2a  cate|memory).  *
2961d 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
2961e 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6a 6f  trICmp(zLeft,"jo
2961f 75 72 6e 61 6c 5f 6d 6f 64 65 22 29 3d 3d 30 20  urnal_mode")==0 
29620 29 7b 0a 20 20 20 20 69 6e 74 20 65 4d 6f 64 65  ){.    int eMode
29621 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 68 61  ;.    static cha
29622 72 20 2a 20 63 6f 6e 73 74 20 61 7a 4d 6f 64 65  r * const azMode
29623 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  Name[] = {.     
29624 20 22 64 65 6c 65 74 65 22 2c 20 22 70 65 72 73   "delete", "pers
29625 69 73 74 22 2c 20 22 6f 66 66 22 2c 20 22 74 72  ist", "off", "tr
29626 75 6e 63 61 74 65 22 2c 20 22 6d 65 6d 6f 72 79  uncate", "memory
29627 22 0a 20 20 20 20 7d 3b 0a 0a 20 20 20 20 69 66  ".    };..    if
29628 28 20 7a 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20  ( zRight==0 ){. 
29629 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 50 41 47       eMode = PAG
2962a 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51  ER_JOURNALMODE_Q
2962b 55 45 52 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  UERY;.    }else{
2962c 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73  .      int n = s
2962d 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
2962e 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 65 4d  Right);.      eM
2962f 6f 64 65 20 3d 20 73 69 7a 65 6f 66 28 61 7a 4d  ode = sizeof(azM
29630 6f 64 65 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28  odeName)/sizeof(
29631 61 7a 4d 6f 64 65 4e 61 6d 65 5b 30 5d 29 20 2d  azModeName[0]) -
29632 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   1;.      while(
29633 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20 73 71 6c   eMode>=0 && sql
29634 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 52 69  ite3StrNICmp(zRi
29635 67 68 74 2c 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b  ght, azModeName[
29636 65 4d 6f 64 65 5d 2c 20 6e 29 21 3d 30 20 29 7b  eMode], n)!=0 ){
29637 0a 20 20 20 20 20 20 20 20 65 4d 6f 64 65 2d 2d  .        eMode--
29638 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
29639 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d      if( pId2->n=
2963a 3d 30 20 26 26 20 65 4d 6f 64 65 3d 3d 50 41 47  =0 && eMode==PAG
2963b 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51  ER_JOURNALMODE_Q
2963c 55 45 52 59 20 29 7b 0a 20 20 20 20 20 20 2f 2a  UERY ){.      /*
2963d 20 53 69 6d 70 6c 65 20 22 50 52 41 47 4d 41 20   Simple "PRAGMA 
2963e 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3b 22 20 73  journal_mode;" s
2963f 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69  tatement. This i
29640 73 20 61 20 71 75 65 72 79 20 66 6f 72 0a 20 20  s a query for.  
29641 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65      ** the curre
29642 6e 74 20 64 65 66 61 75 6c 74 20 6a 6f 75 72 6e  nt default journ
29643 61 6c 20 6d 6f 64 65 20 28 77 68 69 63 68 20 6d  al mode (which m
29644 61 79 20 62 65 20 64 69 66 66 65 72 65 6e 74 20  ay be different 
29645 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  to.      ** the 
29646 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6f 66 20  journal-mode of 
29647 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
29648 65 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  e)..      */.   
29649 20 20 20 65 4d 6f 64 65 20 3d 20 64 62 2d 3e 64     eMode = db->d
2964a 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 0a  fltJournalMode;.
2964b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2964c 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a   Pager *pPager;.
2964d 20 20 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e        if( pId2->
2964e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  n==0 ){.        
2964f 2f 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65  /* This indicate
29650 73 20 74 68 61 74 20 6e 6f 20 64 61 74 61 62 61  s that no databa
29651 73 65 20 6e 61 6d 65 20 77 61 73 20 73 70 65 63  se name was spec
29652 69 66 69 65 64 20 61 73 20 70 61 72 74 0a 20 20  ified as part.  
29653 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
29654 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 20  PRAGMA command. 
29655 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
29656 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6d 75   journal-mode mu
29657 73 74 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a  st be.        **
29658 20 73 65 74 20 6f 6e 20 61 6c 6c 20 61 74 74 61   set on all atta
29659 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2c 20  ched databases, 
2965a 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 6d  as well as the m
2965b 61 69 6e 20 64 62 20 66 69 6c 65 2e 0a 20 20 20  ain db file..   
2965c 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
2965d 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20 73 71 6c  ** Also, the sql
2965e 69 74 65 33 2e 64 66 6c 74 4a 6f 75 72 6e 61 6c  ite3.dfltJournal
2965f 4d 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69 73  Mode variable is
29660 20 73 65 74 20 73 6f 20 74 68 61 74 0a 20 20 20   set so that.   
29661 20 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 62 73       ** any subs
29662 65 71 75 65 6e 74 6c 79 20 61 74 74 61 63 68 65  equently attache
29663 64 20 64 61 74 61 62 61 73 65 73 20 61 6c 73 6f  d databases also
29664 20 75 73 65 20 74 68 65 20 73 70 65 63 69 66 69   use the specifi
29665 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 6a 6f  ed.        ** jo
29666 75 72 6e 61 6c 20 6d 6f 64 65 2e 0a 20 20 20 20  urnal mode..    
29667 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
29668 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61  nt ii;.        a
29669 73 73 65 72 74 28 70 44 62 3d 3d 26 64 62 2d 3e  ssert(pDb==&db->
2966a 61 44 62 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20  aDb[0]);.       
2966b 20 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 64 62   for(ii=1; ii<db
2966c 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20  ->nDb; ii++){.  
2966d 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e          if( db->
2966e 61 44 62 5b 69 69 5d 2e 70 42 74 20 29 7b 0a 20  aDb[ii].pBt ){. 
2966f 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65             pPage
29670 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
29671 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 69 69  Pager(db->aDb[ii
29672 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20  ].pBt);.        
29673 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
29674 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67  JournalMode(pPag
29675 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20  er, eMode);.    
29676 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
29677 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66  }.        db->df
29678 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20  ltJournalMode = 
29679 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 20 20 20  (u8)eMode;.     
2967a 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 20   }.      pPager 
2967b 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
2967c 67 65 72 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20  ger(pDb->pBt);. 
2967d 20 20 20 20 20 65 4d 6f 64 65 20 3d 20 73 71 6c       eMode = sql
2967e 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
2967f 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65 4d 6f  Mode(pPager, eMo
29680 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  de);.    }.    a
29681 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41  ssert( eMode==PA
29682 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
29683 44 45 4c 45 54 45 0a 20 20 20 20 20 20 20 20 20  DELETE.         
29684 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50       || eMode==P
29685 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
29686 5f 54 52 55 4e 43 41 54 45 0a 20 20 20 20 20 20  _TRUNCATE.      
29687 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65          || eMode
29688 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
29689 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 20 20  ODE_PERSIST.    
2968a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
2968b 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
2968c 4c 4d 4f 44 45 5f 4f 46 46 0a 20 20 20 20 20 20  LMODE_OFF.      
2968d 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65          || eMode
2968e 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
2968f 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a 20 20  ODE_MEMORY );.  
29690 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29691 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
29692 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29693 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
29694 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6a 6f  OLNAME_NAME, "jo
29695 75 72 6e 61 6c 5f 6d 6f 64 65 22 2c 20 53 51 4c  urnal_mode", SQL
29696 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
29697 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29698 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
29699 2c 20 30 2c 20 31 2c 20 30 2c 20 0a 20 20 20 20  , 0, 1, 0, .    
2969a 20 20 20 20 20 20 20 61 7a 4d 6f 64 65 4e 61 6d         azModeNam
2969b 65 5b 65 4d 6f 64 65 5d 2c 20 50 34 5f 53 54 41  e[eMode], P4_STA
2969c 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  TIC);.    sqlite
2969d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2969e 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
2969f 31 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f  1);.  }else..  /
296a0 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
296a1 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61  database.]journa
296a2 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 0a 20 20 2a  l_size_limit.  *
296a3 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
296a4 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 73 69 7a  ase.]journal_siz
296a5 65 5f 6c 69 6d 69 74 3d 4e 0a 20 20 2a 2a 0a 20  e_limit=N.  **. 
296a6 20 2a 2a 20 47 65 74 20 6f 72 20 73 65 74 20 74   ** Get or set t
296a7 68 65 20 73 69 7a 65 20 6c 69 6d 69 74 20 6f 6e  he size limit on
296a8 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
296a9 6c 20 66 69 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20  l files..  */.  
296aa 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
296ab 6d 70 28 7a 4c 65 66 74 2c 22 6a 6f 75 72 6e 61  mp(zLeft,"journa
296ac 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 22 29 3d 3d  l_size_limit")==
296ad 30 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a  0 ){.    Pager *
296ae 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
296af 42 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e  BtreePager(pDb->
296b0 70 42 74 29 3b 0a 20 20 20 20 69 36 34 20 69 4c  pBt);.    i64 iL
296b1 69 6d 69 74 20 3d 20 2d 32 3b 0a 20 20 20 20 69  imit = -2;.    i
296b2 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  f( zRight ){.   
296b3 20 20 20 73 71 6c 69 74 65 33 41 74 6f 69 36 34     sqlite3Atoi64
296b4 28 7a 52 69 67 68 74 2c 20 26 69 4c 69 6d 69 74  (zRight, &iLimit
296b5 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 4c 69  );.      if( iLi
296b6 6d 69 74 3c 2d 31 20 29 20 69 4c 69 6d 69 74 20  mit<-1 ) iLimit 
296b7 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  = -1;.    }.    
296b8 69 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33  iLimit = sqlite3
296b9 50 61 67 65 72 4a 6f 75 72 6e 61 6c 53 69 7a 65  PagerJournalSize
296ba 4c 69 6d 69 74 28 70 50 61 67 65 72 2c 20 69 4c  Limit(pPager, iL
296bb 69 6d 69 74 29 3b 0a 20 20 20 20 72 65 74 75 72  imit);.    retur
296bc 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73  nSingleInt(pPars
296bd 65 2c 20 22 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65  e, "journal_size
296be 5f 6c 69 6d 69 74 22 2c 20 69 4c 69 6d 69 74 29  _limit", iLimit)
296bf 3b 0a 20 20 7d 65 6c 73 65 0a 0a 23 65 6e 64 69  ;.  }else..#endi
296c0 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
296c1 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a  _PAGER_PRAGMAS *
296c2 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52  /..  /*.  **  PR
296c3 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
296c4 61 75 74 6f 5f 76 61 63 75 75 6d 0a 20 20 2a 2a  auto_vacuum.  **
296c5 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
296c6 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d 3d  se.]auto_vacuum=
296c7 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20  N.  **.  ** Get 
296c8 6f 72 20 73 65 74 20 74 68 65 20 76 61 6c 75 65  or set the value
296c9 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
296ca 20 27 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70   'auto-vacuum' p
296cb 61 72 61 6d 65 74 65 72 2e 0a 20 20 2a 2a 20 54  arameter..  ** T
296cc 68 65 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20  he value is one 
296cd 6f 66 3a 20 20 30 20 4e 4f 4e 45 20 31 20 46 55  of:  0 NONE 1 FU
296ce 4c 4c 20 32 20 49 4e 43 52 45 4d 45 4e 54 41 4c  LL 2 INCREMENTAL
296cf 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
296d0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
296d1 43 55 55 4d 0a 20 20 69 66 28 20 73 71 6c 69 74  CUUM.  if( sqlit
296d2 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
296d3 22 61 75 74 6f 5f 76 61 63 75 75 6d 22 29 3d 3d  "auto_vacuum")==
296d4 30 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  0 ){.    Btree *
296d5 70 42 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a  pBt = pDb->pBt;.
296d6 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 21      assert( pBt!
296d7 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71  =0 );.    if( sq
296d8 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
296d9 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20  pParse) ){.     
296da 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
296db 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
296dc 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
296dd 20 69 6e 74 20 61 75 74 6f 5f 76 61 63 75 75 6d   int auto_vacuum
296de 3b 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41  ;.      if( ALWA
296df 59 53 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  YS(pBt) ){.     
296e0 20 20 20 20 61 75 74 6f 5f 76 61 63 75 75 6d 20      auto_vacuum 
296e1 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
296e2 74 41 75 74 6f 56 61 63 75 75 6d 28 70 42 74 29  tAutoVacuum(pBt)
296e3 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
296e4 20 20 20 20 20 20 20 20 61 75 74 6f 5f 76 61 63          auto_vac
296e5 75 75 6d 20 3d 20 53 51 4c 49 54 45 5f 44 45 46  uum = SQLITE_DEF
296e6 41 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 3b  AULT_AUTOVACUUM;
296e7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
296e8 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70  eturnSingleInt(p
296e9 50 61 72 73 65 2c 20 22 61 75 74 6f 5f 76 61 63  Parse, "auto_vac
296ea 75 75 6d 22 2c 20 61 75 74 6f 5f 76 61 63 75 75  uum", auto_vacuu
296eb 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  m);.    }else{. 
296ec 20 20 20 20 20 69 6e 74 20 65 41 75 74 6f 20 3d       int eAuto =
296ed 20 67 65 74 41 75 74 6f 56 61 63 75 75 6d 28 7a   getAutoVacuum(z
296ee 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 61 73  Right);.      as
296ef 73 65 72 74 28 20 65 41 75 74 6f 3e 3d 30 20 26  sert( eAuto>=0 &
296f0 26 20 65 41 75 74 6f 3c 3d 32 20 29 3b 0a 20 20  & eAuto<=2 );.  
296f1 20 20 20 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f      db->nextAuto
296f2 76 61 63 20 3d 20 28 75 38 29 65 41 75 74 6f 3b  vac = (u8)eAuto;
296f3 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59  .      if( ALWAY
296f4 53 28 65 41 75 74 6f 3e 3d 30 29 20 29 7b 0a 20  S(eAuto>=0) ){. 
296f5 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20 53         /* Call S
296f6 65 74 41 75 74 6f 56 61 63 75 75 6d 28 29 20 74  etAutoVacuum() t
296f7 6f 20 73 65 74 20 69 6e 69 74 69 61 6c 69 7a 65  o set initialize
296f8 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 61 75   the internal au
296f9 74 6f 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a  to and.        *
296fa 2a 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 66 6c  * incr-vacuum fl
296fb 61 67 73 2e 20 54 68 69 73 20 69 73 20 72 65 71  ags. This is req
296fc 75 69 72 65 64 20 69 6e 20 63 61 73 65 20 74 68  uired in case th
296fd 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20 20  is connection.  
296fe 20 20 20 20 20 20 2a 2a 20 63 72 65 61 74 65 73        ** creates
296ff 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
29700 6c 65 2e 20 49 74 20 69 73 20 69 6d 70 6f 72 74  le. It is import
29701 61 6e 74 20 74 68 61 74 20 69 74 20 69 73 20 63  ant that it is c
29702 72 65 61 74 65 64 0a 20 20 20 20 20 20 20 20 2a  reated.        *
29703 2a 20 61 73 20 61 6e 20 61 75 74 6f 2d 76 61 63  * as an auto-vac
29704 75 75 6d 20 63 61 70 61 62 6c 65 20 64 62 2e 0a  uum capable db..
29705 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
29706 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 69     int rc = sqli
29707 74 65 33 42 74 72 65 65 53 65 74 41 75 74 6f 56  te3BtreeSetAutoV
29708 61 63 75 75 6d 28 70 42 74 2c 20 65 41 75 74 6f  acuum(pBt, eAuto
29709 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
2970a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
2970b 28 65 41 75 74 6f 3d 3d 31 20 7c 7c 20 65 41 75  (eAuto==1 || eAu
2970c 74 6f 3d 3d 32 29 20 29 7b 0a 20 20 20 20 20 20  to==2) ){.      
2970d 20 20 20 20 2f 2a 20 57 68 65 6e 20 73 65 74 74      /* When sett
2970e 69 6e 67 20 74 68 65 20 61 75 74 6f 5f 76 61 63  ing the auto_vac
2970f 75 75 6d 20 6d 6f 64 65 20 74 6f 20 65 69 74 68  uum mode to eith
29710 65 72 20 22 66 75 6c 6c 22 20 6f 72 20 0a 20 20  er "full" or .  
29711 20 20 20 20 20 20 20 20 2a 2a 20 22 69 6e 63 72          ** "incr
29712 65 6d 65 6e 74 61 6c 22 2c 20 77 72 69 74 65 20  emental", write 
29713 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74  the value of met
29714 61 5b 36 5d 20 69 6e 20 74 68 65 20 64 61 74 61  a[6] in the data
29715 62 61 73 65 0a 20 20 20 20 20 20 20 20 20 20 2a  base.          *
29716 2a 20 66 69 6c 65 2e 20 42 65 66 6f 72 65 20 77  * file. Before w
29717 72 69 74 69 6e 67 20 74 6f 20 6d 65 74 61 5b 36  riting to meta[6
29718 5d 2c 20 63 68 65 63 6b 20 74 68 61 74 20 6d 65  ], check that me
29719 74 61 5b 33 5d 20 69 6e 64 69 63 61 74 65 73 0a  ta[3] indicates.
2971a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61            ** tha
2971b 74 20 74 68 69 73 20 72 65 61 6c 6c 79 20 69 73  t this really is
2971c 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   an auto-vacuum 
2971d 63 61 70 61 62 6c 65 20 64 61 74 61 62 61 73 65  capable database
2971e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
2971f 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63 20           static 
29720 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
29721 20 73 65 74 4d 65 74 61 36 5b 5d 20 3d 20 7b 0a   setMeta6[] = {.
29722 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
29723 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20  _Transaction,   
29724 20 30 2c 20 20 20 20 20 20 20 20 20 31 2c 20 20   0,         1,  
29725 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
29726 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20  },    /* 0 */.  
29727 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52            { OP_R
29728 65 61 64 43 6f 6f 6b 69 65 2c 20 20 20 20 20 30  eadCookie,     0
29729 2c 20 20 20 20 20 20 20 20 20 31 2c 20 20 20 20  ,         1,    
2972a 20 20 20 20 20 42 54 52 45 45 5f 4c 41 52 47 45       BTREE_LARGE
2972b 53 54 5f 52 4f 4f 54 5f 50 41 47 45 7d 2c 0a 20  ST_ROOT_PAGE},. 
2972c 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
2972d 49 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  If,             
2972e 31 2c 20 20 20 20 20 20 20 20 20 30 2c 20 20 20  1,         0,   
2972f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 7d                0}
29730 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20  ,    /* 2 */.   
29731 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 48 61           { OP_Ha
29732 6c 74 2c 20 20 20 20 20 20 20 20 20 20 20 53 51  lt,           SQ
29733 4c 49 54 45 5f 4f 4b 2c 20 4f 45 5f 41 62 6f 72  LITE_OK, OE_Abor
29734 74 2c 20 20 20 20 20 20 20 20 20 20 30 7d 2c 20  t,          0}, 
29735 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 20 20 20     /* 3 */.     
29736 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65         { OP_Inte
29737 67 65 72 2c 20 20 20 20 20 20 20 20 30 2c 20 20  ger,        0,  
29738 20 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 20         1,       
29739 20 20 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20            0},   
2973a 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20 20 20 20   /* 4 */.       
2973b 20 20 20 20 20 7b 20 4f 50 5f 53 65 74 43 6f 6f       { OP_SetCoo
2973c 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20 20 20  kie,      0,    
2973d 20 20 20 20 20 42 54 52 45 45 5f 49 4e 43 52 5f       BTREE_INCR_
2973e 56 41 43 55 55 4d 2c 20 31 7d 2c 20 20 20 20 2f  VACUUM, 1},    /
2973f 2a 20 35 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 5 */.         
29740 20 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   };.          in
29741 74 20 69 41 64 64 72 3b 0a 20 20 20 20 20 20 20  t iAddr;.       
29742 20 20 20 69 41 64 64 72 20 3d 20 73 71 6c 69 74     iAddr = sqlit
29743 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
29744 76 2c 20 41 72 72 61 79 53 69 7a 65 28 73 65 74  v, ArraySize(set
29745 4d 65 74 61 36 29 2c 20 73 65 74 4d 65 74 61 36  Meta6), setMeta6
29746 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
29747 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
29748 28 76 2c 20 69 41 64 64 72 2c 20 69 44 62 29 3b  (v, iAddr, iDb);
29749 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2974a 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
2974b 2c 20 69 41 64 64 72 2b 31 2c 20 69 44 62 29 3b  , iAddr+1, iDb);
2974c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2974d 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
2974e 2c 20 69 41 64 64 72 2b 32 2c 20 69 41 64 64 72  , iAddr+2, iAddr
2974f 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +4);.          s
29750 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
29751 50 31 28 76 2c 20 69 41 64 64 72 2b 34 2c 20 65  P1(v, iAddr+4, e
29752 41 75 74 6f 2d 31 29 3b 0a 20 20 20 20 20 20 20  Auto-1);.       
29753 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
29754 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b  angeP1(v, iAddr+
29755 35 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20  5, iDb);.       
29756 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73     sqlite3VdbeUs
29757 65 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b  esBtree(v, iDb);
29758 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29759 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
2975a 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20 20  .#endif..  /*.  
2975b 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
2975c 62 61 73 65 2e 5d 69 6e 63 72 65 6d 65 6e 74 61  base.]incrementa
2975d 6c 5f 76 61 63 75 75 6d 28 4e 29 0a 20 20 2a 2a  l_vacuum(N).  **
2975e 0a 20 20 2a 2a 20 44 6f 20 4e 20 73 74 65 70 73  .  ** Do N steps
2975f 20 6f 66 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20   of incremental 
29760 76 61 63 75 75 6d 69 6e 67 20 6f 6e 20 61 20 64  vacuuming on a d
29761 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 23 69  atabase..  */.#i
29762 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29763 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69  T_AUTOVACUUM.  i
29764 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
29765 70 28 7a 4c 65 66 74 2c 22 69 6e 63 72 65 6d 65  p(zLeft,"increme
29766 6e 74 61 6c 5f 76 61 63 75 75 6d 22 29 3d 3d 30  ntal_vacuum")==0
29767 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 4c 69 6d   ){.    int iLim
29768 69 74 2c 20 61 64 64 72 3b 0a 20 20 20 20 69 66  it, addr;.    if
29769 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
2976a 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
2976b 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61       goto pragma
2976c 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
2976d 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20 7c 7c  if( zRight==0 ||
2976e 20 21 73 71 6c 69 74 65 33 47 65 74 49 6e 74 33   !sqlite3GetInt3
2976f 32 28 7a 52 69 67 68 74 2c 20 26 69 4c 69 6d 69  2(zRight, &iLimi
29770 74 29 20 7c 7c 20 69 4c 69 6d 69 74 3c 3d 30 20  t) || iLimit<=0 
29771 29 7b 0a 20 20 20 20 20 20 69 4c 69 6d 69 74 20  ){.      iLimit 
29772 3d 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20  = 0x7fffffff;.  
29773 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42    }.    sqlite3B
29774 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
29775 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
29776 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  b);.    sqlite3V
29777 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29778 49 6e 74 65 67 65 72 2c 20 69 4c 69 6d 69 74 2c  Integer, iLimit,
29779 20 31 29 3b 0a 20 20 20 20 61 64 64 72 20 3d 20   1);.    addr = 
2977a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2977b 31 28 76 2c 20 4f 50 5f 49 6e 63 72 56 61 63 75  1(v, OP_IncrVacu
2977c 75 6d 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71  um, iDb);.    sq
2977d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2977e 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
2977f 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
29780 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29781 5f 41 64 64 49 6d 6d 2c 20 31 2c 20 2d 31 29 3b  _AddImm, 1, -1);
29782 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
29783 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50  AddOp2(v, OP_IfP
29784 6f 73 2c 20 31 2c 20 61 64 64 72 29 3b 0a 20 20  os, 1, addr);.  
29785 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
29786 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
29787 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a    }else.#endif..
29788 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
29789 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
2978a 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  S.  /*.  **  PRA
2978b 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 63  GMA [database.]c
2978c 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20  ache_size.  **  
2978d 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
2978e 2e 5d 63 61 63 68 65 5f 73 69 7a 65 3d 4e 0a 20  .]cache_size=N. 
2978f 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72   **.  ** The fir
29790 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20  st form reports 
29791 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63 61  the current loca
29792 6c 20 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68  l setting for th
29793 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68  e.  ** page cach
29794 65 20 73 69 7a 65 2e 20 20 54 68 65 20 6c 6f 63  e size.  The loc
29795 61 6c 20 73 65 74 74 69 6e 67 20 63 61 6e 20 62  al setting can b
29796 65 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  e different from
29797 0a 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73  .  ** the persis
29798 74 65 6e 74 20 63 61 63 68 65 20 73 69 7a 65 20  tent cache size 
29799 76 61 6c 75 65 20 74 68 61 74 20 69 73 20 73 74  value that is st
2979a 6f 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 61  ored in the data
2979b 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 20 69  base.  ** file i
2979c 74 73 65 6c 66 2e 20 20 54 68 65 20 76 61 6c 75  tself.  The valu
2979d 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68  e returned is th
2979e 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
2979f 20 6f 66 0a 20 20 2a 2a 20 70 61 67 65 73 20 69   of.  ** pages i
297a0 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
297a1 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f  .  The second fo
297a2 72 6d 20 73 65 74 73 20 74 68 65 20 6c 6f 63 61  rm sets the loca
297a3 6c 0a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68  l.  ** page cach
297a4 65 20 73 69 7a 65 20 76 61 6c 75 65 2e 20 20 49  e size value.  I
297a5 74 20 64 6f 65 73 20 6e 6f 74 20 63 68 61 6e 67  t does not chang
297a6 65 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74  e the persistent
297a7 0a 20 20 2a 2a 20 63 61 63 68 65 20 73 69 7a 65  .  ** cache size
297a8 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 64   stored on the d
297a9 69 73 6b 20 73 6f 20 74 68 65 20 63 61 63 68 65  isk so the cache
297aa 20 73 69 7a 65 20 77 69 6c 6c 20 72 65 76 65 72   size will rever
297ab 74 0a 20 20 2a 2a 20 74 6f 20 69 74 73 20 64 65  t.  ** to its de
297ac 66 61 75 6c 74 20 76 61 6c 75 65 20 77 68 65 6e  fault value when
297ad 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
297ae 20 63 6c 6f 73 65 64 20 61 6e 64 20 72 65 6f 70   closed and reop
297af 65 6e 65 64 2e 0a 20 20 2a 2a 20 4e 20 73 68 6f  ened..  ** N sho
297b0 75 6c 64 20 62 65 20 61 20 70 6f 73 69 74 69 76  uld be a positiv
297b1 65 20 69 6e 74 65 67 65 72 2e 0a 20 20 2a 2f 0a  e integer..  */.
297b2 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
297b3 49 43 6d 70 28 7a 4c 65 66 74 2c 22 63 61 63 68  ICmp(zLeft,"cach
297b4 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b 0a 20  e_size")==0 ){. 
297b5 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
297b6 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
297b7 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
297b8 75 74 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69  ut;.    if( !zRi
297b9 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ght ){.      ret
297ba 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
297bb 72 73 65 2c 20 22 63 61 63 68 65 5f 73 69 7a 65  rse, "cache_size
297bc 22 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  ", pDb->pSchema-
297bd 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20  >cache_size);.  
297be 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
297bf 6e 74 20 73 69 7a 65 20 3d 20 61 74 6f 69 28 7a  nt size = atoi(z
297c0 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66  Right);.      if
297c1 28 20 73 69 7a 65 3c 30 20 29 20 73 69 7a 65 20  ( size<0 ) size 
297c2 3d 20 2d 73 69 7a 65 3b 0a 20 20 20 20 20 20 70  = -size;.      p
297c3 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
297c4 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a  he_size = size;.
297c5 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
297c6 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 70  eeSetCacheSize(p
297c7 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53  Db->pBt, pDb->pS
297c8 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a  chema->cache_siz
297c9 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  e);.    }.  }els
297ca 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50  e..  /*.  **   P
297cb 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65  RAGMA temp_store
297cc 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74  .  **   PRAGMA t
297cd 65 6d 70 5f 73 74 6f 72 65 20 3d 20 22 64 65 66  emp_store = "def
297ce 61 75 6c 74 22 7c 22 6d 65 6d 6f 72 79 22 7c 22  ault"|"memory"|"
297cf 66 69 6c 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20  file".  **.  ** 
297d0 52 65 74 75 72 6e 20 6f 72 20 73 65 74 20 74 68  Return or set th
297d1 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65 20 6f 66  e local value of
297d2 20 74 68 65 20 74 65 6d 70 5f 73 74 6f 72 65 20   the temp_store 
297d3 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a  flag.  Changing.
297d4 20 20 2a 2a 20 74 68 65 20 6c 6f 63 61 6c 20 76    ** the local v
297d5 61 6c 75 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61  alue does not ma
297d6 6b 65 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68  ke changes to th
297d7 65 20 64 69 73 6b 20 66 69 6c 65 20 61 6e 64 20  e disk file and 
297d8 74 68 65 20 64 65 66 61 75 6c 74 0a 20 20 2a 2a  the default.  **
297d9 20 76 61 6c 75 65 20 77 69 6c 6c 20 62 65 20 72   value will be r
297da 65 73 74 6f 72 65 64 20 74 68 65 20 6e 65 78 74  estored the next
297db 20 74 69 6d 65 20 74 68 65 20 64 61 74 61 62 61   time the databa
297dc 73 65 20 69 73 20 6f 70 65 6e 65 64 2e 0a 20 20  se is opened..  
297dd 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61  **.  ** Note tha
297de 74 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  t it is possible
297df 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79   for the library
297e0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70   compile-time op
297e1 74 69 6f 6e 73 20 74 6f 0a 20 20 2a 2a 20 6f 76  tions to.  ** ov
297e2 65 72 72 69 64 65 20 74 68 69 73 20 73 65 74 74  erride this sett
297e3 69 6e 67 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ing.  */.  if( s
297e4 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
297e5 65 66 74 2c 20 22 74 65 6d 70 5f 73 74 6f 72 65  eft, "temp_store
297e6 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ")==0 ){.    if(
297e7 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
297e8 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
297e9 74 28 70 50 61 72 73 65 2c 20 22 74 65 6d 70 5f  t(pParse, "temp_
297ea 73 74 6f 72 65 22 2c 20 64 62 2d 3e 74 65 6d 70  store", db->temp
297eb 5f 73 74 6f 72 65 29 3b 0a 20 20 20 20 7d 65 6c  _store);.    }el
297ec 73 65 7b 0a 20 20 20 20 20 20 63 68 61 6e 67 65  se{.      change
297ed 54 65 6d 70 53 74 6f 72 61 67 65 28 70 50 61 72  TempStorage(pPar
297ee 73 65 2c 20 7a 52 69 67 68 74 29 3b 0a 20 20 20  se, zRight);.   
297ef 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a   }.  }else..  /*
297f0 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74  .  **   PRAGMA t
297f1 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
297f2 6f 72 79 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d  ory.  **   PRAGM
297f3 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72  A temp_store_dir
297f4 65 63 74 6f 72 79 20 3d 20 22 22 7c 22 64 69 72  ectory = ""|"dir
297f5 65 63 74 6f 72 79 5f 6e 61 6d 65 22 0a 20 20 2a  ectory_name".  *
297f6 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72  *.  ** Return or
297f7 20 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76   set the local v
297f8 61 6c 75 65 20 6f 66 20 74 68 65 20 74 65 6d 70  alue of the temp
297f9 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79  _store_directory
297fa 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e 67   flag.  Changing
297fb 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  .  ** the value 
297fc 73 65 74 73 20 61 20 73 70 65 63 69 66 69 63 20  sets a specific 
297fd 64 69 72 65 63 74 6f 72 79 20 74 6f 20 62 65 20  directory to be 
297fe 75 73 65 64 20 66 6f 72 20 74 65 6d 70 6f 72 61  used for tempora
297ff 72 79 20 66 69 6c 65 73 2e 0a 20 20 2a 2a 20 53  ry files..  ** S
29800 65 74 74 69 6e 67 20 74 6f 20 61 20 6e 75 6c 6c  etting to a null
29801 20 73 74 72 69 6e 67 20 72 65 76 65 72 74 73 20   string reverts 
29802 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 74  to the default t
29803 65 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74 6f  emporary directo
29804 72 79 20 73 65 61 72 63 68 2e 0a 20 20 2a 2a 20  ry search..  ** 
29805 49 66 20 74 65 6d 70 6f 72 61 72 79 20 64 69 72  If temporary dir
29806 65 63 74 6f 72 79 20 69 73 20 63 68 61 6e 67 65  ectory is change
29807 64 2c 20 74 68 65 6e 20 69 6e 76 61 6c 69 64 61  d, then invalida
29808 74 65 54 65 6d 70 53 74 6f 72 61 67 65 2e 0a 20  teTempStorage.. 
29809 20 2a 2a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73   **.  */.  if( s
2980a 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
2980b 65 66 74 2c 20 22 74 65 6d 70 5f 73 74 6f 72 65  eft, "temp_store
2980c 5f 64 69 72 65 63 74 6f 72 79 22 29 3d 3d 30 20  _directory")==0 
2980d 29 7b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67  ){.    if( !zRig
2980e 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ht ){.      if( 
2980f 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
29810 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 20 20  ectory ){.      
29811 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29812 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20  NumCols(v, 1);. 
29813 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29814 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
29815 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
29816 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 74   .            "t
29817 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74  emp_store_direct
29818 6f 72 79 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ory", SQLITE_STA
29819 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
2981a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2981b 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
2981c 2c 20 31 2c 20 30 2c 20 73 71 6c 69 74 65 33 5f  , 1, 0, sqlite3_
2981d 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 2c 20  temp_directory, 
2981e 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
2981f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29820 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31   OP_ResultRow, 1
29821 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  , 1);.      }.  
29822 20 20 7d 65 6c 73 65 7b 0a 23 69 66 6e 64 65 66    }else{.#ifndef
29823 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
29824 0a 20 20 20 20 20 20 69 66 28 20 7a 52 69 67 68  .      if( zRigh
29825 74 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20  t[0] ){.        
29826 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 20 20  int rc;.        
29827 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20 20  int res;.       
29828 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41   rc = sqlite3OsA
29829 63 63 65 73 73 28 64 62 2d 3e 70 56 66 73 2c 20  ccess(db->pVfs, 
2982a 7a 52 69 67 68 74 2c 20 53 51 4c 49 54 45 5f 41  zRight, SQLITE_A
2982b 43 43 45 53 53 5f 52 45 41 44 57 52 49 54 45 2c  CCESS_READWRITE,
2982c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20   &res);.        
2982d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2982e 4b 20 7c 7c 20 72 65 73 3d 3d 30 20 29 7b 0a 20  K || res==0 ){. 
2982f 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
29830 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
29831 20 22 6e 6f 74 20 61 20 77 72 69 74 61 62 6c 65   "not a writable
29832 20 64 69 72 65 63 74 6f 72 79 22 29 3b 0a 20 20   directory");.  
29833 20 20 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61          goto pra
29834 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  gma_out;.       
29835 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
29836 20 69 66 28 20 53 51 4c 49 54 45 5f 54 45 4d 50   if( SQLITE_TEMP
29837 5f 53 54 4f 52 45 3d 3d 30 0a 20 20 20 20 20 20  _STORE==0.      
29838 20 7c 7c 20 28 53 51 4c 49 54 45 5f 54 45 4d 50   || (SQLITE_TEMP
29839 5f 53 54 4f 52 45 3d 3d 31 20 26 26 20 64 62 2d  _STORE==1 && db-
2983a 3e 74 65 6d 70 5f 73 74 6f 72 65 3c 3d 31 29 0a  >temp_store<=1).
2983b 20 20 20 20 20 20 20 7c 7c 20 28 53 51 4c 49 54         || (SQLIT
2983c 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 20  E_TEMP_STORE==2 
2983d 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72  && db->temp_stor
2983e 65 3d 3d 31 29 0a 20 20 20 20 20 20 29 7b 0a 20  e==1).      ){. 
2983f 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74         invalidat
29840 65 54 65 6d 70 53 74 6f 72 61 67 65 28 70 50 61  eTempStorage(pPa
29841 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rse);.      }.  
29842 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
29843 28 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69  (sqlite3_temp_di
29844 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20 20 20  rectory);.      
29845 69 66 28 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b  if( zRight[0] ){
29846 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
29847 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20  _temp_directory 
29848 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
29849 70 28 30 2c 20 7a 52 69 67 68 74 29 3b 0a 20 20  p(0, zRight);.  
2984a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2984b 20 20 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f     sqlite3_temp_
2984c 64 69 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a 20  directory = 0;. 
2984d 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a       }.#endif /*
2984e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
2984f 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   */.    }.  }els
29850 65 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  e..#if !defined(
29851 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
29852 43 4b 49 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20  CKING_STYLE).#  
29853 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  if defined(__APP
29854 4c 45 5f 5f 29 0a 23 20 20 20 20 64 65 66 69 6e  LE__).#    defin
29855 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  e SQLITE_ENABLE_
29856 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a  LOCKING_STYLE 1.
29857 23 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66  #  else.#    def
29858 69 6e 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ine SQLITE_ENABL
29859 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
2985a 30 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69  0.#  endif.#endi
2985b 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  f.#if SQLITE_ENA
2985c 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
2985d 45 0a 20 20 2f 2a 0a 20 20 20 2a 2a 20 20 20 50  E.  /*.   **   P
2985e 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
2985f 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65  ]lock_proxy_file
29860 0a 20 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  .   **   PRAGMA 
29861 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f  [database.]lock_
29862 70 72 6f 78 79 5f 66 69 6c 65 20 3d 20 22 3a 61  proxy_file = ":a
29863 75 74 6f 3a 22 7c 22 6c 6f 63 6b 5f 66 69 6c 65  uto:"|"lock_file
29864 5f 70 61 74 68 22 0a 20 20 20 2a 2a 0a 20 20 20  _path".   **.   
29865 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74  ** Return or set
29866 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
29867 65 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c  e lock_proxy_fil
29868 65 20 66 6c 61 67 2e 20 20 43 68 61 6e 67 69 6e  e flag.  Changin
29869 67 0a 20 20 20 2a 2a 20 74 68 65 20 76 61 6c 75  g.   ** the valu
2986a 65 20 73 65 74 73 20 61 20 73 70 65 63 69 66 69  e sets a specifi
2986b 63 20 66 69 6c 65 20 74 6f 20 62 65 20 75 73 65  c file to be use
2986c 64 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 61  d for database a
2986d 63 63 65 73 73 20 6c 6f 63 6b 73 2e 0a 20 20 20  ccess locks..   
2986e 2a 2a 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20 73  **.   */.  if( s
2986f 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
29870 65 66 74 2c 20 22 6c 6f 63 6b 5f 70 72 6f 78 79  eft, "lock_proxy
29871 5f 66 69 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20  _file")==0 ){.  
29872 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
29873 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50  .      Pager *pP
29874 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
29875 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42  reePager(pDb->pB
29876 74 29 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a  t);.      char *
29877 70 72 6f 78 79 5f 66 69 6c 65 5f 70 61 74 68 20  proxy_file_path 
29878 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 73 71  = NULL;.      sq
29879 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
2987a 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  e = sqlite3Pager
2987b 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a 20 20  File(pPager);.  
2987c 20 20 20 20 73 71 6c 69 74 65 33 4f 73 46 69 6c      sqlite3OsFil
2987d 65 43 6f 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20  eControl(pFile, 
2987e 53 51 4c 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50  SQLITE_GET_LOCKP
2987f 52 4f 58 59 46 49 4c 45 2c 20 0a 20 20 20 20 20  ROXYFILE, .     
29880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29881 20 20 20 20 20 20 26 70 72 6f 78 79 5f 66 69 6c        &proxy_fil
29882 65 5f 70 61 74 68 29 3b 0a 20 20 20 20 20 20 0a  e_path);.      .
29883 20 20 20 20 20 20 69 66 28 20 70 72 6f 78 79 5f        if( proxy_
29884 66 69 6c 65 5f 70 61 74 68 20 29 7b 0a 20 20 20  file_path ){.   
29885 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29886 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29  SetNumCols(v, 1)
29887 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29888 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
29889 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
2988a 4d 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  ME, .           
2988b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2988c 20 20 20 22 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66     "lock_proxy_f
2988d 69 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  ile", SQLITE_STA
2988e 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
2988f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
29890 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30  v, OP_String8, 0
29891 2c 20 31 2c 20 30 2c 20 70 72 6f 78 79 5f 66 69  , 1, 0, proxy_fi
29892 6c 65 5f 70 61 74 68 2c 20 30 29 3b 0a 20 20 20  le_path, 0);.   
29893 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29894 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
29895 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20  ultRow, 1, 1);. 
29896 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
29897 7b 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70  {.      Pager *p
29898 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
29899 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70  treePager(pDb->p
2989a 42 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Bt);.      sqlit
2989b 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 20 3d  e3_file *pFile =
2989c 20 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c   sqlite3PagerFil
2989d 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  e(pPager);.     
2989e 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20 20   int res;.      
2989f 69 66 28 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b  if( zRight[0] ){
298a0 0a 20 20 20 20 20 20 20 20 72 65 73 3d 73 71 6c  .        res=sql
298a1 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f  ite3OsFileContro
298a2 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f  l(pFile, SQLITE_
298a3 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  SET_LOCKPROXYFIL
298a4 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  E, .            
298a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
298a6 20 20 20 20 20 20 20 20 20 7a 52 69 67 68 74 29           zRight)
298a7 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b  ;.      } else {
298a8 0a 20 20 20 20 20 20 20 20 72 65 73 3d 73 71 6c  .        res=sql
298a9 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f  ite3OsFileContro
298aa 6c 28 70 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f  l(pFile, SQLITE_
298ab 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  SET_LOCKPROXYFIL
298ac 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  E, .            
298ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
298ae 20 20 20 20 20 20 20 20 20 4e 55 4c 4c 29 3b 0a           NULL);.
298af 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
298b0 28 20 72 65 73 21 3d 53 51 4c 49 54 45 5f 4f 4b  ( res!=SQLITE_OK
298b1 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
298b2 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
298b3 73 65 2c 20 22 66 61 69 6c 65 64 20 74 6f 20 73  se, "failed to s
298b4 65 74 20 6c 6f 63 6b 20 70 72 6f 78 79 20 66 69  et lock proxy fi
298b5 6c 65 22 29 3b 0a 20 20 20 20 20 20 20 20 67 6f  le");.        go
298b6 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
298b7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
298b8 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
298b9 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
298ba 4b 49 4e 47 5f 53 54 59 4c 45 20 2a 2f 20 20 20  KING_STYLE */   
298bb 20 20 20 0a 20 20 20 20 0a 20 20 2f 2a 0a 20 20     .    .  /*.  
298bc 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74  **   PRAGMA [dat
298bd 61 62 61 73 65 2e 5d 73 79 6e 63 68 72 6f 6e 6f  abase.]synchrono
298be 75 73 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  us.  **   PRAGMA
298bf 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 79 6e 63   [database.]sync
298c0 68 72 6f 6e 6f 75 73 3d 4f 46 46 7c 4f 4e 7c 4e  hronous=OFF|ON|N
298c1 4f 52 4d 41 4c 7c 46 55 4c 4c 0a 20 20 2a 2a 0a  ORMAL|FULL.  **.
298c2 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73    ** Return or s
298c3 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c  et the local val
298c4 75 65 20 6f 66 20 74 68 65 20 73 79 6e 63 68 72  ue of the synchr
298c5 6f 6e 6f 75 73 20 66 6c 61 67 2e 20 20 43 68 61  onous flag.  Cha
298c6 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c  nging.  ** the l
298c7 6f 63 61 6c 20 76 61 6c 75 65 20 64 6f 65 73 20  ocal value does 
298c8 6e 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67 65 73  not make changes
298c9 20 74 6f 20 74 68 65 20 64 69 73 6b 20 66 69 6c   to the disk fil
298ca 65 20 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 64  e and the.  ** d
298cb 65 66 61 75 6c 74 20 76 61 6c 75 65 20 77 69 6c  efault value wil
298cc 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 68  l be restored th
298cd 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20  e next time the 
298ce 64 61 74 61 62 61 73 65 20 69 73 0a 20 20 2a 2a  database is.  **
298cf 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20   opened..  */.  
298d0 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
298d1 6d 70 28 7a 4c 65 66 74 2c 22 73 79 6e 63 68 72  mp(zLeft,"synchr
298d2 6f 6e 6f 75 73 22 29 3d 3d 30 20 29 7b 0a 20 20  onous")==0 ){.  
298d3 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
298d4 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
298d5 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
298d6 74 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67  t;.    if( !zRig
298d7 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ht ){.      retu
298d8 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72  rnSingleInt(pPar
298d9 73 65 2c 20 22 73 79 6e 63 68 72 6f 6e 6f 75 73  se, "synchronous
298da 22 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c  ", pDb->safety_l
298db 65 76 65 6c 2d 31 29 3b 0a 20 20 20 20 7d 65 6c  evel-1);.    }el
298dc 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 64  se{.      if( !d
298dd 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b  b->autoCommit ){
298de 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
298df 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
298e0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 53   .            "S
298e1 61 66 65 74 79 20 6c 65 76 65 6c 20 6d 61 79 20  afety level may 
298e2 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 69  not be changed i
298e3 6e 73 69 64 65 20 61 20 74 72 61 6e 73 61 63 74  nside a transact
298e4 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c  ion");.      }el
298e5 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44 62 2d  se{.        pDb-
298e6 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 20 3d 20  >safety_level = 
298e7 67 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 7a  getSafetyLevel(z
298e8 52 69 67 68 74 29 2b 31 3b 0a 20 20 20 20 20 20  Right)+1;.      
298e9 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
298ea 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
298eb 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
298ec 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  MAS */..#ifndef 
298ed 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47  SQLITE_OMIT_FLAG
298ee 5f 50 52 41 47 4d 41 53 0a 20 20 69 66 28 20 66  _PRAGMAS.  if( f
298ef 6c 61 67 50 72 61 67 6d 61 28 70 50 61 72 73 65  lagPragma(pParse
298f0 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68 74 29  , zLeft, zRight)
298f1 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 66   ){.    /* The f
298f2 6c 61 67 50 72 61 67 6d 61 28 29 20 73 75 62 72  lagPragma() subr
298f3 6f 75 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65  outine also gene
298f4 72 61 74 65 73 20 61 6e 79 20 6e 65 63 65 73 73  rates any necess
298f5 61 72 79 20 63 6f 64 65 0a 20 20 20 20 2a 2a 20  ary code.    ** 
298f6 74 68 65 72 65 20 69 73 20 6e 6f 74 68 69 6e 67  there is nothing
298f7 20 6d 6f 72 65 20 74 6f 20 64 6f 20 68 65 72 65   more to do here
298f8 20 2a 2f 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64   */.  }else.#end
298f9 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
298fa 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 20 2a  T_FLAG_PRAGMAS *
298fb 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
298fc 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50 52  E_OMIT_SCHEMA_PR
298fd 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20  AGMAS.  /*.  ** 
298fe 20 20 50 52 41 47 4d 41 20 74 61 62 6c 65 5f 69    PRAGMA table_i
298ff 6e 66 6f 28 3c 74 61 62 6c 65 3e 29 0a 20 20 2a  nfo(<table>).  *
29900 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 61 20  *.  ** Return a 
29901 73 69 6e 67 6c 65 20 72 6f 77 20 66 6f 72 20 65  single row for e
29902 61 63 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  ach column of th
29903 65 20 6e 61 6d 65 64 20 74 61 62 6c 65 2e 20 54  e named table. T
29904 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20  he columns of.  
29905 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  ** the returned 
29906 64 61 74 61 20 73 65 74 20 61 72 65 3a 0a 20 20  data set are:.  
29907 2a 2a 0a 20 20 2a 2a 20 63 69 64 3a 20 20 20 20  **.  ** cid:    
29908 20 20 20 20 43 6f 6c 75 6d 6e 20 69 64 20 28 6e      Column id (n
29909 75 6d 62 65 72 65 64 20 66 72 6f 6d 20 6c 65 66  umbered from lef
2990a 74 20 74 6f 20 72 69 67 68 74 2c 20 73 74 61 72  t to right, star
2990b 74 69 6e 67 20 61 74 20 30 29 0a 20 20 2a 2a 20  ting at 0).  ** 
2990c 6e 61 6d 65 3a 20 20 20 20 20 20 20 43 6f 6c 75  name:       Colu
2990d 6d 6e 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 79 70  mn name.  ** typ
2990e 65 3a 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  e:       Column 
2990f 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
29910 2e 0a 20 20 2a 2a 20 6e 6f 74 6e 75 6c 6c 3a 20  ..  ** notnull: 
29911 20 20 20 54 72 75 65 20 69 66 20 27 4e 4f 54 20     True if 'NOT 
29912 4e 55 4c 4c 27 20 69 73 20 70 61 72 74 20 6f 66  NULL' is part of
29913 20 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74   column declarat
29914 69 6f 6e 0a 20 20 2a 2a 20 64 66 6c 74 5f 76 61  ion.  ** dflt_va
29915 6c 75 65 3a 20 54 68 65 20 64 65 66 61 75 6c 74  lue: The default
29916 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63   value for the c
29917 6f 6c 75 6d 6e 2c 20 69 66 20 61 6e 79 2e 0a 20  olumn, if any.. 
29918 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
29919 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
2991a 22 74 61 62 6c 65 5f 69 6e 66 6f 22 29 3d 3d 30  "table_info")==0
2991b 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20   && zRight ){.  
2991c 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
2991d 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
2991e 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29  adSchema(pParse)
2991f 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f   ) goto pragma_o
29920 75 74 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73  ut;.    pTab = s
29921 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
29922 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29  db, zRight, zDb)
29923 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 29  ;.    if( pTab )
29924 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  {.      int i;. 
29925 20 20 20 20 20 69 6e 74 20 6e 48 69 64 64 65 6e       int nHidden
29926 20 3d 20 30 3b 0a 20 20 20 20 20 20 43 6f 6c 75   = 0;.      Colu
29927 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 20 20 20 20  mn *pCol;.      
29928 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
29929 6d 43 6f 6c 73 28 76 2c 20 36 29 3b 0a 20 20 20  mCols(v, 6);.   
2992a 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
2992b 3d 20 36 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 6;.      sqlit
2992c 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
2992d 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
2992e 41 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c 49  AME, "cid", SQLI
2992f 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
29930 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29931 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f  ColName(v, 1, CO
29932 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d  LNAME_NAME, "nam
29933 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
29934 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
29935 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
29936 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 2, COLNAME_NA
29937 4d 45 2c 20 22 74 79 70 65 22 2c 20 53 51 4c 49  ME, "type", SQLI
29938 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
29939 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2993a 43 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f  ColName(v, 3, CO
2993b 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 6f 74  LNAME_NAME, "not
2993c 6e 75 6c 6c 22 2c 20 53 51 4c 49 54 45 5f 53 54  null", SQLITE_ST
2993d 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
2993e 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2993f 6d 65 28 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45  me(v, 4, COLNAME
29940 5f 4e 41 4d 45 2c 20 22 64 66 6c 74 5f 76 61 6c  _NAME, "dflt_val
29941 75 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ue", SQLITE_STAT
29942 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
29943 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
29944 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 5, COLNAME_N
29945 41 4d 45 2c 20 22 70 6b 22 2c 20 53 51 4c 49 54  AME, "pk", SQLIT
29946 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
29947 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
29948 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73  olumnNames(pPars
29949 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20  e, pTab);.      
2994a 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 70 54  for(i=0, pCol=pT
2994b 61 62 2d 3e 61 43 6f 6c 3b 20 69 3c 70 54 61 62  ab->aCol; i<pTab
2994c 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f  ->nCol; i++, pCo
2994d 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  l++){.        if
2994e 28 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e  ( IsHiddenColumn
2994f 28 70 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20  (pCol) ){.      
29950 20 20 20 20 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20      nHidden++;. 
29951 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75           continu
29952 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  e;.        }.   
29953 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29954 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
29955 65 67 65 72 2c 20 69 2d 6e 48 69 64 64 65 6e 2c  eger, i-nHidden,
29956 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
29957 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
29958 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
29959 20 32 2c 20 30 2c 20 70 43 6f 6c 2d 3e 7a 4e 61   2, 0, pCol->zNa
2995a 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  me, 0);.        
2995b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2995c 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
2995d 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20 20 20   0, 3, 0,.      
2995e 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65       pCol->zType
2995f 20 3f 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3a   ? pCol->zType :
29960 20 22 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20   "", 0);.       
29961 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29962 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
29963 2c 20 28 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c  , (pCol->notNull
29964 20 3f 20 31 20 3a 20 30 29 2c 20 34 29 3b 0a 20   ? 1 : 0), 4);. 
29965 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d         if( pCol-
29966 3e 7a 44 66 6c 74 20 29 7b 0a 20 20 20 20 20 20  >zDflt ){.      
29967 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29968 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
29969 6e 67 38 2c 20 30 2c 20 35 2c 20 30 2c 20 28 63  ng8, 0, 5, 0, (c
2996a 68 61 72 2a 29 70 43 6f 6c 2d 3e 7a 44 66 6c 74  har*)pCol->zDflt
2996b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  , 0);.        }e
2996c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 73  lse{.          s
2996d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2996e 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2996f 35 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  5);.        }.  
29970 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29971 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
29972 74 65 67 65 72 2c 20 70 43 6f 6c 2d 3e 69 73 50  teger, pCol->isP
29973 72 69 6d 4b 65 79 2c 20 36 29 3b 0a 20 20 20 20  rimKey, 6);.    
29974 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29975 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
29976 6c 74 52 6f 77 2c 20 31 2c 20 36 29 3b 0a 20 20  ltRow, 1, 6);.  
29977 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
29978 6c 73 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  lse..  if( sqlit
29979 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
2997a 20 22 69 6e 64 65 78 5f 69 6e 66 6f 22 29 3d 3d   "index_info")==
2997b 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20  0 && zRight ){. 
2997c 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
2997d 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b      Table *pTab;
2997e 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2997f 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
29980 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
29981 5f 6f 75 74 3b 0a 20 20 20 20 70 49 64 78 20 3d  _out;.    pIdx =
29982 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65   sqlite3FindInde
29983 78 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44  x(db, zRight, zD
29984 62 29 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78  b);.    if( pIdx
29985 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   ){.      int i;
29986 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 49  .      pTab = pI
29987 64 78 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20  dx->pTable;.    
29988 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29989 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20  NumCols(v, 3);. 
2998a 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
2998b 6d 20 3d 20 33 3b 0a 20 20 20 20 20 20 73 71 6c  m = 3;.      sql
2998c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
2998d 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
2998e 5f 4e 41 4d 45 2c 20 22 73 65 71 6e 6f 22 2c 20  _NAME, "seqno", 
2998f 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
29990 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29991 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31  eSetColName(v, 1
29992 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
29993 22 63 69 64 22 2c 20 53 51 4c 49 54 45 5f 53 54  "cid", SQLITE_ST
29994 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
29995 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
29996 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45  me(v, 2, COLNAME
29997 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53  _NAME, "name", S
29998 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
29999 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
2999a 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69  pIdx->nColumn; i
2999b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ++){.        int
2999c 20 63 6e 75 6d 20 3d 20 70 49 64 78 2d 3e 61 69   cnum = pIdx->ai
2999d 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20  Column[i];.     
2999e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2999f 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
299a0 65 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20 20  er, i, 1);.     
299a1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
299a2 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
299a3 65 72 2c 20 63 6e 75 6d 2c 20 32 29 3b 0a 20 20  er, cnum, 2);.  
299a4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54        assert( pT
299a5 61 62 2d 3e 6e 43 6f 6c 3e 63 6e 75 6d 20 29 3b  ab->nCol>cnum );
299a6 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
299a7 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
299a8 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20  _String8, 0, 3, 
299a9 30 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 63 6e  0, pTab->aCol[cn
299aa 75 6d 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  um].zName, 0);. 
299ab 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
299ac 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
299ad 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29 3b  esultRow, 1, 3);
299ae 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
299af 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71   }else..  if( sq
299b0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
299b1 66 74 2c 20 22 69 6e 64 65 78 5f 6c 69 73 74 22  ft, "index_list"
299b2 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29  )==0 && zRight )
299b3 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  {.    Index *pId
299b4 78 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  x;.    Table *pT
299b5 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ab;.    if( sqli
299b6 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
299b7 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
299b8 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54 61  gma_out;.    pTa
299b9 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54  b = sqlite3FindT
299ba 61 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74 2c  able(db, zRight,
299bb 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20 70   zDb);.    if( p
299bc 54 61 62 20 29 7b 0a 20 20 20 20 20 20 76 20 3d  Tab ){.      v =
299bd 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
299be 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70  pParse);.      p
299bf 49 64 78 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64  Idx = pTab->pInd
299c0 65 78 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  ex;.      if( pI
299c1 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  dx ){.        in
299c2 74 20 69 20 3d 20 30 3b 20 0a 20 20 20 20 20 20  t i = 0; .      
299c3 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
299c4 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b 0a 20  NumCols(v, 3);. 
299c5 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e         pParse->n
299c6 4d 65 6d 20 3d 20 33 3b 0a 20 20 20 20 20 20 20  Mem = 3;.       
299c7 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
299c8 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
299c9 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22  NAME_NAME, "seq"
299ca 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
299cb 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
299cc 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
299cd 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 1, COLNAME_NA
299ce 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49  ME, "name", SQLI
299cf 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
299d0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
299d1 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20  etColName(v, 2, 
299d2 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 75  COLNAME_NAME, "u
299d3 6e 69 71 75 65 22 2c 20 53 51 4c 49 54 45 5f 53  nique", SQLITE_S
299d4 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
299d5 77 68 69 6c 65 28 70 49 64 78 29 7b 0a 20 20 20  while(pIdx){.   
299d6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
299d7 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
299d8 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20  nteger, i, 1);. 
299d9 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
299da 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
299db 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20  _String8, 0, 2, 
299dc 30 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20  0, pIdx->zName, 
299dd 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  0);.          sq
299de 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
299df 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70  v, OP_Integer, p
299e0 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45  Idx->onError!=OE
299e1 5f 4e 6f 6e 65 2c 20 33 29 3b 0a 20 20 20 20 20  _None, 3);.     
299e2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
299e3 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
299e4 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20  ultRow, 1, 3);. 
299e5 20 20 20 20 20 20 20 20 20 2b 2b 69 3b 0a 20 20           ++i;.  
299e6 20 20 20 20 20 20 20 20 70 49 64 78 20 3d 20 70          pIdx = p
299e7 49 64 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Idx->pNext;.    
299e8 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
299e9 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69    }.  }else..  i
299ea 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
299eb 70 28 7a 4c 65 66 74 2c 20 22 64 61 74 61 62 61  p(zLeft, "databa
299ec 73 65 5f 6c 69 73 74 22 29 3d 3d 30 20 29 7b 0a  se_list")==0 ){.
299ed 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 69      int i;.    i
299ee 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
299ef 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
299f0 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
299f1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
299f2 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 33 29 3b  etNumCols(v, 3);
299f3 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
299f4 6d 20 3d 20 33 3b 0a 20 20 20 20 73 71 6c 69 74  m = 3;.    sqlit
299f5 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
299f6 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
299f7 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49  AME, "seq", SQLI
299f8 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
299f9 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
299fa 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
299fb 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
299fc 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
299fd 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
299fe 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32  eSetColName(v, 2
299ff 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
29a00 22 66 69 6c 65 22 2c 20 53 51 4c 49 54 45 5f 53  "file", SQLITE_S
29a01 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f 72 28  TATIC);.    for(
29a02 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
29a03 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
29a04 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3d 3d  db->aDb[i].pBt==
29a05 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
29a06 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
29a07 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 21 3d 30 20  aDb[i].zName!=0 
29a08 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29a09 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29a0a 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b  _Integer, i, 1);
29a0b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29a0c 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
29a0d 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30 2c  tring8, 0, 2, 0,
29a0e 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d   db->aDb[i].zNam
29a0f 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  e, 0);.      sql
29a10 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
29a11 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
29a12 20 33 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20   3, 0,.         
29a13 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65    sqlite3BtreeGe
29a14 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44  tFilename(db->aD
29a15 62 5b 69 5d 2e 70 42 74 29 2c 20 30 29 3b 0a 20  b[i].pBt), 0);. 
29a16 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29a17 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
29a18 75 6c 74 52 6f 77 2c 20 31 2c 20 33 29 3b 0a 20  ultRow, 1, 3);. 
29a19 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20     }.  }else..  
29a1a 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
29a1b 6d 70 28 7a 4c 65 66 74 2c 20 22 63 6f 6c 6c 61  mp(zLeft, "colla
29a1c 74 69 6f 6e 5f 6c 69 73 74 22 29 3d 3d 30 20 29  tion_list")==0 )
29a1d 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b  {.    int i = 0;
29a1e 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70  .    HashElem *p
29a1f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
29a20 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 32  eSetNumCols(v, 2
29a21 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  );.    pParse->n
29a22 4d 65 6d 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c  Mem = 2;.    sql
29a23 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
29a24 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
29a25 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51  _NAME, "seq", SQ
29a26 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
29a27 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29a28 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f  ColName(v, 1, CO
29a29 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d  LNAME_NAME, "nam
29a2a 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
29a2b 43 29 3b 0a 20 20 20 20 66 6f 72 28 70 3d 73 71  C);.    for(p=sq
29a2c 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 64  liteHashFirst(&d
29a2d 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 20 70 3b  b->aCollSeq); p;
29a2e 20 70 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78   p=sqliteHashNex
29a2f 74 28 70 29 29 7b 0a 20 20 20 20 20 20 43 6f 6c  t(p)){.      Col
29a30 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 28 43  lSeq *pColl = (C
29a31 6f 6c 6c 53 65 71 20 2a 29 73 71 6c 69 74 65 48  ollSeq *)sqliteH
29a32 61 73 68 44 61 74 61 28 70 29 3b 0a 20 20 20 20  ashData(p);.    
29a33 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29a34 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
29a35 72 2c 20 69 2b 2b 2c 20 31 29 3b 0a 20 20 20 20  r, i++, 1);.    
29a36 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29a37 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
29a38 38 2c 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c  8, 0, 2, 0, pCol
29a39 6c 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  l->zName, 0);.  
29a3a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29a3b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
29a3c 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b 0a 20 20  ltRow, 1, 2);.  
29a3d 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64    }.  }else.#end
29a3e 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
29a3f 54 5f 53 43 48 45 4d 41 5f 50 52 41 47 4d 41 53  T_SCHEMA_PRAGMAS
29a40 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
29a41 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
29a42 5f 4b 45 59 0a 20 20 69 66 28 20 73 71 6c 69 74  _KEY.  if( sqlit
29a43 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29a44 20 22 66 6f 72 65 69 67 6e 5f 6b 65 79 5f 6c 69   "foreign_key_li
29a45 73 74 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68  st")==0 && zRigh
29a46 74 20 29 7b 0a 20 20 20 20 46 4b 65 79 20 2a 70  t ){.    FKey *p
29a47 46 4b 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  FK;.    Table *p
29a48 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Tab;.    if( sql
29a49 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
29a4a 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
29a4b 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54  agma_out;.    pT
29a4c 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ab = sqlite3Find
29a4d 54 61 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74  Table(db, zRight
29a4e 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20  , zDb);.    if( 
29a4f 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 76 20  pTab ){.      v 
29a50 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
29a51 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
29a52 70 46 4b 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65  pFK = pTab->pFKe
29a53 79 3b 0a 20 20 20 20 20 20 69 66 28 20 70 46 4b  y;.      if( pFK
29a54 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
29a55 69 20 3d 20 30 3b 20 0a 20 20 20 20 20 20 20 20  i = 0; .        
29a56 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
29a57 6d 43 6f 6c 73 28 76 2c 20 38 29 3b 0a 20 20 20  mCols(v, 8);.   
29a58 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65       pParse->nMe
29a59 6d 20 3d 20 38 3b 0a 20 20 20 20 20 20 20 20 73  m = 8;.        s
29a5a 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
29a5b 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
29a5c 4d 45 5f 4e 41 4d 45 2c 20 22 69 64 22 2c 20 53  ME_NAME, "id", S
29a5d 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
29a5e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29a5f 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
29a60 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
29a61 20 22 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53   "seq", SQLITE_S
29a62 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
29a63 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29a64 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e  lName(v, 2, COLN
29a65 41 4d 45 5f 4e 41 4d 45 2c 20 22 74 61 62 6c 65  AME_NAME, "table
29a66 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
29a67 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
29a68 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
29a69 28 76 2c 20 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 3, COLNAME_N
29a6a 41 4d 45 2c 20 22 66 72 6f 6d 22 2c 20 53 51 4c  AME, "from", SQL
29a6b 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
29a6c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29a6d 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c  SetColName(v, 4,
29a6e 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
29a6f 74 6f 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  to", SQLITE_STAT
29a70 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  IC);.        sql
29a71 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
29a72 6d 65 28 76 2c 20 35 2c 20 43 4f 4c 4e 41 4d 45  me(v, 5, COLNAME
29a73 5f 4e 41 4d 45 2c 20 22 6f 6e 5f 75 70 64 61 74  _NAME, "on_updat
29a74 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
29a75 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  C);.        sqli
29a76 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
29a77 65 28 76 2c 20 36 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 6, COLNAME_
29a78 4e 41 4d 45 2c 20 22 6f 6e 5f 64 65 6c 65 74 65  NAME, "on_delete
29a79 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
29a7a 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
29a7b 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
29a7c 28 76 2c 20 37 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 7, COLNAME_N
29a7d 41 4d 45 2c 20 22 6d 61 74 63 68 22 2c 20 53 51  AME, "match", SQ
29a7e 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
29a7f 20 20 20 20 20 20 77 68 69 6c 65 28 70 46 4b 29        while(pFK)
29a80 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20  {.          int 
29a81 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  j;.          for
29a82 28 6a 3d 30 3b 20 6a 3c 70 46 4b 2d 3e 6e 43 6f  (j=0; j<pFK->nCo
29a83 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; j++){.       
29a84 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20       char *zCol 
29a85 3d 20 70 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a  = pFK->aCol[j].z
29a86 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Col;.           
29a87 20 63 68 61 72 20 2a 7a 4f 6e 44 65 6c 65 74 65   char *zOnDelete
29a88 20 3d 20 28 63 68 61 72 20 2a 29 61 63 74 69 6f   = (char *)actio
29a89 6e 4e 61 6d 65 28 70 46 4b 2d 3e 61 41 63 74 69  nName(pFK->aActi
29a8a 6f 6e 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20  on[0]);.        
29a8b 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e 55 70 64      char *zOnUpd
29a8c 61 74 65 20 3d 20 28 63 68 61 72 20 2a 29 61 63  ate = (char *)ac
29a8d 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d 3e 61 41  tionName(pFK->aA
29a8e 63 74 69 6f 6e 5b 31 5d 29 3b 0a 20 20 20 20 20  ction[1]);.     
29a8f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29a90 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
29a91 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b 0a 20  nteger, i, 1);. 
29a92 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
29a93 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29a94 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6a 2c 20 32  OP_Integer, j, 2
29a95 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
29a96 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
29a97 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
29a98 30 2c 20 33 2c 20 30 2c 20 70 46 4b 2d 3e 7a 54  0, 3, 0, pFK->zT
29a99 6f 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  o, 0);.         
29a9a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29a9b 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
29a9c 67 38 2c 20 30 2c 20 34 2c 20 30 2c 0a 20 20 20  g8, 0, 4, 0,.   
29a9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29a9e 20 20 20 20 20 20 20 20 20 20 20 70 54 61 62 2d             pTab-
29a9f 3e 61 43 6f 6c 5b 70 46 4b 2d 3e 61 43 6f 6c 5b  >aCol[pFK->aCol[
29aa0 6a 5d 2e 69 46 72 6f 6d 5d 2e 7a 4e 61 6d 65 2c  j].iFrom].zName,
29aa1 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
29aa2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29aa3 70 34 28 76 2c 20 7a 43 6f 6c 20 3f 20 4f 50 5f  p4(v, zCol ? OP_
29aa4 53 74 72 69 6e 67 38 20 3a 20 4f 50 5f 4e 75 6c  String8 : OP_Nul
29aa5 6c 2c 20 30 2c 20 35 2c 20 30 2c 20 7a 43 6f 6c  l, 0, 5, 0, zCol
29aa6 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
29aa7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29aa8 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
29aa9 38 2c 20 30 2c 20 36 2c 20 30 2c 20 7a 4f 6e 55  8, 0, 6, 0, zOnU
29aaa 70 64 61 74 65 2c 20 30 29 3b 0a 20 20 20 20 20  pdate, 0);.     
29aab 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29aac 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
29aad 74 72 69 6e 67 38 2c 20 30 2c 20 37 2c 20 30 2c  tring8, 0, 7, 0,
29aae 20 7a 4f 6e 44 65 6c 65 74 65 2c 20 30 29 3b 0a   zOnDelete, 0);.
29aaf 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
29ab0 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
29ab1 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
29ab2 38 2c 20 30 2c 20 22 4e 4f 4e 45 22 2c 20 30 29  8, 0, "NONE", 0)
29ab3 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
29ab4 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29ab5 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
29ab6 20 31 2c 20 38 29 3b 0a 20 20 20 20 20 20 20 20   1, 8);.        
29ab7 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2b 2b    }.          ++
29ab8 69 3b 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b  i;.          pFK
29ab9 20 3d 20 70 46 4b 2d 3e 70 4e 65 78 74 46 72 6f   = pFK->pNextFro
29aba 6d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  m;.        }.   
29abb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
29abc 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  se.#endif /* !de
29abd 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
29abe 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 2a  T_FOREIGN_KEY) *
29abf 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  /..#ifndef NDEBU
29ac0 47 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  G.  if( sqlite3S
29ac1 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 70  trICmp(zLeft, "p
29ac2 61 72 73 65 72 5f 74 72 61 63 65 22 29 3d 3d 30  arser_trace")==0
29ac3 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 52 69 67   ){.    if( zRig
29ac4 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ht ){.      if( 
29ac5 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68  getBoolean(zRigh
29ac6 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  t) ){.        sq
29ac7 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65  lite3ParserTrace
29ac8 28 73 74 64 65 72 72 2c 20 22 70 61 72 73 65 72  (stderr, "parser
29ac9 3a 20 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  : ");.      }els
29aca 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e{.        sqlit
29acb 65 33 50 61 72 73 65 72 54 72 61 63 65 28 30 2c  e3ParserTrace(0,
29acc 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
29acd 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
29ace 66 0a 0a 20 20 2f 2a 20 52 65 69 6e 73 74 61 6c  f..  /* Reinstal
29acf 6c 20 74 68 65 20 4c 49 4b 45 20 61 6e 64 20 47  l the LIKE and G
29ad0 4c 4f 42 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  LOB functions.  
29ad1 54 68 65 20 76 61 72 69 61 6e 74 20 6f 66 20 4c  The variant of L
29ad2 49 4b 45 0a 20 20 2a 2a 20 75 73 65 64 20 77 69  IKE.  ** used wi
29ad3 6c 6c 20 62 65 20 63 61 73 65 20 73 65 6e 73 69  ll be case sensi
29ad4 74 69 76 65 20 6f 72 20 6e 6f 74 20 64 65 70 65  tive or not depe
29ad5 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 52 48 53  nding on the RHS
29ad6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
29ad7 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29ad8 74 2c 20 22 63 61 73 65 5f 73 65 6e 73 69 74 69  t, "case_sensiti
29ad9 76 65 5f 6c 69 6b 65 22 29 3d 3d 30 20 29 7b 0a  ve_like")==0 ){.
29ada 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 29      if( zRight )
29adb 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
29adc 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74  egisterLikeFunct
29add 69 6f 6e 73 28 64 62 2c 20 67 65 74 42 6f 6f 6c  ions(db, getBool
29ade 65 61 6e 28 7a 52 69 67 68 74 29 29 3b 0a 20 20  ean(zRight));.  
29adf 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 23 69 66    }.  }else..#if
29ae0 6e 64 65 66 20 53 51 4c 49 54 45 5f 49 4e 54 45  ndef SQLITE_INTE
29ae1 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f  GRITY_CHECK_ERRO
29ae2 52 5f 4d 41 58 0a 23 20 64 65 66 69 6e 65 20 53  R_MAX.# define S
29ae3 51 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f  QLITE_INTEGRITY_
29ae4 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 20  CHECK_ERROR_MAX 
29ae5 31 30 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  100.#endif..#ifn
29ae6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
29ae7 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a  INTEGRITY_CHECK.
29ae8 20 20 2f 2a 20 50 72 61 67 6d 61 20 22 71 75 69    /* Pragma "qui
29ae9 63 6b 5f 63 68 65 63 6b 22 20 69 73 20 61 6e 20  ck_check" is an 
29aea 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 72 65 64  experimental red
29aeb 75 63 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  uced version of 
29aec 0a 20 20 2a 2a 20 69 6e 74 65 67 72 69 74 79 5f  .  ** integrity_
29aed 63 68 65 63 6b 20 64 65 73 69 67 6e 65 64 20 74  check designed t
29aee 6f 20 64 65 74 65 63 74 20 6d 6f 73 74 20 64 61  o detect most da
29aef 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
29af0 6e 0a 20 20 2a 2a 20 77 69 74 68 6f 75 74 20 6d  n.  ** without m
29af1 6f 73 74 20 6f 66 20 74 68 65 20 6f 76 65 72 68  ost of the overh
29af2 65 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 69 6e  ead of a full in
29af3 74 65 67 72 69 74 79 2d 63 68 65 63 6b 2e 0a 20  tegrity-check.. 
29af4 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
29af5 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
29af6 22 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b  "integrity_check
29af7 22 29 3d 3d 30 0a 20 20 20 7c 7c 20 73 71 6c 69  ")==0.   || sqli
29af8 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
29af9 2c 20 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 29  , "quick_check")
29afa 3d 3d 30 20 0a 20 20 29 7b 0a 20 20 20 20 69 6e  ==0 .  ){.    in
29afb 74 20 69 2c 20 6a 2c 20 61 64 64 72 2c 20 6d 78  t i, j, addr, mx
29afc 45 72 72 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 64  Err;..    /* Cod
29afd 65 20 74 68 61 74 20 61 70 70 65 61 72 73 20 61  e that appears a
29afe 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
29aff 20 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b   integrity check
29b00 2e 20 20 49 66 20 6e 6f 20 65 72 72 6f 72 0a 20  .  If no error. 
29b01 20 20 20 2a 2a 20 6d 65 73 73 61 67 65 73 20 68     ** messages h
29b02 61 76 65 20 62 65 65 6e 20 67 65 6e 65 72 61 74  ave been generat
29b03 65 64 2c 20 6f 75 74 70 75 74 20 4f 4b 2e 20 20  ed, output OK.  
29b04 4f 74 68 65 72 77 69 73 65 20 6f 75 74 70 75 74  Otherwise output
29b05 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 72 72 6f   the.    ** erro
29b06 72 20 6d 65 73 73 61 67 65 0a 20 20 20 20 2a 2f  r message.    */
29b07 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
29b08 74 20 56 64 62 65 4f 70 4c 69 73 74 20 65 6e 64  t VdbeOpList end
29b09 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  Code[] = {.     
29b0a 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20   { OP_AddImm,   
29b0b 20 20 20 31 2c 20 30 2c 20 20 20 20 20 20 20 20     1, 0,        
29b0c 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20  0},    /* 0 */. 
29b0d 20 20 20 20 20 7b 20 4f 50 5f 49 66 4e 65 67 2c       { OP_IfNeg,
29b0e 20 20 20 20 20 20 20 31 2c 20 30 2c 20 20 20 20         1, 0,    
29b0f 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20      0},    /* 1 
29b10 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 53 74  */.      { OP_St
29b11 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 33 2c  ring8,     0, 3,
29b12 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f          0},    /
29b13 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f  * 2 */.      { O
29b14 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 33  P_ResultRow,   3
29b15 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a  , 1,        0},.
29b16 20 20 20 20 7d 3b 0a 0a 20 20 20 20 69 6e 74 20      };..    int 
29b17 69 73 51 75 69 63 6b 20 3d 20 28 7a 4c 65 66 74  isQuick = (zLeft
29b18 5b 30 5d 3d 3d 27 71 27 29 3b 0a 0a 20 20 20 20  [0]=='q');..    
29b19 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  /* Initialize th
29b1a 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 2a  e VDBE program *
29b1b 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  /.    if( sqlite
29b1c 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
29b1d 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d  se) ) goto pragm
29b1e 61 5f 6f 75 74 3b 0a 20 20 20 20 70 50 61 72 73  a_out;.    pPars
29b1f 65 2d 3e 6e 4d 65 6d 20 3d 20 36 3b 0a 20 20 20  e->nMem = 6;.   
29b20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
29b21 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
29b22 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29b23 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f  ColName(v, 0, CO
29b24 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 69 6e 74  LNAME_NAME, "int
29b25 65 67 72 69 74 79 5f 63 68 65 63 6b 22 2c 20 53  egrity_check", S
29b26 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 0a  QLITE_STATIC);..
29b27 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d      /* Set the m
29b28 61 78 69 6d 75 6d 20 65 72 72 6f 72 20 63 6f 75  aximum error cou
29b29 6e 74 20 2a 2f 0a 20 20 20 20 6d 78 45 72 72 20  nt */.    mxErr 
29b2a 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49  = SQLITE_INTEGRI
29b2b 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d  TY_CHECK_ERROR_M
29b2c 41 58 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67  AX;.    if( zRig
29b2d 68 74 20 29 7b 0a 20 20 20 20 20 20 6d 78 45 72  ht ){.      mxEr
29b2e 72 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29  r = atoi(zRight)
29b2f 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 78 45 72  ;.      if( mxEr
29b30 72 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  r<=0 ){.        
29b31 6d 78 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49  mxErr = SQLITE_I
29b32 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45  NTEGRITY_CHECK_E
29b33 52 52 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 20 20  RROR_MAX;.      
29b34 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
29b35 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29b36 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6d 78 45   OP_Integer, mxE
29b37 72 72 2c 20 31 29 3b 20 20 2f 2a 20 72 65 67 5b  rr, 1);  /* reg[
29b38 31 5d 20 68 6f 6c 64 73 20 65 72 72 6f 72 73 20  1] holds errors 
29b39 6c 65 66 74 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  left */..    /* 
29b3a 44 6f 20 61 6e 20 69 6e 74 65 67 72 69 74 79 20  Do an integrity 
29b3b 63 68 65 63 6b 20 6f 6e 20 65 61 63 68 20 64 61  check on each da
29b3c 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20  tabase file */. 
29b3d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
29b3e 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
29b3f 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 78 3b 0a     HashElem *x;.
29b40 20 20 20 20 20 20 48 61 73 68 20 2a 70 54 62 6c        Hash *pTbl
29b41 73 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6e 74  s;.      int cnt
29b42 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 69 66 28   = 0;..      if(
29b43 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20   OMIT_TEMPDB && 
29b44 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b  i==1 ) continue;
29b45 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43  ..      sqlite3C
29b46 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
29b47 70 50 61 72 73 65 2c 20 69 29 3b 0a 20 20 20 20  pParse, i);.    
29b48 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
29b49 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
29b4a 5f 49 66 50 6f 73 2c 20 31 29 3b 20 2f 2a 20 48  _IfPos, 1); /* H
29b4b 61 6c 74 20 69 66 20 6f 75 74 20 6f 66 20 65 72  alt if out of er
29b4c 72 6f 72 73 20 2a 2f 0a 20 20 20 20 20 20 73 71  rors */.      sq
29b4d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29b4e 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30  v, OP_Halt, 0, 0
29b4f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29b50 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
29b51 61 64 64 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  addr);..      /*
29b52 20 44 6f 20 61 6e 20 69 6e 74 65 67 72 69 74 79   Do an integrity
29b53 20 63 68 65 63 6b 20 6f 66 20 74 68 65 20 42 2d   check of the B-
29b54 54 72 65 65 0a 20 20 20 20 20 20 2a 2a 0a 20 20  Tree.      **.  
29b55 20 20 20 20 2a 2a 20 42 65 67 69 6e 20 62 79 20      ** Begin by 
29b56 66 69 6c 6c 69 6e 67 20 72 65 67 69 73 74 65 72  filling register
29b57 73 20 32 2c 20 33 2c 20 2e 2e 2e 20 77 69 74 68  s 2, 3, ... with
29b58 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 73 20   the root pages 
29b59 6e 75 6d 62 65 72 73 0a 20 20 20 20 20 20 2a 2a  numbers.      **
29b5a 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20   for all tables 
29b5b 61 6e 64 20 69 6e 64 69 63 65 73 20 69 6e 20 74  and indices in t
29b5c 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20  he database..   
29b5d 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 54 62 6c     */.      pTbl
29b5e 73 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 2e  s = &db->aDb[i].
29b5f 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
29b60 3b 0a 20 20 20 20 20 20 66 6f 72 28 78 3d 73 71  ;.      for(x=sq
29b61 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 70 54  liteHashFirst(pT
29b62 62 6c 73 29 3b 20 78 3b 20 78 3d 73 71 6c 69 74  bls); x; x=sqlit
29b63 65 48 61 73 68 4e 65 78 74 28 78 29 29 7b 0a 20  eHashNext(x)){. 
29b64 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54         Table *pT
29b65 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44  ab = sqliteHashD
29b66 61 74 61 28 78 29 3b 0a 20 20 20 20 20 20 20 20  ata(x);.        
29b67 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20  Index *pIdx;.   
29b68 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29b69 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
29b6a 65 67 65 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d  eger, pTab->tnum
29b6b 2c 20 32 2b 63 6e 74 29 3b 0a 20 20 20 20 20 20  , 2+cnt);.      
29b6c 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20    cnt++;.       
29b6d 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
29b6e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
29b6f 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
29b70 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
29b71 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29b72 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 49 64 78  OP_Integer, pIdx
29b73 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a  ->tnum, 2+cnt);.
29b74 20 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b            cnt++;
29b75 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29b76 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6b   }..      /* Mak
29b77 65 20 73 75 72 65 20 73 75 66 66 69 63 69 65 6e  e sure sufficien
29b78 74 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 67 69  t number of regi
29b79 73 74 65 72 73 20 68 61 76 65 20 62 65 65 6e 20  sters have been 
29b7a 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 20  allocated */.   
29b7b 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e     if( pParse->n
29b7c 4d 65 6d 20 3c 20 63 6e 74 2b 34 20 29 7b 0a 20  Mem < cnt+4 ){. 
29b7d 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e         pParse->n
29b7e 4d 65 6d 20 3d 20 63 6e 74 2b 34 3b 0a 20 20 20  Mem = cnt+4;.   
29b7f 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 44     }..      /* D
29b80 6f 20 74 68 65 20 62 2d 74 72 65 65 20 69 6e 74  o the b-tree int
29b81 65 67 72 69 74 79 20 63 68 65 63 6b 73 20 2a 2f  egrity checks */
29b82 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29b83 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
29b84 6e 74 65 67 72 69 74 79 43 6b 2c 20 32 2c 20 63  ntegrityCk, 2, c
29b85 6e 74 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  nt, 1);.      sq
29b86 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
29b87 35 28 76 2c 20 28 75 38 29 69 29 3b 0a 20 20 20  5(v, (u8)i);.   
29b88 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
29b89 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
29b8a 50 5f 49 73 4e 75 6c 6c 2c 20 32 29 3b 0a 20 20  P_IsNull, 2);.  
29b8b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29b8c 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
29b8d 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20  ng8, 0, 3, 0,.  
29b8e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50         sqlite3MP
29b8f 72 69 6e 74 66 28 64 62 2c 20 22 2a 2a 2a 20 69  rintf(db, "*** i
29b90 6e 20 64 61 74 61 62 61 73 65 20 25 73 20 2a 2a  n database %s **
29b91 2a 5c 6e 22 2c 20 64 62 2d 3e 61 44 62 5b 69 5d  *\n", db->aDb[i]
29b92 2e 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20 20  .zName),.       
29b93 20 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20    P4_DYNAMIC);. 
29b94 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29b95 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 6f 76  AddOp3(v, OP_Mov
29b96 65 2c 20 32 2c 20 34 2c 20 31 29 3b 0a 20 20 20  e, 2, 4, 1);.   
29b97 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29b98 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6e 63 61  dOp3(v, OP_Conca
29b99 74 2c 20 34 2c 20 33 2c 20 32 29 3b 0a 20 20 20  t, 4, 3, 2);.   
29b9a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29b9b 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c  dOp2(v, OP_Resul
29b9c 74 52 6f 77 2c 20 32 2c 20 31 29 3b 0a 20 20 20  tRow, 2, 1);.   
29b9d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
29b9e 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b  mpHere(v, addr);
29b9f 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20  ..      /* Make 
29ba0 73 75 72 65 20 61 6c 6c 20 74 68 65 20 69 6e 64  sure all the ind
29ba1 69 63 65 73 20 61 72 65 20 63 6f 6e 73 74 72 75  ices are constru
29ba2 63 74 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a  cted correctly..
29ba3 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 66        */.      f
29ba4 6f 72 28 78 3d 73 71 6c 69 74 65 48 61 73 68 46  or(x=sqliteHashF
29ba5 69 72 73 74 28 70 54 62 6c 73 29 3b 20 78 20 26  irst(pTbls); x &
29ba6 26 20 21 69 73 51 75 69 63 6b 3b 20 78 3d 73 71  & !isQuick; x=sq
29ba7 6c 69 74 65 48 61 73 68 4e 65 78 74 28 78 29 29  liteHashNext(x))
29ba8 7b 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20  {.        Table 
29ba9 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61  *pTab = sqliteHa
29baa 73 68 44 61 74 61 28 78 29 3b 0a 20 20 20 20 20  shData(x);.     
29bab 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
29bac 20 20 20 20 20 20 20 20 69 6e 74 20 6c 6f 6f 70          int loop
29bad 54 6f 70 3b 0a 0a 20 20 20 20 20 20 20 20 69 66  Top;..        if
29bae 28 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 3d 3d  ( pTab->pIndex==
29baf 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
29bb0 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
29bb1 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
29bb2 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29 3b 20  , OP_IfPos, 1); 
29bb3 20 2f 2a 20 53 74 6f 70 20 69 66 20 6f 75 74 20   /* Stop if out 
29bb4 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20  of errors */.   
29bb5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29bb6 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c  AddOp2(v, OP_Hal
29bb7 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  t, 0, 0);.      
29bb8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
29bb9 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
29bba 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f          sqlite3O
29bbb 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63  penTableAndIndic
29bbc 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  es(pParse, pTab,
29bbd 20 31 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29   1, OP_OpenRead)
29bbe 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29bbf 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29bc0 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 32 29  P_Integer, 0, 2)
29bc1 3b 20 20 2f 2a 20 72 65 67 28 32 29 20 77 69 6c  ;  /* reg(2) wil
29bc2 6c 20 63 6f 75 6e 74 20 65 6e 74 72 69 65 73 20  l count entries 
29bc3 2a 2f 0a 20 20 20 20 20 20 20 20 6c 6f 6f 70 54  */.        loopT
29bc4 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  op = sqlite3Vdbe
29bc5 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
29bc6 69 6e 64 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  ind, 1, 0);.    
29bc7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29bc8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
29bc9 6d 6d 2c 20 32 2c 20 31 29 3b 20 20 20 2f 2a 20  mm, 2, 1);   /* 
29bca 69 6e 63 72 65 6d 65 6e 74 20 65 6e 74 72 79 20  increment entry 
29bcb 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20  count */.       
29bcc 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70   for(j=0, pIdx=p
29bcd 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
29bce 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
29bcf 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  ext, j++){.     
29bd0 20 20 20 20 20 69 6e 74 20 6a 6d 70 32 3b 0a 20       int jmp2;. 
29bd1 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 3b           int r1;
29bd2 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69  .          stati
29bd3 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
29bd4 73 74 20 69 64 78 45 72 72 5b 5d 20 3d 20 7b 0a  st idxErr[] = {.
29bd5 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
29bd6 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 31 2c  _AddImm,      1,
29bd7 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20   -1,  0},.      
29bd8 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
29bd9 67 38 2c 20 20 20 20 20 30 2c 20 20 33 2c 20 20  g8,     0,  3,  
29bda 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20  0},    /* 1 */. 
29bdb 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
29bdc 52 6f 77 69 64 2c 20 20 20 20 20 20 20 31 2c 20  Rowid,       1, 
29bdd 20 34 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20   4,  0},.       
29bde 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67       { OP_String
29bdf 38 2c 20 20 20 20 20 30 2c 20 20 35 2c 20 20 30  8,     0,  5,  0
29be0 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20  },    /* 3 */.  
29be1 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
29be2 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20  tring8,     0,  
29be3 36 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 34 20  6,  0},    /* 4 
29be4 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
29be5 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20   OP_Concat,     
29be6 20 34 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20   4,  3,  3},.   
29be7 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f           { OP_Co
29be8 6e 63 61 74 2c 20 20 20 20 20 20 35 2c 20 20 33  ncat,      5,  3
29be9 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  3},.         
29bea 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20     { OP_Concat, 
29beb 20 20 20 20 20 36 2c 20 20 33 2c 20 20 33 7d 2c       6,  3,  3},
29bec 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
29bed 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 33  P_ResultRow,   3
29bee 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  1,  0},.     
29bef 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f         { OP_IfPo
29bf0 73 2c 20 20 20 20 20 20 20 31 2c 20 20 30 2c 20  s,       1,  0, 
29bf1 20 30 7d 2c 20 20 20 20 2f 2a 20 39 20 2a 2f 0a   0},    /* 9 */.
29bf2 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
29bf3 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20 30 2c  _Halt,        0,
29bf4 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20    0,  0},.      
29bf5 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20      };.         
29bf6 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 6e   r1 = sqlite3Gen
29bf7 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50  erateIndexKey(pP
29bf8 61 72 73 65 2c 20 70 49 64 78 2c 20 31 2c 20 33  arse, pIdx, 1, 3
29bf9 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
29bfa 6a 6d 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64  jmp2 = sqlite3Vd
29bfb 62 65 41 64 64 4f 70 34 49 6e 74 28 76 2c 20 4f  beAddOp4Int(v, O
29bfc 50 5f 46 6f 75 6e 64 2c 20 6a 2b 32 2c 20 30 2c  P_Found, j+2, 0,
29bfd 20 72 31 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75   r1, pIdx->nColu
29bfe 6d 6e 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20  mn+1);.         
29bff 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
29c00 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20  dbeAddOpList(v, 
29c01 41 72 72 61 79 53 69 7a 65 28 69 64 78 45 72 72  ArraySize(idxErr
29c02 29 2c 20 69 64 78 45 72 72 29 3b 0a 20 20 20 20  ), idxErr);.    
29c03 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29c04 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
29c05 72 2b 31 2c 20 22 72 6f 77 69 64 20 22 2c 20 50  r+1, "rowid ", P
29c06 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  4_STATIC);.     
29c07 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29c08 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72  ChangeP4(v, addr
29c09 2b 33 2c 20 22 20 6d 69 73 73 69 6e 67 20 66 72  +3, " missing fr
29c0a 6f 6d 20 69 6e 64 65 78 20 22 2c 20 50 34 5f 53  om index ", P4_S
29c0b 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
29c0c 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
29c0d 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 34 2c  ngeP4(v, addr+4,
29c0e 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34   pIdx->zName, P4
29c0f 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
29c10 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
29c11 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 2b  umpHere(v, addr+
29c12 39 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  9);.          sq
29c13 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
29c14 65 28 76 2c 20 6a 6d 70 32 29 3b 0a 20 20 20 20  e(v, jmp2);.    
29c15 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
29c16 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29c17 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 31 2c 20 6c  v, OP_Next, 1, l
29c18 6f 6f 70 54 6f 70 2b 31 29 3b 0a 20 20 20 20 20  oopTop+1);.     
29c19 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
29c1a 6d 70 48 65 72 65 28 76 2c 20 6c 6f 6f 70 54 6f  mpHere(v, loopTo
29c1b 70 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  p);.        for(
29c1c 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  j=0, pIdx=pTab->
29c1d 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
29c1e 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
29c1f 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
29c20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62  static const Vdb
29c21 65 4f 70 4c 69 73 74 20 63 6e 74 49 64 78 5b 5d  eOpList cntIdx[]
29c22 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20   = {.           
29c23 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20    { OP_Integer, 
29c24 20 20 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c       0,  3,  0},
29c25 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  .             { 
29c26 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20 20  OP_Rewind,      
29c27 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 2f 2a   0,  0,  0},  /*
29c28 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   1 */.          
29c29 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20     { OP_AddImm, 
29c2a 20 20 20 20 20 20 33 2c 20 20 31 2c 20 20 30 7d        3,  1,  0}
29c2b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  ,.             {
29c2c 20 4f 50 5f 4e 65 78 74 2c 20 20 20 20 20 20 20   OP_Next,       
29c2d 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 20 20 2f    0,  0,  0},  /
29c2e 2a 20 33 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 3 */.         
29c2f 20 20 20 20 7b 20 4f 50 5f 45 71 2c 20 20 20 20      { OP_Eq,    
29c30 20 20 20 20 20 20 20 32 2c 20 20 30 2c 20 20 33         2,  0,  3
29c31 7d 2c 20 20 2f 2a 20 34 20 2a 2f 0a 20 20 20 20  },  /* 4 */.    
29c32 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64           { OP_Ad
29c33 64 49 6d 6d 2c 20 20 20 20 20 20 20 31 2c 20 2d  dImm,       1, -
29c34 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20  1,  0},.        
29c35 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67       { OP_String
29c36 38 2c 20 20 20 20 20 20 30 2c 20 20 32 2c 20 20  8,      0,  2,  
29c37 30 7d 2c 20 20 2f 2a 20 36 20 2a 2f 0a 20 20 20  0},  /* 6 */.   
29c38 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
29c39 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30 2c 20  tring8,      0, 
29c3a 20 33 2c 20 20 30 7d 2c 20 20 2f 2a 20 37 20 2a   3,  0},  /* 7 *
29c3b 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  /.             {
29c3c 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20   OP_Concat,     
29c3d 20 20 33 2c 20 20 32 2c 20 20 32 7d 2c 0a 20 20    3,  2,  2},.  
29c3e 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
29c3f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 20 32 2c  ResultRow,    2,
29c40 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20    1,  0},.      
29c41 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20      };.         
29c42 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
29c43 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
29c44 49 66 50 6f 73 2c 20 31 29 3b 0a 20 20 20 20 20  IfPos, 1);.     
29c45 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29c46 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c  AddOp2(v, OP_Hal
29c47 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  t, 0, 0);.      
29c48 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
29c49 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
29c4a 3b 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 72  ;.          addr
29c4b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
29c4c 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79  dOpList(v, Array
29c4d 53 69 7a 65 28 63 6e 74 49 64 78 29 2c 20 63 6e  Size(cntIdx), cn
29c4e 74 49 64 78 29 3b 0a 20 20 20 20 20 20 20 20 20  tIdx);.         
29c4f 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
29c50 67 65 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20  geP1(v, addr+1, 
29c51 6a 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20  j+2);.          
29c52 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29c53 65 50 32 28 76 2c 20 61 64 64 72 2b 31 2c 20 61  eP2(v, addr+1, a
29c54 64 64 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20  ddr+4);.        
29c55 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
29c56 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b 33 2c  ngeP1(v, addr+3,
29c57 20 6a 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20   j+2);.         
29c58 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
29c59 67 65 50 32 28 76 2c 20 61 64 64 72 2b 33 2c 20  geP2(v, addr+3, 
29c5a 61 64 64 72 2b 32 29 3b 0a 20 20 20 20 20 20 20  addr+2);.       
29c5b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
29c5c 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 2b 34  mpHere(v, addr+4
29c5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
29c5e 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
29c5f 28 76 2c 20 61 64 64 72 2b 36 2c 20 0a 20 20 20  (v, addr+6, .   
29c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c61 20 20 22 77 72 6f 6e 67 20 23 20 6f 66 20 65 6e    "wrong # of en
29c62 74 72 69 65 73 20 69 6e 20 69 6e 64 65 78 20 22  tries in index "
29c63 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
29c64 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29c65 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61  dbeChangeP4(v, a
29c66 64 64 72 2b 37 2c 20 70 49 64 78 2d 3e 7a 4e 61  ddr+7, pIdx->zNa
29c67 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  me, P4_STATIC);.
29c68 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
29c69 7d 20 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64  } .    }.    add
29c6a 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
29c6b 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61  ddOpList(v, Arra
29c6c 79 53 69 7a 65 28 65 6e 64 43 6f 64 65 29 2c 20  ySize(endCode), 
29c6d 65 6e 64 43 6f 64 65 29 3b 0a 20 20 20 20 73 71  endCode);.    sq
29c6e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
29c6f 32 28 76 2c 20 61 64 64 72 2c 20 2d 6d 78 45 72  2(v, addr, -mxEr
29c70 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  r);.    sqlite3V
29c71 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
29c72 64 64 72 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  ddr+1);.    sqli
29c73 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
29c74 76 2c 20 61 64 64 72 2b 32 2c 20 22 6f 6b 22 2c  v, addr+2, "ok",
29c75 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d   P4_STATIC);.  }
29c76 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
29c77 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
29c78 52 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23  RITY_CHECK */..#
29c79 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
29c7a 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a 0a 20 20  IT_UTF16.  /*.  
29c7b 2a 2a 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f  **   PRAGMA enco
29c7c 64 69 6e 67 0a 20 20 2a 2a 20 20 20 50 52 41 47  ding.  **   PRAG
29c7d 4d 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20 22 75  MA encoding = "u
29c7e 74 66 2d 38 22 7c 22 75 74 66 2d 31 36 22 7c 22  tf-8"|"utf-16"|"
29c7f 75 74 66 2d 31 36 6c 65 22 7c 22 75 74 66 2d 31  utf-16le"|"utf-1
29c80 36 62 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  6be".  **.  ** I
29c81 6e 20 69 74 73 20 66 69 72 73 74 20 66 6f 72 6d  n its first form
29c82 2c 20 74 68 69 73 20 70 72 61 67 6d 61 20 72 65  , this pragma re
29c83 74 75 72 6e 73 20 74 68 65 20 65 6e 63 6f 64 69  turns the encodi
29c84 6e 67 20 6f 66 20 74 68 65 20 6d 61 69 6e 0a 20  ng of the main. 
29c85 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 49 66   ** database. If
29c86 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
29c87 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69 7a 65 64   not initialized
29c88 2c 20 69 74 20 69 73 20 69 6e 69 74 69 61 6c 69  , it is initiali
29c89 7a 65 64 20 6e 6f 77 2e 0a 20 20 2a 2a 0a 20 20  zed now..  **.  
29c8a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f  ** The second fo
29c8b 72 6d 20 6f 66 20 74 68 69 73 20 70 72 61 67 6d  rm of this pragm
29c8c 61 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20  a is a no-op if 
29c8d 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
29c8e 65 20 66 69 6c 65 0a 20 20 2a 2a 20 68 61 73 20  e file.  ** has 
29c8f 6e 6f 74 20 61 6c 72 65 61 64 79 20 62 65 65 6e  not already been
29c90 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49 6e   initialized. In
29c91 20 74 68 69 73 20 63 61 73 65 20 69 74 20 73 65   this case it se
29c92 74 73 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20  ts the default. 
29c93 20 2a 2a 20 65 6e 63 6f 64 69 6e 67 20 74 68 61   ** encoding tha
29c94 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66  t will be used f
29c95 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  or the main data
29c96 62 61 73 65 20 66 69 6c 65 20 69 66 20 61 20 6e  base file if a n
29c97 65 77 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20  ew file.  ** is 
29c98 63 72 65 61 74 65 64 2e 20 49 66 20 61 6e 20 65  created. If an e
29c99 78 69 73 74 69 6e 67 20 6d 61 69 6e 20 64 61 74  xisting main dat
29c9a 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70  abase file is op
29c9b 65 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 20  ened, then the. 
29c9c 20 2a 2a 20 64 65 66 61 75 6c 74 20 74 65 78 74   ** default text
29c9d 20 65 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68   encoding for th
29c9e 65 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62  e existing datab
29c9f 61 73 65 20 69 73 20 75 73 65 64 2e 0a 20 20 2a  ase is used..  *
29ca0 2a 20 0a 20 20 2a 2a 20 49 6e 20 61 6c 6c 20 63  * .  ** In all c
29ca1 61 73 65 73 20 6e 65 77 20 64 61 74 61 62 61 73  ases new databas
29ca2 65 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  es created using
29ca3 20 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d   the ATTACH comm
29ca4 61 6e 64 20 61 72 65 0a 20 20 2a 2a 20 63 72 65  and are.  ** cre
29ca5 61 74 65 64 20 74 6f 20 75 73 65 20 74 68 65 20  ated to use the 
29ca6 73 61 6d 65 20 64 65 66 61 75 6c 74 20 74 65 78  same default tex
29ca7 74 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68  t encoding as th
29ca8 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e  e main database.
29ca9 20 49 66 0a 20 20 2a 2a 20 74 68 65 20 6d 61 69   If.  ** the mai
29caa 6e 20 64 61 74 61 62 61 73 65 20 68 61 73 20 6e  n database has n
29cab 6f 74 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  ot been initiali
29cac 7a 65 64 20 61 6e 64 2f 6f 72 20 63 72 65 61 74  zed and/or creat
29cad 65 64 20 77 68 65 6e 20 41 54 54 41 43 48 0a 20  ed when ATTACH. 
29cae 20 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2c   ** is executed,
29caf 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65   this is done be
29cb0 66 6f 72 65 20 74 68 65 20 41 54 54 41 43 48 20  fore the ATTACH 
29cb1 6f 70 65 72 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a  operation..  **.
29cb2 20 20 2a 2a 20 49 6e 20 74 68 65 20 73 65 63 6f    ** In the seco
29cb3 6e 64 20 66 6f 72 6d 20 74 68 69 73 20 70 72 61  nd form this pra
29cb4 67 6d 61 20 73 65 74 73 20 74 68 65 20 74 65 78  gma sets the tex
29cb5 74 20 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65  t encoding to be
29cb6 20 75 73 65 64 20 69 6e 0a 20 20 2a 2a 20 6e 65   used in.  ** ne
29cb7 77 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  w database files
29cb8 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74   created using t
29cb9 68 69 73 20 64 61 74 61 62 61 73 65 20 68 61 6e  his database han
29cba 64 6c 65 2e 20 49 74 20 69 73 20 6f 6e 6c 79 0a  dle. It is only.
29cbb 20 20 2a 2a 20 75 73 65 66 75 6c 20 69 66 20 69    ** useful if i
29cbc 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65  nvoked immediate
29cbd 6c 79 20 61 66 74 65 72 20 74 68 65 20 6d 61 69  ly after the mai
29cbe 6e 20 64 61 74 61 62 61 73 65 20 69 0a 20 20 2a  n database i.  *
29cbf 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
29cc0 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 65  trICmp(zLeft, "e
29cc1 6e 63 6f 64 69 6e 67 22 29 3d 3d 30 20 29 7b 0a  ncoding")==0 ){.
29cc2 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
29cc3 20 73 74 72 75 63 74 20 45 6e 63 4e 61 6d 65 20   struct EncName 
29cc4 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e  {.      char *zN
29cc5 61 6d 65 3b 0a 20 20 20 20 20 20 75 38 20 65 6e  ame;.      u8 en
29cc6 63 3b 0a 20 20 20 20 7d 20 65 6e 63 6e 61 6d 65  c;.    } encname
29cc7 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20  s[] = {.      { 
29cc8 22 55 54 46 38 22 2c 20 20 20 20 20 53 51 4c 49  "UTF8",     SQLI
29cc9 54 45 5f 55 54 46 38 20 20 20 20 20 20 20 20 7d  TE_UTF8        }
29cca 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d 38  ,.      { "UTF-8
29ccb 22 2c 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46  ",    SQLITE_UTF
29ccc 38 20 20 20 20 20 20 20 20 7d 2c 20 20 2f 2a 20  8        },  /* 
29ccd 4d 75 73 74 20 62 65 20 65 6c 65 6d 65 6e 74 20  Must be element 
29cce 5b 31 5d 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22  [1] */.      { "
29ccf 55 54 46 2d 31 36 6c 65 22 2c 20 53 51 4c 49 54  UTF-16le", SQLIT
29cd0 45 5f 55 54 46 31 36 4c 45 20 20 20 20 20 7d 2c  E_UTF16LE     },
29cd1 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 65 6c 65    /* Must be ele
29cd2 6d 65 6e 74 20 5b 32 5d 20 2a 2f 0a 20 20 20 20  ment [2] */.    
29cd3 20 20 7b 20 22 55 54 46 2d 31 36 62 65 22 2c 20    { "UTF-16be", 
29cd4 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20  SQLITE_UTF16BE  
29cd5 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62     },  /* Must b
29cd6 65 20 65 6c 65 6d 65 6e 74 20 5b 33 5d 20 2a 2f  e element [3] */
29cd7 0a 20 20 20 20 20 20 7b 20 22 55 54 46 31 36 6c  .      { "UTF16l
29cd8 65 22 2c 20 20 53 51 4c 49 54 45 5f 55 54 46 31  e",  SQLITE_UTF1
29cd9 36 4c 45 20 20 20 20 20 7d 2c 0a 20 20 20 20 20  6LE     },.     
29cda 20 7b 20 22 55 54 46 31 36 62 65 22 2c 20 20 53   { "UTF16be",  S
29cdb 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20 20  QLITE_UTF16BE   
29cdc 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54    },.      { "UT
29cdd 46 2d 31 36 22 2c 20 20 20 30 20 20 20 20 20 20  F-16",   0      
29cde 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 20 2f              }, /
29cdf 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  * SQLITE_UTF16NA
29ce0 54 49 56 45 20 2a 2f 0a 20 20 20 20 20 20 7b 20  TIVE */.      { 
29ce1 22 55 54 46 31 36 22 2c 20 20 20 20 30 20 20 20  "UTF16",    0   
29ce2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d                 }
29ce3 2c 20 2f 2a 20 53 51 4c 49 54 45 5f 55 54 46 31  , /* SQLITE_UTF1
29ce4 36 4e 41 54 49 56 45 20 2a 2f 0a 20 20 20 20 20  6NATIVE */.     
29ce5 20 7b 20 30 2c 20 30 20 7d 0a 20 20 20 20 7d 3b   { 0, 0 }.    };
29ce6 0a 20 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63  .    const struc
29ce7 74 20 45 6e 63 4e 61 6d 65 20 2a 70 45 6e 63 3b  t EncName *pEnc;
29ce8 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74  .    if( !zRight
29ce9 20 29 7b 20 20 20 20 2f 2a 20 22 50 52 41 47 4d   ){    /* "PRAGM
29cea 41 20 65 6e 63 6f 64 69 6e 67 22 20 2a 2f 0a 20  A encoding" */. 
29ceb 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
29cec 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
29ced 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
29cee 5f 6f 75 74 3b 0a 20 20 20 20 20 20 73 71 6c 69  _out;.      sqli
29cef 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
29cf0 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  s(v, 1);.      s
29cf1 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
29cf2 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
29cf3 4d 45 5f 4e 41 4d 45 2c 20 22 65 6e 63 6f 64 69  ME_NAME, "encodi
29cf4 6e 67 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ng", SQLITE_STAT
29cf5 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
29cf6 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29cf7 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31  OP_String8, 0, 1
29cf8 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
29cf9 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45   encnames[SQLITE
29cfa 5f 55 54 46 38 5d 2e 65 6e 63 3d 3d 53 51 4c 49  _UTF8].enc==SQLI
29cfb 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 20  TE_UTF8 );.     
29cfc 20 61 73 73 65 72 74 28 20 65 6e 63 6e 61 6d 65   assert( encname
29cfd 73 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  s[SQLITE_UTF16LE
29cfe 5d 2e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ].enc==SQLITE_UT
29cff 46 31 36 4c 45 20 29 3b 0a 20 20 20 20 20 20 61  F16LE );.      a
29d00 73 73 65 72 74 28 20 65 6e 63 6e 61 6d 65 73 5b  ssert( encnames[
29d01 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 5d 2e  SQLITE_UTF16BE].
29d02 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc==SQLITE_UTF1
29d03 36 42 45 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  6BE );.      sql
29d04 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
29d05 28 76 2c 20 2d 31 2c 20 65 6e 63 6e 61 6d 65 73  (v, -1, encnames
29d06 5b 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29  [ENC(pParse->db)
29d07 5d 2e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54  ].zName, P4_STAT
29d08 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
29d09 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29d0a 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
29d0b 20 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 20   1);.    }else{ 
29d0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29d0d 20 20 20 20 20 20 20 2f 2a 20 22 50 52 41 47 4d         /* "PRAGM
29d0e 41 20 65 6e 63 6f 64 69 6e 67 20 3d 20 58 58 58  A encoding = XXX
29d0f 22 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 4f 6e  " */.      /* On
29d10 6c 79 20 63 68 61 6e 67 65 20 74 68 65 20 76 61  ly change the va
29d11 6c 75 65 20 6f 66 20 73 71 6c 69 74 65 2e 65 6e  lue of sqlite.en
29d12 63 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73  c if the databas
29d13 65 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 0a  e handle is not.
29d14 20 20 20 20 20 20 2a 2a 20 69 6e 69 74 69 61 6c        ** initial
29d15 69 7a 65 64 2e 20 49 66 20 74 68 65 20 6d 61 69  ized. If the mai
29d16 6e 20 64 61 74 61 62 61 73 65 20 65 78 69 73 74  n database exist
29d17 73 2c 20 74 68 65 20 6e 65 77 20 73 71 6c 69 74  s, the new sqlit
29d18 65 2e 65 6e 63 20 76 61 6c 75 65 0a 20 20 20 20  e.enc value.    
29d19 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 6f 76 65    ** will be ove
29d1a 72 77 72 69 74 74 65 6e 20 77 68 65 6e 20 74 68  rwritten when th
29d1b 65 20 73 63 68 65 6d 61 20 69 73 20 6e 65 78 74  e schema is next
29d1c 20 6c 6f 61 64 65 64 2e 20 49 66 20 69 74 20 64   loaded. If it d
29d1d 6f 65 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a 2a  oes not.      **
29d1e 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2c   already exists,
29d1f 20 69 74 20 77 69 6c 6c 20 62 65 20 63 72 65 61   it will be crea
29d20 74 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 6e  ted to use the n
29d21 65 77 20 65 6e 63 6f 64 69 6e 67 20 76 61 6c 75  ew encoding valu
29d22 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
29d23 20 20 69 66 28 20 0a 20 20 20 20 20 20 20 20 21    if( .        !
29d24 28 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64  (DbHasProperty(d
29d25 62 2c 20 30 2c 20 44 42 5f 53 63 68 65 6d 61 4c  b, 0, DB_SchemaL
29d26 6f 61 64 65 64 29 29 20 7c 7c 20 0a 20 20 20 20  oaded)) || .    
29d27 20 20 20 20 44 62 48 61 73 50 72 6f 70 65 72 74      DbHasPropert
29d28 79 28 64 62 2c 20 30 2c 20 44 42 5f 45 6d 70 74  y(db, 0, DB_Empt
29d29 79 29 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  y) .      ){.   
29d2a 20 20 20 20 20 66 6f 72 28 70 45 6e 63 3d 26 65       for(pEnc=&e
29d2b 6e 63 6e 61 6d 65 73 5b 30 5d 3b 20 70 45 6e 63  ncnames[0]; pEnc
29d2c 2d 3e 7a 4e 61 6d 65 3b 20 70 45 6e 63 2b 2b 29  ->zName; pEnc++)
29d2d 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
29d2e 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
29d2f 70 28 7a 52 69 67 68 74 2c 20 70 45 6e 63 2d 3e  p(zRight, pEnc->
29d30 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20  zName) ){.      
29d31 20 20 20 20 20 20 45 4e 43 28 70 50 61 72 73 65        ENC(pParse
29d32 2d 3e 64 62 29 20 3d 20 70 45 6e 63 2d 3e 65 6e  ->db) = pEnc->en
29d33 63 20 3f 20 70 45 6e 63 2d 3e 65 6e 63 20 3a 20  c ? pEnc->enc : 
29d34 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
29d35 56 45 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  VE;.            
29d36 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
29d37 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
29d38 20 20 20 20 20 69 66 28 20 21 70 45 6e 63 2d 3e       if( !pEnc->
29d39 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20  zName ){.       
29d3a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
29d3b 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 73 75  sg(pParse, "unsu
29d3c 70 70 6f 72 74 65 64 20 65 6e 63 6f 64 69 6e 67  pported encoding
29d3d 3a 20 25 73 22 2c 20 7a 52 69 67 68 74 29 3b 0a  : %s", zRight);.
29d3e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
29d3f 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
29d40 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
29d41 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
29d42 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
29d43 4d 49 54 5f 53 43 48 45 4d 41 5f 56 45 52 53 49  MIT_SCHEMA_VERSI
29d44 4f 4e 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a  ON_PRAGMAS.  /*.
29d45 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64    **   PRAGMA [d
29d46 61 74 61 62 61 73 65 2e 5d 73 63 68 65 6d 61 5f  atabase.]schema_
29d47 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 50  version.  **   P
29d48 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
29d49 5d 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 20  ]schema_version 
29d4a 3d 20 3c 69 6e 74 65 67 65 72 3e 0a 20 20 2a 2a  = <integer>.  **
29d4b 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
29d4c 64 61 74 61 62 61 73 65 2e 5d 75 73 65 72 5f 76  database.]user_v
29d4d 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 50 52  ersion.  **   PR
29d4e 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
29d4f 75 73 65 72 5f 76 65 72 73 69 6f 6e 20 3d 20 3c  user_version = <
29d50 69 6e 74 65 67 65 72 3e 0a 20 20 2a 2a 0a 20 20  integer>.  **.  
29d51 2a 2a 20 54 68 65 20 70 72 61 67 6d 61 27 73 20  ** The pragma's 
29d52 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e 20 61  schema_version a
29d53 6e 64 20 75 73 65 72 5f 76 65 72 73 69 6f 6e 20  nd user_version 
29d54 61 72 65 20 75 73 65 64 20 74 6f 20 73 65 74 20  are used to set 
29d55 6f 72 20 67 65 74 0a 20 20 2a 2a 20 74 68 65 20  or get.  ** the 
29d56 76 61 6c 75 65 20 6f 66 20 74 68 65 20 73 63 68  value of the sch
29d57 65 6d 61 2d 76 65 72 73 69 6f 6e 20 61 6e 64 20  ema-version and 
29d58 75 73 65 72 2d 76 65 72 73 69 6f 6e 2c 20 72 65  user-version, re
29d59 73 70 65 63 74 69 76 65 6c 79 2e 20 42 6f 74 68  spectively. Both
29d5a 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61  .  ** the schema
29d5b 2d 76 65 72 73 69 6f 6e 20 61 6e 64 20 74 68 65  -version and the
29d5c 20 75 73 65 72 2d 76 65 72 73 69 6f 6e 20 61 72   user-version ar
29d5d 65 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20  e 32-bit signed 
29d5e 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 73 74  integers.  ** st
29d5f 6f 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 61  ored in the data
29d60 62 61 73 65 20 68 65 61 64 65 72 2e 0a 20 20 2a  base header..  *
29d61 2a 0a 20 20 2a 2a 20 54 68 65 20 73 63 68 65 6d  *.  ** The schem
29d62 61 2d 63 6f 6f 6b 69 65 20 69 73 20 75 73 75 61  a-cookie is usua
29d63 6c 6c 79 20 6f 6e 6c 79 20 6d 61 6e 69 70 75 6c  lly only manipul
29d64 61 74 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20  ated internally 
29d65 62 79 20 53 51 4c 69 74 65 2e 20 49 74 0a 20 20  by SQLite. It.  
29d66 2a 2a 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  ** is incremente
29d67 64 20 62 79 20 53 51 4c 69 74 65 20 77 68 65 6e  d by SQLite when
29d68 65 76 65 72 20 74 68 65 20 64 61 74 61 62 61 73  ever the databas
29d69 65 20 73 63 68 65 6d 61 20 69 73 20 6d 6f 64 69  e schema is modi
29d6a 66 69 65 64 20 28 62 79 0a 20 20 2a 2a 20 63 72  fied (by.  ** cr
29d6b 65 61 74 69 6e 67 20 6f 72 20 64 72 6f 70 70 69  eating or droppi
29d6c 6e 67 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e  ng a table or in
29d6d 64 65 78 29 2e 20 54 68 65 20 73 63 68 65 6d 61  dex). The schema
29d6e 20 76 65 72 73 69 6f 6e 20 69 73 20 75 73 65 64   version is used
29d6f 20 62 79 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20   by.  ** SQLite 
29d70 65 61 63 68 20 74 69 6d 65 20 61 20 71 75 65 72  each time a quer
29d71 79 20 69 73 20 65 78 65 63 75 74 65 64 20 74 6f  y is executed to
29d72 20 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65   ensure that the
29d73 20 69 6e 74 65 72 6e 61 6c 20 63 61 63 68 65 0a   internal cache.
29d74 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63 68 65    ** of the sche
29d75 6d 61 20 75 73 65 64 20 77 68 65 6e 20 63 6f 6d  ma used when com
29d76 70 69 6c 69 6e 67 20 74 68 65 20 53 51 4c 20 71  piling the SQL q
29d77 75 65 72 79 20 6d 61 74 63 68 65 73 20 74 68 65  uery matches the
29d78 20 73 63 68 65 6d 61 20 6f 66 0a 20 20 2a 2a 20   schema of.  ** 
29d79 74 68 65 20 64 61 74 61 62 61 73 65 20 61 67 61  the database aga
29d7a 69 6e 73 74 20 77 68 69 63 68 20 74 68 65 20 63  inst which the c
29d7b 6f 6d 70 69 6c 65 64 20 71 75 65 72 79 20 69 73  ompiled query is
29d7c 20 61 63 74 75 61 6c 6c 79 20 65 78 65 63 75 74   actually execut
29d7d 65 64 2e 0a 20 20 2a 2a 20 53 75 62 76 65 72 74  ed..  ** Subvert
29d7e 69 6e 67 20 74 68 69 73 20 6d 65 63 68 61 6e 69  ing this mechani
29d7f 73 6d 20 62 79 20 75 73 69 6e 67 20 22 50 52 41  sm by using "PRA
29d80 47 4d 41 20 73 63 68 65 6d 61 5f 76 65 72 73 69  GMA schema_versi
29d81 6f 6e 22 20 74 6f 20 6d 6f 64 69 66 79 0a 20 20  on" to modify.  
29d82 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65  ** the schema-ve
29d83 72 73 69 6f 6e 20 69 73 20 70 6f 74 65 6e 74 69  rsion is potenti
29d84 61 6c 6c 79 20 64 61 6e 67 65 72 6f 75 73 20 61  ally dangerous a
29d85 6e 64 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 70  nd may lead to p
29d86 72 6f 67 72 61 6d 0a 20 20 2a 2a 20 63 72 61 73  rogram.  ** cras
29d87 68 65 73 20 6f 72 20 64 61 74 61 62 61 73 65 20  hes or database 
29d88 63 6f 72 72 75 70 74 69 6f 6e 2e 20 55 73 65 20  corruption. Use 
29d89 77 69 74 68 20 63 61 75 74 69 6f 6e 21 0a 20 20  with caution!.  
29d8a 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 75 73 65 72  **.  ** The user
29d8b 2d 76 65 72 73 69 6f 6e 20 69 73 20 6e 6f 74 20  -version is not 
29d8c 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20  used internally 
29d8d 62 79 20 53 51 4c 69 74 65 2e 20 49 74 20 6d 61  by SQLite. It ma
29d8e 79 20 62 65 20 75 73 65 64 20 62 79 0a 20 20 2a  y be used by.  *
29d8f 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 66  * applications f
29d90 6f 72 20 61 6e 79 20 70 75 72 70 6f 73 65 2e 0a  or any purpose..
29d91 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
29d92 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29d93 20 22 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e   "schema_version
29d94 22 29 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71 6c  ")==0 .   || sql
29d95 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29d96 74 2c 20 22 75 73 65 72 5f 76 65 72 73 69 6f 6e  t, "user_version
29d97 22 29 3d 3d 30 20 0a 20 20 20 7c 7c 20 73 71 6c  ")==0 .   || sql
29d98 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
29d99 74 2c 20 22 66 72 65 65 6c 69 73 74 5f 63 6f 75  t, "freelist_cou
29d9a 6e 74 22 29 3d 3d 30 20 0a 20 20 29 7b 0a 20 20  nt")==0 .  ){.  
29d9b 20 20 69 6e 74 20 69 43 6f 6f 6b 69 65 3b 20 20    int iCookie;  
29d9c 20 2f 2a 20 43 6f 6f 6b 69 65 20 69 6e 64 65 78   /* Cookie index
29d9d 2e 20 31 20 66 6f 72 20 73 63 68 65 6d 61 2d 63  . 1 for schema-c
29d9e 6f 6f 6b 69 65 2c 20 36 20 66 6f 72 20 75 73 65  ookie, 6 for use
29d9f 72 2d 63 6f 6f 6b 69 65 2e 20 2a 2f 0a 20 20 20  r-cookie. */.   
29da0 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
29da1 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20  Btree(v, iDb);. 
29da2 20 20 20 73 77 69 74 63 68 28 20 7a 4c 65 66 74     switch( zLeft
29da3 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 63 61 73  [0] ){.      cas
29da4 65 20 27 66 27 3a 20 63 61 73 65 20 27 46 27 3a  e 'f': case 'F':
29da5 0a 20 20 20 20 20 20 20 20 69 43 6f 6f 6b 69 65  .        iCookie
29da6 20 3d 20 42 54 52 45 45 5f 46 52 45 45 5f 50 41   = BTREE_FREE_PA
29da7 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 20 20 20 20  GE_COUNT;.      
29da8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
29da9 61 73 65 20 27 73 27 3a 20 63 61 73 65 20 27 53  ase 's': case 'S
29daa 27 3a 0a 20 20 20 20 20 20 20 20 69 43 6f 6f 6b  ':.        iCook
29dab 69 65 20 3d 20 42 54 52 45 45 5f 53 43 48 45 4d  ie = BTREE_SCHEM
29dac 41 5f 56 45 52 53 49 4f 4e 3b 0a 20 20 20 20 20  A_VERSION;.     
29dad 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
29dae 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20  default:.       
29daf 20 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52 45 45   iCookie = BTREE
29db0 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e 3b 0a 20  _USER_VERSION;. 
29db1 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
29db2 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 7a 52 69    }..    if( zRi
29db3 67 68 74 20 26 26 20 69 43 6f 6f 6b 69 65 21 3d  ght && iCookie!=
29db4 42 54 52 45 45 5f 46 52 45 45 5f 50 41 47 45 5f  BTREE_FREE_PAGE_
29db5 43 4f 55 4e 54 20 29 7b 0a 20 20 20 20 20 20 2f  COUNT ){.      /
29db6 2a 20 57 72 69 74 65 20 74 68 65 20 73 70 65 63  * Write the spec
29db7 69 66 69 65 64 20 63 6f 6f 6b 69 65 20 76 61 6c  ified cookie val
29db8 75 65 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74  ue */.      stat
29db9 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c  ic const VdbeOpL
29dba 69 73 74 20 73 65 74 43 6f 6f 6b 69 65 5b 5d 20  ist setCookie[] 
29dbb 3d 20 7b 0a 20 20 20 20 20 20 20 20 7b 20 4f 50  = {.        { OP
29dbc 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20  _Transaction,   
29dbd 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20 20 20 20   0,  1,  0},    
29dbe 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 0 */.        
29dbf 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20  { OP_Integer,   
29dc0 20 20 20 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c       0,  1,  0},
29dc1 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20      /* 1 */.    
29dc2 20 20 20 20 7b 20 4f 50 5f 53 65 74 43 6f 6f 6b      { OP_SetCook
29dc3 69 65 2c 20 20 20 20 20 20 30 2c 20 20 30 2c 20  ie,      0,  0, 
29dc4 20 31 7d 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a   1},    /* 2 */.
29dc5 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 69        };.      i
29dc6 6e 74 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65  nt addr = sqlite
29dc7 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
29dc8 2c 20 41 72 72 61 79 53 69 7a 65 28 73 65 74 43  , ArraySize(setC
29dc9 6f 6f 6b 69 65 29 2c 20 73 65 74 43 6f 6f 6b 69  ookie), setCooki
29dca 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
29dcb 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
29dcc 20 61 64 64 72 2c 20 69 44 62 29 3b 0a 20 20 20   addr, iDb);.   
29dcd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
29dce 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b 31  angeP1(v, addr+1
29dcf 2c 20 61 74 6f 69 28 7a 52 69 67 68 74 29 29 3b  , atoi(zRight));
29dd0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
29dd1 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64  beChangeP1(v, ad
29dd2 64 72 2b 32 2c 20 69 44 62 29 3b 0a 20 20 20 20  dr+2, iDb);.    
29dd3 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
29dd4 6e 67 65 50 32 28 76 2c 20 61 64 64 72 2b 32 2c  ngeP2(v, addr+2,
29dd5 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 7d   iCookie);.    }
29dd6 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 52  else{.      /* R
29dd7 65 61 64 20 74 68 65 20 73 70 65 63 69 66 69 65  ead the specifie
29dd8 64 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20 2a  d cookie value *
29dd9 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 20 63  /.      static c
29dda 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20  onst VdbeOpList 
29ddb 72 65 61 64 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b  readCookie[] = {
29ddc 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 54 72  .        { OP_Tr
29ddd 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20 20 30  ansaction,     0
29dde 2c 20 20 30 2c 20 20 30 7d 2c 20 20 20 20 2f 2a  ,  0,  0},    /*
29ddf 20 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20   0 */.        { 
29de0 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20  OP_ReadCookie,  
29de1 20 20 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20      0,  1,  0}, 
29de2 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20     /* 1 */.     
29de3 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f     { OP_ResultRo
29de4 77 2c 20 20 20 20 20 20 20 31 2c 20 20 31 2c 20  w,       1,  1, 
29de5 20 30 7d 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20   0}.      };.   
29de6 20 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 71     int addr = sq
29de7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
29de8 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28  st(v, ArraySize(
29de9 72 65 61 64 43 6f 6f 6b 69 65 29 2c 20 72 65 61  readCookie), rea
29dea 64 43 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20  dCookie);.      
29deb 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
29dec 65 50 31 28 76 2c 20 61 64 64 72 2c 20 69 44 62  eP1(v, addr, iDb
29ded 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29dee 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
29def 61 64 64 72 2b 31 2c 20 69 44 62 29 3b 0a 20 20  addr+1, iDb);.  
29df0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29df1 68 61 6e 67 65 50 33 28 76 2c 20 61 64 64 72 2b  hangeP3(v, addr+
29df2 31 2c 20 69 43 6f 6f 6b 69 65 29 3b 0a 20 20 20  1, iCookie);.   
29df3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29df4 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
29df5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29df6 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
29df7 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
29df8 7a 4c 65 66 74 2c 20 53 51 4c 49 54 45 5f 54 52  zLeft, SQLITE_TR
29df9 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d 0a  ANSIENT);.    }.
29dfa 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f    }else.#endif /
29dfb 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43  * SQLITE_OMIT_SC
29dfc 48 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41  HEMA_VERSION_PRA
29dfd 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 20 64 65 66  GMAS */..#if def
29dfe 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
29dff 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  G) || defined(SQ
29e00 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 2f 2a 0a  LITE_TEST).  /*.
29e01 20 20 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 20    ** Report the 
29e02 63 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66  current state of
29e03 20 66 69 6c 65 20 6c 6f 67 73 20 66 6f 72 20 61   file logs for a
29e04 6c 6c 20 64 61 74 61 62 61 73 65 73 0a 20 20 2a  ll databases.  *
29e05 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  /.  if( sqlite3S
29e06 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6c  trICmp(zLeft, "l
29e07 6f 63 6b 5f 73 74 61 74 75 73 22 29 3d 3d 30 20  ock_status")==0 
29e08 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  ){.    static co
29e09 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20  nst char *const 
29e0a 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 5d 20 3d 20 7b  azLockName[] = {
29e0b 0a 20 20 20 20 20 20 22 75 6e 6c 6f 63 6b 65 64  .      "unlocked
29e0c 22 2c 20 22 73 68 61 72 65 64 22 2c 20 22 72 65  ", "shared", "re
29e0d 73 65 72 76 65 64 22 2c 20 22 70 65 6e 64 69 6e  served", "pendin
29e0e 67 22 2c 20 22 65 78 63 6c 75 73 69 76 65 22 0a  g", "exclusive".
29e0f 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 69      };.    int i
29e10 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
29e11 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 32  eSetNumCols(v, 2
29e12 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  );.    pParse->n
29e13 4d 65 6d 20 3d 20 32 3b 0a 20 20 20 20 73 71 6c  Mem = 2;.    sql
29e14 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
29e15 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
29e16 5f 4e 41 4d 45 2c 20 22 64 61 74 61 62 61 73 65  _NAME, "database
29e17 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
29e18 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
29e19 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
29e1a 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  1, COLNAME_NAME,
29e1b 20 22 73 74 61 74 75 73 22 2c 20 53 51 4c 49 54   "status", SQLIT
29e1c 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66  E_STATIC);.    f
29e1d 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
29e1e 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42  b; i++){.      B
29e1f 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 20  tree *pBt;.     
29e20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a   Pager *pPager;.
29e21 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
29e22 20 2a 7a 53 74 61 74 65 20 3d 20 22 75 6e 6b 6e   *zState = "unkn
29e23 6f 77 6e 22 3b 0a 20 20 20 20 20 20 69 6e 74 20  own";.      int 
29e24 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d  j;.      if( db-
29e25 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d 30  >aDb[i].zName==0
29e26 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
29e27 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
29e28 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
29e29 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 64 62 2d  g8, 0, 1, 0, db-
29e2a 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 50  >aDb[i].zName, P
29e2b 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  4_STATIC);.     
29e2c 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69   pBt = db->aDb[i
29e2d 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28  ].pBt;.      if(
29e2e 20 70 42 74 3d 3d 30 20 7c 7c 20 28 70 50 61 67   pBt==0 || (pPag
29e2f 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
29e30 65 50 61 67 65 72 28 70 42 74 29 29 3d 3d 30 20  ePager(pBt))==0 
29e31 29 7b 0a 20 20 20 20 20 20 20 20 7a 53 74 61 74  ){.        zStat
29e32 65 20 3d 20 22 63 6c 6f 73 65 64 22 3b 0a 20 20  e = "closed";.  
29e33 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71      }else if( sq
29e34 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
29e35 6f 6c 28 64 62 2c 20 69 20 3f 20 64 62 2d 3e 61  ol(db, i ? db->a
29e36 44 62 5b 69 5d 2e 7a 4e 61 6d 65 20 3a 20 30 2c  Db[i].zName : 0,
29e37 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
29e38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29e39 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46 43         SQLITE_FC
29e3a 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 2c 20 26  NTL_LOCKSTATE, &
29e3b 6a 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  j)==SQLITE_OK ){
29e3c 0a 20 20 20 20 20 20 20 20 20 7a 53 74 61 74 65  .         zState
29e3d 20 3d 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 6a 5d   = azLockName[j]
29e3e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
29e3f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29e40 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
29e41 20 30 2c 20 32 2c 20 30 2c 20 7a 53 74 61 74 65   0, 2, 0, zState
29e42 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
29e43 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29e44 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
29e45 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b 0a 20 20  ltRow, 1, 2);.  
29e46 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 0a 23 65 6e    }..  }else.#en
29e47 64 69 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  dif..#if SQLITE_
29e48 48 41 53 5f 43 4f 44 45 43 0a 20 20 69 66 28 20  HAS_CODEC.  if( 
29e49 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
29e4a 4c 65 66 74 2c 20 22 6b 65 79 22 29 3d 3d 30 20  Left, "key")==0 
29e4b 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  && zRight ){.   
29e4c 20 73 71 6c 69 74 65 33 5f 6b 65 79 28 64 62 2c   sqlite3_key(db,
29e4d 20 7a 52 69 67 68 74 2c 20 73 71 6c 69 74 65 33   zRight, sqlite3
29e4e 53 74 72 6c 65 6e 33 30 28 7a 52 69 67 68 74 29  Strlen30(zRight)
29e4f 29 3b 0a 20 20 7d 65 6c 73 65 0a 20 20 69 66 28  );.  }else.  if(
29e50 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
29e51 7a 4c 65 66 74 2c 20 22 72 65 6b 65 79 22 29 3d  zLeft, "rekey")=
29e52 3d 30 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a  =0 && zRight ){.
29e53 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6b 65      sqlite3_reke
29e54 79 28 64 62 2c 20 7a 52 69 67 68 74 2c 20 73 71  y(db, zRight, sq
29e55 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 52  lite3Strlen30(zR
29e56 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73 65 0a  ight));.  }else.
29e57 20 20 69 66 28 20 7a 52 69 67 68 74 20 26 26 20    if( zRight && 
29e58 28 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28  (sqlite3StrICmp(
29e59 7a 4c 65 66 74 2c 20 22 68 65 78 6b 65 79 22 29  zLeft, "hexkey")
29e5a 3d 3d 30 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  ==0 ||.         
29e5b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
29e5c 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 68  trICmp(zLeft, "h
29e5d 65 78 72 65 6b 65 79 22 29 3d 3d 30 29 20 29 7b  exrekey")==0) ){
29e5e 0a 20 20 20 20 69 6e 74 20 69 2c 20 68 31 2c 20  .    int i, h1, 
29e5f 68 32 3b 0a 20 20 20 20 63 68 61 72 20 7a 4b 65  h2;.    char zKe
29e60 79 5b 34 30 5d 3b 0a 20 20 20 20 66 6f 72 28 69  y[40];.    for(i
29e61 3d 30 3b 20 28 68 31 20 3d 20 7a 52 69 67 68 74  =0; (h1 = zRight
29e62 5b 69 5d 29 21 3d 30 20 26 26 20 28 68 32 20 3d  [i])!=0 && (h2 =
29e63 20 7a 52 69 67 68 74 5b 69 2b 31 5d 29 21 3d 30   zRight[i+1])!=0
29e64 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 68  ; i+=2){.      h
29e65 31 20 2b 3d 20 39 2a 28 31 26 28 68 31 3e 3e 36  1 += 9*(1&(h1>>6
29e66 29 29 3b 0a 20 20 20 20 20 20 68 32 20 2b 3d 20  ));.      h2 += 
29e67 39 2a 28 31 26 28 68 32 3e 3e 36 29 29 3b 0a 20  9*(1&(h2>>6));. 
29e68 20 20 20 20 20 7a 4b 65 79 5b 69 2f 32 5d 20 3d       zKey[i/2] =
29e69 20 28 68 32 20 26 20 30 78 30 66 29 20 7c 20 28   (h2 & 0x0f) | (
29e6a 28 68 31 20 26 20 30 78 66 29 3c 3c 34 29 3b 0a  (h1 & 0xf)<<4);.
29e6b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 7a      }.    if( (z
29e6c 4c 65 66 74 5b 33 5d 20 26 20 30 78 66 29 3d 3d  Left[3] & 0xf)==
29e6d 30 78 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  0xb ){.      sql
29e6e 69 74 65 33 5f 6b 65 79 28 64 62 2c 20 7a 4b 65  ite3_key(db, zKe
29e6f 79 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d 65 6c  y, i/2);.    }el
29e70 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
29e71 33 5f 72 65 6b 65 79 28 64 62 2c 20 7a 4b 65 79  3_rekey(db, zKey
29e72 2c 20 69 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20  , i/2);.    }.  
29e73 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 23 69 66  }else.#endif.#if
29e74 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
29e75 43 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  C || defined(SQL
29e76 49 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44  ITE_ENABLE_CEROD
29e77 29 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ).  if( sqlite3S
29e78 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 61  trICmp(zLeft, "a
29e79 63 74 69 76 61 74 65 5f 65 78 74 65 6e 73 69 6f  ctivate_extensio
29e7a 6e 73 22 29 3d 3d 30 20 29 7b 0a 23 69 66 20 53  ns")==0 ){.#if S
29e7b 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a  QLITE_HAS_CODEC.
29e7c 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
29e7d 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20  trNICmp(zRight, 
29e7e 22 73 65 65 2d 22 2c 20 34 29 3d 3d 30 20 29 7b  "see-", 4)==0 ){
29e7f 0a 20 20 20 20 20 20 65 78 74 65 72 6e 20 76 6f  .      extern vo
29e80 69 64 20 73 71 6c 69 74 65 33 5f 61 63 74 69 76  id sqlite3_activ
29e81 61 74 65 5f 73 65 65 28 63 6f 6e 73 74 20 63 68  ate_see(const ch
29e82 61 72 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ar*);.      sqli
29e83 74 65 33 5f 61 63 74 69 76 61 74 65 5f 73 65 65  te3_activate_see
29e84 28 26 7a 52 69 67 68 74 5b 34 5d 29 3b 0a 20 20  (&zRight[4]);.  
29e85 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65    }.#endif.#ifde
29e86 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
29e87 43 45 52 4f 44 0a 20 20 20 20 69 66 28 20 73 71  CEROD.    if( sq
29e88 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 52  lite3StrNICmp(zR
29e89 69 67 68 74 2c 20 22 63 65 72 6f 64 2d 22 2c 20  ight, "cerod-", 
29e8a 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65  6)==0 ){.      e
29e8b 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74  xtern void sqlit
29e8c 65 33 5f 61 63 74 69 76 61 74 65 5f 63 65 72 6f  e3_activate_cero
29e8d 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  d(const char*);.
29e8e 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 63        sqlite3_ac
29e8f 74 69 76 61 74 65 5f 63 65 72 6f 64 28 26 7a 52  tivate_cerod(&zR
29e90 69 67 68 74 5b 36 5d 29 3b 0a 20 20 20 20 7d 0a  ight[6]);.    }.
29e91 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 0a 23  #endif.  }else.#
29e92 65 6e 64 69 66 0a 0a 20 0a 20 20 7b 2f 2a 20 45  endif.. .  {/* E
29e93 6d 70 74 79 20 45 4c 53 45 20 63 6c 61 75 73 65  mpty ELSE clause
29e94 20 2a 2f 7d 0a 0a 20 20 2f 2a 20 43 6f 64 65 20   */}..  /* Code 
29e95 61 6e 20 4f 50 5f 45 78 70 69 72 65 20 61 74 20  an OP_Expire at 
29e96 74 68 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20  the end of each 
29e97 50 52 41 47 4d 41 20 70 72 6f 67 72 61 6d 20 74  PRAGMA program t
29e98 6f 20 63 61 75 73 65 0a 20 20 2a 2a 20 74 68 65  o cause.  ** the
29e99 20 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 69   VDBE implementi
29e9a 6e 67 20 74 68 65 20 70 72 61 67 6d 61 20 74 6f  ng the pragma to
29e9b 20 65 78 70 69 72 65 2e 20 4d 6f 73 74 20 28 61   expire. Most (a
29e9c 6c 6c 3f 29 20 70 72 61 67 6d 61 73 0a 20 20 2a  ll?) pragmas.  *
29e9d 2a 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64  * are only valid
29e9e 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 78   for a single ex
29e9f 65 63 75 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  ecution..  */.  
29ea0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29ea1 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20  2(v, OP_Expire, 
29ea2 31 2c 20 30 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  1, 0);..  /*.  *
29ea3 2a 20 52 65 73 65 74 20 74 68 65 20 73 61 66 65  * Reset the safe
29ea4 74 79 20 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73  ty level, in cas
29ea5 65 20 74 68 65 20 66 75 6c 6c 66 73 79 6e 63 20  e the fullfsync 
29ea6 66 6c 61 67 20 6f 72 20 73 79 6e 63 68 72 6f 6e  flag or synchron
29ea7 6f 75 73 0a 20 20 2a 2a 20 73 65 74 74 69 6e 67  ous.  ** setting
29ea8 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 23   changed..  */.#
29ea9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
29eaa 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
29eab 0a 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43  .  if( db->autoC
29eac 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c  ommit ){.    sql
29ead 69 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65  ite3BtreeSetSafe
29eae 74 79 4c 65 76 65 6c 28 70 44 62 2d 3e 70 42 74  tyLevel(pDb->pBt
29eaf 2c 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65  , pDb->safety_le
29eb0 76 65 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20  vel,.           
29eb1 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73 26 53      (db->flags&S
29eb2 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 29  QLITE_FullFSync)
29eb3 21 3d 30 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  !=0);.  }.#endif
29eb4 0a 70 72 61 67 6d 61 5f 6f 75 74 3a 0a 20 20 73  .pragma_out:.  s
29eb5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
29eb6 20 7a 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69 74   zLeft);.  sqlit
29eb7 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 52 69  e3DbFree(db, zRi
29eb8 67 68 74 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  ght);.}..#endif 
29eb9 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  /* SQLITE_OMIT_P
29eba 52 41 47 4d 41 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  RAGMA */../*****
29ebb 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
29ebc 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a   pragma.c ******
29ebd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ebe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ebf 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
29ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
29ec1 66 69 6c 65 20 70 72 65 70 61 72 65 2e 63 20 2a  file prepare.c *
29ec2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ec3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ec4 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
29ec5 32 30 30 35 20 4d 61 79 20 32 35 0a 2a 2a 0a 2a  2005 May 25.**.*
29ec6 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
29ec7 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
29ec8 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
29ec9 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
29eca 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
29ecb 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
29ecc 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
29ecd 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
29ece 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
29ecf 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
29ed0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
29ed1 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
29ed2 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
29ed3 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
29ed4 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
29ed5 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
29ed6 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
29ed7 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
29ed8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29ed9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29eda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29edb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
29edc 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
29edd 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
29ede 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
29edf 65 33 5f 70 72 65 70 61 72 65 28 29 0a 2a 2a 20  e3_prepare().** 
29ee0 69 6e 74 65 72 66 61 63 65 2c 20 61 6e 64 20 72  interface, and r
29ee1 6f 75 74 69 6e 65 73 20 74 68 61 74 20 63 6f 6e  outines that con
29ee2 74 72 69 62 75 74 65 20 74 6f 20 6c 6f 61 64 69  tribute to loadi
29ee3 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ng the database 
29ee4 73 63 68 65 6d 61 0a 2a 2a 20 66 72 6f 6d 20 64  schema.** from d
29ee5 69 73 6b 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46  isk..*/../*.** F
29ee6 69 6c 6c 20 74 68 65 20 49 6e 69 74 44 61 74 61  ill the InitData
29ee7 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 20   structure with 
29ee8 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
29ee9 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 0a   that indicates.
29eea 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ** that the data
29eeb 62 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e  base is corrupt.
29eec 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
29eed 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28 0a 20  corruptSchema(. 
29eee 20 49 6e 69 74 44 61 74 61 20 2a 70 44 61 74 61   InitData *pData
29eef 2c 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c  ,     /* Initial
29ef0 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20  ization context 
29ef1 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
29ef2 2a 7a 4f 62 6a 2c 20 20 20 20 2f 2a 20 4f 62 6a  *zObj,    /* Obj
29ef3 65 63 74 20 62 65 69 6e 67 20 70 61 72 73 65 64  ect being parsed
29ef4 20 61 74 20 74 68 65 20 70 6f 69 6e 74 20 6f 66   at the point of
29ef5 20 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73   error */.  cons
29ef6 74 20 63 68 61 72 20 2a 7a 45 78 74 72 61 20 20  t char *zExtra  
29ef7 20 2f 2a 20 45 72 72 6f 72 20 69 6e 66 6f 72 6d   /* Error inform
29ef8 61 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71  ation */.){.  sq
29ef9 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 44 61 74  lite3 *db = pDat
29efa 61 2d 3e 64 62 3b 0a 20 20 69 66 28 20 21 64 62  a->db;.  if( !db
29efb 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
29efc 26 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  & (db->flags & S
29efd 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f  QLITE_RecoveryMo
29efe 64 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  de)==0 ){.    if
29eff 28 20 7a 4f 62 6a 3d 3d 30 20 29 20 7a 4f 62 6a  ( zObj==0 ) zObj
29f00 20 3d 20 22 3f 22 3b 0a 20 20 20 20 73 71 6c 69   = "?";.    sqli
29f01 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 44 61  te3SetString(pDa
29f02 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 64 62  ta->pzErrMsg, db
29f03 2c 0a 20 20 20 20 20 20 22 6d 61 6c 66 6f 72 6d  ,.      "malform
29f04 65 64 20 64 61 74 61 62 61 73 65 20 73 63 68 65  ed database sche
29f05 6d 61 20 28 25 73 29 22 2c 20 7a 4f 62 6a 29 3b  ma (%s)", zObj);
29f06 0a 20 20 20 20 69 66 28 20 7a 45 78 74 72 61 20  .    if( zExtra 
29f07 29 7b 0a 20 20 20 20 20 20 2a 70 44 61 74 61 2d  ){.      *pData-
29f08 3e 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69  >pzErrMsg = sqli
29f09 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20  te3MAppendf(db, 
29f0a 2a 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67  *pData->pzErrMsg
29f0b 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
29f0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29f0d 20 20 20 20 22 25 73 20 2d 20 25 73 22 2c 20 2a      "%s - %s", *
29f0e 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 2c  pData->pzErrMsg,
29f0f 20 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a   zExtra);.    }.
29f10 20 20 7d 0a 20 20 70 44 61 74 61 2d 3e 72 63 20    }.  pData->rc 
29f11 3d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  = db->mallocFail
29f12 65 64 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ed ? SQLITE_NOME
29f13 4d 20 3a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  M : SQLITE_CORRU
29f14 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  PT;.}../*.** Thi
29f15 73 20 69 73 20 74 68 65 20 63 61 6c 6c 62 61 63  s is the callbac
29f16 6b 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 74 68  k routine for th
29f17 65 20 63 6f 64 65 20 74 68 61 74 20 69 6e 69 74  e code that init
29f18 69 61 6c 69 7a 65 73 20 74 68 65 0a 2a 2a 20 64  ializes the.** d
29f19 61 74 61 62 61 73 65 2e 20 20 53 65 65 20 73 71  atabase.  See sq
29f1a 6c 69 74 65 33 49 6e 69 74 28 29 20 62 65 6c 6f  lite3Init() belo
29f1b 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  w for additional
29f1c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
29f1d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
29f1e 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20 66 72 6f   also called fro
29f1f 6d 20 74 68 65 20 4f 50 5f 50 61 72 73 65 53 63  m the OP_ParseSc
29f20 68 65 6d 61 20 6f 70 63 6f 64 65 20 6f 66 20 74  hema opcode of t
29f21 68 65 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 45  he VDBE..**.** E
29f22 61 63 68 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e  ach callback con
29f23 74 61 69 6e 73 20 74 68 65 20 66 6f 6c 6c 6f 77  tains the follow
29f24 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 3a  ing information:
29f25 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b  .**.**     argv[
29f26 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74 68 69  0] = name of thi
29f27 6e 67 20 62 65 69 6e 67 20 63 72 65 61 74 65 64  ng being created
29f28 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 31 5d 20  .**     argv[1] 
29f29 3d 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62  = root page numb
29f2a 65 72 20 66 6f 72 20 74 61 62 6c 65 20 6f 72 20  er for table or 
29f2b 69 6e 64 65 78 2e 20 30 20 66 6f 72 20 74 72 69  index. 0 for tri
29f2c 67 67 65 72 20 6f 72 20 76 69 65 77 2e 0a 2a 2a  gger or view..**
29f2d 20 20 20 20 20 61 72 67 76 5b 32 5d 20 3d 20 53       argv[2] = S
29f2e 51 4c 20 74 65 78 74 20 66 6f 72 20 74 68 65 20  QL text for the 
29f2f 43 52 45 41 54 45 20 73 74 61 74 65 6d 65 6e 74  CREATE statement
29f30 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ..**.*/.SQLITE_P
29f31 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
29f32 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76  e3InitCallback(v
29f33 6f 69 64 20 2a 70 49 6e 69 74 2c 20 69 6e 74 20  oid *pInit, int 
29f34 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72 67  argc, char **arg
29f35 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74 55 73 65  v, char **NotUse
29f36 64 29 7b 0a 20 20 49 6e 69 74 44 61 74 61 20 2a  d){.  InitData *
29f37 70 44 61 74 61 20 3d 20 28 49 6e 69 74 44 61 74  pData = (InitDat
29f38 61 2a 29 70 49 6e 69 74 3b 0a 20 20 73 71 6c 69  a*)pInit;.  sqli
29f39 74 65 33 20 2a 64 62 20 3d 20 70 44 61 74 61 2d  te3 *db = pData-
29f3a 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d  >db;.  int iDb =
29f3b 20 70 44 61 74 61 2d 3e 69 44 62 3b 0a 0a 20 20   pData->iDb;..  
29f3c 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 33 20  assert( argc==3 
29f3d 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
29f3e 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20  METER2(NotUsed, 
29f3f 61 72 67 63 29 3b 0a 20 20 61 73 73 65 72 74 28  argc);.  assert(
29f40 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
29f41 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
29f42 3b 0a 20 20 44 62 43 6c 65 61 72 50 72 6f 70 65  ;.  DbClearPrope
29f43 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f  rty(db, iDb, DB_
29f44 45 6d 70 74 79 29 3b 0a 20 20 69 66 28 20 64 62  Empty);.  if( db
29f45 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
29f46 7b 0a 20 20 20 20 63 6f 72 72 75 70 74 53 63 68  {.    corruptSch
29f47 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76 5b  ema(pData, argv[
29f48 30 5d 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75  0], 0);.    retu
29f49 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  rn 1;.  }..  ass
29f4a 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
29f4b 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
29f4c 69 66 28 20 61 72 67 76 3d 3d 30 20 29 20 72 65  if( argv==0 ) re
29f4d 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4d 69 67  turn 0;   /* Mig
29f4e 68 74 20 68 61 70 70 65 6e 20 69 66 20 45 4d 50  ht happen if EMP
29f4f 54 59 5f 52 45 53 55 4c 54 5f 43 41 4c 4c 42 41  TY_RESULT_CALLBA
29f50 43 4b 53 20 61 72 65 20 6f 6e 20 2a 2f 0a 20 20  CKS are on */.  
29f51 69 66 28 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29  if( argv[1]==0 )
29f52 7b 0a 20 20 20 20 63 6f 72 72 75 70 74 53 63 68  {.    corruptSch
29f53 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76 5b  ema(pData, argv[
29f54 30 5d 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65 20  0], 0);.  }else 
29f55 69 66 28 20 61 72 67 76 5b 32 5d 20 26 26 20 61  if( argv[2] && a
29f56 72 67 76 5b 32 5d 5b 30 5d 20 29 7b 0a 20 20 20  rgv[2][0] ){.   
29f57 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 70 61 72   /* Call the par
29f58 73 65 72 20 74 6f 20 70 72 6f 63 65 73 73 20 61  ser to process a
29f59 20 43 52 45 41 54 45 20 54 41 42 4c 45 2c 20 49   CREATE TABLE, I
29f5a 4e 44 45 58 20 6f 72 20 56 49 45 57 2e 0a 20 20  NDEX or VIEW..  
29f5b 20 20 2a 2a 20 42 75 74 20 62 65 63 61 75 73 65    ** But because
29f5c 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69   db->init.busy i
29f5d 73 20 73 65 74 20 74 6f 20 31 2c 20 6e 6f 20 56  s set to 1, no V
29f5e 44 42 45 20 63 6f 64 65 20 69 73 20 67 65 6e 65  DBE code is gene
29f5f 72 61 74 65 64 0a 20 20 20 20 2a 2a 20 6f 72 20  rated.    ** or 
29f60 65 78 65 63 75 74 65 64 2e 20 20 41 6c 6c 20 74  executed.  All t
29f61 68 65 20 70 61 72 73 65 72 20 64 6f 65 73 20 69  he parser does i
29f62 73 20 62 75 69 6c 64 20 74 68 65 20 69 6e 74 65  s build the inte
29f63 72 6e 61 6c 20 64 61 74 61 0a 20 20 20 20 2a 2a  rnal data.    **
29f64 20 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74   structures that
29f65 20 64 65 73 63 72 69 62 65 20 74 68 65 20 74 61   describe the ta
29f66 62 6c 65 2c 20 69 6e 64 65 78 2c 20 6f 72 20 76  ble, index, or v
29f67 69 65 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  iew..    */.    
29f68 63 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 20 20  char *zErr;.    
29f69 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73 65  int rc;.    asse
29f6a 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  rt( db->init.bus
29f6b 79 20 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69  y );.    db->ini
29f6c 74 2e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20  t.iDb = iDb;.   
29f6d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75   db->init.newTnu
29f6e 6d 20 3d 20 61 74 6f 69 28 61 72 67 76 5b 31 5d  m = atoi(argv[1]
29f6f 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e  );.    db->init.
29f70 6f 72 70 68 61 6e 54 72 69 67 67 65 72 20 3d 20  orphanTrigger = 
29f71 30 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  0;.    rc = sqli
29f72 74 65 33 5f 65 78 65 63 28 64 62 2c 20 61 72 67  te3_exec(db, arg
29f73 76 5b 32 5d 2c 20 30 2c 20 30 2c 20 26 7a 45 72  v[2], 0, 0, &zEr
29f74 72 29 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74  r);.    db->init
29f75 2e 69 44 62 20 3d 20 30 3b 0a 20 20 20 20 61 73  .iDb = 0;.    as
29f76 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
29f77 5f 4f 4b 20 7c 7c 20 7a 45 72 72 3d 3d 30 20 29  _OK || zErr==0 )
29f78 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  ;.    if( SQLITE
29f79 5f 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 20  _OK!=rc ){.     
29f7a 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 6f 72   if( db->init.or
29f7b 70 68 61 6e 54 72 69 67 67 65 72 20 29 7b 0a 20  phanTrigger ){. 
29f7c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69         assert( i
29f7d 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 7d  Db==1 );.      }
29f7e 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44  else{.        pD
29f7f 61 74 61 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20  ata->rc = rc;.  
29f80 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
29f81 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
29f82 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c          db->mall
29f83 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
29f84 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
29f85 72 63 21 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52  rc!=SQLITE_INTER
29f86 52 55 50 54 20 26 26 20 72 63 21 3d 53 51 4c 49  RUPT && rc!=SQLI
29f87 54 45 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20  TE_LOCKED ){.   
29f88 20 20 20 20 20 20 20 63 6f 72 72 75 70 74 53 63         corruptSc
29f89 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67 76  hema(pData, argv
29f8a 5b 30 5d 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  [0], zErr);.    
29f8b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
29f8c 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
29f8d 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  e(db, zErr);.   
29f8e 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61   }.  }else if( a
29f8f 72 67 76 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20  rgv[0]==0 ){.   
29f90 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70   corruptSchema(p
29f91 44 61 74 61 2c 20 30 2c 20 30 29 3b 0a 20 20 7d  Data, 0, 0);.  }
29f92 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20  else{.    /* If 
29f93 74 68 65 20 53 51 4c 20 63 6f 6c 75 6d 6e 20 69  the SQL column i
29f94 73 20 62 6c 61 6e 6b 20 69 74 20 6d 65 61 6e 73  s blank it means
29f95 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e 64 65   this is an inde
29f96 78 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 77 61  x that.    ** wa
29f97 73 20 63 72 65 61 74 65 64 20 74 6f 20 62 65 20  s created to be 
29f98 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
29f99 6f 72 20 74 6f 20 66 75 6c 66 69 6c 6c 20 61 20  or to fulfill a 
29f9a 55 4e 49 51 55 45 0a 20 20 20 20 2a 2a 20 63 6f  UNIQUE.    ** co
29f9b 6e 73 74 72 61 69 6e 74 20 66 6f 72 20 61 20 43  nstraint for a C
29f9c 52 45 41 54 45 20 54 41 42 4c 45 2e 20 20 54 68  REATE TABLE.  Th
29f9d 65 20 69 6e 64 65 78 20 73 68 6f 75 6c 64 20 68  e index should h
29f9e 61 76 65 20 61 6c 72 65 61 64 79 0a 20 20 20 20  ave already.    
29f9f 2a 2a 20 62 65 65 6e 20 63 72 65 61 74 65 64 20  ** been created 
29fa0 77 68 65 6e 20 77 65 20 70 72 6f 63 65 73 73 65  when we processe
29fa1 64 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  d the CREATE TAB
29fa2 4c 45 2e 20 20 41 6c 6c 20 77 65 20 68 61 76 65  LE.  All we have
29fa3 0a 20 20 20 20 2a 2a 20 74 6f 20 64 6f 20 68 65  .    ** to do he
29fa4 72 65 20 69 73 20 72 65 63 6f 72 64 20 74 68 65  re is record the
29fa5 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
29fa6 72 20 66 6f 72 20 74 68 61 74 20 69 6e 64 65 78  r for that index
29fa7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 64  ..    */.    Ind
29fa8 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 20 20  ex *pIndex;.    
29fa9 70 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33  pIndex = sqlite3
29faa 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 61 72  FindIndex(db, ar
29fab 67 76 5b 30 5d 2c 20 64 62 2d 3e 61 44 62 5b 69  gv[0], db->aDb[i
29fac 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  Db].zName);.    
29fad 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29 7b  if( pIndex==0 ){
29fae 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63  .      /* This c
29faf 61 6e 20 6f 63 63 75 72 20 69 66 20 74 68 65 72  an occur if ther
29fb0 65 20 65 78 69 73 74 73 20 61 6e 20 69 6e 64 65  e exists an inde
29fb1 78 20 6f 6e 20 61 20 54 45 4d 50 20 74 61 62 6c  x on a TEMP tabl
29fb2 65 20 77 68 69 63 68 0a 20 20 20 20 20 20 2a 2a  e which.      **
29fb3 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 6e 61   has the same na
29fb4 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 20 69 6e  me as another in
29fb5 64 65 78 20 6f 6e 20 61 20 70 65 72 6d 61 6e 65  dex on a permane
29fb6 6e 74 20 69 6e 64 65 78 2e 20 20 53 69 6e 63 65  nt index.  Since
29fb7 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 65  .      ** the pe
29fb8 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65 20 69 73  rmanent table is
29fb9 20 68 69 64 64 65 6e 20 62 79 20 74 68 65 20 54   hidden by the T
29fba 45 4d 50 20 74 61 62 6c 65 2c 20 77 65 20 63 61  EMP table, we ca
29fbb 6e 20 61 6c 73 6f 0a 20 20 20 20 20 20 2a 2a 20  n also.      ** 
29fbc 73 61 66 65 6c 79 20 69 67 6e 6f 72 65 20 74 68  safely ignore th
29fbd 65 20 69 6e 64 65 78 20 6f 6e 20 74 68 65 20 70  e index on the p
29fbe 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65 2e 0a  ermanent table..
29fbf 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 2f        */.      /
29fc0 2a 20 44 6f 20 4e 6f 74 68 69 6e 67 20 2a 2f 3b  * Do Nothing */;
29fc1 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73  .    }else if( s
29fc2 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 61  qlite3GetInt32(a
29fc3 72 67 76 5b 31 5d 2c 20 26 70 49 6e 64 65 78 2d  rgv[1], &pIndex-
29fc4 3e 74 6e 75 6d 29 3d 3d 30 20 29 7b 0a 20 20 20  >tnum)==0 ){.   
29fc5 20 20 20 63 6f 72 72 75 70 74 53 63 68 65 6d 61     corruptSchema
29fc6 28 70 44 61 74 61 2c 20 61 72 67 76 5b 30 5d 2c  (pData, argv[0],
29fc7 20 22 69 6e 76 61 6c 69 64 20 72 6f 6f 74 70 61   "invalid rootpa
29fc8 67 65 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ge");.    }.  }.
29fc9 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
29fca 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
29fcb 72 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73  read the databas
29fcc 65 20 73 63 68 65 6d 61 20 61 6e 64 20 69 6e 69  e schema and ini
29fcd 74 69 61 6c 69 7a 65 20 69 6e 74 65 72 6e 61 6c  tialize internal
29fce 0a 2a 2a 20 64 61 74 61 20 73 74 72 75 63 74 75  .** data structu
29fcf 72 65 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65  res for a single
29fd0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
29fd1 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   The index of th
29fd2 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69  e.** database fi
29fd3 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 69  le is given by i
29fd4 44 62 2e 20 20 69 44 62 3d 3d 30 20 69 73 20 75  Db.  iDb==0 is u
29fd5 73 65 64 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  sed for the main
29fd6 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 69  .** database.  i
29fd7 44 62 3d 3d 31 20 73 68 6f 75 6c 64 20 6e 65 76  Db==1 should nev
29fd8 65 72 20 62 65 20 75 73 65 64 2e 20 20 69 44 62  er be used.  iDb
29fd9 3e 3d 32 20 69 73 20 75 73 65 64 20 66 6f 72 0a  >=2 is used for.
29fda 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  ** auxiliary dat
29fdb 61 62 61 73 65 73 2e 20 20 52 65 74 75 72 6e 20  abases.  Return 
29fdc 6f 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54  one of the SQLIT
29fdd 45 5f 20 65 72 72 6f 72 20 63 6f 64 65 73 20 74  E_ error codes t
29fde 6f 0a 2a 2a 20 69 6e 64 69 63 61 74 65 20 73 75  o.** indicate su
29fdf 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65  ccess or failure
29fe0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
29fe1 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28 73  sqlite3InitOne(s
29fe2 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
29fe3 69 44 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72  iDb, char **pzEr
29fe4 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20 72 63 3b  rMsg){.  int rc;
29fe5 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
29fe6 73 69 7a 65 3b 0a 20 20 54 61 62 6c 65 20 2a 70  size;.  Table *p
29fe7 54 61 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a  Tab;.  Db *pDb;.
29fe8 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 61 7a    char const *az
29fe9 41 72 67 5b 34 5d 3b 0a 20 20 69 6e 74 20 6d 65  Arg[4];.  int me
29fea 74 61 5b 35 5d 3b 0a 20 20 49 6e 69 74 44 61 74  ta[5];.  InitDat
29feb 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20 63 68  a initData;.  ch
29fec 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65  ar const *zMaste
29fed 72 53 63 68 65 6d 61 3b 0a 20 20 63 68 61 72 20  rSchema;.  char 
29fee 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65 72 4e 61  const *zMasterNa
29fef 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c  me = SCHEMA_TABL
29ff0 45 28 69 44 62 29 3b 0a 20 20 69 6e 74 20 6f 70  E(iDb);.  int op
29ff1 65 6e 65 64 54 72 61 6e 73 61 63 74 69 6f 6e 20  enedTransaction 
29ff2 3d 20 30 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  = 0;..  /*.  ** 
29ff3 54 68 65 20 6d 61 73 74 65 72 20 64 61 74 61 62  The master datab
29ff4 61 73 65 20 74 61 62 6c 65 20 68 61 73 20 61 20  ase table has a 
29ff5 73 74 72 75 63 74 75 72 65 20 6c 69 6b 65 20 74  structure like t
29ff6 68 69 73 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69  his.  */.  stati
29ff7 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6d 61 73  c const char mas
29ff8 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20 3d 20 0a  ter_schema[] = .
29ff9 20 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42       "CREATE TAB
29ffa 4c 45 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  LE sqlite_master
29ffb 28 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 79 70  (\n".     "  typ
29ffc 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20  e text,\n".     
29ffd 22 20 20 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22  "  name text,\n"
29ffe 0a 20 20 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d  .     "  tbl_nam
29fff 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20  e text,\n".     
2a000 22 20 20 72 6f 6f 74 70 61 67 65 20 69 6e 74 65  "  rootpage inte
2a001 67 65 72 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20  ger,\n".     "  
2a002 73 71 6c 20 74 65 78 74 5c 6e 22 0a 20 20 20 20  sql text\n".    
2a003 20 22 29 22 0a 20 20 3b 0a 23 69 66 6e 64 65 66   ")".  ;.#ifndef
2a004 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d   SQLITE_OMIT_TEM
2a005 50 44 42 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  PDB.  static con
2a006 73 74 20 63 68 61 72 20 74 65 6d 70 5f 6d 61 73  st char temp_mas
2a007 74 65 72 5f 73 63 68 65 6d 61 5b 5d 20 3d 20 0a  ter_schema[] = .
2a008 20 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d       "CREATE TEM
2a009 50 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f 74  P TABLE sqlite_t
2a00a 65 6d 70 5f 6d 61 73 74 65 72 28 5c 6e 22 0a 20  emp_master(\n". 
2a00b 20 20 20 20 22 20 20 74 79 70 65 20 74 65 78 74      "  type text
2a00c 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 6e 61 6d  ,\n".     "  nam
2a00d 65 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20  e text,\n".     
2a00e 22 20 20 74 62 6c 5f 6e 61 6d 65 20 74 65 78 74  "  tbl_name text
2a00f 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 72 6f 6f  ,\n".     "  roo
2a010 74 70 61 67 65 20 69 6e 74 65 67 65 72 2c 5c 6e  tpage integer,\n
2a011 22 0a 20 20 20 20 20 22 20 20 73 71 6c 20 74 65  ".     "  sql te
2a012 78 74 5c 6e 22 0a 20 20 20 20 20 22 29 22 0a 20  xt\n".     ")". 
2a013 20 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69   ;.#else.  #defi
2a014 6e 65 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f 73  ne temp_master_s
2a015 63 68 65 6d 61 20 30 0a 23 65 6e 64 69 66 0a 0a  chema 0.#endif..
2a016 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
2a017 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
2a018 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
2a019 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
2a01a 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  a );.  assert( s
2a01b 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
2a01c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
2a01d 20 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d 31    assert( iDb==1
2a01e 20 7c 7c 20 73 71 6c 69 74 65 33 42 74 72 65 65   || sqlite3Btree
2a01f 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61  HoldsMutex(db->a
2a020 44 62 5b 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a  Db[iDb].pBt) );.
2a021 0a 20 20 2f 2a 20 7a 4d 61 73 74 65 72 53 63 68  .  /* zMasterSch
2a022 65 6d 61 20 61 6e 64 20 7a 49 6e 69 74 53 63 72  ema and zInitScr
2a023 69 70 74 20 61 72 65 20 73 65 74 20 74 6f 20 70  ipt are set to p
2a024 6f 69 6e 74 20 61 74 20 74 68 65 20 6d 61 73 74  oint at the mast
2a025 65 72 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20 61  er schema.  ** a
2a026 6e 64 20 69 6e 69 74 69 61 6c 69 73 61 74 69 6f  nd initialisatio
2a027 6e 20 73 63 72 69 70 74 20 61 70 70 72 6f 70 72  n script appropr
2a028 69 61 74 65 20 66 6f 72 20 74 68 65 20 64 61 74  iate for the dat
2a029 61 62 61 73 65 20 62 65 69 6e 67 0a 20 20 2a 2a  abase being.  **
2a02a 20 69 6e 69 74 69 61 6c 69 73 65 64 2e 20 7a 4d   initialised. zM
2a02b 61 73 74 65 72 4e 61 6d 65 20 69 73 20 74 68 65  asterName is the
2a02c 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6d 61 73   name of the mas
2a02d 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  ter table..  */.
2a02e 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d 50    if( !OMIT_TEMP
2a02f 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b 0a  DB && iDb==1 ){.
2a030 20 20 20 20 7a 4d 61 73 74 65 72 53 63 68 65 6d      zMasterSchem
2a031 61 20 3d 20 74 65 6d 70 5f 6d 61 73 74 65 72 5f  a = temp_master_
2a032 73 63 68 65 6d 61 3b 0a 20 20 7d 65 6c 73 65 7b  schema;.  }else{
2a033 0a 20 20 20 20 7a 4d 61 73 74 65 72 53 63 68 65  .    zMasterSche
2a034 6d 61 20 3d 20 6d 61 73 74 65 72 5f 73 63 68 65  ma = master_sche
2a035 6d 61 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65  ma;.  }.  zMaste
2a036 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54  rName = SCHEMA_T
2a037 41 42 4c 45 28 69 44 62 29 3b 0a 0a 20 20 2f 2a  ABLE(iDb);..  /*
2a038 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 73   Construct the s
2a039 63 68 65 6d 61 20 74 61 62 6c 65 73 2e 20 20 2a  chema tables.  *
2a03a 2f 0a 20 20 61 7a 41 72 67 5b 30 5d 20 3d 20 7a  /.  azArg[0] = z
2a03b 4d 61 73 74 65 72 4e 61 6d 65 3b 0a 20 20 61 7a  MasterName;.  az
2a03c 41 72 67 5b 31 5d 20 3d 20 22 31 22 3b 0a 20 20  Arg[1] = "1";.  
2a03d 61 7a 41 72 67 5b 32 5d 20 3d 20 7a 4d 61 73 74  azArg[2] = zMast
2a03e 65 72 53 63 68 65 6d 61 3b 0a 20 20 61 7a 41 72  erSchema;.  azAr
2a03f 67 5b 33 5d 20 3d 20 30 3b 0a 20 20 69 6e 69 74  g[3] = 0;.  init
2a040 44 61 74 61 2e 64 62 20 3d 20 64 62 3b 0a 20 20  Data.db = db;.  
2a041 69 6e 69 74 44 61 74 61 2e 69 44 62 20 3d 20 69  initData.iDb = i
2a042 44 62 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 72  Db;.  initData.r
2a043 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
2a044 20 69 6e 69 74 44 61 74 61 2e 70 7a 45 72 72 4d   initData.pzErrM
2a045 73 67 20 3d 20 70 7a 45 72 72 4d 73 67 3b 0a 20  sg = pzErrMsg;. 
2a046 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2a047 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 73  fetyOff(db);.  s
2a048 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62 61  qlite3InitCallba
2a049 63 6b 28 26 69 6e 69 74 44 61 74 61 2c 20 33 2c  ck(&initData, 3,
2a04a 20 28 63 68 61 72 20 2a 2a 29 61 7a 41 72 67 2c   (char **)azArg,
2a04b 20 30 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c   0);.  (void)sql
2a04c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
2a04d 3b 0a 20 20 69 66 28 20 69 6e 69 74 44 61 74 61  ;.  if( initData
2a04e 2e 72 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  .rc ){.    rc = 
2a04f 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20  initData.rc;.   
2a050 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b   goto error_out;
2a051 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 73 71  .  }.  pTab = sq
2a052 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
2a053 62 2c 20 7a 4d 61 73 74 65 72 4e 61 6d 65 2c 20  b, zMasterName, 
2a054 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
2a055 6d 65 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59  me);.  if( ALWAY
2a056 53 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 70  S(pTab) ){.    p
2a057 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d  Tab->tabFlags |=
2a058 20 54 46 5f 52 65 61 64 6f 6e 6c 79 3b 0a 20 20   TF_Readonly;.  
2a059 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 61  }..  /* Create a
2a05a 20 63 75 72 73 6f 72 20 74 6f 20 68 6f 6c 64 20   cursor to hold 
2a05b 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 65  the database ope
2a05c 6e 0a 20 20 2a 2f 0a 20 20 70 44 62 20 3d 20 26  n.  */.  pDb = &
2a05d 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20  db->aDb[iDb];.  
2a05e 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20  if( pDb->pBt==0 
2a05f 29 7b 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54  ){.    if( !OMIT
2a060 5f 54 45 4d 50 44 42 20 26 26 20 41 4c 57 41 59  _TEMPDB && ALWAY
2a061 53 28 69 44 62 3d 3d 31 29 20 29 7b 0a 20 20 20  S(iDb==1) ){.   
2a062 20 20 20 44 62 53 65 74 50 72 6f 70 65 72 74 79     DbSetProperty
2a063 28 64 62 2c 20 31 2c 20 44 42 5f 53 63 68 65 6d  (db, 1, DB_Schem
2a064 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20 20 7d 0a  aLoaded);.    }.
2a065 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2a066 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
2a067 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20  If there is not 
2a068 61 6c 72 65 61 64 79 20 61 20 72 65 61 64 2d 6f  already a read-o
2a069 6e 6c 79 20 28 6f 72 20 72 65 61 64 2d 77 72 69  nly (or read-wri
2a06a 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  te) transaction 
2a06b 6f 70 65 6e 65 64 0a 20 20 2a 2a 20 6f 6e 20 74  opened.  ** on t
2a06c 68 65 20 62 2d 74 72 65 65 20 64 61 74 61 62 61  he b-tree databa
2a06d 73 65 2c 20 6f 70 65 6e 20 6f 6e 65 20 6e 6f 77  se, open one now
2a06e 2e 20 49 66 20 61 20 74 72 61 6e 73 61 63 74 69  . If a transacti
2a06f 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2c 20 69 74  on is opened, it
2a070 20 0a 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63   .  ** will be c
2a071 6c 6f 73 65 64 20 62 65 66 6f 72 65 20 74 68 69  losed before thi
2a072 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
2a073 6e 73 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ns.  */.  sqlite
2a074 33 42 74 72 65 65 45 6e 74 65 72 28 70 44 62 2d  3BtreeEnter(pDb-
2a075 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 21 73 71  >pBt);.  if( !sq
2a076 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65  lite3BtreeIsInRe
2a077 61 64 54 72 61 6e 73 28 70 44 62 2d 3e 70 42 74  adTrans(pDb->pBt
2a078 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  ) ){.    rc = sq
2a079 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 54  lite3BtreeBeginT
2a07a 72 61 6e 73 28 70 44 62 2d 3e 70 42 74 2c 20 30  rans(pDb->pBt, 0
2a07b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
2a07c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2a07d 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
2a07e 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c  ng(pzErrMsg, db,
2a07f 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72   "%s", sqlite3Er
2a080 72 53 74 72 28 72 63 29 29 3b 0a 20 20 20 20 20  rStr(rc));.     
2a081 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72   goto initone_er
2a082 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  ror_out;.    }. 
2a083 20 20 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63     openedTransac
2a084 74 69 6f 6e 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20  tion = 1;.  }.. 
2a085 20 2f 2a 20 47 65 74 20 74 68 65 20 64 61 74 61   /* Get the data
2a086 62 61 73 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d  base meta inform
2a087 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  ation..  **.  **
2a088 20 4d 65 74 61 20 76 61 6c 75 65 73 20 61 72 65   Meta values are
2a089 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a   as follows:.  *
2a08a 2a 20 20 20 20 6d 65 74 61 5b 30 5d 20 20 20 53  *    meta[0]   S
2a08b 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 43  chema cookie.  C
2a08c 68 61 6e 67 65 73 20 77 69 74 68 20 65 61 63 68  hanges with each
2a08d 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a   schema change..
2a08e 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 31 5d 20    **    meta[1] 
2a08f 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 6f 66    File format of
2a090 20 73 63 68 65 6d 61 20 6c 61 79 65 72 2e 0a 20   schema layer.. 
2a091 20 2a 2a 20 20 20 20 6d 65 74 61 5b 32 5d 20 20   **    meta[2]  
2a092 20 53 69 7a 65 20 6f 66 20 74 68 65 20 70 61 67   Size of the pag
2a093 65 20 63 61 63 68 65 2e 0a 20 20 2a 2a 20 20 20  e cache..  **   
2a094 20 6d 65 74 61 5b 33 5d 20 20 20 4c 61 72 67 65   meta[3]   Large
2a095 73 74 20 72 6f 6f 74 70 61 67 65 20 28 61 75 74  st rootpage (aut
2a096 6f 2f 69 6e 63 72 5f 76 61 63 75 75 6d 20 6d 6f  o/incr_vacuum mo
2a097 64 65 29 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  de).  **    meta
2a098 5b 34 5d 20 20 20 44 62 20 74 65 78 74 20 65 6e  [4]   Db text en
2a099 63 6f 64 69 6e 67 2e 20 31 3a 55 54 46 2d 38 20  coding. 1:UTF-8 
2a09a 32 3a 55 54 46 2d 31 36 4c 45 20 33 3a 55 54 46  2:UTF-16LE 3:UTF
2a09b 2d 31 36 42 45 0a 20 20 2a 2a 20 20 20 20 6d 65  -16BE.  **    me
2a09c 74 61 5b 35 5d 20 20 20 55 73 65 72 20 76 65 72  ta[5]   User ver
2a09d 73 69 6f 6e 0a 20 20 2a 2a 20 20 20 20 6d 65 74  sion.  **    met
2a09e 61 5b 36 5d 20 20 20 49 6e 63 72 65 6d 65 6e 74  a[6]   Increment
2a09f 61 6c 20 76 61 63 75 75 6d 20 6d 6f 64 65 0a 20  al vacuum mode. 
2a0a0 20 2a 2a 20 20 20 20 6d 65 74 61 5b 37 5d 20 20   **    meta[7]  
2a0a1 20 75 6e 75 73 65 64 0a 20 20 2a 2a 20 20 20 20   unused.  **    
2a0a2 6d 65 74 61 5b 38 5d 20 20 20 75 6e 75 73 65 64  meta[8]   unused
2a0a3 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 39 5d  .  **    meta[9]
2a0a4 20 20 20 75 6e 75 73 65 64 0a 20 20 2a 2a 0a 20     unused.  **. 
2a0a5 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 65 20 23 64   ** Note: The #d
2a0a6 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 55 54  efined SQLITE_UT
2a0a7 46 2a 20 73 79 6d 62 6f 6c 73 20 69 6e 20 73 71  F* symbols in sq
2a0a8 6c 69 74 65 49 6e 74 2e 68 20 63 6f 72 72 65 73  liteInt.h corres
2a0a9 70 6f 6e 64 20 74 6f 0a 20 20 2a 2a 20 74 68 65  pond to.  ** the
2a0aa 20 70 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73   possible values
2a0ab 20 6f 66 20 6d 65 74 61 5b 34 5d 2e 0a 20 20 2a   of meta[4]..  *
2a0ac 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41  /.  for(i=0; i<A
2a0ad 72 72 61 79 53 69 7a 65 28 6d 65 74 61 29 3b 20  rraySize(meta); 
2a0ae 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
2a0af 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 44  3BtreeGetMeta(pD
2a0b0 62 2d 3e 70 42 74 2c 20 69 2b 31 2c 20 28 75 33  b->pBt, i+1, (u3
2a0b1 32 20 2a 29 26 6d 65 74 61 5b 69 5d 29 3b 0a 20  2 *)&meta[i]);. 
2a0b2 20 7d 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d   }.  pDb->pSchem
2a0b3 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65  a->schema_cookie
2a0b4 20 3d 20 6d 65 74 61 5b 42 54 52 45 45 5f 53 43   = meta[BTREE_SC
2a0b5 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2d 31 5d 3b  HEMA_VERSION-1];
2a0b6 0a 0a 20 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e  ..  /* If openin
2a0b7 67 20 61 20 6e 6f 6e 2d 65 6d 70 74 79 20 64 61  g a non-empty da
2a0b8 74 61 62 61 73 65 2c 20 63 68 65 63 6b 20 74 68  tabase, check th
2a0b9 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 2e  e text encoding.
2a0ba 20 46 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d 61   For the.  ** ma
2a0bb 69 6e 20 64 61 74 61 62 61 73 65 2c 20 73 65 74  in database, set
2a0bc 20 73 71 6c 69 74 65 33 2e 65 6e 63 20 74 6f 20   sqlite3.enc to 
2a0bd 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  the encoding of 
2a0be 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
2a0bf 65 2e 0a 20 20 2a 2a 20 46 6f 72 20 61 6e 20 61  e..  ** For an a
2a0c0 74 74 61 63 68 65 64 20 64 62 2c 20 69 74 20 69  ttached db, it i
2a0c1 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68  s an error if th
2a0c2 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 6e 6f  e encoding is no
2a0c3 74 20 74 68 65 20 73 61 6d 65 0a 20 20 2a 2a 20  t the same.  ** 
2a0c4 61 73 20 73 71 6c 69 74 65 33 2e 65 6e 63 2e 0a  as sqlite3.enc..
2a0c5 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 74 61 5b    */.  if( meta[
2a0c6 42 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44  BTREE_TEXT_ENCOD
2a0c7 49 4e 47 2d 31 5d 20 29 7b 20 20 2f 2a 20 74 65  ING-1] ){  /* te
2a0c8 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20  xt encoding */. 
2a0c9 20 20 20 69 66 28 20 69 44 62 3d 3d 30 20 29 7b     if( iDb==0 ){
2a0ca 0a 20 20 20 20 20 20 75 38 20 65 6e 63 6f 64 69  .      u8 encodi
2a0cb 6e 67 3b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  ng;.      /* If 
2a0cc 6f 70 65 6e 69 6e 67 20 74 68 65 20 6d 61 69 6e  opening the main
2a0cd 20 64 61 74 61 62 61 73 65 2c 20 73 65 74 20 45   database, set E
2a0ce 4e 43 28 64 62 29 2e 20 2a 2f 0a 20 20 20 20 20  NC(db). */.     
2a0cf 20 65 6e 63 6f 64 69 6e 67 20 3d 20 28 75 38 29   encoding = (u8)
2a0d0 6d 65 74 61 5b 42 54 52 45 45 5f 54 45 58 54 5f  meta[BTREE_TEXT_
2a0d1 45 4e 43 4f 44 49 4e 47 2d 31 5d 20 26 20 33 3b  ENCODING-1] & 3;
2a0d2 0a 20 20 20 20 20 20 69 66 28 20 65 6e 63 6f 64  .      if( encod
2a0d3 69 6e 67 3d 3d 30 20 29 20 65 6e 63 6f 64 69 6e  ing==0 ) encodin
2a0d4 67 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  g = SQLITE_UTF8;
2a0d5 0a 20 20 20 20 20 20 45 4e 43 28 64 62 29 20 3d  .      ENC(db) =
2a0d6 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 20 20 20   encoding;.     
2a0d7 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d   db->pDfltColl =
2a0d8 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
2a0d9 53 65 71 28 64 62 2c 20 53 51 4c 49 54 45 5f 55  Seq(db, SQLITE_U
2a0da 54 46 38 2c 20 22 42 49 4e 41 52 59 22 2c 20 30  TF8, "BINARY", 0
2a0db 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2a0dc 20 20 20 20 2f 2a 20 49 66 20 6f 70 65 6e 69 6e      /* If openin
2a0dd 67 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 61  g an attached da
2a0de 74 61 62 61 73 65 2c 20 74 68 65 20 65 6e 63 6f  tabase, the enco
2a0df 64 69 6e 67 20 6d 75 63 68 20 6d 61 74 63 68 20  ding much match 
2a0e0 45 4e 43 28 64 62 29 20 2a 2f 0a 20 20 20 20 20  ENC(db) */.     
2a0e1 20 69 66 28 20 6d 65 74 61 5b 42 54 52 45 45 5f   if( meta[BTREE_
2a0e2 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2d 31 5d  TEXT_ENCODING-1]
2a0e3 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a 20 20 20  !=ENC(db) ){.   
2a0e4 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53       sqlite3SetS
2a0e5 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20  tring(pzErrMsg, 
2a0e6 64 62 2c 20 22 61 74 74 61 63 68 65 64 20 64 61  db, "attached da
2a0e7 74 61 62 61 73 65 73 20 6d 75 73 74 20 75 73 65  tabases must use
2a0e8 20 74 68 65 20 73 61 6d 65 22 0a 20 20 20 20 20   the same".     
2a0e9 20 20 20 20 20 20 20 22 20 74 65 78 74 20 65 6e         " text en
2a0ea 63 6f 64 69 6e 67 20 61 73 20 6d 61 69 6e 20 64  coding as main d
2a0eb 61 74 61 62 61 73 65 22 29 3b 0a 20 20 20 20 20  atabase");.     
2a0ec 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
2a0ed 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 67 6f  RROR;.        go
2a0ee 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72  to initone_error
2a0ef 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  _out;.      }.  
2a0f0 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
2a0f1 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28 64   DbSetProperty(d
2a0f2 62 2c 20 69 44 62 2c 20 44 42 5f 45 6d 70 74 79  b, iDb, DB_Empty
2a0f3 29 3b 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70 53  );.  }.  pDb->pS
2a0f4 63 68 65 6d 61 2d 3e 65 6e 63 20 3d 20 45 4e 43  chema->enc = ENC
2a0f5 28 64 62 29 3b 0a 0a 20 20 69 66 28 20 70 44 62  (db);..  if( pDb
2a0f6 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65  ->pSchema->cache
2a0f7 5f 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20  _size==0 ){.    
2a0f8 73 69 7a 65 20 3d 20 6d 65 74 61 5b 42 54 52 45  size = meta[BTRE
2a0f9 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f  E_DEFAULT_CACHE_
2a0fa 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 69 66 28  SIZE-1];.    if(
2a0fb 20 73 69 7a 65 3d 3d 30 20 29 7b 20 73 69 7a 65   size==0 ){ size
2a0fc 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   = SQLITE_DEFAUL
2a0fd 54 5f 43 41 43 48 45 5f 53 49 5a 45 3b 20 7d 0a  T_CACHE_SIZE; }.
2a0fe 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29      if( size<0 )
2a0ff 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20   size = -size;. 
2a100 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d     pDb->pSchema-
2a101 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20 73 69  >cache_size = si
2a102 7a 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  ze;.    sqlite3B
2a103 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65  treeSetCacheSize
2a104 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e  (pDb->pBt, pDb->
2a105 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73  pSchema->cache_s
2a106 69 7a 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a  ize);.  }..  /*.
2a107 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74    ** file_format
2a108 3d 3d 31 20 20 20 20 56 65 72 73 69 6f 6e 20 33  ==1    Version 3
2a109 2e 30 2e 30 2e 0a 20 20 2a 2a 20 66 69 6c 65 5f  .0.0..  ** file_
2a10a 66 6f 72 6d 61 74 3d 3d 32 20 20 20 20 56 65 72  format==2    Ver
2a10b 73 69 6f 6e 20 33 2e 31 2e 33 2e 20 20 2f 2f 20  sion 3.1.3.  // 
2a10c 41 4c 54 45 52 20 54 41 42 4c 45 20 41 44 44 20  ALTER TABLE ADD 
2a10d 43 4f 4c 55 4d 4e 0a 20 20 2a 2a 20 66 69 6c 65  COLUMN.  ** file
2a10e 5f 66 6f 72 6d 61 74 3d 3d 33 20 20 20 20 56 65  _format==3    Ve
2a10f 72 73 69 6f 6e 20 33 2e 31 2e 34 2e 20 20 2f 2f  rsion 3.1.4.  //
2a110 20 64 69 74 74 6f 20 62 75 74 20 77 69 74 68 20   ditto but with 
2a111 6e 6f 6e 2d 4e 55 4c 4c 20 64 65 66 61 75 6c 74  non-NULL default
2a112 73 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d  s.  ** file_form
2a113 61 74 3d 3d 34 20 20 20 20 56 65 72 73 69 6f 6e  at==4    Version
2a114 20 33 2e 33 2e 30 2e 20 20 2f 2f 20 44 45 53 43   3.3.0.  // DESC
2a115 20 69 6e 64 69 63 65 73 2e 20 20 42 6f 6f 6c 65   indices.  Boole
2a116 61 6e 20 63 6f 6e 73 74 61 6e 74 73 0a 20 20 2a  an constants.  *
2a117 2f 0a 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  /.  pDb->pSchema
2a118 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20  ->file_format = 
2a119 28 75 38 29 6d 65 74 61 5b 42 54 52 45 45 5f 46  (u8)meta[BTREE_F
2a11a 49 4c 45 5f 46 4f 52 4d 41 54 2d 31 5d 3b 0a 20  ILE_FORMAT-1];. 
2a11b 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d   if( pDb->pSchem
2a11c 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d  a->file_format==
2a11d 30 20 29 7b 0a 20 20 20 20 70 44 62 2d 3e 70 53  0 ){.    pDb->pS
2a11e 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
2a11f 61 74 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66  at = 1;.  }.  if
2a120 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  ( pDb->pSchema->
2a121 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 53 51 4c 49  file_format>SQLI
2a122 54 45 5f 4d 41 58 5f 46 49 4c 45 5f 46 4f 52 4d  TE_MAX_FILE_FORM
2a123 41 54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  AT ){.    sqlite
2a124 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72  3SetString(pzErr
2a125 4d 73 67 2c 20 64 62 2c 20 22 75 6e 73 75 70 70  Msg, db, "unsupp
2a126 6f 72 74 65 64 20 66 69 6c 65 20 66 6f 72 6d 61  orted file forma
2a127 74 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  t");.    rc = SQ
2a128 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
2a129 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f 65 72 72  goto initone_err
2a12a 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f  or_out;.  }..  /
2a12b 2a 20 54 69 63 6b 65 74 20 23 32 38 30 34 3a 20  * Ticket #2804: 
2a12c 20 57 68 65 6e 20 77 65 20 6f 70 65 6e 20 61 20   When we open a 
2a12d 64 61 74 61 62 61 73 65 20 69 6e 20 74 68 65 20  database in the 
2a12e 6e 65 77 65 72 20 66 69 6c 65 20 66 6f 72 6d 61  newer file forma
2a12f 74 2c 0a 20 20 2a 2a 20 63 6c 65 61 72 20 74 68  t,.  ** clear th
2a130 65 20 6c 65 67 61 63 79 5f 66 69 6c 65 5f 66 6f  e legacy_file_fo
2a131 72 6d 61 74 20 70 72 61 67 6d 61 20 66 6c 61 67  rmat pragma flag
2a132 20 73 6f 20 74 68 61 74 20 61 20 56 41 43 55 55   so that a VACUU
2a133 4d 20 77 69 6c 6c 0a 20 20 2a 2a 20 6e 6f 74 20  M will.  ** not 
2a134 64 6f 77 6e 67 72 61 64 65 20 74 68 65 20 64 61  downgrade the da
2a135 74 61 62 61 73 65 20 61 6e 64 20 74 68 75 73 20  tabase and thus 
2a136 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 64  invalidate any d
2a137 65 73 63 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 69  escending.  ** i
2a138 6e 64 69 63 65 73 20 74 68 61 74 20 74 68 65 20  ndices that the 
2a139 75 73 65 72 20 6d 69 67 68 74 20 68 61 76 65 20  user might have 
2a13a 63 72 65 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  created..  */.  
2a13b 69 66 28 20 69 44 62 3d 3d 30 20 26 26 20 6d 65  if( iDb==0 && me
2a13c 74 61 5b 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f  ta[BTREE_FILE_FO
2a13d 52 4d 41 54 2d 31 5d 3e 3d 34 20 29 7b 0a 20 20  RMAT-1]>=4 ){.  
2a13e 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e    db->flags &= ~
2a13f 53 51 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c  SQLITE_LegacyFil
2a140 65 46 6d 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  eFmt;.  }..  /* 
2a141 52 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 20  Read the schema 
2a142 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20  information out 
2a143 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 74 61  of the schema ta
2a144 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65  bles.  */.  asse
2a145 72 74 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73  rt( db->init.bus
2a146 79 20 29 3b 0a 20 20 7b 0a 20 20 20 20 63 68 61  y );.  {.    cha
2a147 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 7a 53 71  r *zSql;.    zSq
2a148 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  l = sqlite3MPrin
2a149 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20  tf(db, .        
2a14a 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f  "SELECT name, ro
2a14b 6f 74 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d  otpage, sql FROM
2a14c 20 27 25 71 27 2e 25 73 22 2c 0a 20 20 20 20 20   '%q'.%s",.     
2a14d 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e     db->aDb[iDb].
2a14e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 4e 61  zName, zMasterNa
2a14f 6d 65 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73  me);.    (void)s
2a150 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
2a151 64 62 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  db);.#ifndef SQL
2a152 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
2a153 5a 41 54 49 4f 4e 0a 20 20 20 20 7b 0a 20 20 20  ZATION.    {.   
2a154 20 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28     int (*xAuth)(
2a155 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
2a156 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
2a157 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
2a158 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20  nst char*);.    
2a159 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 41    xAuth = db->xA
2a15a 75 74 68 3b 0a 20 20 20 20 20 20 64 62 2d 3e 78  uth;.      db->x
2a15b 41 75 74 68 20 3d 20 30 3b 0a 23 65 6e 64 69 66  Auth = 0;.#endif
2a15c 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
2a15d 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71  te3_exec(db, zSq
2a15e 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61  l, sqlite3InitCa
2a15f 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74 44 61 74  llback, &initDat
2a160 61 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66 20 53  a, 0);.#ifndef S
2a161 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
2a162 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20 20 64  RIZATION.      d
2a163 62 2d 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68  b->xAuth = xAuth
2a164 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
2a165 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
2a166 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69 6e 69 74  E_OK ) rc = init
2a167 44 61 74 61 2e 72 63 3b 0a 20 20 20 20 28 76 6f  Data.rc;.    (vo
2a168 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
2a169 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69  On(db);.    sqli
2a16a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
2a16b 71 6c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ql);.#ifndef SQL
2a16c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45  ITE_OMIT_ANALYZE
2a16d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
2a16e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
2a16f 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69 73 4c  sqlite3AnalysisL
2a170 6f 61 64 28 64 62 2c 20 69 44 62 29 3b 0a 20 20  oad(db, iDb);.  
2a171 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
2a172 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2a173 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 63 20  ailed ){.    rc 
2a174 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
2a175 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
2a176 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
2a177 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  b, 0);.  }.  if(
2a178 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
2a179 7c 20 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  | (db->flags&SQL
2a17a 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65  ITE_RecoveryMode
2a17b 29 29 7b 0a 20 20 20 20 2f 2a 20 42 6c 61 63 6b  )){.    /* Black
2a17c 20 6d 61 67 69 63 3a 20 49 66 20 74 68 65 20 53   magic: If the S
2a17d 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f  QLITE_RecoveryMo
2a17e 64 65 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20  de flag is set, 
2a17f 74 68 65 6e 20 63 6f 6e 73 69 64 65 72 0a 20 20  then consider.  
2a180 20 20 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20    ** the schema 
2a181 6c 6f 61 64 65 64 2c 20 65 76 65 6e 20 69 66 20  loaded, even if 
2a182 65 72 72 6f 72 73 20 6f 63 63 75 72 72 65 64 2e  errors occurred.
2a183 20 49 6e 20 74 68 69 73 20 73 69 74 75 61 74 69   In this situati
2a184 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 63  on the .    ** c
2a185 75 72 72 65 6e 74 20 73 71 6c 69 74 65 33 5f 70  urrent sqlite3_p
2a186 72 65 70 61 72 65 28 29 20 6f 70 65 72 61 74 69  repare() operati
2a187 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 2c 20 62 75  on will fail, bu
2a188 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  t the following 
2a189 6f 6e 65 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20  one.    ** will 
2a18a 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d 70 69  attempt to compi
2a18b 6c 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  le the supplied 
2a18c 73 74 61 74 65 6d 65 6e 74 20 61 67 61 69 6e 73  statement agains
2a18d 74 20 77 68 61 74 65 76 65 72 20 73 75 62 73 65  t whatever subse
2a18e 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  t.    ** of the 
2a18f 73 63 68 65 6d 61 20 77 61 73 20 6c 6f 61 64 65  schema was loade
2a190 64 20 62 65 66 6f 72 65 20 74 68 65 20 65 72 72  d before the err
2a191 6f 72 20 6f 63 63 75 72 72 65 64 2e 20 54 68 65  or occurred. The
2a192 20 70 72 69 6d 61 72 79 0a 20 20 20 20 2a 2a 20   primary.    ** 
2a193 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20  purpose of this 
2a194 69 73 20 74 6f 20 61 6c 6c 6f 77 20 61 63 63 65  is to allow acce
2a195 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  ss to the sqlite
2a196 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20  _master table.  
2a197 20 20 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20 69    ** even when i
2a198 74 73 20 63 6f 6e 74 65 6e 74 73 20 68 61 76 65  ts contents have
2a199 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64 2e   been corrupted.
2a19a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 44 62 53 65  .    */.    DbSe
2a19b 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44  tProperty(db, iD
2a19c 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  b, DB_SchemaLoad
2a19d 65 64 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  ed);.    rc = SQ
2a19e 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
2a19f 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 66 6f 72  /* Jump here for
2a1a0 20 61 6e 20 65 72 72 6f 72 20 74 68 61 74 20 6f   an error that o
2a1a1 63 63 75 72 73 20 61 66 74 65 72 20 73 75 63 63  ccurs after succ
2a1a2 65 73 73 66 75 6c 6c 79 20 61 6c 6c 6f 63 61 74  essfully allocat
2a1a3 69 6e 67 0a 20 20 2a 2a 20 63 75 72 4d 61 69 6e  ing.  ** curMain
2a1a4 20 61 6e 64 20 63 61 6c 6c 69 6e 67 20 73 71 6c   and calling sql
2a1a5 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 29  ite3BtreeEnter()
2a1a6 2e 20 46 6f 72 20 61 6e 20 65 72 72 6f 72 20 74  . For an error t
2a1a7 68 61 74 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20  hat occurs.  ** 
2a1a8 62 65 66 6f 72 65 20 74 68 61 74 20 70 6f 69 6e  before that poin
2a1a9 74 2c 20 6a 75 6d 70 20 74 6f 20 65 72 72 6f 72  t, jump to error
2a1aa 5f 6f 75 74 2e 0a 20 20 2a 2f 0a 69 6e 69 74 6f  _out..  */.inito
2a1ab 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20  ne_error_out:.  
2a1ac 69 66 28 20 6f 70 65 6e 65 64 54 72 61 6e 73 61  if( openedTransa
2a1ad 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 73 71 6c  ction ){.    sql
2a1ae 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28  ite3BtreeCommit(
2a1af 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 7d 0a 20  pDb->pBt);.  }. 
2a1b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
2a1b1 76 65 28 70 44 62 2d 3e 70 42 74 29 3b 0a 0a 65  ve(pDb->pBt);..e
2a1b2 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20  rror_out:.  if( 
2a1b3 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
2a1b4 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49   || rc==SQLITE_I
2a1b5 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  OERR_NOMEM ){.  
2a1b6 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2a1b7 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65  ed = 1;.  }.  re
2a1b8 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2a1b9 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6c 6c  * Initialize all
2a1ba 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20   database files 
2a1bb 2d 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  - the main datab
2a1bc 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 66 69  ase file, the fi
2a1bd 6c 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74  le.** used to st
2a1be 6f 72 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ore temporary ta
2a1bf 62 6c 65 73 2c 20 61 6e 64 20 61 6e 79 20 61 64  bles, and any ad
2a1c0 64 69 74 69 6f 6e 61 6c 20 64 61 74 61 62 61 73  ditional databas
2a1c1 65 20 66 69 6c 65 73 0a 2a 2a 20 63 72 65 61 74  e files.** creat
2a1c2 65 64 20 75 73 69 6e 67 20 41 54 54 41 43 48 20  ed using ATTACH 
2a1c3 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 52 65 74  statements.  Ret
2a1c4 75 72 6e 20 61 20 73 75 63 63 65 73 73 20 63 6f  urn a success co
2a1c5 64 65 2e 20 20 49 66 20 61 6e 0a 2a 2a 20 65 72  de.  If an.** er
2a1c6 72 6f 72 20 6f 63 63 75 72 73 2c 20 77 72 69 74  ror occurs, writ
2a1c7 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
2a1c8 67 65 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73  ge into *pzErrMs
2a1c9 67 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61  g..**.** After a
2a1ca 20 64 61 74 61 62 61 73 65 20 69 73 20 69 6e 69   database is ini
2a1cb 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 20 44 42  tialized, the DB
2a1cc 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 62 69  _SchemaLoaded bi
2a1cd 74 20 69 73 20 73 65 74 0a 2a 2a 20 62 69 74 20  t is set.** bit 
2a1ce 69 73 20 73 65 74 20 69 6e 20 74 68 65 20 66 6c  is set in the fl
2a1cf 61 67 73 20 66 69 65 6c 64 20 6f 66 20 74 68 65  ags field of the
2a1d0 20 44 62 20 73 74 72 75 63 74 75 72 65 2e 20 49   Db structure. I
2a1d1 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  f the database.*
2a1d2 2a 20 66 69 6c 65 20 77 61 73 20 6f 66 20 7a 65  * file was of ze
2a1d3 72 6f 2d 6c 65 6e 67 74 68 2c 20 74 68 65 6e 20  ro-length, then 
2a1d4 74 68 65 20 44 42 5f 45 6d 70 74 79 20 66 6c 61  the DB_Empty fla
2a1d5 67 20 69 73 20 61 6c 73 6f 20 73 65 74 2e 0a 2a  g is also set..*
2a1d6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2a1d7 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74   int sqlite3Init
2a1d8 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68  (sqlite3 *db, ch
2a1d9 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 29 7b 0a  ar **pzErrMsg){.
2a1da 20 20 69 6e 74 20 69 2c 20 72 63 3b 0a 20 20 69    int i, rc;.  i
2a1db 6e 74 20 63 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e  nt commit_intern
2a1dc 61 6c 20 3d 20 21 28 64 62 2d 3e 66 6c 61 67 73  al = !(db->flags
2a1dd 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  &SQLITE_InternCh
2a1de 61 6e 67 65 73 29 3b 0a 20 20 0a 20 20 61 73 73  anges);.  .  ass
2a1df 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
2a1e0 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
2a1e1 78 29 20 29 3b 0a 20 20 72 63 20 3d 20 53 51 4c  x) );.  rc = SQL
2a1e2 49 54 45 5f 4f 4b 3b 0a 20 20 64 62 2d 3e 69 6e  ITE_OK;.  db->in
2a1e3 69 74 2e 62 75 73 79 20 3d 20 31 3b 0a 20 20 66  it.busy = 1;.  f
2a1e4 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49  or(i=0; rc==SQLI
2a1e5 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e  TE_OK && i<db->n
2a1e6 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  Db; i++){.    if
2a1e7 28 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 28  ( DbHasProperty(
2a1e8 64 62 2c 20 69 2c 20 44 42 5f 53 63 68 65 6d 61  db, i, DB_Schema
2a1e9 4c 6f 61 64 65 64 29 20 7c 7c 20 69 3d 3d 31 20  Loaded) || i==1 
2a1ea 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2a1eb 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74  rc = sqlite3Init
2a1ec 4f 6e 65 28 64 62 2c 20 69 2c 20 70 7a 45 72 72  One(db, i, pzErr
2a1ed 4d 73 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63  Msg);.    if( rc
2a1ee 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2a1ef 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
2a1f0 68 65 6d 61 28 64 62 2c 20 69 29 3b 0a 20 20 20  hema(db, i);.   
2a1f1 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 6e 63   }.  }..  /* Onc
2a1f2 65 20 61 6c 6c 20 74 68 65 20 6f 74 68 65 72 20  e all the other 
2a1f3 64 61 74 61 62 61 73 65 73 20 68 61 76 65 20 62  databases have b
2a1f4 65 65 6e 20 69 6e 69 74 69 61 6c 69 73 65 64 2c  een initialised,
2a1f5 20 6c 6f 61 64 20 74 68 65 20 73 63 68 65 6d 61   load the schema
2a1f6 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 54 45  .  ** for the TE
2a1f7 4d 50 20 64 61 74 61 62 61 73 65 2e 20 54 68 69  MP database. Thi
2a1f8 73 20 69 73 20 6c 6f 61 64 65 64 20 6c 61 73 74  s is loaded last
2a1f9 2c 20 61 73 20 74 68 65 20 54 45 4d 50 20 64 61  , as the TEMP da
2a1fa 74 61 62 61 73 65 0a 20 20 2a 2a 20 73 63 68 65  tabase.  ** sche
2a1fb 6d 61 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 72  ma may contain r
2a1fc 65 66 65 72 65 6e 63 65 73 20 74 6f 20 6f 62 6a  eferences to obj
2a1fd 65 63 74 73 20 69 6e 20 6f 74 68 65 72 20 64 61  ects in other da
2a1fe 74 61 62 61 73 65 73 2e 0a 20 20 2a 2f 0a 23 69  tabases..  */.#i
2a1ff 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2a200 54 5f 54 45 4d 50 44 42 0a 20 20 69 66 28 20 72  T_TEMPDB.  if( r
2a201 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
2a202 41 4c 57 41 59 53 28 64 62 2d 3e 6e 44 62 3e 31  ALWAYS(db->nDb>1
2a203 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
2a204 20 20 20 20 20 20 26 26 20 21 44 62 48 61 73 50        && !DbHasP
2a205 72 6f 70 65 72 74 79 28 64 62 2c 20 31 2c 20 44  roperty(db, 1, D
2a206 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20  B_SchemaLoaded) 
2a207 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
2a208 74 65 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20 31  te3InitOne(db, 1
2a209 2c 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  , pzErrMsg);.   
2a20a 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
2a20b 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
2a20c 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
2a20d 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  1);.    }.  }.#e
2a20e 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 69 6e 69 74  ndif..  db->init
2a20f 2e 62 75 73 79 20 3d 20 30 3b 0a 20 20 69 66 28  .busy = 0;.  if(
2a210 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
2a211 26 20 63 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61  & commit_interna
2a212 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  l ){.    sqlite3
2a213 43 6f 6d 6d 69 74 49 6e 74 65 72 6e 61 6c 43 68  CommitInternalCh
2a214 61 6e 67 65 73 28 64 62 29 3b 0a 20 20 7d 0a 0a  anges(db);.  }..
2a215 20 20 72 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a    return rc; .}.
2a216 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2a217 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  ine is a no-op i
2a218 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  f the database s
2a219 63 68 65 6d 61 20 69 73 20 61 6c 72 65 61 64 79  chema is already
2a21a 20 69 6e 69 74 69 61 6c 69 73 65 64 2e 0a 2a 2a   initialised..**
2a21b 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
2a21c 73 63 68 65 6d 61 20 69 73 20 6c 6f 61 64 65 64  schema is loaded
2a21d 2e 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  . An error code 
2a21e 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
2a21f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2a220 6e 74 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  nt sqlite3ReadSc
2a221 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50 61 72  hema(Parse *pPar
2a222 73 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  se){.  int rc = 
2a223 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c  SQLITE_OK;.  sql
2a224 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2a225 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  e->db;.  assert(
2a226 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
2a227 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
2a228 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69  ;.  if( !db->ini
2a229 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 72 63  t.busy ){.    rc
2a22a 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64   = sqlite3Init(d
2a22b 62 2c 20 26 70 50 61 72 73 65 2d 3e 7a 45 72 72  b, &pParse->zErr
2a22c 4d 73 67 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Msg);.  }.  if( 
2a22d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
2a22e 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
2a22f 3d 20 72 63 3b 0a 20 20 20 20 70 50 61 72 73 65  = rc;.    pParse
2a230 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20  ->nErr++;.  }.  
2a231 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
2a232 2a 0a 2a 2a 20 43 68 65 63 6b 20 73 63 68 65 6d  *.** Check schem
2a233 61 20 63 6f 6f 6b 69 65 73 20 69 6e 20 61 6c 6c  a cookies in all
2a234 20 64 61 74 61 62 61 73 65 73 2e 20 20 49 66 20   databases.  If 
2a235 61 6e 79 20 63 6f 6f 6b 69 65 20 69 73 20 6f 75  any cookie is ou
2a236 74 0a 2a 2a 20 6f 66 20 64 61 74 65 20 73 65 74  t.** of date set
2a237 20 70 50 61 72 73 65 2d 3e 72 63 20 74 6f 20 53   pParse->rc to S
2a238 51 4c 49 54 45 5f 53 43 48 45 4d 41 2e 20 20 49  QLITE_SCHEMA.  I
2a239 66 20 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f 6f  f all schema coo
2a23a 6b 69 65 73 0a 2a 2a 20 6d 61 6b 65 20 6e 6f 20  kies.** make no 
2a23b 63 68 61 6e 67 65 73 20 74 6f 20 70 50 61 72 73  changes to pPars
2a23c 65 2d 3e 72 63 2e 0a 2a 2f 0a 73 74 61 74 69 63  e->rc..*/.static
2a23d 20 76 6f 69 64 20 73 63 68 65 6d 61 49 73 56 61   void schemaIsVa
2a23e 6c 69 64 28 50 61 72 73 65 20 2a 70 50 61 72 73  lid(Parse *pPars
2a23f 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e){.  sqlite3 *d
2a240 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2a241 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 69 6e 74    int iDb;.  int
2a242 20 72 63 3b 0a 20 20 69 6e 74 20 63 6f 6f 6b 69   rc;.  int cooki
2a243 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  e;..  assert( pP
2a244 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d  arse->checkSchem
2a245 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  a );.  assert( s
2a246 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
2a247 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  d(db->mutex) );.
2a248 20 20 66 6f 72 28 69 44 62 3d 30 3b 20 69 44 62    for(iDb=0; iDb
2a249 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29  <db->nDb; iDb++)
2a24a 7b 0a 20 20 20 20 69 6e 74 20 6f 70 65 6e 65 64  {.    int opened
2a24b 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 30 3b  Transaction = 0;
2a24c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2a24d 20 69 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f   if a transactio
2a24e 6e 20 69 73 20 6f 70 65 6e 65 64 20 2a 2f 0a 20  n is opened */. 
2a24f 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
2a250 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
2a251 3b 20 20 20 20 20 2f 2a 20 42 74 72 65 65 20 64  ;     /* Btree d
2a252 61 74 61 62 61 73 65 20 74 6f 20 72 65 61 64 20  atabase to read 
2a253 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 2a 2f 0a 20  cookie from */. 
2a254 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 20     if( pBt==0 ) 
2a255 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 2f  continue;..    /
2a256 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
2a257 74 20 61 6c 72 65 61 64 79 20 61 20 72 65 61 64  t already a read
2a258 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61 64 2d 77  -only (or read-w
2a259 72 69 74 65 29 20 74 72 61 6e 73 61 63 74 69 6f  rite) transactio
2a25a 6e 20 6f 70 65 6e 65 64 0a 20 20 20 20 2a 2a 20  n opened.    ** 
2a25b 6f 6e 20 74 68 65 20 62 2d 74 72 65 65 20 64 61  on the b-tree da
2a25c 74 61 62 61 73 65 2c 20 6f 70 65 6e 20 6f 6e 65  tabase, open one
2a25d 20 6e 6f 77 2e 20 49 66 20 61 20 74 72 61 6e 73   now. If a trans
2a25e 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64  action is opened
2a25f 2c 20 69 74 20 0a 20 20 20 20 2a 2a 20 77 69 6c  , it .    ** wil
2a260 6c 20 62 65 20 63 6c 6f 73 65 64 20 69 6d 6d 65  l be closed imme
2a261 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 72 65  diately after re
2a262 61 64 69 6e 67 20 74 68 65 20 6d 65 74 61 2d 76  ading the meta-v
2a263 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28  alue. */.    if(
2a264 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 49 73   !sqlite3BtreeIs
2a265 49 6e 52 65 61 64 54 72 61 6e 73 28 70 42 74 29  InReadTrans(pBt)
2a266 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
2a267 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69 6e  qlite3BtreeBegin
2a268 54 72 61 6e 73 28 70 42 74 2c 20 30 29 3b 0a 20  Trans(pBt, 0);. 
2a269 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
2a26a 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d  ITE_NOMEM || rc=
2a26b 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f  =SQLITE_IOERR_NO
2a26c 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 64  MEM ){.        d
2a26d 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2a26e 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
2a26f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2a270 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 3b 0a 20  E_OK ) return;. 
2a271 20 20 20 20 20 6f 70 65 6e 65 64 54 72 61 6e 73       openedTrans
2a272 61 63 74 69 6f 6e 20 3d 20 31 3b 0a 20 20 20 20  action = 1;.    
2a273 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74  }..    /* Read t
2a274 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
2a275 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
2a276 73 65 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e  se. If it does n
2a277 6f 74 20 6d 61 74 63 68 20 74 68 65 20 0a 20 20  ot match the .  
2a278 20 20 2a 2a 20 76 61 6c 75 65 20 73 74 6f 72 65    ** value store
2a279 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65  d as part of the
2a27a 20 69 6e 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72   in the in-memor
2a27b 79 20 73 63 68 65 6d 61 20 72 65 70 72 65 73 65  y schema represe
2a27c 6e 74 61 74 69 6f 6e 2c 0a 20 20 20 20 2a 2a 20  ntation,.    ** 
2a27d 73 65 74 20 50 61 72 73 65 2e 72 63 20 74 6f 20  set Parse.rc to 
2a27e 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 2e 20 2a  SQLITE_SCHEMA. *
2a27f 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  /.    sqlite3Btr
2a280 65 65 47 65 74 4d 65 74 61 28 70 42 74 2c 20 42  eeGetMeta(pBt, B
2a281 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53  TREE_SCHEMA_VERS
2a282 49 4f 4e 2c 20 28 75 33 32 20 2a 29 26 63 6f 6f  ION, (u32 *)&coo
2a283 6b 69 65 29 3b 0a 20 20 20 20 69 66 28 20 63 6f  kie);.    if( co
2a284 6f 6b 69 65 21 3d 64 62 2d 3e 61 44 62 5b 69 44  okie!=db->aDb[iD
2a285 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65  b].pSchema->sche
2a286 6d 61 5f 63 6f 6f 6b 69 65 20 29 7b 0a 20 20 20  ma_cookie ){.   
2a287 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20     pParse->rc = 
2a288 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20  SQLITE_SCHEMA;. 
2a289 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f     }..    /* Clo
2a28a 73 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  se the transacti
2a28b 6f 6e 2c 20 69 66 20 6f 6e 65 20 77 61 73 20 6f  on, if one was o
2a28c 70 65 6e 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66  pened. */.    if
2a28d 28 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63 74  ( openedTransact
2a28e 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ion ){.      sql
2a28f 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28  ite3BtreeCommit(
2a290 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  pBt);.    }.  }.
2a291 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
2a292 20 61 20 73 63 68 65 6d 61 20 70 6f 69 6e 74 65   a schema pointe
2a293 72 20 69 6e 74 6f 20 74 68 65 20 69 44 62 20 69  r into the iDb i
2a294 6e 64 65 78 20 74 68 61 74 20 69 6e 64 69 63 61  ndex that indica
2a295 74 65 73 0a 2a 2a 20 77 68 69 63 68 20 64 61 74  tes.** which dat
2a296 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 64 62  abase file in db
2a297 2d 3e 61 44 62 5b 5d 20 74 68 65 20 73 63 68 65  ->aDb[] the sche
2a298 6d 61 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a  ma refers to..**
2a299 0a 2a 2a 20 49 66 20 74 68 65 20 73 61 6d 65 20  .** If the same 
2a29a 64 61 74 61 62 61 73 65 20 69 73 20 61 74 74 61  database is atta
2a29b 63 68 65 64 20 6d 6f 72 65 20 74 68 61 6e 20 6f  ched more than o
2a29c 6e 63 65 2c 20 74 68 65 20 66 69 72 73 74 0a 2a  nce, the first.*
2a29d 2a 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  * attached datab
2a29e 61 73 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ase is returned.
2a29f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2a2a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63  TE int sqlite3Sc
2a2a1 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69  hemaToIndex(sqli
2a2a2 74 65 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 20  te3 *db, Schema 
2a2a3 2a 70 53 63 68 65 6d 61 29 7b 0a 20 20 69 6e 74  *pSchema){.  int
2a2a4 20 69 20 3d 20 2d 31 30 30 30 30 30 30 3b 0a 0a   i = -1000000;..
2a2a5 20 20 2f 2a 20 49 66 20 70 53 63 68 65 6d 61 20    /* If pSchema 
2a2a6 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65  is NULL, then re
2a2a7 74 75 72 6e 20 2d 31 30 30 30 30 30 30 2e 20 54  turn -1000000. T
2a2a8 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e  his happens when
2a2a9 20 63 6f 64 65 20 69 6e 20 0a 20 20 2a 2a 20 65   code in .  ** e
2a2aa 78 70 72 2e 63 20 69 73 20 74 72 79 69 6e 67 20  xpr.c is trying 
2a2ab 74 6f 20 72 65 73 6f 6c 76 65 20 61 20 72 65 66  to resolve a ref
2a2ac 65 72 65 6e 63 65 20 74 6f 20 61 20 74 72 61 6e  erence to a tran
2a2ad 73 69 65 6e 74 20 74 61 62 6c 65 20 28 69 2e 65  sient table (i.e
2a2ae 2e 20 6f 6e 65 0a 20 20 2a 2a 20 63 72 65 61 74  . one.  ** creat
2a2af 65 64 20 62 79 20 61 20 73 75 62 2d 73 65 6c 65  ed by a sub-sele
2a2b0 63 74 29 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ct). In this cas
2a2b1 65 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c  e the return val
2a2b2 75 65 20 6f 66 20 74 68 69 73 20 0a 20 20 2a 2a  ue of this .  **
2a2b3 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64   function should
2a2b4 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 0a   never be used..
2a2b5 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 72 65 74    **.  ** We ret
2a2b6 75 72 6e 20 2d 31 30 30 30 30 30 30 20 69 6e 73  urn -1000000 ins
2a2b7 74 65 61 64 20 6f 66 20 74 68 65 20 6d 6f 72 65  tead of the more
2a2b8 20 75 73 75 61 6c 20 2d 31 20 73 69 6d 70 6c 79   usual -1 simply
2a2b9 20 62 65 63 61 75 73 65 20 75 73 69 6e 67 0a 20   because using. 
2a2ba 20 2a 2a 20 2d 31 30 30 30 30 30 30 20 61 73 20   ** -1000000 as 
2a2bb 74 68 65 20 69 6e 63 6f 72 72 65 63 74 20 69 6e  the incorrect in
2a2bc 64 65 78 20 69 6e 74 6f 20 64 62 2d 3e 61 44 62  dex into db->aDb
2a2bd 5b 5d 20 69 73 20 6d 75 63 68 20 0a 20 20 2a 2a  [] is much .  **
2a2be 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20   more likely to 
2a2bf 63 61 75 73 65 20 61 20 73 65 67 66 61 75 6c 74  cause a segfault
2a2c0 20 74 68 61 6e 20 2d 31 20 28 6f 66 20 63 6f 75   than -1 (of cou
2a2c1 72 73 65 20 74 68 65 72 65 20 61 72 65 20 61 73  rse there are as
2a2c2 73 65 72 74 28 29 0a 20 20 2a 2a 20 73 74 61 74  sert().  ** stat
2a2c3 65 6d 65 6e 74 73 20 74 6f 6f 2c 20 62 75 74 20  ements too, but 
2a2c4 69 74 20 6e 65 76 65 72 20 68 75 72 74 73 20 74  it never hurts t
2a2c5 6f 20 70 6c 61 79 20 74 68 65 20 6f 64 64 73 29  o play the odds)
2a2c6 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
2a2c7 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
2a2c8 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
2a2c9 3b 0a 20 20 69 66 28 20 70 53 63 68 65 6d 61 20  ;.  if( pSchema 
2a2ca 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
2a2cb 41 4c 57 41 59 53 28 69 3c 64 62 2d 3e 6e 44 62  ALWAYS(i<db->nDb
2a2cc 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  ); i++){.      i
2a2cd 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53  f( db->aDb[i].pS
2a2ce 63 68 65 6d 61 3d 3d 70 53 63 68 65 6d 61 20 29  chema==pSchema )
2a2cf 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
2a2d0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2a2d1 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20     assert( i>=0 
2a2d2 26 26 20 69 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a  && i<db->nDb );.
2a2d3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a    }.  return i;.
2a2d4 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c 65  }../*.** Compile
2a2d5 20 74 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64   the UTF-8 encod
2a2d6 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
2a2d7 20 7a 53 71 6c 20 69 6e 74 6f 20 61 20 73 74 61   zSql into a sta
2a2d8 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 2a  tement handle..*
2a2d9 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
2a2da 69 74 65 33 50 72 65 70 61 72 65 28 0a 20 20 73  ite3Prepare(.  s
2a2db 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2a2dc 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2a2dd 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a  base handle. */.
2a2de 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
2a2df 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55  ql,         /* U
2a2e0 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c  TF-8 encoded SQL
2a2e1 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20   statement. */. 
2a2e2 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20   int nBytes,    
2a2e3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
2a2e4 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20  ngth of zSql in 
2a2e5 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20  bytes. */.  int 
2a2e6 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 20 20 20  saveSqlFlag,    
2a2e7 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
2a2e8 20 63 6f 70 79 20 53 51 4c 20 74 65 78 74 20 69   copy SQL text i
2a2e9 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f  nto the sqlite3_
2a2ea 73 74 6d 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a  stmt */.  Vdbe *
2a2eb 70 52 65 70 72 65 70 61 72 65 2c 20 20 20 20 20  pReprepare,     
2a2ec 20 20 20 20 2f 2a 20 56 4d 20 62 65 69 6e 67 20      /* VM being 
2a2ed 72 65 70 72 65 70 61 72 65 64 20 2a 2f 0a 20 20  reprepared */.  
2a2ee 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70  sqlite3_stmt **p
2a2ef 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54  pStmt,    /* OUT
2a2f0 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  : A pointer to t
2a2f1 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
2a2f2 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ement */.  const
2a2f3 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20   char **pzTail  
2a2f4 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64       /* OUT: End
2a2f5 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e   of parsed strin
2a2f6 67 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20  g */.){.  Parse 
2a2f7 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20  *pParse;        
2a2f8 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2a2f9 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 68 61 72  ontext */.  char
2a2fa 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30 3b 20 20   *zErrMsg = 0;  
2a2fb 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d        /* Error m
2a2fc 65 73 73 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  essage */.  int 
2a2fd 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
2a2fe 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
2a2ff 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  code */.  int i;
2a300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a301 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2a302 74 65 72 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c  ter */..  /* All
2a303 6f 63 61 74 65 20 74 68 65 20 70 61 72 73 69 6e  ocate the parsin
2a304 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 70  g context */.  p
2a305 50 61 72 73 65 20 3d 20 73 71 6c 69 74 65 33 53  Parse = sqlite3S
2a306 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 64 62  tackAllocZero(db
2a307 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72 73 65  , sizeof(*pParse
2a308 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  ));.  if( pParse
2a309 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
2a30a 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2a30b 20 20 67 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61    goto end_prepa
2a30c 72 65 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65  re;.  }.  pParse
2a30d 2d 3e 70 52 65 70 72 65 70 61 72 65 20 3d 20 70  ->pReprepare = p
2a30e 52 65 70 72 65 70 61 72 65 3b 0a 0a 20 20 69 66  Reprepare;..  if
2a30f 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
2a310 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 72 63 20  n(db) ){.    rc 
2a311 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  = SQLITE_MISUSE;
2a312 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70 72  .    goto end_pr
2a313 65 70 61 72 65 3b 0a 20 20 7d 0a 20 20 61 73 73  epare;.  }.  ass
2a314 65 72 74 28 20 70 70 53 74 6d 74 20 26 26 20 2a  ert( ppStmt && *
2a315 70 70 53 74 6d 74 3d 3d 30 20 29 3b 0a 20 20 61  ppStmt==0 );.  a
2a316 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
2a317 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 73  ocFailed );.  as
2a318 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
2a319 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
2a31a 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65  ex) );..  /* Che
2a31b 63 6b 20 74 6f 20 76 65 72 69 66 79 20 74 68 61  ck to verify tha
2a31c 74 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  t it is possible
2a31d 20 74 6f 20 67 65 74 20 61 20 72 65 61 64 20 6c   to get a read l
2a31e 6f 63 6b 20 6f 6e 20 61 6c 6c 0a 20 20 2a 2a 20  ock on all.  ** 
2a31f 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 73  database schemas
2a320 2e 20 20 54 68 65 20 69 6e 61 62 69 6c 69 74 79  .  The inability
2a321 20 74 6f 20 67 65 74 20 61 20 72 65 61 64 20 6c   to get a read l
2a322 6f 63 6b 20 69 6e 64 69 63 61 74 65 73 20 74 68  ock indicates th
2a323 61 74 0a 20 20 2a 2a 20 73 6f 6d 65 20 6f 74 68  at.  ** some oth
2a324 65 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  er database conn
2a325 65 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e  ection is holdin
2a326 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 2c 20  g a write-lock, 
2a327 77 68 69 63 68 20 69 6e 0a 20 20 2a 2a 20 74 75  which in.  ** tu
2a328 72 6e 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  rn means that th
2a329 65 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  e other connecti
2a32a 6f 6e 20 68 61 73 20 6d 61 64 65 20 75 6e 63 6f  on has made unco
2a32b 6d 6d 69 74 74 65 64 20 63 68 61 6e 67 65 73 0a  mmitted changes.
2a32c 20 20 2a 2a 20 74 6f 20 74 68 65 20 73 63 68 65    ** to the sche
2a32d 6d 61 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65  ma..  **.  ** We
2a32e 72 65 20 77 65 20 74 6f 20 70 72 6f 63 65 65 64  re we to proceed
2a32f 20 61 6e 64 20 70 72 65 70 61 72 65 20 74 68 65   and prepare the
2a330 20 73 74 61 74 65 6d 65 6e 74 20 61 67 61 69 6e   statement again
2a331 73 74 20 74 68 65 20 75 6e 63 6f 6d 6d 69 74 74  st the uncommitt
2a332 65 64 0a 20 20 2a 2a 20 73 63 68 65 6d 61 20 63  ed.  ** schema c
2a333 68 61 6e 67 65 73 20 61 6e 64 20 69 66 20 74 68  hanges and if th
2a334 6f 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67  ose schema chang
2a335 65 73 20 61 72 65 20 73 75 62 73 65 71 75 65 6e  es are subsequen
2a336 74 6c 79 20 72 6f 6c 6c 65 64 0a 20 20 2a 2a 20  tly rolled.  ** 
2a337 62 61 63 6b 20 61 6e 64 20 64 69 66 66 65 72 65  back and differe
2a338 6e 74 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d  nt changes are m
2a339 61 64 65 20 69 6e 20 74 68 65 69 72 20 70 6c 61  ade in their pla
2a33a 63 65 2c 20 74 68 65 6e 20 77 68 65 6e 20 74 68  ce, then when th
2a33b 69 73 0a 20 20 2a 2a 20 70 72 65 70 61 72 65 64  is.  ** prepared
2a33c 20 73 74 61 74 65 6d 65 6e 74 20 67 6f 65 73 20   statement goes 
2a33d 74 6f 20 72 75 6e 20 74 68 65 20 73 63 68 65 6d  to run the schem
2a33e 61 20 63 6f 6f 6b 69 65 20 77 6f 75 6c 64 20 66  a cookie would f
2a33f 61 69 6c 20 74 6f 20 64 65 74 65 63 74 0a 20 20  ail to detect.  
2a340 2a 2a 20 74 68 65 20 73 63 68 65 6d 61 20 63 68  ** the schema ch
2a341 61 6e 67 65 2e 20 20 44 69 73 61 73 74 65 72 20  ange.  Disaster 
2a342 77 6f 75 6c 64 20 66 6f 6c 6c 6f 77 2e 0a 20 20  would follow..  
2a343 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 74 68 72  **.  ** This thr
2a344 65 61 64 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ead is currently
2a345 20 68 6f 6c 64 69 6e 67 20 6d 75 74 65 78 65 73   holding mutexes
2a346 20 6f 6e 20 61 6c 6c 20 42 74 72 65 65 73 20 28   on all Btrees (
2a347 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 6f 66 20  because.  ** of 
2a348 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  the sqlite3Btree
2a349 45 6e 74 65 72 41 6c 6c 28 29 20 69 6e 20 73 71  EnterAll() in sq
2a34a 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70  lite3LockAndPrep
2a34b 61 72 65 28 29 29 20 73 6f 20 69 74 0a 20 20 2a  are()) so it.  *
2a34c 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  * is not possibl
2a34d 65 20 66 6f 72 20 61 6e 6f 74 68 65 72 20 74 68  e for another th
2a34e 72 65 61 64 20 74 6f 20 73 74 61 72 74 20 61 20  read to start a 
2a34f 6e 65 77 20 73 63 68 65 6d 61 20 63 68 61 6e 67  new schema chang
2a350 65 0a 20 20 2a 2a 20 77 68 69 6c 65 20 74 68 69  e.  ** while thi
2a351 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72 75 6e  s routine is run
2a352 6e 69 6e 67 2e 20 20 48 65 6e 63 65 2c 20 77 65  ning.  Hence, we
2a353 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   do not need to 
2a354 68 6f 6c 64 20 0a 20 20 2a 2a 20 6c 6f 63 6b 73  hold .  ** locks
2a355 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 2c 20   on the schema, 
2a356 77 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f 20  we just need to 
2a357 6d 61 6b 65 20 73 75 72 65 20 6e 6f 62 6f 64 79  make sure nobody
2a358 20 65 6c 73 65 20 69 73 20 0a 20 20 2a 2a 20 68   else is .  ** h
2a359 6f 6c 64 69 6e 67 20 74 68 65 6d 2e 0a 20 20 2a  olding them..  *
2a35a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74  *.  ** Note that
2a35b 20 73 65 74 74 69 6e 67 20 52 45 41 44 5f 55 4e   setting READ_UN
2a35c 43 4f 4d 4d 49 54 54 45 44 20 6f 76 65 72 72 69  COMMITTED overri
2a35d 64 65 73 20 6d 6f 73 74 20 6c 6f 63 6b 20 64 65  des most lock de
2a35e 74 65 63 74 69 6f 6e 2c 0a 20 20 2a 2a 20 62 75  tection,.  ** bu
2a35f 74 20 69 74 20 64 6f 65 73 20 2a 6e 6f 74 2a 20  t it does *not* 
2a360 6f 76 65 72 72 69 64 65 20 73 63 68 65 6d 61 20  override schema 
2a361 6c 6f 63 6b 20 64 65 74 65 63 74 69 6f 6e 2c 20  lock detection, 
2a362 73 6f 20 74 68 69 73 20 61 6c 6c 20 73 74 69 6c  so this all stil
2a363 6c 0a 20 20 2a 2a 20 77 6f 72 6b 73 20 65 76 65  l.  ** works eve
2a364 6e 20 69 66 20 52 45 41 44 5f 55 4e 43 4f 4d 4d  n if READ_UNCOMM
2a365 49 54 54 45 44 20 69 73 20 73 65 74 2e 0a 20 20  ITTED is set..  
2a366 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
2a367 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 20 7b 0a  db->nDb; i++) {.
2a368 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
2a369 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
2a36a 0a 20 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a  .    if( pBt ){.
2a36b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71        assert( sq
2a36c 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
2a36d 75 74 65 78 28 70 42 74 29 20 29 3b 0a 20 20 20  utex(pBt) );.   
2a36e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
2a36f 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64  treeSchemaLocked
2a370 28 70 42 74 29 3b 0a 20 20 20 20 20 20 69 66 28  (pBt);.      if(
2a371 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63   rc ){.        c
2a372 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
2a373 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d   db->aDb[i].zNam
2a374 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  e;.        sqlit
2a375 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
2a376 22 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61  "database schema
2a377 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c   is locked: %s",
2a378 20 7a 44 62 29 3b 0a 20 20 20 20 20 20 20 20 28   zDb);.        (
2a379 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
2a37a 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20  tyOff(db);.     
2a37b 20 20 20 74 65 73 74 63 61 73 65 28 20 64 62 2d     testcase( db-
2a37c 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
2a37d 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20  ReadUncommitted 
2a37e 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  );.        goto 
2a37f 65 6e 64 5f 70 72 65 70 61 72 65 3b 0a 20 20 20  end_prepare;.   
2a380 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
2a381 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
2a382 6f 63 6b 4c 69 73 74 28 64 62 29 3b 0a 0a 20 20  ockList(db);..  
2a383 70 50 61 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b  pParse->db = db;
2a384 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3e 3d 30  .  if( nBytes>=0
2a385 20 26 26 20 28 6e 42 79 74 65 73 3d 3d 30 20 7c   && (nBytes==0 |
2a386 7c 20 7a 53 71 6c 5b 6e 42 79 74 65 73 2d 31 5d  | zSql[nBytes-1]
2a387 21 3d 30 29 20 29 7b 0a 20 20 20 20 63 68 61 72  !=0) ){.    char
2a388 20 2a 7a 53 71 6c 43 6f 70 79 3b 0a 20 20 20 20   *zSqlCopy;.    
2a389 69 6e 74 20 6d 78 4c 65 6e 20 3d 20 64 62 2d 3e  int mxLen = db->
2a38a 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
2a38b 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b  MIT_SQL_LENGTH];
2a38c 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e  .    testcase( n
2a38d 42 79 74 65 73 3d 3d 6d 78 4c 65 6e 20 29 3b 0a  Bytes==mxLen );.
2a38e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6e 42      testcase( nB
2a38f 79 74 65 73 3d 3d 6d 78 4c 65 6e 2b 31 20 29 3b  ytes==mxLen+1 );
2a390 0a 20 20 20 20 69 66 28 20 6e 42 79 74 65 73 3e  .    if( nBytes>
2a391 6d 78 4c 65 6e 20 29 7b 0a 20 20 20 20 20 20 73  mxLen ){.      s
2a392 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
2a393 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 2c 20 22  SQLITE_TOOBIG, "
2a394 73 74 61 74 65 6d 65 6e 74 20 74 6f 6f 20 6c 6f  statement too lo
2a395 6e 67 22 29 3b 0a 20 20 20 20 20 20 28 76 6f 69  ng");.      (voi
2a396 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
2a397 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20 72 63  ff(db);.      rc
2a398 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
2a399 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f  t(db, SQLITE_TOO
2a39a 42 49 47 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  BIG);.      goto
2a39b 20 65 6e 64 5f 70 72 65 70 61 72 65 3b 0a 20 20   end_prepare;.  
2a39c 20 20 7d 0a 20 20 20 20 7a 53 71 6c 43 6f 70 79    }.    zSqlCopy
2a39d 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e   = sqlite3DbStrN
2a39e 44 75 70 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42  Dup(db, zSql, nB
2a39f 79 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 7a  ytes);.    if( z
2a3a0 53 71 6c 43 6f 70 79 20 29 7b 0a 20 20 20 20 20  SqlCopy ){.     
2a3a1 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65   sqlite3RunParse
2a3a2 72 28 70 50 61 72 73 65 2c 20 7a 53 71 6c 43 6f  r(pParse, zSqlCo
2a3a3 70 79 2c 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20  py, &zErrMsg);. 
2a3a4 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2a3a5 65 65 28 64 62 2c 20 7a 53 71 6c 43 6f 70 79 29  ee(db, zSqlCopy)
2a3a6 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
2a3a7 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 70 50  zTail = &zSql[pP
2a3a8 61 72 73 65 2d 3e 7a 54 61 69 6c 2d 7a 53 71 6c  arse->zTail-zSql
2a3a9 43 6f 70 79 5d 3b 0a 20 20 20 20 7d 65 6c 73 65  Copy];.    }else
2a3aa 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  {.      pParse->
2a3ab 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 6e 42  zTail = &zSql[nB
2a3ac 79 74 65 73 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  ytes];.    }.  }
2a3ad 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
2a3ae 33 52 75 6e 50 61 72 73 65 72 28 70 50 61 72 73  3RunParser(pPars
2a3af 65 2c 20 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73  e, zSql, &zErrMs
2a3b0 67 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 64  g);.  }..  if( d
2a3b1 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2a3b2 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  ){.    pParse->r
2a3b3 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
2a3b4 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61 72  ;.  }.  if( pPar
2a3b5 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 44  se->rc==SQLITE_D
2a3b6 4f 4e 45 20 29 20 70 50 61 72 73 65 2d 3e 72 63  ONE ) pParse->rc
2a3b7 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
2a3b8 69 66 28 20 70 50 61 72 73 65 2d 3e 63 68 65 63  if( pParse->chec
2a3b9 6b 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 73  kSchema ){.    s
2a3ba 63 68 65 6d 61 49 73 56 61 6c 69 64 28 70 50 61  chemaIsValid(pPa
2a3bb 72 73 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rse);.  }.  if( 
2a3bc 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49  pParse->rc==SQLI
2a3bd 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a 20 20 20  TE_SCHEMA ){.   
2a3be 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74   sqlite3ResetInt
2a3bf 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20  ernalSchema(db, 
2a3c0 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62  0);.  }.  if( db
2a3c1 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2a3c2 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63  {.    pParse->rc
2a3c3 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
2a3c4 0a 20 20 7d 0a 20 20 69 66 28 20 70 7a 54 61 69  .  }.  if( pzTai
2a3c5 6c 20 29 7b 0a 20 20 20 20 2a 70 7a 54 61 69 6c  l ){.    *pzTail
2a3c6 20 3d 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c   = pParse->zTail
2a3c7 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 70 50 61  ;.  }.  rc = pPa
2a3c8 72 73 65 2d 3e 72 63 3b 0a 0a 23 69 66 6e 64 65  rse->rc;..#ifnde
2a3c9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  f SQLITE_OMIT_EX
2a3ca 50 4c 41 49 4e 0a 20 20 69 66 28 20 72 63 3d 3d  PLAIN.  if( rc==
2a3cb 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61  SQLITE_OK && pPa
2a3cc 72 73 65 2d 3e 70 56 64 62 65 20 26 26 20 70 50  rse->pVdbe && pP
2a3cd 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b  arse->explain ){
2a3ce 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
2a3cf 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61  t char * const a
2a3d0 7a 43 6f 6c 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20  zColName[] = {. 
2a3d1 20 20 20 20 20 20 22 61 64 64 72 22 2c 20 22 6f        "addr", "o
2a3d2 70 63 6f 64 65 22 2c 20 22 70 31 22 2c 20 22 70  pcode", "p1", "p
2a3d3 32 22 2c 20 22 70 33 22 2c 20 22 70 34 22 2c 20  2", "p3", "p4", 
2a3d4 22 70 35 22 2c 20 22 63 6f 6d 6d 65 6e 74 22 2c  "p5", "comment",
2a3d5 0a 20 20 20 20 20 20 20 22 6f 72 64 65 72 22 2c  .       "order",
2a3d6 20 22 66 72 6f 6d 22 2c 20 22 64 65 74 61 69 6c   "from", "detail
2a3d7 22 0a 20 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74  ".    };.    int
2a3d8 20 69 46 69 72 73 74 2c 20 6d 78 3b 0a 20 20 20   iFirst, mx;.   
2a3d9 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70   if( pParse->exp
2a3da 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20 20 20 20  lain==2 ){.     
2a3db 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
2a3dc 75 6d 43 6f 6c 73 28 70 50 61 72 73 65 2d 3e 70  umCols(pParse->p
2a3dd 56 64 62 65 2c 20 33 29 3b 0a 20 20 20 20 20 20  Vdbe, 3);.      
2a3de 69 46 69 72 73 74 20 3d 20 38 3b 0a 20 20 20 20  iFirst = 8;.    
2a3df 20 20 6d 78 20 3d 20 31 31 3b 0a 20 20 20 20 7d    mx = 11;.    }
2a3e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
2a3e1 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
2a3e2 73 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c  s(pParse->pVdbe,
2a3e3 20 38 29 3b 0a 20 20 20 20 20 20 69 46 69 72 73   8);.      iFirs
2a3e4 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 6d 78 20  t = 0;.      mx 
2a3e5 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  = 8;.    }.    f
2a3e6 6f 72 28 69 3d 69 46 69 72 73 74 3b 20 69 3c 6d  or(i=iFirst; i<m
2a3e7 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  x; i++){.      s
2a3e8 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2a3e9 4e 61 6d 65 28 70 50 61 72 73 65 2d 3e 70 56 64  Name(pParse->pVd
2a3ea 62 65 2c 20 69 2d 69 46 69 72 73 74 2c 20 43 4f  be, i-iFirst, CO
2a3eb 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 0a 20 20 20 20  LNAME_NAME,.    
2a3ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a3ed 20 20 20 20 20 20 20 20 61 7a 43 6f 6c 4e 61 6d          azColNam
2a3ee 65 5b 69 5d 2c 20 53 51 4c 49 54 45 5f 53 54 41  e[i], SQLITE_STA
2a3ef 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TIC);.    }.  }.
2a3f0 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71  #endif..  if( sq
2a3f1 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
2a3f2 62 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  b) ){.    rc = S
2a3f3 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
2a3f4 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  }..  assert( db-
2a3f5 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 7c 7c  >init.busy==0 ||
2a3f6 20 73 61 76 65 53 71 6c 46 6c 61 67 3d 3d 30 20   saveSqlFlag==0 
2a3f7 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69  );.  if( db->ini
2a3f8 74 2e 62 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20  t.busy==0 ){.   
2a3f9 20 56 64 62 65 20 2a 70 56 64 62 65 20 3d 20 70   Vdbe *pVdbe = p
2a3fa 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
2a3fb 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
2a3fc 53 71 6c 28 70 56 64 62 65 2c 20 7a 53 71 6c 2c  Sql(pVdbe, zSql,
2a3fd 20 28 69 6e 74 29 28 70 50 61 72 73 65 2d 3e 7a   (int)(pParse->z
2a3fe 54 61 69 6c 2d 7a 53 71 6c 29 2c 20 73 61 76 65  Tail-zSql), save
2a3ff 53 71 6c 46 6c 61 67 29 3b 0a 20 20 7d 0a 20 20  SqlFlag);.  }.  
2a400 69 66 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62  if( pParse->pVdb
2a401 65 20 26 26 20 28 72 63 21 3d 53 51 4c 49 54 45  e && (rc!=SQLITE
2a402 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  _OK || db->mallo
2a403 63 46 61 69 6c 65 64 29 20 29 7b 0a 20 20 20 20  cFailed) ){.    
2a404 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
2a405 69 7a 65 28 70 50 61 72 73 65 2d 3e 70 56 64 62  ize(pParse->pVdb
2a406 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 21  e);.    assert(!
2a407 28 2a 70 70 53 74 6d 74 29 29 3b 0a 20 20 7d 65  (*ppStmt));.  }e
2a408 6c 73 65 7b 0a 20 20 20 20 2a 70 70 53 74 6d 74  lse{.    *ppStmt
2a409 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74   = (sqlite3_stmt
2a40a 2a 29 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  *)pParse->pVdbe;
2a40b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 45 72 72  .  }..  if( zErr
2a40c 4d 73 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Msg ){.    sqlit
2a40d 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
2a40e 22 25 73 22 2c 20 7a 45 72 72 4d 73 67 29 3b 0a  "%s", zErrMsg);.
2a40f 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2a410 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a  e(db, zErrMsg);.
2a411 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
2a412 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
2a413 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  , 0);.  }..  /* 
2a414 44 65 6c 65 74 65 20 61 6e 79 20 54 72 69 67 67  Delete any Trigg
2a415 65 72 50 72 67 20 73 74 72 75 63 74 75 72 65 73  erPrg structures
2a416 20 61 6c 6c 6f 63 61 74 65 64 20 77 68 69 6c 65   allocated while
2a417 20 70 61 72 73 69 6e 67 20 74 68 69 73 20 73 74   parsing this st
2a418 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 77 68  atement. */.  wh
2a419 69 6c 65 28 20 70 50 61 72 73 65 2d 3e 70 54 72  ile( pParse->pTr
2a41a 69 67 67 65 72 50 72 67 20 29 7b 0a 20 20 20 20  iggerPrg ){.    
2a41b 54 72 69 67 67 65 72 50 72 67 20 2a 70 54 20 3d  TriggerPrg *pT =
2a41c 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65   pParse->pTrigge
2a41d 72 50 72 67 3b 0a 20 20 20 20 70 50 61 72 73 65  rPrg;.    pParse
2a41e 2d 3e 70 54 72 69 67 67 65 72 50 72 67 20 3d 20  ->pTriggerPrg = 
2a41f 70 54 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73  pT->pNext;.    s
2a420 71 6c 69 74 65 33 56 64 62 65 50 72 6f 67 72 61  qlite3VdbeProgra
2a421 6d 44 65 6c 65 74 65 28 64 62 2c 20 70 54 2d 3e  mDelete(db, pT->
2a422 70 50 72 6f 67 72 61 6d 2c 20 30 29 3b 0a 20 20  pProgram, 0);.  
2a423 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2a424 64 62 2c 20 70 54 29 3b 0a 20 20 7d 0a 0a 65 6e  db, pT);.  }..en
2a425 64 5f 70 72 65 70 61 72 65 3a 0a 0a 20 20 73 71  d_prepare:..  sq
2a426 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64  lite3StackFree(d
2a427 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 72 63  b, pParse);.  rc
2a428 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
2a429 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 61 73 73  t(db, rc);.  ass
2a42a 65 72 74 28 20 28 72 63 26 64 62 2d 3e 65 72 72  ert( (rc&db->err
2a42b 4d 61 73 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 72  Mask)==rc );.  r
2a42c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 61 74  eturn rc;.}.stat
2a42d 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4c 6f  ic int sqlite3Lo
2a42e 63 6b 41 6e 64 50 72 65 70 61 72 65 28 0a 20 20  ckAndPrepare(.  
2a42f 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2a430 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2a431 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f  abase handle. */
2a432 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2a433 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Sql,         /* 
2a434 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51  UTF-8 encoded SQ
2a435 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a  L statement. */.
2a436 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20    int nBytes,   
2a437 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2a438 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
2a439 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74   bytes. */.  int
2a43a 20 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 20 20   saveSqlFlag,   
2a43b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
2a43c 6f 20 63 6f 70 79 20 53 51 4c 20 74 65 78 74 20  o copy SQL text 
2a43d 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  into the sqlite3
2a43e 5f 73 74 6d 74 20 2a 2f 0a 20 20 56 64 62 65 20  _stmt */.  Vdbe 
2a43f 2a 70 4f 6c 64 2c 20 20 20 20 20 20 20 20 20 20  *pOld,          
2a440 20 20 20 20 20 2f 2a 20 56 4d 20 62 65 69 6e 67       /* VM being
2a441 20 72 65 70 72 65 70 61 72 65 64 20 2a 2f 0a 20   reprepared */. 
2a442 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
2a443 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55  ppStmt,    /* OU
2a444 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  T: A pointer to 
2a445 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
2a446 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73  tement */.  cons
2a447 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20  t char **pzTail 
2a448 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e        /* OUT: En
2a449 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69  d of parsed stri
2a44a 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ng */.){.  int r
2a44b 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 70 53  c;.  assert( ppS
2a44c 74 6d 74 21 3d 30 20 29 3b 0a 20 20 2a 70 70 53  tmt!=0 );.  *ppS
2a44d 74 6d 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 21  tmt = 0;.  if( !
2a44e 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
2a44f 63 6b 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  ckOk(db) ){.    
2a450 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
2a451 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  SUSE;.  }.  sqli
2a452 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2a453 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71  db->mutex);.  sq
2a454 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
2a455 6c 6c 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 73  ll(db);.  rc = s
2a456 71 6c 69 74 65 33 50 72 65 70 61 72 65 28 64 62  qlite3Prepare(db
2a457 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20  , zSql, nBytes, 
2a458 73 61 76 65 53 71 6c 46 6c 61 67 2c 20 70 4f 6c  saveSqlFlag, pOl
2a459 64 2c 20 70 70 53 74 6d 74 2c 20 70 7a 54 61 69  d, ppStmt, pzTai
2a45a 6c 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  l);.  if( rc==SQ
2a45b 4c 49 54 45 5f 53 43 48 45 4d 41 20 29 7b 0a 20  LITE_SCHEMA ){. 
2a45c 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c     sqlite3_final
2a45d 69 7a 65 28 2a 70 70 53 74 6d 74 29 3b 0a 20 20  ize(*ppStmt);.  
2a45e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72    rc = sqlite3Pr
2a45f 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
2a460 6e 42 79 74 65 73 2c 20 73 61 76 65 53 71 6c 46  nBytes, saveSqlF
2a461 6c 61 67 2c 20 70 4f 6c 64 2c 20 70 70 53 74 6d  lag, pOld, ppStm
2a462 74 2c 20 70 7a 54 61 69 6c 29 3b 0a 20 20 7d 0a  t, pzTail);.  }.
2a463 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
2a464 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 73 71  aveAll(db);.  sq
2a465 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2a466 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2a467 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
2a468 0a 2a 2a 20 52 65 72 75 6e 20 74 68 65 20 63 6f  .** Rerun the co
2a469 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 61 20 73  mpilation of a s
2a46a 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 61  tatement after a
2a46b 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 2e 0a   schema change..
2a46c 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61  **.** If the sta
2a46d 74 65 6d 65 6e 74 20 69 73 20 73 75 63 63 65 73  tement is succes
2a46e 73 66 75 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65  sfully recompile
2a46f 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  d, return SQLITE
2a470 5f 4f 4b 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a  _OK. Otherwise,.
2a471 2a 2a 20 69 66 20 74 68 65 20 73 74 61 74 65 6d  ** if the statem
2a472 65 6e 74 20 63 61 6e 6e 6f 74 20 62 65 20 72 65  ent cannot be re
2a473 63 6f 6d 70 69 6c 65 64 20 62 65 63 61 75 73 65  compiled because
2a474 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74   another connect
2a475 69 6f 6e 20 68 61 73 0a 2a 2a 20 6c 6f 63 6b 65  ion has.** locke
2a476 64 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 61  d the sqlite3_ma
2a477 73 74 65 72 20 74 61 62 6c 65 2c 20 72 65 74 75  ster table, retu
2a478 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rn SQLITE_LOCKED
2a479 2e 20 49 66 20 61 6e 79 20 6f 74 68 65 72 20 65  . If any other e
2a47a 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2c 20  rror.** occurs, 
2a47b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 53 43  return SQLITE_SC
2a47c 48 45 4d 41 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  HEMA..*/.SQLITE_
2a47d 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2a47e 74 65 33 52 65 70 72 65 70 61 72 65 28 56 64 62  te3Reprepare(Vdb
2a47f 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  e *p){.  int rc;
2a480 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2a481 2a 70 4e 65 77 3b 0a 20 20 63 6f 6e 73 74 20 63  *pNew;.  const c
2a482 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 73 71 6c  har *zSql;.  sql
2a483 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 61 73 73  ite3 *db;..  ass
2a484 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
2a485 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 56  ex_held(sqlite3V
2a486 64 62 65 44 62 28 70 29 2d 3e 6d 75 74 65 78 29  dbeDb(p)->mutex)
2a487 20 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c   );.  zSql = sql
2a488 69 74 65 33 5f 73 71 6c 28 28 73 71 6c 69 74 65  ite3_sql((sqlite
2a489 33 5f 73 74 6d 74 20 2a 29 70 29 3b 0a 20 20 61  3_stmt *)p);.  a
2a48a 73 73 65 72 74 28 20 7a 53 71 6c 21 3d 30 20 29  ssert( zSql!=0 )
2a48b 3b 20 20 2f 2a 20 52 65 70 72 65 70 61 72 65 20  ;  /* Reprepare 
2a48c 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 6f 72 20  only called for 
2a48d 70 72 65 70 61 72 65 5f 76 32 28 29 20 73 74 61  prepare_v2() sta
2a48e 74 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 64 62 20  tements */.  db 
2a48f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 44 62 28  = sqlite3VdbeDb(
2a490 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  p);.  assert( sq
2a491 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
2a492 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
2a493 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63   rc = sqlite3Loc
2a494 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c 20  kAndPrepare(db, 
2a495 7a 53 71 6c 2c 20 2d 31 2c 20 30 2c 20 70 2c 20  zSql, -1, 0, p, 
2a496 26 70 4e 65 77 2c 20 30 29 3b 0a 20 20 69 66 28  &pNew, 0);.  if(
2a497 20 72 63 20 29 7b 0a 20 20 20 20 69 66 28 20 72   rc ){.    if( r
2a498 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
2a499 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
2a49a 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2a49b 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
2a49c 20 70 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20   pNew==0 );.    
2a49d 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49  return (rc==SQLI
2a49e 54 45 5f 4c 4f 43 4b 45 44 29 20 3f 20 53 51 4c  TE_LOCKED) ? SQL
2a49f 49 54 45 5f 4c 4f 43 4b 45 44 20 3a 20 53 51 4c  ITE_LOCKED : SQL
2a4a0 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 65  ITE_SCHEMA;.  }e
2a4a1 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
2a4a2 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20 20 7d 0a   pNew!=0 );.  }.
2a4a3 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61    sqlite3VdbeSwa
2a4a4 70 28 28 56 64 62 65 2a 29 70 4e 65 77 2c 20 70  p((Vdbe*)pNew, p
2a4a5 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 72 61 6e  );.  sqlite3Tran
2a4a6 73 66 65 72 42 69 6e 64 69 6e 67 73 28 70 4e 65  sferBindings(pNe
2a4a7 77 2c 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  w, (sqlite3_stmt
2a4a8 2a 29 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  *)p);.  sqlite3V
2a4a9 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 75  dbeResetStepResu
2a4aa 6c 74 28 28 56 64 62 65 2a 29 70 4e 65 77 29 3b  lt((Vdbe*)pNew);
2a4ab 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69  .  sqlite3VdbeFi
2a4ac 6e 61 6c 69 7a 65 28 28 56 64 62 65 2a 29 70 4e  nalize((Vdbe*)pN
2a4ad 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ew);.  return SQ
2a4ae 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
2a4af 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f 6e 73 20  ** Two versions 
2a4b0 6f 66 20 74 68 65 20 6f 66 66 69 63 69 61 6c 20  of the official 
2a4b1 41 50 49 2e 20 20 4c 65 67 61 63 79 20 61 6e 64  API.  Legacy and
2a4b2 20 6e 65 77 20 75 73 65 2e 20 20 49 6e 20 74 68   new use.  In th
2a4b3 65 20 6c 65 67 61 63 79 0a 2a 2a 20 76 65 72 73  e legacy.** vers
2a4b4 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61  ion, the origina
2a4b5 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 6e 6f  l SQL text is no
2a4b6 74 20 73 61 76 65 64 20 69 6e 20 74 68 65 20 70  t saved in the p
2a4b7 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2a4b8 74 0a 2a 2a 20 61 6e 64 20 73 6f 20 69 66 20 61  t.** and so if a
2a4b9 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 20 6f   schema change o
2a4ba 63 63 75 72 73 2c 20 53 51 4c 49 54 45 5f 53 43  ccurs, SQLITE_SC
2a4bb 48 45 4d 41 20 69 73 20 72 65 74 75 72 6e 65 64  HEMA is returned
2a4bc 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73   by.** sqlite3_s
2a4bd 74 65 70 28 29 2e 20 20 49 6e 20 74 68 65 20 6e  tep().  In the n
2a4be 65 77 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20  ew version, the 
2a4bf 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78  original SQL tex
2a4c0 74 20 69 73 20 72 65 74 61 69 6e 65 64 0a 2a 2a  t is retained.**
2a4c1 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d 65   and the stateme
2a4c2 6e 74 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61  nt is automatica
2a4c3 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 69  lly recompiled i
2a4c4 66 20 61 6e 20 73 63 68 65 6d 61 20 63 68 61 6e  f an schema chan
2a4c5 67 65 0a 2a 2a 20 6f 63 63 75 72 73 2e 0a 2a 2f  ge.** occurs..*/
2a4c6 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2a4c7 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
2a4c8 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2a4c9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a4ca 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
2a4cb 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2a4cc 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20   *zSql,         
2a4cd 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  /* UTF-8 encoded
2a4ce 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
2a4cf 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c  */.  int nBytes,
2a4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a4d1 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c  * Length of zSql
2a4d2 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20   in bytes. */.  
2a4d3 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70  sqlite3_stmt **p
2a4d4 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54  pStmt,    /* OUT
2a4d5 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  : A pointer to t
2a4d6 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74  he prepared stat
2a4d7 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ement */.  const
2a4d8 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20   char **pzTail  
2a4d9 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64       /* OUT: End
2a4da 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e   of parsed strin
2a4db 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  g */.){.  int rc
2a4dc 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
2a4dd 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64  LockAndPrepare(d
2a4de 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30 2c  b,zSql,nBytes,0,
2a4df 30 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29  0,ppStmt,pzTail)
2a4e0 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
2a4e1 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53  SQLITE_OK || ppS
2a4e2 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d  tmt==0 || *ppStm
2a4e3 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49  t==0 );  /* VERI
2a4e4 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20  FY: F13021 */.  
2a4e5 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
2a4e6 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2a4e7 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 0a  te3_prepare_v2(.
2a4e8 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2a4e9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2a4ea 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20  atabase handle. 
2a4eb 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2a4ec 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f  *zSql,         /
2a4ed 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  * UTF-8 encoded 
2a4ee 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a  SQL statement. *
2a4ef 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20  /.  int nBytes, 
2a4f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a4f1 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20   Length of zSql 
2a4f2 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73  in bytes. */.  s
2a4f3 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
2a4f4 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  Stmt,    /* OUT:
2a4f5 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
2a4f6 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
2a4f7 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ment */.  const 
2a4f8 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
2a4f9 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20      /* OUT: End 
2a4fa 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67  of parsed string
2a4fb 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
2a4fc 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4c  .  rc = sqlite3L
2a4fd 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64 62  ockAndPrepare(db
2a4fe 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 31 2c 30  ,zSql,nBytes,1,0
2a4ff 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b  ,ppStmt,pzTail);
2a500 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
2a501 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74  QLITE_OK || ppSt
2a502 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74  mt==0 || *ppStmt
2a503 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46  ==0 );  /* VERIF
2a504 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72  Y: F13021 */.  r
2a505 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69  eturn rc;.}...#i
2a506 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2a507 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 43 6f  T_UTF16./*.** Co
2a508 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 31 36  mpile the UTF-16
2a509 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
2a50a 74 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f  tement zSql into
2a50b 20 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e   a statement han
2a50c 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  dle..*/.static i
2a50d 6e 74 20 73 71 6c 69 74 65 33 50 72 65 70 61 72  nt sqlite3Prepar
2a50e 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  e16(.  sqlite3 *
2a50f 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2a510 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
2a511 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73 74  dle. */ .  const
2a512 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20   void *zSql,    
2a513 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e       /* UTF-8 en
2a514 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
2a515 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ent. */.  int nB
2a516 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  ytes,           
2a517 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
2a518 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
2a519 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53 71 6c  */.  int saveSql
2a51a 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20 2f  Flag,          /
2a51b 2a 20 54 72 75 65 20 74 6f 20 73 61 76 65 20 53  * True to save S
2a51c 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74 68 65  QL text into the
2a51d 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2f   sqlite3_stmt */
2a51e 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2a51f 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20  **ppStmt,    /* 
2a520 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74  OUT: A pointer t
2a521 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  o the prepared s
2a522 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f  tatement */.  co
2a523 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69  nst void **pzTai
2a524 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20  l       /* OUT: 
2a525 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73 74  End of parsed st
2a526 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 2f 2a 20  ring */.){.  /* 
2a527 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 75  This function cu
2a528 72 72 65 6e 74 6c 79 20 77 6f 72 6b 73 20 62 79  rrently works by
2a529 20 66 69 72 73 74 20 74 72 61 6e 73 66 6f 72 6d   first transform
2a52a 69 6e 67 20 74 68 65 20 55 54 46 2d 31 36 0a 20  ing the UTF-16. 
2a52b 20 2a 2a 20 65 6e 63 6f 64 65 64 20 73 74 72 69   ** encoded stri
2a52c 6e 67 20 74 6f 20 55 54 46 2d 38 2c 20 74 68 65  ng to UTF-8, the
2a52d 6e 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74  n invoking sqlit
2a52e 65 33 5f 70 72 65 70 61 72 65 28 29 2e 20 54 68  e3_prepare(). Th
2a52f 65 0a 20 20 2a 2a 20 74 72 69 63 6b 79 20 62 69  e.  ** tricky bi
2a530 74 20 69 73 20 66 69 67 75 72 69 6e 67 20 6f 75  t is figuring ou
2a531 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f  t the pointer to
2a532 20 72 65 74 75 72 6e 20 69 6e 20 2a 70 7a 54 61   return in *pzTa
2a533 69 6c 2e 0a 20 20 2a 2f 0a 20 20 63 68 61 72 20  il..  */.  char 
2a534 2a 7a 53 71 6c 38 3b 0a 20 20 63 6f 6e 73 74 20  *zSql8;.  const 
2a535 63 68 61 72 20 2a 7a 54 61 69 6c 38 20 3d 20 30  char *zTail8 = 0
2a536 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
2a537 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72  ITE_OK;..  asser
2a538 74 28 20 70 70 53 74 6d 74 20 29 3b 0a 20 20 2a  t( ppStmt );.  *
2a539 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 69 66  ppStmt = 0;.  if
2a53a 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79  ( !sqlite3Safety
2a53b 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a 20  CheckOk(db) ){. 
2a53c 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2a53d 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73  _MISUSE;.  }.  s
2a53e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2a53f 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
2a540 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74 65 33   zSql8 = sqlite3
2a541 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 53 71  Utf16to8(db, zSq
2a542 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 66  l, nBytes);.  if
2a543 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20 20 20 72  ( zSql8 ){.    r
2a544 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41  c = sqlite3LockA
2a545 6e 64 50 72 65 70 61 72 65 28 64 62 2c 20 7a 53  ndPrepare(db, zS
2a546 71 6c 38 2c 20 2d 31 2c 20 73 61 76 65 53 71 6c  ql8, -1, saveSql
2a547 46 6c 61 67 2c 20 30 2c 20 70 70 53 74 6d 74 2c  Flag, 0, ppStmt,
2a548 20 26 7a 54 61 69 6c 38 29 3b 0a 20 20 7d 0a 0a   &zTail8);.  }..
2a549 20 20 69 66 28 20 7a 54 61 69 6c 38 20 26 26 20    if( zTail8 && 
2a54a 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20 20 2f 2a  pzTail ){.    /*
2a54b 20 49 66 20 73 71 6c 69 74 65 33 5f 70 72 65 70   If sqlite3_prep
2a54c 61 72 65 20 72 65 74 75 72 6e 73 20 61 20 74 61  are returns a ta
2a54d 69 6c 20 70 6f 69 6e 74 65 72 2c 20 77 65 20 63  il pointer, we c
2a54e 61 6c 63 75 6c 61 74 65 20 74 68 65 0a 20 20 20  alculate the.   
2a54f 20 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 70   ** equivalent p
2a550 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20  ointer into the 
2a551 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 62 79  UTF-16 string by
2a552 20 63 6f 75 6e 74 69 6e 67 20 74 68 65 20 75 6e   counting the un
2a553 69 63 6f 64 65 0a 20 20 20 20 2a 2a 20 63 68 61  icode.    ** cha
2a554 72 61 63 74 65 72 73 20 62 65 74 77 65 65 6e 20  racters between 
2a555 7a 53 71 6c 38 20 61 6e 64 20 7a 54 61 69 6c 38  zSql8 and zTail8
2a556 2c 20 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72  , and then retur
2a557 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 0a 20  ning a pointer. 
2a558 20 20 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 6e     ** the same n
2a559 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
2a55a 65 72 73 20 69 6e 74 6f 20 74 68 65 20 55 54 46  ers into the UTF
2a55b 2d 31 36 20 73 74 72 69 6e 67 2e 0a 20 20 20 20  -16 string..    
2a55c 2a 2f 0a 20 20 20 20 69 6e 74 20 63 68 61 72 73  */.    int chars
2a55d 5f 70 61 72 73 65 64 20 3d 20 73 71 6c 69 74 65  _parsed = sqlite
2a55e 33 55 74 66 38 43 68 61 72 4c 65 6e 28 7a 53 71  3Utf8CharLen(zSq
2a55f 6c 38 2c 20 28 69 6e 74 29 28 7a 54 61 69 6c 38  l8, (int)(zTail8
2a560 2d 7a 53 71 6c 38 29 29 3b 0a 20 20 20 20 2a 70  -zSql8));.    *p
2a561 7a 54 61 69 6c 20 3d 20 28 75 38 20 2a 29 7a 53  zTail = (u8 *)zS
2a562 71 6c 20 2b 20 73 71 6c 69 74 65 33 55 74 66 31  ql + sqlite3Utf1
2a563 36 42 79 74 65 4c 65 6e 28 7a 53 71 6c 2c 20 63  6ByteLen(zSql, c
2a564 68 61 72 73 5f 70 61 72 73 65 64 29 3b 0a 20 20  hars_parsed);.  
2a565 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
2a566 65 28 64 62 2c 20 7a 53 71 6c 38 29 3b 20 0a 20  e(db, zSql8); . 
2a567 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
2a568 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
2a569 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
2a56a 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
2a56b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2a56c 2f 2a 0a 2a 2a 20 54 77 6f 20 76 65 72 73 69 6f  /*.** Two versio
2a56d 6e 73 20 6f 66 20 74 68 65 20 6f 66 66 69 63 69  ns of the offici
2a56e 61 6c 20 41 50 49 2e 20 20 4c 65 67 61 63 79 20  al API.  Legacy 
2a56f 61 6e 64 20 6e 65 77 20 75 73 65 2e 20 20 49 6e  and new use.  In
2a570 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a 20 76   the legacy.** v
2a571 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67  ersion, the orig
2a572 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73  inal SQL text is
2a573 20 6e 6f 74 20 73 61 76 65 64 20 69 6e 20 74 68   not saved in th
2a574 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
2a575 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f 20 69  ment.** and so i
2a576 66 20 61 20 73 63 68 65 6d 61 20 63 68 61 6e 67  f a schema chang
2a577 65 20 6f 63 63 75 72 73 2c 20 53 51 4c 49 54 45  e occurs, SQLITE
2a578 5f 53 43 48 45 4d 41 20 69 73 20 72 65 74 75 72  _SCHEMA is retur
2a579 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65  ned by.** sqlite
2a57a 33 5f 73 74 65 70 28 29 2e 20 20 49 6e 20 74 68  3_step().  In th
2a57b 65 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c 20 74  e new version, t
2a57c 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20  he original SQL 
2a57d 74 65 78 74 20 69 73 20 72 65 74 61 69 6e 65 64  text is retained
2a57e 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74  .** and the stat
2a57f 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d 61 74  ement is automat
2a580 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65  ically recompile
2a581 64 20 69 66 20 61 6e 20 73 63 68 65 6d 61 20 63  d if an schema c
2a582 68 61 6e 67 65 0a 2a 2a 20 6f 63 63 75 72 73 2e  hange.** occurs.
2a583 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2a584 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nt sqlite3_prepa
2a585 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20  re16(.  sqlite3 
2a586 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2a587 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
2a588 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73  ndle. */ .  cons
2a589 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20  t void *zSql,   
2a58a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65        /* UTF-8 e
2a58b 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
2a58c 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ment. */.  int n
2a58d 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
2a58e 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
2a58f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
2a590 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
2a591 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20  mt **ppStmt,    
2a592 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65  /* OUT: A pointe
2a593 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65  r to the prepare
2a594 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  d statement */. 
2a595 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a   const void **pz
2a596 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
2a597 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
2a598 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
2a599 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73  int rc;.  rc = s
2a59a 71 6c 69 74 65 33 50 72 65 70 61 72 65 31 36 28  qlite3Prepare16(
2a59b 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 30  db,zSql,nBytes,0
2a59c 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b  ,ppStmt,pzTail);
2a59d 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
2a59e 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74  QLITE_OK || ppSt
2a59f 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74  mt==0 || *ppStmt
2a5a0 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46  ==0 );  /* VERIF
2a5a1 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72  Y: F13021 */.  r
2a5a2 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49  eturn rc;.}.SQLI
2a5a3 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2a5a4 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28  e3_prepare16_v2(
2a5a5 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2a5a6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a5a7 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  Database handle.
2a5a8 20 2a 2f 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69   */ .  const voi
2a5a9 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  d *zSql,        
2a5aa 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   /* UTF-8 encode
2a5ab 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
2a5ac 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
2a5ad 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
2a5ae 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
2a5af 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
2a5b0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
2a5b1 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55  ppStmt,    /* OU
2a5b2 54 3a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  T: A pointer to 
2a5b3 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
2a5b4 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73  tement */.  cons
2a5b5 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20  t void **pzTail 
2a5b6 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e        /* OUT: En
2a5b7 64 20 6f 66 20 70 61 72 73 65 64 20 73 74 72 69  d of parsed stri
2a5b8 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ng */.){.  int r
2a5b9 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  c;.  rc = sqlite
2a5ba 33 50 72 65 70 61 72 65 31 36 28 64 62 2c 7a 53  3Prepare16(db,zS
2a5bb 71 6c 2c 6e 42 79 74 65 73 2c 31 2c 70 70 53 74  ql,nBytes,1,ppSt
2a5bc 6d 74 2c 70 7a 54 61 69 6c 29 3b 0a 20 20 61 73  mt,pzTail);.  as
2a5bd 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
2a5be 5f 4f 4b 20 7c 7c 20 70 70 53 74 6d 74 3d 3d 30  _OK || ppStmt==0
2a5bf 20 7c 7c 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29   || *ppStmt==0 )
2a5c0 3b 20 20 2f 2a 20 56 45 52 49 46 59 3a 20 46 31  ;  /* VERIFY: F1
2a5c1 33 30 32 31 20 2a 2f 0a 20 20 72 65 74 75 72 6e  3021 */.  return
2a5c2 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f   rc;.}..#endif /
2a5c3 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
2a5c4 46 31 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  F16 */../*******
2a5c5 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
2a5c6 72 65 70 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  repare.c *******
2a5c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5c9 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2a5ca 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2a5cb 6c 65 20 73 65 6c 65 63 74 2e 63 20 2a 2a 2a 2a  le select.c ****
2a5cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5ce 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2a5cf 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
2a5d0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2a5d1 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2a5d2 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2a5d3 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2a5d4 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2a5d5 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2a5d6 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2a5d7 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2a5d8 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2a5d9 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2a5da 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2a5db 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2a5dc 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2a5dd 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2a5de 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2a5df 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2a5e0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2a5e1 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2a5e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a5e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2a5e6 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
2a5e7 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74  ains C code rout
2a5e8 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61  ines that are ca
2a5e9 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
2a5ea 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20  er.** to handle 
2a5eb 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2a5ec 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a  s in SQLite..*/.
2a5ed 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
2a5ee 6c 6c 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ll the content o
2a5ef 66 20 61 20 53 65 6c 65 63 74 20 73 74 72 75 63  f a Select struc
2a5f0 74 75 72 65 20 62 75 74 20 64 6f 20 6e 6f 74 20  ture but do not 
2a5f1 64 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 74 68  deallocate.** th
2a5f2 65 20 73 65 6c 65 63 74 20 73 74 72 75 63 74 75  e select structu
2a5f3 72 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 73 74  re itself..*/.st
2a5f4 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 53  atic void clearS
2a5f5 65 6c 65 63 74 28 73 71 6c 69 74 65 33 20 2a 64  elect(sqlite3 *d
2a5f6 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  b, Select *p){. 
2a5f7 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2a5f8 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 45  Delete(db, p->pE
2a5f9 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  List);.  sqlite3
2a5fa 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
2a5fb 2c 20 70 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71  , p->pSrc);.  sq
2a5fc 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2a5fd 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a  db, p->pWhere);.
2a5fe 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2a5ff 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  tDelete(db, p->p
2a600 47 72 6f 75 70 42 79 29 3b 0a 20 20 73 71 6c 69  GroupBy);.  sqli
2a601 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2a602 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20  , p->pHaving);. 
2a603 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2a604 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f  Delete(db, p->pO
2a605 72 64 65 72 42 79 29 3b 0a 20 20 73 71 6c 69 74  rderBy);.  sqlit
2a606 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
2a607 62 2c 20 70 2d 3e 70 50 72 69 6f 72 29 3b 0a 20  b, p->pPrior);. 
2a608 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2a609 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74  te(db, p->pLimit
2a60a 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2a60b 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f  Delete(db, p->pO
2a60c 66 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ffset);.}../*.**
2a60d 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 53 65   Initialize a Se
2a60e 6c 65 63 74 44 65 73 74 20 73 74 72 75 63 74 75  lectDest structu
2a60f 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  re..*/.SQLITE_PR
2a610 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2a611 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
2a612 28 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65  (SelectDest *pDe
2a613 73 74 2c 20 69 6e 74 20 65 44 65 73 74 2c 20 69  st, int eDest, i
2a614 6e 74 20 69 50 61 72 6d 29 7b 0a 20 20 70 44 65  nt iParm){.  pDe
2a615 73 74 2d 3e 65 44 65 73 74 20 3d 20 28 75 38 29  st->eDest = (u8)
2a616 65 44 65 73 74 3b 0a 20 20 70 44 65 73 74 2d 3e  eDest;.  pDest->
2a617 69 50 61 72 6d 20 3d 20 69 50 61 72 6d 3b 0a 20  iParm = iParm;. 
2a618 20 70 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79   pDest->affinity
2a619 20 3d 20 30 3b 0a 20 20 70 44 65 73 74 2d 3e 69   = 0;.  pDest->i
2a61a 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 44 65 73 74  Mem = 0;.  pDest
2a61b 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 7d 0a 0a 0a  ->nMem = 0;.}...
2a61c 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
2a61d 20 6e 65 77 20 53 65 6c 65 63 74 20 73 74 72 75   new Select stru
2a61e 63 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e  cture and return
2a61f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2a620 61 74 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  at.** structure.
2a621 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2a622 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74  TE Select *sqlit
2a623 65 33 53 65 6c 65 63 74 4e 65 77 28 0a 20 20 50  e3SelectNew(.  P
2a624 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2a625 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2a626 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
2a627 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20  rList *pEList,  
2a628 20 20 20 2f 2a 20 77 68 69 63 68 20 63 6f 6c 75     /* which colu
2a629 6d 6e 73 20 74 6f 20 69 6e 63 6c 75 64 65 20 69  mns to include i
2a62a 6e 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a  n the result */.
2a62b 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c    SrcList *pSrc,
2a62c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46          /* the F
2a62d 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20 77 68  ROM clause -- wh
2a62e 69 63 68 20 74 61 62 6c 65 73 20 74 6f 20 73 63  ich tables to sc
2a62f 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  an */.  Expr *pW
2a630 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  here,         /*
2a631 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2a632 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
2a633 2a 70 47 72 6f 75 70 42 79 2c 20 20 20 2f 2a 20  *pGroupBy,   /* 
2a634 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  the GROUP BY cla
2a635 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  use */.  Expr *p
2a636 48 61 76 69 6e 67 2c 20 20 20 20 20 20 20 20 2f  Having,        /
2a637 2a 20 74 68 65 20 48 41 56 49 4e 47 20 63 6c 61  * the HAVING cla
2a638 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
2a639 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f  t *pOrderBy,   /
2a63a 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  * the ORDER BY c
2a63b 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lause */.  int i
2a63c 73 44 69 73 74 69 6e 63 74 2c 20 20 20 20 20 20  sDistinct,      
2a63d 20 2f 2a 20 74 72 75 65 20 69 66 20 74 68 65 20   /* true if the 
2a63e 44 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64  DISTINCT keyword
2a63f 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
2a640 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 20   Expr *pLimit,  
2a641 20 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49 54 20         /* LIMIT 
2a642 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d 65 61  value.  NULL mea
2a643 6e 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 20  ns not used */. 
2a644 20 45 78 70 72 20 2a 70 4f 66 66 73 65 74 20 20   Expr *pOffset  
2a645 20 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54         /* OFFSET
2a646 20 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d 65   value.  NULL me
2a647 61 6e 73 20 6e 6f 20 6f 66 66 73 65 74 20 2a 2f  ans no offset */
2a648 0a 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e  .){.  Select *pN
2a649 65 77 3b 0a 20 20 53 65 6c 65 63 74 20 73 74 61  ew;.  Select sta
2a64a 6e 64 69 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20  ndin;.  sqlite3 
2a64b 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2a64c 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
2a64d 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
2a64e 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29  b, sizeof(*pNew)
2a64f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62   );.  assert( db
2a650 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c  ->mallocFailed |
2a651 7c 20 21 70 4f 66 66 73 65 74 20 7c 7c 20 70 4c  | !pOffset || pL
2a652 69 6d 69 74 20 29 3b 20 2f 2a 20 4f 46 46 53 45  imit ); /* OFFSE
2a653 54 20 69 6d 70 6c 69 65 73 20 4c 49 4d 49 54 20  T implies LIMIT 
2a654 2a 2f 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  */.  if( pNew==0
2a655 20 29 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 26   ){.    pNew = &
2a656 73 74 61 6e 64 69 6e 3b 0a 20 20 20 20 6d 65 6d  standin;.    mem
2a657 73 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a  set(pNew, 0, siz
2a658 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 7d  eof(*pNew));.  }
2a659 0a 20 20 69 66 28 20 70 45 4c 69 73 74 3d 3d 30  .  if( pEList==0
2a65a 20 29 7b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d   ){.    pEList =
2a65b 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2a65c 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30  Append(pParse, 0
2a65d 2c 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62  , sqlite3Expr(db
2a65e 2c 54 4b 5f 41 4c 4c 2c 30 29 29 3b 0a 20 20 7d  ,TK_ALL,0));.  }
2a65f 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20  .  pNew->pEList 
2a660 3d 20 70 45 4c 69 73 74 3b 0a 20 20 70 4e 65 77  = pEList;.  pNew
2a661 2d 3e 70 53 72 63 20 3d 20 70 53 72 63 3b 0a 20  ->pSrc = pSrc;. 
2a662 20 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d 20   pNew->pWhere = 
2a663 70 57 68 65 72 65 3b 0a 20 20 70 4e 65 77 2d 3e  pWhere;.  pNew->
2a664 70 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75  pGroupBy = pGrou
2a665 70 42 79 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 61  pBy;.  pNew->pHa
2a666 76 69 6e 67 20 3d 20 70 48 61 76 69 6e 67 3b 0a  ving = pHaving;.
2a667 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 79    pNew->pOrderBy
2a668 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70   = pOrderBy;.  p
2a669 4e 65 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20  New->selFlags = 
2a66a 69 73 44 69 73 74 69 6e 63 74 20 3f 20 53 46 5f  isDistinct ? SF_
2a66b 44 69 73 74 69 6e 63 74 20 3a 20 30 3b 0a 20 20  Distinct : 0;.  
2a66c 70 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45  pNew->op = TK_SE
2a66d 4c 45 43 54 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c  LECT;.  pNew->pL
2a66e 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20  imit = pLimit;. 
2a66f 20 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d   pNew->pOffset =
2a670 20 70 4f 66 66 73 65 74 3b 0a 20 20 61 73 73 65   pOffset;.  asse
2a671 72 74 28 20 70 4f 66 66 73 65 74 3d 3d 30 20 7c  rt( pOffset==0 |
2a672 7c 20 70 4c 69 6d 69 74 21 3d 30 20 29 3b 0a 20  | pLimit!=0 );. 
2a673 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45   pNew->addrOpenE
2a674 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70  phm[0] = -1;.  p
2a675 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  New->addrOpenEph
2a676 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65  m[1] = -1;.  pNe
2a677 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  w->addrOpenEphm[
2a678 32 5d 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 64  2] = -1;.  if( d
2a679 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2a67a 29 20 7b 0a 20 20 20 20 63 6c 65 61 72 53 65 6c  ) {.    clearSel
2a67b 65 63 74 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20  ect(db, pNew);. 
2a67c 20 20 20 69 66 28 20 70 4e 65 77 21 3d 26 73 74     if( pNew!=&st
2a67d 61 6e 64 69 6e 20 29 20 73 71 6c 69 74 65 33 44  andin ) sqlite3D
2a67e 62 46 72 65 65 28 64 62 2c 20 70 4e 65 77 29 3b  bFree(db, pNew);
2a67f 0a 20 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20  .    pNew = 0;. 
2a680 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77   }.  return pNew
2a681 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  ;.}../*.** Delet
2a682 65 20 74 68 65 20 67 69 76 65 6e 20 53 65 6c 65  e the given Sele
2a683 63 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  ct structure and
2a684 20 61 6c 6c 20 6f 66 20 69 74 73 20 73 75 62 73   all of its subs
2a685 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51  tructures..*/.SQ
2a686 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2a687 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  d sqlite3SelectD
2a688 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64  elete(sqlite3 *d
2a689 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  b, Select *p){. 
2a68a 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63 6c   if( p ){.    cl
2a68b 65 61 72 53 65 6c 65 63 74 28 64 62 2c 20 70 29  earSelect(db, p)
2a68c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
2a68d 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a  ree(db, p);.  }.
2a68e 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 31  }../*.** Given 1
2a68f 20 74 6f 20 33 20 69 64 65 6e 74 69 66 69 65 72   to 3 identifier
2a690 73 20 70 72 65 63 65 65 64 69 6e 67 20 74 68 65  s preceeding the
2a691 20 4a 4f 49 4e 20 6b 65 79 77 6f 72 64 2c 20 64   JOIN keyword, d
2a692 65 74 65 72 6d 69 6e 65 20 74 68 65 0a 2a 2a 20  etermine the.** 
2a693 74 79 70 65 20 6f 66 20 6a 6f 69 6e 2e 20 20 52  type of join.  R
2a694 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
2a695 20 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 65   constant that e
2a696 78 70 72 65 73 73 65 73 20 74 68 61 74 20 74 79  xpresses that ty
2a697 70 65 0a 2a 2a 20 69 6e 20 74 65 72 6d 73 20 6f  pe.** in terms o
2a698 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2a699 62 69 74 20 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a  bit values:.**.*
2a69a 2a 20 20 20 20 20 4a 54 5f 49 4e 4e 45 52 0a 2a  *     JT_INNER.*
2a69b 2a 20 20 20 20 20 4a 54 5f 43 52 4f 53 53 0a 2a  *     JT_CROSS.*
2a69c 2a 20 20 20 20 20 4a 54 5f 4f 55 54 45 52 0a 2a  *     JT_OUTER.*
2a69d 2a 20 20 20 20 20 4a 54 5f 4e 41 54 55 52 41 4c  *     JT_NATURAL
2a69e 0a 2a 2a 20 20 20 20 20 4a 54 5f 4c 45 46 54 0a  .**     JT_LEFT.
2a69f 2a 2a 20 20 20 20 20 4a 54 5f 52 49 47 48 54 0a  **     JT_RIGHT.
2a6a0 2a 2a 0a 2a 2a 20 41 20 66 75 6c 6c 20 6f 75 74  **.** A full out
2a6a1 65 72 20 6a 6f 69 6e 20 69 73 20 74 68 65 20 63  er join is the c
2a6a2 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4a 54  ombination of JT
2a6a3 5f 4c 45 46 54 20 61 6e 64 20 4a 54 5f 52 49 47  _LEFT and JT_RIG
2a6a4 48 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  HT..**.** If an 
2a6a5 69 6c 6c 65 67 61 6c 20 6f 72 20 75 6e 73 75 70  illegal or unsup
2a6a6 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65  ported join type
2a6a7 20 69 73 20 73 65 65 6e 2c 20 74 68 65 6e 20 73   is seen, then s
2a6a8 74 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 61  till return.** a
2a6a9 20 6a 6f 69 6e 20 74 79 70 65 2c 20 62 75 74 20   join type, but 
2a6aa 70 75 74 20 61 6e 20 65 72 72 6f 72 20 69 6e 20  put an error in 
2a6ab 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63  the pParse struc
2a6ac 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ture..*/.SQLITE_
2a6ad 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2a6ae 74 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73  te3JoinType(Pars
2a6af 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
2a6b0 20 2a 70 41 2c 20 54 6f 6b 65 6e 20 2a 70 42 2c   *pA, Token *pB,
2a6b1 20 54 6f 6b 65 6e 20 2a 70 43 29 7b 0a 20 20 69   Token *pC){.  i
2a6b2 6e 74 20 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b  nt jointype = 0;
2a6b3 0a 20 20 54 6f 6b 65 6e 20 2a 61 70 41 6c 6c 5b  .  Token *apAll[
2a6b4 33 5d 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 3b 0a  3];.  Token *p;.
2a6b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a6b6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a6b7 20 20 30 31 32 33 34 35 36 37 38 39 20 31 32 33    0123456789 123
2a6b8 34 35 36 37 38 39 20 31 32 33 34 35 36 37 38 39  456789 123456789
2a6b9 20 31 32 33 20 2a 2f 0a 20 20 73 74 61 74 69 63   123 */.  static
2a6ba 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4b 65 79   const char zKey
2a6bb 54 65 78 74 5b 5d 20 3d 20 22 6e 61 74 75 72 61  Text[] = "natura
2a6bc 6c 65 66 74 6f 75 74 65 72 69 67 68 74 66 75 6c  leftouterightful
2a6bd 6c 69 6e 6e 65 72 63 72 6f 73 73 22 3b 0a 20 20  linnercross";.  
2a6be 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72  static const str
2a6bf 75 63 74 20 7b 0a 20 20 20 20 75 38 20 69 3b 20  uct {.    u8 i; 
2a6c0 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e         /* Beginn
2a6c1 69 6e 67 20 6f 66 20 6b 65 79 77 6f 72 64 20 74  ing of keyword t
2a6c2 65 78 74 20 69 6e 20 7a 4b 65 79 54 65 78 74 5b  ext in zKeyText[
2a6c3 5d 20 2a 2f 0a 20 20 20 20 75 38 20 6e 43 68 61  ] */.    u8 nCha
2a6c4 72 3b 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20  r;    /* Length 
2a6c5 6f 66 20 74 68 65 20 6b 65 79 77 6f 72 64 20 69  of the keyword i
2a6c6 6e 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a  n characters */.
2a6c7 20 20 20 20 75 38 20 63 6f 64 65 3b 20 20 20 20      u8 code;    
2a6c8 20 2f 2a 20 4a 6f 69 6e 20 74 79 70 65 20 6d 61   /* Join type ma
2a6c9 73 6b 20 2a 2f 0a 20 20 7d 20 61 4b 65 79 77 6f  sk */.  } aKeywo
2a6ca 72 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 2f 2a 20  rd[] = {.    /* 
2a6cb 6e 61 74 75 72 61 6c 20 2a 2f 20 7b 20 30 2c 20  natural */ { 0, 
2a6cc 20 37 2c 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20   7, JT_NATURAL  
2a6cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c                },
2a6ce 0a 20 20 20 20 2f 2a 20 6c 65 66 74 20 20 20 20  .    /* left    
2a6cf 2a 2f 20 7b 20 36 2c 20 20 34 2c 20 4a 54 5f 4c  */ { 6,  4, JT_L
2a6d0 45 46 54 7c 4a 54 5f 4f 55 54 45 52 20 20 20 20  EFT|JT_OUTER    
2a6d1 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20        },.    /* 
2a6d2 6f 75 74 65 72 20 20 20 2a 2f 20 7b 20 31 30 2c  outer   */ { 10,
2a6d3 20 35 2c 20 4a 54 5f 4f 55 54 45 52 20 20 20 20   5, JT_OUTER    
2a6d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c                },
2a6d5 0a 20 20 20 20 2f 2a 20 72 69 67 68 74 20 20 20  .    /* right   
2a6d6 2a 2f 20 7b 20 31 34 2c 20 35 2c 20 4a 54 5f 52  */ { 14, 5, JT_R
2a6d7 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 20 20  IGHT|JT_OUTER   
2a6d8 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20        },.    /* 
2a6d9 66 75 6c 6c 20 20 20 20 2a 2f 20 7b 20 31 39 2c  full    */ { 19,
2a6da 20 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 52   4, JT_LEFT|JT_R
2a6db 49 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 7d 2c  IGHT|JT_OUTER },
2a6dc 0a 20 20 20 20 2f 2a 20 69 6e 6e 65 72 20 20 20  .    /* inner   
2a6dd 2a 2f 20 7b 20 32 33 2c 20 35 2c 20 4a 54 5f 49  */ { 23, 5, JT_I
2a6de 4e 4e 45 52 20 20 20 20 20 20 20 20 20 20 20 20  NNER            
2a6df 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20        },.    /* 
2a6e0 63 72 6f 73 73 20 20 20 2a 2f 20 7b 20 32 38 2c  cross   */ { 28,
2a6e1 20 35 2c 20 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f   5, JT_INNER|JT_
2a6e2 43 52 4f 53 53 20 20 20 20 20 20 20 20 20 7d 2c  CROSS         },
2a6e3 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  .  };.  int i, j
2a6e4 3b 0a 20 20 61 70 41 6c 6c 5b 30 5d 20 3d 20 70  ;.  apAll[0] = p
2a6e5 41 3b 0a 20 20 61 70 41 6c 6c 5b 31 5d 20 3d 20  A;.  apAll[1] = 
2a6e6 70 42 3b 0a 20 20 61 70 41 6c 6c 5b 32 5d 20 3d  pB;.  apAll[2] =
2a6e7 20 70 43 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20   pC;.  for(i=0; 
2a6e8 69 3c 33 20 26 26 20 61 70 41 6c 6c 5b 69 5d 3b  i<3 && apAll[i];
2a6e9 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 61   i++){.    p = a
2a6ea 70 41 6c 6c 5b 69 5d 3b 0a 20 20 20 20 66 6f 72  pAll[i];.    for
2a6eb 28 6a 3d 30 3b 20 6a 3c 41 72 72 61 79 53 69 7a  (j=0; j<ArraySiz
2a6ec 65 28 61 4b 65 79 77 6f 72 64 29 3b 20 6a 2b 2b  e(aKeyword); j++
2a6ed 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  ){.      if( p->
2a6ee 6e 3d 3d 61 4b 65 79 77 6f 72 64 5b 6a 5d 2e 6e  n==aKeyword[j].n
2a6ef 43 68 61 72 20 0a 20 20 20 20 20 20 20 20 20 20  Char .          
2a6f0 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
2a6f1 6d 70 28 28 63 68 61 72 2a 29 70 2d 3e 7a 2c 20  mp((char*)p->z, 
2a6f2 26 7a 4b 65 79 54 65 78 74 5b 61 4b 65 79 77 6f  &zKeyText[aKeywo
2a6f3 72 64 5b 6a 5d 2e 69 5d 2c 20 70 2d 3e 6e 29 3d  rd[j].i], p->n)=
2a6f4 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 6f  =0 ){.        jo
2a6f5 69 6e 74 79 70 65 20 7c 3d 20 61 4b 65 79 77 6f  intype |= aKeywo
2a6f6 72 64 5b 6a 5d 2e 63 6f 64 65 3b 0a 20 20 20 20  rd[j].code;.    
2a6f7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2a6f8 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73   }.    }.    tes
2a6f9 74 63 61 73 65 28 20 6a 3d 3d 30 20 7c 7c 20 6a  tcase( j==0 || j
2a6fa 3d 3d 31 20 7c 7c 20 6a 3d 3d 32 20 7c 7c 20 6a  ==1 || j==2 || j
2a6fb 3d 3d 33 20 7c 7c 20 6a 3d 3d 34 20 7c 7c 20 6a  ==3 || j==4 || j
2a6fc 3d 3d 35 20 7c 7c 20 6a 3d 3d 36 20 29 3b 0a 20  ==5 || j==6 );. 
2a6fd 20 20 20 69 66 28 20 6a 3e 3d 41 72 72 61 79 53     if( j>=ArrayS
2a6fe 69 7a 65 28 61 4b 65 79 77 6f 72 64 29 20 29 7b  ize(aKeyword) ){
2a6ff 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20  .      jointype 
2a700 7c 3d 20 4a 54 5f 45 52 52 4f 52 3b 0a 20 20 20  |= JT_ERROR;.   
2a701 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2a702 20 20 7d 0a 20 20 69 66 28 0a 20 20 20 20 20 28    }.  if(.     (
2a703 6a 6f 69 6e 74 79 70 65 20 26 20 28 4a 54 5f 49  jointype & (JT_I
2a704 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 52 29 29 3d  NNER|JT_OUTER))=
2a705 3d 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55  =(JT_INNER|JT_OU
2a706 54 45 52 29 20 7c 7c 0a 20 20 20 20 20 28 6a 6f  TER) ||.     (jo
2a707 69 6e 74 79 70 65 20 26 20 4a 54 5f 45 52 52 4f  intype & JT_ERRO
2a708 52 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 63  R)!=0.  ){.    c
2a709 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 70 20 3d  onst char *zSp =
2a70a 20 22 20 22 3b 0a 20 20 20 20 61 73 73 65 72 74   " ";.    assert
2a70b 28 20 70 42 21 3d 30 20 29 3b 0a 20 20 20 20 69  ( pB!=0 );.    i
2a70c 66 28 20 70 43 3d 3d 30 20 29 7b 20 7a 53 70 2b  f( pC==0 ){ zSp+
2a70d 2b 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33  +; }.    sqlite3
2a70e 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2a70f 20 22 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73   "unknown or uns
2a710 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79  upported join ty
2a711 70 65 3a 20 22 0a 20 20 20 20 20 20 20 22 25 54  pe: ".       "%T
2a712 20 25 54 25 73 25 54 22 2c 20 70 41 2c 20 70 42   %T%s%T", pA, pB
2a713 2c 20 7a 53 70 2c 20 70 43 29 3b 0a 20 20 20 20  , zSp, pC);.    
2a714 6a 6f 69 6e 74 79 70 65 20 3d 20 4a 54 5f 49 4e  jointype = JT_IN
2a715 4e 45 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  NER;.  }else if(
2a716 20 28 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f   (jointype & JT_
2a717 4f 55 54 45 52 29 21 3d 30 20 0a 20 20 20 20 20  OUTER)!=0 .     
2a718 20 20 20 20 26 26 20 28 6a 6f 69 6e 74 79 70 65      && (jointype
2a719 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f 52   & (JT_LEFT|JT_R
2a71a 49 47 48 54 29 29 21 3d 4a 54 5f 4c 45 46 54 20  IGHT))!=JT_LEFT 
2a71b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2a71c 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
2a71d 20 20 20 20 20 20 22 52 49 47 48 54 20 61 6e 64        "RIGHT and
2a71e 20 46 55 4c 4c 20 4f 55 54 45 52 20 4a 4f 49 4e   FULL OUTER JOIN
2a71f 73 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65 6e  s are not curren
2a720 74 6c 79 20 73 75 70 70 6f 72 74 65 64 22 29 3b  tly supported");
2a721 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20  .    jointype = 
2a722 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 0a 20 20  JT_INNER;.  }.  
2a723 72 65 74 75 72 6e 20 6a 6f 69 6e 74 79 70 65 3b  return jointype;
2a724 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2a725 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20   the index of a 
2a726 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c  column in a tabl
2a727 65 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66  e.  Return -1 if
2a728 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69   the column.** i
2a729 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20  s not contained 
2a72a 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f  in the table..*/
2a72b 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 75  .static int colu
2a72c 6d 6e 49 6e 64 65 78 28 54 61 62 6c 65 20 2a 70  mnIndex(Table *p
2a72d 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Tab, const char 
2a72e 2a 7a 43 6f 6c 29 7b 0a 20 20 69 6e 74 20 69 3b  *zCol){.  int i;
2a72f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54  .  for(i=0; i<pT
2a730 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ab->nCol; i++){.
2a731 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
2a732 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f  trICmp(pTab->aCo
2a733 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  l[i].zName, zCol
2a734 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b  )==0 ) return i;
2a735 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31  .  }.  return -1
2a736 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
2a737 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
2a738 20 74 6f 20 61 64 64 20 74 65 72 6d 73 20 69 6d   to add terms im
2a739 70 6c 69 65 64 20 62 79 20 4a 4f 49 4e 20 73 79  plied by JOIN sy
2a73a 6e 74 61 78 20 74 6f 20 74 68 65 0a 2a 2a 20 57  ntax to the.** W
2a73b 48 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 72  HERE clause expr
2a73c 65 73 73 69 6f 6e 20 6f 66 20 61 20 53 45 4c 45  ession of a SELE
2a73d 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68  CT statement. Th
2a73e 65 20 6e 65 77 20 74 65 72 6d 2c 20 77 68 69 63  e new term, whic
2a73f 68 0a 2a 2a 20 69 73 20 41 4e 44 65 64 20 77 69  h.** is ANDed wi
2a740 74 68 20 74 68 65 20 65 78 69 73 74 69 6e 67 20  th the existing 
2a741 57 48 45 52 45 20 63 6c 61 75 73 65 2c 20 69 73  WHERE clause, is
2a742 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a   of the form:.**
2a743 0a 2a 2a 20 20 20 20 28 74 61 62 31 2e 63 6f 6c  .**    (tab1.col
2a744 31 20 3d 20 74 61 62 32 2e 63 6f 6c 32 29 0a 2a  1 = tab2.col2).*
2a745 2a 0a 2a 2a 20 77 68 65 72 65 20 74 61 62 31 20  *.** where tab1 
2a746 69 73 20 74 68 65 20 69 53 72 63 27 74 68 20 74  is the iSrc'th t
2a747 61 62 6c 65 20 69 6e 20 53 72 63 4c 69 73 74 20  able in SrcList 
2a748 70 53 72 63 20 61 6e 64 20 74 61 62 32 20 69 73  pSrc and tab2 is
2a749 20 74 68 65 20 0a 2a 2a 20 28 69 53 72 63 2b 31   the .** (iSrc+1
2a74a 29 27 74 68 2e 20 43 6f 6c 75 6d 6e 20 63 6f 6c  )'th. Column col
2a74b 31 20 69 73 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c  1 is column iCol
2a74c 4c 65 66 74 20 6f 66 20 74 61 62 31 2c 20 61 6e  Left of tab1, an
2a74d 64 20 63 6f 6c 32 20 69 73 0a 2a 2a 20 63 6f 6c  d col2 is.** col
2a74e 75 6d 6e 20 69 43 6f 6c 52 69 67 68 74 20 6f 66  umn iColRight of
2a74f 20 74 61 62 32 2e 0a 2a 2f 0a 73 74 61 74 69 63   tab2..*/.static
2a750 20 76 6f 69 64 20 61 64 64 57 68 65 72 65 54 65   void addWhereTe
2a751 72 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  rm(.  Parse *pPa
2a752 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
2a753 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2a754 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72   context */.  Sr
2a755 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20  cList *pSrc,    
2a756 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2a757 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20   List of tables 
2a758 69 6e 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  in FROM clause *
2a759 2f 0a 20 20 69 6e 74 20 69 53 72 63 2c 20 20 20  /.  int iSrc,   
2a75a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a75b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
2a75c 66 69 72 73 74 20 74 61 62 6c 65 20 74 6f 20 6a  first table to j
2a75d 6f 69 6e 20 69 6e 20 70 53 72 63 20 2a 2f 0a 20  oin in pSrc */. 
2a75e 20 69 6e 74 20 69 43 6f 6c 4c 65 66 74 2c 20 20   int iColLeft,  
2a75f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a760 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c   /* Index of col
2a761 75 6d 6e 20 69 6e 20 66 69 72 73 74 20 74 61 62  umn in first tab
2a762 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  le */.  int iCol
2a763 52 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20  Right,          
2a764 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2a765 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 73 65   of column in se
2a766 63 6f 6e 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20  cond table */.  
2a767 69 6e 74 20 69 73 4f 75 74 65 72 4a 6f 69 6e 2c  int isOuterJoin,
2a768 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a769 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
2a76a 69 73 20 61 6e 20 4f 55 54 45 52 20 6a 6f 69 6e  is an OUTER join
2a76b 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 70 70 57   */.  Expr **ppW
2a76c 68 65 72 65 20 20 20 20 20 20 20 20 20 20 20 20  here            
2a76d 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
2a76e 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
2a76f 65 20 74 6f 20 61 64 64 20 74 6f 20 2a 2f 0a 29  e to add to */.)
2a770 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
2a771 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2a772 45 78 70 72 20 2a 70 45 31 3b 0a 20 20 45 78 70  Expr *pE1;.  Exp
2a773 72 20 2a 70 45 32 3b 0a 20 20 45 78 70 72 20 2a  r *pE2;.  Expr *
2a774 70 45 71 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  pEq;..  assert( 
2a775 70 53 72 63 2d 3e 6e 53 72 63 3e 28 69 53 72 63  pSrc->nSrc>(iSrc
2a776 2b 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  +1) );.  assert(
2a777 20 70 53 72 63 2d 3e 61 5b 69 53 72 63 5d 2e 70   pSrc->a[iSrc].p
2a778 54 61 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Tab );.  assert(
2a779 20 70 53 72 63 2d 3e 61 5b 69 53 72 63 2b 31 5d   pSrc->a[iSrc+1]
2a77a 2e 70 54 61 62 20 29 3b 0a 0a 20 20 70 45 31 20  .pTab );..  pE1 
2a77b 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 43  = sqlite3CreateC
2a77c 6f 6c 75 6d 6e 45 78 70 72 28 64 62 2c 20 70 53  olumnExpr(db, pS
2a77d 72 63 2c 20 69 53 72 63 2c 20 69 43 6f 6c 4c 65  rc, iSrc, iColLe
2a77e 66 74 29 3b 0a 20 20 70 45 32 20 3d 20 73 71 6c  ft);.  pE2 = sql
2a77f 69 74 65 33 43 72 65 61 74 65 43 6f 6c 75 6d 6e  ite3CreateColumn
2a780 45 78 70 72 28 64 62 2c 20 70 53 72 63 2c 20 69  Expr(db, pSrc, i
2a781 53 72 63 2b 31 2c 20 69 43 6f 6c 52 69 67 68 74  Src+1, iColRight
2a782 29 3b 0a 0a 20 20 70 45 71 20 3d 20 73 71 6c 69  );..  pEq = sqli
2a783 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2a784 20 54 4b 5f 45 51 2c 20 70 45 31 2c 20 70 45 32   TK_EQ, pE1, pE2
2a785 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 45 71 20  , 0);.  if( pEq 
2a786 26 26 20 69 73 4f 75 74 65 72 4a 6f 69 6e 20 29  && isOuterJoin )
2a787 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f  {.    ExprSetPro
2a788 70 65 72 74 79 28 70 45 71 2c 20 45 50 5f 46 72  perty(pEq, EP_Fr
2a789 6f 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 61 73 73  omJoin);.    ass
2a78a 65 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79  ert( !ExprHasAny
2a78b 50 72 6f 70 65 72 74 79 28 70 45 71 2c 20 45 50  Property(pEq, EP
2a78c 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65  _TokenOnly|EP_Re
2a78d 64 75 63 65 64 29 20 29 3b 0a 20 20 20 20 45 78  duced) );.    Ex
2a78e 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c 65  prSetIrreducible
2a78f 28 70 45 71 29 3b 0a 20 20 20 20 70 45 71 2d 3e  (pEq);.    pEq->
2a790 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20  iRightJoinTable 
2a791 3d 20 28 69 31 36 29 70 45 32 2d 3e 69 54 61 62  = (i16)pE2->iTab
2a792 6c 65 3b 0a 20 20 7d 0a 20 20 2a 70 70 57 68 65  le;.  }.  *ppWhe
2a793 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
2a794 41 6e 64 28 64 62 2c 20 2a 70 70 57 68 65 72 65  And(db, *ppWhere
2a795 2c 20 70 45 71 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  , pEq);.}../*.**
2a796 20 53 65 74 20 74 68 65 20 45 50 5f 46 72 6f 6d   Set the EP_From
2a797 4a 6f 69 6e 20 70 72 6f 70 65 72 74 79 20 6f 6e  Join property on
2a798 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68   all terms of th
2a799 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69  e given expressi
2a79a 6f 6e 2e 0a 2a 2a 20 41 6e 64 20 73 65 74 20 74  on..** And set t
2a79b 68 65 20 45 78 70 72 2e 69 52 69 67 68 74 4a 6f  he Expr.iRightJo
2a79c 69 6e 54 61 62 6c 65 20 74 6f 20 69 54 61 62 6c  inTable to iTabl
2a79d 65 20 66 6f 72 20 65 76 65 72 79 20 74 65 72 6d  e for every term
2a79e 20 69 6e 20 74 68 65 0a 2a 2a 20 65 78 70 72 65   in the.** expre
2a79f 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ssion..**.** The
2a7a0 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f   EP_FromJoin pro
2a7a1 70 65 72 74 79 20 69 73 20 75 73 65 64 20 6f 6e  perty is used on
2a7a2 20 74 65 72 6d 73 20 6f 66 20 61 6e 20 65 78 70   terms of an exp
2a7a3 72 65 73 73 69 6f 6e 20 74 6f 20 74 65 6c 6c 0a  ression to tell.
2a7a4 2a 2a 20 74 68 65 20 4c 45 46 54 20 4f 55 54 45  ** the LEFT OUTE
2a7a5 52 20 4a 4f 49 4e 20 70 72 6f 63 65 73 73 69 6e  R JOIN processin
2a7a6 67 20 6c 6f 67 69 63 20 74 68 61 74 20 74 68 69  g logic that thi
2a7a7 73 20 74 65 72 6d 20 69 73 20 70 61 72 74 20 6f  s term is part o
2a7a8 66 20 74 68 65 0a 2a 2a 20 6a 6f 69 6e 20 72 65  f the.** join re
2a7a9 73 74 72 69 63 74 69 6f 6e 20 73 70 65 63 69 66  striction specif
2a7aa 69 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72  ied in the ON or
2a7ab 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 61 6e   USING clause an
2a7ac 64 20 6e 6f 74 20 61 20 70 61 72 74 0a 2a 2a 20  d not a part.** 
2a7ad 6f 66 20 74 68 65 20 6d 6f 72 65 20 67 65 6e 65  of the more gene
2a7ae 72 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65  ral WHERE clause
2a7af 2e 20 20 54 68 65 73 65 20 74 65 72 6d 73 20 61  .  These terms a
2a7b0 72 65 20 6d 6f 76 65 64 20 6f 76 65 72 20 74 6f  re moved over to
2a7b1 20 74 68 65 0a 2a 2a 20 57 48 45 52 45 20 63 6c   the.** WHERE cl
2a7b2 61 75 73 65 20 64 75 72 69 6e 67 20 6a 6f 69 6e  ause during join
2a7b3 20 70 72 6f 63 65 73 73 69 6e 67 20 62 75 74 20   processing but 
2a7b4 77 65 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d  we need to remem
2a7b5 62 65 72 20 74 68 61 74 20 74 68 65 79 0a 2a 2a  ber that they.**
2a7b6 20 6f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 74   originated in t
2a7b7 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63  he ON or USING c
2a7b8 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  lause..**.** The
2a7b9 20 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e   Expr.iRightJoin
2a7ba 54 61 62 6c 65 20 74 65 6c 6c 73 20 74 68 65 20  Table tells the 
2a7bb 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f  WHERE clause pro
2a7bc 63 65 73 73 69 6e 67 20 74 68 61 74 20 74 68 65  cessing that the
2a7bd 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 64  .** expression d
2a7be 65 70 65 6e 64 73 20 6f 6e 20 74 61 62 6c 65 20  epends on table 
2a7bf 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20  iRightJoinTable 
2a7c0 65 76 65 6e 20 69 66 20 74 68 61 74 20 74 61 62  even if that tab
2a7c1 6c 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 78 70  le is not.** exp
2a7c2 6c 69 63 69 74 6c 79 20 6d 65 6e 74 69 6f 6e 65  licitly mentione
2a7c3 64 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73  d in the express
2a7c4 69 6f 6e 2e 20 20 54 68 61 74 20 69 6e 66 6f 72  ion.  That infor
2a7c5 6d 61 74 69 6f 6e 20 69 73 20 6e 65 65 64 65 64  mation is needed
2a7c6 0a 2a 2a 20 66 6f 72 20 63 61 73 65 73 20 6c 69  .** for cases li
2a7c7 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
2a7c8 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
2a7c9 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20  t1 LEFT JOIN t2 
2a7ca 4f 4e 20 74 31 2e 61 3d 74 32 2e 62 20 41 4e 44  ON t1.a=t2.b AND
2a7cb 20 74 31 2e 78 3d 35 0a 2a 2a 0a 2a 2a 20 54 68   t1.x=5.**.** Th
2a7cc 65 20 77 68 65 72 65 20 63 6c 61 75 73 65 20 6e  e where clause n
2a7cd 65 65 64 73 20 74 6f 20 64 65 66 65 72 20 74 68  eeds to defer th
2a7ce 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74 68  e handling of th
2a7cf 65 20 74 31 2e 78 3d 35 0a 2a 2a 20 74 65 72 6d  e t1.x=5.** term
2a7d0 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65   until after the
2a7d1 20 74 32 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20   t2 loop of the 
2a7d2 6a 6f 69 6e 2e 20 20 49 6e 20 74 68 61 74 20 77  join.  In that w
2a7d3 61 79 2c 20 61 0a 2a 2a 20 4e 55 4c 4c 20 74 32  ay, a.** NULL t2
2a7d4 20 72 6f 77 20 77 69 6c 6c 20 62 65 20 69 6e 73   row will be ins
2a7d5 65 72 74 65 64 20 77 68 65 6e 65 76 65 72 20 74  erted whenever t
2a7d6 31 2e 78 21 3d 35 2e 20 20 49 66 20 77 65 20 64  1.x!=5.  If we d
2a7d7 6f 20 6e 6f 74 0a 2a 2a 20 64 65 66 65 72 20 74  o not.** defer t
2a7d8 68 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74  he handling of t
2a7d9 31 2e 78 3d 35 2c 20 69 74 20 77 69 6c 6c 20 62  1.x=5, it will b
2a7da 65 20 70 72 6f 63 65 73 73 65 64 20 69 6d 6d 65  e processed imme
2a7db 64 69 61 74 65 6c 79 0a 2a 2a 20 61 66 74 65 72  diately.** after
2a7dc 20 74 68 65 20 74 31 20 6c 6f 6f 70 20 61 6e 64   the t1 loop and
2a7dd 20 72 6f 77 73 20 77 69 74 68 20 74 31 2e 78 21   rows with t1.x!
2a7de 3d 35 20 77 69 6c 6c 20 6e 65 76 65 72 20 61 70  =5 will never ap
2a7df 70 65 61 72 20 69 6e 0a 2a 2a 20 74 68 65 20 6f  pear in.** the o
2a7e0 75 74 70 75 74 2c 20 77 68 69 63 68 20 69 73 20  utput, which is 
2a7e1 69 6e 63 6f 72 72 65 63 74 2e 0a 2a 2f 0a 73 74  incorrect..*/.st
2a7e2 61 74 69 63 20 76 6f 69 64 20 73 65 74 4a 6f 69  atic void setJoi
2a7e3 6e 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20 69  nExpr(Expr *p, i
2a7e4 6e 74 20 69 54 61 62 6c 65 29 7b 0a 20 20 77 68  nt iTable){.  wh
2a7e5 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 45 78  ile( p ){.    Ex
2a7e6 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c  prSetProperty(p,
2a7e7 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20   EP_FromJoin);. 
2a7e8 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
2a7e9 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
2a7ea 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45  , EP_TokenOnly|E
2a7eb 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20  P_Reduced) );.  
2a7ec 20 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63    ExprSetIrreduc
2a7ed 69 62 6c 65 28 70 29 3b 0a 20 20 20 20 70 2d 3e  ible(p);.    p->
2a7ee 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20  iRightJoinTable 
2a7ef 3d 20 28 69 31 36 29 69 54 61 62 6c 65 3b 0a 20  = (i16)iTable;. 
2a7f0 20 20 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 70     setJoinExpr(p
2a7f1 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c 65 29  ->pLeft, iTable)
2a7f2 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 52 69  ;.    p = p->pRi
2a7f3 67 68 74 3b 0a 20 20 7d 20 0a 7d 0a 0a 2f 2a 0a  ght;.  } .}../*.
2a7f4 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2a7f5 70 72 6f 63 65 73 73 65 73 20 74 68 65 20 6a 6f  processes the jo
2a7f6 69 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  in information f
2a7f7 6f 72 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  or a SELECT stat
2a7f8 65 6d 65 6e 74 2e 0a 2a 2a 20 4f 4e 20 61 6e 64  ement..** ON and
2a7f9 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 61   USING clauses a
2a7fa 72 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  re converted int
2a7fb 6f 20 65 78 74 72 61 20 74 65 72 6d 73 20 6f 66  o extra terms of
2a7fc 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2a7fd 65 2e 0a 2a 2a 20 4e 41 54 55 52 41 4c 20 6a 6f  e..** NATURAL jo
2a7fe 69 6e 73 20 61 6c 73 6f 20 63 72 65 61 74 65 20  ins also create 
2a7ff 65 78 74 72 61 20 57 48 45 52 45 20 63 6c 61 75  extra WHERE clau
2a800 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20  se terms..**.** 
2a801 54 68 65 20 74 65 72 6d 73 20 6f 66 20 61 20 46  The terms of a F
2a802 52 4f 4d 20 63 6c 61 75 73 65 20 61 72 65 20 63  ROM clause are c
2a803 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
2a804 53 65 6c 65 63 74 2e 70 53 72 63 20 73 74 72 75  Select.pSrc stru
2a805 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 6c 65  cture..** The le
2a806 66 74 20 6d 6f 73 74 20 74 61 62 6c 65 20 69 73  ft most table is
2a807 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
2a808 20 69 6e 20 53 65 6c 65 63 74 2e 70 53 72 63 2e   in Select.pSrc.
2a809 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74    The right-most
2a80a 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 74 68 65  .** table is the
2a80b 20 6c 61 73 74 20 65 6e 74 72 79 2e 20 20 54 68   last entry.  Th
2a80c 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20  e join operator 
2a80d 69 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 65  is held in the e
2a80e 6e 74 72 79 20 74 6f 0a 2a 2a 20 74 68 65 20 6c  ntry to.** the l
2a80f 65 66 74 2e 20 20 54 68 75 73 20 65 6e 74 72 79  eft.  Thus entry
2a810 20 30 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20   0 contains the 
2a811 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 66 6f  join operator fo
2a812 72 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65  r the join betwe
2a813 65 6e 0a 2a 2a 20 65 6e 74 72 69 65 73 20 30 20  en.** entries 0 
2a814 61 6e 64 20 31 2e 20 20 41 6e 79 20 4f 4e 20 6f  and 1.  Any ON o
2a815 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20  r USING clauses 
2a816 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2a817 74 68 65 20 6a 6f 69 6e 20 61 72 65 0a 2a 2a 20  the join are.** 
2a818 61 6c 73 6f 20 61 74 74 61 63 68 65 64 20 74 6f  also attached to
2a819 20 74 68 65 20 6c 65 66 74 20 65 6e 74 72 79 2e   the left entry.
2a81a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2a81b 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ine returns the 
2a81c 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73  number of errors
2a81d 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f   encountered..*/
2a81e 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
2a81f 74 65 50 72 6f 63 65 73 73 4a 6f 69 6e 28 50 61  teProcessJoin(Pa
2a820 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c  rse *pParse, Sel
2a821 65 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69  ect *p){.  SrcLi
2a822 73 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20  st *pSrc;       
2a823 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c             /* Al
2a824 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  l tables in the 
2a825 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
2a826 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20   int i, j;      
2a827 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a828 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2a829 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  s */.  struct Sr
2a82a 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 65 66  cList_item *pLef
2a82b 74 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 74  t;     /* Left t
2a82c 61 62 6c 65 20 62 65 69 6e 67 20 6a 6f 69 6e 65  able being joine
2a82d 64 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  d */.  struct Sr
2a82e 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 52 69 67  cList_item *pRig
2a82f 68 74 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20  ht;    /* Right 
2a830 74 61 62 6c 65 20 62 65 69 6e 67 20 6a 6f 69 6e  table being join
2a831 65 64 20 2a 2f 0a 0a 20 20 70 53 72 63 20 3d 20  ed */..  pSrc = 
2a832 70 2d 3e 70 53 72 63 3b 0a 20 20 70 4c 65 66 74  p->pSrc;.  pLeft
2a833 20 3d 20 26 70 53 72 63 2d 3e 61 5b 30 5d 3b 0a   = &pSrc->a[0];.
2a834 20 20 70 52 69 67 68 74 20 3d 20 26 70 4c 65 66    pRight = &pLef
2a835 74 5b 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  t[1];.  for(i=0;
2a836 20 69 3c 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b   i<pSrc->nSrc-1;
2a837 20 69 2b 2b 2c 20 70 52 69 67 68 74 2b 2b 2c 20   i++, pRight++, 
2a838 70 4c 65 66 74 2b 2b 29 7b 0a 20 20 20 20 54 61  pLeft++){.    Ta
2a839 62 6c 65 20 2a 70 4c 65 66 74 54 61 62 20 3d 20  ble *pLeftTab = 
2a83a 70 4c 65 66 74 2d 3e 70 54 61 62 3b 0a 20 20 20  pLeft->pTab;.   
2a83b 20 54 61 62 6c 65 20 2a 70 52 69 67 68 74 54 61   Table *pRightTa
2a83c 62 20 3d 20 70 52 69 67 68 74 2d 3e 70 54 61 62  b = pRight->pTab
2a83d 3b 0a 20 20 20 20 69 6e 74 20 69 73 4f 75 74 65  ;.    int isOute
2a83e 72 3b 0a 0a 20 20 20 20 69 66 28 20 4e 45 56 45  r;..    if( NEVE
2a83f 52 28 70 4c 65 66 74 54 61 62 3d 3d 30 20 7c 7c  R(pLeftTab==0 ||
2a840 20 70 52 69 67 68 74 54 61 62 3d 3d 30 29 20 29   pRightTab==0) )
2a841 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
2a842 73 4f 75 74 65 72 20 3d 20 28 70 52 69 67 68 74  sOuter = (pRight
2a843 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f  ->jointype & JT_
2a844 4f 55 54 45 52 29 21 3d 30 3b 0a 0a 20 20 20 20  OUTER)!=0;..    
2a845 2f 2a 20 57 68 65 6e 20 74 68 65 20 4e 41 54 55  /* When the NATU
2a846 52 41 4c 20 6b 65 79 77 6f 72 64 20 69 73 20 70  RAL keyword is p
2a847 72 65 73 65 6e 74 2c 20 61 64 64 20 57 48 45 52  resent, add WHER
2a848 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 66  E clause terms f
2a849 6f 72 0a 20 20 20 20 2a 2a 20 65 76 65 72 79 20  or.    ** every 
2a84a 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20  column that the 
2a84b 74 77 6f 20 74 61 62 6c 65 73 20 68 61 76 65 20  two tables have 
2a84c 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a 20 20 20 20 2a  in common..    *
2a84d 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74  /.    if( pRight
2a84e 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f  ->jointype & JT_
2a84f 4e 41 54 55 52 41 4c 20 29 7b 0a 20 20 20 20 20  NATURAL ){.     
2a850 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e   if( pRight->pOn
2a851 20 7c 7c 20 70 52 69 67 68 74 2d 3e 70 55 73 69   || pRight->pUsi
2a852 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ng ){.        sq
2a853 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2a854 61 72 73 65 2c 20 22 61 20 4e 41 54 55 52 41 4c  arse, "a NATURAL
2a855 20 6a 6f 69 6e 20 6d 61 79 20 6e 6f 74 20 68 61   join may not ha
2a856 76 65 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  ve ".           
2a857 22 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20  "an ON or USING 
2a858 63 6c 61 75 73 65 22 2c 20 30 29 3b 0a 20 20 20  clause", 0);.   
2a859 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2a85a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
2a85b 28 6a 3d 30 3b 20 6a 3c 70 4c 65 66 74 54 61 62  (j=0; j<pLeftTab
2a85c 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  ->nCol; j++){.  
2a85d 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d        char *zNam
2a85e 65 20 3d 20 70 4c 65 66 74 54 61 62 2d 3e 61 43  e = pLeftTab->aC
2a85f 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  ol[j].zName;.   
2a860 20 20 20 20 20 69 6e 74 20 69 52 69 67 68 74 43       int iRightC
2a861 6f 6c 20 3d 20 63 6f 6c 75 6d 6e 49 6e 64 65 78  ol = columnIndex
2a862 28 70 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d  (pRightTab, zNam
2a863 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
2a864 69 52 69 67 68 74 43 6f 6c 3e 3d 30 20 29 7b 0a  iRightCol>=0 ){.
2a865 20 20 20 20 20 20 20 20 20 20 61 64 64 57 68 65            addWhe
2a866 72 65 54 65 72 6d 28 70 50 61 72 73 65 2c 20 70  reTerm(pParse, p
2a867 53 72 63 2c 20 69 2c 20 6a 2c 20 69 52 69 67 68  Src, i, j, iRigh
2a868 74 43 6f 6c 2c 20 69 73 4f 75 74 65 72 2c 20 26  tCol, isOuter, &
2a869 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20  p->pWhere);.    
2a86a 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2a86b 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 73 61    }..    /* Disa
2a86c 6c 6c 6f 77 20 62 6f 74 68 20 4f 4e 20 61 6e 64  llow both ON and
2a86d 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 69   USING clauses i
2a86e 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 0a  n the same join.
2a86f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
2a870 52 69 67 68 74 2d 3e 70 4f 6e 20 26 26 20 70 52  Right->pOn && pR
2a871 69 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a  ight->pUsing ){.
2a872 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2a873 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63  orMsg(pParse, "c
2a874 61 6e 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20  annot have both 
2a875 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 22 0a 20  ON and USING ". 
2a876 20 20 20 20 20 20 20 22 63 6c 61 75 73 65 73 20         "clauses 
2a877 69 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e  in the same join
2a878 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ");.      return
2a879 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f   1;.    }..    /
2a87a 2a 20 41 64 64 20 74 68 65 20 4f 4e 20 63 6c 61  * Add the ON cla
2a87b 75 73 65 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  use to the end o
2a87c 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
2a87d 73 65 2c 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  se, connected by
2a87e 0a 20 20 20 20 2a 2a 20 61 6e 20 41 4e 44 20 6f  .    ** an AND o
2a87f 70 65 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a  perator..    */.
2a880 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e      if( pRight->
2a881 70 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28  pOn ){.      if(
2a882 20 69 73 4f 75 74 65 72 20 29 20 73 65 74 4a 6f   isOuter ) setJo
2a883 69 6e 45 78 70 72 28 70 52 69 67 68 74 2d 3e 70  inExpr(pRight->p
2a884 4f 6e 2c 20 70 52 69 67 68 74 2d 3e 69 43 75 72  On, pRight->iCur
2a885 73 6f 72 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70  sor);.      p->p
2a886 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45  Where = sqlite3E
2a887 78 70 72 41 6e 64 28 70 50 61 72 73 65 2d 3e 64  xprAnd(pParse->d
2a888 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 70 52  b, p->pWhere, pR
2a889 69 67 68 74 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20  ight->pOn);.    
2a88a 20 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 3d 20    pRight->pOn = 
2a88b 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  0;.    }..    /*
2a88c 20 43 72 65 61 74 65 20 65 78 74 72 61 20 74 65   Create extra te
2a88d 72 6d 73 20 6f 6e 20 74 68 65 20 57 48 45 52 45  rms on the WHERE
2a88e 20 63 6c 61 75 73 65 20 66 6f 72 20 65 61 63 68   clause for each
2a88f 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 0a 20 20   column named.  
2a890 20 20 2a 2a 20 69 6e 20 74 68 65 20 55 53 49 4e    ** in the USIN
2a891 47 20 63 6c 61 75 73 65 2e 20 20 45 78 61 6d 70  G clause.  Examp
2a892 6c 65 3a 20 49 66 20 74 68 65 20 74 77 6f 20 74  le: If the two t
2a893 61 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e  ables to be join
2a894 65 64 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 41  ed are .    ** A
2a895 20 61 6e 64 20 42 20 61 6e 64 20 74 68 65 20 55   and B and the U
2a896 53 49 4e 47 20 63 6c 61 75 73 65 20 6e 61 6d 65  SING clause name
2a897 73 20 58 2c 20 59 2c 20 61 6e 64 20 5a 2c 20 74  s X, Y, and Z, t
2a898 68 65 6e 20 61 64 64 20 74 68 69 73 0a 20 20 20  hen add this.   
2a899 20 2a 2a 20 74 6f 20 74 68 65 20 57 48 45 52 45   ** to the WHERE
2a89a 20 63 6c 61 75 73 65 3a 20 20 20 20 41 2e 58 3d   clause:    A.X=
2a89b 42 2e 58 20 41 4e 44 20 41 2e 59 3d 42 2e 59 20  B.X AND A.Y=B.Y 
2a89c 41 4e 44 20 41 2e 5a 3d 42 2e 5a 0a 20 20 20 20  AND A.Z=B.Z.    
2a89d 2a 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72  ** Report an err
2a89e 6f 72 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e  or if any column
2a89f 20 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68   mentioned in th
2a8a0 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 69  e USING clause i
2a8a1 73 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 63 6f 6e  s.    ** not con
2a8a2 74 61 69 6e 65 64 20 69 6e 20 62 6f 74 68 20 74  tained in both t
2a8a3 61 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e  ables to be join
2a8a4 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ed..    */.    i
2a8a5 66 28 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e  f( pRight->pUsin
2a8a6 67 20 29 7b 0a 20 20 20 20 20 20 49 64 4c 69 73  g ){.      IdLis
2a8a7 74 20 2a 70 4c 69 73 74 20 3d 20 70 52 69 67 68  t *pList = pRigh
2a8a8 74 2d 3e 70 55 73 69 6e 67 3b 0a 20 20 20 20 20  t->pUsing;.     
2a8a9 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 69 73   for(j=0; j<pLis
2a8aa 74 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20  t->nId; j++){.  
2a8ab 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d        char *zNam
2a8ac 65 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e  e = pList->a[j].
2a8ad 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69  zName;.        i
2a8ae 6e 74 20 69 4c 65 66 74 43 6f 6c 20 3d 20 63 6f  nt iLeftCol = co
2a8af 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66 74 54  lumnIndex(pLeftT
2a8b0 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab, zName);.    
2a8b1 20 20 20 20 69 6e 74 20 69 52 69 67 68 74 43 6f      int iRightCo
2a8b2 6c 20 3d 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28  l = columnIndex(
2a8b3 70 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d 65  pRightTab, zName
2a8b4 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
2a8b5 4c 65 66 74 43 6f 6c 3c 30 20 7c 7c 20 69 52 69  LeftCol<0 || iRi
2a8b6 67 68 74 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20  ghtCol<0 ){.    
2a8b7 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2a8b8 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63  orMsg(pParse, "c
2a8b9 61 6e 6e 6f 74 20 6a 6f 69 6e 20 75 73 69 6e 67  annot join using
2a8ba 20 63 6f 6c 75 6d 6e 20 25 73 20 2d 20 63 6f 6c   column %s - col
2a8bb 75 6d 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20  umn ".          
2a8bc 20 20 22 6e 6f 74 20 70 72 65 73 65 6e 74 20 69    "not present i
2a8bd 6e 20 62 6f 74 68 20 74 61 62 6c 65 73 22 2c 20  n both tables", 
2a8be 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20  zName);.        
2a8bf 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2a8c0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 64      }.        ad
2a8c1 64 57 68 65 72 65 54 65 72 6d 28 70 50 61 72 73  dWhereTerm(pPars
2a8c2 65 2c 20 70 53 72 63 2c 20 69 2c 20 69 4c 65 66  e, pSrc, i, iLef
2a8c3 74 43 6f 6c 2c 20 69 52 69 67 68 74 43 6f 6c 2c  tCol, iRightCol,
2a8c4 20 69 73 4f 75 74 65 72 2c 20 26 70 2d 3e 70 57   isOuter, &p->pW
2a8c5 68 65 72 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  here);.      }. 
2a8c6 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2a8c7 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 0;.}../*.** In
2a8c8 73 65 72 74 20 63 6f 64 65 20 69 6e 74 6f 20 22  sert code into "
2a8c9 76 22 20 74 68 61 74 20 77 69 6c 6c 20 70 75 73  v" that will pus
2a8ca 68 20 74 68 65 20 72 65 63 6f 72 64 20 6f 6e 20  h the record on 
2a8cb 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 0a 2a  the top of the.*
2a8cc 2a 20 73 74 61 63 6b 20 69 6e 74 6f 20 74 68 65  * stack into the
2a8cd 20 73 6f 72 74 65 72 2e 0a 2a 2f 0a 73 74 61 74   sorter..*/.stat
2a8ce 69 63 20 76 6f 69 64 20 70 75 73 68 4f 6e 74 6f  ic void pushOnto
2a8cf 53 6f 72 74 65 72 28 0a 20 20 50 61 72 73 65 20  Sorter(.  Parse 
2a8d0 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2a8d1 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65   /* Parser conte
2a8d2 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
2a8d3 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 2f   *pOrderBy,    /
2a8d4 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
2a8d5 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63  lause */.  Selec
2a8d6 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20  t *pSelect,     
2a8d7 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 53    /* The whole S
2a8d8 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2a8d9 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  */.  int regData
2a8da 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2a8db 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
2a8dc 64 61 74 61 20 74 6f 20 62 65 20 73 6f 72 74 65  data to be sorte
2a8dd 64 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  d */.){.  Vdbe *
2a8de 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2a8df 65 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72 20 3d  e;.  int nExpr =
2a8e0 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
2a8e1 3b 0a 20 20 69 6e 74 20 72 65 67 42 61 73 65 20  ;.  int regBase 
2a8e2 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2a8e3 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 45  Range(pParse, nE
2a8e4 78 70 72 2b 32 29 3b 0a 20 20 69 6e 74 20 72 65  xpr+2);.  int re
2a8e5 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 65  gRecord = sqlite
2a8e6 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2a8e7 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  se);.  sqlite3Ex
2a8e8 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61  prCacheClear(pPa
2a8e9 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  rse);.  sqlite3E
2a8ea 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28  xprCodeExprList(
2a8eb 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79  pParse, pOrderBy
2a8ec 2c 20 72 65 67 42 61 73 65 2c 20 30 29 3b 0a 20  , regBase, 0);. 
2a8ed 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a8ee 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65 6e 63  p2(v, OP_Sequenc
2a8ef 65 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43  e, pOrderBy->iEC
2a8f0 75 72 73 6f 72 2c 20 72 65 67 42 61 73 65 2b 6e  ursor, regBase+n
2a8f1 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  Expr);.  sqlite3
2a8f2 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61  ExprCodeMove(pPa
2a8f3 72 73 65 2c 20 72 65 67 44 61 74 61 2c 20 72 65  rse, regData, re
2a8f4 67 42 61 73 65 2b 6e 45 78 70 72 2b 31 2c 20 31  gBase+nExpr+1, 1
2a8f5 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2a8f6 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
2a8f7 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73 65  eRecord, regBase
2a8f8 2c 20 6e 45 78 70 72 20 2b 20 32 2c 20 72 65 67  , nExpr + 2, reg
2a8f9 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74  Record);.  sqlit
2a8fa 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2a8fb 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 4f  OP_IdxInsert, pO
2a8fc 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72  rderBy->iECursor
2a8fd 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
2a8fe 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2a8ff 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
2a900 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69  gRecord);.  sqli
2a901 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
2a902 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42  nge(pParse, regB
2a903 61 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b 0a 20  ase, nExpr+2);. 
2a904 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 69 4c   if( pSelect->iL
2a905 69 6d 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  imit ){.    int 
2a906 61 64 64 72 31 2c 20 61 64 64 72 32 3b 0a 20 20  addr1, addr2;.  
2a907 20 20 69 6e 74 20 69 4c 69 6d 69 74 3b 0a 20 20    int iLimit;.  
2a908 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 69    if( pSelect->i
2a909 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20  Offset ){.      
2a90a 69 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74  iLimit = pSelect
2a90b 2d 3e 69 4f 66 66 73 65 74 2b 31 3b 0a 20 20 20  ->iOffset+1;.   
2a90c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 4c   }else{.      iL
2a90d 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  imit = pSelect->
2a90e 69 4c 69 6d 69 74 3b 0a 20 20 20 20 7d 0a 20 20  iLimit;.    }.  
2a90f 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
2a910 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2a911 50 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69 74  P_IfZero, iLimit
2a912 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2a913 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
2a914 64 64 49 6d 6d 2c 20 69 4c 69 6d 69 74 2c 20 2d  ddImm, iLimit, -
2a915 31 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d 20  1);.    addr2 = 
2a916 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a917 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20  0(v, OP_Goto);. 
2a918 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
2a919 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29  mpHere(v, addr1)
2a91a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2a91b 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4c 61  eAddOp1(v, OP_La
2a91c 73 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45  st, pOrderBy->iE
2a91d 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 71 6c  Cursor);.    sql
2a91e 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2a91f 2c 20 4f 50 5f 44 65 6c 65 74 65 2c 20 70 4f 72  , OP_Delete, pOr
2a920 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 29  derBy->iECursor)
2a921 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2a922 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
2a923 72 32 29 3b 0a 20 20 20 20 70 53 65 6c 65 63 74  r2);.    pSelect
2a924 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->iLimit = 0;.  
2a925 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63  }.}../*.** Add c
2a926 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
2a927 20 74 68 65 20 4f 46 46 53 45 54 0a 2a 2f 0a 73   the OFFSET.*/.s
2a928 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 4f  tatic void codeO
2a929 66 66 73 65 74 28 0a 20 20 56 64 62 65 20 2a 76  ffset(.  Vdbe *v
2a92a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65  ,          /* Ge
2a92b 6e 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f  nerate code into
2a92c 20 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 53 65   this VM */.  Se
2a92d 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
2a92e 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
2a92f 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f  atement being co
2a930 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  ded */.  int iCo
2a931 6e 74 69 6e 75 65 20 20 20 20 20 2f 2a 20 4a 75  ntinue     /* Ju
2a932 6d 70 20 68 65 72 65 20 74 6f 20 73 6b 69 70 20  mp here to skip 
2a933 74 68 65 20 63 75 72 72 65 6e 74 20 72 65 63 6f  the current reco
2a934 72 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70  rd */.){.  if( p
2a935 2d 3e 69 4f 66 66 73 65 74 20 26 26 20 69 43 6f  ->iOffset && iCo
2a936 6e 74 69 6e 75 65 21 3d 30 20 29 7b 0a 20 20 20  ntinue!=0 ){.   
2a937 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 73   int addr;.    s
2a938 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2a939 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70  (v, OP_AddImm, p
2a93a 2d 3e 69 4f 66 66 73 65 74 2c 20 2d 31 29 3b 0a  ->iOffset, -1);.
2a93b 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
2a93c 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2a93d 4f 50 5f 49 66 4e 65 67 2c 20 70 2d 3e 69 4f 66  OP_IfNeg, p->iOf
2a93e 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  fset);.    sqlit
2a93f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2a940 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43 6f 6e  OP_Goto, 0, iCon
2a941 74 69 6e 75 65 29 3b 0a 20 20 20 20 56 64 62 65  tinue);.    Vdbe
2a942 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73 6b 69  Comment((v, "ski
2a943 70 20 4f 46 46 53 45 54 20 72 65 63 6f 72 64 73  p OFFSET records
2a944 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  "));.    sqlite3
2a945 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2a946 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  addr);.  }.}../*
2a947 0a 2a 2a 20 41 64 64 20 63 6f 64 65 20 74 68 61  .** Add code tha
2a948 74 20 77 69 6c 6c 20 63 68 65 63 6b 20 74 6f 20  t will check to 
2a949 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 4e 20  make sure the N 
2a94a 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69  registers starti
2a94b 6e 67 20 61 74 20 69 4d 65 6d 0a 2a 2a 20 66 6f  ng at iMem.** fo
2a94c 72 6d 20 61 20 64 69 73 74 69 6e 63 74 20 65 6e  rm a distinct en
2a94d 74 72 79 2e 20 20 69 54 61 62 20 69 73 20 61 20  try.  iTab is a 
2a94e 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74 68  sorting index th
2a94f 61 74 20 68 6f 6c 64 73 20 70 72 65 76 69 6f 75  at holds previou
2a950 73 6c 79 0a 2a 2a 20 73 65 65 6e 20 63 6f 6d 62  sly.** seen comb
2a951 69 6e 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  inations of the 
2a952 4e 20 76 61 6c 75 65 73 2e 20 20 41 20 6e 65 77  N values.  A new
2a953 20 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69   entry is made i
2a954 6e 20 69 54 61 62 0a 2a 2a 20 69 66 20 74 68 65  n iTab.** if the
2a955 20 63 75 72 72 65 6e 74 20 4e 20 76 61 6c 75 65   current N value
2a956 73 20 61 72 65 20 6e 65 77 2e 0a 2a 2a 0a 2a 2a  s are new..**.**
2a957 20 41 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 52   A jump to addrR
2a958 65 70 65 61 74 20 69 73 20 6d 61 64 65 20 61 6e  epeat is made an
2a959 64 20 74 68 65 20 4e 2b 31 20 76 61 6c 75 65 73  d the N+1 values
2a95a 20 61 72 65 20 70 6f 70 70 65 64 20 66 72 6f 6d   are popped from
2a95b 20 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 69 66   the.** stack if
2a95c 20 74 68 65 20 74 6f 70 20 4e 20 65 6c 65 6d 65   the top N eleme
2a95d 6e 74 73 20 61 72 65 20 6e 6f 74 20 64 69 73 74  nts are not dist
2a95e 69 6e 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  inct..*/.static 
2a95f 76 6f 69 64 20 63 6f 64 65 44 69 73 74 69 6e 63  void codeDistinc
2a960 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
2a961 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
2a962 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  ng and code gene
2a963 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
2a964 2f 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20 20  /.  int iTab,   
2a965 20 20 20 20 20 20 20 2f 2a 20 41 20 73 6f 72 74         /* A sort
2a966 69 6e 67 20 69 6e 64 65 78 20 75 73 65 64 20 74  ing index used t
2a967 6f 20 74 65 73 74 20 66 6f 72 20 64 69 73 74 69  o test for disti
2a968 6e 63 74 6e 65 73 73 20 2a 2f 0a 20 20 69 6e 74  nctness */.  int
2a969 20 61 64 64 72 52 65 70 65 61 74 2c 20 20 20 20   addrRepeat,    
2a96a 2f 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20  /* Jump to here 
2a96b 69 66 20 6e 6f 74 20 64 69 73 74 69 6e 63 74 20  if not distinct 
2a96c 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 20  */.  int N,     
2a96d 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2a96e 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 2a 2f  r of elements */
2a96f 0a 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20  .  int iMem     
2a970 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65        /* First e
2a971 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56  lement */.){.  V
2a972 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 72 31  dbe *v;.  int r1
2a973 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d  ;..  v = pParse-
2a974 3e 70 56 64 62 65 3b 0a 20 20 72 31 20 3d 20 73  >pVdbe;.  r1 = s
2a975 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2a976 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69  (pParse);.  sqli
2a977 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74  te3VdbeAddOp4Int
2a978 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 69 54  (v, OP_Found, iT
2a979 61 62 2c 20 61 64 64 72 52 65 70 65 61 74 2c 20  ab, addrRepeat, 
2a97a 69 4d 65 6d 2c 20 4e 29 3b 0a 20 20 73 71 6c 69  iMem, N);.  sqli
2a97b 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2a97c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
2a97d 69 4d 65 6d 2c 20 4e 2c 20 72 31 29 3b 0a 20 20  iMem, N, r1);.  
2a97e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a97f 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  2(v, OP_IdxInser
2a980 74 2c 20 69 54 61 62 2c 20 72 31 29 3b 0a 20 20  t, iTab, r1);.  
2a981 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2a982 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
2a983 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  );.}../*.** Gene
2a984 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6d 65  rate an error me
2a985 73 73 61 67 65 20 77 68 65 6e 20 61 20 53 45 4c  ssage when a SEL
2a986 45 43 54 20 69 73 20 75 73 65 64 20 77 69 74 68  ECT is used with
2a987 69 6e 20 61 20 73 75 62 65 78 70 72 65 73 73 69  in a subexpressi
2a988 6f 6e 0a 2a 2a 20 28 65 78 61 6d 70 6c 65 3a 20  on.** (example: 
2a989 20 22 61 20 49 4e 20 28 53 45 4c 45 43 54 20 2a   "a IN (SELECT *
2a98a 20 46 52 4f 4d 20 74 61 62 6c 65 29 22 29 20 62   FROM table)") b
2a98b 75 74 20 69 74 20 68 61 73 20 6d 6f 72 65 20 74  ut it has more t
2a98c 68 61 6e 20 31 20 72 65 73 75 6c 74 0a 2a 2a 20  han 1 result.** 
2a98d 63 6f 6c 75 6d 6e 2e 20 20 57 65 20 64 6f 20 74  column.  We do t
2a98e 68 69 73 20 69 6e 20 61 20 73 75 62 72 6f 75 74  his in a subrout
2a98f 69 6e 65 20 62 65 63 61 75 73 65 20 74 68 65 20  ine because the 
2a990 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20  error occurs in 
2a991 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 70 6c 61 63  multiple.** plac
2a992 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
2a993 74 20 63 68 65 63 6b 46 6f 72 4d 75 6c 74 69 43  t checkForMultiC
2a994 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72 72 6f 72  olumnSelectError
2a995 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2a996 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  e,       /* Pars
2a997 65 20 63 6f 6e 74 65 78 74 2e 20 2a 2f 0a 20 20  e context. */.  
2a998 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
2a999 74 2c 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74  t,   /* Destinat
2a99a 69 6f 6e 20 6f 66 20 53 45 4c 45 43 54 20 72 65  ion of SELECT re
2a99b 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  sults */.  int n
2a99c 45 78 70 72 20 20 20 20 20 20 20 20 20 20 20 20  Expr            
2a99d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 73  /* Number of res
2a99e 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 72 65 74 75  ult columns retu
2a99f 72 6e 65 64 20 62 79 20 53 45 4c 45 43 54 20 2a  rned by SELECT *
2a9a0 2f 0a 29 7b 0a 20 20 69 6e 74 20 65 44 65 73 74  /.){.  int eDest
2a9a1 20 3d 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3b   = pDest->eDest;
2a9a2 0a 20 20 69 66 28 20 6e 45 78 70 72 3e 31 20 26  .  if( nExpr>1 &
2a9a3 26 20 28 65 44 65 73 74 3d 3d 53 52 54 5f 4d 65  & (eDest==SRT_Me
2a9a4 6d 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f  m || eDest==SRT_
2a9a5 53 65 74 29 20 29 7b 0a 20 20 20 20 73 71 6c 69  Set) ){.    sqli
2a9a6 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2a9a7 73 65 2c 20 22 6f 6e 6c 79 20 61 20 73 69 6e 67  se, "only a sing
2a9a8 6c 65 20 72 65 73 75 6c 74 20 61 6c 6c 6f 77 65  le result allowe
2a9a9 64 20 66 6f 72 20 22 0a 20 20 20 20 20 20 20 22  d for ".       "
2a9aa 61 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 73  a SELECT that is
2a9ab 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78 70 72   part of an expr
2a9ac 65 73 73 69 6f 6e 22 29 3b 0a 20 20 20 20 72 65  ession");.    re
2a9ad 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  turn 1;.  }else{
2a9ae 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2a9af 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
2a9b0 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
2a9b1 65 73 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20  es the code for 
2a9b2 74 68 65 20 69 6e 73 69 64 65 20 6f 66 20 74 68  the inside of th
2a9b3 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a 20  e inner loop.** 
2a9b4 6f 66 20 61 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a  of a SELECT..**.
2a9b5 2a 2a 20 49 66 20 73 72 63 54 61 62 20 61 6e 64  ** If srcTab and
2a9b6 20 6e 43 6f 6c 75 6d 6e 20 61 72 65 20 62 6f 74   nColumn are bot
2a9b7 68 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  h zero, then the
2a9b8 20 70 45 4c 69 73 74 20 65 78 70 72 65 73 73 69   pEList expressi
2a9b9 6f 6e 73 0a 2a 2a 20 61 72 65 20 65 76 61 6c 75  ons.** are evalu
2a9ba 61 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f  ated in order to
2a9bb 20 67 65 74 20 74 68 65 20 64 61 74 61 20 66 6f   get the data fo
2a9bc 72 20 74 68 69 73 20 72 6f 77 2e 20 20 49 66 20  r this row.  If 
2a9bd 6e 43 6f 6c 75 6d 6e 3e 30 0a 2a 2a 20 74 68 65  nColumn>0.** the
2a9be 6e 20 64 61 74 61 20 69 73 20 70 75 6c 6c 65 64  n data is pulled
2a9bf 20 66 72 6f 6d 20 73 72 63 54 61 62 20 61 6e 64   from srcTab and
2a9c0 20 70 45 4c 69 73 74 20 69 73 20 75 73 65 64 20   pEList is used 
2a9c1 6f 6e 6c 79 20 74 6f 20 67 65 74 20 74 68 65 0a  only to get the.
2a9c2 2a 2a 20 64 61 74 61 74 79 70 65 73 20 66 6f 72  ** datatypes for
2a9c3 20 65 61 63 68 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f   each column..*/
2a9c4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 6c  .static void sel
2a9c5 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 0a 20 20  ectInnerLoop(.  
2a9c6 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2a9c7 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2a9c8 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2a9c9 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
2a9ca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2a9cb 65 20 63 6f 6d 70 6c 65 74 65 20 73 65 6c 65 63  e complete selec
2a9cc 74 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e  t statement bein
2a9cd 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20 45 78 70  g coded */.  Exp
2a9ce 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20  rList *pEList,  
2a9cf 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
2a9d0 76 61 6c 75 65 73 20 62 65 69 6e 67 20 65 78 74  values being ext
2a9d1 72 61 63 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  racted */.  int 
2a9d2 73 72 63 54 61 62 2c 20 20 20 20 20 20 20 20 20  srcTab,         
2a9d3 20 20 20 20 2f 2a 20 50 75 6c 6c 20 64 61 74 61      /* Pull data
2a9d4 20 66 72 6f 6d 20 74 68 69 73 20 74 61 62 6c 65   from this table
2a9d5 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d   */.  int nColum
2a9d6 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  n,            /*
2a9d7 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
2a9d8 6e 73 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65  ns in the source
2a9d9 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72   table */.  Expr
2a9da 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
2a9db 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20 4e 55      /* If not NU
2a9dc 4c 4c 2c 20 73 6f 72 74 20 72 65 73 75 6c 74 73  LL, sort results
2a9dd 20 75 73 69 6e 67 20 74 68 69 73 20 6b 65 79 20   using this key 
2a9de 2a 2f 0a 20 20 69 6e 74 20 64 69 73 74 69 6e 63  */.  int distinc
2a9df 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t,           /* 
2a9e0 49 66 20 3e 3d 30 2c 20 6d 61 6b 65 20 73 75 72  If >=0, make sur
2a9e1 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 64 69  e results are di
2a9e2 73 74 69 6e 63 74 20 2a 2f 0a 20 20 53 65 6c 65  stinct */.  Sele
2a9e3 63 74 44 65 73 74 20 2a 70 44 65 73 74 2c 20 20  ctDest *pDest,  
2a9e4 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69      /* How to di
2a9e5 73 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73  spose of the res
2a9e6 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ults */.  int iC
2a9e7 6f 6e 74 69 6e 75 65 2c 20 20 20 20 20 20 20 20  ontinue,        
2a9e8 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
2a9e9 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
2a9ea 6e 65 78 74 20 72 6f 77 20 2a 2f 0a 20 20 69 6e  next row */.  in
2a9eb 74 20 69 42 72 65 61 6b 20 20 20 20 20 20 20 20  t iBreak        
2a9ec 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
2a9ed 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20  re to break out 
2a9ee 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f  of the inner loo
2a9ef 70 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  p */.){.  Vdbe *
2a9f0 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2a9f1 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  e;.  int i;.  in
2a9f2 74 20 68 61 73 44 69 73 74 69 6e 63 74 3b 20 20  t hasDistinct;  
2a9f3 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2a9f4 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65   the DISTINCT ke
2a9f5 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74  yword is present
2a9f6 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 65 73   */.  int regRes
2a9f7 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ult;            
2a9f8 20 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 6d 65    /* Start of me
2a9f9 6d 6f 72 79 20 68 6f 6c 64 69 6e 67 20 72 65 73  mory holding res
2a9fa 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 69 6e 74  ult set */.  int
2a9fb 20 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d 3e   eDest = pDest->
2a9fc 65 44 65 73 74 3b 20 20 20 2f 2a 20 48 6f 77 20  eDest;   /* How 
2a9fd 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 72 65  to dispose of re
2a9fe 73 75 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 69  sults */.  int i
2a9ff 50 61 72 6d 20 3d 20 70 44 65 73 74 2d 3e 69 50  Parm = pDest->iP
2aa00 61 72 6d 3b 20 20 20 2f 2a 20 46 69 72 73 74 20  arm;   /* First 
2aa01 61 72 67 75 6d 65 6e 74 20 74 6f 20 64 69 73 70  argument to disp
2aa02 6f 73 61 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  osal method */. 
2aa03 20 69 6e 74 20 6e 52 65 73 75 6c 74 43 6f 6c 3b   int nResultCol;
2aa04 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2aa05 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74  Number of result
2aa06 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 0a 20 20 61   columns */..  a
2aa07 73 73 65 72 74 28 20 76 20 29 3b 0a 20 20 69 66  ssert( v );.  if
2aa08 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20  ( NEVER(v==0) ) 
2aa09 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
2aa0a 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20  ( pEList!=0 );. 
2aa0b 20 68 61 73 44 69 73 74 69 6e 63 74 20 3d 20 64   hasDistinct = d
2aa0c 69 73 74 69 6e 63 74 3e 3d 30 3b 0a 20 20 69 66  istinct>=0;.  if
2aa0d 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 26 26  ( pOrderBy==0 &&
2aa0e 20 21 68 61 73 44 69 73 74 69 6e 63 74 20 29 7b   !hasDistinct ){
2aa0f 0a 20 20 20 20 63 6f 64 65 4f 66 66 73 65 74 28  .    codeOffset(
2aa10 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75 65 29  v, p, iContinue)
2aa11 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 75 6c 6c  ;.  }..  /* Pull
2aa12 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63   the requested c
2aa13 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69  olumns..  */.  i
2aa14 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a  f( nColumn>0 ){.
2aa15 20 20 20 20 6e 52 65 73 75 6c 74 43 6f 6c 20 3d      nResultCol =
2aa16 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 7d 65 6c 73   nColumn;.  }els
2aa17 65 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 43 6f  e{.    nResultCo
2aa18 6c 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  l = pEList->nExp
2aa19 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 65  r;.  }.  if( pDe
2aa1a 73 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29 7b 0a 20  st->iMem==0 ){. 
2aa1b 20 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20 3d     pDest->iMem =
2aa1c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b   pParse->nMem+1;
2aa1d 0a 20 20 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d  .    pDest->nMem
2aa1e 20 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a 20   = nResultCol;. 
2aa1f 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20     pParse->nMem 
2aa20 2b 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a 20  += nResultCol;. 
2aa21 20 7d 65 6c 73 65 7b 20 0a 20 20 20 20 61 73 73   }else{ .    ass
2aa22 65 72 74 28 20 70 44 65 73 74 2d 3e 6e 4d 65 6d  ert( pDest->nMem
2aa23 3d 3d 6e 52 65 73 75 6c 74 43 6f 6c 20 29 3b 0a  ==nResultCol );.
2aa24 20 20 7d 0a 20 20 72 65 67 52 65 73 75 6c 74 20    }.  regResult 
2aa25 3d 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3b 0a 20  = pDest->iMem;. 
2aa26 20 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29   if( nColumn>0 )
2aa27 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
2aa28 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  <nColumn; i++){.
2aa29 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2aa2a 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2aa2b 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 69 2c  lumn, srcTab, i,
2aa2c 20 72 65 67 52 65 73 75 6c 74 2b 69 29 3b 0a 20   regResult+i);. 
2aa2d 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
2aa2e 20 65 44 65 73 74 21 3d 53 52 54 5f 45 78 69 73   eDest!=SRT_Exis
2aa2f 74 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  ts ){.    /* If 
2aa30 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
2aa31 69 73 20 61 6e 20 45 58 49 53 54 53 28 2e 2e 2e  is an EXISTS(...
2aa32 29 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68  ) expression, th
2aa33 65 20 61 63 74 75 61 6c 0a 20 20 20 20 2a 2a 20  e actual.    ** 
2aa34 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20  values returned 
2aa35 62 79 20 74 68 65 20 53 45 4c 45 43 54 20 61 72  by the SELECT ar
2aa36 65 20 6e 6f 74 20 72 65 71 75 69 72 65 64 2e 0a  e not required..
2aa37 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
2aa38 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72  e3ExprCacheClear
2aa39 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 73 71  (pParse);.    sq
2aa3a 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70  lite3ExprCodeExp
2aa3b 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 45  rList(pParse, pE
2aa3c 4c 69 73 74 2c 20 72 65 67 52 65 73 75 6c 74 2c  List, regResult,
2aa3d 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70   eDest==SRT_Outp
2aa3e 75 74 29 3b 0a 20 20 7d 0a 20 20 6e 43 6f 6c 75  ut);.  }.  nColu
2aa3f 6d 6e 20 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b  mn = nResultCol;
2aa40 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 44 49  ..  /* If the DI
2aa41 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 77  STINCT keyword w
2aa42 61 73 20 70 72 65 73 65 6e 74 20 6f 6e 20 74 68  as present on th
2aa43 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
2aa44 6e 74 0a 20 20 2a 2a 20 61 6e 64 20 74 68 69 73  nt.  ** and this
2aa45 20 72 6f 77 20 68 61 73 20 62 65 65 6e 20 73 65   row has been se
2aa46 65 6e 20 62 65 66 6f 72 65 2c 20 74 68 65 6e 20  en before, then 
2aa47 64 6f 20 6e 6f 74 20 6d 61 6b 65 20 74 68 69 73  do not make this
2aa48 20 72 6f 77 0a 20 20 2a 2a 20 70 61 72 74 20 6f   row.  ** part o
2aa49 66 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 20 20  f the result..  
2aa4a 2a 2f 0a 20 20 69 66 28 20 68 61 73 44 69 73 74  */.  if( hasDist
2aa4b 69 6e 63 74 20 29 7b 0a 20 20 20 20 61 73 73 65  inct ){.    asse
2aa4c 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b  rt( pEList!=0 );
2aa4d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c  .    assert( pEL
2aa4e 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6e 43 6f 6c  ist->nExpr==nCol
2aa4f 75 6d 6e 20 29 3b 0a 20 20 20 20 63 6f 64 65 44  umn );.    codeD
2aa50 69 73 74 69 6e 63 74 28 70 50 61 72 73 65 2c 20  istinct(pParse, 
2aa51 64 69 73 74 69 6e 63 74 2c 20 69 43 6f 6e 74 69  distinct, iConti
2aa52 6e 75 65 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 65  nue, nColumn, re
2aa53 67 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 69 66  gResult);.    if
2aa54 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 7b  ( pOrderBy==0 ){
2aa55 0a 20 20 20 20 20 20 63 6f 64 65 4f 66 66 73 65  .      codeOffse
2aa56 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75  t(v, p, iContinu
2aa57 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  e);.    }.  }.. 
2aa58 20 69 66 28 20 63 68 65 63 6b 46 6f 72 4d 75 6c   if( checkForMul
2aa59 74 69 43 6f 6c 75 6d 6e 53 65 6c 65 63 74 45 72  tiColumnSelectEr
2aa5a 72 6f 72 28 70 50 61 72 73 65 2c 20 70 44 65 73  ror(pParse, pDes
2aa5b 74 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  t, pEList->nExpr
2aa5c 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
2aa5d 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28 20  .  }..  switch( 
2aa5e 65 44 65 73 74 20 29 7b 0a 20 20 20 20 2f 2a 20  eDest ){.    /* 
2aa5f 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c 20 77 72  In this mode, wr
2aa60 69 74 65 20 65 61 63 68 20 71 75 65 72 79 20 72  ite each query r
2aa61 65 73 75 6c 74 20 74 6f 20 74 68 65 20 6b 65 79  esult to the key
2aa62 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72   of the temporar
2aa63 79 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 69  y.    ** table i
2aa64 50 61 72 6d 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  Parm..    */.#if
2aa65 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2aa66 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
2aa67 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 55 6e  .    case SRT_Un
2aa68 69 6f 6e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  ion: {.      int
2aa69 20 72 31 3b 0a 20 20 20 20 20 20 72 31 20 3d 20   r1;.      r1 = 
2aa6a 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2aa6b 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
2aa6c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2aa6d 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
2aa6e 6f 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  ord, regResult, 
2aa6f 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20 20  nColumn, r1);.  
2aa70 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2aa71 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49  ddOp2(v, OP_IdxI
2aa72 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 31  nsert, iParm, r1
2aa73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2aa74 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2aa75 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20  Parse, r1);.    
2aa76 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
2aa77 20 20 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74      /* Construct
2aa78 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 74   a record from t
2aa79 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 2c  he query result,
2aa7a 20 62 75 74 20 69 6e 73 74 65 61 64 20 6f 66 0a   but instead of.
2aa7b 20 20 20 20 2a 2a 20 73 61 76 69 6e 67 20 74 68      ** saving th
2aa7c 61 74 20 72 65 63 6f 72 64 2c 20 75 73 65 20 69  at record, use i
2aa7d 74 20 61 73 20 61 20 6b 65 79 20 74 6f 20 64 65  t as a key to de
2aa7e 6c 65 74 65 20 65 6c 65 6d 65 6e 74 73 20 66 72  lete elements fr
2aa7f 6f 6d 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 65  om.    ** the te
2aa80 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 69 50  mporary table iP
2aa81 61 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  arm..    */.    
2aa82 63 61 73 65 20 53 52 54 5f 45 78 63 65 70 74 3a  case SRT_Except:
2aa83 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
2aa84 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2aa85 5f 49 64 78 44 65 6c 65 74 65 2c 20 69 50 61 72  _IdxDelete, iPar
2aa86 6d 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43  m, regResult, nC
2aa87 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 62 72  olumn);.      br
2aa88 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2aa89 66 0a 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20  f..    /* Store 
2aa8a 74 68 65 20 72 65 73 75 6c 74 20 61 73 20 64 61  the result as da
2aa8b 74 61 20 75 73 69 6e 67 20 61 20 75 6e 69 71 75  ta using a uniqu
2aa8c 65 20 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  e key..    */.  
2aa8d 20 20 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65    case SRT_Table
2aa8e 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45  :.    case SRT_E
2aa8f 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20  phemTab: {.     
2aa90 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65   int r1 = sqlite
2aa91 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2aa92 73 65 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  se);.      testc
2aa93 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f  ase( eDest==SRT_
2aa94 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 74  Table );.      t
2aa95 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d  estcase( eDest==
2aa96 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 3b 0a  SRT_EphemTab );.
2aa97 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2aa98 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  eAddOp3(v, OP_Ma
2aa99 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73  keRecord, regRes
2aa9a 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31  ult, nColumn, r1
2aa9b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72  );.      if( pOr
2aa9c 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20  derBy ){.       
2aa9d 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28   pushOntoSorter(
2aa9e 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79  pParse, pOrderBy
2aa9f 2c 20 70 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  , p, r1);.      
2aaa0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69  }else{.        i
2aaa1 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47  nt r2 = sqlite3G
2aaa2 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
2aaa3 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2aaa4 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2aaa5 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 69 50 61  OP_NewRowid, iPa
2aaa6 72 6d 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20  rm, r2);.       
2aaa7 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2aaa8 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
2aaa9 20 69 50 61 72 6d 2c 20 72 31 2c 20 72 32 29 3b   iParm, r1, r2);
2aaaa 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2aaab 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20  VdbeChangeP5(v, 
2aaac 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a  OPFLAG_APPEND);.
2aaad 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
2aaae 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2aaaf 61 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 20  arse, r2);.     
2aab0 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2aab1 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
2aab2 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20  Parse, r1);.    
2aab3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
2aab4 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2aab5 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20  MIT_SUBQUERY.   
2aab6 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63 72   /* If we are cr
2aab7 65 61 74 69 6e 67 20 61 20 73 65 74 20 66 6f 72  eating a set for
2aab8 20 61 6e 20 22 65 78 70 72 20 49 4e 20 28 53 45   an "expr IN (SE
2aab9 4c 45 43 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74  LECT ...)" const
2aaba 72 75 63 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65  ruct,.    ** the
2aabb 6e 20 74 68 65 72 65 20 73 68 6f 75 6c 64 20 62  n there should b
2aabc 65 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20  e a single item 
2aabd 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 57  on the stack.  W
2aabe 72 69 74 65 20 74 68 69 73 0a 20 20 20 20 2a 2a  rite this.    **
2aabf 20 69 74 65 6d 20 69 6e 74 6f 20 74 68 65 20 73   item into the s
2aac0 65 74 20 74 61 62 6c 65 20 77 69 74 68 20 62 6f  et table with bo
2aac1 67 75 73 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f  gus data..    */
2aac2 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 53 65  .    case SRT_Se
2aac3 74 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t: {.      asser
2aac4 74 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b  t( nColumn==1 );
2aac5 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 69 6e 69  .      p->affini
2aac6 74 79 20 3d 20 73 71 6c 69 74 65 33 43 6f 6d 70  ty = sqlite3Comp
2aac7 61 72 65 41 66 66 69 6e 69 74 79 28 70 45 4c 69  areAffinity(pELi
2aac8 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20  st->a[0].pExpr, 
2aac9 70 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 29  pDest->affinity)
2aaca 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 64  ;.      if( pOrd
2aacb 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20  erBy ){.        
2aacc 2f 2a 20 41 74 20 66 69 72 73 74 20 67 6c 61 6e  /* At first glan
2aacd 63 65 20 79 6f 75 20 77 6f 75 6c 64 20 74 68 69  ce you would thi
2aace 6e 6b 20 77 65 20 63 6f 75 6c 64 20 6f 70 74 69  nk we could opti
2aacf 6d 69 7a 65 20 6f 75 74 20 74 68 65 0a 20 20 20  mize out the.   
2aad0 20 20 20 20 20 2a 2a 20 4f 52 44 45 52 20 42 59       ** ORDER BY
2aad1 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 73 69   in this case si
2aad2 6e 63 65 20 74 68 65 20 6f 72 64 65 72 20 6f 66  nce the order of
2aad3 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20   entries in the 
2aad4 73 65 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 64  set.        ** d
2aad5 6f 65 73 20 6e 6f 74 20 6d 61 74 74 65 72 2e 20  oes not matter. 
2aad6 20 42 75 74 20 74 68 65 72 65 20 6d 69 67 68 74   But there might
2aad7 20 62 65 20 61 20 4c 49 4d 49 54 20 63 6c 61 75   be a LIMIT clau
2aad8 73 65 2c 20 69 6e 20 77 68 69 63 68 0a 20 20 20  se, in which.   
2aad9 20 20 20 20 20 2a 2a 20 63 61 73 65 20 74 68 65       ** case the
2aada 20 6f 72 64 65 72 20 64 6f 65 73 20 6d 61 74 74   order does matt
2aadb 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 75  er */.        pu
2aadc 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61  shOntoSorter(pPa
2aadd 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70  rse, pOrderBy, p
2aade 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20  , regResult);.  
2aadf 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2aae0 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69     int r1 = sqli
2aae1 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2aae2 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73  arse);.        s
2aae3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2aae4 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  (v, OP_MakeRecor
2aae5 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 31 2c  d, regResult, 1,
2aae6 20 72 31 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74   r1, &p->affinit
2aae7 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73  y, 1);.        s
2aae8 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
2aae9 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
2aaea 61 72 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c  arse, regResult,
2aaeb 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
2aaec 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2aaed 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20  , OP_IdxInsert, 
2aaee 69 50 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20  iParm, r1);.    
2aaef 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
2aaf0 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2aaf1 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  , r1);.      }. 
2aaf2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2aaf3 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e 79  }..    /* If any
2aaf4 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74 68   row exist in th
2aaf5 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72 65  e result set, re
2aaf6 63 6f 72 64 20 74 68 61 74 20 66 61 63 74 20 61  cord that fact a
2aaf7 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a 2f  nd abort..    */
2aaf8 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78  .    case SRT_Ex
2aaf9 69 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73 71  ists: {.      sq
2aafa 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2aafb 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
2aafc 2c 20 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20  , iParm);.      
2aafd 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61  /* The LIMIT cla
2aafe 75 73 65 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61  use will termina
2aaff 74 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20  te the loop for 
2ab00 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61  us */.      brea
2ab01 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
2ab02 20 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63   If this is a sc
2ab03 61 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74  alar select that
2ab04 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65   is part of an e
2ab05 78 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a  xpression, then.
2ab06 20 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65      ** store the
2ab07 20 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20   results in the 
2ab08 61 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f  appropriate memo
2ab09 72 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61  ry cell and brea
2ab0a 6b 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20  k out.    ** of 
2ab0b 74 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20  the scan loop.. 
2ab0c 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
2ab0d 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20  RT_Mem: {.      
2ab0e 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d  assert( nColumn=
2ab0f 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =1 );.      if( 
2ab10 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2ab11 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74      pushOntoSort
2ab12 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65  er(pParse, pOrde
2ab13 72 42 79 2c 20 70 2c 20 72 65 67 52 65 73 75 6c  rBy, p, regResul
2ab14 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  t);.      }else{
2ab15 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2ab16 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61  ExprCodeMove(pPa
2ab17 72 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  rse, regResult, 
2ab18 69 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20 20  iParm, 1);.     
2ab19 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20     /* The LIMIT 
2ab1a 63 6c 61 75 73 65 20 77 69 6c 6c 20 6a 75 6d 70  clause will jump
2ab1b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70   out of the loop
2ab1c 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20 20   for us */.     
2ab1d 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
2ab1e 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20      }.#endif /* 
2ab1f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2ab20 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a  MIT_SUBQUERY */.
2ab21 0a 20 20 20 20 2f 2a 20 53 65 6e 64 20 74 68 65  .    /* Send the
2ab22 20 64 61 74 61 20 74 6f 20 74 68 65 20 63 61 6c   data to the cal
2ab23 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 6f  lback function o
2ab24 72 20 74 6f 20 61 20 73 75 62 72 6f 75 74 69 6e  r to a subroutin
2ab25 65 2e 20 20 49 6e 20 74 68 65 0a 20 20 20 20 2a  e.  In the.    *
2ab26 2a 20 63 61 73 65 20 6f 66 20 61 20 73 75 62 72  * case of a subr
2ab27 6f 75 74 69 6e 65 2c 20 74 68 65 20 73 75 62 72  outine, the subr
2ab28 6f 75 74 69 6e 65 20 69 74 73 65 6c 66 20 69 73  outine itself is
2ab29 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
2ab2a 0a 20 20 20 20 2a 2a 20 70 6f 70 70 69 6e 67 20  .    ** popping 
2ab2b 74 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68  the data from th
2ab2c 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a  e stack..    */.
2ab2d 20 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72      case SRT_Cor
2ab2e 6f 75 74 69 6e 65 3a 0a 20 20 20 20 63 61 73 65  outine:.    case
2ab2f 20 53 52 54 5f 4f 75 74 70 75 74 3a 20 7b 0a 20   SRT_Output: {. 
2ab30 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65       testcase( e
2ab31 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74  Dest==SRT_Corout
2ab32 69 6e 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ine );.      tes
2ab33 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52  tcase( eDest==SR
2ab34 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20 20 20 20  T_Output );.    
2ab35 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29    if( pOrderBy )
2ab36 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31  {.        int r1
2ab37 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2ab38 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2ab39 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2ab3a 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  eAddOp3(v, OP_Ma
2ab3b 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73  keRecord, regRes
2ab3c 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31  ult, nColumn, r1
2ab3d 29 3b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f  );.        pushO
2ab3e 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65  ntoSorter(pParse
2ab3f 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72  , pOrderBy, p, r
2ab40 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
2ab41 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2ab42 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20  g(pParse, r1);. 
2ab43 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65       }else if( e
2ab44 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74  Dest==SRT_Corout
2ab45 69 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ine ){.        s
2ab46 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2ab47 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44  (v, OP_Yield, pD
2ab48 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20  est->iParm);.   
2ab49 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2ab4a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ab4b 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
2ab4c 52 6f 77 2c 20 72 65 67 52 65 73 75 6c 74 2c 20  Row, regResult, 
2ab4d 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20  nColumn);.      
2ab4e 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2ab4f 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
2ab50 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 73 75  (pParse, regResu
2ab51 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  lt, nColumn);.  
2ab52 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
2ab53 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 21 64  k;.    }..#if !d
2ab54 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2ab55 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20  IT_TRIGGER).    
2ab56 2f 2a 20 44 69 73 63 61 72 64 20 74 68 65 20 72  /* Discard the r
2ab57 65 73 75 6c 74 73 2e 20 20 54 68 69 73 20 69 73  esults.  This is
2ab58 20 75 73 65 64 20 66 6f 72 20 53 45 4c 45 43 54   used for SELECT
2ab59 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 69   statements insi
2ab5a 64 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 62 6f  de.    ** the bo
2ab5b 64 79 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e  dy of a TRIGGER.
2ab5c 20 20 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66    The purpose of
2ab5d 20 73 75 63 68 20 73 65 6c 65 63 74 73 20 69 73   such selects is
2ab5e 20 74 6f 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20   to call.    ** 
2ab5f 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e  user-defined fun
2ab60 63 74 69 6f 6e 73 20 74 68 61 74 20 68 61 76 65  ctions that have
2ab61 20 73 69 64 65 20 65 66 66 65 63 74 73 2e 20 20   side effects.  
2ab62 57 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 0a 20  We do not care. 
2ab63 20 20 20 2a 2a 20 61 62 6f 75 74 20 74 68 65 20     ** about the 
2ab64 61 63 74 75 61 6c 20 72 65 73 75 6c 74 73 20 6f  actual results o
2ab65 66 20 74 68 65 20 73 65 6c 65 63 74 2e 0a 20 20  f the select..  
2ab66 20 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74    */.    default
2ab67 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
2ab68 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 44 69 73  ( eDest==SRT_Dis
2ab69 63 61 72 64 20 29 3b 0a 20 20 20 20 20 20 62 72  card );.      br
2ab6a 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2ab6b 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70  f.  }..  /* Jump
2ab6c 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
2ab6d 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65 20 4c  he loop if the L
2ab6e 49 4d 49 54 20 69 73 20 72 65 61 63 68 65 64 2e  IMIT is reached.
2ab6f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69  .  */.  if( p->i
2ab70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 61 73 73  Limit ){.    ass
2ab71 65 72 74 28 20 70 4f 72 64 65 72 42 79 3d 3d 30  ert( pOrderBy==0
2ab72 20 29 3b 20 20 2f 2a 20 49 66 20 74 68 65 72 65   );  /* If there
2ab73 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 2c   is an ORDER BY,
2ab74 20 74 68 65 20 63 61 6c 6c 20 74 6f 0a 20 20 20   the call to.   
2ab75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ab76 20 20 20 20 20 20 20 20 20 2a 2a 20 70 75 73 68           ** push
2ab77 4f 6e 74 6f 53 6f 72 74 65 72 28 29 20 77 6f 75  OntoSorter() wou
2ab78 6c 64 20 68 61 76 65 20 63 6c 65 61 72 65 64 20  ld have cleared 
2ab79 70 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 20  p->iLimit */.   
2ab7a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ab7b 70 33 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c  p3(v, OP_IfZero,
2ab7c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69 42 72 65   p->iLimit, iBre
2ab7d 61 6b 2c 20 2d 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  ak, -1);.  }.}..
2ab7e 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 65  /*.** Given an e
2ab7f 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20  xpression list, 
2ab80 67 65 6e 65 72 61 74 65 20 61 20 4b 65 79 49 6e  generate a KeyIn
2ab81 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61  fo structure tha
2ab82 74 20 72 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65  t records.** the
2ab83 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2ab84 6e 63 65 20 66 6f 72 20 65 61 63 68 20 65 78 70  nce for each exp
2ab85 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 61 74 20  ression in that 
2ab86 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e  expression list.
2ab87 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78  .**.** If the Ex
2ab88 70 72 4c 69 73 74 20 69 73 20 61 6e 20 4f 52 44  prList is an ORD
2ab89 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42  ER BY or GROUP B
2ab8a 59 20 63 6c 61 75 73 65 20 74 68 65 6e 20 74 68  Y clause then th
2ab8b 65 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20 4b  e resulting.** K
2ab8c 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
2ab8d 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20   is appropriate 
2ab8e 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  for initializing
2ab8f 20 61 20 76 69 72 74 75 61 6c 20 69 6e 64 65 78   a virtual index
2ab90 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74   to.** implement
2ab91 20 74 68 61 74 20 63 6c 61 75 73 65 2e 20 20 49   that clause.  I
2ab92 66 20 74 68 65 20 45 78 70 72 4c 69 73 74 20 69  f the ExprList i
2ab93 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  s the result set
2ab94 20 6f 66 20 61 20 53 45 4c 45 43 54 0a 2a 2a 20   of a SELECT.** 
2ab95 74 68 65 6e 20 74 68 65 20 4b 65 79 49 6e 66 6f  then the KeyInfo
2ab96 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70   structure is ap
2ab97 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 69 6e  propriate for in
2ab98 69 74 69 61 6c 69 7a 69 6e 67 20 61 20 76 69 72  itializing a vir
2ab99 74 75 61 6c 0a 2a 2a 20 69 6e 64 65 78 20 74 6f  tual.** index to
2ab9a 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 44 49 53   implement a DIS
2ab9b 54 49 4e 43 54 20 74 65 73 74 2e 0a 2a 2a 0a 2a  TINCT test..**.*
2ab9c 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
2ab9d 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  the KeyInfo stru
2ab9e 63 74 75 72 65 20 69 73 20 6f 62 74 61 69 6e 20  cture is obtain 
2ab9f 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 20 20 54 68  from malloc.  Th
2aba0 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e  e calling.** fun
2aba1 63 74 69 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73  ction is respons
2aba2 69 62 6c 65 20 66 6f 72 20 73 65 65 69 6e 67 20  ible for seeing 
2aba3 74 68 61 74 20 74 68 69 73 20 73 74 72 75 63 74  that this struct
2aba4 75 72 65 20 69 73 20 65 76 65 6e 74 75 61 6c 6c  ure is eventuall
2aba5 79 0a 2a 2a 20 66 72 65 65 64 2e 20 20 41 64 64  y.** freed.  Add
2aba6 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72   the KeyInfo str
2aba7 75 63 74 75 72 65 20 74 6f 20 74 68 65 20 50 34  ucture to the P4
2aba8 20 66 69 65 6c 64 20 6f 66 20 61 6e 20 6f 70 63   field of an opc
2aba9 6f 64 65 20 75 73 69 6e 67 0a 2a 2a 20 50 34 5f  ode using.** P4_
2abaa 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20  KEYINFO_HANDOFF 
2abab 69 73 20 74 68 65 20 75 73 75 61 6c 20 77 61 79  is the usual way
2abac 20 6f 66 20 64 65 61 6c 69 6e 67 20 77 69 74 68   of dealing with
2abad 20 74 68 69 73 2e 0a 2a 2f 0a 73 74 61 74 69 63   this..*/.static
2abae 20 4b 65 79 49 6e 66 6f 20 2a 6b 65 79 49 6e 66   KeyInfo *keyInf
2abaf 6f 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 50 61  oFromExprList(Pa
2abb0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
2abb1 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20  rList *pList){. 
2abb2 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2abb3 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
2abb4 20 6e 45 78 70 72 3b 0a 20 20 4b 65 79 49 6e 66   nExpr;.  KeyInf
2abb5 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 73 74 72 75  o *pInfo;.  stru
2abb6 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
2abb7 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69   *pItem;.  int i
2abb8 3b 0a 0a 20 20 6e 45 78 70 72 20 3d 20 70 4c 69  ;..  nExpr = pLi
2abb9 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e  st->nExpr;.  pIn
2abba 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  fo = sqlite3DbMa
2abbb 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2abbc 65 6f 66 28 2a 70 49 6e 66 6f 29 20 2b 20 6e 45  eof(*pInfo) + nE
2abbd 78 70 72 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c  xpr*(sizeof(Coll
2abbe 53 65 71 2a 29 2b 31 29 20 29 3b 0a 20 20 69 66  Seq*)+1) );.  if
2abbf 28 20 70 49 6e 66 6f 20 29 7b 0a 20 20 20 20 70  ( pInfo ){.    p
2abc0 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
2abc1 20 3d 20 28 75 38 2a 29 26 70 49 6e 66 6f 2d 3e   = (u8*)&pInfo->
2abc2 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a 20 20  aColl[nExpr];.  
2abc3 20 20 70 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20    pInfo->nField 
2abc4 3d 20 28 75 31 36 29 6e 45 78 70 72 3b 0a 20 20  = (u16)nExpr;.  
2abc5 20 20 70 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45    pInfo->enc = E
2abc6 4e 43 28 64 62 29 3b 0a 20 20 20 20 70 49 6e 66  NC(db);.    pInf
2abc7 6f 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20  o->db = db;.    
2abc8 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70  for(i=0, pItem=p
2abc9 4c 69 73 74 2d 3e 61 3b 20 69 3c 6e 45 78 70 72  List->a; i<nExpr
2abca 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
2abcb 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
2abcc 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70 43 6f  pColl;.      pCo
2abcd 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ll = sqlite3Expr
2abce 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2abcf 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20  pItem->pExpr);. 
2abd0 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20       if( !pColl 
2abd1 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  ){.        pColl
2abd2 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c   = db->pDfltColl
2abd3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2abd4 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20  pInfo->aColl[i] 
2abd5 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 70  = pColl;.      p
2abd6 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
2abd7 5b 69 5d 20 3d 20 70 49 74 65 6d 2d 3e 73 6f 72  [i] = pItem->sor
2abd8 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a 20 20  tOrder;.    }.  
2abd9 7d 0a 20 20 72 65 74 75 72 6e 20 70 49 6e 66 6f  }.  return pInfo
2abda 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  ;.}.../*.** If t
2abdb 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 20 77 61  he inner loop wa
2abdc 73 20 67 65 6e 65 72 61 74 65 64 20 75 73 69 6e  s generated usin
2abdd 67 20 61 20 6e 6f 6e 2d 6e 75 6c 6c 20 70 4f 72  g a non-null pOr
2abde 64 65 72 42 79 20 61 72 67 75 6d 65 6e 74 2c 0a  derBy argument,.
2abdf 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  ** then the resu
2abe0 6c 74 73 20 77 65 72 65 20 70 6c 61 63 65 64 20  lts were placed 
2abe1 69 6e 20 61 20 73 6f 72 74 65 72 2e 20 20 41 66  in a sorter.  Af
2abe2 74 65 72 20 74 68 65 20 6c 6f 6f 70 20 69 73 20  ter the loop is 
2abe3 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 77 65  terminated.** we
2abe4 20 6e 65 65 64 20 74 6f 20 72 75 6e 20 74 68 65   need to run the
2abe5 20 73 6f 72 74 65 72 20 61 6e 64 20 6f 75 74 70   sorter and outp
2abe6 75 74 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20  ut the results. 
2abe7 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   The following.*
2abe8 2a 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61  * routine genera
2abe9 74 65 73 20 74 68 65 20 63 6f 64 65 20 6e 65 65  tes the code nee
2abea 64 65 64 20 74 6f 20 64 6f 20 74 68 61 74 2e 0a  ded to do that..
2abeb 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67  */.static void g
2abec 65 6e 65 72 61 74 65 53 6f 72 74 54 61 69 6c 28  enerateSortTail(
2abed 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2abee 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  ,    /* Parsing 
2abef 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c  context */.  Sel
2abf0 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 2f  ect *p,        /
2abf1 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
2abf2 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 56 64 62 65  tement */.  Vdbe
2abf3 20 2a 76 2c 20 20 20 20 20 20 20 20 20 20 2f 2a   *v,          /*
2abf4 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69   Generate code i
2abf5 6e 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f  nto this VDBE */
2abf6 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 2c 20  .  int nColumn, 
2abf7 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2abf8 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74  f columns of dat
2abf9 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  a */.  SelectDes
2abfa 74 20 2a 70 44 65 73 74 20 2f 2a 20 57 72 69 74  t *pDest /* Writ
2abfb 65 20 74 68 65 20 73 6f 72 74 65 64 20 72 65 73  e the sorted res
2abfc 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ults here */.){.
2abfd 20 20 69 6e 74 20 61 64 64 72 42 72 65 61 6b 20    int addrBreak 
2abfe 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2abff 65 4c 61 62 65 6c 28 76 29 3b 20 20 20 20 20 2f  eLabel(v);     /
2ac00 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 65  * Jump here to e
2ac01 78 69 74 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  xit loop */.  in
2ac02 74 20 61 64 64 72 43 6f 6e 74 69 6e 75 65 20 3d  t addrContinue =
2ac03 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2ac04 4c 61 62 65 6c 28 76 29 3b 20 20 2f 2a 20 4a 75  Label(v);  /* Ju
2ac05 6d 70 20 68 65 72 65 20 66 6f 72 20 6e 65 78 74  mp here for next
2ac06 20 63 79 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   cycle */.  int 
2ac07 61 64 64 72 3b 0a 20 20 69 6e 74 20 69 54 61 62  addr;.  int iTab
2ac08 3b 0a 20 20 69 6e 74 20 70 73 65 75 64 6f 54 61  ;.  int pseudoTa
2ac09 62 20 3d 20 30 3b 0a 20 20 45 78 70 72 4c 69 73  b = 0;.  ExprLis
2ac0a 74 20 2a 70 4f 72 64 65 72 42 79 20 3d 20 70 2d  t *pOrderBy = p-
2ac0b 3e 70 4f 72 64 65 72 42 79 3b 0a 0a 20 20 69 6e  >pOrderBy;..  in
2ac0c 74 20 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d  t eDest = pDest-
2ac0d 3e 65 44 65 73 74 3b 0a 20 20 69 6e 74 20 69 50  >eDest;.  int iP
2ac0e 61 72 6d 20 3d 20 70 44 65 73 74 2d 3e 69 50 61  arm = pDest->iPa
2ac0f 72 6d 3b 0a 0a 20 20 69 6e 74 20 72 65 67 52 6f  rm;..  int regRo
2ac10 77 3b 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 69  w;.  int regRowi
2ac11 64 3b 0a 0a 20 20 69 54 61 62 20 3d 20 70 4f 72  d;..  iTab = pOr
2ac12 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 3b  derBy->iECursor;
2ac13 0a 20 20 72 65 67 52 6f 77 20 3d 20 73 71 6c 69  .  regRow = sqli
2ac14 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
2ac15 61 72 73 65 29 3b 0a 20 20 69 66 28 20 65 44 65  arse);.  if( eDe
2ac16 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c  st==SRT_Output |
2ac17 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72  | eDest==SRT_Cor
2ac18 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 70 73  outine ){.    ps
2ac19 65 75 64 6f 54 61 62 20 3d 20 70 50 61 72 73 65  eudoTab = pParse
2ac1a 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 73 71  ->nTab++;.    sq
2ac1b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2ac1c 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f  v, OP_OpenPseudo
2ac1d 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 72 65 67  , pseudoTab, reg
2ac1e 52 6f 77 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  Row, nColumn);. 
2ac1f 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20 30 3b     regRowid = 0;
2ac20 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
2ac21 67 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  gRowid = sqlite3
2ac22 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2ac23 65 29 3b 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d  e);.  }.  addr =
2ac24 20 31 20 2b 20 73 71 6c 69 74 65 33 56 64 62 65   1 + sqlite3Vdbe
2ac25 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72  AddOp2(v, OP_Sor
2ac26 74 2c 20 69 54 61 62 2c 20 61 64 64 72 42 72 65  t, iTab, addrBre
2ac27 61 6b 29 3b 0a 20 20 63 6f 64 65 4f 66 66 73 65  ak);.  codeOffse
2ac28 74 28 76 2c 20 70 2c 20 61 64 64 72 43 6f 6e 74  t(v, p, addrCont
2ac29 69 6e 75 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  inue);.  sqlite3
2ac2a 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2ac2b 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61 62 2c 20 70  _Column, iTab, p
2ac2c 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20 2b  OrderBy->nExpr +
2ac2d 20 31 2c 20 72 65 67 52 6f 77 29 3b 0a 20 20 73   1, regRow);.  s
2ac2e 77 69 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a  witch( eDest ){.
2ac2f 20 20 20 20 63 61 73 65 20 53 52 54 5f 54 61 62      case SRT_Tab
2ac30 6c 65 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54  le:.    case SRT
2ac31 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20  _EphemTab: {.   
2ac32 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65     testcase( eDe
2ac33 73 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b  st==SRT_Table );
2ac34 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2ac35 20 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65   eDest==SRT_Ephe
2ac36 6d 54 61 62 20 29 3b 0a 20 20 20 20 20 20 73 71  mTab );.      sq
2ac37 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2ac38 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
2ac39 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 69 64 29  iParm, regRowid)
2ac3a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2ac3b 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2ac3c 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72  Insert, iParm, r
2ac3d 65 67 52 6f 77 2c 20 72 65 67 52 6f 77 69 64 29  egRow, regRowid)
2ac3e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2ac3f 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
2ac40 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20  PFLAG_APPEND);. 
2ac41 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ac42 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
2ac43 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
2ac44 20 20 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a     case SRT_Set:
2ac45 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
2ac46 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20   nColumn==1 );. 
2ac47 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2ac48 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp4(v, OP_Mak
2ac49 65 52 65 63 6f 72 64 2c 20 72 65 67 52 6f 77 2c  eRecord, regRow,
2ac4a 20 31 2c 20 72 65 67 52 6f 77 69 64 2c 20 26 70   1, regRowid, &p
2ac4b 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a  ->affinity, 1);.
2ac4c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2ac4d 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
2ac4e 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
2ac4f 52 6f 77 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  Row, 1);.      s
2ac50 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2ac51 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74  (v, OP_IdxInsert
2ac52 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 69  , iParm, regRowi
2ac53 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  d);.      break;
2ac54 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2ac55 53 52 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20  SRT_Mem: {.     
2ac56 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e   assert( nColumn
2ac57 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==1 );.      sql
2ac58 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
2ac59 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 2c  (pParse, regRow,
2ac5a 20 69 50 61 72 6d 2c 20 31 29 3b 0a 20 20 20 20   iParm, 1);.    
2ac5b 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63    /* The LIMIT c
2ac5c 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72 6d 69  lause will termi
2ac5d 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f  nate the loop fo
2ac5e 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72  r us */.      br
2ac5f 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2ac60 66 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  f.    default: {
2ac61 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
2ac62 20 20 20 20 61 73 73 65 72 74 28 20 65 44 65 73      assert( eDes
2ac63 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c  t==SRT_Output ||
2ac64 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f   eDest==SRT_Coro
2ac65 75 74 69 6e 65 20 29 3b 20 0a 20 20 20 20 20 20  utine ); .      
2ac66 74 65 73 74 63 61 73 65 28 20 65 44 65 73 74 3d  testcase( eDest=
2ac67 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 3b 0a 20  =SRT_Output );. 
2ac68 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65       testcase( e
2ac69 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74  Dest==SRT_Corout
2ac6a 69 6e 65 20 29 3b 0a 20 20 20 20 20 20 66 6f 72  ine );.      for
2ac6b 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b  (i=0; i<nColumn;
2ac6c 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61   i++){.        a
2ac6d 73 73 65 72 74 28 20 72 65 67 52 6f 77 21 3d 70  ssert( regRow!=p
2ac6e 44 65 73 74 2d 3e 69 4d 65 6d 2b 69 20 29 3b 0a  Dest->iMem+i );.
2ac6f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2ac70 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2ac71 43 6f 6c 75 6d 6e 2c 20 70 73 65 75 64 6f 54 61  Column, pseudoTa
2ac72 62 2c 20 69 2c 20 70 44 65 73 74 2d 3e 69 4d 65  b, i, pDest->iMe
2ac73 6d 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 69 66  m+i);.        if
2ac74 28 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( i==0 ){.      
2ac75 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2ac76 68 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41  hangeP5(v, OPFLA
2ac77 47 5f 43 4c 45 41 52 43 41 43 48 45 29 3b 0a 20  G_CLEARCACHE);. 
2ac78 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2ac79 0a 20 20 20 20 20 20 69 66 28 20 65 44 65 73 74  .      if( eDest
2ac7a 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a  ==SRT_Output ){.
2ac7b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2ac7c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2ac7d 52 65 73 75 6c 74 52 6f 77 2c 20 70 44 65 73 74  ResultRow, pDest
2ac7e 2d 3e 69 4d 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29  ->iMem, nColumn)
2ac7f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2ac80 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
2ac81 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
2ac82 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e 43   pDest->iMem, nC
2ac83 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 7d 65  olumn);.      }e
2ac84 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
2ac85 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2ac86 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65 73  , OP_Yield, pDes
2ac87 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20  t->iParm);.     
2ac88 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
2ac89 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
2ac8a 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2ac8b 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77  g(pParse, regRow
2ac8c 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65  );.  sqlite3Rele
2ac8d 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2ac8e 65 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20  e, regRowid);.. 
2ac8f 20 2f 2a 20 4c 49 4d 49 54 20 68 61 73 20 62 65   /* LIMIT has be
2ac90 65 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62  en implemented b
2ac91 79 20 74 68 65 20 70 75 73 68 4f 6e 74 6f 53 6f  y the pushOntoSo
2ac92 72 74 65 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a  rter() routine..
2ac93 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
2ac94 2d 3e 69 4c 69 6d 69 74 3d 3d 30 20 29 3b 0a 0a  ->iLimit==0 );..
2ac95 20 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20    /* The bottom 
2ac96 6f 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20 2a 2f  of the loop.  */
2ac97 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
2ac98 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64  solveLabel(v, ad
2ac99 64 72 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 73  drContinue);.  s
2ac9a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2ac9b 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61  (v, OP_Next, iTa
2ac9c 62 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69  b, addr);.  sqli
2ac9d 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
2ac9e 62 65 6c 28 76 2c 20 61 64 64 72 42 72 65 61 6b  bel(v, addrBreak
2ac9f 29 3b 0a 20 20 69 66 28 20 65 44 65 73 74 3d 3d  );.  if( eDest==
2aca0 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c 20 65 44  SRT_Output || eD
2aca1 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69  est==SRT_Corouti
2aca2 6e 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ne ){.    sqlite
2aca3 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2aca4 50 5f 43 6c 6f 73 65 2c 20 70 73 65 75 64 6f 54  P_Close, pseudoT
2aca5 61 62 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  ab, 0);.  }.}../
2aca6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
2aca7 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e  inter to a strin
2aca8 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  g containing the
2aca9 20 27 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79   'declaration ty
2acaa 70 65 27 20 6f 66 20 74 68 65 0a 2a 2a 20 65 78  pe' of the.** ex
2acab 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20  pression pExpr. 
2acac 54 68 65 20 73 74 72 69 6e 67 20 6d 61 79 20 62  The string may b
2acad 65 20 74 72 65 61 74 65 64 20 61 73 20 73 74 61  e treated as sta
2acae 74 69 63 20 62 79 20 74 68 65 20 63 61 6c 6c 65  tic by the calle
2acaf 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 63  r..**.** The dec
2acb0 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 69 73  laration type is
2acb1 20 74 68 65 20 65 78 61 63 74 20 64 61 74 61 74   the exact datat
2acb2 79 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 65  ype definition e
2acb3 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 74 68  xtracted from th
2acb4 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 43 52  e.** original CR
2acb5 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
2acb6 6d 65 6e 74 20 69 66 20 74 68 65 20 65 78 70 72  ment if the expr
2acb7 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75  ession is a colu
2acb8 6d 6e 2e 20 54 68 65 0a 2a 2a 20 64 65 63 6c 61  mn. The.** decla
2acb9 72 61 74 69 6f 6e 20 74 79 70 65 20 66 6f 72 20  ration type for 
2acba 61 20 52 4f 57 49 44 20 66 69 65 6c 64 20 69 73  a ROWID field is
2acbb 20 49 4e 54 45 47 45 52 2e 20 45 78 61 63 74 6c   INTEGER. Exactl
2acbc 79 20 77 68 65 6e 20 61 6e 20 65 78 70 72 65 73  y when an expres
2acbd 73 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6e 73 69  sion.** is consi
2acbe 64 65 72 65 64 20 61 20 63 6f 6c 75 6d 6e 20 63  dered a column c
2acbf 61 6e 20 62 65 20 63 6f 6d 70 6c 65 78 20 69 6e  an be complex in
2acc0 20 74 68 65 20 70 72 65 73 65 6e 63 65 20 6f 66   the presence of
2acc1 20 73 75 62 71 75 65 72 69 65 73 2e 20 54 68 65   subqueries. The
2acc2 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 65  .** result-set e
2acc3 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 61 6c 6c  xpression in all
2acc4 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2acc5 67 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  g SELECT stateme
2acc6 6e 74 73 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 69  nts is .** consi
2acc7 64 65 72 65 64 20 61 20 63 6f 6c 75 6d 6e 20 62  dered a column b
2acc8 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  y this function.
2acc9 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20  .**.**   SELECT 
2acca 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0a 2a 2a  col FROM tbl;.**
2accb 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c 45 43     SELECT (SELEC
2accc 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0a  T col FROM tbl;.
2accd 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c  **   SELECT (SEL
2acce 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c  ECT col FROM tbl
2accf 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 61  );.**   SELECT a
2acd0 62 63 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20  bc FROM (SELECT 
2acd1 63 6f 6c 20 41 53 20 61 62 63 20 46 52 4f 4d 20  col AS abc FROM 
2acd2 74 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20 54 68 65  tbl);.** .** The
2acd3 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
2acd4 65 20 66 6f 72 20 61 6e 79 20 65 78 70 72 65 73  e for any expres
2acd5 73 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20  sion other than 
2acd6 61 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c  a column is NULL
2acd7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
2acd8 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d 6e 54 79  t char *columnTy
2acd9 70 65 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78  pe(.  NameContex
2acda 74 20 2a 70 4e 43 2c 20 0a 20 20 45 78 70 72 20  t *pNC, .  Expr 
2acdb 2a 70 45 78 70 72 2c 0a 20 20 63 6f 6e 73 74 20  *pExpr,.  const 
2acdc 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 44  char **pzOriginD
2acdd 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b,.  const char 
2acde 2a 2a 70 7a 4f 72 69 67 69 6e 54 61 62 2c 0a 20  **pzOriginTab,. 
2acdf 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
2ace0 4f 72 69 67 69 6e 43 6f 6c 0a 29 7b 0a 20 20 63  OriginCol.){.  c
2ace1 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 79 70 65  har const *zType
2ace2 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e   = 0;.  char con
2ace3 73 74 20 2a 7a 4f 72 69 67 69 6e 44 62 20 3d 20  st *zOriginDb = 
2ace4 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  0;.  char const 
2ace5 2a 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20 30 3b  *zOriginTab = 0;
2ace6 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
2ace7 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 30 3b 0a 20  OriginCol = 0;. 
2ace8 20 69 6e 74 20 6a 3b 0a 20 20 69 66 28 20 4e 45   int j;.  if( NE
2ace9 56 45 52 28 70 45 78 70 72 3d 3d 30 29 20 7c 7c  VER(pExpr==0) ||
2acea 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3d 3d   pNC->pSrcList==
2aceb 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20  0 ) return 0;.. 
2acec 20 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e   switch( pExpr->
2aced 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
2acee 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20  K_AGG_COLUMN:.  
2acef 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e    case TK_COLUMN
2acf0 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  : {.      /* The
2acf1 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
2acf2 20 63 6f 6c 75 6d 6e 2e 20 4c 6f 63 61 74 65 20   column. Locate 
2acf3 74 68 65 20 74 61 62 6c 65 20 74 68 65 20 63 6f  the table the co
2acf4 6c 75 6d 6e 20 69 73 20 62 65 69 6e 67 0a 20 20  lumn is being.  
2acf5 20 20 20 20 2a 2a 20 65 78 74 72 61 63 74 65 64      ** extracted
2acf6 20 66 72 6f 6d 20 69 6e 20 4e 61 6d 65 43 6f 6e   from in NameCon
2acf7 74 65 78 74 2e 70 53 72 63 4c 69 73 74 2e 20 54  text.pSrcList. T
2acf8 68 69 73 20 74 61 62 6c 65 20 6d 61 79 20 62 65  his table may be
2acf9 20 72 65 61 6c 0a 20 20 20 20 20 20 2a 2a 20 64   real.      ** d
2acfa 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72  atabase table or
2acfb 20 61 20 73 75 62 71 75 65 72 79 2e 0a 20 20 20   a subquery..   
2acfc 20 20 20 2a 2f 0a 20 20 20 20 20 20 54 61 62 6c     */.      Tabl
2acfd 65 20 2a 70 54 61 62 20 3d 20 30 3b 20 20 20 20  e *pTab = 0;    
2acfe 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
2acff 20 73 74 72 75 63 74 75 72 65 20 63 6f 6c 75 6d   structure colum
2ad00 6e 20 69 73 20 65 78 74 72 61 63 74 65 64 20 66  n is extracted f
2ad01 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c  rom */.      Sel
2ad02 65 63 74 20 2a 70 53 20 3d 20 30 3b 20 20 20 20  ect *pS = 0;    
2ad03 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 6c 65           /* Sele
2ad04 63 74 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73  ct the column is
2ad05 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
2ad06 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f  */.      int iCo
2ad07 6c 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  l = pExpr->iColu
2ad08 6d 6e 3b 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66  mn;  /* Index of
2ad09 20 63 6f 6c 75 6d 6e 20 69 6e 20 70 54 61 62 20   column in pTab 
2ad0a 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
2ad0b 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
2ad0c 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20  _AGG_COLUMN );. 
2ad0d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
2ad0e 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Expr->op==TK_COL
2ad0f 55 4d 4e 20 29 3b 0a 20 20 20 20 20 20 77 68 69  UMN );.      whi
2ad10 6c 65 28 20 70 4e 43 20 26 26 20 21 70 54 61 62  le( pNC && !pTab
2ad11 20 29 7b 0a 20 20 20 20 20 20 20 20 53 72 63 4c   ){.        SrcL
2ad12 69 73 74 20 2a 70 54 61 62 4c 69 73 74 20 3d 20  ist *pTabList = 
2ad13 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20  pNC->pSrcList;. 
2ad14 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 6a         for(j=0;j
2ad15 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20  <pTabList->nSrc 
2ad16 26 26 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a  && pTabList->a[j
2ad17 5d 2e 69 43 75 72 73 6f 72 21 3d 70 45 78 70 72  ].iCursor!=pExpr
2ad18 2d 3e 69 54 61 62 6c 65 3b 6a 2b 2b 29 3b 0a 20  ->iTable;j++);. 
2ad19 20 20 20 20 20 20 20 69 66 28 20 6a 3c 70 54 61         if( j<pTa
2ad1a 62 4c 69 73 74 2d 3e 6e 53 72 63 20 29 7b 0a 20  bList->nSrc ){. 
2ad1b 20 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20           pTab = 
2ad1c 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70  pTabList->a[j].p
2ad1d 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70  Tab;.          p
2ad1e 53 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  S = pTabList->a[
2ad1f 6a 5d 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20  j].pSelect;.    
2ad20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2ad21 20 20 20 20 20 70 4e 43 20 3d 20 70 4e 43 2d 3e       pNC = pNC->
2ad22 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d  pNext;.        }
2ad23 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
2ad24 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20  if( pTab==0 ){. 
2ad25 20 20 20 20 20 20 20 2f 2a 20 41 74 20 6f 6e 65         /* At one
2ad26 20 74 69 6d 65 2c 20 63 6f 64 65 20 73 75 63 68   time, code such
2ad27 20 61 73 20 22 53 45 4c 45 43 54 20 6e 65 77 2e   as "SELECT new.
2ad28 78 22 20 77 69 74 68 69 6e 20 61 20 74 72 69 67  x" within a trig
2ad29 67 65 72 20 77 6f 75 6c 64 0a 20 20 20 20 20 20  ger would.      
2ad2a 20 20 2a 2a 20 63 61 75 73 65 20 74 68 69 73 20    ** cause this 
2ad2b 63 6f 6e 64 69 74 69 6f 6e 20 74 6f 20 72 75 6e  condition to run
2ad2c 2e 20 20 53 69 6e 63 65 20 74 68 65 6e 2c 20 77  .  Since then, w
2ad2d 65 20 68 61 76 65 20 72 65 73 74 72 75 63 74 75  e have restructu
2ad2e 72 65 64 20 68 6f 77 0a 20 20 20 20 20 20 20 20  red how.        
2ad2f 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 64 65 20  ** trigger code 
2ad30 69 73 20 67 65 6e 65 72 61 74 65 64 20 61 6e 64  is generated and
2ad31 20 73 6f 20 74 68 69 73 20 63 6f 6e 64 69 74 69   so this conditi
2ad32 6f 6e 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  on is no longer 
2ad33 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 73 73  .        ** poss
2ad34 69 62 6c 65 2e 20 48 6f 77 65 76 65 72 2c 20 69  ible. However, i
2ad35 74 20 63 61 6e 20 73 74 69 6c 6c 20 62 65 20 74  t can still be t
2ad36 72 75 65 20 66 6f 72 20 73 74 61 74 65 6d 65 6e  rue for statemen
2ad37 74 73 20 6c 69 6b 65 0a 20 20 20 20 20 20 20 20  ts like.        
2ad38 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** the following
2ad39 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20  :.        **.   
2ad3a 20 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45       **   CREATE
2ad3b 20 54 41 42 4c 45 20 74 31 28 63 6f 6c 20 49 4e   TABLE t1(col IN
2ad3c 54 45 47 45 52 29 3b 0a 20 20 20 20 20 20 20 20  TEGER);.        
2ad3d 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c  **   SELECT (SEL
2ad3e 45 43 54 20 74 31 2e 63 6f 6c 29 20 46 52 4f 4d  ECT t1.col) FROM
2ad3f 20 46 52 4f 4d 20 74 31 3b 0a 20 20 20 20 20 20   FROM t1;.      
2ad40 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
2ad41 77 68 65 6e 20 63 6f 6c 75 6d 6e 54 79 70 65 28  when columnType(
2ad42 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74  ) is called on t
2ad43 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22 74  he expression "t
2ad44 31 2e 63 6f 6c 22 20 69 6e 20 74 68 65 20 0a 20  1.col" in the . 
2ad45 20 20 20 20 20 20 20 2a 2a 20 73 75 62 2d 73 65         ** sub-se
2ad46 6c 65 63 74 2e 20 49 6e 20 74 68 69 73 20 63 61  lect. In this ca
2ad47 73 65 2c 20 73 65 74 20 74 68 65 20 63 6f 6c 75  se, set the colu
2ad48 6d 6e 20 74 79 70 65 20 74 6f 20 4e 55 4c 4c 2c  mn type to NULL,
2ad49 20 65 76 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a   even.        **
2ad4a 20 74 68 6f 75 67 68 20 69 74 20 73 68 6f 75 6c   though it shoul
2ad4b 64 20 72 65 61 6c 6c 79 20 62 65 20 22 49 4e 54  d really be "INT
2ad4c 45 47 45 52 22 2e 0a 20 20 20 20 20 20 20 20 2a  EGER"..        *
2ad4d 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69  *.        ** Thi
2ad4e 73 20 69 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c  s is not a probl
2ad4f 65 6d 2c 20 61 73 20 74 68 65 20 63 6f 6c 75 6d  em, as the colum
2ad50 6e 20 74 79 70 65 20 6f 66 20 22 74 31 2e 63 6f  n type of "t1.co
2ad51 6c 22 20 69 73 20 6e 65 76 65 72 0a 20 20 20 20  l" is never.    
2ad52 20 20 20 20 2a 2a 20 75 73 65 64 2e 20 57 68 65      ** used. Whe
2ad53 6e 20 63 6f 6c 75 6d 6e 54 79 70 65 28 29 20 69  n columnType() i
2ad54 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20  s called on the 
2ad55 65 78 70 72 65 73 73 69 6f 6e 20 0a 20 20 20 20  expression .    
2ad56 20 20 20 20 2a 2a 20 22 28 53 45 4c 45 43 54 20      ** "(SELECT 
2ad57 74 31 2e 63 6f 6c 29 22 2c 20 74 68 65 20 63 6f  t1.col)", the co
2ad58 72 72 65 63 74 20 74 79 70 65 20 69 73 20 72 65  rrect type is re
2ad59 74 75 72 6e 65 64 20 28 73 65 65 20 74 68 65 20  turned (see the 
2ad5a 54 4b 5f 53 45 4c 45 43 54 0a 20 20 20 20 20 20  TK_SELECT.      
2ad5b 20 20 2a 2a 20 62 72 61 6e 63 68 20 62 65 6c 6f    ** branch belo
2ad5c 77 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 62  w.  */.        b
2ad5d 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  reak;.      }.. 
2ad5e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61       assert( pTa
2ad5f 62 20 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62  b && pExpr->pTab
2ad60 3d 3d 70 54 61 62 20 29 3b 0a 20 20 20 20 20 20  ==pTab );.      
2ad61 69 66 28 20 70 53 20 29 7b 0a 20 20 20 20 20 20  if( pS ){.      
2ad62 20 20 2f 2a 20 54 68 65 20 22 74 61 62 6c 65 22    /* The "table"
2ad63 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 73   is actually a s
2ad64 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 20 76  ub-select or a v
2ad65 69 65 77 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  iew in the FROM 
2ad66 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 2a  clause.        *
2ad67 2a 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  * of the SELECT 
2ad68 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 74 75 72  statement. Retur
2ad69 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f  n the declaratio
2ad6a 6e 20 74 79 70 65 20 61 6e 64 20 6f 72 69 67 69  n type and origi
2ad6b 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74  n.        ** dat
2ad6c 61 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74  a for the result
2ad6d 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  -set column of t
2ad6e 68 65 20 73 75 62 2d 73 65 6c 65 63 74 2e 0a 20  he sub-select.. 
2ad6f 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2ad70 20 20 69 66 28 20 41 4c 57 41 59 53 28 69 43 6f    if( ALWAYS(iCo
2ad71 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 53 2d  l>=0 && iCol<pS-
2ad72 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 20  >pEList->nExpr) 
2ad73 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
2ad74 49 66 20 69 43 6f 6c 20 69 73 20 6c 65 73 73 20  If iCol is less 
2ad75 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20  than zero, then 
2ad76 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 72  the expression r
2ad77 65 71 75 65 73 74 73 20 74 68 65 0a 20 20 20 20  equests the.    
2ad78 20 20 20 20 20 20 2a 2a 20 72 6f 77 69 64 20 6f        ** rowid o
2ad79 66 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74  f the sub-select
2ad7a 20 6f 72 20 76 69 65 77 2e 20 54 68 69 73 20 65   or view. This e
2ad7b 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6c 65 67  xpression is leg
2ad7c 61 6c 20 28 73 65 65 20 0a 20 20 20 20 20 20 20  al (see .       
2ad7d 20 20 20 2a 2a 20 74 65 73 74 20 63 61 73 65 20     ** test case 
2ad7e 6d 69 73 63 32 2e 32 2e 32 29 20 2d 20 69 74 20  misc2.2.2) - it 
2ad7f 61 6c 77 61 79 73 20 65 76 61 6c 75 61 74 65 73  always evaluates
2ad80 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20   to NULL..      
2ad81 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
2ad82 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43   NameContext sNC
2ad83 3b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72  ;.          Expr
2ad84 20 2a 70 20 3d 20 70 53 2d 3e 70 45 4c 69 73 74   *p = pS->pEList
2ad85 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b  ->a[iCol].pExpr;
2ad86 0a 20 20 20 20 20 20 20 20 20 20 73 4e 43 2e 70  .          sNC.p
2ad87 53 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e 70 53  SrcList = pS->pS
2ad88 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73 4e  rc;.          sN
2ad89 43 2e 70 4e 65 78 74 20 3d 20 70 4e 43 3b 0a 20  C.pNext = pNC;. 
2ad8a 20 20 20 20 20 20 20 20 20 73 4e 43 2e 70 50 61           sNC.pPa
2ad8b 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73  rse = pNC->pPars
2ad8c 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79  e;.          zTy
2ad8d 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28  pe = columnType(
2ad8e 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69  &sNC, p, &zOrigi
2ad8f 6e 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62  nDb, &zOriginTab
2ad90 2c 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20  , &zOriginCol); 
2ad91 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2ad92 20 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59   }else if( ALWAY
2ad93 53 28 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  S(pTab->pSchema)
2ad94 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41   ){.        /* A
2ad95 20 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20   real table */. 
2ad96 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21         assert( !
2ad97 70 53 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  pS );.        if
2ad98 28 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20  ( iCol<0 ) iCol 
2ad99 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20  = pTab->iPKey;. 
2ad9a 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69         assert( i
2ad9b 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c  Col==-1 || (iCol
2ad9c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62  >=0 && iCol<pTab
2ad9d 2d 3e 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20  ->nCol) );.     
2ad9e 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b     if( iCol<0 ){
2ad9f 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65  .          zType
2ada0 20 3d 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20   = "INTEGER";.  
2ada1 20 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43          zOriginC
2ada2 6f 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20  ol = "rowid";.  
2ada3 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2ada4 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20 70         zType = p
2ada5 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
2ada6 7a 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20  zType;.         
2ada7 20 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 70 54   zOriginCol = pT
2ada8 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a  ab->aCol[iCol].z
2ada9 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Name;.        }.
2adaa 20 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 54          zOriginT
2adab 61 62 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ab = pTab->zName
2adac 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e  ;.        if( pN
2adad 43 2d 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 20  C->pParse ){.   
2adae 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d         int iDb =
2adaf 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2adb0 49 6e 64 65 78 28 70 4e 43 2d 3e 70 50 61 72 73  Index(pNC->pPars
2adb1 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
2adb2 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 20  hema);.         
2adb3 20 7a 4f 72 69 67 69 6e 44 62 20 3d 20 70 4e 43   zOriginDb = pNC
2adb4 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44  ->pParse->db->aD
2adb5 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
2adb6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2adb7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2adb8 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
2adb9 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
2adba 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45      case TK_SELE
2adbb 43 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54  CT: {.      /* T
2adbc 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
2adbd 20 61 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 52   a sub-select. R
2adbe 65 74 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72  eturn the declar
2adbf 61 74 69 6f 6e 20 74 79 70 65 20 61 6e 64 0a 20  ation type and. 
2adc0 20 20 20 20 20 2a 2a 20 6f 72 69 67 69 6e 20 69       ** origin i
2adc1 6e 66 6f 20 66 6f 72 20 74 68 65 20 73 69 6e 67  nfo for the sing
2adc2 6c 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  le column in the
2adc3 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
2adc4 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20  he SELECT.      
2adc5 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20  ** statement..  
2adc6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 4e 61 6d      */.      Nam
2adc7 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20  eContext sNC;.  
2adc8 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d      Select *pS =
2adc9 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63   pExpr->x.pSelec
2adca 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  t;.      Expr *p
2adcb 20 3d 20 70 53 2d 3e 70 45 4c 69 73 74 2d 3e 61   = pS->pEList->a
2adcc 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [0].pExpr;.     
2adcd 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73   assert( ExprHas
2adce 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2adcf 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
2add0 0a 20 20 20 20 20 20 73 4e 43 2e 70 53 72 63 4c  .      sNC.pSrcL
2add1 69 73 74 20 3d 20 70 53 2d 3e 70 53 72 63 3b 0a  ist = pS->pSrc;.
2add2 20 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20        sNC.pNext 
2add3 3d 20 70 4e 43 3b 0a 20 20 20 20 20 20 73 4e 43  = pNC;.      sNC
2add4 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70  .pParse = pNC->p
2add5 50 61 72 73 65 3b 0a 20 20 20 20 20 20 7a 54 79  Parse;.      zTy
2add6 70 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28  pe = columnType(
2add7 26 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 69  &sNC, p, &zOrigi
2add8 6e 44 62 2c 20 26 7a 4f 72 69 67 69 6e 54 61 62  nDb, &zOriginTab
2add9 2c 20 26 7a 4f 72 69 67 69 6e 43 6f 6c 29 3b 20  , &zOriginCol); 
2adda 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2addb 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
2addc 20 0a 20 20 69 66 28 20 70 7a 4f 72 69 67 69 6e   .  if( pzOrigin
2addd 44 62 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  Db ){.    assert
2adde 28 20 70 7a 4f 72 69 67 69 6e 54 61 62 20 26 26  ( pzOriginTab &&
2addf 20 70 7a 4f 72 69 67 69 6e 43 6f 6c 20 29 3b 0a   pzOriginCol );.
2ade0 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e 44 62 20      *pzOriginDb 
2ade1 3d 20 7a 4f 72 69 67 69 6e 44 62 3b 0a 20 20 20  = zOriginDb;.   
2ade2 20 2a 70 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20   *pzOriginTab = 
2ade3 7a 4f 72 69 67 69 6e 54 61 62 3b 0a 20 20 20 20  zOriginTab;.    
2ade4 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 7a  *pzOriginCol = z
2ade5 4f 72 69 67 69 6e 43 6f 6c 3b 0a 20 20 7d 0a 20  OriginCol;.  }. 
2ade6 20 72 65 74 75 72 6e 20 7a 54 79 70 65 3b 0a 7d   return zType;.}
2ade7 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2ade8 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
2ade9 74 65 6c 6c 20 74 68 65 20 56 44 42 45 20 74 68  tell the VDBE th
2adea 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79  e declaration ty
2adeb 70 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a  pes of columns.*
2adec 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  * in the result 
2aded 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  set..*/.static v
2adee 6f 69 64 20 67 65 6e 65 72 61 74 65 43 6f 6c 75  oid generateColu
2adef 6d 6e 54 79 70 65 73 28 0a 20 20 50 61 72 73 65  mnTypes(.  Parse
2adf0 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f   *pParse,      /
2adf1 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74  * Parser context
2adf2 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
2adf3 54 61 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73  TabList,  /* Lis
2adf4 74 20 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a 20  t of tables */. 
2adf5 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2adf6 74 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69  t    /* Expressi
2adf7 6f 6e 73 20 64 65 66 69 6e 69 6e 67 20 74 68 65  ons defining the
2adf8 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 29   result set */.)
2adf9 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
2adfa 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 0a 20  _OMIT_DECLTYPE. 
2adfb 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
2adfc 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20  e->pVdbe;.  int 
2adfd 69 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  i;.  NameContext
2adfe 20 73 4e 43 3b 0a 20 20 73 4e 43 2e 70 53 72 63   sNC;.  sNC.pSrc
2adff 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b  List = pTabList;
2ae00 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20  .  sNC.pParse = 
2ae01 70 50 61 72 73 65 3b 0a 20 20 66 6f 72 28 69 3d  pParse;.  for(i=
2ae02 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; i<pEList->nEx
2ae03 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78  pr; i++){.    Ex
2ae04 70 72 20 2a 70 20 3d 20 70 45 4c 69 73 74 2d 3e  pr *p = pEList->
2ae05 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
2ae06 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70  const char *zTyp
2ae07 65 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  e;.#ifdef SQLITE
2ae08 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d  _ENABLE_COLUMN_M
2ae09 45 54 41 44 41 54 41 0a 20 20 20 20 63 6f 6e 73  ETADATA.    cons
2ae0a 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 44 62 20  t char *zOrigDb 
2ae0b 3d 20 30 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  = 0;.    const c
2ae0c 68 61 72 20 2a 7a 4f 72 69 67 54 61 62 20 3d 20  har *zOrigTab = 
2ae0d 30 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  0;.    const cha
2ae0e 72 20 2a 7a 4f 72 69 67 43 6f 6c 20 3d 20 30 3b  r *zOrigCol = 0;
2ae0f 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c  .    zType = col
2ae10 75 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c  umnType(&sNC, p,
2ae11 20 26 7a 4f 72 69 67 44 62 2c 20 26 7a 4f 72 69   &zOrigDb, &zOri
2ae12 67 54 61 62 2c 20 26 7a 4f 72 69 67 43 6f 6c 29  gTab, &zOrigCol)
2ae13 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 76 64  ;..    /* The vd
2ae14 62 65 20 6d 75 73 74 20 6d 61 6b 65 20 69 74 73  be must make its
2ae15 20 6f 77 6e 20 63 6f 70 79 20 6f 66 20 74 68 65   own copy of the
2ae16 20 63 6f 6c 75 6d 6e 2d 74 79 70 65 20 61 6e 64   column-type and
2ae17 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a 20 63   other .    ** c
2ae18 6f 6c 75 6d 6e 20 73 70 65 63 69 66 69 63 20 73  olumn specific s
2ae19 74 72 69 6e 67 73 2c 20 69 6e 20 63 61 73 65 20  trings, in case 
2ae1a 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 72 65  the schema is re
2ae1b 73 65 74 20 62 65 66 6f 72 65 20 74 68 69 73 0a  set before this.
2ae1c 20 20 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d      ** virtual m
2ae1d 61 63 68 69 6e 65 20 69 73 20 64 65 6c 65 74 65  achine is delete
2ae1e 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  d..    */.    sq
2ae1f 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2ae20 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d  ame(v, i, COLNAM
2ae21 45 5f 44 41 54 41 42 41 53 45 2c 20 7a 4f 72 69  E_DATABASE, zOri
2ae22 67 44 62 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  gDb, SQLITE_TRAN
2ae23 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 71 6c 69  SIENT);.    sqli
2ae24 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2ae25 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, i, COLNAME_
2ae26 54 41 42 4c 45 2c 20 7a 4f 72 69 67 54 61 62 2c  TABLE, zOrigTab,
2ae27 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
2ae28 54 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  T);.    sqlite3V
2ae29 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
2ae2a 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55   i, COLNAME_COLU
2ae2b 4d 4e 2c 20 7a 4f 72 69 67 43 6f 6c 2c 20 53 51  MN, zOrigCol, SQ
2ae2c 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
2ae2d 0a 23 65 6c 73 65 0a 20 20 20 20 7a 54 79 70 65  .#else.    zType
2ae2e 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73   = columnType(&s
2ae2f 4e 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30 29 3b  NC, p, 0, 0, 0);
2ae30 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69  .#endif.    sqli
2ae31 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2ae32 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, i, COLNAME_
2ae33 44 45 43 4c 54 59 50 45 2c 20 7a 54 79 70 65 2c  DECLTYPE, zType,
2ae34 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
2ae35 54 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f  T);.  }.#endif /
2ae36 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  * SQLITE_OMIT_DE
2ae37 43 4c 54 59 50 45 20 2a 2f 0a 7d 0a 0a 2f 2a 0a  CLTYPE */.}../*.
2ae38 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
2ae39 20 74 68 61 74 20 77 69 6c 6c 20 74 65 6c 6c 20   that will tell 
2ae3a 74 68 65 20 56 44 42 45 20 74 68 65 20 6e 61 6d  the VDBE the nam
2ae3b 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a  es of columns.**
2ae3c 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
2ae3d 65 74 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d  et.  This inform
2ae3e 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ation is used to
2ae3f 20 70 72 6f 76 69 64 65 20 74 68 65 0a 2a 2a 20   provide the.** 
2ae40 61 7a 43 6f 6c 5b 5d 20 76 61 6c 75 65 73 20 69  azCol[] values i
2ae41 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a  n the callback..
2ae42 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67  */.static void g
2ae43 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d  enerateColumnNam
2ae44 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  es(.  Parse *pPa
2ae45 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72  rse,      /* Par
2ae46 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
2ae47 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
2ae48 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  st,  /* List of 
2ae49 74 61 62 6c 65 73 20 2a 2f 0a 20 20 45 78 70 72  tables */.  Expr
2ae4a 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20  List *pEList    
2ae4b 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 73 20 64  /* Expressions d
2ae4c 65 66 69 6e 69 6e 67 20 74 68 65 20 72 65 73 75  efining the resu
2ae4d 6c 74 20 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 56  lt set */.){.  V
2ae4e 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
2ae4f 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 2c  >pVdbe;.  int i,
2ae50 20 6a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   j;.  sqlite3 *d
2ae51 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2ae52 20 20 69 6e 74 20 66 75 6c 6c 4e 61 6d 65 73 2c    int fullNames,
2ae53 20 73 68 6f 72 74 4e 61 6d 65 73 3b 0a 0a 23 69   shortNames;..#i
2ae54 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2ae55 54 5f 45 58 50 4c 41 49 4e 0a 20 20 2f 2a 20 49  T_EXPLAIN.  /* I
2ae56 66 20 74 68 69 73 20 69 73 20 61 6e 20 45 58 50  f this is an EXP
2ae57 4c 41 49 4e 2c 20 73 6b 69 70 20 74 68 69 73 20  LAIN, skip this 
2ae58 73 74 65 70 20 2a 2f 0a 20 20 69 66 28 20 70 50  step */.  if( pP
2ae59 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b  arse->explain ){
2ae5a 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
2ae5b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 70  .#endif..  if( p
2ae5c 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73 53  Parse->colNamesS
2ae5d 65 74 20 7c 7c 20 4e 45 56 45 52 28 76 3d 3d 30  et || NEVER(v==0
2ae5e 29 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ) || db->mallocF
2ae5f 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
2ae60 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d    pParse->colNam
2ae61 65 73 53 65 74 20 3d 20 31 3b 0a 20 20 66 75 6c  esSet = 1;.  ful
2ae62 6c 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66 6c  lNames = (db->fl
2ae63 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75 6c  ags & SQLITE_Ful
2ae64 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a 20  lColNames)!=0;. 
2ae65 20 73 68 6f 72 74 4e 61 6d 65 73 20 3d 20 28 64   shortNames = (d
2ae66 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
2ae67 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 29  E_ShortColNames)
2ae68 21 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  !=0;.  sqlite3Vd
2ae69 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
2ae6a 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a  pEList->nExpr);.
2ae6b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c    for(i=0; i<pEL
2ae6c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
2ae6d 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 3b 0a 20  {.    Expr *p;. 
2ae6e 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e 61     p = pEList->a
2ae6f 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 69  [i].pExpr;.    i
2ae70 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20 29  f( NEVER(p==0) )
2ae71 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
2ae72 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e  f( pEList->a[i].
2ae73 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 63  zName ){.      c
2ae74 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 45 4c  har *zName = pEL
2ae75 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b  ist->a[i].zName;
2ae76 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2ae77 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
2ae78 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  i, COLNAME_NAME,
2ae79 20 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 54   zName, SQLITE_T
2ae7a 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 7d  RANSIENT);.    }
2ae7b 65 6c 73 65 20 69 66 28 20 28 70 2d 3e 6f 70 3d  else if( (p->op=
2ae7c 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 2d  =TK_COLUMN || p-
2ae7d 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55  >op==TK_AGG_COLU
2ae7e 4d 4e 29 20 26 26 20 70 54 61 62 4c 69 73 74 20  MN) && pTabList 
2ae7f 29 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a  ){.      Table *
2ae80 70 54 61 62 3b 0a 20 20 20 20 20 20 63 68 61 72  pTab;.      char
2ae81 20 2a 7a 43 6f 6c 3b 0a 20 20 20 20 20 20 69 6e   *zCol;.      in
2ae82 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 69 43 6f 6c  t iCol = p->iCol
2ae83 75 6d 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  umn;.      for(j
2ae84 3d 30 3b 20 41 4c 57 41 59 53 28 6a 3c 70 54 61  =0; ALWAYS(j<pTa
2ae85 62 4c 69 73 74 2d 3e 6e 53 72 63 29 3b 20 6a 2b  bList->nSrc); j+
2ae86 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
2ae87 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 69  pTabList->a[j].i
2ae88 43 75 72 73 6f 72 3d 3d 70 2d 3e 69 54 61 62 6c  Cursor==p->iTabl
2ae89 65 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  e ) break;.     
2ae8a 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
2ae8b 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72   j<pTabList->nSr
2ae8c 63 20 29 3b 0a 20 20 20 20 20 20 70 54 61 62 20  c );.      pTab 
2ae8d 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d  = pTabList->a[j]
2ae8e 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28  .pTab;.      if(
2ae8f 20 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d   iCol<0 ) iCol =
2ae90 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20   pTab->iPKey;.  
2ae91 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c      assert( iCol
2ae92 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e 3d 30  ==-1 || (iCol>=0
2ae93 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e   && iCol<pTab->n
2ae94 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20 20 69 66  Col) );.      if
2ae95 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20  ( iCol<0 ){.    
2ae96 20 20 20 20 7a 43 6f 6c 20 3d 20 22 72 6f 77 69      zCol = "rowi
2ae97 64 22 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  d";.      }else{
2ae98 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 20 3d 20  .        zCol = 
2ae99 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d  pTab->aCol[iCol]
2ae9a 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a  .zName;.      }.
2ae9b 20 20 20 20 20 20 69 66 28 20 21 73 68 6f 72 74        if( !short
2ae9c 4e 61 6d 65 73 20 26 26 20 21 66 75 6c 6c 4e 61  Names && !fullNa
2ae9d 6d 65 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73  mes ){.        s
2ae9e 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2ae9f 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
2aea0 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20  ME_NAME, .      
2aea1 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 53        sqlite3DbS
2aea2 74 72 44 75 70 28 64 62 2c 20 70 45 4c 69 73 74  trDup(db, pEList
2aea3 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29 2c 20 53  ->a[i].zSpan), S
2aea4 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a  QLITE_DYNAMIC);.
2aea5 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2aea6 66 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a 20 20 20  fullNames ){.   
2aea7 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65       char *zName
2aea8 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e   = 0;.        zN
2aea9 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
2aeaa 69 6e 74 66 28 64 62 2c 20 22 25 73 2e 25 73 22  intf(db, "%s.%s"
2aeab 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  , pTab->zName, z
2aeac 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71  Col);.        sq
2aead 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2aeae 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d  ame(v, i, COLNAM
2aeaf 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 53  E_NAME, zName, S
2aeb0 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a  QLITE_DYNAMIC);.
2aeb1 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2aeb2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2aeb3 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
2aeb4 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a   COLNAME_NAME, z
2aeb5 43 6f 6c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  Col, SQLITE_TRAN
2aeb6 53 49 45 4e 54 29 3b 0a 20 20 20 20 20 20 7d 0a  SIENT);.      }.
2aeb7 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2aeb8 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2aeb9 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
2aeba 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20  NAME_NAME, .    
2aebb 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 53        sqlite3DbS
2aebc 74 72 44 75 70 28 64 62 2c 20 70 45 4c 69 73 74  trDup(db, pEList
2aebd 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29 2c 20 53  ->a[i].zSpan), S
2aebe 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a  QLITE_DYNAMIC);.
2aebf 20 20 20 20 7d 0a 20 20 7d 0a 20 20 67 65 6e 65      }.  }.  gene
2aec0 72 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28  rateColumnTypes(
2aec1 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
2aec2 2c 20 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a 23 69  , pEList);.}..#i
2aec3 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2aec4 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  T_COMPOUND_SELEC
2aec5 54 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20  T./*.** Name of 
2aec6 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f  the connection o
2aec7 70 65 72 61 74 6f 72 2c 20 75 73 65 64 20 66 6f  perator, used fo
2aec8 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  r error messages
2aec9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
2aeca 74 20 63 68 61 72 20 2a 73 65 6c 65 63 74 4f 70  t char *selectOp
2aecb 4e 61 6d 65 28 69 6e 74 20 69 64 29 7b 0a 20 20  Name(int id){.  
2aecc 63 68 61 72 20 2a 7a 3b 0a 20 20 73 77 69 74 63  char *z;.  switc
2aecd 68 28 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73  h( id ){.    cas
2aece 65 20 54 4b 5f 41 4c 4c 3a 20 20 20 20 20 20 20  e TK_ALL:       
2aecf 7a 20 3d 20 22 55 4e 49 4f 4e 20 41 4c 4c 22 3b  z = "UNION ALL";
2aed0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61     break;.    ca
2aed1 73 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a  se TK_INTERSECT:
2aed2 20 7a 20 3d 20 22 49 4e 54 45 52 53 45 43 54 22   z = "INTERSECT"
2aed3 3b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63  ;   break;.    c
2aed4 61 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 20 20  ase TK_EXCEPT:  
2aed5 20 20 7a 20 3d 20 22 45 58 43 45 50 54 22 3b 20    z = "EXCEPT"; 
2aed6 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2aed7 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20  default:        
2aed8 20 20 20 7a 20 3d 20 22 55 4e 49 4f 4e 22 3b 20     z = "UNION"; 
2aed9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d        break;.  }
2aeda 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23  .  return z;.}.#
2aedb 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2aedc 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
2aedd 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47  LECT */../*.** G
2aede 69 76 65 6e 20 61 20 61 6e 20 65 78 70 72 65 73  iven a an expres
2aedf 73 69 6f 6e 20 6c 69 73 74 20 28 77 68 69 63 68  sion list (which
2aee0 20 69 73 20 72 65 61 6c 6c 79 20 74 68 65 20 6c   is really the l
2aee1 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ist of expressio
2aee2 6e 73 0a 2a 2a 20 74 68 61 74 20 66 6f 72 6d 20  ns.** that form 
2aee3 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
2aee4 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  f a SELECT state
2aee5 6d 65 6e 74 29 20 63 6f 6d 70 75 74 65 20 61 70  ment) compute ap
2aee6 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 63 6f 6c  propriate.** col
2aee7 75 6d 6e 20 6e 61 6d 65 73 20 66 6f 72 20 61 20  umn names for a 
2aee8 74 61 62 6c 65 20 74 68 61 74 20 77 6f 75 6c 64  table that would
2aee9 20 68 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73   hold the expres
2aeea 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a  sion list..**.**
2aeeb 20 41 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   All column name
2aeec 73 20 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65  s will be unique
2aeed 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65  ..**.** Only the
2aeee 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 72   column names ar
2aeef 65 20 63 6f 6d 70 75 74 65 64 2e 20 20 43 6f 6c  e computed.  Col
2aef0 75 6d 6e 2e 7a 54 79 70 65 2c 20 43 6f 6c 75 6d  umn.zType, Colum
2aef1 6e 2e 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61 6e 64 20  n.zColl,.** and 
2aef2 6f 74 68 65 72 20 66 69 65 6c 64 73 20 6f 66 20  other fields of 
2aef3 43 6f 6c 75 6d 6e 20 61 72 65 20 7a 65 72 6f 65  Column are zeroe
2aef4 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  d..**.** Return 
2aef5 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
2aef6 63 65 73 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f  cess.  If a memo
2aef7 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
2aef8 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 73  ror occurs,.** s
2aef9 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 2a 70 61  tore NULL in *pa
2aefa 43 6f 6c 20 61 6e 64 20 30 20 69 6e 20 2a 70 6e  Col and 0 in *pn
2aefb 43 6f 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 53  Col and return S
2aefc 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a  QLITE_NOMEM..*/.
2aefd 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63  static int selec
2aefe 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72  tColumnsFromExpr
2aeff 4c 69 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70  List(.  Parse *p
2af00 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2af01 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2af02 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
2af03 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20   *pEList,       
2af04 2f 2a 20 45 78 70 72 20 6c 69 73 74 20 66 72 6f  /* Expr list fro
2af05 6d 20 77 68 69 63 68 20 74 6f 20 64 65 72 69 76  m which to deriv
2af06 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a  e column names *
2af07 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 2c 20  /.  int *pnCol, 
2af08 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57              /* W
2af09 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  rite the number 
2af0a 6f 66 20 63 6f 6c 75 6d 6e 73 20 68 65 72 65 20  of columns here 
2af0b 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 2a 70 61  */.  Column **pa
2af0c 43 6f 6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  Col          /* 
2af0d 57 72 69 74 65 20 74 68 65 20 6e 65 77 20 63 6f  Write the new co
2af0e 6c 75 6d 6e 20 6c 69 73 74 20 68 65 72 65 20 2a  lumn list here *
2af0f 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
2af10 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2af11 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
2af12 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69  onnection */.  i
2af13 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
2af14 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2af15 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
2af16 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20 20   int cnt;       
2af17 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2af18 49 6e 64 65 78 20 61 64 64 65 64 20 74 6f 20 6d  Index added to m
2af19 61 6b 65 20 74 68 65 20 6e 61 6d 65 20 75 6e 69  ake the name uni
2af1a 71 75 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20  que */.  Column 
2af1b 2a 61 43 6f 6c 2c 20 2a 70 43 6f 6c 3b 20 20 20  *aCol, *pCol;   
2af1c 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70       /* For loop
2af1d 69 6e 67 20 6f 76 65 72 20 72 65 73 75 6c 74 20  ing over result 
2af1e 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e 74  columns */.  int
2af1f 20 6e 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   nCol;          
2af20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2af21 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
2af22 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2af23 2a 2f 0a 20 20 45 78 70 72 20 2a 70 3b 20 20 20  */.  Expr *p;   
2af24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af25 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 66   /* Expression f
2af26 6f 72 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75  or a single resu
2af27 6c 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63  lt column */.  c
2af28 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
2af29 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
2af2a 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 69  lumn name */.  i
2af2b 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20  nt nName;       
2af2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
2af2d 7a 65 20 6f 66 20 6e 61 6d 65 20 69 6e 20 7a 4e  ze of name in zN
2af2e 61 6d 65 5b 5d 20 2a 2f 0a 0a 20 20 2a 70 6e 43  ame[] */..  *pnC
2af2f 6f 6c 20 3d 20 6e 43 6f 6c 20 3d 20 70 45 4c 69  ol = nCol = pELi
2af30 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 43 6f  st->nExpr;.  aCo
2af31 6c 20 3d 20 2a 70 61 43 6f 6c 20 3d 20 73 71 6c  l = *paCol = sql
2af32 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2af33 28 64 62 2c 20 73 69 7a 65 6f 66 28 61 43 6f 6c  (db, sizeof(aCol
2af34 5b 30 5d 29 2a 6e 43 6f 6c 29 3b 0a 20 20 69 66  [0])*nCol);.  if
2af35 28 20 61 43 6f 6c 3d 3d 30 20 29 20 72 65 74 75  ( aCol==0 ) retu
2af36 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2af37 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c  .  for(i=0, pCol
2af38 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69  =aCol; i<nCol; i
2af39 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20  ++, pCol++){.   
2af3a 20 2f 2a 20 47 65 74 20 61 6e 20 61 70 70 72 6f   /* Get an appro
2af3b 70 72 69 61 74 65 20 6e 61 6d 65 20 66 6f 72 20  priate name for 
2af3c 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a  the column.    *
2af3d 2f 0a 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74  /.    p = pEList
2af3e 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[i].pExpr;.  
2af3f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69    assert( p->pRi
2af40 67 68 74 3d 3d 30 20 7c 7c 20 45 78 70 72 48 61  ght==0 || ExprHa
2af41 73 50 72 6f 70 65 72 74 79 28 70 2d 3e 70 52 69  sProperty(p->pRi
2af42 67 68 74 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  ght, EP_IntValue
2af43 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
2af44 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e 75   || p->pRight->u
2af45 2e 7a 54 6f 6b 65 6e 3d 3d 30 20 7c 7c 20 70 2d  .zToken==0 || p-
2af46 3e 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65  >pRight->u.zToke
2af47 6e 5b 30 5d 21 3d 30 20 29 3b 0a 20 20 20 20 69  n[0]!=0 );.    i
2af48 66 28 20 28 7a 4e 61 6d 65 20 3d 20 70 45 4c 69  f( (zName = pELi
2af49 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 21  st->a[i].zName)!
2af4a 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49  =0 ){.      /* I
2af4b 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e  f the column con
2af4c 74 61 69 6e 73 20 61 6e 20 22 41 53 20 3c 6e 61  tains an "AS <na
2af4d 6d 65 3e 22 20 70 68 72 61 73 65 2c 20 75 73 65  me>" phrase, use
2af4e 20 3c 6e 61 6d 65 3e 20 61 73 20 74 68 65 20 6e   <name> as the n
2af4f 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 7a 4e 61  ame */.      zNa
2af50 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
2af51 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b  rDup(db, zName);
2af52 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2af53 20 20 45 78 70 72 20 2a 70 43 6f 6c 45 78 70 72    Expr *pColExpr
2af54 20 3d 20 70 3b 20 20 2f 2a 20 54 68 65 20 65 78   = p;  /* The ex
2af55 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73  pression that is
2af56 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75   the result colu
2af57 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20  mn name */.     
2af58 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
2af59 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 61        /* Table a
2af5a 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2af5b 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  his expression *
2af5c 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70  /.      while( p
2af5d 43 6f 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ColExpr->op==TK_
2af5e 44 4f 54 20 29 20 70 43 6f 6c 45 78 70 72 20 3d  DOT ) pColExpr =
2af5f 20 70 43 6f 6c 45 78 70 72 2d 3e 70 52 69 67 68   pColExpr->pRigh
2af60 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f  t;.      if( pCo
2af61 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  lExpr->op==TK_CO
2af62 4c 55 4d 4e 20 26 26 20 41 4c 57 41 59 53 28 70  LUMN && ALWAYS(p
2af63 43 6f 6c 45 78 70 72 2d 3e 70 54 61 62 21 3d 30  ColExpr->pTab!=0
2af64 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
2af65 46 6f 72 20 63 6f 6c 75 6d 6e 73 20 75 73 65 20  For columns use 
2af66 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  the column name 
2af67 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  name */.        
2af68 69 6e 74 20 69 43 6f 6c 20 3d 20 70 43 6f 6c 45  int iCol = pColE
2af69 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  xpr->iColumn;.  
2af6a 20 20 20 20 20 20 70 54 61 62 20 3d 20 70 43 6f        pTab = pCo
2af6b 6c 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20  lExpr->pTab;.   
2af6c 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20       if( iCol<0 
2af6d 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69  ) iCol = pTab->i
2af6e 50 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 7a 4e  PKey;.        zN
2af6f 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
2af70 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 0a 20  intf(db, "%s",. 
2af71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af72 69 43 6f 6c 3e 3d 30 20 3f 20 70 54 61 62 2d 3e  iCol>=0 ? pTab->
2af73 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65  aCol[iCol].zName
2af74 20 3a 20 22 72 6f 77 69 64 22 29 3b 0a 20 20 20   : "rowid");.   
2af75 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43 6f     }else if( pCo
2af76 6c 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 44  lExpr->op==TK_ID
2af77 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
2af78 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
2af79 65 72 74 79 28 70 43 6f 6c 45 78 70 72 2c 20 45  erty(pColExpr, E
2af7a 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
2af7b 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73         zName = s
2af7c 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2af7d 2c 20 22 25 73 22 2c 20 70 43 6f 6c 45 78 70 72  , "%s", pColExpr
2af7e 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20  ->u.zToken);.   
2af7f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2af80 20 20 2f 2a 20 55 73 65 20 74 68 65 20 6f 72 69    /* Use the ori
2af81 67 69 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68  ginal text of th
2af82 65 20 63 6f 6c 75 6d 6e 20 65 78 70 72 65 73 73  e column express
2af83 69 6f 6e 20 61 73 20 69 74 73 20 6e 61 6d 65 20  ion as its name 
2af84 2a 2f 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65  */.        zName
2af85 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2af86 66 28 64 62 2c 20 22 25 73 22 2c 20 70 45 4c 69  f(db, "%s", pELi
2af87 73 74 2d 3e 61 5b 69 5d 2e 7a 53 70 61 6e 29 3b  st->a[i].zSpan);
2af88 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2af89 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
2af8a 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20  cFailed ){.     
2af8b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2af8c 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b, zName);.     
2af8d 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
2af8e 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
2af8f 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20  the column name 
2af90 69 73 20 75 6e 69 71 75 65 2e 20 20 49 66 20 74  is unique.  If t
2af91 68 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 75  he name is not u
2af92 6e 69 71 75 65 2c 0a 20 20 20 20 2a 2a 20 61 70  nique,.    ** ap
2af93 70 65 6e 64 20 61 20 69 6e 74 65 67 65 72 20 74  pend a integer t
2af94 6f 20 74 68 65 20 6e 61 6d 65 20 73 6f 20 74 68  o the name so th
2af95 61 74 20 69 74 20 62 65 63 6f 6d 65 73 20 75 6e  at it becomes un
2af96 69 71 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ique..    */.   
2af97 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
2af98 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b  Strlen30(zName);
2af99 0a 20 20 20 20 66 6f 72 28 6a 3d 63 6e 74 3d 30  .    for(j=cnt=0
2af9a 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ; j<i; j++){.   
2af9b 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
2af9c 72 49 43 6d 70 28 61 43 6f 6c 5b 6a 5d 2e 7a 4e  rICmp(aCol[j].zN
2af9d 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29  ame, zName)==0 )
2af9e 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  {.        char *
2af9f 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20 20 20 20  zNewName;.      
2afa0 20 20 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d    zName[nName] =
2afa1 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 4e 65 77   0;.        zNew
2afa2 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  Name = sqlite3MP
2afa3 72 69 6e 74 66 28 64 62 2c 20 22 25 73 3a 25 64  rintf(db, "%s:%d
2afa4 22 2c 20 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29  ", zName, ++cnt)
2afa5 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2afa6 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d  3DbFree(db, zNam
2afa7 65 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d  e);.        zNam
2afa8 65 20 3d 20 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20  e = zNewName;.  
2afa9 20 20 20 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20        j = -1;.  
2afaa 20 20 20 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d        if( zName=
2afab 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
2afac 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 43    }.    }.    pC
2afad 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d  ol->zName = zNam
2afae 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d  e;.  }.  if( db-
2afaf 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2afb0 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
2afb1 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 73  i; j++){.      s
2afb2 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2afb3 20 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3b   aCol[j].zName);
2afb4 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2afb5 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 43 6f  e3DbFree(db, aCo
2afb6 6c 29 3b 0a 20 20 20 20 2a 70 61 43 6f 6c 20 3d  l);.    *paCol =
2afb7 20 30 3b 0a 20 20 20 20 2a 70 6e 43 6f 6c 20 3d   0;.    *pnCol =
2afb8 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   0;.    return S
2afb9 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
2afba 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2afbb 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  _OK;.}../*.** Ad
2afbc 64 20 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61  d type and colla
2afbd 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tion information
2afbe 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 6c 69 73   to a column lis
2afbf 74 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 61 20  t based on.** a 
2afc0 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2afc1 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 63 6f 6c  ..** .** The col
2afc2 75 6d 6e 20 6c 69 73 74 20 70 72 65 73 75 6d 61  umn list presuma
2afc3 62 6c 79 20 63 61 6d 65 20 66 72 6f 6d 20 73 65  bly came from se
2afc4 6c 65 63 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 46  lectColumnNamesF
2afc5 72 6f 6d 45 78 70 72 4c 69 73 74 28 29 2e 0a 2a  romExprList()..*
2afc6 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73  * The column lis
2afc7 74 20 68 61 73 20 6f 6e 6c 79 20 6e 61 6d 65 73  t has only names
2afc8 2c 20 6e 6f 74 20 74 79 70 65 73 20 6f 72 20 63  , not types or c
2afc9 6f 6c 6c 61 74 69 6f 6e 73 2e 20 20 54 68 69 73  ollations.  This
2afca 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 6f 65 73  .** routine goes
2afcb 20 74 68 72 6f 75 67 68 20 61 6e 64 20 61 64 64   through and add
2afcc 73 20 74 68 65 20 74 79 70 65 73 20 61 6e 64 20  s the types and 
2afcd 63 6f 6c 6c 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  collations..**.*
2afce 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
2afcf 65 71 75 69 72 65 73 20 74 68 61 74 20 61 6c 6c  equires that all
2afd0 20 69 64 65 6e 74 69 66 69 65 72 73 20 69 6e 20   identifiers in 
2afd1 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74  the SELECT.** st
2afd2 61 74 65 6d 65 6e 74 20 62 65 20 72 65 73 6f 6c  atement be resol
2afd3 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ved..*/.static v
2afd4 6f 69 64 20 73 65 6c 65 63 74 41 64 64 43 6f 6c  oid selectAddCol
2afd5 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61 74  umnTypeAndCollat
2afd6 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
2afd7 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  arse,        /* 
2afd8 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 73  Parsing contexts
2afd9 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20   */.  int nCol, 
2afda 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2afdb 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
2afdc 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43   */.  Column *aC
2afdd 6f 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  ol,         /* L
2afde 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a  ist of columns *
2afdf 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
2afe0 65 63 74 20 20 20 20 20 20 20 2f 2a 20 53 45 4c  ect       /* SEL
2afe1 45 43 54 20 75 73 65 64 20 74 6f 20 64 65 74 65  ECT used to dete
2afe2 72 6d 69 6e 65 20 74 79 70 65 73 20 61 6e 64 20  rmine types and 
2afe3 63 6f 6c 6c 61 74 69 6f 6e 73 20 2a 2f 0a 29 7b  collations */.){
2afe4 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2afe5 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 4e   pParse->db;.  N
2afe6 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a  ameContext sNC;.
2afe7 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a    Column *pCol;.
2afe8 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2afe9 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 78 70  ;.  int i;.  Exp
2afea 72 20 2a 70 3b 0a 20 20 73 74 72 75 63 74 20 45  r *p;.  struct E
2afeb 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61 3b  xprList_item *a;
2afec 0a 0a 20 20 61 73 73 65 72 74 28 20 70 53 65 6c  ..  assert( pSel
2afed 65 63 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ect!=0 );.  asse
2afee 72 74 28 20 28 70 53 65 6c 65 63 74 2d 3e 73 65  rt( (pSelect->se
2afef 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f  lFlags & SF_Reso
2aff0 6c 76 65 64 29 21 3d 30 20 29 3b 0a 20 20 61 73  lved)!=0 );.  as
2aff1 73 65 72 74 28 20 6e 43 6f 6c 3d 3d 70 53 65 6c  sert( nCol==pSel
2aff2 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  ect->pEList->nEx
2aff3 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  pr || db->malloc
2aff4 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20  Failed );.  if( 
2aff5 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2aff6 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d   ) return;.  mem
2aff7 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
2aff8 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43  eof(sNC));.  sNC
2aff9 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 65 6c  .pSrcList = pSel
2affa 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20 61 20 3d  ect->pSrc;.  a =
2affb 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
2affc 2d 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  ->a;.  for(i=0, 
2affd 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f  pCol=aCol; i<nCo
2affe 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b  l; i++, pCol++){
2afff 0a 20 20 20 20 70 20 3d 20 61 5b 69 5d 2e 70 45  .    p = a[i].pE
2b000 78 70 72 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a  xpr;.    pCol->z
2b001 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Type = sqlite3Db
2b002 53 74 72 44 75 70 28 64 62 2c 20 63 6f 6c 75 6d  StrDup(db, colum
2b003 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 30  nType(&sNC, p, 0
2b004 2c 20 30 2c 20 30 29 29 3b 0a 20 20 20 20 70 43  , 0, 0));.    pC
2b005 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73  ol->affinity = s
2b006 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
2b007 74 79 28 70 29 3b 0a 20 20 20 20 69 66 28 20 70  ty(p);.    if( p
2b008 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 30  Col->affinity==0
2b009 20 29 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74   ) pCol->affinit
2b00a 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  y = SQLITE_AFF_N
2b00b 4f 4e 45 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  ONE;.    pColl =
2b00c 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
2b00d 53 65 71 28 70 50 61 72 73 65 2c 20 70 29 3b 0a  Seq(pParse, p);.
2b00e 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b      if( pColl ){
2b00f 0a 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f  .      pCol->zCo
2b010 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  ll = sqlite3DbSt
2b011 72 44 75 70 28 64 62 2c 20 70 43 6f 6c 6c 2d 3e  rDup(db, pColl->
2b012 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20  zName);.    }.  
2b013 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e  }.}../*.** Given
2b014 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2b015 65 6e 74 2c 20 67 65 6e 65 72 61 74 65 20 61 20  ent, generate a 
2b016 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
2b017 74 68 61 74 20 64 65 73 63 72 69 62 65 73 0a 2a  that describes.*
2b018 2a 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  * the result set
2b019 20 6f 66 20 74 68 61 74 20 53 45 4c 45 43 54 2e   of that SELECT.
2b01a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2b01b 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65  TE Table *sqlite
2b01c 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65  3ResultSetOfSele
2b01d 63 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ct(Parse *pParse
2b01e 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63  , Select *pSelec
2b01f 74 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61  t){.  Table *pTa
2b020 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  b;.  sqlite3 *db
2b021 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2b022 20 69 6e 74 20 73 61 76 65 64 46 6c 61 67 73 3b   int savedFlags;
2b023 0a 0a 20 20 73 61 76 65 64 46 6c 61 67 73 20 3d  ..  savedFlags =
2b024 20 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 64 62   db->flags;.  db
2b025 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49  ->flags &= ~SQLI
2b026 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 3b  TE_FullColNames;
2b027 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20  .  db->flags |= 
2b028 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
2b029 61 6d 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 53  ames;.  sqlite3S
2b02a 65 6c 65 63 74 50 72 65 70 28 70 50 61 72 73 65  electPrep(pParse
2b02b 2c 20 70 53 65 6c 65 63 74 2c 20 30 29 3b 0a 20  , pSelect, 0);. 
2b02c 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
2b02d 72 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  r ) return 0;.  
2b02e 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 2d 3e  while( pSelect->
2b02f 70 50 72 69 6f 72 20 29 20 70 53 65 6c 65 63 74  pPrior ) pSelect
2b030 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69   = pSelect->pPri
2b031 6f 72 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20  or;.  db->flags 
2b032 3d 20 73 61 76 65 64 46 6c 61 67 73 3b 0a 20 20  = savedFlags;.  
2b033 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 44 62  pTab = sqlite3Db
2b034 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2b035 69 7a 65 6f 66 28 54 61 62 6c 65 29 20 29 3b 0a  izeof(Table) );.
2b036 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b    if( pTab==0 ){
2b037 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2b038 20 7d 0a 20 20 2f 2a 20 54 68 65 20 73 71 6c 69   }.  /* The sqli
2b039 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65  te3ResultSetOfSe
2b03a 6c 65 63 74 28 29 20 69 73 20 6f 6e 6c 79 20 75  lect() is only u
2b03b 73 65 64 20 6e 20 63 6f 6e 74 65 78 74 73 20 77  sed n contexts w
2b03c 68 65 72 65 20 6c 6f 6f 6b 61 73 69 64 65 0a 20  here lookaside. 
2b03d 20 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64 2c   ** is disabled,
2b03e 20 73 6f 20 77 65 20 6d 69 67 68 74 20 61 73 20   so we might as 
2b03f 77 65 6c 6c 20 68 61 72 64 2d 63 6f 64 65 20 70  well hard-code p
2b040 54 61 62 2d 3e 64 62 4d 65 6d 20 74 6f 20 4e 55  Tab->dbMem to NU
2b041 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  LL. */.  assert(
2b042 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
2b043 45 6e 61 62 6c 65 64 3d 3d 30 20 29 3b 0a 20 20  Enabled==0 );.  
2b044 70 54 61 62 2d 3e 64 62 4d 65 6d 20 3d 20 30 3b  pTab->dbMem = 0;
2b045 0a 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20  .  pTab->nRef = 
2b046 31 3b 0a 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  1;.  pTab->zName
2b047 20 3d 20 30 3b 0a 20 20 73 65 6c 65 63 74 43 6f   = 0;.  selectCo
2b048 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69 73  lumnsFromExprLis
2b049 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63  t(pParse, pSelec
2b04a 74 2d 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62  t->pEList, &pTab
2b04b 2d 3e 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61  ->nCol, &pTab->a
2b04c 43 6f 6c 29 3b 0a 20 20 73 65 6c 65 63 74 41 64  Col);.  selectAd
2b04d 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f  dColumnTypeAndCo
2b04e 6c 6c 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  llation(pParse, 
2b04f 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54 61 62  pTab->nCol, pTab
2b050 2d 3e 61 43 6f 6c 2c 20 70 53 65 6c 65 63 74 29  ->aCol, pSelect)
2b051 3b 0a 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20  ;.  pTab->iPKey 
2b052 3d 20 2d 31 3b 0a 20 20 69 66 28 20 64 62 2d 3e  = -1;.  if( db->
2b053 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2b054 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
2b055 65 54 61 62 6c 65 28 70 54 61 62 29 3b 0a 20 20  eTable(pTab);.  
2b056 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
2b057 20 20 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d    return pTab;.}
2b058 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 56 44  ../*.** Get a VD
2b059 42 45 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  BE for the given
2b05a 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e   parser context.
2b05b 20 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 6f    Create a new o
2b05c 6e 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  ne if necessary.
2b05d 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20  .** If an error 
2b05e 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e  occurs, return N
2b05f 55 4c 4c 20 61 6e 64 20 6c 65 61 76 65 20 61 20  ULL and leave a 
2b060 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73  message in pPars
2b061 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
2b062 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74  VATE Vdbe *sqlit
2b063 65 33 47 65 74 56 64 62 65 28 50 61 72 73 65 20  e3GetVdbe(Parse 
2b064 2a 70 50 61 72 73 65 29 7b 0a 20 20 56 64 62 65  *pParse){.  Vdbe
2b065 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2b066 64 62 65 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20  dbe;.  if( v==0 
2b067 29 7b 0a 20 20 20 20 76 20 3d 20 70 50 61 72 73  ){.    v = pPars
2b068 65 2d 3e 70 56 64 62 65 20 3d 20 73 71 6c 69 74  e->pVdbe = sqlit
2b069 65 33 56 64 62 65 43 72 65 61 74 65 28 70 50 61  e3VdbeCreate(pPa
2b06a 72 73 65 2d 3e 64 62 29 3b 0a 23 69 66 6e 64 65  rse->db);.#ifnde
2b06b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
2b06c 41 43 45 0a 20 20 20 20 69 66 28 20 76 20 29 7b  ACE.    if( v ){
2b06d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b06e 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 54  beAddOp0(v, OP_T
2b06f 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  race);.    }.#en
2b070 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  dif.  }.  return
2b071 20 76 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f   v;.}.../*.** Co
2b072 6d 70 75 74 65 20 74 68 65 20 69 4c 69 6d 69 74  mpute the iLimit
2b073 20 61 6e 64 20 69 4f 66 66 73 65 74 20 66 69 65   and iOffset fie
2b074 6c 64 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43  lds of the SELEC
2b075 54 20 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a  T based on the.*
2b076 2a 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66  * pLimit and pOf
2b077 66 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73  fset expressions
2b078 2e 20 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f  .  pLimit and pO
2b079 66 66 73 65 74 20 68 6f 6c 64 20 74 68 65 20 65  ffset hold the e
2b07a 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68  xpressions.** th
2b07b 61 74 20 61 70 70 65 61 72 20 69 6e 20 74 68 65  at appear in the
2b07c 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74   original SQL st
2b07d 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 74 68  atement after th
2b07e 65 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53  e LIMIT and OFFS
2b07f 45 54 0a 2a 2a 20 6b 65 79 77 6f 72 64 73 2e 20  ET.** keywords. 
2b080 20 4f 72 20 4e 55 4c 4c 20 69 66 20 74 68 6f 73   Or NULL if thos
2b081 65 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 6f  e keywords are o
2b082 6d 69 74 74 65 64 2e 20 69 4c 69 6d 69 74 20 61  mitted. iLimit a
2b083 6e 64 20 69 4f 66 66 73 65 74 20 0a 2a 2a 20 61  nd iOffset .** a
2b084 72 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 6d  re the integer m
2b085 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20 6e  emory register n
2b086 75 6d 62 65 72 73 20 66 6f 72 20 63 6f 75 6e 74  umbers for count
2b087 65 72 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70  ers used to comp
2b088 75 74 65 20 0a 2a 2a 20 74 68 65 20 6c 69 6d 69  ute .** the limi
2b089 74 20 61 6e 64 20 6f 66 66 73 65 74 2e 20 20 49  t and offset.  I
2b08a 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6c 69  f there is no li
2b08b 6d 69 74 20 61 6e 64 2f 6f 72 20 6f 66 66 73 65  mit and/or offse
2b08c 74 2c 20 74 68 65 6e 20 0a 2a 2a 20 69 4c 69 6d  t, then .** iLim
2b08d 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20 61  it and iOffset a
2b08e 72 65 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a  re negative..**.
2b08f 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2b090 63 68 61 6e 67 65 73 20 74 68 65 20 76 61 6c 75  changes the valu
2b091 65 73 20 6f 66 20 69 4c 69 6d 69 74 20 61 6e 64  es of iLimit and
2b092 20 69 4f 66 66 73 65 74 20 6f 6e 6c 79 20 69 66   iOffset only if
2b093 0a 2a 2a 20 61 20 6c 69 6d 69 74 20 6f 72 20 6f  .** a limit or o
2b094 66 66 73 65 74 20 69 73 20 64 65 66 69 6e 65 64  ffset is defined
2b095 20 62 79 20 70 4c 69 6d 69 74 20 61 6e 64 20 70   by pLimit and p
2b096 4f 66 66 73 65 74 2e 20 20 69 4c 69 6d 69 74 20  Offset.  iLimit 
2b097 61 6e 64 0a 2a 2a 20 69 4f 66 66 73 65 74 20 73  and.** iOffset s
2b098 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20  hould have been 
2b099 70 72 65 73 65 74 20 74 6f 20 61 70 70 72 6f 70  preset to approp
2b09a 72 69 61 74 65 20 64 65 66 61 75 6c 74 20 76 61  riate default va
2b09b 6c 75 65 73 0a 2a 2a 20 28 75 73 75 61 6c 6c 79  lues.** (usually
2b09c 20 62 75 74 20 6e 6f 74 20 61 6c 77 61 79 73 20   but not always 
2b09d 2d 31 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c  -1) prior to cal
2b09e 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e  ling this routin
2b09f 65 2e 0a 2a 2a 20 4f 6e 6c 79 20 69 66 20 70 4c  e..** Only if pL
2b0a0 69 6d 69 74 21 3d 30 20 6f 72 20 70 4f 66 66 73  imit!=0 or pOffs
2b0a1 65 74 21 3d 30 20 64 6f 20 74 68 65 20 6c 69 6d  et!=0 do the lim
2b0a2 69 74 20 72 65 67 69 73 74 65 72 73 20 67 65 74  it registers get
2b0a3 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20  .** redefined.  
2b0a4 54 68 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70  The UNION ALL op
2b0a5 65 72 61 74 6f 72 20 75 73 65 73 20 74 68 69 73  erator uses this
2b0a6 20 70 72 6f 70 65 72 74 79 20 74 6f 20 66 6f 72   property to for
2b0a7 63 65 0a 2a 2a 20 74 68 65 20 72 65 75 73 65 20  ce.** the reuse 
2b0a8 6f 66 20 74 68 65 20 73 61 6d 65 20 6c 69 6d 69  of the same limi
2b0a9 74 20 61 6e 64 20 6f 66 66 73 65 74 20 72 65 67  t and offset reg
2b0aa 69 73 74 65 72 73 20 61 63 72 6f 73 73 20 6d 75  isters across mu
2b0ab 6c 74 69 70 6c 65 0a 2a 2a 20 53 45 4c 45 43 54  ltiple.** SELECT
2b0ac 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a   statements..*/.
2b0ad 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70  static void comp
2b0ae 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72  uteLimitRegister
2b0af 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  s(Parse *pParse,
2b0b0 20 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20   Select *p, int 
2b0b1 69 42 72 65 61 6b 29 7b 0a 20 20 56 64 62 65 20  iBreak){.  Vdbe 
2b0b2 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4c  *v = 0;.  int iL
2b0b3 69 6d 69 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20  imit = 0;.  int 
2b0b4 69 4f 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 61  iOffset;.  int a
2b0b5 64 64 72 31 2c 20 6e 3b 0a 20 20 69 66 28 20 70  ddr1, n;.  if( p
2b0b6 2d 3e 69 4c 69 6d 69 74 20 29 20 72 65 74 75 72  ->iLimit ) retur
2b0b7 6e 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20 22  n;..  /* .  ** "
2b0b8 4c 49 4d 49 54 20 2d 31 22 20 61 6c 77 61 79 73  LIMIT -1" always
2b0b9 20 73 68 6f 77 73 20 61 6c 6c 20 72 6f 77 73 2e   shows all rows.
2b0ba 20 20 54 68 65 72 65 20 69 73 20 73 6f 6d 65 0a    There is some.
2b0bb 20 20 2a 2a 20 63 6f 6e 74 72 61 76 65 72 73 79    ** contraversy
2b0bc 20 61 62 6f 75 74 20 77 68 61 74 20 74 68 65 20   about what the 
2b0bd 63 6f 72 72 65 63 74 20 62 65 68 61 76 69 6f 72  correct behavior
2b0be 20 73 68 6f 75 6c 64 20 62 65 2e 0a 20 20 2a 2a   should be..  **
2b0bf 20 54 68 65 20 63 75 72 72 65 6e 74 20 69 6d 70   The current imp
2b0c0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 65  lementation inte
2b0c1 72 70 72 65 74 73 20 22 4c 49 4d 49 54 20 30 22  rprets "LIMIT 0"
2b0c2 20 74 6f 20 6d 65 61 6e 0a 20 20 2a 2a 20 6e 6f   to mean.  ** no
2b0c3 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20 73 71   rows..  */.  sq
2b0c4 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
2b0c5 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 61  ear(pParse);.  a
2b0c6 73 73 65 72 74 28 20 70 2d 3e 70 4f 66 66 73 65  ssert( p->pOffse
2b0c7 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4c 69 6d 69  t==0 || p->pLimi
2b0c8 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d  t!=0 );.  if( p-
2b0c9 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 70  >pLimit ){.    p
2b0ca 2d 3e 69 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69  ->iLimit = iLimi
2b0cb 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  t = ++pParse->nM
2b0cc 65 6d 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69  em;.    v = sqli
2b0cd 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2b0ce 65 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45  e);.    if( NEVE
2b0cf 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  R(v==0) ) return
2b0d0 3b 20 20 2f 2a 20 56 44 42 45 20 73 68 6f 75 6c  ;  /* VDBE shoul
2b0d1 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  d have already b
2b0d2 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  een allocated */
2b0d3 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2b0d4 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 2d  ExprIsInteger(p-
2b0d5 3e 70 4c 69 6d 69 74 2c 20 26 6e 29 20 29 7b 0a  >pLimit, &n) ){.
2b0d6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b0d7 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2b0d8 74 65 67 65 72 2c 20 6e 2c 20 69 4c 69 6d 69 74  teger, n, iLimit
2b0d9 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2b0da 6d 65 6e 74 28 28 76 2c 20 22 4c 49 4d 49 54 20  ment((v, "LIMIT 
2b0db 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20 20 20  counter"));.    
2b0dc 20 20 69 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20    if( n==0 ){.  
2b0dd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b0de 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2b0df 74 6f 2c 20 30 2c 20 69 42 72 65 61 6b 29 3b 0a  to, 0, iBreak);.
2b0e0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
2b0e1 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
2b0e2 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2b0e3 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 69 4c 69 6d   p->pLimit, iLim
2b0e4 69 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  it);.      sqlit
2b0e5 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2b0e6 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 4c  OP_MustBeInt, iL
2b0e7 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 56 64 62  imit);.      Vdb
2b0e8 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c 49  eComment((v, "LI
2b0e9 4d 49 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a  MIT counter"));.
2b0ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b0eb 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
2b0ec 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 2c 20 69 42  Zero, iLimit, iB
2b0ed 72 65 61 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 20  reak);.    }.   
2b0ee 20 69 66 28 20 70 2d 3e 70 4f 66 66 73 65 74 20   if( p->pOffset 
2b0ef 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66  ){.      p->iOff
2b0f0 73 65 74 20 3d 20 69 4f 66 66 73 65 74 20 3d 20  set = iOffset = 
2b0f1 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2b0f2 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
2b0f3 65 6d 2b 2b 3b 20 20 20 2f 2a 20 41 6c 6c 6f 63  em++;   /* Alloc
2b0f4 61 74 65 20 61 6e 20 65 78 74 72 61 20 72 65 67  ate an extra reg
2b0f5 69 73 74 65 72 20 66 6f 72 20 6c 69 6d 69 74 2b  ister for limit+
2b0f6 6f 66 66 73 65 74 20 2a 2f 0a 20 20 20 20 20 20  offset */.      
2b0f7 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2b0f8 70 50 61 72 73 65 2c 20 70 2d 3e 70 4f 66 66 73  pParse, p->pOffs
2b0f9 65 74 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20 20  et, iOffset);.  
2b0fa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b0fb 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74  ddOp1(v, OP_Must
2b0fc 42 65 49 6e 74 2c 20 69 4f 66 66 73 65 74 29 3b  BeInt, iOffset);
2b0fd 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
2b0fe 6e 74 28 28 76 2c 20 22 4f 46 46 53 45 54 20 63  nt((v, "OFFSET c
2b0ff 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20 20 20 20  ounter"));.     
2b100 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33   addr1 = sqlite3
2b101 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2b102 5f 49 66 50 6f 73 2c 20 69 4f 66 66 73 65 74 29  _IfPos, iOffset)
2b103 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b104 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b105 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 4f 66 66  Integer, 0, iOff
2b106 73 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  set);.      sqli
2b107 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2b108 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 20 20 20  v, addr1);.     
2b109 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b10a 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c 20 69 4c  p3(v, OP_Add, iL
2b10b 69 6d 69 74 2c 20 69 4f 66 66 73 65 74 2c 20 69  imit, iOffset, i
2b10c 4f 66 66 73 65 74 2b 31 29 3b 0a 20 20 20 20 20  Offset+1);.     
2b10d 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2b10e 20 22 4c 49 4d 49 54 2b 4f 46 46 53 45 54 22 29   "LIMIT+OFFSET")
2b10f 29 3b 0a 20 20 20 20 20 20 61 64 64 72 31 20 3d  );.      addr1 =
2b110 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b111 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p1(v, OP_IfPos, 
2b112 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 73  iLimit);.      s
2b113 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b114 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2b115 2d 31 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b 0a  -1, iOffset+1);.
2b116 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b117 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
2b118 72 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  r1);.    }.  }.}
2b119 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2b11a 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
2b11b 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ELECT./*.** Retu
2b11c 72 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  rn the appropria
2b11d 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  te collating seq
2b11e 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 69 43  uence for the iC
2b11f 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 0a  ol-th column of.
2b120 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ** the result se
2b121 74 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 6f 75  t for the compou
2b122 6e 64 2d 73 65 6c 65 63 74 20 73 74 61 74 65 6d  nd-select statem
2b123 65 6e 74 20 22 70 22 2e 20 20 52 65 74 75 72 6e  ent "p".  Return
2b124 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20 74 68 65 20   NULL if.** the 
2b125 63 6f 6c 75 6d 6e 20 68 61 73 20 6e 6f 20 64 65  column has no de
2b126 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20  fault collating 
2b127 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a 2a 20  sequence..**.** 
2b128 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  The collating se
2b129 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 63  quence for the c
2b12a 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20 69  ompound select i
2b12b 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65  s taken from the
2b12c 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74 65  .** left-most te
2b12d 72 6d 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74  rm of the select
2b12e 20 74 68 61 74 20 68 61 73 20 61 20 63 6f 6c 6c   that has a coll
2b12f 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a  ating sequence..
2b130 2a 2f 0a 73 74 61 74 69 63 20 43 6f 6c 6c 53 65  */.static CollSe
2b131 71 20 2a 6d 75 6c 74 69 53 65 6c 65 63 74 43 6f  q *multiSelectCo
2b132 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70 50 61  llSeq(Parse *pPa
2b133 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 2c 20  rse, Select *p, 
2b134 69 6e 74 20 69 43 6f 6c 29 7b 0a 20 20 43 6f 6c  int iCol){.  Col
2b135 6c 53 65 71 20 2a 70 52 65 74 3b 0a 20 20 69 66  lSeq *pRet;.  if
2b136 28 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20  ( p->pPrior ){. 
2b137 20 20 20 70 52 65 74 20 3d 20 6d 75 6c 74 69 53     pRet = multiS
2b138 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50 61  electCollSeq(pPa
2b139 72 73 65 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20  rse, p->pPrior, 
2b13a 69 43 6f 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  iCol);.  }else{.
2b13b 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a 20 20      pRet = 0;.  
2b13c 7d 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c  }.  assert( iCol
2b13d 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 52 65  >=0 );.  if( pRe
2b13e 74 3d 3d 30 20 26 26 20 69 43 6f 6c 3c 70 2d 3e  t==0 && iCol<p->
2b13f 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b  pEList->nExpr ){
2b140 0a 20 20 20 20 70 52 65 74 20 3d 20 73 71 6c 69  .    pRet = sqli
2b141 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
2b142 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c 69 73 74  Parse, p->pEList
2b143 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 29  ->a[iCol].pExpr)
2b144 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
2b145 52 65 74 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  Ret;.}.#endif /*
2b146 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
2b147 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a  POUND_SELECT */.
2b148 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65  ./* Forward refe
2b149 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20  rence */.static 
2b14a 69 6e 74 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f  int multiSelectO
2b14b 72 64 65 72 42 79 28 0a 20 20 50 61 72 73 65 20  rderBy(.  Parse 
2b14c 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2b14d 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2b14e 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
2b14f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  p,            /*
2b150 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20   The right-most 
2b151 6f 66 20 53 45 4c 45 43 54 73 20 74 6f 20 62 65  of SELECTs to be
2b152 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65   coded */.  Sele
2b153 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 20 20  ctDest *pDest   
2b154 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20    /* What to do 
2b155 77 69 74 68 20 71 75 65 72 79 20 72 65 73 75 6c  with query resul
2b156 74 73 20 2a 2f 0a 29 3b 0a 0a 0a 23 69 66 6e 64  ts */.);...#ifnd
2b157 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
2b158 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 2f  OMPOUND_SELECT./
2b159 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2b15a 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 70  e is called to p
2b15b 72 6f 63 65 73 73 20 61 20 63 6f 6d 70 6f 75 6e  rocess a compoun
2b15c 64 20 71 75 65 72 79 20 66 6f 72 6d 20 66 72 6f  d query form fro
2b15d 6d 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65  m.** two or more
2b15e 20 73 65 70 61 72 61 74 65 20 71 75 65 72 69 65   separate querie
2b15f 73 20 75 73 69 6e 67 20 55 4e 49 4f 4e 2c 20 55  s using UNION, U
2b160 4e 49 4f 4e 20 41 4c 4c 2c 20 45 58 43 45 50 54  NION ALL, EXCEPT
2b161 2c 20 6f 72 0a 2a 2a 20 49 4e 54 45 52 53 45 43  , or.** INTERSEC
2b162 54 0a 2a 2a 0a 2a 2a 20 22 70 22 20 70 6f 69 6e  T.**.** "p" poin
2b163 74 73 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d  ts to the right-
2b164 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 77 6f 20  most of the two 
2b165 71 75 65 72 69 65 73 2e 20 20 74 68 65 20 71 75  queries.  the qu
2b166 65 72 79 20 6f 6e 20 74 68 65 0a 2a 2a 20 6c 65  ery on the.** le
2b167 66 74 20 69 73 20 70 2d 3e 70 50 72 69 6f 72 2e  ft is p->pPrior.
2b168 20 20 54 68 65 20 6c 65 66 74 20 71 75 65 72 79    The left query
2b169 20 63 6f 75 6c 64 20 61 6c 73 6f 20 62 65 20 61   could also be a
2b16a 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 0a   compound query.
2b16b 2a 2a 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  ** in which case
2b16c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
2b16d 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 72 65 63  ll be called rec
2b16e 75 72 73 69 76 65 6c 79 2e 20 0a 2a 2a 0a 2a 2a  ursively. .**.**
2b16f 20 54 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20   The results of 
2b170 74 68 65 20 74 6f 74 61 6c 20 71 75 65 72 79 20  the total query 
2b171 61 72 65 20 74 6f 20 62 65 20 77 72 69 74 74 65  are to be writte
2b172 6e 20 69 6e 74 6f 20 61 20 64 65 73 74 69 6e 61  n into a destina
2b173 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74 79 70 65 20  tion.** of type 
2b174 65 44 65 73 74 20 77 69 74 68 20 70 61 72 61 6d  eDest with param
2b175 65 74 65 72 20 69 50 61 72 6d 2e 0a 2a 2a 0a 2a  eter iParm..**.*
2b176 2a 20 45 78 61 6d 70 6c 65 20 31 3a 20 20 43 6f  * Example 1:  Co
2b177 6e 73 69 64 65 72 20 61 20 74 68 72 65 65 2d 77  nsider a three-w
2b178 61 79 20 63 6f 6d 70 6f 75 6e 64 20 53 51 4c 20  ay compound SQL 
2b179 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  statement..**.**
2b17a 20 20 20 20 20 53 45 4c 45 43 54 20 61 20 46 52       SELECT a FR
2b17b 4f 4d 20 74 31 20 55 4e 49 4f 4e 20 53 45 4c 45  OM t1 UNION SELE
2b17c 43 54 20 62 20 46 52 4f 4d 20 74 32 20 55 4e 49  CT b FROM t2 UNI
2b17d 4f 4e 20 53 45 4c 45 43 54 20 63 20 46 52 4f 4d  ON SELECT c FROM
2b17e 20 74 33 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73   t3.**.** This s
2b17f 74 61 74 65 6d 65 6e 74 20 69 73 20 70 61 72 73  tatement is pars
2b180 65 64 20 75 70 20 61 73 20 66 6f 6c 6c 6f 77 73  ed up as follows
2b181 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  :.**.**     SELE
2b182 43 54 20 63 20 46 52 4f 4d 20 74 33 0a 2a 2a 20  CT c FROM t3.** 
2b183 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20 60       |.**      `
2b184 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43 54 20 62  ----->  SELECT b
2b185 20 46 52 4f 4d 20 74 32 0a 2a 2a 20 20 20 20 20   FROM t2.**     
2b186 20 20 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20             |.** 
2b187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 60                 `
2b188 2d 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45 43 54 20  ------>  SELECT 
2b189 61 20 46 52 4f 4d 20 74 31 0a 2a 2a 0a 2a 2a 20  a FROM t1.**.** 
2b18a 54 68 65 20 61 72 72 6f 77 73 20 69 6e 20 74 68  The arrows in th
2b18b 65 20 64 69 61 67 72 61 6d 20 61 62 6f 76 65 20  e diagram above 
2b18c 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 53 65  represent the Se
2b18d 6c 65 63 74 2e 70 50 72 69 6f 72 20 70 6f 69 6e  lect.pPrior poin
2b18e 74 65 72 2e 0a 2a 2a 20 53 6f 20 69 66 20 74 68  ter..** So if th
2b18f 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
2b190 6c 6c 65 64 20 77 69 74 68 20 70 20 65 71 75 61  lled with p equa
2b191 6c 20 74 6f 20 74 68 65 20 74 33 20 71 75 65 72  l to the t3 quer
2b192 79 2c 20 74 68 65 6e 0a 2a 2a 20 70 50 72 69 6f  y, then.** pPrio
2b193 72 20 77 69 6c 6c 20 62 65 20 74 68 65 20 74 32  r will be the t2
2b194 20 71 75 65 72 79 2e 20 20 70 2d 3e 6f 70 20 77   query.  p->op w
2b195 69 6c 6c 20 62 65 20 54 4b 5f 55 4e 49 4f 4e 20  ill be TK_UNION 
2b196 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
2b197 0a 2a 2a 20 4e 6f 74 69 63 65 20 74 68 61 74 20  .** Notice that 
2b198 62 65 63 61 75 73 65 20 6f 66 20 74 68 65 20 77  because of the w
2b199 61 79 20 53 51 4c 69 74 65 20 70 61 72 73 65 73  ay SQLite parses
2b19a 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
2b19b 73 2c 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76 69  s, the.** indivi
2b19c 64 75 61 6c 20 73 65 6c 65 63 74 73 20 61 6c 77  dual selects alw
2b19d 61 79 73 20 67 72 6f 75 70 20 66 72 6f 6d 20 6c  ays group from l
2b19e 65 66 74 20 74 6f 20 72 69 67 68 74 2e 0a 2a 2f  eft to right..*/
2b19f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75 6c 74  .static int mult
2b1a0 69 53 65 6c 65 63 74 28 0a 20 20 50 61 72 73 65  iSelect(.  Parse
2b1a1 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2b1a2 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2b1a3 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
2b1a4 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  *p,            /
2b1a5 2a 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  * The right-most
2b1a6 20 6f 66 20 53 45 4c 45 43 54 73 20 74 6f 20 62   of SELECTs to b
2b1a7 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c  e coded */.  Sel
2b1a8 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 20 20  ectDest *pDest  
2b1a9 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f     /* What to do
2b1aa 20 77 69 74 68 20 71 75 65 72 79 20 72 65 73 75   with query resu
2b1ab 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  lts */.){.  int 
2b1ac 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
2b1ad 20 20 2f 2a 20 53 75 63 63 65 73 73 20 63 6f 64    /* Success cod
2b1ae 65 20 66 72 6f 6d 20 61 20 73 75 62 72 6f 75 74  e from a subrout
2b1af 69 6e 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ine */.  Select 
2b1b0 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 2f  *pPrior;       /
2b1b1 2a 20 41 6e 6f 74 68 65 72 20 53 45 4c 45 43 54  * Another SELECT
2b1b2 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20   immediately to 
2b1b3 6f 75 72 20 6c 65 66 74 20 2a 2f 0a 20 20 56 64  our left */.  Vd
2b1b4 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20  be *v;          
2b1b5 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
2b1b6 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 44 42  code to this VDB
2b1b7 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  E */.  SelectDes
2b1b8 74 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a 20  t dest;      /* 
2b1b9 41 6c 74 65 72 6e 61 74 69 76 65 20 64 61 74 61  Alternative data
2b1ba 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a   destination */.
2b1bb 20 20 53 65 6c 65 63 74 20 2a 70 44 65 6c 65 74    Select *pDelet
2b1bc 65 20 3d 20 30 3b 20 20 2f 2a 20 43 68 61 69 6e  e = 0;  /* Chain
2b1bd 20 6f 66 20 73 69 6d 70 6c 65 20 73 65 6c 65 63   of simple selec
2b1be 74 73 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a  ts to delete */.
2b1bf 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
2b1c0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2b1c1 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2b1c2 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  /..  /* Make sur
2b1c3 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20 4f 52  e there is no OR
2b1c4 44 45 52 20 42 59 20 6f 72 20 4c 49 4d 49 54 20  DER BY or LIMIT 
2b1c5 63 6c 61 75 73 65 20 6f 6e 20 70 72 69 6f 72 20  clause on prior 
2b1c6 53 45 4c 45 43 54 73 2e 20 20 4f 6e 6c 79 0a 20  SELECTs.  Only. 
2b1c7 20 2a 2a 20 74 68 65 20 6c 61 73 74 20 28 72 69   ** the last (ri
2b1c8 67 68 74 2d 6d 6f 73 74 29 20 53 45 4c 45 43 54  ght-most) SELECT
2b1c9 20 69 6e 20 74 68 65 20 73 65 72 69 65 73 20 6d   in the series m
2b1ca 61 79 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52  ay have an ORDER
2b1cb 20 42 59 20 6f 72 20 4c 49 4d 49 54 2e 0a 20 20   BY or LIMIT..  
2b1cc 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 20 26  */.  assert( p &
2b1cd 26 20 70 2d 3e 70 50 72 69 6f 72 20 29 3b 20 20  & p->pPrior );  
2b1ce 2f 2a 20 43 61 6c 6c 69 6e 67 20 66 75 6e 63 74  /* Calling funct
2b1cf 69 6f 6e 20 67 75 61 72 61 6e 74 65 65 73 20 74  ion guarantees t
2b1d0 68 69 73 20 6d 75 63 68 20 2a 2f 0a 20 20 64 62  his much */.  db
2b1d1 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2b1d2 20 70 50 72 69 6f 72 20 3d 20 70 2d 3e 70 50 72   pPrior = p->pPr
2b1d3 69 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ior;.  assert( p
2b1d4 50 72 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73  Prior->pRightmos
2b1d5 74 21 3d 70 50 72 69 6f 72 20 29 3b 0a 20 20 61  t!=pPrior );.  a
2b1d6 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70  ssert( pPrior->p
2b1d7 52 69 67 68 74 6d 6f 73 74 3d 3d 70 2d 3e 70 52  Rightmost==p->pR
2b1d8 69 67 68 74 6d 6f 73 74 20 29 3b 0a 20 20 64 65  ightmost );.  de
2b1d9 73 74 20 3d 20 2a 70 44 65 73 74 3b 0a 20 20 69  st = *pDest;.  i
2b1da 66 28 20 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65  f( pPrior->pOrde
2b1db 72 42 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  rBy ){.    sqlit
2b1dc 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2b1dd 65 2c 22 4f 52 44 45 52 20 42 59 20 63 6c 61 75  e,"ORDER BY clau
2b1de 73 65 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61  se should come a
2b1df 66 74 65 72 20 25 73 20 6e 6f 74 20 62 65 66 6f  fter %s not befo
2b1e0 72 65 22 2c 0a 20 20 20 20 20 20 73 65 6c 65 63  re",.      selec
2b1e1 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b  tOpName(p->op));
2b1e2 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20  .    rc = 1;.   
2b1e3 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
2b1e4 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 69 66  ct_end;.  }.  if
2b1e5 28 20 70 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74  ( pPrior->pLimit
2b1e6 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
2b1e7 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22  rrorMsg(pParse,"
2b1e8 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 73 68 6f  LIMIT clause sho
2b1e9 75 6c 64 20 63 6f 6d 65 20 61 66 74 65 72 20 25  uld come after %
2b1ea 73 20 6e 6f 74 20 62 65 66 6f 72 65 22 2c 0a 20  s not before",. 
2b1eb 20 20 20 20 20 73 65 6c 65 63 74 4f 70 4e 61 6d       selectOpNam
2b1ec 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72  e(p->op));.    r
2b1ed 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20  c = 1;.    goto 
2b1ee 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
2b1ef 3b 0a 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71 6c  ;.  }..  v = sql
2b1f0 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2b1f1 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76  se);.  assert( v
2b1f2 21 3d 30 20 29 3b 20 20 2f 2a 20 54 68 65 20 56  !=0 );  /* The V
2b1f3 44 42 45 20 61 6c 72 65 61 64 79 20 63 72 65 61  DBE already crea
2b1f4 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 20 66  ted by calling f
2b1f5 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a  unction */..  /*
2b1f6 20 43 72 65 61 74 65 20 74 68 65 20 64 65 73 74   Create the dest
2b1f7 69 6e 61 74 69 6f 6e 20 74 65 6d 70 6f 72 61 72  ination temporar
2b1f8 79 20 74 61 62 6c 65 20 69 66 20 6e 65 63 65 73  y table if neces
2b1f9 73 61 72 79 0a 20 20 2a 2f 0a 20 20 69 66 28 20  sary.  */.  if( 
2b1fa 64 65 73 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f  dest.eDest==SRT_
2b1fb 45 70 68 65 6d 54 61 62 20 29 7b 0a 20 20 20 20  EphemTab ){.    
2b1fc 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73  assert( p->pELis
2b1fd 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  t );.    sqlite3
2b1fe 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b1ff 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
2b200 64 65 73 74 2e 69 50 61 72 6d 2c 20 70 2d 3e 70  dest.iParm, p->p
2b201 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20  EList->nExpr);. 
2b202 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d 20     dest.eDest = 
2b203 53 52 54 5f 54 61 62 6c 65 3b 0a 20 20 7d 0a 0a  SRT_Table;.  }..
2b204 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61    /* Make sure a
2b205 6c 6c 20 53 45 4c 45 43 54 73 20 69 6e 20 74 68  ll SELECTs in th
2b206 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 76 65  e statement have
2b207 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72   the same number
2b208 20 6f 66 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a   of elements.  *
2b209 2a 20 69 6e 20 74 68 65 69 72 20 72 65 73 75 6c  * in their resul
2b20a 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a 20 20 61  t sets..  */.  a
2b20b 73 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74  ssert( p->pEList
2b20c 20 26 26 20 70 50 72 69 6f 72 2d 3e 70 45 4c 69   && pPrior->pELi
2b20d 73 74 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70  st );.  if( p->p
2b20e 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 70 50  EList->nExpr!=pP
2b20f 72 69 6f 72 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45  rior->pEList->nE
2b210 78 70 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  xpr ){.    sqlit
2b211 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2b212 65 2c 20 22 53 45 4c 45 43 54 73 20 74 6f 20 74  e, "SELECTs to t
2b213 68 65 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68  he left and righ
2b214 74 20 6f 66 20 25 73 22 0a 20 20 20 20 20 20 22  t of %s".      "
2b215 20 64 6f 20 6e 6f 74 20 68 61 76 65 20 74 68 65   do not have the
2b216 20 73 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20   same number of 
2b217 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 22 2c  result columns",
2b218 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d   selectOpName(p-
2b219 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  >op));.    rc = 
2b21a 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74  1;.    goto mult
2b21b 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
2b21c 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70 6f 75 6e 64  }..  /* Compound
2b21d 20 53 45 4c 45 43 54 73 20 74 68 61 74 20 68 61   SELECTs that ha
2b21e 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  ve an ORDER BY c
2b21f 6c 61 75 73 65 20 61 72 65 20 68 61 6e 64 6c 65  lause are handle
2b220 64 20 73 65 70 61 72 61 74 65 6c 79 2e 0a 20 20  d separately..  
2b221 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72 64  */.  if( p->pOrd
2b222 65 72 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75  erBy ){.    retu
2b223 72 6e 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72  rn multiSelectOr
2b224 64 65 72 42 79 28 70 50 61 72 73 65 2c 20 70 2c  derBy(pParse, p,
2b225 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a 20 20   pDest);.  }..  
2b226 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2b227 20 66 6f 72 20 74 68 65 20 6c 65 66 74 20 61 6e   for the left an
2b228 64 20 72 69 67 68 74 20 53 45 4c 45 43 54 20 73  d right SELECT s
2b229 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a  tatements..  */.
2b22a 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20    switch( p->op 
2b22b 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41  ){.    case TK_A
2b22c 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  LL: {.      int 
2b22d 61 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  addr = 0;.      
2b22e 61 73 73 65 72 74 28 20 21 70 50 72 69 6f 72 2d  assert( !pPrior-
2b22f 3e 70 4c 69 6d 69 74 20 29 3b 0a 20 20 20 20 20  >pLimit );.     
2b230 20 70 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20   pPrior->pLimit 
2b231 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20  = p->pLimit;.   
2b232 20 20 20 70 50 72 69 6f 72 2d 3e 70 4f 66 66 73     pPrior->pOffs
2b233 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65 74 3b  et = p->pOffset;
2b234 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
2b235 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
2b236 2c 20 70 50 72 69 6f 72 2c 20 26 64 65 73 74 29  , pPrior, &dest)
2b237 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69  ;.      p->pLimi
2b238 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  t = 0;.      p->
2b239 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20  pOffset = 0;.   
2b23a 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
2b23b 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f       goto multi_
2b23c 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
2b23d 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 70 50 72    }.      p->pPr
2b23e 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ior = 0;.      p
2b23f 2d 3e 69 4c 69 6d 69 74 20 3d 20 70 50 72 69 6f  ->iLimit = pPrio
2b240 72 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 20 20 20  r->iLimit;.     
2b241 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 70 50   p->iOffset = pP
2b242 72 69 6f 72 2d 3e 69 4f 66 66 73 65 74 3b 0a 20  rior->iOffset;. 
2b243 20 20 20 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d       if( p->iLim
2b244 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 61 64  it ){.        ad
2b245 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
2b246 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 5a  AddOp1(v, OP_IfZ
2b247 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d 69 74 29 3b  ero, p->iLimit);
2b248 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d  .        VdbeCom
2b249 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d 70 20 61  ment((v, "Jump a
2b24a 68 65 61 64 20 69 66 20 4c 49 4d 49 54 20 72 65  head if LIMIT re
2b24b 61 63 68 65 64 22 29 29 3b 0a 20 20 20 20 20 20  ached"));.      
2b24c 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  }.      rc = sql
2b24d 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
2b24e 65 2c 20 70 2c 20 26 64 65 73 74 29 3b 0a 20 20  e, p, &dest);.  
2b24f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63      testcase( rc
2b250 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
2b251 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70       pDelete = p
2b252 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  ->pPrior;.      
2b253 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  p->pPrior = pPri
2b254 6f 72 3b 0a 20 20 20 20 20 20 69 66 28 20 61 64  or;.      if( ad
2b255 64 72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  dr ){.        sq
2b256 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2b257 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20  e(v, addr);.    
2b258 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
2b259 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2b25a 54 4b 5f 45 58 43 45 50 54 3a 0a 20 20 20 20 63  TK_EXCEPT:.    c
2b25b 61 73 65 20 54 4b 5f 55 4e 49 4f 4e 3a 20 7b 0a  ase TK_UNION: {.
2b25c 20 20 20 20 20 20 69 6e 74 20 75 6e 69 6f 6e 54        int unionT
2b25d 61 62 3b 20 20 20 20 2f 2a 20 43 75 72 73 6f 72  ab;    /* Cursor
2b25e 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 74   number of the t
2b25f 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 68  emporary table h
2b260 6f 6c 64 69 6e 67 20 72 65 73 75 6c 74 20 2a 2f  olding result */
2b261 0a 20 20 20 20 20 20 75 38 20 6f 70 20 3d 20 30  .      u8 op = 0
2b262 3b 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f  ;       /* One o
2b263 66 20 74 68 65 20 53 52 54 5f 20 6f 70 65 72 61  f the SRT_ opera
2b264 74 69 6f 6e 73 20 74 6f 20 61 70 70 6c 79 20 74  tions to apply t
2b265 6f 20 73 65 6c 66 20 2a 2f 0a 20 20 20 20 20 20  o self */.      
2b266 69 6e 74 20 70 72 69 6f 72 4f 70 3b 20 20 20 20  int priorOp;    
2b267 20 2f 2a 20 54 68 65 20 53 52 54 5f 20 6f 70 65   /* The SRT_ ope
2b268 72 61 74 69 6f 6e 20 74 6f 20 61 70 70 6c 79 20  ration to apply 
2b269 74 6f 20 70 72 69 6f 72 20 73 65 6c 65 63 74 73  to prior selects
2b26a 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
2b26b 70 4c 69 6d 69 74 2c 20 2a 70 4f 66 66 73 65 74  pLimit, *pOffset
2b26c 3b 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65  ; /* Saved value
2b26d 73 20 6f 66 20 70 2d 3e 6e 4c 69 6d 69 74 20 61  s of p->nLimit a
2b26e 6e 64 20 70 2d 3e 6e 4f 66 66 73 65 74 20 2a 2f  nd p->nOffset */
2b26f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b  .      int addr;
2b270 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73  .      SelectDes
2b271 74 20 75 6e 69 6f 6e 64 65 73 74 3b 0a 0a 20 20  t uniondest;..  
2b272 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
2b273 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 29  >op==TK_EXCEPT )
2b274 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2b275 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 4e 49 4f  ( p->op==TK_UNIO
2b276 4e 20 29 3b 0a 20 20 20 20 20 20 70 72 69 6f 72  N );.      prior
2b277 4f 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a  Op = SRT_Union;.
2b278 20 20 20 20 20 20 69 66 28 20 64 65 73 74 2e 65        if( dest.e
2b279 44 65 73 74 3d 3d 70 72 69 6f 72 4f 70 20 26 26  Dest==priorOp &&
2b27a 20 41 4c 57 41 59 53 28 21 70 2d 3e 70 4c 69 6d   ALWAYS(!p->pLim
2b27b 69 74 20 26 26 21 70 2d 3e 70 4f 66 66 73 65 74  it &&!p->pOffset
2b27c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
2b27d 57 65 20 63 61 6e 20 72 65 75 73 65 20 61 20 74  We can reuse a t
2b27e 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 67  emporary table g
2b27f 65 6e 65 72 61 74 65 64 20 62 79 20 61 20 53 45  enerated by a SE
2b280 4c 45 43 54 20 74 6f 20 6f 75 72 0a 20 20 20 20  LECT to our.    
2b281 20 20 20 20 2a 2a 20 72 69 67 68 74 2e 0a 20 20      ** right..  
2b282 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2b283 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67   assert( p->pRig
2b284 68 74 6d 6f 73 74 21 3d 70 20 29 3b 20 20 2f 2a  htmost!=p );  /*
2b285 20 43 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e   Can only happen
2b286 20 66 6f 72 20 6c 65 66 74 77 61 72 64 20 65 6c   for leftward el
2b287 65 6d 65 6e 74 73 0a 20 20 20 20 20 20 20 20 20  ements.         
2b288 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b289 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f              ** o
2b28a 66 20 61 20 33 2d 77 61 79 20 6f 72 20 6d 6f 72  f a 3-way or mor
2b28b 65 20 63 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20  e compound */.  
2b28c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
2b28d 3e 70 4c 69 6d 69 74 3d 3d 30 20 29 3b 20 20 20  >pLimit==0 );   
2b28e 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65     /* Not allowe
2b28f 64 20 6f 6e 20 6c 65 66 74 77 61 72 64 20 65 6c  d on leftward el
2b290 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20  ements */.      
2b291 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 66    assert( p->pOf
2b292 66 73 65 74 3d 3d 30 20 29 3b 20 20 20 20 20 2f  fset==0 );     /
2b293 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e  * Not allowed on
2b294 20 6c 65 66 74 77 61 72 64 20 65 6c 65 6d 65 6e   leftward elemen
2b295 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e  ts */.        un
2b296 69 6f 6e 54 61 62 20 3d 20 64 65 73 74 2e 69 50  ionTab = dest.iP
2b297 61 72 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  arm;.      }else
2b298 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20  {.        /* We 
2b299 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 72 65  will need to cre
2b29a 61 74 65 20 6f 75 72 20 6f 77 6e 20 74 65 6d 70  ate our own temp
2b29b 6f 72 61 72 79 20 74 61 62 6c 65 20 74 6f 20 68  orary table to h
2b29c 6f 6c 64 20 74 68 65 0a 20 20 20 20 20 20 20 20  old the.        
2b29d 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20  ** intermediate 
2b29e 72 65 73 75 6c 74 73 2e 0a 20 20 20 20 20 20 20  results..       
2b29f 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 69 6f   */.        unio
2b2a0 6e 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e  nTab = pParse->n
2b2a1 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61  Tab++;.        a
2b2a2 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64 65 72  ssert( p->pOrder
2b2a3 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  By==0 );.       
2b2a4 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
2b2a5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b2a6 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 75  OpenEphemeral, u
2b2a7 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20 20  nionTab, 0);.   
2b2a8 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
2b2a9 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20  addrOpenEphm[0] 
2b2aa 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 20  == -1 );.       
2b2ab 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
2b2ac 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20 20  [0] = addr;.    
2b2ad 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73      p->pRightmos
2b2ae 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53  t->selFlags |= S
2b2af 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b  F_UsesEphemeral;
2b2b0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2b2b1 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20   p->pEList );.  
2b2b2 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
2b2b3 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54 20  Code the SELECT 
2b2b4 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 6f 75  statements to ou
2b2b5 72 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2f 0a  r left.      */.
2b2b6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
2b2b7 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20  Prior->pOrderBy 
2b2b8 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b2b9 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
2b2ba 75 6e 69 6f 6e 64 65 73 74 2c 20 70 72 69 6f 72  uniondest, prior
2b2bb 4f 70 2c 20 75 6e 69 6f 6e 54 61 62 29 3b 0a 20  Op, unionTab);. 
2b2bc 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2b2bd 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
2b2be 70 50 72 69 6f 72 2c 20 26 75 6e 69 6f 6e 64 65  pPrior, &unionde
2b2bf 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  st);.      if( r
2b2c0 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  c ){.        got
2b2c1 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
2b2c2 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  nd;.      }..   
2b2c3 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 63     /* Code the c
2b2c4 75 72 72 65 6e 74 20 53 45 4c 45 43 54 20 73 74  urrent SELECT st
2b2c5 61 74 65 6d 65 6e 74 0a 20 20 20 20 20 20 2a 2f  atement.      */
2b2c6 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6f 70  .      if( p->op
2b2c7 3d 3d 54 4b 5f 45 58 43 45 50 54 20 29 7b 0a 20  ==TK_EXCEPT ){. 
2b2c8 20 20 20 20 20 20 20 6f 70 20 3d 20 53 52 54 5f         op = SRT_
2b2c9 45 78 63 65 70 74 3b 0a 20 20 20 20 20 20 7d 65  Except;.      }e
2b2ca 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
2b2cb 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55  ert( p->op==TK_U
2b2cc 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 20 20  NION );.        
2b2cd 6f 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e 3b 0a  op = SRT_Union;.
2b2ce 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d        }.      p-
2b2cf 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20  >pPrior = 0;.   
2b2d0 20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70     pLimit = p->p
2b2d1 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e  Limit;.      p->
2b2d2 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
2b2d3 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70    pOffset = p->p
2b2d4 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d  Offset;.      p-
2b2d5 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  >pOffset = 0;.  
2b2d6 20 20 20 20 75 6e 69 6f 6e 64 65 73 74 2e 65 44      uniondest.eD
2b2d7 65 73 74 20 3d 20 6f 70 3b 0a 20 20 20 20 20 20  est = op;.      
2b2d8 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  rc = sqlite3Sele
2b2d9 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26 75  ct(pParse, p, &u
2b2da 6e 69 6f 6e 64 65 73 74 29 3b 0a 20 20 20 20 20  niondest);.     
2b2db 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53   testcase( rc!=S
2b2dc 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
2b2dd 20 20 2f 2a 20 51 75 65 72 79 20 66 6c 61 74 74    /* Query flatt
2b2de 65 6e 69 6e 67 20 69 6e 20 73 71 6c 69 74 65 33  ening in sqlite3
2b2df 53 65 6c 65 63 74 28 29 20 6d 69 67 68 74 20 72  Select() might r
2b2e0 65 66 69 6c 6c 20 70 2d 3e 70 4f 72 64 65 72 42  efill p->pOrderB
2b2e1 79 2e 0a 20 20 20 20 20 20 2a 2a 20 42 65 20 73  y..      ** Be s
2b2e2 75 72 65 20 74 6f 20 64 65 6c 65 74 65 20 70 2d  ure to delete p-
2b2e3 3e 70 4f 72 64 65 72 42 79 2c 20 74 68 65 72 65  >pOrderBy, there
2b2e4 66 6f 72 65 2c 20 74 6f 20 61 76 6f 69 64 20 61  fore, to avoid a
2b2e5 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 2e 20 2a 2f   memory leak. */
2b2e6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2b2e7 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
2b2e8 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20   p->pOrderBy);. 
2b2e9 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70       pDelete = p
2b2ea 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  ->pPrior;.      
2b2eb 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  p->pPrior = pPri
2b2ec 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 72  or;.      p->pOr
2b2ed 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20  derBy = 0;.     
2b2ee 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2b2ef 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74  te(db, p->pLimit
2b2f0 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  );.      p->pLim
2b2f1 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20  it = pLimit;.   
2b2f2 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20     p->pOffset = 
2b2f3 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70  pOffset;.      p
2b2f4 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->iLimit = 0;.  
2b2f5 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d      p->iOffset =
2b2f6 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f   0;..      /* Co
2b2f7 6e 76 65 72 74 20 74 68 65 20 64 61 74 61 20 69  nvert the data i
2b2f8 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  n the temporary 
2b2f9 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 61 74 65  table into whate
2b2fa 76 65 72 20 66 6f 72 6d 0a 20 20 20 20 20 20 2a  ver form.      *
2b2fb 2a 20 69 74 20 69 73 20 74 68 61 74 20 77 65 20  * it is that we 
2b2fc 63 75 72 72 65 6e 74 6c 79 20 6e 65 65 64 2e 0a  currently need..
2b2fd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61        */.      a
2b2fe 73 73 65 72 74 28 20 75 6e 69 6f 6e 54 61 62 3d  ssert( unionTab=
2b2ff 3d 64 65 73 74 2e 69 50 61 72 6d 20 7c 7c 20 64  =dest.iParm || d
2b300 65 73 74 2e 65 44 65 73 74 21 3d 70 72 69 6f 72  est.eDest!=prior
2b301 4f 70 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Op );.      if( 
2b302 64 65 73 74 2e 65 44 65 73 74 21 3d 70 72 69 6f  dest.eDest!=prio
2b303 72 4f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 69  rOp ){.        i
2b304 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b  nt iCont, iBreak
2b305 2c 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20  , iStart;.      
2b306 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c    assert( p->pEL
2b307 69 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 69  ist );.        i
2b308 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d 53  f( dest.eDest==S
2b309 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20  RT_Output ){.   
2b30a 20 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70         Select *p
2b30b 46 69 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 20  First = p;.     
2b30c 20 20 20 20 20 77 68 69 6c 65 28 20 70 46 69 72       while( pFir
2b30d 73 74 2d 3e 70 50 72 69 6f 72 20 29 20 70 46 69  st->pPrior ) pFi
2b30e 72 73 74 20 3d 20 70 46 69 72 73 74 2d 3e 70 50  rst = pFirst->pP
2b30f 72 69 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20  rior;.          
2b310 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61  generateColumnNa
2b311 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70  mes(pParse, 0, p
2b312 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a  First->pEList);.
2b313 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2b314 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74    iBreak = sqlit
2b315 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2b316 76 29 3b 0a 20 20 20 20 20 20 20 20 69 43 6f 6e  v);.        iCon
2b317 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  t = sqlite3VdbeM
2b318 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
2b319 20 20 20 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69       computeLimi
2b31a 74 52 65 67 69 73 74 65 72 73 28 70 50 61 72 73  tRegisters(pPars
2b31b 65 2c 20 70 2c 20 69 42 72 65 61 6b 29 3b 0a 20  e, p, iBreak);. 
2b31c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2b31d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2b31e 65 77 69 6e 64 2c 20 75 6e 69 6f 6e 54 61 62 2c  ewind, unionTab,
2b31f 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20   iBreak);.      
2b320 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74    iStart = sqlit
2b321 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2b322 72 28 76 29 3b 0a 20 20 20 20 20 20 20 20 73 65  r(v);.        se
2b323 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50  lectInnerLoop(pP
2b324 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69  arse, p, p->pELi
2b325 73 74 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 70 2d  st, unionTab, p-
2b326 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 0a  >pEList->nExpr,.
2b327 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b328 20 20 20 20 20 20 20 20 30 2c 20 2d 31 2c 20 26          0, -1, &
2b329 64 65 73 74 2c 20 69 43 6f 6e 74 2c 20 69 42 72  dest, iCont, iBr
2b32a 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71  eak);.        sq
2b32b 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
2b32c 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e 74 29 3b  Label(v, iCont);
2b32d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2b32e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b32f 5f 4e 65 78 74 2c 20 75 6e 69 6f 6e 54 61 62 2c  _Next, unionTab,
2b330 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20   iStart);.      
2b331 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2b332 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42 72  olveLabel(v, iBr
2b333 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 73 71  eak);.        sq
2b334 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b335 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 75 6e 69  v, OP_Close, uni
2b336 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 20  onTab, 0);.     
2b337 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
2b338 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
2b339 74 3a 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 70  t: assert( p->op
2b33a 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29  ==TK_INTERSECT )
2b33b 3b 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 61  ; {.      int ta
2b33c 62 31 2c 20 74 61 62 32 3b 0a 20 20 20 20 20 20  b1, tab2;.      
2b33d 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42 72 65 61  int iCont, iBrea
2b33e 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20  k, iStart;.     
2b33f 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 2a   Expr *pLimit, *
2b340 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69  pOffset;.      i
2b341 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 53  nt addr;.      S
2b342 65 6c 65 63 74 44 65 73 74 20 69 6e 74 65 72 73  electDest inters
2b343 65 63 74 64 65 73 74 3b 0a 20 20 20 20 20 20 69  ectdest;.      i
2b344 6e 74 20 72 31 3b 0a 0a 20 20 20 20 20 20 2f 2a  nt r1;..      /*
2b345 20 49 4e 54 45 52 53 45 43 54 20 69 73 20 64 69   INTERSECT is di
2b346 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65  fferent from the
2b347 20 6f 74 68 65 72 73 20 73 69 6e 63 65 20 69 74   others since it
2b348 20 72 65 71 75 69 72 65 73 0a 20 20 20 20 20 20   requires.      
2b349 2a 2a 20 74 77 6f 20 74 65 6d 70 6f 72 61 72 79  ** two temporary
2b34a 20 74 61 62 6c 65 73 2e 20 20 48 65 6e 63 65 20   tables.  Hence 
2b34b 69 74 20 68 61 73 20 69 74 73 20 6f 77 6e 20 63  it has its own c
2b34c 61 73 65 2e 20 20 42 65 67 69 6e 0a 20 20 20 20  ase.  Begin.    
2b34d 20 20 2a 2a 20 62 79 20 61 6c 6c 6f 63 61 74 69    ** by allocati
2b34e 6e 67 20 74 68 65 20 74 61 62 6c 65 73 20 77 65  ng the tables we
2b34f 20 77 69 6c 6c 20 6e 65 65 64 2e 0a 20 20 20 20   will need..    
2b350 20 20 2a 2f 0a 20 20 20 20 20 20 74 61 62 31 20    */.      tab1 
2b351 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
2b352 3b 0a 20 20 20 20 20 20 74 61 62 32 20 3d 20 70  ;.      tab2 = p
2b353 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
2b354 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
2b355 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 0a  pOrderBy==0 );..
2b356 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
2b357 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b358 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2b359 61 6c 2c 20 74 61 62 31 2c 20 30 29 3b 0a 20 20  al, tab1, 0);.  
2b35a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
2b35b 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d  ddrOpenEphm[0] =
2b35c 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d  = -1 );.      p-
2b35d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d  >addrOpenEphm[0]
2b35e 20 3d 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70   = addr;.      p
2b35f 2d 3e 70 52 69 67 68 74 6d 6f 73 74 2d 3e 73 65  ->pRightmost->se
2b360 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 55 73 65  lFlags |= SF_Use
2b361 73 45 70 68 65 6d 65 72 61 6c 3b 0a 20 20 20 20  sEphemeral;.    
2b362 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45 4c    assert( p->pEL
2b363 69 73 74 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ist );..      /*
2b364 20 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54   Code the SELECT
2b365 73 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 69 6e  s to our left in
2b366 74 6f 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  to temporary tab
2b367 6c 65 20 22 74 61 62 31 22 2e 0a 20 20 20 20 20  le "tab1"..     
2b368 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
2b369 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
2b36a 26 69 6e 74 65 72 73 65 63 74 64 65 73 74 2c 20  &intersectdest, 
2b36b 53 52 54 5f 55 6e 69 6f 6e 2c 20 74 61 62 31 29  SRT_Union, tab1)
2b36c 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
2b36d 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
2b36e 65 2c 20 70 50 72 69 6f 72 2c 20 26 69 6e 74 65  e, pPrior, &inte
2b36f 72 73 65 63 74 64 65 73 74 29 3b 0a 20 20 20 20  rsectdest);.    
2b370 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
2b371 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73      goto multi_s
2b372 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20  elect_end;.     
2b373 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64   }..      /* Cod
2b374 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 53 45  e the current SE
2b375 4c 45 43 54 20 69 6e 74 6f 20 74 65 6d 70 6f 72  LECT into tempor
2b376 61 72 79 20 74 61 62 6c 65 20 22 74 61 62 32 22  ary table "tab2"
2b377 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2b378 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
2b379 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f  beAddOp2(v, OP_O
2b37a 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 74 61  penEphemeral, ta
2b37b 62 32 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73  b2, 0);.      as
2b37c 73 65 72 74 28 20 70 2d 3e 61 64 64 72 4f 70 65  sert( p->addrOpe
2b37d 6e 45 70 68 6d 5b 31 5d 20 3d 3d 20 2d 31 20 29  nEphm[1] == -1 )
2b37e 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 72 4f  ;.      p->addrO
2b37f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 61 64 64  penEphm[1] = add
2b380 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 50 72 69  r;.      p->pPri
2b381 6f 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4c  or = 0;.      pL
2b382 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74  imit = p->pLimit
2b383 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69  ;.      p->pLimi
2b384 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 4f 66  t = 0;.      pOf
2b385 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66 73 65  fset = p->pOffse
2b386 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66  t;.      p->pOff
2b387 73 65 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  set = 0;.      i
2b388 6e 74 65 72 73 65 63 74 64 65 73 74 2e 69 50 61  ntersectdest.iPa
2b389 72 6d 20 3d 20 74 61 62 32 3b 0a 20 20 20 20 20  rm = tab2;.     
2b38a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c   rc = sqlite3Sel
2b38b 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26  ect(pParse, p, &
2b38c 69 6e 74 65 72 73 65 63 74 64 65 73 74 29 3b 0a  intersectdest);.
2b38d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2b38e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
2b38f 0a 20 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d  .      pDelete =
2b390 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20   p->pPrior;.    
2b391 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50    p->pPrior = pP
2b392 72 69 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69  rior;.      sqli
2b393 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2b394 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20  , p->pLimit);.  
2b395 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
2b396 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d  pLimit;.      p-
2b397 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66 66 73  >pOffset = pOffs
2b398 65 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 47 65  et;..      /* Ge
2b399 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74  nerate code to t
2b39a 61 6b 65 20 74 68 65 20 69 6e 74 65 72 73 65 63  ake the intersec
2b39b 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f 20  tion of the two 
2b39c 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 20 20  temporary.      
2b39d 2a 2a 20 74 61 62 6c 65 73 2e 0a 20 20 20 20 20  ** tables..     
2b39e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
2b39f 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20  ( p->pEList );. 
2b3a0 20 20 20 20 20 69 66 28 20 64 65 73 74 2e 65 44       if( dest.eD
2b3a1 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
2b3a2 29 7b 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63  ){.        Selec
2b3a3 74 20 2a 70 46 69 72 73 74 20 3d 20 70 3b 0a 20  t *pFirst = p;. 
2b3a4 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 46         while( pF
2b3a5 69 72 73 74 2d 3e 70 50 72 69 6f 72 20 29 20 70  irst->pPrior ) p
2b3a6 46 69 72 73 74 20 3d 20 70 46 69 72 73 74 2d 3e  First = pFirst->
2b3a7 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 20 20  pPrior;.        
2b3a8 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61  generateColumnNa
2b3a9 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70  mes(pParse, 0, p
2b3aa 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a  First->pEList);.
2b3ab 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 42        }.      iB
2b3ac 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64  reak = sqlite3Vd
2b3ad 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2b3ae 20 20 20 20 20 20 69 43 6f 6e 74 20 3d 20 73 71        iCont = sq
2b3af 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
2b3b0 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 63 6f 6d  el(v);.      com
2b3b1 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74 65  puteLimitRegiste
2b3b2 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69 42  rs(pParse, p, iB
2b3b3 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71 6c  reak);.      sql
2b3b4 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b3b5 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 74 61 62  , OP_Rewind, tab
2b3b6 31 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20  1, iBreak);.    
2b3b7 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
2b3b8 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2b3b9 3b 0a 20 20 20 20 20 20 69 53 74 61 72 74 20 3d  ;.      iStart =
2b3ba 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b3bb 70 32 28 76 2c 20 4f 50 5f 52 6f 77 4b 65 79 2c  p2(v, OP_RowKey,
2b3bc 20 74 61 62 31 2c 20 72 31 29 3b 0a 20 20 20 20   tab1, r1);.    
2b3bd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b3be 4f 70 34 49 6e 74 28 76 2c 20 4f 50 5f 4e 6f 74  Op4Int(v, OP_Not
2b3bf 46 6f 75 6e 64 2c 20 74 61 62 32 2c 20 69 43 6f  Found, tab2, iCo
2b3c0 6e 74 2c 20 72 31 2c 20 30 29 3b 0a 20 20 20 20  nt, r1, 0);.    
2b3c1 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2b3c2 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2b3c3 72 31 29 3b 0a 20 20 20 20 20 20 73 65 6c 65 63  r1);.      selec
2b3c4 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73  tInnerLoop(pPars
2b3c5 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c  e, p, p->pEList,
2b3c6 20 74 61 62 31 2c 20 70 2d 3e 70 45 4c 69 73 74   tab1, p->pEList
2b3c7 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20  ->nExpr,.       
2b3c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
2b3c9 2c 20 2d 31 2c 20 26 64 65 73 74 2c 20 69 43 6f  , -1, &dest, iCo
2b3ca 6e 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20  nt, iBreak);.   
2b3cb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
2b3cc 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43  solveLabel(v, iC
2b3cd 6f 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ont);.      sqli
2b3ce 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b3cf 20 4f 50 5f 4e 65 78 74 2c 20 74 61 62 31 2c 20   OP_Next, tab1, 
2b3d0 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20 73  iStart);.      s
2b3d1 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2b3d2 65 4c 61 62 65 6c 28 76 2c 20 69 42 72 65 61 6b  eLabel(v, iBreak
2b3d3 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2b3d4 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b3d5 5f 43 6c 6f 73 65 2c 20 74 61 62 32 2c 20 30 29  _Close, tab2, 0)
2b3d6 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b3d7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b3d8 43 6c 6f 73 65 2c 20 74 61 62 31 2c 20 30 29 3b  Close, tab1, 0);
2b3d9 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2b3da 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f    }.  }..  /* Co
2b3db 6d 70 75 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20  mpute collating 
2b3dc 73 65 71 75 65 6e 63 65 73 20 75 73 65 64 20 62  sequences used b
2b3dd 79 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72  y .  ** temporar
2b3de 79 20 74 61 62 6c 65 73 20 6e 65 65 64 65 64 20  y tables needed 
2b3df 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
2b3e0 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
2b3e1 2e 0a 20 20 2a 2a 20 41 74 74 61 63 68 20 74 68  ..  ** Attach th
2b3e2 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  e KeyInfo struct
2b3e3 75 72 65 20 74 6f 20 61 6c 6c 20 74 65 6d 70 6f  ure to all tempo
2b3e4 72 61 72 79 20 74 61 62 6c 65 73 2e 0a 20 20 2a  rary tables..  *
2b3e5 2a 0a 20 20 2a 2a 20 54 68 69 73 20 73 65 63 74  *.  ** This sect
2b3e6 69 6f 6e 20 69 73 20 72 75 6e 20 62 79 20 74 68  ion is run by th
2b3e7 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 53 45 4c  e right-most SEL
2b3e8 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e  ECT statement on
2b3e9 6c 79 2e 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20  ly..  ** SELECT 
2b3ea 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 74 68  statements to th
2b3eb 65 20 6c 65 66 74 20 61 6c 77 61 79 73 20 73 6b  e left always sk
2b3ec 69 70 20 74 68 69 73 20 70 61 72 74 2e 20 20 54  ip this part.  T
2b3ed 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20  he right-most.  
2b3ee 2a 2a 20 53 45 4c 45 43 54 20 6d 69 67 68 74 20  ** SELECT might 
2b3ef 61 6c 73 6f 20 73 6b 69 70 20 74 68 69 73 20 70  also skip this p
2b3f0 61 72 74 20 69 66 20 69 74 20 68 61 73 20 6e 6f  art if it has no
2b3f1 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2b3f2 20 61 6e 64 0a 20 20 2a 2a 20 6e 6f 20 74 65 6d   and.  ** no tem
2b3f3 70 20 74 61 62 6c 65 73 20 61 72 65 20 72 65 71  p tables are req
2b3f4 75 69 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  uired..  */.  if
2b3f5 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  ( p->selFlags & 
2b3f6 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c  SF_UsesEphemeral
2b3f7 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 20 20   ){.    int i;  
2b3f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b3f9 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2b3fa 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 4b 65 79  unter */.    Key
2b3fb 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
2b3fc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
2b3fd 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2b3fe 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20   for the result 
2b3ff 73 65 74 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63  set */.    Selec
2b400 74 20 2a 70 4c 6f 6f 70 3b 20 20 20 20 20 20 20  t *pLoop;       
2b401 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20           /* For 
2b402 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20  looping through 
2b403 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2b404 73 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71  s */.    CollSeq
2b405 20 2a 2a 61 70 43 6f 6c 6c 3b 20 20 20 20 20 20   **apColl;      
2b406 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
2b407 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 70 4b  oping through pK
2b408 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 5d 20  eyInfo->aColl[] 
2b409 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b  */.    int nCol;
2b40a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b40b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2b40c 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65 73  f columns in res
2b40d 75 6c 74 20 73 65 74 20 2a 2f 0a 0a 20 20 20 20  ult set */..    
2b40e 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68  assert( p->pRigh
2b40f 74 6d 6f 73 74 3d 3d 70 20 29 3b 0a 20 20 20 20  tmost==p );.    
2b410 6e 43 6f 6c 20 3d 20 70 2d 3e 70 45 4c 69 73 74  nCol = p->pEList
2b411 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 70 4b 65  ->nExpr;.    pKe
2b412 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44  yInfo = sqlite3D
2b413 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 0a  bMallocZero(db,.
2b414 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b415 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70         sizeof(*p
2b416 4b 65 79 49 6e 66 6f 29 2b 6e 43 6f 6c 2a 28 73  KeyInfo)+nCol*(s
2b417 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20  izeof(CollSeq*) 
2b418 2b 20 31 29 29 3b 0a 20 20 20 20 69 66 28 20 21  + 1));.    if( !
2b419 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20  pKeyInfo ){.    
2b41a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2b41b 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  MEM;.      goto 
2b41c 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
2b41d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4b 65  ;.    }..    pKe
2b41e 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43  yInfo->enc = ENC
2b41f 28 64 62 29 3b 0a 20 20 20 20 70 4b 65 79 49 6e  (db);.    pKeyIn
2b420 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31  fo->nField = (u1
2b421 36 29 6e 43 6f 6c 3b 0a 0a 20 20 20 20 66 6f 72  6)nCol;..    for
2b422 28 69 3d 30 2c 20 61 70 43 6f 6c 6c 3d 70 4b 65  (i=0, apColl=pKe
2b423 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b 20 69 3c  yInfo->aColl; i<
2b424 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 61 70 43 6f 6c  nCol; i++, apCol
2b425 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 2a 61 70 43  l++){.      *apC
2b426 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63  oll = multiSelec
2b427 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  tCollSeq(pParse,
2b428 20 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66   p, i);.      if
2b429 28 20 30 3d 3d 2a 61 70 43 6f 6c 6c 20 29 7b 0a  ( 0==*apColl ){.
2b42a 20 20 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20          *apColl 
2b42b 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b  = db->pDfltColl;
2b42c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
2b42d 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b      for(pLoop=p;
2b42e 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c   pLoop; pLoop=pL
2b42f 6f 6f 70 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20  oop->pPrior){.  
2b430 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32      for(i=0; i<2
2b431 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
2b432 69 6e 74 20 61 64 64 72 20 3d 20 70 4c 6f 6f 70  int addr = pLoop
2b433 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69  ->addrOpenEphm[i
2b434 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61  ];.        if( a
2b435 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ddr<0 ){.       
2b436 20 20 20 2f 2a 20 49 66 20 5b 30 5d 20 69 73 20     /* If [0] is 
2b437 75 6e 75 73 65 64 20 74 68 65 6e 20 5b 31 5d 20  unused then [1] 
2b438 69 73 20 61 6c 73 6f 20 75 6e 75 73 65 64 2e 20  is also unused. 
2b439 20 53 6f 20 77 65 20 63 61 6e 0a 20 20 20 20 20   So we can.     
2b43a 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 73       ** always s
2b43b 61 66 65 6c 79 20 61 62 6f 72 74 20 61 73 20 73  afely abort as s
2b43c 6f 6f 6e 20 61 73 20 74 68 65 20 66 69 72 73 74  oon as the first
2b43d 20 75 6e 75 73 65 64 20 73 6c 6f 74 20 69 73 20   unused slot is 
2b43e 66 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20  found */.       
2b43f 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 6f 70     assert( pLoop
2b440 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31  ->addrOpenEphm[1
2b441 5d 3c 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ]<0 );.         
2b442 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2b443 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
2b444 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
2b445 20 61 64 64 72 2c 20 6e 43 6f 6c 29 3b 0a 20 20   addr, nCol);.  
2b446 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b447 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
2b448 72 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e  r, (char*)pKeyIn
2b449 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b  fo, P4_KEYINFO);
2b44a 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e  .        pLoop->
2b44b 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 20  addrOpenEphm[i] 
2b44c 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = -1;.      }.  
2b44d 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
2b44e 62 46 72 65 65 28 64 62 2c 20 70 4b 65 79 49 6e  bFree(db, pKeyIn
2b44f 66 6f 29 3b 0a 20 20 7d 0a 0a 6d 75 6c 74 69 5f  fo);.  }..multi_
2b450 73 65 6c 65 63 74 5f 65 6e 64 3a 0a 20 20 70 44  select_end:.  pD
2b451 65 73 74 2d 3e 69 4d 65 6d 20 3d 20 64 65 73 74  est->iMem = dest
2b452 2e 69 4d 65 6d 3b 0a 20 20 70 44 65 73 74 2d 3e  .iMem;.  pDest->
2b453 6e 4d 65 6d 20 3d 20 64 65 73 74 2e 6e 4d 65 6d  nMem = dest.nMem
2b454 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
2b455 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44 65 6c  tDelete(db, pDel
2b456 65 74 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ete);.  return r
2b457 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  c;.}.#endif /* S
2b458 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f  QLITE_OMIT_COMPO
2b459 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f  UND_SELECT */../
2b45a 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 6f 75 74  *.** Code an out
2b45b 70 75 74 20 73 75 62 72 6f 75 74 69 6e 65 20 66  put subroutine f
2b45c 6f 72 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 69  or a coroutine i
2b45d 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
2b45e 20 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61   a.** SELECT sta
2b45f 74 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tment..**.** The
2b460 20 64 61 74 61 20 74 6f 20 62 65 20 6f 75 74 70   data to be outp
2b461 75 74 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20  ut is contained 
2b462 69 6e 20 70 49 6e 2d 3e 69 4d 65 6d 2e 20 20 54  in pIn->iMem.  T
2b463 68 65 72 65 20 61 72 65 0a 2a 2a 20 70 49 6e 2d  here are.** pIn-
2b464 3e 6e 4d 65 6d 20 63 6f 6c 75 6d 6e 73 20 74 6f  >nMem columns to
2b465 20 62 65 20 6f 75 74 70 75 74 2e 20 20 70 44 65   be output.  pDe
2b466 73 74 20 69 73 20 77 68 65 72 65 20 74 68 65 20  st is where the 
2b467 6f 75 74 70 75 74 20 73 68 6f 75 6c 64 0a 2a 2a  output should.**
2b468 20 62 65 20 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20   be sent..**.** 
2b469 72 65 67 52 65 74 75 72 6e 20 69 73 20 74 68 65  regReturn is the
2b46a 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72   number of the r
2b46b 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
2b46c 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 0a 2a  the subroutine.*
2b46d 2a 20 72 65 74 75 72 6e 20 61 64 64 72 65 73 73  * return address
2b46e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 50 72  ..**.** If regPr
2b46f 65 76 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20  ev>0 then it is 
2b470 61 20 74 68 65 20 66 69 72 73 74 20 72 65 67 69  a the first regi
2b471 73 74 65 72 20 69 6e 20 61 20 76 65 63 74 6f 72  ster in a vector
2b472 20 74 68 61 74 0a 2a 2a 20 72 65 63 6f 72 64 73   that.** records
2b473 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 6f 75   the previous ou
2b474 74 70 75 74 2e 20 20 6d 65 6d 5b 72 65 67 50 72  tput.  mem[regPr
2b475 65 76 5d 20 69 73 20 61 20 66 6c 61 67 20 74 68  ev] is a flag th
2b476 61 74 20 69 73 20 66 61 6c 73 65 0a 2a 2a 20 69  at is false.** i
2b477 66 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e  f there has been
2b478 20 6e 6f 20 70 72 65 76 69 6f 75 73 20 6f 75 74   no previous out
2b479 70 75 74 2e 20 20 49 66 20 72 65 67 50 72 65 76  put.  If regPrev
2b47a 3e 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 0a  >0 then code is.
2b47b 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20  ** generated to 
2b47c 73 75 70 70 72 65 73 73 20 64 75 70 6c 69 63 61  suppress duplica
2b47d 74 65 73 2e 20 20 70 4b 65 79 49 6e 66 6f 20 69  tes.  pKeyInfo i
2b47e 73 20 75 73 65 64 20 66 6f 72 20 63 6f 6d 70 61  s used for compa
2b47f 72 69 6e 67 0a 2a 2a 20 6b 65 79 73 2e 0a 2a 2a  ring.** keys..**
2b480 0a 2a 2a 20 49 66 20 74 68 65 20 4c 49 4d 49 54  .** If the LIMIT
2b481 20 66 6f 75 6e 64 20 69 6e 20 70 2d 3e 69 4c 69   found in p->iLi
2b482 6d 69 74 20 69 73 20 72 65 61 63 68 65 64 2c 20  mit is reached, 
2b483 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
2b484 20 74 6f 0a 2a 2a 20 69 42 72 65 61 6b 2e 0a 2a   to.** iBreak..*
2b485 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 6e  /.static int gen
2b486 65 72 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f  erateOutputSubro
2b487 75 74 69 6e 65 28 0a 20 20 50 61 72 73 65 20 2a  utine(.  Parse *
2b488 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2b489 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2b48a 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
2b48b 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
2b48c 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73   /* The SELECT s
2b48d 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65  tatement */.  Se
2b48e 6c 65 63 74 44 65 73 74 20 2a 70 49 6e 2c 20 20  lectDest *pIn,  
2b48f 20 20 20 20 20 20 2f 2a 20 43 6f 72 6f 75 74 69        /* Corouti
2b490 6e 65 20 73 75 70 70 6c 79 69 6e 67 20 64 61 74  ne supplying dat
2b491 61 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  a */.  SelectDes
2b492 74 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f  t *pDest,      /
2b493 2a 20 57 68 65 72 65 20 74 6f 20 73 65 6e 64 20  * Where to send 
2b494 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  the data */.  in
2b495 74 20 72 65 67 52 65 74 75 72 6e 2c 20 20 20 20  t regReturn,    
2b496 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74        /* The ret
2b497 75 72 6e 20 61 64 64 72 65 73 73 20 72 65 67 69  urn address regi
2b498 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ster */.  int re
2b499 67 50 72 65 76 2c 20 20 20 20 20 20 20 20 20 20  gPrev,          
2b49a 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 72 65    /* Previous re
2b49b 73 75 6c 74 20 72 65 67 69 73 74 65 72 2e 20 20  sult register.  
2b49c 4e 6f 20 75 6e 69 71 75 65 6e 65 73 73 20 69 66  No uniqueness if
2b49d 20 30 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20   0 */.  KeyInfo 
2b49e 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20  *pKeyInfo,      
2b49f 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 72 69 6e 67  /* For comparing
2b4a0 20 77 69 74 68 20 70 72 65 76 69 6f 75 73 20 65   with previous e
2b4a1 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 70 34  ntry */.  int p4
2b4a2 74 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20  type,           
2b4a3 20 20 2f 2a 20 54 68 65 20 70 34 20 74 79 70 65    /* The p4 type
2b4a4 20 66 6f 72 20 70 4b 65 79 49 6e 66 6f 20 2a 2f   for pKeyInfo */
2b4a5 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20  .  int iBreak   
2b4a6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75             /* Ju
2b4a7 6d 70 20 68 65 72 65 20 69 66 20 77 65 20 68 69  mp here if we hi
2b4a8 74 20 74 68 65 20 4c 49 4d 49 54 20 2a 2f 0a 29  t the LIMIT */.)
2b4a9 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
2b4aa 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
2b4ab 6e 74 20 69 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  nt iContinue;.  
2b4ac 69 6e 74 20 61 64 64 72 3b 0a 0a 20 20 61 64 64  int addr;..  add
2b4ad 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  r = sqlite3VdbeC
2b4ae 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
2b4af 20 69 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c   iContinue = sql
2b4b0 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
2b4b1 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20 53 75 70 70  l(v);..  /* Supp
2b4b2 72 65 73 73 20 64 75 70 6c 69 63 61 74 65 73 20  ress duplicates 
2b4b3 66 6f 72 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50  for UNION, EXCEP
2b4b4 54 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54  T, and INTERSECT
2b4b5 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67   .  */.  if( reg
2b4b6 50 72 65 76 20 29 7b 0a 20 20 20 20 69 6e 74 20  Prev ){.    int 
2b4b7 6a 31 2c 20 6a 32 3b 0a 20 20 20 20 6a 31 20 3d  j1, j2;.    j1 =
2b4b8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b4b9 70 31 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20  p1(v, OP_IfNot, 
2b4ba 72 65 67 50 72 65 76 29 3b 0a 20 20 20 20 6a 32  regPrev);.    j2
2b4bb 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2b4bc 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61  dOp4(v, OP_Compa
2b4bd 72 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 72  re, pIn->iMem, r
2b4be 65 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e  egPrev+1, pIn->n
2b4bf 4d 65 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Mem,.           
2b4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4c1 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e     (char*)pKeyIn
2b4c2 66 6f 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 20  fo, p4type);.   
2b4c3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b4c4 70 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a  p3(v, OP_Jump, j
2b4c5 32 2b 32 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20  2+2, iContinue, 
2b4c6 6a 32 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74  j2+2);.    sqlit
2b4c7 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2b4c8 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , j1);.    sqlit
2b4c9 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79 28 70  e3ExprCodeCopy(p
2b4ca 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d  Parse, pIn->iMem
2b4cb 2c 20 72 65 67 50 72 65 76 2b 31 2c 20 70 49 6e  , regPrev+1, pIn
2b4cc 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 73 71 6c  ->nMem);.    sql
2b4cd 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b4ce 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
2b4cf 20 72 65 67 50 72 65 76 29 3b 0a 20 20 7d 0a 20   regPrev);.  }. 
2b4d0 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d   if( pParse->db-
2b4d1 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
2b4d2 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20  return 0;..  /* 
2b4d3 53 75 70 70 72 65 73 73 20 74 68 65 20 74 68 65  Suppress the the
2b4d4 20 66 69 72 73 74 20 4f 46 46 53 45 54 20 65 6e   first OFFSET en
2b4d5 74 72 69 65 73 20 69 66 20 74 68 65 72 65 20 69  tries if there i
2b4d6 73 20 61 6e 20 4f 46 46 53 45 54 20 63 6c 61 75  s an OFFSET clau
2b4d7 73 65 0a 20 20 2a 2f 0a 20 20 63 6f 64 65 4f 66  se.  */.  codeOf
2b4d8 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74  fset(v, p, iCont
2b4d9 69 6e 75 65 29 3b 0a 0a 20 20 73 77 69 74 63 68  inue);..  switch
2b4da 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20 29  ( pDest->eDest )
2b4db 7b 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74  {.    /* Store t
2b4dc 68 65 20 72 65 73 75 6c 74 20 61 73 20 64 61 74  he result as dat
2b4dd 61 20 75 73 69 6e 67 20 61 20 75 6e 69 71 75 65  a using a unique
2b4de 20 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20   key..    */.   
2b4df 20 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a   case SRT_Table:
2b4e0 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 70  .    case SRT_Ep
2b4e1 68 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20  hemTab: {.      
2b4e2 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33  int r1 = sqlite3
2b4e3 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2b4e4 65 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 32  e);.      int r2
2b4e5 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2b4e6 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2b4e7 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 44      testcase( pD
2b4e8 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2b4e9 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 74  Table );.      t
2b4ea 65 73 74 63 61 73 65 28 20 70 44 65 73 74 2d 3e  estcase( pDest->
2b4eb 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d  eDest==SRT_Ephem
2b4ec 54 61 62 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  Tab );.      sql
2b4ed 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2b4ee 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
2b4ef 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 49 6e 2d   pIn->iMem, pIn-
2b4f0 3e 6e 4d 65 6d 2c 20 72 31 29 3b 0a 20 20 20 20  >nMem, r1);.    
2b4f1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b4f2 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77  Op2(v, OP_NewRow
2b4f3 69 64 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  id, pDest->iParm
2b4f4 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  , r2);.      sql
2b4f5 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2b4f6 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 70 44 65  , OP_Insert, pDe
2b4f7 73 74 2d 3e 69 50 61 72 6d 2c 20 72 31 2c 20 72  st->iParm, r1, r
2b4f8 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
2b4f9 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2b4fa 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b   OPFLAG_APPEND);
2b4fb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
2b4fc 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2b4fd 72 73 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20  rse, r2);.      
2b4fe 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2b4ff 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
2b500 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2b501 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
2b502 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2b503 45 52 59 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  ERY.    /* If we
2b504 20 61 72 65 20 63 72 65 61 74 69 6e 67 20 61 20   are creating a 
2b505 73 65 74 20 66 6f 72 20 61 6e 20 22 65 78 70 72  set for an "expr
2b506 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29   IN (SELECT ...)
2b507 22 20 63 6f 6e 73 74 72 75 63 74 2c 0a 20 20 20  " construct,.   
2b508 20 2a 2a 20 74 68 65 6e 20 74 68 65 72 65 20 73   ** then there s
2b509 68 6f 75 6c 64 20 62 65 20 61 20 73 69 6e 67 6c  hould be a singl
2b50a 65 20 69 74 65 6d 20 6f 6e 20 74 68 65 20 73 74  e item on the st
2b50b 61 63 6b 2e 20 20 57 72 69 74 65 20 74 68 69 73  ack.  Write this
2b50c 0a 20 20 20 20 2a 2a 20 69 74 65 6d 20 69 6e 74  .    ** item int
2b50d 6f 20 74 68 65 20 73 65 74 20 74 61 62 6c 65 20  o the set table 
2b50e 77 69 74 68 20 62 6f 67 75 73 20 64 61 74 61 2e  with bogus data.
2b50f 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
2b510 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20 20   SRT_Set: {.    
2b511 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 20 20    int r1;.      
2b512 61 73 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65  assert( pIn->nMe
2b513 6d 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d  m==1 );.      p-
2b514 3e 61 66 66 69 6e 69 74 79 20 3d 20 0a 20 20 20  >affinity = .   
2b515 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d        sqlite3Com
2b516 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 2d 3e  pareAffinity(p->
2b517 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
2b518 70 72 2c 20 70 44 65 73 74 2d 3e 61 66 66 69 6e  pr, pDest->affin
2b519 69 74 79 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  ity);.      r1 =
2b51a 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2b51b 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
2b51c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b51d 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op4(v, OP_MakeRe
2b51e 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  cord, pIn->iMem,
2b51f 20 31 2c 20 72 31 2c 20 26 70 2d 3e 61 66 66 69   1, r1, &p->affi
2b520 6e 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20  nity, 1);.      
2b521 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2b522 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
2b523 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d  Parse, pIn->iMem
2b524 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
2b525 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b526 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70   OP_IdxInsert, p
2b527 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 72 31 29  Dest->iParm, r1)
2b528 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
2b529 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2b52a 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20  arse, r1);.     
2b52b 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23   break;.    }..#
2b52c 69 66 20 30 20 20 2f 2a 20 4e 65 76 65 72 20 6f  if 0  /* Never o
2b52d 63 63 75 72 73 20 6f 6e 20 61 6e 20 4f 52 44 45  ccurs on an ORDE
2b52e 52 20 42 59 20 71 75 65 72 79 20 2a 2f 0a 20 20  R BY query */.  
2b52f 20 20 2f 2a 20 49 66 20 61 6e 79 20 72 6f 77 20    /* If any row 
2b530 65 78 69 73 74 20 69 6e 20 74 68 65 20 72 65 73  exist in the res
2b531 75 6c 74 20 73 65 74 2c 20 72 65 63 6f 72 64 20  ult set, record 
2b532 74 68 61 74 20 66 61 63 74 20 61 6e 64 20 61 62  that fact and ab
2b533 6f 72 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ort..    */.    
2b534 63 61 73 65 20 53 52 54 5f 45 78 69 73 74 73 3a  case SRT_Exists:
2b535 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
2b536 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b537 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 70 44 65  _Integer, 1, pDe
2b538 73 74 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20  st->iParm);.    
2b539 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63    /* The LIMIT c
2b53a 6c 61 75 73 65 20 77 69 6c 6c 20 74 65 72 6d 69  lause will termi
2b53b 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f  nate the loop fo
2b53c 72 20 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72  r us */.      br
2b53d 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2b53e 66 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  f..    /* If thi
2b53f 73 20 69 73 20 61 20 73 63 61 6c 61 72 20 73 65  s is a scalar se
2b540 6c 65 63 74 20 74 68 61 74 20 69 73 20 70 61 72  lect that is par
2b541 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69  t of an expressi
2b542 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20  on, then.    ** 
2b543 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
2b544 73 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72  s in the appropr
2b545 69 61 74 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  iate memory cell
2b546 20 61 6e 64 20 62 72 65 61 6b 20 6f 75 74 0a 20   and break out. 
2b547 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63 61     ** of the sca
2b548 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20  n loop..    */. 
2b549 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d 3a     case SRT_Mem:
2b54a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
2b54b 20 70 49 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20 29 3b   pIn->nMem==1 );
2b54c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2b54d 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73  prCodeMove(pPars
2b54e 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 44  e, pIn->iMem, pD
2b54f 65 73 74 2d 3e 69 50 61 72 6d 2c 20 31 29 3b 0a  est->iParm, 1);.
2b550 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d        /* The LIM
2b551 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 6a  IT clause will j
2b552 75 6d 70 20 6f 75 74 20 6f 66 20 74 68 65 20 6c  ump out of the l
2b553 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20  oop for us */.  
2b554 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2b555 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64  .#endif /* #ifnd
2b556 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2b557 55 42 51 55 45 52 59 20 2a 2f 0a 0a 20 20 20 20  UBQUERY */..    
2b558 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 73 20 61  /* The results a
2b559 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73  re stored in a s
2b55a 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69 73  equence of regis
2b55b 74 65 72 73 0a 20 20 20 20 2a 2a 20 73 74 61 72  ters.    ** star
2b55c 74 69 6e 67 20 61 74 20 70 44 65 73 74 2d 3e 69  ting at pDest->i
2b55d 4d 65 6d 2e 20 20 54 68 65 6e 20 74 68 65 20 63  Mem.  Then the c
2b55e 6f 2d 72 6f 75 74 69 6e 65 20 79 69 65 6c 64 73  o-routine yields
2b55f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
2b560 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 3a  e SRT_Coroutine:
2b561 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 44 65   {.      if( pDe
2b562 73 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29 7b 0a 20  st->iMem==0 ){. 
2b563 20 20 20 20 20 20 20 70 44 65 73 74 2d 3e 69 4d         pDest->iM
2b564 65 6d 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  em = sqlite3GetT
2b565 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
2b566 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20   pIn->nMem);.   
2b567 20 20 20 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d       pDest->nMem
2b568 20 3d 20 70 49 6e 2d 3e 6e 4d 65 6d 3b 0a 20 20   = pIn->nMem;.  
2b569 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2b56a 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28  te3ExprCodeMove(
2b56b 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65  pParse, pIn->iMe
2b56c 6d 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20  m, pDest->iMem, 
2b56d 70 44 65 73 74 2d 3e 6e 4d 65 6d 29 3b 0a 20 20  pDest->nMem);.  
2b56e 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b56f 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
2b570 64 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29  d, pDest->iParm)
2b571 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2b572 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
2b573 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76  none of the abov
2b574 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  e, then the resu
2b575 6c 74 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d  lt destination m
2b576 75 73 74 20 62 65 0a 20 20 20 20 2a 2a 20 53 52  ust be.    ** SR
2b577 54 5f 4f 75 74 70 75 74 2e 20 20 54 68 69 73 20  T_Output.  This 
2b578 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72  routine is never
2b579 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 6e 79   called with any
2b57a 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 64 65   other.    ** de
2b57b 73 74 69 6e 61 74 69 6f 6e 20 6f 74 68 65 72 20  stination other 
2b57c 74 68 61 6e 20 74 68 65 20 6f 6e 65 73 20 68 61  than the ones ha
2b57d 6e 64 6c 65 64 20 61 62 6f 76 65 20 6f 72 20 53  ndled above or S
2b57e 52 54 5f 4f 75 74 70 75 74 2e 0a 20 20 20 20 2a  RT_Output..    *
2b57f 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 20 53 52 54  *.    ** For SRT
2b580 5f 4f 75 74 70 75 74 2c 20 72 65 73 75 6c 74 73  _Output, results
2b581 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61   are stored in a
2b582 20 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67   sequence of reg
2b583 69 73 74 65 72 73 2e 20 20 0a 20 20 20 20 2a 2a  isters.  .    **
2b584 20 54 68 65 6e 20 74 68 65 20 4f 50 5f 52 65 73   Then the OP_Res
2b585 75 6c 74 52 6f 77 20 6f 70 63 6f 64 65 20 69 73  ultRow opcode is
2b586 20 75 73 65 64 20 74 6f 20 63 61 75 73 65 20 73   used to cause s
2b587 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 74 6f  qlite3_step() to
2b588 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 74  .    ** return t
2b589 68 65 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 72  he next row of r
2b58a 65 73 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  esult..    */.  
2b58b 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
2b58c 20 20 20 61 73 73 65 72 74 28 20 70 44 65 73 74     assert( pDest
2b58d 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  ->eDest==SRT_Out
2b58e 70 75 74 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  put );.      sql
2b58f 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b590 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
2b591 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e  pIn->iMem, pIn->
2b592 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c  nMem);.      sql
2b593 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
2b594 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
2b595 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70  se, pIn->iMem, p
2b596 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20  In->nMem);.     
2b597 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2b598 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20  }..  /* Jump to 
2b599 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c  the end of the l
2b59a 6f 6f 70 20 69 66 20 74 68 65 20 4c 49 4d 49 54  oop if the LIMIT
2b59b 20 69 73 20 72 65 61 63 68 65 64 2e 0a 20 20 2a   is reached..  *
2b59c 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69  /.  if( p->iLimi
2b59d 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
2b59e 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2b59f 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d  _IfZero, p->iLim
2b5a0 69 74 2c 20 69 42 72 65 61 6b 2c 20 2d 31 29 3b  it, iBreak, -1);
2b5a1 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
2b5a2 61 74 65 20 74 68 65 20 73 75 62 72 6f 75 74 69  ate the subrouti
2b5a3 6e 65 20 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20  ne return.  */. 
2b5a4 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2b5a5 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e  lveLabel(v, iCon
2b5a6 74 69 6e 75 65 29 3b 0a 20 20 73 71 6c 69 74 65  tinue);.  sqlite
2b5a7 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2b5a8 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 52 65 74  P_Return, regRet
2b5a9 75 72 6e 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  urn);..  return 
2b5aa 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  addr;.}../*.** A
2b5ab 6c 74 65 72 6e 61 74 69 76 65 20 63 6f 6d 70 6f  lternative compo
2b5ac 75 6e 64 20 73 65 6c 65 63 74 20 63 6f 64 65 20  und select code 
2b5ad 67 65 6e 65 72 61 74 6f 72 20 66 6f 72 20 63 61  generator for ca
2b5ae 73 65 73 20 77 68 65 6e 20 74 68 65 72 65 0a 2a  ses when there.*
2b5af 2a 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  * is an ORDER BY
2b5b0 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 57   clause..**.** W
2b5b1 65 20 61 73 73 75 6d 65 20 61 20 71 75 65 72 79  e assume a query
2b5b2 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2b5b3 67 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  g form:.**.**   
2b5b4 20 20 20 3c 73 65 6c 65 63 74 41 3e 20 20 3c 6f     <selectA>  <o
2b5b5 70 65 72 61 74 6f 72 3e 20 20 3c 73 65 6c 65 63  perator>  <selec
2b5b6 74 42 3e 20 20 4f 52 44 45 52 20 42 59 20 3c 6f  tB>  ORDER BY <o
2b5b7 72 64 65 72 62 79 6c 69 73 74 3e 0a 2a 2a 0a 2a  rderbylist>.**.*
2b5b8 2a 20 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73 20  * <operator> is 
2b5b9 6f 6e 65 20 6f 66 20 55 4e 49 4f 4e 20 41 4c 4c  one of UNION ALL
2b5ba 2c 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c  , UNION, EXCEPT,
2b5bb 20 6f 72 20 49 4e 54 45 52 53 45 43 54 2e 20 20   or INTERSECT.  
2b5bc 54 68 65 20 69 64 65 61 0a 2a 2a 20 69 73 20 74  The idea.** is t
2b5bd 6f 20 63 6f 64 65 20 62 6f 74 68 20 3c 73 65 6c  o code both <sel
2b5be 65 63 74 41 3e 20 61 6e 64 20 3c 73 65 6c 65 63  ectA> and <selec
2b5bf 74 42 3e 20 77 69 74 68 20 74 68 65 20 4f 52 44  tB> with the ORD
2b5c0 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 73 0a  ER BY clause as.
2b5c1 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 73 2e 20  ** co-routines. 
2b5c2 20 54 68 65 6e 20 72 75 6e 20 74 68 65 20 63 6f   Then run the co
2b5c3 2d 72 6f 75 74 69 6e 65 73 20 69 6e 20 70 61 72  -routines in par
2b5c4 61 6c 6c 65 6c 20 61 6e 64 20 6d 65 72 67 65 20  allel and merge 
2b5c5 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69  the results.** i
2b5c6 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 2e 20  nto the output. 
2b5c7 20 49 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20   In addition to 
2b5c8 74 68 65 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e  the two coroutin
2b5c9 65 73 20 28 63 61 6c 6c 65 64 20 73 65 6c 65 63  es (called selec
2b5ca 74 41 20 61 6e 64 0a 2a 2a 20 73 65 6c 65 63 74  tA and.** select
2b5cb 42 29 20 74 68 65 72 65 20 61 72 65 20 37 20 73  B) there are 7 s
2b5cc 75 62 72 6f 75 74 69 6e 65 73 3a 0a 2a 2a 0a 2a  ubroutines:.**.*
2b5cd 2a 20 20 20 20 6f 75 74 41 3a 20 20 20 20 4d 6f  *    outA:    Mo
2b5ce 76 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f 66  ve the output of
2b5cf 20 74 68 65 20 73 65 6c 65 63 74 41 20 63 6f 72   the selectA cor
2b5d0 6f 75 74 69 6e 65 20 69 6e 74 6f 20 74 68 65 20  outine into the 
2b5d1 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20  output.**       
2b5d2 20 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f 6d        of the com
2b5d3 70 6f 75 6e 64 20 71 75 65 72 79 2e 0a 2a 2a 0a  pound query..**.
2b5d4 2a 2a 20 20 20 20 6f 75 74 42 3a 20 20 20 20 4d  **    outB:    M
2b5d5 6f 76 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f  ove the output o
2b5d6 66 20 74 68 65 20 73 65 6c 65 63 74 42 20 63 6f  f the selectB co
2b5d7 72 6f 75 74 69 6e 65 20 69 6e 74 6f 20 74 68 65  routine into the
2b5d8 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20   output.**      
2b5d9 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f         of the co
2b5da 6d 70 6f 75 6e 64 20 71 75 65 72 79 2e 20 20 28  mpound query.  (
2b5db 4f 6e 6c 79 20 67 65 6e 65 72 61 74 65 64 20 66  Only generated f
2b5dc 6f 72 20 55 4e 49 4f 4e 20 61 6e 64 0a 2a 2a 20  or UNION and.** 
2b5dd 20 20 20 20 20 20 20 20 20 20 20 20 55 4e 49 4f              UNIO
2b5de 4e 20 41 4c 4c 2e 20 20 45 58 43 45 50 54 20 61  N ALL.  EXCEPT a
2b5df 6e 64 20 49 4e 53 45 52 54 53 45 43 54 20 6e 65  nd INSERTSECT ne
2b5e0 76 65 72 20 6f 75 74 70 75 74 20 61 20 72 6f 77  ver output a row
2b5e1 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
2b5e2 20 20 20 20 20 61 70 70 65 61 72 73 20 6f 6e 6c       appears onl
2b5e3 79 20 69 6e 20 42 2e 29 0a 2a 2a 0a 2a 2a 20 20  y in B.).**.**  
2b5e4 20 20 41 6c 74 42 3a 20 20 20 20 43 61 6c 6c 65    AltB:    Calle
2b5e5 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73 20  d when there is 
2b5e6 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63  data from both c
2b5e7 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3c  oroutines and A<
2b5e8 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 65 71 42  B..**.**    AeqB
2b5e9 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e  :    Called when
2b5ea 20 74 68 65 72 65 20 69 73 20 64 61 74 61 20 66   there is data f
2b5eb 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74 69  rom both corouti
2b5ec 6e 65 73 20 61 6e 64 20 41 3d 3d 42 2e 0a 2a 2a  nes and A==B..**
2b5ed 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20 20 20 20  .**    AgtB:    
2b5ee 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72  Called when ther
2b5ef 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20 62  e is data from b
2b5f0 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20 61  oth coroutines a
2b5f1 6e 64 20 41 3e 42 2e 0a 2a 2a 0a 2a 2a 20 20 20  nd A>B..**.**   
2b5f2 20 45 6f 66 41 3a 20 20 20 20 43 61 6c 6c 65 64   EofA:    Called
2b5f3 20 77 68 65 6e 20 64 61 74 61 20 69 73 20 65 78   when data is ex
2b5f4 68 61 75 73 74 65 64 20 66 72 6f 6d 20 73 65 6c  hausted from sel
2b5f5 65 63 74 41 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45  ectA..**.**    E
2b5f6 6f 66 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77  ofB:    Called w
2b5f7 68 65 6e 20 64 61 74 61 20 69 73 20 65 78 68 61  hen data is exha
2b5f8 75 73 74 65 64 20 66 72 6f 6d 20 73 65 6c 65 63  usted from selec
2b5f9 74 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d  tB..**.** The im
2b5fa 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
2b5fb 74 68 65 20 6c 61 74 74 65 72 20 66 69 76 65 20  the latter five 
2b5fc 73 75 62 72 6f 75 74 69 6e 65 73 20 64 65 70 65  subroutines depe
2b5fd 6e 64 20 6f 6e 20 77 68 69 63 68 20 0a 2a 2a 20  nd on which .** 
2b5fe 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73 20 75 73  <operator> is us
2b5ff 65 64 3a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 20 20 20  ed:.**.**.**    
2b600 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41           UNION A
2b601 4c 4c 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e  LL         UNION
2b602 20 20 20 20 20 20 20 20 20 20 20 20 45 58 43 45              EXCE
2b603 50 54 20 20 20 20 20 20 20 20 20 20 49 4e 54 45  PT          INTE
2b604 52 53 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  RSECT.**        
2b605 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20    ------------- 
2b606 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
2b607 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --  ------------
2b608 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --  ------------
2b609 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 41 6c 74 42 3a  -----.**   AltB:
2b60a 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20     outA, nextA  
2b60b 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20      outA, nextA 
2b60c 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74        outA, next
2b60d 41 20 20 20 20 20 20 20 20 20 6e 65 78 74 41 0a  A         nextA.
2b60e 2a 2a 0a 2a 2a 20 20 20 41 65 71 42 3a 20 20 20  **.**   AeqB:   
2b60f 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
2b610 20 20 20 20 6e 65 78 74 41 20 20 20 20 20 20 20      nextA       
2b611 20 20 20 20 20 20 6e 65 78 74 41 20 20 20 20 20        nextA     
2b612 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 0a      outA, nextA.
2b613 2a 2a 0a 2a 2a 20 20 20 41 67 74 42 3a 20 20 20  **.**   AgtB:   
2b614 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20  outB, nextB     
2b615 20 6f 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20   outB, nextB    
2b616 20 20 20 20 20 20 6e 65 78 74 42 20 20 20 20 20        nextB     
2b617 20 20 20 20 20 20 20 6e 65 78 74 42 0a 2a 2a 0a         nextB.**.
2b618 2a 2a 20 20 20 45 6f 66 41 3a 20 20 20 6f 75 74  **   EofA:   out
2b619 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 6f 75  B, nextB      ou
2b61a 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 20  tB, nextB       
2b61b 20 20 20 68 61 6c 74 20 20 20 20 20 20 20 20 20     halt         
2b61c 20 20 20 20 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 20      halt.**.**  
2b61d 20 45 6f 66 42 3a 20 20 20 6f 75 74 41 2c 20 6e   EofB:   outA, n
2b61e 65 78 74 41 20 20 20 20 20 20 6f 75 74 41 2c 20  extA      outA, 
2b61f 6e 65 78 74 41 20 20 20 20 20 20 20 6f 75 74 41  nextA       outA
2b620 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20  , nextA         
2b621 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68  halt.**.** In th
2b622 65 20 41 6c 74 42 2c 20 41 65 71 42 2c 20 61 6e  e AltB, AeqB, an
2b623 64 20 41 67 74 42 20 73 75 62 72 6f 75 74 69 6e  d AgtB subroutin
2b624 65 73 2c 20 61 6e 20 45 4f 46 20 6f 6e 20 41 20  es, an EOF on A 
2b625 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 41 0a  following nextA.
2b626 2a 2a 20 63 61 75 73 65 73 20 61 6e 20 69 6d 6d  ** causes an imm
2b627 65 64 69 61 74 65 20 6a 75 6d 70 20 74 6f 20 45  ediate jump to E
2b628 6f 66 41 20 61 6e 64 20 61 6e 20 45 4f 46 20 6f  ofA and an EOF o
2b629 6e 20 42 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65  n B following ne
2b62a 78 74 42 20 63 61 75 73 65 73 0a 2a 2a 20 61 6e  xtB causes.** an
2b62b 20 69 6d 6d 65 64 69 61 74 65 20 6a 75 6d 70 20   immediate jump 
2b62c 74 6f 20 45 6f 66 42 2e 20 20 57 69 74 68 69 6e  to EofB.  Within
2b62d 20 45 6f 66 41 20 61 6e 64 20 45 6f 66 42 2c 20   EofA and EofB, 
2b62e 61 6e 64 20 45 4f 46 20 6f 6e 20 65 6e 74 72 79  and EOF on entry
2b62f 20 6f 72 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67   or.** following
2b630 20 6e 65 78 74 58 20 63 61 75 73 65 73 20 61 20   nextX causes a 
2b631 6a 75 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20  jump to the end 
2b632 6f 66 20 74 68 65 20 73 65 6c 65 63 74 20 70 72  of the select pr
2b633 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  ocessing..**.** 
2b634 44 75 70 6c 69 63 61 74 65 20 72 65 6d 6f 76 61  Duplicate remova
2b635 6c 20 69 6e 20 74 68 65 20 55 4e 49 4f 4e 2c 20  l in the UNION, 
2b636 45 58 43 45 50 54 2c 20 61 6e 64 20 49 4e 54 45  EXCEPT, and INTE
2b637 52 53 45 43 54 20 63 61 73 65 73 20 69 73 20 68  RSECT cases is h
2b638 61 6e 64 6c 65 64 0a 2a 2a 20 77 69 74 68 69 6e  andled.** within
2b639 20 74 68 65 20 6f 75 74 70 75 74 20 73 75 62 72   the output subr
2b63a 6f 75 74 69 6e 65 2e 20 20 54 68 65 20 72 65 67  outine.  The reg
2b63b 50 72 65 76 20 72 65 67 69 73 74 65 72 20 73 65  Prev register se
2b63c 74 20 68 6f 6c 64 73 20 74 68 65 20 70 72 65 76  t holds the prev
2b63d 69 6f 75 73 6c 79 0a 2a 2a 20 6f 75 74 70 75 74  iously.** output
2b63e 20 76 61 6c 75 65 2e 20 20 41 20 63 6f 6d 70 61   value.  A compa
2b63f 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 20 61 67  rison is made ag
2b640 61 69 6e 73 74 20 74 68 69 73 20 76 61 6c 75 65  ainst this value
2b641 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0a   and the output.
2b642 2a 2a 20 69 73 20 73 6b 69 70 70 65 64 20 69 66  ** is skipped if
2b643 20 74 68 65 20 6e 65 78 74 20 72 65 73 75 6c 74   the next result
2b644 73 20 77 6f 75 6c 64 20 62 65 20 74 68 65 20 73  s would be the s
2b645 61 6d 65 20 61 73 20 74 68 65 20 70 72 65 76 69  ame as the previ
2b646 6f 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ous..**.** The i
2b647 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 70 6c  mplementation pl
2b648 61 6e 20 69 73 20 74 6f 20 69 6d 70 6c 65 6d 65  an is to impleme
2b649 6e 74 20 74 68 65 20 74 77 6f 20 63 6f 72 6f 75  nt the two corou
2b64a 74 69 6e 65 73 20 61 6e 64 20 73 65 76 65 6e 0a  tines and seven.
2b64b 2a 2a 20 73 75 62 72 6f 75 74 69 6e 65 73 20 66  ** subroutines f
2b64c 69 72 73 74 2c 20 74 68 65 6e 20 70 75 74 20 74  irst, then put t
2b64d 68 65 20 63 6f 6e 74 72 6f 6c 20 6c 6f 67 69 63  he control logic
2b64e 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 2e 20   at the bottom. 
2b64f 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   Like this:.**.*
2b650 2a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  *          goto 
2b651 49 6e 69 74 0a 2a 2a 20 20 20 20 20 63 6f 41 3a  Init.**     coA:
2b652 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c   coroutine for l
2b653 65 66 74 20 71 75 65 72 79 20 28 41 29 0a 2a 2a  eft query (A).**
2b654 20 20 20 20 20 63 6f 42 3a 20 63 6f 72 6f 75 74       coB: corout
2b655 69 6e 65 20 66 6f 72 20 72 69 67 68 74 20 71 75  ine for right qu
2b656 65 72 79 20 28 42 29 0a 2a 2a 20 20 20 20 6f 75  ery (B).**    ou
2b657 74 41 3a 20 6f 75 74 70 75 74 20 6f 6e 65 20 72  tA: output one r
2b658 6f 77 20 6f 66 20 41 0a 2a 2a 20 20 20 20 6f 75  ow of A.**    ou
2b659 74 42 3a 20 6f 75 74 70 75 74 20 6f 6e 65 20 72  tB: output one r
2b65a 6f 77 20 6f 66 20 42 20 28 55 4e 49 4f 4e 20 61  ow of B (UNION a
2b65b 6e 64 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 6e 6c  nd UNION ALL onl
2b65c 79 29 0a 2a 2a 20 20 20 20 45 6f 66 41 3a 20 2e  y).**    EofA: .
2b65d 2e 2e 0a 2a 2a 20 20 20 20 45 6f 66 42 3a 20 2e  ...**    EofB: .
2b65e 2e 2e 0a 2a 2a 20 20 20 20 41 6c 74 42 3a 20 2e  ...**    AltB: .
2b65f 2e 2e 0a 2a 2a 20 20 20 20 41 65 71 42 3a 20 2e  ...**    AeqB: .
2b660 2e 2e 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20 2e  ...**    AgtB: .
2b661 2e 2e 0a 2a 2a 20 20 20 20 49 6e 69 74 3a 20 69  ...**    Init: i
2b662 6e 69 74 69 61 6c 69 7a 65 20 63 6f 72 6f 75 74  nitialize corout
2b663 69 6e 65 20 72 65 67 69 73 74 65 72 73 0a 2a 2a  ine registers.**
2b664 20 20 20 20 20 20 20 20 20 20 79 69 65 6c 64 20            yield 
2b665 63 6f 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  coA.**          
2b666 69 66 20 65 6f 66 28 41 29 20 67 6f 74 6f 20 45  if eof(A) goto E
2b667 6f 66 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ofA.**          
2b668 79 69 65 6c 64 20 63 6f 42 0a 2a 2a 20 20 20 20  yield coB.**    
2b669 20 20 20 20 20 20 69 66 20 65 6f 66 28 42 29 20        if eof(B) 
2b66a 67 6f 74 6f 20 45 6f 66 42 0a 2a 2a 20 20 20 20  goto EofB.**    
2b66b 43 6d 70 72 3a 20 43 6f 6d 70 61 72 65 20 41 2c  Cmpr: Compare A,
2b66c 20 42 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4a   B.**          J
2b66d 75 6d 70 20 41 6c 74 42 2c 20 41 65 71 42 2c 20  ump AltB, AeqB, 
2b66e 41 67 74 42 0a 2a 2a 20 20 20 20 20 45 6e 64 3a  AgtB.**     End:
2b66f 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61   ....**.** We ca
2b670 6c 6c 20 41 6c 74 42 2c 20 41 65 71 42 2c 20 41  ll AltB, AeqB, A
2b671 67 74 42 2c 20 45 6f 66 41 2c 20 61 6e 64 20 45  gtB, EofA, and E
2b672 6f 66 42 20 22 73 75 62 72 6f 75 74 69 6e 65 73  ofB "subroutines
2b673 22 20 62 75 74 20 74 68 65 79 20 61 72 65 20 6e  " but they are n
2b674 6f 74 0a 2a 2a 20 61 63 74 75 61 6c 6c 79 20 63  ot.** actually c
2b675 61 6c 6c 65 64 20 75 73 69 6e 67 20 47 6f 73 75  alled using Gosu
2b676 62 20 61 6e 64 20 74 68 65 79 20 64 6f 20 6e 6f  b and they do no
2b677 74 20 52 65 74 75 72 6e 2e 20 20 45 6f 66 41 20  t Return.  EofA 
2b678 61 6e 64 20 45 6f 66 42 20 6c 6f 6f 70 0a 2a 2a  and EofB loop.**
2b679 20 75 6e 74 69 6c 20 61 6c 6c 20 64 61 74 61 20   until all data 
2b67a 69 73 20 65 78 68 61 75 73 74 65 64 20 74 68 65  is exhausted the
2b67b 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 22 65  n jump to the "e
2b67c 6e 64 22 20 6c 61 62 65 2e 20 20 41 6c 74 42 2c  nd" labe.  AltB,
2b67d 20 41 65 71 42 2c 0a 2a 2a 20 61 6e 64 20 41 67   AeqB,.** and Ag
2b67e 74 42 20 6a 75 6d 70 20 74 6f 20 65 69 74 68 65  tB jump to eithe
2b67f 72 20 4c 32 20 6f 72 20 74 6f 20 6f 6e 65 20 6f  r L2 or to one o
2b680 66 20 45 6f 66 41 20 6f 72 20 45 6f 66 42 2e 0a  f EofA or EofB..
2b681 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2b682 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
2b683 53 45 4c 45 43 54 0a 73 74 61 74 69 63 20 69 6e  SELECT.static in
2b684 74 20 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 64  t multiSelectOrd
2b685 65 72 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70  erBy(.  Parse *p
2b686 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a  Parse,        /*
2b687 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2b688 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
2b689 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2b68a 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66  he right-most of
2b68b 20 53 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63   SELECTs to be c
2b68c 6f 64 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74  oded */.  Select
2b68d 44 65 73 74 20 2a 70 44 65 73 74 20 20 20 20 20  Dest *pDest     
2b68e 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69  /* What to do wi
2b68f 74 68 20 71 75 65 72 79 20 72 65 73 75 6c 74 73  th query results
2b690 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20   */.){.  int i, 
2b691 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  j;             /
2b692 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
2b693 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72  */.  Select *pPr
2b694 69 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e  ior;       /* An
2b695 6f 74 68 65 72 20 53 45 4c 45 43 54 20 69 6d 6d  other SELECT imm
2b696 65 64 69 61 74 65 6c 79 20 74 6f 20 6f 75 72 20  ediately to our 
2b697 6c 65 66 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a  left */.  Vdbe *
2b698 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
2b699 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2b69a 20 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f   to this VDBE */
2b69b 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65  .  SelectDest de
2b69c 73 74 41 3b 20 20 20 20 20 2f 2a 20 44 65 73 74  stA;     /* Dest
2b69d 69 6e 61 74 69 6f 6e 20 66 6f 72 20 63 6f 72 6f  ination for coro
2b69e 75 74 69 6e 65 20 41 20 2a 2f 0a 20 20 53 65 6c  utine A */.  Sel
2b69f 65 63 74 44 65 73 74 20 64 65 73 74 42 3b 20 20  ectDest destB;  
2b6a0 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f     /* Destinatio
2b6a1 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69 6e 65 20  n for coroutine 
2b6a2 42 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 64  B */.  int regAd
2b6a3 64 72 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  drA;         /* 
2b6a4 41 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72  Address register
2b6a5 20 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 63 6f   for select-A co
2b6a6 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
2b6a7 20 72 65 67 45 6f 66 41 3b 20 20 20 20 20 20 20   regEofA;       
2b6a8 20 20 20 2f 2a 20 46 6c 61 67 20 74 6f 20 69 6e     /* Flag to in
2b6a9 64 69 63 61 74 65 20 77 68 65 6e 20 73 65 6c 65  dicate when sele
2b6aa 63 74 2d 41 20 69 73 20 63 6f 6d 70 6c 65 74 65  ct-A is complete
2b6ab 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 64 64   */.  int regAdd
2b6ac 72 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  rB;         /* A
2b6ad 64 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20  ddress register 
2b6ae 66 6f 72 20 73 65 6c 65 63 74 2d 42 20 63 6f 72  for select-B cor
2b6af 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
2b6b0 72 65 67 45 6f 66 42 3b 20 20 20 20 20 20 20 20  regEofB;        
2b6b1 20 20 2f 2a 20 46 6c 61 67 20 74 6f 20 69 6e 64    /* Flag to ind
2b6b2 69 63 61 74 65 20 77 68 65 6e 20 73 65 6c 65 63  icate when selec
2b6b3 74 2d 42 20 69 73 20 63 6f 6d 70 6c 65 74 65 20  t-B is complete 
2b6b4 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c  */.  int addrSel
2b6b5 65 63 74 41 3b 20 20 20 20 20 20 2f 2a 20 41 64  ectA;      /* Ad
2b6b6 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c  dress of the sel
2b6b7 65 63 74 2d 41 20 63 6f 72 6f 75 74 69 6e 65 20  ect-A coroutine 
2b6b8 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c  */.  int addrSel
2b6b9 65 63 74 42 3b 20 20 20 20 20 20 2f 2a 20 41 64  ectB;      /* Ad
2b6ba 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c  dress of the sel
2b6bb 65 63 74 2d 42 20 63 6f 72 6f 75 74 69 6e 65 20  ect-B coroutine 
2b6bc 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 75 74 41  */.  int regOutA
2b6bd 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  ;          /* Ad
2b6be 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66  dress register f
2b6bf 6f 72 20 74 68 65 20 6f 75 74 70 75 74 2d 41 20  or the output-A 
2b6c0 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20  subroutine */.  
2b6c1 69 6e 74 20 72 65 67 4f 75 74 42 3b 20 20 20 20  int regOutB;    
2b6c2 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
2b6c3 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 68   register for th
2b6c4 65 20 6f 75 74 70 75 74 2d 42 20 73 75 62 72 6f  e output-B subro
2b6c5 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61  utine */.  int a
2b6c6 64 64 72 4f 75 74 41 3b 20 20 20 20 20 20 20 20  ddrOutA;        
2b6c7 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
2b6c8 68 65 20 6f 75 74 70 75 74 2d 41 20 73 75 62 72  he output-A subr
2b6c9 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
2b6ca 61 64 64 72 4f 75 74 42 20 3d 20 30 3b 20 20 20  addrOutB = 0;   
2b6cb 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
2b6cc 74 68 65 20 6f 75 74 70 75 74 2d 42 20 73 75 62  the output-B sub
2b6cd 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
2b6ce 20 61 64 64 72 45 6f 66 41 3b 20 20 20 20 20 20   addrEofA;      
2b6cf 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
2b6d0 20 74 68 65 20 73 65 6c 65 63 74 2d 41 2d 65 78   the select-A-ex
2b6d1 68 61 75 73 74 65 64 20 73 75 62 72 6f 75 74 69  hausted subrouti
2b6d2 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
2b6d3 45 6f 66 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  EofB;         /*
2b6d4 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
2b6d5 73 65 6c 65 63 74 2d 42 2d 65 78 68 61 75 73 74  select-B-exhaust
2b6d6 65 64 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f  ed subroutine */
2b6d7 0a 20 20 69 6e 74 20 61 64 64 72 41 6c 74 42 3b  .  int addrAltB;
2b6d8 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
2b6d9 65 73 73 20 6f 66 20 74 68 65 20 41 3c 42 20 73  ess of the A<B s
2b6da 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  ubroutine */.  i
2b6db 6e 74 20 61 64 64 72 41 65 71 42 3b 20 20 20 20  nt addrAeqB;    
2b6dc 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
2b6dd 6f 66 20 74 68 65 20 41 3d 3d 42 20 73 75 62 72  of the A==B subr
2b6de 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
2b6df 61 64 64 72 41 67 74 42 3b 20 20 20 20 20 20 20  addrAgtB;       
2b6e0 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20    /* Address of 
2b6e1 74 68 65 20 41 3e 42 20 73 75 62 72 6f 75 74 69  the A>B subrouti
2b6e2 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c  ne */.  int regL
2b6e3 69 6d 69 74 41 3b 20 20 20 20 20 20 20 20 2f 2a  imitA;        /*
2b6e4 20 4c 69 6d 69 74 20 72 65 67 69 73 74 65 72 20   Limit register 
2b6e5 66 6f 72 20 73 65 6c 65 63 74 2d 41 20 2a 2f 0a  for select-A */.
2b6e6 20 20 69 6e 74 20 72 65 67 4c 69 6d 69 74 42 3b    int regLimitB;
2b6e7 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6d 69 74          /* Limit
2b6e8 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 73 65   register for se
2b6e9 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69 6e 74 20  lect-A */.  int 
2b6ea 72 65 67 50 72 65 76 3b 20 20 20 20 20 20 20 20  regPrev;        
2b6eb 20 20 2f 2a 20 41 20 72 61 6e 67 65 20 6f 66 20    /* A range of 
2b6ec 72 65 67 69 73 74 65 72 73 20 74 6f 20 68 6f 6c  registers to hol
2b6ed 64 20 70 72 65 76 69 6f 75 73 20 6f 75 74 70 75  d previous outpu
2b6ee 74 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64  t */.  int saved
2b6ef 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a 20  Limit;       /* 
2b6f0 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 70  Saved value of p
2b6f1 2d 3e 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 69 6e  ->iLimit */.  in
2b6f2 74 20 73 61 76 65 64 4f 66 66 73 65 74 3b 20 20  t savedOffset;  
2b6f3 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c      /* Saved val
2b6f4 75 65 20 6f 66 20 70 2d 3e 69 4f 66 66 73 65 74  ue of p->iOffset
2b6f5 20 2a 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c 43   */.  int labelC
2b6f6 6d 70 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  mpr;        /* L
2b6f7 61 62 65 6c 20 66 6f 72 20 74 68 65 20 73 74 61  abel for the sta
2b6f8 72 74 20 6f 66 20 74 68 65 20 6d 65 72 67 65 20  rt of the merge 
2b6f9 61 6c 67 6f 72 69 74 68 6d 20 2a 2f 0a 20 20 69  algorithm */.  i
2b6fa 6e 74 20 6c 61 62 65 6c 45 6e 64 3b 20 20 20 20  nt labelEnd;    
2b6fb 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f       /* Label fo
2b6fc 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  r the end of the
2b6fd 20 6f 76 65 72 61 6c 6c 20 53 45 4c 45 43 54 20   overall SELECT 
2b6fe 73 74 6d 74 20 2a 2f 0a 20 20 69 6e 74 20 6a 31  stmt */.  int j1
2b6ff 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2b700 2f 2a 20 4a 75 6d 70 20 69 6e 73 74 72 75 63 74  /* Jump instruct
2b701 69 6f 6e 73 20 74 68 61 74 20 67 65 74 20 72 65  ions that get re
2b702 74 61 72 67 65 74 74 65 64 20 2a 2f 0a 20 20 69  targetted */.  i
2b703 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  nt op;          
2b704 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54       /* One of T
2b705 4b 5f 41 4c 4c 2c 20 54 4b 5f 55 4e 49 4f 4e 2c  K_ALL, TK_UNION,
2b706 20 54 4b 5f 45 58 43 45 50 54 2c 20 54 4b 5f 49   TK_EXCEPT, TK_I
2b707 4e 54 45 52 53 45 43 54 20 2a 2f 0a 20 20 4b 65  NTERSECT */.  Ke
2b708 79 49 6e 66 6f 20 2a 70 4b 65 79 44 75 70 20 3d  yInfo *pKeyDup =
2b709 20 30 3b 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f   0; /* Compariso
2b70a 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  n information fo
2b70b 72 20 64 75 70 6c 69 63 61 74 65 20 72 65 6d 6f  r duplicate remo
2b70c 76 61 6c 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f  val */.  KeyInfo
2b70d 20 2a 70 4b 65 79 4d 65 72 67 65 3b 20 20 20 2f   *pKeyMerge;   /
2b70e 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 66  * Comparison inf
2b70f 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 6d 65 72  ormation for mer
2b710 67 69 6e 67 20 72 6f 77 73 20 2a 2f 0a 20 20 73  ging rows */.  s
2b711 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
2b712 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2b713 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
2b714 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65   ExprList *pOrde
2b715 72 42 79 3b 20 20 20 2f 2a 20 54 68 65 20 4f 52  rBy;   /* The OR
2b716 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
2b717 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b  .  int nOrderBy;
2b718 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2b719 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74  er of terms in t
2b71a 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2b71b 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65  se */.  int *aPe
2b71c 72 6d 75 74 65 3b 20 20 20 20 20 20 20 20 2f 2a  rmute;        /*
2b71d 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 4f 52   Mapping from OR
2b71e 44 45 52 20 42 59 20 74 65 72 6d 73 20 74 6f 20  DER BY terms to 
2b71f 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d  result set colum
2b720 6e 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ns */..  assert(
2b721 20 70 2d 3e 70 4f 72 64 65 72 42 79 21 3d 30 20   p->pOrderBy!=0 
2b722 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65  );.  assert( pKe
2b723 79 44 75 70 3d 3d 30 20 29 3b 20 2f 2a 20 22 4d  yDup==0 ); /* "M
2b724 61 6e 61 67 65 64 22 20 63 6f 64 65 20 6e 65 65  anaged" code nee
2b725 64 73 20 74 68 69 73 2e 20 20 54 69 63 6b 65 74  ds this.  Ticket
2b726 20 23 33 33 38 32 2e 20 2a 2f 0a 20 20 64 62 20   #3382. */.  db 
2b727 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
2b728 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2b729 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  e;.  assert( v!=
2b72a 30 20 29 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c  0 );       /* Al
2b72b 72 65 61 64 79 20 74 68 72 6f 77 6e 20 74 68 65  ready thrown the
2b72c 20 65 72 72 6f 72 20 69 66 20 56 44 42 45 20 61   error if VDBE a
2b72d 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a 20  lloc failed */. 
2b72e 20 6c 61 62 65 6c 45 6e 64 20 3d 20 73 71 6c 69   labelEnd = sqli
2b72f 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2b730 28 76 29 3b 0a 20 20 6c 61 62 65 6c 43 6d 70 72  (v);.  labelCmpr
2b731 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2b732 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 0a 20 20  keLabel(v);...  
2b733 2f 2a 20 50 61 74 63 68 20 75 70 20 74 68 65 20  /* Patch up the 
2b734 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 0a  ORDER BY clause.
2b735 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f    */.  op = p->o
2b736 70 3b 20 20 0a 20 20 70 50 72 69 6f 72 20 3d 20  p;  .  pPrior = 
2b737 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73 73  p->pPrior;.  ass
2b738 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 4f 72  ert( pPrior->pOr
2b739 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 70 4f  derBy==0 );.  pO
2b73a 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64  rderBy = p->pOrd
2b73b 65 72 42 79 3b 0a 20 20 61 73 73 65 72 74 28 20  erBy;.  assert( 
2b73c 70 4f 72 64 65 72 42 79 20 29 3b 0a 20 20 6e 4f  pOrderBy );.  nO
2b73d 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42  rderBy = pOrderB
2b73e 79 2d 3e 6e 45 78 70 72 3b 0a 0a 20 20 2f 2a 20  y->nExpr;..  /* 
2b73f 46 6f 72 20 6f 70 65 72 61 74 6f 72 73 20 6f 74  For operators ot
2b740 68 65 72 20 74 68 61 6e 20 55 4e 49 4f 4e 20 41  her than UNION A
2b741 4c 4c 20 77 65 20 68 61 76 65 20 74 6f 20 6d 61  LL we have to ma
2b742 6b 65 20 73 75 72 65 20 74 68 61 74 0a 20 20 2a  ke sure that.  *
2b743 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  * the ORDER BY c
2b744 6c 61 75 73 65 20 63 6f 76 65 72 73 20 65 76 65  lause covers eve
2b745 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 72  ry term of the r
2b746 65 73 75 6c 74 20 73 65 74 2e 20 20 41 64 64 0a  esult set.  Add.
2b747 20 20 2a 2a 20 74 65 72 6d 73 20 74 6f 20 74 68    ** terms to th
2b748 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2b749 65 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a  e as necessary..
2b74a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 21 3d 54    */.  if( op!=T
2b74b 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 66 6f 72  K_ALL ){.    for
2b74c 28 69 3d 31 3b 20 64 62 2d 3e 6d 61 6c 6c 6f 63  (i=1; db->malloc
2b74d 46 61 69 6c 65 64 3d 3d 30 20 26 26 20 69 3c 3d  Failed==0 && i<=
2b74e 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
2b74f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74  ; i++){.      st
2b750 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
2b751 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20  em *pItem;.     
2b752 20 66 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d 3d   for(j=0, pItem=
2b753 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 6a 3c 6e  pOrderBy->a; j<n
2b754 4f 72 64 65 72 42 79 3b 20 6a 2b 2b 2c 20 70 49  OrderBy; j++, pI
2b755 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  tem++){.        
2b756 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 69  assert( pItem->i
2b757 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  Col>0 );.       
2b758 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c   if( pItem->iCol
2b759 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==i ) break;.   
2b75a 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a     }.      if( j
2b75b 3d 3d 6e 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ==nOrderBy ){.  
2b75c 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77        Expr *pNew
2b75d 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
2b75e 62 2c 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30  b, TK_INTEGER, 0
2b75f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
2b760 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  New==0 ) return 
2b761 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2b762 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67        pNew->flag
2b763 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65  s |= EP_IntValue
2b764 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
2b765 75 2e 69 56 61 6c 75 65 20 3d 20 69 3b 0a 20 20  u.iValue = i;.  
2b766 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d        pOrderBy =
2b767 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2b768 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
2b769 4f 72 64 65 72 42 79 2c 20 70 4e 65 77 29 3b 0a  OrderBy, pNew);.
2b76a 20 20 20 20 20 20 20 20 70 4f 72 64 65 72 42 79          pOrderBy
2b76b 2d 3e 61 5b 6e 4f 72 64 65 72 42 79 2b 2b 5d 2e  ->a[nOrderBy++].
2b76c 69 43 6f 6c 20 3d 20 28 75 31 36 29 69 3b 0a 20  iCol = (u16)i;. 
2b76d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2b76e 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74  ..  /* Compute t
2b76f 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 70 65  he comparison pe
2b770 72 6d 75 74 61 74 69 6f 6e 20 61 6e 64 20 6b 65  rmutation and ke
2b771 79 69 6e 66 6f 20 74 68 61 74 20 69 73 20 75 73  yinfo that is us
2b772 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 74 68 65  ed with.  ** the
2b773 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 73 65   permutation use
2b774 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69  d to determine i
2b775 66 20 74 68 65 20 6e 65 78 74 0a 20 20 2a 2a 20  f the next.  ** 
2b776 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 73 20 63  row of results c
2b777 6f 6d 65 73 20 66 72 6f 6d 20 73 65 6c 65 63 74  omes from select
2b778 41 20 6f 72 20 73 65 6c 65 63 74 42 2e 20 20 41  A or selectB.  A
2b779 6c 73 6f 20 61 64 64 20 65 78 70 6c 69 63 69 74  lso add explicit
2b77a 0a 20 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 73  .  ** collations
2b77b 20 74 6f 20 74 68 65 20 4f 52 44 45 52 20 42 59   to the ORDER BY
2b77c 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 73 6f   clause terms so
2b77d 20 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 73   that when the s
2b77e 75 62 71 75 65 72 69 65 73 0a 20 20 2a 2a 20 74  ubqueries.  ** t
2b77f 6f 20 74 68 65 20 72 69 67 68 74 20 61 6e 64 20  o the right and 
2b780 74 68 65 20 6c 65 66 74 20 61 72 65 20 65 76 61  the left are eva
2b781 6c 75 61 74 65 64 2c 20 74 68 65 79 20 75 73 65  luated, they use
2b782 20 74 68 65 20 63 6f 72 72 65 63 74 0a 20 20 2a   the correct.  *
2b783 2a 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 20 20 2a  * collation..  *
2b784 2f 0a 20 20 61 50 65 72 6d 75 74 65 20 3d 20 73  /.  aPermute = s
2b785 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
2b786 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74  w(db, sizeof(int
2b787 29 2a 6e 4f 72 64 65 72 42 79 29 3b 0a 20 20 69  )*nOrderBy);.  i
2b788 66 28 20 61 50 65 72 6d 75 74 65 20 29 7b 0a 20  f( aPermute ){. 
2b789 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69     struct ExprLi
2b78a 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
2b78b 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74      for(i=0, pIt
2b78c 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20  em=pOrderBy->a; 
2b78d 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b 2c  i<nOrderBy; i++,
2b78e 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
2b78f 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e   assert( pItem->
2b790 69 43 6f 6c 3e 30 20 20 26 26 20 70 49 74 65 6d  iCol>0  && pItem
2b791 2d 3e 69 43 6f 6c 3c 3d 70 2d 3e 70 45 4c 69 73  ->iCol<=p->pELis
2b792 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20 20  t->nExpr );.    
2b793 20 20 61 50 65 72 6d 75 74 65 5b 69 5d 20 3d 20    aPermute[i] = 
2b794 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 2d 20 31 3b  pItem->iCol - 1;
2b795 0a 20 20 20 20 7d 0a 20 20 20 20 70 4b 65 79 4d  .    }.    pKeyM
2b796 65 72 67 65 20 3d 0a 20 20 20 20 20 20 73 71 6c  erge =.      sql
2b797 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
2b798 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79  db, sizeof(*pKey
2b799 4d 65 72 67 65 29 2b 6e 4f 72 64 65 72 42 79 2a  Merge)+nOrderBy*
2b79a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a  (sizeof(CollSeq*
2b79b 29 2b 31 29 29 3b 0a 20 20 20 20 69 66 28 20 70  )+1));.    if( p
2b79c 4b 65 79 4d 65 72 67 65 20 29 7b 0a 20 20 20 20  KeyMerge ){.    
2b79d 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61 53 6f    pKeyMerge->aSo
2b79e 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26  rtOrder = (u8*)&
2b79f 70 4b 65 79 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c  pKeyMerge->aColl
2b7a0 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20 20 20 20  [nOrderBy];.    
2b7a1 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 6e 46 69    pKeyMerge->nFi
2b7a2 65 6c 64 20 3d 20 28 75 31 36 29 6e 4f 72 64 65  eld = (u16)nOrde
2b7a3 72 42 79 3b 0a 20 20 20 20 20 20 70 4b 65 79 4d  rBy;.      pKeyM
2b7a4 65 72 67 65 2d 3e 65 6e 63 20 3d 20 45 4e 43 28  erge->enc = ENC(
2b7a5 64 62 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  db);.      for(i
2b7a6 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20  =0; i<nOrderBy; 
2b7a7 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 43 6f  i++){.        Co
2b7a8 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20  llSeq *pColl;.  
2b7a9 20 20 20 20 20 20 45 78 70 72 20 2a 70 54 65 72        Expr *pTer
2b7aa 6d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  m = pOrderBy->a[
2b7ab 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
2b7ac 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 66 6c 61    if( pTerm->fla
2b7ad 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
2b7ae 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  te ){.          
2b7af 70 43 6f 6c 6c 20 3d 20 70 54 65 72 6d 2d 3e 70  pColl = pTerm->p
2b7b0 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 7d 65  Coll;.        }e
2b7b1 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70  lse{.          p
2b7b2 43 6f 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65  Coll = multiSele
2b7b3 63 74 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  ctCollSeq(pParse
2b7b4 2c 20 70 2c 20 61 50 65 72 6d 75 74 65 5b 69 5d  , p, aPermute[i]
2b7b5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65  );.          pTe
2b7b6 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f  rm->flags |= EP_
2b7b7 45 78 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20  ExpCollate;.    
2b7b8 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 43 6f        pTerm->pCo
2b7b9 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  ll = pColl;.    
2b7ba 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4b      }.        pK
2b7bb 65 79 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c 5b 69  eyMerge->aColl[i
2b7bc 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  ] = pColl;.     
2b7bd 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61 53     pKeyMerge->aS
2b7be 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 4f  ortOrder[i] = pO
2b7bf 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f 72  rderBy->a[i].sor
2b7c0 74 4f 72 64 65 72 3b 0a 20 20 20 20 20 20 7d 0a  tOrder;.      }.
2b7c1 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
2b7c2 20 20 20 70 4b 65 79 4d 65 72 67 65 20 3d 20 30     pKeyMerge = 0
2b7c3 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 74  ;.  }..  /* Reat
2b7c4 74 61 63 68 20 74 68 65 20 4f 52 44 45 52 20 42  tach the ORDER B
2b7c5 59 20 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20  Y clause to the 
2b7c6 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 70 2d  query..  */.  p-
2b7c7 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64  >pOrderBy = pOrd
2b7c8 65 72 42 79 3b 0a 20 20 70 50 72 69 6f 72 2d 3e  erBy;.  pPrior->
2b7c9 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74  pOrderBy = sqlit
2b7ca 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 70 50  e3ExprListDup(pP
2b7cb 61 72 73 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72  arse->db, pOrder
2b7cc 42 79 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 41 6c  By, 0);..  /* Al
2b7cd 6c 6f 63 61 74 65 20 61 20 72 61 6e 67 65 20 6f  locate a range o
2b7ce 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69  f temporary regi
2b7cf 73 74 65 72 73 20 61 6e 64 20 74 68 65 20 4b 65  sters and the Ke
2b7d0 79 49 6e 66 6f 20 6e 65 65 64 65 64 0a 20 20 2a  yInfo needed.  *
2b7d1 2a 20 66 6f 72 20 74 68 65 20 6c 6f 67 69 63 20  * for the logic 
2b7d2 74 68 61 74 20 72 65 6d 6f 76 65 73 20 64 75 70  that removes dup
2b7d3 6c 69 63 61 74 65 20 72 65 73 75 6c 74 20 72 6f  licate result ro
2b7d4 77 73 20 77 68 65 6e 20 74 68 65 0a 20 20 2a 2a  ws when the.  **
2b7d5 20 6f 70 65 72 61 74 6f 72 20 69 73 20 55 4e 49   operator is UNI
2b7d6 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f 72 20 49  ON, EXCEPT, or I
2b7d7 4e 54 45 52 53 45 43 54 20 28 62 75 74 20 6e 6f  NTERSECT (but no
2b7d8 74 20 55 4e 49 4f 4e 20 41 4c 4c 29 2e 0a 20 20  t UNION ALL)..  
2b7d9 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  */.  if( op==TK_
2b7da 41 4c 4c 20 29 7b 0a 20 20 20 20 72 65 67 50 72  ALL ){.    regPr
2b7db 65 76 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  ev = 0;.  }else{
2b7dc 0a 20 20 20 20 69 6e 74 20 6e 45 78 70 72 20 3d  .    int nExpr =
2b7dd 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70   p->pEList->nExp
2b7de 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  r;.    assert( n
2b7df 4f 72 64 65 72 42 79 3e 3d 6e 45 78 70 72 20 7c  OrderBy>=nExpr |
2b7e0 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
2b7e1 65 64 20 29 3b 0a 20 20 20 20 72 65 67 50 72 65  ed );.    regPre
2b7e2 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  v = sqlite3GetTe
2b7e3 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
2b7e4 6e 45 78 70 72 2b 31 29 3b 0a 20 20 20 20 73 71  nExpr+1);.    sq
2b7e5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b7e6 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
2b7e7 2c 20 72 65 67 50 72 65 76 29 3b 0a 20 20 20 20  , regPrev);.    
2b7e8 70 4b 65 79 44 75 70 20 3d 20 73 71 6c 69 74 65  pKeyDup = sqlite
2b7e9 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2b7ea 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2b7eb 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79      sizeof(*pKey
2b7ec 44 75 70 29 20 2b 20 6e 45 78 70 72 2a 28 73 69  Dup) + nExpr*(si
2b7ed 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31  zeof(CollSeq*)+1
2b7ee 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4b 65  ) );.    if( pKe
2b7ef 79 44 75 70 20 29 7b 0a 20 20 20 20 20 20 70 4b  yDup ){.      pK
2b7f0 65 79 44 75 70 2d 3e 61 53 6f 72 74 4f 72 64 65  eyDup->aSortOrde
2b7f1 72 20 3d 20 28 75 38 2a 29 26 70 4b 65 79 44 75  r = (u8*)&pKeyDu
2b7f2 70 2d 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b  p->aColl[nExpr];
2b7f3 0a 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e  .      pKeyDup->
2b7f4 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 45  nField = (u16)nE
2b7f5 78 70 72 3b 0a 20 20 20 20 20 20 70 4b 65 79 44  xpr;.      pKeyD
2b7f6 75 70 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62  up->enc = ENC(db
2b7f7 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
2b7f8 3b 20 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  ; i<nExpr; i++){
2b7f9 0a 20 20 20 20 20 20 20 20 70 4b 65 79 44 75 70  .        pKeyDup
2b7fa 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 6d 75 6c  ->aColl[i] = mul
2b7fb 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28  tiSelectCollSeq(
2b7fc 70 50 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20  pParse, p, i);. 
2b7fd 20 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e         pKeyDup->
2b7fe 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20  aSortOrder[i] = 
2b7ff 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
2b800 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 53 65 70 61  .  }. .  /* Sepa
2b801 72 61 74 65 20 74 68 65 20 6c 65 66 74 20 61 6e  rate the left an
2b802 64 20 74 68 65 20 72 69 67 68 74 20 71 75 65 72  d the right quer
2b803 79 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68  y from one anoth
2b804 65 72 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 50 72  er.  */.  p->pPr
2b805 69 6f 72 20 3d 20 30 3b 0a 20 20 70 50 72 69 6f  ior = 0;.  pPrio
2b806 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  r->pRightmost = 
2b807 30 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f  0;.  sqlite3Reso
2b808 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28  lveOrderGroupBy(
2b809 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 4f  pParse, p, p->pO
2b80a 72 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29  rderBy, "ORDER")
2b80b 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e  ;.  if( pPrior->
2b80c 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20  pPrior==0 ){.   
2b80d 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f   sqlite3ResolveO
2b80e 72 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 72  rderGroupBy(pPar
2b80f 73 65 2c 20 70 50 72 69 6f 72 2c 20 70 50 72 69  se, pPrior, pPri
2b810 6f 72 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f  or->pOrderBy, "O
2b811 52 44 45 52 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f  RDER");.  }..  /
2b812 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6c 69  * Compute the li
2b813 6d 69 74 20 72 65 67 69 73 74 65 72 73 20 2a 2f  mit registers */
2b814 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52  .  computeLimitR
2b815 65 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c  egisters(pParse,
2b816 20 70 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20   p, labelEnd);. 
2b817 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 26   if( p->iLimit &
2b818 26 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a  & op==TK_ALL ){.
2b819 20 20 20 20 72 65 67 4c 69 6d 69 74 41 20 3d 20      regLimitA = 
2b81a 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2b81b 20 20 20 20 72 65 67 4c 69 6d 69 74 42 20 3d 20      regLimitB = 
2b81c 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2b81d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b81e 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79  ddOp2(v, OP_Copy
2b81f 2c 20 70 2d 3e 69 4f 66 66 73 65 74 20 3f 20 70  , p->iOffset ? p
2b820 2d 3e 69 4f 66 66 73 65 74 2b 31 20 3a 20 70 2d  ->iOffset+1 : p-
2b821 3e 69 4c 69 6d 69 74 2c 0a 20 20 20 20 20 20 20  >iLimit,.       
2b822 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b823 20 20 20 20 20 20 20 20 20 20 20 72 65 67 4c 69             regLi
2b824 6d 69 74 41 29 3b 0a 20 20 20 20 73 71 6c 69 74  mitA);.    sqlit
2b825 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b826 4f 50 5f 43 6f 70 79 2c 20 72 65 67 4c 69 6d 69  OP_Copy, regLimi
2b827 74 41 2c 20 72 65 67 4c 69 6d 69 74 42 29 3b 0a  tA, regLimitB);.
2b828 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 67    }else{.    reg
2b829 4c 69 6d 69 74 41 20 3d 20 72 65 67 4c 69 6d 69  LimitA = regLimi
2b82a 74 42 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  tB = 0;.  }.  sq
2b82b 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2b82c 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a  db, p->pLimit);.
2b82d 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b    p->pLimit = 0;
2b82e 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
2b82f 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66  lete(db, p->pOff
2b830 73 65 74 29 3b 0a 20 20 70 2d 3e 70 4f 66 66 73  set);.  p->pOffs
2b831 65 74 20 3d 20 30 3b 0a 0a 20 20 72 65 67 41 64  et = 0;..  regAd
2b832 64 72 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  drA = ++pParse->
2b833 6e 4d 65 6d 3b 0a 20 20 72 65 67 45 6f 66 41 20  nMem;.  regEofA 
2b834 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2b835 3b 0a 20 20 72 65 67 41 64 64 72 42 20 3d 20 2b  ;.  regAddrB = +
2b836 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
2b837 20 72 65 67 45 6f 66 42 20 3d 20 2b 2b 70 50 61   regEofB = ++pPa
2b838 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67  rse->nMem;.  reg
2b839 4f 75 74 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d  OutA = ++pParse-
2b83a 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 75 74 42  >nMem;.  regOutB
2b83b 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2b83c 6d 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  m;.  sqlite3Sele
2b83d 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
2b83e 41 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  A, SRT_Coroutine
2b83f 2c 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73  , regAddrA);.  s
2b840 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
2b841 49 6e 69 74 28 26 64 65 73 74 42 2c 20 53 52 54  Init(&destB, SRT
2b842 5f 43 6f 72 6f 75 74 69 6e 65 2c 20 72 65 67 41  _Coroutine, regA
2b843 64 64 72 42 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d  ddrB);..  /* Jum
2b844 70 20 70 61 73 74 20 74 68 65 20 76 61 72 69 6f  p past the vario
2b845 75 73 20 73 75 62 72 6f 75 74 69 6e 65 73 20 61  us subroutines a
2b846 6e 64 20 63 6f 72 6f 75 74 69 6e 65 73 20 74 6f  nd coroutines to
2b847 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 6d   the main.  ** m
2b848 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20  erge loop.  */. 
2b849 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j1 = sqlite3Vdb
2b84a 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f  eAddOp0(v, OP_Go
2b84b 74 6f 29 3b 0a 20 20 61 64 64 72 53 65 6c 65 63  to);.  addrSelec
2b84c 74 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  tA = sqlite3Vdbe
2b84d 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
2b84e 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
2b84f 61 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65  a coroutine to e
2b850 76 61 6c 75 61 74 65 20 74 68 65 20 53 45 4c 45  valuate the SELE
2b851 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  CT statement to 
2b852 74 68 65 0a 20 20 2a 2a 20 6c 65 66 74 20 6f 66  the.  ** left of
2b853 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 6f 70   the compound op
2b854 65 72 61 74 6f 72 20 2d 20 74 68 65 20 22 41 22  erator - the "A"
2b855 20 73 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20   select..  */.  
2b856 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28  VdbeNoopComment(
2b857 28 76 2c 20 22 42 65 67 69 6e 20 63 6f 72 6f 75  (v, "Begin corou
2b858 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 53 45  tine for left SE
2b859 4c 45 43 54 22 29 29 3b 0a 20 20 70 50 72 69 6f  LECT"));.  pPrio
2b85a 72 2d 3e 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c  r->iLimit = regL
2b85b 69 6d 69 74 41 3b 0a 20 20 73 71 6c 69 74 65 33  imitA;.  sqlite3
2b85c 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2b85d 50 72 69 6f 72 2c 20 26 64 65 73 74 41 29 3b 0a  Prior, &destA);.
2b85e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b85f 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2b860 72 2c 20 31 2c 20 72 65 67 45 6f 66 41 29 3b 0a  r, 1, regEofA);.
2b861 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b862 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2b863 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 56 64   regAddrA);.  Vd
2b864 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2b865 2c 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65  , "End coroutine
2b866 20 66 6f 72 20 6c 65 66 74 20 53 45 4c 45 43 54   for left SELECT
2b867 22 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  "));..  /* Gener
2b868 61 74 65 20 61 20 63 6f 72 6f 75 74 69 6e 65 20  ate a coroutine 
2b869 74 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20  to evaluate the 
2b86a 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2b86b 20 6f 6e 20 0a 20 20 2a 2a 20 74 68 65 20 72 69   on .  ** the ri
2b86c 67 68 74 20 2d 20 74 68 65 20 22 42 22 20 73 65  ght - the "B" se
2b86d 6c 65 63 74 0a 20 20 2a 2f 0a 20 20 61 64 64 72  lect.  */.  addr
2b86e 53 65 6c 65 63 74 42 20 3d 20 73 71 6c 69 74 65  SelectB = sqlite
2b86f 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2b870 28 76 29 3b 0a 20 20 56 64 62 65 4e 6f 6f 70 43  (v);.  VdbeNoopC
2b871 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 42 65 67 69  omment((v, "Begi
2b872 6e 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20  n coroutine for 
2b873 72 69 67 68 74 20 53 45 4c 45 43 54 22 29 29 3b  right SELECT"));
2b874 0a 20 20 73 61 76 65 64 4c 69 6d 69 74 20 3d 20  .  savedLimit = 
2b875 70 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 73 61 76  p->iLimit;.  sav
2b876 65 64 4f 66 66 73 65 74 20 3d 20 70 2d 3e 69 4f  edOffset = p->iO
2b877 66 66 73 65 74 3b 0a 20 20 70 2d 3e 69 4c 69 6d  ffset;.  p->iLim
2b878 69 74 20 3d 20 72 65 67 4c 69 6d 69 74 42 3b 0a  it = regLimitB;.
2b879 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30    p->iOffset = 0
2b87a 3b 20 20 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  ;  .  sqlite3Sel
2b87b 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26  ect(pParse, p, &
2b87c 64 65 73 74 42 29 3b 0a 20 20 70 2d 3e 69 4c 69  destB);.  p->iLi
2b87d 6d 69 74 20 3d 20 73 61 76 65 64 4c 69 6d 69 74  mit = savedLimit
2b87e 3b 0a 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d  ;.  p->iOffset =
2b87f 20 73 61 76 65 64 4f 66 66 73 65 74 3b 0a 20 20   savedOffset;.  
2b880 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b881 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2b882 20 31 2c 20 72 65 67 45 6f 66 42 29 3b 0a 20 20   1, regEofB);.  
2b883 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b884 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72  1(v, OP_Yield, r
2b885 65 67 41 64 64 72 42 29 3b 0a 20 20 56 64 62 65  egAddrB);.  Vdbe
2b886 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
2b887 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20 66  "End coroutine f
2b888 6f 72 20 72 69 67 68 74 20 53 45 4c 45 43 54 22  or right SELECT"
2b889 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  ));..  /* Genera
2b88a 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  te a subroutine 
2b88b 74 68 61 74 20 6f 75 74 70 75 74 73 20 74 68 65  that outputs the
2b88c 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
2b88d 74 68 65 20 41 0a 20 20 2a 2a 20 73 65 6c 65 63  the A.  ** selec
2b88e 74 20 61 73 20 74 68 65 20 6e 65 78 74 20 6f 75  t as the next ou
2b88f 74 70 75 74 20 72 6f 77 20 6f 66 20 74 68 65 20  tput row of the 
2b890 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e  compound select.
2b891 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70  .  */.  VdbeNoop
2b892 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 75 74  Comment((v, "Out
2b893 70 75 74 20 72 6f 75 74 69 6e 65 20 66 6f 72 20  put routine for 
2b894 41 22 29 29 3b 0a 20 20 61 64 64 72 4f 75 74 41  A"));.  addrOutA
2b895 20 3d 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75   = generateOutpu
2b896 74 53 75 62 72 6f 75 74 69 6e 65 28 70 50 61 72  tSubroutine(pPar
2b897 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  se,.            
2b898 20 20 20 20 20 70 2c 20 26 64 65 73 74 41 2c 20       p, &destA, 
2b899 70 44 65 73 74 2c 20 72 65 67 4f 75 74 41 2c 0a  pDest, regOutA,.
2b89a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b89b 20 72 65 67 50 72 65 76 2c 20 70 4b 65 79 44 75   regPrev, pKeyDu
2b89c 70 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41  p, P4_KEYINFO_HA
2b89d 4e 44 4f 46 46 2c 20 6c 61 62 65 6c 45 6e 64 29  NDOFF, labelEnd)
2b89e 3b 0a 20 20 0a 20 20 2f 2a 20 47 65 6e 65 72 61  ;.  .  /* Genera
2b89f 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  te a subroutine 
2b8a0 74 68 61 74 20 6f 75 74 70 75 74 73 20 74 68 65  that outputs the
2b8a1 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20   current row of 
2b8a2 74 68 65 20 42 0a 20 20 2a 2a 20 73 65 6c 65 63  the B.  ** selec
2b8a3 74 20 61 73 20 74 68 65 20 6e 65 78 74 20 6f 75  t as the next ou
2b8a4 74 70 75 74 20 72 6f 77 20 6f 66 20 74 68 65 20  tput row of the 
2b8a5 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e  compound select.
2b8a6 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d  .  */.  if( op==
2b8a7 54 4b 5f 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b  TK_ALL || op==TK
2b8a8 5f 55 4e 49 4f 4e 20 29 7b 0a 20 20 20 20 56 64  _UNION ){.    Vd
2b8a9 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2b8aa 2c 20 22 4f 75 74 70 75 74 20 72 6f 75 74 69 6e  , "Output routin
2b8ab 65 20 66 6f 72 20 42 22 29 29 3b 0a 20 20 20 20  e for B"));.    
2b8ac 61 64 64 72 4f 75 74 42 20 3d 20 67 65 6e 65 72  addrOutB = gener
2b8ad 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74  ateOutputSubrout
2b8ae 69 6e 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  ine(pParse,.    
2b8af 20 20 20 20 20 20 20 20 20 20 20 20 20 70 2c 20               p, 
2b8b0 26 64 65 73 74 42 2c 20 70 44 65 73 74 2c 20 72  &destB, pDest, r
2b8b1 65 67 4f 75 74 42 2c 0a 20 20 20 20 20 20 20 20  egOutB,.        
2b8b2 20 20 20 20 20 20 20 20 20 72 65 67 50 72 65 76           regPrev
2b8b3 2c 20 70 4b 65 79 44 75 70 2c 20 50 34 5f 4b 45  , pKeyDup, P4_KE
2b8b4 59 49 4e 46 4f 5f 53 54 41 54 49 43 2c 20 6c 61  YINFO_STATIC, la
2b8b5 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20  belEnd);.  }..  
2b8b6 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 75  /* Generate a su
2b8b7 62 72 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e 20  broutine to run 
2b8b8 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73  when the results
2b8b9 20 66 72 6f 6d 20 73 65 6c 65 63 74 20 41 0a 20   from select A. 
2b8ba 20 2a 2a 20 61 72 65 20 65 78 68 61 75 73 74 65   ** are exhauste
2b8bb 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61 20  d and only data 
2b8bc 69 6e 20 73 65 6c 65 63 74 20 42 20 72 65 6d 61  in select B rema
2b8bd 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65  ins..  */.  Vdbe
2b8be 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
2b8bf 22 65 6f 66 2d 41 20 73 75 62 72 6f 75 74 69 6e  "eof-A subroutin
2b8c0 65 22 29 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  e"));.  if( op==
2b8c1 54 4b 5f 45 58 43 45 50 54 20 7c 7c 20 6f 70 3d  TK_EXCEPT || op=
2b8c2 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b  =TK_INTERSECT ){
2b8c3 0a 20 20 20 20 61 64 64 72 45 6f 66 41 20 3d 20  .    addrEofA = 
2b8c4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b8c5 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2b8c6 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 65   labelEnd);.  }e
2b8c7 6c 73 65 7b 20 20 0a 20 20 20 20 61 64 64 72 45  lse{  .    addrE
2b8c8 6f 66 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ofA = sqlite3Vdb
2b8c9 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
2b8ca 2c 20 72 65 67 45 6f 66 42 2c 20 6c 61 62 65 6c  , regEofB, label
2b8cb 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  End);.    sqlite
2b8cc 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b8cd 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 42  P_Gosub, regOutB
2b8ce 2c 20 61 64 64 72 4f 75 74 42 29 3b 0a 20 20 20  , addrOutB);.   
2b8cf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b8d0 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
2b8d1 72 65 67 41 64 64 72 42 29 3b 0a 20 20 20 20 73  regAddrB);.    s
2b8d2 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2b8d3 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
2b8d4 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 7d 0a 0a  addrEofA);.  }..
2b8d5 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
2b8d6 73 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 72 75  subroutine to ru
2b8d7 6e 20 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c  n when the resul
2b8d8 74 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 20 42  ts from select B
2b8d9 0a 20 20 2a 2a 20 61 72 65 20 65 78 68 61 75 73  .  ** are exhaus
2b8da 74 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74  ted and only dat
2b8db 61 20 69 6e 20 73 65 6c 65 63 74 20 41 20 72 65  a in select A re
2b8dc 6d 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  mains..  */.  if
2b8dd 28 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45  ( op==TK_INTERSE
2b8de 43 54 20 29 7b 0a 20 20 20 20 61 64 64 72 45 6f  CT ){.    addrEo
2b8df 66 42 20 3d 20 61 64 64 72 45 6f 66 41 3b 0a 20  fB = addrEofA;. 
2b8e0 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 56 64   }else{  .    Vd
2b8e1 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2b8e2 2c 20 22 65 6f 66 2d 42 20 73 75 62 72 6f 75 74  , "eof-B subrout
2b8e3 69 6e 65 22 29 29 3b 0a 20 20 20 20 61 64 64 72  ine"));.    addr
2b8e4 45 6f 66 42 20 3d 20 73 71 6c 69 74 65 33 56 64  EofB = sqlite3Vd
2b8e5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2b8e6 66 2c 20 72 65 67 45 6f 66 41 2c 20 6c 61 62 65  f, regEofA, labe
2b8e7 6c 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  lEnd);.    sqlit
2b8e8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b8e9 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74  OP_Gosub, regOut
2b8ea 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a 20 20  A, addrOutA);.  
2b8eb 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b8ec 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2b8ed 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 20 20   regAddrA);.    
2b8ee 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b8ef 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2b8f0 20 61 64 64 72 45 6f 66 42 29 3b 0a 20 20 7d 0a   addrEofB);.  }.
2b8f1 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63  .  /* Generate c
2b8f2 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68  ode to handle th
2b8f3 65 20 63 61 73 65 20 6f 66 20 41 3c 42 0a 20 20  e case of A<B.  
2b8f4 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d  */.  VdbeNoopCom
2b8f5 6d 65 6e 74 28 28 76 2c 20 22 41 2d 6c 74 2d 42  ment((v, "A-lt-B
2b8f6 20 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a   subroutine"));.
2b8f7 20 20 61 64 64 72 41 6c 74 42 20 3d 20 73 71 6c    addrAltB = sql
2b8f8 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b8f9 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f  , OP_Gosub, regO
2b8fa 75 74 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a  utA, addrOutA);.
2b8fb 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b8fc 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2b8fd 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73 71   regAddrA);.  sq
2b8fe 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b8ff 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
2b900 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20  A, addrEofA);.  
2b901 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b902 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2b903 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20   labelCmpr);..  
2b904 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
2b905 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 63   to handle the c
2b906 61 73 65 20 6f 66 20 41 3d 3d 42 0a 20 20 2a 2f  ase of A==B.  */
2b907 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c  .  if( op==TK_AL
2b908 4c 20 29 7b 0a 20 20 20 20 61 64 64 72 41 65 71  L ){.    addrAeq
2b909 42 20 3d 20 61 64 64 72 41 6c 74 42 3b 0a 20 20  B = addrAltB;.  
2b90a 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b  }else if( op==TK
2b90b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b 0a 20 20  _INTERSECT ){.  
2b90c 20 20 61 64 64 72 41 65 71 42 20 3d 20 61 64 64    addrAeqB = add
2b90d 72 41 6c 74 42 3b 0a 20 20 20 20 61 64 64 72 41  rAltB;.    addrA
2b90e 6c 74 42 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ltB++;.  }else{.
2b90f 20 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d      VdbeNoopComm
2b910 65 6e 74 28 28 76 2c 20 22 41 2d 65 71 2d 42 20  ent((v, "A-eq-B 
2b911 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20  subroutine"));. 
2b912 20 20 20 61 64 64 72 41 65 71 42 20 3d 0a 20 20     addrAeqB =.  
2b913 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b914 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2b915 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 20 20   regAddrA);.    
2b916 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b917 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45  2(v, OP_If, regE
2b918 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a  ofA, addrEofA);.
2b919 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2b91a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
2b91b 2c 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b  , 0, labelCmpr);
2b91c 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72  .  }..  /* Gener
2b91d 61 74 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64  ate code to hand
2b91e 6c 65 20 74 68 65 20 63 61 73 65 20 6f 66 20 41  le the case of A
2b91f 3e 42 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f  >B.  */.  VdbeNo
2b920 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41  opComment((v, "A
2b921 2d 67 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65  -gt-B subroutine
2b922 22 29 29 3b 0a 20 20 61 64 64 72 41 67 74 42 20  "));.  addrAgtB 
2b923 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2b924 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 69  rentAddr(v);.  i
2b925 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c  f( op==TK_ALL ||
2b926 20 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b   op==TK_UNION ){
2b927 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2b928 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73  AddOp2(v, OP_Gos
2b929 75 62 2c 20 72 65 67 4f 75 74 42 2c 20 61 64 64  ub, regOutB, add
2b92a 72 4f 75 74 42 29 3b 0a 20 20 7d 0a 20 20 73 71  rOutB);.  }.  sq
2b92b 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2b92c 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67  v, OP_Yield, reg
2b92d 41 64 64 72 42 29 3b 0a 20 20 73 71 6c 69 74 65  AddrB);.  sqlite
2b92e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b92f 50 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 61  P_If, regEofB, a
2b930 64 64 72 45 6f 66 42 29 3b 0a 20 20 73 71 6c 69  ddrEofB);.  sqli
2b931 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b932 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62   OP_Goto, 0, lab
2b933 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 54  elCmpr);..  /* T
2b934 68 69 73 20 63 6f 64 65 20 72 75 6e 73 20 6f 6e  his code runs on
2b935 63 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  ce to initialize
2b936 20 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a   everything..  *
2b937 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a  /.  sqlite3VdbeJ
2b938 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a  umpHere(v, j1);.
2b939 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b93a 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2b93b 72 2c 20 30 2c 20 72 65 67 45 6f 66 41 29 3b 0a  r, 0, regEofA);.
2b93c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b93d 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2b93e 72 2c 20 30 2c 20 72 65 67 45 6f 66 42 29 3b 0a  r, 0, regEofB);.
2b93f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b940 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c  Op2(v, OP_Gosub,
2b941 20 72 65 67 41 64 64 72 41 2c 20 61 64 64 72 53   regAddrA, addrS
2b942 65 6c 65 63 74 41 29 3b 0a 20 20 73 71 6c 69 74  electA);.  sqlit
2b943 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b944 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 41 64 64  OP_Gosub, regAdd
2b945 72 42 2c 20 61 64 64 72 53 65 6c 65 63 74 42 29  rB, addrSelectB)
2b946 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2b947 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
2b948 72 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66  regEofA, addrEof
2b949 41 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  A);.  sqlite3Vdb
2b94a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
2b94b 2c 20 72 65 67 45 6f 66 42 2c 20 61 64 64 72 45  , regEofB, addrE
2b94c 6f 66 42 29 3b 0a 0a 20 20 2f 2a 20 49 6d 70 6c  ofB);..  /* Impl
2b94d 65 6d 65 6e 74 20 74 68 65 20 6d 61 69 6e 20 6d  ement the main m
2b94e 65 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20  erge loop.  */. 
2b94f 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2b950 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6c 61 62 65  lveLabel(v, labe
2b951 6c 43 6d 70 72 29 3b 0a 20 20 73 71 6c 69 74 65  lCmpr);.  sqlite
2b952 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
2b953 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 30  P_Permutation, 0
2b954 2c 20 30 2c 20 30 2c 20 28 63 68 61 72 2a 29 61  , 0, 0, (char*)a
2b955 50 65 72 6d 75 74 65 2c 20 50 34 5f 49 4e 54 41  Permute, P4_INTA
2b956 52 52 41 59 29 3b 0a 20 20 73 71 6c 69 74 65 33  RRAY);.  sqlite3
2b957 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2b958 5f 43 6f 6d 70 61 72 65 2c 20 64 65 73 74 41 2e  _Compare, destA.
2b959 69 4d 65 6d 2c 20 64 65 73 74 42 2e 69 4d 65 6d  iMem, destB.iMem
2b95a 2c 20 6e 4f 72 64 65 72 42 79 2c 0a 20 20 20 20  , nOrderBy,.    
2b95b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b95c 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79       (char*)pKey
2b95d 4d 65 72 67 65 2c 20 50 34 5f 4b 45 59 49 4e 46  Merge, P4_KEYINF
2b95e 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 73 71  O_HANDOFF);.  sq
2b95f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2b960 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 61 64 64 72  v, OP_Jump, addr
2b961 41 6c 74 42 2c 20 61 64 64 72 41 65 71 42 2c 20  AltB, addrAeqB, 
2b962 61 64 64 72 41 67 74 42 29 3b 0a 0a 20 20 2f 2a  addrAgtB);..  /*
2b963 20 52 65 6c 65 61 73 65 20 74 65 6d 70 6f 72 61   Release tempora
2b964 72 79 20 72 65 67 69 73 74 65 72 73 0a 20 20 2a  ry registers.  *
2b965 2f 0a 20 20 69 66 28 20 72 65 67 50 72 65 76 20  /.  if( regPrev 
2b966 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  ){.    sqlite3Re
2b967 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70  leaseTempRange(p
2b968 50 61 72 73 65 2c 20 72 65 67 50 72 65 76 2c 20  Parse, regPrev, 
2b969 6e 4f 72 64 65 72 42 79 2b 31 29 3b 0a 20 20 7d  nOrderBy+1);.  }
2b96a 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74  ..  /* Jump to t
2b96b 68 65 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e  he this point in
2b96c 20 6f 72 64 65 72 20 74 6f 20 74 65 72 6d 69 6e   order to termin
2b96d 61 74 65 20 74 68 65 20 71 75 65 72 79 2e 0a 20  ate the query.. 
2b96e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2b96f 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2b970 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 0a 20 20 2f   labelEnd);..  /
2b971 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72  * Set the number
2b972 20 6f 66 20 6f 75 74 70 75 74 20 63 6f 6c 75 6d   of output colum
2b973 6e 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44  ns.  */.  if( pD
2b974 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f  est->eDest==SRT_
2b975 4f 75 74 70 75 74 20 29 7b 0a 20 20 20 20 53 65  Output ){.    Se
2b976 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70  lect *pFirst = p
2b977 50 72 69 6f 72 3b 0a 20 20 20 20 77 68 69 6c 65  Prior;.    while
2b978 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  ( pFirst->pPrior
2b979 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72   ) pFirst = pFir
2b97a 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20  st->pPrior;.    
2b97b 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61  generateColumnNa
2b97c 6d 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70  mes(pParse, 0, p
2b97d 46 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a  First->pEList);.
2b97e 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 73 73 65    }..  /* Reasse
2b97f 6d 62 6c 79 20 74 68 65 20 63 6f 6d 70 6f 75 6e  mbly the compoun
2b980 64 20 71 75 65 72 79 20 73 6f 20 74 68 61 74 20  d query so that 
2b981 69 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64  it will be freed
2b982 20 63 6f 72 72 65 63 74 6c 79 0a 20 20 2a 2a 20   correctly.  ** 
2b983 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66  by the calling f
2b984 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28  unction */.  if(
2b985 20 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20   p->pPrior ){.  
2b986 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2b987 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 50 72  elete(db, p->pPr
2b988 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70  ior);.  }.  p->p
2b989 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a  Prior = pPrior;.
2b98a 0a 20 20 2f 2a 2a 2a 20 54 42 44 3a 20 20 49 6e  .  /*** TBD:  In
2b98b 73 65 72 74 20 73 75 62 72 6f 75 74 69 6e 65 20  sert subroutine 
2b98c 63 61 6c 6c 73 20 74 6f 20 63 6c 6f 73 65 20 63  calls to close c
2b98d 75 72 73 6f 72 73 20 6f 6e 20 69 6e 63 6f 6d 70  ursors on incomp
2b98e 6c 65 74 65 0a 20 20 2a 2a 2a 2a 20 73 75 62 71  lete.  **** subq
2b98f 75 65 72 69 65 73 20 2a 2a 2a 2a 2f 0a 20 20 72  ueries ****/.  r
2b990 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2b991 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21  .}.#endif..#if !
2b992 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2b993 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c  MIT_SUBQUERY) ||
2b994 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2b995 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 20 46  _OMIT_VIEW)./* F
2b996 6f 72 77 61 72 64 20 44 65 63 6c 61 72 61 74 69  orward Declarati
2b997 6f 6e 73 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ons */.static vo
2b998 69 64 20 73 75 62 73 74 45 78 70 72 4c 69 73 74  id substExprList
2b999 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c  (sqlite3*, ExprL
2b99a 69 73 74 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c  ist*, int, ExprL
2b99b 69 73 74 2a 29 3b 0a 73 74 61 74 69 63 20 76 6f  ist*);.static vo
2b99c 69 64 20 73 75 62 73 74 53 65 6c 65 63 74 28 73  id substSelect(s
2b99d 71 6c 69 74 65 33 2a 2c 20 53 65 6c 65 63 74 20  qlite3*, Select 
2b99e 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74  *, int, ExprList
2b99f 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e   *);../*.** Scan
2b9a0 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 78 70   through the exp
2b9a1 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 20  ression pExpr.  
2b9a2 52 65 70 6c 61 63 65 20 65 76 65 72 79 20 72 65  Replace every re
2b9a3 66 65 72 65 6e 63 65 20 74 6f 0a 2a 2a 20 61 20  ference to.** a 
2b9a4 63 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20  column in table 
2b9a5 6e 75 6d 62 65 72 20 69 54 61 62 6c 65 20 77 69  number iTable wi
2b9a6 74 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  th a copy of the
2b9a7 20 69 43 6f 6c 75 6d 6e 2d 74 68 0a 2a 2a 20 65   iColumn-th.** e
2b9a8 6e 74 72 79 20 69 6e 20 70 45 4c 69 73 74 2e 20  ntry in pEList. 
2b9a9 20 28 42 75 74 20 6c 65 61 76 65 20 72 65 66 65   (But leave refe
2b9aa 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20 52 4f  rences to the RO
2b9ab 57 49 44 20 63 6f 6c 75 6d 6e 20 0a 2a 2a 20 75  WID column .** u
2b9ac 6e 63 68 61 6e 67 65 64 2e 29 0a 2a 2a 0a 2a 2a  nchanged.).**.**
2b9ad 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2b9ae 20 70 61 72 74 20 6f 66 20 74 68 65 20 66 6c 61   part of the fla
2b9af 74 74 65 6e 69 6e 67 20 70 72 6f 63 65 64 75 72  ttening procedur
2b9b0 65 2e 20 20 41 20 73 75 62 71 75 65 72 79 0a 2a  e.  A subquery.*
2b9b1 2a 20 77 68 6f 73 65 20 72 65 73 75 6c 74 20 73  * whose result s
2b9b2 65 74 20 69 73 20 64 65 66 69 6e 65 64 20 62 79  et is defined by
2b9b3 20 70 45 4c 69 73 74 20 61 70 70 65 61 72 73 20   pEList appears 
2b9b4 61 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0a  as entry in the.
2b9b5 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  ** FROM clause o
2b9b6 66 20 61 20 53 45 4c 45 43 54 20 73 75 63 68 20  f a SELECT such 
2b9b7 74 68 61 74 20 74 68 65 20 56 44 42 45 20 63 75  that the VDBE cu
2b9b8 72 73 6f 72 20 61 73 73 69 67 6e 65 64 20 74 6f  rsor assigned to
2b9b9 20 74 68 61 74 0a 2a 2a 20 46 4f 52 4d 20 63 6c   that.** FORM cl
2b9ba 61 75 73 65 20 65 6e 74 72 79 20 69 73 20 69 54  ause entry is iT
2b9bb 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74  able.  This rout
2b9bc 69 6e 65 20 6d 61 6b 65 20 74 68 65 20 6e 65 63  ine make the nec
2b9bd 65 73 73 61 72 79 20 0a 2a 2a 20 63 68 61 6e 67  essary .** chang
2b9be 65 73 20 74 6f 20 70 45 78 70 72 20 73 6f 20 74  es to pExpr so t
2b9bf 68 61 74 20 69 74 20 72 65 66 65 72 73 20 64 69  hat it refers di
2b9c0 72 65 63 74 6c 79 20 74 6f 20 74 68 65 20 73 6f  rectly to the so
2b9c1 75 72 63 65 20 74 61 62 6c 65 0a 2a 2a 20 6f 66  urce table.** of
2b9c2 20 74 68 65 20 73 75 62 71 75 65 72 79 20 72 61   the subquery ra
2b9c3 74 68 65 72 20 74 68 65 20 72 65 73 75 6c 74 20  ther the result 
2b9c4 73 65 74 20 6f 66 20 74 68 65 20 73 75 62 71 75  set of the subqu
2b9c5 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 45  ery..*/.static E
2b9c6 78 70 72 20 2a 73 75 62 73 74 45 78 70 72 28 0a  xpr *substExpr(.
2b9c7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2b9c8 20 20 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 20        /* Report 
2b9c9 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 74 6f  malloc errors to
2b9ca 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e   this connection
2b9cb 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
2b9cc 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70  r,        /* Exp
2b9cd 72 20 69 6e 20 77 68 69 63 68 20 73 75 62 73 74  r in which subst
2b9ce 69 74 75 74 69 6f 6e 20 6f 63 63 75 72 73 20 2a  itution occurs *
2b9cf 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  /.  int iTable, 
2b9d0 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
2b9d1 20 74 6f 20 62 65 20 73 75 62 73 74 69 74 75 74   to be substitut
2b9d2 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ed */.  ExprList
2b9d3 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 53   *pEList    /* S
2b9d4 75 62 73 74 69 74 75 74 65 20 65 78 70 72 65 73  ubstitute expres
2b9d5 73 69 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 69 66  sions */.){.  if
2b9d6 28 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74  ( pExpr==0 ) ret
2b9d7 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 78  urn 0;.  if( pEx
2b9d8 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d  pr->op==TK_COLUM
2b9d9 4e 20 26 26 20 70 45 78 70 72 2d 3e 69 54 61 62  N && pExpr->iTab
2b9da 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20  le==iTable ){.  
2b9db 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f    if( pExpr->iCo
2b9dc 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20  lumn<0 ){.      
2b9dd 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e  pExpr->op = TK_N
2b9de 55 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ULL;.    }else{.
2b9df 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77        Expr *pNew
2b9e0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2b9e1 70 45 4c 69 73 74 21 3d 30 20 26 26 20 70 45 78  pEList!=0 && pEx
2b9e2 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 45 4c 69  pr->iColumn<pELi
2b9e3 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20  st->nExpr );.   
2b9e4 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
2b9e5 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45  ->pLeft==0 && pE
2b9e6 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29  xpr->pRight==0 )
2b9e7 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73  ;.      pNew = s
2b9e8 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2b9e9 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 70 45 78 70  , pEList->a[pExp
2b9ea 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70  r->iColumn].pExp
2b9eb 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  r, 0);.      if(
2b9ec 20 70 4e 65 77 20 26 26 20 70 45 78 70 72 2d 3e   pNew && pExpr->
2b9ed 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  pColl ){.       
2b9ee 20 70 4e 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70   pNew->pColl = p
2b9ef 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  Expr->pColl;.   
2b9f0 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
2b9f1 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2b9f2 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 70   pExpr);.      p
2b9f3 45 78 70 72 20 3d 20 70 4e 65 77 3b 0a 20 20 20  Expr = pNew;.   
2b9f4 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
2b9f5 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 73  pExpr->pLeft = s
2b9f6 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 45 78  ubstExpr(db, pEx
2b9f7 70 72 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c  pr->pLeft, iTabl
2b9f8 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  e, pEList);.    
2b9f9 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 3d 20  pExpr->pRight = 
2b9fa 73 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 45  substExpr(db, pE
2b9fb 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 69 54 61  xpr->pRight, iTa
2b9fc 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2b9fd 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
2b9fe 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
2b9ff 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
2ba00 20 20 20 20 73 75 62 73 74 53 65 6c 65 63 74 28      substSelect(
2ba01 64 62 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65  db, pExpr->x.pSe
2ba02 6c 65 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45  lect, iTable, pE
2ba03 4c 69 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  List);.    }else
2ba04 7b 0a 20 20 20 20 20 20 73 75 62 73 74 45 78 70  {.      substExp
2ba05 72 4c 69 73 74 28 64 62 2c 20 70 45 78 70 72 2d  rList(db, pExpr-
2ba06 3e 78 2e 70 4c 69 73 74 2c 20 69 54 61 62 6c 65  >x.pList, iTable
2ba07 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d  , pEList);.    }
2ba08 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 45  .  }.  return pE
2ba09 78 70 72 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  xpr;.}.static vo
2ba0a 69 64 20 73 75 62 73 74 45 78 70 72 4c 69 73 74  id substExprList
2ba0b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2ba0c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6f           /* Repo
2ba0d 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73  rt malloc errors
2ba0e 20 68 65 72 65 20 2a 2f 0a 20 20 45 78 70 72 4c   here */.  ExprL
2ba0f 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20  ist *pList,     
2ba10 2f 2a 20 4c 69 73 74 20 74 6f 20 73 63 61 6e 20  /* List to scan 
2ba11 61 6e 64 20 69 6e 20 77 68 69 63 68 20 74 6f 20  and in which to 
2ba12 6d 61 6b 65 20 73 75 62 73 74 69 74 75 74 65 73  make substitutes
2ba13 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
2ba14 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61  ,          /* Ta
2ba15 62 6c 65 20 74 6f 20 62 65 20 73 75 62 73 74 69  ble to be substi
2ba16 74 75 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c  tuted */.  ExprL
2ba17 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20 20 20  ist *pEList     
2ba18 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 76 61  /* Substitute va
2ba19 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  lues */.){.  int
2ba1a 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d   i;.  if( pList=
2ba1b 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  =0 ) return;.  f
2ba1c 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d  or(i=0; i<pList-
2ba1d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  >nExpr; i++){.  
2ba1e 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45    pList->a[i].pE
2ba1f 78 70 72 20 3d 20 73 75 62 73 74 45 78 70 72 28  xpr = substExpr(
2ba20 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  db, pList->a[i].
2ba21 70 45 78 70 72 2c 20 69 54 61 62 6c 65 2c 20 70  pExpr, iTable, p
2ba22 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74  EList);.  }.}.st
2ba23 61 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 53  atic void substS
2ba24 65 6c 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33  elect(.  sqlite3
2ba25 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a   *db,         /*
2ba26 20 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65   Report malloc e
2ba27 72 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20  rrors here */.  
2ba28 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20  Select *p,      
2ba29 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73       /* SELECT s
2ba2a 74 61 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63  tatement in whic
2ba2b 68 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69  h to make substi
2ba2c 74 75 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  tutions */.  int
2ba2d 20 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20   iTable,        
2ba2e 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65    /* Table to be
2ba2f 20 72 65 70 6c 61 63 65 64 20 2a 2f 0a 20 20 45   replaced */.  E
2ba30 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20  xprList *pEList 
2ba31 20 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74      /* Substitut
2ba32 65 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20  e values */.){. 
2ba33 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a   SrcList *pSrc;.
2ba34 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2ba35 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
2ba36 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 70 20  int i;.  if( !p 
2ba37 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 75 62 73  ) return;.  subs
2ba38 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d  tExprList(db, p-
2ba39 3e 70 45 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c  >pEList, iTable,
2ba3a 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73   pEList);.  subs
2ba3b 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d  tExprList(db, p-
2ba3c 3e 70 47 72 6f 75 70 42 79 2c 20 69 54 61 62 6c  >pGroupBy, iTabl
2ba3d 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75  e, pEList);.  su
2ba3e 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20  bstExprList(db, 
2ba3f 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 54 61  p->pOrderBy, iTa
2ba40 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2ba41 70 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62  p->pHaving = sub
2ba42 73 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 48  stExpr(db, p->pH
2ba43 61 76 69 6e 67 2c 20 69 54 61 62 6c 65 2c 20 70  aving, iTable, p
2ba44 45 4c 69 73 74 29 3b 0a 20 20 70 2d 3e 70 57 68  EList);.  p->pWh
2ba45 65 72 65 20 3d 20 73 75 62 73 74 45 78 70 72 28  ere = substExpr(
2ba46 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 69  db, p->pWhere, i
2ba47 54 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a  Table, pEList);.
2ba48 20 20 73 75 62 73 74 53 65 6c 65 63 74 28 64 62    substSelect(db
2ba49 2c 20 70 2d 3e 70 50 72 69 6f 72 2c 20 69 54 61  , p->pPrior, iTa
2ba4a 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2ba4b 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a  pSrc = p->pSrc;.
2ba4c 20 20 61 73 73 65 72 74 28 20 70 53 72 63 20 29    assert( pSrc )
2ba4d 3b 20 20 2f 2a 20 45 76 65 6e 20 66 6f 72 20 28  ;  /* Even for (
2ba4e 53 45 4c 45 43 54 20 31 29 20 77 65 20 68 61 76  SELECT 1) we hav
2ba4f 65 3a 20 70 53 72 63 21 3d 30 20 62 75 74 20 70  e: pSrc!=0 but p
2ba50 53 72 63 2d 3e 6e 53 72 63 3d 3d 30 20 2a 2f 0a  Src->nSrc==0 */.
2ba51 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53 72    if( ALWAYS(pSr
2ba52 63 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  c) ){.    for(i=
2ba53 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65  pSrc->nSrc, pIte
2ba54 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20  m=pSrc->a; i>0; 
2ba55 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i--, pItem++){. 
2ba56 20 20 20 20 20 73 75 62 73 74 53 65 6c 65 63 74       substSelect
2ba57 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c  (db, pItem->pSel
2ba58 65 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  ect, iTable, pEL
2ba59 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ist);.    }.  }.
2ba5a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
2ba5b 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2ba5c 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64  _SUBQUERY) || !d
2ba5d 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2ba5e 49 54 5f 56 49 45 57 29 20 2a 2f 0a 0a 23 69 66  IT_VIEW) */..#if
2ba5f 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2ba60 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20  _OMIT_SUBQUERY) 
2ba61 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
2ba62 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a  TE_OMIT_VIEW)./*
2ba63 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2ba64 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66 6c 61   attempts to fla
2ba65 74 74 65 6e 20 73 75 62 71 75 65 72 69 65 73 20  tten subqueries 
2ba66 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 70 65 65  in order to spee
2ba67 64 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 2e 20  d.** execution. 
2ba68 20 49 74 20 72 65 74 75 72 6e 73 20 31 20 69 66   It returns 1 if
2ba69 20 69 74 20 6d 61 6b 65 73 20 63 68 61 6e 67 65   it makes change
2ba6a 73 20 61 6e 64 20 30 20 69 66 20 6e 6f 20 66 6c  s and 0 if no fl
2ba6b 61 74 74 65 6e 69 6e 67 0a 2a 2a 20 6f 63 63 75  attening.** occu
2ba6c 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 6e 64  rs..**.** To und
2ba6d 65 72 73 74 61 6e 64 20 74 68 65 20 63 6f 6e 63  erstand the conc
2ba6e 65 70 74 20 6f 66 20 66 6c 61 74 74 65 6e 69 6e  ept of flattenin
2ba6f 67 2c 20 63 6f 6e 73 69 64 65 72 20 74 68 65 20  g, consider the 
2ba70 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 71 75 65  following.** que
2ba71 72 79 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  ry:.**.**     SE
2ba72 4c 45 43 54 20 61 20 46 52 4f 4d 20 28 53 45 4c  LECT a FROM (SEL
2ba73 45 43 54 20 78 2b 79 20 41 53 20 61 20 46 52 4f  ECT x+y AS a FRO
2ba74 4d 20 74 31 20 57 48 45 52 45 20 7a 3c 31 30 30  M t1 WHERE z<100
2ba75 29 20 57 48 45 52 45 20 61 3e 35 0a 2a 2a 0a 2a  ) WHERE a>5.**.*
2ba76 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 77 61  * The default wa
2ba77 79 20 6f 66 20 69 6d 70 6c 65 6d 65 6e 74 69 6e  y of implementin
2ba78 67 20 74 68 69 73 20 71 75 65 72 79 20 69 73 20  g this query is 
2ba79 74 6f 20 65 78 65 63 75 74 65 20 74 68 65 0a 2a  to execute the.*
2ba7a 2a 20 73 75 62 71 75 65 72 79 20 66 69 72 73 74  * subquery first
2ba7b 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72   and store the r
2ba7c 65 73 75 6c 74 73 20 69 6e 20 61 20 74 65 6d 70  esults in a temp
2ba7d 6f 72 61 72 79 20 74 61 62 6c 65 2c 20 74 68 65  orary table, the
2ba7e 6e 0a 2a 2a 20 72 75 6e 20 74 68 65 20 6f 75 74  n.** run the out
2ba7f 65 72 20 71 75 65 72 79 20 6f 6e 20 74 68 61 74  er query on that
2ba80 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
2ba81 2e 20 20 54 68 69 73 20 72 65 71 75 69 72 65 73  .  This requires
2ba82 20 74 77 6f 0a 2a 2a 20 70 61 73 73 65 73 20 6f   two.** passes o
2ba83 76 65 72 20 74 68 65 20 64 61 74 61 2e 20 20 46  ver the data.  F
2ba84 75 72 74 68 65 72 6d 6f 72 65 2c 20 62 65 63 61  urthermore, beca
2ba85 75 73 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72  use the temporar
2ba86 79 20 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e  y table.** has n
2ba87 6f 20 69 6e 64 69 63 65 73 2c 20 74 68 65 20 57  o indices, the W
2ba88 48 45 52 45 20 63 6c 61 75 73 65 20 6f 6e 20 74  HERE clause on t
2ba89 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 63  he outer query c
2ba8a 61 6e 6e 6f 74 20 62 65 0a 2a 2a 20 6f 70 74 69  annot be.** opti
2ba8b 6d 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  mized..**.** Thi
2ba8c 73 20 72 6f 75 74 69 6e 65 20 61 74 74 65 6d 70  s routine attemp
2ba8d 74 73 20 74 6f 20 72 65 77 72 69 74 65 20 71 75  ts to rewrite qu
2ba8e 65 72 69 65 73 20 73 75 63 68 20 61 73 20 74 68  eries such as th
2ba8f 65 20 61 62 6f 76 65 20 69 6e 74 6f 0a 2a 2a 20  e above into.** 
2ba90 61 20 73 69 6e 67 6c 65 20 66 6c 61 74 20 73 65  a single flat se
2ba91 6c 65 63 74 2c 20 6c 69 6b 65 20 74 68 69 73 3a  lect, like this:
2ba92 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
2ba93 54 20 78 2b 79 20 41 53 20 61 20 46 52 4f 4d 20  T x+y AS a FROM 
2ba94 74 31 20 57 48 45 52 45 20 7a 3c 31 30 30 20 41  t1 WHERE z<100 A
2ba95 4e 44 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ND a>5.**.** The
2ba96 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
2ba97 66 6f 72 20 74 68 69 73 20 73 69 6d 70 69 66 69  for this simpifi
2ba98 63 61 74 69 6f 6e 20 67 69 76 65 73 20 74 68 65  cation gives the
2ba99 20 73 61 6d 65 20 72 65 73 75 6c 74 0a 2a 2a 20   same result.** 
2ba9a 62 75 74 20 6f 6e 6c 79 20 68 61 73 20 74 6f 20  but only has to 
2ba9b 73 63 61 6e 20 74 68 65 20 64 61 74 61 20 6f 6e  scan the data on
2ba9c 63 65 2e 20 20 41 6e 64 20 62 65 63 61 75 73 65  ce.  And because
2ba9d 20 69 6e 64 69 63 65 73 20 6d 69 67 68 74 20 0a   indices might .
2ba9e 2a 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 65 20  ** exist on the 
2ba9f 74 61 62 6c 65 20 74 31 2c 20 61 20 63 6f 6d 70  table t1, a comp
2baa0 6c 65 74 65 20 73 63 61 6e 20 6f 66 20 74 68 65  lete scan of the
2baa1 20 64 61 74 61 20 6d 69 67 68 74 20 62 65 0a 2a   data might be.*
2baa2 2a 20 61 76 6f 69 64 65 64 2e 0a 2a 2a 0a 2a 2a  * avoided..**.**
2baa3 20 46 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6f   Flattening is o
2baa4 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69 66  nly attempted if
2baa5 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c   all of the foll
2baa6 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a  owing are true:.
2baa7 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 54 68 65  **.**   (1)  The
2baa8 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 74 68   subquery and th
2baa9 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f  e outer query do
2baaa 20 6e 6f 74 20 62 6f 74 68 20 75 73 65 20 61 67   not both use ag
2baab 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  gregates..**.** 
2baac 20 20 28 32 29 20 20 54 68 65 20 73 75 62 71 75    (2)  The subqu
2baad 65 72 79 20 69 73 20 6e 6f 74 20 61 6e 20 61 67  ery is not an ag
2baae 67 72 65 67 61 74 65 20 6f 72 20 74 68 65 20 6f  gregate or the o
2baaf 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
2bab0 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20  t a join..**.** 
2bab1 20 20 28 33 29 20 20 54 68 65 20 73 75 62 71 75    (3)  The subqu
2bab2 65 72 79 20 69 73 20 6e 6f 74 20 74 68 65 20 72  ery is not the r
2bab3 69 67 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20  ight operand of 
2bab4 61 20 6c 65 66 74 20 6f 75 74 65 72 20 6a 6f 69  a left outer joi
2bab5 6e 0a 2a 2a 20 20 20 20 20 20 20 20 28 4f 72 69  n.**        (Ori
2bab6 67 69 6e 61 6c 6c 79 20 74 69 63 6b 65 74 20 23  ginally ticket #
2bab7 33 30 36 2e 20 20 53 74 72 65 6e 67 68 74 65 6e  306.  Strenghten
2bab8 65 64 20 62 79 20 74 69 63 6b 65 74 20 23 33 33  ed by ticket #33
2bab9 30 30 29 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29 20  00).**.**   (4) 
2baba 20 54 68 65 20 73 75 62 71 75 65 72 79 20 69 73   The subquery is
2babb 20 6e 6f 74 20 44 49 53 54 49 4e 43 54 20 6f 72   not DISTINCT or
2babc 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2babd 20 69 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a   is not a join..
2babe 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 20 54 68 65  **.**   (5)  The
2babf 20 73 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74   subquery is not
2bac0 20 44 49 53 54 49 4e 43 54 20 6f 72 20 74 68 65   DISTINCT or the
2bac1 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65   outer query doe
2bac2 73 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20  s not use.**    
2bac3 20 20 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a      aggregates..
2bac4 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 20 54 68 65  **.**   (6)  The
2bac5 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e   subquery does n
2bac6 6f 74 20 75 73 65 20 61 67 67 72 65 67 61 74 65  ot use aggregate
2bac7 73 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71  s or the outer q
2bac8 75 65 72 79 20 69 73 20 6e 6f 74 0a 2a 2a 20 20  uery is not.**  
2bac9 20 20 20 20 20 20 44 49 53 54 49 4e 43 54 2e 0a        DISTINCT..
2baca 2a 2a 0a 2a 2a 20 20 20 28 37 29 20 20 54 68 65  **.**   (7)  The
2bacb 20 73 75 62 71 75 65 72 79 20 68 61 73 20 61 20   subquery has a 
2bacc 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a  FROM clause..**.
2bacd 2a 2a 20 20 20 28 38 29 20 20 54 68 65 20 73 75  **   (8)  The su
2bace 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
2bacf 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65  use LIMIT or the
2bad0 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20   outer query is 
2bad1 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a  not a join..**.*
2bad2 2a 20 20 20 28 39 29 20 20 54 68 65 20 73 75 62  *   (9)  The sub
2bad3 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75  query does not u
2bad4 73 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20  se LIMIT or the 
2bad5 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73  outer query does
2bad6 20 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20   not use.**     
2bad7 20 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a     aggregates..*
2bad8 2a 0a 2a 2a 20 20 28 31 30 29 20 20 54 68 65 20  *.**  (10)  The 
2bad9 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
2bada 74 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73  t use aggregates
2badb 20 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75   or the outer qu
2badc 65 72 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20  ery does not.** 
2badd 20 20 20 20 20 20 20 75 73 65 20 4c 49 4d 49 54         use LIMIT
2bade 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 31 29 20 20 54  ..**.**  (11)  T
2badf 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 20  he subquery and 
2bae0 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  the outer query 
2bae1 64 6f 20 6e 6f 74 20 62 6f 74 68 20 68 61 76 65  do not both have
2bae2 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2bae3 73 2e 0a 2a 2a 0a 2a 2a 20 20 28 31 32 29 20 20  s..**.**  (12)  
2bae4 4e 6f 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2e  Not implemented.
2bae5 20 20 53 75 62 73 75 6d 65 64 20 69 6e 74 6f 20    Subsumed into 
2bae6 72 65 73 74 72 69 63 74 69 6f 6e 20 28 33 29 2e  restriction (3).
2bae7 20 20 57 61 73 20 70 72 65 76 69 6f 75 73 6c 79    Was previously
2bae8 0a 2a 2a 20 20 20 20 20 20 20 20 61 20 73 65 70  .**        a sep
2bae9 61 72 61 74 65 20 72 65 73 74 72 69 63 74 69 6f  arate restrictio
2baea 6e 20 64 65 72 69 76 69 6e 67 20 66 72 6f 6d 20  n deriving from 
2baeb 74 69 63 6b 65 74 20 23 33 35 30 2e 0a 2a 2a 0a  ticket #350..**.
2baec 2a 2a 20 20 28 31 33 29 20 20 54 68 65 20 73 75  **  (13)  The su
2baed 62 71 75 65 72 79 20 61 6e 64 20 6f 75 74 65 72  bquery and outer
2baee 20 71 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f   query do not bo
2baef 74 68 20 75 73 65 20 4c 49 4d 49 54 0a 2a 2a 0a  th use LIMIT.**.
2baf0 2a 2a 20 20 28 31 34 29 20 20 54 68 65 20 73 75  **  (14)  The su
2baf1 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
2baf2 75 73 65 20 4f 46 46 53 45 54 0a 2a 2a 0a 2a 2a  use OFFSET.**.**
2baf3 20 20 28 31 35 29 20 20 54 68 65 20 6f 75 74 65    (15)  The oute
2baf4 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20 70  r query is not p
2baf5 61 72 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e  art of a compoun
2baf6 64 20 73 65 6c 65 63 74 20 6f 72 20 74 68 65 0a  d select or the.
2baf7 2a 2a 20 20 20 20 20 20 20 20 73 75 62 71 75 65  **        subque
2baf8 72 79 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  ry does not have
2baf9 20 62 6f 74 68 20 61 6e 20 4f 52 44 45 52 20 42   both an ORDER B
2bafa 59 20 61 6e 64 20 61 20 4c 49 4d 49 54 20 63 6c  Y and a LIMIT cl
2bafb 61 75 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20  ause..**        
2bafc 28 53 65 65 20 74 69 63 6b 65 74 20 23 32 33 33  (See ticket #233
2bafd 39 29 0a 2a 2a 0a 2a 2a 20 20 28 31 36 29 20 20  9).**.**  (16)  
2bafe 54 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20  The outer query 
2baff 69 73 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67  is not an aggreg
2bb00 61 74 65 20 6f 72 20 74 68 65 20 73 75 62 71 75  ate or the subqu
2bb01 65 72 79 20 64 6f 65 73 0a 2a 2a 20 20 20 20 20  ery does.**     
2bb02 20 20 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 4f     not contain O
2bb03 52 44 45 52 20 42 59 2e 20 20 28 54 69 63 6b 65  RDER BY.  (Ticke
2bb04 74 20 23 32 39 34 32 29 20 20 54 68 69 73 20 75  t #2942)  This u
2bb05 73 65 64 20 74 6f 20 6e 6f 74 20 6d 61 74 74 65  sed to not matte
2bb06 72 0a 2a 2a 20 20 20 20 20 20 20 20 75 6e 74 69  r.**        unti
2bb07 6c 20 77 65 20 69 6e 74 72 6f 64 75 63 65 64 20  l we introduced 
2bb08 74 68 65 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74  the group_concat
2bb09 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 0a 2a  () function.  .*
2bb0a 2a 0a 2a 2a 20 20 28 31 37 29 20 20 54 68 65 20  *.**  (17)  The 
2bb0b 73 75 62 2d 71 75 65 72 79 20 69 73 20 6e 6f 74  sub-query is not
2bb0c 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65   a compound sele
2bb0d 63 74 2c 20 6f 72 20 69 74 20 69 73 20 61 20 55  ct, or it is a U
2bb0e 4e 49 4f 4e 20 41 4c 4c 20 0a 2a 2a 20 20 20 20  NION ALL .**    
2bb0f 20 20 20 20 63 6f 6d 70 6f 75 6e 64 20 63 6c 61      compound cla
2bb10 75 73 65 20 6d 61 64 65 20 75 70 20 65 6e 74 69  use made up enti
2bb11 72 65 6c 79 20 6f 66 20 6e 6f 6e 2d 61 67 67 72  rely of non-aggr
2bb12 65 67 61 74 65 20 71 75 65 72 69 65 73 2c 20 61  egate queries, a
2bb13 6e 64 20 0a 2a 2a 20 20 20 20 20 20 20 20 74 68  nd .**        th
2bb14 65 20 70 61 72 65 6e 74 20 71 75 65 72 79 3a 0a  e parent query:.
2bb15 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a  **.**          *
2bb16 20 69 73 20 6e 6f 74 20 69 74 73 65 6c 66 20 70   is not itself p
2bb17 61 72 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e  art of a compoun
2bb18 64 20 73 65 6c 65 63 74 2c 0a 2a 2a 20 20 20 20  d select,.**    
2bb19 20 20 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 61        * is not a
2bb1a 6e 20 61 67 67 72 65 67 61 74 65 20 6f 72 20 44  n aggregate or D
2bb1b 49 53 54 49 4e 43 54 20 71 75 65 72 79 2c 20 61  ISTINCT query, a
2bb1c 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a  nd.**          *
2bb1d 20 68 61 73 20 6e 6f 20 6f 74 68 65 72 20 74 61   has no other ta
2bb1e 62 6c 65 73 20 6f 72 20 73 75 62 2d 73 65 6c 65  bles or sub-sele
2bb1f 63 74 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  cts in the FROM 
2bb20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  clause..**.**   
2bb21 20 20 20 20 20 54 68 65 20 70 61 72 65 6e 74 20       The parent 
2bb22 61 6e 64 20 73 75 62 2d 71 75 65 72 79 20 6d 61  and sub-query ma
2bb23 79 20 63 6f 6e 74 61 69 6e 20 57 48 45 52 45 20  y contain WHERE 
2bb24 63 6c 61 75 73 65 73 2e 20 53 75 62 6a 65 63 74  clauses. Subject
2bb25 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 72 75   to.**        ru
2bb26 6c 65 73 20 28 31 31 29 2c 20 28 31 33 29 20 61  les (11), (13) a
2bb27 6e 64 20 28 31 34 29 2c 20 74 68 65 79 20 6d 61  nd (14), they ma
2bb28 79 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 20 4f  y also contain O
2bb29 52 44 45 52 20 42 59 2c 0a 2a 2a 20 20 20 20 20  RDER BY,.**     
2bb2a 20 20 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46     LIMIT and OFF
2bb2b 53 45 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a  SET clauses..**.
2bb2c 2a 2a 20 20 28 31 38 29 20 20 49 66 20 74 68 65  **  (18)  If the
2bb2d 20 73 75 62 2d 71 75 65 72 79 20 69 73 20 61 20   sub-query is a 
2bb2e 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c  compound select,
2bb2f 20 74 68 65 6e 20 61 6c 6c 20 74 65 72 6d 73 20   then all terms 
2bb30 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  of the.**       
2bb31 20 4f 52 44 45 52 20 62 79 20 63 6c 61 75 73 65   ORDER by clause
2bb32 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 6d   of the parent m
2bb33 75 73 74 20 62 65 20 73 69 6d 70 6c 65 20 72 65  ust be simple re
2bb34 66 65 72 65 6e 63 65 73 20 74 6f 20 0a 2a 2a 20  ferences to .** 
2bb35 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 73 20 6f         columns o
2bb36 66 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 2e  f the sub-query.
2bb37 0a 2a 2a 0a 2a 2a 20 20 28 31 39 29 20 20 54 68  .**.**  (19)  Th
2bb38 65 20 73 75 62 71 75 65 72 79 20 64 6f 65 73 20  e subquery does 
2bb39 6e 6f 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72  not use LIMIT or
2bb3a 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2bb3b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20   does not.**    
2bb3c 20 20 20 20 68 61 76 65 20 61 20 57 48 45 52 45      have a WHERE
2bb3d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20   clause..**.**  
2bb3e 28 32 30 29 20 20 49 66 20 74 68 65 20 73 75 62  (20)  If the sub
2bb3f 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f 6d 70  -query is a comp
2bb40 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20 74 68 65  ound select, the
2bb41 6e 20 69 74 20 6d 75 73 74 20 6e 6f 74 20 75 73  n it must not us
2bb42 65 0a 2a 2a 20 20 20 20 20 20 20 20 61 6e 20 4f  e.**        an O
2bb43 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20  RDER BY clause. 
2bb44 20 54 69 63 6b 65 74 20 23 33 37 37 33 2e 20 20   Ticket #3773.  
2bb45 57 65 20 63 6f 75 6c 64 20 72 65 6c 61 78 20 74  We could relax t
2bb46 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  his constraint.*
2bb47 2a 20 20 20 20 20 20 20 20 73 6f 6d 65 77 68 61  *        somewha
2bb48 74 20 62 79 20 73 61 79 69 6e 67 20 74 68 61 74  t by saying that
2bb49 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68   the terms of th
2bb4a 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
2bb4b 65 20 6d 75 73 74 0a 2a 2a 20 20 20 20 20 20 20  e must.**       
2bb4c 20 61 70 70 65 61 72 20 61 73 20 75 6e 6d 6f 64   appear as unmod
2bb4d 69 66 69 65 64 20 72 65 73 75 6c 74 20 63 6f 6c  ified result col
2bb4e 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f 75 74 65  umns in the oute
2bb4f 72 20 71 75 65 72 79 2e 20 20 42 75 74 0a 2a 2a  r query.  But.**
2bb50 20 20 20 20 20 20 20 20 68 61 76 65 20 6f 74 68          have oth
2bb51 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  er optimizations
2bb52 20 69 6e 20 6d 69 6e 64 20 74 6f 20 64 65 61 6c   in mind to deal
2bb53 20 77 69 74 68 20 74 68 61 74 20 63 61 73 65 2e   with that case.
2bb54 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 72  .**.** In this r
2bb55 6f 75 74 69 6e 65 2c 20 74 68 65 20 22 70 22 20  outine, the "p" 
2bb56 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 20 70  parameter is a p
2bb57 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 75  ointer to the ou
2bb58 74 65 72 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68  ter query..** Th
2bb59 65 20 73 75 62 71 75 65 72 79 20 69 73 20 70 2d  e subquery is p-
2bb5a 3e 70 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e  >pSrc->a[iFrom].
2bb5b 20 20 69 73 41 67 67 20 69 73 20 74 72 75 65 20    isAgg is true 
2bb5c 69 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  if the outer que
2bb5d 72 79 0a 2a 2a 20 75 73 65 73 20 61 67 67 72 65  ry.** uses aggre
2bb5e 67 61 74 65 73 20 61 6e 64 20 73 75 62 71 75 65  gates and subque
2bb5f 72 79 49 73 41 67 67 20 69 73 20 74 72 75 65 20  ryIsAgg is true 
2bb60 69 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20  if the subquery 
2bb61 75 73 65 73 20 61 67 67 72 65 67 61 74 65 73 2e  uses aggregates.
2bb62 0a 2a 2a 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65  .**.** If flatte
2bb63 6e 69 6e 67 20 69 73 20 6e 6f 74 20 61 74 74 65  ning is not atte
2bb64 6d 70 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74  mpted, this rout
2bb65 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61  ine is a no-op a
2bb66 6e 64 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a  nd returns 0..**
2bb67 20 49 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69   If flattening i
2bb68 73 20 61 74 74 65 6d 70 74 65 64 20 74 68 69 73  s attempted this
2bb69 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
2bb6a 20 31 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66   1..**.** All of
2bb6b 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
2bb6c 61 6e 61 6c 79 73 69 73 20 6d 75 73 74 20 6f 63  analysis must oc
2bb6d 63 75 72 20 6f 6e 20 62 6f 74 68 20 74 68 65 20  cur on both the 
2bb6e 6f 75 74 65 72 20 71 75 65 72 79 20 61 6e 64 0a  outer query and.
2bb6f 2a 2a 20 74 68 65 20 73 75 62 71 75 65 72 79 20  ** the subquery 
2bb70 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74  before this rout
2bb71 69 6e 65 20 72 75 6e 73 2e 0a 2a 2f 0a 73 74 61  ine runs..*/.sta
2bb72 74 69 63 20 69 6e 74 20 66 6c 61 74 74 65 6e 53  tic int flattenS
2bb73 75 62 71 75 65 72 79 28 0a 20 20 50 61 72 73 65  ubquery(.  Parse
2bb74 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2bb75 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2bb76 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
2bb77 70 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p,           /* 
2bb78 54 68 65 20 70 61 72 65 6e 74 20 6f 72 20 6f 75  The parent or ou
2bb79 74 65 72 20 53 45 4c 45 43 54 20 73 74 61 74 65  ter SELECT state
2bb7a 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 46  ment */.  int iF
2bb7b 72 6f 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f  rom,           /
2bb7c 2a 20 49 6e 64 65 78 20 69 6e 20 70 2d 3e 70 53  * Index in p->pS
2bb7d 72 63 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 20 69  rc->a[] of the i
2bb7e 6e 6e 65 72 20 73 75 62 71 75 65 72 79 20 2a 2f  nner subquery */
2bb7f 0a 20 20 69 6e 74 20 69 73 41 67 67 2c 20 20 20  .  int isAgg,   
2bb80 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2bb81 69 66 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20  if outer SELECT 
2bb82 75 73 65 73 20 61 67 67 72 65 67 61 74 65 20 66  uses aggregate f
2bb83 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  unctions */.  in
2bb84 74 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20  t subqueryIsAgg 
2bb85 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
2bb86 65 20 73 75 62 71 75 65 72 79 20 75 73 65 73 20  e subquery uses 
2bb87 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
2bb88 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73  ons */.){.  cons
2bb89 74 20 63 68 61 72 20 2a 7a 53 61 76 65 64 41 75  t char *zSavedAu
2bb8a 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72  thContext = pPar
2bb8b 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
2bb8c 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 61 72  ;.  Select *pPar
2bb8d 65 6e 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70  ent;.  Select *p
2bb8e 53 75 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  Sub;       /* Th
2bb8f 65 20 69 6e 6e 65 72 20 71 75 65 72 79 20 6f 72  e inner query or
2bb90 20 22 73 75 62 71 75 65 72 79 22 20 2a 2f 0a 20   "subquery" */. 
2bb91 20 53 65 6c 65 63 74 20 2a 70 53 75 62 31 3b 20   Select *pSub1; 
2bb92 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
2bb93 74 6f 20 74 68 65 20 72 69 67 68 74 6d 6f 73 74  to the rightmost
2bb94 20 73 65 6c 65 63 74 20 69 6e 20 73 75 62 2d 71   select in sub-q
2bb95 75 65 72 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73  uery */.  SrcLis
2bb96 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20 2f 2a  t *pSrc;      /*
2bb97 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   The FROM clause
2bb98 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75   of the outer qu
2bb99 65 72 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ery */.  SrcList
2bb9a 20 2a 70 53 75 62 53 72 63 3b 20 20 20 2f 2a 20   *pSubSrc;   /* 
2bb9b 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  The FROM clause 
2bb9c 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20  of the subquery 
2bb9d 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2bb9e 4c 69 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20  List;    /* The 
2bb9f 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
2bba0 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f  e outer query */
2bba1 0a 20 20 69 6e 74 20 69 50 61 72 65 6e 74 3b 20  .  int iParent; 
2bba2 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 63         /* VDBE c
2bba3 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  ursor number of 
2bba4 74 68 65 20 70 53 75 62 20 72 65 73 75 6c 74 20  the pSub result 
2bba5 73 65 74 20 74 65 6d 70 20 74 61 62 6c 65 20 2a  set temp table *
2bba6 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
2bba7 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2bba8 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70  counter */.  Exp
2bba9 72 20 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20  r *pWhere;      
2bbaa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2bbab 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
2bbac 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  e */.  struct Sr
2bbad 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 75 62  cList_item *pSub
2bbae 69 74 65 6d 3b 20 20 20 2f 2a 20 54 68 65 20 73  item;   /* The s
2bbaf 75 62 71 75 65 72 79 20 2a 2f 0a 20 20 73 71 6c  ubquery */.  sql
2bbb0 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2bbb1 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 43 68 65  e->db;..  /* Che
2bbb2 63 6b 20 74 6f 20 73 65 65 20 69 66 20 66 6c 61  ck to see if fla
2bbb3 74 74 65 6e 69 6e 67 20 69 73 20 70 65 72 6d 69  ttening is permi
2bbb4 74 74 65 64 2e 20 20 52 65 74 75 72 6e 20 30 20  tted.  Return 0 
2bbb5 69 66 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 61  if not..  */.  a
2bbb6 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
2bbb7 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72 69   assert( p->pPri
2bbb8 6f 72 3d 3d 30 20 29 3b 20 20 2f 2a 20 55 6e 61  or==0 );  /* Una
2bbb9 62 6c 65 20 74 6f 20 66 6c 61 74 74 65 6e 20 63  ble to flatten c
2bbba 6f 6d 70 6f 75 6e 64 20 71 75 65 72 69 65 73 20  ompound queries 
2bbbb 2a 2f 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70  */.  pSrc = p->p
2bbbc 53 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Src;.  assert( p
2bbbd 53 72 63 20 26 26 20 69 46 72 6f 6d 3e 3d 30 20  Src && iFrom>=0 
2bbbe 26 26 20 69 46 72 6f 6d 3c 70 53 72 63 2d 3e 6e  && iFrom<pSrc->n
2bbbf 53 72 63 20 29 3b 0a 20 20 70 53 75 62 69 74 65  Src );.  pSubite
2bbc0 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b 69 46 72  m = &pSrc->a[iFr
2bbc1 6f 6d 5d 3b 0a 20 20 69 50 61 72 65 6e 74 20 3d  om];.  iParent =
2bbc2 20 70 53 75 62 69 74 65 6d 2d 3e 69 43 75 72 73   pSubitem->iCurs
2bbc3 6f 72 3b 0a 20 20 70 53 75 62 20 3d 20 70 53 75  or;.  pSub = pSu
2bbc4 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a  bitem->pSelect;.
2bbc5 20 20 61 73 73 65 72 74 28 20 70 53 75 62 21 3d    assert( pSub!=
2bbc6 30 20 29 3b 0a 20 20 69 66 28 20 69 73 41 67 67  0 );.  if( isAgg
2bbc7 20 26 26 20 73 75 62 71 75 65 72 79 49 73 41 67   && subqueryIsAg
2bbc8 67 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  g ) return 0;   
2bbc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2bbca 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 29   Restriction (1)
2bbcb 20 20 2a 2f 0a 20 20 69 66 28 20 73 75 62 71 75    */.  if( subqu
2bbcc 65 72 79 49 73 41 67 67 20 26 26 20 70 53 72 63  eryIsAgg && pSrc
2bbcd 2d 3e 6e 53 72 63 3e 31 20 29 20 72 65 74 75 72  ->nSrc>1 ) retur
2bbce 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  n 0;          /*
2bbcf 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 32 29   Restriction (2)
2bbd0 20 20 2a 2f 0a 20 20 70 53 75 62 53 72 63 20 3d    */.  pSubSrc =
2bbd1 20 70 53 75 62 2d 3e 70 53 72 63 3b 0a 20 20 61   pSub->pSrc;.  a
2bbd2 73 73 65 72 74 28 20 70 53 75 62 53 72 63 20 29  ssert( pSubSrc )
2bbd3 3b 0a 20 20 2f 2a 20 50 72 69 6f 72 20 74 6f 20  ;.  /* Prior to 
2bbd4 76 65 72 73 69 6f 6e 20 33 2e 31 2e 32 2c 20 77  version 3.1.2, w
2bbd5 68 65 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46  hen LIMIT and OF
2bbd6 46 53 45 54 20 68 61 64 20 74 6f 20 62 65 20 73  FSET had to be s
2bbd7 69 6d 70 6c 65 20 63 6f 6e 73 74 61 6e 74 73 2c  imple constants,
2bbd8 0a 20 20 2a 2a 20 6e 6f 74 20 61 72 62 69 74 72  .  ** not arbitr
2bbd9 61 72 79 20 65 78 70 72 65 73 73 73 69 6f 6e 73  ary expresssions
2bbda 2c 20 77 65 20 61 6c 6c 6f 77 65 64 20 73 6f 6d  , we allowed som
2bbdb 65 20 63 6f 6d 62 69 6e 69 6e 67 20 6f 66 20 4c  e combining of L
2bbdc 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a  IMIT and OFFSET.
2bbdd 20 20 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65    ** because the
2bbde 79 20 63 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75  y could be compu
2bbdf 74 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  ted at compile-t
2bbe0 69 6d 65 2e 20 20 42 75 74 20 77 68 65 6e 20 4c  ime.  But when L
2bbe1 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a  IMIT and OFFSET.
2bbe2 20 20 2a 2a 20 62 65 63 61 6d 65 20 61 72 62 69    ** became arbi
2bbe3 74 72 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e  trary expression
2bbe4 73 2c 20 77 65 20 77 65 72 65 20 66 6f 72 63 65  s, we were force
2bbe5 64 20 74 6f 20 61 64 64 20 72 65 73 74 72 69 63  d to add restric
2bbe6 74 69 6f 6e 73 20 28 31 33 29 0a 20 20 2a 2a 20  tions (13).  ** 
2bbe7 61 6e 64 20 28 31 34 29 2e 20 2a 2f 0a 20 20 69  and (14). */.  i
2bbe8 66 28 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20  f( pSub->pLimit 
2bbe9 26 26 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72  && p->pLimit ) r
2bbea 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
2bbeb 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
2bbec 74 69 6f 6e 20 28 31 33 29 20 2a 2f 0a 20 20 69  tion (13) */.  i
2bbed 66 28 20 70 53 75 62 2d 3e 70 4f 66 66 73 65 74  f( pSub->pOffset
2bbee 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
2bbef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbf0 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63        /* Restric
2bbf1 74 69 6f 6e 20 28 31 34 29 20 2a 2f 0a 20 20 69  tion (14) */.  i
2bbf2 66 28 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74  f( p->pRightmost
2bbf3 20 26 26 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74   && pSub->pLimit
2bbf4 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72   && pSub->pOrder
2bbf5 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  By ){.    return
2bbf6 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2bbf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2bbf9 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31  * Restriction (1
2bbfa 35 29 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  5) */.  }.  if( 
2bbfb 70 53 75 62 53 72 63 2d 3e 6e 53 72 63 3d 3d 30  pSubSrc->nSrc==0
2bbfc 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
2bbfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bbfe 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
2bbff 6e 20 28 37 29 20 20 2a 2f 0a 20 20 69 66 28 20  n (7)  */.  if( 
2bc00 28 28 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73  ((pSub->selFlags
2bc01 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21   & SF_Distinct)!
2bc02 3d 30 20 7c 7c 20 70 53 75 62 2d 3e 70 4c 69 6d  =0 || pSub->pLim
2bc03 69 74 29 20 0a 20 20 20 20 20 20 20 20 20 26 26  it) .         &&
2bc04 20 28 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 7c   (pSrc->nSrc>1 |
2bc05 7c 20 69 73 41 67 67 29 20 29 7b 20 20 20 20 20  | isAgg) ){     
2bc06 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74       /* Restrict
2bc07 69 6f 6e 73 20 28 34 29 28 35 29 28 38 29 28 39  ions (4)(5)(8)(9
2bc08 29 20 2a 2f 0a 20 20 20 20 20 72 65 74 75 72 6e  ) */.     return
2bc09 20 30 3b 20 20 20 20 20 20 20 0a 20 20 7d 0a 20   0;       .  }. 
2bc0a 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67   if( (p->selFlag
2bc0b 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29  s & SF_Distinct)
2bc0c 21 3d 30 20 26 26 20 73 75 62 71 75 65 72 79 49  !=0 && subqueryI
2bc0d 73 41 67 67 20 29 7b 0a 20 20 20 20 20 72 65 74  sAgg ){.     ret
2bc0e 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 2f  urn 0;         /
2bc0f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 36  * Restriction (6
2bc10 29 20 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  )  */.  }.  if( 
2bc11 70 2d 3e 70 4f 72 64 65 72 42 79 20 26 26 20 70  p->pOrderBy && p
2bc12 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b  Sub->pOrderBy ){
2bc13 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  .     return 0; 
2bc14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bc15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bc16 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
2bc17 74 72 69 63 74 69 6f 6e 20 28 31 31 29 20 2a 2f  triction (11) */
2bc18 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 41 67 67  .  }.  if( isAgg
2bc19 20 26 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72   && pSub->pOrder
2bc1a 42 79 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  By ) return 0;  
2bc1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2bc1c 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 36   Restriction (16
2bc1d 29 20 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d  ) */.  if( pSub-
2bc1e 3e 70 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70 57  >pLimit && p->pW
2bc1f 68 65 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b  here ) return 0;
2bc20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2bc21 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 39   Restriction (19
2bc22 29 20 2a 2f 0a 0a 20 20 2f 2a 20 4f 42 53 4f 4c  ) */..  /* OBSOL
2bc23 45 54 45 20 43 4f 4d 4d 45 4e 54 20 31 3a 0a 20  ETE COMMENT 1:. 
2bc24 20 2a 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   ** Restriction 
2bc25 33 3a 20 20 49 66 20 74 68 65 20 73 75 62 71 75  3:  If the subqu
2bc26 65 72 79 20 69 73 20 61 20 6a 6f 69 6e 2c 20 6d  ery is a join, m
2bc27 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 75 62  ake sure the sub
2bc28 71 75 65 72 79 20 69 73 20 0a 20 20 2a 2a 20 6e  query is .  ** n
2bc29 6f 74 20 75 73 65 64 20 61 73 20 74 68 65 20 72  ot used as the r
2bc2a 69 67 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20  ight operand of 
2bc2b 61 6e 20 6f 75 74 65 72 20 6a 6f 69 6e 2e 20 20  an outer join.  
2bc2c 45 78 61 6d 70 6c 65 73 20 6f 66 20 77 68 79 20  Examples of why 
2bc2d 74 68 69 73 0a 20 20 2a 2a 20 69 73 20 6e 6f 74  this.  ** is not
2bc2e 20 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20   allowed:.  **. 
2bc2f 20 2a 2a 20 20 20 20 20 20 20 20 20 74 31 20 4c   **         t1 L
2bc30 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28  EFT OUTER JOIN (
2bc31 74 32 20 4a 4f 49 4e 20 74 33 29 0a 20 20 2a 2a  t2 JOIN t3).  **
2bc32 0a 20 20 2a 2a 20 49 66 20 77 65 20 66 6c 61 74  .  ** If we flat
2bc33 74 65 6e 20 74 68 65 20 61 62 6f 76 65 2c 20 77  ten the above, w
2bc34 65 20 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a  e would get.  **
2bc35 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 28 74  .  **         (t
2bc36 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  1 LEFT OUTER JOI
2bc37 4e 20 74 32 29 20 4a 4f 49 4e 20 74 33 0a 20 20  N t2) JOIN t3.  
2bc38 2a 2a 0a 20 20 2a 2a 20 77 68 69 63 68 20 69 73  **.  ** which is
2bc39 20 6e 6f 74 20 61 74 20 61 6c 6c 20 74 68 65 20   not at all the 
2bc3a 73 61 6d 65 20 74 68 69 6e 67 2e 0a 20 20 2a 2a  same thing..  **
2bc3b 0a 20 20 2a 2a 20 4f 42 53 4f 4c 45 54 45 20 43  .  ** OBSOLETE C
2bc3c 4f 4d 4d 45 4e 54 20 32 3a 0a 20 20 2a 2a 20 52  OMMENT 2:.  ** R
2bc3d 65 73 74 72 69 63 74 69 6f 6e 20 31 32 3a 20 20  estriction 12:  
2bc3e 49 66 20 74 68 65 20 73 75 62 71 75 65 72 79 20  If the subquery 
2bc3f 69 73 20 74 68 65 20 72 69 67 68 74 20 6f 70 65  is the right ope
2bc40 72 61 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f  rand of a left o
2bc41 75 74 65 72 0a 20 20 2a 2a 20 6a 6f 69 6e 2c 20  uter.  ** join, 
2bc42 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 75  make sure the su
2bc43 62 71 75 65 72 79 20 68 61 73 20 6e 6f 20 57 48  bquery has no WH
2bc44 45 52 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a  ERE clause..  **
2bc45 20 41 6e 20 65 78 61 6d 70 6c 65 73 20 6f 66 20   An examples of 
2bc46 77 68 79 20 74 68 69 73 20 69 73 20 6e 6f 74 20  why this is not 
2bc47 61 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20  allowed:.  **.  
2bc48 2a 2a 20 20 20 20 20 20 20 20 20 74 31 20 4c 45  **         t1 LE
2bc49 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28 53  FT OUTER JOIN (S
2bc4a 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 32 20  ELECT * FROM t2 
2bc4b 57 48 45 52 45 20 74 32 2e 78 3e 30 29 0a 20 20  WHERE t2.x>0).  
2bc4c 2a 2a 0a 20 20 2a 2a 20 49 66 20 77 65 20 66 6c  **.  ** If we fl
2bc4d 61 74 74 65 6e 20 74 68 65 20 61 62 6f 76 65 2c  atten the above,
2bc4e 20 77 65 20 77 6f 75 6c 64 20 67 65 74 0a 20 20   we would get.  
2bc4f 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  **.  **         
2bc50 28 74 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a  (t1 LEFT OUTER J
2bc51 4f 49 4e 20 74 32 29 20 57 48 45 52 45 20 74 32  OIN t2) WHERE t2
2bc52 2e 78 3e 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42  .x>0.  **.  ** B
2bc53 75 74 20 74 68 65 20 74 32 2e 78 3e 30 20 74 65  ut the t2.x>0 te
2bc54 73 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 66  st will always f
2bc55 61 69 6c 20 6f 6e 20 61 20 4e 55 4c 4c 20 72 6f  ail on a NULL ro
2bc56 77 20 6f 66 20 74 32 2c 20 77 68 69 63 68 0a 20  w of t2, which. 
2bc57 20 2a 2a 20 65 66 66 65 63 74 69 76 65 6c 79 20   ** effectively 
2bc58 63 6f 6e 76 65 72 74 73 20 74 68 65 20 4f 55 54  converts the OUT
2bc59 45 52 20 4a 4f 49 4e 20 69 6e 74 6f 20 61 6e 20  ER JOIN into an 
2bc5a 49 4e 4e 45 52 20 4a 4f 49 4e 2e 0a 20 20 2a 2a  INNER JOIN..  **
2bc5b 0a 20 20 2a 2a 20 54 48 49 53 20 4f 56 45 52 52  .  ** THIS OVERR
2bc5c 49 44 45 53 20 4f 42 53 4f 4c 45 54 45 20 43 4f  IDES OBSOLETE CO
2bc5d 4d 4d 45 4e 54 53 20 31 20 41 4e 44 20 32 20 41  MMENTS 1 AND 2 A
2bc5e 42 4f 56 45 3a 0a 20 20 2a 2a 20 54 69 63 6b 65  BOVE:.  ** Ticke
2bc5f 74 20 23 33 33 30 30 20 73 68 6f 77 73 20 74 68  t #3300 shows th
2bc60 61 74 20 66 6c 61 74 74 65 6e 69 6e 67 20 74 68  at flattening th
2bc61 65 20 72 69 67 68 74 20 74 65 72 6d 20 6f 66 20  e right term of 
2bc62 61 20 4c 45 46 54 20 4a 4f 49 4e 0a 20 20 2a 2a  a LEFT JOIN.  **
2bc63 20 69 73 20 66 72 61 75 67 68 74 20 77 69 74 68   is fraught with
2bc64 20 64 61 6e 67 65 72 2e 20 20 42 65 73 74 20 74   danger.  Best t
2bc65 6f 20 61 76 6f 69 64 20 74 68 65 20 77 68 6f 6c  o avoid the whol
2bc66 65 20 74 68 69 6e 67 2e 20 20 49 66 20 74 68 65  e thing.  If the
2bc67 0a 20 20 2a 2a 20 73 75 62 71 75 65 72 79 20 69  .  ** subquery i
2bc68 73 20 74 68 65 20 72 69 67 68 74 20 74 65 72 6d  s the right term
2bc69 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2c   of a LEFT JOIN,
2bc6a 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 66 6c 61   then do not fla
2bc6b 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tten..  */.  if(
2bc6c 20 28 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e   (pSubitem->join
2bc6d 74 79 70 65 20 26 20 4a 54 5f 4f 55 54 45 52 29  type & JT_OUTER)
2bc6e 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=0 ){.    retur
2bc6f 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  n 0;.  }..  /* R
2bc70 65 73 74 72 69 63 74 69 6f 6e 20 31 37 3a 20 49  estriction 17: I
2bc71 66 20 74 68 65 20 73 75 62 2d 71 75 65 72 79 20  f the sub-query 
2bc72 69 73 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45  is a compound SE
2bc73 4c 45 43 54 2c 20 74 68 65 6e 20 69 74 20 6d 75  LECT, then it mu
2bc74 73 74 0a 20 20 2a 2a 20 75 73 65 20 6f 6e 6c 79  st.  ** use only
2bc75 20 74 68 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f   the UNION ALL o
2bc76 70 65 72 61 74 6f 72 2e 20 41 6e 64 20 6e 6f 6e  perator. And non
2bc77 65 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 20  e of the simple 
2bc78 73 65 6c 65 63 74 20 71 75 65 72 69 65 73 0a 20  select queries. 
2bc79 20 2a 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70   ** that make up
2bc7a 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45   the compound SE
2bc7b 4c 45 43 54 20 61 72 65 20 61 6c 6c 6f 77 65 64  LECT are allowed
2bc7c 20 74 6f 20 62 65 20 61 67 67 72 65 67 61 74 65   to be aggregate
2bc7d 20 6f 72 20 64 69 73 74 69 6e 63 74 0a 20 20 2a   or distinct.  *
2bc7e 2a 20 71 75 65 72 69 65 73 2e 0a 20 20 2a 2f 0a  * queries..  */.
2bc7f 20 20 69 66 28 20 70 53 75 62 2d 3e 70 50 72 69    if( pSub->pPri
2bc80 6f 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 53  or ){.    if( pS
2bc81 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  ub->pOrderBy ){.
2bc82 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20        return 0; 
2bc83 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
2bc84 32 30 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  20 */.    }.    
2bc85 69 66 28 20 69 73 41 67 67 20 7c 7c 20 28 70 2d  if( isAgg || (p-
2bc86 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44  >selFlags & SF_D
2bc87 69 73 74 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70  istinct)!=0 || p
2bc88 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a  Src->nSrc!=1 ){.
2bc89 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
2bc8a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 53      }.    for(pS
2bc8b 75 62 31 3d 70 53 75 62 3b 20 70 53 75 62 31 3b  ub1=pSub; pSub1;
2bc8c 20 70 53 75 62 31 3d 70 53 75 62 31 2d 3e 70 50   pSub1=pSub1->pP
2bc8d 72 69 6f 72 29 7b 0a 20 20 20 20 20 20 74 65 73  rior){.      tes
2bc8e 74 63 61 73 65 28 20 28 70 53 75 62 31 2d 3e 73  tcase( (pSub1->s
2bc8f 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69  elFlags & (SF_Di
2bc90 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67  stinct|SF_Aggreg
2bc91 61 74 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e  ate))==SF_Distin
2bc92 63 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ct );.      test
2bc93 63 61 73 65 28 20 28 70 53 75 62 31 2d 3e 73 65  case( (pSub1->se
2bc94 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73  lFlags & (SF_Dis
2bc95 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61  tinct|SF_Aggrega
2bc96 74 65 29 29 3d 3d 53 46 5f 41 67 67 72 65 67 61  te))==SF_Aggrega
2bc97 74 65 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  te );.      if( 
2bc98 28 70 53 75 62 31 2d 3e 73 65 6c 46 6c 61 67 73  (pSub1->selFlags
2bc99 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c   & (SF_Distinct|
2bc9a 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 21 3d  SF_Aggregate))!=
2bc9b 30 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 53 75  0.       || (pSu
2bc9c 62 31 2d 3e 70 50 72 69 6f 72 20 26 26 20 70 53  b1->pPrior && pS
2bc9d 75 62 31 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29  ub1->op!=TK_ALL)
2bc9e 20 0a 20 20 20 20 20 20 20 7c 7c 20 4e 45 56 45   .       || NEVE
2bc9f 52 28 70 53 75 62 31 2d 3e 70 53 72 63 3d 3d 30  R(pSub1->pSrc==0
2bca0 29 20 7c 7c 20 70 53 75 62 31 2d 3e 70 53 72 63  ) || pSub1->pSrc
2bca1 2d 3e 6e 53 72 63 21 3d 31 0a 20 20 20 20 20 20  ->nSrc!=1.      
2bca2 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
2bca3 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
2bca4 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 74 72   }..    /* Restr
2bca5 69 63 74 69 6f 6e 20 31 38 2e 20 2a 2f 0a 20 20  iction 18. */.  
2bca6 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42    if( p->pOrderB
2bca7 79 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  y ){.      int i
2bca8 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d  i;.      for(ii=
2bca9 30 3b 20 69 69 3c 70 2d 3e 70 4f 72 64 65 72 42  0; ii<p->pOrderB
2bcaa 79 2d 3e 6e 45 78 70 72 3b 20 69 69 2b 2b 29 7b  y->nExpr; ii++){
2bcab 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e  .        if( p->
2bcac 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 69 5d 2e  pOrderBy->a[ii].
2bcad 69 43 6f 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e  iCol==0 ) return
2bcae 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
2bcaf 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 2a 2a 2a 2a 20  }.  }..  /***** 
2bcb0 49 66 20 77 65 20 72 65 61 63 68 20 74 68 69 73  If we reach this
2bcb1 20 70 6f 69 6e 74 2c 20 66 6c 61 74 74 65 6e 69   point, flatteni
2bcb2 6e 67 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e  ng is permitted.
2bcb3 20 2a 2a 2a 2a 2a 2f 0a 0a 20 20 2f 2a 20 41 75   *****/..  /* Au
2bcb4 74 68 6f 72 69 7a 65 20 74 68 65 20 73 75 62 71  thorize the subq
2bcb5 75 65 72 79 20 2a 2f 0a 20 20 70 50 61 72 73 65  uery */.  pParse
2bcb6 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d  ->zAuthContext =
2bcb7 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65   pSubitem->zName
2bcb8 3b 0a 20 20 73 71 6c 69 74 65 33 41 75 74 68 43  ;.  sqlite3AuthC
2bcb9 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c  heck(pParse, SQL
2bcba 49 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30  ITE_SELECT, 0, 0
2bcbb 2c 20 30 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  , 0);.  pParse->
2bcbc 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a  zAuthContext = z
2bcbd 53 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74  SavedAuthContext
2bcbe 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73  ;..  /* If the s
2bcbf 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f  ub-query is a co
2bcc0 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74  mpound SELECT st
2bcc1 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 28 62  atement, then (b
2bcc2 79 20 72 65 73 74 72 69 63 74 69 6f 6e 73 0a 20  y restrictions. 
2bcc3 20 2a 2a 20 31 37 20 61 6e 64 20 31 38 20 61 62   ** 17 and 18 ab
2bcc4 6f 76 65 29 20 69 74 20 6d 75 73 74 20 62 65 20  ove) it must be 
2bcc5 61 20 55 4e 49 4f 4e 20 41 4c 4c 20 61 6e 64 20  a UNION ALL and 
2bcc6 74 68 65 20 70 61 72 65 6e 74 20 71 75 65 72 79  the parent query
2bcc7 20 6d 75 73 74 20 0a 20 20 2a 2a 20 62 65 20 6f   must .  ** be o
2bcc8 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a  f the form:.  **
2bcc9 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  .  **     SELECT
2bcca 20 3c 65 78 70 72 2d 6c 69 73 74 3e 20 46 52 4f   <expr-list> FRO
2bccb 4d 20 28 3c 73 75 62 2d 71 75 65 72 79 3e 29 20  M (<sub-query>) 
2bccc 3c 77 68 65 72 65 2d 63 6c 61 75 73 65 3e 20 0a  <where-clause> .
2bccd 20 20 2a 2a 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77    **.  ** follow
2bcce 65 64 20 62 79 20 61 6e 79 20 4f 52 44 45 52 20  ed by any ORDER 
2bccf 42 59 2c 20 4c 49 4d 49 54 20 61 6e 64 2f 6f 72  BY, LIMIT and/or
2bcd0 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e   OFFSET clauses.
2bcd1 20 54 68 69 73 20 62 6c 6f 63 6b 0a 20 20 2a 2a   This block.  **
2bcd2 20 63 72 65 61 74 65 73 20 4e 2d 31 20 63 6f 70   creates N-1 cop
2bcd3 69 65 73 20 6f 66 20 74 68 65 20 70 61 72 65 6e  ies of the paren
2bcd4 74 20 71 75 65 72 79 20 77 69 74 68 6f 75 74 20  t query without 
2bcd5 61 6e 79 20 4f 52 44 45 52 20 42 59 2c 20 4c 49  any ORDER BY, LI
2bcd6 4d 49 54 20 6f 72 20 0a 20 20 2a 2a 20 4f 46 46  MIT or .  ** OFF
2bcd7 53 45 54 20 63 6c 61 75 73 65 73 20 61 6e 64 20  SET clauses and 
2bcd8 6a 6f 69 6e 73 20 74 68 65 6d 20 74 6f 20 74 68  joins them to th
2bcd9 65 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64 65  e left-hand-side
2bcda 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
2bcdb 0a 20 20 2a 2a 20 75 73 69 6e 67 20 55 4e 49 4f  .  ** using UNIO
2bcdc 4e 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 73 2e  N ALL operators.
2bcdd 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 4e 20   In this case N 
2bcde 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
2bcdf 20 73 69 6d 70 6c 65 0a 20 20 2a 2a 20 73 65 6c   simple.  ** sel
2bce0 65 63 74 20 73 74 61 74 65 6d 65 6e 74 73 20 69  ect statements i
2bce1 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  n the compound s
2bce2 75 62 2d 71 75 65 72 79 2e 0a 20 20 2a 2a 0a 20  ub-query..  **. 
2bce3 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 2a   ** Example:.  *
2bce4 2a 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43  *.  **     SELEC
2bce5 54 20 61 2b 31 20 46 52 4f 4d 20 28 0a 20 20 2a  T a+1 FROM (.  *
2bce6 2a 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20  *        SELECT 
2bce7 78 20 46 52 4f 4d 20 74 61 62 0a 20 20 2a 2a 20  x FROM tab.  ** 
2bce8 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c         UNION ALL
2bce9 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45 4c  .  **        SEL
2bcea 45 43 54 20 79 20 46 52 4f 4d 20 74 61 62 0a 20  ECT y FROM tab. 
2bceb 20 2a 2a 20 20 20 20 20 20 20 20 55 4e 49 4f 4e   **        UNION
2bcec 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 20 20   ALL.  **       
2bced 20 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29   SELECT abs(z*2)
2bcee 20 46 52 4f 4d 20 74 61 62 32 0a 20 20 2a 2a 20   FROM tab2.  ** 
2bcef 20 20 20 20 29 20 57 48 45 52 45 20 61 21 3d 35      ) WHERE a!=5
2bcf0 20 4f 52 44 45 52 20 42 59 20 31 0a 20 20 2a 2a   ORDER BY 1.  **
2bcf1 0a 20 20 2a 2a 20 54 72 61 6e 73 66 6f 72 6d 65  .  ** Transforme
2bcf2 64 20 69 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a  d into:.  **.  *
2bcf3 2a 20 20 20 20 20 53 45 4c 45 43 54 20 78 2b 31  *     SELECT x+1
2bcf4 20 46 52 4f 4d 20 74 61 62 20 57 48 45 52 45 20   FROM tab WHERE 
2bcf5 78 2b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20  x+1!=5.  **     
2bcf6 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20  UNION ALL.  **  
2bcf7 20 20 20 53 45 4c 45 43 54 20 79 2b 31 20 46 52     SELECT y+1 FR
2bcf8 4f 4d 20 74 61 62 20 57 48 45 52 45 20 79 2b 31  OM tab WHERE y+1
2bcf9 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 55 4e 49  !=5.  **     UNI
2bcfa 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20  ON ALL.  **     
2bcfb 53 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29 2b  SELECT abs(z*2)+
2bcfc 31 20 46 52 4f 4d 20 74 61 62 32 20 57 48 45 52  1 FROM tab2 WHER
2bcfd 45 20 61 62 73 28 7a 2a 32 29 2b 31 21 3d 35 0a  E abs(z*2)+1!=5.
2bcfe 20 20 2a 2a 20 20 20 20 20 4f 52 44 45 52 20 42    **     ORDER B
2bcff 59 20 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65  Y 1.  **.  ** We
2bd00 20 63 61 6c 6c 20 74 68 69 73 20 74 68 65 20 22   call this the "
2bd01 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72  compound-subquer
2bd02 79 20 66 6c 61 74 74 65 6e 69 6e 67 22 2e 0a 20  y flattening".. 
2bd03 20 2a 2f 0a 20 20 66 6f 72 28 70 53 75 62 3d 70   */.  for(pSub=p
2bd04 53 75 62 2d 3e 70 50 72 69 6f 72 3b 20 70 53 75  Sub->pPrior; pSu
2bd05 62 3b 20 70 53 75 62 3d 70 53 75 62 2d 3e 70 50  b; pSub=pSub->pP
2bd06 72 69 6f 72 29 7b 0a 20 20 20 20 53 65 6c 65 63  rior){.    Selec
2bd07 74 20 2a 70 4e 65 77 3b 0a 20 20 20 20 45 78 70  t *pNew;.    Exp
2bd08 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20  rList *pOrderBy 
2bd09 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20  = p->pOrderBy;. 
2bd0a 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 20     Expr *pLimit 
2bd0b 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20  = p->pLimit;.   
2bd0c 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 20   Select *pPrior 
2bd0d 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20  = p->pPrior;.   
2bd0e 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30   p->pOrderBy = 0
2bd0f 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20  ;.    p->pSrc = 
2bd10 30 3b 0a 20 20 20 20 70 2d 3e 70 50 72 69 6f 72  0;.    p->pPrior
2bd11 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c 69   = 0;.    p->pLi
2bd12 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 70 4e 65  mit = 0;.    pNe
2bd13 77 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63  w = sqlite3Selec
2bd14 74 44 75 70 28 64 62 2c 20 70 2c 20 30 29 3b 0a  tDup(db, p, 0);.
2bd15 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20      p->pLimit = 
2bd16 70 4c 69 6d 69 74 3b 0a 20 20 20 20 70 2d 3e 70  pLimit;.    p->p
2bd17 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72  OrderBy = pOrder
2bd18 42 79 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20  By;.    p->pSrc 
2bd19 3d 20 70 53 72 63 3b 0a 20 20 20 20 70 2d 3e 6f  = pSrc;.    p->o
2bd1a 70 20 3d 20 54 4b 5f 41 4c 4c 3b 0a 20 20 20 20  p = TK_ALL;.    
2bd1b 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  p->pRightmost = 
2bd1c 30 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d  0;.    if( pNew=
2bd1d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77  =0 ){.      pNew
2bd1e 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 7d   = pPrior;.    }
2bd1f 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77  else{.      pNew
2bd20 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f  ->pPrior = pPrio
2bd21 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70  r;.      pNew->p
2bd22 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20  Rightmost = 0;. 
2bd23 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 50 72 69     }.    p->pPri
2bd24 6f 72 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 69  or = pNew;.    i
2bd25 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2bd26 6c 65 64 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  led ) return 1;.
2bd27 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20    }..  /* Begin 
2bd28 66 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20 69  flattening the i
2bd29 46 72 6f 6d 2d 74 68 20 65 6e 74 72 79 20 6f 66  From-th entry of
2bd2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2bd2b 20 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f 75   .  ** in the ou
2bd2c 74 65 72 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a  ter query..  */.
2bd2d 20 20 70 53 75 62 20 3d 20 70 53 75 62 31 20 3d    pSub = pSub1 =
2bd2e 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65   pSubitem->pSele
2bd2f 63 74 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65  ct;..  /* Delete
2bd30 20 74 68 65 20 74 72 61 6e 73 69 65 6e 74 20 74   the transient t
2bd31 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 61  able structure a
2bd32 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2bd33 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65 72 79  he.  ** subquery
2bd34 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44  .  */.  sqlite3D
2bd35 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 74  bFree(db, pSubit
2bd36 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a  em->zDatabase);.
2bd37 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2bd38 64 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e  db, pSubitem->zN
2bd39 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ame);.  sqlite3D
2bd3a 62 46 72 65 65 28 64 62 2c 20 70 53 75 62 69 74  bFree(db, pSubit
2bd3b 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 70  em->zAlias);.  p
2bd3c 53 75 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62 61  Subitem->zDataba
2bd3d 73 65 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74  se = 0;.  pSubit
2bd3e 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20  em->zName = 0;. 
2bd3f 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61   pSubitem->zAlia
2bd40 73 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74 65  s = 0;.  pSubite
2bd41 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 30 3b 0a  m->pSelect = 0;.
2bd42 0a 20 20 2f 2a 20 44 65 66 65 72 20 64 65 6c 65  .  /* Defer dele
2bd43 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65 20 6f  ting the Table o
2bd44 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74 65 64  bject associated
2bd45 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73   with the.  ** s
2bd46 75 62 71 75 65 72 79 20 75 6e 74 69 6c 20 63 6f  ubquery until co
2bd47 64 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 69 73  de generation is
2bd48 0a 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 2c 20  .  ** complete, 
2bd49 73 69 6e 63 65 20 74 68 65 72 65 20 6d 61 79 20  since there may 
2bd4a 73 74 69 6c 6c 20 65 78 69 73 74 20 45 78 70 72  still exist Expr
2bd4b 2e 70 54 61 62 20 65 6e 74 72 69 65 73 20 74 68  .pTab entries th
2bd4c 61 74 0a 20 20 2a 2a 20 72 65 66 65 72 20 74 6f  at.  ** refer to
2bd4d 20 74 68 65 20 73 75 62 71 75 65 72 79 20 65 76   the subquery ev
2bd4e 65 6e 20 61 66 74 65 72 20 66 6c 61 74 74 65 6e  en after flatten
2bd4f 69 6e 67 2e 20 20 54 69 63 6b 65 74 20 23 33 33  ing.  Ticket #33
2bd50 34 36 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 70 53  46..  **.  ** pS
2bd51 75 62 69 74 65 6d 2d 3e 70 54 61 62 20 69 73 20  ubitem->pTab is 
2bd52 61 6c 77 61 79 73 20 6e 6f 6e 2d 4e 55 4c 4c 20  always non-NULL 
2bd53 62 79 20 74 65 73 74 20 72 65 73 74 72 69 63 74  by test restrict
2bd54 69 6f 6e 73 20 61 6e 64 20 74 65 73 74 73 20 61  ions and tests a
2bd55 62 6f 76 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  bove..  */.  if(
2bd56 20 41 4c 57 41 59 53 28 70 53 75 62 69 74 65 6d   ALWAYS(pSubitem
2bd57 2d 3e 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20  ->pTab!=0) ){.  
2bd58 20 20 54 61 62 6c 65 20 2a 70 54 61 62 54 6f 44    Table *pTabToD
2bd59 65 6c 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70  el = pSubitem->p
2bd5a 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61  Tab;.    if( pTa
2bd5b 62 54 6f 44 65 6c 2d 3e 6e 52 65 66 3d 3d 31 20  bToDel->nRef==1 
2bd5c 29 7b 0a 20 20 20 20 20 20 50 61 72 73 65 20 2a  ){.      Parse *
2bd5d 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69  pToplevel = sqli
2bd5e 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
2bd5f 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2bd60 70 54 61 62 54 6f 44 65 6c 2d 3e 70 4e 65 78 74  pTabToDel->pNext
2bd61 5a 6f 6d 62 69 65 20 3d 20 70 54 6f 70 6c 65 76  Zombie = pToplev
2bd62 65 6c 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b 0a  el->pZombieTab;.
2bd63 20 20 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d        pToplevel-
2bd64 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20 70 54  >pZombieTab = pT
2bd65 61 62 54 6f 44 65 6c 3b 0a 20 20 20 20 7d 65 6c  abToDel;.    }el
2bd66 73 65 7b 0a 20 20 20 20 20 20 70 54 61 62 54 6f  se{.      pTabTo
2bd67 44 65 6c 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20  Del->nRef--;.   
2bd68 20 7d 0a 20 20 20 20 70 53 75 62 69 74 65 6d 2d   }.    pSubitem-
2bd69 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 7d 0a 0a  >pTab = 0;.  }..
2bd6a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
2bd6b 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63  ng loop runs onc
2bd6c 65 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20  e for each term 
2bd6d 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 2d 73 75  in a compound-su
2bd6e 62 71 75 65 72 79 0a 20 20 2a 2a 20 66 6c 61 74  bquery.  ** flat
2bd6f 74 65 6e 69 6e 67 20 28 61 73 20 64 65 73 63 72  tening (as descr
2bd70 69 62 65 64 20 61 62 6f 76 65 29 2e 20 20 49 66  ibed above).  If
2bd71 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61 20   we are doing a 
2bd72 64 69 66 66 65 72 65 6e 74 20 6b 69 6e 64 0a 20  different kind. 
2bd73 20 2a 2a 20 6f 66 20 66 6c 61 74 74 65 6e 69 6e   ** of flattenin
2bd74 67 20 2d 20 61 20 66 6c 61 74 74 65 6e 69 6e 67  g - a flattening
2bd75 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20 63 6f   other than a co
2bd76 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79 20  mpound-subquery 
2bd77 66 6c 61 74 74 65 6e 69 6e 67 20 2d 0a 20 20 2a  flattening -.  *
2bd78 2a 20 74 68 65 6e 20 74 68 69 73 20 6c 6f 6f 70  * then this loop
2bd79 20 6f 6e 6c 79 20 72 75 6e 73 20 6f 6e 63 65 2e   only runs once.
2bd7a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
2bd7b 6c 6f 6f 70 20 6d 6f 76 65 73 20 61 6c 6c 20 6f  loop moves all o
2bd7c 66 20 74 68 65 20 46 52 4f 4d 20 65 6c 65 6d 65  f the FROM eleme
2bd7d 6e 74 73 20 6f 66 20 74 68 65 20 73 75 62 71 75  nts of the subqu
2bd7e 65 72 79 20 69 6e 74 6f 20 74 68 65 0a 20 20 2a  ery into the.  *
2bd7f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * the FROM claus
2bd80 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71  e of the outer q
2bd81 75 65 72 79 2e 20 20 42 65 66 6f 72 65 20 64 6f  uery.  Before do
2bd82 69 6e 67 20 74 68 69 73 2c 20 72 65 6d 65 6d 62  ing this, rememb
2bd83 65 72 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 73  er.  ** the curs
2bd84 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  or number for th
2bd85 65 20 6f 72 69 67 69 6e 61 6c 20 6f 75 74 65 72  e original outer
2bd86 20 71 75 65 72 79 20 46 52 4f 4d 20 65 6c 65 6d   query FROM elem
2bd87 65 6e 74 20 69 6e 0a 20 20 2a 2a 20 69 50 61 72  ent in.  ** iPar
2bd88 65 6e 74 2e 20 20 54 68 65 20 69 50 61 72 65 6e  ent.  The iParen
2bd89 74 20 63 75 72 73 6f 72 20 77 69 6c 6c 20 6e 65  t cursor will ne
2bd8a 76 65 72 20 62 65 20 75 73 65 64 2e 20 20 53 75  ver be used.  Su
2bd8b 62 73 65 71 75 65 6e 74 20 63 6f 64 65 0a 20 20  bsequent code.  
2bd8c 2a 2a 20 77 69 6c 6c 20 73 63 61 6e 20 65 78 70  ** will scan exp
2bd8d 72 65 73 73 69 6f 6e 73 20 6c 6f 6f 6b 69 6e 67  ressions looking
2bd8e 20 66 6f 72 20 69 50 61 72 65 6e 74 20 72 65 66   for iParent ref
2bd8f 65 72 65 6e 63 65 73 20 61 6e 64 20 72 65 70 6c  erences and repl
2bd90 61 63 65 0a 20 20 2a 2a 20 74 68 6f 73 65 20 72  ace.  ** those r
2bd91 65 66 65 72 65 6e 63 65 73 20 77 69 74 68 20 65  eferences with e
2bd92 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20  xpressions that 
2bd93 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 73  resolve to the s
2bd94 75 62 71 75 65 72 79 20 46 52 4f 4d 0a 20 20 2a  ubquery FROM.  *
2bd95 2a 20 65 6c 65 6d 65 6e 74 73 20 77 65 20 61 72  * elements we ar
2bd96 65 20 6e 6f 77 20 63 6f 70 79 69 6e 67 20 69 6e  e now copying in
2bd97 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 50 61  ..  */.  for(pPa
2bd98 72 65 6e 74 3d 70 3b 20 70 50 61 72 65 6e 74 3b  rent=p; pParent;
2bd99 20 70 50 61 72 65 6e 74 3d 70 50 61 72 65 6e 74   pParent=pParent
2bd9a 2d 3e 70 50 72 69 6f 72 2c 20 70 53 75 62 3d 70  ->pPrior, pSub=p
2bd9b 53 75 62 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20  Sub->pPrior){.  
2bd9c 20 20 69 6e 74 20 6e 53 75 62 53 72 63 3b 0a 20    int nSubSrc;. 
2bd9d 20 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 20 3d     u8 jointype =
2bd9e 20 30 3b 0a 20 20 20 20 70 53 75 62 53 72 63 20   0;.    pSubSrc 
2bd9f 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b 20 20 20  = pSub->pSrc;   
2bda0 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
2bda1 20 6f 66 20 73 75 62 71 75 65 72 79 20 2a 2f 0a   of subquery */.
2bda2 20 20 20 20 6e 53 75 62 53 72 63 20 3d 20 70 53      nSubSrc = pS
2bda3 75 62 53 72 63 2d 3e 6e 53 72 63 3b 20 20 2f 2a  ubSrc->nSrc;  /*
2bda4 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   Number of terms
2bda5 20 69 6e 20 73 75 62 71 75 65 72 79 20 46 52 4f   in subquery FRO
2bda6 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  M clause */.    
2bda7 70 53 72 63 20 3d 20 70 50 61 72 65 6e 74 2d 3e  pSrc = pParent->
2bda8 70 53 72 63 3b 20 20 20 20 20 2f 2a 20 46 52 4f  pSrc;     /* FRO
2bda9 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20  M clause of the 
2bdaa 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0a 0a  outer query */..
2bdab 20 20 20 20 69 66 28 20 70 53 72 63 20 29 7b 0a      if( pSrc ){.
2bdac 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
2bdad 61 72 65 6e 74 3d 3d 70 20 29 3b 20 20 2f 2a 20  arent==p );  /* 
2bdae 46 69 72 73 74 20 74 69 6d 65 20 74 68 72 6f 75  First time throu
2bdaf 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  gh the loop */. 
2bdb0 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20       jointype = 
2bdb1 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79  pSubitem->jointy
2bdb2 70 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  pe;.    }else{. 
2bdb3 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
2bdb4 72 65 6e 74 21 3d 70 20 29 3b 20 20 2f 2a 20 32  rent!=p );  /* 2
2bdb5 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e  nd and subsequen
2bdb6 74 20 74 69 6d 65 73 20 74 68 72 6f 75 67 68 20  t times through 
2bdb7 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20  the loop */.    
2bdb8 20 20 70 53 72 63 20 3d 20 70 50 61 72 65 6e 74    pSrc = pParent
2bdb9 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33  ->pSrc = sqlite3
2bdba 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62  SrcListAppend(db
2bdbb 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
2bdbc 20 20 69 66 28 20 70 53 72 63 3d 3d 30 20 29 7b    if( pSrc==0 ){
2bdbd 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2bdbe 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2bdbf 64 20 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65  d );.        bre
2bdc0 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2bdc1 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 75  }..    /* The su
2bdc2 62 71 75 65 72 79 20 75 73 65 73 20 61 20 73 69  bquery uses a si
2bdc3 6e 67 6c 65 20 73 6c 6f 74 20 6f 66 20 74 68 65  ngle slot of the
2bdc4 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
2bdc5 74 68 65 20 6f 75 74 65 72 0a 20 20 20 20 2a 2a  the outer.    **
2bdc6 20 71 75 65 72 79 2e 20 20 49 66 20 74 68 65 20   query.  If the 
2bdc7 73 75 62 71 75 65 72 79 20 68 61 73 20 6d 6f 72  subquery has mor
2bdc8 65 20 74 68 61 6e 20 6f 6e 65 20 65 6c 65 6d 65  e than one eleme
2bdc9 6e 74 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 63  nt in its FROM c
2bdca 6c 61 75 73 65 2c 0a 20 20 20 20 2a 2a 20 74 68  lause,.    ** th
2bdcb 65 6e 20 65 78 70 61 6e 64 20 74 68 65 20 6f 75  en expand the ou
2bdcc 74 65 72 20 71 75 65 72 79 20 74 6f 20 6d 61 6b  ter query to mak
2bdcd 65 20 73 70 61 63 65 20 66 6f 72 20 69 74 20 74  e space for it t
2bdce 6f 20 68 6f 6c 64 20 61 6c 6c 20 65 6c 65 6d 65  o hold all eleme
2bdcf 6e 74 73 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  nts.    ** of th
2bdd0 65 20 73 75 62 71 75 65 72 79 2e 0a 20 20 20 20  e subquery..    
2bdd1 2a 2a 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c  **.    ** Exampl
2bdd2 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e:.    **.    **
2bdd3 20 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f      SELECT * FRO
2bdd4 4d 20 74 61 62 41 2c 20 28 53 45 4c 45 43 54 20  M tabA, (SELECT 
2bdd5 2a 20 46 52 4f 4d 20 73 75 62 31 2c 20 73 75 62  * FROM sub1, sub
2bdd6 32 29 2c 20 74 61 62 42 3b 0a 20 20 20 20 2a 2a  2), tabB;.    **
2bdd7 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f 75 74 65  .    ** The oute
2bdd8 72 20 71 75 65 72 79 20 68 61 73 20 33 20 73 6c  r query has 3 sl
2bdd9 6f 74 73 20 69 6e 20 69 74 73 20 46 52 4f 4d 20  ots in its FROM 
2bdda 63 6c 61 75 73 65 2e 20 20 4f 6e 65 20 73 6c 6f  clause.  One slo
2bddb 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  t of the.    ** 
2bddc 6f 75 74 65 72 20 71 75 65 72 79 20 28 74 68 65  outer query (the
2bddd 20 6d 69 64 64 6c 65 20 73 6c 6f 74 29 20 69 73   middle slot) is
2bdde 20 75 73 65 64 20 62 79 20 74 68 65 20 73 75 62   used by the sub
2bddf 71 75 65 72 79 2e 20 20 54 68 65 20 6e 65 78 74  query.  The next
2bde0 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 6f 66  .    ** block of
2bde1 20 63 6f 64 65 20 77 69 6c 6c 20 65 78 70 61 6e   code will expan
2bde2 64 20 74 68 65 20 6f 75 74 20 71 75 65 72 79 20  d the out query 
2bde3 74 6f 20 34 20 73 6c 6f 74 73 2e 20 20 54 68 65  to 4 slots.  The
2bde4 20 6d 69 64 64 6c 65 0a 20 20 20 20 2a 2a 20 73   middle.    ** s
2bde5 6c 6f 74 20 69 73 20 65 78 70 61 6e 64 65 64 20  lot is expanded 
2bde6 74 6f 20 74 77 6f 20 73 6c 6f 74 73 20 69 6e 20  to two slots in 
2bde7 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65 20 73 70  order to make sp
2bde8 61 63 65 20 66 6f 72 20 74 68 65 0a 20 20 20 20  ace for the.    
2bde9 2a 2a 20 74 77 6f 20 65 6c 65 6d 65 6e 74 73 20  ** two elements 
2bdea 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2bdeb 73 65 20 6f 66 20 74 68 65 20 73 75 62 71 75 65  se of the subque
2bdec 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ry..    */.    i
2bded 66 28 20 6e 53 75 62 53 72 63 3e 31 20 29 7b 0a  f( nSubSrc>1 ){.
2bdee 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
2bdef 53 72 63 20 3d 20 70 53 72 63 20 3d 20 73 71 6c  Src = pSrc = sql
2bdf0 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72  ite3SrcListEnlar
2bdf1 67 65 28 64 62 2c 20 70 53 72 63 2c 20 6e 53 75  ge(db, pSrc, nSu
2bdf2 62 53 72 63 2d 31 2c 69 46 72 6f 6d 2b 31 29 3b  bSrc-1,iFrom+1);
2bdf3 0a 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  .      if( db->m
2bdf4 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2bdf5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2bdf6 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
2bdf7 20 2f 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65   /* Transfer the
2bdf8 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
2bdf9 6d 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 71  ms from the subq
2bdfa 75 65 72 79 20 69 6e 74 6f 20 74 68 65 0a 20 20  uery into the.  
2bdfb 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79    ** outer query
2bdfc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  ..    */.    for
2bdfd 28 69 3d 30 3b 20 69 3c 6e 53 75 62 53 72 63 3b  (i=0; i<nSubSrc;
2bdfe 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
2bdff 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
2be00 28 64 62 2c 20 70 53 72 63 2d 3e 61 5b 69 2b 69  (db, pSrc->a[i+i
2be01 46 72 6f 6d 5d 2e 70 55 73 69 6e 67 29 3b 0a 20  From].pUsing);. 
2be02 20 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b 69       pSrc->a[i+i
2be03 46 72 6f 6d 5d 20 3d 20 70 53 75 62 53 72 63 2d  From] = pSubSrc-
2be04 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 6d 65 6d  >a[i];.      mem
2be05 73 65 74 28 26 70 53 75 62 53 72 63 2d 3e 61 5b  set(&pSubSrc->a[
2be06 69 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 53  i], 0, sizeof(pS
2be07 75 62 53 72 63 2d 3e 61 5b 69 5d 29 29 3b 0a 20  ubSrc->a[i]));. 
2be08 20 20 20 7d 0a 20 20 20 20 70 53 72 63 2d 3e 61     }.    pSrc->a
2be09 5b 69 46 72 6f 6d 5d 2e 6a 6f 69 6e 74 79 70 65  [iFrom].jointype
2be0a 20 3d 20 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 0a   = jointype;.  .
2be0b 20 20 20 20 2f 2a 20 4e 6f 77 20 62 65 67 69 6e      /* Now begin
2be0c 20 73 75 62 73 74 69 74 75 74 69 6e 67 20 73 75   substituting su
2be0d 62 71 75 65 72 79 20 72 65 73 75 6c 74 20 73 65  bquery result se
2be0e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 66 6f  t expressions fo
2be0f 72 20 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 65  r .    ** refere
2be10 6e 63 65 73 20 74 6f 20 74 68 65 20 69 50 61 72  nces to the iPar
2be11 65 6e 74 20 69 6e 20 74 68 65 20 6f 75 74 65 72  ent in the outer
2be12 20 71 75 65 72 79 2e 0a 20 20 20 20 2a 2a 20 0a   query..    ** .
2be13 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a      ** Example:.
2be14 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2be15 53 45 4c 45 43 54 20 61 2b 35 2c 20 62 2a 31 30  SELECT a+5, b*10
2be16 20 46 52 4f 4d 20 28 53 45 4c 45 43 54 20 78 2a   FROM (SELECT x*
2be17 33 20 41 53 20 61 2c 20 79 2b 31 30 20 41 53 20  3 AS a, y+10 AS 
2be18 62 20 46 52 4f 4d 20 74 31 29 20 57 48 45 52 45  b FROM t1) WHERE
2be19 20 61 3e 62 3b 0a 20 20 20 20 2a 2a 20 20 20 5c   a>b;.    **   \
2be1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be1b 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f       \__________
2be1c 5f 5f 5f 20 73 75 62 71 75 65 72 79 20 5f 5f 5f  ___ subquery ___
2be1d 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 20  _______/        
2be1e 20 20 2f 0a 20 20 20 20 2a 2a 20 20 20 20 5c 5f    /.    **    \_
2be1f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2be20 5f 5f 5f 5f 20 6f 75 74 65 72 20 71 75 65 72 79  ____ outer query
2be21 20 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f   _______________
2be22 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f  _______________/
2be23 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57  .    **.    ** W
2be24 65 20 6c 6f 6f 6b 20 61 74 20 65 76 65 72 79 20  e look at every 
2be25 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68  expression in th
2be26 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 61 6e  e outer query an
2be27 64 20 65 76 65 72 79 20 70 6c 61 63 65 20 77 65  d every place we
2be28 20 73 65 65 0a 20 20 20 20 2a 2a 20 22 61 22 20   see.    ** "a" 
2be29 77 65 20 73 75 62 73 74 69 74 75 74 65 20 22 78  we substitute "x
2be2a 2a 33 22 20 61 6e 64 20 65 76 65 72 79 20 70 6c  *3" and every pl
2be2b 61 63 65 20 77 65 20 73 65 65 20 22 62 22 20 77  ace we see "b" w
2be2c 65 20 73 75 62 73 74 69 74 75 74 65 20 22 79 2b  e substitute "y+
2be2d 31 30 22 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  10"..    */.    
2be2e 70 4c 69 73 74 20 3d 20 70 50 61 72 65 6e 74 2d  pList = pParent-
2be2f 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 66 6f 72  >pEList;.    for
2be30 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
2be31 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
2be32 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b 69    if( pList->a[i
2be33 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  ].zName==0 ){.  
2be34 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2be35 20 2a 7a 53 70 61 6e 20 3d 20 70 4c 69 73 74 2d   *zSpan = pList-
2be36 3e 61 5b 69 5d 2e 7a 53 70 61 6e 3b 0a 20 20 20  >a[i].zSpan;.   
2be37 20 20 20 20 20 69 66 28 20 41 4c 57 41 59 53 28       if( ALWAYS(
2be38 7a 53 70 61 6e 29 20 29 7b 0a 20 20 20 20 20 20  zSpan) ){.      
2be39 20 20 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e      pList->a[i].
2be3a 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
2be3b 62 53 74 72 44 75 70 28 64 62 2c 20 7a 53 70 61  bStrDup(db, zSpa
2be3c 6e 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  n);.        }.  
2be3d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2be3e 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62  substExprList(db
2be3f 2c 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c 69 73  , pParent->pELis
2be40 74 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62  t, iParent, pSub
2be41 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 69  ->pEList);.    i
2be42 66 28 20 69 73 41 67 67 20 29 7b 0a 20 20 20 20  f( isAgg ){.    
2be43 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28    substExprList(
2be44 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 47 72  db, pParent->pGr
2be45 6f 75 70 42 79 2c 20 69 50 61 72 65 6e 74 2c 20  oupBy, iParent, 
2be46 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  pSub->pEList);. 
2be47 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48       pParent->pH
2be48 61 76 69 6e 67 20 3d 20 73 75 62 73 74 45 78 70  aving = substExp
2be49 72 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70  r(db, pParent->p
2be4a 48 61 76 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c  Having, iParent,
2be4b 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
2be4c 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
2be4d 75 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a  ub->pOrderBy ){.
2be4e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
2be4f 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 3d  arent->pOrderBy=
2be50 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72  =0 );.      pPar
2be51 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20  ent->pOrderBy = 
2be52 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 3b 0a  pSub->pOrderBy;.
2be53 20 20 20 20 20 20 70 53 75 62 2d 3e 70 4f 72 64        pSub->pOrd
2be54 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 7d 65  erBy = 0;.    }e
2be55 6c 73 65 20 69 66 28 20 70 50 61 72 65 6e 74 2d  lse if( pParent-
2be56 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  >pOrderBy ){.   
2be57 20 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74     substExprList
2be58 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 4f  (db, pParent->pO
2be59 72 64 65 72 42 79 2c 20 69 50 61 72 65 6e 74 2c  rderBy, iParent,
2be5a 20 70 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a   pSub->pEList);.
2be5b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53      }.    if( pS
2be5c 75 62 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20  ub->pWhere ){.  
2be5d 20 20 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c      pWhere = sql
2be5e 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2be5f 70 53 75 62 2d 3e 70 57 68 65 72 65 2c 20 30 29  pSub->pWhere, 0)
2be60 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2be61 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20     pWhere = 0;. 
2be62 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 75 62     }.    if( sub
2be63 71 75 65 72 79 49 73 41 67 67 20 29 7b 0a 20 20  queryIsAgg ){.  
2be64 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
2be65 65 6e 74 2d 3e 70 48 61 76 69 6e 67 3d 3d 30 20  ent->pHaving==0 
2be66 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  );.      pParent
2be67 2d 3e 70 48 61 76 69 6e 67 20 3d 20 70 50 61 72  ->pHaving = pPar
2be68 65 6e 74 2d 3e 70 57 68 65 72 65 3b 0a 20 20 20  ent->pWhere;.   
2be69 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65     pParent->pWhe
2be6a 72 65 20 3d 20 70 57 68 65 72 65 3b 0a 20 20 20  re = pWhere;.   
2be6b 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76     pParent->pHav
2be6c 69 6e 67 20 3d 20 73 75 62 73 74 45 78 70 72 28  ing = substExpr(
2be6d 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61  db, pParent->pHa
2be6e 76 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20 70  ving, iParent, p
2be6f 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  Sub->pEList);.  
2be70 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61      pParent->pHa
2be71 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 78  ving = sqlite3Ex
2be72 70 72 41 6e 64 28 64 62 2c 20 70 50 61 72 65 6e  prAnd(db, pParen
2be73 74 2d 3e 70 48 61 76 69 6e 67 2c 20 0a 20 20 20  t->pHaving, .   
2be74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2be76 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2be77 2c 20 70 53 75 62 2d 3e 70 48 61 76 69 6e 67 2c  , pSub->pHaving,
2be78 20 30 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65   0));.      asse
2be79 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 47 72  rt( pParent->pGr
2be7a 6f 75 70 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20  oupBy==0 );.    
2be7b 20 20 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f 75    pParent->pGrou
2be7c 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70  pBy = sqlite3Exp
2be7d 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53 75  rListDup(db, pSu
2be7e 62 2d 3e 70 47 72 6f 75 70 42 79 2c 20 30 29 3b  b->pGroupBy, 0);
2be7f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2be80 20 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72    pParent->pWher
2be81 65 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62  e = substExpr(db
2be82 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72  , pParent->pWher
2be83 65 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62  e, iParent, pSub
2be84 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20  ->pEList);.     
2be85 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65   pParent->pWhere
2be86 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e   = sqlite3ExprAn
2be87 64 28 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70  d(db, pParent->p
2be88 57 68 65 72 65 2c 20 70 57 68 65 72 65 29 3b 0a  Where, pWhere);.
2be89 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20      }.  .    /* 
2be8a 54 68 65 20 66 6c 61 74 74 65 6e 65 64 20 71 75  The flattened qu
2be8b 65 72 79 20 69 73 20 64 69 73 74 69 6e 63 74 20  ery is distinct 
2be8c 69 66 20 65 69 74 68 65 72 20 74 68 65 20 69 6e  if either the in
2be8d 6e 65 72 20 6f 72 20 74 68 65 0a 20 20 20 20 2a  ner or the.    *
2be8e 2a 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73  * outer query is
2be8f 20 64 69 73 74 69 6e 63 74 2e 20 0a 20 20 20 20   distinct. .    
2be90 2a 2f 0a 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  */.    pParent->
2be91 73 65 6c 46 6c 61 67 73 20 7c 3d 20 70 53 75 62  selFlags |= pSub
2be92 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
2be93 44 69 73 74 69 6e 63 74 3b 0a 20 20 0a 20 20 20  Distinct;.  .   
2be94 20 2f 2a 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43   /*.    ** SELEC
2be95 54 20 2e 2e 2e 20 46 52 4f 4d 20 28 53 45 4c 45  T ... FROM (SELE
2be96 43 54 20 2e 2e 2e 20 4c 49 4d 49 54 20 61 20 4f  CT ... LIMIT a O
2be97 46 46 53 45 54 20 62 29 20 4c 49 4d 49 54 20 78  FFSET b) LIMIT x
2be98 20 4f 46 46 53 45 54 20 79 3b 0a 20 20 20 20 2a   OFFSET y;.    *
2be99 2a 0a 20 20 20 20 2a 2a 20 4f 6e 65 20 69 73 20  *.    ** One is 
2be9a 74 65 6d 70 74 65 64 20 74 6f 20 74 72 79 20 74  tempted to try t
2be9b 6f 20 61 64 64 20 61 20 61 6e 64 20 62 20 74 6f  o add a and b to
2be9c 20 63 6f 6d 62 69 6e 65 20 74 68 65 20 6c 69 6d   combine the lim
2be9d 69 74 73 2e 20 20 42 75 74 20 74 68 69 73 0a 20  its.  But this. 
2be9e 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 77     ** does not w
2be9f 6f 72 6b 20 69 66 20 65 69 74 68 65 72 20 6c 69  ork if either li
2bea0 6d 69 74 20 69 73 20 6e 65 67 61 74 69 76 65 2e  mit is negative.
2bea1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
2bea2 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a  pSub->pLimit ){.
2bea3 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70        pParent->p
2bea4 4c 69 6d 69 74 20 3d 20 70 53 75 62 2d 3e 70 4c  Limit = pSub->pL
2bea5 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 53 75 62  imit;.      pSub
2bea6 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->pLimit = 0;.  
2bea7 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69    }.  }..  /* Fi
2bea8 6e 69 61 6c 6c 79 2c 20 64 65 6c 65 74 65 20 77  nially, delete w
2bea9 68 61 74 20 69 73 20 6c 65 66 74 20 6f 66 20 74  hat is left of t
2beaa 68 65 20 73 75 62 71 75 65 72 79 20 61 6e 64 20  he subquery and 
2beab 72 65 74 75 72 6e 0a 20 20 2a 2a 20 73 75 63 63  return.  ** succ
2beac 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ess..  */.  sqli
2bead 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
2beae 64 62 2c 20 70 53 75 62 31 29 3b 0a 0a 20 20 72  db, pSub1);..  r
2beaf 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69  eturn 1;.}.#endi
2beb0 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
2beb1 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2beb2 52 59 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  RY) || !defined(
2beb3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2beb4 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c  ) */../*.** Anal
2beb5 79 7a 65 20 74 68 65 20 53 45 4c 45 43 54 20 73  yze the SELECT s
2beb6 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20  tatement passed 
2beb7 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74  as an argument t
2beb8 6f 20 73 65 65 20 69 66 20 69 74 0a 2a 2a 20 69  o see if it.** i
2beb9 73 20 61 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78  s a min() or max
2beba 28 29 20 71 75 65 72 79 2e 20 52 65 74 75 72 6e  () query. Return
2bebb 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d   WHERE_ORDERBY_M
2bebc 49 4e 20 6f 72 20 57 48 45 52 45 5f 4f 52 44 45  IN or WHERE_ORDE
2bebd 52 42 59 5f 4d 41 58 20 69 66 20 0a 2a 2a 20 69  RBY_MAX if .** i
2bebe 74 20 69 73 2c 20 6f 72 20 30 20 6f 74 68 65 72  t is, or 0 other
2bebf 77 69 73 65 2e 20 41 74 20 70 72 65 73 65 6e 74  wise. At present
2bec0 2c 20 61 20 71 75 65 72 79 20 69 73 20 63 6f 6e  , a query is con
2bec1 73 69 64 65 72 65 64 20 74 6f 20 62 65 0a 2a 2a  sidered to be.**
2bec2 20 61 20 6d 69 6e 28 29 2f 6d 61 78 28 29 20 71   a min()/max() q
2bec3 75 65 72 79 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20  uery if:.**.**  
2bec4 20 31 2e 20 54 68 65 72 65 20 69 73 20 61 20 73   1. There is a s
2bec5 69 6e 67 6c 65 20 6f 62 6a 65 63 74 20 69 6e 20  ingle object in 
2bec6 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
2bec7 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65 72  .**.**   2. Ther
2bec8 65 20 69 73 20 61 20 73 69 6e 67 6c 65 20 65 78  e is a single ex
2bec9 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20  pression in the 
2beca 72 65 73 75 6c 74 20 73 65 74 2c 20 61 6e 64 20  result set, and 
2becb 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 65 69  it is.**      ei
2becc 74 68 65 72 20 6d 69 6e 28 78 29 20 6f 72 20 6d  ther min(x) or m
2becd 61 78 28 78 29 2c 20 77 68 65 72 65 20 78 20 69  ax(x), where x i
2bece 73 20 61 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72  s a column refer
2becf 65 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ence..*/.static 
2bed0 75 38 20 6d 69 6e 4d 61 78 51 75 65 72 79 28 53  u8 minMaxQuery(S
2bed1 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 45 78 70  elect *p){.  Exp
2bed2 72 20 2a 70 45 78 70 72 3b 0a 20 20 45 78 70 72  r *pExpr;.  Expr
2bed3 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 70  List *pEList = p
2bed4 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 69 66 28  ->pEList;..  if(
2bed5 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d   pEList->nExpr!=
2bed6 31 20 29 20 72 65 74 75 72 6e 20 57 48 45 52 45  1 ) return WHERE
2bed7 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b  _ORDERBY_NORMAL;
2bed8 0a 20 20 70 45 78 70 72 20 3d 20 70 45 4c 69 73  .  pExpr = pELis
2bed9 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20  t->a[0].pExpr;. 
2beda 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d   if( pExpr->op!=
2bedb 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20  TK_AGG_FUNCTION 
2bedc 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
2bedd 28 20 4e 45 56 45 52 28 45 78 70 72 48 61 73 50  ( NEVER(ExprHasP
2bede 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
2bedf 50 5f 78 49 73 53 65 6c 65 63 74 29 29 20 29 20  P_xIsSelect)) ) 
2bee0 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 45 4c 69  return 0;.  pELi
2bee1 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  st = pExpr->x.pL
2bee2 69 73 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 73  ist;.  if( pELis
2bee3 74 3d 3d 30 20 7c 7c 20 70 45 4c 69 73 74 2d 3e  t==0 || pEList->
2bee4 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75 72  nExpr!=1 ) retur
2bee5 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 4c 69 73  n 0;.  if( pELis
2bee6 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f  t->a[0].pExpr->o
2bee7 70 21 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p!=TK_AGG_COLUMN
2bee8 20 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f   ) return WHERE_
2bee9 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a  ORDERBY_NORMAL;.
2beea 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
2beeb 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
2beec 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
2beed 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  ;.  if( sqlite3S
2beee 74 72 49 43 6d 70 28 70 45 78 70 72 2d 3e 75 2e  trICmp(pExpr->u.
2beef 7a 54 6f 6b 65 6e 2c 22 6d 69 6e 22 29 3d 3d 30  zToken,"min")==0
2bef0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   ){.    return W
2bef1 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
2bef2 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
2bef3 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 45 78  lite3StrICmp(pEx
2bef4 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d 61  pr->u.zToken,"ma
2bef5 78 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  x")==0 ){.    re
2bef6 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52  turn WHERE_ORDER
2bef7 42 59 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 72 65  BY_MAX;.  }.  re
2bef8 74 75 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52  turn WHERE_ORDER
2bef9 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 7d 0a 0a 2f 2a  BY_NORMAL;.}../*
2befa 0a 2a 2a 20 54 68 65 20 73 65 6c 65 63 74 20 73  .** The select s
2befb 74 61 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20  tatement passed 
2befc 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
2befd 75 6d 65 6e 74 20 69 73 20 61 6e 20 61 67 67 72  ument is an aggr
2befe 65 67 61 74 65 20 71 75 65 72 79 2e 0a 2a 2a 20  egate query..** 
2beff 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 6d 65  The second argme
2bf00 6e 74 20 69 73 20 74 68 65 20 61 73 73 6f 63 69  nt is the associ
2bf01 61 74 65 64 20 61 67 67 72 65 67 61 74 65 2d 69  ated aggregate-i
2bf02 6e 66 6f 20 6f 62 6a 65 63 74 2e 20 54 68 69 73  nfo object. This
2bf03 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 65   .** function te
2bf04 73 74 73 20 69 66 20 74 68 65 20 53 45 4c 45 43  sts if the SELEC
2bf05 54 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  T is of the form
2bf06 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54  :.**.**   SELECT
2bf07 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c   count(*) FROM <
2bf08 74 62 6c 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65  tbl>.**.** where
2bf09 20 74 61 62 6c 65 20 69 73 20 61 20 64 61 74 61   table is a data
2bf0a 62 61 73 65 20 74 61 62 6c 65 2c 20 6e 6f 74 20  base table, not 
2bf0b 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20  a sub-select or 
2bf0c 76 69 65 77 2e 20 49 66 20 74 68 65 20 71 75 65  view. If the que
2bf0d 72 79 0a 2a 2a 20 64 6f 65 73 20 6d 61 74 63 68  ry.** does match
2bf0e 20 74 68 69 73 20 70 61 74 74 65 72 6e 2c 20 74   this pattern, t
2bf0f 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  hen a pointer to
2bf10 20 74 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63   the Table objec
2bf11 74 20 72 65 70 72 65 73 65 6e 74 69 6e 67 0a 2a  t representing.*
2bf12 2a 20 3c 74 62 6c 3e 20 69 73 20 72 65 74 75 72  * <tbl> is retur
2bf13 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ned. Otherwise, 
2bf14 30 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  0 is returned..*
2bf15 2f 0a 73 74 61 74 69 63 20 54 61 62 6c 65 20 2a  /.static Table *
2bf16 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 53 65  isSimpleCount(Se
2bf17 6c 65 63 74 20 2a 70 2c 20 41 67 67 49 6e 66 6f  lect *p, AggInfo
2bf18 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 54   *pAggInfo){.  T
2bf19 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 45 78  able *pTab;.  Ex
2bf1a 70 72 20 2a 70 45 78 70 72 3b 0a 0a 20 20 61 73  pr *pExpr;..  as
2bf1b 73 65 72 74 28 20 21 70 2d 3e 70 47 72 6f 75 70  sert( !p->pGroup
2bf1c 42 79 20 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e  By );..  if( p->
2bf1d 70 57 68 65 72 65 20 7c 7c 20 70 2d 3e 70 45 4c  pWhere || p->pEL
2bf1e 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20 0a 20  ist->nExpr!=1 . 
2bf1f 20 20 7c 7c 20 70 2d 3e 70 53 72 63 2d 3e 6e 53    || p->pSrc->nS
2bf20 72 63 21 3d 31 20 7c 7c 20 70 2d 3e 70 53 72 63  rc!=1 || p->pSrc
2bf21 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63 74 0a 20  ->a[0].pSelect. 
2bf22 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2bf23 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 70  ;.  }.  pTab = p
2bf24 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61  ->pSrc->a[0].pTa
2bf25 62 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 2d 3e  b;.  pExpr = p->
2bf26 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
2bf27 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54  pr;.  assert( pT
2bf28 61 62 20 26 26 20 21 70 54 61 62 2d 3e 70 53 65  ab && !pTab->pSe
2bf29 6c 65 63 74 20 26 26 20 70 45 78 70 72 20 29 3b  lect && pExpr );
2bf2a 0a 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  ..  if( IsVirtua
2bf2b 6c 28 70 54 61 62 29 20 29 20 72 65 74 75 72 6e  l(pTab) ) return
2bf2c 20 30 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d   0;.  if( pExpr-
2bf2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e 43  >op!=TK_AGG_FUNC
2bf2e 54 49 4f 4e 20 29 20 72 65 74 75 72 6e 20 30 3b  TION ) return 0;
2bf2f 0a 20 20 69 66 28 20 28 70 41 67 67 49 6e 66 6f  .  if( (pAggInfo
2bf30 2d 3e 61 46 75 6e 63 5b 30 5d 2e 70 46 75 6e 63  ->aFunc[0].pFunc
2bf31 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46  ->flags&SQLITE_F
2bf32 55 4e 43 5f 43 4f 55 4e 54 29 3d 3d 30 20 29 20  UNC_COUNT)==0 ) 
2bf33 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
2bf34 70 45 78 70 72 2d 3e 66 6c 61 67 73 26 45 50 5f  pExpr->flags&EP_
2bf35 44 69 73 74 69 6e 63 74 20 29 20 72 65 74 75 72  Distinct ) retur
2bf36 6e 20 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70  n 0;..  return p
2bf37 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66  Tab;.}../*.** If
2bf38 20 74 68 65 20 73 6f 75 72 63 65 2d 6c 69 73 74   the source-list
2bf39 20 69 74 65 6d 20 70 61 73 73 65 64 20 61 73 20   item passed as 
2bf3a 61 6e 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20  an argument was 
2bf3b 61 75 67 6d 65 6e 74 65 64 20 77 69 74 68 20 61  augmented with a
2bf3c 6e 0a 2a 2a 20 49 4e 44 45 58 45 44 20 42 59 20  n.** INDEXED BY 
2bf3d 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 74 72 79  clause, then try
2bf3e 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73   to locate the s
2bf3f 70 65 63 69 66 69 65 64 20 69 6e 64 65 78 2e 20  pecified index. 
2bf40 49 66 20 74 68 65 72 65 0a 2a 2a 20 77 61 73 20  If there.** was 
2bf41 73 75 63 68 20 61 20 63 6c 61 75 73 65 20 61 6e  such a clause an
2bf42 64 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65  d the named inde
2bf43 78 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  x cannot be foun
2bf44 64 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51  d, return .** SQ
2bf45 4c 49 54 45 5f 45 52 52 4f 52 20 61 6e 64 20 6c  LITE_ERROR and l
2bf46 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e  eave an error in
2bf47 20 70 50 61 72 73 65 2e 20 4f 74 68 65 72 77 69   pParse. Otherwi
2bf48 73 65 2c 20 70 6f 70 75 6c 61 74 65 20 0a 2a 2a  se, populate .**
2bf49 20 70 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 20 61   pFrom->pIndex a
2bf4a 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
2bf4b 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  _OK..*/.SQLITE_P
2bf4c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2bf4d 65 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75  e3IndexedByLooku
2bf4e 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  p(Parse *pParse,
2bf4f 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2bf50 69 74 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20  item *pFrom){.  
2bf51 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20  if( pFrom->pTab 
2bf52 26 26 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78  && pFrom->zIndex
2bf53 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
2bf54 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61  Tab = pFrom->pTa
2bf55 62 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 49 6e  b;.    char *zIn
2bf56 64 65 78 20 3d 20 70 46 72 6f 6d 2d 3e 7a 49 6e  dex = pFrom->zIn
2bf57 64 65 78 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a  dex;.    Index *
2bf58 70 49 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49  pIdx;.    for(pI
2bf59 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
2bf5a 20 0a 20 20 20 20 20 20 20 20 70 49 64 78 20 26   .        pIdx &
2bf5b 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
2bf5c 28 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 49  (pIdx->zName, zI
2bf5d 6e 64 65 78 29 3b 20 0a 20 20 20 20 20 20 20 20  ndex); .        
2bf5e 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
2bf5f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20  .    );.    if( 
2bf60 21 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 73  !pIdx ){.      s
2bf61 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2bf62 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20  Parse, "no such 
2bf63 69 6e 64 65 78 3a 20 25 73 22 2c 20 7a 49 6e 64  index: %s", zInd
2bf64 65 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65  ex, 0);.      re
2bf65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
2bf66 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 46 72  R;.    }.    pFr
2bf67 6f 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 64  om->pIndex = pId
2bf68 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  x;.  }.  return 
2bf69 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2bf6a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2bf6b 20 69 73 20 61 20 57 61 6c 6b 65 72 20 63 61 6c   is a Walker cal
2bf6c 6c 62 61 63 6b 20 66 6f 72 20 22 65 78 70 61 6e  lback for "expan
2bf6d 64 69 6e 67 22 20 61 20 53 45 4c 45 43 54 20 73  ding" a SELECT s
2bf6e 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 22 45 78  tatement..** "Ex
2bf6f 70 61 6e 64 69 6e 67 22 20 6d 65 61 6e 73 20 74  panding" means t
2bf70 6f 20 64 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  o do the followi
2bf71 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29  ng:.**.**    (1)
2bf72 20 20 4d 61 6b 65 20 73 75 72 65 20 56 44 42 45    Make sure VDBE
2bf73 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2bf74 68 61 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e  have been assign
2bf75 65 64 20 74 6f 20 65 76 65 72 79 0a 2a 2a 20 20  ed to every.**  
2bf76 20 20 20 20 20 20 20 65 6c 65 6d 65 6e 74 20 6f         element o
2bf77 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  f the FROM claus
2bf78 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20  e..**.**    (2) 
2bf79 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 54 61   Fill in the pTa
2bf7a 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70 54 61 62 20  bList->a[].pTab 
2bf7b 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 53 72  fields in the Sr
2bf7c 63 4c 69 73 74 20 74 68 61 74 20 0a 2a 2a 20 20  cList that .**  
2bf7d 20 20 20 20 20 20 20 64 65 66 69 6e 65 73 20 46         defines F
2bf7e 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 57 68 65  ROM clause.  Whe
2bf7f 6e 20 76 69 65 77 73 20 61 70 70 65 61 72 20 69  n views appear i
2bf80 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
2bf81 65 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 69  e,.**         fi
2bf82 6c 6c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d  ll pTabList->a[]
2bf83 2e 70 53 65 6c 65 63 74 20 77 69 74 68 20 61 20  .pSelect with a 
2bf84 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45  copy of the SELE
2bf85 43 54 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  CT statement.** 
2bf86 20 20 20 20 20 20 20 20 74 68 61 74 20 69 6d 70          that imp
2bf87 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77  lements the view
2bf88 2e 20 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64  .  A copy is mad
2bf89 65 20 6f 66 20 74 68 65 20 76 69 65 77 27 73 20  e of the view's 
2bf8a 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20  SELECT.**       
2bf8b 20 20 73 74 61 74 65 6d 65 6e 74 20 73 6f 20 74    statement so t
2bf8c 68 61 74 20 77 65 20 63 61 6e 20 66 72 65 65 6c  hat we can freel
2bf8d 79 20 6d 6f 64 69 66 79 20 6f 72 20 64 65 6c 65  y modify or dele
2bf8e 74 65 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e  te that statemen
2bf8f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 77 69 74  t.**         wit
2bf90 68 6f 75 74 20 77 6f 72 72 79 69 6e 67 20 61 62  hout worrying ab
2bf91 6f 75 74 20 6d 65 73 73 69 6e 67 20 75 70 20 74  out messing up t
2bf92 68 65 20 70 72 65 73 69 73 74 65 6e 74 20 72 65  he presistent re
2bf93 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  presentation.** 
2bf94 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 76          of the v
2bf95 69 65 77 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33  iew..**.**    (3
2bf96 29 20 20 41 64 64 20 74 65 72 6d 73 20 74 6f 20  )  Add terms to 
2bf97 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2bf98 20 74 6f 20 61 63 63 6f 6d 6f 64 61 74 65 20 74   to accomodate t
2bf99 68 65 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f  he NATURAL keywo
2bf9a 72 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 6e  rd.**         on
2bf9b 20 6a 6f 69 6e 73 20 61 6e 64 20 74 68 65 20 4f   joins and the O
2bf9c 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75  N and USING clau
2bf9d 73 65 20 6f 66 20 6a 6f 69 6e 73 2e 0a 2a 2a 0a  se of joins..**.
2bf9e 2a 2a 20 20 20 20 28 34 29 20 20 53 63 61 6e 20  **    (4)  Scan 
2bf9f 74 68 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75  the list of colu
2bfa0 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  mns in the resul
2bfa1 74 20 73 65 74 20 28 70 45 4c 69 73 74 29 20 6c  t set (pEList) l
2bfa2 6f 6f 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20  ooking.**       
2bfa3 20 20 66 6f 72 20 69 6e 73 74 61 6e 63 65 73 20    for instances 
2bfa4 6f 66 20 74 68 65 20 22 2a 22 20 6f 70 65 72 61  of the "*" opera
2bfa5 74 6f 72 20 6f 72 20 74 68 65 20 54 41 42 4c 45  tor or the TABLE
2bfa6 2e 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20  .* operator..** 
2bfa7 20 20 20 20 20 20 20 20 49 66 20 66 6f 75 6e 64          If found
2bfa8 2c 20 65 78 70 61 6e 64 20 65 61 63 68 20 22 2a  , expand each "*
2bfa9 22 20 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f  " to be every co
2bfaa 6c 75 6d 6e 20 69 6e 20 65 76 65 72 79 20 74 61  lumn in every ta
2bfab 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 61  ble.**         a
2bfac 6e 64 20 54 41 42 4c 45 2e 2a 20 74 6f 20 62 65  nd TABLE.* to be
2bfad 20 65 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e   every column in
2bfae 20 54 41 42 4c 45 2e 0a 2a 2a 0a 2a 2f 0a 73 74   TABLE..**.*/.st
2bfaf 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 45  atic int selectE
2bfb0 78 70 61 6e 64 65 72 28 57 61 6c 6b 65 72 20 2a  xpander(Walker *
2bfb1 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20  pWalker, Select 
2bfb2 2a 70 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  *p){.  Parse *pP
2bfb3 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e  arse = pWalker->
2bfb4 70 50 61 72 73 65 3b 0a 20 20 69 6e 74 20 69 2c  pParse;.  int i,
2bfb5 20 6a 2c 20 6b 3b 0a 20 20 53 72 63 4c 69 73 74   j, k;.  SrcList
2bfb6 20 2a 70 54 61 62 4c 69 73 74 3b 0a 20 20 45 78   *pTabList;.  Ex
2bfb7 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a  prList *pEList;.
2bfb8 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2bfb9 5f 69 74 65 6d 20 2a 70 46 72 6f 6d 3b 0a 20 20  _item *pFrom;.  
2bfba 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2bfbb 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28  arse->db;..  if(
2bfbc 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2bfbd 64 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  d  ){.    return
2bfbe 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a   WRC_Abort;.  }.
2bfbf 20 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e 70    if( NEVER(p->p
2bfc0 53 72 63 3d 3d 30 29 20 7c 7c 20 28 70 2d 3e 73  Src==0) || (p->s
2bfc1 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70  elFlags & SF_Exp
2bfc2 61 6e 64 65 64 29 21 3d 30 20 29 7b 0a 20 20 20  anded)!=0 ){.   
2bfc3 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
2bfc4 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 73 65 6c 46  e;.  }.  p->selF
2bfc5 6c 61 67 73 20 7c 3d 20 53 46 5f 45 78 70 61 6e  lags |= SF_Expan
2bfc6 64 65 64 3b 0a 20 20 70 54 61 62 4c 69 73 74 20  ded;.  pTabList 
2bfc7 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70 45 4c  = p->pSrc;.  pEL
2bfc8 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b  ist = p->pEList;
2bfc9 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
2bfca 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2bfcb 68 61 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e  have been assign
2bfcc 65 64 20 74 6f 20 61 6c 6c 20 65 6e 74 72 69 65  ed to all entrie
2bfcd 73 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 46 52  s in.  ** the FR
2bfce 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  OM clause of the
2bfcf 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2bfd0 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  t..  */.  sqlite
2bfd1 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75  3SrcListAssignCu
2bfd2 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20 70 54  rsors(pParse, pT
2bfd3 61 62 4c 69 73 74 29 3b 0a 0a 20 20 2f 2a 20 4c  abList);..  /* L
2bfd4 6f 6f 6b 20 75 70 20 65 76 65 72 79 20 74 61 62  ook up every tab
2bfd5 6c 65 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20  le named in the 
2bfd6 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
2bfd7 68 65 20 73 65 6c 65 63 74 2e 20 20 49 66 0a 20  he select.  If. 
2bfd8 20 2a 2a 20 61 6e 20 65 6e 74 72 79 20 6f 66 20   ** an entry of 
2bfd9 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2bfda 69 73 20 61 20 73 75 62 71 75 65 72 79 20 69 6e  is a subquery in
2bfdb 73 74 65 61 64 20 6f 66 20 61 20 74 61 62 6c 65  stead of a table
2bfdc 20 6f 72 20 76 69 65 77 2c 0a 20 20 2a 2a 20 74   or view,.  ** t
2bfdd 68 65 6e 20 63 72 65 61 74 65 20 61 20 74 72 61  hen create a tra
2bfde 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 73 74 72  nsient table str
2bfdf 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72 69  ucture to descri
2bfe0 62 65 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  be the subquery.
2bfe1 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c  .  */.  for(i=0,
2bfe2 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d   pFrom=pTabList-
2bfe3 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e  >a; i<pTabList->
2bfe4 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d  nSrc; i++, pFrom
2bfe5 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  ++){.    Table *
2bfe6 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 70 46  pTab;.    if( pF
2bfe7 72 6f 6d 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a  rom->pTab!=0 ){.
2bfe8 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74        /* This st
2bfe9 61 74 65 6d 65 6e 74 20 68 61 73 20 61 6c 72 65  atement has alre
2bfea 61 64 79 20 62 65 65 6e 20 70 72 65 70 61 72 65  ady been prepare
2bfeb 64 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20  d.  There is no 
2bfec 6e 65 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f  need.      ** to
2bfed 20 67 6f 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a   go further. */.
2bfee 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3d        assert( i=
2bfef 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75  =0 );.      retu
2bff0 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
2bff1 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 72 6f    }.    if( pFro
2bff2 6d 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 23  m->zName==0 ){.#
2bff3 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2bff4 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20  IT_SUBQUERY.    
2bff5 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 20 3d    Select *pSel =
2bff6 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b   pFrom->pSelect;
2bff7 0a 20 20 20 20 20 20 2f 2a 20 41 20 73 75 62 2d  .      /* A sub-
2bff8 71 75 65 72 79 20 69 6e 20 74 68 65 20 46 52 4f  query in the FRO
2bff9 4d 20 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45  M clause of a SE
2bffa 4c 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 61 73  LECT */.      as
2bffb 73 65 72 74 28 20 70 53 65 6c 21 3d 30 20 29 3b  sert( pSel!=0 );
2bffc 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2bffd 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29 3b  From->pTab==0 );
2bffe 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 61  .      sqlite3Wa
2bfff 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72  lkSelect(pWalker
2c000 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20 20 20 70  , pSel);.      p
2c001 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20 70 54 61  From->pTab = pTa
2c002 62 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  b = sqlite3DbMal
2c003 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
2c004 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 20 20  of(Table));.    
2c005 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20    if( pTab==0 ) 
2c006 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2c007 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 64 62  ;.      pTab->db
2c008 4d 65 6d 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  Mem = db->lookas
2c009 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3f 20 64  ide.bEnabled ? d
2c00a 62 20 3a 20 30 3b 0a 20 20 20 20 20 20 70 54 61  b : 0;.      pTa
2c00b 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  b->nRef = 1;.   
2c00c 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d     pTab->zName =
2c00d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2c00e 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 75 62 71  db, "sqlite_subq
2c00f 75 65 72 79 5f 25 70 5f 22 2c 20 28 76 6f 69 64  uery_%p_", (void
2c010 2a 29 70 54 61 62 29 3b 0a 20 20 20 20 20 20 77  *)pTab);.      w
2c011 68 69 6c 65 28 20 70 53 65 6c 2d 3e 70 50 72 69  hile( pSel->pPri
2c012 6f 72 20 29 7b 20 70 53 65 6c 20 3d 20 70 53 65  or ){ pSel = pSe
2c013 6c 2d 3e 70 50 72 69 6f 72 3b 20 7d 0a 20 20 20  l->pPrior; }.   
2c014 20 20 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73     selectColumns
2c015 46 72 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61  FromExprList(pPa
2c016 72 73 65 2c 20 70 53 65 6c 2d 3e 70 45 4c 69 73  rse, pSel->pELis
2c017 74 2c 20 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20  t, &pTab->nCol, 
2c018 26 70 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20  &pTab->aCol);.  
2c019 20 20 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20      pTab->iPKey 
2c01a 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 54 61 62  = -1;.      pTab
2c01b 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46  ->tabFlags |= TF
2c01c 5f 45 70 68 65 6d 65 72 61 6c 3b 0a 23 65 6e 64  _Ephemeral;.#end
2c01d 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  if.    }else{.  
2c01e 20 20 20 20 2f 2a 20 41 6e 20 6f 72 64 69 6e 61      /* An ordina
2c01f 72 79 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77  ry table or view
2c020 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 46 52 4f   name in the FRO
2c021 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  M clause */.    
2c022 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d    assert( pFrom-
2c023 3e 70 54 61 62 3d 3d 30 20 29 3b 0a 20 20 20 20  >pTab==0 );.    
2c024 20 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20    pFrom->pTab = 
2c025 70 54 61 62 20 3d 20 0a 20 20 20 20 20 20 20 20  pTab = .        
2c026 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
2c027 6c 65 28 70 50 61 72 73 65 2c 30 2c 70 46 72 6f  le(pParse,0,pFro
2c028 6d 2d 3e 7a 4e 61 6d 65 2c 70 46 72 6f 6d 2d 3e  m->zName,pFrom->
2c029 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20  zDatabase);.    
2c02a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20    if( pTab==0 ) 
2c02b 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2c02c 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52  ;.      pTab->nR
2c02d 65 66 2b 2b 3b 0a 23 69 66 20 21 64 65 66 69 6e  ef++;.#if !defin
2c02e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
2c02f 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  IEW) || !defined
2c030 20 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49   (SQLITE_OMIT_VI
2c031 52 54 55 41 4c 54 41 42 4c 45 29 0a 20 20 20 20  RTUALTABLE).    
2c032 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c    if( pTab->pSel
2c033 65 63 74 20 7c 7c 20 49 73 56 69 72 74 75 61 6c  ect || IsVirtual
2c034 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
2c035 20 20 2f 2a 20 57 65 20 72 65 61 63 68 20 68 65    /* We reach he
2c036 72 65 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20  re if the named 
2c037 74 61 62 6c 65 20 69 73 20 61 20 72 65 61 6c 6c  table is a reall
2c038 79 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 20 20  y a view */.    
2c039 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56      if( sqlite3V
2c03a 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  iewGetColumnName
2c03b 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 20  s(pParse, pTab) 
2c03c 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
2c03d 72 74 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  rt;.        asse
2c03e 72 74 28 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65  rt( pFrom->pSele
2c03f 63 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  ct==0 );.       
2c040 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 20   pFrom->pSelect 
2c041 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
2c042 75 70 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 65  up(db, pTab->pSe
2c043 6c 65 63 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  lect, 0);.      
2c044 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c    sqlite3WalkSel
2c045 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 46 72  ect(pWalker, pFr
2c046 6f 6d 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20  om->pSelect);.  
2c047 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
2c048 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 61 74   }..    /* Locat
2c049 65 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65  e the index name
2c04a 64 20 62 79 20 74 68 65 20 49 4e 44 45 58 45 44  d by the INDEXED
2c04b 20 42 59 20 63 6c 61 75 73 65 2c 20 69 66 20 61   BY clause, if a
2c04c 6e 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73  ny. */.    if( s
2c04d 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c  qlite3IndexedByL
2c04e 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 46  ookup(pParse, pF
2c04f 72 6f 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65  rom) ){.      re
2c050 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
2c051 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2c052 50 72 6f 63 65 73 73 20 4e 41 54 55 52 41 4c 20  Process NATURAL 
2c053 6b 65 79 77 6f 72 64 73 2c 20 61 6e 64 20 4f 4e  keywords, and ON
2c054 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73   and USING claus
2c055 65 73 20 6f 66 20 6a 6f 69 6e 73 2e 0a 20 20 2a  es of joins..  *
2c056 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  /.  if( db->mall
2c057 6f 63 46 61 69 6c 65 64 20 7c 7c 20 73 71 6c 69  ocFailed || sqli
2c058 74 65 50 72 6f 63 65 73 73 4a 6f 69 6e 28 70 50  teProcessJoin(pP
2c059 61 72 73 65 2c 20 70 29 20 29 7b 0a 20 20 20 20  arse, p) ){.    
2c05a 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2c05b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20  ;.  }..  /* For 
2c05c 65 76 65 72 79 20 22 2a 22 20 74 68 61 74 20 6f  every "*" that o
2c05d 63 63 75 72 73 20 69 6e 20 74 68 65 20 63 6f 6c  ccurs in the col
2c05e 75 6d 6e 20 6c 69 73 74 2c 20 69 6e 73 65 72 74  umn list, insert
2c05f 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 0a 20 20   the names of.  
2c060 2a 2a 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69  ** all columns i
2c061 6e 20 61 6c 6c 20 74 61 62 6c 65 73 2e 20 20 41  n all tables.  A
2c062 6e 64 20 66 6f 72 20 65 76 65 72 79 20 54 41 42  nd for every TAB
2c063 4c 45 2e 2a 20 69 6e 73 65 72 74 20 74 68 65 20  LE.* insert the 
2c064 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6c  names.  ** of al
2c065 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 54 41 42  l columns in TAB
2c066 4c 45 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  LE.  The parser 
2c067 69 6e 73 65 72 74 65 64 20 61 20 73 70 65 63 69  inserted a speci
2c068 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20  al expression.  
2c069 2a 2a 20 77 69 74 68 20 74 68 65 20 54 4b 5f 41  ** with the TK_A
2c06a 4c 4c 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20  LL operator for 
2c06b 65 61 63 68 20 22 2a 22 20 74 68 61 74 20 69 74  each "*" that it
2c06c 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 63 6f   found in the co
2c06d 6c 75 6d 6e 20 6c 69 73 74 2e 0a 20 20 2a 2a 20  lumn list..  ** 
2c06e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  The following co
2c06f 64 65 20 6a 75 73 74 20 68 61 73 20 74 6f 20 6c  de just has to l
2c070 6f 63 61 74 65 20 74 68 65 20 54 4b 5f 41 4c 4c  ocate the TK_ALL
2c071 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64   expressions and
2c072 20 65 78 70 61 6e 64 0a 20 20 2a 2a 20 65 61 63   expand.  ** eac
2c073 68 20 6f 6e 65 20 74 6f 20 74 68 65 20 6c 69 73  h one to the lis
2c074 74 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73  t of all columns
2c075 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65 73 2e 0a   in all tables..
2c076 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69    **.  ** The fi
2c077 72 73 74 20 6c 6f 6f 70 20 6a 75 73 74 20 63 68  rst loop just ch
2c078 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74  ecks to see if t
2c079 68 65 72 65 20 61 72 65 20 61 6e 79 20 22 2a 22  here are any "*"
2c07a 20 6f 70 65 72 61 74 6f 72 73 0a 20 20 2a 2a 20   operators.  ** 
2c07b 74 68 61 74 20 6e 65 65 64 20 65 78 70 61 6e 64  that need expand
2c07c 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  ing..  */.  for(
2c07d 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e  k=0; k<pEList->n
2c07e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20  Expr; k++){.    
2c07f 45 78 70 72 20 2a 70 45 20 3d 20 70 45 4c 69 73  Expr *pE = pELis
2c080 74 2d 3e 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20  t->a[k].pExpr;. 
2c081 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54     if( pE->op==T
2c082 4b 5f 41 4c 4c 20 29 20 62 72 65 61 6b 3b 0a 20  K_ALL ) break;. 
2c083 20 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e 6f     assert( pE->o
2c084 70 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d  p!=TK_DOT || pE-
2c085 3e 70 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20  >pRight!=0 );.  
2c086 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e 6f 70    assert( pE->op
2c087 21 3d 54 4b 5f 44 4f 54 20 7c 7c 20 28 70 45 2d  !=TK_DOT || (pE-
2c088 3e 70 4c 65 66 74 21 3d 30 20 26 26 20 70 45 2d  >pLeft!=0 && pE-
2c089 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49  >pLeft->op==TK_I
2c08a 44 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 45  D) );.    if( pE
2c08b 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20  ->op==TK_DOT && 
2c08c 70 45 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 3d 3d  pE->pRight->op==
2c08d 54 4b 5f 41 4c 4c 20 29 20 62 72 65 61 6b 3b 0a  TK_ALL ) break;.
2c08e 20 20 7d 0a 20 20 69 66 28 20 6b 3c 70 45 4c 69    }.  if( k<pELi
2c08f 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
2c090 20 2f 2a 0a 20 20 20 20 2a 2a 20 49 66 20 77 65   /*.    ** If we
2c091 20 67 65 74 20 68 65 72 65 20 69 74 20 6d 65 61   get here it mea
2c092 6e 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ns the result se
2c093 74 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f  t contains one o
2c094 72 20 6d 6f 72 65 20 22 2a 22 0a 20 20 20 20 2a  r more "*".    *
2c095 2a 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61 74  * operators that
2c096 20 6e 65 65 64 20 74 6f 20 62 65 20 65 78 70 61   need to be expa
2c097 6e 64 65 64 2e 20 20 4c 6f 6f 70 20 74 68 72 6f  nded.  Loop thro
2c098 75 67 68 20 65 61 63 68 20 65 78 70 72 65 73 73  ugh each express
2c099 69 6f 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68  ion.    ** in th
2c09a 65 20 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64  e result set and
2c09b 20 65 78 70 61 6e 64 20 74 68 65 6d 20 6f 6e 65   expand them one
2c09c 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2f 0a   by one..    */.
2c09d 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
2c09e 69 73 74 5f 69 74 65 6d 20 2a 61 20 3d 20 70 45  ist_item *a = pE
2c09f 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 45 78 70  List->a;.    Exp
2c0a0 72 4c 69 73 74 20 2a 70 4e 65 77 20 3d 20 30 3b  rList *pNew = 0;
2c0a1 0a 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d  .    int flags =
2c0a2 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61   pParse->db->fla
2c0a3 67 73 3b 0a 20 20 20 20 69 6e 74 20 6c 6f 6e 67  gs;.    int long
2c0a4 4e 61 6d 65 73 20 3d 20 28 66 6c 61 67 73 20 26  Names = (flags &
2c0a5 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e   SQLITE_FullColN
2c0a6 61 6d 65 73 29 21 3d 30 0a 20 20 20 20 20 20 20  ames)!=0.       
2c0a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
2c0a8 26 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  & (flags & SQLIT
2c0a9 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 29  E_ShortColNames)
2c0aa 3d 3d 30 3b 0a 0a 20 20 20 20 66 6f 72 28 6b 3d  ==0;..    for(k=
2c0ab 30 3b 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; k<pEList->nEx
2c0ac 70 72 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; k++){.      
2c0ad 45 78 70 72 20 2a 70 45 20 3d 20 61 5b 6b 5d 2e  Expr *pE = a[k].
2c0ae 70 45 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73  pExpr;.      ass
2c0af 65 72 74 28 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f  ert( pE->op!=TK_
2c0b0 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67 68  DOT || pE->pRigh
2c0b1 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  t!=0 );.      if
2c0b2 28 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c  ( pE->op!=TK_ALL
2c0b3 20 26 26 20 28 70 45 2d 3e 6f 70 21 3d 54 4b 5f   && (pE->op!=TK_
2c0b4 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67 68  DOT || pE->pRigh
2c0b5 74 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29 20 29  t->op!=TK_ALL) )
2c0b6 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
2c0b7 73 20 70 61 72 74 69 63 75 6c 61 72 20 65 78 70  s particular exp
2c0b8 72 65 73 73 69 6f 6e 20 64 6f 65 73 20 6e 6f 74  ression does not
2c0b9 20 6e 65 65 64 20 74 6f 20 62 65 20 65 78 70 61   need to be expa
2c0ba 6e 64 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f  nded..        */
2c0bb 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20  .        pNew = 
2c0bc 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2c0bd 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4e  ppend(pParse, pN
2c0be 65 77 2c 20 61 5b 6b 5d 2e 70 45 78 70 72 29 3b  ew, a[k].pExpr);
2c0bf 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 65  .        if( pNe
2c0c0 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  w ){.          p
2c0c1 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 45 78  New->a[pNew->nEx
2c0c2 70 72 2d 31 5d 2e 7a 4e 61 6d 65 20 3d 20 61 5b  pr-1].zName = a[
2c0c3 6b 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  k].zName;.      
2c0c4 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 77      pNew->a[pNew
2c0c5 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a 53 70 61 6e  ->nExpr-1].zSpan
2c0c6 20 3d 20 61 5b 6b 5d 2e 7a 53 70 61 6e 3b 0a 20   = a[k].zSpan;. 
2c0c7 20 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 4e           a[k].zN
2c0c8 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ame = 0;.       
2c0c9 20 20 20 61 5b 6b 5d 2e 7a 53 70 61 6e 20 3d 20     a[k].zSpan = 
2c0ca 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
2c0cb 20 20 20 20 20 61 5b 6b 5d 2e 70 45 78 70 72 20       a[k].pExpr 
2c0cc 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 0;.      }else
2c0cd 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  {.        /* Thi
2c0ce 73 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  s expression is 
2c0cf 61 20 22 2a 22 20 6f 72 20 61 20 22 54 41 42 4c  a "*" or a "TABL
2c0d0 45 2e 2a 22 20 61 6e 64 20 6e 65 65 64 73 20 74  E.*" and needs t
2c0d1 6f 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  o be.        ** 
2c0d2 65 78 70 61 6e 64 65 64 2e 20 2a 2f 0a 20 20 20  expanded. */.   
2c0d3 20 20 20 20 20 69 6e 74 20 74 61 62 6c 65 53 65       int tableSe
2c0d4 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  en = 0;      /* 
2c0d5 53 65 74 20 74 6f 20 31 20 77 68 65 6e 20 54 41  Set to 1 when TA
2c0d6 42 4c 45 20 6d 61 74 63 68 65 73 20 2a 2f 0a 20  BLE matches */. 
2c0d7 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 4e         char *zTN
2c0d8 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
2c0d9 2f 2a 20 74 65 78 74 20 6f 66 20 6e 61 6d 65 20  /* text of name 
2c0da 6f 66 20 54 41 42 4c 45 20 2a 2f 0a 20 20 20 20  of TABLE */.    
2c0db 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d      if( pE->op==
2c0dc 54 4b 5f 44 4f 54 20 29 7b 0a 20 20 20 20 20 20  TK_DOT ){.      
2c0dd 20 20 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e      assert( pE->
2c0de 70 4c 65 66 74 21 3d 30 20 29 3b 0a 20 20 20 20  pLeft!=0 );.    
2c0df 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45        assert( !E
2c0e0 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
2c0e1 45 2d 3e 70 4c 65 66 74 2c 20 45 50 5f 49 6e 74  E->pLeft, EP_Int
2c0e2 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20  Value) );.      
2c0e3 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20 70 45 2d      zTName = pE-
2c0e4 3e 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e  >pLeft->u.zToken
2c0e5 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
2c0e6 0a 20 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d  .          zTNam
2c0e7 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  e = 0;.        }
2c0e8 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
2c0e9 2c 20 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74  , pFrom=pTabList
2c0ea 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d  ->a; i<pTabList-
2c0eb 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f  >nSrc; i++, pFro
2c0ec 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  m++){.          
2c0ed 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46  Table *pTab = pF
2c0ee 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20  rom->pTab;.     
2c0ef 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e       char *zTabN
2c0f0 61 6d 65 20 3d 20 70 46 72 6f 6d 2d 3e 7a 41 6c  ame = pFrom->zAl
2c0f1 69 61 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ias;.          i
2c0f2 66 28 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 29  f( zTabName==0 )
2c0f3 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 54  {.            zT
2c0f4 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a  abName = pTab->z
2c0f5 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Name;.          
2c0f6 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  }.          if( 
2c0f7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2c0f8 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2c0f9 20 20 20 20 69 66 28 20 7a 54 4e 61 6d 65 20 26      if( zTName &
2c0fa 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
2c0fb 28 7a 54 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d  (zTName, zTabNam
2c0fc 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)!=0 ){.       
2c0fd 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2c0fe 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2c0ff 20 20 20 20 20 74 61 62 6c 65 53 65 65 6e 20 3d       tableSeen =
2c100 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   1;.          fo
2c101 72 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e  r(j=0; j<pTab->n
2c102 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  Col; j++){.     
2c103 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78         Expr *pEx
2c104 70 72 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20  pr, *pRight;.   
2c105 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a           char *z
2c106 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f  Name = pTab->aCo
2c107 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  l[j].zName;.    
2c108 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43          char *zC
2c109 6f 6c 6e 61 6d 65 3b 20 20 2f 2a 20 54 68 65 20  olname;  /* The 
2c10a 63 6f 6d 70 75 74 65 64 20 63 6f 6c 75 6d 6e 20  computed column 
2c10b 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  name */.        
2c10c 20 20 20 20 63 68 61 72 20 2a 7a 54 6f 46 72 65      char *zToFre
2c10d 65 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 65 64  e;   /* Malloced
2c10e 20 73 74 72 69 6e 67 20 74 68 61 74 20 6e 65 65   string that nee
2c10f 64 73 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a  ds to be freed *
2c110 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 54 6f  /.            To
2c111 6b 65 6e 20 73 43 6f 6c 6e 61 6d 65 3b 20 20 2f  ken sColname;  /
2c112 2a 20 43 6f 6d 70 75 74 65 64 20 63 6f 6c 75 6d  * Computed colum
2c113 6e 20 6e 61 6d 65 20 61 73 20 61 20 74 6f 6b 65  n name as a toke
2c114 6e 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20 20 20  n */..          
2c115 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e    /* If a column
2c116 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 27 68   is marked as 'h
2c117 69 64 64 65 6e 27 20 28 63 75 72 72 65 6e 74 6c  idden' (currentl
2c118 79 20 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 0a  y only possible.
2c119 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66              ** f
2c11a 6f 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  or virtual table
2c11b 73 29 2c 20 64 6f 20 6e 6f 74 20 69 6e 63 6c 75  s), do not inclu
2c11c 64 65 20 69 74 20 69 6e 20 74 68 65 20 65 78 70  de it in the exp
2c11d 61 6e 64 65 64 0a 20 20 20 20 20 20 20 20 20 20  anded.          
2c11e 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20    ** result-set 
2c11f 6c 69 73 74 2e 0a 20 20 20 20 20 20 20 20 20 20  list..          
2c120 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20    */.           
2c121 20 69 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c   if( IsHiddenCol
2c122 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b  umn(&pTab->aCol[
2c123 6a 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  j]) ){.         
2c124 20 20 20 20 20 61 73 73 65 72 74 28 49 73 56 69       assert(IsVi
2c125 72 74 75 61 6c 28 70 54 61 62 29 29 3b 0a 20 20  rtual(pTab));.  
2c126 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74              cont
2c127 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20  inue;.          
2c128 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 20    }..           
2c129 20 69 66 28 20 69 3e 30 20 26 26 20 7a 54 4e 61   if( i>0 && zTNa
2c12a 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  me==0 ){.       
2c12b 20 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72         struct Sr
2c12c 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 65 66  cList_item *pLef
2c12d 74 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61  t = &pTabList->a
2c12e 5b 69 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20 20  [i-1];.         
2c12f 20 20 20 20 20 69 66 28 20 28 70 4c 65 66 74 5b       if( (pLeft[
2c130 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54  1].jointype & JT
2c131 5f 4e 41 54 55 52 41 4c 29 21 3d 30 20 26 26 0a  _NATURAL)!=0 &&.
2c132 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c133 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 49 6e          columnIn
2c134 64 65 78 28 70 4c 65 66 74 2d 3e 70 54 61 62 2c  dex(pLeft->pTab,
2c135 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a 20 20   zName)>=0 ){.  
2c136 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2c137 20 49 6e 20 61 20 4e 41 54 55 52 41 4c 20 6a 6f   In a NATURAL jo
2c138 69 6e 2c 20 6f 6d 69 74 20 74 68 65 20 6a 6f 69  in, omit the joi
2c139 6e 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 74  n columns from t
2c13a 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  he .            
2c13b 20 20 20 20 2a 2a 20 74 61 62 6c 65 20 6f 6e 20      ** table on 
2c13c 74 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20  the right */.   
2c13d 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
2c13e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20  tinue;.         
2c13f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2c140 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2c141 49 64 4c 69 73 74 49 6e 64 65 78 28 70 4c 65 66  IdListIndex(pLef
2c142 74 5b 31 5d 2e 70 55 73 69 6e 67 2c 20 7a 4e 61  t[1].pUsing, zNa
2c143 6d 65 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)>=0 ){.      
2c144 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20            /* In 
2c145 61 20 6a 6f 69 6e 20 77 69 74 68 20 61 20 55 53  a join with a US
2c146 49 4e 47 20 63 6c 61 75 73 65 2c 20 6f 6d 69 74  ING clause, omit
2c147 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a   columns in the.
2c148 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c149 2a 2a 20 75 73 69 6e 67 20 63 6c 61 75 73 65 20  ** using clause 
2c14a 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 6f  from the table o
2c14b 6e 20 74 68 65 20 72 69 67 68 74 2e 20 2a 2f 0a  n the right. */.
2c14c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c14d 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
2c14e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2c14f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c150 20 20 20 20 70 52 69 67 68 74 20 3d 20 73 71 6c      pRight = sql
2c151 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
2c152 49 44 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ID, zName);.    
2c153 20 20 20 20 20 20 20 20 7a 43 6f 6c 6e 61 6d 65          zColname
2c154 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20   = zName;.      
2c155 20 20 20 20 20 20 7a 54 6f 46 72 65 65 20 3d 20        zToFree = 
2c156 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  0;.            i
2c157 66 28 20 6c 6f 6e 67 4e 61 6d 65 73 20 7c 7c 20  f( longNames || 
2c158 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 31  pTabList->nSrc>1
2c159 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2c15a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 0a 20    Expr *pLeft;. 
2c15b 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 65               pLe
2c15c 66 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ft = sqlite3Expr
2c15d 28 64 62 2c 20 54 4b 5f 49 44 2c 20 7a 54 61 62  (db, TK_ID, zTab
2c15e 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Name);.         
2c15f 20 20 20 20 20 70 45 78 70 72 20 3d 20 73 71 6c       pExpr = sql
2c160 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2c161 2c 20 54 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 2c  , TK_DOT, pLeft,
2c162 20 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20   pRight, 0);.   
2c163 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6c             if( l
2c164 6f 6e 67 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20  ongNames ){.    
2c165 20 20 20 20 20 20 20 20 20 20 20 20 7a 43 6f 6c              zCol
2c166 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  name = sqlite3MP
2c167 72 69 6e 74 66 28 64 62 2c 20 22 25 73 2e 25 73  rintf(db, "%s.%s
2c168 22 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 7a 4e 61  ", zTabName, zNa
2c169 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  me);.           
2c16a 20 20 20 20 20 7a 54 6f 46 72 65 65 20 3d 20 7a       zToFree = z
2c16b 43 6f 6c 6e 61 6d 65 3b 0a 20 20 20 20 20 20 20  Colname;.       
2c16c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2c16d 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2c16e 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 20            pExpr 
2c16f 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  = pRight;.      
2c170 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c171 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
2c172 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
2c173 28 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 70  (pParse, pNew, p
2c174 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20 20  Expr);.         
2c175 20 20 20 73 43 6f 6c 6e 61 6d 65 2e 7a 20 3d 20     sColname.z = 
2c176 7a 43 6f 6c 6e 61 6d 65 3b 0a 20 20 20 20 20 20  zColname;.      
2c177 20 20 20 20 20 20 73 43 6f 6c 6e 61 6d 65 2e 6e        sColname.n
2c178 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2c179 33 30 28 7a 43 6f 6c 6e 61 6d 65 29 3b 0a 20 20  30(zColname);.  
2c17a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2c17b 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65  3ExprListSetName
2c17c 28 70 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 26  (pParse, pNew, &
2c17d 73 43 6f 6c 6e 61 6d 65 2c 20 30 29 3b 0a 20 20  sColname, 0);.  
2c17e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2c17f 33 44 62 46 72 65 65 28 64 62 2c 20 7a 54 6f 46  3DbFree(db, zToF
2c180 72 65 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ree);.          
2c181 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
2c182 20 20 20 20 69 66 28 20 21 74 61 62 6c 65 53 65      if( !tableSe
2c183 65 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  en ){.          
2c184 69 66 28 20 7a 54 4e 61 6d 65 20 29 7b 0a 20 20  if( zTName ){.  
2c185 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2c186 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2c187 2c 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65  , "no such table
2c188 3a 20 25 73 22 2c 20 7a 54 4e 61 6d 65 29 3b 0a  : %s", zTName);.
2c189 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
2c18a 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2c18b 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2c18c 72 73 65 2c 20 22 6e 6f 20 74 61 62 6c 65 73 20  rse, "no tables 
2c18d 73 70 65 63 69 66 69 65 64 22 29 3b 0a 20 20 20  specified");.   
2c18e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2c18f 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
2c190 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2c191 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2c192 45 4c 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70  EList);.    p->p
2c193 45 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20  EList = pNew;.  
2c194 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  }.#if SQLITE_MAX
2c195 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d  _COLUMN.  if( p-
2c196 3e 70 45 4c 69 73 74 20 26 26 20 70 2d 3e 70 45  >pEList && p->pE
2c197 4c 69 73 74 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e  List->nExpr>db->
2c198 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
2c199 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20  MIT_COLUMN] ){. 
2c19a 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2c19b 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20  sg(pParse, "too 
2c19c 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  many columns in 
2c19d 72 65 73 75 6c 74 20 73 65 74 22 29 3b 0a 20 20  result set");.  
2c19e 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  }.#endif.  retur
2c19f 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  n WRC_Continue;.
2c1a0 7d 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 72  }../*.** No-op r
2c1a1 6f 75 74 69 6e 65 20 66 6f 72 20 74 68 65 20 70  outine for the p
2c1a2 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b 65 72  arse-tree walker
2c1a3 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69  ..**.** When thi
2c1a4 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65  s routine is the
2c1a5 20 57 61 6c 6b 65 72 2e 78 45 78 70 72 43 61 6c   Walker.xExprCal
2c1a6 6c 62 61 63 6b 20 74 68 65 6e 20 65 78 70 72 65  lback then expre
2c1a7 73 73 69 6f 6e 20 74 72 65 65 73 0a 2a 2a 20 61  ssion trees.** a
2c1a8 72 65 20 77 61 6c 6b 65 64 20 77 69 74 68 6f 75  re walked withou
2c1a9 74 20 61 6e 79 20 61 63 74 69 6f 6e 73 20 62 65  t any actions be
2c1aa 69 6e 67 20 74 61 6b 65 6e 20 61 74 20 65 61 63  ing taken at eac
2c1ab 68 20 6e 6f 64 65 2e 20 20 50 72 65 73 75 6d 61  h node.  Presuma
2c1ac 62 6c 79 2c 0a 2a 2a 20 77 68 65 6e 20 74 68 69  bly,.** when thi
2c1ad 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
2c1ae 64 20 66 6f 72 20 57 61 6c 6b 65 72 2e 78 45 78  d for Walker.xEx
2c1af 70 72 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e 20  prCallback then 
2c1b0 0a 2a 2a 20 57 61 6c 6b 65 72 2e 78 53 65 6c 65  .** Walker.xSele
2c1b1 63 74 43 61 6c 6c 62 61 63 6b 20 69 73 20 73 65  ctCallback is se
2c1b2 74 20 74 6f 20 64 6f 20 73 6f 6d 65 74 68 69 6e  t to do somethin
2c1b3 67 20 75 73 65 66 75 6c 20 66 6f 72 20 65 76 65  g useful for eve
2c1b4 72 79 20 0a 2a 2a 20 73 75 62 71 75 65 72 79 20  ry .** subquery 
2c1b5 69 6e 20 74 68 65 20 70 61 72 73 65 72 20 74 72  in the parser tr
2c1b6 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ee..*/.static in
2c1b7 74 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 28 57  t exprWalkNoop(W
2c1b8 61 6c 6b 65 72 20 2a 4e 6f 74 55 73 65 64 2c 20  alker *NotUsed, 
2c1b9 45 78 70 72 20 2a 4e 6f 74 55 73 65 64 32 29 7b  Expr *NotUsed2){
2c1ba 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
2c1bb 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
2c1bc 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72  tUsed2);.  retur
2c1bd 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  n WRC_Continue;.
2c1be 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2c1bf 75 74 69 6e 65 20 22 65 78 70 61 6e 64 73 22 20  utine "expands" 
2c1c0 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
2c1c1 6e 74 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74  nt and all of it
2c1c2 73 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a  s subqueries..**
2c1c3 20 46 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   For additional 
2c1c4 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 77  information on w
2c1c5 68 61 74 20 69 74 20 6d 65 61 6e 73 20 74 6f 20  hat it means to 
2c1c6 22 65 78 70 61 6e 64 22 20 61 20 53 45 4c 45 43  "expand" a SELEC
2c1c7 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2c 20  T.** statement, 
2c1c8 73 65 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20  see the comment 
2c1c9 6f 6e 20 74 68 65 20 73 65 6c 65 63 74 45 78 70  on the selectExp
2c1ca 61 6e 64 20 77 6f 72 6b 65 72 20 63 61 6c 6c 62  and worker callb
2c1cb 61 63 6b 20 61 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a  ack above..**.**
2c1cc 20 45 78 70 61 6e 64 69 6e 67 20 61 20 53 45 4c   Expanding a SEL
2c1cd 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ECT statement is
2c1ce 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70 20   the first step 
2c1cf 69 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61 0a  in processing a.
2c1d0 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ** SELECT statem
2c1d1 65 6e 74 2e 20 20 54 68 65 20 53 45 4c 45 43 54  ent.  The SELECT
2c1d2 20 73 74 61 74 65 6d 65 6e 74 20 6d 75 73 74 20   statement must 
2c1d3 62 65 20 65 78 70 61 6e 64 65 64 20 62 65 66 6f  be expanded befo
2c1d4 72 65 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c  re.** name resol
2c1d5 75 74 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d  ution is perform
2c1d6 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79  ed..**.** If any
2c1d7 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
2c1d8 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  , an error messa
2c1d9 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  ge is written in
2c1da 74 6f 20 70 50 61 72 73 65 2e 0a 2a 2a 20 54 68  to pParse..** Th
2c1db 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
2c1dc 6f 6e 20 63 61 6e 20 64 65 74 65 63 74 20 74 68  on can detect th
2c1dd 65 20 70 72 6f 62 6c 65 6d 20 62 79 20 6c 6f 6f  e problem by loo
2c1de 6b 69 6e 67 20 61 74 20 70 50 61 72 73 65 2d 3e  king at pParse->
2c1df 6e 45 72 72 0a 2a 2a 20 61 6e 64 2f 6f 72 20 70  nErr.** and/or p
2c1e0 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
2c1e1 63 46 61 69 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74  cFailed..*/.stat
2c1e2 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  ic void sqlite3S
2c1e3 65 6c 65 63 74 45 78 70 61 6e 64 28 50 61 72 73  electExpand(Pars
2c1e4 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63  e *pParse, Selec
2c1e5 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 57  t *pSelect){.  W
2c1e6 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 53 65  alker w;.  w.xSe
2c1e7 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73  lectCallback = s
2c1e8 65 6c 65 63 74 45 78 70 61 6e 64 65 72 3b 0a 20  electExpander;. 
2c1e9 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   w.xExprCallback
2c1ea 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b   = exprWalkNoop;
2c1eb 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50  .  w.pParse = pP
2c1ec 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57  arse;.  sqlite3W
2c1ed 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 53  alkSelect(&w, pS
2c1ee 65 6c 65 63 74 29 3b 0a 7d 0a 0a 0a 23 69 66 6e  elect);.}...#ifn
2c1ef 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2c1f0 53 55 42 51 55 45 52 59 0a 2f 2a 0a 2a 2a 20 54  SUBQUERY./*.** T
2c1f1 68 69 73 20 69 73 20 61 20 57 61 6c 6b 65 72 2e  his is a Walker.
2c1f2 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
2c1f3 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 65  callback for the
2c1f4 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 54 79   sqlite3SelectTy
2c1f5 70 65 49 6e 66 6f 28 29 0a 2a 2a 20 69 6e 74 65  peInfo().** inte
2c1f6 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  rface..**.** For
2c1f7 20 65 61 63 68 20 46 52 4f 4d 2d 63 6c 61 75 73   each FROM-claus
2c1f8 65 20 73 75 62 71 75 65 72 79 2c 20 61 64 64 20  e subquery, add 
2c1f9 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65 20 61 6e 64  Column.zType and
2c1fa 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 0a 2a 2a   Column.zColl.**
2c1fb 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20   information to 
2c1fc 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
2c1fd 75 72 65 20 74 68 61 74 20 72 65 70 72 65 73 65  ure that represe
2c1fe 6e 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 73  nts the result s
2c1ff 65 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20 73 75  et.** of that su
2c200 62 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  bquery..**.** Th
2c201 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
2c202 65 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74  e that represent
2c203 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  s the result set
2c204 20 77 61 73 20 63 6f 6e 73 74 72 75 63 74 65 64   was constructed
2c205 0a 2a 2a 20 62 79 20 73 65 6c 65 63 74 45 78 70  .** by selectExp
2c206 61 6e 64 65 72 28 29 20 62 75 74 20 74 68 65 20  ander() but the 
2c207 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69  type and collati
2c208 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77  on information w
2c209 61 73 20 6f 6d 69 74 74 65 64 0a 2a 2a 20 61 74  as omitted.** at
2c20a 20 74 68 61 74 20 70 6f 69 6e 74 20 62 65 63 61   that point beca
2c20b 75 73 65 20 69 64 65 6e 74 69 66 69 65 72 73 20  use identifiers 
2c20c 68 61 64 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  had not yet been
2c20d 20 72 65 73 6f 6c 76 65 64 2e 20 20 54 68 69 73   resolved.  This
2c20e 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63  .** routine is c
2c20f 61 6c 6c 65 64 20 61 66 74 65 72 20 69 64 65 6e  alled after iden
2c210 74 69 66 69 65 72 20 72 65 73 6f 6c 75 74 69 6f  tifier resolutio
2c211 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  n..*/.static int
2c212 20 73 65 6c 65 63 74 41 64 64 53 75 62 71 75 65   selectAddSubque
2c213 72 79 54 79 70 65 49 6e 66 6f 28 57 61 6c 6b 65  ryTypeInfo(Walke
2c214 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
2c215 63 74 20 2a 70 29 7b 0a 20 20 50 61 72 73 65 20  ct *p){.  Parse 
2c216 2a 70 50 61 72 73 65 3b 0a 20 20 69 6e 74 20 69  *pParse;.  int i
2c217 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61  ;.  SrcList *pTa
2c218 62 4c 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20  bList;.  struct 
2c219 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46  SrcList_item *pF
2c21a 72 6f 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  rom;..  assert( 
2c21b 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
2c21c 5f 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 61  _Resolved );.  a
2c21d 73 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c  ssert( (p->selFl
2c21e 61 67 73 20 26 20 53 46 5f 48 61 73 54 79 70 65  ags & SF_HasType
2c21f 49 6e 66 6f 29 3d 3d 30 20 29 3b 0a 20 20 70 2d  Info)==0 );.  p-
2c220 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f  >selFlags |= SF_
2c221 48 61 73 54 79 70 65 49 6e 66 6f 3b 0a 20 20 70  HasTypeInfo;.  p
2c222 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d  Parse = pWalker-
2c223 3e 70 50 61 72 73 65 3b 0a 20 20 70 54 61 62 4c  >pParse;.  pTabL
2c224 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  ist = p->pSrc;. 
2c225 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d   for(i=0, pFrom=
2c226 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  pTabList->a; i<p
2c227 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
2c228 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20  ++, pFrom++){.  
2c229 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2c22a 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20  pFrom->pTab;.   
2c22b 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61 62   if( ALWAYS(pTab
2c22c 21 3d 30 29 20 26 26 20 28 70 54 61 62 2d 3e 74  !=0) && (pTab->t
2c22d 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68  abFlags & TF_Eph
2c22e 65 6d 65 72 61 6c 29 21 3d 30 20 29 7b 0a 20 20  emeral)!=0 ){.  
2c22f 20 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75 65      /* A sub-que
2c230 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ry in the FROM c
2c231 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
2c232 54 20 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63  T */.      Selec
2c233 74 20 2a 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d  t *pSel = pFrom-
2c234 3e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20  >pSelect;.      
2c235 61 73 73 65 72 74 28 20 70 53 65 6c 20 29 3b 0a  assert( pSel );.
2c236 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 65        while( pSe
2c237 6c 2d 3e 70 50 72 69 6f 72 20 29 20 70 53 65 6c  l->pPrior ) pSel
2c238 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 3b   = pSel->pPrior;
2c239 0a 20 20 20 20 20 20 73 65 6c 65 63 74 41 64 64  .      selectAdd
2c23a 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c  ColumnTypeAndCol
2c23b 6c 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  lation(pParse, p
2c23c 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54 61 62 2d  Tab->nCol, pTab-
2c23d 3e 61 43 6f 6c 2c 20 70 53 65 6c 29 3b 0a 20 20  >aCol, pSel);.  
2c23e 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2c23f 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
2c240 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
2c241 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64  This routine add
2c242 73 20 64 61 74 61 74 79 70 65 20 61 6e 64 20 63  s datatype and c
2c243 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2c244 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  e information to
2c245 0a 2a 2a 20 74 68 65 20 54 61 62 6c 65 20 73 74  .** the Table st
2c246 72 75 63 74 75 72 65 73 20 6f 66 20 61 6c 6c 20  ructures of all 
2c247 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71  FROM-clause subq
2c248 75 65 72 69 65 73 20 69 6e 20 61 0a 2a 2a 20 53  ueries in a.** S
2c249 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
2c24a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 69 73 20  .**.** Use this 
2c24b 72 6f 75 74 69 6e 65 20 61 66 74 65 72 20 6e 61  routine after na
2c24c 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a  me resolution..*
2c24d 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
2c24e 6c 69 74 65 33 53 65 6c 65 63 74 41 64 64 54 79  lite3SelectAddTy
2c24f 70 65 49 6e 66 6f 28 50 61 72 73 65 20 2a 70 50  peInfo(Parse *pP
2c250 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53  arse, Select *pS
2c251 65 6c 65 63 74 29 7b 0a 23 69 66 6e 64 65 66 20  elect){.#ifndef 
2c252 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2c253 55 45 52 59 0a 20 20 57 61 6c 6b 65 72 20 77 3b  UERY.  Walker w;
2c254 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
2c255 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 41 64 64  back = selectAdd
2c256 53 75 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f  SubqueryTypeInfo
2c257 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
2c258 61 63 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f  ack = exprWalkNo
2c259 6f 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d  op;.  w.pParse =
2c25a 20 70 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74   pParse;.  sqlit
2c25b 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c  e3WalkSelect(&w,
2c25c 20 70 53 65 6c 65 63 74 29 3b 0a 23 65 6e 64 69   pSelect);.#endi
2c25d 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  f.}.../*.** This
2c25e 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 6f 66   routine sets of
2c25f 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2c260 65 6e 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69  ent for processi
2c261 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c  ng.  The.** foll
2c262 6f 77 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c  owing is accompl
2c263 69 73 68 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ished:.**.**    
2c264 20 2a 20 20 56 44 42 45 20 43 75 72 73 6f 72 20   *  VDBE Cursor 
2c265 6e 75 6d 62 65 72 73 20 61 72 65 20 61 73 73 69  numbers are assi
2c266 67 6e 65 64 20 74 6f 20 61 6c 6c 20 46 52 4f 4d  gned to all FROM
2c267 2d 63 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a  -clause terms..*
2c268 2a 20 20 20 20 20 2a 20 20 45 70 68 65 6d 65 72  *     *  Ephemer
2c269 61 6c 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 73  al Table objects
2c26a 20 61 72 65 20 63 72 65 61 74 65 64 20 66 6f 72   are created for
2c26b 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65   all FROM-clause
2c26c 20 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20   subqueries..** 
2c26d 20 20 20 20 2a 20 20 4f 4e 20 61 6e 64 20 55 53      *  ON and US
2c26e 49 4e 47 20 63 6c 61 75 73 65 73 20 61 72 65 20  ING clauses are 
2c26f 73 68 69 66 74 65 64 20 69 6e 74 6f 20 57 48 45  shifted into WHE
2c270 52 45 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a  RE statements.**
2c271 20 20 20 20 20 2a 20 20 57 69 6c 64 63 61 72 64       *  Wildcard
2c272 73 20 22 2a 22 20 61 6e 64 20 22 54 41 42 4c 45  s "*" and "TABLE
2c273 2e 2a 22 20 69 6e 20 72 65 73 75 6c 74 20 73 65  .*" in result se
2c274 74 73 20 61 72 65 20 65 78 70 61 6e 64 65 64 2e  ts are expanded.
2c275 0a 2a 2a 20 20 20 20 20 2a 20 20 49 64 65 6e 74  .**     *  Ident
2c276 69 66 69 65 72 73 20 69 6e 20 65 78 70 72 65 73  ifiers in expres
2c277 73 69 6f 6e 20 61 72 65 20 6d 61 74 63 68 65 64  sion are matched
2c278 20 74 6f 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a   to tables..**.*
2c279 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
2c27a 63 74 73 20 72 65 63 75 72 73 69 76 65 6c 79 20  cts recursively 
2c27b 6f 6e 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65  on all subquerie
2c27c 73 20 77 69 74 68 69 6e 20 74 68 65 20 53 45 4c  s within the SEL
2c27d 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ECT..*/.SQLITE_P
2c27e 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2c27f 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 0a 20  te3SelectPrep(. 
2c280 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2c281 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2c282 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2c283 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
2c284 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2c285 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2c286 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a  t being coded. *
2c287 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  /.  NameContext 
2c288 2a 70 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61  *pOuterNC  /* Na
2c289 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 63  me context for c
2c28a 6f 6e 74 61 69 6e 65 72 20 2a 2f 0a 29 7b 0a 20  ontainer */.){. 
2c28b 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20   sqlite3 *db;.  
2c28c 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20  if( NEVER(p==0) 
2c28d 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d  ) return;.  db =
2c28e 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
2c28f 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  f( p->selFlags &
2c290 20 53 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 20   SF_HasTypeInfo 
2c291 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
2c292 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28  te3SelectExpand(
2c293 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 69 66  pParse, p);.  if
2c294 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
2c295 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
2c296 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  ed ) return;.  s
2c297 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c  qlite3ResolveSel
2c298 65 63 74 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  ectNames(pParse,
2c299 20 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20   p, pOuterNC);. 
2c29a 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72   if( pParse->nEr
2c29b 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  r || db->mallocF
2c29c 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
2c29d 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 41    sqlite3SelectA
2c29e 64 64 54 79 70 65 49 6e 66 6f 28 70 50 61 72 73  ddTypeInfo(pPars
2c29f 65 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e, p);.}../*.** 
2c2a0 52 65 73 65 74 20 74 68 65 20 61 67 67 72 65 67  Reset the aggreg
2c2a1 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e  ate accumulator.
2c2a2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 67 67 72 65  .**.** The aggre
2c2a3 67 61 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72  gate accumulator
2c2a4 20 69 73 20 61 20 73 65 74 20 6f 66 20 6d 65 6d   is a set of mem
2c2a5 6f 72 79 20 63 65 6c 6c 73 20 74 68 61 74 20 68  ory cells that h
2c2a6 6f 6c 64 0a 2a 2a 20 69 6e 74 65 72 6d 65 64 69  old.** intermedi
2c2a7 61 74 65 20 72 65 73 75 6c 74 73 20 77 68 69 6c  ate results whil
2c2a8 65 20 63 61 6c 63 75 6c 61 74 69 6e 67 20 61 6e  e calculating an
2c2a9 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 69   aggregate.  Thi
2c2aa 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 73 69 6d  s.** routine sim
2c2ab 70 6c 79 20 73 74 6f 72 65 73 20 4e 55 4c 4c 73  ply stores NULLs
2c2ac 20 69 6e 20 61 6c 6c 20 6f 66 20 74 68 6f 73 65   in all of those
2c2ad 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2e 0a 2a   memory cells..*
2c2ae 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
2c2af 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 50  setAccumulator(P
2c2b0 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 41 67  arse *pParse, Ag
2c2b1 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29  gInfo *pAggInfo)
2c2b2 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
2c2b3 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
2c2b4 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 41  nt i;.  struct A
2c2b5 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 75  ggInfo_func *pFu
2c2b6 6e 63 3b 0a 20 20 69 66 28 20 70 41 67 67 49 6e  nc;.  if( pAggIn
2c2b7 66 6f 2d 3e 6e 46 75 6e 63 2b 70 41 67 67 49 6e  fo->nFunc+pAggIn
2c2b8 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 29  fo->nColumn==0 )
2c2b9 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
2c2ba 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  }.  for(i=0; i<p
2c2bb 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e  AggInfo->nColumn
2c2bc 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
2c2bd 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c2be 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 70 41 67   OP_Null, 0, pAg
2c2bf 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 69 5d 2e 69  gInfo->aCol[i].i
2c2c0 4d 65 6d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  Mem);.  }.  for(
2c2c1 70 46 75 6e 63 3d 70 41 67 67 49 6e 66 6f 2d 3e  pFunc=pAggInfo->
2c2c2 61 46 75 6e 63 2c 20 69 3d 30 3b 20 69 3c 70 41  aFunc, i=0; i<pA
2c2c3 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69  ggInfo->nFunc; i
2c2c4 2b 2b 2c 20 70 46 75 6e 63 2b 2b 29 7b 0a 20 20  ++, pFunc++){.  
2c2c5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c2c6 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
2c2c7 30 2c 20 70 46 75 6e 63 2d 3e 69 4d 65 6d 29 3b  0, pFunc->iMem);
2c2c8 0a 20 20 20 20 69 66 28 20 70 46 75 6e 63 2d 3e  .    if( pFunc->
2c2c9 69 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a  iDistinct>=0 ){.
2c2ca 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d        Expr *pE =
2c2cb 20 70 46 75 6e 63 2d 3e 70 45 78 70 72 3b 0a 20   pFunc->pExpr;. 
2c2cc 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
2c2cd 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2c2ce 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2c2cf 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d  );.      if( pE-
2c2d0 3e 78 2e 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 70  >x.pList==0 || p
2c2d1 45 2d 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70  E->x.pList->nExp
2c2d2 72 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  r!=1 ){.        
2c2d3 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2c2d4 70 50 61 72 73 65 2c 20 22 44 49 53 54 49 4e 43  pParse, "DISTINC
2c2d5 54 20 61 67 67 72 65 67 61 74 65 73 20 6d 75 73  T aggregates mus
2c2d6 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f  t have exactly o
2c2d7 6e 65 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  ne ".           
2c2d8 22 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 20  "argument");.   
2c2d9 20 20 20 20 20 70 46 75 6e 63 2d 3e 69 44 69 73       pFunc->iDis
2c2da 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20  tinct = -1;.    
2c2db 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2c2dc 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
2c2dd 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d  fo = keyInfoFrom
2c2de 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
2c2df 20 70 45 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20   pE->x.pList);. 
2c2e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2c2e1 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
2c2e2 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 46  penEphemeral, pF
2c2e3 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 2c 20  unc->iDistinct, 
2c2e4 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  0, 0,.          
2c2e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2e6 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
2c2e7 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
2c2e8 4f 46 46 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OFF);.      }.  
2c2e9 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
2c2ea 20 49 6e 76 6f 6b 65 20 74 68 65 20 4f 50 5f 41   Invoke the OP_A
2c2eb 67 67 46 69 6e 61 6c 69 7a 65 20 6f 70 63 6f 64  ggFinalize opcod
2c2ec 65 20 66 6f 72 20 65 76 65 72 79 20 61 67 67 72  e for every aggr
2c2ed 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a  egate function.*
2c2ee 2a 20 69 6e 20 74 68 65 20 41 67 67 49 6e 66 6f  * in the AggInfo
2c2ef 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
2c2f0 74 61 74 69 63 20 76 6f 69 64 20 66 69 6e 61 6c  tatic void final
2c2f1 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28  izeAggFunctions(
2c2f2 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 41  Parse *pParse, A
2c2f3 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
2c2f4 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
2c2f5 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
2c2f6 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
2c2f7 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46  AggInfo_func *pF
2c2f8 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d  ;.  for(i=0, pF=
2c2f9 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b  pAggInfo->aFunc;
2c2fa 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75   i<pAggInfo->nFu
2c2fb 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a  nc; i++, pF++){.
2c2fc 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
2c2fd 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d  ist = pF->pExpr-
2c2fe 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 61 73  >x.pList;.    as
2c2ff 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72  sert( !ExprHasPr
2c300 6f 70 65 72 74 79 28 70 46 2d 3e 70 45 78 70 72  operty(pF->pExpr
2c301 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2c302 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2c303 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 41  beAddOp4(v, OP_A
2c304 67 67 46 69 6e 61 6c 2c 20 70 46 2d 3e 69 4d 65  ggFinal, pF->iMe
2c305 6d 2c 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74  m, pList ? pList
2c306 2d 3e 6e 45 78 70 72 20 3a 20 30 2c 20 30 2c 0a  ->nExpr : 0, 0,.
2c307 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c308 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 46 2d        (void*)pF-
2c309 3e 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44  >pFunc, P4_FUNCD
2c30a 45 46 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  EF);.  }.}../*.*
2c30b 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 63 63  * Update the acc
2c30c 75 6d 75 6c 61 74 6f 72 20 6d 65 6d 6f 72 79 20  umulator memory 
2c30d 63 65 6c 6c 73 20 66 6f 72 20 61 6e 20 61 67 67  cells for an agg
2c30e 72 65 67 61 74 65 20 62 61 73 65 64 20 6f 6e 0a  regate based on.
2c30f 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  ** the current c
2c310 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a  ursor position..
2c311 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
2c312 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72  pdateAccumulator
2c313 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2c314 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66  AggInfo *pAggInf
2c315 6f 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  o){.  Vdbe *v = 
2c316 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2c317 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
2c318 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70   AggInfo_func *p
2c319 46 3b 0a 20 20 73 74 72 75 63 74 20 41 67 67 49  F;.  struct AggI
2c31a 6e 66 6f 5f 63 6f 6c 20 2a 70 43 3b 0a 0a 20 20  nfo_col *pC;..  
2c31b 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74  pAggInfo->direct
2c31c 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 73 71 6c 69  Mode = 1;.  sqli
2c31d 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61  te3ExprCacheClea
2c31e 72 28 70 50 61 72 73 65 29 3b 0a 20 20 66 6f 72  r(pParse);.  for
2c31f 28 69 3d 30 2c 20 70 46 3d 70 41 67 67 49 6e 66  (i=0, pF=pAggInf
2c320 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c 70 41 67 67  o->aFunc; i<pAgg
2c321 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b  Info->nFunc; i++
2c322 2c 20 70 46 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  , pF++){.    int
2c323 20 6e 41 72 67 3b 0a 20 20 20 20 69 6e 74 20 61   nArg;.    int a
2c324 64 64 72 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20  ddrNext = 0;.   
2c325 20 69 6e 74 20 72 65 67 41 67 67 3b 0a 20 20 20   int regAgg;.   
2c326 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2c327 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d 3e 78 2e   = pF->pExpr->x.
2c328 70 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72  pList;.    asser
2c329 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
2c32a 72 74 79 28 70 46 2d 3e 70 45 78 70 72 2c 20 45  rty(pF->pExpr, E
2c32b 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
2c32c 20 20 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b      if( pList ){
2c32d 0a 20 20 20 20 20 20 6e 41 72 67 20 3d 20 70 4c  .      nArg = pL
2c32e 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20  ist->nExpr;.    
2c32f 20 20 72 65 67 41 67 67 20 3d 20 73 71 6c 69 74    regAgg = sqlit
2c330 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
2c331 50 61 72 73 65 2c 20 6e 41 72 67 29 3b 0a 20 20  Parse, nArg);.  
2c332 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2c333 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72  odeExprList(pPar
2c334 73 65 2c 20 70 4c 69 73 74 2c 20 72 65 67 41 67  se, pList, regAg
2c335 67 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  g, 0);.    }else
2c336 7b 0a 20 20 20 20 20 20 6e 41 72 67 20 3d 20 30  {.      nArg = 0
2c337 3b 0a 20 20 20 20 20 20 72 65 67 41 67 67 20 3d  ;.      regAgg =
2c338 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   0;.    }.    if
2c339 28 20 70 46 2d 3e 69 44 69 73 74 69 6e 63 74 3e  ( pF->iDistinct>
2c33a 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 64 64 72  =0 ){.      addr
2c33b 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64  Next = sqlite3Vd
2c33c 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2c33d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 41        assert( nA
2c33e 72 67 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 63  rg==1 );.      c
2c33f 6f 64 65 44 69 73 74 69 6e 63 74 28 70 50 61 72  odeDistinct(pPar
2c340 73 65 2c 20 70 46 2d 3e 69 44 69 73 74 69 6e 63  se, pF->iDistinc
2c341 74 2c 20 61 64 64 72 4e 65 78 74 2c 20 31 2c 20  t, addrNext, 1, 
2c342 72 65 67 41 67 67 29 3b 0a 20 20 20 20 7d 0a 20  regAgg);.    }. 
2c343 20 20 20 69 66 28 20 70 46 2d 3e 70 46 75 6e 63     if( pF->pFunc
2c344 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2c345 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29  _FUNC_NEEDCOLL )
2c346 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  {.      CollSeq 
2c347 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20  *pColl = 0;.    
2c348 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
2c349 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
2c34a 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
2c34b 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21    assert( pList!
2c34c 3d 30 20 29 3b 20 20 2f 2a 20 70 4c 69 73 74 21  =0 );  /* pList!
2c34d 3d 30 20 69 66 20 70 46 2d 3e 70 46 75 6e 63 20  =0 if pF->pFunc 
2c34e 68 61 73 20 4e 45 45 44 43 4f 4c 4c 20 2a 2f 0a  has NEEDCOLL */.
2c34f 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70        for(j=0, p
2c350 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 21  Item=pList->a; !
2c351 70 43 6f 6c 6c 20 26 26 20 6a 3c 6e 41 72 67 3b  pColl && j<nArg;
2c352 20 6a 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   j++, pItem++){.
2c353 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20          pColl = 
2c354 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
2c355 65 71 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d  eq(pParse, pItem
2c356 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  ->pExpr);.      
2c357 7d 0a 20 20 20 20 20 20 69 66 28 20 21 70 43 6f  }.      if( !pCo
2c358 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  ll ){.        pC
2c359 6f 6c 6c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  oll = pParse->db
2c35a 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20  ->pDfltColl;.   
2c35b 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
2c35c 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2c35d 4f 50 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30  OP_CollSeq, 0, 0
2c35e 2c 20 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f  , 0, (char *)pCo
2c35f 6c 6c 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b  ll, P4_COLLSEQ);
2c360 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2c361 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2c362 4f 50 5f 41 67 67 53 74 65 70 2c 20 30 2c 20 72  OP_AggStep, 0, r
2c363 65 67 41 67 67 2c 20 70 46 2d 3e 69 4d 65 6d 2c  egAgg, pF->iMem,
2c364 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c365 20 20 20 20 20 20 20 28 76 6f 69 64 2a 29 70 46         (void*)pF
2c366 2d 3e 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43  ->pFunc, P4_FUNC
2c367 44 45 46 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  DEF);.    sqlite
2c368 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2c369 20 28 75 38 29 6e 41 72 67 29 3b 0a 20 20 20 20   (u8)nArg);.    
2c36a 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2c36b 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
2c36c 72 65 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20  regAgg, nArg);. 
2c36d 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
2c36e 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
2c36f 65 28 70 50 61 72 73 65 2c 20 72 65 67 41 67 67  e(pParse, regAgg
2c370 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20 69 66 28  , nArg);.    if(
2c371 20 61 64 64 72 4e 65 78 74 20 29 7b 0a 20 20 20   addrNext ){.   
2c372 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
2c373 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64  solveLabel(v, ad
2c374 64 72 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 73  drNext);.      s
2c375 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
2c376 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
2c377 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d    }.  }.  for(i=
2c378 30 2c 20 70 43 3d 70 41 67 67 49 6e 66 6f 2d 3e  0, pC=pAggInfo->
2c379 61 43 6f 6c 3b 20 69 3c 70 41 67 67 49 6e 66 6f  aCol; i<pAggInfo
2c37a 2d 3e 6e 41 63 63 75 6d 75 6c 61 74 6f 72 3b 20  ->nAccumulator; 
2c37b 69 2b 2b 2c 20 70 43 2b 2b 29 7b 0a 20 20 20 20  i++, pC++){.    
2c37c 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2c37d 70 50 61 72 73 65 2c 20 70 43 2d 3e 70 45 78 70  pParse, pC->pExp
2c37e 72 2c 20 70 43 2d 3e 69 4d 65 6d 29 3b 0a 20 20  r, pC->iMem);.  
2c37f 7d 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64 69  }.  pAggInfo->di
2c380 72 65 63 74 4d 6f 64 65 20 3d 20 30 3b 0a 20 20  rectMode = 0;.  
2c381 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2c382 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 7d  Clear(pParse);.}
2c383 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2c384 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 53 45   code for the SE
2c385 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 67  LECT statement g
2c386 69 76 65 6e 20 69 6e 20 74 68 65 20 70 20 61 72  iven in the p ar
2c387 67 75 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20  gument.  .**.** 
2c388 54 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  The results are 
2c389 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 76  distributed in v
2c38a 61 72 69 6f 75 73 20 77 61 79 73 20 64 65 70 65  arious ways depe
2c38b 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0a 2a 2a 20  nding on the.** 
2c38c 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20  contents of the 
2c38d 53 65 6c 65 63 74 44 65 73 74 20 73 74 72 75 63  SelectDest struc
2c38e 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  ture pointed to 
2c38f 62 79 20 61 72 67 75 6d 65 6e 74 20 70 44 65 73  by argument pDes
2c390 74 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  t.** as follows:
2c391 0a 2a 2a 0a 2a 2a 20 20 20 20 20 70 44 65 73 74  .**.**     pDest
2c392 2d 3e 65 44 65 73 74 20 20 20 20 52 65 73 75 6c  ->eDest    Resul
2c393 74 0a 2a 2a 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  t.**     -------
2c394 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  -----    -------
2c395 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c396 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c397 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ----.**     SRT_
2c398 4f 75 74 70 75 74 20 20 20 20 20 20 47 65 6e 65  Output      Gene
2c399 72 61 74 65 20 61 20 72 6f 77 20 6f 66 20 6f 75  rate a row of ou
2c39a 74 70 75 74 20 28 75 73 69 6e 67 20 74 68 65 20  tput (using the 
2c39b 4f 50 5f 52 65 73 75 6c 74 52 6f 77 0a 2a 2a 20  OP_ResultRow.** 
2c39c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c39d 20 20 20 20 6f 70 63 6f 64 65 29 20 66 6f 72 20      opcode) for 
2c39e 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20  each row in the 
2c39f 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a  result set..**.*
2c3a0 2a 20 20 20 20 20 53 52 54 5f 4d 65 6d 20 20 20  *     SRT_Mem   
2c3a1 20 20 20 20 20 20 4f 6e 6c 79 20 76 61 6c 69 64        Only valid
2c3a2 20 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69   if the result i
2c3a3 73 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d  s a single colum
2c3a4 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  n..**           
2c3a5 20 20 20 20 20 20 20 20 20 20 53 74 6f 72 65 20            Store 
2c3a6 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e  the first column
2c3a7 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 72 65   of the first re
2c3a8 73 75 6c 74 20 72 6f 77 0a 2a 2a 20 20 20 20 20  sult row.**     
2c3a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3aa 69 6e 20 72 65 67 69 73 74 65 72 20 70 44 65 73  in register pDes
2c3ab 74 2d 3e 69 50 61 72 6d 20 74 68 65 6e 20 61 62  t->iParm then ab
2c3ac 61 6e 64 6f 6e 20 74 68 65 20 72 65 73 74 0a 2a  andon the rest.*
2c3ad 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2c3ae 20 20 20 20 20 20 6f 66 20 74 68 65 20 71 75 65        of the que
2c3af 72 79 2e 20 20 54 68 69 73 20 64 65 73 74 69 6e  ry.  This destin
2c3b0 61 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 22 4c  ation implies "L
2c3b1 49 4d 49 54 20 31 22 2e 0a 2a 2a 0a 2a 2a 20 20  IMIT 1"..**.**  
2c3b2 20 20 20 53 52 54 5f 53 65 74 20 20 20 20 20 20     SRT_Set      
2c3b3 20 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 75     The result mu
2c3b4 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63  st be a single c
2c3b5 6f 6c 75 6d 6e 2e 20 20 53 74 6f 72 65 20 65 61  olumn.  Store ea
2c3b6 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ch.**           
2c3b7 20 20 20 20 20 20 20 20 20 20 72 6f 77 20 6f 66            row of
2c3b8 20 72 65 73 75 6c 74 20 61 73 20 74 68 65 20 6b   result as the k
2c3b9 65 79 20 69 6e 20 74 61 62 6c 65 20 70 44 65 73  ey in table pDes
2c3ba 74 2d 3e 69 50 61 72 6d 2e 20 0a 2a 2a 20 20 20  t->iParm. .**   
2c3bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3bc 20 20 41 70 70 6c 79 20 74 68 65 20 61 66 66 69    Apply the affi
2c3bd 6e 69 74 79 20 70 44 65 73 74 2d 3e 61 66 66 69  nity pDest->affi
2c3be 6e 69 74 79 20 62 65 66 6f 72 65 20 73 74 6f 72  nity before stor
2c3bf 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ing.**          
2c3c0 20 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c             resul
2c3c1 74 73 2e 20 20 55 73 65 64 20 74 6f 20 69 6d 70  ts.  Used to imp
2c3c2 6c 65 6d 65 6e 74 20 22 49 4e 20 28 53 45 4c 45  lement "IN (SELE
2c3c3 43 54 20 2e 2e 2e 29 22 2e 0a 2a 2a 0a 2a 2a 20  CT ...)"..**.** 
2c3c4 20 20 20 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20      SRT_Union   
2c3c5 20 20 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74      Store result
2c3c6 73 20 61 73 20 61 20 6b 65 79 20 69 6e 20 61 20  s as a key in a 
2c3c7 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
2c3c8 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a  pDest->iParm..**
2c3c9 0a 2a 2a 20 20 20 20 20 53 52 54 5f 45 78 63 65  .**     SRT_Exce
2c3ca 70 74 20 20 20 20 20 20 52 65 6d 6f 76 65 20 72  pt      Remove r
2c3cb 65 73 75 6c 74 73 20 66 72 6f 6d 20 74 68 65 20  esults from the 
2c3cc 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
2c3cd 70 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a  pDest->iParm..**
2c3ce 0a 2a 2a 20 20 20 20 20 53 52 54 5f 54 61 62 6c  .**     SRT_Tabl
2c3cf 65 20 20 20 20 20 20 20 53 74 6f 72 65 20 72 65  e       Store re
2c3d0 73 75 6c 74 73 20 69 6e 20 74 65 6d 70 6f 72 61  sults in tempora
2c3d1 72 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e  ry table pDest->
2c3d2 69 50 61 72 6d 2e 0a 2a 2a 20 20 20 20 20 20 20  iParm..**       
2c3d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68                Th
2c3d4 69 73 20 69 73 20 6c 69 6b 65 20 53 52 54 5f 45  is is like SRT_E
2c3d5 70 68 65 6d 54 61 62 20 65 78 63 65 70 74 20 74  phemTab except t
2c3d6 68 61 74 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a  hat the table.**
2c3d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3d8 20 20 20 20 20 69 73 20 61 73 73 75 6d 65 64 20       is assumed 
2c3d9 74 6f 20 61 6c 72 65 61 64 79 20 62 65 20 6f 70  to already be op
2c3da 65 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52  en..**.**     SR
2c3db 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 43 72  T_EphemTab    Cr
2c3dc 65 61 74 65 20 61 6e 20 74 65 6d 70 6f 72 61 72  eate an temporar
2c3dd 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69  y table pDest->i
2c3de 50 61 72 6d 20 61 6e 64 20 73 74 6f 72 65 0a 2a  Parm and store.*
2c3df 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2c3e0 20 20 20 20 20 20 74 68 65 20 72 65 73 75 6c 74        the result
2c3e1 20 74 68 65 72 65 2e 20 54 68 65 20 63 75 72 73   there. The curs
2c3e2 6f 72 20 69 73 20 6c 65 66 74 20 6f 70 65 6e 20  or is left open 
2c3e3 61 66 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20  after.**        
2c3e4 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
2c3e5 75 72 6e 69 6e 67 2e 20 20 54 68 69 73 20 69 73  urning.  This is
2c3e6 20 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20   like SRT_Table 
2c3e7 65 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 20  except that.**  
2c3e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3e9 20 20 20 74 68 69 73 20 64 65 73 74 69 6e 61 74     this destinat
2c3ea 69 6f 6e 20 75 73 65 73 20 4f 50 5f 4f 70 65 6e  ion uses OP_Open
2c3eb 45 70 68 65 6d 65 72 61 6c 20 74 6f 20 63 72 65  Ephemeral to cre
2c3ec 61 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ate.**          
2c3ed 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74             the t
2c3ee 61 62 6c 65 20 66 69 72 73 74 2e 0a 2a 2a 0a 2a  able first..**.*
2c3ef 2a 20 20 20 20 20 53 52 54 5f 43 6f 72 6f 75 74  *     SRT_Corout
2c3f0 69 6e 65 20 20 20 47 65 6e 65 72 61 74 65 20 61  ine   Generate a
2c3f1 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74 68 61 74   co-routine that
2c3f2 20 72 65 74 75 72 6e 73 20 61 20 6e 65 77 20 72   returns a new r
2c3f3 6f 77 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  ow of.**        
2c3f4 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 73               res
2c3f5 75 6c 74 73 20 65 61 63 68 20 74 69 6d 65 20 69  ults each time i
2c3f6 74 20 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 54  t is invoked.  T
2c3f7 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 0a 2a  he entry point.*
2c3f8 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
2c3f9 20 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f 2d        of the co-
2c3fa 72 6f 75 74 69 6e 65 20 69 73 20 73 74 6f 72 65  routine is store
2c3fb 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 44  d in register pD
2c3fc 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a  est->iParm..**.*
2c3fd 2a 20 20 20 20 20 53 52 54 5f 45 78 69 73 74 73  *     SRT_Exists
2c3fe 20 20 20 20 20 20 53 74 6f 72 65 20 61 20 31 20        Store a 1 
2c3ff 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70  in memory cell p
2c400 44 65 73 74 2d 3e 69 50 61 72 6d 20 69 66 20 74  Dest->iParm if t
2c401 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 20 20 20  he result.**    
2c402 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c403 20 73 65 74 20 69 73 20 6e 6f 74 20 65 6d 70 74   set is not empt
2c404 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  y..**.**     SRT
2c405 5f 44 69 73 63 61 72 64 20 20 20 20 20 54 68 72  _Discard     Thr
2c406 6f 77 20 74 68 65 20 72 65 73 75 6c 74 73 20 61  ow the results a
2c407 77 61 79 2e 20 20 54 68 69 73 20 69 73 20 75 73  way.  This is us
2c408 65 64 20 62 79 20 53 45 4c 45 43 54 0a 2a 2a 20  ed by SELECT.** 
2c409 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c40a 20 20 20 20 73 74 61 74 65 6d 65 6e 74 73 20 77      statements w
2c40b 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 77  ithin triggers w
2c40c 68 6f 73 65 20 6f 6e 6c 79 20 70 75 72 70 6f 73  hose only purpos
2c40d 65 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  e is.**         
2c40e 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
2c40f 73 69 64 65 2d 65 66 66 65 63 74 73 20 6f 66 20  side-effects of 
2c410 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  functions..**.**
2c411 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
2c412 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
2c413 20 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20   of errors.  If 
2c414 61 6e 79 20 65 72 72 6f 72 73 20 61 72 65 0a 2a  any errors are.*
2c415 2a 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74  * encountered, t
2c416 68 65 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61  hen an appropria
2c417 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  te error message
2c418 20 69 73 20 6c 65 66 74 20 69 6e 0a 2a 2a 20 70   is left in.** p
2c419 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a  Parse->zErrMsg..
2c41a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2c41b 6e 65 20 64 6f 65 73 20 4e 4f 54 20 66 72 65 65  ne does NOT free
2c41c 20 74 68 65 20 53 65 6c 65 63 74 20 73 74 72 75   the Select stru
2c41d 63 74 75 72 65 20 70 61 73 73 65 64 20 69 6e 2e  cture passed in.
2c41e 20 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67    The.** calling
2c41f 20 66 75 6e 63 74 69 6f 6e 20 6e 65 65 64 73 20   function needs 
2c420 74 6f 20 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a 53  to do that..*/.S
2c421 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2c422 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  t sqlite3Select(
2c423 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2c424 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2c425 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20   parser context 
2c426 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20  */.  Select *p, 
2c427 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2c428 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2c429 65 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e  ent being coded.
2c42a 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
2c42b 20 2a 70 44 65 73 74 20 20 20 20 20 20 2f 2a 20   *pDest      /* 
2c42c 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20  What to do with 
2c42d 74 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74  the query result
2c42e 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  s */.){.  int i,
2c42f 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
2c430 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2c431 73 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f  s */.  WhereInfo
2c432 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a   *pWInfo;     /*
2c433 20 52 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c   Return from sql
2c434 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
2c435 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20   */.  Vdbe *v;  
2c436 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c437 54 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  The virtual mach
2c438 69 6e 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72  ine under constr
2c439 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  uction */.  int 
2c43a 69 73 41 67 67 3b 20 20 20 20 20 20 20 20 20 20  isAgg;          
2c43b 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 73     /* True for s
2c43c 65 6c 65 63 74 20 6c 69 73 74 73 20 6c 69 6b 65  elect lists like
2c43d 20 22 63 6f 75 6e 74 28 2a 29 22 20 2a 2f 0a 20   "count(*)" */. 
2c43e 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
2c43f 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20  t;      /* List 
2c440 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 65 78  of columns to ex
2c441 74 72 61 63 74 2e 20 2a 2f 0a 20 20 53 72 63 4c  tract. */.  SrcL
2c442 69 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20  ist *pTabList;  
2c443 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61     /* List of ta
2c444 62 6c 65 73 20 74 6f 20 73 65 6c 65 63 74 20 66  bles to select f
2c445 72 6f 6d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  rom */.  Expr *p
2c446 57 68 65 72 65 3b 20 20 20 20 20 20 20 20 20 20  Where;          
2c447 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2c448 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  use.  May be NUL
2c449 4c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  L */.  ExprList 
2c44a 2a 70 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a  *pOrderBy;    /*
2c44b 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
2c44c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55  ause.  May be NU
2c44d 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  LL */.  ExprList
2c44e 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 2f   *pGroupBy;    /
2c44f 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59 20 63  * The GROUP BY c
2c450 6c 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e  lause.  May be N
2c451 55 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ULL */.  Expr *p
2c452 48 61 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20  Having;         
2c453 2f 2a 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c  /* The HAVING cl
2c454 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55  ause.  May be NU
2c455 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69  LL */.  int isDi
2c456 73 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f  stinct;        /
2c457 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 44 49  * True if the DI
2c458 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69  STINCT keyword i
2c459 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69  s present */.  i
2c45a 6e 74 20 64 69 73 74 69 6e 63 74 3b 20 20 20 20  nt distinct;    
2c45b 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74        /* Table t
2c45c 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20 64 69  o use for the di
2c45d 73 74 69 6e 63 74 20 73 65 74 20 2a 2f 0a 20 20  stinct set */.  
2c45e 69 6e 74 20 72 63 20 3d 20 31 3b 20 20 20 20 20  int rc = 1;     
2c45f 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
2c460 74 6f 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74  to return from t
2c461 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  his function */.
2c462 20 20 69 6e 74 20 61 64 64 72 53 6f 72 74 49 6e    int addrSortIn
2c463 64 65 78 3b 20 20 20 20 20 2f 2a 20 41 64 64 72  dex;     /* Addr
2c464 65 73 73 20 6f 66 20 61 6e 20 4f 50 5f 4f 70 65  ess of an OP_Ope
2c465 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72  nEphemeral instr
2c466 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49  uction */.  AggI
2c467 6e 66 6f 20 73 41 67 67 49 6e 66 6f 3b 20 20 20  nfo sAggInfo;   
2c468 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f     /* Informatio
2c469 6e 20 75 73 65 64 20 62 79 20 61 67 67 72 65 67  n used by aggreg
2c46a 61 74 65 20 71 75 65 72 69 65 73 20 2a 2f 0a 20  ate queries */. 
2c46b 20 69 6e 74 20 69 45 6e 64 3b 20 20 20 20 20 20   int iEnd;      
2c46c 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2c46d 73 73 20 6f 66 20 74 68 65 20 65 6e 64 20 6f 66  ss of the end of
2c46e 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20   the query */.  
2c46f 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
2c470 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2c471 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2c472 6e 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 70 50 61  n */..  db = pPa
2c473 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  rse->db;.  if( p
2c474 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ==0 || db->mallo
2c475 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73  cFailed || pPars
2c476 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 72  e->nErr ){.    r
2c477 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69  eturn 1;.  }.  i
2c478 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
2c479 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
2c47a 54 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c  TE_SELECT, 0, 0,
2c47b 20 30 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a   0) ) return 1;.
2c47c 20 20 6d 65 6d 73 65 74 28 26 73 41 67 67 49 6e    memset(&sAggIn
2c47d 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 41  fo, 0, sizeof(sA
2c47e 67 67 49 6e 66 6f 29 29 3b 0a 0a 20 20 69 66 28  ggInfo));..  if(
2c47f 20 49 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62   IgnorableOrderb
2c480 79 28 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20  y(pDest) ){.    
2c481 61 73 73 65 72 74 28 70 44 65 73 74 2d 3e 65 44  assert(pDest->eD
2c482 65 73 74 3d 3d 53 52 54 5f 45 78 69 73 74 73 20  est==SRT_Exists 
2c483 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d  || pDest->eDest=
2c484 3d 53 52 54 5f 55 6e 69 6f 6e 20 7c 7c 20 0a 20  =SRT_Union || . 
2c485 20 20 20 20 20 20 20 20 20 20 70 44 65 73 74 2d            pDest-
2c486 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 78 63 65  >eDest==SRT_Exce
2c487 70 74 20 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65  pt || pDest->eDe
2c488 73 74 3d 3d 53 52 54 5f 44 69 73 63 61 72 64 29  st==SRT_Discard)
2c489 3b 0a 20 20 20 20 2f 2a 20 49 66 20 4f 52 44 45  ;.    /* If ORDE
2c48a 52 20 42 59 20 6d 61 6b 65 73 20 6e 6f 20 64 69  R BY makes no di
2c48b 66 66 65 72 65 6e 63 65 20 69 6e 20 74 68 65 20  fference in the 
2c48c 6f 75 74 70 75 74 20 74 68 65 6e 20 6e 65 69 74  output then neit
2c48d 68 65 72 20 64 6f 65 73 0a 20 20 20 20 2a 2a 20  her does.    ** 
2c48e 44 49 53 54 49 4e 43 54 20 73 6f 20 69 74 20 63  DISTINCT so it c
2c48f 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 20 74 6f  an be removed to
2c490 6f 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  o. */.    sqlite
2c491 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2c492 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29  db, p->pOrderBy)
2c493 3b 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42  ;.    p->pOrderB
2c494 79 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 73 65  y = 0;.    p->se
2c495 6c 46 6c 61 67 73 20 26 3d 20 7e 53 46 5f 44 69  lFlags &= ~SF_Di
2c496 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 20 20 73 71  stinct;.  }.  sq
2c497 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28  lite3SelectPrep(
2c498 70 50 61 72 73 65 2c 20 70 2c 20 30 29 3b 0a 20  pParse, p, 0);. 
2c499 20 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70   pOrderBy = p->p
2c49a 4f 72 64 65 72 42 79 3b 0a 20 20 70 54 61 62 4c  OrderBy;.  pTabL
2c49b 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  ist = p->pSrc;. 
2c49c 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c   pEList = p->pEL
2c49d 69 73 74 3b 0a 20 20 69 66 28 20 70 50 61 72 73  ist;.  if( pPars
2c49e 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d  e->nErr || db->m
2c49f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2c4a0 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65     goto select_e
2c4a1 6e 64 3b 0a 20 20 7d 0a 20 20 69 73 41 67 67 20  nd;.  }.  isAgg 
2c4a2 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  = (p->selFlags &
2c4a3 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 21 3d   SF_Aggregate)!=
2c4a4 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c  0;.  assert( pEL
2c4a5 69 73 74 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  ist!=0 );..  /* 
2c4a6 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67  Begin generating
2c4a7 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20   code..  */.  v 
2c4a8 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2c4a9 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
2c4aa 76 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65  v==0 ) goto sele
2c4ab 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 47 65  ct_end;..  /* Ge
2c4ac 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2c4ad 61 6c 6c 20 73 75 62 2d 71 75 65 72 69 65 73 20  all sub-queries 
2c4ae 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2c4af 73 65 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66  se.  */.#if !def
2c4b0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2c4b1 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64  _SUBQUERY) || !d
2c4b2 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2c4b3 49 54 5f 56 49 45 57 29 0a 20 20 66 6f 72 28 69  IT_VIEW).  for(i
2c4b4 3d 30 3b 20 21 70 2d 3e 70 50 72 69 6f 72 20 26  =0; !p->pPrior &
2c4b5 26 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  & i<pTabList->nS
2c4b6 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74  rc; i++){.    st
2c4b7 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2c4b8 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54 61 62  m *pItem = &pTab
2c4b9 4c 69 73 74 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  List->a[i];.    
2c4ba 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b  SelectDest dest;
2c4bb 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 75  .    Select *pSu
2c4bc 62 20 3d 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65  b = pItem->pSele
2c4bd 63 74 3b 0a 20 20 20 20 69 6e 74 20 69 73 41 67  ct;.    int isAg
2c4be 67 53 75 62 3b 0a 0a 20 20 20 20 69 66 28 20 70  gSub;..    if( p
2c4bf 53 75 62 3d 3d 30 20 7c 7c 20 70 49 74 65 6d 2d  Sub==0 || pItem-
2c4c0 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 29 20 63  >isPopulated ) c
2c4c1 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 2f 2a  ontinue;..    /*
2c4c2 20 49 6e 63 72 65 6d 65 6e 74 20 50 61 72 73 65   Increment Parse
2c4c3 2e 6e 48 65 69 67 68 74 20 62 79 20 74 68 65 20  .nHeight by the 
2c4c4 68 65 69 67 68 74 20 6f 66 20 74 68 65 20 6c 61  height of the la
2c4c5 72 67 65 73 74 20 65 78 70 72 65 73 73 69 6f 6e  rgest expression
2c4c6 0a 20 20 20 20 2a 2a 20 74 72 65 65 20 72 65 66  .    ** tree ref
2c4c7 65 72 65 64 20 74 6f 20 62 79 20 74 68 69 73 2c  ered to by this,
2c4c8 20 74 68 65 20 70 61 72 65 6e 74 20 73 65 6c 65   the parent sele
2c4c9 63 74 2e 20 54 68 65 20 63 68 69 6c 64 20 73 65  ct. The child se
2c4ca 6c 65 63 74 0a 20 20 20 20 2a 2a 20 6d 61 79 20  lect.    ** may 
2c4cb 63 6f 6e 74 61 69 6e 20 65 78 70 72 65 73 73 69  contain expressi
2c4cc 6f 6e 20 74 72 65 65 73 20 6f 66 20 61 74 20 6d  on trees of at m
2c4cd 6f 73 74 0a 20 20 20 20 2a 2a 20 28 53 51 4c 49  ost.    ** (SQLI
2c4ce 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
2c4cf 48 2d 50 61 72 73 65 2e 6e 48 65 69 67 68 74 29  H-Parse.nHeight)
2c4d0 20 68 65 69 67 68 74 2e 20 54 68 69 73 20 69 73   height. This is
2c4d1 20 61 20 62 69 74 0a 20 20 20 20 2a 2a 20 6d 6f   a bit.    ** mo
2c4d2 72 65 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20  re conservative 
2c4d3 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79 2c 20  than necessary, 
2c4d4 62 75 74 20 6d 75 63 68 20 65 61 73 69 65 72 20  but much easier 
2c4d5 74 68 61 6e 20 65 6e 66 6f 72 63 69 6e 67 0a 20  than enforcing. 
2c4d6 20 20 20 2a 2a 20 61 6e 20 65 78 61 63 74 20 6c     ** an exact l
2c4d7 69 6d 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  imit..    */.   
2c4d8 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74   pParse->nHeight
2c4d9 20 2b 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63   += sqlite3Selec
2c4da 74 45 78 70 72 48 65 69 67 68 74 28 70 29 3b 0a  tExprHeight(p);.
2c4db 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  .    /* Check to
2c4dc 20 73 65 65 20 69 66 20 74 68 65 20 73 75 62 71   see if the subq
2c4dd 75 65 72 79 20 63 61 6e 20 62 65 20 61 62 73 6f  uery can be abso
2c4de 72 62 65 64 20 69 6e 74 6f 20 74 68 65 20 70 61  rbed into the pa
2c4df 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 73 41  rent. */.    isA
2c4e0 67 67 53 75 62 20 3d 20 28 70 53 75 62 2d 3e 73  ggSub = (pSub->s
2c4e1 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67  elFlags & SF_Agg
2c4e2 72 65 67 61 74 65 29 21 3d 30 3b 0a 20 20 20 20  regate)!=0;.    
2c4e3 69 66 28 20 66 6c 61 74 74 65 6e 53 75 62 71 75  if( flattenSubqu
2c4e4 65 72 79 28 70 50 61 72 73 65 2c 20 70 2c 20 69  ery(pParse, p, i
2c4e5 2c 20 69 73 41 67 67 2c 20 69 73 41 67 67 53 75  , isAgg, isAggSu
2c4e6 62 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  b) ){.      if( 
2c4e7 69 73 41 67 67 53 75 62 20 29 7b 0a 20 20 20 20  isAggSub ){.    
2c4e8 20 20 20 20 69 73 41 67 67 20 3d 20 31 3b 0a 20      isAgg = 1;. 
2c4e9 20 20 20 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61         p->selFla
2c4ea 67 73 20 7c 3d 20 53 46 5f 41 67 67 72 65 67 61  gs |= SF_Aggrega
2c4eb 74 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  te;.      }.    
2c4ec 20 20 69 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65    i = -1;.    }e
2c4ed 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
2c4ee 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
2c4ef 28 26 64 65 73 74 2c 20 53 52 54 5f 45 70 68 65  (&dest, SRT_Ephe
2c4f0 6d 54 61 62 2c 20 70 49 74 65 6d 2d 3e 69 43 75  mTab, pItem->iCu
2c4f1 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 61 73 73  rsor);.      ass
2c4f2 65 72 74 28 20 70 49 74 65 6d 2d 3e 69 73 50 6f  ert( pItem->isPo
2c4f3 70 75 6c 61 74 65 64 3d 3d 30 20 29 3b 0a 20 20  pulated==0 );.  
2c4f4 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2c4f5 74 28 70 50 61 72 73 65 2c 20 70 53 75 62 2c 20  t(pParse, pSub, 
2c4f6 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 70 49  &dest);.      pI
2c4f7 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64  tem->isPopulated
2c4f8 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
2c4f9 69 66 28 20 2f 2a 70 50 61 72 73 65 2d 3e 6e 45  if( /*pParse->nE
2c4fa 72 72 20 7c 7c 2a 2f 20 64 62 2d 3e 6d 61 6c 6c  rr ||*/ db->mall
2c4fb 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2c4fc 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e    goto select_en
2c4fd 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  d;.    }.    pPa
2c4fe 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20  rse->nHeight -= 
2c4ff 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
2c500 72 48 65 69 67 68 74 28 70 29 3b 0a 20 20 20 20  rHeight(p);.    
2c501 70 54 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53  pTabList = p->pS
2c502 72 63 3b 0a 20 20 20 20 69 66 28 20 21 49 67 6e  rc;.    if( !Ign
2c503 6f 72 61 62 6c 65 4f 72 64 65 72 62 79 28 70 44  orableOrderby(pD
2c504 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 70 4f  est) ){.      pO
2c505 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64  rderBy = p->pOrd
2c506 65 72 42 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  erBy;.    }.  }.
2c507 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45    pEList = p->pE
2c508 4c 69 73 74 3b 0a 23 65 6e 64 69 66 0a 20 20 70  List;.#endif.  p
2c509 57 68 65 72 65 20 3d 20 70 2d 3e 70 57 68 65 72  Where = p->pWher
2c50a 65 3b 0a 20 20 70 47 72 6f 75 70 42 79 20 3d 20  e;.  pGroupBy = 
2c50b 70 2d 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 70  p->pGroupBy;.  p
2c50c 48 61 76 69 6e 67 20 3d 20 70 2d 3e 70 48 61 76  Having = p->pHav
2c50d 69 6e 67 3b 0a 20 20 69 73 44 69 73 74 69 6e 63  ing;.  isDistinc
2c50e 74 20 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73  t = (p->selFlags
2c50f 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21   & SF_Distinct)!
2c510 3d 30 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  =0;..#ifndef SQL
2c511 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e  ITE_OMIT_COMPOUN
2c512 44 5f 53 45 4c 45 43 54 0a 20 20 2f 2a 20 49 66  D_SELECT.  /* If
2c513 20 74 68 65 72 65 20 69 73 20 61 72 65 20 61 20   there is are a 
2c514 73 65 71 75 65 6e 63 65 20 6f 66 20 71 75 65 72  sequence of quer
2c515 69 65 73 2c 20 64 6f 20 74 68 65 20 65 61 72 6c  ies, do the earl
2c516 69 65 72 20 6f 6e 65 73 20 66 69 72 73 74 2e 0a  ier ones first..
2c517 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50    */.  if( p->pP
2c518 72 69 6f 72 20 29 7b 0a 20 20 20 20 69 66 28 20  rior ){.    if( 
2c519 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 30  p->pRightmost==0
2c51a 20 29 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74   ){.      Select
2c51b 20 2a 70 4c 6f 6f 70 2c 20 2a 70 52 69 67 68 74   *pLoop, *pRight
2c51c 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20   = 0;.      int 
2c51d 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  cnt = 0;.      i
2c51e 6e 74 20 6d 78 53 65 6c 65 63 74 3b 0a 20 20 20  nt mxSelect;.   
2c51f 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20     for(pLoop=p; 
2c520 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f  pLoop; pLoop=pLo
2c521 6f 70 2d 3e 70 50 72 69 6f 72 2c 20 63 6e 74 2b  op->pPrior, cnt+
2c522 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f  +){.        pLoo
2c523 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20  p->pRightmost = 
2c524 70 3b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70  p;.        pLoop
2c525 2d 3e 70 4e 65 78 74 20 3d 20 70 52 69 67 68 74  ->pNext = pRight
2c526 3b 0a 20 20 20 20 20 20 20 20 70 52 69 67 68 74  ;.        pRight
2c527 20 3d 20 70 4c 6f 6f 70 3b 0a 20 20 20 20 20 20   = pLoop;.      
2c528 7d 0a 20 20 20 20 20 20 6d 78 53 65 6c 65 63 74  }.      mxSelect
2c529 20 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51   = db->aLimit[SQ
2c52a 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f  LITE_LIMIT_COMPO
2c52b 55 4e 44 5f 53 45 4c 45 43 54 5d 3b 0a 20 20 20  UND_SELECT];.   
2c52c 20 20 20 69 66 28 20 6d 78 53 65 6c 65 63 74 20     if( mxSelect 
2c52d 26 26 20 63 6e 74 3e 6d 78 53 65 6c 65 63 74 20  && cnt>mxSelect 
2c52e 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
2c52f 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2c530 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72  e, "too many ter
2c531 6d 73 20 69 6e 20 63 6f 6d 70 6f 75 6e 64 20 53  ms in compound S
2c532 45 4c 45 43 54 22 29 3b 0a 20 20 20 20 20 20 20  ELECT");.       
2c533 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
2c534 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74   }.    }.    ret
2c535 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65 63 74 28  urn multiSelect(
2c536 70 50 61 72 73 65 2c 20 70 2c 20 70 44 65 73 74  pParse, p, pDest
2c537 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
2c538 20 2f 2a 20 49 66 20 77 72 69 74 69 6e 67 20 74   /* If writing t
2c539 6f 20 6d 65 6d 6f 72 79 20 6f 72 20 67 65 6e 65  o memory or gene
2c53a 72 61 74 69 6e 67 20 61 20 73 65 74 0a 20 20 2a  rating a set.  *
2c53b 2a 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20  * only a single 
2c53c 63 6f 6c 75 6d 6e 20 6d 61 79 20 62 65 20 6f 75  column may be ou
2c53d 74 70 75 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  tput..  */.#ifnd
2c53e 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
2c53f 55 42 51 55 45 52 59 0a 20 20 69 66 28 20 63 68  UBQUERY.  if( ch
2c540 65 63 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d  eckForMultiColum
2c541 6e 53 65 6c 65 63 74 45 72 72 6f 72 28 70 50 61  nSelectError(pPa
2c542 72 73 65 2c 20 70 44 65 73 74 2c 20 70 45 4c 69  rse, pDest, pELi
2c543 73 74 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20  st->nExpr) ){.  
2c544 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e    goto select_en
2c545 64 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  d;.  }.#endif.. 
2c546 20 2f 2a 20 49 66 20 70 6f 73 73 69 62 6c 65 2c   /* If possible,
2c547 20 72 65 77 72 69 74 65 20 74 68 65 20 71 75 65   rewrite the que
2c548 72 79 20 74 6f 20 75 73 65 20 47 52 4f 55 50 20  ry to use GROUP 
2c549 42 59 20 69 6e 73 74 65 61 64 20 6f 66 20 44 49  BY instead of DI
2c54a 53 54 49 4e 43 54 2e 0a 20 20 2a 2a 20 47 52 4f  STINCT..  ** GRO
2c54b 55 50 20 42 59 20 6d 69 67 68 74 20 75 73 65 20  UP BY might use 
2c54c 61 6e 20 69 6e 64 65 78 2c 20 44 49 53 54 49 4e  an index, DISTIN
2c54d 43 54 20 6e 65 76 65 72 20 64 6f 65 73 2e 0a 20  CT never does.. 
2c54e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
2c54f 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20 7c 7c 20  >pGroupBy==0 || 
2c550 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  (p->selFlags & S
2c551 46 5f 41 67 67 72 65 67 61 74 65 29 21 3d 30 20  F_Aggregate)!=0 
2c552 29 3b 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c  );.  if( (p->sel
2c553 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74  Flags & (SF_Dist
2c554 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74  inct|SF_Aggregat
2c555 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74  e))==SF_Distinct
2c556 20 29 7b 0a 20 20 20 20 70 2d 3e 70 47 72 6f 75   ){.    p->pGrou
2c557 70 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70  pBy = sqlite3Exp
2c558 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
2c559 70 45 4c 69 73 74 2c 20 30 29 3b 0a 20 20 20 20  pEList, 0);.    
2c55a 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47  pGroupBy = p->pG
2c55b 72 6f 75 70 42 79 3b 0a 20 20 20 20 70 2d 3e 73  roupBy;.    p->s
2c55c 65 6c 46 6c 61 67 73 20 26 3d 20 7e 53 46 5f 44  elFlags &= ~SF_D
2c55d 69 73 74 69 6e 63 74 3b 0a 20 20 20 20 69 73 44  istinct;.    isD
2c55e 69 73 74 69 6e 63 74 20 3d 20 30 3b 0a 20 20 7d  istinct = 0;.  }
2c55f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
2c560 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
2c561 6c 61 75 73 65 2c 20 74 68 65 6e 20 74 68 69 73  lause, then this
2c562 20 73 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 69 6e   sorting.  ** in
2c563 64 65 78 20 6d 69 67 68 74 20 65 6e 64 20 75 70  dex might end up
2c564 20 62 65 69 6e 67 20 75 6e 75 73 65 64 20 69 66   being unused if
2c565 20 74 68 65 20 64 61 74 61 20 63 61 6e 20 62 65   the data can be
2c566 20 0a 20 20 2a 2a 20 65 78 74 72 61 63 74 65 64   .  ** extracted
2c567 20 69 6e 20 70 72 65 2d 73 6f 72 74 65 64 20 6f   in pre-sorted o
2c568 72 64 65 72 2e 20 20 49 66 20 74 68 61 74 20 69  rder.  If that i
2c569 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e  s the case, then
2c56a 20 74 68 65 0a 20 20 2a 2a 20 4f 50 5f 4f 70 65   the.  ** OP_Ope
2c56b 6e 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72  nEphemeral instr
2c56c 75 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63  uction will be c
2c56d 68 61 6e 67 65 64 20 74 6f 20 61 6e 20 4f 50 5f  hanged to an OP_
2c56e 4e 6f 6f 70 20 6f 6e 63 65 0a 20 20 2a 2a 20 77  Noop once.  ** w
2c56f 65 20 66 69 67 75 72 65 20 6f 75 74 20 74 68 61  e figure out tha
2c570 74 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e  t the sorting in
2c571 64 65 78 20 69 73 20 6e 6f 74 20 6e 65 65 64 65  dex is not neede
2c572 64 2e 20 20 54 68 65 20 61 64 64 72 53 6f 72 74  d.  The addrSort
2c573 49 6e 64 65 78 0a 20 20 2a 2a 20 76 61 72 69 61  Index.  ** varia
2c574 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20 66  ble is used to f
2c575 61 63 69 6c 69 74 61 74 65 20 74 68 61 74 20 63  acilitate that c
2c576 68 61 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  hange..  */.  if
2c577 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ( pOrderBy ){.  
2c578 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
2c579 6e 66 6f 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66  nfo;.    pKeyInf
2c57a 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45  o = keyInfoFromE
2c57b 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  xprList(pParse, 
2c57c 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 70  pOrderBy);.    p
2c57d 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f  OrderBy->iECurso
2c57e 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62  r = pParse->nTab
2c57f 2b 2b 3b 0a 20 20 20 20 70 2d 3e 61 64 64 72 4f  ++;.    p->addrO
2c580 70 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 61 64 64  penEphm[2] = add
2c581 72 53 6f 72 74 49 6e 64 65 78 20 3d 0a 20 20 20  rSortIndex =.   
2c582 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c583 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45  dOp4(v, OP_OpenE
2c584 70 68 65 6d 65 72 61 6c 2c 0a 20 20 20 20 20 20  phemeral,.      
2c585 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c586 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69       pOrderBy->i
2c587 45 43 75 72 73 6f 72 2c 20 70 4f 72 64 65 72 42  ECursor, pOrderB
2c588 79 2d 3e 6e 45 78 70 72 2b 32 2c 20 30 2c 0a 20  y->nExpr+2, 0,. 
2c589 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c58a 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
2c58b 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45  )pKeyInfo, P4_KE
2c58c 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
2c58d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64    }else{.    add
2c58e 72 53 6f 72 74 49 6e 64 65 78 20 3d 20 2d 31 3b  rSortIndex = -1;
2c58f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
2c590 65 20 6f 75 74 70 75 74 20 69 73 20 64 65 73 74  e output is dest
2c591 69 6e 65 64 20 66 6f 72 20 61 20 74 65 6d 70 6f  ined for a tempo
2c592 72 61 72 79 20 74 61 62 6c 65 2c 20 6f 70 65 6e  rary table, open
2c593 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 20 20 2a   that table..  *
2c594 2f 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 65  /.  if( pDest->e
2c595 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54  Dest==SRT_EphemT
2c596 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ab ){.    sqlite
2c597 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c598 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
2c599 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 70   pDest->iParm, p
2c59a 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20  EList->nExpr);. 
2c59b 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
2c59c 20 6c 69 6d 69 74 65 72 2e 0a 20 20 2a 2f 0a 20   limiter..  */. 
2c59d 20 69 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56   iEnd = sqlite3V
2c59e 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
2c59f 0a 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52  .  computeLimitR
2c5a0 65 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c  egisters(pParse,
2c5a1 20 70 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a   p, iEnd);..  /*
2c5a2 20 4f 70 65 6e 20 61 20 76 69 72 74 75 61 6c 20   Open a virtual 
2c5a3 69 6e 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72  index to use for
2c5a4 20 74 68 65 20 64 69 73 74 69 6e 63 74 20 73 65   the distinct se
2c5a5 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  t..  */.  if( is
2c5a6 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20  Distinct ){.    
2c5a7 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
2c5a8 6f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  o;.    assert( i
2c5a9 73 41 67 67 20 7c 7c 20 70 47 72 6f 75 70 42 79  sAgg || pGroupBy
2c5aa 20 29 3b 0a 20 20 20 20 64 69 73 74 69 6e 63 74   );.    distinct
2c5ab 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2c5ac 2b 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20  +;.    pKeyInfo 
2c5ad 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70  = keyInfoFromExp
2c5ae 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 2d  rList(pParse, p-
2c5af 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 71  >pEList);.    sq
2c5b0 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2c5b1 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
2c5b2 72 61 6c 2c 20 64 69 73 74 69 6e 63 74 2c 20 30  ral, distinct, 0
2c5b3 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
2c5b4 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68               (ch
2c5b5 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34  ar*)pKeyInfo, P4
2c5b6 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
2c5b7 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2c5b8 64 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20  distinct = -1;. 
2c5b9 20 7d 0a 0a 20 20 2f 2a 20 41 67 67 72 65 67 61   }..  /* Aggrega
2c5ba 74 65 20 61 6e 64 20 6e 6f 6e 2d 61 67 67 72 65  te and non-aggre
2c5bb 67 61 74 65 20 71 75 65 72 69 65 73 20 61 72 65  gate queries are
2c5bc 20 68 61 6e 64 6c 65 64 20 64 69 66 66 65 72 65   handled differe
2c5bd 6e 74 6c 79 20 2a 2f 0a 20 20 69 66 28 20 21 69  ntly */.  if( !i
2c5be 73 41 67 67 20 26 26 20 70 47 72 6f 75 70 42 79  sAgg && pGroupBy
2c5bf 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  ==0 ){.    /* Th
2c5c0 69 73 20 63 61 73 65 20 69 73 20 66 6f 72 20 6e  is case is for n
2c5c1 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 71 75 65  on-aggregate que
2c5c2 72 69 65 73 0a 20 20 20 20 2a 2a 20 42 65 67 69  ries.    ** Begi
2c5c3 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  n the database s
2c5c4 63 61 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  can.    */.    p
2c5c5 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57  WInfo = sqlite3W
2c5c6 68 65 72 65 42 65 67 69 6e 28 70 50 61 72 73 65  hereBegin(pParse
2c5c7 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65  , pTabList, pWhe
2c5c8 72 65 2c 20 26 70 4f 72 64 65 72 42 79 2c 20 30  re, &pOrderBy, 0
2c5c9 29 3b 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66  );.    if( pWInf
2c5ca 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65  o==0 ) goto sele
2c5cb 63 74 5f 65 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20  ct_end;..    /* 
2c5cc 49 66 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  If sorting index
2c5cd 20 74 68 61 74 20 77 61 73 20 63 72 65 61 74 65   that was create
2c5ce 64 20 62 79 20 61 20 70 72 69 6f 72 20 4f 50 5f  d by a prior OP_
2c5cf 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 0a 20  OpenEphemeral . 
2c5d0 20 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f     ** instructio
2c5d1 6e 20 65 6e 64 65 64 20 75 70 20 6e 6f 74 20 62  n ended up not b
2c5d2 65 69 6e 67 20 6e 65 65 64 65 64 2c 20 74 68 65  eing needed, the
2c5d3 6e 20 63 68 61 6e 67 65 20 74 68 65 20 4f 50 5f  n change the OP_
2c5d4 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 0a 20 20  OpenEphemeral.  
2c5d5 20 20 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f    ** into an OP_
2c5d6 4e 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  Noop..    */.   
2c5d7 20 69 66 28 20 61 64 64 72 53 6f 72 74 49 6e 64   if( addrSortInd
2c5d8 65 78 3e 3d 30 20 26 26 20 70 4f 72 64 65 72 42  ex>=0 && pOrderB
2c5d9 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  y==0 ){.      sq
2c5da 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 54  lite3VdbeChangeT
2c5db 6f 4e 6f 6f 70 28 76 2c 20 61 64 64 72 53 6f 72  oNoop(v, addrSor
2c5dc 74 49 6e 64 65 78 2c 20 31 29 3b 0a 20 20 20 20  tIndex, 1);.    
2c5dd 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68    p->addrOpenEph
2c5de 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 7d  m[2] = -1;.    }
2c5df 0a 0a 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65  ..    /* Use the
2c5e0 20 73 74 61 6e 64 61 72 64 20 69 6e 6e 65 72 20   standard inner 
2c5e1 6c 6f 6f 70 0a 20 20 20 20 2a 2f 0a 20 20 20 20  loop.    */.    
2c5e2 61 73 73 65 72 74 28 21 69 73 44 69 73 74 69 6e  assert(!isDistin
2c5e3 63 74 29 3b 0a 20 20 20 20 73 65 6c 65 63 74 49  ct);.    selectI
2c5e4 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c  nnerLoop(pParse,
2c5e5 20 70 2c 20 70 45 4c 69 73 74 2c 20 30 2c 20 30   p, pEList, 0, 0
2c5e6 2c 20 70 4f 72 64 65 72 42 79 2c 20 2d 31 2c 20  , pOrderBy, -1, 
2c5e7 70 44 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20  pDest,.         
2c5e8 20 20 20 20 20 20 20 20 20 20 20 70 57 49 6e 66             pWInf
2c5e9 6f 2d 3e 69 43 6f 6e 74 69 6e 75 65 2c 20 70 57  o->iContinue, pW
2c5ea 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29 3b 0a 0a  Info->iBreak);..
2c5eb 20 20 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64      /* End the d
2c5ec 61 74 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f  atabase scan loo
2c5ed 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  p..    */.    sq
2c5ee 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57  lite3WhereEnd(pW
2c5ef 49 6e 66 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Info);.  }else{.
2c5f0 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74      /* This is t
2c5f1 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f  he processing fo
2c5f2 72 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72  r aggregate quer
2c5f3 69 65 73 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43  ies */.    NameC
2c5f4 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 2f  ontext sNC;    /
2c5f5 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66  * Name context f
2c5f6 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 20 61 67  or processing ag
2c5f7 67 72 65 67 61 74 65 20 69 6e 66 6f 72 6d 61 74  gregate informat
2c5f8 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ion */.    int i
2c5f9 41 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  AMem;          /
2c5fa 2a 20 46 69 72 73 74 20 4d 65 6d 20 61 64 64 72  * First Mem addr
2c5fb 65 73 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20  ess for storing 
2c5fc 63 75 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59  current GROUP BY
2c5fd 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 42 4d 65   */.    int iBMe
2c5fe 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  m;          /* F
2c5ff 69 72 73 74 20 4d 65 6d 20 61 64 64 72 65 73 73  irst Mem address
2c600 20 66 6f 72 20 70 72 65 76 69 6f 75 73 20 47 52   for previous GR
2c601 4f 55 50 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e  OUP BY */.    in
2c602 74 20 69 55 73 65 46 6c 61 67 3b 20 20 20 20 20  t iUseFlag;     
2c603 20 20 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73    /* Mem address
2c604 20 68 6f 6c 64 69 6e 67 20 66 6c 61 67 20 69 6e   holding flag in
2c605 64 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 74  dicating that at
2c606 20 6c 65 61 73 74 0a 20 20 20 20 20 20 20 20 20   least.         
2c607 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
2c608 2a 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65  * one row of the
2c609 20 69 6e 70 75 74 20 74 6f 20 74 68 65 20 61 67   input to the ag
2c60a 67 72 65 67 61 74 6f 72 20 68 61 73 20 62 65 65  gregator has bee
2c60b 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n.              
2c60c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f            ** pro
2c60d 63 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 69 6e  cessed */.    in
2c60e 74 20 69 41 62 6f 72 74 46 6c 61 67 3b 20 20 20  t iAbortFlag;   
2c60f 20 20 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73    /* Mem address
2c610 20 77 68 69 63 68 20 63 61 75 73 65 73 20 71 75   which causes qu
2c611 65 72 79 20 61 62 6f 72 74 20 69 66 20 70 6f 73  ery abort if pos
2c612 69 74 69 76 65 20 2a 2f 0a 20 20 20 20 69 6e 74  itive */.    int
2c613 20 67 72 6f 75 70 42 79 53 6f 72 74 3b 20 20 20   groupBySort;   
2c614 20 2f 2a 20 52 6f 77 73 20 63 6f 6d 65 20 66 72   /* Rows come fr
2c615 6f 6d 20 73 6f 75 72 63 65 20 69 6e 20 47 52 4f  om source in GRO
2c616 55 50 20 42 59 20 6f 72 64 65 72 20 2a 2f 0a 20  UP BY order */. 
2c617 20 20 20 69 6e 74 20 61 64 64 72 45 6e 64 3b 20     int addrEnd; 
2c618 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66         /* End of
2c619 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20   processing for 
2c61a 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 0a  this SELECT */..
2c61b 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e      /* Remove an
2c61c 79 20 61 6e 64 20 61 6c 6c 20 61 6c 69 61 73 65  y and all aliase
2c61d 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 72 65  s between the re
2c61e 73 75 6c 74 20 73 65 74 20 61 6e 64 20 74 68 65  sult set and the
2c61f 0a 20 20 20 20 2a 2a 20 47 52 4f 55 50 20 42 59  .    ** GROUP BY
2c620 20 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a   clause..    */.
2c621 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 79      if( pGroupBy
2c622 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b   ){.      int k;
2c623 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c624 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
2c625 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20  counter */.     
2c626 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
2c627 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 2f  _item *pItem;  /
2c628 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
2c629 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  er expression in
2c62a 20 61 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20   a list */..    
2c62b 20 20 66 6f 72 28 6b 3d 70 2d 3e 70 45 4c 69 73    for(k=p->pELis
2c62c 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d  t->nExpr, pItem=
2c62d 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b 20 6b 3e  p->pEList->a; k>
2c62e 30 3b 20 6b 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29  0; k--, pItem++)
2c62f 7b 0a 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d  {.        pItem-
2c630 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 20  >iAlias = 0;.   
2c631 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6b     }.      for(k
2c632 3d 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72  =pGroupBy->nExpr
2c633 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79  , pItem=pGroupBy
2c634 2d 3e 61 3b 20 6b 3e 30 3b 20 6b 2d 2d 2c 20 70  ->a; k>0; k--, p
2c635 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
2c636 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d   pItem->iAlias =
2c637 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
2c638 7d 0a 0a 20 0a 20 20 20 20 2f 2a 20 43 72 65 61  }.. .    /* Crea
2c639 74 65 20 61 20 6c 61 62 65 6c 20 74 6f 20 6a 75  te a label to ju
2c63a 6d 70 20 74 6f 20 77 68 65 6e 20 77 65 20 77 61  mp to when we wa
2c63b 6e 74 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20  nt to abort the 
2c63c 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 61 64 64  query */.    add
2c63d 72 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64  rEnd = sqlite3Vd
2c63e 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
2c63f 0a 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20  .    /* Convert 
2c640 54 4b 5f 43 4f 4c 55 4d 4e 20 6e 6f 64 65 73 20  TK_COLUMN nodes 
2c641 69 6e 74 6f 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  into TK_AGG_COLU
2c642 4d 4e 20 61 6e 64 20 6d 61 6b 65 20 65 6e 74 72  MN and make entr
2c643 69 65 73 20 69 6e 0a 20 20 20 20 2a 2a 20 73 41  ies in.    ** sA
2c644 67 67 49 6e 66 6f 20 66 6f 72 20 61 6c 6c 20 54  ggInfo for all T
2c645 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 6e  K_AGG_FUNCTION n
2c646 6f 64 65 73 20 69 6e 20 65 78 70 72 65 73 73 69  odes in expressi
2c647 6f 6e 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a  ons of the.    *
2c648 2a 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  * SELECT stateme
2c649 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d  nt..    */.    m
2c64a 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73  emset(&sNC, 0, s
2c64b 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20  izeof(sNC));.   
2c64c 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50   sNC.pParse = pP
2c64d 61 72 73 65 3b 0a 20 20 20 20 73 4e 43 2e 70 53  arse;.    sNC.pS
2c64e 72 63 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73  rcList = pTabLis
2c64f 74 3b 0a 20 20 20 20 73 4e 43 2e 70 41 67 67 49  t;.    sNC.pAggI
2c650 6e 66 6f 20 3d 20 26 73 41 67 67 49 6e 66 6f 3b  nfo = &sAggInfo;
2c651 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e 53  .    sAggInfo.nS
2c652 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 20 3d 20 70  ortingColumn = p
2c653 47 72 6f 75 70 42 79 20 3f 20 70 47 72 6f 75 70  GroupBy ? pGroup
2c654 42 79 2d 3e 6e 45 78 70 72 2b 31 20 3a 20 30 3b  By->nExpr+1 : 0;
2c655 0a 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 70 47  .    sAggInfo.pG
2c656 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42  roupBy = pGroupB
2c657 79 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  y;.    sqlite3Ex
2c658 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74  prAnalyzeAggList
2c659 28 26 73 4e 43 2c 20 70 45 4c 69 73 74 29 3b 0a  (&sNC, pEList);.
2c65a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41      sqlite3ExprA
2c65b 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73  nalyzeAggList(&s
2c65c 4e 43 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20  NC, pOrderBy);. 
2c65d 20 20 20 69 66 28 20 70 48 61 76 69 6e 67 20 29     if( pHaving )
2c65e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
2c65f 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67  xprAnalyzeAggreg
2c660 61 74 65 73 28 26 73 4e 43 2c 20 70 48 61 76 69  ates(&sNC, pHavi
2c661 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ng);.    }.    s
2c662 41 67 67 49 6e 66 6f 2e 6e 41 63 63 75 6d 75 6c  AggInfo.nAccumul
2c663 61 74 6f 72 20 3d 20 73 41 67 67 49 6e 66 6f 2e  ator = sAggInfo.
2c664 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 66 6f 72  nColumn;.    for
2c665 28 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f  (i=0; i<sAggInfo
2c666 2e 6e 46 75 6e 63 3b 20 69 2b 2b 29 7b 0a 20 20  .nFunc; i++){.  
2c667 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2c668 72 48 61 73 50 72 6f 70 65 72 74 79 28 73 41 67  rHasProperty(sAg
2c669 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e 70  gInfo.aFunc[i].p
2c66a 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65  Expr, EP_xIsSele
2c66b 63 74 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ct) );.      sql
2c66c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
2c66d 67 67 4c 69 73 74 28 26 73 4e 43 2c 20 73 41 67  ggList(&sNC, sAg
2c66e 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e 70  gInfo.aFunc[i].p
2c66f 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a  Expr->x.pList);.
2c670 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62      }.    if( db
2c671 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2c672 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
2c673 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73  ;..    /* Proces
2c674 73 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61  sing for aggrega
2c675 74 65 73 20 77 69 74 68 20 47 52 4f 55 50 20 42  tes with GROUP B
2c676 59 20 69 73 20 76 65 72 79 20 64 69 66 66 65 72  Y is very differ
2c677 65 6e 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 6d  ent and.    ** m
2c678 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78  uch more complex
2c679 20 74 68 61 6e 20 61 67 67 72 65 67 61 74 65 73   than aggregates
2c67a 20 77 69 74 68 6f 75 74 20 61 20 47 52 4f 55 50   without a GROUP
2c67b 20 42 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   BY..    */.    
2c67c 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a  if( pGroupBy ){.
2c67d 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
2c67e 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 4b 65 79  KeyInfo;  /* Key
2c67f 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
2c680 66 6f 72 20 74 68 65 20 67 72 6f 75 70 20 62 79  for the group by
2c681 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20   clause */.     
2c682 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20   int j1;        
2c683 20 20 20 20 20 2f 2a 20 41 2d 76 73 2d 42 20 63       /* A-vs-B c
2c684 6f 6d 70 61 72 69 73 69 6f 6e 20 6a 75 6d 70 20  omparision jump 
2c685 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  */.      int add
2c686 72 4f 75 74 70 75 74 52 6f 77 3b 20 20 2f 2a 20  rOutputRow;  /* 
2c687 53 74 61 72 74 20 6f 66 20 73 75 62 72 6f 75 74  Start of subrout
2c688 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73  ine that outputs
2c689 20 61 20 72 65 73 75 6c 74 20 72 6f 77 20 2a 2f   a result row */
2c68a 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 4f 75  .      int regOu
2c68b 74 70 75 74 52 6f 77 3b 20 20 20 2f 2a 20 52 65  tputRow;   /* Re
2c68c 74 75 72 6e 20 61 64 64 72 65 73 73 20 72 65 67  turn address reg
2c68d 69 73 74 65 72 20 66 6f 72 20 6f 75 74 70 75 74  ister for output
2c68e 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20   subroutine */. 
2c68f 20 20 20 20 20 69 6e 74 20 61 64 64 72 53 65 74       int addrSet
2c690 41 62 6f 72 74 3b 20 20 20 2f 2a 20 53 65 74 20  Abort;   /* Set 
2c691 74 68 65 20 61 62 6f 72 74 20 66 6c 61 67 20 61  the abort flag a
2c692 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20  nd return */.   
2c693 20 20 20 69 6e 74 20 61 64 64 72 54 6f 70 4f 66     int addrTopOf
2c694 4c 6f 6f 70 3b 20 20 2f 2a 20 54 6f 70 20 6f 66  Loop;  /* Top of
2c695 20 74 68 65 20 69 6e 70 75 74 20 6c 6f 6f 70 20   the input loop 
2c696 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  */.      int add
2c697 72 53 6f 72 74 69 6e 67 49 64 78 3b 20 2f 2a 20  rSortingIdx; /* 
2c698 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  The OP_OpenEphem
2c699 65 72 61 6c 20 66 6f 72 20 74 68 65 20 73 6f 72  eral for the sor
2c69a 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20  ting index */.  
2c69b 20 20 20 20 69 6e 74 20 61 64 64 72 52 65 73 65      int addrRese
2c69c 74 3b 20 20 20 20 20 20 2f 2a 20 53 75 62 72 6f  t;      /* Subro
2c69d 75 74 69 6e 65 20 66 6f 72 20 72 65 73 65 74 74  utine for resett
2c69e 69 6e 67 20 74 68 65 20 61 63 63 75 6d 75 6c 61  ing the accumula
2c69f 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  tor */.      int
2c6a0 20 72 65 67 52 65 73 65 74 3b 20 20 20 20 20 20   regReset;      
2c6a1 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64 72 65   /* Return addre
2c6a2 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ss register for 
2c6a3 72 65 73 65 74 20 73 75 62 72 6f 75 74 69 6e 65  reset subroutine
2c6a4 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66   */..      /* If
2c6a5 20 74 68 65 72 65 20 69 73 20 61 20 47 52 4f 55   there is a GROU
2c6a6 50 20 42 59 20 63 6c 61 75 73 65 20 77 65 20 6d  P BY clause we m
2c6a7 69 67 68 74 20 6e 65 65 64 20 61 20 73 6f 72 74  ight need a sort
2c6a8 69 6e 67 20 69 6e 64 65 78 20 74 6f 0a 20 20 20  ing index to.   
2c6a9 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20     ** implement 
2c6aa 69 74 2e 20 20 41 6c 6c 6f 63 61 74 65 20 74 68  it.  Allocate th
2c6ab 61 74 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  at sorting index
2c6ac 20 6e 6f 77 2e 20 20 49 66 20 69 74 20 74 75 72   now.  If it tur
2c6ad 6e 73 20 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20  ns out.      ** 
2c6ae 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 6e  that we do not n
2c6af 65 65 64 20 69 74 20 61 66 74 65 72 20 61 6c 6c  eed it after all
2c6b0 2c 20 74 68 65 20 4f 70 65 6e 45 70 68 65 6d 65  , the OpenEpheme
2c6b1 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a  ral instruction.
2c6b2 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65        ** will be
2c6b3 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
2c6b4 61 20 4e 6f 6f 70 2e 20 20 0a 20 20 20 20 20 20  a Noop.  .      
2c6b5 2a 2f 0a 20 20 20 20 20 20 73 41 67 67 49 6e 66  */.      sAggInf
2c6b6 6f 2e 73 6f 72 74 69 6e 67 49 64 78 20 3d 20 70  o.sortingIdx = p
2c6b7 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
2c6b8 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20       pKeyInfo = 
2c6b9 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c  keyInfoFromExprL
2c6ba 69 73 74 28 70 50 61 72 73 65 2c 20 70 47 72 6f  ist(pParse, pGro
2c6bb 75 70 42 79 29 3b 0a 20 20 20 20 20 20 61 64 64  upBy);.      add
2c6bc 72 53 6f 72 74 69 6e 67 49 64 78 20 3d 20 73 71  rSortingIdx = sq
2c6bd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
2c6be 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
2c6bf 72 61 6c 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ral, .          
2c6c0 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67  sAggInfo.sorting
2c6c1 49 64 78 2c 20 73 41 67 67 49 6e 66 6f 2e 6e 53  Idx, sAggInfo.nS
2c6c2 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 2c 20 0a 20  ortingColumn, . 
2c6c3 20 20 20 20 20 20 20 20 20 30 2c 20 28 63 68 61           0, (cha
2c6c4 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f  r*)pKeyInfo, P4_
2c6c5 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
2c6c6 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 69 74  ;..      /* Init
2c6c7 69 61 6c 69 7a 65 20 6d 65 6d 6f 72 79 20 6c 6f  ialize memory lo
2c6c8 63 61 74 69 6f 6e 73 20 75 73 65 64 20 62 79 20  cations used by 
2c6c9 47 52 4f 55 50 20 42 59 20 61 67 67 72 65 67 61  GROUP BY aggrega
2c6ca 74 65 20 70 72 6f 63 65 73 73 69 6e 67 0a 20 20  te processing.  
2c6cb 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 55 73      */.      iUs
2c6cc 65 46 6c 61 67 20 3d 20 2b 2b 70 50 61 72 73 65  eFlag = ++pParse
2c6cd 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69 41  ->nMem;.      iA
2c6ce 62 6f 72 74 46 6c 61 67 20 3d 20 2b 2b 70 50 61  bortFlag = ++pPa
2c6cf 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20  rse->nMem;.     
2c6d0 20 72 65 67 4f 75 74 70 75 74 52 6f 77 20 3d 20   regOutputRow = 
2c6d1 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2c6d2 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75 74        addrOutput
2c6d3 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Row = sqlite3Vdb
2c6d4 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
2c6d5 20 20 20 20 20 72 65 67 52 65 73 65 74 20 3d 20       regReset = 
2c6d6 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2c6d7 20 20 20 20 20 20 61 64 64 72 52 65 73 65 74 20        addrReset 
2c6d8 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2c6d9 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
2c6da 20 69 41 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d   iAMem = pParse-
2c6db 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20  >nMem + 1;.     
2c6dc 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d   pParse->nMem +=
2c6dd 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72   pGroupBy->nExpr
2c6de 3b 0a 20 20 20 20 20 20 69 42 4d 65 6d 20 3d 20  ;.      iBMem = 
2c6df 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31  pParse->nMem + 1
2c6e0 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
2c6e1 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79  nMem += pGroupBy
2c6e2 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 73  ->nExpr;.      s
2c6e3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c6e4 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2c6e5 30 2c 20 69 41 62 6f 72 74 46 6c 61 67 29 3b 0a  0, iAbortFlag);.
2c6e6 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
2c6e7 74 28 28 76 2c 20 22 63 6c 65 61 72 20 61 62 6f  t((v, "clear abo
2c6e8 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20  rt flag"));.    
2c6e9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c6ea 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2c6eb 72 2c 20 30 2c 20 69 55 73 65 46 6c 61 67 29 3b  r, 0, iUseFlag);
2c6ec 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
2c6ed 6e 74 28 28 76 2c 20 22 69 6e 64 69 63 61 74 65  nt((v, "indicate
2c6ee 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 65 6d 70   accumulator emp
2c6ef 74 79 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ty"));..      /*
2c6f0 20 42 65 67 69 6e 20 61 20 6c 6f 6f 70 20 74 68   Begin a loop th
2c6f1 61 74 20 77 69 6c 6c 20 65 78 74 72 61 63 74 20  at will extract 
2c6f2 61 6c 6c 20 73 6f 75 72 63 65 20 72 6f 77 73 20  all source rows 
2c6f3 69 6e 20 47 52 4f 55 50 20 42 59 20 6f 72 64 65  in GROUP BY orde
2c6f4 72 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73  r..      ** This
2c6f5 20 6d 69 67 68 74 20 69 6e 76 6f 6c 76 65 20 74   might involve t
2c6f6 77 6f 20 73 65 70 61 72 61 74 65 20 6c 6f 6f 70  wo separate loop
2c6f7 73 20 77 69 74 68 20 61 6e 20 4f 50 5f 53 6f 72  s with an OP_Sor
2c6f8 74 20 69 6e 20 62 65 74 77 65 65 6e 2c 20 6f 72  t in between, or
2c6f9 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 67  .      ** it mig
2c6fa 68 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 6c  ht be a single l
2c6fb 6f 6f 70 20 74 68 61 74 20 75 73 65 73 20 61 6e  oop that uses an
2c6fc 20 69 6e 64 65 78 20 74 6f 20 65 78 74 72 61 63   index to extrac
2c6fd 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 20 20  t information.  
2c6fe 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 69      ** in the ri
2c6ff 67 68 74 20 6f 72 64 65 72 20 74 6f 20 62 65 67  ght order to beg
2c700 69 6e 20 77 69 74 68 2e 0a 20 20 20 20 20 20 2a  in with..      *
2c701 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
2c702 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c703 47 6f 73 75 62 2c 20 72 65 67 52 65 73 65 74 2c  Gosub, regReset,
2c704 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20   addrReset);.   
2c705 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69     pWInfo = sqli
2c706 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50  te3WhereBegin(pP
2c707 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
2c708 70 57 68 65 72 65 2c 20 26 70 47 72 6f 75 70 42  pWhere, &pGroupB
2c709 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  y, 0);.      if(
2c70a 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74   pWInfo==0 ) got
2c70b 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  o select_end;.  
2c70c 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42 79      if( pGroupBy
2c70d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
2c70e 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20  * The optimizer 
2c70f 69 73 20 61 62 6c 65 20 74 6f 20 64 65 6c 69 76  is able to deliv
2c710 65 72 20 72 6f 77 73 20 69 6e 20 67 72 6f 75 70  er rows in group
2c711 20 62 79 20 6f 72 64 65 72 20 73 6f 0a 20 20 20   by order so.   
2c712 20 20 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e 6f       ** we do no
2c713 74 20 68 61 76 65 20 74 6f 20 73 6f 72 74 2e 20  t have to sort. 
2c714 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65   The OP_OpenEphe
2c715 6d 65 72 61 6c 20 74 61 62 6c 65 20 77 69 6c 6c  meral table will
2c716 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 63   be.        ** c
2c717 61 6e 63 65 6c 6c 65 64 20 6c 61 74 65 72 20 62  ancelled later b
2c718 65 63 61 75 73 65 20 77 65 20 73 74 69 6c 6c 20  ecause we still 
2c719 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 65 20  need to use the 
2c71a 70 4b 65 79 49 6e 66 6f 0a 20 20 20 20 20 20 20  pKeyInfo.       
2c71b 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 47 72 6f   */.        pGro
2c71c 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70  upBy = p->pGroup
2c71d 42 79 3b 0a 20 20 20 20 20 20 20 20 67 72 6f 75  By;.        grou
2c71e 70 42 79 53 6f 72 74 20 3d 20 30 3b 0a 20 20 20  pBySort = 0;.   
2c71f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2c720 20 20 2f 2a 20 52 6f 77 73 20 61 72 65 20 63 6f    /* Rows are co
2c721 6d 69 6e 67 20 6f 75 74 20 69 6e 20 75 6e 64 65  ming out in unde
2c722 74 65 72 6d 69 6e 65 64 20 6f 72 64 65 72 2e 20  termined order. 
2c723 20 57 65 20 68 61 76 65 20 74 6f 20 70 75 73 68   We have to push
2c724 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 61 63 68  .        ** each
2c725 20 72 6f 77 20 69 6e 74 6f 20 61 20 73 6f 72 74   row into a sort
2c726 69 6e 67 20 69 6e 64 65 78 2c 20 74 65 72 6d 69  ing index, termi
2c727 6e 61 74 65 20 74 68 65 20 66 69 72 73 74 20 6c  nate the first l
2c728 6f 6f 70 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20  oop,.        ** 
2c729 74 68 65 6e 20 6c 6f 6f 70 20 6f 76 65 72 20 74  then loop over t
2c72a 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78  he sorting index
2c72b 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 74   in order to get
2c72c 20 74 68 65 20 6f 75 74 70 75 74 0a 20 20 20 20   the output.    
2c72d 20 20 20 20 2a 2a 20 69 6e 20 73 6f 72 74 65 64      ** in sorted
2c72e 20 6f 72 64 65 72 0a 20 20 20 20 20 20 20 20 2a   order.        *
2c72f 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 65  /.        int re
2c730 67 42 61 73 65 3b 0a 20 20 20 20 20 20 20 20 69  gBase;.        i
2c731 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 0a 20 20  nt regRecord;.  
2c732 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a        int nCol;.
2c733 20 20 20 20 20 20 20 20 69 6e 74 20 6e 47 72 6f          int nGro
2c734 75 70 42 79 3b 0a 0a 20 20 20 20 20 20 20 20 67  upBy;..        g
2c735 72 6f 75 70 42 79 53 6f 72 74 20 3d 20 31 3b 0a  roupBySort = 1;.
2c736 20 20 20 20 20 20 20 20 6e 47 72 6f 75 70 42 79          nGroupBy
2c737 20 3d 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78   = pGroupBy->nEx
2c738 70 72 3b 0a 20 20 20 20 20 20 20 20 6e 43 6f 6c  pr;.        nCol
2c739 20 3d 20 6e 47 72 6f 75 70 42 79 20 2b 20 31 3b   = nGroupBy + 1;
2c73a 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 6e 47 72  .        j = nGr
2c73b 6f 75 70 42 79 2b 31 3b 0a 20 20 20 20 20 20 20  oupBy+1;.       
2c73c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67 67   for(i=0; i<sAgg
2c73d 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Info.nColumn; i+
2c73e 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
2c73f 28 20 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b  ( sAggInfo.aCol[
2c740 69 5d 2e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  i].iSorterColumn
2c741 3e 3d 6a 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=j ){.         
2c742 20 20 20 6e 43 6f 6c 2b 2b 3b 0a 20 20 20 20 20     nCol++;.     
2c743 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20         j++;.    
2c744 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c745 7d 0a 20 20 20 20 20 20 20 20 72 65 67 42 61 73  }.        regBas
2c746 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  e = sqlite3GetTe
2c747 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
2c748 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73  nCol);.        s
2c749 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43  qlite3ExprCacheC
2c74a 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20  lear(pParse);.  
2c74b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2c74c 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50  rCodeExprList(pP
2c74d 61 72 73 65 2c 20 70 47 72 6f 75 70 42 79 2c 20  arse, pGroupBy, 
2c74e 72 65 67 42 61 73 65 2c 20 30 29 3b 0a 20 20 20  regBase, 0);.   
2c74f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c750 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71  AddOp2(v, OP_Seq
2c751 75 65 6e 63 65 2c 20 73 41 67 67 49 6e 66 6f 2e  uence, sAggInfo.
2c752 73 6f 72 74 69 6e 67 49 64 78 2c 72 65 67 42 61  sortingIdx,regBa
2c753 73 65 2b 6e 47 72 6f 75 70 42 79 29 3b 0a 20 20  se+nGroupBy);.  
2c754 20 20 20 20 20 20 6a 20 3d 20 6e 47 72 6f 75 70        j = nGroup
2c755 42 79 2b 31 3b 0a 20 20 20 20 20 20 20 20 66 6f  By+1;.        fo
2c756 72 28 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66  r(i=0; i<sAggInf
2c757 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  o.nColumn; i++){
2c758 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63  .          struc
2c759 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70  t AggInfo_col *p
2c75a 43 6f 6c 20 3d 20 26 73 41 67 67 49 6e 66 6f 2e  Col = &sAggInfo.
2c75b 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20  aCol[i];.       
2c75c 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53 6f     if( pCol->iSo
2c75d 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b  rterColumn>=j ){
2c75e 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
2c75f 20 72 31 20 3d 20 6a 20 2b 20 72 65 67 42 61 73   r1 = j + regBas
2c760 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  e;.            i
2c761 6e 74 20 72 32 3b 0a 0a 20 20 20 20 20 20 20 20  nt r2;..        
2c762 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33      r2 = sqlite3
2c763 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d  ExprCodeGetColum
2c764 6e 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  n(pParse, .     
2c765 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c766 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
2c767 70 54 61 62 2c 20 70 43 6f 6c 2d 3e 69 43 6f 6c  pTab, pCol->iCol
2c768 75 6d 6e 2c 20 70 43 6f 6c 2d 3e 69 54 61 62 6c  umn, pCol->iTabl
2c769 65 2c 20 72 31 2c 20 30 29 3b 0a 20 20 20 20 20  e, r1, 0);.     
2c76a 20 20 20 20 20 20 20 69 66 28 20 72 31 21 3d 72         if( r1!=r
2c76b 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  2 ){.           
2c76c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c76d 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79  dOp2(v, OP_SCopy
2c76e 2c 20 72 32 2c 20 72 31 29 3b 0a 20 20 20 20 20  , r2, r1);.     
2c76f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2c770 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20       j++;.      
2c771 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
2c772 20 20 20 20 20 20 20 20 72 65 67 52 65 63 6f 72          regRecor
2c773 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
2c774 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2c775 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2c776 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
2c777 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61  akeRecord, regBa
2c778 73 65 2c 20 6e 43 6f 6c 2c 20 72 65 67 52 65 63  se, nCol, regRec
2c779 6f 72 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ord);.        sq
2c77a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2c77b 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c  v, OP_IdxInsert,
2c77c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
2c77d 67 49 64 78 2c 20 72 65 67 52 65 63 6f 72 64 29  gIdx, regRecord)
2c77e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c77f 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2c780 70 50 61 72 73 65 2c 20 72 65 67 52 65 63 6f 72  pParse, regRecor
2c781 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  d);.        sqli
2c782 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
2c783 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42  nge(pParse, regB
2c784 61 73 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20  ase, nCol);.    
2c785 20 20 20 20 73 71 6c 69 74 65 33 57 68 65 72 65      sqlite3Where
2c786 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20  End(pWInfo);.   
2c787 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c788 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72  AddOp2(v, OP_Sor
2c789 74 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74  t, sAggInfo.sort
2c78a 69 6e 67 49 64 78 2c 20 61 64 64 72 45 6e 64 29  ingIdx, addrEnd)
2c78b 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f  ;.        VdbeCo
2c78c 6d 6d 65 6e 74 28 28 76 2c 20 22 47 52 4f 55 50  mment((v, "GROUP
2c78d 20 42 59 20 73 6f 72 74 22 29 29 3b 0a 20 20 20   BY sort"));.   
2c78e 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e 75 73       sAggInfo.us
2c78f 65 53 6f 72 74 69 6e 67 49 64 78 20 3d 20 31 3b  eSortingIdx = 1;
2c790 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c791 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28 70  ExprCacheClear(p
2c792 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a  Parse);.      }.
2c793 0a 20 20 20 20 20 20 2f 2a 20 45 76 61 6c 75 61  .      /* Evalua
2c794 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 47  te the current G
2c795 52 4f 55 50 20 42 59 20 74 65 72 6d 73 20 61 6e  ROUP BY terms an
2c796 64 20 73 74 6f 72 65 20 69 6e 20 62 30 2c 20 62  d store in b0, b
2c797 31 2c 20 62 32 2e 2e 2e 0a 20 20 20 20 20 20 2a  1, b2....      *
2c798 2a 20 28 62 30 20 69 73 20 6d 65 6d 6f 72 79 20  * (b0 is memory 
2c799 6c 6f 63 61 74 69 6f 6e 20 69 42 4d 65 6d 2b 30  location iBMem+0
2c79a 2c 20 62 31 20 69 73 20 69 42 4d 65 6d 2b 31 2c  , b1 is iBMem+1,
2c79b 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 20   and so forth). 
2c79c 20 20 20 20 20 2a 2a 20 54 68 65 6e 20 63 6f 6d       ** Then com
2c79d 70 61 72 65 20 74 68 65 20 63 75 72 72 65 6e 74  pare the current
2c79e 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73 20   GROUP BY terms 
2c79f 61 67 61 69 6e 73 74 20 74 68 65 20 47 52 4f 55  against the GROU
2c7a0 50 20 42 59 20 74 65 72 6d 73 0a 20 20 20 20 20  P BY terms.     
2c7a1 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70 72 65   ** from the pre
2c7a2 76 69 6f 75 73 20 72 6f 77 20 63 75 72 72 65 6e  vious row curren
2c7a3 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 61 30  tly stored in a0
2c7a4 2c 20 61 31 2c 20 61 32 2e 2e 2e 0a 20 20 20 20  , a1, a2....    
2c7a5 20 20 2a 2f 0a 20 20 20 20 20 20 61 64 64 72 54    */.      addrT
2c7a6 6f 70 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74  opOfLoop = sqlit
2c7a7 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2c7a8 72 28 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  r(v);.      sqli
2c7a9 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65 61  te3ExprCacheClea
2c7aa 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  r(pParse);.     
2c7ab 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72 6f   for(j=0; j<pGro
2c7ac 75 70 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b  upBy->nExpr; j++
2c7ad 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 67  ){.        if( g
2c7ae 72 6f 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 20  roupBySort ){.  
2c7af 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2c7b0 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2c7b1 43 6f 6c 75 6d 6e 2c 20 73 41 67 67 49 6e 66 6f  Column, sAggInfo
2c7b2 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20 6a 2c 20  .sortingIdx, j, 
2c7b3 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20 20 20  iBMem+j);.      
2c7b4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2c7b5 20 20 20 73 41 67 67 49 6e 66 6f 2e 64 69 72 65     sAggInfo.dire
2c7b6 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 20 20  ctMode = 1;.    
2c7b7 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2c7b8 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 47  rCode(pParse, pG
2c7b9 72 6f 75 70 42 79 2d 3e 61 5b 6a 5d 2e 70 45 78  roupBy->a[j].pEx
2c7ba 70 72 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20  pr, iBMem+j);.  
2c7bb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2c7bc 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c7bd 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f  eAddOp4(v, OP_Co
2c7be 6d 70 61 72 65 2c 20 69 41 4d 65 6d 2c 20 69 42  mpare, iAMem, iB
2c7bf 4d 65 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e 6e  Mem, pGroupBy->n
2c7c0 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20  Expr,.          
2c7c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c7c2 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
2c7c3 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20   P4_KEYINFO);.  
2c7c4 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
2c7c5 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
2c7c6 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  v);.      sqlite
2c7c7 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2c7c8 50 5f 4a 75 6d 70 2c 20 6a 31 2b 31 2c 20 30 2c  P_Jump, j1+1, 0,
2c7c9 20 6a 31 2b 31 29 3b 0a 0a 20 20 20 20 20 20 2f   j1+1);..      /
2c7ca 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2c7cb 74 68 61 74 20 72 75 6e 73 20 77 68 65 6e 65 76  that runs whenev
2c7cc 65 72 20 74 68 65 20 47 52 4f 55 50 20 42 59 20  er the GROUP BY 
2c7cd 63 68 61 6e 67 65 73 2e 0a 20 20 20 20 20 20 2a  changes..      *
2c7ce 2a 20 43 68 61 6e 67 65 73 20 69 6e 20 74 68 65  * Changes in the
2c7cf 20 47 52 4f 55 50 20 42 59 20 61 72 65 20 64 65   GROUP BY are de
2c7d0 74 65 63 74 65 64 20 62 79 20 74 68 65 20 70 72  tected by the pr
2c7d1 65 76 69 6f 75 73 20 63 6f 64 65 0a 20 20 20 20  evious code.    
2c7d2 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20    ** block.  If 
2c7d3 74 68 65 72 65 20 77 65 72 65 20 6e 6f 20 63 68  there were no ch
2c7d4 61 6e 67 65 73 2c 20 74 68 69 73 20 62 6c 6f 63  anges, this bloc
2c7d5 6b 20 69 73 20 73 6b 69 70 70 65 64 2e 0a 20 20  k is skipped..  
2c7d6 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
2c7d7 54 68 69 73 20 63 6f 64 65 20 63 6f 70 69 65 73  This code copies
2c7d8 20 63 75 72 72 65 6e 74 20 67 72 6f 75 70 20 62   current group b
2c7d9 79 20 74 65 72 6d 73 20 69 6e 20 62 30 2c 62 31  y terms in b0,b1
2c7da 2c 62 32 2c 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a  ,b2,....      **
2c7db 20 6f 76 65 72 20 74 6f 20 61 30 2c 61 31 2c 61   over to a0,a1,a
2c7dc 32 2e 20 20 49 74 20 74 68 65 6e 20 63 61 6c 6c  2.  It then call
2c7dd 73 20 74 68 65 20 6f 75 74 70 75 74 20 73 75 62  s the output sub
2c7de 72 6f 75 74 69 6e 65 0a 20 20 20 20 20 20 2a 2a  routine.      **
2c7df 20 61 6e 64 20 72 65 73 65 74 73 20 74 68 65 20   and resets the 
2c7e0 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75  aggregate accumu
2c7e1 6c 61 74 6f 72 20 72 65 67 69 73 74 65 72 73 20  lator registers 
2c7e2 69 6e 20 70 72 65 70 61 72 61 74 69 6f 6e 0a 20  in preparation. 
2c7e3 20 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20       ** for the 
2c7e4 6e 65 78 74 20 47 52 4f 55 50 20 42 59 20 62 61  next GROUP BY ba
2c7e5 74 63 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  tch..      */.  
2c7e6 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2c7e7 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20  odeMove(pParse, 
2c7e8 69 42 4d 65 6d 2c 20 69 41 4d 65 6d 2c 20 70 47  iBMem, iAMem, pG
2c7e9 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 29 3b 0a  roupBy->nExpr);.
2c7ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c7eb 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2c7ec 73 75 62 2c 20 72 65 67 4f 75 74 70 75 74 52 6f  sub, regOutputRo
2c7ed 77 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  w, addrOutputRow
2c7ee 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2c7ef 6d 65 6e 74 28 28 76 2c 20 22 6f 75 74 70 75 74  ment((v, "output
2c7f0 20 6f 6e 65 20 72 6f 77 22 29 29 3b 0a 20 20 20   one row"));.   
2c7f1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c7f2 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73  dOp2(v, OP_IfPos
2c7f3 2c 20 69 41 62 6f 72 74 46 6c 61 67 2c 20 61 64  , iAbortFlag, ad
2c7f4 64 72 45 6e 64 29 3b 0a 20 20 20 20 20 20 56 64  drEnd);.      Vd
2c7f5 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63  beComment((v, "c
2c7f6 68 65 63 6b 20 61 62 6f 72 74 20 66 6c 61 67 22  heck abort flag"
2c7f7 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ));.      sqlite
2c7f8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c7f9 50 5f 47 6f 73 75 62 2c 20 72 65 67 52 65 73 65  P_Gosub, regRese
2c7fa 74 2c 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20  t, addrReset);. 
2c7fb 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2c7fc 28 28 76 2c 20 22 72 65 73 65 74 20 61 63 63 75  ((v, "reset accu
2c7fd 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20  mulator"));..   
2c7fe 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
2c7ff 20 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d   aggregate accum
2c800 75 6c 61 74 6f 72 73 20 62 61 73 65 64 20 6f 6e  ulators based on
2c801 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a   the content of.
2c802 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72        ** the cur
2c803 72 65 6e 74 20 72 6f 77 0a 20 20 20 20 20 20 2a  rent row.      *
2c804 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
2c805 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
2c806 31 29 3b 0a 20 20 20 20 20 20 75 70 64 61 74 65  1);.      update
2c807 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72  Accumulator(pPar
2c808 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a  se, &sAggInfo);.
2c809 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c80a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
2c80b 74 65 67 65 72 2c 20 31 2c 20 69 55 73 65 46 6c  teger, 1, iUseFl
2c80c 61 67 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  ag);.      VdbeC
2c80d 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 69 6e 64 69  omment((v, "indi
2c80e 63 61 74 65 20 64 61 74 61 20 69 6e 20 61 63 63  cate data in acc
2c80f 75 6d 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20  umulator"));..  
2c810 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68      /* End of th
2c811 65 20 6c 6f 6f 70 0a 20 20 20 20 20 20 2a 2f 0a  e loop.      */.
2c812 20 20 20 20 20 20 69 66 28 20 67 72 6f 75 70 42        if( groupB
2c813 79 53 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20  ySort ){.       
2c814 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c815 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73  p2(v, OP_Next, s
2c816 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49  AggInfo.sortingI
2c817 64 78 2c 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f  dx, addrTopOfLoo
2c818 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  p);.      }else{
2c819 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c81a 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29  WhereEnd(pWInfo)
2c81b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c81c 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f  3VdbeChangeToNoo
2c81d 70 28 76 2c 20 61 64 64 72 53 6f 72 74 69 6e 67  p(v, addrSorting
2c81e 49 64 78 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d  Idx, 1);.      }
2c81f 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75  ..      /* Outpu
2c820 74 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 20  t the final row 
2c821 6f 66 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20  of result.      
2c822 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
2c823 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2c824 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 70 75  _Gosub, regOutpu
2c825 74 52 6f 77 2c 20 61 64 64 72 4f 75 74 70 75 74  tRow, addrOutput
2c826 52 6f 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65  Row);.      Vdbe
2c827 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 6f 75 74  Comment((v, "out
2c828 70 75 74 20 66 69 6e 61 6c 20 72 6f 77 22 29 29  put final row"))
2c829 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70  ;..      /* Jump
2c82a 20 6f 76 65 72 20 74 68 65 20 73 75 62 72 6f 75   over the subrou
2c82b 74 69 6e 65 73 0a 20 20 20 20 20 20 2a 2f 0a 20  tines.      */. 
2c82c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c82d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
2c82e 6f 2c 20 30 2c 20 61 64 64 72 45 6e 64 29 3b 0a  o, 0, addrEnd);.
2c82f 0a 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  .      /* Genera
2c830 74 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20  te a subroutine 
2c831 74 68 61 74 20 6f 75 74 70 75 74 73 20 61 20 73  that outputs a s
2c832 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65  ingle row of the
2c833 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20 2a 2a   result.      **
2c834 20 73 65 74 2e 20 20 54 68 69 73 20 73 75 62 72   set.  This subr
2c835 6f 75 74 69 6e 65 20 66 69 72 73 74 20 6c 6f 6f  outine first loo
2c836 6b 73 20 61 74 20 74 68 65 20 69 55 73 65 46 6c  ks at the iUseFl
2c837 61 67 2e 20 20 49 66 20 69 55 73 65 46 6c 61 67  ag.  If iUseFlag
2c838 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 6c 65 73  .      ** is les
2c839 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  s than or equal 
2c83a 74 6f 20 7a 65 72 6f 2c 20 74 68 65 20 73 75 62  to zero, the sub
2c83b 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
2c83c 6f 70 2e 20 20 49 66 0a 20 20 20 20 20 20 2a 2a  op.  If.      **
2c83d 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20   the processing 
2c83e 63 61 6c 6c 73 20 66 6f 72 20 74 68 65 20 71 75  calls for the qu
2c83f 65 72 79 20 74 6f 20 61 62 6f 72 74 2c 20 74 68  ery to abort, th
2c840 69 73 20 73 75 62 72 6f 75 74 69 6e 65 0a 20 20  is subroutine.  
2c841 20 20 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74      ** increment
2c842 73 20 74 68 65 20 69 41 62 6f 72 74 46 6c 61 67  s the iAbortFlag
2c843 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
2c844 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
2c845 67 20 69 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 72  g in.      ** or
2c846 64 65 72 20 74 6f 20 73 69 67 6e 61 6c 20 74 68  der to signal th
2c847 65 20 63 61 6c 6c 65 72 20 74 6f 20 61 62 6f 72  e caller to abor
2c848 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  t..      */.    
2c849 20 20 61 64 64 72 53 65 74 41 62 6f 72 74 20 3d    addrSetAbort =
2c84a 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
2c84b 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
2c84c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2c84d 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2c84e 72 2c 20 31 2c 20 69 41 62 6f 72 74 46 6c 61 67  r, 1, iAbortFlag
2c84f 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2c850 6d 65 6e 74 28 28 76 2c 20 22 73 65 74 20 61 62  ment((v, "set ab
2c851 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20  ort flag"));.   
2c852 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c853 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72  dOp1(v, OP_Retur
2c854 6e 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 29  n, regOutputRow)
2c855 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2c856 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2c857 76 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  v, addrOutputRow
2c858 29 3b 0a 20 20 20 20 20 20 61 64 64 72 4f 75 74  );.      addrOut
2c859 70 75 74 52 6f 77 20 3d 20 73 71 6c 69 74 65 33  putRow = sqlite3
2c85a 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
2c85b 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  v);.      sqlite
2c85c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2c85d 50 5f 49 66 50 6f 73 2c 20 69 55 73 65 46 6c 61  P_IfPos, iUseFla
2c85e 67 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  g, addrOutputRow
2c85f 2b 32 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  +2);.      VdbeC
2c860 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 47 72 6f 75  omment((v, "Grou
2c861 70 62 79 20 72 65 73 75 6c 74 20 67 65 6e 65 72  pby result gener
2c862 61 74 6f 72 20 65 6e 74 72 79 20 70 6f 69 6e 74  ator entry point
2c863 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  "));.      sqlit
2c864 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2c865 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75  OP_Return, regOu
2c866 74 70 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20  tputRow);.      
2c867 66 69 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74  finalizeAggFunct
2c868 69 6f 6e 73 28 70 50 61 72 73 65 2c 20 26 73 41  ions(pParse, &sA
2c869 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73  ggInfo);.      s
2c86a 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
2c86b 65 28 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e  e(pParse, pHavin
2c86c 67 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77  g, addrOutputRow
2c86d 2b 31 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  +1, SQLITE_JUMPI
2c86e 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 65  FNULL);.      se
2c86f 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50  lectInnerLoop(pP
2c870 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69  arse, p, p->pELi
2c871 73 74 2c 20 30 2c 20 30 2c 20 70 4f 72 64 65 72  st, 0, 0, pOrder
2c872 42 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  By,.            
2c873 20 20 20 20 20 20 20 20 20 20 64 69 73 74 69 6e            distin
2c874 63 74 2c 20 70 44 65 73 74 2c 0a 20 20 20 20 20  ct, pDest,.     
2c875 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c876 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 31   addrOutputRow+1
2c877 2c 20 61 64 64 72 53 65 74 41 62 6f 72 74 29 3b  , addrSetAbort);
2c878 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2c879 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52  beAddOp1(v, OP_R
2c87a 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70 75 74  eturn, regOutput
2c87b 52 6f 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65  Row);.      Vdbe
2c87c 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64  Comment((v, "end
2c87d 20 67 72 6f 75 70 62 79 20 72 65 73 75 6c 74 20   groupby result 
2c87e 67 65 6e 65 72 61 74 6f 72 22 29 29 3b 0a 0a 20  generator"));.. 
2c87f 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2c880 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68   a subroutine th
2c881 61 74 20 77 69 6c 6c 20 72 65 73 65 74 20 74 68  at will reset th
2c882 65 20 67 72 6f 75 70 2d 62 79 20 61 63 63 75 6d  e group-by accum
2c883 75 6c 61 74 6f 72 0a 20 20 20 20 20 20 2a 2f 0a  ulator.      */.
2c884 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c885 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2c886 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20   addrReset);.   
2c887 20 20 20 72 65 73 65 74 41 63 63 75 6d 75 6c 61     resetAccumula
2c888 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41 67  tor(pParse, &sAg
2c889 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71  gInfo);.      sq
2c88a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2c88b 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65  v, OP_Return, re
2c88c 67 52 65 73 65 74 29 3b 0a 20 20 20 20 20 0a 20  gReset);.     . 
2c88d 20 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 47     } /* endif pG
2c88e 72 6f 75 70 42 79 2e 20 20 42 65 67 69 6e 20 61  roupBy.  Begin a
2c88f 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73  ggregate queries
2c890 20 77 69 74 68 6f 75 74 20 47 52 4f 55 50 20 42   without GROUP B
2c891 59 3a 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 7b  Y: */.    else {
2c892 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20  .      ExprList 
2c893 2a 70 44 65 6c 20 3d 20 30 3b 0a 23 69 66 6e 64  *pDel = 0;.#ifnd
2c894 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
2c895 54 52 45 45 43 4f 55 4e 54 0a 20 20 20 20 20 20  TREECOUNT.      
2c896 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20  Table *pTab;.   
2c897 20 20 20 69 66 28 20 28 70 54 61 62 20 3d 20 69     if( (pTab = i
2c898 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 70 2c 20  sSimpleCount(p, 
2c899 26 73 41 67 67 49 6e 66 6f 29 29 21 3d 30 20 29  &sAggInfo))!=0 )
2c89a 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  {.        /* If 
2c89b 69 73 53 69 6d 70 6c 65 43 6f 75 6e 74 28 29 20  isSimpleCount() 
2c89c 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65  returns a pointe
2c89d 72 20 74 6f 20 61 20 54 61 62 6c 65 20 73 74 72  r to a Table str
2c89e 75 63 74 75 72 65 2c 20 74 68 65 6e 0a 20 20 20  ucture, then.   
2c89f 20 20 20 20 20 2a 2a 20 74 68 65 20 53 51 4c 20       ** the SQL 
2c8a0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20  statement is of 
2c8a1 74 68 65 20 66 6f 72 6d 3a 0a 20 20 20 20 20 20  the form:.      
2c8a2 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
2c8a3 20 20 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a    SELECT count(*
2c8a4 29 20 46 52 4f 4d 20 3c 74 62 6c 3e 0a 20 20 20  ) FROM <tbl>.   
2c8a5 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
2c8a6 2a 2a 20 77 68 65 72 65 20 74 68 65 20 54 61 62  ** where the Tab
2c8a7 6c 65 20 73 74 72 75 63 74 75 72 65 20 72 65 74  le structure ret
2c8a8 75 72 6e 65 64 20 72 65 70 72 65 73 65 6e 74 73  urned represents
2c8a9 20 74 61 62 6c 65 20 3c 74 62 6c 3e 2e 0a 20 20   table <tbl>..  
2c8aa 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
2c8ab 20 2a 2a 20 54 68 69 73 20 73 74 61 74 65 6d 65   ** This stateme
2c8ac 6e 74 20 69 73 20 73 6f 20 63 6f 6d 6d 6f 6e 20  nt is so common 
2c8ad 74 68 61 74 20 69 74 20 69 73 20 6f 70 74 69 6d  that it is optim
2c8ae 69 7a 65 64 20 73 70 65 63 69 61 6c 6c 79 2e 20  ized specially. 
2c8af 54 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f  The.        ** O
2c8b0 50 5f 43 6f 75 6e 74 20 69 6e 73 74 72 75 63 74  P_Count instruct
2c8b1 69 6f 6e 20 69 73 20 65 78 65 63 75 74 65 64 20  ion is executed 
2c8b2 65 69 74 68 65 72 20 6f 6e 20 74 68 65 20 69 6e  either on the in
2c8b3 74 6b 65 79 20 74 61 62 6c 65 20 74 68 61 74 0a  tkey table that.
2c8b4 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61          ** conta
2c8b5 69 6e 73 20 74 68 65 20 64 61 74 61 20 66 6f 72  ins the data for
2c8b6 20 74 61 62 6c 65 20 3c 74 62 6c 3e 20 6f 72 20   table <tbl> or 
2c8b7 6f 6e 20 6f 6e 65 20 6f 66 20 69 74 73 20 69 6e  on one of its in
2c8b8 64 65 78 65 73 2e 20 49 74 0a 20 20 20 20 20 20  dexes. It.      
2c8b9 20 20 2a 2a 20 69 73 20 62 65 74 74 65 72 20 74    ** is better t
2c8ba 6f 20 65 78 65 63 75 74 65 20 74 68 65 20 6f 70  o execute the op
2c8bb 20 6f 6e 20 61 6e 20 69 6e 64 65 78 2c 20 61 73   on an index, as
2c8bc 20 69 6e 64 65 78 65 73 20 61 72 65 20 61 6c 6d   indexes are alm
2c8bd 6f 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  ost.        ** a
2c8be 6c 77 61 79 73 20 73 70 72 65 61 64 20 61 63 72  lways spread acr
2c8bf 6f 73 73 20 6c 65 73 73 20 70 61 67 65 73 20 74  oss less pages t
2c8c0 68 61 6e 20 74 68 65 69 72 20 63 6f 72 72 65 73  han their corres
2c8c1 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 73 2e 0a  ponding tables..
2c8c2 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2c8c3 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 62     const int iDb
2c8c4 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
2c8c5 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e  ToIndex(pParse->
2c8c6 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
2c8c7 61 29 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73  a);.        cons
2c8c8 74 20 69 6e 74 20 69 43 73 72 20 3d 20 70 50 61  t int iCsr = pPa
2c8c9 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20  rse->nTab++;    
2c8ca 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 73 63   /* Cursor to sc
2c8cb 61 6e 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 20  an b-tree */.   
2c8cc 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
2c8cd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c8ce 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
2c8cf 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
2c8d0 2f 0a 20 20 20 20 20 20 20 20 4b 65 79 49 6e 66  /.        KeyInf
2c8d1 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b  o *pKeyInfo = 0;
2c8d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c8d3 2a 20 4b 65 79 69 6e 66 6f 20 66 6f 72 20 73 63  * Keyinfo for sc
2c8d4 61 6e 6e 65 64 20 69 6e 64 65 78 20 2a 2f 0a 20  anned index */. 
2c8d5 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 42         Index *pB
2c8d6 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20  est = 0;        
2c8d7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
2c8d8 65 73 74 20 69 6e 64 65 78 20 66 6f 75 6e 64 20  est index found 
2c8d9 73 6f 20 66 61 72 20 2a 2f 0a 20 20 20 20 20 20  so far */.      
2c8da 20 20 69 6e 74 20 69 52 6f 6f 74 20 3d 20 70 54    int iRoot = pT
2c8db 61 62 2d 3e 74 6e 75 6d 3b 20 20 20 20 20 20 20  ab->tnum;       
2c8dc 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70         /* Root p
2c8dd 61 67 65 20 6f 66 20 73 63 61 6e 6e 65 64 20 62  age of scanned b
2c8de 2d 74 72 65 65 20 2a 2f 0a 0a 20 20 20 20 20 20  -tree */..      
2c8df 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72    sqlite3CodeVer
2c8e0 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65  ifySchema(pParse
2c8e1 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20  , iDb);.        
2c8e2 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
2c8e3 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
2c8e4 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61  ab->tnum, 0, pTa
2c8e5 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20  b->zName);..    
2c8e6 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f      /* Search fo
2c8e7 72 20 74 68 65 20 69 6e 64 65 78 20 74 68 61 74  r the index that
2c8e8 20 68 61 73 20 74 68 65 20 6c 65 61 73 74 20 61   has the least a
2c8e9 6d 6f 75 6e 74 20 6f 66 20 63 6f 6c 75 6d 6e 73  mount of columns
2c8ea 2e 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a 20  . If.        ** 
2c8eb 74 68 65 72 65 20 69 73 20 73 75 63 68 20 61 6e  there is such an
2c8ec 20 69 6e 64 65 78 2c 20 61 6e 64 20 69 74 20 68   index, and it h
2c8ed 61 73 20 6c 65 73 73 20 63 6f 6c 75 6d 6e 73 20  as less columns 
2c8ee 74 68 61 6e 20 74 68 65 20 74 61 62 6c 65 0a 20  than the table. 
2c8ef 20 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 2c 20         ** does, 
2c8f0 74 68 65 6e 20 77 65 20 63 61 6e 20 61 73 73 75  then we can assu
2c8f1 6d 65 20 74 68 61 74 20 69 74 20 63 6f 6e 73 75  me that it consu
2c8f2 6d 65 73 20 6c 65 73 73 20 73 70 61 63 65 20 6f  mes less space o
2c8f3 6e 20 64 69 73 6b 20 61 6e 64 0a 20 20 20 20 20  n disk and.     
2c8f4 20 20 20 2a 2a 20 77 69 6c 6c 20 74 68 65 72 65     ** will there
2c8f5 66 6f 72 65 20 62 65 20 63 68 65 61 70 65 72 20  fore be cheaper 
2c8f6 74 6f 20 73 63 61 6e 20 74 6f 20 64 65 74 65 72  to scan to deter
2c8f7 6d 69 6e 65 20 74 68 65 20 71 75 65 72 79 20 72  mine the query r
2c8f8 65 73 75 6c 74 2e 0a 20 20 20 20 20 20 20 20 2a  esult..        *
2c8f9 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 73  * In this case s
2c8fa 65 74 20 69 52 6f 6f 74 20 74 6f 20 74 68 65 20  et iRoot to the 
2c8fb 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
2c8fc 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 2d   of the index b-
2c8fd 74 72 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  tree.        ** 
2c8fe 61 6e 64 20 70 4b 65 79 49 6e 66 6f 20 74 6f 20  and pKeyInfo to 
2c8ff 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  the KeyInfo stru
2c900 63 74 75 72 65 20 72 65 71 75 69 72 65 64 20 74  cture required t
2c901 6f 20 6e 61 76 69 67 61 74 65 20 74 68 65 0a 20  o navigate the. 
2c902 20 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 78 2e         ** index.
2c903 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
2c904 20 20 20 20 2a 2a 20 49 6e 20 70 72 61 63 74 69      ** In practi
2c905 63 65 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  ce the KeyInfo s
2c906 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20 6e 6f  tructure will no
2c907 74 20 62 65 20 75 73 65 64 2e 20 49 74 20 69 73  t be used. It is
2c908 20 6f 6e 6c 79 20 0a 20 20 20 20 20 20 20 20 2a   only .        *
2c909 2a 20 70 61 73 73 65 64 20 74 6f 20 6b 65 65 70  * passed to keep
2c90a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 68 61 70   OP_OpenRead hap
2c90b 70 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  py..        */. 
2c90c 20 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d         for(pIdx=
2c90d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2c90e 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2c90f 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20  Next){.         
2c910 20 69 66 28 20 21 70 42 65 73 74 20 7c 7c 20 70   if( !pBest || p
2c911 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3c 70 42 65  Idx->nColumn<pBe
2c912 73 74 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  st->nColumn ){. 
2c913 20 20 20 20 20 20 20 20 20 20 20 70 42 65 73 74             pBest
2c914 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 20 20 20   = pIdx;.       
2c915 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
2c916 20 20 20 20 20 20 20 69 66 28 20 70 42 65 73 74         if( pBest
2c917 20 26 26 20 70 42 65 73 74 2d 3e 6e 43 6f 6c 75   && pBest->nColu
2c918 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b  mn<pTab->nCol ){
2c919 0a 20 20 20 20 20 20 20 20 20 20 69 52 6f 6f 74  .          iRoot
2c91a 20 3d 20 70 42 65 73 74 2d 3e 74 6e 75 6d 3b 0a   = pBest->tnum;.
2c91b 20 20 20 20 20 20 20 20 20 20 70 4b 65 79 49 6e            pKeyIn
2c91c 66 6f 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  fo = sqlite3Inde
2c91d 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
2c91e 20 70 42 65 73 74 29 3b 0a 20 20 20 20 20 20 20   pBest);.       
2c91f 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f   }..        /* O
2c920 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20  pen a read-only 
2c921 63 75 72 73 6f 72 2c 20 65 78 65 63 75 74 65 20  cursor, execute 
2c922 74 68 65 20 4f 50 5f 43 6f 75 6e 74 2c 20 63 6c  the OP_Count, cl
2c923 6f 73 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20  ose the cursor. 
2c924 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
2c925 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2c926 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 43 73  OP_OpenRead, iCs
2c927 72 2c 20 69 52 6f 6f 74 2c 20 69 44 62 29 3b 0a  r, iRoot, iDb);.
2c928 20 20 20 20 20 20 20 20 69 66 28 20 70 4b 65 79          if( pKey
2c929 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20  Info ){.        
2c92a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
2c92b 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68  ngeP4(v, -1, (ch
2c92c 61 72 20 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50  ar *)pKeyInfo, P
2c92d 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
2c92e 46 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  F);.        }.  
2c92f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c930 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f  eAddOp2(v, OP_Co
2c931 75 6e 74 2c 20 69 43 73 72 2c 20 73 41 67 67 49  unt, iCsr, sAggI
2c932 6e 66 6f 2e 61 46 75 6e 63 5b 30 5d 2e 69 4d 65  nfo.aFunc[0].iMe
2c933 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  m);.        sqli
2c934 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2c935 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 73 72 29   OP_Close, iCsr)
2c936 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65  ;.      }else.#e
2c937 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2c938 4d 49 54 5f 42 54 52 45 45 43 4f 55 4e 54 20 2a  MIT_BTREECOUNT *
2c939 2f 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  /.      {.      
2c93a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68    /* Check if th
2c93b 65 20 71 75 65 72 79 20 69 73 20 6f 66 20 6f 6e  e query is of on
2c93c 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
2c93d 6e 67 20 66 6f 72 6d 73 3a 0a 20 20 20 20 20 20  ng forms:.      
2c93e 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
2c93f 20 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 20    SELECT min(x) 
2c940 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20 20  FROM ....       
2c941 20 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d 61 78   **   SELECT max
2c942 28 78 29 20 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20  (x) FROM ....   
2c943 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
2c944 2a 2a 20 49 66 20 69 74 20 69 73 2c 20 74 68 65  ** If it is, the
2c945 6e 20 61 73 6b 20 74 68 65 20 63 6f 64 65 20 69  n ask the code i
2c946 6e 20 77 68 65 72 65 2e 63 20 74 6f 20 61 74 74  n where.c to att
2c947 65 6d 70 74 20 74 6f 20 73 6f 72 74 20 72 65 73  empt to sort res
2c948 75 6c 74 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  ults.        ** 
2c949 61 73 20 69 66 20 74 68 65 72 65 20 77 61 73 20  as if there was 
2c94a 61 6e 20 22 4f 52 44 45 52 20 4f 4e 20 78 22 20  an "ORDER ON x" 
2c94b 6f 72 20 22 4f 52 44 45 52 20 4f 4e 20 78 20 44  or "ORDER ON x D
2c94c 45 53 43 22 20 63 6c 61 75 73 65 2e 20 0a 20 20  ESC" clause. .  
2c94d 20 20 20 20 20 20 2a 2a 20 49 66 20 77 68 65 72        ** If wher
2c94e 65 2e 63 20 69 73 20 61 62 6c 65 20 74 6f 20 70  e.c is able to p
2c94f 72 6f 64 75 63 65 20 72 65 73 75 6c 74 73 20 73  roduce results s
2c950 6f 72 74 65 64 20 69 6e 20 74 68 69 73 20 6f 72  orted in this or
2c951 64 65 72 2c 20 74 68 65 6e 0a 20 20 20 20 20 20  der, then.      
2c952 20 20 2a 2a 20 61 64 64 20 76 64 62 65 20 63 6f    ** add vdbe co
2c953 64 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20  de to break out 
2c954 6f 66 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e  of the processin
2c955 67 20 6c 6f 6f 70 20 61 66 74 65 72 20 74 68 65  g loop after the
2c956 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 72   .        ** fir
2c957 73 74 20 69 74 65 72 61 74 69 6f 6e 20 28 73 69  st iteration (si
2c958 6e 63 65 20 74 68 65 20 66 69 72 73 74 20 69 74  nce the first it
2c959 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  eration of the l
2c95a 6f 6f 70 20 69 73 20 0a 20 20 20 20 20 20 20 20  oop is .        
2c95b 2a 2a 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  ** guaranteed to
2c95c 20 6f 70 65 72 61 74 65 20 6f 6e 20 74 68 65 20   operate on the 
2c95d 72 6f 77 20 77 69 74 68 20 74 68 65 20 6d 69 6e  row with the min
2c95e 69 6d 75 6d 20 6f 72 20 6d 61 78 69 6d 75 6d 20  imum or maximum 
2c95f 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c 75  .        ** valu
2c960 65 20 6f 66 20 78 2c 20 74 68 65 20 6f 6e 6c 79  e of x, the only
2c961 20 72 6f 77 20 72 65 71 75 69 72 65 64 29 2e 0a   row required)..
2c962 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
2c963 20 20 20 2a 2a 20 41 20 73 70 65 63 69 61 6c 20     ** A special 
2c964 66 6c 61 67 20 6d 75 73 74 20 62 65 20 70 61 73  flag must be pas
2c965 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 57 68  sed to sqlite3Wh
2c966 65 72 65 42 65 67 69 6e 28 29 20 74 6f 20 73 6c  ereBegin() to sl
2c967 69 67 68 74 6c 79 0a 20 20 20 20 20 20 20 20 2a  ightly.        *
2c968 2a 20 6d 6f 64 69 66 79 20 62 65 68 61 76 69 6f  * modify behavio
2c969 75 72 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20  ur as follows:. 
2c96a 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
2c96b 20 20 2a 2a 20 20 20 2b 20 49 66 20 74 68 65 20    **   + If the 
2c96c 71 75 65 72 79 20 69 73 20 61 20 22 53 45 4c 45  query is a "SELE
2c96d 43 54 20 6d 69 6e 28 78 29 22 2c 20 74 68 65 6e  CT min(x)", then
2c96e 20 74 68 65 20 6c 6f 6f 70 20 63 6f 64 65 64 20   the loop coded 
2c96f 62 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  by.        **   
2c970 20 20 77 68 65 72 65 2e 63 20 73 68 6f 75 6c 64    where.c should
2c971 20 6e 6f 74 20 69 74 65 72 61 74 65 20 6f 76 65   not iterate ove
2c972 72 20 61 6e 79 20 76 61 6c 75 65 73 20 77 69 74  r any values wit
2c973 68 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 0a 20  h a NULL value. 
2c974 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20 66 6f         **     fo
2c975 72 20 78 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  r x..        **.
2c976 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2b 20 54          **   + T
2c977 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 63 6f 64  he optimizer cod
2c978 65 20 69 6e 20 77 68 65 72 65 2e 63 20 28 74 68  e in where.c (th
2c979 65 20 74 68 69 6e 67 20 74 68 61 74 20 64 65 63  e thing that dec
2c97a 69 64 65 73 20 77 68 69 63 68 0a 20 20 20 20 20  ides which.     
2c97b 20 20 20 2a 2a 20 20 20 20 20 69 6e 64 65 78 20     **     index 
2c97c 6f 72 20 69 6e 64 69 63 65 73 20 74 6f 20 75 73  or indices to us
2c97d 65 29 20 73 68 6f 75 6c 64 20 70 6c 61 63 65 20  e) should place 
2c97e 61 20 64 69 66 66 65 72 65 6e 74 20 70 72 69 6f  a different prio
2c97f 72 69 74 79 20 6f 6e 20 0a 20 20 20 20 20 20 20  rity on .       
2c980 20 2a 2a 20 20 20 20 20 73 61 74 69 73 66 79 69   **     satisfyi
2c981 6e 67 20 74 68 65 20 27 4f 52 44 45 52 20 42 59  ng the 'ORDER BY
2c982 27 20 63 6c 61 75 73 65 20 74 68 61 6e 20 69 74  ' clause than it
2c983 20 64 6f 65 73 20 69 6e 20 6f 74 68 65 72 20 63   does in other c
2c984 61 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a  ases..        **
2c985 20 20 20 20 20 52 65 66 65 72 20 74 6f 20 63 6f       Refer to co
2c986 64 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 20  de and comments 
2c987 69 6e 20 77 68 65 72 65 2e 63 20 66 6f 72 20 64  in where.c for d
2c988 65 74 61 69 6c 73 2e 0a 20 20 20 20 20 20 20 20  etails..        
2c989 2a 2f 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c  */.        ExprL
2c98a 69 73 74 20 2a 70 4d 69 6e 4d 61 78 20 3d 20 30  ist *pMinMax = 0
2c98b 3b 0a 20 20 20 20 20 20 20 20 75 38 20 66 6c 61  ;.        u8 fla
2c98c 67 20 3d 20 6d 69 6e 4d 61 78 51 75 65 72 79 28  g = minMaxQuery(
2c98d 70 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  p);.        if( 
2c98e 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20  flag ){.        
2c98f 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
2c990 61 73 50 72 6f 70 65 72 74 79 28 70 2d 3e 70 45  asProperty(p->pE
2c991 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
2c992 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2c993 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d 69  );.          pMi
2c994 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 45 78  nMax = sqlite3Ex
2c995 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d  prListDup(db, p-
2c996 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  >pEList->a[0].pE
2c997 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2c 30 29 3b  xpr->x.pList,0);
2c998 0a 20 20 20 20 20 20 20 20 20 20 70 44 65 6c 20  .          pDel 
2c999 3d 20 70 4d 69 6e 4d 61 78 3b 0a 20 20 20 20 20  = pMinMax;.     
2c99a 20 20 20 20 20 69 66 28 20 70 4d 69 6e 4d 61 78       if( pMinMax
2c99b 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46   && !db->mallocF
2c99c 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20  ailed ){.       
2c99d 20 20 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b       pMinMax->a[
2c99e 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 66  0].sortOrder = f
2c99f 6c 61 67 21 3d 57 48 45 52 45 5f 4f 52 44 45 52  lag!=WHERE_ORDER
2c9a0 42 59 5f 4d 49 4e 20 3f 31 3a 30 3b 0a 20 20 20  BY_MIN ?1:0;.   
2c9a1 20 20 20 20 20 20 20 20 20 70 4d 69 6e 4d 61 78           pMinMax
2c9a2 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70  ->a[0].pExpr->op
2c9a3 20 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20   = TK_COLUMN;.  
2c9a4 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2c9a5 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 2f    }.  .        /
2c9a6 2a 20 54 68 69 73 20 63 61 73 65 20 72 75 6e 73  * This case runs
2c9a7 20 69 66 20 74 68 65 20 61 67 67 72 65 67 61 74   if the aggregat
2c9a8 65 20 68 61 73 20 6e 6f 20 47 52 4f 55 50 20 42  e has no GROUP B
2c9a9 59 20 63 6c 61 75 73 65 2e 20 20 54 68 65 0a 20  Y clause.  The. 
2c9aa 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73         ** proces
2c9ab 73 69 6e 67 20 69 73 20 6d 75 63 68 20 73 69 6d  sing is much sim
2c9ac 70 6c 65 72 20 73 69 6e 63 65 20 74 68 65 72 65  pler since there
2c9ad 20 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c   is only a singl
2c9ae 65 20 72 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a  e row.        **
2c9af 20 6f 66 20 6f 75 74 70 75 74 2e 0a 20 20 20 20   of output..    
2c9b0 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72      */.        r
2c9b1 65 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28  esetAccumulator(
2c9b2 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66  pParse, &sAggInf
2c9b3 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 57 49 6e  o);.        pWIn
2c9b4 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72  fo = sqlite3Wher
2c9b5 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70  eBegin(pParse, p
2c9b6 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c  TabList, pWhere,
2c9b7 20 26 70 4d 69 6e 4d 61 78 2c 20 66 6c 61 67 29   &pMinMax, flag)
2c9b8 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 57  ;.        if( pW
2c9b9 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Info==0 ){.     
2c9ba 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2c9bb 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2c9bc 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Del);.          
2c9bd 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
2c9be 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2c9bf 20 20 20 75 70 64 61 74 65 41 63 63 75 6d 75 6c     updateAccumul
2c9c0 61 74 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41  ator(pParse, &sA
2c9c1 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  ggInfo);.       
2c9c2 20 69 66 28 20 21 70 4d 69 6e 4d 61 78 20 26 26   if( !pMinMax &&
2c9c3 20 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20   flag ){.       
2c9c4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c9c5 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2c9c6 20 30 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65   0, pWInfo->iBre
2c9c7 61 6b 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56  ak);.          V
2c9c8 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2c9c9 25 73 28 29 20 62 79 20 69 6e 64 65 78 22 2c 0a  %s() by index",.
2c9ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c9cb 28 66 6c 61 67 3d 3d 57 48 45 52 45 5f 4f 52 44  (flag==WHERE_ORD
2c9cc 45 52 42 59 5f 4d 49 4e 3f 22 6d 69 6e 22 3a 22  ERBY_MIN?"min":"
2c9cd 6d 61 78 22 29 29 29 3b 0a 20 20 20 20 20 20 20  max")));.       
2c9ce 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
2c9cf 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66  e3WhereEnd(pWInf
2c9d0 6f 29 3b 0a 20 20 20 20 20 20 20 20 66 69 6e 61  o);.        fina
2c9d1 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73  lizeAggFunctions
2c9d2 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
2c9d3 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  fo);.      }..  
2c9d4 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 30      pOrderBy = 0
2c9d5 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2c9d6 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73  xprIfFalse(pPars
2c9d7 65 2c 20 70 48 61 76 69 6e 67 2c 20 61 64 64 72  e, pHaving, addr
2c9d8 45 6e 64 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  End, SQLITE_JUMP
2c9d9 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73  IFNULL);.      s
2c9da 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70  electInnerLoop(p
2c9db 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c  Parse, p, p->pEL
2c9dc 69 73 74 2c 20 30 2c 20 30 2c 20 30 2c 20 2d 31  ist, 0, 0, 0, -1
2c9dd 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2c9de 20 20 20 20 20 20 20 20 20 70 44 65 73 74 2c 20           pDest, 
2c9df 61 64 64 72 45 6e 64 2c 20 61 64 64 72 45 6e 64  addrEnd, addrEnd
2c9e0 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c9e1 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
2c9e2 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 7d 0a  b, pDel);.    }.
2c9e3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
2c9e4 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61  esolveLabel(v, a
2c9e5 64 64 72 45 6e 64 29 3b 0a 20 20 20 20 0a 20 20  ddrEnd);.    .  
2c9e6 7d 20 2f 2a 20 65 6e 64 69 66 20 61 67 67 72 65  } /* endif aggre
2c9e7 67 61 74 65 20 71 75 65 72 79 20 2a 2f 0a 0a 20  gate query */.. 
2c9e8 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
2c9e9 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  an ORDER BY clau
2c9ea 73 65 2c 20 74 68 65 6e 20 77 65 20 6e 65 65 64  se, then we need
2c9eb 20 74 6f 20 73 6f 72 74 20 74 68 65 20 72 65 73   to sort the res
2c9ec 75 6c 74 73 0a 20 20 2a 2a 20 61 6e 64 20 73 65  ults.  ** and se
2c9ed 6e 64 20 74 68 65 6d 20 74 6f 20 74 68 65 20 63  nd them to the c
2c9ee 61 6c 6c 62 61 63 6b 20 6f 6e 65 20 62 79 20 6f  allback one by o
2c9ef 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ne..  */.  if( p
2c9f0 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 67  OrderBy ){.    g
2c9f1 65 6e 65 72 61 74 65 53 6f 72 74 54 61 69 6c 28  enerateSortTail(
2c9f2 70 50 61 72 73 65 2c 20 70 2c 20 76 2c 20 70 45  pParse, p, v, pE
2c9f3 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 44 65  List->nExpr, pDe
2c9f4 73 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a  st);.  }..  /* J
2c9f5 75 6d 70 20 68 65 72 65 20 74 6f 20 73 6b 69 70  ump here to skip
2c9f6 20 74 68 69 73 20 71 75 65 72 79 0a 20 20 2a 2f   this query.  */
2c9f7 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
2c9f8 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 45  solveLabel(v, iE
2c9f9 6e 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 53  nd);..  /* The S
2c9fa 45 4c 45 43 54 20 77 61 73 20 73 75 63 63 65 73  ELECT was succes
2c9fb 73 66 75 6c 6c 79 20 63 6f 64 65 64 2e 20 20 20  sfully coded.   
2c9fc 53 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63  Set the return c
2c9fd 6f 64 65 20 74 6f 20 30 0a 20 20 2a 2a 20 74 6f  ode to 0.  ** to
2c9fe 20 69 6e 64 69 63 61 74 65 20 6e 6f 20 65 72 72   indicate no err
2c9ff 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ors..  */.  rc =
2ca00 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 74 72 6f   0;..  /* Contro
2ca01 6c 20 6a 75 6d 70 73 20 74 6f 20 68 65 72 65 20  l jumps to here 
2ca02 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 65  if an error is e
2ca03 6e 63 6f 75 6e 74 65 72 65 64 20 61 62 6f 76 65  ncountered above
2ca04 2c 20 6f 72 20 75 70 6f 6e 0a 20 20 2a 2a 20 73  , or upon.  ** s
2ca05 75 63 63 65 73 73 66 75 6c 20 63 6f 64 69 6e 67  uccessful coding
2ca06 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a   of the SELECT..
2ca07 20 20 2a 2f 0a 73 65 6c 65 63 74 5f 65 6e 64 3a    */.select_end:
2ca08 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66 79 20  ..  /* Identify 
2ca09 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 66 20  column names if 
2ca0a 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53  results of the S
2ca0b 45 4c 45 43 54 20 61 72 65 20 74 6f 20 62 65 20  ELECT are to be 
2ca0c 6f 75 74 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 69  output..  */.  i
2ca0d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2ca0e 20 26 26 20 70 44 65 73 74 2d 3e 65 44 65 73 74   && pDest->eDest
2ca0f 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a  ==SRT_Output ){.
2ca10 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75      generateColu
2ca11 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
2ca12 70 54 61 62 4c 69 73 74 2c 20 70 45 4c 69 73 74  pTabList, pEList
2ca13 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  );.  }..  sqlite
2ca14 33 44 62 46 72 65 65 28 64 62 2c 20 73 41 67 67  3DbFree(db, sAgg
2ca15 49 6e 66 6f 2e 61 43 6f 6c 29 3b 0a 20 20 73 71  Info.aCol);.  sq
2ca16 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2ca17 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 29 3b  sAggInfo.aFunc);
2ca18 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2ca19 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
2ca1a 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a  ITE_DEBUG)./*.**
2ca1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ca1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2ca20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
2ca21 6f 64 65 20 69 73 20 75 73 65 64 20 66 6f 72 20  ode is used for 
2ca22 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  testing and debu
2ca23 67 67 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68 65  gging only.  The
2ca24 20 63 6f 64 65 0a 2a 2a 20 74 68 61 74 20 66 6f   code.** that fo
2ca25 6c 6c 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61  llows does not a
2ca26 70 70 65 61 72 20 69 6e 20 6e 6f 72 6d 61 6c 20  ppear in normal 
2ca27 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  builds..**.** Th
2ca28 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
2ca29 20 75 73 65 64 20 74 6f 20 70 72 69 6e 74 20 6f   used to print o
2ca2a 75 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ut the content o
2ca2b 66 20 61 6c 6c 20 6f 72 20 70 61 72 74 20 6f 66  f all or part of
2ca2c 20 61 20 0a 2a 2a 20 70 61 72 73 65 20 73 74 72   a .** parse str
2ca2d 75 63 74 75 72 65 73 20 73 75 63 68 20 61 73 20  uctures such as 
2ca2e 53 65 6c 65 63 74 20 6f 72 20 45 78 70 72 2e 20  Select or Expr. 
2ca2f 20 53 75 63 68 20 70 72 69 6e 74 6f 75 74 73 20   Such printouts 
2ca30 61 72 65 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f  are useful.** fo
2ca31 72 20 68 65 6c 70 69 6e 67 20 74 6f 20 75 6e 64  r helping to und
2ca32 65 72 73 74 61 6e 64 20 77 68 61 74 20 69 73 20  erstand what is 
2ca33 68 61 70 70 65 6e 69 6e 67 20 69 6e 73 69 64 65  happening inside
2ca34 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
2ca35 74 6f 72 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68  tor.** during th
2ca36 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 63  e execution of c
2ca37 6f 6d 70 6c 65 78 20 53 45 4c 45 43 54 20 73 74  omplex SELECT st
2ca38 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
2ca39 54 68 65 73 65 20 72 6f 75 74 69 6e 65 20 61 72  These routine ar
2ca3a 65 20 6e 6f 74 20 63 61 6c 6c 65 64 20 61 6e 79  e not called any
2ca3b 77 68 65 72 65 20 66 72 6f 6d 20 77 69 74 68 69  where from withi
2ca3c 6e 20 74 68 65 20 6e 6f 72 6d 61 6c 0a 2a 2a 20  n the normal.** 
2ca3d 63 6f 64 65 20 62 61 73 65 2e 20 20 54 68 65 6e  code base.  Then
2ca3e 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f   are intended to
2ca3f 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
2ca40 77 69 74 68 69 6e 20 74 68 65 20 64 65 62 75 67  within the debug
2ca41 67 65 72 0a 2a 2a 20 6f 72 20 66 72 6f 6d 20 74  ger.** or from t
2ca42 65 6d 70 6f 72 61 72 79 20 22 70 72 69 6e 74 66  emporary "printf
2ca43 22 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73  " statements ins
2ca44 65 72 74 65 64 20 66 6f 72 20 64 65 62 75 67 67  erted for debugg
2ca45 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
2ca46 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2ca47 74 65 33 50 72 69 6e 74 45 78 70 72 28 45 78 70  te3PrintExpr(Exp
2ca48 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 45 78  r *p){.  if( !Ex
2ca49 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
2ca4a 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26   EP_IntValue) &&
2ca4b 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a   p->u.zToken ){.
2ca4c 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2ca4d 50 72 69 6e 74 66 28 22 28 25 73 22 2c 20 70 2d  Printf("(%s", p-
2ca4e 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d 65  >u.zToken);.  }e
2ca4f 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
2ca50 44 65 62 75 67 50 72 69 6e 74 66 28 22 28 25 64  DebugPrintf("(%d
2ca51 22 2c 20 70 2d 3e 6f 70 29 3b 0a 20 20 7d 0a 20  ", p->op);.  }. 
2ca52 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b   if( p->pLeft ){
2ca53 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
2ca54 67 50 72 69 6e 74 66 28 22 20 22 29 3b 0a 20 20  gPrintf(" ");.  
2ca55 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
2ca56 70 72 28 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  pr(p->pLeft);.  
2ca57 7d 0a 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68  }.  if( p->pRigh
2ca58 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
2ca59 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 22 29  DebugPrintf(" ")
2ca5a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69  ;.    sqlite3Pri
2ca5b 6e 74 45 78 70 72 28 70 2d 3e 70 52 69 67 68 74  ntExpr(p->pRight
2ca5c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2ca5d 44 65 62 75 67 50 72 69 6e 74 66 28 22 29 22 29  DebugPrintf(")")
2ca5e 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
2ca5f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
2ca60 72 69 6e 74 45 78 70 72 4c 69 73 74 28 45 78 70  rintExprList(Exp
2ca61 72 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20  rList *pList){. 
2ca62 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
2ca63 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70  0; i<pList->nExp
2ca64 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  r; i++){.    sql
2ca65 69 74 65 33 50 72 69 6e 74 45 78 70 72 28 70 4c  ite3PrintExpr(pL
2ca66 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
2ca67 3b 0a 20 20 20 20 69 66 28 20 69 3c 70 4c 69 73  ;.    if( i<pLis
2ca68 74 2d 3e 6e 45 78 70 72 2d 31 20 29 7b 0a 20 20  t->nExpr-1 ){.  
2ca69 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2ca6a 50 72 69 6e 74 66 28 22 2c 20 22 29 3b 0a 20 20  Printf(", ");.  
2ca6b 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45    }.  }.}.SQLITE
2ca6c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2ca6d 6c 69 74 65 33 50 72 69 6e 74 53 65 6c 65 63 74  lite3PrintSelect
2ca6e 28 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20  (Select *p, int 
2ca6f 69 6e 64 65 6e 74 29 7b 0a 20 20 73 71 6c 69 74  indent){.  sqlit
2ca70 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
2ca71 2a 73 53 45 4c 45 43 54 28 25 70 29 20 22 2c 20  *sSELECT(%p) ", 
2ca72 69 6e 64 65 6e 74 2c 20 22 22 2c 20 70 29 3b 0a  indent, "", p);.
2ca73 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
2ca74 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74  prList(p->pEList
2ca75 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75  );.  sqlite3Debu
2ca76 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20  gPrintf("\n");. 
2ca77 20 69 66 28 20 70 2d 3e 70 53 72 63 20 29 7b 0a   if( p->pSrc ){.
2ca78 20 20 20 20 63 68 61 72 20 2a 7a 50 72 65 66 69      char *zPrefi
2ca79 78 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  x;.    int i;.  
2ca7a 20 20 7a 50 72 65 66 69 78 20 3d 20 22 46 52 4f    zPrefix = "FRO
2ca7b 4d 22 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  M";.    for(i=0;
2ca7c 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63   i<p->pSrc->nSrc
2ca7d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74  ; i++){.      st
2ca7e 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2ca7f 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70  m *pItem = &p->p
2ca80 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20  Src->a[i];.     
2ca81 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2ca82 6e 74 66 28 22 25 2a 73 20 22 2c 20 69 6e 64 65  ntf("%*s ", inde
2ca83 6e 74 2b 36 2c 20 7a 50 72 65 66 69 78 29 3b 0a  nt+6, zPrefix);.
2ca84 20 20 20 20 20 20 7a 50 72 65 66 69 78 20 3d 20        zPrefix = 
2ca85 22 22 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  "";.      if( pI
2ca86 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  tem->pSelect ){.
2ca87 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2ca88 65 62 75 67 50 72 69 6e 74 66 28 22 28 5c 6e 22  ebugPrintf("(\n"
2ca89 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2ca8a 65 33 50 72 69 6e 74 53 65 6c 65 63 74 28 70 49  e3PrintSelect(pI
2ca8b 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 69 6e  tem->pSelect, in
2ca8c 64 65 6e 74 2b 31 30 29 3b 0a 20 20 20 20 20 20  dent+10);.      
2ca8d 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2ca8e 69 6e 74 66 28 22 25 2a 73 29 22 2c 20 69 6e 64  intf("%*s)", ind
2ca8f 65 6e 74 2b 38 2c 20 22 22 29 3b 0a 20 20 20 20  ent+8, "");.    
2ca90 20 20 7d 65 6c 73 65 20 69 66 28 20 70 49 74 65    }else if( pIte
2ca91 6d 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20  m->zName ){.    
2ca92 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2ca93 50 72 69 6e 74 66 28 22 25 73 22 2c 20 70 49 74  Printf("%s", pIt
2ca94 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  em->zName);.    
2ca95 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 49    }.      if( pI
2ca96 74 65 6d 2d 3e 70 54 61 62 20 29 7b 0a 20 20 20  tem->pTab ){.   
2ca97 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75       sqlite3Debu
2ca98 67 50 72 69 6e 74 66 28 22 28 74 61 62 6c 65 3a  gPrintf("(table:
2ca99 20 25 73 29 22 2c 20 70 49 74 65 6d 2d 3e 70 54   %s)", pItem->pT
2ca9a 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
2ca9b 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 49    }.      if( pI
2ca9c 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20  tem->zAlias ){. 
2ca9d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65         sqlite3De
2ca9e 62 75 67 50 72 69 6e 74 66 28 22 20 41 53 20 25  bugPrintf(" AS %
2ca9f 73 22 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  s", pItem->zAlia
2caa0 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  s);.      }.    
2caa1 20 20 69 66 28 20 69 3c 70 2d 3e 70 53 72 63 2d    if( i<p->pSrc-
2caa2 3e 6e 53 72 63 2d 31 20 29 7b 0a 20 20 20 20 20  >nSrc-1 ){.     
2caa3 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2caa4 72 69 6e 74 66 28 22 2c 22 29 3b 0a 20 20 20 20  rintf(",");.    
2caa5 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2caa6 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e  3DebugPrintf("\n
2caa7 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ");.    }.  }.  
2caa8 69 66 28 20 70 2d 3e 70 57 68 65 72 65 20 29 7b  if( p->pWhere ){
2caa9 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
2caaa 67 50 72 69 6e 74 66 28 22 25 2a 73 20 57 48 45  gPrintf("%*s WHE
2caab 52 45 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22  RE ", indent, ""
2caac 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72  );.    sqlite3Pr
2caad 69 6e 74 45 78 70 72 28 70 2d 3e 70 57 68 65 72  intExpr(p->pWher
2caae 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  e);.    sqlite3D
2caaf 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29  ebugPrintf("\n")
2cab0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
2cab1 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 73  GroupBy ){.    s
2cab2 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2cab3 66 28 22 25 2a 73 20 47 52 4f 55 50 20 42 59 20  f("%*s GROUP BY 
2cab4 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a  ", indent, "");.
2cab5 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74      sqlite3Print
2cab6 45 78 70 72 4c 69 73 74 28 70 2d 3e 70 47 72 6f  ExprList(p->pGro
2cab7 75 70 42 79 29 3b 0a 20 20 20 20 73 71 6c 69 74  upBy);.    sqlit
2cab8 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c  e3DebugPrintf("\
2cab9 6e 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n");.  }.  if( p
2caba 2d 3e 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20  ->pHaving ){.   
2cabb 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2cabc 6e 74 66 28 22 25 2a 73 20 48 41 56 49 4e 47 20  ntf("%*s HAVING 
2cabd 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a  ", indent, "");.
2cabe 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74      sqlite3Print
2cabf 45 78 70 72 28 70 2d 3e 70 48 61 76 69 6e 67 29  Expr(p->pHaving)
2cac0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  ;.    sqlite3Deb
2cac1 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a  ugPrintf("\n");.
2cac2 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72    }.  if( p->pOr
2cac3 64 65 72 42 79 20 29 7b 0a 20 20 20 20 73 71 6c  derBy ){.    sql
2cac4 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2cac5 22 25 2a 73 20 4f 52 44 45 52 20 42 59 20 22 2c  "%*s ORDER BY ",
2cac6 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20   indent, "");.  
2cac7 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
2cac8 70 72 4c 69 73 74 28 70 2d 3e 70 4f 72 64 65 72  prList(p->pOrder
2cac9 42 79 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  By);.    sqlite3
2caca 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22  DebugPrintf("\n"
2cacb 29 3b 0a 20 20 7d 0a 7d 0a 2f 2a 20 45 6e 64 20  );.  }.}./* End 
2cacc 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 72 65  of the structure
2cacd 20 64 65 62 75 67 20 70 72 69 6e 74 69 6e 67 20   debug printing 
2cace 63 6f 64 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  code.***********
2cacf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cad1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cad2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cad3 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65  **/.#endif /* de
2cad4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
2cad5 54 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  T) || defined(SQ
2cad6 4c 49 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a  LITE_DEBUG) */..
2cad7 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2cad8 45 6e 64 20 6f 66 20 73 65 6c 65 63 74 2e 63 20  End of select.c 
2cad9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cada 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cadb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2cadc 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2cadd 42 65 67 69 6e 20 66 69 6c 65 20 74 61 62 6c 65  Begin file table
2cade 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2cadf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2cae1 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
2cae2 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
2cae3 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2cae4 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2cae5 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2cae6 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2cae7 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2cae8 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2cae9 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2caea 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2caeb 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2caec 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2caed 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2caee 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2caef 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2caf0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2caf1 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2caf2 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2caf3 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2caf4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2caf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2caf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2caf7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2caf8 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
2caf9 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
2cafa 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62   sqlite3_get_tab
2cafb 6c 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  le() and sqlite3
2cafc 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 0a 2a 2a  _free_table().**
2cafd 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   interface routi
2cafe 6e 65 73 2e 20 20 54 68 65 73 65 20 61 72 65 20  nes.  These are 
2caff 6a 75 73 74 20 77 72 61 70 70 65 72 73 20 61 72  just wrappers ar
2cb00 6f 75 6e 64 20 74 68 65 20 6d 61 69 6e 0a 2a 2a  ound the main.**
2cb01 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69   interface routi
2cb02 6e 65 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78  ne of sqlite3_ex
2cb03 65 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ec()..**.** Thes
2cb04 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 69  e routines are i
2cb05 6e 20 61 20 73 65 70 61 72 61 74 65 20 66 69 6c  n a separate fil
2cb06 65 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 20  es so that they 
2cb07 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6c 69 6e 6b  will not be link
2cb08 65 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72  ed.** if they ar
2cb09 65 20 6e 6f 74 20 75 73 65 64 2e 0a 2a 2f 0a 0a  e not used..*/..
2cb0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2cb0b 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 0a 0a 2f  MIT_GET_TABLE../
2cb0c 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74  *.** This struct
2cb0d 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 70  ure is used to p
2cb0e 61 73 73 20 64 61 74 61 20 66 72 6f 6d 20 73 71  ass data from sq
2cb0f 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
2cb10 29 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20  ) through.** to 
2cb11 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  the callback fun
2cb12 63 74 69 6f 6e 20 69 73 20 75 73 65 73 20 74 6f  ction is uses to
2cb13 20 62 75 69 6c 64 20 74 68 65 20 72 65 73 75 6c   build the resul
2cb14 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  t..*/.typedef st
2cb15 72 75 63 74 20 54 61 62 52 65 73 75 6c 74 20 7b  ruct TabResult {
2cb16 0a 20 20 63 68 61 72 20 2a 2a 61 7a 52 65 73 75  .  char **azResu
2cb17 6c 74 3b 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c  lt;   /* Accumul
2cb18 61 74 65 64 20 6f 75 74 70 75 74 20 2a 2f 0a 20  ated output */. 
2cb19 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20   char *zErrMsg; 
2cb1a 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73      /* Error mes
2cb1b 73 61 67 65 20 74 65 78 74 2c 20 69 66 20 61 6e  sage text, if an
2cb1c 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 2a 2f   error occurs */
2cb1d 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20  .  int nAlloc;  
2cb1e 20 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20 61        /* Slots a
2cb1f 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 7a 52  llocated for azR
2cb20 65 73 75 6c 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74  esult[] */.  int
2cb21 20 6e 52 6f 77 3b 20 20 20 20 20 20 20 20 20 20   nRow;          
2cb22 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 6f 77  /* Number of row
2cb23 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
2cb24 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e  */.  int nColumn
2cb25 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
2cb26 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
2cb27 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20  the result */.  
2cb28 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20 20  int nData;      
2cb29 20 20 20 2f 2a 20 53 6c 6f 74 73 20 75 73 65 64     /* Slots used
2cb2a 20 69 6e 20 61 7a 52 65 73 75 6c 74 5b 5d 2e 20   in azResult[]. 
2cb2b 20 28 6e 52 6f 77 2b 31 29 2a 6e 43 6f 6c 75 6d   (nRow+1)*nColum
2cb2c 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  n */.  int rc;  
2cb2d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
2cb2e 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 71  urn code from sq
2cb2f 6c 69 74 65 33 5f 65 78 65 63 28 29 20 2a 2f 0a  lite3_exec() */.
2cb30 7d 20 54 61 62 52 65 73 75 6c 74 3b 0a 0a 2f 2a  } TabResult;../*
2cb31 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2cb32 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20   is called once 
2cb33 66 6f 72 20 65 61 63 68 20 72 6f 77 20 69 6e 20  for each row in 
2cb34 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
2cb35 2e 20 20 49 74 73 20 6a 6f 62 0a 2a 2a 20 69 73  .  Its job.** is
2cb36 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20   to fill in the 
2cb37 54 61 62 52 65 73 75 6c 74 20 73 74 72 75 63 74  TabResult struct
2cb38 75 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c  ure appropriatel
2cb39 79 2c 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 65  y, allocating ne
2cb3a 77 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 73 20 6e  w.** memory as n
2cb3b 65 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61  ecessary..*/.sta
2cb3c 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  tic int sqlite3_
2cb3d 67 65 74 5f 74 61 62 6c 65 5f 63 62 28 76 6f 69  get_table_cb(voi
2cb3e 64 20 2a 70 41 72 67 2c 20 69 6e 74 20 6e 43 6f  d *pArg, int nCo
2cb3f 6c 2c 20 63 68 61 72 20 2a 2a 61 72 67 76 2c 20  l, char **argv, 
2cb40 63 68 61 72 20 2a 2a 63 6f 6c 76 29 7b 0a 20 20  char **colv){.  
2cb41 54 61 62 52 65 73 75 6c 74 20 2a 70 20 3d 20 28  TabResult *p = (
2cb42 54 61 62 52 65 73 75 6c 74 2a 29 70 41 72 67 3b  TabResult*)pArg;
2cb43 20 20 2f 2a 20 52 65 73 75 6c 74 20 61 63 63 75    /* Result accu
2cb44 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74  mulator */.  int
2cb45 20 6e 65 65 64 3b 20 20 20 20 20 20 20 20 20 20   need;          
2cb46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cb47 2a 20 53 6c 6f 74 73 20 6e 65 65 64 65 64 20 69  * Slots needed i
2cb48 6e 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 5d 20  n p->azResult[] 
2cb49 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
2cb4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb4b 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
2cb4c 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63 68 61 72  ounter */.  char
2cb4d 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
2cb4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2cb4f 20 41 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e   A single column
2cb50 20 6f 66 20 72 65 73 75 6c 74 20 2a 2f 0a 0a 20   of result */.. 
2cb51 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68   /* Make sure th
2cb52 65 72 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70  ere is enough sp
2cb53 61 63 65 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75  ace in p->azResu
2cb54 6c 74 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79  lt to hold every
2cb55 74 68 69 6e 67 0a 20 20 2a 2a 20 77 65 20 6e 65  thing.  ** we ne
2cb56 65 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 66  ed to remember f
2cb57 72 6f 6d 20 74 68 69 73 20 69 6e 76 6f 63 61 74  rom this invocat
2cb58 69 6f 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 62  ion of the callb
2cb59 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ack..  */.  if( 
2cb5a 70 2d 3e 6e 52 6f 77 3d 3d 30 20 26 26 20 61 72  p->nRow==0 && ar
2cb5b 67 76 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 65  gv!=0 ){.    nee
2cb5c 64 20 3d 20 6e 43 6f 6c 2a 32 3b 0a 20 20 7d 65  d = nCol*2;.  }e
2cb5d 6c 73 65 7b 0a 20 20 20 20 6e 65 65 64 20 3d 20  lse{.    need = 
2cb5e 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20  nCol;.  }.  if( 
2cb5f 70 2d 3e 6e 44 61 74 61 20 2b 20 6e 65 65 64 20  p->nData + need 
2cb60 3e 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20  > p->nAlloc ){. 
2cb61 20 20 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b     char **azNew;
2cb62 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d  .    p->nAlloc =
2cb63 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 6e   p->nAlloc*2 + n
2cb64 65 65 64 3b 0a 20 20 20 20 61 7a 4e 65 77 20 3d  eed;.    azNew =
2cb65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
2cb66 28 20 70 2d 3e 61 7a 52 65 73 75 6c 74 2c 20 73  ( p->azResult, s
2cb67 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 70 2d 3e  izeof(char*)*p->
2cb68 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 69 66  nAlloc );.    if
2cb69 28 20 61 7a 4e 65 77 3d 3d 30 20 29 20 67 6f 74  ( azNew==0 ) got
2cb6a 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b  o malloc_failed;
2cb6b 0a 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74  .    p->azResult
2cb6c 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a 0a 20   = azNew;.  }.. 
2cb6d 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74   /* If this is t
2cb6e 68 65 20 66 69 72 73 74 20 72 6f 77 2c 20 74 68  he first row, th
2cb6f 65 6e 20 67 65 6e 65 72 61 74 65 20 61 6e 20 65  en generate an e
2cb70 78 74 72 61 20 72 6f 77 20 63 6f 6e 74 61 69 6e  xtra row contain
2cb71 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d  ing.  ** the nam
2cb72 65 73 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e  es of all column
2cb73 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d  s..  */.  if( p-
2cb74 3e 6e 52 6f 77 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRow==0 ){.    
2cb75 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 43 6f  p->nColumn = nCo
2cb76 6c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  l;.    for(i=0; 
2cb77 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  i<nCol; i++){.  
2cb78 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f      z = sqlite3_
2cb79 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20 63 6f  mprintf("%s", co
2cb7a 6c 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66  lv[i]);.      if
2cb7b 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f 20 6d 61  ( z==0 ) goto ma
2cb7c 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20 20 20  lloc_failed;.   
2cb7d 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70     p->azResult[p
2cb7e 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a  ->nData++] = z;.
2cb7f 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
2cb80 28 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 21 3d 6e 43  ( p->nColumn!=nC
2cb81 6f 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ol ){.    sqlite
2cb82 33 5f 66 72 65 65 28 70 2d 3e 7a 45 72 72 4d 73  3_free(p->zErrMs
2cb83 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d  g);.    p->zErrM
2cb84 73 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  sg = sqlite3_mpr
2cb85 69 6e 74 66 28 0a 20 20 20 20 20 20 20 22 73 71  intf(.       "sq
2cb86 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
2cb87 29 20 63 61 6c 6c 65 64 20 77 69 74 68 20 74 77  ) called with tw
2cb88 6f 20 6f 72 20 6d 6f 72 65 20 69 6e 63 6f 6d 70  o or more incomp
2cb89 61 74 69 62 6c 65 20 71 75 65 72 69 65 73 22 0a  atible queries".
2cb8a 20 20 20 20 29 3b 0a 20 20 20 20 70 2d 3e 72 63      );.    p->rc
2cb8b 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
2cb8c 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
2cb8d 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76   }..  /* Copy ov
2cb8e 65 72 20 74 68 65 20 72 6f 77 20 64 61 74 61 0a  er the row data.
2cb8f 20 20 2a 2f 0a 20 20 69 66 28 20 61 72 67 76 21    */.  if( argv!
2cb90 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d  =0 ){.    for(i=
2cb91 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
2cb92 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 76 5b  .      if( argv[
2cb93 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  i]==0 ){.       
2cb94 20 7a 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65   z = 0;.      }e
2cb95 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  lse{.        int
2cb96 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
2cb97 65 6e 33 30 28 61 72 67 76 5b 69 5d 29 2b 31 3b  en30(argv[i])+1;
2cb98 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 73 71 6c  .        z = sql
2cb99 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 6e 20 29  ite3_malloc( n )
2cb9a 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 3d  ;.        if( z=
2cb9b 3d 30 20 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63  =0 ) goto malloc
2cb9c 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20  _failed;.       
2cb9d 20 6d 65 6d 63 70 79 28 7a 2c 20 61 72 67 76 5b   memcpy(z, argv[
2cb9e 69 5d 2c 20 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  i], n);.      }.
2cb9f 20 20 20 20 20 20 70 2d 3e 61 7a 52 65 73 75 6c        p->azResul
2cba0 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d 20  t[p->nData++] = 
2cba1 7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  z;.    }.    p->
2cba2 6e 52 6f 77 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65  nRow++;.  }.  re
2cba3 74 75 72 6e 20 30 3b 0a 0a 6d 61 6c 6c 6f 63 5f  turn 0;..malloc_
2cba4 66 61 69 6c 65 64 3a 0a 20 20 70 2d 3e 72 63 20  failed:.  p->rc 
2cba5 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
2cba6 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
2cba7 2a 0a 2a 2a 20 51 75 65 72 79 20 74 68 65 20 64  *.** Query the d
2cba8 61 74 61 62 61 73 65 2e 20 20 42 75 74 20 69 6e  atabase.  But in
2cba9 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e  stead of invokin
2cbaa 67 20 61 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72  g a callback for
2cbab 20 65 61 63 68 20 72 6f 77 2c 0a 2a 2a 20 6d 61   each row,.** ma
2cbac 6c 6c 6f 63 28 29 20 66 6f 72 20 73 70 61 63 65  lloc() for space
2cbad 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73   to hold the res
2cbae 75 6c 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ult and return t
2cbaf 68 65 20 65 6e 74 69 72 65 20 72 65 73 75 6c 74  he entire result
2cbb0 73 0a 2a 2a 20 61 74 20 74 68 65 20 63 6f 6e 63  s.** at the conc
2cbb1 6c 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 61  lusion of the ca
2cbb2 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  ll..**.** The re
2cbb3 73 75 6c 74 20 74 68 61 74 20 69 73 20 77 72 69  sult that is wri
2cbb4 74 74 65 6e 20 74 6f 20 2a 2a 2a 70 61 7a 52 65  tten to ***pazRe
2cbb5 73 75 6c 74 20 69 73 20 68 65 6c 64 20 69 6e 20  sult is held in 
2cbb6 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a  memory obtained.
2cbb7 2a 2a 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ** from malloc()
2cbb8 2e 20 20 42 75 74 20 74 68 65 20 63 61 6c 6c 65  .  But the calle
2cbb9 72 20 63 61 6e 6e 6f 74 20 66 72 65 65 20 74 68  r cannot free th
2cbba 69 73 20 6d 65 6d 6f 72 79 20 64 69 72 65 63 74  is memory direct
2cbbb 6c 79 2e 20 20 0a 2a 2a 20 49 6e 73 74 65 61 64  ly.  .** Instead
2cbbc 2c 20 74 68 65 20 65 6e 74 69 72 65 20 74 61 62  , the entire tab
2cbbd 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73  le should be pas
2cbbe 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 66  sed to sqlite3_f
2cbbf 72 65 65 5f 74 61 62 6c 65 28 29 20 77 68 65 6e  ree_table() when
2cbc0 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  .** the calling 
2cbc1 70 72 6f 63 65 64 75 72 65 20 69 73 20 66 69 6e  procedure is fin
2cbc2 69 73 68 65 64 20 75 73 69 6e 67 20 69 74 2e 0a  ished using it..
2cbc3 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2cbc4 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  t sqlite3_get_ta
2cbc5 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ble(.  sqlite3 *
2cbc6 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2cbc7 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2cbc8 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20  se on which the 
2cbc9 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a 2f 0a  SQL executes */.
2cbca 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
2cbcb 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ql,           /*
2cbcc 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65 20 65   The SQL to be e
2cbcd 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 63 68 61  xecuted */.  cha
2cbce 72 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20  r ***pazResult, 
2cbcf 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
2cbd0 65 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62  e the result tab
2cbd1 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  le here */.  int
2cbd2 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 20 20   *pnRow,        
2cbd3 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
2cbd4 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
2cbd5 72 6f 77 73 20 69 6e 20 74 68 65 20 72 65 73 75  rows in the resu
2cbd6 6c 74 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74  lt here */.  int
2cbd7 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20   *pnColumn,     
2cbd8 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
2cbd9 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
2cbda 63 6f 6c 75 6d 6e 73 20 6f 66 20 72 65 73 75 6c  columns of resul
2cbdb 74 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72  t here */.  char
2cbdc 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20   **pzErrMsg     
2cbdd 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
2cbde 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20   error messages 
2cbdf 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  here */.){.  int
2cbe0 20 72 63 3b 0a 20 20 54 61 62 52 65 73 75 6c 74   rc;.  TabResult
2cbe1 20 72 65 73 3b 0a 0a 20 20 2a 70 61 7a 52 65 73   res;..  *pazRes
2cbe2 75 6c 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  ult = 0;.  if( p
2cbe3 6e 43 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c  nColumn ) *pnCol
2cbe4 75 6d 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  umn = 0;.  if( p
2cbe5 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20 3d 20  nRow ) *pnRow = 
2cbe6 30 3b 0a 20 20 69 66 28 20 70 7a 45 72 72 4d 73  0;.  if( pzErrMs
2cbe7 67 20 29 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20  g ) *pzErrMsg = 
2cbe8 30 3b 0a 20 20 72 65 73 2e 7a 45 72 72 4d 73 67  0;.  res.zErrMsg
2cbe9 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 52 6f 77   = 0;.  res.nRow
2cbea 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 43 6f 6c   = 0;.  res.nCol
2cbeb 75 6d 6e 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e  umn = 0;.  res.n
2cbec 44 61 74 61 20 3d 20 31 3b 0a 20 20 72 65 73 2e  Data = 1;.  res.
2cbed 6e 41 6c 6c 6f 63 20 3d 20 32 30 3b 0a 20 20 72  nAlloc = 20;.  r
2cbee 65 73 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  es.rc = SQLITE_O
2cbef 4b 3b 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c  K;.  res.azResul
2cbf0 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  t = sqlite3_mall
2cbf1 6f 63 28 73 69 7a 65 6f 66 28 63 68 61 72 2a 29  oc(sizeof(char*)
2cbf2 2a 72 65 73 2e 6e 41 6c 6c 6f 63 20 29 3b 0a 20  *res.nAlloc );. 
2cbf3 20 69 66 28 20 72 65 73 2e 61 7a 52 65 73 75 6c   if( res.azResul
2cbf4 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 64 62 2d  t==0 ){.     db-
2cbf5 3e 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54  >errCode = SQLIT
2cbf6 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 72 65  E_NOMEM;.     re
2cbf7 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2cbf8 4d 3b 0a 20 20 7d 0a 20 20 72 65 73 2e 61 7a 52  M;.  }.  res.azR
2cbf9 65 73 75 6c 74 5b 30 5d 20 3d 20 30 3b 0a 20 20  esult[0] = 0;.  
2cbfa 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65  rc = sqlite3_exe
2cbfb 63 28 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69  c(db, zSql, sqli
2cbfc 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62  te3_get_table_cb
2cbfd 2c 20 26 72 65 73 2c 20 70 7a 45 72 72 4d 73 67  , &res, pzErrMsg
2cbfe 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  );.  assert( siz
2cbff 65 6f 66 28 72 65 73 2e 61 7a 52 65 73 75 6c 74  eof(res.azResult
2cc00 5b 30 5d 29 3e 3d 20 73 69 7a 65 6f 66 28 72 65  [0])>= sizeof(re
2cc01 73 2e 6e 44 61 74 61 29 20 29 3b 0a 20 20 72 65  s.nData) );.  re
2cc02 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20  s.azResult[0] = 
2cc03 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54  SQLITE_INT_TO_PT
2cc04 52 28 72 65 73 2e 6e 44 61 74 61 29 3b 0a 20 20  R(res.nData);.  
2cc05 69 66 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53  if( (rc&0xff)==S
2cc06 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20  QLITE_ABORT ){. 
2cc07 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f     sqlite3_free_
2cc08 74 61 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73  table(&res.azRes
2cc09 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28  ult[1]);.    if(
2cc0a 20 72 65 73 2e 7a 45 72 72 4d 73 67 20 29 7b 0a   res.zErrMsg ){.
2cc0b 20 20 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d        if( pzErrM
2cc0c 73 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  sg ){.        sq
2cc0d 6c 69 74 65 33 5f 66 72 65 65 28 2a 70 7a 45 72  lite3_free(*pzEr
2cc0e 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a  rMsg);.        *
2cc0f 70 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  pzErrMsg = sqlit
2cc10 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c  e3_mprintf("%s",
2cc11 72 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20  res.zErrMsg);.  
2cc12 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
2cc13 74 65 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72  te3_free(res.zEr
2cc14 72 4d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rMsg);.    }.   
2cc15 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72   db->errCode = r
2cc16 65 73 2e 72 63 3b 20 20 2f 2a 20 41 73 73 75 6d  es.rc;  /* Assum
2cc17 65 20 33 32 2d 62 69 74 20 61 73 73 69 67 6e 6d  e 32-bit assignm
2cc18 65 6e 74 20 69 73 20 61 74 6f 6d 69 63 20 2a 2f  ent is atomic */
2cc19 0a 20 20 20 20 72 65 74 75 72 6e 20 72 65 73 2e  .    return res.
2cc1a 72 63 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  rc;.  }.  sqlite
2cc1b 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72 72 4d  3_free(res.zErrM
2cc1c 73 67 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  sg);.  if( rc!=S
2cc1d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2cc1e 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
2cc1f 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74  le(&res.azResult
2cc20 5b 31 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  [1]);.    return
2cc21 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72   rc;.  }.  if( r
2cc22 65 73 2e 6e 41 6c 6c 6f 63 3e 72 65 73 2e 6e 44  es.nAlloc>res.nD
2cc23 61 74 61 20 29 7b 0a 20 20 20 20 63 68 61 72 20  ata ){.    char 
2cc24 2a 2a 61 7a 4e 65 77 3b 0a 20 20 20 20 61 7a 4e  **azNew;.    azN
2cc25 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
2cc26 6c 6c 6f 63 28 20 72 65 73 2e 61 7a 52 65 73 75  lloc( res.azResu
2cc27 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a  lt, sizeof(char*
2cc28 29 2a 72 65 73 2e 6e 44 61 74 61 20 29 3b 0a 20  )*res.nData );. 
2cc29 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20     if( azNew==0 
2cc2a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2cc2b 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72 65 73  _free_table(&res
2cc2c 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20  .azResult[1]);. 
2cc2d 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65       db->errCode
2cc2e 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
2cc2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
2cc30 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
2cc31 7d 0a 20 20 20 20 72 65 73 2e 61 7a 52 65 73 75  }.    res.azResu
2cc32 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a  lt = azNew;.  }.
2cc33 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20 26    *pazResult = &
2cc34 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 3b  res.azResult[1];
2cc35 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20  .  if( pnColumn 
2cc36 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20 72 65  ) *pnColumn = re
2cc37 73 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28  s.nColumn;.  if(
2cc38 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20   pnRow ) *pnRow 
2cc39 3d 20 72 65 73 2e 6e 52 6f 77 3b 0a 20 20 72 65  = res.nRow;.  re
2cc3a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2cc3b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66  * This routine f
2cc3c 72 65 65 73 20 74 68 65 20 73 70 61 63 65 20 74  rees the space t
2cc3d 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  he sqlite3_get_t
2cc3e 61 62 6c 65 28 29 20 6d 61 6c 6c 6f 63 65 64 2e  able() malloced.
2cc3f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
2cc40 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65  oid sqlite3_free
2cc41 5f 74 61 62 6c 65 28 0a 20 20 63 68 61 72 20 2a  _table(.  char *
2cc42 2a 61 7a 52 65 73 75 6c 74 20 20 20 20 20 20 20  *azResult       
2cc43 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 72       /* Result r
2cc44 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 66 72 6f  eturned from fro
2cc45 6d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  m sqlite3_get_ta
2cc46 62 6c 65 28 29 20 2a 2f 0a 29 7b 0a 20 20 69 66  ble() */.){.  if
2cc47 28 20 61 7a 52 65 73 75 6c 74 20 29 7b 0a 20 20  ( azResult ){.  
2cc48 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20    int i, n;.    
2cc49 61 7a 52 65 73 75 6c 74 2d 2d 3b 0a 20 20 20 20  azResult--;.    
2cc4a 61 73 73 65 72 74 28 20 61 7a 52 65 73 75 6c 74  assert( azResult
2cc4b 21 3d 30 20 29 3b 0a 20 20 20 20 6e 20 3d 20 53  !=0 );.    n = S
2cc4c 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54  QLITE_PTR_TO_INT
2cc4d 28 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3b 0a 20  (azResult[0]);. 
2cc4e 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 3b     for(i=1; i<n;
2cc4f 20 69 2b 2b 29 7b 20 69 66 28 20 61 7a 52 65 73   i++){ if( azRes
2cc50 75 6c 74 5b 69 5d 20 29 20 73 71 6c 69 74 65 33  ult[i] ) sqlite3
2cc51 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74 5b 69  _free(azResult[i
2cc52 5d 29 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65  ]); }.    sqlite
2cc53 33 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74 29  3_free(azResult)
2cc54 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20  ;.  }.}..#endif 
2cc55 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47  /* SQLITE_OMIT_G
2cc56 45 54 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a  ET_TABLE */../**
2cc57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2cc58 20 6f 66 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a   of table.c ****
2cc59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2cc5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2cc5d 69 6e 20 66 69 6c 65 20 74 72 69 67 67 65 72 2e  in file trigger.
2cc5e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2cc5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2cc61 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2cc62 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2cc63 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2cc64 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2cc65 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2cc66 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2cc67 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2cc68 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2cc69 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2cc6a 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2cc6b 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2cc6c 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2cc6d 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2cc6e 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2cc6f 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2cc70 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2cc71 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2cc72 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2cc73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2cc76 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2cc77 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
2cc78 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65  ains the impleme
2cc79 6e 74 61 74 69 6f 6e 20 66 6f 72 20 54 52 49 47  ntation for TRIG
2cc7a 47 45 52 73 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66  GERs.*/..#ifndef
2cc7b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
2cc7c 47 47 45 52 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  GGER./*.** Delet
2cc7d 65 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  e a linked list 
2cc7e 6f 66 20 54 72 69 67 67 65 72 53 74 65 70 20 73  of TriggerStep s
2cc7f 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51  tructures..*/.SQ
2cc80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2cc81 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54  d sqlite3DeleteT
2cc82 72 69 67 67 65 72 53 74 65 70 28 73 71 6c 69 74  riggerStep(sqlit
2cc83 65 33 20 2a 64 62 2c 20 54 72 69 67 67 65 72 53  e3 *db, TriggerS
2cc84 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
2cc85 70 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 54 72  p){.  while( pTr
2cc86 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20  iggerStep ){.   
2cc87 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 20 70   TriggerStep * p
2cc88 54 6d 70 20 3d 20 70 54 72 69 67 67 65 72 53 74  Tmp = pTriggerSt
2cc89 65 70 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  ep;.    pTrigger
2cc8a 53 74 65 70 20 3d 20 70 54 72 69 67 67 65 72 53  Step = pTriggerS
2cc8b 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 20  tep->pNext;..   
2cc8c 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2cc8d 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 57 68  te(db, pTmp->pWh
2cc8e 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ere);.    sqlite
2cc8f 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2cc90 64 62 2c 20 70 54 6d 70 2d 3e 70 45 78 70 72 4c  db, pTmp->pExprL
2cc91 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ist);.    sqlite
2cc92 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2cc93 2c 20 70 54 6d 70 2d 3e 70 53 65 6c 65 63 74 29  , pTmp->pSelect)
2cc94 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c  ;.    sqlite3IdL
2cc95 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54  istDelete(db, pT
2cc96 6d 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a 0a 20  mp->pIdList);.. 
2cc97 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2cc98 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20 7d 0a  (db, pTmp);.  }.
2cc99 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74  }../*.** Given t
2cc9a 61 62 6c 65 20 70 54 61 62 2c 20 72 65 74 75 72  able pTab, retur
2cc9b 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  n a list of all 
2cc9c 74 68 65 20 74 72 69 67 67 65 72 73 20 61 74 74  the triggers att
2cc9d 61 63 68 65 64 20 74 6f 20 0a 2a 2a 20 74 68 65  ached to .** the
2cc9e 20 74 61 62 6c 65 2e 20 54 68 65 20 6c 69 73 74   table. The list
2cc9f 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79   is connected by
2cca0 20 54 72 69 67 67 65 72 2e 70 4e 65 78 74 20 70   Trigger.pNext p
2cca1 6f 69 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 41  ointers..**.** A
2cca2 6c 6c 20 6f 66 20 74 68 65 20 74 72 69 67 67 65  ll of the trigge
2cca3 72 73 20 6f 6e 20 70 54 61 62 20 74 68 61 74 20  rs on pTab that 
2cca4 61 72 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20  are in the same 
2cca5 64 61 74 61 62 61 73 65 20 61 73 20 70 54 61 62  database as pTab
2cca6 0a 2a 2a 20 61 72 65 20 61 6c 72 65 61 64 79 20  .** are already 
2cca7 61 74 74 61 63 68 65 64 20 74 6f 20 70 54 61 62  attached to pTab
2cca8 2d 3e 70 54 72 69 67 67 65 72 2e 20 20 42 75 74  ->pTrigger.  But
2cca9 20 74 68 65 72 65 20 6d 69 67 68 74 20 62 65 20   there might be 
2ccaa 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 74 72  additional.** tr
2ccab 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 20 69  iggers on pTab i
2ccac 6e 20 74 68 65 20 54 45 4d 50 20 73 63 68 65 6d  n the TEMP schem
2ccad 61 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  a.  This routine
2ccae 20 70 72 65 70 65 6e 64 73 20 61 6c 6c 0a 2a 2a   prepends all.**
2ccaf 20 54 45 4d 50 20 74 72 69 67 67 65 72 73 20 6f   TEMP triggers o
2ccb0 6e 20 70 54 61 62 20 74 6f 20 74 68 65 20 62 65  n pTab to the be
2ccb1 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70  ginning of the p
2ccb2 54 61 62 2d 3e 70 54 72 69 67 67 65 72 20 6c 69  Tab->pTrigger li
2ccb3 73 74 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e  st.** and return
2ccb4 73 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 6c  s the combined l
2ccb5 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 73 74  ist..**.** To st
2ccb6 61 74 65 20 69 74 20 61 6e 6f 74 68 65 72 20 77  ate it another w
2ccb7 61 79 3a 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ay:  This routin
2ccb8 65 20 72 65 74 75 72 6e 73 20 61 20 6c 69 73 74  e returns a list
2ccb9 20 6f 66 20 61 6c 6c 20 74 72 69 67 67 65 72 73   of all triggers
2ccba 0a 2a 2a 20 74 68 61 74 20 66 69 72 65 20 6f 66  .** that fire of
2ccbb 66 20 6f 66 20 70 54 61 62 2e 20 20 54 68 65 20  f of pTab.  The 
2ccbc 6c 69 73 74 20 77 69 6c 6c 20 69 6e 63 6c 75 64  list will includ
2ccbd 65 20 61 6e 79 20 54 45 4d 50 20 74 72 69 67 67  e any TEMP trigg
2ccbe 65 72 73 20 6f 6e 0a 2a 2a 20 70 54 61 62 20 61  ers on.** pTab a
2ccbf 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 74 72  s well as the tr
2ccc0 69 67 67 65 72 73 20 6c 69 73 65 64 20 69 6e 20  iggers lised in 
2ccc1 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 2e 0a  pTab->pTrigger..
2ccc2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2ccc3 45 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74  E Trigger *sqlit
2ccc4 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 50 61  e3TriggerList(Pa
2ccc5 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
2ccc6 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 53 63 68  le *pTab){.  Sch
2ccc7 65 6d 61 20 2a 20 63 6f 6e 73 74 20 70 54 6d 70  ema * const pTmp
2ccc8 53 63 68 65 6d 61 20 3d 20 70 50 61 72 73 65 2d  Schema = pParse-
2ccc9 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68  >db->aDb[1].pSch
2ccca 65 6d 61 3b 0a 20 20 54 72 69 67 67 65 72 20 2a  ema;.  Trigger *
2cccb 70 4c 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20  pList = 0;      
2cccc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2cccd 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73 20  ist of triggers 
2ccce 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20  to return */..  
2cccf 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61  if( pParse->disa
2ccd0 62 6c 65 54 72 69 67 67 65 72 73 20 29 7b 0a 20  bleTriggers ){. 
2ccd1 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2ccd2 0a 0a 20 20 69 66 28 20 70 54 6d 70 53 63 68 65  ..  if( pTmpSche
2ccd3 6d 61 21 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d  ma!=pTab->pSchem
2ccd4 61 20 29 7b 0a 20 20 20 20 48 61 73 68 45 6c 65  a ){.    HashEle
2ccd5 6d 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d  m *p;.    for(p=
2ccd6 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
2ccd7 26 70 54 6d 70 53 63 68 65 6d 61 2d 3e 74 72 69  &pTmpSchema->tri
2ccd8 67 48 61 73 68 29 3b 20 70 3b 20 70 3d 73 71 6c  gHash); p; p=sql
2ccd9 69 74 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b  iteHashNext(p)){
2ccda 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 20 2a  .      Trigger *
2ccdb 70 54 72 69 67 20 3d 20 28 54 72 69 67 67 65 72  pTrig = (Trigger
2ccdc 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74   *)sqliteHashDat
2ccdd 61 28 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20  a(p);.      if( 
2ccde 70 54 72 69 67 2d 3e 70 54 61 62 53 63 68 65 6d  pTrig->pTabSchem
2ccdf 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
2cce0 0a 20 20 20 20 20 20 20 26 26 20 30 3d 3d 73 71  .       && 0==sq
2cce1 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 72  lite3StrICmp(pTr
2cce2 69 67 2d 3e 74 61 62 6c 65 2c 20 70 54 61 62 2d  ig->table, pTab-
2cce3 3e 7a 4e 61 6d 65 29 20 0a 20 20 20 20 20 20 29  >zName) .      )
2cce4 7b 0a 20 20 20 20 20 20 20 20 70 54 72 69 67 2d  {.        pTrig-
2cce5 3e 70 4e 65 78 74 20 3d 20 28 70 4c 69 73 74 20  >pNext = (pList 
2cce6 3f 20 70 4c 69 73 74 20 3a 20 70 54 61 62 2d 3e  ? pList : pTab->
2cce7 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20  pTrigger);.     
2cce8 20 20 20 70 4c 69 73 74 20 3d 20 70 54 72 69 67     pList = pTrig
2cce9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2ccea 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 70    }..  return (p
2cceb 4c 69 73 74 20 3f 20 70 4c 69 73 74 20 3a 20 70  List ? pList : p
2ccec 54 61 62 2d 3e 70 54 72 69 67 67 65 72 29 3b 0a  Tab->pTrigger);.
2cced 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  }../*.** This is
2ccee 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
2ccef 61 72 73 65 72 20 77 68 65 6e 20 69 74 20 73 65  arser when it se
2ccf0 65 73 20 61 20 43 52 45 41 54 45 20 54 52 49 47  es a CREATE TRIG
2ccf1 47 45 52 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  GER statement.**
2ccf2 20 75 70 20 74 6f 20 74 68 65 20 70 6f 69 6e 74   up to the point
2ccf3 20 6f 66 20 74 68 65 20 42 45 47 49 4e 20 62 65   of the BEGIN be
2ccf4 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72  fore the trigger
2ccf5 20 61 63 74 69 6f 6e 73 2e 20 20 41 20 54 72 69   actions.  A Tri
2ccf6 67 67 65 72 0a 2a 2a 20 73 74 72 75 63 74 75 72  gger.** structur
2ccf7 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62  e is generated b
2ccf8 61 73 65 64 20 6f 6e 20 74 68 65 20 69 6e 66 6f  ased on the info
2ccf9 72 6d 61 74 69 6f 6e 20 61 76 61 69 6c 61 62 6c  rmation availabl
2ccfa 65 20 61 6e 64 20 73 74 6f 72 65 64 0a 2a 2a 20  e and stored.** 
2ccfb 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  in pParse->pNewT
2ccfc 72 69 67 67 65 72 2e 20 20 41 66 74 65 72 20 74  rigger.  After t
2ccfd 68 65 20 74 72 69 67 67 65 72 20 61 63 74 69 6f  he trigger actio
2ccfe 6e 73 20 68 61 76 65 20 62 65 65 6e 20 70 61 72  ns have been par
2ccff 73 65 64 2c 20 74 68 65 0a 2a 2a 20 73 71 6c 69  sed, the.** sqli
2cd00 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72  te3FinishTrigger
2cd01 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  () function is c
2cd02 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74  alled to complet
2cd03 65 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a  e the trigger.**
2cd04 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 70 72   construction pr
2cd05 6f 63 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ocess..*/.SQLITE
2cd06 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2cd07 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67 67 65  lite3BeginTrigge
2cd08 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  r(.  Parse *pPar
2cd09 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  se,      /* The 
2cd0a 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 6f 66  parse context of
2cd0b 20 74 68 65 20 43 52 45 41 54 45 20 54 52 49 47   the CREATE TRIG
2cd0c 47 45 52 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  GER statement */
2cd0d 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31  .  Token *pName1
2cd0e 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61  ,      /* The na
2cd0f 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67 67 65  me of the trigge
2cd10 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  r */.  Token *pN
2cd11 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20 54 68  ame2,      /* Th
2cd12 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72  e name of the tr
2cd13 69 67 67 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74  igger */.  int t
2cd14 72 5f 74 6d 2c 20 20 20 20 20 20 20 20 20 20 2f  r_tm,          /
2cd15 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 42 45 46 4f  * One of TK_BEFO
2cd16 52 45 2c 20 54 4b 5f 41 46 54 45 52 2c 20 54 4b  RE, TK_AFTER, TK
2cd17 5f 49 4e 53 54 45 41 44 20 2a 2f 0a 20 20 69 6e  _INSTEAD */.  in
2cd18 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
2cd19 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 49    /* One of TK_I
2cd1a 4e 53 45 52 54 2c 20 54 4b 5f 55 50 44 41 54 45  NSERT, TK_UPDATE
2cd1b 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20  , TK_DELETE */. 
2cd1c 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e   IdList *pColumn
2cd1d 73 2c 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6c  s,   /* column l
2cd1e 69 73 74 20 69 66 20 74 68 69 73 20 69 73 20 61  ist if this is a
2cd1f 6e 20 55 50 44 41 54 45 20 4f 46 20 74 72 69 67  n UPDATE OF trig
2cd20 67 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ger */.  SrcList
2cd21 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 2f 2a 20   *pTableName,/* 
2cd22 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
2cd23 74 61 62 6c 65 2f 76 69 65 77 20 74 68 65 20 74  table/view the t
2cd24 72 69 67 67 65 72 20 61 70 70 6c 69 65 73 20 74  rigger applies t
2cd25 6f 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  o */.  Expr *pWh
2cd26 65 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 48  en,        /* WH
2cd27 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  EN clause */.  i
2cd28 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20  nt isTemp,      
2cd29 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
2cd2a 65 20 54 45 4d 50 4f 52 41 52 59 20 6b 65 79 77  e TEMPORARY keyw
2cd2b 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a  ord is present *
2cd2c 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20  /.  int noErr   
2cd2d 20 20 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72          /* Suppr
2cd2e 65 73 73 20 65 72 72 6f 72 73 20 69 66 20 74 68  ess errors if th
2cd2f 65 20 74 72 69 67 67 65 72 20 61 6c 72 65 61 64  e trigger alread
2cd30 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20  y exists */.){. 
2cd31 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
2cd32 65 72 20 3d 20 30 3b 20 20 2f 2a 20 54 68 65 20  er = 0;  /* The 
2cd33 6e 65 77 20 74 72 69 67 67 65 72 20 2a 2f 0a 20  new trigger */. 
2cd34 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
2cd35 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2cd36 65 20 74 68 61 74 20 74 68 65 20 74 72 69 67 67  e that the trigg
2cd37 65 72 20 66 69 72 65 73 20 6f 66 66 20 6f 66 20  er fires off of 
2cd38 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  */.  char *zName
2cd39 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
2cd3a 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67  Name of the trig
2cd3b 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ger */.  sqlite3
2cd3c 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2cd3d 62 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  b;  /* The datab
2cd3e 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2cd3f 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
2cd40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2cd41 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 73  he database to s
2cd42 74 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72  tore the trigger
2cd43 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a   in */.  Token *
2cd44 70 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  pName;          
2cd45 20 2f 2a 20 54 68 65 20 75 6e 71 75 61 6c 69 66   /* The unqualif
2cd46 69 65 64 20 64 62 20 6e 61 6d 65 20 2a 2f 0a 20  ied db name */. 
2cd47 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 20 20   DbFixer sFix;  
2cd48 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
2cd49 65 20 76 65 63 74 6f 72 20 66 6f 72 20 74 68 65  e vector for the
2cd4a 20 44 42 20 66 69 78 65 72 20 2a 2f 0a 20 20 69   DB fixer */.  i
2cd4b 6e 74 20 69 54 61 62 44 62 3b 20 20 20 20 20 20  nt iTabDb;      
2cd4c 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2cd4d 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
2cd4e 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a  holding pTab */.
2cd4f 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65  .  assert( pName
2cd50 31 21 3d 30 20 29 3b 20 20 20 2f 2a 20 70 4e 61  1!=0 );   /* pNa
2cd51 6d 65 31 2d 3e 7a 20 6d 69 67 68 74 20 62 65 20  me1->z might be 
2cd52 4e 55 4c 4c 2c 20 62 75 74 20 6e 6f 74 20 70 4e  NULL, but not pN
2cd53 61 6d 65 31 20 69 74 73 65 6c 66 20 2a 2f 0a 20  ame1 itself */. 
2cd54 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 32 21   assert( pName2!
2cd55 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
2cd56 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 7c 7c  op==TK_INSERT ||
2cd57 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 7c   op==TK_UPDATE |
2cd58 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20  | op==TK_DELETE 
2cd59 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e  );.  assert( op>
2cd5a 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29 3b 0a  0 && op<0xff );.
2cd5b 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a    if( isTemp ){.
2cd5c 20 20 20 20 2f 2a 20 49 66 20 54 45 4d 50 20 77      /* If TEMP w
2cd5d 61 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 68  as specified, th
2cd5e 65 6e 20 74 68 65 20 74 72 69 67 67 65 72 20 6e  en the trigger n
2cd5f 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 71  ame may not be q
2cd60 75 61 6c 69 66 69 65 64 2e 20 2a 2f 0a 20 20 20  ualified. */.   
2cd61 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30   if( pName2->n>0
2cd62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2cd63 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2cd64 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 74 72 69  , "temporary tri
2cd65 67 67 65 72 20 6d 61 79 20 6e 6f 74 20 68 61 76  gger may not hav
2cd66 65 20 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65  e qualified name
2cd67 22 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74  ");.      goto t
2cd68 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
2cd69 20 20 20 20 7d 0a 20 20 20 20 69 44 62 20 3d 20      }.    iDb = 
2cd6a 31 3b 0a 20 20 20 20 70 4e 61 6d 65 20 3d 20 70  1;.    pName = p
2cd6b 4e 61 6d 65 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Name1;.  }else{.
2cd6c 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75      /* Figure ou
2cd6d 74 20 74 68 65 20 64 62 20 74 68 61 74 20 74 68  t the db that th
2cd6e 65 20 74 68 65 20 74 72 69 67 67 65 72 20 77 69  e the trigger wi
2cd6f 6c 6c 20 62 65 20 63 72 65 61 74 65 64 20 69 6e  ll be created in
2cd70 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 71   */.    iDb = sq
2cd71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65  lite3TwoPartName
2cd72 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c  (pParse, pName1,
2cd73 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29   pName2, &pName)
2cd74 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20  ;.    if( iDb<0 
2cd75 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72  ){.      goto tr
2cd76 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
2cd77 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
2cd78 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6e 61  f the trigger na
2cd79 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69 66 69  me was unqualifi
2cd7a 65 64 2c 20 61 6e 64 20 74 68 65 20 74 61 62 6c  ed, and the tabl
2cd7b 65 20 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c  e is a temp tabl
2cd7c 65 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74  e,.  ** then set
2cd7d 20 69 44 62 20 74 6f 20 31 20 74 6f 20 63 72 65   iDb to 1 to cre
2cd7e 61 74 65 20 74 68 65 20 74 72 69 67 67 65 72 20  ate the trigger 
2cd7f 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  in the temporary
2cd80 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20   database..  ** 
2cd81 49 66 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  If sqlite3SrcLis
2cd82 74 4c 6f 6f 6b 75 70 28 29 20 72 65 74 75 72 6e  tLookup() return
2cd83 73 20 30 2c 20 69 6e 64 69 63 61 74 69 6e 67 20  s 0, indicating 
2cd84 74 68 65 20 74 61 62 6c 65 20 64 6f 65 73 20 6e  the table does n
2cd85 6f 74 0a 20 20 2a 2a 20 65 78 69 73 74 2c 20 74  ot.  ** exist, t
2cd86 68 65 20 65 72 72 6f 72 20 69 73 20 63 61 75 67  he error is caug
2cd87 68 74 20 62 79 20 74 68 65 20 62 6c 6f 63 6b 20  ht by the block 
2cd88 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66  below..  */.  if
2cd89 28 20 21 70 54 61 62 6c 65 4e 61 6d 65 20 7c 7c  ( !pTableName ||
2cd8a 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2cd8b 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72  d ){.    goto tr
2cd8c 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
2cd8d 20 7d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69   }.  pTab = sqli
2cd8e 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70  te3SrcListLookup
2cd8f 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65 4e  (pParse, pTableN
2cd90 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d  ame);.  if( pNam
2cd91 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62  e2->n==0 && pTab
2cd92 20 26 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d   && pTab->pSchem
2cd93 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  a==db->aDb[1].pS
2cd94 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 69 44 62  chema ){.    iDb
2cd95 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 1;.  }..  /* 
2cd96 45 6e 73 75 72 65 20 74 68 65 20 74 61 62 6c 65  Ensure the table
2cd97 20 6e 61 6d 65 20 6d 61 74 63 68 65 73 20 64 61   name matches da
2cd98 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6e 64 20  tabase name and 
2cd99 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 65  that the table e
2cd9a 78 69 73 74 73 20 2a 2f 0a 20 20 69 66 28 20 64  xists */.  if( d
2cd9b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2cd9c 29 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63  ) goto trigger_c
2cd9d 6c 65 61 6e 75 70 3b 0a 20 20 61 73 73 65 72 74  leanup;.  assert
2cd9e 28 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 6e 53  ( pTableName->nS
2cd9f 72 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 73  rc==1 );.  if( s
2cda0 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73  qlite3FixInit(&s
2cda1 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62  Fix, pParse, iDb
2cda2 2c 20 22 74 72 69 67 67 65 72 22 2c 20 70 4e 61  , "trigger", pNa
2cda3 6d 65 29 20 26 26 20 0a 20 20 20 20 20 20 73 71  me) && .      sq
2cda4 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28  lite3FixSrcList(
2cda5 26 73 46 69 78 2c 20 70 54 61 62 6c 65 4e 61 6d  &sFix, pTableNam
2cda6 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  e) ){.    goto t
2cda7 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
2cda8 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c    }.  pTab = sql
2cda9 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
2cdaa 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65  p(pParse, pTable
2cdab 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 54  Name);.  if( !pT
2cdac 61 62 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  ab ){.    /* The
2cdad 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20   table does not 
2cdae 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20 69 66  exist. */.    if
2cdaf 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d  ( db->init.iDb==
2cdb0 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 69  1 ){.      /* Ti
2cdb1 63 6b 65 74 20 23 33 38 31 30 2e 0a 20 20 20 20  cket #3810..    
2cdb2 20 20 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 77    ** Normally, w
2cdb3 68 65 6e 65 76 65 72 20 61 20 74 61 62 6c 65 20  henever a table 
2cdb4 69 73 20 64 72 6f 70 70 65 64 2c 20 61 6c 6c 20  is dropped, all 
2cdb5 61 73 73 6f 63 69 61 74 65 64 20 74 72 69 67 67  associated trigg
2cdb6 65 72 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a  ers are.      **
2cdb7 20 64 72 6f 70 70 65 64 20 74 6f 6f 2e 20 20 42   dropped too.  B
2cdb8 75 74 20 69 66 20 61 20 54 45 4d 50 20 74 72 69  ut if a TEMP tri
2cdb9 67 67 65 72 20 69 73 20 63 72 65 61 74 65 64 20  gger is created 
2cdba 6f 6e 20 61 20 6e 6f 6e 2d 54 45 4d 50 20 74 61  on a non-TEMP ta
2cdbb 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64  ble.      ** and
2cdbc 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 64 72   the table is dr
2cdbd 6f 70 70 65 64 20 62 79 20 61 20 64 69 66 66 65  opped by a diffe
2cdbe 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f  rent database co
2cdbf 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 0a 20 20  nnection, the.  
2cdc0 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20 69      ** trigger i
2cdc1 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 20 74 6f  s not visible to
2cdc2 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
2cdc3 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 64 6f  nnection that do
2cdc4 65 73 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  es the.      ** 
2cdc5 64 72 6f 70 20 73 6f 20 74 68 65 20 74 72 69 67  drop so the trig
2cdc6 67 65 72 20 63 61 6e 6e 6f 74 20 62 65 20 64 72  ger cannot be dr
2cdc7 6f 70 70 65 64 2e 20 20 54 68 69 73 20 72 65 73  opped.  This res
2cdc8 75 6c 74 73 20 69 6e 20 61 6e 0a 20 20 20 20 20  ults in an.     
2cdc9 20 2a 2a 20 22 6f 72 70 68 61 6e 65 64 20 74 72   ** "orphaned tr
2cdca 69 67 67 65 72 22 20 2d 20 61 20 74 72 69 67 67  igger" - a trigg
2cdcb 65 72 20 77 68 6f 73 65 20 61 73 73 6f 63 69 61  er whose associa
2cdcc 74 65 64 20 74 61 62 6c 65 20 69 73 20 6d 69 73  ted table is mis
2cdcd 73 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  sing..      */. 
2cdce 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6f 72       db->init.or
2cdcf 70 68 61 6e 54 72 69 67 67 65 72 20 3d 20 31 3b  phanTrigger = 1;
2cdd0 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20  .    }.    goto 
2cdd1 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2cdd2 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72  .  }.  if( IsVir
2cdd3 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
2cdd4 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2cdd5 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
2cdd6 74 20 63 72 65 61 74 65 20 74 72 69 67 67 65 72  t create trigger
2cdd7 73 20 6f 6e 20 76 69 72 74 75 61 6c 20 74 61 62  s on virtual tab
2cdd8 6c 65 73 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  les");.    goto 
2cdd9 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2cdda 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  .  }..  /* Check
2cddb 20 74 68 61 74 20 74 68 65 20 74 72 69 67 67 65   that the trigge
2cddc 72 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 72 65  r name is not re
2cddd 73 65 72 76 65 64 20 61 6e 64 20 74 68 61 74 20  served and that 
2cdde 6e 6f 20 74 72 69 67 67 65 72 20 6f 66 20 74 68  no trigger of th
2cddf 65 0a 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64  e.  ** specified
2cde0 20 6e 61 6d 65 20 65 78 69 73 74 73 20 2a 2f 0a   name exists */.
2cde1 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
2cde2 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
2cde3 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b, pName);.  if(
2cde4 20 21 7a 4e 61 6d 65 20 7c 7c 20 53 51 4c 49 54   !zName || SQLIT
2cde5 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65  E_OK!=sqlite3Che
2cde6 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61  ckObjectName(pPa
2cde7 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20  rse, zName) ){. 
2cde8 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
2cde9 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
2cdea 66 28 20 73 71 6c 69 74 65 33 48 61 73 68 46 69  f( sqlite3HashFi
2cdeb 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b 69 44 62  nd(&(db->aDb[iDb
2cdec 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48  ].pSchema->trigH
2cded 61 73 68 29 2c 0a 20 20 20 20 20 20 20 20 20 20  ash),.          
2cdee 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d              zNam
2cdef 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  e, sqlite3Strlen
2cdf0 33 30 28 7a 4e 61 6d 65 29 29 20 29 7b 0a 20 20  30(zName)) ){.  
2cdf1 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a    if( !noErr ){.
2cdf2 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2cdf3 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
2cdf4 72 69 67 67 65 72 20 25 54 20 61 6c 72 65 61 64  rigger %T alread
2cdf5 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65  y exists", pName
2cdf6 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74  );.    }.    got
2cdf7 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
2cdf8 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20  p;.  }..  /* Do 
2cdf9 6e 6f 74 20 63 72 65 61 74 65 20 61 20 74 72 69  not create a tri
2cdfa 67 67 65 72 20 6f 6e 20 61 20 73 79 73 74 65 6d  gger on a system
2cdfb 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20   table */.  if( 
2cdfc 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2cdfd 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71  pTab->zName, "sq
2cdfe 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b  lite_", 7)==0 ){
2cdff 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2ce00 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61  rMsg(pParse, "ca
2ce01 6e 6e 6f 74 20 63 72 65 61 74 65 20 74 72 69 67  nnot create trig
2ce02 67 65 72 20 6f 6e 20 73 79 73 74 65 6d 20 74 61  ger on system ta
2ce03 62 6c 65 22 29 3b 0a 20 20 20 20 70 50 61 72 73  ble");.    pPars
2ce04 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67  e->nErr++;.    g
2ce05 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
2ce06 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  nup;.  }..  /* I
2ce07 4e 53 54 45 41 44 20 6f 66 20 74 72 69 67 67 65  NSTEAD of trigge
2ce08 72 73 20 61 72 65 20 6f 6e 6c 79 20 66 6f 72 20  rs are only for 
2ce09 76 69 65 77 73 20 61 6e 64 20 76 69 65 77 73 20  views and views 
2ce0a 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 49 4e 53  only support INS
2ce0b 54 45 41 44 0a 20 20 2a 2a 20 6f 66 20 74 72 69  TEAD.  ** of tri
2ce0c 67 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  ggers..  */.  if
2ce0d 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
2ce0e 26 26 20 74 72 5f 74 6d 21 3d 54 4b 5f 49 4e 53  && tr_tm!=TK_INS
2ce0f 54 45 41 44 20 29 7b 0a 20 20 20 20 73 71 6c 69  TEAD ){.    sqli
2ce10 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2ce11 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61  se, "cannot crea
2ce12 74 65 20 25 73 20 74 72 69 67 67 65 72 20 6f 6e  te %s trigger on
2ce13 20 76 69 65 77 3a 20 25 53 22 2c 20 0a 20 20 20   view: %S", .   
2ce14 20 20 20 20 20 28 74 72 5f 74 6d 20 3d 3d 20 54       (tr_tm == T
2ce15 4b 5f 42 45 46 4f 52 45 29 3f 22 42 45 46 4f 52  K_BEFORE)?"BEFOR
2ce16 45 22 3a 22 41 46 54 45 52 22 2c 20 70 54 61 62  E":"AFTER", pTab
2ce17 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  leName, 0);.    
2ce18 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
2ce19 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20  anup;.  }.  if( 
2ce1a 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 26  !pTab->pSelect &
2ce1b 26 20 74 72 5f 74 6d 3d 3d 54 4b 5f 49 4e 53 54  & tr_tm==TK_INST
2ce1c 45 41 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  EAD ){.    sqlit
2ce1d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2ce1e 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74  e, "cannot creat
2ce1f 65 20 49 4e 53 54 45 41 44 20 4f 46 22 0a 20 20  e INSTEAD OF".  
2ce20 20 20 20 20 20 20 22 20 74 72 69 67 67 65 72 20        " trigger 
2ce21 6f 6e 20 74 61 62 6c 65 3a 20 25 53 22 2c 20 70  on table: %S", p
2ce22 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a 20  TableName, 0);. 
2ce23 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
2ce24 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
2ce25 54 61 62 44 62 20 3d 20 73 71 6c 69 74 65 33 53  TabDb = sqlite3S
2ce26 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
2ce27 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
2ce28 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2ce29 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
2ce2a 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20  ION.  {.    int 
2ce2b 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52  code = SQLITE_CR
2ce2c 45 41 54 45 5f 54 52 49 47 47 45 52 3b 0a 20 20  EATE_TRIGGER;.  
2ce2d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
2ce2e 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 54 61 62  b = db->aDb[iTab
2ce2f 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  Db].zName;.    c
2ce30 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 54 72  onst char *zDbTr
2ce31 69 67 20 3d 20 69 73 54 65 6d 70 20 3f 20 64 62  ig = isTemp ? db
2ce32 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3a  ->aDb[1].zName :
2ce33 20 7a 44 62 3b 0a 20 20 20 20 69 66 28 20 69 54   zDb;.    if( iT
2ce34 61 62 44 62 3d 3d 31 20 7c 7c 20 69 73 54 65 6d  abDb==1 || isTem
2ce35 70 20 29 20 63 6f 64 65 20 3d 20 53 51 4c 49 54  p ) code = SQLIT
2ce36 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52  E_CREATE_TEMP_TR
2ce37 49 47 47 45 52 3b 0a 20 20 20 20 69 66 28 20 73  IGGER;.    if( s
2ce38 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2ce39 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e  pParse, code, zN
2ce3a 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
2ce3b 2c 20 7a 44 62 54 72 69 67 29 20 29 7b 0a 20 20  , zDbTrig) ){.  
2ce3c 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
2ce3d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
2ce3e 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
2ce3f 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2ce40 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20   SQLITE_INSERT, 
2ce41 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 54 61  SCHEMA_TABLE(iTa
2ce42 62 44 62 29 2c 30 2c 7a 44 62 29 29 7b 0a 20 20  bDb),0,zDb)){.  
2ce43 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
2ce44 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
2ce45 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
2ce46 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67   INSTEAD OF trig
2ce47 67 65 72 73 20 63 61 6e 20 6f 6e 6c 79 20 61 70  gers can only ap
2ce48 70 65 61 72 20 6f 6e 20 76 69 65 77 73 20 61 6e  pear on views an
2ce49 64 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72  d BEFORE trigger
2ce4a 73 0a 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 61 70  s.  ** cannot ap
2ce4b 70 65 61 72 20 6f 6e 20 76 69 65 77 73 2e 20 20  pear on views.  
2ce4c 53 6f 20 77 65 20 6d 69 67 68 74 20 61 73 20 77  So we might as w
2ce4d 65 6c 6c 20 74 72 61 6e 73 6c 61 74 65 20 65 76  ell translate ev
2ce4e 65 72 79 0a 20 20 2a 2a 20 49 4e 53 54 45 41 44  ery.  ** INSTEAD
2ce4f 20 4f 46 20 74 72 69 67 67 65 72 20 69 6e 74 6f   OF trigger into
2ce50 20 61 20 42 45 46 4f 52 45 20 74 72 69 67 67 65   a BEFORE trigge
2ce51 72 2e 20 20 49 74 20 73 69 6d 70 6c 69 66 69 65  r.  It simplifie
2ce52 73 20 63 6f 64 65 0a 20 20 2a 2a 20 65 6c 73 65  s code.  ** else
2ce53 77 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  where..  */.  if
2ce54 20 28 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f 49 4e   (tr_tm == TK_IN
2ce55 53 54 45 41 44 29 7b 0a 20 20 20 20 74 72 5f 74  STEAD){.    tr_t
2ce56 6d 20 3d 20 54 4b 5f 42 45 46 4f 52 45 3b 0a 20  m = TK_BEFORE;. 
2ce57 20 7d 0a 0a 20 20 2f 2a 20 42 75 69 6c 64 20 74   }..  /* Build t
2ce58 68 65 20 54 72 69 67 67 65 72 20 6f 62 6a 65 63  he Trigger objec
2ce59 74 20 2a 2f 0a 20 20 70 54 72 69 67 67 65 72 20  t */.  pTrigger 
2ce5a 3d 20 28 54 72 69 67 67 65 72 2a 29 73 71 6c 69  = (Trigger*)sqli
2ce5b 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2ce5c 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67  db, sizeof(Trigg
2ce5d 65 72 29 29 3b 0a 20 20 69 66 28 20 70 54 72 69  er));.  if( pTri
2ce5e 67 67 65 72 3d 3d 30 20 29 20 67 6f 74 6f 20 74  gger==0 ) goto t
2ce5f 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
2ce60 20 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d    pTrigger->zNam
2ce61 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7a 4e 61  e = zName;.  zNa
2ce62 6d 65 20 3d 20 30 3b 0a 20 20 70 54 72 69 67 67  me = 0;.  pTrigg
2ce63 65 72 2d 3e 74 61 62 6c 65 20 3d 20 73 71 6c 69  er->table = sqli
2ce64 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
2ce65 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 61 5b 30 5d  pTableName->a[0]
2ce66 2e 7a 4e 61 6d 65 29 3b 0a 20 20 70 54 72 69 67  .zName);.  pTrig
2ce67 67 65 72 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64  ger->pSchema = d
2ce68 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
2ce69 65 6d 61 3b 0a 20 20 70 54 72 69 67 67 65 72 2d  ema;.  pTrigger-
2ce6a 3e 70 54 61 62 53 63 68 65 6d 61 20 3d 20 70 54  >pTabSchema = pT
2ce6b 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 70  ab->pSchema;.  p
2ce6c 54 72 69 67 67 65 72 2d 3e 6f 70 20 3d 20 28 75  Trigger->op = (u
2ce6d 38 29 6f 70 3b 0a 20 20 70 54 72 69 67 67 65 72  8)op;.  pTrigger
2ce6e 2d 3e 74 72 5f 74 6d 20 3d 20 74 72 5f 74 6d 3d  ->tr_tm = tr_tm=
2ce6f 3d 54 4b 5f 42 45 46 4f 52 45 20 3f 20 54 52 49  =TK_BEFORE ? TRI
2ce70 47 47 45 52 5f 42 45 46 4f 52 45 20 3a 20 54 52  GGER_BEFORE : TR
2ce71 49 47 47 45 52 5f 41 46 54 45 52 3b 0a 20 20 70  IGGER_AFTER;.  p
2ce72 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 20 3d  Trigger->pWhen =
2ce73 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2ce74 64 62 2c 20 70 57 68 65 6e 2c 20 45 58 50 52 44  db, pWhen, EXPRD
2ce75 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 70 54  UP_REDUCE);.  pT
2ce76 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d 6e 73  rigger->pColumns
2ce77 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74   = sqlite3IdList
2ce78 44 75 70 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 73  Dup(db, pColumns
2ce79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
2ce7a 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
2ce7b 3d 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65 2d  ==0 );.  pParse-
2ce7c 3e 70 4e 65 77 54 72 69 67 67 65 72 20 3d 20 70  >pNewTrigger = p
2ce7d 54 72 69 67 67 65 72 3b 0a 0a 74 72 69 67 67 65  Trigger;..trigge
2ce7e 72 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c  r_cleanup:.  sql
2ce7f 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2ce80 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
2ce81 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
2ce82 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20  , pTableName);. 
2ce83 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65   sqlite3IdListDe
2ce84 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e  lete(db, pColumn
2ce85 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  s);.  sqlite3Exp
2ce86 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65  rDelete(db, pWhe
2ce87 6e 29 3b 0a 20 20 69 66 28 20 21 70 50 61 72 73  n);.  if( !pPars
2ce88 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20 29  e->pNewTrigger )
2ce89 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c  {.    sqlite3Del
2ce8a 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70  eteTrigger(db, p
2ce8b 54 72 69 67 67 65 72 29 3b 0a 20 20 7d 65 6c 73  Trigger);.  }els
2ce8c 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
2ce8d 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2ce8e 65 72 3d 3d 70 54 72 69 67 67 65 72 20 29 3b 0a  er==pTrigger );.
2ce8f 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
2ce90 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
2ce91 6c 65 64 20 61 66 74 65 72 20 61 6c 6c 20 6f 66  led after all of
2ce92 20 74 68 65 20 74 72 69 67 67 65 72 20 61 63 74   the trigger act
2ce93 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 70  ions have been p
2ce94 61 72 73 65 64 0a 2a 2a 20 69 6e 20 6f 72 64 65  arsed.** in orde
2ce95 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  r to complete th
2ce96 65 20 70 72 6f 63 65 73 73 20 6f 66 20 62 75 69  e process of bui
2ce97 6c 64 69 6e 67 20 74 68 65 20 74 72 69 67 67 65  lding the trigge
2ce98 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
2ce99 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2ce9a 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 0a  3FinishTrigger(.
2ce9b 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2ce9c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
2ce9d 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
2ce9e 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53   TriggerStep *pS
2ce9f 74 65 70 4c 69 73 74 2c 20 2f 2a 20 54 68 65 20  tepList, /* The 
2cea0 74 72 69 67 67 65 72 65 64 20 70 72 6f 67 72 61  triggered progra
2cea1 6d 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 41  m */.  Token *pA
2cea2 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ll             /
2cea3 2a 20 54 6f 6b 65 6e 20 74 68 61 74 20 64 65 73  * Token that des
2cea4 63 72 69 62 65 73 20 74 68 65 20 63 6f 6d 70 6c  cribes the compl
2cea5 65 74 65 20 43 52 45 41 54 45 20 54 52 49 47 47  ete CREATE TRIGG
2cea6 45 52 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67  ER */.){.  Trigg
2cea7 65 72 20 2a 70 54 72 69 67 20 3d 20 70 50 61 72  er *pTrig = pPar
2cea8 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3b  se->pNewTrigger;
2cea9 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 62      /* Trigger b
2ceaa 65 69 6e 67 20 66 69 6e 69 73 68 65 64 20 2a 2f  eing finished */
2ceab 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
2ceac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cead 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2ceae 61 6d 65 20 6f 66 20 74 72 69 67 67 65 72 20 2a  ame of trigger *
2ceaf 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2ceb0 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
2ceb1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ceb2 54 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  The database */.
2ceb3 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 0a    DbFixer sFix;.
2ceb4 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
2ceb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ceb6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2ceb7 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
2ceb8 67 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f  g the trigger */
2ceb9 0a 20 20 54 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b  .  Token nameTok
2ceba 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  en;           /*
2cebb 20 54 72 69 67 67 65 72 20 6e 61 6d 65 20 66 6f   Trigger name fo
2cebc 72 20 65 72 72 6f 72 20 72 65 70 6f 72 74 69 6e  r error reportin
2cebd 67 20 2a 2f 0a 0a 20 20 70 54 72 69 67 20 3d 20  g */..  pTrig = 
2cebe 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
2cebf 67 65 72 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70  ger;.  pParse->p
2cec0 4e 65 77 54 72 69 67 67 65 72 20 3d 20 30 3b 0a  NewTrigger = 0;.
2cec1 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 72    if( NEVER(pPar
2cec2 73 65 2d 3e 6e 45 72 72 29 20 7c 7c 20 21 70 54  se->nErr) || !pT
2cec3 72 69 67 20 29 20 67 6f 74 6f 20 74 72 69 67 67  rig ) goto trigg
2cec4 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70  erfinish_cleanup
2cec5 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 54 72 69  ;.  zName = pTri
2cec6 67 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 44 62 20  g->zName;.  iDb 
2cec7 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2cec8 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
2cec9 62 2c 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d  b, pTrig->pSchem
2ceca 61 29 3b 0a 20 20 70 54 72 69 67 2d 3e 73 74 65  a);.  pTrig->ste
2cecb 70 5f 6c 69 73 74 20 3d 20 70 53 74 65 70 4c 69  p_list = pStepLi
2cecc 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 74  st;.  while( pSt
2cecd 65 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 70 53  epList ){.    pS
2cece 74 65 70 4c 69 73 74 2d 3e 70 54 72 69 67 20 3d  tepList->pTrig =
2cecf 20 70 54 72 69 67 3b 0a 20 20 20 20 70 53 74 65   pTrig;.    pSte
2ced0 70 4c 69 73 74 20 3d 20 70 53 74 65 70 4c 69 73  pList = pStepLis
2ced1 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  t->pNext;.  }.  
2ced2 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 20 3d 20 70 54  nameToken.z = pT
2ced3 72 69 67 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 6e 61  rig->zName;.  na
2ced4 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 73 71 6c 69  meToken.n = sqli
2ced5 74 65 33 53 74 72 6c 65 6e 33 30 28 6e 61 6d 65  te3Strlen30(name
2ced6 54 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 69 66 28 20  Token.z);.  if( 
2ced7 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26  sqlite3FixInit(&
2ced8 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44  sFix, pParse, iD
2ced9 62 2c 20 22 74 72 69 67 67 65 72 22 2c 20 26 6e  b, "trigger", &n
2ceda 61 6d 65 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20  ameToken) .     
2cedb 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46       && sqlite3F
2cedc 69 78 54 72 69 67 67 65 72 53 74 65 70 28 26 73  ixTriggerStep(&s
2cedd 46 69 78 2c 20 70 54 72 69 67 2d 3e 73 74 65 70  Fix, pTrig->step
2cede 5f 6c 69 73 74 29 20 29 7b 0a 20 20 20 20 67 6f  _list) ){.    go
2cedf 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73 68  to triggerfinish
2cee0 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
2cee1 20 2f 2a 20 69 66 20 77 65 20 61 72 65 20 6e 6f   /* if we are no
2cee2 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 2c 20  t initializing, 
2cee3 61 6e 64 20 74 68 69 73 20 74 72 69 67 67 65 72  and this trigger
2cee4 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 54 45 4d   is not on a TEM
2cee5 50 20 74 61 62 6c 65 2c 20 0a 20 20 2a 2a 20 62  P table, .  ** b
2cee6 75 69 6c 64 20 74 68 65 20 73 71 6c 69 74 65 5f  uild the sqlite_
2cee7 6d 61 73 74 65 72 20 65 6e 74 72 79 0a 20 20 2a  master entry.  *
2cee8 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69  /.  if( !db->ini
2cee9 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 56 64  t.busy ){.    Vd
2ceea 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20  be *v;.    char 
2ceeb 2a 7a 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65  *z;..    /* Make
2ceec 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 65   an entry in the
2ceed 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
2ceee 61 62 6c 65 20 2a 2f 0a 20 20 20 20 76 20 3d 20  able */.    v = 
2ceef 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2cef0 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20  Parse);.    if( 
2cef1 76 3d 3d 30 20 29 20 67 6f 74 6f 20 74 72 69 67  v==0 ) goto trig
2cef2 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75  gerfinish_cleanu
2cef3 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  p;.    sqlite3Be
2cef4 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
2cef5 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
2cef6 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  );.    z = sqlit
2cef7 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
2cef8 28 63 68 61 72 2a 29 70 41 6c 6c 2d 3e 7a 2c 20  (char*)pAll->z, 
2cef9 70 41 6c 6c 2d 3e 6e 29 3b 0a 20 20 20 20 73 71  pAll->n);.    sq
2cefa 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2cefb 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
2cefc 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 2e  "INSERT INTO %Q.
2cefd 25 73 20 56 41 4c 55 45 53 28 27 74 72 69 67 67  %s VALUES('trigg
2cefe 65 72 27 2c 25 51 2c 25 51 2c 30 2c 27 43 52 45  er',%Q,%Q,0,'CRE
2ceff 41 54 45 20 54 52 49 47 47 45 52 20 25 71 27 29  ATE TRIGGER %q')
2cf00 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44  ",.       db->aD
2cf01 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43  b[iDb].zName, SC
2cf02 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c  HEMA_TABLE(iDb),
2cf03 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 70   zName,.       p
2cf04 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20 7a 29 3b  Trig->table, z);
2cf05 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2cf06 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 73  ee(db, z);.    s
2cf07 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
2cf08 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
2cf09 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2cf0a 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72  AddOp4(v, OP_Par
2cf0b 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30  seSchema, iDb, 0
2cf0c 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d 50 72 69  , 0, sqlite3MPri
2cf0d 6e 74 66 28 0a 20 20 20 20 20 20 20 20 64 62 2c  ntf(.        db,
2cf0e 20 22 74 79 70 65 3d 27 74 72 69 67 67 65 72 27   "type='trigger'
2cf0f 20 41 4e 44 20 6e 61 6d 65 3d 27 25 71 27 22 2c   AND name='%q'",
2cf10 20 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41   zName), P4_DYNA
2cf11 4d 49 43 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a  MIC.    );.  }..
2cf12 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
2cf13 75 73 79 20 29 7b 0a 20 20 20 20 54 72 69 67 67  usy ){.    Trigg
2cf14 65 72 20 2a 70 4c 69 6e 6b 20 3d 20 70 54 72 69  er *pLink = pTri
2cf15 67 3b 0a 20 20 20 20 48 61 73 68 20 2a 70 48 61  g;.    Hash *pHa
2cf16 73 68 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  sh = &db->aDb[iD
2cf17 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67  b].pSchema->trig
2cf18 48 61 73 68 3b 0a 20 20 20 20 70 54 72 69 67 20  Hash;.    pTrig 
2cf19 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
2cf1a 65 72 74 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65  ert(pHash, zName
2cf1b 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
2cf1c 30 28 7a 4e 61 6d 65 29 2c 20 70 54 72 69 67 29  0(zName), pTrig)
2cf1d 3b 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 20  ;.    if( pTrig 
2cf1e 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
2cf1f 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2cf20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 69     }else if( pLi
2cf21 6e 6b 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 4c 69  nk->pSchema==pLi
2cf22 6e 6b 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 29  nk->pTabSchema )
2cf23 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  {.      Table *p
2cf24 54 61 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  Tab;.      int n
2cf25 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2cf26 33 30 28 70 4c 69 6e 6b 2d 3e 74 61 62 6c 65 29  30(pLink->table)
2cf27 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 73  ;.      pTab = s
2cf28 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
2cf29 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68 65 6d  pLink->pTabSchem
2cf2a 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 4c 69 6e  a->tblHash, pLin
2cf2b 6b 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a 20 20  k->table, n);.  
2cf2c 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
2cf2d 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4c 69  !=0 );.      pLi
2cf2e 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61 62  nk->pNext = pTab
2cf2f 2d 3e 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20  ->pTrigger;.    
2cf30 20 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72    pTab->pTrigger
2cf31 20 3d 20 70 4c 69 6e 6b 3b 0a 20 20 20 20 7d 0a   = pLink;.    }.
2cf32 20 20 7d 0a 0a 74 72 69 67 67 65 72 66 69 6e 69    }..triggerfini
2cf33 73 68 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71  sh_cleanup:.  sq
2cf34 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
2cf35 65 72 28 64 62 2c 20 70 54 72 69 67 29 3b 0a 20  er(db, pTrig);. 
2cf36 20 61 73 73 65 72 74 28 20 21 70 50 61 72 73 65   assert( !pParse
2cf37 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20 29 3b  ->pNewTrigger );
2cf38 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  .  sqlite3Delete
2cf39 54 72 69 67 67 65 72 53 74 65 70 28 64 62 2c 20  TriggerStep(db, 
2cf3a 70 53 74 65 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f  pStepList);.}../
2cf3b 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 53 45 4c 45  *.** Turn a SELE
2cf3c 43 54 20 73 74 61 74 65 6d 65 6e 74 20 28 74 68  CT statement (th
2cf3d 61 74 20 74 68 65 20 70 53 65 6c 65 63 74 20 70  at the pSelect p
2cf3e 61 72 61 6d 65 74 65 72 20 70 6f 69 6e 74 73 20  arameter points 
2cf3f 74 6f 29 20 69 6e 74 6f 0a 2a 2a 20 61 20 74 72  to) into.** a tr
2cf40 69 67 67 65 72 20 73 74 65 70 2e 20 20 52 65 74  igger step.  Ret
2cf41 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
2cf42 20 61 20 54 72 69 67 67 65 72 53 74 65 70 20 73   a TriggerStep s
2cf43 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
2cf44 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
2cf45 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68   this routine wh
2cf46 65 6e 20 69 74 20 66 69 6e 64 73 20 61 20 53 45  en it finds a SE
2cf47 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69  LECT statement i
2cf48 6e 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20 54  n.** body of a T
2cf49 52 49 47 47 45 52 2e 20 20 0a 2a 2f 0a 53 51 4c  RIGGER.  .*/.SQL
2cf4a 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67  ITE_PRIVATE Trig
2cf4b 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
2cf4c 54 72 69 67 67 65 72 53 65 6c 65 63 74 53 74 65  TriggerSelectSte
2cf4d 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53  p(sqlite3 *db, S
2cf4e 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b  elect *pSelect){
2cf4f 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
2cf50 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73  pTriggerStep = s
2cf51 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
2cf52 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72  ro(db, sizeof(Tr
2cf53 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69  iggerStep));.  i
2cf54 66 28 20 70 54 72 69 67 67 65 72 53 74 65 70 3d  f( pTriggerStep=
2cf55 3d 30 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74  =0 ) {.    sqlit
2cf56 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
2cf57 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  b, pSelect);.   
2cf58 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2cf59 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
2cf5a 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20  p = TK_SELECT;. 
2cf5b 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
2cf5c 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74  Select = pSelect
2cf5d 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ;.  pTriggerStep
2cf5e 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65  ->orconf = OE_De
2cf5f 66 61 75 6c 74 3b 0a 20 20 72 65 74 75 72 6e 20  fault;.  return 
2cf60 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a  pTriggerStep;.}.
2cf61 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
2cf62 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 61 20  space to hold a 
2cf63 6e 65 77 20 74 72 69 67 67 65 72 20 73 74 65 70  new trigger step
2cf64 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 65 64  .  The allocated
2cf65 20 73 70 61 63 65 0a 2a 2a 20 68 6f 6c 64 73 20   space.** holds 
2cf66 62 6f 74 68 20 74 68 65 20 54 72 69 67 67 65 72  both the Trigger
2cf67 53 74 65 70 20 6f 62 6a 65 63 74 20 61 6e 64 20  Step object and 
2cf68 74 68 65 20 54 72 69 67 67 65 72 53 74 65 70 2e  the TriggerStep.
2cf69 74 61 72 67 65 74 2e 7a 20 73 74 72 69 6e 67 2e  target.z string.
2cf6a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f 4d  .**.** If an OOM
2cf6b 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e   error occurs, N
2cf6c 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
2cf6d 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  and db->mallocFa
2cf6e 69 6c 65 64 20 69 73 20 73 65 74 2e 0a 2a 2f 0a  iled is set..*/.
2cf6f 73 74 61 74 69 63 20 54 72 69 67 67 65 72 53 74  static TriggerSt
2cf70 65 70 20 2a 74 72 69 67 67 65 72 53 74 65 70 41  ep *triggerStepA
2cf71 6c 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74  llocate(.  sqlit
2cf72 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
2cf73 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2cf74 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2cf75 0a 20 20 75 38 20 6f 70 2c 20 20 20 20 20 20 20  .  u8 op,       
2cf76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cf77 2a 20 54 72 69 67 67 65 72 20 6f 70 63 6f 64 65  * Trigger opcode
2cf78 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
2cf79 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  me              
2cf7a 20 20 2f 2a 20 54 68 65 20 74 61 72 67 65 74 20    /* The target 
2cf7b 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  name */.){.  Tri
2cf7c 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67  ggerStep *pTrigg
2cf7d 65 72 53 74 65 70 3b 0a 0a 20 20 70 54 72 69 67  erStep;..  pTrig
2cf7e 67 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74 65  gerStep = sqlite
2cf7f 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2cf80 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72  , sizeof(Trigger
2cf81 53 74 65 70 29 20 2b 20 70 4e 61 6d 65 2d 3e 6e  Step) + pName->n
2cf82 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  );.  if( pTrigge
2cf83 72 53 74 65 70 20 29 7b 0a 20 20 20 20 63 68 61  rStep ){.    cha
2cf84 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 26 70  r *z = (char*)&p
2cf85 54 72 69 67 67 65 72 53 74 65 70 5b 31 5d 3b 0a  TriggerStep[1];.
2cf86 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 4e      memcpy(z, pN
2cf87 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e  ame->z, pName->n
2cf88 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53  );.    pTriggerS
2cf89 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a 20 3d 20  tep->target.z = 
2cf8a 7a 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53  z;.    pTriggerS
2cf8b 74 65 70 2d 3e 74 61 72 67 65 74 2e 6e 20 3d 20  tep->target.n = 
2cf8c 70 4e 61 6d 65 2d 3e 6e 3b 0a 20 20 20 20 70 54  pName->n;.    pT
2cf8d 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d  riggerStep->op =
2cf8e 20 6f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   op;.  }.  retur
2cf8f 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a  n pTriggerStep;.
2cf90 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 69 6c 64 20 61  }../*.** Build a
2cf91 20 74 72 69 67 67 65 72 20 73 74 65 70 20 6f 75   trigger step ou
2cf92 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 73  t of an INSERT s
2cf93 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 72  tatement.  Retur
2cf94 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  n a pointer.** t
2cf95 6f 20 74 68 65 20 6e 65 77 20 74 72 69 67 67 65  o the new trigge
2cf96 72 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68  r step..**.** Th
2cf97 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74  e parser calls t
2cf98 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
2cf99 20 69 74 20 73 65 65 73 20 61 6e 20 49 4e 53 45   it sees an INSE
2cf9a 52 54 20 69 6e 73 69 64 65 20 74 68 65 0a 2a 2a  RT inside the.**
2cf9b 20 62 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67   body of a trigg
2cf9c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
2cf9d 49 56 41 54 45 20 54 72 69 67 67 65 72 53 74 65  IVATE TriggerSte
2cf9e 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65  p *sqlite3Trigge
2cf9f 72 49 6e 73 65 72 74 53 74 65 70 28 0a 20 20 73  rInsertStep(.  s
2cfa0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2cfa1 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2cfa2 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2cfa3 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65  .  Token *pTable
2cfa4 4e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f  Name,  /* Name o
2cfa5 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f  f the table into
2cfa6 20 77 68 69 63 68 20 77 65 20 69 6e 73 65 72 74   which we insert
2cfa7 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43   */.  IdList *pC
2cfa8 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 4c 69 73  olumn,    /* Lis
2cfa9 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  t of columns in 
2cfaa 70 54 61 62 6c 65 4e 61 6d 65 20 74 6f 20 69 6e  pTableName to in
2cfab 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 45  sert into */.  E
2cfac 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
2cfad 20 20 20 2f 2a 20 54 68 65 20 56 41 4c 55 45 20     /* The VALUE 
2cfae 63 6c 61 75 73 65 3a 20 61 20 6c 69 73 74 20 6f  clause: a list o
2cfaf 66 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69  f values to be i
2cfb0 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 53 65 6c  nserted */.  Sel
2cfb1 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
2cfb2 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61   /* A SELECT sta
2cfb3 74 65 6d 65 6e 74 20 74 68 61 74 20 73 75 70 70  tement that supp
2cfb4 6c 69 65 73 20 76 61 6c 75 65 73 20 2a 2f 0a 20  lies values */. 
2cfb5 20 75 38 20 6f 72 63 6f 6e 66 20 20 20 20 20 20   u8 orconf      
2cfb6 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 66       /* The conf
2cfb7 6c 69 63 74 20 61 6c 67 6f 72 69 74 68 6d 20 28  lict algorithm (
2cfb8 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 52 65 70  OE_Abort, OE_Rep
2cfb9 6c 61 63 65 2c 20 65 74 63 2e 29 20 2a 2f 0a 29  lace, etc.) */.)
2cfba 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  {.  TriggerStep 
2cfbb 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 0a  *pTriggerStep;..
2cfbc 20 20 61 73 73 65 72 74 28 70 45 4c 69 73 74 20    assert(pEList 
2cfbd 3d 3d 20 30 20 7c 7c 20 70 53 65 6c 65 63 74 20  == 0 || pSelect 
2cfbe 3d 3d 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28  == 0);.  assert(
2cfbf 70 45 4c 69 73 74 20 21 3d 20 30 20 7c 7c 20 70  pEList != 0 || p
2cfc0 53 65 6c 65 63 74 20 21 3d 20 30 20 7c 7c 20 64  Select != 0 || d
2cfc1 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29  b->mallocFailed)
2cfc2 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74 65  ;..  pTriggerSte
2cfc3 70 20 3d 20 74 72 69 67 67 65 72 53 74 65 70 41  p = triggerStepA
2cfc4 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f 49  llocate(db, TK_I
2cfc5 4e 53 45 52 54 2c 20 70 54 61 62 6c 65 4e 61 6d  NSERT, pTableNam
2cfc6 65 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67  e);.  if( pTrigg
2cfc7 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70 54  erStep ){.    pT
2cfc8 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c  riggerStep->pSel
2cfc9 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
2cfca 65 63 74 44 75 70 28 64 62 2c 20 70 53 65 6c 65  ectDup(db, pSele
2cfcb 63 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  ct, EXPRDUP_REDU
2cfcc 43 45 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65  CE);.    pTrigge
2cfcd 72 53 74 65 70 2d 3e 70 49 64 4c 69 73 74 20 3d  rStep->pIdList =
2cfce 20 70 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 54   pColumn;.    pT
2cfcf 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70  riggerStep->pExp
2cfd0 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  rList = sqlite3E
2cfd1 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
2cfd2 45 4c 69 73 74 2c 20 45 58 50 52 44 55 50 5f 52  EList, EXPRDUP_R
2cfd3 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54 72 69  EDUCE);.    pTri
2cfd4 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66  ggerStep->orconf
2cfd5 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 7d 65 6c   = orconf;.  }el
2cfd6 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 49  se{.    sqlite3I
2cfd7 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  dListDelete(db, 
2cfd8 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20 7d 0a 20 20  pColumn);.  }.  
2cfd9 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2cfda 65 6c 65 74 65 28 64 62 2c 20 70 45 4c 69 73 74  elete(db, pEList
2cfdb 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
2cfdc 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65  ctDelete(db, pSe
2cfdd 6c 65 63 74 29 3b 0a 0a 20 20 72 65 74 75 72 6e  lect);..  return
2cfde 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d   pTriggerStep;.}
2cfdf 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63  ../*.** Construc
2cfe0 74 20 61 20 74 72 69 67 67 65 72 20 73 74 65 70  t a trigger step
2cfe1 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
2cfe2 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65   an UPDATE state
2cfe3 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0a  ment and return.
2cfe4 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
2cfe5 74 68 61 74 20 74 72 69 67 67 65 72 20 73 74 65  that trigger ste
2cfe6 70 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63  p.  The parser c
2cfe7 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
2cfe8 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65 65  e when it.** see
2cfe9 73 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74  s an UPDATE stat
2cfea 65 6d 65 6e 74 20 69 6e 73 69 64 65 20 74 68 65  ement inside the
2cfeb 20 62 6f 64 79 20 6f 66 20 61 20 43 52 45 41 54   body of a CREAT
2cfec 45 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51  E TRIGGER..*/.SQ
2cfed 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69  LITE_PRIVATE Tri
2cfee 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
2cfef 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74  3TriggerUpdateSt
2cff0 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ep(.  sqlite3 *d
2cff1 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b,         /* Th
2cff2 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
2cff3 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ction */.  Token
2cff4 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20   *pTableName,   
2cff5 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
2cff6 61 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61 74  able to be updat
2cff7 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ed */.  ExprList
2cff8 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 2f 2a 20   *pEList,    /* 
2cff9 54 68 65 20 53 45 54 20 63 6c 61 75 73 65 3a 20  The SET clause: 
2cffa 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 20 61  list of column a
2cffb 6e 64 20 6e 65 77 20 76 61 6c 75 65 73 20 2a 2f  nd new values */
2cffc 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c  .  Expr *pWhere,
2cffd 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
2cffe 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2cfff 20 75 38 20 6f 72 63 6f 6e 66 20 20 20 20 20 20   u8 orconf      
2d000 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e        /* The con
2d001 66 6c 69 63 74 20 61 6c 67 6f 72 69 74 68 6d 2e  flict algorithm.
2d002 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49   (OE_Abort, OE_I
2d003 67 6e 6f 72 65 2c 20 65 74 63 29 20 2a 2f 0a 29  gnore, etc) */.)
2d004 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  {.  TriggerStep 
2d005 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 0a  *pTriggerStep;..
2d006 20 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d    pTriggerStep =
2d007 20 74 72 69 67 67 65 72 53 74 65 70 41 6c 6c 6f   triggerStepAllo
2d008 63 61 74 65 28 64 62 2c 20 54 4b 5f 55 50 44 41  cate(db, TK_UPDA
2d009 54 45 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b  TE, pTableName);
2d00a 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53  .  if( pTriggerS
2d00b 74 65 70 20 29 7b 0a 20 20 20 20 70 54 72 69 67  tep ){.    pTrig
2d00c 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  gerStep->pExprLi
2d00d 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
2d00e 4c 69 73 74 44 75 70 28 64 62 2c 20 70 45 4c 69  ListDup(db, pELi
2d00f 73 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  st, EXPRDUP_REDU
2d010 43 45 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65  CE);.    pTrigge
2d011 72 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d 20  rStep->pWhere = 
2d012 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2d013 62 2c 20 70 57 68 65 72 65 2c 20 45 58 50 52 44  b, pWhere, EXPRD
2d014 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20  UP_REDUCE);.    
2d015 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72  pTriggerStep->or
2d016 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20  conf = orconf;. 
2d017 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
2d018 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2d019 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  EList);.  sqlite
2d01a 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2d01b 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72  pWhere);.  retur
2d01c 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a  n pTriggerStep;.
2d01d 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75  }../*.** Constru
2d01e 63 74 20 61 20 74 72 69 67 67 65 72 20 73 74 65  ct a trigger ste
2d01f 70 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  p that implement
2d020 73 20 61 20 44 45 4c 45 54 45 20 73 74 61 74 65  s a DELETE state
2d021 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0a  ment and return.
2d022 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
2d023 74 68 61 74 20 74 72 69 67 67 65 72 20 73 74 65  that trigger ste
2d024 70 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63  p.  The parser c
2d025 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
2d026 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65 65  e when it.** see
2d027 73 20 61 20 44 45 4c 45 54 45 20 73 74 61 74 65  s a DELETE state
2d028 6d 65 6e 74 20 69 6e 73 69 64 65 20 74 68 65 20  ment inside the 
2d029 62 6f 64 79 20 6f 66 20 61 20 43 52 45 41 54 45  body of a CREATE
2d02a 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c   TRIGGER..*/.SQL
2d02b 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67  ITE_PRIVATE Trig
2d02c 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
2d02d 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65  TriggerDeleteSte
2d02e 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  p(.  sqlite3 *db
2d02f 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2d030 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
2d031 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ion */.  Token *
2d032 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20  pTableName,     
2d033 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72   /* The table fr
2d034 6f 6d 20 77 68 69 63 68 20 72 6f 77 73 20 61 72  om which rows ar
2d035 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 45  e deleted */.  E
2d036 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 20  xpr *pWhere     
2d037 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
2d038 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 29 7b  ERE clause */.){
2d039 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
2d03a 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 0a 20  pTriggerStep;.. 
2d03b 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20   pTriggerStep = 
2d03c 74 72 69 67 67 65 72 53 74 65 70 41 6c 6c 6f 63  triggerStepAlloc
2d03d 61 74 65 28 64 62 2c 20 54 4b 5f 44 45 4c 45 54  ate(db, TK_DELET
2d03e 45 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a  E, pTableName);.
2d03f 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74    if( pTriggerSt
2d040 65 70 20 29 7b 0a 20 20 20 20 70 54 72 69 67 67  ep ){.    pTrigg
2d041 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d  erStep->pWhere =
2d042 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2d043 64 62 2c 20 70 57 68 65 72 65 2c 20 45 58 50 52  db, pWhere, EXPR
2d044 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
2d045 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
2d046 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66 61 75  rconf = OE_Defau
2d047 6c 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  lt;.  }.  sqlite
2d048 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2d049 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72  pWhere);.  retur
2d04a 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a  n pTriggerStep;.
2d04b 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 63 75 72 73  }../* .** Recurs
2d04c 69 76 65 6c 79 20 64 65 6c 65 74 65 20 61 20 54  ively delete a T
2d04d 72 69 67 67 65 72 20 73 74 72 75 63 74 75 72 65  rigger structure
2d04e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2d04f 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
2d050 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c  eleteTrigger(sql
2d051 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67 67 65  ite3 *db, Trigge
2d052 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20  r *pTrigger){.  
2d053 69 66 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20  if( pTrigger==0 
2d054 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
2d055 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
2d056 53 74 65 70 28 64 62 2c 20 70 54 72 69 67 67 65  Step(db, pTrigge
2d057 72 2d 3e 73 74 65 70 5f 6c 69 73 74 29 3b 0a 20  r->step_list);. 
2d058 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2d059 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61  b, pTrigger->zNa
2d05a 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  me);.  sqlite3Db
2d05b 46 72 65 65 28 64 62 2c 20 70 54 72 69 67 67 65  Free(db, pTrigge
2d05c 72 2d 3e 74 61 62 6c 65 29 3b 0a 20 20 73 71 6c  r->table);.  sql
2d05d 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2d05e 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68  b, pTrigger->pWh
2d05f 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64  en);.  sqlite3Id
2d060 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2d061 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d 6e  Trigger->pColumn
2d062 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  s);.  sqlite3DbF
2d063 72 65 65 28 64 62 2c 20 70 54 72 69 67 67 65 72  ree(db, pTrigger
2d064 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
2d065 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
2d066 6c 65 64 20 74 6f 20 64 72 6f 70 20 61 20 74 72  led to drop a tr
2d067 69 67 67 65 72 20 66 72 6f 6d 20 74 68 65 20 64  igger from the d
2d068 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
2d069 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 79 20  .**.** This may 
2d06a 62 65 20 63 61 6c 6c 65 64 20 64 69 72 65 63 74  be called direct
2d06b 6c 79 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73  ly from the pars
2d06c 65 72 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65  er and therefore
2d06d 20 69 64 65 6e 74 69 66 69 65 73 0a 2a 2a 20 74   identifies.** t
2d06e 68 65 20 74 72 69 67 67 65 72 20 62 79 20 6e 61  he trigger by na
2d06f 6d 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  me.  The sqlite3
2d070 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 29  DropTriggerPtr()
2d071 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68   routine does th
2d072 65 0a 2a 2a 20 73 61 6d 65 20 6a 6f 62 20 61 73  e.** same job as
2d073 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78   this routine ex
2d074 63 65 70 74 20 69 74 20 74 61 6b 65 73 20 61 20  cept it takes a 
2d075 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74  pointer to the t
2d076 72 69 67 67 65 72 0a 2a 2a 20 69 6e 73 74 65 61  rigger.** instea
2d077 64 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  d of the trigger
2d078 20 6e 61 6d 65 2e 0a 2a 2a 2f 0a 53 51 4c 49 54   name..**/.SQLIT
2d079 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2d07a 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65  qlite3DropTrigge
2d07b 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  r(Parse *pParse,
2d07c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c   SrcList *pName,
2d07d 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54   int noErr){.  T
2d07e 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
2d07f 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20   = 0;.  int i;. 
2d080 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
2d081 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2d082 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61  zName;.  int nNa
2d083 6d 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  me;.  sqlite3 *d
2d084 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2d085 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2d086 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 64  cFailed ) goto d
2d087 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61  rop_trigger_clea
2d088 6e 75 70 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  nup;.  if( SQLIT
2d089 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61  E_OK!=sqlite3Rea
2d08a 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
2d08b 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 72 6f 70  ){.    goto drop
2d08c 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70  _trigger_cleanup
2d08d 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
2d08e 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20   pName->nSrc==1 
2d08f 29 3b 0a 20 20 7a 44 62 20 3d 20 70 4e 61 6d 65  );.  zDb = pName
2d090 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65  ->a[0].zDatabase
2d091 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 4e 61 6d  ;.  zName = pNam
2d092 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20  e->a[0].zName;. 
2d093 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
2d094 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b  Strlen30(zName);
2d095 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45  .  for(i=OMIT_TE
2d096 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  MPDB; i<db->nDb;
2d097 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a   i++){.    int j
2d098 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a   = (i<2) ? i^1 :
2d099 20 69 3b 20 20 2f 2a 20 53 65 61 72 63 68 20 54   i;  /* Search T
2d09a 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20  EMP before MAIN 
2d09b 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 62 20 26  */.    if( zDb &
2d09c 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
2d09d 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d  (db->aDb[j].zNam
2d09e 65 2c 20 7a 44 62 29 20 29 20 63 6f 6e 74 69 6e  e, zDb) ) contin
2d09f 75 65 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  ue;.    pTrigger
2d0a0 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69   = sqlite3HashFi
2d0a1 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e  nd(&(db->aDb[j].
2d0a2 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73  pSchema->trigHas
2d0a3 68 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  h), zName, nName
2d0a4 29 3b 0a 20 20 20 20 69 66 28 20 70 54 72 69 67  );.    if( pTrig
2d0a5 67 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  ger ) break;.  }
2d0a6 0a 20 20 69 66 28 20 21 70 54 72 69 67 67 65 72  .  if( !pTrigger
2d0a7 20 29 7b 0a 20 20 20 20 69 66 28 20 21 6e 6f 45   ){.    if( !noE
2d0a8 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rr ){.      sqli
2d0a9 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2d0aa 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 74 72 69  se, "no such tri
2d0ab 67 67 65 72 3a 20 25 53 22 2c 20 70 4e 61 6d 65  gger: %S", pName
2d0ac 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
2d0ad 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65  goto drop_trigge
2d0ae 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  r_cleanup;.  }. 
2d0af 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
2d0b0 67 65 72 50 74 72 28 70 50 61 72 73 65 2c 20 70  gerPtr(pParse, p
2d0b1 54 72 69 67 67 65 72 29 3b 0a 0a 64 72 6f 70 5f  Trigger);..drop_
2d0b2 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3a  trigger_cleanup:
2d0b3 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
2d0b4 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d  tDelete(db, pNam
2d0b5 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  e);.}../*.** Ret
2d0b6 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
2d0b7 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
2d0b8 74 75 72 65 20 66 6f 72 20 74 68 65 20 74 61 62  ture for the tab
2d0b9 6c 65 20 74 68 61 74 20 61 20 74 72 69 67 67 65  le that a trigge
2d0ba 72 0a 2a 2a 20 69 73 20 73 65 74 20 6f 6e 2e 0a  r.** is set on..
2d0bb 2a 2f 0a 73 74 61 74 69 63 20 54 61 62 6c 65 20  */.static Table 
2d0bc 2a 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28  *tableOfTrigger(
2d0bd 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
2d0be 72 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 71  r){.  int n = sq
2d0bf 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54  lite3Strlen30(pT
2d0c0 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0a  rigger->table);.
2d0c1 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
2d0c2 48 61 73 68 46 69 6e 64 28 26 70 54 72 69 67 67  HashFind(&pTrigg
2d0c3 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 2d 3e  er->pTabSchema->
2d0c4 74 62 6c 48 61 73 68 2c 20 70 54 72 69 67 67 65  tblHash, pTrigge
2d0c5 72 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a 7d 0a  r->table, n);.}.
2d0c6 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 74  ../*.** Drop a t
2d0c7 72 69 67 67 65 72 20 67 69 76 65 6e 20 61 20 70  rigger given a p
2d0c8 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 74  ointer to that t
2d0c9 72 69 67 67 65 72 2e 20 0a 2a 2f 0a 53 51 4c 49  rigger. .*/.SQLI
2d0ca 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2d0cb 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67  sqlite3DropTrigg
2d0cc 65 72 50 74 72 28 50 61 72 73 65 20 2a 70 50 61  erPtr(Parse *pPa
2d0cd 72 73 65 2c 20 54 72 69 67 67 65 72 20 2a 70 54  rse, Trigger *pT
2d0ce 72 69 67 67 65 72 29 7b 0a 20 20 54 61 62 6c 65  rigger){.  Table
2d0cf 20 20 20 2a 70 54 61 62 6c 65 3b 0a 20 20 56 64     *pTable;.  Vd
2d0d0 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33  be *v;.  sqlite3
2d0d1 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2d0d2 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20  b;.  int iDb;.. 
2d0d3 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
2d0d4 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
2d0d5 73 65 2d 3e 64 62 2c 20 70 54 72 69 67 67 65 72  se->db, pTrigger
2d0d6 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73  ->pSchema);.  as
2d0d7 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
2d0d8 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
2d0d9 20 70 54 61 62 6c 65 20 3d 20 74 61 62 6c 65 4f   pTable = tableO
2d0da 66 54 72 69 67 67 65 72 28 70 54 72 69 67 67 65  fTrigger(pTrigge
2d0db 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54  r);.  assert( pT
2d0dc 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74  able );.  assert
2d0dd 28 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d  ( pTable->pSchem
2d0de 61 3d 3d 70 54 72 69 67 67 65 72 2d 3e 70 53 63  a==pTrigger->pSc
2d0df 68 65 6d 61 20 7c 7c 20 69 44 62 3d 3d 31 20 29  hema || iDb==1 )
2d0e0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2d0e1 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
2d0e2 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20  ION.  {.    int 
2d0e3 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
2d0e4 4f 50 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20  OP_TRIGGER;.    
2d0e5 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
2d0e6 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
2d0e7 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Name;.    const 
2d0e8 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48  char *zTab = SCH
2d0e9 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a  EMA_TABLE(iDb);.
2d0ea 20 20 20 20 69 66 28 20 69 44 62 3d 3d 31 20 29      if( iDb==1 )
2d0eb 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
2d0ec 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52  ROP_TEMP_TRIGGER
2d0ed 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
2d0ee 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
2d0ef 65 2c 20 63 6f 64 65 2c 20 70 54 72 69 67 67 65  e, code, pTrigge
2d0f0 72 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 6c 65  r->zName, pTable
2d0f1 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 7c 7c  ->zName, zDb) ||
2d0f2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 75  .      sqlite3Au
2d0f3 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2d0f4 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a  SQLITE_DELETE, z
2d0f5 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  Tab, 0, zDb) ){.
2d0f6 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2d0f7 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
2d0f8 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
2d0f9 64 65 20 74 6f 20 64 65 73 74 72 6f 79 20 74 68  de to destroy th
2d0fa 65 20 64 61 74 61 62 61 73 65 20 72 65 63 6f 72  e database recor
2d0fb 64 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  d of the trigger
2d0fc 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
2d0fd 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20   pTable!=0 );.  
2d0fe 69 66 28 20 28 76 20 3d 20 73 71 6c 69 74 65 33  if( (v = sqlite3
2d0ff 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 29  GetVdbe(pParse))
2d100 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 62  !=0 ){.    int b
2d101 61 73 65 3b 0a 20 20 20 20 73 74 61 74 69 63 20  ase;.    static 
2d102 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
2d103 20 64 72 6f 70 54 72 69 67 67 65 72 5b 5d 20 3d   dropTrigger[] =
2d104 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65   {.      { OP_Re
2d105 77 69 6e 64 2c 20 20 20 20 20 30 2c 20 41 44 44  wind,     0, ADD
2d106 52 28 39 29 2c 20 20 30 7d 2c 0a 20 20 20 20 20  R(9),  0},.     
2d107 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
2d108 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30    0, 1,        0
2d109 7d 2c 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20  }, /* 1 */.     
2d10a 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20   { OP_Column,   
2d10b 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 32    0, 1,        2
2d10c 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65  },.      { OP_Ne
2d10d 2c 20 20 20 20 20 20 20 20 20 32 2c 20 41 44 44  ,         2, ADD
2d10e 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20  R(8),  1},.     
2d10f 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
2d110 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30    0, 1,        0
2d111 7d 2c 20 2f 2a 20 34 3a 20 22 74 72 69 67 67 65  }, /* 4: "trigge
2d112 72 22 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50  r" */.      { OP
2d113 5f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 30 2c 20  _Column,     0, 
2d114 30 2c 20 20 20 20 20 20 20 20 32 7d 2c 0a 20 20  0,        2},.  
2d115 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20      { OP_Ne,    
2d116 20 20 20 20 20 32 2c 20 41 44 44 52 28 38 29 2c       2, ADDR(8),
2d117 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    1},.      { OP
2d118 5f 44 65 6c 65 74 65 2c 20 20 20 20 20 30 2c 20  _Delete,     0, 
2d119 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20  0,        0},.  
2d11a 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 20      { OP_Next,  
2d11b 20 20 20 20 20 30 2c 20 41 44 44 52 28 31 29 2c       0, ADDR(1),
2d11c 20 20 30 7d 2c 20 2f 2a 20 38 20 2a 2f 0a 20 20    0}, /* 8 */.  
2d11d 20 20 7d 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65    };..    sqlite
2d11e 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
2d11f 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20  tion(pParse, 0, 
2d120 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
2d121 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65  3OpenMasterTable
2d122 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
2d123 20 20 20 62 61 73 65 20 3d 20 73 71 6c 69 74 65     base = sqlite
2d124 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
2d125 2c 20 20 41 72 72 61 79 53 69 7a 65 28 64 72 6f  ,  ArraySize(dro
2d126 70 54 72 69 67 67 65 72 29 2c 20 64 72 6f 70 54  pTrigger), dropT
2d127 72 69 67 67 65 72 29 3b 0a 20 20 20 20 73 71 6c  rigger);.    sql
2d128 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
2d129 28 76 2c 20 62 61 73 65 2b 31 2c 20 70 54 72 69  (v, base+1, pTri
2d12a 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  gger->zName, 0);
2d12b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2d12c 43 68 61 6e 67 65 50 34 28 76 2c 20 62 61 73 65  ChangeP4(v, base
2d12d 2b 34 2c 20 22 74 72 69 67 67 65 72 22 2c 20 50  +4, "trigger", P
2d12e 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  4_STATIC);.    s
2d12f 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
2d130 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
2d131 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2d132 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
2d133 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  se, 0, 0);.    s
2d134 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2d135 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67  (v, OP_DropTrigg
2d136 65 72 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70  er, iDb, 0, 0, p
2d137 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20  Trigger->zName, 
2d138 30 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72  0);.    if( pPar
2d139 73 65 2d 3e 6e 4d 65 6d 3c 33 20 29 7b 0a 20 20  se->nMem<3 ){.  
2d13a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
2d13b 20 3d 20 33 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 3;.    }.  }.
2d13c 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
2d13d 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20 74  a trigger from t
2d13e 68 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 6f  he hash tables o
2d13f 66 20 74 68 65 20 73 71 6c 69 74 65 2a 20 70 6f  f the sqlite* po
2d140 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
2d141 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2d142 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
2d143 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69  leteTrigger(sqli
2d144 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
2d145 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
2d146 61 6d 65 29 7b 0a 20 20 48 61 73 68 20 2a 70 48  ame){.  Hash *pH
2d147 61 73 68 20 3d 20 26 28 64 62 2d 3e 61 44 62 5b  ash = &(db->aDb[
2d148 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72  iDb].pSchema->tr
2d149 69 67 48 61 73 68 29 3b 0a 20 20 54 72 69 67 67  igHash);.  Trigg
2d14a 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a 20 20  er *pTrigger;.  
2d14b 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  pTrigger = sqlit
2d14c 65 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61  e3HashInsert(pHa
2d14d 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74  sh, zName, sqlit
2d14e 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
2d14f 29 2c 20 30 29 3b 0a 20 20 69 66 28 20 41 4c 57  ), 0);.  if( ALW
2d150 41 59 53 28 70 54 72 69 67 67 65 72 29 20 29 7b  AYS(pTrigger) ){
2d151 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67 65  .    if( pTrigge
2d152 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 72 69  r->pSchema==pTri
2d153 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61  gger->pTabSchema
2d154 20 29 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20   ){.      Table 
2d155 2a 70 54 61 62 20 3d 20 74 61 62 6c 65 4f 66 54  *pTab = tableOfT
2d156 72 69 67 67 65 72 28 70 54 72 69 67 67 65 72 29  rigger(pTrigger)
2d157 3b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 20  ;.      Trigger 
2d158 2a 2a 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28  **pp;.      for(
2d159 70 70 3d 26 70 54 61 62 2d 3e 70 54 72 69 67 67  pp=&pTab->pTrigg
2d15a 65 72 3b 20 2a 70 70 21 3d 70 54 72 69 67 67 65  er; *pp!=pTrigge
2d15b 72 3b 20 70 70 3d 26 28 28 2a 70 70 29 2d 3e 70  r; pp=&((*pp)->p
2d15c 4e 65 78 74 29 29 3b 0a 20 20 20 20 20 20 2a 70  Next));.      *p
2d15d 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74  p = (*pp)->pNext
2d15e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2d15f 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
2d160 28 64 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a  (db, pTrigger);.
2d161 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
2d162 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
2d163 61 6e 67 65 73 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  anges;.  }.}../*
2d164 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73 20 74 68  .** pEList is th
2d165 65 20 53 45 54 20 63 6c 61 75 73 65 20 6f 66 20  e SET clause of 
2d166 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
2d167 65 6e 74 2e 20 20 45 61 63 68 20 65 6e 74 72 79  ent.  Each entry
2d168 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74 20 69 73  .** in pEList is
2d169 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 3c   of the format <
2d16a 69 64 3e 3d 3c 65 78 70 72 3e 2e 20 20 49 66 20  id>=<expr>.  If 
2d16b 61 6e 79 20 6f 66 20 74 68 65 20 65 6e 74 72 69  any of the entri
2d16c 65 73 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74 20  es.** in pEList 
2d16d 68 61 76 65 20 61 6e 20 3c 69 64 3e 20 77 68 69  have an <id> whi
2d16e 63 68 20 6d 61 74 63 68 65 73 20 61 6e 20 69 64  ch matches an id
2d16f 65 6e 74 69 66 69 65 72 20 69 6e 20 70 49 64 4c  entifier in pIdL
2d170 69 73 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74  ist,.** then ret
2d171 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20 70 49  urn TRUE.  If pI
2d172 64 4c 69 73 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65  dList==NULL, the
2d173 6e 20 69 74 20 69 73 20 63 6f 6e 73 69 64 65 72  n it is consider
2d174 65 64 20 61 0a 2a 2a 20 77 69 6c 64 63 61 72 64  ed a.** wildcard
2d175 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 61 6e   that matches an
2d176 79 74 68 69 6e 67 2e 20 20 4c 69 6b 65 77 69 73  ything.  Likewis
2d177 65 20 69 66 20 70 45 4c 69 73 74 3d 3d 4e 55 4c  e if pEList==NUL
2d178 4c 20 74 68 65 6e 0a 2a 2a 20 69 74 20 6d 61 74  L then.** it mat
2d179 63 68 65 73 20 61 6e 79 74 68 69 6e 67 20 73 6f  ches anything so
2d17a 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 74   always return t
2d17b 72 75 65 2e 20 20 52 65 74 75 72 6e 20 66 61 6c  rue.  Return fal
2d17c 73 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 74 68  se only.** if th
2d17d 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e  ere is no match.
2d17e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
2d17f 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61  heckColumnOverla
2d180 70 28 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73  p(IdList *pIdLis
2d181 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  t, ExprList *pEL
2d182 69 73 74 29 7b 0a 20 20 69 6e 74 20 65 3b 0a 20  ist){.  int e;. 
2d183 20 69 66 28 20 70 49 64 4c 69 73 74 3d 3d 30 20   if( pIdList==0 
2d184 7c 7c 20 4e 45 56 45 52 28 70 45 4c 69 73 74 3d  || NEVER(pEList=
2d185 3d 30 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  =0) ) return 1;.
2d186 20 20 66 6f 72 28 65 3d 30 3b 20 65 3c 70 45 4c    for(e=0; e<pEL
2d187 69 73 74 2d 3e 6e 45 78 70 72 3b 20 65 2b 2b 29  ist->nExpr; e++)
2d188 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
2d189 33 49 64 4c 69 73 74 49 6e 64 65 78 28 70 49 64  3IdListIndex(pId
2d18a 4c 69 73 74 2c 20 70 45 4c 69 73 74 2d 3e 61 5b  List, pEList->a[
2d18b 65 5d 2e 7a 4e 61 6d 65 29 3e 3d 30 20 29 20 72  e].zName)>=0 ) r
2d18c 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72  eturn 1;.  }.  r
2d18d 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a  eturn 0; .}../*.
2d18e 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69 73 74  ** Return a list
2d18f 20 6f 66 20 61 6c 6c 20 74 72 69 67 67 65 72 73   of all triggers
2d190 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20 69   on table pTab i
2d191 66 20 74 68 65 72 65 20 65 78 69 73 74 73 20 61  f there exists a
2d192 74 20 6c 65 61 73 74 0a 2a 2a 20 6f 6e 65 20 74  t least.** one t
2d193 72 69 67 67 65 72 20 74 68 61 74 20 6d 75 73 74  rigger that must
2d194 20 62 65 20 66 69 72 65 64 20 77 68 65 6e 20 61   be fired when a
2d195 6e 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74  n operation of t
2d196 79 70 65 20 27 6f 70 27 20 69 73 20 0a 2a 2a 20  ype 'op' is .** 
2d197 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65  performed on the
2d198 20 74 61 62 6c 65 2c 20 61 6e 64 2c 20 69 66 20   table, and, if 
2d199 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 69  that operation i
2d19a 73 20 61 6e 20 55 50 44 41 54 45 2c 20 69 66 20  s an UPDATE, if 
2d19b 61 74 0a 2a 2a 20 6c 65 61 73 74 20 6f 6e 65 20  at.** least one 
2d19c 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 69  of the columns i
2d19d 6e 20 70 43 68 61 6e 67 65 73 20 69 73 20 62 65  n pChanges is be
2d19e 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2f  ing modified..*/
2d19f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2d1a0 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33  Trigger *sqlite3
2d1a1 54 72 69 67 67 65 72 73 45 78 69 73 74 28 0a 20  TriggersExist(. 
2d1a2 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2d1a3 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2d1a4 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  e context */.  T
2d1a5 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
2d1a6 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
2d1a7 62 6c 65 20 74 68 65 20 63 6f 6e 74 61 69 6e 73  ble the contains
2d1a8 20 74 68 65 20 74 72 69 67 67 65 72 73 20 2a 2f   the triggers */
2d1a9 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20  .  int op,      
2d1aa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e             /* on
2d1ab 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20  e of TK_DELETE, 
2d1ac 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55 50  TK_INSERT, TK_UP
2d1ad 44 41 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69  DATE */.  ExprLi
2d1ae 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 20  st *pChanges,   
2d1af 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 74 68 61    /* Columns tha
2d1b0 74 20 63 68 61 6e 67 65 20 69 6e 20 61 6e 20 55  t change in an U
2d1b1 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  PDATE statement 
2d1b2 2a 2f 0a 20 20 69 6e 74 20 2a 70 4d 61 73 6b 20  */.  int *pMask 
2d1b3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d1b4 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20 54 52 49  OUT: Mask of TRI
2d1b5 47 47 45 52 5f 42 45 46 4f 52 45 7c 54 52 49 47  GGER_BEFORE|TRIG
2d1b6 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 29 7b 0a  GER_AFTER */.){.
2d1b7 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20 30 3b 0a    int mask = 0;.
2d1b8 20 20 54 72 69 67 67 65 72 20 2a 70 4c 69 73 74    Trigger *pList
2d1b9 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
2d1ba 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54  rList(pParse, pT
2d1bb 61 62 29 3b 0a 20 20 54 72 69 67 67 65 72 20 2a  ab);.  Trigger *
2d1bc 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69  p;.  assert( pLi
2d1bd 73 74 3d 3d 30 20 7c 7c 20 49 73 56 69 72 74 75  st==0 || IsVirtu
2d1be 61 6c 28 70 54 61 62 29 3d 3d 30 20 29 3b 0a 20  al(pTab)==0 );. 
2d1bf 20 66 6f 72 28 70 3d 70 4c 69 73 74 3b 20 70 3b   for(p=pList; p;
2d1c0 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
2d1c1 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 6f 70 20    if( p->op==op 
2d1c2 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76  && checkColumnOv
2d1c3 65 72 6c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e  erlap(p->pColumn
2d1c4 73 2c 20 70 43 68 61 6e 67 65 73 29 20 29 7b 0a  s, pChanges) ){.
2d1c5 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20 70 2d        mask |= p-
2d1c6 3e 74 72 5f 74 6d 3b 0a 20 20 20 20 7d 0a 20 20  >tr_tm;.    }.  
2d1c7 7d 0a 20 20 69 66 28 20 70 4d 61 73 6b 20 29 7b  }.  if( pMask ){
2d1c8 0a 20 20 20 20 2a 70 4d 61 73 6b 20 3d 20 6d 61  .    *pMask = ma
2d1c9 73 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  sk;.  }.  return
2d1ca 20 28 6d 61 73 6b 20 3f 20 70 4c 69 73 74 20 3a   (mask ? pList :
2d1cb 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   0);.}../*.** Co
2d1cc 6e 76 65 72 74 20 74 68 65 20 70 53 74 65 70 2d  nvert the pStep-
2d1cd 3e 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 69 6e  >target token in
2d1ce 74 6f 20 61 20 53 72 63 4c 69 73 74 20 61 6e 64  to a SrcList and
2d1cf 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
2d1d0 72 0a 2a 2a 20 74 6f 20 74 68 61 74 20 53 72 63  r.** to that Src
2d1d1 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  List..**.** This
2d1d2 20 72 6f 75 74 69 6e 65 20 61 64 64 73 20 61 20   routine adds a 
2d1d3 73 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73  specific databas
2d1d4 65 20 6e 61 6d 65 2c 20 69 66 20 6e 65 65 64 65  e name, if neede
2d1d5 64 2c 20 74 6f 20 74 68 65 20 74 61 72 67 65 74  d, to the target
2d1d6 20 77 68 65 6e 0a 2a 2a 20 66 6f 72 6d 69 6e 67   when.** forming
2d1d7 20 74 68 65 20 53 72 63 4c 69 73 74 2e 20 20 54   the SrcList.  T
2d1d8 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 74  his prevents a t
2d1d9 72 69 67 67 65 72 20 69 6e 20 6f 6e 65 20 64 61  rigger in one da
2d1da 74 61 62 61 73 65 20 66 72 6f 6d 0a 2a 2a 20 72  tabase from.** r
2d1db 65 66 65 72 72 69 6e 67 20 74 6f 20 61 20 74 61  eferring to a ta
2d1dc 72 67 65 74 20 69 6e 20 61 6e 6f 74 68 65 72 20  rget in another 
2d1dd 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 65 78  database.  An ex
2d1de 63 65 70 74 69 6f 6e 20 69 73 20 77 68 65 6e 20  ception is when 
2d1df 74 68 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 69  the.** trigger i
2d1e0 73 20 69 6e 20 54 45 4d 50 20 69 6e 20 77 68 69  s in TEMP in whi
2d1e1 63 68 20 63 61 73 65 20 69 74 20 63 61 6e 20 72  ch case it can r
2d1e2 65 66 65 72 20 74 6f 20 61 6e 79 20 6f 74 68 65  efer to any othe
2d1e3 72 20 64 61 74 61 62 61 73 65 20 69 74 0a 2a 2a  r database it.**
2d1e4 20 77 61 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69   wants..*/.stati
2d1e5 63 20 53 72 63 4c 69 73 74 20 2a 74 61 72 67 65  c SrcList *targe
2d1e6 74 53 72 63 4c 69 73 74 28 0a 20 20 50 61 72 73  tSrcList(.  Pars
2d1e7 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2d1e8 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
2d1e9 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69  context */.  Tri
2d1ea 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20  ggerStep *pStep 
2d1eb 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72    /* The trigger
2d1ec 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2d1ed 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 2a 2f 0a  target token */.
2d1ee 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  ){.  int iDb;   
2d1ef 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
2d1f0 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
2d1f1 73 65 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 53  se to use */.  S
2d1f2 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20  rcList *pSrc;   
2d1f3 20 20 20 20 2f 2a 20 53 72 63 4c 69 73 74 20 74      /* SrcList t
2d1f4 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f  o be returned */
2d1f5 0a 0a 20 20 70 53 72 63 20 3d 20 73 71 6c 69 74  ..  pSrc = sqlit
2d1f6 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
2d1f7 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 20 26  pParse->db, 0, &
2d1f8 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2c 20 30  pStep->target, 0
2d1f9 29 3b 0a 20 20 69 66 28 20 70 53 72 63 20 29 7b  );.  if( pSrc ){
2d1fa 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72  .    assert( pSr
2d1fb 63 2d 3e 6e 53 72 63 3e 30 20 29 3b 0a 20 20 20  c->nSrc>0 );.   
2d1fc 20 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 61   assert( pSrc->a
2d1fd 21 3d 30 20 29 3b 0a 20 20 20 20 69 44 62 20 3d  !=0 );.    iDb =
2d1fe 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2d1ff 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
2d200 2c 20 70 53 74 65 70 2d 3e 70 54 72 69 67 2d 3e  , pStep->pTrig->
2d201 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 69 66  pSchema);.    if
2d202 28 20 69 44 62 3d 3d 30 20 7c 7c 20 69 44 62 3e  ( iDb==0 || iDb>
2d203 3d 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =2 ){.      sqli
2d204 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2d205 2d 3e 64 62 3b 0a 20 20 20 20 20 20 61 73 73 65  ->db;.      asse
2d206 72 74 28 20 69 44 62 3c 70 50 61 72 73 65 2d 3e  rt( iDb<pParse->
2d207 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 20  db->nDb );.     
2d208 20 70 53 72 63 2d 3e 61 5b 70 53 72 63 2d 3e 6e   pSrc->a[pSrc->n
2d209 53 72 63 2d 31 5d 2e 7a 44 61 74 61 62 61 73 65  Src-1].zDatabase
2d20a 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
2d20b 75 70 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 69  up(db, db->aDb[i
2d20c 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  Db].zName);.    
2d20d 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
2d20e 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Src;.}../*.** Ge
2d20f 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64 65  nerate VDBE code
2d210 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65   for the stateme
2d211 6e 74 73 20 69 6e 73 69 64 65 20 74 68 65 20 62  nts inside the b
2d212 6f 64 79 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  ody of a single 
2d213 0a 2a 2a 20 74 72 69 67 67 65 72 2e 0a 2a 2f 0a  .** trigger..*/.
2d214 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 54  static int codeT
2d215 72 69 67 67 65 72 50 72 6f 67 72 61 6d 28 0a 20  riggerProgram(. 
2d216 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2d217 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2d218 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
2d219 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65   */.  TriggerSte
2d21a 70 20 2a 70 53 74 65 70 4c 69 73 74 2c 20 20 20  p *pStepList,   
2d21b 2f 2a 20 4c 69 73 74 20 6f 66 20 73 74 61 74 65  /* List of state
2d21c 6d 65 6e 74 73 20 69 6e 73 69 64 65 20 74 68 65  ments inside the
2d21d 20 74 72 69 67 67 65 72 20 62 6f 64 79 20 2a 2f   trigger body */
2d21e 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20  .  int orconf   
2d21f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2d220 43 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69 74  Conflict algorit
2d221 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c 20 65  hm. (OE_Abort, e
2d222 74 63 29 20 2a 2f 20 20 0a 29 7b 0a 20 20 54 72  tc) */  .){.  Tr
2d223 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70  iggerStep *pStep
2d224 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  ;.  Vdbe *v = pP
2d225 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73  arse->pVdbe;.  s
2d226 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2d227 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65  rse->db;..  asse
2d228 72 74 28 20 70 50 61 72 73 65 2d 3e 70 54 72 69  rt( pParse->pTri
2d229 67 67 65 72 54 61 62 20 26 26 20 70 50 61 72 73  ggerTab && pPars
2d22a 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 29 3b 0a  e->pToplevel );.
2d22b 20 20 61 73 73 65 72 74 28 20 70 53 74 65 70 4c    assert( pStepL
2d22c 69 73 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ist );.  assert(
2d22d 20 76 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70   v!=0 );.  for(p
2d22e 53 74 65 70 3d 70 53 74 65 70 4c 69 73 74 3b 20  Step=pStepList; 
2d22f 70 53 74 65 70 3b 20 70 53 74 65 70 3d 70 53 74  pStep; pStep=pSt
2d230 65 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  ep->pNext){.    
2d231 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68  /* Figure out th
2d232 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  e ON CONFLICT po
2d233 6c 69 63 79 20 74 68 61 74 20 77 69 6c 6c 20 62  licy that will b
2d234 65 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20  e used for this 
2d235 73 74 65 70 0a 20 20 20 20 2a 2a 20 6f 66 20 74  step.    ** of t
2d236 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  he trigger progr
2d237 61 6d 2e 20 49 66 20 74 68 65 20 73 74 61 74 65  am. If the state
2d238 6d 65 6e 74 20 74 68 61 74 20 63 61 75 73 65 64  ment that caused
2d239 20 74 68 69 73 20 74 72 69 67 67 65 72 0a 20 20   this trigger.  
2d23a 20 20 2a 2a 20 74 6f 20 66 69 72 65 20 68 61 64    ** to fire had
2d23b 20 61 6e 20 65 78 70 6c 69 63 69 74 20 4f 4e 20   an explicit ON 
2d23c 43 4f 4e 46 4c 49 43 54 2c 20 74 68 65 6e 20 75  CONFLICT, then u
2d23d 73 65 20 69 74 2e 20 4f 74 68 65 72 77 69 73 65  se it. Otherwise
2d23e 2c 20 75 73 65 0a 20 20 20 20 2a 2a 20 74 68 65  , use.    ** the
2d23f 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c   ON CONFLICT pol
2d240 69 63 79 20 74 68 61 74 20 77 61 73 20 73 70 65  icy that was spe
2d241 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20 6f  cified as part o
2d242 66 20 74 68 65 20 74 72 69 67 67 65 72 0a 20 20  f the trigger.  
2d243 20 20 2a 2a 20 73 74 65 70 20 73 74 61 74 65 6d    ** step statem
2d244 65 6e 74 2e 20 45 78 61 6d 70 6c 65 3a 0a 20 20  ent. Example:.  
2d245 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 43 52    **.    **   CR
2d246 45 41 54 45 20 54 52 49 47 47 45 52 20 41 46 54  EATE TRIGGER AFT
2d247 45 52 20 49 4e 53 45 52 54 20 4f 4e 20 74 31 20  ER INSERT ON t1 
2d248 42 45 47 49 4e 3b 0a 20 20 20 20 2a 2a 20 20 20  BEGIN;.    **   
2d249 20 20 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c    INSERT OR REPL
2d24a 41 43 45 20 49 4e 54 4f 20 74 32 20 56 41 4c 55  ACE INTO t2 VALU
2d24b 45 53 28 6e 65 77 2e 61 2c 20 6e 65 77 2e 62 29  ES(new.a, new.b)
2d24c 3b 0a 20 20 20 20 2a 2a 20 20 20 45 4e 44 3b 0a  ;.    **   END;.
2d24d 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2d24e 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 2e  INSERT INTO t1 .
2d24f 2e 2e 20 3b 20 20 20 20 20 20 20 20 20 20 20 20  .. ;            
2d250 2d 2d 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 74  -- insert into t
2d251 32 20 75 73 65 73 20 52 45 50 4c 41 43 45 20 70  2 uses REPLACE p
2d252 6f 6c 69 63 79 0a 20 20 20 20 2a 2a 20 20 20 49  olicy.    **   I
2d253 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20  NSERT OR IGNORE 
2d254 49 4e 54 4f 20 74 31 20 2e 2e 2e 20 3b 20 20 2d  INTO t1 ... ;  -
2d255 2d 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 74 32  - insert into t2
2d256 20 75 73 65 73 20 49 47 4e 4f 52 45 20 70 6f 6c   uses IGNORE pol
2d257 69 63 79 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  icy.    */.    p
2d258 50 61 72 73 65 2d 3e 65 4f 72 63 6f 6e 66 20 3d  Parse->eOrconf =
2d259 20 28 6f 72 63 6f 6e 66 3d 3d 4f 45 5f 44 65 66   (orconf==OE_Def
2d25a 61 75 6c 74 29 3f 70 53 74 65 70 2d 3e 6f 72 63  ault)?pStep->orc
2d25b 6f 6e 66 3a 28 75 38 29 6f 72 63 6f 6e 66 3b 0a  onf:(u8)orconf;.
2d25c 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 53 74  .    switch( pSt
2d25d 65 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20  ep->op ){.      
2d25e 63 61 73 65 20 54 4b 5f 55 50 44 41 54 45 3a 20  case TK_UPDATE: 
2d25f 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2d260 33 55 70 64 61 74 65 28 70 50 61 72 73 65 2c 20  3Update(pParse, 
2d261 0a 20 20 20 20 20 20 20 20 20 20 74 61 72 67 65  .          targe
2d262 74 53 72 63 4c 69 73 74 28 70 50 61 72 73 65 2c  tSrcList(pParse,
2d263 20 70 53 74 65 70 29 2c 0a 20 20 20 20 20 20 20   pStep),.       
2d264 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2d265 73 74 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d  stDup(db, pStep-
2d266 3e 70 45 78 70 72 4c 69 73 74 2c 20 30 29 2c 20  >pExprList, 0), 
2d267 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2d268 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53  e3ExprDup(db, pS
2d269 74 65 70 2d 3e 70 57 68 65 72 65 2c 20 30 29 2c  tep->pWhere, 0),
2d26a 20 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72   .          pPar
2d26b 73 65 2d 3e 65 4f 72 63 6f 6e 66 0a 20 20 20 20  se->eOrconf.    
2d26c 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 62      );.        b
2d26d 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2d26e 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 53 45      case TK_INSE
2d26f 52 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  RT: {.        sq
2d270 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72  lite3Insert(pPar
2d271 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 74  se, .          t
2d272 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61  argetSrcList(pPa
2d273 72 73 65 2c 20 70 53 74 65 70 29 2c 0a 20 20 20  rse, pStep),.   
2d274 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2d275 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53  prListDup(db, pS
2d276 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c 20  tep->pExprList, 
2d277 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73  0), .          s
2d278 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
2d279 64 62 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65  db, pStep->pSele
2d27a 63 74 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20  ct, 0), .       
2d27b 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74     sqlite3IdList
2d27c 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70  Dup(db, pStep->p
2d27d 49 64 4c 69 73 74 29 2c 20 0a 20 20 20 20 20 20  IdList), .      
2d27e 20 20 20 20 70 50 61 72 73 65 2d 3e 65 4f 72 63      pParse->eOrc
2d27f 6f 6e 66 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  onf.        );. 
2d280 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2d281 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
2d282 20 54 4b 5f 44 45 4c 45 54 45 3a 20 7b 0a 20 20   TK_DELETE: {.  
2d283 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c        sqlite3Del
2d284 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65 2c 20  eteFrom(pParse, 
2d285 0a 20 20 20 20 20 20 20 20 20 20 74 61 72 67 65  .          targe
2d286 74 53 72 63 4c 69 73 74 28 70 50 61 72 73 65 2c  tSrcList(pParse,
2d287 20 70 53 74 65 70 29 2c 0a 20 20 20 20 20 20 20   pStep),.       
2d288 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
2d289 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 57 68  p(db, pStep->pWh
2d28a 65 72 65 2c 20 30 29 0a 20 20 20 20 20 20 20 20  ere, 0).        
2d28b 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
2d28c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2d28d 64 65 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28  default: assert(
2d28e 20 70 53 74 65 70 2d 3e 6f 70 3d 3d 54 4b 5f 53   pStep->op==TK_S
2d28f 45 4c 45 43 54 20 29 3b 20 7b 0a 20 20 20 20 20  ELECT ); {.     
2d290 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 73 44     SelectDest sD
2d291 65 73 74 3b 0a 20 20 20 20 20 20 20 20 53 65 6c  est;.        Sel
2d292 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 73  ect *pSelect = s
2d293 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
2d294 64 62 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65  db, pStep->pSele
2d295 63 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ct, 0);.        
2d296 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
2d297 74 49 6e 69 74 28 26 73 44 65 73 74 2c 20 53 52  tInit(&sDest, SR
2d298 54 5f 44 69 73 63 61 72 64 2c 20 30 29 3b 0a 20  T_Discard, 0);. 
2d299 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
2d29a 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65  lect(pParse, pSe
2d29b 6c 65 63 74 2c 20 26 73 44 65 73 74 29 3b 0a 20  lect, &sDest);. 
2d29c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
2d29d 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2d29e 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20  Select);.       
2d29f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2d2a0 20 20 20 20 7d 20 0a 20 20 20 20 69 66 28 20 70      } .    if( p
2d2a1 53 74 65 70 2d 3e 6f 70 21 3d 54 4b 5f 53 45 4c  Step->op!=TK_SEL
2d2a2 45 43 54 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ECT ){.      sql
2d2a3 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
2d2a4 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 29  , OP_ResetCount)
2d2a5 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
2d2a6 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 69 66 64  eturn 0;.}..#ifd
2d2a7 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
2d2a8 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2d2a9 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61  ion is used to a
2d2aa 64 64 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 29  dd VdbeComment()
2d2ab 20 61 6e 6e 6f 74 61 74 69 6f 6e 73 20 74 6f 20   annotations to 
2d2ac 61 20 56 44 42 45 0a 2a 2a 20 70 72 6f 67 72 61  a VDBE.** progra
2d2ad 6d 2e 20 49 74 20 69 73 20 6e 6f 74 20 75 73 65  m. It is not use
2d2ae 64 20 69 6e 20 70 72 6f 64 75 63 74 69 6f 6e 20  d in production 
2d2af 63 6f 64 65 2c 20 6f 6e 6c 79 20 66 6f 72 20 64  code, only for d
2d2b0 65 62 75 67 67 69 6e 67 2e 0a 2a 2f 0a 73 74 61  ebugging..*/.sta
2d2b1 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
2d2b2 6f 6e 45 72 72 6f 72 54 65 78 74 28 69 6e 74 20  onErrorText(int 
2d2b3 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 73 77 69 74  onError){.  swit
2d2b4 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20  ch( onError ){. 
2d2b5 20 20 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74     case OE_Abort
2d2b6 3a 20 20 20 20 72 65 74 75 72 6e 20 22 61 62 6f  :    return "abo
2d2b7 72 74 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  rt";.    case OE
2d2b8 5f 52 6f 6c 6c 62 61 63 6b 3a 20 72 65 74 75 72  _Rollback: retur
2d2b9 6e 20 22 72 6f 6c 6c 62 61 63 6b 22 3b 0a 20 20  n "rollback";.  
2d2ba 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20    case OE_Fail: 
2d2bb 20 20 20 20 72 65 74 75 72 6e 20 22 66 61 69 6c      return "fail
2d2bc 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 52  ";.    case OE_R
2d2bd 65 70 6c 61 63 65 3a 20 20 72 65 74 75 72 6e 20  eplace:  return 
2d2be 22 72 65 70 6c 61 63 65 22 3b 0a 20 20 20 20 63  "replace";.    c
2d2bf 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 20  ase OE_Ignore:  
2d2c0 20 72 65 74 75 72 6e 20 22 69 67 6e 6f 72 65 22   return "ignore"
2d2c1 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 44 65  ;.    case OE_De
2d2c2 66 61 75 6c 74 3a 20 20 72 65 74 75 72 6e 20 22  fault:  return "
2d2c3 64 65 66 61 75 6c 74 22 3b 0a 20 20 7d 0a 20 20  default";.  }.  
2d2c4 72 65 74 75 72 6e 20 22 6e 2f 61 22 3b 0a 7d 0a  return "n/a";.}.
2d2c5 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61  #endif../*.** Pa
2d2c6 72 73 65 20 63 6f 6e 74 65 78 74 20 73 74 72 75  rse context stru
2d2c7 63 74 75 72 65 20 70 46 72 6f 6d 20 68 61 73 20  cture pFrom has 
2d2c8 6a 75 73 74 20 62 65 65 6e 20 75 73 65 64 20 74  just been used t
2d2c9 6f 20 63 72 65 61 74 65 20 61 20 73 75 62 2d 76  o create a sub-v
2d2ca 64 62 65 0a 2a 2a 20 28 74 72 69 67 67 65 72 20  dbe.** (trigger 
2d2cb 70 72 6f 67 72 61 6d 29 2e 20 49 66 20 61 6e 20  program). If an 
2d2cc 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
2d2cd 65 64 2c 20 74 72 61 6e 73 66 65 72 20 65 72 72  ed, transfer err
2d2ce 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  or information.*
2d2cf 2a 20 66 72 6f 6d 20 70 46 72 6f 6d 20 74 6f 20  * from pFrom to 
2d2d0 70 54 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  pTo..*/.static v
2d2d1 6f 69 64 20 74 72 61 6e 73 66 65 72 50 61 72 73  oid transferPars
2d2d2 65 45 72 72 6f 72 28 50 61 72 73 65 20 2a 70 54  eError(Parse *pT
2d2d3 6f 2c 20 50 61 72 73 65 20 2a 70 46 72 6f 6d 29  o, Parse *pFrom)
2d2d4 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 6f  {.  assert( pFro
2d2d5 6d 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 7c 7c  m->zErrMsg==0 ||
2d2d6 20 70 46 72 6f 6d 2d 3e 6e 45 72 72 20 29 3b 0a   pFrom->nErr );.
2d2d7 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 7a    assert( pTo->z
2d2d8 45 72 72 4d 73 67 3d 3d 30 20 7c 7c 20 70 54 6f  ErrMsg==0 || pTo
2d2d9 2d 3e 6e 45 72 72 20 29 3b 0a 20 20 69 66 28 20  ->nErr );.  if( 
2d2da 70 54 6f 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a  pTo->nErr==0 ){.
2d2db 20 20 20 20 70 54 6f 2d 3e 7a 45 72 72 4d 73 67      pTo->zErrMsg
2d2dc 20 3d 20 70 46 72 6f 6d 2d 3e 7a 45 72 72 4d 73   = pFrom->zErrMs
2d2dd 67 3b 0a 20 20 20 20 70 54 6f 2d 3e 6e 45 72 72  g;.    pTo->nErr
2d2de 20 3d 20 70 46 72 6f 6d 2d 3e 6e 45 72 72 3b 0a   = pFrom->nErr;.
2d2df 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
2d2e0 69 74 65 33 44 62 46 72 65 65 28 70 46 72 6f 6d  ite3DbFree(pFrom
2d2e1 2d 3e 64 62 2c 20 70 46 72 6f 6d 2d 3e 7a 45 72  ->db, pFrom->zEr
2d2e2 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rMsg);.  }.}../*
2d2e3 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e 64 20 70  .** Create and p
2d2e4 6f 70 75 6c 61 74 65 20 61 20 6e 65 77 20 54 72  opulate a new Tr
2d2e5 69 67 67 65 72 50 72 67 20 6f 62 6a 65 63 74 20  iggerPrg object 
2d2e6 77 69 74 68 20 61 20 73 75 62 2d 70 72 6f 67 72  with a sub-progr
2d2e7 61 6d 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  am .** implement
2d2e8 69 6e 67 20 74 72 69 67 67 65 72 20 70 54 72 69  ing trigger pTri
2d2e9 67 67 65 72 20 77 69 74 68 20 4f 4e 20 43 4f 4e  gger with ON CON
2d2ea 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 6f 72 63  FLICT policy orc
2d2eb 6f 6e 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54  onf..*/.static T
2d2ec 72 69 67 67 65 72 50 72 67 20 2a 63 6f 64 65 52  riggerPrg *codeR
2d2ed 6f 77 54 72 69 67 67 65 72 28 0a 20 20 50 61 72  owTrigger(.  Par
2d2ee 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2d2ef 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70 61 72    /* Current par
2d2f0 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  se context */.  
2d2f1 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
2d2f2 72 2c 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20  r,   /* Trigger 
2d2f3 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20 54 61 62  to code */.  Tab
2d2f4 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
2d2f5 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 70    /* The table p
2d2f6 54 72 69 67 67 65 72 20 69 73 20 61 74 74 61 63  Trigger is attac
2d2f7 68 65 64 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  hed to */.  int 
2d2f8 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20  orconf          
2d2f9 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20   /* ON CONFLICT 
2d2fa 70 6f 6c 69 63 79 20 74 6f 20 63 6f 64 65 20 74  policy to code t
2d2fb 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 77  rigger program w
2d2fc 69 74 68 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73  ith */.){.  Pars
2d2fd 65 20 2a 70 54 6f 70 20 3d 20 73 71 6c 69 74 65  e *pTop = sqlite
2d2fe 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70  3ParseToplevel(p
2d2ff 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
2d300 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2d301 64 62 3b 20 20 20 2f 2a 20 44 61 74 61 62 61 73  db;   /* Databas
2d302 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 54 72  e handle */.  Tr
2d303 69 67 67 65 72 50 72 67 20 2a 70 50 72 67 3b 20  iggerPrg *pPrg; 
2d304 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
2d305 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  ue to return */.
2d306 20 20 45 78 70 72 20 2a 70 57 68 65 6e 20 3d 20    Expr *pWhen = 
2d307 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  0;            /*
2d308 20 44 75 70 6c 69 63 61 74 65 20 6f 66 20 74 72   Duplicate of tr
2d309 69 67 67 65 72 20 57 48 45 4e 20 65 78 70 72 65  igger WHEN expre
2d30a 73 73 69 6f 6e 20 2a 2f 0a 20 20 56 64 62 65 20  ssion */.  Vdbe 
2d30b 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
2d30c 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72         /* Tempor
2d30d 61 72 79 20 56 4d 20 2a 2f 0a 20 20 4e 61 6d 65  ary VM */.  Name
2d30e 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
2d30f 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2d310 63 6f 6e 74 65 78 74 20 66 6f 72 20 73 75 62 2d  context for sub-
2d311 76 64 62 65 20 2a 2f 0a 20 20 53 75 62 50 72 6f  vdbe */.  SubPro
2d312 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 20 3d  gram *pProgram =
2d313 20 30 3b 20 20 20 2f 2a 20 53 75 62 2d 76 64 62   0;   /* Sub-vdb
2d314 65 20 66 6f 72 20 74 72 69 67 67 65 72 20 70 72  e for trigger pr
2d315 6f 67 72 61 6d 20 2a 2f 0a 20 20 50 61 72 73 65  ogram */.  Parse
2d316 20 2a 70 53 75 62 50 61 72 73 65 3b 20 20 20 20   *pSubParse;    
2d317 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
2d318 63 6f 6e 74 65 78 74 20 66 6f 72 20 73 75 62 2d  context for sub-
2d319 76 64 62 65 20 2a 2f 0a 20 20 69 6e 74 20 69 45  vdbe */.  int iE
2d31a 6e 64 54 72 69 67 67 65 72 20 3d 20 30 3b 20 20  ndTrigger = 0;  
2d31b 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 74        /* Label t
2d31c 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 57 48 45  o jump to if WHE
2d31d 4e 20 69 73 20 66 61 6c 73 65 20 2a 2f 0a 0a 20  N is false */.. 
2d31e 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65   assert( pTrigge
2d31f 72 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 7c 7c 20 70  r->zName==0 || p
2d320 54 61 62 3d 3d 74 61 62 6c 65 4f 66 54 72 69 67  Tab==tableOfTrig
2d321 67 65 72 28 70 54 72 69 67 67 65 72 29 20 29 3b  ger(pTrigger) );
2d322 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
2d323 74 68 65 20 54 72 69 67 67 65 72 50 72 67 20 61  the TriggerPrg a
2d324 6e 64 20 53 75 62 50 72 6f 67 72 61 6d 20 6f 62  nd SubProgram ob
2d325 6a 65 63 74 73 2e 20 54 6f 20 65 6e 73 75 72 65  jects. To ensure
2d326 20 74 68 61 74 20 74 68 65 79 0a 20 20 2a 2a 20   that they.  ** 
2d327 61 72 65 20 66 72 65 65 64 20 69 66 20 61 6e 20  are freed if an 
2d328 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 69  error occurs, li
2d329 6e 6b 20 74 68 65 6d 20 69 6e 74 6f 20 74 68 65  nk them into the
2d32a 20 50 61 72 73 65 2e 70 54 72 69 67 67 65 72 50   Parse.pTriggerP
2d32b 72 67 20 0a 20 20 2a 2a 20 6c 69 73 74 20 6f 66  rg .  ** list of
2d32c 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 50   the top-level P
2d32d 61 72 73 65 20 6f 62 6a 65 63 74 20 73 6f 6f 6e  arse object soon
2d32e 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c  er rather than l
2d32f 61 74 65 72 2e 20 20 2a 2f 0a 20 20 70 50 72 67  ater.  */.  pPrg
2d330 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2d331 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
2d332 66 28 54 72 69 67 67 65 72 50 72 67 29 29 3b 0a  f(TriggerPrg));.
2d333 20 20 69 66 28 20 21 70 50 72 67 20 29 20 72 65    if( !pPrg ) re
2d334 74 75 72 6e 20 30 3b 0a 20 20 70 50 72 67 2d 3e  turn 0;.  pPrg->
2d335 70 4e 65 78 74 20 3d 20 70 54 6f 70 2d 3e 70 54  pNext = pTop->pT
2d336 72 69 67 67 65 72 50 72 67 3b 0a 20 20 70 54 6f  riggerPrg;.  pTo
2d337 70 2d 3e 70 54 72 69 67 67 65 72 50 72 67 20 3d  p->pTriggerPrg =
2d338 20 70 50 72 67 3b 0a 20 20 70 50 72 67 2d 3e 70   pPrg;.  pPrg->p
2d339 50 72 6f 67 72 61 6d 20 3d 20 70 50 72 6f 67 72  Program = pProgr
2d33a 61 6d 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  am = sqlite3DbMa
2d33b 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2d33c 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 29 29  eof(SubProgram))
2d33d 3b 0a 20 20 69 66 28 20 21 70 50 72 6f 67 72 61  ;.  if( !pProgra
2d33e 6d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  m ) return 0;.  
2d33f 70 50 72 6f 67 72 61 6d 2d 3e 6e 52 65 66 20 3d  pProgram->nRef =
2d340 20 31 3b 0a 20 20 70 50 72 67 2d 3e 70 54 72 69   1;.  pPrg->pTri
2d341 67 67 65 72 20 3d 20 70 54 72 69 67 67 65 72 3b  gger = pTrigger;
2d342 0a 20 20 70 50 72 67 2d 3e 6f 72 63 6f 6e 66 20  .  pPrg->orconf 
2d343 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 70 50 72 67  = orconf;.  pPrg
2d344 2d 3e 61 43 6f 6c 6d 61 73 6b 5b 30 5d 20 3d 20  ->aColmask[0] = 
2d345 30 78 66 66 66 66 66 66 66 66 3b 0a 20 20 70 50  0xffffffff;.  pP
2d346 72 67 2d 3e 61 43 6f 6c 6d 61 73 6b 5b 31 5d 20  rg->aColmask[1] 
2d347 3d 20 30 78 66 66 66 66 66 66 66 66 3b 0a 0a 20  = 0xffffffff;.. 
2d348 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64   /* Allocate and
2d349 20 70 6f 70 75 6c 61 74 65 20 61 20 6e 65 77 20   populate a new 
2d34a 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 74 6f  Parse context to
2d34b 20 75 73 65 20 66 6f 72 20 63 6f 64 69 6e 67 20   use for coding 
2d34c 74 68 65 20 0a 20 20 2a 2a 20 74 72 69 67 67 65  the .  ** trigge
2d34d 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 20  r sub-program.  
2d34e 2a 2f 0a 20 20 70 53 75 62 50 61 72 73 65 20 3d  */.  pSubParse =
2d34f 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c   sqlite3StackAll
2d350 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
2d351 66 28 50 61 72 73 65 29 29 3b 0a 20 20 69 66 28  f(Parse));.  if(
2d352 20 21 70 53 75 62 50 61 72 73 65 20 29 20 72 65   !pSubParse ) re
2d353 74 75 72 6e 20 30 3b 0a 20 20 6d 65 6d 73 65 74  turn 0;.  memset
2d354 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66  (&sNC, 0, sizeof
2d355 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50  (sNC));.  sNC.pP
2d356 61 72 73 65 20 3d 20 70 53 75 62 50 61 72 73 65  arse = pSubParse
2d357 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e 64  ;.  pSubParse->d
2d358 62 20 3d 20 64 62 3b 0a 20 20 70 53 75 62 50 61  b = db;.  pSubPa
2d359 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62  rse->pTriggerTab
2d35a 20 3d 20 70 54 61 62 3b 0a 20 20 70 53 75 62 50   = pTab;.  pSubP
2d35b 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20  arse->pToplevel 
2d35c 3d 20 70 54 6f 70 3b 0a 20 20 70 53 75 62 50 61  = pTop;.  pSubPa
2d35d 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78  rse->zAuthContex
2d35e 74 20 3d 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e  t = pTrigger->zN
2d35f 61 6d 65 3b 0a 20 20 70 53 75 62 50 61 72 73 65  ame;.  pSubParse
2d360 2d 3e 65 54 72 69 67 67 65 72 4f 70 20 3d 20 70  ->eTriggerOp = p
2d361 54 72 69 67 67 65 72 2d 3e 6f 70 3b 0a 0a 20 20  Trigger->op;..  
2d362 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
2d363 62 65 28 70 53 75 62 50 61 72 73 65 29 3b 0a 20  be(pSubParse);. 
2d364 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 56 64   if( v ){.    Vd
2d365 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 53  beComment((v, "S
2d366 74 61 72 74 3a 20 25 73 2e 25 73 20 28 25 73 20  tart: %s.%s (%s 
2d367 25 73 25 73 25 73 20 4f 4e 20 25 73 29 22 2c 20  %s%s%s ON %s)", 
2d368 0a 20 20 20 20 20 20 70 54 72 69 67 67 65 72 2d  .      pTrigger-
2d369 3e 7a 4e 61 6d 65 2c 20 6f 6e 45 72 72 6f 72 54  >zName, onErrorT
2d36a 65 78 74 28 6f 72 63 6f 6e 66 29 2c 0a 20 20 20  ext(orconf),.   
2d36b 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e 74 72     (pTrigger->tr
2d36c 5f 74 6d 3d 3d 54 52 49 47 47 45 52 5f 42 45 46  _tm==TRIGGER_BEF
2d36d 4f 52 45 20 3f 20 22 42 45 46 4f 52 45 22 20 3a  ORE ? "BEFORE" :
2d36e 20 22 41 46 54 45 52 22 29 2c 0a 20 20 20 20 20   "AFTER"),.     
2d36f 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e 6f 70     (pTrigger->op
2d370 3d 3d 54 4b 5f 55 50 44 41 54 45 20 3f 20 22 55  ==TK_UPDATE ? "U
2d371 50 44 41 54 45 22 20 3a 20 22 22 29 2c 0a 20 20  PDATE" : ""),.  
2d372 20 20 20 20 20 20 28 70 54 72 69 67 67 65 72 2d        (pTrigger-
2d373 3e 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 3f  >op==TK_INSERT ?
2d374 20 22 49 4e 53 45 52 54 22 20 3a 20 22 22 29 2c   "INSERT" : ""),
2d375 0a 20 20 20 20 20 20 20 20 28 70 54 72 69 67 67  .        (pTrigg
2d376 65 72 2d 3e 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54  er->op==TK_DELET
2d377 45 20 3f 20 22 44 45 4c 45 54 45 22 20 3a 20 22  E ? "DELETE" : "
2d378 22 29 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  "),.      pTab->
2d379 7a 4e 61 6d 65 0a 20 20 20 20 29 29 3b 0a 23 69  zName.    ));.#i
2d37a 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2d37b 54 5f 54 52 41 43 45 0a 20 20 20 20 73 71 6c 69  T_TRACE.    sqli
2d37c 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
2d37d 76 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 73 71  v, -1, .      sq
2d37e 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2d37f 20 22 2d 2d 20 54 52 49 47 47 45 52 20 25 73 22   "-- TRIGGER %s"
2d380 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d  , pTrigger->zNam
2d381 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43 0a 20  e), P4_DYNAMIC. 
2d382 20 20 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20     );.#endif..  
2d383 20 20 2f 2a 20 49 66 20 6f 6e 65 20 77 61 73 20    /* If one was 
2d384 73 70 65 63 69 66 69 65 64 2c 20 63 6f 64 65 20  specified, code 
2d385 74 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 2e  the WHEN clause.
2d386 20 49 66 20 69 74 20 65 76 61 6c 75 61 74 65 73   If it evaluates
2d387 20 74 6f 20 66 61 6c 73 65 0a 20 20 20 20 2a 2a   to false.    **
2d388 20 28 6f 72 20 4e 55 4c 4c 29 20 74 68 65 20 73   (or NULL) the s
2d389 75 62 2d 76 64 62 65 20 69 73 20 69 6d 6d 65 64  ub-vdbe is immed
2d38a 69 61 74 65 6c 79 20 68 61 6c 74 65 64 20 62 79  iately halted by
2d38b 20 6a 75 6d 70 69 6e 67 20 74 6f 20 74 68 65 20   jumping to the 
2d38c 0a 20 20 20 20 2a 2a 20 4f 50 5f 48 61 6c 74 20  .    ** OP_Halt 
2d38d 69 6e 73 65 72 74 65 64 20 61 74 20 74 68 65 20  inserted at the 
2d38e 65 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72  end of the progr
2d38f 61 6d 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  am.  */.    if( 
2d390 70 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 20  pTrigger->pWhen 
2d391 29 7b 0a 20 20 20 20 20 20 70 57 68 65 6e 20 3d  ){.      pWhen =
2d392 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2d393 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 57  db, pTrigger->pW
2d394 68 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  hen, 0);.      i
2d395 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71  f( SQLITE_OK==sq
2d396 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
2d397 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65  Names(&sNC, pWhe
2d398 6e 29 20 0a 20 20 20 20 20 20 20 26 26 20 64 62  n) .       && db
2d399 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d  ->mallocFailed==
2d39a 30 20 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  0 .      ){.    
2d39b 20 20 20 20 69 45 6e 64 54 72 69 67 67 65 72 20      iEndTrigger 
2d39c 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2d39d 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
2d39e 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
2d39f 46 61 6c 73 65 28 70 53 75 62 50 61 72 73 65 2c  False(pSubParse,
2d3a0 20 70 57 68 65 6e 2c 20 69 45 6e 64 54 72 69 67   pWhen, iEndTrig
2d3a1 67 65 72 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  ger, SQLITE_JUMP
2d3a2 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 7d  IFNULL);.      }
2d3a3 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2d3a4 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68  prDelete(db, pWh
2d3a5 65 6e 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  en);.    }..    
2d3a6 2f 2a 20 43 6f 64 65 20 74 68 65 20 74 72 69 67  /* Code the trig
2d3a7 67 65 72 20 70 72 6f 67 72 61 6d 20 69 6e 74 6f  ger program into
2d3a8 20 74 68 65 20 73 75 62 2d 76 64 62 65 2e 20 2a   the sub-vdbe. *
2d3a9 2f 0a 20 20 20 20 63 6f 64 65 54 72 69 67 67 65  /.    codeTrigge
2d3aa 72 50 72 6f 67 72 61 6d 28 70 53 75 62 50 61 72  rProgram(pSubPar
2d3ab 73 65 2c 20 70 54 72 69 67 67 65 72 2d 3e 73 74  se, pTrigger->st
2d3ac 65 70 5f 6c 69 73 74 2c 20 6f 72 63 6f 6e 66 29  ep_list, orconf)
2d3ad 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74  ;..    /* Insert
2d3ae 20 61 6e 20 4f 50 5f 48 61 6c 74 20 61 74 20 74   an OP_Halt at t
2d3af 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 75  he end of the su
2d3b0 62 2d 70 72 6f 67 72 61 6d 2e 20 2a 2f 0a 20 20  b-program. */.  
2d3b1 20 20 69 66 28 20 69 45 6e 64 54 72 69 67 67 65    if( iEndTrigge
2d3b2 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  r ){.      sqlit
2d3b3 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
2d3b4 65 6c 28 76 2c 20 69 45 6e 64 54 72 69 67 67 65  el(v, iEndTrigge
2d3b5 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  r);.    }.    sq
2d3b6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
2d3b7 76 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a 20 20 20  v, OP_Halt);.   
2d3b8 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
2d3b9 20 22 45 6e 64 3a 20 25 73 2e 25 73 22 2c 20 70   "End: %s.%s", p
2d3ba 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20  Trigger->zName, 
2d3bb 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f 72 63 6f  onErrorText(orco
2d3bc 6e 66 29 29 29 3b 0a 0a 20 20 20 20 74 72 61 6e  nf)));..    tran
2d3bd 73 66 65 72 50 61 72 73 65 45 72 72 6f 72 28 70  sferParseError(p
2d3be 50 61 72 73 65 2c 20 70 53 75 62 50 61 72 73 65  Parse, pSubParse
2d3bf 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  );.    if( db->m
2d3c0 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29  allocFailed==0 )
2d3c1 7b 0a 20 20 20 20 20 20 70 50 72 6f 67 72 61 6d  {.      pProgram
2d3c2 2d 3e 61 4f 70 20 3d 20 73 71 6c 69 74 65 33 56  ->aOp = sqlite3V
2d3c3 64 62 65 54 61 6b 65 4f 70 41 72 72 61 79 28 76  dbeTakeOpArray(v
2d3c4 2c 20 26 70 50 72 6f 67 72 61 6d 2d 3e 6e 4f 70  , &pProgram->nOp
2d3c5 2c 20 26 70 54 6f 70 2d 3e 6e 4d 61 78 41 72 67  , &pTop->nMaxArg
2d3c6 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 72  );.    }.    pPr
2d3c7 6f 67 72 61 6d 2d 3e 6e 4d 65 6d 20 3d 20 70 53  ogram->nMem = pS
2d3c8 75 62 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  ubParse->nMem;. 
2d3c9 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73     pProgram->nCs
2d3ca 72 20 3d 20 70 53 75 62 50 61 72 73 65 2d 3e 6e  r = pSubParse->n
2d3cb 54 61 62 3b 0a 20 20 20 20 70 50 72 6f 67 72 61  Tab;.    pProgra
2d3cc 6d 2d 3e 74 6f 6b 65 6e 20 3d 20 28 76 6f 69 64  m->token = (void
2d3cd 20 2a 29 70 54 72 69 67 67 65 72 3b 0a 20 20 20   *)pTrigger;.   
2d3ce 20 70 50 72 67 2d 3e 61 43 6f 6c 6d 61 73 6b 5b   pPrg->aColmask[
2d3cf 30 5d 20 3d 20 70 53 75 62 50 61 72 73 65 2d 3e  0] = pSubParse->
2d3d0 6f 6c 64 6d 61 73 6b 3b 0a 20 20 20 20 70 50 72  oldmask;.    pPr
2d3d1 67 2d 3e 61 43 6f 6c 6d 61 73 6b 5b 31 5d 20 3d  g->aColmask[1] =
2d3d2 20 70 53 75 62 50 61 72 73 65 2d 3e 6e 65 77 6d   pSubParse->newm
2d3d3 61 73 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ask;.    sqlite3
2d3d4 56 64 62 65 44 65 6c 65 74 65 28 76 29 3b 0a 20  VdbeDelete(v);. 
2d3d5 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70   }..  assert( !p
2d3d6 53 75 62 50 61 72 73 65 2d 3e 70 41 69 6e 63 20  SubParse->pAinc 
2d3d7 20 20 20 20 20 20 26 26 20 21 70 53 75 62 50 61        && !pSubPa
2d3d8 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20  rse->pZombieTab 
2d3d9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 53  );.  assert( !pS
2d3da 75 62 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65  ubParse->pTrigge
2d3db 72 50 72 67 20 26 26 20 21 70 53 75 62 50 61 72  rPrg && !pSubPar
2d3dc 73 65 2d 3e 6e 4d 61 78 41 72 67 20 29 3b 0a 20  se->nMaxArg );. 
2d3dd 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65   sqlite3StackFre
2d3de 65 28 64 62 2c 20 70 53 75 62 50 61 72 73 65 29  e(db, pSubParse)
2d3df 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 50 72 67  ;..  return pPrg
2d3e0 3b 0a 7d 0a 20 20 20 20 0a 2f 2a 0a 2a 2a 20 52  ;.}.    ./*.** R
2d3e1 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
2d3e2 74 6f 20 61 20 54 72 69 67 67 65 72 50 72 67 20  to a TriggerPrg 
2d3e3 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 69 6e  object containin
2d3e4 67 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61  g the sub-progra
2d3e5 6d 20 66 6f 72 0a 2a 2a 20 74 72 69 67 67 65 72  m for.** trigger
2d3e6 20 70 54 72 69 67 67 65 72 20 77 69 74 68 20 64   pTrigger with d
2d3e7 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c 49  efault ON CONFLI
2d3e8 43 54 20 61 6c 67 6f 72 69 74 68 6d 20 6f 72 63  CT algorithm orc
2d3e9 6f 6e 66 2e 20 49 66 20 6e 6f 20 73 75 63 68 0a  onf. If no such.
2d3ea 2a 2a 20 54 72 69 67 67 65 72 50 72 67 20 6f 62  ** TriggerPrg ob
2d3eb 6a 65 63 74 20 65 78 69 73 74 73 2c 20 61 20 6e  ject exists, a n
2d3ec 65 77 20 6f 62 6a 65 63 74 20 69 73 20 61 6c 6c  ew object is all
2d3ed 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c  ocated and popul
2d3ee 61 74 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 62  ated before.** b
2d3ef 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a  eing returned..*
2d3f0 2f 0a 73 74 61 74 69 63 20 54 72 69 67 67 65 72  /.static Trigger
2d3f1 50 72 67 20 2a 67 65 74 52 6f 77 54 72 69 67 67  Prg *getRowTrigg
2d3f2 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  er(.  Parse *pPa
2d3f3 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 43 75  rse,       /* Cu
2d3f4 72 72 65 6e 74 20 70 61 72 73 65 20 63 6f 6e 74  rrent parse cont
2d3f5 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ext */.  Trigger
2d3f6 20 2a 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a   *pTrigger,   /*
2d3f7 20 54 72 69 67 67 65 72 20 74 6f 20 63 6f 64 65   Trigger to code
2d3f8 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2d3f9 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b,         /* Th
2d3fa 65 20 74 61 62 6c 65 20 74 72 69 67 67 65 72 20  e table trigger 
2d3fb 70 54 72 69 67 67 65 72 20 69 73 20 61 74 74 61  pTrigger is atta
2d3fc 63 68 65 64 20 74 6f 20 2a 2f 0a 20 20 69 6e 74  ched to */.  int
2d3fd 20 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20   orconf         
2d3fe 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54    /* ON CONFLICT
2d3ff 20 61 6c 67 6f 72 69 74 68 6d 2e 20 2a 2f 0a 29   algorithm. */.)
2d400 7b 0a 20 20 50 61 72 73 65 20 2a 70 52 6f 6f 74  {.  Parse *pRoot
2d401 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54   = sqlite3ParseT
2d402 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b  oplevel(pParse);
2d403 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70  .  TriggerPrg *p
2d404 50 72 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  Prg;..  assert( 
2d405 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 3d  pTrigger->zName=
2d406 3d 30 20 7c 7c 20 70 54 61 62 3d 3d 74 61 62 6c  =0 || pTab==tabl
2d407 65 4f 66 54 72 69 67 67 65 72 28 70 54 72 69 67  eOfTrigger(pTrig
2d408 67 65 72 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 74  ger) );..  /* It
2d409 20 6d 61 79 20 62 65 20 74 68 61 74 20 74 68 69   may be that thi
2d40a 73 20 74 72 69 67 67 65 72 20 68 61 73 20 61 6c  s trigger has al
2d40b 72 65 61 64 79 20 62 65 65 6e 20 63 6f 64 65 64  ready been coded
2d40c 20 28 6f 72 20 69 73 20 69 6e 20 74 68 65 0a 20   (or is in the. 
2d40d 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f 66 20 62   ** process of b
2d40e 65 69 6e 67 20 63 6f 64 65 64 29 2e 20 49 66 20  eing coded). If 
2d40f 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65  this is the case
2d410 2c 20 74 68 65 6e 20 61 6e 20 65 6e 74 72 79 20  , then an entry 
2d411 77 69 74 68 0a 20 20 2a 2a 20 61 20 6d 61 74 63  with.  ** a matc
2d412 68 69 6e 67 20 54 72 69 67 67 65 72 50 72 67 2e  hing TriggerPrg.
2d413 70 54 72 69 67 67 65 72 20 66 69 65 6c 64 20 77  pTrigger field w
2d414 69 6c 6c 20 62 65 20 70 72 65 73 65 6e 74 20 73  ill be present s
2d415 6f 6d 65 77 68 65 72 65 0a 20 20 2a 2a 20 69 6e  omewhere.  ** in
2d416 20 74 68 65 20 50 61 72 73 65 2e 70 54 72 69 67   the Parse.pTrig
2d417 67 65 72 50 72 67 20 6c 69 73 74 2e 20 53 65 61  gerPrg list. Sea
2d418 72 63 68 20 66 6f 72 20 73 75 63 68 20 61 6e 20  rch for such an 
2d419 65 6e 74 72 79 2e 20 20 2a 2f 0a 20 20 66 6f 72  entry.  */.  for
2d41a 28 70 50 72 67 3d 70 52 6f 6f 74 2d 3e 70 54 72  (pPrg=pRoot->pTr
2d41b 69 67 67 65 72 50 72 67 3b 20 0a 20 20 20 20 20  iggerPrg; .     
2d41c 20 70 50 72 67 20 26 26 20 28 70 50 72 67 2d 3e   pPrg && (pPrg->
2d41d 70 54 72 69 67 67 65 72 21 3d 70 54 72 69 67 67  pTrigger!=pTrigg
2d41e 65 72 20 7c 7c 20 70 50 72 67 2d 3e 6f 72 63 6f  er || pPrg->orco
2d41f 6e 66 21 3d 6f 72 63 6f 6e 66 29 3b 20 0a 20 20  nf!=orconf); .  
2d420 20 20 20 20 70 50 72 67 3d 70 50 72 67 2d 3e 70      pPrg=pPrg->p
2d421 4e 65 78 74 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20  Next.  );..  /* 
2d422 49 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 54  If an existing T
2d423 72 69 67 67 65 72 50 72 67 20 63 6f 75 6c 64 20  riggerPrg could 
2d424 6e 6f 74 20 62 65 20 6c 6f 63 61 74 65 64 2c 20  not be located, 
2d425 63 72 65 61 74 65 20 61 20 6e 65 77 20 6f 6e 65  create a new one
2d426 2e 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 72 67  . */.  if( !pPrg
2d427 20 29 7b 0a 20 20 20 20 70 50 72 67 20 3d 20 63   ){.    pPrg = c
2d428 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50  odeRowTrigger(pP
2d429 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20  arse, pTrigger, 
2d42a 70 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20  pTab, orconf);. 
2d42b 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 50 72   }..  return pPr
2d42c 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  g;.}../*.** Gene
2d42d 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68  rate code for th
2d42e 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  e trigger progra
2d42f 6d 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  m associated wit
2d430 68 20 74 72 69 67 67 65 72 20 70 20 6f 6e 20 0a  h trigger p on .
2d431 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 2e 20 54  ** table pTab. T
2d432 68 65 20 72 65 67 2c 20 6f 72 63 6f 6e 66 20 61  he reg, orconf a
2d433 6e 64 20 69 67 6e 6f 72 65 4a 75 6d 70 20 70 61  nd ignoreJump pa
2d434 72 61 6d 65 74 65 72 73 20 70 61 73 73 65 64 20  rameters passed 
2d435 74 6f 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74  to this.** funct
2d436 69 6f 6e 20 61 72 65 20 74 68 65 20 73 61 6d 65  ion are the same
2d437 20 61 73 20 74 68 6f 73 65 20 64 65 73 63 72 69   as those descri
2d438 62 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65  bed in the heade
2d439 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 0a 2a  r function for.*
2d43a 2a 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77  * sqlite3CodeRow
2d43b 54 72 69 67 67 65 72 28 29 0a 2a 2f 0a 53 51 4c  Trigger().*/.SQL
2d43c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2d43d 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
2d43e 72 69 67 67 65 72 44 69 72 65 63 74 28 0a 20 20  riggerDirect(.  
2d43f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2d440 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f       /* Parse co
2d441 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67  ntext */.  Trigg
2d442 65 72 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  er *p,          
2d443 2f 2a 20 54 72 69 67 67 65 72 20 74 6f 20 63 6f  /* Trigger to co
2d444 64 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  de */.  Table *p
2d445 54 61 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Tab,         /* 
2d446 54 68 65 20 74 61 62 6c 65 20 74 6f 20 63 6f 64  The table to cod
2d447 65 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20  e triggers from 
2d448 2a 2f 0a 20 20 69 6e 74 20 72 65 67 2c 20 20 20  */.  int reg,   
2d449 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
2d44a 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 69 6e   array containin
2d44b 67 20 4f 4c 44 2e 2a 20 61 6e 64 20 4e 45 57 2e  g OLD.* and NEW.
2d44c 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 69 6e  * values */.  in
2d44d 74 20 6f 72 63 6f 6e 66 2c 20 20 20 20 20 20 20  t orconf,       
2d44e 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43     /* ON CONFLIC
2d44f 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e  T policy */.  in
2d450 74 20 69 67 6e 6f 72 65 4a 75 6d 70 20 20 20 20  t ignoreJump    
2d451 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69 6f     /* Instructio
2d452 6e 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f 72  n to jump to for
2d453 20 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20 2a   RAISE(IGNORE) *
2d454 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
2d455 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2d456 70 50 61 72 73 65 29 3b 20 2f 2a 20 4d 61 69 6e  pParse); /* Main
2d457 20 56 4d 20 2a 2f 0a 20 20 54 72 69 67 67 65 72   VM */.  Trigger
2d458 50 72 67 20 2a 70 50 72 67 3b 0a 20 20 70 50 72  Prg *pPrg;.  pPr
2d459 67 20 3d 20 67 65 74 52 6f 77 54 72 69 67 67 65  g = getRowTrigge
2d45a 72 28 70 50 61 72 73 65 2c 20 70 2c 20 70 54 61  r(pParse, p, pTa
2d45b 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 61 73  b, orconf);.  as
2d45c 73 65 72 74 28 20 70 50 72 67 20 7c 7c 20 70 50  sert( pPrg || pP
2d45d 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 70 50  arse->nErr || pP
2d45e 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
2d45f 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a 20  Failed );..  /* 
2d460 43 6f 64 65 20 74 68 65 20 4f 50 5f 50 72 6f 67  Code the OP_Prog
2d461 72 61 6d 20 6f 70 63 6f 64 65 20 69 6e 20 74 68  ram opcode in th
2d462 65 20 70 61 72 65 6e 74 20 56 44 42 45 2e 20 50  e parent VDBE. P
2d463 34 20 6f 66 20 74 68 65 20 4f 50 5f 50 72 6f 67  4 of the OP_Prog
2d464 72 61 6d 20 0a 20 20 2a 2a 20 69 73 20 61 20 70  ram .  ** is a p
2d465 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 75  ointer to the su
2d466 62 2d 76 64 62 65 20 63 6f 6e 74 61 69 6e 69 6e  b-vdbe containin
2d467 67 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  g the trigger pr
2d468 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 69 66 28  ogram.  */.  if(
2d469 20 70 50 72 67 20 29 7b 0a 20 20 20 20 73 71 6c   pPrg ){.    sql
2d46a 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2d46b 2c 20 4f 50 5f 50 72 6f 67 72 61 6d 2c 20 72 65  , OP_Program, re
2d46c 67 2c 20 69 67 6e 6f 72 65 4a 75 6d 70 2c 20 2b  g, ignoreJump, +
2d46d 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a  +pParse->nMem);.
2d46e 20 20 20 20 70 50 72 67 2d 3e 70 50 72 6f 67 72      pPrg->pProgr
2d46f 61 6d 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  am->nRef++;.    
2d470 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2d471 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73  eP4(v, -1, (cons
2d472 74 20 63 68 61 72 20 2a 29 70 50 72 67 2d 3e 70  t char *)pPrg->p
2d473 50 72 6f 67 72 61 6d 2c 20 50 34 5f 53 55 42 50  Program, P4_SUBP
2d474 52 4f 47 52 41 4d 29 3b 0a 20 20 20 20 56 64 62  ROGRAM);.    Vdb
2d475 65 43 6f 6d 6d 65 6e 74 28 0a 20 20 20 20 20 20  eComment(.      
2d476 20 20 28 76 2c 20 22 43 61 6c 6c 3a 20 25 73 2e    (v, "Call: %s.
2d477 25 73 22 2c 20 28 70 2d 3e 7a 4e 61 6d 65 3f 70  %s", (p->zName?p
2d478 2d 3e 7a 4e 61 6d 65 3a 22 66 6b 65 79 22 29 2c  ->zName:"fkey"),
2d479 20 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f 72 63   onErrorText(orc
2d47a 6f 6e 66 29 29 29 3b 0a 0a 20 20 20 20 2f 2a 20  onf)));..    /* 
2d47b 53 65 74 20 74 68 65 20 50 35 20 6f 70 65 72 61  Set the P5 opera
2d47c 6e 64 20 6f 66 20 74 68 65 20 4f 50 5f 50 72 6f  nd of the OP_Pro
2d47d 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e  gram instruction
2d47e 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 0a   to non-zero if.
2d47f 20 20 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65      ** recursive
2d480 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74   invocation of t
2d481 68 69 73 20 74 72 69 67 67 65 72 20 70 72 6f 67  his trigger prog
2d482 72 61 6d 20 69 73 20 64 69 73 61 6c 6c 6f 77 65  ram is disallowe
2d483 64 2e 20 52 65 63 75 72 73 69 76 65 0a 20 20 20  d. Recursive.   
2d484 20 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 69   ** invocation i
2d485 73 20 64 69 73 61 6c 6c 6f 77 65 64 20 69 66 20  s disallowed if 
2d486 28 61 29 20 74 68 65 20 73 75 62 2d 70 72 6f 67  (a) the sub-prog
2d487 72 61 6d 20 69 73 20 72 65 61 6c 6c 79 20 61 20  ram is really a 
2d488 74 72 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20  trigger,.    ** 
2d489 6e 6f 74 20 61 20 66 6f 72 65 69 67 6e 20 6b 65  not a foreign ke
2d48a 79 20 61 63 74 69 6f 6e 2c 20 61 6e 64 20 28 62  y action, and (b
2d48b 29 20 74 68 65 20 66 6c 61 67 20 74 6f 20 65 6e  ) the flag to en
2d48c 61 62 6c 65 20 72 65 63 75 72 73 69 76 65 20 74  able recursive t
2d48d 72 69 67 67 65 72 73 0a 20 20 20 20 2a 2a 20 69  riggers.    ** i
2d48e 73 20 63 6c 65 61 72 2e 20 20 2a 2f 0a 20 20 20  s clear.  */.   
2d48f 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2d490 67 65 50 35 28 76 2c 20 28 75 38 29 28 70 2d 3e  geP5(v, (u8)(p->
2d491 7a 4e 61 6d 65 20 26 26 20 21 28 70 50 61 72 73  zName && !(pPars
2d492 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  e->db->flags&SQL
2d493 49 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 29  ITE_RecTriggers)
2d494 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ));.  }.}../*.**
2d495 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   This is called 
2d496 74 6f 20 63 6f 64 65 20 74 68 65 20 72 65 71 75  to code the requ
2d497 69 72 65 64 20 46 4f 52 20 45 41 43 48 20 52 4f  ired FOR EACH RO
2d498 57 20 74 72 69 67 67 65 72 73 20 66 6f 72 20 61  W triggers for a
2d499 6e 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 6f  n operation.** o
2d49a 6e 20 74 61 62 6c 65 20 70 54 61 62 2e 20 54 68  n table pTab. Th
2d49b 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 63  e operation to c
2d49c 6f 64 65 20 74 72 69 67 67 65 72 73 20 66 6f 72  ode triggers for
2d49d 20 28 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45   (INSERT, UPDATE
2d49e 20 6f 72 20 44 45 4c 45 54 45 29 0a 2a 2a 20 69   or DELETE).** i
2d49f 73 20 67 69 76 65 6e 20 62 79 20 74 68 65 20 6f  s given by the o
2d4a0 70 20 70 61 72 61 6d 61 74 65 72 2e 20 54 68 65  p paramater. The
2d4a1 20 74 72 5f 74 6d 20 70 61 72 61 6d 65 74 65 72   tr_tm parameter
2d4a2 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74   determines whet
2d4a3 68 65 72 20 74 68 65 0a 2a 2a 20 42 45 46 4f 52  her the.** BEFOR
2d4a4 45 20 6f 72 20 41 46 54 45 52 20 74 72 69 67 67  E or AFTER trigg
2d4a5 65 72 73 20 61 72 65 20 63 6f 64 65 64 2e 20 49  ers are coded. I
2d4a6 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  f the operation 
2d4a7 69 73 20 61 6e 20 55 50 44 41 54 45 2c 20 74 68  is an UPDATE, th
2d4a8 65 6e 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  en.** parameter 
2d4a9 70 43 68 61 6e 67 65 73 20 69 73 20 70 61 73 73  pChanges is pass
2d4aa 65 64 20 74 68 65 20 6c 69 73 74 20 6f 66 20 63  ed the list of c
2d4ab 6f 6c 75 6d 6e 73 20 62 65 69 6e 67 20 6d 6f 64  olumns being mod
2d4ac 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ified..**.** If 
2d4ad 74 68 65 72 65 20 61 72 65 20 6e 6f 20 74 72 69  there are no tri
2d4ae 67 67 65 72 73 20 74 68 61 74 20 66 69 72 65 20  ggers that fire 
2d4af 61 74 20 74 68 65 20 73 70 65 63 69 66 69 65 64  at the specified
2d4b0 20 74 69 6d 65 20 66 6f 72 20 74 68 65 20 73 70   time for the sp
2d4b1 65 63 69 66 69 65 64 0a 2a 2a 20 6f 70 65 72 61  ecified.** opera
2d4b2 74 69 6f 6e 20 6f 6e 20 70 54 61 62 2c 20 74 68  tion on pTab, th
2d4b3 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
2d4b4 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68   no-op..**.** Th
2d4b5 65 20 72 65 67 20 61 72 67 75 6d 65 6e 74 20 69  e reg argument i
2d4b6 73 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  s the address of
2d4b7 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e   the first in an
2d4b8 20 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74   array of regist
2d4b9 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 63 6f 6e  ers .** that con
2d4ba 74 61 69 6e 20 74 68 65 20 76 61 6c 75 65 73 20  tain the values 
2d4bb 73 75 62 73 74 69 74 75 74 65 64 20 66 6f 72 20  substituted for 
2d4bc 74 68 65 20 6e 65 77 2e 2a 20 61 6e 64 20 6f 6c  the new.* and ol
2d4bd 64 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 0a 2a  d.* references.*
2d4be 2a 20 69 6e 20 74 68 65 20 74 72 69 67 67 65 72  * in the trigger
2d4bf 20 70 72 6f 67 72 61 6d 2e 20 49 66 20 4e 20 69   program. If N i
2d4c0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
2d4c1 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 61 62 6c 65  columns in table
2d4c2 20 70 54 61 62 0a 2a 2a 20 28 61 20 63 6f 70 79   pTab.** (a copy
2d4c3 20 6f 66 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 2c   of pTab->nCol),
2d4c4 20 74 68 65 6e 20 72 65 67 69 73 74 65 72 73 20   then registers 
2d4c5 61 72 65 20 70 6f 70 75 6c 61 74 65 64 20 61 73  are populated as
2d4c6 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
2d4c7 20 20 52 65 67 69 73 74 65 72 20 20 20 20 20 20    Register      
2d4c8 20 43 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20 20 2d   Contains.**   -
2d4c9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d4ca 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d4cb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d4cc 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 72 65 67 2b 30  -----.**   reg+0
2d4cd 20 20 20 20 20 20 20 20 20 20 4f 4c 44 2e 72 6f            OLD.ro
2d4ce 77 69 64 0a 2a 2a 20 20 20 72 65 67 2b 31 20 20  wid.**   reg+1  
2d4cf 20 20 20 20 20 20 20 20 4f 4c 44 2e 2a 20 76 61          OLD.* va
2d4d0 6c 75 65 20 6f 66 20 6c 65 66 74 2d 6d 6f 73 74  lue of left-most
2d4d1 20 63 6f 6c 75 6d 6e 20 6f 66 20 70 54 61 62 0a   column of pTab.
2d4d2 2a 2a 20 20 20 2e 2e 2e 20 20 20 20 20 20 20 20  **   ...        
2d4d3 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 72 65 67      ....**   reg
2d4d4 2b 4e 20 20 20 20 20 20 20 20 20 20 4f 4c 44 2e  +N          OLD.
2d4d5 2a 20 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74  * value of right
2d4d6 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  -most column of 
2d4d7 70 54 61 62 0a 2a 2a 20 20 20 72 65 67 2b 4e 2b  pTab.**   reg+N+
2d4d8 31 20 20 20 20 20 20 20 20 4e 45 57 2e 72 6f 77  1        NEW.row
2d4d9 69 64 0a 2a 2a 20 20 20 72 65 67 2b 4e 2b 32 20  id.**   reg+N+2 
2d4da 20 20 20 20 20 20 20 4f 4c 44 2e 2a 20 76 61 6c         OLD.* val
2d4db 75 65 20 6f 66 20 6c 65 66 74 2d 6d 6f 73 74 20  ue of left-most 
2d4dc 63 6f 6c 75 6d 6e 20 6f 66 20 70 54 61 62 0a 2a  column of pTab.*
2d4dd 2a 20 20 20 2e 2e 2e 20 20 20 20 20 20 20 20 20  *   ...         
2d4de 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 72 65 67 2b     ....**   reg+
2d4df 4e 2b 4e 2b 31 20 20 20 20 20 20 4e 45 57 2e 2a  N+N+1      NEW.*
2d4e0 20 76 61 6c 75 65 20 6f 66 20 72 69 67 68 74 2d   value of right-
2d4e1 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 70  most column of p
2d4e2 54 61 62 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 4f 4e  Tab.**.** For ON
2d4e3 20 44 45 4c 45 54 45 20 74 72 69 67 67 65 72 73   DELETE triggers
2d4e4 2c 20 74 68 65 20 72 65 67 69 73 74 65 72 73 20  , the registers 
2d4e5 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 4e  containing the N
2d4e6 45 57 2e 2a 20 76 61 6c 75 65 73 20 77 69 6c 6c  EW.* values will
2d4e7 0a 2a 2a 20 6e 65 76 65 72 20 62 65 20 61 63 63  .** never be acc
2d4e8 65 73 73 65 64 20 62 79 20 74 68 65 20 74 72 69  essed by the tri
2d4e9 67 67 65 72 20 70 72 6f 67 72 61 6d 2c 20 73 6f  gger program, so
2d4ea 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c   they are not al
2d4eb 6c 6f 63 61 74 65 64 20 6f 72 20 0a 2a 2a 20 70  located or .** p
2d4ec 6f 70 75 6c 61 74 65 64 20 62 79 20 74 68 65 20  opulated by the 
2d4ed 63 61 6c 6c 65 72 20 28 74 68 65 72 65 20 69 73  caller (there is
2d4ee 20 6e 6f 20 64 61 74 61 20 74 6f 20 70 6f 70 75   no data to popu
2d4ef 6c 61 74 65 20 74 68 65 6d 20 77 69 74 68 20 61  late them with a
2d4f0 6e 79 77 61 79 29 2e 20 0a 2a 2a 20 53 69 6d 69  nyway). .** Simi
2d4f1 6c 61 72 6c 79 2c 20 66 6f 72 20 4f 4e 20 49 4e  larly, for ON IN
2d4f2 53 45 52 54 20 74 72 69 67 67 65 72 73 20 74 68  SERT triggers th
2d4f3 65 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64 20  e values stored 
2d4f4 69 6e 20 74 68 65 20 4f 4c 44 2e 2a 20 72 65 67  in the OLD.* reg
2d4f5 69 73 74 65 72 73 0a 2a 2a 20 61 72 65 20 6e 65  isters.** are ne
2d4f6 76 65 72 20 61 63 63 65 73 73 65 64 2c 20 61 6e  ver accessed, an
2d4f7 64 20 73 6f 20 61 72 65 20 6e 6f 74 20 61 6c 6c  d so are not all
2d4f8 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 63 61  ocated by the ca
2d4f9 6c 6c 65 72 2e 20 53 6f 2c 20 66 6f 72 20 61 6e  ller. So, for an
2d4fa 0a 2a 2a 20 4f 4e 20 49 4e 53 45 52 54 20 74 72  .** ON INSERT tr
2d4fb 69 67 67 65 72 2c 20 74 68 65 20 76 61 6c 75 65  igger, the value
2d4fc 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
2d4fd 66 75 6e 63 74 69 6f 6e 20 61 73 20 70 61 72 61  function as para
2d4fe 6d 65 74 65 72 20 72 65 67 0a 2a 2a 20 69 73 20  meter reg.** is 
2d4ff 6e 6f 74 20 61 20 72 65 61 64 61 62 6c 65 20 72  not a readable r
2d500 65 67 69 73 74 65 72 2c 20 61 6c 74 68 6f 75 67  egister, althoug
2d501 68 20 72 65 67 69 73 74 65 72 73 20 28 72 65 67  h registers (reg
2d502 2b 4e 29 20 74 68 72 6f 75 67 68 20 0a 2a 2a 20  +N) through .** 
2d503 28 72 65 67 2b 4e 2b 4e 2b 31 29 20 61 72 65 2e  (reg+N+N+1) are.
2d504 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  .**.** Parameter
2d505 20 6f 72 63 6f 6e 66 20 69 73 20 74 68 65 20 64   orconf is the d
2d506 65 66 61 75 6c 74 20 63 6f 6e 66 6c 69 63 74 20  efault conflict 
2d507 72 65 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72  resolution algor
2d508 69 74 68 6d 20 66 6f 72 20 74 68 65 0a 2a 2a 20  ithm for the.** 
2d509 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20  trigger program 
2d50a 74 6f 20 75 73 65 20 28 52 45 50 4c 41 43 45 2c  to use (REPLACE,
2d50b 20 49 47 4e 4f 52 45 20 65 74 63 2e 29 2e 20 50   IGNORE etc.). P
2d50c 61 72 61 6d 65 74 65 72 20 69 67 6e 6f 72 65 4a  arameter ignoreJ
2d50d 75 6d 70 0a 2a 2a 20 69 73 20 74 68 65 20 69 6e  ump.** is the in
2d50e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20 63  struction that c
2d50f 6f 6e 74 72 6f 6c 20 73 68 6f 75 6c 64 20 6a 75  ontrol should ju
2d510 6d 70 20 74 6f 20 69 66 20 61 20 74 72 69 67 67  mp to if a trigg
2d511 65 72 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 72 61  er program.** ra
2d512 69 73 65 73 20 61 6e 20 49 47 4e 4f 52 45 20 65  ises an IGNORE e
2d513 78 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  xception..*/.SQL
2d514 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2d515 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
2d516 72 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20  rigger(.  Parse 
2d517 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
2d518 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
2d519 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54  */.  Trigger *pT
2d51a 72 69 67 67 65 72 2c 20 20 20 2f 2a 20 4c 69 73  rigger,   /* Lis
2d51b 74 20 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e  t of triggers on
2d51c 20 74 61 62 6c 65 20 70 54 61 62 20 2a 2f 0a 20   table pTab */. 
2d51d 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
2d51e 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
2d51f 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e  TK_UPDATE, TK_IN
2d520 53 45 52 54 2c 20 54 4b 5f 44 45 4c 45 54 45 20  SERT, TK_DELETE 
2d521 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2d522 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 43 68 61  Changes,  /* Cha
2d523 6e 67 65 73 20 6c 69 73 74 20 66 6f 72 20 61 6e  nges list for an
2d524 79 20 55 50 44 41 54 45 20 4f 46 20 74 72 69 67  y UPDATE OF trig
2d525 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 74 72  gers */.  int tr
2d526 5f 74 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f  _tm,           /
2d527 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52  * One of TRIGGER
2d528 5f 42 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52  _BEFORE, TRIGGER
2d529 5f 41 46 54 45 52 20 2a 2f 0a 20 20 54 61 62 6c  _AFTER */.  Tabl
2d52a 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
2d52b 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f   /* The table to
2d52c 20 63 6f 64 65 20 74 72 69 67 67 65 72 73 20 66   code triggers f
2d52d 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  rom */.  int reg
2d52e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
2d52f 20 54 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e   The first in an
2d530 20 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74   array of regist
2d531 65 72 73 20 28 73 65 65 20 61 62 6f 76 65 29 20  ers (see above) 
2d532 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 2c  */.  int orconf,
2d533 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 4e 20            /* ON 
2d534 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20  CONFLICT policy 
2d535 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65 4a  */.  int ignoreJ
2d536 75 6d 70 20 20 20 20 20 20 20 2f 2a 20 49 6e 73  ump       /* Ins
2d537 74 72 75 63 74 69 6f 6e 20 74 6f 20 6a 75 6d 70  truction to jump
2d538 20 74 6f 20 66 6f 72 20 52 41 49 53 45 28 49 47   to for RAISE(IG
2d539 4e 4f 52 45 29 20 2a 2f 0a 29 7b 0a 20 20 54 72  NORE) */.){.  Tr
2d53a 69 67 67 65 72 20 2a 70 3b 20 20 20 20 20 20 20  igger *p;       
2d53b 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74     /* Used to it
2d53c 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 70 54  erate through pT
2d53d 72 69 67 67 65 72 20 6c 69 73 74 20 2a 2f 0a 0a  rigger list */..
2d53e 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 54 4b    assert( op==TK
2d53f 5f 55 50 44 41 54 45 20 7c 7c 20 6f 70 3d 3d 54  _UPDATE || op==T
2d540 4b 5f 49 4e 53 45 52 54 20 7c 7c 20 6f 70 3d 3d  K_INSERT || op==
2d541 54 4b 5f 44 45 4c 45 54 45 20 29 3b 0a 20 20 61  TK_DELETE );.  a
2d542 73 73 65 72 74 28 20 74 72 5f 74 6d 3d 3d 54 52  ssert( tr_tm==TR
2d543 49 47 47 45 52 5f 42 45 46 4f 52 45 20 7c 7c 20  IGGER_BEFORE || 
2d544 74 72 5f 74 6d 3d 3d 54 52 49 47 47 45 52 5f 41  tr_tm==TRIGGER_A
2d545 46 54 45 52 20 29 3b 0a 20 20 61 73 73 65 72 74  FTER );.  assert
2d546 28 20 28 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45  ( (op==TK_UPDATE
2d547 29 3d 3d 28 70 43 68 61 6e 67 65 73 21 3d 30 29  )==(pChanges!=0)
2d548 20 29 3b 0a 0a 20 20 66 6f 72 28 70 3d 70 54 72   );..  for(p=pTr
2d549 69 67 67 65 72 3b 20 70 3b 20 70 3d 70 2d 3e 70  igger; p; p=p->p
2d54a 4e 65 78 74 29 7b 0a 0a 20 20 20 20 2f 2a 20 53  Next){..    /* S
2d54b 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 3a 20  anity checking: 
2d54c 20 54 68 65 20 73 63 68 65 6d 61 20 66 6f 72 20   The schema for 
2d54d 74 68 65 20 74 72 69 67 67 65 72 20 61 6e 64 20  the trigger and 
2d54e 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 61 72  for the table ar
2d54f 65 0a 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20  e.    ** always 
2d550 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 74 72  defined.  The tr
2d551 69 67 67 65 72 20 6d 75 73 74 20 62 65 20 69 6e  igger must be in
2d552 20 74 68 65 20 73 61 6d 65 20 73 63 68 65 6d 61   the same schema
2d553 20 61 73 20 74 68 65 20 74 61 62 6c 65 0a 20 20   as the table.  
2d554 20 20 2a 2a 20 6f 72 20 65 6c 73 65 20 69 74 20    ** or else it 
2d555 6d 75 73 74 20 62 65 20 61 20 54 45 4d 50 20 74  must be a TEMP t
2d556 72 69 67 67 65 72 2e 20 2a 2f 0a 20 20 20 20 61  rigger. */.    a
2d557 73 73 65 72 74 28 20 70 2d 3e 70 53 63 68 65 6d  ssert( p->pSchem
2d558 61 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  a!=0 );.    asse
2d559 72 74 28 20 70 2d 3e 70 54 61 62 53 63 68 65 6d  rt( p->pTabSchem
2d55a 61 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  a!=0 );.    asse
2d55b 72 74 28 20 70 2d 3e 70 53 63 68 65 6d 61 3d 3d  rt( p->pSchema==
2d55c 70 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 0a 20  p->pTabSchema . 
2d55d 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 70 53          || p->pS
2d55e 63 68 65 6d 61 3d 3d 70 50 61 72 73 65 2d 3e 64  chema==pParse->d
2d55f 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d  b->aDb[1].pSchem
2d560 61 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 74  a );..    /* Det
2d561 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 77  ermine whether w
2d562 65 20 73 68 6f 75 6c 64 20 63 6f 64 65 20 74 68  e should code th
2d563 69 73 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20  is trigger */.  
2d564 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 6f 70 20    if( p->op==op 
2d565 0a 20 20 20 20 20 26 26 20 70 2d 3e 74 72 5f 74  .     && p->tr_t
2d566 6d 3d 3d 74 72 5f 74 6d 20 0a 20 20 20 20 20 26  m==tr_tm .     &
2d567 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65  & checkColumnOve
2d568 72 6c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e 73  rlap(p->pColumns
2d569 2c 20 70 43 68 61 6e 67 65 73 29 0a 20 20 20 20  , pChanges).    
2d56a 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2d56b 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44 69  CodeRowTriggerDi
2d56c 72 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20  rect(pParse, p, 
2d56d 70 54 61 62 2c 20 72 65 67 2c 20 6f 72 63 6f 6e  pTab, reg, orcon
2d56e 66 2c 20 69 67 6e 6f 72 65 4a 75 6d 70 29 3b 0a  f, ignoreJump);.
2d56f 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
2d570 2a 2a 20 54 72 69 67 67 65 72 73 20 6d 61 79 20  ** Triggers may 
2d571 61 63 63 65 73 73 20 76 61 6c 75 65 73 20 73 74  access values st
2d572 6f 72 65 64 20 69 6e 20 74 68 65 20 6f 6c 64 2e  ored in the old.
2d573 2a 20 6f 72 20 6e 65 77 2e 2a 20 70 73 65 75 64  * or new.* pseud
2d574 6f 2d 74 61 62 6c 65 2e 20 0a 2a 2a 20 54 68 69  o-table. .** Thi
2d575 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
2d576 6e 73 20 61 20 33 32 2d 62 69 74 20 62 69 74 6d  ns a 32-bit bitm
2d577 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67 20 77  ask indicating w
2d578 68 69 63 68 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  hich columns of 
2d579 74 68 65 20 0a 2a 2a 20 6f 6c 64 2e 2a 20 6f 72  the .** old.* or
2d57a 20 6e 65 77 2e 2a 20 74 61 62 6c 65 73 20 61 63   new.* tables ac
2d57b 74 75 61 6c 6c 79 20 61 72 65 20 75 73 65 64 20  tually are used 
2d57c 62 79 20 74 72 69 67 67 65 72 73 2e 20 54 68 69  by triggers. Thi
2d57d 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a  s information .*
2d57e 2a 20 6d 61 79 20 62 65 20 75 73 65 64 20 62 79  * may be used by
2d57f 20 74 68 65 20 63 61 6c 6c 65 72 2c 20 66 6f 72   the caller, for
2d580 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 76 6f   example, to avo
2d581 69 64 20 68 61 76 69 6e 67 20 74 6f 20 6c 6f 61  id having to loa
2d582 64 20 74 68 65 20 65 6e 74 69 72 65 0a 2a 2a 20  d the entire.** 
2d583 6f 6c 64 2e 2a 20 72 65 63 6f 72 64 20 69 6e 74  old.* record int
2d584 6f 20 6d 65 6d 6f 72 79 20 77 68 65 6e 20 65 78  o memory when ex
2d585 65 63 75 74 69 6e 67 20 61 6e 20 55 50 44 41 54  ecuting an UPDAT
2d586 45 20 6f 72 20 44 45 4c 45 54 45 20 63 6f 6d 6d  E or DELETE comm
2d587 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30  and..**.** Bit 0
2d588 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e 65 64   of the returned
2d589 20 6d 61 73 6b 20 69 73 20 73 65 74 20 69 66 20   mask is set if 
2d58a 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f  the left-most co
2d58b 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 74  lumn of the.** t
2d58c 61 62 6c 65 20 6d 61 79 20 62 65 20 61 63 63 65  able may be acce
2d58d 73 73 65 64 20 75 73 69 6e 67 20 61 6e 20 5b 6f  ssed using an [o
2d58e 6c 64 7c 6e 65 77 5d 2e 3c 63 6f 6c 3e 20 72 65  ld|new].<col> re
2d58f 66 65 72 65 6e 63 65 2e 20 42 69 74 20 31 20 69  ference. Bit 1 i
2d590 73 20 73 65 74 20 69 66 0a 2a 2a 20 74 68 65 20  s set if.** the 
2d591 73 65 63 6f 6e 64 20 6c 65 66 74 6d 6f 73 74 20  second leftmost 
2d592 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20  column value is 
2d593 72 65 71 75 69 72 65 64 2c 20 61 6e 64 20 73 6f  required, and so
2d594 20 6f 6e 2e 20 49 66 20 74 68 65 72 65 0a 2a 2a   on. If there.**
2d595 20 61 72 65 20 6d 6f 72 65 20 74 68 61 6e 20 33   are more than 3
2d596 32 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  2 columns in the
2d597 20 74 61 62 6c 65 2c 20 61 6e 64 20 61 74 20 6c   table, and at l
2d598 65 61 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 20  east one of the 
2d599 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 77 69 74 68 20  columns.** with 
2d59a 61 6e 20 69 6e 64 65 78 20 67 72 65 61 74 65 72  an index greater
2d59b 20 74 68 61 6e 20 33 32 20 6d 61 79 20 62 65 20   than 32 may be 
2d59c 61 63 63 65 73 73 65 64 2c 20 30 78 66 66 66 66  accessed, 0xffff
2d59d 66 66 66 66 20 69 73 20 72 65 74 75 72 6e 65 64  ffff is returned
2d59e 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f  ..**.** It is no
2d59f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 64 65  t possible to de
2d5a0 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20 6f  termine if the o
2d5a1 6c 64 2e 72 6f 77 69 64 20 6f 72 20 6e 65 77 2e  ld.rowid or new.
2d5a2 72 6f 77 69 64 20 63 6f 6c 75 6d 6e 20 69 73 20  rowid column is 
2d5a3 0a 2a 2a 20 61 63 63 65 73 73 65 64 20 62 79 20  .** accessed by 
2d5a4 74 72 69 67 67 65 72 73 2e 20 54 68 65 20 63 61  triggers. The ca
2d5a5 6c 6c 65 72 20 6d 75 73 74 20 61 6c 77 61 79 73  ller must always
2d5a6 20 61 73 73 75 6d 65 20 74 68 61 74 20 69 74 20   assume that it 
2d5a7 69 73 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65  is..**.** Parame
2d5a8 74 65 72 20 69 73 4e 65 77 20 6d 75 73 74 20 62  ter isNew must b
2d5a9 65 20 65 69 74 68 65 72 20 31 20 6f 72 20 30 2e  e either 1 or 0.
2d5aa 20 49 66 20 69 74 20 69 73 20 30 2c 20 74 68 65   If it is 0, the
2d5ab 6e 20 74 68 65 20 6d 61 73 6b 20 72 65 74 75 72  n the mask retur
2d5ac 6e 65 64 0a 2a 2a 20 61 70 70 6c 69 65 73 20 74  ned.** applies t
2d5ad 6f 20 74 68 65 20 6f 6c 64 2e 2a 20 74 61 62 6c  o the old.* tabl
2d5ae 65 2e 20 49 66 20 31 2c 20 74 68 65 20 6e 65 77  e. If 1, the new
2d5af 2e 2a 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  .* table..**.** 
2d5b0 50 61 72 61 6d 65 74 65 72 20 74 72 5f 74 6d 20  Parameter tr_tm 
2d5b1 6d 75 73 74 20 62 65 20 61 20 6d 61 73 6b 20 77  must be a mask w
2d5b2 69 74 68 20 6f 6e 65 20 6f 72 20 62 6f 74 68 20  ith one or both 
2d5b3 6f 66 20 74 68 65 20 54 52 49 47 47 45 52 5f 42  of the TRIGGER_B
2d5b4 45 46 4f 52 45 0a 2a 2a 20 61 6e 64 20 54 52 49  EFORE.** and TRI
2d5b5 47 47 45 52 5f 41 46 54 45 52 20 62 69 74 73 20  GGER_AFTER bits 
2d5b6 73 65 74 2e 20 56 61 6c 75 65 73 20 61 63 63 65  set. Values acce
2d5b7 73 73 65 64 20 62 79 20 42 45 46 4f 52 45 20 74  ssed by BEFORE t
2d5b8 72 69 67 67 65 72 73 20 61 72 65 20 6f 6e 6c 79  riggers are only
2d5b9 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20  .** included in 
2d5ba 74 68 65 20 72 65 74 75 72 6e 65 64 20 6d 61 73  the returned mas
2d5bb 6b 20 69 66 20 74 68 65 20 54 52 49 47 47 45 52  k if the TRIGGER
2d5bc 5f 42 45 46 4f 52 45 20 62 69 74 20 69 73 20 73  _BEFORE bit is s
2d5bd 65 74 20 69 6e 20 74 68 65 0a 2a 2a 20 74 72 5f  et in the.** tr_
2d5be 74 6d 20 70 61 72 61 6d 65 74 65 72 2e 20 53 69  tm parameter. Si
2d5bf 6d 69 6c 61 72 6c 79 2c 20 76 61 6c 75 65 73 20  milarly, values 
2d5c0 61 63 63 65 73 73 65 64 20 62 79 20 41 46 54 45  accessed by AFTE
2d5c1 52 20 74 72 69 67 67 65 72 73 20 61 72 65 20 6f  R triggers are o
2d5c2 6e 6c 79 0a 2a 2a 20 69 6e 63 6c 75 64 65 64 20  nly.** included 
2d5c3 69 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  in the returned 
2d5c4 6d 61 73 6b 20 69 66 20 74 68 65 20 54 52 49 47  mask if the TRIG
2d5c5 47 45 52 5f 41 46 54 45 52 20 62 69 74 20 69 73  GER_AFTER bit is
2d5c6 20 73 65 74 20 69 6e 20 74 72 5f 74 6d 2e 0a 2a   set in tr_tm..*
2d5c7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2d5c8 20 75 33 32 20 73 71 6c 69 74 65 33 54 72 69 67   u32 sqlite3Trig
2d5c9 67 65 72 43 6f 6c 6d 61 73 6b 28 0a 20 20 50 61  gerColmask(.  Pa
2d5ca 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2d5cb 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74     /* Parse cont
2d5cc 65 78 74 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  ext */.  Trigger
2d5cd 20 2a 70 54 72 69 67 67 65 72 2c 20 20 20 2f 2a   *pTrigger,   /*
2d5ce 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72   List of trigger
2d5cf 73 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20  s on table pTab 
2d5d0 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2d5d1 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 43 68 61  Changes,  /* Cha
2d5d2 6e 67 65 73 20 6c 69 73 74 20 66 6f 72 20 61 6e  nges list for an
2d5d3 79 20 55 50 44 41 54 45 20 4f 46 20 74 72 69 67  y UPDATE OF trig
2d5d4 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73  gers */.  int is
2d5d5 4e 65 77 2c 20 20 20 20 20 20 20 20 20 20 20 2f  New,           /
2d5d6 2a 20 31 20 66 6f 72 20 6e 65 77 2e 2a 20 72 65  * 1 for new.* re
2d5d7 66 20 6d 61 73 6b 2c 20 30 20 66 6f 72 20 6f 6c  f mask, 0 for ol
2d5d8 64 2e 2a 20 72 65 66 20 6d 61 73 6b 20 2a 2f 0a  d.* ref mask */.
2d5d9 20 20 69 6e 74 20 74 72 5f 74 6d 2c 20 20 20 20    int tr_tm,    
2d5da 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
2d5db 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  f TRIGGER_BEFORE
2d5dc 7c 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 2a  |TRIGGER_AFTER *
2d5dd 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
2d5de 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2d5df 74 61 62 6c 65 20 74 6f 20 63 6f 64 65 20 74 72  table to code tr
2d5e0 69 67 67 65 72 73 20 66 72 6f 6d 20 2a 2f 0a 20  iggers from */. 
2d5e1 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20 20   int orconf     
2d5e2 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74        /* Default
2d5e3 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c   ON CONFLICT pol
2d5e4 69 63 79 20 66 6f 72 20 74 72 69 67 67 65 72 20  icy for trigger 
2d5e5 73 74 65 70 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f  steps */.){.  co
2d5e6 6e 73 74 20 69 6e 74 20 6f 70 20 3d 20 70 43 68  nst int op = pCh
2d5e7 61 6e 67 65 73 20 3f 20 54 4b 5f 55 50 44 41 54  anges ? TK_UPDAT
2d5e8 45 20 3a 20 54 4b 5f 44 45 4c 45 54 45 3b 0a 20  E : TK_DELETE;. 
2d5e9 20 75 33 32 20 6d 61 73 6b 20 3d 20 30 3b 0a 20   u32 mask = 0;. 
2d5ea 20 54 72 69 67 67 65 72 20 2a 70 3b 0a 0a 20 20   Trigger *p;..  
2d5eb 61 73 73 65 72 74 28 20 69 73 4e 65 77 3d 3d 31  assert( isNew==1
2d5ec 20 7c 7c 20 69 73 4e 65 77 3d 3d 30 20 29 3b 0a   || isNew==0 );.
2d5ed 20 20 66 6f 72 28 70 3d 70 54 72 69 67 67 65 72    for(p=pTrigger
2d5ee 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
2d5ef 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d  {.    if( p->op=
2d5f0 3d 6f 70 20 26 26 20 28 74 72 5f 74 6d 26 70 2d  =op && (tr_tm&p-
2d5f1 3e 74 72 5f 74 6d 29 0a 20 20 20 20 20 26 26 20  >tr_tm).     && 
2d5f2 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 6c  checkColumnOverl
2d5f3 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 70  ap(p->pColumns,p
2d5f4 43 68 61 6e 67 65 73 29 0a 20 20 20 20 29 7b 0a  Changes).    ){.
2d5f5 20 20 20 20 20 20 54 72 69 67 67 65 72 50 72 67        TriggerPrg
2d5f6 20 2a 70 50 72 67 3b 0a 20 20 20 20 20 20 70 50   *pPrg;.      pP
2d5f7 72 67 20 3d 20 67 65 74 52 6f 77 54 72 69 67 67  rg = getRowTrigg
2d5f8 65 72 28 70 50 61 72 73 65 2c 20 70 2c 20 70 54  er(pParse, p, pT
2d5f9 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 20  ab, orconf);.   
2d5fa 20 20 20 69 66 28 20 70 50 72 67 20 29 7b 0a 20     if( pPrg ){. 
2d5fb 20 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20 70         mask |= p
2d5fc 50 72 67 2d 3e 61 43 6f 6c 6d 61 73 6b 5b 69 73  Prg->aColmask[is
2d5fd 4e 65 77 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  New];.      }.  
2d5fe 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
2d5ff 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 23 65 6e 64 69  n mask;.}..#endi
2d600 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
2d601 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
2d602 52 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  R) */../********
2d603 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 72  ****** End of tr
2d604 69 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  igger.c ********
2d605 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d606 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d607 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2d608 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
2d609 65 20 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a  e update.c *****
2d60a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d60b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d60c 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
2d60d 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a  1 September 15.*
2d60e 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
2d60f 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
2d610 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
2d611 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
2d612 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
2d613 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
2d614 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
2d615 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
2d616 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
2d617 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
2d618 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
2d619 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
2d61a 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
2d61b 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
2d61c 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
2d61d 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
2d61e 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
2d61f 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
2d620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d621 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d622 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d623 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
2d624 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
2d625 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69  ins C code routi
2d626 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61 6c  nes that are cal
2d627 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
2d628 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c 65 20 55  r.** to handle U
2d629 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 73  PDATE statements
2d62a 2e 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  ..*/..#ifndef SQ
2d62b 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2d62c 4c 54 41 42 4c 45 0a 2f 2a 20 46 6f 72 77 61 72  LTABLE./* Forwar
2d62d 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f  d declaration */
2d62e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64  .static void upd
2d62f 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28  ateVirtualTable(
2d630 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2d631 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  ,       /* The p
2d632 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2d633 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
2d634 63 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  c,       /* The 
2d635 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f  virtual table to
2d636 20 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a   be modified */.
2d637 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
2d638 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
2d639 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
2d63a 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e   ExprList *pChan
2d63b 67 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c  ges,  /* The col
2d63c 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69  umns to change i
2d63d 6e 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  n the UPDATE sta
2d63e 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72  tement */.  Expr
2d63f 20 2a 70 52 6f 77 69 64 45 78 70 72 2c 20 20 20   *pRowidExpr,   
2d640 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75   /* Expression u
2d641 73 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65  sed to recompute
2d642 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20   the rowid */.  
2d643 69 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20  int *aXRef,     
2d644 20 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20       /* Mapping 
2d645 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  from columns of 
2d646 70 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20  pTab to entries 
2d647 69 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20  in pChanges */. 
2d648 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20   Expr *pWhere   
2d649 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63        /* WHERE c
2d64a 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44  lause of the UPD
2d64b 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ATE statement */
2d64c 0a 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .);.#endif /* SQ
2d64d 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2d64e 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  LTABLE */../*.**
2d64f 20 54 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   The most recent
2d650 6c 79 20 63 6f 64 65 64 20 69 6e 73 74 72 75 63  ly coded instruc
2d651 74 69 6f 6e 20 77 61 73 20 61 6e 20 4f 50 5f 43  tion was an OP_C
2d652 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69 65 76  olumn to retriev
2d653 65 20 74 68 65 0a 2a 2a 20 69 2d 74 68 20 63 6f  e the.** i-th co
2d654 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 70 54  lumn of table pT
2d655 61 62 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65  ab. This routine
2d656 20 73 65 74 73 20 74 68 65 20 50 34 20 70 61 72   sets the P4 par
2d657 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 0a 2a  ameter of the .*
2d658 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 74  * OP_Column to t
2d659 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
2d65a 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20  , if any..**.** 
2d65b 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  The default valu
2d65c 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 69 73  e of a column is
2d65d 20 73 70 65 63 69 66 69 65 64 20 62 79 20 61 20   specified by a 
2d65e 44 45 46 41 55 4c 54 20 63 6c 61 75 73 65 20 69  DEFAULT clause i
2d65f 6e 20 74 68 65 20 0a 2a 2a 20 63 6f 6c 75 6d 6e  n the .** column
2d660 20 64 65 66 69 6e 69 74 69 6f 6e 2e 20 54 68 69   definition. Thi
2d661 73 20 77 61 73 20 65 69 74 68 65 72 20 73 75 70  s was either sup
2d662 70 6c 69 65 64 20 62 79 20 74 68 65 20 75 73 65  plied by the use
2d663 72 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c 65  r when the table
2d664 0a 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 2c  .** was created,
2d665 20 6f 72 20 61 64 64 65 64 20 6c 61 74 65 72 20   or added later 
2d666 74 6f 20 74 68 65 20 74 61 62 6c 65 20 64 65 66  to the table def
2d667 69 6e 69 74 69 6f 6e 20 62 79 20 61 6e 20 41 4c  inition by an AL
2d668 54 45 52 20 54 41 42 4c 45 0a 2a 2a 20 63 6f 6d  TER TABLE.** com
2d669 6d 61 6e 64 2e 20 49 66 20 74 68 65 20 6c 61 74  mand. If the lat
2d66a 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20 72 6f  ter, then the ro
2d66b 77 2d 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65  w-records in the
2d66c 20 74 61 62 6c 65 20 62 74 72 65 65 20 6f 6e 20   table btree on 
2d66d 64 69 73 6b 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20  disk.** may not 
2d66e 63 6f 6e 74 61 69 6e 20 61 20 76 61 6c 75 65 20  contain a value 
2d66f 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61  for the column a
2d670 6e 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  nd the default v
2d671 61 6c 75 65 2c 20 74 61 6b 65 6e 0a 2a 2a 20 66  alue, taken.** f
2d672 72 6f 6d 20 74 68 65 20 50 34 20 70 61 72 61 6d  rom the P4 param
2d673 65 74 65 72 20 6f 66 20 74 68 65 20 4f 50 5f 43  eter of the OP_C
2d674 6f 6c 75 6d 6e 20 69 6e 73 74 72 75 63 74 69 6f  olumn instructio
2d675 6e 2c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  n, is returned i
2d676 6e 73 74 65 61 64 2e 0a 2a 2a 20 49 66 20 74 68  nstead..** If th
2d677 65 20 66 6f 72 6d 65 72 2c 20 74 68 65 6e 20 61  e former, then a
2d678 6c 6c 20 72 6f 77 2d 72 65 63 6f 72 64 73 20 61  ll row-records a
2d679 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f  re guaranteed to
2d67a 20 69 6e 63 6c 75 64 65 20 61 20 76 61 6c 75 65   include a value
2d67b 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  .** for the colu
2d67c 6d 6e 20 61 6e 64 20 74 68 65 20 50 34 20 76 61  mn and the P4 va
2d67d 6c 75 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69  lue is not requi
2d67e 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6c 75 6d  red..**.** Colum
2d67f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 63 72  n definitions cr
2d680 65 61 74 65 64 20 62 79 20 61 6e 20 41 4c 54 45  eated by an ALTE
2d681 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 20  R TABLE command 
2d682 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 0a 2a  may only have .*
2d683 2a 20 6c 69 74 65 72 61 6c 20 64 65 66 61 75 6c  * literal defaul
2d684 74 20 76 61 6c 75 65 73 20 73 70 65 63 69 66 69  t values specifi
2d685 65 64 3a 20 61 20 6e 75 6d 62 65 72 2c 20 6e 75  ed: a number, nu
2d686 6c 6c 20 6f 72 20 61 20 73 74 72 69 6e 67 2e 20  ll or a string. 
2d687 28 49 66 20 61 20 6d 6f 72 65 0a 2a 2a 20 63 6f  (If a more.** co
2d688 6d 70 6c 69 63 61 74 65 64 20 64 65 66 61 75 6c  mplicated defaul
2d689 74 20 65 78 70 72 65 73 73 69 6f 6e 20 76 61 6c  t expression val
2d68a 75 65 20 77 61 73 20 70 72 6f 76 69 64 65 64 2c  ue was provided,
2d68b 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 65 64   it is evaluated
2d68c 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 4c   .** when the AL
2d68d 54 45 52 20 54 41 42 4c 45 20 69 73 20 65 78 65  TER TABLE is exe
2d68e 63 75 74 65 64 20 61 6e 64 20 6f 6e 65 20 6f 66  cuted and one of
2d68f 20 74 68 65 20 6c 69 74 65 72 61 6c 20 76 61 6c   the literal val
2d690 75 65 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69  ues written.** i
2d691 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d  nto the sqlite_m
2d692 61 73 74 65 72 20 74 61 62 6c 65 2e 29 0a 2a 2a  aster table.).**
2d693 0a 2a 2a 20 54 68 65 72 65 66 6f 72 65 2c 20 74  .** Therefore, t
2d694 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20  he P4 parameter 
2d695 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64  is only required
2d696 20 69 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   if the default 
2d697 76 61 6c 75 65 20 66 6f 72 0a 2a 2a 20 74 68 65  value for.** the
2d698 20 63 6f 6c 75 6d 6e 20 69 73 20 61 20 6c 69 74   column is a lit
2d699 65 72 61 6c 20 6e 75 6d 62 65 72 2c 20 73 74 72  eral number, str
2d69a 69 6e 67 20 6f 72 20 6e 75 6c 6c 2e 20 54 68 65  ing or null. The
2d69b 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f   sqlite3ValueFro
2d69c 6d 45 78 70 72 28 29 0a 2a 2a 20 66 75 6e 63 74  mExpr().** funct
2d69d 69 6f 6e 20 69 73 20 63 61 70 61 62 6c 65 20 6f  ion is capable o
2d69e 66 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74  f transforming t
2d69f 68 65 73 65 20 74 79 70 65 73 20 6f 66 20 65 78  hese types of ex
2d6a0 70 72 65 73 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a  pressions into.*
2d6a1 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  * sqlite3_value 
2d6a2 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 49  objects..**.** I
2d6a3 66 20 70 61 72 61 6d 65 74 65 72 20 69 52 65 67  f parameter iReg
2d6a4 20 69 73 20 6e 6f 74 20 6e 65 67 61 74 69 76 65   is not negative
2d6a5 2c 20 63 6f 64 65 20 61 6e 20 4f 50 5f 52 65 61  , code an OP_Rea
2d6a6 6c 41 66 66 69 6e 69 74 79 20 69 6e 73 74 72 75  lAffinity instru
2d6a7 63 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 72 65 67 69  ction.** on regi
2d6a8 73 74 65 72 20 69 52 65 67 2e 20 54 68 69 73 20  ster iReg. This 
2d6a9 69 73 20 75 73 65 64 20 77 68 65 6e 20 61 6e 20  is used when an 
2d6aa 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 67  equivalent integ
2d6ab 65 72 20 76 61 6c 75 65 20 69 73 20 0a 2a 2a 20  er value is .** 
2d6ac 73 74 6f 72 65 64 20 69 6e 20 70 6c 61 63 65 20  stored in place 
2d6ad 6f 66 20 61 6e 20 38 2d 62 79 74 65 20 66 6c 6f  of an 8-byte flo
2d6ae 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
2d6af 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 61  e in order to sa
2d6b0 76 65 20 0a 2a 2a 20 73 70 61 63 65 2e 0a 2a 2f  ve .** space..*/
2d6b1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2d6b2 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c 75  void sqlite3Colu
2d6b3 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20 2a  mnDefault(Vdbe *
2d6b4 76 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  v, Table *pTab, 
2d6b5 69 6e 74 20 69 2c 20 69 6e 74 20 69 52 65 67 29  int i, int iReg)
2d6b6 7b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  {.  assert( pTab
2d6b7 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 21 70 54  !=0 );.  if( !pT
2d6b8 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20  ab->pSelect ){. 
2d6b9 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
2d6ba 20 2a 70 56 61 6c 75 65 3b 0a 20 20 20 20 75 38   *pValue;.    u8
2d6bb 20 65 6e 63 20 3d 20 45 4e 43 28 73 71 6c 69 74   enc = ENC(sqlit
2d6bc 65 33 56 64 62 65 44 62 28 76 29 29 3b 0a 20 20  e3VdbeDb(v));.  
2d6bd 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d    Column *pCol =
2d6be 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 3b   &pTab->aCol[i];
2d6bf 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  .    VdbeComment
2d6c0 28 28 76 2c 20 22 25 73 2e 25 73 22 2c 20 70 54  ((v, "%s.%s", pT
2d6c1 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70 43 6f 6c 2d  ab->zName, pCol-
2d6c2 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61 73  >zName));.    as
2d6c3 73 65 72 74 28 20 69 3c 70 54 61 62 2d 3e 6e 43  sert( i<pTab->nC
2d6c4 6f 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ol );.    sqlite
2d6c5 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 73  3ValueFromExpr(s
2d6c6 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c  qlite3VdbeDb(v),
2d6c7 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 2c 20 65 6e   pCol->pDflt, en
2d6c8 63 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  c, .            
2d6c9 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
2d6ca 6c 2d 3e 61 66 66 69 6e 69 74 79 2c 20 26 70 56  l->affinity, &pV
2d6cb 61 6c 75 65 29 3b 0a 20 20 20 20 69 66 28 20 70  alue);.    if( p
2d6cc 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 20 20 73  Value ){.      s
2d6cd 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2d6ce 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73 74  P4(v, -1, (const
2d6cf 20 63 68 61 72 20 2a 29 70 56 61 6c 75 65 2c 20   char *)pValue, 
2d6d0 50 34 5f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a 23  P4_MEM);.    }.#
2d6d1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2d6d2 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
2d6d3 54 0a 20 20 20 20 69 66 28 20 69 52 65 67 3e 3d  T.    if( iReg>=
2d6d4 30 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  0 && pTab->aCol[
2d6d5 69 5d 2e 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c  i].affinity==SQL
2d6d6 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 29 7b 0a  ITE_AFF_REAL ){.
2d6d7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2d6d8 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65  eAddOp1(v, OP_Re
2d6d9 61 6c 41 66 66 69 6e 69 74 79 2c 20 69 52 65 67  alAffinity, iReg
2d6da 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
2d6db 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f    }.}../*.** Pro
2d6dc 63 65 73 73 20 61 6e 20 55 50 44 41 54 45 20 73  cess an UPDATE s
2d6dd 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
2d6de 20 20 55 50 44 41 54 45 20 4f 52 20 49 47 4e 4f    UPDATE OR IGNO
2d6df 52 45 20 74 61 62 6c 65 5f 77 78 79 7a 20 53 45  RE table_wxyz SE
2d6e0 54 20 61 3d 62 2c 20 63 3d 64 20 57 48 45 52 45  T a=b, c=d WHERE
2d6e1 20 65 3c 35 20 41 4e 44 20 66 20 4e 4f 54 20 4e   e<5 AND f NOT N
2d6e2 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20  ULL;.**         
2d6e3 20 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f   \_______/ \____
2d6e4 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f  ____/     \_____
2d6e5 5f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f  _/       \______
2d6e6 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 20 20 20  __________/.*   
2d6e7 20 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72           onError
2d6e8 20 20 20 70 54 61 62 4c 69 73 74 20 20 20 20 20     pTabList     
2d6e9 20 70 43 68 61 6e 67 65 73 20 20 20 20 20 20 20   pChanges       
2d6ea 20 20 20 20 20 20 70 57 68 65 72 65 0a 2a 2f 0a        pWhere.*/.
2d6eb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2d6ec 6f 69 64 20 73 71 6c 69 74 65 33 55 70 64 61 74  oid sqlite3Updat
2d6ed 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
2d6ee 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  se,         /* T
2d6ef 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
2d6f0 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
2d6f1 70 54 61 62 4c 69 73 74 2c 20 20 20 20 20 2f 2a  pTabList,     /*
2d6f2 20 54 68 65 20 74 61 62 6c 65 20 69 6e 20 77 68   The table in wh
2d6f3 69 63 68 20 77 65 20 73 68 6f 75 6c 64 20 63 68  ich we should ch
2d6f4 61 6e 67 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20  ange things */. 
2d6f5 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e   ExprList *pChan
2d6f6 67 65 73 2c 20 20 20 20 2f 2a 20 54 68 69 6e 67  ges,    /* Thing
2d6f7 73 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 20  s to be changed 
2d6f8 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
2d6f9 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e,          /* T
2d6fa 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
2d6fb 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f    May be null */
2d6fc 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20  .  int onError  
2d6fd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77            /* How
2d6fe 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74   to handle const
2d6ff 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a  raint errors */.
2d700 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20  ){.  int i, j;  
2d701 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2d702 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
2d703 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
2d704 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2d705 74 61 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61  table to be upda
2d706 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ted */.  int add
2d707 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
2d708 2f 2a 20 56 44 42 45 20 69 6e 73 74 72 75 63 74  /* VDBE instruct
2d709 69 6f 6e 20 61 64 64 72 65 73 73 20 6f 66 20 74  ion address of t
2d70a 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
2d70b 6c 6f 6f 70 20 2a 2f 0a 20 20 57 68 65 72 65 49  loop */.  WhereI
2d70c 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20  nfo *pWInfo;    
2d70d 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
2d70e 61 62 6f 75 74 20 74 68 65 20 57 48 45 52 45 20  about the WHERE 
2d70f 63 6c 61 75 73 65 20 2a 2f 0a 20 20 56 64 62 65  clause */.  Vdbe
2d710 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
2d711 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
2d712 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  l database engin
2d713 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  e */.  Index *pI
2d714 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  dx;           /*
2d715 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
2d716 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69  r indices */.  i
2d717 6e 74 20 6e 49 64 78 3b 20 20 20 20 20 20 20 20  nt nIdx;        
2d718 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2d719 6f 66 20 69 6e 64 69 63 65 73 20 74 68 61 74 20  of indices that 
2d71a 6e 65 65 64 20 75 70 64 61 74 69 6e 67 20 2a 2f  need updating */
2d71b 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20  .  int iCur;    
2d71c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42            /* VDB
2d71d 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E Cursor number 
2d71e 6f 66 20 70 54 61 62 20 2a 2f 0a 20 20 73 71 6c  of pTab */.  sql
2d71f 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
2d720 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
2d721 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ase structure */
2d722 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20  .  int *aRegIdx 
2d723 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 6e 65  = 0;      /* One
2d724 20 72 65 67 69 73 74 65 72 20 61 73 73 69 67 6e   register assign
2d725 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65 78  ed to each index
2d726 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a   to be updated *
2d727 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 20 3d  /.  int *aXRef =
2d728 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 58   0;        /* aX
2d729 52 65 66 5b 69 5d 20 69 73 20 74 68 65 20 69 6e  Ref[i] is the in
2d72a 64 65 78 20 69 6e 20 70 43 68 61 6e 67 65 73 2d  dex in pChanges-
2d72b 3e 61 5b 5d 20 6f 66 20 74 68 65 0a 20 20 20 20  >a[] of the.    
2d72c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d72d 20 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72 65       ** an expre
2d72e 73 73 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 2d  ssion for the i-
2d72f 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
2d730 20 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 20 20   table..        
2d731 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d732 20 2a 2a 20 61 58 52 65 66 5b 69 5d 3d 3d 2d 31   ** aXRef[i]==-1
2d733 20 69 66 20 74 68 65 20 69 2d 74 68 20 63 6f 6c   if the i-th col
2d734 75 6d 6e 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  umn is not chang
2d735 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 63 68 6e  ed. */.  int chn
2d736 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  gRowid;         
2d737 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 72  /* True if the r
2d738 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20  ecord number is 
2d739 62 65 69 6e 67 20 63 68 61 6e 67 65 64 20 2a 2f  being changed */
2d73a 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64 45  .  Expr *pRowidE
2d73b 78 70 72 20 3d 20 30 3b 20 20 2f 2a 20 45 78 70  xpr = 0;  /* Exp
2d73c 72 65 73 73 69 6f 6e 20 64 65 66 69 6e 69 6e 67  ression defining
2d73d 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
2d73e 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20  number */.  int 
2d73f 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b 20 20 20 20  openAll = 0;    
2d740 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c     /* True if al
2d741 6c 20 69 6e 64 69 63 65 73 20 6e 65 65 64 20 74  l indices need t
2d742 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20  o be opened */. 
2d743 20 41 75 74 68 43 6f 6e 74 65 78 74 20 73 43 6f   AuthContext sCo
2d744 6e 74 65 78 74 3b 20 20 2f 2a 20 54 68 65 20 61  ntext;  /* The a
2d745 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e  uthorization con
2d746 74 65 78 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  text */.  NameCo
2d747 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20  ntext sNC;      
2d748 20 2f 2a 20 54 68 65 20 6e 61 6d 65 2d 63 6f 6e   /* The name-con
2d749 74 65 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20  text to resolve 
2d74a 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a  expressions in *
2d74b 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
2d74c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2d74d 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
2d74e 67 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  g the table bein
2d74f 67 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69  g updated */.  i
2d750 6e 74 20 6f 6b 4f 6e 65 50 61 73 73 3b 20 20 20  nt okOnePass;   
2d751 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
2d752 72 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72  r one-pass algor
2d753 69 74 68 6d 20 77 69 74 68 6f 75 74 20 74 68 65  ithm without the
2d754 20 46 49 46 4f 20 2a 2f 0a 20 20 69 6e 74 20 68   FIFO */.  int h
2d755 61 73 46 4b 3b 20 20 20 20 20 20 20 20 20 20 20  asFK;           
2d756 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66 6f 72    /* True if for
2d757 65 69 67 6e 20 6b 65 79 20 70 72 6f 63 65 73 73  eign key process
2d758 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 20  ing is required 
2d759 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
2d75a 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
2d75b 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20 20 20    int isView;   
2d75c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2d75d 20 77 68 65 6e 20 75 70 64 61 74 69 6e 67 20 61   when updating a
2d75e 20 76 69 65 77 20 28 49 4e 53 54 45 41 44 20 4f   view (INSTEAD O
2d75f 46 20 74 72 69 67 67 65 72 29 20 2a 2f 0a 20 20  F trigger) */.  
2d760 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
2d761 72 3b 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f  r;     /* List o
2d762 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54  f triggers on pT
2d763 61 62 2c 20 69 66 20 72 65 71 75 69 72 65 64 20  ab, if required 
2d764 2a 2f 0a 20 20 69 6e 74 20 74 6d 61 73 6b 3b 20  */.  int tmask; 
2d765 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
2d766 61 73 6b 20 6f 66 20 54 52 49 47 47 45 52 5f 42  ask of TRIGGER_B
2d767 45 46 4f 52 45 7c 54 52 49 47 47 45 52 5f 41 46  EFORE|TRIGGER_AF
2d768 54 45 52 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20  TER */.#endif.  
2d769 69 6e 74 20 6e 65 77 6d 61 73 6b 3b 20 20 20 20  int newmask;    
2d76a 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f         /* Mask o
2d76b 66 20 4e 45 57 2e 2a 20 63 6f 6c 75 6d 6e 73 20  f NEW.* columns 
2d76c 61 63 63 65 73 73 65 64 20 62 79 20 42 45 46 4f  accessed by BEFO
2d76d 52 45 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 0a  RE triggers */..
2d76e 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 41 6c    /* Register Al
2d76f 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  locations */.  i
2d770 6e 74 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d  nt regRowCount =
2d771 20 30 3b 20 20 20 2f 2a 20 41 20 63 6f 75 6e 74   0;   /* A count
2d772 20 6f 66 20 72 6f 77 73 20 63 68 61 6e 67 65 64   of rows changed
2d773 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f 6c 64   */.  int regOld
2d774 52 6f 77 69 64 3b 20 20 20 20 20 20 20 2f 2a 20  Rowid;       /* 
2d775 54 68 65 20 6f 6c 64 20 72 6f 77 69 64 20 2a 2f  The old rowid */
2d776 0a 20 20 69 6e 74 20 72 65 67 4e 65 77 52 6f 77  .  int regNewRow
2d777 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  id;       /* The
2d778 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20   new rowid */.  
2d779 69 6e 74 20 72 65 67 4e 65 77 3b 0a 20 20 69 6e  int regNew;.  in
2d77a 74 20 72 65 67 4f 6c 64 20 3d 20 30 3b 0a 20 20  t regOld = 0;.  
2d77b 69 6e 74 20 72 65 67 52 6f 77 53 65 74 20 3d 20  int regRowSet = 
2d77c 30 3b 20 20 20 20 20 2f 2a 20 52 6f 77 73 65 74  0;     /* Rowset
2d77d 20 6f 66 20 72 6f 77 73 20 74 6f 20 62 65 20 75   of rows to be u
2d77e 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  pdated */.  int 
2d77f 72 65 67 52 65 63 3b 20 20 20 20 20 20 20 20 20  regRec;         
2d780 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75     /* Register u
2d781 73 65 64 20 66 6f 72 20 6e 65 77 20 74 61 62 6c  sed for new tabl
2d782 65 20 72 65 63 6f 72 64 20 74 6f 20 69 6e 73 65  e record to inse
2d783 72 74 20 2a 2f 0a 0a 20 20 6d 65 6d 73 65 74 28  rt */..  memset(
2d784 26 73 43 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69  &sContext, 0, si
2d785 7a 65 6f 66 28 73 43 6f 6e 74 65 78 74 29 29 3b  zeof(sContext));
2d786 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  .  db = pParse->
2d787 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  db;.  if( pParse
2d788 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61  ->nErr || db->ma
2d789 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2d78a 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c    goto update_cl
2d78b 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73  eanup;.  }.  ass
2d78c 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e  ert( pTabList->n
2d78d 53 72 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20  Src==1 );..  /* 
2d78e 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65  Locate the table
2d78f 20 77 68 69 63 68 20 77 65 20 77 61 6e 74 20 74   which we want t
2d790 6f 20 75 70 64 61 74 65 2e 20 0a 20 20 2a 2f 0a  o update. .  */.
2d791 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
2d792 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50  SrcListLookup(pP
2d793 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b  arse, pTabList);
2d794 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29  .  if( pTab==0 )
2d795 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
2d796 61 6e 75 70 3b 0a 20 20 69 44 62 20 3d 20 73 71  anup;.  iDb = sq
2d797 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
2d798 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ex(pParse->db, p
2d799 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a  Tab->pSchema);..
2d79a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
2d79b 69 66 20 77 65 20 68 61 76 65 20 61 6e 79 20 74  if we have any t
2d79c 72 69 67 67 65 72 73 20 61 6e 64 20 69 66 20 74  riggers and if t
2d79d 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 0a 20  he table being. 
2d79e 20 2a 2a 20 75 70 64 61 74 65 64 20 69 73 20 61   ** updated is a
2d79f 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 23 69 66 6e   view..  */.#ifn
2d7a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2d7a1 54 52 49 47 47 45 52 0a 20 20 70 54 72 69 67 67  TRIGGER.  pTrigg
2d7a2 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  er = sqlite3Trig
2d7a3 67 65 72 73 45 78 69 73 74 28 70 50 61 72 73 65  gersExist(pParse
2d7a4 2c 20 70 54 61 62 2c 20 54 4b 5f 55 50 44 41 54  , pTab, TK_UPDAT
2d7a5 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 26 74 6d  E, pChanges, &tm
2d7a6 61 73 6b 29 3b 0a 20 20 69 73 56 69 65 77 20 3d  ask);.  isView =
2d7a7 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 21 3d   pTab->pSelect!=
2d7a8 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 72  0;.  assert( pTr
2d7a9 69 67 67 65 72 20 7c 7c 20 74 6d 61 73 6b 3d 3d  igger || tmask==
2d7aa 30 20 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  0 );.#else.# def
2d7ab 69 6e 65 20 70 54 72 69 67 67 65 72 20 30 0a 23  ine pTrigger 0.#
2d7ac 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30   define isView 0
2d7ad 0a 23 20 64 65 66 69 6e 65 20 74 6d 61 73 6b 20  .# define tmask 
2d7ae 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  0.#endif.#ifdef 
2d7af 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2d7b0 0a 23 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a  .# undef isView.
2d7b1 23 20 64 65 66 69 6e 65 20 69 73 56 69 65 77 20  # define isView 
2d7b2 30 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  0.#endif..  if( 
2d7b3 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f  sqlite3ViewGetCo
2d7b4 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65  lumnNames(pParse
2d7b5 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67  , pTab) ){.    g
2d7b6 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2d7b7 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  up;.  }.  if( sq
2d7b8 6c 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28  lite3IsReadOnly(
2d7b9 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 74 6d  pParse, pTab, tm
2d7ba 61 73 6b 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ask) ){.    goto
2d7bb 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2d7bc 0a 20 20 7d 0a 20 20 61 58 52 65 66 20 3d 20 73  .  }.  aXRef = s
2d7bd 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
2d7be 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74  w(db, sizeof(int
2d7bf 29 20 2a 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 29  ) * pTab->nCol )
2d7c0 3b 0a 20 20 69 66 28 20 61 58 52 65 66 3d 3d 30  ;.  if( aXRef==0
2d7c1 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63   ) goto update_c
2d7c2 6c 65 61 6e 75 70 3b 0a 20 20 66 6f 72 28 69 3d  leanup;.  for(i=
2d7c3 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; i<pTab->nCol;
2d7c4 20 69 2b 2b 29 20 61 58 52 65 66 5b 69 5d 20 3d   i++) aXRef[i] =
2d7c5 20 2d 31 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63   -1;..  /* Alloc
2d7c6 61 74 65 20 61 20 63 75 72 73 6f 72 73 20 66 6f  ate a cursors fo
2d7c7 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  r the main datab
2d7c8 61 73 65 20 74 61 62 6c 65 20 61 6e 64 20 66 6f  ase table and fo
2d7c9 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 2e 0a 20  r all indices.. 
2d7ca 20 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 63 75   ** The index cu
2d7cb 72 73 6f 72 73 20 6d 69 67 68 74 20 6e 6f 74 20  rsors might not 
2d7cc 62 65 20 75 73 65 64 2c 20 62 75 74 20 69 66 20  be used, but if 
2d7cd 74 68 65 79 20 61 72 65 20 75 73 65 64 20 74 68  they are used th
2d7ce 65 79 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20  ey.  ** need to 
2d7cf 6f 63 63 75 72 20 72 69 67 68 74 20 61 66 74 65  occur right afte
2d7d0 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  r the database c
2d7d1 75 72 73 6f 72 2e 20 20 53 6f 20 67 6f 20 61 68  ursor.  So go ah
2d7d2 65 61 64 20 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c  ead and.  ** all
2d7d3 6f 63 61 74 65 20 65 6e 6f 75 67 68 20 73 70 61  ocate enough spa
2d7d4 63 65 2c 20 6a 75 73 74 20 69 6e 20 63 61 73 65  ce, just in case
2d7d5 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62 4c 69 73  ..  */.  pTabLis
2d7d6 74 2d 3e 61 5b 30 5d 2e 69 43 75 72 73 6f 72 20  t->a[0].iCursor 
2d7d7 3d 20 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d  = iCur = pParse-
2d7d8 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70  >nTab++;.  for(p
2d7d9 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
2d7da 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
2d7db 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70  x->pNext){.    p
2d7dc 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
2d7dd 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c   }..  /* Initial
2d7de 69 7a 65 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e  ize the name-con
2d7df 74 65 78 74 20 2a 2f 0a 20 20 6d 65 6d 73 65 74  text */.  memset
2d7e0 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66  (&sNC, 0, sizeof
2d7e1 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43 2e 70 50  (sNC));.  sNC.pP
2d7e2 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
2d7e3 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20   sNC.pSrcList = 
2d7e4 70 54 61 62 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20  pTabList;..  /* 
2d7e5 52 65 73 6f 6c 76 65 20 74 68 65 20 63 6f 6c 75  Resolve the colu
2d7e6 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20  mn names in all 
2d7e7 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  the expressions 
2d7e8 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6f 66 20 74  of the.  ** of t
2d7e9 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  he UPDATE statem
2d7ea 65 6e 74 2e 20 20 41 6c 73 6f 20 66 69 6e 64 20  ent.  Also find 
2d7eb 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78  the column index
2d7ec 0a 20 20 2a 2a 20 66 6f 72 20 65 61 63 68 20 63  .  ** for each c
2d7ed 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75 70 64 61  olumn to be upda
2d7ee 74 65 64 20 69 6e 20 74 68 65 20 70 43 68 61 6e  ted in the pChan
2d7ef 67 65 73 20 61 72 72 61 79 2e 20 20 46 6f 72 20  ges array.  For 
2d7f0 65 61 63 68 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e  each.  ** column
2d7f1 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2c 20   to be updated, 
2d7f2 6d 61 6b 65 20 73 75 72 65 20 77 65 20 68 61 76  make sure we hav
2d7f3 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e authorization 
2d7f4 74 6f 20 63 68 61 6e 67 65 0a 20 20 2a 2a 20 74  to change.  ** t
2d7f5 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2f  hat column..  */
2d7f6 0a 20 20 63 68 6e 67 52 6f 77 69 64 20 3d 20 30  .  chngRowid = 0
2d7f7 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2d7f8 43 68 61 6e 67 65 73 2d 3e 6e 45 78 70 72 3b 20  Changes->nExpr; 
2d7f9 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71  i++){.    if( sq
2d7fa 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
2d7fb 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 43 68 61  Names(&sNC, pCha
2d7fc 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  nges->a[i].pExpr
2d7fd 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
2d7fe 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
2d7ff 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 6a 3d      }.    for(j=
2d800 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; j<pTab->nCol;
2d801 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   j++){.      if(
2d802 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2d803 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e  pTab->aCol[j].zN
2d804 61 6d 65 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61  ame, pChanges->a
2d805 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  [i].zName)==0 ){
2d806 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d  .        if( j==
2d807 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  pTab->iPKey ){. 
2d808 20 20 20 20 20 20 20 20 20 63 68 6e 67 52 6f 77           chngRow
2d809 69 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  id = 1;.        
2d80a 20 20 70 52 6f 77 69 64 45 78 70 72 20 3d 20 70    pRowidExpr = p
2d80b 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45  Changes->a[i].pE
2d80c 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  xpr;.        }. 
2d80d 20 20 20 20 20 20 20 61 58 52 65 66 5b 6a 5d 20         aXRef[j] 
2d80e 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = i;.        bre
2d80f 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2d810 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61  }.    if( j>=pTa
2d811 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
2d812 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f   if( sqlite3IsRo
2d813 77 69 64 28 70 43 68 61 6e 67 65 73 2d 3e 61 5b  wid(pChanges->a[
2d814 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20  i].zName) ){.   
2d815 20 20 20 20 20 63 68 6e 67 52 6f 77 69 64 20 3d       chngRowid =
2d816 20 31 3b 0a 20 20 20 20 20 20 20 20 70 52 6f 77   1;.        pRow
2d817 69 64 45 78 70 72 20 3d 20 70 43 68 61 6e 67 65  idExpr = pChange
2d818 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  s->a[i].pExpr;. 
2d819 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2d81a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2d81b 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20  Msg(pParse, "no 
2d81c 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 25 73 22  such column: %s"
2d81d 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d  , pChanges->a[i]
2d81e 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  .zName);.       
2d81f 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
2d820 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20  anup;.      }.  
2d821 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
2d822 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
2d823 41 54 49 4f 4e 0a 20 20 20 20 7b 0a 20 20 20 20  ATION.    {.    
2d824 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20 20    int rc;.      
2d825 72 63 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68  rc = sqlite3Auth
2d826 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
2d827 4c 49 54 45 5f 55 50 44 41 54 45 2c 20 70 54 61  LITE_UPDATE, pTa
2d828 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  b->zName,.      
2d829 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d82a 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b       pTab->aCol[
2d82b 6a 5d 2e 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61 44  j].zName, db->aD
2d82c 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20  b[iDb].zName);. 
2d82d 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
2d82e 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20  ITE_DENY ){.    
2d82f 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
2d830 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d  cleanup;.      }
2d831 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c  else if( rc==SQL
2d832 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20  ITE_IGNORE ){.  
2d833 20 20 20 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d        aXRef[j] =
2d834 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   -1;.      }.   
2d835 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20   }.#endif.  }.. 
2d836 20 68 61 73 46 4b 20 3d 20 73 71 6c 69 74 65 33   hasFK = sqlite3
2d837 46 6b 52 65 71 75 69 72 65 64 28 70 50 61 72 73  FkRequired(pPars
2d838 65 2c 20 70 54 61 62 2c 20 61 58 52 65 66 2c 20  e, pTab, aXRef, 
2d839 63 68 6e 67 52 6f 77 69 64 29 3b 0a 0a 20 20 2f  chngRowid);..  /
2d83a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
2d83b 79 20 66 6f 72 20 74 68 65 20 61 72 72 61 79 20  y for the array 
2d83c 61 52 65 67 49 64 78 5b 5d 2e 20 20 54 68 65 72  aRegIdx[].  Ther
2d83d 65 20 69 73 20 6f 6e 65 20 65 6e 74 72 79 20 69  e is one entry i
2d83e 6e 20 74 68 65 0a 20 20 2a 2a 20 61 72 72 61 79  n the.  ** array
2d83f 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20   for each index 
2d840 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2d841 74 61 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61  table being upda
2d842 74 65 64 2e 20 20 46 69 6c 6c 20 69 6e 0a 20 20  ted.  Fill in.  
2d843 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 77 69 74  ** the value wit
2d844 68 20 61 20 72 65 67 69 73 74 65 72 20 6e 75 6d  h a register num
2d845 62 65 72 20 66 6f 72 20 69 6e 64 69 63 65 73 20  ber for indices 
2d846 74 68 61 74 20 61 72 65 20 74 6f 20 62 65 20 75  that are to be u
2d847 73 65 64 0a 20 20 2a 2a 20 61 6e 64 20 77 69 74  sed.  ** and wit
2d848 68 20 7a 65 72 6f 20 66 6f 72 20 75 6e 75 73 65  h zero for unuse
2d849 64 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a  d indices..  */.
2d84a 20 20 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49    for(nIdx=0, pI
2d84b 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
2d84c 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
2d84d 2d 3e 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29  ->pNext, nIdx++)
2d84e 7b 7d 0a 20 20 69 66 28 20 6e 49 64 78 3e 30 20  {}.  if( nIdx>0 
2d84f 29 7b 0a 20 20 20 20 61 52 65 67 49 64 78 20 3d  ){.    aRegIdx =
2d850 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2d851 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 49  Raw(db, sizeof(I
2d852 6e 64 65 78 2a 29 20 2a 20 6e 49 64 78 20 29 3b  ndex*) * nIdx );
2d853 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64 78  .    if( aRegIdx
2d854 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74  ==0 ) goto updat
2d855 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  e_cleanup;.  }. 
2d856 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70   for(j=0, pIdx=p
2d857 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
2d858 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
2d859 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 69  ext, j++){.    i
2d85a 6e 74 20 72 65 67 3b 0a 20 20 20 20 69 66 28 20  nt reg;.    if( 
2d85b 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20  chngRowid ){.   
2d85c 20 20 20 72 65 67 20 3d 20 2b 2b 70 50 61 72 73     reg = ++pPars
2d85d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c  e->nMem;.    }el
2d85e 73 65 7b 0a 20 20 20 20 20 20 72 65 67 20 3d 20  se{.      reg = 
2d85f 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  0;.      for(i=0
2d860 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ; i<pIdx->nColum
2d861 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  n; i++){.       
2d862 20 69 66 28 20 61 58 52 65 66 5b 70 49 64 78 2d   if( aXRef[pIdx-
2d863 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 3e 3d 30  >aiColumn[i]]>=0
2d864 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
2d865 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  g = ++pParse->nM
2d866 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  em;.          br
2d867 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
2d868 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2d869 20 61 52 65 67 49 64 78 5b 6a 5d 20 3d 20 72 65   aRegIdx[j] = re
2d86a 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67  g;.  }..  /* Beg
2d86b 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  in generating co
2d86c 64 65 2e 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c  de. */.  v = sql
2d86d 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
2d86e 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20  se);.  if( v==0 
2d86f 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c  ) goto update_cl
2d870 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 70 50 61  eanup;.  if( pPa
2d871 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
2d872 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e   sqlite3VdbeCoun
2d873 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73  tChanges(v);.  s
2d874 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
2d875 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
2d876 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e  , 1, iDb);..#ifn
2d877 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2d878 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 2f  VIRTUALTABLE.  /
2d879 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73  * Virtual tables
2d87a 20 6d 75 73 74 20 62 65 20 68 61 6e 64 6c 65 64   must be handled
2d87b 20 73 65 70 61 72 61 74 65 6c 79 20 2a 2f 0a 20   separately */. 
2d87c 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
2d87d 54 61 62 29 20 29 7b 0a 20 20 20 20 75 70 64 61  Tab) ){.    upda
2d87e 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28 70  teVirtualTable(p
2d87f 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
2d880 20 70 54 61 62 2c 20 70 43 68 61 6e 67 65 73 2c   pTab, pChanges,
2d881 20 70 52 6f 77 69 64 45 78 70 72 2c 20 61 58 52   pRowidExpr, aXR
2d882 65 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ef,.            
2d883 20 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72             pWher
2d884 65 29 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d  e);.    pWhere =
2d885 20 30 3b 0a 20 20 20 20 70 54 61 62 4c 69 73 74   0;.    pTabList
2d886 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 75   = 0;.    goto u
2d887 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
2d888 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
2d889 41 6c 6c 6f 63 61 74 65 20 72 65 71 75 69 72 65  Allocate require
2d88a 64 20 72 65 67 69 73 74 65 72 73 2e 20 2a 2f 0a  d registers. */.
2d88b 20 20 72 65 67 4f 6c 64 52 6f 77 69 64 20 3d 20    regOldRowid = 
2d88c 72 65 67 4e 65 77 52 6f 77 69 64 20 3d 20 2b 2b  regNewRowid = ++
2d88d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2d88e 69 66 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20  if( pTrigger || 
2d88f 68 61 73 46 4b 20 29 7b 0a 20 20 20 20 72 65 67  hasFK ){.    reg
2d890 4f 6c 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d  Old = pParse->nM
2d891 65 6d 20 2b 20 31 3b 0a 20 20 20 20 70 50 61 72  em + 1;.    pPar
2d892 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62  se->nMem += pTab
2d893 2d 3e 6e 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66  ->nCol;.  }.  if
2d894 28 20 63 68 6e 67 52 6f 77 69 64 20 7c 7c 20 70  ( chngRowid || p
2d895 54 72 69 67 67 65 72 20 7c 7c 20 68 61 73 46 4b  Trigger || hasFK
2d896 20 29 7b 0a 20 20 20 20 72 65 67 4e 65 77 52 6f   ){.    regNewRo
2d897 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  wid = ++pParse->
2d898 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 67 4e  nMem;.  }.  regN
2d899 65 77 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  ew = pParse->nMe
2d89a 6d 20 2b 20 31 3b 0a 20 20 70 50 61 72 73 65 2d  m + 1;.  pParse-
2d89b 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e  >nMem += pTab->n
2d89c 43 6f 6c 3b 0a 20 20 72 65 67 52 65 63 20 3d 20  Col;.  regRec = 
2d89d 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2d89e 0a 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20  .  /* Start the 
2d89f 76 69 65 77 20 63 6f 6e 74 65 78 74 2e 20 2a 2f  view context. */
2d8a0 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b  .  if( isView ){
2d8a1 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68  .    sqlite3Auth
2d8a2 43 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61 72  ContextPush(pPar
2d8a3 73 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20 70  se, &sContext, p
2d8a4 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7d  Tab->zName);.  }
2d8a5 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65  ..  /* If we are
2d8a6 20 74 72 79 69 6e 67 20 74 6f 20 75 70 64 61 74   trying to updat
2d8a7 65 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69 7a  e a view, realiz
2d8a8 65 20 74 68 61 74 20 76 69 65 77 20 69 6e 74 6f  e that view into
2d8a9 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72 61  .  ** a ephemera
2d8aa 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69  l table..  */.#i
2d8ab 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2d8ac 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20  E_OMIT_VIEW) && 
2d8ad 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2d8ae 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20 20  OMIT_TRIGGER).  
2d8af 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20  if( isView ){.  
2d8b0 20 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61    sqlite3Materia
2d8b1 6c 69 7a 65 56 69 65 77 28 70 50 61 72 73 65 2c  lizeView(pParse,
2d8b2 20 70 54 61 62 2c 20 70 57 68 65 72 65 2c 20 69   pTab, pWhere, i
2d8b3 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  Cur);.  }.#endif
2d8b4 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74  ..  /* Resolve t
2d8b5 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  he column names 
2d8b6 69 6e 20 61 6c 6c 20 74 68 65 20 65 78 70 72 65  in all the expre
2d8b7 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 0a 20 20  ssions in the.  
2d8b8 2a 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  ** WHERE clause.
2d8b9 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
2d8ba 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
2d8bb 6d 65 73 28 26 73 4e 43 2c 20 70 57 68 65 72 65  mes(&sNC, pWhere
2d8bc 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70  ) ){.    goto up
2d8bd 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
2d8be 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 74 68  }..  /* Begin th
2d8bf 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a  e database scan.
2d8c0 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
2d8c1 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
2d8c2 75 6c 6c 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f  ull, 0, regOldRo
2d8c3 77 69 64 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d  wid);.  pWInfo =
2d8c4 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
2d8c5 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c  in(pParse, pTabL
2d8c6 69 73 74 2c 20 70 57 68 65 72 65 2c 30 2c 20 57  ist, pWhere,0, W
2d8c7 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53  HERE_ONEPASS_DES
2d8c8 49 52 45 44 29 3b 0a 20 20 69 66 28 20 70 57 49  IRED);.  if( pWI
2d8c9 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70  nfo==0 ) goto up
2d8ca 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
2d8cb 6f 6b 4f 6e 65 50 61 73 73 20 3d 20 70 57 49 6e  okOnePass = pWIn
2d8cc 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 3b 0a 0a  fo->okOnePass;..
2d8cd 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20 74 68    /* Remember th
2d8ce 65 20 72 6f 77 69 64 20 6f 66 20 65 76 65 72 79  e rowid of every
2d8cf 20 69 74 65 6d 20 74 6f 20 62 65 20 75 70 64 61   item to be upda
2d8d0 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ted..  */.  sqli
2d8d1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2d8d2 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c   OP_Rowid, iCur,
2d8d3 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20   regOldRowid);. 
2d8d4 20 69 66 28 20 21 6f 6b 4f 6e 65 50 61 73 73 20   if( !okOnePass 
2d8d5 29 7b 0a 20 20 20 20 72 65 67 52 6f 77 53 65 74  ){.    regRowSet
2d8d6 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2d8d7 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  m;.    sqlite3Vd
2d8d8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2d8d9 6f 77 53 65 74 41 64 64 2c 20 72 65 67 52 6f 77  owSetAdd, regRow
2d8da 53 65 74 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  Set, regOldRowid
2d8db 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 64  );.  }..  /* End
2d8dc 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
2d8dd 61 6e 20 6c 6f 6f 70 2e 0a 20 20 2a 2f 0a 20 20  an loop..  */.  
2d8de 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
2d8df 70 57 49 6e 66 6f 29 3b 0a 0a 20 20 2f 2a 20 49  pWInfo);..  /* I
2d8e0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f  nitialize the co
2d8e1 75 6e 74 20 6f 66 20 75 70 64 61 74 65 64 20 72  unt of updated r
2d8e2 6f 77 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  ows.  */.  if( (
2d8e3 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
2d8e4 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 20 26 26  TE_CountRows) &&
2d8e5 20 21 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67   !pParse->pTrigg
2d8e6 65 72 54 61 62 20 29 7b 0a 20 20 20 20 72 65 67  erTab ){.    reg
2d8e7 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70 50 61  RowCount = ++pPa
2d8e8 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73  rse->nMem;.    s
2d8e9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2d8ea 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2d8eb 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74 29 3b  0, regRowCount);
2d8ec 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 69 73 56  .  }..  if( !isV
2d8ed 69 65 77 20 29 7b 0a 20 20 20 20 2f 2a 20 0a 20  iew ){.    /* . 
2d8ee 20 20 20 2a 2a 20 4f 70 65 6e 20 65 76 65 72 79     ** Open every
2d8ef 20 69 6e 64 65 78 20 74 68 61 74 20 6e 65 65 64   index that need
2d8f0 73 20 75 70 64 61 74 69 6e 67 2e 20 20 4e 6f 74  s updating.  Not
2d8f1 65 20 74 68 61 74 20 69 66 20 61 6e 79 0a 20 20  e that if any.  
2d8f2 20 20 2a 2a 20 69 6e 64 65 78 20 63 6f 75 6c 64    ** index could
2d8f3 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 69 6e 76   potentially inv
2d8f4 6f 6b 65 20 61 20 52 45 50 4c 41 43 45 20 63 6f  oke a REPLACE co
2d8f5 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f  nflict resolutio
2d8f6 6e 20 0a 20 20 20 20 2a 2a 20 61 63 74 69 6f 6e  n .    ** action
2d8f7 2c 20 74 68 65 6e 20 77 65 20 6e 65 65 64 20 74  , then we need t
2d8f8 6f 20 6f 70 65 6e 20 61 6c 6c 20 69 6e 64 69 63  o open all indic
2d8f9 65 73 20 62 65 63 61 75 73 65 20 77 65 20 6d 69  es because we mi
2d8fa 67 68 74 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20  ght need.    ** 
2d8fb 74 6f 20 62 65 20 64 65 6c 65 74 69 6e 67 20 73  to be deleting s
2d8fc 6f 6d 65 20 72 65 63 6f 72 64 73 2e 0a 20 20 20  ome records..   
2d8fd 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 6f 6b 4f   */.    if( !okO
2d8fe 6e 65 50 61 73 73 20 29 20 73 71 6c 69 74 65 33  nePass ) sqlite3
2d8ff 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
2d900 2c 20 69 43 75 72 2c 20 69 44 62 2c 20 70 54 61  , iCur, iDb, pTa
2d901 62 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29  b, OP_OpenWrite)
2d902 3b 20 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72  ; .    if( onErr
2d903 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29  or==OE_Replace )
2d904 7b 0a 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20  {.      openAll 
2d905 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 1;.    }else{.
2d906 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20        openAll = 
2d907 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64  0;.      for(pId
2d908 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2d909 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
2d90a 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20  >pNext){.       
2d90b 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72   if( pIdx->onErr
2d90c 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29  or==OE_Replace )
2d90d 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 70 65 6e  {.          open
2d90e 41 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  All = 1;.       
2d90f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2d910 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2d911 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70  }.    for(i=0, p
2d912 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
2d913 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
2d914 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a  x->pNext, i++){.
2d915 20 20 20 20 20 20 69 66 28 20 6f 70 65 6e 41 6c        if( openAl
2d916 6c 20 7c 7c 20 61 52 65 67 49 64 78 5b 69 5d 3e  l || aRegIdx[i]>
2d917 30 20 29 7b 0a 20 20 20 20 20 20 20 20 4b 65 79  0 ){.        Key
2d918 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c  Info *pKey = sql
2d919 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
2d91a 28 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a  (pParse, pIdx);.
2d91b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2d91c 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2d91d 4f 70 65 6e 57 72 69 74 65 2c 20 69 43 75 72 2b  OpenWrite, iCur+
2d91e 69 2b 31 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c  i+1, pIdx->tnum,
2d91f 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20   iDb,.          
2d920 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68               (ch
2d921 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59  ar*)pKey, P4_KEY
2d922 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20  INFO_HANDOFF);. 
2d923 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
2d924 50 61 72 73 65 2d 3e 6e 54 61 62 3e 69 43 75 72  Parse->nTab>iCur
2d925 2b 69 2b 31 20 29 3b 0a 20 20 20 20 20 20 7d 0a  +i+1 );.      }.
2d926 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
2d927 54 6f 70 20 6f 66 20 74 68 65 20 75 70 64 61 74  Top of the updat
2d928 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66 28 20  e loop */.  if( 
2d929 6f 6b 4f 6e 65 50 61 73 73 20 29 7b 0a 20 20 20  okOnePass ){.   
2d92a 20 69 6e 74 20 61 31 20 3d 20 73 71 6c 69 74 65   int a1 = sqlite
2d92b 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2d92c 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 4f 6c  P_NotNull, regOl
2d92d 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 61 64 64  dRowid);.    add
2d92e 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
2d92f 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp0(v, OP_Goto
2d930 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2d931 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 31  beJumpHere(v, a1
2d932 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2d933 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
2d934 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52  beAddOp3(v, OP_R
2d935 6f 77 53 65 74 52 65 61 64 2c 20 72 65 67 52 6f  owSetRead, regRo
2d936 77 53 65 74 2c 20 30 2c 20 72 65 67 4f 6c 64 52  wSet, 0, regOldR
2d937 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  owid);.  }..  /*
2d938 20 4d 61 6b 65 20 63 75 72 73 6f 72 20 69 43 75   Make cursor iCu
2d939 72 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72  r point to the r
2d93a 65 63 6f 72 64 20 74 68 61 74 20 69 73 20 62 65  ecord that is be
2d93b 69 6e 67 20 75 70 64 61 74 65 64 2e 20 49 66 0a  ing updated. If.
2d93c 20 20 2a 2a 20 74 68 69 73 20 72 65 63 6f 72 64    ** this record
2d93d 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
2d93e 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e 20  for some reason 
2d93f 28 64 65 6c 65 74 65 64 20 62 79 20 61 20 74 72  (deleted by a tr
2d940 69 67 67 65 72 2c 0a 20 20 2a 2a 20 66 6f 72 20  igger,.  ** for 
2d941 65 78 61 6d 70 6c 65 2c 20 74 68 65 6e 20 6a 75  example, then ju
2d942 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69  mp to the next i
2d943 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  teration of the 
2d944 52 6f 77 53 65 74 20 6c 6f 6f 70 2e 20 20 2a 2f  RowSet loop.  */
2d945 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
2d946 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
2d947 69 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72  ists, iCur, addr
2d948 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a  , regOldRowid);.
2d949 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 63  .  /* If the rec
2d94a 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20  ord number will 
2d94b 63 68 61 6e 67 65 2c 20 73 65 74 20 72 65 67 69  change, set regi
2d94c 73 74 65 72 20 72 65 67 4e 65 77 52 6f 77 69 64  ster regNewRowid
2d94d 20 74 6f 0a 20 20 2a 2a 20 63 6f 6e 74 61 69 6e   to.  ** contain
2d94e 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 2e 20   the new value. 
2d94f 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75  If the record nu
2d950 6d 62 65 72 20 69 73 20 6e 6f 74 20 62 65 69 6e  mber is not bein
2d951 67 20 6d 6f 64 69 66 69 65 64 2c 0a 20 20 2a 2a  g modified,.  **
2d952 20 74 68 65 6e 20 72 65 67 4e 65 77 52 6f 77 69   then regNewRowi
2d953 64 20 69 73 20 74 68 65 20 73 61 6d 65 20 72 65  d is the same re
2d954 67 69 73 74 65 72 20 61 73 20 72 65 67 4f 6c 64  gister as regOld
2d955 52 6f 77 69 64 2c 20 77 68 69 63 68 20 69 73 0a  Rowid, which is.
2d956 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 70 6f 70    ** already pop
2d957 75 6c 61 74 65 64 2e 20 20 2a 2f 0a 20 20 61 73  ulated.  */.  as
2d958 73 65 72 74 28 20 63 68 6e 67 52 6f 77 69 64 20  sert( chngRowid 
2d959 7c 7c 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68  || pTrigger || h
2d95a 61 73 46 4b 20 7c 7c 20 72 65 67 4f 6c 64 52 6f  asFK || regOldRo
2d95b 77 69 64 3d 3d 72 65 67 4e 65 77 52 6f 77 69 64  wid==regNewRowid
2d95c 20 29 3b 0a 20 20 69 66 28 20 63 68 6e 67 52 6f   );.  if( chngRo
2d95d 77 69 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  wid ){.    sqlit
2d95e 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
2d95f 65 2c 20 70 52 6f 77 69 64 45 78 70 72 2c 20 72  e, pRowidExpr, r
2d960 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20 20  egNewRowid);.   
2d961 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d962 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
2d963 6e 74 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29  nt, regNewRowid)
2d964 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
2d965 68 65 72 65 20 61 72 65 20 74 72 69 67 67 65 72  here are trigger
2d966 73 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c  s on this table,
2d967 20 70 6f 70 75 6c 61 74 65 20 61 6e 20 61 72 72   populate an arr
2d968 61 79 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ay of registers 
2d969 0a 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 72  .  ** with the r
2d96a 65 71 75 69 72 65 64 20 6f 6c 64 2e 2a 20 63 6f  equired old.* co
2d96b 6c 75 6d 6e 20 64 61 74 61 2e 20 20 2a 2f 0a 20  lumn data.  */. 
2d96c 20 69 66 28 20 68 61 73 46 4b 20 7c 7c 20 70 54   if( hasFK || pT
2d96d 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20 75 33  rigger ){.    u3
2d96e 32 20 6f 6c 64 6d 61 73 6b 20 3d 20 28 68 61 73  2 oldmask = (has
2d96f 46 4b 20 3f 20 73 71 6c 69 74 65 33 46 6b 4f 6c  FK ? sqlite3FkOl
2d970 64 6d 61 73 6b 28 70 50 61 72 73 65 2c 20 70 54  dmask(pParse, pT
2d971 61 62 29 20 3a 20 30 29 3b 0a 20 20 20 20 6f 6c  ab) : 0);.    ol
2d972 64 6d 61 73 6b 20 7c 3d 20 73 71 6c 69 74 65 33  dmask |= sqlite3
2d973 54 72 69 67 67 65 72 43 6f 6c 6d 61 73 6b 28 70  TriggerColmask(p
2d974 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
2d975 70 54 72 69 67 67 65 72 2c 20 70 43 68 61 6e 67  pTrigger, pChang
2d976 65 73 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 42  es, 0, TRIGGER_B
2d977 45 46 4f 52 45 7c 54 52 49 47 47 45 52 5f 41 46  EFORE|TRIGGER_AF
2d978 54 45 52 2c 20 70 54 61 62 2c 20 6f 6e 45 72 72  TER, pTab, onErr
2d979 6f 72 0a 20 20 20 20 29 3b 0a 20 20 20 20 66 6f  or.    );.    fo
2d97a 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e  r(i=0; i<pTab->n
2d97b 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
2d97c 20 69 66 28 20 61 58 52 65 66 5b 69 5d 3c 30 20   if( aXRef[i]<0 
2d97d 7c 7c 20 6f 6c 64 6d 61 73 6b 3d 3d 30 78 66 66  || oldmask==0xff
2d97e 66 66 66 66 66 66 20 7c 7c 20 28 6f 6c 64 6d 61  ffffff || (oldma
2d97f 73 6b 20 26 20 28 31 3c 3c 69 29 29 20 29 7b 0a  sk & (1<<i)) ){.
2d980 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2d981 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2d982 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 2c  Column, iCur, i,
2d983 20 72 65 67 4f 6c 64 2b 69 29 3b 0a 20 20 20 20   regOld+i);.    
2d984 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d      sqlite3Colum
2d985 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62  nDefault(v, pTab
2d986 2c 20 69 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0a  , i, regOld+i);.
2d987 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2d988 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2d989 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2d98a 6c 2c 20 30 2c 20 72 65 67 4f 6c 64 2b 69 29 3b  l, 0, regOld+i);
2d98b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2d98c 20 20 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64     if( chngRowid
2d98d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
2d98e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2d98f 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 65 67 4f 6c  , OP_Copy, regOl
2d990 64 52 6f 77 69 64 2c 20 72 65 67 4e 65 77 52 6f  dRowid, regNewRo
2d991 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  wid);.    }.  }.
2d992 0a 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74  .  /* Populate t
2d993 68 65 20 61 72 72 61 79 20 6f 66 20 72 65 67 69  he array of regi
2d994 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20  sters beginning 
2d995 61 74 20 72 65 67 4e 65 77 20 77 69 74 68 20 74  at regNew with t
2d996 68 65 20 6e 65 77 0a 20 20 2a 2a 20 72 6f 77 20  he new.  ** row 
2d997 64 61 74 61 2e 20 54 68 69 73 20 61 72 72 61 79  data. This array
2d998 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 65 63   is used to chec
2d999 6b 20 63 6f 6e 73 74 61 69 6e 74 73 2c 20 63 72  k constaints, cr
2d99a 65 61 74 65 20 74 68 65 20 6e 65 77 0a 20 20 2a  eate the new.  *
2d99b 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65  * table and inde
2d99c 78 20 72 65 63 6f 72 64 73 2c 20 61 6e 64 20 61  x records, and a
2d99d 73 20 74 68 65 20 76 61 6c 75 65 73 20 66 6f 72  s the values for
2d99e 20 61 6e 79 20 6e 65 77 2e 2a 20 72 65 66 65 72   any new.* refer
2d99f 65 6e 63 65 73 0a 20 20 2a 2a 20 6d 61 64 65 20  ences.  ** made 
2d9a0 62 79 20 74 72 69 67 67 65 72 73 2e 0a 20 20 2a  by triggers..  *
2d9a1 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20  *.  ** If there 
2d9a2 61 72 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  are one or more 
2d9a3 42 45 46 4f 52 45 20 74 72 69 67 67 65 72 73 2c  BEFORE triggers,
2d9a4 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 70 6f 70   then do not pop
2d9a5 75 6c 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 72  ulate the.  ** r
2d9a6 65 67 69 73 74 65 72 73 20 61 73 73 6f 63 69 61  egisters associa
2d9a7 74 65 64 20 77 69 74 68 20 63 6f 6c 75 6d 6e 73  ted with columns
2d9a8 20 74 68 61 74 20 61 72 65 20 28 61 29 20 6e 6f   that are (a) no
2d9a9 74 20 6d 6f 64 69 66 69 65 64 20 62 79 0a 20 20  t modified by.  
2d9aa 2a 2a 20 74 68 69 73 20 55 50 44 41 54 45 20 73  ** this UPDATE s
2d9ab 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 28 62 29  tatement and (b)
2d9ac 20 6e 6f 74 20 61 63 63 65 73 73 65 64 20 62 79   not accessed by
2d9ad 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e 63 65   new.* reference
2d9ae 73 2e 20 54 68 65 0a 20 20 2a 2a 20 76 61 6c 75  s. The.  ** valu
2d9af 65 73 20 66 6f 72 20 72 65 67 69 73 74 65 72 73  es for registers
2d9b0 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20 62 79   not modified by
2d9b1 20 74 68 65 20 55 50 44 41 54 45 20 6d 75 73 74   the UPDATE must
2d9b2 20 62 65 20 72 65 6c 6f 61 64 65 64 20 66 72 6f   be reloaded fro
2d9b3 6d 20 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61  m .  ** the data
2d9b4 62 61 73 65 20 61 66 74 65 72 20 74 68 65 20 42  base after the B
2d9b5 45 46 4f 52 45 20 74 72 69 67 67 65 72 73 20 61  EFORE triggers a
2d9b6 72 65 20 66 69 72 65 64 20 61 6e 79 77 61 79 20  re fired anyway 
2d9b7 28 61 73 20 74 68 65 20 74 72 69 67 67 65 72 20  (as the trigger 
2d9b8 0a 20 20 2a 2a 20 6d 61 79 20 68 61 76 65 20 6d  .  ** may have m
2d9b9 6f 64 69 66 69 65 64 20 74 68 65 6d 29 2e 20 53  odified them). S
2d9ba 6f 20 6e 6f 74 20 6c 6f 61 64 69 6e 67 20 74 68  o not loading th
2d9bb 6f 73 65 20 74 68 61 74 20 61 72 65 20 6e 6f 74  ose that are not
2d9bc 20 67 6f 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 62   going to.  ** b
2d9bd 65 20 75 73 65 64 20 65 6c 69 6d 69 6e 61 74 65  e used eliminate
2d9be 73 20 73 6f 6d 65 20 72 65 64 75 6e 64 61 6e 74  s some redundant
2d9bf 20 6f 70 63 6f 64 65 73 2e 0a 20 20 2a 2f 0a 20   opcodes..  */. 
2d9c0 20 6e 65 77 6d 61 73 6b 20 3d 20 73 71 6c 69 74   newmask = sqlit
2d9c1 65 33 54 72 69 67 67 65 72 43 6f 6c 6d 61 73 6b  e3TriggerColmask
2d9c2 28 0a 20 20 20 20 20 20 70 50 61 72 73 65 2c 20  (.      pParse, 
2d9c3 70 54 72 69 67 67 65 72 2c 20 70 43 68 61 6e 67  pTrigger, pChang
2d9c4 65 73 2c 20 31 2c 20 54 52 49 47 47 45 52 5f 42  es, 1, TRIGGER_B
2d9c5 45 46 4f 52 45 2c 20 70 54 61 62 2c 20 6f 6e 45  EFORE, pTab, onE
2d9c6 72 72 6f 72 0a 20 20 29 3b 0a 20 20 66 6f 72 28  rror.  );.  for(
2d9c7 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
2d9c8 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  l; i++){.    if(
2d9c9 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20   i==pTab->iPKey 
2d9ca 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2d9cb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2d9cc 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 4e 65 77  _Null, 0, regNew
2d9cd 2b 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  +i);.    }else{.
2d9ce 20 20 20 20 20 20 6a 20 3d 20 61 58 52 65 66 5b        j = aXRef[
2d9cf 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 6a 3e  i];.      if( j>
2d9d0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =0 ){.        sq
2d9d1 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2d9d2 61 72 73 65 2c 20 70 43 68 61 6e 67 65 73 2d 3e  arse, pChanges->
2d9d3 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 4e  a[j].pExpr, regN
2d9d4 65 77 2b 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c  ew+i);.      }el
2d9d5 73 65 20 69 66 28 20 30 3d 3d 28 74 6d 61 73 6b  se if( 0==(tmask
2d9d6 26 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45 29  &TRIGGER_BEFORE)
2d9d7 20 7c 7c 20 69 3e 33 31 20 7c 7c 20 28 6e 65 77   || i>31 || (new
2d9d8 6d 61 73 6b 26 28 31 3c 3c 69 29 29 20 29 7b 0a  mask&(1<<i)) ){.
2d9d9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
2d9da 62 72 61 6e 63 68 20 6c 6f 61 64 73 20 74 68 65  branch loads the
2d9db 20 76 61 6c 75 65 20 6f 66 20 61 20 63 6f 6c 75   value of a colu
2d9dc 6d 6e 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74  mn that will not
2d9dd 20 62 65 20 63 68 61 6e 67 65 64 20 0a 20 20 20   be changed .   
2d9de 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 61 20 72       ** into a r
2d9df 65 67 69 73 74 65 72 2e 20 54 68 69 73 20 69 73  egister. This is
2d9e0 20 64 6f 6e 65 20 69 66 20 74 68 65 72 65 20 61   done if there a
2d9e1 72 65 20 6e 6f 20 42 45 46 4f 52 45 20 74 72 69  re no BEFORE tri
2d9e2 67 67 65 72 73 2c 20 6f 72 0a 20 20 20 20 20 20  ggers, or.      
2d9e3 20 20 2a 2a 20 69 66 20 74 68 65 72 65 20 61 72    ** if there ar
2d9e4 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 42 45  e one or more BE
2d9e5 46 4f 52 45 20 74 72 69 67 67 65 72 73 20 74 68  FORE triggers th
2d9e6 61 74 20 75 73 65 20 74 68 69 73 20 76 61 6c 75  at use this valu
2d9e7 65 20 76 69 61 0a 20 20 20 20 20 20 20 20 2a 2a  e via.        **
2d9e8 20 61 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e   a new.* referen
2d9e9 63 65 20 69 6e 20 61 20 74 72 69 67 67 65 72 20  ce in a trigger 
2d9ea 70 72 6f 67 72 61 6d 2e 0a 20 20 20 20 20 20 20  program..       
2d9eb 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 65 73 74   */.        test
2d9ec 63 61 73 65 28 20 69 3d 3d 33 31 20 29 3b 0a 20  case( i==31 );. 
2d9ed 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
2d9ee 20 69 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20   i==32 );.      
2d9ef 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d9f0 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2d9f1 2c 20 69 43 75 72 2c 20 69 2c 20 72 65 67 4e 65  , iCur, i, regNe
2d9f2 77 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 73 71  w+i);.        sq
2d9f3 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75  lite3ColumnDefau
2d9f4 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 2c 20 72  lt(v, pTab, i, r
2d9f5 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20 20 20  egNew+i);.      
2d9f6 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
2d9f7 2a 20 46 69 72 65 20 61 6e 79 20 42 45 46 4f 52  * Fire any BEFOR
2d9f8 45 20 55 50 44 41 54 45 20 74 72 69 67 67 65 72  E UPDATE trigger
2d9f9 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  s. This happens 
2d9fa 62 65 66 6f 72 65 20 63 6f 6e 73 74 72 61 69 6e  before constrain
2d9fb 74 73 20 61 72 65 0a 20 20 2a 2a 20 76 65 72 69  ts are.  ** veri
2d9fc 66 69 65 64 2e 20 4f 6e 65 20 63 6f 75 6c 64 20  fied. One could 
2d9fd 61 72 67 75 65 20 74 68 61 74 20 74 68 69 73 20  argue that this 
2d9fe 69 73 20 77 72 6f 6e 67 2e 0a 20 20 2a 2f 0a 20  is wrong..  */. 
2d9ff 20 69 66 28 20 74 6d 61 73 6b 26 54 52 49 47 47   if( tmask&TRIGG
2da00 45 52 5f 42 45 46 4f 52 45 20 29 7b 0a 20 20 20  ER_BEFORE ){.   
2da01 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2da02 70 32 28 76 2c 20 4f 50 5f 41 66 66 69 6e 69 74  p2(v, OP_Affinit
2da03 79 2c 20 72 65 67 4e 65 77 2c 20 70 54 61 62 2d  y, regNew, pTab-
2da04 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73 71 6c 69  >nCol);.    sqli
2da05 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79  te3TableAffinity
2da06 53 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 20 20  Str(v, pTab);.  
2da07 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77    sqlite3CodeRow
2da08 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20  Trigger(pParse, 
2da09 70 54 72 69 67 67 65 72 2c 20 54 4b 5f 55 50 44  pTrigger, TK_UPD
2da0a 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 0a  ATE, pChanges, .
2da0b 20 20 20 20 20 20 20 20 54 52 49 47 47 45 52 5f          TRIGGER_
2da0c 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 20 72 65  BEFORE, pTab, re
2da0d 67 4f 6c 64 52 6f 77 69 64 2c 20 6f 6e 45 72 72  gOldRowid, onErr
2da0e 6f 72 2c 20 61 64 64 72 29 3b 0a 0a 20 20 20 20  or, addr);..    
2da0f 2f 2a 20 54 68 65 20 72 6f 77 2d 74 72 69 67 67  /* The row-trigg
2da10 65 72 20 6d 61 79 20 68 61 76 65 20 64 65 6c 65  er may have dele
2da11 74 65 64 20 74 68 65 20 72 6f 77 20 62 65 69 6e  ted the row bein
2da12 67 20 75 70 64 61 74 65 64 2e 20 49 6e 20 74 68  g updated. In th
2da13 69 73 0a 20 20 20 20 2a 2a 20 63 61 73 65 2c 20  is.    ** case, 
2da14 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78 74  jump to the next
2da15 20 72 6f 77 2e 20 4e 6f 20 75 70 64 61 74 65 73   row. No updates
2da16 20 6f 72 20 41 46 54 45 52 20 74 72 69 67 67 65   or AFTER trigge
2da17 72 73 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 72  rs are .    ** r
2da18 65 71 75 69 72 65 64 2e 20 54 68 69 73 20 62 65  equired. This be
2da19 68 61 76 69 6f 75 72 20 2d 20 77 68 61 74 20 68  haviour - what h
2da1a 61 70 70 65 6e 73 20 77 68 65 6e 20 74 68 65 20  appens when the 
2da1b 72 6f 77 20 62 65 69 6e 67 20 75 70 64 61 74 65  row being update
2da1c 64 0a 20 20 20 20 2a 2a 20 69 73 20 64 65 6c 65  d.    ** is dele
2da1d 74 65 64 20 6f 72 20 72 65 6e 61 6d 65 64 20 62  ted or renamed b
2da1e 79 20 61 20 42 45 46 4f 52 45 20 74 72 69 67 67  y a BEFORE trigg
2da1f 65 72 20 2d 20 69 73 20 6c 65 66 74 20 75 6e 64  er - is left und
2da20 65 66 69 6e 65 64 20 69 6e 20 74 68 65 0a 20 20  efined in the.  
2da21 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69    ** documentati
2da22 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  on..    */.    s
2da23 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2da24 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
2da25 2c 20 69 43 75 72 2c 20 61 64 64 72 2c 20 72 65  , iCur, addr, re
2da26 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a 20 20 20  gOldRowid);..   
2da27 20 2f 2a 20 49 66 20 69 74 20 64 69 64 20 6e 6f   /* If it did no
2da28 74 20 64 65 6c 65 74 65 20 69 74 2c 20 74 68 65  t delete it, the
2da29 20 72 6f 77 2d 74 72 69 67 67 65 72 20 6d 61 79   row-trigger may
2da2a 20 73 74 69 6c 6c 20 68 61 76 65 20 6d 6f 64 69   still have modi
2da2b 66 69 65 64 20 0a 20 20 20 20 2a 2a 20 73 6f 6d  fied .    ** som
2da2c 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73  e of the columns
2da2d 20 6f 66 20 74 68 65 20 72 6f 77 20 62 65 69 6e   of the row bein
2da2e 67 20 75 70 64 61 74 65 64 2e 20 4c 6f 61 64 20  g updated. Load 
2da2f 74 68 65 20 76 61 6c 75 65 73 20 66 6f 72 20 0a  the values for .
2da30 20 20 20 20 2a 2a 20 61 6c 6c 20 63 6f 6c 75 6d      ** all colum
2da31 6e 73 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64 20  ns not modified 
2da32 62 79 20 74 68 65 20 75 70 64 61 74 65 20 73 74  by the update st
2da33 61 74 65 6d 65 6e 74 20 69 6e 74 6f 20 74 68 65  atement into the
2da34 69 72 20 0a 20 20 20 20 2a 2a 20 72 65 67 69 73  ir .    ** regis
2da35 74 65 72 73 20 69 6e 20 63 61 73 65 20 74 68 69  ters in case thi
2da36 73 20 68 61 73 20 68 61 70 70 65 6e 65 64 2e 0a  s has happened..
2da37 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
2da38 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c  =0; i<pTab->nCol
2da39 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
2da3a 28 20 61 58 52 65 66 5b 69 5d 3c 30 20 26 26 20  ( aXRef[i]<0 && 
2da3b 69 21 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29  i!=pTab->iPKey )
2da3c 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2da3d 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2da3e 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20  P_Column, iCur, 
2da3f 69 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20  i, regNew+i);.  
2da40 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c        sqlite3Col
2da41 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54  umnDefault(v, pT
2da42 61 62 2c 20 69 2c 20 72 65 67 4e 65 77 2b 69 29  ab, i, regNew+i)
2da43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2da44 20 20 7d 0a 0a 20 20 69 66 28 20 21 69 73 56 69    }..  if( !isVi
2da45 65 77 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31  ew ){.    int j1
2da46 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2da47 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
2da48 73 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72  ss of jump instr
2da49 75 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 20 20 2f  uction */..    /
2da4a 2a 20 44 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20  * Do constraint 
2da4b 63 68 65 63 6b 73 2e 20 2a 2f 0a 20 20 20 20 73  checks. */.    s
2da4c 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f  qlite3GenerateCo
2da4d 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 70  nstraintChecks(p
2da4e 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
2da4f 72 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 2c 0a  r, regNewRowid,.
2da50 20 20 20 20 20 20 20 20 61 52 65 67 49 64 78 2c          aRegIdx,
2da51 20 28 63 68 6e 67 52 6f 77 69 64 3f 72 65 67 4f   (chngRowid?regO
2da52 6c 64 52 6f 77 69 64 3a 30 29 2c 20 31 2c 20 6f  ldRowid:0), 1, o
2da53 6e 45 72 72 6f 72 2c 20 61 64 64 72 2c 20 30 29  nError, addr, 0)
2da54 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 46 4b 20  ;..    /* Do FK 
2da55 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b  constraint check
2da56 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 68 61  s. */.    if( ha
2da57 73 46 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  sFK ){.      sql
2da58 69 74 65 33 46 6b 43 68 65 63 6b 28 70 50 61 72  ite3FkCheck(pPar
2da59 73 65 2c 20 70 54 61 62 2c 20 72 65 67 4f 6c 64  se, pTab, regOld
2da5a 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20 7d  Rowid, 0);.    }
2da5b 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20  ..    /* Delete 
2da5c 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 69 65  the index entrie
2da5d 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2da5e 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 65  h the current re
2da5f 63 6f 72 64 2e 20 20 2a 2f 0a 20 20 20 20 6a 31  cord.  */.    j1
2da60 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2da61 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
2da62 69 73 74 73 2c 20 69 43 75 72 2c 20 30 2c 20 72  ists, iCur, 0, r
2da63 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20  egOldRowid);.   
2da64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
2da65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65 28 70  RowIndexDelete(p
2da66 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43 75  Parse, pTab, iCu
2da67 72 2c 20 61 52 65 67 49 64 78 29 3b 0a 20 20 0a  r, aRegIdx);.  .
2da68 20 20 20 20 2f 2a 20 49 66 20 63 68 61 6e 67 69      /* If changi
2da69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75  ng the record nu
2da6a 6d 62 65 72 2c 20 64 65 6c 65 74 65 20 74 68 65  mber, delete the
2da6b 20 6f 6c 64 20 72 65 63 6f 72 64 2e 20 20 2a 2f   old record.  */
2da6c 0a 20 20 20 20 69 66 28 20 68 61 73 46 4b 20 7c  .    if( hasFK |
2da6d 7c 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a 20  | chngRowid ){. 
2da6e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2da6f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65 6c  AddOp2(v, OP_Del
2da70 65 74 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a 20  ete, iCur, 0);. 
2da71 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2da72 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2da73 6a 31 29 3b 0a 0a 20 20 20 20 69 66 28 20 68 61  j1);..    if( ha
2da74 73 46 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  sFK ){.      sql
2da75 69 74 65 33 46 6b 43 68 65 63 6b 28 70 50 61 72  ite3FkCheck(pPar
2da76 73 65 2c 20 70 54 61 62 2c 20 30 2c 20 72 65 67  se, pTab, 0, reg
2da77 4e 65 77 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d  NewRowid);.    }
2da78 0a 20 20 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72  .  .    /* Inser
2da79 74 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20  t the new index 
2da7a 65 6e 74 72 69 65 73 20 61 6e 64 20 74 68 65 20  entries and the 
2da7b 6e 65 77 20 72 65 63 6f 72 64 2e 20 2a 2f 0a 20  new record. */. 
2da7c 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65     sqlite3Comple
2da7d 74 65 49 6e 73 65 72 74 69 6f 6e 28 70 50 61 72  teInsertion(pPar
2da7e 73 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20  se, pTab, iCur, 
2da7f 72 65 67 4e 65 77 52 6f 77 69 64 2c 20 61 52 65  regNewRowid, aRe
2da80 67 49 64 78 2c 20 31 2c 20 30 2c 20 30 29 3b 0a  gIdx, 1, 0, 0);.
2da81 0a 20 20 20 20 2f 2a 20 44 6f 20 61 6e 79 20 4f  .    /* Do any O
2da82 4e 20 43 41 53 43 41 44 45 2c 20 53 45 54 20 4e  N CASCADE, SET N
2da83 55 4c 4c 20 6f 72 20 53 45 54 20 44 45 46 41 55  ULL or SET DEFAU
2da84 4c 54 20 6f 70 65 72 61 74 69 6f 6e 73 20 72 65  LT operations re
2da85 71 75 69 72 65 64 20 74 6f 0a 20 20 20 20 2a 2a  quired to.    **
2da86 20 68 61 6e 64 6c 65 20 72 6f 77 73 20 28 70 6f   handle rows (po
2da87 73 73 69 62 6c 79 20 69 6e 20 6f 74 68 65 72 20  ssibly in other 
2da88 74 61 62 6c 65 73 29 20 74 68 61 74 20 72 65 66  tables) that ref
2da89 65 72 20 76 69 61 20 61 20 66 6f 72 65 69 67 6e  er via a foreign
2da8a 20 6b 65 79 0a 20 20 20 20 2a 2a 20 74 6f 20 74   key.    ** to t
2da8b 68 65 20 72 6f 77 20 6a 75 73 74 20 75 70 64 61  he row just upda
2da8c 74 65 64 2e 20 2a 2f 20 0a 20 20 20 20 69 66 28  ted. */ .    if(
2da8d 20 68 61 73 46 4b 20 29 7b 0a 20 20 20 20 20 20   hasFK ){.      
2da8e 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e 73  sqlite3FkActions
2da8f 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70  (pParse, pTab, p
2da90 43 68 61 6e 67 65 73 2c 20 72 65 67 4f 6c 64 52  Changes, regOldR
2da91 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  owid);.    }.  }
2da92 0a 0a 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74  ..  /* Increment
2da93 20 74 68 65 20 72 6f 77 20 63 6f 75 6e 74 65 72   the row counter
2da94 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62   .  */.  if( (db
2da95 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2da96 5f 43 6f 75 6e 74 52 6f 77 73 29 20 26 26 20 21  _CountRows) && !
2da97 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
2da98 54 61 62 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Tab){.    sqlite
2da99 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2da9a 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77  P_AddImm, regRow
2da9b 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a  Count, 1);.  }..
2da9c 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77    sqlite3CodeRow
2da9d 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20  Trigger(pParse, 
2da9e 70 54 72 69 67 67 65 72 2c 20 54 4b 5f 55 50 44  pTrigger, TK_UPD
2da9f 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 0a  ATE, pChanges, .
2daa0 20 20 20 20 20 20 54 52 49 47 47 45 52 5f 41 46        TRIGGER_AF
2daa1 54 45 52 2c 20 70 54 61 62 2c 20 72 65 67 4f 6c  TER, pTab, regOl
2daa2 64 52 6f 77 69 64 2c 20 6f 6e 45 72 72 6f 72 2c  dRowid, onError,
2daa3 20 61 64 64 72 29 3b 0a 0a 20 20 2f 2a 20 52 65   addr);..  /* Re
2daa4 70 65 61 74 20 74 68 65 20 61 62 6f 76 65 20 77  peat the above w
2daa5 69 74 68 20 74 68 65 20 6e 65 78 74 20 72 65 63  ith the next rec
2daa6 6f 72 64 20 74 6f 20 62 65 20 75 70 64 61 74 65  ord to be update
2daa7 64 2c 20 75 6e 74 69 6c 0a 20 20 2a 2a 20 61 6c  d, until.  ** al
2daa8 6c 20 72 65 63 6f 72 64 20 73 65 6c 65 63 74 65  l record selecte
2daa9 64 20 62 79 20 74 68 65 20 57 48 45 52 45 20 63  d by the WHERE c
2daaa 6c 61 75 73 65 20 68 61 76 65 20 62 65 65 6e 20  lause have been 
2daab 75 70 64 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20  updated..  */.  
2daac 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2daad 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
2daae 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69 74 65   addr);.  sqlite
2daaf 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2dab0 20 61 64 64 72 29 3b 0a 0a 20 20 2f 2a 20 43 6c   addr);..  /* Cl
2dab1 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73 20 2a  ose all tables *
2dab2 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 64  /.  for(i=0, pId
2dab3 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2dab4 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
2dab5 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20  >pNext, i++){.  
2dab6 20 20 69 66 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c    if( openAll ||
2dab7 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20 29 7b   aRegIdx[i]>0 ){
2dab8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2dab9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
2daba 6c 6f 73 65 2c 20 69 43 75 72 2b 69 2b 31 2c 20  lose, iCur+i+1, 
2dabb 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  0);.    }.  }.  
2dabc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2dabd 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  2(v, OP_Close, i
2dabe 43 75 72 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 55  Cur, 0);..  /* U
2dabf 70 64 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  pdate the sqlite
2dac0 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
2dac1 62 79 20 73 74 6f 72 69 6e 67 20 74 68 65 20 63  by storing the c
2dac2 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a 20 20  ontent of the.  
2dac3 2a 2a 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  ** maximum rowid
2dac4 20 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73 20   counter values 
2dac5 72 65 63 6f 72 64 65 64 20 77 68 69 6c 65 20 69  recorded while i
2dac6 6e 73 65 72 74 69 6e 67 20 69 6e 74 6f 0a 20 20  nserting into.  
2dac7 2a 2a 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  ** autoincrement
2dac8 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
2dac9 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74  if( pParse->nest
2daca 65 64 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d  ed==0 && pParse-
2dacb 3e 70 54 72 69 67 67 65 72 54 61 62 3d 3d 30 20  >pTriggerTab==0 
2dacc 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75  ){.    sqlite3Au
2dacd 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 70  toincrementEnd(p
2dace 50 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f  Parse);.  }..  /
2dacf 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74 68  *.  ** Return th
2dad0 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
2dad1 20 74 68 61 74 20 77 65 72 65 20 63 68 61 6e 67   that were chang
2dad2 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74  ed. If this rout
2dad3 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e  ine is .  ** gen
2dad4 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63  erating code bec
2dad5 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74  ause of a call t
2dad6 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50  o sqlite3NestedP
2dad7 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20  arse(), do not. 
2dad8 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63   ** invoke the c
2dad9 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
2dada 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62  ..  */.  if( (db
2dadb 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43  ->flags&SQLITE_C
2dadc 6f 75 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50  ountRows) && !pP
2dadd 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61  arse->pTriggerTa
2dade 62 20 26 26 20 21 70 50 61 72 73 65 2d 3e 6e 65  b && !pParse->ne
2dadf 73 74 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  sted ){.    sqli
2dae0 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2dae1 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72   OP_ResultRow, r
2dae2 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a  egRowCount, 1);.
2dae3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2dae4 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
2dae5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2dae6 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
2dae7 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
2dae8 72 6f 77 73 20 75 70 64 61 74 65 64 22 2c 20 53  rows updated", S
2dae9 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2daea 20 7d 0a 0a 75 70 64 61 74 65 5f 63 6c 65 61 6e   }..update_clean
2daeb 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 41 75 74  up:.  sqlite3Aut
2daec 68 43 6f 6e 74 65 78 74 50 6f 70 28 26 73 43 6f  hContextPop(&sCo
2daed 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65  ntext);.  sqlite
2daee 33 44 62 46 72 65 65 28 64 62 2c 20 61 52 65 67  3DbFree(db, aReg
2daef 49 64 78 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Idx);.  sqlite3D
2daf0 62 46 72 65 65 28 64 62 2c 20 61 58 52 65 66 29  bFree(db, aXRef)
2daf1 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  ;.  sqlite3SrcLi
2daf2 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61  stDelete(db, pTa
2daf3 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  bList);.  sqlite
2daf4 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2daf5 64 62 2c 20 70 43 68 61 6e 67 65 73 29 3b 0a 20  db, pChanges);. 
2daf6 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2daf7 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a  te(db, pWhere);.
2daf8 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 2f 2a 20 4d    return;.}./* M
2daf9 61 6b 65 20 73 75 72 65 20 22 69 73 56 69 65 77  ake sure "isView
2dafa 22 20 61 6e 64 20 6f 74 68 65 72 20 6d 61 63 72  " and other macr
2dafb 6f 73 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65  os defined above
2dafc 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 20   are undefined. 
2dafd 4f 74 68 65 72 77 69 73 65 0a 2a 2a 20 74 68 65  Otherwise.** the
2dafe 6c 79 20 6d 61 79 20 69 6e 74 65 72 66 65 72 65  ly may interfere
2daff 20 77 69 74 68 20 63 6f 6d 70 69 6c 61 74 69 6f   with compilatio
2db00 6e 20 6f 66 20 6f 74 68 65 72 20 66 75 6e 63 74  n of other funct
2db01 69 6f 6e 73 20 69 6e 20 74 68 69 73 20 66 69 6c  ions in this fil
2db02 65 0a 2a 2a 20 28 6f 72 20 69 6e 20 61 6e 6f 74  e.** (or in anot
2db03 68 65 72 20 66 69 6c 65 2c 20 69 66 20 74 68 69  her file, if thi
2db04 73 20 66 69 6c 65 20 62 65 63 6f 6d 65 73 20 70  s file becomes p
2db05 61 72 74 20 6f 66 20 74 68 65 20 61 6d 61 6c 67  art of the amalg
2db06 61 6d 61 74 69 6f 6e 29 2e 20 20 2a 2f 0a 23 69  amation).  */.#i
2db07 66 64 65 66 20 69 73 56 69 65 77 0a 20 23 75 6e  fdef isView. #un
2db08 64 65 66 20 69 73 56 69 65 77 0a 23 65 6e 64 69  def isView.#endi
2db09 66 0a 23 69 66 64 65 66 20 70 54 72 69 67 67 65  f.#ifdef pTrigge
2db0a 72 0a 20 23 75 6e 64 65 66 20 70 54 72 69 67 67  r. #undef pTrigg
2db0b 65 72 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  er.#endif..#ifnd
2db0c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
2db0d 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a  IRTUALTABLE./*.*
2db0e 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2db0f 66 6f 72 20 61 6e 20 55 50 44 41 54 45 20 6f 66  for an UPDATE of
2db10 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2db11 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 74 72 61  ..**.** The stra
2db12 74 65 67 79 20 69 73 20 74 68 61 74 20 77 65 20  tegy is that we 
2db13 63 72 65 61 74 65 20 61 6e 20 65 70 68 65 6d 65  create an epheme
2db14 72 69 61 6c 20 74 61 62 6c 65 20 74 68 61 74 20  rial table that 
2db15 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 66 6f 72 20  contains.** for 
2db16 65 61 63 68 20 72 6f 77 20 74 6f 20 62 65 20 63  each row to be c
2db17 68 61 6e 67 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20  hanged:.**.**   
2db18 28 41 29 20 20 54 68 65 20 6f 72 69 67 69 6e 61  (A)  The origina
2db19 6c 20 72 6f 77 69 64 20 6f 66 20 74 68 61 74 20  l rowid of that 
2db1a 72 6f 77 2e 0a 2a 2a 20 20 20 28 42 29 20 20 54  row..**   (B)  T
2db1b 68 65 20 72 65 76 69 73 65 64 20 72 6f 77 69 64  he revised rowid
2db1c 20 66 6f 72 20 74 68 65 20 72 6f 77 2e 20 28 6e   for the row. (n
2db1d 6f 74 65 31 29 0a 2a 2a 20 20 20 28 43 29 20 20  ote1).**   (C)  
2db1e 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 65  The content of e
2db1f 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 74  very column in t
2db20 68 65 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68  he row..**.** Th
2db21 65 6e 20 77 65 20 6c 6f 6f 70 20 6f 76 65 72 20  en we loop over 
2db22 74 68 69 73 20 65 70 68 65 6d 65 72 61 6c 20 74  this ephemeral t
2db23 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 65 61 63  able and for eac
2db24 68 20 72 6f 77 20 69 6e 0a 2a 2a 20 74 68 65 20  h row in.** the 
2db25 65 70 68 65 72 6d 65 72 61 6c 20 74 61 62 6c 65  ephermeral table
2db26 20 63 61 6c 6c 20 56 55 70 64 61 74 65 2e 0a 2a   call VUpdate..*
2db27 2a 0a 2a 2a 20 57 68 65 6e 20 66 69 6e 69 73 68  *.** When finish
2db28 65 64 2c 20 64 72 6f 70 20 74 68 65 20 65 70 68  ed, drop the eph
2db29 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  emeral table..**
2db2a 0a 2a 2a 20 28 6e 6f 74 65 31 29 20 41 63 74 75  .** (note1) Actu
2db2b 61 6c 6c 79 2c 20 69 66 20 77 65 20 6b 6e 6f 77  ally, if we know
2db2c 20 69 6e 20 61 64 76 61 6e 63 65 20 74 68 61 74   in advance that
2db2d 20 28 41 29 20 69 73 20 61 6c 77 61 79 73 20 74   (A) is always t
2db2e 68 65 20 73 61 6d 65 0a 2a 2a 20 61 73 20 28 42  he same.** as (B
2db2f 29 20 77 65 20 6f 6e 6c 79 20 73 74 6f 72 65 20  ) we only store 
2db30 28 41 29 2c 20 74 68 65 6e 20 64 75 70 6c 69 63  (A), then duplic
2db31 61 74 65 20 28 41 29 20 77 68 65 6e 20 70 75 6c  ate (A) when pul
2db32 6c 69 6e 67 0a 2a 2a 20 69 74 20 6f 75 74 20 6f  ling.** it out o
2db33 66 20 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20  f the ephemeral 
2db34 74 61 62 6c 65 20 62 65 66 6f 72 65 20 63 61 6c  table before cal
2db35 6c 69 6e 67 20 56 55 70 64 61 74 65 2e 0a 2a 2f  ling VUpdate..*/
2db36 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64  .static void upd
2db37 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c 65 28  ateVirtualTable(
2db38 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2db39 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70  ,       /* The p
2db3a 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2db3b 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
2db3c 63 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  c,       /* The 
2db3d 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74 6f  virtual table to
2db3e 20 62 65 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a   be modified */.
2db3f 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
2db40 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
2db41 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
2db42 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e   ExprList *pChan
2db43 67 65 73 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c  ges,  /* The col
2db44 75 6d 6e 73 20 74 6f 20 63 68 61 6e 67 65 20 69  umns to change i
2db45 6e 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  n the UPDATE sta
2db46 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72  tement */.  Expr
2db47 20 2a 70 52 6f 77 69 64 2c 20 20 20 20 20 20 20   *pRowid,       
2db48 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 75   /* Expression u
2db49 73 65 64 20 74 6f 20 72 65 63 6f 6d 70 75 74 65  sed to recompute
2db4a 20 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20   the rowid */.  
2db4b 69 6e 74 20 2a 61 58 52 65 66 2c 20 20 20 20 20  int *aXRef,     
2db4c 20 20 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20       /* Mapping 
2db4d 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  from columns of 
2db4e 70 54 61 62 20 74 6f 20 65 6e 74 72 69 65 73 20  pTab to entries 
2db4f 69 6e 20 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20  in pChanges */. 
2db50 20 45 78 70 72 20 2a 70 57 68 65 72 65 20 20 20   Expr *pWhere   
2db51 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63        /* WHERE c
2db52 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50 44  lause of the UPD
2db53 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ATE statement */
2db54 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  .){.  Vdbe *v = 
2db55 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20  pParse->pVdbe;  
2db56 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61 63 68 69  /* Virtual machi
2db57 6e 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ne under constru
2db58 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c  ction */.  ExprL
2db59 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 30 3b  ist *pEList = 0;
2db5a 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75       /* The resu
2db5b 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45  lt set of the SE
2db5c 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
2db5d 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
2db5e 65 63 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  ect = 0;      /*
2db5f 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   The SELECT stat
2db60 65 6d 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20  ement */.  Expr 
2db61 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
2db62 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72       /* Temporar
2db63 79 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  y expression */.
2db64 20 20 69 6e 74 20 65 70 68 65 6d 54 61 62 3b 20    int ephemTab; 
2db65 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2db66 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20 74 68 65  able holding the
2db67 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 53   result of the S
2db68 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74 20 69  ELECT */.  int i
2db69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2db6a 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
2db6b 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 64  nter */.  int ad
2db6c 64 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dr;             
2db6d 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
2db6e 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f  f top of loop */
2db6f 0a 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20  .  int iReg;    
2db70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2db71 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20 69  First register i
2db72 6e 20 73 65 74 20 70 61 73 73 65 64 20 74 6f 20  n set passed to 
2db73 4f 50 5f 56 55 70 64 61 74 65 20 2a 2f 0a 20 20  OP_VUpdate */.  
2db74 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2db75 61 72 73 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74  arse->db; /* Dat
2db76 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2db77 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2db78 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74   *pVTab = (const
2db79 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 65   char*)sqlite3Ge
2db7a 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62  tVTable(db, pTab
2db7b 29 3b 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  );.  SelectDest 
2db7c 64 65 73 74 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73  dest;..  /* Cons
2db7d 74 72 75 63 74 20 74 68 65 20 53 45 4c 45 43 54  truct the SELECT
2db7e 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
2db7f 77 69 6c 6c 20 66 69 6e 64 20 74 68 65 20 6e 65  will find the ne
2db80 77 20 76 61 6c 75 65 73 20 66 6f 72 0a 20 20 2a  w values for.  *
2db81 2a 20 61 6c 6c 20 75 70 64 61 74 65 64 20 72 6f  * all updated ro
2db82 77 73 2e 20 0a 20 20 2a 2f 0a 20 20 70 45 4c 69  ws. .  */.  pELi
2db83 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
2db84 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2db85 65 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70  e, 0, sqlite3Exp
2db86 72 28 64 62 2c 20 54 4b 5f 49 44 2c 20 22 5f 72  r(db, TK_ID, "_r
2db87 6f 77 69 64 5f 22 29 29 3b 0a 20 20 69 66 28 20  owid_"));.  if( 
2db88 70 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 70 45  pRowid ){.    pE
2db89 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
2db8a 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2db8b 72 73 65 2c 20 70 45 4c 69 73 74 2c 0a 20 20 20  rse, pEList,.   
2db8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db8e 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2db8f 62 2c 20 70 52 6f 77 69 64 2c 20 30 29 29 3b 0a  b, pRowid, 0));.
2db90 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54    }.  assert( pT
2db91 61 62 2d 3e 69 50 4b 65 79 3c 30 20 29 3b 0a 20  ab->iPKey<0 );. 
2db92 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
2db93 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
2db94 20 20 69 66 28 20 61 58 52 65 66 5b 69 5d 3e 3d    if( aXRef[i]>=
2db95 30 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70 72  0 ){.      pExpr
2db96 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
2db97 70 28 64 62 2c 20 70 43 68 61 6e 67 65 73 2d 3e  p(db, pChanges->
2db98 61 5b 61 58 52 65 66 5b 69 5d 5d 2e 70 45 78 70  a[aXRef[i]].pExp
2db99 72 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65  r, 0);.    }else
2db9a 7b 0a 20 20 20 20 20 20 70 45 78 70 72 20 3d 20  {.      pExpr = 
2db9b 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20  sqlite3Expr(db, 
2db9c 54 4b 5f 49 44 2c 20 70 54 61 62 2d 3e 61 43 6f  TK_ID, pTab->aCo
2db9d 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20  l[i].zName);.   
2db9e 20 7d 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20   }.    pEList = 
2db9f 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2dba0 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 45  ppend(pParse, pE
2dba1 4c 69 73 74 2c 20 70 45 78 70 72 29 3b 0a 20 20  List, pExpr);.  
2dba2 7d 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71  }.  pSelect = sq
2dba3 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70  lite3SelectNew(p
2dba4 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70  Parse, pEList, p
2dba5 53 72 63 2c 20 70 57 68 65 72 65 2c 20 30 2c 20  Src, pWhere, 0, 
2dba6 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  0, 0, 0, 0, 0);.
2dba7 20 20 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74    .  /* Create t
2dba8 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
2dba9 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74 68  le into which th
2dbaa 65 20 75 70 64 61 74 65 20 72 65 73 75 6c 74 73  e update results
2dbab 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 73 74   will.  ** be st
2dbac 6f 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  ored..  */.  ass
2dbad 65 72 74 28 20 76 20 29 3b 0a 20 20 65 70 68 65  ert( v );.  ephe
2dbae 6d 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e  mTab = pParse->n
2dbaf 54 61 62 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33  Tab++;.  sqlite3
2dbb0 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2dbb1 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
2dbb2 65 70 68 65 6d 54 61 62 2c 20 70 54 61 62 2d 3e  ephemTab, pTab->
2dbb3 6e 43 6f 6c 2b 31 2b 28 70 52 6f 77 69 64 21 3d  nCol+1+(pRowid!=
2dbb4 30 29 29 3b 0a 0a 20 20 2f 2a 20 66 69 6c 6c 20  0));..  /* fill 
2dbb5 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61  the ephemeral ta
2dbb6 62 6c 65 20 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ble .  */.  sqli
2dbb7 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
2dbb8 74 28 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62  t(&dest, SRT_Tab
2dbb9 6c 65 2c 20 65 70 68 65 6d 54 61 62 29 3b 0a 20  le, ephemTab);. 
2dbba 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
2dbbb 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20  Parse, pSelect, 
2dbbc 26 64 65 73 74 29 3b 0a 0a 20 20 2f 2a 20 47 65  &dest);..  /* Ge
2dbbd 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73  nerate code to s
2dbbe 63 61 6e 20 74 68 65 20 65 70 68 65 6d 65 72 61  can the ephemera
2dbbf 6c 20 74 61 62 6c 65 20 61 6e 64 20 63 61 6c 6c  l table and call
2dbc0 20 56 55 70 64 61 74 65 2e 20 2a 2f 0a 20 20 69   VUpdate. */.  i
2dbc1 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Reg = ++pParse->
2dbc2 6e 4d 65 6d 3b 0a 20 20 70 50 61 72 73 65 2d 3e  nMem;.  pParse->
2dbc3 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43  nMem += pTab->nC
2dbc4 6f 6c 2b 31 3b 0a 20 20 61 64 64 72 20 3d 20 73  ol+1;.  addr = s
2dbc5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2dbc6 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 65  (v, OP_Rewind, e
2dbc7 70 68 65 6d 54 61 62 2c 20 30 29 3b 0a 20 20 73  phemTab, 0);.  s
2dbc8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2dbc9 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20  (v, OP_Column,  
2dbca 65 70 68 65 6d 54 61 62 2c 20 30 2c 20 69 52 65  ephemTab, 0, iRe
2dbcb 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  g);.  sqlite3Vdb
2dbcc 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2dbcd 6c 75 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20  lumn, ephemTab, 
2dbce 28 70 52 6f 77 69 64 3f 31 3a 30 29 2c 20 69 52  (pRowid?1:0), iR
2dbcf 65 67 2b 31 29 3b 0a 20 20 66 6f 72 28 69 3d 30  eg+1);.  for(i=0
2dbd0 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  ; i<pTab->nCol; 
2dbd1 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
2dbd2 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2dbd3 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70 68 65 6d 54  P_Column, ephemT
2dbd4 61 62 2c 20 69 2b 31 2b 28 70 52 6f 77 69 64 21  ab, i+1+(pRowid!
2dbd5 3d 30 29 2c 20 69 52 65 67 2b 32 2b 69 29 3b 0a  =0), iReg+2+i);.
2dbd6 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 74 61    }.  sqlite3Vta
2dbd7 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50  bMakeWritable(pP
2dbd8 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 73  arse, pTab);.  s
2dbd9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2dbda 28 76 2c 20 4f 50 5f 56 55 70 64 61 74 65 2c 20  (v, OP_VUpdate, 
2dbdb 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c  0, pTab->nCol+2,
2dbdc 20 69 52 65 67 2c 20 70 56 54 61 62 2c 20 50 34   iReg, pVTab, P4
2dbdd 5f 56 54 41 42 29 3b 0a 20 20 73 71 6c 69 74 65  _VTAB);.  sqlite
2dbde 33 4d 61 79 41 62 6f 72 74 28 70 50 61 72 73 65  3MayAbort(pParse
2dbdf 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
2dbe0 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
2dbe1 74 2c 20 65 70 68 65 6d 54 61 62 2c 20 61 64 64  t, ephemTab, add
2dbe2 72 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  r+1);.  sqlite3V
2dbe3 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
2dbe4 64 64 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  ddr);.  sqlite3V
2dbe5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2dbe6 43 6c 6f 73 65 2c 20 65 70 68 65 6d 54 61 62 2c  Close, ephemTab,
2dbe7 20 30 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e   0);..  /* Clean
2dbe8 75 70 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53  up */.  sqlite3S
2dbe9 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
2dbea 70 53 65 6c 65 63 74 29 3b 20 20 0a 7d 0a 23 65  pSelect);  .}.#e
2dbeb 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2dbec 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2dbed 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
2dbee 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 70 64 61  **** End of upda
2dbef 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  te.c ***********
2dbf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbf1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbf2 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2dbf3 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2dbf4 76 61 63 75 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a  vacuum.c *******
2dbf5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbf6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dbf7 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20  ***/./*.** 2003 
2dbf8 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68  April 6.**.** Th
2dbf9 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
2dbfa 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
2dbfb 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
2dbfc 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
2dbfd 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
2dbfe 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
2dbff 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
2dc00 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
2dc01 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
2dc02 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
2dc03 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
2dc04 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
2dc05 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
2dc06 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
2dc07 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
2dc08 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
2dc09 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
2dc0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc0e 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
2dc0f 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
2dc10 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  e used to implem
2dc11 65 6e 74 20 74 68 65 20 56 41 43 55 55 4d 20 63  ent the VACUUM c
2dc12 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 4d 6f  ommand..**.** Mo
2dc13 73 74 20 6f 66 20 74 68 65 20 63 6f 64 65 20 69  st of the code i
2dc14 6e 20 74 68 69 73 20 66 69 6c 65 20 6d 61 79 20  n this file may 
2dc15 62 65 20 6f 6d 69 74 74 65 64 20 62 79 20 64 65  be omitted by de
2dc16 66 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 53 51  fining the.** SQ
2dc17 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
2dc18 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 0a 23 69 66 20   macro..*/..#if 
2dc19 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2dc1a 4f 4d 49 54 5f 56 41 43 55 55 4d 29 20 26 26 20  OMIT_VACUUM) && 
2dc1b 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2dc1c 4f 4d 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a 0a  OMIT_ATTACH)./*.
2dc1d 2a 2a 20 45 78 65 63 75 74 65 20 7a 53 71 6c 20  ** Execute zSql 
2dc1e 6f 6e 20 64 61 74 61 62 61 73 65 20 64 62 2e 20  on database db. 
2dc1f 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
2dc20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  code..*/.static 
2dc21 69 6e 74 20 65 78 65 63 53 71 6c 28 73 71 6c 69  int execSql(sqli
2dc22 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
2dc23 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20 20 73 71  har *zSql){.  sq
2dc24 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
2dc25 74 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69  t;.  VVA_ONLY( i
2dc26 6e 74 20 72 63 3b 20 29 0a 20 20 69 66 28 20 21  nt rc; ).  if( !
2dc27 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 65 74 75  zSql ){.    retu
2dc28 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2dc29 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54  .  }.  if( SQLIT
2dc2a 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 5f 70 72  E_OK!=sqlite3_pr
2dc2b 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
2dc2c 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 20 29  -1, &pStmt, 0) )
2dc2d 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
2dc2e 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29  ite3_errcode(db)
2dc2f 3b 0a 20 20 7d 0a 20 20 56 56 41 5f 4f 4e 4c 59  ;.  }.  VVA_ONLY
2dc30 28 20 72 63 20 3d 20 29 20 73 71 6c 69 74 65 33  ( rc = ) sqlite3
2dc31 5f 73 74 65 70 28 70 53 74 6d 74 29 3b 0a 20 20  _step(pStmt);.  
2dc32 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
2dc33 54 45 5f 52 4f 57 20 29 3b 0a 20 20 72 65 74 75  TE_ROW );.  retu
2dc34 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  rn sqlite3_final
2dc35 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f  ize(pStmt);.}../
2dc36 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 7a 53 71  *.** Execute zSq
2dc37 6c 20 6f 6e 20 64 61 74 61 62 61 73 65 20 64 62  l on database db
2dc38 2e 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20  . The statement 
2dc39 72 65 74 75 72 6e 73 20 65 78 61 63 74 6c 79 0a  returns exactly.
2dc3a 2a 2a 20 6f 6e 65 20 63 6f 6c 75 6d 6e 2e 20 45  ** one column. E
2dc3b 78 65 63 75 74 65 20 74 68 69 73 20 61 73 20 53  xecute this as S
2dc3c 51 4c 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64  QL on the same d
2dc3d 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74  atabase..*/.stat
2dc3e 69 63 20 69 6e 74 20 65 78 65 63 45 78 65 63 53  ic int execExecS
2dc3f 71 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ql(sqlite3 *db, 
2dc40 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
2dc41 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
2dc42 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20  t *pStmt;.  int 
2dc43 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
2dc44 74 65 33 5f 70 72 65 70 61 72 65 28 64 62 2c 20  te3_prepare(db, 
2dc45 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74  zSql, -1, &pStmt
2dc46 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
2dc47 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2dc48 72 6e 20 72 63 3b 0a 0a 20 20 77 68 69 6c 65 28  rn rc;..  while(
2dc49 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c   SQLITE_ROW==sql
2dc4a 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29  ite3_step(pStmt)
2dc4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 65 78 65   ){.    rc = exe
2dc4c 63 53 71 6c 28 64 62 2c 20 28 63 68 61 72 2a 29  cSql(db, (char*)
2dc4d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
2dc4e 65 78 74 28 70 53 74 6d 74 2c 20 30 29 29 3b 0a  ext(pStmt, 0));.
2dc4f 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2dc50 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
2dc51 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
2dc52 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 72 65  pStmt);.      re
2dc53 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
2dc54 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c   }..  return sql
2dc55 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
2dc56 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  tmt);.}../*.** T
2dc57 68 65 20 6e 6f 6e 2d 73 74 61 6e 64 61 72 64 20  he non-standard 
2dc58 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 69  VACUUM command i
2dc59 73 20 75 73 65 64 20 74 6f 20 63 6c 65 61 6e 20  s used to clean 
2dc5a 75 70 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  up the database,
2dc5b 0a 2a 2a 20 63 6f 6c 6c 61 70 73 65 20 66 72 65  .** collapse fre
2dc5c 65 20 73 70 61 63 65 2c 20 65 74 63 2e 20 20 49  e space, etc.  I
2dc5d 74 20 69 73 20 6d 6f 64 65 6c 6c 65 64 20 61 66  t is modelled af
2dc5e 74 65 72 20 74 68 65 20 56 41 43 55 55 4d 20 63  ter the VACUUM c
2dc5f 6f 6d 6d 61 6e 64 0a 2a 2a 20 69 6e 20 50 6f 73  ommand.** in Pos
2dc60 74 67 72 65 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49  tgreSQL..**.** I
2dc61 6e 20 76 65 72 73 69 6f 6e 20 31 2e 30 2e 78 20  n version 1.0.x 
2dc62 6f 66 20 53 51 4c 69 74 65 2c 20 74 68 65 20 56  of SQLite, the V
2dc63 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 77 6f  ACUUM command wo
2dc64 75 6c 64 20 63 61 6c 6c 0a 2a 2a 20 67 64 62 6d  uld call.** gdbm
2dc65 5f 72 65 6f 72 67 61 6e 69 7a 65 28 29 20 6f 6e  _reorganize() on
2dc66 20 61 6c 6c 20 74 68 65 20 64 61 74 61 62 61 73   all the databas
2dc67 65 20 74 61 62 6c 65 73 2e 20 20 42 75 74 20 62  e tables.  But b
2dc68 65 67 69 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 68  eginning.** with
2dc69 20 32 2e 30 2e 30 2c 20 53 51 4c 69 74 65 20 6e   2.0.0, SQLite n
2dc6a 6f 20 6c 6f 6e 67 65 72 20 75 73 65 73 20 47 44  o longer uses GD
2dc6b 42 4d 20 73 6f 20 74 68 69 73 20 63 6f 6d 6d 61  BM so this comma
2dc6c 6e 64 20 68 61 73 0a 2a 2a 20 62 65 63 6f 6d 65  nd has.** become
2dc6d 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c   a no-op..*/.SQL
2dc6e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2dc6f 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50   sqlite3Vacuum(P
2dc70 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
2dc71 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
2dc72 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2dc73 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  );.  if( v ){.  
2dc74 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2dc75 4f 70 32 28 76 2c 20 4f 50 5f 56 61 63 75 75 6d  Op2(v, OP_Vacuum
2dc76 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  , 0, 0);.  }.  r
2dc77 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eturn;.}../*.** 
2dc78 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70  This routine imp
2dc79 6c 65 6d 65 6e 74 73 20 74 68 65 20 4f 50 5f 56  lements the OP_V
2dc7a 61 63 75 75 6d 20 6f 70 63 6f 64 65 20 6f 66 20  acuum opcode of 
2dc7b 74 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c  the VDBE..*/.SQL
2dc7c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2dc7d 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75 75 6d  sqlite3RunVacuum
2dc7e 28 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67  (char **pzErrMsg
2dc7f 2c 20 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  , sqlite3 *db){.
2dc80 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
2dc81 45 5f 4f 4b 3b 20 20 20 20 20 2f 2a 20 52 65 74  E_OK;     /* Ret
2dc82 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 65  urn code from se
2dc83 72 76 69 63 65 20 72 6f 75 74 69 6e 65 73 20 2a  rvice routines *
2dc84 2f 0a 20 20 42 74 72 65 65 20 2a 70 4d 61 69 6e  /.  Btree *pMain
2dc85 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
2dc86 68 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e  he database bein
2dc87 67 20 76 61 63 75 75 6d 65 64 20 2a 2f 0a 20 20  g vacuumed */.  
2dc88 42 74 72 65 65 20 2a 70 54 65 6d 70 3b 20 20 20  Btree *pTemp;   
2dc89 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
2dc8a 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
2dc8b 65 20 77 65 20 76 61 63 75 75 6d 20 69 6e 74 6f  e we vacuum into
2dc8c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c   */.  char *zSql
2dc8d 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
2dc8e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
2dc8f 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 5f 66  */.  int saved_f
2dc90 6c 61 67 73 3b 20 20 20 20 20 20 20 20 2f 2a 20  lags;        /* 
2dc91 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66 20 74  Saved value of t
2dc92 68 65 20 64 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a  he db->flags */.
2dc93 20 20 69 6e 74 20 73 61 76 65 64 5f 6e 43 68 61    int saved_nCha
2dc94 6e 67 65 3b 20 20 20 20 20 20 2f 2a 20 53 61 76  nge;      /* Sav
2dc95 65 64 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e  ed value of db->
2dc96 6e 43 68 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74  nChange */.  int
2dc97 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c 43 68 61   saved_nTotalCha
2dc98 6e 67 65 3b 20 2f 2a 20 53 61 76 65 64 20 76 61  nge; /* Saved va
2dc99 6c 75 65 20 6f 66 20 64 62 2d 3e 6e 54 6f 74 61  lue of db->nTota
2dc9a 6c 43 68 61 6e 67 65 20 2a 2f 0a 20 20 44 62 20  lChange */.  Db 
2dc9b 2a 70 44 62 20 3d 20 30 3b 20 20 20 20 20 20 20  *pDb = 0;       
2dc9c 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2dc9d 20 74 6f 20 64 65 74 61 63 68 20 61 74 20 65 6e   to detach at en
2dc9e 64 20 6f 66 20 76 61 63 75 75 6d 20 2a 2f 0a 20  d of vacuum */. 
2dc9f 20 69 6e 74 20 69 73 4d 65 6d 44 62 3b 20 20 20   int isMemDb;   
2dca0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2dca1 20 69 66 20 76 61 63 75 75 6d 69 6e 67 20 61 20   if vacuuming a 
2dca2 3a 6d 65 6d 6f 72 79 3a 20 64 61 74 61 62 61 73  :memory: databas
2dca3 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73 3b  e */.  int nRes;
2dca4 0a 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74  ..  if( !db->aut
2dca5 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73  oCommit ){.    s
2dca6 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
2dca7 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63  pzErrMsg, db, "c
2dca8 61 6e 6e 6f 74 20 56 41 43 55 55 4d 20 66 72 6f  annot VACUUM fro
2dca9 6d 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73  m within a trans
2dcaa 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 72 65  action");.    re
2dcab 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
2dcac 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76  R;.  }..  /* Sav
2dcad 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  e the current va
2dcae 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  lue of the datab
2dcaf 61 73 65 20 66 6c 61 67 73 20 73 6f 20 74 68 61  ase flags so tha
2dcb0 74 20 69 74 20 63 61 6e 20 62 65 20 0a 20 20 2a  t it can be .  *
2dcb1 2a 20 72 65 73 74 6f 72 65 64 20 62 65 66 6f 72  * restored befor
2dcb2 65 20 72 65 74 75 72 6e 69 6e 67 2e 20 54 68 65  e returning. The
2dcb3 6e 20 73 65 74 20 74 68 65 20 77 72 69 74 61 62  n set the writab
2dcb4 6c 65 2d 73 63 68 65 6d 61 20 66 6c 61 67 2c 20  le-schema flag, 
2dcb5 61 6e 64 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65  and.  ** disable
2dcb6 20 43 48 45 43 4b 20 61 6e 64 20 66 6f 72 65 69   CHECK and forei
2dcb7 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
2dcb8 74 73 2e 20 20 2a 2f 0a 20 20 73 61 76 65 64 5f  ts.  */.  saved_
2dcb9 66 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67  flags = db->flag
2dcba 73 3b 0a 20 20 73 61 76 65 64 5f 6e 43 68 61 6e  s;.  saved_nChan
2dcbb 67 65 20 3d 20 64 62 2d 3e 6e 43 68 61 6e 67 65  ge = db->nChange
2dcbc 3b 0a 20 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c  ;.  saved_nTotal
2dcbd 43 68 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 54 6f  Change = db->nTo
2dcbe 74 61 6c 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d  talChange;.  db-
2dcbf 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45  >flags |= SQLITE
2dcc0 5f 57 72 69 74 65 53 63 68 65 6d 61 20 7c 20 53  _WriteSchema | S
2dcc1 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63  QLITE_IgnoreChec
2dcc2 6b 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20  ks;.  db->flags 
2dcc3 26 3d 20 7e 53 51 4c 49 54 45 5f 46 6f 72 65 69  &= ~SQLITE_Forei
2dcc4 67 6e 4b 65 79 73 3b 0a 0a 20 20 70 4d 61 69 6e  gnKeys;..  pMain
2dcc5 20 3d 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42   = db->aDb[0].pB
2dcc6 74 3b 0a 20 20 69 73 4d 65 6d 44 62 20 3d 20 73  t;.  isMemDb = s
2dcc7 71 6c 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d  qlite3PagerIsMem
2dcc8 64 62 28 73 71 6c 69 74 65 33 42 74 72 65 65 50  db(sqlite3BtreeP
2dcc9 61 67 65 72 28 70 4d 61 69 6e 29 29 3b 0a 0a 20  ager(pMain));.. 
2dcca 20 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20 74   /* Attach the t
2dccb 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
2dccc 65 20 61 73 20 27 76 61 63 75 75 6d 5f 64 62 27  e as 'vacuum_db'
2dccd 2e 20 54 68 65 20 73 79 6e 63 68 72 6f 6e 6f 75  . The synchronou
2dcce 73 20 70 72 61 67 6d 61 0a 20 20 2a 2a 20 63 61  s pragma.  ** ca
2dccf 6e 20 62 65 20 73 65 74 20 74 6f 20 27 6f 66 66  n be set to 'off
2dcd0 27 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 2c  ' for this file,
2dcd1 20 61 73 20 69 74 20 69 73 20 6e 6f 74 20 72 65   as it is not re
2dcd2 63 6f 76 65 72 65 64 20 69 66 20 61 20 63 72 61  covered if a cra
2dcd3 73 68 0a 20 20 2a 2a 20 6f 63 63 75 72 73 20 61  sh.  ** occurs a
2dcd4 6e 79 77 61 79 2e 20 54 68 65 20 69 6e 74 65 67  nyway. The integ
2dcd5 72 69 74 79 20 6f 66 20 74 68 65 20 64 61 74 61  rity of the data
2dcd6 62 61 73 65 20 69 73 20 6d 61 69 6e 74 61 69 6e  base is maintain
2dcd7 65 64 20 62 79 20 61 0a 20 20 2a 2a 20 28 70 6f  ed by a.  ** (po
2dcd8 73 73 69 62 6c 79 20 73 79 6e 63 68 72 6f 6e 6f  ssibly synchrono
2dcd9 75 73 29 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  us) transaction 
2dcda 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 6d 61  opened on the ma
2dcdb 69 6e 20 64 61 74 61 62 61 73 65 20 62 65 66 6f  in database befo
2dcdc 72 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 42  re.  ** sqlite3B
2dcdd 74 72 65 65 43 6f 70 79 46 69 6c 65 28 29 20 69  treeCopyFile() i
2dcde 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a 2a 0a 20  s called..  **. 
2dcdf 20 2a 2a 20 41 6e 20 6f 70 74 69 6d 69 73 61 74   ** An optimisat
2dce0 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20 74 6f 20  ion would be to 
2dce1 75 73 65 20 61 20 6e 6f 6e 2d 6a 6f 75 72 6e 61  use a non-journa
2dce2 6c 65 64 20 70 61 67 65 72 2e 0a 20 20 2a 2a 20  led pager..  ** 
2dce3 28 4c 61 74 65 72 3a 29 20 49 20 74 72 69 65 64  (Later:) I tried
2dce4 20 73 65 74 74 69 6e 67 20 22 50 52 41 47 4d 41   setting "PRAGMA
2dce5 20 76 61 63 75 75 6d 5f 64 62 2e 6a 6f 75 72 6e   vacuum_db.journ
2dce6 61 6c 5f 6d 6f 64 65 3d 4f 46 46 22 20 62 75 74  al_mode=OFF" but
2dce7 0a 20 20 2a 2a 20 74 68 61 74 20 61 63 74 75 61  .  ** that actua
2dce8 6c 6c 79 20 6d 61 64 65 20 74 68 65 20 56 41 43  lly made the VAC
2dce9 55 55 4d 20 72 75 6e 20 73 6c 6f 77 65 72 2e 20  UUM run slower. 
2dcea 20 56 65 72 79 20 6c 69 74 74 6c 65 20 6a 6f 75   Very little jou
2dceb 72 6e 61 6c 6c 69 6e 67 0a 20 20 2a 2a 20 61 63  rnalling.  ** ac
2dcec 74 75 61 6c 6c 79 20 6f 63 63 75 72 73 20 77 68  tually occurs wh
2dced 65 6e 20 64 6f 69 6e 67 20 61 20 76 61 63 75 75  en doing a vacuu
2dcee 6d 20 73 69 6e 63 65 20 74 68 65 20 76 61 63 75  m since the vacu
2dcef 75 6d 5f 64 62 20 69 73 20 69 6e 69 74 69 61 6c  um_db is initial
2dcf0 6c 79 0a 20 20 2a 2a 20 65 6d 70 74 79 2e 20 20  ly.  ** empty.  
2dcf1 4f 6e 6c 79 20 74 68 65 20 6a 6f 75 72 6e 61 6c  Only the journal
2dcf2 20 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74   header is writt
2dcf3 65 6e 2e 20 20 41 70 70 61 72 65 6e 74 6c 79 20  en.  Apparently 
2dcf4 69 74 20 74 61 6b 65 73 20 6d 6f 72 65 0a 20 20  it takes more.  
2dcf5 2a 2a 20 74 69 6d 65 20 74 6f 20 70 61 72 73 65  ** time to parse
2dcf6 20 61 6e 64 20 72 75 6e 20 74 68 65 20 50 52 41   and run the PRA
2dcf7 47 4d 41 20 74 6f 20 74 75 72 6e 20 6a 6f 75 72  GMA to turn jour
2dcf8 6e 61 6c 6c 69 6e 67 20 6f 66 66 20 74 68 61 6e  nalling off than
2dcf9 20 69 74 20 64 6f 65 73 0a 20 20 2a 2a 20 74 6f   it does.  ** to
2dcfa 20 77 72 69 74 65 20 74 68 65 20 6a 6f 75 72 6e   write the journ
2dcfb 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 2e 0a  al header file..
2dcfc 20 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 22 41    */.  zSql = "A
2dcfd 54 54 41 43 48 20 27 27 20 41 53 20 76 61 63 75  TTACH '' AS vacu
2dcfe 75 6d 5f 64 62 3b 22 3b 0a 20 20 72 63 20 3d 20  um_db;";.  rc = 
2dcff 65 78 65 63 53 71 6c 28 64 62 2c 20 7a 53 71 6c  execSql(db, zSql
2dd00 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2dd01 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
2dd02 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 70  d_of_vacuum;.  p
2dd03 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 64 62  Db = &db->aDb[db
2dd04 2d 3e 6e 44 62 2d 31 5d 3b 0a 20 20 61 73 73 65  ->nDb-1];.  asse
2dd05 72 74 28 20 73 74 72 63 6d 70 28 64 62 2d 3e 61  rt( strcmp(db->a
2dd06 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d 2e 7a 4e  Db[db->nDb-1].zN
2dd07 61 6d 65 2c 22 76 61 63 75 75 6d 5f 64 62 22 29  ame,"vacuum_db")
2dd08 3d 3d 30 20 29 3b 0a 20 20 70 54 65 6d 70 20 3d  ==0 );.  pTemp =
2dd09 20 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62   db->aDb[db->nDb
2dd0a 2d 31 5d 2e 70 42 74 3b 0a 0a 20 20 2f 2a 20 54  -1].pBt;..  /* T
2dd0b 68 65 20 63 61 6c 6c 20 74 6f 20 65 78 65 63 53  he call to execS
2dd0c 71 6c 28 29 20 74 6f 20 61 74 74 61 63 68 20 74  ql() to attach t
2dd0d 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65  he temp database
2dd0e 20 68 61 73 20 6c 65 66 74 20 74 68 65 20 66 69   has left the fi
2dd0f 6c 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64 20 28  le.  ** locked (
2dd10 61 73 20 74 68 65 72 65 20 77 61 73 20 6d 6f 72  as there was mor
2dd11 65 20 74 68 61 6e 20 6f 6e 65 20 61 63 74 69 76  e than one activ
2dd12 65 20 73 74 61 74 65 6d 65 6e 74 20 77 68 65 6e  e statement when
2dd13 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
2dd14 0a 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 74 68  .  ** to read th
2dd15 65 20 73 63 68 65 6d 61 20 77 61 73 20 63 6f 6e  e schema was con
2dd16 63 6c 75 64 65 64 2e 20 55 6e 6c 6f 63 6b 20 69  cluded. Unlock i
2dd17 74 20 68 65 72 65 20 73 6f 20 74 68 61 74 20 74  t here so that t
2dd18 68 69 73 20 64 6f 65 73 6e 27 74 0a 20 20 2a 2a  his doesn't.  **
2dd19 20 63 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 20   cause problems 
2dd1a 66 6f 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  for the call to 
2dd1b 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65  BtreeSetPageSize
2dd1c 28 29 20 62 65 6c 6f 77 2e 20 20 2a 2f 0a 20 20  () below.  */.  
2dd1d 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
2dd1e 69 74 28 70 54 65 6d 70 29 3b 0a 0a 20 20 6e 52  it(pTemp);..  nR
2dd1f 65 73 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  es = sqlite3Btre
2dd20 65 47 65 74 52 65 73 65 72 76 65 28 70 4d 61 69  eGetReserve(pMai
2dd21 6e 29 3b 0a 0a 20 20 2f 2a 20 41 20 56 41 43 55  n);..  /* A VACU
2dd22 55 4d 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65  UM cannot change
2dd23 20 74 68 65 20 70 61 67 65 73 69 7a 65 20 6f 66   the pagesize of
2dd24 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20 64 61   an encrypted da
2dd25 74 61 62 61 73 65 2e 20 2a 2f 0a 23 69 66 64 65  tabase. */.#ifde
2dd26 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  f SQLITE_HAS_COD
2dd27 45 43 0a 20 20 69 66 28 20 64 62 2d 3e 6e 65 78  EC.  if( db->nex
2dd28 74 50 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20  tPagesize ){.   
2dd29 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c   extern void sql
2dd2a 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28  ite3CodecGetKey(
2dd2b 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 2c 20 76  sqlite3*, int, v
2dd2c 6f 69 64 2a 2a 2c 20 69 6e 74 2a 29 3b 0a 20 20  oid**, int*);.  
2dd2d 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 20 20    int nKey;.    
2dd2e 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 20 20 20 20  char *zKey;.    
2dd2f 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b  sqlite3CodecGetK
2dd30 65 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a  ey(db, 0, (void*
2dd31 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b  *)&zKey, &nKey);
2dd32 0a 20 20 20 20 69 66 28 20 6e 4b 65 79 20 29 20  .    if( nKey ) 
2dd33 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65  db->nextPagesize
2dd34 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66   = 0;.  }.#endif
2dd35 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 42  ..  if( sqlite3B
2dd36 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28  treeSetPageSize(
2dd37 70 54 65 6d 70 2c 20 73 71 6c 69 74 65 33 42 74  pTemp, sqlite3Bt
2dd38 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
2dd39 4d 61 69 6e 29 2c 20 6e 52 65 73 2c 20 30 29 0a  Main), nRes, 0).
2dd3a 20 20 20 7c 7c 20 28 21 69 73 4d 65 6d 44 62 20     || (!isMemDb 
2dd3b 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  && sqlite3BtreeS
2dd3c 65 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70  etPageSize(pTemp
2dd3d 2c 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69  , db->nextPagesi
2dd3e 7a 65 2c 20 6e 52 65 73 2c 20 30 29 29 0a 20 20  ze, nRes, 0)).  
2dd3f 20 7c 7c 20 4e 45 56 45 52 28 64 62 2d 3e 6d 61   || NEVER(db->ma
2dd40 6c 6c 6f 63 46 61 69 6c 65 64 29 0a 20 20 29 7b  llocFailed).  ){
2dd41 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
2dd42 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f  _NOMEM;.    goto
2dd43 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
2dd44 20 20 7d 0a 20 20 72 63 20 3d 20 65 78 65 63 53    }.  rc = execS
2dd45 71 6c 28 64 62 2c 20 22 50 52 41 47 4d 41 20 76  ql(db, "PRAGMA v
2dd46 61 63 75 75 6d 5f 64 62 2e 73 79 6e 63 68 72 6f  acuum_db.synchro
2dd47 6e 6f 75 73 3d 4f 46 46 22 29 3b 0a 20 20 69 66  nous=OFF");.  if
2dd48 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2dd49 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  ){.    goto end_
2dd4a 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 0a  of_vacuum;.  }..
2dd4b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2dd4c 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
2dd4d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
2dd4e 41 75 74 6f 56 61 63 75 75 6d 28 70 54 65 6d 70  AutoVacuum(pTemp
2dd4f 2c 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61  , db->nextAutova
2dd50 63 3e 3d 30 20 3f 20 64 62 2d 3e 6e 65 78 74 41  c>=0 ? db->nextA
2dd51 75 74 6f 76 61 63 20 3a 0a 20 20 20 20 20 20 20  utovac :.       
2dd52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dd53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dd54 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
2dd55 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 4d  GetAutoVacuum(pM
2dd56 61 69 6e 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  ain));.#endif.. 
2dd57 20 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e   /* Begin a tran
2dd58 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 72 63 20  saction */.  rc 
2dd59 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 20 22 42  = execSql(db, "B
2dd5a 45 47 49 4e 20 45 58 43 4c 55 53 49 56 45 3b 22  EGIN EXCLUSIVE;"
2dd5b 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2dd5c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
2dd5d 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20  d_of_vacuum;..  
2dd5e 2f 2a 20 51 75 65 72 79 20 74 68 65 20 73 63 68  /* Query the sch
2dd5f 65 6d 61 20 6f 66 20 74 68 65 20 6d 61 69 6e 20  ema of the main 
2dd60 64 61 74 61 62 61 73 65 2e 20 43 72 65 61 74 65  database. Create
2dd61 20 61 20 6d 69 72 72 6f 72 20 73 63 68 65 6d 61   a mirror schema
2dd62 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 74 65 6d  .  ** in the tem
2dd63 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2e  porary database.
2dd64 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65  .  */.  rc = exe
2dd65 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20  cExecSql(db, .  
2dd66 20 20 20 20 22 53 45 4c 45 43 54 20 27 43 52 45      "SELECT 'CRE
2dd67 41 54 45 20 54 41 42 4c 45 20 76 61 63 75 75 6d  ATE TABLE vacuum
2dd68 5f 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28  _db.' || substr(
2dd69 73 71 6c 2c 31 34 29 20 22 0a 20 20 20 20 20 20  sql,14) ".      
2dd6a 22 20 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d  "  FROM sqlite_m
2dd6b 61 73 74 65 72 20 57 48 45 52 45 20 74 79 70 65  aster WHERE type
2dd6c 3d 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d  ='table' AND nam
2dd6d 65 21 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65  e!='sqlite_seque
2dd6e 6e 63 65 27 22 0a 20 20 20 20 20 20 22 20 20 20  nce'".      "   
2dd6f 41 4e 44 20 72 6f 6f 74 70 61 67 65 3e 30 22 0a  AND rootpage>0".
2dd70 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53    );.  if( rc!=S
2dd71 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
2dd72 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2dd73 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71   rc = execExecSq
2dd74 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45  l(db, .      "SE
2dd75 4c 45 43 54 20 27 43 52 45 41 54 45 20 49 4e 44  LECT 'CREATE IND
2dd76 45 58 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c  EX vacuum_db.' |
2dd77 7c 20 73 75 62 73 74 72 28 73 71 6c 2c 31 34 29  | substr(sql,14)
2dd78 22 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20  ".      "  FROM 
2dd79 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48  sqlite_master WH
2dd7a 45 52 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52  ERE sql LIKE 'CR
2dd7b 45 41 54 45 20 49 4e 44 45 58 20 25 27 20 22 29  EATE INDEX %' ")
2dd7c 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2dd7d 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
2dd7e 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63  _of_vacuum;.  rc
2dd7f 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64   = execExecSql(d
2dd80 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43  b, .      "SELEC
2dd81 54 20 27 43 52 45 41 54 45 20 55 4e 49 51 55 45  T 'CREATE UNIQUE
2dd82 20 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62   INDEX vacuum_db
2dd83 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c  .' || substr(sql
2dd84 2c 32 31 29 20 22 0a 20 20 20 20 20 20 22 20 20  ,21) ".      "  
2dd85 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74  FROM sqlite_mast
2dd86 65 72 20 57 48 45 52 45 20 73 71 6c 20 4c 49 4b  er WHERE sql LIK
2dd87 45 20 27 43 52 45 41 54 45 20 55 4e 49 51 55 45  E 'CREATE UNIQUE
2dd88 20 49 4e 44 45 58 20 25 27 22 29 3b 0a 20 20 69   INDEX %'");.  i
2dd89 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2dd8a 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76   ) goto end_of_v
2dd8b 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f  acuum;..  /* Loo
2dd8c 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61  p through the ta
2dd8d 62 6c 65 73 20 69 6e 20 74 68 65 20 6d 61 69 6e  bles in the main
2dd8e 20 64 61 74 61 62 61 73 65 2e 20 46 6f 72 20 65   database. For e
2dd8f 61 63 68 2c 20 64 6f 0a 20 20 2a 2a 20 61 6e 20  ach, do.  ** an 
2dd90 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63  "INSERT INTO vac
2dd91 75 75 6d 5f 64 62 2e 78 78 78 20 53 45 4c 45 43  uum_db.xxx SELEC
2dd92 54 20 2a 20 46 52 4f 4d 20 6d 61 69 6e 2e 78 78  T * FROM main.xx
2dd93 78 3b 22 20 74 6f 20 63 6f 70 79 0a 20 20 2a 2a  x;" to copy.  **
2dd94 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 74 6f   the contents to
2dd95 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64   the temporary d
2dd96 61 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20  atabase..  */.  
2dd97 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c  rc = execExecSql
2dd98 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c  (db, .      "SEL
2dd99 45 43 54 20 27 49 4e 53 45 52 54 20 49 4e 54 4f  ECT 'INSERT INTO
2dd9a 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20   vacuum_db.' || 
2dd9b 71 75 6f 74 65 28 6e 61 6d 65 29 20 22 0a 20 20  quote(name) ".  
2dd9c 20 20 20 20 22 7c 7c 20 27 20 53 45 4c 45 43 54      "|| ' SELECT
2dd9d 20 2a 20 46 52 4f 4d 20 6d 61 69 6e 2e 27 20 7c   * FROM main.' |
2dd9e 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c  | quote(name) ||
2dd9f 20 27 3b 27 22 0a 20 20 20 20 20 20 22 46 52 4f   ';'".      "FRO
2dda0 4d 20 6d 61 69 6e 2e 73 71 6c 69 74 65 5f 6d 61  M main.sqlite_ma
2dda1 73 74 65 72 20 22 0a 20 20 20 20 20 20 22 57 48  ster ".      "WH
2dda2 45 52 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c  ERE type = 'tabl
2dda3 65 27 20 41 4e 44 20 6e 61 6d 65 21 3d 27 73 71  e' AND name!='sq
2dda4 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27 20 22  lite_sequence' "
2dda5 0a 20 20 20 20 20 20 22 20 20 41 4e 44 20 72 6f  .      "  AND ro
2dda6 6f 74 70 61 67 65 3e 30 22 0a 0a 20 20 29 3b 0a  otpage>0"..  );.
2dda7 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2dda8 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2dda9 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20  f_vacuum;..  /* 
2ddaa 43 6f 70 79 20 6f 76 65 72 20 74 68 65 20 73 65  Copy over the se
2ddab 71 75 65 6e 63 65 20 74 61 62 6c 65 0a 20 20 2a  quence table.  *
2ddac 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  /.  rc = execExe
2ddad 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
2ddae 22 53 45 4c 45 43 54 20 27 44 45 4c 45 54 45 20  "SELECT 'DELETE 
2ddaf 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 27  FROM vacuum_db.'
2ddb0 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20   || quote(name) 
2ddb1 7c 7c 20 27 3b 27 20 22 0a 20 20 20 20 20 20 22  || ';' ".      "
2ddb2 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73  FROM vacuum_db.s
2ddb3 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
2ddb4 52 45 20 6e 61 6d 65 3d 27 73 71 6c 69 74 65 5f  RE name='sqlite_
2ddb5 73 65 71 75 65 6e 63 65 27 20 22 0a 20 20 29 3b  sequence' ".  );
2ddb6 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2ddb7 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
2ddb8 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20  of_vacuum;.  rc 
2ddb9 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62  = execExecSql(db
2ddba 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54  , .      "SELECT
2ddbb 20 27 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61   'INSERT INTO va
2ddbc 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f  cuum_db.' || quo
2ddbd 74 65 28 6e 61 6d 65 29 20 22 0a 20 20 20 20 20  te(name) ".     
2ddbe 20 22 7c 7c 20 27 20 53 45 4c 45 43 54 20 2a 20   "|| ' SELECT * 
2ddbf 46 52 4f 4d 20 6d 61 69 6e 2e 27 20 7c 7c 20 71  FROM main.' || q
2ddc0 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b  uote(name) || ';
2ddc1 27 20 22 0a 20 20 20 20 20 20 22 46 52 4f 4d 20  ' ".      "FROM 
2ddc2 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65  vacuum_db.sqlite
2ddc3 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20 6e 61  _master WHERE na
2ddc4 6d 65 3d 3d 27 73 71 6c 69 74 65 5f 73 65 71 75  me=='sqlite_sequ
2ddc5 65 6e 63 65 27 3b 22 0a 20 20 29 3b 0a 20 20 69  ence';".  );.  i
2ddc6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2ddc7 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76   ) goto end_of_v
2ddc8 61 63 75 75 6d 3b 0a 0a 0a 20 20 2f 2a 20 43 6f  acuum;...  /* Co
2ddc9 70 79 20 74 68 65 20 74 72 69 67 67 65 72 73 2c  py the triggers,
2ddca 20 76 69 65 77 73 2c 20 61 6e 64 20 76 69 72 74   views, and virt
2ddcb 75 61 6c 20 74 61 62 6c 65 73 20 66 72 6f 6d 20  ual tables from 
2ddcc 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
2ddcd 65 0a 20 20 2a 2a 20 6f 76 65 72 20 74 6f 20 74  e.  ** over to t
2ddce 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74  he temporary dat
2ddcf 61 62 61 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20  abase.  None of 
2ddd0 74 68 65 73 65 20 6f 62 6a 65 63 74 73 20 68 61  these objects ha
2ddd1 73 20 61 6e 79 0a 20 20 2a 2a 20 61 73 73 6f 63  s any.  ** assoc
2ddd2 69 61 74 65 64 20 73 74 6f 72 61 67 65 2c 20 73  iated storage, s
2ddd3 6f 20 61 6c 6c 20 77 65 20 68 61 76 65 20 74 6f  o all we have to
2ddd4 20 64 6f 20 69 73 20 63 6f 70 79 20 74 68 65 69   do is copy thei
2ddd5 72 20 65 6e 74 72 69 65 73 0a 20 20 2a 2a 20 66  r entries.  ** f
2ddd6 72 6f 6d 20 74 68 65 20 53 51 4c 49 54 45 5f 4d  rom the SQLITE_M
2ddd7 41 53 54 45 52 20 74 61 62 6c 65 2e 0a 20 20 2a  ASTER table..  *
2ddd8 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c  /.  rc = execSql
2ddd9 28 64 62 2c 0a 20 20 20 20 20 20 22 49 4e 53 45  (db,.      "INSE
2ddda 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64  RT INTO vacuum_d
2dddb 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  b.sqlite_master 
2dddc 22 0a 20 20 20 20 20 20 22 20 20 53 45 4c 45 43  ".      "  SELEC
2dddd 54 20 74 79 70 65 2c 20 6e 61 6d 65 2c 20 74 62  T type, name, tb
2ddde 6c 5f 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65  l_name, rootpage
2dddf 2c 20 73 71 6c 22 0a 20 20 20 20 20 20 22 20 20  , sql".      "  
2dde0 20 20 46 52 4f 4d 20 6d 61 69 6e 2e 73 71 6c 69    FROM main.sqli
2dde1 74 65 5f 6d 61 73 74 65 72 22 0a 20 20 20 20 20  te_master".     
2dde2 20 22 20 20 20 57 48 45 52 45 20 74 79 70 65 3d   "   WHERE type=
2dde3 27 76 69 65 77 27 20 4f 52 20 74 79 70 65 3d 27  'view' OR type='
2dde4 74 72 69 67 67 65 72 27 22 0a 20 20 20 20 20 20  trigger'".      
2dde5 22 20 20 20 20 20 20 4f 52 20 28 74 79 70 65 3d  "      OR (type=
2dde6 27 74 61 62 6c 65 27 20 41 4e 44 20 72 6f 6f 74  'table' AND root
2dde7 70 61 67 65 3d 30 29 22 0a 20 20 29 3b 0a 20 20  page=0)".  );.  
2dde8 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e  if( rc ) goto en
2dde9 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20  d_of_vacuum;..  
2ddea 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
2ddeb 2c 20 75 6e 6c 65 73 73 20 74 68 65 20 6d 61 69  , unless the mai
2ddec 6e 20 64 62 20 77 61 73 20 63 6f 6d 70 6c 65 74  n db was complet
2dded 65 6c 79 20 65 6d 70 74 79 2c 20 74 68 65 72 65  ely empty, there
2ddee 20 69 73 20 6e 6f 77 20 61 0a 20 20 2a 2a 20 74   is now a.  ** t
2ddef 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20  ransaction open 
2ddf0 6f 6e 20 74 68 65 20 76 61 63 75 75 6d 20 64 61  on the vacuum da
2ddf1 74 61 62 61 73 65 2c 20 62 75 74 20 6e 6f 74 20  tabase, but not 
2ddf2 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  on the main data
2ddf3 62 61 73 65 2e 0a 20 20 2a 2a 20 4f 70 65 6e 20  base..  ** Open 
2ddf4 61 20 62 74 72 65 65 20 6c 65 76 65 6c 20 74 72  a btree level tr
2ddf5 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65  ansaction on the
2ddf6 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20   main database. 
2ddf7 54 68 69 73 20 61 6c 6c 6f 77 73 20 61 0a 20 20  This allows a.  
2ddf8 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ** call to sqlit
2ddf9 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65 28  e3BtreeCopyFile(
2ddfa 29 2e 20 54 68 65 20 6d 61 69 6e 20 64 61 74 61  ). The main data
2ddfb 62 61 73 65 20 62 74 72 65 65 20 6c 65 76 65 6c  base btree level
2ddfc 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  .  ** transactio
2ddfd 6e 20 69 73 20 74 68 65 6e 20 63 6f 6d 6d 69 74  n is then commit
2ddfe 74 65 64 2c 20 73 6f 20 74 68 65 20 53 51 4c 20  ted, so the SQL 
2ddff 6c 65 76 65 6c 20 6e 65 76 65 72 20 6b 6e 6f 77  level never know
2de00 73 20 69 74 20 77 61 73 0a 20 20 2a 2a 20 6f 70  s it was.  ** op
2de01 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67  ened for writing
2de02 2e 20 54 68 69 73 20 77 61 79 2c 20 74 68 65 20  . This way, the 
2de03 53 51 4c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  SQL transaction 
2de04 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74  used to create t
2de05 68 65 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72  he.  ** temporar
2de06 79 20 64 61 74 61 62 61 73 65 20 6e 65 76 65 72  y database never
2de07 20 6e 65 65 64 73 20 74 6f 20 62 65 20 63 6f 6d   needs to be com
2de08 6d 69 74 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 7b  mitted..  */.  {
2de09 0a 20 20 20 20 75 33 32 20 6d 65 74 61 3b 0a 20  .    u32 meta;. 
2de0a 20 20 20 69 6e 74 20 69 3b 0a 0a 20 20 20 20 2f     int i;..    /
2de0b 2a 20 54 68 69 73 20 61 72 72 61 79 20 64 65 74  * This array det
2de0c 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20 6d 65  ermines which me
2de0d 74 61 20 6d 65 74 61 20 76 61 6c 75 65 73 20 61  ta meta values a
2de0e 72 65 20 70 72 65 73 65 72 76 65 64 20 69 6e 20  re preserved in 
2de0f 74 68 65 0a 20 20 20 20 2a 2a 20 76 61 63 75 75  the.    ** vacuu
2de10 6d 2e 20 20 45 76 65 6e 20 65 6e 74 72 69 65 73  m.  Even entries
2de11 20 61 72 65 20 74 68 65 20 6d 65 74 61 20 76 61   are the meta va
2de12 6c 75 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 6f  lue number and o
2de13 64 64 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a  dd entries.    *
2de14 2a 20 61 72 65 20 61 6e 20 69 6e 63 72 65 6d 65  * are an increme
2de15 6e 74 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 74  nt to apply to t
2de16 68 65 20 6d 65 74 61 20 76 61 6c 75 65 20 61 66  he meta value af
2de17 74 65 72 20 74 68 65 20 76 61 63 75 75 6d 2e 0a  ter the vacuum..
2de18 20 20 20 20 2a 2a 20 54 68 65 20 69 6e 63 72 65      ** The incre
2de19 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20  ment is used to 
2de1a 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 63 68  increase the sch
2de1b 65 6d 61 20 63 6f 6f 6b 69 65 20 73 6f 20 74 68  ema cookie so th
2de1c 61 74 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20  at other.    ** 
2de1d 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74  connections to t
2de1e 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
2de1f 20 77 69 6c 6c 20 6b 6e 6f 77 20 74 6f 20 72 65   will know to re
2de20 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 2e  read the schema.
2de21 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 61 74  .    */.    stat
2de22 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
2de23 64 20 63 68 61 72 20 61 43 6f 70 79 5b 5d 20 3d  d char aCopy[] =
2de24 20 7b 0a 20 20 20 20 20 20 20 42 54 52 45 45 5f   {.       BTREE_
2de25 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20  SCHEMA_VERSION, 
2de26 20 20 20 20 31 2c 20 20 2f 2a 20 41 64 64 20 6f      1,  /* Add o
2de27 6e 65 20 74 6f 20 74 68 65 20 6f 6c 64 20 73 63  ne to the old sc
2de28 68 65 6d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 20  hema cookie */. 
2de29 20 20 20 20 20 20 42 54 52 45 45 5f 44 45 46 41        BTREE_DEFA
2de2a 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20  ULT_CACHE_SIZE, 
2de2b 30 2c 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20  0,  /* Preserve 
2de2c 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65  the default page
2de2d 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20   cache size */. 
2de2e 20 20 20 20 20 20 42 54 52 45 45 5f 54 45 58 54        BTREE_TEXT
2de2f 5f 45 4e 43 4f 44 49 4e 47 2c 20 20 20 20 20 20  _ENCODING,      
2de30 30 2c 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20  0,  /* Preserve 
2de31 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  the text encodin
2de32 67 20 2a 2f 0a 20 20 20 20 20 20 20 42 54 52 45  g */.       BTRE
2de33 45 5f 55 53 45 52 5f 56 45 52 53 49 4f 4e 2c 20  E_USER_VERSION, 
2de34 20 20 20 20 20 20 30 2c 20 20 2f 2a 20 50 72 65        0,  /* Pre
2de35 73 65 72 76 65 20 74 68 65 20 75 73 65 72 20 76  serve the user v
2de36 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 7d 3b  ersion */.    };
2de37 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 31 3d  ..    assert( 1=
2de38 3d 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49  =sqlite3BtreeIsI
2de39 6e 54 72 61 6e 73 28 70 54 65 6d 70 29 20 29 3b  nTrans(pTemp) );
2de3a 0a 20 20 20 20 61 73 73 65 72 74 28 20 31 3d 3d  .    assert( 1==
2de3b 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
2de3c 54 72 61 6e 73 28 70 4d 61 69 6e 29 20 29 3b 0a  Trans(pMain) );.
2de3d 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20 42 74 72  .    /* Copy Btr
2de3e 65 65 20 6d 65 74 61 20 76 61 6c 75 65 73 20 2a  ee meta values *
2de3f 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  /.    for(i=0; i
2de40 3c 41 72 72 61 79 53 69 7a 65 28 61 43 6f 70 79  <ArraySize(aCopy
2de41 29 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20  ); i+=2){.      
2de42 2f 2a 20 47 65 74 4d 65 74 61 28 29 20 61 6e 64  /* GetMeta() and
2de43 20 55 70 64 61 74 65 4d 65 74 61 28 29 20 63 61   UpdateMeta() ca
2de44 6e 6e 6f 74 20 66 61 69 6c 20 69 6e 20 74 68 69  nnot fail in thi
2de45 73 20 63 6f 6e 74 65 78 74 20 62 65 63 61 75 73  s context becaus
2de46 65 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 61 6c  e.      ** we al
2de47 72 65 61 64 79 20 68 61 76 65 20 70 61 67 65 20  ready have page 
2de48 31 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 63 61  1 loaded into ca
2de49 63 68 65 20 61 6e 64 20 6d 61 72 6b 65 64 20 64  che and marked d
2de4a 69 72 74 79 2e 20 2a 2f 0a 20 20 20 20 20 20 73  irty. */.      s
2de4b 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65  qlite3BtreeGetMe
2de4c 74 61 28 70 4d 61 69 6e 2c 20 61 43 6f 70 79 5b  ta(pMain, aCopy[
2de4d 69 5d 2c 20 26 6d 65 74 61 29 3b 0a 20 20 20 20  i], &meta);.    
2de4e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
2de4f 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70 54  reeUpdateMeta(pT
2de50 65 6d 70 2c 20 61 43 6f 70 79 5b 69 5d 2c 20 6d  emp, aCopy[i], m
2de51 65 74 61 2b 61 43 6f 70 79 5b 69 2b 31 5d 29 3b  eta+aCopy[i+1]);
2de52 0a 20 20 20 20 20 20 69 66 28 20 4e 45 56 45 52  .      if( NEVER
2de53 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20  (rc!=SQLITE_OK) 
2de54 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
2de55 63 75 75 6d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  cuum;.    }..   
2de56 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
2de57 65 65 43 6f 70 79 46 69 6c 65 28 70 4d 61 69 6e  eeCopyFile(pMain
2de58 2c 20 70 54 65 6d 70 29 3b 0a 20 20 20 20 69 66  , pTemp);.    if
2de59 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2de5a 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
2de5b 63 75 75 6d 3b 0a 20 20 20 20 72 63 20 3d 20 73  cuum;.    rc = s
2de5c 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
2de5d 74 28 70 54 65 6d 70 29 3b 0a 20 20 20 20 69 66  t(pTemp);.    if
2de5e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2de5f 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
2de60 63 75 75 6d 3b 0a 23 69 66 6e 64 65 66 20 53 51  cuum;.#ifndef SQ
2de61 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
2de62 43 55 55 4d 0a 20 20 20 20 73 71 6c 69 74 65 33  CUUM.    sqlite3
2de63 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63 75  BtreeSetAutoVacu
2de64 75 6d 28 70 4d 61 69 6e 2c 20 73 71 6c 69 74 65  um(pMain, sqlite
2de65 33 42 74 72 65 65 47 65 74 41 75 74 6f 56 61 63  3BtreeGetAutoVac
2de66 75 75 6d 28 70 54 65 6d 70 29 29 3b 0a 23 65 6e  uum(pTemp));.#en
2de67 64 69 66 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  dif.  }..  asser
2de68 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
2de69 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
2de6a 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69  e3BtreeSetPageSi
2de6b 7a 65 28 70 4d 61 69 6e 2c 20 73 71 6c 69 74 65  ze(pMain, sqlite
2de6c 33 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a  3BtreeGetPageSiz
2de6d 65 28 70 54 65 6d 70 29 2c 20 6e 52 65 73 2c 31  e(pTemp), nRes,1
2de6e 29 3b 0a 0a 65 6e 64 5f 6f 66 5f 76 61 63 75 75  );..end_of_vacuu
2de6f 6d 3a 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20  m:.  /* Restore 
2de70 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c  the original val
2de71 75 65 20 6f 66 20 64 62 2d 3e 66 6c 61 67 73 20  ue of db->flags 
2de72 2a 2f 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d  */.  db->flags =
2de73 20 73 61 76 65 64 5f 66 6c 61 67 73 3b 0a 20 20   saved_flags;.  
2de74 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 73 61  db->nChange = sa
2de75 76 65 64 5f 6e 43 68 61 6e 67 65 3b 0a 20 20 64  ved_nChange;.  d
2de76 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20  b->nTotalChange 
2de77 3d 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c 43 68  = saved_nTotalCh
2de78 61 6e 67 65 3b 0a 0a 20 20 2f 2a 20 43 75 72 72  ange;..  /* Curr
2de79 65 6e 74 6c 79 20 74 68 65 72 65 20 69 73 20 61  ently there is a
2de7a 6e 20 53 51 4c 20 6c 65 76 65 6c 20 74 72 61 6e  n SQL level tran
2de7b 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20  saction open on 
2de7c 74 68 65 20 76 61 63 75 75 6d 0a 20 20 2a 2a 20  the vacuum.  ** 
2de7d 64 61 74 61 62 61 73 65 2e 20 4e 6f 20 6c 6f 63  database. No loc
2de7e 6b 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 61  ks are held on a
2de7f 6e 79 20 6f 74 68 65 72 20 66 69 6c 65 73 20 28  ny other files (
2de80 73 69 6e 63 65 20 74 68 65 20 6d 61 69 6e 20 66  since the main f
2de81 69 6c 65 0a 20 20 2a 2a 20 77 61 73 20 63 6f 6d  ile.  ** was com
2de82 6d 69 74 74 65 64 20 61 74 20 74 68 65 20 62 74  mitted at the bt
2de83 72 65 65 20 6c 65 76 65 6c 29 2e 20 53 6f 20 69  ree level). So i
2de84 74 20 73 61 66 65 20 74 6f 20 65 6e 64 20 74 68  t safe to end th
2de85 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  e transaction.  
2de86 2a 2a 20 62 79 20 6d 61 6e 75 61 6c 6c 79 20 73  ** by manually s
2de87 65 74 74 69 6e 67 20 74 68 65 20 61 75 74 6f 43  etting the autoC
2de88 6f 6d 6d 69 74 20 66 6c 61 67 20 74 6f 20 74 72  ommit flag to tr
2de89 75 65 20 61 6e 64 20 64 65 74 61 63 68 69 6e 67  ue and detaching
2de8a 20 74 68 65 0a 20 20 2a 2a 20 76 61 63 75 75 6d   the.  ** vacuum
2de8b 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20 76   database. The v
2de8c 61 63 75 75 6d 5f 64 62 20 6a 6f 75 72 6e 61 6c  acuum_db journal
2de8d 20 66 69 6c 65 20 69 73 20 64 65 6c 65 74 65 64   file is deleted
2de8e 20 77 68 65 6e 20 74 68 65 20 70 61 67 65 72 0a   when the pager.
2de8f 20 20 2a 2a 20 69 73 20 63 6c 6f 73 65 64 20 62    ** is closed b
2de90 79 20 74 68 65 20 44 45 54 41 43 48 2e 0a 20 20  y the DETACH..  
2de91 2a 2f 0a 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d  */.  db->autoCom
2de92 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20  mit = 1;..  if( 
2de93 70 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  pDb ){.    sqlit
2de94 65 33 42 74 72 65 65 43 6c 6f 73 65 28 70 44 62  e3BtreeClose(pDb
2de95 2d 3e 70 42 74 29 3b 0a 20 20 20 20 70 44 62 2d  ->pBt);.    pDb-
2de96 3e 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 70 44  >pBt = 0;.    pD
2de97 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a  b->pSchema = 0;.
2de98 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 52 65    }..  sqlite3Re
2de99 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
2de9a 61 28 64 62 2c 20 30 29 3b 0a 0a 20 20 72 65 74  a(db, 0);..  ret
2de9b 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
2de9c 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54    /* SQLITE_OMIT
2de9d 5f 56 41 43 55 55 4d 20 26 26 20 53 51 4c 49 54  _VACUUM && SQLIT
2de9e 45 5f 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f  E_OMIT_ATTACH */
2de9f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2dea0 2a 20 45 6e 64 20 6f 66 20 76 61 63 75 75 6d 2e  * End of vacuum.
2dea1 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2dea2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dea3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dea4 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
2dea5 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76 74 61  * Begin file vta
2dea6 62 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  b.c ************
2dea7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dea8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dea9 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e  /./*.** 2006 Jun
2deaa 65 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  e 10.**.** The a
2deab 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
2deac 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
2dead 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
2deae 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
2deaf 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2deb0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2deb1 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2deb2 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2deb3 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
2deb4 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2deb5 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2deb6 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2deb7 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2deb8 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2deb9 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2deba 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2debb 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
2debc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2debd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2debe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2debf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dec0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
2dec1 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75   contains code u
2dec2 73 65 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c  sed to help impl
2dec3 65 6d 65 6e 74 20 76 69 72 74 75 61 6c 20 74 61  ement virtual ta
2dec4 62 6c 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  bles..*/.#ifndef
2dec5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2dec6 54 55 41 4c 54 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a  TUALTABLE../*.**
2dec7 20 54 68 65 20 61 63 74 75 61 6c 20 66 75 6e 63   The actual func
2dec8 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20 74  tion that does t
2dec9 68 65 20 77 6f 72 6b 20 6f 66 20 63 72 65 61 74  he work of creat
2deca 69 6e 67 20 61 20 6e 65 77 20 6d 6f 64 75 6c 65  ing a new module
2decb 2e 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ..** This functi
2decc 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  on implements th
2decd 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
2dece 5f 6d 6f 64 75 6c 65 28 29 20 61 6e 64 0a 2a 2a  _module() and.**
2decf 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2ded0 6d 6f 64 75 6c 65 5f 76 32 28 29 20 69 6e 74 65  module_v2() inte
2ded1 72 66 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  rfaces..*/.stati
2ded2 63 20 69 6e 74 20 63 72 65 61 74 65 4d 6f 64 75  c int createModu
2ded3 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  le(.  sqlite3 *d
2ded4 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
2ded5 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2ded6 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c  e in which modul
2ded7 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64 20  e is registered 
2ded8 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2ded9 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
2deda 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73       /* Name ass
2dedb 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f  igned to this mo
2dedc 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  dule */.  const 
2dedd 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
2dede 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65  pModule,  /* The
2dedf 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74   definition of t
2dee0 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76  he module */.  v
2dee1 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20 20 20  oid *pAux,      
2dee2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2dee3 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65  * Context pointe
2dee4 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43  r for xCreate/xC
2dee5 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64  onnect */.  void
2dee6 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69   (*xDestroy)(voi
2dee7 64 20 2a 29 20 20 20 20 20 20 20 20 2f 2a 20 4d  d *)        /* M
2dee8 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f 72  odule destructor
2dee9 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a   function */.){.
2deea 20 20 69 6e 74 20 72 63 2c 20 6e 4e 61 6d 65 3b    int rc, nName;
2deeb 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b  .  Module *pMod;
2deec 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
2deed 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
2deee 78 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71  x);.  nName = sq
2deef 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
2def0 61 6d 65 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28  ame);.  pMod = (
2def1 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33  Module *)sqlite3
2def2 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
2def3 73 69 7a 65 6f 66 28 4d 6f 64 75 6c 65 29 20 2b  sizeof(Module) +
2def4 20 6e 4e 61 6d 65 20 2b 20 31 29 3b 0a 20 20 69   nName + 1);.  i
2def5 66 28 20 70 4d 6f 64 20 29 7b 0a 20 20 20 20 4d  f( pMod ){.    M
2def6 6f 64 75 6c 65 20 2a 70 44 65 6c 3b 0a 20 20 20  odule *pDel;.   
2def7 20 63 68 61 72 20 2a 7a 43 6f 70 79 20 3d 20 28   char *zCopy = (
2def8 63 68 61 72 20 2a 29 28 26 70 4d 6f 64 5b 31 5d  char *)(&pMod[1]
2def9 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 43  );.    memcpy(zC
2defa 6f 70 79 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  opy, zName, nNam
2defb 65 2b 31 29 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e  e+1);.    pMod->
2defc 7a 4e 61 6d 65 20 3d 20 7a 43 6f 70 79 3b 0a 20  zName = zCopy;. 
2defd 20 20 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65     pMod->pModule
2defe 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20   = pModule;.    
2deff 70 4d 6f 64 2d 3e 70 41 75 78 20 3d 20 70 41 75  pMod->pAux = pAu
2df00 78 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 78 44 65  x;.    pMod->xDe
2df01 73 74 72 6f 79 20 3d 20 78 44 65 73 74 72 6f 79  stroy = xDestroy
2df02 3b 0a 20 20 20 20 70 44 65 6c 20 3d 20 28 4d 6f  ;.    pDel = (Mo
2df03 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 48 61  dule *)sqlite3Ha
2df04 73 68 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 4d  shInsert(&db->aM
2df05 6f 64 75 6c 65 2c 20 7a 43 6f 70 79 2c 20 6e 4e  odule, zCopy, nN
2df06 61 6d 65 2c 20 28 76 6f 69 64 2a 29 70 4d 6f 64  ame, (void*)pMod
2df07 29 3b 0a 20 20 20 20 69 66 28 20 70 44 65 6c 20  );.    if( pDel 
2df08 26 26 20 70 44 65 6c 2d 3e 78 44 65 73 74 72 6f  && pDel->xDestro
2df09 79 20 29 7b 0a 20 20 20 20 20 20 70 44 65 6c 2d  y ){.      pDel-
2df0a 3e 78 44 65 73 74 72 6f 79 28 70 44 65 6c 2d 3e  >xDestroy(pDel->
2df0b 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pAux);.    }.   
2df0c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2df0d 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 69 66  b, pDel);.    if
2df0e 28 20 70 44 65 6c 3d 3d 70 4d 6f 64 20 29 7b 0a  ( pDel==pMod ){.
2df0f 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
2df10 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
2df11 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  }.    sqlite3Res
2df12 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
2df13 28 64 62 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  (db, 0);.  }else
2df14 20 69 66 28 20 78 44 65 73 74 72 6f 79 20 29 7b   if( xDestroy ){
2df15 0a 20 20 20 20 78 44 65 73 74 72 6f 79 28 70 41  .    xDestroy(pA
2df16 75 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  ux);.  }.  rc = 
2df17 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
2df18 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20  b, SQLITE_OK);. 
2df19 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2df1a 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2df1b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2df1c 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c  ../*.** External
2df1d 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75 73   API function us
2df1e 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  ed to create a n
2df1f 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  ew virtual-table
2df20 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   module..*/.SQLI
2df21 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2df22 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
2df23 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
2df24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2df25 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2df26 69 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20  in which module 
2df27 69 73 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f  is registered */
2df28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2df29 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20  Name,           
2df2a 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67     /* Name assig
2df2b 6e 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75  ned to this modu
2df2c 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71  le */.  const sq
2df2d 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
2df2e 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64  odule,  /* The d
2df2f 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65  efinition of the
2df30 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69   module */.  voi
2df31 64 20 2a 70 41 75 78 20 20 20 20 20 20 20 20 20  d *pAux         
2df32 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2df33 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20  Context pointer 
2df34 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e  for xCreate/xCon
2df35 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20 20 72 65 74  nect */.){.  ret
2df36 75 72 6e 20 63 72 65 61 74 65 4d 6f 64 75 6c 65  urn createModule
2df37 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 4d 6f 64  (db, zName, pMod
2df38 75 6c 65 2c 20 70 41 75 78 2c 20 30 29 3b 0a 7d  ule, pAux, 0);.}
2df39 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65 72 6e 61 6c  ../*.** External
2df3a 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 75 73   API function us
2df3b 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e  ed to create a n
2df3c 65 77 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  ew virtual-table
2df3d 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   module..*/.SQLI
2df3e 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2df3f 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
2df40 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  _v2(.  sqlite3 *
2df41 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2df42 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2df43 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64 75  se in which modu
2df44 6c 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64  le is registered
2df45 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2df46 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
2df47 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61 73        /* Name as
2df48 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20 6d  signed to this m
2df49 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  odule */.  const
2df4a 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
2df4b 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54 68  *pModule,  /* Th
2df4c 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  e definition of 
2df4d 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
2df4e 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20 20 20  void *pAux,     
2df4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2df50 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74  /* Context point
2df51 65 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78  er for xCreate/x
2df52 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69  Connect */.  voi
2df53 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76 6f  d (*xDestroy)(vo
2df54 69 64 20 2a 29 20 20 20 20 20 20 20 20 2f 2a 20  id *)        /* 
2df55 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63 74 6f  Module destructo
2df56 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  r function */.){
2df57 0a 20 20 72 65 74 75 72 6e 20 63 72 65 61 74 65  .  return create
2df58 4d 6f 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d 65  Module(db, zName
2df59 2c 20 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78 2c  , pModule, pAux,
2df5a 20 78 44 65 73 74 72 6f 79 29 3b 0a 7d 0a 0a 2f   xDestroy);.}../
2df5b 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 76 69  *.** Lock the vi
2df5c 72 74 75 61 6c 20 74 61 62 6c 65 20 73 6f 20 74  rtual table so t
2df5d 68 61 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65  hat it cannot be
2df5e 20 64 69 73 63 6f 6e 6e 65 63 74 65 64 2e 0a 2a   disconnected..*
2df5f 2a 20 4c 6f 63 6b 73 20 6e 65 73 74 2e 20 20 45  * Locks nest.  E
2df60 76 65 72 79 20 6c 6f 63 6b 20 73 68 6f 75 6c 64  very lock should
2df61 20 68 61 76 65 20 61 20 63 6f 72 72 65 73 70 6f   have a correspo
2df62 6e 64 69 6e 67 20 75 6e 6c 6f 63 6b 2e 0a 2a 2a  nding unlock..**
2df63 20 49 66 20 61 6e 20 75 6e 6c 6f 63 6b 20 69 73   If an unlock is
2df64 20 6f 6d 69 74 74 65 64 2c 20 72 65 73 6f 75 72   omitted, resour
2df65 63 65 73 20 6c 65 61 6b 73 20 77 69 6c 6c 20 6f  ces leaks will o
2df66 63 63 75 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66  ccur.  .**.** If
2df67 20 61 20 64 69 73 63 6f 6e 6e 65 63 74 20 69 73   a disconnect is
2df68 20 61 74 74 65 6d 70 74 65 64 20 77 68 69 6c 65   attempted while
2df69 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2df6a 20 69 73 20 6c 6f 63 6b 65 64 2c 0a 2a 2a 20 74   is locked,.** t
2df6b 68 65 20 64 69 73 63 6f 6e 6e 65 63 74 20 69 73  he disconnect is
2df6c 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20   deferred until 
2df6d 61 6c 6c 20 6c 6f 63 6b 73 20 68 61 76 65 20 62  all locks have b
2df6e 65 65 6e 20 72 65 6d 6f 76 65 64 2e 0a 2a 2f 0a  een removed..*/.
2df6f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
2df70 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4c  oid sqlite3VtabL
2df71 6f 63 6b 28 56 54 61 62 6c 65 20 2a 70 56 54 61  ock(VTable *pVTa
2df72 62 29 7b 0a 20 20 70 56 54 61 62 2d 3e 6e 52 65  b){.  pVTab->nRe
2df73 66 2b 2b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 70  f++;.}.../*.** p
2df74 54 61 62 20 69 73 20 61 20 70 6f 69 6e 74 65 72  Tab is a pointer
2df75 20 74 6f 20 61 20 54 61 62 6c 65 20 73 74 72 75   to a Table stru
2df76 63 74 75 72 65 20 72 65 70 72 65 73 65 6e 74 69  cture representi
2df77 6e 67 20 61 20 76 69 72 74 75 61 6c 2d 74 61 62  ng a virtual-tab
2df78 6c 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  le..** Return a 
2df79 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56  pointer to the V
2df7a 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 75 73 65  Table object use
2df7b 64 20 62 79 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  d by connection 
2df7c 64 62 20 74 6f 20 61 63 63 65 73 73 20 0a 2a 2a  db to access .**
2df7d 20 74 68 69 73 20 76 69 72 74 75 61 6c 2d 74 61   this virtual-ta
2df7e 62 6c 65 2c 20 69 66 20 6f 6e 65 20 68 61 73 20  ble, if one has 
2df7f 62 65 65 6e 20 63 72 65 61 74 65 64 2c 20 6f 72  been created, or
2df80 20 4e 55 4c 4c 20 6f 74 68 65 72 77 69 73 65 2e   NULL otherwise.
2df81 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2df82 54 45 20 56 54 61 62 6c 65 20 2a 73 71 6c 69 74  TE VTable *sqlit
2df83 65 33 47 65 74 56 54 61 62 6c 65 28 73 71 6c 69  e3GetVTable(sqli
2df84 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a  te3 *db, Table *
2df85 70 54 61 62 29 7b 0a 20 20 56 54 61 62 6c 65 20  pTab){.  VTable 
2df86 2a 70 56 74 61 62 3b 0a 20 20 61 73 73 65 72 74  *pVtab;.  assert
2df87 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
2df88 29 20 29 3b 0a 20 20 66 6f 72 28 70 56 74 61 62  ) );.  for(pVtab
2df89 3d 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 3b 20  =pTab->pVTable; 
2df8a 70 56 74 61 62 20 26 26 20 70 56 74 61 62 2d 3e  pVtab && pVtab->
2df8b 64 62 21 3d 64 62 3b 20 70 56 74 61 62 3d 70 56  db!=db; pVtab=pV
2df8c 74 61 62 2d 3e 70 4e 65 78 74 29 3b 0a 20 20 72  tab->pNext);.  r
2df8d 65 74 75 72 6e 20 70 56 74 61 62 3b 0a 7d 0a 0a  eturn pVtab;.}..
2df8e 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20  /*.** Decrement 
2df8f 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 6f 6e  the ref-count on
2df90 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2df91 20 6f 62 6a 65 63 74 2e 20 57 68 65 6e 20 74 68   object. When th
2df92 65 20 72 65 66 2d 63 6f 75 6e 74 0a 2a 2a 20 72  e ref-count.** r
2df93 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 63 61 6c  eaches zero, cal
2df94 6c 20 74 68 65 20 78 44 69 73 63 6f 6e 6e 65 63  l the xDisconnec
2df95 74 28 29 20 6d 65 74 68 6f 64 20 74 6f 20 64 65  t() method to de
2df96 6c 65 74 65 20 74 68 65 20 6f 62 6a 65 63 74 2e  lete the object.
2df97 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2df98 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
2df99 74 61 62 55 6e 6c 6f 63 6b 28 56 54 61 62 6c 65  tabUnlock(VTable
2df9a 20 2a 70 56 54 61 62 29 7b 0a 20 20 73 71 6c 69   *pVTab){.  sqli
2df9b 74 65 33 20 2a 64 62 20 3d 20 70 56 54 61 62 2d  te3 *db = pVTab-
2df9c 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  >db;..  assert( 
2df9d 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  db );.  assert( 
2df9e 70 56 54 61 62 2d 3e 6e 52 65 66 3e 30 20 29 3b  pVTab->nRef>0 );
2df9f 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
2dfa0 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28  e3SafetyCheckOk(
2dfa1 64 62 29 20 29 3b 0a 0a 20 20 70 56 54 61 62 2d  db) );..  pVTab-
2dfa2 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70  >nRef--;.  if( p
2dfa3 56 54 61 62 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b  VTab->nRef==0 ){
2dfa4 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61  .    sqlite3_vta
2dfa5 62 20 2a 70 20 3d 20 70 56 54 61 62 2d 3e 70 56  b *p = pVTab->pV
2dfa6 74 61 62 3b 0a 20 20 20 20 69 66 28 20 70 20 29  tab;.    if( p )
2dfa7 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
2dfa8 44 45 42 55 47 0a 20 20 20 20 20 20 69 66 28 20  DEBUG.      if( 
2dfa9 70 56 54 61 62 2d 3e 64 62 2d 3e 6d 61 67 69 63  pVTab->db->magic
2dfaa 3d 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  ==SQLITE_MAGIC_B
2dfab 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 28  USY ){.        (
2dfac 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65  void)sqlite3Safe
2dfad 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20  tyOff(db);.     
2dfae 20 20 20 70 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78     p->pModule->x
2dfaf 44 69 73 63 6f 6e 6e 65 63 74 28 70 29 3b 0a 20  Disconnect(p);. 
2dfb0 20 20 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c         (void)sql
2dfb1 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
2dfb2 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 0a 23  ;.      } else.#
2dfb3 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20  endif.      {.  
2dfb4 20 20 20 20 20 20 70 2d 3e 70 4d 6f 64 75 6c 65        p->pModule
2dfb5 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28 70 29  ->xDisconnect(p)
2dfb6 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2dfb7 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2dfb8 65 28 64 62 2c 20 70 56 54 61 62 29 3b 0a 20 20  e(db, pVTab);.  
2dfb9 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65  }.}../*.** Table
2dfba 20 70 20 69 73 20 61 20 76 69 72 74 75 61 6c 20   p is a virtual 
2dfbb 74 61 62 6c 65 2e 20 54 68 69 73 20 66 75 6e 63  table. This func
2dfbc 74 69 6f 6e 20 6d 6f 76 65 73 20 61 6c 6c 20 65  tion moves all e
2dfbd 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 0a 2a  lements in the.*
2dfbe 2a 20 70 2d 3e 70 56 54 61 62 6c 65 20 6c 69 73  * p->pVTable lis
2dfbf 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  t to the sqlite3
2dfc0 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73  .pDisconnect lis
2dfc1 74 73 20 6f 66 20 74 68 65 69 72 20 61 73 73 6f  ts of their asso
2dfc2 63 69 61 74 65 64 0a 2a 2a 20 64 61 74 61 62 61  ciated.** databa
2dfc3 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  se connections t
2dfc4 6f 20 62 65 20 64 69 73 63 6f 6e 6e 65 63 74 65  o be disconnecte
2dfc5 64 20 61 74 20 74 68 65 20 6e 65 78 74 20 6f 70  d at the next op
2dfc6 70 6f 72 74 75 6e 69 74 79 2e 20 0a 2a 2a 20 45  portunity. .** E
2dfc7 78 63 65 70 74 2c 20 69 66 20 61 72 67 75 6d 65  xcept, if argume
2dfc8 6e 74 20 64 62 20 69 73 20 6e 6f 74 20 4e 55 4c  nt db is not NUL
2dfc9 4c 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 72  L, then the entr
2dfca 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
2dfcb 68 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  h.** connection 
2dfcc 64 62 20 69 73 20 6c 65 66 74 20 69 6e 20 74 68  db is left in th
2dfcd 65 20 70 2d 3e 70 56 54 61 62 6c 65 20 6c 69 73  e p->pVTable lis
2dfce 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 54 61  t..*/.static VTa
2dfcf 62 6c 65 20 2a 76 74 61 62 44 69 73 63 6f 6e 6e  ble *vtabDisconn
2dfd0 65 63 74 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a  ectAll(sqlite3 *
2dfd1 64 62 2c 20 54 61 62 6c 65 20 2a 70 29 7b 0a 20  db, Table *p){. 
2dfd2 20 56 54 61 62 6c 65 20 2a 70 52 65 74 20 3d 20   VTable *pRet = 
2dfd3 30 3b 0a 20 20 56 54 61 62 6c 65 20 2a 70 56 54  0;.  VTable *pVT
2dfd4 61 62 6c 65 20 3d 20 70 2d 3e 70 56 54 61 62 6c  able = p->pVTabl
2dfd5 65 3b 0a 20 20 70 2d 3e 70 56 54 61 62 6c 65 20  e;.  p->pVTable 
2dfd6 3d 20 30 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72  = 0;..  /* Asser
2dfd7 74 20 74 68 61 74 20 74 68 65 20 6d 75 74 65 78  t that the mutex
2dfd8 20 28 69 66 20 61 6e 79 29 20 61 73 73 6f 63 69   (if any) associ
2dfd9 61 74 65 64 20 77 69 74 68 20 74 68 65 20 42 74  ated with the Bt
2dfda 53 68 61 72 65 64 20 64 61 74 61 62 61 73 65 20  Shared database 
2dfdb 0a 20 20 2a 2a 20 74 68 61 74 20 63 6f 6e 74 61  .  ** that conta
2dfdc 69 6e 73 20 74 61 62 6c 65 20 70 20 69 73 20 68  ins table p is h
2dfdd 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65  eld by the calle
2dfde 72 2e 20 53 65 65 20 68 65 61 64 65 72 20 63 6f  r. See header co
2dfdf 6d 6d 65 6e 74 73 20 0a 20 20 2a 2a 20 61 62 6f  mments .  ** abo
2dfe0 76 65 20 66 75 6e 63 74 69 6f 6e 20 73 71 6c 69  ve function sqli
2dfe1 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73  te3VtabUnlockLis
2dfe2 74 28 29 20 66 6f 72 20 61 6e 20 65 78 70 6c 61  t() for an expla
2dfe3 6e 61 74 69 6f 6e 20 6f 66 20 77 68 79 0a 20 20  nation of why.  
2dfe4 2a 2a 20 74 68 69 73 20 6d 61 6b 65 73 20 69 74  ** this makes it
2dfe5 20 73 61 66 65 20 74 6f 20 61 63 63 65 73 73 20   safe to access 
2dfe6 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73  the sqlite3.pDis
2dfe7 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20 6f 66 20  connect list of 
2dfe8 61 6e 79 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  any.  ** databas
2dfe9 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61  e connection tha
2dfea 74 20 6d 61 79 20 68 61 76 65 20 61 6e 20 65 6e  t may have an en
2dfeb 74 72 79 20 69 6e 20 74 68 65 20 70 2d 3e 70 56  try in the p->pV
2dfec 54 61 62 6c 65 20 6c 69 73 74 2e 20 20 2a 2f 0a  Table list.  */.
2dfed 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20    assert( db==0 
2dfee 7c 7c 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ||.    sqlite3Bt
2dfef 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62  reeHoldsMutex(db
2dff0 2d 3e 61 44 62 5b 73 71 6c 69 74 65 33 53 63 68  ->aDb[sqlite3Sch
2dff1 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
2dff2 2d 3e 70 53 63 68 65 6d 61 29 5d 2e 70 42 74 29  ->pSchema)].pBt)
2dff3 20 0a 20 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28   .  );..  while(
2dff4 20 70 56 54 61 62 6c 65 20 29 7b 0a 20 20 20 20   pVTable ){.    
2dff5 73 71 6c 69 74 65 33 20 2a 64 62 32 20 3d 20 70  sqlite3 *db2 = p
2dff6 56 54 61 62 6c 65 2d 3e 64 62 3b 0a 20 20 20 20  VTable->db;.    
2dff7 56 54 61 62 6c 65 20 2a 70 4e 65 78 74 20 3d 20  VTable *pNext = 
2dff8 70 56 54 61 62 6c 65 2d 3e 70 4e 65 78 74 3b 0a  pVTable->pNext;.
2dff9 20 20 20 20 61 73 73 65 72 74 28 20 64 62 32 20      assert( db2 
2dffa 29 3b 0a 20 20 20 20 69 66 28 20 64 62 32 3d 3d  );.    if( db2==
2dffb 64 62 20 29 7b 0a 20 20 20 20 20 20 70 52 65 74  db ){.      pRet
2dffc 20 3d 20 70 56 54 61 62 6c 65 3b 0a 20 20 20 20   = pVTable;.    
2dffd 20 20 70 2d 3e 70 56 54 61 62 6c 65 20 3d 20 70    p->pVTable = p
2dffe 52 65 74 3b 0a 20 20 20 20 20 20 70 52 65 74 2d  Ret;.      pRet-
2dfff 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20  >pNext = 0;.    
2e000 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 56 54  }else{.      pVT
2e001 61 62 6c 65 2d 3e 70 4e 65 78 74 20 3d 20 64 62  able->pNext = db
2e002 32 2d 3e 70 44 69 73 63 6f 6e 6e 65 63 74 3b 0a  2->pDisconnect;.
2e003 20 20 20 20 20 20 64 62 32 2d 3e 70 44 69 73 63        db2->pDisc
2e004 6f 6e 6e 65 63 74 20 3d 20 70 56 54 61 62 6c 65  onnect = pVTable
2e005 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 56 54 61  ;.    }.    pVTa
2e006 62 6c 65 20 3d 20 70 4e 65 78 74 3b 0a 20 20 7d  ble = pNext;.  }
2e007 0a 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 20  ..  assert( !db 
2e008 7c 7c 20 70 52 65 74 20 29 3b 0a 20 20 72 65 74  || pRet );.  ret
2e009 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 0a 2f 2a  urn pRet;.}.../*
2e00a 0a 2a 2a 20 44 69 73 63 6f 6e 6e 65 63 74 20 61  .** Disconnect a
2e00b 6c 6c 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  ll the virtual t
2e00c 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 69 6e 20  able objects in 
2e00d 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73  the sqlite3.pDis
2e00e 63 6f 6e 6e 65 63 74 20 6c 69 73 74 2e 0a 2a 2a  connect list..**
2e00f 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
2e010 6e 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 63 61  n may only be ca
2e011 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 20 6d 75  lled when the mu
2e012 74 65 78 65 73 20 61 73 73 6f 63 69 61 74 65 64  texes associated
2e013 20 77 69 74 68 20 61 6c 6c 0a 2a 2a 20 73 68 61   with all.** sha
2e014 72 65 64 20 62 2d 74 72 65 65 20 64 61 74 61 62  red b-tree datab
2e015 61 73 65 73 20 6f 70 65 6e 65 64 20 75 73 69 6e  ases opened usin
2e016 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20  g connection db 
2e017 61 72 65 20 68 65 6c 64 20 62 79 20 74 68 65 20  are held by the 
2e018 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54 68 69 73  .** caller. This
2e019 20 69 73 20 64 6f 6e 65 20 74 6f 20 70 72 6f 74   is done to prot
2e01a 65 63 74 20 74 68 65 20 73 71 6c 69 74 65 33 2e  ect the sqlite3.
2e01b 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74  pDisconnect list
2e01c 2e 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33  . The.** sqlite3
2e01d 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73  .pDisconnect lis
2e01e 74 20 69 73 20 61 63 63 65 73 73 65 64 20 6f 6e  t is accessed on
2e01f 6c 79 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ly as follows:.*
2e020 2a 0a 2a 2a 20 20 20 31 29 20 42 79 20 74 68 69  *.**   1) By thi
2e021 73 20 66 75 6e 63 74 69 6f 6e 2e 20 49 6e 20 74  s function. In t
2e022 68 69 73 20 63 61 73 65 2c 20 61 6c 6c 20 42 74  his case, all Bt
2e023 53 68 61 72 65 64 20 6d 75 74 65 78 65 73 20 61  Shared mutexes a
2e024 6e 64 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20  nd the mutex.** 
2e025 20 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20       associated 
2e026 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
2e027 65 20 68 61 6e 64 6c 65 20 69 74 73 65 6c 66 20  e handle itself 
2e028 6d 75 73 74 20 62 65 20 68 65 6c 64 2e 0a 2a 2a  must be held..**
2e029 0a 2a 2a 20 20 20 32 29 20 42 79 20 66 75 6e 63  .**   2) By func
2e02a 74 69 6f 6e 20 76 74 61 62 44 69 73 63 6f 6e 6e  tion vtabDisconn
2e02b 65 63 74 41 6c 6c 28 29 2c 20 77 68 65 6e 20 69  ectAll(), when i
2e02c 74 20 61 64 64 73 20 61 20 56 54 61 62 6c 65 20  t adds a VTable 
2e02d 65 6e 74 72 79 20 74 6f 0a 2a 2a 20 20 20 20 20  entry to.**     
2e02e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69   the sqlite3.pDi
2e02f 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74 2e 20 49  sconnect list. I
2e030 6e 20 74 68 69 73 20 63 61 73 65 20 65 69 74 68  n this case eith
2e031 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 20  er the BtShared 
2e032 6d 75 74 65 78 0a 2a 2a 20 20 20 20 20 20 61 73  mutex.**      as
2e033 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
2e034 65 20 64 61 74 61 62 61 73 65 20 74 68 65 20 76  e database the v
2e035 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20  irtual table is 
2e036 73 74 6f 72 65 64 20 69 6e 20 69 73 20 68 65 6c  stored in is hel
2e037 64 0a 2a 2a 20 20 20 20 20 20 6f 72 2c 20 69 66  d.**      or, if
2e038 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2e039 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  le is stored in 
2e03a 61 20 6e 6f 6e 2d 73 68 61 72 61 62 6c 65 20 64  a non-sharable d
2e03b 61 74 61 62 61 73 65 2c 20 74 68 65 6e 0a 2a 2a  atabase, then.**
2e03c 20 20 20 20 20 20 74 68 65 20 64 61 74 61 62 61        the databa
2e03d 73 65 20 68 61 6e 64 6c 65 20 6d 75 74 65 78 20  se handle mutex 
2e03e 69 73 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 41  is held..**.** A
2e03f 73 20 61 20 72 65 73 75 6c 74 2c 20 61 20 73 71  s a result, a sq
2e040 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63  lite3.pDisconnec
2e041 74 20 63 61 6e 6e 6f 74 20 62 65 20 61 63 63 65  t cannot be acce
2e042 73 73 65 64 20 73 69 6d 75 6c 74 61 6e 65 6f 75  ssed simultaneou
2e043 73 6c 79 20 0a 2a 2a 20 62 79 20 6d 75 6c 74 69  sly .** by multi
2e044 70 6c 65 20 74 68 72 65 61 64 73 2e 20 49 74 20  ple threads. It 
2e045 69 73 20 74 68 72 65 61 64 2d 73 61 66 65 2e 0a  is thread-safe..
2e046 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2e047 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
2e048 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 73 71 6c  abUnlockList(sql
2e049 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56 54 61  ite3 *db){.  VTa
2e04a 62 6c 65 20 2a 70 20 3d 20 64 62 2d 3e 70 44 69  ble *p = db->pDi
2e04b 73 63 6f 6e 6e 65 63 74 3b 0a 20 20 64 62 2d 3e  sconnect;.  db->
2e04c 70 44 69 73 63 6f 6e 6e 65 63 74 20 3d 20 30 3b  pDisconnect = 0;
2e04d 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
2e04e 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
2e04f 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20  Mutexes(db) );. 
2e050 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2e051 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e  _mutex_held(db->
2e052 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69 66 28  mutex) );..  if(
2e053 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   p ){.    sqlite
2e054 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
2e055 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20  tatements(db);. 
2e056 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 56 54     do {.      VT
2e057 61 62 6c 65 20 2a 70 4e 65 78 74 20 3d 20 70 2d  able *pNext = p-
2e058 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71  >pNext;.      sq
2e059 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
2e05a 70 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 70 4e  p);.      p = pN
2e05b 65 78 74 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  ext;.    }while(
2e05c 20 70 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   p );.  }.}../*.
2e05d 2a 2a 20 43 6c 65 61 72 20 61 6e 79 20 61 6e 64  ** Clear any and
2e05e 20 61 6c 6c 20 76 69 72 74 75 61 6c 2d 74 61 62   all virtual-tab
2e05f 6c 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  le information f
2e060 72 6f 6d 20 74 68 65 20 54 61 62 6c 65 20 72 65  rom the Table re
2e061 63 6f 72 64 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  cord..** This ro
2e062 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
2e063 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6a 75   for example, ju
2e064 73 74 20 62 65 66 6f 72 65 20 64 65 6c 65 74 69  st before deleti
2e065 6e 67 20 74 68 65 20 54 61 62 6c 65 0a 2a 2a 20  ng the Table.** 
2e066 72 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 53 69  record..**.** Si
2e067 6e 63 65 20 69 74 20 69 73 20 61 20 76 69 72 74  nce it is a virt
2e068 75 61 6c 2d 74 61 62 6c 65 2c 20 74 68 65 20 54  ual-table, the T
2e069 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 63  able structure c
2e06a 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
2e06b 72 0a 2a 2a 20 74 6f 20 74 68 65 20 68 65 61 64  r.** to the head
2e06c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   of a linked lis
2e06d 74 20 6f 66 20 56 54 61 62 6c 65 20 73 74 72 75  t of VTable stru
2e06e 63 74 75 72 65 73 2e 20 45 61 63 68 20 56 54 61  ctures. Each VTa
2e06f 62 6c 65 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  ble .** structur
2e070 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20  e is associated 
2e071 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 73 71  with a single sq
2e072 6c 69 74 65 33 2a 20 75 73 65 72 20 6f 66 20 74  lite3* user of t
2e073 68 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 20 54 68  he schema..** Th
2e074 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
2e075 74 20 6f 66 20 74 68 65 20 56 54 61 62 6c 65 20  t of the VTable 
2e076 73 74 72 75 63 74 75 72 65 20 61 73 73 6f 63 69  structure associ
2e077 61 74 65 64 20 77 69 74 68 20 64 61 74 61 62 61  ated with databa
2e078 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f  se .** connectio
2e079 6e 20 64 62 20 69 73 20 64 65 63 72 65 6d 65 6e  n db is decremen
2e07a 74 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ted immediately 
2e07b 28 77 68 69 63 68 20 6d 61 79 20 6c 65 61 64 20  (which may lead 
2e07c 74 6f 20 74 68 65 20 0a 2a 2a 20 73 74 72 75 63  to the .** struc
2e07d 74 75 72 65 20 62 65 69 6e 67 20 78 44 69 73 63  ture being xDisc
2e07e 6f 6e 6e 65 63 74 65 64 20 61 6e 64 20 66 72 65  onnected and fre
2e07f 65 29 2e 20 41 6e 79 20 6f 74 68 65 72 20 56 54  e). Any other VT
2e080 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 73 0a  able structures.
2e081 2a 2a 20 69 6e 20 74 68 65 20 6c 69 73 74 20 61  ** in the list a
2e082 72 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20  re moved to the 
2e083 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e  sqlite3.pDisconn
2e084 65 63 74 20 6c 69 73 74 20 6f 66 20 74 68 65 20  ect list of the 
2e085 61 73 73 6f 63 69 61 74 65 64 20 0a 2a 2a 20 64  associated .** d
2e086 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2e087 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
2e088 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2e089 65 33 56 74 61 62 43 6c 65 61 72 28 54 61 62 6c  e3VtabClear(Tabl
2e08a 65 20 2a 70 29 7b 0a 20 20 76 74 61 62 44 69 73  e *p){.  vtabDis
2e08b 63 6f 6e 6e 65 63 74 41 6c 6c 28 30 2c 20 70 29  connectAll(0, p)
2e08c 3b 0a 20 20 69 66 28 20 70 2d 3e 61 7a 4d 6f 64  ;.  if( p->azMod
2e08d 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 69 6e  uleArg ){.    in
2e08e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
2e08f 3b 20 69 3c 70 2d 3e 6e 4d 6f 64 75 6c 65 41 72  ; i<p->nModuleAr
2e090 67 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  g; i++){.      s
2e091 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e  qlite3DbFree(p->
2e092 64 62 4d 65 6d 2c 20 70 2d 3e 61 7a 4d 6f 64 75  dbMem, p->azModu
2e093 6c 65 41 72 67 5b 69 5d 29 3b 0a 20 20 20 20 7d  leArg[i]);.    }
2e094 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2e095 65 65 28 70 2d 3e 64 62 4d 65 6d 2c 20 70 2d 3e  ee(p->dbMem, p->
2e096 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20  azModuleArg);.  
2e097 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61  }.}../*.** Add a
2e098 20 6e 65 77 20 6d 6f 64 75 6c 65 20 61 72 67 75   new module argu
2e099 6d 65 6e 74 20 74 6f 20 70 54 61 62 6c 65 2d 3e  ment to pTable->
2e09a 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 5d 2e 0a 2a  azModuleArg[]..*
2e09b 2a 20 54 68 65 20 73 74 72 69 6e 67 20 69 73 20  * The string is 
2e09c 6e 6f 74 20 63 6f 70 69 65 64 20 2d 20 74 68 65  not copied - the
2e09d 20 70 6f 69 6e 74 65 72 20 69 73 20 73 74 6f 72   pointer is stor
2e09e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 73 74 72 69  ed.  The.** stri
2e09f 6e 67 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64  ng will be freed
2e0a0 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77   automatically w
2e0a1 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69 73  hen the table is
2e0a2 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a  .** deleted..*/.
2e0a3 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 4d  static void addM
2e0a4 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 73 71  oduleArgument(sq
2e0a5 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65  lite3 *db, Table
2e0a6 20 2a 70 54 61 62 6c 65 2c 20 63 68 61 72 20 2a   *pTable, char *
2e0a7 7a 41 72 67 29 7b 0a 20 20 69 6e 74 20 69 20 3d  zArg){.  int i =
2e0a8 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65   pTable->nModule
2e0a9 41 72 67 2b 2b 3b 0a 20 20 69 6e 74 20 6e 42 79  Arg++;.  int nBy
2e0aa 74 65 73 20 3d 20 73 69 7a 65 6f 66 28 63 68 61  tes = sizeof(cha
2e0ab 72 20 2a 29 2a 28 31 2b 70 54 61 62 6c 65 2d 3e  r *)*(1+pTable->
2e0ac 6e 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20 63  nModuleArg);.  c
2e0ad 68 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72  har **azModuleAr
2e0ae 67 3b 0a 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67  g;.  azModuleArg
2e0af 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
2e0b0 6c 6f 63 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  loc(db, pTable->
2e0b1 61 7a 4d 6f 64 75 6c 65 41 72 67 2c 20 6e 42 79  azModuleArg, nBy
2e0b2 74 65 73 29 3b 0a 20 20 69 66 28 20 61 7a 4d 6f  tes);.  if( azMo
2e0b3 64 75 6c 65 41 72 67 3d 3d 30 20 29 7b 0a 20 20  duleArg==0 ){.  
2e0b4 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 66 6f 72    int j;.    for
2e0b5 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b  (j=0; j<i; j++){
2e0b6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2e0b7 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d  Free(db, pTable-
2e0b8 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 6a 5d 29  >azModuleArg[j])
2e0b9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2e0ba 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 41  te3DbFree(db, zA
2e0bb 72 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rg);.    sqlite3
2e0bc 44 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c  DbFree(db, pTabl
2e0bd 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b  e->azModuleArg);
2e0be 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f  .    pTable->nMo
2e0bf 64 75 6c 65 41 72 67 20 3d 20 30 3b 0a 20 20 7d  duleArg = 0;.  }
2e0c0 65 6c 73 65 7b 0a 20 20 20 20 61 7a 4d 6f 64 75  else{.    azModu
2e0c1 6c 65 41 72 67 5b 69 5d 20 3d 20 7a 41 72 67 3b  leArg[i] = zArg;
2e0c2 0a 20 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67  .    azModuleArg
2e0c3 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20  [i+1] = 0;.  }. 
2e0c4 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c   pTable->azModul
2e0c5 65 41 72 67 20 3d 20 61 7a 4d 6f 64 75 6c 65 41  eArg = azModuleA
2e0c6 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rg;.}../*.** The
2e0c7 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68   parser calls th
2e0c8 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20  is routine when 
2e0c9 69 74 20 66 69 72 73 74 20 73 65 65 73 20 61 20  it first sees a 
2e0ca 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
2e0cb 41 42 4c 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  ABLE.** statemen
2e0cc 74 2e 20 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e  t.  The module n
2e0cd 61 6d 65 20 68 61 73 20 62 65 65 6e 20 70 61 72  ame has been par
2e0ce 73 65 64 2c 20 62 75 74 20 74 68 65 20 6f 70 74  sed, but the opt
2e0cf 69 6f 6e 61 6c 20 6c 69 73 74 0a 2a 2a 20 6f 66  ional list.** of
2e0d0 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74   parameters that
2e0d1 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6d 6f 64 75   follow the modu
2e0d2 6c 65 20 6e 61 6d 65 20 61 72 65 20 73 74 69 6c  le name are stil
2e0d3 6c 20 70 65 6e 64 69 6e 67 2e 0a 2a 2f 0a 53 51  l pending..*/.SQ
2e0d4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2e0d5 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67  d sqlite3VtabBeg
2e0d6 69 6e 50 61 72 73 65 28 0a 20 20 50 61 72 73 65  inParse(.  Parse
2e0d7 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2e0d8 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2e0d9 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ext */.  Token *
2e0da 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 20 20 2f  pName1,        /
2e0db 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74 61  * Name of new ta
2e0dc 62 6c 65 2c 20 6f 72 20 64 61 74 61 62 61 73 65  ble, or database
2e0dd 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e   name */.  Token
2e0de 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 20   *pName2,       
2e0df 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20   /* Name of new 
2e0e0 74 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f  table or NULL */
2e0e1 0a 20 20 54 6f 6b 65 6e 20 2a 70 4d 6f 64 75 6c  .  Token *pModul
2e0e2 65 4e 61 6d 65 20 20 20 20 2f 2a 20 4e 61 6d 65  eName    /* Name
2e0e3 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 66   of the module f
2e0e4 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  or the virtual t
2e0e5 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  able */.){.  int
2e0e6 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20   iDb;           
2e0e7 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2e0e8 73 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  se the table is 
2e0e9 62 65 69 6e 67 20 63 72 65 61 74 65 64 20 69 6e  being created in
2e0ea 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2e0eb 62 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ble;        /* T
2e0ec 68 65 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74  he new virtual t
2e0ed 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  able */.  sqlite
2e0ee 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
2e0ef 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
2e0f0 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 73 71 6c  ection */..  sql
2e0f1 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 70  ite3StartTable(p
2e0f2 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70  Parse, pName1, p
2e0f3 4e 61 6d 65 32 2c 20 30 2c 20 30 2c 20 31 2c 20  Name2, 0, 0, 1, 
2e0f4 30 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 70  0);.  pTable = p
2e0f5 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2e0f6 3b 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d  ;.  if( pTable==
2e0f7 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73  0 ) return;.  as
2e0f8 73 65 72 74 28 20 30 3d 3d 70 54 61 62 6c 65 2d  sert( 0==pTable-
2e0f9 3e 70 49 6e 64 65 78 20 29 3b 0a 0a 20 20 64 62  >pIndex );..  db
2e0fa 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2e0fb 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
2e0fc 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
2e0fd 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 29  pTable->pSchema)
2e0fe 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ;.  assert( iDb>
2e0ff 3d 30 20 29 3b 0a 0a 20 20 70 54 61 62 6c 65 2d  =0 );..  pTable-
2e100 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f  >tabFlags |= TF_
2e101 56 69 72 74 75 61 6c 3b 0a 20 20 70 54 61 62 6c  Virtual;.  pTabl
2e102 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20  e->nModuleArg = 
2e103 30 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41 72  0;.  addModuleAr
2e104 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62 6c  gument(db, pTabl
2e105 65 2c 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72  e, sqlite3NameFr
2e106 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4d 6f 64  omToken(db, pMod
2e107 75 6c 65 4e 61 6d 65 29 29 3b 0a 20 20 61 64 64  uleName));.  add
2e108 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64  ModuleArgument(d
2e109 62 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69 74  b, pTable, sqlit
2e10a 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 64  e3DbStrDup(db, d
2e10b 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
2e10c 65 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65  e));.  addModule
2e10d 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61  Argument(db, pTa
2e10e 62 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74  ble, sqlite3DbSt
2e10f 72 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d  rDup(db, pTable-
2e110 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 70 50 61 72  >zName));.  pPar
2e111 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e  se->sNameToken.n
2e112 20 3d 20 28 69 6e 74 29 28 26 70 4d 6f 64 75 6c   = (int)(&pModul
2e113 65 4e 61 6d 65 2d 3e 7a 5b 70 4d 6f 64 75 6c 65  eName->z[pModule
2e114 4e 61 6d 65 2d 3e 6e 5d 20 2d 20 70 4e 61 6d 65  Name->n] - pName
2e115 31 2d 3e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20  1->z);..#ifndef 
2e116 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
2e117 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 43  ORIZATION.  /* C
2e118 72 65 61 74 69 6e 67 20 61 20 76 69 72 74 75 61  reating a virtua
2e119 6c 20 74 61 62 6c 65 20 69 6e 76 6f 6b 65 73 20  l table invokes 
2e11a 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  the authorizatio
2e11b 6e 20 63 61 6c 6c 62 61 63 6b 20 74 77 69 63 65  n callback twice
2e11c 2e 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74  ..  ** The first
2e11d 20 69 6e 76 6f 63 61 74 69 6f 6e 2c 20 74 6f 20   invocation, to 
2e11e 6f 62 74 61 69 6e 20 70 65 72 6d 69 73 73 69 6f  obtain permissio
2e11f 6e 20 74 6f 20 49 4e 53 45 52 54 20 61 20 72 6f  n to INSERT a ro
2e120 77 20 69 6e 74 6f 20 74 68 65 0a 20 20 2a 2a 20  w into the.  ** 
2e121 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
2e122 62 6c 65 2c 20 68 61 73 20 61 6c 72 65 61 64 79  ble, has already
2e123 20 62 65 65 6e 20 6d 61 64 65 20 62 79 20 73 71   been made by sq
2e124 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
2e125 29 2e 0a 20 20 2a 2a 20 54 68 65 20 73 65 63 6f  )..  ** The seco
2e126 6e 64 20 63 61 6c 6c 2c 20 74 6f 20 6f 62 74 61  nd call, to obta
2e127 69 6e 20 70 65 72 6d 69 73 73 69 6f 6e 20 74 6f  in permission to
2e128 20 63 72 65 61 74 65 20 74 68 65 20 74 61 62 6c   create the tabl
2e129 65 2c 20 69 73 20 6d 61 64 65 20 6e 6f 77 2e 0a  e, is made now..
2e12a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c    */.  if( pTabl
2e12b 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 29  e->azModuleArg )
2e12c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74  {.    sqlite3Aut
2e12d 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
2e12e 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 56 54 41  QLITE_CREATE_VTA
2e12f 42 4c 45 2c 20 70 54 61 62 6c 65 2d 3e 7a 4e 61  BLE, pTable->zNa
2e130 6d 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  me, .           
2e131 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c   pTable->azModul
2e132 65 41 72 67 5b 30 5d 2c 20 70 50 61 72 73 65 2d  eArg[0], pParse-
2e133 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e  >db->aDb[iDb].zN
2e134 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  ame);.  }.#endif
2e135 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2e136 6f 75 74 69 6e 65 20 74 61 6b 65 73 20 74 68 65  outine takes the
2e137 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74   module argument
2e138 20 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 61   that has been a
2e139 63 63 75 6d 75 6c 61 74 69 6e 67 0a 2a 2a 20 69  ccumulating.** i
2e13a 6e 20 70 50 61 72 73 65 2d 3e 7a 41 72 67 5b 5d  n pParse->zArg[]
2e13b 20 61 6e 64 20 61 70 70 65 6e 64 73 20 69 74 20   and appends it 
2e13c 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61  to the list of a
2e13d 72 67 75 6d 65 6e 74 73 20 6f 6e 20 74 68 65 0a  rguments on the.
2e13e 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ** virtual table
2e13f 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72   currently under
2e140 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e   construction in
2e141 20 70 50 61 72 73 65 2d 3e 70 54 61 62 6c 65 2e   pParse->pTable.
2e142 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2e143 61 64 64 41 72 67 75 6d 65 6e 74 54 6f 56 74 61  addArgumentToVta
2e144 62 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  b(Parse *pParse)
2e145 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  {.  if( pParse->
2e146 73 41 72 67 2e 7a 20 26 26 20 41 4c 57 41 59 53  sArg.z && ALWAYS
2e147 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62  (pParse->pNewTab
2e148 6c 65 29 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  le) ){.    const
2e149 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73   char *z = (cons
2e14a 74 20 63 68 61 72 2a 29 70 50 61 72 73 65 2d 3e  t char*)pParse->
2e14b 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69 6e 74 20  sArg.z;.    int 
2e14c 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73 41 72 67  n = pParse->sArg
2e14d 2e 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  .n;.    sqlite3 
2e14e 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2e14f 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75 6c 65 41  ;.    addModuleA
2e150 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 50 61 72  rgument(db, pPar
2e151 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20 73  se->pNewTable, s
2e152 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
2e153 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a 20 20 7d 0a  db, z, n));.  }.
2e154 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72  }../*.** The par
2e155 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
2e156 6f 75 74 69 6e 65 20 61 66 74 65 72 20 74 68 65  outine after the
2e157 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
2e158 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 0a  TABLE statement.
2e159 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70  ** has been comp
2e15a 6c 65 74 65 6c 79 20 70 61 72 73 65 64 2e 0a 2a  letely parsed..*
2e15b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2e15c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
2e15d 62 46 69 6e 69 73 68 50 61 72 73 65 28 50 61 72  bFinishParse(Par
2e15e 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
2e15f 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 54 61 62 6c  n *pEnd){.  Tabl
2e160 65 20 2a 70 54 61 62 20 3d 20 70 50 61 72 73 65  e *pTab = pParse
2e161 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 20 20 2f 2a  ->pNewTable;  /*
2e162 20 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   The table being
2e163 20 63 6f 6e 73 74 72 75 63 74 65 64 20 2a 2f 0a   constructed */.
2e164 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2e165 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
2e166 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
2e167 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2e168 2f 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  /..  if( pTab==0
2e169 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 64 64   ) return;.  add
2e16a 41 72 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 70  ArgumentToVtab(p
2e16b 50 61 72 73 65 29 3b 0a 20 20 70 50 61 72 73 65  Parse);.  pParse
2e16c 2d 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0a 20 20  ->sArg.z = 0;.  
2e16d 69 66 28 20 70 54 61 62 2d 3e 6e 4d 6f 64 75 6c  if( pTab->nModul
2e16e 65 41 72 67 3c 31 20 29 20 72 65 74 75 72 6e 3b  eArg<1 ) return;
2e16f 0a 20 20 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  .  .  /* If the 
2e170 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54  CREATE VIRTUAL T
2e171 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69  ABLE statement i
2e172 73 20 62 65 69 6e 67 20 65 6e 74 65 72 65 64 20  s being entered 
2e173 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 66 69 72  for the.  ** fir
2e174 73 74 20 74 69 6d 65 20 28 69 6e 20 6f 74 68 65  st time (in othe
2e175 72 20 77 6f 72 64 73 20 69 66 20 74 68 65 20 76  r words if the v
2e176 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20  irtual table is 
2e177 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 0a 20  actually being. 
2e178 20 2a 2a 20 63 72 65 61 74 65 64 20 6e 6f 77 20   ** created now 
2e179 69 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74 20  instead of just 
2e17a 62 65 69 6e 67 20 72 65 61 64 20 6f 75 74 20 6f  being read out o
2e17b 66 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 29  f sqlite_master)
2e17c 20 74 68 65 6e 0a 20 20 2a 2a 20 64 6f 20 61 64   then.  ** do ad
2e17d 64 69 74 69 6f 6e 61 6c 20 69 6e 69 74 69 61 6c  ditional initial
2e17e 69 7a 61 74 69 6f 6e 20 77 6f 72 6b 20 61 6e 64  ization work and
2e17f 20 73 74 6f 72 65 20 74 68 65 20 73 74 61 74 65   store the state
2e180 6d 65 6e 74 20 74 65 78 74 0a 20 20 2a 2a 20 69  ment text.  ** i
2e181 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
2e182 74 65 72 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  ter table..  */.
2e183 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
2e184 62 75 73 79 20 29 7b 0a 20 20 20 20 63 68 61 72  busy ){.    char
2e185 20 2a 7a 53 74 6d 74 3b 0a 20 20 20 20 63 68 61   *zStmt;.    cha
2e186 72 20 2a 7a 57 68 65 72 65 3b 0a 20 20 20 20 69  r *zWhere;.    i
2e187 6e 74 20 69 44 62 3b 0a 20 20 20 20 56 64 62 65  nt iDb;.    Vdbe
2e188 20 2a 76 3b 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d   *v;..    /* Com
2e189 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c 65 74  pute the complet
2e18a 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52  e text of the CR
2e18b 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42  EATE VIRTUAL TAB
2e18c 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  LE statement */.
2e18d 20 20 20 20 69 66 28 20 70 45 6e 64 20 29 7b 0a      if( pEnd ){.
2e18e 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 73 4e        pParse->sN
2e18f 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 28 69 6e  ameToken.n = (in
2e190 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61  t)(pEnd->z - pPa
2e191 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
2e192 7a 29 20 2b 20 70 45 6e 64 2d 3e 6e 3b 0a 20 20  z) + pEnd->n;.  
2e193 20 20 7d 0a 20 20 20 20 7a 53 74 6d 74 20 3d 20    }.    zStmt = 
2e194 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2e195 62 2c 20 22 43 52 45 41 54 45 20 56 49 52 54 55  b, "CREATE VIRTU
2e196 41 4c 20 54 41 42 4c 45 20 25 54 22 2c 20 26 70  AL TABLE %T", &p
2e197 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
2e198 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c  n);..    /* A sl
2e199 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72  ot for the recor
2e19a 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  d has already be
2e19b 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  en allocated in 
2e19c 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49  the .    ** SQLI
2e19d 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e  TE_MASTER table.
2e19e 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74    We just need t
2e19f 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c  o update that sl
2e1a0 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20  ot with all.    
2e1a1 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69  ** the informati
2e1a2 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74  on we've collect
2e1a3 65 64 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20  ed.  .    **.   
2e1a4 20 2a 2a 20 54 68 65 20 56 4d 20 72 65 67 69 73   ** The VM regis
2e1a5 74 65 72 20 6e 75 6d 62 65 72 20 70 50 61 72 73  ter number pPars
2e1a6 65 2d 3e 72 65 67 52 6f 77 69 64 20 68 6f 6c 64  e->regRowid hold
2e1a7 73 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61  s the rowid of a
2e1a8 6e 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 69  n.    ** entry i
2e1a9 6e 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73  n the sqlite_mas
2e1aa 74 65 72 20 74 61 62 6c 65 20 74 68 74 20 77 61  ter table tht wa
2e1ab 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 68  s created for th
2e1ac 69 73 20 76 74 61 62 0a 20 20 20 20 2a 2a 20 62  is vtab.    ** b
2e1ad 79 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61  y sqlite3StartTa
2e1ae 62 6c 65 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  ble()..    */.  
2e1af 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
2e1b0 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
2e1b1 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
2e1b2 0a 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74  .    sqlite3Nest
2e1b3 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
2e1b4 20 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51        "UPDATE %Q
2e1b5 2e 25 73 20 22 0a 20 20 20 20 20 20 20 20 20 22  .%s ".         "
2e1b6 53 45 54 20 74 79 70 65 3d 27 74 61 62 6c 65 27  SET type='table'
2e1b7 2c 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e  , name=%Q, tbl_n
2e1b8 61 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65  ame=%Q, rootpage
2e1b9 3d 30 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20  =0, sql=%Q ".   
2e1ba 20 20 20 20 22 57 48 45 52 45 20 72 6f 77 69 64      "WHERE rowid
2e1bb 3d 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62 2d  =#%d",.      db-
2e1bc 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c  >aDb[iDb].zName,
2e1bd 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
2e1be 62 29 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  b),.      pTab->
2e1bf 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 54 61  zName,.      pTa
2e1c0 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  b->zName,.      
2e1c1 7a 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50 61  zStmt,.      pPa
2e1c2 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20  rse->regRowid.  
2e1c3 20 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33    );.    sqlite3
2e1c4 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74  DbFree(db, zStmt
2e1c5 29 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74  );.    v = sqlit
2e1c6 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2e1c7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68  );.    sqlite3Ch
2e1c8 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
2e1c9 65 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 73 71  e, iDb);..    sq
2e1ca 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2e1cb 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c  v, OP_Expire, 0,
2e1cc 20 30 29 3b 0a 20 20 20 20 7a 57 68 65 72 65 20   0);.    zWhere 
2e1cd 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2e1ce 28 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22  (db, "name='%q'"
2e1cf 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2e1d0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2e1d1 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73  ddOp4(v, OP_Pars
2e1d2 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 31 2c  eSchema, iDb, 1,
2e1d3 20 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44   0, zWhere, P4_D
2e1d4 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 73 71 6c  YNAMIC);.    sql
2e1d5 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2e1d6 2c 20 4f 50 5f 56 43 72 65 61 74 65 2c 20 69 44  , OP_VCreate, iD
2e1d7 62 2c 20 30 2c 20 30 2c 20 0a 20 20 20 20 20 20  b, 0, 0, .      
2e1d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e1d9 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20     pTab->zName, 
2e1da 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2e1db 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 20 2b 20 31  pTab->zName) + 1
2e1dc 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
2e1dd 77 65 20 61 72 65 20 72 65 72 65 61 64 69 6e 67  we are rereading
2e1de 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
2e1df 65 72 20 74 61 62 6c 65 20 63 72 65 61 74 65 20  er table create 
2e1e0 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20  the in-memory.  
2e1e1 2a 2a 20 72 65 63 6f 72 64 20 6f 66 20 74 68 65  ** record of the
2e1e2 20 74 61 62 6c 65 2e 20 54 68 65 20 78 43 6f 6e   table. The xCon
2e1e3 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 20 69 73  nect() method is
2e1e4 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 74 69   not called unti
2e1e5 6c 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74  l.  ** the first
2e1e6 20 74 69 6d 65 20 74 68 65 20 76 69 72 74 75 61   time the virtua
2e1e7 6c 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20  l table is used 
2e1e8 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  in an SQL statem
2e1e9 65 6e 74 2e 20 54 68 69 73 0a 20 20 2a 2a 20 61  ent. This.  ** a
2e1ea 6c 6c 6f 77 73 20 61 20 73 63 68 65 6d 61 20 74  llows a schema t
2e1eb 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 76 69 72  hat contains vir
2e1ec 74 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 62  tual tables to b
2e1ed 65 20 6c 6f 61 64 65 64 20 62 65 66 6f 72 65 0a  e loaded before.
2e1ee 20 20 2a 2a 20 74 68 65 20 72 65 71 75 69 72 65    ** the require
2e1ef 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  d virtual table 
2e1f0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
2e1f1 61 72 65 20 72 65 67 69 73 74 65 72 65 64 2e 20  are registered. 
2e1f2 20 2a 2f 0a 20 20 65 6c 73 65 20 7b 0a 20 20 20   */.  else {.   
2e1f3 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20   Table *pOld;.  
2e1f4 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
2e1f5 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  a = pTab->pSchem
2e1f6 61 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  a;.    const cha
2e1f7 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 54 61 62 2d  r *zName = pTab-
2e1f8 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 6e 74 20  >zName;.    int 
2e1f9 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
2e1fa 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a  trlen30(zName);.
2e1fb 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74      pOld = sqlit
2e1fc 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 53  e3HashInsert(&pS
2e1fd 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20  chema->tblHash, 
2e1fe 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 54  zName, nName, pT
2e1ff 61 62 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c  ab);.    if( pOl
2e200 64 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  d ){.      db->m
2e201 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2e202 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2e203 54 61 62 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a  Tab==pOld );  /*
2e204 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76   Malloc must hav
2e205 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20  e failed inside 
2e206 48 61 73 68 49 6e 73 65 72 74 28 29 20 2a 2f 0a  HashInsert() */.
2e207 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2e208 20 20 7d 0a 20 20 20 20 70 53 63 68 65 6d 61 2d    }.    pSchema-
2e209 3e 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  >db = pParse->db
2e20a 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e  ;.    pParse->pN
2e20b 65 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 7d  ewTable = 0;.  }
2e20c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61  .}../*.** The pa
2e20d 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20  rser calls this 
2e20e 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20  routine when it 
2e20f 73 65 65 73 20 74 68 65 20 66 69 72 73 74 20 74  sees the first t
2e210 6f 6b 65 6e 0a 2a 2a 20 6f 66 20 61 6e 20 61 72  oken.** of an ar
2e211 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f  gument to the mo
2e212 64 75 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20 43  dule name in a C
2e213 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
2e214 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  BLE statement..*
2e215 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2e216 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
2e217 62 41 72 67 49 6e 69 74 28 50 61 72 73 65 20 2a  bArgInit(Parse *
2e218 70 50 61 72 73 65 29 7b 0a 20 20 61 64 64 41 72  pParse){.  addAr
2e219 67 75 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61  gumentToVtab(pPa
2e21a 72 73 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  rse);.  pParse->
2e21b 73 41 72 67 2e 7a 20 3d 20 30 3b 0a 20 20 70 50  sArg.z = 0;.  pP
2e21c 61 72 73 65 2d 3e 73 41 72 67 2e 6e 20 3d 20 30  arse->sArg.n = 0
2e21d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  ;.}../*.** The p
2e21e 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73  arser calls this
2e21f 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 65 61 63   routine for eac
2e220 68 20 74 6f 6b 65 6e 20 61 66 74 65 72 20 74 68  h token after th
2e221 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a  e first token.**
2e222 20 69 6e 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   in an argument 
2e223 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61  to the module na
2e224 6d 65 20 69 6e 20 61 20 43 52 45 41 54 45 20 56  me in a CREATE V
2e225 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
2e226 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  tement..*/.SQLIT
2e227 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2e228 71 6c 69 74 65 33 56 74 61 62 41 72 67 45 78 74  qlite3VtabArgExt
2e229 65 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72 73  end(Parse *pPars
2e22a 65 2c 20 54 6f 6b 65 6e 20 2a 70 29 7b 0a 20 20  e, Token *p){.  
2e22b 54 6f 6b 65 6e 20 2a 70 41 72 67 20 3d 20 26 70  Token *pArg = &p
2e22c 50 61 72 73 65 2d 3e 73 41 72 67 3b 0a 20 20 69  Parse->sArg;.  i
2e22d 66 28 20 70 41 72 67 2d 3e 7a 3d 3d 30 20 29 7b  f( pArg->z==0 ){
2e22e 0a 20 20 20 20 70 41 72 67 2d 3e 7a 20 3d 20 70  .    pArg->z = p
2e22f 2d 3e 7a 3b 0a 20 20 20 20 70 41 72 67 2d 3e 6e  ->z;.    pArg->n
2e230 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 65 6c 73 65   = p->n;.  }else
2e231 7b 0a 20 20 20 20 61 73 73 65 72 74 28 70 41 72  {.    assert(pAr
2e232 67 2d 3e 7a 20 3c 20 70 2d 3e 7a 29 3b 0a 20 20  g->z < p->z);.  
2e233 20 20 70 41 72 67 2d 3e 6e 20 3d 20 28 69 6e 74    pArg->n = (int
2e234 29 28 26 70 2d 3e 7a 5b 70 2d 3e 6e 5d 20 2d 20  )(&p->z[p->n] - 
2e235 70 41 72 67 2d 3e 7a 29 3b 0a 20 20 7d 0a 7d 0a  pArg->z);.  }.}.
2e236 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20  ./*.** Invoke a 
2e237 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 6f  virtual table co
2e238 6e 73 74 72 75 63 74 6f 72 20 28 65 69 74 68 65  nstructor (eithe
2e239 72 20 78 43 72 65 61 74 65 20 6f 72 20 78 43 6f  r xCreate or xCo
2e23a 6e 6e 65 63 74 29 2e 20 54 68 65 0a 2a 2a 20 70  nnect). The.** p
2e23b 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75  ointer to the fu
2e23c 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65  nction to invoke
2e23d 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68   is passed as th
2e23e 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74  e fourth paramet
2e23f 65 72 0a 2a 2a 20 74 6f 20 74 68 69 73 20 70 72  er.** to this pr
2e240 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61 74  ocedure..*/.stat
2e241 69 63 20 69 6e 74 20 76 74 61 62 43 61 6c 6c 43  ic int vtabCallC
2e242 6f 6e 73 74 72 75 63 74 6f 72 28 0a 20 20 73 71  onstructor(.  sq
2e243 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 54 61  lite3 *db, .  Ta
2e244 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20 4d 6f 64  ble *pTab,.  Mod
2e245 75 6c 65 20 2a 70 4d 6f 64 2c 0a 20 20 69 6e 74  ule *pMod,.  int
2e246 20 28 2a 78 43 6f 6e 73 74 72 75 63 74 29 28 73   (*xConstruct)(s
2e247 71 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e  qlite3*,void*,in
2e248 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f 6e  t,const char*con
2e249 73 74 2a 2c 73 71 6c 69 74 65 33 5f 76 74 61 62  st*,sqlite3_vtab
2e24a 2a 2a 2c 63 68 61 72 2a 2a 29 2c 0a 20 20 63 68  **,char**),.  ch
2e24b 61 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20  ar **pzErr.){.  
2e24c 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 3b  VTable *pVTable;
2e24d 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 6f 6e  .  int rc;.  con
2e24e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61  st char *const*a
2e24f 7a 41 72 67 20 3d 20 28 63 6f 6e 73 74 20 63 68  zArg = (const ch
2e250 61 72 20 2a 63 6f 6e 73 74 2a 29 70 54 61 62 2d  ar *const*)pTab-
2e251 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20  >azModuleArg;.  
2e252 69 6e 74 20 6e 41 72 67 20 3d 20 70 54 61 62 2d  int nArg = pTab-
2e253 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b 0a 20 20 63  >nModuleArg;.  c
2e254 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20  har *zErr = 0;. 
2e255 20 63 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 4e 61   char *zModuleNa
2e256 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
2e257 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 20 70 54  ntf(db, "%s", pT
2e258 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69  ab->zName);..  i
2e259 66 28 20 21 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20  f( !zModuleName 
2e25a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
2e25b 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
2e25c 0a 20 20 70 56 54 61 62 6c 65 20 3d 20 73 71 6c  .  pVTable = sql
2e25d 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2e25e 28 64 62 2c 20 73 69 7a 65 6f 66 28 56 54 61 62  (db, sizeof(VTab
2e25f 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 70 56 54  le));.  if( !pVT
2e260 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c 69  able ){.    sqli
2e261 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d  te3DbFree(db, zM
2e262 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20  oduleName);.    
2e263 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
2e264 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 56 54 61 62  MEM;.  }.  pVTab
2e265 6c 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 70  le->db = db;.  p
2e266 56 54 61 62 6c 65 2d 3e 70 4d 6f 64 20 3d 20 70  VTable->pMod = p
2e267 4d 6f 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  Mod;..  assert( 
2e268 21 64 62 2d 3e 70 56 54 61 62 20 29 3b 0a 20 20  !db->pVTab );.  
2e269 61 73 73 65 72 74 28 20 78 43 6f 6e 73 74 72 75  assert( xConstru
2e26a 63 74 20 29 3b 0a 20 20 64 62 2d 3e 70 56 54 61  ct );.  db->pVTa
2e26b 62 20 3d 20 70 54 61 62 3b 0a 0a 20 20 2f 2a 20  b = pTab;..  /* 
2e26c 49 6e 76 6f 6b 65 20 74 68 65 20 76 69 72 74 75  Invoke the virtu
2e26d 61 6c 20 74 61 62 6c 65 20 63 6f 6e 73 74 72 75  al table constru
2e26e 63 74 6f 72 20 2a 2f 0a 20 20 28 76 6f 69 64 29  ctor */.  (void)
2e26f 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
2e270 28 64 62 29 3b 0a 20 20 72 63 20 3d 20 78 43 6f  (db);.  rc = xCo
2e271 6e 73 74 72 75 63 74 28 64 62 2c 20 70 4d 6f 64  nstruct(db, pMod
2e272 2d 3e 70 41 75 78 2c 20 6e 41 72 67 2c 20 61 7a  ->pAux, nArg, az
2e273 41 72 67 2c 20 26 70 56 54 61 62 6c 65 2d 3e 70  Arg, &pVTable->p
2e274 56 74 61 62 2c 20 26 7a 45 72 72 29 3b 0a 20 20  Vtab, &zErr);.  
2e275 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
2e276 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 69 66 28  etyOn(db);.  if(
2e277 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
2e278 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  M ) db->mallocFa
2e279 69 6c 65 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28  iled = 1;..  if(
2e27a 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29   SQLITE_OK!=rc )
2e27b 7b 0a 20 20 20 20 69 66 28 20 7a 45 72 72 3d 3d  {.    if( zErr==
2e27c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72  0 ){.      *pzEr
2e27d 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  r = sqlite3MPrin
2e27e 74 66 28 64 62 2c 20 22 76 74 61 62 6c 65 20 63  tf(db, "vtable c
2e27f 6f 6e 73 74 72 75 63 74 6f 72 20 66 61 69 6c 65  onstructor faile
2e280 64 3a 20 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 4e  d: %s", zModuleN
2e281 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ame);.    }else 
2e282 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d  {.      *pzErr =
2e283 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
2e284 64 62 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 3b  db, "%s", zErr);
2e285 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
2e286 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a  Free(db, zErr);.
2e287 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2e288 33 44 62 46 72 65 65 28 64 62 2c 20 70 56 54 61  3DbFree(db, pVTa
2e289 62 6c 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ble);.  }else if
2e28a 28 20 41 4c 57 41 59 53 28 70 56 54 61 62 6c 65  ( ALWAYS(pVTable
2e28b 2d 3e 70 56 74 61 62 29 20 29 7b 0a 20 20 20 20  ->pVtab) ){.    
2e28c 2f 2a 20 4a 75 73 74 69 66 69 63 61 74 69 6f 6e  /* Justification
2e28d 20 6f 66 20 41 4c 57 41 59 53 28 29 3a 20 20 41   of ALWAYS():  A
2e28e 20 63 6f 72 72 65 63 74 20 76 74 61 62 20 63 6f   correct vtab co
2e28f 6e 73 74 72 75 63 74 6f 72 20 6d 75 73 74 20 61  nstructor must a
2e290 6c 6c 6f 63 61 74 65 0a 20 20 20 20 2a 2a 20 74  llocate.    ** t
2e291 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  he sqlite3_vtab 
2e292 6f 62 6a 65 63 74 20 69 66 20 73 75 63 63 65 73  object if succes
2e293 73 66 75 6c 2e 20 20 2a 2f 0a 20 20 20 20 70 56  sful.  */.    pV
2e294 54 61 62 6c 65 2d 3e 70 56 74 61 62 2d 3e 70 4d  Table->pVtab->pM
2e295 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 2d 3e 70 4d  odule = pMod->pM
2e296 6f 64 75 6c 65 3b 0a 20 20 20 20 70 56 54 61 62  odule;.    pVTab
2e297 6c 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  le->nRef = 1;.  
2e298 20 20 69 66 28 20 64 62 2d 3e 70 56 54 61 62 20    if( db->pVTab 
2e299 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  ){.      const c
2e29a 68 61 72 20 2a 7a 46 6f 72 6d 61 74 20 3d 20 22  har *zFormat = "
2e29b 76 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74  vtable construct
2e29c 6f 72 20 64 69 64 20 6e 6f 74 20 64 65 63 6c 61  or did not decla
2e29d 72 65 20 73 63 68 65 6d 61 3a 20 25 73 22 3b 0a  re schema: %s";.
2e29e 20 20 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73        *pzErr = s
2e29f 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2e2a0 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54 61 62 2d  , zFormat, pTab-
2e2a1 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73  >zName);.      s
2e2a2 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b  qlite3VtabUnlock
2e2a3 28 70 56 54 61 62 6c 65 29 3b 0a 20 20 20 20 20  (pVTable);.     
2e2a4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
2e2a5 4f 52 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  OR;.    }else{. 
2e2a6 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20       int iCol;. 
2e2a7 20 20 20 20 20 2f 2a 20 49 66 20 65 76 65 72 79       /* If every
2e2a8 74 68 69 6e 67 20 77 65 6e 74 20 61 63 63 6f 72  thing went accor
2e2a9 64 69 6e 67 20 74 6f 20 70 6c 61 6e 2c 20 6c 69  ding to plan, li
2e2aa 6e 6b 20 74 68 65 20 6e 65 77 20 56 54 61 62 6c  nk the new VTabl
2e2ab 65 20 73 74 72 75 63 74 75 72 65 0a 20 20 20 20  e structure.    
2e2ac 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 6c 69    ** into the li
2e2ad 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64  nked list headed
2e2ae 20 62 79 20 70 54 61 62 2d 3e 70 56 54 61 62 6c   by pTab->pVTabl
2e2af 65 2e 20 54 68 65 6e 20 6c 6f 6f 70 20 74 68 72  e. Then loop thr
2e2b0 6f 75 67 68 20 74 68 65 20 0a 20 20 20 20 20 20  ough the .      
2e2b1 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  ** columns of th
2e2b2 65 20 74 61 62 6c 65 20 74 6f 20 73 65 65 20 69  e table to see i
2e2b3 66 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 63 6f  f any of them co
2e2b4 6e 74 61 69 6e 20 74 68 65 20 74 6f 6b 65 6e 20  ntain the token 
2e2b5 22 68 69 64 64 65 6e 22 2e 0a 20 20 20 20 20 20  "hidden"..      
2e2b6 2a 2a 20 49 66 20 73 6f 2c 20 73 65 74 20 74 68  ** If so, set th
2e2b7 65 20 43 6f 6c 75 6d 6e 2e 69 73 48 69 64 64 65  e Column.isHidde
2e2b8 6e 20 66 6c 61 67 20 61 6e 64 20 72 65 6d 6f 76  n flag and remov
2e2b9 65 20 74 68 65 20 74 6f 6b 65 6e 20 66 72 6f 6d  e the token from
2e2ba 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 74 79  .      ** the ty
2e2bb 70 65 20 73 74 72 69 6e 67 2e 20 20 2a 2f 0a 20  pe string.  */. 
2e2bc 20 20 20 20 20 70 56 54 61 62 6c 65 2d 3e 70 4e       pVTable->pN
2e2bd 65 78 74 20 3d 20 70 54 61 62 2d 3e 70 56 54 61  ext = pTab->pVTa
2e2be 62 6c 65 3b 0a 20 20 20 20 20 20 70 54 61 62 2d  ble;.      pTab-
2e2bf 3e 70 56 54 61 62 6c 65 20 3d 20 70 56 54 61 62  >pVTable = pVTab
2e2c0 6c 65 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 69  le;..      for(i
2e2c1 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62  Col=0; iCol<pTab
2e2c2 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b  ->nCol; iCol++){
2e2c3 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
2e2c4 54 79 70 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f  Type = pTab->aCo
2e2c5 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20  l[iCol].zType;. 
2e2c6 20 20 20 20 20 20 20 69 6e 74 20 6e 54 79 70 65         int nType
2e2c7 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 20  ;.        int i 
2e2c8 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  = 0;.        if(
2e2c9 20 21 7a 54 79 70 65 20 29 20 63 6f 6e 74 69 6e   !zType ) contin
2e2ca 75 65 3b 0a 20 20 20 20 20 20 20 20 6e 54 79 70  ue;.        nTyp
2e2cb 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  e = sqlite3Strle
2e2cc 6e 33 30 28 7a 54 79 70 65 29 3b 0a 20 20 20 20  n30(zType);.    
2e2cd 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
2e2ce 74 72 4e 49 43 6d 70 28 22 68 69 64 64 65 6e 22  trNICmp("hidden"
2e2cf 2c 20 7a 54 79 70 65 2c 20 36 29 7c 7c 28 7a 54  , zType, 6)||(zT
2e2d0 79 70 65 5b 36 5d 20 26 26 20 7a 54 79 70 65 5b  ype[6] && zType[
2e2d1 36 5d 21 3d 27 20 27 29 20 29 7b 0a 20 20 20 20  6]!=' ') ){.    
2e2d2 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
2e2d3 3c 6e 54 79 70 65 3b 20 69 2b 2b 29 7b 0a 20 20  <nType; i++){.  
2e2d4 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28 30            if( (0
2e2d5 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ==sqlite3StrNICm
2e2d6 70 28 22 20 68 69 64 64 65 6e 22 2c 20 26 7a 54  p(" hidden", &zT
2e2d7 79 70 65 5b 69 5d 2c 20 37 29 29 0a 20 20 20 20  ype[i], 7)).    
2e2d8 20 20 20 20 20 20 20 20 20 26 26 20 28 7a 54 79           && (zTy
2e2d9 70 65 5b 69 2b 37 5d 3d 3d 27 5c 30 27 20 7c 7c  pe[i+7]=='\0' ||
2e2da 20 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d 27 20 27   zType[i+7]==' '
2e2db 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 7b  ).            ){
2e2dc 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69  .              i
2e2dd 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ++;.            
2e2de 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
2e2df 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2e2e0 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2e2e1 20 20 20 20 20 69 66 28 20 69 3c 6e 54 79 70 65       if( i<nType
2e2e2 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
2e2e3 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 69  t j;.          i
2e2e4 6e 74 20 6e 44 65 6c 20 3d 20 36 20 2b 20 28 7a  nt nDel = 6 + (z
2e2e5 54 79 70 65 5b 69 2b 36 5d 20 3f 20 31 20 3a 20  Type[i+6] ? 1 : 
2e2e6 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f  0);.          fo
2e2e7 72 28 6a 3d 69 3b 20 28 6a 2b 6e 44 65 6c 29 3c  r(j=i; (j+nDel)<
2e2e8 3d 6e 54 79 70 65 3b 20 6a 2b 2b 29 7b 0a 20 20  =nType; j++){.  
2e2e9 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 5b            zType[
2e2ea 6a 5d 20 3d 20 7a 54 79 70 65 5b 6a 2b 6e 44 65  j] = zType[j+nDe
2e2eb 6c 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  l];.          }.
2e2ec 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54            if( zT
2e2ed 79 70 65 5b 69 5d 3d 3d 27 5c 30 27 20 26 26 20  ype[i]=='\0' && 
2e2ee 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  i>0 ){.         
2e2ef 20 20 20 61 73 73 65 72 74 28 7a 54 79 70 65 5b     assert(zType[
2e2f0 69 2d 31 5d 3d 3d 27 20 27 29 3b 0a 20 20 20 20  i-1]==' ');.    
2e2f1 20 20 20 20 20 20 20 20 7a 54 79 70 65 5b 69 2d          zType[i-
2e2f2 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  1] = '\0';.     
2e2f3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2e2f4 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c   pTab->aCol[iCol
2e2f5 5d 2e 69 73 48 69 64 64 65 6e 20 3d 20 31 3b 0a  ].isHidden = 1;.
2e2f6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2e2f7 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73  }.    }.  }..  s
2e2f8 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2e2f9 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a 20   zModuleName);. 
2e2fa 20 64 62 2d 3e 70 56 54 61 62 20 3d 20 30 3b 0a   db->pVTab = 0;.
2e2fb 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
2e2fc 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2e2fd 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62  ion is invoked b
2e2fe 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20  y the parser to 
2e2ff 63 61 6c 6c 20 74 68 65 20 78 43 6f 6e 6e 65 63  call the xConnec
2e300 74 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66  t() method.** of
2e301 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2e302 6c 65 20 70 54 61 62 2e 20 49 66 20 61 6e 20 65  le pTab. If an e
2e303 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20  rror occurs, an 
2e304 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
2e305 74 75 72 6e 65 64 20 0a 2a 2a 20 61 6e 64 20 61  turned .** and a
2e306 6e 20 65 72 72 6f 72 20 6c 65 66 74 20 69 6e 20  n error left in 
2e307 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  pParse..**.** Th
2e308 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
2e309 6f 70 20 69 66 20 74 61 62 6c 65 20 70 54 61 62  op if table pTab
2e30a 20 69 73 20 6e 6f 74 20 61 20 76 69 72 74 75 61   is not a virtua
2e30b 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  l table..*/.SQLI
2e30c 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2e30d 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 6f  qlite3VtabCallCo
2e30e 6e 6e 65 63 74 28 50 61 72 73 65 20 2a 70 50 61  nnect(Parse *pPa
2e30f 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
2e310 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
2e311 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2e312 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f   const char *zMo
2e313 64 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f  d;.  Module *pMo
2e314 64 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  d;.  int rc;..  
2e315 61 73 73 65 72 74 28 20 70 54 61 62 20 29 3b 0a  assert( pTab );.
2e316 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74 61 62    if( (pTab->tab
2e317 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75  Flags & TF_Virtu
2e318 61 6c 29 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65  al)==0 || sqlite
2e319 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70  3GetVTable(db, p
2e31a 54 61 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75  Tab) ){.    retu
2e31b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
2e31c 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  }..  /* Locate t
2e31d 68 65 20 72 65 71 75 69 72 65 64 20 76 69 72 74  he required virt
2e31e 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65  ual table module
2e31f 20 2a 2f 0a 20 20 7a 4d 6f 64 20 3d 20 70 54 61   */.  zMod = pTa
2e320 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30  b->azModuleArg[0
2e321 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 4d 6f 64  ];.  pMod = (Mod
2e322 75 6c 65 2a 29 73 71 6c 69 74 65 33 48 61 73 68  ule*)sqlite3Hash
2e323 46 69 6e 64 28 26 64 62 2d 3e 61 4d 6f 64 75 6c  Find(&db->aModul
2e324 65 2c 20 7a 4d 6f 64 2c 20 73 71 6c 69 74 65 33  e, zMod, sqlite3
2e325 53 74 72 6c 65 6e 33 30 28 7a 4d 6f 64 29 29 3b  Strlen30(zMod));
2e326 0a 0a 20 20 69 66 28 20 21 70 4d 6f 64 20 29 7b  ..  if( !pMod ){
2e327 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
2e328 2a 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62 2d  *zModule = pTab-
2e329 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 3b  >azModuleArg[0];
2e32a 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2e32b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
2e32c 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73   such module: %s
2e32d 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20 20  ", zModule);.   
2e32e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
2e32f 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OR;.  }else{.   
2e330 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b   char *zErr = 0;
2e331 0a 20 20 20 20 72 63 20 3d 20 76 74 61 62 43 61  .    rc = vtabCa
2e332 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 64 62  llConstructor(db
2e333 2c 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70 4d  , pTab, pMod, pM
2e334 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f  od->pModule->xCo
2e335 6e 6e 65 63 74 2c 20 26 7a 45 72 72 29 3b 0a 20  nnect, &zErr);. 
2e336 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2e337 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71  E_OK ){.      sq
2e338 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2e339 61 72 73 65 2c 20 22 25 73 22 2c 20 7a 45 72 72  arse, "%s", zErr
2e33a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2e33b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2e33c 45 72 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  Err);.  }..  ret
2e33d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
2e33e 20 41 64 64 20 74 68 65 20 76 69 72 74 75 61 6c   Add the virtual
2e33f 20 74 61 62 6c 65 20 70 56 54 61 62 20 74 6f 20   table pVTab to 
2e340 74 68 65 20 61 72 72 61 79 20 73 71 6c 69 74 65  the array sqlite
2e341 33 2e 61 56 54 72 61 6e 73 5b 5d 2e 0a 2a 2f 0a  3.aVTrans[]..*/.
2e342 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 54 6f  static int addTo
2e343 56 54 72 61 6e 73 28 73 71 6c 69 74 65 33 20 2a  VTrans(sqlite3 *
2e344 64 62 2c 20 56 54 61 62 6c 65 20 2a 70 56 54 61  db, VTable *pVTa
2e345 62 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  b){.  const int 
2e346 41 52 52 41 59 5f 49 4e 43 52 20 3d 20 35 3b 0a  ARRAY_INCR = 5;.
2e347 0a 20 20 2f 2a 20 47 72 6f 77 20 74 68 65 20 73  .  /* Grow the s
2e348 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61  qlite3.aVTrans a
2e349 72 72 61 79 20 69 66 20 72 65 71 75 69 72 65 64  rray if required
2e34a 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 6e   */.  if( (db->n
2e34b 56 54 72 61 6e 73 25 41 52 52 41 59 5f 49 4e 43  VTrans%ARRAY_INC
2e34c 52 29 3d 3d 30 20 29 7b 0a 20 20 20 20 56 54 61  R)==0 ){.    VTa
2e34d 62 6c 65 20 2a 2a 61 56 54 72 61 6e 73 3b 0a 20  ble **aVTrans;. 
2e34e 20 20 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20     int nBytes = 
2e34f 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76  sizeof(sqlite3_v
2e350 74 61 62 20 2a 29 20 2a 20 28 64 62 2d 3e 6e 56  tab *) * (db->nV
2e351 54 72 61 6e 73 20 2b 20 41 52 52 41 59 5f 49 4e  Trans + ARRAY_IN
2e352 43 52 29 3b 0a 20 20 20 20 61 56 54 72 61 6e 73  CR);.    aVTrans
2e353 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
2e354 6c 6f 63 28 64 62 2c 20 28 76 6f 69 64 20 2a 29  loc(db, (void *)
2e355 64 62 2d 3e 61 56 54 72 61 6e 73 2c 20 6e 42 79  db->aVTrans, nBy
2e356 74 65 73 29 3b 0a 20 20 20 20 69 66 28 20 21 61  tes);.    if( !a
2e357 56 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 20 20  VTrans ){.      
2e358 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
2e359 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  MEM;.    }.    m
2e35a 65 6d 73 65 74 28 26 61 56 54 72 61 6e 73 5b 64  emset(&aVTrans[d
2e35b 62 2d 3e 6e 56 54 72 61 6e 73 5d 2c 20 30 2c 20  b->nVTrans], 0, 
2e35c 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 76  sizeof(sqlite3_v
2e35d 74 61 62 20 2a 29 2a 41 52 52 41 59 5f 49 4e 43  tab *)*ARRAY_INC
2e35e 52 29 3b 0a 20 20 20 20 64 62 2d 3e 61 56 54 72  R);.    db->aVTr
2e35f 61 6e 73 20 3d 20 61 56 54 72 61 6e 73 3b 0a 20  ans = aVTrans;. 
2e360 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 70 56 74   }..  /* Add pVt
2e361 61 62 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  ab to the end of
2e362 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73   sqlite3.aVTrans
2e363 20 2a 2f 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e   */.  db->aVTran
2e364 73 5b 64 62 2d 3e 6e 56 54 72 61 6e 73 2b 2b 5d  s[db->nVTrans++]
2e365 20 3d 20 70 56 54 61 62 3b 0a 20 20 73 71 6c 69   = pVTab;.  sqli
2e366 74 65 33 56 74 61 62 4c 6f 63 6b 28 70 56 54 61  te3VtabLock(pVTa
2e367 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  b);.  return SQL
2e368 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
2e369 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
2e36a 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 74 68 65  s invoked by the
2e36b 20 76 64 62 65 20 74 6f 20 63 61 6c 6c 20 74 68   vdbe to call th
2e36c 65 20 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64  e xCreate method
2e36d 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75  .** of the virtu
2e36e 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a  al table named z
2e36f 54 61 62 20 69 6e 20 64 61 74 61 62 61 73 65 20  Tab in database 
2e370 69 44 62 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61  iDb. .**.** If a
2e371 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
2e372 2a 70 7a 45 72 72 20 69 73 20 73 65 74 20 74 6f  *pzErr is set to
2e373 20 70 6f 69 6e 74 20 61 6e 20 61 6e 20 45 6e 67   point an an Eng
2e374 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 0a 2a 2a  lish language.**
2e375 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20   description of 
2e376 74 68 65 20 65 72 72 6f 72 20 61 6e 64 20 61 6e  the error and an
2e377 20 53 51 4c 49 54 45 5f 58 58 58 20 65 72 72 6f   SQLITE_XXX erro
2e378 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
2e379 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 69 73 20 63  ed..** In this c
2e37a 61 73 65 20 74 68 65 20 63 61 6c 6c 65 72 20 6d  ase the caller m
2e37b 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  ust call sqlite3
2e37c 44 62 46 72 65 65 28 64 62 2c 20 29 20 6f 6e 20  DbFree(db, ) on 
2e37d 2a 70 7a 45 72 72 2e 0a 2a 2f 0a 53 51 4c 49 54  *pzErr..*/.SQLIT
2e37e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2e37f 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65  lite3VtabCallCre
2e380 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ate(sqlite3 *db,
2e381 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20   int iDb, const 
2e382 63 68 61 72 20 2a 7a 54 61 62 2c 20 63 68 61 72  char *zTab, char
2e383 20 2a 2a 70 7a 45 72 72 29 7b 0a 20 20 69 6e 74   **pzErr){.  int
2e384 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
2e385 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
2e386 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a    Module *pMod;.
2e387 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d    const char *zM
2e388 6f 64 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71  od;..  pTab = sq
2e389 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
2e38a 62 2c 20 7a 54 61 62 2c 20 64 62 2d 3e 61 44 62  b, zTab, db->aDb
2e38b 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
2e38c 61 73 73 65 72 74 28 20 70 54 61 62 20 26 26 20  assert( pTab && 
2e38d 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20  (pTab->tabFlags 
2e38e 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30  & TF_Virtual)!=0
2e38f 20 26 26 20 21 70 54 61 62 2d 3e 70 56 54 61 62   && !pTab->pVTab
2e390 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61  le );..  /* Loca
2e391 74 65 20 74 68 65 20 72 65 71 75 69 72 65 64 20  te the required 
2e392 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
2e393 64 75 6c 65 20 2a 2f 0a 20 20 7a 4d 6f 64 20 3d  dule */.  zMod =
2e394 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41   pTab->azModuleA
2e395 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20  rg[0];.  pMod = 
2e396 28 4d 6f 64 75 6c 65 2a 29 73 71 6c 69 74 65 33  (Module*)sqlite3
2e397 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 4d  HashFind(&db->aM
2e398 6f 64 75 6c 65 2c 20 7a 4d 6f 64 2c 20 73 71 6c  odule, zMod, sql
2e399 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4d 6f  ite3Strlen30(zMo
2e39a 64 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  d));..  /* If th
2e39b 65 20 6d 6f 64 75 6c 65 20 68 61 73 20 62 65 65  e module has bee
2e39c 6e 20 72 65 67 69 73 74 65 72 65 64 20 61 6e 64  n registered and
2e39d 20 69 6e 63 6c 75 64 65 73 20 61 20 43 72 65 61   includes a Crea
2e39e 74 65 20 6d 65 74 68 6f 64 2c 20 0a 20 20 2a 2a  te method, .  **
2e39f 20 69 6e 76 6f 6b 65 20 69 74 20 6e 6f 77 2e 20   invoke it now. 
2e3a0 49 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 68 61  If the module ha
2e3a1 73 20 6e 6f 74 20 62 65 65 6e 20 72 65 67 69 73  s not been regis
2e3a2 74 65 72 65 64 2c 20 72 65 74 75 72 6e 20 61 6e  tered, return an
2e3a3 20 0a 20 20 2a 2a 20 65 72 72 6f 72 2e 20 4f 74   .  ** error. Ot
2e3a4 68 65 72 77 69 73 65 2c 20 64 6f 20 6e 6f 74 68  herwise, do noth
2e3a5 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ing..  */.  if( 
2e3a6 21 70 4d 6f 64 20 29 7b 0a 20 20 20 20 2a 70 7a  !pMod ){.    *pz
2e3a7 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Err = sqlite3MPr
2e3a8 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73 75 63  intf(db, "no suc
2e3a9 68 20 6d 6f 64 75 6c 65 3a 20 25 73 22 2c 20 7a  h module: %s", z
2e3aa 4d 6f 64 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  Mod);.    rc = S
2e3ab 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2e3ac 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 76  else{.    rc = v
2e3ad 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74  tabCallConstruct
2e3ae 6f 72 28 64 62 2c 20 70 54 61 62 2c 20 70 4d 6f  or(db, pTab, pMo
2e3af 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65  d, pMod->pModule
2e3b0 2d 3e 78 43 72 65 61 74 65 2c 20 70 7a 45 72 72  ->xCreate, pzErr
2e3b1 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 73  );.  }..  /* Jus
2e3b2 74 69 66 69 63 61 74 69 6f 6e 20 6f 66 20 41 4c  tification of AL
2e3b3 57 41 59 53 28 29 3a 20 20 54 68 65 20 78 43 6f  WAYS():  The xCo
2e3b4 6e 73 74 72 75 63 74 6f 72 20 6d 65 74 68 6f 64  nstructor method
2e3b5 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 0a   is required to.
2e3b6 20 20 2a 2a 20 63 72 65 61 74 65 20 61 20 76 61    ** create a va
2e3b7 6c 69 64 20 73 71 6c 69 74 65 33 5f 76 74 61 62  lid sqlite3_vtab
2e3b8 20 69 66 20 69 74 20 72 65 74 75 72 6e 73 20 53   if it returns S
2e3b9 51 4c 49 54 45 5f 4f 4b 2e 20 2a 2f 0a 20 20 69  QLITE_OK. */.  i
2e3ba 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
2e3bb 20 26 26 20 41 4c 57 41 59 53 28 73 71 6c 69 74   && ALWAYS(sqlit
2e3bc 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20  e3GetVTable(db, 
2e3bd 70 54 61 62 29 29 20 29 7b 0a 20 20 20 20 20 20  pTab)) ){.      
2e3be 72 63 20 3d 20 61 64 64 54 6f 56 54 72 61 6e 73  rc = addToVTrans
2e3bf 28 64 62 2c 20 73 71 6c 69 74 65 33 47 65 74 56  (db, sqlite3GetV
2e3c0 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 29  Table(db, pTab))
2e3c1 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
2e3c2 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
2e3c3 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
2e3c4 65 64 20 74 6f 20 73 65 74 20 74 68 65 20 73 63  ed to set the sc
2e3c5 68 65 6d 61 20 6f 66 20 61 20 76 69 72 74 75 61  hema of a virtua
2e3c6 6c 20 74 61 62 6c 65 2e 20 20 49 74 20 69 73 20  l table.  It is 
2e3c7 6f 6e 6c 79 0a 2a 2a 20 76 61 6c 69 64 20 74 6f  only.** valid to
2e3c8 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
2e3c9 69 6f 6e 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ion from within 
2e3ca 74 68 65 20 78 43 72 65 61 74 65 28 29 20 6f 72  the xCreate() or
2e3cb 20 78 43 6f 6e 6e 65 63 74 28 29 20 6f 66 20 61   xConnect() of a
2e3cc 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c  .** virtual tabl
2e3cd 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c  e module..*/.SQL
2e3ce 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2e3cf 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
2e3d0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
2e3d1 6e 73 74 20 63 68 61 72 20 2a 7a 43 72 65 61 74  nst char *zCreat
2e3d2 65 54 61 62 6c 65 29 7b 0a 20 20 50 61 72 73 65  eTable){.  Parse
2e3d3 20 2a 70 50 61 72 73 65 3b 0a 0a 20 20 69 6e 74   *pParse;..  int
2e3d4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
2e3d5 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a  .  Table *pTab;.
2e3d6 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30    char *zErr = 0
2e3d7 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ;..  sqlite3_mut
2e3d8 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2e3d9 65 78 29 3b 0a 20 20 70 54 61 62 20 3d 20 64 62  ex);.  pTab = db
2e3da 2d 3e 70 56 54 61 62 3b 0a 20 20 69 66 28 20 21  ->pVTab;.  if( !
2e3db 70 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69  pTab ){.    sqli
2e3dc 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
2e3dd 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29 3b 0a  ITE_MISUSE, 0);.
2e3de 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
2e3df 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2e3e0 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  x);.    return S
2e3e1 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
2e3e2 7d 0a 20 20 61 73 73 65 72 74 28 20 28 70 54 61  }.  assert( (pTa
2e3e3 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
2e3e4 5f 56 69 72 74 75 61 6c 29 21 3d 30 20 29 3b 0a  _Virtual)!=0 );.
2e3e5 0a 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c 69  .  pParse = sqli
2e3e6 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72  te3StackAllocZer
2e3e7 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50  o(db, sizeof(*pP
2e3e8 61 72 73 65 29 29 3b 0a 20 20 69 66 28 20 70 50  arse));.  if( pP
2e3e9 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  arse==0 ){.    r
2e3ea 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
2e3eb 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
2e3ec 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74  Parse->declareVt
2e3ed 61 62 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 72  ab = 1;.    pPar
2e3ee 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 0a  se->db = db;.  .
2e3ef 20 20 20 20 69 66 28 20 0a 20 20 20 20 20 20 20      if( .       
2e3f0 20 53 51 4c 49 54 45 5f 4f 4b 20 3d 3d 20 73 71   SQLITE_OK == sq
2e3f1 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 70  lite3RunParser(p
2e3f2 50 61 72 73 65 2c 20 7a 43 72 65 61 74 65 54 61  Parse, zCreateTa
2e3f3 62 6c 65 2c 20 26 7a 45 72 72 29 20 26 26 20 0a  ble, &zErr) && .
2e3f4 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e          pParse->
2e3f5 70 4e 65 77 54 61 62 6c 65 20 26 26 20 0a 20 20  pNewTable && .  
2e3f6 20 20 20 20 20 20 21 70 50 61 72 73 65 2d 3e 70        !pParse->p
2e3f7 4e 65 77 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63  NewTable->pSelec
2e3f8 74 20 26 26 20 0a 20 20 20 20 20 20 20 20 28 70  t && .        (p
2e3f9 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2e3fa 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
2e3fb 56 69 72 74 75 61 6c 29 3d 3d 30 0a 20 20 20 20  Virtual)==0.    
2e3fc 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70 54  ){.      if( !pT
2e3fd 61 62 2d 3e 61 43 6f 6c 20 29 7b 0a 20 20 20 20  ab->aCol ){.    
2e3fe 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 20 3d      pTab->aCol =
2e3ff 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
2e400 6c 65 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20  le->aCol;.      
2e401 20 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 70    pTab->nCol = p
2e402 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
2e403 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  ->nCol;.        
2e404 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2e405 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20  e->nCol = 0;.   
2e406 20 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65       pParse->pNe
2e407 77 54 61 62 6c 65 2d 3e 61 43 6f 6c 20 3d 20 30  wTable->aCol = 0
2e408 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2e409 64 62 2d 3e 70 56 54 61 62 20 3d 20 30 3b 0a 20  db->pVTab = 0;. 
2e40a 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
2e40b 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
2e40c 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c  b, SQLITE_ERROR,
2e40d 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 73 71   zErr);.      sq
2e40e 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2e40f 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 72 63 20  zErr);.      rc 
2e410 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2e411 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65      }.    pParse
2e412 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 20 3d 20  ->declareVtab = 
2e413 30 3b 0a 20 20 0a 20 20 20 20 69 66 28 20 70 50  0;.  .    if( pP
2e414 61 72 73 65 2d 3e 70 56 64 62 65 20 29 7b 0a 20  arse->pVdbe ){. 
2e415 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2e416 46 69 6e 61 6c 69 7a 65 28 70 50 61 72 73 65 2d  Finalize(pParse-
2e417 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 7d 0a 20  >pVdbe);.    }. 
2e418 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
2e419 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70 4e  Table(pParse->pN
2e41a 65 77 54 61 62 6c 65 29 3b 0a 20 20 20 20 73 71  ewTable);.    sq
2e41b 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64  lite3StackFree(d
2e41c 62 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a  b, pParse);.  }.
2e41d 0a 20 20 61 73 73 65 72 74 28 20 28 72 63 26 30  .  assert( (rc&0
2e41e 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20 72 63  xff)==rc );.  rc
2e41f 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
2e420 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
2e421 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2e422 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
2e423 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
2e424 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2e425 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20 74   is invoked by t
2e426 68 65 20 76 64 62 65 20 74 6f 20 63 61 6c 6c 20  he vdbe to call 
2e427 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74  the xDestroy met
2e428 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69  hod.** of the vi
2e429 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65  rtual table name
2e42a 64 20 7a 54 61 62 20 69 6e 20 64 61 74 61 62 61  d zTab in databa
2e42b 73 65 20 69 44 62 2e 20 54 68 69 73 20 6f 63 63  se iDb. This occ
2e42c 75 72 73 0a 2a 2a 20 77 68 65 6e 20 61 20 44 52  urs.** when a DR
2e42d 4f 50 20 54 41 42 4c 45 20 69 73 20 6d 65 6e 74  OP TABLE is ment
2e42e 69 6f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ioned..**.** Thi
2e42f 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f  s call is a no-o
2e430 70 20 69 66 20 7a 54 61 62 20 69 73 20 6e 6f 74  p if zTab is not
2e431 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
2e432 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2e433 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
2e434 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 73  tabCallDestroy(s
2e435 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
2e436 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  iDb, const char 
2e437 2a 7a 54 61 62 29 7b 0a 20 20 69 6e 74 20 72 63  *zTab){.  int rc
2e438 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
2e439 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a 20 20  Table *pTab;..  
2e43a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69  pTab = sqlite3Fi
2e43b 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62  ndTable(db, zTab
2e43c 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  , db->aDb[iDb].z
2e43d 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 41 4c 57  Name);.  if( ALW
2e43e 41 59 53 28 70 54 61 62 21 3d 30 20 26 26 20 70  AYS(pTab!=0 && p
2e43f 54 61 62 2d 3e 70 56 54 61 62 6c 65 21 3d 30 29  Tab->pVTable!=0)
2e440 20 29 7b 0a 20 20 20 20 56 54 61 62 6c 65 20 2a   ){.    VTable *
2e441 70 20 3d 20 76 74 61 62 44 69 73 63 6f 6e 6e 65  p = vtabDisconne
2e442 63 74 41 6c 6c 28 64 62 2c 20 70 54 61 62 29 3b  ctAll(db, pTab);
2e443 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
2e444 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
2e445 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d  .    assert( rc=
2e446 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
2e447 20 20 72 63 20 3d 20 70 2d 3e 70 4d 6f 64 2d 3e    rc = p->pMod->
2e448 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73 74 72 6f  pModule->xDestro
2e449 79 28 70 2d 3e 70 56 74 61 62 29 3b 0a 20 20 20  y(p->pVtab);.   
2e44a 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2e44b 66 65 74 79 4f 6e 28 64 62 29 3b 0a 0a 20 20 20  fetyOn(db);..   
2e44c 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 73   /* Remove the s
2e44d 71 6c 69 74 65 33 5f 76 74 61 62 2a 20 66 72 6f  qlite3_vtab* fro
2e44e 6d 20 74 68 65 20 61 56 54 72 61 6e 73 5b 5d 20  m the aVTrans[] 
2e44f 61 72 72 61 79 2c 20 69 66 20 61 70 70 6c 69 63  array, if applic
2e450 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 66 28 20  able */.    if( 
2e451 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2e452 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2e453 54 61 62 2d 3e 70 56 54 61 62 6c 65 3d 3d 70 20  Tab->pVTable==p 
2e454 26 26 20 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 29  && p->pNext==0 )
2e455 3b 0a 20 20 20 20 20 20 70 2d 3e 70 56 74 61 62  ;.      p->pVtab
2e456 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 54 61 62   = 0;.      pTab
2e457 2d 3e 70 56 54 61 62 6c 65 20 3d 20 30 3b 0a 20  ->pVTable = 0;. 
2e458 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
2e459 55 6e 6c 6f 63 6b 28 70 29 3b 0a 20 20 20 20 7d  Unlock(p);.    }
2e45a 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
2e45b 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
2e45c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76 6f 6b 65   function invoke
2e45d 73 20 65 69 74 68 65 72 20 74 68 65 20 78 52 6f  s either the xRo
2e45e 6c 6c 62 61 63 6b 20 6f 72 20 78 43 6f 6d 6d 69  llback or xCommi
2e45f 74 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 65  t method.** of e
2e460 61 63 68 20 6f 66 20 74 68 65 20 76 69 72 74 75  ach of the virtu
2e461 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  al tables in the
2e462 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73   sqlite3.aVTrans
2e463 20 61 72 72 61 79 2e 20 54 68 65 20 6d 65 74 68   array. The meth
2e464 6f 64 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 73 20  od.** called is 
2e465 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68  identified by th
2e466 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
2e467 74 2c 20 22 6f 66 66 73 65 74 22 2c 20 77 68 69  t, "offset", whi
2e468 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 6f 66 66  ch is.** the off
2e469 73 65 74 20 6f 66 20 74 68 65 20 6d 65 74 68 6f  set of the metho
2e46a 64 20 74 6f 20 63 61 6c 6c 20 69 6e 20 74 68 65  d to call in the
2e46b 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
2e46c 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
2e46d 20 54 68 65 20 61 72 72 61 79 20 69 73 20 63 6c   The array is cl
2e46e 65 61 72 65 64 20 61 66 74 65 72 20 69 6e 76 6f  eared after invo
2e46f 6b 69 6e 67 20 74 68 65 20 63 61 6c 6c 62 61 63  king the callbac
2e470 6b 73 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ks. .*/.static v
2e471 6f 69 64 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65  oid callFinalise
2e472 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  r(sqlite3 *db, i
2e473 6e 74 20 6f 66 66 73 65 74 29 7b 0a 20 20 69 6e  nt offset){.  in
2e474 74 20 69 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61  t i;.  if( db->a
2e475 56 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 66 6f  VTrans ){.    fo
2e476 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54  r(i=0; i<db->nVT
2e477 72 61 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  rans; i++){.    
2e478 20 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 20    VTable *pVTab 
2e479 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d  = db->aVTrans[i]
2e47a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
2e47b 76 74 61 62 20 2a 70 20 3d 20 70 56 54 61 62 2d  vtab *p = pVTab-
2e47c 3e 70 56 74 61 62 3b 0a 20 20 20 20 20 20 69 66  >pVtab;.      if
2e47d 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ( p ){.        i
2e47e 6e 74 20 28 2a 78 29 28 73 71 6c 69 74 65 33 5f  nt (*x)(sqlite3_
2e47f 76 74 61 62 20 2a 29 3b 0a 20 20 20 20 20 20 20  vtab *);.       
2e480 20 78 20 3d 20 2a 28 69 6e 74 20 28 2a 2a 29 28   x = *(int (**)(
2e481 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 29  sqlite3_vtab *))
2e482 28 28 63 68 61 72 20 2a 29 70 2d 3e 70 4d 6f 64  ((char *)p->pMod
2e483 75 6c 65 20 2b 20 6f 66 66 73 65 74 29 3b 0a 20  ule + offset);. 
2e484 20 20 20 20 20 20 20 69 66 28 20 78 20 29 20 78         if( x ) x
2e485 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  (p);.      }.   
2e486 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e     sqlite3VtabUn
2e487 6c 6f 63 6b 28 70 56 54 61 62 29 3b 0a 20 20 20  lock(pVTab);.   
2e488 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
2e489 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 56 54  Free(db, db->aVT
2e48a 72 61 6e 73 29 3b 0a 20 20 20 20 64 62 2d 3e 6e  rans);.    db->n
2e48b 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 20 20  VTrans = 0;.    
2e48c 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 30 3b  db->aVTrans = 0;
2e48d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  .  }.}../*.** In
2e48e 76 6f 6b 65 20 74 68 65 20 78 53 79 6e 63 20 6d  voke the xSync m
2e48f 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69 72  ethod of all vir
2e490 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20 74  tual tables in t
2e491 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61  he sqlite3.aVTra
2e492 6e 73 0a 2a 2a 20 61 72 72 61 79 2e 20 52 65 74  ns.** array. Ret
2e493 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f  urn the error co
2e494 64 65 20 66 6f 72 20 74 68 65 20 66 69 72 73 74  de for the first
2e495 20 65 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75   error that occu
2e496 72 73 2c 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  rs, or.** SQLITE
2e497 5f 4f 4b 20 69 66 20 61 6c 6c 20 78 53 79 6e 63  _OK if all xSync
2e498 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20   operations are 
2e499 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a 0a 2a  successful..**.*
2e49a 2a 20 53 65 74 20 2a 70 7a 45 72 72 6d 73 67 20  * Set *pzErrmsg 
2e49b 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75  to point to a bu
2e49c 66 66 65 72 20 74 68 61 74 20 73 68 6f 75 6c 64  ffer that should
2e49d 20 62 65 20 72 65 6c 65 61 73 65 64 20 75 73 69   be released usi
2e49e 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62  ng .** sqlite3Db
2e49f 46 72 65 65 28 29 20 63 6f 6e 74 61 69 6e 69 6e  Free() containin
2e4a0 67 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  g an error messa
2e4a1 67 65 2c 20 69 66 20 6f 6e 65 20 69 73 20 61 76  ge, if one is av
2e4a2 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  ailable..*/.SQLI
2e4a3 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2e4a4 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 73  qlite3VtabSync(s
2e4a5 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72  qlite3 *db, char
2e4a6 20 2a 2a 70 7a 45 72 72 6d 73 67 29 7b 0a 20 20   **pzErrmsg){.  
2e4a7 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20  int i;.  int rc 
2e4a8 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
2e4a9 6e 74 20 72 63 73 61 66 65 74 79 3b 0a 20 20 56  nt rcsafety;.  V
2e4aa 54 61 62 6c 65 20 2a 2a 61 56 54 72 61 6e 73 20  Table **aVTrans 
2e4ab 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 3b 0a 0a  = db->aVTrans;..
2e4ac 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 61    rc = sqlite3Sa
2e4ad 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 64  fetyOff(db);.  d
2e4ae 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 30 3b 0a  b->aVTrans = 0;.
2e4af 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
2e4b0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
2e4b1 2d 3e 6e 56 54 72 61 6e 73 3b 20 69 2b 2b 29 7b  ->nVTrans; i++){
2e4b2 0a 20 20 20 20 69 6e 74 20 28 2a 78 29 28 73 71  .    int (*x)(sq
2e4b3 6c 69 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a 20  lite3_vtab *);. 
2e4b4 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20     sqlite3_vtab 
2e4b5 2a 70 56 74 61 62 20 3d 20 61 56 54 72 61 6e 73  *pVtab = aVTrans
2e4b6 5b 69 5d 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20  [i]->pVtab;.    
2e4b7 69 66 28 20 70 56 74 61 62 20 26 26 20 28 78 20  if( pVtab && (x 
2e4b8 3d 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  = pVtab->pModule
2e4b9 2d 3e 78 53 79 6e 63 29 21 3d 30 20 29 7b 0a 20  ->xSync)!=0 ){. 
2e4ba 20 20 20 20 20 72 63 20 3d 20 78 28 70 56 74 61       rc = x(pVta
2e4bb 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
2e4bc 33 44 62 46 72 65 65 28 64 62 2c 20 2a 70 7a 45  3DbFree(db, *pzE
2e4bd 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 2a 70  rrmsg);.      *p
2e4be 7a 45 72 72 6d 73 67 20 3d 20 70 56 74 61 62 2d  zErrmsg = pVtab-
2e4bf 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 20 20  >zErrMsg;.      
2e4c0 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d  pVtab->zErrMsg =
2e4c1 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
2e4c2 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 61 56  db->aVTrans = aV
2e4c3 54 72 61 6e 73 3b 0a 20 20 72 63 73 61 66 65 74  Trans;.  rcsafet
2e4c4 79 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74  y = sqlite3Safet
2e4c5 79 4f 6e 28 64 62 29 3b 0a 0a 20 20 69 66 28 20  yOn(db);..  if( 
2e4c6 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2e4c7 0a 20 20 20 20 72 63 20 3d 20 72 63 73 61 66 65  .    rc = rcsafe
2e4c8 74 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ty;.  }.  return
2e4c9 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e   rc;.}../*.** In
2e4ca 76 6f 6b 65 20 74 68 65 20 78 52 6f 6c 6c 62 61  voke the xRollba
2e4cb 63 6b 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c  ck method of all
2e4cc 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
2e4cd 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74  in the .** sqlit
2e4ce 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79  e3.aVTrans array
2e4cf 2e 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65  . Then clear the
2e4d0 20 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a   array itself..*
2e4d1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2e4d2 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62   int sqlite3Vtab
2e4d3 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33  Rollback(sqlite3
2e4d4 20 2a 64 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e   *db){.  callFin
2e4d5 61 6c 69 73 65 72 28 64 62 2c 20 6f 66 66 73 65  aliser(db, offse
2e4d6 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75  tof(sqlite3_modu
2e4d7 6c 65 2c 78 52 6f 6c 6c 62 61 63 6b 29 29 3b 0a  le,xRollback));.
2e4d8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2e4d9 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76  OK;.}../*.** Inv
2e4da 6f 6b 65 20 74 68 65 20 78 43 6f 6d 6d 69 74 20  oke the xCommit 
2e4db 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76 69  method of all vi
2e4dc 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 20  rtual tables in 
2e4dd 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 2e  the .** sqlite3.
2e4de 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20 54  aVTrans array. T
2e4df 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 61 72  hen clear the ar
2e4e0 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53  ray itself..*/.S
2e4e1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2e4e2 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d  t sqlite3VtabCom
2e4e3 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  mit(sqlite3 *db)
2e4e4 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69 73 65  {.  callFinalise
2e4e5 72 28 64 62 2c 20 6f 66 66 73 65 74 6f 66 28 73  r(db, offsetof(s
2e4e6 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c 78 43  qlite3_module,xC
2e4e7 6f 6d 6d 69 74 29 29 3b 0a 20 20 72 65 74 75 72  ommit));.  retur
2e4e8 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
2e4e9 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 69 72  /*.** If the vir
2e4ea 74 75 61 6c 20 74 61 62 6c 65 20 70 56 74 61 62  tual table pVtab
2e4eb 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 74 72   supports the tr
2e4ec 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 65 72 66  ansaction interf
2e4ed 61 63 65 0a 2a 2a 20 28 78 42 65 67 69 6e 2f 78  ace.** (xBegin/x
2e4ee 52 6f 6c 6c 62 61 63 6b 2f 78 43 6f 6d 6d 69 74  Rollback/xCommit
2e4ef 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79 20   and optionally 
2e4f0 78 53 79 6e 63 29 20 61 6e 64 20 61 20 74 72 61  xSync) and a tra
2e4f1 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e  nsaction is.** n
2e4f2 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65  ot currently ope
2e4f3 6e 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 42  n, invoke the xB
2e4f4 65 67 69 6e 20 6d 65 74 68 6f 64 20 6e 6f 77 2e  egin method now.
2e4f5 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 78 42  .**.** If the xB
2e4f6 65 67 69 6e 20 63 61 6c 6c 20 69 73 20 73 75 63  egin call is suc
2e4f7 63 65 73 73 66 75 6c 2c 20 70 6c 61 63 65 20 74  cessful, place t
2e4f8 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  he sqlite3_vtab 
2e4f9 70 6f 69 6e 74 65 72 0a 2a 2a 20 69 6e 20 74 68  pointer.** in th
2e4fa 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61 6e  e sqlite3.aVTran
2e4fb 73 20 61 72 72 61 79 2e 0a 2a 2f 0a 53 51 4c 49  s array..*/.SQLI
2e4fc 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
2e4fd 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28  qlite3VtabBegin(
2e4fe 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 56 54 61  sqlite3 *db, VTa
2e4ff 62 6c 65 20 2a 70 56 54 61 62 29 7b 0a 20 20 69  ble *pVTab){.  i
2e500 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
2e501 4b 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  K;.  const sqlit
2e502 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
2e503 6c 65 3b 0a 0a 20 20 2f 2a 20 53 70 65 63 69 61  le;..  /* Specia
2e504 6c 20 63 61 73 65 3a 20 49 66 20 64 62 2d 3e 61  l case: If db->a
2e505 56 54 72 61 6e 73 20 69 73 20 4e 55 4c 4c 20 61  VTrans is NULL a
2e506 6e 64 20 64 62 2d 3e 6e 56 54 72 61 6e 73 20 69  nd db->nVTrans i
2e507 73 20 67 72 65 61 74 65 72 0a 20 20 2a 2a 20 74  s greater.  ** t
2e508 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 74  han zero, then t
2e509 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
2e50a 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f  being called fro
2e50b 6d 20 77 69 74 68 69 6e 20 61 0a 20 20 2a 2a 20  m within a.  ** 
2e50c 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20 78  virtual module x
2e50d 53 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 2e  Sync() callback.
2e50e 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
2e50f 6f 20 77 72 69 74 65 20 74 6f 20 0a 20 20 2a 2a  o write to .  **
2e510 20 76 69 72 74 75 61 6c 20 6d 6f 64 75 6c 65 20   virtual module 
2e511 74 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20 63  tables in this c
2e512 61 73 65 2c 20 73 6f 20 72 65 74 75 72 6e 20 53  ase, so return S
2e513 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 20 20  QLITE_LOCKED..  
2e514 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
2e515 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 29  VtabInSync(db) )
2e516 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
2e517 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a  ITE_LOCKED;.  }.
2e518 20 20 69 66 28 20 21 70 56 54 61 62 20 29 7b 0a    if( !pVTab ){.
2e519 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2e51a 45 5f 4f 4b 3b 0a 20 20 7d 20 0a 20 20 70 4d 6f  E_OK;.  } .  pMo
2e51b 64 75 6c 65 20 3d 20 70 56 54 61 62 2d 3e 70 56  dule = pVTab->pV
2e51c 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20  tab->pModule;.. 
2e51d 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 42   if( pModule->xB
2e51e 65 67 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20  egin ){.    int 
2e51f 69 3b 0a 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70  i;...    /* If p
2e520 56 74 61 62 20 69 73 20 61 6c 72 65 61 64 79 20  Vtab is already 
2e521 69 6e 20 74 68 65 20 61 56 54 72 61 6e 73 20 61  in the aVTrans a
2e522 72 72 61 79 2c 20 72 65 74 75 72 6e 20 65 61 72  rray, return ear
2e523 6c 79 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d  ly */.    for(i=
2e524 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73  0; i<db->nVTrans
2e525 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
2e526 28 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d  ( db->aVTrans[i]
2e527 3d 3d 70 56 54 61 62 20 29 7b 0a 20 20 20 20 20  ==pVTab ){.     
2e528 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2e529 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  _OK;.      }.   
2e52a 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b   }..    /* Invok
2e52b 65 20 74 68 65 20 78 42 65 67 69 6e 20 6d 65 74  e the xBegin met
2e52c 68 6f 64 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  hod */.    rc = 
2e52d 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 67 69 6e 28  pModule->xBegin(
2e52e 70 56 54 61 62 2d 3e 70 56 74 61 62 29 3b 0a 20  pVTab->pVtab);. 
2e52f 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
2e530 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
2e531 20 3d 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64   = addToVTrans(d
2e532 62 2c 20 70 56 54 61 62 29 3b 0a 20 20 20 20 7d  b, pVTab);.    }
2e533 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
2e534 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
2e535 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 28  irst parameter (
2e536 70 44 65 66 29 20 69 73 20 61 20 66 75 6e 63 74  pDef) is a funct
2e537 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ion implementati
2e538 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f  on.  The.** seco
2e539 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 70 45  nd parameter (pE
2e53a 78 70 72 29 20 69 73 20 74 68 65 20 66 69 72 73  xpr) is the firs
2e53b 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
2e53c 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20  is function..** 
2e53d 49 66 20 70 45 78 70 72 20 69 73 20 61 20 63 6f  If pExpr is a co
2e53e 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61  lumn in a virtua
2e53f 6c 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6c 65  l table, then le
2e540 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a  t the virtual.**
2e541 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74   table implement
2e542 61 74 69 6f 6e 20 68 61 76 65 20 61 6e 20 6f 70  ation have an op
2e543 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 6f 76 65  portunity to ove
2e544 72 6c 6f 61 64 20 74 68 65 20 66 75 6e 63 74 69  rload the functi
2e545 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  on..**.** This r
2e546 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
2e547 6f 20 61 6c 6c 6f 77 20 76 69 72 74 75 61 6c 20  o allow virtual 
2e548 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
2e549 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 6f 76 65 72  tions to.** over
2e54a 6c 6f 61 64 20 4d 41 54 43 48 2c 20 4c 49 4b 45  load MATCH, LIKE
2e54b 2c 20 47 4c 4f 42 2c 20 61 6e 64 20 52 45 47 45  , GLOB, and REGE
2e54c 58 50 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a  XP operators..**
2e54d 0a 2a 2a 20 52 65 74 75 72 6e 20 65 69 74 68 65  .** Return eithe
2e54e 72 20 74 68 65 20 70 44 65 66 20 61 72 67 75 6d  r the pDef argum
2e54f 65 6e 74 20 28 69 6e 64 69 63 61 74 69 6e 67 20  ent (indicating 
2e550 6e 6f 20 63 68 61 6e 67 65 29 20 6f 72 20 61 20  no change) or a 
2e551 0a 2a 2a 20 6e 65 77 20 46 75 6e 63 44 65 66 20  .** new FuncDef 
2e552 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 69  structure that i
2e553 73 20 6d 61 72 6b 65 64 20 61 73 20 65 70 68 65  s marked as ephe
2e554 6d 65 72 61 6c 20 75 73 69 6e 67 20 74 68 65 0a  meral using the.
2e555 2a 2a 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45  ** SQLITE_FUNC_E
2e556 50 48 45 4d 20 66 6c 61 67 2e 0a 2a 2f 0a 53 51  PHEM flag..*/.SQ
2e557 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e  LITE_PRIVATE Fun
2e558 63 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74 61  cDef *sqlite3Vta
2e559 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f  bOverloadFunctio
2e55a 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n(.  sqlite3 *db
2e55b 2c 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  ,    /* Database
2e55c 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20   connection for 
2e55d 72 65 70 6f 72 74 69 6e 67 20 6d 61 6c 6c 6f 63  reporting malloc
2e55e 20 70 72 6f 62 6c 65 6d 73 20 2a 2f 0a 20 20 46   problems */.  F
2e55f 75 6e 63 44 65 66 20 2a 70 44 65 66 2c 20 20 2f  uncDef *pDef,  /
2e560 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 70 6f  * Function to po
2e561 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20  ssibly overload 
2e562 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20  */.  int nArg,  
2e563 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2e564 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  f arguments to t
2e565 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  he function */. 
2e566 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20   Expr *pExpr    
2e567 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65   /* First argume
2e568 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
2e569 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65  on */.){.  Table
2e56a 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65   *pTab;.  sqlite
2e56b 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20  3_vtab *pVtab;. 
2e56c 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
2e56d 2a 70 4d 6f 64 3b 0a 20 20 76 6f 69 64 20 28 2a  *pMod;.  void (*
2e56e 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
2e56f 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
2e570 74 65 33 5f 76 61 6c 75 65 2a 2a 29 20 3d 20 30  te3_value**) = 0
2e571 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20 3d  ;.  void *pArg =
2e572 20 30 3b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70   0;.  FuncDef *p
2e573 4e 65 77 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  New;.  int rc = 
2e574 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 77 65  0;.  char *zLowe
2e575 72 4e 61 6d 65 3b 0a 20 20 75 6e 73 69 67 6e 65  rName;.  unsigne
2e576 64 20 63 68 61 72 20 2a 7a 3b 0a 0a 0a 20 20 2f  d char *z;...  /
2e577 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 74  * Check to see t
2e578 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  he left operand 
2e579 69 73 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61  is a column in a
2e57a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
2e57b 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 45  /.  if( NEVER(pE
2e57c 78 70 72 3d 3d 30 29 20 29 20 72 65 74 75 72 6e  xpr==0) ) return
2e57d 20 70 44 65 66 3b 0a 20 20 69 66 28 20 70 45 78   pDef;.  if( pEx
2e57e 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  pr->op!=TK_COLUM
2e57f 4e 20 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b  N ) return pDef;
2e580 0a 20 20 70 54 61 62 20 3d 20 70 45 78 70 72 2d  .  pTab = pExpr-
2e581 3e 70 54 61 62 3b 0a 20 20 69 66 28 20 4e 45 56  >pTab;.  if( NEV
2e582 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20 72 65  ER(pTab==0) ) re
2e583 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 69 66 28  turn pDef;.  if(
2e584 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
2e585 20 26 20 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d   & TF_Virtual)==
2e586 30 20 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b  0 ) return pDef;
2e587 0a 20 20 70 56 74 61 62 20 3d 20 73 71 6c 69 74  .  pVtab = sqlit
2e588 65 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20  e3GetVTable(db, 
2e589 70 54 61 62 29 2d 3e 70 56 74 61 62 3b 0a 20 20  pTab)->pVtab;.  
2e58a 61 73 73 65 72 74 28 20 70 56 74 61 62 21 3d 30  assert( pVtab!=0
2e58b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56   );.  assert( pV
2e58c 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 21 3d 30 20  tab->pModule!=0 
2e58d 29 3b 0a 20 20 70 4d 6f 64 20 3d 20 28 73 71 6c  );.  pMod = (sql
2e58e 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56  ite3_module *)pV
2e58f 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  tab->pModule;.  
2e590 69 66 28 20 70 4d 6f 64 2d 3e 78 46 69 6e 64 46  if( pMod->xFindF
2e591 75 6e 63 74 69 6f 6e 3d 3d 30 20 29 20 72 65 74  unction==0 ) ret
2e592 75 72 6e 20 70 44 65 66 3b 0a 20 0a 20 20 2f 2a  urn pDef;. .  /*
2e593 20 43 61 6c 6c 20 74 68 65 20 78 46 69 6e 64 46   Call the xFindF
2e594 75 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64 20 6f  unction method o
2e595 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  n the virtual ta
2e596 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
2e597 6f 6e 0a 20 20 2a 2a 20 74 6f 20 73 65 65 20 69  on.  ** to see i
2e598 66 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  f the implementa
2e599 74 69 6f 6e 20 77 61 6e 74 73 20 74 6f 20 6f 76  tion wants to ov
2e59a 65 72 6c 6f 61 64 20 74 68 69 73 20 66 75 6e 63  erload this func
2e59b 74 69 6f 6e 20 0a 20 20 2a 2f 0a 20 20 7a 4c 6f  tion .  */.  zLo
2e59c 77 65 72 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  werName = sqlite
2e59d 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 44  3DbStrDup(db, pD
2e59e 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66  ef->zName);.  if
2e59f 28 20 7a 4c 6f 77 65 72 4e 61 6d 65 20 29 7b 0a  ( zLowerName ){.
2e5a0 20 20 20 20 66 6f 72 28 7a 3d 28 75 6e 73 69 67      for(z=(unsig
2e5a1 6e 65 64 20 63 68 61 72 2a 29 7a 4c 6f 77 65 72  ned char*)zLower
2e5a2 4e 61 6d 65 3b 20 2a 7a 3b 20 7a 2b 2b 29 7b 0a  Name; *z; z++){.
2e5a3 20 20 20 20 20 20 2a 7a 20 3d 20 73 71 6c 69 74        *z = sqlit
2e5a4 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  e3UpperToLower[*
2e5a5 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  z];.    }.    rc
2e5a6 20 3d 20 70 4d 6f 64 2d 3e 78 46 69 6e 64 46 75   = pMod->xFindFu
2e5a7 6e 63 74 69 6f 6e 28 70 56 74 61 62 2c 20 6e 41  nction(pVtab, nA
2e5a8 72 67 2c 20 7a 4c 6f 77 65 72 4e 61 6d 65 2c 20  rg, zLowerName, 
2e5a9 26 78 46 75 6e 63 2c 20 26 70 41 72 67 29 3b 0a  &xFunc, &pArg);.
2e5aa 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2e5ab 65 28 64 62 2c 20 7a 4c 6f 77 65 72 4e 61 6d 65  e(db, zLowerName
2e5ac 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
2e5ad 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
2e5ae 20 70 44 65 66 3b 0a 20 20 7d 0a 0a 20 20 2f 2a   pDef;.  }..  /*
2e5af 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 65 70   Create a new ep
2e5b0 68 65 6d 65 72 61 6c 20 66 75 6e 63 74 69 6f 6e  hemeral function
2e5b1 20 64 65 66 69 6e 69 74 69 6f 6e 20 66 6f 72 20   definition for 
2e5b2 74 68 65 20 6f 76 65 72 6c 6f 61 64 65 64 0a 20  the overloaded. 
2e5b3 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a   ** function */.
2e5b4 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
2e5b5 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
2e5b6 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 0a 20   sizeof(*pNew). 
2e5b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e5b8 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 73 71              + sq
2e5b9 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44  lite3Strlen30(pD
2e5ba 65 66 2d 3e 7a 4e 61 6d 65 29 20 2b 20 31 29 3b  ef->zName) + 1);
2e5bb 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
2e5bc 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 44 65  {.    return pDe
2e5bd 66 3b 0a 20 20 7d 0a 20 20 2a 70 4e 65 77 20 3d  f;.  }.  *pNew =
2e5be 20 2a 70 44 65 66 3b 0a 20 20 70 4e 65 77 2d 3e   *pDef;.  pNew->
2e5bf 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 20 2a 29  zName = (char *)
2e5c0 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63  &pNew[1];.  memc
2e5c1 70 79 28 70 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20  py(pNew->zName, 
2e5c2 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c  pDef->zName, sql
2e5c3 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 44 65  ite3Strlen30(pDe
2e5c4 66 2d 3e 7a 4e 61 6d 65 29 2b 31 29 3b 0a 20 20  f->zName)+1);.  
2e5c5 70 4e 65 77 2d 3e 78 46 75 6e 63 20 3d 20 78 46  pNew->xFunc = xF
2e5c6 75 6e 63 3b 0a 20 20 70 4e 65 77 2d 3e 70 55 73  unc;.  pNew->pUs
2e5c7 65 72 44 61 74 61 20 3d 20 70 41 72 67 3b 0a 20  erData = pArg;. 
2e5c8 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c 3d 20   pNew->flags |= 
2e5c9 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45  SQLITE_FUNC_EPHE
2e5ca 4d 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77  M;.  return pNew
2e5cb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  ;.}../*.** Make 
2e5cc 73 75 72 65 20 76 69 72 74 75 61 6c 20 74 61 62  sure virtual tab
2e5cd 6c 65 20 70 54 61 62 20 69 73 20 63 6f 6e 74 61  le pTab is conta
2e5ce 69 6e 65 64 20 69 6e 20 74 68 65 20 70 50 61 72  ined in the pPar
2e5cf 73 65 2d 3e 61 70 56 69 72 74 75 61 6c 4c 6f 63  se->apVirtualLoc
2e5d0 6b 5b 5d 0a 2a 2a 20 61 72 72 61 79 20 73 6f 20  k[].** array so 
2e5d1 74 68 61 74 20 61 6e 20 4f 50 5f 56 42 65 67 69  that an OP_VBegi
2e5d2 6e 20 77 69 6c 6c 20 67 65 74 20 67 65 6e 65 72  n will get gener
2e5d3 61 74 65 64 20 66 6f 72 20 69 74 2e 20 20 41 64  ated for it.  Ad
2e5d4 64 20 70 54 61 62 20 74 6f 20 74 68 65 0a 2a 2a  d pTab to the.**
2e5d5 20 61 72 72 61 79 20 69 66 20 69 74 20 69 73 20   array if it is 
2e5d6 6d 69 73 73 69 6e 67 2e 20 20 49 66 20 70 54 61  missing.  If pTa
2e5d7 62 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  b is already in 
2e5d8 74 68 65 20 61 72 72 61 79 2c 20 74 68 69 73 20  the array, this 
2e5d9 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20  routine.** is a 
2e5da 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45  no-op..*/.SQLITE
2e5db 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2e5dc 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69  lite3VtabMakeWri
2e5dd 74 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61  table(Parse *pPa
2e5de 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62  rse, Table *pTab
2e5df 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 54 6f 70  ){.  Parse *pTop
2e5e0 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65 33 50  level = sqlite3P
2e5e1 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61  arseToplevel(pPa
2e5e2 72 73 65 29 3b 0a 20 20 69 6e 74 20 69 2c 20 6e  rse);.  int i, n
2e5e3 3b 0a 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74  ;.  Table **apVt
2e5e4 61 62 4c 6f 63 6b 3b 0a 0a 20 20 61 73 73 65 72  abLock;..  asser
2e5e5 74 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  t( IsVirtual(pTa
2e5e6 62 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  b) );.  for(i=0;
2e5e7 20 69 3c 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 56   i<pToplevel->nV
2e5e8 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20  tabLock; i++){. 
2e5e9 20 20 20 69 66 28 20 70 54 61 62 3d 3d 70 54 6f     if( pTab==pTo
2e5ea 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f  plevel->apVtabLo
2e5eb 63 6b 5b 69 5d 20 29 20 72 65 74 75 72 6e 3b 0a  ck[i] ) return;.
2e5ec 20 20 7d 0a 20 20 6e 20 3d 20 28 70 54 6f 70 6c    }.  n = (pTopl
2e5ed 65 76 65 6c 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b  evel->nVtabLock+
2e5ee 31 29 2a 73 69 7a 65 6f 66 28 70 54 6f 70 6c 65  1)*sizeof(pTople
2e5ef 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 5b  vel->apVtabLock[
2e5f0 30 5d 29 3b 0a 20 20 61 70 56 74 61 62 4c 6f 63  0]);.  apVtabLoc
2e5f1 6b 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  k = sqlite3_real
2e5f2 6c 6f 63 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 61  loc(pToplevel->a
2e5f3 70 56 74 61 62 4c 6f 63 6b 2c 20 6e 29 3b 0a 20  pVtabLock, n);. 
2e5f4 20 69 66 28 20 61 70 56 74 61 62 4c 6f 63 6b 20   if( apVtabLock 
2e5f5 29 7b 0a 20 20 20 20 70 54 6f 70 6c 65 76 65 6c  ){.    pToplevel
2e5f6 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 20 3d 20 61  ->apVtabLock = a
2e5f7 70 56 74 61 62 4c 6f 63 6b 3b 0a 20 20 20 20 70  pVtabLock;.    p
2e5f8 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61 62  Toplevel->apVtab
2e5f9 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76 65 6c 2d 3e  Lock[pToplevel->
2e5fa 6e 56 74 61 62 4c 6f 63 6b 2b 2b 5d 20 3d 20 70  nVtabLock++] = p
2e5fb 54 61 62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Tab;.  }else{.  
2e5fc 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62 2d    pToplevel->db-
2e5fd 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2e5fe 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66  1;.  }.}..#endif
2e5ff 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2e600 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
2e601 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2e602 20 45 6e 64 20 6f 66 20 76 74 61 62 2e 63 20 2a   End of vtab.c *
2e603 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e604 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e605 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2e606 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2e607 20 42 65 67 69 6e 20 66 69 6c 65 20 77 68 65 72   Begin file wher
2e608 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
2e609 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e60a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2e60b 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
2e60c 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
2e60d 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
2e60e 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
2e60f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
2e610 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
2e611 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
2e612 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
2e613 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
2e614 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
2e615 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
2e616 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
2e617 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
2e618 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
2e619 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
2e61a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
2e61b 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
2e61c 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
2e61d 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2e61e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e61f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e621 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e622 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
2e623 6d 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 73 20  module contains 
2e624 43 20 63 6f 64 65 20 74 68 61 74 20 67 65 6e 65  C code that gene
2e625 72 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20  rates VDBE code 
2e626 75 73 65 64 20 74 6f 20 70 72 6f 63 65 73 73 0a  used to process.
2e627 2a 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  ** the WHERE cla
2e628 75 73 65 20 6f 66 20 53 51 4c 20 73 74 61 74 65  use of SQL state
2e629 6d 65 6e 74 73 2e 20 20 54 68 69 73 20 6d 6f 64  ments.  This mod
2e62a 75 6c 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ule is responsib
2e62b 6c 65 20 66 6f 72 0a 2a 2a 20 67 65 6e 65 72 61  le for.** genera
2e62c 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20 74 68  ting the code th
2e62d 61 74 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68  at loops through
2e62e 20 61 20 74 61 62 6c 65 20 6c 6f 6f 6b 69 6e 67   a table looking
2e62f 20 66 6f 72 20 61 70 70 6c 69 63 61 62 6c 65 0a   for applicable.
2e630 2a 2a 20 72 6f 77 73 2e 20 20 49 6e 64 69 63 65  ** rows.  Indice
2e631 73 20 61 72 65 20 73 65 6c 65 63 74 65 64 20 61  s are selected a
2e632 6e 64 20 75 73 65 64 20 74 6f 20 73 70 65 65 64  nd used to speed
2e633 20 74 68 65 20 73 65 61 72 63 68 20 77 68 65 6e   the search when
2e634 20 64 6f 69 6e 67 0a 2a 2a 20 73 6f 20 69 73 20   doing.** so is 
2e635 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 42 65 63  applicable.  Bec
2e636 61 75 73 65 20 74 68 69 73 20 6d 6f 64 75 6c 65  ause this module
2e637 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
2e638 66 6f 72 20 73 65 6c 65 63 74 69 6e 67 0a 2a 2a  for selecting.**
2e639 20 69 6e 64 69 63 65 73 2c 20 79 6f 75 20 6d 69   indices, you mi
2e63a 67 68 74 20 61 6c 73 6f 20 74 68 69 6e 6b 20 6f  ght also think o
2e63b 66 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 73  f this module as
2e63c 20 74 68 65 20 22 71 75 65 72 79 20 6f 70 74 69   the "query opti
2e63d 6d 69 7a 65 72 22 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  mizer"..*/../*.*
2e63e 2a 20 54 72 61 63 65 20 6f 75 74 70 75 74 20 6d  * Trace output m
2e63f 61 63 72 6f 73 0a 2a 2f 0a 23 69 66 20 64 65 66  acros.*/.#if def
2e640 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
2e641 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
2e642 49 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54  ITE_DEBUG).SQLIT
2e643 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2e644 6c 69 74 65 33 57 68 65 72 65 54 72 61 63 65 20  lite3WhereTrace 
2e645 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20  = 0;.#endif.#if 
2e646 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
2e647 45 53 54 29 20 26 26 20 64 65 66 69 6e 65 64 28  EST) && defined(
2e648 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 23 20  SQLITE_DEBUG).# 
2e649 64 65 66 69 6e 65 20 57 48 45 52 45 54 52 41 43  define WHERETRAC
2e64a 45 28 58 29 20 20 69 66 28 73 71 6c 69 74 65 33  E(X)  if(sqlite3
2e64b 57 68 65 72 65 54 72 61 63 65 29 20 73 71 6c 69  WhereTrace) sqli
2e64c 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 58  te3DebugPrintf X
2e64d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
2e64e 57 48 45 52 45 54 52 41 43 45 28 58 29 0a 23 65  WHERETRACE(X).#e
2e64f 6e 64 69 66 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  ndif../* Forward
2e650 20 72 65 66 65 72 65 6e 63 65 0a 2a 2f 0a 74 79   reference.*/.ty
2e651 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65  pedef struct Whe
2e652 72 65 43 6c 61 75 73 65 20 57 68 65 72 65 43 6c  reClause WhereCl
2e653 61 75 73 65 3b 0a 74 79 70 65 64 65 66 20 73 74  ause;.typedef st
2e654 72 75 63 74 20 57 68 65 72 65 4d 61 73 6b 53 65  ruct WhereMaskSe
2e655 74 20 57 68 65 72 65 4d 61 73 6b 53 65 74 3b 0a  t WhereMaskSet;.
2e656 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
2e657 68 65 72 65 4f 72 49 6e 66 6f 20 57 68 65 72 65  hereOrInfo Where
2e658 4f 72 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20  OrInfo;.typedef 
2e659 73 74 72 75 63 74 20 57 68 65 72 65 41 6e 64 49  struct WhereAndI
2e65a 6e 66 6f 20 57 68 65 72 65 41 6e 64 49 6e 66 6f  nfo WhereAndInfo
2e65b 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
2e65c 20 57 68 65 72 65 43 6f 73 74 20 57 68 65 72 65   WhereCost Where
2e65d 43 6f 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  Cost;../*.** The
2e65e 20 71 75 65 72 79 20 67 65 6e 65 72 61 74 6f 72   query generator
2e65f 20 75 73 65 73 20 61 6e 20 61 72 72 61 79 20 6f   uses an array o
2e660 66 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 74  f instances of t
2e661 68 69 73 20 73 74 72 75 63 74 75 72 65 20 74 6f  his structure to
2e662 0a 2a 2a 20 68 65 6c 70 20 69 74 20 61 6e 61 6c  .** help it anal
2e663 79 7a 65 20 74 68 65 20 73 75 62 65 78 70 72 65  yze the subexpre
2e664 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 57 48  ssions of the WH
2e665 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45 61 63  ERE clause.  Eac
2e666 68 20 57 48 45 52 45 0a 2a 2a 20 63 6c 61 75 73  h WHERE.** claus
2e667 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20  e subexpression 
2e668 69 73 20 73 65 70 61 72 61 74 65 64 20 66 72 6f  is separated fro
2e669 6d 20 74 68 65 20 6f 74 68 65 72 73 20 62 79 20  m the others by 
2e66a 41 4e 44 20 6f 70 65 72 61 74 6f 72 73 2c 0a 2a  AND operators,.*
2e66b 2a 20 75 73 75 61 6c 6c 79 2c 20 6f 72 20 73 6f  * usually, or so
2e66c 6d 65 74 69 6d 65 73 20 73 75 62 65 78 70 72 65  metimes subexpre
2e66d 73 73 69 6f 6e 73 20 73 65 70 61 72 61 74 65 64  ssions separated
2e66e 20 62 79 20 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 6c   by OR..**.** Al
2e66f 6c 20 57 68 65 72 65 54 65 72 6d 73 20 61 72 65  l WhereTerms are
2e670 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20   collected into 
2e671 61 20 73 69 6e 67 6c 65 20 57 68 65 72 65 43 6c  a single WhereCl
2e672 61 75 73 65 20 73 74 72 75 63 74 75 72 65 2e 20  ause structure. 
2e673 20 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69   .** The followi
2e674 6e 67 20 69 64 65 6e 74 69 74 79 20 68 6f 6c 64  ng identity hold
2e675 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
2e676 57 68 65 72 65 54 65 72 6d 2e 70 57 43 2d 3e 61  WhereTerm.pWC->a
2e677 5b 57 68 65 72 65 54 65 72 6d 2e 69 64 78 5d 20  [WhereTerm.idx] 
2e678 3d 3d 20 57 68 65 72 65 54 65 72 6d 0a 2a 2a 0a  == WhereTerm.**.
2e679 2a 2a 20 57 68 65 6e 20 61 20 74 65 72 6d 20 69  ** When a term i
2e67a 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a  s of the form:.*
2e67b 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  *.**            
2e67c 20 20 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 0a    X <op> <expr>.
2e67d 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 58 20 69 73  **.** where X is
2e67e 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61   a column name a
2e67f 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f  nd <op> is one o
2e680 66 20 63 65 72 74 61 69 6e 20 6f 70 65 72 61 74  f certain operat
2e681 6f 72 73 2c 0a 2a 2a 20 74 68 65 6e 20 57 68 65  ors,.** then Whe
2e682 72 65 54 65 72 6d 2e 6c 65 66 74 43 75 72 73 6f  reTerm.leftCurso
2e683 72 20 61 6e 64 20 57 68 65 72 65 54 65 72 6d 2e  r and WhereTerm.
2e684 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 72 65 63  u.leftColumn rec
2e685 6f 72 64 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f  ord the.** curso
2e686 72 20 6e 75 6d 62 65 72 20 61 6e 64 20 63 6f 6c  r number and col
2e687 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 58  umn number for X
2e688 2e 20 20 57 68 65 72 65 54 65 72 6d 2e 65 4f 70  .  WhereTerm.eOp
2e689 65 72 61 74 6f 72 20 72 65 63 6f 72 64 73 0a 2a  erator records.*
2e68a 2a 20 74 68 65 20 3c 6f 70 3e 20 75 73 69 6e 67  * the <op> using
2e68b 20 61 20 62 69 74 6d 61 73 6b 20 65 6e 63 6f 64   a bitmask encod
2e68c 69 6e 67 20 64 65 66 69 6e 65 64 20 62 79 20 57  ing defined by W
2e68d 4f 5f 78 78 78 20 62 65 6c 6f 77 2e 20 20 54 68  O_xxx below.  Th
2e68e 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 20 62 69  e.** use of a bi
2e68f 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 66  tmask encoding f
2e690 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20  or the operator 
2e691 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 73 65 61  allows us to sea
2e692 72 63 68 0a 2a 2a 20 71 75 69 63 6b 6c 79 20 66  rch.** quickly f
2e693 6f 72 20 74 65 72 6d 73 20 74 68 61 74 20 6d 61  or terms that ma
2e694 74 63 68 20 61 6e 79 20 6f 66 20 73 65 76 65 72  tch any of sever
2e695 61 6c 20 64 69 66 66 65 72 65 6e 74 20 6f 70 65  al different ope
2e696 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 20  rators..**.** A 
2e697 57 68 65 72 65 54 65 72 6d 20 6d 69 67 68 74 20  WhereTerm might 
2e698 61 6c 73 6f 20 62 65 20 74 77 6f 20 6f 72 20 6d  also be two or m
2e699 6f 72 65 20 73 75 62 74 65 72 6d 73 20 63 6f 6e  ore subterms con
2e69a 6e 65 63 74 65 64 20 62 79 20 4f 52 3a 0a 2a 2a  nected by OR:.**
2e69b 0a 2a 2a 20 20 20 20 20 20 20 20 20 28 74 31 2e  .**         (t1.
2e69c 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 29 20 4f  X <op> <expr>) O
2e69d 52 20 28 74 31 2e 59 20 3c 6f 70 3e 20 3c 65 78  R (t1.Y <op> <ex
2e69e 70 72 3e 29 20 4f 52 20 2e 2e 2e 2e 0a 2a 2a 0a  pr>) OR .....**.
2e69f 2a 2a 20 49 6e 20 74 68 69 73 20 73 65 63 6f 6e  ** In this secon
2e6a0 64 20 63 61 73 65 2c 20 77 74 46 6c 61 67 20 61  d case, wtFlag a
2e6a1 73 20 74 68 65 20 54 45 52 4d 5f 4f 52 49 4e 46  s the TERM_ORINF
2e6a2 4f 20 73 65 74 20 61 6e 64 20 65 4f 70 65 72 61  O set and eOpera
2e6a3 74 6f 72 3d 3d 57 4f 5f 4f 52 0a 2a 2a 20 61 6e  tor==WO_OR.** an
2e6a4 64 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 2e  d the WhereTerm.
2e6a5 75 2e 70 4f 72 49 6e 66 6f 20 66 69 65 6c 64 20  u.pOrInfo field 
2e6a6 70 6f 69 6e 74 73 20 74 6f 20 61 75 78 69 6c 69  points to auxili
2e6a7 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ary information 
2e6a8 74 68 61 74 0a 2a 2a 20 69 73 20 63 6f 6c 6c 65  that.** is colle
2e6a9 63 74 65 64 20 61 62 6f 75 74 20 74 68 65 0a 2a  cted about the.*
2e6aa 2a 0a 2a 2a 20 49 66 20 61 20 74 65 72 6d 20 69  *.** If a term i
2e6ab 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
2e6ac 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63  se does not matc
2e6ad 68 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20  h either of the 
2e6ae 74 77 6f 20 70 72 65 76 69 6f 75 73 0a 2a 2a 20  two previous.** 
2e6af 63 61 74 65 67 6f 72 69 65 73 2c 20 74 68 65 6e  categories, then
2e6b0 20 65 4f 70 65 72 61 74 6f 72 3d 3d 30 2e 20 20   eOperator==0.  
2e6b1 54 68 65 20 57 68 65 72 65 54 65 72 6d 2e 70 45  The WhereTerm.pE
2e6b2 78 70 72 20 66 69 65 6c 64 20 69 73 20 73 74 69  xpr field is sti
2e6b3 6c 6c 20 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65  ll set.** to the
2e6b4 20 6f 72 69 67 69 6e 61 6c 20 73 75 62 65 78 70   original subexp
2e6b5 72 65 73 73 69 6f 6e 20 63 6f 6e 74 65 6e 74 20  ression content 
2e6b6 61 6e 64 20 77 74 46 6c 61 67 73 20 69 73 20 73  and wtFlags is s
2e6b7 65 74 20 75 70 20 61 70 70 72 6f 70 72 69 61 74  et up appropriat
2e6b8 65 6c 79 0a 2a 2a 20 62 75 74 20 6e 6f 20 6f 74  ely.** but no ot
2e6b9 68 65 72 20 66 69 65 6c 64 73 20 69 6e 20 74 68  her fields in th
2e6ba 65 20 57 68 65 72 65 54 65 72 6d 20 6f 62 6a 65  e WhereTerm obje
2e6bb 63 74 20 61 72 65 20 6d 65 61 6e 69 6e 67 66 75  ct are meaningfu
2e6bc 6c 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 65 4f  l..**.** When eO
2e6bd 70 65 72 61 74 6f 72 21 3d 30 2c 20 70 72 65 72  perator!=0, prer
2e6be 65 71 52 69 67 68 74 20 61 6e 64 20 70 72 65 72  eqRight and prer
2e6bf 65 71 41 6c 6c 20 72 65 63 6f 72 64 20 73 65 74  eqAll record set
2e6c0 73 20 6f 66 20 63 75 72 73 6f 72 20 6e 75 6d 62  s of cursor numb
2e6c1 65 72 73 2c 0a 2a 2a 20 62 75 74 20 74 68 65 79  ers,.** but they
2e6c2 20 64 6f 20 73 6f 20 69 6e 64 69 72 65 63 74 6c   do so indirectl
2e6c3 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 57 68 65  y.  A single Whe
2e6c4 72 65 4d 61 73 6b 53 65 74 20 73 74 72 75 63 74  reMaskSet struct
2e6c5 75 72 65 20 74 72 61 6e 73 6c 61 74 65 73 0a 2a  ure translates.*
2e6c6 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * cursor number 
2e6c7 69 6e 74 6f 20 62 69 74 73 20 61 6e 64 20 74 68  into bits and th
2e6c8 65 20 74 72 61 6e 73 6c 61 74 65 64 20 62 69 74  e translated bit
2e6c9 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68   is stored in th
2e6ca 65 20 70 72 65 72 65 71 0a 2a 2a 20 66 69 65 6c  e prereq.** fiel
2e6cb 64 73 2e 20 20 54 68 65 20 74 72 61 6e 73 6c 61  ds.  The transla
2e6cc 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 20  tion is used in 
2e6cd 6f 72 64 65 72 20 74 6f 20 6d 61 78 69 6d 69 7a  order to maximiz
2e6ce 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  e the number of.
2e6cf 2a 2a 20 62 69 74 73 20 74 68 61 74 20 77 69 6c  ** bits that wil
2e6d0 6c 20 66 69 74 20 69 6e 20 61 20 42 69 74 6d 61  l fit in a Bitma
2e6d1 73 6b 2e 20 20 54 68 65 20 56 44 42 45 20 63 75  sk.  The VDBE cu
2e6d2 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d 69 67  rsor numbers mig
2e6d3 68 74 20 62 65 0a 2a 2a 20 73 70 72 65 61 64 20  ht be.** spread 
2e6d4 6f 75 74 20 6f 76 65 72 20 74 68 65 20 6e 6f 6e  out over the non
2e6d5 2d 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67 65  -negative intege
2e6d6 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  rs.  For example
2e6d7 2c 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20  , the cursor.** 
2e6d8 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20 62 65  numbers might be
2e6d9 20 33 2c 20 38 2c 20 39 2c 20 31 30 2c 20 32 30   3, 8, 9, 10, 20
2e6da 2c 20 32 33 2c 20 34 31 2c 20 61 6e 64 20 34 35  , 23, 41, and 45
2e6db 2e 20 20 54 68 65 20 57 68 65 72 65 4d 61 73 6b  .  The WhereMask
2e6dc 53 65 74 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65  Set.** translate
2e6dd 73 20 74 68 65 73 65 20 73 70 61 72 73 65 20 63  s these sparse c
2e6de 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e  ursor numbers in
2e6df 74 6f 20 63 6f 6e 73 65 63 75 74 69 76 65 20 69  to consecutive i
2e6e0 6e 74 65 67 65 72 73 0a 2a 2a 20 62 65 67 69 6e  ntegers.** begin
2e6e1 6e 69 6e 67 20 77 69 74 68 20 30 20 69 6e 20 6f  ning with 0 in o
2e6e2 72 64 65 72 20 74 6f 20 6d 61 6b 65 20 74 68 65  rder to make the
2e6e3 20 62 65 73 74 20 70 6f 73 73 69 62 6c 65 20 75   best possible u
2e6e4 73 65 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61  se of the availa
2e6e5 62 6c 65 0a 2a 2a 20 62 69 74 73 20 69 6e 20 74  ble.** bits in t
2e6e6 68 65 20 42 69 74 6d 61 73 6b 2e 20 20 53 6f 2c  he Bitmask.  So,
2e6e7 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20   in the example 
2e6e8 61 62 6f 76 65 2c 20 74 68 65 20 63 75 72 73 6f  above, the curso
2e6e9 72 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77 6f 75  r numbers.** wou
2e6ea 6c 64 20 62 65 20 6d 61 70 70 65 64 20 69 6e 74  ld be mapped int
2e6eb 6f 20 69 6e 74 65 67 65 72 73 20 30 20 74 68 72  o integers 0 thr
2e6ec 6f 75 67 68 20 37 2e 0a 2a 2a 0a 2a 2a 20 54 68  ough 7..**.** Th
2e6ed 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  e number of term
2e6ee 73 20 69 6e 20 61 20 6a 6f 69 6e 20 69 73 20 6c  s in a join is l
2e6ef 69 6d 69 74 65 64 20 62 79 20 74 68 65 20 6e 75  imited by the nu
2e6f0 6d 62 65 72 20 6f 66 20 62 69 74 73 0a 2a 2a 20  mber of bits.** 
2e6f1 69 6e 20 70 72 65 72 65 71 52 69 67 68 74 20 61  in prereqRight a
2e6f2 6e 64 20 70 72 65 72 65 71 41 6c 6c 2e 20 20 54  nd prereqAll.  T
2e6f3 68 65 20 64 65 66 61 75 6c 74 20 69 73 20 36 34  he default is 64
2e6f4 20 62 69 74 73 2c 20 68 65 6e 63 65 20 53 51 4c   bits, hence SQL
2e6f5 69 74 65 0a 2a 2a 20 69 73 20 6f 6e 6c 79 20 61  ite.** is only a
2e6f6 62 6c 65 20 74 6f 20 70 72 6f 63 65 73 73 20 6a  ble to process j
2e6f7 6f 69 6e 73 20 77 69 74 68 20 36 34 20 6f 72 20  oins with 64 or 
2e6f8 66 65 77 65 72 20 74 61 62 6c 65 73 2e 0a 2a 2f  fewer tables..*/
2e6f9 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2e6fa 57 68 65 72 65 54 65 72 6d 20 57 68 65 72 65 54  WhereTerm WhereT
2e6fb 65 72 6d 3b 0a 73 74 72 75 63 74 20 57 68 65 72  erm;.struct Wher
2e6fc 65 54 65 72 6d 20 7b 0a 20 20 45 78 70 72 20 2a  eTerm {.  Expr *
2e6fd 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  pExpr;          
2e6fe 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
2e6ff 74 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f  the subexpressio
2e700 6e 20 74 68 61 74 20 69 73 20 74 68 69 73 20 74  n that is this t
2e701 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61  erm */.  int iPa
2e702 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  rent;           
2e703 20 2f 2a 20 44 69 73 61 62 6c 65 20 70 57 43 2d   /* Disable pWC-
2e704 3e 61 5b 69 50 61 72 65 6e 74 5d 20 77 68 65 6e  >a[iParent] when
2e705 20 74 68 69 73 20 74 65 72 6d 20 64 69 73 61 62   this term disab
2e706 6c 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 66  led */.  int lef
2e707 74 43 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20  tCursor;        
2e708 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
2e709 72 20 6f 66 20 58 20 69 6e 20 22 58 20 3c 6f 70  r of X in "X <op
2e70a 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a 20 20 75  > <expr>" */.  u
2e70b 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 6e 74 20 6c  nion {.    int l
2e70c 65 66 74 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  eftColumn;      
2e70d 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d     /* Column num
2e70e 62 65 72 20 6f 66 20 58 20 69 6e 20 22 58 20 3c  ber of X in "X <
2e70f 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a 2f 0a 20  op> <expr>" */. 
2e710 20 20 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a     WhereOrInfo *
2e711 70 4f 72 49 6e 66 6f 3b 20 20 20 2f 2a 20 45 78  pOrInfo;   /* Ex
2e712 74 72 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  tra information 
2e713 69 66 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  if eOperator==WO
2e714 5f 4f 52 20 2a 2f 0a 20 20 20 20 57 68 65 72 65  _OR */.    Where
2e715 41 6e 64 49 6e 66 6f 20 2a 70 41 6e 64 49 6e 66  AndInfo *pAndInf
2e716 6f 3b 20 2f 2a 20 45 78 74 72 61 20 69 6e 66 6f  o; /* Extra info
2e717 72 6d 61 74 69 6f 6e 20 69 66 20 65 4f 70 65 72  rmation if eOper
2e718 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 2a 2f 0a  ator==WO_AND */.
2e719 20 20 7d 20 75 3b 0a 20 20 75 31 36 20 65 4f 70    } u;.  u16 eOp
2e71a 65 72 61 74 6f 72 3b 20 20 20 20 20 20 20 20 20  erator;         
2e71b 20 2f 2a 20 41 20 57 4f 5f 78 78 20 76 61 6c 75   /* A WO_xx valu
2e71c 65 20 64 65 73 63 72 69 62 69 6e 67 20 3c 6f 70  e describing <op
2e71d 3e 20 2a 2f 0a 20 20 75 38 20 77 74 46 6c 61 67  > */.  u8 wtFlag
2e71e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  s;             /
2e71f 2a 20 54 45 52 4d 5f 78 78 78 20 62 69 74 20 66  * TERM_xxx bit f
2e720 6c 61 67 73 2e 20 20 53 65 65 20 62 65 6c 6f 77  lags.  See below
2e721 20 2a 2f 0a 20 20 75 38 20 6e 43 68 69 6c 64 3b   */.  u8 nChild;
2e722 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e723 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 69 6c 64   Number of child
2e724 72 65 6e 20 74 68 61 74 20 6d 75 73 74 20 64 69  ren that must di
2e725 73 61 62 6c 65 20 75 73 20 2a 2f 0a 20 20 57 68  sable us */.  Wh
2e726 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20  ereClause *pWC; 
2e727 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6c 61        /* The cla
2e728 75 73 65 20 74 68 69 73 20 74 65 72 6d 20 69 73  use this term is
2e729 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 42 69   part of */.  Bi
2e72a 74 6d 61 73 6b 20 70 72 65 72 65 71 52 69 67 68  tmask prereqRigh
2e72b 74 3b 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b  t;    /* Bitmask
2e72c 20 6f 66 20 74 61 62 6c 65 73 20 75 73 65 64 20   of tables used 
2e72d 62 79 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  by pExpr->pRight
2e72e 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72   */.  Bitmask pr
2e72f 65 72 65 71 41 6c 6c 3b 20 20 20 20 20 20 2f 2a  ereqAll;      /*
2e730 20 42 69 74 6d 61 73 6b 20 6f 66 20 74 61 62 6c   Bitmask of tabl
2e731 65 73 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  es referenced by
2e732 20 70 45 78 70 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   pExpr */.};../*
2e733 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
2e734 65 73 20 6f 66 20 57 68 65 72 65 54 65 72 6d 2e  es of WhereTerm.
2e735 77 74 46 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69  wtFlags.*/.#defi
2e736 6e 65 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20  ne TERM_DYNAMIC 
2e737 20 20 20 30 78 30 31 20 20 20 2f 2a 20 4e 65 65     0x01   /* Nee
2e738 64 20 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65  d to call sqlite
2e739 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2e73a 70 45 78 70 72 29 20 2a 2f 0a 23 64 65 66 69 6e  pExpr) */.#defin
2e73b 65 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 20  e TERM_VIRTUAL  
2e73c 20 20 30 78 30 32 20 20 20 2f 2a 20 41 64 64 65    0x02   /* Adde
2e73d 64 20 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a  d by the optimiz
2e73e 65 72 2e 20 20 44 6f 20 6e 6f 74 20 63 6f 64 65  er.  Do not code
2e73f 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d   */.#define TERM
2e740 5f 43 4f 44 45 44 20 20 20 20 20 20 30 78 30 34  _CODED      0x04
2e741 20 20 20 2f 2a 20 54 68 69 73 20 74 65 72 6d 20     /* This term 
2e742 69 73 20 61 6c 72 65 61 64 79 20 63 6f 64 65 64  is already coded
2e743 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d   */.#define TERM
2e744 5f 43 4f 50 49 45 44 20 20 20 20 20 30 78 30 38  _COPIED     0x08
2e745 20 20 20 2f 2a 20 48 61 73 20 61 20 63 68 69 6c     /* Has a chil
2e746 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52  d */.#define TER
2e747 4d 5f 4f 52 49 4e 46 4f 20 20 20 20 20 30 78 31  M_ORINFO     0x1
2e748 30 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20 66  0   /* Need to f
2e749 72 65 65 20 74 68 65 20 57 68 65 72 65 54 65 72  ree the WhereTer
2e74a 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 6f 62 6a 65  m.u.pOrInfo obje
2e74b 63 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45  ct */.#define TE
2e74c 52 4d 5f 41 4e 44 49 4e 46 4f 20 20 20 20 30 78  RM_ANDINFO    0x
2e74d 32 30 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20  20   /* Need to 
2e74e 66 72 65 65 20 74 68 65 20 57 68 65 72 65 54 65  free the WhereTe
2e74f 72 6d 2e 75 2e 70 41 6e 64 49 6e 66 6f 20 6f 62  rm.u.pAndInfo ob
2e750 6a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52  j */.#define TER
2e751 4d 5f 4f 52 5f 4f 4b 20 20 20 20 20 20 30 78 34  M_OR_OK      0x4
2e752 30 20 20 20 2f 2a 20 55 73 65 64 20 64 75 72 69  0   /* Used duri
2e753 6e 67 20 4f 52 2d 63 6c 61 75 73 65 20 70 72 6f  ng OR-clause pro
2e754 63 65 73 73 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a  cessing */../*.*
2e755 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
2e756 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
2e757 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61  tructure holds a
2e758 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ll information a
2e759 62 6f 75 74 20 61 0a 2a 2a 20 57 48 45 52 45 20  bout a.** WHERE 
2e75a 63 6c 61 75 73 65 2e 20 20 4d 6f 73 74 6c 79 20  clause.  Mostly 
2e75b 74 68 69 73 20 69 73 20 61 20 63 6f 6e 74 61 69  this is a contai
2e75c 6e 65 72 20 66 6f 72 20 6f 6e 65 20 6f 72 20 6d  ner for one or m
2e75d 6f 72 65 20 57 68 65 72 65 54 65 72 6d 73 2e 0a  ore WhereTerms..
2e75e 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 43  */.struct WhereC
2e75f 6c 61 75 73 65 20 7b 0a 20 20 50 61 72 73 65 20  lause {.  Parse 
2e760 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20  *pParse;        
2e761 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
2e762 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68   context */.  Wh
2e763 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2e764 6b 53 65 74 3b 20 20 2f 2a 20 4d 61 70 70 69 6e  kSet;  /* Mappin
2e765 67 20 6f 66 20 74 61 62 6c 65 20 63 75 72 73 6f  g of table curso
2e766 72 20 6e 75 6d 62 65 72 73 20 74 6f 20 62 69 74  r numbers to bit
2e767 6d 61 73 6b 73 20 2a 2f 0a 20 20 42 69 74 6d 61  masks */.  Bitma
2e768 73 6b 20 76 6d 61 73 6b 3b 20 20 20 20 20 20 20  sk vmask;       
2e769 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 69      /* Bitmask i
2e76a 64 65 6e 74 69 66 79 69 6e 67 20 76 69 72 74 75  dentifying virtu
2e76b 61 6c 20 74 61 62 6c 65 20 63 75 72 73 6f 72 73  al table cursors
2e76c 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20   */.  u8 op;    
2e76d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e76e 2a 20 53 70 6c 69 74 20 6f 70 65 72 61 74 6f 72  * Split operator
2e76f 2e 20 20 54 4b 5f 41 4e 44 20 6f 72 20 54 4b 5f  .  TK_AND or TK_
2e770 4f 52 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72  OR */.  int nTer
2e771 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
2e772 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65   /* Number of te
2e773 72 6d 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 6c  rms */.  int nSl
2e774 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ot;             
2e775 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
2e776 6e 74 72 69 65 73 20 69 6e 20 61 5b 5d 20 2a 2f  ntries in a[] */
2e777 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b  .  WhereTerm *a;
2e778 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
2e779 61 63 68 20 61 5b 5d 20 64 65 73 63 72 69 62 65  ach a[] describe
2e77a 73 20 61 20 74 65 72 6d 20 6f 66 20 74 68 65 20  s a term of the 
2e77b 57 48 45 52 45 20 63 6c 75 61 73 65 20 2a 2f 0a  WHERE cluase */.
2e77c 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
2e77d 54 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 4b 29 0a  TE_SMALL_STACK).
2e77e 20 20 57 68 65 72 65 54 65 72 6d 20 61 53 74 61    WhereTerm aSta
2e77f 74 69 63 5b 31 5d 3b 20 20 20 20 2f 2a 20 49 6e  tic[1];    /* In
2e780 69 74 69 61 6c 20 73 74 61 74 69 63 20 73 70 61  itial static spa
2e781 63 65 20 66 6f 72 20 61 5b 5d 20 2a 2f 0a 23 65  ce for a[] */.#e
2e782 6c 73 65 0a 20 20 57 68 65 72 65 54 65 72 6d 20  lse.  WhereTerm 
2e783 61 53 74 61 74 69 63 5b 38 5d 3b 20 20 20 20 2f  aStatic[8];    /
2e784 2a 20 49 6e 69 74 69 61 6c 20 73 74 61 74 69 63  * Initial static
2e785 20 73 70 61 63 65 20 66 6f 72 20 61 5b 5d 20 2a   space for a[] *
2e786 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a  /.#endif.};../*.
2e787 2a 2a 20 41 20 57 68 65 72 65 54 65 72 6d 20 77  ** A WhereTerm w
2e788 69 74 68 20 65 4f 70 65 72 61 74 6f 72 3d 3d 57  ith eOperator==W
2e789 4f 5f 4f 52 20 68 61 73 20 69 74 73 20 75 2e 70  O_OR has its u.p
2e78a 4f 72 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 73  OrInfo pointer s
2e78b 65 74 20 74 6f 0a 2a 2a 20 61 20 64 79 6e 61 6d  et to.** a dynam
2e78c 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
2e78d 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
2e78e 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
2e78f 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ture..*/.struct 
2e790 57 68 65 72 65 4f 72 49 6e 66 6f 20 7b 0a 20 20  WhereOrInfo {.  
2e791 57 68 65 72 65 43 6c 61 75 73 65 20 77 63 3b 20  WhereClause wc; 
2e792 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 63 6f           /* Deco
2e793 6d 70 6f 73 69 74 69 6f 6e 20 69 6e 74 6f 20 73  mposition into s
2e794 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20 42 69 74  ubterms */.  Bit
2e795 6d 61 73 6b 20 69 6e 64 65 78 61 62 6c 65 3b 20  mask indexable; 
2e796 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b        /* Bitmask
2e797 20 6f 66 20 61 6c 6c 20 69 6e 64 65 78 61 62 6c   of all indexabl
2e798 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  e tables in the 
2e799 63 6c 61 75 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  clause */.};../*
2e79a 0a 2a 2a 20 41 20 57 68 65 72 65 54 65 72 6d 20  .** A WhereTerm 
2e79b 77 69 74 68 20 65 4f 70 65 72 61 74 6f 72 3d 3d  with eOperator==
2e79c 57 4f 5f 41 4e 44 20 68 61 73 20 69 74 73 20 75  WO_AND has its u
2e79d 2e 70 41 6e 64 49 6e 66 6f 20 70 6f 69 6e 74 65  .pAndInfo pointe
2e79e 72 20 73 65 74 20 74 6f 0a 2a 2a 20 61 20 64 79  r set to.** a dy
2e79f 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
2e7a0 74 65 64 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  ted instance of 
2e7a1 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
2e7a2 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
2e7a3 63 74 20 57 68 65 72 65 41 6e 64 49 6e 66 6f 20  ct WhereAndInfo 
2e7a4 7b 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  {.  WhereClause 
2e7a5 77 63 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  wc;          /* 
2e7a6 54 68 65 20 73 75 62 65 78 70 72 65 73 73 69 6f  The subexpressio
2e7a7 6e 20 62 72 6f 6b 65 6e 20 6f 75 74 20 2a 2f 0a  n broken out */.
2e7a8 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
2e7a9 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
2e7aa 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
2e7ab 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20   keeps track of 
2e7ac 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20 62 65 74  a mapping.** bet
2e7ad 77 65 65 6e 20 56 44 42 45 20 63 75 72 73 6f 72  ween VDBE cursor
2e7ae 20 6e 75 6d 62 65 72 73 20 61 6e 64 20 62 69 74   numbers and bit
2e7af 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61 73 6b  s of the bitmask
2e7b0 73 20 69 6e 20 57 68 65 72 65 54 65 72 6d 2e 0a  s in WhereTerm..
2e7b1 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20 63  **.** The VDBE c
2e7b2 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72  ursor numbers ar
2e7b3 65 20 73 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73  e small integers
2e7b4 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 0a 2a   contained in .*
2e7b5 2a 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 2e 69  * SrcList_item.i
2e7b6 43 75 72 73 6f 72 20 61 6e 64 20 45 78 70 72 2e  Cursor and Expr.
2e7b7 69 54 61 62 6c 65 20 66 69 65 6c 64 73 2e 20 20  iTable fields.  
2e7b8 46 6f 72 20 61 6e 79 20 67 69 76 65 6e 20 57 48  For any given WH
2e7b9 45 52 45 20 0a 2a 2a 20 63 6c 61 75 73 65 2c 20  ERE .** clause, 
2e7ba 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  the cursor numbe
2e7bb 72 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 67  rs might not beg
2e7bc 69 6e 20 77 69 74 68 20 30 20 61 6e 64 20 74 68  in with 0 and th
2e7bd 65 79 20 6d 69 67 68 74 0a 2a 2a 20 63 6f 6e 74  ey might.** cont
2e7be 61 69 6e 20 67 61 70 73 20 69 6e 20 74 68 65 20  ain gaps in the 
2e7bf 6e 75 6d 62 65 72 69 6e 67 20 73 65 71 75 65 6e  numbering sequen
2e7c0 63 65 2e 20 20 42 75 74 20 77 65 20 77 61 6e 74  ce.  But we want
2e7c1 20 74 6f 20 6d 61 6b 65 20 6d 61 78 69 6d 75 6d   to make maximum
2e7c2 0a 2a 2a 20 75 73 65 20 6f 66 20 74 68 65 20 62  .** use of the b
2e7c3 69 74 73 20 69 6e 20 6f 75 72 20 62 69 74 6d 61  its in our bitma
2e7c4 73 6b 73 2e 20 20 54 68 69 73 20 73 74 72 75 63  sks.  This struc
2e7c5 74 75 72 65 20 70 72 6f 76 69 64 65 73 20 61 20  ture provides a 
2e7c6 6d 61 70 70 69 6e 67 0a 2a 2a 20 66 72 6f 6d 20  mapping.** from 
2e7c7 74 68 65 20 73 70 61 72 73 65 20 63 75 72 73 6f  the sparse curso
2e7c8 72 20 6e 75 6d 62 65 72 73 20 69 6e 74 6f 20 63  r numbers into c
2e7c9 6f 6e 73 65 63 75 74 69 76 65 20 69 6e 74 65 67  onsecutive integ
2e7ca 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a  ers beginning.**
2e7cb 20 77 69 74 68 20 30 2e 0a 2a 2a 0a 2a 2a 20 49   with 0..**.** I
2e7cc 66 20 57 68 65 72 65 4d 61 73 6b 53 65 74 2e 69  f WhereMaskSet.i
2e7cd 78 5b 41 5d 3d 3d 42 20 69 74 20 6d 65 61 6e 73  x[A]==B it means
2e7ce 20 74 68 61 74 20 54 68 65 20 41 2d 74 68 20 62   that The A-th b
2e7cf 69 74 20 6f 66 20 61 20 42 69 74 6d 61 73 6b 0a  it of a Bitmask.
2e7d0 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 56  ** corresponds V
2e7d1 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
2e7d2 72 20 42 2e 20 20 54 68 65 20 41 2d 74 68 20 62  r B.  The A-th b
2e7d3 69 74 20 6f 66 20 61 20 62 69 74 6d 61 73 6b 20  it of a bitmask 
2e7d4 69 73 20 31 3c 3c 41 2e 0a 2a 2a 0a 2a 2a 20 46  is 1<<A..**.** F
2e7d5 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74  or example, if t
2e7d6 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2e7d7 65 78 70 72 65 73 73 69 6f 6e 20 75 73 65 64 20  expression used 
2e7d8 74 68 65 73 65 20 56 44 42 45 0a 2a 2a 20 63 75  these VDBE.** cu
2e7d9 72 73 6f 72 73 3a 20 20 34 2c 20 35 2c 20 38 2c  rsors:  4, 5, 8,
2e7da 20 32 39 2c 20 35 37 2c 20 37 33 2e 20 20 54 68   29, 57, 73.  Th
2e7db 65 6e 20 74 68 65 20 20 57 68 65 72 65 4d 61 73  en the  WhereMas
2e7dc 6b 53 65 74 20 73 74 72 75 63 74 75 72 65 0a 2a  kSet structure.*
2e7dd 2a 20 77 6f 75 6c 64 20 6d 61 70 20 74 68 6f 73  * would map thos
2e7de 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
2e7df 20 69 6e 74 6f 20 62 69 74 73 20 30 20 74 68 72   into bits 0 thr
2e7e0 6f 75 67 68 20 35 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  ough 5..**.** No
2e7e1 74 65 20 74 68 61 74 20 74 68 65 20 6d 61 70 70  te that the mapp
2e7e2 69 6e 67 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  ing is not neces
2e7e3 73 61 72 69 6c 79 20 6f 72 64 65 72 65 64 2e 20  sarily ordered. 
2e7e4 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 0a   In the example.
2e7e5 2a 2a 20 61 62 6f 76 65 2c 20 74 68 65 20 6d 61  ** above, the ma
2e7e6 70 70 69 6e 67 20 6d 69 67 68 74 20 67 6f 20 6c  pping might go l
2e7e7 69 6b 65 20 74 68 69 73 3a 20 20 34 2d 3e 33 2c  ike this:  4->3,
2e7e8 20 35 2d 3e 31 2c 20 38 2d 3e 32 2c 20 32 39 2d   5->1, 8->2, 29-
2e7e9 3e 30 2c 0a 2a 2a 20 35 37 2d 3e 35 2c 20 37 33  >0,.** 57->5, 73
2e7ea 2d 3e 34 2e 20 20 4f 72 20 6f 6e 65 20 6f 66 20  ->4.  Or one of 
2e7eb 37 31 39 20 6f 74 68 65 72 20 63 6f 6d 62 69 6e  719 other combin
2e7ec 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 62 65 20  ations might be 
2e7ed 75 73 65 64 2e 20 49 74 0a 2a 2a 20 64 6f 65 73  used. It.** does
2e7ee 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6d 61 74 74   not really matt
2e7ef 65 72 2e 20 20 57 68 61 74 20 69 73 20 69 6d 70  er.  What is imp
2e7f0 6f 72 74 61 6e 74 20 69 73 20 74 68 61 74 20 73  ortant is that s
2e7f1 70 61 72 73 65 20 63 75 72 73 6f 72 0a 2a 2a 20  parse cursor.** 
2e7f2 6e 75 6d 62 65 72 73 20 61 6c 6c 20 67 65 74 20  numbers all get 
2e7f3 6d 61 70 70 65 64 20 69 6e 74 6f 20 62 69 74 20  mapped into bit 
2e7f4 6e 75 6d 62 65 72 73 20 74 68 61 74 20 62 65 67  numbers that beg
2e7f5 69 6e 20 77 69 74 68 20 30 20 61 6e 64 20 63 6f  in with 0 and co
2e7f6 6e 74 61 69 6e 0a 2a 2a 20 6e 6f 20 67 61 70 73  ntain.** no gaps
2e7f7 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
2e7f8 65 4d 61 73 6b 53 65 74 20 7b 0a 20 20 69 6e 74  eMaskSet {.  int
2e7f9 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   n;             
2e7fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2e7fb 6d 62 65 72 20 6f 66 20 61 73 73 69 67 6e 65 64  mber of assigned
2e7fc 20 63 75 72 73 6f 72 20 76 61 6c 75 65 73 20 2a   cursor values *
2e7fd 2f 0a 20 20 69 6e 74 20 69 78 5b 42 4d 53 5d 3b  /.  int ix[BMS];
2e7fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e7ff 20 20 2f 2a 20 43 75 72 73 6f 72 20 61 73 73 69    /* Cursor assi
2e800 67 6e 65 64 20 74 6f 20 65 61 63 68 20 62 69 74  gned to each bit
2e801 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
2e802 57 68 65 72 65 43 6f 73 74 20 6f 62 6a 65 63 74  WhereCost object
2e803 20 72 65 63 6f 72 64 73 20 61 20 6c 6f 6f 6b 75   records a looku
2e804 70 20 73 74 72 61 74 65 67 79 20 61 6e 64 20 74  p strategy and t
2e805 68 65 20 65 73 74 69 6d 61 74 65 64 0a 2a 2a 20  he estimated.** 
2e806 63 6f 73 74 20 6f 66 20 70 75 72 73 75 69 6e 67  cost of pursuing
2e807 20 74 68 61 74 20 73 74 72 61 74 65 67 79 2e 0a   that strategy..
2e808 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72 65 43  */.struct WhereC
2e809 6f 73 74 20 7b 0a 20 20 57 68 65 72 65 50 6c 61  ost {.  WherePla
2e80a 6e 20 70 6c 61 6e 3b 20 20 20 20 2f 2a 20 54 68  n plan;    /* Th
2e80b 65 20 6c 6f 6f 6b 75 70 20 73 74 72 61 74 65 67  e lookup strateg
2e80c 79 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 43  y */.  double rC
2e80d 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 4f 76 65  ost;      /* Ove
2e80e 72 61 6c 6c 20 63 6f 73 74 20 6f 66 20 70 75 72  rall cost of pur
2e80f 73 75 69 6e 67 20 74 68 69 73 20 73 65 61 72 63  suing this searc
2e810 68 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20  h strategy */.  
2e811 64 6f 75 62 6c 65 20 6e 52 6f 77 3b 20 20 20 20  double nRow;    
2e812 20 20 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20     /* Estimated 
2e813 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74  number of output
2e814 20 72 6f 77 73 20 2a 2f 0a 20 20 42 69 74 6d 61   rows */.  Bitma
2e815 73 6b 20 75 73 65 64 3b 20 20 20 20 20 20 2f 2a  sk used;      /*
2e816 20 42 69 74 6d 61 73 6b 20 6f 66 20 63 75 72 73   Bitmask of curs
2e817 6f 72 73 20 75 73 65 64 20 62 79 20 74 68 69 73  ors used by this
2e818 20 70 6c 61 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   plan */.};../*.
2e819 2a 2a 20 42 69 74 6d 61 73 6b 73 20 66 6f 72 20  ** Bitmasks for 
2e81a 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20 74 68  the operators th
2e81b 61 74 20 69 6e 64 69 63 65 73 20 61 72 65 20 61  at indices are a
2e81c 62 6c 65 20 74 6f 20 65 78 70 6c 6f 69 74 2e 20  ble to exploit. 
2e81d 20 41 6e 0a 2a 2a 20 4f 52 2d 65 64 20 63 6f 6d   An.** OR-ed com
2e81e 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65 73  bination of thes
2e81f 65 20 76 61 6c 75 65 73 20 63 61 6e 20 62 65 20  e values can be 
2e820 75 73 65 64 20 77 68 65 6e 20 73 65 61 72 63 68  used when search
2e821 69 6e 67 20 66 6f 72 0a 2a 2a 20 74 65 72 6d 73  ing for.** terms
2e822 20 69 6e 20 74 68 65 20 77 68 65 72 65 20 63 6c   in the where cl
2e823 61 75 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ause..*/.#define
2e824 20 57 4f 5f 49 4e 20 20 20 20 20 30 78 30 30 31   WO_IN     0x001
2e825 0a 23 64 65 66 69 6e 65 20 57 4f 5f 45 51 20 20  .#define WO_EQ  
2e826 20 20 20 30 78 30 30 32 0a 23 64 65 66 69 6e 65     0x002.#define
2e827 20 57 4f 5f 4c 54 20 20 20 20 20 28 57 4f 5f 45   WO_LT     (WO_E
2e828 51 3c 3c 28 54 4b 5f 4c 54 2d 54 4b 5f 45 51 29  Q<<(TK_LT-TK_EQ)
2e829 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4c 45 20  ).#define WO_LE 
2e82a 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f      (WO_EQ<<(TK_
2e82b 4c 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69  LE-TK_EQ)).#defi
2e82c 6e 65 20 57 4f 5f 47 54 20 20 20 20 20 28 57 4f  ne WO_GT     (WO
2e82d 5f 45 51 3c 3c 28 54 4b 5f 47 54 2d 54 4b 5f 45  _EQ<<(TK_GT-TK_E
2e82e 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 47  Q)).#define WO_G
2e82f 45 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54  E     (WO_EQ<<(T
2e830 4b 5f 47 45 2d 54 4b 5f 45 51 29 29 0a 23 64 65  K_GE-TK_EQ)).#de
2e831 66 69 6e 65 20 57 4f 5f 4d 41 54 43 48 20 20 30  fine WO_MATCH  0
2e832 78 30 34 30 0a 23 64 65 66 69 6e 65 20 57 4f 5f  x040.#define WO_
2e833 49 53 4e 55 4c 4c 20 30 78 30 38 30 0a 23 64 65  ISNULL 0x080.#de
2e834 66 69 6e 65 20 57 4f 5f 4f 52 20 20 20 20 20 30  fine WO_OR     0
2e835 78 31 30 30 20 20 20 20 20 20 20 2f 2a 20 54 77  x100       /* Tw
2e836 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d 63 6f 6e  o or more OR-con
2e837 6e 65 63 74 65 64 20 74 65 72 6d 73 20 2a 2f 0a  nected terms */.
2e838 23 64 65 66 69 6e 65 20 57 4f 5f 41 4e 44 20 20  #define WO_AND  
2e839 20 20 30 78 32 30 30 20 20 20 20 20 20 20 2f 2a    0x200       /*
2e83a 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 41 4e 44   Two or more AND
2e83b 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d 73  -connected terms
2e83c 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 57 4f 5f   */..#define WO_
2e83d 41 4c 4c 20 20 20 20 30 78 66 66 66 20 20 20 20  ALL    0xfff    
2e83e 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 61 6c     /* Mask of al
2e83f 6c 20 70 6f 73 73 69 62 6c 65 20 57 4f 5f 2a 20  l possible WO_* 
2e840 76 61 6c 75 65 73 20 2a 2f 0a 23 64 65 66 69 6e  values */.#defin
2e841 65 20 57 4f 5f 53 49 4e 47 4c 45 20 30 78 30 66  e WO_SINGLE 0x0f
2e842 66 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20  f       /* Mask 
2e843 6f 66 20 61 6c 6c 20 6e 6f 6e 2d 63 6f 6d 70 6f  of all non-compo
2e844 75 6e 64 20 57 4f 5f 2a 20 76 61 6c 75 65 73 20  und WO_* values 
2e845 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 75 65 20  */../*.** Value 
2e846 66 6f 72 20 77 73 46 6c 61 67 73 20 72 65 74 75  for wsFlags retu
2e847 72 6e 65 64 20 62 79 20 62 65 73 74 49 6e 64 65  rned by bestInde
2e848 78 28 29 20 61 6e 64 20 73 74 6f 72 65 64 20 69  x() and stored i
2e849 6e 0a 2a 2a 20 57 68 65 72 65 4c 65 76 65 6c 2e  n.** WhereLevel.
2e84a 77 73 46 6c 61 67 73 2e 20 20 54 68 65 73 65 20  wsFlags.  These 
2e84b 66 6c 61 67 73 20 64 65 74 65 72 6d 69 6e 65 20  flags determine 
2e84c 77 68 69 63 68 20 73 65 61 72 63 68 0a 2a 2a 20  which search.** 
2e84d 73 74 72 61 74 65 67 69 65 73 20 61 72 65 20 61  strategies are a
2e84e 70 70 72 6f 70 72 69 61 74 65 2e 0a 2a 2a 0a 2a  ppropriate..**.*
2e84f 2a 20 54 68 65 20 6c 65 61 73 74 20 73 69 67 6e  * The least sign
2e850 69 66 69 63 61 6e 74 20 31 32 20 62 69 74 73 20  ificant 12 bits 
2e851 69 73 20 72 65 73 65 72 76 65 64 20 61 73 20 61  is reserved as a
2e852 20 6d 61 73 6b 20 66 6f 72 20 57 4f 5f 20 76 61   mask for WO_ va
2e853 6c 75 65 73 20 61 62 6f 76 65 2e 0a 2a 2a 20 54  lues above..** T
2e854 68 65 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73  he WhereLevel.ws
2e855 46 6c 61 67 73 20 66 69 65 6c 64 20 69 73 20 75  Flags field is u
2e856 73 75 61 6c 6c 79 20 73 65 74 20 74 6f 20 57 4f  sually set to WO
2e857 5f 49 4e 7c 57 4f 5f 45 51 7c 57 4f 5f 49 53 4e  _IN|WO_EQ|WO_ISN
2e858 55 4c 4c 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74  ULL..** But if t
2e859 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  he table is the 
2e85a 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61  right table of a
2e85b 20 6c 65 66 74 20 6a 6f 69 6e 2c 20 57 68 65 72   left join, Wher
2e85c 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73 0a 2a  eLevel.wsFlags.*
2e85d 2a 20 69 73 20 73 65 74 20 74 6f 20 57 4f 5f 49  * is set to WO_I
2e85e 4e 7c 57 4f 5f 45 51 2e 20 20 54 68 65 20 57 68  N|WO_EQ.  The Wh
2e85f 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c 61 67 73  ereLevel.wsFlags
2e860 20 66 69 65 6c 64 20 63 61 6e 20 74 68 65 6e 20   field can then 
2e861 62 65 20 75 73 65 64 20 61 73 0a 2a 2a 20 74 68  be used as.** th
2e862 65 20 22 6f 70 22 20 70 61 72 61 6d 65 74 65 72  e "op" parameter
2e863 20 74 6f 20 66 69 6e 64 54 65 72 6d 20 77 68 65   to findTerm whe
2e864 6e 20 77 65 20 61 72 65 20 72 65 73 6f 6c 76 69  n we are resolvi
2e865 6e 67 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73  ng equality cons
2e866 74 72 61 69 6e 74 73 2e 0a 2a 2a 20 49 53 4e 55  traints..** ISNU
2e867 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77  LL constraints w
2e868 69 6c 6c 20 74 68 65 6e 20 6e 6f 74 20 62 65 20  ill then not be 
2e869 75 73 65 64 20 6f 6e 20 74 68 65 20 72 69 67 68  used on the righ
2e86a 74 20 74 61 62 6c 65 20 6f 66 20 61 20 6c 65 66  t table of a lef
2e86b 74 0a 2a 2a 20 6a 6f 69 6e 2e 20 20 54 69 63 6b  t.** join.  Tick
2e86c 65 74 73 20 23 32 31 37 37 20 61 6e 64 20 23 32  ets #2177 and #2
2e86d 31 38 39 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  189..*/.#define 
2e86e 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 20  WHERE_ROWID_EQ  
2e86f 20 20 20 30 78 30 30 30 30 31 30 30 30 20 20 2f     0x00001000  /
2e870 2a 20 72 6f 77 69 64 3d 45 58 50 52 20 6f 72 20  * rowid=EXPR or 
2e871 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29 20 2a  rowid IN (...) *
2e872 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2e873 52 4f 57 49 44 5f 52 41 4e 47 45 20 20 30 78 30  ROWID_RANGE  0x0
2e874 30 30 30 32 30 30 30 20 20 2f 2a 20 72 6f 77 69  0002000  /* rowi
2e875 64 3c 45 58 50 52 20 61 6e 64 2f 6f 72 20 72 6f  d<EXPR and/or ro
2e876 77 69 64 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66  wid>EXPR */.#def
2e877 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  ine WHERE_COLUMN
2e878 5f 45 51 20 20 20 20 30 78 30 30 30 31 30 30 30  _EQ    0x0001000
2e879 30 20 20 2f 2a 20 78 3d 45 58 50 52 20 6f 72 20  0  /* x=EXPR or 
2e87a 78 20 49 4e 20 28 2e 2e 2e 29 20 6f 72 20 78 20  x IN (...) or x 
2e87b 49 53 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69  IS NULL */.#defi
2e87c 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  ne WHERE_COLUMN_
2e87d 52 41 4e 47 45 20 30 78 30 30 30 32 30 30 30 30  RANGE 0x00020000
2e87e 20 20 2f 2a 20 78 3c 45 58 50 52 20 61 6e 64 2f    /* x<EXPR and/
2e87f 6f 72 20 78 3e 45 58 50 52 20 2a 2f 0a 23 64 65  or x>EXPR */.#de
2e880 66 69 6e 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d  fine WHERE_COLUM
2e881 4e 5f 49 4e 20 20 20 20 30 78 30 30 30 34 30 30  N_IN    0x000400
2e882 30 30 20 20 2f 2a 20 78 20 49 4e 20 28 2e 2e 2e  00  /* x IN (...
2e883 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ) */.#define WHE
2e884 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 20 20  RE_COLUMN_NULL  
2e885 30 78 30 30 30 38 30 30 30 30 20 20 2f 2a 20 78  0x00080000  /* x
2e886 20 49 53 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66   IS NULL */.#def
2e887 69 6e 65 20 57 48 45 52 45 5f 49 4e 44 45 58 45  ine WHERE_INDEXE
2e888 44 20 20 20 20 20 20 30 78 30 30 30 66 30 30 30  D      0x000f000
2e889 30 20 20 2f 2a 20 41 6e 79 74 68 69 6e 67 20 74  0  /* Anything t
2e88a 68 61 74 20 75 73 65 73 20 61 6e 20 69 6e 64 65  hat uses an inde
2e88b 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  x */.#define WHE
2e88c 52 45 5f 49 4e 5f 41 42 4c 45 20 20 20 20 20 20  RE_IN_ABLE      
2e88d 30 78 30 30 30 66 31 30 30 30 20 20 2f 2a 20 41  0x000f1000  /* A
2e88e 62 6c 65 20 74 6f 20 73 75 70 70 6f 72 74 20 61  ble to support a
2e88f 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  n IN operator */
2e890 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 54  .#define WHERE_T
2e891 4f 50 5f 4c 49 4d 49 54 20 20 20 20 30 78 30 30  OP_LIMIT    0x00
2e892 31 30 30 30 30 30 20 20 2f 2a 20 78 3c 45 58 50  100000  /* x<EXP
2e893 52 20 6f 72 20 78 3c 3d 45 58 50 52 20 63 6f 6e  R or x<=EXPR con
2e894 73 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69  straint */.#defi
2e895 6e 65 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d  ne WHERE_BTM_LIM
2e896 49 54 20 20 20 20 30 78 30 30 32 30 30 30 30 30  IT    0x00200000
2e897 20 20 2f 2a 20 78 3e 45 58 50 52 20 6f 72 20 78    /* x>EXPR or x
2e898 3e 3d 45 58 50 52 20 63 6f 6e 73 74 72 61 69 6e  >=EXPR constrain
2e899 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  t */.#define WHE
2e89a 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 20 20 20 20  RE_IDX_ONLY     
2e89b 30 78 30 30 38 30 30 30 30 30 20 20 2f 2a 20 55  0x00800000  /* U
2e89c 73 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20 2d 20  se index only - 
2e89d 6f 6d 69 74 20 74 61 62 6c 65 20 2a 2f 0a 23 64  omit table */.#d
2e89e 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 52 44 45  efine WHERE_ORDE
2e89f 52 42 59 20 20 20 20 20 20 30 78 30 31 30 30 30  RBY      0x01000
2e8a0 30 30 30 20 20 2f 2a 20 4f 75 74 70 75 74 20 77  000  /* Output w
2e8a1 69 6c 6c 20 61 70 70 65 61 72 20 69 6e 20 63 6f  ill appear in co
2e8a2 72 72 65 63 74 20 6f 72 64 65 72 20 2a 2f 0a 23  rrect order */.#
2e8a3 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52 45 56  define WHERE_REV
2e8a4 45 52 53 45 20 20 20 20 20 20 30 78 30 32 30 30  ERSE      0x0200
2e8a5 30 30 30 30 20 20 2f 2a 20 53 63 61 6e 20 69 6e  0000  /* Scan in
2e8a6 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a   reverse order *
2e8a7 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2e8a8 55 4e 49 51 55 45 20 20 20 20 20 20 20 30 78 30  UNIQUE       0x0
2e8a9 34 30 30 30 30 30 30 20 20 2f 2a 20 53 65 6c 65  4000000  /* Sele
2e8aa 63 74 73 20 6e 6f 20 6d 6f 72 65 20 74 68 61 6e  cts no more than
2e8ab 20 6f 6e 65 20 72 6f 77 20 2a 2f 0a 23 64 65 66   one row */.#def
2e8ac 69 6e 65 20 57 48 45 52 45 5f 56 49 52 54 55 41  ine WHERE_VIRTUA
2e8ad 4c 54 41 42 4c 45 20 30 78 30 38 30 30 30 30 30  LTABLE 0x0800000
2e8ae 30 20 20 2f 2a 20 55 73 65 20 76 69 72 74 75 61  0  /* Use virtua
2e8af 6c 2d 74 61 62 6c 65 20 70 72 6f 63 65 73 73 69  l-table processi
2e8b0 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  ng */.#define WH
2e8b1 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20 20 20 20  ERE_MULTI_OR    
2e8b2 20 30 78 31 30 30 30 30 30 30 30 20 20 2f 2a 20   0x10000000  /* 
2e8b3 4f 52 20 75 73 69 6e 67 20 6d 75 6c 74 69 70 6c  OR using multipl
2e8b4 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a 0a 2f 2a  e indices */../*
2e8b5 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
2e8b6 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 20 57 68   preallocated Wh
2e8b7 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74  ereClause struct
2e8b8 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ure..*/.static v
2e8b9 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73 65 49  oid whereClauseI
2e8ba 6e 69 74 28 0a 20 20 57 68 65 72 65 43 6c 61 75  nit(.  WhereClau
2e8bb 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20  se *pWC,        
2e8bc 2f 2a 20 54 68 65 20 57 68 65 72 65 43 6c 61 75  /* The WhereClau
2e8bd 73 65 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c  se to be initial
2e8be 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73 65 20  ized */.  Parse 
2e8bf 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2e8c0 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
2e8c1 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57  g context */.  W
2e8c2 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61  hereMaskSet *pMa
2e8c3 73 6b 53 65 74 20 20 20 2f 2a 20 4d 61 70 70 69  skSet   /* Mappi
2e8c4 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 20 63 75  ng from table cu
2e8c5 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 74 6f 20  rsor numbers to 
2e8c6 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 29 7b 0a 20  bitmasks */.){. 
2e8c7 20 70 57 43 2d 3e 70 50 61 72 73 65 20 3d 20 70   pWC->pParse = p
2e8c8 50 61 72 73 65 3b 0a 20 20 70 57 43 2d 3e 70 4d  Parse;.  pWC->pM
2e8c9 61 73 6b 53 65 74 20 3d 20 70 4d 61 73 6b 53 65  askSet = pMaskSe
2e8ca 74 3b 0a 20 20 70 57 43 2d 3e 6e 54 65 72 6d 20  t;.  pWC->nTerm 
2e8cb 3d 20 30 3b 0a 20 20 70 57 43 2d 3e 6e 53 6c 6f  = 0;.  pWC->nSlo
2e8cc 74 20 3d 20 41 72 72 61 79 53 69 7a 65 28 70 57  t = ArraySize(pW
2e8cd 43 2d 3e 61 53 74 61 74 69 63 29 3b 0a 20 20 70  C->aStatic);.  p
2e8ce 57 43 2d 3e 61 20 3d 20 70 57 43 2d 3e 61 53 74  WC->a = pWC->aSt
2e8cf 61 74 69 63 3b 0a 20 20 70 57 43 2d 3e 76 6d 61  atic;.  pWC->vma
2e8d0 73 6b 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f  sk = 0;.}../* Fo
2e8d1 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 20  rward reference 
2e8d2 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
2e8d3 68 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28  hereClauseClear(
2e8d4 57 68 65 72 65 43 6c 61 75 73 65 2a 29 3b 0a 0a  WhereClause*);..
2e8d5 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65  /*.** Deallocate
2e8d6 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f   all memory asso
2e8d7 63 69 61 74 65 64 20 77 69 74 68 20 61 20 57 68  ciated with a Wh
2e8d8 65 72 65 4f 72 49 6e 66 6f 20 6f 62 6a 65 63 74  ereOrInfo object
2e8d9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2e8da 20 77 68 65 72 65 4f 72 49 6e 66 6f 44 65 6c 65   whereOrInfoDele
2e8db 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  te(sqlite3 *db, 
2e8dc 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a 70 29 7b  WhereOrInfo *p){
2e8dd 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c  .  whereClauseCl
2e8de 65 61 72 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73  ear(&p->wc);.  s
2e8df 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2e8e0 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65   p);.}../*.** De
2e8e1 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 6d 65 6d  allocate all mem
2e8e2 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  ory associated w
2e8e3 69 74 68 20 61 20 57 68 65 72 65 41 6e 64 49 6e  ith a WhereAndIn
2e8e4 66 6f 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74  fo object..*/.st
2e8e5 61 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 41  atic void whereA
2e8e6 6e 64 49 6e 66 6f 44 65 6c 65 74 65 28 73 71 6c  ndInfoDelete(sql
2e8e7 69 74 65 33 20 2a 64 62 2c 20 57 68 65 72 65 41  ite3 *db, WhereA
2e8e8 6e 64 49 6e 66 6f 20 2a 70 29 7b 0a 20 20 77 68  ndInfo *p){.  wh
2e8e9 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28 26  ereClauseClear(&
2e8ea 70 2d 3e 77 63 29 3b 0a 20 20 73 71 6c 69 74 65  p->wc);.  sqlite
2e8eb 33 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a  3DbFree(db, p);.
2e8ec 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63  }../*.** Dealloc
2e8ed 61 74 65 20 61 20 57 68 65 72 65 43 6c 61 75 73  ate a WhereClaus
2e8ee 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  e structure.  Th
2e8ef 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73 74  e WhereClause st
2e8f0 72 75 63 74 75 72 65 0a 2a 2a 20 69 74 73 65 6c  ructure.** itsel
2e8f1 66 20 69 73 20 6e 6f 74 20 66 72 65 65 64 2e 20  f is not freed. 
2e8f2 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2e8f3 20 74 68 65 20 69 6e 76 65 72 73 65 20 6f 66 20   the inverse of 
2e8f4 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28  whereClauseInit(
2e8f5 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
2e8f6 64 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c 65  d whereClauseCle
2e8f7 61 72 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a  ar(WhereClause *
2e8f8 70 57 43 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  pWC){.  int i;. 
2e8f9 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b 0a 20   WhereTerm *a;. 
2e8fa 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2e8fb 57 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a  WC->pParse->db;.
2e8fc 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65    for(i=pWC->nTe
2e8fd 72 6d 2d 31 2c 20 61 3d 70 57 43 2d 3e 61 3b 20  rm-1, a=pWC->a; 
2e8fe 69 3e 3d 30 3b 20 69 2d 2d 2c 20 61 2b 2b 29 7b  i>=0; i--, a++){
2e8ff 0a 20 20 20 20 69 66 28 20 61 2d 3e 77 74 46 6c  .    if( a->wtFl
2e900 61 67 73 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d  ags & TERM_DYNAM
2e901 49 43 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  IC ){.      sqli
2e902 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2e903 2c 20 61 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20  , a->pExpr);.   
2e904 20 7d 0a 20 20 20 20 69 66 28 20 61 2d 3e 77 74   }.    if( a->wt
2e905 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49  Flags & TERM_ORI
2e906 4e 46 4f 20 29 7b 0a 20 20 20 20 20 20 77 68 65  NFO ){.      whe
2e907 72 65 4f 72 49 6e 66 6f 44 65 6c 65 74 65 28 64  reOrInfoDelete(d
2e908 62 2c 20 61 2d 3e 75 2e 70 4f 72 49 6e 66 6f 29  b, a->u.pOrInfo)
2e909 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
2e90a 61 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  a->wtFlags & TER
2e90b 4d 5f 41 4e 44 49 4e 46 4f 20 29 7b 0a 20 20 20  M_ANDINFO ){.   
2e90c 20 20 20 77 68 65 72 65 41 6e 64 49 6e 66 6f 44     whereAndInfoD
2e90d 65 6c 65 74 65 28 64 62 2c 20 61 2d 3e 75 2e 70  elete(db, a->u.p
2e90e 41 6e 64 49 6e 66 6f 29 3b 0a 20 20 20 20 7d 0a  AndInfo);.    }.
2e90f 20 20 7d 0a 20 20 69 66 28 20 70 57 43 2d 3e 61    }.  if( pWC->a
2e910 21 3d 70 57 43 2d 3e 61 53 74 61 74 69 63 20 29  !=pWC->aStatic )
2e911 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
2e912 72 65 65 28 64 62 2c 20 70 57 43 2d 3e 61 29 3b  ree(db, pWC->a);
2e913 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  .  }.}../*.** Ad
2e914 64 20 61 20 73 69 6e 67 6c 65 20 6e 65 77 20 57  d a single new W
2e915 68 65 72 65 54 65 72 6d 20 65 6e 74 72 79 20 74  hereTerm entry t
2e916 6f 20 74 68 65 20 57 68 65 72 65 43 6c 61 75 73  o the WhereClaus
2e917 65 20 6f 62 6a 65 63 74 20 70 57 43 2e 0a 2a 2a  e object pWC..**
2e918 20 54 68 65 20 6e 65 77 20 57 68 65 72 65 54 65   The new WhereTe
2e919 72 6d 20 6f 62 6a 65 63 74 20 69 73 20 63 6f 6e  rm object is con
2e91a 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 45 78  structed from Ex
2e91b 70 72 20 70 20 61 6e 64 20 77 69 74 68 20 77 74  pr p and with wt
2e91c 46 6c 61 67 73 2e 0a 2a 2a 20 54 68 65 20 69 6e  Flags..** The in
2e91d 64 65 78 20 69 6e 20 70 57 43 2d 3e 61 5b 5d 20  dex in pWC->a[] 
2e91e 6f 66 20 74 68 65 20 6e 65 77 20 57 68 65 72 65  of the new Where
2e91f 54 65 72 6d 20 69 73 20 72 65 74 75 72 6e 65 64  Term is returned
2e920 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20   on success..** 
2e921 30 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  0 is returned if
2e922 20 74 68 65 20 6e 65 77 20 57 68 65 72 65 54 65   the new WhereTe
2e923 72 6d 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  rm could not be 
2e924 61 64 64 65 64 20 64 75 65 20 74 6f 20 61 20 6d  added due to a m
2e925 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
2e926 69 6f 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20  ion error.  The 
2e927 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
2e928 6e 20 66 61 69 6c 75 72 65 20 77 69 6c 6c 20 62  n failure will b
2e929 65 20 72 65 63 6f 72 64 65 64 20 69 6e 0a 2a 2a  e recorded in.**
2e92a 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   the db->mallocF
2e92b 61 69 6c 65 64 20 66 6c 61 67 20 73 6f 20 74 68  ailed flag so th
2e92c 61 74 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20  at higher-level 
2e92d 66 75 6e 63 74 69 6f 6e 73 20 63 61 6e 20 64 65  functions can de
2e92e 74 65 63 74 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54  tect it..**.** T
2e92f 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
2e930 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 69   increase the si
2e931 7a 65 20 6f 66 20 74 68 65 20 70 57 43 2d 3e 61  ze of the pWC->a
2e932 5b 5d 20 61 72 72 61 79 20 61 73 20 6e 65 63 65  [] array as nece
2e933 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssary..**.** If 
2e934 74 68 65 20 77 74 46 6c 61 67 73 20 61 72 67 75  the wtFlags argu
2e935 6d 65 6e 74 20 69 6e 63 6c 75 64 65 73 20 54 45  ment includes TE
2e936 52 4d 5f 44 59 4e 41 4d 49 43 2c 20 74 68 65 6e  RM_DYNAMIC, then
2e937 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 0a   responsibility.
2e938 2a 2a 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74  ** for freeing t
2e939 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 70 20  he expression p 
2e93a 69 73 20 61 73 73 75 6d 65 64 20 62 79 20 74 68  is assumed by th
2e93b 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62  e WhereClause ob
2e93c 6a 65 63 74 20 70 57 43 2e 0a 2a 2a 20 54 68 69  ject pWC..** Thi
2e93d 73 20 69 73 20 74 72 75 65 20 65 76 65 6e 20 69  s is true even i
2e93e 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  f this routine f
2e93f 61 69 6c 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65  ails to allocate
2e940 20 61 20 6e 65 77 20 57 68 65 72 65 54 65 72 6d   a new WhereTerm
2e941 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a  ..**.** WARNING:
2e942 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d    This routine m
2e943 69 67 68 74 20 72 65 61 6c 6c 6f 63 61 74 65 20  ight reallocate 
2e944 74 68 65 20 73 70 61 63 65 20 75 73 65 64 20 74  the space used t
2e945 6f 20 73 74 6f 72 65 0a 2a 2a 20 57 68 65 72 65  o store.** Where
2e946 54 65 72 6d 73 2e 20 20 41 6c 6c 20 70 6f 69 6e  Terms.  All poin
2e947 74 65 72 73 20 74 6f 20 57 68 65 72 65 54 65 72  ters to WhereTer
2e948 6d 73 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 76  ms should be inv
2e949 61 6c 69 64 61 74 65 64 20 61 66 74 65 72 0a 2a  alidated after.*
2e94a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  * calling this r
2e94b 6f 75 74 69 6e 65 2e 20 20 53 75 63 68 20 70 6f  outine.  Such po
2e94c 69 6e 74 65 72 73 20 6d 61 79 20 62 65 20 72 65  inters may be re
2e94d 69 6e 69 74 69 61 6c 69 7a 65 64 20 62 79 20 72  initialized by r
2e94e 65 66 65 72 65 6e 63 69 6e 67 0a 2a 2a 20 74 68  eferencing.** th
2e94f 65 20 70 57 43 2d 3e 61 5b 5d 20 61 72 72 61 79  e pWC->a[] array
2e950 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2e951 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
2e952 74 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70  t(WhereClause *p
2e953 57 43 2c 20 45 78 70 72 20 2a 70 2c 20 75 38 20  WC, Expr *p, u8 
2e954 77 74 46 6c 61 67 73 29 7b 0a 20 20 57 68 65 72  wtFlags){.  Wher
2e955 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20  eTerm *pTerm;.  
2e956 69 6e 74 20 69 64 78 3b 0a 20 20 69 66 28 20 70  int idx;.  if( p
2e957 57 43 2d 3e 6e 54 65 72 6d 3e 3d 70 57 43 2d 3e  WC->nTerm>=pWC->
2e958 6e 53 6c 6f 74 20 29 7b 0a 20 20 20 20 57 68 65  nSlot ){.    Whe
2e959 72 65 54 65 72 6d 20 2a 70 4f 6c 64 20 3d 20 70  reTerm *pOld = p
2e95a 57 43 2d 3e 61 3b 0a 20 20 20 20 73 71 6c 69 74  WC->a;.    sqlit
2e95b 65 33 20 2a 64 62 20 3d 20 70 57 43 2d 3e 70 50  e3 *db = pWC->pP
2e95c 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 57  arse->db;.    pW
2e95d 43 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 44 62  C->a = sqlite3Db
2e95e 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69  MallocRaw(db, si
2e95f 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a  zeof(pWC->a[0])*
2e960 70 57 43 2d 3e 6e 53 6c 6f 74 2a 32 20 29 3b 0a  pWC->nSlot*2 );.
2e961 20 20 20 20 69 66 28 20 70 57 43 2d 3e 61 3d 3d      if( pWC->a==
2e962 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 77  0 ){.      if( w
2e963 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 44 59  tFlags & TERM_DY
2e964 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20 20 20  NAMIC ){.       
2e965 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2e966 74 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20  te(db, p);.     
2e967 20 7d 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 20   }.      pWC->a 
2e968 3d 20 70 4f 6c 64 3b 0a 20 20 20 20 20 20 72 65  = pOld;.      re
2e969 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
2e96a 20 20 6d 65 6d 63 70 79 28 70 57 43 2d 3e 61 2c    memcpy(pWC->a,
2e96b 20 70 4f 6c 64 2c 20 73 69 7a 65 6f 66 28 70 57   pOld, sizeof(pW
2e96c 43 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e 54  C->a[0])*pWC->nT
2e96d 65 72 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 4f  erm);.    if( pO
2e96e 6c 64 21 3d 70 57 43 2d 3e 61 53 74 61 74 69 63  ld!=pWC->aStatic
2e96f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2e970 33 44 62 46 72 65 65 28 64 62 2c 20 70 4f 6c 64  3DbFree(db, pOld
2e971 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 57 43  );.    }.    pWC
2e972 2d 3e 6e 53 6c 6f 74 20 3d 20 73 71 6c 69 74 65  ->nSlot = sqlite
2e973 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28 64 62  3DbMallocSize(db
2e974 2c 20 70 57 43 2d 3e 61 29 2f 73 69 7a 65 6f 66  , pWC->a)/sizeof
2e975 28 70 57 43 2d 3e 61 5b 30 5d 29 3b 0a 20 20 7d  (pWC->a[0]);.  }
2e976 0a 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d  .  pTerm = &pWC-
2e977 3e 61 5b 69 64 78 20 3d 20 70 57 43 2d 3e 6e 54  >a[idx = pWC->nT
2e978 65 72 6d 2b 2b 5d 3b 0a 20 20 70 54 65 72 6d 2d  erm++];.  pTerm-
2e979 3e 70 45 78 70 72 20 3d 20 70 3b 0a 20 20 70 54  >pExpr = p;.  pT
2e97a 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 3d 20 77  erm->wtFlags = w
2e97b 74 46 6c 61 67 73 3b 0a 20 20 70 54 65 72 6d 2d  tFlags;.  pTerm-
2e97c 3e 70 57 43 20 3d 20 70 57 43 3b 0a 20 20 70 54  >pWC = pWC;.  pT
2e97d 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d  erm->iParent = -
2e97e 31 3b 0a 20 20 72 65 74 75 72 6e 20 69 64 78 3b  1;.  return idx;
2e97f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
2e980 6f 75 74 69 6e 65 20 69 64 65 6e 74 69 66 69 65  outine identifie
2e981 73 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  s subexpressions
2e982 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
2e983 61 75 73 65 20 77 68 65 72 65 0a 2a 2a 20 65 61  ause where.** ea
2e984 63 68 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  ch subexpression
2e985 20 69 73 20 73 65 70 61 72 61 74 65 64 20 62 79   is separated by
2e986 20 74 68 65 20 41 4e 44 20 6f 70 65 72 61 74 6f   the AND operato
2e987 72 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a  r or some other.
2e988 2a 2a 20 6f 70 65 72 61 74 6f 72 20 73 70 65 63  ** operator spec
2e989 69 66 69 65 64 20 69 6e 20 74 68 65 20 6f 70 20  ified in the op 
2e98a 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 20  parameter.  The 
2e98b 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75  WhereClause stru
2e98c 63 74 75 72 65 0a 2a 2a 20 69 73 20 66 69 6c 6c  cture.** is fill
2e98d 65 64 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73  ed with pointers
2e98e 20 74 6f 20 73 75 62 65 78 70 72 65 73 73 69 6f   to subexpressio
2e98f 6e 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  ns.  For example
2e990 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 57 48 45 52 45  :.**.**    WHERE
2e991 20 20 61 3d 3d 27 68 65 6c 6c 6f 27 20 41 4e 44    a=='hello' AND
2e992 20 63 6f 61 6c 65 73 63 65 28 62 2c 31 31 29 3c   coalesce(b,11)<
2e993 31 30 20 41 4e 44 20 28 63 2b 31 32 21 3d 64 20  10 AND (c+12!=d 
2e994 4f 52 20 63 3d 3d 32 32 29 0a 2a 2a 20 20 20 20  OR c==22).**    
2e995 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f         \________
2e996 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f  /     \_________
2e997 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f  ______/     \___
2e998 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a  _____________/.*
2e999 2a 20 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f  *            slo
2e99a 74 5b 30 5d 20 20 20 20 20 20 20 20 20 20 20 20  t[0]            
2e99b 73 6c 6f 74 5b 31 5d 20 20 20 20 20 20 20 20 20  slot[1]         
2e99c 20 20 20 20 20 20 73 6c 6f 74 5b 32 5d 0a 2a 2a        slot[2].**
2e99d 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c  .** The original
2e99e 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e   WHERE clause in
2e99f 20 70 45 78 70 72 20 69 73 20 75 6e 61 6c 74 65   pExpr is unalte
2e9a0 72 65 64 2e 20 20 41 6c 6c 20 74 68 69 73 20 72  red.  All this r
2e9a1 6f 75 74 69 6e 65 0a 2a 2a 20 64 6f 65 73 20 69  outine.** does i
2e9a2 73 20 6d 61 6b 65 20 73 6c 6f 74 5b 5d 20 65 6e  s make slot[] en
2e9a3 74 72 69 65 73 20 70 6f 69 6e 74 20 74 6f 20 73  tries point to s
2e9a4 75 62 73 74 72 75 63 74 75 72 65 20 77 69 74 68  ubstructure with
2e9a5 69 6e 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20  in pExpr..**.** 
2e9a6 49 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  In the previous 
2e9a7 73 65 6e 74 65 6e 63 65 20 61 6e 64 20 69 6e 20  sentence and in 
2e9a8 74 68 65 20 64 69 61 67 72 61 6d 2c 20 22 73 6c  the diagram, "sl
2e9a9 6f 74 5b 5d 22 20 72 65 66 65 72 73 20 74 6f 0a  ot[]" refers to.
2e9aa 2a 2a 20 74 68 65 20 57 68 65 72 65 43 6c 61 75  ** the WhereClau
2e9ab 73 65 2e 61 5b 5d 20 61 72 72 61 79 2e 20 20 54  se.a[] array.  T
2e9ac 68 65 20 73 6c 6f 74 5b 5d 20 61 72 72 61 79 20  he slot[] array 
2e9ad 67 72 6f 77 73 20 61 73 20 6e 65 65 64 65 64 20  grows as needed 
2e9ae 74 6f 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 6c  to contain.** al
2e9af 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57  l terms of the W
2e9b0 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a  HERE clause..*/.
2e9b1 73 74 61 74 69 63 20 76 6f 69 64 20 77 68 65 72  static void wher
2e9b2 65 53 70 6c 69 74 28 57 68 65 72 65 43 6c 61 75  eSplit(WhereClau
2e9b3 73 65 20 2a 70 57 43 2c 20 45 78 70 72 20 2a 70  se *pWC, Expr *p
2e9b4 45 78 70 72 2c 20 69 6e 74 20 6f 70 29 7b 0a 20  Expr, int op){. 
2e9b5 20 70 57 43 2d 3e 6f 70 20 3d 20 28 75 38 29 6f   pWC->op = (u8)o
2e9b6 70 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d  p;.  if( pExpr==
2e9b7 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
2e9b8 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 6f 70 20  ( pExpr->op!=op 
2e9b9 29 7b 0a 20 20 20 20 77 68 65 72 65 43 6c 61 75  ){.    whereClau
2e9ba 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 45  seInsert(pWC, pE
2e9bb 78 70 72 2c 20 30 29 3b 0a 20 20 7d 65 6c 73 65  xpr, 0);.  }else
2e9bc 7b 0a 20 20 20 20 77 68 65 72 65 53 70 6c 69 74  {.    whereSplit
2e9bd 28 70 57 43 2c 20 70 45 78 70 72 2d 3e 70 4c 65  (pWC, pExpr->pLe
2e9be 66 74 2c 20 6f 70 29 3b 0a 20 20 20 20 77 68 65  ft, op);.    whe
2e9bf 72 65 53 70 6c 69 74 28 70 57 43 2c 20 70 45 78  reSplit(pWC, pEx
2e9c0 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 29 3b  pr->pRight, op);
2e9c1 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  .  }.}../*.** In
2e9c2 69 74 69 61 6c 69 7a 65 20 61 6e 20 65 78 70 72  itialize an expr
2e9c3 65 73 73 69 6f 6e 20 6d 61 73 6b 20 73 65 74 20  ession mask set 
2e9c4 28 61 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20  (a WhereMaskSet 
2e9c5 6f 62 6a 65 63 74 29 0a 2a 2f 0a 23 64 65 66 69  object).*/.#defi
2e9c6 6e 65 20 69 6e 69 74 4d 61 73 6b 53 65 74 28 50  ne initMaskSet(P
2e9c7 29 20 20 6d 65 6d 73 65 74 28 50 2c 20 30 2c 20  )  memset(P, 0, 
2e9c8 73 69 7a 65 6f 66 28 2a 50 29 29 0a 0a 2f 2a 0a  sizeof(*P))../*.
2e9c9 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62 69  ** Return the bi
2e9ca 74 6d 61 73 6b 20 66 6f 72 20 74 68 65 20 67 69  tmask for the gi
2e9cb 76 65 6e 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ven cursor numbe
2e9cc 72 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 0a  r.  Return 0 if.
2e9cd 2a 2a 20 69 43 75 72 73 6f 72 20 69 73 20 6e 6f  ** iCursor is no
2e9ce 74 20 69 6e 20 74 68 65 20 73 65 74 2e 0a 2a 2f  t in the set..*/
2e9cf 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20  .static Bitmask 
2e9d0 67 65 74 4d 61 73 6b 28 57 68 65 72 65 4d 61 73  getMask(WhereMas
2e9d1 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20  kSet *pMaskSet, 
2e9d2 69 6e 74 20 69 43 75 72 73 6f 72 29 7b 0a 20 20  int iCursor){.  
2e9d3 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28  int i;.  assert(
2e9d4 20 70 4d 61 73 6b 53 65 74 2d 3e 6e 3c 3d 73 69   pMaskSet->n<=si
2e9d5 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38 20  zeof(Bitmask)*8 
2e9d6 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
2e9d7 70 4d 61 73 6b 53 65 74 2d 3e 6e 3b 20 69 2b 2b  pMaskSet->n; i++
2e9d8 29 7b 0a 20 20 20 20 69 66 28 20 70 4d 61 73 6b  ){.    if( pMask
2e9d9 53 65 74 2d 3e 69 78 5b 69 5d 3d 3d 69 43 75 72  Set->ix[i]==iCur
2e9da 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 72 65 74  sor ){.      ret
2e9db 75 72 6e 20 28 28 42 69 74 6d 61 73 6b 29 31 29  urn ((Bitmask)1)
2e9dc 3c 3c 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  <<i;.    }.  }. 
2e9dd 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
2e9de 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
2e9df 20 6d 61 73 6b 20 66 6f 72 20 63 75 72 73 6f 72   mask for cursor
2e9e0 20 69 43 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20   iCursor..**.** 
2e9e1 54 68 65 72 65 20 69 73 20 6f 6e 65 20 63 75 72  There is one cur
2e9e2 73 6f 72 20 70 65 72 20 74 61 62 6c 65 20 69 6e  sor per table in
2e9e3 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2e9e4 2e 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  .  The number of
2e9e5 0a 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68  .** tables in th
2e9e6 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73  e FROM clause is
2e9e7 20 6c 69 6d 69 74 65 64 20 62 79 20 61 20 74 65   limited by a te
2e9e8 73 74 20 65 61 72 6c 79 20 69 6e 20 74 68 65 0a  st early in the.
2e9e9 2a 2a 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  ** sqlite3WhereB
2e9ea 65 67 69 6e 28 29 20 72 6f 75 74 69 6e 65 2e 20  egin() routine. 
2e9eb 20 53 6f 20 77 65 20 6b 6e 6f 77 20 74 68 61 74   So we know that
2e9ec 20 74 68 65 20 70 4d 61 73 6b 53 65 74 2d 3e 69   the pMaskSet->i
2e9ed 78 5b 5d 0a 2a 2a 20 61 72 72 61 79 20 77 69 6c  x[].** array wil
2e9ee 6c 20 6e 65 76 65 72 20 6f 76 65 72 66 6c 6f 77  l never overflow
2e9ef 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2e9f0 20 63 72 65 61 74 65 4d 61 73 6b 28 57 68 65 72   createMask(Wher
2e9f1 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  eMaskSet *pMaskS
2e9f2 65 74 2c 20 69 6e 74 20 69 43 75 72 73 6f 72 29  et, int iCursor)
2e9f3 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d 61 73  {.  assert( pMas
2e9f4 6b 53 65 74 2d 3e 6e 20 3c 20 41 72 72 61 79 53  kSet->n < ArrayS
2e9f5 69 7a 65 28 70 4d 61 73 6b 53 65 74 2d 3e 69 78  ize(pMaskSet->ix
2e9f6 29 20 29 3b 0a 20 20 70 4d 61 73 6b 53 65 74 2d  ) );.  pMaskSet-
2e9f7 3e 69 78 5b 70 4d 61 73 6b 53 65 74 2d 3e 6e 2b  >ix[pMaskSet->n+
2e9f8 2b 5d 20 3d 20 69 43 75 72 73 6f 72 3b 0a 7d 0a  +] = iCursor;.}.
2e9f9 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2e9fa 69 6e 65 20 77 61 6c 6b 73 20 28 72 65 63 75 72  ine walks (recur
2e9fb 73 69 76 65 6c 79 29 20 61 6e 20 65 78 70 72 65  sively) an expre
2e9fc 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64 20 67  ssion tree and g
2e9fd 65 6e 65 72 61 74 65 73 0a 2a 2a 20 61 20 62 69  enerates.** a bi
2e9fe 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67  tmask indicating
2e9ff 20 77 68 69 63 68 20 74 61 62 6c 65 73 20 61 72   which tables ar
2ea00 65 20 75 73 65 64 20 69 6e 20 74 68 61 74 20 65  e used in that e
2ea01 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65  xpression.** tre
2ea02 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65  e..**.** In orde
2ea03 72 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74 69  r for this routi
2ea04 6e 65 20 74 6f 20 77 6f 72 6b 2c 20 74 68 65 20  ne to work, the 
2ea05 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
2ea06 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a 20 70 72   must have.** pr
2ea07 65 76 69 6f 75 73 6c 79 20 69 6e 76 6f 6b 65 64  eviously invoked
2ea08 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
2ea09 78 70 72 4e 61 6d 65 73 28 29 20 6f 6e 20 74 68  xprNames() on th
2ea0a 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 53  e expression.  S
2ea0b 65 65 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72  ee.** the header
2ea0c 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68 61 74   comment on that
2ea0d 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 64 64   routine for add
2ea0e 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
2ea0f 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ion..** The sqli
2ea10 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
2ea11 6d 65 73 28 29 20 72 6f 75 74 69 6e 65 73 20 6c  mes() routines l
2ea12 6f 6f 6b 73 20 66 6f 72 20 63 6f 6c 75 6d 6e 20  ooks for column 
2ea13 6e 61 6d 65 73 20 61 6e 64 0a 2a 2a 20 73 65 74  names and.** set
2ea14 73 20 74 68 65 69 72 20 6f 70 63 6f 64 65 73 20  s their opcodes 
2ea15 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61 6e 64  to TK_COLUMN and
2ea16 20 74 68 65 69 72 20 45 78 70 72 2e 69 54 61 62   their Expr.iTab
2ea17 6c 65 20 66 69 65 6c 64 73 20 74 6f 0a 2a 2a 20  le fields to.** 
2ea18 74 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  the VDBE cursor 
2ea19 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 74 61  number of the ta
2ea1a 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ble.  This routi
2ea1b 6e 65 20 6a 75 73 74 20 68 61 73 20 74 6f 0a 2a  ne just has to.*
2ea1c 2a 20 74 72 61 6e 73 6c 61 74 65 20 74 68 65 20  * translate the 
2ea1d 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69  cursor numbers i
2ea1e 6e 74 6f 20 62 69 74 6d 61 73 6b 20 76 61 6c 75  nto bitmask valu
2ea1f 65 73 20 61 6e 64 20 4f 52 20 61 6c 6c 0a 2a 2a  es and OR all.**
2ea20 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20 74 6f   the bitmasks to
2ea21 67 65 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  gether..*/.stati
2ea22 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 4c 69  c Bitmask exprLi
2ea23 73 74 54 61 62 6c 65 55 73 61 67 65 28 57 68 65  stTableUsage(Whe
2ea24 72 65 4d 61 73 6b 53 65 74 2a 2c 20 45 78 70 72  reMaskSet*, Expr
2ea25 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69 63 20 42  List*);.static B
2ea26 69 74 6d 61 73 6b 20 65 78 70 72 53 65 6c 65 63  itmask exprSelec
2ea27 74 54 61 62 6c 65 55 73 61 67 65 28 57 68 65 72  tTableUsage(Wher
2ea28 65 4d 61 73 6b 53 65 74 2a 2c 20 53 65 6c 65 63  eMaskSet*, Selec
2ea29 74 2a 29 3b 0a 73 74 61 74 69 63 20 42 69 74 6d  t*);.static Bitm
2ea2a 61 73 6b 20 65 78 70 72 54 61 62 6c 65 55 73 61  ask exprTableUsa
2ea2b 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20  ge(WhereMaskSet 
2ea2c 2a 70 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 20  *pMaskSet, Expr 
2ea2d 2a 70 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d  *p){.  Bitmask m
2ea2e 61 73 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  ask = 0;.  if( p
2ea2f 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
2ea30 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f    if( p->op==TK_
2ea31 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6d 61  COLUMN ){.    ma
2ea32 73 6b 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61  sk = getMask(pMa
2ea33 73 6b 53 65 74 2c 20 70 2d 3e 69 54 61 62 6c 65  skSet, p->iTable
2ea34 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 61  );.    return ma
2ea35 73 6b 3b 0a 20 20 7d 0a 20 20 6d 61 73 6b 20 3d  sk;.  }.  mask =
2ea36 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
2ea37 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70 52 69  pMaskSet, p->pRi
2ea38 67 68 74 29 3b 0a 20 20 6d 61 73 6b 20 7c 3d 20  ght);.  mask |= 
2ea39 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
2ea3a 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70 4c 65 66  MaskSet, p->pLef
2ea3b 74 29 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61  t);.  if( ExprHa
2ea3c 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  sProperty(p, EP_
2ea3d 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
2ea3e 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 53 65    mask |= exprSe
2ea3f 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28 70  lectTableUsage(p
2ea40 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70 53  MaskSet, p->x.pS
2ea41 65 6c 65 63 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  elect);.  }else{
2ea42 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
2ea43 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
2ea44 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 78 2e 70  pMaskSet, p->x.p
2ea45 4c 69 73 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74  List);.  }.  ret
2ea46 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74  urn mask;.}.stat
2ea47 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 4c  ic Bitmask exprL
2ea48 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 57 68  istTableUsage(Wh
2ea49 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2ea4a 6b 53 65 74 2c 20 45 78 70 72 4c 69 73 74 20 2a  kSet, ExprList *
2ea4b 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b  pList){.  int i;
2ea4c 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20  .  Bitmask mask 
2ea4d 3d 20 30 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  = 0;.  if( pList
2ea4e 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
2ea4f 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
2ea50 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 61 73   i++){.      mas
2ea51 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73  k |= exprTableUs
2ea52 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c  age(pMaskSet, pL
2ea53 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
2ea54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2ea55 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61  turn mask;.}.sta
2ea56 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72  tic Bitmask expr
2ea57 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65  SelectTableUsage
2ea58 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70  (WhereMaskSet *p
2ea59 4d 61 73 6b 53 65 74 2c 20 53 65 6c 65 63 74 20  MaskSet, Select 
2ea5a 2a 70 53 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20  *pS){.  Bitmask 
2ea5b 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 77 68 69 6c  mask = 0;.  whil
2ea5c 65 28 20 70 53 20 29 7b 0a 20 20 20 20 6d 61 73  e( pS ){.    mas
2ea5d 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62  k |= exprListTab
2ea5e 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2ea5f 2c 20 70 53 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  , pS->pEList);. 
2ea60 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c     mask |= exprL
2ea61 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d  istTableUsage(pM
2ea62 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 47 72 6f  askSet, pS->pGro
2ea63 75 70 42 79 29 3b 0a 20 20 20 20 6d 61 73 6b 20  upBy);.    mask 
2ea64 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65  |= exprListTable
2ea65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2ea66 70 53 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20  pS->pOrderBy);. 
2ea67 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54     mask |= exprT
2ea68 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
2ea69 65 74 2c 20 70 53 2d 3e 70 57 68 65 72 65 29 3b  et, pS->pWhere);
2ea6a 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  .    mask |= exp
2ea6b 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  rTableUsage(pMas
2ea6c 6b 53 65 74 2c 20 70 53 2d 3e 70 48 61 76 69 6e  kSet, pS->pHavin
2ea6d 67 29 3b 0a 20 20 20 20 70 53 20 3d 20 70 53 2d  g);.    pS = pS-
2ea6e 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72  >pPrior;.  }.  r
2ea6f 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 2f  eturn mask;.}../
2ea70 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
2ea71 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 6f 70   if the given op
2ea72 65 72 61 74 6f 72 20 69 73 20 6f 6e 65 20 6f 66  erator is one of
2ea73 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20 74   the operators t
2ea74 68 61 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65  hat is.** allowe
2ea75 64 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 61 62  d for an indexab
2ea76 6c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  le WHERE clause 
2ea77 74 65 72 6d 2e 20 20 54 68 65 20 61 6c 6c 6f 77  term.  The allow
2ea78 65 64 20 6f 70 65 72 61 74 6f 72 73 20 61 72 65  ed operators are
2ea79 0a 2a 2a 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3e  .** "=", "<", ">
2ea7a 22 2c 20 22 3c 3d 22 2c 20 22 3e 3d 22 2c 20 61  ", "<=", ">=", a
2ea7b 6e 64 20 22 49 4e 22 2e 0a 2a 2f 0a 73 74 61 74  nd "IN"..*/.stat
2ea7c 69 63 20 69 6e 74 20 61 6c 6c 6f 77 65 64 4f 70  ic int allowedOp
2ea7d 28 69 6e 74 20 6f 70 29 7b 0a 20 20 61 73 73 65  (int op){.  asse
2ea7e 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20  rt( TK_GT>TK_EQ 
2ea7f 26 26 20 54 4b 5f 47 54 3c 54 4b 5f 47 45 20 29  && TK_GT<TK_GE )
2ea80 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c  ;.  assert( TK_L
2ea81 54 3e 54 4b 5f 45 51 20 26 26 20 54 4b 5f 4c 54  T>TK_EQ && TK_LT
2ea82 3c 54 4b 5f 47 45 20 29 3b 0a 20 20 61 73 73 65  <TK_GE );.  asse
2ea83 72 74 28 20 54 4b 5f 4c 45 3e 54 4b 5f 45 51 20  rt( TK_LE>TK_EQ 
2ea84 26 26 20 54 4b 5f 4c 45 3c 54 4b 5f 47 45 20 29  && TK_LE<TK_GE )
2ea85 3b 0a 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47  ;.  assert( TK_G
2ea86 45 3d 3d 54 4b 5f 45 51 2b 34 20 29 3b 0a 20 20  E==TK_EQ+4 );.  
2ea87 72 65 74 75 72 6e 20 6f 70 3d 3d 54 4b 5f 49 4e  return op==TK_IN
2ea88 20 7c 7c 20 28 6f 70 3e 3d 54 4b 5f 45 51 20 26   || (op>=TK_EQ &
2ea89 26 20 6f 70 3c 3d 54 4b 5f 47 45 29 20 7c 7c 20  & op<=TK_GE) || 
2ea8a 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 3b 0a 7d  op==TK_ISNULL;.}
2ea8b 0a 0a 2f 2a 0a 2a 2a 20 53 77 61 70 20 74 77 6f  ../*.** Swap two
2ea8c 20 6f 62 6a 65 63 74 73 20 6f 66 20 74 79 70 65   objects of type
2ea8d 20 54 59 50 45 2e 0a 2a 2f 0a 23 64 65 66 69 6e   TYPE..*/.#defin
2ea8e 65 20 53 57 41 50 28 54 59 50 45 2c 41 2c 42 29  e SWAP(TYPE,A,B)
2ea8f 20 7b 54 59 50 45 20 74 3d 41 3b 20 41 3d 42 3b   {TYPE t=A; A=B;
2ea90 20 42 3d 74 3b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   B=t;}../*.** Co
2ea91 6d 6d 75 74 65 20 61 20 63 6f 6d 70 61 72 69 73  mmute a comparis
2ea92 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 20 20 45 78  on operator.  Ex
2ea93 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74 68 65  pressions of the
2ea94 20 66 6f 72 6d 20 22 58 20 6f 70 20 59 22 0a 2a   form "X op Y".*
2ea95 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20  * are converted 
2ea96 69 6e 74 6f 20 22 59 20 6f 70 20 58 22 2e 0a 2a  into "Y op X"..*
2ea97 2a 0a 2a 2a 20 49 66 20 61 20 63 6f 6c 6c 61 74  *.** If a collat
2ea98 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20  ion sequence is 
2ea99 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2ea9a 65 69 74 68 65 72 20 74 68 65 20 6c 65 66 74 20  either the left 
2ea9b 6f 72 20 72 69 67 68 74 0a 2a 2a 20 73 69 64 65  or right.** side
2ea9c 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73   of the comparis
2ea9d 6f 6e 2c 20 69 74 20 72 65 6d 61 69 6e 73 20 61  on, it remains a
2ea9e 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2ea9f 68 65 20 73 61 6d 65 20 73 69 64 65 20 61 66 74  he same side aft
2eaa0 65 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 75 74  er.** the commut
2eaa1 61 74 69 6f 6e 2e 20 53 6f 20 22 59 20 63 6f 6c  ation. So "Y col
2eaa2 6c 61 74 65 20 4e 4f 43 41 53 45 20 6f 70 20 58  late NOCASE op X
2eaa3 22 20 62 65 63 6f 6d 65 73 20 0a 2a 2a 20 22 58  " becomes .** "X
2eaa4 20 63 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45 20   collate NOCASE 
2eaa5 6f 70 20 59 22 2e 20 54 68 69 73 20 69 73 20 62  op Y". This is b
2eaa6 65 63 61 75 73 65 20 61 6e 79 20 63 6f 6c 6c 61  ecause any colla
2eaa7 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 6e  tion sequence on
2eaa8 0a 2a 2a 20 74 68 65 20 6c 65 66 74 20 68 61 6e  .** the left han
2eaa9 64 20 73 69 64 65 20 6f 66 20 61 20 63 6f 6d 70  d side of a comp
2eaaa 61 72 69 73 6f 6e 20 6f 76 65 72 72 69 64 65 73  arison overrides
2eaab 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   any collation s
2eaac 65 71 75 65 6e 63 65 20 0a 2a 2a 20 61 74 74 61  equence .** atta
2eaad 63 68 65 64 20 74 6f 20 74 68 65 20 72 69 67 68  ched to the righ
2eaae 74 2e 20 46 6f 72 20 74 68 65 20 73 61 6d 65 20  t. For the same 
2eaaf 72 65 61 73 6f 6e 20 74 68 65 20 45 50 5f 45 78  reason the EP_Ex
2eab0 70 43 6f 6c 6c 61 74 65 20 66 6c 61 67 0a 2a 2a  pCollate flag.**
2eab1 20 69 73 20 6e 6f 74 20 63 6f 6d 6d 75 74 65 64   is not commuted
2eab2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2eab3 20 65 78 70 72 43 6f 6d 6d 75 74 65 28 50 61 72   exprCommute(Par
2eab4 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
2eab5 20 2a 70 45 78 70 72 29 7b 0a 20 20 75 31 36 20   *pExpr){.  u16 
2eab6 65 78 70 52 69 67 68 74 20 3d 20 28 70 45 78 70  expRight = (pExp
2eab7 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 73  r->pRight->flags
2eab8 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
2eab9 29 3b 0a 20 20 75 31 36 20 65 78 70 4c 65 66 74  );.  u16 expLeft
2eaba 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65 66 74   = (pExpr->pLeft
2eabb 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70  ->flags & EP_Exp
2eabc 43 6f 6c 6c 61 74 65 29 3b 0a 20 20 61 73 73 65  Collate);.  asse
2eabd 72 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28 70 45  rt( allowedOp(pE
2eabe 78 70 72 2d 3e 6f 70 29 20 26 26 20 70 45 78 70  xpr->op) && pExp
2eabf 72 2d 3e 6f 70 21 3d 54 4b 5f 49 4e 20 29 3b 0a  r->op!=TK_IN );.
2eac0 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d    pExpr->pRight-
2eac1 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33  >pColl = sqlite3
2eac2 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
2eac3 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  se, pExpr->pRigh
2eac4 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65  t);.  pExpr->pLe
2eac5 66 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  ft->pColl = sqli
2eac6 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
2eac7 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
2eac8 65 66 74 29 3b 0a 20 20 53 57 41 50 28 43 6f 6c  eft);.  SWAP(Col
2eac9 6c 53 65 71 2a 2c 70 45 78 70 72 2d 3e 70 52 69  lSeq*,pExpr->pRi
2eaca 67 68 74 2d 3e 70 43 6f 6c 6c 2c 70 45 78 70 72  ght->pColl,pExpr
2eacb 2d 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 29 3b  ->pLeft->pColl);
2eacc 0a 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  .  pExpr->pRight
2eacd 2d 3e 66 6c 61 67 73 20 3d 20 28 70 45 78 70 72  ->flags = (pExpr
2eace 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20  ->pRight->flags 
2eacf 26 20 7e 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65  & ~EP_ExpCollate
2ead0 29 20 7c 20 65 78 70 4c 65 66 74 3b 0a 20 20 70  ) | expLeft;.  p
2ead1 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61  Expr->pLeft->fla
2ead2 67 73 20 3d 20 28 70 45 78 70 72 2d 3e 70 4c 65  gs = (pExpr->pLe
2ead3 66 74 2d 3e 66 6c 61 67 73 20 26 20 7e 45 50 5f  ft->flags & ~EP_
2ead4 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c 20 65 78  ExpCollate) | ex
2ead5 70 52 69 67 68 74 3b 0a 20 20 53 57 41 50 28 45  pRight;.  SWAP(E
2ead6 78 70 72 2a 2c 70 45 78 70 72 2d 3e 70 52 69 67  xpr*,pExpr->pRig
2ead7 68 74 2c 70 45 78 70 72 2d 3e 70 4c 65 66 74 29  ht,pExpr->pLeft)
2ead8 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ;.  if( pExpr->o
2ead9 70 3e 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20 20  p>=TK_GT ){.    
2eada 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d 3d 54  assert( TK_LT==T
2eadb 4b 5f 47 54 2b 32 20 29 3b 0a 20 20 20 20 61 73  K_GT+2 );.    as
2eadc 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b 5f  sert( TK_GE==TK_
2eadd 4c 45 2b 32 20 29 3b 0a 20 20 20 20 61 73 73 65  LE+2 );.    asse
2eade 72 74 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20  rt( TK_GT>TK_EQ 
2eadf 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 54  );.    assert( T
2eae0 4b 5f 47 54 3c 54 4b 5f 4c 45 20 29 3b 0a 20 20  K_GT<TK_LE );.  
2eae1 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
2eae2 3e 6f 70 3e 3d 54 4b 5f 47 54 20 26 26 20 70 45  >op>=TK_GT && pE
2eae3 78 70 72 2d 3e 6f 70 3c 3d 54 4b 5f 47 45 20 29  xpr->op<=TK_GE )
2eae4 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20  ;.    pExpr->op 
2eae5 3d 20 28 28 70 45 78 70 72 2d 3e 6f 70 2d 54 4b  = ((pExpr->op-TK
2eae6 5f 47 54 29 5e 32 29 2b 54 4b 5f 47 54 3b 0a 20  _GT)^2)+TK_GT;. 
2eae7 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e   }.}../*.** Tran
2eae8 73 6c 61 74 65 20 66 72 6f 6d 20 54 4b 5f 78 78  slate from TK_xx
2eae9 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 57 4f 5f   operator to WO_
2eaea 78 78 20 62 69 74 6d 61 73 6b 2e 0a 2a 2f 0a 73  xx bitmask..*/.s
2eaeb 74 61 74 69 63 20 75 31 36 20 6f 70 65 72 61 74  tatic u16 operat
2eaec 6f 72 4d 61 73 6b 28 69 6e 74 20 6f 70 29 7b 0a  orMask(int op){.
2eaed 20 20 75 31 36 20 63 3b 0a 20 20 61 73 73 65 72    u16 c;.  asser
2eaee 74 28 20 61 6c 6c 6f 77 65 64 4f 70 28 6f 70 29  t( allowedOp(op)
2eaef 20 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b   );.  if( op==TK
2eaf0 5f 49 4e 20 29 7b 0a 20 20 20 20 63 20 3d 20 57  _IN ){.    c = W
2eaf1 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 20 69 66  O_IN;.  }else if
2eaf2 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
2eaf3 29 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49 53  ){.    c = WO_IS
2eaf4 4e 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  NULL;.  }else{. 
2eaf5 20 20 20 61 73 73 65 72 74 28 20 28 57 4f 5f 45     assert( (WO_E
2eaf6 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 29 20 3c  Q<<(op-TK_EQ)) <
2eaf7 20 30 78 37 66 66 66 20 29 3b 0a 20 20 20 20 63   0x7fff );.    c
2eaf8 20 3d 20 28 75 31 36 29 28 57 4f 5f 45 51 3c 3c   = (u16)(WO_EQ<<
2eaf9 28 6f 70 2d 54 4b 5f 45 51 29 29 3b 0a 20 20 7d  (op-TK_EQ));.  }
2eafa 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54  .  assert( op!=T
2eafb 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 63 3d 3d 57  K_ISNULL || c==W
2eafc 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 61 73  O_ISNULL );.  as
2eafd 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 49 4e 20  sert( op!=TK_IN 
2eafe 7c 7c 20 63 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20  || c==WO_IN );. 
2eaff 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f   assert( op!=TK_
2eb00 45 51 20 7c 7c 20 63 3d 3d 57 4f 5f 45 51 20 29  EQ || c==WO_EQ )
2eb01 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d  ;.  assert( op!=
2eb02 54 4b 5f 4c 54 20 7c 7c 20 63 3d 3d 57 4f 5f 4c  TK_LT || c==WO_L
2eb03 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f  T );.  assert( o
2eb04 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 63 3d 3d 57  p!=TK_LE || c==W
2eb05 4f 5f 4c 45 20 29 3b 0a 20 20 61 73 73 65 72 74  O_LE );.  assert
2eb06 28 20 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 63  ( op!=TK_GT || c
2eb07 3d 3d 57 4f 5f 47 54 20 29 3b 0a 20 20 61 73 73  ==WO_GT );.  ass
2eb08 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47 45 20 7c  ert( op!=TK_GE |
2eb09 7c 20 63 3d 3d 57 4f 5f 47 45 20 29 3b 0a 20 20  | c==WO_GE );.  
2eb0a 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 2f 2a 0a  return c;.}../*.
2eb0b 2a 2a 20 53 65 61 72 63 68 20 66 6f 72 20 61 20  ** Search for a 
2eb0c 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45 52  term in the WHER
2eb0d 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 69 73  E clause that is
2eb0e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20   of the form "X 
2eb0f 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 0a 2a 2a 20  <op> <expr>".** 
2eb10 77 68 65 72 65 20 58 20 69 73 20 61 20 72 65 66  where X is a ref
2eb11 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 69 43  erence to the iC
2eb12 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 69  olumn of table i
2eb13 43 75 72 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20  Cur and <op> is 
2eb14 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20 57 4f  one of.** the WO
2eb15 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 63 6f 64  _xx operator cod
2eb16 65 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20  es specified by 
2eb17 74 68 65 20 6f 70 20 70 61 72 61 6d 65 74 65 72  the op parameter
2eb18 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  ..** Return a po
2eb19 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74 65 72  inter to the ter
2eb1a 6d 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 20  m.  Return 0 if 
2eb1b 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 73 74  not found..*/.st
2eb1c 61 74 69 63 20 57 68 65 72 65 54 65 72 6d 20 2a  atic WhereTerm *
2eb1d 66 69 6e 64 54 65 72 6d 28 0a 20 20 57 68 65 72  findTerm(.  Wher
2eb1e 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20  eClause *pWC,   
2eb1f 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
2eb20 6c 61 75 73 65 20 74 6f 20 62 65 20 73 65 61 72  lause to be sear
2eb21 63 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43  ched */.  int iC
2eb22 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ur,             
2eb23 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
2eb24 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20 69 6e 74   of LHS */.  int
2eb25 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20   iColumn,       
2eb26 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d     /* Column num
2eb27 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a 20 20  ber of LHS */.  
2eb28 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
2eb29 2c 20 20 20 20 20 2f 2a 20 52 48 53 20 6d 75 73  ,     /* RHS mus
2eb2a 74 20 6e 6f 74 20 6f 76 65 72 6c 61 70 20 77 69  t not overlap wi
2eb2b 74 68 20 74 68 69 73 20 6d 61 73 6b 20 2a 2f 0a  th this mask */.
2eb2c 20 20 75 33 32 20 6f 70 2c 20 20 20 20 20 20 20    u32 op,       
2eb2d 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
2eb2e 6f 66 20 57 4f 5f 78 78 20 76 61 6c 75 65 73 20  of WO_xx values 
2eb2f 64 65 73 63 72 69 62 69 6e 67 20 6f 70 65 72 61  describing opera
2eb30 74 6f 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  tor */.  Index *
2eb31 70 49 64 78 20 20 20 20 20 20 20 20 20 20 20 2f  pIdx           /
2eb32 2a 20 4d 75 73 74 20 62 65 20 63 6f 6d 70 61 74  * Must be compat
2eb33 69 62 6c 65 20 77 69 74 68 20 74 68 69 73 20 69  ible with this i
2eb34 6e 64 65 78 2c 20 69 66 20 6e 6f 74 20 4e 55 4c  ndex, if not NUL
2eb35 4c 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54  L */.){.  WhereT
2eb36 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e  erm *pTerm;.  in
2eb37 74 20 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 69  t k;.  assert( i
2eb38 43 75 72 3e 3d 30 20 29 3b 0a 20 20 6f 70 20 26  Cur>=0 );.  op &
2eb39 3d 20 57 4f 5f 41 4c 4c 3b 0a 20 20 66 6f 72 28  = WO_ALL;.  for(
2eb3a 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6b 3d  pTerm=pWC->a, k=
2eb3b 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6b 3b 20 6b  pWC->nTerm; k; k
2eb3c 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20  --, pTerm++){.  
2eb3d 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66    if( pTerm->lef
2eb3e 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 0a 20 20  tCursor==iCur.  
2eb3f 20 20 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e       && (pTerm->
2eb40 70 72 65 72 65 71 52 69 67 68 74 20 26 20 6e 6f  prereqRight & no
2eb41 74 52 65 61 64 79 29 3d 3d 30 0a 20 20 20 20 20  tReady)==0.     
2eb42 20 20 26 26 20 70 54 65 72 6d 2d 3e 75 2e 6c 65    && pTerm->u.le
2eb43 66 74 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 75 6d  ftColumn==iColum
2eb44 6e 0a 20 20 20 20 20 20 20 26 26 20 28 70 54 65  n.       && (pTe
2eb45 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2eb46 6f 70 29 21 3d 30 0a 20 20 20 20 29 7b 0a 20 20  op)!=0.    ){.  
2eb47 20 20 20 20 69 66 28 20 70 49 64 78 20 26 26 20      if( pIdx && 
2eb48 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2eb49 21 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20  !=WO_ISNULL ){. 
2eb4a 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 20         Expr *pX 
2eb4b 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a  = pTerm->pExpr;.
2eb4c 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20          CollSeq 
2eb4d 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20  *pColl;.        
2eb4e 63 68 61 72 20 69 64 78 61 66 66 3b 0a 20 20 20  char idxaff;.   
2eb4f 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
2eb50 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73      Parse *pPars
2eb51 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 3b  e = pWC->pParse;
2eb52 0a 0a 20 20 20 20 20 20 20 20 69 64 78 61 66 66  ..        idxaff
2eb53 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d   = pIdx->pTable-
2eb54 3e 61 43 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61  >aCol[iColumn].a
2eb55 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 20  ffinity;.       
2eb56 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 6e 64   if( !sqlite3Ind
2eb57 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 70 58 2c  exAffinityOk(pX,
2eb58 20 69 64 78 61 66 66 29 20 29 20 63 6f 6e 74 69   idxaff) ) conti
2eb59 6e 75 65 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  nue;..        /*
2eb5a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68 65 20   Figure out the 
2eb5b 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2eb5c 63 65 20 72 65 71 75 69 72 65 64 20 66 72 6f 6d  ce required from
2eb5d 20 61 6e 20 69 6e 64 65 78 20 66 6f 72 0a 20 20   an index for.  
2eb5e 20 20 20 20 20 20 2a 2a 20 69 74 20 74 6f 20 62        ** it to b
2eb5f 65 20 75 73 65 66 75 6c 20 66 6f 72 20 6f 70 74  e useful for opt
2eb60 69 6d 69 73 69 6e 67 20 65 78 70 72 65 73 73 69  imising expressi
2eb61 6f 6e 20 70 58 2e 20 53 74 6f 72 65 20 74 68 69  on pX. Store thi
2eb62 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 61 6c  s.        ** val
2eb63 75 65 20 69 6e 20 76 61 72 69 61 62 6c 65 20 70  ue in variable p
2eb64 43 6f 6c 6c 2e 0a 20 20 20 20 20 20 20 20 2a 2f  Coll..        */
2eb65 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2eb66 70 58 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20  pX->pLeft);.    
2eb67 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
2eb68 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65  te3BinaryCompare
2eb69 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2eb6a 70 58 2d 3e 70 4c 65 66 74 2c 20 70 58 2d 3e 70  pX->pLeft, pX->p
2eb6b 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20  Right);.        
2eb6c 61 73 73 65 72 74 28 70 43 6f 6c 6c 20 7c 7c 20  assert(pColl || 
2eb6d 70 50 61 72 73 65 2d 3e 6e 45 72 72 29 3b 0a 0a  pParse->nErr);..
2eb6e 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
2eb6f 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
2eb70 6a 5d 21 3d 69 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b  j]!=iColumn; j++
2eb71 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
2eb72 20 4e 45 56 45 52 28 6a 3e 3d 70 49 64 78 2d 3e   NEVER(j>=pIdx->
2eb73 6e 43 6f 6c 75 6d 6e 29 20 29 20 72 65 74 75 72  nColumn) ) retur
2eb74 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n 0;.        }. 
2eb75 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c         if( pColl
2eb76 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
2eb77 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c  mp(pColl->zName,
2eb78 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6a 5d   pIdx->azColl[j]
2eb79 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  ) ) continue;.  
2eb7a 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
2eb7b 72 6e 20 70 54 65 72 6d 3b 0a 20 20 20 20 7d 0a  rn pTerm;.    }.
2eb7c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
2eb7d 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65  }../* Forward re
2eb7e 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69  ference */.stati
2eb7f 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79  c void exprAnaly
2eb80 7a 65 28 53 72 63 4c 69 73 74 2a 2c 20 57 68 65  ze(SrcList*, Whe
2eb81 72 65 43 6c 61 75 73 65 2a 2c 20 69 6e 74 29 3b  reClause*, int);
2eb82 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 65 78 70  ../*.** Call exp
2eb83 72 41 6e 61 6c 79 7a 65 20 6f 6e 20 61 6c 6c 20  rAnalyze on all 
2eb84 74 65 72 6d 73 20 69 6e 20 61 20 57 48 45 52 45  terms in a WHERE
2eb85 20 63 6c 61 75 73 65 2e 20 20 0a 2a 2a 0a 2a 2a   clause.  .**.**
2eb86 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2eb87 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 0a  exprAnalyzeAll(.
2eb88 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
2eb89 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 74 68  ist,       /* th
2eb8a 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  e FROM clause */
2eb8b 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2eb8c 70 57 43 20 20 20 20 20 20 20 20 20 2f 2a 20 74  pWC         /* t
2eb8d 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2eb8e 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a  to be analyzed *
2eb8f 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
2eb90 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d  for(i=pWC->nTerm
2eb91 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a  -1; i>=0; i--){.
2eb92 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28      exprAnalyze(
2eb93 70 54 61 62 4c 69 73 74 2c 20 70 57 43 2c 20 69  pTabList, pWC, i
2eb94 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65  );.  }.}..#ifnde
2eb95 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49  f SQLITE_OMIT_LI
2eb96 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a  KE_OPTIMIZATION.
2eb97 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73  /*.** Check to s
2eb98 65 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20  ee if the given 
2eb99 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
2eb9a 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 6f 70 65  LIKE or GLOB ope
2eb9b 72 61 74 6f 72 20 74 68 61 74 0a 2a 2a 20 63 61  rator that.** ca
2eb9c 6e 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 75  n be optimized u
2eb9d 73 69 6e 67 20 69 6e 65 71 75 61 6c 69 74 79 20  sing inequality 
2eb9e 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 52 65  constraints.  Re
2eb9f 74 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 20  turn TRUE if it 
2eba0 69 73 0a 2a 2a 20 73 6f 20 61 6e 64 20 66 61 6c  is.** so and fal
2eba1 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a  se if not..**.**
2eba2 20 49 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68   In order for th
2eba3 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65  e operator to be
2eba4 20 6f 70 74 69 6d 69 7a 69 62 6c 65 2c 20 74 68   optimizible, th
2eba5 65 20 52 48 53 20 6d 75 73 74 20 62 65 20 61 20  e RHS must be a 
2eba6 73 74 72 69 6e 67 0a 2a 2a 20 6c 69 74 65 72 61  string.** litera
2eba7 6c 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  l that does not 
2eba8 62 65 67 69 6e 20 77 69 74 68 20 61 20 77 69 6c  begin with a wil
2eba9 64 63 61 72 64 2e 20 20 0a 2a 2f 0a 73 74 61 74  dcard.  .*/.stat
2ebaa 69 63 20 69 6e 74 20 69 73 4c 69 6b 65 4f 72 47  ic int isLikeOrG
2ebab 6c 6f 62 28 0a 20 20 50 61 72 73 65 20 2a 70 50  lob(.  Parse *pP
2ebac 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73  arse,    /* Pars
2ebad 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ing and code gen
2ebae 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20  erating context 
2ebaf 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
2ebb0 2c 20 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74  ,      /* Test t
2ebb1 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  his expression *
2ebb2 2f 0a 20 20 45 78 70 72 20 2a 2a 70 70 50 72 65  /.  Expr **ppPre
2ebb3 66 69 78 2c 20 20 2f 2a 20 50 6f 69 6e 74 65 72  fix,  /* Pointer
2ebb4 20 74 6f 20 54 4b 5f 53 54 52 49 4e 47 20 65 78   to TK_STRING ex
2ebb5 70 72 65 73 73 69 6f 6e 20 77 69 74 68 20 70 61  pression with pa
2ebb6 74 74 65 72 6e 20 70 72 65 66 69 78 20 2a 2f 0a  ttern prefix */.
2ebb7 20 20 69 6e 74 20 2a 70 69 73 43 6f 6d 70 6c 65    int *pisComple
2ebb8 74 65 2c 20 2f 2a 20 54 72 75 65 20 69 66 20 74  te, /* True if t
2ebb9 68 65 20 6f 6e 6c 79 20 77 69 6c 64 63 61 72 64  he only wildcard
2ebba 20 69 73 20 25 20 69 6e 20 74 68 65 20 6c 61 73   is % in the las
2ebbb 74 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20  t character */. 
2ebbc 20 69 6e 74 20 2a 70 6e 6f 43 61 73 65 20 20 20   int *pnoCase   
2ebbd 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75 70     /* True if up
2ebbe 70 65 72 63 61 73 65 20 69 73 20 65 71 75 69 76  percase is equiv
2ebbf 61 6c 65 6e 74 20 74 6f 20 6c 6f 77 65 72 63 61  alent to lowerca
2ebc0 73 65 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74  se */.){.  const
2ebc1 20 63 68 61 72 20 2a 7a 20 3d 20 30 3b 20 20 20   char *z = 0;   
2ebc2 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20        /* String 
2ebc3 6f 6e 20 52 48 53 20 6f 66 20 4c 49 4b 45 20 6f  on RHS of LIKE o
2ebc4 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 45 78 70  perator */.  Exp
2ebc5 72 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65 66  r *pRight, *pLef
2ebc6 74 3b 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74  t;      /* Right
2ebc7 20 61 6e 64 20 6c 65 66 74 20 73 69 7a 65 20 6f   and left size o
2ebc8 66 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20  f LIKE operator 
2ebc9 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2ebca 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  List;           
2ebcb 2f 2a 20 4c 69 73 74 20 6f 66 20 6f 70 65 72 61  /* List of opera
2ebcc 6e 64 73 20 74 6f 20 74 68 65 20 4c 49 4b 45 20  nds to the LIKE 
2ebcd 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e  operator */.  in
2ebce 74 20 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  t c;            
2ebcf 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
2ebd0 63 68 61 72 61 63 74 65 72 20 69 6e 20 7a 5b 5d  character in z[]
2ebd1 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20   */.  int cnt;  
2ebd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ebd3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f   /* Number of no
2ebd4 6e 2d 77 69 6c 64 63 61 72 64 20 70 72 65 66 69  n-wildcard prefi
2ebd5 78 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a  x characters */.
2ebd6 20 20 63 68 61 72 20 77 63 5b 33 5d 3b 20 20 20    char wc[3];   
2ebd7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ebd8 57 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74  Wildcard charact
2ebd9 65 72 73 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71  ers */.  CollSeq
2ebda 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20   *pColl;        
2ebdb 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67      /* Collating
2ebdc 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 4c 48   sequence for LH
2ebdd 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  S */.  sqlite3 *
2ebde 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2ebdf 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
2ebe0 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71  nnection */.  sq
2ebe1 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
2ebe2 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6f 70 3b  l = 0;.  int op;
2ebe3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ebe4 20 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 6f 66      /* Opcode of
2ebe5 20 70 52 69 67 68 74 20 2a 2f 0a 0a 20 20 69 66   pRight */..  if
2ebe6 28 20 21 73 71 6c 69 74 65 33 49 73 4c 69 6b 65  ( !sqlite3IsLike
2ebe7 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 70 45 78  Function(db, pEx
2ebe8 70 72 2c 20 70 6e 6f 43 61 73 65 2c 20 77 63 29  pr, pnoCase, wc)
2ebe9 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2ebea 3b 0a 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c  ;.  }.#ifdef SQL
2ebeb 49 54 45 5f 45 42 43 44 49 43 0a 20 20 69 66 28  ITE_EBCDIC.  if(
2ebec 20 2a 70 6e 6f 43 61 73 65 20 29 20 72 65 74 75   *pnoCase ) retu
2ebed 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 70  rn 0;.#endif.  p
2ebee 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e  List = pExpr->x.
2ebef 70 4c 69 73 74 3b 0a 20 20 70 4c 65 66 74 20 3d  pList;.  pLeft =
2ebf0 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78   pList->a[1].pEx
2ebf1 70 72 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 2d  pr;.  if( pLeft-
2ebf2 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c  >op!=TK_COLUMN |
2ebf3 7c 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  | sqlite3ExprAff
2ebf4 69 6e 69 74 79 28 70 4c 65 66 74 29 21 3d 53 51  inity(pLeft)!=SQ
2ebf5 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 29 7b  LITE_AFF_TEXT ){
2ebf6 0a 20 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 30  .    /* IMP: R-0
2ebf7 32 30 36 35 2d 34 39 34 36 35 20 54 68 65 20 6c  2065-49465 The l
2ebf8 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  eft-hand side of
2ebf9 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c 4f   the LIKE or GLO
2ebfa 42 20 6f 70 65 72 61 74 6f 72 20 6d 75 73 74 0a  B operator must.
2ebfb 20 20 20 20 2a 2a 20 62 65 20 74 68 65 20 6e 61      ** be the na
2ebfc 6d 65 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64  me of an indexed
2ebfd 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 54 45 58   column with TEX
2ebfe 54 20 61 66 66 69 6e 69 74 79 2e 20 2a 2f 0a 20  T affinity. */. 
2ebff 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2ec00 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74  .  assert( pLeft
2ec01 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 28 2d 31 29 20  ->iColumn!=(-1) 
2ec02 29 3b 20 2f 2a 20 42 65 63 61 75 73 65 20 49 50  ); /* Because IP
2ec03 4b 20 6e 65 76 65 72 20 68 61 73 20 41 46 46 5f  K never has AFF_
2ec04 54 45 58 54 20 2a 2f 0a 20 20 70 43 6f 6c 6c 20  TEXT */.  pColl 
2ec05 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
2ec06 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65  lSeq(pParse, pLe
2ec07 66 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ft);.  assert( p
2ec08 43 6f 6c 6c 21 3d 30 20 29 3b 20 20 2f 2a 20 45  Coll!=0 );  /* E
2ec09 76 65 72 79 20 6e 6f 6e 2d 49 50 4b 20 63 6f 6c  very non-IPK col
2ec0a 75 6d 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61 74  umn has a collat
2ec0b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ing sequence */.
2ec0c 20 20 69 66 28 20 28 70 43 6f 6c 6c 2d 3e 74 79    if( (pColl->ty
2ec0d 70 65 21 3d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f  pe!=SQLITE_COLL_
2ec0e 42 49 4e 41 52 59 20 7c 7c 20 2a 70 6e 6f 43 61  BINARY || *pnoCa
2ec0f 73 65 29 20 26 26 0a 20 20 20 20 20 20 28 70 43  se) &&.      (pC
2ec10 6f 6c 6c 2d 3e 74 79 70 65 21 3d 53 51 4c 49 54  oll->type!=SQLIT
2ec11 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 7c 7c  E_COLL_NOCASE ||
2ec12 20 21 2a 70 6e 6f 43 61 73 65 29 20 29 7b 0a 20   !*pnoCase) ){. 
2ec13 20 20 20 2f 2a 20 49 4d 50 3a 20 52 2d 30 39 30     /* IMP: R-090
2ec14 30 33 2d 33 32 30 34 36 20 46 6f 72 20 74 68 65  03-32046 For the
2ec15 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 2c 20   GLOB operator, 
2ec16 74 68 65 20 63 6f 6c 75 6d 6e 20 6d 75 73 74 20  the column must 
2ec17 75 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64  use the.    ** d
2ec18 65 66 61 75 6c 74 20 42 49 4e 41 52 59 20 63 6f  efault BINARY co
2ec19 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2ec1a 2e 0a 20 20 20 20 2a 2a 20 49 4d 50 3a 20 52 2d  ..    ** IMP: R-
2ec1b 34 31 34 30 38 2d 32 38 33 30 36 20 46 6f 72 20  41408-28306 For 
2ec1c 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  the LIKE operato
2ec1d 72 2c 20 69 66 20 63 61 73 65 5f 73 65 6e 73 69  r, if case_sensi
2ec1e 74 69 76 65 5f 6c 69 6b 65 20 6d 6f 64 65 0a 20  tive_like mode. 
2ec1f 20 20 20 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64     ** is enabled
2ec20 20 74 68 65 6e 20 74 68 65 20 63 6f 6c 75 6d 6e   then the column
2ec21 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 64 65   must use the de
2ec22 66 61 75 6c 74 20 42 49 4e 41 52 59 20 63 6f 6c  fault BINARY col
2ec23 6c 61 74 69 6e 67 0a 20 20 20 20 2a 2a 20 73 65  lating.    ** se
2ec24 71 75 65 6e 63 65 2c 20 6f 72 20 69 66 20 63 61  quence, or if ca
2ec25 73 65 5f 73 65 6e 73 69 74 69 76 65 5f 6c 69 6b  se_sensitive_lik
2ec26 65 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c  e mode is disabl
2ec27 65 64 20 74 68 65 6e 20 74 68 65 20 63 6f 6c 75  ed then the colu
2ec28 6d 6e 0a 20 20 20 20 2a 2a 20 6d 75 73 74 20 75  mn.    ** must u
2ec29 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  se the built-in 
2ec2a 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 74 69 6e 67  NOCASE collating
2ec2b 20 73 65 71 75 65 6e 63 65 2e 0a 20 20 20 20 2a   sequence..    *
2ec2c 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  /.    return 0;.
2ec2d 20 20 7d 0a 0a 20 20 70 52 69 67 68 74 20 3d 20    }..  pRight = 
2ec2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  pList->a[0].pExp
2ec2f 72 3b 0a 20 20 6f 70 20 3d 20 70 52 69 67 68 74  r;.  op = pRight
2ec30 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  ->op;.  if( op==
2ec31 54 4b 5f 52 45 47 49 53 54 45 52 20 29 7b 0a 20  TK_REGISTER ){. 
2ec32 20 20 20 6f 70 20 3d 20 70 52 69 67 68 74 2d 3e     op = pRight->
2ec33 6f 70 32 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f  op2;.  }.  if( o
2ec34 70 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20 29  p==TK_VARIABLE )
2ec35 7b 0a 20 20 20 20 56 64 62 65 20 2a 70 52 65 70  {.    Vdbe *pRep
2ec36 72 65 70 61 72 65 20 3d 20 70 50 61 72 73 65 2d  repare = pParse-
2ec37 3e 70 52 65 70 72 65 70 61 72 65 3b 0a 20 20 20  >pReprepare;.   
2ec38 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56   pVal = sqlite3V
2ec39 64 62 65 47 65 74 56 61 6c 75 65 28 70 52 65 70  dbeGetValue(pRep
2ec3a 72 65 70 61 72 65 2c 20 70 52 69 67 68 74 2d 3e  repare, pRight->
2ec3b 69 43 6f 6c 75 6d 6e 2c 20 53 51 4c 49 54 45 5f  iColumn, SQLITE_
2ec3c 41 46 46 5f 4e 4f 4e 45 29 3b 0a 20 20 20 20 69  AFF_NONE);.    i
2ec3d 66 28 20 70 56 61 6c 20 26 26 20 73 71 6c 69 74  f( pVal && sqlit
2ec3e 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 70 56  e3_value_type(pV
2ec3f 61 6c 29 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54  al)==SQLITE_TEXT
2ec40 20 29 7b 0a 20 20 20 20 20 20 7a 20 3d 20 28 63   ){.      z = (c
2ec41 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
2ec42 6c 75 65 5f 74 65 78 74 28 70 56 61 6c 29 3b 0a  lue_text(pVal);.
2ec43 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2ec44 33 56 64 62 65 53 65 74 56 61 72 6d 61 73 6b 28  3VdbeSetVarmask(
2ec45 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 70  pParse->pVdbe, p
2ec46 52 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 29 3b  Right->iColumn);
2ec47 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52 69  .    assert( pRi
2ec48 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 56 41 52 49  ght->op==TK_VARI
2ec49 41 42 4c 45 20 7c 7c 20 70 52 69 67 68 74 2d 3e  ABLE || pRight->
2ec4a 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20  op==TK_REGISTER 
2ec4b 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f  );.  }else if( o
2ec4c 70 3d 3d 54 4b 5f 53 54 52 49 4e 47 20 29 7b 0a  p==TK_STRING ){.
2ec4d 20 20 20 20 7a 20 3d 20 70 52 69 67 68 74 2d 3e      z = pRight->
2ec4e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 7d 0a 20 20  u.zToken;.  }.  
2ec4f 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 63 6e 74  if( z ){.    cnt
2ec50 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28   = 0;.    while(
2ec51 20 28 63 3d 7a 5b 63 6e 74 5d 29 21 3d 30 20 26   (c=z[cnt])!=0 &
2ec52 26 20 63 21 3d 77 63 5b 30 5d 20 26 26 20 63 21  & c!=wc[0] && c!
2ec53 3d 77 63 5b 31 5d 20 26 26 20 63 21 3d 77 63 5b  =wc[1] && c!=wc[
2ec54 32 5d 20 29 7b 0a 20 20 20 20 20 20 63 6e 74 2b  2] ){.      cnt+
2ec55 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  +;.    }.    if(
2ec56 20 63 6e 74 21 3d 30 20 26 26 20 63 21 3d 30 20   cnt!=0 && c!=0 
2ec57 26 26 20 32 35 35 21 3d 28 75 38 29 7a 5b 63 6e  && 255!=(u8)z[cn
2ec58 74 2d 31 5d 20 29 7b 0a 20 20 20 20 20 20 45 78  t-1] ){.      Ex
2ec59 70 72 20 2a 70 50 72 65 66 69 78 3b 0a 20 20 20  pr *pPrefix;.   
2ec5a 20 20 20 2a 70 69 73 43 6f 6d 70 6c 65 74 65 20     *pisComplete 
2ec5b 3d 20 7a 5b 63 6e 74 5d 3d 3d 77 63 5b 30 5d 20  = z[cnt]==wc[0] 
2ec5c 26 26 20 7a 5b 63 6e 74 2b 31 5d 3d 3d 30 3b 0a  && z[cnt+1]==0;.
2ec5d 20 20 20 20 20 20 70 50 72 65 66 69 78 20 3d 20        pPrefix = 
2ec5e 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20  sqlite3Expr(db, 
2ec5f 54 4b 5f 53 54 52 49 4e 47 2c 20 7a 29 3b 0a 20  TK_STRING, z);. 
2ec60 20 20 20 20 20 69 66 28 20 70 50 72 65 66 69 78       if( pPrefix
2ec61 20 29 20 70 50 72 65 66 69 78 2d 3e 75 2e 7a 54   ) pPrefix->u.zT
2ec62 6f 6b 65 6e 5b 63 6e 74 5d 20 3d 20 30 3b 0a 20  oken[cnt] = 0;. 
2ec63 20 20 20 20 20 2a 70 70 50 72 65 66 69 78 20 3d       *ppPrefix =
2ec64 20 70 50 72 65 66 69 78 3b 0a 20 20 20 20 20 20   pPrefix;.      
2ec65 69 66 28 20 6f 70 3d 3d 54 4b 5f 56 41 52 49 41  if( op==TK_VARIA
2ec66 42 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 56  BLE ){.        V
2ec67 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
2ec68 3e 70 56 64 62 65 3b 0a 20 20 20 20 20 20 20 20  >pVdbe;.        
2ec69 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61  sqlite3VdbeSetVa
2ec6a 72 6d 61 73 6b 28 76 2c 20 70 52 69 67 68 74 2d  rmask(v, pRight-
2ec6b 3e 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20  >iColumn);.     
2ec6c 20 20 20 69 66 28 20 2a 70 69 73 43 6f 6d 70 6c     if( *pisCompl
2ec6d 65 74 65 20 26 26 20 70 52 69 67 68 74 2d 3e 75  ete && pRight->u
2ec6e 2e 7a 54 6f 6b 65 6e 5b 31 5d 20 29 7b 0a 20 20  .zToken[1] ){.  
2ec6f 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
2ec70 65 20 72 68 73 20 6f 66 20 74 68 65 20 4c 49 4b  e rhs of the LIK
2ec71 45 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  E expression is 
2ec72 61 20 76 61 72 69 61 62 6c 65 2c 20 61 6e 64 20  a variable, and 
2ec73 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20 20 20  the current.    
2ec74 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 20 6f        ** value o
2ec75 66 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 6d  f the variable m
2ec76 65 61 6e 73 20 74 68 65 72 65 20 69 73 20 6e 6f  eans there is no
2ec77 20 6e 65 65 64 20 74 6f 20 69 6e 76 6f 6b 65 20   need to invoke 
2ec78 74 68 65 20 4c 49 4b 45 0a 20 20 20 20 20 20 20  the LIKE.       
2ec79 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2c 20     ** function, 
2ec7a 74 68 65 6e 20 6e 6f 20 4f 50 5f 56 61 72 69 61  then no OP_Varia
2ec7b 62 6c 65 20 77 69 6c 6c 20 62 65 20 61 64 64 65  ble will be adde
2ec7c 64 20 74 6f 20 74 68 65 20 70 72 6f 67 72 61 6d  d to the program
2ec7d 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54  ..          ** T
2ec7e 68 69 73 20 63 61 75 73 65 73 20 70 72 6f 62 6c  his causes probl
2ec7f 65 6d 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69  ems for the sqli
2ec80 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
2ec81 65 72 5f 6e 61 6d 65 28 29 0a 20 20 20 20 20 20  er_name().      
2ec82 20 20 20 20 2a 2a 20 41 50 49 2e 20 54 6f 20 77      ** API. To w
2ec83 6f 72 6b 61 72 6f 75 6e 64 20 74 68 65 6d 2c 20  orkaround them, 
2ec84 61 64 64 20 61 20 64 75 6d 6d 79 20 4f 50 5f 56  add a dummy OP_V
2ec85 61 72 69 61 62 6c 65 20 68 65 72 65 2e 0a 20 20  ariable here..  
2ec86 20 20 20 20 20 20 20 20 2a 2f 20 0a 20 20 20 20          */ .    
2ec87 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73        int r1 = s
2ec88 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2ec89 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2ec8a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2ec8b 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65  odeTarget(pParse
2ec8c 2c 20 70 52 69 67 68 74 2c 20 72 31 29 3b 0a 20  , pRight, r1);. 
2ec8d 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2ec8e 56 64 62 65 43 68 61 6e 67 65 50 33 28 76 2c 20  VdbeChangeP3(v, 
2ec8f 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2ec90 6e 74 41 64 64 72 28 76 29 2d 31 2c 20 30 29 3b  ntAddr(v)-1, 0);
2ec91 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2ec92 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2ec93 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
2ec94 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2ec95 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2ec96 20 7a 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20   z = 0;.    }.  
2ec97 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75  }..  sqlite3Valu
2ec98 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 72  eFree(pVal);.  r
2ec99 65 74 75 72 6e 20 28 7a 21 3d 30 29 3b 0a 7d 0a  eturn (z!=0);.}.
2ec9a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2ec9b 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d  _OMIT_LIKE_OPTIM
2ec9c 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 23 69 66  IZATION */...#if
2ec9d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2ec9e 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
2ec9f 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
2eca0 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 65 78   if the given ex
2eca1 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74  pression is of t
2eca2 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20  he form.**.**   
2eca3 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20 4d 41 54        column MAT
2eca4 43 48 20 65 78 70 72 0a 2a 2a 0a 2a 2a 20 49 66  CH expr.**.** If
2eca5 20 69 74 20 69 73 20 74 68 65 6e 20 72 65 74 75   it is then retu
2eca6 72 6e 20 54 52 55 45 2e 20 20 49 66 20 6e 6f 74  rn TRUE.  If not
2eca7 2c 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a  , return FALSE..
2eca8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  */.static int is
2eca9 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e 28 0a 20  MatchOfColumn(. 
2ecaa 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20   Expr *pExpr    
2ecab 20 20 2f 2a 20 54 65 73 74 20 74 68 69 73 20 65    /* Test this e
2ecac 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a  xpression */.){.
2ecad 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2ecae 74 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 2d  t;..  if( pExpr-
2ecaf 3e 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e  >op!=TK_FUNCTION
2ecb0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2ecb1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69  ;.  }.  if( sqli
2ecb2 74 65 33 53 74 72 49 43 6d 70 28 70 45 78 70 72  te3StrICmp(pExpr
2ecb3 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d 61 74 63  ->u.zToken,"matc
2ecb4 68 22 29 21 3d 30 20 29 7b 0a 20 20 20 20 72 65  h")!=0 ){.    re
2ecb5 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c  turn 0;.  }.  pL
2ecb6 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  ist = pExpr->x.p
2ecb7 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 4c 69 73  List;.  if( pLis
2ecb8 74 2d 3e 6e 45 78 70 72 21 3d 32 20 29 7b 0a 20  t->nExpr!=2 ){. 
2ecb9 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2ecba 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b  .  if( pList->a[
2ecbb 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 20 21 3d 20  1].pExpr->op != 
2ecbc 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20  TK_COLUMN ){.   
2ecbd 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2ecbe 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e   return 1;.}.#en
2ecbf 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2ecc0 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
2ecc1 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  */../*.** If the
2ecc2 20 70 42 61 73 65 20 65 78 70 72 65 73 73 69 6f   pBase expressio
2ecc3 6e 20 6f 72 69 67 69 6e 61 74 65 64 20 69 6e 20  n originated in 
2ecc4 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20  the ON or USING 
2ecc5 63 6c 61 75 73 65 20 6f 66 0a 2a 2a 20 61 20 6a  clause of.** a j
2ecc6 6f 69 6e 2c 20 74 68 65 6e 20 74 72 61 6e 73 66  oin, then transf
2ecc7 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  er the appropria
2ecc8 74 65 20 6d 61 72 6b 69 6e 67 73 20 6f 76 65 72  te markings over
2ecc9 20 74 6f 20 64 65 72 69 76 65 64 2e 0a 2a 2f 0a   to derived..*/.
2ecca 73 74 61 74 69 63 20 76 6f 69 64 20 74 72 61 6e  static void tran
2eccb 73 66 65 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73  sferJoinMarkings
2eccc 28 45 78 70 72 20 2a 70 44 65 72 69 76 65 64 2c  (Expr *pDerived,
2eccd 20 45 78 70 72 20 2a 70 42 61 73 65 29 7b 0a 20   Expr *pBase){. 
2ecce 20 70 44 65 72 69 76 65 64 2d 3e 66 6c 61 67 73   pDerived->flags
2eccf 20 7c 3d 20 70 42 61 73 65 2d 3e 66 6c 61 67 73   |= pBase->flags
2ecd0 20 26 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 3b 0a   & EP_FromJoin;.
2ecd1 20 20 70 44 65 72 69 76 65 64 2d 3e 69 52 69 67    pDerived->iRig
2ecd2 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d 20 70 42  htJoinTable = pB
2ecd3 61 73 65 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54  ase->iRightJoinT
2ecd4 61 62 6c 65 3b 0a 7d 0a 0a 23 69 66 20 21 64 65  able;.}..#if !de
2ecd5 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2ecd6 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  T_OR_OPTIMIZATIO
2ecd7 4e 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  N) && !defined(S
2ecd8 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2ecd9 45 52 59 29 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79  ERY)./*.** Analy
2ecda 7a 65 20 61 20 74 65 72 6d 20 74 68 61 74 20 63  ze a term that c
2ecdb 6f 6e 73 69 73 74 73 20 6f 66 20 74 77 6f 20 6f  onsists of two o
2ecdc 72 20 6d 6f 72 65 20 4f 52 2d 63 6f 6e 6e 65 63  r more OR-connec
2ecdd 74 65 64 0a 2a 2a 20 73 75 62 74 65 72 6d 73 2e  ted.** subterms.
2ecde 20 20 53 6f 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20    So in:.**.**  
2ecdf 20 20 20 2e 2e 2e 20 57 48 45 52 45 20 20 28 61     ... WHERE  (a
2ece0 3d 35 29 20 41 4e 44 20 28 62 3d 37 20 4f 52 20  =5) AND (b=7 OR 
2ece1 63 3d 39 20 4f 52 20 64 3d 31 33 29 20 41 4e 44  c=9 OR d=13) AND
2ece2 20 28 64 3d 31 33 29 0a 2a 2a 20 20 20 20 20 20   (d=13).**      
2ece3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ece4 20 20 20 20 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e      ^^^^^^^^^^^^
2ece5 5e 5e 5e 5e 5e 5e 5e 5e 0a 2a 2a 0a 2a 2a 20 54  ^^^^^^^^.**.** T
2ece6 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6e 61 6c  his routine anal
2ece7 79 7a 65 73 20 74 65 72 6d 73 20 73 75 63 68 20  yzes terms such 
2ece8 61 73 20 74 68 65 20 6d 69 64 64 6c 65 20 74 65  as the middle te
2ece9 72 6d 20 69 6e 20 74 68 65 20 61 62 6f 76 65 20  rm in the above 
2ecea 65 78 61 6d 70 6c 65 2e 0a 2a 2a 20 41 20 57 68  example..** A Wh
2eceb 65 72 65 4f 72 54 65 72 6d 20 6f 62 6a 65 63 74  ereOrTerm object
2ecec 20 69 73 20 63 6f 6d 70 75 74 65 64 20 61 6e 64   is computed and
2eced 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65   attached to the
2ecee 20 74 65 72 6d 20 75 6e 64 65 72 0a 2a 2a 20 61   term under.** a
2ecef 6e 61 6c 79 73 69 73 2c 20 72 65 67 61 72 64 6c  nalysis, regardl
2ecf0 65 73 73 20 6f 66 20 74 68 65 20 6f 75 74 63 6f  ess of the outco
2ecf1 6d 65 20 6f 66 20 74 68 65 20 61 6e 61 6c 79 73  me of the analys
2ecf2 69 73 2e 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a  is.  Hence:.**.*
2ecf3 2a 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e  *     WhereTerm.
2ecf4 77 74 46 6c 61 67 73 20 20 20 7c 3d 20 20 54 45  wtFlags   |=  TE
2ecf5 52 4d 5f 4f 52 49 4e 46 4f 0a 2a 2a 20 20 20 20  RM_ORINFO.**    
2ecf6 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f 72   WhereTerm.u.pOr
2ecf7 49 6e 66 6f 20 20 3d 20 20 61 20 64 79 6e 61 6d  Info  =  a dynam
2ecf8 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64  ically allocated
2ecf9 20 57 68 65 72 65 4f 72 54 65 72 6d 20 6f 62 6a   WhereOrTerm obj
2ecfa 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65  ect.**.** The te
2ecfb 72 6d 20 62 65 69 6e 67 20 61 6e 61 6c 79 7a 65  rm being analyze
2ecfc 64 20 6d 75 73 74 20 68 61 76 65 20 74 77 6f 20  d must have two 
2ecfd 6f 72 20 6d 6f 72 65 20 6f 66 20 4f 52 2d 63 6f  or more of OR-co
2ecfe 6e 6e 65 63 74 65 64 20 73 75 62 74 65 72 6d 73  nnected subterms
2ecff 2e 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 73 75  ..** A single su
2ed00 62 74 65 72 6d 20 6d 69 67 68 74 20 62 65 20 61  bterm might be a
2ed01 20 73 65 74 20 6f 66 20 41 4e 44 2d 63 6f 6e 6e   set of AND-conn
2ed02 65 63 74 65 64 20 73 75 62 2d 73 75 62 74 65 72  ected sub-subter
2ed03 6d 73 2e 0a 2a 2a 20 45 78 61 6d 70 6c 65 73 20  ms..** Examples 
2ed04 6f 66 20 74 65 72 6d 73 20 75 6e 64 65 72 20 61  of terms under a
2ed05 6e 61 6c 79 73 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  nalysis:.**.**  
2ed06 20 20 20 28 41 29 20 20 20 20 20 74 31 2e 78 3d     (A)     t1.x=
2ed07 74 32 2e 79 20 4f 52 20 74 31 2e 78 3d 74 32 2e  t2.y OR t1.x=t2.
2ed08 7a 20 4f 52 20 74 31 2e 79 3d 31 35 20 4f 52 20  z OR t1.y=15 OR 
2ed09 74 31 2e 7a 3d 74 33 2e 61 2b 35 0a 2a 2a 20 20  t1.z=t3.a+5.**  
2ed0a 20 20 20 28 42 29 20 20 20 20 20 78 3d 65 78 70     (B)     x=exp
2ed0b 72 31 20 4f 52 20 65 78 70 72 32 3d 78 20 4f 52  r1 OR expr2=x OR
2ed0c 20 78 3d 65 78 70 72 33 0a 2a 2a 20 20 20 20 20   x=expr3.**     
2ed0d 28 43 29 20 20 20 20 20 74 31 2e 78 3d 74 32 2e  (C)     t1.x=t2.
2ed0e 79 20 4f 52 20 28 74 31 2e 78 3d 74 32 2e 7a 20  y OR (t1.x=t2.z 
2ed0f 41 4e 44 20 74 31 2e 79 3d 31 35 29 0a 2a 2a 20  AND t1.y=15).** 
2ed10 20 20 20 20 28 44 29 20 20 20 20 20 78 3d 65 78      (D)     x=ex
2ed11 70 72 31 20 4f 52 20 28 79 3e 31 31 20 41 4e 44  pr1 OR (y>11 AND
2ed12 20 79 3c 32 32 20 41 4e 44 20 7a 20 4c 49 4b 45   y<22 AND z LIKE
2ed13 20 27 2a 68 65 6c 6c 6f 2a 27 29 0a 2a 2a 20 20   '*hello*').**  
2ed14 20 20 20 28 45 29 20 20 20 20 20 28 70 2e 61 3d     (E)     (p.a=
2ed15 31 20 41 4e 44 20 71 2e 62 3d 32 20 41 4e 44 20  1 AND q.b=2 AND 
2ed16 72 2e 63 3d 33 29 20 4f 52 20 28 70 2e 78 3d 34  r.c=3) OR (p.x=4
2ed17 20 41 4e 44 20 71 2e 79 3d 35 20 41 4e 44 20 72   AND q.y=5 AND r
2ed18 2e 7a 3d 36 29 0a 2a 2a 0a 2a 2a 20 43 41 53 45  .z=6).**.** CASE
2ed19 20 31 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6c 6c   1:.**.** If all
2ed1a 20 73 75 62 74 65 72 6d 73 20 61 72 65 20 6f 66   subterms are of
2ed1b 20 74 68 65 20 66 6f 72 6d 20 54 2e 43 3d 65 78   the form T.C=ex
2ed1c 70 72 20 66 6f 72 20 73 6f 6d 65 20 73 69 6e 67  pr for some sing
2ed1d 6c 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 43 0a 2a  le column of C.*
2ed1e 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  * a single table
2ed1f 20 54 20 28 61 73 20 73 68 6f 77 6e 20 69 6e 20   T (as shown in 
2ed20 65 78 61 6d 70 6c 65 20 42 20 61 62 6f 76 65 29  example B above)
2ed21 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20 6e   then create a n
2ed22 65 77 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 65  ew virtual.** te
2ed23 72 6d 20 74 68 61 74 20 69 73 20 61 6e 20 65 71  rm that is an eq
2ed24 75 69 76 61 6c 65 6e 74 20 49 4e 20 65 78 70 72  uivalent IN expr
2ed25 65 73 73 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 65  ession.  In othe
2ed26 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20  r words, if the 
2ed27 74 65 72 6d 0a 2a 2a 20 62 65 69 6e 67 20 61 6e  term.** being an
2ed28 61 6c 79 7a 65 64 20 69 73 3a 0a 2a 2a 0a 2a 2a  alyzed is:.**.**
2ed29 20 20 20 20 20 20 78 20 3d 20 65 78 70 72 31 20        x = expr1 
2ed2a 20 4f 52 20 20 65 78 70 72 32 20 3d 20 78 20 20   OR  expr2 = x  
2ed2b 4f 52 20 20 78 20 3d 20 65 78 70 72 33 0a 2a 2a  OR  x = expr3.**
2ed2c 0a 2a 2a 20 74 68 65 6e 20 63 72 65 61 74 65 20  .** then create 
2ed2d 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 65  a new virtual te
2ed2e 72 6d 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  rm like this:.**
2ed2f 0a 2a 2a 20 20 20 20 20 20 78 20 49 4e 20 28 65  .**      x IN (e
2ed30 78 70 72 31 2c 65 78 70 72 32 2c 65 78 70 72 33  xpr1,expr2,expr3
2ed31 29 0a 2a 2a 0a 2a 2a 20 43 41 53 45 20 32 3a 0a  ).**.** CASE 2:.
2ed32 2a 2a 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75 62  **.** If all sub
2ed33 74 65 72 6d 73 20 61 72 65 20 69 6e 64 65 78 61  terms are indexa
2ed34 62 6c 65 20 62 79 20 61 20 73 69 6e 67 6c 65 20  ble by a single 
2ed35 74 61 62 6c 65 20 54 2c 20 74 68 65 6e 20 73 65  table T, then se
2ed36 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 57 68 65 72  t.**.**     Wher
2ed37 65 54 65 72 6d 2e 65 4f 70 65 72 61 74 6f 72 20  eTerm.eOperator 
2ed38 20 20 20 20 20 20 20 20 20 20 20 20 20 3d 20 20               =  
2ed39 57 4f 5f 4f 52 0a 2a 2a 20 20 20 20 20 57 68 65  WO_OR.**     Whe
2ed3a 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f  reTerm.u.pOrInfo
2ed3b 2d 3e 69 6e 64 65 78 61 62 6c 65 20 20 7c 3d 20  ->indexable  |= 
2ed3c 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62   the cursor numb
2ed3d 65 72 20 66 6f 72 20 74 61 62 6c 65 20 54 0a 2a  er for table T.*
2ed3e 2a 0a 2a 2a 20 41 20 73 75 62 74 65 72 6d 20 69  *.** A subterm i
2ed3f 73 20 22 69 6e 64 65 78 61 62 6c 65 22 20 69 66  s "indexable" if
2ed40 20 69 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f   it is of the fo
2ed41 72 6d 0a 2a 2a 20 22 54 2e 43 20 3c 6f 70 3e 20  rm.** "T.C <op> 
2ed42 3c 65 78 70 72 3e 22 20 77 68 65 72 65 20 43 20  <expr>" where C 
2ed43 69 73 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f 66  is any column of
2ed44 20 74 61 62 6c 65 20 54 20 61 6e 64 20 0a 2a 2a   table T and .**
2ed45 20 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f 66 20   <op> is one of 
2ed46 22 3d 22 2c 20 22 3c 22 2c 20 22 3c 3d 22 2c 20  "=", "<", "<=", 
2ed47 22 3e 22 2c 20 22 3e 3d 22 2c 20 22 49 53 20 4e  ">", ">=", "IS N
2ed48 55 4c 4c 22 2c 20 6f 72 20 22 49 4e 22 2e 0a 2a  ULL", or "IN"..*
2ed49 2a 20 41 20 73 75 62 74 65 72 6d 20 69 73 20 61  * A subterm is a
2ed4a 6c 73 6f 20 69 6e 64 65 78 61 62 6c 65 20 69 66  lso indexable if
2ed4b 20 69 74 20 69 73 20 61 6e 20 41 4e 44 20 6f 66   it is an AND of
2ed4c 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20   two or more.** 
2ed4d 73 75 62 73 75 62 74 65 72 6d 73 20 61 74 20 6c  subsubterms at l
2ed4e 65 61 73 74 20 6f 6e 65 20 6f 66 20 77 68 69 63  east one of whic
2ed4f 68 20 69 73 20 69 6e 64 65 78 61 62 6c 65 2e 20  h is indexable. 
2ed50 20 49 6e 64 65 78 61 62 6c 65 20 41 4e 44 20 0a   Indexable AND .
2ed51 2a 2a 20 73 75 62 74 65 72 6d 73 20 68 61 76 65  ** subterms have
2ed52 20 74 68 65 69 72 20 65 4f 70 65 72 61 74 6f 72   their eOperator
2ed53 20 73 65 74 20 74 6f 20 57 4f 5f 41 4e 44 20 61   set to WO_AND a
2ed54 6e 64 20 74 68 65 79 20 68 61 76 65 0a 2a 2a 20  nd they have.** 
2ed55 75 2e 70 41 6e 64 49 6e 66 6f 20 73 65 74 20 74  u.pAndInfo set t
2ed56 6f 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20  o a dynamically 
2ed57 61 6c 6c 6f 63 61 74 65 64 20 57 68 65 72 65 41  allocated WhereA
2ed58 6e 64 54 65 72 6d 20 6f 62 6a 65 63 74 2e 0a 2a  ndTerm object..*
2ed59 2a 0a 2a 2a 20 46 72 6f 6d 20 61 6e 6f 74 68 65  *.** From anothe
2ed5a 72 20 70 6f 69 6e 74 20 6f 66 20 76 69 65 77 2c  r point of view,
2ed5b 20 22 69 6e 64 65 78 61 62 6c 65 22 20 6d 65 61   "indexable" mea
2ed5c 6e 73 20 74 68 61 74 20 74 68 65 20 73 75 62 74  ns that the subt
2ed5d 65 72 6d 20 63 6f 75 6c 64 0a 2a 2a 20 70 6f 74  erm could.** pot
2ed5e 65 6e 74 69 61 6c 6c 79 20 62 65 20 75 73 65 64  entially be used
2ed5f 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 20 69   with an index i
2ed60 66 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  f an appropriate
2ed61 20 69 6e 64 65 78 20 65 78 69 73 74 73 2e 0a 2a   index exists..*
2ed62 2a 20 54 68 69 73 20 61 6e 61 6c 79 73 69 73 20  * This analysis 
2ed63 64 6f 65 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65  does not conside
2ed64 72 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  r whether or not
2ed65 20 74 68 65 20 69 6e 64 65 78 20 65 78 69 73 74   the index exist
2ed66 73 3b 20 74 68 61 74 0a 2a 2a 20 69 73 20 73 6f  s; that.** is so
2ed67 6d 65 74 68 69 6e 67 20 74 68 65 20 62 65 73 74  mething the best
2ed68 49 6e 64 65 78 28 29 20 72 6f 75 74 69 6e 65 20  Index() routine 
2ed69 77 69 6c 6c 20 64 65 74 65 72 6d 69 6e 65 2e 20  will determine. 
2ed6a 20 54 68 69 73 20 61 6e 61 6c 79 73 69 73 0a 2a   This analysis.*
2ed6b 2a 20 6f 6e 6c 79 20 6c 6f 6f 6b 73 20 61 74 20  * only looks at 
2ed6c 77 68 65 74 68 65 72 20 73 75 62 74 65 72 6d 73  whether subterms
2ed6d 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
2ed6e 20 69 6e 64 65 78 69 6e 67 20 65 78 69 73 74 2e   indexing exist.
2ed6f 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 65 78 61 6d 70  .**.** All examp
2ed70 6c 65 73 20 41 20 74 68 72 6f 75 67 68 20 45 20  les A through E 
2ed71 61 62 6f 76 65 20 61 6c 6c 20 73 61 74 69 73 66  above all satisf
2ed72 79 20 63 61 73 65 20 32 2e 20 20 42 75 74 20 69  y case 2.  But i
2ed73 66 20 61 20 74 65 72 6d 0a 2a 2a 20 61 6c 73 6f  f a term.** also
2ed74 20 73 74 61 74 69 73 66 69 65 73 20 63 61 73 65   statisfies case
2ed75 20 31 20 28 73 75 63 68 20 61 73 20 42 29 20 77   1 (such as B) w
2ed76 65 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 20  e know that the 
2ed77 6f 70 74 69 6d 69 7a 65 72 20 77 69 6c 6c 0a 2a  optimizer will.*
2ed78 2a 20 61 6c 77 61 79 73 20 70 72 65 66 65 72 20  * always prefer 
2ed79 63 61 73 65 20 31 2c 20 73 6f 20 69 6e 20 74 68  case 1, so in th
2ed7a 61 74 20 63 61 73 65 20 77 65 20 70 72 65 74 65  at case we prete
2ed7b 6e 64 20 74 68 61 74 20 63 61 73 65 20 32 20 69  nd that case 2 i
2ed7c 73 20 6e 6f 74 0a 2a 2a 20 73 61 74 69 73 66 69  s not.** satisfi
2ed7d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 69 67  ed..**.** It mig
2ed7e 68 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74  ht be the case t
2ed7f 68 61 74 20 6d 75 6c 74 69 70 6c 65 20 74 61 62  hat multiple tab
2ed80 6c 65 73 20 61 72 65 20 69 6e 64 65 78 61 62 6c  les are indexabl
2ed81 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  e.  For example,
2ed82 0a 2a 2a 20 28 45 29 20 61 62 6f 76 65 20 69 73  .** (E) above is
2ed83 20 69 6e 64 65 78 61 62 6c 65 20 6f 6e 20 74 61   indexable on ta
2ed84 62 6c 65 73 20 50 2c 20 51 2c 20 61 6e 64 20 52  bles P, Q, and R
2ed85 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73 20 74 68  ..**.** Terms th
2ed86 61 74 20 73 61 74 69 73 66 79 20 63 61 73 65 20  at satisfy case 
2ed87 32 20 61 72 65 20 63 61 6e 64 69 64 61 74 65 73  2 are candidates
2ed88 20 66 6f 72 20 6c 6f 6f 6b 75 70 20 62 79 20 75   for lookup by u
2ed89 73 69 6e 67 0a 2a 2a 20 73 65 70 61 72 61 74 65  sing.** separate
2ed8a 20 69 6e 64 69 63 65 73 20 74 6f 20 66 69 6e 64   indices to find
2ed8b 20 72 6f 77 69 64 73 20 66 6f 72 20 65 61 63 68   rowids for each
2ed8c 20 73 75 62 74 65 72 6d 20 61 6e 64 20 63 6f 6d   subterm and com
2ed8d 70 6f 73 69 6e 67 0a 2a 2a 20 74 68 65 20 75 6e  posing.** the un
2ed8e 69 6f 6e 20 6f 66 20 61 6c 6c 20 72 6f 77 69 64  ion of all rowid
2ed8f 73 20 75 73 69 6e 67 20 61 20 52 6f 77 53 65 74  s using a RowSet
2ed90 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 69   object.  This i
2ed91 73 20 73 69 6d 69 6c 61 72 0a 2a 2a 20 74 6f 20  s similar.** to 
2ed92 22 62 69 74 6d 61 70 20 69 6e 64 69 63 65 73 22  "bitmap indices"
2ed93 20 69 6e 20 6f 74 68 65 72 20 64 61 74 61 62 61   in other databa
2ed94 73 65 20 65 6e 67 69 6e 65 73 2e 0a 2a 2a 0a 2a  se engines..**.*
2ed95 2a 20 4f 54 48 45 52 57 49 53 45 3a 0a 2a 2a 0a  * OTHERWISE:.**.
2ed96 2a 2a 20 49 66 20 6e 65 69 74 68 65 72 20 63 61  ** If neither ca
2ed97 73 65 20 31 20 6e 6f 72 20 63 61 73 65 20 32 20  se 1 nor case 2 
2ed98 61 70 70 6c 79 2c 20 74 68 65 6e 20 6c 65 61 76  apply, then leav
2ed99 65 20 74 68 65 20 65 4f 70 65 72 61 74 6f 72 20  e the eOperator 
2ed9a 73 65 74 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e 20  set to.** zero. 
2ed9b 20 54 68 69 73 20 74 65 72 6d 20 69 73 20 6e 6f   This term is no
2ed9c 74 20 75 73 65 66 75 6c 20 66 6f 72 20 73 65 61  t useful for sea
2ed9d 72 63 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  rch..*/.static v
2ed9e 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65 4f  oid exprAnalyzeO
2ed9f 72 54 65 72 6d 28 0a 20 20 53 72 63 4c 69 73 74  rTerm(.  SrcList
2eda0 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 20   *pSrc,         
2eda1 20 20 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63     /* the FROM c
2eda2 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65  lause */.  Where
2eda3 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20  Clause *pWC,    
2eda4 20 20 20 20 20 2f 2a 20 74 68 65 20 63 6f 6d 70       /* the comp
2eda5 6c 65 74 65 20 57 48 45 52 45 20 63 6c 61 75 73  lete WHERE claus
2eda6 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 54 65  e */.  int idxTe
2eda7 72 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rm              
2eda8 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65   /* Index of the
2eda9 20 4f 52 2d 74 65 72 6d 20 74 6f 20 62 65 20 61   OR-term to be a
2edaa 6e 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20  nalyzed */.){.  
2edab 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20  Parse *pParse = 
2edac 70 57 43 2d 3e 70 50 61 72 73 65 3b 20 20 20 20  pWC->pParse;    
2edad 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
2edae 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 73  r context */.  s
2edaf 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2edb0 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20  rse->db;        
2edb1 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2edb2 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2edb3 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
2edb4 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64  erm = &pWC->a[id
2edb5 78 54 65 72 6d 5d 3b 20 20 20 20 2f 2a 20 54 68  xTerm];    /* Th
2edb6 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61  e term to be ana
2edb7 6c 79 7a 65 64 20 2a 2f 0a 20 20 45 78 70 72 20  lyzed */.  Expr 
2edb8 2a 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e  *pExpr = pTerm->
2edb9 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20 20  pExpr;          
2edba 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
2edbb 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 65 72 6d  sion of the term
2edbc 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53   */.  WhereMaskS
2edbd 65 74 20 2a 70 4d 61 73 6b 53 65 74 20 3d 20 70  et *pMaskSet = p
2edbe 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b 20 2f 2a  WC->pMaskSet; /*
2edbf 20 54 61 62 6c 65 20 75 73 65 20 6d 61 73 6b 73   Table use masks
2edc0 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
2edc1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2edc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2edc3 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a   Loop counters *
2edc4 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
2edc5 2a 70 4f 72 57 63 3b 20 20 20 20 20 20 20 2f 2a  *pOrWc;       /*
2edc6 20 42 72 65 61 6b 75 70 20 6f 66 20 70 54 65 72   Breakup of pTer
2edc7 6d 20 69 6e 74 6f 20 73 75 62 74 65 72 6d 73 20  m into subterms 
2edc8 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  */.  WhereTerm *
2edc9 70 4f 72 54 65 72 6d 3b 20 20 20 20 20 20 20 2f  pOrTerm;       /
2edca 2a 20 41 20 53 75 62 2d 74 65 72 6d 20 77 69 74  * A Sub-term wit
2edcb 68 69 6e 20 74 68 65 20 70 4f 72 57 63 20 2a 2f  hin the pOrWc */
2edcc 0a 20 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 2a  .  WhereOrInfo *
2edcd 70 4f 72 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20  pOrInfo;     /* 
2edce 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  Additional infor
2edcf 6d 61 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65  mation associate
2edd0 64 20 77 69 74 68 20 70 54 65 72 6d 20 2a 2f 0a  d with pTerm */.
2edd1 20 20 42 69 74 6d 61 73 6b 20 63 68 6e 67 54 6f    Bitmask chngTo
2edd2 49 4e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  IN;         /* T
2edd3 61 62 6c 65 73 20 74 68 61 74 20 6d 69 67 68 74  ables that might
2edd4 20 73 61 74 69 73 66 79 20 63 61 73 65 20 31 20   satisfy case 1 
2edd5 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 69 6e 64  */.  Bitmask ind
2edd6 65 78 61 62 6c 65 3b 20 20 20 20 20 20 20 20 2f  exable;        /
2edd7 2a 20 54 61 62 6c 65 73 20 74 68 61 74 20 61 72  * Tables that ar
2edd8 65 20 69 6e 64 65 78 61 62 6c 65 2c 20 73 61 74  e indexable, sat
2edd9 69 73 66 79 69 6e 67 20 63 61 73 65 20 32 20 2a  isfying case 2 *
2edda 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 42 72 65  /..  /*.  ** Bre
2eddb 61 6b 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65  ak the OR clause
2eddc 20 69 6e 74 6f 20 69 74 73 20 73 65 70 61 72 61   into its separa
2eddd 74 65 20 73 75 62 74 65 72 6d 73 2e 20 20 54 68  te subterms.  Th
2edde 65 20 73 75 62 74 65 72 6d 73 20 61 72 65 0a 20  e subterms are. 
2eddf 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 61 20   ** stored in a 
2ede0 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75  WhereClause stru
2ede1 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67  cture containing
2ede2 20 77 69 74 68 69 6e 20 74 68 65 20 57 68 65 72   within the Wher
2ede3 65 4f 72 49 6e 66 6f 0a 20 20 2a 2a 20 6f 62 6a  eOrInfo.  ** obj
2ede4 65 63 74 20 74 68 61 74 20 69 73 20 61 74 74 61  ect that is atta
2ede5 63 68 65 64 20 74 6f 20 74 68 65 20 6f 72 69 67  ched to the orig
2ede6 69 6e 61 6c 20 4f 52 20 63 6c 61 75 73 65 20 74  inal OR clause t
2ede7 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  erm..  */.  asse
2ede8 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46 6c  rt( (pTerm->wtFl
2ede9 61 67 73 20 26 20 28 54 45 52 4d 5f 44 59 4e 41  ags & (TERM_DYNA
2edea 4d 49 43 7c 54 45 52 4d 5f 4f 52 49 4e 46 4f 7c  MIC|TERM_ORINFO|
2edeb 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 29 29 3d 3d  TERM_ANDINFO))==
2edec 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
2eded 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52 20  Expr->op==TK_OR 
2edee 29 3b 0a 20 20 70 54 65 72 6d 2d 3e 75 2e 70 4f  );.  pTerm->u.pO
2edef 72 49 6e 66 6f 20 3d 20 70 4f 72 49 6e 66 6f 20  rInfo = pOrInfo 
2edf0 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2edf1 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2edf2 28 2a 70 4f 72 49 6e 66 6f 29 29 3b 0a 20 20 69  (*pOrInfo));.  i
2edf3 66 28 20 70 4f 72 49 6e 66 6f 3d 3d 30 20 29 20  f( pOrInfo==0 ) 
2edf4 72 65 74 75 72 6e 3b 0a 20 20 70 54 65 72 6d 2d  return;.  pTerm-
2edf5 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d  >wtFlags |= TERM
2edf6 5f 4f 52 49 4e 46 4f 3b 0a 20 20 70 4f 72 57 63  _ORINFO;.  pOrWc
2edf7 20 3d 20 26 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b   = &pOrInfo->wc;
2edf8 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e  .  whereClauseIn
2edf9 69 74 28 70 4f 72 57 63 2c 20 70 57 43 2d 3e 70  it(pOrWc, pWC->p
2edfa 50 61 72 73 65 2c 20 70 4d 61 73 6b 53 65 74 29  Parse, pMaskSet)
2edfb 3b 0a 20 20 77 68 65 72 65 53 70 6c 69 74 28 70  ;.  whereSplit(p
2edfc 4f 72 57 63 2c 20 70 45 78 70 72 2c 20 54 4b 5f  OrWc, pExpr, TK_
2edfd 4f 52 29 3b 0a 20 20 65 78 70 72 41 6e 61 6c 79  OR);.  exprAnaly
2edfe 7a 65 41 6c 6c 28 70 53 72 63 2c 20 70 4f 72 57  zeAll(pSrc, pOrW
2edff 63 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  c);.  if( db->ma
2ee00 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
2ee01 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70  urn;.  assert( p
2ee02 4f 72 57 63 2d 3e 6e 54 65 72 6d 3e 3d 32 20 29  OrWc->nTerm>=2 )
2ee03 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6f 6d  ;..  /*.  ** Com
2ee04 70 75 74 65 20 74 68 65 20 73 65 74 20 6f 66 20  pute the set of 
2ee05 74 61 62 6c 65 73 20 74 68 61 74 20 6d 69 67 68  tables that migh
2ee06 74 20 73 61 74 69 73 66 79 20 63 61 73 65 73 20  t satisfy cases 
2ee07 31 20 6f 72 20 32 2e 0a 20 20 2a 2f 0a 20 20 69  1 or 2..  */.  i
2ee08 6e 64 65 78 61 62 6c 65 20 3d 20 7e 28 42 69 74  ndexable = ~(Bit
2ee09 6d 61 73 6b 29 30 3b 0a 20 20 63 68 6e 67 54 6f  mask)0;.  chngTo
2ee0a 49 4e 20 3d 20 7e 28 70 57 43 2d 3e 76 6d 61 73  IN = ~(pWC->vmas
2ee0b 6b 29 3b 0a 20 20 66 6f 72 28 69 3d 70 4f 72 57  k);.  for(i=pOrW
2ee0c 63 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70 4f 72 54  c->nTerm-1, pOrT
2ee0d 65 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b 20 69 3e  erm=pOrWc->a; i>
2ee0e 3d 30 20 26 26 20 69 6e 64 65 78 61 62 6c 65 3b  =0 && indexable;
2ee0f 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29   i--, pOrTerm++)
2ee10 7b 0a 20 20 20 20 69 66 28 20 28 70 4f 72 54 65  {.    if( (pOrTe
2ee11 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2ee12 57 4f 5f 53 49 4e 47 4c 45 29 3d 3d 30 20 29 7b  WO_SINGLE)==0 ){
2ee13 0a 20 20 20 20 20 20 57 68 65 72 65 41 6e 64 49  .      WhereAndI
2ee14 6e 66 6f 20 2a 70 41 6e 64 49 6e 66 6f 3b 0a 20  nfo *pAndInfo;. 
2ee15 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72       assert( pOr
2ee16 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
2ee17 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  =0 );.      asse
2ee18 72 74 28 20 28 70 4f 72 54 65 72 6d 2d 3e 77 74  rt( (pOrTerm->wt
2ee19 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 41 4e  Flags & (TERM_AN
2ee1a 44 49 4e 46 4f 7c 54 45 52 4d 5f 4f 52 49 4e 46  DINFO|TERM_ORINF
2ee1b 4f 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  O))==0 );.      
2ee1c 63 68 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20  chngToIN = 0;.  
2ee1d 20 20 20 20 70 41 6e 64 49 6e 66 6f 20 3d 20 73      pAndInfo = s
2ee1e 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
2ee1f 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 41  w(db, sizeof(*pA
2ee20 6e 64 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20  ndInfo));.      
2ee21 69 66 28 20 70 41 6e 64 49 6e 66 6f 20 29 7b 0a  if( pAndInfo ){.
2ee22 20 20 20 20 20 20 20 20 57 68 65 72 65 43 6c 61          WhereCla
2ee23 75 73 65 20 2a 70 41 6e 64 57 43 3b 0a 20 20 20  use *pAndWC;.   
2ee24 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a       WhereTerm *
2ee25 70 41 6e 64 54 65 72 6d 3b 0a 20 20 20 20 20 20  pAndTerm;.      
2ee26 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20    int j;.       
2ee27 20 42 69 74 6d 61 73 6b 20 62 20 3d 20 30 3b 0a   Bitmask b = 0;.
2ee28 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d          pOrTerm-
2ee29 3e 75 2e 70 41 6e 64 49 6e 66 6f 20 3d 20 70 41  >u.pAndInfo = pA
2ee2a 6e 64 49 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20  ndInfo;.        
2ee2b 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  pOrTerm->wtFlags
2ee2c 20 7c 3d 20 54 45 52 4d 5f 41 4e 44 49 4e 46 4f   |= TERM_ANDINFO
2ee2d 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54 65 72  ;.        pOrTer
2ee2e 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 57  m->eOperator = W
2ee2f 4f 5f 41 4e 44 3b 0a 20 20 20 20 20 20 20 20 70  O_AND;.        p
2ee30 41 6e 64 57 43 20 3d 20 26 70 41 6e 64 49 6e 66  AndWC = &pAndInf
2ee31 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 20 20 77  o->wc;.        w
2ee32 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28 70  hereClauseInit(p
2ee33 41 6e 64 57 43 2c 20 70 57 43 2d 3e 70 50 61 72  AndWC, pWC->pPar
2ee34 73 65 2c 20 70 4d 61 73 6b 53 65 74 29 3b 0a 20  se, pMaskSet);. 
2ee35 20 20 20 20 20 20 20 77 68 65 72 65 53 70 6c 69         whereSpli
2ee36 74 28 70 41 6e 64 57 43 2c 20 70 4f 72 54 65 72  t(pAndWC, pOrTer
2ee37 6d 2d 3e 70 45 78 70 72 2c 20 54 4b 5f 41 4e 44  m->pExpr, TK_AND
2ee38 29 3b 0a 20 20 20 20 20 20 20 20 65 78 70 72 41  );.        exprA
2ee39 6e 61 6c 79 7a 65 41 6c 6c 28 70 53 72 63 2c 20  nalyzeAll(pSrc, 
2ee3a 70 41 6e 64 57 43 29 3b 0a 20 20 20 20 20 20 20  pAndWC);.       
2ee3b 20 74 65 73 74 63 61 73 65 28 20 64 62 2d 3e 6d   testcase( db->m
2ee3c 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
2ee3d 20 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e         if( !db->
2ee3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2ee3f 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d            for(j=
2ee40 30 2c 20 70 41 6e 64 54 65 72 6d 3d 70 41 6e 64  0, pAndTerm=pAnd
2ee41 57 43 2d 3e 61 3b 20 6a 3c 70 41 6e 64 57 43 2d  WC->a; j<pAndWC-
2ee42 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70 41 6e  >nTerm; j++, pAn
2ee43 64 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  dTerm++){.      
2ee44 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 41        assert( pA
2ee45 6e 64 54 65 72 6d 2d 3e 70 45 78 70 72 20 29 3b  ndTerm->pExpr );
2ee46 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
2ee47 20 61 6c 6c 6f 77 65 64 4f 70 28 70 41 6e 64 54   allowedOp(pAndT
2ee48 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 6f 70 29 20  erm->pExpr->op) 
2ee49 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
2ee4a 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b 28 70 4d   b |= getMask(pM
2ee4b 61 73 6b 53 65 74 2c 20 70 41 6e 64 54 65 72 6d  askSet, pAndTerm
2ee4c 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b 0a 20  ->leftCursor);. 
2ee4d 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2ee4e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2ee4f 20 7d 0a 20 20 20 20 20 20 20 20 69 6e 64 65 78   }.        index
2ee50 61 62 6c 65 20 26 3d 20 62 3b 0a 20 20 20 20 20  able &= b;.     
2ee51 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
2ee52 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67   pOrTerm->wtFlag
2ee53 73 20 26 20 54 45 52 4d 5f 43 4f 50 49 45 44 20  s & TERM_COPIED 
2ee54 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 6b 69 70  ){.      /* Skip
2ee55 20 74 68 69 73 20 74 65 72 6d 20 66 6f 72 20 6e   this term for n
2ee56 6f 77 2e 20 20 57 65 20 72 65 76 69 73 69 74 20  ow.  We revisit 
2ee57 69 74 20 77 68 65 6e 20 77 65 20 70 72 6f 63 65  it when we proce
2ee58 73 73 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  ss the.      ** 
2ee59 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 54 45  corresponding TE
2ee5a 52 4d 5f 56 49 52 54 55 41 4c 20 74 65 72 6d 20  RM_VIRTUAL term 
2ee5b 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  */.    }else{.  
2ee5c 20 20 20 20 42 69 74 6d 61 73 6b 20 62 3b 0a 20      Bitmask b;. 
2ee5d 20 20 20 20 20 62 20 3d 20 67 65 74 4d 61 73 6b       b = getMask
2ee5e 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f 72 54 65  (pMaskSet, pOrTe
2ee5f 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b  rm->leftCursor);
2ee60 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65  .      if( pOrTe
2ee61 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
2ee62 52 4d 5f 56 49 52 54 55 41 4c 20 29 7b 0a 20 20  RM_VIRTUAL ){.  
2ee63 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
2ee64 2a 70 4f 74 68 65 72 20 3d 20 26 70 4f 72 57 63  *pOther = &pOrWc
2ee65 2d 3e 61 5b 70 4f 72 54 65 72 6d 2d 3e 69 50 61  ->a[pOrTerm->iPa
2ee66 72 65 6e 74 5d 3b 0a 20 20 20 20 20 20 20 20 62  rent];.        b
2ee67 20 7c 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73   |= getMask(pMas
2ee68 6b 53 65 74 2c 20 70 4f 74 68 65 72 2d 3e 6c 65  kSet, pOther->le
2ee69 66 74 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  ftCursor);.     
2ee6a 20 7d 0a 20 20 20 20 20 20 69 6e 64 65 78 61 62   }.      indexab
2ee6b 6c 65 20 26 3d 20 62 3b 0a 20 20 20 20 20 20 69  le &= b;.      i
2ee6c 66 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65  f( pOrTerm->eOpe
2ee6d 72 61 74 6f 72 21 3d 57 4f 5f 45 51 20 29 7b 0a  rator!=WO_EQ ){.
2ee6e 20 20 20 20 20 20 20 20 63 68 6e 67 54 6f 49 4e          chngToIN
2ee6f 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73   = 0;.      }els
2ee70 65 7b 0a 20 20 20 20 20 20 20 20 63 68 6e 67 54  e{.        chngT
2ee71 6f 49 4e 20 26 3d 20 62 3b 0a 20 20 20 20 20 20  oIN &= b;.      
2ee72 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
2ee73 2a 0a 20 20 2a 2a 20 52 65 63 6f 72 64 20 74 68  *.  ** Record th
2ee74 65 20 73 65 74 20 6f 66 20 74 61 62 6c 65 73 20  e set of tables 
2ee75 74 68 61 74 20 73 61 74 69 73 66 79 20 63 61 73  that satisfy cas
2ee76 65 20 32 2e 20 20 54 68 65 20 73 65 74 20 6d 69  e 2.  The set mi
2ee77 67 68 74 20 62 65 0a 20 20 2a 2a 20 65 6d 70 74  ght be.  ** empt
2ee78 79 2e 0a 20 20 2a 2f 0a 20 20 70 4f 72 49 6e 66  y..  */.  pOrInf
2ee79 6f 2d 3e 69 6e 64 65 78 61 62 6c 65 20 3d 20 69  o->indexable = i
2ee7a 6e 64 65 78 61 62 6c 65 3b 0a 20 20 70 54 65 72  ndexable;.  pTer
2ee7b 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 69  m->eOperator = i
2ee7c 6e 64 65 78 61 62 6c 65 3d 3d 30 20 3f 20 30 20  ndexable==0 ? 0 
2ee7d 3a 20 57 4f 5f 4f 52 3b 0a 0a 20 20 2f 2a 0a 20  : WO_OR;..  /*. 
2ee7e 20 2a 2a 20 63 68 6e 67 54 6f 49 4e 20 68 6f 6c   ** chngToIN hol
2ee7f 64 73 20 61 20 73 65 74 20 6f 66 20 74 61 62 6c  ds a set of tabl
2ee80 65 73 20 74 68 61 74 20 2a 6d 69 67 68 74 2a 20  es that *might* 
2ee81 73 61 74 69 73 66 79 20 63 61 73 65 20 31 2e 20  satisfy case 1. 
2ee82 20 42 75 74 0a 20 20 2a 2a 20 77 65 20 68 61 76   But.  ** we hav
2ee83 65 20 74 6f 20 64 6f 20 73 6f 6d 65 20 61 64 64  e to do some add
2ee84 69 74 69 6f 6e 61 6c 20 63 68 65 63 6b 69 6e 67  itional checking
2ee85 20 74 6f 20 73 65 65 20 69 66 20 63 61 73 65 20   to see if case 
2ee86 31 20 72 65 61 6c 6c 79 0a 20 20 2a 2a 20 69 73  1 really.  ** is
2ee87 20 73 61 74 69 73 66 69 65 64 2e 0a 20 20 2a 2a   satisfied..  **
2ee88 0a 20 20 2a 2a 20 63 68 6e 67 54 6f 49 4e 20 77  .  ** chngToIN w
2ee89 69 6c 6c 20 68 6f 6c 64 20 65 69 74 68 65 72 20  ill hold either 
2ee8a 30 2c 20 31 2c 20 6f 72 20 32 20 62 69 74 73 2e  0, 1, or 2 bits.
2ee8b 20 20 54 68 65 20 30 2d 62 69 74 20 63 61 73 65    The 0-bit case
2ee8c 20 6d 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 74   means.  ** that
2ee8d 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70 6f 73   there is no pos
2ee8e 73 69 62 69 6c 69 74 79 20 6f 66 20 74 72 61 6e  sibility of tran
2ee8f 73 66 6f 72 6d 69 6e 67 20 74 68 65 20 4f 52 20  sforming the OR 
2ee90 63 6c 61 75 73 65 20 69 6e 74 6f 20 61 6e 0a 20  clause into an. 
2ee91 20 2a 2a 20 49 4e 20 6f 70 65 72 61 74 6f 72 20   ** IN operator 
2ee92 62 65 63 61 75 73 65 20 6f 6e 65 20 6f 72 20 6d  because one or m
2ee93 6f 72 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65  ore terms in the
2ee94 20 4f 52 20 63 6c 61 75 73 65 20 63 6f 6e 74 61   OR clause conta
2ee95 69 6e 0a 20 20 2a 2a 20 73 6f 6d 65 74 68 69 6e  in.  ** somethin
2ee96 67 20 6f 74 68 65 72 20 74 68 61 6e 20 3d 3d 20  g other than == 
2ee97 6f 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74  on a column in t
2ee98 68 65 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e  he single table.
2ee99 20 20 54 68 65 20 31 2d 62 69 74 0a 20 20 2a 2a    The 1-bit.  **
2ee9a 20 63 61 73 65 20 6d 65 61 6e 73 20 74 68 61 74   case means that
2ee9b 20 65 76 65 72 79 20 74 65 72 6d 20 6f 66 20 74   every term of t
2ee9c 68 65 20 4f 52 20 63 6c 61 75 73 65 20 69 73 20  he OR clause is 
2ee9d 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 20 2a 2a  of the form.  **
2ee9e 20 22 74 61 62 6c 65 2e 63 6f 6c 75 6d 6e 3d 65   "table.column=e
2ee9f 78 70 72 22 20 66 6f 72 20 73 6f 6d 65 20 73 69  xpr" for some si
2eea0 6e 67 6c 65 20 74 61 62 6c 65 2e 20 20 54 68 65  ngle table.  The
2eea1 20 6f 6e 65 20 62 69 74 20 74 68 61 74 20 69 73   one bit that is
2eea2 20 73 65 74 0a 20 20 2a 2a 20 77 69 6c 6c 20 63   set.  ** will c
2eea3 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65  orrespond to the
2eea4 20 63 6f 6d 6d 6f 6e 20 74 61 62 6c 65 2e 20 20   common table.  
2eea5 57 65 20 73 74 69 6c 6c 20 6e 65 65 64 20 74 6f  We still need to
2eea6 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65 0a 20   check to make. 
2eea7 20 2a 2a 20 73 75 72 65 20 74 68 65 20 73 61 6d   ** sure the sam
2eea8 65 20 63 6f 6c 75 6d 6e 20 69 73 20 75 73 65 64  e column is used
2eea9 20 6f 6e 20 61 6c 6c 20 74 65 72 6d 73 2e 20 20   on all terms.  
2eeaa 54 68 65 20 32 2d 62 69 74 20 63 61 73 65 20 69  The 2-bit case i
2eeab 73 20 77 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20  s when.  ** the 
2eeac 61 6c 6c 20 74 65 72 6d 73 20 61 72 65 20 6f 66  all terms are of
2eead 20 74 68 65 20 66 6f 72 6d 20 22 74 61 62 6c 65   the form "table
2eeae 31 2e 63 6f 6c 75 6d 6e 3d 74 61 62 6c 65 32 2e  1.column=table2.
2eeaf 63 6f 6c 75 6d 6e 22 2e 20 20 49 74 0a 20 20 2a  column".  It.  *
2eeb0 2a 20 6d 69 67 68 74 20 62 65 20 70 6f 73 73 69  * might be possi
2eeb1 62 6c 65 20 74 6f 20 66 6f 72 6d 20 61 6e 20 49  ble to form an I
2eeb2 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20  N operator with 
2eeb3 65 69 74 68 65 72 20 74 61 62 6c 65 31 2e 63 6f  either table1.co
2eeb4 6c 75 6d 6e 0a 20 20 2a 2a 20 6f 72 20 74 61 62  lumn.  ** or tab
2eeb5 6c 65 32 2e 63 6f 6c 75 6d 6e 20 61 73 20 74 68  le2.column as th
2eeb6 65 20 4c 48 53 20 69 66 20 65 69 74 68 65 72 20  e LHS if either 
2eeb7 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 65 76 65  is common to eve
2eeb8 72 79 20 74 65 72 6d 20 6f 66 0a 20 20 2a 2a 20  ry term of.  ** 
2eeb9 74 68 65 20 4f 52 20 63 6c 61 75 73 65 2e 0a 20  the OR clause.. 
2eeba 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74 68   **.  ** Note th
2eebb 61 74 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20  at terms of the 
2eebc 66 6f 72 6d 20 22 74 61 62 6c 65 2e 63 6f 6c 75  form "table.colu
2eebd 6d 6e 31 3d 74 61 62 6c 65 2e 63 6f 6c 75 6d 6e  mn1=table.column
2eebe 32 22 20 28 74 68 65 0a 20 20 2a 2a 20 73 61 6d  2" (the.  ** sam
2eebf 65 20 74 61 62 6c 65 20 6f 6e 20 62 6f 74 68 20  e table on both 
2eec0 73 69 7a 65 73 20 6f 66 20 74 68 65 20 3d 3d 29  sizes of the ==)
2eec1 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 74 69 6d   cannot be optim
2eec2 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ized..  */.  if(
2eec3 20 63 68 6e 67 54 6f 49 4e 20 29 7b 0a 20 20 20   chngToIN ){.   
2eec4 20 69 6e 74 20 6f 6b 54 6f 43 68 6e 67 54 6f 49   int okToChngToI
2eec5 4e 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 72  N = 0;     /* Tr
2eec6 75 65 20 69 66 20 74 68 65 20 63 6f 6e 76 65 72  ue if the conver
2eec7 73 69 6f 6e 20 74 6f 20 49 4e 20 69 73 20 76 61  sion to IN is va
2eec8 6c 69 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  lid */.    int i
2eec9 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20 20  Column = -1;    
2eeca 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 69       /* Column i
2eecb 6e 64 65 78 20 6f 6e 20 6c 68 73 20 6f 66 20 49  ndex on lhs of I
2eecc 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  N operator */.  
2eecd 20 20 69 6e 74 20 69 43 75 72 73 6f 72 20 3d 20    int iCursor = 
2eece 2d 31 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  -1;         /* T
2eecf 61 62 6c 65 20 63 75 72 73 6f 72 20 63 6f 6d 6d  able cursor comm
2eed0 6f 6e 20 74 6f 20 61 6c 6c 20 74 65 72 6d 73 20  on to all terms 
2eed1 2a 2f 0a 20 20 20 20 69 6e 74 20 6a 20 3d 20 30  */.    int j = 0
2eed2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2eed3 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2eed4 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53 65 61 72   */..    /* Sear
2eed5 63 68 20 66 6f 72 20 61 20 74 61 62 6c 65 20 61  ch for a table a
2eed6 6e 64 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61  nd column that a
2eed7 70 70 65 61 72 73 20 6f 6e 20 6f 6e 65 20 73 69  ppears on one si
2eed8 64 65 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a  de or the.    **
2eed9 20 6f 74 68 65 72 20 6f 66 20 74 68 65 20 3d 3d   other of the ==
2eeda 20 6f 70 65 72 61 74 6f 72 20 69 6e 20 65 76 65   operator in eve
2eedb 72 79 20 73 75 62 74 65 72 6d 2e 20 20 54 68 61  ry subterm.  Tha
2eedc 74 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  t table and colu
2eedd 6d 6e 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62  mn.    ** will b
2eede 65 20 72 65 63 6f 72 64 65 64 20 69 6e 20 69 43  e recorded in iC
2eedf 75 72 73 6f 72 20 61 6e 64 20 69 43 6f 6c 75 6d  ursor and iColum
2eee0 6e 2e 20 20 54 68 65 72 65 20 6d 69 67 68 74 20  n.  There might 
2eee1 6e 6f 74 20 62 65 20 61 6e 79 0a 20 20 20 20 2a  not be any.    *
2eee2 2a 20 73 75 63 68 20 74 61 62 6c 65 20 61 6e 64  * such table and
2eee3 20 63 6f 6c 75 6d 6e 2e 20 20 53 65 74 20 6f 6b   column.  Set ok
2eee4 54 6f 43 68 6e 67 54 6f 49 4e 20 69 66 20 61 6e  ToChngToIN if an
2eee5 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 61 62   appropriate tab
2eee6 6c 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 63 6f  le.    ** and co
2eee7 6c 75 6d 6e 20 69 73 20 66 6f 75 6e 64 20 62 75  lumn is found bu
2eee8 74 20 6c 65 61 76 65 20 6f 6b 54 6f 43 68 6e 67  t leave okToChng
2eee9 54 6f 49 4e 20 66 61 6c 73 65 20 69 66 20 6e 6f  ToIN false if no
2eeea 74 20 66 6f 75 6e 64 2e 0a 20 20 20 20 2a 2f 0a  t found..    */.
2eeeb 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 32      for(j=0; j<2
2eeec 20 26 26 20 21 6f 6b 54 6f 43 68 6e 67 54 6f 49   && !okToChngToI
2eeed 4e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 70  N; j++){.      p
2eeee 4f 72 54 65 72 6d 20 3d 20 70 4f 72 57 63 2d 3e  OrTerm = pOrWc->
2eeef 61 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 70  a;.      for(i=p
2eef0 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 3b 20 69  OrWc->nTerm-1; i
2eef1 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72  >=0; i--, pOrTer
2eef2 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73  m++){.        as
2eef3 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 65  sert( pOrTerm->e
2eef4 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 45 51 20  Operator==WO_EQ 
2eef5 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 72 54 65  );.        pOrTe
2eef6 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 3d 20 7e  rm->wtFlags &= ~
2eef7 54 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20  TERM_OR_OK;.    
2eef8 20 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d      if( pOrTerm-
2eef9 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75  >leftCursor==iCu
2eefa 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20  rsor ){.        
2eefb 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
2eefc 20 32 2d 62 69 74 20 63 61 73 65 20 61 6e 64 20   2-bit case and 
2eefd 77 65 20 61 72 65 20 6f 6e 20 74 68 65 20 73 65  we are on the se
2eefe 63 6f 6e 64 20 69 74 65 72 61 74 69 6f 6e 20 61  cond iteration a
2eeff 6e 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  nd.          ** 
2ef00 63 75 72 72 65 6e 74 20 74 65 72 6d 20 69 73 20  current term is 
2ef01 66 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 69  from the first i
2ef02 74 65 72 61 74 69 6f 6e 2e 20 20 53 6f 20 73 6b  teration.  So sk
2ef03 69 70 20 74 68 69 73 20 74 65 72 6d 2e 20 2a 2f  ip this term. */
2ef04 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
2ef05 74 28 20 6a 3d 3d 31 20 29 3b 0a 20 20 20 20 20  t( j==1 );.     
2ef06 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
2ef07 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2ef08 20 69 66 28 20 28 63 68 6e 67 54 6f 49 4e 20 26   if( (chngToIN &
2ef09 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65   getMask(pMaskSe
2ef0a 74 2c 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74  t, pOrTerm->left
2ef0b 43 75 72 73 6f 72 29 29 3d 3d 30 20 29 7b 0a 20  Cursor))==0 ){. 
2ef0c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
2ef0d 20 74 65 72 6d 20 6d 75 73 74 20 62 65 20 6f 66   term must be of
2ef0e 20 74 68 65 20 66 6f 72 6d 20 74 31 2e 61 3d 3d   the form t1.a==
2ef0f 74 32 2e 62 20 77 68 65 72 65 20 74 32 20 69 73  t2.b where t2 is
2ef10 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20   in the.        
2ef11 20 20 2a 2a 20 63 68 6e 67 54 6f 49 4e 20 73 65    ** chngToIN se
2ef12 74 20 62 75 74 20 74 31 20 69 73 20 6e 6f 74 2e  t but t1 is not.
2ef13 20 20 54 68 69 73 20 74 65 72 6d 20 77 69 6c 6c    This term will
2ef14 20 62 65 20 65 69 74 68 65 72 20 70 72 65 63 65   be either prece
2ef15 65 64 65 64 0a 20 20 20 20 20 20 20 20 20 20 2a  eded.          *
2ef16 2a 20 6f 72 20 66 6f 6c 6c 77 65 64 20 62 79 20  * or follwed by 
2ef17 61 6e 20 69 6e 76 65 72 74 65 64 20 63 6f 70 79  an inverted copy
2ef18 20 28 74 32 2e 62 3d 3d 74 31 2e 61 29 2e 20 20   (t2.b==t1.a).  
2ef19 53 6b 69 70 20 74 68 69 73 20 74 65 72 6d 20 0a  Skip this term .
2ef1a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64            ** and
2ef1b 20 75 73 65 20 69 74 73 20 69 6e 76 65 72 73 69   use its inversi
2ef1c 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  on. */.         
2ef1d 20 74 65 73 74 63 61 73 65 28 20 70 4f 72 54 65   testcase( pOrTe
2ef1e 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
2ef1f 52 4d 5f 43 4f 50 49 45 44 20 29 3b 0a 20 20 20  RM_COPIED );.   
2ef20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
2ef21 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67   pOrTerm->wtFlag
2ef22 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  s & TERM_VIRTUAL
2ef23 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   );.          as
2ef24 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e 77  sert( pOrTerm->w
2ef25 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 43  tFlags & (TERM_C
2ef26 4f 50 49 45 44 7c 54 45 52 4d 5f 56 49 52 54 55  OPIED|TERM_VIRTU
2ef27 41 4c 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20  AL) );.         
2ef28 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2ef29 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 43 6f     }.        iCo
2ef2a 6c 75 6d 6e 20 3d 20 70 4f 72 54 65 72 6d 2d 3e  lumn = pOrTerm->
2ef2b 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20  u.leftColumn;.  
2ef2c 20 20 20 20 20 20 69 43 75 72 73 6f 72 20 3d 20        iCursor = 
2ef2d 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  pOrTerm->leftCur
2ef2e 73 6f 72 3b 0a 20 20 20 20 20 20 20 20 62 72 65  sor;.        bre
2ef2f 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2ef30 20 20 69 66 28 20 69 3c 30 20 29 7b 0a 20 20 20    if( i<0 ){.   
2ef31 20 20 20 20 20 2f 2a 20 4e 6f 20 63 61 6e 64 69       /* No candi
2ef32 64 61 74 65 20 74 61 62 6c 65 2b 63 6f 6c 75 6d  date table+colum
2ef33 6e 20 77 61 73 20 66 6f 75 6e 64 2e 20 20 54 68  n was found.  Th
2ef34 69 73 20 63 61 6e 20 6f 6e 6c 79 20 6f 63 63 75  is can only occu
2ef35 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20  r.        ** on 
2ef36 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65 72 61  the second itera
2ef37 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tion */.        
2ef38 61 73 73 65 72 74 28 20 6a 3d 3d 31 20 29 3b 0a  assert( j==1 );.
2ef39 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2ef3a 28 63 68 6e 67 54 6f 49 4e 26 28 63 68 6e 67 54  (chngToIN&(chngT
2ef3b 6f 49 4e 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  oIN-1))==0 );.  
2ef3c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63 68        assert( ch
2ef3d 6e 67 54 6f 49 4e 3d 3d 67 65 74 4d 61 73 6b 28  ngToIN==getMask(
2ef3e 70 4d 61 73 6b 53 65 74 2c 20 69 43 75 72 73 6f  pMaskSet, iCurso
2ef3f 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 62 72  r) );.        br
2ef40 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2ef41 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 3d 3d     testcase( j==
2ef42 31 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 57  1 );..      /* W
2ef43 65 20 68 61 76 65 20 66 6f 75 6e 64 20 61 20 63  e have found a c
2ef44 61 6e 64 69 64 61 74 65 20 74 61 62 6c 65 20 61  andidate table a
2ef45 6e 64 20 63 6f 6c 75 6d 6e 2e 20 20 43 68 65 63  nd column.  Chec
2ef46 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 61 74  k to see if that
2ef47 0a 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 20  .      ** table 
2ef48 61 6e 64 20 63 6f 6c 75 6d 6e 20 69 73 20 63 6f  and column is co
2ef49 6d 6d 6f 6e 20 74 6f 20 65 76 65 72 79 20 74 65  mmon to every te
2ef4a 72 6d 20 69 6e 20 74 68 65 20 4f 52 20 63 6c 61  rm in the OR cla
2ef4b 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 6f 6b 54  use */.      okT
2ef4c 6f 43 68 6e 67 54 6f 49 4e 20 3d 20 31 3b 0a 20  oChngToIN = 1;. 
2ef4d 20 20 20 20 20 66 6f 72 28 3b 20 69 3e 3d 30 20       for(; i>=0 
2ef4e 26 26 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 3b  && okToChngToIN;
2ef4f 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29   i--, pOrTerm++)
2ef50 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
2ef51 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72  ( pOrTerm->eOper
2ef52 61 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20  ator==WO_EQ );. 
2ef53 20 20 20 20 20 20 20 69 66 28 20 70 4f 72 54 65         if( pOrTe
2ef54 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 21 3d  rm->leftCursor!=
2ef55 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20  iCursor ){.     
2ef56 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74       pOrTerm->wt
2ef57 46 6c 61 67 73 20 26 3d 20 7e 54 45 52 4d 5f 4f  Flags &= ~TERM_O
2ef58 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 65  R_OK;.        }e
2ef59 6c 73 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d  lse if( pOrTerm-
2ef5a 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 21 3d 69  >u.leftColumn!=i
2ef5b 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20  Column ){.      
2ef5c 20 20 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e      okToChngToIN
2ef5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65   = 0;.        }e
2ef5e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69  lse{.          i
2ef5f 6e 74 20 61 66 66 4c 65 66 74 2c 20 61 66 66 52  nt affLeft, affR
2ef60 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20  ight;.          
2ef61 2f 2a 20 49 66 20 74 68 65 20 72 69 67 68 74 2d  /* If the right-
2ef62 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 6c 73  hand side is als
2ef63 6f 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e  o a column, then
2ef64 20 74 68 65 20 61 66 66 69 6e 69 74 69 65 73 0a   the affinities.
2ef65 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20            ** of 
2ef66 62 6f 74 68 20 72 69 67 68 74 20 61 6e 64 20 6c  both right and l
2ef67 65 66 74 20 73 69 64 65 73 20 6d 75 73 74 20 62  eft sides must b
2ef68 65 20 73 75 63 68 20 74 68 61 74 20 6e 6f 20 74  e such that no t
2ef69 79 70 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ype.          **
2ef6a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 72 65   conversions are
2ef6b 20 72 65 71 75 69 72 65 64 20 6f 6e 20 74 68 65   required on the
2ef6c 20 72 69 67 68 74 2e 20 20 28 54 69 63 6b 65 74   right.  (Ticket
2ef6d 20 23 32 32 34 39 29 0a 20 20 20 20 20 20 20 20   #2249).        
2ef6e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 61    */.          a
2ef6f 66 66 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65  ffRight = sqlite
2ef70 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 4f  3ExprAffinity(pO
2ef71 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52  rTerm->pExpr->pR
2ef72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20 20  ight);.         
2ef73 20 61 66 66 4c 65 66 74 20 3d 20 73 71 6c 69 74   affLeft = sqlit
2ef74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70  e3ExprAffinity(p
2ef75 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70  OrTerm->pExpr->p
2ef76 4c 65 66 74 29 3b 0a 20 20 20 20 20 20 20 20 20  Left);.         
2ef77 20 69 66 28 20 61 66 66 52 69 67 68 74 21 3d 30   if( affRight!=0
2ef78 20 26 26 20 61 66 66 52 69 67 68 74 21 3d 61 66   && affRight!=af
2ef79 66 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  fLeft ){.       
2ef7a 20 20 20 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49       okToChngToI
2ef7b 4e 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  N = 0;.         
2ef7c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2ef7d 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74 46      pOrTerm->wtF
2ef7e 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 4f 52 5f  lags |= TERM_OR_
2ef7f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  OK;.          }.
2ef80 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2ef81 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
2ef82 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 6f  At this point, o
2ef83 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 69 73 20 74  kToChngToIN is t
2ef84 72 75 65 20 69 66 20 6f 72 69 67 69 6e 61 6c 20  rue if original 
2ef85 70 54 65 72 6d 20 73 61 74 69 73 66 69 65 73 0a  pTerm satisfies.
2ef86 20 20 20 20 2a 2a 20 63 61 73 65 20 31 2e 20 20      ** case 1.  
2ef87 49 6e 20 74 68 61 74 20 63 61 73 65 2c 20 63 6f  In that case, co
2ef88 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 76 69  nstruct a new vi
2ef89 72 74 75 61 6c 20 74 65 72 6d 20 74 68 61 74 20  rtual term that 
2ef8a 69 73 20 0a 20 20 20 20 2a 2a 20 70 54 65 72 6d  is .    ** pTerm
2ef8b 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
2ef8c 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 2e 0a  an IN operator..
2ef8d 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f      */.    if( o
2ef8e 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 29 7b 0a 20  kToChngToIN ){. 
2ef8f 20 20 20 20 20 45 78 70 72 20 2a 70 44 75 70 3b       Expr *pDup;
2ef90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2ef91 20 74 72 61 6e 73 69 65 6e 74 20 64 75 70 6c 69   transient dupli
2ef92 63 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 20  cate expression 
2ef93 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73  */.      ExprLis
2ef94 74 20 2a 70 4c 69 73 74 20 3d 20 30 3b 20 20 20  t *pList = 0;   
2ef95 2f 2a 20 54 68 65 20 52 48 53 20 6f 66 20 74 68  /* The RHS of th
2ef96 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  e IN operator */
2ef97 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65  .      Expr *pLe
2ef98 66 74 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  ft = 0;       /*
2ef99 20 54 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20   The LHS of the 
2ef9a 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  IN operator */. 
2ef9b 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 3b       Expr *pNew;
2ef9c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2ef9d 68 65 20 63 6f 6d 70 6c 65 74 65 20 49 4e 20 6f  he complete IN o
2ef9e 70 65 72 61 74 6f 72 20 2a 2f 0a 0a 20 20 20 20  perator */..    
2ef9f 20 20 66 6f 72 28 69 3d 70 4f 72 57 63 2d 3e 6e    for(i=pOrWc->n
2efa0 54 65 72 6d 2d 31 2c 20 70 4f 72 54 65 72 6d 3d  Term-1, pOrTerm=
2efa1 70 4f 72 57 63 2d 3e 61 3b 20 69 3e 3d 30 3b 20  pOrWc->a; i>=0; 
2efa2 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b 29 7b  i--, pOrTerm++){
2efa3 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70 4f  .        if( (pO
2efa4 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  rTerm->wtFlags &
2efa5 20 54 45 52 4d 5f 4f 52 5f 4f 4b 29 3d 3d 30 20   TERM_OR_OK)==0 
2efa6 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2efa7 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54      assert( pOrT
2efa8 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  erm->eOperator==
2efa9 57 4f 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 20  WO_EQ );.       
2efaa 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d   assert( pOrTerm
2efab 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43  ->leftCursor==iC
2efac 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 20 20 20  ursor );.       
2efad 20 61 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d   assert( pOrTerm
2efae 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d  ->u.leftColumn==
2efaf 69 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 20  iColumn );.     
2efb0 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65     pDup = sqlite
2efb1 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72  3ExprDup(db, pOr
2efb2 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69  Term->pExpr->pRi
2efb3 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ght, 0);.       
2efb4 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33   pList = sqlite3
2efb5 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2efb6 57 43 2d 3e 70 50 61 72 73 65 2c 20 70 4c 69 73  WC->pParse, pLis
2efb7 74 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20 20  t, pDup);.      
2efb8 20 20 70 4c 65 66 74 20 3d 20 70 4f 72 54 65 72    pLeft = pOrTer
2efb9 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b  m->pExpr->pLeft;
2efba 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
2efbb 73 73 65 72 74 28 20 70 4c 65 66 74 21 3d 30 20  ssert( pLeft!=0 
2efbc 29 3b 0a 20 20 20 20 20 20 70 44 75 70 20 3d 20  );.      pDup = 
2efbd 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2efbe 62 2c 20 70 4c 65 66 74 2c 20 30 29 3b 0a 20 20  b, pLeft, 0);.  
2efbf 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
2efc0 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2efc1 54 4b 5f 49 4e 2c 20 70 44 75 70 2c 20 30 2c 20  TK_IN, pDup, 0, 
2efc2 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e  0);.      if( pN
2efc3 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  ew ){.        in
2efc4 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20  t idxNew;.      
2efc5 20 20 74 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61    transferJoinMa
2efc6 72 6b 69 6e 67 73 28 70 4e 65 77 2c 20 70 45 78  rkings(pNew, pEx
2efc7 70 72 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  pr);.        ass
2efc8 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
2efc9 70 65 72 74 79 28 70 4e 65 77 2c 20 45 50 5f 78  perty(pNew, EP_x
2efca 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20  IsSelect) );.   
2efcb 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70 4c 69       pNew->x.pLi
2efcc 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20  st = pList;.    
2efcd 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65      idxNew = whe
2efce 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70  reClauseInsert(p
2efcf 57 43 2c 20 70 4e 65 77 2c 20 54 45 52 4d 5f 56  WC, pNew, TERM_V
2efd0 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41  IRTUAL|TERM_DYNA
2efd1 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 74 65  MIC);.        te
2efd2 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 3d 3d  stcase( idxNew==
2efd3 30 20 29 3b 0a 20 20 20 20 20 20 20 20 65 78 70  0 );.        exp
2efd4 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70  rAnalyze(pSrc, p
2efd5 57 43 2c 20 69 64 78 4e 65 77 29 3b 0a 20 20 20  WC, idxNew);.   
2efd6 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57       pTerm = &pW
2efd7 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
2efd8 20 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64         pWC->a[id
2efd9 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74 20 3d 20  xNew].iParent = 
2efda 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 20  idxTerm;.       
2efdb 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d   pTerm->nChild =
2efdc 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   1;.      }else{
2efdd 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2efde 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
2efdf 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 20  b, pList);.     
2efe0 20 7d 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e   }.      pTerm->
2efe1 65 4f 70 65 72 61 74 6f 72 20 3d 20 30 3b 20 20  eOperator = 0;  
2efe2 2f 2a 20 63 61 73 65 20 31 20 74 72 75 6d 70 73  /* case 1 trumps
2efe3 20 63 61 73 65 20 32 20 2a 2f 0a 20 20 20 20 7d   case 2 */.    }
2efe4 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
2efe5 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52   !SQLITE_OMIT_OR
2efe6 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 26 26  _OPTIMIZATION &&
2efe7 20 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55   !SQLITE_OMIT_SU
2efe8 42 51 55 45 52 59 20 2a 2f 0a 0a 0a 2f 2a 0a 2a  BQUERY */.../*.*
2efe9 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 74  * The input to t
2efea 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
2efeb 6e 20 57 68 65 72 65 54 65 72 6d 20 73 74 72 75  n WhereTerm stru
2efec 63 74 75 72 65 20 77 69 74 68 20 6f 6e 6c 79 20  cture with only 
2efed 74 68 65 0a 2a 2a 20 22 70 45 78 70 72 22 20 66  the.** "pExpr" f
2efee 69 65 6c 64 20 66 69 6c 6c 65 64 20 69 6e 2e 20  ield filled in. 
2efef 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73   The job of this
2eff0 20 72 6f 75 74 69 6e 65 20 69 73 20 74 6f 20 61   routine is to a
2eff1 6e 61 6c 79 7a 65 20 74 68 65 0a 2a 2a 20 73 75  nalyze the.** su
2eff2 62 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20  bexpression and 
2eff3 70 6f 70 75 6c 61 74 65 20 61 6c 6c 20 74 68 65  populate all the
2eff4 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20 6f 66   other fields of
2eff5 20 74 68 65 20 57 68 65 72 65 54 65 72 6d 0a 2a   the WhereTerm.*
2eff6 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  * structure..**.
2eff7 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
2eff8 73 69 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66  sion is of the f
2eff9 6f 72 6d 20 22 3c 65 78 70 72 3e 20 3c 6f 70 3e  orm "<expr> <op>
2effa 20 58 22 20 69 74 20 67 65 74 73 20 63 6f 6d 6d   X" it gets comm
2effb 75 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 73  uted.** to the s
2effc 74 61 6e 64 61 72 64 20 66 6f 72 6d 20 6f 66 20  tandard form of 
2effd 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 2e  "X <op> <expr>".
2effe 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78  .**.** If the ex
2efff 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74  pression is of t
2f000 68 65 20 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20  he form "X <op> 
2f001 59 22 20 77 68 65 72 65 20 62 6f 74 68 20 58 20  Y" where both X 
2f002 61 6e 64 20 59 20 61 72 65 0a 2a 2a 20 63 6f 6c  and Y are.** col
2f003 75 6d 6e 73 2c 20 74 68 65 6e 20 74 68 65 20 6f  umns, then the o
2f004 72 69 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69  riginal expressi
2f005 6f 6e 20 69 73 20 75 6e 63 68 61 6e 67 65 64 20  on is unchanged 
2f006 61 6e 64 20 61 20 6e 65 77 20 76 69 72 74 75 61  and a new virtua
2f007 6c 0a 2a 2a 20 74 65 72 6d 20 6f 66 20 74 68 65  l.** term of the
2f008 20 66 6f 72 6d 20 22 59 20 3c 6f 70 3e 20 58 22   form "Y <op> X"
2f009 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65   is added to the
2f00a 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 61 6e   WHERE clause an
2f00b 64 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 73 65  d.** analyzed se
2f00c 70 61 72 61 74 65 6c 79 2e 20 20 54 68 65 20 6f  parately.  The o
2f00d 72 69 67 69 6e 61 6c 20 74 65 72 6d 20 69 73 20  riginal term is 
2f00e 6d 61 72 6b 65 64 20 77 69 74 68 20 54 45 52 4d  marked with TERM
2f00f 5f 43 4f 50 49 45 44 0a 2a 2a 20 61 6e 64 20 74  _COPIED.** and t
2f010 68 65 20 6e 65 77 20 74 65 72 6d 20 69 73 20 6d  he new term is m
2f011 61 72 6b 65 64 20 77 69 74 68 20 54 45 52 4d 5f  arked with TERM_
2f012 44 59 4e 41 4d 49 43 20 28 62 65 63 61 75 73 65  DYNAMIC (because
2f013 20 69 74 27 73 20 70 45 78 70 72 0a 2a 2a 20 6e   it's pExpr.** n
2f014 65 65 64 73 20 74 6f 20 62 65 20 66 72 65 65 64  eeds to be freed
2f015 20 77 69 74 68 20 74 68 65 20 57 68 65 72 65 43   with the WhereC
2f016 6c 61 75 73 65 29 20 61 6e 64 20 54 45 52 4d 5f  lause) and TERM_
2f017 56 49 52 54 55 41 4c 20 28 62 65 63 61 75 73 65  VIRTUAL (because
2f018 20 69 74 0a 2a 2a 20 69 73 20 61 20 63 6f 6d 6d   it.** is a comm
2f019 75 74 65 64 20 63 6f 70 79 20 6f 66 20 61 20 70  uted copy of a p
2f01a 72 69 6f 72 20 74 65 72 6d 2e 29 20 20 54 68 65  rior term.)  The
2f01b 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 20 68   original term h
2f01c 61 73 20 6e 43 68 69 6c 64 3d 31 0a 2a 2a 20 61  as nChild=1.** a
2f01d 6e 64 20 74 68 65 20 63 6f 70 79 20 68 61 73 20  nd the copy has 
2f01e 69 64 78 50 61 72 65 6e 74 20 73 65 74 20 74 6f  idxParent set to
2f01f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   the index of th
2f020 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72 6d 2e  e original term.
2f021 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2f022 65 78 70 72 41 6e 61 6c 79 7a 65 28 0a 20 20 53  exprAnalyze(.  S
2f023 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20  rcList *pSrc,   
2f024 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
2f025 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
2f026 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2f027 43 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68  C,         /* th
2f028 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2f029 2f 0a 20 20 69 6e 74 20 69 64 78 54 65 72 6d 20  /.  int idxTerm 
2f02a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2f02b 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 74 65   Index of the te
2f02c 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  rm to be analyze
2f02d 64 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54  d */.){.  WhereT
2f02e 65 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20  erm *pTerm;     
2f02f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2f030 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e 61  e term to be ana
2f031 6c 79 7a 65 64 20 2a 2f 0a 20 20 57 68 65 72 65  lyzed */.  Where
2f032 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2f033 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  t;          /* S
2f034 65 74 20 6f 66 20 74 61 62 6c 65 20 69 6e 64 65  et of table inde
2f035 78 20 6d 61 73 6b 73 20 2a 2f 0a 20 20 45 78 70  x masks */.  Exp
2f036 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
2f037 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2f038 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
2f039 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a  to be analyzed *
2f03a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72  /.  Bitmask prer
2f03b 65 71 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20  eqLeft;         
2f03c 20 20 20 20 20 2f 2a 20 50 72 65 72 65 71 75 65       /* Prereque
2f03d 73 69 74 65 73 20 6f 66 20 74 68 65 20 70 45 78  sites of the pEx
2f03e 70 72 2d 3e 70 4c 65 66 74 20 2a 2f 0a 20 20 42  pr->pLeft */.  B
2f03f 69 74 6d 61 73 6b 20 70 72 65 72 65 71 41 6c 6c  itmask prereqAll
2f040 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2f041 2f 2a 20 50 72 65 72 65 71 75 65 73 69 74 65 73  /* Prerequesites
2f042 20 6f 66 20 70 45 78 70 72 20 2a 2f 0a 20 20 42   of pExpr */.  B
2f043 69 74 6d 61 73 6b 20 65 78 74 72 61 52 69 67 68  itmask extraRigh
2f044 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
2f045 2f 2a 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 53  /* */.  Expr *pS
2f046 74 72 31 20 3d 20 30 3b 20 20 20 20 20 20 20 20  tr1 = 0;        
2f047 20 20 20 20 20 20 20 20 20 2f 2a 20 52 48 53 20           /* RHS 
2f048 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f 70 65  of LIKE/GLOB ope
2f049 72 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69  rator */.  int i
2f04a 73 43 6f 6d 70 6c 65 74 65 20 3d 20 30 3b 20 20  sComplete = 0;  
2f04b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2f04c 48 53 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20  HS of LIKE/GLOB 
2f04d 65 6e 64 73 20 77 69 74 68 20 77 69 6c 64 63 61  ends with wildca
2f04e 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 61  rd */.  int noCa
2f04f 73 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  se = 0;         
2f050 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 49 4b 45           /* LIKE
2f051 2f 47 4c 4f 42 20 64 69 73 74 69 6e 67 75 69 73  /GLOB distinguis
2f052 68 65 73 20 63 61 73 65 20 2a 2f 0a 20 20 69 6e  hes case */.  in
2f053 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  t op;           
2f054 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f055 2a 20 54 6f 70 2d 6c 65 76 65 6c 20 6f 70 65 72  * Top-level oper
2f056 61 74 6f 72 2e 20 20 70 45 78 70 72 2d 3e 6f 70  ator.  pExpr->op
2f057 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a 70 50 61   */.  Parse *pPa
2f058 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73  rse = pWC->pPars
2f059 65 3b 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  e;     /* Parsin
2f05a 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 73  g context */.  s
2f05b 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2f05c 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20  rse->db;        
2f05d 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
2f05e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28  ection */..  if(
2f05f 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2f060 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  d ){.    return;
2f061 0a 20 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20 26  .  }.  pTerm = &
2f062 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b  pWC->a[idxTerm];
2f063 0a 20 20 70 4d 61 73 6b 53 65 74 20 3d 20 70 57  .  pMaskSet = pW
2f064 43 2d 3e 70 4d 61 73 6b 53 65 74 3b 0a 20 20 70  C->pMaskSet;.  p
2f065 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45  Expr = pTerm->pE
2f066 78 70 72 3b 0a 20 20 70 72 65 72 65 71 4c 65 66  xpr;.  prereqLef
2f067 74 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  t = exprTableUsa
2f068 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78  ge(pMaskSet, pEx
2f069 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 6f 70  pr->pLeft);.  op
2f06a 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20   = pExpr->op;.  
2f06b 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29 7b  if( op==TK_IN ){
2f06c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78  .    assert( pEx
2f06d 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b  pr->pRight==0 );
2f06e 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73  .    if( ExprHas
2f06f 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2f070 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b  EP_xIsSelect) ){
2f071 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 70 72  .      pTerm->pr
2f072 65 72 65 71 52 69 67 68 74 20 3d 20 65 78 70 72  ereqRight = expr
2f073 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65  SelectTableUsage
2f074 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
2f075 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20  ->x.pSelect);.  
2f076 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
2f077 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68  Term->prereqRigh
2f078 74 20 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c  t = exprListTabl
2f079 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
2f07a 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 29   pExpr->x.pList)
2f07b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
2f07c 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c  if( op==TK_ISNUL
2f07d 4c 20 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e  L ){.    pTerm->
2f07e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 30 3b  prereqRight = 0;
2f07f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54  .  }else{.    pT
2f080 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
2f081 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67   = exprTableUsag
2f082 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70  e(pMaskSet, pExp
2f083 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a  r->pRight);.  }.
2f084 20 20 70 72 65 72 65 71 41 6c 6c 20 3d 20 65 78    prereqAll = ex
2f085 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  prTableUsage(pMa
2f086 73 6b 53 65 74 2c 20 70 45 78 70 72 29 3b 0a 20  skSet, pExpr);. 
2f087 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70   if( ExprHasProp
2f088 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 46  erty(pExpr, EP_F
2f089 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20  romJoin) ){.    
2f08a 42 69 74 6d 61 73 6b 20 78 20 3d 20 67 65 74 4d  Bitmask x = getM
2f08b 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 45  ask(pMaskSet, pE
2f08c 78 70 72 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54  xpr->iRightJoinT
2f08d 61 62 6c 65 29 3b 0a 20 20 20 20 70 72 65 72 65  able);.    prere
2f08e 71 41 6c 6c 20 7c 3d 20 78 3b 0a 20 20 20 20 65  qAll |= x;.    e
2f08f 78 74 72 61 52 69 67 68 74 20 3d 20 78 2d 31 3b  xtraRight = x-1;
2f090 20 20 2f 2a 20 4f 4e 20 63 6c 61 75 73 65 20 74    /* ON clause t
2f091 65 72 6d 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  erms may not be 
2f092 75 73 65 64 20 77 69 74 68 20 61 6e 20 69 6e 64  used with an ind
2f093 65 78 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ex.             
2f094 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20            ** on 
2f095 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20 61 20  left table of a 
2f096 4c 45 46 54 20 4a 4f 49 4e 2e 20 20 54 69 63 6b  LEFT JOIN.  Tick
2f097 65 74 20 23 33 30 31 35 20 2a 2f 0a 20 20 7d 0a  et #3015 */.  }.
2f098 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41    pTerm->prereqA
2f099 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a  ll = prereqAll;.
2f09a 20 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72    pTerm->leftCur
2f09b 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72  sor = -1;.  pTer
2f09c 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b  m->iParent = -1;
2f09d 0a 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  .  pTerm->eOpera
2f09e 74 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 61  tor = 0;.  if( a
2f09f 6c 6c 6f 77 65 64 4f 70 28 6f 70 29 20 26 26 20  llowedOp(op) && 
2f0a0 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69  (pTerm->prereqRi
2f0a1 67 68 74 20 26 20 70 72 65 72 65 71 4c 65 66 74  ght & prereqLeft
2f0a2 29 3d 3d 30 20 29 7b 0a 20 20 20 20 45 78 70 72  )==0 ){.    Expr
2f0a3 20 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d   *pLeft = pExpr-
2f0a4 3e 70 4c 65 66 74 3b 0a 20 20 20 20 45 78 70 72  >pLeft;.    Expr
2f0a5 20 2a 70 52 69 67 68 74 20 3d 20 70 45 78 70 72   *pRight = pExpr
2f0a6 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66  ->pRight;.    if
2f0a7 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ( pLeft->op==TK_
2f0a8 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20  COLUMN ){.      
2f0a9 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
2f0aa 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c  r = pLeft->iTabl
2f0ab 65 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  e;.      pTerm->
2f0ac 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70  u.leftColumn = p
2f0ad 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  Left->iColumn;. 
2f0ae 20 20 20 20 20 70 54 65 72 6d 2d 3e 65 4f 70 65       pTerm->eOpe
2f0af 72 61 74 6f 72 20 3d 20 6f 70 65 72 61 74 6f 72  rator = operator
2f0b0 4d 61 73 6b 28 6f 70 29 3b 0a 20 20 20 20 7d 0a  Mask(op);.    }.
2f0b1 20 20 20 20 69 66 28 20 70 52 69 67 68 74 20 26      if( pRight &
2f0b2 26 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b  & pRight->op==TK
2f0b3 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20  _COLUMN ){.     
2f0b4 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4e 65 77   WhereTerm *pNew
2f0b5 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 44  ;.      Expr *pD
2f0b6 75 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54  up;.      if( pT
2f0b7 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3e  erm->leftCursor>
2f0b8 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  =0 ){.        in
2f0b9 74 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20  t idxNew;.      
2f0ba 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
2f0bb 45 78 70 72 44 75 70 28 64 62 2c 20 70 45 78 70  ExprDup(db, pExp
2f0bc 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  r, 0);.        i
2f0bd 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2f0be 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  led ){.         
2f0bf 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2f0c0 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 20 20  te(db, pDup);.  
2f0c1 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
2f0c2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f0c3 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65    idxNew = where
2f0c4 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
2f0c5 2c 20 70 44 75 70 2c 20 54 45 52 4d 5f 56 49 52  , pDup, TERM_VIR
2f0c6 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49  TUAL|TERM_DYNAMI
2f0c7 43 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  C);.        if( 
2f0c8 69 64 78 4e 65 77 3d 3d 30 20 29 20 72 65 74 75  idxNew==0 ) retu
2f0c9 72 6e 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  rn;.        pNew
2f0ca 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65   = &pWC->a[idxNe
2f0cb 77 5d 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77  w];.        pNew
2f0cc 2d 3e 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54  ->iParent = idxT
2f0cd 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 65  erm;.        pTe
2f0ce 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78  rm = &pWC->a[idx
2f0cf 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70  Term];.        p
2f0d0 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31  Term->nChild = 1
2f0d1 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 2d  ;.        pTerm-
2f0d2 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d  >wtFlags |= TERM
2f0d3 5f 43 4f 50 49 45 44 3b 0a 20 20 20 20 20 20 7d  _COPIED;.      }
2f0d4 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 44  else{.        pD
2f0d5 75 70 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20  up = pExpr;.    
2f0d6 20 20 20 20 70 4e 65 77 20 3d 20 70 54 65 72 6d      pNew = pTerm
2f0d7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2f0d8 65 78 70 72 43 6f 6d 6d 75 74 65 28 70 50 61 72  exprCommute(pPar
2f0d9 73 65 2c 20 70 44 75 70 29 3b 0a 20 20 20 20 20  se, pDup);.     
2f0da 20 70 4c 65 66 74 20 3d 20 70 44 75 70 2d 3e 70   pLeft = pDup->p
2f0db 4c 65 66 74 3b 0a 20 20 20 20 20 20 70 4e 65 77  Left;.      pNew
2f0dc 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d 20 70  ->leftCursor = p
2f0dd 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20  Left->iTable;.  
2f0de 20 20 20 20 70 4e 65 77 2d 3e 75 2e 6c 65 66 74      pNew->u.left
2f0df 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e  Column = pLeft->
2f0e0 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70  iColumn;.      p
2f0e1 4e 65 77 2d 3e 70 72 65 72 65 71 52 69 67 68 74  New->prereqRight
2f0e2 20 3d 20 70 72 65 72 65 71 4c 65 66 74 3b 0a 20   = prereqLeft;. 
2f0e3 20 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 72 65       pNew->prere
2f0e4 71 41 6c 6c 20 3d 20 70 72 65 72 65 71 41 6c 6c  qAll = prereqAll
2f0e5 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 65 4f  ;.      pNew->eO
2f0e6 70 65 72 61 74 6f 72 20 3d 20 6f 70 65 72 61 74  perator = operat
2f0e7 6f 72 4d 61 73 6b 28 70 44 75 70 2d 3e 6f 70 29  orMask(pDup->op)
2f0e8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66  ;.    }.  }..#if
2f0e9 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2f0ea 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a  _BETWEEN_OPTIMIZ
2f0eb 41 54 49 4f 4e 0a 20 20 2f 2a 20 49 66 20 61 20  ATION.  /* If a 
2f0ec 74 65 72 6d 20 69 73 20 74 68 65 20 42 45 54 57  term is the BETW
2f0ed 45 45 4e 20 6f 70 65 72 61 74 6f 72 2c 20 63 72  EEN operator, cr
2f0ee 65 61 74 65 20 74 77 6f 20 6e 65 77 20 76 69 72  eate two new vir
2f0ef 74 75 61 6c 20 74 65 72 6d 73 0a 20 20 2a 2a 20  tual terms.  ** 
2f0f0 74 68 61 74 20 64 65 66 69 6e 65 20 74 68 65 20  that define the 
2f0f1 72 61 6e 67 65 20 74 68 61 74 20 74 68 65 20 42  range that the B
2f0f2 45 54 57 45 45 4e 20 69 6d 70 6c 65 6d 65 6e 74  ETWEEN implement
2f0f3 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a  s.  For example:
2f0f4 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
2f0f5 61 20 42 45 54 57 45 45 4e 20 62 20 41 4e 44 20  a BETWEEN b AND 
2f0f6 63 0a 20 20 2a 2a 0a 20 20 2a 2a 20 69 73 20 63  c.  **.  ** is c
2f0f7 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 3a 0a 20  onverted into:. 
2f0f8 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 28 61   **.  **      (a
2f0f9 20 42 45 54 57 45 45 4e 20 62 20 41 4e 44 20 63   BETWEEN b AND c
2f0fa 29 20 41 4e 44 20 28 61 3e 3d 62 29 20 41 4e 44  ) AND (a>=b) AND
2f0fb 20 28 61 3c 3d 63 29 0a 20 20 2a 2a 0a 20 20 2a   (a<=c).  **.  *
2f0fc 2a 20 54 68 65 20 74 77 6f 20 6e 65 77 20 74 65  * The two new te
2f0fd 72 6d 73 20 61 72 65 20 61 64 64 65 64 20 6f 6e  rms are added on
2f0fe 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
2f0ff 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 6f 62  e WhereClause ob
2f100 6a 65 63 74 2e 0a 20 20 2a 2a 20 54 68 65 20 6e  ject..  ** The n
2f101 65 77 20 74 65 72 6d 73 20 61 72 65 20 22 64 79  ew terms are "dy
2f102 6e 61 6d 69 63 22 20 61 6e 64 20 61 72 65 20 63  namic" and are c
2f103 68 69 6c 64 72 65 6e 20 6f 66 20 74 68 65 20 6f  hildren of the o
2f104 72 69 67 69 6e 61 6c 20 42 45 54 57 45 45 4e 0a  riginal BETWEEN.
2f105 20 20 2a 2a 20 74 65 72 6d 2e 20 20 54 68 61 74    ** term.  That
2f106 20 6d 65 61 6e 73 20 74 68 61 74 20 69 66 20 74   means that if t
2f107 68 65 20 42 45 54 57 45 45 4e 20 74 65 72 6d 20  he BETWEEN term 
2f108 69 73 20 63 6f 64 65 64 2c 20 74 68 65 20 63 68  is coded, the ch
2f109 69 6c 64 72 65 6e 20 61 72 65 0a 20 20 2a 2a 20  ildren are.  ** 
2f10a 73 6b 69 70 70 65 64 2e 20 20 4f 72 2c 20 69 66  skipped.  Or, if
2f10b 20 74 68 65 20 63 68 69 6c 64 72 65 6e 20 61 72   the children ar
2f10c 65 20 73 61 74 69 73 66 69 65 64 20 62 79 20 61  e satisfied by a
2f10d 6e 20 69 6e 64 65 78 2c 20 74 68 65 20 6f 72 69  n index, the ori
2f10e 67 69 6e 61 6c 0a 20 20 2a 2a 20 42 45 54 57 45  ginal.  ** BETWE
2f10f 45 4e 20 74 65 72 6d 20 69 73 20 73 6b 69 70 70  EN term is skipp
2f110 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20  ed..  */.  else 
2f111 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  if( pExpr->op==T
2f112 4b 5f 42 45 54 57 45 45 4e 20 26 26 20 70 57 43  K_BETWEEN && pWC
2f113 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 29 7b 0a  ->op==TK_AND ){.
2f114 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
2f115 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  ist = pExpr->x.p
2f116 4c 69 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b  List;.    int i;
2f117 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
2f118 74 20 75 38 20 6f 70 73 5b 5d 20 3d 20 7b 54 4b  t u8 ops[] = {TK
2f119 5f 47 45 2c 20 54 4b 5f 4c 45 7d 3b 0a 20 20 20  _GE, TK_LE};.   
2f11a 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d   assert( pList!=
2f11b 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
2f11c 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 32   pList->nExpr==2
2f11d 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   );.    for(i=0;
2f11e 20 69 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<2; i++){.    
2f11f 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72    Expr *pNewExpr
2f120 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e  ;.      int idxN
2f121 65 77 3b 0a 20 20 20 20 20 20 70 4e 65 77 45 78  ew;.      pNewEx
2f122 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
2f123 72 28 70 50 61 72 73 65 2c 20 6f 70 73 5b 69 5d  r(pParse, ops[i]
2f124 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2f125 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f126 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2f127 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  b, pExpr->pLeft,
2f128 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20   0),.           
2f129 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f12a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70    sqlite3ExprDup
2f12b 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  (db, pList->a[i]
2f12c 2e 70 45 78 70 72 2c 20 30 29 2c 20 30 29 3b 0a  .pExpr, 0), 0);.
2f12d 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77        idxNew = w
2f12e 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74  hereClauseInsert
2f12f 28 70 57 43 2c 20 70 4e 65 77 45 78 70 72 2c 20  (pWC, pNewExpr, 
2f130 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52  TERM_VIRTUAL|TER
2f131 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  M_DYNAMIC);.    
2f132 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e    testcase( idxN
2f133 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 65  ew==0 );.      e
2f134 78 70 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c  xprAnalyze(pSrc,
2f135 20 70 57 43 2c 20 69 64 78 4e 65 77 29 3b 0a 20   pWC, idxNew);. 
2f136 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57       pTerm = &pW
2f137 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
2f138 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e       pWC->a[idxN
2f139 65 77 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64  ew].iParent = id
2f13a 78 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  xTerm;.    }.   
2f13b 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d   pTerm->nChild =
2f13c 20 32 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f   2;.  }.#endif /
2f13d 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 45  * SQLITE_OMIT_BE
2f13e 54 57 45 45 4e 5f 4f 50 54 49 4d 49 5a 41 54 49  TWEEN_OPTIMIZATI
2f13f 4f 4e 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69  ON */..#if !defi
2f140 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2f141 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 29  OR_OPTIMIZATION)
2f142 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
2f143 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2f144 59 29 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a 65 20  Y).  /* Analyze 
2f145 61 20 74 65 72 6d 20 74 68 61 74 20 69 73 20 63  a term that is c
2f146 6f 6d 70 6f 73 65 64 20 6f 66 20 74 77 6f 20 6f  omposed of two o
2f147 72 20 6d 6f 72 65 20 73 75 62 74 65 72 6d 73 20  r more subterms 
2f148 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a 20 20 2a  connected by.  *
2f149 2a 20 61 6e 20 4f 52 20 6f 70 65 72 61 74 6f 72  * an OR operator
2f14a 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66  ..  */.  else if
2f14b 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
2f14c 4f 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  OR ){.    assert
2f14d 28 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e  ( pWC->op==TK_AN
2f14e 44 20 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61  D );.    exprAna
2f14f 6c 79 7a 65 4f 72 54 65 72 6d 28 70 53 72 63 2c  lyzeOrTerm(pSrc,
2f150 20 70 57 43 2c 20 69 64 78 54 65 72 6d 29 3b 0a   pWC, idxTerm);.
2f151 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43      pTerm = &pWC
2f152 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20  ->a[idxTerm];.  
2f153 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2f154 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
2f155 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 6e  IZATION */..#ifn
2f156 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2f157 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f  LIKE_OPTIMIZATIO
2f158 4e 0a 20 20 2f 2a 20 41 64 64 20 63 6f 6e 73 74  N.  /* Add const
2f159 72 61 69 6e 74 73 20 74 6f 20 72 65 64 75 63 65  raints to reduce
2f15a 20 74 68 65 20 73 65 61 72 63 68 20 73 70 61 63   the search spac
2f15b 65 20 6f 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47  e on a LIKE or G
2f15c 4c 4f 42 0a 20 20 2a 2a 20 6f 70 65 72 61 74 6f  LOB.  ** operato
2f15d 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 6c  r..  **.  ** A l
2f15e 69 6b 65 20 70 61 74 74 65 72 6e 20 6f 66 20 74  ike pattern of t
2f15f 68 65 20 66 6f 72 6d 20 22 78 20 4c 49 4b 45 20  he form "x LIKE 
2f160 27 61 62 63 25 27 22 20 69 73 20 63 68 61 6e 67  'abc%'" is chang
2f161 65 64 20 69 6e 74 6f 20 63 6f 6e 73 74 72 61 69  ed into constrai
2f162 6e 74 73 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  nts.  **.  **   
2f163 20 20 20 20 20 20 20 78 3e 3d 27 61 62 63 27 20         x>='abc' 
2f164 41 4e 44 20 78 3c 27 61 62 64 27 20 41 4e 44 20  AND x<'abd' AND 
2f165 78 20 4c 49 4b 45 20 27 61 62 63 25 27 0a 20 20  x LIKE 'abc%'.  
2f166 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 61 73 74  **.  ** The last
2f167 20 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68   character of th
2f168 65 20 70 72 65 66 69 78 20 22 61 62 63 22 20 69  e prefix "abc" i
2f169 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 74 6f  s incremented to
2f16a 20 66 6f 72 6d 20 74 68 65 0a 20 20 2a 2a 20 74   form the.  ** t
2f16b 65 72 6d 69 6e 61 74 69 6f 6e 20 63 6f 6e 64 69  ermination condi
2f16c 74 69 6f 6e 20 22 61 62 64 22 2e 0a 20 20 2a 2f  tion "abd"..  */
2f16d 0a 20 20 69 66 28 20 70 57 43 2d 3e 6f 70 3d 3d  .  if( pWC->op==
2f16e 54 4b 5f 41 4e 44 20 0a 20 20 20 26 26 20 69 73  TK_AND .   && is
2f16f 4c 69 6b 65 4f 72 47 6c 6f 62 28 70 50 61 72 73  LikeOrGlob(pPars
2f170 65 2c 20 70 45 78 70 72 2c 20 26 70 53 74 72 31  e, pExpr, &pStr1
2f171 2c 20 26 69 73 43 6f 6d 70 6c 65 74 65 2c 20 26  , &isComplete, &
2f172 6e 6f 43 61 73 65 29 0a 20 20 29 7b 0a 20 20 20  noCase).  ){.   
2f173 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 20 20 20   Expr *pLeft;   
2f174 20 20 20 20 2f 2a 20 4c 48 53 20 6f 66 20 4c 49      /* LHS of LI
2f175 4b 45 2f 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72  KE/GLOB operator
2f176 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 53   */.    Expr *pS
2f177 74 72 32 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f  tr2;       /* Co
2f178 70 79 20 6f 66 20 70 53 74 72 31 20 2d 20 52 48  py of pStr1 - RH
2f179 53 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f  S of LIKE/GLOB o
2f17a 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45  perator */.    E
2f17b 78 70 72 20 2a 70 4e 65 77 45 78 70 72 31 3b 0a  xpr *pNewExpr1;.
2f17c 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78      Expr *pNewEx
2f17d 70 72 32 3b 0a 20 20 20 20 69 6e 74 20 69 64 78  pr2;.    int idx
2f17e 4e 65 77 31 3b 0a 20 20 20 20 69 6e 74 20 69 64  New1;.    int id
2f17f 78 4e 65 77 32 3b 0a 0a 20 20 20 20 70 4c 65 66  xNew2;..    pLef
2f180 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  t = pExpr->x.pLi
2f181 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a  st->a[1].pExpr;.
2f182 20 20 20 20 70 53 74 72 32 20 3d 20 73 71 6c 69      pStr2 = sqli
2f183 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
2f184 53 74 72 31 2c 20 30 29 3b 0a 20 20 20 20 69 66  Str1, 0);.    if
2f185 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
2f186 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 75 38 20  led ){.      u8 
2f187 63 2c 20 2a 70 43 3b 20 20 20 20 20 20 20 2f 2a  c, *pC;       /*
2f188 20 4c 61 73 74 20 63 68 61 72 61 63 74 65 72 20   Last character 
2f189 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74  before the first
2f18a 20 77 69 6c 64 63 61 72 64 20 2a 2f 0a 20 20 20   wildcard */.   
2f18b 20 20 20 70 43 20 3d 20 28 75 38 2a 29 26 70 53     pC = (u8*)&pS
2f18c 74 72 32 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 73 71  tr2->u.zToken[sq
2f18d 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 53  lite3Strlen30(pS
2f18e 74 72 32 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 2d 31  tr2->u.zToken)-1
2f18f 5d 3b 0a 20 20 20 20 20 20 63 20 3d 20 2a 70 43  ];.      c = *pC
2f190 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61  ;.      if( noCa
2f191 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  se ){.        /*
2f192 20 54 68 65 20 70 6f 69 6e 74 20 69 73 20 74 6f   The point is to
2f193 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 6c   increment the l
2f194 61 73 74 20 63 68 61 72 61 63 74 65 72 20 62 65  ast character be
2f195 66 6f 72 65 20 74 68 65 20 66 69 72 73 74 0a 20  fore the first. 
2f196 20 20 20 20 20 20 20 2a 2a 20 77 69 6c 64 63 61         ** wildca
2f197 72 64 2e 20 20 42 75 74 20 69 66 20 77 65 20 69  rd.  But if we i
2f198 6e 63 72 65 6d 65 6e 74 20 27 40 27 2c 20 74 68  ncrement '@', th
2f199 61 74 20 77 69 6c 6c 20 70 75 73 68 20 69 74 20  at will push it 
2f19a 69 6e 74 6f 20 74 68 65 0a 20 20 20 20 20 20 20  into the.       
2f19b 20 2a 2a 20 61 6c 70 68 61 62 65 74 69 63 20 72   ** alphabetic r
2f19c 61 6e 67 65 20 77 68 65 72 65 20 63 61 73 65 20  ange where case 
2f19d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77 69 6c 6c  conversions will
2f19e 20 6d 65 73 73 20 75 70 20 74 68 65 20 0a 20 20   mess up the .  
2f19f 20 20 20 20 20 20 2a 2a 20 69 6e 65 71 75 61 6c        ** inequal
2f1a0 69 74 79 2e 20 20 54 6f 20 61 76 6f 69 64 20 74  ity.  To avoid t
2f1a1 68 69 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 74  his, make sure t
2f1a2 6f 20 61 6c 73 6f 20 72 75 6e 20 74 68 65 20 66  o also run the f
2f1a3 75 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 4c  ull.        ** L
2f1a4 49 4b 45 20 6f 6e 20 61 6c 6c 20 63 61 6e 64 69  IKE on all candi
2f1a5 64 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73  date expressions
2f1a6 20 62 79 20 63 6c 65 61 72 69 6e 67 20 74 68 65   by clearing the
2f1a7 20 69 73 43 6f 6d 70 6c 65 74 65 20 66 6c 61 67   isComplete flag
2f1a8 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
2f1a9 20 20 20 20 69 66 28 20 63 3d 3d 27 41 27 2d 31      if( c=='A'-1
2f1aa 20 29 20 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20   ) isComplete = 
2f1ab 30 3b 0a 0a 20 20 20 20 20 20 20 20 63 20 3d 20  0;..        c = 
2f1ac 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
2f1ad 77 65 72 5b 63 5d 3b 0a 20 20 20 20 20 20 7d 0a  wer[c];.      }.
2f1ae 20 20 20 20 20 20 2a 70 43 20 3d 20 63 20 2b 20        *pC = c + 
2f1af 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65  1;.    }.    pNe
2f1b0 77 45 78 70 72 31 20 3d 20 73 71 6c 69 74 65 33  wExpr1 = sqlite3
2f1b1 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2f1b2 5f 47 45 2c 20 73 71 6c 69 74 65 33 45 78 70 72  _GE, sqlite3Expr
2f1b3 44 75 70 28 64 62 2c 70 4c 65 66 74 2c 30 29 2c  Dup(db,pLeft,0),
2f1b4 70 53 74 72 31 2c 30 29 3b 0a 20 20 20 20 69 64  pStr1,0);.    id
2f1b5 78 4e 65 77 31 20 3d 20 77 68 65 72 65 43 6c 61  xNew1 = whereCla
2f1b6 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70  useInsert(pWC, p
2f1b7 4e 65 77 45 78 70 72 31 2c 20 54 45 52 4d 5f 56  NewExpr1, TERM_V
2f1b8 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41  IRTUAL|TERM_DYNA
2f1b9 4d 49 43 29 3b 0a 20 20 20 20 74 65 73 74 63 61  MIC);.    testca
2f1ba 73 65 28 20 69 64 78 4e 65 77 31 3d 3d 30 20 29  se( idxNew1==0 )
2f1bb 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a  ;.    exprAnalyz
2f1bc 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78  e(pSrc, pWC, idx
2f1bd 4e 65 77 31 29 3b 0a 20 20 20 20 70 4e 65 77 45  New1);.    pNewE
2f1be 78 70 72 32 20 3d 20 73 71 6c 69 74 65 33 50 45  xpr2 = sqlite3PE
2f1bf 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4c  xpr(pParse, TK_L
2f1c0 54 2c 20 73 71 6c 69 74 65 33 45 78 70 72 44 75  T, sqlite3ExprDu
2f1c1 70 28 64 62 2c 70 4c 65 66 74 2c 30 29 2c 70 53  p(db,pLeft,0),pS
2f1c2 74 72 32 2c 30 29 3b 0a 20 20 20 20 69 64 78 4e  tr2,0);.    idxN
2f1c3 65 77 32 20 3d 20 77 68 65 72 65 43 6c 61 75 73  ew2 = whereClaus
2f1c4 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65  eInsert(pWC, pNe
2f1c5 77 45 78 70 72 32 2c 20 54 45 52 4d 5f 56 49 52  wExpr2, TERM_VIR
2f1c6 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49  TUAL|TERM_DYNAMI
2f1c7 43 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  C);.    testcase
2f1c8 28 20 69 64 78 4e 65 77 32 3d 3d 30 20 29 3b 0a  ( idxNew2==0 );.
2f1c9 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28      exprAnalyze(
2f1ca 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65  pSrc, pWC, idxNe
2f1cb 77 32 29 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d  w2);.    pTerm =
2f1cc 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
2f1cd 5d 3b 0a 20 20 20 20 69 66 28 20 69 73 43 6f 6d  ];.    if( isCom
2f1ce 70 6c 65 74 65 20 29 7b 0a 20 20 20 20 20 20 70  plete ){.      p
2f1cf 57 43 2d 3e 61 5b 69 64 78 4e 65 77 31 5d 2e 69  WC->a[idxNew1].i
2f1d0 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d  Parent = idxTerm
2f1d1 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69  ;.      pWC->a[i
2f1d2 64 78 4e 65 77 32 5d 2e 69 50 61 72 65 6e 74 20  dxNew2].iParent 
2f1d3 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20  = idxTerm;.     
2f1d4 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d   pTerm->nChild =
2f1d5 20 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65   2;.    }.  }.#e
2f1d6 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2f1d7 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a  MIT_LIKE_OPTIMIZ
2f1d8 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 6e 64 65  ATION */..#ifnde
2f1d9 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2f1da 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 2f 2a 20  RTUALTABLE.  /* 
2f1db 41 64 64 20 61 20 57 4f 5f 4d 41 54 43 48 20 61  Add a WO_MATCH a
2f1dc 75 78 69 6c 69 61 72 79 20 74 65 72 6d 20 74 6f  uxiliary term to
2f1dd 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
2f1de 73 65 74 20 69 66 20 74 68 65 0a 20 20 2a 2a 20  set if the.  ** 
2f1df 63 75 72 72 65 6e 74 20 65 78 70 72 65 73 73 69  current expressi
2f1e0 6f 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  on is of the for
2f1e1 6d 3a 20 20 63 6f 6c 75 6d 6e 20 4d 41 54 43 48  m:  column MATCH
2f1e2 20 65 78 70 72 2e 0a 20 20 2a 2a 20 54 68 69 73   expr..  ** This
2f1e3 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
2f1e4 75 73 65 64 20 62 79 20 74 68 65 20 78 42 65 73  used by the xBes
2f1e5 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 73 20 6f  tIndex methods o
2f1e6 66 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 74  f.  ** virtual t
2f1e7 61 62 6c 65 73 2e 20 20 54 68 65 20 6e 61 74 69  ables.  The nati
2f1e8 76 65 20 71 75 65 72 79 20 6f 70 74 69 6d 69 7a  ve query optimiz
2f1e9 65 72 20 64 6f 65 73 20 6e 6f 74 20 61 74 74 65  er does not atte
2f1ea 6d 70 74 0a 20 20 2a 2a 20 74 6f 20 64 6f 20 61  mpt.  ** to do a
2f1eb 6e 79 74 68 69 6e 67 20 77 69 74 68 20 4d 41 54  nything with MAT
2f1ec 43 48 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20  CH functions..  
2f1ed 2a 2f 0a 20 20 69 66 28 20 69 73 4d 61 74 63 68  */.  if( isMatch
2f1ee 4f 66 43 6f 6c 75 6d 6e 28 70 45 78 70 72 29 20  OfColumn(pExpr) 
2f1ef 29 7b 0a 20 20 20 20 69 6e 74 20 69 64 78 4e 65  ){.    int idxNe
2f1f0 77 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 52 69  w;.    Expr *pRi
2f1f1 67 68 74 2c 20 2a 70 4c 65 66 74 3b 0a 20 20 20  ght, *pLeft;.   
2f1f2 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4e 65 77   WhereTerm *pNew
2f1f3 54 65 72 6d 3b 0a 20 20 20 20 42 69 74 6d 61 73  Term;.    Bitmas
2f1f4 6b 20 70 72 65 72 65 71 43 6f 6c 75 6d 6e 2c 20  k prereqColumn, 
2f1f5 70 72 65 72 65 71 45 78 70 72 3b 0a 0a 20 20 20  prereqExpr;..   
2f1f6 20 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d   pRight = pExpr-
2f1f7 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  >x.pList->a[0].p
2f1f8 45 78 70 72 3b 0a 20 20 20 20 70 4c 65 66 74 20  Expr;.    pLeft 
2f1f9 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
2f1fa 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[1].pExpr;.  
2f1fb 20 20 70 72 65 72 65 71 45 78 70 72 20 3d 20 65    prereqExpr = e
2f1fc 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
2f1fd 61 73 6b 53 65 74 2c 20 70 52 69 67 68 74 29 3b  askSet, pRight);
2f1fe 0a 20 20 20 20 70 72 65 72 65 71 43 6f 6c 75 6d  .    prereqColum
2f1ff 6e 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  n = exprTableUsa
2f200 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c 65  ge(pMaskSet, pLe
2f201 66 74 29 3b 0a 20 20 20 20 69 66 28 20 28 70 72  ft);.    if( (pr
2f202 65 72 65 71 45 78 70 72 20 26 20 70 72 65 72 65  ereqExpr & prere
2f203 71 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20  qColumn)==0 ){. 
2f204 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45       Expr *pNewE
2f205 78 70 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 45  xpr;.      pNewE
2f206 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
2f207 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4d 41  pr(pParse, TK_MA
2f208 54 43 48 2c 20 0a 20 20 20 20 20 20 20 20 20 20  TCH, .          
2f209 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f20a 20 20 20 20 30 2c 20 73 71 6c 69 74 65 33 45 78      0, sqlite3Ex
2f20b 70 72 44 75 70 28 64 62 2c 20 70 52 69 67 68 74  prDup(db, pRight
2f20c 2c 20 30 29 2c 20 30 29 3b 0a 20 20 20 20 20 20  , 0), 0);.      
2f20d 69 64 78 4e 65 77 20 3d 20 77 68 65 72 65 43 6c  idxNew = whereCl
2f20e 61 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20  auseInsert(pWC, 
2f20f 70 4e 65 77 45 78 70 72 2c 20 54 45 52 4d 5f 56  pNewExpr, TERM_V
2f210 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41  IRTUAL|TERM_DYNA
2f211 4d 49 43 29 3b 0a 20 20 20 20 20 20 74 65 73 74  MIC);.      test
2f212 63 61 73 65 28 20 69 64 78 4e 65 77 3d 3d 30 20  case( idxNew==0 
2f213 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72  );.      pNewTer
2f214 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e  m = &pWC->a[idxN
2f215 65 77 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 54  ew];.      pNewT
2f216 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
2f217 20 3d 20 70 72 65 72 65 71 45 78 70 72 3b 0a 20   = prereqExpr;. 
2f218 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 6c       pNewTerm->l
2f219 65 66 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66  eftCursor = pLef
2f21a 74 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20  t->iTable;.     
2f21b 20 70 4e 65 77 54 65 72 6d 2d 3e 75 2e 6c 65 66   pNewTerm->u.lef
2f21c 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 2d  tColumn = pLeft-
2f21d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  >iColumn;.      
2f21e 70 4e 65 77 54 65 72 6d 2d 3e 65 4f 70 65 72 61  pNewTerm->eOpera
2f21f 74 6f 72 20 3d 20 57 4f 5f 4d 41 54 43 48 3b 0a  tor = WO_MATCH;.
2f220 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e        pNewTerm->
2f221 69 50 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72  iParent = idxTer
2f222 6d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d  m;.      pTerm =
2f223 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
2f224 5d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e  ];.      pTerm->
2f225 6e 43 68 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20  nChild = 1;.    
2f226 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73    pTerm->wtFlags
2f227 20 7c 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b   |= TERM_COPIED;
2f228 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d  .      pNewTerm-
2f229 3e 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 54 65  >prereqAll = pTe
2f22a 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 3b 0a 20  rm->prereqAll;. 
2f22b 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20     }.  }.#endif 
2f22c 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
2f22d 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
2f22e 20 20 2f 2a 20 50 72 65 76 65 6e 74 20 4f 4e 20    /* Prevent ON 
2f22f 63 6c 61 75 73 65 20 74 65 72 6d 73 20 6f 66 20  clause terms of 
2f230 61 20 4c 45 46 54 20 4a 4f 49 4e 20 66 72 6f 6d  a LEFT JOIN from
2f231 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 64   being used to d
2f232 72 69 76 65 0a 20 20 2a 2a 20 61 6e 20 69 6e 64  rive.  ** an ind
2f233 65 78 20 66 6f 72 20 74 61 62 6c 65 73 20 74 6f  ex for tables to
2f234 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
2f235 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 70 54   join..  */.  pT
2f236 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
2f237 20 7c 3d 20 65 78 74 72 61 52 69 67 68 74 3b 0a   |= extraRight;.
2f238 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2f239 54 52 55 45 20 69 66 20 61 6e 79 20 6f 66 20 74  TRUE if any of t
2f23a 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  he expressions i
2f23b 6e 20 70 4c 69 73 74 2d 3e 61 5b 69 46 69 72 73  n pList->a[iFirs
2f23c 74 2e 2e 2e 5d 20 63 6f 6e 74 61 69 6e 0a 2a 2a  t...] contain.**
2f23d 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
2f23e 61 6e 79 20 74 61 62 6c 65 20 6f 74 68 65 72 20  any table other 
2f23f 74 68 61 6e 20 74 68 65 20 69 42 61 73 65 20 74  than the iBase t
2f240 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
2f241 69 6e 74 20 72 65 66 65 72 65 6e 63 65 73 4f 74  int referencesOt
2f242 68 65 72 54 61 62 6c 65 73 28 0a 20 20 45 78 70  herTables(.  Exp
2f243 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20  rList *pList,   
2f244 20 20 20 20 20 20 20 2f 2a 20 53 65 61 72 63 68         /* Search
2f245 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
2f246 74 68 73 20 6c 69 73 74 20 2a 2f 0a 20 20 57 68  ths list */.  Wh
2f247 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  ereMaskSet *pMas
2f248 6b 53 65 74 2c 20 20 20 2f 2a 20 4d 61 70 70 69  kSet,   /* Mappi
2f249 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 73 20 74  ng from tables t
2f24a 6f 20 62 69 74 6d 61 70 73 20 2a 2f 0a 20 20 69  o bitmaps */.  i
2f24b 6e 74 20 69 46 69 72 73 74 2c 20 20 20 20 20 20  nt iFirst,      
2f24c 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 20 73           /* Be s
2f24d 65 61 72 63 68 69 6e 67 20 77 69 74 68 20 74 68  earching with th
2f24e 65 20 69 46 69 72 73 74 2d 74 68 20 65 78 70 72  e iFirst-th expr
2f24f 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ession */.  int 
2f250 69 42 61 73 65 20 20 20 20 20 20 20 20 20 20 20  iBase           
2f251 20 20 20 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20        /* Ignore 
2f252 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
2f253 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  is table */.){. 
2f254 20 42 69 74 6d 61 73 6b 20 61 6c 6c 6f 77 65 64   Bitmask allowed
2f255 20 3d 20 7e 67 65 74 4d 61 73 6b 28 70 4d 61 73   = ~getMask(pMas
2f256 6b 53 65 74 2c 20 69 42 61 73 65 29 3b 0a 20 20  kSet, iBase);.  
2f257 77 68 69 6c 65 28 20 69 46 69 72 73 74 3c 70 4c  while( iFirst<pL
2f258 69 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20  ist->nExpr ){.  
2f259 20 20 69 66 28 20 28 65 78 70 72 54 61 62 6c 65    if( (exprTable
2f25a 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2f25b 70 4c 69 73 74 2d 3e 61 5b 69 46 69 72 73 74 2b  pList->a[iFirst+
2f25c 2b 5d 2e 70 45 78 70 72 29 26 61 6c 6c 6f 77 65  +].pExpr)&allowe
2f25d 64 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  d)!=0 ){.      r
2f25e 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
2f25f 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
2f260 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  .../*.** This ro
2f261 75 74 69 6e 65 20 64 65 63 69 64 65 73 20 69 66  utine decides if
2f262 20 70 49 64 78 20 63 61 6e 20 62 65 20 75 73 65   pIdx can be use
2f263 64 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65  d to satisfy the
2f264 20 4f 52 44 45 52 20 42 59 0a 2a 2a 20 63 6c 61   ORDER BY.** cla
2f265 75 73 65 2e 20 20 49 66 20 69 74 20 63 61 6e 2c  use.  If it can,
2f266 20 69 74 20 72 65 74 75 72 6e 73 20 31 2e 20 20   it returns 1.  
2f267 49 66 20 70 49 64 78 20 63 61 6e 6e 6f 74 20 73  If pIdx cannot s
2f268 61 74 69 73 66 79 20 74 68 65 0a 2a 2a 20 4f 52  atisfy the.** OR
2f269 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74  DER BY clause, t
2f26a 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
2f26b 72 6e 73 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 4f 72  rns 0..**.** pOr
2f26c 64 65 72 42 79 20 69 73 20 61 6e 20 4f 52 44 45  derBy is an ORDE
2f26d 52 20 42 59 20 63 6c 61 75 73 65 20 66 72 6f 6d  R BY clause from
2f26e 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
2f26f 65 6e 74 2e 20 20 70 54 61 62 20 69 73 20 74 68  ent.  pTab is th
2f270 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74  e.** left-most t
2f271 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  able in the FROM
2f272 20 63 6c 61 75 73 65 20 6f 66 20 74 68 61 74 20   clause of that 
2f273 73 61 6d 65 20 53 45 4c 45 43 54 20 73 74 61 74  same SELECT stat
2f274 65 6d 65 6e 74 20 61 6e 64 0a 2a 2a 20 74 68 65  ement and.** the
2f275 20 74 61 62 6c 65 20 68 61 73 20 61 20 63 75 72   table has a cur
2f276 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 22 62  sor number of "b
2f277 61 73 65 22 2e 20 20 70 49 64 78 20 69 73 20 61  ase".  pIdx is a
2f278 6e 20 69 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e  n index on pTab.
2f279 0a 2a 2a 0a 2a 2a 20 6e 45 71 43 6f 6c 20 69 73  .**.** nEqCol is
2f27a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
2f27b 6f 6c 75 6d 6e 73 20 6f 66 20 70 49 64 78 20 74  olumns of pIdx t
2f27c 68 61 74 20 61 72 65 20 75 73 65 64 20 61 73 20  hat are used as 
2f27d 65 71 75 61 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73  equality.** cons
2f27e 74 72 61 69 6e 74 73 2e 20 20 41 6e 79 20 6f 66  traints.  Any of
2f27f 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e 73 20 6d   these columns m
2f280 61 79 20 62 65 20 6d 69 73 73 69 6e 67 20 66 72  ay be missing fr
2f281 6f 6d 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a  om the ORDER BY.
2f282 2a 2a 20 63 6c 61 75 73 65 20 61 6e 64 20 74 68  ** clause and th
2f283 65 20 6d 61 74 63 68 20 63 61 6e 20 73 74 69 6c  e match can stil
2f284 6c 20 62 65 20 61 20 73 75 63 63 65 73 73 2e 0a  l be a success..
2f285 2a 2a 0a 2a 2a 20 41 6c 6c 20 74 65 72 6d 73 20  **.** All terms 
2f286 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
2f287 74 68 61 74 20 6d 61 74 63 68 20 61 67 61 69 6e  that match again
2f288 73 74 20 74 68 65 20 69 6e 64 65 78 20 6d 75 73  st the index mus
2f289 74 20 62 65 20 65 69 74 68 65 72 0a 2a 2a 20 41  t be either.** A
2f28a 53 43 20 6f 72 20 44 45 53 43 2e 20 20 28 54 65  SC or DESC.  (Te
2f28b 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45 52  rms of the ORDER
2f28c 20 42 59 20 63 6c 61 75 73 65 20 70 61 73 74 20   BY clause past 
2f28d 74 68 65 20 65 6e 64 20 6f 66 20 61 20 55 4e 49  the end of a UNI
2f28e 51 55 45 0a 2a 2a 20 69 6e 64 65 78 20 64 6f 20  QUE.** index do 
2f28f 6e 6f 74 20 6e 65 65 64 20 74 6f 20 73 61 74 69  not need to sati
2f290 73 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61  sfy this constra
2f291 69 6e 74 2e 29 20 20 54 68 65 20 2a 70 62 52 65  int.)  The *pbRe
2f292 76 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65  v value is.** se
2f293 74 20 74 6f 20 31 20 69 66 20 74 68 65 20 4f 52  t to 1 if the OR
2f294 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73  DER BY clause is
2f295 20 61 6c 6c 20 44 45 53 43 20 61 6e 64 20 69 74   all DESC and it
2f296 20 69 73 20 73 65 74 20 74 6f 20 30 20 69 66 0a   is set to 0 if.
2f297 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ** the ORDER BY 
2f298 63 6c 61 75 73 65 20 69 73 20 61 6c 6c 20 41 53  clause is all AS
2f299 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  C..*/.static int
2f29a 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65 78 28   isSortingIndex(
2f29b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2f29c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
2f29d 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2f29e 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20  .  WhereMaskSet 
2f29f 2a 70 4d 61 73 6b 53 65 74 2c 20 2f 2a 20 4d 61  *pMaskSet, /* Ma
2f2a0 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65  pping from table
2f2a1 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20   cursor numbers 
2f2a2 74 6f 20 62 69 74 6d 61 70 73 20 2a 2f 0a 20 20  to bitmaps */.  
2f2a3 49 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 20 20  Index *pIdx,    
2f2a4 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69          /* The i
2f2a5 6e 64 65 78 20 77 65 20 61 72 65 20 74 65 73 74  ndex we are test
2f2a6 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73  ing */.  int bas
2f2a7 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
2f2a8 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65   /* Cursor numbe
2f2a9 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  r for the table 
2f2aa 74 6f 20 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a  to be sorted */.
2f2ab 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
2f2ac 65 72 42 79 2c 20 20 20 20 20 2f 2a 20 54 68 65  erBy,     /* The
2f2ad 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2f2ae 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 71 43 6f 6c   */.  int nEqCol
2f2af 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
2f2b0 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e 64 65 78   Number of index
2f2b1 20 63 6f 6c 75 6d 6e 73 20 77 69 74 68 20 3d 3d   columns with ==
2f2b2 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
2f2b3 20 20 69 6e 74 20 2a 70 62 52 65 76 20 20 20 20    int *pbRev    
2f2b4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74            /* Set
2f2b5 20 74 6f 20 31 20 69 66 20 4f 52 44 45 52 20 42   to 1 if ORDER B
2f2b6 59 20 69 73 20 44 45 53 43 20 2a 2f 0a 29 7b 0a  Y is DESC */.){.
2f2b7 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20    int i, j;     
2f2b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f2b9 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2f2ba 72 73 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74  rs */.  int sort
2f2bb 4f 72 64 65 72 20 3d 20 30 3b 20 20 20 20 20 20  Order = 0;      
2f2bc 20 20 20 20 20 20 20 20 2f 2a 20 58 4f 52 20 6f          /* XOR o
2f2bd 66 20 69 6e 64 65 78 20 61 6e 64 20 4f 52 44 45  f index and ORDE
2f2be 52 20 42 59 20 73 6f 72 74 20 64 69 72 65 63 74  R BY sort direct
2f2bf 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65  ion */.  int nTe
2f2c0 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rm;             
2f2c1 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2f2c2 65 72 20 6f 66 20 4f 52 44 45 52 20 42 59 20 74  er of ORDER BY t
2f2c3 65 72 6d 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  erms */.  struct
2f2c4 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
2f2c5 70 54 65 72 6d 3b 20 20 20 20 2f 2a 20 41 20 74  pTerm;    /* A t
2f2c6 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52  erm of the ORDER
2f2c7 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
2f2c8 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
2f2c9 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73  arse->db;..  ass
2f2ca 65 72 74 28 20 70 4f 72 64 65 72 42 79 21 3d 30  ert( pOrderBy!=0
2f2cb 20 29 3b 0a 20 20 6e 54 65 72 6d 20 3d 20 70 4f   );.  nTerm = pO
2f2cc 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 20  rderBy->nExpr;. 
2f2cd 20 61 73 73 65 72 74 28 20 6e 54 65 72 6d 3e 30   assert( nTerm>0
2f2ce 20 29 3b 0a 0a 20 20 2f 2a 20 41 72 67 75 6d 65   );..  /* Argume
2f2cf 6e 74 20 70 49 64 78 20 6d 75 73 74 20 65 69 74  nt pIdx must eit
2f2d0 68 65 72 20 70 6f 69 6e 74 20 74 6f 20 61 20 27  her point to a '
2f2d1 72 65 61 6c 27 20 6e 61 6d 65 64 20 69 6e 64 65  real' named inde
2f2d2 78 20 73 74 72 75 63 74 75 72 65 2c 20 0a 20 20  x structure, .  
2f2d3 2a 2a 20 6f 72 20 61 6e 20 69 6e 64 65 78 20 73  ** or an index s
2f2d4 74 72 75 63 74 75 72 65 20 61 6c 6c 6f 63 61 74  tructure allocat
2f2d5 65 64 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20  ed on the stack 
2f2d6 62 79 20 62 65 73 74 42 74 72 65 65 49 6e 64 65  by bestBtreeInde
2f2d7 78 28 29 20 74 6f 0a 20 20 2a 2a 20 72 65 70 72  x() to.  ** repr
2f2d8 65 73 65 6e 74 20 74 68 65 20 72 6f 77 69 64 20  esent the rowid 
2f2d9 69 6e 64 65 78 20 74 68 61 74 20 69 73 20 70 61  index that is pa
2f2da 72 74 20 6f 66 20 65 76 65 72 79 20 74 61 62 6c  rt of every tabl
2f2db 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  e.  */.  assert(
2f2dc 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20 7c 7c 20   pIdx->zName || 
2f2dd 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d  (pIdx->nColumn==
2f2de 31 20 26 26 20 70 49 64 78 2d 3e 61 69 43 6f 6c  1 && pIdx->aiCol
2f2df 75 6d 6e 5b 30 5d 3d 3d 2d 31 29 20 29 3b 0a 0a  umn[0]==-1) );..
2f2e0 20 20 2f 2a 20 4d 61 74 63 68 20 74 65 72 6d 73    /* Match terms
2f2e1 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2f2e2 20 63 6c 61 75 73 65 20 61 67 61 69 6e 73 74 20   clause against 
2f2e3 63 6f 6c 75 6d 6e 73 20 6f 66 0a 20 20 2a 2a 20  columns of.  ** 
2f2e4 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 2a 2a 0a  the index..  **.
2f2e5 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69    ** Note that i
2f2e6 6e 64 69 63 65 73 20 68 61 76 65 20 70 49 64 78  ndices have pIdx
2f2e7 2d 3e 6e 43 6f 6c 75 6d 6e 20 72 65 67 75 6c 61  ->nColumn regula
2f2e8 72 20 63 6f 6c 75 6d 6e 73 20 70 6c 75 73 0a 20  r columns plus. 
2f2e9 20 2a 2a 20 6f 6e 65 20 61 64 64 69 74 69 6f 6e   ** one addition
2f2ea 61 6c 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69  al column contai
2f2eb 6e 69 6e 67 20 74 68 65 20 72 6f 77 69 64 2e 20  ning the rowid. 
2f2ec 20 54 68 65 20 72 6f 77 69 64 20 63 6f 6c 75 6d   The rowid colum
2f2ed 6e 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 69 6e  n.  ** of the in
2f2ee 64 65 78 20 69 73 20 61 6c 73 6f 20 61 6c 6c 6f  dex is also allo
2f2ef 77 65 64 20 74 6f 20 6d 61 74 63 68 20 61 67 61  wed to match aga
2f2f0 69 6e 73 74 20 74 68 65 20 4f 52 44 45 52 20 42  inst the ORDER B
2f2f1 59 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e 0a 20  Y.  ** clause.. 
2f2f2 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 2c   */.  for(i=j=0,
2f2f3 20 70 54 65 72 6d 3d 70 4f 72 64 65 72 42 79 2d   pTerm=pOrderBy-
2f2f4 3e 61 3b 20 6a 3c 6e 54 65 72 6d 20 26 26 20 69  >a; j<nTerm && i
2f2f5 3c 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  <=pIdx->nColumn;
2f2f6 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20   i++){.    Expr 
2f2f7 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 2f 2a  *pExpr;       /*
2f2f8 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
2f2f9 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
2f2fa 70 54 65 72 6d 20 2a 2f 0a 20 20 20 20 43 6f 6c  pTerm */.    Col
2f2fb 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20  lSeq *pColl;    
2f2fc 2f 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e 67  /* The collating
2f2fd 20 73 65 71 75 65 6e 63 65 20 6f 66 20 70 45 78   sequence of pEx
2f2fe 70 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 74 65  pr */.    int te
2f2ff 72 6d 53 6f 72 74 4f 72 64 65 72 3b 20 2f 2a 20  rmSortOrder; /* 
2f300 53 6f 72 74 20 6f 72 64 65 72 20 66 6f 72 20 74  Sort order for t
2f301 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20  his term */.    
2f302 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
2f303 20 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20 63     /* The i-th c
2f304 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64  olumn of the ind
2f305 65 78 2e 20 20 2d 31 20 66 6f 72 20 72 6f 77 69  ex.  -1 for rowi
2f306 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f  d */.    int iSo
2f307 72 74 4f 72 64 65 72 3b 20 20 20 20 2f 2a 20 31  rtOrder;    /* 1
2f308 20 66 6f 72 20 44 45 53 43 2c 20 30 20 66 6f 72   for DESC, 0 for
2f309 20 41 53 43 20 6f 6e 20 74 68 65 20 69 2d 74 68   ASC on the i-th
2f30a 20 69 6e 64 65 78 20 74 65 72 6d 20 2a 2f 0a 20   index term */. 
2f30b 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2f30c 43 6f 6c 6c 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66  Coll; /* Name of
2f30d 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
2f30e 65 71 75 65 6e 63 65 20 66 6f 72 20 69 2d 74 68  equence for i-th
2f30f 20 69 6e 64 65 78 20 74 65 72 6d 20 2a 2f 0a 0a   index term */..
2f310 20 20 20 20 70 45 78 70 72 20 3d 20 70 54 65 72      pExpr = pTer
2f311 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69 66  m->pExpr;.    if
2f312 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
2f313 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d  COLUMN || pExpr-
2f314 3e 69 54 61 62 6c 65 21 3d 62 61 73 65 20 29 7b  >iTable!=base ){
2f315 0a 20 20 20 20 20 20 2f 2a 20 43 61 6e 20 6e 6f  .      /* Can no
2f316 74 20 75 73 65 20 61 6e 20 69 6e 64 65 78 20 73  t use an index s
2f317 6f 72 74 20 6f 6e 20 61 6e 79 74 68 69 6e 67 20  ort on anything 
2f318 74 68 61 74 20 69 73 20 6e 6f 74 20 61 20 63 6f  that is not a co
2f319 6c 75 6d 6e 20 69 6e 20 74 68 65 0a 20 20 20 20  lumn in the.    
2f31a 20 20 2a 2a 20 6c 65 66 74 2d 6d 6f 73 74 20 74    ** left-most t
2f31b 61 62 6c 65 20 6f 66 20 74 68 65 20 46 52 4f 4d  able of the FROM
2f31c 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20   clause */.     
2f31d 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2f31e 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
2f31f 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
2f320 72 73 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20  rse, pExpr);.   
2f321 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20   if( !pColl ){. 
2f322 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d       pColl = db-
2f323 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20  >pDfltColl;.    
2f324 7d 0a 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e  }.    if( pIdx->
2f325 7a 4e 61 6d 65 20 26 26 20 69 3c 70 49 64 78 2d  zName && i<pIdx-
2f326 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  >nColumn ){.    
2f327 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 49 64 78    iColumn = pIdx
2f328 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20  ->aiColumn[i];. 
2f329 20 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e       if( iColumn
2f32a 3d 3d 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e  ==pIdx->pTable->
2f32b 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  iPKey ){.       
2f32c 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20   iColumn = -1;. 
2f32d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 53 6f       }.      iSo
2f32e 72 74 4f 72 64 65 72 20 3d 20 70 49 64 78 2d 3e  rtOrder = pIdx->
2f32f 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20  aSortOrder[i];. 
2f330 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 49 64       zColl = pId
2f331 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20  x->azColl[i];.  
2f332 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
2f333 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20  Column = -1;.   
2f334 20 20 20 69 53 6f 72 74 4f 72 64 65 72 20 3d 20     iSortOrder = 
2f335 30 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d  0;.      zColl =
2f336 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20   pColl->zName;. 
2f337 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 45 78     }.    if( pEx
2f338 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 69 43 6f  pr->iColumn!=iCo
2f339 6c 75 6d 6e 20 7c 7c 20 73 71 6c 69 74 65 33 53  lumn || sqlite3S
2f33a 74 72 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a 4e  trICmp(pColl->zN
2f33b 61 6d 65 2c 20 7a 43 6f 6c 6c 29 20 29 7b 0a 20  ame, zColl) ){. 
2f33c 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 6a 20 6f       /* Term j o
2f33d 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
2f33e 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d  lause does not m
2f33f 61 74 63 68 20 63 6f 6c 75 6d 6e 20 69 20 6f 66  atch column i of
2f340 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20   the index */.  
2f341 20 20 20 20 69 66 28 20 69 3c 6e 45 71 43 6f 6c      if( i<nEqCol
2f342 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49   ){.        /* I
2f343 66 20 61 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d  f an index colum
2f344 6e 20 74 68 61 74 20 69 73 20 63 6f 6e 73 74 72  n that is constr
2f345 61 69 6e 65 64 20 62 79 20 3d 3d 20 66 61 69 6c  ained by == fail
2f346 73 20 74 6f 20 6d 61 74 63 68 20 61 6e 0a 20 20  s to match an.  
2f347 20 20 20 20 20 20 2a 2a 20 4f 52 44 45 52 20 42        ** ORDER B
2f348 59 20 74 65 72 6d 2c 20 74 68 61 74 20 69 73 20  Y term, that is 
2f349 4f 4b 2e 20 20 4a 75 73 74 20 69 67 6e 6f 72 65  OK.  Just ignore
2f34a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 6f 66 20   that column of 
2f34b 74 68 65 20 69 6e 64 65 78 0a 20 20 20 20 20 20  the index.      
2f34c 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 6f 6e    */.        con
2f34d 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 65 6c  tinue;.      }el
2f34e 73 65 20 69 66 28 20 69 3d 3d 70 49 64 78 2d 3e  se if( i==pIdx->
2f34f 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20  nColumn ){.     
2f350 20 20 20 2f 2a 20 49 6e 64 65 78 20 63 6f 6c 75     /* Index colu
2f351 6d 6e 20 69 20 69 73 20 74 68 65 20 72 6f 77 69  mn i is the rowi
2f352 64 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 74 65  d.  All other te
2f353 72 6d 73 20 6d 61 74 63 68 2e 20 2a 2f 0a 20 20  rms match. */.  
2f354 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2f355 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2f356 20 20 2f 2a 20 49 66 20 61 6e 20 69 6e 64 65 78    /* If an index
2f357 20 63 6f 6c 75 6d 6e 20 66 61 69 6c 73 20 74 6f   column fails to
2f358 20 6d 61 74 63 68 20 61 6e 64 20 69 73 20 6e 6f   match and is no
2f359 74 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 62 79  t constrained by
2f35a 20 3d 3d 0a 20 20 20 20 20 20 20 20 2a 2a 20 74   ==.        ** t
2f35b 68 65 6e 20 74 68 65 20 69 6e 64 65 78 20 63 61  hen the index ca
2f35c 6e 6e 6f 74 20 73 61 74 69 73 66 79 20 74 68 65  nnot satisfy the
2f35d 20 4f 52 44 45 52 20 42 59 20 63 6f 6e 73 74 72   ORDER BY constr
2f35e 61 69 6e 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f  aint..        */
2f35f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2f360 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
2f361 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49 64  .    assert( pId
2f362 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 21 3d 30  x->aSortOrder!=0
2f363 20 7c 7c 20 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20   || iColumn==-1 
2f364 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2f365 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3d  Term->sortOrder=
2f366 3d 30 20 7c 7c 20 70 54 65 72 6d 2d 3e 73 6f 72  =0 || pTerm->sor
2f367 74 4f 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20  tOrder==1 );.   
2f368 20 61 73 73 65 72 74 28 20 69 53 6f 72 74 4f 72   assert( iSortOr
2f369 64 65 72 3d 3d 30 20 7c 7c 20 69 53 6f 72 74 4f  der==0 || iSortO
2f36a 72 64 65 72 3d 3d 31 20 29 3b 0a 20 20 20 20 74  rder==1 );.    t
2f36b 65 72 6d 53 6f 72 74 4f 72 64 65 72 20 3d 20 69  ermSortOrder = i
2f36c 53 6f 72 74 4f 72 64 65 72 20 5e 20 70 54 65 72  SortOrder ^ pTer
2f36d 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20  m->sortOrder;.  
2f36e 20 20 69 66 28 20 69 3e 6e 45 71 43 6f 6c 20 29    if( i>nEqCol )
2f36f 7b 0a 20 20 20 20 20 20 69 66 28 20 74 65 72 6d  {.      if( term
2f370 53 6f 72 74 4f 72 64 65 72 21 3d 73 6f 72 74 4f  SortOrder!=sortO
2f371 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20  rder ){.        
2f372 2f 2a 20 49 6e 64 69 63 65 73 20 63 61 6e 20 6f  /* Indices can o
2f373 6e 6c 79 20 62 65 20 75 73 65 64 20 69 66 20 61  nly be used if a
2f374 6c 6c 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  ll ORDER BY term
2f375 73 20 70 61 73 74 20 74 68 65 0a 20 20 20 20 20  s past the.     
2f376 20 20 20 2a 2a 20 65 71 75 61 6c 69 74 79 20 63     ** equality c
2f377 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 61  onstraints are a
2f378 6c 6c 20 65 69 74 68 65 72 20 44 45 53 43 20 6f  ll either DESC o
2f379 72 20 41 53 43 2e 20 2a 2f 0a 20 20 20 20 20 20  r ASC. */.      
2f37a 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
2f37b 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
2f37c 20 20 20 20 20 73 6f 72 74 4f 72 64 65 72 20 3d       sortOrder =
2f37d 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 3b 0a   termSortOrder;.
2f37e 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b 3b 0a 20      }.    j++;. 
2f37f 20 20 20 70 54 65 72 6d 2b 2b 3b 0a 20 20 20 20     pTerm++;.    
2f380 69 66 28 20 69 43 6f 6c 75 6d 6e 3c 30 20 26 26  if( iColumn<0 &&
2f381 20 21 72 65 66 65 72 65 6e 63 65 73 4f 74 68 65   !referencesOthe
2f382 72 54 61 62 6c 65 73 28 70 4f 72 64 65 72 42 79  rTables(pOrderBy
2f383 2c 20 70 4d 61 73 6b 53 65 74 2c 20 6a 2c 20 62  , pMaskSet, j, b
2f384 61 73 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  ase) ){.      /*
2f385 20 49 66 20 74 68 65 20 69 6e 64 65 78 65 64 20   If the indexed 
2f386 63 6f 6c 75 6d 6e 20 69 73 20 74 68 65 20 70 72  column is the pr
2f387 69 6d 61 72 79 20 6b 65 79 20 61 6e 64 20 65 76  imary key and ev
2f388 65 72 79 74 68 69 6e 67 20 6d 61 74 63 68 65 73  erything matches
2f389 0a 20 20 20 20 20 20 2a 2a 20 73 6f 20 66 61 72  .      ** so far
2f38a 20 61 6e 64 20 6e 6f 6e 65 20 6f 66 20 74 68 65   and none of the
2f38b 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20   ORDER BY terms 
2f38c 74 6f 20 74 68 65 20 72 69 67 68 74 20 72 65 66  to the right ref
2f38d 65 72 65 6e 63 65 20 6f 74 68 65 72 0a 20 20 20  erence other.   
2f38e 20 20 20 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20     ** tables in 
2f38f 74 68 65 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 77  the join, then w
2f390 65 20 61 72 65 20 61 73 73 75 72 65 64 20 74 68  e are assured th
2f391 61 74 20 74 68 65 20 69 6e 64 65 78 20 63 61 6e  at the index can
2f392 20 62 65 20 75 73 65 64 20 0a 20 20 20 20 20 20   be used .      
2f393 2a 2a 20 74 6f 20 73 6f 72 74 20 62 65 63 61 75  ** to sort becau
2f394 73 65 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  se the primary k
2f395 65 79 20 69 73 20 75 6e 69 71 75 65 20 61 6e 64  ey is unique and
2f396 20 73 6f 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20   so none of the 
2f397 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 63  other.      ** c
2f398 6f 6c 75 6d 6e 73 20 77 69 6c 6c 20 6d 61 6b 65  olumns will make
2f399 20 61 6e 79 20 64 69 66 66 65 72 65 6e 63 65 0a   any difference.
2f39a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6a        */.      j
2f39b 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20 7d 0a   = nTerm;.    }.
2f39c 20 20 7d 0a 0a 20 20 2a 70 62 52 65 76 20 3d 20    }..  *pbRev = 
2f39d 73 6f 72 74 4f 72 64 65 72 21 3d 30 3b 0a 20 20  sortOrder!=0;.  
2f39e 69 66 28 20 6a 3e 3d 6e 54 65 72 6d 20 29 7b 0a  if( j>=nTerm ){.
2f39f 20 20 20 20 2f 2a 20 41 6c 6c 20 74 65 72 6d 73      /* All terms
2f3a0 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
2f3a1 20 63 6c 61 75 73 65 20 61 72 65 20 63 6f 76 65   clause are cove
2f3a2 72 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65  red by this inde
2f3a3 78 20 73 6f 0a 20 20 20 20 2a 2a 20 74 68 69 73  x so.    ** this
2f3a4 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73   index can be us
2f3a5 65 64 20 66 6f 72 20 73 6f 72 74 69 6e 67 2e 20  ed for sorting. 
2f3a6 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  */.    return 1;
2f3a7 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78 2d  .  }.  if( pIdx-
2f3a8 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
2f3a9 65 20 26 26 20 69 3d 3d 70 49 64 78 2d 3e 6e 43  e && i==pIdx->nC
2f3aa 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 26 26 20 21  olumn.      && !
2f3ab 72 65 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54  referencesOtherT
2f3ac 61 62 6c 65 73 28 70 4f 72 64 65 72 42 79 2c 20  ables(pOrderBy, 
2f3ad 70 4d 61 73 6b 53 65 74 2c 20 6a 2c 20 62 61 73  pMaskSet, j, bas
2f3ae 65 29 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c  e) ){.    /* All
2f3af 20 74 65 72 6d 73 20 6f 66 20 74 68 69 73 20 69   terms of this i
2f3b0 6e 64 65 78 20 6d 61 74 63 68 20 73 6f 6d 65 20  ndex match some 
2f3b1 70 72 65 66 69 78 20 6f 66 20 74 68 65 20 4f 52  prefix of the OR
2f3b2 44 45 52 20 42 59 20 63 6c 61 75 73 65 0a 20 20  DER BY clause.  
2f3b3 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64    ** and the ind
2f3b4 65 78 20 69 73 20 55 4e 49 51 55 45 20 61 6e 64  ex is UNIQUE and
2f3b5 20 6e 6f 20 74 65 72 6d 73 20 6f 6e 20 74 68 65   no terms on the
2f3b6 20 74 61 69 6c 20 6f 66 20 74 68 65 20 4f 52 44   tail of the ORD
2f3b7 45 52 20 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61  ER BY.    ** cla
2f3b8 75 73 65 20 72 65 66 65 72 65 6e 63 65 20 6f 74  use reference ot
2f3b9 68 65 72 20 74 61 62 6c 65 73 20 69 6e 20 61 20  her tables in a 
2f3ba 6a 6f 69 6e 2e 20 20 49 66 20 74 68 69 73 20 69  join.  If this i
2f3bb 73 20 61 6c 6c 20 74 72 75 65 20 74 68 65 6e 0a  s all true then.
2f3bc 20 20 20 20 2a 2a 20 74 68 65 20 6f 72 64 65 72      ** the order
2f3bd 20 62 79 20 63 6c 61 75 73 65 20 69 73 20 73 75   by clause is su
2f3be 70 65 72 66 6c 75 6f 75 73 2e 20 2a 2f 0a 20 20  perfluous. */.  
2f3bf 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
2f3c0 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
2f3c1 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 61 20 63  *.** Prepare a c
2f3c2 72 75 64 65 20 65 73 74 69 6d 61 74 65 20 6f 66  rude estimate of
2f3c3 20 74 68 65 20 6c 6f 67 61 72 69 74 68 6d 20 6f   the logarithm o
2f3c4 66 20 74 68 65 20 69 6e 70 75 74 20 76 61 6c 75  f the input valu
2f3c5 65 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  e..** The result
2f3c6 73 20 6e 65 65 64 20 6e 6f 74 20 62 65 20 65 78  s need not be ex
2f3c7 61 63 74 2e 20 20 54 68 69 73 20 69 73 20 6f 6e  act.  This is on
2f3c8 6c 79 20 75 73 65 64 20 66 6f 72 20 65 73 74 69  ly used for esti
2f3c9 6d 61 74 69 6e 67 0a 2a 2a 20 74 68 65 20 74 6f  mating.** the to
2f3ca 74 61 6c 20 63 6f 73 74 20 6f 66 20 70 65 72 66  tal cost of perf
2f3cb 6f 72 6d 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  orming operation
2f3cc 73 20 77 69 74 68 20 4f 28 6c 6f 67 4e 29 20 6f  s with O(logN) o
2f3cd 72 20 4f 28 4e 6c 6f 67 4e 29 0a 2a 2a 20 63 6f  r O(NlogN).** co
2f3ce 6d 70 6c 65 78 69 74 79 2e 20 20 42 65 63 61 75  mplexity.  Becau
2f3cf 73 65 20 4e 20 69 73 20 6a 75 73 74 20 61 20 67  se N is just a g
2f3d0 75 65 73 73 2c 20 69 74 20 69 73 20 6e 6f 20 67  uess, it is no g
2f3d1 72 65 61 74 20 74 72 61 67 65 64 79 20 69 66 0a  reat tragedy if.
2f3d2 2a 2a 20 6c 6f 67 4e 20 69 73 20 61 20 6c 69 74  ** logN is a lit
2f3d3 74 6c 65 20 6f 66 66 2e 0a 2a 2f 0a 73 74 61 74  tle off..*/.stat
2f3d4 69 63 20 64 6f 75 62 6c 65 20 65 73 74 4c 6f 67  ic double estLog
2f3d5 28 64 6f 75 62 6c 65 20 4e 29 7b 0a 20 20 64 6f  (double N){.  do
2f3d6 75 62 6c 65 20 6c 6f 67 4e 20 3d 20 31 3b 0a 20  uble logN = 1;. 
2f3d7 20 64 6f 75 62 6c 65 20 78 20 3d 20 31 30 3b 0a   double x = 10;.
2f3d8 20 20 77 68 69 6c 65 28 20 4e 3e 78 20 29 7b 0a    while( N>x ){.
2f3d9 20 20 20 20 6c 6f 67 4e 20 2b 3d 20 31 3b 0a 20      logN += 1;. 
2f3da 20 20 20 78 20 2a 3d 20 31 30 3b 0a 20 20 7d 0a     x *= 10;.  }.
2f3db 20 20 72 65 74 75 72 6e 20 6c 6f 67 4e 3b 0a 7d    return logN;.}
2f3dc 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 72 6f 75 74  ../*.** Two rout
2f3dd 69 6e 65 73 20 66 6f 72 20 70 72 69 6e 74 69 6e  ines for printin
2f3de 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  g the content of
2f3df 20 61 6e 20 73 71 6c 69 74 65 33 5f 69 6e 64 65   an sqlite3_inde
2f3e0 78 5f 69 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74  x_info.** struct
2f3e1 75 72 65 2e 20 20 55 73 65 64 20 66 6f 72 20 74  ure.  Used for t
2f3e2 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
2f3e3 67 69 6e 67 20 6f 6e 6c 79 2e 20 20 49 66 20 6e  ging only.  If n
2f3e4 65 69 74 68 65 72 0a 2a 2a 20 53 51 4c 49 54 45  either.** SQLITE
2f3e5 5f 54 45 53 54 20 6f 72 20 53 51 4c 49 54 45 5f  _TEST or SQLITE_
2f3e6 44 45 42 55 47 20 61 72 65 20 64 65 66 69 6e 65  DEBUG are define
2f3e7 64 2c 20 74 68 65 6e 20 74 68 65 73 65 20 72 6f  d, then these ro
2f3e8 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 6e 6f  utines.** are no
2f3e9 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  -ops..*/.#if !de
2f3ea 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2f3eb 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 20  T_VIRTUALTABLE) 
2f3ec 26 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  && defined(SQLIT
2f3ed 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63 20  E_DEBUG).static 
2f3ee 76 6f 69 64 20 54 52 41 43 45 5f 49 44 58 5f 49  void TRACE_IDX_I
2f3ef 4e 50 55 54 53 28 73 71 6c 69 74 65 33 5f 69 6e  NPUTS(sqlite3_in
2f3f0 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20  dex_info *p){.  
2f3f1 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71  int i;.  if( !sq
2f3f2 6c 69 74 65 33 57 68 65 72 65 54 72 61 63 65 20  lite3WhereTrace 
2f3f3 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28  ) return;.  for(
2f3f4 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74  i=0; i<p->nConst
2f3f5 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20  raint; i++){.   
2f3f6 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2f3f7 6e 74 66 28 22 20 20 63 6f 6e 73 74 72 61 69 6e  ntf("  constrain
2f3f8 74 5b 25 64 5d 3a 20 63 6f 6c 3d 25 64 20 74 65  t[%d]: col=%d te
2f3f9 72 6d 69 64 3d 25 64 20 6f 70 3d 25 64 20 75 73  rmid=%d op=%d us
2f3fa 61 62 6c 65 64 3d 25 64 5c 6e 22 2c 0a 20 20 20  abled=%d\n",.   
2f3fb 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20 70 2d      i,.       p-
2f3fc 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e  >aConstraint[i].
2f3fd 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20  iColumn,.       
2f3fe 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69  p->aConstraint[i
2f3ff 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 2c 0a 20  ].iTermOffset,. 
2f400 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72        p->aConstr
2f401 61 69 6e 74 5b 69 5d 2e 6f 70 2c 0a 20 20 20 20  aint[i].op,.    
2f402 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e     p->aConstrain
2f403 74 5b 69 5d 2e 75 73 61 62 6c 65 29 3b 0a 20 20  t[i].usable);.  
2f404 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  }.  for(i=0; i<p
2f405 2d 3e 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29  ->nOrderBy; i++)
2f406 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
2f407 75 67 50 72 69 6e 74 66 28 22 20 20 6f 72 64 65  ugPrintf("  orde
2f408 72 62 79 5b 25 64 5d 3a 20 63 6f 6c 3d 25 64 20  rby[%d]: col=%d 
2f409 64 65 73 63 3d 25 64 5c 6e 22 2c 0a 20 20 20 20  desc=%d\n",.    
2f40a 20 20 20 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e     i,.       p->
2f40b 61 4f 72 64 65 72 42 79 5b 69 5d 2e 69 43 6f 6c  aOrderBy[i].iCol
2f40c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61  umn,.       p->a
2f40d 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65 73 63 29  OrderBy[i].desc)
2f40e 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  ;.  }.}.static v
2f40f 6f 69 64 20 54 52 41 43 45 5f 49 44 58 5f 4f 55  oid TRACE_IDX_OU
2f410 54 50 55 54 53 28 73 71 6c 69 74 65 33 5f 69 6e  TPUTS(sqlite3_in
2f411 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20  dex_info *p){.  
2f412 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 73 71  int i;.  if( !sq
2f413 6c 69 74 65 33 57 68 65 72 65 54 72 61 63 65 20  lite3WhereTrace 
2f414 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28  ) return;.  for(
2f415 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6e 73 74  i=0; i<p->nConst
2f416 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20  raint; i++){.   
2f417 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2f418 6e 74 66 28 22 20 20 75 73 61 67 65 5b 25 64 5d  ntf("  usage[%d]
2f419 3a 20 61 72 67 76 49 64 78 3d 25 64 20 6f 6d 69  : argvIdx=%d omi
2f41a 74 3d 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20  t=%d\n",.       
2f41b 69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f  i,.       p->aCo
2f41c 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d  nstraintUsage[i]
2f41d 2e 61 72 67 76 49 6e 64 65 78 2c 0a 20 20 20 20  .argvIndex,.    
2f41e 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e     p->aConstrain
2f41f 74 55 73 61 67 65 5b 69 5d 2e 6f 6d 69 74 29 3b  tUsage[i].omit);
2f420 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65  .  }.  sqlite3De
2f421 62 75 67 50 72 69 6e 74 66 28 22 20 20 69 64 78  bugPrintf("  idx
2f422 4e 75 6d 3d 25 64 5c 6e 22 2c 20 70 2d 3e 69 64  Num=%d\n", p->id
2f423 78 4e 75 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33  xNum);.  sqlite3
2f424 44 65 62 75 67 50 72 69 6e 74 66 28 22 20 20 69  DebugPrintf("  i
2f425 64 78 53 74 72 3d 25 73 5c 6e 22 2c 20 70 2d 3e  dxStr=%s\n", p->
2f426 69 64 78 53 74 72 29 3b 0a 20 20 73 71 6c 69 74  idxStr);.  sqlit
2f427 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
2f428 20 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64   orderByConsumed
2f429 3d 25 64 5c 6e 22 2c 20 70 2d 3e 6f 72 64 65 72  =%d\n", p->order
2f42a 42 79 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 73  ByConsumed);.  s
2f42b 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2f42c 66 28 22 20 20 65 73 74 69 6d 61 74 65 64 43 6f  f("  estimatedCo
2f42d 73 74 3d 25 67 5c 6e 22 2c 20 70 2d 3e 65 73 74  st=%g\n", p->est
2f42e 69 6d 61 74 65 64 43 6f 73 74 29 3b 0a 7d 0a 23  imatedCost);.}.#
2f42f 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 52 41  else.#define TRA
2f430 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28 41 29  CE_IDX_INPUTS(A)
2f431 0a 23 64 65 66 69 6e 65 20 54 52 41 43 45 5f 49  .#define TRACE_I
2f432 44 58 5f 4f 55 54 50 55 54 53 28 41 29 0a 23 65  DX_OUTPUTS(A).#e
2f433 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 71  ndif../* .** Req
2f434 75 69 72 65 64 20 62 65 63 61 75 73 65 20 62 65  uired because be
2f435 73 74 49 6e 64 65 78 28 29 20 69 73 20 63 61 6c  stIndex() is cal
2f436 6c 65 64 20 62 79 20 62 65 73 74 4f 72 43 6c 61  led by bestOrCla
2f437 75 73 65 49 6e 64 65 78 28 29 20 0a 2a 2f 0a 73  useIndex() .*/.s
2f438 74 61 74 69 63 20 76 6f 69 64 20 62 65 73 74 49  tatic void bestI
2f439 6e 64 65 78 28 0a 20 20 20 20 50 61 72 73 65 2a  ndex(.    Parse*
2f43a 2c 20 57 68 65 72 65 43 6c 61 75 73 65 2a 2c 20  , WhereClause*, 
2f43b 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2f43c 74 65 6d 2a 2c 20 42 69 74 6d 61 73 6b 2c 20 45  tem*, Bitmask, E
2f43d 78 70 72 4c 69 73 74 2a 2c 20 57 68 65 72 65 43  xprList*, WhereC
2f43e 6f 73 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  ost*);../*.** Th
2f43f 69 73 20 72 6f 75 74 69 6e 65 20 61 74 74 65 6d  is routine attem
2f440 70 74 73 20 74 6f 20 66 69 6e 64 20 61 6e 20 73  pts to find an s
2f441 63 61 6e 6e 69 6e 67 20 73 74 72 61 74 65 67 79  canning strategy
2f442 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
2f443 64 20 0a 2a 2a 20 74 6f 20 6f 70 74 69 6d 69 7a  d .** to optimiz
2f444 65 20 61 6e 20 27 4f 52 27 20 65 78 70 72 65 73  e an 'OR' expres
2f445 73 69 6f 6e 20 74 68 61 74 20 69 73 20 70 61 72  sion that is par
2f446 74 20 6f 66 20 61 20 57 48 45 52 45 20 63 6c 61  t of a WHERE cla
2f447 75 73 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20  use. .**.** The 
2f448 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64  table associated
2f449 20 77 69 74 68 20 46 52 4f 4d 20 63 6c 61 75 73   with FROM claus
2f44a 65 20 74 65 72 6d 20 70 53 72 63 20 6d 61 79 20  e term pSrc may 
2f44b 62 65 20 65 69 74 68 65 72 20 61 0a 2a 2a 20 72  be either a.** r
2f44c 65 67 75 6c 61 72 20 42 2d 54 72 65 65 20 74 61  egular B-Tree ta
2f44d 62 6c 65 20 6f 72 20 61 20 76 69 72 74 75 61 6c  ble or a virtual
2f44e 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69   table..*/.stati
2f44f 63 20 76 6f 69 64 20 62 65 73 74 4f 72 43 6c 61  c void bestOrCla
2f450 75 73 65 49 6e 64 65 78 28 0a 20 20 50 61 72 73  useIndex(.  Pars
2f451 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2f452 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2f453 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2f454 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
2f455 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20 20 20  *pWC,           
2f456 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2f457 75 73 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  use */.  struct 
2f458 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53  SrcList_item *pS
2f459 72 63 2c 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d  rc,  /* The FROM
2f45a 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20   clause term to 
2f45b 73 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d  search */.  Bitm
2f45c 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20  ask notReady,   
2f45d 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
2f45e 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  of cursors that 
2f45f 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  are not availabl
2f460 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
2f461 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20  *pOrderBy,      
2f462 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
2f463 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57  BY clause */.  W
2f464 68 65 72 65 43 6f 73 74 20 2a 70 43 6f 73 74 20  hereCost *pCost 
2f465 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2f466 77 65 73 74 20 63 6f 73 74 20 71 75 65 72 79 20  west cost query 
2f467 70 6c 61 6e 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64  plan */.){.#ifnd
2f468 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f  ef SQLITE_OMIT_O
2f469 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 20  R_OPTIMIZATION. 
2f46a 20 63 6f 6e 73 74 20 69 6e 74 20 69 43 75 72 20   const int iCur 
2f46b 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 3b  = pSrc->iCursor;
2f46c 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72     /* The cursor
2f46d 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
2f46e 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a   be accessed */.
2f46f 20 20 63 6f 6e 73 74 20 42 69 74 6d 61 73 6b 20    const Bitmask 
2f470 6d 61 73 6b 53 72 63 20 3d 20 67 65 74 4d 61 73  maskSrc = getMas
2f471 6b 28 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 2c  k(pWC->pMaskSet,
2f472 20 69 43 75 72 29 3b 20 20 2f 2a 20 42 69 74 6d   iCur);  /* Bitm
2f473 61 73 6b 20 66 6f 72 20 70 53 72 63 20 2a 2f 0a  ask for pSrc */.
2f474 20 20 57 68 65 72 65 54 65 72 6d 20 2a 20 63 6f    WhereTerm * co
2f475 6e 73 74 20 70 57 43 45 6e 64 20 3d 20 26 70 57  nst pWCEnd = &pW
2f476 43 2d 3e 61 5b 70 57 43 2d 3e 6e 54 65 72 6d 5d  C->a[pWC->nTerm]
2f477 3b 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20  ;        /* End 
2f478 6f 66 20 70 57 43 2d 3e 61 5b 5d 20 2a 2f 0a 20  of pWC->a[] */. 
2f479 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2f47a 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m;              
2f47b 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74     /* A single t
2f47c 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45  erm of the WHERE
2f47d 20 63 6c 61 75 73 65 20 2a 2f 0a 0a 20 20 2f 2a   clause */..  /*
2f47e 20 53 65 61 72 63 68 20 74 68 65 20 57 48 45 52   Search the WHER
2f47f 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 66  E clause terms f
2f480 6f 72 20 61 20 75 73 61 62 6c 65 20 57 4f 5f 4f  or a usable WO_O
2f481 52 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 66 6f 72  R term. */.  for
2f482 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20 70  (pTerm=pWC->a; p
2f483 54 65 72 6d 3c 70 57 43 45 6e 64 3b 20 70 54 65  Term<pWCEnd; pTe
2f484 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70  rm++){.    if( p
2f485 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d  Term->eOperator=
2f486 3d 57 4f 5f 4f 52 20 0a 20 20 20 20 20 26 26 20  =WO_OR .     && 
2f487 28 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41  ((pTerm->prereqA
2f488 6c 6c 20 26 20 7e 6d 61 73 6b 53 72 63 29 20 26  ll & ~maskSrc) &
2f489 20 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a 20 20   notReady)==0.  
2f48a 20 20 20 26 26 20 28 70 54 65 72 6d 2d 3e 75 2e     && (pTerm->u.
2f48b 70 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62  pOrInfo->indexab
2f48c 6c 65 20 26 20 6d 61 73 6b 53 72 63 29 21 3d 30  le & maskSrc)!=0
2f48d 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 57   .    ){.      W
2f48e 68 65 72 65 43 6c 61 75 73 65 20 2a 20 63 6f 6e  hereClause * con
2f48f 73 74 20 70 4f 72 57 43 20 3d 20 26 70 54 65 72  st pOrWC = &pTer
2f490 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 77 63  m->u.pOrInfo->wc
2f491 3b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72  ;.      WhereTer
2f492 6d 20 2a 20 63 6f 6e 73 74 20 70 4f 72 57 43 45  m * const pOrWCE
2f493 6e 64 20 3d 20 26 70 4f 72 57 43 2d 3e 61 5b 70  nd = &pOrWC->a[p
2f494 4f 72 57 43 2d 3e 6e 54 65 72 6d 5d 3b 0a 20 20  OrWC->nTerm];.  
2f495 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
2f496 4f 72 54 65 72 6d 3b 0a 20 20 20 20 20 20 69 6e  OrTerm;.      in
2f497 74 20 66 6c 61 67 73 20 3d 20 57 48 45 52 45 5f  t flags = WHERE_
2f498 4d 55 4c 54 49 5f 4f 52 3b 0a 20 20 20 20 20 20  MULTI_OR;.      
2f499 64 6f 75 62 6c 65 20 72 54 6f 74 61 6c 20 3d 20  double rTotal = 
2f49a 30 3b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  0;.      double 
2f49b 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20 20 20  nRow = 0;.      
2f49c 42 69 74 6d 61 73 6b 20 75 73 65 64 20 3d 20 30  Bitmask used = 0
2f49d 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28 70 4f 72  ;..      for(pOr
2f49e 54 65 72 6d 3d 70 4f 72 57 43 2d 3e 61 3b 20 70  Term=pOrWC->a; p
2f49f 4f 72 54 65 72 6d 3c 70 4f 72 57 43 45 6e 64 3b  OrTerm<pOrWCEnd;
2f4a0 20 70 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20   pOrTerm++){.   
2f4a1 20 20 20 20 20 57 68 65 72 65 43 6f 73 74 20 73       WhereCost s
2f4a2 54 65 72 6d 43 6f 73 74 3b 0a 20 20 20 20 20 20  TermCost;.      
2f4a3 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2e    WHERETRACE((".
2f4a4 2e 2e 20 4d 75 6c 74 69 2d 69 6e 64 65 78 20 4f  .. Multi-index O
2f4a5 52 20 74 65 73 74 69 6e 67 20 66 6f 72 20 74 65  R testing for te
2f4a6 72 6d 20 25 64 20 6f 66 20 25 64 2e 2e 2e 2e 5c  rm %d of %d....\
2f4a7 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20 28  n", .          (
2f4a8 70 4f 72 54 65 72 6d 20 2d 20 70 4f 72 57 43 2d  pOrTerm - pOrWC-
2f4a9 3e 61 29 2c 20 28 70 54 65 72 6d 20 2d 20 70 57  >a), (pTerm - pW
2f4aa 43 2d 3e 61 29 0a 20 20 20 20 20 20 20 20 29 29  C->a).        ))
2f4ab 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f  ;.        if( pO
2f4ac 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  rTerm->eOperator
2f4ad 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20 20 20 20  ==WO_AND ){.    
2f4ae 20 20 20 20 20 20 57 68 65 72 65 43 6c 61 75 73        WhereClaus
2f4af 65 20 2a 70 41 6e 64 57 43 20 3d 20 26 70 4f 72  e *pAndWC = &pOr
2f4b0 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f  Term->u.pAndInfo
2f4b1 2d 3e 77 63 3b 0a 20 20 20 20 20 20 20 20 20 20  ->wc;.          
2f4b2 62 65 73 74 49 6e 64 65 78 28 70 50 61 72 73 65  bestIndex(pParse
2f4b3 2c 20 70 41 6e 64 57 43 2c 20 70 53 72 63 2c 20  , pAndWC, pSrc, 
2f4b4 6e 6f 74 52 65 61 64 79 2c 20 30 2c 20 26 73 54  notReady, 0, &sT
2f4b5 65 72 6d 43 6f 73 74 29 3b 0a 20 20 20 20 20 20  ermCost);.      
2f4b6 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 72 54    }else if( pOrT
2f4b7 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d  erm->leftCursor=
2f4b8 3d 69 43 75 72 20 29 7b 0a 20 20 20 20 20 20 20  =iCur ){.       
2f4b9 20 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 74     WhereClause t
2f4ba 65 6d 70 57 43 3b 0a 20 20 20 20 20 20 20 20 20  empWC;.         
2f4bb 20 74 65 6d 70 57 43 2e 70 50 61 72 73 65 20 3d   tempWC.pParse =
2f4bc 20 70 57 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20   pWC->pParse;.  
2f4bd 20 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e 70          tempWC.p
2f4be 4d 61 73 6b 53 65 74 20 3d 20 70 57 43 2d 3e 70  MaskSet = pWC->p
2f4bf 4d 61 73 6b 53 65 74 3b 0a 20 20 20 20 20 20 20  MaskSet;.       
2f4c0 20 20 20 74 65 6d 70 57 43 2e 6f 70 20 3d 20 54     tempWC.op = T
2f4c1 4b 5f 41 4e 44 3b 0a 20 20 20 20 20 20 20 20 20  K_AND;.         
2f4c2 20 74 65 6d 70 57 43 2e 61 20 3d 20 70 4f 72 54   tempWC.a = pOrT
2f4c3 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 74  erm;.          t
2f4c4 65 6d 70 57 43 2e 6e 54 65 72 6d 20 3d 20 31 3b  empWC.nTerm = 1;
2f4c5 0a 20 20 20 20 20 20 20 20 20 20 62 65 73 74 49  .          bestI
2f4c6 6e 64 65 78 28 70 50 61 72 73 65 2c 20 26 74 65  ndex(pParse, &te
2f4c7 6d 70 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52  mpWC, pSrc, notR
2f4c8 65 61 64 79 2c 20 30 2c 20 26 73 54 65 72 6d 43  eady, 0, &sTermC
2f4c9 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  ost);.        }e
2f4ca 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63  lse{.          c
2f4cb 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
2f4cc 20 7d 0a 20 20 20 20 20 20 20 20 72 54 6f 74 61   }.        rTota
2f4cd 6c 20 2b 3d 20 73 54 65 72 6d 43 6f 73 74 2e 72  l += sTermCost.r
2f4ce 43 6f 73 74 3b 0a 20 20 20 20 20 20 20 20 6e 52  Cost;.        nR
2f4cf 6f 77 20 2b 3d 20 73 54 65 72 6d 43 6f 73 74 2e  ow += sTermCost.
2f4d0 6e 52 6f 77 3b 0a 20 20 20 20 20 20 20 20 75 73  nRow;.        us
2f4d1 65 64 20 7c 3d 20 73 54 65 72 6d 43 6f 73 74 2e  ed |= sTermCost.
2f4d2 75 73 65 64 3b 0a 20 20 20 20 20 20 20 20 69 66  used;.        if
2f4d3 28 20 72 54 6f 74 61 6c 3e 3d 70 43 6f 73 74 2d  ( rTotal>=pCost-
2f4d4 3e 72 43 6f 73 74 20 29 20 62 72 65 61 6b 3b 0a  >rCost ) break;.
2f4d5 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
2f4d6 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e  * If there is an
2f4d7 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2f4d8 2c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73  , increase the s
2f4d9 63 61 6e 20 63 6f 73 74 20 74 6f 20 61 63 63 6f  can cost to acco
2f4da 75 6e 74 20 0a 20 20 20 20 20 20 2a 2a 20 66 6f  unt .      ** fo
2f4db 72 20 74 68 65 20 63 6f 73 74 20 6f 66 20 74 68  r the cost of th
2f4dc 65 20 73 6f 72 74 2e 20 2a 2f 0a 20 20 20 20 20  e sort. */.     
2f4dd 20 69 66 28 20 70 4f 72 64 65 72 42 79 21 3d 30   if( pOrderBy!=0
2f4de 20 29 7b 0a 20 20 20 20 20 20 20 20 72 54 6f 74   ){.        rTot
2f4df 61 6c 20 2b 3d 20 6e 52 6f 77 2a 65 73 74 4c 6f  al += nRow*estLo
2f4e0 67 28 6e 52 6f 77 29 3b 0a 20 20 20 20 20 20 20  g(nRow);.       
2f4e1 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e   WHERETRACE(("..
2f4e2 2e 20 73 6f 72 74 69 6e 67 20 69 6e 63 72 65 61  . sorting increa
2f4e3 73 65 73 20 4f 52 20 63 6f 73 74 20 74 6f 20 25  ses OR cost to %
2f4e4 2e 39 67 5c 6e 22 2c 20 72 54 6f 74 61 6c 29 29  .9g\n", rTotal))
2f4e5 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
2f4e6 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 73 74 20   /* If the cost 
2f4e7 6f 66 20 73 63 61 6e 6e 69 6e 67 20 75 73 69 6e  of scanning usin
2f4e8 67 20 74 68 69 73 20 4f 52 20 74 65 72 6d 20 66  g this OR term f
2f4e9 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  or optimization 
2f4ea 69 73 0a 20 20 20 20 20 20 2a 2a 20 6c 65 73 73  is.      ** less
2f4eb 20 74 68 61 6e 20 74 68 65 20 63 75 72 72 65 6e   than the curren
2f4ec 74 20 63 6f 73 74 20 73 74 6f 72 65 64 20 69 6e  t cost stored in
2f4ed 20 70 43 6f 73 74 2c 20 72 65 70 6c 61 63 65 20   pCost, replace 
2f4ee 74 68 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 20  the contents.   
2f4ef 20 20 20 2a 2a 20 6f 66 20 70 43 6f 73 74 2e 20     ** of pCost. 
2f4f0 2a 2f 0a 20 20 20 20 20 20 57 48 45 52 45 54 52  */.      WHERETR
2f4f1 41 43 45 28 28 22 2e 2e 2e 20 6d 75 6c 74 69 2d  ACE(("... multi-
2f4f2 69 6e 64 65 78 20 4f 52 20 63 6f 73 74 3d 25 2e  index OR cost=%.
2f4f3 39 67 20 6e 72 6f 77 3d 25 2e 39 67 5c 6e 22 2c  9g nrow=%.9g\n",
2f4f4 20 72 54 6f 74 61 6c 2c 20 6e 52 6f 77 29 29 3b   rTotal, nRow));
2f4f5 0a 20 20 20 20 20 20 69 66 28 20 72 54 6f 74 61  .      if( rTota
2f4f6 6c 3c 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 29  l<pCost->rCost )
2f4f7 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d  {.        pCost-
2f4f8 3e 72 43 6f 73 74 20 3d 20 72 54 6f 74 61 6c 3b  >rCost = rTotal;
2f4f9 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d 3e  .        pCost->
2f4fa 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20 20 20  nRow = nRow;.   
2f4fb 20 20 20 20 20 70 43 6f 73 74 2d 3e 75 73 65 64       pCost->used
2f4fc 20 3d 20 75 73 65 64 3b 0a 20 20 20 20 20 20 20   = used;.       
2f4fd 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46   pCost->plan.wsF
2f4fe 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20  lags = flags;.  
2f4ff 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61        pCost->pla
2f500 6e 2e 75 2e 70 54 65 72 6d 20 3d 20 70 54 65 72  n.u.pTerm = pTer
2f501 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  m;.      }.    }
2f502 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
2f503 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50  QLITE_OMIT_OR_OP
2f504 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 7d 0a  TIMIZATION */.}.
2f505 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2f506 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2f507 45 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  E./*.** Allocate
2f508 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 6e   and populate an
2f509 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2f50a 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 49  nfo structure. I
2f50b 74 20 69 73 20 74 68 65 20 0a 2a 2a 20 72 65 73  t is the .** res
2f50c 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74  ponsibility of t
2f50d 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 65 76 65  he caller to eve
2f50e 6e 74 75 61 6c 6c 79 20 72 65 6c 65 61 73 65 20  ntually release 
2f50f 74 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  the structure.**
2f510 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20   by passing the 
2f511 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e 65 64  pointer returned
2f512 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
2f513 6e 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65  n to sqlite3_fre
2f514 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  e()..*/.static s
2f515 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2f516 6f 20 2a 61 6c 6c 6f 63 61 74 65 49 6e 64 65 78  o *allocateIndex
2f517 49 6e 66 6f 28 0a 20 20 50 61 72 73 65 20 2a 70  Info(.  Parse *p
2f518 50 61 72 73 65 2c 20 0a 20 20 57 68 65 72 65 43  Parse, .  WhereC
2f519 6c 61 75 73 65 20 2a 70 57 43 2c 0a 20 20 73 74  lause *pWC,.  st
2f51a 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2f51b 6d 20 2a 70 53 72 63 2c 0a 20 20 45 78 70 72 4c  m *pSrc,.  ExprL
2f51c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 0a 29 7b  ist *pOrderBy.){
2f51d 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69  .  int i, j;.  i
2f51e 6e 74 20 6e 54 65 72 6d 3b 0a 20 20 73 74 72 75  nt nTerm;.  stru
2f51f 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  ct sqlite3_index
2f520 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70 49 64  _constraint *pId
2f521 78 43 6f 6e 73 3b 0a 20 20 73 74 72 75 63 74 20  xCons;.  struct 
2f522 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72  sqlite3_index_or
2f523 64 65 72 62 79 20 2a 70 49 64 78 4f 72 64 65 72  derby *pIdxOrder
2f524 42 79 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c  By;.  struct sql
2f525 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
2f526 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 70 55 73  raint_usage *pUs
2f527 61 67 65 3b 0a 20 20 57 68 65 72 65 54 65 72 6d  age;.  WhereTerm
2f528 20 2a 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 6e   *pTerm;.  int n
2f529 4f 72 64 65 72 42 79 3b 0a 20 20 73 71 6c 69 74  OrderBy;.  sqlit
2f52a 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
2f52b 49 64 78 49 6e 66 6f 3b 0a 0a 20 20 57 48 45 52  IdxInfo;..  WHER
2f52c 45 54 52 41 43 45 28 28 22 52 65 63 6f 6d 70 75  ETRACE(("Recompu
2f52d 74 69 6e 67 20 69 6e 64 65 78 20 69 6e 66 6f 20  ting index info 
2f52e 66 6f 72 20 25 73 2e 2e 2e 5c 6e 22 2c 20 70 53  for %s...\n", pS
2f52f 72 63 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  rc->pTab->zName)
2f530 29 3b 0a 0a 20 20 2f 2a 20 43 6f 75 6e 74 20 74  );..  /* Count t
2f531 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 6f 73  he number of pos
2f532 73 69 62 6c 65 20 57 48 45 52 45 20 63 6c 61 75  sible WHERE clau
2f533 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 72  se constraints r
2f534 65 66 65 72 72 69 6e 67 0a 20 20 2a 2a 20 74 6f  eferring.  ** to
2f535 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 74 61   this virtual ta
2f536 62 6c 65 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6e  ble */.  for(i=n
2f537 54 65 72 6d 3d 30 2c 20 70 54 65 72 6d 3d 70 57  Term=0, pTerm=pW
2f538 43 2d 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65  C->a; i<pWC->nTe
2f539 72 6d 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b  rm; i++, pTerm++
2f53a 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  ){.    if( pTerm
2f53b 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 21 3d 20  ->leftCursor != 
2f53c 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20  pSrc->iCursor ) 
2f53d 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 61 73  continue;.    as
2f53e 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 65 4f  sert( (pTerm->eO
2f53f 70 65 72 61 74 6f 72 26 28 70 54 65 72 6d 2d 3e  perator&(pTerm->
2f540 65 4f 70 65 72 61 74 6f 72 2d 31 29 29 3d 3d 30  eOperator-1))==0
2f541 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2f542 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  ( pTerm->eOperat
2f543 6f 72 3d 3d 57 4f 5f 49 4e 20 29 3b 0a 20 20 20  or==WO_IN );.   
2f544 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d   testcase( pTerm
2f545 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f  ->eOperator==WO_
2f546 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 69 66  ISNULL );.    if
2f547 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  ( pTerm->eOperat
2f548 6f 72 20 26 20 28 57 4f 5f 49 4e 7c 57 4f 5f 49  or & (WO_IN|WO_I
2f549 53 4e 55 4c 4c 29 20 29 20 63 6f 6e 74 69 6e 75  SNULL) ) continu
2f54a 65 3b 0a 20 20 20 20 6e 54 65 72 6d 2b 2b 3b 0a  e;.    nTerm++;.
2f54b 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
2f54c 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2f54d 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 63   contains only c
2f54e 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 75  olumns in the cu
2f54f 72 72 65 6e 74 20 0a 20 20 2a 2a 20 76 69 72 74  rrent .  ** virt
2f550 75 61 6c 20 74 61 62 6c 65 20 74 68 65 6e 20 61  ual table then a
2f551 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
2f552 72 20 74 68 65 20 61 4f 72 64 65 72 42 79 20 70  r the aOrderBy p
2f553 61 72 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20  art of.  ** the 
2f554 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2f555 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20  fo structure..  
2f556 2a 2f 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d 20  */.  nOrderBy = 
2f557 30 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42  0;.  if( pOrderB
2f558 79 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  y ){.    for(i=0
2f559 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45  ; i<pOrderBy->nE
2f55a 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
2f55b 20 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70   Expr *pExpr = p
2f55c 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 70 45  OrderBy->a[i].pE
2f55d 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70  xpr;.      if( p
2f55e 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c  Expr->op!=TK_COL
2f55f 55 4d 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54  UMN || pExpr->iT
2f560 61 62 6c 65 21 3d 70 53 72 63 2d 3e 69 43 75 72  able!=pSrc->iCur
2f561 73 6f 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  sor ) break;.   
2f562 20 7d 0a 20 20 20 20 69 66 28 20 69 3d 3d 70 4f   }.    if( i==pO
2f563 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20 29 7b  rderBy->nExpr ){
2f564 0a 20 20 20 20 20 20 6e 4f 72 64 65 72 42 79 20  .      nOrderBy 
2f565 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  = pOrderBy->nExp
2f566 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  r;.    }.  }..  
2f567 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20  /* Allocate the 
2f568 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2f569 66 6f 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a  fo structure.  *
2f56a 2f 0a 20 20 70 49 64 78 49 6e 66 6f 20 3d 20 73  /.  pIdxInfo = s
2f56b 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
2f56c 72 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73  ro(pParse->db, s
2f56d 69 7a 65 6f 66 28 2a 70 49 64 78 49 6e 66 6f 29  izeof(*pIdxInfo)
2f56e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f56f 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 28 73              + (s
2f570 69 7a 65 6f 66 28 2a 70 49 64 78 43 6f 6e 73 29  izeof(*pIdxCons)
2f571 20 2b 20 73 69 7a 65 6f 66 28 2a 70 55 73 61 67   + sizeof(*pUsag
2f572 65 29 29 2a 6e 54 65 72 6d 0a 20 20 20 20 20 20  e))*nTerm.      
2f573 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f574 20 20 20 20 20 2b 20 73 69 7a 65 6f 66 28 2a 70       + sizeof(*p
2f575 49 64 78 4f 72 64 65 72 42 79 29 2a 6e 4f 72 64  IdxOrderBy)*nOrd
2f576 65 72 42 79 20 29 3b 0a 20 20 69 66 28 20 70 49  erBy );.  if( pI
2f577 64 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20  dxInfo==0 ){.   
2f578 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2f579 28 70 50 61 72 73 65 2c 20 22 6f 75 74 20 6f 66  (pParse, "out of
2f57a 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 20 20 2f   memory");.    /
2f57b 2a 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63  * (double)0 In c
2f57c 61 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d  ase of SQLITE_OM
2f57d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
2f57e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  T... */.    retu
2f57f 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
2f580 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73  Initialize the s
2f581 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 73  tructure.  The s
2f582 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2f583 6f 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74  o structure cont
2f584 61 69 6e 73 0a 20 20 2a 2a 20 6d 61 6e 79 20 66  ains.  ** many f
2f585 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 20 64  ields that are d
2f586 65 63 6c 61 72 65 64 20 22 63 6f 6e 73 74 22 20  eclared "const" 
2f587 74 6f 20 70 72 65 76 65 6e 74 20 78 42 65 73 74  to prevent xBest
2f588 49 6e 64 65 78 20 66 72 6f 6d 0a 20 20 2a 2a 20  Index from.  ** 
2f589 63 68 61 6e 67 69 6e 67 20 74 68 65 6d 2e 20 20  changing them.  
2f58a 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 73 6f  We have to do so
2f58b 6d 65 20 66 75 6e 6b 79 20 63 61 73 74 69 6e 67  me funky casting
2f58c 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 20 20 2a   in order to.  *
2f58d 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 6f  * initialize tho
2f58e 73 65 20 66 69 65 6c 64 73 2e 0a 20 20 2a 2f 0a  se fields..  */.
2f58f 20 20 70 49 64 78 43 6f 6e 73 20 3d 20 28 73 74    pIdxCons = (st
2f590 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2f591 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 29 26  ex_constraint*)&
2f592 70 49 64 78 49 6e 66 6f 5b 31 5d 3b 0a 20 20 70  pIdxInfo[1];.  p
2f593 49 64 78 4f 72 64 65 72 42 79 20 3d 20 28 73 74  IdxOrderBy = (st
2f594 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2f595 65 78 5f 6f 72 64 65 72 62 79 2a 29 26 70 49 64  ex_orderby*)&pId
2f596 78 43 6f 6e 73 5b 6e 54 65 72 6d 5d 3b 0a 20 20  xCons[nTerm];.  
2f597 70 55 73 61 67 65 20 3d 20 28 73 74 72 75 63 74  pUsage = (struct
2f598 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
2f599 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65 2a  onstraint_usage*
2f59a 29 26 70 49 64 78 4f 72 64 65 72 42 79 5b 6e 4f  )&pIdxOrderBy[nO
2f59b 72 64 65 72 42 79 5d 3b 0a 20 20 2a 28 69 6e 74  rderBy];.  *(int
2f59c 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f  *)&pIdxInfo->nCo
2f59d 6e 73 74 72 61 69 6e 74 20 3d 20 6e 54 65 72 6d  nstraint = nTerm
2f59e 3b 0a 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 78  ;.  *(int*)&pIdx
2f59f 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d  Info->nOrderBy =
2f5a0 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20 2a 28 73   nOrderBy;.  *(s
2f5a1 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2f5a2 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a  dex_constraint**
2f5a3 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e  )&pIdxInfo->aCon
2f5a4 73 74 72 61 69 6e 74 20 3d 20 70 49 64 78 43 6f  straint = pIdxCo
2f5a5 6e 73 3b 0a 20 20 2a 28 73 74 72 75 63 74 20 73  ns;.  *(struct s
2f5a6 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64  qlite3_index_ord
2f5a7 65 72 62 79 2a 2a 29 26 70 49 64 78 49 6e 66 6f  erby**)&pIdxInfo
2f5a8 2d 3e 61 4f 72 64 65 72 42 79 20 3d 20 70 49 64  ->aOrderBy = pId
2f5a9 78 4f 72 64 65 72 42 79 3b 0a 20 20 2a 28 73 74  xOrderBy;.  *(st
2f5aa 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2f5ab 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73  ex_constraint_us
2f5ac 61 67 65 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d  age**)&pIdxInfo-
2f5ad 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67  >aConstraintUsag
2f5ae 65 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20  e =.            
2f5af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f5b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f5b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f5b2 20 20 20 20 20 20 20 70 55 73 61 67 65 3b 0a 0a         pUsage;..
2f5b3 20 20 66 6f 72 28 69 3d 6a 3d 30 2c 20 70 54 65    for(i=j=0, pTe
2f5b4 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43  rm=pWC->a; i<pWC
2f5b5 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54  ->nTerm; i++, pT
2f5b6 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  erm++){.    if( 
2f5b7 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
2f5b8 72 20 21 3d 20 70 53 72 63 2d 3e 69 43 75 72 73  r != pSrc->iCurs
2f5b9 6f 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  or ) continue;. 
2f5ba 20 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72     assert( (pTer
2f5bb 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26 28 70 54  m->eOperator&(pT
2f5bc 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 2d 31  erm->eOperator-1
2f5bd 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 74 65 73  ))==0 );.    tes
2f5be 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f  tcase( pTerm->eO
2f5bf 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29  perator==WO_IN )
2f5c0 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2f5c1 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2f5c2 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20  ==WO_ISNULL );. 
2f5c3 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f     if( pTerm->eO
2f5c4 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 4e  perator & (WO_IN
2f5c5 7c 57 4f 5f 49 53 4e 55 4c 4c 29 20 29 20 63 6f  |WO_ISNULL) ) co
2f5c6 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 49 64 78  ntinue;.    pIdx
2f5c7 43 6f 6e 73 5b 6a 5d 2e 69 43 6f 6c 75 6d 6e 20  Cons[j].iColumn 
2f5c8 3d 20 70 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43  = pTerm->u.leftC
2f5c9 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 49 64 78 43  olumn;.    pIdxC
2f5ca 6f 6e 73 5b 6a 5d 2e 69 54 65 72 6d 4f 66 66 73  ons[j].iTermOffs
2f5cb 65 74 20 3d 20 69 3b 0a 20 20 20 20 70 49 64 78  et = i;.    pIdx
2f5cc 43 6f 6e 73 5b 6a 5d 2e 6f 70 20 3d 20 28 75 38  Cons[j].op = (u8
2f5cd 29 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  )pTerm->eOperato
2f5ce 72 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69  r;.    /* The di
2f5cf 72 65 63 74 20 61 73 73 69 67 6e 6d 65 6e 74 20  rect assignment 
2f5d0 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  in the previous 
2f5d1 6c 69 6e 65 20 69 73 20 70 6f 73 73 69 62 6c 65  line is possible
2f5d2 20 6f 6e 6c 79 20 62 65 63 61 75 73 65 0a 20 20   only because.  
2f5d3 20 20 2a 2a 20 74 68 65 20 57 4f 5f 20 61 6e 64    ** the WO_ and
2f5d4 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
2f5d5 4e 53 54 52 41 49 4e 54 5f 20 63 6f 64 65 73 20  NSTRAINT_ codes 
2f5d6 61 72 65 20 69 64 65 6e 74 69 63 61 6c 2e 20 20  are identical.  
2f5d7 54 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f  The.    ** follo
2f5d8 77 69 6e 67 20 61 73 73 65 72 74 73 20 76 65 72  wing asserts ver
2f5d9 69 66 79 20 74 68 69 73 20 66 61 63 74 2e 20 2a  ify this fact. *
2f5da 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 57 4f  /.    assert( WO
2f5db 5f 45 51 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  _EQ==SQLITE_INDE
2f5dc 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20  X_CONSTRAINT_EQ 
2f5dd 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 57  );.    assert( W
2f5de 4f 5f 4c 54 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  O_LT==SQLITE_IND
2f5df 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54  EX_CONSTRAINT_LT
2f5e0 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2f5e1 57 4f 5f 4c 45 3d 3d 53 51 4c 49 54 45 5f 49 4e  WO_LE==SQLITE_IN
2f5e2 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c  DEX_CONSTRAINT_L
2f5e3 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  E );.    assert(
2f5e4 20 57 4f 5f 47 54 3d 3d 53 51 4c 49 54 45 5f 49   WO_GT==SQLITE_I
2f5e5 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
2f5e6 47 54 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  GT );.    assert
2f5e7 28 20 57 4f 5f 47 45 3d 3d 53 51 4c 49 54 45 5f  ( WO_GE==SQLITE_
2f5e8 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
2f5e9 5f 47 45 20 29 3b 0a 20 20 20 20 61 73 73 65 72  _GE );.    asser
2f5ea 74 28 20 57 4f 5f 4d 41 54 43 48 3d 3d 53 51 4c  t( WO_MATCH==SQL
2f5eb 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
2f5ec 41 49 4e 54 5f 4d 41 54 43 48 20 29 3b 0a 20 20  AINT_MATCH );.  
2f5ed 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d    assert( pTerm-
2f5ee 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f  >eOperator & (WO
2f5ef 5f 45 51 7c 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c  _EQ|WO_LT|WO_LE|
2f5f0 57 4f 5f 47 54 7c 57 4f 5f 47 45 7c 57 4f 5f 4d  WO_GT|WO_GE|WO_M
2f5f1 41 54 43 48 29 20 29 3b 0a 20 20 20 20 6a 2b 2b  ATCH) );.    j++
2f5f2 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
2f5f3 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b   i<nOrderBy; i++
2f5f4 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78  ){.    Expr *pEx
2f5f5 70 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61  pr = pOrderBy->a
2f5f6 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70  [i].pExpr;.    p
2f5f7 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 69 43  IdxOrderBy[i].iC
2f5f8 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69  olumn = pExpr->i
2f5f9 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 49 64 78  Column;.    pIdx
2f5fa 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65 73 63 20  OrderBy[i].desc 
2f5fb 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d  = pOrderBy->a[i]
2f5fc 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d 0a  .sortOrder;.  }.
2f5fd 0a 20 20 72 65 74 75 72 6e 20 70 49 64 78 49 6e  .  return pIdxIn
2f5fe 66 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  fo;.}../*.** The
2f5ff 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 20 72 65   table object re
2f600 66 65 72 65 6e 63 65 20 70 61 73 73 65 64 20 61  ference passed a
2f601 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
2f602 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75  ument to this fu
2f603 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 72  nction.** must r
2f604 65 70 72 65 73 65 6e 74 20 61 20 76 69 72 74 75  epresent a virtu
2f605 61 6c 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66  al table. This f
2f606 75 6e 63 74 69 6f 6e 20 69 6e 76 6f 6b 65 73 20  unction invokes 
2f607 74 68 65 20 78 42 65 73 74 49 6e 64 65 78 28 29  the xBestIndex()
2f608 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  .** method of th
2f609 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2f60a 77 69 74 68 20 74 68 65 20 73 71 6c 69 74 65 33  with the sqlite3
2f60b 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 70 6f 69 6e  _index_info poin
2f60c 74 65 72 20 70 61 73 73 65 64 0a 2a 2a 20 61 73  ter passed.** as
2f60d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   the argument..*
2f60e 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
2f60f 20 6f 63 63 75 72 73 2c 20 70 50 61 72 73 65 20   occurs, pParse 
2f610 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  is populated wit
2f611 68 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  h an error messa
2f612 67 65 20 61 6e 64 20 61 0a 2a 2a 20 6e 6f 6e 2d  ge and a.** non-
2f613 7a 65 72 6f 20 76 61 6c 75 65 20 69 73 20 72 65  zero value is re
2f614 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  turned. Otherwis
2f615 65 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65 64  e, 0 is returned
2f616 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0a   and the output.
2f617 2a 2a 20 70 61 72 74 20 6f 66 20 74 68 65 20 73  ** part of the s
2f618 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2f619 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20 6c  o structure is l
2f61a 65 66 74 20 70 6f 70 75 6c 61 74 65 64 2e 0a 2a  eft populated..*
2f61b 2a 0a 2a 2a 20 57 68 65 74 68 65 72 20 6f 72 20  *.** Whether or 
2f61c 6e 6f 74 20 61 6e 20 65 72 72 6f 72 20 69 73 20  not an error is 
2f61d 72 65 74 75 72 6e 65 64 2c 20 69 74 20 69 73 20  returned, it is 
2f61e 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69  the responsibili
2f61f 74 79 20 6f 66 20 74 68 65 0a 2a 2a 20 63 61 6c  ty of the.** cal
2f620 6c 65 72 20 74 6f 20 65 76 65 6e 74 75 61 6c 6c  ler to eventuall
2f621 79 20 66 72 65 65 20 70 2d 3e 69 64 78 53 74 72  y free p->idxStr
2f622 20 69 66 20 70 2d 3e 6e 65 65 64 54 6f 46 72 65   if p->needToFre
2f623 65 49 64 78 53 74 72 20 69 6e 64 69 63 61 74 65  eIdxStr indicate
2f624 73 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20 69  s.** that this i
2f625 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73  s required..*/.s
2f626 74 61 74 69 63 20 69 6e 74 20 76 74 61 62 42 65  tatic int vtabBe
2f627 73 74 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70  stIndex(Parse *p
2f628 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
2f629 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  ab, sqlite3_inde
2f62a 78 5f 69 6e 66 6f 20 2a 70 29 7b 0a 20 20 73 71  x_info *p){.  sq
2f62b 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
2f62c 62 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 54  b = sqlite3GetVT
2f62d 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  able(pParse->db,
2f62e 20 70 54 61 62 29 2d 3e 70 56 74 61 62 3b 0a 20   pTab)->pVtab;. 
2f62f 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63   int i;.  int rc
2f630 3b 0a 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  ;..  (void)sqlit
2f631 65 33 53 61 66 65 74 79 4f 66 66 28 70 50 61 72  e3SafetyOff(pPar
2f632 73 65 2d 3e 64 62 29 3b 0a 20 20 57 48 45 52 45  se->db);.  WHERE
2f633 54 52 41 43 45 28 28 22 78 42 65 73 74 49 6e 64  TRACE(("xBestInd
2f634 65 78 20 66 6f 72 20 25 73 5c 6e 22 2c 20 70 54  ex for %s\n", pT
2f635 61 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 54  ab->zName));.  T
2f636 52 41 43 45 5f 49 44 58 5f 49 4e 50 55 54 53 28  RACE_IDX_INPUTS(
2f637 70 29 3b 0a 20 20 72 63 20 3d 20 70 56 74 61 62  p);.  rc = pVtab
2f638 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 42 65 73 74  ->pModule->xBest
2f639 49 6e 64 65 78 28 70 56 74 61 62 2c 20 70 29 3b  Index(pVtab, p);
2f63a 0a 20 20 54 52 41 43 45 5f 49 44 58 5f 4f 55 54  .  TRACE_IDX_OUT
2f63b 50 55 54 53 28 70 29 3b 0a 20 20 28 76 6f 69 64  PUTS(p);.  (void
2f63c 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
2f63d 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 0a 20  (pParse->db);.. 
2f63e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2f63f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63  OK ){.    if( rc
2f640 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
2f641 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  {.      pParse->
2f642 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2f643 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 1;.    }else 
2f644 69 66 28 20 21 70 56 74 61 62 2d 3e 7a 45 72 72  if( !pVtab->zErr
2f645 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Msg ){.      sql
2f646 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2f647 72 73 65 2c 20 22 25 73 22 2c 20 73 71 6c 69 74  rse, "%s", sqlit
2f648 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20  e3ErrStr(rc));. 
2f649 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2f64a 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2f64b 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 70 56  pParse, "%s", pV
2f64c 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  tab->zErrMsg);. 
2f64d 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
2f64e 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d  e3DbFree(pParse-
2f64f 3e 64 62 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72  >db, pVtab->zErr
2f650 4d 73 67 29 3b 0a 20 20 70 56 74 61 62 2d 3e 7a  Msg);.  pVtab->z
2f651 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 66  ErrMsg = 0;..  f
2f652 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f  or(i=0; i<p->nCo
2f653 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a  nstraint; i++){.
2f654 20 20 20 20 69 66 28 20 21 70 2d 3e 61 43 6f 6e      if( !p->aCon
2f655 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c  straint[i].usabl
2f656 65 20 26 26 20 70 2d 3e 61 43 6f 6e 73 74 72 61  e && p->aConstra
2f657 69 6e 74 55 73 61 67 65 5b 69 5d 2e 61 72 67 76  intUsage[i].argv
2f658 49 6e 64 65 78 3e 30 20 29 7b 0a 20 20 20 20 20  Index>0 ){.     
2f659 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2f65a 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
2f65b 20 20 20 20 22 74 61 62 6c 65 20 25 73 3a 20 78      "table %s: x
2f65c 42 65 73 74 49 6e 64 65 78 20 72 65 74 75 72 6e  BestIndex return
2f65d 65 64 20 61 6e 20 69 6e 76 61 6c 69 64 20 70 6c  ed an invalid pl
2f65e 61 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  an", pTab->zName
2f65f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
2f660 72 65 74 75 72 6e 20 70 50 61 72 73 65 2d 3e 6e  return pParse->n
2f661 45 72 72 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43  Err;.}.../*.** C
2f662 6f 6d 70 75 74 65 20 74 68 65 20 62 65 73 74 20  ompute the best 
2f663 69 6e 64 65 78 20 66 6f 72 20 61 20 76 69 72 74  index for a virt
2f664 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ual table..**.**
2f665 20 54 68 65 20 62 65 73 74 20 69 6e 64 65 78 20   The best index 
2f666 69 73 20 63 6f 6d 70 75 74 65 64 20 62 79 20 74  is computed by t
2f667 68 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65  he xBestIndex me
2f668 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74  thod of the virt
2f669 75 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 6d 6f 64  ual.** table mod
2f66a 75 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ule.  This routi
2f66b 6e 65 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73  ne is really jus
2f66c 74 20 61 20 77 72 61 70 70 65 72 20 74 68 61 74  t a wrapper that
2f66d 20 73 65 74 73 20 75 70 0a 2a 2a 20 74 68 65 20   sets up.** the 
2f66e 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2f66f 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61  fo structure tha
2f670 74 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d  t is used to com
2f671 6d 75 6e 69 63 61 74 65 20 77 69 74 68 0a 2a 2a  municate with.**
2f672 20 78 42 65 73 74 49 6e 64 65 78 2e 0a 2a 2a 0a   xBestIndex..**.
2f673 2a 2a 20 49 6e 20 61 20 6a 6f 69 6e 2c 20 74 68  ** In a join, th
2f674 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74  is routine might
2f675 20 62 65 20 63 61 6c 6c 65 64 20 6d 75 6c 74 69   be called multi
2f676 70 6c 65 20 74 69 6d 65 73 20 66 6f 72 20 74 68  ple times for th
2f677 65 0a 2a 2a 20 73 61 6d 65 20 76 69 72 74 75 61  e.** same virtua
2f678 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 73 71  l table.  The sq
2f679 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2f67a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72   structure is cr
2f67b 65 61 74 65 64 0a 2a 2a 20 61 6e 64 20 69 6e 69  eated.** and ini
2f67c 74 69 61 6c 69 7a 65 64 20 6f 6e 20 74 68 65 20  tialized on the 
2f67d 66 69 72 73 74 20 69 6e 76 6f 63 61 74 69 6f 6e  first invocation
2f67e 20 61 6e 64 20 72 65 75 73 65 64 20 6f 6e 20 61   and reused on a
2f67f 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a  ll subsequent.**
2f680 20 69 6e 76 6f 63 61 74 69 6f 6e 73 2e 20 20 54   invocations.  T
2f681 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
2f682 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  _info structure 
2f683 69 73 20 61 6c 73 6f 20 75 73 65 64 20 77 68 65  is also used whe
2f684 6e 0a 2a 2a 20 63 6f 64 65 20 69 73 20 67 65 6e  n.** code is gen
2f685 65 72 61 74 65 64 20 74 6f 20 61 63 63 65 73 73  erated to access
2f686 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
2f687 6c 65 2e 20 20 54 68 65 20 77 68 65 72 65 49 6e  le.  The whereIn
2f688 66 6f 44 65 6c 65 74 65 28 29 20 0a 2a 2a 20 72  foDelete() .** r
2f689 6f 75 74 69 6e 65 20 74 61 6b 65 73 20 63 61 72  outine takes car
2f68a 65 20 6f 66 20 66 72 65 65 69 6e 67 20 74 68 65  e of freeing the
2f68b 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
2f68c 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 61 66  nfo structure af
2f68d 74 65 72 0a 2a 2a 20 65 76 65 72 79 62 6f 64 79  ter.** everybody
2f68e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69   has finished wi
2f68f 74 68 20 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  th it..*/.static
2f690 20 76 6f 69 64 20 62 65 73 74 56 69 72 74 75 61   void bestVirtua
2f691 6c 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20  lIndex(.  Parse 
2f692 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2f693 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2f694 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
2f695 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
2f696 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20  e *pWC,         
2f697 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
2f698 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73  RE clause */.  s
2f699 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2f69a 65 6d 20 2a 70 53 72 63 2c 20 20 20 20 20 20 2f  em *pSrc,      /
2f69b 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  * The FROM claus
2f69c 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63 68  e term to search
2f69d 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f   */.  Bitmask no
2f69e 74 52 65 61 64 79 2c 20 20 20 20 20 20 20 20 20  tReady,         
2f69f 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
2f6a0 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 61 72   cursors that ar
2f6a1 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20  e not available 
2f6a2 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2f6a3 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20  OrderBy,        
2f6a4 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 64 65       /* The orde
2f6a5 72 20 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20  r by clause */. 
2f6a6 20 57 68 65 72 65 43 6f 73 74 20 2a 70 43 6f 73   WhereCost *pCos
2f6a7 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
2f6a8 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74 20   /* Lowest cost 
2f6a9 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0a 20 20  query plan */.  
2f6aa 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2f6ab 66 6f 20 2a 2a 70 70 49 64 78 49 6e 66 6f 20 20  fo **ppIdxInfo  
2f6ac 2f 2a 20 49 6e 64 65 78 20 69 6e 66 6f 72 6d 61  /* Index informa
2f6ad 74 69 6f 6e 20 70 61 73 73 65 64 20 74 6f 20 78  tion passed to x
2f6ae 42 65 73 74 49 6e 64 65 78 20 2a 2f 0a 29 7b 0a  BestIndex */.){.
2f6af 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
2f6b0 70 53 72 63 2d 3e 70 54 61 62 3b 0a 20 20 73 71  pSrc->pTab;.  sq
2f6b1 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2f6b2 20 2a 70 49 64 78 49 6e 66 6f 3b 0a 20 20 73 74   *pIdxInfo;.  st
2f6b3 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2f6b4 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 70  ex_constraint *p
2f6b5 49 64 78 43 6f 6e 73 3b 0a 20 20 73 74 72 75 63  IdxCons;.  struc
2f6b6 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2f6b7 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
2f6b8 20 2a 70 55 73 61 67 65 3b 0a 20 20 57 68 65 72   *pUsage;.  Wher
2f6b9 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20  eTerm *pTerm;.  
2f6ba 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 6e 74 20  int i, j;.  int 
2f6bb 6e 4f 72 64 65 72 42 79 3b 0a 0a 20 20 2f 2a 20  nOrderBy;..  /* 
2f6bc 4d 61 6b 65 20 73 75 72 65 20 77 73 46 6c 61 67  Make sure wsFlag
2f6bd 73 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  s is initialized
2f6be 20 74 6f 20 73 6f 6d 65 20 73 61 6e 65 20 76 61   to some sane va
2f6bf 6c 75 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  lue. Otherwise, 
2f6c0 69 66 20 74 68 65 20 0a 20 20 2a 2a 20 6d 61 6c  if the .  ** mal
2f6c1 6c 6f 63 20 69 6e 20 61 6c 6c 6f 63 61 74 65 49  loc in allocateI
2f6c2 6e 64 65 78 49 6e 66 6f 28 29 20 66 61 69 6c 73  ndexInfo() fails
2f6c3 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69   and this functi
2f6c4 6f 6e 20 72 65 74 75 72 6e 73 20 6c 65 61 76 69  on returns leavi
2f6c5 6e 67 0a 20 20 2a 2a 20 77 73 46 6c 61 67 73 20  ng.  ** wsFlags 
2f6c6 69 6e 20 61 6e 20 75 6e 69 6e 69 74 69 61 6c 69  in an uninitiali
2f6c7 7a 65 64 20 73 74 61 74 65 2c 20 74 68 65 20 63  zed state, the c
2f6c8 61 6c 6c 65 72 20 6d 61 79 20 62 65 68 61 76 65  aller may behave
2f6c9 20 75 6e 70 72 65 64 69 63 74 61 62 6c 79 2e 0a   unpredictably..
2f6ca 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 70 43    */.  memset(pC
2f6cb 6f 73 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a  ost, 0, sizeof(*
2f6cc 70 43 6f 73 74 29 29 3b 0a 20 20 70 43 6f 73 74  pCost));.  pCost
2f6cd 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d  ->plan.wsFlags =
2f6ce 20 57 48 45 52 45 5f 56 49 52 54 55 41 4c 54 41   WHERE_VIRTUALTA
2f6cf 42 4c 45 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  BLE;..  /* If th
2f6d0 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
2f6d1 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 68  info structure h
2f6d2 61 73 20 6e 6f 74 20 62 65 65 6e 20 70 72 65 76  as not been prev
2f6d3 69 6f 75 73 6c 79 0a 20 20 2a 2a 20 61 6c 6c 6f  iously.  ** allo
2f6d4 63 61 74 65 64 20 61 6e 64 20 69 6e 69 74 69 61  cated and initia
2f6d5 6c 69 7a 65 64 2c 20 74 68 65 6e 20 61 6c 6c 6f  lized, then allo
2f6d6 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c  cate and initial
2f6d7 69 7a 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f  ize it now..  */
2f6d8 0a 20 20 70 49 64 78 49 6e 66 6f 20 3d 20 2a 70  .  pIdxInfo = *p
2f6d9 70 49 64 78 49 6e 66 6f 3b 0a 20 20 69 66 28 20  pIdxInfo;.  if( 
2f6da 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20  pIdxInfo==0 ){. 
2f6db 20 20 20 2a 70 70 49 64 78 49 6e 66 6f 20 3d 20     *ppIdxInfo = 
2f6dc 70 49 64 78 49 6e 66 6f 20 3d 20 61 6c 6c 6f 63  pIdxInfo = alloc
2f6dd 61 74 65 49 6e 64 65 78 49 6e 66 6f 28 70 50 61  ateIndexInfo(pPa
2f6de 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20  rse, pWC, pSrc, 
2f6df 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 7d 0a 20  pOrderBy);.  }. 
2f6e0 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30   if( pIdxInfo==0
2f6e1 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
2f6e2 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69    }..  /* At thi
2f6e3 73 20 70 6f 69 6e 74 2c 20 74 68 65 20 73 71 6c  s point, the sql
2f6e4 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2f6e5 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 70  structure that p
2f6e6 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74 73 0a 20  IdxInfo points. 
2f6e7 20 2a 2a 20 74 6f 20 77 69 6c 6c 20 68 61 76 65   ** to will have
2f6e8 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   been initialize
2f6e9 64 2c 20 65 69 74 68 65 72 20 64 75 72 69 6e 67  d, either during
2f6ea 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 6e 76   the current inv
2f6eb 6f 63 61 74 69 6f 6e 20 6f 72 0a 20 20 2a 2a 20  ocation or.  ** 
2f6ec 64 75 72 69 6e 67 20 73 6f 6d 65 20 70 72 69 6f  during some prio
2f6ed 72 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 4e  r invocation.  N
2f6ee 6f 77 20 77 65 20 6a 75 73 74 20 68 61 76 65 20  ow we just have 
2f6ef 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68 65  to customize the
2f6f0 0a 20 20 2a 2a 20 64 65 74 61 69 6c 73 20 6f 66  .  ** details of
2f6f1 20 70 49 64 78 49 6e 66 6f 20 66 6f 72 20 74 68   pIdxInfo for th
2f6f2 65 20 63 75 72 72 65 6e 74 20 69 6e 76 6f 63 61  e current invoca
2f6f3 74 69 6f 6e 20 61 6e 64 20 70 61 73 73 20 69 74  tion and pass it
2f6f4 20 74 6f 0a 20 20 2a 2a 20 78 42 65 73 74 49 6e   to.  ** xBestIn
2f6f5 64 65 78 2e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20  dex..  */..  /* 
2f6f6 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20  The module name 
2f6f7 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 2e  must be defined.
2f6f8 20 41 6c 73 6f 2c 20 62 79 20 74 68 69 73 20 70   Also, by this p
2f6f9 6f 69 6e 74 20 74 68 65 72 65 20 6d 75 73 74 0a  oint there must.
2f6fa 20 20 2a 2a 20 62 65 20 61 20 70 6f 69 6e 74 65    ** be a pointe
2f6fb 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f  r to an sqlite3_
2f6fc 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 20  vtab structure. 
2f6fd 4f 74 68 65 72 77 69 73 65 0a 20 20 2a 2a 20 73  Otherwise.  ** s
2f6fe 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
2f6ff 75 6d 6e 4e 61 6d 65 73 28 29 20 77 6f 75 6c 64  umnNames() would
2f700 20 68 61 76 65 20 70 69 63 6b 65 64 20 75 70 20   have picked up 
2f701 74 68 65 20 65 72 72 6f 72 2e 20 0a 20 20 2a 2f  the error. .  */
2f702 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d  .  assert( pTab-
2f703 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 20 26 26 20  >azModuleArg && 
2f704 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  pTab->azModuleAr
2f705 67 5b 30 5d 20 29 3b 0a 20 20 61 73 73 65 72 74  g[0] );.  assert
2f706 28 20 73 71 6c 69 74 65 33 47 65 74 56 54 61 62  ( sqlite3GetVTab
2f707 6c 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  le(pParse->db, p
2f708 54 61 62 29 20 29 3b 0a 0a 20 20 2f 2a 20 53 65  Tab) );..  /* Se
2f709 74 20 74 68 65 20 61 43 6f 6e 73 74 72 61 69 6e  t the aConstrain
2f70a 74 5b 5d 2e 75 73 61 62 6c 65 20 66 69 65 6c 64  t[].usable field
2f70b 73 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  s and initialize
2f70c 20 61 6c 6c 20 0a 20 20 2a 2a 20 6f 75 74 70 75   all .  ** outpu
2f70d 74 20 76 61 72 69 61 62 6c 65 73 20 74 6f 20 7a  t variables to z
2f70e 65 72 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61  ero..  **.  ** a
2f70f 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61  Constraint[].usa
2f710 62 6c 65 20 69 73 20 74 72 75 65 20 66 6f 72 20  ble is true for 
2f711 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 68 65 72  constraints wher
2f712 65 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64  e the right-hand
2f713 0a 20 20 2a 2a 20 73 69 64 65 20 63 6f 6e 74 61  .  ** side conta
2f714 69 6e 73 20 6f 6e 6c 79 20 72 65 66 65 72 65 6e  ins only referen
2f715 63 65 73 20 74 6f 20 74 61 62 6c 65 73 20 74 6f  ces to tables to
2f716 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
2f717 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 74 61   current.  ** ta
2f718 62 6c 65 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ble.  In other w
2f719 6f 72 64 73 2c 20 69 66 20 74 68 65 20 63 6f 6e  ords, if the con
2f71a 73 74 72 61 69 6e 74 20 69 73 20 6f 66 20 74 68  straint is of th
2f71b 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a  e form:.  **.  *
2f71c 2a 20 20 20 20 20 20 20 20 20 20 20 63 6f 6c 75  *           colu
2f71d 6d 6e 20 3d 20 65 78 70 72 0a 20 20 2a 2a 0a 20  mn = expr.  **. 
2f71e 20 2a 2a 20 61 6e 64 20 77 65 20 61 72 65 20 65   ** and we are e
2f71f 76 61 6c 75 61 74 69 6e 67 20 61 20 6a 6f 69 6e  valuating a join
2f720 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74  , then the const
2f721 72 61 69 6e 74 20 6f 6e 20 63 6f 6c 75 6d 6e 20  raint on column 
2f722 69 73 20 0a 20 20 2a 2a 20 6f 6e 6c 79 20 76 61  is .  ** only va
2f723 6c 69 64 20 69 66 20 61 6c 6c 20 74 61 62 6c 65  lid if all table
2f724 73 20 72 65 66 65 72 65 6e 63 65 64 20 69 6e 20  s referenced in 
2f725 65 78 70 72 20 6f 63 63 75 72 20 74 6f 20 74 68  expr occur to th
2f726 65 20 6c 65 66 74 0a 20 20 2a 2a 20 6f 66 20 74  e left.  ** of t
2f727 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
2f728 69 6e 67 20 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2a  ing column..  **
2f729 0a 20 20 2a 2a 20 54 68 65 20 61 43 6f 6e 73 74  .  ** The aConst
2f72a 72 61 69 6e 74 73 5b 5d 20 61 72 72 61 79 20 63  raints[] array c
2f72b 6f 6e 74 61 69 6e 73 20 65 6e 74 72 69 65 73 20  ontains entries 
2f72c 66 6f 72 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69  for all constrai
2f72d 6e 74 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  nts.  ** on the 
2f72e 63 75 72 72 65 6e 74 20 74 61 62 6c 65 2e 20 20  current table.  
2f72f 54 68 61 74 20 77 61 79 20 77 65 20 6f 6e 6c 79  That way we only
2f730 20 68 61 76 65 20 74 6f 20 63 6f 6d 70 75 74 65   have to compute
2f731 20 69 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 65 76   it once.  ** ev
2f732 65 6e 20 74 68 6f 75 67 68 20 77 65 20 6d 69 67  en though we mig
2f733 68 74 20 74 72 79 20 74 6f 20 70 69 63 6b 20 74  ht try to pick t
2f734 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 6d 75  he best index mu
2f735 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 0a 20 20  ltiple times..  
2f736 2a 2a 20 46 6f 72 20 65 61 63 68 20 61 74 74 65  ** For each atte
2f737 6d 70 74 20 61 74 20 70 69 63 6b 69 6e 67 20 61  mpt at picking a
2f738 6e 20 69 6e 64 65 78 2c 20 74 68 65 20 6f 72 64  n index, the ord
2f739 65 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  er of tables in 
2f73a 74 68 65 0a 20 20 2a 2a 20 6a 6f 69 6e 20 6d 69  the.  ** join mi
2f73b 67 68 74 20 62 65 20 64 69 66 66 65 72 65 6e 74  ght be different
2f73c 20 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 72   so we have to r
2f73d 65 63 6f 6d 70 75 74 65 20 74 68 65 20 75 73 61  ecompute the usa
2f73e 62 6c 65 20 66 6c 61 67 0a 20 20 2a 2a 20 65 61  ble flag.  ** ea
2f73f 63 68 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20  ch time..  */.  
2f740 70 49 64 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72  pIdxCons = *(str
2f741 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
2f742 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26  x_constraint**)&
2f743 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
2f744 72 61 69 6e 74 3b 0a 20 20 70 55 73 61 67 65 20  raint;.  pUsage 
2f745 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e  = pIdxInfo->aCon
2f746 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a 20 20  straintUsage;.  
2f747 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 49  for(i=0; i<pIdxI
2f748 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  nfo->nConstraint
2f749 3b 20 69 2b 2b 2c 20 70 49 64 78 43 6f 6e 73 2b  ; i++, pIdxCons+
2f74a 2b 29 7b 0a 20 20 20 20 6a 20 3d 20 70 49 64 78  +){.    j = pIdx
2f74b 43 6f 6e 73 2d 3e 69 54 65 72 6d 4f 66 66 73 65  Cons->iTermOffse
2f74c 74 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d 20 26  t;.    pTerm = &
2f74d 70 57 43 2d 3e 61 5b 6a 5d 3b 0a 20 20 20 20 70  pWC->a[j];.    p
2f74e 49 64 78 43 6f 6e 73 2d 3e 75 73 61 62 6c 65 20  IdxCons->usable 
2f74f 3d 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  = (pTerm->prereq
2f750 52 69 67 68 74 26 6e 6f 74 52 65 61 64 79 29 20  Right&notReady) 
2f751 3f 20 30 20 3a 20 31 3b 0a 20 20 7d 0a 20 20 6d  ? 0 : 1;.  }.  m
2f752 65 6d 73 65 74 28 70 55 73 61 67 65 2c 20 30 2c  emset(pUsage, 0,
2f753 20 73 69 7a 65 6f 66 28 70 55 73 61 67 65 5b 30   sizeof(pUsage[0
2f754 5d 29 2a 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f  ])*pIdxInfo->nCo
2f755 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 69 66 28  nstraint);.  if(
2f756 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54   pIdxInfo->needT
2f757 6f 46 72 65 65 49 64 78 53 74 72 20 29 7b 0a 20  oFreeIdxStr ){. 
2f758 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
2f759 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72  pIdxInfo->idxStr
2f75a 29 3b 0a 20 20 7d 0a 20 20 70 49 64 78 49 6e 66  );.  }.  pIdxInf
2f75b 6f 2d 3e 69 64 78 53 74 72 20 3d 20 30 3b 0a 20  o->idxStr = 0;. 
2f75c 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 4e 75   pIdxInfo->idxNu
2f75d 6d 20 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66  m = 0;.  pIdxInf
2f75e 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  o->needToFreeIdx
2f75f 53 74 72 20 3d 20 30 3b 0a 20 20 70 49 64 78 49  Str = 0;.  pIdxI
2f760 6e 66 6f 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73  nfo->orderByCons
2f761 75 6d 65 64 20 3d 20 30 3b 0a 20 20 2f 2a 20 28  umed = 0;.  /* (
2f762 28 64 6f 75 62 6c 65 29 32 29 20 49 6e 20 63 61  (double)2) In ca
2f763 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49  se of SQLITE_OMI
2f764 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
2f765 2e 2e 2e 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66  ... */.  pIdxInf
2f766 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  o->estimatedCost
2f767 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42   = SQLITE_BIG_DB
2f768 4c 20 2f 20 28 28 64 6f 75 62 6c 65 29 32 29 3b  L / ((double)2);
2f769 0a 20 20 6e 4f 72 64 65 72 42 79 20 3d 20 70 49  .  nOrderBy = pI
2f76a 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79  dxInfo->nOrderBy
2f76b 3b 0a 20 20 69 66 28 20 21 70 4f 72 64 65 72 42  ;.  if( !pOrderB
2f76c 79 20 29 7b 0a 20 20 20 20 70 49 64 78 49 6e 66  y ){.    pIdxInf
2f76d 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20 30 3b  o->nOrderBy = 0;
2f76e 0a 20 20 7d 0a 0a 20 20 69 66 28 20 76 74 61 62  .  }..  if( vtab
2f76f 42 65 73 74 49 6e 64 65 78 28 70 50 61 72 73 65  BestIndex(pParse
2f770 2c 20 70 54 61 62 2c 20 70 49 64 78 49 6e 66 6f  , pTab, pIdxInfo
2f771 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
2f772 0a 20 20 7d 0a 0a 20 20 70 49 64 78 43 6f 6e 73  .  }..  pIdxCons
2f773 20 3d 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69   = *(struct sqli
2f774 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
2f775 61 69 6e 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f  aint**)&pIdxInfo
2f776 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20  ->aConstraint;. 
2f777 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78   for(i=0; i<pIdx
2f778 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e  Info->nConstrain
2f779 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  t; i++){.    if(
2f77a 20 70 55 73 61 67 65 5b 69 5d 2e 61 72 67 76 49   pUsage[i].argvI
2f77b 6e 64 65 78 3e 30 20 29 7b 0a 20 20 20 20 20 20  ndex>0 ){.      
2f77c 70 43 6f 73 74 2d 3e 75 73 65 64 20 7c 3d 20 70  pCost->used |= p
2f77d 57 43 2d 3e 61 5b 70 49 64 78 43 6f 6e 73 5b 69  WC->a[pIdxCons[i
2f77e 5d 2e 69 54 65 72 6d 4f 66 66 73 65 74 5d 2e 70  ].iTermOffset].p
2f77f 72 65 72 65 71 52 69 67 68 74 3b 0a 20 20 20 20  rereqRight;.    
2f780 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  }.  }..  /* The 
2f781 63 6f 73 74 20 69 73 20 6e 6f 74 20 61 6c 6c 6f  cost is not allo
2f782 77 65 64 20 74 6f 20 62 65 20 6c 61 72 67 65 72  wed to be larger
2f783 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 42 49 47   than SQLITE_BIG
2f784 5f 44 42 4c 20 28 74 68 65 0a 20 20 2a 2a 20 69  _DBL (the.  ** i
2f785 6e 69 74 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c  nital value of l
2f786 6f 77 65 73 74 43 6f 73 74 20 69 6e 20 74 68 69  owestCost in thi
2f787 73 20 6c 6f 6f 70 2e 20 49 66 20 69 74 20 69 73  s loop. If it is
2f788 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20  , then the.  ** 
2f789 28 63 6f 73 74 3c 6c 6f 77 65 73 74 43 6f 73 74  (cost<lowestCost
2f78a 29 20 74 65 73 74 20 62 65 6c 6f 77 20 77 69 6c  ) test below wil
2f78b 6c 20 6e 65 76 65 72 20 62 65 20 74 72 75 65 2e  l never be true.
2f78c 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 55 73 65 20  .  ** .  ** Use 
2f78d 22 28 64 6f 75 62 6c 65 29 32 22 20 69 6e 73 74  "(double)2" inst
2f78e 65 61 64 20 6f 66 20 22 32 2e 30 22 20 69 6e 20  ead of "2.0" in 
2f78f 63 61 73 65 20 4f 4d 49 54 5f 46 4c 4f 41 54 49  case OMIT_FLOATI
2f790 4e 47 5f 50 4f 49 4e 54 20 0a 20 20 2a 2a 20 69  NG_POINT .  ** i
2f791 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 2a 2f 0a  s defined..  */.
2f792 20 20 69 66 28 20 28 53 51 4c 49 54 45 5f 42 49    if( (SQLITE_BI
2f793 47 5f 44 42 4c 2f 28 28 64 6f 75 62 6c 65 29 32  G_DBL/((double)2
2f794 29 29 3c 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74  ))<pIdxInfo->est
2f795 69 6d 61 74 65 64 43 6f 73 74 20 29 7b 0a 20 20  imatedCost ){.  
2f796 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d    pCost->rCost =
2f797 20 28 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c   (SQLITE_BIG_DBL
2f798 2f 28 28 64 6f 75 62 6c 65 29 32 29 29 3b 0a 20  /((double)2));. 
2f799 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 6f 73   }else{.    pCos
2f79a 74 2d 3e 72 43 6f 73 74 20 3d 20 70 49 64 78 49  t->rCost = pIdxI
2f79b 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f  nfo->estimatedCo
2f79c 73 74 3b 0a 20 20 7d 0a 20 20 70 43 6f 73 74 2d  st;.  }.  pCost-
2f79d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64 78  >plan.u.pVtabIdx
2f79e 20 3d 20 70 49 64 78 49 6e 66 6f 3b 0a 20 20 69   = pIdxInfo;.  i
2f79f 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e 6f 72 64  f( pIdxInfo->ord
2f7a0 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 29 7b 0a  erByConsumed ){.
2f7a1 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e      pCost->plan.
2f7a2 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45  wsFlags |= WHERE
2f7a3 5f 4f 52 44 45 52 42 59 3b 0a 20 20 7d 0a 20 20  _ORDERBY;.  }.  
2f7a4 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20  pCost->plan.nEq 
2f7a5 3d 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d  = 0;.  pIdxInfo-
2f7a6 3e 6e 4f 72 64 65 72 42 79 20 3d 20 6e 4f 72 64  >nOrderBy = nOrd
2f7a7 65 72 42 79 3b 0a 0a 20 20 2f 2a 20 54 72 79 20  erBy;..  /* Try 
2f7a8 74 6f 20 66 69 6e 64 20 61 20 6d 6f 72 65 20 65  to find a more e
2f7a9 66 66 69 63 69 65 6e 74 20 61 63 63 65 73 73 20  fficient access 
2f7aa 70 61 74 74 65 72 6e 20 62 79 20 75 73 69 6e 67  pattern by using
2f7ab 20 6d 75 6c 74 69 70 6c 65 20 69 6e 64 65 78 65   multiple indexe
2f7ac 73 0a 20 20 2a 2a 20 74 6f 20 6f 70 74 69 6d 69  s.  ** to optimi
2f7ad 7a 65 20 61 6e 20 4f 52 20 65 78 70 72 65 73 73  ze an OR express
2f7ae 69 6f 6e 20 77 69 74 68 69 6e 20 74 68 65 20 57  ion within the W
2f7af 48 45 52 45 20 63 6c 61 75 73 65 2e 20 0a 20 20  HERE clause. .  
2f7b0 2a 2f 0a 20 20 62 65 73 74 4f 72 43 6c 61 75 73  */.  bestOrClaus
2f7b1 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  eIndex(pParse, p
2f7b2 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52 65 61  WC, pSrc, notRea
2f7b3 64 79 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 43  dy, pOrderBy, pC
2f7b4 6f 73 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  ost);.}.#endif /
2f7b5 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
2f7b6 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f  RTUALTABLE */../
2f7b7 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 70 49  *.** Argument pI
2f7b8 64 78 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  dx is a pointer 
2f7b9 74 6f 20 61 6e 20 69 6e 64 65 78 20 73 74 72 75  to an index stru
2f7ba 63 74 75 72 65 20 74 68 61 74 20 68 61 73 20 61  cture that has a
2f7bb 6e 20 61 72 72 61 79 20 6f 66 0a 2a 2a 20 53 51  n array of.** SQ
2f7bc 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c  LITE_INDEX_SAMPL
2f7bd 45 53 20 65 76 65 6e 6c 79 20 73 70 61 63 65 64  ES evenly spaced
2f7be 20 73 61 6d 70 6c 65 73 20 6f 66 20 74 68 65 20   samples of the 
2f7bf 66 69 72 73 74 20 69 6e 64 65 78 65 64 20 63 6f  first indexed co
2f7c0 6c 75 6d 6e 0a 2a 2a 20 73 74 6f 72 65 64 20 69  lumn.** stored i
2f7c1 6e 20 49 6e 64 65 78 2e 61 53 61 6d 70 6c 65 2e  n Index.aSample.
2f7c2 20 54 68 65 20 64 6f 6d 61 69 6e 20 6f 66 20 76   The domain of v
2f7c3 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20  alues stored in 
2f7c4 73 61 69 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6d  said column.** m
2f7c5 61 79 20 62 65 20 74 68 6f 75 67 68 74 20 6f 66  ay be thought of
2f7c6 20 61 73 20 64 69 76 69 64 65 64 20 69 6e 74 6f   as divided into
2f7c7 20 28 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53   (SQLITE_INDEX_S
2f7c8 41 4d 50 4c 45 53 2b 31 29 20 72 65 67 69 6f 6e  AMPLES+1) region
2f7c9 73 2e 0a 2a 2a 20 52 65 67 69 6f 6e 20 30 20 63  s..** Region 0 c
2f7ca 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 76 61 6c 75  ontains all valu
2f7cb 65 73 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  es smaller than 
2f7cc 74 68 65 20 66 69 72 73 74 20 73 61 6d 70 6c 65  the first sample
2f7cd 20 76 61 6c 75 65 2e 20 52 65 67 69 6f 6e 0a 2a   value. Region.*
2f7ce 2a 20 31 20 63 6f 6e 74 61 69 6e 73 20 76 61 6c  * 1 contains val
2f7cf 75 65 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20  ues larger than 
2f7d0 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
2f7d1 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 69 72  value of the fir
2f7d2 73 74 20 73 61 6d 70 6c 65 2c 0a 2a 2a 20 62 75  st sample,.** bu
2f7d3 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74  t smaller than t
2f7d4 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
2f7d5 73 65 63 6f 6e 64 2e 20 41 6e 64 20 73 6f 20 6f  second. And so o
2f7d6 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63  n..**.** If succ
2f7d7 65 73 73 66 75 6c 2c 20 74 68 69 73 20 66 75 6e  essful, this fun
2f7d8 63 74 69 6f 6e 20 64 65 74 65 72 6d 69 6e 65 73  ction determines
2f7d9 20 77 68 69 63 68 20 6f 66 20 74 68 65 20 72 65   which of the re
2f7da 67 69 6f 6e 73 20 76 61 6c 75 65 20 0a 2a 2a 20  gions value .** 
2f7db 70 56 61 6c 20 6c 69 65 73 20 69 6e 2c 20 73 65  pVal lies in, se
2f7dc 74 73 20 2a 70 69 52 65 67 69 6f 6e 20 74 6f 20  ts *piRegion to 
2f7dd 74 68 65 20 72 65 67 69 6f 6e 20 69 6e 64 65 78  the region index
2f7de 20 28 61 20 76 61 6c 75 65 20 62 65 74 77 65 65   (a value betwee
2f7df 6e 20 30 0a 2a 2a 20 61 6e 64 20 53 51 4c 49 54  n 0.** and SQLIT
2f7e0 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2b  E_INDEX_SAMPLES+
2f7e1 31 2c 20 69 6e 63 6c 75 73 69 76 65 29 20 61 6e  1, inclusive) an
2f7e2 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  d returns SQLITE
2f7e3 5f 4f 4b 2e 0a 2a 2a 20 4f 72 2c 20 69 66 20 61  _OK..** Or, if a
2f7e4 6e 20 4f 4f 4d 20 6f 63 63 75 72 73 20 77 68 69  n OOM occurs whi
2f7e5 6c 65 20 63 6f 6e 76 65 72 74 69 6e 67 20 74 65  le converting te
2f7e6 78 74 20 76 61 6c 75 65 73 20 62 65 74 77 65 65  xt values betwee
2f7e7 6e 20 65 6e 63 6f 64 69 6e 67 73 2c 0a 2a 2a 20  n encodings,.** 
2f7e8 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20  SQLITE_NOMEM is 
2f7e9 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 69  returned and *pi
2f7ea 52 65 67 69 6f 6e 20 69 73 20 75 6e 64 65 66 69  Region is undefi
2f7eb 6e 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ned..*/.#ifdef S
2f7ec 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
2f7ed 54 32 0a 73 74 61 74 69 63 20 69 6e 74 20 77 68  T2.static int wh
2f7ee 65 72 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 0a  ereRangeRegion(.
2f7ef 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2f7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2f7f1 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
2f7f2 74 69 6f 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20  tion */.  Index 
2f7f3 2a 70 49 64 78 2c 20 20 20 20 20 20 20 20 20 20  *pIdx,          
2f7f4 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 74        /* Index t
2f7f5 6f 20 63 6f 6e 73 69 64 65 72 20 64 6f 6d 61 69  o consider domai
2f7f6 6e 20 6f 66 20 2a 2f 0a 20 20 73 71 6c 69 74 65  n of */.  sqlite
2f7f7 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 2c 20 20  3_value *pVal,  
2f7f8 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
2f7f9 6f 20 63 6f 6e 73 69 64 65 72 20 2a 2f 0a 20 20  o consider */.  
2f7fa 69 6e 74 20 2a 70 69 52 65 67 69 6f 6e 20 20 20  int *piRegion   
2f7fb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
2f7fc 55 54 3a 20 52 65 67 69 6f 6e 20 6f 66 20 64 6f  UT: Region of do
2f7fd 6d 61 69 6e 20 69 6e 20 77 68 69 63 68 20 76 61  main in which va
2f7fe 6c 75 65 20 6c 69 65 73 20 2a 2f 0a 29 7b 0a 20  lue lies */.){. 
2f7ff 20 69 66 28 20 41 4c 57 41 59 53 28 70 56 61 6c   if( ALWAYS(pVal
2f800 29 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 53 61  ) ){.    IndexSa
2f801 6d 70 6c 65 20 2a 61 53 61 6d 70 6c 65 20 3d 20  mple *aSample = 
2f802 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 3b 0a 20  pIdx->aSample;. 
2f803 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20     int i = 0;.  
2f804 20 20 69 6e 74 20 65 54 79 70 65 20 3d 20 73 71    int eType = sq
2f805 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
2f806 28 70 56 61 6c 29 3b 0a 0a 20 20 20 20 69 66 28  (pVal);..    if(
2f807 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49   eType==SQLITE_I
2f808 4e 54 45 47 45 52 20 7c 7c 20 65 54 79 70 65 3d  NTEGER || eType=
2f809 3d 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 29 7b  =SQLITE_FLOAT ){
2f80a 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 20  .      double r 
2f80b 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2f80c 64 6f 75 62 6c 65 28 70 56 61 6c 29 3b 0a 20 20  double(pVal);.  
2f80d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 53      for(i=0; i<S
2f80e 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
2f80f 4c 45 53 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  LES; i++){.     
2f810 20 20 20 69 66 28 20 61 53 61 6d 70 6c 65 5b 69     if( aSample[i
2f811 5d 2e 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  ].eType==SQLITE_
2f812 4e 55 4c 4c 20 29 20 63 6f 6e 74 69 6e 75 65 3b  NULL ) continue;
2f813 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 53 61  .        if( aSa
2f814 6d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3e 3d 53  mple[i].eType>=S
2f815 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c 20 61 53  QLITE_TEXT || aS
2f816 61 6d 70 6c 65 5b 69 5d 2e 75 2e 72 3e 72 20 29  ample[i].u.r>r )
2f817 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2f818 20 20 20 20 7d 65 6c 73 65 7b 20 0a 20 20 20 20      }else{ .    
2f819 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
2f81a 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20  pParse->db;.    
2f81b 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2f81c 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 75 38  ;.      const u8
2f81d 20 2a 7a 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e   *z;.      int n
2f81e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 70 56 61 6c  ;..      /* pVal
2f81f 20 63 6f 6d 65 73 20 66 72 6f 6d 20 73 71 6c 69   comes from sqli
2f820 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
2f821 28 29 20 73 6f 20 74 68 65 20 74 79 70 65 20 63  () so the type c
2f822 61 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 20 2a 2f  annot be NULL */
2f823 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65  .      assert( e
2f824 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58  Type==SQLITE_TEX
2f825 54 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49  T || eType==SQLI
2f826 54 45 5f 42 4c 4f 42 20 29 3b 0a 0a 20 20 20 20  TE_BLOB );..    
2f827 20 20 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c    if( eType==SQL
2f828 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20  ITE_BLOB ){.    
2f829 20 20 20 20 7a 20 3d 20 28 63 6f 6e 73 74 20 75      z = (const u
2f82a 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  8 *)sqlite3_valu
2f82b 65 5f 62 6c 6f 62 28 70 56 61 6c 29 3b 0a 20 20  e_blob(pVal);.  
2f82c 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62        pColl = db
2f82d 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20  ->pDfltColl;.   
2f82e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f       assert( pCo
2f82f 6c 6c 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  ll->enc==SQLITE_
2f830 55 54 46 38 20 29 3b 0a 20 20 20 20 20 20 7d 65  UTF8 );.      }e
2f831 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 43 6f  lse{.        pCo
2f832 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 43  ll = sqlite3GetC
2f833 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c 49 54  ollSeq(db, SQLIT
2f834 45 5f 55 54 46 38 2c 20 30 2c 20 2a 70 49 64 78  E_UTF8, 0, *pIdx
2f835 2d 3e 61 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20  ->azColl);.     
2f836 20 20 20 69 66 28 20 70 43 6f 6c 6c 3d 3d 30 20     if( pColl==0 
2f837 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
2f838 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2f839 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f  rse, "no such co
2f83a 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2f83b 3a 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20  : %s",.         
2f83c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f83d 20 2a 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 29 3b   *pIdx->azColl);
2f83e 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
2f83f 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
2f840 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2f841 20 20 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 20    z = (const u8 
2f842 2a 29 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  *)sqlite3ValueTe
2f843 78 74 28 70 56 61 6c 2c 20 70 43 6f 6c 6c 2d 3e  xt(pVal, pColl->
2f844 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 69 66  enc);.        if
2f845 28 20 21 7a 20 29 7b 0a 20 20 20 20 20 20 20 20  ( !z ){.        
2f846 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2f847 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 7d  NOMEM;.        }
2f848 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2f849 20 7a 20 26 26 20 70 43 6f 6c 6c 20 26 26 20 70   z && pColl && p
2f84a 43 6f 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a 20 20  Coll->xCmp );.  
2f84b 20 20 20 20 7d 0a 20 20 20 20 20 20 6e 20 3d 20      }.      n = 
2f84c 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65  sqlite3ValueByte
2f84d 73 28 70 56 61 6c 2c 20 70 43 6f 6c 6c 2d 3e 65  s(pVal, pColl->e
2f84e 6e 63 29 3b 0a 0a 20 20 20 20 20 20 66 6f 72 28  nc);..      for(
2f84f 69 3d 30 3b 20 69 3c 53 51 4c 49 54 45 5f 49 4e  i=0; i<SQLITE_IN
2f850 44 45 58 5f 53 41 4d 50 4c 45 53 3b 20 69 2b 2b  DEX_SAMPLES; i++
2f851 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  ){.        int r
2f852 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 65 53  ;.        int eS
2f853 61 6d 70 6c 65 74 79 70 65 20 3d 20 61 53 61 6d  ampletype = aSam
2f854 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3b 0a 20 20  ple[i].eType;.  
2f855 20 20 20 20 20 20 69 66 28 20 65 53 61 6d 70 6c        if( eSampl
2f856 65 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4e 55  etype==SQLITE_NU
2f857 4c 4c 20 7c 7c 20 65 53 61 6d 70 6c 65 74 79 70  LL || eSampletyp
2f858 65 3c 65 54 79 70 65 20 29 20 63 6f 6e 74 69 6e  e<eType ) contin
2f859 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ue;.        if( 
2f85a 28 65 53 61 6d 70 6c 65 74 79 70 65 21 3d 65 54  (eSampletype!=eT
2f85b 79 70 65 29 20 29 20 62 72 65 61 6b 3b 0a 23 69  ype) ) break;.#i
2f85c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2f85d 54 5f 55 54 46 31 36 0a 20 20 20 20 20 20 20 20  T_UTF16.        
2f85e 69 66 28 20 70 43 6f 6c 6c 2d 3e 65 6e 63 21 3d  if( pColl->enc!=
2f85f 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20  SQLITE_UTF8 ){. 
2f860 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 53 61           int nSa
2f861 6d 70 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20  mple;.          
2f862 63 68 61 72 20 2a 7a 53 61 6d 70 6c 65 20 3d 20  char *zSample = 
2f863 73 71 6c 69 74 65 33 55 74 66 38 74 6f 31 36 28  sqlite3Utf8to16(
2f864 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64  .              d
2f865 62 2c 20 70 43 6f 6c 6c 2d 3e 65 6e 63 2c 20 61  b, pColl->enc, a
2f866 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e 7a 2c 20 61  Sample[i].u.z, a
2f867 53 61 6d 70 6c 65 5b 69 5d 2e 6e 42 79 74 65 2c  Sample[i].nByte,
2f868 20 26 6e 53 61 6d 70 6c 65 0a 20 20 20 20 20 20   &nSample.      
2f869 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20      );.         
2f86a 20 69 66 28 20 21 7a 53 61 6d 70 6c 65 20 29 7b   if( !zSample ){
2f86b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
2f86c 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
2f86d 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20  ailed );.       
2f86e 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2f86f 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
2f870 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2f871 72 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28  r = pColl->xCmp(
2f872 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e 53  pColl->pUser, nS
2f873 61 6d 70 6c 65 2c 20 7a 53 61 6d 70 6c 65 2c 20  ample, zSample, 
2f874 6e 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 20  n, z);.         
2f875 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2f876 62 2c 20 7a 53 61 6d 70 6c 65 29 3b 0a 20 20 20  b, zSample);.   
2f877 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69       }else.#endi
2f878 66 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20  f.        {.    
2f879 20 20 20 20 20 20 72 20 3d 20 70 43 6f 6c 6c 2d        r = pColl-
2f87a 3e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73  >xCmp(pColl->pUs
2f87b 65 72 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 6e  er, aSample[i].n
2f87c 42 79 74 65 2c 20 61 53 61 6d 70 6c 65 5b 69 5d  Byte, aSample[i]
2f87d 2e 75 2e 7a 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20  .u.z, n, z);.   
2f87e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
2f87f 66 28 20 72 3e 30 20 29 20 62 72 65 61 6b 3b 0a  f( r>0 ) break;.
2f880 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
2f881 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20     assert( i>=0 
2f882 26 26 20 69 3c 3d 53 51 4c 49 54 45 5f 49 4e 44  && i<=SQLITE_IND
2f883 45 58 5f 53 41 4d 50 4c 45 53 20 29 3b 0a 20 20  EX_SAMPLES );.  
2f884 20 20 2a 70 69 52 65 67 69 6f 6e 20 3d 20 69 3b    *piRegion = i;
2f885 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
2f886 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
2f887 66 20 20 20 2f 2a 20 23 69 66 64 65 66 20 53 51  f   /* #ifdef SQ
2f888 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54  LITE_ENABLE_STAT
2f889 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 65  2 */../*.** If e
2f88a 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
2f88b 72 65 70 72 65 73 65 6e 74 73 20 61 20 6c 69 74  represents a lit
2f88c 65 72 61 6c 20 76 61 6c 75 65 2c 20 73 65 74 20  eral value, set 
2f88d 2a 70 70 20 74 6f 20 70 6f 69 6e 74 20 74 6f 0a  *pp to point to.
2f88e 2a 2a 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61  ** an sqlite3_va
2f88f 6c 75 65 20 73 74 72 75 63 74 75 72 65 20 63 6f  lue structure co
2f890 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73 61 6d  ntaining the sam
2f891 65 20 76 61 6c 75 65 2c 20 77 69 74 68 20 61 66  e value, with af
2f892 66 69 6e 69 74 79 0a 2a 2a 20 61 66 66 20 61 70  finity.** aff ap
2f893 70 6c 69 65 64 20 74 6f 20 69 74 2c 20 62 65 66  plied to it, bef
2f894 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 20 49  ore returning. I
2f895 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73  t is the respons
2f896 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 0a  ibility of the .
2f897 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20 65 76 65  ** caller to eve
2f898 6e 74 75 61 6c 6c 79 20 72 65 6c 65 61 73 65 20  ntually release 
2f899 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 62  this structure b
2f89a 79 20 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20  y passing it to 
2f89b 0a 2a 2a 20 73 71 6c 69 74 65 33 56 61 6c 75 65  .** sqlite3Value
2f89c 46 72 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66  Free()..**.** If
2f89d 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61 72   the current par
2f89e 73 65 20 69 73 20 61 20 72 65 63 6f 6d 70 69 6c  se is a recompil
2f89f 65 20 28 73 71 6c 69 74 65 33 52 65 70 72 65 70  e (sqlite3Reprep
2f8a0 61 72 65 28 29 29 20 61 6e 64 20 70 45 78 70 72  are()) and pExpr
2f8a1 0a 2a 2a 20 69 73 20 61 6e 20 53 51 4c 20 76 61  .** is an SQL va
2f8a2 72 69 61 62 6c 65 20 74 68 61 74 20 63 75 72 72  riable that curr
2f8a3 65 6e 74 6c 79 20 68 61 73 20 61 20 6e 6f 6e 2d  ently has a non-
2f8a4 4e 55 4c 4c 20 76 61 6c 75 65 20 62 6f 75 6e 64  NULL value bound
2f8a5 20 74 6f 20 69 74 2c 0a 2a 2a 20 63 72 65 61 74   to it,.** creat
2f8a6 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  e an sqlite3_val
2f8a7 75 65 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  ue structure con
2f8a8 74 61 69 6e 69 6e 67 20 74 68 69 73 20 76 61 6c  taining this val
2f8a9 75 65 2c 20 61 67 61 69 6e 20 77 69 74 68 0a 2a  ue, again with.*
2f8aa 2a 20 61 66 66 69 6e 69 74 79 20 61 66 66 20 61  * affinity aff a
2f8ab 70 70 6c 69 65 64 20 74 6f 20 69 74 2c 20 69 6e  pplied to it, in
2f8ac 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  stead..**.** If 
2f8ad 6e 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 61  neither of the a
2f8ae 62 6f 76 65 20 61 70 70 6c 79 2c 20 73 65 74 20  bove apply, set 
2f8af 2a 70 70 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a  *pp to NULL..**.
2f8b0 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
2f8b1 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20 61 6e  ccurs, return an
2f8b2 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 4f 74 68   error code. Oth
2f8b3 65 72 77 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f  erwise, SQLITE_O
2f8b4 4b 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  K..*/.#ifdef SQL
2f8b5 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
2f8b6 0a 73 74 61 74 69 63 20 69 6e 74 20 76 61 6c 75  .static int valu
2f8b7 65 46 72 6f 6d 45 78 70 72 28 0a 20 20 50 61 72  eFromExpr(.  Par
2f8b8 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 45  se *pParse, .  E
2f8b9 78 70 72 20 2a 70 45 78 70 72 2c 20 0a 20 20 75  xpr *pExpr, .  u
2f8ba 38 20 61 66 66 2c 20 0a 20 20 73 71 6c 69 74 65  8 aff, .  sqlite
2f8bb 33 5f 76 61 6c 75 65 20 2a 2a 70 70 0a 29 7b 0a  3_value **pp.){.
2f8bc 20 20 2f 2a 20 54 68 65 20 65 76 61 6c 43 6f 6e    /* The evalCon
2f8bd 73 74 45 78 70 72 28 29 20 66 75 6e 63 74 69 6f  stExpr() functio
2f8be 6e 20 77 69 6c 6c 20 68 61 76 65 20 61 6c 72 65  n will have alre
2f8bf 61 64 79 20 63 6f 6e 76 65 72 74 65 64 20 61 6e  ady converted an
2f8c0 79 20 54 4b 5f 56 41 52 49 41 42 4c 45 0a 20 20  y TK_VARIABLE.  
2f8c1 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ** expression in
2f8c2 76 6f 6c 76 65 64 20 69 6e 20 61 6e 20 63 6f 6d  volved in an com
2f8c3 70 61 72 69 73 6f 6e 20 69 6e 74 6f 20 61 20 54  parison into a T
2f8c4 4b 5f 52 45 47 49 53 54 45 52 2e 20 2a 2f 0a 20  K_REGISTER. */. 
2f8c5 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
2f8c6 6f 70 21 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20  op!=TK_VARIABLE 
2f8c7 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  );.  if( pExpr->
2f8c8 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20  op==TK_REGISTER 
2f8c9 26 26 20 70 45 78 70 72 2d 3e 6f 70 32 3d 3d 54  && pExpr->op2==T
2f8ca 4b 5f 56 41 52 49 41 42 4c 45 20 29 7b 0a 20 20  K_VARIABLE ){.  
2f8cb 20 20 69 6e 74 20 69 56 61 72 20 3d 20 70 45 78    int iVar = pEx
2f8cc 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  pr->iColumn;.   
2f8cd 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 56   sqlite3VdbeSetV
2f8ce 61 72 6d 61 73 6b 28 70 50 61 72 73 65 2d 3e 70  armask(pParse->p
2f8cf 56 64 62 65 2c 20 69 56 61 72 29 3b 0a 20 20 20  Vdbe, iVar);.   
2f8d0 20 2a 70 70 20 3d 20 73 71 6c 69 74 65 33 56 64   *pp = sqlite3Vd
2f8d1 62 65 47 65 74 56 61 6c 75 65 28 70 50 61 72 73  beGetValue(pPars
2f8d2 65 2d 3e 70 52 65 70 72 65 70 61 72 65 2c 20 69  e->pReprepare, i
2f8d3 56 61 72 2c 20 61 66 66 29 3b 0a 20 20 20 20 72  Var, aff);.    r
2f8d4 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2f8d5 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
2f8d6 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
2f8d7 70 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  pr(pParse->db, p
2f8d8 45 78 70 72 2c 20 53 51 4c 49 54 45 5f 55 54 46  Expr, SQLITE_UTF
2f8d9 38 2c 20 61 66 66 2c 20 70 70 29 3b 0a 7d 0a 23  8, aff, pp);.}.#
2f8da 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  endif../*.** Thi
2f8db 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
2f8dc 65 64 20 74 6f 20 65 73 74 69 6d 61 74 65 20 74  ed to estimate t
2f8dd 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
2f8de 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 76  s that will be v
2f8df 69 73 69 74 65 64 0a 2a 2a 20 62 79 20 73 63 61  isited.** by sca
2f8e0 6e 6e 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 66  nning an index f
2f8e1 6f 72 20 61 20 72 61 6e 67 65 20 6f 66 20 76 61  or a range of va
2f8e2 6c 75 65 73 2e 20 54 68 65 20 72 61 6e 67 65 20  lues. The range 
2f8e3 6d 61 79 20 68 61 76 65 20 61 6e 20 75 70 70 65  may have an uppe
2f8e4 72 0a 2a 2a 20 62 6f 75 6e 64 2c 20 61 20 6c 6f  r.** bound, a lo
2f8e5 77 65 72 20 62 6f 75 6e 64 2c 20 6f 72 20 62 6f  wer bound, or bo
2f8e6 74 68 2e 20 54 68 65 20 57 48 45 52 45 20 63 6c  th. The WHERE cl
2f8e7 61 75 73 65 20 74 65 72 6d 73 20 74 68 61 74 20  ause terms that 
2f8e8 73 65 74 20 74 68 65 20 75 70 70 65 72 0a 2a 2a  set the upper.**
2f8e9 20 61 6e 64 20 6c 6f 77 65 72 20 62 6f 75 6e 64   and lower bound
2f8ea 73 20 61 72 65 20 72 65 70 72 65 73 65 6e 74 65  s are represente
2f8eb 64 20 62 79 20 70 4c 6f 77 65 72 20 61 6e 64 20  d by pLower and 
2f8ec 70 55 70 70 65 72 20 72 65 73 70 65 63 74 69 76  pUpper respectiv
2f8ed 65 6c 79 2e 20 46 6f 72 0a 2a 2a 20 65 78 61 6d  ely. For.** exam
2f8ee 70 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68  ple, assuming th
2f8ef 61 74 20 69 6e 64 65 78 20 70 20 69 73 20 6f 6e  at index p is on
2f8f0 20 74 31 28 61 29 3a 0a 2a 2a 0a 2a 2a 20 20 20   t1(a):.**.**   
2f8f1 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 57 48 45 52  ... FROM t1 WHER
2f8f2 45 20 61 20 3e 20 3f 20 41 4e 44 20 61 20 3c 20  E a > ? AND a < 
2f8f3 3f 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20 20 20  ? ....**        
2f8f4 20 20 20 20 20 20 20 20 20 20 20 20 7c 5f 5f 5f              |___
2f8f5 5f 5f 7c 20 20 20 7c 5f 5f 5f 5f 5f 7c 0a 2a 2a  __|   |_____|.**
2f8f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8f7 20 20 20 20 20 20 20 7c 20 20 20 20 20 20 20 20         |        
2f8f8 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   |.**           
2f8f9 20 20 20 20 20 20 20 20 20 20 70 4c 6f 77 65 72            pLower
2f8fa 20 20 20 20 70 55 70 70 65 72 0a 2a 2a 0a 2a 2a      pUpper.**.**
2f8fb 20 49 66 20 65 69 74 68 65 72 20 6f 66 20 74 68   If either of th
2f8fc 65 20 75 70 70 65 72 20 6f 72 20 6c 6f 77 65 72  e upper or lower
2f8fd 20 62 6f 75 6e 64 20 69 73 20 6e 6f 74 20 70 72   bound is not pr
2f8fe 65 73 65 6e 74 2c 20 74 68 65 6e 20 4e 55 4c 4c  esent, then NULL
2f8ff 20 69 73 20 70 61 73 73 65 64 20 69 6e 0a 2a 2a   is passed in.**
2f900 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20 63 6f   place of the co
2f901 72 72 65 73 70 6f 6e 64 69 6e 67 20 57 68 65 72  rresponding Wher
2f902 65 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  eTerm..**.** The
2f903 20 6e 45 71 20 70 61 72 61 6d 65 74 65 72 20 69   nEq parameter i
2f904 73 20 70 61 73 73 65 64 20 74 68 65 20 69 6e 64  s passed the ind
2f905 65 78 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  ex of the index 
2f906 63 6f 6c 75 6d 6e 20 73 75 62 6a 65 63 74 20 74  column subject t
2f907 6f 20 74 68 65 0a 2a 2a 20 72 61 6e 67 65 20 63  o the.** range c
2f908 6f 6e 73 74 72 61 69 6e 74 2e 20 4f 72 2c 20 65  onstraint. Or, e
2f909 71 75 69 76 61 6c 65 6e 74 6c 79 2c 20 74 68 65  quivalently, the
2f90a 20 6e 75 6d 62 65 72 20 6f 66 20 65 71 75 61 6c   number of equal
2f90b 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  ity constraints.
2f90c 2a 2a 20 6f 70 74 69 6d 69 7a 65 64 20 62 79 20  ** optimized by 
2f90d 74 68 65 20 70 72 6f 70 6f 73 65 64 20 69 6e 64  the proposed ind
2f90e 65 78 20 73 63 61 6e 2e 20 46 6f 72 20 65 78 61  ex scan. For exa
2f90f 6d 70 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 69  mple, assuming i
2f910 6e 64 65 78 20 70 20 69 73 0a 2a 2a 20 6f 6e 20  ndex p is.** on 
2f911 74 31 28 61 2c 20 62 29 2c 20 61 6e 64 20 74 68  t1(a, b), and th
2f912 65 20 53 51 4c 20 71 75 65 72 79 20 69 73 3a 0a  e SQL query is:.
2f913 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f 4d  **.**   ... FROM
2f914 20 74 31 20 57 48 45 52 45 20 61 20 3d 20 3f 20   t1 WHERE a = ? 
2f915 41 4e 44 20 62 20 3e 20 3f 20 41 4e 44 20 62 20  AND b > ? AND b 
2f916 3c 20 3f 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 74 68  < ? ....**.** th
2f917 65 6e 20 6e 45 71 20 73 68 6f 75 6c 64 20 62 65  en nEq should be
2f918 20 70 61 73 73 65 64 20 74 68 65 20 76 61 6c 75   passed the valu
2f919 65 20 31 20 28 61 73 20 74 68 65 20 72 61 6e 67  e 1 (as the rang
2f91a 65 20 72 65 73 74 72 69 63 74 65 64 20 63 6f 6c  e restricted col
2f91b 75 6d 6e 2c 0a 2a 2a 20 62 2c 20 69 73 20 74 68  umn,.** b, is th
2f91c 65 20 73 65 63 6f 6e 64 20 6c 65 66 74 2d 6d 6f  e second left-mo
2f91d 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  st column of the
2f91e 20 69 6e 64 65 78 29 2e 20 4f 72 2c 20 69 66 20   index). Or, if 
2f91f 74 68 65 20 71 75 65 72 79 20 69 73 3a 0a 2a 2a  the query is:.**
2f920 0a 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f 4d 20 74  .**   ... FROM t
2f921 31 20 57 48 45 52 45 20 61 20 3e 20 3f 20 41 4e  1 WHERE a > ? AN
2f922 44 20 61 20 3c 20 3f 20 2e 2e 2e 0a 2a 2a 0a 2a  D a < ? ....**.*
2f923 2a 20 74 68 65 6e 20 6e 45 71 20 73 68 6f 75 6c  * then nEq shoul
2f924 64 20 62 65 20 70 61 73 73 65 64 20 30 2e 0a 2a  d be passed 0..*
2f925 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  *.** The returne
2f926 64 20 76 61 6c 75 65 20 69 73 20 61 6e 20 69 6e  d value is an in
2f927 74 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20  teger between 1 
2f928 61 6e 64 20 31 30 30 2c 20 69 6e 63 6c 75 73 69  and 100, inclusi
2f929 76 65 2e 20 41 20 72 65 74 75 72 6e 0a 2a 2a 20  ve. A return.** 
2f92a 76 61 6c 75 65 20 6f 66 20 31 20 69 6e 64 69 63  value of 1 indic
2f92b 61 74 65 73 20 74 68 61 74 20 74 68 65 20 70 72  ates that the pr
2f92c 6f 70 6f 73 65 64 20 72 61 6e 67 65 20 73 63 61  oposed range sca
2f92d 6e 20 69 73 20 65 78 70 65 63 74 65 64 20 74 6f  n is expected to
2f92e 20 76 69 73 69 74 0a 2a 2a 20 61 70 70 72 6f 78   visit.** approx
2f92f 69 6d 61 74 65 6c 79 20 31 2f 31 30 30 74 68 20  imately 1/100th 
2f930 28 31 25 29 20 6f 66 20 74 68 65 20 72 6f 77 73  (1%) of the rows
2f931 20 73 65 6c 65 63 74 65 64 20 62 79 20 74 68 65   selected by the
2f932 20 6e 45 71 20 65 71 75 61 6c 69 74 79 0a 2a 2a   nEq equality.**
2f933 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 28 69 66   constraints (if
2f934 20 61 6e 79 29 2e 20 41 20 72 65 74 75 72 6e 20   any). A return 
2f935 76 61 6c 75 65 20 6f 66 20 31 30 30 20 69 6e 64  value of 100 ind
2f936 69 63 61 74 65 73 20 74 68 61 74 20 69 74 20 69  icates that it i
2f937 73 20 65 78 70 65 63 74 65 64 0a 2a 2a 20 74 68  s expected.** th
2f938 61 74 20 74 68 65 20 72 61 6e 67 65 20 73 63 61  at the range sca
2f939 6e 20 77 69 6c 6c 20 76 69 73 69 74 20 65 76 65  n will visit eve
2f93a 72 79 20 72 6f 77 20 28 31 30 30 25 29 20 73 65  ry row (100%) se
2f93b 6c 65 63 74 65 64 20 62 79 20 74 68 65 20 65 71  lected by the eq
2f93c 75 61 6c 69 74 79 0a 2a 2a 20 63 6f 6e 73 74 72  uality.** constr
2f93d 61 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  aints..**.** In 
2f93e 74 68 65 20 61 62 73 65 6e 63 65 20 6f 66 20 73  the absence of s
2f93f 71 6c 69 74 65 5f 73 74 61 74 32 20 41 4e 41 4c  qlite_stat2 ANAL
2f940 59 5a 45 20 64 61 74 61 2c 20 65 61 63 68 20 72  YZE data, each r
2f941 61 6e 67 65 20 69 6e 65 71 75 61 6c 69 74 79 0a  ange inequality.
2f942 2a 2a 20 72 65 64 75 63 65 73 20 74 68 65 20 73  ** reduces the s
2f943 65 61 72 63 68 20 73 70 61 63 65 20 62 79 20 32  earch space by 2
2f944 2f 33 72 64 73 2e 20 20 48 65 6e 63 65 20 61 20  /3rds.  Hence a 
2f945 73 69 6e 67 6c 65 20 63 6f 6e 73 74 72 61 69 6e  single constrain
2f946 74 20 28 78 3e 3f 29 0a 2a 2a 20 72 65 73 75 6c  t (x>?).** resul
2f947 74 73 20 69 6e 20 61 20 72 65 74 75 72 6e 20 6f  ts in a return o
2f948 66 20 33 33 20 61 6e 64 20 61 20 72 61 6e 67 65  f 33 and a range
2f949 20 63 6f 6e 73 74 72 61 69 6e 74 20 28 78 3e 3f   constraint (x>?
2f94a 20 41 4e 44 20 78 3c 3f 29 20 72 65 73 75 6c 74   AND x<?) result
2f94b 73 0a 2a 2a 20 69 6e 20 61 20 72 65 74 75 72 6e  s.** in a return
2f94c 20 6f 66 20 31 31 2e 0a 2a 2f 0a 73 74 61 74 69   of 11..*/.stati
2f94d 63 20 69 6e 74 20 77 68 65 72 65 52 61 6e 67 65  c int whereRange
2f94e 53 63 61 6e 45 73 74 28 0a 20 20 50 61 72 73 65  ScanEst(.  Parse
2f94f 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2f950 2f 2a 20 50 61 72 73 69 6e 67 20 26 20 63 6f 64  /* Parsing & cod
2f951 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
2f952 74 65 78 74 20 2a 2f 0a 20 20 49 6e 64 65 78 20  text */.  Index 
2f953 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  *p,            /
2f954 2a 20 54 68 65 20 69 6e 64 65 78 20 63 6f 6e 74  * The index cont
2f955 61 69 6e 69 6e 67 20 74 68 65 20 72 61 6e 67 65  aining the range
2f956 2d 63 6f 6d 70 61 72 65 64 20 63 6f 6c 75 6d 6e  -compared column
2f957 3b 20 22 78 22 20 2a 2f 0a 20 20 69 6e 74 20 6e  ; "x" */.  int n
2f958 45 71 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  Eq,             
2f959 2f 2a 20 69 6e 64 65 78 20 69 6e 74 6f 20 70 2d  /* index into p-
2f95a 3e 61 43 6f 6c 5b 5d 20 6f 66 20 74 68 65 20 72  >aCol[] of the r
2f95b 61 6e 67 65 2d 63 6f 6d 70 61 72 65 64 20 63 6f  ange-compared co
2f95c 6c 75 6d 6e 20 2a 2f 0a 20 20 57 68 65 72 65 54  lumn */.  WhereT
2f95d 65 72 6d 20 2a 70 4c 6f 77 65 72 2c 20 20 20 2f  erm *pLower,   /
2f95e 2a 20 4c 6f 77 65 72 20 62 6f 75 6e 64 20 6f 6e  * Lower bound on
2f95f 20 74 68 65 20 72 61 6e 67 65 2e 20 65 78 3a 20   the range. ex: 
2f960 22 78 3e 31 32 33 22 20 4d 69 67 68 74 20 62 65  "x>123" Might be
2f961 20 4e 55 4c 4c 20 2a 2f 0a 20 20 57 68 65 72 65   NULL */.  Where
2f962 54 65 72 6d 20 2a 70 55 70 70 65 72 2c 20 20 20  Term *pUpper,   
2f963 2f 2a 20 55 70 70 65 72 20 62 6f 75 6e 64 20 6f  /* Upper bound o
2f964 6e 20 74 68 65 20 72 61 6e 67 65 2e 20 65 78 3a  n the range. ex:
2f965 20 22 78 3c 34 35 35 22 20 4d 69 67 68 74 20 62   "x<455" Might b
2f966 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20  e NULL */.  int 
2f967 2a 70 69 45 73 74 20 20 20 20 20 20 20 20 20 20  *piEst          
2f968 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20   /* OUT: Return 
2f969 76 61 6c 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  value */.){.  in
2f96a 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
2f96b 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
2f96c 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 0a 20  _ENABLE_STAT2.. 
2f96d 20 69 66 28 20 6e 45 71 3d 3d 30 20 26 26 20 70   if( nEq==0 && p
2f96e 2d 3e 61 53 61 6d 70 6c 65 20 29 7b 0a 20 20 20  ->aSample ){.   
2f96f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2f970 70 4c 6f 77 65 72 56 61 6c 20 3d 20 30 3b 0a 20  pLowerVal = 0;. 
2f971 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
2f972 20 2a 70 55 70 70 65 72 56 61 6c 20 3d 20 30 3b   *pUpperVal = 0;
2f973 0a 20 20 20 20 69 6e 74 20 69 45 73 74 3b 0a 20  .    int iEst;. 
2f974 20 20 20 69 6e 74 20 69 4c 6f 77 65 72 20 3d 20     int iLower = 
2f975 30 3b 0a 20 20 20 20 69 6e 74 20 69 55 70 70 65  0;.    int iUppe
2f976 72 20 3d 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  r = SQLITE_INDEX
2f977 5f 53 41 4d 50 4c 45 53 3b 0a 20 20 20 20 75 38  _SAMPLES;.    u8
2f978 20 61 66 66 20 3d 20 70 2d 3e 70 54 61 62 6c 65   aff = p->pTable
2f979 2d 3e 61 43 6f 6c 5b 70 2d 3e 61 69 43 6f 6c 75  ->aCol[p->aiColu
2f97a 6d 6e 5b 30 5d 5d 2e 61 66 66 69 6e 69 74 79 3b  mn[0]].affinity;
2f97b 0a 0a 20 20 20 20 69 66 28 20 70 4c 6f 77 65 72  ..    if( pLower
2f97c 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   ){.      Expr *
2f97d 70 45 78 70 72 20 3d 20 70 4c 6f 77 65 72 2d 3e  pExpr = pLower->
2f97e 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
2f97f 20 20 20 20 20 72 63 20 3d 20 76 61 6c 75 65 46       rc = valueF
2f980 72 6f 6d 45 78 70 72 28 70 50 61 72 73 65 2c 20  romExpr(pParse, 
2f981 70 45 78 70 72 2c 20 61 66 66 2c 20 26 70 4c 6f  pExpr, aff, &pLo
2f982 77 65 72 56 61 6c 29 3b 0a 20 20 20 20 7d 0a 20  werVal);.    }. 
2f983 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
2f984 45 5f 4f 4b 20 26 26 20 70 55 70 70 65 72 20 29  E_OK && pUpper )
2f985 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45  {.      Expr *pE
2f986 78 70 72 20 3d 20 70 55 70 70 65 72 2d 3e 70 45  xpr = pUpper->pE
2f987 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  xpr->pRight;.   
2f988 20 20 20 72 63 20 3d 20 76 61 6c 75 65 46 72 6f     rc = valueFro
2f989 6d 45 78 70 72 28 70 50 61 72 73 65 2c 20 70 45  mExpr(pParse, pE
2f98a 78 70 72 2c 20 61 66 66 2c 20 26 70 55 70 70 65  xpr, aff, &pUppe
2f98b 72 56 61 6c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  rVal);.    }..  
2f98c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2f98d 5f 4f 4b 20 7c 7c 20 28 70 4c 6f 77 65 72 56 61  _OK || (pLowerVa
2f98e 6c 3d 3d 30 20 26 26 20 70 55 70 70 65 72 56 61  l==0 && pUpperVa
2f98f 6c 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 73  l==0) ){.      s
2f990 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
2f991 70 4c 6f 77 65 72 56 61 6c 29 3b 0a 20 20 20 20  pLowerVal);.    
2f992 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
2f993 65 65 28 70 55 70 70 65 72 56 61 6c 29 3b 0a 20  ee(pUpperVal);. 
2f994 20 20 20 20 20 67 6f 74 6f 20 72 61 6e 67 65 5f       goto range_
2f995 65 73 74 5f 66 61 6c 6c 62 61 63 6b 3b 0a 20 20  est_fallback;.  
2f996 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 6f 77    }else if( pLow
2f997 65 72 56 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  erVal==0 ){.    
2f998 20 20 72 63 20 3d 20 77 68 65 72 65 52 61 6e 67    rc = whereRang
2f999 65 52 65 67 69 6f 6e 28 70 50 61 72 73 65 2c 20  eRegion(pParse, 
2f99a 70 2c 20 70 55 70 70 65 72 56 61 6c 2c 20 26 69  p, pUpperVal, &i
2f99b 55 70 70 65 72 29 3b 0a 20 20 20 20 20 20 69 66  Upper);.      if
2f99c 28 20 70 4c 6f 77 65 72 20 29 20 69 4c 6f 77 65  ( pLower ) iLowe
2f99d 72 20 3d 20 69 55 70 70 65 72 2f 32 3b 0a 20 20  r = iUpper/2;.  
2f99e 20 20 7d 65 6c 73 65 20 69 66 28 20 70 55 70 70    }else if( pUpp
2f99f 65 72 56 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  erVal==0 ){.    
2f9a0 20 20 72 63 20 3d 20 77 68 65 72 65 52 61 6e 67    rc = whereRang
2f9a1 65 52 65 67 69 6f 6e 28 70 50 61 72 73 65 2c 20  eRegion(pParse, 
2f9a2 70 2c 20 70 4c 6f 77 65 72 56 61 6c 2c 20 26 69  p, pLowerVal, &i
2f9a3 4c 6f 77 65 72 29 3b 0a 20 20 20 20 20 20 69 66  Lower);.      if
2f9a4 28 20 70 55 70 70 65 72 20 29 20 69 55 70 70 65  ( pUpper ) iUppe
2f9a5 72 20 3d 20 28 69 4c 6f 77 65 72 20 2b 20 53 51  r = (iLower + SQ
2f9a6 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c  LITE_INDEX_SAMPL
2f9a7 45 53 20 2b 20 31 29 2f 32 3b 0a 20 20 20 20 7d  ES + 1)/2;.    }
2f9a8 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
2f9a9 20 77 68 65 72 65 52 61 6e 67 65 52 65 67 69 6f   whereRangeRegio
2f9aa 6e 28 70 50 61 72 73 65 2c 20 70 2c 20 70 55 70  n(pParse, p, pUp
2f9ab 70 65 72 56 61 6c 2c 20 26 69 55 70 70 65 72 29  perVal, &iUpper)
2f9ac 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
2f9ad 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
2f9ae 20 20 20 20 20 72 63 20 3d 20 77 68 65 72 65 52       rc = whereR
2f9af 61 6e 67 65 52 65 67 69 6f 6e 28 70 50 61 72 73  angeRegion(pPars
2f9b0 65 2c 20 70 2c 20 70 4c 6f 77 65 72 56 61 6c 2c  e, p, pLowerVal,
2f9b1 20 26 69 4c 6f 77 65 72 29 3b 0a 20 20 20 20 20   &iLower);.     
2f9b2 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 45   }.    }..    iE
2f9b3 73 74 20 3d 20 69 55 70 70 65 72 20 2d 20 69 4c  st = iUpper - iL
2f9b4 6f 77 65 72 3b 0a 20 20 20 20 74 65 73 74 63 61  ower;.    testca
2f9b5 73 65 28 20 69 45 73 74 3d 3d 53 51 4c 49 54 45  se( iEst==SQLITE
2f9b6 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 29  _INDEX_SAMPLES )
2f9b7 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 45  ;.    assert( iE
2f9b8 73 74 3c 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58  st<=SQLITE_INDEX
2f9b9 5f 53 41 4d 50 4c 45 53 20 29 3b 0a 20 20 20 20  _SAMPLES );.    
2f9ba 69 66 28 20 69 45 73 74 3c 31 20 29 7b 0a 20 20  if( iEst<1 ){.  
2f9bb 20 20 20 20 69 45 73 74 20 3d 20 31 3b 0a 20 20      iEst = 1;.  
2f9bc 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
2f9bd 56 61 6c 75 65 46 72 65 65 28 70 4c 6f 77 65 72  ValueFree(pLower
2f9be 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Val);.    sqlite
2f9bf 33 56 61 6c 75 65 46 72 65 65 28 70 55 70 70 65  3ValueFree(pUppe
2f9c0 72 56 61 6c 29 3b 0a 20 20 20 20 2a 70 69 45 73  rVal);.    *piEs
2f9c1 74 20 3d 20 28 69 45 73 74 20 2a 20 31 30 30 29  t = (iEst * 100)
2f9c2 2f 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41  /SQLITE_INDEX_SA
2f9c3 4d 50 4c 45 53 3b 0a 20 20 20 20 72 65 74 75 72  MPLES;.    retur
2f9c4 6e 20 72 63 3b 0a 20 20 7d 0a 72 61 6e 67 65 5f  n rc;.  }.range_
2f9c5 65 73 74 5f 66 61 6c 6c 62 61 63 6b 3a 0a 23 65  est_fallback:.#e
2f9c6 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  lse.  UNUSED_PAR
2f9c7 41 4d 45 54 45 52 28 70 50 61 72 73 65 29 3b 0a  AMETER(pParse);.
2f9c8 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
2f9c9 45 52 28 70 29 3b 0a 20 20 55 4e 55 53 45 44 5f  ER(p);.  UNUSED_
2f9ca 50 41 52 41 4d 45 54 45 52 28 6e 45 71 29 3b 0a  PARAMETER(nEq);.
2f9cb 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74 28  #endif.  assert(
2f9cc 20 70 4c 6f 77 65 72 20 7c 7c 20 70 55 70 70 65   pLower || pUppe
2f9cd 72 20 29 3b 0a 20 20 69 66 28 20 70 4c 6f 77 65  r );.  if( pLowe
2f9ce 72 20 26 26 20 70 55 70 70 65 72 20 29 7b 0a 20  r && pUpper ){. 
2f9cf 20 20 20 2a 70 69 45 73 74 20 3d 20 31 31 3b 0a     *piEst = 11;.
2f9d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 69    }else{.    *pi
2f9d1 45 73 74 20 3d 20 33 33 3b 0a 20 20 7d 0a 20 20  Est = 33;.  }.  
2f9d2 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
2f9d3 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 71 75  *.** Find the qu
2f9d4 65 72 79 20 70 6c 61 6e 20 66 6f 72 20 61 63 63  ery plan for acc
2f9d5 65 73 73 69 6e 67 20 61 20 70 61 72 74 69 63 75  essing a particu
2f9d6 6c 61 72 20 74 61 62 6c 65 2e 20 20 57 72 69 74  lar table.  Writ
2f9d7 65 20 74 68 65 0a 2a 2a 20 62 65 73 74 20 71 75  e the.** best qu
2f9d8 65 72 79 20 70 6c 61 6e 20 61 6e 64 20 69 74 73  ery plan and its
2f9d9 20 63 6f 73 74 20 69 6e 74 6f 20 74 68 65 20 57   cost into the W
2f9da 68 65 72 65 43 6f 73 74 20 6f 62 6a 65 63 74 20  hereCost object 
2f9db 73 75 70 70 6c 69 65 64 20 61 73 20 74 68 65 0a  supplied as the.
2f9dc 2a 2a 20 6c 61 73 74 20 70 61 72 61 6d 65 74 65  ** last paramete
2f9dd 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 6f 77  r..**.** The low
2f9de 65 73 74 20 63 6f 73 74 20 70 6c 61 6e 20 77 69  est cost plan wi
2f9df 6e 73 2e 20 20 54 68 65 20 63 6f 73 74 20 69 73  ns.  The cost is
2f9e0 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20   an estimate of 
2f9e1 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a 2a  the amount of.**
2f9e2 20 43 50 55 20 61 6e 64 20 64 69 73 6b 20 49 2f   CPU and disk I/
2f9e3 4f 20 6e 65 65 64 20 74 6f 20 70 72 6f 63 65 73  O need to proces
2f9e4 73 20 74 68 65 20 72 65 71 75 65 73 74 20 75 73  s the request us
2f9e5 69 6e 67 20 74 68 65 20 73 65 6c 65 63 74 65 64  ing the selected
2f9e6 20 70 6c 61 6e 2e 0a 2a 2a 20 46 61 63 74 6f 72   plan..** Factor
2f9e7 73 20 74 68 61 74 20 69 6e 66 6c 75 65 6e 63 65  s that influence
2f9e8 20 63 6f 73 74 20 69 6e 63 6c 75 64 65 3a 0a 2a   cost include:.*
2f9e9 2a 0a 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 65  *.**    *  The e
2f9ea 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65 72 20  stimated number 
2f9eb 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c  of rows that wil
2f9ec 6c 20 62 65 20 72 65 74 72 69 65 76 65 64 2e 20  l be retrieved. 
2f9ed 20 28 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 66   (The.**       f
2f9ee 65 77 65 72 20 74 68 65 20 62 65 74 74 65 72 2e  ewer the better.
2f9ef 29 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 57 68  ).**.**    *  Wh
2f9f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 6f 72  ether or not sor
2f9f1 74 69 6e 67 20 6d 75 73 74 20 6f 63 63 75 72 2e  ting must occur.
2f9f2 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 57 68 65  .**.**    *  Whe
2f9f3 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 72  ther or not ther
2f9f4 65 20 6d 75 73 74 20 62 65 20 73 65 70 61 72 61  e must be separa
2f9f5 74 65 20 6c 6f 6f 6b 75 70 73 20 69 6e 20 74 68  te lookups in th
2f9f6 65 0a 2a 2a 20 20 20 20 20 20 20 69 6e 64 65 78  e.**       index
2f9f7 20 61 6e 64 20 69 6e 20 74 68 65 20 6d 61 69 6e   and in the main
2f9f8 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   table..**.** If
2f9f9 20 74 68 65 72 65 20 77 61 73 20 61 6e 20 49 4e   there was an IN
2f9fa 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65 20  DEXED BY clause 
2f9fb 28 70 53 72 63 2d 3e 70 49 6e 64 65 78 29 20 61  (pSrc->pIndex) a
2f9fc 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 74  ttached to the t
2f9fd 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68 65 20 53  able in.** the S
2f9fe 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  QL statement, th
2f9ff 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
2fa00 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72 73 20   only considers 
2fa01 70 6c 61 6e 73 20 75 73 69 6e 67 20 74 68 65 20  plans using the 
2fa02 0a 2a 2a 20 6e 61 6d 65 64 20 69 6e 64 65 78 2e  .** named index.
2fa03 20 49 66 20 6e 6f 20 73 75 63 68 20 70 6c 61 6e   If no such plan
2fa04 20 69 73 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20   is found, then 
2fa05 74 68 65 20 72 65 74 75 72 6e 65 64 20 63 6f 73  the returned cos
2fa06 74 20 69 73 0a 2a 2a 20 53 51 4c 49 54 45 5f 42  t is.** SQLITE_B
2fa07 49 47 5f 44 42 4c 2e 20 49 66 20 61 20 70 6c 61  IG_DBL. If a pla
2fa08 6e 20 69 73 20 66 6f 75 6e 64 20 74 68 61 74 20  n is found that 
2fa09 75 73 65 73 20 74 68 65 20 6e 61 6d 65 64 20 69  uses the named i
2fa0a 6e 64 65 78 2c 20 0a 2a 2a 20 74 68 65 6e 20 74  ndex, .** then t
2fa0b 68 65 20 63 6f 73 74 20 69 73 20 63 61 6c 63 75  he cost is calcu
2fa0c 6c 61 74 65 64 20 69 6e 20 74 68 65 20 75 73 75  lated in the usu
2fa0d 61 6c 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 49 66  al way..**.** If
2fa0e 20 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63   a NOT INDEXED c
2fa0f 6c 61 75 73 65 20 28 70 53 72 63 2d 3e 6e 6f 74  lause (pSrc->not
2fa10 49 6e 64 65 78 65 64 21 3d 30 29 20 77 61 73 20  Indexed!=0) was 
2fa11 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20  attached to the 
2fa12 74 61 62 6c 65 20 0a 2a 2a 20 69 6e 20 74 68 65  table .** in the
2fa13 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2fa14 74 2c 20 74 68 65 6e 20 6e 6f 20 69 6e 64 65 78  t, then no index
2fa15 65 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65  es are considere
2fa16 64 2e 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20  d. However, the 
2fa17 0a 2a 2a 20 73 65 6c 65 63 74 65 64 20 70 6c 61  .** selected pla
2fa18 6e 20 6d 61 79 20 73 74 69 6c 6c 20 74 61 6b 65  n may still take
2fa19 20 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68   advantage of th
2fa1a 65 20 74 61 62 6c 65 73 20 62 75 69 6c 74 2d 69  e tables built-i
2fa1b 6e 20 72 6f 77 69 64 0a 2a 2a 20 69 6e 64 65 78  n rowid.** index
2fa1c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2fa1d 20 62 65 73 74 42 74 72 65 65 49 6e 64 65 78 28   bestBtreeIndex(
2fa1e 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2fa1f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2fa20 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
2fa21 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65  ntext */.  Where
2fa22 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20  Clause *pWC,    
2fa23 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
2fa24 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2fa25 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2fa26 74 65 6d 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54  tem *pSrc,  /* T
2fa27 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74  he FROM clause t
2fa28 65 72 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f  erm to search */
2fa29 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65  .  Bitmask notRe
2fa2a 61 64 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ady,           /
2fa2b 2a 20 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72  * Mask of cursor
2fa2c 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61  s that are not a
2fa2d 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78  vailable */.  Ex
2fa2e 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
2fa2f 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2fa30 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2fa31 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f 73 74 20   */.  WhereCost 
2fa32 2a 70 43 6f 73 74 20 20 20 20 20 20 20 20 20 20  *pCost          
2fa33 20 20 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74    /* Lowest cost
2fa34 20 71 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0a 29   query plan */.)
2fa35 7b 0a 20 20 69 6e 74 20 69 43 75 72 20 3d 20 70  {.  int iCur = p
2fa36 53 72 63 2d 3e 69 43 75 72 73 6f 72 3b 20 20 20  Src->iCursor;   
2fa37 2f 2a 20 54 68 65 20 63 75 72 73 6f 72 20 6f 66  /* The cursor of
2fa38 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65   the table to be
2fa39 20 61 63 63 65 73 73 65 64 20 2a 2f 0a 20 20 49   accessed */.  I
2fa3a 6e 64 65 78 20 2a 70 50 72 6f 62 65 3b 20 20 20  ndex *pProbe;   
2fa3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e             /* An
2fa3c 20 69 6e 64 65 78 20 77 65 20 61 72 65 20 65 76   index we are ev
2fa3d 61 6c 75 61 74 69 6e 67 20 2a 2f 0a 20 20 49 6e  aluating */.  In
2fa3e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20  dex *pIdx;      
2fa3f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70            /* Cop
2fa40 79 20 6f 66 20 70 50 72 6f 62 65 2c 20 6f 72 20  y of pProbe, or 
2fa41 7a 65 72 6f 20 66 6f 72 20 49 50 4b 20 69 6e 64  zero for IPK ind
2fa42 65 78 20 2a 2f 0a 20 20 69 6e 74 20 65 71 54 65  ex */.  int eqTe
2fa43 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  rmMask;         
2fa44 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6d      /* Current m
2fa45 61 73 6b 20 6f 66 20 76 61 6c 69 64 20 65 71 75  ask of valid equ
2fa46 61 6c 69 74 79 20 6f 70 65 72 61 74 6f 72 73 20  ality operators 
2fa47 2a 2f 0a 20 20 69 6e 74 20 69 64 78 45 71 54 65  */.  int idxEqTe
2fa48 72 6d 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  rmMask;         
2fa49 20 2f 2a 20 49 6e 64 65 78 20 6d 61 73 6b 20 6f   /* Index mask o
2fa4a 66 20 76 61 6c 69 64 20 65 71 75 61 6c 69 74 79  f valid equality
2fa4b 20 6f 70 65 72 61 74 6f 72 73 20 2a 2f 0a 20 20   operators */.  
2fa4c 49 6e 64 65 78 20 73 50 6b 3b 20 20 20 20 20 20  Index sPk;      
2fa4d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2fa4e 20 66 61 6b 65 20 69 6e 64 65 78 20 6f 62 6a 65   fake index obje
2fa4f 63 74 20 66 6f 72 20 74 68 65 20 70 72 69 6d 61  ct for the prima
2fa50 72 79 20 6b 65 79 20 2a 2f 0a 20 20 75 6e 73 69  ry key */.  unsi
2fa51 67 6e 65 64 20 69 6e 74 20 61 69 52 6f 77 45 73  gned int aiRowEs
2fa52 74 50 6b 5b 32 5d 3b 20 2f 2a 20 54 68 65 20 61  tPk[2]; /* The a
2fa53 69 52 6f 77 45 73 74 5b 5d 20 76 61 6c 75 65 20  iRowEst[] value 
2fa54 66 6f 72 20 74 68 65 20 73 50 6b 20 69 6e 64 65  for the sPk inde
2fa55 78 20 2a 2f 0a 20 20 69 6e 74 20 61 69 43 6f 6c  x */.  int aiCol
2fa56 75 6d 6e 50 6b 20 3d 20 2d 31 3b 20 20 20 20 20  umnPk = -1;     
2fa57 20 20 20 2f 2a 20 54 68 65 20 61 43 6f 6c 75 6d     /* The aColum
2fa58 6e 5b 5d 20 76 61 6c 75 65 20 66 6f 72 20 74 68  n[] value for th
2fa59 65 20 73 50 6b 20 69 6e 64 65 78 20 2a 2f 0a 20  e sPk index */. 
2fa5a 20 69 6e 74 20 77 73 46 6c 61 67 4d 61 73 6b 3b   int wsFlagMask;
2fa5b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2fa5c 41 6c 6c 6f 77 65 64 20 66 6c 61 67 73 20 69 6e  Allowed flags in
2fa5d 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46   pCost->plan.wsF
2fa5e 6c 61 67 20 2a 2f 0a 0a 20 20 2f 2a 20 49 6e 69  lag */..  /* Ini
2fa5f 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 73 74  tialize the cost
2fa60 20 74 6f 20 61 20 77 6f 72 73 74 2d 63 61 73 65   to a worst-case
2fa61 20 76 61 6c 75 65 20 2a 2f 0a 20 20 6d 65 6d 73   value */.  mems
2fa62 65 74 28 70 43 6f 73 74 2c 20 30 2c 20 73 69 7a  et(pCost, 0, siz
2fa63 65 6f 66 28 2a 70 43 6f 73 74 29 29 3b 0a 20 20  eof(*pCost));.  
2fa64 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 53  pCost->rCost = S
2fa65 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 3b 0a 0a  QLITE_BIG_DBL;..
2fa66 20 20 2f 2a 20 49 66 20 74 68 65 20 70 53 72 63    /* If the pSrc
2fa67 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 72 69   table is the ri
2fa68 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 4c  ght table of a L
2fa69 45 46 54 20 4a 4f 49 4e 20 74 68 65 6e 20 77 65  EFT JOIN then we
2fa6a 20 6d 61 79 20 6e 6f 74 0a 20 20 2a 2a 20 75 73   may not.  ** us
2fa6b 65 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 73 61  e an index to sa
2fa6c 74 69 73 66 79 20 49 53 20 4e 55 4c 4c 20 63 6f  tisfy IS NULL co
2fa6d 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 74 68 61  nstraints on tha
2fa6e 74 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69  t table.  This i
2fa6f 73 0a 20 20 2a 2a 20 62 65 63 61 75 73 65 20 63  s.  ** because c
2fa70 6f 6c 75 6d 6e 73 20 6d 69 67 68 74 20 65 6e 64  olumns might end
2fa71 20 75 70 20 62 65 69 6e 67 20 4e 55 4c 4c 20 69   up being NULL i
2fa72 66 20 74 68 65 20 74 61 62 6c 65 20 64 6f 65 73  f the table does
2fa73 20 6e 6f 74 20 6d 61 74 63 68 20 2d 0a 20 20 2a   not match -.  *
2fa74 2a 20 61 20 63 69 72 63 75 6d 73 74 61 6e 63 65  * a circumstance
2fa75 20 77 68 69 63 68 20 74 68 65 20 69 6e 64 65 78   which the index
2fa76 20 63 61 6e 6e 6f 74 20 68 65 6c 70 20 75 73 20   cannot help us 
2fa77 64 69 73 63 6f 76 65 72 2e 20 20 54 69 63 6b 65  discover.  Ticke
2fa78 74 20 23 32 31 37 37 2e 0a 20 20 2a 2f 0a 20 20  t #2177..  */.  
2fa79 69 66 28 20 70 53 72 63 2d 3e 6a 6f 69 6e 74 79  if( pSrc->jointy
2fa7a 70 65 20 26 20 4a 54 5f 4c 45 46 54 20 29 7b 0a  pe & JT_LEFT ){.
2fa7b 20 20 20 20 69 64 78 45 71 54 65 72 6d 4d 61 73      idxEqTermMas
2fa7c 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 3b  k = WO_EQ|WO_IN;
2fa7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 64  .  }else{.    id
2fa7e 78 45 71 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f  xEqTermMask = WO
2fa7f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e  _EQ|WO_IN|WO_ISN
2fa80 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  ULL;.  }..  if( 
2fa81 70 53 72 63 2d 3e 70 49 6e 64 65 78 20 29 7b 0a  pSrc->pIndex ){.
2fa82 20 20 20 20 2f 2a 20 41 6e 20 49 4e 44 45 58 45      /* An INDEXE
2fa83 44 20 42 59 20 63 6c 61 75 73 65 20 73 70 65 63  D BY clause spec
2fa84 69 66 69 65 73 20 61 20 70 61 72 74 69 63 75 6c  ifies a particul
2fa85 61 72 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20  ar index to use 
2fa86 2a 2f 0a 20 20 20 20 70 49 64 78 20 3d 20 70 50  */.    pIdx = pP
2fa87 72 6f 62 65 20 3d 20 70 53 72 63 2d 3e 70 49 6e  robe = pSrc->pIn
2fa88 64 65 78 3b 0a 20 20 20 20 77 73 46 6c 61 67 4d  dex;.    wsFlagM
2fa89 61 73 6b 20 3d 20 7e 28 57 48 45 52 45 5f 52 4f  ask = ~(WHERE_RO
2fa8a 57 49 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57  WID_EQ|WHERE_ROW
2fa8b 49 44 5f 52 41 4e 47 45 29 3b 0a 20 20 20 20 65  ID_RANGE);.    e
2fa8c 71 54 65 72 6d 4d 61 73 6b 20 3d 20 69 64 78 45  qTermMask = idxE
2fa8d 71 54 65 72 6d 4d 61 73 6b 3b 0a 20 20 7d 65 6c  qTermMask;.  }el
2fa8e 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72 65  se{.    /* There
2fa8f 20 69 73 20 6e 6f 20 49 4e 44 45 58 45 44 20 42   is no INDEXED B
2fa90 59 20 63 6c 61 75 73 65 2e 20 20 43 72 65 61 74  Y clause.  Creat
2fa91 65 20 61 20 66 61 6b 65 20 49 6e 64 65 78 20 6f  e a fake Index o
2fa92 62 6a 65 63 74 20 74 6f 0a 20 20 20 20 2a 2a 20  bject to.    ** 
2fa93 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 70 72  represent the pr
2fa94 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20 20 20  imary key */.   
2fa95 20 49 6e 64 65 78 20 2a 70 46 69 72 73 74 3b 20   Index *pFirst; 
2fa96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2fa97 2a 20 41 6e 79 20 6f 74 68 65 72 20 69 6e 64 65  * Any other inde
2fa98 78 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 2a  x on the table *
2fa99 2f 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 50  /.    memset(&sP
2fa9a 6b 2c 20 30 2c 20 73 69 7a 65 6f 66 28 49 6e 64  k, 0, sizeof(Ind
2fa9b 65 78 29 29 3b 0a 20 20 20 20 73 50 6b 2e 6e 43  ex));.    sPk.nC
2fa9c 6f 6c 75 6d 6e 20 3d 20 31 3b 0a 20 20 20 20 73  olumn = 1;.    s
2fa9d 50 6b 2e 61 69 43 6f 6c 75 6d 6e 20 3d 20 26 61  Pk.aiColumn = &a
2fa9e 69 43 6f 6c 75 6d 6e 50 6b 3b 0a 20 20 20 20 73  iColumnPk;.    s
2fa9f 50 6b 2e 61 69 52 6f 77 45 73 74 20 3d 20 61 69  Pk.aiRowEst = ai
2faa0 52 6f 77 45 73 74 50 6b 3b 0a 20 20 20 20 61 69  RowEstPk;.    ai
2faa1 52 6f 77 45 73 74 50 6b 5b 31 5d 20 3d 20 31 3b  RowEstPk[1] = 1;
2faa2 0a 20 20 20 20 73 50 6b 2e 6f 6e 45 72 72 6f 72  .    sPk.onError
2faa3 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 0a 20   = OE_Replace;. 
2faa4 20 20 20 73 50 6b 2e 70 54 61 62 6c 65 20 3d 20     sPk.pTable = 
2faa5 70 53 72 63 2d 3e 70 54 61 62 3b 0a 20 20 20 20  pSrc->pTab;.    
2faa6 70 46 69 72 73 74 20 3d 20 70 53 72 63 2d 3e 70  pFirst = pSrc->p
2faa7 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20  Tab->pIndex;.   
2faa8 20 69 66 28 20 70 53 72 63 2d 3e 6e 6f 74 49 6e   if( pSrc->notIn
2faa9 64 65 78 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  dexed==0 ){.    
2faaa 20 20 73 50 6b 2e 70 4e 65 78 74 20 3d 20 70 46    sPk.pNext = pF
2faab 69 72 73 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  irst;.    }.    
2faac 2f 2a 20 54 68 65 20 61 69 52 6f 77 45 73 74 50  /* The aiRowEstP
2faad 6b 5b 30 5d 20 69 73 20 61 6e 20 65 73 74 69 6d  k[0] is an estim
2faae 61 74 65 20 6f 66 20 74 68 65 20 74 6f 74 61 6c  ate of the total
2faaf 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
2fab0 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 61  in the.    ** ta
2fab1 62 6c 65 2e 20 20 47 65 74 20 74 68 69 73 20 69  ble.  Get this i
2fab2 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
2fab3 74 68 65 20 41 4e 41 4c 59 5a 45 20 69 6e 66 6f  the ANALYZE info
2fab4 72 6d 61 74 69 6f 6e 20 69 66 20 69 74 20 69 73  rmation if it is
2fab5 0a 20 20 20 20 2a 2a 20 61 76 61 69 6c 61 62 6c  .    ** availabl
2fab6 65 2e 20 20 49 66 20 6e 6f 74 20 61 76 61 69 6c  e.  If not avail
2fab7 61 62 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 65  able, assume the
2fab8 20 74 61 62 6c 65 20 31 20 6d 69 6c 6c 69 6f 6e   table 1 million
2fab9 20 72 6f 77 73 20 69 6e 20 73 69 7a 65 2e 0a 20   rows in size.. 
2faba 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 46     */.    if( pF
2fabb 69 72 73 74 20 29 7b 0a 20 20 20 20 20 20 61 73  irst ){.      as
2fabc 73 65 72 74 28 20 70 46 69 72 73 74 2d 3e 61 69  sert( pFirst->ai
2fabd 52 6f 77 45 73 74 21 3d 30 20 29 3b 20 2f 2a 20  RowEst!=0 ); /* 
2fabe 41 6c 6c 6f 63 61 74 65 64 20 74 6f 67 65 74 68  Allocated togeth
2fabf 65 72 20 77 69 74 68 20 70 46 69 72 73 74 20 2a  er with pFirst *
2fac0 2f 0a 20 20 20 20 20 20 61 69 52 6f 77 45 73 74  /.      aiRowEst
2fac1 50 6b 5b 30 5d 20 3d 20 70 46 69 72 73 74 2d 3e  Pk[0] = pFirst->
2fac2 61 69 52 6f 77 45 73 74 5b 30 5d 3b 0a 20 20 20  aiRowEst[0];.   
2fac3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 69   }else{.      ai
2fac4 52 6f 77 45 73 74 50 6b 5b 30 5d 20 3d 20 31 30  RowEstPk[0] = 10
2fac5 30 30 30 30 30 3b 0a 20 20 20 20 7d 0a 20 20 20  00000;.    }.   
2fac6 20 70 50 72 6f 62 65 20 3d 20 26 73 50 6b 3b 0a   pProbe = &sPk;.
2fac7 20 20 20 20 77 73 46 6c 61 67 4d 61 73 6b 20 3d      wsFlagMask =
2fac8 20 7e 28 0a 20 20 20 20 20 20 20 20 57 48 45 52   ~(.        WHER
2fac9 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57 48 45 52  E_COLUMN_IN|WHER
2faca 45 5f 43 4f 4c 55 4d 4e 5f 45 51 7c 57 48 45 52  E_COLUMN_EQ|WHER
2facb 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 7c 57 48  E_COLUMN_NULL|WH
2facc 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45  ERE_COLUMN_RANGE
2facd 0a 20 20 20 20 29 3b 0a 20 20 20 20 65 71 54 65  .    );.    eqTe
2face 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57  rmMask = WO_EQ|W
2facf 4f 5f 49 4e 3b 0a 20 20 20 20 70 49 64 78 20 3d  O_IN;.    pIdx =
2fad0 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f   0;.  }..  /* Lo
2fad1 6f 70 20 6f 76 65 72 20 61 6c 6c 20 69 6e 64 69  op over all indi
2fad2 63 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  ces looking for 
2fad3 74 68 65 20 62 65 73 74 20 6f 6e 65 20 74 6f 20  the best one to 
2fad4 75 73 65 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 3b  use.  */.  for(;
2fad5 20 70 50 72 6f 62 65 3b 20 70 49 64 78 3d 70 50   pProbe; pIdx=pP
2fad6 72 6f 62 65 3d 70 50 72 6f 62 65 2d 3e 70 4e 65  robe=pProbe->pNe
2fad7 78 74 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75  xt){.    const u
2fad8 6e 73 69 67 6e 65 64 20 69 6e 74 20 2a 20 63 6f  nsigned int * co
2fad9 6e 73 74 20 61 69 52 6f 77 45 73 74 20 3d 20 70  nst aiRowEst = p
2fada 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45 73 74 3b  Probe->aiRowEst;
2fadb 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 6f 73 74  .    double cost
2fadc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2fadd 20 2f 2a 20 43 6f 73 74 20 6f 66 20 75 73 69 6e   /* Cost of usin
2fade 67 20 70 50 72 6f 62 65 20 2a 2f 0a 20 20 20 20  g pProbe */.    
2fadf 64 6f 75 62 6c 65 20 6e 52 6f 77 3b 20 20 20 20  double nRow;    
2fae0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
2fae1 73 74 69 6d 61 74 65 64 20 6e 75 6d 62 65 72 20  stimated number 
2fae2 6f 66 20 72 6f 77 73 20 69 6e 20 72 65 73 75 6c  of rows in resul
2fae3 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 69 6e 74  t set */.    int
2fae4 20 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20   rev;           
2fae5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
2fae6 20 74 6f 20 73 63 61 6e 20 69 6e 20 72 65 76 65   to scan in reve
2fae7 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20  rse order */.   
2fae8 20 69 6e 74 20 77 73 46 6c 61 67 73 20 3d 20 30   int wsFlags = 0
2fae9 3b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 75 73  ;.    Bitmask us
2faea 65 64 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20  ed = 0;..    /* 
2faeb 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
2faec 72 69 61 62 6c 65 73 20 61 72 65 20 70 6f 70 75  riables are popu
2faed 6c 61 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74  lated based on t
2faee 68 65 20 70 72 6f 70 65 72 74 69 65 73 20 6f 66  he properties of
2faef 0a 20 20 20 20 2a 2a 20 73 63 61 6e 20 62 65 69  .    ** scan bei
2faf0 6e 67 20 65 76 61 6c 75 61 74 65 64 2e 20 54 68  ng evaluated. Th
2faf1 65 79 20 61 72 65 20 74 68 65 6e 20 75 73 65 64  ey are then used
2faf2 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 68   to determine th
2faf3 65 20 65 78 70 65 63 74 65 64 0a 20 20 20 20 2a  e expected.    *
2faf4 2a 20 63 6f 73 74 20 61 6e 64 20 6e 75 6d 62 65  * cost and numbe
2faf5 72 20 6f 66 20 72 6f 77 73 20 72 65 74 75 72 6e  r of rows return
2faf6 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ed..    **.    *
2faf7 2a 20 20 6e 45 71 3a 20 0a 20 20 20 20 2a 2a 20  *  nEq: .    ** 
2faf8 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 65 71 75     Number of equ
2faf9 61 6c 69 74 79 20 74 65 72 6d 73 20 74 68 61 74  ality terms that
2fafa 20 63 61 6e 20 62 65 20 69 6d 70 6c 65 6d 65 6e   can be implemen
2fafb 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 69 6e  ted using the in
2fafc 64 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  dex..    **.    
2fafd 2a 2a 20 20 6e 49 6e 4d 75 6c 3a 20 20 0a 20 20  **  nInMul:  .  
2fafe 20 20 2a 2a 20 20 20 20 54 68 65 20 22 69 6e 2d    **    The "in-
2faff 6d 75 6c 74 69 70 6c 69 65 72 22 2e 20 54 68 69  multiplier". Thi
2fb00 73 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65  s is an estimate
2fb01 20 6f 66 20 68 6f 77 20 6d 61 6e 79 20 73 65 65   of how many see
2fb02 6b 20 6f 70 65 72 61 74 69 6f 6e 73 20 0a 20 20  k operations .  
2fb03 20 20 2a 2a 20 20 20 20 53 51 4c 69 74 65 20 6d    **    SQLite m
2fb04 75 73 74 20 70 65 72 66 6f 72 6d 20 6f 6e 20 74  ust perform on t
2fb05 68 65 20 69 6e 64 65 78 20 69 6e 20 71 75 65 73  he index in ques
2fb06 74 69 6f 6e 2e 20 46 6f 72 20 65 78 61 6d 70 6c  tion. For exampl
2fb07 65 2c 20 69 66 20 74 68 65 20 0a 20 20 20 20 2a  e, if the .    *
2fb08 2a 20 20 20 20 57 48 45 52 45 20 63 6c 61 75 73  *    WHERE claus
2fb09 65 20 69 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  e is:.    **.   
2fb0a 20 2a 2a 20 20 20 20 20 20 57 48 45 52 45 20 61   **      WHERE a
2fb0b 20 49 4e 20 28 31 2c 20 32 2c 20 33 29 20 41 4e   IN (1, 2, 3) AN
2fb0c 44 20 62 20 49 4e 20 28 34 2c 20 35 2c 20 36 29  D b IN (4, 5, 6)
2fb0d 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2fb0e 20 20 53 51 4c 69 74 65 20 6d 75 73 74 20 70 65    SQLite must pe
2fb0f 72 66 6f 72 6d 20 39 20 6c 6f 6f 6b 75 70 73 20  rform 9 lookups 
2fb10 6f 6e 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 28  on an index on (
2fb11 61 2c 20 62 29 2c 20 73 6f 20 6e 49 6e 4d 75 6c  a, b), so nInMul
2fb12 20 69 73 20 0a 20 20 20 20 2a 2a 20 20 20 20 73   is .    **    s
2fb13 65 74 20 74 6f 20 39 2e 20 47 69 76 65 6e 20 74  et to 9. Given t
2fb14 68 65 20 73 61 6d 65 20 73 63 68 65 6d 61 20 61  he same schema a
2fb15 6e 64 20 65 69 74 68 65 72 20 6f 66 20 74 68 65  nd either of the
2fb16 20 66 6f 6c 6c 6f 77 69 6e 67 20 57 48 45 52 45   following WHERE
2fb17 20 0a 20 20 20 20 2a 2a 20 20 20 20 63 6c 61 75   .    **    clau
2fb18 73 65 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ses:.    **.    
2fb19 2a 2a 20 20 20 20 20 20 57 48 45 52 45 20 61 20  **      WHERE a 
2fb1a 3d 20 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20  =  1.    **     
2fb1b 20 57 48 45 52 45 20 61 20 3e 3d 20 32 0a 20 20   WHERE a >= 2.  
2fb1c 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 6e    **.    **    n
2fb1d 49 6e 4d 75 6c 20 69 73 20 73 65 74 20 74 6f 20  InMul is set to 
2fb1e 31 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  1..    **.    **
2fb1f 20 20 20 20 49 66 20 74 68 65 72 65 20 65 78 69      If there exi
2fb20 73 74 73 20 61 20 57 48 45 52 45 20 74 65 72 6d  sts a WHERE term
2fb21 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 20   of the form "x 
2fb22 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22  IN (SELECT ...)"
2fb23 2c 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a 20 20  , then .    **  
2fb24 20 20 74 68 65 20 73 75 62 2d 73 65 6c 65 63 74    the sub-select
2fb25 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 72   is assumed to r
2fb26 65 74 75 72 6e 20 32 35 20 72 6f 77 73 20 66 6f  eturn 25 rows fo
2fb27 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f  r the purposes o
2fb28 66 20 0a 20 20 20 20 2a 2a 20 20 20 20 64 65 74  f .    **    det
2fb29 65 72 6d 69 6e 69 6e 67 20 6e 49 6e 4d 75 6c 2e  ermining nInMul.
2fb2a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2fb2b 62 49 6e 45 73 74 3a 20 20 0a 20 20 20 20 2a 2a  bInEst:  .    **
2fb2c 20 20 20 20 53 65 74 20 74 6f 20 74 72 75 65 20      Set to true 
2fb2d 69 66 20 74 68 65 72 65 20 77 61 73 20 61 74 20  if there was at 
2fb2e 6c 65 61 73 74 20 6f 6e 65 20 22 78 20 49 4e 20  least one "x IN 
2fb2f 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20 74 65  (SELECT ...)" te
2fb30 72 6d 20 75 73 65 64 20 0a 20 20 20 20 2a 2a 20  rm used .    ** 
2fb31 20 20 20 69 6e 20 64 65 74 65 72 6d 69 6e 69 6e     in determinin
2fb32 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e  g the value of n
2fb33 49 6e 4d 75 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20  InMul..    **.  
2fb34 20 20 2a 2a 20 20 6e 42 6f 75 6e 64 3a 0a 20 20    **  nBound:.  
2fb35 20 20 2a 2a 20 20 20 20 41 6e 20 65 73 74 69 6d    **    An estim
2fb36 61 74 65 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e  ate on the amoun
2fb37 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  t of the table t
2fb38 68 61 74 20 6d 75 73 74 20 62 65 20 73 65 61 72  hat must be sear
2fb39 63 68 65 64 2e 20 20 41 0a 20 20 20 20 2a 2a 20  ched.  A.    ** 
2fb3a 20 20 20 76 61 6c 75 65 20 6f 66 20 31 30 30 20     value of 100 
2fb3b 6d 65 61 6e 73 20 74 68 65 20 65 6e 74 69 72 65  means the entire
2fb3c 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63 68   table is search
2fb3d 65 64 2e 20 20 52 61 6e 67 65 20 63 6f 6e 73 74  ed.  Range const
2fb3e 72 61 69 6e 74 73 0a 20 20 20 20 2a 2a 20 20 20  raints.    **   
2fb3f 20 6d 69 67 68 74 20 72 65 64 75 63 65 20 74 68   might reduce th
2fb40 69 73 20 74 6f 20 61 20 76 61 6c 75 65 20 6c 65  is to a value le
2fb41 73 73 20 74 68 61 6e 20 31 30 30 20 74 6f 20 69  ss than 100 to i
2fb42 6e 64 69 63 61 74 65 20 74 68 61 74 20 6f 6e 6c  ndicate that onl
2fb43 79 0a 20 20 20 20 2a 2a 20 20 20 20 61 20 66 72  y.    **    a fr
2fb44 61 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 74 61  action of the ta
2fb45 62 6c 65 20 6e 65 65 64 73 20 73 65 61 72 63 68  ble needs search
2fb46 69 6e 67 2e 20 20 49 6e 20 74 68 65 20 61 62 73  ing.  In the abs
2fb47 65 6e 63 65 20 6f 66 0a 20 20 20 20 2a 2a 20 20  ence of.    **  
2fb48 20 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 41    sqlite_stat2 A
2fb49 4e 41 4c 59 5a 45 20 64 61 74 61 2c 20 61 20 73  NALYZE data, a s
2fb4a 69 6e 67 6c 65 20 69 6e 65 71 75 61 6c 69 74 79  ingle inequality
2fb4b 20 72 65 64 75 63 65 73 20 74 68 65 20 73 65 61   reduces the sea
2fb4c 72 63 68 0a 20 20 20 20 2a 2a 20 20 20 20 73 70  rch.    **    sp
2fb4d 61 63 65 20 74 6f 20 31 2f 33 72 64 20 69 74 73  ace to 1/3rd its
2fb4e 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a 65 2e 20   original size. 
2fb4f 20 53 6f 20 61 6e 20 78 3e 3f 20 63 6f 6e 73 74   So an x>? const
2fb50 72 61 69 6e 74 20 72 65 64 75 63 65 73 0a 20 20  raint reduces.  
2fb51 20 20 2a 2a 20 20 20 20 6e 42 6f 75 6e 64 20 74    **    nBound t
2fb52 6f 20 33 33 2e 20 20 54 77 6f 20 63 6f 6e 73 74  o 33.  Two const
2fb53 72 61 69 6e 74 73 20 28 78 3e 3f 20 41 4e 44 20  raints (x>? AND 
2fb54 78 3c 3f 29 20 72 65 64 75 63 65 20 6e 42 6f 75  x<?) reduce nBou
2fb55 6e 64 20 74 6f 20 31 31 2e 0a 20 20 20 20 2a 2a  nd to 11..    **
2fb56 0a 20 20 20 20 2a 2a 20 20 62 53 6f 72 74 3a 20  .    **  bSort: 
2fb57 20 20 0a 20 20 20 20 2a 2a 20 20 20 20 42 6f 6f    .    **    Boo
2fb58 6c 65 61 6e 2e 20 54 72 75 65 20 69 66 20 74 68  lean. True if th
2fb59 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20  ere is an ORDER 
2fb5a 42 59 20 63 6c 61 75 73 65 20 74 68 61 74 20 77  BY clause that w
2fb5b 69 6c 6c 20 72 65 71 75 69 72 65 20 61 6e 20 0a  ill require an .
2fb5c 20 20 20 20 2a 2a 20 20 20 20 65 78 74 65 72 6e      **    extern
2fb5d 61 6c 20 73 6f 72 74 20 28 69 2e 65 2e 20 73 63  al sort (i.e. sc
2fb5e 61 6e 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78  anning the index
2fb5f 20 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64   being evaluated
2fb60 20 77 69 6c 6c 20 6e 6f 74 20 0a 20 20 20 20 2a   will not .    *
2fb61 2a 20 20 20 20 63 6f 72 72 65 63 74 6c 79 20 6f  *    correctly o
2fb62 72 64 65 72 20 72 65 63 6f 72 64 73 29 2e 0a 20  rder records).. 
2fb63 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 62 4c     **.    **  bL
2fb64 6f 6f 6b 75 70 3a 20 0a 20 20 20 20 2a 2a 20 20  ookup: .    **  
2fb65 20 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20    Boolean. True 
2fb66 69 66 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65  if for each inde
2fb67 78 20 65 6e 74 72 79 20 76 69 73 69 74 65 64 20  x entry visited 
2fb68 61 20 6c 6f 6f 6b 75 70 20 6f 6e 20 74 68 65 20  a lookup on the 
2fb69 0a 20 20 20 20 2a 2a 20 20 20 20 63 6f 72 72 65  .    **    corre
2fb6a 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65 20 62  sponding table b
2fb6b 2d 74 72 65 65 20 69 73 20 72 65 71 75 69 72 65  -tree is require
2fb6c 64 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61 79  d. This is alway
2fb6d 73 20 66 61 6c 73 65 20 0a 20 20 20 20 2a 2a 20  s false .    ** 
2fb6e 20 20 20 66 6f 72 20 74 68 65 20 72 6f 77 69 64     for the rowid
2fb6f 20 69 6e 64 65 78 2e 20 46 6f 72 20 6f 74 68 65   index. For othe
2fb70 72 20 69 6e 64 65 78 65 73 2c 20 69 74 20 69 73  r indexes, it is
2fb71 20 74 72 75 65 20 75 6e 6c 65 73 73 20 61 6c 6c   true unless all
2fb72 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20 20   the .    **    
2fb73 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74  columns of the t
2fb74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65  able used by the
2fb75 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2fb76 74 20 61 72 65 20 70 72 65 73 65 6e 74 20 69 6e  t are present in
2fb77 20 0a 20 20 20 20 2a 2a 20 20 20 20 74 68 65 20   .    **    the 
2fb78 69 6e 64 65 78 20 28 73 75 63 68 20 61 6e 20 69  index (such an i
2fb79 6e 64 65 78 20 69 73 20 73 6f 6d 65 74 69 6d 65  ndex is sometime
2fb7a 73 20 64 65 73 63 72 69 62 65 64 20 61 73 20 61  s described as a
2fb7b 20 63 6f 76 65 72 69 6e 67 20 69 6e 64 65 78 29   covering index)
2fb7c 2e 0a 20 20 20 20 2a 2a 20 20 20 20 46 6f 72 20  ..    **    For 
2fb7d 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20 74  example, given t
2fb7e 68 65 20 69 6e 64 65 78 20 6f 6e 20 28 61 2c 20  he index on (a, 
2fb7f 62 29 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 6f  b), the second o
2fb80 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2fb81 0a 20 20 20 20 2a 2a 20 20 20 20 74 77 6f 20 71  .    **    two q
2fb82 75 65 72 69 65 73 20 72 65 71 75 69 72 65 73 20  ueries requires 
2fb83 74 61 62 6c 65 20 62 2d 74 72 65 65 20 6c 6f 6f  table b-tree loo
2fb84 6b 75 70 73 2c 20 62 75 74 20 74 68 65 20 66 69  kups, but the fi
2fb85 72 73 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20  rst does not..  
2fb86 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
2fb87 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 61          SELECT a
2fb88 2c 20 62 20 20 20 20 46 52 4f 4d 20 74 62 6c 20  , b    FROM tbl 
2fb89 57 48 45 52 45 20 61 20 3d 20 31 3b 0a 20 20 20  WHERE a = 1;.   
2fb8a 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20   **             
2fb8b 53 45 4c 45 43 54 20 61 2c 20 62 2c 20 63 20 46  SELECT a, b, c F
2fb8c 52 4f 4d 20 74 62 6c 20 57 48 45 52 45 20 61 20  ROM tbl WHERE a 
2fb8d 3d 20 31 3b 0a 20 20 20 20 2a 2f 0a 20 20 20 20  = 1;.    */.    
2fb8e 69 6e 74 20 6e 45 71 3b 0a 20 20 20 20 69 6e 74  int nEq;.    int
2fb8f 20 62 49 6e 45 73 74 20 3d 20 30 3b 0a 20 20 20   bInEst = 0;.   
2fb90 20 69 6e 74 20 6e 49 6e 4d 75 6c 20 3d 20 31 3b   int nInMul = 1;
2fb91 0a 20 20 20 20 69 6e 74 20 6e 42 6f 75 6e 64 20  .    int nBound 
2fb92 3d 20 31 30 30 3b 0a 20 20 20 20 69 6e 74 20 62  = 100;.    int b
2fb93 53 6f 72 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e  Sort = 0;.    in
2fb94 74 20 62 4c 6f 6f 6b 75 70 20 3d 20 30 3b 0a 0a  t bLookup = 0;..
2fb95 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65      /* Determine
2fb96 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 6e   the values of n
2fb97 45 71 20 61 6e 64 20 6e 49 6e 4d 75 6c 20 2a 2f  Eq and nInMul */
2fb98 0a 20 20 20 20 66 6f 72 28 6e 45 71 3d 30 3b 20  .    for(nEq=0; 
2fb99 6e 45 71 3c 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c  nEq<pProbe->nCol
2fb9a 75 6d 6e 3b 20 6e 45 71 2b 2b 29 7b 0a 20 20 20  umn; nEq++){.   
2fb9b 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54     WhereTerm *pT
2fb9c 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f  erm;           /
2fb9d 2a 20 41 20 73 69 6e 67 6c 65 20 74 65 72 6d 20  * A single term 
2fb9e 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  of the WHERE cla
2fb9f 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  use */.      int
2fba0 20 6a 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43   j = pProbe->aiC
2fba1 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b 0a 20 20 20 20  olumn[nEq];.    
2fba2 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65    pTerm = findTe
2fba3 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c  rm(pWC, iCur, j,
2fba4 20 6e 6f 74 52 65 61 64 79 2c 20 65 71 54 65 72   notReady, eqTer
2fba5 6d 4d 61 73 6b 2c 20 70 49 64 78 29 3b 0a 20 20  mMask, pIdx);.  
2fba6 20 20 20 20 69 66 28 20 70 54 65 72 6d 3d 3d 30      if( pTerm==0
2fba7 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
2fba8 77 73 46 6c 61 67 73 20 7c 3d 20 28 57 48 45 52  wsFlags |= (WHER
2fba9 45 5f 43 4f 4c 55 4d 4e 5f 45 51 7c 57 48 45 52  E_COLUMN_EQ|WHER
2fbaa 45 5f 52 4f 57 49 44 5f 45 51 29 3b 0a 20 20 20  E_ROWID_EQ);.   
2fbab 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f     if( pTerm->eO
2fbac 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20  perator & WO_IN 
2fbad 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20  ){.        Expr 
2fbae 2a 70 45 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e  *pExpr = pTerm->
2fbaf 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 77  pExpr;.        w
2fbb0 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f  sFlags |= WHERE_
2fbb1 43 4f 4c 55 4d 4e 5f 49 4e 3b 0a 20 20 20 20 20  COLUMN_IN;.     
2fbb2 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
2fbb3 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2fbb4 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
2fbb5 20 20 20 20 20 20 20 20 20 6e 49 6e 4d 75 6c 20           nInMul 
2fbb6 2a 3d 20 32 35 3b 0a 20 20 20 20 20 20 20 20 20  *= 25;.         
2fbb7 20 62 49 6e 45 73 74 20 3d 20 31 3b 0a 20 20 20   bInEst = 1;.   
2fbb8 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
2fbb9 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 29 7b  Expr->x.pList ){
2fbba 0a 20 20 20 20 20 20 20 20 20 20 6e 49 6e 4d 75  .          nInMu
2fbbb 6c 20 2a 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  l *= pExpr->x.pL
2fbbc 69 73 74 2d 3e 6e 45 78 70 72 20 2b 20 31 3b 0a  ist->nExpr + 1;.
2fbbd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2fbbe 7d 65 6c 73 65 20 69 66 28 20 70 54 65 72 6d 2d  }else if( pTerm-
2fbbf 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f  >eOperator & WO_
2fbc0 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  ISNULL ){.      
2fbc1 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45    wsFlags |= WHE
2fbc2 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 3b 0a  RE_COLUMN_NULL;.
2fbc3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 73        }.      us
2fbc4 65 64 20 7c 3d 20 70 54 65 72 6d 2d 3e 70 72 65  ed |= pTerm->pre
2fbc5 72 65 71 52 69 67 68 74 3b 0a 20 20 20 20 7d 0a  reqRight;.    }.
2fbc6 0a 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e  .    /* Determin
2fbc7 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e  e the value of n
2fbc8 42 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 69 66  Bound. */.    if
2fbc9 28 20 6e 45 71 3c 70 50 72 6f 62 65 2d 3e 6e 43  ( nEq<pProbe->nC
2fbca 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 69  olumn ){.      i
2fbcb 6e 74 20 6a 20 3d 20 70 50 72 6f 62 65 2d 3e 61  nt j = pProbe->a
2fbcc 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b 0a 20 20  iColumn[nEq];.  
2fbcd 20 20 20 20 69 66 28 20 66 69 6e 64 54 65 72 6d      if( findTerm
2fbce 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e  (pWC, iCur, j, n
2fbcf 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57  otReady, WO_LT|W
2fbd0 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45  O_LE|WO_GT|WO_GE
2fbd1 2c 20 70 49 64 78 29 20 29 7b 0a 20 20 20 20 20  , pIdx) ){.     
2fbd2 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54     WhereTerm *pT
2fbd3 6f 70 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57  op = findTerm(pW
2fbd4 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52  C, iCur, j, notR
2fbd5 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c  eady, WO_LT|WO_L
2fbd6 45 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20  E, pIdx);.      
2fbd7 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 42 74    WhereTerm *pBt
2fbd8 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43  m = findTerm(pWC
2fbd9 2c 20 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65  , iCur, j, notRe
2fbda 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45  ady, WO_GT|WO_GE
2fbdb 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 20  , pIdx);.       
2fbdc 20 77 68 65 72 65 52 61 6e 67 65 53 63 61 6e 45   whereRangeScanE
2fbdd 73 74 28 70 50 61 72 73 65 2c 20 70 50 72 6f 62  st(pParse, pProb
2fbde 65 2c 20 6e 45 71 2c 20 70 42 74 6d 2c 20 70 54  e, nEq, pBtm, pT
2fbdf 6f 70 2c 20 26 6e 42 6f 75 6e 64 29 3b 0a 20 20  op, &nBound);.  
2fbe0 20 20 20 20 20 20 69 66 28 20 70 54 6f 70 20 29        if( pTop )
2fbe1 7b 0a 20 20 20 20 20 20 20 20 20 20 77 73 46 6c  {.          wsFl
2fbe2 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 54 4f 50  ags |= WHERE_TOP
2fbe3 5f 4c 49 4d 49 54 3b 0a 20 20 20 20 20 20 20 20  _LIMIT;.        
2fbe4 20 20 75 73 65 64 20 7c 3d 20 70 54 6f 70 2d 3e    used |= pTop->
2fbe5 70 72 65 72 65 71 52 69 67 68 74 3b 0a 20 20 20  prereqRight;.   
2fbe6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
2fbe7 66 28 20 70 42 74 6d 20 29 7b 0a 20 20 20 20 20  f( pBtm ){.     
2fbe8 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20       wsFlags |= 
2fbe9 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 3b  WHERE_BTM_LIMIT;
2fbea 0a 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20  .          used 
2fbeb 7c 3d 20 70 42 74 6d 2d 3e 70 72 65 72 65 71 52  |= pBtm->prereqR
2fbec 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ight;.        }.
2fbed 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20          wsFlags 
2fbee 7c 3d 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  |= (WHERE_COLUMN
2fbef 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f 52 4f 57  _RANGE|WHERE_ROW
2fbf0 49 44 5f 52 41 4e 47 45 29 3b 0a 20 20 20 20 20  ID_RANGE);.     
2fbf1 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
2fbf2 20 70 50 72 6f 62 65 2d 3e 6f 6e 45 72 72 6f 72   pProbe->onError
2fbf3 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20  !=OE_None ){.   
2fbf4 20 20 20 74 65 73 74 63 61 73 65 28 20 77 73 46     testcase( wsF
2fbf5 6c 61 67 73 20 26 20 57 48 45 52 45 5f 43 4f 4c  lags & WHERE_COL
2fbf6 55 4d 4e 5f 49 4e 20 29 3b 0a 20 20 20 20 20 20  UMN_IN );.      
2fbf7 74 65 73 74 63 61 73 65 28 20 77 73 46 6c 61 67  testcase( wsFlag
2fbf8 73 20 26 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  s & WHERE_COLUMN
2fbf9 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 69  _NULL );.      i
2fbfa 66 28 20 28 77 73 46 6c 61 67 73 20 26 20 28 57  f( (wsFlags & (W
2fbfb 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57  HERE_COLUMN_IN|W
2fbfc 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c  HERE_COLUMN_NULL
2fbfd 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ))==0 ){.       
2fbfe 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
2fbff 45 5f 55 4e 49 51 55 45 3b 0a 20 20 20 20 20 20  E_UNIQUE;.      
2fc00 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
2fc01 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f  If there is an O
2fc02 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61  RDER BY clause a
2fc03 6e 64 20 74 68 65 20 69 6e 64 65 78 20 62 65 69  nd the index bei
2fc04 6e 67 20 63 6f 6e 73 69 64 65 72 65 64 20 77 69  ng considered wi
2fc05 6c 6c 0a 20 20 20 20 2a 2a 20 6e 61 74 75 72 61  ll.    ** natura
2fc06 6c 6c 79 20 73 63 61 6e 20 72 6f 77 73 20 69 6e  lly scan rows in
2fc07 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6f 72   the required or
2fc08 64 65 72 2c 20 73 65 74 20 74 68 65 20 61 70 70  der, set the app
2fc09 72 6f 70 72 69 61 74 65 20 66 6c 61 67 73 0a 20  ropriate flags. 
2fc0a 20 20 20 2a 2a 20 69 6e 20 77 73 46 6c 61 67 73     ** in wsFlags
2fc0b 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20  . Otherwise, if 
2fc0c 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52 44 45  there is an ORDE
2fc0d 52 20 42 59 20 63 6c 61 75 73 65 20 62 75 74 20  R BY clause but 
2fc0e 74 68 65 20 69 6e 64 65 78 0a 20 20 20 20 2a 2a  the index.    **
2fc0f 20 77 69 6c 6c 20 73 63 61 6e 20 72 6f 77 73 20   will scan rows 
2fc10 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 6f  in a different o
2fc11 72 64 65 72 2c 20 73 65 74 20 74 68 65 20 62 53  rder, set the bS
2fc12 6f 72 74 20 76 61 72 69 61 62 6c 65 2e 20 20 2a  ort variable.  *
2fc13 2f 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65 72  /.    if( pOrder
2fc14 42 79 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  By ){.      if( 
2fc15 28 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52  (wsFlags & (WHER
2fc16 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 7c 57 48 45 52  E_COLUMN_IN|WHER
2fc17 45 5f 43 4f 4c 55 4d 4e 5f 4e 55 4c 4c 29 29 3d  E_COLUMN_NULL))=
2fc18 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 69 73  =0.        && is
2fc19 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 70 50 61  SortingIndex(pPa
2fc1a 72 73 65 2c 70 57 43 2d 3e 70 4d 61 73 6b 53 65  rse,pWC->pMaskSe
2fc1b 74 2c 70 50 72 6f 62 65 2c 69 43 75 72 2c 70 4f  t,pProbe,iCur,pO
2fc1c 72 64 65 72 42 79 2c 6e 45 71 2c 26 72 65 76 29  rderBy,nEq,&rev)
2fc1d 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
2fc1e 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45    wsFlags |= WHE
2fc1f 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 7c 57  RE_ROWID_RANGE|W
2fc20 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47  HERE_COLUMN_RANG
2fc21 45 7c 57 48 45 52 45 5f 4f 52 44 45 52 42 59 3b  E|WHERE_ORDERBY;
2fc22 0a 20 20 20 20 20 20 20 20 77 73 46 6c 61 67 73  .        wsFlags
2fc23 20 7c 3d 20 28 72 65 76 20 3f 20 57 48 45 52 45   |= (rev ? WHERE
2fc24 5f 52 45 56 45 52 53 45 20 3a 20 30 29 3b 0a 20  _REVERSE : 0);. 
2fc25 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2fc26 20 20 20 20 62 53 6f 72 74 20 3d 20 31 3b 0a 20      bSort = 1;. 
2fc27 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
2fc28 20 20 2f 2a 20 49 66 20 63 75 72 72 65 6e 74 6c    /* If currentl
2fc29 79 20 63 61 6c 63 75 6c 61 74 69 6e 67 20 74 68  y calculating th
2fc2a 65 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67 20  e cost of using 
2fc2b 61 6e 20 69 6e 64 65 78 20 28 6e 6f 74 20 74 68  an index (not th
2fc2c 65 20 49 50 4b 0a 20 20 20 20 2a 2a 20 69 6e 64  e IPK.    ** ind
2fc2d 65 78 29 2c 20 64 65 74 65 72 6d 69 6e 65 20 69  ex), determine i
2fc2e 66 20 61 6c 6c 20 72 65 71 75 69 72 65 64 20 63  f all required c
2fc2f 6f 6c 75 6d 6e 20 64 61 74 61 20 6d 61 79 20 62  olumn data may b
2fc30 65 20 6f 62 74 61 69 6e 65 64 20 77 69 74 68 6f  e obtained witho
2fc31 75 74 20 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69  ut .    ** seeki
2fc32 6e 67 20 74 6f 20 65 6e 74 72 69 65 73 20 69 6e  ng to entries in
2fc33 20 74 68 65 20 6d 61 69 6e 20 74 61 62 6c 65 20   the main table 
2fc34 28 69 2e 65 2e 20 69 66 20 74 68 65 20 69 6e 64  (i.e. if the ind
2fc35 65 78 20 69 73 20 61 20 63 6f 76 65 72 69 6e 67  ex is a covering
2fc36 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 66 6f  .    ** index fo
2fc37 72 20 74 68 69 73 20 71 75 65 72 79 29 2e 20 49  r this query). I
2fc38 66 20 69 74 20 69 73 2c 20 73 65 74 20 74 68 65  f it is, set the
2fc39 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20   WHERE_IDX_ONLY 
2fc3a 66 6c 61 67 20 69 6e 0a 20 20 20 20 2a 2a 20 77  flag in.    ** w
2fc3b 73 46 6c 61 67 73 2e 20 4f 74 68 65 72 77 69 73  sFlags. Otherwis
2fc3c 65 2c 20 73 65 74 20 74 68 65 20 62 4c 6f 6f 6b  e, set the bLook
2fc3d 75 70 20 76 61 72 69 61 62 6c 65 20 74 6f 20 74  up variable to t
2fc3e 72 75 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28  rue.  */.    if(
2fc3f 20 70 49 64 78 20 26 26 20 77 73 46 6c 61 67 73   pIdx && wsFlags
2fc40 20 29 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73   ){.      Bitmas
2fc41 6b 20 6d 20 3d 20 70 53 72 63 2d 3e 63 6f 6c 55  k m = pSrc->colU
2fc42 73 65 64 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a  sed;.      int j
2fc43 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ;.      for(j=0;
2fc44 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   j<pIdx->nColumn
2fc45 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
2fc46 69 6e 74 20 78 20 3d 20 70 49 64 78 2d 3e 61 69  int x = pIdx->ai
2fc47 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 20  Column[j];.     
2fc48 20 20 20 69 66 28 20 78 3c 42 4d 53 2d 31 20 29     if( x<BMS-1 )
2fc49 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 20 26 3d  {.          m &=
2fc4a 20 7e 28 28 28 42 69 74 6d 61 73 6b 29 31 29 3c   ~(((Bitmask)1)<
2fc4b 3c 78 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  <x);.        }. 
2fc4c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2fc4d 20 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   m==0 ){.       
2fc4e 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
2fc4f 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 20 20 20 20  E_IDX_ONLY;.    
2fc50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2fc51 20 62 4c 6f 6f 6b 75 70 20 3d 20 31 3b 0a 20 20   bLookup = 1;.  
2fc52 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
2fc53 20 2f 2a 2a 2a 2a 20 42 65 67 69 6e 20 61 64 64   /**** Begin add
2fc54 69 6e 67 20 75 70 20 74 68 65 20 63 6f 73 74 20  ing up the cost 
2fc55 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20 69 6e  of using this in
2fc56 64 65 78 20 28 4e 65 65 64 73 20 69 6d 70 72 6f  dex (Needs impro
2fc57 76 65 6d 65 6e 74 73 29 0a 20 20 20 20 2a 2a 0a  vements).    **.
2fc58 20 20 20 20 2a 2a 20 45 73 74 69 6d 61 74 65 20      ** Estimate 
2fc59 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
2fc5a 77 73 20 6f 66 20 6f 75 74 70 75 74 2e 20 20 46  ws of output.  F
2fc5b 6f 72 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f  or an IN operato
2fc5c 72 2c 0a 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74  r,.    ** do not
2fc5d 20 6c 65 74 20 74 68 65 20 65 73 74 69 6d 61 74   let the estimat
2fc5e 65 20 65 78 63 65 65 64 20 68 61 6c 66 20 74 68  e exceed half th
2fc5f 65 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61  e rows in the ta
2fc60 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ble..    */.    
2fc61 6e 52 6f 77 20 3d 20 28 64 6f 75 62 6c 65 29 28  nRow = (double)(
2fc62 61 69 52 6f 77 45 73 74 5b 6e 45 71 5d 20 2a 20  aiRowEst[nEq] * 
2fc63 6e 49 6e 4d 75 6c 29 3b 0a 20 20 20 20 69 66 28  nInMul);.    if(
2fc64 20 62 49 6e 45 73 74 20 26 26 20 6e 52 6f 77 2a   bInEst && nRow*
2fc65 32 3e 61 69 52 6f 77 45 73 74 5b 30 5d 20 29 7b  2>aiRowEst[0] ){
2fc66 0a 20 20 20 20 20 20 6e 52 6f 77 20 3d 20 61 69  .      nRow = ai
2fc67 52 6f 77 45 73 74 5b 30 5d 2f 32 3b 0a 20 20 20  RowEst[0]/2;.   
2fc68 20 20 20 6e 49 6e 4d 75 6c 20 3d 20 28 69 6e 74     nInMul = (int
2fc69 29 28 6e 52 6f 77 20 2f 20 61 69 52 6f 77 45 73  )(nRow / aiRowEs
2fc6a 74 5b 6e 45 71 5d 29 3b 0a 20 20 20 20 7d 0a 0a  t[nEq]);.    }..
2fc6b 20 20 20 20 2f 2a 20 41 73 73 75 6d 65 20 63 6f      /* Assume co
2fc6c 6e 73 74 61 6e 74 20 63 6f 73 74 20 74 6f 20 61  nstant cost to a
2fc6d 63 63 65 73 73 20 61 20 72 6f 77 20 61 6e 64 20  ccess a row and 
2fc6e 6c 6f 67 61 72 69 74 68 6d 69 63 20 63 6f 73 74  logarithmic cost
2fc6f 20 74 6f 0a 20 20 20 20 2a 2a 20 64 6f 20 61 20   to.    ** do a 
2fc70 62 69 6e 61 72 79 20 73 65 61 72 63 68 2e 20 20  binary search.  
2fc71 48 65 6e 63 65 2c 20 74 68 65 20 69 6e 69 74 69  Hence, the initi
2fc72 61 6c 20 63 6f 73 74 20 69 73 20 74 68 65 20 6e  al cost is the n
2fc73 75 6d 62 65 72 20 6f 66 20 6f 75 74 70 75 74 0a  umber of output.
2fc74 20 20 20 20 2a 2a 20 72 6f 77 73 20 70 6c 75 73      ** rows plus
2fc75 20 6c 6f 67 32 28 74 61 62 6c 65 2d 73 69 7a 65   log2(table-size
2fc76 29 20 74 69 6d 65 73 20 74 68 65 20 6e 75 6d 62  ) times the numb
2fc77 65 72 20 6f 66 20 62 69 6e 61 72 79 20 73 65 61  er of binary sea
2fc78 72 63 68 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  rches..    */.  
2fc79 20 20 63 6f 73 74 20 3d 20 6e 52 6f 77 20 2b 20    cost = nRow + 
2fc7a 6e 49 6e 4d 75 6c 2a 65 73 74 4c 6f 67 28 61 69  nInMul*estLog(ai
2fc7b 52 6f 77 45 73 74 5b 30 5d 29 3b 0a 0a 20 20 20  RowEst[0]);..   
2fc7c 20 2f 2a 20 41 64 6a 75 73 74 20 74 68 65 20 6e   /* Adjust the n
2fc7d 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e  umber of rows an
2fc7e 64 20 74 68 65 20 63 6f 73 74 20 64 6f 77 6e 77  d the cost downw
2fc7f 61 72 64 20 74 6f 20 72 65 66 6c 65 63 74 20 72  ard to reflect r
2fc80 6f 77 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  ows.    ** that 
2fc81 61 72 65 20 65 78 63 6c 75 64 65 64 20 62 79 20  are excluded by 
2fc82 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74  range constraint
2fc83 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 52  s..    */.    nR
2fc84 6f 77 20 3d 20 28 6e 52 6f 77 20 2a 20 28 64 6f  ow = (nRow * (do
2fc85 75 62 6c 65 29 6e 42 6f 75 6e 64 29 20 2f 20 28  uble)nBound) / (
2fc86 64 6f 75 62 6c 65 29 31 30 30 3b 0a 20 20 20 20  double)100;.    
2fc87 63 6f 73 74 20 3d 20 28 63 6f 73 74 20 2a 20 28  cost = (cost * (
2fc88 64 6f 75 62 6c 65 29 6e 42 6f 75 6e 64 29 20 2f  double)nBound) /
2fc89 20 28 64 6f 75 62 6c 65 29 31 30 30 3b 0a 0a 20   (double)100;.. 
2fc8a 20 20 20 2f 2a 20 41 64 64 20 69 6e 20 74 68 65     /* Add in the
2fc8b 20 65 73 74 69 6d 61 74 65 64 20 63 6f 73 74 20   estimated cost 
2fc8c 6f 66 20 73 6f 72 74 69 6e 67 20 74 68 65 20 72  of sorting the r
2fc8d 65 73 75 6c 74 0a 20 20 20 20 2a 2f 0a 20 20 20  esult.    */.   
2fc8e 20 69 66 28 20 62 53 6f 72 74 20 29 7b 0a 20 20   if( bSort ){.  
2fc8f 20 20 20 20 63 6f 73 74 20 2b 3d 20 63 6f 73 74      cost += cost
2fc90 2a 65 73 74 4c 6f 67 28 63 6f 73 74 29 3b 0a 20  *estLog(cost);. 
2fc91 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
2fc92 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  all information 
2fc93 63 61 6e 20 62 65 20 74 61 6b 65 6e 20 64 69 72  can be taken dir
2fc94 65 63 74 6c 79 20 66 72 6f 6d 20 74 68 65 20 69  ectly from the i
2fc95 6e 64 65 78 2c 20 77 65 20 61 76 6f 69 64 0a 20  ndex, we avoid. 
2fc96 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 61 62 6c     ** doing tabl
2fc97 65 20 6c 6f 6f 6b 75 70 73 2e 20 20 54 68 69 73  e lookups.  This
2fc98 20 72 65 64 75 63 65 73 20 74 68 65 20 63 6f 73   reduces the cos
2fc99 74 20 62 79 20 68 61 6c 66 2e 20 20 28 4e 6f 74  t by half.  (Not
2fc9a 20 72 65 61 6c 6c 79 20 2d 0a 20 20 20 20 2a 2a   really -.    **
2fc9b 20 74 68 69 73 20 6e 65 65 64 73 20 74 6f 20 62   this needs to b
2fc9c 65 20 66 69 78 65 64 2e 29 0a 20 20 20 20 2a 2f  e fixed.).    */
2fc9d 0a 20 20 20 20 69 66 28 20 70 49 64 78 20 26 26  .    if( pIdx &&
2fc9e 20 62 4c 6f 6f 6b 75 70 3d 3d 30 20 29 7b 0a 20   bLookup==0 ){. 
2fc9f 20 20 20 20 20 63 6f 73 74 20 2f 3d 20 28 64 6f       cost /= (do
2fca0 75 62 6c 65 29 32 3b 0a 20 20 20 20 7d 0a 20 20  uble)2;.    }.  
2fca1 20 20 2f 2a 2a 2a 2a 20 43 6f 73 74 20 6f 66 20    /**** Cost of 
2fca2 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78  using this index
2fca3 20 68 61 73 20 6e 6f 77 20 62 65 65 6e 20 63 6f   has now been co
2fca4 6d 70 75 74 65 64 20 2a 2a 2a 2a 2f 0a 0a 20 20  mputed ****/..  
2fca5 20 20 57 48 45 52 45 54 52 41 43 45 28 28 0a 20    WHERETRACE((. 
2fca6 20 20 20 20 20 22 74 62 6c 3d 25 73 20 69 64 78       "tbl=%s idx
2fca7 3d 25 73 20 6e 45 71 3d 25 64 20 6e 49 6e 4d 75  =%s nEq=%d nInMu
2fca8 6c 3d 25 64 20 6e 42 6f 75 6e 64 3d 25 64 20 62  l=%d nBound=%d b
2fca9 53 6f 72 74 3d 25 64 20 62 4c 6f 6f 6b 75 70 3d  Sort=%d bLookup=
2fcaa 25 64 22 0a 20 20 20 20 20 20 22 20 77 73 46 6c  %d".      " wsFl
2fcab 61 67 73 3d 25 64 20 20 20 28 6e 52 6f 77 3d 25  ags=%d   (nRow=%
2fcac 2e 32 66 20 63 6f 73 74 3d 25 2e 32 66 29 5c 6e  .2f cost=%.2f)\n
2fcad 22 2c 0a 20 20 20 20 20 20 70 53 72 63 2d 3e 70  ",.      pSrc->p
2fcae 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 28 70 49 64  Tab->zName, (pId
2fcaf 78 20 3f 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 20  x ? pIdx->zName 
2fcb0 3a 20 22 69 70 6b 22 29 2c 20 0a 20 20 20 20 20  : "ipk"), .     
2fcb1 20 6e 45 71 2c 20 6e 49 6e 4d 75 6c 2c 20 6e 42   nEq, nInMul, nB
2fcb2 6f 75 6e 64 2c 20 62 53 6f 72 74 2c 20 62 4c 6f  ound, bSort, bLo
2fcb3 6f 6b 75 70 2c 20 77 73 46 6c 61 67 73 2c 20 6e  okup, wsFlags, n
2fcb4 52 6f 77 2c 20 63 6f 73 74 0a 20 20 20 20 29 29  Row, cost.    ))
2fcb5 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  ;..    /* If thi
2fcb6 73 20 69 6e 64 65 78 20 69 73 20 74 68 65 20 62  s index is the b
2fcb7 65 73 74 20 77 65 20 68 61 76 65 20 73 65 65 6e  est we have seen
2fcb8 20 73 6f 20 66 61 72 2c 20 74 68 65 6e 20 72 65   so far, then re
2fcb9 63 6f 72 64 20 74 68 69 73 0a 20 20 20 20 2a 2a  cord this.    **
2fcba 20 69 6e 64 65 78 20 61 6e 64 20 69 74 73 20 63   index and its c
2fcbb 6f 73 74 20 69 6e 20 74 68 65 20 70 43 6f 73 74  ost in the pCost
2fcbc 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20   structure..    
2fcbd 2a 2f 0a 20 20 20 20 69 66 28 20 28 21 70 49 64  */.    if( (!pId
2fcbe 78 20 7c 7c 20 77 73 46 6c 61 67 73 29 20 26 26  x || wsFlags) &&
2fcbf 20 63 6f 73 74 3c 70 43 6f 73 74 2d 3e 72 43 6f   cost<pCost->rCo
2fcc0 73 74 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 73  st ){.      pCos
2fcc1 74 2d 3e 72 43 6f 73 74 20 3d 20 63 6f 73 74 3b  t->rCost = cost;
2fcc2 0a 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 6e 52  .      pCost->nR
2fcc3 6f 77 20 3d 20 6e 52 6f 77 3b 0a 20 20 20 20 20  ow = nRow;.     
2fcc4 20 70 43 6f 73 74 2d 3e 75 73 65 64 20 3d 20 75   pCost->used = u
2fcc5 73 65 64 3b 0a 20 20 20 20 20 20 70 43 6f 73 74  sed;.      pCost
2fcc6 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 3d  ->plan.wsFlags =
2fcc7 20 28 77 73 46 6c 61 67 73 26 77 73 46 6c 61 67   (wsFlags&wsFlag
2fcc8 4d 61 73 6b 29 3b 0a 20 20 20 20 20 20 70 43 6f  Mask);.      pCo
2fcc9 73 74 2d 3e 70 6c 61 6e 2e 6e 45 71 20 3d 20 6e  st->plan.nEq = n
2fcca 45 71 3b 0a 20 20 20 20 20 20 70 43 6f 73 74 2d  Eq;.      pCost-
2fccb 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 20 3d 20 70  >plan.u.pIdx = p
2fccc 49 64 78 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  Idx;.    }..    
2fccd 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 20  /* If there was 
2fcce 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  an INDEXED BY cl
2fccf 61 75 73 65 2c 20 74 68 65 6e 20 6f 6e 6c 79 20  ause, then only 
2fcd0 74 68 61 74 20 6f 6e 65 20 69 6e 64 65 78 20 69  that one index i
2fcd1 73 0a 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64 65  s.    ** conside
2fcd2 72 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  red. */.    if( 
2fcd3 70 53 72 63 2d 3e 70 49 6e 64 65 78 20 29 20 62  pSrc->pIndex ) b
2fcd4 72 65 61 6b 3b 0a 0a 20 20 20 20 2f 2a 20 52 65  reak;..    /* Re
2fcd5 73 65 74 20 6d 61 73 6b 73 20 66 6f 72 20 74 68  set masks for th
2fcd6 65 20 6e 65 78 74 20 69 6e 64 65 78 20 69 6e 20  e next index in 
2fcd7 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20  the loop */.    
2fcd8 77 73 46 6c 61 67 4d 61 73 6b 20 3d 20 7e 28 57  wsFlagMask = ~(W
2fcd9 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48  HERE_ROWID_EQ|WH
2fcda 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29  ERE_ROWID_RANGE)
2fcdb 3b 0a 20 20 20 20 65 71 54 65 72 6d 4d 61 73 6b  ;.    eqTermMask
2fcdc 20 3d 20 69 64 78 45 71 54 65 72 6d 4d 61 73 6b   = idxEqTermMask
2fcdd 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
2fcde 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52  here is no ORDER
2fcdf 20 42 59 20 63 6c 61 75 73 65 20 61 6e 64 20 74   BY clause and t
2fce0 68 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73  he SQLITE_Revers
2fce1 65 4f 72 64 65 72 20 66 6c 61 67 0a 20 20 2a 2a  eOrder flag.  **
2fce2 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 72 65   is set, then re
2fce3 76 65 72 73 65 20 74 68 65 20 6f 72 64 65 72 20  verse the order 
2fce4 74 68 61 74 20 74 68 65 20 69 6e 64 65 78 20 77  that the index w
2fce5 69 6c 6c 20 62 65 20 73 63 61 6e 6e 65 64 0a 20  ill be scanned. 
2fce6 20 2a 2a 20 69 6e 2e 20 54 68 69 73 20 69 73 20   ** in. This is 
2fce7 75 73 65 64 20 66 6f 72 20 61 70 70 6c 69 63 61  used for applica
2fce8 74 69 6f 6e 20 74 65 73 74 69 6e 67 2c 20 74 6f  tion testing, to
2fce9 20 68 65 6c 70 20 66 69 6e 64 20 63 61 73 65 73   help find cases
2fcea 0a 20 20 2a 2a 20 77 68 65 72 65 20 61 70 70 6c  .  ** where appl
2fceb 69 63 61 74 69 6f 6e 20 62 65 68 61 76 69 6f 75  ication behaviou
2fcec 72 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68 65  r depends on the
2fced 20 28 75 6e 64 65 66 69 6e 65 64 29 20 6f 72 64   (undefined) ord
2fcee 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 53 51 4c  er that.  ** SQL
2fcef 69 74 65 20 6f 75 74 70 75 74 73 20 72 6f 77 73  ite outputs rows
2fcf0 20 69 6e 20 69 6e 20 74 68 65 20 61 62 73 65 6e   in in the absen
2fcf1 63 65 20 6f 66 20 61 6e 20 4f 52 44 45 52 20 42  ce of an ORDER B
2fcf2 59 20 63 6c 61 75 73 65 2e 20 20 2a 2f 0a 20 20  Y clause.  */.  
2fcf3 69 66 28 20 21 70 4f 72 64 65 72 42 79 20 26 26  if( !pOrderBy &&
2fcf4 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61   pParse->db->fla
2fcf5 67 73 20 26 20 53 51 4c 49 54 45 5f 52 65 76 65  gs & SQLITE_Reve
2fcf6 72 73 65 4f 72 64 65 72 20 29 7b 0a 20 20 20 20  rseOrder ){.    
2fcf7 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c  pCost->plan.wsFl
2fcf8 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 52 45 56  ags |= WHERE_REV
2fcf9 45 52 53 45 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  ERSE;.  }..  ass
2fcfa 65 72 74 28 20 70 4f 72 64 65 72 42 79 20 7c 7c  ert( pOrderBy ||
2fcfb 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73   (pCost->plan.ws
2fcfc 46 6c 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45  Flags&WHERE_ORDE
2fcfd 52 42 59 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73  RBY)==0 );.  ass
2fcfe 65 72 74 28 20 70 43 6f 73 74 2d 3e 70 6c 61 6e  ert( pCost->plan
2fcff 2e 75 2e 70 49 64 78 3d 3d 30 20 7c 7c 20 28 70  .u.pIdx==0 || (p
2fd00 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  Cost->plan.wsFla
2fd01 67 73 26 57 48 45 52 45 5f 52 4f 57 49 44 5f 45  gs&WHERE_ROWID_E
2fd02 51 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  Q)==0 );.  asser
2fd03 74 28 20 70 53 72 63 2d 3e 70 49 6e 64 65 78 3d  t( pSrc->pIndex=
2fd04 3d 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 70 43  =0 .       || pC
2fd05 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78  ost->plan.u.pIdx
2fd06 3d 3d 30 20 0a 20 20 20 20 20 20 20 7c 7c 20 70  ==0 .       || p
2fd07 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64  Cost->plan.u.pId
2fd08 78 3d 3d 70 53 72 63 2d 3e 70 49 6e 64 65 78 20  x==pSrc->pIndex 
2fd09 0a 20 20 29 3b 0a 0a 20 20 57 48 45 52 45 54 52  .  );..  WHERETR
2fd0a 41 43 45 28 28 22 62 65 73 74 20 69 6e 64 65 78  ACE(("best index
2fd0b 20 69 73 3a 20 25 73 5c 6e 22 2c 20 0a 20 20 20   is: %s\n", .   
2fd0c 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e   (pCost->plan.u.
2fd0d 70 49 64 78 20 3f 20 70 43 6f 73 74 2d 3e 70 6c  pIdx ? pCost->pl
2fd0e 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65  an.u.pIdx->zName
2fd0f 20 3a 20 22 69 70 6b 22 29 0a 20 20 29 29 3b 0a   : "ipk").  ));.
2fd10 20 20 0a 20 20 62 65 73 74 4f 72 43 6c 61 75 73    .  bestOrClaus
2fd11 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  eIndex(pParse, p
2fd12 57 43 2c 20 70 53 72 63 2c 20 6e 6f 74 52 65 61  WC, pSrc, notRea
2fd13 64 79 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 43  dy, pOrderBy, pC
2fd14 6f 73 74 29 3b 0a 20 20 70 43 6f 73 74 2d 3e 70  ost);.  pCost->p
2fd15 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c 3d 20 65  lan.wsFlags |= e
2fd16 71 54 65 72 6d 4d 61 73 6b 3b 0a 7d 0a 0a 2f 2a  qTermMask;.}../*
2fd17 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 71 75 65  .** Find the que
2fd18 72 79 20 70 6c 61 6e 20 66 6f 72 20 61 63 63 65  ry plan for acce
2fd19 73 73 69 6e 67 20 74 61 62 6c 65 20 70 53 72 63  ssing table pSrc
2fd1a 2d 3e 70 54 61 62 2e 20 57 72 69 74 65 20 74 68  ->pTab. Write th
2fd1b 65 0a 2a 2a 20 62 65 73 74 20 71 75 65 72 79 20  e.** best query 
2fd1c 70 6c 61 6e 20 61 6e 64 20 69 74 73 20 63 6f 73  plan and its cos
2fd1d 74 20 69 6e 74 6f 20 74 68 65 20 57 68 65 72 65  t into the Where
2fd1e 43 6f 73 74 20 6f 62 6a 65 63 74 20 73 75 70 70  Cost object supp
2fd1f 6c 69 65 64 20 0a 2a 2a 20 61 73 20 74 68 65 20  lied .** as the 
2fd20 6c 61 73 74 20 70 61 72 61 6d 65 74 65 72 2e 20  last parameter. 
2fd21 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61  This function ma
2fd22 79 20 63 61 6c 63 75 6c 61 74 65 20 74 68 65 20  y calculate the 
2fd23 63 6f 73 74 20 6f 66 0a 2a 2a 20 62 6f 74 68 20  cost of.** both 
2fd24 72 65 61 6c 20 61 6e 64 20 76 69 72 74 75 61 6c  real and virtual
2fd25 20 74 61 62 6c 65 20 73 63 61 6e 73 2e 0a 2a 2f   table scans..*/
2fd26 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 65 73  .static void bes
2fd27 74 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20  tIndex(.  Parse 
2fd28 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2fd29 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2fd2a 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
2fd2b 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
2fd2c 57 43 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  WC,           /*
2fd2d 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
2fd2e 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  e */.  struct Sr
2fd2f 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63  cList_item *pSrc
2fd30 2c 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63  ,  /* The FROM c
2fd31 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65  lause term to se
2fd32 61 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73  arch */.  Bitmas
2fd33 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20  k notReady,     
2fd34 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66        /* Mask of
2fd35 20 63 75 72 73 6f 72 73 20 74 68 61 74 20 61 72   cursors that ar
2fd36 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20  e not available 
2fd37 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2fd38 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20  OrderBy,        
2fd39 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
2fd3a 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65   clause */.  Whe
2fd3b 72 65 43 6f 73 74 20 2a 70 43 6f 73 74 20 20 20  reCost *pCost   
2fd3c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65           /* Lowe
2fd3d 73 74 20 63 6f 73 74 20 71 75 65 72 79 20 70 6c  st cost query pl
2fd3e 61 6e 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66  an */.){.#ifndef
2fd3f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2fd40 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20  TUALTABLE.  if( 
2fd41 49 73 56 69 72 74 75 61 6c 28 70 53 72 63 2d 3e  IsVirtual(pSrc->
2fd42 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c  pTab) ){.    sql
2fd43 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20  ite3_index_info 
2fd44 2a 70 20 3d 20 30 3b 0a 20 20 20 20 62 65 73 74  *p = 0;.    best
2fd45 56 69 72 74 75 61 6c 49 6e 64 65 78 28 70 50 61  VirtualIndex(pPa
2fd46 72 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20  rse, pWC, pSrc, 
2fd47 6e 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72  notReady, pOrder
2fd48 42 79 2c 20 70 43 6f 73 74 2c 20 26 70 29 3b 0a  By, pCost, &p);.
2fd49 20 20 20 20 69 66 28 20 70 2d 3e 6e 65 65 64 54      if( p->needT
2fd4a 6f 46 72 65 65 49 64 78 53 74 72 20 29 7b 0a 20  oFreeIdxStr ){. 
2fd4b 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
2fd4c 65 28 70 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20  e(p->idxStr);.  
2fd4d 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
2fd4e 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62  bFree(pParse->db
2fd4f 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65  , p);.  }else.#e
2fd50 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 62 65 73  ndif.  {.    bes
2fd51 74 42 74 72 65 65 49 6e 64 65 78 28 70 50 61 72  tBtreeIndex(pPar
2fd52 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e  se, pWC, pSrc, n
2fd53 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42  otReady, pOrderB
2fd54 79 2c 20 70 43 6f 73 74 29 3b 0a 20 20 7d 0a 7d  y, pCost);.  }.}
2fd55 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 61 62 6c 65 20  ../*.** Disable 
2fd56 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57 48  a term in the WH
2fd57 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45 78 63  ERE clause.  Exc
2fd58 65 70 74 2c 20 64 6f 20 6e 6f 74 20 64 69 73 61  ept, do not disa
2fd59 62 6c 65 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20  ble the term.** 
2fd5a 69 66 20 69 74 20 63 6f 6e 74 72 6f 6c 73 20 61  if it controls a
2fd5b 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e   LEFT OUTER JOIN
2fd5c 20 61 6e 64 20 69 74 20 64 69 64 20 6e 6f 74 20   and it did not 
2fd5d 6f 72 69 67 69 6e 61 74 65 20 69 6e 20 74 68 65  originate in the
2fd5e 20 4f 4e 0a 2a 2a 20 6f 72 20 55 53 49 4e 47 20   ON.** or USING 
2fd5f 63 6c 61 75 73 65 20 6f 66 20 74 68 61 74 20 6a  clause of that j
2fd60 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73 69  oin..**.** Consi
2fd61 64 65 72 20 74 68 65 20 74 65 72 6d 20 74 32 2e  der the term t2.
2fd62 7a 3d 27 6f 6b 27 20 69 6e 20 74 68 65 20 66 6f  z='ok' in the fo
2fd63 6c 6c 6f 77 69 6e 67 20 71 75 65 72 69 65 73 3a  llowing queries:
2fd64 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 53 45  .**.**   (1)  SE
2fd65 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 4c  LECT * FROM t1 L
2fd66 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 74  EFT JOIN t2 ON t
2fd67 31 2e 61 3d 74 32 2e 78 20 57 48 45 52 45 20 74  1.a=t2.x WHERE t
2fd68 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20 20 28 32  2.z='ok'.**   (2
2fd69 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  )  SELECT * FROM
2fd6a 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32   t1 LEFT JOIN t2
2fd6b 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 41 4e   ON t1.a=t2.x AN
2fd6c 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20  D t2.z='ok'.**  
2fd6d 20 28 33 29 20 20 53 45 4c 45 43 54 20 2a 20 46   (3)  SELECT * F
2fd6e 52 4f 4d 20 74 31 2c 20 74 32 20 57 48 45 52 45  ROM t1, t2 WHERE
2fd6f 20 74 31 2e 61 3d 74 32 2e 78 20 41 4e 44 20 74   t1.a=t2.x AND t
2fd70 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 0a 2a 2a 20 54  2.z='ok'.**.** T
2fd71 68 65 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 73 20  he t2.z='ok' is 
2fd72 64 69 73 61 62 6c 65 64 20 69 6e 20 74 68 65 20  disabled in the 
2fd73 69 6e 20 28 32 29 20 62 65 63 61 75 73 65 20 69  in (2) because i
2fd74 74 20 6f 72 69 67 69 6e 61 74 65 73 0a 2a 2a 20  t originates.** 
2fd75 69 6e 20 74 68 65 20 4f 4e 20 63 6c 61 75 73 65  in the ON clause
2fd76 2e 20 20 54 68 65 20 74 65 72 6d 20 69 73 20 64  .  The term is d
2fd77 69 73 61 62 6c 65 64 20 69 6e 20 28 33 29 20 62  isabled in (3) b
2fd78 65 63 61 75 73 65 20 69 74 20 69 73 20 6e 6f 74  ecause it is not
2fd79 20 70 61 72 74 0a 2a 2a 20 6f 66 20 61 20 4c 45   part.** of a LE
2fd7a 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2e 20 20  FT OUTER JOIN.  
2fd7b 49 6e 20 28 31 29 2c 20 74 68 65 20 74 65 72 6d  In (1), the term
2fd7c 20 69 73 20 6e 6f 74 20 64 69 73 61 62 6c 65 64   is not disabled
2fd7d 2e 0a 2a 2a 0a 2a 2a 20 44 69 73 61 62 6c 69 6e  ..**.** Disablin
2fd7e 67 20 61 20 74 65 72 6d 20 63 61 75 73 65 73 20  g a term causes 
2fd7f 74 68 61 74 20 74 65 72 6d 20 74 6f 20 6e 6f 74  that term to not
2fd80 20 62 65 20 74 65 73 74 65 64 20 69 6e 20 74 68   be tested in th
2fd81 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a 20  e inner loop.** 
2fd82 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 44 69  of the join.  Di
2fd83 73 61 62 6c 69 6e 67 20 69 73 20 61 6e 20 6f 70  sabling is an op
2fd84 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 57 68 65  timization.  Whe
2fd85 6e 20 74 65 72 6d 73 20 61 72 65 20 73 61 74 69  n terms are sati
2fd86 73 66 69 65 64 0a 2a 2a 20 62 79 20 69 6e 64 69  sfied.** by indi
2fd87 63 65 73 2c 20 77 65 20 64 69 73 61 62 6c 65 20  ces, we disable 
2fd88 74 68 65 6d 20 74 6f 20 70 72 65 76 65 6e 74 20  them to prevent 
2fd89 72 65 64 75 6e 64 61 6e 74 20 74 65 73 74 73 20  redundant tests 
2fd8a 69 6e 20 74 68 65 20 69 6e 6e 65 72 0a 2a 2a 20  in the inner.** 
2fd8b 6c 6f 6f 70 2e 20 20 57 65 20 77 6f 75 6c 64 20  loop.  We would 
2fd8c 67 65 74 20 74 68 65 20 63 6f 72 72 65 63 74 20  get the correct 
2fd8d 72 65 73 75 6c 74 73 20 69 66 20 6e 6f 74 68 69  results if nothi
2fd8e 6e 67 20 77 65 72 65 20 65 76 65 72 20 64 69 73  ng were ever dis
2fd8f 61 62 6c 65 64 2c 0a 2a 2a 20 62 75 74 20 6a 6f  abled,.** but jo
2fd90 69 6e 73 20 6d 69 67 68 74 20 72 75 6e 20 61 20  ins might run a 
2fd91 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20 20  little slower.  
2fd92 54 68 65 20 74 72 69 63 6b 20 69 73 20 74 6f 20  The trick is to 
2fd93 64 69 73 61 62 6c 65 20 61 73 20 6d 75 63 68 0a  disable as much.
2fd94 2a 2a 20 61 73 20 77 65 20 63 61 6e 20 77 69 74  ** as we can wit
2fd95 68 6f 75 74 20 64 69 73 61 62 6c 69 6e 67 20 74  hout disabling t
2fd96 6f 6f 20 6d 75 63 68 2e 20 20 49 66 20 77 65 20  oo much.  If we 
2fd97 64 69 73 61 62 6c 65 64 20 69 6e 20 28 31 29 2c  disabled in (1),
2fd98 20 77 65 27 64 20 67 65 74 0a 2a 2a 20 74 68 65   we'd get.** the
2fd99 20 77 72 6f 6e 67 20 61 6e 73 77 65 72 2e 20 20   wrong answer.  
2fd9a 53 65 65 20 74 69 63 6b 65 74 20 23 38 31 33 2e  See ticket #813.
2fd9b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2fd9c 64 69 73 61 62 6c 65 54 65 72 6d 28 57 68 65 72  disableTerm(Wher
2fd9d 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20  eLevel *pLevel, 
2fd9e 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
2fd9f 29 7b 0a 20 20 69 66 28 20 70 54 65 72 6d 0a 20  ){.  if( pTerm. 
2fda0 20 20 20 20 20 26 26 20 41 4c 57 41 59 53 28 28       && ALWAYS((
2fda1 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26  pTerm->wtFlags &
2fda2 20 54 45 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 29   TERM_CODED)==0)
2fda3 0a 20 20 20 20 20 20 26 26 20 28 70 4c 65 76 65  .      && (pLeve
2fda4 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 3d 3d 30 20  l->iLeftJoin==0 
2fda5 7c 7c 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  || ExprHasProper
2fda6 74 79 28 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c  ty(pTerm->pExpr,
2fda7 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 29 0a 20   EP_FromJoin)). 
2fda8 20 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e 77   ){.    pTerm->w
2fda9 74 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43  tFlags |= TERM_C
2fdaa 4f 44 45 44 3b 0a 20 20 20 20 69 66 28 20 70 54  ODED;.    if( pT
2fdab 65 72 6d 2d 3e 69 50 61 72 65 6e 74 3e 3d 30 20  erm->iParent>=0 
2fdac 29 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65  ){.      WhereTe
2fdad 72 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26 70 54  rm *pOther = &pT
2fdae 65 72 6d 2d 3e 70 57 43 2d 3e 61 5b 70 54 65 72  erm->pWC->a[pTer
2fdaf 6d 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20 20 20  m->iParent];.   
2fdb0 20 20 20 69 66 28 20 28 2d 2d 70 4f 74 68 65 72     if( (--pOther
2fdb1 2d 3e 6e 43 68 69 6c 64 29 3d 3d 30 20 29 7b 0a  ->nChild)==0 ){.
2fdb2 20 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54          disableT
2fdb3 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 4f 74 68  erm(pLevel, pOth
2fdb4 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  er);.      }.   
2fdb5 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
2fdb6 43 6f 64 65 20 61 6e 20 4f 50 5f 41 66 66 69 6e  Code an OP_Affin
2fdb7 69 74 79 20 6f 70 63 6f 64 65 20 74 6f 20 61 70  ity opcode to ap
2fdb8 70 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61  ply the column a
2fdb9 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 7a  ffinity string z
2fdba 41 66 66 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 20  Aff.** to the n 
2fdbb 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69  registers starti
2fdbc 6e 67 20 61 74 20 62 61 73 65 2e 20 0a 2a 2a 0a  ng at base. .**.
2fdbd 2a 2a 20 41 73 20 61 6e 20 6f 70 74 69 6d 69 7a  ** As an optimiz
2fdbe 61 74 69 6f 6e 2c 20 53 51 4c 49 54 45 5f 41 46  ation, SQLITE_AF
2fdbf 46 5f 4e 4f 4e 45 20 65 6e 74 72 69 65 73 20 28  F_NONE entries (
2fdc0 77 68 69 63 68 20 61 72 65 20 6e 6f 2d 6f 70 73  which are no-ops
2fdc1 29 20 61 74 20 74 68 65 0a 2a 2a 20 62 65 67 69  ) at the.** begi
2fdc2 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20 6f 66  nning and end of
2fdc3 20 7a 41 66 66 20 61 72 65 20 69 67 6e 6f 72 65   zAff are ignore
2fdc4 64 2e 20 20 49 66 20 61 6c 6c 20 65 6e 74 72 69  d.  If all entri
2fdc5 65 73 20 69 6e 20 7a 41 66 66 20 61 72 65 0a 2a  es in zAff are.*
2fdc6 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  * SQLITE_AFF_NON
2fdc7 45 2c 20 74 68 65 6e 20 6e 6f 20 63 6f 64 65 20  E, then no code 
2fdc8 67 65 74 73 20 67 65 6e 65 72 61 74 65 64 2e 0a  gets generated..
2fdc9 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2fdca 6e 65 20 6d 61 6b 65 73 20 69 74 73 20 6f 77 6e  ne makes its own
2fdcb 20 63 6f 70 79 20 6f 66 20 7a 41 66 66 20 73 6f   copy of zAff so
2fdcc 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 65 72   that the caller
2fdcd 20 69 73 20 66 72 65 65 0a 2a 2a 20 74 6f 20 6d   is free.** to m
2fdce 6f 64 69 66 79 20 7a 41 66 66 20 61 66 74 65 72  odify zAff after
2fdcf 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
2fdd0 74 75 72 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  turns..*/.static
2fdd1 20 76 6f 69 64 20 63 6f 64 65 41 70 70 6c 79 41   void codeApplyA
2fdd2 66 66 69 6e 69 74 79 28 50 61 72 73 65 20 2a 70  ffinity(Parse *p
2fdd3 50 61 72 73 65 2c 20 69 6e 74 20 62 61 73 65 2c  Parse, int base,
2fdd4 20 69 6e 74 20 6e 2c 20 63 68 61 72 20 2a 7a 41   int n, char *zA
2fdd5 66 66 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  ff){.  Vdbe *v =
2fdd6 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
2fdd7 20 20 69 66 28 20 7a 41 66 66 3d 3d 30 20 29 7b    if( zAff==0 ){
2fdd8 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
2fdd9 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
2fdda 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74  ailed );.    ret
2fddb 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  urn;.  }.  asser
2fddc 74 28 20 76 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a  t( v!=0 );..  /*
2fddd 20 41 64 6a 75 73 74 20 62 61 73 65 20 61 6e 64   Adjust base and
2fdde 20 6e 20 74 6f 20 73 6b 69 70 20 6f 76 65 72 20   n to skip over 
2fddf 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20  SQLITE_AFF_NONE 
2fde0 65 6e 74 72 69 65 73 20 61 74 20 74 68 65 20 62  entries at the b
2fde1 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20 61 6e  eginning.  ** an
2fde2 64 20 65 6e 64 20 6f 66 20 74 68 65 20 61 66 66  d end of the aff
2fde3 69 6e 69 74 79 20 73 74 72 69 6e 67 2e 0a 20 20  inity string..  
2fde4 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30 20  */.  while( n>0 
2fde5 26 26 20 7a 41 66 66 5b 30 5d 3d 3d 53 51 4c 49  && zAff[0]==SQLI
2fde6 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20  TE_AFF_NONE ){. 
2fde7 20 20 20 6e 2d 2d 3b 0a 20 20 20 20 62 61 73 65     n--;.    base
2fde8 2b 2b 3b 0a 20 20 20 20 7a 41 66 66 2b 2b 3b 0a  ++;.    zAff++;.
2fde9 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31    }.  while( n>1
2fdea 20 26 26 20 7a 41 66 66 5b 6e 2d 31 5d 3d 3d 53   && zAff[n-1]==S
2fdeb 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29  QLITE_AFF_NONE )
2fdec 7b 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20 7d 0a 0a  {.    n--;.  }..
2fded 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 4f 50    /* Code the OP
2fdee 5f 41 66 66 69 6e 69 74 79 20 6f 70 63 6f 64 65  _Affinity opcode
2fdef 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 79   if there is any
2fdf0 74 68 69 6e 67 20 6c 65 66 74 20 74 6f 20 64 6f  thing left to do
2fdf1 2e 20 2a 2f 0a 20 20 69 66 28 20 6e 3e 30 20 29  . */.  if( n>0 )
2fdf2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2fdf3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 66  eAddOp2(v, OP_Af
2fdf4 66 69 6e 69 74 79 2c 20 62 61 73 65 2c 20 6e 29  finity, base, n)
2fdf5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2fdf6 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
2fdf7 20 7a 41 66 66 2c 20 6e 29 3b 0a 20 20 20 20 73   zAff, n);.    s
2fdf8 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
2fdf9 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
2fdfa 61 72 73 65 2c 20 62 61 73 65 2c 20 6e 29 3b 0a  arse, base, n);.
2fdfb 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65    }.}.../*.** Ge
2fdfc 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2fdfd 61 20 73 69 6e 67 6c 65 20 65 71 75 61 6c 69 74  a single equalit
2fdfe 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48  y term of the WH
2fdff 45 52 45 20 63 6c 61 75 73 65 2e 20 20 41 6e 20  ERE clause.  An 
2fe00 65 71 75 61 6c 69 74 79 0a 2a 2a 20 74 65 72 6d  equality.** term
2fe01 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 58   can be either X
2fe02 3d 65 78 70 72 20 6f 72 20 58 20 49 4e 20 28 2e  =expr or X IN (.
2fe03 2e 2e 29 2e 20 20 20 70 54 65 72 6d 20 69 73 20  ..).   pTerm is 
2fe04 74 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20 0a  the term to be .
2fe05 2a 2a 20 63 6f 64 65 64 2e 0a 2a 2a 0a 2a 2a 20  ** coded..**.** 
2fe06 54 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  The current valu
2fe07 65 20 66 6f 72 20 74 68 65 20 63 6f 6e 73 74 72  e for the constr
2fe08 61 69 6e 74 20 69 73 20 6c 65 66 74 20 69 6e 20  aint is left in 
2fe09 72 65 67 69 73 74 65 72 20 69 52 65 67 2e 0a 2a  register iReg..*
2fe0a 2a 0a 2a 2a 20 46 6f 72 20 61 20 63 6f 6e 73 74  *.** For a const
2fe0b 72 61 69 6e 74 20 6f 66 20 74 68 65 20 66 6f 72  raint of the for
2fe0c 6d 20 58 3d 65 78 70 72 2c 20 74 68 65 20 65 78  m X=expr, the ex
2fe0d 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76 61 6c  pression is eval
2fe0e 75 61 74 65 64 20 61 6e 64 20 69 74 73 0a 2a 2a  uated and its.**
2fe0f 20 72 65 73 75 6c 74 20 69 73 20 6c 65 66 74 20   result is left 
2fe10 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 46  on the stack.  F
2fe11 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f  or constraints o
2fe12 66 20 74 68 65 20 66 6f 72 6d 20 58 20 49 4e 20  f the form X IN 
2fe13 28 2e 2e 2e 29 0a 2a 2a 20 74 68 69 73 20 72 6f  (...).** this ro
2fe14 75 74 69 6e 65 20 73 65 74 73 20 75 70 20 61 20  utine sets up a 
2fe15 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c 20 69  loop that will i
2fe16 74 65 72 61 74 65 20 6f 76 65 72 20 61 6c 6c 20  terate over all 
2fe17 76 61 6c 75 65 73 20 6f 66 20 58 2e 0a 2a 2f 0a  values of X..*/.
2fe18 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 45  static int codeE
2fe19 71 75 61 6c 69 74 79 54 65 72 6d 28 0a 20 20 50  qualityTerm(.  P
2fe1a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2fe1b 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
2fe1c 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57  g context */.  W
2fe1d 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 2c  hereTerm *pTerm,
2fe1e 20 20 20 2f 2a 20 54 68 65 20 74 65 72 6d 20 6f     /* The term o
2fe1f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
2fe20 73 65 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a  se to be coded *
2fe21 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20 2a  /.  WhereLevel *
2fe22 70 4c 65 76 65 6c 2c 20 2f 2a 20 57 68 65 6e 20  pLevel, /* When 
2fe23 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 46 52 4f  level of the FRO
2fe24 4d 20 63 6c 61 75 73 65 20 77 65 20 61 72 65 20  M clause we are 
2fe25 77 6f 72 6b 69 6e 67 20 6f 6e 20 2a 2f 0a 20 20  working on */.  
2fe26 69 6e 74 20 69 54 61 72 67 65 74 20 20 20 20 20  int iTarget     
2fe27 20 20 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 74      /* Attempt t
2fe28 6f 20 6c 65 61 76 65 20 72 65 73 75 6c 74 73 20  o leave results 
2fe29 69 6e 20 74 68 69 73 20 72 65 67 69 73 74 65 72  in this register
2fe2a 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
2fe2b 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  X = pTerm->pExpr
2fe2c 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  ;.  Vdbe *v = pP
2fe2d 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
2fe2e 6e 74 20 69 52 65 67 3b 20 20 20 20 20 20 20 20  nt iReg;        
2fe2f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
2fe30 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 72 65  ister holding re
2fe31 73 75 6c 74 73 20 2a 2f 0a 0a 20 20 61 73 73 65  sults */..  asse
2fe32 72 74 28 20 69 54 61 72 67 65 74 3e 30 20 29 3b  rt( iTarget>0 );
2fe33 0a 20 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54  .  if( pX->op==T
2fe34 4b 5f 45 51 20 29 7b 0a 20 20 20 20 69 52 65 67  K_EQ ){.    iReg
2fe35 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
2fe36 64 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c  deTarget(pParse,
2fe37 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 69 54 61   pX->pRight, iTa
2fe38 72 67 65 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69  rget);.  }else i
2fe39 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 53  f( pX->op==TK_IS
2fe3a 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 52 65 67  NULL ){.    iReg
2fe3b 20 3d 20 69 54 61 72 67 65 74 3b 0a 20 20 20 20   = iTarget;.    
2fe3c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2fe3d 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
2fe3e 20 69 52 65 67 29 3b 0a 23 69 66 6e 64 65 66 20   iReg);.#ifndef 
2fe3f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
2fe40 55 45 52 59 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  UERY.  }else{.  
2fe41 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 20 20 20    int eType;.   
2fe42 20 69 6e 74 20 69 54 61 62 3b 0a 20 20 20 20 73   int iTab;.    s
2fe43 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 2a 70 49  truct InLoop *pI
2fe44 6e 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  n;..    assert( 
2fe45 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b  pX->op==TK_IN );
2fe46 0a 20 20 20 20 69 52 65 67 20 3d 20 69 54 61 72  .    iReg = iTar
2fe47 67 65 74 3b 0a 20 20 20 20 65 54 79 70 65 20 3d  get;.    eType =
2fe48 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49 6e   sqlite3FindInIn
2fe49 64 65 78 28 70 50 61 72 73 65 2c 20 70 58 2c 20  dex(pParse, pX, 
2fe4a 30 29 3b 0a 20 20 20 20 69 54 61 62 20 3d 20 70  0);.    iTab = p
2fe4b 58 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 73  X->iTable;.    s
2fe4c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2fe4d 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
2fe4e 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 61 73 73  Tab, 0);.    ass
2fe4f 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  ert( pLevel->pla
2fe50 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
2fe51 45 5f 49 4e 5f 41 42 4c 45 20 29 3b 0a 20 20 20  E_IN_ABLE );.   
2fe52 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 75 2e 69   if( pLevel->u.i
2fe53 6e 2e 6e 49 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  n.nIn==0 ){.    
2fe54 20 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78    pLevel->addrNx
2fe55 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  t = sqlite3VdbeM
2fe56 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
2fe57 20 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 75   }.    pLevel->u
2fe58 2e 69 6e 2e 6e 49 6e 2b 2b 3b 0a 20 20 20 20 70  .in.nIn++;.    p
2fe59 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c  Level->u.in.aInL
2fe5a 6f 6f 70 20 3d 0a 20 20 20 20 20 20 20 73 71 6c  oop =.       sql
2fe5b 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
2fe5c 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ree(pParse->db, 
2fe5d 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e  pLevel->u.in.aIn
2fe5e 4c 6f 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20  Loop,.          
2fe5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe60 20 20 20 20 73 69 7a 65 6f 66 28 70 4c 65 76 65      sizeof(pLeve
2fe61 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 5b  l->u.in.aInLoop[
2fe62 30 5d 29 2a 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  0])*pLevel->u.in
2fe63 2e 6e 49 6e 29 3b 0a 20 20 20 20 70 49 6e 20 3d  .nIn);.    pIn =
2fe64 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49   pLevel->u.in.aI
2fe65 6e 4c 6f 6f 70 3b 0a 20 20 20 20 69 66 28 20 70  nLoop;.    if( p
2fe66 49 6e 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 20  In ){.      pIn 
2fe67 2b 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e  += pLevel->u.in.
2fe68 6e 49 6e 20 2d 20 31 3b 0a 20 20 20 20 20 20 70  nIn - 1;.      p
2fe69 49 6e 2d 3e 69 43 75 72 20 3d 20 69 54 61 62 3b  In->iCur = iTab;
2fe6a 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65  .      if( eType
2fe6b 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44  ==IN_INDEX_ROWID
2fe6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49 6e 2d   ){.        pIn-
2fe6d 3e 61 64 64 72 49 6e 54 6f 70 20 3d 20 73 71 6c  >addrInTop = sql
2fe6e 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2fe6f 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54 61 62  , OP_Rowid, iTab
2fe70 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20 20 7d  , iReg);.      }
2fe71 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 49  else{.        pI
2fe72 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20 3d 20 73  n->addrInTop = s
2fe73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2fe74 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69  (v, OP_Column, i
2fe75 54 61 62 2c 20 30 2c 20 69 52 65 67 29 3b 0a 20  Tab, 0, iReg);. 
2fe76 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
2fe77 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2fe78 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 69 52 65  , OP_IsNull, iRe
2fe79 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  g);.    }else{. 
2fe7a 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69       pLevel->u.i
2fe7b 6e 2e 6e 49 6e 20 3d 20 30 3b 0a 20 20 20 20 7d  n.nIn = 0;.    }
2fe7c 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 64 69  .#endif.  }.  di
2fe7d 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
2fe7e 2c 20 70 54 65 72 6d 29 3b 0a 20 20 72 65 74 75  , pTerm);.  retu
2fe7f 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn iReg;.}../*.*
2fe80 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2fe81 74 68 61 74 20 77 69 6c 6c 20 65 76 61 6c 75 61  that will evalua
2fe82 74 65 20 61 6c 6c 20 3d 3d 20 61 6e 64 20 49 4e  te all == and IN
2fe83 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f 72   constraints for
2fe84 20 61 6e 0a 2a 2a 20 69 6e 64 65 78 2e 0a 2a 2a   an.** index..**
2fe85 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  .** For example,
2fe86 20 63 6f 6e 73 69 64 65 72 20 74 61 62 6c 65 20   consider table 
2fe87 74 31 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20  t1(a,b,c,d,e,f) 
2fe88 77 69 74 68 20 69 6e 64 65 78 20 69 31 28 61 2c  with index i1(a,
2fe89 62 2c 63 29 2e 0a 2a 2a 20 53 75 70 70 6f 73 65  b,c)..** Suppose
2fe8a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2fe8b 65 20 69 73 20 74 68 69 73 3a 20 20 61 3d 3d 35  e is this:  a==5
2fe8c 20 41 4e 44 20 62 20 49 4e 20 28 31 2c 32 2c 33   AND b IN (1,2,3
2fe8d 29 20 41 4e 44 20 63 3e 35 20 41 4e 44 20 63 3c  ) AND c>5 AND c<
2fe8e 31 30 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20  10.** The index 
2fe8f 68 61 73 20 61 73 20 6d 61 6e 79 20 61 73 20 74  has as many as t
2fe90 68 72 65 65 20 65 71 75 61 6c 69 74 79 20 63 6f  hree equality co
2fe91 6e 73 74 72 61 69 6e 74 73 2c 20 62 75 74 20 69  nstraints, but i
2fe92 6e 20 74 68 69 73 0a 2a 2a 20 65 78 61 6d 70 6c  n this.** exampl
2fe93 65 2c 20 74 68 65 20 74 68 69 72 64 20 22 63 22  e, the third "c"
2fe94 20 76 61 6c 75 65 20 69 73 20 61 6e 20 69 6e 65   value is an ine
2fe95 71 75 61 6c 69 74 79 2e 20 20 53 6f 20 6f 6e 6c  quality.  So onl
2fe96 79 20 74 77 6f 20 0a 2a 2a 20 63 6f 6e 73 74 72  y two .** constr
2fe97 61 69 6e 74 73 20 61 72 65 20 63 6f 64 65 64 2e  aints are coded.
2fe98 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77    This routine w
2fe99 69 6c 6c 20 67 65 6e 65 72 61 74 65 20 63 6f 64  ill generate cod
2fe9a 65 20 74 6f 20 65 76 61 6c 75 61 74 65 0a 2a 2a  e to evaluate.**
2fe9b 20 61 3d 3d 35 20 61 6e 64 20 62 20 49 4e 20 28   a==5 and b IN (
2fe9c 31 2c 32 2c 33 29 2e 20 20 54 68 65 20 63 75 72  1,2,3).  The cur
2fe9d 72 65 6e 74 20 76 61 6c 75 65 73 20 66 6f 72 20  rent values for 
2fe9e 61 20 61 6e 64 20 62 20 77 69 6c 6c 20 62 65 20  a and b will be 
2fe9f 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 63 6f 6e  stored.** in con
2fea0 73 65 63 75 74 69 76 65 20 72 65 67 69 73 74 65  secutive registe
2fea1 72 73 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  rs and the index
2fea2 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 72 65   of the first re
2fea3 67 69 73 74 65 72 20 69 73 20 72 65 74 75 72 6e  gister is return
2fea4 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  ed..**.** In the
2fea5 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 20 6e   example above n
2fea6 45 71 3d 3d 32 2e 20 20 42 75 74 20 74 68 69 73  Eq==2.  But this
2fea7 20 73 75 62 72 6f 75 74 69 6e 65 20 77 6f 72 6b   subroutine work
2fea8 73 20 66 6f 72 20 61 6e 79 20 76 61 6c 75 65 0a  s for any value.
2fea9 2a 2a 20 6f 66 20 6e 45 71 20 69 6e 63 6c 75 64  ** of nEq includ
2feaa 69 6e 67 20 30 2e 20 20 49 66 20 6e 45 71 3d 3d  ing 0.  If nEq==
2feab 30 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  0, this routine 
2feac 69 73 20 6e 65 61 72 6c 79 20 61 20 6e 6f 2d 6f  is nearly a no-o
2fead 70 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74  p..** The only t
2feae 68 69 6e 67 20 69 74 20 64 6f 65 73 20 69 73 20  hing it does is 
2feaf 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 70 4c 65  allocate the pLe
2feb0 76 65 6c 2d 3e 69 4d 65 6d 20 6d 65 6d 6f 72 79  vel->iMem memory
2feb1 20 63 65 6c 6c 20 61 6e 64 0a 2a 2a 20 63 6f 6d   cell and.** com
2feb2 70 75 74 65 20 74 68 65 20 61 66 66 69 6e 69 74  pute the affinit
2feb3 79 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  y string..**.** 
2feb4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 77  This routine alw
2feb5 61 79 73 20 61 6c 6c 6f 63 61 74 65 73 20 61 74  ays allocates at
2feb6 20 6c 65 61 73 74 20 6f 6e 65 20 6d 65 6d 6f 72   least one memor
2feb7 79 20 63 65 6c 6c 20 61 6e 64 20 72 65 74 75 72  y cell and retur
2feb8 6e 73 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  ns.** the index 
2feb9 6f 66 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 63  of that memory c
2feba 65 6c 6c 2e 20 54 68 65 20 63 6f 64 65 20 74 68  ell. The code th
2febb 61 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 68 69 73  at.** calls this
2febc 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 75 73   routine will us
2febd 65 20 74 68 61 74 20 6d 65 6d 6f 72 79 20 63 65  e that memory ce
2febe 6c 6c 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ll to store the 
2febf 74 65 72 6d 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6b  termination.** k
2fec0 65 79 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ey value of the 
2fec1 6c 6f 6f 70 2e 20 20 49 66 20 6f 6e 65 20 6f 72  loop.  If one or
2fec2 20 6d 6f 72 65 20 49 4e 20 6f 70 65 72 61 74 6f   more IN operato
2fec3 72 73 20 61 70 70 65 61 72 2c 20 74 68 65 6e 0a  rs appear, then.
2fec4 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
2fec5 61 6c 6c 6f 63 61 74 65 73 20 61 6e 20 61 64 64  allocates an add
2fec6 69 74 69 6f 6e 61 6c 20 6e 45 71 20 6d 65 6d 6f  itional nEq memo
2fec7 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 69 6e 74  ry cells for int
2fec8 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 2e 0a 2a 2a  ernal.** use..**
2fec9 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72  .** Before retur
2feca 6e 69 6e 67 2c 20 2a 70 7a 41 66 66 20 69 73 20  ning, *pzAff is 
2fecb 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  set to point to 
2fecc 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e  a buffer contain
2fecd 69 6e 67 20 61 0a 2a 2a 20 63 6f 70 79 20 6f 66  ing a.** copy of
2fece 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   the column affi
2fecf 6e 69 74 79 20 73 74 72 69 6e 67 20 6f 66 20 74  nity string of t
2fed0 68 65 20 69 6e 64 65 78 20 61 6c 6c 6f 63 61 74  he index allocat
2fed1 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69  ed using.** sqli
2fed2 74 65 33 44 62 4d 61 6c 6c 6f 63 28 29 2e 20 45  te3DbMalloc(). E
2fed3 78 63 65 70 74 2c 20 65 6e 74 72 69 65 73 20 69  xcept, entries i
2fed4 6e 20 74 68 65 20 63 6f 70 79 20 6f 66 20 74 68  n the copy of th
2fed5 65 20 73 74 72 69 6e 67 20 61 73 73 6f 63 69 61  e string associa
2fed6 74 65 64 0a 2a 2a 20 77 69 74 68 20 65 71 75 61  ted.** with equa
2fed7 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2fed8 20 74 68 61 74 20 75 73 65 20 4e 4f 4e 45 20 61   that use NONE a
2fed9 66 66 69 6e 69 74 79 20 61 72 65 20 73 65 74 20  ffinity are set 
2feda 74 6f 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46  to.** SQLITE_AFF
2fedb 5f 4e 4f 4e 45 2e 20 54 68 69 73 20 69 73 20 74  _NONE. This is t
2fedc 6f 20 64 65 61 6c 20 77 69 74 68 20 53 51 4c 20  o deal with SQL 
2fedd 73 75 63 68 20 61 73 20 74 68 65 20 66 6f 6c 6c  such as the foll
2fede 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 43  owing:.**.**   C
2fedf 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61  REATE TABLE t1(a
2fee0 20 54 45 58 54 20 50 52 49 4d 41 52 59 20 4b 45   TEXT PRIMARY KE
2fee1 59 2c 20 62 29 3b 0a 2a 2a 20 20 20 53 45 4c 45  Y, b);.**   SELE
2fee2 43 54 20 2e 2e 2e 20 46 52 4f 4d 20 74 31 20 41  CT ... FROM t1 A
2fee3 53 20 74 32 2c 20 74 31 20 57 48 45 52 45 20 74  S t2, t1 WHERE t
2fee4 31 2e 61 20 3d 20 74 32 2e 62 3b 0a 2a 2a 0a 2a  1.a = t2.b;.**.*
2fee5 2a 20 49 6e 20 74 68 65 20 65 78 61 6d 70 6c 65  * In the example
2fee6 20 61 62 6f 76 65 2c 20 74 68 65 20 69 6e 64 65   above, the inde
2fee7 78 20 6f 6e 20 74 31 28 61 29 20 68 61 73 20 54  x on t1(a) has T
2fee8 45 58 54 20 61 66 66 69 6e 69 74 79 2e 20 42 75  EXT affinity. Bu
2fee9 74 20 73 69 6e 63 65 0a 2a 2a 20 74 68 65 20 72  t since.** the r
2feea 69 67 68 74 20 68 61 6e 64 20 73 69 64 65 20 6f  ight hand side o
2feeb 66 20 74 68 65 20 65 71 75 61 6c 69 74 79 20 63  f the equality c
2feec 6f 6e 73 74 72 61 69 6e 74 20 28 74 32 2e 62 29  onstraint (t2.b)
2feed 20 68 61 73 20 4e 4f 4e 45 20 61 66 66 69 6e 69   has NONE affini
2feee 74 79 2c 0a 2a 2a 20 6e 6f 20 63 6f 6e 76 65 72  ty,.** no conver
2feef 73 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 61  sion should be a
2fef0 74 74 65 6d 70 74 65 64 20 62 65 66 6f 72 65 20  ttempted before 
2fef1 75 73 69 6e 67 20 61 20 74 32 2e 62 20 76 61 6c  using a t2.b val
2fef2 75 65 20 61 73 20 70 61 72 74 20 6f 66 0a 2a 2a  ue as part of.**
2fef3 20 61 20 6b 65 79 20 74 6f 20 73 65 61 72 63 68   a key to search
2fef4 20 74 68 65 20 69 6e 64 65 78 2e 20 48 65 6e 63   the index. Henc
2fef5 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  e the first byte
2fef6 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64   in the returned
2fef7 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 73 74 72   affinity.** str
2fef8 69 6e 67 20 69 6e 20 74 68 69 73 20 65 78 61 6d  ing in this exam
2fef9 70 6c 65 20 77 6f 75 6c 64 20 62 65 20 73 65 74  ple would be set
2fefa 20 74 6f 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e   to SQLITE_AFF_N
2fefb 4f 4e 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ONE..*/.static i
2fefc 6e 74 20 63 6f 64 65 41 6c 6c 45 71 75 61 6c 69  nt codeAllEquali
2fefd 74 79 54 65 72 6d 73 28 0a 20 20 50 61 72 73 65  tyTerms(.  Parse
2fefe 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2feff 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
2ff00 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65  ext */.  WhereLe
2ff01 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 20 20 2f  vel *pLevel,   /
2ff02 2a 20 57 68 69 63 68 20 6e 65 73 74 65 64 20 6c  * Which nested l
2ff03 6f 6f 70 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  oop of the FROM 
2ff04 77 65 20 61 72 65 20 63 6f 64 69 6e 67 20 2a 2f  we are coding */
2ff05 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2ff06 70 57 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  pWC,     /* The 
2ff07 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2ff08 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61    Bitmask notRea
2ff09 64 79 2c 20 20 20 20 20 2f 2a 20 57 68 69 63 68  dy,     /* Which
2ff0a 20 70 61 72 74 73 20 6f 66 20 46 52 4f 4d 20 68   parts of FROM h
2ff0b 61 76 65 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  ave not yet been
2ff0c 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20   coded */.  int 
2ff0d 6e 45 78 74 72 61 52 65 67 2c 20 20 20 20 20 20  nExtraReg,      
2ff0e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
2ff0f 78 74 72 61 20 72 65 67 69 73 74 65 72 73 20 74  xtra registers t
2ff10 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20  o allocate */.  
2ff11 63 68 61 72 20 2a 2a 70 7a 41 66 66 20 20 20 20  char **pzAff    
2ff12 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65        /* OUT: Se
2ff13 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 66  t to point to af
2ff14 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 2a 2f  finity string */
2ff15 0a 29 7b 0a 20 20 69 6e 74 20 6e 45 71 20 3d 20  .){.  int nEq = 
2ff16 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e 45 71  pLevel->plan.nEq
2ff17 3b 20 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65  ;   /* The numbe
2ff18 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f  r of == or IN co
2ff19 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 63 6f 64  nstraints to cod
2ff1a 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d  e */.  Vdbe *v =
2ff1b 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20   pParse->pVdbe; 
2ff1c 20 20 20 20 20 2f 2a 20 54 68 65 20 76 6d 20 75       /* The vm u
2ff1d 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
2ff1e 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  n */.  Index *pI
2ff1f 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
2ff20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65       /* The inde
2ff21 78 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f 72  x being used for
2ff22 20 74 68 69 73 20 6c 6f 6f 70 20 2a 2f 0a 20 20   this loop */.  
2ff23 69 6e 74 20 69 43 75 72 20 3d 20 70 4c 65 76 65  int iCur = pLeve
2ff24 6c 2d 3e 69 54 61 62 43 75 72 3b 20 20 20 2f 2a  l->iTabCur;   /*
2ff25 20 54 68 65 20 63 75 72 73 6f 72 20 6f 66 20 74   The cursor of t
2ff26 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 57 68  he table */.  Wh
2ff27 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 20  ereTerm *pTerm; 
2ff28 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
2ff29 20 73 69 6e 67 6c 65 20 63 6f 6e 73 74 72 61 69   single constrai
2ff2a 6e 74 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74  nt term */.  int
2ff2b 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   j;             
2ff2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
2ff2d 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
2ff2e 69 6e 74 20 72 65 67 42 61 73 65 3b 20 20 20 20  int regBase;    
2ff2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ff30 20 42 61 73 65 20 72 65 67 69 73 74 65 72 20 2a   Base register *
2ff31 2f 0a 20 20 69 6e 74 20 6e 52 65 67 3b 20 20 20  /.  int nReg;   
2ff32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff33 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
2ff34 65 67 69 73 74 65 72 73 20 74 6f 20 61 6c 6c 6f  egisters to allo
2ff35 63 61 74 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  cate */.  char *
2ff36 7a 41 66 66 3b 20 20 20 20 20 20 20 20 20 20 20  zAff;           
2ff37 20 20 20 20 20 20 20 20 2f 2a 20 41 66 66 69 6e          /* Affin
2ff38 69 74 79 20 73 74 72 69 6e 67 20 74 6f 20 72 65  ity string to re
2ff39 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68  turn */..  /* Th
2ff3a 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 6f 6e 6c  is module is onl
2ff3b 79 20 63 61 6c 6c 65 64 20 6f 6e 20 71 75 65 72  y called on quer
2ff3c 79 20 70 6c 61 6e 73 20 74 68 61 74 20 75 73 65  y plans that use
2ff3d 20 61 6e 20 69 6e 64 65 78 2e 20 2a 2f 0a 20 20   an index. */.  
2ff3e 61 73 73 65 72 74 28 20 70 4c 65 76 65 6c 2d 3e  assert( pLevel->
2ff3f 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
2ff40 48 45 52 45 5f 49 4e 44 45 58 45 44 20 29 3b 0a  HERE_INDEXED );.
2ff41 20 20 70 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d    pIdx = pLevel-
2ff42 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 0a 20  >plan.u.pIdx;.. 
2ff43 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
2ff44 6f 77 20 6d 61 6e 79 20 6d 65 6d 6f 72 79 20 63  ow many memory c
2ff45 65 6c 6c 73 20 77 65 20 77 69 6c 6c 20 6e 65 65  ells we will nee
2ff46 64 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20  d then allocate 
2ff47 74 68 65 6d 2e 0a 20 20 2a 2f 0a 20 20 72 65 67  them..  */.  reg
2ff48 42 61 73 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e  Base = pParse->n
2ff49 4d 65 6d 20 2b 20 31 3b 0a 20 20 6e 52 65 67 20  Mem + 1;.  nReg 
2ff4a 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 6e  = pLevel->plan.n
2ff4b 45 71 20 2b 20 6e 45 78 74 72 61 52 65 67 3b 0a  Eq + nExtraReg;.
2ff4c 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
2ff4d 3d 20 6e 52 65 67 3b 0a 0a 20 20 7a 41 66 66 20  = nReg;..  zAff 
2ff4e 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
2ff4f 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 71  p(pParse->db, sq
2ff50 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69  lite3IndexAffini
2ff51 74 79 53 74 72 28 76 2c 20 70 49 64 78 29 29 3b  tyStr(v, pIdx));
2ff52 0a 20 20 69 66 28 20 21 7a 41 66 66 20 29 7b 0a  .  if( !zAff ){.
2ff53 20 20 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e      pParse->db->
2ff54 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
2ff55 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 76 61 6c  ;.  }..  /* Eval
2ff56 75 61 74 65 20 74 68 65 20 65 71 75 61 6c 69 74  uate the equalit
2ff57 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20  y constraints.  
2ff58 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 64  */.  assert( pId
2ff59 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6e 45 71 20  x->nColumn>=nEq 
2ff5a 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  );.  for(j=0; j<
2ff5b 6e 45 71 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69  nEq; j++){.    i
2ff5c 6e 74 20 72 31 3b 0a 20 20 20 20 69 6e 74 20 6b  nt r1;.    int k
2ff5d 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d   = pIdx->aiColum
2ff5e 6e 5b 6a 5d 3b 0a 20 20 20 20 70 54 65 72 6d 20  n[j];.    pTerm 
2ff5f 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
2ff60 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64  iCur, k, notRead
2ff61 79 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  y, pLevel->plan.
2ff62 77 73 46 6c 61 67 73 2c 20 70 49 64 78 29 3b 0a  wsFlags, pIdx);.
2ff63 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 70 54      if( NEVER(pT
2ff64 65 72 6d 3d 3d 30 29 20 29 20 62 72 65 61 6b 3b  erm==0) ) break;
2ff65 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 54  .    assert( (pT
2ff66 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
2ff67 45 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 20 29 3b  ERM_CODED)==0 );
2ff68 0a 20 20 20 20 72 31 20 3d 20 63 6f 64 65 45 71  .    r1 = codeEq
2ff69 75 61 6c 69 74 79 54 65 72 6d 28 70 50 61 72 73  ualityTerm(pPars
2ff6a 65 2c 20 70 54 65 72 6d 2c 20 70 4c 65 76 65 6c  e, pTerm, pLevel
2ff6b 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20  , regBase+j);.  
2ff6c 20 20 69 66 28 20 72 31 21 3d 72 65 67 42 61 73    if( r1!=regBas
2ff6d 65 2b 6a 20 29 7b 0a 20 20 20 20 20 20 69 66 28  e+j ){.      if(
2ff6e 20 6e 52 65 67 3d 3d 31 20 29 7b 0a 20 20 20 20   nReg==1 ){.    
2ff6f 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
2ff70 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2ff71 2c 20 72 65 67 42 61 73 65 29 3b 0a 20 20 20 20  , regBase);.    
2ff72 20 20 20 20 72 65 67 42 61 73 65 20 3d 20 72 31      regBase = r1
2ff73 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2ff74 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2ff75 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
2ff76 43 6f 70 79 2c 20 72 31 2c 20 72 65 67 42 61 73  Copy, r1, regBas
2ff77 65 2b 6a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  e+j);.      }.  
2ff78 20 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65    }.    testcase
2ff79 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  ( pTerm->eOperat
2ff7a 6f 72 20 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29  or & WO_ISNULL )
2ff7b 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2ff7c 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2ff7d 20 26 20 57 4f 5f 49 4e 20 29 3b 0a 20 20 20 20   & WO_IN );.    
2ff7e 69 66 28 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65  if( (pTerm->eOpe
2ff7f 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 53 4e 55  rator & (WO_ISNU
2ff80 4c 4c 7c 57 4f 5f 49 4e 29 29 3d 3d 30 20 29 7b  LL|WO_IN))==0 ){
2ff81 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 52 69  .      Expr *pRi
2ff82 67 68 74 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  ght = pTerm->pEx
2ff83 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pr->pRight;.    
2ff84 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2ff85 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76 2c 20 70  eIsNullJump(v, p
2ff86 52 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b 6a  Right, regBase+j
2ff87 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72  , pLevel->addrBr
2ff88 6b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 41  k);.      if( zA
2ff89 66 66 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ff ){.        if
2ff8a 28 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65  ( sqlite3Compare
2ff8b 41 66 66 69 6e 69 74 79 28 70 52 69 67 68 74 2c  Affinity(pRight,
2ff8c 20 7a 41 66 66 5b 6a 5d 29 3d 3d 53 51 4c 49 54   zAff[j])==SQLIT
2ff8d 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20  E_AFF_NONE ){.  
2ff8e 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6a 5d 20          zAff[j] 
2ff8f 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  = SQLITE_AFF_NON
2ff90 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  E;.        }.   
2ff91 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
2ff92 45 78 70 72 4e 65 65 64 73 4e 6f 41 66 66 69 6e  ExprNeedsNoAffin
2ff93 69 74 79 43 68 61 6e 67 65 28 70 52 69 67 68 74  ityChange(pRight
2ff94 2c 20 7a 41 66 66 5b 6a 5d 29 20 29 7b 0a 20 20  , zAff[j]) ){.  
2ff95 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6a 5d 20          zAff[j] 
2ff96 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  = SQLITE_AFF_NON
2ff97 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  E;.        }.   
2ff98 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
2ff99 20 2a 70 7a 41 66 66 20 3d 20 7a 41 66 66 3b 0a   *pzAff = zAff;.
2ff9a 20 20 72 65 74 75 72 6e 20 72 65 67 42 61 73 65    return regBase
2ff9b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  ;.}../*.** Gener
2ff9c 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65  ate code for the
2ff9d 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 69 4c   start of the iL
2ff9e 65 76 65 6c 2d 74 68 20 6c 6f 6f 70 20 69 6e 20  evel-th loop in 
2ff9f 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2ffa0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
2ffa1 6f 6e 20 64 65 73 63 72 69 62 65 64 20 62 79 20  on described by 
2ffa2 70 57 49 6e 66 6f 2e 0a 2a 2f 0a 73 74 61 74 69  pWInfo..*/.stati
2ffa3 63 20 42 69 74 6d 61 73 6b 20 63 6f 64 65 4f 6e  c Bitmask codeOn
2ffa4 65 4c 6f 6f 70 53 74 61 72 74 28 0a 20 20 57 68  eLoopStart(.  Wh
2ffa5 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 2c  ereInfo *pWInfo,
2ffa6 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 69     /* Complete i
2ffa7 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
2ffa8 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2ffa9 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 76 65  e */.  int iLeve
2ffaa 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  l,          /* W
2ffab 68 69 63 68 20 6c 65 76 65 6c 20 6f 66 20 70 57  hich level of pW
2ffac 49 6e 66 6f 2d 3e 61 5b 5d 20 73 68 6f 75 6c 64  Info->a[] should
2ffad 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 75   be coded */.  u
2ffae 31 36 20 77 63 74 72 6c 46 6c 61 67 73 2c 20 20  16 wctrlFlags,  
2ffaf 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
2ffb0 65 20 57 48 45 52 45 5f 2a 20 66 6c 61 67 73 20  e WHERE_* flags 
2ffb1 64 65 66 69 6e 65 64 20 69 6e 20 73 71 6c 69 74  defined in sqlit
2ffb2 65 49 6e 74 2e 68 20 2a 2f 0a 20 20 42 69 74 6d  eInt.h */.  Bitm
2ffb3 61 73 6b 20 6e 6f 74 52 65 61 64 79 20 20 20 20  ask notReady    
2ffb4 20 2f 2a 20 57 68 69 63 68 20 74 61 62 6c 65 73   /* Which tables
2ffb5 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20 61   are currently a
2ffb6 76 61 69 6c 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  vailable */.){. 
2ffb7 20 69 6e 74 20 6a 2c 20 6b 3b 20 20 20 20 20 20   int j, k;      
2ffb8 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2ffb9 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20  unters */.  int 
2ffba 69 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20  iCur;           
2ffbb 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
2ffbc 73 6f 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c  sor for the tabl
2ffbd 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4e  e */.  int addrN
2ffbe 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 57  xt;         /* W
2ffbf 68 65 72 65 20 74 6f 20 6a 75 6d 70 20 74 6f 20  here to jump to 
2ffc0 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74 68  continue with th
2ffc1 65 20 6e 65 78 74 20 49 4e 20 63 61 73 65 20 2a  e next IN case *
2ffc2 2f 0a 20 20 69 6e 74 20 6f 6d 69 74 54 61 62 6c  /.  int omitTabl
2ffc3 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  e;       /* True
2ffc4 20 69 66 20 77 65 20 75 73 65 20 74 68 65 20 69   if we use the i
2ffc5 6e 64 65 78 20 6f 6e 6c 79 20 2a 2f 0a 20 20 69  ndex only */.  i
2ffc6 6e 74 20 62 52 65 76 3b 20 20 20 20 20 20 20 20  nt bRev;        
2ffc7 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77      /* True if w
2ffc8 65 20 6e 65 65 64 20 74 6f 20 73 63 61 6e 20 69  e need to scan i
2ffc9 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20  n reverse order 
2ffca 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20  */.  WhereLevel 
2ffcb 2a 70 4c 65 76 65 6c 3b 20 20 2f 2a 20 54 68 65  *pLevel;  /* The
2ffcc 20 77 68 65 72 65 20 6c 65 76 65 6c 20 74 6f 20   where level to 
2ffcd 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20 57 68  be coded */.  Wh
2ffce 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 3b 20  ereClause *pWC; 
2ffcf 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f 73 69 74     /* Decomposit
2ffd0 69 6f 6e 20 6f 66 20 74 68 65 20 65 6e 74 69 72  ion of the entir
2ffd1 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2ffd2 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  /.  WhereTerm *p
2ffd3 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20  Term;           
2ffd4 20 20 20 20 2f 2a 20 41 20 57 48 45 52 45 20 63      /* A WHERE c
2ffd5 6c 61 75 73 65 20 74 65 72 6d 20 2a 2f 0a 20 20  lause term */.  
2ffd6 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20  Parse *pParse;  
2ffd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ffd8 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2ffd9 78 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b  xt */.  Vdbe *v;
2ffda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ffdb 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2ffdc 72 65 70 61 72 65 64 20 73 74 6d 74 20 75 6e 64  repared stmt und
2ffdd 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 73  er constructions
2ffde 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
2ffdf 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49  List_item *pTabI
2ffe0 74 65 6d 3b 20 20 2f 2a 20 46 52 4f 4d 20 63 6c  tem;  /* FROM cl
2ffe1 61 75 73 65 20 74 65 72 6d 20 62 65 69 6e 67 20  ause term being 
2ffe2 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 61  coded */.  int a
2ffe3 64 64 72 42 72 6b 3b 20 20 20 20 20 20 20 20 20  ddrBrk;         
2ffe4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75             /* Ju
2ffe5 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65 61 6b  mp here to break
2ffe6 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70   out of the loop
2ffe7 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f   */.  int addrCo
2ffe8 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
2ffe9 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
2ffea 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77  re to continue w
2ffeb 69 74 68 20 6e 65 78 74 20 63 79 63 6c 65 20 2a  ith next cycle *
2ffec 2f 0a 20 20 69 6e 74 20 69 52 6f 77 69 64 52 65  /.  int iRowidRe
2ffed 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  g = 0;        /*
2ffee 20 52 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64   Rowid is stored
2ffef 20 69 6e 20 74 68 69 73 20 72 65 67 69 73 74 65   in this registe
2fff0 72 2c 20 69 66 20 6e 6f 74 20 7a 65 72 6f 20 2a  r, if not zero *
2fff1 2f 0a 20 20 69 6e 74 20 69 52 65 6c 65 61 73 65  /.  int iRelease
2fff2 52 65 67 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  Reg = 0;      /*
2fff3 20 54 65 6d 70 20 72 65 67 69 73 74 65 72 20 74   Temp register t
2fff4 6f 20 66 72 65 65 20 62 65 66 6f 72 65 20 72 65  o free before re
2fff5 74 75 72 6e 69 6e 67 20 2a 2f 0a 0a 20 20 70 50  turning */..  pP
2fff6 61 72 73 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 70  arse = pWInfo->p
2fff7 50 61 72 73 65 3b 0a 20 20 76 20 3d 20 70 50 61  Parse;.  v = pPa
2fff8 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 70 57  rse->pVdbe;.  pW
2fff9 43 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 57 43 3b  C = pWInfo->pWC;
2fffa 0a 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57 49  .  pLevel = &pWI
2fffb 6e 66 6f 2d 3e 61 5b 69 4c 65 76 65 6c 5d 3b 0a  nfo->a[iLevel];.
2fffc 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70 57    pTabItem = &pW
2fffd 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 2d 3e  Info->pTabList->
2fffe 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d  a[pLevel->iFrom]
2ffff 3b 0a 20 20 69 43 75 72 20 3d 20 70 54 61 62 49  ;.  iCur = pTabI
30000 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
30001 62 52 65 76 20 3d 20 28 70 4c 65 76 65 6c 2d 3e  bRev = (pLevel->
30002 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
30003 48 45 52 45 5f 52 45 56 45 52 53 45 29 21 3d 30  HERE_REVERSE)!=0
30004 3b 0a 20 20 6f 6d 69 74 54 61 62 6c 65 20 3d 20  ;.  omitTable = 
30005 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
30006 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44  Flags & WHERE_ID
30007 58 5f 4f 4e 4c 59 29 21 3d 30 20 0a 20 20 20 20  X_ONLY)!=0 .    
30008 20 20 20 20 20 20 20 26 26 20 28 77 63 74 72 6c         && (wctrl
30009 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 46 4f  Flags & WHERE_FO
3000a 52 43 45 5f 54 41 42 4c 45 29 3d 3d 30 3b 0a 0a  RCE_TABLE)==0;..
3000b 20 20 2f 2a 20 43 72 65 61 74 65 20 6c 61 62 65    /* Create labe
3000c 6c 73 20 66 6f 72 20 74 68 65 20 22 62 72 65 61  ls for the "brea
3000d 6b 22 20 61 6e 64 20 22 63 6f 6e 74 69 6e 75 65  k" and "continue
3000e 22 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 0a 20  " instructions. 
3000f 20 2a 2a 20 66 6f 72 20 74 68 65 20 63 75 72 72   ** for the curr
30010 65 6e 74 20 6c 6f 6f 70 2e 20 20 4a 75 6d 70 20  ent loop.  Jump 
30011 74 6f 20 61 64 64 72 42 72 6b 20 74 6f 20 62 72  to addrBrk to br
30012 65 61 6b 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f  eak out of a loo
30013 70 2e 0a 20 20 2a 2a 20 4a 75 6d 70 20 74 6f 20  p..  ** Jump to 
30014 63 6f 6e 74 20 74 6f 20 67 6f 20 69 6d 6d 65 64  cont to go immed
30015 69 61 74 65 6c 79 20 74 6f 20 74 68 65 20 6e 65  iately to the ne
30016 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  xt iteration of 
30017 74 68 65 0a 20 20 2a 2a 20 6c 6f 6f 70 2e 0a 20  the.  ** loop.. 
30018 20 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 68   **.  ** When th
30019 65 72 65 20 69 73 20 61 6e 20 49 4e 20 6f 70 65  ere is an IN ope
3001a 72 61 74 6f 72 2c 20 77 65 20 61 6c 73 6f 20 68  rator, we also h
3001b 61 76 65 20 61 20 22 61 64 64 72 4e 78 74 22 20  ave a "addrNxt" 
3001c 6c 61 62 65 6c 20 74 68 61 74 0a 20 20 2a 2a 20  label that.  ** 
3001d 6d 65 61 6e 73 20 74 6f 20 63 6f 6e 74 69 6e 75  means to continu
3001e 65 20 77 69 74 68 20 74 68 65 20 6e 65 78 74 20  e with the next 
3001f 49 4e 20 76 61 6c 75 65 20 63 6f 6d 62 69 6e 61  IN value combina
30020 74 69 6f 6e 2e 20 20 57 68 65 6e 0a 20 20 2a 2a  tion.  When.  **
30021 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 49 4e   there are no IN
30022 20 6f 70 65 72 61 74 6f 72 73 20 69 6e 20 74 68   operators in th
30023 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74  e constraints, t
30024 68 65 20 22 61 64 64 72 4e 78 74 22 20 6c 61 62  he "addrNxt" lab
30025 65 6c 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 73  el.  ** is the s
30026 61 6d 65 20 61 73 20 22 61 64 64 72 42 72 6b 22  ame as "addrBrk"
30027 2e 0a 20 20 2a 2f 0a 20 20 61 64 64 72 42 72 6b  ..  */.  addrBrk
30028 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42   = pLevel->addrB
30029 72 6b 20 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64  rk = pLevel->add
3002a 72 4e 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64  rNxt = sqlite3Vd
3002b 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
3002c 20 20 61 64 64 72 43 6f 6e 74 20 3d 20 70 4c 65    addrCont = pLe
3002d 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 20 3d 20  vel->addrCont = 
3002e 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
3002f 61 62 65 6c 28 76 29 3b 0a 0a 20 20 2f 2a 20 49  abel(v);..  /* I
30030 66 20 74 68 69 73 20 69 73 20 74 68 65 20 72 69  f this is the ri
30031 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20 4c  ght table of a L
30032 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20  EFT OUTER JOIN, 
30033 61 6c 6c 6f 63 61 74 65 20 61 6e 64 0a 20 20 2a  allocate and.  *
30034 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6d  * initialize a m
30035 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20  emory cell that 
30036 72 65 63 6f 72 64 73 20 69 66 20 74 68 69 73 20  records if this 
30037 74 61 62 6c 65 20 6d 61 74 63 68 65 73 20 61 6e  table matches an
30038 79 0a 20 20 2a 2a 20 72 6f 77 20 6f 66 20 74 68  y.  ** row of th
30039 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66 20  e left table of 
3003a 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20  the join..  */. 
3003b 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 46 72   if( pLevel->iFr
3003c 6f 6d 3e 30 20 26 26 20 28 70 54 61 62 49 74 65  om>0 && (pTabIte
3003d 6d 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 26 20  m[0].jointype & 
3003e 4a 54 5f 4c 45 46 54 29 21 3d 30 20 29 7b 0a 20  JT_LEFT)!=0 ){. 
3003f 20 20 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74     pLevel->iLeft
30040 4a 6f 69 6e 20 3d 20 2b 2b 70 50 61 72 73 65 2d  Join = ++pParse-
30041 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >nMem;.    sqlit
30042 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
30043 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 70  OP_Integer, 0, p
30044 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
30045 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
30046 6e 74 28 28 76 2c 20 22 69 6e 69 74 20 4c 45 46  nt((v, "init LEF
30047 54 20 4a 4f 49 4e 20 6e 6f 2d 6d 61 74 63 68 20  T JOIN no-match 
30048 66 6c 61 67 22 29 29 3b 0a 20 20 7d 0a 0a 23 69  flag"));.  }..#i
30049 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3004a 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
3004b 20 69 66 28 20 20 28 70 4c 65 76 65 6c 2d 3e 70   if(  (pLevel->p
3004c 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
3004d 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  ERE_VIRTUALTABLE
3004e 29 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43  )!=0 ){.    /* C
3004f 61 73 65 20 30 3a 20 20 54 68 65 20 74 61 62 6c  ase 0:  The tabl
30050 65 20 69 73 20 61 20 76 69 72 74 75 61 6c 2d 74  e is a virtual-t
30051 61 62 6c 65 2e 20 20 55 73 65 20 74 68 65 20 56  able.  Use the V
30052 46 69 6c 74 65 72 20 61 6e 64 20 56 4e 65 78 74  Filter and VNext
30053 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
30054 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 64   to access the d
30055 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ata..    */.    
30056 69 6e 74 20 69 52 65 67 3b 20 20 20 2f 2a 20 50  int iReg;   /* P
30057 33 20 56 61 6c 75 65 20 66 6f 72 20 4f 50 5f 56  3 Value for OP_V
30058 46 69 6c 74 65 72 20 2a 2f 0a 20 20 20 20 73 71  Filter */.    sq
30059 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
3005a 20 2a 70 56 74 61 62 49 64 78 20 3d 20 70 4c 65   *pVtabIdx = pLe
3005b 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61  vel->plan.u.pVta
3005c 62 49 64 78 3b 0a 20 20 20 20 69 6e 74 20 6e 43  bIdx;.    int nC
3005d 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70 56 74 61  onstraint = pVta
3005e 62 49 64 78 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e  bIdx->nConstrain
3005f 74 3b 0a 20 20 20 20 73 74 72 75 63 74 20 73 71  t;.    struct sq
30060 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73  lite3_index_cons
30061 74 72 61 69 6e 74 5f 75 73 61 67 65 20 2a 61 55  traint_usage *aU
30062 73 61 67 65 20 3d 0a 20 20 20 20 20 20 20 20 20  sage =.         
30063 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30064 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30065 20 20 20 20 20 20 20 70 56 74 61 62 49 64 78 2d         pVtabIdx-
30066 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67  >aConstraintUsag
30067 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 74 72  e;.    const str
30068 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
30069 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a 61 43  x_constraint *aC
3006a 6f 6e 73 74 72 61 69 6e 74 20 3d 0a 20 20 20 20  onstraint =.    
3006b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3006c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3006d 20 20 20 20 20 20 20 20 20 20 20 20 70 56 74 61              pVta
3006e 62 49 64 78 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  bIdx->aConstrain
3006f 74 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 45  t;..    sqlite3E
30070 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50 61  xprCachePush(pPa
30071 72 73 65 29 3b 0a 20 20 20 20 69 52 65 67 20 3d  rse);.    iReg =
30072 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
30073 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 43 6f  ange(pParse, nCo
30074 6e 73 74 72 61 69 6e 74 2b 32 29 3b 0a 20 20 20  nstraint+2);.   
30075 20 66 6f 72 28 6a 3d 31 3b 20 6a 3c 3d 6e 43 6f   for(j=1; j<=nCo
30076 6e 73 74 72 61 69 6e 74 3b 20 6a 2b 2b 29 7b 0a  nstraint; j++){.
30077 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b        for(k=0; k
30078 3c 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6b 2b  <nConstraint; k+
30079 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
3007a 61 55 73 61 67 65 5b 6b 5d 2e 61 72 67 76 49 6e  aUsage[k].argvIn
3007b 64 65 78 3d 3d 6a 20 29 7b 0a 20 20 20 20 20 20  dex==j ){.      
3007c 20 20 20 20 69 6e 74 20 69 54 65 72 6d 20 3d 20      int iTerm = 
3007d 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6b 5d 2e 69  aConstraint[k].i
3007e 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20 20 20  TermOffset;.    
3007f 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
30080 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 57  rCode(pParse, pW
30081 43 2d 3e 61 5b 69 54 65 72 6d 5d 2e 70 45 78 70  C->a[iTerm].pExp
30082 72 2d 3e 70 52 69 67 68 74 2c 20 69 52 65 67 2b  r->pRight, iReg+
30083 6a 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  j+1);.          
30084 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
30085 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
30086 66 28 20 6b 3d 3d 6e 43 6f 6e 73 74 72 61 69 6e  f( k==nConstrain
30087 74 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d  t ) break;.    }
30088 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
30089 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
3008a 65 67 65 72 2c 20 70 56 74 61 62 49 64 78 2d 3e  eger, pVtabIdx->
3008b 69 64 78 4e 75 6d 2c 20 69 52 65 67 29 3b 0a 20  idxNum, iReg);. 
3008c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
3008d 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
3008e 65 72 2c 20 6a 2d 31 2c 20 69 52 65 67 2b 31 29  er, j-1, iReg+1)
3008f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
30090 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 46  eAddOp4(v, OP_VF
30091 69 6c 74 65 72 2c 20 69 43 75 72 2c 20 61 64 64  ilter, iCur, add
30092 72 42 72 6b 2c 20 69 52 65 67 2c 20 70 56 74 61  rBrk, iReg, pVta
30093 62 49 64 78 2d 3e 69 64 78 53 74 72 2c 0a 20 20  bIdx->idxStr,.  
30094 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30095 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e 6e 65      pVtabIdx->ne
30096 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 3f  edToFreeIdxStr ?
30097 20 50 34 5f 4d 50 52 49 4e 54 46 20 3a 20 50 34   P4_MPRINTF : P4
30098 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 70 56  _STATIC);.    pV
30099 74 61 62 49 64 78 2d 3e 6e 65 65 64 54 6f 46 72  tabIdx->needToFr
3009a 65 65 49 64 78 53 74 72 20 3d 20 30 3b 0a 20 20  eeIdxStr = 0;.  
3009b 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 43 6f    for(j=0; j<nCo
3009c 6e 73 74 72 61 69 6e 74 3b 20 6a 2b 2b 29 7b 0a  nstraint; j++){.
3009d 20 20 20 20 20 20 69 66 28 20 61 55 73 61 67 65        if( aUsage
3009e 5b 6a 5d 2e 6f 6d 69 74 20 29 7b 0a 20 20 20 20  [j].omit ){.    
3009f 20 20 20 20 69 6e 74 20 69 54 65 72 6d 20 3d 20      int iTerm = 
300a0 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6a 5d 2e 69  aConstraint[j].i
300a1 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20 20 20  TermOffset;.    
300a2 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28      disableTerm(
300a3 70 4c 65 76 65 6c 2c 20 26 70 57 43 2d 3e 61 5b  pLevel, &pWC->a[
300a4 69 54 65 72 6d 5d 29 3b 0a 20 20 20 20 20 20 7d  iTerm]);.      }
300a5 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65  .    }.    pLeve
300a6 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 56 4e 65 78 74  l->op = OP_VNext
300a7 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31  ;.    pLevel->p1
300a8 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 70 4c 65   = iCur;.    pLe
300a9 76 65 6c 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65  vel->p2 = sqlite
300aa 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
300ab 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  (v);.    sqlite3
300ac 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
300ad 28 70 50 61 72 73 65 2c 20 69 52 65 67 2c 20 6e  (pParse, iReg, n
300ae 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b 0a 20  Constraint+2);. 
300af 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
300b0 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31  chePop(pParse, 1
300b1 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  );.  }else.#endi
300b2 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
300b3 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
300b4 0a 0a 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e  ..  if( pLevel->
300b5 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
300b6 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 29 7b  HERE_ROWID_EQ ){
300b7 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 31 3a 20  .    /* Case 1: 
300b8 20 57 65 20 63 61 6e 20 64 69 72 65 63 74 6c 79   We can directly
300b9 20 72 65 66 65 72 65 6e 63 65 20 61 20 73 69 6e   reference a sin
300ba 67 6c 65 20 72 6f 77 20 75 73 69 6e 67 20 61 6e  gle row using an
300bb 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
300bc 20 65 71 75 61 6c 69 74 79 20 63 6f 6d 70 61 72   equality compar
300bd 69 73 6f 6e 20 61 67 61 69 6e 73 74 20 74 68 65  ison against the
300be 20 52 4f 57 49 44 20 66 69 65 6c 64 2e 20 20 4f   ROWID field.  O
300bf 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  r.    **        
300c0 20 20 77 65 20 72 65 66 65 72 65 6e 63 65 20 6d    we reference m
300c1 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 75 73 69  ultiple rows usi
300c2 6e 67 20 61 20 22 72 6f 77 69 64 20 49 4e 20 28  ng a "rowid IN (
300c3 2e 2e 2e 29 22 0a 20 20 20 20 2a 2a 20 20 20 20  ...)".    **    
300c4 20 20 20 20 20 20 63 6f 6e 73 74 72 75 63 74 2e        construct.
300c5 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 52 65 6c  .    */.    iRel
300c6 65 61 73 65 52 65 67 20 3d 20 73 71 6c 69 74 65  easeReg = sqlite
300c7 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
300c8 73 65 29 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d  se);.    pTerm =
300c9 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
300ca 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64  Cur, -1, notRead
300cb 79 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 2c 20  y, WO_EQ|WO_IN, 
300cc 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  0);.    assert( 
300cd 70 54 65 72 6d 21 3d 30 20 29 3b 0a 20 20 20 20  pTerm!=0 );.    
300ce 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 70  assert( pTerm->p
300cf 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20 61  Expr!=0 );.    a
300d0 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e 6c 65  ssert( pTerm->le
300d1 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29  ftCursor==iCur )
300d2 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d  ;.    assert( om
300d3 69 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20  itTable==0 );.  
300d4 20 20 69 52 6f 77 69 64 52 65 67 20 3d 20 63 6f    iRowidReg = co
300d5 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28 70  deEqualityTerm(p
300d6 50 61 72 73 65 2c 20 70 54 65 72 6d 2c 20 70 4c  Parse, pTerm, pL
300d7 65 76 65 6c 2c 20 69 52 65 6c 65 61 73 65 52 65  evel, iReleaseRe
300d8 67 29 3b 0a 20 20 20 20 61 64 64 72 4e 78 74 20  g);.    addrNxt 
300d9 3d 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78  = pLevel->addrNx
300da 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  t;.    sqlite3Vd
300db 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4d  beAddOp2(v, OP_M
300dc 75 73 74 42 65 49 6e 74 2c 20 69 52 6f 77 69 64  ustBeInt, iRowid
300dd 52 65 67 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20  Reg, addrNxt);. 
300de 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
300df 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
300e0 69 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72  ists, iCur, addr
300e1 4e 78 74 2c 20 69 52 6f 77 69 64 52 65 67 29 3b  Nxt, iRowidReg);
300e2 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
300e3 43 61 63 68 65 53 74 6f 72 65 28 70 50 61 72 73  CacheStore(pPars
300e4 65 2c 20 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f  e, iCur, -1, iRo
300e5 77 69 64 52 65 67 29 3b 0a 20 20 20 20 56 64 62  widReg);.    Vdb
300e6 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 70 6b  eComment((v, "pk
300e7 22 29 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  "));.    pLevel-
300e8 3e 6f 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20  >op = OP_Noop;. 
300e9 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65   }else if( pLeve
300ea 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
300eb 26 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41  & WHERE_ROWID_RA
300ec 4e 47 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61  NGE ){.    /* Ca
300ed 73 65 20 32 3a 20 20 57 65 20 68 61 76 65 20 61  se 2:  We have a
300ee 6e 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6d  n inequality com
300ef 70 61 72 69 73 6f 6e 20 61 67 61 69 6e 73 74 20  parison against 
300f0 74 68 65 20 52 4f 57 49 44 20 66 69 65 6c 64 2e  the ROWID field.
300f1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
300f2 74 65 73 74 4f 70 20 3d 20 4f 50 5f 4e 6f 6f 70  testOp = OP_Noop
300f3 3b 0a 20 20 20 20 69 6e 74 20 73 74 61 72 74 3b  ;.    int start;
300f4 0a 20 20 20 20 69 6e 74 20 6d 65 6d 45 6e 64 56  .    int memEndV
300f5 61 6c 75 65 20 3d 20 30 3b 0a 20 20 20 20 57 68  alue = 0;.    Wh
300f6 65 72 65 54 65 72 6d 20 2a 70 53 74 61 72 74 2c  ereTerm *pStart,
300f7 20 2a 70 45 6e 64 3b 0a 0a 20 20 20 20 61 73 73   *pEnd;..    ass
300f8 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d  ert( omitTable==
300f9 30 20 29 3b 0a 20 20 20 20 70 53 74 61 72 74 20  0 );.    pStart 
300fa 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
300fb 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61  iCur, -1, notRea
300fc 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c  dy, WO_GT|WO_GE,
300fd 20 30 29 3b 0a 20 20 20 20 70 45 6e 64 20 3d 20   0);.    pEnd = 
300fe 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43  findTerm(pWC, iC
300ff 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
30100 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 30  , WO_LT|WO_LE, 0
30101 29 3b 0a 20 20 20 20 69 66 28 20 62 52 65 76 20  );.    if( bRev 
30102 29 7b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d  ){.      pTerm =
30103 20 70 53 74 61 72 74 3b 0a 20 20 20 20 20 20 70   pStart;.      p
30104 53 74 61 72 74 20 3d 20 70 45 6e 64 3b 0a 20 20  Start = pEnd;.  
30105 20 20 20 20 70 45 6e 64 20 3d 20 70 54 65 72 6d      pEnd = pTerm
30106 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
30107 70 53 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20  pStart ){.      
30108 45 78 70 72 20 2a 70 58 3b 20 20 20 20 20 20 20  Expr *pX;       
30109 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
3010a 72 65 73 73 69 6f 6e 20 74 68 61 74 20 64 65 66  ression that def
3010b 69 6e 65 73 20 74 68 65 20 73 74 61 72 74 20 62  ines the start b
3010c 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ound */.      in
3010d 74 20 72 31 2c 20 72 54 65 6d 70 3b 20 20 20 20  t r1, rTemp;    
3010e 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 73      /* Registers
3010f 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65   for holding the
30110 20 73 74 61 72 74 20 62 6f 75 6e 64 61 72 79 20   start boundary 
30111 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  */..      /* The
30112 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74   following const
30113 61 6e 74 20 6d 61 70 73 20 54 4b 5f 78 78 20 63  ant maps TK_xx c
30114 6f 64 65 73 20 69 6e 74 6f 20 63 6f 72 72 65 73  odes into corres
30115 70 6f 6e 64 69 6e 67 20 0a 20 20 20 20 20 20 2a  ponding .      *
30116 2a 20 73 65 65 6b 20 6f 70 63 6f 64 65 73 2e 20  * seek opcodes. 
30117 20 49 74 20 64 65 70 65 6e 64 73 20 6f 6e 20 61   It depends on a
30118 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65   particular orde
30119 72 69 6e 67 20 6f 66 20 54 4b 5f 78 78 0a 20 20  ring of TK_xx.  
3011a 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e      */.      con
3011b 73 74 20 75 38 20 61 4d 6f 76 65 4f 70 5b 5d 20  st u8 aMoveOp[] 
3011c 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 2f  = {.           /
3011d 2a 20 54 4b 5f 47 54 20 2a 2f 20 20 4f 50 5f 53  * TK_GT */  OP_S
3011e 65 65 6b 47 74 2c 0a 20 20 20 20 20 20 20 20 20  eekGt,.         
3011f 20 20 2f 2a 20 54 4b 5f 4c 45 20 2a 2f 20 20 4f    /* TK_LE */  O
30120 50 5f 53 65 65 6b 4c 65 2c 0a 20 20 20 20 20 20  P_SeekLe,.      
30121 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 54 20 2a 2f       /* TK_LT */
30122 20 20 4f 50 5f 53 65 65 6b 4c 74 2c 0a 20 20 20    OP_SeekLt,.   
30123 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 47 45          /* TK_GE
30124 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 47 65 0a 20   */  OP_SeekGe. 
30125 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 61 73       };.      as
30126 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d 54 4b 5f  sert( TK_LE==TK_
30127 47 54 2b 31 20 29 3b 20 20 20 20 20 20 2f 2a 20  GT+1 );      /* 
30128 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6f 72  Make sure the or
30129 64 65 72 69 6e 67 2e 2e 20 2a 2f 0a 20 20 20 20  dering.. */.    
3012a 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3d    assert( TK_LT=
3012b 3d 54 4b 5f 47 54 2b 32 20 29 3b 20 20 20 20 20  =TK_GT+2 );     
3012c 20 2f 2a 20 20 2e 2e 2e 20 6f 66 20 74 68 65 20   /*  ... of the 
3012d 54 4b 5f 78 78 20 76 61 6c 75 65 73 2e 2e 2e 20  TK_xx values... 
3012e 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
3012f 20 54 4b 5f 47 45 3d 3d 54 4b 5f 47 54 2b 33 20   TK_GE==TK_GT+3 
30130 29 3b 20 20 20 20 20 20 2f 2a 20 20 2e 2e 2e 20  );      /*  ... 
30131 69 73 20 63 6f 72 72 65 63 63 74 2e 20 2a 2f 0a  is correcct. */.
30132 0a 20 20 20 20 20 20 70 58 20 3d 20 70 53 74 61  .      pX = pSta
30133 72 74 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  rt->pExpr;.     
30134 20 61 73 73 65 72 74 28 20 70 58 21 3d 30 20 29   assert( pX!=0 )
30135 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
30136 70 53 74 61 72 74 2d 3e 6c 65 66 74 43 75 72 73  pStart->leftCurs
30137 6f 72 3d 3d 69 43 75 72 20 29 3b 0a 20 20 20 20  or==iCur );.    
30138 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78    r1 = sqlite3Ex
30139 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
3013a 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 26  e, pX->pRight, &
3013b 72 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71  rTemp);.      sq
3013c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
3013d 76 2c 20 61 4d 6f 76 65 4f 70 5b 70 58 2d 3e 6f  v, aMoveOp[pX->o
3013e 70 2d 54 4b 5f 47 54 5d 2c 20 69 43 75 72 2c 20  p-TK_GT], iCur, 
3013f 61 64 64 72 42 72 6b 2c 20 72 31 29 3b 0a 20 20  addrBrk, r1);.  
30140 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
30141 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20 20  (v, "pk"));.    
30142 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
30143 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  heAffinityChange
30144 28 70 50 61 72 73 65 2c 20 72 31 2c 20 31 29 3b  (pParse, r1, 1);
30145 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
30146 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
30147 72 73 65 2c 20 72 54 65 6d 70 29 3b 0a 20 20 20  rse, rTemp);.   
30148 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70     disableTerm(p
30149 4c 65 76 65 6c 2c 20 70 53 74 61 72 74 29 3b 0a  Level, pStart);.
3014a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3014b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
3014c 70 32 28 76 2c 20 62 52 65 76 20 3f 20 4f 50 5f  p2(v, bRev ? OP_
3014d 4c 61 73 74 20 3a 20 4f 50 5f 52 65 77 69 6e 64  Last : OP_Rewind
3014e 2c 20 69 43 75 72 2c 20 61 64 64 72 42 72 6b 29  , iCur, addrBrk)
3014f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
30150 70 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 45 78  pEnd ){.      Ex
30151 70 72 20 2a 70 58 3b 0a 20 20 20 20 20 20 70 58  pr *pX;.      pX
30152 20 3d 20 70 45 6e 64 2d 3e 70 45 78 70 72 3b 0a   = pEnd->pExpr;.
30153 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 58        assert( pX
30154 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  !=0 );.      ass
30155 65 72 74 28 20 70 45 6e 64 2d 3e 6c 65 66 74 43  ert( pEnd->leftC
30156 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b 0a 20  ursor==iCur );. 
30157 20 20 20 20 20 6d 65 6d 45 6e 64 56 61 6c 75 65       memEndValue
30158 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
30159 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  m;.      sqlite3
3015a 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
3015b 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 6d 65 6d   pX->pRight, mem
3015c 45 6e 64 56 61 6c 75 65 29 3b 0a 20 20 20 20 20  EndValue);.     
3015d 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f   if( pX->op==TK_
3015e 4c 54 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b  LT || pX->op==TK
3015f 5f 47 54 20 29 7b 0a 20 20 20 20 20 20 20 20 74  _GT ){.        t
30160 65 73 74 4f 70 20 3d 20 62 52 65 76 20 3f 20 4f  estOp = bRev ? O
30161 50 5f 4c 65 20 3a 20 4f 50 5f 47 65 3b 0a 20 20  P_Le : OP_Ge;.  
30162 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
30163 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65 76     testOp = bRev
30164 20 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50 5f 47 74   ? OP_Lt : OP_Gt
30165 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
30166 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76  disableTerm(pLev
30167 65 6c 2c 20 70 45 6e 64 29 3b 0a 20 20 20 20 7d  el, pEnd);.    }
30168 0a 20 20 20 20 73 74 61 72 74 20 3d 20 73 71 6c  .    start = sql
30169 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
3016a 64 64 72 28 76 29 3b 0a 20 20 20 20 70 4c 65 76  ddr(v);.    pLev
3016b 65 6c 2d 3e 6f 70 20 3d 20 62 52 65 76 20 3f 20  el->op = bRev ? 
3016c 4f 50 5f 50 72 65 76 20 3a 20 4f 50 5f 4e 65 78  OP_Prev : OP_Nex
3016d 74 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  t;.    pLevel->p
3016e 31 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 70 4c  1 = iCur;.    pL
3016f 65 76 65 6c 2d 3e 70 32 20 3d 20 73 74 61 72 74  evel->p2 = start
30170 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 35  ;.    pLevel->p5
30171 20 3d 20 28 70 53 74 61 72 74 3d 3d 30 20 26 26   = (pStart==0 &&
30172 20 70 45 6e 64 3d 3d 30 29 20 3f 31 3a 30 3b 0a   pEnd==0) ?1:0;.
30173 20 20 20 20 69 66 28 20 74 65 73 74 4f 70 21 3d      if( testOp!=
30174 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20  OP_Noop ){.     
30175 20 69 52 6f 77 69 64 52 65 67 20 3d 20 69 52 65   iRowidReg = iRe
30176 6c 65 61 73 65 52 65 67 20 3d 20 73 71 6c 69 74  leaseReg = sqlit
30177 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
30178 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  rse);.      sqli
30179 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
3017a 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c   OP_Rowid, iCur,
3017b 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20   iRowidReg);.   
3017c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
3017d 63 68 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c  cheStore(pParse,
3017e 20 69 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69   iCur, -1, iRowi
3017f 64 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c  dReg);.      sql
30180 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
30181 2c 20 74 65 73 74 4f 70 2c 20 6d 65 6d 45 6e 64  , testOp, memEnd
30182 56 61 6c 75 65 2c 20 61 64 64 72 42 72 6b 2c 20  Value, addrBrk, 
30183 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20  iRowidReg);.    
30184 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
30185 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f  ngeP5(v, SQLITE_
30186 41 46 46 5f 4e 55 4d 45 52 49 43 20 7c 20 53 51  AFF_NUMERIC | SQ
30187 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29  LITE_JUMPIFNULL)
30188 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
30189 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
3018a 2e 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52  .wsFlags & (WHER
3018b 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57  E_COLUMN_RANGE|W
3018c 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 29 20  HERE_COLUMN_EQ) 
3018d 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20 33  ){.    /* Case 3
3018e 3a 20 41 20 73 63 61 6e 20 75 73 69 6e 67 20 61  : A scan using a
3018f 6e 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a 0a  n index..    **.
30190 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 54      **         T
30191 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
30192 6d 61 79 20 63 6f 6e 74 61 69 6e 20 7a 65 72 6f  may contain zero
30193 20 6f 72 20 6d 6f 72 65 20 65 71 75 61 6c 69 74   or more equalit
30194 79 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  y .    **       
30195 20 20 74 65 72 6d 73 20 28 22 3d 3d 22 20 6f 72    terms ("==" or
30196 20 22 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 29   "IN" operators)
30197 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74   that refer to t
30198 68 65 20 4e 0a 20 20 20 20 2a 2a 20 20 20 20 20  he N.    **     
30199 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f      left-most co
3019a 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 69 6e 64  lumns of the ind
3019b 65 78 2e 20 49 74 20 6d 61 79 20 61 6c 73 6f 20  ex. It may also 
3019c 63 6f 6e 74 61 69 6e 0a 20 20 20 20 2a 2a 20 20  contain.    **  
3019d 20 20 20 20 20 20 20 69 6e 65 71 75 61 6c 69 74         inequalit
3019e 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 28 3e  y constraints (>
3019f 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c 3d 29 20 6f  , <, >= or <=) o
301a0 6e 20 74 68 65 20 69 6e 64 65 78 65 64 0a 20 20  n the indexed.  
301a1 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6c    **         col
301a2 75 6d 6e 20 74 68 61 74 20 69 6d 6d 65 64 69 61  umn that immedia
301a3 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65  tely follows the
301a4 20 4e 20 65 71 75 61 6c 69 74 69 65 73 2e 20 4f   N equalities. O
301a5 6e 6c 79 20 0a 20 20 20 20 2a 2a 20 20 20 20 20  nly .    **     
301a6 20 20 20 20 74 68 65 20 72 69 67 68 74 2d 6d 6f      the right-mo
301a7 73 74 20 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65  st column can be
301a8 20 61 6e 20 69 6e 65 71 75 61 6c 69 74 79 20 2d   an inequality -
301a9 20 74 68 65 20 72 65 73 74 20 6d 75 73 74 0a 20   the rest must. 
301aa 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 75 73     **         us
301ab 65 20 74 68 65 20 22 3d 3d 22 20 61 6e 64 20 22  e the "==" and "
301ac 49 4e 22 20 6f 70 65 72 61 74 6f 72 73 2e 20 46  IN" operators. F
301ad 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74  or example, if t
301ae 68 65 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  he .    **      
301af 20 20 20 69 6e 64 65 78 20 69 73 20 6f 6e 20 28     index is on (
301b0 78 2c 79 2c 7a 29 2c 20 74 68 65 6e 20 74 68 65  x,y,z), then the
301b1 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6c 61 75 73   following claus
301b2 65 73 20 61 72 65 20 61 6c 6c 20 0a 20 20 20 20  es are all .    
301b3 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 74 69 6d  **         optim
301b4 69 7a 65 64 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  ized:.    **.   
301b5 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78   **            x
301b6 3d 35 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  =5.    **       
301b7 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3d 31       x=5 AND y=1
301b8 30 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  0.    **        
301b9 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3c 31 30      x=5 AND y<10
301ba 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
301bb 20 20 20 78 3d 35 20 41 4e 44 20 79 3e 35 20 41     x=5 AND y>5 A
301bc 4e 44 20 79 3c 31 30 0a 20 20 20 20 2a 2a 20 20  ND y<10.    **  
301bd 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e            x=5 AN
301be 44 20 79 3d 35 20 41 4e 44 20 7a 3c 3d 31 30 0a  D y=5 AND z<=10.
301bf 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
301c0 20 20 20 20 20 20 54 68 65 20 7a 3c 31 30 20 74        The z<10 t
301c1 65 72 6d 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  erm of the follo
301c2 77 69 6e 67 20 63 61 6e 6e 6f 74 20 62 65 20 75  wing cannot be u
301c3 73 65 64 2c 20 6f 6e 6c 79 0a 20 20 20 20 2a 2a  sed, only.    **
301c4 20 20 20 20 20 20 20 20 20 74 68 65 20 78 3d 35           the x=5
301c5 20 74 65 72 6d 3a 0a 20 20 20 20 2a 2a 0a 20 20   term:.    **.  
301c6 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
301c7 78 3d 35 20 41 4e 44 20 7a 3c 31 30 0a 20 20 20  x=5 AND z<10.   
301c8 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
301c9 20 20 20 4e 20 6d 61 79 20 62 65 20 7a 65 72 6f     N may be zero
301ca 20 69 66 20 74 68 65 72 65 20 61 72 65 20 69 6e   if there are in
301cb 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
301cc 69 6e 74 73 2e 0a 20 20 20 20 2a 2a 20 20 20 20  ints..    **    
301cd 20 20 20 20 20 49 66 20 74 68 65 72 65 20 61 72       If there ar
301ce 65 20 6e 6f 20 69 6e 65 71 75 61 6c 69 74 79 20  e no inequality 
301cf 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65  constraints, the
301d0 6e 20 4e 20 69 73 20 61 74 0a 20 20 20 20 2a 2a  n N is at.    **
301d1 20 20 20 20 20 20 20 20 20 6c 65 61 73 74 20 6f           least o
301d2 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ne..    **.    *
301d3 2a 20 20 20 20 20 20 20 20 20 54 68 69 73 20 63  *         This c
301d4 61 73 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64  ase is also used
301d5 20 77 68 65 6e 20 74 68 65 72 65 20 61 72 65 20   when there are 
301d6 6e 6f 20 57 48 45 52 45 20 63 6c 61 75 73 65 0a  no WHERE clause.
301d7 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63      **         c
301d8 6f 6e 73 74 72 61 69 6e 74 73 20 62 75 74 20 61  onstraints but a
301d9 6e 20 69 6e 64 65 78 20 69 73 20 73 65 6c 65 63  n index is selec
301da 74 65 64 20 61 6e 79 77 61 79 2c 20 69 6e 20 6f  ted anyway, in o
301db 72 64 65 72 0a 20 20 20 20 2a 2a 20 20 20 20 20  rder.    **     
301dc 20 20 20 20 74 6f 20 66 6f 72 63 65 20 74 68 65      to force the
301dd 20 6f 75 74 70 75 74 20 6f 72 64 65 72 20 74 6f   output order to
301de 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61 6e 20 4f   conform to an O
301df 52 44 45 52 20 42 59 2e 0a 20 20 20 20 2a 2f 20  RDER BY..    */ 
301e0 20 0a 20 20 20 20 69 6e 74 20 61 53 74 61 72 74   .    int aStart
301e1 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 30  Op[] = {.      0
301e2 2c 0a 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20  ,.      0,.     
301e3 20 4f 50 5f 52 65 77 69 6e 64 2c 20 20 20 20 20   OP_Rewind,     
301e4 20 20 20 20 20 20 2f 2a 20 32 3a 20 28 21 73 74        /* 2: (!st
301e5 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20  art_constraints 
301e6 26 26 20 73 74 61 72 74 45 71 20 26 26 20 20 21  && startEq &&  !
301e7 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f  bRev) */.      O
301e8 50 5f 4c 61 73 74 2c 20 20 20 20 20 20 20 20 20  P_Last,         
301e9 20 20 20 20 2f 2a 20 33 3a 20 28 21 73 74 61 72      /* 3: (!star
301ea 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 26 26  t_constraints &&
301eb 20 73 74 61 72 74 45 71 20 26 26 20 20 20 62 52   startEq &&   bR
301ec 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f  ev) */.      OP_
301ed 53 65 65 6b 47 74 2c 20 20 20 20 20 20 20 20 20  SeekGt,         
301ee 20 20 2f 2a 20 34 3a 20 28 73 74 61 72 74 5f 63    /* 4: (start_c
301ef 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 21  onstraints  && !
301f0 73 74 61 72 74 45 71 20 26 26 20 21 62 52 65 76  startEq && !bRev
301f1 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65  ) */.      OP_Se
301f2 65 6b 4c 74 2c 20 20 20 20 20 20 20 20 20 20 20  ekLt,           
301f3 2f 2a 20 35 3a 20 28 73 74 61 72 74 5f 63 6f 6e  /* 5: (start_con
301f4 73 74 72 61 69 6e 74 73 20 20 26 26 20 21 73 74  straints  && !st
301f5 61 72 74 45 71 20 26 26 20 20 62 52 65 76 29 20  artEq &&  bRev) 
301f6 2a 2f 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 6b  */.      OP_Seek
301f7 47 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Ge,           /*
301f8 20 36 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 74   6: (start_const
301f9 72 61 69 6e 74 73 20 20 26 26 20 20 73 74 61 72  raints  &&  star
301fa 74 45 71 20 26 26 20 21 62 52 65 76 29 20 2a 2f  tEq && !bRev) */
301fb 0a 20 20 20 20 20 20 4f 50 5f 53 65 65 6b 4c 65  .      OP_SeekLe
301fc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 37              /* 7
301fd 3a 20 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 61  : (start_constra
301fe 69 6e 74 73 20 20 26 26 20 20 73 74 61 72 74 45  ints  &&  startE
301ff 71 20 26 26 20 20 62 52 65 76 29 20 2a 2f 0a 20  q &&  bRev) */. 
30200 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 61 45     };.    int aE
30201 6e 64 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  ndOp[] = {.     
30202 20 4f 50 5f 4e 6f 6f 70 2c 20 20 20 20 20 20 20   OP_Noop,       
30203 20 20 20 20 20 20 2f 2a 20 30 3a 20 28 21 65 6e        /* 0: (!en
30204 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73 29 20 2a  d_constraints) *
30205 2f 0a 20 20 20 20 20 20 4f 50 5f 49 64 78 47 45  /.      OP_IdxGE
30206 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
30207 31 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69  1: (end_constrai
30208 6e 74 73 20 26 26 20 21 62 52 65 76 29 20 2a 2f  nts && !bRev) */
30209 0a 20 20 20 20 20 20 4f 50 5f 49 64 78 4c 54 20  .      OP_IdxLT 
3020a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32              /* 2
3020b 3a 20 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e  : (end_constrain
3020c 74 73 20 26 26 20 62 52 65 76 29 20 2a 2f 0a 20  ts && bRev) */. 
3020d 20 20 20 7d 3b 0a 20 20 20 20 69 6e 74 20 6e 45     };.    int nE
3020e 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  q = pLevel->plan
3020f 2e 6e 45 71 3b 0a 20 20 20 20 69 6e 74 20 69 73  .nEq;.    int is
30210 4d 69 6e 51 75 65 72 79 20 3d 20 30 3b 20 20 20  MinQuery = 0;   
30211 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69         /* If thi
30212 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 65  s is an optimize
30213 64 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 2e  d SELECT min(x).
30214 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67  . */.    int reg
30215 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  Base;           
30216 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 65        /* Base re
30217 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63  gister holding c
30218 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c 75 65 73  onstraint values
30219 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 31 3b 20   */.    int r1; 
3021a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3021b 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 72 65 67       /* Temp reg
3021c 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 57 68 65  ister */.    Whe
3021d 72 65 54 65 72 6d 20 2a 70 52 61 6e 67 65 53 74  reTerm *pRangeSt
3021e 61 72 74 20 3d 20 30 3b 20 20 2f 2a 20 49 6e 65  art = 0;  /* Ine
3021f 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
30220 6e 74 20 61 74 20 72 61 6e 67 65 20 73 74 61 72  nt at range star
30221 74 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 54 65  t */.    WhereTe
30222 72 6d 20 2a 70 52 61 6e 67 65 45 6e 64 20 3d 20  rm *pRangeEnd = 
30223 30 3b 20 20 20 20 2f 2a 20 49 6e 65 71 75 61 6c  0;    /* Inequal
30224 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 61  ity constraint a
30225 74 20 72 61 6e 67 65 20 65 6e 64 20 2a 2f 0a 20  t range end */. 
30226 20 20 20 69 6e 74 20 73 74 61 72 74 45 71 3b 20     int startEq; 
30227 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30228 2f 2a 20 54 72 75 65 20 69 66 20 72 61 6e 67 65  /* True if range
30229 20 73 74 61 72 74 20 75 73 65 73 20 3d 3d 2c 20   start uses ==, 
3022a 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20 20  >= or <= */.    
3022b 69 6e 74 20 65 6e 64 45 71 3b 20 20 20 20 20 20  int endEq;      
3022c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3022d 54 72 75 65 20 69 66 20 72 61 6e 67 65 20 65 6e  True if range en
3022e 64 20 75 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72  d uses ==, >= or
3022f 20 3c 3d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73   <= */.    int s
30230 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73  tart_constraints
30231 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74  ;       /* Start
30232 20 6f 66 20 72 61 6e 67 65 20 69 73 20 63 6f 6e   of range is con
30233 73 74 72 61 69 6e 65 64 20 2a 2f 0a 20 20 20 20  strained */.    
30234 69 6e 74 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  int nConstraint;
30235 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30236 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6e 73 74 72  Number of constr
30237 61 69 6e 74 20 74 65 72 6d 73 20 2a 2f 0a 20 20  aint terms */.  
30238 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
30239 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
3023a 64 65 78 20 77 65 20 77 69 6c 6c 20 62 65 20 75  dex we will be u
3023b 73 69 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20  sing */.    int 
3023c 69 49 64 78 43 75 72 3b 20 20 20 20 20 20 20 20  iIdxCur;        
3023d 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75 72   /* The VDBE cur
3023e 73 6f 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65  sor for the inde
3023f 78 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 78  x */.    int nEx
30240 74 72 61 52 65 67 20 3d 20 30 3b 20 20 20 2f 2a  traReg = 0;   /*
30241 20 4e 75 6d 62 65 72 20 6f 66 20 65 78 74 72 61   Number of extra
30242 20 72 65 67 69 73 74 65 72 73 20 6e 65 65 64 65   registers neede
30243 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 70 3b  d */.    int op;
30244 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
30245 20 49 6e 73 74 72 75 63 74 69 6f 6e 20 6f 70 63   Instruction opc
30246 6f 64 65 20 2a 2f 0a 20 20 20 20 63 68 61 72 20  ode */.    char 
30247 2a 7a 41 66 66 3b 0a 0a 20 20 20 20 70 49 64 78  *zAff;..    pIdx
30248 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
30249 75 2e 70 49 64 78 3b 0a 20 20 20 20 69 49 64 78  u.pIdx;.    iIdx
3024a 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49  Cur = pLevel->iI
3024b 64 78 43 75 72 3b 0a 20 20 20 20 6b 20 3d 20 70  dxCur;.    k = p
3024c 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45  Idx->aiColumn[nE
3024d 71 5d 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d  q];     /* Colum
3024e 6e 20 66 6f 72 20 69 6e 65 71 75 61 6c 69 74 79  n for inequality
3024f 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a   constraints */.
30250 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
30251 6c 6f 6f 70 20 73 61 74 69 73 66 69 65 73 20 61  loop satisfies a
30252 20 73 6f 72 74 20 6f 72 64 65 72 20 28 70 4f 72   sort order (pOr
30253 64 65 72 42 79 29 20 72 65 71 75 65 73 74 20 74  derBy) request t
30254 68 61 74 20 0a 20 20 20 20 2a 2a 20 77 61 73 20  hat .    ** was 
30255 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66  passed to this f
30256 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6d 70 6c 65  unction to imple
30257 6d 65 6e 74 20 61 20 22 53 45 4c 45 43 54 20 6d  ment a "SELECT m
30258 69 6e 28 78 29 20 2e 2e 2e 22 20 0a 20 20 20 20  in(x) ..." .    
30259 2a 2a 20 71 75 65 72 79 2c 20 74 68 65 6e 20 74  ** query, then t
3025a 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6f  he caller will o
3025b 6e 6c 79 20 61 6c 6c 6f 77 20 74 68 65 20 6c 6f  nly allow the lo
3025c 6f 70 20 74 6f 20 72 75 6e 20 66 6f 72 0a 20 20  op to run for.  
3025d 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 69 74    ** a single it
3025e 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20 6d 65  eration. This me
3025f 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 69 72  ans that the fir
30260 73 74 20 72 6f 77 20 72 65 74 75 72 6e 65 64 0a  st row returned.
30261 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f      ** should no
30262 74 20 68 61 76 65 20 61 20 4e 55 4c 4c 20 76 61  t have a NULL va
30263 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 27 78  lue stored in 'x
30264 27 2e 20 49 66 20 63 6f 6c 75 6d 6e 20 27 78 27  '. If column 'x'
30265 20 69 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 66   is.    ** the f
30266 69 72 73 74 20 6f 6e 65 20 61 66 74 65 72 20 74  irst one after t
30267 68 65 20 6e 45 71 20 65 71 75 61 6c 69 74 79 20  he nEq equality 
30268 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e 20 74  constraints in t
30269 68 65 20 69 6e 64 65 78 2c 0a 20 20 20 20 2a 2a  he index,.    **
3026a 20 74 68 69 73 20 72 65 71 75 69 72 65 73 20 73   this requires s
3026b 6f 6d 65 20 73 70 65 63 69 61 6c 20 68 61 6e 64  ome special hand
3026c 6c 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ling..    */.   
3026d 20 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73   if( (wctrlFlags
3026e 26 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d  &WHERE_ORDERBY_M
3026f 49 4e 29 21 3d 30 0a 20 20 20 20 20 26 26 20 28  IN)!=0.     && (
30270 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
30271 6c 61 67 73 26 57 48 45 52 45 5f 4f 52 44 45 52  lags&WHERE_ORDER
30272 42 59 29 0a 20 20 20 20 20 26 26 20 28 70 49 64  BY).     && (pId
30273 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 6e 45 71 29 0a  x->nColumn>nEq).
30274 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20      ){.      /* 
30275 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79  assert( pOrderBy
30276 2d 3e 6e 45 78 70 72 3d 3d 31 20 29 3b 20 2a 2f  ->nExpr==1 ); */
30277 0a 20 20 20 20 20 20 2f 2a 20 61 73 73 65 72 74  .      /* assert
30278 28 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 30 5d  ( pOrderBy->a[0]
30279 2e 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3d  .pExpr->iColumn=
3027a 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  =pIdx->aiColumn[
3027b 6e 45 71 5d 20 29 3b 20 2a 2f 0a 20 20 20 20 20  nEq] ); */.     
3027c 20 69 73 4d 69 6e 51 75 65 72 79 20 3d 20 31 3b   isMinQuery = 1;
3027d 0a 20 20 20 20 20 20 6e 45 78 74 72 61 52 65 67  .      nExtraReg
3027e 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 1;.    }..   
3027f 20 2f 2a 20 46 69 6e 64 20 61 6e 79 20 69 6e 65   /* Find any ine
30280 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
30281 6e 74 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65  nt terms for the
30282 20 73 74 61 72 74 20 61 6e 64 20 65 6e 64 20 0a   start and end .
30283 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 61      ** of the ra
30284 6e 67 65 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20  nge. .    */.   
30285 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61   if( pLevel->pla
30286 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
30287 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29 7b 0a 20  E_TOP_LIMIT ){. 
30288 20 20 20 20 20 70 52 61 6e 67 65 45 6e 64 20 3d       pRangeEnd =
30289 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
3028a 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79  Cur, k, notReady
3028b 2c 20 28 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 29 2c  , (WO_LT|WO_LE),
3028c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20 6e 45   pIdx);.      nE
3028d 78 74 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20  xtraReg = 1;.   
3028e 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65   }.    if( pLeve
3028f 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
30290 26 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49  & WHERE_BTM_LIMI
30291 54 20 29 7b 0a 20 20 20 20 20 20 70 52 61 6e 67  T ){.      pRang
30292 65 53 74 61 72 74 20 3d 20 66 69 6e 64 54 65 72  eStart = findTer
30293 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20  m(pWC, iCur, k, 
30294 6e 6f 74 52 65 61 64 79 2c 20 28 57 4f 5f 47 54  notReady, (WO_GT
30295 7c 57 4f 5f 47 45 29 2c 20 70 49 64 78 29 3b 0a  |WO_GE), pIdx);.
30296 20 20 20 20 20 20 6e 45 78 74 72 61 52 65 67 20        nExtraReg 
30297 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 1;.    }..    
30298 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
30299 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6c 6c   to evaluate all
3029a 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d   constraint term
3029b 73 20 75 73 69 6e 67 20 3d 3d 20 6f 72 20 49 4e  s using == or IN
3029c 0a 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72  .    ** and stor
3029d 65 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20  e the values of 
3029e 74 68 6f 73 65 20 74 65 72 6d 73 20 69 6e 20 61  those terms in a
3029f 6e 20 61 72 72 61 79 20 6f 66 20 72 65 67 69 73  n array of regis
302a0 74 65 72 73 0a 20 20 20 20 2a 2a 20 73 74 61 72  ters.    ** star
302a1 74 69 6e 67 20 61 74 20 72 65 67 42 61 73 65 2e  ting at regBase.
302a2 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 42  .    */.    regB
302a3 61 73 65 20 3d 20 63 6f 64 65 41 6c 6c 45 71 75  ase = codeAllEqu
302a4 61 6c 69 74 79 54 65 72 6d 73 28 0a 20 20 20 20  alityTerms(.    
302a5 20 20 20 20 70 50 61 72 73 65 2c 20 70 4c 65 76      pParse, pLev
302a6 65 6c 2c 20 70 57 43 2c 20 6e 6f 74 52 65 61 64  el, pWC, notRead
302a7 79 2c 20 6e 45 78 74 72 61 52 65 67 2c 20 26 7a  y, nExtraReg, &z
302a8 41 66 66 0a 20 20 20 20 29 3b 0a 20 20 20 20 61  Aff.    );.    a
302a9 64 64 72 4e 78 74 20 3d 20 70 4c 65 76 65 6c 2d  ddrNxt = pLevel-
302aa 3e 61 64 64 72 4e 78 74 3b 0a 0a 20 20 20 20 2f  >addrNxt;..    /
302ab 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e  * If we are doin
302ac 67 20 61 20 72 65 76 65 72 73 65 20 6f 72 64 65  g a reverse orde
302ad 72 20 73 63 61 6e 20 6f 6e 20 61 6e 20 61 73 63  r scan on an asc
302ae 65 6e 64 69 6e 67 20 69 6e 64 65 78 2c 20 6f 72  ending index, or
302af 0a 20 20 20 20 2a 2a 20 61 20 66 6f 72 77 61 72  .    ** a forwar
302b0 64 20 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e 20  d order scan on 
302b1 61 20 64 65 73 63 65 6e 64 69 6e 67 20 69 6e 64  a descending ind
302b2 65 78 2c 20 69 6e 74 65 72 63 68 61 6e 67 65 20  ex, interchange 
302b3 74 68 65 20 0a 20 20 20 20 2a 2a 20 73 74 61 72  the .    ** star
302b4 74 20 61 6e 64 20 65 6e 64 20 74 65 72 6d 73 20  t and end terms 
302b5 28 70 52 61 6e 67 65 53 74 61 72 74 20 61 6e 64  (pRangeStart and
302b6 20 70 52 61 6e 67 65 45 6e 64 29 2e 0a 20 20 20   pRangeEnd)..   
302b7 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 52 65 76   */.    if( bRev
302b8 3d 3d 28 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72  ==(pIdx->aSortOr
302b9 64 65 72 5b 6e 45 71 5d 3d 3d 53 51 4c 49 54 45  der[nEq]==SQLITE
302ba 5f 53 4f 5f 41 53 43 29 20 29 7b 0a 20 20 20 20  _SO_ASC) ){.    
302bb 20 20 53 57 41 50 28 57 68 65 72 65 54 65 72 6d    SWAP(WhereTerm
302bc 20 2a 2c 20 70 52 61 6e 67 65 45 6e 64 2c 20 70   *, pRangeEnd, p
302bd 52 61 6e 67 65 53 74 61 72 74 29 3b 0a 20 20 20  RangeStart);.   
302be 20 7d 0a 0a 20 20 20 20 74 65 73 74 63 61 73 65   }..    testcase
302bf 28 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26  ( pRangeStart &&
302c0 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f   pRangeStart->eO
302c1 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20  perator & WO_LE 
302c2 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
302c3 20 70 52 61 6e 67 65 53 74 61 72 74 20 26 26 20   pRangeStart && 
302c4 70 52 61 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70  pRangeStart->eOp
302c5 65 72 61 74 6f 72 20 26 20 57 4f 5f 47 45 20 29  erator & WO_GE )
302c6 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
302c7 70 52 61 6e 67 65 45 6e 64 20 26 26 20 70 52 61  pRangeEnd && pRa
302c8 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f  ngeEnd->eOperato
302c9 72 20 26 20 57 4f 5f 4c 45 20 29 3b 0a 20 20 20  r & WO_LE );.   
302ca 20 74 65 73 74 63 61 73 65 28 20 70 52 61 6e 67   testcase( pRang
302cb 65 45 6e 64 20 26 26 20 70 52 61 6e 67 65 45 6e  eEnd && pRangeEn
302cc 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57  d->eOperator & W
302cd 4f 5f 47 45 20 29 3b 0a 20 20 20 20 73 74 61 72  O_GE );.    star
302ce 74 45 71 20 3d 20 21 70 52 61 6e 67 65 53 74 61  tEq = !pRangeSta
302cf 72 74 20 7c 7c 20 70 52 61 6e 67 65 53 74 61 72  rt || pRangeStar
302d0 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  t->eOperator & (
302d1 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a 20 20  WO_LE|WO_GE);.  
302d2 20 20 65 6e 64 45 71 20 3d 20 20 20 21 70 52 61    endEq =   !pRa
302d3 6e 67 65 45 6e 64 20 7c 7c 20 70 52 61 6e 67 65  ngeEnd || pRange
302d4 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26  End->eOperator &
302d5 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b 0a   (WO_LE|WO_GE);.
302d6 20 20 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72      start_constr
302d7 61 69 6e 74 73 20 3d 20 70 52 61 6e 67 65 53 74  aints = pRangeSt
302d8 61 72 74 20 7c 7c 20 6e 45 71 3e 30 3b 0a 0a 20  art || nEq>0;.. 
302d9 20 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 69     /* Seek the i
302da 6e 64 65 78 20 63 75 72 73 6f 72 20 74 6f 20 74  ndex cursor to t
302db 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
302dc 72 61 6e 67 65 2e 20 2a 2f 0a 20 20 20 20 6e 43  range. */.    nC
302dd 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b  onstraint = nEq;
302de 0a 20 20 20 20 69 66 28 20 70 52 61 6e 67 65 53  .    if( pRangeS
302df 74 61 72 74 20 29 7b 0a 20 20 20 20 20 20 45 78  tart ){.      Ex
302e0 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 52 61  pr *pRight = pRa
302e1 6e 67 65 53 74 61 72 74 2d 3e 70 45 78 70 72 2d  ngeStart->pExpr-
302e2 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 73  >pRight;.      s
302e3 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
302e4 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 72  Parse, pRight, r
302e5 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20  egBase+nEq);.   
302e6 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
302e7 64 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76 2c 20  deIsNullJump(v, 
302e8 70 52 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b  pRight, regBase+
302e9 6e 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20  nEq, addrNxt);. 
302ea 20 20 20 20 20 69 66 28 20 7a 41 66 66 20 29 7b       if( zAff ){
302eb 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
302ec 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e  ite3CompareAffin
302ed 69 74 79 28 70 52 69 67 68 74 2c 20 7a 41 66 66  ity(pRight, zAff
302ee 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 29 3d 3d  [nConstraint])==
302ef 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29  SQLITE_AFF_NONE)
302f0 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  {.          /* S
302f1 69 6e 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69  ince the compari
302f2 73 6f 6e 20 69 73 20 74 6f 20 62 65 20 70 65 72  son is to be per
302f3 66 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f 20 63  formed with no c
302f4 6f 6e 76 65 72 73 69 6f 6e 73 0a 20 20 20 20 20  onversions.     
302f5 20 20 20 20 20 2a 2a 20 61 70 70 6c 69 65 64 20       ** applied 
302f6 74 6f 20 74 68 65 20 6f 70 65 72 61 6e 64 73 2c  to the operands,
302f7 20 73 65 74 20 74 68 65 20 61 66 66 69 6e 69 74   set the affinit
302f8 79 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70 52  y to apply to pR
302f9 69 67 68 74 20 74 6f 20 0a 20 20 20 20 20 20 20  ight to .       
302fa 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46     ** SQLITE_AFF
302fb 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20 20 20  _NONE.  */.     
302fc 20 20 20 20 20 7a 41 66 66 5b 6e 43 6f 6e 73 74       zAff[nConst
302fd 72 61 69 6e 74 5d 20 3d 20 53 51 4c 49 54 45 5f  raint] = SQLITE_
302fe 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20  AFF_NONE;.      
302ff 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
30300 73 71 6c 69 74 65 33 45 78 70 72 4e 65 65 64 73  sqlite3ExprNeeds
30301 4e 6f 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65  NoAffinityChange
30302 28 70 52 69 67 68 74 2c 20 7a 41 66 66 5b 6e 43  (pRight, zAff[nC
30303 6f 6e 73 74 72 61 69 6e 74 5d 29 20 29 7b 0a 20  onstraint]) ){. 
30304 20 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6e 43           zAff[nC
30305 6f 6e 73 74 72 61 69 6e 74 5d 20 3d 20 53 51 4c  onstraint] = SQL
30306 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20  ITE_AFF_NONE;.  
30307 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20        }.      } 
30308 20 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74 72 61   .      nConstra
30309 69 6e 74 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65  int++;.    }else
3030a 20 69 66 28 20 69 73 4d 69 6e 51 75 65 72 79 20   if( isMinQuery 
3030b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
3030c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
3030d 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 42 61 73  _Null, 0, regBas
3030e 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20 6e 43  e+nEq);.      nC
3030f 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20  onstraint++;.   
30310 20 20 20 73 74 61 72 74 45 71 20 3d 20 30 3b 0a     startEq = 0;.
30311 20 20 20 20 20 20 73 74 61 72 74 5f 63 6f 6e 73        start_cons
30312 74 72 61 69 6e 74 73 20 3d 20 31 3b 0a 20 20 20  traints = 1;.   
30313 20 7d 0a 20 20 20 20 63 6f 64 65 41 70 70 6c 79   }.    codeApply
30314 41 66 66 69 6e 69 74 79 28 70 50 61 72 73 65 2c  Affinity(pParse,
30315 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6e 73 74   regBase, nConst
30316 72 61 69 6e 74 2c 20 7a 41 66 66 29 3b 0a 20 20  raint, zAff);.  
30317 20 20 6f 70 20 3d 20 61 53 74 61 72 74 4f 70 5b    op = aStartOp[
30318 28 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e  (start_constrain
30319 74 73 3c 3c 32 29 20 2b 20 28 73 74 61 72 74 45  ts<<2) + (startE
3031a 71 3c 3c 31 29 20 2b 20 62 52 65 76 5d 3b 0a 20  q<<1) + bRev];. 
3031b 20 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 30     assert( op!=0
3031c 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
3031d 28 20 6f 70 3d 3d 4f 50 5f 52 65 77 69 6e 64 20  ( op==OP_Rewind 
3031e 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
3031f 20 6f 70 3d 3d 4f 50 5f 4c 61 73 74 20 29 3b 0a   op==OP_Last );.
30320 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
30321 3d 3d 4f 50 5f 53 65 65 6b 47 74 20 29 3b 0a 20  ==OP_SeekGt );. 
30322 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
30323 3d 4f 50 5f 53 65 65 6b 47 65 20 29 3b 0a 20 20  =OP_SeekGe );.  
30324 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
30325 4f 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20  OP_SeekLe );.   
30326 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f   testcase( op==O
30327 50 5f 53 65 65 6b 4c 74 20 29 3b 0a 20 20 20 20  P_SeekLt );.    
30328 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
30329 34 49 6e 74 28 76 2c 20 6f 70 2c 20 69 49 64 78  4Int(v, op, iIdx
3032a 43 75 72 2c 20 61 64 64 72 4e 78 74 2c 20 72 65  Cur, addrNxt, re
3032b 67 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69  gBase, nConstrai
3032c 6e 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61  nt);..    /* Loa
3032d 64 20 74 68 65 20 76 61 6c 75 65 20 66 6f 72 20  d the value for 
3032e 74 68 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63  the inequality c
3032f 6f 6e 73 74 72 61 69 6e 74 20 61 74 20 74 68 65  onstraint at the
30330 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 20 20   end of the.    
30331 2a 2a 20 72 61 6e 67 65 20 28 69 66 20 61 6e 79  ** range (if any
30332 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 43  )..    */.    nC
30333 6f 6e 73 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b  onstraint = nEq;
30334 0a 20 20 20 20 69 66 28 20 70 52 61 6e 67 65 45  .    if( pRangeE
30335 6e 64 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72  nd ){.      Expr
30336 20 2a 70 52 69 67 68 74 20 3d 20 70 52 61 6e 67   *pRight = pRang
30337 65 45 6e 64 2d 3e 70 45 78 70 72 2d 3e 70 52 69  eEnd->pExpr->pRi
30338 67 68 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ght;.      sqlit
30339 65 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f 76  e3ExprCacheRemov
3033a 65 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73  e(pParse, regBas
3033b 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20 73 71  e+nEq);.      sq
3033c 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
3033d 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 72 65  arse, pRight, re
3033e 67 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20  gBase+nEq);.    
3033f 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
30340 65 49 73 4e 75 6c 6c 4a 75 6d 70 28 76 2c 20 70  eIsNullJump(v, p
30341 52 69 67 68 74 2c 20 72 65 67 42 61 73 65 2b 6e  Right, regBase+n
30342 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a 20 20  Eq, addrNxt);.  
30343 20 20 20 20 69 66 28 20 7a 41 66 66 20 29 7b 0a      if( zAff ){.
30344 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
30345 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
30346 74 79 28 70 52 69 67 68 74 2c 20 7a 41 66 66 5b  ty(pRight, zAff[
30347 6e 43 6f 6e 73 74 72 61 69 6e 74 5d 29 3d 3d 53  nConstraint])==S
30348 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 7b  QLITE_AFF_NONE){
30349 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69  .          /* Si
3034a 6e 63 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  nce the comparis
3034b 6f 6e 20 69 73 20 74 6f 20 62 65 20 70 65 72 66  on is to be perf
3034c 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f 20 63 6f  ormed with no co
3034d 6e 76 65 72 73 69 6f 6e 73 0a 20 20 20 20 20 20  nversions.      
3034e 20 20 20 20 2a 2a 20 61 70 70 6c 69 65 64 20 74      ** applied t
3034f 6f 20 74 68 65 20 6f 70 65 72 61 6e 64 73 2c 20  o the operands, 
30350 73 65 74 20 74 68 65 20 61 66 66 69 6e 69 74 79  set the affinity
30351 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70 52 69   to apply to pRi
30352 67 68 74 20 74 6f 20 0a 20 20 20 20 20 20 20 20  ght to .        
30353 20 20 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f    ** SQLITE_AFF_
30354 4e 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20 20 20 20  NONE.  */.      
30355 20 20 20 20 7a 41 66 66 5b 6e 43 6f 6e 73 74 72      zAff[nConstr
30356 61 69 6e 74 5d 20 3d 20 53 51 4c 49 54 45 5f 41  aint] = SQLITE_A
30357 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20  FF_NONE;.       
30358 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 73   }.        if( s
30359 71 6c 69 74 65 33 45 78 70 72 4e 65 65 64 73 4e  qlite3ExprNeedsN
3035a 6f 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  oAffinityChange(
3035b 70 52 69 67 68 74 2c 20 7a 41 66 66 5b 6e 43 6f  pRight, zAff[nCo
3035c 6e 73 74 72 61 69 6e 74 5d 29 20 29 7b 0a 20 20  nstraint]) ){.  
3035d 20 20 20 20 20 20 20 20 7a 41 66 66 5b 6e 43 6f          zAff[nCo
3035e 6e 73 74 72 61 69 6e 74 5d 20 3d 20 53 51 4c 49  nstraint] = SQLI
3035f 54 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20  TE_AFF_NONE;.   
30360 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 20       }.      }  
30361 0a 20 20 20 20 20 20 63 6f 64 65 41 70 70 6c 79  .      codeApply
30362 41 66 66 69 6e 69 74 79 28 70 50 61 72 73 65 2c  Affinity(pParse,
30363 20 72 65 67 42 61 73 65 2c 20 6e 45 71 2b 31 2c   regBase, nEq+1,
30364 20 7a 41 66 66 29 3b 0a 20 20 20 20 20 20 6e 43   zAff);.      nC
30365 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20  onstraint++;.   
30366 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
30367 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  Free(pParse->db,
30368 20 7a 41 66 66 29 3b 0a 0a 20 20 20 20 2f 2a 20   zAff);..    /* 
30369 54 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  Top of the loop 
3036a 62 6f 64 79 20 2a 2f 0a 20 20 20 20 70 4c 65 76  body */.    pLev
3036b 65 6c 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65 33  el->p2 = sqlite3
3036c 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
3036d 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63  v);..    /* Chec
3036e 6b 20 69 66 20 74 68 65 20 69 6e 64 65 78 20 63  k if the index c
3036f 75 72 73 6f 72 20 69 73 20 70 61 73 74 20 74 68  ursor is past th
30370 65 20 65 6e 64 20 6f 66 20 74 68 65 20 72 61 6e  e end of the ran
30371 67 65 2e 20 2a 2f 0a 20 20 20 20 6f 70 20 3d 20  ge. */.    op = 
30372 61 45 6e 64 4f 70 5b 28 70 52 61 6e 67 65 45 6e  aEndOp[(pRangeEn
30373 64 20 7c 7c 20 6e 45 71 29 20 2a 20 28 31 20 2b  d || nEq) * (1 +
30374 20 62 52 65 76 29 5d 3b 0a 20 20 20 20 74 65 73   bRev)];.    tes
30375 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4e 6f  tcase( op==OP_No
30376 6f 70 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  op );.    testca
30377 73 65 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 47 45  se( op==OP_IdxGE
30378 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
30379 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 4c 54 20 29  ( op==OP_IdxLT )
3037a 3b 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 4f 50  ;.    if( op!=OP
3037b 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 73  _Noop ){.      s
3037c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
3037d 49 6e 74 28 76 2c 20 6f 70 2c 20 69 49 64 78 43  Int(v, op, iIdxC
3037e 75 72 2c 20 61 64 64 72 4e 78 74 2c 20 72 65 67  ur, addrNxt, reg
3037f 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e  Base, nConstrain
30380 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
30381 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
30382 20 65 6e 64 45 71 21 3d 62 52 65 76 20 3f 31 3a   endEq!=bRev ?1:
30383 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  0);.    }..    /
30384 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 69  * If there are i
30385 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72  nequality constr
30386 61 69 6e 74 73 2c 20 63 68 65 63 6b 20 74 68 61  aints, check tha
30387 74 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20  t the value.    
30388 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ** of the table 
30389 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20  column that the 
3038a 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 74 72  inequality contr
3038b 61 69 6e 73 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ains is not NULL
3038c 2e 0a 20 20 20 20 2a 2a 20 49 66 20 69 74 20 69  ..    ** If it i
3038d 73 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e  s, jump to the n
3038e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  ext iteration of
3038f 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a   the loop..    *
30390 2f 0a 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74  /.    r1 = sqlit
30391 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
30392 72 73 65 29 3b 0a 20 20 20 20 74 65 73 74 63 61  rse);.    testca
30393 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  se( pLevel->plan
30394 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
30395 5f 42 54 4d 5f 4c 49 4d 49 54 20 29 3b 0a 20 20  _BTM_LIMIT );.  
30396 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 76    testcase( pLev
30397 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
30398 20 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d   & WHERE_TOP_LIM
30399 49 54 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  IT );.    if( pL
3039a 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
3039b 67 73 20 26 20 28 57 48 45 52 45 5f 42 54 4d 5f  gs & (WHERE_BTM_
3039c 4c 49 4d 49 54 7c 57 48 45 52 45 5f 54 4f 50 5f  LIMIT|WHERE_TOP_
3039d 4c 49 4d 49 54 29 20 29 7b 0a 20 20 20 20 20 20  LIMIT) ){.      
3039e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
3039f 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
303a0 69 49 64 78 43 75 72 2c 20 6e 45 71 2c 20 72 31  iIdxCur, nEq, r1
303a1 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
303a2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
303a3 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c 20 61 64 64  _IsNull, r1, add
303a4 72 43 6f 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20  rCont);.    }.  
303a5 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
303a6 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
303a7 72 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65  r1);..    /* See
303a8 6b 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 73  k the table curs
303a9 6f 72 2c 20 69 66 20 72 65 71 75 69 72 65 64 20  or, if required 
303aa 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 54 65  */.    disableTe
303ab 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e 67  rm(pLevel, pRang
303ac 65 53 74 61 72 74 29 3b 0a 20 20 20 20 64 69 73  eStart);.    dis
303ad 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
303ae 20 70 52 61 6e 67 65 45 6e 64 29 3b 0a 20 20 20   pRangeEnd);.   
303af 20 69 66 28 20 21 6f 6d 69 74 54 61 62 6c 65 20   if( !omitTable 
303b0 29 7b 0a 20 20 20 20 20 20 69 52 6f 77 69 64 52  ){.      iRowidR
303b1 65 67 20 3d 20 69 52 65 6c 65 61 73 65 52 65 67  eg = iReleaseReg
303b2 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
303b3 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
303b4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
303b5 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 52  ddOp2(v, OP_IdxR
303b6 6f 77 69 64 2c 20 69 49 64 78 43 75 72 2c 20 69  owid, iIdxCur, i
303b7 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 20  RowidReg);.     
303b8 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
303b9 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69  eStore(pParse, i
303ba 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69 64 52  Cur, -1, iRowidR
303bb 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  eg);.      sqlit
303bc 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
303bd 4f 50 5f 53 65 65 6b 2c 20 69 43 75 72 2c 20 69  OP_Seek, iCur, i
303be 52 6f 77 69 64 52 65 67 29 3b 20 20 2f 2a 20 44  RowidReg);  /* D
303bf 65 66 65 72 72 65 64 20 73 65 65 6b 20 2a 2f 0a  eferred seek */.
303c0 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
303c1 63 6f 72 64 20 74 68 65 20 69 6e 73 74 72 75 63  cord the instruc
303c2 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65 72  tion used to ter
303c3 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e  minate the loop.
303c4 20 44 69 73 61 62 6c 65 20 0a 20 20 20 20 2a 2a   Disable .    **
303c5 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
303c6 72 6d 73 20 6d 61 64 65 20 72 65 64 75 6e 64 61  rms made redunda
303c7 6e 74 20 62 79 20 74 68 65 20 69 6e 64 65 78 20  nt by the index 
303c8 72 61 6e 67 65 20 73 63 61 6e 2e 0a 20 20 20 20  range scan..    
303c9 2a 2f 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f  */.    pLevel->o
303ca 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50 72  p = bRev ? OP_Pr
303cb 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20  ev : OP_Next;.  
303cc 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69    pLevel->p1 = i
303cd 49 64 78 43 75 72 3b 0a 20 20 7d 65 6c 73 65 0a  IdxCur;.  }else.
303ce 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
303cf 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
303d0 54 49 4f 4e 0a 20 20 69 66 28 20 70 4c 65 76 65  TION.  if( pLeve
303d1 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
303d2 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52  & WHERE_MULTI_OR
303d3 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20   ){.    /* Case 
303d4 34 3a 20 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20  4:  Two or more 
303d5 73 65 70 61 72 61 74 65 6c 79 20 69 6e 64 65 78  separately index
303d6 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74  ed terms connect
303d7 65 64 20 62 79 20 4f 52 0a 20 20 20 20 2a 2a 0a  ed by OR.    **.
303d8 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a      ** Example:.
303d9 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
303da 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
303db 61 2c 62 2c 63 2c 64 29 3b 0a 20 20 20 20 2a 2a  a,b,c,d);.    **
303dc 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20     CREATE INDEX 
303dd 69 31 20 4f 4e 20 74 31 28 61 29 3b 0a 20 20 20  i1 ON t1(a);.   
303de 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44   **   CREATE IND
303df 45 58 20 69 32 20 4f 4e 20 74 31 28 62 29 3b 0a  EX i2 ON t1(b);.
303e0 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20      **   CREATE 
303e1 49 4e 44 45 58 20 69 33 20 4f 4e 20 74 31 28 63  INDEX i3 ON t1(c
303e2 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  );.    **.    **
303e3 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
303e4 20 74 31 20 57 48 45 52 45 20 61 3d 35 20 4f 52   t1 WHERE a=5 OR
303e5 20 62 3d 37 20 4f 52 20 28 63 3d 31 31 20 41 4e   b=7 OR (c=11 AN
303e6 44 20 64 3d 31 33 29 0a 20 20 20 20 2a 2a 0a 20  D d=13).    **. 
303e7 20 20 20 2a 2a 20 49 6e 20 74 68 65 20 65 78 61     ** In the exa
303e8 6d 70 6c 65 2c 20 74 68 65 72 65 20 61 72 65 20  mple, there are 
303e9 74 68 72 65 65 20 69 6e 64 65 78 65 64 20 74 65  three indexed te
303ea 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  rms connected by
303eb 20 4f 52 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20   OR..    ** The 
303ec 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  top of the loop 
303ed 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a  looks like this:
303ee 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
303ef 20 20 20 20 20 20 20 20 4e 75 6c 6c 20 20 20 20          Null    
303f0 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20     1            
303f1 20 20 20 20 23 20 5a 65 72 6f 20 74 68 65 20 72      # Zero the r
303f2 6f 77 73 65 74 20 69 6e 20 72 65 67 20 31 0a 20  owset in reg 1. 
303f3 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
303f4 6e 2c 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65  n, for each inde
303f5 78 65 64 20 74 65 72 6d 2c 20 74 68 65 20 66 6f  xed term, the fo
303f6 6c 6c 6f 77 69 6e 67 2e 20 54 68 65 20 61 72 67  llowing. The arg
303f7 75 6d 65 6e 74 73 20 74 6f 0a 20 20 20 20 2a 2a  uments to.    **
303f8 20 52 6f 77 53 65 74 54 65 73 74 20 61 72 65 20   RowSetTest are 
303f9 73 75 63 68 20 74 68 61 74 20 74 68 65 20 72 6f  such that the ro
303fa 77 69 64 20 6f 66 20 74 68 65 20 63 75 72 72 65  wid of the curre
303fb 6e 74 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74  nt row is insert
303fc 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74  ed.    ** into t
303fd 68 65 20 52 6f 77 53 65 74 2e 20 49 66 20 69 74  he RowSet. If it
303fe 20 69 73 20 61 6c 72 65 61 64 79 20 70 72 65 73   is already pres
303ff 65 6e 74 2c 20 63 6f 6e 74 72 6f 6c 20 73 6b 69  ent, control ski
30400 70 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 47 6f  ps the.    ** Go
30401 73 75 62 20 6f 70 63 6f 64 65 20 61 6e 64 20 6a  sub opcode and j
30402 75 6d 70 73 20 73 74 72 61 69 67 68 74 20 74 6f  umps straight to
30403 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
30404 74 65 64 20 62 79 20 57 68 65 72 65 45 6e 64 28  ted by WhereEnd(
30405 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  )..    **.    **
30406 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57          sqlite3W
30407 68 65 72 65 42 65 67 69 6e 28 3c 74 65 72 6d 3e  hereBegin(<term>
30408 29 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  ).    **        
30409 20 20 52 6f 77 53 65 74 54 65 73 74 20 20 20 20    RowSetTest    
3040a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23 20                # 
3040b 49 6e 73 65 72 74 20 72 6f 77 69 64 20 69 6e 74  Insert rowid int
3040c 6f 20 72 6f 77 73 65 74 0a 20 20 20 20 2a 2a 20  o rowset.    ** 
3040d 20 20 20 20 20 20 20 20 20 47 6f 73 75 62 20 20           Gosub  
3040e 20 20 20 20 32 20 41 0a 20 20 20 20 2a 2a 20 20      2 A.    **  
3040f 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68 65        sqlite3Whe
30410 72 65 45 6e 64 28 29 0a 20 20 20 20 2a 2a 0a 20  reEnd().    **. 
30411 20 20 20 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20     ** Following 
30412 74 68 65 20 61 62 6f 76 65 2c 20 63 6f 64 65 20  the above, code 
30413 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65  to terminate the
30414 20 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 41 2c 20   loop. Label A, 
30415 74 68 65 20 74 61 72 67 65 74 0a 20 20 20 20 2a  the target.    *
30416 2a 20 6f 66 20 74 68 65 20 47 6f 73 75 62 20 61  * of the Gosub a
30417 62 6f 76 65 2c 20 6a 75 6d 70 73 20 74 6f 20 74  bove, jumps to t
30418 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 72  he instruction r
30419 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20 47  ight after the G
3041a 6f 74 6f 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  oto..    **.    
3041b 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 75 6c 6c  **          Null
3041c 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20         1        
3041d 20 20 20 20 20 20 20 20 23 20 5a 65 72 6f 20 74          # Zero t
3041e 68 65 20 72 6f 77 73 65 74 20 69 6e 20 72 65 67  he rowset in reg
3041f 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   1.    **       
30420 20 20 20 47 6f 74 6f 20 20 20 20 20 20 20 42 20     Goto       B 
30421 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23                 #
30422 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 66 69 6e   The loop is fin
30423 69 73 68 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20  ished..    **.  
30424 20 20 2a 2a 20 20 20 20 20 20 20 41 3a 20 3c 6c    **       A: <l
30425 6f 6f 70 20 62 6f 64 79 3e 20 20 20 20 20 20 20  oop body>       
30426 20 20 20 20 20 20 20 20 20 20 23 20 52 65 74 75            # Retu
30427 72 6e 20 64 61 74 61 2c 20 77 68 61 74 65 76 65  rn data, whateve
30428 72 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  r..    **.    **
30429 20 20 20 20 20 20 20 20 20 20 52 65 74 75 72 6e            Return
3042a 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20       2          
3042b 20 20 20 20 20 20 23 20 4a 75 6d 70 20 62 61 63        # Jump bac
3042c 6b 20 74 6f 20 74 68 65 20 47 6f 73 75 62 0a 20  k to the Gosub. 
3042d 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
3042e 20 20 20 42 3a 20 3c 61 66 74 65 72 20 74 68 65     B: <after the
3042f 20 6c 6f 6f 70 3e 0a 20 20 20 20 2a 2a 0a 20 20   loop>.    **.  
30430 20 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 43 6c    */.    WhereCl
30431 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20 20 20  ause *pOrWc;    
30432 2f 2a 20 54 68 65 20 4f 52 2d 63 6c 61 75 73 65  /* The OR-clause
30433 20 62 72 6f 6b 65 6e 20 6f 75 74 20 69 6e 74 6f   broken out into
30434 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20 20   subterms */.   
30435 20 57 68 65 72 65 54 65 72 6d 20 2a 70 46 69 6e   WhereTerm *pFin
30436 61 6c 3b 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c  al;     /* Final
30437 20 73 75 62 74 65 72 6d 20 77 69 74 68 69 6e 20   subterm within 
30438 74 68 65 20 4f 52 2d 63 6c 61 75 73 65 2e 20 2a  the OR-clause. *
30439 2f 0a 20 20 20 20 53 72 63 4c 69 73 74 20 6f 6e  /.    SrcList on
3043a 65 54 61 62 3b 20 20 20 20 20 20 20 20 2f 2a 20  eTab;        /* 
3043b 53 68 6f 72 74 65 6e 65 64 20 74 61 62 6c 65 20  Shortened table 
3043c 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20 69 6e 74  list */..    int
3043d 20 72 65 67 52 65 74 75 72 6e 20 3d 20 2b 2b 70   regReturn = ++p
3043e 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20  Parse->nMem;    
3043f 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
30440 65 72 20 75 73 65 64 20 77 69 74 68 20 4f 50 5f  er used with OP_
30441 47 6f 73 75 62 20 2a 2f 0a 20 20 20 20 69 6e 74  Gosub */.    int
30442 20 72 65 67 52 6f 77 73 65 74 20 3d 20 30 3b 20   regRowset = 0; 
30443 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30444 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
30445 65 72 20 66 6f 72 20 52 6f 77 53 65 74 20 6f 62  er for RowSet ob
30446 6a 65 63 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ject */.    int 
30447 72 65 67 52 6f 77 69 64 20 3d 20 30 3b 20 20 20  regRowid = 0;   
30448 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30449 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
3044a 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 20  r holding rowid 
3044b 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4c 6f 6f 70  */.    int iLoop
3044c 42 6f 64 79 20 3d 20 73 71 6c 69 74 65 33 56 64  Body = sqlite3Vd
3044d 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20  beMakeLabel(v); 
3044e 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 6c 6f 6f   /* Start of loo
3044f 70 20 62 6f 64 79 20 2a 2f 0a 20 20 20 20 69 6e  p body */.    in
30450 74 20 69 52 65 74 49 6e 69 74 3b 20 20 20 20 20  t iRetInit;     
30451 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30452 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
30453 73 73 20 6f 66 20 72 65 67 52 65 74 75 72 6e 20  ss of regReturn 
30454 69 6e 69 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  init */.    int 
30455 69 69 3b 0a 20 20 20 0a 20 20 20 20 70 54 65 72  ii;.   .    pTer
30456 6d 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  m = pLevel->plan
30457 2e 75 2e 70 54 65 72 6d 3b 0a 20 20 20 20 61 73  .u.pTerm;.    as
30458 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29  sert( pTerm!=0 )
30459 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
3045a 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  erm->eOperator==
3045b 57 4f 5f 4f 52 20 29 3b 0a 20 20 20 20 61 73 73  WO_OR );.    ass
3045c 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46  ert( (pTerm->wtF
3045d 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e  lags & TERM_ORIN
3045e 46 4f 29 21 3d 30 20 29 3b 0a 20 20 20 20 70 4f  FO)!=0 );.    pO
3045f 72 57 63 20 3d 20 26 70 54 65 72 6d 2d 3e 75 2e  rWc = &pTerm->u.
30460 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20  pOrInfo->wc;.   
30461 20 70 46 69 6e 61 6c 20 3d 20 26 70 4f 72 57 63   pFinal = &pOrWc
30462 2d 3e 61 5b 70 4f 72 57 63 2d 3e 6e 54 65 72 6d  ->a[pOrWc->nTerm
30463 2d 31 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74  -1];..    /* Set
30464 20 75 70 20 61 20 53 72 63 4c 69 73 74 20 63 6f   up a SrcList co
30465 6e 74 61 69 6e 69 6e 67 20 6a 75 73 74 20 74 68  ntaining just th
30466 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 73 63  e table being sc
30467 61 6e 6e 65 64 20 62 79 20 74 68 69 73 20 6c 6f  anned by this lo
30468 6f 70 2e 20 2a 2f 0a 20 20 20 20 6f 6e 65 54 61  op. */.    oneTa
30469 62 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 20  b.nSrc = 1;.    
3046a 6f 6e 65 54 61 62 2e 6e 41 6c 6c 6f 63 20 3d 20  oneTab.nAlloc = 
3046b 31 3b 0a 20 20 20 20 6f 6e 65 54 61 62 2e 61 5b  1;.    oneTab.a[
3046c 30 5d 20 3d 20 2a 70 54 61 62 49 74 65 6d 3b 0a  0] = *pTabItem;.
3046d 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  .    /* Initiali
3046e 7a 65 20 74 68 65 20 72 6f 77 73 65 74 20 72 65  ze the rowset re
3046f 67 69 73 74 65 72 20 74 6f 20 63 6f 6e 74 61 69  gister to contai
30470 6e 20 4e 55 4c 4c 2e 20 41 6e 20 53 51 4c 20 4e  n NULL. An SQL N
30471 55 4c 4c 20 69 73 20 0a 20 20 20 20 2a 2a 20 65  ULL is .    ** e
30472 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 6e 20  quivalent to an 
30473 65 6d 70 74 79 20 72 6f 77 73 65 74 2e 0a 20 20  empty rowset..  
30474 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f    **.    ** Also
30475 20 69 6e 69 74 69 61 6c 69 7a 65 20 72 65 67 52   initialize regR
30476 65 74 75 72 6e 20 74 6f 20 63 6f 6e 74 61 69 6e  eturn to contain
30477 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
30478 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  the instruction 
30479 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74  .    ** immediat
3047a 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ely following th
3047b 65 20 4f 50 5f 52 65 74 75 72 6e 20 61 74 20 74  e OP_Return at t
3047c 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65  he bottom of the
3047d 20 6c 6f 6f 70 2e 20 54 68 69 73 0a 20 20 20 20   loop. This.    
3047e 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 69  ** is required i
3047f 6e 20 61 20 66 65 77 20 6f 62 73 63 75 72 65 20  n a few obscure 
30480 4c 45 46 54 20 4a 4f 49 4e 20 63 61 73 65 73 20  LEFT JOIN cases 
30481 77 68 65 72 65 20 63 6f 6e 74 72 6f 6c 20 6a 75  where control ju
30482 6d 70 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72 20  mps.    ** over 
30483 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6c  the top of the l
30484 6f 6f 70 20 69 6e 74 6f 20 74 68 65 20 62 6f 64  oop into the bod
30485 79 20 6f 66 20 69 74 2e 20 49 6e 20 74 68 69 73  y of it. In this
30486 20 63 61 73 65 20 74 68 65 20 0a 20 20 20 20 2a   case the .    *
30487 2a 20 63 6f 72 72 65 63 74 20 72 65 73 70 6f 6e  * correct respon
30488 73 65 20 66 6f 72 20 74 68 65 20 65 6e 64 2d 6f  se for the end-o
30489 66 2d 6c 6f 6f 70 20 63 6f 64 65 20 28 74 68 65  f-loop code (the
3048a 20 4f 50 5f 52 65 74 75 72 6e 29 20 69 73 20 74   OP_Return) is t
3048b 6f 20 0a 20 20 20 20 2a 2a 20 66 61 6c 6c 20 74  o .    ** fall t
3048c 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
3048d 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 20  xt instruction, 
3048e 6a 75 73 74 20 61 73 20 61 6e 20 4f 50 5f 4e 65  just as an OP_Ne
3048f 78 74 20 64 6f 65 73 20 69 66 0a 20 20 20 20 2a  xt does if.    *
30490 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 75  * called on an u
30491 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 75 72  ninitialized cur
30492 73 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  sor..    */.    
30493 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20  if( (wctrlFlags 
30494 26 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54  & WHERE_DUPLICAT
30495 45 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0a 20 20 20  ES_OK)==0 ){.   
30496 20 20 20 72 65 67 52 6f 77 73 65 74 20 3d 20 2b     regRowset = +
30497 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
30498 20 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20       regRowid = 
30499 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
3049a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3049b 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
3049c 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 73 65 74  ll, 0, regRowset
3049d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 52 65  );.    }.    iRe
3049e 74 49 6e 69 74 20 3d 20 73 71 6c 69 74 65 33 56  tInit = sqlite3V
3049f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
304a0 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52  Integer, 0, regR
304a1 65 74 75 72 6e 29 3b 0a 0a 20 20 20 20 66 6f 72  eturn);..    for
304a2 28 69 69 3d 30 3b 20 69 69 3c 70 4f 72 57 63 2d  (ii=0; ii<pOrWc-
304a3 3e 6e 54 65 72 6d 3b 20 69 69 2b 2b 29 7b 0a 20  >nTerm; ii++){. 
304a4 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a       WhereTerm *
304a5 70 4f 72 54 65 72 6d 20 3d 20 26 70 4f 72 57 63  pOrTerm = &pOrWc
304a6 2d 3e 61 5b 69 69 5d 3b 0a 20 20 20 20 20 20 69  ->a[ii];.      i
304a7 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74  f( pOrTerm->left
304a8 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 7c 7c 20  Cursor==iCur || 
304a9 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
304aa 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20 20  or==WO_AND ){.  
304ab 20 20 20 20 20 20 57 68 65 72 65 49 6e 66 6f 20        WhereInfo 
304ac 2a 70 53 75 62 57 49 6e 66 6f 3b 20 20 20 20 20  *pSubWInfo;     
304ad 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 66 6f 72       /* Info for
304ae 20 73 69 6e 67 6c 65 20 4f 52 2d 74 65 72 6d 20   single OR-term 
304af 73 63 61 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  scan */.        
304b0 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
304b1 74 61 62 6c 65 20 65 6e 74 72 69 65 73 20 74 68  table entries th
304b2 61 74 20 6d 61 74 63 68 20 74 65 72 6d 20 70 4f  at match term pO
304b3 72 54 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20  rTerm. */.      
304b4 20 20 70 53 75 62 57 49 6e 66 6f 20 3d 20 73 71    pSubWInfo = sq
304b5 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
304b6 70 50 61 72 73 65 2c 20 26 6f 6e 65 54 61 62 2c  pParse, &oneTab,
304b7 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2c   pOrTerm->pExpr,
304b8 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
304b9 20 20 20 20 20 20 20 20 20 20 20 20 57 48 45 52              WHER
304ba 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 7c 20 57 48  E_OMIT_OPEN | WH
304bb 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 7c  ERE_OMIT_CLOSE |
304bc 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 41 42   WHERE_FORCE_TAB
304bd 4c 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  LE);.        if(
304be 20 70 53 75 62 57 49 6e 66 6f 20 29 7b 0a 20 20   pSubWInfo ){.  
304bf 20 20 20 20 20 20 20 20 69 66 28 20 28 77 63 74          if( (wct
304c0 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  rlFlags & WHERE_
304c1 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3d 3d  DUPLICATES_OK)==
304c2 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
304c3 20 69 6e 74 20 69 53 65 74 20 3d 20 28 28 69 69   int iSet = ((ii
304c4 3d 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31  ==pOrWc->nTerm-1
304c5 29 3f 2d 31 3a 69 69 29 3b 0a 20 20 20 20 20 20  )?-1:ii);.      
304c6 20 20 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20        int r;.   
304c7 20 20 20 20 20 20 20 20 20 72 20 3d 20 73 71 6c           r = sql
304c8 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43  ite3ExprCodeGetC
304c9 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 54  olumn(pParse, pT
304ca 61 62 49 74 65 6d 2d 3e 70 54 61 62 2c 20 2d 31  abItem->pTab, -1
304cb 2c 20 69 43 75 72 2c 20 0a 20 20 20 20 20 20 20  , iCur, .       
304cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304ce 20 20 72 65 67 52 6f 77 69 64 2c 20 30 29 3b 0a    regRowid, 0);.
304cf 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
304d0 74 65 33 56 64 62 65 41 64 64 4f 70 34 49 6e 74  te3VdbeAddOp4Int
304d1 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 54 65 73  (v, OP_RowSetTes
304d2 74 2c 20 72 65 67 52 6f 77 73 65 74 2c 0a 20 20  t, regRowset,.  
304d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
304d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
304d5 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
304d6 74 41 64 64 72 28 76 29 2b 32 2c 20 72 2c 20 69  tAddr(v)+2, r, i
304d7 53 65 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Set);.          
304d8 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  }.          sqli
304d9 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
304da 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 52 65   OP_Gosub, regRe
304db 74 75 72 6e 2c 20 69 4c 6f 6f 70 42 6f 64 79 29  turn, iLoopBody)
304dc 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;..          /* 
304dd 46 69 6e 69 73 68 20 74 68 65 20 6c 6f 6f 70 20  Finish the loop 
304de 74 68 72 6f 75 67 68 20 74 61 62 6c 65 20 65 6e  through table en
304df 74 72 69 65 73 20 74 68 61 74 20 6d 61 74 63 68  tries that match
304e0 20 74 65 72 6d 20 70 4f 72 54 65 72 6d 2e 20 2a   term pOrTerm. *
304e1 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  /.          sqli
304e2 74 65 33 57 68 65 72 65 45 6e 64 28 70 53 75 62  te3WhereEnd(pSub
304e3 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20  WInfo);.        
304e4 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
304e5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
304e6 68 61 6e 67 65 50 31 28 76 2c 20 69 52 65 74 49  hangeP1(v, iRetI
304e7 6e 69 74 2c 20 73 71 6c 69 74 65 33 56 64 62 65  nit, sqlite3Vdbe
304e8 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 29 3b  CurrentAddr(v));
304e9 0a 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 56  .    /* sqlite3V
304ea 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
304eb 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 73  Null, 0, regRows
304ec 65 74 29 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69  et); */.    sqli
304ed 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
304ee 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 4c 65   OP_Goto, 0, pLe
304ef 76 65 6c 2d 3e 61 64 64 72 42 72 6b 29 3b 0a 20  vel->addrBrk);. 
304f0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
304f1 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 4c  solveLabel(v, iL
304f2 6f 6f 70 42 6f 64 79 29 3b 0a 0a 20 20 20 20 70  oopBody);..    p
304f3 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 52  Level->op = OP_R
304f4 65 74 75 72 6e 3b 0a 20 20 20 20 70 4c 65 76 65  eturn;.    pLeve
304f5 6c 2d 3e 70 31 20 3d 20 72 65 67 52 65 74 75 72  l->p1 = regRetur
304f6 6e 3b 0a 20 20 20 20 64 69 73 61 62 6c 65 54 65  n;.    disableTe
304f7 72 6d 28 70 4c 65 76 65 6c 2c 20 70 54 65 72 6d  rm(pLevel, pTerm
304f8 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69  );.  }else.#endi
304f9 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
304fa 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  _OR_OPTIMIZATION
304fb 20 2a 2f 0a 0a 20 20 7b 0a 20 20 20 20 2f 2a 20   */..  {.    /* 
304fc 43 61 73 65 20 35 3a 20 20 54 68 65 72 65 20 69  Case 5:  There i
304fd 73 20 6e 6f 20 75 73 61 62 6c 65 20 69 6e 64 65  s no usable inde
304fe 78 2e 20 20 57 65 20 6d 75 73 74 20 64 6f 20 61  x.  We must do a
304ff 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a   complete.    **
30500 20 20 20 20 20 20 20 20 20 20 73 63 61 6e 20 6f            scan o
30501 66 20 74 68 65 20 65 6e 74 69 72 65 20 74 61 62  f the entire tab
30502 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  le..    */.    s
30503 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61  tatic const u8 a
30504 53 74 65 70 5b 5d 20 3d 20 7b 20 4f 50 5f 4e 65  Step[] = { OP_Ne
30505 78 74 2c 20 4f 50 5f 50 72 65 76 20 7d 3b 0a 20  xt, OP_Prev };. 
30506 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
30507 75 38 20 61 53 74 61 72 74 5b 5d 20 3d 20 7b 20  u8 aStart[] = { 
30508 4f 50 5f 52 65 77 69 6e 64 2c 20 4f 50 5f 4c 61  OP_Rewind, OP_La
30509 73 74 20 7d 3b 0a 20 20 20 20 61 73 73 65 72 74  st };.    assert
3050a 28 20 62 52 65 76 3d 3d 30 20 7c 7c 20 62 52 65  ( bRev==0 || bRe
3050b 76 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65  v==1 );.    asse
3050c 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30  rt( omitTable==0
3050d 20 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e   );.    pLevel->
3050e 6f 70 20 3d 20 61 53 74 65 70 5b 62 52 65 76 5d  op = aStep[bRev]
3050f 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 31  ;.    pLevel->p1
30510 20 3d 20 69 43 75 72 3b 0a 20 20 20 20 70 4c 65   = iCur;.    pLe
30511 76 65 6c 2d 3e 70 32 20 3d 20 31 20 2b 20 73 71  vel->p2 = 1 + sq
30512 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
30513 76 2c 20 61 53 74 61 72 74 5b 62 52 65 76 5d 2c  v, aStart[bRev],
30514 20 69 43 75 72 2c 20 61 64 64 72 42 72 6b 29 3b   iCur, addrBrk);
30515 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 35 20  .    pLevel->p5 
30516 3d 20 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41  = SQLITE_STMTSTA
30517 54 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45  TUS_FULLSCAN_STE
30518 50 3b 0a 20 20 7d 0a 20 20 6e 6f 74 52 65 61 64  P;.  }.  notRead
30519 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28 70 57  y &= ~getMask(pW
3051a 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 69 43 75  C->pMaskSet, iCu
3051b 72 29 3b 0a 0a 20 20 2f 2a 20 49 6e 73 65 72 74  r);..  /* Insert
3051c 20 63 6f 64 65 20 74 6f 20 74 65 73 74 20 65 76   code to test ev
3051d 65 72 79 20 73 75 62 65 78 70 72 65 73 73 69 6f  ery subexpressio
3051e 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f  n that can be co
3051f 6d 70 6c 65 74 65 6c 79 0a 20 20 2a 2a 20 63 6f  mpletely.  ** co
30520 6d 70 75 74 65 64 20 75 73 69 6e 67 20 74 68 65  mputed using the
30521 20 63 75 72 72 65 6e 74 20 73 65 74 20 6f 66 20   current set of 
30522 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 6b  tables..  */.  k
30523 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 54 65 72   = 0;.  for(pTer
30524 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 70 57 43 2d  m=pWC->a, j=pWC-
30525 3e 6e 54 65 72 6d 3b 20 6a 3e 30 3b 20 6a 2d 2d  >nTerm; j>0; j--
30526 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20  , pTerm++){.    
30527 45 78 70 72 20 2a 70 45 3b 0a 20 20 20 20 74 65  Expr *pE;.    te
30528 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 77  stcase( pTerm->w
30529 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49  tFlags & TERM_VI
3052a 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 74 65 73  RTUAL );.    tes
3052b 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 77 74  tcase( pTerm->wt
3052c 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44  Flags & TERM_COD
3052d 45 44 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54  ED );.    if( pT
3052e 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28  erm->wtFlags & (
3052f 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52  TERM_VIRTUAL|TER
30530 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f 6e 74 69  M_CODED) ) conti
30531 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 28 70 54  nue;.    if( (pT
30532 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26  erm->prereqAll &
30533 20 6e 6f 74 52 65 61 64 79 29 21 3d 30 20 29 20   notReady)!=0 ) 
30534 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 45  continue;.    pE
30535 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b   = pTerm->pExpr;
30536 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 45 21  .    assert( pE!
30537 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  =0 );.    if( pL
30538 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20  evel->iLeftJoin 
30539 26 26 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  && !ExprHasPrope
3053a 72 74 79 28 70 45 2c 20 45 50 5f 46 72 6f 6d 4a  rty(pE, EP_FromJ
3053b 6f 69 6e 29 20 29 7b 0a 20 20 20 20 20 20 63 6f  oin) ){.      co
3053c 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20  ntinue;.    }.  
3053d 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
3053e 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 2c  alse(pParse, pE,
3053f 20 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c 49 54   addrCont, SQLIT
30540 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
30541 20 20 20 6b 20 3d 20 31 3b 0a 20 20 20 20 70 54     k = 1;.    pT
30542 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20  erm->wtFlags |= 
30543 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 7d 0a  TERM_CODED;.  }.
30544 0a 20 20 2f 2a 20 46 6f 72 20 61 20 4c 45 46 54  .  /* For a LEFT
30545 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 67 65 6e   OUTER JOIN, gen
30546 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20  erate code that 
30547 77 69 6c 6c 20 72 65 63 6f 72 64 20 74 68 65 20  will record the 
30548 66 61 63 74 20 74 68 61 74 0a 20 20 2a 2a 20 61  fact that.  ** a
30549 74 20 6c 65 61 73 74 20 6f 6e 65 20 72 6f 77 20  t least one row 
3054a 6f 66 20 74 68 65 20 72 69 67 68 74 20 74 61 62  of the right tab
3054b 6c 65 20 68 61 73 20 6d 61 74 63 68 65 64 20 74  le has matched t
3054c 68 65 20 6c 65 66 74 20 74 61 62 6c 65 2e 20 20  he left table.  
3054d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4c 65 76  .  */.  if( pLev
3054e 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 7b  el->iLeftJoin ){
3054f 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61 64 64  .    pLevel->add
30550 72 46 69 72 73 74 20 3d 20 73 71 6c 69 74 65 33  rFirst = sqlite3
30551 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
30552 76 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  v);.    sqlite3V
30553 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
30554 49 6e 74 65 67 65 72 2c 20 31 2c 20 70 4c 65 76  Integer, 1, pLev
30555 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a  el->iLeftJoin);.
30556 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
30557 28 76 2c 20 22 72 65 63 6f 72 64 20 4c 45 46 54  (v, "record LEFT
30558 20 4a 4f 49 4e 20 68 69 74 22 29 29 3b 0a 20 20   JOIN hit"));.  
30559 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
3055a 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
3055b 0a 20 20 20 20 66 6f 72 28 70 54 65 72 6d 3d 70  .    for(pTerm=p
3055c 57 43 2d 3e 61 2c 20 6a 3d 30 3b 20 6a 3c 70 57  WC->a, j=0; j<pW
3055d 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70  C->nTerm; j++, p
3055e 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 74  Term++){.      t
3055f 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e  estcase( pTerm->
30560 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56  wtFlags & TERM_V
30561 49 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 20 20  IRTUAL );.      
30562 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
30563 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
30564 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 20 20 69  CODED );.      i
30565 66 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67  f( pTerm->wtFlag
30566 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54 55 41  s & (TERM_VIRTUA
30567 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 20  L|TERM_CODED) ) 
30568 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
30569 69 66 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72  if( (pTerm->prer
3056a 65 71 41 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79  eqAll & notReady
3056b 29 21 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  )!=0 ) continue;
3056c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
3056d 54 65 72 6d 2d 3e 70 45 78 70 72 20 29 3b 0a 20  Term->pExpr );. 
3056e 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
3056f 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
30570 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 61 64  pTerm->pExpr, ad
30571 64 72 43 6f 6e 74 2c 20 53 51 4c 49 54 45 5f 4a  drCont, SQLITE_J
30572 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20  UMPIFNULL);.    
30573 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73    pTerm->wtFlags
30574 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a   |= TERM_CODED;.
30575 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
30576 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
30577 67 28 70 50 61 72 73 65 2c 20 69 52 65 6c 65 61  g(pParse, iRelea
30578 73 65 52 65 67 29 3b 0a 0a 20 20 72 65 74 75 72  seReg);..  retur
30579 6e 20 6e 6f 74 52 65 61 64 79 3b 0a 7d 0a 0a 23  n notReady;.}..#
3057a 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
3057b 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20 54 68  E_TEST)./*.** Th
3057c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69  e following vari
3057d 61 62 6c 65 20 68 6f 6c 64 73 20 61 20 74 65 78  able holds a tex
3057e 74 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  t description of
3057f 20 71 75 65 72 79 20 70 6c 61 6e 20 67 65 6e 65   query plan gene
30580 72 61 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  rated.** by the 
30581 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c  most recent call
30582 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65 72 65   to sqlite3Where
30583 42 65 67 69 6e 28 29 2e 20 20 45 61 63 68 20 63  Begin().  Each c
30584 61 6c 6c 20 74 6f 20 57 68 65 72 65 42 65 67 69  all to WhereBegi
30585 6e 0a 2a 2a 20 6f 76 65 72 77 72 69 74 65 73 20  n.** overwrites 
30586 74 68 65 20 70 72 65 76 69 6f 75 73 2e 20 20 54  the previous.  T
30587 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
30588 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
30589 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c 79  ing and.** analy
3058a 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c  sis only..*/.SQL
3058b 49 54 45 5f 41 50 49 20 63 68 61 72 20 73 71 6c  ITE_API char sql
3058c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
3058d 42 4d 53 2a 32 2a 34 30 5d 3b 20 20 2f 2a 20 54  BMS*2*40];  /* T
3058e 65 78 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e 20  ext of the join 
3058f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 51  */.static int nQ
30590 50 6c 61 6e 20 3d 20 30 3b 20 20 20 20 20 20 20  Plan = 0;       
30591 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 66         /* Next f
30592 72 65 65 20 73 6c 6f 77 20 69 6e 20 5f 71 75 65  ree slow in _que
30593 72 79 5f 70 6c 61 6e 5b 5d 20 2a 2f 0a 0a 23 65  ry_plan[] */..#e
30594 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54  ndif /* SQLITE_T
30595 45 53 54 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46  EST */.../*.** F
30596 72 65 65 20 61 20 57 68 65 72 65 49 6e 66 6f 20  ree a WhereInfo 
30597 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a 73 74 61  structure.*/.sta
30598 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 49 6e  tic void whereIn
30599 66 6f 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a  foFree(sqlite3 *
3059a 64 62 2c 20 57 68 65 72 65 49 6e 66 6f 20 2a 70  db, WhereInfo *p
3059b 57 49 6e 66 6f 29 7b 0a 20 20 69 66 28 20 70 57  WInfo){.  if( pW
3059c 49 6e 66 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20  Info ){.    int 
3059d 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  i;.    for(i=0; 
3059e 69 3c 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c  i<pWInfo->nLevel
3059f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
305a0 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
305a1 20 2a 70 49 6e 66 6f 20 3d 20 70 57 49 6e 66 6f   *pInfo = pWInfo
305a2 2d 3e 61 5b 69 5d 2e 70 49 64 78 49 6e 66 6f 3b  ->a[i].pIdxInfo;
305a3 0a 20 20 20 20 20 20 69 66 28 20 70 49 6e 66 6f  .      if( pInfo
305a4 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 61   ){.        /* a
305a5 73 73 65 72 74 28 20 70 49 6e 66 6f 2d 3e 6e 65  ssert( pInfo->ne
305a6 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 3d 3d  edToFreeIdxStr==
305a7 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  0 || db->mallocF
305a8 61 69 6c 65 64 20 29 3b 20 2a 2f 0a 20 20 20 20  ailed ); */.    
305a9 20 20 20 20 69 66 28 20 70 49 6e 66 6f 2d 3e 6e      if( pInfo->n
305aa 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20  eedToFreeIdxStr 
305ab 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
305ac 69 74 65 33 5f 66 72 65 65 28 70 49 6e 66 6f 2d  ite3_free(pInfo-
305ad 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20 20 20  >idxStr);.      
305ae 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
305af 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49  te3DbFree(db, pI
305b0 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  nfo);.      }.  
305b1 20 20 7d 0a 20 20 20 20 77 68 65 72 65 43 6c 61    }.    whereCla
305b2 75 73 65 43 6c 65 61 72 28 70 57 49 6e 66 6f 2d  useClear(pWInfo-
305b3 3e 70 57 43 29 3b 0a 20 20 20 20 73 71 6c 69 74  >pWC);.    sqlit
305b4 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 57 49  e3DbFree(db, pWI
305b5 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a  nfo);.  }.}.../*
305b6 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65  .** Generate the
305b7 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
305b8 65 20 6c 6f 6f 70 20 75 73 65 64 20 66 6f 72 20  e loop used for 
305b9 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f  WHERE clause pro
305ba 63 65 73 73 69 6e 67 2e 0a 2a 2a 20 54 68 65 20  cessing..** The 
305bb 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
305bc 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
305bd 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65  opaque structure
305be 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a   that contains.*
305bf 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65  * information ne
305c0 65 64 65 64 20 74 6f 20 74 65 72 6d 69 6e 61 74  eded to terminat
305c1 65 20 74 68 65 20 6c 6f 6f 70 2e 20 20 4c 61 74  e the loop.  Lat
305c2 65 72 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  er, the calling 
305c3 72 6f 75 74 69 6e 65 0a 2a 2a 20 73 68 6f 75 6c  routine.** shoul
305c4 64 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33  d invoke sqlite3
305c5 57 68 65 72 65 45 6e 64 28 29 20 77 69 74 68 20  WhereEnd() with 
305c6 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
305c7 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
305c8 6e 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f  n.** in order to
305c9 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 57 48   complete the WH
305ca 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65  ERE clause proce
305cb 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssing..**.** If 
305cc 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
305cd 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
305ce 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  turns NULL..**.*
305cf 2a 20 54 68 65 20 62 61 73 69 63 20 69 64 65 61  * The basic idea
305d0 20 69 73 20 74 6f 20 64 6f 20 61 20 6e 65 73 74   is to do a nest
305d1 65 64 20 6c 6f 6f 70 2c 20 6f 6e 65 20 6c 6f 6f  ed loop, one loo
305d2 70 20 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65  p for each table
305d3 20 69 6e 0a 2a 2a 20 74 68 65 20 46 52 4f 4d 20   in.** the FROM 
305d4 63 6c 61 75 73 65 20 6f 66 20 61 20 73 65 6c 65  clause of a sele
305d5 63 74 2e 20 20 28 49 4e 53 45 52 54 20 61 6e 64  ct.  (INSERT and
305d6 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
305d7 74 73 20 61 72 65 20 74 68 65 0a 2a 2a 20 73 61  ts are the.** sa
305d8 6d 65 20 61 73 20 61 20 53 45 4c 45 43 54 20 77  me as a SELECT w
305d9 69 74 68 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c  ith only a singl
305da 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  e table in the F
305db 52 4f 4d 20 63 6c 61 75 73 65 2e 29 20 20 46 6f  ROM clause.)  Fo
305dc 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66  r.** example, if
305dd 20 74 68 65 20 53 51 4c 20 69 73 20 74 68 69 73   the SQL is this
305de 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 45  :.**.**       SE
305df 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20  LECT * FROM t1, 
305e0 74 32 2c 20 74 33 20 57 48 45 52 45 20 2e 2e 2e  t2, t3 WHERE ...
305e1 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74 68 65  ;.**.** Then the
305e2 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
305e3 69 73 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20  is conceptually 
305e4 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  like the followi
305e5 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66  ng:.**.**      f
305e6 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e 20 74  oreach row1 in t
305e7 31 20 64 6f 20 20 20 20 20 20 20 5c 20 20 20 20  1 do       \    
305e8 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a 2a  Code generated.*
305e9 2a 20 20 20 20 20 20 20 20 66 6f 72 65 61 63 68  *        foreach
305ea 20 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 20 20   row2 in t2 do  
305eb 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69 74      |-- by sqlit
305ec 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 0a 2a  e3WhereBegin().*
305ed 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 65 61  *          forea
305ee 63 68 20 72 6f 77 33 20 69 6e 20 74 33 20 64 6f  ch row3 in t3 do
305ef 20 20 20 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20     /.**         
305f0 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20 20     ....**       
305f1 20 20 20 65 6e 64 20 20 20 20 20 20 20 20 20 20     end          
305f2 20 20 20 20 20 20 20 20 20 20 20 5c 20 20 20 20             \    
305f3 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a 2a  Code generated.*
305f4 2a 20 20 20 20 20 20 20 20 65 6e 64 20 20 20 20  *        end    
305f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
305f6 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69 74      |-- by sqlit
305f7 65 33 57 68 65 72 65 45 6e 64 28 29 0a 2a 2a 20  e3WhereEnd().** 
305f8 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20 20       end        
305f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
305fa 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68   /.**.** Note th
305fb 61 74 20 74 68 65 20 6c 6f 6f 70 73 20 6d 69 67  at the loops mig
305fc 68 74 20 6e 6f 74 20 62 65 20 6e 65 73 74 65 64  ht not be nested
305fd 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20 69 6e   in the order in
305fe 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20 61   which they.** a
305ff 70 70 65 61 72 20 69 6e 20 74 68 65 20 46 52 4f  ppear in the FRO
30600 4d 20 63 6c 61 75 73 65 20 69 66 20 61 20 64 69  M clause if a di
30601 66 66 65 72 65 6e 74 20 6f 72 64 65 72 20 69 73  fferent order is
30602 20 62 65 74 74 65 72 20 61 62 6c 65 20 74 6f 20   better able to 
30603 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 69  make.** use of i
30604 6e 64 69 63 65 73 2e 20 20 4e 6f 74 65 20 61 6c  ndices.  Note al
30605 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68 65  so that when the
30606 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 61 70 70   IN operator app
30607 65 61 72 73 20 69 6e 0a 2a 2a 20 74 68 65 20 57  ears in.** the W
30608 48 45 52 45 20 63 6c 61 75 73 65 2c 20 69 74 20  HERE clause, it 
30609 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20  might result in 
3060a 61 64 64 69 74 69 6f 6e 61 6c 20 6e 65 73 74 65  additional neste
3060b 64 20 6c 6f 6f 70 73 20 66 6f 72 0a 2a 2a 20 73  d loops for.** s
3060c 63 61 6e 6e 69 6e 67 20 74 68 72 6f 75 67 68 20  canning through 
3060d 61 6c 6c 20 76 61 6c 75 65 73 20 6f 6e 20 74 68  all values on th
3060e 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
3060f 65 20 6f 66 20 74 68 65 20 49 4e 2e 0a 2a 2a 0a  e of the IN..**.
30610 2a 2a 20 54 68 65 72 65 20 61 72 65 20 42 74 72  ** There are Btr
30611 65 65 20 63 75 72 73 6f 72 73 20 61 73 73 6f 63  ee cursors assoc
30612 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20  iated with each 
30613 74 61 62 6c 65 2e 20 20 74 31 20 75 73 65 73 20  table.  t1 uses 
30614 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72  cursor.** number
30615 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e   pTabList->a[0].
30616 69 43 75 72 73 6f 72 2e 20 20 74 32 20 75 73 65  iCursor.  t2 use
30617 73 20 74 68 65 20 63 75 72 73 6f 72 20 70 54 61  s the cursor pTa
30618 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e 69 43 75 72  bList->a[1].iCur
30619 73 6f 72 2e 0a 2a 2a 20 41 6e 64 20 73 6f 20 66  sor..** And so f
3061a 6f 72 74 68 2e 20 20 54 68 69 73 20 72 6f 75 74  orth.  This rout
3061b 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
3061c 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 6f 73 65  de to open those
3061d 20 56 44 42 45 20 63 75 72 73 6f 72 73 0a 2a 2a   VDBE cursors.**
3061e 20 61 6e 64 20 73 71 6c 69 74 65 33 57 68 65 72   and sqlite3Wher
3061f 65 45 6e 64 28 29 20 67 65 6e 65 72 61 74 65 73  eEnd() generates
30620 20 74 68 65 20 63 6f 64 65 20 74 6f 20 63 6c 6f   the code to clo
30621 73 65 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54  se them..**.** T
30622 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 71 6c  he code that sql
30623 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
30624 20 67 65 6e 65 72 61 74 65 73 20 6c 65 61 76 65   generates leave
30625 73 20 74 68 65 20 63 75 72 73 6f 72 73 20 6e 61  s the cursors na
30626 6d 65 64 0a 2a 2a 20 69 6e 20 70 54 61 62 4c 69  med.** in pTabLi
30627 73 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74  st pointing at t
30628 68 65 69 72 20 61 70 70 72 6f 70 72 69 61 74 65  heir appropriate
30629 20 65 6e 74 72 69 65 73 2e 20 20 54 68 65 20 5b   entries.  The [
3062a 2e 2e 2e 5d 20 63 6f 64 65 0a 2a 2a 20 63 61 6e  ...] code.** can
3062b 20 75 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61   use OP_Column a
3062c 6e 64 20 4f 50 5f 52 6f 77 69 64 20 6f 70 63 6f  nd OP_Rowid opco
3062d 64 65 73 20 6f 6e 20 74 68 65 73 65 20 63 75 72  des on these cur
3062e 73 6f 72 73 20 74 6f 20 65 78 74 72 61 63 74 0a  sors to extract.
3062f 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  ** data from the
30630 20 76 61 72 69 6f 75 73 20 74 61 62 6c 65 73 20   various tables 
30631 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a  of the loop..**.
30632 2a 2a 20 49 66 20 74 68 65 20 57 48 45 52 45 20  ** If the WHERE 
30633 63 6c 61 75 73 65 20 69 73 20 65 6d 70 74 79 2c  clause is empty,
30634 20 74 68 65 20 66 6f 72 65 61 63 68 20 6c 6f 6f   the foreach loo
30635 70 73 20 6d 75 73 74 20 65 61 63 68 20 73 63 61  ps must each sca
30636 6e 20 74 68 65 69 72 0a 2a 2a 20 65 6e 74 69 72  n their.** entir
30637 65 20 74 61 62 6c 65 73 2e 20 20 54 68 75 73 20  e tables.  Thus 
30638 61 20 74 68 72 65 65 2d 77 61 79 20 6a 6f 69 6e  a three-way join
30639 20 69 73 20 61 6e 20 4f 28 4e 5e 33 29 20 6f 70   is an O(N^3) op
3063a 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 66  eration.  But if
3063b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 73 20 68  .** the tables h
3063c 61 76 65 20 69 6e 64 69 63 65 73 20 61 6e 64 20  ave indices and 
3063d 74 68 65 72 65 20 61 72 65 20 74 65 72 6d 73 20  there are terms 
3063e 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  in the WHERE cla
3063f 75 73 65 20 74 68 61 74 0a 2a 2a 20 72 65 66 65  use that.** refe
30640 72 20 74 6f 20 74 68 6f 73 65 20 69 6e 64 69 63  r to those indic
30641 65 73 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20 74  es, a complete t
30642 61 62 6c 65 20 73 63 61 6e 20 63 61 6e 20 62 65  able scan can be
30643 20 61 76 6f 69 64 65 64 20 61 6e 64 20 74 68 65   avoided and the
30644 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 72 75  .** code will ru
30645 6e 20 6d 75 63 68 20 66 61 73 74 65 72 2e 20 20  n much faster.  
30646 4d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b  Most of the work
30647 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
30648 20 69 73 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20   is checking.** 
30649 74 6f 20 73 65 65 20 69 66 20 74 68 65 72 65 20  to see if there 
3064a 61 72 65 20 69 6e 64 69 63 65 73 20 74 68 61 74  are indices that
3064b 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
3064c 73 70 65 65 64 20 75 70 20 74 68 65 20 6c 6f 6f  speed up the loo
3064d 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73 20 6f  p..**.** Terms o
3064e 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
3064f 73 65 20 61 72 65 20 61 6c 73 6f 20 75 73 65 64  se are also used
30650 20 74 6f 20 6c 69 6d 69 74 20 77 68 69 63 68 20   to limit which 
30651 72 6f 77 73 20 61 63 74 75 61 6c 6c 79 0a 2a 2a  rows actually.**
30652 20 6d 61 6b 65 20 69 74 20 74 6f 20 74 68 65 20   make it to the 
30653 22 2e 2e 2e 22 20 69 6e 20 74 68 65 20 6d 69 64  "..." in the mid
30654 64 6c 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e  dle of the loop.
30655 20 20 41 66 74 65 72 20 65 61 63 68 20 22 66 6f    After each "fo
30656 72 65 61 63 68 22 2c 0a 2a 2a 20 74 65 72 6d 73  reach",.** terms
30657 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
30658 61 75 73 65 20 74 68 61 74 20 75 73 65 20 6f 6e  ause that use on
30659 6c 79 20 74 65 72 6d 73 20 69 6e 20 74 68 61 74  ly terms in that
3065a 20 6c 6f 6f 70 20 61 6e 64 20 6f 75 74 65 72 0a   loop and outer.
3065b 2a 2a 20 6c 6f 6f 70 73 20 61 72 65 20 65 76 61  ** loops are eva
3065c 6c 75 61 74 65 64 20 61 6e 64 20 69 66 20 66 61  luated and if fa
3065d 6c 73 65 20 61 20 6a 75 6d 70 20 69 73 20 6d 61  lse a jump is ma
3065e 64 65 20 61 72 6f 75 6e 64 20 61 6c 6c 20 73 75  de around all su
3065f 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e 6e 65  bsequent.** inne
30660 72 20 6c 6f 6f 70 73 20 28 6f 72 20 61 72 6f 75  r loops (or arou
30661 6e 64 20 74 68 65 20 22 2e 2e 2e 22 20 69 66 20  nd the "..." if 
30662 74 68 65 20 74 65 73 74 20 6f 63 63 75 72 73 20  the test occurs 
30663 77 69 74 68 69 6e 20 74 68 65 20 69 6e 6e 65 72  within the inner
30664 2d 0a 2a 2a 20 6d 6f 73 74 20 6c 6f 6f 70 29 0a  -.** most loop).
30665 2a 2a 0a 2a 2a 20 4f 55 54 45 52 20 4a 4f 49 4e  **.** OUTER JOIN
30666 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 6f 75 74 65 72  S.**.** An outer
30667 20 6a 6f 69 6e 20 6f 66 20 74 61 62 6c 65 73 20   join of tables 
30668 74 31 20 61 6e 64 20 74 32 20 69 73 20 63 6f 6e  t1 and t2 is con
30669 63 65 70 74 61 6c 6c 79 20 63 6f 64 65 64 20 61  ceptally coded a
3066a 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
3066b 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31      foreach row1
3066c 20 69 6e 20 74 31 20 64 6f 0a 2a 2a 20 20 20 20   in t1 do.**    
3066d 20 20 66 6c 61 67 20 3d 20 30 0a 2a 2a 20 20 20    flag = 0.**   
3066e 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 32 20     foreach row2 
3066f 69 6e 20 74 32 20 64 6f 0a 2a 2a 20 20 20 20 20  in t2 do.**     
30670 20 20 20 73 74 61 72 74 3a 0a 2a 2a 20 20 20 20     start:.**    
30671 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20        ....**    
30672 20 20 20 20 20 20 66 6c 61 67 20 3d 20 31 0a 2a        flag = 1.*
30673 2a 20 20 20 20 20 20 65 6e 64 0a 2a 2a 20 20 20  *      end.**   
30674 20 20 20 69 66 20 66 6c 61 67 3d 3d 30 20 74 68     if flag==0 th
30675 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 6d 6f 76  en.**        mov
30676 65 20 74 68 65 20 72 6f 77 32 20 63 75 72 73 6f  e the row2 curso
30677 72 20 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 0a  r to a null row.
30678 2a 2a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73  **        goto s
30679 74 61 72 74 0a 2a 2a 20 20 20 20 20 20 66 69 0a  tart.**      fi.
3067a 2a 2a 20 20 20 20 65 6e 64 0a 2a 2a 0a 2a 2a 20  **    end.**.** 
3067b 4f 52 44 45 52 20 42 59 20 43 4c 41 55 53 45 20  ORDER BY CLAUSE 
3067c 50 52 4f 43 45 53 53 49 4e 47 0a 2a 2a 0a 2a 2a  PROCESSING.**.**
3067d 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 61   *ppOrderBy is a
3067e 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
3067f 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
30680 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
30681 65 6d 65 6e 74 2c 0a 2a 2a 20 69 66 20 74 68 65  ement,.** if the
30682 72 65 20 69 73 20 6f 6e 65 2e 20 20 49 66 20 74  re is one.  If t
30683 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52  here is no ORDER
30684 20 42 59 20 63 6c 61 75 73 65 20 6f 72 20 69 66   BY clause or if
30685 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
30686 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   is called from 
30687 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c  an UPDATE or DEL
30688 45 54 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  ETE statement, t
30689 68 65 6e 20 70 70 4f 72 64 65 72 42 79 20 69 73  hen ppOrderBy is
3068a 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   NULL..**.** If 
3068b 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20  an index can be 
3068c 75 73 65 64 20 73 6f 20 74 68 61 74 20 74 68 65  used so that the
3068d 20 6e 61 74 75 72 61 6c 20 6f 75 74 70 75 74 20   natural output 
3068e 6f 72 64 65 72 20 6f 66 20 74 68 65 20 74 61 62  order of the tab
3068f 6c 65 0a 2a 2a 20 73 63 61 6e 20 69 73 20 63 6f  le.** scan is co
30690 72 72 65 63 74 20 66 6f 72 20 74 68 65 20 4f 52  rrect for the OR
30691 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74  DER BY clause, t
30692 68 65 6e 20 74 68 61 74 20 69 6e 64 65 78 20 69  hen that index i
30693 73 20 75 73 65 64 20 61 6e 64 0a 2a 2a 20 2a 70  s used and.** *p
30694 70 4f 72 64 65 72 42 79 20 69 73 20 73 65 74 20  pOrderBy is set 
30695 74 6f 20 4e 55 4c 4c 2e 20 20 54 68 69 73 20 69  to NULL.  This i
30696 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
30697 6e 20 74 68 61 74 20 70 72 65 76 65 6e 74 73 20  n that prevents 
30698 61 6e 0a 2a 2a 20 75 6e 6e 65 63 65 73 73 61 72  an.** unnecessar
30699 79 20 73 6f 72 74 20 6f 66 20 74 68 65 20 72 65  y sort of the re
3069a 73 75 6c 74 20 73 65 74 20 69 66 20 61 6e 20 69  sult set if an i
3069b 6e 64 65 78 20 61 70 70 72 6f 70 72 69 61 74 65  ndex appropriate
3069c 20 66 6f 72 20 74 68 65 0a 2a 2a 20 4f 52 44 45   for the.** ORDE
3069d 52 20 42 59 20 63 6c 61 75 73 65 20 61 6c 72 65  R BY clause alre
3069e 61 64 79 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a  ady exists..**.*
3069f 2a 20 49 66 20 74 68 65 20 77 68 65 72 65 20 63  * If the where c
306a0 6c 61 75 73 65 20 6c 6f 6f 70 73 20 63 61 6e 6e  lause loops cann
306a1 6f 74 20 62 65 20 61 72 72 61 6e 67 65 64 20 74  ot be arranged t
306a2 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20 63 6f  o provide the co
306a3 72 72 65 63 74 0a 2a 2a 20 6f 75 74 70 75 74 20  rrect.** output 
306a4 6f 72 64 65 72 2c 20 74 68 65 6e 20 74 68 65 20  order, then the 
306a5 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 75 6e  *ppOrderBy is un
306a6 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49  changed..*/.SQLI
306a7 54 45 5f 50 52 49 56 41 54 45 20 57 68 65 72 65  TE_PRIVATE Where
306a8 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65  Info *sqlite3Whe
306a9 72 65 42 65 67 69 6e 28 0a 20 20 50 61 72 73 65  reBegin(.  Parse
306aa 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
306ab 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
306ac 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
306ad 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20  ist *pTabList,  
306ae 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 61    /* A list of a
306af 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20  ll tables to be 
306b0 73 63 61 6e 6e 65 64 20 2a 2f 0a 20 20 45 78 70  scanned */.  Exp
306b1 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20  r *pWhere,      
306b2 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
306b3 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
306b4 4c 69 73 74 20 2a 2a 70 70 4f 72 64 65 72 42 79  List **ppOrderBy
306b5 2c 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59  , /* An ORDER BY
306b6 20 63 6c 61 75 73 65 2c 20 6f 72 20 4e 55 4c 4c   clause, or NULL
306b7 20 2a 2f 0a 20 20 75 31 36 20 77 63 74 72 6c 46   */.  u16 wctrlF
306b8 6c 61 67 73 20 20 20 20 20 20 20 20 2f 2a 20 4f  lags        /* O
306b9 6e 65 20 6f 66 20 74 68 65 20 57 48 45 52 45 5f  ne of the WHERE_
306ba 2a 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20  * flags defined 
306bb 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
306bc 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
306bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
306be 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
306bf 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  r */.  int nByte
306c0 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 20  WInfo;          
306c1 20 20 2f 2a 20 4e 75 6d 2e 20 62 79 74 65 73 20    /* Num. bytes 
306c2 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 57 68  allocated for Wh
306c3 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74 20 2a  ereInfo struct *
306c4 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70  /.  WhereInfo *p
306c5 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20 2f  WInfo;         /
306c6 2a 20 57 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68  * Will become th
306c7 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f  e return value o
306c8 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  f this function 
306c9 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  */.  Vdbe *v = p
306ca 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20  Parse->pVdbe;   
306cb 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 64  /* The virtual d
306cc 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a  atabase engine *
306cd 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52  /.  Bitmask notR
306ce 65 61 64 79 3b 20 20 20 20 20 20 20 20 20 20 2f  eady;          /
306cf 2a 20 43 75 72 73 6f 72 73 20 74 68 61 74 20 61  * Cursors that a
306d0 72 65 20 6e 6f 74 20 79 65 74 20 70 6f 73 69 74  re not yet posit
306d1 69 6f 6e 65 64 20 2a 2f 0a 20 20 57 68 65 72 65  ioned */.  Where
306d2 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
306d3 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70  t;    /* The exp
306d4 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20 73 65 74  ression mask set
306d5 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
306d6 65 20 2a 70 57 43 3b 20 20 20 20 20 20 20 20 20  e *pWC;         
306d7 20 20 20 20 20 20 2f 2a 20 44 65 63 6f 6d 70 6f        /* Decompo
306d8 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20 57 48  sition of the WH
306d9 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
306da 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
306db 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 3b 20 20  tem *pTabItem;  
306dc 2f 2a 20 41 20 73 69 6e 67 6c 65 20 65 6e 74 72  /* A single entr
306dd 79 20 66 72 6f 6d 20 70 54 61 62 4c 69 73 74 20  y from pTabList 
306de 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c 20  */.  WhereLevel 
306df 2a 70 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20  *pLevel;        
306e0 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
306e1 20 6c 65 76 65 6c 20 69 6e 20 74 68 65 20 70 57   level in the pW
306e2 49 6e 66 6f 20 6c 69 73 74 20 2a 2f 0a 20 20 69  Info list */.  i
306e3 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20  nt iFrom;       
306e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
306e5 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20 46  * First unused F
306e6 52 4f 4d 20 63 6c 61 75 73 65 20 65 6c 65 6d 65  ROM clause eleme
306e7 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 61 6e 64 46  nt */.  int andF
306e8 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
306e9 20 20 20 2f 2a 20 41 4e 44 2d 65 64 20 63 6f 6d     /* AND-ed com
306ea 62 69 6e 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20  bination of all 
306eb 70 57 43 2d 3e 61 5b 5d 2e 77 74 46 6c 61 67 73  pWC->a[].wtFlags
306ec 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
306ed 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
306ee 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
306ef 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a  nection */..  /*
306f0 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   The number of t
306f1 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52 4f  ables in the FRO
306f2 4d 20 63 6c 61 75 73 65 20 69 73 20 6c 69 6d 69  M clause is limi
306f3 74 65 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65  ted by the numbe
306f4 72 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73 20 69  r of.  ** bits i
306f5 6e 20 61 20 42 69 74 6d 61 73 6b 20 0a 20 20 2a  n a Bitmask .  *
306f6 2f 0a 20 20 69 66 28 20 70 54 61 62 4c 69 73 74  /.  if( pTabList
306f7 2d 3e 6e 53 72 63 3e 42 4d 53 20 29 7b 0a 20 20  ->nSrc>BMS ){.  
306f8 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
306f9 67 28 70 50 61 72 73 65 2c 20 22 61 74 20 6d 6f  g(pParse, "at mo
306fa 73 74 20 25 64 20 74 61 62 6c 65 73 20 69 6e 20  st %d tables in 
306fb 61 20 6a 6f 69 6e 22 2c 20 42 4d 53 29 3b 0a 20  a join", BMS);. 
306fc 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
306fd 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
306fe 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 74  and initialize t
306ff 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72  he WhereInfo str
30700 75 63 74 75 72 65 20 74 68 61 74 20 77 69 6c 6c  ucture that will
30701 20 62 65 63 6f 6d 65 20 74 68 65 0a 20 20 2a 2a   become the.  **
30702 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 20 41   return value. A
30703 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63 61 74 69   single allocati
30704 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 73 74  on is used to st
30705 6f 72 65 20 74 68 65 20 57 68 65 72 65 49 6e 66  ore the WhereInf
30706 6f 0a 20 20 2a 2a 20 73 74 72 75 63 74 2c 20 74  o.  ** struct, t
30707 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 57  he contents of W
30708 68 65 72 65 49 6e 66 6f 2e 61 5b 5d 2c 20 74 68  hereInfo.a[], th
30709 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73 74  e WhereClause st
3070a 72 75 63 74 75 72 65 0a 20 20 2a 2a 20 61 6e 64  ructure.  ** and
3070b 20 74 68 65 20 57 68 65 72 65 4d 61 73 6b 53 65   the WhereMaskSe
3070c 74 20 73 74 72 75 63 74 75 72 65 2e 20 53 69 6e  t structure. Sin
3070d 63 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 63  ce WhereClause c
3070e 6f 6e 74 61 69 6e 73 20 61 6e 20 38 2d 62 79 74  ontains an 8-byt
3070f 65 0a 20 20 2a 2a 20 66 69 65 6c 64 20 28 74 79  e.  ** field (ty
30710 70 65 20 42 69 74 6d 61 73 6b 29 20 69 74 20 6d  pe Bitmask) it m
30711 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20 6f  ust be aligned o
30712 6e 20 61 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e  n an 8-byte boun
30713 64 61 72 79 20 6f 6e 0a 20 20 2a 2a 20 73 6f 6d  dary on.  ** som
30714 65 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2e  e architectures.
30715 20 48 65 6e 63 65 20 74 68 65 20 52 4f 55 4e 44   Hence the ROUND
30716 38 28 29 20 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a  8() below..  */.
30717 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
30718 62 3b 0a 20 20 6e 42 79 74 65 57 49 6e 66 6f 20  b;.  nByteWInfo 
30719 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28  = ROUND8(sizeof(
3071a 57 68 65 72 65 49 6e 66 6f 29 2b 28 70 54 61 62  WhereInfo)+(pTab
3071b 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 29 2a 73 69  List->nSrc-1)*si
3071c 7a 65 6f 66 28 57 68 65 72 65 4c 65 76 65 6c 29  zeof(WhereLevel)
3071d 29 3b 0a 20 20 70 57 49 6e 66 6f 20 3d 20 73 71  );.  pWInfo = sq
3071e 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
3071f 6f 28 64 62 2c 20 0a 20 20 20 20 20 20 6e 42 79  o(db, .      nBy
30720 74 65 57 49 6e 66 6f 20 2b 20 0a 20 20 20 20 20  teWInfo + .     
30721 20 73 69 7a 65 6f 66 28 57 68 65 72 65 43 6c 61   sizeof(WhereCla
30722 75 73 65 29 20 2b 0a 20 20 20 20 20 20 73 69 7a  use) +.      siz
30723 65 6f 66 28 57 68 65 72 65 4d 61 73 6b 53 65 74  eof(WhereMaskSet
30724 29 0a 20 20 29 3b 0a 20 20 69 66 28 20 64 62 2d  ).  );.  if( db-
30725 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
30726 0a 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42  .    goto whereB
30727 65 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 20  eginError;.  }. 
30728 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 20   pWInfo->nLevel 
30729 3d 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  = pTabList->nSrc
3072a 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72  ;.  pWInfo->pPar
3072b 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70  se = pParse;.  p
3072c 57 49 6e 66 6f 2d 3e 70 54 61 62 4c 69 73 74 20  WInfo->pTabList 
3072d 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 70 57  = pTabList;.  pW
3072e 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 20 3d 20 73  Info->iBreak = s
3072f 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
30730 62 65 6c 28 76 29 3b 0a 20 20 70 57 49 6e 66 6f  bel(v);.  pWInfo
30731 2d 3e 70 57 43 20 3d 20 70 57 43 20 3d 20 28 57  ->pWC = pWC = (W
30732 68 65 72 65 43 6c 61 75 73 65 20 2a 29 26 28 28  hereClause *)&((
30733 75 38 20 2a 29 70 57 49 6e 66 6f 29 5b 6e 42 79  u8 *)pWInfo)[nBy
30734 74 65 57 49 6e 66 6f 5d 3b 0a 20 20 70 57 49 6e  teWInfo];.  pWIn
30735 66 6f 2d 3e 77 63 74 72 6c 46 6c 61 67 73 20 3d  fo->wctrlFlags =
30736 20 77 63 74 72 6c 46 6c 61 67 73 3b 0a 20 20 70   wctrlFlags;.  p
30737 4d 61 73 6b 53 65 74 20 3d 20 28 57 68 65 72 65  MaskSet = (Where
30738 4d 61 73 6b 53 65 74 2a 29 26 70 57 43 5b 31 5d  MaskSet*)&pWC[1]
30739 3b 0a 0a 20 20 2f 2a 20 53 70 6c 69 74 20 74 68  ;..  /* Split th
3073a 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69  e WHERE clause i
3073b 6e 74 6f 20 73 65 70 61 72 61 74 65 20 73 75 62  nto separate sub
3073c 65 78 70 72 65 73 73 69 6f 6e 73 20 77 68 65 72  expressions wher
3073d 65 20 65 61 63 68 0a 20 20 2a 2a 20 73 75 62 65  e each.  ** sube
3073e 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 70  xpression is sep
3073f 61 72 61 74 65 64 20 62 79 20 61 6e 20 41 4e 44  arated by an AND
30740 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a   operator..  */.
30741 20 20 69 6e 69 74 4d 61 73 6b 53 65 74 28 70 4d    initMaskSet(pM
30742 61 73 6b 53 65 74 29 3b 0a 20 20 77 68 65 72 65  askSet);.  where
30743 43 6c 61 75 73 65 49 6e 69 74 28 70 57 43 2c 20  ClauseInit(pWC, 
30744 70 50 61 72 73 65 2c 20 70 4d 61 73 6b 53 65 74  pParse, pMaskSet
30745 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
30746 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 70 50  CodeConstants(pP
30747 61 72 73 65 2c 20 70 57 68 65 72 65 29 3b 0a 20  arse, pWhere);. 
30748 20 77 68 65 72 65 53 70 6c 69 74 28 70 57 43 2c   whereSplit(pWC,
30749 20 70 57 68 65 72 65 2c 20 54 4b 5f 41 4e 44 29   pWhere, TK_AND)
3074a 3b 0a 20 20 20 20 0a 20 20 2f 2a 20 53 70 65 63  ;.    .  /* Spec
3074b 69 61 6c 20 63 61 73 65 3a 20 61 20 57 48 45 52  ial case: a WHER
3074c 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 69 73  E clause that is
3074d 20 63 6f 6e 73 74 61 6e 74 2e 20 20 45 76 61 6c   constant.  Eval
3074e 75 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 65 78  uate the.  ** ex
3074f 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 65 69 74  pression and eit
30750 68 65 72 20 6a 75 6d 70 20 6f 76 65 72 20 61 6c  her jump over al
30751 6c 20 6f 66 20 74 68 65 20 63 6f 64 65 20 6f 72  l of the code or
30752 20 66 61 6c 6c 20 74 68 72 75 2e 0a 20 20 2a 2f   fall thru..  */
30753 0a 20 20 69 66 28 20 70 57 68 65 72 65 20 26 26  .  if( pWhere &&
30754 20 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63   (pTabList->nSrc
30755 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 45 78  ==0 || sqlite3Ex
30756 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a  prIsConstantNotJ
30757 6f 69 6e 28 70 57 68 65 72 65 29 29 20 29 7b 0a  oin(pWhere)) ){.
30758 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
30759 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
3075a 57 68 65 72 65 2c 20 70 57 49 6e 66 6f 2d 3e 69  Where, pWInfo->i
3075b 42 72 65 61 6b 2c 20 53 51 4c 49 54 45 5f 4a 55  Break, SQLITE_JU
3075c 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 70  MPIFNULL);.    p
3075d 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a  Where = 0;.  }..
3075e 20 20 2f 2a 20 41 73 73 69 67 6e 20 61 20 62 69    /* Assign a bi
3075f 74 20 66 72 6f 6d 20 74 68 65 20 62 69 74 6d 61  t from the bitma
30760 73 6b 20 74 6f 20 65 76 65 72 79 20 74 65 72 6d  sk to every term
30761 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
30762 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  use..  **.  ** W
30763 68 65 6e 20 61 73 73 69 67 6e 69 6e 67 20 62 69  hen assigning bi
30764 74 6d 61 73 6b 20 76 61 6c 75 65 73 20 74 6f 20  tmask values to 
30765 46 52 4f 4d 20 63 6c 61 75 73 65 20 63 75 72 73  FROM clause curs
30766 6f 72 73 2c 20 69 74 20 6d 75 73 74 20 62 65 0a  ors, it must be.
30767 20 20 2a 2a 20 74 68 65 20 63 61 73 65 20 74 68    ** the case th
30768 61 74 20 69 66 20 58 20 69 73 20 74 68 65 20 62  at if X is the b
30769 69 74 6d 61 73 6b 20 66 6f 72 20 74 68 65 20 4e  itmask for the N
3076a 2d 74 68 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  -th FROM clause 
3076b 74 65 72 6d 20 74 68 65 6e 0a 20 20 2a 2a 20 74  term then.  ** t
3076c 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 61  he bitmask for a
3076d 6c 6c 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74  ll FROM clause t
3076e 65 72 6d 73 20 74 6f 20 74 68 65 20 6c 65 66 74  erms to the left
3076f 20 6f 66 20 74 68 65 20 4e 2d 74 68 20 74 65 72   of the N-th ter
30770 6d 0a 20 20 2a 2a 20 69 73 20 28 58 2d 31 29 2e  m.  ** is (X-1).
30771 20 20 20 41 6e 20 65 78 70 72 65 73 73 69 6f 6e     An expression
30772 20 66 72 6f 6d 20 74 68 65 20 4f 4e 20 63 6c 61   from the ON cla
30773 75 73 65 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f  use of a LEFT JO
30774 49 4e 20 63 61 6e 20 75 73 65 0a 20 20 2a 2a 20  IN can use.  ** 
30775 69 74 73 20 45 78 70 72 2e 69 52 69 67 68 74 4a  its Expr.iRightJ
30776 6f 69 6e 54 61 62 6c 65 20 76 61 6c 75 65 20 74  oinTable value t
30777 6f 20 66 69 6e 64 20 74 68 65 20 62 69 74 6d 61  o find the bitma
30778 73 6b 20 6f 66 20 74 68 65 20 72 69 67 68 74 20  sk of the right 
30779 74 61 62 6c 65 0a 20 20 2a 2a 20 6f 66 20 74 68  table.  ** of th
3077a 65 20 6a 6f 69 6e 2e 20 20 53 75 62 74 72 61 63  e join.  Subtrac
3077b 74 69 6e 67 20 6f 6e 65 20 66 72 6f 6d 20 74 68  ting one from th
3077c 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 62 69  e right table bi
3077d 74 6d 61 73 6b 20 67 69 76 65 73 20 61 0a 20 20  tmask gives a.  
3077e 2a 2a 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 61  ** bitmask for a
3077f 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65  ll tables to the
30780 20 6c 65 66 74 20 6f 66 20 74 68 65 20 6a 6f 69   left of the joi
30781 6e 2e 20 20 4b 6e 6f 77 69 6e 67 20 74 68 65 20  n.  Knowing the 
30782 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20 66 6f 72  bitmask.  ** for
30783 20 61 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 74   all tables to t
30784 68 65 20 6c 65 66 74 20 6f 66 20 61 20 6c 65 66  he left of a lef
30785 74 20 6a 6f 69 6e 20 69 73 20 69 6d 70 6f 72 74  t join is import
30786 61 6e 74 2e 20 20 54 69 63 6b 65 74 20 23 33 30  ant.  Ticket #30
30787 31 35 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 6f  15..  **.  ** Co
30788 6e 66 69 67 75 72 65 20 74 68 65 20 57 68 65 72  nfigure the Wher
30789 65 43 6c 61 75 73 65 2e 76 6d 61 73 6b 20 76 61  eClause.vmask va
3078a 72 69 61 62 6c 65 20 73 6f 20 74 68 61 74 20 62  riable so that b
3078b 69 74 73 20 74 68 61 74 20 63 6f 72 72 65 73 70  its that corresp
3078c 6f 6e 64 0a 20 20 2a 2a 20 74 6f 20 76 69 72 74  ond.  ** to virt
3078d 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73 6f 72  ual table cursor
3078e 73 20 61 72 65 20 73 65 74 2e 20 54 68 69 73 20  s are set. This 
3078f 69 73 20 75 73 65 64 20 74 6f 20 73 65 6c 65 63  is used to selec
30790 74 69 76 65 6c 79 20 64 69 73 61 62 6c 65 20 0a  tively disable .
30791 20 20 2a 2a 20 74 68 65 20 4f 52 2d 74 6f 2d 49    ** the OR-to-I
30792 4e 20 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e  N transformation
30793 20 69 6e 20 65 78 70 72 41 6e 61 6c 79 7a 65 4f   in exprAnalyzeO
30794 72 54 65 72 6d 28 29 2e 20 49 74 20 69 73 20 6e  rTerm(). It is n
30795 6f 74 20 68 65 6c 70 66 75 6c 20 0a 20 20 2a 2a  ot helpful .  **
30796 20 77 69 74 68 20 76 69 72 74 75 61 6c 20 74 61   with virtual ta
30797 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  bles..  */.  ass
30798 65 72 74 28 20 70 57 43 2d 3e 76 6d 61 73 6b 3d  ert( pWC->vmask=
30799 3d 30 20 26 26 20 70 4d 61 73 6b 53 65 74 2d 3e  =0 && pMaskSet->
3079a 6e 3d 3d 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d  n==0 );.  for(i=
3079b 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  0; i<pTabList->n
3079c 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  Src; i++){.    c
3079d 72 65 61 74 65 4d 61 73 6b 28 70 4d 61 73 6b 53  reateMask(pMaskS
3079e 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  et, pTabList->a[
3079f 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 23 69 66  i].iCursor);.#if
307a0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
307a1 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
307a2 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61    if( ALWAYS(pTa
307a3 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 54 61 62  bList->a[i].pTab
307a4 29 20 26 26 20 49 73 56 69 72 74 75 61 6c 28 70  ) && IsVirtual(p
307a5 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 54  TabList->a[i].pT
307a6 61 62 29 20 29 7b 0a 20 20 20 20 20 20 70 57 43  ab) ){.      pWC
307a7 2d 3e 76 6d 61 73 6b 20 7c 3d 20 28 28 42 69 74  ->vmask |= ((Bit
307a8 6d 61 73 6b 29 31 20 3c 3c 20 69 29 3b 0a 20 20  mask)1 << i);.  
307a9 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 23    }.#endif.  }.#
307aa 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
307ab 7b 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 74 6f  {.    Bitmask to
307ac 54 68 65 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20  TheLeft = 0;.   
307ad 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
307ae 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29  List->nSrc; i++)
307af 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20  {.      Bitmask 
307b0 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73  m = getMask(pMas
307b1 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e  kSet, pTabList->
307b2 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20  a[i].iCursor);. 
307b3 20 20 20 20 20 61 73 73 65 72 74 28 20 28 6d 2d       assert( (m-
307b4 31 29 3d 3d 74 6f 54 68 65 4c 65 66 74 20 29 3b  1)==toTheLeft );
307b5 0a 20 20 20 20 20 20 74 6f 54 68 65 4c 65 66 74  .      toTheLeft
307b6 20 7c 3d 20 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d   |= m;.    }.  }
307b7 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 6e  .#endif..  /* An
307b8 61 6c 79 7a 65 20 61 6c 6c 20 6f 66 20 74 68 65  alyze all of the
307b9 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 2e   subexpressions.
307ba 20 20 4e 6f 74 65 20 74 68 61 74 20 65 78 70 72    Note that expr
307bb 41 6e 61 6c 79 7a 65 28 29 20 6d 69 67 68 74 0a  Analyze() might.
307bc 20 20 2a 2a 20 61 64 64 20 6e 65 77 20 76 69 72    ** add new vir
307bd 74 75 61 6c 20 74 65 72 6d 73 20 6f 6e 74 6f 20  tual terms onto 
307be 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57  the end of the W
307bf 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 57 65  HERE clause.  We
307c0 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 77 61 6e   do not.  ** wan
307c1 74 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65  t to analyze the
307c2 73 65 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73  se virtual terms
307c3 2c 20 73 6f 20 73 74 61 72 74 20 61 6e 61 6c 79  , so start analy
307c4 7a 69 6e 67 20 61 74 20 74 68 65 20 65 6e 64 0a  zing at the end.
307c5 20 20 2a 2a 20 61 6e 64 20 77 6f 72 6b 20 66 6f    ** and work fo
307c6 72 77 61 72 64 20 73 6f 20 74 68 61 74 20 74 68  rward so that th
307c7 65 20 61 64 64 65 64 20 76 69 72 74 75 61 6c 20  e added virtual 
307c8 74 65 72 6d 73 20 61 72 65 20 6e 65 76 65 72 20  terms are never 
307c9 70 72 6f 63 65 73 73 65 64 2e 0a 20 20 2a 2f 0a  processed..  */.
307ca 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c    exprAnalyzeAll
307cb 28 70 54 61 62 4c 69 73 74 2c 20 70 57 43 29 3b  (pTabList, pWC);
307cc 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
307cd 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
307ce 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72  oto whereBeginEr
307cf 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  ror;.  }..  /* C
307d0 68 6f 73 65 20 74 68 65 20 62 65 73 74 20 69 6e  hose the best in
307d1 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72 20 65  dex to use for e
307d2 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74 68 65  ach table in the
307d3 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 20 20   FROM clause..  
307d4 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f  **.  ** This loo
307d5 70 20 66 69 6c 6c 73 20 69 6e 20 74 68 65 20 66  p fills in the f
307d6 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 73 3a  ollowing fields:
307d7 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 70 57 49  .  **.  **   pWI
307d8 6e 66 6f 2d 3e 61 5b 5d 2e 70 49 64 78 20 20 20  nfo->a[].pIdx   
307d9 20 20 20 54 68 65 20 69 6e 64 65 78 20 74 6f 20     The index to 
307da 75 73 65 20 66 6f 72 20 74 68 69 73 20 6c 65 76  use for this lev
307db 65 6c 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a  el of the loop..
307dc 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61    **   pWInfo->a
307dd 5b 5d 2e 77 73 46 6c 61 67 73 20 20 20 57 48 45  [].wsFlags   WHE
307de 52 45 5f 78 78 78 20 66 6c 61 67 73 20 61 73 73  RE_xxx flags ass
307df 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 49 64  ociated with pId
307e0 78 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  x.  **   pWInfo-
307e1 3e 61 5b 5d 2e 6e 45 71 20 20 20 20 20 20 20 54  >a[].nEq       T
307e2 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20  he number of == 
307e3 61 6e 64 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e  and IN constrain
307e4 74 73 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f  ts.  **   pWInfo
307e5 2d 3e 61 5b 5d 2e 69 46 72 6f 6d 20 20 20 20 20  ->a[].iFrom     
307e6 57 68 69 63 68 20 74 65 72 6d 20 6f 66 20 74 68  Which term of th
307e7 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73  e FROM clause is
307e8 20 62 65 69 6e 67 20 63 6f 64 65 64 0a 20 20 2a   being coded.  *
307e9 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e  *   pWInfo->a[].
307ea 69 54 61 62 43 75 72 20 20 20 54 68 65 20 56 44  iTabCur   The VD
307eb 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68  BE cursor for th
307ec 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  e database table
307ed 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e  .  **   pWInfo->
307ee 61 5b 5d 2e 69 49 64 78 43 75 72 20 20 20 54 68  a[].iIdxCur   Th
307ef 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f  e VDBE cursor fo
307f0 72 20 74 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a  r the index.  **
307f1 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70     pWInfo->a[].p
307f2 54 65 72 6d 20 20 20 20 20 57 68 65 6e 20 77 73  Term     When ws
307f3 46 6c 61 67 73 3d 3d 57 4f 5f 4f 52 2c 20 74 68  Flags==WO_OR, th
307f4 65 20 4f 52 2d 63 6c 61 75 73 65 20 74 65 72 6d  e OR-clause term
307f5 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
307f6 6c 6f 6f 70 20 61 6c 73 6f 20 66 69 67 75 72 65  loop also figure
307f7 73 20 6f 75 74 20 74 68 65 20 6e 65 73 74 69 6e  s out the nestin
307f8 67 20 6f 72 64 65 72 20 6f 66 20 74 61 62 6c 65  g order of table
307f9 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20  s in the FROM.  
307fa 2a 2a 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a  ** clause..  */.
307fb 20 20 6e 6f 74 52 65 61 64 79 20 3d 20 7e 28 42    notReady = ~(B
307fc 69 74 6d 61 73 6b 29 30 3b 0a 20 20 70 54 61 62  itmask)0;.  pTab
307fd 49 74 65 6d 20 3d 20 70 54 61 62 4c 69 73 74 2d  Item = pTabList-
307fe 3e 61 3b 0a 20 20 70 4c 65 76 65 6c 20 3d 20 70  >a;.  pLevel = p
307ff 57 49 6e 66 6f 2d 3e 61 3b 0a 20 20 61 6e 64 46  WInfo->a;.  andF
30800 6c 61 67 73 20 3d 20 7e 30 3b 0a 20 20 57 48 45  lags = ~0;.  WHE
30801 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70  RETRACE(("*** Op
30802 74 69 6d 69 7a 65 72 20 53 74 61 72 74 20 2a 2a  timizer Start **
30803 2a 5c 6e 22 29 29 3b 0a 20 20 66 6f 72 28 69 3d  *\n"));.  for(i=
30804 69 46 72 6f 6d 3d 30 2c 20 70 4c 65 76 65 6c 3d  iFrom=0, pLevel=
30805 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61  pWInfo->a; i<pTa
30806 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
30807 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20  , pLevel++){.   
30808 20 57 68 65 72 65 43 6f 73 74 20 62 65 73 74 50   WhereCost bestP
30809 6c 61 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  lan;         /* 
3080a 4d 6f 73 74 20 65 66 66 69 63 69 65 6e 74 20 70  Most efficient p
3080b 6c 61 6e 20 73 65 65 6e 20 73 6f 20 66 61 72 20  lan seen so far 
3080c 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  */.    Index *pI
3080d 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
3080e 20 20 20 2f 2a 20 49 6e 64 65 78 20 66 6f 72 20     /* Index for 
3080f 46 52 4f 4d 20 74 61 62 6c 65 20 61 74 20 70 54  FROM table at pT
30810 61 62 49 74 65 6d 20 2a 2f 0a 20 20 20 20 69 6e  abItem */.    in
30811 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  t j;            
30812 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
30813 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 46 52   looping over FR
30814 4f 4d 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20  OM tables */.   
30815 20 69 6e 74 20 62 65 73 74 4a 20 3d 20 2d 31 3b   int bestJ = -1;
30816 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
30817 54 68 65 20 76 61 6c 75 65 20 6f 66 20 6a 20 2a  The value of j *
30818 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 3b  /.    Bitmask m;
30819 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3081a 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 76 61 6c    /* Bitmask val
3081b 75 65 20 66 6f 72 20 6a 20 6f 72 20 62 65 73 74  ue for j or best
3081c 4a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 73 4f  J */.    int isO
3081d 70 74 69 6d 61 6c 3b 20 20 20 20 20 20 20 20 20  ptimal;         
3081e 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
3081f 20 66 6f 72 20 6f 70 74 69 6d 61 6c 2f 6e 6f 6e   for optimal/non
30820 2d 6f 70 74 69 6d 61 6c 20 73 65 61 72 63 68 20  -optimal search 
30821 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  */..    memset(&
30822 62 65 73 74 50 6c 61 6e 2c 20 30 2c 20 73 69 7a  bestPlan, 0, siz
30823 65 6f 66 28 62 65 73 74 50 6c 61 6e 29 29 3b 0a  eof(bestPlan));.
30824 20 20 20 20 62 65 73 74 50 6c 61 6e 2e 72 43 6f      bestPlan.rCo
30825 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f  st = SQLITE_BIG_
30826 44 42 4c 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 6f  DBL;..    /* Loo
30827 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 72 65  p through the re
30828 6d 61 69 6e 69 6e 67 20 65 6e 74 72 69 65 73 20  maining entries 
30829 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
3082a 73 65 20 74 6f 20 66 69 6e 64 20 74 68 65 0a 20  se to find the. 
3082b 20 20 20 2a 2a 20 6e 65 78 74 20 6e 65 73 74 65     ** next neste
3082c 64 20 6c 6f 6f 70 2e 20 54 68 65 20 46 52 4f 4d  d loop. The FROM
3082d 20 63 6c 61 75 73 65 20 65 6e 74 72 69 65 73 20   clause entries 
3082e 6d 61 79 20 62 65 20 69 74 65 72 61 74 65 64 20  may be iterated 
3082f 74 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 65  through.    ** e
30830 69 74 68 65 72 20 6f 6e 63 65 20 6f 72 20 74 77  ither once or tw
30831 69 63 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20  ice. .    **.   
30832 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 69 74   ** The first it
30833 65 72 61 74 69 6f 6e 2c 20 77 68 69 63 68 20 69  eration, which i
30834 73 20 61 6c 77 61 79 73 20 70 65 72 66 6f 72 6d  s always perform
30835 65 64 2c 20 73 65 61 72 63 68 65 73 20 66 6f 72  ed, searches for
30836 20 74 68 65 0a 20 20 20 20 2a 2a 20 46 52 4f 4d   the.    ** FROM
30837 20 63 6c 61 75 73 65 20 65 6e 74 72 79 20 74 68   clause entry th
30838 61 74 20 70 65 72 6d 69 74 73 20 74 68 65 20 6c  at permits the l
30839 6f 77 65 73 74 2d 63 6f 73 74 2c 20 22 6f 70 74  owest-cost, "opt
3083a 69 6d 61 6c 22 20 73 63 61 6e 2e 20 49 6e 0a 20  imal" scan. In. 
3083b 20 20 20 2a 2a 20 74 68 69 73 20 63 6f 6e 74 65     ** this conte
3083c 78 74 20 61 6e 20 6f 70 74 69 6d 61 6c 20 73 63  xt an optimal sc
3083d 61 6e 20 69 73 20 6f 6e 65 20 74 68 61 74 20 75  an is one that u
3083e 73 65 73 20 74 68 65 20 73 61 6d 65 20 73 74 72  ses the same str
3083f 61 74 65 67 79 0a 20 20 20 20 2a 2a 20 66 6f 72  ategy.    ** for
30840 20 74 68 65 20 67 69 76 65 6e 20 46 52 4f 4d 20   the given FROM 
30841 63 6c 61 75 73 65 20 65 6e 74 72 79 20 61 73 20  clause entry as 
30842 77 6f 75 6c 64 20 62 65 20 73 65 6c 65 63 74 65  would be selecte
30843 64 20 69 66 20 74 68 65 20 65 6e 74 72 79 0a 20  d if the entry. 
30844 20 20 20 2a 2a 20 77 65 72 65 20 75 73 65 64 20     ** were used 
30845 61 73 20 74 68 65 20 69 6e 6e 65 72 6d 6f 73 74  as the innermost
30846 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2e 20 20 49   nested loop.  I
30847 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 61  n other words, a
30848 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 69 73   table.    ** is
30849 20 63 68 6f 73 65 6e 20 73 75 63 68 20 74 68 61   chosen such tha
3084a 74 20 74 68 65 20 63 6f 73 74 20 6f 66 20 72 75  t the cost of ru
3084b 6e 6e 69 6e 67 20 74 68 61 74 20 74 61 62 6c 65  nning that table
3084c 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 64 75 63   cannot be reduc
3084d 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 77 61 69  ed.    ** by wai
3084e 74 69 6e 67 20 66 6f 72 20 6f 74 68 65 72 20 74  ting for other t
3084f 61 62 6c 65 73 20 74 6f 20 72 75 6e 20 66 69 72  ables to run fir
30850 73 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  st..    **.    *
30851 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 69 74 65  * The second ite
30852 72 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 70  ration is only p
30853 65 72 66 6f 72 6d 65 64 20 69 66 20 6e 6f 20 6f  erformed if no o
30854 70 74 69 6d 61 6c 20 73 63 61 6e 20 73 74 72 61  ptimal scan stra
30855 74 65 67 69 65 73 0a 20 20 20 20 2a 2a 20 77 65  tegies.    ** we
30856 72 65 20 66 6f 75 6e 64 20 62 79 20 74 68 65 20  re found by the 
30857 66 69 72 73 74 2e 20 54 68 69 73 20 69 74 65 72  first. This iter
30858 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ation is used to
30859 20 73 65 61 72 63 68 20 66 6f 72 20 74 68 65 0a   search for the.
3085a 20 20 20 20 2a 2a 20 6c 6f 77 65 73 74 20 63 6f      ** lowest co
3085b 73 74 20 73 63 61 6e 20 6f 76 65 72 61 6c 6c 2e  st scan overall.
3085c 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 50  .    **.    ** P
3085d 72 65 76 69 6f 75 73 20 76 65 72 73 69 6f 6e 73  revious versions
3085e 20 6f 66 20 53 51 4c 69 74 65 20 70 65 72 66 6f   of SQLite perfo
3085f 72 6d 65 64 20 6f 6e 6c 79 20 74 68 65 20 73 65  rmed only the se
30860 63 6f 6e 64 20 69 74 65 72 61 74 69 6f 6e 20 2d  cond iteration -
30861 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78 74  .    ** the next
30862 20 6f 75 74 65 72 6d 6f 73 74 20 6c 6f 6f 70 20   outermost loop 
30863 77 61 73 20 61 6c 77 61 79 73 20 74 68 61 74 20  was always that 
30864 77 69 74 68 20 74 68 65 20 6c 6f 77 65 73 74 20  with the lowest 
30865 6f 76 65 72 61 6c 6c 0a 20 20 20 20 2a 2a 20 63  overall.    ** c
30866 6f 73 74 2e 20 48 6f 77 65 76 65 72 2c 20 74 68  ost. However, th
30867 69 73 20 6d 65 61 6e 74 20 74 68 61 74 20 53 51  is meant that SQ
30868 4c 69 74 65 20 63 6f 75 6c 64 20 73 65 6c 65 63  Lite could selec
30869 74 20 74 68 65 20 77 72 6f 6e 67 20 70 6c 61 6e  t the wrong plan
3086a 0a 20 20 20 20 2a 2a 20 66 6f 72 20 73 63 72 69  .    ** for scri
3086b 70 74 73 20 73 75 63 68 20 61 73 20 74 68 65 20  pts such as the 
3086c 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20 2a  following:.    *
3086d 2a 20 20 20 0a 20 20 20 20 2a 2a 20 20 20 43 52  *   .    **   CR
3086e 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c  EATE TABLE t1(a,
3086f 20 62 29 3b 20 0a 20 20 20 20 2a 2a 20 20 20 43   b); .    **   C
30870 52 45 41 54 45 20 54 41 42 4c 45 20 74 32 28 63  REATE TABLE t2(c
30871 2c 20 64 29 3b 0a 20 20 20 20 2a 2a 20 20 20 53  , d);.    **   S
30872 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 32 2c  ELECT * FROM t2,
30873 20 74 31 20 57 48 45 52 45 20 74 32 2e 72 6f 77   t1 WHERE t2.row
30874 69 64 20 3d 20 74 31 2e 61 3b 0a 20 20 20 20 2a  id = t1.a;.    *
30875 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 62 65 73  *.    ** The bes
30876 74 20 73 74 72 61 74 65 67 79 20 69 73 20 74 6f  t strategy is to
30877 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
30878 20 74 61 62 6c 65 20 74 31 20 66 69 72 73 74 2e   table t1 first.
30879 20 48 6f 77 65 76 65 72 20 69 74 0a 20 20 20 20   However it.    
3087a 2a 2a 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  ** is not possib
3087b 6c 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  le to determine 
3087c 74 68 69 73 20 77 69 74 68 20 61 20 73 69 6d 70  this with a simp
3087d 6c 65 20 67 72 65 65 64 79 20 61 6c 67 6f 72 69  le greedy algori
3087e 74 68 6d 2e 0a 20 20 20 20 2a 2a 20 48 6f 77 65  thm..    ** Howe
3087f 76 65 72 2c 20 73 69 6e 63 65 20 74 68 65 20 63  ver, since the c
30880 6f 73 74 20 6f 66 20 61 20 6c 69 6e 65 61 72 20  ost of a linear 
30881 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74 61 62  scan through tab
30882 6c 65 20 74 32 20 69 73 20 74 68 65 20 73 61 6d  le t2 is the sam
30883 65 20 0a 20 20 20 20 2a 2a 20 61 73 20 74 68 65  e .    ** as the
30884 20 63 6f 73 74 20 6f 66 20 61 20 6c 69 6e 65 61   cost of a linea
30885 72 20 73 63 61 6e 20 74 68 72 6f 75 67 68 20 74  r scan through t
30886 61 62 6c 65 20 74 31 2c 20 61 20 73 69 6d 70 6c  able t1, a simpl
30887 65 20 67 72 65 65 64 79 20 0a 20 20 20 20 2a 2a  e greedy .    **
30888 20 61 6c 67 6f 72 69 74 68 6d 20 6d 61 79 20 63   algorithm may c
30889 68 6f 6f 73 65 20 74 6f 20 75 73 65 20 74 32 20  hoose to use t2 
3088a 66 6f 72 20 74 68 65 20 6f 75 74 65 72 20 6c 6f  for the outer lo
3088b 6f 70 2c 20 77 68 69 63 68 20 69 73 20 61 20 6d  op, which is a m
3088c 75 63 68 0a 20 20 20 20 2a 2a 20 63 6f 73 74 6c  uch.    ** costl
3088d 69 65 72 20 61 70 70 72 6f 61 63 68 2e 0a 20 20  ier approach..  
3088e 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 73 4f    */.    for(isO
3088f 70 74 69 6d 61 6c 3d 31 3b 20 69 73 4f 70 74 69  ptimal=1; isOpti
30890 6d 61 6c 3e 3d 30 20 26 26 20 62 65 73 74 4a 3c  mal>=0 && bestJ<
30891 30 3b 20 69 73 4f 70 74 69 6d 61 6c 2d 2d 29 7b  0; isOptimal--){
30892 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 6d  .      Bitmask m
30893 61 73 6b 20 3d 20 28 69 73 4f 70 74 69 6d 61 6c  ask = (isOptimal
30894 20 3f 20 30 20 3a 20 6e 6f 74 52 65 61 64 79 29   ? 0 : notReady)
30895 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
30896 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2d  (pTabList->nSrc-
30897 69 46 72 6f 6d 29 3e 31 20 7c 7c 20 69 73 4f 70  iFrom)>1 || isOp
30898 74 69 6d 61 6c 20 29 3b 0a 20 20 20 20 20 20 66  timal );.      f
30899 6f 72 28 6a 3d 69 46 72 6f 6d 2c 20 70 54 61 62  or(j=iFrom, pTab
3089a 49 74 65 6d 3d 26 70 54 61 62 4c 69 73 74 2d 3e  Item=&pTabList->
3089b 61 5b 6a 5d 3b 20 6a 3c 70 54 61 62 4c 69 73 74  a[j]; j<pTabList
3089c 2d 3e 6e 53 72 63 3b 20 6a 2b 2b 2c 20 70 54 61  ->nSrc; j++, pTa
3089d 62 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  bItem++){.      
3089e 20 20 69 6e 74 20 64 6f 4e 6f 74 52 65 6f 72 64    int doNotReord
3089f 65 72 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69  er;    /* True i
308a0 66 20 74 68 69 73 20 74 61 62 6c 65 20 73 68 6f  f this table sho
308a1 75 6c 64 20 6e 6f 74 20 62 65 20 72 65 6f 72 64  uld not be reord
308a2 65 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  ered */.        
308a3 57 68 65 72 65 43 6f 73 74 20 73 43 6f 73 74 3b  WhereCost sCost;
308a4 20 20 20 20 20 2f 2a 20 43 6f 73 74 20 69 6e 66       /* Cost inf
308a5 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 62 65  ormation from be
308a6 73 74 5b 56 69 72 74 75 61 6c 5d 49 6e 64 65 78  st[Virtual]Index
308a7 28 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 45 78  () */.        Ex
308a8 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
308a9 3b 20 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 63  ;  /* ORDER BY c
308aa 6c 61 75 73 65 20 66 6f 72 20 69 6e 64 65 78 20  lause for index 
308ab 74 6f 20 6f 70 74 69 6d 69 7a 65 20 2a 2f 0a 20  to optimize */. 
308ac 20 0a 20 20 20 20 20 20 20 20 64 6f 4e 6f 74 52   .        doNotR
308ad 65 6f 72 64 65 72 20 3d 20 20 28 70 54 61 62 49  eorder =  (pTabI
308ae 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20  tem->jointype & 
308af 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f 43 52 4f 53  (JT_LEFT|JT_CROS
308b0 53 29 29 21 3d 30 3b 0a 20 20 20 20 20 20 20 20  S))!=0;.        
308b1 69 66 28 20 6a 21 3d 69 46 72 6f 6d 20 26 26 20  if( j!=iFrom && 
308b2 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29 20 62  doNotReorder ) b
308b3 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 6d 20  reak;.        m 
308b4 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53  = getMask(pMaskS
308b5 65 74 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43  et, pTabItem->iC
308b6 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20  ursor);.        
308b7 69 66 28 20 28 6d 20 26 20 6e 6f 74 52 65 61 64  if( (m & notRead
308b8 79 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  y)==0 ){.       
308b9 20 20 20 69 66 28 20 6a 3d 3d 69 46 72 6f 6d 20     if( j==iFrom 
308ba 29 20 69 46 72 6f 6d 2b 2b 3b 0a 20 20 20 20 20  ) iFrom++;.     
308bb 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
308bc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
308bd 20 70 4f 72 64 65 72 42 79 20 3d 20 28 28 69 3d   pOrderBy = ((i=
308be 3d 30 20 26 26 20 70 70 4f 72 64 65 72 42 79 20  =0 && ppOrderBy 
308bf 29 3f 2a 70 70 4f 72 64 65 72 42 79 3a 30 29 3b  )?*ppOrderBy:0);
308c0 0a 20 20 0a 20 20 20 20 20 20 20 20 61 73 73 65  .  .        asse
308c1 72 74 28 20 70 54 61 62 49 74 65 6d 2d 3e 70 54  rt( pTabItem->pT
308c2 61 62 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  ab );.#ifndef SQ
308c3 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
308c4 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 20 20 69  LTABLE.        i
308c5 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
308c6 62 49 74 65 6d 2d 3e 70 54 61 62 29 20 29 7b 0a  bItem->pTab) ){.
308c7 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
308c8 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a 70  3_index_info **p
308c9 70 20 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 6a  p = &pWInfo->a[j
308ca 5d 2e 70 49 64 78 49 6e 66 6f 3b 0a 20 20 20 20  ].pIdxInfo;.    
308cb 20 20 20 20 20 20 62 65 73 74 56 69 72 74 75 61        bestVirtua
308cc 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  lIndex(pParse, p
308cd 57 43 2c 20 70 54 61 62 49 74 65 6d 2c 20 6d 61  WC, pTabItem, ma
308ce 73 6b 2c 20 70 4f 72 64 65 72 42 79 2c 20 26 73  sk, pOrderBy, &s
308cf 43 6f 73 74 2c 20 70 70 29 3b 0a 20 20 20 20 20  Cost, pp);.     
308d0 20 20 20 7d 65 6c 73 65 20 0a 23 65 6e 64 69 66     }else .#endif
308d1 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20  .        {.     
308d2 20 20 20 20 20 62 65 73 74 42 74 72 65 65 49 6e       bestBtreeIn
308d3 64 65 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c  dex(pParse, pWC,
308d4 20 70 54 61 62 49 74 65 6d 2c 20 6d 61 73 6b 2c   pTabItem, mask,
308d5 20 70 4f 72 64 65 72 42 79 2c 20 26 73 43 6f 73   pOrderBy, &sCos
308d6 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  t);.        }.  
308d7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 73        assert( is
308d8 4f 70 74 69 6d 61 6c 20 7c 7c 20 28 73 43 6f 73  Optimal || (sCos
308d9 74 2e 75 73 65 64 26 6e 6f 74 52 65 61 64 79 29  t.used&notReady)
308da 3d 3d 30 20 29 3b 0a 0a 20 20 20 20 20 20 20 20  ==0 );..        
308db 69 66 28 20 28 73 43 6f 73 74 2e 75 73 65 64 26  if( (sCost.used&
308dc 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a 20 20 20  notReady)==0.   
308dd 20 20 20 20 20 20 26 26 20 28 6a 3d 3d 69 46 72        && (j==iFr
308de 6f 6d 20 7c 7c 20 73 43 6f 73 74 2e 72 43 6f 73  om || sCost.rCos
308df 74 3c 62 65 73 74 50 6c 61 6e 2e 72 43 6f 73 74  t<bestPlan.rCost
308e0 29 20 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20  ) .        ){.  
308e1 20 20 20 20 20 20 20 20 62 65 73 74 50 6c 61 6e          bestPlan
308e2 20 3d 20 73 43 6f 73 74 3b 0a 20 20 20 20 20 20   = sCost;.      
308e3 20 20 20 20 62 65 73 74 4a 20 3d 20 6a 3b 0a 20      bestJ = j;. 
308e4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
308e5 20 69 66 28 20 64 6f 4e 6f 74 52 65 6f 72 64 65   if( doNotReorde
308e6 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  r ) break;.     
308e7 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73   }.    }.    ass
308e8 65 72 74 28 20 62 65 73 74 4a 3e 3d 30 20 29 3b  ert( bestJ>=0 );
308e9 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 6f 74  .    assert( not
308ea 52 65 61 64 79 20 26 20 67 65 74 4d 61 73 6b 28  Ready & getMask(
308eb 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69  pMaskSet, pTabLi
308ec 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75  st->a[bestJ].iCu
308ed 72 73 6f 72 29 20 29 3b 0a 20 20 20 20 57 48 45  rsor) );.    WHE
308ee 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f 70  RETRACE(("*** Op
308ef 74 69 6d 69 7a 65 72 20 73 65 6c 65 63 74 73 20  timizer selects 
308f0 74 61 62 6c 65 20 25 64 20 66 6f 72 20 6c 6f 6f  table %d for loo
308f1 70 20 25 64 5c 6e 22 2c 20 62 65 73 74 4a 2c 0a  p %d\n", bestJ,.
308f2 20 20 20 20 20 20 20 20 20 20 20 70 4c 65 76 65             pLeve
308f3 6c 2d 70 57 49 6e 66 6f 2d 3e 61 29 29 3b 0a 20  l-pWInfo->a));. 
308f4 20 20 20 69 66 28 20 28 62 65 73 74 50 6c 61 6e     if( (bestPlan
308f5 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  .plan.wsFlags & 
308f6 57 48 45 52 45 5f 4f 52 44 45 52 42 59 29 21 3d  WHERE_ORDERBY)!=
308f7 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 4f 72  0 ){.      *ppOr
308f8 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 7d  derBy = 0;.    }
308f9 0a 20 20 20 20 61 6e 64 46 6c 61 67 73 20 26 3d  .    andFlags &=
308fa 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77   bestPlan.plan.w
308fb 73 46 6c 61 67 73 3b 0a 20 20 20 20 70 4c 65 76  sFlags;.    pLev
308fc 65 6c 2d 3e 70 6c 61 6e 20 3d 20 62 65 73 74 50  el->plan = bestP
308fd 6c 61 6e 2e 70 6c 61 6e 3b 0a 20 20 20 20 69 66  lan.plan;.    if
308fe 28 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e  ( bestPlan.plan.
308ff 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
30900 49 4e 44 45 58 45 44 20 29 7b 0a 20 20 20 20 20  INDEXED ){.     
30901 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
30902 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
30903 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  +;.    }else{.  
30904 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78      pLevel->iIdx
30905 43 75 72 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a  Cur = -1;.    }.
30906 20 20 20 20 6e 6f 74 52 65 61 64 79 20 26 3d 20      notReady &= 
30907 7e 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65  ~getMask(pMaskSe
30908 74 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62  t, pTabList->a[b
30909 65 73 74 4a 5d 2e 69 43 75 72 73 6f 72 29 3b 0a  estJ].iCursor);.
3090a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f      pLevel->iFro
3090b 6d 20 3d 20 28 75 38 29 62 65 73 74 4a 3b 0a 0a  m = (u8)bestJ;..
3090c 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61      /* Check tha
3090d 74 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 73  t if the table s
3090e 63 61 6e 6e 65 64 20 62 79 20 74 68 69 73 20 6c  canned by this l
3090f 6f 6f 70 20 69 74 65 72 61 74 69 6f 6e 20 68 61  oop iteration ha
30910 64 20 61 6e 0a 20 20 20 20 2a 2a 20 49 4e 44 45  d an.    ** INDE
30911 58 45 44 20 42 59 20 63 6c 61 75 73 65 20 61 74  XED BY clause at
30912 74 61 63 68 65 64 20 74 6f 20 69 74 2c 20 74 68  tached to it, th
30913 61 74 20 74 68 65 20 6e 61 6d 65 64 20 69 6e 64  at the named ind
30914 65 78 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20  ex is being.    
30915 2a 2a 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  ** used for the 
30916 73 63 61 6e 2e 20 49 66 20 6e 6f 74 2c 20 74 68  scan. If not, th
30917 65 6e 20 71 75 65 72 79 20 63 6f 6d 70 69 6c 61  en query compila
30918 74 69 6f 6e 20 68 61 73 20 66 61 69 6c 65 64 2e  tion has failed.
30919 0a 20 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 61  .    ** Return a
3091a 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a  n error..    */.
3091b 20 20 20 20 70 49 64 78 20 3d 20 70 54 61 62 4c      pIdx = pTabL
3091c 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 70 49  ist->a[bestJ].pI
3091d 6e 64 65 78 3b 0a 20 20 20 20 69 66 28 20 70 49  ndex;.    if( pI
3091e 64 78 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  dx ){.      if( 
3091f 28 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77  (bestPlan.plan.w
30920 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
30921 4e 44 45 58 45 44 29 3d 3d 30 20 29 7b 0a 20 20  NDEXED)==0 ){.  
30922 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
30923 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63  orMsg(pParse, "c
30924 61 6e 6e 6f 74 20 75 73 65 20 69 6e 64 65 78 3a  annot use index:
30925 20 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d   %s", pIdx->zNam
30926 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  e);.        goto
30927 20 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72   whereBeginError
30928 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
30929 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20         /* If an 
3092a 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
3092b 65 20 69 73 20 75 73 65 64 2c 20 74 68 65 20 62  e is used, the b
3092c 65 73 74 49 6e 64 65 78 28 29 20 66 75 6e 63 74  estIndex() funct
3092d 69 6f 6e 20 69 73 0a 20 20 20 20 20 20 20 20 2a  ion is.        *
3092e 2a 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  * guaranteed to 
3092f 66 69 6e 64 20 74 68 65 20 69 6e 64 65 78 20 73  find the index s
30930 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
30931 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73  INDEXED BY claus
30932 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66 20  e.        ** if 
30933 69 74 20 66 69 6e 64 20 61 6e 20 69 6e 64 65 78  it find an index
30934 20 61 74 20 61 6c 6c 2e 20 2a 2f 0a 20 20 20 20   at all. */.    
30935 20 20 20 20 61 73 73 65 72 74 28 20 62 65 73 74      assert( best
30936 50 6c 61 6e 2e 70 6c 61 6e 2e 75 2e 70 49 64 78  Plan.plan.u.pIdx
30937 3d 3d 70 49 64 78 20 29 3b 0a 20 20 20 20 20 20  ==pIdx );.      
30938 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 57 48  }.    }.  }.  WH
30939 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f  ERETRACE(("*** O
3093a 70 74 69 6d 69 7a 65 72 20 46 69 6e 69 73 68 65  ptimizer Finishe
3093b 64 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20 69 66  d ***\n"));.  if
3093c 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
3093d 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
3093e 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 77  ed ){.    goto w
3093f 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a  hereBeginError;.
30940 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
30941 20 74 6f 74 61 6c 20 71 75 65 72 79 20 6f 6e 6c   total query onl
30942 79 20 73 65 6c 65 63 74 73 20 61 20 73 69 6e 67  y selects a sing
30943 6c 65 20 72 6f 77 2c 20 74 68 65 6e 20 74 68 65  le row, then the
30944 20 4f 52 44 45 52 20 42 59 0a 20 20 2a 2a 20 63   ORDER BY.  ** c
30945 6c 61 75 73 65 20 69 73 20 69 72 72 65 6c 65 76  lause is irrelev
30946 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ant..  */.  if( 
30947 28 61 6e 64 46 6c 61 67 73 20 26 20 57 48 45 52  (andFlags & WHER
30948 45 5f 55 4e 49 51 55 45 29 21 3d 30 20 26 26 20  E_UNIQUE)!=0 && 
30949 70 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  ppOrderBy ){.   
3094a 20 2a 70 70 4f 72 64 65 72 42 79 20 3d 20 30 3b   *ppOrderBy = 0;
3094b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
3094c 65 20 63 61 6c 6c 65 72 20 69 73 20 61 6e 20 55  e caller is an U
3094d 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20  PDATE or DELETE 
3094e 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 69  statement that i
3094f 73 20 72 65 71 75 65 73 74 69 6e 67 0a 20 20 2a  s requesting.  *
30950 2a 20 74 6f 20 75 73 65 20 61 20 6f 6e 65 2d 70  * to use a one-p
30951 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 2c 20 64  ass algorithm, d
30952 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 69 73  etermine if this
30953 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e   is appropriate.
30954 0a 20 20 2a 2a 20 54 68 65 20 6f 6e 65 2d 70 61  .  ** The one-pa
30955 73 73 20 61 6c 67 6f 72 69 74 68 6d 20 6f 6e 6c  ss algorithm onl
30956 79 20 77 6f 72 6b 73 20 69 66 20 74 68 65 20 57  y works if the W
30957 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 73  HERE clause cons
30958 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 74 68 65  traints.  ** the
30959 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 70   statement to up
3095a 64 61 74 65 20 61 20 73 69 6e 67 6c 65 20 72 6f  date a single ro
3095b 77 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  w..  */.  assert
3095c 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20  ( (wctrlFlags & 
3095d 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44 45  WHERE_ONEPASS_DE
3095e 53 49 52 45 44 29 3d 3d 30 20 7c 7c 20 70 57 49  SIRED)==0 || pWI
3095f 6e 66 6f 2d 3e 6e 4c 65 76 65 6c 3d 3d 31 20 29  nfo->nLevel==1 )
30960 3b 0a 20 20 69 66 28 20 28 77 63 74 72 6c 46 6c  ;.  if( (wctrlFl
30961 61 67 73 20 26 20 57 48 45 52 45 5f 4f 4e 45 50  ags & WHERE_ONEP
30962 41 53 53 5f 44 45 53 49 52 45 44 29 21 3d 30 20  ASS_DESIRED)!=0 
30963 26 26 20 28 61 6e 64 46 6c 61 67 73 20 26 20 57  && (andFlags & W
30964 48 45 52 45 5f 55 4e 49 51 55 45 29 21 3d 30 20  HERE_UNIQUE)!=0 
30965 29 7b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 6f  ){.    pWInfo->o
30966 6b 4f 6e 65 50 61 73 73 20 3d 20 31 3b 0a 20 20  kOnePass = 1;.  
30967 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 30 5d 2e 70    pWInfo->a[0].p
30968 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 3d 20 7e  lan.wsFlags &= ~
30969 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0a  WHERE_IDX_ONLY;.
3096a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61    }..  /* Open a
3096b 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  ll tables in the
3096c 20 70 54 61 62 4c 69 73 74 20 61 6e 64 20 61 6e   pTabList and an
3096d 79 20 69 6e 64 69 63 65 73 20 73 65 6c 65 63 74  y indices select
3096e 65 64 20 66 6f 72 0a 20 20 2a 2a 20 73 65 61 72  ed for.  ** sear
3096f 63 68 69 6e 67 20 74 68 6f 73 65 20 74 61 62 6c  ching those tabl
30970 65 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  es..  */.  sqlit
30971 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65  e3CodeVerifySche
30972 6d 61 28 70 50 61 72 73 65 2c 20 2d 31 29 3b 20  ma(pParse, -1); 
30973 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63 6f  /* Insert the co
30974 6f 6b 69 65 20 76 65 72 69 66 69 65 72 20 47 6f  okie verifier Go
30975 74 6f 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c  to */.  for(i=0,
30976 20 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e   pLevel=pWInfo->
30977 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  a; i<pTabList->n
30978 53 72 63 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c  Src; i++, pLevel
30979 2b 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  ++){.    Table *
3097a 70 54 61 62 3b 20 20 20 20 20 2f 2a 20 54 61 62  pTab;     /* Tab
3097b 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20  le to open */.  
3097c 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
3097d 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64     /* Index of d
3097e 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
3097f 6e 67 20 74 61 62 6c 65 2f 69 6e 64 65 78 20 2a  ng table/index *
30980 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
30981 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
30982 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65     if( pParse->e
30983 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20 20  xplain==2 ){.   
30984 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a 20     char *zMsg;. 
30985 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c       struct SrcL
30986 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
30987 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70  = &pTabList->a[p
30988 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20  Level->iFrom];. 
30989 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
3098a 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
3098b 54 41 42 4c 45 20 25 73 22 2c 20 70 49 74 65 6d  TABLE %s", pItem
3098c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
3098d 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  if( pItem->zAlia
3098e 73 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73  s ){.        zMs
3098f 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65  g = sqlite3MAppe
30990 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25  ndf(db, zMsg, "%
30991 73 20 41 53 20 25 73 22 2c 20 7a 4d 73 67 2c 20  s AS %s", zMsg, 
30992 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a  pItem->zAlias);.
30993 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
30994 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ( (pLevel->plan.
30995 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
30996 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20  INDEXED)!=0 ){. 
30997 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71         zMsg = sq
30998 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62  lite3MAppendf(db
30999 2c 20 7a 4d 73 67 2c 20 22 25 73 20 57 49 54 48  , zMsg, "%s WITH
3099a 20 49 4e 44 45 58 20 25 73 22 2c 0a 20 20 20 20   INDEX %s",.    
3099b 20 20 20 20 20 20 20 7a 4d 73 67 2c 20 70 4c 65         zMsg, pLe
3099c 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78  vel->plan.u.pIdx
3099d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
3099e 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c  }else if( pLevel
3099f 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
309a0 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 20   WHERE_MULTI_OR 
309a1 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20  ){.        zMsg 
309a2 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64  = sqlite3MAppend
309a3 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20  f(db, zMsg, "%s 
309a4 56 49 41 20 4d 55 4c 54 49 2d 49 4e 44 45 58 20  VIA MULTI-INDEX 
309a5 55 4e 49 4f 4e 22 2c 20 7a 4d 73 67 29 3b 0a 20  UNION", zMsg);. 
309a6 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
309a7 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
309a8 61 67 73 20 26 20 28 57 48 45 52 45 5f 52 4f 57  ags & (WHERE_ROW
309a9 49 44 5f 45 51 7c 57 48 45 52 45 5f 52 4f 57 49  ID_EQ|WHERE_ROWI
309aa 44 5f 52 41 4e 47 45 29 20 29 7b 0a 20 20 20 20  D_RANGE) ){.    
309ab 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74      zMsg = sqlit
309ac 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a  e3MAppendf(db, z
309ad 4d 73 67 2c 20 22 25 73 20 55 53 49 4e 47 20 50  Msg, "%s USING P
309ae 52 49 4d 41 52 59 20 4b 45 59 22 2c 20 7a 4d 73  RIMARY KEY", zMs
309af 67 29 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e  g);.      }.#ifn
309b0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
309b1 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20  VIRTUALTABLE.   
309b2 20 20 20 65 6c 73 65 20 69 66 28 20 28 70 4c 65     else if( (pLe
309b3 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
309b4 73 20 26 20 57 48 45 52 45 5f 56 49 52 54 55 41  s & WHERE_VIRTUA
309b5 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b 0a 20 20  LTABLE)!=0 ){.  
309b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
309b7 64 65 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49  dex_info *pVtabI
309b8 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61  dx = pLevel->pla
309b9 6e 2e 75 2e 70 56 74 61 62 49 64 78 3b 0a 20 20  n.u.pVtabIdx;.  
309ba 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c        zMsg = sql
309bb 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c  ite3MAppendf(db,
309bc 20 7a 4d 73 67 2c 20 22 25 73 20 56 49 52 54 55   zMsg, "%s VIRTU
309bd 41 4c 20 54 41 42 4c 45 20 49 4e 44 45 58 20 25  AL TABLE INDEX %
309be 64 3a 25 73 22 2c 20 7a 4d 73 67 2c 0a 20 20 20  d:%s", zMsg,.   
309bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
309c0 20 70 56 74 61 62 49 64 78 2d 3e 69 64 78 4e 75   pVtabIdx->idxNu
309c1 6d 2c 20 70 56 74 61 62 49 64 78 2d 3e 69 64 78  m, pVtabIdx->idx
309c2 53 74 72 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  Str);.      }.#e
309c3 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 70  ndif.      if( p
309c4 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
309c5 61 67 73 20 26 20 57 48 45 52 45 5f 4f 52 44 45  ags & WHERE_ORDE
309c6 52 42 59 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  RBY ){.        z
309c7 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70  Msg = sqlite3MAp
309c8 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20  pendf(db, zMsg, 
309c9 22 25 73 20 4f 52 44 45 52 20 42 59 22 2c 20 7a  "%s ORDER BY", z
309ca 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Msg);.      }.  
309cb 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
309cc 64 64 4f 70 34 28 76 2c 20 4f 50 5f 45 78 70 6c  ddOp4(v, OP_Expl
309cd 61 69 6e 2c 20 69 2c 20 70 4c 65 76 65 6c 2d 3e  ain, i, pLevel->
309ce 69 46 72 6f 6d 2c 20 30 2c 20 7a 4d 73 67 2c 20  iFrom, 0, zMsg, 
309cf 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  P4_DYNAMIC);.   
309d0 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
309d1 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
309d2 20 2a 2f 0a 20 20 20 20 70 54 61 62 49 74 65 6d   */.    pTabItem
309d3 20 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b   = &pTabList->a[
309d4 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a  pLevel->iFrom];.
309d5 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 49      pTab = pTabI
309d6 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69  tem->pTab;.    i
309d7 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
309d8 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
309d9 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
309da 20 20 69 66 28 20 28 70 54 61 62 2d 3e 74 61 62    if( (pTab->tab
309db 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68 65 6d  Flags & TF_Ephem
309dc 65 72 61 6c 29 21 3d 30 20 7c 7c 20 70 54 61 62  eral)!=0 || pTab
309dd 2d 3e 70 53 65 6c 65 63 74 20 29 20 63 6f 6e 74  ->pSelect ) cont
309de 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66 20 53 51  inue;.#ifndef SQ
309df 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
309e0 4c 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 28  LTABLE.    if( (
309e1 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
309e2 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56 49 52  lags & WHERE_VIR
309e3 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20 29 7b  TUALTABLE)!=0 ){
309e4 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
309e5 72 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e 73  r *pVTab = (cons
309e6 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
309e7 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54  GetVTable(db, pT
309e8 61 62 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 69  ab);.      int i
309e9 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e  Cur = pTabItem->
309ea 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 73  iCursor;.      s
309eb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
309ec 28 76 2c 20 4f 50 5f 56 4f 70 65 6e 2c 20 69 43  (v, OP_VOpen, iC
309ed 75 72 2c 20 30 2c 20 30 2c 20 70 56 54 61 62 2c  ur, 0, 0, pVTab,
309ee 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 7d   P4_VTAB);.    }
309ef 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
309f0 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  if( (pLevel->pla
309f1 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
309f2 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30 0a 20  E_IDX_ONLY)==0. 
309f3 20 20 20 20 20 20 20 20 26 26 20 28 77 63 74 72          && (wctr
309f4 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 4f  lFlags & WHERE_O
309f5 4d 49 54 5f 4f 50 45 4e 29 3d 3d 30 20 29 7b 0a  MIT_OPEN)==0 ){.
309f6 20 20 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 70        int op = p
309f7 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73  WInfo->okOnePass
309f8 20 3f 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20   ? OP_OpenWrite 
309f9 3a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 3b 0a 20  : OP_OpenRead;. 
309fa 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e       sqlite3Open
309fb 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
309fc 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 2c  abItem->iCursor,
309fd 20 69 44 62 2c 20 70 54 61 62 2c 20 6f 70 29 3b   iDb, pTab, op);
309fe 0a 20 20 20 20 20 20 69 66 28 20 21 70 57 49 6e  .      if( !pWIn
309ff 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 26 26  fo->okOnePass &&
30a00 20 70 54 61 62 2d 3e 6e 43 6f 6c 3c 42 4d 53 20   pTab->nCol<BMS 
30a01 29 7b 0a 20 20 20 20 20 20 20 20 42 69 74 6d 61  ){.        Bitma
30a02 73 6b 20 62 20 3d 20 70 54 61 62 49 74 65 6d 2d  sk b = pTabItem-
30a03 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20 20 20 20  >colUsed;.      
30a04 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 20    int n = 0;.   
30a05 20 20 20 20 20 66 6f 72 28 3b 20 62 3b 20 62 3d       for(; b; b=
30a06 62 3e 3e 31 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20  b>>1, n++){}.   
30a07 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
30a08 43 68 61 6e 67 65 50 34 28 76 2c 20 73 71 6c 69  ChangeP4(v, sqli
30a09 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64  te3VdbeCurrentAd
30a0a 64 72 28 76 29 2d 31 2c 20 0a 20 20 20 20 20 20  dr(v)-1, .      
30a0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30a0c 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 54        SQLITE_INT
30a0d 5f 54 4f 5f 50 54 52 28 6e 29 2c 20 50 34 5f 49  _TO_PTR(n), P4_I
30a0e 4e 54 33 32 29 3b 0a 20 20 20 20 20 20 20 20 61  NT32);.        a
30a0f 73 73 65 72 74 28 20 6e 3c 3d 70 54 61 62 2d 3e  ssert( n<=pTab->
30a10 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 20 20 7d 0a  nCol );.      }.
30a11 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
30a12 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63   sqlite3TableLoc
30a13 6b 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70  k(pParse, iDb, p
30a14 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54  Tab->tnum, 0, pT
30a15 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
30a16 7d 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 54  }.    pLevel->iT
30a17 61 62 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d  abCur = pTabItem
30a18 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 69  ->iCursor;.    i
30a19 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  f( (pLevel->plan
30a1a 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
30a1b 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a  _INDEXED)!=0 ){.
30a1c 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 78        Index *pIx
30a1d 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e   = pLevel->plan.
30a1e 75 2e 70 49 64 78 3b 0a 20 20 20 20 20 20 4b 65  u.pIdx;.      Ke
30a1f 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71  yInfo *pKey = sq
30a20 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66  lite3IndexKeyinf
30a21 6f 28 70 50 61 72 73 65 2c 20 70 49 78 29 3b 0a  o(pParse, pIx);.
30a22 20 20 20 20 20 20 69 6e 74 20 69 49 64 78 43 75        int iIdxCu
30a23 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78  r = pLevel->iIdx
30a24 43 75 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  Cur;.      asser
30a25 74 28 20 70 49 78 2d 3e 70 53 63 68 65 6d 61 3d  t( pIx->pSchema=
30a26 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29  =pTab->pSchema )
30a27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
30a28 69 49 64 78 43 75 72 3e 3d 30 20 29 3b 0a 20 20  iIdxCur>=0 );.  
30a29 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
30a2a 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
30a2b 52 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70  Read, iIdxCur, p
30a2c 49 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20  Ix->tnum, iDb,. 
30a2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30a2e 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
30a2f 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  ey, P4_KEYINFO_H
30a30 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 56  ANDOFF);.      V
30a31 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
30a32 25 73 22 2c 20 70 49 78 2d 3e 7a 4e 61 6d 65 29  %s", pIx->zName)
30a33 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
30a34 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
30a35 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62  hema(pParse, iDb
30a36 29 3b 0a 20 20 7d 0a 20 20 70 57 49 6e 66 6f 2d  );.  }.  pWInfo-
30a37 3e 69 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56  >iTop = sqlite3V
30a38 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
30a39 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  );..  /* Generat
30a3a 65 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 6f  e the code to do
30a3b 20 74 68 65 20 73 65 61 72 63 68 2e 20 20 45 61   the search.  Ea
30a3c 63 68 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20  ch iteration of 
30a3d 74 68 65 20 66 6f 72 0a 20 20 2a 2a 20 6c 6f 6f  the for.  ** loo
30a3e 70 20 62 65 6c 6f 77 20 67 65 6e 65 72 61 74 65  p below generate
30a3f 73 20 63 6f 64 65 20 66 6f 72 20 61 20 73 69 6e  s code for a sin
30a40 67 6c 65 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20  gle nested loop 
30a41 6f 66 20 74 68 65 20 56 4d 0a 20 20 2a 2a 20 70  of the VM.  ** p
30a42 72 6f 67 72 61 6d 2e 0a 20 20 2a 2f 0a 20 20 6e  rogram..  */.  n
30a43 6f 74 52 65 61 64 79 20 3d 20 7e 28 42 69 74 6d  otReady = ~(Bitm
30a44 61 73 6b 29 30 3b 0a 20 20 66 6f 72 28 69 3d 30  ask)0;.  for(i=0
30a45 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
30a46 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f  rc; i++){.    no
30a47 74 52 65 61 64 79 20 3d 20 63 6f 64 65 4f 6e 65  tReady = codeOne
30a48 4c 6f 6f 70 53 74 61 72 74 28 70 57 49 6e 66 6f  LoopStart(pWInfo
30a49 2c 20 69 2c 20 77 63 74 72 6c 46 6c 61 67 73 2c  , i, wctrlFlags,
30a4a 20 6e 6f 74 52 65 61 64 79 29 3b 0a 20 20 20 20   notReady);.    
30a4b 70 57 49 6e 66 6f 2d 3e 69 43 6f 6e 74 69 6e 75  pWInfo->iContinu
30a4c 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d  e = pWInfo->a[i]
30a4d 2e 61 64 64 72 43 6f 6e 74 3b 0a 20 20 7d 0a 0a  .addrCont;.  }..
30a4e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
30a4f 53 54 20 20 2f 2a 20 46 6f 72 20 74 65 73 74 69  ST  /* For testi
30a50 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
30a51 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20 2f   use only */.  /
30a52 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65 20  * Record in the 
30a53 71 75 65 72 79 20 70 6c 61 6e 20 69 6e 66 6f 72  query plan infor
30a54 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
30a55 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 0a 20   current table. 
30a56 20 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64 65   ** and the inde
30a57 78 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  x used to access
30a58 20 69 74 20 28 69 66 20 61 6e 79 29 2e 20 20 49   it (if any).  I
30a59 66 20 74 68 65 20 74 61 62 6c 65 20 69 74 73 65  f the table itse
30a5a 6c 66 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 75  lf.  ** is not u
30a5b 73 65 64 2c 20 69 74 73 20 6e 61 6d 65 20 69 73  sed, its name is
30a5c 20 6a 75 73 74 20 27 7b 7d 27 2e 20 20 49 66 20   just '{}'.  If 
30a5d 6e 6f 20 69 6e 64 65 78 20 69 73 20 75 73 65 64  no index is used
30a5e 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20  .  ** the index 
30a5f 69 73 20 6c 69 73 74 65 64 20 61 73 20 22 7b 7d  is listed as "{}
30a60 22 2e 20 20 49 66 20 74 68 65 20 70 72 69 6d 61  ".  If the prima
30a61 72 79 20 6b 65 79 20 69 73 20 75 73 65 64 20 74  ry key is used t
30a62 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6e 61  he.  ** index na
30a63 6d 65 20 69 73 20 27 2a 27 2e 0a 20 20 2a 2f 0a  me is '*'..  */.
30a64 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
30a65 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
30a66 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 0a  ){.    char *z;.
30a67 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 70      int n;.    p
30a68 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d  Level = &pWInfo-
30a69 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 54 61 62 49  >a[i];.    pTabI
30a6a 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  tem = &pTabList-
30a6b 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
30a6c 5d 3b 0a 20 20 20 20 7a 20 3d 20 70 54 61 62 49  ];.    z = pTabI
30a6d 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20 20  tem->zAlias;.   
30a6e 20 69 66 28 20 7a 3d 3d 30 20 29 20 7a 20 3d 20   if( z==0 ) z = 
30a6f 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2d 3e  pTabItem->pTab->
30a70 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 20 3d 20 73  zName;.    n = s
30a71 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a  qlite3Strlen30(z
30a72 29 3b 0a 20 20 20 20 69 66 28 20 6e 2b 6e 51 50  );.    if( n+nQP
30a73 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c  lan < sizeof(sql
30a74 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29  ite3_query_plan)
30a75 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  -10 ){.      if(
30a76 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
30a77 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44  Flags & WHERE_ID
30a78 58 5f 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20  X_ONLY ){.      
30a79 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65    memcpy(&sqlite
30a7a 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50  3_query_plan[nQP
30a7b 6c 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32 29 3b 0a  lan], "{}", 2);.
30a7c 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b          nQPlan +
30a7d 3d 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 2;.      }else
30a7e 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
30a7f 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  (&sqlite3_query_
30a80 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 7a 2c  plan[nQPlan], z,
30a81 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 51 50   n);.        nQP
30a82 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20  lan += n;.      
30a83 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  }.      sqlite3_
30a84 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61  query_plan[nQPla
30a85 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20  n++] = ' ';.    
30a86 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  }.    testcase( 
30a87 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
30a88 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57  lags & WHERE_ROW
30a89 49 44 5f 45 51 20 29 3b 0a 20 20 20 20 74 65 73  ID_EQ );.    tes
30a8a 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70  tcase( pLevel->p
30a8b 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
30a8c 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 20  ERE_ROWID_RANGE 
30a8d 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  );.    if( pLeve
30a8e 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
30a8f 26 20 28 57 48 45 52 45 5f 52 4f 57 49 44 5f 45  & (WHERE_ROWID_E
30a90 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41  Q|WHERE_ROWID_RA
30a91 4e 47 45 29 20 29 7b 0a 20 20 20 20 20 20 6d 65  NGE) ){.      me
30a92 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75  mcpy(&sqlite3_qu
30a93 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
30a94 2c 20 22 2a 20 22 2c 20 32 29 3b 0a 20 20 20 20  , "* ", 2);.    
30a95 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 32 3b 0a 20    nQPlan += 2;. 
30a96 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 4c     }else if( (pL
30a97 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
30a98 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58  gs & WHERE_INDEX
30a99 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ED)!=0 ){.      
30a9a 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
30a9b 6e 33 30 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  n30(pLevel->plan
30a9c 2e 75 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b  .u.pIdx->zName);
30a9d 0a 20 20 20 20 20 20 69 66 28 20 6e 2b 6e 51 50  .      if( n+nQP
30a9e 6c 61 6e 20 3c 20 73 69 7a 65 6f 66 28 73 71 6c  lan < sizeof(sql
30a9f 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 29  ite3_query_plan)
30aa0 2d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  -2 ){.        me
30aa1 6d 63 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75  mcpy(&sqlite3_qu
30aa2 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
30aa3 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  , pLevel->plan.u
30aa4 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 6e 29  .pIdx->zName, n)
30aa5 3b 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e  ;.        nQPlan
30aa6 20 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 73   += n;.        s
30aa7 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61  qlite3_query_pla
30aa8 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20  n[nQPlan++] = ' 
30aa9 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ';.      }.    }
30aaa 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63  else{.      memc
30aab 70 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72  py(&sqlite3_quer
30aac 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20  y_plan[nQPlan], 
30aad 22 7b 7d 20 22 2c 20 33 29 3b 0a 20 20 20 20 20  "{} ", 3);.     
30aae 20 6e 51 50 6c 61 6e 20 2b 3d 20 33 3b 0a 20 20   nQPlan += 3;.  
30aaf 20 20 7d 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28    }.  }.  while(
30ab0 20 6e 51 50 6c 61 6e 3e 30 20 26 26 20 73 71 6c   nQPlan>0 && sql
30ab1 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
30ab2 6e 51 50 6c 61 6e 2d 31 5d 3d 3d 27 20 27 20 29  nQPlan-1]==' ' )
30ab3 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 71 75  {.    sqlite3_qu
30ab4 65 72 79 5f 70 6c 61 6e 5b 2d 2d 6e 51 50 6c 61  ery_plan[--nQPla
30ab5 6e 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  n] = 0;.  }.  sq
30ab6 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
30ab7 5b 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a 20 20  [nQPlan] = 0;.  
30ab8 6e 51 50 6c 61 6e 20 3d 20 30 3b 0a 23 65 6e 64  nQPlan = 0;.#end
30ab9 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53  if /* SQLITE_TES
30aba 54 20 2f 2f 20 54 65 73 74 69 6e 67 20 61 6e 64  T // Testing and
30abb 20 64 65 62 75 67 67 69 6e 67 20 75 73 65 20 6f   debugging use o
30abc 6e 6c 79 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 63  nly */..  /* Rec
30abd 6f 72 64 20 74 68 65 20 63 6f 6e 74 69 6e 75 61  ord the continua
30abe 74 69 6f 6e 20 61 64 64 72 65 73 73 20 69 6e 20  tion address in 
30abf 74 68 65 20 57 68 65 72 65 49 6e 66 6f 20 73 74  the WhereInfo st
30ac0 72 75 63 74 75 72 65 2e 20 20 54 68 65 6e 0a 20  ructure.  Then. 
30ac1 20 2a 2a 20 63 6c 65 61 6e 20 75 70 20 61 6e 64   ** clean up and
30ac2 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20   return..  */.  
30ac3 72 65 74 75 72 6e 20 70 57 49 6e 66 6f 3b 0a 0a  return pWInfo;..
30ac4 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
30ac5 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a  f malloc fails *
30ac6 2f 0a 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f  /.whereBeginErro
30ac7 72 3a 0a 20 20 77 68 65 72 65 49 6e 66 6f 46 72  r:.  whereInfoFr
30ac8 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a  ee(db, pWInfo);.
30ac9 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
30aca 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68  *.** Generate th
30acb 65 20 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45  e end of the WHE
30acc 52 45 20 6c 6f 6f 70 2e 20 20 53 65 65 20 63 6f  RE loop.  See co
30acd 6d 6d 65 6e 74 73 20 6f 6e 20 0a 2a 2a 20 73 71  mments on .** sq
30ace 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
30acf 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  ) for additional
30ad0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
30ad1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
30ad2 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65 72  void sqlite3Wher
30ad3 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 20 2a  eEnd(WhereInfo *
30ad4 70 57 49 6e 66 6f 29 7b 0a 20 20 50 61 72 73 65  pWInfo){.  Parse
30ad5 20 2a 70 50 61 72 73 65 20 3d 20 70 57 49 6e 66   *pParse = pWInf
30ad6 6f 2d 3e 70 50 61 72 73 65 3b 0a 20 20 56 64 62  o->pParse;.  Vdb
30ad7 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
30ad8 56 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Vdbe;.  int i;. 
30ad9 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65   WhereLevel *pLe
30ada 76 65 6c 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a  vel;.  SrcList *
30adb 70 54 61 62 4c 69 73 74 20 3d 20 70 57 49 6e 66  pTabList = pWInf
30adc 6f 2d 3e 70 54 61 62 4c 69 73 74 3b 0a 20 20 73  o->pTabList;.  s
30add 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
30ade 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 47  rse->db;..  /* G
30adf 65 6e 65 72 61 74 65 20 6c 6f 6f 70 20 74 65 72  enerate loop ter
30ae0 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 2e 0a 20  mination code.. 
30ae1 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70   */.  sqlite3Exp
30ae2 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
30ae3 73 65 29 3b 0a 20 20 66 6f 72 28 69 3d 70 54 61  se);.  for(i=pTa
30ae4 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 3b 20 69  bList->nSrc-1; i
30ae5 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70  >=0; i--){.    p
30ae6 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d  Level = &pWInfo-
30ae7 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 71 6c 69 74  >a[i];.    sqlit
30ae8 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
30ae9 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 61 64  el(v, pLevel->ad
30aea 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 69 66 28  drCont);.    if(
30aeb 20 70 4c 65 76 65 6c 2d 3e 6f 70 21 3d 4f 50 5f   pLevel->op!=OP_
30aec 4e 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 73 71  Noop ){.      sq
30aed 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
30aee 76 2c 20 70 4c 65 76 65 6c 2d 3e 6f 70 2c 20 70  v, pLevel->op, p
30aef 4c 65 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65  Level->p1, pLeve
30af0 6c 2d 3e 70 32 29 3b 0a 20 20 20 20 20 20 73 71  l->p2);.      sq
30af1 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
30af2 35 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 70 35 29  5(v, pLevel->p5)
30af3 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
30af4 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
30af5 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 5f  lags & WHERE_IN_
30af6 41 42 4c 45 20 26 26 20 70 4c 65 76 65 6c 2d 3e  ABLE && pLevel->
30af7 75 2e 69 6e 2e 6e 49 6e 3e 30 20 29 7b 0a 20 20  u.in.nIn>0 ){.  
30af8 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f      struct InLoo
30af9 70 20 2a 70 49 6e 3b 0a 20 20 20 20 20 20 69 6e  p *pIn;.      in
30afa 74 20 6a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  t j;.      sqlit
30afb 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
30afc 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 61 64  el(v, pLevel->ad
30afd 64 72 4e 78 74 29 3b 0a 20 20 20 20 20 20 66 6f  drNxt);.      fo
30afe 72 28 6a 3d 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  r(j=pLevel->u.in
30aff 2e 6e 49 6e 2c 20 70 49 6e 3d 26 70 4c 65 76 65  .nIn, pIn=&pLeve
30b00 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f 6f 70 5b  l->u.in.aInLoop[
30b01 6a 2d 31 5d 3b 20 6a 3e 30 3b 20 6a 2d 2d 2c 20  j-1]; j>0; j--, 
30b02 70 49 6e 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20  pIn--){.        
30b03 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
30b04 65 72 65 28 76 2c 20 70 49 6e 2d 3e 61 64 64 72  ere(v, pIn->addr
30b05 49 6e 54 6f 70 2b 31 29 3b 0a 20 20 20 20 20 20  InTop+1);.      
30b06 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
30b07 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
30b08 70 49 6e 2d 3e 69 43 75 72 2c 20 70 49 6e 2d 3e  pIn->iCur, pIn->
30b09 61 64 64 72 49 6e 54 6f 70 29 3b 0a 20 20 20 20  addrInTop);.    
30b0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
30b0b 75 6d 70 48 65 72 65 28 76 2c 20 70 49 6e 2d 3e  umpHere(v, pIn->
30b0c 61 64 64 72 49 6e 54 6f 70 2d 31 29 3b 0a 20 20  addrInTop-1);.  
30b0d 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
30b0e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
30b0f 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c 6f  evel->u.in.aInLo
30b10 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  op);.    }.    s
30b11 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
30b12 65 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76 65 6c  eLabel(v, pLevel
30b13 2d 3e 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20  ->addrBrk);.    
30b14 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66  if( pLevel->iLef
30b15 74 4a 6f 69 6e 20 29 7b 0a 20 20 20 20 20 20 69  tJoin ){.      i
30b16 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20 61  nt addr;.      a
30b17 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
30b18 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
30b19 50 6f 73 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65  Pos, pLevel->iLe
30b1a 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20 20 20 61  ftJoin);.      a
30b1b 73 73 65 72 74 28 20 28 70 4c 65 76 65 6c 2d 3e  ssert( (pLevel->
30b1c 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
30b1d 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d  HERE_IDX_ONLY)==
30b1e 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  0.           || 
30b1f 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73  (pLevel->plan.ws
30b20 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e  Flags & WHERE_IN
30b21 44 45 58 45 44 29 21 3d 30 20 29 3b 0a 20 20 20  DEXED)!=0 );.   
30b22 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e     if( (pLevel->
30b23 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
30b24 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d  HERE_IDX_ONLY)==
30b25 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
30b26 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
30b27 2c 20 4f 50 5f 4e 75 6c 6c 52 6f 77 2c 20 70 54  , OP_NullRow, pT
30b28 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75  abList->a[i].iCu
30b29 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  rsor);.      }. 
30b2a 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d       if( pLevel-
30b2b 3e 69 49 64 78 43 75 72 3e 3d 30 20 29 7b 0a 20  >iIdxCur>=0 ){. 
30b2c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
30b2d 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e  beAddOp1(v, OP_N
30b2e 75 6c 6c 52 6f 77 2c 20 70 4c 65 76 65 6c 2d 3e  ullRow, pLevel->
30b2f 69 49 64 78 43 75 72 29 3b 0a 20 20 20 20 20 20  iIdxCur);.      
30b30 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 76  }.      if( pLev
30b31 65 6c 2d 3e 6f 70 3d 3d 4f 50 5f 52 65 74 75 72  el->op==OP_Retur
30b32 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  n ){.        sql
30b33 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
30b34 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 70 4c 65 76  , OP_Gosub, pLev
30b35 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65 6c 2d 3e  el->p1, pLevel->
30b36 61 64 64 72 46 69 72 73 74 29 3b 0a 20 20 20 20  addrFirst);.    
30b37 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
30b38 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
30b39 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
30b3a 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 46 69  , pLevel->addrFi
30b3b 72 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rst);.      }.  
30b3c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
30b3d 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
30b3e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
30b3f 2a 20 54 68 65 20 22 62 72 65 61 6b 22 20 70 6f  * The "break" po
30b40 69 6e 74 20 69 73 20 68 65 72 65 2c 20 6a 75 73  int is here, jus
30b41 74 20 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f  t past the end o
30b42 66 20 74 68 65 20 6f 75 74 65 72 20 6c 6f 6f 70  f the outer loop
30b43 2e 0a 20 20 2a 2a 20 53 65 74 20 69 74 2e 0a 20  ..  ** Set it.. 
30b44 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
30b45 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
30b46 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 29   pWInfo->iBreak)
30b47 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20 61 6c  ;..  /* Close al
30b48 6c 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 73  l of the cursors
30b49 20 74 68 61 74 20 77 65 72 65 20 6f 70 65 6e 65   that were opene
30b4a 64 20 62 79 20 73 71 6c 69 74 65 33 57 68 65 72  d by sqlite3Wher
30b4b 65 42 65 67 69 6e 2e 0a 20 20 2a 2f 0a 20 20 66  eBegin..  */.  f
30b4c 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d 70  or(i=0, pLevel=p
30b4d 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62  WInfo->a; i<pTab
30b4e 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
30b4f 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20   pLevel++){.    
30b50 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
30b51 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 20 3d 20  tem *pTabItem = 
30b52 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65  &pTabList->a[pLe
30b53 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20  vel->iFrom];.   
30b54 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
30b55 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20  TabItem->pTab;. 
30b56 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21     assert( pTab!
30b57 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 28 70  =0 );.    if( (p
30b58 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
30b59 54 46 5f 45 70 68 65 6d 65 72 61 6c 29 21 3d 30  TF_Ephemeral)!=0
30b5a 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   || pTab->pSelec
30b5b 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  t ) continue;.  
30b5c 20 20 69 66 28 20 28 70 57 49 6e 66 6f 2d 3e 77    if( (pWInfo->w
30b5d 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52  ctrlFlags & WHER
30b5e 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 29 3d 3d 30  E_OMIT_CLOSE)==0
30b5f 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 70   ){.      if( !p
30b60 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73  WInfo->okOnePass
30b61 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61   && (pLevel->pla
30b62 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
30b63 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30 20 29  E_IDX_ONLY)==0 )
30b64 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
30b65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
30b66 50 5f 43 6c 6f 73 65 2c 20 70 54 61 62 49 74 65  P_Close, pTabIte
30b67 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20  m->iCursor);.   
30b68 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28     }.      if( (
30b69 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
30b6a 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44  lags & WHERE_IND
30b6b 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20 20 20  EXED)!=0 ){.    
30b6c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
30b6d 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
30b6e 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43  e, pLevel->iIdxC
30b6f 75 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ur);.      }.   
30b70 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
30b71 69 73 20 73 63 61 6e 20 75 73 65 73 20 61 6e 20  is scan uses an 
30b72 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63 6f 64 65  index, make code
30b73 20 73 75 62 73 74 69 74 75 74 69 6f 6e 73 20 74   substitutions t
30b74 6f 20 72 65 61 64 20 64 61 74 61 0a 20 20 20 20  o read data.    
30b75 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 64 65  ** from the inde
30b76 78 20 69 6e 20 70 72 65 66 65 72 65 6e 63 65 20  x in preference 
30b77 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 20 53 6f  to the table. So
30b78 6d 65 74 69 6d 65 73 2c 20 74 68 69 73 20 6d 65  metimes, this me
30b79 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 74  ans.    ** the t
30b7a 61 62 6c 65 20 6e 65 65 64 20 6e 65 76 65 72 20  able need never 
30b7b 62 65 20 72 65 61 64 20 66 72 6f 6d 2e 20 54 68  be read from. Th
30b7c 69 73 20 69 73 20 61 20 70 65 72 66 6f 72 6d 61  is is a performa
30b7d 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20 20 20 2a  nce boost,.    *
30b7e 2a 20 61 73 20 74 68 65 20 76 64 62 65 20 6c 65  * as the vdbe le
30b7f 76 65 6c 20 77 61 69 74 73 20 75 6e 74 69 6c 20  vel waits until 
30b80 74 68 65 20 74 61 62 6c 65 20 69 73 20 72 65 61  the table is rea
30b81 64 20 62 65 66 6f 72 65 20 61 63 74 75 61 6c 6c  d before actuall
30b82 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69 6e 67  y.    ** seeking
30b83 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 73 6f   the table curso
30b84 72 20 74 6f 20 74 68 65 20 72 65 63 6f 72 64 20  r to the record 
30b85 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f  corresponding to
30b86 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20 20   the current.   
30b87 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20   ** position in 
30b88 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a  the index..    *
30b89 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c 6c 73 20  * .    ** Calls 
30b8a 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65  to the code gene
30b8b 72 61 74 6f 72 20 69 6e 20 62 65 74 77 65 65 6e  rator in between
30b8c 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67   sqlite3WhereBeg
30b8d 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a 20 73 71  in and.    ** sq
30b8e 6c 69 74 65 33 57 68 65 72 65 45 6e 64 20 77 69  lite3WhereEnd wi
30b8f 6c 6c 20 68 61 76 65 20 63 72 65 61 74 65 64 20  ll have created 
30b90 63 6f 64 65 20 74 68 61 74 20 72 65 66 65 72 65  code that refere
30b91 6e 63 65 73 20 74 68 65 20 74 61 62 6c 65 0a 20  nces the table. 
30b92 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20     ** directly. 
30b93 20 54 68 69 73 20 6c 6f 6f 70 20 73 63 61 6e 73   This loop scans
30b94 20 61 6c 6c 20 74 68 61 74 20 63 6f 64 65 20 6c   all that code l
30b95 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63 6f 64  ooking for opcod
30b96 65 73 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 72  es.    ** that r
30b97 65 66 65 72 65 6e 63 65 20 74 68 65 20 74 61 62  eference the tab
30b98 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72 74 73 20  le and converts 
30b99 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63 6f 64 65  them into opcode
30b9a 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 72 65  s that.    ** re
30b9b 66 65 72 65 6e 63 65 20 74 68 65 20 69 6e 64 65  ference the inde
30b9c 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  x..    */.    if
30b9d 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  ( (pLevel->plan.
30b9e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
30b9f 49 4e 44 45 58 45 44 29 21 3d 30 20 26 26 20 21  INDEXED)!=0 && !
30ba0 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
30ba1 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 2c 20  ){.      int k, 
30ba2 6a 2c 20 6c 61 73 74 3b 0a 20 20 20 20 20 20 56  j, last;.      V
30ba3 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 20 20  dbeOp *pOp;.    
30ba4 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20    Index *pIdx = 
30ba5 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70  pLevel->plan.u.p
30ba6 49 64 78 3b 0a 0a 20 20 20 20 20 20 61 73 73 65  Idx;..      asse
30ba7 72 74 28 20 70 49 64 78 21 3d 30 20 29 3b 0a 20  rt( pIdx!=0 );. 
30ba8 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c 69 74       pOp = sqlit
30ba9 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 70  e3VdbeGetOp(v, p
30baa 57 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b 0a 20 20  WInfo->iTop);.  
30bab 20 20 20 20 6c 61 73 74 20 3d 20 73 71 6c 69 74      last = sqlit
30bac 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
30bad 72 28 76 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  r(v);.      for(
30bae 6b 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b 20  k=pWInfo->iTop; 
30baf 6b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f 70  k<last; k++, pOp
30bb0 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
30bb1 20 70 4f 70 2d 3e 70 31 21 3d 70 4c 65 76 65 6c   pOp->p1!=pLevel
30bb2 2d 3e 69 54 61 62 43 75 72 20 29 20 63 6f 6e 74  ->iTabCur ) cont
30bb3 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66  inue;.        if
30bb4 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
30bb5 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20  P_Column ){.    
30bb6 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
30bb7 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  <pIdx->nColumn; 
30bb8 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
30bb9 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 70    if( pOp->p2==p
30bba 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
30bbb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
30bbc 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b 0a 20    pOp->p2 = j;. 
30bbd 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4f 70               pOp
30bbe 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e 69  ->p1 = pLevel->i
30bbf 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20 20 20  IdxCur;.        
30bc0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
30bc1 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
30bc2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
30bc3 20 61 73 73 65 72 74 28 20 28 70 4c 65 76 65 6c   assert( (pLevel
30bc4 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
30bc5 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29   WHERE_IDX_ONLY)
30bc6 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 20  ==0.            
30bc7 20 20 20 7c 7c 20 6a 3c 70 49 64 78 2d 3e 6e 43     || j<pIdx->nC
30bc8 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20 20 20 20  olumn );.       
30bc9 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e   }else if( pOp->
30bca 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 69 64  opcode==OP_Rowid
30bcb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f   ){.          pO
30bcc 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e  p->p1 = pLevel->
30bcd 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20 20  iIdxCur;.       
30bce 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d     pOp->opcode =
30bcf 20 4f 50 5f 49 64 78 52 6f 77 69 64 3b 0a 20 20   OP_IdxRowid;.  
30bd0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
30bd1 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
30bd2 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70 0a 20 20  Final cleanup.  
30bd3 2a 2f 0a 20 20 77 68 65 72 65 49 6e 66 6f 46 72  */.  whereInfoFr
30bd4 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a  ee(db, pWInfo);.
30bd5 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a    return;.}../**
30bd6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
30bd7 20 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a   of where.c ****
30bd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30bd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30bda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
30bdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
30bdc 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e 63 20  in file parse.c 
30bdd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30bde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30bdf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
30be0 44 72 69 76 65 72 20 74 65 6d 70 6c 61 74 65 20  Driver template 
30be1 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20 70 61  for the LEMON pa
30be2 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a  rser generator..
30be3 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
30be4 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
30be5 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
30be6 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   code..**.** Thi
30be7 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 22 6c 65  s version of "le
30be8 6d 70 61 72 2e 63 22 20 69 73 20 6d 6f 64 69 66  mpar.c" is modif
30be9 69 65 64 2c 20 73 6c 69 67 68 74 6c 79 2c 20 66  ied, slightly, f
30bea 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  or use by SQLite
30beb 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 6d 6f  ..** The only mo
30bec 64 69 66 69 63 61 74 69 6f 6e 73 20 61 72 65 20  difications are 
30bed 74 68 65 20 61 64 64 69 74 69 6f 6e 20 6f 66 20  the addition of 
30bee 61 20 63 6f 75 70 6c 65 20 6f 66 20 4e 45 56 45  a couple of NEVE
30bef 52 28 29 0a 2a 2a 20 6d 61 63 72 6f 73 20 74 6f  R().** macros to
30bf0 20 64 69 73 61 62 6c 65 20 74 65 73 74 73 20 74   disable tests t
30bf1 68 61 74 20 61 72 65 20 6e 65 65 64 65 64 20 69  hat are needed i
30bf2 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20 61 20  n the case of a 
30bf3 67 65 6e 65 72 61 6c 0a 2a 2a 20 4c 41 4c 52 28  general.** LALR(
30bf4 31 29 20 67 72 61 6d 6d 61 72 20 62 75 74 20 77  1) grammar but w
30bf5 68 69 63 68 20 61 72 65 20 61 6c 77 61 79 73 20  hich are always 
30bf6 66 61 6c 73 65 20 69 6e 20 74 68 65 0a 2a 2a 20  false in the.** 
30bf7 73 70 65 63 69 66 69 63 20 67 72 61 6d 6d 61 72  specific grammar
30bf8 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 2e   used by SQLite.
30bf9 0a 2a 2f 0a 2f 2a 20 46 69 72 73 74 20 6f 66 66  .*/./* First off
30bfa 2c 20 63 6f 64 65 20 69 73 20 69 6e 63 6c 75 64  , code is includ
30bfb 65 64 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20  ed that follows 
30bfc 74 68 65 20 22 69 6e 63 6c 75 64 65 22 20 64 65  the "include" de
30bfd 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20  claration.** in 
30bfe 74 68 65 20 69 6e 70 75 74 20 67 72 61 6d 6d 61  the input gramma
30bff 72 20 66 69 6c 65 2e 20 2a 2f 0a 0a 0a 2f 2a 0a  r file. */.../*.
30c00 2a 2a 20 44 69 73 61 62 6c 65 20 61 6c 6c 20 65  ** Disable all e
30c01 72 72 6f 72 20 72 65 63 6f 76 65 72 79 20 70 72  rror recovery pr
30c02 6f 63 65 73 73 69 6e 67 20 69 6e 20 74 68 65 20  ocessing in the 
30c03 70 61 72 73 65 72 20 70 75 73 68 2d 64 6f 77 6e  parser push-down
30c04 0a 2a 2a 20 61 75 74 6f 6d 61 74 6f 6e 2e 0a 2a  .** automaton..*
30c05 2f 0a 23 64 65 66 69 6e 65 20 59 59 4e 4f 45 52  /.#define YYNOER
30c06 52 4f 52 52 45 43 4f 56 45 52 59 20 31 0a 0a 2f  RORRECOVERY 1../
30c07 2a 0a 2a 2a 20 4d 61 6b 65 20 79 79 74 65 73 74  *.** Make yytest
30c08 63 61 73 65 28 29 20 74 68 65 20 73 61 6d 65 20  case() the same 
30c09 61 73 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2f  as testcase().*/
30c0a 0a 23 64 65 66 69 6e 65 20 79 79 74 65 73 74 63  .#define yytestc
30c0b 61 73 65 28 58 29 20 74 65 73 74 63 61 73 65 28  ase(X) testcase(
30c0c 58 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  X)../*.** An ins
30c0d 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
30c0e 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69 6e  ructure holds in
30c0f 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
30c10 74 68 65 0a 2a 2a 20 4c 49 4d 49 54 20 63 6c 61  the.** LIMIT cla
30c11 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20  use of a SELECT 
30c12 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74  statement..*/.st
30c13 72 75 63 74 20 4c 69 6d 69 74 56 61 6c 20 7b 0a  ruct LimitVal {.
30c14 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 3b 20    Expr *pLimit; 
30c15 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20     /* The LIMIT 
30c16 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4e 55 4c  expression.  NUL
30c17 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  L if there is no
30c18 20 6c 69 6d 69 74 20 2a 2f 0a 20 20 45 78 70 72   limit */.  Expr
30c19 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20   *pOffset;   /* 
30c1a 54 68 65 20 4f 46 46 53 45 54 20 65 78 70 72 65  The OFFSET expre
30c1b 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66 20  ssion.  NULL if 
30c1c 74 68 65 72 65 20 69 73 20 6e 6f 6e 65 20 2a 2f  there is none */
30c1d 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  .};../*.** An in
30c1e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
30c1f 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
30c20 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 4c 49   to store the LI
30c21 4b 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20 4e 4f 54  KE,.** GLOB, NOT
30c22 20 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f 54 20 47   LIKE, and NOT G
30c23 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a  LOB operators..*
30c24 2f 0a 73 74 72 75 63 74 20 4c 69 6b 65 4f 70 20  /.struct LikeOp 
30c25 7b 0a 20 20 54 6f 6b 65 6e 20 65 4f 70 65 72 61  {.  Token eOpera
30c26 74 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b 65 22 20  tor;  /* "like" 
30c27 6f 72 20 22 67 6c 6f 62 22 20 6f 72 20 22 72 65  or "glob" or "re
30c28 67 65 78 70 22 20 2a 2f 0a 20 20 69 6e 74 20 6e  gexp" */.  int n
30c29 6f 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ot;         /* T
30c2a 72 75 65 20 69 66 20 74 68 65 20 4e 4f 54 20 6b  rue if the NOT k
30c2b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e  eyword is presen
30c2c 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  t */.};../*.** A
30c2d 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
30c2e 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
30c2f 63 74 75 72 65 20 64 65 73 63 72 69 62 65 73 20  cture describes 
30c30 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61 0a 2a  the event of a.*
30c31 2a 20 54 52 49 47 47 45 52 2e 20 20 22 61 22 20  * TRIGGER.  "a" 
30c32 69 73 20 74 68 65 20 65 76 65 6e 74 20 74 79 70  is the event typ
30c33 65 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f 55 50 44  e, one of TK_UPD
30c34 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 0a  ATE, TK_INSERT,.
30c35 2a 2a 20 54 4b 5f 44 45 4c 45 54 45 2c 20 6f 72  ** TK_DELETE, or
30c36 20 54 4b 5f 49 4e 53 54 45 41 44 2e 20 20 49 66   TK_INSTEAD.  If
30c37 20 74 68 65 20 65 76 65 6e 74 20 69 73 20 6f 66   the event is of
30c38 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20   the form.**.** 
30c39 20 20 20 20 20 55 50 44 41 54 45 20 4f 4e 20 28       UPDATE ON (
30c3a 61 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a 20 54 68 65  a,b,c).**.** The
30c3b 6e 20 74 68 65 20 22 62 22 20 49 64 4c 69 73 74  n the "b" IdList
30c3c 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c 69 73   records the lis
30c3d 74 20 22 61 2c 62 2c 63 22 2e 0a 2a 2f 0a 73 74  t "a,b,c"..*/.st
30c3e 72 75 63 74 20 54 72 69 67 45 76 65 6e 74 20 7b  ruct TrigEvent {
30c3f 20 69 6e 74 20 61 3b 20 49 64 4c 69 73 74 20 2a   int a; IdList *
30c40 20 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e   b; };../*.** An
30c41 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
30c42 73 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64  s structure hold
30c43 73 20 74 68 65 20 41 54 54 41 43 48 20 6b 65 79  s the ATTACH key
30c44 20 61 6e 64 20 74 68 65 20 6b 65 79 20 74 79 70   and the key typ
30c45 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41 74 74  e..*/.struct Att
30c46 61 63 68 4b 65 79 20 7b 20 69 6e 74 20 74 79 70  achKey { int typ
30c47 65 3b 20 20 54 6f 6b 65 6e 20 6b 65 79 3b 20 7d  e;  Token key; }
30c48 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73  ;...  /* This is
30c49 20 61 20 75 74 69 6c 69 74 79 20 72 6f 75 74 69   a utility routi
30c4a 6e 65 20 75 73 65 64 20 74 6f 20 73 65 74 20 74  ne used to set t
30c4b 68 65 20 45 78 70 72 53 70 61 6e 2e 7a 53 74 61  he ExprSpan.zSta
30c4c 72 74 20 61 6e 64 0a 20 20 2a 2a 20 45 78 70 72  rt and.  ** Expr
30c4d 53 70 61 6e 2e 7a 45 6e 64 20 76 61 6c 75 65 73  Span.zEnd values
30c4e 20 6f 66 20 70 4f 75 74 20 73 6f 20 74 68 61 74   of pOut so that
30c4f 20 74 68 65 20 73 70 61 6e 20 63 6f 76 65 72 73   the span covers
30c50 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 20 20   the complete.  
30c51 2a 2a 20 72 61 6e 67 65 20 6f 66 20 74 65 78 74  ** range of text
30c52 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
30c53 70 53 74 61 72 74 20 61 6e 64 20 67 6f 69 6e 67  pStart and going
30c54 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 70   to the end of p
30c55 45 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74  End..  */.  stat
30c56 69 63 20 76 6f 69 64 20 73 70 61 6e 53 65 74 28  ic void spanSet(
30c57 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c 20  ExprSpan *pOut, 
30c58 54 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c 20 54  Token *pStart, T
30c59 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 20  oken *pEnd){.   
30c5a 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20   pOut->zStart = 
30c5b 70 53 74 61 72 74 2d 3e 7a 3b 0a 20 20 20 20 70  pStart->z;.    p
30c5c 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 26 70 45 6e  Out->zEnd = &pEn
30c5d 64 2d 3e 7a 5b 70 45 6e 64 2d 3e 6e 5d 3b 0a 20  d->z[pEnd->n];. 
30c5e 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75   }..  /* Constru
30c5f 63 74 20 61 20 6e 65 77 20 45 78 70 72 20 6f 62  ct a new Expr ob
30c60 6a 65 63 74 20 66 72 6f 6d 20 61 20 73 69 6e 67  ject from a sing
30c61 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20  le identifier.  
30c62 55 73 65 20 74 68 65 0a 20 20 2a 2a 20 6e 65 77  Use the.  ** new
30c63 20 45 78 70 72 20 74 6f 20 70 6f 70 75 6c 61 74   Expr to populat
30c64 65 20 70 4f 75 74 2e 20 20 53 65 74 20 74 68 65  e pOut.  Set the
30c65 20 73 70 61 6e 20 6f 66 20 70 4f 75 74 20 74 6f   span of pOut to
30c66 20 62 65 20 74 68 65 20 69 64 65 6e 74 69 66 69   be the identifi
30c67 65 72 0a 20 20 2a 2a 20 74 68 61 74 20 63 72 65  er.  ** that cre
30c68 61 74 65 64 20 74 68 65 20 65 78 70 72 65 73 73  ated the express
30c69 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74  ion..  */.  stat
30c6a 69 63 20 76 6f 69 64 20 73 70 61 6e 45 78 70 72  ic void spanExpr
30c6b 28 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c  (ExprSpan *pOut,
30c6c 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
30c6d 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20 2a 70  int op, Token *p
30c6e 56 61 6c 75 65 29 7b 0a 20 20 20 20 70 4f 75 74  Value){.    pOut
30c6f 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  ->pExpr = sqlite
30c70 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f  3PExpr(pParse, o
30c71 70 2c 20 30 2c 20 30 2c 20 70 56 61 6c 75 65 29  p, 0, 0, pValue)
30c72 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61  ;.    pOut->zSta
30c73 72 74 20 3d 20 70 56 61 6c 75 65 2d 3e 7a 3b 0a  rt = pValue->z;.
30c74 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d      pOut->zEnd =
30c75 20 26 70 56 61 6c 75 65 2d 3e 7a 5b 70 56 61 6c   &pValue->z[pVal
30c76 75 65 2d 3e 6e 5d 3b 0a 20 20 7d 0a 0a 20 20 2f  ue->n];.  }..  /
30c77 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
30c78 6f 6e 73 74 72 75 63 74 73 20 61 20 62 69 6e 61  onstructs a bina
30c79 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f  ry expression no
30c7a 64 65 20 6f 75 74 20 6f 66 20 74 77 6f 20 45 78  de out of two Ex
30c7b 70 72 53 70 61 6e 0a 20 20 2a 2a 20 6f 62 6a 65  prSpan.  ** obje
30c7c 63 74 73 20 61 6e 64 20 75 73 65 73 20 74 68 65  cts and uses the
30c7d 20 72 65 73 75 6c 74 20 74 6f 20 70 6f 70 75 6c   result to popul
30c7e 61 74 65 20 61 20 6e 65 77 20 45 78 70 72 53 70  ate a new ExprSp
30c7f 61 6e 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2f 0a  an object..  */.
30c80 20 20 73 74 61 74 69 63 20 76 6f 69 64 20 73 70    static void sp
30c81 61 6e 42 69 6e 61 72 79 45 78 70 72 28 0a 20 20  anBinaryExpr(.  
30c82 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74    ExprSpan *pOut
30c83 2c 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74  ,     /* Write t
30c84 68 65 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a  he result here *
30c85 2f 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61  /.    Parse *pPa
30c86 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  rse,      /* The
30c87 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
30c88 2e 20 20 45 72 72 6f 72 73 20 61 63 63 75 6d 75  .  Errors accumu
30c89 6c 61 74 65 20 68 65 72 65 20 2a 2f 0a 20 20 20  late here */.   
30c8a 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
30c8b 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69 6e 61       /* The bina
30c8c 72 79 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  ry operation */.
30c8d 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4c      ExprSpan *pL
30c8e 65 66 74 2c 20 20 20 20 2f 2a 20 54 68 65 20 6c  eft,    /* The l
30c8f 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  eft operand */. 
30c90 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 52 69     ExprSpan *pRi
30c91 67 68 74 20 20 20 20 2f 2a 20 54 68 65 20 72 69  ght    /* The ri
30c92 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  ght operand */. 
30c93 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 70 45   ){.    pOut->pE
30c94 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
30c95 70 72 28 70 50 61 72 73 65 2c 20 6f 70 2c 20 70  pr(pParse, op, p
30c96 4c 65 66 74 2d 3e 70 45 78 70 72 2c 20 70 52 69  Left->pExpr, pRi
30c97 67 68 74 2d 3e 70 45 78 70 72 2c 20 30 29 3b 0a  ght->pExpr, 0);.
30c98 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74      pOut->zStart
30c99 20 3d 20 70 4c 65 66 74 2d 3e 7a 53 74 61 72 74   = pLeft->zStart
30c9a 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64  ;.    pOut->zEnd
30c9b 20 3d 20 70 52 69 67 68 74 2d 3e 7a 45 6e 64 3b   = pRight->zEnd;
30c9c 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74  .  }..  /* Const
30c9d 72 75 63 74 20 61 6e 20 65 78 70 72 65 73 73 69  ruct an expressi
30c9e 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20 75 6e  on node for a un
30c9f 61 72 79 20 70 6f 73 74 66 69 78 20 6f 70 65 72  ary postfix oper
30ca0 61 74 6f 72 0a 20 20 2a 2f 0a 20 20 73 74 61 74  ator.  */.  stat
30ca1 69 63 20 76 6f 69 64 20 73 70 61 6e 55 6e 61 72  ic void spanUnar
30ca2 79 50 6f 73 74 66 69 78 28 0a 20 20 20 20 45 78  yPostfix(.    Ex
30ca3 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c 20 20 20  prSpan *pOut,   
30ca4 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
30ca5 65 20 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e  e new expression
30ca6 20 6e 6f 64 65 20 68 65 72 65 20 2a 2f 0a 20 20   node here */.  
30ca7 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
30ca8 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
30ca9 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 72  ing context to r
30caa 65 63 6f 72 64 20 65 72 72 6f 72 73 20 2a 2f 0a  ecord errors */.
30cab 20 20 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20      int op,     
30cac 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
30cad 65 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  e operator */.  
30cae 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 70 65    ExprSpan *pOpe
30caf 72 61 6e 64 2c 20 20 20 20 2f 2a 20 54 68 65 20  rand,    /* The 
30cb0 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 54  operand */.    T
30cb1 6f 6b 65 6e 20 2a 70 50 6f 73 74 4f 70 20 20 20  oken *pPostOp   
30cb2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 65        /* The ope
30cb3 72 61 6e 64 20 74 6f 6b 65 6e 20 66 6f 72 20 73  rand token for s
30cb4 65 74 74 69 6e 67 20 74 68 65 20 73 70 61 6e 20  etting the span 
30cb5 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 70 4f 75 74  */.  ){.    pOut
30cb6 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  ->pExpr = sqlite
30cb7 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f  3PExpr(pParse, o
30cb8 70 2c 20 70 4f 70 65 72 61 6e 64 2d 3e 70 45 78  p, pOperand->pEx
30cb9 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 70  pr, 0, 0);.    p
30cba 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20 70 4f  Out->zStart = pO
30cbb 70 65 72 61 6e 64 2d 3e 7a 53 74 61 72 74 3b 0a  perand->zStart;.
30cbc 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d      pOut->zEnd =
30cbd 20 26 70 50 6f 73 74 4f 70 2d 3e 7a 5b 70 50 6f   &pPostOp->z[pPo
30cbe 73 74 4f 70 2d 3e 6e 5d 3b 0a 20 20 7d 20 20 20  stOp->n];.  }   
30cbf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30cc0 20 20 20 20 20 20 20 20 0a 0a 20 20 2f 2a 20 41          ..  /* A
30cc1 20 72 6f 75 74 69 6e 65 20 74 6f 20 63 6f 6e 76   routine to conv
30cc2 65 72 74 20 61 20 62 69 6e 61 72 79 20 54 4b 5f  ert a binary TK_
30cc3 49 53 20 6f 72 20 54 4b 5f 49 53 4e 4f 54 20 65  IS or TK_ISNOT e
30cc4 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20 61  xpression into a
30cc5 0a 20 20 2a 2a 20 75 6e 61 72 79 20 54 4b 5f 49  .  ** unary TK_I
30cc6 53 4e 55 4c 4c 20 6f 72 20 54 4b 5f 4e 4f 54 4e  SNULL or TK_NOTN
30cc7 55 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  ULL expression. 
30cc8 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64  */.  static void
30cc9 20 62 69 6e 61 72 79 54 6f 55 6e 61 72 79 49 66   binaryToUnaryIf
30cca 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72  Null(Parse *pPar
30ccb 73 65 2c 20 45 78 70 72 20 2a 70 59 2c 20 45 78  se, Expr *pY, Ex
30ccc 70 72 20 2a 70 41 2c 20 69 6e 74 20 6f 70 29 7b  pr *pA, int op){
30ccd 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
30cce 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
30ccf 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
30cd0 63 46 61 69 6c 65 64 3d 3d 30 20 26 26 20 70 59  cFailed==0 && pY
30cd1 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b  ->op==TK_NULL ){
30cd2 0a 20 20 20 20 20 20 70 41 2d 3e 6f 70 20 3d 20  .      pA->op = 
30cd3 28 75 38 29 6f 70 3b 0a 20 20 20 20 20 20 73 71  (u8)op;.      sq
30cd4 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
30cd5 64 62 2c 20 70 41 2d 3e 70 52 69 67 68 74 29 3b  db, pA->pRight);
30cd6 0a 20 20 20 20 20 20 70 41 2d 3e 70 52 69 67 68  .      pA->pRigh
30cd7 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  t = 0;.    }.  }
30cd8 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74  ..  /* Construct
30cd9 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6e   an expression n
30cda 6f 64 65 20 66 6f 72 20 61 20 75 6e 61 72 79 20  ode for a unary 
30cdb 70 72 65 66 69 78 20 6f 70 65 72 61 74 6f 72 0a  prefix operator.
30cdc 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 76 6f    */.  static vo
30cdd 69 64 20 73 70 61 6e 55 6e 61 72 79 50 72 65 66  id spanUnaryPref
30cde 69 78 28 0a 20 20 20 20 45 78 70 72 53 70 61 6e  ix(.    ExprSpan
30cdf 20 2a 70 4f 75 74 2c 20 20 20 20 20 20 20 20 2f   *pOut,        /
30ce0 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20  * Write the new 
30ce1 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
30ce2 68 65 72 65 20 2a 2f 0a 20 20 20 20 50 61 72 73  here */.    Pars
30ce3 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
30ce4 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
30ce5 6e 74 65 78 74 20 74 6f 20 72 65 63 6f 72 64 20  ntext to record 
30ce6 65 72 72 6f 72 73 20 2a 2f 0a 20 20 20 20 69 6e  errors */.    in
30ce7 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
30ce8 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 70 65 72       /* The oper
30ce9 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72  ator */.    Expr
30cea 53 70 61 6e 20 2a 70 4f 70 65 72 61 6e 64 2c 20  Span *pOperand, 
30ceb 20 20 20 2f 2a 20 54 68 65 20 6f 70 65 72 61 6e     /* The operan
30cec 64 20 2a 2f 0a 20 20 20 20 54 6f 6b 65 6e 20 2a  d */.    Token *
30ced 70 50 72 65 4f 70 20 20 20 20 20 20 20 20 20 2f  pPreOp         /
30cee 2a 20 54 68 65 20 6f 70 65 72 61 6e 64 20 74 6f  * The operand to
30cef 6b 65 6e 20 66 6f 72 20 73 65 74 74 69 6e 67 20  ken for setting 
30cf0 74 68 65 20 73 70 61 6e 20 2a 2f 0a 20 20 29 7b  the span */.  ){
30cf1 0a 20 20 20 20 70 4f 75 74 2d 3e 70 45 78 70 72  .    pOut->pExpr
30cf2 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
30cf3 70 50 61 72 73 65 2c 20 6f 70 2c 20 70 4f 70 65  pParse, op, pOpe
30cf4 72 61 6e 64 2d 3e 70 45 78 70 72 2c 20 30 2c 20  rand->pExpr, 0, 
30cf5 30 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53  0);.    pOut->zS
30cf6 74 61 72 74 20 3d 20 70 50 72 65 4f 70 2d 3e 7a  tart = pPreOp->z
30cf7 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64  ;.    pOut->zEnd
30cf8 20 3d 20 70 4f 70 65 72 61 6e 64 2d 3e 7a 45 6e   = pOperand->zEn
30cf9 64 3b 0a 20 20 7d 0a 2f 2a 20 4e 65 78 74 20 69  d;.  }./* Next i
30cfa 73 20 61 6c 6c 20 74 6f 6b 65 6e 20 76 61 6c 75  s all token valu
30cfb 65 73 2c 20 69 6e 20 61 20 66 6f 72 6d 20 73 75  es, in a form su
30cfc 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 62  itable for use b
30cfd 79 20 6d 61 6b 65 68 65 61 64 65 72 73 2e 0a 2a  y makeheaders..*
30cfe 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20 77  * This section w
30cff 69 6c 6c 20 62 65 20 6e 75 6c 6c 20 75 6e 6c 65  ill be null unle
30d00 73 73 20 6c 65 6d 6f 6e 20 69 73 20 72 75 6e 20  ss lemon is run 
30d01 77 69 74 68 20 74 68 65 20 2d 6d 20 73 77 69 74  with the -m swit
30d02 63 68 2e 0a 2a 2f 0a 2f 2a 20 0a 2a 2a 20 54 68  ch..*/./* .** Th
30d03 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 28 61  ese constants (a
30d04 6c 6c 20 67 65 6e 65 72 61 74 65 64 20 61 75 74  ll generated aut
30d05 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20 74 68  omatically by th
30d06 65 20 70 61 72 73 65 72 20 67 65 6e 65 72 61 74  e parser generat
30d07 6f 72 29 0a 2a 2a 20 73 70 65 63 69 66 79 20 74  or).** specify t
30d08 68 65 20 76 61 72 69 6f 75 73 20 6b 69 6e 64 73  he various kinds
30d09 20 6f 66 20 74 6f 6b 65 6e 73 20 28 74 65 72 6d   of tokens (term
30d0a 69 6e 61 6c 73 29 20 74 68 61 74 20 74 68 65 20  inals) that the 
30d0b 70 61 72 73 65 72 0a 2a 2a 20 75 6e 64 65 72 73  parser.** unders
30d0c 74 61 6e 64 73 2e 20 0a 2a 2a 0a 2a 2a 20 45 61  tands. .**.** Ea
30d0d 63 68 20 73 79 6d 62 6f 6c 20 68 65 72 65 20 69  ch symbol here i
30d0e 73 20 61 20 74 65 72 6d 69 6e 61 6c 20 73 79 6d  s a terminal sym
30d0f 62 6f 6c 20 69 6e 20 74 68 65 20 67 72 61 6d 6d  bol in the gramm
30d10 61 72 2e 0a 2a 2f 0a 2f 2a 20 4d 61 6b 65 20 73  ar..*/./* Make s
30d11 75 72 65 20 74 68 65 20 49 4e 54 45 52 46 41 43  ure the INTERFAC
30d12 45 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e  E macro is defin
30d13 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49  ed..*/.#ifndef I
30d14 4e 54 45 52 46 41 43 45 0a 23 20 64 65 66 69 6e  NTERFACE.# defin
30d15 65 20 49 4e 54 45 52 46 41 43 45 20 31 0a 23 65  e INTERFACE 1.#e
30d16 6e 64 69 66 0a 2f 2a 20 54 68 65 20 6e 65 78 74  ndif./* The next
30d17 20 74 68 69 6e 67 20 69 6e 63 6c 75 64 65 64 20   thing included 
30d18 69 73 20 73 65 72 69 65 73 20 6f 66 20 64 65 66  is series of def
30d19 69 6e 65 73 20 77 68 69 63 68 20 63 6f 6e 74 72  ines which contr
30d1a 6f 6c 0a 2a 2a 20 76 61 72 69 6f 75 73 20 61 73  ol.** various as
30d1b 70 65 63 74 73 20 6f 66 20 74 68 65 20 67 65 6e  pects of the gen
30d1c 65 72 61 74 65 64 20 70 61 72 73 65 72 2e 0a 2a  erated parser..*
30d1d 2a 20 20 20 20 59 59 43 4f 44 45 54 59 50 45 20  *    YYCODETYPE 
30d1e 20 20 20 20 20 20 20 20 69 73 20 74 68 65 20 64          is the d
30d1f 61 74 61 20 74 79 70 65 20 75 73 65 64 20 66 6f  ata type used fo
30d20 72 20 73 74 6f 72 69 6e 67 20 74 65 72 6d 69 6e  r storing termin
30d21 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  al.**           
30d22 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20              and 
30d23 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62  nonterminal numb
30d24 65 72 73 2e 20 20 22 75 6e 73 69 67 6e 65 64 20  ers.  "unsigned 
30d25 63 68 61 72 22 20 69 73 0a 2a 2a 20 20 20 20 20  char" is.**     
30d26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d27 20 20 75 73 65 64 20 69 66 20 74 68 65 72 65 20    used if there 
30d28 61 72 65 20 66 65 77 65 72 20 74 68 61 6e 20 32  are fewer than 2
30d29 35 30 20 74 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20  50 terminals.** 
30d2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d2b 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72        and nonter
30d2c 6d 69 6e 61 6c 73 2e 20 20 22 69 6e 74 22 20 69  minals.  "int" i
30d2d 73 20 75 73 65 64 20 6f 74 68 65 72 77 69 73 65  s used otherwise
30d2e 2e 0a 2a 2a 20 20 20 20 59 59 4e 4f 43 4f 44 45  ..**    YYNOCODE
30d2f 20 20 20 20 20 20 20 20 20 20 20 69 73 20 61 20             is a 
30d30 6e 75 6d 62 65 72 20 6f 66 20 74 79 70 65 20 59  number of type Y
30d31 59 43 4f 44 45 54 59 50 45 20 77 68 69 63 68 20  YCODETYPE which 
30d32 63 6f 72 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 20  corresponds.**  
30d33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d34 20 20 20 20 20 74 6f 20 6e 6f 20 6c 65 67 61 6c       to no legal
30d35 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e   terminal or non
30d36 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 2e  terminal number.
30d37 20 20 54 68 69 73 0a 2a 2a 20 20 20 20 20 20 20    This.**       
30d38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d39 6e 75 6d 62 65 72 20 69 73 20 75 73 65 64 20 74  number is used t
30d3a 6f 20 66 69 6c 6c 20 69 6e 20 65 6d 70 74 79 20  o fill in empty 
30d3b 73 6c 6f 74 73 20 6f 66 20 74 68 65 20 68 61 73  slots of the has
30d3c 68 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  h .**           
30d3d 20 20 20 20 20 20 20 20 20 20 20 20 74 61 62 6c              tabl
30d3e 65 2e 0a 2a 2a 20 20 20 20 59 59 46 41 4c 4c 42  e..**    YYFALLB
30d3f 41 43 4b 20 20 20 20 20 20 20 20 20 49 66 20 64  ACK         If d
30d40 65 66 69 6e 65 64 2c 20 74 68 69 73 20 69 6e 64  efined, this ind
30d41 69 63 61 74 65 73 20 74 68 61 74 20 6f 6e 65 20  icates that one 
30d42 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 0a 2a  or more tokens.*
30d43 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
30d44 20 20 20 20 20 20 20 20 68 61 76 65 20 66 61 6c          have fal
30d45 6c 2d 62 61 63 6b 20 76 61 6c 75 65 73 20 77 68  l-back values wh
30d46 69 63 68 20 73 68 6f 75 6c 64 20 62 65 20 75 73  ich should be us
30d47 65 64 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 20  ed if the.**    
30d48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d49 20 20 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75     original valu
30d4a 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 77  e of the token w
30d4b 69 6c 6c 20 6e 6f 74 20 70 61 72 73 65 2e 0a 2a  ill not parse..*
30d4c 2a 20 20 20 20 59 59 41 43 54 49 4f 4e 54 59 50  *    YYACTIONTYP
30d4d 45 20 20 20 20 20 20 20 69 73 20 74 68 65 20 64  E       is the d
30d4e 61 74 61 20 74 79 70 65 20 75 73 65 64 20 66 6f  ata type used fo
30d4f 72 20 73 74 6f 72 69 6e 67 20 74 65 72 6d 69 6e  r storing termin
30d50 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  al.**           
30d51 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20              and 
30d52 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 6e 75 6d 62  nonterminal numb
30d53 65 72 73 2e 20 20 22 75 6e 73 69 67 6e 65 64 20  ers.  "unsigned 
30d54 63 68 61 72 22 20 69 73 0a 2a 2a 20 20 20 20 20  char" is.**     
30d55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d56 20 20 75 73 65 64 20 69 66 20 74 68 65 72 65 20    used if there 
30d57 61 72 65 20 66 65 77 65 72 20 74 68 61 6e 20 32  are fewer than 2
30d58 35 30 20 72 75 6c 65 73 20 61 6e 64 0a 2a 2a 20  50 rules and.** 
30d59 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d5a 20 20 20 20 20 20 73 74 61 74 65 73 20 63 6f 6d        states com
30d5b 62 69 6e 65 64 2e 20 20 22 69 6e 74 22 20 69 73  bined.  "int" is
30d5c 20 75 73 65 64 20 6f 74 68 65 72 77 69 73 65 2e   used otherwise.
30d5d 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33 50 61  .**    sqlite3Pa
30d5e 72 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 20 20  rserTOKENTYPE   
30d5f 20 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79    is the data ty
30d60 70 65 20 75 73 65 64 20 66 6f 72 20 6d 69 6e 6f  pe used for mino
30d61 72 20 74 6f 6b 65 6e 73 20 67 69 76 65 6e 20 0a  r tokens given .
30d62 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
30d63 20 20 20 20 20 20 20 20 20 64 69 72 65 63 74 6c           directl
30d64 79 20 74 6f 20 74 68 65 20 70 61 72 73 65 72 20  y to the parser 
30d65 66 72 6f 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a  from the tokeniz
30d66 65 72 2e 0a 2a 2a 20 20 20 20 59 59 4d 49 4e 4f  er..**    YYMINO
30d67 52 54 59 50 45 20 20 20 20 20 20 20 20 69 73 20  RTYPE        is 
30d68 74 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73  the data type us
30d69 65 64 20 66 6f 72 20 61 6c 6c 20 6d 69 6e 6f 72  ed for all minor
30d6a 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 20 20 20 20 20   tokens..**     
30d6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d6c 20 20 54 68 69 73 20 69 73 20 74 79 70 69 63 61    This is typica
30d6d 6c 6c 79 20 61 20 75 6e 69 6f 6e 20 6f 66 20 6d  lly a union of m
30d6e 61 6e 79 20 74 79 70 65 73 2c 20 6f 6e 65 20 6f  any types, one o
30d6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  f.**            
30d70 20 20 20 20 20 20 20 20 20 20 20 77 68 69 63 68             which
30d71 20 69 73 20 73 71 6c 69 74 65 33 50 61 72 73 65   is sqlite3Parse
30d72 72 54 4f 4b 45 4e 54 59 50 45 2e 20 20 54 68 65  rTOKENTYPE.  The
30d73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 75 6e   entry in the un
30d74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ion.**          
30d75 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72               for
30d76 20 62 61 73 65 20 74 6f 6b 65 6e 73 20 69 73 20   base tokens is 
30d77 63 61 6c 6c 65 64 20 22 79 79 30 22 2e 0a 2a 2a  called "yy0"..**
30d78 20 20 20 20 59 59 53 54 41 43 4b 44 45 50 54 48      YYSTACKDEPTH
30d79 20 20 20 20 20 20 20 69 73 20 74 68 65 20 6d 61         is the ma
30d7a 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74  ximum depth of t
30d7b 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63  he parser's stac
30d7c 6b 2e 20 20 49 66 0a 2a 2a 20 20 20 20 20 20 20  k.  If.**       
30d7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30d7e 7a 65 72 6f 20 74 68 65 20 73 74 61 63 6b 20 69  zero the stack i
30d7f 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 73 69  s dynamically si
30d80 7a 65 64 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f  zed using reallo
30d81 63 28 29 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65  c().**    sqlite
30d82 33 50 61 72 73 65 72 41 52 47 5f 53 44 45 43 4c  3ParserARG_SDECL
30d83 20 20 20 20 20 41 20 73 74 61 74 69 63 20 76 61       A static va
30d84 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69  riable declarati
30d85 6f 6e 20 66 6f 72 20 74 68 65 20 25 65 78 74 72  on for the %extr
30d86 61 5f 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20  a_argument.**   
30d87 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
30d88 47 5f 50 44 45 43 4c 20 20 20 20 20 41 20 70 61  G_PDECL     A pa
30d89 72 61 6d 65 74 65 72 20 64 65 63 6c 61 72 61 74  rameter declarat
30d8a 69 6f 6e 20 66 6f 72 20 74 68 65 20 25 65 78 74  ion for the %ext
30d8b 72 61 5f 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20  ra_argument.**  
30d8c 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
30d8d 52 47 5f 53 54 4f 52 45 20 20 20 20 20 43 6f 64  RG_STORE     Cod
30d8e 65 20 74 6f 20 73 74 6f 72 65 20 25 65 78 74 72  e to store %extr
30d8f 61 5f 61 72 67 75 6d 65 6e 74 20 69 6e 74 6f 20  a_argument into 
30d90 79 79 70 50 61 72 73 65 72 0a 2a 2a 20 20 20 20  yypParser.**    
30d91 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
30d92 5f 46 45 54 43 48 20 20 20 20 20 43 6f 64 65 20  _FETCH     Code 
30d93 74 6f 20 65 78 74 72 61 63 74 20 25 65 78 74 72  to extract %extr
30d94 61 5f 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20  a_argument from 
30d95 79 79 70 50 61 72 73 65 72 0a 2a 2a 20 20 20 20  yypParser.**    
30d96 59 59 4e 53 54 41 54 45 20 20 20 20 20 20 20 20  YYNSTATE        
30d97 20 20 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20     the combined 
30d98 6e 75 6d 62 65 72 20 6f 66 20 73 74 61 74 65 73  number of states
30d99 2e 0a 2a 2a 20 20 20 20 59 59 4e 52 55 4c 45 20  ..**    YYNRULE 
30d9a 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6e             the n
30d9b 75 6d 62 65 72 20 6f 66 20 72 75 6c 65 73 20 69  umber of rules i
30d9c 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 0a 2a 2a  n the grammar.**
30d9d 20 20 20 20 59 59 45 52 52 4f 52 53 59 4d 42 4f      YYERRORSYMBO
30d9e 4c 20 20 20 20 20 20 69 73 20 74 68 65 20 63 6f  L      is the co
30d9f 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  de number of the
30da0 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2e 20 20   error symbol.  
30da1 49 66 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20  If not.**       
30da2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30da3 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 64 6f  defined, then do
30da4 20 6e 6f 20 65 72 72 6f 72 20 70 72 6f 63 65 73   no error proces
30da5 73 69 6e 67 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  sing..*/.#define
30da6 20 59 59 43 4f 44 45 54 59 50 45 20 75 6e 73 69   YYCODETYPE unsi
30da7 67 6e 65 64 20 63 68 61 72 0a 23 64 65 66 69 6e  gned char.#defin
30da8 65 20 59 59 4e 4f 43 4f 44 45 20 32 35 34 0a 23  e YYNOCODE 254.#
30da9 64 65 66 69 6e 65 20 59 59 41 43 54 49 4f 4e 54  define YYACTIONT
30daa 59 50 45 20 75 6e 73 69 67 6e 65 64 20 73 68 6f  YPE unsigned sho
30dab 72 74 20 69 6e 74 0a 23 64 65 66 69 6e 65 20 59  rt int.#define Y
30dac 59 57 49 4c 44 43 41 52 44 20 36 37 0a 23 64 65  YWILDCARD 67.#de
30dad 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73  fine sqlite3Pars
30dae 65 72 54 4f 4b 45 4e 54 59 50 45 20 54 6f 6b 65  erTOKENTYPE Toke
30daf 6e 0a 74 79 70 65 64 65 66 20 75 6e 69 6f 6e 20  n.typedef union 
30db0 7b 0a 20 20 69 6e 74 20 79 79 69 6e 69 74 3b 0a  {.  int yyinit;.
30db1 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54    sqlite3ParserT
30db2 4f 4b 45 4e 54 59 50 45 20 79 79 30 3b 0a 20 20  OKENTYPE yy0;.  
30db3 53 65 6c 65 63 74 2a 20 79 79 33 3b 0a 20 20 45  Select* yy3;.  E
30db4 78 70 72 4c 69 73 74 2a 20 79 79 31 34 3b 0a 20  xprList* yy14;. 
30db5 20 53 72 63 4c 69 73 74 2a 20 79 79 36 35 3b 0a   SrcList* yy65;.
30db6 20 20 73 74 72 75 63 74 20 4c 69 6b 65 4f 70 20    struct LikeOp 
30db7 79 79 39 36 3b 0a 20 20 45 78 70 72 2a 20 79 79  yy96;.  Expr* yy
30db8 31 33 32 3b 0a 20 20 75 38 20 79 79 31 38 36 3b  132;.  u8 yy186;
30db9 0a 20 20 69 6e 74 20 79 79 33 32 38 3b 0a 20 20  .  int yy328;.  
30dba 45 78 70 72 53 70 61 6e 20 79 79 33 34 36 3b 0a  ExprSpan yy346;.
30dbb 20 20 73 74 72 75 63 74 20 54 72 69 67 45 76 65    struct TrigEve
30dbc 6e 74 20 79 79 33 37 38 3b 0a 20 20 49 64 4c 69  nt yy378;.  IdLi
30dbd 73 74 2a 20 79 79 34 30 38 3b 0a 20 20 73 74 72  st* yy408;.  str
30dbe 75 63 74 20 7b 69 6e 74 20 76 61 6c 75 65 3b 20  uct {int value; 
30dbf 69 6e 74 20 6d 61 73 6b 3b 7d 20 79 79 34 32 39  int mask;} yy429
30dc0 3b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 2a  ;.  TriggerStep*
30dc1 20 79 79 34 37 33 3b 0a 20 20 73 74 72 75 63 74   yy473;.  struct
30dc2 20 4c 69 6d 69 74 56 61 6c 20 79 79 34 37 36 3b   LimitVal yy476;
30dc3 0a 7d 20 59 59 4d 49 4e 4f 52 54 59 50 45 3b 0a  .} YYMINORTYPE;.
30dc4 23 69 66 6e 64 65 66 20 59 59 53 54 41 43 4b 44  #ifndef YYSTACKD
30dc5 45 50 54 48 0a 23 64 65 66 69 6e 65 20 59 59 53  EPTH.#define YYS
30dc6 54 41 43 4b 44 45 50 54 48 20 31 30 30 0a 23 65  TACKDEPTH 100.#e
30dc7 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndif.#define sql
30dc8 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 44  ite3ParserARG_SD
30dc9 45 43 4c 20 50 61 72 73 65 20 2a 70 50 61 72 73  ECL Parse *pPars
30dca 65 3b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  e;.#define sqlit
30dcb 65 33 50 61 72 73 65 72 41 52 47 5f 50 44 45 43  e3ParserARG_PDEC
30dcc 4c 20 2c 50 61 72 73 65 20 2a 70 50 61 72 73 65  L ,Parse *pParse
30dcd 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
30dce 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 20  ParserARG_FETCH 
30dcf 50 61 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20  Parse *pParse = 
30dd0 79 79 70 50 61 72 73 65 72 2d 3e 70 50 61 72 73  yypParser->pPars
30dd1 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
30dd2 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45  3ParserARG_STORE
30dd3 20 79 79 70 50 61 72 73 65 72 2d 3e 70 50 61 72   yypParser->pPar
30dd4 73 65 20 3d 20 70 50 61 72 73 65 0a 23 64 65 66  se = pParse.#def
30dd5 69 6e 65 20 59 59 4e 53 54 41 54 45 20 36 32 39  ine YYNSTATE 629
30dd6 0a 23 64 65 66 69 6e 65 20 59 59 4e 52 55 4c 45  .#define YYNRULE
30dd7 20 33 32 39 0a 23 64 65 66 69 6e 65 20 59 59 46   329.#define YYF
30dd8 41 4c 4c 42 41 43 4b 20 31 0a 23 64 65 66 69 6e  ALLBACK 1.#defin
30dd9 65 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 20 20  e YY_NO_ACTION  
30dda 20 20 20 20 28 59 59 4e 53 54 41 54 45 2b 59 59      (YYNSTATE+YY
30ddb 4e 52 55 4c 45 2b 32 29 0a 23 64 65 66 69 6e 65  NRULE+2).#define
30ddc 20 59 59 5f 41 43 43 45 50 54 5f 41 43 54 49 4f   YY_ACCEPT_ACTIO
30ddd 4e 20 20 28 59 59 4e 53 54 41 54 45 2b 59 59 4e  N  (YYNSTATE+YYN
30dde 52 55 4c 45 2b 31 29 0a 23 64 65 66 69 6e 65 20  RULE+1).#define 
30ddf 59 59 5f 45 52 52 4f 52 5f 41 43 54 49 4f 4e 20  YY_ERROR_ACTION 
30de0 20 20 28 59 59 4e 53 54 41 54 45 2b 59 59 4e 52    (YYNSTATE+YYNR
30de1 55 4c 45 29 0a 0a 2f 2a 20 54 68 65 20 79 79 7a  ULE)../* The yyz
30de2 65 72 6f 6d 69 6e 6f 72 20 63 6f 6e 73 74 61 6e  erominor constan
30de3 74 20 69 73 20 75 73 65 64 20 74 6f 20 69 6e 69  t is used to ini
30de4 74 69 61 6c 69 7a 65 20 69 6e 73 74 61 6e 63 65  tialize instance
30de5 73 20 6f 66 0a 2a 2a 20 59 59 4d 49 4e 4f 52 54  s of.** YYMINORT
30de6 59 50 45 20 6f 62 6a 65 63 74 73 20 74 6f 20 7a  YPE objects to z
30de7 65 72 6f 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63  ero. */.static c
30de8 6f 6e 73 74 20 59 59 4d 49 4e 4f 52 54 59 50 45  onst YYMINORTYPE
30de9 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 20 3d 20 7b   yyzerominor = {
30dea 20 30 20 7d 3b 0a 0a 2f 2a 20 44 65 66 69 6e 65   0 };../* Define
30deb 20 74 68 65 20 79 79 74 65 73 74 63 61 73 65 28   the yytestcase(
30dec 29 20 6d 61 63 72 6f 20 74 6f 20 62 65 20 61 20  ) macro to be a 
30ded 6e 6f 2d 6f 70 20 69 66 20 69 73 20 6e 6f 74 20  no-op if is not 
30dee 61 6c 72 65 61 64 79 20 64 65 66 69 6e 65 64 0a  already defined.
30def 2a 2a 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a  ** otherwise..**
30df0 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  .** Applications
30df1 20 63 61 6e 20 63 68 6f 6f 73 65 20 74 6f 20 64   can choose to d
30df2 65 66 69 6e 65 20 79 79 74 65 73 74 63 61 73 65  efine yytestcase
30df3 28 29 20 69 6e 20 74 68 65 20 25 69 6e 63 6c 75  () in the %inclu
30df4 64 65 20 73 65 63 74 69 6f 6e 0a 2a 2a 20 74 6f  de section.** to
30df5 20 61 20 6d 61 63 72 6f 20 74 68 61 74 20 63 61   a macro that ca
30df6 6e 20 61 73 73 69 73 74 20 69 6e 20 76 65 72 69  n assist in veri
30df7 66 79 69 6e 67 20 63 6f 64 65 20 63 6f 76 65 72  fying code cover
30df8 61 67 65 2e 20 20 46 6f 72 20 70 72 6f 64 75 63  age.  For produc
30df9 74 69 6f 6e 0a 2a 2a 20 63 6f 64 65 20 74 68 65  tion.** code the
30dfa 20 79 79 74 65 73 74 63 61 73 65 28 29 20 6d 61   yytestcase() ma
30dfb 63 72 6f 20 73 68 6f 75 6c 64 20 62 65 20 74 75  cro should be tu
30dfc 72 6e 65 64 20 6f 66 66 2e 20 20 42 75 74 20 69  rned off.  But i
30dfd 74 20 69 73 20 75 73 65 66 75 6c 0a 2a 2a 20 66  t is useful.** f
30dfe 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23  or testing..*/.#
30dff 69 66 6e 64 65 66 20 79 79 74 65 73 74 63 61 73  ifndef yytestcas
30e00 65 0a 23 20 64 65 66 69 6e 65 20 79 79 74 65 73  e.# define yytes
30e01 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69 66 0a  tcase(X).#endif.
30e02 0a 0a 2f 2a 20 4e 65 78 74 20 61 72 65 20 74 68  ../* Next are th
30e03 65 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f  e tables used to
30e04 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20   determine what 
30e05 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 62  action to take b
30e06 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63  ased on the.** c
30e07 75 72 72 65 6e 74 20 73 74 61 74 65 20 61 6e 64  urrent state and
30e08 20 6c 6f 6f 6b 61 68 65 61 64 20 74 6f 6b 65 6e   lookahead token
30e09 2e 20 20 54 68 65 73 65 20 74 61 62 6c 65 73 20  .  These tables 
30e0a 61 72 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  are used to impl
30e0b 65 6d 65 6e 74 0a 2a 2a 20 66 75 6e 63 74 69 6f  ement.** functio
30e0c 6e 73 20 74 68 61 74 20 74 61 6b 65 20 61 20 73  ns that take a s
30e0d 74 61 74 65 20 6e 75 6d 62 65 72 20 61 6e 64 20  tate number and 
30e0e 6c 6f 6f 6b 61 68 65 61 64 20 76 61 6c 75 65 20  lookahead value 
30e0f 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 0a 2a 2a  and return an.**
30e10 20 61 63 74 69 6f 6e 20 69 6e 74 65 67 65 72 2e   action integer.
30e11 20 20 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65    .**.** Suppose
30e12 20 74 68 65 20 61 63 74 69 6f 6e 20 69 6e 74 65   the action inte
30e13 67 65 72 20 69 73 20 4e 2e 20 20 54 68 65 6e 20  ger is N.  Then 
30e14 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 64 65  the action is de
30e15 74 65 72 6d 69 6e 65 64 20 61 73 0a 2a 2a 20 66  termined as.** f
30e16 6f 6c 6c 6f 77 73 0a 2a 2a 0a 2a 2a 20 20 20 30  ollows.**.**   0
30e17 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54 45   <= N < YYNSTATE
30e18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e19 20 20 53 68 69 66 74 20 4e 2e 20 20 54 68 61 74    Shift N.  That
30e1a 20 69 73 2c 20 70 75 73 68 20 74 68 65 20 6c 6f   is, push the lo
30e1b 6f 6b 61 68 65 61 64 0a 2a 2a 20 20 20 20 20 20  okahead.**      
30e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e1e 74 6f 6b 65 6e 20 6f 6e 74 6f 20 74 68 65 20 73  token onto the s
30e1f 74 61 63 6b 20 61 6e 64 20 67 6f 74 6f 20 73 74  tack and goto st
30e20 61 74 65 20 4e 2e 0a 2a 2a 0a 2a 2a 20 20 20 59  ate N..**.**   Y
30e21 59 4e 53 54 41 54 45 20 3c 3d 20 4e 20 3c 20 59  YNSTATE <= N < Y
30e22 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 20  YNSTATE+YYNRULE 
30e23 20 20 52 65 64 75 63 65 20 62 79 20 72 75 6c 65    Reduce by rule
30e24 20 4e 2d 59 59 4e 53 54 41 54 45 2e 0a 2a 2a 0a   N-YYNSTATE..**.
30e25 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41  **   N == YYNSTA
30e26 54 45 2b 59 59 4e 52 55 4c 45 20 20 20 20 20 20  TE+YYNRULE      
30e27 20 20 20 20 20 20 20 20 41 20 73 79 6e 74 61 78          A syntax
30e28 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72   error has occur
30e29 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d  red..**.**   N =
30e2a 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55  = YYNSTATE+YYNRU
30e2b 4c 45 2b 31 20 20 20 20 20 20 20 20 20 20 20 20  LE+1            
30e2c 54 68 65 20 70 61 72 73 65 72 20 61 63 63 65 70  The parser accep
30e2d 74 73 20 69 74 73 20 69 6e 70 75 74 2e 0a 2a 2a  ts its input..**
30e2e 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54  .**   N == YYNST
30e2f 41 54 45 2b 59 59 4e 52 55 4c 45 2b 32 20 20 20  ATE+YYNRULE+2   
30e30 20 20 20 20 20 20 20 20 20 4e 6f 20 73 75 63 68           No such
30e31 20 61 63 74 69 6f 6e 2e 20 20 44 65 6e 6f 74 65   action.  Denote
30e32 73 20 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20  s unused.**     
30e33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e35 20 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 79 79   slots in the yy
30e36 5f 61 63 74 69 6f 6e 5b 5d 20 74 61 62 6c 65 2e  _action[] table.
30e37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 74 69 6f  .**.** The actio
30e38 6e 20 74 61 62 6c 65 20 69 73 20 63 6f 6e 73 74  n table is const
30e39 72 75 63 74 65 64 20 61 73 20 61 20 73 69 6e 67  ructed as a sing
30e3a 6c 65 20 6c 61 72 67 65 20 74 61 62 6c 65 20 6e  le large table n
30e3b 61 6d 65 64 20 79 79 5f 61 63 74 69 6f 6e 5b 5d  amed yy_action[]
30e3c 2e 0a 2a 2a 20 47 69 76 65 6e 20 73 74 61 74 65  ..** Given state
30e3d 20 53 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64   S and lookahead
30e3e 20 58 2c 20 74 68 65 20 61 63 74 69 6f 6e 20 69   X, the action i
30e3f 73 20 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a  s computed as.**
30e40 0a 2a 2a 20 20 20 20 20 20 79 79 5f 61 63 74 69  .**      yy_acti
30e41 6f 6e 5b 20 79 79 5f 73 68 69 66 74 5f 6f 66 73  on[ yy_shift_ofs
30e42 74 5b 53 5d 20 2b 20 58 20 5d 0a 2a 2a 0a 2a 2a  t[S] + X ].**.**
30e43 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 76 61   If the index va
30e44 6c 75 65 20 79 79 5f 73 68 69 66 74 5f 6f 66 73  lue yy_shift_ofs
30e45 74 5b 53 5d 2b 58 20 69 73 20 6f 75 74 20 6f 66  t[S]+X is out of
30e46 20 72 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65   range or if the
30e47 20 76 61 6c 75 65 0a 2a 2a 20 79 79 5f 6c 6f 6f   value.** yy_loo
30e48 6b 61 68 65 61 64 5b 79 79 5f 73 68 69 66 74 5f  kahead[yy_shift_
30e49 6f 66 73 74 5b 53 5d 2b 58 5d 20 69 73 20 6e 6f  ofst[S]+X] is no
30e4a 74 20 65 71 75 61 6c 20 74 6f 20 58 20 6f 72 20  t equal to X or 
30e4b 69 66 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74  if yy_shift_ofst
30e4c 5b 53 5d 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20  [S].** is equal 
30e4d 74 6f 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f  to YY_SHIFT_USE_
30e4e 44 46 4c 54 2c 20 69 74 20 6d 65 61 6e 73 20 74  DFLT, it means t
30e4f 68 61 74 20 74 68 65 20 61 63 74 69 6f 6e 20 69  hat the action i
30e50 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 74 61 62  s not in the tab
30e51 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 79  le.** and that y
30e52 79 5f 64 65 66 61 75 6c 74 5b 53 5d 20 73 68 6f  y_default[S] sho
30e53 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e 73 74  uld be used inst
30e54 65 61 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ead.  .**.** The
30e55 20 66 6f 72 6d 75 6c 61 20 61 62 6f 76 65 20 69   formula above i
30e56 73 20 66 6f 72 20 63 6f 6d 70 75 74 69 6e 67 20  s for computing 
30e57 74 68 65 20 61 63 74 69 6f 6e 20 77 68 65 6e 20  the action when 
30e58 74 68 65 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73  the lookahead is
30e59 0a 2a 2a 20 61 20 74 65 72 6d 69 6e 61 6c 20 73  .** a terminal s
30e5a 79 6d 62 6f 6c 2e 20 20 49 66 20 74 68 65 20 6c  ymbol.  If the l
30e5b 6f 6f 6b 61 68 65 61 64 20 69 73 20 61 20 6e 6f  ookahead is a no
30e5c 6e 2d 74 65 72 6d 69 6e 61 6c 20 28 61 73 20 6f  n-terminal (as o
30e5d 63 63 75 72 73 20 61 66 74 65 72 0a 2a 2a 20 61  ccurs after.** a
30e5e 20 72 65 64 75 63 65 20 61 63 74 69 6f 6e 29 20   reduce action) 
30e5f 74 68 65 6e 20 74 68 65 20 79 79 5f 72 65 64 75  then the yy_redu
30e60 63 65 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20  ce_ofst[] array 
30e61 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65  is used in place
30e62 20 6f 66 0a 2a 2a 20 74 68 65 20 79 79 5f 73 68   of.** the yy_sh
30e63 69 66 74 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79  ift_ofst[] array
30e64 20 61 6e 64 20 59 59 5f 52 45 44 55 43 45 5f 55   and YY_REDUCE_U
30e65 53 45 5f 44 46 4c 54 20 69 73 20 75 73 65 64 20  SE_DFLT is used 
30e66 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 59  in place of.** Y
30e67 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54  Y_SHIFT_USE_DFLT
30e68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ..**.** The foll
30e69 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20 74 61  owing are the ta
30e6a 62 6c 65 73 20 67 65 6e 65 72 61 74 65 64 20 69  bles generated i
30e6b 6e 20 74 68 69 73 20 73 65 63 74 69 6f 6e 3a 0a  n this section:.
30e6c 2a 2a 0a 2a 2a 20 20 79 79 5f 61 63 74 69 6f 6e  **.**  yy_action
30e6d 5b 5d 20 20 20 20 20 20 20 20 41 20 73 69 6e 67  []        A sing
30e6e 6c 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  le table contain
30e6f 69 6e 67 20 61 6c 6c 20 61 63 74 69 6f 6e 73 2e  ing all actions.
30e70 0a 2a 2a 20 20 79 79 5f 6c 6f 6f 6b 61 68 65 61  .**  yy_lookahea
30e71 64 5b 5d 20 20 20 20 20 41 20 74 61 62 6c 65 20  d[]     A table 
30e72 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6c  containing the l
30e73 6f 6f 6b 61 68 65 61 64 20 66 6f 72 20 65 61 63  ookahead for eac
30e74 68 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 20 20  h entry in.**   
30e75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30e76 20 20 79 79 5f 61 63 74 69 6f 6e 2e 20 20 55 73    yy_action.  Us
30e77 65 64 20 74 6f 20 64 65 74 65 63 74 20 68 61 73  ed to detect has
30e78 68 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 2a 2a  h collisions..**
30e79 20 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b    yy_shift_ofst[
30e7a 5d 20 20 20 20 46 6f 72 20 65 61 63 68 20 73 74  ]    For each st
30e7b 61 74 65 2c 20 74 68 65 20 6f 66 66 73 65 74 20  ate, the offset 
30e7c 69 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66  into yy_action f
30e7d 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  or.**           
30e7e 20 20 20 20 20 20 20 20 20 20 73 68 69 66 74 69            shifti
30e7f 6e 67 20 74 65 72 6d 69 6e 61 6c 73 2e 0a 2a 2a  ng terminals..**
30e80 20 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74    yy_reduce_ofst
30e81 5b 5d 20 20 20 46 6f 72 20 65 61 63 68 20 73 74  []   For each st
30e82 61 74 65 2c 20 74 68 65 20 6f 66 66 73 65 74 20  ate, the offset 
30e83 69 6e 74 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66  into yy_action f
30e84 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  or.**           
30e85 20 20 20 20 20 20 20 20 20 20 73 68 69 66 74 69            shifti
30e86 6e 67 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 73  ng non-terminals
30e87 20 61 66 74 65 72 20 61 20 72 65 64 75 63 65 2e   after a reduce.
30e88 0a 2a 2a 20 20 79 79 5f 64 65 66 61 75 6c 74 5b  .**  yy_default[
30e89 5d 20 20 20 20 20 20 20 44 65 66 61 75 6c 74 20  ]       Default 
30e8a 61 63 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20  action for each 
30e8b 73 74 61 74 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  state..*/.#defin
30e8c 65 20 59 59 5f 41 43 54 54 41 42 5f 43 4f 55 4e  e YY_ACTTAB_COUN
30e8d 54 20 28 31 35 34 33 29 0a 73 74 61 74 69 63 20  T (1543).static 
30e8e 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e 54 59  const YYACTIONTY
30e8f 50 45 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 3d  PE yy_action[] =
30e90 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20   {. /*     0 */ 
30e91 20 20 33 31 32 2c 20 20 20 34 39 2c 20 20 35 35    312,   49,  55
30e92 34 2c 20 20 20 34 36 2c 20 20 31 34 37 2c 20 20  4,   46,  147,  
30e93 31 37 32 2c 20 20 36 32 36 2c 20 20 35 39 36 2c  172,  626,  596,
30e94 20 20 20 35 35 2c 20 20 20 35 35 2c 0a 20 2f 2a     55,   55,. /*
30e95 20 20 20 20 31 30 20 2a 2f 20 20 20 20 35 35 2c      10 */    55,
30e96 20 20 20 35 35 2c 20 20 33 30 31 2c 20 20 20 35     55,  301,   5
30e97 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  3,   53,   53,  
30e98 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   53,   52,   52,
30e99 20 20 20 35 31 2c 0a 20 2f 2a 20 20 20 20 32 30     51,. /*    20
30e9a 20 2a 2f 20 20 20 20 35 31 2c 20 20 20 35 31 2c   */    51,   51,
30e9b 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20 36 31     50,  237,  61
30e9c 37 2c 20 20 36 31 36 2c 20 20 36 31 35 2c 20 20  7,  616,  615,  
30e9d 36 32 32 2c 20 20 36 32 31 2c 20 20 36 30 37 2c  622,  621,  607,
30e9e 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20  . /*    30 */   
30e9f 35 38 39 2c 20 20 35 38 33 2c 20 20 20 34 38 2c  589,  583,   48,
30ea0 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
30ea1 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20  3,   53,   52,  
30ea2 20 35 32 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20   52,   51,. /*  
30ea3 20 20 34 30 20 2a 2f 20 20 20 20 35 31 2c 20 20    40 */    51,  
30ea4 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c   51,   50,  237,
30ea5 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
30ea6 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20  1,   50,  237,  
30ea7 20 35 36 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a   56,. /*    50 *
30ea8 2f 20 20 20 20 35 37 2c 20 20 20 34 37 2c 20 20  /    57,   47,  
30ea9 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c  581,  580,  582,
30eaa 20 20 35 38 32 2c 20 20 20 35 34 2c 20 20 20 35    582,   54,   5
30eab 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 0a 20  4,   55,   55,. 
30eac 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 20 35  /*    60 */    5
30ead 35 2c 20 20 20 35 35 2c 20 20 32 31 36 2c 20 20  5,   55,  216,  
30eae 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
30eaf 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35     53,   52,   5
30eb0 32 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20 20 20  2,   51,. /*    
30eb1 37 30 20 2a 2f 20 20 20 20 35 31 2c 20 20 20 35  70 */    51,   5
30eb2 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20  1,   50,  237,  
30eb3 33 31 32 2c 20 20 35 39 36 2c 20 20 20 34 39 2c  312,  596,   49,
30eb4 20 20 33 32 39 2c 20 20 20 34 36 2c 20 20 31 34    329,   46,  14
30eb5 37 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20  7,. /*    80 */ 
30eb6 20 20 20 33 32 2c 20 20 20 35 33 2c 20 20 20 35     32,   53,   5
30eb7 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  3,   53,   53,  
30eb8 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c   52,   52,   51,
30eb9 20 20 20 35 31 2c 20 20 20 35 31 2c 0a 20 2f 2a     51,   51,. /*
30eba 20 20 20 20 39 30 20 2a 2f 20 20 20 20 35 30 2c      90 */    50,
30ebb 20 20 32 33 37 2c 20 20 33 32 39 2c 20 20 36 32    237,  329,  62
30ebc 32 2c 20 20 36 32 31 2c 20 20 36 31 39 2c 20 20  2,  621,  619,  
30ebd 36 31 38 2c 20 20 31 36 36 2c 20 20 34 33 33 2c  618,  166,  433,
30ebe 20 20 35 34 37 2c 0a 20 2f 2a 20 20 20 31 30 30    547,. /*   100
30ebf 20 2a 2f 20 20 20 33 38 31 2c 20 20 33 37 38 2c   */   381,  378,
30ec0 20 20 33 37 37 2c 20 20 35 34 39 2c 20 20 35 38    377,  549,  58
30ec1 39 2c 20 20 35 38 33 2c 20 20 33 38 39 2c 20 20  9,  583,  389,  
30ec2 34 39 30 2c 20 20 31 36 36 2c 20 20 20 35 38 2c  490,  166,   58,
30ec3 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20  . /*   110 */   
30ec4 33 37 36 2c 20 20 33 38 31 2c 20 20 33 37 38 2c  376,  381,  378,
30ec5 20 20 33 37 37 2c 20 20 33 39 30 2c 20 20 32 39    377,  390,  29
30ec6 39 2c 20 20 36 32 32 2c 20 20 36 32 31 2c 20 20  9,  622,  621,  
30ec7 34 38 30 2c 20 20 20 36 37 2c 0a 20 2f 2a 20 20  480,   67,. /*  
30ec8 20 31 32 30 20 2a 2f 20 20 20 36 37 30 2c 20 20   120 */   670,  
30ec9 33 37 36 2c 20 20 36 32 30 2c 20 20 20 35 36 2c  376,  620,   56,
30eca 20 20 20 35 37 2c 20 20 20 34 37 2c 20 20 35 38     57,   47,  58
30ecb 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20  1,  580,  582,  
30ecc 35 38 32 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a  582,. /*   130 *
30ecd 2f 20 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20  /    54,   54,  
30ece 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   55,   55,   55,
30ecf 20 20 20 35 35 2c 20 20 32 35 33 2c 20 20 20 35     55,  253,   5
30ed0 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 0a 20  3,   53,   53,. 
30ed1 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20 35  /*   140 */    5
30ed2 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20  3,   52,   52,  
30ed3 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c   51,   51,   51,
30ed4 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31     50,  237,  31
30ed5 32 2c 20 20 34 30 38 2c 0a 20 2f 2a 20 20 20 31  2,  408,. /*   1
30ed6 35 30 20 2a 2f 20 20 20 32 32 35 2c 20 20 35 37  50 */   225,  57
30ed7 38 2c 20 20 35 37 38 2c 20 20 31 33 33 2c 20 20  8,  578,  133,  
30ed8 31 37 37 2c 20 20 31 33 39 2c 20 20 32 38 33 2c  177,  139,  283,
30ed9 20 20 33 38 34 2c 20 20 32 37 38 2c 20 20 33 38    384,  278,  38
30eda 33 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20  3,. /*   160 */ 
30edb 20 20 31 36 39 2c 20 20 36 31 39 2c 20 20 36 31    169,  619,  61
30edc 38 2c 20 20 36 30 31 2c 20 20 31 39 37 2c 20 20  8,  601,  197,  
30edd 32 32 35 2c 20 20 32 37 34 2c 20 20 36 30 32 2c  225,  274,  602,
30ede 20 20 34 33 39 2c 20 20 31 34 36 2c 0a 20 2f 2a    439,  146,. /*
30edf 20 20 20 31 37 30 20 2a 2f 20 20 20 31 33 39 2c     170 */   139,
30ee0 20 20 32 38 33 2c 20 20 33 38 34 2c 20 20 32 37    283,  384,  27
30ee1 38 2c 20 20 33 38 33 2c 20 20 31 36 39 2c 20 20  8,  383,  169,  
30ee2 35 36 39 2c 20 20 32 33 35 2c 20 20 35 38 39 2c  569,  235,  589,
30ee3 20 20 35 38 33 2c 0a 20 2f 2a 20 20 20 31 38 30    583,. /*   180
30ee4 20 2a 2f 20 20 20 32 35 30 2c 20 20 32 37 34 2c   */   250,  274,
30ee5 20 20 32 35 32 2c 20 20 36 32 30 2c 20 20 36 31    252,  620,  61
30ee6 39 2c 20 20 36 31 38 2c 20 20 35 34 36 2c 20 20  9,  618,  546,  
30ee7 34 33 36 2c 20 20 34 34 30 2c 20 20 34 34 31 2c  436,  440,  441,
30ee8 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20  . /*   190 */   
30ee9 31 36 38 2c 20 20 36 32 32 2c 20 20 36 32 31 2c  168,  622,  621,
30eea 20 20 35 34 37 2c 20 20 34 33 38 2c 20 20 34 33    547,  438,  43
30eeb 37 2c 20 20 31 39 32 2c 20 20 20 35 36 2c 20 20  7,  192,   56,  
30eec 20 35 37 2c 20 20 20 34 37 2c 0a 20 2f 2a 20 20   57,   47,. /*  
30eed 20 32 30 30 20 2a 2f 20 20 20 35 38 31 2c 20 20   200 */   581,  
30eee 35 38 30 2c 20 20 35 38 32 2c 20 20 35 38 32 2c  580,  582,  582,
30eef 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35     54,   54,   5
30ef0 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  5,   55,   55,  
30ef1 20 35 35 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a   55,. /*   210 *
30ef2 2f 20 20 20 20 20 36 2c 20 20 20 35 33 2c 20 20  /     6,   53,  
30ef3 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
30ef4 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35     52,   52,   5
30ef5 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 0a 20  1,   51,   51,. 
30ef6 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 20 35  /*   220 */    5
30ef7 30 2c 20 20 32 33 37 2c 20 20 33 31 32 2c 20 20  0,  237,  312,  
30ef8 32 38 32 2c 20 20 20 35 32 2c 20 20 20 35 32 2c  282,   52,   52,
30ef9 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
30efa 31 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20 20 32  1,   50,. /*   2
30efb 33 30 20 2a 2f 20 20 20 32 33 37 2c 20 20 34 39  30 */   237,  49
30efc 30 2c 20 20 31 38 33 2c 20 20 32 38 31 2c 20 20  0,  183,  281,  
30efd 35 34 37 2c 20 20 31 36 36 2c 20 20 34 33 39 2c  547,  166,  439,
30efe 20 20 35 36 35 2c 20 20 33 38 31 2c 20 20 33 37    565,  381,  37
30eff 38 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20  8,. /*   240 */ 
30f00 20 20 33 37 37 2c 20 20 35 39 36 2c 20 20 36 30    377,  596,  60
30f01 36 2c 20 20 20 36 37 2c 20 20 33 32 37 2c 20 20  6,   67,  327,  
30f02 31 37 32 2c 20 20 36 32 30 2c 20 20 35 39 36 2c  172,  620,  596,
30f03 20 20 33 37 36 2c 20 20 34 34 32 2c 0a 20 2f 2a    376,  442,. /*
30f04 20 20 20 32 35 30 20 2a 2f 20 20 20 32 33 36 2c     250 */   236,
30f05 20 20 36 32 30 2c 20 20 35 38 39 2c 20 20 35 38    620,  589,  58
30f06 33 2c 20 20 33 30 36 2c 20 20 34 32 33 2c 20 20  3,  306,  423,  
30f07 34 34 30 2c 20 20 33 33 39 2c 20 20 32 35 31 2c  440,  339,  251,
30f08 20 20 36 31 39 2c 0a 20 2f 2a 20 20 20 32 36 30    619,. /*   260
30f09 20 2a 2f 20 20 20 36 31 38 2c 20 20 33 33 31 2c   */   618,  331,
30f0a 20 20 35 37 34 2c 20 20 35 37 33 2c 20 20 20 20    574,  573,    
30f0b 37 2c 20 20 35 32 34 2c 20 20 31 39 34 2c 20 20  7,  524,  194,  
30f0c 34 38 31 2c 20 20 20 31 36 2c 20 20 35 39 34 2c  481,   16,  594,
30f0d 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20  . /*   270 */   
30f0e 31 38 39 2c 20 20 20 35 36 2c 20 20 20 35 37 2c  189,   56,   57,
30f0f 20 20 20 34 37 2c 20 20 35 38 31 2c 20 20 35 38     47,  581,  58
30f10 30 2c 20 20 35 38 32 2c 20 20 35 38 32 2c 20 20  0,  582,  582,  
30f11 20 35 34 2c 20 20 20 35 34 2c 0a 20 2f 2a 20 20   54,   54,. /*  
30f12 20 32 38 30 20 2a 2f 20 20 20 20 35 35 2c 20 20   280 */    55,  
30f13 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   55,   55,   55,
30f14 20 20 35 34 35 2c 20 20 20 35 33 2c 20 20 20 35    545,   53,   5
30f15 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  3,   53,   53,  
30f16 20 35 32 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a   52,. /*   290 *
30f17 2f 20 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20  /    52,   51,  
30f18 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c   51,   51,   50,
30f19 20 20 32 33 37 2c 20 20 33 31 32 2c 20 20 34 31    237,  312,  41
30f1a 30 2c 20 20 34 36 34 2c 20 20 34 32 31 2c 0a 20  0,  464,  421,. 
30f1b 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 35 39  /*   300 */   59
30f1c 32 2c 20 20 35 39 32 2c 20 20 35 39 32 2c 20 20  2,  592,  592,  
30f1d 36 37 31 2c 20 20 31 34 36 2c 20 20 34 31 30 2c  671,  146,  410,
30f1e 20 20 20 20 31 2c 20 20 32 30 35 2c 20 20 34 31      1,  205,  41
30f1f 30 2c 20 20 35 39 36 2c 0a 20 2f 2a 20 20 20 33  0,  596,. /*   3
30f20 31 30 20 2a 2f 20 20 20 36 32 32 2c 20 20 36 32  10 */   622,  62
30f21 31 2c 20 20 34 31 33 2c 20 20 34 32 30 2c 20 20  1,  413,  420,  
30f22 39 34 39 2c 20 20 35 39 36 2c 20 20 39 34 39 2c  949,  596,  949,
30f23 20 20 33 34 30 2c 20 20 32 33 36 2c 20 20 35 33    340,  236,  53
30f24 30 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20  0,. /*   320 */ 
30f25 20 20 34 31 33 2c 20 20 36 30 30 2c 20 20 20 37    413,  600,   7
30f26 34 2c 20 20 34 31 33 2c 20 20 32 33 36 2c 20 20  4,  413,  236,  
30f27 35 35 32 2c 20 20 35 38 39 2c 20 20 35 38 33 2c  552,  589,  583,
30f28 20 20 35 34 37 2c 20 20 36 30 30 2c 0a 20 2f 2a    547,  600,. /*
30f29 20 20 20 33 33 30 20 2a 2f 20 20 20 20 39 35 2c     330 */    95,
30f2a 20 20 20 36 38 2c 20 20 36 30 30 2c 20 20 20 38     68,  600,   8
30f2b 38 2c 20 20 35 35 31 2c 20 20 36 32 32 2c 20 20  8,  551,  622,  
30f2c 36 32 31 2c 20 20 34 36 35 2c 20 20 35 34 32 2c  621,  465,  542,
30f2d 20 20 20 33 38 2c 0a 20 2f 2a 20 20 20 33 34 30     38,. /*   340
30f2e 20 2a 2f 20 20 20 20 34 39 2c 20 20 35 39 39 2c   */    49,  599,
30f2f 20 20 20 34 36 2c 20 20 31 34 37 2c 20 20 34 36     46,  147,  46
30f30 35 2c 20 20 20 35 36 2c 20 20 20 35 37 2c 20 20  5,   56,   57,  
30f31 20 34 37 2c 20 20 35 38 31 2c 20 20 35 38 30 2c   47,  581,  580,
30f32 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20  . /*   350 */   
30f33 35 38 32 2c 20 20 35 38 32 2c 20 20 20 35 34 2c  582,  582,   54,
30f34 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20 20 35     54,   55,   5
30f35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  5,   55,   55,  
30f36 34 32 34 2c 20 20 20 35 33 2c 0a 20 2f 2a 20 20  424,   53,. /*  
30f37 20 33 36 30 20 2a 2f 20 20 20 20 35 33 2c 20 20   360 */    53,  
30f38 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c   53,   53,   52,
30f39 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20 20 35     52,   51,   5
30f3a 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20  1,   51,   50,  
30f3b 32 33 37 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a  237,. /*   370 *
30f3c 2f 20 20 20 33 31 32 2c 20 20 33 39 37 2c 20 20  /   312,  397,  
30f3d 33 39 35 2c 20 20 32 33 32 2c 20 20 35 32 39 2c  395,  232,  529,
30f3e 20 20 35 37 37 2c 20 20 33 38 37 2c 20 20 35 33    577,  387,  53
30f3f 33 2c 20 20 36 31 39 2c 20 20 36 31 38 2c 0a 20  3,  619,  618,. 
30f40 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 36 30  /*   380 */   60
30f41 35 2c 20 20 34 39 32 2c 20 20 35 36 30 2c 20 20  5,  492,  560,  
30f42 35 38 38 2c 20 20 35 38 37 2c 20 20 33 35 30 2c  588,  587,  350,
30f43 20 20 32 35 37 2c 20 20 36 32 32 2c 20 20 36 32    257,  622,  62
30f44 31 2c 20 20 34 39 35 2c 0a 20 2f 2a 20 20 20 33  1,  495,. /*   3
30f45 39 30 20 2a 2f 20 20 20 35 36 34 2c 20 20 33 35  90 */   564,  35
30f46 36 2c 20 20 33 35 30 2c 20 20 32 35 37 2c 20 20  6,  350,  257,  
30f47 20 34 39 2c 20 20 32 33 39 2c 20 20 20 34 36 2c   49,  239,   46,
30f48 20 20 31 34 37 2c 20 20 35 35 39 2c 20 20 33 35    147,  559,  35
30f49 37 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20  7,. /*   400 */ 
30f4a 20 20 35 38 39 2c 20 20 35 38 33 2c 20 20 32 33    589,  583,  23
30f4b 39 2c 20 20 36 31 39 2c 20 20 36 31 38 2c 20 20  9,  619,  618,  
30f4c 35 38 35 2c 20 20 35 38 34 2c 20 20 34 30 38 2c  585,  584,  408,
30f4d 20 20 32 35 38 2c 20 20 35 37 38 2c 0a 20 2f 2a    258,  578,. /*
30f4e 20 20 20 34 31 30 20 2a 2f 20 20 20 35 37 38 2c     410 */   578,
30f4f 20 20 36 37 32 2c 20 20 32 30 39 2c 20 20 20 33    672,  209,   3
30f50 35 2c 20 20 35 35 38 2c 20 20 32 35 38 2c 20 20  5,  558,  258,  
30f51 34 30 31 2c 20 20 36 32 32 2c 20 20 36 32 31 2c  401,  622,  621,
30f52 20 20 20 35 36 2c 0a 20 2f 2a 20 20 20 34 32 30     56,. /*   420
30f53 20 2a 2f 20 20 20 20 35 37 2c 20 20 20 34 37 2c   */    57,   47,
30f54 20 20 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38    581,  580,  58
30f55 32 2c 20 20 35 38 32 2c 20 20 20 35 34 2c 20 20  2,  582,   54,  
30f56 20 35 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   54,   55,   55,
30f57 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20  . /*   430 */   
30f58 20 35 35 2c 20 20 20 35 35 2c 20 20 35 38 36 2c   55,   55,  586,
30f59 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
30f5a 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20  3,   53,   52,  
30f5b 20 35 32 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20   52,   51,. /*  
30f5c 20 34 34 30 20 2a 2f 20 20 20 20 35 31 2c 20 20   440 */    51,  
30f5d 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c   51,   50,  237,
30f5e 20 20 33 31 32 2c 20 20 35 36 30 2c 20 20 35 39    312,  560,  59
30f5f 39 2c 20 20 34 31 30 2c 20 20 35 32 36 2c 20 20  9,  410,  526,  
30f60 35 33 31 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a  531,. /*   450 *
30f61 2f 20 20 20 31 38 34 2c 20 20 35 31 34 2c 20 20  /   184,  514,  
30f62 35 31 33 2c 20 20 34 37 34 2c 20 20 33 36 36 2c  513,  474,  366,
30f63 20 20 36 31 39 2c 20 20 36 31 38 2c 20 20 35 37    619,  618,  57
30f64 36 2c 20 20 34 31 30 2c 20 20 20 36 35 2c 0a 20  6,  410,   65,. 
30f65 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 31 37  /*   460 */   17
30f66 36 2c 20 20 35 35 39 2c 20 20 34 31 33 2c 20 20  6,  559,  413,  
30f67 34 30 38 2c 20 20 33 31 31 2c 20 20 35 37 38 2c  408,  311,  578,
30f68 20 20 35 37 38 2c 20 20 35 36 37 2c 20 20 34 39    578,  567,  49
30f69 31 2c 20 20 32 31 35 2c 0a 20 2f 2a 20 20 20 34  1,  215,. /*   4
30f6a 37 30 20 2a 2f 20 20 20 33 35 32 2c 20 20 36 30  70 */   352,  60
30f6b 30 2c 20 20 20 39 34 2c 20 20 34 31 33 2c 20 20  0,   94,  413,  
30f6c 35 38 39 2c 20 20 35 38 33 2c 20 20 34 37 34 2c  589,  583,  474,
30f6d 20 20 35 35 38 2c 20 20 34 30 38 2c 20 20 35 31    558,  408,  51
30f6e 38 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20  8,. /*   480 */ 
30f6f 20 20 35 37 38 2c 20 20 35 37 38 2c 20 20 36 30    578,  578,  60
30f70 30 2c 20 20 20 39 35 2c 20 20 34 37 30 2c 20 20  0,   95,  470,  
30f71 36 31 39 2c 20 20 36 31 38 2c 20 20 20 36 32 2c  619,  618,   62,
30f72 20 20 34 32 30 2c 20 20 39 34 38 2c 0a 20 2f 2a    420,  948,. /*
30f73 20 20 20 34 39 30 20 2a 2f 20 20 20 35 31 37 2c     490 */   517,
30f74 20 20 39 34 38 2c 20 20 33 34 39 2c 20 20 20 35    948,  349,   5
30f75 36 2c 20 20 20 35 37 2c 20 20 20 34 37 2c 20 20  6,   57,   47,  
30f76 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c  581,  580,  582,
30f77 20 20 35 38 32 2c 0a 20 2f 2a 20 20 20 35 30 30    582,. /*   500
30f78 20 2a 2f 20 20 20 20 35 34 2c 20 20 20 35 34 2c   */    54,   54,
30f79 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
30f7a 35 2c 20 20 20 35 35 2c 20 20 31 37 35 2c 20 20  5,   55,  175,  
30f7b 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
30f7c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20  . /*   510 */   
30f7d 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   53,   52,   52,
30f7e 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
30f7f 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20  1,   50,  237,  
30f80 33 31 32 2c 20 20 34 39 30 2c 0a 20 2f 2a 20 20  312,  490,. /*  
30f81 20 35 32 30 20 2a 2f 20 20 20 31 35 37 2c 20 20   520 */   157,  
30f82 34 31 30 2c 20 20 35 30 39 2c 20 20 32 39 32 2c  410,  509,  292,
30f83 20 20 33 39 33 2c 20 20 33 37 33 2c 20 20 33 34    393,  373,  34
30f84 38 2c 20 20 34 31 30 2c 20 20 36 32 33 2c 20 20  8,  410,  623,  
30f85 34 31 30 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a  410,. /*   530 *
30f86 2f 20 20 20 34 32 38 2c 20 20 20 36 37 2c 20 20  /   428,   67,  
30f87 36 31 31 2c 20 20 34 32 34 2c 20 20 36 32 30 2c  611,  424,  620,
30f88 20 20 34 31 30 2c 20 20 34 31 33 2c 20 20 35 34    410,  413,  54
30f89 30 2c 20 20 34 30 38 2c 20 20 31 37 31 2c 0a 20  0,  408,  171,. 
30f8a 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 35 37  /*   540 */   57
30f8b 38 2c 20 20 35 37 38 2c 20 20 34 31 33 2c 20 20  8,  578,  413,  
30f8c 36 32 30 2c 20 20 34 31 33 2c 20 20 36 30 30 2c  620,  413,  600,
30f8d 20 20 20 37 33 2c 20 20 36 32 30 2c 20 20 35 38     73,  620,  58
30f8e 39 2c 20 20 35 38 33 2c 0a 20 2f 2a 20 20 20 35  9,  583,. /*   5
30f8f 35 30 20 2a 2f 20 20 20 34 31 33 2c 20 20 36 30  50 */   413,  60
30f90 30 2c 20 20 20 38 30 2c 20 20 36 30 30 2c 20 20  0,   80,  600,  
30f91 20 38 38 2c 20 20 32 33 38 2c 20 20 31 36 38 2c   88,  238,  168,
30f92 20 20 33 30 36 2c 20 20 34 32 32 2c 20 20 36 30    306,  422,  60
30f93 30 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20  0,. /*   560 */ 
30f94 20 20 20 38 30 2c 20 20 32 30 31 2c 20 20 20 31     80,  201,   1
30f95 38 2c 20 20 34 36 38 2c 20 20 34 30 36 2c 20 20  8,  468,  406,  
30f96 35 37 34 2c 20 20 35 37 33 2c 20 20 20 35 36 2c  574,  573,   56,
30f97 20 20 20 35 37 2c 20 20 20 34 37 2c 0a 20 2f 2a     57,   47,. /*
30f98 20 20 20 35 37 30 20 2a 2f 20 20 20 35 38 31 2c     570 */   581,
30f99 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35 38    580,  582,  58
30f9a 32 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 20 20  2,   54,   54,  
30f9b 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   55,   55,   55,
30f9c 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20 35 38 30     55,. /*   580
30f9d 20 2a 2f 20 20 20 35 37 39 2c 20 20 20 35 33 2c   */   579,   53,
30f9e 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
30f9f 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20 20  3,   52,   52,  
30fa0 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c   51,   51,   51,
30fa1 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20  . /*   590 */   
30fa2 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32 2c   50,  237,  312,
30fa3 20 20 20 34 34 2c 20 20 32 33 33 2c 20 20 35 39     44,  233,  59
30fa4 39 2c 20 20 32 37 31 2c 20 20 33 32 30 2c 20 20  9,  271,  320,  
30fa5 33 34 31 2c 20 20 34 37 32 2c 0a 20 2f 2a 20 20  341,  472,. /*  
30fa6 20 36 30 30 20 2a 2f 20 20 20 34 31 30 2c 20 20   600 */   410,  
30fa7 38 37 34 2c 20 20 34 32 31 2c 20 20 34 37 33 2c  874,  421,  473,
30fa8 20 20 35 30 33 2c 20 20 33 31 39 2c 20 20 34 31    503,  319,  41
30fa9 30 2c 20 20 32 30 30 2c 20 20 31 34 34 2c 20 20  0,  200,  144,  
30faa 20 36 36 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a   66,. /*   610 *
30fab 2f 20 20 20 33 32 37 2c 20 20 34 38 33 2c 20 20  /   327,  483,  
30fac 35 30 38 2c 20 20 35 39 36 2c 20 20 32 37 34 2c  508,  596,  274,
30fad 20 20 34 31 33 2c 20 20 32 33 39 2c 20 20 33 36    413,  239,  36
30fae 34 2c 20 20 34 38 34 2c 20 20 33 38 32 2c 0a 20  4,  484,  382,. 
30faf 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 20 33  /*   620 */    3
30fb0 30 2c 20 20 34 31 33 2c 20 20 35 38 39 2c 20 20  0,  413,  589,  
30fb1 35 38 33 2c 20 20 36 30 30 2c 20 20 20 36 39 2c  583,  600,   69,
30fb2 20 20 35 30 32 2c 20 20 32 33 36 2c 20 20 33 34    502,  236,  34
30fb3 32 2c 20 20 35 37 35 2c 0a 20 2f 2a 20 20 20 36  2,  575,. /*   6
30fb4 33 30 20 2a 2f 20 20 20 36 30 30 2c 20 20 20 39  30 */   600,   9
30fb5 37 2c 20 20 31 39 39 2c 20 20 31 39 38 2c 20 20  7,  199,  198,  
30fb6 32 30 39 2c 20 20 39 35 39 2c 20 20 31 38 36 2c  209,  959,  186,
30fb7 20 20 34 31 38 2c 20 20 20 20 32 2c 20 20 35 36    418,    2,  56
30fb8 36 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20  6,. /*   640 */ 
30fb9 20 20 32 36 39 2c 20 20 20 35 36 2c 20 20 20 35    269,   56,   5
30fba 37 2c 20 20 20 34 37 2c 20 20 35 38 31 2c 20 20  7,   47,  581,  
30fbb 35 38 30 2c 20 20 35 38 32 2c 20 20 35 38 32 2c  580,  582,  582,
30fbc 20 20 20 35 34 2c 20 20 20 35 34 2c 0a 20 2f 2a     54,   54,. /*
30fbd 20 20 20 36 35 30 20 2a 2f 20 20 20 20 35 35 2c     650 */    55,
30fbe 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
30fbf 35 2c 20 20 34 31 30 2c 20 20 20 35 33 2c 20 20  5,  410,   53,  
30fc0 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c   53,   53,   53,
30fc1 20 20 20 35 32 2c 0a 20 2f 2a 20 20 20 36 36 30     52,. /*   660
30fc2 20 2a 2f 20 20 20 20 35 32 2c 20 20 20 35 31 2c   */    52,   51,
30fc3 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
30fc4 30 2c 20 20 32 33 37 2c 20 20 33 31 32 2c 20 20  0,  237,  312,  
30fc5 32 36 33 2c 20 20 35 39 39 2c 20 20 34 31 33 2c  263,  599,  413,
30fc6 0a 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20 20 20  . /*   670 */   
30fc7 34 31 30 2c 20 20 20 32 31 2c 20 20 31 39 30 2c  410,   21,  190,
30fc8 20 20 33 35 38 2c 20 20 34 31 30 2c 20 20 33 32    358,  410,  32
30fc9 36 2c 20 20 34 31 30 2c 20 20 32 30 32 2c 20 20  6,  410,  202,  
30fca 36 30 30 2c 20 20 31 30 30 2c 0a 20 2f 2a 20 20  600,  100,. /*  
30fcb 20 36 38 30 20 2a 2f 20 20 20 33 38 36 2c 20 20   680 */   386,  
30fcc 35 39 36 2c 20 20 36 32 30 2c 20 20 35 36 32 2c  596,  620,  562,
30fcd 20 20 32 36 35 2c 20 20 34 31 33 2c 20 20 32 36    265,  413,  26
30fce 37 2c 20 20 34 31 30 2c 20 20 36 32 30 2c 20 20  7,  410,  620,  
30fcf 34 31 33 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a  413,. /*   690 *
30fd0 2f 20 20 20 35 36 33 2c 20 20 34 31 33 2c 20 20  /   563,  413,  
30fd1 33 35 32 2c 20 20 20 20 34 2c 20 20 36 30 30 2c  352,    4,  600,
30fd2 20 20 20 39 38 2c 20 20 35 38 39 2c 20 20 35 38     98,  589,  58
30fd3 33 2c 20 20 36 30 30 2c 20 20 31 30 36 2c 0a 20  3,  600,  106,. 
30fd4 2f 2a 20 20 20 37 30 30 20 2a 2f 20 20 20 36 30  /*   700 */   60
30fd5 30 2c 20 20 31 30 34 2c 20 20 34 31 33 2c 20 20  0,  104,  413,  
30fd6 31 37 34 2c 20 20 36 30 31 2c 20 20 36 32 39 2c  174,  601,  629,
30fd7 20 20 36 32 37 2c 20 20 33 33 33 2c 20 20 36 30    627,  333,  60
30fd8 32 2c 20 20 20 33 34 2c 0a 20 2f 2a 20 20 20 37  2,   34,. /*   7
30fd9 31 30 20 2a 2f 20 20 20 33 33 37 2c 20 20 36 30  10 */   337,  60
30fda 30 2c 20 20 31 30 38 2c 20 20 35 36 31 2c 20 20  0,  108,  561,  
30fdb 33 35 39 2c 20 20 20 35 36 2c 20 20 20 35 37 2c  359,   56,   57,
30fdc 20 20 20 34 37 2c 20 20 35 38 31 2c 20 20 35 38     47,  581,  58
30fdd 30 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20  0,. /*   720 */ 
30fde 20 20 35 38 32 2c 20 20 35 38 32 2c 20 20 20 35    582,  582,   5
30fdf 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20  4,   54,   55,  
30fe0 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c   55,   55,   55,
30fe1 20 20 34 31 30 2c 20 20 20 35 33 2c 0a 20 2f 2a    410,   53,. /*
30fe2 20 20 20 37 33 30 20 2a 2f 20 20 20 20 35 33 2c     730 */    53,
30fe3 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
30fe4 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c 20 20  2,   52,   51,  
30fe5 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35 30 2c   51,   51,   50,
30fe6 20 20 32 33 37 2c 0a 20 2f 2a 20 20 20 37 34 30    237,. /*   740
30fe7 20 2a 2f 20 20 20 33 31 32 2c 20 20 34 31 30 2c   */   312,  410,
30fe8 20 20 34 39 39 2c 20 20 34 31 33 2c 20 20 31 36    499,  413,  16
30fe9 37 2c 20 20 35 36 37 2c 20 20 34 30 35 2c 20 20  7,  567,  405,  
30fea 32 31 35 2c 20 20 35 30 34 2c 20 20 35 30 35 2c  215,  504,  505,
30feb 0a 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20 20 20  . /*   750 */   
30fec 33 31 36 2c 20 20 35 35 37 2c 20 20 36 30 30 2c  316,  557,  600,
30fed 20 20 31 30 39 2c 20 20 33 35 33 2c 20 20 20 31    109,  353,   1
30fee 33 2c 20 20 34 31 33 2c 20 20 34 31 30 2c 20 20  3,  413,  410,  
30fef 20 31 32 2c 20 20 34 31 30 2c 0a 20 2f 2a 20 20   12,  410,. /*  
30ff0 20 37 36 30 20 2a 2f 20 20 20 35 33 38 2c 20 20   760 */   538,  
30ff1 34 31 30 2c 20 20 33 33 35 2c 20 20 33 35 38 2c  410,  335,  358,
30ff2 20 20 32 32 33 2c 20 20 36 30 30 2c 20 20 31 33    223,  600,  13
30ff3 34 2c 20 20 35 37 31 2c 20 20 35 37 31 2c 20 20  4,  571,  571,  
30ff4 36 32 30 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a  620,. /*   770 *
30ff5 2f 20 20 20 35 38 39 2c 20 20 35 38 33 2c 20 20  /   589,  583,  
30ff6 34 31 33 2c 20 20 20 32 30 2c 20 20 34 31 33 2c  413,   20,  413,
30ff7 20 20 36 32 30 2c 20 20 34 31 33 2c 20 20 32 37    620,  413,  27
30ff8 32 2c 20 20 36 32 30 2c 20 20 31 36 37 2c 0a 20  2,  620,  167,. 
30ff9 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20 20 31 36  /*   780 */   16
30ffa 37 2c 20 20 36 30 30 2c 20 20 31 33 35 2c 20 20  7,  600,  135,  
30ffb 36 30 30 2c 20 20 20 36 31 2c 20 20 36 30 30 2c  600,   61,  600,
30ffc 20 20 31 30 35 2c 20 20 33 31 37 2c 20 20 31 34    105,  317,  14
30ffd 38 2c 20 20 20 35 36 2c 0a 20 2f 2a 20 20 20 37  8,   56,. /*   7
30ffe 39 30 20 2a 2f 20 20 20 20 35 37 2c 20 20 20 34  90 */    57,   4
30fff 37 2c 20 20 35 38 31 2c 20 20 35 38 30 2c 20 20  7,  581,  580,  
31000 35 38 32 2c 20 20 35 38 32 2c 20 20 20 35 34 2c  582,  582,   54,
31001 20 20 20 35 34 2c 20 20 20 35 35 2c 20 20 20 35     54,   55,   5
31002 35 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20  5,. /*   800 */ 
31003 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 34 31     55,   55,  41
31004 30 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  0,   53,   53,  
31005 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35 32 2c   53,   53,   52,
31006 20 20 20 35 32 2c 20 20 20 35 31 2c 0a 20 2f 2a     52,   51,. /*
31007 20 20 20 38 31 30 20 2a 2f 20 20 20 20 35 31 2c     810 */    51,
31008 20 20 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33     51,   50,  23
31009 37 2c 20 20 33 31 32 2c 20 20 34 31 30 2c 20 20  7,  312,  410,  
3100a 32 37 35 2c 20 20 34 31 33 2c 20 20 34 31 30 2c  275,  413,  410,
3100b 20 20 32 37 35 2c 0a 20 2f 2a 20 20 20 38 32 30    275,. /*   820
3100c 20 2a 2f 20 20 20 32 37 35 2c 20 20 32 32 32 2c   */   275,  222,
3100d 20 20 34 31 30 2c 20 20 33 33 30 2c 20 20 33 36    410,  330,  36
3100e 33 2c 20 20 35 34 34 2c 20 20 36 30 30 2c 20 20  3,  544,  600,  
3100f 31 30 33 2c 20 20 31 33 32 2c 20 20 33 36 30 2c  103,  132,  360,
31010 0a 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20 20 20  . /*   830 */   
31011 34 31 33 2c 20 20 36 32 30 2c 20 20 35 32 32 2c  413,  620,  522,
31012 20 20 34 31 33 2c 20 20 36 32 30 2c 20 20 36 32    413,  620,  62
31013 30 2c 20 20 34 31 30 2c 20 20 34 31 33 2c 20 20  0,  410,  413,  
31014 31 37 30 2c 20 20 36 30 30 2c 0a 20 2f 2a 20 20  170,  600,. /*  
31015 20 38 34 30 20 2a 2f 20 20 20 20 39 36 2c 20 20   840 */    96,  
31016 36 30 33 2c 20 20 36 30 30 2c 20 20 31 30 32 2c  603,  600,  102,
31017 20 20 35 38 39 2c 20 20 35 38 33 2c 20 20 36 30    589,  583,  60
31018 30 2c 20 20 20 37 37 2c 20 20 33 37 34 2c 20 20  0,   77,  374,  
31019 35 33 36 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a  536,. /*   850 *
3101a 2f 20 20 20 31 36 37 2c 20 20 34 31 33 2c 20 20  /   167,  413,  
3101b 31 34 33 2c 20 20 33 32 35 2c 20 20 32 35 36 2c  143,  325,  256,
3101c 20 20 20 32 38 2c 20 20 32 32 34 2c 20 20 33 32     28,  224,  32
3101d 34 2c 20 20 35 31 31 2c 20 20 35 32 38 2c 0a 20  4,  511,  528,. 
3101e 2f 2a 20 20 20 38 36 30 20 2a 2f 20 20 20 36 30  /*   860 */   60
3101f 30 2c 20 20 20 39 39 2c 20 20 35 32 37 2c 20 20  0,   99,  527,  
31020 20 35 36 2c 20 20 20 35 37 2c 20 20 20 34 37 2c   56,   57,   47,
31021 20 20 35 38 31 2c 20 20 35 38 30 2c 20 20 35 38    581,  580,  58
31022 32 2c 20 20 35 38 32 2c 0a 20 2f 2a 20 20 20 38  2,  582,. /*   8
31023 37 30 20 2a 2f 20 20 20 20 35 34 2c 20 20 20 35  70 */    54,   5
31024 34 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  4,   55,   55,  
31025 20 35 35 2c 20 20 20 35 35 2c 20 20 34 31 30 2c   55,   55,  410,
31026 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
31027 33 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20  3,. /*   880 */ 
31028 20 20 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35     53,   52,   5
31029 32 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20  2,   51,   51,  
3102a 20 35 31 2c 20 20 20 35 30 2c 20 20 32 33 37 2c   51,   50,  237,
3102b 20 20 33 31 32 2c 20 20 34 31 30 2c 0a 20 2f 2a    312,  410,. /*
3102c 20 20 20 38 39 30 20 2a 2f 20 20 20 32 37 35 2c     890 */   275,
3102d 20 20 34 31 33 2c 20 20 34 31 30 2c 20 20 34 36    413,  410,  46
3102e 39 2c 20 20 32 37 35 2c 20 20 31 36 37 2c 20 20  9,  275,  167,  
3102f 34 35 38 2c 20 20 20 33 39 2c 20 20 31 37 31 2c  458,   39,  171,
31030 20 20 20 33 37 2c 0a 20 2f 2a 20 20 20 39 30 30     37,. /*   900
31031 20 2a 2f 20 20 20 36 30 30 2c 20 20 31 33 38 2c   */   600,  138,
31032 20 20 32 31 34 2c 20 20 31 34 34 2c 20 20 34 31    214,  144,  41
31033 33 2c 20 20 36 32 30 2c 20 20 31 34 32 2c 20 20  3,  620,  142,  
31034 34 31 33 2c 20 20 34 31 30 2c 20 20 36 32 30 2c  413,  410,  620,
31035 0a 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20 20 20  . /*   910 */   
31036 34 31 30 2c 20 20 33 35 38 2c 20 20 32 33 39 2c  410,  358,  239,
31037 20 20 36 30 30 2c 20 20 31 33 37 2c 20 20 32 33    600,  137,  23
31038 30 2c 20 20 36 30 30 2c 20 20 31 33 36 2c 20 20  0,  600,  136,  
31039 35 38 39 2c 20 20 35 38 33 2c 0a 20 2f 2a 20 20  589,  583,. /*  
3103a 20 39 32 30 20 2a 2f 20 20 20 34 35 37 2c 20 20   920 */   457,  
3103b 32 36 33 2c 20 20 20 32 33 2c 20 20 34 31 33 2c  263,   23,  413,
3103c 20 20 33 35 31 2c 20 20 34 31 33 2c 20 20 36 32    351,  413,  62
3103d 30 2c 20 20 33 32 33 2c 20 20 34 34 35 2c 20 20  0,  323,  445,  
3103e 35 30 31 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a  501,. /*   930 *
3103f 2f 20 20 20 20 32 33 2c 20 20 33 32 32 2c 20 20  /    23,  322,  
31040 36 30 30 2c 20 20 20 37 36 2c 20 20 36 30 30 2c  600,   76,  600,
31041 20 20 20 39 33 2c 20 20 36 32 30 2c 20 20 20 35     93,  620,   5
31042 36 2c 20 20 20 34 35 2c 20 20 20 34 37 2c 0a 20  6,   45,   47,. 
31043 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20 20 35 38  /*   940 */   58
31044 31 2c 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20  1,  580,  582,  
31045 35 38 32 2c 20 20 20 35 34 2c 20 20 20 35 34 2c  582,   54,   54,
31046 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
31047 35 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20 39  5,   55,. /*   9
31048 35 30 20 2a 2f 20 20 20 34 31 30 2c 20 20 20 35  50 */   410,   5
31049 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  3,   53,   53,  
3104a 20 35 33 2c 20 20 20 35 32 2c 20 20 20 35 32 2c   53,   52,   52,
3104b 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
3104c 31 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20  1,. /*   960 */ 
3104d 20 20 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31     50,  237,  31
3104e 32 2c 20 20 34 31 30 2c 20 20 32 36 32 2c 20 20  2,  410,  262,  
3104f 34 31 33 2c 20 20 34 31 30 2c 20 20 34 32 36 2c  413,  410,  426,
31050 20 20 32 36 33 2c 20 20 33 30 38 2c 0a 20 2f 2a    263,  308,. /*
31051 20 20 20 39 37 30 20 2a 2f 20 20 20 32 30 33 2c     970 */   203,
31052 20 20 32 31 33 2c 20 20 32 31 32 2c 20 20 33 38    213,  212,  38
31053 30 2c 20 20 36 30 30 2c 20 20 20 39 32 2c 20 20  0,  600,   92,  
31054 35 32 30 2c 20 20 35 31 39 2c 20 20 34 31 33 2c  520,  519,  413,
31055 20 20 31 33 30 2c 0a 20 2f 2a 20 20 20 39 38 30    130,. /*   980
31056 20 2a 2f 20 20 20 35 33 38 2c 20 20 34 31 33 2c   */   538,  413,
31057 20 20 35 33 38 2c 20 20 36 32 30 2c 20 20 34 31    538,  620,  41
31058 30 2c 20 20 36 32 38 2c 20 20 20 20 32 2c 20 20  0,  628,    2,  
31059 36 30 30 2c 20 20 20 37 35 2c 20 20 32 37 33 2c  600,   75,  273,
3105a 0a 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20 20 20  . /*   990 */   
3105b 36 30 30 2c 20 20 20 39 31 2c 20 20 35 38 39 2c  600,   91,  589,
3105c 20 20 35 38 33 2c 20 20 33 37 35 2c 20 20 36 32    583,  375,  62
3105d 30 2c 20 20 31 32 39 2c 20 20 36 32 30 2c 20 20  0,  129,  620,  
3105e 20 32 37 2c 20 20 34 31 33 2c 0a 20 2f 2a 20 20   27,  413,. /*  
3105f 31 30 30 30 20 2a 2f 20 20 20 34 32 35 2c 20 20  1000 */   425,  
31060 33 30 37 2c 20 20 32 32 31 2c 20 20 31 32 38 2c  307,  221,  128,
31061 20 20 35 39 39 2c 20 20 35 39 39 2c 20 20 35 39    599,  599,  59
31062 39 2c 20 20 32 38 31 2c 20 20 36 30 30 2c 20 20  9,  281,  600,  
31063 20 39 30 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a   90,. /*  1010 *
31064 2f 20 20 20 33 37 31 2c 20 20 34 35 32 2c 20 20  /   371,  452,  
31065 20 35 37 2c 20 20 20 34 37 2c 20 20 35 38 31 2c   57,   47,  581,
31066 20 20 35 38 30 2c 20 20 35 38 32 2c 20 20 35 38    580,  582,  58
31067 32 2c 20 20 20 35 34 2c 20 20 20 35 34 2c 0a 20  2,   54,   54,. 
31068 2f 2a 20 20 31 30 32 30 20 2a 2f 20 20 20 20 35  /*  1020 */    5
31069 35 2c 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20  5,   55,   55,  
3106a 20 35 35 2c 20 20 34 31 30 2c 20 20 20 35 33 2c   55,  410,   53,
3106b 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20 20 35     53,   53,   5
3106c 33 2c 20 20 20 35 32 2c 0a 20 2f 2a 20 20 31 30  3,   52,. /*  10
3106d 33 30 20 2a 2f 20 20 20 20 35 32 2c 20 20 20 35  30 */    52,   5
3106e 31 2c 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20  1,   51,   51,  
3106f 20 35 30 2c 20 20 32 33 37 2c 20 20 33 31 32 2c   50,  237,  312,
31070 20 20 34 31 30 2c 20 20 32 36 33 2c 20 20 34 31    410,  263,  41
31071 33 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20  3,. /*  1040 */ 
31072 20 20 34 31 30 2c 20 20 32 36 33 2c 20 20 32 36    410,  263,  26
31073 33 2c 20 20 33 36 35 2c 20 20 32 30 38 2c 20 20  3,  365,  208,  
31074 33 32 31 2c 20 20 32 30 36 2c 20 20 35 34 32 2c  321,  206,  542,
31075 20 20 36 30 30 2c 20 20 31 30 31 2c 0a 20 2f 2a    600,  101,. /*
31076 20 20 31 30 35 30 20 2a 2f 20 20 20 20 35 30 2c    1050 */    50,
31077 20 20 32 33 37 2c 20 20 34 31 33 2c 20 20 36 32    237,  413,  62
31078 30 2c 20 20 36 31 30 2c 20 20 34 31 33 2c 20 20  0,  610,  413,  
31079 36 32 30 2c 20 20 36 32 30 2c 20 20 34 31 30 2c  620,  620,  410,
3107a 20 20 35 34 32 2c 0a 20 2f 2a 20 20 31 30 36 30    542,. /*  1060
3107b 20 2a 2f 20 20 20 31 36 35 2c 20 20 36 30 30 2c   */   165,  600,
3107c 20 20 20 38 39 2c 20 20 31 38 38 2c 20 20 36 30     89,  188,  60
3107d 30 2c 20 20 20 38 37 2c 20 20 35 38 39 2c 20 20  0,   87,  589,  
3107e 35 38 33 2c 20 20 34 37 38 2c 20 20 36 32 30 2c  583,  478,  620,
3107f 0a 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20 20 20  . /*  1070 */   
31080 34 36 37 2c 20 20 35 31 39 2c 20 20 31 32 35 2c  467,  519,  125,
31081 20 20 34 31 33 2c 20 20 35 36 39 2c 20 20 32 33    413,  569,  23
31082 35 2c 20 20 35 34 32 2c 20 20 33 36 37 2c 20 20  5,  542,  367,  
31083 35 39 39 2c 20 20 34 37 35 2c 0a 20 2f 2a 20 20  599,  475,. /*  
31084 31 30 38 30 20 2a 2f 20 20 20 35 39 39 2c 20 20  1080 */   599,  
31085 34 35 30 2c 20 20 36 30 30 2c 20 20 20 38 36 2c  450,  600,   86,
31086 20 20 34 34 39 2c 20 20 34 34 38 2c 20 20 32 33    449,  448,  23
31087 31 2c 20 20 20 34 37 2c 20 20 35 38 31 2c 20 20  1,   47,  581,  
31088 35 38 30 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a  580,. /*  1090 *
31089 2f 20 20 20 35 38 32 2c 20 20 35 38 32 2c 20 20  /   582,  582,  
3108a 20 35 34 2c 20 20 20 35 34 2c 20 20 20 35 35 2c   54,   54,   55,
3108b 20 20 20 35 35 2c 20 20 20 35 35 2c 20 20 20 35     55,   55,   5
3108c 35 2c 20 20 32 38 37 2c 20 20 20 35 33 2c 0a 20  5,  287,   53,. 
3108d 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20 20 20 35  /*  1100 */    5
3108e 33 2c 20 20 20 35 33 2c 20 20 20 35 33 2c 20 20  3,   53,   53,  
3108f 20 35 32 2c 20 20 20 35 32 2c 20 20 20 35 31 2c   52,   52,   51,
31090 20 20 20 35 31 2c 20 20 20 35 31 2c 20 20 20 35     51,   51,   5
31091 30 2c 20 20 32 33 37 2c 0a 20 2f 2a 20 20 31 31  0,  237,. /*  11
31092 31 30 20 2a 2f 20 20 20 20 34 33 2c 20 20 34 30  10 */    43,  40
31093 34 2c 20 20 34 31 30 2c 20 20 20 20 33 2c 20 20  4,  410,    3,  
31094 34 31 30 2c 20 20 32 38 35 2c 20 20 32 36 30 2c  410,  285,  260,
31095 20 20 34 31 34 2c 20 20 36 32 31 2c 20 20 32 36    414,  621,  26
31096 33 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20  3,. /*  1120 */ 
31097 20 20 36 30 39 2c 20 20 36 32 37 2c 20 20 33 33    609,  627,  33
31098 33 2c 20 20 20 34 33 2c 20 20 34 30 34 2c 20 20  3,   43,  404,  
31099 34 31 30 2c 20 20 20 20 33 2c 20 20 34 31 33 2c  410,    3,  413,
3109a 20 20 34 30 37 2c 20 20 34 31 33 2c 0a 20 2f 2a    407,  413,. /*
3109b 20 20 31 31 33 30 20 2a 2f 20 20 20 34 31 34 2c    1130 */   414,
3109c 20 20 36 32 31 2c 20 20 31 37 31 2c 20 20 32 36    621,  171,  26
3109d 33 2c 20 20 36 32 30 2c 20 20 36 32 30 2c 20 20  3,  620,  620,  
3109e 36 30 30 2c 20 20 20 38 35 2c 20 20 36 30 30 2c  600,   85,  600,
3109f 20 20 20 37 32 2c 0a 20 2f 2a 20 20 31 31 34 30     72,. /*  1140
310a0 20 2a 2f 20 20 20 34 31 33 2c 20 20 34 30 37 2c   */   413,  407,
310a1 20 20 31 32 34 2c 20 20 31 34 30 2c 20 20 33 35    124,  140,  35
310a2 33 2c 20 20 36 30 34 2c 20 20 34 30 39 2c 20 20  3,  604,  409,  
310a3 34 30 32 2c 20 20 36 32 30 2c 20 20 36 30 30 2c  402,  620,  600,
310a4 0a 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20 20 20  . /*  1150 */   
310a5 20 37 31 2c 20 20 32 39 31 2c 20 20 34 37 31 2c   71,  291,  471,
310a6 20 20 34 39 35 2c 20 20 31 36 30 2c 20 20 31 32    495,  160,  12
310a7 33 2c 20 20 35 39 33 2c 20 20 35 36 35 2c 20 20  3,  593,  565,  
310a8 36 32 30 2c 20 20 36 32 30 2c 0a 20 2f 2a 20 20  620,  620,. /*  
310a9 31 31 36 30 20 2a 2f 20 20 20 34 30 32 2c 20 20  1160 */   402,  
310aa 36 32 30 2c 20 20 32 32 30 2c 20 20 20 31 35 2c  620,  220,   15,
310ab 20 20 34 36 33 2c 20 20 34 36 30 2c 20 20 36 32    463,  460,  62
310ac 30 2c 20 20 34 31 37 2c 20 20 36 32 35 2c 20 20  0,  417,  625,  
310ad 31 35 39 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a  159,. /*  1170 *
310ae 2f 20 20 20 35 36 35 2c 20 20 36 32 30 2c 20 20  /   565,  620,  
310af 33 39 39 2c 20 20 32 34 30 2c 20 20 31 35 38 2c  399,  240,  158,
310b0 20 20 31 32 36 2c 20 20 32 31 39 2c 20 20 20 34    126,  219,   4
310b1 30 2c 20 20 20 34 31 2c 20 20 35 33 32 2c 0a 20  0,   41,  532,. 
310b2 2f 2a 20 20 31 31 38 30 20 2a 2f 20 20 20 34 31  /*  1180 */   41
310b3 30 2c 20 20 32 30 37 2c 20 20 31 32 31 2c 20 20  0,  207,  121,  
310b4 31 32 30 2c 20 20 20 34 32 2c 20 20 34 31 32 2c  120,   42,  412,
310b5 20 20 34 31 31 2c 20 20 36 32 30 2c 20 20 32 36    411,  620,  26
310b6 33 2c 20 20 35 39 34 2c 0a 20 2f 2a 20 20 31 31  3,  594,. /*  11
310b7 39 30 20 2a 2f 20 20 20 20 34 30 2c 20 20 20 34  90 */    40,   4
310b8 31 2c 20 20 35 35 36 2c 20 20 35 34 33 2c 20 20  1,  556,  543,  
310b9 20 32 35 2c 20 20 34 31 33 2c 20 20 20 31 31 2c   25,  413,   11,
310ba 20 20 20 34 32 2c 20 20 34 31 32 2c 20 20 34 31     42,  412,  41
310bb 31 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20  1,. /*  1200 */ 
310bc 20 20 20 32 34 2c 20 20 31 31 38 2c 20 20 35 39     24,  118,  59
310bd 34 2c 20 20 36 32 30 2c 20 20 36 30 30 2c 20 20  4,  620,  600,  
310be 20 38 34 2c 20 20 34 35 35 2c 20 20 36 32 30 2c   84,  455,  620,
310bf 20 20 36 32 30 2c 20 20 20 34 33 2c 0a 20 2f 2a    620,   43,. /*
310c0 20 20 31 32 31 30 20 2a 2f 20 20 20 34 30 34 2c    1210 */   404,
310c1 20 20 32 31 38 2c 20 20 20 20 33 2c 20 20 35 33    218,    3,  53
310c2 39 2c 20 20 31 35 36 2c 20 20 35 39 39 2c 20 20  9,  156,  599,  
310c3 34 31 34 2c 20 20 36 32 31 2c 20 20 31 31 33 2c  414,  621,  113,
310c4 20 20 32 33 39 2c 0a 20 2f 2a 20 20 31 32 32 30    239,. /*  1220
310c5 20 2a 2f 20 20 20 35 39 32 2c 20 20 35 39 32 2c   */   592,  592,
310c6 20 20 35 39 32 2c 20 20 35 39 31 2c 20 20 35 39    592,  591,  59
310c7 30 2c 20 20 20 31 34 2c 20 20 31 35 35 2c 20 20  0,   14,  155,  
310c8 34 30 37 2c 20 20 36 32 30 2c 20 20 35 33 37 2c  407,  620,  537,
310c9 0a 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20 20 20  . /*  1230 */   
310ca 34 35 31 2c 20 20 32 34 37 2c 20 20 34 34 34 2c  451,  247,  444,
310cb 20 20 35 39 32 2c 20 20 35 39 32 2c 20 20 35 39    592,  592,  59
310cc 32 2c 20 20 35 39 31 2c 20 20 35 39 30 2c 20 20  2,  591,  590,  
310cd 20 31 34 2c 20 20 33 34 33 2c 0a 20 2f 2a 20 20   14,  343,. /*  
310ce 31 32 34 30 20 2a 2f 20 20 20 34 31 30 2c 20 20  1240 */   410,  
310cf 31 31 31 2c 20 20 34 31 30 2c 20 20 32 37 37 2c  111,  410,  277,
310d0 20 20 36 32 30 2c 20 20 34 31 30 2c 20 20 34 30    620,  410,  40
310d1 32 2c 20 20 34 31 30 2c 20 20 35 30 37 2c 20 20  2,  410,  507,  
310d2 31 31 30 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a  110,. /*  1250 *
310d3 2f 20 20 20 20 31 30 2c 20 20 20 36 34 2c 20 20  /    10,   64,  
310d4 32 30 34 2c 20 20 33 33 36 2c 20 20 34 33 35 2c  204,  336,  435,
310d5 20 20 34 31 33 2c 20 20 35 36 35 2c 20 20 34 31    413,  565,  41
310d6 33 2c 20 20 36 32 30 2c 20 20 32 37 36 2c 0a 20  3,  620,  276,. 
310d7 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20 20 34 31  /*  1260 */   41
310d8 33 2c 20 20 34 33 34 2c 20 20 34 31 33 2c 20 20  3,  434,  413,  
310d9 36 32 30 2c 20 20 36 30 30 2c 20 20 20 38 33 2c  620,  600,   83,
310da 20 20 36 30 30 2c 20 20 20 39 35 2c 20 20 33 33    600,   95,  33
310db 34 2c 20 20 36 30 30 2c 0a 20 2f 2a 20 20 31 32  4,  600,. /*  12
310dc 37 30 20 2a 2f 20 20 20 20 38 32 2c 20 20 36 30  70 */    82,  60
310dd 30 2c 20 20 20 38 31 2c 20 20 31 35 30 2c 20 20  0,   81,  150,  
310de 36 32 30 2c 20 20 34 38 38 2c 20 20 20 34 30 2c  620,  488,   40,
310df 20 20 20 34 31 2c 20 20 32 37 30 2c 20 20 32 36     41,  270,  26
310e0 38 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20  8,. /*  1280 */ 
310e1 20 20 32 36 36 2c 20 20 31 39 31 2c 20 20 33 33    266,  191,  33
310e2 32 2c 20 20 20 34 32 2c 20 20 34 31 32 2c 20 20  2,   42,  412,  
310e3 34 31 31 2c 20 20 35 39 39 2c 20 20 34 31 30 2c  411,  599,  410,
310e4 20 20 35 39 34 2c 20 20 32 34 31 2c 0a 20 2f 2a    594,  241,. /*
310e5 20 20 31 32 39 30 20 2a 2f 20 20 20 36 32 30 2c    1290 */   620,
310e6 20 20 34 31 30 2c 20 20 32 36 34 2c 20 20 36 32    410,  264,  62
310e7 30 2c 20 20 36 32 30 2c 20 20 36 32 30 2c 20 20  0,  620,  620,  
310e8 20 33 33 2c 20 20 34 30 34 2c 20 20 34 31 39 2c   33,  404,  419,
310e9 20 20 20 20 33 2c 0a 20 2f 2a 20 20 31 33 30 30      3,. /*  1300
310ea 20 2a 2f 20 20 20 31 30 37 2c 20 20 32 32 39 2c   */   107,  229,
310eb 20 20 34 31 33 2c 20 20 34 31 34 2c 20 20 36 32    413,  414,  62
310ec 31 2c 20 20 31 34 39 2c 20 20 34 31 33 2c 20 20  1,  149,  413,  
310ed 36 32 30 2c 20 20 33 39 37 2c 20 20 31 38 31 2c  620,  397,  181,
310ee 0a 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20  . /*  1310 */   
310ef 32 35 39 2c 20 20 36 30 30 2c 20 20 20 37 30 2c  259,  600,   70,
310f0 20 20 33 39 38 2c 20 20 34 30 37 2c 20 20 36 30    398,  407,  60
310f1 30 2c 20 20 20 31 37 2c 20 20 33 31 35 2c 20 20  0,   17,  315,  
310f2 33 31 34 2c 20 20 35 39 32 2c 0a 20 2f 2a 20 20  314,  592,. /*  
310f3 31 33 32 30 20 2a 2f 20 20 20 35 39 32 2c 20 20  1320 */   592,  
310f4 35 39 32 2c 20 20 35 39 31 2c 20 20 35 39 30 2c  592,  591,  590,
310f5 20 20 20 31 34 2c 20 20 36 32 30 2c 20 20 31 32     14,  620,  12
310f6 37 2c 20 20 33 36 31 2c 20 20 36 32 34 2c 20 20  7,  361,  624,  
310f7 32 31 37 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a  217,. /*  1330 *
310f8 2f 20 20 20 34 36 32 2c 20 20 34 36 31 2c 20 20  /   462,  461,  
310f9 33 35 34 2c 20 20 34 30 32 2c 20 20 33 30 34 2c  354,  402,  304,
310fa 20 20 33 30 33 2c 20 20 33 30 32 2c 20 20 31 37    303,  302,  17
310fb 39 2c 20 20 33 30 30 2c 20 20 32 35 34 2c 0a 20  9,  300,  254,. 
310fc 2f 2a 20 20 31 33 34 30 20 2a 2f 20 20 20 36 31  /*  1340 */   61
310fd 34 2c 20 20 34 35 33 2c 20 20 36 32 30 2c 20 20  4,  453,  620,  
310fe 35 36 35 2c 20 20 34 35 34 2c 20 20 36 32 30 2c  565,  454,  620,
310ff 20 20 36 32 30 2c 20 20 36 32 30 2c 20 20 36 31    620,  620,  61
31100 33 2c 20 20 36 31 32 2c 0a 20 2f 2a 20 20 31 33  3,  612,. /*  13
31101 35 30 20 2a 2f 20 20 20 34 34 33 2c 20 20 34 31  50 */   443,  41
31102 36 2c 20 20 31 38 30 2c 20 20 32 34 36 2c 20 20  6,  180,  246,  
31103 36 32 30 2c 20 20 31 35 31 2c 20 20 34 31 35 2c  620,  151,  415,
31104 20 20 32 34 35 2c 20 20 32 34 33 2c 20 20 36 32    245,  243,  62
31105 30 2c 0a 20 2f 2a 20 20 31 33 36 30 20 2a 2f 20  0,. /*  1360 */ 
31106 20 20 31 37 38 2c 20 20 35 39 38 2c 20 20 32 34    178,  598,  24
31107 32 2c 20 20 20 34 30 2c 20 20 20 34 31 2c 20 20  2,   40,   41,  
31108 36 32 30 2c 20 20 32 34 34 2c 20 20 20 20 38 2c  620,  244,    8,
31109 20 20 36 32 30 2c 20 20 32 33 39 2c 0a 20 2f 2a    620,  239,. /*
3110a 20 20 31 33 37 30 20 2a 2f 20 20 20 20 34 32 2c    1370 */    42,
3110b 20 20 34 31 32 2c 20 20 34 31 31 2c 20 20 36 32    412,  411,  62
3110c 30 2c 20 20 34 31 30 2c 20 20 35 39 34 2c 20 20  0,  410,  594,  
3110d 34 31 30 2c 20 20 36 32 30 2c 20 20 20 36 30 2c  410,  620,   60,
3110e 20 20 31 35 33 2c 0a 20 2f 2a 20 20 31 33 38 30    153,. /*  1380
3110f 20 2a 2f 20 20 20 34 32 39 2c 20 20 34 36 35 2c   */   429,  465,
31110 20 20 36 32 32 2c 20 20 36 32 31 2c 20 20 32 39    622,  621,  29
31111 36 2c 20 20 31 35 34 2c 20 20 20 33 30 2c 20 20  6,  154,   30,  
31112 31 34 35 2c 20 20 31 35 32 2c 20 20 34 31 33 2c  145,  152,  413,
31113 0a 20 2f 2a 20 20 31 33 39 30 20 2a 2f 20 20 20  . /*  1390 */   
31114 33 38 38 2c 20 20 34 31 33 2c 20 20 32 39 35 2c  388,  413,  295,
31115 20 20 33 39 34 2c 20 20 32 39 34 2c 20 20 36 32    394,  294,  62
31116 30 2c 20 20 20 33 31 2c 20 20 33 39 32 2c 20 20  0,   31,  392,  
31117 36 30 30 2c 20 20 20 37 39 2c 0a 20 2f 2a 20 20  600,   79,. /*  
31118 31 34 30 30 20 2a 2f 20 20 20 36 30 30 2c 20 20  1400 */   600,  
31119 20 37 38 2c 20 20 36 32 30 2c 20 20 32 39 30 2c   78,  620,  290,
3111a 20 20 33 39 36 2c 20 20 34 31 33 2c 20 20 35 39    396,  413,  59
3111b 32 2c 20 20 35 39 32 2c 20 20 35 39 32 2c 20 20  2,  592,  592,  
3111c 35 39 31 2c 0a 20 2f 2a 20 20 31 34 31 30 20 2a  591,. /*  1410 *
3111d 2f 20 20 20 35 39 30 2c 20 20 20 31 34 2c 20 20  /   590,   14,  
3111e 36 32 30 2c 20 20 32 39 33 2c 20 20 36 30 30 2c  620,  293,  600,
3111f 20 20 20 20 39 2c 20 20 35 39 37 2c 20 20 20 35      9,  597,   5
31120 39 2c 20 20 36 32 30 2c 20 20 20 33 36 2c 0a 20  9,  620,   36,. 
31121 2f 2a 20 20 31 34 32 30 20 2a 2f 20 20 20 35 35  /*  1420 */   55
31122 35 2c 20 20 31 37 33 2c 20 20 35 36 35 2c 20 20  5,  173,  565,  
31123 32 33 34 2c 20 20 31 38 35 2c 20 20 32 38 38 2c  234,  185,  288,
31124 20 20 20 32 39 2c 20 20 35 34 31 2c 20 20 33 39     29,  541,  39
31125 31 2c 20 20 33 34 35 2c 0a 20 2f 2a 20 20 31 34  1,  345,. /*  14
31126 33 30 20 2a 2f 20 20 20 32 34 38 2c 20 20 32 38  30 */   248,  28
31127 36 2c 20 20 35 32 31 2c 20 20 35 33 35 2c 20 20  6,  521,  535,  
31128 33 31 33 2c 20 20 32 38 34 2c 20 20 33 38 35 2c  313,  284,  385,
31129 20 20 33 32 38 2c 20 20 35 33 34 2c 20 20 32 33    328,  534,  23
3112a 39 2c 0a 20 2f 2a 20 20 31 34 34 30 20 2a 2f 20  9,. /*  1440 */ 
3112b 20 20 35 31 36 2c 20 20 35 31 35 2c 20 20 31 39    516,  515,  19
3112c 36 2c 20 20 31 39 35 2c 20 20 32 37 39 2c 20 20  6,  195,  279,  
3112d 33 31 30 2c 20 20 35 31 31 2c 20 20 35 31 32 2c  310,  511,  512,
3112e 20 20 35 31 30 2c 20 20 31 33 31 2c 0a 20 2f 2a    510,  131,. /*
3112f 20 20 31 34 35 30 20 2a 2f 20 20 20 35 32 34 2c    1450 */   524,
31130 20 20 32 32 37 2c 20 20 32 35 38 2c 20 20 32 32    227,  258,  22
31131 38 2c 20 20 35 39 34 2c 20 20 33 30 39 2c 20 20  8,  594,  309,  
31132 34 38 37 2c 20 20 34 38 36 2c 20 20 34 39 33 2c  487,  486,  493,
31133 20 20 32 32 36 2c 0a 20 2f 2a 20 20 31 34 36 30    226,. /*  1460
31134 20 2a 2f 20 20 20 33 37 32 2c 20 20 34 38 35 2c   */   372,  485,
31135 20 20 31 36 34 2c 20 20 33 33 38 2c 20 20 34 37    164,  338,  47
31136 39 2c 20 20 31 36 33 2c 20 20 33 36 38 2c 20 20  9,  163,  368,  
31137 33 37 30 2c 20 20 31 36 32 2c 20 20 20 32 36 2c  370,  162,   26,
31138 0a 20 2f 2a 20 20 31 34 37 30 20 2a 2f 20 20 20  . /*  1470 */   
31139 32 31 31 2c 20 20 34 37 37 2c 20 20 32 36 31 2c  211,  477,  261,
3113a 20 20 31 36 31 2c 20 20 31 34 31 2c 20 20 34 37    161,  141,  47
3113b 36 2c 20 20 33 36 32 2c 20 20 34 36 36 2c 20 20  6,  362,  466,  
3113c 31 32 32 2c 20 20 31 38 37 2c 0a 20 2f 2a 20 20  122,  187,. /*  
3113d 31 34 38 30 20 2a 2f 20 20 20 31 31 39 2c 20 20  1480 */   119,  
3113e 34 35 36 2c 20 20 33 34 37 2c 20 20 31 31 37 2c  456,  347,  117,
3113f 20 20 33 34 36 2c 20 20 35 39 32 2c 20 20 35 39    346,  592,  59
31140 32 2c 20 20 35 39 32 2c 20 20 31 31 36 2c 20 20  2,  592,  116,  
31141 31 31 35 2c 0a 20 2f 2a 20 20 31 34 39 30 20 2a  115,. /*  1490 *
31142 2f 20 20 20 31 31 34 2c 20 20 34 34 37 2c 20 20  /   114,  447,  
31143 31 31 32 2c 20 20 31 38 32 2c 20 20 33 31 38 2c  112,  182,  318,
31144 20 20 20 32 32 2c 20 20 34 33 32 2c 20 20 20 31     22,  432,   1
31145 39 2c 20 20 34 33 31 2c 20 20 34 33 30 2c 0a 20  9,  431,  430,. 
31146 2f 2a 20 20 31 35 30 30 20 2a 2f 20 20 20 20 36  /*  1500 */    6
31147 33 2c 20 20 34 32 37 2c 20 20 36 30 38 2c 20 20  3,  427,  608,  
31148 31 39 33 2c 20 20 32 39 37 2c 20 20 35 39 35 2c  193,  297,  595,
31149 20 20 35 37 32 2c 20 20 35 37 30 2c 20 20 34 30    572,  570,  40
3114a 33 2c 20 20 35 35 33 2c 0a 20 2f 2a 20 20 31 35  3,  553,. /*  15
3114b 31 30 20 2a 2f 20 20 20 35 35 30 2c 20 20 32 38  10 */   550,  28
3114c 39 2c 20 20 32 38 30 2c 20 20 35 30 38 2c 20 20  9,  280,  508,  
3114d 34 39 38 2c 20 20 34 39 37 2c 20 20 34 39 36 2c  498,  497,  496,
3114e 20 20 34 39 34 2c 20 20 33 37 39 2c 20 20 33 35    494,  379,  35
3114f 35 2c 0a 20 2f 2a 20 20 31 35 32 30 20 2a 2f 20  5,. /*  1520 */ 
31150 20 20 34 35 39 2c 20 20 32 35 35 2c 20 20 32 34    459,  255,  24
31151 39 2c 20 20 33 34 34 2c 20 20 34 34 36 2c 20 20  9,  344,  446,  
31152 33 30 35 2c 20 20 20 20 35 2c 20 20 35 36 38 2c  305,    5,  568,
31153 20 20 35 34 38 2c 20 20 32 39 38 2c 0a 20 2f 2a    548,  298,. /*
31154 20 20 31 35 33 30 20 2a 2f 20 20 20 32 39 38 2c    1530 */   298,
31155 20 20 32 31 30 2c 20 20 33 36 39 2c 20 20 32 39    210,  369,  29
31156 38 2c 20 20 34 30 30 2c 20 20 35 30 36 2c 20 20  8,  400,  506,  
31157 35 30 30 2c 20 20 34 38 39 2c 20 20 35 32 35 2c  500,  489,  525,
31158 20 20 35 32 33 2c 0a 20 2f 2a 20 20 31 35 34 30    523,. /*  1540
31159 20 2a 2f 20 20 20 34 38 32 2c 20 20 32 33 39 2c   */   482,  239,
3115a 20 20 32 33 37 2c 0a 7d 3b 0a 73 74 61 74 69 63    237,.};.static
3115b 20 63 6f 6e 73 74 20 59 59 43 4f 44 45 54 59 50   const YYCODETYP
3115c 45 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d  E yy_lookahead[]
3115d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a   = {. /*     0 *
3115e 2f 20 20 20 20 31 39 2c 20 20 32 32 32 2c 20 20  /    19,  222,  
3115f 32 32 33 2c 20 20 32 32 34 2c 20 20 32 32 35 2c  223,  224,  225,
31160 20 20 20 32 34 2c 20 20 20 20 31 2c 20 20 20 32     24,    1,   2
31161 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a 20  6,   77,   78,. 
31162 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 20 37  /*    10 */    7
31163 39 2c 20 20 20 38 30 2c 20 20 20 31 35 2c 20 20  9,   80,   15,  
31164 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
31165 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
31166 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 20  7,   88,. /*    
31167 32 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39  20 */    89,   9
31168 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  0,   91,   92,  
31169 20 20 37 2c 20 20 20 20 38 2c 20 20 20 20 39 2c    7,    8,    9,
3116a 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32     26,   27,   2
3116b 33 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20  3,. /*    30 */ 
3116c 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 20 38     49,   50,   8
3116d 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  1,   82,   83,  
3116e 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
3116f 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a     87,   88,. /*
31170 20 20 20 20 34 30 20 2a 2f 20 20 20 20 38 39 2c      40 */    89,
31171 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
31172 32 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  2,   88,   89,  
31173 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   90,   91,   92,
31174 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 20 35 30     68,. /*    50
31175 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c   */    69,   70,
31176 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
31177 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20  3,   74,   75,  
31178 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
31179 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20  . /*    60 */   
3117a 20 37 39 2c 20 20 20 38 30 2c 20 20 20 32 32 2c   79,   80,   22,
3117b 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
3117c 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  4,   85,   86,  
3117d 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20   87,   88,. /*  
3117e 20 20 37 30 20 2a 2f 20 20 20 20 38 39 2c 20 20    70 */    89,  
3117f 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   90,   91,   92,
31180 20 20 20 31 39 2c 20 20 20 39 34 2c 20 20 32 32     19,   94,  22
31181 32 2c 20 20 20 31 39 2c 20 20 32 32 34 2c 20 20  2,   19,  224,  
31182 32 32 35 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a  225,. /*    80 *
31183 2f 20 20 20 20 32 35 2c 20 20 20 38 32 2c 20 20  /    25,   82,  
31184 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
31185 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
31186 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0a 20  8,   89,   90,. 
31187 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 20 39  /*    90 */    9
31188 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20  1,   92,   19,  
31189 20 32 36 2c 20 20 20 32 37 2c 20 20 20 39 34 2c   26,   27,   94,
3118a 20 20 20 39 35 2c 20 20 20 39 36 2c 20 20 32 34     95,   96,  24
3118b 34 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20 20 31  4,   25,. /*   1
3118c 30 30 20 2a 2f 20 20 20 20 39 39 2c 20 20 31 30  00 */    99,  10
3118d 30 2c 20 20 31 30 31 2c 20 20 20 32 35 2c 20 20  0,  101,   25,  
3118e 20 34 39 2c 20 20 20 35 30 2c 20 20 20 31 39 2c   49,   50,   19,
3118f 20 20 31 35 30 2c 20 20 20 39 36 2c 20 20 20 35    150,   96,   5
31190 34 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20  4,. /*   110 */ 
31191 20 20 31 30 39 2c 20 20 20 39 39 2c 20 20 31 30    109,   99,  10
31192 30 2c 20 20 31 30 31 2c 20 20 20 32 37 2c 20 20  0,  101,   27,  
31193 31 35 38 2c 20 20 20 32 36 2c 20 20 20 32 37 2c  158,   26,   27,
31194 20 20 31 36 31 2c 20 20 31 36 32 2c 0a 20 2f 2a    161,  162,. /*
31195 20 20 20 31 32 30 20 2a 2f 20 20 20 31 31 37 2c     120 */   117,
31196 20 20 31 30 39 2c 20 20 31 36 35 2c 20 20 20 36    109,  165,   6
31197 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20  8,   69,   70,  
31198 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
31199 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 31 33 30     74,. /*   130
3119a 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c   */    75,   76,
3119b 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
3119c 39 2c 20 20 20 38 30 2c 20 20 20 31 36 2c 20 20  9,   80,   16,  
3119d 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
3119e 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20  . /*   140 */   
3119f 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
311a0 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
311a1 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  0,   91,   92,  
311a2 20 31 39 2c 20 20 31 31 31 2c 0a 20 2f 2a 20 20   19,  111,. /*  
311a3 20 31 35 30 20 2a 2f 20 20 20 20 39 32 2c 20 20   150 */    92,  
311a4 31 31 33 2c 20 20 31 31 34 2c 20 20 20 32 34 2c  113,  114,   24,
311a5 20 20 20 39 36 2c 20 20 20 39 37 2c 20 20 20 39     96,   97,   9
311a6 38 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20  8,   99,  100,  
311a7 31 30 31 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a  101,. /*   160 *
311a8 2f 20 20 20 31 30 32 2c 20 20 20 39 34 2c 20 20  /   102,   94,  
311a9 20 39 35 2c 20 20 31 31 32 2c 20 20 20 32 35 2c   95,  112,   25,
311aa 20 20 20 39 32 2c 20 20 31 30 38 2c 20 20 31 31     92,  108,  11
311ab 36 2c 20 20 31 35 30 2c 20 20 20 39 35 2c 0a 20  6,  150,   95,. 
311ac 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 20 39  /*   170 */    9
311ad 37 2c 20 20 20 39 38 2c 20 20 20 39 39 2c 20 20  7,   98,   99,  
311ae 31 30 30 2c 20 20 31 30 31 2c 20 20 31 30 32 2c  100,  101,  102,
311af 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 34     86,   87,   4
311b0 39 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20 20 31  9,   50,. /*   1
311b1 38 30 20 2a 2f 20 20 20 20 36 30 2c 20 20 31 30  80 */    60,  10
311b2 38 2c 20 20 20 36 32 2c 20 20 31 36 35 2c 20 20  8,   62,  165,  
311b3 20 39 34 2c 20 20 20 39 35 2c 20 20 31 31 39 2c   94,   95,  119,
311b4 20 20 20 39 37 2c 20 20 31 37 30 2c 20 20 31 37     97,  170,  17
311b5 31 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20  1,. /*   190 */ 
311b6 20 20 20 35 30 2c 20 20 20 32 36 2c 20 20 20 32     50,   26,   2
311b7 37 2c 20 20 31 31 39 2c 20 20 31 30 34 2c 20 20  7,  119,  104,  
311b8 31 30 35 2c 20 20 31 31 38 2c 20 20 20 36 38 2c  105,  118,   68,
311b9 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a     69,   70,. /*
311ba 20 20 20 32 30 30 20 2a 2f 20 20 20 20 37 31 2c     200 */    71,
311bb 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
311bc 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20  4,   75,   76,  
311bd 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
311be 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 32 31 30     80,. /*   210
311bf 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 38 32 2c   */    22,   82,
311c0 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
311c1 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20  5,   86,   87,  
311c2 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
311c3 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20  . /*   220 */   
311c4 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c   91,   92,   19,
311c5 20 20 20 39 38 2c 20 20 20 38 36 2c 20 20 20 38     98,   86,   8
311c6 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
311c7 20 39 30 2c 20 20 20 39 31 2c 0a 20 2f 2a 20 20   90,   91,. /*  
311c8 20 32 33 30 20 2a 2f 20 20 20 20 39 32 2c 20 20   230 */    92,  
311c9 31 35 30 2c 20 20 20 32 33 2c 20 20 31 30 38 2c  150,   23,  108,
311ca 20 20 20 32 35 2c 20 20 20 39 36 2c 20 20 31 35     25,   96,  15
311cb 30 2c 20 20 20 36 36 2c 20 20 20 39 39 2c 20 20  0,   66,   99,  
311cc 31 30 30 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a  100,. /*   240 *
311cd 2f 20 20 20 31 30 31 2c 20 20 20 32 36 2c 20 20  /   101,   26,  
311ce 31 36 31 2c 20 20 31 36 32 2c 20 20 31 30 34 2c  161,  162,  104,
311cf 20 20 20 32 34 2c 20 20 31 36 35 2c 20 20 20 32     24,  165,   2
311d0 36 2c 20 20 31 30 39 2c 20 20 32 33 31 2c 0a 20  6,  109,  231,. 
311d1 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 32 33  /*   250 */   23
311d2 32 2c 20 20 31 36 35 2c 20 20 20 34 39 2c 20 20  2,  165,   49,  
311d3 20 35 30 2c 20 20 20 32 32 2c 20 20 20 32 33 2c   50,   22,   23,
311d4 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31 33    170,  171,  13
311d5 38 2c 20 20 20 39 34 2c 0a 20 2f 2a 20 20 20 32  8,   94,. /*   2
311d6 36 30 20 2a 2f 20 20 20 20 39 35 2c 20 20 31 36  60 */    95,  16
311d7 39 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20  9,  170,  171,  
311d8 20 37 36 2c 20 20 20 39 34 2c 20 20 31 38 35 2c   76,   94,  185,
311d9 20 20 31 38 36 2c 20 20 20 32 32 2c 20 20 20 39    186,   22,   9
311da 38 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20  8,. /*   270 */ 
311db 20 20 20 32 34 2c 20 20 20 36 38 2c 20 20 20 36     24,   68,   6
311dc 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  9,   70,   71,  
311dd 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c   72,   73,   74,
311de 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a     75,   76,. /*
311df 20 20 20 32 38 30 20 2a 2f 20 20 20 20 37 37 2c     280 */    77,
311e0 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
311e1 30 2c 20 20 31 31 39 2c 20 20 20 38 32 2c 20 20  0,  119,   82,  
311e2 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
311e3 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20 32 39 30     86,. /*   290
311e4 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c   */    87,   88,
311e5 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39     89,   90,   9
311e6 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20  1,   92,   19,  
311e7 31 35 30 2c 20 20 20 31 31 2c 20 20 20 36 37 2c  150,   11,   67,
311e8 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20  . /*   300 */   
311e9 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33 31 2c  129,  130,  131,
311ea 20 20 31 31 37 2c 20 20 20 39 35 2c 20 20 31 35    117,   95,  15
311eb 30 2c 20 20 20 32 32 2c 20 20 31 36 30 2c 20 20  0,   22,  160,  
311ec 31 35 30 2c 20 20 20 39 34 2c 0a 20 2f 2a 20 20  150,   94,. /*  
311ed 20 33 31 30 20 2a 2f 20 20 20 20 32 36 2c 20 20   310 */    26,  
311ee 20 32 37 2c 20 20 31 36 35 2c 20 20 20 32 32 2c   27,  165,   22,
311ef 20 20 20 32 33 2c 20 20 20 39 34 2c 20 20 20 32     23,   94,   2
311f0 35 2c 20 20 32 33 31 2c 20 20 32 33 32 2c 20 20  5,  231,  232,  
311f1 20 32 33 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a   23,. /*   320 *
311f2 2f 20 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20  /   165,  174,  
311f3 31 37 35 2c 20 20 31 36 35 2c 20 20 32 33 32 2c  175,  165,  232,
311f4 20 20 20 33 32 2c 20 20 20 34 39 2c 20 20 20 35     32,   49,   5
311f5 30 2c 20 20 31 31 39 2c 20 20 31 37 34 2c 0a 20  0,  119,  174,. 
311f6 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 31 37  /*   330 */   17
311f7 35 2c 20 20 20 32 32 2c 20 20 31 37 34 2c 20 20  5,   22,  174,  
311f8 31 37 35 2c 20 20 20 34 31 2c 20 20 20 32 36 2c  175,   41,   26,
311f9 20 20 20 32 37 2c 20 20 20 35 37 2c 20 20 31 36     27,   57,  16
311fa 36 2c 20 20 31 33 36 2c 0a 20 2f 2a 20 20 20 33  6,  136,. /*   3
311fb 34 30 20 2a 2f 20 20 20 32 32 32 2c 20 20 31 39  40 */   222,  19
311fc 34 2c 20 20 32 32 34 2c 20 20 32 32 35 2c 20 20  4,  224,  225,  
311fd 20 35 37 2c 20 20 20 36 38 2c 20 20 20 36 39 2c   57,   68,   69,
311fe 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37     70,   71,   7
311ff 32 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20  2,. /*   350 */ 
31200 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
31201 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
31202 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c   78,   79,   80,
31203 20 20 20 36 37 2c 20 20 20 38 32 2c 0a 20 2f 2a     67,   82,. /*
31204 20 20 20 33 36 30 20 2a 2f 20 20 20 20 38 33 2c     360 */    83,
31205 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
31206 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20  6,   87,   88,  
31207 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c   89,   90,   91,
31208 20 20 20 39 32 2c 0a 20 2f 2a 20 20 20 33 37 30     92,. /*   370
31209 20 2a 2f 20 20 20 20 31 39 2c 20 20 32 31 36 2c   */    19,  216,
3120a 20 20 32 31 34 2c 20 20 32 31 35 2c 20 20 20 32    214,  215,   2
3120b 33 2c 20 20 20 32 33 2c 20 20 32 32 31 2c 20 20  3,   23,  221,  
3120c 32 30 35 2c 20 20 20 39 34 2c 20 20 20 39 35 2c  205,   94,   95,
3120d 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20  . /*   380 */   
3120e 31 37 32 2c 20 20 31 37 33 2c 20 20 20 31 32 2c  172,  173,   12,
3120f 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 31 30     49,   50,  10
31210 35 2c 20 20 31 30 36 2c 20 20 20 32 36 2c 20 20  5,  106,   26,  
31211 20 32 37 2c 20 20 31 38 31 2c 0a 20 2f 2a 20 20   27,  181,. /*  
31212 20 33 39 30 20 2a 2f 20 20 20 20 32 33 2c 20 20   390 */    23,  
31213 20 31 39 2c 20 20 31 30 35 2c 20 20 31 30 36 2c   19,  105,  106,
31214 20 20 32 32 32 2c 20 20 31 31 35 2c 20 20 32 32    222,  115,  22
31215 34 2c 20 20 32 32 35 2c 20 20 20 32 38 2c 20 20  4,  225,   28,  
31216 20 32 37 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a   27,. /*   400 *
31217 2f 20 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  /    49,   50,  
31218 31 31 35 2c 20 20 20 39 34 2c 20 20 20 39 35 2c  115,   94,   95,
31219 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 31     71,   72,  11
3121a 31 2c 20 20 31 32 38 2c 20 20 31 31 33 2c 0a 20  1,  128,  113,. 
3121b 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 31 31  /*   410 */   11
3121c 34 2c 20 20 31 31 37 2c 20 20 31 36 30 2c 20 20  4,  117,  160,  
3121d 31 33 36 2c 20 20 20 34 34 2c 20 20 31 32 38 2c  136,   44,  128,
3121e 20 20 20 34 36 2c 20 20 20 32 36 2c 20 20 20 32     46,   26,   2
3121f 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20 20 34  7,   68,. /*   4
31220 32 30 20 2a 2f 20 20 20 20 36 39 2c 20 20 20 37  20 */    69,   7
31221 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
31222 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c   73,   74,   75,
31223 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37     76,   77,   7
31224 38 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20  8,. /*   430 */ 
31225 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 39     79,   80,   9
31226 38 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  8,   82,   83,  
31227 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c   84,   85,   86,
31228 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a     87,   88,. /*
31229 20 20 20 34 34 30 20 2a 2f 20 20 20 20 38 39 2c     440 */    89,
3122a 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
3122b 32 2c 20 20 20 31 39 2c 20 20 20 31 32 2c 20 20  2,   19,   12,  
3122c 31 39 34 2c 20 20 31 35 30 2c 20 20 20 32 33 2c  194,  150,   23,
3122d 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 34 35 30     88,. /*   450
3122e 20 2a 2f 20 20 20 20 32 33 2c 20 20 20 20 37 2c   */    23,    7,
3122f 20 20 20 20 38 2c 20 20 31 30 35 2c 20 20 31 30      8,  105,  10
31230 36 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20  6,   94,   95,  
31231 20 32 33 2c 20 20 31 35 30 2c 20 20 20 32 35 2c   23,  150,   25,
31232 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20  . /*   460 */   
31233 31 31 37 2c 20 20 20 32 38 2c 20 20 31 36 35 2c  117,   28,  165,
31234 20 20 31 31 31 2c 20 20 31 36 33 2c 20 20 31 31    111,  163,  11
31235 33 2c 20 20 31 31 34 2c 20 20 31 36 36 2c 20 20  3,  114,  166,  
31236 31 36 37 2c 20 20 31 36 38 2c 0a 20 2f 2a 20 20  167,  168,. /*  
31237 20 34 37 30 20 2a 2f 20 20 20 32 31 38 2c 20 20   470 */   218,  
31238 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c  174,  175,  165,
31239 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 31 32     49,   50,  12
3123a 38 2c 20 20 20 34 34 2c 20 20 31 31 31 2c 20 20  8,   44,  111,  
3123b 20 34 36 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a   46,. /*   480 *
3123c 2f 20 20 20 31 31 33 2c 20 20 31 31 34 2c 20 20  /   113,  114,  
3123d 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32 31 2c  174,  175,   21,
3123e 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20 32 33     94,   95,  23
3123f 35 2c 20 20 20 32 32 2c 20 20 20 32 33 2c 0a 20  5,   22,   23,. 
31240 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 20 35  /*   490 */    5
31241 37 2c 20 20 20 32 35 2c 20 20 32 34 30 2c 20 20  7,   25,  240,  
31242 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
31243 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37     71,   72,   7
31244 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20 35  3,   74,. /*   5
31245 30 30 20 2a 2f 20 20 20 20 37 35 2c 20 20 20 37  00 */    75,   7
31246 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  6,   77,   78,  
31247 20 37 39 2c 20 20 20 38 30 2c 20 20 31 31 37 2c   79,   80,  117,
31248 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38     82,   83,   8
31249 34 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20  4,. /*   510 */ 
3124a 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
3124b 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
3124c 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c   90,   91,   92,
3124d 20 20 20 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a     19,  150,. /*
3124e 20 20 20 35 32 30 20 2a 2f 20 20 20 20 32 35 2c     520 */    25,
3124f 20 20 31 35 30 2c 20 20 20 32 33 2c 20 20 32 32    150,   23,  22
31250 36 2c 20 20 32 31 36 2c 20 20 20 31 39 2c 20 20  6,  216,   19,  
31251 20 36 33 2c 20 20 31 35 30 2c 20 20 31 35 30 2c   63,  150,  150,
31252 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 35 33 30    150,. /*   530
31253 20 2a 2f 20 20 20 31 36 31 2c 20 20 31 36 32 2c   */   161,  162,
31254 20 20 31 35 30 2c 20 20 20 36 37 2c 20 20 31 36    150,   67,  16
31255 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  5,  150,  165,  
31256 20 32 33 2c 20 20 31 31 31 2c 20 20 20 32 35 2c   23,  111,   25,
31257 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20  . /*   540 */   
31258 31 31 33 2c 20 20 31 31 34 2c 20 20 31 36 35 2c  113,  114,  165,
31259 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 37    165,  165,  17
3125a 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20  4,  175,  165,  
3125b 20 34 39 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20   49,   50,. /*  
3125c 20 35 35 30 20 2a 2f 20 20 20 31 36 35 2c 20 20   550 */   165,  
3125d 31 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c  174,  175,  174,
3125e 20 20 31 37 35 2c 20 20 31 39 37 2c 20 20 20 35    175,  197,   5
3125f 30 2c 20 20 20 32 32 2c 20 20 20 32 33 2c 20 20  0,   22,   23,  
31260 31 37 34 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a  174,. /*   560 *
31261 2f 20 20 20 31 37 35 2c 20 20 31 36 30 2c 20 20  /   175,  160,  
31262 32 30 34 2c 20 20 31 30 30 2c 20 20 31 36 39 2c  204,  100,  169,
31263 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 20 36    170,  171,   6
31264 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 0a 20  8,   69,   70,. 
31265 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 20 37  /*   570 */    7
31266 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
31267 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
31268 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37     77,   78,   7
31269 39 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 35  9,   80,. /*   5
3126a 38 30 20 2a 2f 20 20 20 31 31 32 2c 20 20 20 38  80 */   112,   8
3126b 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
3126c 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c   85,   86,   87,
3126d 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39     88,   89,   9
3126e 30 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20  0,. /*   590 */ 
3126f 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31     91,   92,   1
31270 39 2c 20 20 20 32 32 2c 20 20 32 31 35 2c 20 20  9,   22,  215,  
31271 31 39 34 2c 20 20 20 32 33 2c 20 20 32 32 30 2c  194,   23,  220,
31272 20 20 20 39 37 2c 20 20 20 33 30 2c 0a 20 2f 2a     97,   30,. /*
31273 20 20 20 36 30 30 20 2a 2f 20 20 20 31 35 30 2c     600 */   150,
31274 20 20 31 33 38 2c 20 20 20 36 37 2c 20 20 20 33    138,   67,   3
31275 34 2c 20 20 20 33 36 2c 20 20 32 32 30 2c 20 20  4,   36,  220,  
31276 31 35 30 2c 20 20 32 30 36 2c 20 20 32 30 37 2c  150,  206,  207,
31277 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20 36 31 30     22,. /*   610
31278 20 2a 2f 20 20 20 31 30 34 2c 20 20 31 38 31 2c   */   104,  181,
31279 20 20 31 38 32 2c 20 20 20 32 36 2c 20 20 31 30    182,   26,  10
3127a 38 2c 20 20 31 36 35 2c 20 20 31 31 35 2c 20 20  8,  165,  115,  
3127b 20 34 38 2c 20 20 31 38 38 2c 20 20 20 35 31 2c   48,  188,   51,
3127c 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20  . /*   620 */   
3127d 31 32 35 2c 20 20 31 36 35 2c 20 20 20 34 39 2c  125,  165,   49,
3127e 20 20 20 35 30 2c 20 20 31 37 34 2c 20 20 31 37     50,  174,  17
3127f 35 2c 20 20 20 35 38 2c 20 20 32 33 32 2c 20 20  5,   58,  232,  
31280 31 32 37 2c 20 20 20 32 33 2c 0a 20 2f 2a 20 20  127,   23,. /*  
31281 20 36 33 30 20 2a 2f 20 20 20 31 37 34 2c 20 20   630 */   174,  
31282 31 37 35 2c 20 20 31 30 35 2c 20 20 31 30 36 2c  175,  105,  106,
31283 20 20 31 36 30 2c 20 20 31 34 32 2c 20 20 31 34    160,  142,  14
31284 33 2c 20 20 31 34 34 2c 20 20 31 34 35 2c 20 20  3,  144,  145,  
31285 20 32 33 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a   23,. /*   640 *
31286 2f 20 20 20 20 31 36 2c 20 20 20 36 38 2c 20 20  /    16,   68,  
31287 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c   69,   70,   71,
31288 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37     72,   73,   7
31289 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20  4,   75,   76,. 
3128a 2f 2a 20 20 20 36 35 30 20 2a 2f 20 20 20 20 37  /*   650 */    7
3128b 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
3128c 20 38 30 2c 20 20 31 35 30 2c 20 20 20 38 32 2c   80,  150,   82,
3128d 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38     83,   84,   8
3128e 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20 20 36  5,   86,. /*   6
3128f 36 30 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38  60 */    87,   8
31290 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  8,   89,   90,  
31291 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c   91,   92,   19,
31292 20 20 31 35 30 2c 20 20 31 39 34 2c 20 20 31 36    150,  194,  16
31293 35 2c 0a 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20  5,. /*   670 */ 
31294 20 20 31 35 30 2c 20 20 20 32 34 2c 20 20 20 32    150,   24,   2
31295 32 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  2,  150,  150,  
31296 31 30 37 2c 20 20 31 35 30 2c 20 20 20 32 32 2c  107,  150,   22,
31297 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a    174,  175,. /*
31298 20 20 20 36 38 30 20 2a 2f 20 20 20 20 38 38 2c     680 */    88,
31299 20 20 20 39 34 2c 20 20 31 36 35 2c 20 20 20 32     94,  165,   2
3129a 33 2c 20 20 20 36 30 2c 20 20 31 36 35 2c 20 20  3,   60,  165,  
3129b 20 36 32 2c 20 20 31 35 30 2c 20 20 31 36 35 2c   62,  150,  165,
3129c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 36 39 30    165,. /*   690
3129d 20 2a 2f 20 20 20 20 31 31 2c 20 20 31 36 35 2c   */    11,  165,
3129e 20 20 32 31 38 2c 20 20 20 33 35 2c 20 20 31 37    218,   35,  17
3129f 34 2c 20 20 31 37 35 2c 20 20 20 34 39 2c 20 20  4,  175,   49,  
312a0 20 35 30 2c 20 20 31 37 34 2c 20 20 31 37 35 2c   50,  174,  175,
312a1 0a 20 2f 2a 20 20 20 37 30 30 20 2a 2f 20 20 20  . /*   700 */   
312a2 31 37 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c  174,  175,  165,
312a3 20 20 20 32 35 2c 20 20 31 31 32 2c 20 20 20 20     25,  112,    
312a4 30 2c 20 20 20 20 31 2c 20 20 20 20 32 2c 20 20  0,    1,    2,  
312a5 31 31 36 2c 20 20 20 32 35 2c 0a 20 2f 2a 20 20  116,   25,. /*  
312a6 20 37 31 30 20 2a 2f 20 20 20 31 39 33 2c 20 20   710 */   193,  
312a7 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32 33 2c  174,  175,   23,
312a8 20 20 32 34 30 2c 20 20 20 36 38 2c 20 20 20 36    240,   68,   6
312a9 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20  9,   70,   71,  
312aa 20 37 32 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a   72,. /*   720 *
312ab 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20  /    73,   74,  
312ac 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c   75,   76,   77,
312ad 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38     78,   79,   8
312ae 30 2c 20 20 31 35 30 2c 20 20 20 38 32 2c 0a 20  0,  150,   82,. 
312af 2f 2a 20 20 20 37 33 30 20 2a 2f 20 20 20 20 38  /*   730 */    8
312b0 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20  3,   84,   85,  
312b1 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
312b2 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39     89,   90,   9
312b3 31 2c 20 20 20 39 32 2c 0a 20 2f 2a 20 20 20 37  1,   92,. /*   7
312b4 34 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 31 35  40 */    19,  15
312b5 30 2c 20 20 20 32 33 2c 20 20 31 36 35 2c 20 20  0,   23,  165,  
312b6 20 32 35 2c 20 20 31 36 36 2c 20 20 31 36 37 2c   25,  166,  167,
312b7 20 20 31 36 38 2c 20 20 20 39 37 2c 20 20 20 39    168,   97,   9
312b8 38 2c 0a 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20  8,. /*   750 */ 
312b9 20 20 31 35 35 2c 20 20 20 32 33 2c 20 20 31 37    155,   23,  17
312ba 34 2c 20 20 31 37 35 2c 20 20 31 35 30 2c 20 20  4,  175,  150,  
312bb 20 32 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c   25,  165,  150,
312bc 20 20 20 33 35 2c 20 20 31 35 30 2c 0a 20 2f 2a     35,  150,. /*
312bd 20 20 20 37 36 30 20 2a 2f 20 20 20 31 35 30 2c     760 */   150,
312be 20 20 31 35 30 2c 20 20 32 34 35 2c 20 20 31 35    150,  245,  15
312bf 30 2c 20 20 32 34 31 2c 20 20 31 37 34 2c 20 20  0,  241,  174,  
312c0 31 37 35 2c 20 20 31 32 39 2c 20 20 31 33 30 2c  175,  129,  130,
312c1 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 37 37 30    165,. /*   770
312c2 20 2a 2f 20 20 20 20 34 39 2c 20 20 20 35 30 2c   */    49,   50,
312c3 20 20 31 36 35 2c 20 20 20 35 32 2c 20 20 31 36    165,   52,  16
312c4 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  5,  165,  165,  
312c5 20 32 33 2c 20 20 31 36 35 2c 20 20 20 32 35 2c   23,  165,   25,
312c6 0a 20 2f 2a 20 20 20 37 38 30 20 2a 2f 20 20 20  . /*   780 */   
312c7 20 32 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c   25,  174,  175,
312c8 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 37    174,  175,  17
312c9 34 2c 20 20 31 37 35 2c 20 20 32 34 38 2c 20 20  4,  175,  248,  
312ca 32 34 39 2c 20 20 20 36 38 2c 0a 20 2f 2a 20 20  249,   68,. /*  
312cb 20 37 39 30 20 2a 2f 20 20 20 20 36 39 2c 20 20   790 */    69,  
312cc 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c   70,   71,   72,
312cd 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37     73,   74,   7
312ce 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20  5,   76,   77,  
312cf 20 37 38 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a   78,. /*   800 *
312d0 2f 20 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20  /    79,   80,  
312d1 31 35 30 2c 20 20 20 38 32 2c 20 20 20 38 33 2c  150,   82,   83,
312d2 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38     84,   85,   8
312d3 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 0a 20  6,   87,   88,. 
312d4 2f 2a 20 20 20 38 31 30 20 2a 2f 20 20 20 20 38  /*   810 */    8
312d5 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20  9,   90,   91,  
312d6 20 39 32 2c 20 20 20 31 39 2c 20 20 31 35 30 2c   92,   19,  150,
312d7 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 35    150,  165,  15
312d8 30 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20 38  0,  150,. /*   8
312d9 32 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 32 31  20 */   150,  21
312da 37 2c 20 20 31 35 30 2c 20 20 32 31 33 2c 20 20  7,  150,  213,  
312db 32 32 39 2c 20 20 31 31 39 2c 20 20 31 37 34 2c  229,  119,  174,
312dc 20 20 31 37 35 2c 20 20 20 32 32 2c 20 20 32 33    175,   22,  23
312dd 34 2c 0a 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20  4,. /*   830 */ 
312de 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36    165,  165,  16
312df 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  5,  165,  165,  
312e0 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  165,  150,  165,
312e1 20 20 20 33 35 2c 20 20 31 37 34 2c 0a 20 2f 2a     35,  174,. /*
312e2 20 20 20 38 34 30 20 2a 2f 20 20 20 31 37 35 2c     840 */   175,
312e3 20 20 31 37 34 2c 20 20 31 37 34 2c 20 20 31 37    174,  174,  17
312e4 35 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20  5,   49,   50,  
312e5 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32 33 2c  174,  175,   23,
312e6 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20 38 35 30     27,. /*   850
312e7 20 2a 2f 20 20 20 20 32 35 2c 20 20 31 36 35 2c   */    25,  165,
312e8 20 20 31 31 37 2c 20 20 31 38 37 2c 20 20 32 34    117,  187,  24
312e9 31 2c 20 20 20 32 32 2c 20 20 31 38 37 2c 20 20  1,   22,  187,  
312ea 31 38 37 2c 20 20 31 30 33 2c 20 20 20 32 33 2c  187,  103,   23,
312eb 0a 20 2f 2a 20 20 20 38 36 30 20 2a 2f 20 20 20  . /*   860 */   
312ec 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32 33 2c  174,  175,   23,
312ed 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37     68,   69,   7
312ee 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20 20  0,   71,   72,  
312ef 20 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20   73,   74,. /*  
312f0 20 38 37 30 20 2a 2f 20 20 20 20 37 35 2c 20 20   870 */    75,  
312f1 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c   76,   77,   78,
312f2 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31 35     79,   80,  15
312f3 30 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20  0,   82,   83,  
312f4 20 38 34 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a   84,. /*   880 *
312f5 2f 20 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20  /    85,   86,  
312f6 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c   87,   88,   89,
312f7 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39     90,   91,   9
312f8 32 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 0a 20  2,   19,  150,. 
312f9 2f 2a 20 20 20 38 39 30 20 2a 2f 20 20 20 31 35  /*   890 */   15
312fa 30 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20  0,  165,  150,  
312fb 20 32 33 2c 20 20 31 35 30 2c 20 20 20 32 35 2c   23,  150,   25,
312fc 20 20 20 32 33 2c 20 20 31 33 35 2c 20 20 20 32     23,  135,   2
312fd 35 2c 20 20 31 33 37 2c 0a 20 2f 2a 20 20 20 39  5,  137,. /*   9
312fe 30 30 20 2a 2f 20 20 20 31 37 34 2c 20 20 31 37  00 */   174,  17
312ff 35 2c 20 20 32 30 36 2c 20 20 32 30 37 2c 20 20  5,  206,  207,  
31300 31 36 35 2c 20 20 31 36 35 2c 20 20 20 33 39 2c  165,  165,   39,
31301 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36    165,  150,  16
31302 35 2c 0a 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20  5,. /*   910 */ 
31303 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 31    150,  150,  11
31304 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
31305 20 35 32 2c 20 20 31 37 34 2c 20 20 31 37 35 2c   52,  174,  175,
31306 20 20 20 34 39 2c 20 20 20 35 30 2c 0a 20 2f 2a     49,   50,. /*
31307 20 20 20 39 32 30 20 2a 2f 20 20 20 20 32 33 2c     920 */    23,
31308 20 20 31 35 30 2c 20 20 20 32 35 2c 20 20 31 36    150,   25,  16
31309 35 2c 20 20 31 32 37 2c 20 20 31 36 35 2c 20 20  5,  127,  165,  
3130a 31 36 35 2c 20 20 31 38 37 2c 20 20 20 32 33 2c  165,  187,   23,
3130b 20 20 20 32 39 2c 0a 20 2f 2a 20 20 20 39 33 30     29,. /*   930
3130c 20 2a 2f 20 20 20 20 32 35 2c 20 20 31 38 37 2c   */    25,  187,
3130d 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 37    174,  175,  17
3130e 34 2c 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20  4,  175,  165,  
3130f 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c   68,   69,   70,
31310 0a 20 2f 2a 20 20 20 39 34 30 20 2a 2f 20 20 20  . /*   940 */   
31311 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c   71,   72,   73,
31312 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37     74,   75,   7
31313 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20  6,   77,   78,  
31314 20 37 39 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20   79,   80,. /*  
31315 20 39 35 30 20 2a 2f 20 20 20 31 35 30 2c 20 20   950 */   150,  
31316 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c   82,   83,   84,
31317 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38     85,   86,   8
31318 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20  7,   88,   89,  
31319 20 39 30 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a   90,. /*   960 *
3131a 2f 20 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20  /    91,   92,  
3131b 20 31 39 2c 20 20 31 35 30 2c 20 20 31 39 33 2c   19,  150,  193,
3131c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 32    165,  150,   2
3131d 33 2c 20 20 31 35 30 2c 20 20 20 32 35 2c 0a 20  3,  150,   25,. 
3131e 2f 2a 20 20 20 39 37 30 20 2a 2f 20 20 20 31 36  /*   970 */   16
3131f 30 2c 20 20 31 36 30 2c 20 20 31 36 30 2c 20 20  0,  160,  160,  
31320 20 35 32 2c 20 20 31 37 34 2c 20 20 31 37 35 2c   52,  174,  175,
31321 20 20 31 39 30 2c 20 20 31 39 31 2c 20 20 31 36    190,  191,  16
31322 35 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 20 39  5,   22,. /*   9
31323 38 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 36  80 */   150,  16
31324 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20  5,  150,  165,  
31325 31 35 30 2c 20 20 31 34 34 2c 20 20 31 34 35 2c  150,  144,  145,
31326 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 32    174,  175,   2
31327 33 2c 0a 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20  3,. /*   990 */ 
31328 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34    174,  175,   4
31329 39 2c 20 20 20 35 30 2c 20 20 20 35 32 2c 20 20  9,   50,   52,  
3132a 31 36 35 2c 20 20 20 32 32 2c 20 20 31 36 35 2c  165,   22,  165,
3132b 20 20 20 32 32 2c 20 20 31 36 35 2c 0a 20 2f 2a     22,  165,. /*
3132c 20 20 31 30 30 30 20 2a 2f 20 20 20 32 35 30 2c    1000 */   250,
3132d 20 20 32 35 31 2c 20 20 32 34 31 2c 20 20 20 32    251,  241,   2
3132e 32 2c 20 20 31 39 34 2c 20 20 31 39 34 2c 20 20  2,  194,  194,  
3132f 31 39 34 2c 20 20 31 30 38 2c 20 20 31 37 34 2c  194,  108,  174,
31330 20 20 31 37 35 2c 0a 20 2f 2a 20 20 31 30 31 30    175,. /*  1010
31331 20 2a 2f 20 20 20 20 31 39 2c 20 20 31 39 33 2c   */    19,  193,
31332 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37     69,   70,   7
31333 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20  1,   72,   73,  
31334 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c   74,   75,   76,
31335 0a 20 2f 2a 20 20 31 30 32 30 20 2a 2f 20 20 20  . /*  1020 */   
31336 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c   77,   78,   79,
31337 20 20 20 38 30 2c 20 20 31 35 30 2c 20 20 20 38     80,  150,   8
31338 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20  2,   83,   84,  
31339 20 38 35 2c 20 20 20 38 36 2c 0a 20 2f 2a 20 20   85,   86,. /*  
3133a 31 30 33 30 20 2a 2f 20 20 20 20 38 37 2c 20 20  1030 */    87,  
3133b 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c   88,   89,   90,
3133c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31     91,   92,   1
3133d 39 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  9,  150,  150,  
3133e 31 36 35 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a  165,. /*  1040 *
3133f 2f 20 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  /   150,  150,  
31340 31 35 30 2c 20 20 32 31 33 2c 20 20 31 36 30 2c  150,  213,  160,
31341 20 20 32 31 33 2c 20 20 31 36 30 2c 20 20 31 36    213,  160,  16
31342 36 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20  6,  174,  175,. 
31343 2f 2a 20 20 31 30 35 30 20 2a 2f 20 20 20 20 39  /*  1050 */    9
31344 31 2c 20 20 20 39 32 2c 20 20 31 36 35 2c 20 20  1,   92,  165,  
31345 31 36 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c  165,  150,  165,
31346 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 35    165,  165,  15
31347 30 2c 20 20 31 36 36 2c 0a 20 2f 2a 20 20 31 30  0,  166,. /*  10
31348 36 30 20 2a 2f 20 20 20 31 30 32 2c 20 20 31 37  60 */   102,  17
31349 34 2c 20 20 31 37 35 2c 20 20 20 32 34 2c 20 20  4,  175,   24,  
3134a 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34 39 2c  174,  175,   49,
3134b 20 20 20 35 30 2c 20 20 20 32 30 2c 20 20 31 36     50,   20,  16
3134c 35 2c 0a 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20  5,. /*  1070 */ 
3134d 20 20 31 39 30 2c 20 20 31 39 31 2c 20 20 31 30    190,  191,  10
3134e 34 2c 20 20 31 36 35 2c 20 20 20 38 36 2c 20 20  4,  165,   86,  
3134f 20 38 37 2c 20 20 31 36 36 2c 20 20 20 34 33 2c   87,  166,   43,
31350 20 20 31 39 34 2c 20 20 20 35 39 2c 0a 20 2f 2a    194,   59,. /*
31351 20 20 31 30 38 30 20 2a 2f 20 20 20 31 39 34 2c    1080 */   194,
31352 20 20 31 39 33 2c 20 20 31 37 34 2c 20 20 31 37    193,  174,  17
31353 35 2c 20 20 31 39 33 2c 20 20 31 39 33 2c 20 20  5,  193,  193,  
31354 32 30 35 2c 20 20 20 37 30 2c 20 20 20 37 31 2c  205,   70,   71,
31355 20 20 20 37 32 2c 0a 20 2f 2a 20 20 31 30 39 30     72,. /*  1090
31356 20 2a 2f 20 20 20 20 37 33 2c 20 20 20 37 34 2c   */    73,   74,
31357 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37     75,   76,   7
31358 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20  7,   78,   79,  
31359 20 38 30 2c 20 20 32 30 35 2c 20 20 20 38 32 2c   80,  205,   82,
3135a 0a 20 2f 2a 20 20 31 31 30 30 20 2a 2f 20 20 20  . /*  1100 */   
3135b 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c   83,   84,   85,
3135c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38     86,   87,   8
3135d 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20  8,   89,   90,  
3135e 20 39 31 2c 20 20 20 39 32 2c 0a 20 2f 2a 20 20   91,   92,. /*  
3135f 31 31 31 30 20 2a 2f 20 20 20 20 31 39 2c 20 20  1110 */    19,  
31360 20 32 30 2c 20 20 31 35 30 2c 20 20 20 32 32 2c   20,  150,   22,
31361 20 20 31 35 30 2c 20 20 32 30 35 2c 20 20 31 33    150,  205,  13
31362 38 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  8,   26,   27,  
31363 31 35 30 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a  150,. /*  1120 *
31364 2f 20 20 20 31 35 30 2c 20 20 20 20 31 2c 20 20  /   150,    1,  
31365 20 20 32 2c 20 20 20 31 39 2c 20 20 20 32 30 2c    2,   19,   20,
31366 20 20 31 35 30 2c 20 20 20 32 32 2c 20 20 31 36    150,   22,  16
31367 35 2c 20 20 20 33 37 2c 20 20 31 36 35 2c 0a 20  5,   37,  165,. 
31368 2f 2a 20 20 31 31 33 30 20 2a 2f 20 20 20 20 32  /*  1130 */    2
31369 36 2c 20 20 20 32 37 2c 20 20 20 32 35 2c 20 20  6,   27,   25,  
3136a 31 35 30 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  150,  165,  165,
3136b 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 37    174,  175,  17
3136c 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 31 31  4,  175,. /*  11
3136d 34 30 20 2a 2f 20 20 20 31 36 35 2c 20 20 20 33  40 */   165,   3
3136e 37 2c 20 20 20 35 33 2c 20 20 31 35 30 2c 20 20  7,   53,  150,  
3136f 31 35 30 2c 20 20 31 37 33 2c 20 20 31 35 30 2c  150,  173,  150,
31370 20 20 20 35 36 2c 20 20 31 36 35 2c 20 20 31 37     56,  165,  17
31371 34 2c 0a 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20  4,. /*  1150 */ 
31372 20 20 31 37 35 2c 20 20 31 35 30 2c 20 20 20 35    175,  150,   5
31373 33 2c 20 20 31 38 31 2c 20 20 31 30 34 2c 20 20  3,  181,  104,  
31374 20 32 32 2c 20 20 31 35 30 2c 20 20 20 36 36 2c   22,  150,   66,
31375 20 20 31 36 35 2c 20 20 31 36 35 2c 0a 20 2f 2a    165,  165,. /*
31376 20 20 31 31 36 30 20 2a 2f 20 20 20 20 35 36 2c    1160 */    56,
31377 20 20 31 36 35 2c 20 20 31 39 33 2c 20 20 20 20    165,  193,    
31378 35 2c 20 20 20 20 31 2c 20 20 20 32 37 2c 20 20  5,    1,   27,  
31379 31 36 35 2c 20 20 31 34 36 2c 20 20 31 34 37 2c  165,  146,  147,
3137a 20 20 31 31 37 2c 0a 20 2f 2a 20 20 31 31 37 30    117,. /*  1170
3137b 20 2a 2f 20 20 20 20 36 36 2c 20 20 31 36 35 2c   */    66,  165,
3137c 20 20 31 35 30 2c 20 20 31 35 32 2c 20 20 20 33    150,  152,   3
3137d 35 2c 20 20 31 35 34 2c 20 20 31 39 33 2c 20 20  5,  154,  193,  
3137e 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c   86,   87,   88,
3137f 0a 20 2f 2a 20 20 31 31 38 30 20 2a 2f 20 20 20  . /*  1180 */   
31380 31 35 30 2c 20 20 31 36 30 2c 20 20 31 30 37 2c  150,  160,  107,
31381 20 20 31 32 36 2c 20 20 20 39 33 2c 20 20 20 39    126,   93,   9
31382 34 2c 20 20 20 39 35 2c 20 20 31 36 35 2c 20 20  4,   95,  165,  
31383 31 35 30 2c 20 20 20 39 38 2c 0a 20 2f 2a 20 20  150,   98,. /*  
31384 31 31 39 30 20 2a 2f 20 20 20 20 38 36 2c 20 20  1190 */    86,  
31385 20 38 37 2c 20 20 31 35 30 2c 20 20 31 35 30 2c   87,  150,  150,
31386 20 20 20 37 36 2c 20 20 31 36 35 2c 20 20 20 32     76,  165,   2
31387 32 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20 20  2,   93,   94,  
31388 20 39 35 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a   95,. /*  1200 *
31389 2f 20 20 20 20 37 36 2c 20 20 31 31 38 2c 20 20  /    76,  118,  
3138a 20 39 38 2c 20 20 31 36 35 2c 20 20 31 37 34 2c   98,  165,  174,
3138b 20 20 31 37 35 2c 20 20 20 20 31 2c 20 20 31 36    175,    1,  16
3138c 35 2c 20 20 31 36 35 2c 20 20 20 31 39 2c 0a 20  5,  165,   19,. 
3138d 2f 2a 20 20 31 32 31 30 20 2a 2f 20 20 20 20 32  /*  1210 */    2
3138e 30 2c 20 20 32 31 37 2c 20 20 20 32 32 2c 20 20  0,  217,   22,  
3138f 31 35 30 2c 20 20 20 31 36 2c 20 20 31 39 34 2c  150,   16,  194,
31390 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31 31     26,   27,  11
31391 38 2c 20 20 31 31 35 2c 0a 20 2f 2a 20 20 31 32  8,  115,. /*  12
31392 32 30 20 2a 2f 20 20 20 31 32 39 2c 20 20 31 33  20 */   129,  13
31393 30 2c 20 20 31 33 31 2c 20 20 31 33 32 2c 20 20  0,  131,  132,  
31394 31 33 33 2c 20 20 31 33 34 2c 20 20 31 32 30 2c  133,  134,  120,
31395 20 20 20 33 37 2c 20 20 31 36 35 2c 20 20 31 35     37,  165,  15
31396 30 2c 0a 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20  0,. /*  1230 */ 
31397 20 20 20 32 30 2c 20 20 31 39 33 2c 20 20 31 32     20,  193,  12
31398 37 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20  7,  129,  130,  
31399 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c  131,  132,  133,
3139a 20 20 31 33 34 2c 20 20 32 31 38 2c 0a 20 2f 2a    134,  218,. /*
3139b 20 20 31 32 34 30 20 2a 2f 20 20 20 31 35 30 2c    1240 */   150,
3139c 20 20 31 30 37 2c 20 20 31 35 30 2c 20 20 31 35    107,  150,  15
3139d 30 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20  0,  165,  150,  
3139e 20 35 36 2c 20 20 31 35 30 2c 20 20 31 35 30 2c   56,  150,  150,
3139f 20 20 31 32 36 2c 0a 20 2f 2a 20 20 31 32 35 30    126,. /*  1250
313a0 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 31 36 2c   */    22,   16,
313a1 20 20 31 36 30 2c 20 20 20 36 35 2c 20 20 20 32    160,   65,   2
313a2 33 2c 20 20 31 36 35 2c 20 20 20 36 36 2c 20 20  3,  165,   66,  
313a3 31 36 35 2c 20 20 31 36 35 2c 20 20 31 35 30 2c  165,  165,  150,
313a4 0a 20 2f 2a 20 20 31 32 36 30 20 2a 2f 20 20 20  . /*  1260 */   
313a5 31 36 35 2c 20 20 20 32 33 2c 20 20 31 36 35 2c  165,   23,  165,
313a6 20 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37    165,  174,  17
313a7 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20  5,  174,  175,  
313a8 32 34 37 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20  247,  174,. /*  
313a9 31 32 37 30 20 2a 2f 20 20 20 31 37 35 2c 20 20  1270 */   175,  
313aa 31 37 34 2c 20 20 31 37 35 2c 20 20 20 31 35 2c  174,  175,   15,
313ab 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 20 38    165,  150,   8
313ac 36 2c 20 20 20 38 37 2c 20 20 31 35 30 2c 20 20  6,   87,  150,  
313ad 31 35 30 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a  150,. /*  1280 *
313ae 2f 20 20 20 31 35 30 2c 20 20 20 32 32 2c 20 20  /   150,   22,  
313af 20 20 33 2c 20 20 20 39 33 2c 20 20 20 39 34 2c    3,   93,   94,
313b0 20 20 20 39 35 2c 20 20 31 39 34 2c 20 20 31 35     95,  194,  15
313b1 30 2c 20 20 20 39 38 2c 20 20 31 34 30 2c 0a 20  0,   98,  140,. 
313b2 2f 2a 20 20 31 32 39 30 20 2a 2f 20 20 20 31 36  /*  1290 */   16
313b3 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20  5,  150,  150,  
313b4 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c  165,  165,  165,
313b5 20 20 20 31 39 2c 20 20 20 32 30 2c 20 20 20 20     19,   20,    
313b6 34 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 31 33  4,   22,. /*  13
313b7 30 30 20 2a 2f 20 20 20 31 36 34 2c 20 20 31 38  00 */   164,  18
313b8 30 2c 20 20 31 36 35 2c 20 20 20 32 36 2c 20 20  0,  165,   26,  
313b9 20 32 37 2c 20 20 32 34 39 2c 20 20 31 36 35 2c   27,  249,  165,
313ba 20 20 31 36 35 2c 20 20 32 31 36 2c 20 20 20 20    165,  216,    
313bb 36 2c 0a 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20  6,. /*  1310 */ 
313bc 20 20 31 35 30 2c 20 20 31 37 34 2c 20 20 31 37    150,  174,  17
313bd 35 2c 20 20 32 32 31 2c 20 20 20 33 37 2c 20 20  5,  221,   37,  
313be 31 37 34 2c 20 20 31 37 35 2c 20 20 32 35 32 2c  174,  175,  252,
313bf 20 20 32 35 32 2c 20 20 31 32 39 2c 0a 20 2f 2a    252,  129,. /*
313c0 20 20 31 33 32 30 20 2a 2f 20 20 20 31 33 30 2c    1320 */   130,
313c1 20 20 31 33 31 2c 20 20 31 33 32 2c 20 20 31 33    131,  132,  13
313c2 33 2c 20 20 31 33 34 2c 20 20 31 36 35 2c 20 20  3,  134,  165,  
313c3 31 38 30 2c 20 20 31 35 30 2c 20 20 31 34 39 2c  180,  150,  149,
313c4 20 20 20 20 35 2c 0a 20 2f 2a 20 20 31 33 33 30      5,. /*  1330
313c5 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 35 30 2c   */   150,  150,
313c6 20 20 31 35 30 2c 20 20 20 35 36 2c 20 20 20 31    150,   56,   1
313c7 30 2c 20 20 20 31 31 2c 20 20 20 31 32 2c 20 20  0,   11,   12,  
313c8 20 31 33 2c 20 20 20 31 34 2c 20 20 31 35 30 2c   13,   14,  150,
313c9 0a 20 2f 2a 20 20 31 33 34 30 20 2a 2f 20 20 20  . /*  1340 */   
313ca 31 34 39 2c 20 20 20 31 37 2c 20 20 31 36 35 2c  149,   17,  165,
313cb 20 20 20 36 36 2c 20 20 31 35 30 2c 20 20 31 36     66,  150,  16
313cc 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20 20  5,  165,  165,  
313cd 31 34 39 2c 20 20 20 31 33 2c 0a 20 2f 2a 20 20  149,   13,. /*  
313ce 31 33 35 30 20 2a 2f 20 20 20 31 35 30 2c 20 20  1350 */   150,  
313cf 31 34 39 2c 20 20 31 35 31 2c 20 20 31 35 30 2c  149,  151,  150,
313d0 20 20 31 36 35 2c 20 20 20 33 31 2c 20 20 31 35    165,   31,  15
313d1 39 2c 20 20 20 33 33 2c 20 20 31 35 30 2c 20 20  9,   33,  150,  
313d2 31 36 35 2c 0a 20 2f 2a 20 20 31 33 36 30 20 2a  165,. /*  1360 *
313d3 2f 20 20 20 31 35 31 2c 20 20 31 39 34 2c 20 20  /   151,  194,  
313d4 31 35 30 2c 20 20 20 38 36 2c 20 20 20 38 37 2c  150,   86,   87,
313d5 20 20 31 36 35 2c 20 20 20 34 32 2c 20 20 20 32    165,   42,   2
313d6 35 2c 20 20 31 36 35 2c 20 20 31 31 35 2c 0a 20  5,  165,  115,. 
313d7 2f 2a 20 20 31 33 37 30 20 2a 2f 20 20 20 20 39  /*  1370 */    9
313d8 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20 20  3,   94,   95,  
313d9 31 36 35 2c 20 20 31 35 30 2c 20 20 20 39 38 2c  165,  150,   98,
313da 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 20 32    150,  165,   2
313db 32 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20 31 33  2,   55,. /*  13
313dc 38 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 35  80 */   150,   5
313dd 37 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20  7,   26,   27,  
313de 31 39 39 2c 20 20 20 36 31 2c 20 20 31 32 35 2c  199,   61,  125,
313df 20 20 31 35 30 2c 20 20 20 36 34 2c 20 20 31 36    150,   64,  16
313e0 35 2c 0a 20 2f 2a 20 20 31 33 39 30 20 2a 2f 20  5,. /*  1390 */ 
313e1 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 32 30    150,  165,  20
313e2 30 2c 20 20 31 32 32 2c 20 20 32 30 31 2c 20 20  0,  122,  201,  
313e3 31 36 35 2c 20 20 31 32 33 2c 20 20 31 35 30 2c  165,  123,  150,
313e4 20 20 31 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a    174,  175,. /*
313e5 20 20 31 34 30 30 20 2a 2f 20 20 20 31 37 34 2c    1400 */   174,
313e6 20 20 31 37 35 2c 20 20 31 36 35 2c 20 20 31 35    175,  165,  15
313e7 30 2c 20 20 31 32 31 2c 20 20 31 36 35 2c 20 20  0,  121,  165,  
313e8 31 32 39 2c 20 20 31 33 30 2c 20 20 31 33 31 2c  129,  130,  131,
313e9 20 20 31 33 32 2c 0a 20 2f 2a 20 20 31 34 31 30    132,. /*  1410
313ea 20 2a 2f 20 20 20 31 33 33 2c 20 20 31 33 34 2c   */   133,  134,
313eb 20 20 31 36 35 2c 20 20 32 30 32 2c 20 20 31 37    165,  202,  17
313ec 34 2c 20 20 31 37 35 2c 20 20 32 30 33 2c 20 20  4,  175,  203,  
313ed 31 32 34 2c 20 20 31 36 35 2c 20 20 31 33 35 2c  124,  165,  135,
313ee 0a 20 2f 2a 20 20 31 34 32 30 20 2a 2f 20 20 20  . /*  1420 */   
313ef 31 35 37 2c 20 20 31 31 37 2c 20 20 20 36 36 2c  157,  117,   66,
313f0 20 20 32 32 37 2c 20 20 31 35 37 2c 20 20 32 31    227,  157,  21
313f1 30 2c 20 20 31 30 34 2c 20 20 32 31 31 2c 20 20  0,  104,  211,  
313f2 31 32 30 2c 20 20 31 30 35 2c 0a 20 2f 2a 20 20  120,  105,. /*  
313f3 31 34 33 30 20 2a 2f 20 20 20 31 30 36 2c 20 20  1430 */   106,  
313f4 32 31 30 2c 20 20 31 37 36 2c 20 20 32 31 31 2c  210,  176,  211,
313f5 20 20 31 31 30 2c 20 20 32 31 30 2c 20 20 31 30    110,  210,  10
313f6 34 2c 20 20 20 34 37 2c 20 20 32 31 31 2c 20 20  4,   47,  211,  
313f7 31 31 35 2c 0a 20 2f 2a 20 20 31 34 34 30 20 2a  115,. /*  1440 *
313f8 2f 20 20 20 31 37 36 2c 20 20 31 38 34 2c 20 20  /   176,  184,  
313f9 20 38 36 2c 20 20 20 38 37 2c 20 20 31 37 36 2c   86,   87,  176,
313fa 20 20 31 37 39 2c 20 20 31 30 33 2c 20 20 31 37    179,  103,  17
313fb 38 2c 20 20 31 37 36 2c 20 20 20 32 32 2c 0a 20  8,  176,   22,. 
313fc 2f 2a 20 20 31 34 35 30 20 2a 2f 20 20 20 20 39  /*  1450 */    9
313fd 34 2c 20 20 20 39 32 2c 20 20 31 32 38 2c 20 20  4,   92,  128,  
313fe 32 33 30 2c 20 20 20 39 38 2c 20 20 31 37 39 2c  230,   98,  179,
313ff 20 20 31 37 36 2c 20 20 31 37 36 2c 20 20 31 38    176,  176,  18
31400 34 2c 20 20 32 33 30 2c 0a 20 2f 2a 20 20 31 34  4,  230,. /*  14
31401 36 30 20 2a 2f 20 20 20 20 31 38 2c 20 20 31 37  60 */    18,  17
31402 36 2c 20 20 31 35 36 2c 20 20 31 33 39 2c 20 20  6,  156,  139,  
31403 31 35 37 2c 20 20 31 35 36 2c 20 20 20 34 35 2c  157,  156,   45,
31404 20 20 31 35 37 2c 20 20 31 35 36 2c 20 20 31 33    157,  156,  13
31405 35 2c 0a 20 2f 2a 20 20 31 34 37 30 20 2a 2f 20  5,. /*  1470 */ 
31406 20 20 31 35 37 2c 20 20 31 35 37 2c 20 20 32 33    157,  157,  23
31407 38 2c 20 20 31 35 36 2c 20 20 20 36 38 2c 20 20  8,  156,   68,  
31408 32 33 39 2c 20 20 31 35 37 2c 20 20 31 38 39 2c  239,  157,  189,
31409 20 20 31 38 39 2c 20 20 32 31 39 2c 0a 20 2f 2a    189,  219,. /*
3140a 20 20 31 34 38 30 20 2a 2f 20 20 20 20 32 32 2c    1480 */    22,
3140b 20 20 31 39 39 2c 20 20 31 35 37 2c 20 20 31 39    199,  157,  19
3140c 32 2c 20 20 20 31 38 2c 20 20 31 32 39 2c 20 20  2,   18,  129,  
3140d 31 33 30 2c 20 20 31 33 31 2c 20 20 31 39 32 2c  130,  131,  192,
3140e 20 20 31 39 32 2c 0a 20 2f 2a 20 20 31 34 39 30    192,. /*  1490
3140f 20 2a 2f 20 20 20 31 39 32 2c 20 20 31 39 39 2c   */   192,  199,
31410 20 20 31 38 39 2c 20 20 32 31 39 2c 20 20 31 35    189,  219,  15
31411 37 2c 20 20 32 34 33 2c 20 20 20 34 30 2c 20 20  7,  243,   40,  
31412 32 34 33 2c 20 20 31 35 37 2c 20 20 31 35 37 2c  243,  157,  157,
31413 0a 20 2f 2a 20 20 31 35 30 30 20 2a 2f 20 20 20  . /*  1500 */   
31414 32 34 36 2c 20 20 20 33 38 2c 20 20 31 35 33 2c  246,   38,  153,
31415 20 20 31 39 36 2c 20 20 31 39 38 2c 20 20 31 36    196,  198,  16
31416 36 2c 20 20 32 33 33 2c 20 20 32 33 33 2c 20 20  6,  233,  233,  
31417 32 32 38 2c 20 20 31 37 37 2c 0a 20 2f 2a 20 20  228,  177,. /*  
31418 31 35 31 30 20 2a 2f 20 20 20 31 37 37 2c 20 20  1510 */   177,  
31419 32 30 39 2c 20 20 31 37 37 2c 20 20 31 38 32 2c  209,  177,  182,
3141a 20 20 31 37 37 2c 20 20 31 36 36 2c 20 20 31 37    177,  166,  17
3141b 37 2c 20 20 31 36 36 2c 20 20 31 37 38 2c 20 20  7,  166,  178,  
3141c 32 34 32 2c 0a 20 2f 2a 20 20 31 35 32 30 20 2a  242,. /*  1520 *
3141d 2f 20 20 20 31 39 39 2c 20 20 32 34 32 2c 20 20  /   199,  242,  
3141e 32 30 39 2c 20 20 32 30 39 2c 20 20 31 39 39 2c  209,  209,  199,
3141f 20 20 31 34 38 2c 20 20 31 39 36 2c 20 20 31 36    148,  196,  16
31420 36 2c 20 20 32 30 38 2c 20 20 31 39 35 2c 0a 20  6,  208,  195,. 
31421 2f 2a 20 20 31 35 33 30 20 2a 2f 20 20 20 31 39  /*  1530 */   19
31422 35 2c 20 20 32 33 36 2c 20 20 32 33 37 2c 20 20  5,  236,  237,  
31423 31 39 35 2c 20 20 31 39 31 2c 20 20 31 38 33 2c  195,  191,  183,
31424 20 20 31 38 33 2c 20 20 31 38 36 2c 20 20 31 37    183,  186,  17
31425 34 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 31 35  4,  174,. /*  15
31426 34 30 20 2a 2f 20 20 20 31 38 36 2c 20 20 31 31  40 */   186,  11
31427 35 2c 20 20 20 39 32 2c 0a 7d 3b 0a 23 64 65 66  5,   92,.};.#def
31428 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 55 53 45  ine YY_SHIFT_USE
31429 5f 44 46 4c 54 20 28 2d 37 30 29 0a 23 64 65 66  _DFLT (-70).#def
3142a 69 6e 65 20 59 59 5f 53 48 49 46 54 5f 43 4f 55  ine YY_SHIFT_COU
3142b 4e 54 20 28 34 31 37 29 0a 23 64 65 66 69 6e 65  NT (417).#define
3142c 20 59 59 5f 53 48 49 46 54 5f 4d 49 4e 20 20 20   YY_SHIFT_MIN   
3142d 28 2d 36 39 29 0a 23 64 65 66 69 6e 65 20 59 59  (-69).#define YY
3142e 5f 53 48 49 46 54 5f 4d 41 58 20 20 20 28 31 34  _SHIFT_MAX   (14
3142f 36 36 29 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  66).static const
31430 20 73 68 6f 72 74 20 79 79 5f 73 68 69 66 74 5f   short yy_shift_
31431 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20  ofst[] = {. /*  
31432 20 20 20 30 20 2a 2f 20 20 31 31 32 30 2c 20 31     0 */  1120, 1
31433 31 30 34 2c 20 31 33 32 34 2c 20 31 31 30 34 2c  104, 1324, 1104,
31434 20 31 31 39 30 2c 20 31 31 39 30 2c 20 20 20 39   1190, 1190,   9
31435 30 2c 20 20 20 39 30 2c 20 20 20 20 31 2c 20 20  0,   90,    1,  
31436 2d 31 39 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a  -19,. /*    10 *
31437 2f 20 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31  /  1190, 1190, 1
31438 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c  190, 1190, 1190,
31439 20 20 32 38 30 2c 20 20 33 39 31 2c 20 20 37 32    280,  391,  72
3143a 31 2c 20 31 30 39 31 2c 20 31 31 39 30 2c 0a 20  1, 1091, 1190,. 
3143b 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 31 31 39  /*    20 */  119
3143c 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31  0, 1190, 1190, 1
3143d 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c  190, 1190, 1190,
3143e 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39   1190, 1190, 119
3143f 30 2c 20 31 31 39 30 2c 0a 20 2f 2a 20 20 20 20  0, 1190,. /*    
31440 33 30 20 2a 2f 20 20 31 31 39 30 2c 20 31 31 39  30 */  1190, 119
31441 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31  0, 1190, 1190, 1
31442 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c  190, 1190, 1190,
31443 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39   1190, 1190, 119
31444 30 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20  0,. /*    40 */ 
31445 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39   1190, 1190, 119
31446 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31  0, 1190, 1190, 1
31447 31 39 30 2c 20 31 31 39 30 2c 20 31 32 37 37 2c  190, 1190, 1277,
31448 20 31 31 39 30 2c 20 31 31 39 30 2c 0a 20 2f 2a   1190, 1190,. /*
31449 20 20 20 20 35 30 20 2a 2f 20 20 31 31 39 30 2c      50 */  1190,
3144a 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39   1190, 1190, 119
3144b 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c 20 31  0, 1190, 1190, 1
3144c 31 39 30 2c 20 31 31 39 30 2c 20 31 31 39 30 2c  190, 1190, 1190,
3144d 20 31 31 39 30 2c 0a 20 2f 2a 20 20 20 20 36 30   1190,. /*    60
3144e 20 2a 2f 20 20 31 31 39 30 2c 20 20 2d 34 39 2c   */  1190,  -49,
3144f 20 20 32 38 37 2c 20 20 33 39 31 2c 20 20 33 39    287,  391,  39
31450 31 2c 20 20 39 38 38 2c 20 20 39 38 38 2c 20 20  1,  988,  988,  
31451 32 31 35 2c 20 31 34 32 36 2c 20 20 20 35 35 2c  215, 1426,   55,
31452 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20  . /*    70 */   
31453 36 34 37 2c 20 20 35 37 33 2c 20 20 34 39 39 2c  647,  573,  499,
31454 20 20 34 32 35 2c 20 20 33 35 31 2c 20 20 32 37    425,  351,  27
31455 37 2c 20 20 32 30 33 2c 20 20 31 32 39 2c 20 20  7,  203,  129,  
31456 37 39 35 2c 20 20 37 39 35 2c 0a 20 2f 2a 20 20  795,  795,. /*  
31457 20 20 38 30 20 2a 2f 20 20 20 37 39 35 2c 20 20    80 */   795,  
31458 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c  795,  795,  795,
31459 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39    795,  795,  79
3145a 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20  5,  795,  795,  
3145b 37 39 35 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a  795,. /*    90 *
3145c 2f 20 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20  /   795,  795,  
3145d 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c  795,  795,  795,
3145e 20 20 37 39 35 2c 20 20 38 36 39 2c 20 20 37 39    795,  869,  79
3145f 35 2c 20 20 39 34 33 2c 20 31 30 31 37 2c 0a 20  5,  943, 1017,. 
31460 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 31 30 31  /*   100 */  101
31461 37 2c 20 20 2d 36 39 2c 20 20 2d 36 39 2c 20 20  7,  -69,  -69,  
31462 2d 36 39 2c 20 20 2d 36 39 2c 20 20 20 2d 31 2c  -69,  -69,   -1,
31463 20 20 20 2d 31 2c 20 20 20 35 38 2c 20 20 31 33     -1,   58,  13
31464 38 2c 20 20 2d 34 34 2c 0a 20 2f 2a 20 20 20 31  8,  -44,. /*   1
31465 31 30 20 2a 2f 20 20 20 33 39 31 2c 20 20 33 39  10 */   391,  39
31466 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  1,  391,  391,  
31467 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c  391,  391,  391,
31468 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39    391,  391,  39
31469 31 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20  1,. /*   120 */ 
3146a 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39    391,  391,  39
3146b 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  1,  391,  391,  
3146c 33 39 31 2c 20 20 34 36 33 2c 20 20 35 30 36 2c  391,  463,  506,
3146d 20 20 33 39 31 2c 20 20 33 39 31 2c 0a 20 2f 2a    391,  391,. /*
3146e 20 20 20 31 33 30 20 2a 2f 20 20 20 33 39 31 2c     130 */   391,
3146f 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 32 31    391,  391,  21
31470 35 2c 20 20 39 35 39 2c 20 31 34 35 30 2c 20 20  5,  959, 1450,  
31471 2d 37 30 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c  -70,  -70,  -70,
31472 20 31 33 35 36 2c 0a 20 2f 2a 20 20 20 31 34 30   1356,. /*   140
31473 20 2a 2f 20 20 20 20 37 33 2c 20 20 34 33 33 2c   */    73,  433,
31474 20 20 34 33 33 2c 20 20 33 36 31 2c 20 20 33 30    433,  361,  30
31475 39 2c 20 20 31 36 35 2c 20 20 20 36 37 2c 20 20  9,  165,   67,  
31476 32 38 34 2c 20 20 34 36 36 2c 20 20 32 39 31 2c  284,  466,  291,
31477 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20  . /*   150 */   
31478 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c  391,  391,  391,
31479 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39    391,  391,  39
3147a 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  1,  391,  391,  
3147b 33 39 31 2c 20 20 33 39 31 2c 0a 20 2f 2a 20 20  391,  391,. /*  
3147c 20 31 36 30 20 2a 2f 20 20 20 33 39 31 2c 20 20   160 */   391,  
3147d 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c  391,  391,  391,
3147e 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39    391,  391,  39
3147f 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  1,  391,  391,  
31480 33 39 31 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a  391,. /*   170 *
31481 2f 20 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20  /   391,  391,  
31482 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c  391,  391,  391,
31483 20 20 33 39 31 2c 20 20 33 39 31 2c 20 20 33 39    391,  391,  39
31484 31 2c 20 20 33 39 31 2c 20 20 33 39 31 2c 0a 20  1,  391,  391,. 
31485 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 33 39  /*   180 */   39
31486 31 2c 20 20 33 39 31 2c 20 20 35 30 31 2c 20 20  1,  391,  501,  
31487 32 32 31 2c 20 20 32 32 31 2c 20 20 32 32 31 2c  221,  221,  221,
31488 20 20 37 30 35 2c 20 20 37 39 37 2c 20 31 34 32    705,  797, 142
31489 36 2c 20 31 34 32 36 2c 0a 20 2f 2a 20 20 20 31  6, 1426,. /*   1
3148a 39 30 20 2a 2f 20 20 31 34 32 36 2c 20 20 2d 37  90 */  1426,  -7
3148b 30 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c 20 20  0,  -70,  -70,  
3148c 31 33 39 2c 20 20 31 37 31 2c 20 20 31 37 31 2c  139,  171,  171,
3148d 20 20 20 31 32 2c 20 20 35 36 38 2c 20 20 35 36     12,  568,  56
3148e 38 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20  8,. /*   200 */ 
3148f 20 20 32 30 39 2c 20 20 34 32 37 2c 20 20 33 37    209,  427,  37
31490 30 2c 20 20 33 36 37 2c 20 20 33 35 32 2c 20 20  0,  367,  352,  
31491 32 39 36 2c 20 20 20 33 38 2c 20 20 20 33 38 2c  296,   38,   38,
31492 20 20 20 33 38 2c 20 20 20 33 38 2c 0a 20 2f 2a     38,   38,. /*
31493 20 20 20 32 31 30 20 2a 2f 20 20 20 33 34 38 2c     210 */   348,
31494 20 20 35 36 39 2c 20 20 20 33 38 2c 20 20 20 33    569,   38,   3
31495 38 2c 20 20 20 37 34 2c 20 20 35 38 37 2c 20 20  8,   74,  587,  
31496 35 39 32 2c 20 20 20 31 37 2c 20 20 34 39 35 2c  592,   17,  495,
31497 20 20 20 38 37 2c 0a 20 2f 2a 20 20 20 32 32 30     87,. /*   220
31498 20 2a 2f 20 20 20 20 38 37 2c 20 20 33 37 32 2c   */    87,  372,
31499 20 20 34 39 35 2c 20 20 33 37 32 2c 20 20 37 35    495,  372,  75
3149a 35 2c 20 20 32 31 35 2c 20 20 32 39 33 2c 20 20  5,  215,  293,  
3149b 32 31 35 2c 20 20 32 39 33 2c 20 20 31 34 30 2c  215,  293,  140,
3149c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20  . /*   230 */   
3149d 32 39 33 2c 20 20 20 38 37 2c 20 20 32 39 33 2c  293,   87,  293,
3149e 20 20 32 39 33 2c 20 20 37 36 32 2c 20 20 36 33    293,  762,  63
3149f 38 2c 20 20 36 33 38 2c 20 20 32 31 35 2c 20 20  8,  638,  215,  
314a0 20 37 38 2c 20 20 20 35 31 2c 0a 20 2f 2a 20 20   78,   51,. /*  
314a1 20 32 34 30 20 2a 2f 20 20 20 32 34 36 2c 20 31   240 */   246, 1
314a2 34 36 33 2c 20 31 33 30 34 2c 20 31 33 30 34 2c  463, 1304, 1304,
314a3 20 31 34 35 36 2c 20 31 34 35 36 2c 20 31 33 30   1456, 1456, 130
314a4 34 2c 20 31 34 35 38 2c 20 31 34 30 36 2c 20 31  4, 1458, 1406, 1
314a5 32 36 31 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a  261,. /*   250 *
314a6 2f 20 20 31 34 36 36 2c 20 31 34 36 36 2c 20 31  /  1466, 1466, 1
314a7 34 36 36 2c 20 31 34 36 36 2c 20 31 33 30 34 2c  466, 1466, 1304,
314a8 20 31 32 36 31 2c 20 31 34 35 38 2c 20 31 34 30   1261, 1458, 140
314a9 36 2c 20 31 34 30 36 2c 20 31 33 30 34 2c 0a 20  6, 1406, 1304,. 
314aa 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 31 34 34  /*   260 */  144
314ab 32 2c 20 31 33 33 34 2c 20 31 34 32 31 2c 20 31  2, 1334, 1421, 1
314ac 33 30 34 2c 20 31 33 30 34 2c 20 31 34 34 32 2c  304, 1304, 1442,
314ad 20 31 33 30 34 2c 20 31 34 34 32 2c 20 31 33 30   1304, 1442, 130
314ae 34 2c 20 31 34 34 32 2c 0a 20 2f 2a 20 20 20 32  4, 1442,. /*   2
314af 37 30 20 2a 2f 20 20 31 34 32 37 2c 20 31 33 33  70 */  1427, 133
314b0 32 2c 20 31 33 33 32 2c 20 31 33 33 32 2c 20 31  2, 1332, 1332, 1
314b1 33 39 30 2c 20 31 33 35 39 2c 20 31 33 35 39 2c  390, 1359, 1359,
314b2 20 31 34 32 37 2c 20 31 33 33 32 2c 20 31 33 34   1427, 1332, 134
314b3 33 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20  3,. /*   280 */ 
314b4 20 31 33 33 32 2c 20 31 33 39 30 2c 20 31 33 33   1332, 1390, 133
314b5 32 2c 20 31 33 33 32 2c 20 31 33 30 38 2c 20 31  2, 1332, 1308, 1
314b6 33 32 32 2c 20 31 33 30 38 2c 20 31 33 32 32 2c  322, 1308, 1322,
314b7 20 31 33 30 38 2c 20 31 33 32 32 2c 0a 20 2f 2a   1308, 1322,. /*
314b8 20 20 20 32 39 30 20 2a 2f 20 20 31 33 30 34 2c     290 */  1304,
314b9 20 31 33 30 34 2c 20 31 32 38 34 2c 20 31 32 39   1304, 1284, 129
314ba 33 2c 20 31 32 38 33 2c 20 31 32 37 33 2c 20 31  3, 1283, 1273, 1
314bb 32 37 31 2c 20 31 32 36 31 2c 20 31 32 35 34 2c  271, 1261, 1254,
314bc 20 31 33 34 32 2c 0a 20 2f 2a 20 20 20 33 30 30   1342,. /*   300
314bd 20 2a 2f 20 20 31 33 33 36 2c 20 31 33 33 36 2c   */  1336, 1336,
314be 20 31 33 30 33 2c 20 31 33 30 33 2c 20 31 33 30   1303, 1303, 130
314bf 33 2c 20 31 33 30 33 2c 20 20 2d 37 30 2c 20 20  3, 1303,  -70,  
314c0 2d 37 30 2c 20 20 2d 37 30 2c 20 20 2d 37 30 2c  -70,  -70,  -70,
314c1 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20  . /*   310 */   
314c2 2d 37 30 2c 20 20 2d 37 30 2c 20 20 33 33 34 2c  -70,  -70,  334,
314c3 20 20 31 32 30 2c 20 20 35 33 35 2c 20 20 32 33    120,  535,  23
314c4 32 2c 20 20 36 32 34 2c 20 20 39 34 34 2c 20 20  2,  624,  944,  
314c5 31 38 38 2c 20 20 39 30 35 2c 0a 20 2f 2a 20 20  188,  905,. /*  
314c6 20 33 32 30 20 2a 2f 20 20 20 38 39 37 2c 20 20   320 */   897,  
314c7 38 37 33 2c 20 20 38 37 30 2c 20 20 38 32 35 2c  873,  870,  825,
314c8 20 20 37 35 34 2c 20 20 37 31 39 2c 20 20 36 35    754,  719,  65
314c9 31 2c 20 20 35 32 37 2c 20 20 34 34 34 2c 20 20  1,  527,  444,  
314ca 31 32 35 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a  125,. /*   330 *
314cb 2f 20 20 20 35 31 34 2c 20 20 34 33 34 2c 20 31  /   514,  434, 1
314cc 32 39 34 2c 20 31 32 37 39 2c 20 31 32 35 39 2c  294, 1279, 1259,
314cd 20 31 31 34 39 2c 20 31 32 35 38 2c 20 31 31 38   1149, 1258, 118
314ce 38 2c 20 31 32 33 35 2c 20 31 32 33 38 2c 0a 20  8, 1235, 1238,. 
314cf 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 31 32 33  /*   340 */  123
314d0 31 2c 20 31 31 30 35 2c 20 31 32 32 38 2c 20 31  1, 1105, 1228, 1
314d1 31 32 33 2c 20 31 31 33 34 2c 20 31 31 30 30 2c  123, 1134, 1100,
314d2 20 31 32 31 30 2c 20 31 31 30 36 2c 20 31 31 39   1210, 1106, 119
314d3 38 2c 20 31 32 30 35 2c 0a 20 2f 2a 20 20 20 33  8, 1205,. /*   3
314d4 35 30 20 2a 2f 20 20 31 30 38 33 2c 20 31 31 37  50 */  1083, 117
314d5 34 2c 20 31 30 35 37 2c 20 31 31 32 34 2c 20 31  4, 1057, 1124, 1
314d6 31 31 38 2c 20 31 30 37 35 2c 20 31 31 33 38 2c  118, 1075, 1138,
314d7 20 31 31 33 39 2c 20 31 30 35 32 2c 20 31 31 36   1139, 1052, 116
314d8 33 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20  3,. /*   360 */ 
314d9 20 31 31 35 38 2c 20 31 31 33 33 2c 20 31 30 35   1158, 1133, 105
314da 30 2c 20 20 39 37 38 2c 20 31 30 39 39 2c 20 31  0,  978, 1099, 1
314db 31 30 37 2c 20 31 30 38 39 2c 20 31 30 32 30 2c  107, 1089, 1020,
314dc 20 31 30 33 34 2c 20 20 39 36 38 2c 0a 20 2f 2a   1034,  968,. /*
314dd 20 20 20 33 37 30 20 2a 2f 20 20 31 30 33 39 2c     370 */  1039,
314de 20 31 30 34 38 2c 20 20 39 39 31 2c 20 20 38 39   1048,  991,  89
314df 39 2c 20 20 39 35 38 2c 20 20 39 38 31 2c 20 20  9,  958,  981,  
314e0 39 34 32 2c 20 20 39 37 36 2c 20 20 39 37 34 2c  942,  976,  974,
314e1 20 20 39 36 36 2c 0a 20 2f 2a 20 20 20 33 38 30    966,. /*   380
314e2 20 2a 2f 20 20 20 39 35 37 2c 20 20 39 32 31 2c   */   957,  921,
314e3 20 20 39 30 30 2c 20 20 38 33 33 2c 20 20 38 36    900,  833,  86
314e4 33 2c 20 20 38 36 37 2c 20 20 38 33 39 2c 20 20  3,  867,  839,  
314e5 38 33 36 2c 20 20 37 33 35 2c 20 20 38 32 32 2c  836,  735,  822,
314e6 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20  . /*   390 */   
314e7 38 30 33 2c 20 20 38 30 36 2c 20 20 37 30 36 2c  803,  806,  706,
314e8 20 20 36 38 34 2c 20 20 37 32 33 2c 20 20 37 33    684,  723,  73
314e9 30 2c 20 20 36 35 38 2c 20 20 36 38 34 2c 20 20  0,  658,  684,  
314ea 37 32 38 2c 20 20 36 39 30 2c 0a 20 2f 2a 20 20  728,  690,. /*  
314eb 20 34 30 30 20 2a 2f 20 20 20 36 37 38 2c 20 20   400 */   678,  
314ec 36 36 30 2c 20 20 36 35 35 2c 20 20 36 37 39 2c  660,  655,  679,
314ed 20 20 36 35 30 2c 20 20 36 31 36 2c 20 20 36 30    650,  616,  60
314ee 36 2c 20 20 35 37 31 2c 20 20 34 36 38 2c 20 20  6,  571,  468,  
314ef 33 38 39 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a  389,. /*   410 *
314f0 2f 20 20 20 33 34 33 2c 20 20 32 39 34 2c 20 20  /   343,  294,  
314f1 31 38 36 2c 20 20 20 20 33 2c 20 20 20 34 30 2c  186,    3,   40,
314f2 20 20 20 20 36 2c 20 20 20 2d 33 2c 20 20 20 20      6,   -3,    
314f3 35 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59  5,.};.#define YY
314f4 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54  _REDUCE_USE_DFLT
314f5 20 28 2d 32 32 32 29 0a 23 64 65 66 69 6e 65 20   (-222).#define 
314f6 59 59 5f 52 45 44 55 43 45 5f 43 4f 55 4e 54 20  YY_REDUCE_COUNT 
314f7 28 33 31 31 29 0a 23 64 65 66 69 6e 65 20 59 59  (311).#define YY
314f8 5f 52 45 44 55 43 45 5f 4d 49 4e 20 20 20 28 2d  _REDUCE_MIN   (-
314f9 32 32 31 29 0a 23 64 65 66 69 6e 65 20 59 59 5f  221).#define YY_
314fa 52 45 44 55 43 45 5f 4d 41 58 20 20 20 28 31 33  REDUCE_MAX   (13
314fb 37 37 29 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  77).static const
314fc 20 73 68 6f 72 74 20 79 79 5f 72 65 64 75 63 65   short yy_reduce
314fd 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20  _ofst[] = {. /* 
314fe 20 20 20 20 30 20 2a 2f 20 20 20 34 39 33 2c 20      0 */   493, 
314ff 31 30 39 32 2c 20 31 30 32 31 2c 20 20 31 34 37  1092, 1021,  147
31500 2c 20 20 31 35 38 2c 20 20 31 35 35 2c 20 20 20  ,  158,  155,   
31501 38 36 2c 20 20 20 31 38 2c 20 20 20 38 31 2c 20  86,   18,   81, 
31502 20 31 37 32 2c 0a 20 2f 2a 20 20 20 20 31 30 20   172,. /*    10 
31503 2a 2f 20 20 20 33 38 35 2c 20 20 33 37 37 2c 20  */   385,  377, 
31504 20 33 30 38 2c 20 20 33 37 39 2c 20 20 32 39 37   308,  379,  297
31505 2c 20 20 32 35 32 2c 20 20 2d 34 33 2c 20 2d 31  ,  252,  -43, -1
31506 34 36 2c 20 31 32 34 30 2c 20 31 32 32 36 2c 0a  46, 1240, 1226,.
31507 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 31 32   /*    20 */  12
31508 32 34 2c 20 31 31 34 31 2c 20 31 31 33 37 2c 20  24, 1141, 1137, 
31509 31 30 39 37 2c 20 31 30 39 35 2c 20 31 30 39 30  1097, 1095, 1090
3150a 2c 20 31 30 33 30 2c 20 20 39 37 35 2c 20 20 39  , 1030,  975,  9
3150b 36 34 2c 20 20 39 36 32 2c 0a 20 2f 2a 20 20 20  64,  962,. /*   
3150c 20 33 30 20 2a 2f 20 20 20 39 30 38 2c 20 20 38   30 */   908,  8
3150d 39 30 2c 20 20 38 38 37 2c 20 20 38 37 34 2c 20  90,  887,  874, 
3150e 20 38 33 34 2c 20 20 38 31 36 2c 20 20 38 31 33   834,  816,  813
3150f 2c 20 20 38 30 30 2c 20 20 37 36 30 2c 20 20 37  ,  800,  760,  7
31510 35 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f  58,. /*    40 */
31511 20 20 20 37 34 32 2c 20 20 37 33 39 2c 20 20 37     742,  739,  7
31512 32 36 2c 20 20 36 38 36 2c 20 20 36 37 32 2c 20  26,  686,  672, 
31513 20 36 36 38 2c 20 20 36 36 35 2c 20 20 36 35 32   668,  665,  652
31514 2c 20 20 36 31 31 2c 20 20 36 30 39 2c 0a 20 2f  ,  611,  609,. /
31515 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 36 30 37  *    50 */   607
31516 2c 20 20 35 39 31 2c 20 20 35 37 38 2c 20 20 35  ,  591,  578,  5
31517 33 37 2c 20 20 35 32 36 2c 20 20 35 32 34 2c 20  37,  526,  524, 
31518 20 35 32 30 2c 20 20 35 30 34 2c 20 20 34 35 36   520,  504,  456
31519 2c 20 20 34 35 30 2c 0a 20 2f 2a 20 20 20 20 36  ,  450,. /*    6
3151a 30 20 2a 2f 20 20 20 33 37 31 2c 20 2d 32 32 31  0 */   371, -221
3151b 2c 20 20 34 37 34 2c 20 20 33 36 39 2c 20 20 35  ,  474,  369,  5
3151c 31 37 2c 20 20 33 39 35 2c 20 20 20 39 32 2c 20  17,  395,   92, 
3151d 20 33 30 31 2c 20 20 34 30 31 2c 20 20 31 31 38   301,  401,  118
3151e 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20  ,. /*    70 */  
3151f 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38   118,  118,  118
31520 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31  ,  118,  118,  1
31521 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20  18,  118,  118, 
31522 20 31 31 38 2c 20 20 31 31 38 2c 0a 20 2f 2a 20   118,  118,. /* 
31523 20 20 20 38 30 20 2a 2f 20 20 20 31 31 38 2c 20     80 */   118, 
31524 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38   118,  118,  118
31525 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31  ,  118,  118,  1
31526 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20  18,  118,  118, 
31527 20 31 31 38 2c 0a 20 2f 2a 20 20 20 20 39 30 20   118,. /*    90 
31528 2a 2f 20 20 20 31 31 38 2c 20 20 31 31 38 2c 20  */   118,  118, 
31529 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38   118,  118,  118
3152a 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31  ,  118,  118,  1
3152b 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 0a  18,  118,  118,.
3152c 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 31   /*   100 */   1
3152d 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20  18,  118,  118, 
3152e 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38   118,  118,  118
3152f 2c 20 20 31 31 38 2c 20 20 32 30 38 2c 20 20 31  ,  118,  208,  1
31530 31 38 2c 20 20 31 31 38 2c 0a 20 2f 2a 20 20 20  18,  118,. /*   
31531 31 31 30 20 2a 2f 20 20 31 30 33 38 2c 20 20 39  110 */  1038,  9
31532 39 34 2c 20 20 39 38 33 2c 20 20 39 36 39 2c 20  94,  983,  969, 
31533 20 38 39 32 2c 20 20 38 39 31 2c 20 20 38 38 38   892,  891,  888
31534 2c 20 20 38 31 38 2c 20 20 37 36 31 2c 20 20 38  ,  818,  761,  8
31535 33 32 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f  32,. /*   120 */
31536 20 20 20 36 31 33 2c 20 20 36 30 34 2c 20 20 35     613,  604,  5
31537 32 33 2c 20 20 37 34 34 2c 20 20 38 33 30 2c 20  23,  744,  830, 
31538 20 37 37 31 2c 20 20 35 39 35 2c 20 20 34 33 30   771,  595,  430
31539 2c 20 20 37 34 30 2c 20 20 36 37 30 2c 0a 20 2f  ,  740,  670,. /
3153a 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 36 36 39  *   130 */   669
3153b 2c 20 20 36 36 36 2c 20 20 36 31 30 2c 20 20 35  ,  666,  610,  5
3153c 37 39 2c 20 20 31 31 38 2c 20 20 31 31 38 2c 20  79,  118,  118, 
3153d 20 31 31 38 2c 20 20 31 31 38 2c 20 20 31 31 38   118,  118,  118
3153e 2c 20 20 36 36 37 2c 0a 20 2f 2a 20 20 20 31 34  ,  667,. /*   14
3153f 30 20 2a 2f 20 20 20 39 37 32 2c 20 20 38 38 30  0 */   972,  880
31540 2c 20 20 37 38 36 2c 20 20 39 39 36 2c 20 31 32  ,  786,  996, 12
31541 35 33 2c 20 31 32 34 37 2c 20 31 32 33 37 2c 20  53, 1247, 1237, 
31542 31 30 30 31 2c 20 20 37 35 30 2c 20 20 37 35 30  1001,  750,  750
31543 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20  ,. /*   150 */  
31544 31 32 33 30 2c 20 31 32 31 32 2c 20 31 32 30 38  1230, 1212, 1208
31545 2c 20 31 32 30 33 2c 20 31 32 30 30 2c 20 31 31  , 1203, 1200, 11
31546 39 34 2c 20 31 31 38 39 2c 20 31 31 38 32 2c 20  94, 1189, 1182, 
31547 31 31 38 31 2c 20 31 31 38 30 2c 0a 20 2f 2a 20  1181, 1180,. /* 
31548 20 20 31 36 30 20 2a 2f 20 20 31 31 37 37 2c 20    160 */  1177, 
31549 31 31 36 30 2c 20 31 31 34 32 2c 20 31 31 33 30  1160, 1142, 1130
3154a 2c 20 31 31 32 39 2c 20 31 31 32 38 2c 20 31 31  , 1129, 1128, 11
3154b 32 35 2c 20 31 31 30 39 2c 20 31 30 39 38 2c 20  25, 1109, 1098, 
3154c 31 30 39 33 2c 0a 20 2f 2a 20 20 20 31 37 30 20  1093,. /*   170 
3154d 2a 2f 20 20 31 30 37 39 2c 20 31 30 36 33 2c 20  */  1079, 1063, 
3154e 31 30 34 33 2c 20 31 30 34 32 2c 20 31 30 32 32  1043, 1042, 1022
3154f 2c 20 31 30 30 36 2c 20 20 39 39 36 2c 20 20 39  , 1006,  996,  9
31550 39 33 2c 20 20 39 37 30 2c 20 20 39 30 34 2c 0a  93,  970,  904,.
31551 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 33   /*   180 */   3
31552 38 32 2c 20 20 33 37 38 2c 20 20 38 38 36 2c 20  82,  378,  886, 
31553 20 39 31 30 2c 20 20 38 39 33 2c 20 20 38 38 31   910,  893,  881
31554 2c 20 20 38 34 31 2c 20 20 38 38 34 2c 20 20 38  ,  841,  884,  8
31555 31 32 2c 20 20 38 31 31 2c 0a 20 2f 2a 20 20 20  12,  811,. /*   
31556 31 39 30 20 2a 2f 20 20 20 38 31 30 2c 20 20 35  190 */   810,  5
31557 33 39 2c 20 20 36 39 36 2c 20 20 33 35 38 2c 20  39,  696,  358, 
31558 31 33 35 34 2c 20 31 33 36 35 2c 20 31 33 36 34  1354, 1365, 1364
31559 2c 20 31 33 35 31 2c 20 31 33 35 33 2c 20 31 33  , 1351, 1353, 13
3155a 35 32 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f  52,. /*   200 */
3155b 20 20 31 33 32 30 2c 20 31 33 33 38 2c 20 31 33    1320, 1338, 13
3155c 34 33 2c 20 31 33 33 38 2c 20 31 33 33 38 2c 20  43, 1338, 1338, 
3155d 31 33 33 38 2c 20 31 33 33 38 2c 20 31 33 33 38  1338, 1338, 1338
3155e 2c 20 31 33 33 38 2c 20 31 33 33 38 2c 0a 20 2f  , 1338, 1338,. /
3155f 2a 20 20 20 32 31 30 20 2a 2f 20 20 31 32 39 35  *   210 */  1295
31560 2c 20 31 32 39 35 2c 20 31 33 33 35 2c 20 31 33  , 1295, 1335, 13
31561 33 34 2c 20 31 33 32 30 2c 20 31 33 36 31 2c 20  34, 1320, 1361, 
31562 31 33 33 30 2c 20 31 33 37 37 2c 20 31 33 32 35  1330, 1377, 1325
31563 2c 20 31 33 31 34 2c 0a 20 2f 2a 20 20 20 32 32  , 1314,. /*   22
31564 30 20 2a 2f 20 20 31 33 31 33 2c 20 31 32 37 39  0 */  1313, 1279
31565 2c 20 31 33 32 31 2c 20 31 32 37 37 2c 20 31 33  , 1321, 1277, 13
31566 34 30 2c 20 31 33 35 31 2c 20 31 33 33 39 2c 20  40, 1351, 1339, 
31567 31 33 34 39 2c 20 31 33 33 37 2c 20 31 33 33 31  1349, 1337, 1331
31568 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20  ,. /*   230 */  
31569 31 33 33 35 2c 20 31 33 30 32 2c 20 31 33 33 33  1335, 1302, 1333
3156a 2c 20 31 33 33 32 2c 20 31 32 38 30 2c 20 31 32  , 1332, 1280, 12
3156b 37 34 2c 20 31 32 37 33 2c 20 31 33 33 39 2c 20  74, 1273, 1339, 
3156c 31 33 30 36 2c 20 31 33 30 37 2c 0a 20 2f 2a 20  1306, 1307,. /* 
3156d 20 20 32 34 30 20 2a 2f 20 20 31 33 34 39 2c 20    240 */  1349, 
3156e 31 32 35 34 2c 20 31 33 34 32 2c 20 31 33 34 31  1254, 1342, 1341
3156f 2c 20 31 32 35 34 2c 20 31 32 35 32 2c 20 31 33  , 1254, 1252, 13
31570 33 37 2c 20 31 32 37 34 2c 20 31 33 30 33 2c 20  37, 1274, 1303, 
31571 31 32 39 32 2c 0a 20 2f 2a 20 20 20 32 35 30 20  1292,. /*   250 
31572 2a 2f 20 20 31 32 39 38 2c 20 31 32 39 37 2c 20  */  1298, 1297, 
31573 31 32 39 36 2c 20 31 32 39 31 2c 20 31 33 32 35  1296, 1291, 1325
31574 2c 20 31 32 38 32 2c 20 31 32 36 30 2c 20 31 32  , 1282, 1260, 12
31575 38 39 2c 20 31 32 38 38 2c 20 31 33 31 39 2c 0a  89, 1288, 1319,.
31576 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 31 33   /*   260 */  13
31577 31 37 2c 20 31 32 33 36 2c 20 31 32 33 34 2c 20  17, 1236, 1234, 
31578 31 33 31 34 2c 20 31 33 31 33 2c 20 31 33 31 32  1314, 1313, 1312
31579 2c 20 31 33 31 30 2c 20 31 33 30 39 2c 20 31 33  , 1310, 1309, 13
3157a 30 37 2c 20 31 33 30 36 2c 0a 20 2f 2a 20 20 20  07, 1306,. /*   
3157b 32 37 30 20 2a 2f 20 20 31 32 37 36 2c 20 31 32  270 */  1276, 12
3157c 38 35 2c 20 31 32 38 31 2c 20 31 32 38 30 2c 20  85, 1281, 1280, 
3157d 31 32 37 34 2c 20 31 32 32 39 2c 20 31 32 32 33  1274, 1229, 1223
3157e 2c 20 31 32 36 36 2c 20 31 32 37 32 2c 20 31 32  , 1266, 1272, 12
3157f 36 39 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f  69,. /*   280 */
31580 20 20 31 32 36 38 2c 20 31 32 35 37 2c 20 31 32    1268, 1257, 12
31581 36 34 2c 20 31 32 35 36 2c 20 31 32 32 37 2c 20  64, 1256, 1227, 
31582 31 32 32 35 2c 20 31 32 32 32 2c 20 31 32 32 31  1225, 1222, 1221
31583 2c 20 31 32 31 36 2c 20 31 32 31 35 2c 0a 20 2f  , 1216, 1215,. /
31584 2a 20 20 20 32 39 30 20 2a 2f 20 20 31 32 36 37  *   290 */  1267
31585 2c 20 31 32 36 33 2c 20 31 31 39 36 2c 20 31 32  , 1263, 1196, 12
31586 31 33 2c 20 31 32 31 31 2c 20 31 31 39 33 2c 20  13, 1211, 1193, 
31587 31 31 39 32 2c 20 31 31 38 35 2c 20 31 31 36 37  1192, 1185, 1167
31588 2c 20 31 31 39 37 2c 0a 20 2f 2a 20 20 20 33 30  , 1197,. /*   30
31589 30 20 2a 2f 20 20 31 32 30 39 2c 20 31 32 30 31  0 */  1209, 1201
3158a 2c 20 31 32 30 32 2c 20 31 31 39 39 2c 20 31 31  , 1202, 1199, 11
3158b 39 31 2c 20 31 31 37 39 2c 20 31 30 36 36 2c 20  91, 1179, 1066, 
3158c 31 30 36 35 2c 20 31 30 35 36 2c 20 31 31 34 36  1065, 1056, 1146
3158d 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20  ,. /*   310 */  
3158e 31 31 32 31 2c 20 31 31 33 36 2c 0a 7d 3b 0a 73  1121, 1136,.};.s
3158f 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59 41 43  tatic const YYAC
31590 54 49 4f 4e 54 59 50 45 20 79 79 5f 64 65 66 61  TIONTYPE yy_defa
31591 75 6c 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20  ult[] = {. /*   
31592 20 20 30 20 2a 2f 20 20 20 36 33 34 2c 20 20 38    0 */   634,  8
31593 36 39 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  69,  958,  958, 
31594 20 39 35 38 2c 20 20 38 36 39 2c 20 20 38 39 38   958,  869,  898
31595 2c 20 20 38 39 38 2c 20 20 39 35 38 2c 20 20 37  ,  898,  958,  7
31596 35 37 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f  57,. /*    10 */
31597 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
31598 35 38 2c 20 20 39 35 38 2c 20 20 38 36 37 2c 20  58,  958,  867, 
31599 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 33 32   958,  958,  932
3159a 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
3159b 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 39 35 38  *    20 */   958
3159c 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
3159d 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
3159e 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
3159f 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20 33  ,  958,. /*    3
315a0 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
315a1 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
315a2 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
315a3 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315a4 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20  ,. /*    40 */  
315a5 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315a6 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
315a7 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
315a8 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
315a9 20 20 20 35 30 20 2a 2f 20 20 20 39 35 38 2c 20     50 */   958, 
315aa 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315ab 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
315ac 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
315ad 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20 36 30 20   958,. /*    60 
315ae 2a 2f 20 20 20 39 35 38 2c 20 20 38 34 31 2c 20  */   958,  841, 
315af 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315b0 2c 20 20 38 39 38 2c 20 20 38 39 38 2c 20 20 36  ,  898,  898,  6
315b1 37 33 2c 20 20 37 36 31 2c 20 20 37 39 32 2c 0a  73,  761,  792,.
315b2 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 39   /*    70 */   9
315b3 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
315b4 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315b5 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
315b6 33 31 2c 20 20 39 33 33 2c 0a 20 2f 2a 20 20 20  31,  933,. /*   
315b7 20 38 30 20 2a 2f 20 20 20 38 30 37 2c 20 20 38   80 */   807,  8
315b8 30 36 2c 20 20 38 30 30 2c 20 20 37 39 39 2c 20  06,  800,  799, 
315b9 20 39 31 31 2c 20 20 37 37 32 2c 20 20 37 39 37   911,  772,  797
315ba 2c 20 20 37 39 30 2c 20 20 37 38 33 2c 20 20 37  ,  790,  783,  7
315bb 39 34 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f  94,. /*    90 */
315bc 20 20 20 38 37 30 2c 20 20 38 36 33 2c 20 20 38     870,  863,  8
315bd 36 34 2c 20 20 38 36 32 2c 20 20 38 36 36 2c 20  64,  862,  866, 
315be 20 38 37 31 2c 20 20 39 35 38 2c 20 20 37 39 33   871,  958,  793
315bf 2c 20 20 38 32 39 2c 20 20 38 34 37 2c 0a 20 2f  ,  829,  847,. /
315c0 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 38 32 38  *   100 */   828
315c1 2c 20 20 38 34 36 2c 20 20 38 35 33 2c 20 20 38  ,  846,  853,  8
315c2 34 35 2c 20 20 38 33 31 2c 20 20 38 34 30 2c 20  45,  831,  840, 
315c3 20 38 33 30 2c 20 20 36 36 35 2c 20 20 38 33 32   830,  665,  832
315c4 2c 20 20 38 33 33 2c 0a 20 2f 2a 20 20 20 31 31  ,  833,. /*   11
315c5 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
315c6 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
315c7 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
315c8 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315c9 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20  ,. /*   120 */  
315ca 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315cb 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
315cc 35 38 2c 20 20 36 36 30 2c 20 20 37 32 36 2c 20  58,  660,  726, 
315cd 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
315ce 20 20 31 33 30 20 2a 2f 20 20 20 39 35 38 2c 20    130 */   958, 
315cf 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315d0 2c 20 20 38 33 34 2c 20 20 38 33 35 2c 20 20 38  ,  834,  835,  8
315d1 35 30 2c 20 20 38 34 39 2c 20 20 38 34 38 2c 20  50,  849,  848, 
315d2 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31 34 30 20   958,. /*   140 
315d3 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
315d4 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315d5 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
315d6 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  58,  958,  958,.
315d7 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20 39   /*   150 */   9
315d8 35 38 2c 20 20 39 33 38 2c 20 20 39 33 36 2c 20  58,  938,  936, 
315d9 20 39 35 38 2c 20 20 38 38 32 2c 20 20 39 35 38   958,  882,  958
315da 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
315db 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
315dc 31 36 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  160 */   958,  9
315dd 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
315de 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315df 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
315e0 35 38 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f  58,. /*   170 */
315e1 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
315e2 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
315e3 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315e4 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
315e5 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 39 35 38  *   180 */   958
315e6 2c 20 20 36 34 30 2c 20 20 39 35 38 2c 20 20 37  ,  640,  958,  7
315e7 35 37 2c 20 20 37 35 37 2c 20 20 37 35 37 2c 20  57,  757,  757, 
315e8 20 36 33 34 2c 20 20 39 35 38 2c 20 20 39 35 38   634,  958,  958
315e9 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31 39  ,  958,. /*   19
315ea 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 30  0 */   958,  950
315eb 2c 20 20 37 36 31 2c 20 20 37 35 31 2c 20 20 37  ,  761,  751,  7
315ec 31 37 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  17,  958,  958, 
315ed 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315ee 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20  ,. /*   200 */  
315ef 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
315f0 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
315f1 35 38 2c 20 20 38 30 32 2c 20 20 37 34 30 2c 20  58,  802,  740, 
315f2 20 39 32 31 2c 20 20 39 32 33 2c 0a 20 2f 2a 20   921,  923,. /* 
315f3 20 20 32 31 30 20 2a 2f 20 20 20 39 35 38 2c 20    210 */   958, 
315f4 20 39 30 34 2c 20 20 37 33 38 2c 20 20 36 36 32   904,  738,  662
315f5 2c 20 20 37 35 39 2c 20 20 36 37 35 2c 20 20 37  ,  759,  675,  7
315f6 34 39 2c 20 20 36 34 32 2c 20 20 37 39 36 2c 20  49,  642,  796, 
315f7 20 37 37 34 2c 0a 20 2f 2a 20 20 20 32 32 30 20   774,. /*   220 
315f8 2a 2f 20 20 20 37 37 34 2c 20 20 39 31 36 2c 20  */   774,  916, 
315f9 20 37 39 36 2c 20 20 39 31 36 2c 20 20 36 39 39   796,  916,  699
315fa 2c 20 20 39 35 38 2c 20 20 37 38 36 2c 20 20 39  ,  958,  786,  9
315fb 35 38 2c 20 20 37 38 36 2c 20 20 36 39 36 2c 0a  58,  786,  696,.
315fc 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 37   /*   230 */   7
315fd 38 36 2c 20 20 37 37 34 2c 20 20 37 38 36 2c 20  86,  774,  786, 
315fe 20 37 38 36 2c 20 20 38 36 35 2c 20 20 39 35 38   786,  865,  958
315ff 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 37  ,  958,  958,  7
31600 35 38 2c 20 20 37 34 39 2c 0a 20 2f 2a 20 20 20  58,  749,. /*   
31601 32 34 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  240 */   958,  9
31602 34 33 2c 20 20 37 36 35 2c 20 20 37 36 35 2c 20  43,  765,  765, 
31603 20 39 33 35 2c 20 20 39 33 35 2c 20 20 37 36 35   935,  935,  765
31604 2c 20 20 38 30 38 2c 20 20 37 33 30 2c 20 20 37  ,  808,  730,  7
31605 39 36 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f  96,. /*   250 */
31606 20 20 20 37 33 37 2c 20 20 37 33 37 2c 20 20 37     737,  737,  7
31607 33 37 2c 20 20 37 33 37 2c 20 20 37 36 35 2c 20  37,  737,  765, 
31608 20 37 39 36 2c 20 20 38 30 38 2c 20 20 37 33 30   796,  808,  730
31609 2c 20 20 37 33 30 2c 20 20 37 36 35 2c 0a 20 2f  ,  730,  765,. /
3160a 2a 20 20 20 32 36 30 20 2a 2f 20 20 20 36 35 37  *   260 */   657
3160b 2c 20 20 39 31 30 2c 20 20 39 30 38 2c 20 20 37  ,  910,  908,  7
3160c 36 35 2c 20 20 37 36 35 2c 20 20 36 35 37 2c 20  65,  765,  657, 
3160d 20 37 36 35 2c 20 20 36 35 37 2c 20 20 37 36 35   765,  657,  765
3160e 2c 20 20 36 35 37 2c 0a 20 2f 2a 20 20 20 32 37  ,  657,. /*   27
3160f 30 20 2a 2f 20 20 20 38 37 35 2c 20 20 37 32 38  0 */   875,  728
31610 2c 20 20 37 32 38 2c 20 20 37 32 38 2c 20 20 37  ,  728,  728,  7
31611 31 33 2c 20 20 38 37 39 2c 20 20 38 37 39 2c 20  13,  879,  879, 
31612 20 38 37 35 2c 20 20 37 32 38 2c 20 20 36 39 39   875,  728,  699
31613 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20  ,. /*   280 */  
31614 20 37 32 38 2c 20 20 37 31 33 2c 20 20 37 32 38   728,  713,  728
31615 2c 20 20 37 32 38 2c 20 20 37 37 38 2c 20 20 37  ,  728,  778,  7
31616 37 33 2c 20 20 37 37 38 2c 20 20 37 37 33 2c 20  73,  778,  773, 
31617 20 37 37 38 2c 20 20 37 37 33 2c 0a 20 2f 2a 20   778,  773,. /* 
31618 20 20 32 39 30 20 2a 2f 20 20 20 37 36 35 2c 20    290 */   765, 
31619 20 37 36 35 2c 20 20 39 35 38 2c 20 20 37 39 31   765,  958,  791
3161a 2c 20 20 37 37 39 2c 20 20 37 38 39 2c 20 20 37  ,  779,  789,  7
3161b 38 37 2c 20 20 37 39 36 2c 20 20 39 35 38 2c 20  87,  796,  958, 
3161c 20 37 31 36 2c 0a 20 2f 2a 20 20 20 33 30 30 20   716,. /*   300 
3161d 2a 2f 20 20 20 36 35 30 2c 20 20 36 35 30 2c 20  */   650,  650, 
3161e 20 36 33 39 2c 20 20 36 33 39 2c 20 20 36 33 39   639,  639,  639
3161f 2c 20 20 36 33 39 2c 20 20 39 35 35 2c 20 20 39  ,  639,  955,  9
31620 35 35 2c 20 20 39 35 30 2c 20 20 37 30 31 2c 0a  55,  950,  701,.
31621 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 20 37   /*   310 */   7
31622 30 31 2c 20 20 36 38 33 2c 20 20 39 35 38 2c 20  01,  683,  958, 
31623 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
31624 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38  ,  958,  958,  8
31625 38 34 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  84,  958,. /*   
31626 33 32 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  320 */   958,  9
31627 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31628 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
31629 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
3162a 35 38 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f  58,. /*   330 */
3162b 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39     958,  958,  9
3162c 35 38 2c 20 20 36 33 35 2c 20 20 39 34 35 2c 20  58,  635,  945, 
3162d 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 34 32   958,  958,  942
3162e 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f  ,  958,  958,. /
3162f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20 39 35 38  *   340 */   958
31630 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31631 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31632 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
31633 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 35  ,  958,. /*   35
31634 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38  0 */   958,  958
31635 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31636 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31637 20 39 35 38 2c 20 20 39 31 34 2c 20 20 39 35 38   958,  914,  958
31638 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20  ,. /*   360 */  
31639 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
3163a 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
3163b 30 37 2c 20 20 39 30 36 2c 20 20 39 35 38 2c 20  07,  906,  958, 
3163c 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20   958,  958,. /* 
3163d 20 20 33 37 30 20 2a 2f 20 20 20 39 35 38 2c 20    370 */   958, 
3163e 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
3163f 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31640 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31641 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 38 30 20   958,. /*   380 
31642 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20  */   958,  958, 
31643 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
31644 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39  ,  958,  958,  9
31645 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a  58,  958,  958,.
31646 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 20 39   /*   390 */   9
31647 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
31648 20 37 38 38 2c 20 20 39 35 38 2c 20 20 37 38 30   788,  958,  780
31649 2c 20 20 39 35 38 2c 20 20 38 36 38 2c 20 20 39  ,  958,  868,  9
3164a 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20  58,  958,. /*   
3164b 34 30 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39  400 */   958,  9
3164c 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20  58,  958,  958, 
3164d 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38   958,  958,  958
3164e 2c 20 20 39 35 38 2c 20 20 37 34 33 2c 20 20 38  ,  958,  743,  8
3164f 31 37 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f  17,. /*   410 */
31650 20 20 20 39 35 38 2c 20 20 38 31 36 2c 20 20 38     958,  816,  8
31651 32 30 2c 20 20 38 31 35 2c 20 20 36 36 37 2c 20  20,  815,  667, 
31652 20 39 35 38 2c 20 20 36 34 38 2c 20 20 39 35 38   958,  648,  958
31653 2c 20 20 36 33 31 2c 20 20 36 33 36 2c 0a 20 2f  ,  631,  636,. /
31654 2a 20 20 20 34 32 30 20 2a 2f 20 20 20 39 35 34  *   420 */   954
31655 2c 20 20 39 35 37 2c 20 20 39 35 36 2c 20 20 39  ,  957,  956,  9
31656 35 33 2c 20 20 39 35 32 2c 20 20 39 35 31 2c 20  53,  952,  951, 
31657 20 39 34 36 2c 20 20 39 34 34 2c 20 20 39 34 31   946,  944,  941
31658 2c 20 20 39 34 30 2c 0a 20 2f 2a 20 20 20 34 33  ,  940,. /*   43
31659 30 20 2a 2f 20 20 20 39 33 39 2c 20 20 39 33 37  0 */   939,  937
3165a 2c 20 20 39 33 34 2c 20 20 39 33 30 2c 20 20 38  ,  934,  930,  8
3165b 38 38 2c 20 20 38 38 36 2c 20 20 38 39 33 2c 20  88,  886,  893, 
3165c 20 38 39 32 2c 20 20 38 39 31 2c 20 20 38 39 30   892,  891,  890
3165d 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f 20 20  ,. /*   440 */  
3165e 20 38 38 39 2c 20 20 38 38 37 2c 20 20 38 38 35   889,  887,  885
3165f 2c 20 20 38 38 33 2c 20 20 38 30 33 2c 20 20 38  ,  883,  803,  8
31660 30 31 2c 20 20 37 39 38 2c 20 20 37 39 35 2c 20  01,  798,  795, 
31661 20 39 32 39 2c 20 20 38 38 31 2c 0a 20 2f 2a 20   929,  881,. /* 
31662 20 20 34 35 30 20 2a 2f 20 20 20 37 33 39 2c 20    450 */   739, 
31663 20 37 33 36 2c 20 20 37 33 35 2c 20 20 36 35 36   736,  735,  656
31664 2c 20 20 39 34 37 2c 20 20 39 31 33 2c 20 20 39  ,  947,  913,  9
31665 32 32 2c 20 20 39 32 30 2c 20 20 38 30 39 2c 20  22,  920,  809, 
31666 20 39 31 39 2c 0a 20 2f 2a 20 20 20 34 36 30 20   919,. /*   460 
31667 2a 2f 20 20 20 39 31 38 2c 20 20 39 31 37 2c 20  */   918,  917, 
31668 20 39 31 35 2c 20 20 39 31 32 2c 20 20 38 39 39   915,  912,  899
31669 2c 20 20 38 30 35 2c 20 20 38 30 34 2c 20 20 37  ,  805,  804,  7
3166a 33 31 2c 20 20 38 37 33 2c 20 20 38 37 32 2c 0a  31,  873,  872,.
3166b 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20 20 36   /*   470 */   6
3166c 35 39 2c 20 20 39 30 33 2c 20 20 39 30 32 2c 20  59,  903,  902, 
3166d 20 39 30 31 2c 20 20 39 30 35 2c 20 20 39 30 39   901,  905,  909
3166e 2c 20 20 39 30 30 2c 20 20 37 36 37 2c 20 20 36  ,  900,  767,  6
3166f 35 38 2c 20 20 36 35 35 2c 0a 20 2f 2a 20 20 20  58,  655,. /*   
31670 34 38 30 20 2a 2f 20 20 20 36 36 34 2c 20 20 37  480 */   664,  7
31671 32 30 2c 20 20 37 31 39 2c 20 20 37 32 37 2c 20  20,  719,  727, 
31672 20 37 32 35 2c 20 20 37 32 34 2c 20 20 37 32 33   725,  724,  723
31673 2c 20 20 37 32 32 2c 20 20 37 32 31 2c 20 20 37  ,  722,  721,  7
31674 31 38 2c 0a 20 2f 2a 20 20 20 34 39 30 20 2a 2f  18,. /*   490 */
31675 20 20 20 36 36 36 2c 20 20 36 37 34 2c 20 20 36     666,  674,  6
31676 38 35 2c 20 20 37 31 32 2c 20 20 36 39 38 2c 20  85,  712,  698, 
31677 20 36 39 37 2c 20 20 38 37 38 2c 20 20 38 38 30   697,  878,  880
31678 2c 20 20 38 37 37 2c 20 20 38 37 36 2c 0a 20 2f  ,  877,  876,. /
31679 2a 20 20 20 35 30 30 20 2a 2f 20 20 20 37 30 35  *   500 */   705
3167a 2c 20 20 37 31 30 2c 20 20 37 30 39 2c 20 20 37  ,  710,  709,  7
3167b 30 38 2c 20 20 37 30 37 2c 20 20 37 30 36 2c 20  08,  707,  706, 
3167c 20 37 30 34 2c 20 20 37 30 33 2c 20 20 37 30 32   704,  703,  702
3167d 2c 20 20 36 39 35 2c 0a 20 2f 2a 20 20 20 35 31  ,  695,. /*   51
3167e 30 20 2a 2f 20 20 20 36 39 34 2c 20 20 37 30 30  0 */   694,  700
3167f 2c 20 20 36 39 33 2c 20 20 37 31 35 2c 20 20 37  ,  693,  715,  7
31680 31 34 2c 20 20 37 31 31 2c 20 20 36 39 32 2c 20  14,  711,  692, 
31681 20 37 33 34 2c 20 20 37 33 33 2c 20 20 37 33 32   734,  733,  732
31682 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f 20 20  ,. /*   520 */  
31683 20 37 32 39 2c 20 20 36 39 31 2c 20 20 36 39 30   729,  691,  690
31684 2c 20 20 36 38 39 2c 20 20 38 32 30 2c 20 20 36  ,  689,  820,  6
31685 38 38 2c 20 20 36 38 37 2c 20 20 38 32 36 2c 20  88,  687,  826, 
31686 20 38 32 35 2c 20 20 38 31 33 2c 0a 20 2f 2a 20   825,  813,. /* 
31687 20 20 35 33 30 20 2a 2f 20 20 20 38 35 37 2c 20    530 */   857, 
31688 20 37 35 34 2c 20 20 37 35 33 2c 20 20 37 35 32   754,  753,  752
31689 2c 20 20 37 36 34 2c 20 20 37 36 33 2c 20 20 37  ,  764,  763,  7
3168a 37 36 2c 20 20 37 37 35 2c 20 20 38 31 31 2c 20  76,  775,  811, 
3168b 20 38 31 30 2c 0a 20 2f 2a 20 20 20 35 34 30 20   810,. /*   540 
3168c 2a 2f 20 20 20 37 37 37 2c 20 20 37 36 32 2c 20  */   777,  762, 
3168d 20 37 35 36 2c 20 20 37 35 35 2c 20 20 37 37 31   756,  755,  771
3168e 2c 20 20 37 37 30 2c 20 20 37 36 39 2c 20 20 37  ,  770,  769,  7
3168f 36 38 2c 20 20 37 36 30 2c 20 20 37 35 30 2c 0a  68,  760,  750,.
31690 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20 20 37   /*   550 */   7
31691 38 32 2c 20 20 37 38 35 2c 20 20 37 38 34 2c 20  82,  785,  784, 
31692 20 37 38 31 2c 20 20 38 34 32 2c 20 20 38 35 39   781,  842,  859
31693 2c 20 20 37 36 36 2c 20 20 38 35 36 2c 20 20 39  ,  766,  856,  9
31694 32 38 2c 20 20 39 32 37 2c 0a 20 2f 2a 20 20 20  28,  927,. /*   
31695 35 36 30 20 2a 2f 20 20 20 39 32 36 2c 20 20 39  560 */   926,  9
31696 32 35 2c 20 20 39 32 34 2c 20 20 38 36 31 2c 20  25,  924,  861, 
31697 20 38 36 30 2c 20 20 38 32 37 2c 20 20 38 32 34   860,  827,  824
31698 2c 20 20 36 37 38 2c 20 20 36 37 39 2c 20 20 38  ,  678,  679,  8
31699 39 37 2c 0a 20 2f 2a 20 20 20 35 37 30 20 2a 2f  97,. /*   570 */
3169a 20 20 20 38 39 35 2c 20 20 38 39 36 2c 20 20 38     895,  896,  8
3169b 39 34 2c 20 20 36 38 31 2c 20 20 36 38 30 2c 20  94,  681,  680, 
3169c 20 36 37 37 2c 20 20 36 37 36 2c 20 20 38 35 38   677,  676,  858
3169d 2c 20 20 37 34 35 2c 20 20 37 34 34 2c 0a 20 2f  ,  745,  744,. /
3169e 2a 20 20 20 35 38 30 20 2a 2f 20 20 20 38 35 34  *   580 */   854
3169f 2c 20 20 38 35 31 2c 20 20 38 34 33 2c 20 20 38  ,  851,  843,  8
316a0 33 38 2c 20 20 38 35 35 2c 20 20 38 35 32 2c 20  38,  855,  852, 
316a1 20 38 34 34 2c 20 20 38 33 39 2c 20 20 38 33 37   844,  839,  837
316a2 2c 20 20 38 33 36 2c 0a 20 2f 2a 20 20 20 35 39  ,  836,. /*   59
316a3 30 20 2a 2f 20 20 20 38 32 32 2c 20 20 38 32 31  0 */   822,  821
316a4 2c 20 20 38 31 39 2c 20 20 38 31 38 2c 20 20 38  ,  819,  818,  8
316a5 31 34 2c 20 20 38 32 33 2c 20 20 36 36 39 2c 20  14,  823,  669, 
316a6 20 37 34 36 2c 20 20 37 34 32 2c 20 20 37 34 31   746,  742,  741
316a7 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f 20 20  ,. /*   600 */  
316a8 20 38 31 32 2c 20 20 37 34 38 2c 20 20 37 34 37   812,  748,  747
316a9 2c 20 20 36 38 36 2c 20 20 36 38 34 2c 20 20 36  ,  686,  684,  6
316aa 38 32 2c 20 20 36 36 33 2c 20 20 36 36 31 2c 20  82,  663,  661, 
316ab 20 36 35 34 2c 20 20 36 35 32 2c 0a 20 2f 2a 20   654,  652,. /* 
316ac 20 20 36 31 30 20 2a 2f 20 20 20 36 35 31 2c 20    610 */   651, 
316ad 20 36 35 33 2c 20 20 36 34 39 2c 20 20 36 34 37   653,  649,  647
316ae 2c 20 20 36 34 36 2c 20 20 36 34 35 2c 20 20 36  ,  646,  645,  6
316af 34 34 2c 20 20 36 34 33 2c 20 20 36 37 32 2c 20  44,  643,  672, 
316b0 20 36 37 31 2c 0a 20 2f 2a 20 20 20 36 32 30 20   671,. /*   620 
316b1 2a 2f 20 20 20 36 37 30 2c 20 20 36 36 38 2c 20  */   670,  668, 
316b2 20 36 36 37 2c 20 20 36 34 31 2c 20 20 36 33 38   667,  641,  638
316b3 2c 20 20 36 33 37 2c 20 20 36 33 33 2c 20 20 36  ,  637,  633,  6
316b4 33 32 2c 20 20 36 33 30 2c 0a 7d 3b 0a 0a 2f 2a  32,  630,.};../*
316b5 20 54 68 65 20 6e 65 78 74 20 74 61 62 6c 65 20   The next table 
316b6 6d 61 70 73 20 74 6f 6b 65 6e 73 20 69 6e 74 6f  maps tokens into
316b7 20 66 61 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e 73   fallback tokens
316b8 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72 75 63  .  If a construc
316b9 74 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 66 6f  t.** like the fo
316ba 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 20 0a 2a 2a 20  llowing:.** .** 
316bb 20 20 20 20 20 25 66 61 6c 6c 62 61 63 6b 20 49       %fallback I
316bc 44 20 58 20 59 20 5a 2e 0a 2a 2a 0a 2a 2a 20 61  D X Y Z..**.** a
316bd 70 70 65 61 72 73 20 69 6e 20 74 68 65 20 67 72  ppears in the gr
316be 61 6d 6d 61 72 2c 20 74 68 65 6e 20 49 44 20 62  ammar, then ID b
316bf 65 63 6f 6d 65 73 20 61 20 66 61 6c 6c 62 61 63  ecomes a fallbac
316c0 6b 20 74 6f 6b 65 6e 20 66 6f 72 20 58 2c 20 59  k token for X, Y
316c1 2c 0a 2a 2a 20 61 6e 64 20 5a 2e 20 20 57 68 65  ,.** and Z.  Whe
316c2 6e 65 76 65 72 20 6f 6e 65 20 6f 66 20 74 68 65  never one of the
316c3 20 74 6f 6b 65 6e 73 20 58 2c 20 59 2c 20 6f 72   tokens X, Y, or
316c4 20 5a 20 69 73 20 69 6e 70 75 74 20 74 6f 20 74   Z is input to t
316c5 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 62 75 74  he parser.** but
316c6 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 70 61 72   it does not par
316c7 73 65 2c 20 74 68 65 20 74 79 70 65 20 6f 66 20  se, the type of 
316c8 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 63 68 61  the token is cha
316c9 6e 67 65 64 20 74 6f 20 49 44 20 61 6e 64 0a 2a  nged to ID and.*
316ca 2a 20 74 68 65 20 70 61 72 73 65 20 69 73 20 72  * the parse is r
316cb 65 74 72 69 65 64 20 62 65 66 6f 72 65 20 61 6e  etried before an
316cc 20 65 72 72 6f 72 20 69 73 20 74 68 72 6f 77 6e   error is thrown
316cd 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 59 59 46 41  ..*/.#ifdef YYFA
316ce 4c 4c 42 41 43 4b 0a 73 74 61 74 69 63 20 63 6f  LLBACK.static co
316cf 6e 73 74 20 59 59 43 4f 44 45 54 59 50 45 20 79  nst YYCODETYPE y
316d0 79 46 61 6c 6c 62 61 63 6b 5b 5d 20 3d 20 7b 0a  yFallback[] = {.
316d1 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
316d2 20 20 20 20 24 20 3d 3e 20 6e 6f 74 68 69 6e 67      $ => nothing
316d3 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
316d4 20 20 20 20 20 53 45 4d 49 20 3d 3e 20 6e 6f 74       SEMI => not
316d5 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20  hing */.   26,  
316d6 2f 2a 20 20 20 20 45 58 50 4c 41 49 4e 20 3d 3e  /*    EXPLAIN =>
316d7 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
316d8 2a 20 20 20 20 20 20 51 55 45 52 59 20 3d 3e 20  *      QUERY => 
316d9 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
316da 20 20 20 20 20 20 20 50 4c 41 4e 20 3d 3e 20 49         PLAN => I
316db 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
316dc 20 20 20 20 20 42 45 47 49 4e 20 3d 3e 20 49 44       BEGIN => ID
316dd 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 54   */.    0,  /* T
316de 52 41 4e 53 41 43 54 49 4f 4e 20 3d 3e 20 6e 6f  RANSACTION => no
316df 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20  thing */.   26, 
316e0 20 2f 2a 20 20 20 44 45 46 45 52 52 45 44 20 3d   /*   DEFERRED =
316e1 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
316e2 2f 2a 20 20 49 4d 4d 45 44 49 41 54 45 20 3d 3e  /*  IMMEDIATE =>
316e3 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
316e4 2a 20 20 45 58 43 4c 55 53 49 56 45 20 3d 3e 20  *  EXCLUSIVE => 
316e5 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ID */.    0,  /*
316e6 20 20 20 20 20 43 4f 4d 4d 49 54 20 3d 3e 20 6e       COMMIT => n
316e7 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c  othing */.   26,
316e8 20 20 2f 2a 20 20 20 20 20 20 20 20 45 4e 44 20    /*        END 
316e9 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
316ea 20 2f 2a 20 20 20 52 4f 4c 4c 42 41 43 4b 20 3d   /*   ROLLBACK =
316eb 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
316ec 2f 2a 20 20 53 41 56 45 50 4f 49 4e 54 20 3d 3e  /*  SAVEPOINT =>
316ed 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
316ee 2a 20 20 20 20 52 45 4c 45 41 53 45 20 3d 3e 20  *    RELEASE => 
316ef 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ID */.    0,  /*
316f0 20 20 20 20 20 20 20 20 20 54 4f 20 3d 3e 20 6e           TO => n
316f1 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
316f2 20 20 2f 2a 20 20 20 20 20 20 54 41 42 4c 45 20    /*      TABLE 
316f3 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
316f4 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 43 52 45    0,  /*     CRE
316f5 41 54 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  ATE => nothing *
316f6 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
316f7 20 20 20 20 20 49 46 20 3d 3e 20 49 44 20 2a 2f       IF => ID */
316f8 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20  .    0,  /*     
316f9 20 20 20 4e 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e     NOT => nothin
316fa 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20  g */.    0,  /* 
316fb 20 20 20 20 45 58 49 53 54 53 20 3d 3e 20 6e 6f      EXISTS => no
316fc 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20  thing */.   26, 
316fd 20 2f 2a 20 20 20 20 20 20 20 54 45 4d 50 20 3d   /*       TEMP =
316fe 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20  > ID */.    0,  
316ff 2f 2a 20 20 20 20 20 20 20 20 20 4c 50 20 3d 3e  /*         LP =>
31700 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
31701 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 52  0,  /*         R
31702 50 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  P => nothing */.
31703 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20      0,  /*      
31704 20 20 20 41 53 20 3d 3e 20 6e 6f 74 68 69 6e 67     AS => nothing
31705 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
31706 20 20 20 20 43 4f 4d 4d 41 20 3d 3e 20 6e 6f 74      COMMA => not
31707 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
31708 2f 2a 20 20 20 20 20 20 20 20 20 49 44 20 3d 3e  /*         ID =>
31709 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
3170a 30 2c 20 20 2f 2a 20 20 20 20 49 4e 44 45 58 45  0,  /*    INDEXE
3170b 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  D => nothing */.
3170c 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
3170d 41 42 4f 52 54 20 3d 3e 20 49 44 20 2a 2f 0a 20  ABORT => ID */. 
3170e 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 41 43    26,  /*     AC
3170f 54 49 4f 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  TION => ID */.  
31710 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 41 46   26,  /*      AF
31711 54 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  TER => ID */.   
31712 32 36 2c 20 20 2f 2a 20 20 20 20 41 4e 41 4c 59  26,  /*    ANALY
31713 5a 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ZE => ID */.   2
31714 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 53  6,  /*        AS
31715 43 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  C => ID */.   26
31716 2c 20 20 2f 2a 20 20 20 20 20 41 54 54 41 43 48  ,  /*     ATTACH
31717 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
31718 20 20 2f 2a 20 20 20 20 20 42 45 46 4f 52 45 20    /*     BEFORE 
31719 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
3171a 20 2f 2a 20 20 20 20 20 20 20 20 20 42 59 20 3d   /*         BY =
3171b 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
3171c 2f 2a 20 20 20 20 43 41 53 43 41 44 45 20 3d 3e  /*    CASCADE =>
3171d 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
3171e 2a 20 20 20 20 20 20 20 43 41 53 54 20 3d 3e 20  *       CAST => 
3171f 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
31720 20 20 20 43 4f 4c 55 4d 4e 4b 57 20 3d 3e 20 49     COLUMNKW => I
31721 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
31722 20 20 43 4f 4e 46 4c 49 43 54 20 3d 3e 20 49 44    CONFLICT => ID
31723 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
31724 20 44 41 54 41 42 41 53 45 20 3d 3e 20 49 44 20   DATABASE => ID 
31725 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
31726 20 20 20 20 44 45 53 43 20 3d 3e 20 49 44 20 2a      DESC => ID *
31727 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
31728 20 44 45 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f   DETACH => ID */
31729 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
3172a 20 20 45 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a    EACH => ID */.
3172b 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
3172c 20 46 41 49 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20   FAIL => ID */. 
3172d 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
3172e 20 46 4f 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20   FOR => ID */.  
3172f 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 49 47 4e   26,  /*     IGN
31730 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ORE => ID */.   
31731 32 36 2c 20 20 2f 2a 20 20 49 4e 49 54 49 41 4c  26,  /*  INITIAL
31732 4c 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  LY => ID */.   2
31733 36 2c 20 20 2f 2a 20 20 20 20 49 4e 53 54 45 41  6,  /*    INSTEA
31734 44 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  D => ID */.   26
31735 2c 20 20 2f 2a 20 20 20 20 4c 49 4b 45 5f 4b 57  ,  /*    LIKE_KW
31736 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
31737 20 20 2f 2a 20 20 20 20 20 20 4d 41 54 43 48 20    /*      MATCH 
31738 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
31739 20 2f 2a 20 20 20 20 20 20 20 20 20 4e 4f 20 3d   /*         NO =
3173a 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
3173b 2f 2a 20 20 20 20 20 20 20 20 4b 45 59 20 3d 3e  /*        KEY =>
3173c 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
3173d 2a 20 20 20 20 20 20 20 20 20 4f 46 20 3d 3e 20  *         OF => 
3173e 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
3173f 20 20 20 20 20 4f 46 46 53 45 54 20 3d 3e 20 49       OFFSET => I
31740 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
31741 20 20 20 20 50 52 41 47 4d 41 20 3d 3e 20 49 44      PRAGMA => ID
31742 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
31743 20 20 20 20 52 41 49 53 45 20 3d 3e 20 49 44 20      RAISE => ID 
31744 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
31745 20 52 45 50 4c 41 43 45 20 3d 3e 20 49 44 20 2a   REPLACE => ID *
31746 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 52  /.   26,  /*   R
31747 45 53 54 52 49 43 54 20 3d 3e 20 49 44 20 2a 2f  ESTRICT => ID */
31748 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
31749 20 20 20 52 4f 57 20 3d 3e 20 49 44 20 2a 2f 0a     ROW => ID */.
3174a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 54 52     26,  /*    TR
3174b 49 47 47 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20  IGGER => ID */. 
3174c 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 56 41    26,  /*     VA
3174d 43 55 55 4d 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  CUUM => ID */.  
3174e 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 56   26,  /*       V
3174f 49 45 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  IEW => ID */.   
31750 32 36 2c 20 20 2f 2a 20 20 20 20 56 49 52 54 55  26,  /*    VIRTU
31751 41 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  AL => ID */.   2
31752 36 2c 20 20 2f 2a 20 20 20 20 52 45 49 4e 44 45  6,  /*    REINDE
31753 58 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  X => ID */.   26
31754 2c 20 20 2f 2a 20 20 20 20 20 52 45 4e 41 4d 45  ,  /*     RENAME
31755 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
31756 20 20 2f 2a 20 20 20 43 54 49 4d 45 5f 4b 57 20    /*   CTIME_KW 
31757 3d 3e 20 49 44 20 2a 2f 0a 7d 3b 0a 23 65 6e 64  => ID */.};.#end
31758 69 66 20 2f 2a 20 59 59 46 41 4c 4c 42 41 43 4b  if /* YYFALLBACK
31759 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c   */../* The foll
3175a 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
3175b 72 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e  represents a sin
3175c 67 6c 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  gle element of t
3175d 68 65 0a 2a 2a 20 70 61 72 73 65 72 27 73 20 73  he.** parser's s
3175e 74 61 63 6b 2e 20 20 49 6e 66 6f 72 6d 61 74 69  tack.  Informati
3175f 6f 6e 20 73 74 6f 72 65 64 20 69 6e 63 6c 75 64  on stored includ
31760 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54  es:.**.**   +  T
31761 68 65 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20  he state number 
31762 66 6f 72 20 74 68 65 20 70 61 72 73 65 72 20 61  for the parser a
31763 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66 20  t this level of 
31764 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a  the stack..**.**
31765 20 20 20 2b 20 20 54 68 65 20 76 61 6c 75 65 20     +  The value 
31766 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 73 74 6f  of the token sto
31767 72 65 64 20 61 74 20 74 68 69 73 20 6c 65 76 65  red at this leve
31768 6c 20 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a  l of the stack..
31769 2a 2a 20 20 20 20 20 20 28 49 6e 20 6f 74 68 65  **      (In othe
3176a 72 20 77 6f 72 64 73 2c 20 74 68 65 20 22 6d 61  r words, the "ma
3176b 6a 6f 72 22 20 74 6f 6b 65 6e 2e 29 0a 2a 2a 0a  jor" token.).**.
3176c 2a 2a 20 20 20 2b 20 20 54 68 65 20 73 65 6d 61  **   +  The sema
3176d 6e 74 69 63 20 76 61 6c 75 65 20 73 74 6f 72 65  ntic value store
3176e 64 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20  d at this level 
3176f 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54  of the stack.  T
31770 68 69 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 74  his is.**      t
31771 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75  he information u
31772 73 65 64 20 62 79 20 74 68 65 20 61 63 74 69 6f  sed by the actio
31773 6e 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68  n routines in th
31774 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2a 20 20 20  e grammar..**   
31775 20 20 20 49 74 20 69 73 20 73 6f 6d 65 74 69 6d     It is sometim
31776 65 73 20 63 61 6c 6c 65 64 20 74 68 65 20 22 6d  es called the "m
31777 69 6e 6f 72 22 20 74 6f 6b 65 6e 2e 0a 2a 2f 0a  inor" token..*/.
31778 73 74 72 75 63 74 20 79 79 53 74 61 63 6b 45 6e  struct yyStackEn
31779 74 72 79 20 7b 0a 20 20 59 59 41 43 54 49 4f 4e  try {.  YYACTION
3177a 54 59 50 45 20 73 74 61 74 65 6e 6f 3b 20 20 2f  TYPE stateno;  /
3177b 2a 20 54 68 65 20 73 74 61 74 65 2d 6e 75 6d 62  * The state-numb
3177c 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59  er */.  YYCODETY
3177d 50 45 20 6d 61 6a 6f 72 3b 20 20 20 20 20 20 2f  PE major;      /
3177e 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65  * The major toke
3177f 6e 20 76 61 6c 75 65 2e 20 20 54 68 69 73 20 69  n value.  This i
31780 73 20 74 68 65 20 63 6f 64 65 0a 20 20 20 20 20  s the code.     
31781 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31782 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f      ** number fo
31783 72 20 74 68 65 20 74 6f 6b 65 6e 20 61 74 20 74  r the token at t
31784 68 69 73 20 73 74 61 63 6b 20 6c 65 76 65 6c 20  his stack level 
31785 2a 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45  */.  YYMINORTYPE
31786 20 6d 69 6e 6f 72 3b 20 20 20 20 20 2f 2a 20 54   minor;     /* T
31787 68 65 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64  he user-supplied
31788 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 76 61 6c   minor token val
31789 75 65 2e 20 20 54 68 69 73 0a 20 20 20 20 20 20  ue.  This.      
3178a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3178b 20 20 20 2a 2a 20 69 73 20 74 68 65 20 76 61 6c     ** is the val
3178c 75 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  ue of the token 
3178d 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73   */.};.typedef s
3178e 74 72 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74  truct yyStackEnt
3178f 72 79 20 79 79 53 74 61 63 6b 45 6e 74 72 79 3b  ry yyStackEntry;
31790 0a 0a 2f 2a 20 54 68 65 20 73 74 61 74 65 20 6f  ../* The state o
31791 66 20 74 68 65 20 70 61 72 73 65 72 20 69 73 20  f the parser is 
31792 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 6e 74 61  completely conta
31793 69 6e 65 64 20 69 6e 20 61 6e 20 69 6e 73 74 61  ined in an insta
31794 6e 63 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f  nce of.** the fo
31795 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
31796 65 20 2a 2f 0a 73 74 72 75 63 74 20 79 79 50 61  e */.struct yyPa
31797 72 73 65 72 20 7b 0a 20 20 69 6e 74 20 79 79 69  rser {.  int yyi
31798 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
31799 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
3179a 6f 66 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69  of top element i
3179b 6e 20 73 74 61 63 6b 20 2a 2f 0a 23 69 66 64 65  n stack */.#ifde
3179c 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43  f YYTRACKMAXSTAC
3179d 4b 44 45 50 54 48 0a 20 20 69 6e 74 20 79 79 69  KDEPTH.  int yyi
3179e 64 78 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20  dxMax;          
3179f 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
317a0 6d 20 76 61 6c 75 65 20 6f 66 20 79 79 69 64 78  m value of yyidx
317a1 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74   */.#endif.  int
317a2 20 79 79 65 72 72 63 6e 74 3b 20 20 20 20 20 20   yyerrcnt;      
317a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68             /* Sh
317a4 69 66 74 73 20 6c 65 66 74 20 62 65 66 6f 72 65  ifts left before
317a5 20 6f 75 74 20 6f 66 20 74 68 65 20 65 72 72 6f   out of the erro
317a6 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61  r */.  sqlite3Pa
317a7 72 73 65 72 41 52 47 5f 53 44 45 43 4c 20 20 20  rserARG_SDECL   
317a8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
317a9 41 20 70 6c 61 63 65 20 74 6f 20 68 6f 6c 64 20  A place to hold 
317aa 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20  %extra_argument 
317ab 2a 2f 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45  */.#if YYSTACKDE
317ac 50 54 48 3c 3d 30 0a 20 20 69 6e 74 20 79 79 73  PTH<=0.  int yys
317ad 74 6b 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20  tksz;           
317ae 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
317af 74 20 73 69 64 65 20 6f 66 20 74 68 65 20 73 74  t side of the st
317b0 61 63 6b 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b  ack */.  yyStack
317b1 45 6e 74 72 79 20 2a 79 79 73 74 61 63 6b 3b 20  Entry *yystack; 
317b2 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
317b3 72 73 65 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a  rser's stack */.
317b4 23 65 6c 73 65 0a 20 20 79 79 53 74 61 63 6b 45  #else.  yyStackE
317b5 6e 74 72 79 20 79 79 73 74 61 63 6b 5b 59 59 53  ntry yystack[YYS
317b6 54 41 43 4b 44 45 50 54 48 5d 3b 20 20 2f 2a 20  TACKDEPTH];  /* 
317b7 54 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61  The parser's sta
317b8 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  ck */.#endif.};.
317b9 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 79  typedef struct y
317ba 79 50 61 72 73 65 72 20 79 79 50 61 72 73 65 72  yParser yyParser
317bb 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  ;..#ifndef NDEBU
317bc 47 0a 73 74 61 74 69 63 20 46 49 4c 45 20 2a 79  G.static FILE *y
317bd 79 54 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a  yTraceFILE = 0;.
317be 73 74 61 74 69 63 20 63 68 61 72 20 2a 79 79 54  static char *yyT
317bf 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a  racePrompt = 0;.
317c0 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
317c1 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45   */..#ifndef NDE
317c2 42 55 47 0a 2f 2a 20 0a 2a 2a 20 54 75 72 6e 20  BUG./* .** Turn 
317c3 70 61 72 73 65 72 20 74 72 61 63 69 6e 67 20 6f  parser tracing o
317c4 6e 20 62 79 20 67 69 76 69 6e 67 20 61 20 73 74  n by giving a st
317c5 72 65 61 6d 20 74 6f 20 77 68 69 63 68 20 74 6f  ream to which to
317c6 20 77 72 69 74 65 20 74 68 65 20 74 72 61 63 65   write the trace
317c7 0a 2a 2a 20 61 6e 64 20 61 20 70 72 6f 6d 70 74  .** and a prompt
317c8 20 74 6f 20 70 72 65 66 61 63 65 20 65 61 63 68   to preface each
317c9 20 74 72 61 63 65 20 6d 65 73 73 61 67 65 2e 20   trace message. 
317ca 20 54 72 61 63 69 6e 67 20 69 73 20 74 75 72 6e   Tracing is turn
317cb 65 64 20 6f 66 66 0a 2a 2a 20 62 79 20 6d 61 6b  ed off.** by mak
317cc 69 6e 67 20 65 69 74 68 65 72 20 61 72 67 75 6d  ing either argum
317cd 65 6e 74 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a 2a 20  ent NULL .**.** 
317ce 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a  Inputs:.** <ul>.
317cf 2a 2a 20 3c 6c 69 3e 20 41 20 46 49 4c 45 2a 20  ** <li> A FILE* 
317d0 74 6f 20 77 68 69 63 68 20 74 72 61 63 65 20 6f  to which trace o
317d1 75 74 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20  utput should be 
317d2 77 72 69 74 74 65 6e 2e 0a 2a 2a 20 20 20 20 20  written..**     
317d3 20 49 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74   If NULL, then t
317d4 72 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64  racing is turned
317d5 20 6f 66 66 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 20   off..** <li> A 
317d6 70 72 65 66 69 78 20 73 74 72 69 6e 67 20 77 72  prefix string wr
317d7 69 74 74 65 6e 20 61 74 20 74 68 65 20 62 65 67  itten at the beg
317d8 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a  inning of every.
317d9 2a 2a 20 20 20 20 20 20 6c 69 6e 65 20 6f 66 20  **      line of 
317da 74 72 61 63 65 20 6f 75 74 70 75 74 2e 20 20 49  trace output.  I
317db 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61  f NULL, then tra
317dc 63 69 6e 67 20 69 73 0a 2a 2a 20 20 20 20 20 20  cing is.**      
317dd 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c  turned off..** <
317de 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75  /ul>.**.** Outpu
317df 74 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a  ts:.** None..*/.
317e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
317e1 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
317e2 72 54 72 61 63 65 28 46 49 4c 45 20 2a 54 72 61  rTrace(FILE *Tra
317e3 63 65 46 49 4c 45 2c 20 63 68 61 72 20 2a 7a 54  ceFILE, char *zT
317e4 72 61 63 65 50 72 6f 6d 70 74 29 7b 0a 20 20 79  racePrompt){.  y
317e5 79 54 72 61 63 65 46 49 4c 45 20 3d 20 54 72 61  yTraceFILE = Tra
317e6 63 65 46 49 4c 45 3b 0a 20 20 79 79 54 72 61 63  ceFILE;.  yyTrac
317e7 65 50 72 6f 6d 70 74 20 3d 20 7a 54 72 61 63 65  ePrompt = zTrace
317e8 50 72 6f 6d 70 74 3b 0a 20 20 69 66 28 20 79 79  Prompt;.  if( yy
317e9 54 72 61 63 65 46 49 4c 45 3d 3d 30 20 29 20 79  TraceFILE==0 ) y
317ea 79 54 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 30  yTracePrompt = 0
317eb 3b 0a 20 20 65 6c 73 65 20 69 66 28 20 79 79 54  ;.  else if( yyT
317ec 72 61 63 65 50 72 6f 6d 70 74 3d 3d 30 20 29 20  racePrompt==0 ) 
317ed 79 79 54 72 61 63 65 46 49 4c 45 20 3d 20 30 3b  yyTraceFILE = 0;
317ee 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45  .}.#endif /* NDE
317ef 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  BUG */..#ifndef 
317f0 4e 44 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72  NDEBUG./* For tr
317f1 61 63 69 6e 67 20 73 68 69 66 74 73 2c 20 74 68  acing shifts, th
317f2 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 74  e names of all t
317f3 65 72 6d 69 6e 61 6c 73 20 61 6e 64 20 6e 6f 6e  erminals and non
317f4 74 65 72 6d 69 6e 61 6c 73 0a 2a 2a 20 61 72 65  terminals.** are
317f5 20 72 65 71 75 69 72 65 64 2e 20 20 54 68 65 20   required.  The 
317f6 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20  following table 
317f7 73 75 70 70 6c 69 65 73 20 74 68 65 73 65 20 6e  supplies these n
317f8 61 6d 65 73 20 2a 2f 0a 73 74 61 74 69 63 20 63  ames */.static c
317f9 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
317fa 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20 3d   yyTokenName[] =
317fb 20 7b 20 0a 20 20 22 24 22 2c 20 20 20 20 20 20   { .  "$",      
317fc 20 20 20 20 20 20 20 22 53 45 4d 49 22 2c 20 20         "SEMI",  
317fd 20 20 20 20 20 20 20 20 22 45 58 50 4c 41 49 4e          "EXPLAIN
317fe 22 2c 20 20 20 20 20 20 20 22 51 55 45 52 59 22  ",       "QUERY"
317ff 2c 20 20 20 20 20 20 20 0a 20 20 22 50 4c 41 4e  ,       .  "PLAN
31800 22 2c 20 20 20 20 20 20 20 20 20 20 22 42 45 47  ",          "BEG
31801 49 4e 22 2c 20 20 20 20 20 20 20 20 20 22 54 52  IN",         "TR
31802 41 4e 53 41 43 54 49 4f 4e 22 2c 20 20 20 22 44  ANSACTION",   "D
31803 45 46 45 52 52 45 44 22 2c 20 20 20 20 0a 20 20  EFERRED",    .  
31804 22 49 4d 4d 45 44 49 41 54 45 22 2c 20 20 20 20  "IMMEDIATE",    
31805 20 22 45 58 43 4c 55 53 49 56 45 22 2c 20 20 20   "EXCLUSIVE",   
31806 20 20 22 43 4f 4d 4d 49 54 22 2c 20 20 20 20 20    "COMMIT",     
31807 20 20 20 22 45 4e 44 22 2c 20 20 20 20 20 20 20     "END",       
31808 20 20 0a 20 20 22 52 4f 4c 4c 42 41 43 4b 22 2c    .  "ROLLBACK",
31809 20 20 20 20 20 20 22 53 41 56 45 50 4f 49 4e 54        "SAVEPOINT
3180a 22 2c 20 20 20 20 20 22 52 45 4c 45 41 53 45 22  ",     "RELEASE"
3180b 2c 20 20 20 20 20 20 20 22 54 4f 22 2c 20 20 20  ,       "TO",   
3180c 20 20 20 20 20 20 20 0a 20 20 22 54 41 42 4c 45         .  "TABLE
3180d 22 2c 20 20 20 20 20 20 20 20 20 22 43 52 45 41  ",         "CREA
3180e 54 45 22 2c 20 20 20 20 20 20 20 20 22 49 46 22  TE",        "IF"
3180f 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 4e 4f  ,            "NO
31810 54 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22  T",         .  "
31811 45 58 49 53 54 53 22 2c 20 20 20 20 20 20 20 20  EXISTS",        
31812 22 54 45 4d 50 22 2c 20 20 20 20 20 20 20 20 20  "TEMP",         
31813 20 22 4c 50 22 2c 20 20 20 20 20 20 20 20 20 20   "LP",          
31814 20 20 22 52 50 22 2c 20 20 20 20 20 20 20 20 20    "RP",         
31815 20 0a 20 20 22 41 53 22 2c 20 20 20 20 20 20 20   .  "AS",       
31816 20 20 20 20 20 22 43 4f 4d 4d 41 22 2c 20 20 20       "COMMA",   
31817 20 20 20 20 20 20 22 49 44 22 2c 20 20 20 20 20        "ID",     
31818 20 20 20 20 20 20 20 22 49 4e 44 45 58 45 44 22         "INDEXED"
31819 2c 20 20 20 20 20 0a 20 20 22 41 42 4f 52 54 22  ,     .  "ABORT"
3181a 2c 20 20 20 20 20 20 20 20 20 22 41 43 54 49 4f  ,         "ACTIO
3181b 4e 22 2c 20 20 20 20 20 20 20 20 22 41 46 54 45  N",        "AFTE
3181c 52 22 2c 20 20 20 20 20 20 20 20 20 22 41 4e 41  R",         "ANA
3181d 4c 59 5a 45 22 2c 20 20 20 20 20 0a 20 20 22 41  LYZE",     .  "A
3181e 53 43 22 2c 20 20 20 20 20 20 20 20 20 20 20 22  SC",           "
3181f 41 54 54 41 43 48 22 2c 20 20 20 20 20 20 20 20  ATTACH",        
31820 22 42 45 46 4f 52 45 22 2c 20 20 20 20 20 20 20  "BEFORE",       
31821 20 22 42 59 22 2c 20 20 20 20 20 20 20 20 20 20   "BY",          
31822 0a 20 20 22 43 41 53 43 41 44 45 22 2c 20 20 20  .  "CASCADE",   
31823 20 20 20 20 22 43 41 53 54 22 2c 20 20 20 20 20      "CAST",     
31824 20 20 20 20 20 22 43 4f 4c 55 4d 4e 4b 57 22 2c       "COLUMNKW",
31825 20 20 20 20 20 20 22 43 4f 4e 46 4c 49 43 54 22        "CONFLICT"
31826 2c 20 20 20 20 0a 20 20 22 44 41 54 41 42 41 53  ,    .  "DATABAS
31827 45 22 2c 20 20 20 20 20 20 22 44 45 53 43 22 2c  E",      "DESC",
31828 20 20 20 20 20 20 20 20 20 20 22 44 45 54 41 43            "DETAC
31829 48 22 2c 20 20 20 20 20 20 20 20 22 45 41 43 48  H",        "EACH
3182a 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 46 41  ",        .  "FA
3182b 49 4c 22 2c 20 20 20 20 20 20 20 20 20 20 22 46  IL",          "F
3182c 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20 20 22  OR",           "
3182d 49 47 4e 4f 52 45 22 2c 20 20 20 20 20 20 20 20  IGNORE",        
3182e 22 49 4e 49 54 49 41 4c 4c 59 22 2c 20 20 20 0a  "INITIALLY",   .
3182f 20 20 22 49 4e 53 54 45 41 44 22 2c 20 20 20 20    "INSTEAD",    
31830 20 20 20 22 4c 49 4b 45 5f 4b 57 22 2c 20 20 20     "LIKE_KW",   
31831 20 20 20 20 22 4d 41 54 43 48 22 2c 20 20 20 20      "MATCH",    
31832 20 20 20 20 20 22 4e 4f 22 2c 20 20 20 20 20 20       "NO",      
31833 20 20 20 20 0a 20 20 22 4b 45 59 22 2c 20 20 20      .  "KEY",   
31834 20 20 20 20 20 20 20 20 22 4f 46 22 2c 20 20 20          "OF",   
31835 20 20 20 20 20 20 20 20 20 22 4f 46 46 53 45 54           "OFFSET
31836 22 2c 20 20 20 20 20 20 20 20 22 50 52 41 47 4d  ",        "PRAGM
31837 41 22 2c 20 20 20 20 20 20 0a 20 20 22 52 41 49  A",      .  "RAI
31838 53 45 22 2c 20 20 20 20 20 20 20 20 20 22 52 45  SE",         "RE
31839 50 4c 41 43 45 22 2c 20 20 20 20 20 20 20 22 52  PLACE",       "R
3183a 45 53 54 52 49 43 54 22 2c 20 20 20 20 20 20 22  ESTRICT",      "
3183b 52 4f 57 22 2c 20 20 20 20 20 20 20 20 20 0a 20  ROW",         . 
3183c 20 22 54 52 49 47 47 45 52 22 2c 20 20 20 20 20   "TRIGGER",     
3183d 20 20 22 56 41 43 55 55 4d 22 2c 20 20 20 20 20    "VACUUM",     
3183e 20 20 20 22 56 49 45 57 22 2c 20 20 20 20 20 20     "VIEW",      
3183f 20 20 20 20 22 56 49 52 54 55 41 4c 22 2c 20 20      "VIRTUAL",  
31840 20 20 20 0a 20 20 22 52 45 49 4e 44 45 58 22 2c     .  "REINDEX",
31841 20 20 20 20 20 20 20 22 52 45 4e 41 4d 45 22 2c         "RENAME",
31842 20 20 20 20 20 20 20 20 22 43 54 49 4d 45 5f 4b          "CTIME_K
31843 57 22 2c 20 20 20 20 20 20 22 41 4e 59 22 2c 20  W",      "ANY", 
31844 20 20 20 20 20 20 20 20 0a 20 20 22 4f 52 22 2c          .  "OR",
31845 20 20 20 20 20 20 20 20 20 20 20 20 22 41 4e 44              "AND
31846 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 49 53  ",           "IS
31847 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 42  ",            "B
31848 45 54 57 45 45 4e 22 2c 20 20 20 20 20 0a 20 20  ETWEEN",     .  
31849 22 49 4e 22 2c 20 20 20 20 20 20 20 20 20 20 20  "IN",           
3184a 20 22 49 53 4e 55 4c 4c 22 2c 20 20 20 20 20 20   "ISNULL",      
3184b 20 20 22 4e 4f 54 4e 55 4c 4c 22 2c 20 20 20 20    "NOTNULL",    
3184c 20 20 20 22 4e 45 22 2c 20 20 20 20 20 20 20 20     "NE",        
3184d 20 20 0a 20 20 22 45 51 22 2c 20 20 20 20 20 20    .  "EQ",      
3184e 20 20 20 20 20 20 22 47 54 22 2c 20 20 20 20 20        "GT",     
3184f 20 20 20 20 20 20 20 22 4c 45 22 2c 20 20 20 20         "LE",    
31850 20 20 20 20 20 20 20 20 22 4c 54 22 2c 20 20 20          "LT",   
31851 20 20 20 20 20 20 20 0a 20 20 22 47 45 22 2c 20         .  "GE", 
31852 20 20 20 20 20 20 20 20 20 20 20 22 45 53 43 41             "ESCA
31853 50 45 22 2c 20 20 20 20 20 20 20 20 22 42 49 54  PE",        "BIT
31854 41 4e 44 22 2c 20 20 20 20 20 20 20 20 22 42 49  AND",        "BI
31855 54 4f 52 22 2c 20 20 20 20 20 20 20 0a 20 20 22  TOR",       .  "
31856 4c 53 48 49 46 54 22 2c 20 20 20 20 20 20 20 20  LSHIFT",        
31857 22 52 53 48 49 46 54 22 2c 20 20 20 20 20 20 20  "RSHIFT",       
31858 20 22 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20   "PLUS",        
31859 20 20 22 4d 49 4e 55 53 22 2c 20 20 20 20 20 20    "MINUS",      
3185a 20 0a 20 20 22 53 54 41 52 22 2c 20 20 20 20 20   .  "STAR",     
3185b 20 20 20 20 20 22 53 4c 41 53 48 22 2c 20 20 20       "SLASH",   
3185c 20 20 20 20 20 20 22 52 45 4d 22 2c 20 20 20 20        "REM",    
3185d 20 20 20 20 20 20 20 22 43 4f 4e 43 41 54 22 2c         "CONCAT",
3185e 20 20 20 20 20 20 0a 20 20 22 43 4f 4c 4c 41 54        .  "COLLAT
3185f 45 22 2c 20 20 20 20 20 20 20 22 42 49 54 4e 4f  E",       "BITNO
31860 54 22 2c 20 20 20 20 20 20 20 20 22 53 54 52 49  T",        "STRI
31861 4e 47 22 2c 20 20 20 20 20 20 20 20 22 4a 4f 49  NG",        "JOI
31862 4e 5f 4b 57 22 2c 20 20 20 20 20 0a 20 20 22 43  N_KW",     .  "C
31863 4f 4e 53 54 52 41 49 4e 54 22 2c 20 20 20 20 22  ONSTRAINT",    "
31864 44 45 46 41 55 4c 54 22 2c 20 20 20 20 20 20 20  DEFAULT",       
31865 22 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20 20 20  "NULL",         
31866 20 22 50 52 49 4d 41 52 59 22 2c 20 20 20 20 20   "PRIMARY",     
31867 0a 20 20 22 55 4e 49 51 55 45 22 2c 20 20 20 20  .  "UNIQUE",    
31868 20 20 20 20 22 43 48 45 43 4b 22 2c 20 20 20 20      "CHECK",    
31869 20 20 20 20 20 22 52 45 46 45 52 45 4e 43 45 53       "REFERENCES
3186a 22 2c 20 20 20 20 22 41 55 54 4f 49 4e 43 52 22  ",    "AUTOINCR"
3186b 2c 20 20 20 20 0a 20 20 22 4f 4e 22 2c 20 20 20  ,    .  "ON",   
3186c 20 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45           "DELETE
3186d 22 2c 20 20 20 20 20 20 20 20 22 55 50 44 41 54  ",        "UPDAT
3186e 45 22 2c 20 20 20 20 20 20 20 20 22 53 45 54 22  E",        "SET"
3186f 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22 44 45  ,         .  "DE
31870 46 45 52 52 41 42 4c 45 22 2c 20 20 20 20 22 46  FERRABLE",    "F
31871 4f 52 45 49 47 4e 22 2c 20 20 20 20 20 20 20 22  OREIGN",       "
31872 44 52 4f 50 22 2c 20 20 20 20 20 20 20 20 20 20  DROP",          
31873 22 55 4e 49 4f 4e 22 2c 20 20 20 20 20 20 20 0a  "UNION",       .
31874 20 20 22 41 4c 4c 22 2c 20 20 20 20 20 20 20 20    "ALL",        
31875 20 20 20 22 45 58 43 45 50 54 22 2c 20 20 20 20     "EXCEPT",    
31876 20 20 20 20 22 49 4e 54 45 52 53 45 43 54 22 2c      "INTERSECT",
31877 20 20 20 20 20 22 53 45 4c 45 43 54 22 2c 20 20       "SELECT",  
31878 20 20 20 20 0a 20 20 22 44 49 53 54 49 4e 43 54      .  "DISTINCT
31879 22 2c 20 20 20 20 20 20 22 44 4f 54 22 2c 20 20  ",      "DOT",  
3187a 20 20 20 20 20 20 20 20 20 22 46 52 4f 4d 22 2c           "FROM",
3187b 20 20 20 20 20 20 20 20 20 20 22 4a 4f 49 4e 22            "JOIN"
3187c 2c 20 20 20 20 20 20 20 20 0a 20 20 22 55 53 49  ,        .  "USI
3187d 4e 47 22 2c 20 20 20 20 20 20 20 20 20 22 4f 52  NG",         "OR
3187e 44 45 52 22 2c 20 20 20 20 20 20 20 20 20 22 47  DER",         "G
3187f 52 4f 55 50 22 2c 20 20 20 20 20 20 20 20 20 22  ROUP",         "
31880 48 41 56 49 4e 47 22 2c 20 20 20 20 20 20 0a 20  HAVING",      . 
31881 20 22 4c 49 4d 49 54 22 2c 20 20 20 20 20 20 20   "LIMIT",       
31882 20 20 22 57 48 45 52 45 22 2c 20 20 20 20 20 20    "WHERE",      
31883 20 20 20 22 49 4e 54 4f 22 2c 20 20 20 20 20 20     "INTO",      
31884 20 20 20 20 22 56 41 4c 55 45 53 22 2c 20 20 20      "VALUES",   
31885 20 20 20 0a 20 20 22 49 4e 53 45 52 54 22 2c 20     .  "INSERT", 
31886 20 20 20 20 20 20 20 22 49 4e 54 45 47 45 52 22         "INTEGER"
31887 2c 20 20 20 20 20 20 20 22 46 4c 4f 41 54 22 2c  ,       "FLOAT",
31888 20 20 20 20 20 20 20 20 20 22 42 4c 4f 42 22 2c           "BLOB",
31889 20 20 20 20 20 20 20 20 0a 20 20 22 52 45 47 49          .  "REGI
3188a 53 54 45 52 22 2c 20 20 20 20 20 20 22 56 41 52  STER",      "VAR
3188b 49 41 42 4c 45 22 2c 20 20 20 20 20 20 22 43 41  IABLE",      "CA
3188c 53 45 22 2c 20 20 20 20 20 20 20 20 20 20 22 57  SE",          "W
3188d 48 45 4e 22 2c 20 20 20 20 20 20 20 20 0a 20 20  HEN",        .  
3188e 22 54 48 45 4e 22 2c 20 20 20 20 20 20 20 20 20  "THEN",         
3188f 20 22 45 4c 53 45 22 2c 20 20 20 20 20 20 20 20   "ELSE",        
31890 20 20 22 49 4e 44 45 58 22 2c 20 20 20 20 20 20    "INDEX",      
31891 20 20 20 22 41 4c 54 45 52 22 2c 20 20 20 20 20     "ALTER",     
31892 20 20 0a 20 20 22 41 44 44 22 2c 20 20 20 20 20    .  "ADD",     
31893 20 20 20 20 20 20 22 65 72 72 6f 72 22 2c 20 20        "error",  
31894 20 20 20 20 20 20 20 22 69 6e 70 75 74 22 2c 20         "input", 
31895 20 20 20 20 20 20 20 20 22 63 6d 64 6c 69 73 74          "cmdlist
31896 22 2c 20 20 20 20 20 0a 20 20 22 65 63 6d 64 22  ",     .  "ecmd"
31897 2c 20 20 20 20 20 20 20 20 20 20 22 65 78 70 6c  ,          "expl
31898 61 69 6e 22 2c 20 20 20 20 20 20 20 22 63 6d 64  ain",       "cmd
31899 78 22 2c 20 20 20 20 20 20 20 20 20 20 22 63 6d  x",          "cm
3189a 64 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22  d",         .  "
3189b 74 72 61 6e 73 74 79 70 65 22 2c 20 20 20 20 20  transtype",     
3189c 22 74 72 61 6e 73 5f 6f 70 74 22 2c 20 20 20 20  "trans_opt",    
3189d 20 22 6e 6d 22 2c 20 20 20 20 20 20 20 20 20 20   "nm",          
3189e 20 20 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74    "savepoint_opt
3189f 22 2c 0a 20 20 22 63 72 65 61 74 65 5f 74 61 62  ",.  "create_tab
318a0 6c 65 22 2c 20 20 22 63 72 65 61 74 65 5f 74 61  le",  "create_ta
318a1 62 6c 65 5f 61 72 67 73 22 2c 20 20 22 63 72 65  ble_args",  "cre
318a2 61 74 65 6b 77 22 2c 20 20 20 20 20 20 22 74 65  atekw",      "te
318a3 6d 70 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22  mp",        .  "
318a4 69 66 6e 6f 74 65 78 69 73 74 73 22 2c 20 20 20  ifnotexists",   
318a5 22 64 62 6e 6d 22 2c 20 20 20 20 20 20 20 20 20  "dbnm",         
318a6 20 22 63 6f 6c 75 6d 6e 6c 69 73 74 22 2c 20 20   "columnlist",  
318a7 20 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 22    "conslist_opt"
318a8 2c 0a 20 20 22 73 65 6c 65 63 74 22 2c 20 20 20  ,.  "select",   
318a9 20 20 20 20 20 22 63 6f 6c 75 6d 6e 22 2c 20 20       "column",  
318aa 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 69 64 22        "columnid"
318ab 2c 20 20 20 20 20 20 22 74 79 70 65 22 2c 20 20  ,      "type",  
318ac 20 20 20 20 20 20 0a 20 20 22 63 61 72 67 6c 69        .  "cargli
318ad 73 74 22 2c 20 20 20 20 20 20 22 69 64 22 2c 20  st",      "id", 
318ae 20 20 20 20 20 20 20 20 20 20 20 22 69 64 73 22             "ids"
318af 2c 20 20 20 20 20 20 20 20 20 20 20 22 74 79 70  ,           "typ
318b0 65 74 6f 6b 65 6e 22 2c 20 20 20 0a 20 20 22 74  etoken",   .  "t
318b1 79 70 65 6e 61 6d 65 22 2c 20 20 20 20 20 20 22  ypename",      "
318b2 73 69 67 6e 65 64 22 2c 20 20 20 20 20 20 20 20  signed",        
318b3 22 70 6c 75 73 5f 6e 75 6d 22 2c 20 20 20 20 20  "plus_num",     
318b4 20 22 6d 69 6e 75 73 5f 6e 75 6d 22 2c 20 20 20   "minus_num",   
318b5 0a 20 20 22 63 61 72 67 22 2c 20 20 20 20 20 20  .  "carg",      
318b6 20 20 20 20 22 63 63 6f 6e 73 22 2c 20 20 20 20      "ccons",    
318b7 20 20 20 20 20 22 74 65 72 6d 22 2c 20 20 20 20       "term",    
318b8 20 20 20 20 20 20 22 65 78 70 72 22 2c 20 20 20        "expr",   
318b9 20 20 20 20 20 0a 20 20 22 6f 6e 63 6f 6e 66 22       .  "onconf"
318ba 2c 20 20 20 20 20 20 20 20 22 73 6f 72 74 6f 72  ,        "sortor
318bb 64 65 72 22 2c 20 20 20 20 20 22 61 75 74 6f 69  der",     "autoi
318bc 6e 63 22 2c 20 20 20 20 20 20 20 22 69 64 78 6c  nc",       "idxl
318bd 69 73 74 5f 6f 70 74 22 2c 20 0a 20 20 22 72 65  ist_opt", .  "re
318be 66 61 72 67 73 22 2c 20 20 20 20 20 20 20 22 64  fargs",       "d
318bf 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c  efer_subclause",
318c0 20 20 22 72 65 66 61 72 67 22 2c 20 20 20 20 20    "refarg",     
318c1 20 20 20 22 72 65 66 61 63 74 22 2c 20 20 20 20     "refact",    
318c2 20 20 0a 20 20 22 69 6e 69 74 5f 64 65 66 65 72    .  "init_defer
318c3 72 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c 20 20  red_pred_opt",  
318c4 22 63 6f 6e 73 6c 69 73 74 22 2c 20 20 20 20 20  "conslist",     
318c5 20 22 74 63 6f 6e 73 22 2c 20 20 20 20 20 20 20   "tcons",       
318c6 20 20 22 69 64 78 6c 69 73 74 22 2c 20 20 20 20    "idxlist",    
318c7 20 0a 20 20 22 64 65 66 65 72 5f 73 75 62 63 6c   .  "defer_subcl
318c8 61 75 73 65 5f 6f 70 74 22 2c 20 20 22 6f 72 63  ause_opt",  "orc
318c9 6f 6e 66 22 2c 20 20 20 20 20 20 20 20 22 72 65  onf",        "re
318ca 73 6f 6c 76 65 74 79 70 65 22 2c 20 20 20 22 72  solvetype",   "r
318cb 61 69 73 65 74 79 70 65 22 2c 20 20 20 0a 20 20  aisetype",   .  
318cc 22 69 66 65 78 69 73 74 73 22 2c 20 20 20 20 20  "ifexists",     
318cd 20 22 66 75 6c 6c 6e 61 6d 65 22 2c 20 20 20 20   "fullname",    
318ce 20 20 22 6f 6e 65 73 65 6c 65 63 74 22 2c 20 20    "oneselect",  
318cf 20 20 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f     "multiselect_
318d0 6f 70 22 2c 0a 20 20 22 64 69 73 74 69 6e 63 74  op",.  "distinct
318d1 22 2c 20 20 20 20 20 20 22 73 65 6c 63 6f 6c 6c  ",      "selcoll
318d2 69 73 74 22 2c 20 20 20 20 22 66 72 6f 6d 22 2c  ist",    "from",
318d3 20 20 20 20 20 20 20 20 20 20 22 77 68 65 72 65            "where
318d4 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22 67 72 6f  _opt",   .  "gro
318d5 75 70 62 79 5f 6f 70 74 22 2c 20 20 20 22 68 61  upby_opt",   "ha
318d6 76 69 6e 67 5f 6f 70 74 22 2c 20 20 20 20 22 6f  ving_opt",    "o
318d7 72 64 65 72 62 79 5f 6f 70 74 22 2c 20 20 20 22  rderby_opt",   "
318d8 6c 69 6d 69 74 5f 6f 70 74 22 2c 20 20 20 0a 20  limit_opt",   . 
318d9 20 22 73 63 6c 70 22 2c 20 20 20 20 20 20 20 20   "sclp",        
318da 20 20 22 61 73 22 2c 20 20 20 20 20 20 20 20 20    "as",         
318db 20 20 20 22 73 65 6c 74 61 62 6c 69 73 74 22 2c     "seltablist",
318dc 20 20 20 20 22 73 74 6c 5f 70 72 65 66 69 78 22      "stl_prefix"
318dd 2c 20 20 0a 20 20 22 6a 6f 69 6e 6f 70 22 2c 20  ,  .  "joinop", 
318de 20 20 20 20 20 20 20 22 69 6e 64 65 78 65 64 5f         "indexed_
318df 6f 70 74 22 2c 20 20 20 22 6f 6e 5f 6f 70 74 22  opt",   "on_opt"
318e0 2c 20 20 20 20 20 20 20 20 22 75 73 69 6e 67 5f  ,        "using_
318e1 6f 70 74 22 2c 20 20 20 0a 20 20 22 6a 6f 69 6e  opt",   .  "join
318e2 6f 70 32 22 2c 20 20 20 20 20 20 20 22 69 6e 73  op2",       "ins
318e3 63 6f 6c 6c 69 73 74 22 2c 20 20 20 20 22 73 6f  collist",    "so
318e4 72 74 6c 69 73 74 22 2c 20 20 20 20 20 20 22 73  rtlist",      "s
318e5 6f 72 74 69 74 65 6d 22 2c 20 20 20 20 0a 20 20  ortitem",    .  
318e6 22 6e 65 78 70 72 6c 69 73 74 22 2c 20 20 20 20  "nexprlist",    
318e7 20 22 73 65 74 6c 69 73 74 22 2c 20 20 20 20 20   "setlist",     
318e8 20 20 22 69 6e 73 65 72 74 5f 63 6d 64 22 2c 20    "insert_cmd", 
318e9 20 20 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f     "inscollist_o
318ea 70 74 22 2c 0a 20 20 22 69 74 65 6d 6c 69 73 74  pt",.  "itemlist
318eb 22 2c 20 20 20 20 20 20 22 65 78 70 72 6c 69 73  ",      "exprlis
318ec 74 22 2c 20 20 20 20 20 20 22 6c 69 6b 65 6f 70  t",      "likeop
318ed 22 2c 20 20 20 20 20 20 20 20 22 65 73 63 61 70  ",        "escap
318ee 65 22 2c 20 20 20 20 20 20 0a 20 20 22 62 65 74  e",      .  "bet
318ef 77 65 65 6e 5f 6f 70 22 2c 20 20 20 20 22 69 6e  ween_op",    "in
318f0 5f 6f 70 22 2c 20 20 20 20 20 20 20 20 20 22 63  _op",         "c
318f1 61 73 65 5f 6f 70 65 72 61 6e 64 22 2c 20 20 22  ase_operand",  "
318f2 63 61 73 65 5f 65 78 70 72 6c 69 73 74 22 2c 0a  case_exprlist",.
318f3 20 20 22 63 61 73 65 5f 65 6c 73 65 22 2c 20 20    "case_else",  
318f4 20 20 20 22 75 6e 69 71 75 65 66 6c 61 67 22 2c     "uniqueflag",
318f5 20 20 20 20 22 63 6f 6c 6c 61 74 65 22 2c 20 20      "collate",  
318f6 20 20 20 20 20 22 6e 6d 6e 75 6d 22 2c 20 20 20       "nmnum",   
318f7 20 20 20 20 0a 20 20 22 70 6c 75 73 5f 6f 70 74      .  "plus_opt
318f8 22 2c 20 20 20 20 20 20 22 6e 75 6d 62 65 72 22  ",      "number"
318f9 2c 20 20 20 20 20 20 20 20 22 74 72 69 67 67 65  ,        "trigge
318fa 72 5f 64 65 63 6c 22 2c 20 20 22 74 72 69 67 67  r_decl",  "trigg
318fb 65 72 5f 63 6d 64 5f 6c 69 73 74 22 2c 0a 20 20  er_cmd_list",.  
318fc 22 74 72 69 67 67 65 72 5f 74 69 6d 65 22 2c 20  "trigger_time", 
318fd 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e 74 22   "trigger_event"
318fe 2c 20 20 22 66 6f 72 65 61 63 68 5f 63 6c 61 75  ,  "foreach_clau
318ff 73 65 22 2c 20 20 22 77 68 65 6e 5f 63 6c 61 75  se",  "when_clau
31900 73 65 22 2c 20 0a 20 20 22 74 72 69 67 67 65 72  se", .  "trigger
31901 5f 63 6d 64 22 2c 20 20 20 22 74 72 6e 6d 22 2c  _cmd",   "trnm",
31902 20 20 20 20 20 20 20 20 20 20 22 74 72 69 64 78            "tridx
31903 62 79 22 2c 20 20 20 20 20 20 20 22 64 61 74 61  by",       "data
31904 62 61 73 65 5f 6b 77 5f 6f 70 74 22 2c 0a 20 20  base_kw_opt",.  
31905 22 6b 65 79 5f 6f 70 74 22 2c 20 20 20 20 20 20  "key_opt",      
31906 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c   "add_column_ful
31907 6c 6e 61 6d 65 22 2c 20 20 22 6b 77 63 6f 6c 75  lname",  "kwcolu
31908 6d 6e 5f 6f 70 74 22 2c 20 20 22 63 72 65 61 74  mn_opt",  "creat
31909 65 5f 76 74 61 62 22 2c 20 0a 20 20 22 76 74 61  e_vtab", .  "vta
3190a 62 61 72 67 6c 69 73 74 22 2c 20 20 20 22 76 74  barglist",   "vt
3190b 61 62 61 72 67 22 2c 20 20 20 20 20 20 20 22 76  abarg",       "v
3190c 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c 20 20 22  tabargtoken",  "
3190d 6c 70 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20  lp",          . 
3190e 20 22 61 6e 79 6c 69 73 74 22 2c 20 20 20 20 20   "anylist",     
3190f 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44  .};.#endif /* ND
31910 45 42 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  EBUG */..#ifndef
31911 20 4e 44 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74   NDEBUG./* For t
31912 72 61 63 69 6e 67 20 72 65 64 75 63 65 20 61 63  racing reduce ac
31913 74 69 6f 6e 73 2c 20 74 68 65 20 6e 61 6d 65 73  tions, the names
31914 20 6f 66 20 61 6c 6c 20 72 75 6c 65 73 20 61 72   of all rules ar
31915 65 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73  e required..*/.s
31916 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
31917 20 2a 63 6f 6e 73 74 20 79 79 52 75 6c 65 4e 61   *const yyRuleNa
31918 6d 65 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 30  me[] = {. /*   0
31919 20 2a 2f 20 22 69 6e 70 75 74 20 3a 3a 3d 20 63   */ "input ::= c
3191a 6d 64 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 20 31  mdlist",. /*   1
3191b 20 2a 2f 20 22 63 6d 64 6c 69 73 74 20 3a 3a 3d   */ "cmdlist ::=
3191c 20 63 6d 64 6c 69 73 74 20 65 63 6d 64 22 2c 0a   cmdlist ecmd",.
3191d 20 2f 2a 20 20 20 32 20 2a 2f 20 22 63 6d 64 6c   /*   2 */ "cmdl
3191e 69 73 74 20 3a 3a 3d 20 65 63 6d 64 22 2c 0a 20  ist ::= ecmd",. 
3191f 2f 2a 20 20 20 33 20 2a 2f 20 22 65 63 6d 64 20  /*   3 */ "ecmd 
31920 3a 3a 3d 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 20  ::= SEMI",. /*  
31921 20 34 20 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d 20   4 */ "ecmd ::= 
31922 65 78 70 6c 61 69 6e 20 63 6d 64 78 20 53 45 4d  explain cmdx SEM
31923 49 22 2c 0a 20 2f 2a 20 20 20 35 20 2a 2f 20 22  I",. /*   5 */ "
31924 65 78 70 6c 61 69 6e 20 3a 3a 3d 22 2c 0a 20 2f  explain ::=",. /
31925 2a 20 20 20 36 20 2a 2f 20 22 65 78 70 6c 61 69  *   6 */ "explai
31926 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 22 2c 0a  n ::= EXPLAIN",.
31927 20 2f 2a 20 20 20 37 20 2a 2f 20 22 65 78 70 6c   /*   7 */ "expl
31928 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20  ain ::= EXPLAIN 
31929 51 55 45 52 59 20 50 4c 41 4e 22 2c 0a 20 2f 2a  QUERY PLAN",. /*
3192a 20 20 20 38 20 2a 2f 20 22 63 6d 64 78 20 3a 3a     8 */ "cmdx ::
3192b 3d 20 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 39 20  = cmd",. /*   9 
3192c 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 42 45 47 49  */ "cmd ::= BEGI
3192d 4e 20 74 72 61 6e 73 74 79 70 65 20 74 72 61 6e  N transtype tran
3192e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 30 20  s_opt",. /*  10 
3192f 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a  */ "trans_opt ::
31930 3d 22 2c 0a 20 2f 2a 20 20 31 31 20 2a 2f 20 22  =",. /*  11 */ "
31931 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52  trans_opt ::= TR
31932 41 4e 53 41 43 54 49 4f 4e 22 2c 0a 20 2f 2a 20  ANSACTION",. /* 
31933 20 31 32 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70   12 */ "trans_op
31934 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f  t ::= TRANSACTIO
31935 4e 20 6e 6d 22 2c 0a 20 2f 2a 20 20 31 33 20 2a  N nm",. /*  13 *
31936 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d  / "transtype ::=
31937 22 2c 0a 20 2f 2a 20 20 31 34 20 2a 2f 20 22 74  ",. /*  14 */ "t
31938 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44 45 46  ranstype ::= DEF
31939 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20 31 35 20  ERRED",. /*  15 
3193a 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a  */ "transtype ::
3193b 3d 20 49 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f  = IMMEDIATE",. /
3193c 2a 20 20 31 36 20 2a 2f 20 22 74 72 61 6e 73 74  *  16 */ "transt
3193d 79 70 65 20 3a 3a 3d 20 45 58 43 4c 55 53 49 56  ype ::= EXCLUSIV
3193e 45 22 2c 0a 20 2f 2a 20 20 31 37 20 2a 2f 20 22  E",. /*  17 */ "
3193f 63 6d 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74  cmd ::= COMMIT t
31940 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20  rans_opt",. /*  
31941 31 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 45  18 */ "cmd ::= E
31942 4e 44 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20  ND trans_opt",. 
31943 2f 2a 20 20 31 39 20 2a 2f 20 22 63 6d 64 20 3a  /*  19 */ "cmd :
31944 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e  := ROLLBACK tran
31945 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 32 30 20  s_opt",. /*  20 
31946 2a 2f 20 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70  */ "savepoint_op
31947 74 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 22  t ::= SAVEPOINT"
31948 2c 0a 20 2f 2a 20 20 32 31 20 2a 2f 20 22 73 61  ,. /*  21 */ "sa
31949 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 22  vepoint_opt ::="
3194a 2c 0a 20 2f 2a 20 20 32 32 20 2a 2f 20 22 63 6d  ,. /*  22 */ "cm
3194b 64 20 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20  d ::= SAVEPOINT 
3194c 6e 6d 22 2c 0a 20 2f 2a 20 20 32 33 20 2a 2f 20  nm",. /*  23 */ 
3194d 22 63 6d 64 20 3a 3a 3d 20 52 45 4c 45 41 53 45  "cmd ::= RELEASE
3194e 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e   savepoint_opt n
3194f 6d 22 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f 20 22  m",. /*  24 */ "
31950 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b  cmd ::= ROLLBACK
31951 20 74 72 61 6e 73 5f 6f 70 74 20 54 4f 20 73 61   trans_opt TO sa
31952 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c  vepoint_opt nm",
31953 0a 20 2f 2a 20 20 32 35 20 2a 2f 20 22 63 6d 64  . /*  25 */ "cmd
31954 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62 6c   ::= create_tabl
31955 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61  e create_table_a
31956 72 67 73 22 2c 0a 20 2f 2a 20 20 32 36 20 2a 2f  rgs",. /*  26 */
31957 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 20 3a   "create_table :
31958 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d 70  := createkw temp
31959 20 54 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73   TABLE ifnotexis
3195a 74 73 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a  ts nm dbnm",. /*
3195b 20 20 32 37 20 2a 2f 20 22 63 72 65 61 74 65 6b    27 */ "createk
3195c 77 20 3a 3a 3d 20 43 52 45 41 54 45 22 2c 0a 20  w ::= CREATE",. 
3195d 2f 2a 20 20 32 38 20 2a 2f 20 22 69 66 6e 6f 74  /*  28 */ "ifnot
3195e 65 78 69 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a  exists ::=",. /*
3195f 20 20 32 39 20 2a 2f 20 22 69 66 6e 6f 74 65 78    29 */ "ifnotex
31960 69 73 74 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20  ists ::= IF NOT 
31961 45 58 49 53 54 53 22 2c 0a 20 2f 2a 20 20 33 30  EXISTS",. /*  30
31962 20 2a 2f 20 22 74 65 6d 70 20 3a 3a 3d 20 54 45   */ "temp ::= TE
31963 4d 50 22 2c 0a 20 2f 2a 20 20 33 31 20 2a 2f 20  MP",. /*  31 */ 
31964 22 74 65 6d 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  "temp ::=",. /* 
31965 20 33 32 20 2a 2f 20 22 63 72 65 61 74 65 5f 74   32 */ "create_t
31966 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50  able_args ::= LP
31967 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73   columnlist cons
31968 6c 69 73 74 5f 6f 70 74 20 52 50 22 2c 0a 20 2f  list_opt RP",. /
31969 2a 20 20 33 33 20 2a 2f 20 22 63 72 65 61 74 65  *  33 */ "create
3196a 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20  _table_args ::= 
3196b 41 53 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20  AS select",. /* 
3196c 20 33 34 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69   34 */ "columnli
3196d 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73  st ::= columnlis
3196e 74 20 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 22 2c  t COMMA column",
3196f 0a 20 2f 2a 20 20 33 35 20 2a 2f 20 22 63 6f 6c  . /*  35 */ "col
31970 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75  umnlist ::= colu
31971 6d 6e 22 2c 0a 20 2f 2a 20 20 33 36 20 2a 2f 20  mn",. /*  36 */ 
31972 22 63 6f 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75  "column ::= colu
31973 6d 6e 69 64 20 74 79 70 65 20 63 61 72 67 6c 69  mnid type cargli
31974 73 74 22 2c 0a 20 2f 2a 20 20 33 37 20 2a 2f 20  st",. /*  37 */ 
31975 22 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d  "columnid ::= nm
31976 22 2c 0a 20 2f 2a 20 20 33 38 20 2a 2f 20 22 69  ",. /*  38 */ "i
31977 64 20 3a 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 20  d ::= ID",. /*  
31978 33 39 20 2a 2f 20 22 69 64 20 3a 3a 3d 20 49 4e  39 */ "id ::= IN
31979 44 45 58 45 44 22 2c 0a 20 2f 2a 20 20 34 30 20  DEXED",. /*  40 
3197a 2a 2f 20 22 69 64 73 20 3a 3a 3d 20 49 44 7c 53  */ "ids ::= ID|S
3197b 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 34 31 20  TRING",. /*  41 
3197c 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 69 64 22 2c 0a  */ "nm ::= id",.
3197d 20 2f 2a 20 20 34 32 20 2a 2f 20 22 6e 6d 20 3a   /*  42 */ "nm :
3197e 3a 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20  := STRING",. /* 
3197f 20 34 33 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 4a   43 */ "nm ::= J
31980 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 20 34 34  OIN_KW",. /*  44
31981 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d 22 2c 0a   */ "type ::=",.
31982 20 2f 2a 20 20 34 35 20 2a 2f 20 22 74 79 70 65   /*  45 */ "type
31983 20 3a 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 22 2c   ::= typetoken",
31984 0a 20 2f 2a 20 20 34 36 20 2a 2f 20 22 74 79 70  . /*  46 */ "typ
31985 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e  etoken ::= typen
31986 61 6d 65 22 2c 0a 20 2f 2a 20 20 34 37 20 2a 2f  ame",. /*  47 */
31987 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20   "typetoken ::= 
31988 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e  typename LP sign
31989 65 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 38 20  ed RP",. /*  48 
3198a 2a 2f 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a  */ "typetoken ::
3198b 3d 20 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69  = typename LP si
3198c 67 6e 65 64 20 43 4f 4d 4d 41 20 73 69 67 6e 65  gned COMMA signe
3198d 64 20 52 50 22 2c 0a 20 2f 2a 20 20 34 39 20 2a  d RP",. /*  49 *
3198e 2f 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20  / "typename ::= 
3198f 69 64 73 22 2c 0a 20 2f 2a 20 20 35 30 20 2a 2f  ids",. /*  50 */
31990 20 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74   "typename ::= t
31991 79 70 65 6e 61 6d 65 20 69 64 73 22 2c 0a 20 2f  ypename ids",. /
31992 2a 20 20 35 31 20 2a 2f 20 22 73 69 67 6e 65 64  *  51 */ "signed
31993 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a   ::= plus_num",.
31994 20 2f 2a 20 20 35 32 20 2a 2f 20 22 73 69 67 6e   /*  52 */ "sign
31995 65 64 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d  ed ::= minus_num
31996 22 2c 0a 20 2f 2a 20 20 35 33 20 2a 2f 20 22 63  ",. /*  53 */ "c
31997 61 72 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67  arglist ::= carg
31998 6c 69 73 74 20 63 61 72 67 22 2c 0a 20 2f 2a 20  list carg",. /* 
31999 20 35 34 20 2a 2f 20 22 63 61 72 67 6c 69 73 74   54 */ "carglist
3199a 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 35 35 20 2a   ::=",. /*  55 *
3199b 2f 20 22 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53  / "carg ::= CONS
3199c 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 22  TRAINT nm ccons"
3199d 2c 0a 20 2f 2a 20 20 35 36 20 2a 2f 20 22 63 61  ,. /*  56 */ "ca
3199e 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 22 2c 0a 20  rg ::= ccons",. 
3199f 2f 2a 20 20 35 37 20 2a 2f 20 22 63 63 6f 6e 73  /*  57 */ "ccons
319a0 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 74 65 72   ::= DEFAULT ter
319a1 6d 22 2c 0a 20 2f 2a 20 20 35 38 20 2a 2f 20 22  m",. /*  58 */ "
319a2 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
319a3 54 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20  T LP expr RP",. 
319a4 2f 2a 20 20 35 39 20 2a 2f 20 22 63 63 6f 6e 73  /*  59 */ "ccons
319a5 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c 55   ::= DEFAULT PLU
319a6 53 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 36 30  S term",. /*  60
319a7 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44   */ "ccons ::= D
319a8 45 46 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72  EFAULT MINUS ter
319a9 6d 22 2c 0a 20 2f 2a 20 20 36 31 20 2a 2f 20 22  m",. /*  61 */ "
319aa 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
319ab 54 20 69 64 22 2c 0a 20 2f 2a 20 20 36 32 20 2a  T id",. /*  62 *
319ac 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c  / "ccons ::= NUL
319ad 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20  L onconf",. /*  
319ae 36 33 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  63 */ "ccons ::=
319af 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66   NOT NULL onconf
319b0 22 2c 0a 20 2f 2a 20 20 36 34 20 2a 2f 20 22 63  ",. /*  64 */ "c
319b1 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59  cons ::= PRIMARY
319b2 20 4b 45 59 20 73 6f 72 74 6f 72 64 65 72 20 6f   KEY sortorder o
319b3 6e 63 6f 6e 66 20 61 75 74 6f 69 6e 63 22 2c 0a  nconf autoinc",.
319b4 20 2f 2a 20 20 36 35 20 2a 2f 20 22 63 63 6f 6e   /*  65 */ "ccon
319b5 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63  s ::= UNIQUE onc
319b6 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 36 20 2a 2f  onf",. /*  66 */
319b7 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43   "ccons ::= CHEC
319b8 4b 20 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20  K LP expr RP",. 
319b9 2f 2a 20 20 36 37 20 2a 2f 20 22 63 63 6f 6e 73  /*  67 */ "ccons
319ba 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53 20   ::= REFERENCES 
319bb 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 72  nm idxlist_opt r
319bc 65 66 61 72 67 73 22 2c 0a 20 2f 2a 20 20 36 38  efargs",. /*  68
319bd 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 64   */ "ccons ::= d
319be 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c  efer_subclause",
319bf 0a 20 2f 2a 20 20 36 39 20 2a 2f 20 22 63 63 6f  . /*  69 */ "cco
319c0 6e 73 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69  ns ::= COLLATE i
319c1 64 73 22 2c 0a 20 2f 2a 20 20 37 30 20 2a 2f 20  ds",. /*  70 */ 
319c2 22 61 75 74 6f 69 6e 63 20 3a 3a 3d 22 2c 0a 20  "autoinc ::=",. 
319c3 2f 2a 20 20 37 31 20 2a 2f 20 22 61 75 74 6f 69  /*  71 */ "autoi
319c4 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 22  nc ::= AUTOINCR"
319c5 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f 20 22 72 65  ,. /*  72 */ "re
319c6 66 61 72 67 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  fargs ::=",. /* 
319c7 20 37 33 20 2a 2f 20 22 72 65 66 61 72 67 73 20   73 */ "refargs 
319c8 3a 3a 3d 20 72 65 66 61 72 67 73 20 72 65 66 61  ::= refargs refa
319c9 72 67 22 2c 0a 20 2f 2a 20 20 37 34 20 2a 2f 20  rg",. /*  74 */ 
319ca 22 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43  "refarg ::= MATC
319cb 48 20 6e 6d 22 2c 0a 20 2f 2a 20 20 37 35 20 2a  H nm",. /*  75 *
319cc 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e  / "refarg ::= ON
319cd 20 44 45 4c 45 54 45 20 72 65 66 61 63 74 22 2c   DELETE refact",
319ce 0a 20 2f 2a 20 20 37 36 20 2a 2f 20 22 72 65 66  . /*  76 */ "ref
319cf 61 72 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54  arg ::= ON UPDAT
319d0 45 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20  E refact",. /*  
319d1 37 37 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a  77 */ "refact ::
319d2 3d 20 53 45 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a  = SET NULL",. /*
319d3 20 20 37 38 20 2a 2f 20 22 72 65 66 61 63 74 20    78 */ "refact 
319d4 3a 3a 3d 20 53 45 54 20 44 45 46 41 55 4c 54 22  ::= SET DEFAULT"
319d5 2c 0a 20 2f 2a 20 20 37 39 20 2a 2f 20 22 72 65  ,. /*  79 */ "re
319d6 66 61 63 74 20 3a 3a 3d 20 43 41 53 43 41 44 45  fact ::= CASCADE
319d7 22 2c 0a 20 2f 2a 20 20 38 30 20 2a 2f 20 22 72  ",. /*  80 */ "r
319d8 65 66 61 63 74 20 3a 3a 3d 20 52 45 53 54 52 49  efact ::= RESTRI
319d9 43 54 22 2c 0a 20 2f 2a 20 20 38 31 20 2a 2f 20  CT",. /*  81 */ 
319da 22 72 65 66 61 63 74 20 3a 3a 3d 20 4e 4f 20 41  "refact ::= NO A
319db 43 54 49 4f 4e 22 2c 0a 20 2f 2a 20 20 38 32 20  CTION",. /*  82 
319dc 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61  */ "defer_subcla
319dd 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44 45 46 45  use ::= NOT DEFE
319de 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65 66 65  RRABLE init_defe
319df 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c 0a  rred_pred_opt",.
319e0 20 2f 2a 20 20 38 33 20 2a 2f 20 22 64 65 66 65   /*  83 */ "defe
319e1 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20  r_subclause ::= 
319e2 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f  DEFERRABLE init_
319e3 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
319e4 74 22 2c 0a 20 2f 2a 20 20 38 34 20 2a 2f 20 22  t",. /*  84 */ "
319e5 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72  init_deferred_pr
319e6 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  ed_opt ::=",. /*
319e7 20 20 38 35 20 2a 2f 20 22 69 6e 69 74 5f 64 65    85 */ "init_de
319e8 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20  ferred_pred_opt 
319e9 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 44 45  ::= INITIALLY DE
319ea 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20 20 38 36  FERRED",. /*  86
319eb 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65 72 72   */ "init_deferr
319ec 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20  ed_pred_opt ::= 
319ed 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49  INITIALLY IMMEDI
319ee 41 54 45 22 2c 0a 20 2f 2a 20 20 38 37 20 2a 2f  ATE",. /*  87 */
319ef 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a   "conslist_opt :
319f0 3a 3d 22 2c 0a 20 2f 2a 20 20 38 38 20 2a 2f 20  :=",. /*  88 */ 
319f1 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a  "conslist_opt ::
319f2 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69 73 74  = COMMA conslist
319f3 22 2c 0a 20 2f 2a 20 20 38 39 20 2a 2f 20 22 63  ",. /*  89 */ "c
319f4 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73  onslist ::= cons
319f5 6c 69 73 74 20 43 4f 4d 4d 41 20 74 63 6f 6e 73  list COMMA tcons
319f6 22 2c 0a 20 2f 2a 20 20 39 30 20 2a 2f 20 22 63  ",. /*  90 */ "c
319f7 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73  onslist ::= cons
319f8 6c 69 73 74 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a  list tcons",. /*
319f9 20 20 39 31 20 2a 2f 20 22 63 6f 6e 73 6c 69 73    91 */ "conslis
319fa 74 20 3a 3a 3d 20 74 63 6f 6e 73 22 2c 0a 20 2f  t ::= tcons",. /
319fb 2a 20 20 39 32 20 2a 2f 20 22 74 63 6f 6e 73 20  *  92 */ "tcons 
319fc 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54 20 6e  ::= CONSTRAINT n
319fd 6d 22 2c 0a 20 2f 2a 20 20 39 33 20 2a 2f 20 22  m",. /*  93 */ "
319fe 74 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52  tcons ::= PRIMAR
319ff 59 20 4b 45 59 20 4c 50 20 69 64 78 6c 69 73 74  Y KEY LP idxlist
31a00 20 61 75 74 6f 69 6e 63 20 52 50 20 6f 6e 63 6f   autoinc RP onco
31a01 6e 66 22 2c 0a 20 2f 2a 20 20 39 34 20 2a 2f 20  nf",. /*  94 */ 
31a02 22 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55  "tcons ::= UNIQU
31a03 45 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20  E LP idxlist RP 
31a04 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 35  onconf",. /*  95
31a05 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 43   */ "tcons ::= C
31a06 48 45 43 4b 20 4c 50 20 65 78 70 72 20 52 50 20  HECK LP expr RP 
31a07 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 36  onconf",. /*  96
31a08 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 46   */ "tcons ::= F
31a09 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50 20 69 64  OREIGN KEY LP id
31a0a 78 6c 69 73 74 20 52 50 20 52 45 46 45 52 45 4e  xlist RP REFEREN
31a0b 43 45 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f  CES nm idxlist_o
31a0c 70 74 20 72 65 66 61 72 67 73 20 64 65 66 65 72  pt refargs defer
31a0d 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 22 2c  _subclause_opt",
31a0e 0a 20 2f 2a 20 20 39 37 20 2a 2f 20 22 64 65 66  . /*  97 */ "def
31a0f 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74  er_subclause_opt
31a10 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39 38 20 2a   ::=",. /*  98 *
31a11 2f 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75  / "defer_subclau
31a12 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65 66 65 72  se_opt ::= defer
31a13 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a 20 2f 2a  _subclause",. /*
31a14 20 20 39 39 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20    99 */ "onconf 
31a15 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 30 30 20 2a 2f  ::=",. /* 100 */
31a16 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20   "onconf ::= ON 
31a17 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f 6c 76 65  CONFLICT resolve
31a18 74 79 70 65 22 2c 0a 20 2f 2a 20 31 30 31 20 2a  type",. /* 101 *
31a19 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d 22 2c 0a  / "orconf ::=",.
31a1a 20 2f 2a 20 31 30 32 20 2a 2f 20 22 6f 72 63 6f   /* 102 */ "orco
31a1b 6e 66 20 3a 3a 3d 20 4f 52 20 72 65 73 6f 6c 76  nf ::= OR resolv
31a1c 65 74 79 70 65 22 2c 0a 20 2f 2a 20 31 30 33 20  etype",. /* 103 
31a1d 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20  */ "resolvetype 
31a1e 3a 3a 3d 20 72 61 69 73 65 74 79 70 65 22 2c 0a  ::= raisetype",.
31a1f 20 2f 2a 20 31 30 34 20 2a 2f 20 22 72 65 73 6f   /* 104 */ "reso
31a20 6c 76 65 74 79 70 65 20 3a 3a 3d 20 49 47 4e 4f  lvetype ::= IGNO
31a21 52 45 22 2c 0a 20 2f 2a 20 31 30 35 20 2a 2f 20  RE",. /* 105 */ 
31a22 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d  "resolvetype ::=
31a23 20 52 45 50 4c 41 43 45 22 2c 0a 20 2f 2a 20 31   REPLACE",. /* 1
31a24 30 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44  06 */ "cmd ::= D
31a25 52 4f 50 20 54 41 42 4c 45 20 69 66 65 78 69 73  ROP TABLE ifexis
31a26 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f  ts fullname",. /
31a27 2a 20 31 30 37 20 2a 2f 20 22 69 66 65 78 69 73  * 107 */ "ifexis
31a28 74 73 20 3a 3a 3d 20 49 46 20 45 58 49 53 54 53  ts ::= IF EXISTS
31a29 22 2c 0a 20 2f 2a 20 31 30 38 20 2a 2f 20 22 69  ",. /* 108 */ "i
31a2a 66 65 78 69 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f  fexists ::=",. /
31a2b 2a 20 31 30 39 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 109 */ "cmd ::
31a2c 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20  = createkw temp 
31a2d 56 49 45 57 20 69 66 6e 6f 74 65 78 69 73 74 73  VIEW ifnotexists
31a2e 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65 6c 65   nm dbnm AS sele
31a2f 63 74 22 2c 0a 20 2f 2a 20 31 31 30 20 2a 2f 20  ct",. /* 110 */ 
31a30 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56 49  "cmd ::= DROP VI
31a31 45 57 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c  EW ifexists full
31a32 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31 31 31 20 2a  name",. /* 111 *
31a33 2f 20 22 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63  / "cmd ::= selec
31a34 74 22 2c 0a 20 2f 2a 20 31 31 32 20 2a 2f 20 22  t",. /* 112 */ "
31a35 73 65 6c 65 63 74 20 3a 3a 3d 20 6f 6e 65 73 65  select ::= onese
31a36 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 31 33 20 2a  lect",. /* 113 *
31a37 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20 73 65  / "select ::= se
31a38 6c 65 63 74 20 6d 75 6c 74 69 73 65 6c 65 63 74  lect multiselect
31a39 5f 6f 70 20 6f 6e 65 73 65 6c 65 63 74 22 2c 0a  _op oneselect",.
31a3a 20 2f 2a 20 31 31 34 20 2a 2f 20 22 6d 75 6c 74   /* 114 */ "mult
31a3b 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55  iselect_op ::= U
31a3c 4e 49 4f 4e 22 2c 0a 20 2f 2a 20 31 31 35 20 2a  NION",. /* 115 *
31a3d 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f  / "multiselect_o
31a3e 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c 22  p ::= UNION ALL"
31a3f 2c 0a 20 2f 2a 20 31 31 36 20 2a 2f 20 22 6d 75  ,. /* 116 */ "mu
31a40 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d  ltiselect_op ::=
31a41 20 45 58 43 45 50 54 7c 49 4e 54 45 52 53 45 43   EXCEPT|INTERSEC
31a42 54 22 2c 0a 20 2f 2a 20 31 31 37 20 2a 2f 20 22  T",. /* 117 */ "
31a43 6f 6e 65 73 65 6c 65 63 74 20 3a 3a 3d 20 53 45  oneselect ::= SE
31a44 4c 45 43 54 20 64 69 73 74 69 6e 63 74 20 73 65  LECT distinct se
31a45 6c 63 6f 6c 6c 69 73 74 20 66 72 6f 6d 20 77 68  lcollist from wh
31a46 65 72 65 5f 6f 70 74 20 67 72 6f 75 70 62 79 5f  ere_opt groupby_
31a47 6f 70 74 20 68 61 76 69 6e 67 5f 6f 70 74 20 6f  opt having_opt o
31a48 72 64 65 72 62 79 5f 6f 70 74 20 6c 69 6d 69 74  rderby_opt limit
31a49 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 31 38 20 2a  _opt",. /* 118 *
31a4a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20  / "distinct ::= 
31a4b 44 49 53 54 49 4e 43 54 22 2c 0a 20 2f 2a 20 31  DISTINCT",. /* 1
31a4c 31 39 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20  19 */ "distinct 
31a4d 3a 3a 3d 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31 32  ::= ALL",. /* 12
31a4e 30 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a  0 */ "distinct :
31a4f 3a 3d 22 2c 0a 20 2f 2a 20 31 32 31 20 2a 2f 20  :=",. /* 121 */ 
31a50 22 73 63 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c  "sclp ::= selcol
31a51 6c 69 73 74 20 43 4f 4d 4d 41 22 2c 0a 20 2f 2a  list COMMA",. /*
31a52 20 31 32 32 20 2a 2f 20 22 73 63 6c 70 20 3a 3a   122 */ "sclp ::
31a53 3d 22 2c 0a 20 2f 2a 20 31 32 33 20 2a 2f 20 22  =",. /* 123 */ "
31a54 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73  selcollist ::= s
31a55 63 6c 70 20 65 78 70 72 20 61 73 22 2c 0a 20 2f  clp expr as",. /
31a56 2a 20 31 32 34 20 2a 2f 20 22 73 65 6c 63 6f 6c  * 124 */ "selcol
31a57 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 53 54  list ::= sclp ST
31a58 41 52 22 2c 0a 20 2f 2a 20 31 32 35 20 2a 2f 20  AR",. /* 125 */ 
31a59 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20  "selcollist ::= 
31a5a 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54 41 52  sclp nm DOT STAR
31a5b 22 2c 0a 20 2f 2a 20 31 32 36 20 2a 2f 20 22 61  ",. /* 126 */ "a
31a5c 73 20 3a 3a 3d 20 41 53 20 6e 6d 22 2c 0a 20 2f  s ::= AS nm",. /
31a5d 2a 20 31 32 37 20 2a 2f 20 22 61 73 20 3a 3a 3d  * 127 */ "as ::=
31a5e 20 69 64 73 22 2c 0a 20 2f 2a 20 31 32 38 20 2a   ids",. /* 128 *
31a5f 2f 20 22 61 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  / "as ::=",. /* 
31a60 31 32 39 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d  129 */ "from ::=
31a61 22 2c 0a 20 2f 2a 20 31 33 30 20 2a 2f 20 22 66  ",. /* 130 */ "f
31a62 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c  rom ::= FROM sel
31a63 74 61 62 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 33  tablist",. /* 13
31a64 31 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66 69 78  1 */ "stl_prefix
31a65 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73 74 20   ::= seltablist 
31a66 6a 6f 69 6e 6f 70 22 2c 0a 20 2f 2a 20 31 33 32  joinop",. /* 132
31a67 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66 69 78 20   */ "stl_prefix 
31a68 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 33 20 2a 2f  ::=",. /* 133 */
31a69 20 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d   "seltablist ::=
31a6a 20 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64   stl_prefix nm d
31a6b 62 6e 6d 20 61 73 20 69 6e 64 65 78 65 64 5f 6f  bnm as indexed_o
31a6c 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f  pt on_opt using_
31a6d 6f 70 74 22 2c 0a 20 2f 2a 20 31 33 34 20 2a 2f  opt",. /* 134 */
31a6e 20 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d   "seltablist ::=
31a6f 20 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73   stl_prefix LP s
31a70 65 6c 65 63 74 20 52 50 20 61 73 20 6f 6e 5f 6f  elect RP as on_o
31a71 70 74 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20  pt using_opt",. 
31a72 2f 2a 20 31 33 35 20 2a 2f 20 22 73 65 6c 74 61  /* 135 */ "selta
31a73 62 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72  blist ::= stl_pr
31a74 65 66 69 78 20 4c 50 20 73 65 6c 74 61 62 6c 69  efix LP seltabli
31a75 73 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20  st RP as on_opt 
31a76 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20  using_opt",. /* 
31a77 31 33 36 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d  136 */ "dbnm ::=
31a78 22 2c 0a 20 2f 2a 20 31 33 37 20 2a 2f 20 22 64  ",. /* 137 */ "d
31a79 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 22 2c  bnm ::= DOT nm",
31a7a 0a 20 2f 2a 20 31 33 38 20 2a 2f 20 22 66 75 6c  . /* 138 */ "ful
31a7b 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62 6e  lname ::= nm dbn
31a7c 6d 22 2c 0a 20 2f 2a 20 31 33 39 20 2a 2f 20 22  m",. /* 139 */ "
31a7d 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41  joinop ::= COMMA
31a7e 7c 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 30 20  |JOIN",. /* 140 
31a7f 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a  */ "joinop ::= J
31a80 4f 49 4e 5f 4b 57 20 4a 4f 49 4e 22 2c 0a 20 2f  OIN_KW JOIN",. /
31a81 2a 20 31 34 31 20 2a 2f 20 22 6a 6f 69 6e 6f 70  * 141 */ "joinop
31a82 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20   ::= JOIN_KW nm 
31a83 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 32 20 2a  JOIN",. /* 142 *
31a84 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f  / "joinop ::= JO
31a85 49 4e 5f 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e  IN_KW nm nm JOIN
31a86 22 2c 0a 20 2f 2a 20 31 34 33 20 2a 2f 20 22 6f  ",. /* 143 */ "o
31a87 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70  n_opt ::= ON exp
31a88 72 22 2c 0a 20 2f 2a 20 31 34 34 20 2a 2f 20 22  r",. /* 144 */ "
31a89 6f 6e 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  on_opt ::=",. /*
31a8a 20 31 34 35 20 2a 2f 20 22 69 6e 64 65 78 65 64   145 */ "indexed
31a8b 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  _opt ::=",. /* 1
31a8c 34 36 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f  46 */ "indexed_o
31a8d 70 74 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42  pt ::= INDEXED B
31a8e 59 20 6e 6d 22 2c 0a 20 2f 2a 20 31 34 37 20 2a  Y nm",. /* 147 *
31a8f 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a  / "indexed_opt :
31a90 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45 44 22 2c  := NOT INDEXED",
31a91 0a 20 2f 2a 20 31 34 38 20 2a 2f 20 22 75 73 69  . /* 148 */ "usi
31a92 6e 67 5f 6f 70 74 20 3a 3a 3d 20 55 53 49 4e 47  ng_opt ::= USING
31a93 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52   LP inscollist R
31a94 50 22 2c 0a 20 2f 2a 20 31 34 39 20 2a 2f 20 22  P",. /* 149 */ "
31a95 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  using_opt ::=",.
31a96 20 2f 2a 20 31 35 30 20 2a 2f 20 22 6f 72 64 65   /* 150 */ "orde
31a97 72 62 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  rby_opt ::=",. /
31a98 2a 20 31 35 31 20 2a 2f 20 22 6f 72 64 65 72 62  * 151 */ "orderb
31a99 79 5f 6f 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20  y_opt ::= ORDER 
31a9a 42 59 20 73 6f 72 74 6c 69 73 74 22 2c 0a 20 2f  BY sortlist",. /
31a9b 2a 20 31 35 32 20 2a 2f 20 22 73 6f 72 74 6c 69  * 152 */ "sortli
31a9c 73 74 20 3a 3a 3d 20 73 6f 72 74 6c 69 73 74 20  st ::= sortlist 
31a9d 43 4f 4d 4d 41 20 73 6f 72 74 69 74 65 6d 20 73  COMMA sortitem s
31a9e 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 31  ortorder",. /* 1
31a9f 35 33 20 2a 2f 20 22 73 6f 72 74 6c 69 73 74 20  53 */ "sortlist 
31aa0 3a 3a 3d 20 73 6f 72 74 69 74 65 6d 20 73 6f 72  ::= sortitem sor
31aa1 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 31 35 34  torder",. /* 154
31aa2 20 2a 2f 20 22 73 6f 72 74 69 74 65 6d 20 3a 3a   */ "sortitem ::
31aa3 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 35 35  = expr",. /* 155
31aa4 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65 72 20 3a   */ "sortorder :
31aa5 3a 3d 20 41 53 43 22 2c 0a 20 2f 2a 20 31 35 36  := ASC",. /* 156
31aa6 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65 72 20 3a   */ "sortorder :
31aa7 3a 3d 20 44 45 53 43 22 2c 0a 20 2f 2a 20 31 35  := DESC",. /* 15
31aa8 37 20 2a 2f 20 22 73 6f 72 74 6f 72 64 65 72 20  7 */ "sortorder 
31aa9 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 38 20 2a 2f  ::=",. /* 158 */
31aaa 20 22 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a   "groupby_opt ::
31aab 3d 22 2c 0a 20 2f 2a 20 31 35 39 20 2a 2f 20 22  =",. /* 159 */ "
31aac 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20  groupby_opt ::= 
31aad 47 52 4f 55 50 20 42 59 20 6e 65 78 70 72 6c 69  GROUP BY nexprli
31aae 73 74 22 2c 0a 20 2f 2a 20 31 36 30 20 2a 2f 20  st",. /* 160 */ 
31aaf 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 22  "having_opt ::="
31ab0 2c 0a 20 2f 2a 20 31 36 31 20 2a 2f 20 22 68 61  ,. /* 161 */ "ha
31ab1 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48 41 56  ving_opt ::= HAV
31ab2 49 4e 47 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31  ING expr",. /* 1
31ab3 36 32 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74  62 */ "limit_opt
31ab4 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 33 20 2a   ::=",. /* 163 *
31ab5 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d  / "limit_opt ::=
31ab6 20 4c 49 4d 49 54 20 65 78 70 72 22 2c 0a 20 2f   LIMIT expr",. /
31ab7 2a 20 31 36 34 20 2a 2f 20 22 6c 69 6d 69 74 5f  * 164 */ "limit_
31ab8 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78  opt ::= LIMIT ex
31ab9 70 72 20 4f 46 46 53 45 54 20 65 78 70 72 22 2c  pr OFFSET expr",
31aba 0a 20 2f 2a 20 31 36 35 20 2a 2f 20 22 6c 69 6d  . /* 165 */ "lim
31abb 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54  it_opt ::= LIMIT
31abc 20 65 78 70 72 20 43 4f 4d 4d 41 20 65 78 70 72   expr COMMA expr
31abd 22 2c 0a 20 2f 2a 20 31 36 36 20 2a 2f 20 22 63  ",. /* 166 */ "c
31abe 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52  md ::= DELETE FR
31abf 4f 4d 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65  OM fullname inde
31ac0 78 65 64 5f 6f 70 74 20 77 68 65 72 65 5f 6f 70  xed_opt where_op
31ac1 74 22 2c 0a 20 2f 2a 20 31 36 37 20 2a 2f 20 22  t",. /* 167 */ "
31ac2 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  where_opt ::=",.
31ac3 20 2f 2a 20 31 36 38 20 2a 2f 20 22 77 68 65 72   /* 168 */ "wher
31ac4 65 5f 6f 70 74 20 3a 3a 3d 20 57 48 45 52 45 20  e_opt ::= WHERE 
31ac5 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 39 20 2a  expr",. /* 169 *
31ac6 2f 20 22 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54  / "cmd ::= UPDAT
31ac7 45 20 6f 72 63 6f 6e 66 20 66 75 6c 6c 6e 61 6d  E orconf fullnam
31ac8 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 53 45  e indexed_opt SE
31ac9 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f  T setlist where_
31aca 6f 70 74 22 2c 0a 20 2f 2a 20 31 37 30 20 2a 2f  opt",. /* 170 */
31acb 20 22 73 65 74 6c 69 73 74 20 3a 3a 3d 20 73 65   "setlist ::= se
31acc 74 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 45  tlist COMMA nm E
31acd 51 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 31  Q expr",. /* 171
31ace 20 2a 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a 3d   */ "setlist ::=
31acf 20 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a 20 2f   nm EQ expr",. /
31ad0 2a 20 31 37 32 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 172 */ "cmd ::
31ad1 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
31ad2 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f  O fullname insco
31ad3 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53  llist_opt VALUES
31ad4 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50 22   LP itemlist RP"
31ad5 2c 0a 20 2f 2a 20 31 37 33 20 2a 2f 20 22 63 6d  ,. /* 173 */ "cm
31ad6 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
31ad7 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69   INTO fullname i
31ad8 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65  nscollist_opt se
31ad9 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 37 34 20 2a  lect",. /* 174 *
31ada 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72  / "cmd ::= inser
31adb 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e  t_cmd INTO fulln
31adc 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f  ame inscollist_o
31add 70 74 20 44 45 46 41 55 4c 54 20 56 41 4c 55 45  pt DEFAULT VALUE
31ade 53 22 2c 0a 20 2f 2a 20 31 37 35 20 2a 2f 20 22  S",. /* 175 */ "
31adf 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 49  insert_cmd ::= I
31ae0 4e 53 45 52 54 20 6f 72 63 6f 6e 66 22 2c 0a 20  NSERT orconf",. 
31ae1 2f 2a 20 31 37 36 20 2a 2f 20 22 69 6e 73 65 72  /* 176 */ "inser
31ae2 74 5f 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41 43  t_cmd ::= REPLAC
31ae3 45 22 2c 0a 20 2f 2a 20 31 37 37 20 2a 2f 20 22  E",. /* 177 */ "
31ae4 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69 74 65  itemlist ::= ite
31ae5 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78 70 72  mlist COMMA expr
31ae6 22 2c 0a 20 2f 2a 20 31 37 38 20 2a 2f 20 22 69  ",. /* 178 */ "i
31ae7 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72  temlist ::= expr
31ae8 22 2c 0a 20 2f 2a 20 31 37 39 20 2a 2f 20 22 69  ",. /* 179 */ "i
31ae9 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a  nscollist_opt ::
31aea 3d 22 2c 0a 20 2f 2a 20 31 38 30 20 2a 2f 20 22  =",. /* 180 */ "
31aeb 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a  inscollist_opt :
31aec 3a 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74  := LP inscollist
31aed 20 52 50 22 2c 0a 20 2f 2a 20 31 38 31 20 2a 2f   RP",. /* 181 */
31aee 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d   "inscollist ::=
31aef 20 69 6e 73 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d   inscollist COMM
31af0 41 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 32 20 2a  A nm",. /* 182 *
31af1 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a  / "inscollist ::
31af2 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 33 20 2a  = nm",. /* 183 *
31af3 2f 20 22 65 78 70 72 20 3a 3a 3d 20 74 65 72 6d  / "expr ::= term
31af4 22 2c 0a 20 2f 2a 20 31 38 34 20 2a 2f 20 22 65  ",. /* 184 */ "e
31af5 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20  xpr ::= LP expr 
31af6 52 50 22 2c 0a 20 2f 2a 20 31 38 35 20 2a 2f 20  RP",. /* 185 */ 
31af7 22 74 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 22 2c  "term ::= NULL",
31af8 0a 20 2f 2a 20 31 38 36 20 2a 2f 20 22 65 78 70  . /* 186 */ "exp
31af9 72 20 3a 3a 3d 20 69 64 22 2c 0a 20 2f 2a 20 31  r ::= id",. /* 1
31afa 38 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  87 */ "expr ::= 
31afb 4a 4f 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 31 38  JOIN_KW",. /* 18
31afc 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 6e  8 */ "expr ::= n
31afd 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31  m DOT nm",. /* 1
31afe 38 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  89 */ "expr ::= 
31aff 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f 54 20 6e 6d  nm DOT nm DOT nm
31b00 22 2c 0a 20 2f 2a 20 31 39 30 20 2a 2f 20 22 74  ",. /* 190 */ "t
31b01 65 72 6d 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c  erm ::= INTEGER|
31b02 46 4c 4f 41 54 7c 42 4c 4f 42 22 2c 0a 20 2f 2a  FLOAT|BLOB",. /*
31b03 20 31 39 31 20 2a 2f 20 22 74 65 72 6d 20 3a 3a   191 */ "term ::
31b04 3d 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 31  = STRING",. /* 1
31b05 39 32 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  92 */ "expr ::= 
31b06 52 45 47 49 53 54 45 52 22 2c 0a 20 2f 2a 20 31  REGISTER",. /* 1
31b07 39 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  93 */ "expr ::= 
31b08 56 41 52 49 41 42 4c 45 22 2c 0a 20 2f 2a 20 31  VARIABLE",. /* 1
31b09 39 34 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  94 */ "expr ::= 
31b0a 65 78 70 72 20 43 4f 4c 4c 41 54 45 20 69 64 73  expr COLLATE ids
31b0b 22 2c 0a 20 2f 2a 20 31 39 35 20 2a 2f 20 22 65  ",. /* 195 */ "e
31b0c 78 70 72 20 3a 3a 3d 20 43 41 53 54 20 4c 50 20  xpr ::= CAST LP 
31b0d 65 78 70 72 20 41 53 20 74 79 70 65 74 6f 6b 65  expr AS typetoke
31b0e 6e 20 52 50 22 2c 0a 20 2f 2a 20 31 39 36 20 2a  n RP",. /* 196 *
31b0f 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c  / "expr ::= ID L
31b10 50 20 64 69 73 74 69 6e 63 74 20 65 78 70 72 6c  P distinct exprl
31b11 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 39 37  ist RP",. /* 197
31b12 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44   */ "expr ::= ID
31b13 20 4c 50 20 53 54 41 52 20 52 50 22 2c 0a 20 2f   LP STAR RP",. /
31b14 2a 20 31 39 38 20 2a 2f 20 22 74 65 72 6d 20 3a  * 198 */ "term :
31b15 3a 3d 20 43 54 49 4d 45 5f 4b 57 22 2c 0a 20 2f  := CTIME_KW",. /
31b16 2a 20 31 39 39 20 2a 2f 20 22 65 78 70 72 20 3a  * 199 */ "expr :
31b17 3a 3d 20 65 78 70 72 20 41 4e 44 20 65 78 70 72  := expr AND expr
31b18 22 2c 0a 20 2f 2a 20 32 30 30 20 2a 2f 20 22 65  ",. /* 200 */ "e
31b19 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20  xpr ::= expr OR 
31b1a 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 31 20 2a  expr",. /* 201 *
31b1b 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
31b1c 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70   LT|GT|GE|LE exp
31b1d 72 22 2c 0a 20 2f 2a 20 32 30 32 20 2a 2f 20 22  r",. /* 202 */ "
31b1e 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 45 51  expr ::= expr EQ
31b1f 7c 4e 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  |NE expr",. /* 2
31b20 30 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  03 */ "expr ::= 
31b21 65 78 70 72 20 42 49 54 41 4e 44 7c 42 49 54 4f  expr BITAND|BITO
31b22 52 7c 4c 53 48 49 46 54 7c 52 53 48 49 46 54 20  R|LSHIFT|RSHIFT 
31b23 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 34 20 2a  expr",. /* 204 *
31b24 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
31b25 20 50 4c 55 53 7c 4d 49 4e 55 53 20 65 78 70 72   PLUS|MINUS expr
31b26 22 2c 0a 20 2f 2a 20 32 30 35 20 2a 2f 20 22 65  ",. /* 205 */ "e
31b27 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 53 54 41  xpr ::= expr STA
31b28 52 7c 53 4c 41 53 48 7c 52 45 4d 20 65 78 70 72  R|SLASH|REM expr
31b29 22 2c 0a 20 2f 2a 20 32 30 36 20 2a 2f 20 22 65  ",. /* 206 */ "e
31b2a 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e  xpr ::= expr CON
31b2b 43 41 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  CAT expr",. /* 2
31b2c 30 37 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a  07 */ "likeop ::
31b2d 3d 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a 20  = LIKE_KW",. /* 
31b2e 32 30 38 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a  208 */ "likeop :
31b2f 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 22 2c  := NOT LIKE_KW",
31b30 0a 20 2f 2a 20 32 30 39 20 2a 2f 20 22 6c 69 6b  . /* 209 */ "lik
31b31 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48 22 2c 0a  eop ::= MATCH",.
31b32 20 2f 2a 20 32 31 30 20 2a 2f 20 22 6c 69 6b 65   /* 210 */ "like
31b33 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43 48  op ::= NOT MATCH
31b34 22 2c 0a 20 2f 2a 20 32 31 31 20 2a 2f 20 22 65  ",. /* 211 */ "e
31b35 73 63 61 70 65 20 3a 3a 3d 20 45 53 43 41 50 45  scape ::= ESCAPE
31b36 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 32 20   expr",. /* 212 
31b37 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a 3d 22 2c  */ "escape ::=",
31b38 0a 20 2f 2a 20 32 31 33 20 2a 2f 20 22 65 78 70  . /* 213 */ "exp
31b39 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f  r ::= expr likeo
31b3a 70 20 65 78 70 72 20 65 73 63 61 70 65 22 2c 0a  p expr escape",.
31b3b 20 2f 2a 20 32 31 34 20 2a 2f 20 22 65 78 70 72   /* 214 */ "expr
31b3c 20 3a 3a 3d 20 65 78 70 72 20 49 53 4e 55 4c 4c   ::= expr ISNULL
31b3d 7c 4e 4f 54 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32  |NOTNULL",. /* 2
31b3e 31 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  15 */ "expr ::= 
31b3f 65 78 70 72 20 4e 4f 54 20 4e 55 4c 4c 22 2c 0a  expr NOT NULL",.
31b40 20 2f 2a 20 32 31 36 20 2a 2f 20 22 65 78 70 72   /* 216 */ "expr
31b41 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 65 78 70   ::= expr IS exp
31b42 72 22 2c 0a 20 2f 2a 20 32 31 37 20 2a 2f 20 22  r",. /* 217 */ "
31b43 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53  expr ::= expr IS
31b44 20 4e 4f 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20   NOT expr",. /* 
31b45 32 31 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  218 */ "expr ::=
31b46 20 4e 4f 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20   NOT expr",. /* 
31b47 32 31 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  219 */ "expr ::=
31b48 20 42 49 54 4e 4f 54 20 65 78 70 72 22 2c 0a 20   BITNOT expr",. 
31b49 2f 2a 20 32 32 30 20 2a 2f 20 22 65 78 70 72 20  /* 220 */ "expr 
31b4a 3a 3a 3d 20 4d 49 4e 55 53 20 65 78 70 72 22 2c  ::= MINUS expr",
31b4b 0a 20 2f 2a 20 32 32 31 20 2a 2f 20 22 65 78 70  . /* 221 */ "exp
31b4c 72 20 3a 3a 3d 20 50 4c 55 53 20 65 78 70 72 22  r ::= PLUS expr"
31b4d 2c 0a 20 2f 2a 20 32 32 32 20 2a 2f 20 22 62 65  ,. /* 222 */ "be
31b4e 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45 54  tween_op ::= BET
31b4f 57 45 45 4e 22 2c 0a 20 2f 2a 20 32 32 33 20 2a  WEEN",. /* 223 *
31b50 2f 20 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a  / "between_op ::
31b51 3d 20 4e 4f 54 20 42 45 54 57 45 45 4e 22 2c 0a  = NOT BETWEEN",.
31b52 20 2f 2a 20 32 32 34 20 2a 2f 20 22 65 78 70 72   /* 224 */ "expr
31b53 20 3a 3a 3d 20 65 78 70 72 20 62 65 74 77 65 65   ::= expr betwee
31b54 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44 20 65 78  n_op expr AND ex
31b55 70 72 22 2c 0a 20 2f 2a 20 32 32 35 20 2a 2f 20  pr",. /* 225 */ 
31b56 22 69 6e 5f 6f 70 20 3a 3a 3d 20 49 4e 22 2c 0a  "in_op ::= IN",.
31b57 20 2f 2a 20 32 32 36 20 2a 2f 20 22 69 6e 5f 6f   /* 226 */ "in_o
31b58 70 20 3a 3a 3d 20 4e 4f 54 20 49 4e 22 2c 0a 20  p ::= NOT IN",. 
31b59 2f 2a 20 32 32 37 20 2a 2f 20 22 65 78 70 72 20  /* 227 */ "expr 
31b5a 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c  ::= expr in_op L
31b5b 50 20 65 78 70 72 6c 69 73 74 20 52 50 22 2c 0a  P exprlist RP",.
31b5c 20 2f 2a 20 32 32 38 20 2a 2f 20 22 65 78 70 72   /* 228 */ "expr
31b5d 20 3a 3a 3d 20 4c 50 20 73 65 6c 65 63 74 20 52   ::= LP select R
31b5e 50 22 2c 0a 20 2f 2a 20 32 32 39 20 2a 2f 20 22  P",. /* 229 */ "
31b5f 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e  expr ::= expr in
31b60 5f 6f 70 20 4c 50 20 73 65 6c 65 63 74 20 52 50  _op LP select RP
31b61 22 2c 0a 20 2f 2a 20 32 33 30 20 2a 2f 20 22 65  ",. /* 230 */ "e
31b62 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f  xpr ::= expr in_
31b63 6f 70 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a  op nm dbnm",. /*
31b64 20 32 33 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a   231 */ "expr ::
31b65 3d 20 45 58 49 53 54 53 20 4c 50 20 73 65 6c 65  = EXISTS LP sele
31b66 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 33 32 20  ct RP",. /* 232 
31b67 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 43 41 53  */ "expr ::= CAS
31b68 45 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 63  E case_operand c
31b69 61 73 65 5f 65 78 70 72 6c 69 73 74 20 63 61 73  ase_exprlist cas
31b6a 65 5f 65 6c 73 65 20 45 4e 44 22 2c 0a 20 2f 2a  e_else END",. /*
31b6b 20 32 33 33 20 2a 2f 20 22 63 61 73 65 5f 65 78   233 */ "case_ex
31b6c 70 72 6c 69 73 74 20 3a 3a 3d 20 63 61 73 65 5f  prlist ::= case_
31b6d 65 78 70 72 6c 69 73 74 20 57 48 45 4e 20 65 78  exprlist WHEN ex
31b6e 70 72 20 54 48 45 4e 20 65 78 70 72 22 2c 0a 20  pr THEN expr",. 
31b6f 2f 2a 20 32 33 34 20 2a 2f 20 22 63 61 73 65 5f  /* 234 */ "case_
31b70 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 57 48 45  exprlist ::= WHE
31b71 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78 70 72  N expr THEN expr
31b72 22 2c 0a 20 2f 2a 20 32 33 35 20 2a 2f 20 22 63  ",. /* 235 */ "c
31b73 61 73 65 5f 65 6c 73 65 20 3a 3a 3d 20 45 4c 53  ase_else ::= ELS
31b74 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 36  E expr",. /* 236
31b75 20 2a 2f 20 22 63 61 73 65 5f 65 6c 73 65 20 3a   */ "case_else :
31b76 3a 3d 22 2c 0a 20 2f 2a 20 32 33 37 20 2a 2f 20  :=",. /* 237 */ 
31b77 22 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a  "case_operand ::
31b78 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 38  = expr",. /* 238
31b79 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72 61 6e   */ "case_operan
31b7a 64 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 33 39 20  d ::=",. /* 239 
31b7b 2a 2f 20 22 65 78 70 72 6c 69 73 74 20 3a 3a 3d  */ "exprlist ::=
31b7c 20 6e 65 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a   nexprlist",. /*
31b7d 20 32 34 30 20 2a 2f 20 22 65 78 70 72 6c 69 73   240 */ "exprlis
31b7e 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 31 20  t ::=",. /* 241 
31b7f 2a 2f 20 22 6e 65 78 70 72 6c 69 73 74 20 3a 3a  */ "nexprlist ::
31b80 3d 20 6e 65 78 70 72 6c 69 73 74 20 43 4f 4d 4d  = nexprlist COMM
31b81 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 34 32  A expr",. /* 242
31b82 20 2a 2f 20 22 6e 65 78 70 72 6c 69 73 74 20 3a   */ "nexprlist :
31b83 3a 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 34  := expr",. /* 24
31b84 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72  3 */ "cmd ::= cr
31b85 65 61 74 65 6b 77 20 75 6e 69 71 75 65 66 6c 61  eatekw uniquefla
31b86 67 20 49 4e 44 45 58 20 69 66 6e 6f 74 65 78 69  g INDEX ifnotexi
31b87 73 74 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e 20 6e  sts nm dbnm ON n
31b88 6d 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 22  m LP idxlist RP"
31b89 2c 0a 20 2f 2a 20 32 34 34 20 2a 2f 20 22 75 6e  ,. /* 244 */ "un
31b8a 69 71 75 65 66 6c 61 67 20 3a 3a 3d 20 55 4e 49  iqueflag ::= UNI
31b8b 51 55 45 22 2c 0a 20 2f 2a 20 32 34 35 20 2a 2f  QUE",. /* 245 */
31b8c 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d   "uniqueflag ::=
31b8d 22 2c 0a 20 2f 2a 20 32 34 36 20 2a 2f 20 22 69  ",. /* 246 */ "i
31b8e 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 22 2c  dxlist_opt ::=",
31b8f 0a 20 2f 2a 20 32 34 37 20 2a 2f 20 22 69 64 78  . /* 247 */ "idx
31b90 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20  list_opt ::= LP 
31b91 69 64 78 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  idxlist RP",. /*
31b92 20 32 34 38 20 2a 2f 20 22 69 64 78 6c 69 73 74   248 */ "idxlist
31b93 20 3a 3a 3d 20 69 64 78 6c 69 73 74 20 43 4f 4d   ::= idxlist COM
31b94 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f  MA nm collate so
31b95 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 32 34  rtorder",. /* 24
31b96 39 20 2a 2f 20 22 69 64 78 6c 69 73 74 20 3a 3a  9 */ "idxlist ::
31b97 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72  = nm collate sor
31b98 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 32 35 30  torder",. /* 250
31b99 20 2a 2f 20 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d   */ "collate ::=
31b9a 22 2c 0a 20 2f 2a 20 32 35 31 20 2a 2f 20 22 63  ",. /* 251 */ "c
31b9b 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c 41  ollate ::= COLLA
31b9c 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20 32 35 32  TE ids",. /* 252
31b9d 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f   */ "cmd ::= DRO
31b9e 50 20 49 4e 44 45 58 20 69 66 65 78 69 73 74 73  P INDEX ifexists
31b9f 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20   fullname",. /* 
31ba0 32 35 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  253 */ "cmd ::= 
31ba1 56 41 43 55 55 4d 22 2c 0a 20 2f 2a 20 32 35 34  VACUUM",. /* 254
31ba2 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 56 41 43   */ "cmd ::= VAC
31ba3 55 55 4d 20 6e 6d 22 2c 0a 20 2f 2a 20 32 35 35  UUM nm",. /* 255
31ba4 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41   */ "cmd ::= PRA
31ba5 47 4d 41 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f  GMA nm dbnm",. /
31ba6 2a 20 32 35 36 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 256 */ "cmd ::
31ba7 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d  = PRAGMA nm dbnm
31ba8 20 45 51 20 6e 6d 6e 75 6d 22 2c 0a 20 2f 2a 20   EQ nmnum",. /* 
31ba9 32 35 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  257 */ "cmd ::= 
31baa 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c  PRAGMA nm dbnm L
31bab 50 20 6e 6d 6e 75 6d 20 52 50 22 2c 0a 20 2f 2a  P nmnum RP",. /*
31bac 20 32 35 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   258 */ "cmd ::=
31bad 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
31bae 45 51 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20  EQ minus_num",. 
31baf 2f 2a 20 32 35 39 20 2a 2f 20 22 63 6d 64 20 3a  /* 259 */ "cmd :
31bb0 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e  := PRAGMA nm dbn
31bb1 6d 20 4c 50 20 6d 69 6e 75 73 5f 6e 75 6d 20 52  m LP minus_num R
31bb2 50 22 2c 0a 20 2f 2a 20 32 36 30 20 2a 2f 20 22  P",. /* 260 */ "
31bb3 6e 6d 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e  nmnum ::= plus_n
31bb4 75 6d 22 2c 0a 20 2f 2a 20 32 36 31 20 2a 2f 20  um",. /* 261 */ 
31bb5 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a  "nmnum ::= nm",.
31bb6 20 2f 2a 20 32 36 32 20 2a 2f 20 22 6e 6d 6e 75   /* 262 */ "nmnu
31bb7 6d 20 3a 3a 3d 20 4f 4e 22 2c 0a 20 2f 2a 20 32  m ::= ON",. /* 2
31bb8 36 33 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d  63 */ "nmnum ::=
31bb9 20 44 45 4c 45 54 45 22 2c 0a 20 2f 2a 20 32 36   DELETE",. /* 26
31bba 34 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20  4 */ "nmnum ::= 
31bbb 44 45 46 41 55 4c 54 22 2c 0a 20 2f 2a 20 32 36  DEFAULT",. /* 26
31bbc 35 20 2a 2f 20 22 70 6c 75 73 5f 6e 75 6d 20 3a  5 */ "plus_num :
31bbd 3a 3d 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62  := plus_opt numb
31bbe 65 72 22 2c 0a 20 2f 2a 20 32 36 36 20 2a 2f 20  er",. /* 266 */ 
31bbf 22 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d  "minus_num ::= M
31bc0 49 4e 55 53 20 6e 75 6d 62 65 72 22 2c 0a 20 2f  INUS number",. /
31bc1 2a 20 32 36 37 20 2a 2f 20 22 6e 75 6d 62 65 72  * 267 */ "number
31bc2 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f   ::= INTEGER|FLO
31bc3 41 54 22 2c 0a 20 2f 2a 20 32 36 38 20 2a 2f 20  AT",. /* 268 */ 
31bc4 22 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c  "plus_opt ::= PL
31bc5 55 53 22 2c 0a 20 2f 2a 20 32 36 39 20 2a 2f 20  US",. /* 269 */ 
31bc6 22 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  "plus_opt ::=",.
31bc7 20 2f 2a 20 32 37 30 20 2a 2f 20 22 63 6d 64 20   /* 270 */ "cmd 
31bc8 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 72 69  ::= createkw tri
31bc9 67 67 65 72 5f 64 65 63 6c 20 42 45 47 49 4e 20  gger_decl BEGIN 
31bca 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
31bcb 20 45 4e 44 22 2c 0a 20 2f 2a 20 32 37 31 20 2a   END",. /* 271 *
31bcc 2f 20 22 74 72 69 67 67 65 72 5f 64 65 63 6c 20  / "trigger_decl 
31bcd 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47 45 52  ::= temp TRIGGER
31bce 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
31bcf 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74 69 6d  dbnm trigger_tim
31bd0 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20  e trigger_event 
31bd1 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65  ON fullname fore
31bd2 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f  ach_clause when_
31bd3 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 32 37 32  clause",. /* 272
31bd4 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74 69 6d   */ "trigger_tim
31bd5 65 20 3a 3a 3d 20 42 45 46 4f 52 45 22 2c 0a 20  e ::= BEFORE",. 
31bd6 2f 2a 20 32 37 33 20 2a 2f 20 22 74 72 69 67 67  /* 273 */ "trigg
31bd7 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 41 46 54 45  er_time ::= AFTE
31bd8 52 22 2c 0a 20 2f 2a 20 32 37 34 20 2a 2f 20 22  R",. /* 274 */ "
31bd9 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d  trigger_time ::=
31bda 20 49 4e 53 54 45 41 44 20 4f 46 22 2c 0a 20 2f   INSTEAD OF",. /
31bdb 2a 20 32 37 35 20 2a 2f 20 22 74 72 69 67 67 65  * 275 */ "trigge
31bdc 72 5f 74 69 6d 65 20 3a 3a 3d 22 2c 0a 20 2f 2a  r_time ::=",. /*
31bdd 20 32 37 36 20 2a 2f 20 22 74 72 69 67 67 65 72   276 */ "trigger
31bde 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c 45 54  _event ::= DELET
31bdf 45 7c 49 4e 53 45 52 54 22 2c 0a 20 2f 2a 20 32  E|INSERT",. /* 2
31be0 37 37 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 65  77 */ "trigger_e
31be1 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 22  vent ::= UPDATE"
31be2 2c 0a 20 2f 2a 20 32 37 38 20 2a 2f 20 22 74 72  ,. /* 278 */ "tr
31be3 69 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20  igger_event ::= 
31be4 55 50 44 41 54 45 20 4f 46 20 69 6e 73 63 6f 6c  UPDATE OF inscol
31be5 6c 69 73 74 22 2c 0a 20 2f 2a 20 32 37 39 20 2a  list",. /* 279 *
31be6 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73  / "foreach_claus
31be7 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 30 20  e ::=",. /* 280 
31be8 2a 2f 20 22 66 6f 72 65 61 63 68 5f 63 6c 61 75  */ "foreach_clau
31be9 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43 48 20  se ::= FOR EACH 
31bea 52 4f 57 22 2c 0a 20 2f 2a 20 32 38 31 20 2a 2f  ROW",. /* 281 */
31beb 20 22 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a   "when_clause ::
31bec 3d 22 2c 0a 20 2f 2a 20 32 38 32 20 2a 2f 20 22  =",. /* 282 */ "
31bed 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20  when_clause ::= 
31bee 57 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20  WHEN expr",. /* 
31bef 32 38 33 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  283 */ "trigger_
31bf0 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69  cmd_list ::= tri
31bf1 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72  gger_cmd_list tr
31bf2 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 22 2c  igger_cmd SEMI",
31bf3 0a 20 2f 2a 20 32 38 34 20 2a 2f 20 22 74 72 69  . /* 284 */ "tri
31bf4 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a  gger_cmd_list ::
31bf5 3d 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45  = trigger_cmd SE
31bf6 4d 49 22 2c 0a 20 2f 2a 20 32 38 35 20 2a 2f 20  MI",. /* 285 */ 
31bf7 22 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a 20  "trnm ::= nm",. 
31bf8 2f 2a 20 32 38 36 20 2a 2f 20 22 74 72 6e 6d 20  /* 286 */ "trnm 
31bf9 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a  ::= nm DOT nm",.
31bfa 20 2f 2a 20 32 38 37 20 2a 2f 20 22 74 72 69 64   /* 287 */ "trid
31bfb 78 62 79 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38  xby ::=",. /* 28
31bfc 38 20 2a 2f 20 22 74 72 69 64 78 62 79 20 3a 3a  8 */ "tridxby ::
31bfd 3d 20 49 4e 44 45 58 45 44 20 42 59 20 6e 6d 22  = INDEXED BY nm"
31bfe 2c 0a 20 2f 2a 20 32 38 39 20 2a 2f 20 22 74 72  ,. /* 289 */ "tr
31bff 69 64 78 62 79 20 3a 3a 3d 20 4e 4f 54 20 49 4e  idxby ::= NOT IN
31c00 44 45 58 45 44 22 2c 0a 20 2f 2a 20 32 39 30 20  DEXED",. /* 290 
31c01 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20  */ "trigger_cmd 
31c02 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e  ::= UPDATE orcon
31c03 66 20 74 72 6e 6d 20 74 72 69 64 78 62 79 20 53  f trnm tridxby S
31c04 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65  ET setlist where
31c05 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 39 31 20 2a  _opt",. /* 291 *
31c06 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a  / "trigger_cmd :
31c07 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
31c08 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69  TO trnm inscolli
31c09 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50  st_opt VALUES LP
31c0a 20 69 74 65 6d 6c 69 73 74 20 52 50 22 2c 0a 20   itemlist RP",. 
31c0b 2f 2a 20 32 39 32 20 2a 2f 20 22 74 72 69 67 67  /* 292 */ "trigg
31c0c 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72  er_cmd ::= inser
31c0d 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72 6e 6d 20  t_cmd INTO trnm 
31c0e 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73  inscollist_opt s
31c0f 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 32 39 33 20  elect",. /* 293 
31c10 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20  */ "trigger_cmd 
31c11 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  ::= DELETE FROM 
31c12 74 72 6e 6d 20 74 72 69 64 78 62 79 20 77 68 65  trnm tridxby whe
31c13 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 39 34  re_opt",. /* 294
31c14 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64   */ "trigger_cmd
31c15 20 3a 3a 3d 20 73 65 6c 65 63 74 22 2c 0a 20 2f   ::= select",. /
31c16 2a 20 32 39 35 20 2a 2f 20 22 65 78 70 72 20 3a  * 295 */ "expr :
31c17 3a 3d 20 52 41 49 53 45 20 4c 50 20 49 47 4e 4f  := RAISE LP IGNO
31c18 52 45 20 52 50 22 2c 0a 20 2f 2a 20 32 39 36 20  RE RP",. /* 296 
31c19 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52 41 49  */ "expr ::= RAI
31c1a 53 45 20 4c 50 20 72 61 69 73 65 74 79 70 65 20  SE LP raisetype 
31c1b 43 4f 4d 4d 41 20 6e 6d 20 52 50 22 2c 0a 20 2f  COMMA nm RP",. /
31c1c 2a 20 32 39 37 20 2a 2f 20 22 72 61 69 73 65 74  * 297 */ "raiset
31c1d 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b  ype ::= ROLLBACK
31c1e 22 2c 0a 20 2f 2a 20 32 39 38 20 2a 2f 20 22 72  ",. /* 298 */ "r
31c1f 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f  aisetype ::= ABO
31c20 52 54 22 2c 0a 20 2f 2a 20 32 39 39 20 2a 2f 20  RT",. /* 299 */ 
31c21 22 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 46  "raisetype ::= F
31c22 41 49 4c 22 2c 0a 20 2f 2a 20 33 30 30 20 2a 2f  AIL",. /* 300 */
31c23 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54   "cmd ::= DROP T
31c24 52 49 47 47 45 52 20 69 66 65 78 69 73 74 73 20  RIGGER ifexists 
31c25 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 33  fullname",. /* 3
31c26 30 31 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41  01 */ "cmd ::= A
31c27 54 54 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b  TTACH database_k
31c28 77 5f 6f 70 74 20 65 78 70 72 20 41 53 20 65 78  w_opt expr AS ex
31c29 70 72 20 6b 65 79 5f 6f 70 74 22 2c 0a 20 2f 2a  pr key_opt",. /*
31c2a 20 33 30 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   302 */ "cmd ::=
31c2b 20 44 45 54 41 43 48 20 64 61 74 61 62 61 73 65   DETACH database
31c2c 5f 6b 77 5f 6f 70 74 20 65 78 70 72 22 2c 0a 20  _kw_opt expr",. 
31c2d 2f 2a 20 33 30 33 20 2a 2f 20 22 6b 65 79 5f 6f  /* 303 */ "key_o
31c2e 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 30 34  pt ::=",. /* 304
31c2f 20 2a 2f 20 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d   */ "key_opt ::=
31c30 20 4b 45 59 20 65 78 70 72 22 2c 0a 20 2f 2a 20   KEY expr",. /* 
31c31 33 30 35 20 2a 2f 20 22 64 61 74 61 62 61 73 65  305 */ "database
31c32 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41  _kw_opt ::= DATA
31c33 42 41 53 45 22 2c 0a 20 2f 2a 20 33 30 36 20 2a  BASE",. /* 306 *
31c34 2f 20 22 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f  / "database_kw_o
31c35 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 30 37  pt ::=",. /* 307
31c36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49   */ "cmd ::= REI
31c37 4e 44 45 58 22 2c 0a 20 2f 2a 20 33 30 38 20 2a  NDEX",. /* 308 *
31c38 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44  / "cmd ::= REIND
31c39 45 58 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a  EX nm dbnm",. /*
31c3a 20 33 30 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   309 */ "cmd ::=
31c3b 20 41 4e 41 4c 59 5a 45 22 2c 0a 20 2f 2a 20 33   ANALYZE",. /* 3
31c3c 31 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41  10 */ "cmd ::= A
31c3d 4e 41 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d 22 2c  NALYZE nm dbnm",
31c3e 0a 20 2f 2a 20 33 31 31 20 2a 2f 20 22 63 6d 64  . /* 311 */ "cmd
31c3f 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45   ::= ALTER TABLE
31c40 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45   fullname RENAME
31c41 20 54 4f 20 6e 6d 22 2c 0a 20 2f 2a 20 33 31 32   TO nm",. /* 312
31c42 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54   */ "cmd ::= ALT
31c43 45 52 20 54 41 42 4c 45 20 61 64 64 5f 63 6f 6c  ER TABLE add_col
31c44 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 41 44 44  umn_fullname ADD
31c45 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 63 6f   kwcolumn_opt co
31c46 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 33 31 33 20 2a  lumn",. /* 313 *
31c47 2f 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75  / "add_column_fu
31c48 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e  llname ::= fulln
31c49 61 6d 65 22 2c 0a 20 2f 2a 20 33 31 34 20 2a 2f  ame",. /* 314 */
31c4a 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a   "kwcolumn_opt :
31c4b 3a 3d 22 2c 0a 20 2f 2a 20 33 31 35 20 2a 2f 20  :=",. /* 315 */ 
31c4c 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a  "kwcolumn_opt ::
31c4d 3d 20 43 4f 4c 55 4d 4e 4b 57 22 2c 0a 20 2f 2a  = COLUMNKW",. /*
31c4e 20 33 31 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   316 */ "cmd ::=
31c4f 20 63 72 65 61 74 65 5f 76 74 61 62 22 2c 0a 20   create_vtab",. 
31c50 2f 2a 20 33 31 37 20 2a 2f 20 22 63 6d 64 20 3a  /* 317 */ "cmd :
31c51 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62 20 4c  := create_vtab L
31c52 50 20 76 74 61 62 61 72 67 6c 69 73 74 20 52 50  P vtabarglist RP
31c53 22 2c 0a 20 2f 2a 20 33 31 38 20 2a 2f 20 22 63  ",. /* 318 */ "c
31c54 72 65 61 74 65 5f 76 74 61 62 20 3a 3a 3d 20 63  reate_vtab ::= c
31c55 72 65 61 74 65 6b 77 20 56 49 52 54 55 41 4c 20  reatekw VIRTUAL 
31c56 54 41 42 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53  TABLE nm dbnm US
31c57 49 4e 47 20 6e 6d 22 2c 0a 20 2f 2a 20 33 31 39  ING nm",. /* 319
31c58 20 2a 2f 20 22 76 74 61 62 61 72 67 6c 69 73 74   */ "vtabarglist
31c59 20 3a 3a 3d 20 76 74 61 62 61 72 67 22 2c 0a 20   ::= vtabarg",. 
31c5a 2f 2a 20 33 32 30 20 2a 2f 20 22 76 74 61 62 61  /* 320 */ "vtaba
31c5b 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61  rglist ::= vtaba
31c5c 72 67 6c 69 73 74 20 43 4f 4d 4d 41 20 76 74 61  rglist COMMA vta
31c5d 62 61 72 67 22 2c 0a 20 2f 2a 20 33 32 31 20 2a  barg",. /* 321 *
31c5e 2f 20 22 76 74 61 62 61 72 67 20 3a 3a 3d 22 2c  / "vtabarg ::=",
31c5f 0a 20 2f 2a 20 33 32 32 20 2a 2f 20 22 76 74 61  . /* 322 */ "vta
31c60 62 61 72 67 20 3a 3a 3d 20 76 74 61 62 61 72 67  barg ::= vtabarg
31c61 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c 0a   vtabargtoken",.
31c62 20 2f 2a 20 33 32 33 20 2a 2f 20 22 76 74 61 62   /* 323 */ "vtab
31c63 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59  argtoken ::= ANY
31c64 22 2c 0a 20 2f 2a 20 33 32 34 20 2a 2f 20 22 76  ",. /* 324 */ "v
31c65 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20  tabargtoken ::= 
31c66 6c 70 20 61 6e 79 6c 69 73 74 20 52 50 22 2c 0a  lp anylist RP",.
31c67 20 2f 2a 20 33 32 35 20 2a 2f 20 22 6c 70 20 3a   /* 325 */ "lp :
31c68 3a 3d 20 4c 50 22 2c 0a 20 2f 2a 20 33 32 36 20  := LP",. /* 326 
31c69 2a 2f 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 22  */ "anylist ::="
31c6a 2c 0a 20 2f 2a 20 33 32 37 20 2a 2f 20 22 61 6e  ,. /* 327 */ "an
31c6b 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73  ylist ::= anylis
31c6c 74 20 4c 50 20 61 6e 79 6c 69 73 74 20 52 50 22  t LP anylist RP"
31c6d 2c 0a 20 2f 2a 20 33 32 38 20 2a 2f 20 22 61 6e  ,. /* 328 */ "an
31c6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73  ylist ::= anylis
31c6f 74 20 41 4e 59 22 2c 0a 7d 3b 0a 23 65 6e 64 69  t ANY",.};.#endi
31c70 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a  f /* NDEBUG */..
31c71 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54  .#if YYSTACKDEPT
31c72 48 3c 3d 30 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74  H<=0./*.** Try t
31c73 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73  o increase the s
31c74 69 7a 65 20 6f 66 20 74 68 65 20 70 61 72 73 65  ize of the parse
31c75 72 20 73 74 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74  r stack..*/.stat
31c76 69 63 20 76 6f 69 64 20 79 79 47 72 6f 77 53 74  ic void yyGrowSt
31c77 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a 70 29  ack(yyParser *p)
31c78 7b 0a 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b  {.  int newSize;
31c79 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  .  yyStackEntry 
31c7a 2a 70 4e 65 77 3b 0a 0a 20 20 6e 65 77 53 69 7a  *pNew;..  newSiz
31c7b 65 20 3d 20 70 2d 3e 79 79 73 74 6b 73 7a 2a 32  e = p->yystksz*2
31c7c 20 2b 20 31 30 30 3b 0a 20 20 70 4e 65 77 20 3d   + 100;.  pNew =
31c7d 20 72 65 61 6c 6c 6f 63 28 70 2d 3e 79 79 73 74   realloc(p->yyst
31c7e 61 63 6b 2c 20 6e 65 77 53 69 7a 65 2a 73 69 7a  ack, newSize*siz
31c7f 65 6f 66 28 70 4e 65 77 5b 30 5d 29 29 3b 0a 20  eof(pNew[0]));. 
31c80 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
31c81 20 70 2d 3e 79 79 73 74 61 63 6b 20 3d 20 70 4e   p->yystack = pN
31c82 65 77 3b 0a 20 20 20 20 70 2d 3e 79 79 73 74 6b  ew;.    p->yystk
31c83 73 7a 20 3d 20 6e 65 77 53 69 7a 65 3b 0a 23 69  sz = newSize;.#i
31c84 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  fndef NDEBUG.   
31c85 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
31c86 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
31c87 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
31c88 73 53 74 61 63 6b 20 67 72 6f 77 73 20 74 6f 20  sStack grows to 
31c89 25 64 20 65 6e 74 72 69 65 73 21 5c 6e 22 2c 0a  %d entries!\n",.
31c8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79                yy
31c8b 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 70 2d 3e  TracePrompt, p->
31c8c 79 79 73 74 6b 73 7a 29 3b 0a 20 20 20 20 7d 0a  yystksz);.    }.
31c8d 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 23 65 6e  #endif.  }.}.#en
31c8e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73  dif../* .** This
31c8f 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61   function alloca
31c90 74 65 73 20 61 20 6e 65 77 20 70 61 72 73 65 72  tes a new parser
31c91 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 61 72  ..** The only ar
31c92 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e  gument is a poin
31c93 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
31c94 6e 20 77 68 69 63 68 20 77 6f 72 6b 73 20 6c 69  n which works li
31c95 6b 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a  ke.** malloc..**
31c96 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 41  .** Inputs:.** A
31c97 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
31c98 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f  function used to
31c99 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79   allocate memory
31c9a 2e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a  ..**.** Outputs:
31c9b 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  .** A pointer to
31c9c 20 61 20 70 61 72 73 65 72 2e 20 20 54 68 69 73   a parser.  This
31c9d 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64   pointer is used
31c9e 20 69 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63   in subsequent c
31c9f 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  alls.** to sqlit
31ca0 65 33 50 61 72 73 65 72 20 61 6e 64 20 73 71 6c  e3Parser and sql
31ca1 69 74 65 33 50 61 72 73 65 72 46 72 65 65 2e 0a  ite3ParserFree..
31ca2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
31ca3 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50  E void *sqlite3P
31ca4 61 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 20  arserAlloc(void 
31ca5 2a 28 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29 28 73  *(*mallocProc)(s
31ca6 69 7a 65 5f 74 29 29 7b 0a 20 20 79 79 50 61 72  ize_t)){.  yyPar
31ca7 73 65 72 20 2a 70 50 61 72 73 65 72 3b 0a 20 20  ser *pParser;.  
31ca8 70 50 61 72 73 65 72 20 3d 20 28 79 79 50 61 72  pParser = (yyPar
31ca9 73 65 72 2a 29 28 2a 6d 61 6c 6c 6f 63 50 72 6f  ser*)(*mallocPro
31caa 63 29 28 20 28 73 69 7a 65 5f 74 29 73 69 7a 65  c)( (size_t)size
31cab 6f 66 28 79 79 50 61 72 73 65 72 29 20 29 3b 0a  of(yyParser) );.
31cac 20 20 69 66 28 20 70 50 61 72 73 65 72 20 29 7b    if( pParser ){
31cad 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79  .    pParser->yy
31cae 69 64 78 20 3d 20 2d 31 3b 0a 23 69 66 64 65 66  idx = -1;.#ifdef
31caf 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b   YYTRACKMAXSTACK
31cb0 44 45 50 54 48 0a 20 20 20 20 70 50 61 72 73 65  DEPTH.    pParse
31cb1 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20 30 3b  r->yyidxMax = 0;
31cb2 0a 23 65 6e 64 69 66 0a 23 69 66 20 59 59 53 54  .#endif.#if YYST
31cb3 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20 20  ACKDEPTH<=0.    
31cb4 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
31cb5 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 70 50 61   = NULL;.    pPa
31cb6 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20 3d 20  rser->yystksz = 
31cb7 30 3b 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61  0;.    yyGrowSta
31cb8 63 6b 28 70 50 61 72 73 65 72 29 3b 0a 23 65 6e  ck(pParser);.#en
31cb9 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  dif.  }.  return
31cba 20 70 50 61 72 73 65 72 3b 0a 7d 0a 0a 2f 2a 20   pParser;.}../* 
31cbb 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75  The following fu
31cbc 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74  nction deletes t
31cbd 68 65 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61  he value associa
31cbe 74 65 64 20 77 69 74 68 20 61 0a 2a 2a 20 73 79  ted with a.** sy
31cbf 6d 62 6f 6c 2e 20 20 54 68 65 20 73 79 6d 62 6f  mbol.  The symbo
31cc0 6c 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  l can be either 
31cc1 61 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f  a terminal or no
31cc2 6e 74 65 72 6d 69 6e 61 6c 2e 0a 2a 2a 20 22 79  nterminal..** "y
31cc3 79 6d 61 6a 6f 72 22 20 69 73 20 74 68 65 20 73  ymajor" is the s
31cc4 79 6d 62 6f 6c 20 63 6f 64 65 2c 20 61 6e 64 20  ymbol code, and 
31cc5 22 79 79 70 6d 69 6e 6f 72 22 20 69 73 20 61 20  "yypminor" is a 
31cc6 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68  pointer to.** th
31cc7 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  e value..*/.stat
31cc8 69 63 20 76 6f 69 64 20 79 79 5f 64 65 73 74 72  ic void yy_destr
31cc9 75 63 74 6f 72 28 0a 20 20 79 79 50 61 72 73 65  uctor(.  yyParse
31cca 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20  r *yypParser,   
31ccb 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
31ccc 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 79  /.  YYCODETYPE y
31ccd 79 6d 61 6a 6f 72 2c 20 20 20 20 20 2f 2a 20 54  ymajor,     /* T
31cce 79 70 65 20 63 6f 64 65 20 66 6f 72 20 6f 62 6a  ype code for obj
31ccf 65 63 74 20 74 6f 20 64 65 73 74 72 6f 79 20 2a  ect to destroy *
31cd0 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20  /.  YYMINORTYPE 
31cd1 2a 79 79 70 6d 69 6e 6f 72 20 20 20 2f 2a 20 54  *yypminor   /* T
31cd2 68 65 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20  he object to be 
31cd3 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 29 7b 0a  destroyed */.){.
31cd4 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
31cd5 52 47 5f 46 45 54 43 48 3b 0a 20 20 73 77 69 74  RG_FETCH;.  swit
31cd6 63 68 28 20 79 79 6d 61 6a 6f 72 20 29 7b 0a 20  ch( yymajor ){. 
31cd7 20 20 20 2f 2a 20 48 65 72 65 20 69 73 20 69 6e     /* Here is in
31cd8 73 65 72 74 65 64 20 74 68 65 20 61 63 74 69 6f  serted the actio
31cd9 6e 73 20 77 68 69 63 68 20 74 61 6b 65 20 70 6c  ns which take pl
31cda 61 63 65 20 77 68 65 6e 20 61 0a 20 20 20 20 2a  ace when a.    *
31cdb 2a 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f  * terminal or no
31cdc 6e 2d 74 65 72 6d 69 6e 61 6c 20 69 73 20 64 65  n-terminal is de
31cdd 73 74 72 6f 79 65 64 2e 20 20 54 68 69 73 20 63  stroyed.  This c
31cde 61 6e 20 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a  an happen.    **
31cdf 20 77 68 65 6e 20 74 68 65 20 73 79 6d 62 6f 6c   when the symbol
31ce0 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20   is popped from 
31ce1 74 68 65 20 73 74 61 63 6b 20 64 75 72 69 6e 67  the stack during
31ce2 20 61 0a 20 20 20 20 2a 2a 20 72 65 64 75 63 65   a.    ** reduce
31ce3 20 6f 72 20 64 75 72 69 6e 67 20 65 72 72 6f 72   or during error
31ce4 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 72 20 77   processing or w
31ce5 68 65 6e 20 61 20 70 61 72 73 65 72 20 69 73 20  hen a parser is 
31ce6 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 65  .    ** being de
31ce7 73 74 72 6f 79 65 64 20 62 65 66 6f 72 65 20 69  stroyed before i
31ce8 74 20 69 73 20 66 69 6e 69 73 68 65 64 20 70 61  t is finished pa
31ce9 72 73 69 6e 67 2e 0a 20 20 20 20 2a 2a 0a 20 20  rsing..    **.  
31cea 20 20 2a 2a 20 4e 6f 74 65 3a 20 64 75 72 69 6e    ** Note: durin
31ceb 67 20 61 20 72 65 64 75 63 65 2c 20 74 68 65 20  g a reduce, the 
31cec 6f 6e 6c 79 20 73 79 6d 62 6f 6c 73 20 64 65 73  only symbols des
31ced 74 72 6f 79 65 64 20 61 72 65 20 74 68 6f 73 65  troyed are those
31cee 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 20 61 70  .    ** which ap
31cef 70 65 61 72 20 6f 6e 20 74 68 65 20 52 48 53 20  pear on the RHS 
31cf0 6f 66 20 74 68 65 20 72 75 6c 65 2c 20 62 75 74  of the rule, but
31cf1 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 75   which are not u
31cf2 73 65 64 0a 20 20 20 20 2a 2a 20 69 6e 73 69 64  sed.    ** insid
31cf3 65 20 74 68 65 20 43 20 63 6f 64 65 2e 0a 20 20  e the C code..  
31cf4 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 36    */.    case 16
31cf5 30 3a 20 2f 2a 20 73 65 6c 65 63 74 20 2a 2f 0a  0: /* select */.
31cf6 20 20 20 20 63 61 73 65 20 31 39 34 3a 20 2f 2a      case 194: /*
31cf7 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a   oneselect */.{.
31cf8 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
31cf9 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
31cfa 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 29 29  (yypminor->yy3))
31cfb 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.}.      break;
31cfc 0a 20 20 20 20 63 61 73 65 20 31 37 34 3a 20 2f  .    case 174: /
31cfd 2a 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 63 61  * term */.    ca
31cfe 73 65 20 31 37 35 3a 20 2f 2a 20 65 78 70 72 20  se 175: /* expr 
31cff 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 33 3a  */.    case 223:
31d00 20 2f 2a 20 65 73 63 61 70 65 20 2a 2f 0a 7b 0a   /* escape */.{.
31d01 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
31d02 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79  e(pParse->db, (y
31d03 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 34 36 29 2e  ypminor->yy346).
31d04 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20 20  pExpr);.}.      
31d05 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
31d06 31 37 39 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f  179: /* idxlist_
31d07 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
31d08 31 38 37 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20  187: /* idxlist 
31d09 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39 37 3a  */.    case 197:
31d0a 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 2a   /* selcollist *
31d0b 2f 0a 20 20 20 20 63 61 73 65 20 32 30 30 3a 20  /.    case 200: 
31d0c 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 2a  /* groupby_opt *
31d0d 2f 0a 20 20 20 20 63 61 73 65 20 32 30 32 3a 20  /.    case 202: 
31d0e 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 2a  /* orderby_opt *
31d0f 2f 0a 20 20 20 20 63 61 73 65 20 32 30 34 3a 20  /.    case 204: 
31d10 2f 2a 20 73 63 6c 70 20 2a 2f 0a 20 20 20 20 63  /* sclp */.    c
31d11 61 73 65 20 32 31 34 3a 20 2f 2a 20 73 6f 72 74  ase 214: /* sort
31d12 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  list */.    case
31d13 20 32 31 36 3a 20 2f 2a 20 6e 65 78 70 72 6c 69   216: /* nexprli
31d14 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  st */.    case 2
31d15 31 37 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20 2a  17: /* setlist *
31d16 2f 0a 20 20 20 20 63 61 73 65 20 32 32 30 3a 20  /.    case 220: 
31d17 2f 2a 20 69 74 65 6d 6c 69 73 74 20 2a 2f 0a 20  /* itemlist */. 
31d18 20 20 20 63 61 73 65 20 32 32 31 3a 20 2f 2a 20     case 221: /* 
31d19 65 78 70 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20  exprlist */.    
31d1a 63 61 73 65 20 32 32 37 3a 20 2f 2a 20 63 61 73  case 227: /* cas
31d1b 65 5f 65 78 70 72 6c 69 73 74 20 2a 2f 0a 7b 0a  e_exprlist */.{.
31d1c 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
31d1d 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
31d1e 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31  , (yypminor->yy1
31d1f 34 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  4));.}.      bre
31d20 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 39 33  ak;.    case 193
31d21 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f  : /* fullname */
31d22 0a 20 20 20 20 63 61 73 65 20 31 39 38 3a 20 2f  .    case 198: /
31d23 2a 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 61  * from */.    ca
31d24 73 65 20 32 30 36 3a 20 2f 2a 20 73 65 6c 74 61  se 206: /* selta
31d25 62 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73  blist */.    cas
31d26 65 20 32 30 37 3a 20 2f 2a 20 73 74 6c 5f 70 72  e 207: /* stl_pr
31d27 65 66 69 78 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65  efix */.{.sqlite
31d28 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70  3SrcListDelete(p
31d29 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d  Parse->db, (yypm
31d2a 69 6e 6f 72 2d 3e 79 79 36 35 29 29 3b 0a 7d 0a  inor->yy65));.}.
31d2b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31d2c 20 63 61 73 65 20 31 39 39 3a 20 2f 2a 20 77 68   case 199: /* wh
31d2d 65 72 65 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63  ere_opt */.    c
31d2e 61 73 65 20 32 30 31 3a 20 2f 2a 20 68 61 76 69  ase 201: /* havi
31d2f 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61  ng_opt */.    ca
31d30 73 65 20 32 31 30 3a 20 2f 2a 20 6f 6e 5f 6f 70  se 210: /* on_op
31d31 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31  t */.    case 21
31d32 35 3a 20 2f 2a 20 73 6f 72 74 69 74 65 6d 20 2a  5: /* sortitem *
31d33 2f 0a 20 20 20 20 63 61 73 65 20 32 32 36 3a 20  /.    case 226: 
31d34 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20  /* case_operand 
31d35 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 32 38 3a  */.    case 228:
31d36 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20 2a 2f   /* case_else */
31d37 0a 20 20 20 20 63 61 73 65 20 32 33 39 3a 20 2f  .    case 239: /
31d38 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 2a 2f  * when_clause */
31d39 0a 20 20 20 20 63 61 73 65 20 32 34 34 3a 20 2f  .    case 244: /
31d3a 2a 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a 73  * key_opt */.{.s
31d3b 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
31d3c 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79  (pParse->db, (yy
31d3d 70 6d 69 6e 6f 72 2d 3e 79 79 31 33 32 29 29 3b  pminor->yy132));
31d3e 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .}.      break;.
31d3f 20 20 20 20 63 61 73 65 20 32 31 31 3a 20 2f 2a      case 211: /*
31d40 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20   using_opt */.  
31d41 20 20 63 61 73 65 20 32 31 33 3a 20 2f 2a 20 69    case 213: /* i
31d42 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a 20 20 20  nscollist */.   
31d43 20 63 61 73 65 20 32 31 39 3a 20 2f 2a 20 69 6e   case 219: /* in
31d44 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 2a 2f 0a  scollist_opt */.
31d45 7b 0a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44  {.sqlite3IdListD
31d46 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
31d47 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 34  , (yypminor->yy4
31d48 30 38 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72  08));.}.      br
31d49 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 33  eak;.    case 23
31d4a 35 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d  5: /* trigger_cm
31d4b 64 5f 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61  d_list */.    ca
31d4c 73 65 20 32 34 30 3a 20 2f 2a 20 74 72 69 67 67  se 240: /* trigg
31d4d 65 72 5f 63 6d 64 20 2a 2f 0a 7b 0a 73 71 6c 69  er_cmd */.{.sqli
31d4e 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
31d4f 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  Step(pParse->db,
31d50 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 37   (yypminor->yy47
31d51 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  3));.}.      bre
31d52 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 33 37  ak;.    case 237
31d53 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65  : /* trigger_eve
31d54 6e 74 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49  nt */.{.sqlite3I
31d55 64 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  dListDelete(pPar
31d56 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f  se->db, (yypmino
31d57 72 2d 3e 79 79 33 37 38 29 2e 62 29 3b 0a 7d 0a  r->yy378).b);.}.
31d58 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31d59 20 64 65 66 61 75 6c 74 3a 20 20 62 72 65 61 6b   default:  break
31d5a 3b 20 20 20 2f 2a 20 49 66 20 6e 6f 20 64 65 73  ;   /* If no des
31d5b 74 72 75 63 74 6f 72 20 61 63 74 69 6f 6e 20 73  tructor action s
31d5c 70 65 63 69 66 69 65 64 3a 20 64 6f 20 6e 6f 74  pecified: do not
31d5d 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f  hing */.  }.}../
31d5e 2a 0a 2a 2a 20 50 6f 70 20 74 68 65 20 70 61 72  *.** Pop the par
31d5f 73 65 72 27 73 20 73 74 61 63 6b 20 6f 6e 63 65  ser's stack once
31d60 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
31d61 20 69 73 20 61 20 64 65 73 74 72 75 63 74 6f 72   is a destructor
31d62 20 72 6f 75 74 69 6e 65 20 61 73 73 6f 63 69 61   routine associa
31d63 74 65 64 20 77 69 74 68 20 74 68 65 20 74 6f 6b  ted with the tok
31d64 65 6e 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 70  en which.** is p
31d65 6f 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73  opped from the s
31d66 74 61 63 6b 2c 20 74 68 65 6e 20 63 61 6c 6c 20  tack, then call 
31d67 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  it..**.** Return
31d68 20 74 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e   the major token
31d69 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
31d6a 73 79 6d 62 6f 6c 20 70 6f 70 70 65 64 2e 0a 2a  symbol popped..*
31d6b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f  /.static int yy_
31d6c 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
31d6d 28 79 79 50 61 72 73 65 72 20 2a 70 50 61 72 73  (yyParser *pPars
31d6e 65 72 29 7b 0a 20 20 59 59 43 4f 44 45 54 59 50  er){.  YYCODETYP
31d6f 45 20 79 79 6d 61 6a 6f 72 3b 0a 20 20 79 79 53  E yymajor;.  yyS
31d70 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f 73  tackEntry *yytos
31d71 20 3d 20 26 70 50 61 72 73 65 72 2d 3e 79 79 73   = &pParser->yys
31d72 74 61 63 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79  tack[pParser->yy
31d73 69 64 78 5d 3b 0a 0a 20 20 2f 2a 20 54 68 65 72  idx];..  /* Ther
31d74 65 20 69 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73  e is no mechanis
31d75 6d 20 62 79 20 77 68 69 63 68 20 74 68 65 20 70  m by which the p
31d76 61 72 73 65 72 20 73 74 61 63 6b 20 63 61 6e 20  arser stack can 
31d77 62 65 20 70 6f 70 70 65 64 20 62 65 6c 6f 77 0a  be popped below.
31d78 20 20 2a 2a 20 65 6d 70 74 79 20 69 6e 20 53 51    ** empty in SQ
31d79 4c 69 74 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20  Lite.  */.  if( 
31d7a 4e 45 56 45 52 28 70 50 61 72 73 65 72 2d 3e 79  NEVER(pParser->y
31d7b 79 69 64 78 3c 30 29 20 29 20 72 65 74 75 72 6e  yidx<0) ) return
31d7c 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42   0;.#ifndef NDEB
31d7d 55 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65  UG.  if( yyTrace
31d7e 46 49 4c 45 20 26 26 20 70 50 61 72 73 65 72 2d  FILE && pParser-
31d7f 3e 79 79 69 64 78 3e 3d 30 20 29 7b 0a 20 20 20  >yyidx>=0 ){.   
31d80 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
31d81 46 49 4c 45 2c 22 25 73 50 6f 70 70 69 6e 67 20  FILE,"%sPopping 
31d82 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 79 79 54  %s\n",.      yyT
31d83 72 61 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20 20  racePrompt,.    
31d84 20 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79    yyTokenName[yy
31d85 74 6f 73 2d 3e 6d 61 6a 6f 72 5d 29 3b 0a 20 20  tos->major]);.  
31d86 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79 6d 61 6a  }.#endif.  yymaj
31d87 6f 72 20 3d 20 79 79 74 6f 73 2d 3e 6d 61 6a 6f  or = yytos->majo
31d88 72 3b 0a 20 20 79 79 5f 64 65 73 74 72 75 63 74  r;.  yy_destruct
31d89 6f 72 28 70 50 61 72 73 65 72 2c 20 79 79 6d 61  or(pParser, yyma
31d8a 6a 6f 72 2c 20 26 79 79 74 6f 73 2d 3e 6d 69 6e  jor, &yytos->min
31d8b 6f 72 29 3b 0a 20 20 70 50 61 72 73 65 72 2d 3e  or);.  pParser->
31d8c 79 79 69 64 78 2d 2d 3b 0a 20 20 72 65 74 75 72  yyidx--;.  retur
31d8d 6e 20 79 79 6d 61 6a 6f 72 3b 0a 7d 0a 0a 2f 2a  n yymajor;.}../*
31d8e 20 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20   .** Deallocate 
31d8f 61 6e 64 20 64 65 73 74 72 6f 79 20 61 20 70 61  and destroy a pa
31d90 72 73 65 72 2e 20 20 44 65 73 74 72 75 63 74 6f  rser.  Destructo
31d91 72 73 20 61 72 65 20 61 6c 6c 20 63 61 6c 6c 65  rs are all calle
31d92 64 20 66 6f 72 0a 2a 2a 20 61 6c 6c 20 73 74 61  d for.** all sta
31d93 63 6b 20 65 6c 65 6d 65 6e 74 73 20 62 65 66 6f  ck elements befo
31d94 72 65 20 73 68 75 74 74 69 6e 67 20 74 68 65 20  re shutting the 
31d95 70 61 72 73 65 72 20 64 6f 77 6e 2e 0a 2a 2a 0a  parser down..**.
31d96 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75  ** Inputs:.** <u
31d97 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 41 20 70 6f  l>.** <li>  A po
31d98 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 72  inter to the par
31d99 73 65 72 2e 20 20 54 68 69 73 20 73 68 6f 75 6c  ser.  This shoul
31d9a 64 20 62 65 20 61 20 70 6f 69 6e 74 65 72 0a 2a  d be a pointer.*
31d9b 2a 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65 64  *       obtained
31d9c 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 72   from sqlite3Par
31d9d 73 65 72 41 6c 6c 6f 63 2e 0a 2a 2a 20 3c 6c 69  serAlloc..** <li
31d9e 3e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  >  A pointer to 
31d9f 61 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20  a function used 
31da0 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72  to reclaim memor
31da1 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20  y obtained.**   
31da2 20 20 20 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e      from malloc.
31da3 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f 0a 53 51 4c  .** </ul>.*/.SQL
31da4 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
31da5 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72   sqlite3ParserFr
31da6 65 65 28 0a 20 20 76 6f 69 64 20 2a 70 2c 20 20  ee(.  void *p,  
31da7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31da8 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
31da9 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f  to be deleted */
31daa 0a 20 20 76 6f 69 64 20 28 2a 66 72 65 65 50 72  .  void (*freePr
31dab 6f 63 29 28 76 6f 69 64 2a 29 20 20 20 20 20 2f  oc)(void*)     /
31dac 2a 20 46 75 6e 63 74 69 6f 6e 20 75 73 65 64 20  * Function used 
31dad 74 6f 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72  to reclaim memor
31dae 79 20 2a 2f 0a 29 7b 0a 20 20 79 79 50 61 72 73  y */.){.  yyPars
31daf 65 72 20 2a 70 50 61 72 73 65 72 20 3d 20 28 79  er *pParser = (y
31db0 79 50 61 72 73 65 72 2a 29 70 3b 0a 20 20 2f 2a  yParser*)p;.  /*
31db1 20 49 6e 20 53 51 4c 69 74 65 2c 20 77 65 20 6e   In SQLite, we n
31db2 65 76 65 72 20 74 72 79 20 74 6f 20 64 65 73 74  ever try to dest
31db3 72 6f 79 20 61 20 70 61 72 73 65 72 20 74 68 61  roy a parser tha
31db4 74 20 77 61 73 20 6e 6f 74 20 73 75 63 63 65 73  t was not succes
31db5 73 66 75 6c 6c 79 0a 20 20 2a 2a 20 63 72 65 61  sfully.  ** crea
31db6 74 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74  ted in the first
31db7 20 70 6c 61 63 65 2e 20 2a 2f 0a 20 20 69 66 28   place. */.  if(
31db8 20 4e 45 56 45 52 28 70 50 61 72 73 65 72 3d 3d   NEVER(pParser==
31db9 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 77  0) ) return;.  w
31dba 68 69 6c 65 28 20 70 50 61 72 73 65 72 2d 3e 79  hile( pParser->y
31dbb 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70  yidx>=0 ) yy_pop
31dbc 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 70 50  _parser_stack(pP
31dbd 61 72 73 65 72 29 3b 0a 23 69 66 20 59 59 53 54  arser);.#if YYST
31dbe 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 66 72  ACKDEPTH<=0.  fr
31dbf 65 65 28 70 50 61 72 73 65 72 2d 3e 79 79 73 74  ee(pParser->yyst
31dc0 61 63 6b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 28  ack);.#endif.  (
31dc1 2a 66 72 65 65 50 72 6f 63 29 28 28 76 6f 69 64  *freeProc)((void
31dc2 2a 29 70 50 61 72 73 65 72 29 3b 0a 7d 0a 0a 2f  *)pParser);.}../
31dc3 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
31dc4 70 65 61 6b 20 64 65 70 74 68 20 6f 66 20 74 68  peak depth of th
31dc5 65 20 73 74 61 63 6b 20 66 6f 72 20 61 20 70 61  e stack for a pa
31dc6 72 73 65 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  rser..*/.#ifdef 
31dc7 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44  YYTRACKMAXSTACKD
31dc8 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 56  EPTH.SQLITE_PRIV
31dc9 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
31dca 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b 28 76  arserStackPeak(v
31dcb 6f 69 64 20 2a 70 29 7b 0a 20 20 79 79 50 61 72  oid *p){.  yyPar
31dcc 73 65 72 20 2a 70 50 61 72 73 65 72 20 3d 20 28  ser *pParser = (
31dcd 79 79 50 61 72 73 65 72 2a 29 70 3b 0a 20 20 72  yyParser*)p;.  r
31dce 65 74 75 72 6e 20 70 50 61 72 73 65 72 2d 3e 79  eturn pParser->y
31dcf 79 69 64 78 4d 61 78 3b 0a 7d 0a 23 65 6e 64 69  yidxMax;.}.#endi
31dd0 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  f../*.** Find th
31dd1 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 61 63  e appropriate ac
31dd2 74 69 6f 6e 20 66 6f 72 20 61 20 70 61 72 73 65  tion for a parse
31dd3 72 20 67 69 76 65 6e 20 74 68 65 20 74 65 72 6d  r given the term
31dd4 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65  inal.** look-ahe
31dd5 61 64 20 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68  ad token iLookAh
31dd6 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ead..**.** If th
31dd7 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b  e look-ahead tok
31dd8 65 6e 20 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20  en is YYNOCODE, 
31dd9 74 68 65 6e 20 63 68 65 63 6b 20 74 6f 20 73 65  then check to se
31dda 65 20 69 66 20 74 68 65 20 61 63 74 69 6f 6e 20  e if the action 
31ddb 69 73 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e  is.** independen
31ddc 74 20 6f 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68  t of the look-ah
31ddd 65 61 64 2e 20 20 49 66 20 69 74 20 69 73 2c 20  ead.  If it is, 
31dde 72 65 74 75 72 6e 20 74 68 65 20 61 63 74 69 6f  return the actio
31ddf 6e 2c 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20  n, otherwise.** 
31de0 72 65 74 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54  return YY_NO_ACT
31de1 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ION..*/.static i
31de2 6e 74 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74  nt yy_find_shift
31de3 5f 61 63 74 69 6f 6e 28 0a 20 20 79 79 50 61 72  _action(.  yyPar
31de4 73 65 72 20 2a 70 50 61 72 73 65 72 2c 20 20 20  ser *pParser,   
31de5 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
31de6 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59  er */.  YYCODETY
31de7 50 45 20 69 4c 6f 6f 6b 41 68 65 61 64 20 20 20  PE iLookAhead   
31de8 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61 68    /* The look-ah
31de9 65 61 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a  ead token */.){.
31dea 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 73    int i;.  int s
31deb 74 61 74 65 6e 6f 20 3d 20 70 50 61 72 73 65 72  tateno = pParser
31dec 2d 3e 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65  ->yystack[pParse
31ded 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e  r->yyidx].staten
31dee 6f 3b 0a 20 0a 20 20 69 66 28 20 73 74 61 74 65  o;. .  if( state
31def 6e 6f 3e 59 59 5f 53 48 49 46 54 5f 43 4f 55 4e  no>YY_SHIFT_COUN
31df0 54 0a 20 20 20 7c 7c 20 28 69 20 3d 20 79 79 5f  T.   || (i = yy_
31df1 73 68 69 66 74 5f 6f 66 73 74 5b 73 74 61 74 65  shift_ofst[state
31df2 6e 6f 5d 29 3d 3d 59 59 5f 53 48 49 46 54 5f 55  no])==YY_SHIFT_U
31df3 53 45 5f 44 46 4c 54 20 29 7b 0a 20 20 20 20 72  SE_DFLT ){.    r
31df4 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74  eturn yy_default
31df5 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 20  [stateno];.  }. 
31df6 20 61 73 73 65 72 74 28 20 69 4c 6f 6f 6b 41 68   assert( iLookAh
31df7 65 61 64 21 3d 59 59 4e 4f 43 4f 44 45 20 29 3b  ead!=YYNOCODE );
31df8 0a 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68 65  .  i += iLookAhe
31df9 61 64 3b 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c  ad;.  if( i<0 ||
31dfa 20 69 3e 3d 59 59 5f 41 43 54 54 41 42 5f 43 4f   i>=YY_ACTTAB_CO
31dfb 55 4e 54 20 7c 7c 20 79 79 5f 6c 6f 6f 6b 61 68  UNT || yy_lookah
31dfc 65 61 64 5b 69 5d 21 3d 69 4c 6f 6f 6b 41 68 65  ead[i]!=iLookAhe
31dfd 61 64 20 29 7b 0a 20 20 20 20 69 66 28 20 69 4c  ad ){.    if( iL
31dfe 6f 6f 6b 41 68 65 61 64 3e 30 20 29 7b 0a 23 69  ookAhead>0 ){.#i
31dff 66 64 65 66 20 59 59 46 41 4c 4c 42 41 43 4b 0a  fdef YYFALLBACK.
31e00 20 20 20 20 20 20 59 59 43 4f 44 45 54 59 50 45        YYCODETYPE
31e01 20 69 46 61 6c 6c 62 61 63 6b 3b 20 20 20 20 20   iFallback;     
31e02 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 62 61         /* Fallba
31e03 63 6b 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20  ck token */.    
31e04 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61 64    if( iLookAhead
31e05 3c 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61  <sizeof(yyFallba
31e06 63 6b 29 2f 73 69 7a 65 6f 66 28 79 79 46 61 6c  ck)/sizeof(yyFal
31e07 6c 62 61 63 6b 5b 30 5d 29 0a 20 20 20 20 20 20  lback[0]).      
31e08 20 20 20 20 20 20 20 26 26 20 28 69 46 61 6c 6c         && (iFall
31e09 62 61 63 6b 20 3d 20 79 79 46 61 6c 6c 62 61 63  back = yyFallbac
31e0a 6b 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 29 21 3d  k[iLookAhead])!=
31e0b 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45  0 ){.#ifndef NDE
31e0c 42 55 47 0a 20 20 20 20 20 20 20 20 69 66 28 20  BUG.        if( 
31e0d 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
31e0e 20 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66           fprintf
31e0f 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20 22 25  (yyTraceFILE, "%
31e10 73 46 41 4c 4c 42 41 43 4b 20 25 73 20 3d 3e 20  sFALLBACK %s => 
31e11 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  %s\n",.         
31e12 20 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70      yyTracePromp
31e13 74 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69  t, yyTokenName[i
31e14 4c 6f 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f  LookAhead], yyTo
31e15 6b 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c 62 61 63  kenName[iFallbac
31e16 6b 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23  k]);.        }.#
31e17 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 72 65  endif.        re
31e18 74 75 72 6e 20 79 79 5f 66 69 6e 64 5f 73 68 69  turn yy_find_shi
31e19 66 74 5f 61 63 74 69 6f 6e 28 70 50 61 72 73 65  ft_action(pParse
31e1a 72 2c 20 69 46 61 6c 6c 62 61 63 6b 29 3b 0a 20  r, iFallback);. 
31e1b 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69       }.#endif.#i
31e1c 66 64 65 66 20 59 59 57 49 4c 44 43 41 52 44 0a  fdef YYWILDCARD.
31e1d 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
31e1e 69 6e 74 20 6a 20 3d 20 69 20 2d 20 69 4c 6f 6f  int j = i - iLoo
31e1f 6b 41 68 65 61 64 20 2b 20 59 59 57 49 4c 44 43  kAhead + YYWILDC
31e20 41 52 44 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ARD;.        if(
31e21 20 0a 23 69 66 20 59 59 5f 53 48 49 46 54 5f 4d   .#if YY_SHIFT_M
31e22 49 4e 2b 59 59 57 49 4c 44 43 41 52 44 3c 30 0a  IN+YYWILDCARD<0.
31e23 20 20 20 20 20 20 20 20 20 20 6a 3e 3d 30 20 26            j>=0 &
31e24 26 0a 23 65 6e 64 69 66 0a 23 69 66 20 59 59 5f  &.#endif.#if YY_
31e25 53 48 49 46 54 5f 4d 41 58 2b 59 59 57 49 4c 44  SHIFT_MAX+YYWILD
31e26 43 41 52 44 3e 3d 59 59 5f 41 43 54 54 41 42 5f  CARD>=YY_ACTTAB_
31e27 43 4f 55 4e 54 0a 20 20 20 20 20 20 20 20 20 20  COUNT.          
31e28 6a 3c 59 59 5f 41 43 54 54 41 42 5f 43 4f 55 4e  j<YY_ACTTAB_COUN
31e29 54 20 26 26 0a 23 65 6e 64 69 66 0a 20 20 20 20  T &&.#endif.    
31e2a 20 20 20 20 20 20 79 79 5f 6c 6f 6f 6b 61 68 65        yy_lookahe
31e2b 61 64 5b 6a 5d 3d 3d 59 59 57 49 4c 44 43 41 52  ad[j]==YYWILDCAR
31e2c 44 0a 20 20 20 20 20 20 20 20 29 7b 0a 23 69 66  D.        ){.#if
31e2d 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20  ndef NDEBUG.    
31e2e 20 20 20 20 20 20 69 66 28 20 79 79 54 72 61 63        if( yyTrac
31e2f 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20  eFILE ){.       
31e30 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
31e31 72 61 63 65 46 49 4c 45 2c 20 22 25 73 57 49 4c  raceFILE, "%sWIL
31e32 44 43 41 52 44 20 25 73 20 3d 3e 20 25 73 5c 6e  DCARD %s => %s\n
31e33 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
31e34 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c    yyTracePrompt,
31e35 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f   yyTokenName[iLo
31e36 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65  okAhead], yyToke
31e37 6e 4e 61 6d 65 5b 59 59 57 49 4c 44 43 41 52 44  nName[YYWILDCARD
31e38 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ]);.          }.
31e39 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47  #endif /* NDEBUG
31e3a 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 65   */.          re
31e3b 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 6a  turn yy_action[j
31e3c 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ];.        }.   
31e3d 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 59     }.#endif /* Y
31e3e 59 57 49 4c 44 43 41 52 44 20 2a 2f 0a 20 20 20  YWILDCARD */.   
31e3f 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79   }.    return yy
31e40 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f  _default[stateno
31e41 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
31e42 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e  return yy_action
31e43 5b 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  [i];.  }.}../*.*
31e44 2a 20 46 69 6e 64 20 74 68 65 20 61 70 70 72 6f  * Find the appro
31e45 70 72 69 61 74 65 20 61 63 74 69 6f 6e 20 66 6f  priate action fo
31e46 72 20 61 20 70 61 72 73 65 72 20 67 69 76 65 6e  r a parser given
31e47 20 74 68 65 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61   the non-termina
31e48 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20  l.** look-ahead 
31e49 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61 64  token iLookAhead
31e4a 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
31e4b 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20  ook-ahead token 
31e4c 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68 65  is YYNOCODE, the
31e4d 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69  n check to see i
31e4e 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 0a  f the action is.
31e4f 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 6f  ** independent o
31e50 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61 64  f the look-ahead
31e51 2e 20 20 49 66 20 69 74 20 69 73 2c 20 72 65 74  .  If it is, ret
31e52 75 72 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c 20  urn the action, 
31e53 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72 65 74  otherwise.** ret
31e54 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e  urn YY_NO_ACTION
31e55 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31e56 79 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61  yy_find_reduce_a
31e57 63 74 69 6f 6e 28 0a 20 20 69 6e 74 20 73 74 61  ction(.  int sta
31e58 74 65 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  teno,           
31e59 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 74     /* Current st
31e5a 61 74 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ate number */.  
31e5b 59 59 43 4f 44 45 54 59 50 45 20 69 4c 6f 6f 6b  YYCODETYPE iLook
31e5c 41 68 65 61 64 20 20 20 20 20 2f 2a 20 54 68 65  Ahead     /* The
31e5d 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
31e5e 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  n */.){.  int i;
31e5f 0a 23 69 66 64 65 66 20 59 59 45 52 52 4f 52 53  .#ifdef YYERRORS
31e60 59 4d 42 4f 4c 0a 20 20 69 66 28 20 73 74 61 74  YMBOL.  if( stat
31e61 65 6e 6f 3e 59 59 5f 52 45 44 55 43 45 5f 43 4f  eno>YY_REDUCE_CO
31e62 55 4e 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72  UNT ){.    retur
31e63 6e 20 79 79 5f 64 65 66 61 75 6c 74 5b 73 74 61  n yy_default[sta
31e64 74 65 6e 6f 5d 3b 0a 20 20 7d 0a 23 65 6c 73 65  teno];.  }.#else
31e65 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 74 65  .  assert( state
31e66 6e 6f 3c 3d 59 59 5f 52 45 44 55 43 45 5f 43 4f  no<=YY_REDUCE_CO
31e67 55 4e 54 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20  UNT );.#endif.  
31e68 69 20 3d 20 79 79 5f 72 65 64 75 63 65 5f 6f 66  i = yy_reduce_of
31e69 73 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 61  st[stateno];.  a
31e6a 73 73 65 72 74 28 20 69 21 3d 59 59 5f 52 45 44  ssert( i!=YY_RED
31e6b 55 43 45 5f 55 53 45 5f 44 46 4c 54 20 29 3b 0a  UCE_USE_DFLT );.
31e6c 20 20 61 73 73 65 72 74 28 20 69 4c 6f 6f 6b 41    assert( iLookA
31e6d 68 65 61 64 21 3d 59 59 4e 4f 43 4f 44 45 20 29  head!=YYNOCODE )
31e6e 3b 0a 20 20 69 20 2b 3d 20 69 4c 6f 6f 6b 41 68  ;.  i += iLookAh
31e6f 65 61 64 3b 0a 23 69 66 64 65 66 20 59 59 45 52  ead;.#ifdef YYER
31e70 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 66 28 20  RORSYMBOL.  if( 
31e71 69 3c 30 20 7c 7c 20 69 3e 3d 59 59 5f 41 43 54  i<0 || i>=YY_ACT
31e72 54 41 42 5f 43 4f 55 4e 54 20 7c 7c 20 79 79 5f  TAB_COUNT || yy_
31e73 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d 69 4c  lookahead[i]!=iL
31e74 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20 20 20  ookAhead ){.    
31e75 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c  return yy_defaul
31e76 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 0a  t[stateno];.  }.
31e77 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74 28 20  #else.  assert( 
31e78 69 3e 3d 30 20 26 26 20 69 3c 59 59 5f 41 43 54  i>=0 && i<YY_ACT
31e79 54 41 42 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 61  TAB_COUNT );.  a
31e7a 73 73 65 72 74 28 20 79 79 5f 6c 6f 6f 6b 61 68  ssert( yy_lookah
31e7b 65 61 64 5b 69 5d 3d 3d 69 4c 6f 6f 6b 41 68 65  ead[i]==iLookAhe
31e7c 61 64 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72  ad );.#endif.  r
31e7d 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b  eturn yy_action[
31e7e 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  i];.}../*.** The
31e7f 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
31e80 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20  ne is called if 
31e81 74 68 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c  the stack overfl
31e82 6f 77 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ows..*/.static v
31e83 6f 69 64 20 79 79 53 74 61 63 6b 4f 76 65 72 66  oid yyStackOverf
31e84 6c 6f 77 28 79 79 50 61 72 73 65 72 20 2a 79 79  low(yyParser *yy
31e85 70 50 61 72 73 65 72 2c 20 59 59 4d 49 4e 4f 52  pParser, YYMINOR
31e86 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f 72 29 7b  TYPE *yypMinor){
31e87 0a 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  .   sqlite3Parse
31e88 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20 20 79  rARG_FETCH;.   y
31e89 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d  ypParser->yyidx-
31e8a 2d 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  -;.#ifndef NDEBU
31e8b 47 0a 20 20 20 69 66 28 20 79 79 54 72 61 63 65  G.   if( yyTrace
31e8c 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 66 70 72  FILE ){.     fpr
31e8d 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
31e8e 2c 22 25 73 53 74 61 63 6b 20 4f 76 65 72 66 6c  ,"%sStack Overfl
31e8f 6f 77 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72  ow!\n",yyTracePr
31e90 6f 6d 70 74 29 3b 0a 20 20 20 7d 0a 23 65 6e 64  ompt);.   }.#end
31e91 69 66 0a 20 20 20 77 68 69 6c 65 28 20 79 79 70  if.   while( yyp
31e92 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30  Parser->yyidx>=0
31e93 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72   ) yy_pop_parser
31e94 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72  _stack(yypParser
31e95 29 3b 0a 20 20 20 2f 2a 20 48 65 72 65 20 63 6f  );.   /* Here co
31e96 64 65 20 69 73 20 69 6e 73 65 72 74 65 64 20 77  de is inserted w
31e97 68 69 63 68 20 77 69 6c 6c 20 65 78 65 63 75 74  hich will execut
31e98 65 20 69 66 20 74 68 65 20 70 61 72 73 65 72 0a  e if the parser.
31e99 20 20 20 2a 2a 20 73 74 61 63 6b 20 65 76 65 72     ** stack ever
31e9a 79 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f 0a 0a  y overflows */..
31e9b 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
31e9c 45 52 28 79 79 70 4d 69 6e 6f 72 29 3b 20 2f 2a  ER(yypMinor); /*
31e9d 20 53 69 6c 65 6e 63 65 20 73 6f 6d 65 20 63 6f   Silence some co
31e9e 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20  mpiler warnings 
31e9f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f  */.  sqlite3Erro
31ea0 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70 61  rMsg(pParse, "pa
31ea1 72 73 65 72 20 73 74 61 63 6b 20 6f 76 65 72 66  rser stack overf
31ea2 6c 6f 77 22 29 3b 0a 20 20 70 50 61 72 73 65 2d  low");.  pParse-
31ea3 3e 70 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b  >parseError = 1;
31ea4 0a 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  .   sqlite3Parse
31ea5 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53  rARG_STORE; /* S
31ea6 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20  uppress warning 
31ea7 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78  about unused %ex
31ea8 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72  tra_argument var
31ea9 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72   */.}../*.** Per
31eaa 66 6f 72 6d 20 61 20 73 68 69 66 74 20 61 63 74  form a shift act
31eab 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
31eac 6f 69 64 20 79 79 5f 73 68 69 66 74 28 0a 20 20  oid yy_shift(.  
31ead 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72  yyParser *yypPar
31eae 73 65 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ser,          /*
31eaf 20 54 68 65 20 70 61 72 73 65 72 20 74 6f 20 62   The parser to b
31eb0 65 20 73 68 69 66 74 65 64 20 2a 2f 0a 20 20 69  e shifted */.  i
31eb1 6e 74 20 79 79 4e 65 77 53 74 61 74 65 2c 20 20  nt yyNewState,  
31eb2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31eb3 54 68 65 20 6e 65 77 20 73 74 61 74 65 20 74 6f  The new state to
31eb4 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 69   shift in */.  i
31eb5 6e 74 20 79 79 4d 61 6a 6f 72 2c 20 20 20 20 20  nt yyMajor,     
31eb6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31eb7 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20  The major token 
31eb8 74 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20  to shift in */. 
31eb9 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79   YYMINORTYPE *yy
31eba 70 4d 69 6e 6f 72 20 20 20 20 20 20 20 20 20 2f  pMinor         /
31ebb 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
31ebc 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 74 6f 20   minor token to 
31ebd 73 68 69 66 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20  shift in */.){. 
31ebe 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79   yyStackEntry *y
31ebf 79 74 6f 73 3b 0a 20 20 79 79 70 50 61 72 73 65  ytos;.  yypParse
31ec0 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a 23 69 66 64  r->yyidx++;.#ifd
31ec1 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41  ef YYTRACKMAXSTA
31ec2 43 4b 44 45 50 54 48 0a 20 20 69 66 28 20 79 79  CKDEPTH.  if( yy
31ec3 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 79  pParser->yyidx>y
31ec4 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d  ypParser->yyidxM
31ec5 61 78 20 29 7b 0a 20 20 20 20 79 79 70 50 61 72  ax ){.    yypPar
31ec6 73 65 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20  ser->yyidxMax = 
31ec7 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
31ec8 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66  ;.  }.#endif.#if
31ec9 20 59 59 53 54 41 43 4b 44 45 50 54 48 3e 30 20   YYSTACKDEPTH>0 
31eca 0a 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72  .  if( yypParser
31ecb 2d 3e 79 79 69 64 78 3e 3d 59 59 53 54 41 43 4b  ->yyidx>=YYSTACK
31ecc 44 45 50 54 48 20 29 7b 0a 20 20 20 20 79 79 53  DEPTH ){.    yyS
31ecd 74 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70  tackOverflow(yyp
31ece 50 61 72 73 65 72 2c 20 79 79 70 4d 69 6e 6f 72  Parser, yypMinor
31ecf 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
31ed0 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 79   }.#else.  if( y
31ed1 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e  ypParser->yyidx>
31ed2 3d 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74  =yypParser->yyst
31ed3 6b 73 7a 20 29 7b 0a 20 20 20 20 79 79 47 72 6f  ksz ){.    yyGro
31ed4 77 53 74 61 63 6b 28 79 79 70 50 61 72 73 65 72  wStack(yypParser
31ed5 29 3b 0a 20 20 20 20 69 66 28 20 79 79 70 50 61  );.    if( yypPa
31ed6 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70  rser->yyidx>=yyp
31ed7 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20  Parser->yystksz 
31ed8 29 7b 0a 20 20 20 20 20 20 79 79 53 74 61 63 6b  ){.      yyStack
31ed9 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73  Overflow(yypPars
31eda 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20  er, yypMinor);. 
31edb 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
31edc 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20   }.  }.#endif.  
31edd 79 79 74 6f 73 20 3d 20 26 79 79 70 50 61 72 73  yytos = &yypPars
31ede 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50  er->yystack[yypP
31edf 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 20  arser->yyidx];. 
31ee0 20 79 79 74 6f 73 2d 3e 73 74 61 74 65 6e 6f 20   yytos->stateno 
31ee1 3d 20 28 59 59 41 43 54 49 4f 4e 54 59 50 45 29  = (YYACTIONTYPE)
31ee2 79 79 4e 65 77 53 74 61 74 65 3b 0a 20 20 79 79  yyNewState;.  yy
31ee3 74 6f 73 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59 59  tos->major = (YY
31ee4 43 4f 44 45 54 59 50 45 29 79 79 4d 61 6a 6f 72  CODETYPE)yyMajor
31ee5 3b 0a 20 20 79 79 74 6f 73 2d 3e 6d 69 6e 6f 72  ;.  yytos->minor
31ee6 20 3d 20 2a 79 79 70 4d 69 6e 6f 72 3b 0a 23 69   = *yypMinor;.#i
31ee7 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69  fndef NDEBUG.  i
31ee8 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 26  f( yyTraceFILE &
31ee9 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69  & yypParser->yyi
31eea 64 78 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  dx>0 ){.    int 
31eeb 69 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79  i;.    fprintf(y
31eec 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53 68  yTraceFILE,"%sSh
31eed 69 66 74 20 25 64 5c 6e 22 2c 79 79 54 72 61 63  ift %d\n",yyTrac
31eee 65 50 72 6f 6d 70 74 2c 79 79 4e 65 77 53 74 61  ePrompt,yyNewSta
31eef 74 65 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66  te);.    fprintf
31ef0 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73  (yyTraceFILE,"%s
31ef1 53 74 61 63 6b 3a 22 2c 79 79 54 72 61 63 65 50  Stack:",yyTraceP
31ef2 72 6f 6d 70 74 29 3b 0a 20 20 20 20 66 6f 72 28  rompt);.    for(
31ef3 69 3d 31 3b 20 69 3c 3d 79 79 70 50 61 72 73 65  i=1; i<=yypParse
31ef4 72 2d 3e 79 79 69 64 78 3b 20 69 2b 2b 29 0a 20  r->yyidx; i++). 
31ef5 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54       fprintf(yyT
31ef6 72 61 63 65 46 49 4c 45 2c 22 20 25 73 22 2c 79  raceFILE," %s",y
31ef7 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 70 50 61  yTokenName[yypPa
31ef8 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 69 5d  rser->yystack[i]
31ef9 2e 6d 61 6a 6f 72 5d 29 3b 0a 20 20 20 20 66 70  .major]);.    fp
31efa 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
31efb 45 2c 22 5c 6e 22 29 3b 0a 20 20 7d 0a 23 65 6e  E,"\n");.  }.#en
31efc 64 69 66 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f  dif.}../* The fo
31efd 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 63 6f  llowing table co
31efe 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69  ntains informati
31eff 6f 6e 20 61 62 6f 75 74 20 65 76 65 72 79 20 72  on about every r
31f00 75 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 75  ule that.** is u
31f01 73 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 72  sed during the r
31f02 65 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  educe..*/.static
31f03 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a   const struct {.
31f04 20 20 59 59 43 4f 44 45 54 59 50 45 20 6c 68 73    YYCODETYPE lhs
31f05 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 79 6d  ;         /* Sym
31f06 62 6f 6c 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d  bol on the left-
31f07 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65  hand side of the
31f08 20 72 75 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67   rule */.  unsig
31f09 6e 65 64 20 63 68 61 72 20 6e 72 68 73 3b 20 20  ned char nrhs;  
31f0a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
31f0b 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
31f0c 73 79 6d 62 6f 6c 73 20 69 6e 20 74 68 65 20 72  symbols in the r
31f0d 75 6c 65 20 2a 2f 0a 7d 20 79 79 52 75 6c 65 49  ule */.} yyRuleI
31f0e 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 31 34  nfo[] = {.  { 14
31f0f 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c  2, 1 },.  { 143,
31f10 20 32 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 31   2 },.  { 143, 1
31f11 20 7d 2c 0a 20 20 7b 20 31 34 34 2c 20 31 20 7d   },.  { 144, 1 }
31f12 2c 0a 20 20 7b 20 31 34 34 2c 20 33 20 7d 2c 0a  ,.  { 144, 3 },.
31f13 20 20 7b 20 31 34 35 2c 20 30 20 7d 2c 0a 20 20    { 145, 0 },.  
31f14 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 145, 1 },.  { 
31f15 31 34 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34  145, 3 },.  { 14
31f16 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  6, 1 },.  { 147,
31f17 20 33 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 30   3 },.  { 149, 0
31f18 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 31 20 7d   },.  { 149, 1 }
31f19 2c 0a 20 20 7b 20 31 34 39 2c 20 32 20 7d 2c 0a  ,.  { 149, 2 },.
31f1a 20 20 7b 20 31 34 38 2c 20 30 20 7d 2c 0a 20 20    { 148, 0 },.  
31f1b 7b 20 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 148, 1 },.  { 
31f1c 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  148, 1 },.  { 14
31f1d 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  8, 1 },.  { 147,
31f1e 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32   2 },.  { 147, 2
31f1f 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d   },.  { 147, 2 }
31f20 2c 0a 20 20 7b 20 31 35 31 2c 20 31 20 7d 2c 0a  ,.  { 151, 1 },.
31f21 20 20 7b 20 31 35 31 2c 20 30 20 7d 2c 0a 20 20    { 151, 0 },.  
31f22 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 147, 2 },.  { 
31f23 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34  147, 3 },.  { 14
31f24 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 5 },.  { 147,
31f25 20 32 20 7d 2c 0a 20 20 7b 20 31 35 32 2c 20 36   2 },.  { 152, 6
31f26 20 7d 2c 0a 20 20 7b 20 31 35 34 2c 20 31 20 7d   },.  { 154, 1 }
31f27 2c 0a 20 20 7b 20 31 35 36 2c 20 30 20 7d 2c 0a  ,.  { 156, 0 },.
31f28 20 20 7b 20 31 35 36 2c 20 33 20 7d 2c 0a 20 20    { 156, 3 },.  
31f29 7b 20 31 35 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 155, 1 },.  { 
31f2a 31 35 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35  155, 0 },.  { 15
31f2b 33 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 35 33 2c  3, 4 },.  { 153,
31f2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 33   2 },.  { 158, 3
31f2d 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 31 20 7d   },.  { 158, 1 }
31f2e 2c 0a 20 20 7b 20 31 36 31 2c 20 33 20 7d 2c 0a  ,.  { 161, 3 },.
31f2f 20 20 7b 20 31 36 32 2c 20 31 20 7d 2c 0a 20 20    { 162, 1 },.  
31f30 7b 20 31 36 35 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 165, 1 },.  { 
31f31 31 36 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  165, 1 },.  { 16
31f32 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c  6, 1 },.  { 150,
31f33 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31   1 },.  { 150, 1
31f34 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d   },.  { 150, 1 }
31f35 2c 0a 20 20 7b 20 31 36 33 2c 20 30 20 7d 2c 0a  ,.  { 163, 0 },.
31f36 20 20 7b 20 31 36 33 2c 20 31 20 7d 2c 0a 20 20    { 163, 1 },.  
31f37 7b 20 31 36 37 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 167, 1 },.  { 
31f38 31 36 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36  167, 4 },.  { 16
31f39 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 36 38 2c  7, 6 },.  { 168,
31f3a 20 31 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 32   1 },.  { 168, 2
31f3b 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d   },.  { 169, 1 }
31f3c 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a  ,.  { 169, 1 },.
31f3d 20 20 7b 20 31 36 34 2c 20 32 20 7d 2c 0a 20 20    { 164, 2 },.  
31f3e 7b 20 31 36 34 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 164, 0 },.  { 
31f3f 31 37 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  172, 3 },.  { 17
31f40 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  2, 1 },.  { 173,
31f41 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34   2 },.  { 173, 4
31f42 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d   },.  { 173, 3 }
31f43 2c 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a  ,.  { 173, 3 },.
31f44 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20    { 173, 2 },.  
31f45 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 173, 2 },.  { 
31f46 31 37 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  173, 3 },.  { 17
31f47 33 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  3, 5 },.  { 173,
31f48 20 32 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34   2 },.  { 173, 4
31f49 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d   },.  { 173, 4 }
31f4a 2c 0a 20 20 7b 20 31 37 33 2c 20 31 20 7d 2c 0a  ,.  { 173, 1 },.
31f4b 20 20 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20    { 173, 2 },.  
31f4c 7b 20 31 37 38 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 178, 0 },.  { 
31f4d 31 37 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38  178, 1 },.  { 18
31f4e 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 30 2c  0, 0 },.  { 180,
31f4f 20 32 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 32   2 },.  { 182, 2
31f50 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d   },.  { 182, 3 }
31f51 2c 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a  ,.  { 182, 3 },.
31f52 20 20 7b 20 31 38 33 2c 20 32 20 7d 2c 0a 20 20    { 183, 2 },.  
31f53 7b 20 31 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 183, 2 },.  { 
31f54 31 38 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38  183, 1 },.  { 18
31f55 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 33 2c  3, 1 },.  { 183,
31f56 20 32 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 33   2 },.  { 181, 3
31f57 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 32 20 7d   },.  { 181, 2 }
31f58 2c 0a 20 20 7b 20 31 38 34 2c 20 30 20 7d 2c 0a  ,.  { 184, 0 },.
31f59 20 20 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20    { 184, 2 },.  
31f5a 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 184, 2 },.  { 
31f5b 31 35 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35  159, 0 },.  { 15
31f5c 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 35 2c  9, 2 },.  { 185,
31f5d 20 33 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 32   3 },.  { 185, 2
31f5e 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 31 20 7d   },.  { 185, 1 }
31f5f 2c 0a 20 20 7b 20 31 38 36 2c 20 32 20 7d 2c 0a  ,.  { 186, 2 },.
31f60 20 20 7b 20 31 38 36 2c 20 37 20 7d 2c 0a 20 20    { 186, 7 },.  
31f61 7b 20 31 38 36 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 186, 5 },.  { 
31f62 31 38 36 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38  186, 5 },.  { 18
31f63 36 2c 20 31 30 20 7d 2c 0a 20 20 7b 20 31 38 38  6, 10 },.  { 188
31f64 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20  , 0 },.  { 188, 
31f65 31 20 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 30 20  1 },.  { 176, 0 
31f66 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 33 20 7d 2c  },.  { 176, 3 },
31f67 0a 20 20 7b 20 31 38 39 2c 20 30 20 7d 2c 0a 20  .  { 189, 0 },. 
31f68 20 7b 20 31 38 39 2c 20 32 20 7d 2c 0a 20 20 7b   { 189, 2 },.  {
31f69 20 31 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   190, 1 },.  { 1
31f6a 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 30  90, 1 },.  { 190
31f6b 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
31f6c 34 20 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 32 20  4 },.  { 192, 2 
31f6d 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 30 20 7d 2c  },.  { 192, 0 },
31f6e 0a 20 20 7b 20 31 34 37 2c 20 38 20 7d 2c 0a 20  .  { 147, 8 },. 
31f6f 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b   { 147, 4 },.  {
31f70 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   147, 1 },.  { 1
31f71 36 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 30  60, 1 },.  { 160
31f72 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20  , 3 },.  { 195, 
31f73 31 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 32 20  1 },.  { 195, 2 
31f74 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 31 20 7d 2c  },.  { 195, 1 },
31f75 0a 20 20 7b 20 31 39 34 2c 20 39 20 7d 2c 0a 20  .  { 194, 9 },. 
31f76 20 7b 20 31 39 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 196, 1 },.  {
31f77 20 31 39 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   196, 1 },.  { 1
31f78 39 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 34  96, 0 },.  { 204
31f79 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20  , 2 },.  { 204, 
31f7a 30 20 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 33 20  0 },.  { 197, 3 
31f7b 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 32 20 7d 2c  },.  { 197, 2 },
31f7c 0a 20 20 7b 20 31 39 37 2c 20 34 20 7d 2c 0a 20  .  { 197, 4 },. 
31f7d 20 7b 20 32 30 35 2c 20 32 20 7d 2c 0a 20 20 7b   { 205, 2 },.  {
31f7e 20 32 30 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   205, 1 },.  { 2
31f7f 30 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38  05, 0 },.  { 198
31f80 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20  , 0 },.  { 198, 
31f81 32 20 7d 2c 0a 20 20 7b 20 32 30 37 2c 20 32 20  2 },.  { 207, 2 
31f82 7d 2c 0a 20 20 7b 20 32 30 37 2c 20 30 20 7d 2c  },.  { 207, 0 },
31f83 0a 20 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20  .  { 206, 7 },. 
31f84 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b   { 206, 7 },.  {
31f85 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 31   206, 7 },.  { 1
31f86 35 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 37  57, 0 },.  { 157
31f87 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 33 2c 20  , 2 },.  { 193, 
31f88 32 20 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 31 20  2 },.  { 208, 1 
31f89 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 32 20 7d 2c  },.  { 208, 2 },
31f8a 0a 20 20 7b 20 32 30 38 2c 20 33 20 7d 2c 0a 20  .  { 208, 3 },. 
31f8b 20 7b 20 32 30 38 2c 20 34 20 7d 2c 0a 20 20 7b   { 208, 4 },.  {
31f8c 20 32 31 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   210, 2 },.  { 2
31f8d 31 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 39  10, 0 },.  { 209
31f8e 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20  , 0 },.  { 209, 
31f8f 33 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 32 20  3 },.  { 209, 2 
31f90 7d 2c 0a 20 20 7b 20 32 31 31 2c 20 34 20 7d 2c  },.  { 211, 4 },
31f91 0a 20 20 7b 20 32 31 31 2c 20 30 20 7d 2c 0a 20  .  { 211, 0 },. 
31f92 20 7b 20 32 30 32 2c 20 30 20 7d 2c 0a 20 20 7b   { 202, 0 },.  {
31f93 20 32 30 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   202, 3 },.  { 2
31f94 31 34 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 31 34  14, 4 },.  { 214
31f95 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 35 2c 20  , 2 },.  { 215, 
31f96 31 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 31 20  1 },.  { 177, 1 
31f97 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 31 20 7d 2c  },.  { 177, 1 },
31f98 0a 20 20 7b 20 31 37 37 2c 20 30 20 7d 2c 0a 20  .  { 177, 0 },. 
31f99 20 7b 20 32 30 30 2c 20 30 20 7d 2c 0a 20 20 7b   { 200, 0 },.  {
31f9a 20 32 30 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   200, 3 },.  { 2
31f9b 30 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 31  01, 0 },.  { 201
31f9c 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20  , 2 },.  { 203, 
31f9d 30 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 32 20  0 },.  { 203, 2 
31f9e 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 34 20 7d 2c  },.  { 203, 4 },
31f9f 0a 20 20 7b 20 32 30 33 2c 20 34 20 7d 2c 0a 20  .  { 203, 4 },. 
31fa0 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b   { 147, 5 },.  {
31fa1 20 31 39 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   199, 0 },.  { 1
31fa2 39 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37  99, 2 },.  { 147
31fa3 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20  , 7 },.  { 217, 
31fa4 35 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20 33 20  5 },.  { 217, 3 
31fa5 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 38 20 7d 2c  },.  { 147, 8 },
31fa6 0a 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20  .  { 147, 5 },. 
31fa7 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b   { 147, 6 },.  {
31fa8 20 32 31 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   218, 2 },.  { 2
31fa9 31 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 30  18, 1 },.  { 220
31faa 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20  , 3 },.  { 220, 
31fab 31 20 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 30 20  1 },.  { 219, 0 
31fac 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 33 20 7d 2c  },.  { 219, 3 },
31fad 0a 20 20 7b 20 32 31 33 2c 20 33 20 7d 2c 0a 20  .  { 213, 3 },. 
31fae 20 7b 20 32 31 33 2c 20 31 20 7d 2c 0a 20 20 7b   { 213, 1 },.  {
31faf 20 31 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   175, 1 },.  { 1
31fb0 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 34  75, 3 },.  { 174
31fb1 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 1 },.  { 175, 
31fb2 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20  1 },.  { 175, 1 
31fb3 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
31fb4 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20  .  { 175, 5 },. 
31fb5 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 174, 1 },.  {
31fb6 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   174, 1 },.  { 1
31fb7 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 1 },.  { 175
31fb8 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 1 },.  { 175, 
31fb9 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 36 20  3 },.  { 175, 6 
31fba 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c  },.  { 175, 5 },
31fbb 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20  .  { 175, 4 },. 
31fbc 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 174, 1 },.  {
31fbd 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
31fbe 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
31fbf 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 3 },.  { 175, 
31fc0 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20  3 },.  { 175, 3 
31fc1 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
31fc2 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20  .  { 175, 3 },. 
31fc3 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
31fc4 20 32 32 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   222, 1 },.  { 2
31fc5 32 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 32  22, 2 },.  { 222
31fc6 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20  , 1 },.  { 222, 
31fc7 32 20 7d 2c 0a 20 20 7b 20 32 32 33 2c 20 32 20  2 },.  { 223, 2 
31fc8 7d 2c 0a 20 20 7b 20 32 32 33 2c 20 30 20 7d 2c  },.  { 223, 0 },
31fc9 0a 20 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20  .  { 175, 4 },. 
31fca 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b   { 175, 2 },.  {
31fcb 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   175, 3 },.  { 1
31fcc 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
31fcd 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 4 },.  { 175, 
31fce 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20  2 },.  { 175, 2 
31fcf 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c  },.  { 175, 2 },
31fd0 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20  .  { 175, 2 },. 
31fd1 20 7b 20 32 32 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 224, 1 },.  {
31fd2 20 32 32 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   224, 2 },.  { 1
31fd3 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 35  75, 5 },.  { 225
31fd4 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20  , 1 },.  { 225, 
31fd5 32 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20  2 },.  { 175, 5 
31fd6 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
31fd7 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20  .  { 175, 5 },. 
31fd8 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b   { 175, 4 },.  {
31fd9 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   175, 4 },.  { 1
31fda 37 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 37  75, 5 },.  { 227
31fdb 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20  , 5 },.  { 227, 
31fdc 34 20 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 32 20  4 },.  { 228, 2 
31fdd 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 30 20 7d 2c  },.  { 228, 0 },
31fde 0a 20 20 7b 20 32 32 36 2c 20 31 20 7d 2c 0a 20  .  { 226, 1 },. 
31fdf 20 7b 20 32 32 36 2c 20 30 20 7d 2c 0a 20 20 7b   { 226, 0 },.  {
31fe0 20 32 32 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   221, 1 },.  { 2
31fe1 32 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31 36  21, 0 },.  { 216
31fe2 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20  , 3 },.  { 216, 
31fe3 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 31  1 },.  { 147, 11
31fe4 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20 31 20 7d   },.  { 229, 1 }
31fe5 2c 0a 20 20 7b 20 32 32 39 2c 20 30 20 7d 2c 0a  ,.  { 229, 0 },.
31fe6 20 20 7b 20 31 37 39 2c 20 30 20 7d 2c 0a 20 20    { 179, 0 },.  
31fe7 7b 20 31 37 39 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 179, 3 },.  { 
31fe8 31 38 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38  187, 5 },.  { 18
31fe9 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 30 2c  7, 3 },.  { 230,
31fea 20 30 20 7d 2c 0a 20 20 7b 20 32 33 30 2c 20 32   0 },.  { 230, 2
31feb 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d   },.  { 147, 4 }
31fec 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a  ,.  { 147, 1 },.
31fed 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20    { 147, 2 },.  
31fee 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 147, 3 },.  { 
31fef 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34  147, 5 },.  { 14
31ff0 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 6 },.  { 147,
31ff1 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36   5 },.  { 147, 6
31ff2 20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d   },.  { 231, 1 }
31ff3 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a  ,.  { 231, 1 },.
31ff4 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20    { 231, 1 },.  
31ff5 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 231, 1 },.  { 
31ff6 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  231, 1 },.  { 17
31ff7 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 31 2c  0, 2 },.  { 171,
31ff8 20 32 20 7d 2c 0a 20 20 7b 20 32 33 33 2c 20 31   2 },.  { 233, 1
31ff9 20 7d 2c 0a 20 20 7b 20 32 33 32 2c 20 31 20 7d   },.  { 232, 1 }
31ffa 2c 0a 20 20 7b 20 32 33 32 2c 20 30 20 7d 2c 0a  ,.  { 232, 0 },.
31ffb 20 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20    { 147, 5 },.  
31ffc 7b 20 32 33 34 2c 20 31 31 20 7d 2c 0a 20 20 7b  { 234, 11 },.  {
31ffd 20 32 33 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   236, 1 },.  { 2
31ffe 33 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 36  36, 1 },.  { 236
31fff 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20  , 2 },.  { 236, 
32000 30 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 31 20  0 },.  { 237, 1 
32001 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 31 20 7d 2c  },.  { 237, 1 },
32002 0a 20 20 7b 20 32 33 37 2c 20 33 20 7d 2c 0a 20  .  { 237, 3 },. 
32003 20 7b 20 32 33 38 2c 20 30 20 7d 2c 0a 20 20 7b   { 238, 0 },.  {
32004 20 32 33 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   238, 3 },.  { 2
32005 33 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 39  39, 0 },.  { 239
32006 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20  , 2 },.  { 235, 
32007 33 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 32 20  3 },.  { 235, 2 
32008 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 31 20 7d 2c  },.  { 241, 1 },
32009 0a 20 20 7b 20 32 34 31 2c 20 33 20 7d 2c 0a 20  .  { 241, 3 },. 
3200a 20 7b 20 32 34 32 2c 20 30 20 7d 2c 0a 20 20 7b   { 242, 0 },.  {
3200b 20 32 34 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   242, 3 },.  { 2
3200c 34 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 34 30  42, 2 },.  { 240
3200d 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20  , 7 },.  { 240, 
3200e 38 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 35 20  8 },.  { 240, 5 
3200f 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 35 20 7d 2c  },.  { 240, 5 },
32010 0a 20 20 7b 20 32 34 30 2c 20 31 20 7d 2c 0a 20  .  { 240, 1 },. 
32011 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b   { 175, 4 },.  {
32012 20 31 37 35 2c 20 36 20 7d 2c 0a 20 20 7b 20 31   175, 6 },.  { 1
32013 39 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 31  91, 1 },.  { 191
32014 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20  , 1 },.  { 191, 
32015 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20  1 },.  { 147, 4 
32016 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c  },.  { 147, 6 },
32017 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20  .  { 147, 3 },. 
32018 20 7b 20 32 34 34 2c 20 30 20 7d 2c 0a 20 20 7b   { 244, 0 },.  {
32019 20 32 34 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   244, 2 },.  { 2
3201a 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 33  43, 1 },.  { 243
3201b 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 0 },.  { 147, 
3201c 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20  1 },.  { 147, 3 
3201d 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c  },.  { 147, 1 },
3201e 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20  .  { 147, 3 },. 
3201f 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b   { 147, 6 },.  {
32020 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32   147, 6 },.  { 2
32021 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 36  45, 1 },.  { 246
32022 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34 36 2c 20  , 0 },.  { 246, 
32023 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20  1 },.  { 147, 1 
32024 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c  },.  { 147, 4 },
32025 0a 20 20 7b 20 32 34 37 2c 20 37 20 7d 2c 0a 20  .  { 247, 7 },. 
32026 20 7b 20 32 34 38 2c 20 31 20 7d 2c 0a 20 20 7b   { 248, 1 },.  {
32027 20 32 34 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   248, 3 },.  { 2
32028 34 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34 39  49, 0 },.  { 249
32029 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 35 30 2c 20  , 2 },.  { 250, 
3202a 31 20 7d 2c 0a 20 20 7b 20 32 35 30 2c 20 33 20  1 },.  { 250, 3 
3202b 7d 2c 0a 20 20 7b 20 32 35 31 2c 20 31 20 7d 2c  },.  { 251, 1 },
3202c 0a 20 20 7b 20 32 35 32 2c 20 30 20 7d 2c 0a 20  .  { 252, 0 },. 
3202d 20 7b 20 32 35 32 2c 20 34 20 7d 2c 0a 20 20 7b   { 252, 4 },.  {
3202e 20 32 35 32 2c 20 32 20 7d 2c 0a 7d 3b 0a 0a 73   252, 2 },.};..s
3202f 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 61 63  tatic void yy_ac
32030 63 65 70 74 28 79 79 50 61 72 73 65 72 2a 29 3b  cept(yyParser*);
32031 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 44 65 63    /* Forward Dec
32032 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a  laration */../*.
32033 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 65 64  ** Perform a red
32034 75 63 65 20 61 63 74 69 6f 6e 20 61 6e 64 20 74  uce action and t
32035 68 65 20 73 68 69 66 74 20 74 68 61 74 20 6d 75  he shift that mu
32036 73 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a  st immediately.*
32037 2a 20 66 6f 6c 6c 6f 77 20 74 68 65 20 72 65 64  * follow the red
32038 75 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  uce..*/.static v
32039 6f 69 64 20 79 79 5f 72 65 64 75 63 65 28 0a 20  oid yy_reduce(. 
3203a 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50 61   yyParser *yypPa
3203b 72 73 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  rser,         /*
3203c 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20   The parser */. 
3203d 20 69 6e 74 20 79 79 72 75 6c 65 6e 6f 20 20 20   int yyruleno   
3203e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3203f 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72   Number of the r
32040 75 6c 65 20 62 79 20 77 68 69 63 68 20 74 6f 20  ule by which to 
32041 72 65 64 75 63 65 20 2a 2f 0a 29 7b 0a 20 20 69  reduce */.){.  i
32042 6e 74 20 79 79 67 6f 74 6f 3b 20 20 20 20 20 20  nt yygoto;      
32043 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32044 2a 20 54 68 65 20 6e 65 78 74 20 73 74 61 74 65  * The next state
32045 20 2a 2f 0a 20 20 69 6e 74 20 79 79 61 63 74 3b   */.  int yyact;
32046 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32047 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78        /* The nex
32048 74 20 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 59 59  t action */.  YY
32049 4d 49 4e 4f 52 54 59 50 45 20 79 79 67 6f 74 6f  MINORTYPE yygoto
3204a 6d 69 6e 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a  minor;        /*
3204b 20 54 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20   The LHS of the 
3204c 72 75 6c 65 20 72 65 64 75 63 65 64 20 2a 2f 0a  rule reduced */.
3204d 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a    yyStackEntry *
3204e 79 79 6d 73 70 3b 20 20 20 20 20 20 20 20 20 20  yymsp;          
3204f 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20    /* The top of 
32050 74 68 65 20 70 61 72 73 65 72 27 73 20 73 74 61  the parser's sta
32051 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 79 79 73 69  ck */.  int yysi
32052 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
32053 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e          /* Amoun
32054 74 20 74 6f 20 70 6f 70 20 74 68 65 20 73 74 61  t to pop the sta
32055 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50  ck */.  sqlite3P
32056 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a  arserARG_FETCH;.
32057 20 20 79 79 6d 73 70 20 3d 20 26 79 79 70 50 61    yymsp = &yypPa
32058 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79  rser->yystack[yy
32059 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b  pParser->yyidx];
3205a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
3205b 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
3205c 45 20 26 26 20 79 79 72 75 6c 65 6e 6f 3e 3d 30  E && yyruleno>=0
3205d 20 0a 20 20 20 20 20 20 20 20 26 26 20 79 79 72   .        && yyr
3205e 75 6c 65 6e 6f 3c 28 69 6e 74 29 28 73 69 7a 65  uleno<(int)(size
3205f 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65 29 2f 73  of(yyRuleName)/s
32060 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65  izeof(yyRuleName
32061 5b 30 5d 29 29 20 29 7b 0a 20 20 20 20 66 70 72  [0])) ){.    fpr
32062 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
32063 2c 20 22 25 73 52 65 64 75 63 65 20 5b 25 73 5d  , "%sReduce [%s]
32064 2e 5c 6e 22 2c 20 79 79 54 72 61 63 65 50 72 6f  .\n", yyTracePro
32065 6d 70 74 2c 0a 20 20 20 20 20 20 79 79 52 75 6c  mpt,.      yyRul
32066 65 4e 61 6d 65 5b 79 79 72 75 6c 65 6e 6f 5d 29  eName[yyruleno])
32067 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
32068 4e 44 45 42 55 47 20 2a 2f 0a 0a 20 20 2f 2a 20  NDEBUG */..  /* 
32069 53 69 6c 65 6e 63 65 20 63 6f 6d 70 6c 61 69 6e  Silence complain
3206a 74 73 20 66 72 6f 6d 20 70 75 72 69 66 79 20 61  ts from purify a
3206b 62 6f 75 74 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  bout yygotominor
3206c 20 62 65 69 6e 67 20 75 6e 69 6e 69 74 69 61 6c   being uninitial
3206d 69 7a 65 64 0a 20 20 2a 2a 20 69 6e 20 73 6f 6d  ized.  ** in som
3206e 65 20 63 61 73 65 73 20 77 68 65 6e 20 69 74 20  e cases when it 
3206f 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74  is copied into t
32070 68 65 20 73 74 61 63 6b 20 61 66 74 65 72 20 74  he stack after t
32071 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a  he following.  *
32072 2a 20 73 77 69 74 63 68 2e 20 20 79 79 67 6f 74  * switch.  yygot
32073 6f 6d 69 6e 6f 72 20 69 73 20 75 6e 69 6e 69 74  ominor is uninit
32074 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 61 20 72  ialized when a r
32075 75 6c 65 20 72 65 64 75 63 65 73 20 74 68 61 74  ule reduces that
32076 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 73   does.  ** not s
32077 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  et the value of 
32078 69 74 73 20 6c 65 66 74 2d 68 61 6e 64 20 73 69  its left-hand si
32079 64 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 20  de nonterminal. 
3207a 20 4c 65 61 76 69 6e 67 20 74 68 65 0a 20 20 2a   Leaving the.  *
3207b 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e  * value of the n
3207c 6f 6e 74 65 72 6d 69 6e 61 6c 20 75 6e 69 6e 69  onterminal unini
3207d 74 69 61 6c 69 7a 65 64 20 69 73 20 75 74 74 65  tialized is utte
3207e 72 6c 79 20 68 61 72 6d 6c 65 73 73 20 61 73 20  rly harmless as 
3207f 6c 6f 6e 67 0a 20 20 2a 2a 20 61 73 20 74 68 65  long.  ** as the
32080 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20   value is never 
32081 75 73 65 64 2e 20 20 53 6f 20 72 65 61 6c 6c 79  used.  So really
32082 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   the only thing 
32083 74 68 69 73 20 63 6f 64 65 0a 20 20 2a 2a 20 61  this code.  ** a
32084 63 63 6f 6d 70 6c 69 73 68 65 73 20 69 73 20 74  ccomplishes is t
32085 6f 20 71 75 69 65 74 65 6e 20 70 75 72 69 66 79  o quieten purify
32086 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30  .  .  **.  ** 20
32087 30 37 2d 30 31 2d 31 36 3a 20 20 54 68 65 20 77  07-01-16:  The w
32088 69 72 65 73 68 61 72 6b 20 70 72 6f 6a 65 63 74  ireshark project
32089 20 28 77 77 77 2e 77 69 72 65 73 68 61 72 6b 2e   (www.wireshark.
3208a 6f 72 67 29 20 72 65 70 6f 72 74 73 20 74 68 61  org) reports tha
3208b 74 0a 20 20 2a 2a 20 77 69 74 68 6f 75 74 20 74  t.  ** without t
3208c 68 69 73 20 63 6f 64 65 2c 20 74 68 65 69 72 20  his code, their 
3208d 70 61 72 73 65 72 20 73 65 67 66 61 75 6c 74 73  parser segfaults
3208e 2e 20 20 49 27 6d 20 6e 6f 74 20 73 75 72 65 20  .  I'm not sure 
3208f 77 68 61 74 20 74 68 65 72 65 0a 20 20 2a 2a 20  what there.  ** 
32090 70 61 72 73 65 72 20 69 73 20 64 6f 69 6e 67 20  parser is doing 
32091 74 6f 20 6d 61 6b 65 20 74 68 69 73 20 68 61 70  to make this hap
32092 70 65 6e 2e 20 20 54 68 69 73 20 69 73 20 74 68  pen.  This is th
32093 65 20 73 65 63 6f 6e 64 20 62 75 67 20 72 65 70  e second bug rep
32094 6f 72 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 77 69  ort.  ** from wi
32095 72 65 73 68 61 72 6b 20 74 68 69 73 20 77 65 65  reshark this wee
32096 6b 2e 20 20 43 6c 65 61 72 6c 79 20 74 68 65 79  k.  Clearly they
32097 20 61 72 65 20 73 74 72 65 73 73 69 6e 67 20 4c   are stressing L
32098 65 6d 6f 6e 20 69 6e 20 77 61 79 73 0a 20 20 2a  emon in ways.  *
32099 2a 20 74 68 61 74 20 69 74 20 68 61 73 20 6e 6f  * that it has no
3209a 74 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c  t been previousl
3209b 79 20 73 74 72 65 73 73 65 64 2e 2e 2e 20 20 28  y stressed...  (
3209c 53 51 4c 69 74 65 20 74 69 63 6b 65 74 20 23 32  SQLite ticket #2
3209d 31 37 32 29 0a 20 20 2a 2f 0a 20 20 2f 2a 6d 65  172).  */.  /*me
3209e 6d 73 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  mset(&yygotomino
3209f 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 79 79 67  r, 0, sizeof(yyg
320a0 6f 74 6f 6d 69 6e 6f 72 29 29 3b 2a 2f 0a 20 20  otominor));*/.  
320a1 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 3d 20 79 79  yygotominor = yy
320a2 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 0a 0a 20 20 73  zerominor;...  s
320a3 77 69 74 63 68 28 20 79 79 72 75 6c 65 6e 6f 20  witch( yyruleno 
320a4 29 7b 0a 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e  ){.  /* Beginnin
320a5 67 20 68 65 72 65 20 61 72 65 20 74 68 65 20 72  g here are the r
320a6 65 64 75 63 74 69 6f 6e 20 63 61 73 65 73 2e 20  eduction cases. 
320a7 20 41 20 74 79 70 69 63 61 6c 20 65 78 61 6d 70   A typical examp
320a8 6c 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 73 3a  le.  ** follows:
320a9 0a 20 20 2a 2a 20 20 20 63 61 73 65 20 30 3a 0a  .  **   case 0:.
320aa 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c 69 6e    **  #line <lin
320ab 65 6e 6f 3e 20 3c 67 72 61 6d 6d 61 72 66 69 6c  eno> <grammarfil
320ac 65 3e 0a 20 20 2a 2a 20 20 20 20 20 7b 20 2e 2e  e>.  **     { ..
320ad 2e 20 7d 20 20 20 20 20 20 20 20 20 20 20 2f 2f  . }           //
320ae 20 55 73 65 72 20 73 75 70 70 6c 69 65 64 20 63   User supplied c
320af 6f 64 65 0a 20 20 2a 2a 20 20 23 6c 69 6e 65 20  ode.  **  #line 
320b0 3c 6c 69 6e 65 6e 6f 3e 20 3c 74 68 69 73 66 69  <lineno> <thisfi
320b1 6c 65 3e 0a 20 20 2a 2a 20 20 20 20 20 62 72 65  le>.  **     bre
320b2 61 6b 3b 0a 20 20 2a 2f 0a 20 20 20 20 20 20 63  ak;.  */.      c
320b3 61 73 65 20 35 3a 20 2f 2a 20 65 78 70 6c 61 69  ase 5: /* explai
320b4 6e 20 3a 3a 3d 20 2a 2f 0a 7b 20 73 71 6c 69 74  n ::= */.{ sqlit
320b5 65 33 42 65 67 69 6e 50 61 72 73 65 28 70 50 61  e3BeginParse(pPa
320b6 72 73 65 2c 20 30 29 3b 20 7d 0a 20 20 20 20 20  rse, 0); }.     
320b7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
320b8 63 61 73 65 20 36 3a 20 2f 2a 20 65 78 70 6c 61  case 6: /* expla
320b9 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 2a  in ::= EXPLAIN *
320ba 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e  /.{ sqlite3Begin
320bb 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 31 29  Parse(pParse, 1)
320bc 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
320bd 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 3a  k;.      case 7:
320be 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20   /* explain ::= 
320bf 45 58 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c  EXPLAIN QUERY PL
320c0 41 4e 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42  AN */.{ sqlite3B
320c1 65 67 69 6e 50 61 72 73 65 28 70 50 61 72 73 65  eginParse(pParse
320c2 2c 20 32 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  , 2); }.        
320c3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
320c4 65 20 38 3a 20 2f 2a 20 63 6d 64 78 20 3a 3a 3d  e 8: /* cmdx ::=
320c5 20 63 6d 64 20 2a 2f 0a 7b 20 73 71 6c 69 74 65   cmd */.{ sqlite
320c6 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 70 50  3FinishCoding(pP
320c7 61 72 73 65 29 3b 20 7d 0a 20 20 20 20 20 20 20  arse); }.       
320c8 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
320c9 73 65 20 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d  se 9: /* cmd ::=
320ca 20 42 45 47 49 4e 20 74 72 61 6e 73 74 79 70 65   BEGIN transtype
320cb 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 7b 73   trans_opt */.{s
320cc 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73  qlite3BeginTrans
320cd 61 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 79  action(pParse, y
320ce 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
320cf 79 33 32 38 29 3b 7d 0a 20 20 20 20 20 20 20 20  y328);}.        
320d0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
320d1 65 20 31 33 3a 20 2f 2a 20 74 72 61 6e 73 74 79  e 13: /* transty
320d2 70 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  pe ::= */.{yygot
320d3 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54  ominor.yy328 = T
320d4 4b 5f 44 45 46 45 52 52 45 44 3b 7d 0a 20 20 20  K_DEFERRED;}.   
320d5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
320d6 20 20 63 61 73 65 20 31 34 3a 20 2f 2a 20 74 72    case 14: /* tr
320d7 61 6e 73 74 79 70 65 20 3a 3a 3d 20 44 45 46 45  anstype ::= DEFE
320d8 52 52 45 44 20 2a 2f 0a 20 20 20 20 20 20 63 61  RRED */.      ca
320d9 73 65 20 31 35 3a 20 2f 2a 20 74 72 61 6e 73 74  se 15: /* transt
320da 79 70 65 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54  ype ::= IMMEDIAT
320db 45 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  E */ yytestcase(
320dc 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 29 3b 0a 20  yyruleno==15);. 
320dd 20 20 20 20 20 63 61 73 65 20 31 36 3a 20 2f 2a       case 16: /*
320de 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45   transtype ::= E
320df 58 43 4c 55 53 49 56 45 20 2a 2f 20 79 79 74 65  XCLUSIVE */ yyte
320e0 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
320e1 3d 31 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65  =16);.      case
320e2 20 31 31 34 3a 20 2f 2a 20 6d 75 6c 74 69 73 65   114: /* multise
320e3 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f  lect_op ::= UNIO
320e4 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  N */ yytestcase(
320e5 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 34 29 3b 0a  yyruleno==114);.
320e6 20 20 20 20 20 20 63 61 73 65 20 31 31 36 3a 20        case 116: 
320e7 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f  /* multiselect_o
320e8 70 20 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54  p ::= EXCEPT|INT
320e9 45 52 53 45 43 54 20 2a 2f 20 79 79 74 65 73 74  ERSECT */ yytest
320ea 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
320eb 31 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  16);.{yygotomino
320ec 72 2e 79 79 33 32 38 20 3d 20 79 79 6d 73 70 5b  r.yy328 = yymsp[
320ed 30 5d 2e 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20 20  0].major;}.     
320ee 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
320ef 63 61 73 65 20 31 37 3a 20 2f 2a 20 63 6d 64 20  case 17: /* cmd 
320f0 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73  ::= COMMIT trans
320f1 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63 61  _opt */.      ca
320f2 73 65 20 31 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 18: /* cmd ::
320f3 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74 20  = END trans_opt 
320f4 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
320f5 72 75 6c 65 6e 6f 3d 3d 31 38 29 3b 0a 7b 73 71  ruleno==18);.{sq
320f6 6c 69 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73  lite3CommitTrans
320f7 61 63 74 69 6f 6e 28 70 50 61 72 73 65 29 3b 7d  action(pParse);}
320f8 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
320f9 20 20 20 20 20 20 63 61 73 65 20 31 39 3a 20 2f        case 19: /
320fa 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41  * cmd ::= ROLLBA
320fb 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a  CK trans_opt */.
320fc 7b 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b  {sqlite3Rollback
320fd 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72  Transaction(pPar
320fe 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  se);}.        br
320ff 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32100 32 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 53  22: /* cmd ::= S
32101 41 56 45 50 4f 49 4e 54 20 6e 6d 20 2a 2f 0a 7b  AVEPOINT nm */.{
32102 0a 20 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f  .  sqlite3Savepo
32103 69 6e 74 28 70 50 61 72 73 65 2c 20 53 41 56 45  int(pParse, SAVE
32104 50 4f 49 4e 54 5f 42 45 47 49 4e 2c 20 26 79 79  POINT_BEGIN, &yy
32105 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32106 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
32107 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
32108 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45  3: /* cmd ::= RE
32109 4c 45 41 53 45 20 73 61 76 65 70 6f 69 6e 74 5f  LEASE savepoint_
3210a 6f 70 74 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71  opt nm */.{.  sq
3210b 6c 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 70  lite3Savepoint(p
3210c 50 61 72 73 65 2c 20 53 41 56 45 50 4f 49 4e 54  Parse, SAVEPOINT
3210d 5f 52 45 4c 45 41 53 45 2c 20 26 79 79 6d 73 70  _RELEASE, &yymsp
3210e 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
3210f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32110 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 3a 20  .      case 24: 
32111 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42  /* cmd ::= ROLLB
32112 41 43 4b 20 74 72 61 6e 73 5f 6f 70 74 20 54 4f  ACK trans_opt TO
32113 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e   savepoint_opt n
32114 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  m */.{.  sqlite3
32115 53 61 76 65 70 6f 69 6e 74 28 70 50 61 72 73 65  Savepoint(pParse
32116 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  , SAVEPOINT_ROLL
32117 42 41 43 4b 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  BACK, &yymsp[0].
32118 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
32119 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3211a 20 20 20 63 61 73 65 20 32 36 3a 20 2f 2a 20 63     case 26: /* c
3211b 72 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20  reate_table ::= 
3211c 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20 54 41  createkw temp TA
3211d 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74 73 20  BLE ifnotexists 
3211e 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20  nm dbnm */.{.   
3211f 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c  sqlite3StartTabl
32120 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  e(pParse,&yymsp[
32121 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -1].minor.yy0,&y
32122 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32123 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  0,yymsp[-4].mino
32124 72 2e 79 79 33 32 38 2c 30 2c 30 2c 79 79 6d 73  r.yy328,0,0,yyms
32125 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-2].minor.yy32
32126 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  8);.}.        br
32127 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32128 32 37 3a 20 2f 2a 20 63 72 65 61 74 65 6b 77 20  27: /* createkw 
32129 3a 3a 3d 20 43 52 45 41 54 45 20 2a 2f 0a 7b 0a  ::= CREATE */.{.
3212a 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6c 6f    pParse->db->lo
3212b 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
3212c 20 3d 20 30 3b 0a 20 20 79 79 67 6f 74 6f 6d 69   = 0;.  yygotomi
3212d 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b  nor.yy0 = yymsp[
3212e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a  0].minor.yy0;.}.
3212f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32130 20 20 20 20 20 63 61 73 65 20 32 38 3a 20 2f 2a       case 28: /*
32131 20 69 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d   ifnotexists ::=
32132 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
32133 31 3a 20 2f 2a 20 74 65 6d 70 20 3a 3a 3d 20 2a  1: /* temp ::= *
32134 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32135 75 6c 65 6e 6f 3d 3d 33 31 29 3b 0a 20 20 20 20  uleno==31);.    
32136 20 20 63 61 73 65 20 37 30 3a 20 2f 2a 20 61 75    case 70: /* au
32137 74 6f 69 6e 63 20 3a 3a 3d 20 2a 2f 20 79 79 74  toinc ::= */ yyt
32138 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32139 3d 3d 37 30 29 3b 0a 20 20 20 20 20 20 63 61 73  ==70);.      cas
3213a 65 20 38 32 3a 20 2f 2a 20 64 65 66 65 72 5f 73  e 82: /* defer_s
3213b 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54  ubclause ::= NOT
3213c 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74   DEFERRABLE init
3213d 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
3213e 70 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pt */ yytestcase
3213f 28 79 79 72 75 6c 65 6e 6f 3d 3d 38 32 29 3b 0a  (yyruleno==82);.
32140 20 20 20 20 20 20 63 61 73 65 20 38 34 3a 20 2f        case 84: /
32141 2a 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f  * init_deferred_
32142 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  pred_opt ::= */ 
32143 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32144 65 6e 6f 3d 3d 38 34 29 3b 0a 20 20 20 20 20 20  eno==84);.      
32145 63 61 73 65 20 38 36 3a 20 2f 2a 20 69 6e 69 74  case 86: /* init
32146 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
32147 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59  pt ::= INITIALLY
32148 20 49 4d 4d 45 44 49 41 54 45 20 2a 2f 20 79 79   IMMEDIATE */ yy
32149 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3214a 6f 3d 3d 38 36 29 3b 0a 20 20 20 20 20 20 63 61  o==86);.      ca
3214b 73 65 20 39 37 3a 20 2f 2a 20 64 65 66 65 72 5f  se 97: /* defer_
3214c 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a  subclause_opt ::
3214d 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
3214e 79 79 72 75 6c 65 6e 6f 3d 3d 39 37 29 3b 0a 20  yyruleno==97);. 
3214f 20 20 20 20 20 63 61 73 65 20 31 30 38 3a 20 2f       case 108: /
32150 2a 20 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 2a  * ifexists ::= *
32151 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32152 75 6c 65 6e 6f 3d 3d 31 30 38 29 3b 0a 20 20 20  uleno==108);.   
32153 20 20 20 63 61 73 65 20 31 31 39 3a 20 2f 2a 20     case 119: /* 
32154 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c  distinct ::= ALL
32155 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32156 79 72 75 6c 65 6e 6f 3d 3d 31 31 39 29 3b 0a 20  yruleno==119);. 
32157 20 20 20 20 20 63 61 73 65 20 31 32 30 3a 20 2f       case 120: /
32158 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 2a  * distinct ::= *
32159 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3215a 75 6c 65 6e 6f 3d 3d 31 32 30 29 3b 0a 20 20 20  uleno==120);.   
3215b 20 20 20 63 61 73 65 20 32 32 32 3a 20 2f 2a 20     case 222: /* 
3215c 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42  between_op ::= B
3215d 45 54 57 45 45 4e 20 2a 2f 20 79 79 74 65 73 74  ETWEEN */ yytest
3215e 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
3215f 32 32 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  22);.      case 
32160 32 32 35 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a  225: /* in_op ::
32161 3d 20 49 4e 20 2a 2f 20 79 79 74 65 73 74 63 61  = IN */ yytestca
32162 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 35  se(yyruleno==225
32163 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  );.{yygotominor.
32164 79 79 33 32 38 20 3d 20 30 3b 7d 0a 20 20 20 20  yy328 = 0;}.    
32165 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32166 20 63 61 73 65 20 32 39 3a 20 2f 2a 20 69 66 6e   case 29: /* ifn
32167 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20  otexists ::= IF 
32168 4e 4f 54 20 45 58 49 53 54 53 20 2a 2f 0a 20 20  NOT EXISTS */.  
32169 20 20 20 20 63 61 73 65 20 33 30 3a 20 2f 2a 20      case 30: /* 
3216a 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 20 2a 2f  temp ::= TEMP */
3216b 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3216c 6c 65 6e 6f 3d 3d 33 30 29 3b 0a 20 20 20 20 20  leno==30);.     
3216d 20 63 61 73 65 20 37 31 3a 20 2f 2a 20 61 75 74   case 71: /* aut
3216e 6f 69 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43  oinc ::= AUTOINC
3216f 52 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  R */ yytestcase(
32170 79 79 72 75 6c 65 6e 6f 3d 3d 37 31 29 3b 0a 20  yyruleno==71);. 
32171 20 20 20 20 20 63 61 73 65 20 38 35 3a 20 2f 2a       case 85: /*
32172 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70   init_deferred_p
32173 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54  red_opt ::= INIT
32174 49 41 4c 4c 59 20 44 45 46 45 52 52 45 44 20 2a  IALLY DEFERRED *
32175 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32176 75 6c 65 6e 6f 3d 3d 38 35 29 3b 0a 20 20 20 20  uleno==85);.    
32177 20 20 63 61 73 65 20 31 30 37 3a 20 2f 2a 20 69    case 107: /* i
32178 66 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 45  fexists ::= IF E
32179 58 49 53 54 53 20 2a 2f 20 79 79 74 65 73 74 63  XISTS */ yytestc
3217a 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30  ase(yyruleno==10
3217b 37 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  7);.      case 1
3217c 31 38 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20  18: /* distinct 
3217d 3a 3a 3d 20 44 49 53 54 49 4e 43 54 20 2a 2f 20  ::= DISTINCT */ 
3217e 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3217f 65 6e 6f 3d 3d 31 31 38 29 3b 0a 20 20 20 20 20  eno==118);.     
32180 20 63 61 73 65 20 32 32 33 3a 20 2f 2a 20 62 65   case 223: /* be
32181 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54  tween_op ::= NOT
32182 20 42 45 54 57 45 45 4e 20 2a 2f 20 79 79 74 65   BETWEEN */ yyte
32183 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32184 3d 32 32 33 29 3b 0a 20 20 20 20 20 20 63 61 73  =223);.      cas
32185 65 20 32 32 36 3a 20 2f 2a 20 69 6e 5f 6f 70 20  e 226: /* in_op 
32186 3a 3a 3d 20 4e 4f 54 20 49 4e 20 2a 2f 20 79 79  ::= NOT IN */ yy
32187 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32188 6f 3d 3d 32 32 36 29 3b 0a 7b 79 79 67 6f 74 6f  o==226);.{yygoto
32189 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 31 3b  minor.yy328 = 1;
3218a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3218b 0a 20 20 20 20 20 20 63 61 73 65 20 33 32 3a 20  .      case 32: 
3218c 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  /* create_table_
3218d 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75  args ::= LP colu
3218e 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f  mnlist conslist_
3218f 6f 70 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71  opt RP */.{.  sq
32190 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50  lite3EndTable(pP
32191 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  arse,&yymsp[-1].
32192 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
32193 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29  [0].minor.yy0,0)
32194 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
32195 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 33  k;.      case 33
32196 3a 20 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c  : /* create_tabl
32197 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53 20 73 65  e_args ::= AS se
32198 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  lect */.{.  sqli
32199 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72  te3EndTable(pPar
3219a 73 65 2c 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e  se,0,0,yymsp[0].
3219b 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20 20 73 71  minor.yy3);.  sq
3219c 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
3219d 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79  e(pParse->db, yy
3219e 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
3219f 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
321a0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
321a1 36 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 20 3a 3a 3d  6: /* column ::=
321a2 20 63 6f 6c 75 6d 6e 69 64 20 74 79 70 65 20 63   columnid type c
321a3 61 72 67 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20 79  arglist */.{.  y
321a4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a  ygotominor.yy0.z
321a5 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e   = yymsp[-2].min
321a6 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f  or.yy0.z;.  yygo
321a7 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20  tominor.yy0.n = 
321a8 28 69 6e 74 29 28 70 50 61 72 73 65 2d 3e 73 4c  (int)(pParse->sL
321a9 61 73 74 54 6f 6b 65 6e 2e 7a 2d 79 79 6d 73 70  astToken.z-yymsp
321aa 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-2].minor.yy0.z
321ab 29 20 2b 20 70 50 61 72 73 65 2d 3e 73 4c 61 73  ) + pParse->sLas
321ac 74 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a 20 20 20 20  tToken.n;.}.    
321ad 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
321ae 20 63 61 73 65 20 33 37 3a 20 2f 2a 20 63 6f 6c   case 37: /* col
321af 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a  umnid ::= nm */.
321b0 7b 0a 20 20 73 71 6c 69 74 65 33 41 64 64 43 6f  {.  sqlite3AddCo
321b1 6c 75 6d 6e 28 70 50 61 72 73 65 2c 26 79 79 6d  lumn(pParse,&yym
321b2 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
321b3 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
321b4 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  yy0 = yymsp[0].m
321b5 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a 20 20 20 20  inor.yy0;.}.    
321b6 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
321b7 20 63 61 73 65 20 33 38 3a 20 2f 2a 20 69 64 20   case 38: /* id 
321b8 3a 3a 3d 20 49 44 20 2a 2f 0a 20 20 20 20 20 20  ::= ID */.      
321b9 63 61 73 65 20 33 39 3a 20 2f 2a 20 69 64 20 3a  case 39: /* id :
321ba 3a 3d 20 49 4e 44 45 58 45 44 20 2a 2f 20 79 79  := INDEXED */ yy
321bb 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
321bc 6f 3d 3d 33 39 29 3b 0a 20 20 20 20 20 20 63 61  o==39);.      ca
321bd 73 65 20 34 30 3a 20 2f 2a 20 69 64 73 20 3a 3a  se 40: /* ids ::
321be 3d 20 49 44 7c 53 54 52 49 4e 47 20 2a 2f 20 79  = ID|STRING */ y
321bf 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
321c0 6e 6f 3d 3d 34 30 29 3b 0a 20 20 20 20 20 20 63  no==40);.      c
321c1 61 73 65 20 34 31 3a 20 2f 2a 20 6e 6d 20 3a 3a  ase 41: /* nm ::
321c2 3d 20 69 64 20 2a 2f 20 79 79 74 65 73 74 63 61  = id */ yytestca
321c3 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 31 29  se(yyruleno==41)
321c4 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 32 3a  ;.      case 42:
321c5 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 53 54 52 49 4e   /* nm ::= STRIN
321c6 47 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  G */ yytestcase(
321c7 79 79 72 75 6c 65 6e 6f 3d 3d 34 32 29 3b 0a 20  yyruleno==42);. 
321c8 20 20 20 20 20 63 61 73 65 20 34 33 3a 20 2f 2a       case 43: /*
321c9 20 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20   nm ::= JOIN_KW 
321ca 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
321cb 72 75 6c 65 6e 6f 3d 3d 34 33 29 3b 0a 20 20 20  ruleno==43);.   
321cc 20 20 20 63 61 73 65 20 34 36 3a 20 2f 2a 20 74     case 46: /* t
321cd 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70  ypetoken ::= typ
321ce 65 6e 61 6d 65 20 2a 2f 20 79 79 74 65 73 74 63  ename */ yytestc
321cf 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 36  ase(yyruleno==46
321d0 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 39  );.      case 49
321d1 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a  : /* typename ::
321d2 3d 20 69 64 73 20 2a 2f 20 79 79 74 65 73 74 63  = ids */ yytestc
321d3 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 39  ase(yyruleno==49
321d4 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32  );.      case 12
321d5 36 3a 20 2f 2a 20 61 73 20 3a 3a 3d 20 41 53 20  6: /* as ::= AS 
321d6 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  nm */ yytestcase
321d7 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 36 29 3b  (yyruleno==126);
321d8 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 37 3a  .      case 127:
321d9 20 2f 2a 20 61 73 20 3a 3a 3d 20 69 64 73 20 2a   /* as ::= ids *
321da 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
321db 75 6c 65 6e 6f 3d 3d 31 32 37 29 3b 0a 20 20 20  uleno==127);.   
321dc 20 20 20 63 61 73 65 20 31 33 37 3a 20 2f 2a 20     case 137: /* 
321dd 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 20  dbnm ::= DOT nm 
321de 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
321df 72 75 6c 65 6e 6f 3d 3d 31 33 37 29 3b 0a 20 20  ruleno==137);.  
321e0 20 20 20 20 63 61 73 65 20 31 34 36 3a 20 2f 2a      case 146: /*
321e1 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d   indexed_opt ::=
321e2 20 49 4e 44 45 58 45 44 20 42 59 20 6e 6d 20 2a   INDEXED BY nm *
321e3 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
321e4 75 6c 65 6e 6f 3d 3d 31 34 36 29 3b 0a 20 20 20  uleno==146);.   
321e5 20 20 20 63 61 73 65 20 32 35 31 3a 20 2f 2a 20     case 251: /* 
321e6 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c  collate ::= COLL
321e7 41 54 45 20 69 64 73 20 2a 2f 20 79 79 74 65 73  ATE ids */ yytes
321e8 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
321e9 32 35 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65  251);.      case
321ea 20 32 36 30 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a   260: /* nmnum :
321eb 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f 20 79  := plus_num */ y
321ec 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
321ed 6e 6f 3d 3d 32 36 30 29 3b 0a 20 20 20 20 20 20  no==260);.      
321ee 63 61 73 65 20 32 36 31 3a 20 2f 2a 20 6e 6d 6e  case 261: /* nmn
321ef 75 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f 20 79 79 74  um ::= nm */ yyt
321f0 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
321f1 3d 3d 32 36 31 29 3b 0a 20 20 20 20 20 20 63 61  ==261);.      ca
321f2 73 65 20 32 36 32 3a 20 2f 2a 20 6e 6d 6e 75 6d  se 262: /* nmnum
321f3 20 3a 3a 3d 20 4f 4e 20 2a 2f 20 79 79 74 65 73   ::= ON */ yytes
321f4 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
321f5 32 36 32 29 3b 0a 20 20 20 20 20 20 63 61 73 65  262);.      case
321f6 20 32 36 33 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a   263: /* nmnum :
321f7 3a 3d 20 44 45 4c 45 54 45 20 2a 2f 20 79 79 74  := DELETE */ yyt
321f8 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
321f9 3d 3d 32 36 33 29 3b 0a 20 20 20 20 20 20 63 61  ==263);.      ca
321fa 73 65 20 32 36 34 3a 20 2f 2a 20 6e 6d 6e 75 6d  se 264: /* nmnum
321fb 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 2a 2f 20   ::= DEFAULT */ 
321fc 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
321fd 65 6e 6f 3d 3d 32 36 34 29 3b 0a 20 20 20 20 20  eno==264);.     
321fe 20 63 61 73 65 20 32 36 35 3a 20 2f 2a 20 70 6c   case 265: /* pl
321ff 75 73 5f 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f  us_num ::= plus_
32200 6f 70 74 20 6e 75 6d 62 65 72 20 2a 2f 20 79 79  opt number */ yy
32201 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32202 6f 3d 3d 32 36 35 29 3b 0a 20 20 20 20 20 20 63  o==265);.      c
32203 61 73 65 20 32 36 36 3a 20 2f 2a 20 6d 69 6e 75  ase 266: /* minu
32204 73 5f 6e 75 6d 20 3a 3a 3d 20 4d 49 4e 55 53 20  s_num ::= MINUS 
32205 6e 75 6d 62 65 72 20 2a 2f 20 79 79 74 65 73 74  number */ yytest
32206 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
32207 36 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  66);.      case 
32208 32 36 37 3a 20 2f 2a 20 6e 75 6d 62 65 72 20 3a  267: /* number :
32209 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54  := INTEGER|FLOAT
3220a 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
3220b 79 72 75 6c 65 6e 6f 3d 3d 32 36 37 29 3b 0a 20  yruleno==267);. 
3220c 20 20 20 20 20 63 61 73 65 20 32 38 35 3a 20 2f       case 285: /
3220d 2a 20 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f  * trnm ::= nm */
3220e 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3220f 6c 65 6e 6f 3d 3d 32 38 35 29 3b 0a 7b 79 79 67  leno==285);.{yyg
32210 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79  otominor.yy0 = y
32211 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32212 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
32213 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 34 35  k;.      case 45
32214 3a 20 2f 2a 20 74 79 70 65 20 3a 3a 3d 20 74 79  : /* type ::= ty
32215 70 65 74 6f 6b 65 6e 20 2a 2f 0a 7b 73 71 6c 69  petoken */.{sqli
32216 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  te3AddColumnType
32217 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30  (pParse,&yymsp[0
32218 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
32219 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3221a 20 20 20 20 63 61 73 65 20 34 37 3a 20 2f 2a 20      case 47: /* 
3221b 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79  typetoken ::= ty
3221c 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64  pename LP signed
3221d 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
3221e 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79  ominor.yy0.z = y
3221f 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
32220 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  y0.z;.  yygotomi
32221 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74  nor.yy0.n = (int
32222 29 28 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  )(&yymsp[0].mino
32223 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d  r.yy0.z[yymsp[0]
32224 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20 2d 20  .minor.yy0.n] - 
32225 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
32226 79 79 30 2e 7a 29 3b 0a 7d 0a 20 20 20 20 20 20  yy0.z);.}.      
32227 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32228 61 73 65 20 34 38 3a 20 2f 2a 20 74 79 70 65 74  ase 48: /* typet
32229 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d  oken ::= typenam
3222a 65 20 4c 50 20 73 69 67 6e 65 64 20 43 4f 4d 4d  e LP signed COMM
3222b 41 20 73 69 67 6e 65 64 20 52 50 20 2a 2f 0a 7b  A signed RP */.{
3222c 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
3222d 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 35 5d  y0.z = yymsp[-5]
3222e 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
3222f 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e  yygotominor.yy0.
32230 6e 20 3d 20 28 69 6e 74 29 28 26 79 79 6d 73 70  n = (int)(&yymsp
32231 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b  [0].minor.yy0.z[
32232 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32233 79 30 2e 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 35  y0.n] - yymsp[-5
32234 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 0a  ].minor.yy0.z);.
32235 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32236 0a 20 20 20 20 20 20 63 61 73 65 20 35 30 3a 20  .      case 50: 
32237 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20  /* typename ::= 
32238 74 79 70 65 6e 61 6d 65 20 69 64 73 20 2a 2f 0a  typename ids */.
32239 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30  {yygotominor.yy0
3223a 2e 7a 3d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  .z=yymsp[-1].min
3223b 6f 72 2e 79 79 30 2e 7a 3b 20 79 79 67 6f 74 6f  or.yy0.z; yygoto
3223c 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 79 79 6d 73  minor.yy0.n=yyms
3223d 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  p[0].minor.yy0.n
3223e 2b 28 69 6e 74 29 28 79 79 6d 73 70 5b 30 5d 2e  +(int)(yymsp[0].
3223f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 2d 79 79 6d 73  minor.yy0.z-yyms
32240 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-1].minor.yy0.
32241 7a 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  z);}.        bre
32242 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35  ak;.      case 5
32243 37 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  7: /* ccons ::= 
32244 44 45 46 41 55 4c 54 20 74 65 72 6d 20 2a 2f 0a  DEFAULT term */.
32245 20 20 20 20 20 20 63 61 73 65 20 35 39 3a 20 2f        case 59: /
32246 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41  * ccons ::= DEFA
32247 55 4c 54 20 50 4c 55 53 20 74 65 72 6d 20 2a 2f  ULT PLUS term */
32248 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32249 6c 65 6e 6f 3d 3d 35 39 29 3b 0a 7b 73 71 6c 69  leno==59);.{sqli
3224a 74 65 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c  te3AddDefaultVal
3224b 75 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  ue(pParse,&yymsp
3224c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29  [0].minor.yy346)
3224d 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3224e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 38 3a  ;.      case 58:
3224f 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45   /* ccons ::= DE
32250 46 41 55 4c 54 20 4c 50 20 65 78 70 72 20 52 50  FAULT LP expr RP
32251 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 44   */.{sqlite3AddD
32252 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72  efaultValue(pPar
32253 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se,&yymsp[-1].mi
32254 6e 6f 72 2e 79 79 33 34 36 29 3b 7d 0a 20 20 20  nor.yy346);}.   
32255 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32256 20 20 63 61 73 65 20 36 30 3a 20 2f 2a 20 63 63    case 60: /* cc
32257 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
32258 4d 49 4e 55 53 20 74 65 72 6d 20 2a 2f 0a 7b 0a  MINUS term */.{.
32259 20 20 45 78 70 72 53 70 61 6e 20 76 3b 0a 20 20    ExprSpan v;.  
3225a 76 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  v.pExpr = sqlite
3225b 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3225c 4b 5f 55 4d 49 4e 55 53 2c 20 79 79 6d 73 70 5b  K_UMINUS, yymsp[
3225d 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  0].minor.yy346.p
3225e 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 76  Expr, 0, 0);.  v
3225f 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
32260 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b  -1].minor.yy0.z;
32261 0a 20 20 76 2e 7a 45 6e 64 20 3d 20 79 79 6d 73  .  v.zEnd = yyms
32262 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  p[0].minor.yy346
32263 2e 7a 45 6e 64 3b 0a 20 20 73 71 6c 69 74 65 33  .zEnd;.  sqlite3
32264 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28  AddDefaultValue(
32265 70 50 61 72 73 65 2c 26 76 29 3b 0a 7d 0a 20 20  pParse,&v);.}.  
32266 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32267 20 20 20 63 61 73 65 20 36 31 3a 20 2f 2a 20 63     case 61: /* c
32268 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54  cons ::= DEFAULT
32269 20 69 64 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 53   id */.{.  ExprS
3226a 70 61 6e 20 76 3b 0a 20 20 73 70 61 6e 45 78 70  pan v;.  spanExp
3226b 72 28 26 76 2c 20 70 50 61 72 73 65 2c 20 54 4b  r(&v, pParse, TK
3226c 5f 53 54 52 49 4e 47 2c 20 26 79 79 6d 73 70 5b  _STRING, &yymsp[
3226d 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
3226e 20 73 71 6c 69 74 65 33 41 64 64 44 65 66 61 75   sqlite3AddDefau
3226f 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c 26  ltValue(pParse,&
32270 76 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  v);.}.        br
32271 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32272 36 33 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d  63: /* ccons ::=
32273 20 4e 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66   NOT NULL onconf
32274 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 4e   */.{sqlite3AddN
32275 6f 74 4e 75 6c 6c 28 70 50 61 72 73 65 2c 20 79  otNull(pParse, y
32276 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32277 33 32 38 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  328);}.        b
32278 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32279 20 36 34 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a   64: /* ccons ::
3227a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20 73 6f  = PRIMARY KEY so
3227b 72 74 6f 72 64 65 72 20 6f 6e 63 6f 6e 66 20 61  rtorder onconf a
3227c 75 74 6f 69 6e 63 20 2a 2f 0a 7b 73 71 6c 69 74  utoinc */.{sqlit
3227d 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28  e3AddPrimaryKey(
3227e 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 2d  pParse,0,yymsp[-
3227f 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 79  1].minor.yy328,y
32280 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32281 33 32 38 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  328,yymsp[-2].mi
32282 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20  nor.yy328);}.   
32283 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32284 20 20 63 61 73 65 20 36 35 3a 20 2f 2a 20 63 63    case 65: /* cc
32285 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f  ons ::= UNIQUE o
32286 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65  nconf */.{sqlite
32287 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50 61  3CreateIndex(pPa
32288 72 73 65 2c 30 2c 30 2c 30 2c 30 2c 79 79 6d 73  rse,0,0,0,0,yyms
32289 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
3228a 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20  ,0,0,0,0);}.    
3228b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3228c 20 63 61 73 65 20 36 36 3a 20 2f 2a 20 63 63 6f   case 66: /* cco
3228d 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20  ns ::= CHECK LP 
3228e 65 78 70 72 20 52 50 20 2a 2f 0a 7b 73 71 6c 69  expr RP */.{sqli
3228f 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74  te3AddCheckConst
32290 72 61 69 6e 74 28 70 50 61 72 73 65 2c 79 79 6d  raint(pParse,yym
32291 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
32292 34 36 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20 20  46.pExpr);}.    
32293 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32294 20 63 61 73 65 20 36 37 3a 20 2f 2a 20 63 63 6f   case 67: /* cco
32295 6e 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45  ns ::= REFERENCE
32296 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74  S nm idxlist_opt
32297 20 72 65 66 61 72 67 73 20 2a 2f 0a 7b 73 71 6c   refargs */.{sql
32298 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67  ite3CreateForeig
32299 6e 4b 65 79 28 70 50 61 72 73 65 2c 30 2c 26 79  nKey(pParse,0,&y
3229a 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3229b 79 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  y0,yymsp[-1].min
3229c 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b 30 5d  or.yy14,yymsp[0]
3229d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a  .minor.yy328);}.
3229e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3229f 20 20 20 20 20 63 61 73 65 20 36 38 3a 20 2f 2a       case 68: /*
322a0 20 63 63 6f 6e 73 20 3a 3a 3d 20 64 65 66 65 72   ccons ::= defer
322a1 5f 73 75 62 63 6c 61 75 73 65 20 2a 2f 0a 7b 73  _subclause */.{s
322a2 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69  qlite3DeferForei
322a3 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 79 79 6d  gnKey(pParse,yym
322a4 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
322a5 38 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  8);}.        bre
322a6 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
322a7 39 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  9: /* ccons ::= 
322a8 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a 7b  COLLATE ids */.{
322a9 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74  sqlite3AddCollat
322aa 65 54 79 70 65 28 70 50 61 72 73 65 2c 20 26 79  eType(pParse, &y
322ab 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
322ac 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
322ad 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37  ak;.      case 7
322ae 32 3a 20 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a  2: /* refargs ::
322af 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  = */.{ yygotomin
322b0 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 4e 6f  or.yy328 = OE_No
322b1 6e 65 2a 30 78 30 31 30 31 3b 20 2f 2a 20 45 56  ne*0x0101; /* EV
322b2 3a 20 52 2d 31 39 38 30 33 2d 34 35 38 38 34 20  : R-19803-45884 
322b3 2a 2f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  */}.        brea
322b4 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 33  k;.      case 73
322b5 3a 20 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d  : /* refargs ::=
322b6 20 72 65 66 61 72 67 73 20 72 65 66 61 72 67 20   refargs refarg 
322b7 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
322b8 2e 79 79 33 32 38 20 3d 20 28 79 79 6d 73 70 5b  .yy328 = (yymsp[
322b9 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20  -1].minor.yy328 
322ba 26 20 7e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  & ~yymsp[0].mino
322bb 72 2e 79 79 34 32 39 2e 6d 61 73 6b 29 20 7c 20  r.yy429.mask) | 
322bc 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
322bd 79 34 32 39 2e 76 61 6c 75 65 3b 20 7d 0a 20 20  y429.value; }.  
322be 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
322bf 20 20 20 63 61 73 65 20 37 34 3a 20 2f 2a 20 72     case 74: /* r
322c0 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20  efarg ::= MATCH 
322c1 6e 6d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  nm */.{ yygotomi
322c2 6e 6f 72 2e 79 79 34 32 39 2e 76 61 6c 75 65 20  nor.yy429.value 
322c3 3d 20 30 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d  = 0;     yygotom
322c4 69 6e 6f 72 2e 79 79 34 32 39 2e 6d 61 73 6b 20  inor.yy429.mask 
322c5 3d 20 30 78 30 30 30 30 30 30 3b 20 7d 0a 20 20  = 0x000000; }.  
322c6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
322c7 20 20 20 63 61 73 65 20 37 35 3a 20 2f 2a 20 72     case 75: /* r
322c8 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c  efarg ::= ON DEL
322c9 45 54 45 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20  ETE refact */.{ 
322ca 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32  yygotominor.yy42
322cb 39 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b  9.value = yymsp[
322cc 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 20  0].minor.yy328; 
322cd 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
322ce 79 79 34 32 39 2e 6d 61 73 6b 20 3d 20 30 78 30  yy429.mask = 0x0
322cf 30 30 30 66 66 3b 20 7d 0a 20 20 20 20 20 20 20  000ff; }.       
322d0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
322d1 73 65 20 37 36 3a 20 2f 2a 20 72 65 66 61 72 67  se 76: /* refarg
322d2 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20 72   ::= ON UPDATE r
322d3 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67 6f 74  efact */.{ yygot
322d4 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 76 61 6c  ominor.yy429.val
322d5 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  ue = yymsp[0].mi
322d6 6e 6f 72 2e 79 79 33 32 38 3c 3c 38 3b 20 20 79  nor.yy328<<8;  y
322d7 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39  ygotominor.yy429
322d8 2e 6d 61 73 6b 20 3d 20 30 78 30 30 66 66 30 30  .mask = 0x00ff00
322d9 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ; }.        brea
322da 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 37  k;.      case 77
322db 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20  : /* refact ::= 
322dc 53 45 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 20 79 79  SET NULL */.{ yy
322dd 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
322de 3d 20 4f 45 5f 53 65 74 4e 75 6c 6c 3b 20 20 2f  = OE_SetNull;  /
322df 2a 20 45 56 3a 20 52 2d 33 33 33 32 36 2d 34 35  * EV: R-33326-45
322e0 32 35 32 20 2a 2f 7d 0a 20 20 20 20 20 20 20 20  252 */}.        
322e1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
322e2 65 20 37 38 3a 20 2f 2a 20 72 65 66 61 63 74 20  e 78: /* refact 
322e3 3a 3a 3d 20 53 45 54 20 44 45 46 41 55 4c 54 20  ::= SET DEFAULT 
322e4 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
322e5 2e 79 79 33 32 38 20 3d 20 4f 45 5f 53 65 74 44  .yy328 = OE_SetD
322e6 66 6c 74 3b 20 20 2f 2a 20 45 56 3a 20 52 2d 33  flt;  /* EV: R-3
322e7 33 33 32 36 2d 34 35 32 35 32 20 2a 2f 7d 0a 20  3326-45252 */}. 
322e8 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
322e9 20 20 20 20 63 61 73 65 20 37 39 3a 20 2f 2a 20      case 79: /* 
322ea 72 65 66 61 63 74 20 3a 3a 3d 20 43 41 53 43 41  refact ::= CASCA
322eb 44 45 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  DE */.{ yygotomi
322ec 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 43  nor.yy328 = OE_C
322ed 61 73 63 61 64 65 3b 20 20 2f 2a 20 45 56 3a 20  ascade;  /* EV: 
322ee 52 2d 33 33 33 32 36 2d 34 35 32 35 32 20 2a 2f  R-33326-45252 */
322ef 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
322f0 0a 20 20 20 20 20 20 63 61 73 65 20 38 30 3a 20  .      case 80: 
322f1 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 52 45  /* refact ::= RE
322f2 53 54 52 49 43 54 20 2a 2f 0a 7b 20 79 79 67 6f  STRICT */.{ yygo
322f3 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
322f4 4f 45 5f 52 65 73 74 72 69 63 74 3b 20 2f 2a 20  OE_Restrict; /* 
322f5 45 56 3a 20 52 2d 33 33 33 32 36 2d 34 35 32 35  EV: R-33326-4525
322f6 32 20 2a 2f 7d 0a 20 20 20 20 20 20 20 20 62 72  2 */}.        br
322f7 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
322f8 38 31 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a  81: /* refact ::
322f9 3d 20 4e 4f 20 41 43 54 49 4f 4e 20 2a 2f 0a 7b  = NO ACTION */.{
322fa 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
322fb 32 38 20 3d 20 4f 45 5f 4e 6f 6e 65 3b 20 20 20  28 = OE_None;   
322fc 20 20 2f 2a 20 45 56 3a 20 52 2d 33 33 33 32 36    /* EV: R-33326
322fd 2d 34 35 32 35 32 20 2a 2f 7d 0a 20 20 20 20 20  -45252 */}.     
322fe 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
322ff 63 61 73 65 20 38 33 3a 20 2f 2a 20 64 65 66 65  case 83: /* defe
32300 72 5f 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20  r_subclause ::= 
32301 44 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f  DEFERRABLE init_
32302 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70  deferred_pred_op
32303 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  t */.      case 
32304 39 38 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62  98: /* defer_sub
32305 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64  clause_opt ::= d
32306 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 2a  efer_subclause *
32307 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32308 75 6c 65 6e 6f 3d 3d 39 38 29 3b 0a 20 20 20 20  uleno==98);.    
32309 20 20 63 61 73 65 20 31 30 30 3a 20 2f 2a 20 6f    case 100: /* o
3230a 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e  nconf ::= ON CON
3230b 46 4c 49 43 54 20 72 65 73 6f 6c 76 65 74 79 70  FLICT resolvetyp
3230c 65 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  e */ yytestcase(
3230d 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 30 29 3b 0a  yyruleno==100);.
3230e 20 20 20 20 20 20 63 61 73 65 20 31 30 33 3a 20        case 103: 
3230f 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a  /* resolvetype :
32310 3a 3d 20 72 61 69 73 65 74 79 70 65 20 2a 2f 20  := raisetype */ 
32311 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32312 65 6e 6f 3d 3d 31 30 33 29 3b 0a 7b 79 79 67 6f  eno==103);.{yygo
32313 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
32314 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32315 79 33 32 38 3b 7d 0a 20 20 20 20 20 20 20 20 62  y328;}.        b
32316 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32317 20 38 37 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74   87: /* conslist
32318 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  _opt ::= */.{yyg
32319 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d  otominor.yy0.n =
3231a 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e   0; yygotominor.
3231b 79 79 30 2e 7a 20 3d 20 30 3b 7d 0a 20 20 20 20  yy0.z = 0;}.    
3231c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3231d 20 63 61 73 65 20 38 38 3a 20 2f 2a 20 63 6f 6e   case 88: /* con
3231e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f  slist_opt ::= CO
3231f 4d 4d 41 20 63 6f 6e 73 6c 69 73 74 20 2a 2f 0a  MMA conslist */.
32320 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30  {yygotominor.yy0
32321 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
32322 6f 72 2e 79 79 30 3b 7d 0a 20 20 20 20 20 20 20  or.yy0;}.       
32323 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32324 73 65 20 39 33 3a 20 2f 2a 20 74 63 6f 6e 73 20  se 93: /* tcons 
32325 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20  ::= PRIMARY KEY 
32326 4c 50 20 69 64 78 6c 69 73 74 20 61 75 74 6f 69  LP idxlist autoi
32327 6e 63 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a  nc RP onconf */.
32328 7b 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61  {sqlite3AddPrima
32329 72 79 4b 65 79 28 70 50 61 72 73 65 2c 79 79 6d  ryKey(pParse,yym
3232a 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
3232b 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  4,yymsp[0].minor
3232c 2e 79 79 33 32 38 2c 79 79 6d 73 70 5b 2d 32 5d  .yy328,yymsp[-2]
3232d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 30 29 3b  .minor.yy328,0);
3232e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3232f 0a 20 20 20 20 20 20 63 61 73 65 20 39 34 3a 20  .      case 94: 
32330 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49  /* tcons ::= UNI
32331 51 55 45 20 4c 50 20 69 64 78 6c 69 73 74 20 52  QUE LP idxlist R
32332 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c  P onconf */.{sql
32333 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28  ite3CreateIndex(
32334 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c 79 79 6d  pParse,0,0,0,yym
32335 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-2].minor.yy1
32336 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  4,yymsp[0].minor
32337 2e 79 79 33 32 38 2c 30 2c 30 2c 30 2c 30 29 3b  .yy328,0,0,0,0);
32338 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32339 0a 20 20 20 20 20 20 63 61 73 65 20 39 35 3a 20  .      case 95: 
3233a 2f 2a 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45  /* tcons ::= CHE
3233b 43 4b 20 4c 50 20 65 78 70 72 20 52 50 20 6f 6e  CK LP expr RP on
3233c 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  conf */.{sqlite3
3233d 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69  AddCheckConstrai
3233e 6e 74 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  nt(pParse,yymsp[
3233f 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -2].minor.yy346.
32340 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20  pExpr);}.       
32341 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32342 73 65 20 39 36 3a 20 2f 2a 20 74 63 6f 6e 73 20  se 96: /* tcons 
32343 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20  ::= FOREIGN KEY 
32344 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 52 45  LP idxlist RP RE
32345 46 45 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c  FERENCES nm idxl
32346 69 73 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20  ist_opt refargs 
32347 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f  defer_subclause_
32348 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c  opt */.{.    sql
32349 69 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67  ite3CreateForeig
3234a 6e 4b 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d  nKey(pParse, yym
3234b 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-6].minor.yy1
3234c 34 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  4, &yymsp[-3].mi
3234d 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d  nor.yy0, yymsp[-
3234e 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79  2].minor.yy14, y
3234f 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
32350 79 33 32 38 29 3b 0a 20 20 20 20 73 71 6c 69 74  y328);.    sqlit
32351 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65  e3DeferForeignKe
32352 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  y(pParse, yymsp[
32353 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  0].minor.yy328);
32354 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32355 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 39 3a  ;.      case 99:
32356 20 2f 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 2a   /* onconf ::= *
32357 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
32358 79 33 32 38 20 3d 20 4f 45 5f 44 65 66 61 75 6c  y328 = OE_Defaul
32359 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  t;}.        brea
3235a 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  k;.      case 10
3235b 31 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d  1: /* orconf ::=
3235c 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
3235d 2e 79 79 31 38 36 20 3d 20 4f 45 5f 44 65 66 61  .yy186 = OE_Defa
3235e 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ult;}.        br
3235f 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32360 31 30 32 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a  102: /* orconf :
32361 3a 3d 20 4f 52 20 72 65 73 6f 6c 76 65 74 79 70  := OR resolvetyp
32362 65 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  e */.{yygotomino
32363 72 2e 79 79 31 38 36 20 3d 20 28 75 38 29 79 79  r.yy186 = (u8)yy
32364 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
32365 32 38 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  28;}.        bre
32366 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
32367 30 34 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79  04: /* resolvety
32368 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45 20 2a 2f  pe ::= IGNORE */
32369 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
3236a 33 32 38 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b  328 = OE_Ignore;
3236b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3236c 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 35 3a  .      case 105:
3236d 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20   /* resolvetype 
3236e 3a 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a 7b  ::= REPLACE */.{
3236f 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
32370 38 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d  8 = OE_Replace;}
32371 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32372 20 20 20 20 20 20 63 61 73 65 20 31 30 36 3a 20        case 106: 
32373 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20  /* cmd ::= DROP 
32374 54 41 42 4c 45 20 69 66 65 78 69 73 74 73 20 66  TABLE ifexists f
32375 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73  ullname */.{.  s
32376 71 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28  qlite3DropTable(
32377 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
32378 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 30 2c 20  .minor.yy65, 0, 
32379 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3237a 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20  yy328);.}.      
3237b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3237c 61 73 65 20 31 30 39 3a 20 2f 2a 20 63 6d 64 20  ase 109: /* cmd 
3237d 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d  ::= createkw tem
3237e 70 20 56 49 45 57 20 69 66 6e 6f 74 65 78 69 73  p VIEW ifnotexis
3237f 74 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65  ts nm dbnm AS se
32380 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  lect */.{.  sqli
32381 74 65 33 43 72 65 61 74 65 56 69 65 77 28 70 50  te3CreateView(pP
32382 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 37 5d  arse, &yymsp[-7]
32383 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
32384 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
32385 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
32386 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 30 5d  or.yy0, yymsp[0]
32387 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 79 79 6d 73  .minor.yy3, yyms
32388 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-6].minor.yy32
32389 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  8, yymsp[-4].min
3238a 6f 72 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20  or.yy328);.}.   
3238b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3238c 20 20 63 61 73 65 20 31 31 30 3a 20 2f 2a 20 63    case 110: /* c
3238d 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57  md ::= DROP VIEW
3238e 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
3238f 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  me */.{.  sqlite
32390 33 44 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73  3DropTable(pPars
32391 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  e, yymsp[0].mino
32392 72 2e 79 79 36 35 2c 20 31 2c 20 79 79 6d 73 70  r.yy65, 1, yymsp
32393 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-1].minor.yy328
32394 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
32395 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
32396 31 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 73  11: /* cmd ::= s
32397 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 53 65 6c  elect */.{.  Sel
32398 65 63 74 44 65 73 74 20 64 65 73 74 20 3d 20 7b  ectDest dest = {
32399 53 52 54 5f 4f 75 74 70 75 74 2c 20 30 2c 20 30  SRT_Output, 0, 0
3239a 2c 20 30 2c 20 30 7d 3b 0a 20 20 73 71 6c 69 74  , 0, 0};.  sqlit
3239b 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
3239c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3239d 79 79 33 2c 20 26 64 65 73 74 29 3b 0a 20 20 73  yy3, &dest);.  s
3239e 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
3239f 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
323a0 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
323a1 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  3);.}.        br
323a2 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
323a3 31 31 32 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a  112: /* select :
323a4 3a 3d 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a  := oneselect */.
323a5 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
323a6 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
323a7 72 2e 79 79 33 3b 7d 0a 20 20 20 20 20 20 20 20  r.yy3;}.        
323a8 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
323a9 65 20 31 31 33 3a 20 2f 2a 20 73 65 6c 65 63 74  e 113: /* select
323aa 20 3a 3a 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74   ::= select mult
323ab 69 73 65 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65  iselect_op onese
323ac 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 69 66 28 20  lect */.{.  if( 
323ad 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
323ae 79 33 20 29 7b 0a 20 20 20 20 79 79 6d 73 70 5b  y3 ){.    yymsp[
323af 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2d 3e 6f 70  0].minor.yy3->op
323b0 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 2d 31 5d   = (u8)yymsp[-1]
323b1 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 20 20  .minor.yy328;.  
323b2 20 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72    yymsp[0].minor
323b3 2e 79 79 33 2d 3e 70 50 72 69 6f 72 20 3d 20 79  .yy3->pPrior = y
323b4 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
323b5 79 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  y3;.  }else{.   
323b6 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
323b7 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
323b8 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
323b9 2e 79 79 33 29 3b 0a 20 20 7d 0a 20 20 79 79 67  .yy3);.  }.  yyg
323ba 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 20 3d 20 79  otominor.yy3 = y
323bb 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
323bc 33 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  3;.}.        bre
323bd 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
323be 31 35 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65  15: /* multisele
323bf 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20  ct_op ::= UNION 
323c0 41 4c 4c 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ALL */.{yygotomi
323c1 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54 4b 5f 41  nor.yy328 = TK_A
323c2 4c 4c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  LL;}.        bre
323c3 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
323c4 31 37 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74  17: /* oneselect
323c5 20 3a 3a 3d 20 53 45 4c 45 43 54 20 64 69 73 74   ::= SELECT dist
323c6 69 6e 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20  inct selcollist 
323c7 66 72 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20 67  from where_opt g
323c8 72 6f 75 70 62 79 5f 6f 70 74 20 68 61 76 69 6e  roupby_opt havin
323c9 67 5f 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f 70  g_opt orderby_op
323ca 74 20 6c 69 6d 69 74 5f 6f 70 74 20 2a 2f 0a 7b  t limit_opt */.{
323cb 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
323cc 79 33 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  y3 = sqlite3Sele
323cd 63 74 4e 65 77 28 70 50 61 72 73 65 2c 79 79 6d  ctNew(pParse,yym
323ce 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-6].minor.yy1
323cf 34 2c 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  4,yymsp[-5].mino
323d0 72 2e 79 79 36 35 2c 79 79 6d 73 70 5b 2d 34 5d  r.yy65,yymsp[-4]
323d1 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d  .minor.yy132,yym
323d2 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-3].minor.yy1
323d3 34 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  4,yymsp[-2].mino
323d4 72 2e 79 79 31 33 32 2c 79 79 6d 73 70 5b 2d 31  r.yy132,yymsp[-1
323d5 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d  ].minor.yy14,yym
323d6 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-7].minor.yy3
323d7 32 38 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  28,yymsp[0].mino
323d8 72 2e 79 79 34 37 36 2e 70 4c 69 6d 69 74 2c 79  r.yy476.pLimit,y
323d9 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
323da 34 37 36 2e 70 4f 66 66 73 65 74 29 3b 0a 7d 0a  476.pOffset);.}.
323db 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
323dc 20 20 20 20 20 63 61 73 65 20 31 32 31 3a 20 2f       case 121: /
323dd 2a 20 73 63 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f  * sclp ::= selco
323de 6c 6c 69 73 74 20 43 4f 4d 4d 41 20 2a 2f 0a 20  llist COMMA */. 
323df 20 20 20 20 20 63 61 73 65 20 32 34 37 3a 20 2f       case 247: /
323e0 2a 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a  * idxlist_opt ::
323e1 3d 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20  = LP idxlist RP 
323e2 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
323e3 72 75 6c 65 6e 6f 3d 3d 32 34 37 29 3b 0a 7b 79  ruleno==247);.{y
323e4 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20  ygotominor.yy14 
323e5 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
323e6 72 2e 79 79 31 34 3b 7d 0a 20 20 20 20 20 20 20  r.yy14;}.       
323e7 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
323e8 73 65 20 31 32 32 3a 20 2f 2a 20 73 63 6c 70 20  se 122: /* sclp 
323e9 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
323ea 65 20 31 35 30 3a 20 2f 2a 20 6f 72 64 65 72 62  e 150: /* orderb
323eb 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  y_opt ::= */ yyt
323ec 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
323ed 3d 3d 31 35 30 29 3b 0a 20 20 20 20 20 20 63 61  ==150);.      ca
323ee 73 65 20 31 35 38 3a 20 2f 2a 20 67 72 6f 75 70  se 158: /* group
323ef 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  by_opt ::= */ yy
323f0 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
323f1 6f 3d 3d 31 35 38 29 3b 0a 20 20 20 20 20 20 63  o==158);.      c
323f2 61 73 65 20 32 34 30 3a 20 2f 2a 20 65 78 70 72  ase 240: /* expr
323f3 6c 69 73 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  list ::= */ yyte
323f4 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
323f5 3d 32 34 30 29 3b 0a 20 20 20 20 20 20 63 61 73  =240);.      cas
323f6 65 20 32 34 36 3a 20 2f 2a 20 69 64 78 6c 69 73  e 246: /* idxlis
323f7 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  t_opt ::= */ yyt
323f8 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
323f9 3d 3d 32 34 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==246);.{yygotom
323fa 69 6e 6f 72 2e 79 79 31 34 20 3d 20 30 3b 7d 0a  inor.yy14 = 0;}.
323fb 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
323fc 20 20 20 20 20 63 61 73 65 20 31 32 33 3a 20 2f       case 123: /
323fd 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d  * selcollist ::=
323fe 20 73 63 6c 70 20 65 78 70 72 20 61 73 20 2a 2f   sclp expr as */
323ff 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  .{.   yygotomino
32400 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
32401 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
32402 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d  Parse, yymsp[-2]
32403 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d  .minor.yy14, yym
32404 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
32405 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20 69 66  46.pExpr);.   if
32406 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ( yymsp[0].minor
32407 2e 79 79 30 2e 6e 3e 30 20 29 20 73 71 6c 69 74  .yy0.n>0 ) sqlit
32408 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d  e3ExprListSetNam
32409 65 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  e(pParse, yygoto
3240a 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d  minor.yy14, &yym
3240b 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
3240c 20 31 29 3b 0a 20 20 20 73 71 6c 69 74 65 33 45   1);.   sqlite3E
3240d 78 70 72 4c 69 73 74 53 65 74 53 70 61 6e 28 70  xprListSetSpan(p
3240e 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f  Parse,yygotomino
3240f 72 2e 79 79 31 34 2c 26 79 79 6d 73 70 5b 2d 31  r.yy14,&yymsp[-1
32410 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 0a  ].minor.yy346);.
32411 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32412 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 34 3a  .      case 124:
32413 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a   /* selcollist :
32414 3a 3d 20 73 63 6c 70 20 53 54 41 52 20 2a 2f 0a  := sclp STAR */.
32415 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71  {.  Expr *p = sq
32416 6c 69 74 65 33 45 78 70 72 28 70 50 61 72 73 65  lite3Expr(pParse
32417 2d 3e 64 62 2c 20 54 4b 5f 41 4c 4c 2c 20 30 29  ->db, TK_ALL, 0)
32418 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
32419 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78  yy14 = sqlite3Ex
3241a 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
3241b 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  rse, yymsp[-1].m
3241c 69 6e 6f 72 2e 79 79 31 34 2c 20 70 29 3b 0a 7d  inor.yy14, p);.}
3241d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3241e 20 20 20 20 20 20 63 61 73 65 20 31 32 35 3a 20        case 125: 
3241f 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a  /* selcollist ::
32420 3d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54  = sclp nm DOT ST
32421 41 52 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a  AR */.{.  Expr *
32422 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33  pRight = sqlite3
32423 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
32424 5f 41 4c 4c 2c 20 30 2c 20 30 2c 20 26 79 79 6d  _ALL, 0, 0, &yym
32425 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
32426 3b 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20  ;.  Expr *pLeft 
32427 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
32428 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
32429 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d   0, &yymsp[-2].m
3242a 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70  inor.yy0);.  Exp
3242b 72 20 2a 70 44 6f 74 20 3d 20 73 71 6c 69 74 65  r *pDot = sqlite
3242c 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
3242d 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 2c 20 70 52  K_DOT, pLeft, pR
3242e 69 67 68 74 2c 20 30 29 3b 0a 20 20 79 79 67 6f  ight, 0);.  yygo
3242f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73  tominor.yy14 = s
32430 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
32431 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
32432 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  p[-3].minor.yy14
32433 2c 20 70 44 6f 74 29 3b 0a 7d 0a 20 20 20 20 20  , pDot);.}.     
32434 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32435 63 61 73 65 20 31 32 38 3a 20 2f 2a 20 61 73 20  case 128: /* as 
32436 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
32437 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a  nor.yy0.n = 0;}.
32438 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32439 20 20 20 20 20 63 61 73 65 20 31 32 39 3a 20 2f       case 129: /
3243a 2a 20 66 72 6f 6d 20 3a 3a 3d 20 2a 2f 0a 7b 79  * from ::= */.{y
3243b 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20  ygotominor.yy65 
3243c 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
3243d 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 62  cZero(pParse->db
3243e 2c 20 73 69 7a 65 6f 66 28 2a 79 79 67 6f 74 6f  , sizeof(*yygoto
3243f 6d 69 6e 6f 72 2e 79 79 36 35 29 29 3b 7d 0a 20  minor.yy65));}. 
32440 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32441 20 20 20 20 63 61 73 65 20 31 33 30 3a 20 2f 2a      case 130: /*
32442 20 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73   from ::= FROM s
32443 65 6c 74 61 62 6c 69 73 74 20 2a 2f 0a 7b 0a 20  eltablist */.{. 
32444 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36   yygotominor.yy6
32445 35 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  5 = yymsp[0].min
32446 6f 72 2e 79 79 36 35 3b 0a 20 20 73 71 6c 69 74  or.yy65;.  sqlit
32447 65 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f  e3SrcListShiftJo
32448 69 6e 54 79 70 65 28 79 79 67 6f 74 6f 6d 69 6e  inType(yygotomin
32449 6f 72 2e 79 79 36 35 29 3b 0a 7d 0a 20 20 20 20  or.yy65);.}.    
3244a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3244b 20 63 61 73 65 20 31 33 31 3a 20 2f 2a 20 73 74   case 131: /* st
3244c 6c 5f 70 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c  l_prefix ::= sel
3244d 74 61 62 6c 69 73 74 20 6a 6f 69 6e 6f 70 20 2a  tablist joinop *
3244e 2f 0a 7b 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e  /.{.   yygotomin
3244f 6f 72 2e 79 79 36 35 20 3d 20 79 79 6d 73 70 5b  or.yy65 = yymsp[
32450 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 3b 0a  -1].minor.yy65;.
32451 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 79 79     if( ALWAYS(yy
32452 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 26  gotominor.yy65 &
32453 26 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  & yygotominor.yy
32454 36 35 2d 3e 6e 53 72 63 3e 30 29 20 29 20 79 79  65->nSrc>0) ) yy
32455 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 2d 3e  gotominor.yy65->
32456 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  a[yygotominor.yy
32457 36 35 2d 3e 6e 53 72 63 2d 31 5d 2e 6a 6f 69 6e  65->nSrc-1].join
32458 74 79 70 65 20 3d 20 28 75 38 29 79 79 6d 73 70  type = (u8)yymsp
32459 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b  [0].minor.yy328;
3245a 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
3245b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 32  ;.      case 132
3245c 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20  : /* stl_prefix 
3245d 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
3245e 6e 6f 72 2e 79 79 36 35 20 3d 20 30 3b 7d 0a 20  nor.yy65 = 0;}. 
3245f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32460 20 20 20 20 63 61 73 65 20 31 33 33 3a 20 2f 2a      case 133: /*
32461 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20   seltablist ::= 
32462 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64 62  stl_prefix nm db
32463 6e 6d 20 61 73 20 69 6e 64 65 78 65 64 5f 6f 70  nm as indexed_op
32464 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f  t on_opt using_o
32465 70 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pt */.{.  yygoto
32466 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c  minor.yy65 = sql
32467 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
32468 64 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73 65  dFromTerm(pParse
32469 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  ,yymsp[-6].minor
3246a 2e 79 79 36 35 2c 26 79 79 6d 73 70 5b 2d 35 5d  .yy65,&yymsp[-5]
3246b 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
3246c 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-4].minor.yy0,
3246d 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
3246e 2e 79 79 30 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d  .yy0,0,yymsp[-1]
3246f 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d  .minor.yy132,yym
32470 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  sp[0].minor.yy40
32471 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  8);.  sqlite3Src
32472 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 70 50  ListIndexedBy(pP
32473 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
32474 72 2e 79 79 36 35 2c 20 26 79 79 6d 73 70 5b 2d  r.yy65, &yymsp[-
32475 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  2].minor.yy0);.}
32476 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32477 20 20 20 20 20 20 63 61 73 65 20 31 33 34 3a 20        case 134: 
32478 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a  /* seltablist ::
32479 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20  = stl_prefix LP 
3247a 73 65 6c 65 63 74 20 52 50 20 61 73 20 6f 6e 5f  select RP as on_
3247b 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f  opt using_opt */
3247c 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  .{.    yygotomin
3247d 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c 69 74 65  or.yy65 = sqlite
3247e 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72  3SrcListAppendFr
3247f 6f 6d 54 65 72 6d 28 70 50 61 72 73 65 2c 79 79  omTerm(pParse,yy
32480 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
32481 36 35 2c 30 2c 30 2c 26 79 79 6d 73 70 5b 2d 32  65,0,0,&yymsp[-2
32482 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73  ].minor.yy0,yyms
32483 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c  p[-4].minor.yy3,
32484 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
32485 79 79 31 33 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d  yy132,yymsp[0].m
32486 69 6e 6f 72 2e 79 79 34 30 38 29 3b 0a 20 20 7d  inor.yy408);.  }
32487 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32488 20 20 20 20 20 20 63 61 73 65 20 31 33 35 3a 20        case 135: 
32489 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a  /* seltablist ::
3248a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20  = stl_prefix LP 
3248b 73 65 6c 74 61 62 6c 69 73 74 20 52 50 20 61 73  seltablist RP as
3248c 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70   on_opt using_op
3248d 74 20 2a 2f 0a 7b 0a 20 20 20 20 69 66 28 20 79  t */.{.    if( y
3248e 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
3248f 79 36 35 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b  y65==0 && yymsp[
32490 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d  -2].minor.yy0.n=
32491 3d 30 20 26 26 20 79 79 6d 73 70 5b 2d 31 5d 2e  =0 && yymsp[-1].
32492 6d 69 6e 6f 72 2e 79 79 31 33 32 3d 3d 30 20 26  minor.yy132==0 &
32493 26 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  & yymsp[0].minor
32494 2e 79 79 34 30 38 3d 3d 30 20 29 7b 0a 20 20 20  .yy408==0 ){.   
32495 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
32496 79 36 35 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e  y65 = yymsp[-4].
32497 6d 69 6e 6f 72 2e 79 79 36 35 3b 0a 20 20 20 20  minor.yy65;.    
32498 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 53 65 6c  }else{.      Sel
32499 65 63 74 20 2a 70 53 75 62 71 75 65 72 79 3b 0a  ect *pSubquery;.
3249a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72 63        sqlite3Src
3249b 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70  ListShiftJoinTyp
3249c 65 28 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  e(yymsp[-4].mino
3249d 72 2e 79 79 36 35 29 3b 0a 20 20 20 20 20 20 70  r.yy65);.      p
3249e 53 75 62 71 75 65 72 79 20 3d 20 73 71 6c 69 74  Subquery = sqlit
3249f 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72  e3SelectNew(pPar
324a0 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d  se,0,yymsp[-4].m
324a1 69 6e 6f 72 2e 79 79 36 35 2c 30 2c 30 2c 30 2c  inor.yy65,0,0,0,
324a2 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20  0,0,0,0);.      
324a3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35  yygotominor.yy65
324a4 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
324a5 74 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28  tAppendFromTerm(
324a6 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d  pParse,yymsp[-6]
324a7 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 30 2c 30 2c  .minor.yy65,0,0,
324a8 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
324a9 2e 79 79 30 2c 70 53 75 62 71 75 65 72 79 2c 79  .yy0,pSubquery,y
324aa 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
324ab 79 31 33 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  y132,yymsp[0].mi
324ac 6e 6f 72 2e 79 79 34 30 38 29 3b 0a 20 20 20 20  nor.yy408);.    
324ad 7d 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72  }.  }.        br
324ae 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
324af 31 33 36 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d  136: /* dbnm ::=
324b0 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
324b1 34 35 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f  45: /* indexed_o
324b2 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
324b3 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
324b4 34 35 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  45);.{yygotomino
324b5 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79 67 6f 74  r.yy0.z=0; yygot
324b6 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 30 3b 7d  ominor.yy0.n=0;}
324b7 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
324b8 20 20 20 20 20 20 63 61 73 65 20 31 33 38 3a 20        case 138: 
324b9 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20  /* fullname ::= 
324ba 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 79 79 67 6f  nm dbnm */.{yygo
324bb 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73  tominor.yy65 = s
324bc 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
324bd 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30  end(pParse->db,0
324be 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
324bf 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
324c0 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
324c1 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
324c2 20 20 63 61 73 65 20 31 33 39 3a 20 2f 2a 20 6a    case 139: /* j
324c3 6f 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c  oinop ::= COMMA|
324c4 4a 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  JOIN */.{ yygoto
324c5 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4a 54  minor.yy328 = JT
324c6 5f 49 4e 4e 45 52 3b 20 7d 0a 20 20 20 20 20 20  _INNER; }.      
324c7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
324c8 61 73 65 20 31 34 30 3a 20 2f 2a 20 6a 6f 69 6e  ase 140: /* join
324c9 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a  op ::= JOIN_KW J
324ca 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  OIN */.{ yygotom
324cb 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 73 71 6c  inor.yy328 = sql
324cc 69 74 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61  ite3JoinType(pPa
324cd 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  rse,&yymsp[-1].m
324ce 69 6e 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 20 7d  inor.yy0,0,0); }
324cf 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
324d0 20 20 20 20 20 20 63 61 73 65 20 31 34 31 3a 20        case 141: 
324d1 2f 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f  /* joinop ::= JO
324d2 49 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 20 2a 2f  IN_KW nm JOIN */
324d3 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
324d4 79 33 32 38 20 3d 20 73 71 6c 69 74 65 33 4a 6f  y328 = sqlite3Jo
324d5 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79  inType(pParse,&y
324d6 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
324d7 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  y0,&yymsp[-1].mi
324d8 6e 6f 72 2e 79 79 30 2c 30 29 3b 20 7d 0a 20 20  nor.yy0,0); }.  
324d9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
324da 20 20 20 63 61 73 65 20 31 34 32 3a 20 2f 2a 20     case 142: /* 
324db 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
324dc 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 20 2a 2f  KW nm nm JOIN */
324dd 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
324de 79 33 32 38 20 3d 20 73 71 6c 69 74 65 33 4a 6f  y328 = sqlite3Jo
324df 69 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79  inType(pParse,&y
324e0 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
324e1 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  y0,&yymsp[-2].mi
324e2 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d  nor.yy0,&yymsp[-
324e3 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d  1].minor.yy0); }
324e4 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
324e5 20 20 20 20 20 20 63 61 73 65 20 31 34 33 3a 20        case 143: 
324e6 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e  /* on_opt ::= ON
324e7 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
324e8 61 73 65 20 31 35 34 3a 20 2f 2a 20 73 6f 72 74  ase 154: /* sort
324e9 69 74 65 6d 20 3a 3a 3d 20 65 78 70 72 20 2a 2f  item ::= expr */
324ea 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
324eb 6c 65 6e 6f 3d 3d 31 35 34 29 3b 0a 20 20 20 20  leno==154);.    
324ec 20 20 63 61 73 65 20 31 36 31 3a 20 2f 2a 20 68    case 161: /* h
324ed 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48 41  aving_opt ::= HA
324ee 56 49 4e 47 20 65 78 70 72 20 2a 2f 20 79 79 74  VING expr */ yyt
324ef 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
324f0 3d 3d 31 36 31 29 3b 0a 20 20 20 20 20 20 63 61  ==161);.      ca
324f1 73 65 20 31 36 38 3a 20 2f 2a 20 77 68 65 72 65  se 168: /* where
324f2 5f 6f 70 74 20 3a 3a 3d 20 57 48 45 52 45 20 65  _opt ::= WHERE e
324f3 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  xpr */ yytestcas
324f4 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 38 29  e(yyruleno==168)
324f5 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 35  ;.      case 235
324f6 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a  : /* case_else :
324f7 3a 3d 20 45 4c 53 45 20 65 78 70 72 20 2a 2f 20  := ELSE expr */ 
324f8 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
324f9 65 6e 6f 3d 3d 32 33 35 29 3b 0a 20 20 20 20 20  eno==235);.     
324fa 20 63 61 73 65 20 32 33 37 3a 20 2f 2a 20 63 61   case 237: /* ca
324fb 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65  se_operand ::= e
324fc 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  xpr */ yytestcas
324fd 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 37 29  e(yyruleno==237)
324fe 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;.{yygotominor.y
324ff 79 31 33 32 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y132 = yymsp[0].
32500 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32501 72 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  r;}.        brea
32502 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  k;.      case 14
32503 34 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d  4: /* on_opt ::=
32504 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
32505 36 30 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70  60: /* having_op
32506 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
32507 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36  ase(yyruleno==16
32508 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  0);.      case 1
32509 36 37 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74  67: /* where_opt
3250a 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
3250b 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 37  se(yyruleno==167
3250c 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  );.      case 23
3250d 36 3a 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20  6: /* case_else 
3250e 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
3250f 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 36 29  e(yyruleno==236)
32510 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 38  ;.      case 238
32511 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e  : /* case_operan
32512 64 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  d ::= */ yytestc
32513 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33  ase(yyruleno==23
32514 38 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  8);.{yygotominor
32515 2e 79 79 31 33 32 20 3d 20 30 3b 7d 0a 20 20 20  .yy132 = 0;}.   
32516 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32517 20 20 63 61 73 65 20 31 34 37 3a 20 2f 2a 20 69    case 147: /* i
32518 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e  ndexed_opt ::= N
32519 4f 54 20 49 4e 44 45 58 45 44 20 2a 2f 0a 7b 79  OT INDEXED */.{y
3251a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a  ygotominor.yy0.z
3251b 3d 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  =0; yygotominor.
3251c 79 79 30 2e 6e 3d 31 3b 7d 0a 20 20 20 20 20 20  yy0.n=1;}.      
3251d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3251e 61 73 65 20 31 34 38 3a 20 2f 2a 20 75 73 69 6e  ase 148: /* usin
3251f 67 5f 6f 70 74 20 3a 3a 3d 20 55 53 49 4e 47 20  g_opt ::= USING 
32520 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50  LP inscollist RP
32521 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
32522 38 30 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73  80: /* inscollis
32523 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 6e 73  t_opt ::= LP ins
32524 63 6f 6c 6c 69 73 74 20 52 50 20 2a 2f 20 79 79  collist RP */ yy
32525 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32526 6f 3d 3d 31 38 30 29 3b 0a 7b 79 79 67 6f 74 6f  o==180);.{yygoto
32527 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d 20 79 79  minor.yy408 = yy
32528 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
32529 34 30 38 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  408;}.        br
3252a 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3252b 31 34 39 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70  149: /* using_op
3252c 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  t ::= */.      c
3252d 61 73 65 20 31 37 39 3a 20 2f 2a 20 69 6e 73 63  ase 179: /* insc
3252e 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a  ollist_opt ::= *
3252f 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32530 75 6c 65 6e 6f 3d 3d 31 37 39 29 3b 0a 7b 79 79  uleno==179);.{yy
32531 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 38 20  gotominor.yy408 
32532 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 0;}.        br
32533 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32534 31 35 31 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f  151: /* orderby_
32535 6f 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20 42 59  opt ::= ORDER BY
32536 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20   sortlist */.   
32537 20 20 20 63 61 73 65 20 31 35 39 3a 20 2f 2a 20     case 159: /* 
32538 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20  groupby_opt ::= 
32539 47 52 4f 55 50 20 42 59 20 6e 65 78 70 72 6c 69  GROUP BY nexprli
3253a 73 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  st */ yytestcase
3253b 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 35 39 29 3b  (yyruleno==159);
3253c 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 39 3a  .      case 239:
3253d 20 2f 2a 20 65 78 70 72 6c 69 73 74 20 3a 3a 3d   /* exprlist ::=
3253e 20 6e 65 78 70 72 6c 69 73 74 20 2a 2f 20 79 79   nexprlist */ yy
3253f 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32540 6f 3d 3d 32 33 39 29 3b 0a 7b 79 79 67 6f 74 6f  o==239);.{yygoto
32541 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 79 79 6d  minor.yy14 = yym
32542 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34  sp[0].minor.yy14
32543 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
32544 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 32  ;.      case 152
32545 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a  : /* sortlist ::
32546 3d 20 73 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41  = sortlist COMMA
32547 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72   sortitem sortor
32548 64 65 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  der */.{.  yygot
32549 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71  ominor.yy14 = sq
3254a 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
3254b 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70  end(pParse,yymsp
3254c 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-3].minor.yy14,
3254d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3254e 79 79 31 33 32 29 3b 0a 20 20 69 66 28 20 79 79  yy132);.  if( yy
3254f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 29  gotominor.yy14 )
32550 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
32551 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72  4->a[yygotominor
32552 2e 79 79 31 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e  .yy14->nExpr-1].
32553 73 6f 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29  sortOrder = (u8)
32554 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32555 79 33 32 38 3b 0a 7d 0a 20 20 20 20 20 20 20 20  y328;.}.        
32556 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32557 65 20 31 35 33 3a 20 2f 2a 20 73 6f 72 74 6c 69  e 153: /* sortli
32558 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74 65 6d 20  st ::= sortitem 
32559 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20  sortorder */.{. 
3255a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
3255b 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  4 = sqlite3ExprL
3255c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
3255d 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  ,0,yymsp[-1].min
3255e 6f 72 2e 79 79 31 33 32 29 3b 0a 20 20 69 66 28  or.yy132);.  if(
3255f 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
32560 34 20 26 26 20 41 4c 57 41 59 53 28 79 79 67 6f  4 && ALWAYS(yygo
32561 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 61 29  tominor.yy14->a)
32562 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
32563 79 31 34 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72  y14->a[0].sortOr
32564 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b  der = (u8)yymsp[
32565 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a  0].minor.yy328;.
32566 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32567 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 35 3a  .      case 155:
32568 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a   /* sortorder ::
32569 3d 20 41 53 43 20 2a 2f 0a 20 20 20 20 20 20 63  = ASC */.      c
3256a 61 73 65 20 31 35 37 3a 20 2f 2a 20 73 6f 72 74  ase 157: /* sort
3256b 6f 72 64 65 72 20 3a 3a 3d 20 2a 2f 20 79 79 74  order ::= */ yyt
3256c 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3256d 3d 3d 31 35 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==157);.{yygotom
3256e 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 53 51 4c  inor.yy328 = SQL
3256f 49 54 45 5f 53 4f 5f 41 53 43 3b 7d 0a 20 20 20  ITE_SO_ASC;}.   
32570 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32571 20 20 63 61 73 65 20 31 35 36 3a 20 2f 2a 20 73    case 156: /* s
32572 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53  ortorder ::= DES
32573 43 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  C */.{yygotomino
32574 72 2e 79 79 33 32 38 20 3d 20 53 51 4c 49 54 45  r.yy328 = SQLITE
32575 5f 53 4f 5f 44 45 53 43 3b 7d 0a 20 20 20 20 20  _SO_DESC;}.     
32576 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32577 63 61 73 65 20 31 36 32 3a 20 2f 2a 20 6c 69 6d  case 162: /* lim
32578 69 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79  it_opt ::= */.{y
32579 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36  ygotominor.yy476
3257a 2e 70 4c 69 6d 69 74 20 3d 20 30 3b 20 79 79 67  .pLimit = 0; yyg
3257b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70  otominor.yy476.p
3257c 4f 66 66 73 65 74 20 3d 20 30 3b 7d 0a 20 20 20  Offset = 0;}.   
3257d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3257e 20 20 63 61 73 65 20 31 36 33 3a 20 2f 2a 20 6c    case 163: /* l
3257f 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d  imit_opt ::= LIM
32580 49 54 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f  IT expr */.{yygo
32581 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c  tominor.yy476.pL
32582 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  imit = yymsp[0].
32583 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32584 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  r; yygotominor.y
32585 79 34 37 36 2e 70 4f 66 66 73 65 74 20 3d 20 30  y476.pOffset = 0
32586 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
32587 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 34  ;.      case 164
32588 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a  : /* limit_opt :
32589 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72 20 4f 46  := LIMIT expr OF
3258a 46 53 45 54 20 65 78 70 72 20 2a 2f 0a 7b 79 79  FSET expr */.{yy
3258b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e  gotominor.yy476.
3258c 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 2d  pLimit = yymsp[-
3258d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  2].minor.yy346.p
3258e 45 78 70 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  Expr; yygotomino
3258f 72 2e 79 79 34 37 36 2e 70 4f 66 66 73 65 74 20  r.yy476.pOffset 
32590 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
32591 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 7d 0a 20  .yy346.pExpr;}. 
32592 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32593 20 20 20 20 63 61 73 65 20 31 36 35 3a 20 2f 2a      case 165: /*
32594 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c   limit_opt ::= L
32595 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d 41 20  IMIT expr COMMA 
32596 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  expr */.{yygotom
32597 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4f 66 66 73  inor.yy476.pOffs
32598 65 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  et = yymsp[-2].m
32599 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3259a 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
3259b 34 37 36 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d  476.pLimit = yym
3259c 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
3259d 36 2e 70 45 78 70 72 3b 7d 0a 20 20 20 20 20 20  6.pExpr;}.      
3259e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3259f 61 73 65 20 31 36 36 3a 20 2f 2a 20 63 6d 64 20  ase 166: /* cmd 
325a0 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  ::= DELETE FROM 
325a1 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64  fullname indexed
325a2 5f 6f 70 74 20 77 68 65 72 65 5f 6f 70 74 20 2a  _opt where_opt *
325a3 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  /.{.  sqlite3Src
325a4 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 70 50  ListIndexedBy(pP
325a5 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  arse, yymsp[-2].
325a6 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 26 79 79 6d  minor.yy65, &yym
325a7 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
325a8 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65  );.  sqlite3Dele
325a9 74 65 46 72 6f 6d 28 70 50 61 72 73 65 2c 79 79  teFrom(pParse,yy
325aa 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
325ab 36 35 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  65,yymsp[0].mino
325ac 72 2e 79 79 31 33 32 29 3b 0a 7d 0a 20 20 20 20  r.yy132);.}.    
325ad 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
325ae 20 63 61 73 65 20 31 36 39 3a 20 2f 2a 20 63 6d   case 169: /* cm
325af 64 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63  d ::= UPDATE orc
325b0 6f 6e 66 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64  onf fullname ind
325b1 65 78 65 64 5f 6f 70 74 20 53 45 54 20 73 65 74  exed_opt SET set
325b2 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74 20 2a  list where_opt *
325b3 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 72 63  /.{.  sqlite3Src
325b4 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 70 50  ListIndexedBy(pP
325b5 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  arse, yymsp[-4].
325b6 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 26 79 79 6d  minor.yy65, &yym
325b7 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
325b8 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
325b9 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28  ListCheckLength(
325ba 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d  pParse,yymsp[-1]
325bb 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 22 73 65 74  .minor.yy14,"set
325bc 20 6c 69 73 74 22 29 3b 20 0a 20 20 73 71 6c 69   list"); .  sqli
325bd 74 65 33 55 70 64 61 74 65 28 70 50 61 72 73 65  te3Update(pParse
325be 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
325bf 2e 79 79 36 35 2c 79 79 6d 73 70 5b 2d 31 5d 2e  .yy65,yymsp[-1].
325c0 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70  minor.yy14,yymsp
325c1 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c  [0].minor.yy132,
325c2 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
325c3 79 79 31 38 36 29 3b 0a 7d 0a 20 20 20 20 20 20  yy186);.}.      
325c4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
325c5 61 73 65 20 31 37 30 3a 20 2f 2a 20 73 65 74 6c  ase 170: /* setl
325c6 69 73 74 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20  ist ::= setlist 
325c7 43 4f 4d 4d 41 20 6e 6d 20 45 51 20 65 78 70 72  COMMA nm EQ expr
325c8 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
325c9 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
325ca 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
325cb 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
325cc 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79  4].minor.yy14, y
325cd 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
325ce 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 73 71  346.pExpr);.  sq
325cf 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
325d0 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 79 79 67  Name(pParse, yyg
325d1 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26  otominor.yy14, &
325d2 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
325d3 79 79 30 2c 20 31 29 3b 0a 7d 0a 20 20 20 20 20  yy0, 1);.}.     
325d4 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
325d5 63 61 73 65 20 31 37 31 3a 20 2f 2a 20 73 65 74  case 171: /* set
325d6 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65  list ::= nm EQ e
325d7 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  xpr */.{.  yygot
325d8 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71  ominor.yy14 = sq
325d9 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
325da 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 79  end(pParse, 0, y
325db 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
325dc 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 73 71  346.pExpr);.  sq
325dd 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53 65 74  lite3ExprListSet
325de 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 79 79 67  Name(pParse, yyg
325df 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26  otominor.yy14, &
325e0 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
325e1 79 79 30 2c 20 31 29 3b 0a 7d 0a 20 20 20 20 20  yy0, 1);.}.     
325e2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
325e3 63 61 73 65 20 31 37 32 3a 20 2f 2a 20 63 6d 64  case 172: /* cmd
325e4 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20   ::= insert_cmd 
325e5 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e  INTO fullname in
325e6 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c  scollist_opt VAL
325e7 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20  UES LP itemlist 
325e8 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e  RP */.{sqlite3In
325e9 73 65 72 74 28 70 50 61 72 73 65 2c 20 79 79 6d  sert(pParse, yym
325ea 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 36  sp[-5].minor.yy6
325eb 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  5, yymsp[-1].min
325ec 6f 72 2e 79 79 31 34 2c 20 30 2c 20 79 79 6d 73  or.yy14, 0, yyms
325ed 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  p[-4].minor.yy40
325ee 38 2c 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e  8, yymsp[-7].min
325ef 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20 20 20 20  or.yy186);}.    
325f0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
325f1 20 63 61 73 65 20 31 37 33 3a 20 2f 2a 20 63 6d   case 173: /* cm
325f2 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
325f3 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69   INTO fullname i
325f4 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65  nscollist_opt se
325f5 6c 65 63 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  lect */.{sqlite3
325f6 49 6e 73 65 72 74 28 70 50 61 72 73 65 2c 20 79  Insert(pParse, y
325f7 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
325f8 79 36 35 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d  y65, 0, yymsp[0]
325f9 2e 6d 69 6e 6f 72 2e 79 79 33 2c 20 79 79 6d 73  .minor.yy3, yyms
325fa 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  p[-1].minor.yy40
325fb 38 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  8, yymsp[-4].min
325fc 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20 20 20 20  or.yy186);}.    
325fd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
325fe 20 63 61 73 65 20 31 37 34 3a 20 2f 2a 20 63 6d   case 174: /* cm
325ff 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
32600 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69   INTO fullname i
32601 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 44 45  nscollist_opt DE
32602 46 41 55 4c 54 20 56 41 4c 55 45 53 20 2a 2f 0a  FAULT VALUES */.
32603 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70  {sqlite3Insert(p
32604 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 33 5d  Parse, yymsp[-3]
32605 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20 30 2c 20  .minor.yy65, 0, 
32606 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  0, yymsp[-2].min
32607 6f 72 2e 79 79 34 30 38 2c 20 79 79 6d 73 70 5b  or.yy408, yymsp[
32608 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 29  -5].minor.yy186)
32609 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3260a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 35  ;.      case 175
3260b 3a 20 2f 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20  : /* insert_cmd 
3260c 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72 63 6f 6e  ::= INSERT orcon
3260d 66 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  f */.{yygotomino
3260e 72 2e 79 79 31 38 36 20 3d 20 79 79 6d 73 70 5b  r.yy186 = yymsp[
3260f 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 3b 7d  0].minor.yy186;}
32610 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32611 20 20 20 20 20 20 63 61 73 65 20 31 37 36 3a 20        case 176: 
32612 2f 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a  /* insert_cmd ::
32613 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a 7b 79 79  = REPLACE */.{yy
32614 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 36 20  gotominor.yy186 
32615 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20  = OE_Replace;}. 
32616 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32617 20 20 20 20 63 61 73 65 20 31 37 37 3a 20 2f 2a      case 177: /*
32618 20 69 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69 74   itemlist ::= it
32619 65 6d 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78 70  emlist COMMA exp
3261a 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  r */.      case 
3261b 32 34 31 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73  241: /* nexprlis
3261c 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20  t ::= nexprlist 
3261d 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 20 79 79  COMMA expr */ yy
3261e 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3261f 6f 3d 3d 32 34 31 29 3b 0a 7b 79 79 67 6f 74 6f  o==241);.{yygoto
32620 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c  minor.yy14 = sql
32621 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
32622 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  nd(pParse,yymsp[
32623 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79  -2].minor.yy14,y
32624 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32625 33 34 36 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20  346.pExpr);}.   
32626 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32627 20 20 63 61 73 65 20 31 37 38 3a 20 2f 2a 20 69    case 178: /* i
32628 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72  temlist ::= expr
32629 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
3262a 34 32 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74  42: /* nexprlist
3262b 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 20 79 79 74   ::= expr */ yyt
3262c 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3262d 3d 3d 32 34 32 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==242);.{yygotom
3262e 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
3262f 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
32630 64 28 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70  d(pParse,0,yymsp
32631 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
32632 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20  pExpr);}.       
32633 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32634 73 65 20 31 38 31 3a 20 2f 2a 20 69 6e 73 63 6f  se 181: /* insco
32635 6c 6c 69 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c  llist ::= inscol
32636 6c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 2a 2f  list COMMA nm */
32637 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
32638 34 30 38 20 3d 20 73 71 6c 69 74 65 33 49 64 4c  408 = sqlite3IdL
32639 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
3263a 2d 3e 64 62 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  ->db,yymsp[-2].m
3263b 69 6e 6f 72 2e 79 79 34 30 38 2c 26 79 79 6d 73  inor.yy408,&yyms
3263c 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
3263d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3263e 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 32 3a  .      case 182:
3263f 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a   /* inscollist :
32640 3a 3d 20 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f  := nm */.{yygoto
32641 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d 20 73 71  minor.yy408 = sq
32642 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e  lite3IdListAppen
32643 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26  d(pParse->db,0,&
32644 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32645 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
32646 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32647 31 38 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  183: /* expr ::=
32648 20 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 63   term */.      c
32649 61 73 65 20 32 31 31 3a 20 2f 2a 20 65 73 63 61  ase 211: /* esca
3264a 70 65 20 3a 3a 3d 20 45 53 43 41 50 45 20 65 78  pe ::= ESCAPE ex
3264b 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  pr */ yytestcase
3264c 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 31 31 29 3b  (yyruleno==211);
3264d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
3264e 33 34 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  346 = yymsp[0].m
3264f 69 6e 6f 72 2e 79 79 33 34 36 3b 7d 0a 20 20 20  inor.yy346;}.   
32650 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32651 20 20 63 61 73 65 20 31 38 34 3a 20 2f 2a 20 65    case 184: /* e
32652 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20  xpr ::= LP expr 
32653 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  RP */.{yygotomin
32654 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
32655 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
32656 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 20 73 70  .yy346.pExpr; sp
32657 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  anSet(&yygotomin
32658 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b  or.yy346,&yymsp[
32659 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -2].minor.yy0,&y
3265a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3265b 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
3265c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
3265d 38 35 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20  85: /* term ::= 
3265e 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61  NULL */.      ca
3265f 73 65 20 31 39 30 3a 20 2f 2a 20 74 65 72 6d 20  se 190: /* term 
32660 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41  ::= INTEGER|FLOA
32661 54 7c 42 4c 4f 42 20 2a 2f 20 79 79 74 65 73 74  T|BLOB */ yytest
32662 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
32663 39 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  90);.      case 
32664 31 39 31 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d  191: /* term ::=
32665 20 53 54 52 49 4e 47 20 2a 2f 20 79 79 74 65 73   STRING */ yytes
32666 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32667 31 39 31 29 3b 0a 7b 73 70 61 6e 45 78 70 72 28  191);.{spanExpr(
32668 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
32669 34 36 2c 20 70 50 61 72 73 65 2c 20 79 79 6d 73  46, pParse, yyms
3266a 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 20 26 79 79 6d  p[0].major, &yym
3266b 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
3266c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3266d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 36  ;.      case 186
3266e 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 69 64  : /* expr ::= id
3266f 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
32670 38 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  87: /* expr ::= 
32671 4a 4f 49 4e 5f 4b 57 20 2a 2f 20 79 79 74 65 73  JOIN_KW */ yytes
32672 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32673 31 38 37 29 3b 0a 7b 73 70 61 6e 45 78 70 72 28  187);.{spanExpr(
32674 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
32675 34 36 2c 20 70 50 61 72 73 65 2c 20 54 4b 5f 49  46, pParse, TK_I
32676 44 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  D, &yymsp[0].min
32677 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
32678 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32679 61 73 65 20 31 38 38 3a 20 2f 2a 20 65 78 70 72  ase 188: /* expr
3267a 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a   ::= nm DOT nm *
3267b 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  /.{.  Expr *temp
3267c 31 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  1 = sqlite3PExpr
3267d 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
3267e 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  0, 0, &yymsp[-2]
3267f 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
32680 78 70 72 20 2a 74 65 6d 70 32 20 3d 20 73 71 6c  xpr *temp2 = sql
32681 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32682 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
32683 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32684 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  y0);.  yygotomin
32685 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
32686 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
32687 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65  arse, TK_DOT, te
32688 6d 70 31 2c 20 74 65 6d 70 32 2c 20 30 29 3b 0a  mp1, temp2, 0);.
32689 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74    spanSet(&yygot
3268a 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79  ominor.yy346,&yy
3268b 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
3268c 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
3268d 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
3268e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3268f 61 73 65 20 31 38 39 3a 20 2f 2a 20 65 78 70 72  ase 189: /* expr
32690 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44   ::= nm DOT nm D
32691 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70  OT nm */.{.  Exp
32692 72 20 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74  r *temp1 = sqlit
32693 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
32694 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
32695 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
32696 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70  0);.  Expr *temp
32697 32 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  2 = sqlite3PExpr
32698 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
32699 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  0, 0, &yymsp[-2]
3269a 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45  .minor.yy0);.  E
3269b 78 70 72 20 2a 74 65 6d 70 33 20 3d 20 73 71 6c  xpr *temp3 = sql
3269c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
3269d 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
3269e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3269f 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d  y0);.  Expr *tem
326a0 70 34 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  p4 = sqlite3PExp
326a1 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54  r(pParse, TK_DOT
326a2 2c 20 74 65 6d 70 32 2c 20 74 65 6d 70 33 2c 20  , temp2, temp3, 
326a3 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  0);.  yygotomino
326a4 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
326a5 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
326a6 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d  rse, TK_DOT, tem
326a7 70 31 2c 20 74 65 6d 70 34 2c 20 30 29 3b 0a 20  p1, temp4, 0);. 
326a8 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f   spanSet(&yygoto
326a9 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d  minor.yy346,&yym
326aa 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-4].minor.yy0
326ab 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
326ac 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
326ad 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
326ae 73 65 20 31 39 32 3a 20 2f 2a 20 65 78 70 72 20  se 192: /* expr 
326af 3a 3a 3d 20 52 45 47 49 53 54 45 52 20 2a 2f 0a  ::= REGISTER */.
326b0 7b 0a 20 20 2f 2a 20 57 68 65 6e 20 64 6f 69 6e  {.  /* When doin
326b1 67 20 61 20 6e 65 73 74 65 64 20 70 61 72 73 65  g a nested parse
326b2 2c 20 6f 6e 65 20 63 61 6e 20 69 6e 63 6c 75 64  , one can includ
326b3 65 20 74 65 72 6d 73 20 69 6e 20 61 6e 20 65 78  e terms in an ex
326b4 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 74 68  pression.  ** th
326b5 61 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 74 68 69  at look like thi
326b6 73 3a 20 20 20 23 31 20 23 32 20 2e 2e 2e 20 20  s:   #1 #2 ...  
326b7 54 68 65 73 65 20 74 65 72 6d 73 20 72 65 66 65  These terms refe
326b8 72 20 74 6f 20 72 65 67 69 73 74 65 72 73 0a 20  r to registers. 
326b9 20 2a 2a 20 69 6e 20 74 68 65 20 76 69 72 74 75   ** in the virtu
326ba 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20 23 4e 20  al machine.  #N 
326bb 69 73 20 74 68 65 20 4e 2d 74 68 20 72 65 67 69  is the N-th regi
326bc 73 74 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ster. */.  if( p
326bd 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
326be 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
326bf 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
326c0 22 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79  "near \"%T\": sy
326c1 6e 74 61 78 20 65 72 72 6f 72 22 2c 20 26 79 79  ntax error", &yy
326c2 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
326c3 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  );.    yygotomin
326c4 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
326c5 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
326c6 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
326c7 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  46.pExpr = sqlit
326c8 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
326c9 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 30 2c 20  TK_REGISTER, 0, 
326ca 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
326cb 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20 69 66 28  or.yy0);.    if(
326cc 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
326cd 34 36 2e 70 45 78 70 72 20 29 20 73 71 6c 69 74  46.pExpr ) sqlit
326ce 65 33 47 65 74 49 6e 74 33 32 28 26 79 79 6d 73  e3GetInt32(&yyms
326cf 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
326d0 5b 31 5d 2c 20 26 79 79 67 6f 74 6f 6d 69 6e 6f  [1], &yygotomino
326d1 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 69  r.yy346.pExpr->i
326d2 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 73 70  Table);.  }.  sp
326d3 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  anSet(&yygotomin
326d4 6f 72 2e 79 79 33 34 36 2c 20 26 79 79 6d 73 70  or.yy346, &yymsp
326d5 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  [0].minor.yy0, &
326d6 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
326d7 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
326d8 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
326d9 20 31 39 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   193: /* expr ::
326da 3d 20 56 41 52 49 41 42 4c 45 20 2a 2f 0a 7b 0a  = VARIABLE */.{.
326db 20 20 73 70 61 6e 45 78 70 72 28 26 79 79 67 6f    spanExpr(&yygo
326dc 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20 70  tominor.yy346, p
326dd 50 61 72 73 65 2c 20 54 4b 5f 56 41 52 49 41 42  Parse, TK_VARIAB
326de 4c 45 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  LE, &yymsp[0].mi
326df 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69  nor.yy0);.  sqli
326e0 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 72  te3ExprAssignVar
326e1 4e 75 6d 62 65 72 28 70 50 61 72 73 65 2c 20 79  Number(pParse, y
326e2 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
326e3 2e 70 45 78 70 72 29 3b 0a 20 20 73 70 61 6e 53  .pExpr);.  spanS
326e4 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  et(&yygotominor.
326e5 79 79 33 34 36 2c 20 26 79 79 6d 73 70 5b 30 5d  yy346, &yymsp[0]
326e6 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d  .minor.yy0, &yym
326e7 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
326e8 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
326e9 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  k;.      case 19
326ea 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  4: /* expr ::= e
326eb 78 70 72 20 43 4f 4c 4c 41 54 45 20 69 64 73 20  xpr COLLATE ids 
326ec 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
326ed 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
326ee 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43   sqlite3ExprSetC
326ef 6f 6c 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73  oll(pParse, yyms
326f0 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-2].minor.yy34
326f1 36 2e 70 45 78 70 72 2c 20 26 79 79 6d 73 70 5b  6.pExpr, &yymsp[
326f2 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  0].minor.yy0);. 
326f3 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
326f4 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73  46.zStart = yyms
326f5 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-2].minor.yy34
326f6 36 2e 7a 53 74 61 72 74 3b 0a 20 20 79 79 67 6f  6.zStart;.  yygo
326f7 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45  tominor.yy346.zE
326f8 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  nd = &yymsp[0].m
326f9 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70  inor.yy0.z[yymsp
326fa 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d  [0].minor.yy0.n]
326fb 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
326fc 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  k;.      case 19
326fd 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 43  5: /* expr ::= C
326fe 41 53 54 20 4c 50 20 65 78 70 72 20 41 53 20 74  AST LP expr AS t
326ff 79 70 65 74 6f 6b 65 6e 20 52 50 20 2a 2f 0a 7b  ypetoken RP */.{
32700 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
32701 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
32702 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32703 2c 20 54 4b 5f 43 41 53 54 2c 20 79 79 6d 73 70  , TK_CAST, yymsp
32704 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-3].minor.yy346
32705 2e 70 45 78 70 72 2c 20 30 2c 20 26 79 79 6d 73  .pExpr, 0, &yyms
32706 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
32707 3b 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67  ;.  spanSet(&yyg
32708 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26  otominor.yy346,&
32709 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
3270a 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
3270b 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
3270c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3270d 20 63 61 73 65 20 31 39 36 3a 20 2f 2a 20 65 78   case 196: /* ex
3270e 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 64 69 73  pr ::= ID LP dis
3270f 74 69 6e 63 74 20 65 78 70 72 6c 69 73 74 20 52  tinct exprlist R
32710 50 20 2a 2f 0a 7b 0a 20 20 69 66 28 20 79 79 6d  P */.{.  if( yym
32711 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
32712 34 20 26 26 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  4 && yymsp[-1].m
32713 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72  inor.yy14->nExpr
32714 3e 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69  >pParse->db->aLi
32715 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
32716 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 5d 20 29  _FUNCTION_ARG] )
32717 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
32718 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
32719 6f 6f 20 6d 61 6e 79 20 61 72 67 75 6d 65 6e 74  oo many argument
3271a 73 20 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 25 54  s on function %T
3271b 22 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  ", &yymsp[-4].mi
3271c 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20  nor.yy0);.  }.  
3271d 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3271e 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65  6.pExpr = sqlite
3271f 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50  3ExprFunction(pP
32720 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  arse, yymsp[-1].
32721 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d  minor.yy14, &yym
32722 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-4].minor.yy0
32723 29 3b 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79  );.  spanSet(&yy
32724 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
32725 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
32726 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
32727 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28  inor.yy0);.  if(
32728 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
32729 2e 79 79 33 32 38 20 26 26 20 79 79 67 6f 74 6f  .yy328 && yygoto
3272a 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
3272b 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  r ){.    yygotom
3272c 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3272d 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 44 69  ->flags |= EP_Di
3272e 73 74 69 6e 63 74 3b 0a 20 20 7d 0a 7d 0a 20 20  stinct;.  }.}.  
3272f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32730 20 20 20 63 61 73 65 20 31 39 37 3a 20 2f 2a 20     case 197: /* 
32731 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53  expr ::= ID LP S
32732 54 41 52 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79  TAR RP */.{.  yy
32733 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32734 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  pExpr = sqlite3E
32735 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  xprFunction(pPar
32736 73 65 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 33  se, 0, &yymsp[-3
32737 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
32738 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
32739 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73  inor.yy346,&yyms
3273a 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-3].minor.yy0,
3273b 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3273c 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
3273d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3273e 65 20 31 39 38 3a 20 2f 2a 20 74 65 72 6d 20 3a  e 198: /* term :
3273f 3a 3d 20 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 7b  := CTIME_KW */.{
32740 0a 20 20 2f 2a 20 54 68 65 20 43 55 52 52 45 4e  .  /* The CURREN
32741 54 5f 54 49 4d 45 2c 20 43 55 52 52 45 4e 54 5f  T_TIME, CURRENT_
32742 44 41 54 45 2c 20 61 6e 64 20 43 55 52 52 45 4e  DATE, and CURREN
32743 54 5f 54 49 4d 45 53 54 41 4d 50 20 76 61 6c 75  T_TIMESTAMP valu
32744 65 73 20 61 72 65 0a 20 20 2a 2a 20 74 72 65 61  es are.  ** trea
32745 74 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 73  ted as functions
32746 20 74 68 61 74 20 72 65 74 75 72 6e 20 63 6f 6e   that return con
32747 73 74 61 6e 74 73 20 2a 2f 0a 20 20 79 79 67 6f  stants */.  yygo
32748 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32749 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70  xpr = sqlite3Exp
3274a 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65  rFunction(pParse
3274b 2c 20 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  , 0,&yymsp[0].mi
3274c 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28 20  nor.yy0);.  if( 
3274d 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
3274e 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 79  6.pExpr ){.    y
3274f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32750 2e 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f  .pExpr->op = TK_
32751 43 4f 4e 53 54 5f 46 55 4e 43 3b 20 20 0a 20 20  CONST_FUNC;  .  
32752 7d 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67  }.  spanSet(&yyg
32753 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20  otominor.yy346, 
32754 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
32755 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  yy0, &yymsp[0].m
32756 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
32757 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32758 20 20 63 61 73 65 20 31 39 39 3a 20 2f 2a 20 65    case 199: /* e
32759 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44  xpr ::= expr AND
3275a 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
3275b 61 73 65 20 32 30 30 3a 20 2f 2a 20 65 78 70 72  ase 200: /* expr
3275c 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65 78 70   ::= expr OR exp
3275d 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
3275e 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 30 29 3b 0a  yyruleno==200);.
3275f 20 20 20 20 20 20 63 61 73 65 20 32 30 31 3a 20        case 201: 
32760 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
32761 20 4c 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70   LT|GT|GE|LE exp
32762 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
32763 79 79 72 75 6c 65 6e 6f 3d 3d 32 30 31 29 3b 0a  yyruleno==201);.
32764 20 20 20 20 20 20 63 61 73 65 20 32 30 32 3a 20        case 202: 
32765 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
32766 20 45 51 7c 4e 45 20 65 78 70 72 20 2a 2f 20 79   EQ|NE expr */ y
32767 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32768 6e 6f 3d 3d 32 30 32 29 3b 0a 20 20 20 20 20 20  no==202);.      
32769 63 61 73 65 20 32 30 33 3a 20 2f 2a 20 65 78 70  case 203: /* exp
3276a 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e  r ::= expr BITAN
3276b 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52  D|BITOR|LSHIFT|R
3276c 53 48 49 46 54 20 65 78 70 72 20 2a 2f 20 79 79  SHIFT expr */ yy
3276d 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
3276e 6f 3d 3d 32 30 33 29 3b 0a 20 20 20 20 20 20 63  o==203);.      c
3276f 61 73 65 20 32 30 34 3a 20 2f 2a 20 65 78 70 72  ase 204: /* expr
32770 20 3a 3a 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d   ::= expr PLUS|M
32771 49 4e 55 53 20 65 78 70 72 20 2a 2f 20 79 79 74  INUS expr */ yyt
32772 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32773 3d 3d 32 30 34 29 3b 0a 20 20 20 20 20 20 63 61  ==204);.      ca
32774 73 65 20 32 30 35 3a 20 2f 2a 20 65 78 70 72 20  se 205: /* expr 
32775 3a 3a 3d 20 65 78 70 72 20 53 54 41 52 7c 53 4c  ::= expr STAR|SL
32776 41 53 48 7c 52 45 4d 20 65 78 70 72 20 2a 2f 20  ASH|REM expr */ 
32777 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32778 65 6e 6f 3d 3d 32 30 35 29 3b 0a 20 20 20 20 20  eno==205);.     
32779 20 63 61 73 65 20 32 30 36 3a 20 2f 2a 20 65 78   case 206: /* ex
3277a 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43  pr ::= expr CONC
3277b 41 54 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  AT expr */ yytes
3277c 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3277d 32 30 36 29 3b 0a 7b 73 70 61 6e 42 69 6e 61 72  206);.{spanBinar
3277e 79 45 78 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e  yExpr(&yygotomin
3277f 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65 2c  or.yy346,pParse,
32780 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c  yymsp[-1].major,
32781 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
32782 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d  .yy346,&yymsp[0]
32783 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 7d 0a  .minor.yy346);}.
32784 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32785 20 20 20 20 20 63 61 73 65 20 32 30 37 3a 20 2f       case 207: /
32786 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b  * likeop ::= LIK
32787 45 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61  E_KW */.      ca
32788 73 65 20 32 30 39 3a 20 2f 2a 20 6c 69 6b 65 6f  se 209: /* likeo
32789 70 20 3a 3a 3d 20 4d 41 54 43 48 20 2a 2f 20 79  p ::= MATCH */ y
3278a 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3278b 6e 6f 3d 3d 32 30 39 29 3b 0a 7b 79 79 67 6f 74  no==209);.{yygot
3278c 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65  ominor.yy96.eOpe
3278d 72 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d  rator = yymsp[0]
3278e 2e 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f  .minor.yy0; yygo
3278f 74 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74  tominor.yy96.not
32790 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
32791 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32792 20 32 30 38 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20   208: /* likeop 
32793 3a 3a 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 20  ::= NOT LIKE_KW 
32794 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  */.      case 21
32795 30 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d  0: /* likeop ::=
32796 20 4e 4f 54 20 4d 41 54 43 48 20 2a 2f 20 79 79   NOT MATCH */ yy
32797 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32798 6f 3d 3d 32 31 30 29 3b 0a 7b 79 79 67 6f 74 6f  o==210);.{yygoto
32799 6d 69 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65 72  minor.yy96.eOper
3279a 61 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  ator = yymsp[0].
3279b 6d 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74  minor.yy0; yygot
3279c 6f 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74 20  ominor.yy96.not 
3279d 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  = 1;}.        br
3279e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3279f 32 31 32 3a 20 2f 2a 20 65 73 63 61 70 65 20 3a  212: /* escape :
327a0 3a 3d 20 2a 2f 0a 7b 6d 65 6d 73 65 74 28 26 79  := */.{memset(&y
327a1 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
327a2 2c 30 2c 73 69 7a 65 6f 66 28 79 79 67 6f 74 6f  ,0,sizeof(yygoto
327a3 6d 69 6e 6f 72 2e 79 79 33 34 36 29 29 3b 7d 0a  minor.yy346));}.
327a4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
327a5 20 20 20 20 20 63 61 73 65 20 32 31 33 3a 20 2f       case 213: /
327a6 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
327a7 6c 69 6b 65 6f 70 20 65 78 70 72 20 65 73 63 61  likeop expr esca
327a8 70 65 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c 69  pe */.{.  ExprLi
327a9 73 74 20 2a 70 4c 69 73 74 3b 0a 20 20 70 4c 69  st *pList;.  pLi
327aa 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
327ab 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
327ac 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  e,0, yymsp[-1].m
327ad 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
327ae 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  );.  pList = sql
327af 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
327b0 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c  nd(pParse,pList,
327b1 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
327b2 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20  .yy346.pExpr);. 
327b3 20 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69   if( yymsp[0].mi
327b4 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
327b5 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73  ){.    pList = s
327b6 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
327b7 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73  pend(pParse,pLis
327b8 74 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  t, yymsp[0].mino
327b9 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
327ba 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f    }.  yygotomino
327bb 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
327bc 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74  sqlite3ExprFunct
327bd 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 4c 69 73  ion(pParse, pLis
327be 74 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  t, &yymsp[-2].mi
327bf 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65 72 61 74  nor.yy96.eOperat
327c0 6f 72 29 3b 0a 20 20 69 66 28 20 79 79 6d 73 70  or);.  if( yymsp
327c1 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 39 36 2e  [-2].minor.yy96.
327c2 6e 6f 74 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  not ) yygotomino
327c3 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
327c4 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
327c5 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67  rse, TK_NOT, yyg
327c6 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
327c7 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 79  Expr, 0, 0);.  y
327c8 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
327c9 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
327ca 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -3].minor.yy346.
327cb 7a 53 74 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f  zStart;.  yygoto
327cc 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
327cd 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
327ce 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 3b 0a 20  or.yy346.zEnd;. 
327cf 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
327d0 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 20 79  .yy346.pExpr ) y
327d1 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
327d2 2e 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d  .pExpr->flags |=
327d3 20 45 50 5f 49 6e 66 69 78 46 75 6e 63 3b 0a 7d   EP_InfixFunc;.}
327d4 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
327d5 20 20 20 20 20 20 63 61 73 65 20 32 31 34 3a 20        case 214: 
327d6 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
327d7 20 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 20   ISNULL|NOTNULL 
327d8 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79 50 6f 73  */.{spanUnaryPos
327d9 74 66 69 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  tfix(&yygotomino
327da 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65 2c 79  r.yy346,pParse,y
327db 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 26 79  ymsp[0].major,&y
327dc 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
327dd 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  y346,&yymsp[0].m
327de 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
327df 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
327e0 20 63 61 73 65 20 32 31 35 3a 20 2f 2a 20 65 78   case 215: /* ex
327e1 70 72 20 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20  pr ::= expr NOT 
327e2 4e 55 4c 4c 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61  NULL */.{spanUna
327e3 72 79 50 6f 73 74 66 69 78 28 26 79 79 67 6f 74  ryPostfix(&yygot
327e4 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61  ominor.yy346,pPa
327e5 72 73 65 2c 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 26  rse,TK_NOTNULL,&
327e6 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
327e7 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e  yy346,&yymsp[0].
327e8 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
327e9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
327ea 20 20 63 61 73 65 20 32 31 36 3a 20 2f 2a 20 65    case 216: /* e
327eb 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20  xpr ::= expr IS 
327ec 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73 70 61 6e  expr */.{.  span
327ed 42 69 6e 61 72 79 45 78 70 72 28 26 79 79 67 6f  BinaryExpr(&yygo
327ee 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50  tominor.yy346,pP
327ef 61 72 73 65 2c 54 4b 5f 49 53 2c 26 79 79 6d 73  arse,TK_IS,&yyms
327f0 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  p[-2].minor.yy34
327f1 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  6,&yymsp[0].mino
327f2 72 2e 79 79 33 34 36 29 3b 0a 20 20 62 69 6e 61  r.yy346);.  bina
327f3 72 79 54 6f 55 6e 61 72 79 49 66 4e 75 6c 6c 28  ryToUnaryIfNull(
327f4 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
327f5 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
327f6 70 72 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pr, yygotominor.
327f7 79 79 33 34 36 2e 70 45 78 70 72 2c 20 54 4b 5f  yy346.pExpr, TK_
327f8 49 53 4e 55 4c 4c 29 3b 0a 7d 0a 20 20 20 20 20  ISNULL);.}.     
327f9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
327fa 63 61 73 65 20 32 31 37 3a 20 2f 2a 20 65 78 70  case 217: /* exp
327fb 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e 4f  r ::= expr IS NO
327fc 54 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73 70  T expr */.{.  sp
327fd 61 6e 42 69 6e 61 72 79 45 78 70 72 28 26 79 79  anBinaryExpr(&yy
327fe 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  gotominor.yy346,
327ff 70 50 61 72 73 65 2c 54 4b 5f 49 53 4e 4f 54 2c  pParse,TK_ISNOT,
32800 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
32801 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d  .yy346,&yymsp[0]
32802 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 0a 20  .minor.yy346);. 
32803 20 62 69 6e 61 72 79 54 6f 55 6e 61 72 79 49 66   binaryToUnaryIf
32804 4e 75 6c 6c 28 70 50 61 72 73 65 2c 20 79 79 6d  Null(pParse, yym
32805 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
32806 36 2e 70 45 78 70 72 2c 20 79 79 67 6f 74 6f 6d  6.pExpr, yygotom
32807 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
32808 2c 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 29 3b 0a 7d  , TK_NOTNULL);.}
32809 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3280a 20 20 20 20 20 20 63 61 73 65 20 32 31 38 3a 20        case 218: 
3280b 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4e 4f 54 20  /* expr ::= NOT 
3280c 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
3280d 73 65 20 32 31 39 3a 20 2f 2a 20 65 78 70 72 20  se 219: /* expr 
3280e 3a 3a 3d 20 42 49 54 4e 4f 54 20 65 78 70 72 20  ::= BITNOT expr 
3280f 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
32810 72 75 6c 65 6e 6f 3d 3d 32 31 39 29 3b 0a 7b 73  ruleno==219);.{s
32811 70 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28 26  panUnaryPrefix(&
32812 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32813 36 2c 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  6,pParse,yymsp[-
32814 31 5d 2e 6d 61 6a 6f 72 2c 26 79 79 6d 73 70 5b  1].major,&yymsp[
32815 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26  0].minor.yy346,&
32816 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
32817 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
32818 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32819 20 32 32 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   220: /* expr ::
3281a 3d 20 4d 49 4e 55 53 20 65 78 70 72 20 2a 2f 0a  = MINUS expr */.
3281b 7b 73 70 61 6e 55 6e 61 72 79 50 72 65 66 69 78  {spanUnaryPrefix
3281c 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  (&yygotominor.yy
3281d 33 34 36 2c 70 50 61 72 73 65 2c 54 4b 5f 55 4d  346,pParse,TK_UM
3281e 49 4e 55 53 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  INUS,&yymsp[0].m
3281f 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73  inor.yy346,&yyms
32820 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
32821 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
32822 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 31  ;.      case 221
32823 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 50 4c  : /* expr ::= PL
32824 55 53 20 65 78 70 72 20 2a 2f 0a 7b 73 70 61 6e  US expr */.{span
32825 55 6e 61 72 79 50 72 65 66 69 78 28 26 79 79 67  UnaryPrefix(&yyg
32826 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70  otominor.yy346,p
32827 50 61 72 73 65 2c 54 4b 5f 55 50 4c 55 53 2c 26  Parse,TK_UPLUS,&
32828 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32829 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  y346,&yymsp[-1].
3282a 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
3282b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3282c 20 20 63 61 73 65 20 32 32 34 3a 20 2f 2a 20 65    case 224: /* e
3282d 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 62 65 74  xpr ::= expr bet
3282e 77 65 65 6e 5f 6f 70 20 65 78 70 72 20 41 4e 44  ween_op expr AND
3282f 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 45 78 70   expr */.{.  Exp
32830 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 73  rList *pList = s
32831 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
32832 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79  pend(pParse,0, y
32833 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32834 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 70  y346.pExpr);.  p
32835 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
32836 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
32837 72 73 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73 70  rse,pList, yymsp
32838 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
32839 70 45 78 70 72 29 3b 0a 20 20 79 79 67 6f 74 6f  pExpr);.  yygoto
3283a 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
3283b 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
3283c 28 70 50 61 72 73 65 2c 20 54 4b 5f 42 45 54 57  (pParse, TK_BETW
3283d 45 45 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  EEN, yymsp[-4].m
3283e 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3283f 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 79  , 0, 0);.  if( y
32840 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32841 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 79 79  .pExpr ){.    yy
32842 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32843 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3d  pExpr->x.pList =
32844 20 70 4c 69 73 74 3b 0a 20 20 7d 65 6c 73 65 7b   pList;.  }else{
32845 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
32846 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
32847 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20  e->db, pList);. 
32848 20 7d 20 0a 20 20 69 66 28 20 79 79 6d 73 70 5b   } .  if( yymsp[
32849 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20  -3].minor.yy328 
3284a 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ) yygotominor.yy
3284b 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
3284c 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
3284d 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d   TK_NOT, yygotom
3284e 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3284f 2c 20 30 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74  , 0, 0);.  yygot
32850 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74  ominor.yy346.zSt
32851 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e  art = yymsp[-4].
32852 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61  minor.yy346.zSta
32853 72 74 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  rt;.  yygotomino
32854 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 79  r.yy346.zEnd = y
32855 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32856 33 34 36 2e 7a 45 6e 64 3b 0a 7d 0a 20 20 20 20  346.zEnd;.}.    
32857 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32858 20 63 61 73 65 20 32 32 37 3a 20 2f 2a 20 65 78   case 227: /* ex
32859 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f  pr ::= expr in_o
3285a 70 20 4c 50 20 65 78 70 72 6c 69 73 74 20 52 50  p LP exprlist RP
3285b 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f   */.{.    yygoto
3285c 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
3285d 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
3285e 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20  (pParse, TK_IN, 
3285f 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
32860 79 79 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20  yy346.pExpr, 0, 
32861 30 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f  0);.    if( yygo
32862 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32863 78 70 72 20 29 7b 0a 20 20 20 20 20 20 79 79 67  xpr ){.      yyg
32864 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32865 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20  Expr->x.pList = 
32866 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
32867 79 79 31 34 3b 0a 20 20 20 20 20 20 73 71 6c 69  yy14;.      sqli
32868 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
32869 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d  (pParse, yygotom
3286a 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3286b 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
3286c 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c      sqlite3ExprL
3286d 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
3286e 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  ->db, yymsp[-1].
3286f 6d 69 6e 6f 72 2e 79 79 31 34 29 3b 0a 20 20 20  minor.yy14);.   
32870 20 7d 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70   }.    if( yymsp
32871 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-3].minor.yy328
32872 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
32873 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c  y346.pExpr = sql
32874 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
32875 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f  , TK_NOT, yygoto
32876 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32877 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 79 79  r, 0, 0);.    yy
32878 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32879 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d  zStart = yymsp[-
3287a 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  4].minor.yy346.z
3287b 53 74 61 72 74 3b 0a 20 20 20 20 79 79 67 6f 74  Start;.    yygot
3287c 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e  ominor.yy346.zEn
3287d 64 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  d = &yymsp[0].mi
3287e 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b  nor.yy0.z[yymsp[
3287f 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b  0].minor.yy0.n];
32880 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  .  }.        bre
32881 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
32882 32 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  28: /* expr ::= 
32883 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a  LP select RP */.
32884 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  {.    yygotomino
32885 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
32886 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
32887 72 73 65 2c 20 54 4b 5f 53 45 4c 45 43 54 2c 20  rse, TK_SELECT, 
32888 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 0, 0);.    if
32889 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
3288a 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20  346.pExpr ){.   
3288b 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
3288c 79 33 34 36 2e 70 45 78 70 72 2d 3e 78 2e 70 53  y346.pExpr->x.pS
3288d 65 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31  elect = yymsp[-1
3288e 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 20  ].minor.yy3;.   
3288f 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
32890 74 79 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ty(yygotominor.y
32891 79 33 34 36 2e 70 45 78 70 72 2c 20 45 50 5f 78  y346.pExpr, EP_x
32892 49 73 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20  IsSelect);.     
32893 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
32894 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79  eight(pParse, yy
32895 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32896 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73  pExpr);.    }els
32897 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
32898 53 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61  SelectDelete(pPa
32899 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d  rse->db, yymsp[-
3289a 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b 0a 20  1].minor.yy3);. 
3289b 20 20 20 7d 0a 20 20 20 20 79 79 67 6f 74 6f 6d     }.    yygotom
3289c 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72  inor.yy346.zStar
3289d 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  t = yymsp[-2].mi
3289e 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 20 20 79  nor.yy0.z;.    y
3289f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
328a0 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30  .zEnd = &yymsp[0
328a1 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79  ].minor.yy0.z[yy
328a2 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
328a3 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20  .n];.  }.       
328a4 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
328a5 73 65 20 32 32 39 3a 20 2f 2a 20 65 78 70 72 20  se 229: /* expr 
328a6 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c  ::= expr in_op L
328a7 50 20 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b  P select RP */.{
328a8 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
328a9 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
328aa 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
328ab 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70  se, TK_IN, yymsp
328ac 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-4].minor.yy346
328ad 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20  .pExpr, 0, 0);. 
328ae 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e     if( yygotomin
328af 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29  or.yy346.pExpr )
328b0 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69  {.      yygotomi
328b1 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d  nor.yy346.pExpr-
328b2 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d  >x.pSelect = yym
328b3 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
328b4 3b 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 50  ;.      ExprSetP
328b5 72 6f 70 65 72 74 79 28 79 79 67 6f 74 6f 6d 69  roperty(yygotomi
328b6 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c  nor.yy346.pExpr,
328b7 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a   EP_xIsSelect);.
328b8 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
328b9 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73  rSetHeight(pPars
328ba 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  e, yygotominor.y
328bb 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20  y346.pExpr);.   
328bc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
328bd 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
328be 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79  e(pParse->db, yy
328bf 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
328c0 33 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  3);.    }.    if
328c1 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ( yymsp[-3].mino
328c2 72 2e 79 79 33 32 38 20 29 20 79 79 67 6f 74 6f  r.yy328 ) yygoto
328c3 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
328c4 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
328c5 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c  (pParse, TK_NOT,
328c6 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
328c7 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b  46.pExpr, 0, 0);
328c8 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
328c9 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d 20  .yy346.zStart = 
328ca 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
328cb 79 79 33 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20  yy346.zStart;.  
328cc 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
328cd 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73  346.zEnd = &yyms
328ce 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
328cf 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  [yymsp[0].minor.
328d0 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20  yy0.n];.  }.    
328d1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
328d2 20 63 61 73 65 20 32 33 30 3a 20 2f 2a 20 65 78   case 230: /* ex
328d3 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f  pr ::= expr in_o
328d4 70 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 0a 20  p nm dbnm */.{. 
328d5 20 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63     SrcList *pSrc
328d6 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
328d7 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
328d8 64 62 2c 20 30 2c 26 79 79 6d 73 70 5b 2d 31 5d  db, 0,&yymsp[-1]
328d9 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
328da 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
328db 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
328dc 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
328dd 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
328de 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d 73 70  se, TK_IN, yymsp
328df 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-3].minor.yy346
328e0 2e 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20  .pExpr, 0, 0);. 
328e1 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e     if( yygotomin
328e2 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29  or.yy346.pExpr )
328e3 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69  {.      yygotomi
328e4 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d  nor.yy346.pExpr-
328e5 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c  >x.pSelect = sql
328e6 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50  ite3SelectNew(pP
328e7 61 72 73 65 2c 20 30 2c 70 53 72 63 2c 30 2c 30  arse, 0,pSrc,0,0
328e8 2c 30 2c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20  ,0,0,0,0,0);.   
328e9 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
328ea 74 79 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ty(yygotominor.y
328eb 79 33 34 36 2e 70 45 78 70 72 2c 20 45 50 5f 78  y346.pExpr, EP_x
328ec 49 73 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20  IsSelect);.     
328ed 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
328ee 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79  eight(pParse, yy
328ef 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
328f0 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73  pExpr);.    }els
328f1 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
328f2 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 50  SrcListDelete(pP
328f3 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 29 3b  arse->db, pSrc);
328f4 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 79  .    }.    if( y
328f5 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
328f6 79 33 32 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e  y328 ) yygotomin
328f7 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
328f8 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
328f9 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79  arse, TK_NOT, yy
328fa 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
328fb 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20  pExpr, 0, 0);.  
328fc 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
328fd 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d  346.zStart = yym
328fe 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-3].minor.yy3
328ff 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20 20 20 79  46.zStart;.    y
32900 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32901 2e 7a 45 6e 64 20 3d 20 79 79 6d 73 70 5b 30 5d  .zEnd = yymsp[0]
32902 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3f 20 26  .minor.yy0.z ? &
32903 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32904 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0.z[yymsp[0].mi
32905 6e 6f 72 2e 79 79 30 2e 6e 5d 20 3a 20 26 79 79  nor.yy0.n] : &yy
32906 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
32907 30 2e 7a 5b 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0.z[yymsp[-1].mi
32908 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a  nor.yy0.n];.  }.
32909 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3290a 20 20 20 20 20 63 61 73 65 20 32 33 31 3a 20 2f       case 231: /
3290b 2a 20 65 78 70 72 20 3a 3a 3d 20 45 58 49 53 54  * expr ::= EXIST
3290c 53 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20 2a  S LP select RP *
3290d 2f 0a 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 20  /.{.    Expr *p 
3290e 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  = yygotominor.yy
3290f 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69  346.pExpr = sqli
32910 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
32911 20 54 4b 5f 45 58 49 53 54 53 2c 20 30 2c 20 30   TK_EXISTS, 0, 0
32912 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 20  , 0);.    if( p 
32913 29 7b 0a 20 20 20 20 20 20 70 2d 3e 78 2e 70 53  ){.      p->x.pS
32914 65 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31  elect = yymsp[-1
32915 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b 0a 20 20 20  ].minor.yy3;.   
32916 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
32917 74 79 28 70 2c 20 45 50 5f 78 49 73 53 65 6c 65  ty(p, EP_xIsSele
32918 63 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ct);.      sqlit
32919 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28  e3ExprSetHeight(
3291a 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20  pParse, p);.    
3291b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
3291c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
3291d 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
3291e 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3291f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 79 79 67  );.    }.    yyg
32920 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
32921 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33  Start = yymsp[-3
32922 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20  ].minor.yy0.z;. 
32923 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
32924 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d  y346.zEnd = &yym
32925 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
32926 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  z[yymsp[0].minor
32927 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20  .yy0.n];.  }.   
32928 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32929 20 20 63 61 73 65 20 32 33 32 3a 20 2f 2a 20 65    case 232: /* e
3292a 78 70 72 20 3a 3a 3d 20 43 41 53 45 20 63 61 73  xpr ::= CASE cas
3292b 65 5f 6f 70 65 72 61 6e 64 20 63 61 73 65 5f 65  e_operand case_e
3292c 78 70 72 6c 69 73 74 20 63 61 73 65 5f 65 6c 73  xprlist case_els
3292d 65 20 45 4e 44 20 2a 2f 0a 7b 0a 20 20 79 79 67  e END */.{.  yyg
3292e 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
3292f 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
32930 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43  xpr(pParse, TK_C
32931 41 53 45 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  ASE, yymsp[-3].m
32932 69 6e 6f 72 2e 79 79 31 33 32 2c 20 79 79 6d 73  inor.yy132, yyms
32933 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33  p[-1].minor.yy13
32934 32 2c 20 30 29 3b 0a 20 20 69 66 28 20 79 79 67  2, 0);.  if( yyg
32935 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32936 45 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f  Expr ){.    yygo
32937 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32938 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 79  xpr->x.pList = y
32939 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
3293a 79 31 34 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  y14;.    sqlite3
3293b 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
3293c 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
3293d 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
3293e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
3293f 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65  ite3ExprListDele
32940 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
32941 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32942 79 31 34 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f  y14);.  }.  yygo
32943 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  tominor.yy346.zS
32944 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d  tart = yymsp[-4]
32945 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
32946 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32947 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b  6.zEnd = &yymsp[
32948 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79  0].minor.yy0.z[y
32949 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
3294a 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20  0.n];.}.        
3294b 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3294c 65 20 32 33 33 3a 20 2f 2a 20 63 61 73 65 5f 65  e 233: /* case_e
3294d 78 70 72 6c 69 73 74 20 3a 3a 3d 20 63 61 73 65  xprlist ::= case
3294e 5f 65 78 70 72 6c 69 73 74 20 57 48 45 4e 20 65  _exprlist WHEN e
3294f 78 70 72 20 54 48 45 4e 20 65 78 70 72 20 2a 2f  xpr THEN expr */
32950 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
32951 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45  .yy14 = sqlite3E
32952 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
32953 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d  arse,yymsp[-4].m
32954 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70  inor.yy14, yymsp
32955 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-2].minor.yy346
32956 2e 70 45 78 70 72 29 3b 0a 20 20 79 79 67 6f 74  .pExpr);.  yygot
32957 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71  ominor.yy14 = sq
32958 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
32959 65 6e 64 28 70 50 61 72 73 65 2c 79 79 67 6f 74  end(pParse,yygot
3295a 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d  ominor.yy14, yym
3295b 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
3295c 36 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20 20 20  6.pExpr);.}.    
3295d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3295e 20 63 61 73 65 20 32 33 34 3a 20 2f 2a 20 63 61   case 234: /* ca
3295f 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20  se_exprlist ::= 
32960 57 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65  WHEN expr THEN e
32961 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  xpr */.{.  yygot
32962 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71  ominor.yy14 = sq
32963 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
32964 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 79 79  end(pParse,0, yy
32965 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
32966 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 79 79  346.pExpr);.  yy
32967 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d  gotominor.yy14 =
32968 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
32969 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79  Append(pParse,yy
3296a 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20  gotominor.yy14, 
3296b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3296c 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 7d 0a 20  y346.pExpr);.}. 
3296d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3296e 20 20 20 20 63 61 73 65 20 32 34 33 3a 20 2f 2a      case 243: /*
3296f 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b   cmd ::= createk
32970 77 20 75 6e 69 71 75 65 66 6c 61 67 20 49 4e 44  w uniqueflag IND
32971 45 58 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e  EX ifnotexists n
32972 6d 20 64 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20  m dbnm ON nm LP 
32973 69 64 78 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a  idxlist RP */.{.
32974 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 49    sqlite3CreateI
32975 6e 64 65 78 28 70 50 61 72 73 65 2c 20 26 79 79  ndex(pParse, &yy
32976 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
32977 30 2c 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69  0, &yymsp[-5].mi
32978 6e 6f 72 2e 79 79 30 2c 20 0a 20 20 20 20 20 20  nor.yy0, .      
32979 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
3297a 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
3297b 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30  end(pParse->db,0
3297c 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
3297d 72 2e 79 79 30 2c 30 29 2c 20 79 79 6d 73 70 5b  r.yy0,0), yymsp[
3297e 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20  -1].minor.yy14, 
3297f 79 79 6d 73 70 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e  yymsp[-9].minor.
32980 79 79 33 32 38 2c 0a 20 20 20 20 20 20 20 20 20  yy328,.         
32981 20 20 20 20 20 20 20 20 20 20 20 20 20 26 79 79               &yy
32982 6d 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79  msp[-10].minor.y
32983 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0, &yymsp[0].mi
32984 6e 6f 72 2e 79 79 30 2c 20 53 51 4c 49 54 45 5f  nor.yy0, SQLITE_
32985 53 4f 5f 41 53 43 2c 20 79 79 6d 73 70 5b 2d 37  SO_ASC, yymsp[-7
32986 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a  ].minor.yy328);.
32987 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32988 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 34 3a  .      case 244:
32989 20 2f 2a 20 75 6e 69 71 75 65 66 6c 61 67 20 3a   /* uniqueflag :
3298a 3a 3d 20 55 4e 49 51 55 45 20 2a 2f 0a 20 20 20  := UNIQUE */.   
3298b 20 20 20 63 61 73 65 20 32 39 38 3a 20 2f 2a 20     case 298: /* 
3298c 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 41 42  raisetype ::= AB
3298d 4f 52 54 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ORT */ yytestcas
3298e 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 39 38 29  e(yyruleno==298)
3298f 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;.{yygotominor.y
32990 79 33 32 38 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  y328 = OE_Abort;
32991 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32992 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 35 3a  .      case 245:
32993 20 2f 2a 20 75 6e 69 71 75 65 66 6c 61 67 20 3a   /* uniqueflag :
32994 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  := */.{yygotomin
32995 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 4e 6f  or.yy328 = OE_No
32996 6e 65 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ne;}.        bre
32997 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
32998 34 38 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20 3a  48: /* idxlist :
32999 3a 3d 20 69 64 78 6c 69 73 74 20 43 4f 4d 4d 41  := idxlist COMMA
3299a 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74   nm collate sort
3299b 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 45 78 70  order */.{.  Exp
3299c 72 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20  r *p = 0;.  if( 
3299d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3299e 79 79 30 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70  yy0.n>0 ){.    p
3299f 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 70   = sqlite3Expr(p
329a0 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 43 4f  Parse->db, TK_CO
329a1 4c 55 4d 4e 2c 20 30 29 3b 0a 20 20 20 20 73 71  LUMN, 0);.    sq
329a2 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c  lite3ExprSetColl
329a3 28 70 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d  (pParse, p, &yym
329a4 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
329a5 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d  );.  }.  yygotom
329a6 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
329a7 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
329a8 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
329a9 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 70  4].minor.yy14, p
329aa 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
329ab 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72  ListSetName(pPar
329ac 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  se,yygotominor.y
329ad 79 31 34 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y14,&yymsp[-2].m
329ae 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 0a 20 20 73  inor.yy0,1);.  s
329af 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68  qlite3ExprListCh
329b0 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65  eckLength(pParse
329b1 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
329b2 31 34 2c 20 22 69 6e 64 65 78 22 29 3b 0a 20 20  14, "index");.  
329b3 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  if( yygotominor.
329b4 79 79 31 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e  yy14 ) yygotomin
329b5 6f 72 2e 79 79 31 34 2d 3e 61 5b 79 79 67 6f 74  or.yy14->a[yygot
329b6 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45 78  ominor.yy14->nEx
329b7 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20  pr-1].sortOrder 
329b8 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d  = (u8)yymsp[0].m
329b9 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a 20 20  inor.yy328;.}.  
329ba 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
329bb 20 20 20 63 61 73 65 20 32 34 39 3a 20 2f 2a 20     case 249: /* 
329bc 69 64 78 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 63  idxlist ::= nm c
329bd 6f 6c 6c 61 74 65 20 73 6f 72 74 6f 72 64 65 72  ollate sortorder
329be 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 20   */.{.  Expr *p 
329bf 3d 20 30 3b 0a 20 20 69 66 28 20 79 79 6d 73 70  = 0;.  if( yymsp
329c0 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  [-1].minor.yy0.n
329c1 3e 30 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71  >0 ){.    p = sq
329c2 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
329c3 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c  e, TK_COLUMN, 0,
329c4 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
329c5 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 70  te3ExprSetColl(p
329c6 50 61 72 73 65 2c 20 70 2c 20 26 79 79 6d 73 70  Parse, p, &yymsp
329c7 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-1].minor.yy0);
329c8 0a 20 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  .  }.  yygotomin
329c9 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65  or.yy14 = sqlite
329ca 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
329cb 70 50 61 72 73 65 2c 30 2c 20 70 29 3b 0a 20 20  pParse,0, p);.  
329cc 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 53  sqlite3ExprListS
329cd 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 79  etName(pParse, y
329ce 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c  ygotominor.yy14,
329cf 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f   &yymsp[-2].mino
329d0 72 2e 79 79 30 2c 20 31 29 3b 0a 20 20 73 71 6c  r.yy0, 1);.  sql
329d1 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63  ite3ExprListChec
329d2 6b 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 20  kLength(pParse, 
329d3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34  yygotominor.yy14
329d4 2c 20 22 69 6e 64 65 78 22 29 3b 0a 20 20 69 66  , "index");.  if
329d5 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
329d6 31 34 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  14 ) yygotominor
329d7 2e 79 79 31 34 2d 3e 61 5b 79 79 67 6f 74 6f 6d  .yy14->a[yygotom
329d8 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72  inor.yy14->nExpr
329d9 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20  -1].sortOrder = 
329da 28 75 38 29 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  (u8)yymsp[0].min
329db 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a 20 20 20 20  or.yy328;.}.    
329dc 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
329dd 20 63 61 73 65 20 32 35 30 3a 20 2f 2a 20 63 6f   case 250: /* co
329de 6c 6c 61 74 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79  llate ::= */.{yy
329df 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20  gotominor.yy0.z 
329e0 3d 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  = 0; yygotominor
329e1 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20  .yy0.n = 0;}.   
329e2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
329e3 20 20 63 61 73 65 20 32 35 32 3a 20 2f 2a 20 63    case 252: /* c
329e4 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45  md ::= DROP INDE
329e5 58 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e  X ifexists fulln
329e6 61 6d 65 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 44  ame */.{sqlite3D
329e7 72 6f 70 49 6e 64 65 78 28 70 50 61 72 73 65 2c  ropIndex(pParse,
329e8 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
329e9 79 79 36 35 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  yy65, yymsp[-1].
329ea 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20  minor.yy328);}. 
329eb 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
329ec 20 20 20 20 63 61 73 65 20 32 35 33 3a 20 2f 2a      case 253: /*
329ed 20 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20   cmd ::= VACUUM 
329ee 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  */.      case 25
329ef 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56 41  4: /* cmd ::= VA
329f0 43 55 55 4d 20 6e 6d 20 2a 2f 20 79 79 74 65 73  CUUM nm */ yytes
329f1 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
329f2 32 35 34 29 3b 0a 7b 73 71 6c 69 74 65 33 56 61  254);.{sqlite3Va
329f3 63 75 75 6d 28 70 50 61 72 73 65 29 3b 7d 0a 20  cuum(pParse);}. 
329f4 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
329f5 20 20 20 20 63 61 73 65 20 32 35 35 3a 20 2f 2a      case 255: /*
329f6 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20   cmd ::= PRAGMA 
329f7 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69  nm dbnm */.{sqli
329f8 74 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65  te3Pragma(pParse
329f9 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
329fa 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
329fb 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 7d  minor.yy0,0,0);}
329fc 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
329fd 20 20 20 20 20 20 63 61 73 65 20 32 35 36 3a 20        case 256: 
329fe 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  /* cmd ::= PRAGM
329ff 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20 6e 6d 6e  A nm dbnm EQ nmn
32a00 75 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72  um */.{sqlite3Pr
32a01 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79 6d  agma(pParse,&yym
32a02 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
32a03 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
32a04 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
32a05 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20  minor.yy0,0);}. 
32a06 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32a07 20 20 20 20 63 61 73 65 20 32 35 37 3a 20 2f 2a      case 257: /*
32a08 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20   cmd ::= PRAGMA 
32a09 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e 75 6d  nm dbnm LP nmnum
32a0a 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 50   RP */.{sqlite3P
32a0b 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79 79  ragma(pParse,&yy
32a0c 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
32a0d 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  0,&yymsp[-3].min
32a0e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31  or.yy0,&yymsp[-1
32a0f 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 7d  ].minor.yy0,0);}
32a10 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32a11 20 20 20 20 20 20 63 61 73 65 20 32 35 38 3a 20        case 258: 
32a12 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  /* cmd ::= PRAGM
32a13 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20 6d 69 6e  A nm dbnm EQ min
32a14 75 73 5f 6e 75 6d 20 2a 2f 0a 7b 73 71 6c 69 74  us_num */.{sqlit
32a15 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c  e3Pragma(pParse,
32a16 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
32a17 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  .yy0,&yymsp[-2].
32a18 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
32a19 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29  [0].minor.yy0,1)
32a1a 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
32a1b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 39  ;.      case 259
32a1c 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41  : /* cmd ::= PRA
32a1d 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6d  GMA nm dbnm LP m
32a1e 69 6e 75 73 5f 6e 75 6d 20 52 50 20 2a 2f 0a 7b  inus_num RP */.{
32a1f 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50  sqlite3Pragma(pP
32a20 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e  arse,&yymsp[-4].
32a21 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
32a22 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-3].minor.yy0,&
32a23 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
32a24 79 79 30 2c 31 29 3b 7d 0a 20 20 20 20 20 20 20  yy0,1);}.       
32a25 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32a26 73 65 20 32 37 30 3a 20 2f 2a 20 63 6d 64 20 3a  se 270: /* cmd :
32a27 3a 3d 20 63 72 65 61 74 65 6b 77 20 74 72 69 67  := createkw trig
32a28 67 65 72 5f 64 65 63 6c 20 42 45 47 49 4e 20 74  ger_decl BEGIN t
32a29 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20  rigger_cmd_list 
32a2a 45 4e 44 20 2a 2f 0a 7b 0a 20 20 54 6f 6b 65 6e  END */.{.  Token
32a2b 20 61 6c 6c 3b 0a 20 20 61 6c 6c 2e 7a 20 3d 20   all;.  all.z = 
32a2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
32a2d 79 79 30 2e 7a 3b 0a 20 20 61 6c 6c 2e 6e 20 3d  yy0.z;.  all.n =
32a2e 20 28 69 6e 74 29 28 79 79 6d 73 70 5b 30 5d 2e   (int)(yymsp[0].
32a2f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 2d 20 79 79  minor.yy0.z - yy
32a30 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
32a31 30 2e 7a 29 20 2b 20 79 79 6d 73 70 5b 30 5d 2e  0.z) + yymsp[0].
32a32 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3b 0a 20 20 73  minor.yy0.n;.  s
32a33 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
32a34 67 65 72 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ger(pParse, yyms
32a35 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37  p[-1].minor.yy47
32a36 33 2c 20 26 61 6c 6c 29 3b 0a 7d 0a 20 20 20 20  3, &all);.}.    
32a37 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32a38 20 63 61 73 65 20 32 37 31 3a 20 2f 2a 20 74 72   case 271: /* tr
32a39 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d 20 74  igger_decl ::= t
32a3a 65 6d 70 20 54 52 49 47 47 45 52 20 69 66 6e 6f  emp TRIGGER ifno
32a3b 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20  texists nm dbnm 
32a3c 74 72 69 67 67 65 72 5f 74 69 6d 65 20 74 72 69  trigger_time tri
32a3d 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20 66 75  gger_event ON fu
32a3e 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68 5f 63  llname foreach_c
32a3f 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c 61 75 73  lause when_claus
32a40 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  e */.{.  sqlite3
32a41 42 65 67 69 6e 54 72 69 67 67 65 72 28 70 50 61  BeginTrigger(pPa
32a42 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d 37 5d 2e  rse, &yymsp[-7].
32a43 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73  minor.yy0, &yyms
32a44 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-6].minor.yy0,
32a45 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72   yymsp[-5].minor
32a46 2e 79 79 33 32 38 2c 20 79 79 6d 73 70 5b 2d 34  .yy328, yymsp[-4
32a47 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37 38 2e 61 2c  ].minor.yy378.a,
32a48 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
32a49 2e 79 79 33 37 38 2e 62 2c 20 79 79 6d 73 70 5b  .yy378.b, yymsp[
32a4a 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 20  -2].minor.yy65, 
32a4b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32a4c 79 31 33 32 2c 20 79 79 6d 73 70 5b 2d 31 30 5d  y132, yymsp[-10]
32a4d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 20 79 79  .minor.yy328, yy
32a4e 6d 73 70 5b 2d 38 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-8].minor.yy
32a4f 33 32 38 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  328);.  yygotomi
32a50 6e 6f 72 2e 79 79 30 20 3d 20 28 79 79 6d 73 70  nor.yy0 = (yymsp
32a51 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e  [-6].minor.yy0.n
32a52 3d 3d 30 3f 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69  ==0?yymsp[-7].mi
32a53 6e 6f 72 2e 79 79 30 3a 79 79 6d 73 70 5b 2d 36  nor.yy0:yymsp[-6
32a54 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
32a55 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32a56 20 20 20 20 20 63 61 73 65 20 32 37 32 3a 20 2f       case 272: /
32a57 2a 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a  * trigger_time :
32a58 3a 3d 20 42 45 46 4f 52 45 20 2a 2f 0a 20 20 20  := BEFORE */.   
32a59 20 20 20 63 61 73 65 20 32 37 35 3a 20 2f 2a 20     case 275: /* 
32a5a 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d  trigger_time ::=
32a5b 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32a5c 79 72 75 6c 65 6e 6f 3d 3d 32 37 35 29 3b 0a 7b  yruleno==275);.{
32a5d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32a5e 32 38 20 3d 20 54 4b 5f 42 45 46 4f 52 45 3b 20  28 = TK_BEFORE; 
32a5f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32a60 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 33 3a  .      case 273:
32a61 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d 65   /* trigger_time
32a62 20 3a 3a 3d 20 41 46 54 45 52 20 2a 2f 0a 7b 20   ::= AFTER */.{ 
32a63 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
32a64 38 20 3d 20 54 4b 5f 41 46 54 45 52 3b 20 20 7d  8 = TK_AFTER;  }
32a65 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32a66 20 20 20 20 20 20 63 61 73 65 20 32 37 34 3a 20        case 274: 
32a67 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20  /* trigger_time 
32a68 3a 3a 3d 20 49 4e 53 54 45 41 44 20 4f 46 20 2a  ::= INSTEAD OF *
32a69 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
32a6a 79 79 33 32 38 20 3d 20 54 4b 5f 49 4e 53 54 45  yy328 = TK_INSTE
32a6b 41 44 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  AD;}.        bre
32a6c 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
32a6d 37 36 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65  76: /* trigger_e
32a6e 76 65 6e 74 20 3a 3a 3d 20 44 45 4c 45 54 45 7c  vent ::= DELETE|
32a6f 49 4e 53 45 52 54 20 2a 2f 0a 20 20 20 20 20 20  INSERT */.      
32a70 63 61 73 65 20 32 37 37 3a 20 2f 2a 20 74 72 69  case 277: /* tri
32a71 67 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55  gger_event ::= U
32a72 50 44 41 54 45 20 2a 2f 20 79 79 74 65 73 74 63  PDATE */ yytestc
32a73 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 37  ase(yyruleno==27
32a74 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72  7);.{yygotominor
32a75 2e 79 79 33 37 38 2e 61 20 3d 20 79 79 6d 73 70  .yy378.a = yymsp
32a76 5b 30 5d 2e 6d 61 6a 6f 72 3b 20 79 79 67 6f 74  [0].major; yygot
32a77 6f 6d 69 6e 6f 72 2e 79 79 33 37 38 2e 62 20 3d  ominor.yy378.b =
32a78 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
32a79 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
32a7a 37 38 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65  78: /* trigger_e
32a7b 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20  vent ::= UPDATE 
32a7c 4f 46 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f  OF inscollist */
32a7d 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
32a7e 33 37 38 2e 61 20 3d 20 54 4b 5f 55 50 44 41 54  378.a = TK_UPDAT
32a7f 45 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  E; yygotominor.y
32a80 79 33 37 38 2e 62 20 3d 20 79 79 6d 73 70 5b 30  y378.b = yymsp[0
32a81 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 3b 7d 0a  ].minor.yy408;}.
32a82 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32a83 20 20 20 20 20 63 61 73 65 20 32 38 31 3a 20 2f       case 281: /
32a84 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a  * when_clause ::
32a85 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
32a86 33 30 33 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20  303: /* key_opt 
32a87 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
32a88 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 33 29  e(yyruleno==303)
32a89 3b 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.{ yygotominor.
32a8a 79 79 31 33 32 20 3d 20 30 3b 20 7d 0a 20 20 20  yy132 = 0; }.   
32a8b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32a8c 20 20 63 61 73 65 20 32 38 32 3a 20 2f 2a 20 77    case 282: /* w
32a8d 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 57  hen_clause ::= W
32a8e 48 45 4e 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  HEN expr */.    
32a8f 20 20 63 61 73 65 20 33 30 34 3a 20 2f 2a 20 6b    case 304: /* k
32a90 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b 45 59 20 65  ey_opt ::= KEY e
32a91 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  xpr */ yytestcas
32a92 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 30 34 29  e(yyruleno==304)
32a93 3b 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.{ yygotominor.
32a94 79 79 31 33 32 20 3d 20 79 79 6d 73 70 5b 30 5d  yy132 = yymsp[0]
32a95 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
32a96 70 72 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  pr; }.        br
32a97 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32a98 32 38 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  283: /* trigger_
32a99 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69  cmd_list ::= tri
32a9a 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72  gger_cmd_list tr
32a9b 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a  igger_cmd SEMI *
32a9c 2f 0a 7b 0a 20 20 61 73 73 65 72 74 28 20 79 79  /.{.  assert( yy
32a9d 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
32a9e 34 37 33 21 3d 30 20 29 3b 0a 20 20 79 79 6d 73  473!=0 );.  yyms
32a9f 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37  p[-2].minor.yy47
32aa0 33 2d 3e 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20  3->pLast->pNext 
32aa1 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
32aa2 72 2e 79 79 34 37 33 3b 0a 20 20 79 79 6d 73 70  r.yy473;.  yymsp
32aa3 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33  [-2].minor.yy473
32aa4 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d 73 70 5b  ->pLast = yymsp[
32aa5 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 3b  -1].minor.yy473;
32aa6 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
32aa7 79 34 37 33 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  y473 = yymsp[-2]
32aa8 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 7d 0a  .minor.yy473;.}.
32aa9 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32aaa 20 20 20 20 20 63 61 73 65 20 32 38 34 3a 20 2f       case 284: /
32aab 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69  * trigger_cmd_li
32aac 73 74 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63  st ::= trigger_c
32aad 6d 64 20 53 45 4d 49 20 2a 2f 0a 7b 20 0a 20 20  md SEMI */.{ .  
32aae 61 73 73 65 72 74 28 20 79 79 6d 73 70 5b 2d 31  assert( yymsp[-1
32aaf 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 21 3d 30  ].minor.yy473!=0
32ab0 20 29 3b 0a 20 20 79 79 6d 73 70 5b 2d 31 5d 2e   );.  yymsp[-1].
32ab1 6d 69 6e 6f 72 2e 79 79 34 37 33 2d 3e 70 4c 61  minor.yy473->pLa
32ab2 73 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  st = yymsp[-1].m
32ab3 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 20 20 79 79  inor.yy473;.  yy
32ab4 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20  gotominor.yy473 
32ab5 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
32ab6 72 2e 79 79 34 37 33 3b 0a 7d 0a 20 20 20 20 20  r.yy473;.}.     
32ab7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32ab8 63 61 73 65 20 32 38 36 3a 20 2f 2a 20 74 72 6e  case 286: /* trn
32ab9 6d 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20  m ::= nm DOT nm 
32aba 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
32abb 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30  or.yy0 = yymsp[0
32abc 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 73  ].minor.yy0;.  s
32abd 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
32abe 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
32abf 22 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c 65  "qualified table
32ac0 20 6e 61 6d 65 73 20 61 72 65 20 6e 6f 74 20 61   names are not a
32ac1 6c 6c 6f 77 65 64 20 6f 6e 20 49 4e 53 45 52 54  llowed on INSERT
32ac2 2c 20 55 50 44 41 54 45 2c 20 61 6e 64 20 44 45  , UPDATE, and DE
32ac3 4c 45 54 45 20 22 0a 20 20 20 20 20 20 20 20 22  LETE ".        "
32ac4 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74 68 69  statements withi
32ac5 6e 20 74 72 69 67 67 65 72 73 22 29 3b 0a 7d 0a  n triggers");.}.
32ac6 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32ac7 20 20 20 20 20 63 61 73 65 20 32 38 38 3a 20 2f       case 288: /
32ac8 2a 20 74 72 69 64 78 62 79 20 3a 3a 3d 20 49 4e  * tridxby ::= IN
32ac9 44 45 58 45 44 20 42 59 20 6e 6d 20 2a 2f 0a 7b  DEXED BY nm */.{
32aca 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d  .  sqlite3ErrorM
32acb 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  sg(pParse,.     
32acc 20 20 20 22 74 68 65 20 49 4e 44 45 58 45 44 20     "the INDEXED 
32acd 42 59 20 63 6c 61 75 73 65 20 69 73 20 6e 6f 74  BY clause is not
32ace 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 55 50 44 41   allowed on UPDA
32acf 54 45 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61  TE or DELETE sta
32ad0 74 65 6d 65 6e 74 73 20 22 0a 20 20 20 20 20 20  tements ".      
32ad1 20 20 22 77 69 74 68 69 6e 20 74 72 69 67 67 65    "within trigge
32ad2 72 73 22 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  rs");.}.        
32ad3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32ad4 65 20 32 38 39 3a 20 2f 2a 20 74 72 69 64 78 62  e 289: /* tridxb
32ad5 79 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58 45  y ::= NOT INDEXE
32ad6 44 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  D */.{.  sqlite3
32ad7 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
32ad8 0a 20 20 20 20 20 20 20 20 22 74 68 65 20 4e 4f  .        "the NO
32ad9 54 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65  T INDEXED clause
32ada 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20   is not allowed 
32adb 6f 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c  on UPDATE or DEL
32adc 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73 20 22  ETE statements "
32add 0a 20 20 20 20 20 20 20 20 22 77 69 74 68 69 6e  .        "within
32ade 20 74 72 69 67 67 65 72 73 22 29 3b 0a 7d 0a 20   triggers");.}. 
32adf 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32ae0 20 20 20 20 63 61 73 65 20 32 39 30 3a 20 2f 2a      case 290: /*
32ae1 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d   trigger_cmd ::=
32ae2 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 74   UPDATE orconf t
32ae3 72 6e 6d 20 74 72 69 64 78 62 79 20 53 45 54 20  rnm tridxby SET 
32ae4 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70  setlist where_op
32ae5 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  t */.{ yygotomin
32ae6 6f 72 2e 79 79 34 37 33 20 3d 20 73 71 6c 69 74  or.yy473 = sqlit
32ae7 65 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53  e3TriggerUpdateS
32ae8 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  tep(pParse->db, 
32ae9 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
32aea 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  .yy0, yymsp[-1].
32aeb 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73  minor.yy14, yyms
32aec 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32  p[0].minor.yy132
32aed 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f  , yymsp[-5].mino
32aee 72 2e 79 79 31 38 36 29 3b 20 7d 0a 20 20 20 20  r.yy186); }.    
32aef 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32af0 20 63 61 73 65 20 32 39 31 3a 20 2f 2a 20 74 72   case 291: /* tr
32af1 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e  igger_cmd ::= in
32af2 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20 74 72  sert_cmd INTO tr
32af3 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70  nm inscollist_op
32af4 74 20 56 41 4c 55 45 53 20 4c 50 20 69 74 65 6d  t VALUES LP item
32af5 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 79 79 67 6f  list RP */.{yygo
32af6 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20  tominor.yy473 = 
32af7 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 49 6e  sqlite3TriggerIn
32af8 73 65 72 74 53 74 65 70 28 70 50 61 72 73 65 2d  sertStep(pParse-
32af9 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d 35 5d 2e  >db, &yymsp[-5].
32afa 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70  minor.yy0, yymsp
32afb 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38  [-4].minor.yy408
32afc 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
32afd 72 2e 79 79 31 34 2c 20 30 2c 20 79 79 6d 73 70  r.yy14, 0, yymsp
32afe 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36  [-7].minor.yy186
32aff 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
32b00 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39  k;.      case 29
32b01 32 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d  2: /* trigger_cm
32b02 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64  d ::= insert_cmd
32b03 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e 73 63 6f   INTO trnm insco
32b04 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63 74  llist_opt select
32b05 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
32b06 2e 79 79 34 37 33 20 3d 20 73 71 6c 69 74 65 33  .yy473 = sqlite3
32b07 54 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65  TriggerInsertSte
32b08 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79  p(pParse->db, &y
32b09 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32b0a 79 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  y0, yymsp[-1].mi
32b0b 6e 6f 72 2e 79 79 34 30 38 2c 20 30 2c 20 79 79  nor.yy408, 0, yy
32b0c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
32b0d 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
32b0e 72 2e 79 79 31 38 36 29 3b 7d 0a 20 20 20 20 20  r.yy186);}.     
32b0f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32b10 63 61 73 65 20 32 39 33 3a 20 2f 2a 20 74 72 69  case 293: /* tri
32b11 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 44 45 4c  gger_cmd ::= DEL
32b12 45 54 45 20 46 52 4f 4d 20 74 72 6e 6d 20 74 72  ETE FROM trnm tr
32b13 69 64 78 62 79 20 77 68 65 72 65 5f 6f 70 74 20  idxby where_opt 
32b14 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
32b15 79 79 34 37 33 20 3d 20 73 71 6c 69 74 65 33 54  yy473 = sqlite3T
32b16 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70  riggerDeleteStep
32b17 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79  (pParse->db, &yy
32b18 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
32b19 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0, yymsp[0].mino
32b1a 72 2e 79 79 31 33 32 29 3b 7d 0a 20 20 20 20 20  r.yy132);}.     
32b1b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32b1c 63 61 73 65 20 32 39 34 3a 20 2f 2a 20 74 72 69  case 294: /* tri
32b1d 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c  gger_cmd ::= sel
32b1e 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ect */.{yygotomi
32b1f 6e 6f 72 2e 79 79 34 37 33 20 3d 20 73 71 6c 69  nor.yy473 = sqli
32b20 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74  te3TriggerSelect
32b21 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  Step(pParse->db,
32b22 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
32b23 79 79 33 29 3b 20 7d 0a 20 20 20 20 20 20 20 20  yy3); }.        
32b24 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32b25 65 20 32 39 35 3a 20 2f 2a 20 65 78 70 72 20 3a  e 295: /* expr :
32b26 3a 3d 20 52 41 49 53 45 20 4c 50 20 49 47 4e 4f  := RAISE LP IGNO
32b27 52 45 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67  RE RP */.{.  yyg
32b28 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32b29 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45  Expr = sqlite3PE
32b2a 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52  xpr(pParse, TK_R
32b2b 41 49 53 45 2c 20 30 2c 20 30 2c 20 30 29 3b 20  AISE, 0, 0, 0); 
32b2c 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e  .  if( yygotomin
32b2d 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29  or.yy346.pExpr )
32b2e 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  {.    yygotomino
32b2f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 61  r.yy346.pExpr->a
32b30 66 66 69 6e 69 74 79 20 3d 20 4f 45 5f 49 67 6e  ffinity = OE_Ign
32b31 6f 72 65 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74  ore;.  }.  yygot
32b32 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74  ominor.yy346.zSt
32b33 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e  art = yymsp[-3].
32b34 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79  minor.yy0.z;.  y
32b35 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32b36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30  .zEnd = &yymsp[0
32b37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79  ].minor.yy0.z[yy
32b38 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32b39 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  .n];.}.        b
32b3a 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32b3b 20 32 39 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   296: /* expr ::
32b3c 3d 20 52 41 49 53 45 20 4c 50 20 72 61 69 73 65  = RAISE LP raise
32b3d 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50  type COMMA nm RP
32b3e 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
32b3f 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
32b40 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
32b41 50 61 72 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c  Parse, TK_RAISE,
32b42 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31   0, 0, &yymsp[-1
32b43 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 0a 20  ].minor.yy0); . 
32b44 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
32b45 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 20 7b  .yy346.pExpr ) {
32b46 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
32b47 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 61 66  .yy346.pExpr->af
32b48 66 69 6e 69 74 79 20 3d 20 28 63 68 61 72 29 79  finity = (char)y
32b49 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
32b4a 79 33 32 38 3b 0a 20 20 7d 0a 20 20 79 79 67 6f  y328;.  }.  yygo
32b4b 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  tominor.yy346.zS
32b4c 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 35 5d  tart = yymsp[-5]
32b4d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20  .minor.yy0.z;.  
32b4e 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32b4f 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b  6.zEnd = &yymsp[
32b50 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79  0].minor.yy0.z[y
32b51 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32b52 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20 20 20 20  0.n];.}.        
32b53 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32b54 65 20 32 39 37 3a 20 2f 2a 20 72 61 69 73 65 74  e 297: /* raiset
32b55 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b  ype ::= ROLLBACK
32b56 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
32b57 2e 79 79 33 32 38 20 3d 20 4f 45 5f 52 6f 6c 6c  .yy328 = OE_Roll
32b58 62 61 63 6b 3b 7d 0a 20 20 20 20 20 20 20 20 62  back;}.        b
32b59 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32b5a 20 32 39 39 3a 20 2f 2a 20 72 61 69 73 65 74 79   299: /* raisety
32b5b 70 65 20 3a 3a 3d 20 46 41 49 4c 20 2a 2f 0a 7b  pe ::= FAIL */.{
32b5c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
32b5d 38 20 3d 20 4f 45 5f 46 61 69 6c 3b 7d 0a 20 20  8 = OE_Fail;}.  
32b5e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32b5f 20 20 20 63 61 73 65 20 33 30 30 3a 20 2f 2a 20     case 300: /* 
32b60 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49  cmd ::= DROP TRI
32b61 47 47 45 52 20 69 66 65 78 69 73 74 73 20 66 75  GGER ifexists fu
32b62 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71  llname */.{.  sq
32b63 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65 72  lite3DropTrigger
32b64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d  (pParse,yymsp[0]
32b65 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c 79 79 6d 73  .minor.yy65,yyms
32b66 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-1].minor.yy32
32b67 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  8);.}.        br
32b68 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32b69 33 30 31 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  301: /* cmd ::= 
32b6a 41 54 54 41 43 48 20 64 61 74 61 62 61 73 65 5f  ATTACH database_
32b6b 6b 77 5f 6f 70 74 20 65 78 70 72 20 41 53 20 65  kw_opt expr AS e
32b6c 78 70 72 20 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b  xpr key_opt */.{
32b6d 0a 20 20 73 71 6c 69 74 65 33 41 74 74 61 63 68  .  sqlite3Attach
32b6e 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
32b6f 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  3].minor.yy346.p
32b70 45 78 70 72 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  Expr, yymsp[-1].
32b71 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32b72 72 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  r, yymsp[0].mino
32b73 72 2e 79 79 31 33 32 29 3b 0a 7d 0a 20 20 20 20  r.yy132);.}.    
32b74 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32b75 20 63 61 73 65 20 33 30 32 3a 20 2f 2a 20 63 6d   case 302: /* cm
32b76 64 20 3a 3a 3d 20 44 45 54 41 43 48 20 64 61 74  d ::= DETACH dat
32b77 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70  abase_kw_opt exp
32b78 72 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  r */.{.  sqlite3
32b79 44 65 74 61 63 68 28 70 50 61 72 73 65 2c 20 79  Detach(pParse, y
32b7a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32b7b 33 34 36 2e 70 45 78 70 72 29 3b 0a 7d 0a 20 20  346.pExpr);.}.  
32b7c 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32b7d 20 20 20 63 61 73 65 20 33 30 37 3a 20 2f 2a 20     case 307: /* 
32b7e 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20  cmd ::= REINDEX 
32b7f 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e 64  */.{sqlite3Reind
32b80 65 78 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29  ex(pParse, 0, 0)
32b81 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
32b82 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 38  ;.      case 308
32b83 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49  : /* cmd ::= REI
32b84 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a  NDEX nm dbnm */.
32b85 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28  {sqlite3Reindex(
32b86 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
32b87 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  1].minor.yy0, &y
32b88 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32b89 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
32b8a 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
32b8b 30 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41  09: /* cmd ::= A
32b8c 4e 41 4c 59 5a 45 20 2a 2f 0a 7b 73 71 6c 69 74  NALYZE */.{sqlit
32b8d 65 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65  e3Analyze(pParse
32b8e 2c 20 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20  , 0, 0);}.      
32b8f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
32b90 61 73 65 20 33 31 30 3a 20 2f 2a 20 63 6d 64 20  ase 310: /* cmd 
32b91 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64  ::= ANALYZE nm d
32b92 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  bnm */.{sqlite3A
32b93 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 26  nalyze(pParse, &
32b94 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
32b95 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  yy0, &yymsp[0].m
32b96 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
32b97 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32b98 20 63 61 73 65 20 33 31 31 3a 20 2f 2a 20 63 6d   case 311: /* cm
32b99 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c  d ::= ALTER TABL
32b9a 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d  E fullname RENAM
32b9b 45 20 54 4f 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73  E TO nm */.{.  s
32b9c 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d  qlite3AlterRenam
32b9d 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 79 79  eTable(pParse,yy
32b9e 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
32b9f 36 35 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  65,&yymsp[0].min
32ba0 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
32ba1 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32ba2 63 61 73 65 20 33 31 32 3a 20 2f 2a 20 63 6d 64  case 312: /* cmd
32ba3 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45   ::= ALTER TABLE
32ba4 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c   add_column_full
32ba5 6e 61 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75 6d  name ADD kwcolum
32ba6 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  n_opt column */.
32ba7 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72  {.  sqlite3Alter
32ba8 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28  FinishAddColumn(
32ba9 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 30  pParse, &yymsp[0
32baa 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
32bab 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32bac 20 20 20 20 20 63 61 73 65 20 33 31 33 3a 20 2f       case 313: /
32bad 2a 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c  * add_column_ful
32bae 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61  lname ::= fullna
32baf 6d 65 20 2a 2f 0a 7b 0a 20 20 70 50 61 72 73 65  me */.{.  pParse
32bb0 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ->db->lookaside.
32bb1 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20  bEnabled = 0;.  
32bb2 73 71 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69  sqlite3AlterBegi
32bb3 6e 41 64 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73  nAddColumn(pPars
32bb4 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  e, yymsp[0].mino
32bb5 72 2e 79 79 36 35 29 3b 0a 7d 0a 20 20 20 20 20  r.yy65);.}.     
32bb6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32bb7 63 61 73 65 20 33 31 36 3a 20 2f 2a 20 63 6d 64  case 316: /* cmd
32bb8 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62   ::= create_vtab
32bb9 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62   */.{sqlite3Vtab
32bba 46 69 6e 69 73 68 50 61 72 73 65 28 70 50 61 72  FinishParse(pPar
32bbb 73 65 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  se,0);}.        
32bbc 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32bbd 65 20 33 31 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 317: /* cmd ::
32bbe 3d 20 63 72 65 61 74 65 5f 76 74 61 62 20 4c 50  = create_vtab LP
32bbf 20 76 74 61 62 61 72 67 6c 69 73 74 20 52 50 20   vtabarglist RP 
32bc0 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 46  */.{sqlite3VtabF
32bc1 69 6e 69 73 68 50 61 72 73 65 28 70 50 61 72 73  inishParse(pPars
32bc2 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  e,&yymsp[0].mino
32bc3 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
32bc4 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32bc5 73 65 20 33 31 38 3a 20 2f 2a 20 63 72 65 61 74  se 318: /* creat
32bc6 65 5f 76 74 61 62 20 3a 3a 3d 20 63 72 65 61 74  e_vtab ::= creat
32bc7 65 6b 77 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ekw VIRTUAL TABL
32bc8 45 20 6e 6d 20 64 62 6e 6d 20 55 53 49 4e 47 20  E nm dbnm USING 
32bc9 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c 69  nm */.{.    sqli
32bca 74 65 33 56 74 61 62 42 65 67 69 6e 50 61 72 73  te3VtabBeginPars
32bcb 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70  e(pParse, &yymsp
32bcc 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-3].minor.yy0, 
32bcd 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
32bce 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  .yy0, &yymsp[0].
32bcf 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20  minor.yy0);.}.  
32bd0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32bd1 20 20 20 63 61 73 65 20 33 32 31 3a 20 2f 2a 20     case 321: /* 
32bd2 76 74 61 62 61 72 67 20 3a 3a 3d 20 2a 2f 0a 7b  vtabarg ::= */.{
32bd3 73 71 6c 69 74 65 33 56 74 61 62 41 72 67 49 6e  sqlite3VtabArgIn
32bd4 69 74 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20  it(pParse);}.   
32bd5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32bd6 20 20 63 61 73 65 20 33 32 33 3a 20 2f 2a 20 76    case 323: /* v
32bd7 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20  tabargtoken ::= 
32bd8 41 4e 59 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ANY */.      cas
32bd9 65 20 33 32 34 3a 20 2f 2a 20 76 74 61 62 61 72  e 324: /* vtabar
32bda 67 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70 20 61 6e  gtoken ::= lp an
32bdb 79 6c 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65  ylist RP */ yyte
32bdc 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32bdd 3d 33 32 34 29 3b 0a 20 20 20 20 20 20 63 61 73  =324);.      cas
32bde 65 20 33 32 35 3a 20 2f 2a 20 6c 70 20 3a 3a 3d  e 325: /* lp ::=
32bdf 20 4c 50 20 2a 2f 20 79 79 74 65 73 74 63 61 73   LP */ yytestcas
32be0 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 35 29  e(yyruleno==325)
32be1 3b 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 41 72  ;.{sqlite3VtabAr
32be2 67 45 78 74 65 6e 64 28 70 50 61 72 73 65 2c 26  gExtend(pParse,&
32be3 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32be4 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
32be5 65 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75  eak;.      defau
32be6 6c 74 3a 0a 20 20 20 20 20 20 2f 2a 20 28 30 29  lt:.      /* (0)
32be7 20 69 6e 70 75 74 20 3a 3a 3d 20 63 6d 64 6c 69   input ::= cmdli
32be8 73 74 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  st */ yytestcase
32be9 28 79 79 72 75 6c 65 6e 6f 3d 3d 30 29 3b 0a 20  (yyruleno==0);. 
32bea 20 20 20 20 20 2f 2a 20 28 31 29 20 63 6d 64 6c       /* (1) cmdl
32beb 69 73 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20  ist ::= cmdlist 
32bec 65 63 6d 64 20 2a 2f 20 79 79 74 65 73 74 63 61  ecmd */ yytestca
32bed 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 29 3b  se(yyruleno==1);
32bee 0a 20 20 20 20 20 20 2f 2a 20 28 32 29 20 63 6d  .      /* (2) cm
32bef 64 6c 69 73 74 20 3a 3a 3d 20 65 63 6d 64 20 2a  dlist ::= ecmd *
32bf0 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32bf1 75 6c 65 6e 6f 3d 3d 32 29 3b 0a 20 20 20 20 20  uleno==2);.     
32bf2 20 2f 2a 20 28 33 29 20 65 63 6d 64 20 3a 3a 3d   /* (3) ecmd ::=
32bf3 20 53 45 4d 49 20 2a 2f 20 79 79 74 65 73 74 63   SEMI */ yytestc
32bf4 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 29  ase(yyruleno==3)
32bf5 3b 0a 20 20 20 20 20 20 2f 2a 20 28 34 29 20 65  ;.      /* (4) e
32bf6 63 6d 64 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20  cmd ::= explain 
32bf7 63 6d 64 78 20 53 45 4d 49 20 2a 2f 20 79 79 74  cmdx SEMI */ yyt
32bf8 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32bf9 3d 3d 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  ==4);.      /* (
32bfa 31 30 29 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a  10) trans_opt ::
32bfb 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
32bfc 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 29 3b 0a 20  yyruleno==10);. 
32bfd 20 20 20 20 20 2f 2a 20 28 31 31 29 20 74 72 61       /* (11) tra
32bfe 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e 53  ns_opt ::= TRANS
32bff 41 43 54 49 4f 4e 20 2a 2f 20 79 79 74 65 73 74  ACTION */ yytest
32c00 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
32c01 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 31 32  1);.      /* (12
32c02 29 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20  ) trans_opt ::= 
32c03 54 52 41 4e 53 41 43 54 49 4f 4e 20 6e 6d 20 2a  TRANSACTION nm *
32c04 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32c05 75 6c 65 6e 6f 3d 3d 31 32 29 3b 0a 20 20 20 20  uleno==12);.    
32c06 20 20 2f 2a 20 28 32 30 29 20 73 61 76 65 70 6f    /* (20) savepo
32c07 69 6e 74 5f 6f 70 74 20 3a 3a 3d 20 53 41 56 45  int_opt ::= SAVE
32c08 50 4f 49 4e 54 20 2a 2f 20 79 79 74 65 73 74 63  POINT */ yytestc
32c09 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30  ase(yyruleno==20
32c0a 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 31 29  );.      /* (21)
32c0b 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a   savepoint_opt :
32c0c 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
32c0d 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 31 29 3b 0a  (yyruleno==21);.
32c0e 20 20 20 20 20 20 2f 2a 20 28 32 35 29 20 63 6d        /* (25) cm
32c0f 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 74 61 62  d ::= create_tab
32c10 6c 65 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  le create_table_
32c11 61 72 67 73 20 2a 2f 20 79 79 74 65 73 74 63 61  args */ yytestca
32c12 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 35 29  se(yyruleno==25)
32c13 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 34 29 20  ;.      /* (34) 
32c14 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20 63  columnlist ::= c
32c15 6f 6c 75 6d 6e 6c 69 73 74 20 43 4f 4d 4d 41 20  olumnlist COMMA 
32c16 63 6f 6c 75 6d 6e 20 2a 2f 20 79 79 74 65 73 74  column */ yytest
32c17 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33  case(yyruleno==3
32c18 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 35  4);.      /* (35
32c19 29 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d  ) columnlist ::=
32c1a 20 63 6f 6c 75 6d 6e 20 2a 2f 20 79 79 74 65 73   column */ yytes
32c1b 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32c1c 33 35 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 34  35);.      /* (4
32c1d 34 29 20 74 79 70 65 20 3a 3a 3d 20 2a 2f 20 79  4) type ::= */ y
32c1e 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32c1f 6e 6f 3d 3d 34 34 29 3b 0a 20 20 20 20 20 20 2f  no==44);.      /
32c20 2a 20 28 35 31 29 20 73 69 67 6e 65 64 20 3a 3a  * (51) signed ::
32c21 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f 20 79 79  = plus_num */ yy
32c22 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32c23 6f 3d 3d 35 31 29 3b 0a 20 20 20 20 20 20 2f 2a  o==51);.      /*
32c24 20 28 35 32 29 20 73 69 67 6e 65 64 20 3a 3a 3d   (52) signed ::=
32c25 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f 20 79 79   minus_num */ yy
32c26 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32c27 6f 3d 3d 35 32 29 3b 0a 20 20 20 20 20 20 2f 2a  o==52);.      /*
32c28 20 28 35 33 29 20 63 61 72 67 6c 69 73 74 20 3a   (53) carglist :
32c29 3a 3d 20 63 61 72 67 6c 69 73 74 20 63 61 72 67  := carglist carg
32c2a 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32c2b 79 72 75 6c 65 6e 6f 3d 3d 35 33 29 3b 0a 20 20  yruleno==53);.  
32c2c 20 20 20 20 2f 2a 20 28 35 34 29 20 63 61 72 67      /* (54) carg
32c2d 6c 69 73 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  list ::= */ yyte
32c2e 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32c2f 3d 35 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =54);.      /* (
32c30 35 35 29 20 63 61 72 67 20 3a 3a 3d 20 43 4f 4e  55) carg ::= CON
32c31 53 54 52 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73  STRAINT nm ccons
32c32 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32c33 79 72 75 6c 65 6e 6f 3d 3d 35 35 29 3b 0a 20 20  yruleno==55);.  
32c34 20 20 20 20 2f 2a 20 28 35 36 29 20 63 61 72 67      /* (56) carg
32c35 20 3a 3a 3d 20 63 63 6f 6e 73 20 2a 2f 20 79 79   ::= ccons */ yy
32c36 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32c37 6f 3d 3d 35 36 29 3b 0a 20 20 20 20 20 20 2f 2a  o==56);.      /*
32c38 20 28 36 32 29 20 63 63 6f 6e 73 20 3a 3a 3d 20   (62) ccons ::= 
32c39 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 20 79  NULL onconf */ y
32c3a 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32c3b 6e 6f 3d 3d 36 32 29 3b 0a 20 20 20 20 20 20 2f  no==62);.      /
32c3c 2a 20 28 38 39 29 20 63 6f 6e 73 6c 69 73 74 20  * (89) conslist 
32c3d 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 43 4f 4d  ::= conslist COM
32c3e 4d 41 20 74 63 6f 6e 73 20 2a 2f 20 79 79 74 65  MA tcons */ yyte
32c3f 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32c40 3d 38 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =89);.      /* (
32c41 39 30 29 20 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d  90) conslist ::=
32c42 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73 20   conslist tcons 
32c43 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
32c44 72 75 6c 65 6e 6f 3d 3d 39 30 29 3b 0a 20 20 20  ruleno==90);.   
32c45 20 20 20 2f 2a 20 28 39 31 29 20 63 6f 6e 73 6c     /* (91) consl
32c46 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73 20 2a 2f  ist ::= tcons */
32c47 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
32c48 6c 65 6e 6f 3d 3d 39 31 29 3b 0a 20 20 20 20 20  leno==91);.     
32c49 20 2f 2a 20 28 39 32 29 20 74 63 6f 6e 73 20 3a   /* (92) tcons :
32c4a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d  := CONSTRAINT nm
32c4b 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32c4c 79 72 75 6c 65 6e 6f 3d 3d 39 32 29 3b 0a 20 20  yruleno==92);.  
32c4d 20 20 20 20 2f 2a 20 28 32 36 38 29 20 70 6c 75      /* (268) plu
32c4e 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55 53 20 2a  s_opt ::= PLUS *
32c4f 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32c50 75 6c 65 6e 6f 3d 3d 32 36 38 29 3b 0a 20 20 20  uleno==268);.   
32c51 20 20 20 2f 2a 20 28 32 36 39 29 20 70 6c 75 73     /* (269) plus
32c52 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  _opt ::= */ yyte
32c53 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32c54 3d 32 36 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20  =269);.      /* 
32c55 28 32 37 39 29 20 66 6f 72 65 61 63 68 5f 63 6c  (279) foreach_cl
32c56 61 75 73 65 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  ause ::= */ yyte
32c57 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32c58 3d 32 37 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20  =279);.      /* 
32c59 28 32 38 30 29 20 66 6f 72 65 61 63 68 5f 63 6c  (280) foreach_cl
32c5a 61 75 73 65 20 3a 3a 3d 20 46 4f 52 20 45 41 43  ause ::= FOR EAC
32c5b 48 20 52 4f 57 20 2a 2f 20 79 79 74 65 73 74 63  H ROW */ yytestc
32c5c 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 38  ase(yyruleno==28
32c5d 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 38  0);.      /* (28
32c5e 37 29 20 74 72 69 64 78 62 79 20 3a 3a 3d 20 2a  7) tridxby ::= *
32c5f 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32c60 75 6c 65 6e 6f 3d 3d 32 38 37 29 3b 0a 20 20 20  uleno==287);.   
32c61 20 20 20 2f 2a 20 28 33 30 35 29 20 64 61 74 61     /* (305) data
32c62 62 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20  base_kw_opt ::= 
32c63 44 41 54 41 42 41 53 45 20 2a 2f 20 79 79 74 65  DATABASE */ yyte
32c64 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32c65 3d 33 30 35 29 3b 0a 20 20 20 20 20 20 2f 2a 20  =305);.      /* 
32c66 28 33 30 36 29 20 64 61 74 61 62 61 73 65 5f 6b  (306) database_k
32c67 77 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74  w_opt ::= */ yyt
32c68 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32c69 3d 3d 33 30 36 29 3b 0a 20 20 20 20 20 20 2f 2a  ==306);.      /*
32c6a 20 28 33 31 34 29 20 6b 77 63 6f 6c 75 6d 6e 5f   (314) kwcolumn_
32c6b 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
32c6c 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32c6d 33 31 34 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  314);.      /* (
32c6e 33 31 35 29 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70  315) kwcolumn_op
32c6f 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 20 2a  t ::= COLUMNKW *
32c70 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
32c71 75 6c 65 6e 6f 3d 3d 33 31 35 29 3b 0a 20 20 20  uleno==315);.   
32c72 20 20 20 2f 2a 20 28 33 31 39 29 20 76 74 61 62     /* (319) vtab
32c73 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62  arglist ::= vtab
32c74 61 72 67 20 2a 2f 20 79 79 74 65 73 74 63 61 73  arg */ yytestcas
32c75 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 39 29  e(yyruleno==319)
32c76 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 30 29  ;.      /* (320)
32c77 20 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d   vtabarglist ::=
32c78 20 76 74 61 62 61 72 67 6c 69 73 74 20 43 4f 4d   vtabarglist COM
32c79 4d 41 20 76 74 61 62 61 72 67 20 2a 2f 20 79 79  MA vtabarg */ yy
32c7a 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32c7b 6f 3d 3d 33 32 30 29 3b 0a 20 20 20 20 20 20 2f  o==320);.      /
32c7c 2a 20 28 33 32 32 29 20 76 74 61 62 61 72 67 20  * (322) vtabarg 
32c7d 3a 3a 3d 20 76 74 61 62 61 72 67 20 76 74 61 62  ::= vtabarg vtab
32c7e 61 72 67 74 6f 6b 65 6e 20 2a 2f 20 79 79 74 65  argtoken */ yyte
32c7f 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32c80 3d 33 32 32 29 3b 0a 20 20 20 20 20 20 2f 2a 20  =322);.      /* 
32c81 28 33 32 36 29 20 61 6e 79 6c 69 73 74 20 3a 3a  (326) anylist ::
32c82 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
32c83 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 36 29 3b 0a  yyruleno==326);.
32c84 20 20 20 20 20 20 2f 2a 20 28 33 32 37 29 20 61        /* (327) a
32c85 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69  nylist ::= anyli
32c86 73 74 20 4c 50 20 61 6e 79 6c 69 73 74 20 52 50  st LP anylist RP
32c87 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
32c88 79 72 75 6c 65 6e 6f 3d 3d 33 32 37 29 3b 0a 20  yruleno==327);. 
32c89 20 20 20 20 20 2f 2a 20 28 33 32 38 29 20 61 6e       /* (328) an
32c8a 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73  ylist ::= anylis
32c8b 74 20 41 4e 59 20 2a 2f 20 79 79 74 65 73 74 63  t ANY */ yytestc
32c8c 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32  ase(yyruleno==32
32c8d 38 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  8);.        brea
32c8e 6b 3b 0a 20 20 7d 3b 0a 20 20 79 79 67 6f 74 6f  k;.  };.  yygoto
32c8f 20 3d 20 79 79 52 75 6c 65 49 6e 66 6f 5b 79 79   = yyRuleInfo[yy
32c90 72 75 6c 65 6e 6f 5d 2e 6c 68 73 3b 0a 20 20 79  ruleno].lhs;.  y
32c91 79 73 69 7a 65 20 3d 20 79 79 52 75 6c 65 49 6e  ysize = yyRuleIn
32c92 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6e 72 68  fo[yyruleno].nrh
32c93 73 3b 0a 20 20 79 79 70 50 61 72 73 65 72 2d 3e  s;.  yypParser->
32c94 79 79 69 64 78 20 2d 3d 20 79 79 73 69 7a 65 3b  yyidx -= yysize;
32c95 0a 20 20 79 79 61 63 74 20 3d 20 79 79 5f 66 69  .  yyact = yy_fi
32c96 6e 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f 6e  nd_reduce_action
32c97 28 79 79 6d 73 70 5b 2d 79 79 73 69 7a 65 5d 2e  (yymsp[-yysize].
32c98 73 74 61 74 65 6e 6f 2c 28 59 59 43 4f 44 45 54  stateno,(YYCODET
32c99 59 50 45 29 79 79 67 6f 74 6f 29 3b 0a 20 20 69  YPE)yygoto);.  i
32c9a 66 28 20 79 79 61 63 74 20 3c 20 59 59 4e 53 54  f( yyact < YYNST
32c9b 41 54 45 20 29 7b 0a 23 69 66 64 65 66 20 4e 44  ATE ){.#ifdef ND
32c9c 45 42 55 47 0a 20 20 20 20 2f 2a 20 49 66 20 77  EBUG.    /* If w
32c9d 65 20 61 72 65 20 6e 6f 74 20 64 65 62 75 67 67  e are not debugg
32c9e 69 6e 67 20 61 6e 64 20 74 68 65 20 72 65 64 75  ing and the redu
32c9f 63 65 20 61 63 74 69 6f 6e 20 70 6f 70 70 65 64  ce action popped
32ca0 20 61 74 20 6c 65 61 73 74 0a 20 20 20 20 2a 2a   at least.    **
32ca1 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 20 6f 66 66   one element off
32ca2 20 74 68 65 20 73 74 61 63 6b 2c 20 74 68 65 6e   the stack, then
32ca3 20 77 65 20 63 61 6e 20 70 75 73 68 20 74 68 65   we can push the
32ca4 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20 62 61 63   new element bac
32ca5 6b 0a 20 20 20 20 2a 2a 20 6f 6e 74 6f 20 74 68  k.    ** onto th
32ca6 65 20 73 74 61 63 6b 20 68 65 72 65 2c 20 61 6e  e stack here, an
32ca7 64 20 73 6b 69 70 20 74 68 65 20 73 74 61 63 6b  d skip the stack
32ca8 20 6f 76 65 72 66 6c 6f 77 20 74 65 73 74 20 69   overflow test i
32ca9 6e 20 79 79 5f 73 68 69 66 74 28 29 2e 0a 20 20  n yy_shift()..  
32caa 20 20 2a 2a 20 54 68 61 74 20 67 69 76 65 73 20    ** That gives 
32cab 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 73 70  a significant sp
32cac 65 65 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74 2e  eed improvement.
32cad 20 2a 2f 0a 20 20 20 20 69 66 28 20 79 79 73 69   */.    if( yysi
32cae 7a 65 20 29 7b 0a 20 20 20 20 20 20 79 79 70 50  ze ){.      yypP
32caf 61 72 73 65 72 2d 3e 79 79 69 64 78 2b 2b 3b 0a  arser->yyidx++;.
32cb0 20 20 20 20 20 20 79 79 6d 73 70 20 2d 3d 20 79        yymsp -= y
32cb1 79 73 69 7a 65 2d 31 3b 0a 20 20 20 20 20 20 79  ysize-1;.      y
32cb2 79 6d 73 70 2d 3e 73 74 61 74 65 6e 6f 20 3d 20  ymsp->stateno = 
32cb3 28 59 59 41 43 54 49 4f 4e 54 59 50 45 29 79 79  (YYACTIONTYPE)yy
32cb4 61 63 74 3b 0a 20 20 20 20 20 20 79 79 6d 73 70  act;.      yymsp
32cb5 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59 59 43 4f 44  ->major = (YYCOD
32cb6 45 54 59 50 45 29 79 79 67 6f 74 6f 3b 0a 20 20  ETYPE)yygoto;.  
32cb7 20 20 20 20 79 79 6d 73 70 2d 3e 6d 69 6e 6f 72      yymsp->minor
32cb8 20 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 0a   = yygotominor;.
32cb9 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
32cba 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 79 79 5f  .    {.      yy_
32cbb 73 68 69 66 74 28 79 79 70 50 61 72 73 65 72 2c  shift(yypParser,
32cbc 79 79 61 63 74 2c 79 79 67 6f 74 6f 2c 26 79 79  yyact,yygoto,&yy
32cbd 67 6f 74 6f 6d 69 6e 6f 72 29 3b 0a 20 20 20 20  gotominor);.    
32cbe 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  }.  }else{.    a
32cbf 73 73 65 72 74 28 20 79 79 61 63 74 20 3d 3d 20  ssert( yyact == 
32cc0 59 59 4e 53 54 41 54 45 20 2b 20 59 59 4e 52 55  YYNSTATE + YYNRU
32cc1 4c 45 20 2b 20 31 20 29 3b 0a 20 20 20 20 79 79  LE + 1 );.    yy
32cc2 5f 61 63 63 65 70 74 28 79 79 70 50 61 72 73 65  _accept(yypParse
32cc3 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  r);.  }.}../*.**
32cc4 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
32cc5 6f 64 65 20 65 78 65 63 75 74 65 73 20 77 68 65  ode executes whe
32cc6 6e 20 74 68 65 20 70 61 72 73 65 20 66 61 69 6c  n the parse fail
32cc7 73 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 59 59 4e  s.*/.#ifndef YYN
32cc8 4f 45 52 52 4f 52 52 45 43 4f 56 45 52 59 0a 73  OERRORRECOVERY.s
32cc9 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 70 61  tatic void yy_pa
32cca 72 73 65 5f 66 61 69 6c 65 64 28 0a 20 20 79 79  rse_failed(.  yy
32ccb 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65  Parser *yypParse
32ccc 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  r           /* T
32ccd 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a  he parser */.){.
32cce 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
32ccf 52 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64 65  RG_FETCH;.#ifnde
32cd0 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79  f NDEBUG.  if( y
32cd1 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
32cd2 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
32cd3 65 46 49 4c 45 2c 22 25 73 46 61 69 6c 21 5c 6e  eFILE,"%sFail!\n
32cd4 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29  ",yyTracePrompt)
32cd5 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77  ;.  }.#endif.  w
32cd6 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d  hile( yypParser-
32cd7 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70  >yyidx>=0 ) yy_p
32cd8 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28  op_parser_stack(
32cd9 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a  yypParser);.  /*
32cda 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e   Here code is in
32cdb 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69 6c  serted which wil
32cdc 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77 68  l be executed wh
32cdd 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a 20  enever the.  ** 
32cde 70 61 72 73 65 72 20 66 61 69 6c 73 20 2a 2f 0a  parser fails */.
32cdf 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
32ce0 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70  RG_STORE; /* Sup
32ce1 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62  press warning ab
32ce2 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72  out unused %extr
32ce3 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61  a_argument varia
32ce4 62 6c 65 20 2a 2f 0a 7d 0a 23 65 6e 64 69 66 20  ble */.}.#endif 
32ce5 2f 2a 20 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f  /* YYNOERRORRECO
32ce6 56 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  VERY */../*.** T
32ce7 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64  he following cod
32ce8 65 20 65 78 65 63 75 74 65 73 20 77 68 65 6e 20  e executes when 
32ce9 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72 20 66  a syntax error f
32cea 69 72 73 74 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a  irst occurs..*/.
32ceb 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 73  static void yy_s
32cec 79 6e 74 61 78 5f 65 72 72 6f 72 28 0a 20 20 79  yntax_error(.  y
32ced 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
32cee 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  er,           /*
32cef 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20   The parser */. 
32cf0 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20   int yymajor,   
32cf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32cf2 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 79 70  /* The major typ
32cf3 65 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 74  e of the error t
32cf4 6f 6b 65 6e 20 2a 2f 0a 20 20 59 59 4d 49 4e 4f  oken */.  YYMINO
32cf5 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 20 20 20  RTYPE yyminor   
32cf6 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
32cf7 6d 69 6e 6f 72 20 74 79 70 65 20 6f 66 20 74 68  minor type of th
32cf8 65 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20 2a 2f  e error token */
32cf9 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72  .){.  sqlite3Par
32cfa 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 23 64  serARG_FETCH;.#d
32cfb 65 66 69 6e 65 20 54 4f 4b 45 4e 20 28 79 79 6d  efine TOKEN (yym
32cfc 69 6e 6f 72 2e 79 79 30 29 0a 0a 20 20 55 4e 55  inor.yy0)..  UNU
32cfd 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79 79  SED_PARAMETER(yy
32cfe 6d 61 6a 6f 72 29 3b 20 20 2f 2a 20 53 69 6c 65  major);  /* Sile
32cff 6e 63 65 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65  nce some compile
32d00 72 20 77 61 72 6e 69 6e 67 73 20 2a 2f 0a 20 20  r warnings */.  
32d01 61 73 73 65 72 74 28 20 54 4f 4b 45 4e 2e 7a 5b  assert( TOKEN.z[
32d02 30 5d 20 29 3b 20 20 2f 2a 20 54 68 65 20 74 6f  0] );  /* The to
32d03 6b 65 6e 69 7a 65 72 20 61 6c 77 61 79 73 20 67  kenizer always g
32d04 69 76 65 73 20 75 73 20 61 20 74 6f 6b 65 6e 20  ives us a token 
32d05 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f  */.  sqlite3Erro
32d06 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 65  rMsg(pParse, "ne
32d07 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74 61  ar \"%T\": synta
32d08 78 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b 45 4e  x error", &TOKEN
32d09 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70 61 72  );.  pParse->par
32d0a 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20 73  seError = 1;.  s
32d0b 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
32d0c 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70 72 65  STORE; /* Suppre
32d0d 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f 75 74  ss warning about
32d0e 20 75 6e 75 73 65 64 20 25 65 78 74 72 61 5f 61   unused %extra_a
32d0f 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62 6c 65  rgument variable
32d10 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   */.}../*.** The
32d11 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 65 78   following is ex
32d12 65 63 75 74 65 64 20 77 68 65 6e 20 74 68 65 20  ecuted when the 
32d13 70 61 72 73 65 72 20 61 63 63 65 70 74 73 0a 2a  parser accepts.*
32d14 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79 79  /.static void yy
32d15 5f 61 63 63 65 70 74 28 0a 20 20 79 79 50 61 72  _accept(.  yyPar
32d16 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 20 20  ser *yypParser  
32d17 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
32d18 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20 20 73  parser */.){.  s
32d19 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f  qlite3ParserARG_
32d1a 46 45 54 43 48 3b 0a 23 69 66 6e 64 65 66 20 4e  FETCH;.#ifndef N
32d1b 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72  DEBUG.  if( yyTr
32d1c 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66  aceFILE ){.    f
32d1d 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
32d1e 4c 45 2c 22 25 73 41 63 63 65 70 74 21 5c 6e 22  LE,"%sAccept!\n"
32d1f 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 29 3b  ,yyTracePrompt);
32d20 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 77 68  .  }.#endif.  wh
32d21 69 6c 65 28 20 79 79 70 50 61 72 73 65 72 2d 3e  ile( yypParser->
32d22 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f  yyidx>=0 ) yy_po
32d23 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79  p_parser_stack(y
32d24 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f 2a 20  ypParser);.  /* 
32d25 48 65 72 65 20 63 6f 64 65 20 69 73 20 69 6e 73  Here code is ins
32d26 65 72 74 65 64 20 77 68 69 63 68 20 77 69 6c 6c  erted which will
32d27 20 62 65 20 65 78 65 63 75 74 65 64 20 77 68 65   be executed whe
32d28 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a 20 70  never the.  ** p
32d29 61 72 73 65 72 20 61 63 63 65 70 74 73 20 2a 2f  arser accepts */
32d2a 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
32d2b 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75  ARG_STORE; /* Su
32d2c 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61  ppress warning a
32d2d 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74  bout unused %ext
32d2e 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69  ra_argument vari
32d2f 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20 54 68  able */.}../* Th
32d30 65 20 6d 61 69 6e 20 70 61 72 73 65 72 20 70 72  e main parser pr
32d31 6f 67 72 61 6d 2e 0a 2a 2a 20 54 68 65 20 66 69  ogram..** The fi
32d32 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20  rst argument is 
32d33 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73  a pointer to a s
32d34 74 72 75 63 74 75 72 65 20 6f 62 74 61 69 6e 65  tructure obtaine
32d35 64 20 66 72 6f 6d 0a 2a 2a 20 22 73 71 6c 69 74  d from.** "sqlit
32d36 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 22 20 77  e3ParserAlloc" w
32d37 68 69 63 68 20 64 65 73 63 72 69 62 65 73 20 74  hich describes t
32d38 68 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65  he current state
32d39 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 2e 0a   of the parser..
32d3a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  ** The second ar
32d3b 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6d 61  gument is the ma
32d3c 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72  jor token number
32d3d 2e 20 20 54 68 65 20 74 68 69 72 64 20 69 73 0a  .  The third is.
32d3e 2a 2a 20 74 68 65 20 6d 69 6e 6f 72 20 74 6f 6b  ** the minor tok
32d3f 65 6e 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20  en.  The fourth 
32d40 6f 70 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65 6e  optional argumen
32d41 74 20 69 73 20 77 68 61 74 65 76 65 72 20 74 68  t is whatever th
32d42 65 0a 2a 2a 20 75 73 65 72 20 77 61 6e 74 73 20  e.** user wants 
32d43 28 61 6e 64 20 73 70 65 63 69 66 69 65 64 20 69  (and specified i
32d44 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 29 20 61  n the grammar) a
32d45 6e 64 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  nd is available 
32d46 66 6f 72 0a 2a 2a 20 75 73 65 20 62 79 20 74 68  for.** use by th
32d47 65 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65  e action routine
32d48 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a  s..**.** Inputs:
32d49 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
32d4a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
32d4b 65 20 70 61 72 73 65 72 20 28 61 6e 20 6f 70 61  e parser (an opa
32d4c 71 75 65 20 73 74 72 75 63 74 75 72 65 2e 29 0a  que structure.).
32d4d 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 61 6a 6f  ** <li> The majo
32d4e 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a  r token number..
32d4f 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 69 6e 6f  ** <li> The mino
32d50 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 2e 0a  r token number..
32d51 2a 2a 20 3c 6c 69 3e 20 41 6e 20 6f 70 74 69 6f  ** <li> An optio
32d52 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 61 20  n argument of a 
32d53 67 72 61 6d 6d 61 72 2d 73 70 65 63 69 66 69 65  grammar-specifie
32d54 64 20 74 79 70 65 2e 0a 2a 2a 20 3c 2f 75 6c 3e  d type..** </ul>
32d55 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a  .**.** Outputs:.
32d56 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49  ** None..*/.SQLI
32d57 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
32d58 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 0a 20  sqlite3Parser(. 
32d59 20 76 6f 69 64 20 2a 79 79 70 2c 20 20 20 20 20   void *yyp,     
32d5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32d5b 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20   The parser */. 
32d5c 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20 20 20   int yymajor,   
32d5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
32d5e 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e   The major token
32d5f 20 63 6f 64 65 20 6e 75 6d 62 65 72 20 2a 2f 0a   code number */.
32d60 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54    sqlite3ParserT
32d61 4f 4b 45 4e 54 59 50 45 20 79 79 6d 69 6e 6f 72  OKENTYPE yyminor
32d62 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61         /* The va
32d63 6c 75 65 20 66 6f 72 20 74 68 65 20 74 6f 6b 65  lue for the toke
32d64 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61  n */.  sqlite3Pa
32d65 72 73 65 72 41 52 47 5f 50 44 45 43 4c 20 20 20  rserARG_PDECL   
32d66 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
32d67 70 74 69 6f 6e 61 6c 20 25 65 78 74 72 61 5f 61  ptional %extra_a
32d68 72 67 75 6d 65 6e 74 20 70 61 72 61 6d 65 74 65  rgument paramete
32d69 72 20 2a 2f 0a 29 7b 0a 20 20 59 59 4d 49 4e 4f  r */.){.  YYMINO
32d6a 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 75 6e 69  RTYPE yyminoruni
32d6b 6f 6e 3b 0a 20 20 69 6e 74 20 79 79 61 63 74 3b  on;.  int yyact;
32d6c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
32d6d 68 65 20 70 61 72 73 65 72 20 61 63 74 69 6f 6e  he parser action
32d6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 79 79 65 6e 64  . */.  int yyend
32d6f 6f 66 69 6e 70 75 74 3b 20 20 20 20 20 2f 2a 20  ofinput;     /* 
32d70 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20 61  True if we are a
32d71 74 20 74 68 65 20 65 6e 64 20 6f 66 20 69 6e 70  t the end of inp
32d72 75 74 20 2a 2f 0a 23 69 66 64 65 66 20 59 59 45  ut */.#ifdef YYE
32d73 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69 6e 74  RRORSYMBOL.  int
32d74 20 79 79 65 72 72 6f 72 68 69 74 20 3d 20 30 3b   yyerrorhit = 0;
32d75 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 79 79     /* True if yy
32d76 6d 61 6a 6f 72 20 68 61 73 20 69 6e 76 6f 6b 65  major has invoke
32d77 64 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 23 65  d an error */.#e
32d78 6e 64 69 66 0a 20 20 79 79 50 61 72 73 65 72 20  ndif.  yyParser 
32d79 2a 79 79 70 50 61 72 73 65 72 3b 20 20 2f 2a 20  *yypParser;  /* 
32d7a 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 0a 20  The parser */.. 
32d7b 20 2f 2a 20 28 72 65 29 69 6e 69 74 69 61 6c 69   /* (re)initiali
32d7c 7a 65 20 74 68 65 20 70 61 72 73 65 72 2c 20 69  ze the parser, i
32d7d 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f 0a 20  f necessary */. 
32d7e 20 79 79 70 50 61 72 73 65 72 20 3d 20 28 79 79   yypParser = (yy
32d7f 50 61 72 73 65 72 2a 29 79 79 70 3b 0a 20 20 69  Parser*)yyp;.  i
32d80 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79  f( yypParser->yy
32d81 69 64 78 3c 30 20 29 7b 0a 23 69 66 20 59 59 53  idx<0 ){.#if YYS
32d82 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20  TACKDEPTH<=0.   
32d83 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e   if( yypParser->
32d84 79 79 73 74 6b 73 7a 20 3c 3d 30 20 29 7b 0a 20  yystksz <=0 ){. 
32d85 20 20 20 20 20 2f 2a 6d 65 6d 73 65 74 28 26 79       /*memset(&y
32d86 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2c 20 30 2c 20  yminorunion, 0, 
32d87 73 69 7a 65 6f 66 28 79 79 6d 69 6e 6f 72 75 6e  sizeof(yyminorun
32d88 69 6f 6e 29 29 3b 2a 2f 0a 20 20 20 20 20 20 79  ion));*/.      y
32d89 79 6d 69 6e 6f 72 75 6e 69 6f 6e 20 3d 20 79 79  yminorunion = yy
32d8a 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 20 20 20 20 20  zerominor;.     
32d8b 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f 77   yyStackOverflow
32d8c 28 79 79 70 50 61 72 73 65 72 2c 20 26 79 79 6d  (yypParser, &yym
32d8d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20  inorunion);.    
32d8e 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
32d8f 23 65 6e 64 69 66 0a 20 20 20 20 79 79 70 50 61  #endif.    yypPa
32d90 72 73 65 72 2d 3e 79 79 69 64 78 20 3d 20 30 3b  rser->yyidx = 0;
32d91 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e  .    yypParser->
32d92 79 79 65 72 72 63 6e 74 20 3d 20 2d 31 3b 0a 20  yyerrcnt = -1;. 
32d93 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
32d94 73 74 61 63 6b 5b 30 5d 2e 73 74 61 74 65 6e 6f  stack[0].stateno
32d95 20 3d 20 30 3b 0a 20 20 20 20 79 79 70 50 61 72   = 0;.    yypPar
32d96 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30 5d 2e  ser->yystack[0].
32d97 6d 61 6a 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 20  major = 0;.  }. 
32d98 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2e 79 79   yyminorunion.yy
32d99 30 20 3d 20 79 79 6d 69 6e 6f 72 3b 0a 20 20 79  0 = yyminor;.  y
32d9a 79 65 6e 64 6f 66 69 6e 70 75 74 20 3d 20 28 79  yendofinput = (y
32d9b 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0a 20 20 73 71  ymajor==0);.  sq
32d9c 6c 69 74 65 33 50 61 72 73 65 72 41 52 47 5f 53  lite3ParserARG_S
32d9d 54 4f 52 45 3b 0a 0a 23 69 66 6e 64 65 66 20 4e  TORE;..#ifndef N
32d9e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54 72  DEBUG.  if( yyTr
32d9f 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 66  aceFILE ){.    f
32da0 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
32da1 4c 45 2c 22 25 73 49 6e 70 75 74 20 25 73 5c 6e  LE,"%sInput %s\n
32da2 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c  ",yyTracePrompt,
32da3 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61  yyTokenName[yyma
32da4 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  jor]);.  }.#endi
32da5 66 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 79 79 61  f..  do{.    yya
32da6 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 73 68 69  ct = yy_find_shi
32da7 66 74 5f 61 63 74 69 6f 6e 28 79 79 70 50 61 72  ft_action(yypPar
32da8 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45 29  ser,(YYCODETYPE)
32da9 79 79 6d 61 6a 6f 72 29 3b 0a 20 20 20 20 69 66  yymajor);.    if
32daa 28 20 79 79 61 63 74 3c 59 59 4e 53 54 41 54 45  ( yyact<YYNSTATE
32dab 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
32dac 28 20 21 79 79 65 6e 64 6f 66 69 6e 70 75 74 20  ( !yyendofinput 
32dad 29 3b 20 20 2f 2a 20 49 6d 70 6f 73 73 69 62 6c  );  /* Impossibl
32dae 65 20 74 6f 20 73 68 69 66 74 20 74 68 65 20 24  e to shift the $
32daf 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20   token */.      
32db0 79 79 5f 73 68 69 66 74 28 79 79 70 50 61 72 73  yy_shift(yypPars
32db1 65 72 2c 79 79 61 63 74 2c 79 79 6d 61 6a 6f 72  er,yyact,yymajor
32db2 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b  ,&yyminorunion);
32db3 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72  .      yypParser
32db4 2d 3e 79 79 65 72 72 63 6e 74 2d 2d 3b 0a 20 20  ->yyerrcnt--;.  
32db5 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59      yymajor = YY
32db6 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 7d 65 6c 73  NOCODE;.    }els
32db7 65 20 69 66 28 20 79 79 61 63 74 20 3c 20 59 59  e if( yyact < YY
32db8 4e 53 54 41 54 45 20 2b 20 59 59 4e 52 55 4c 45  NSTATE + YYNRULE
32db9 20 29 7b 0a 20 20 20 20 20 20 79 79 5f 72 65 64   ){.      yy_red
32dba 75 63 65 28 79 79 70 50 61 72 73 65 72 2c 79 79  uce(yypParser,yy
32dbb 61 63 74 2d 59 59 4e 53 54 41 54 45 29 3b 0a 20  act-YYNSTATE);. 
32dbc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
32dbd 61 73 73 65 72 74 28 20 79 79 61 63 74 20 3d 3d  assert( yyact ==
32dbe 20 59 59 5f 45 52 52 4f 52 5f 41 43 54 49 4f 4e   YY_ERROR_ACTION
32dbf 20 29 3b 0a 23 69 66 64 65 66 20 59 59 45 52 52   );.#ifdef YYERR
32dc0 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20 20 20 69  ORSYMBOL.      i
32dc1 6e 74 20 79 79 6d 78 3b 0a 23 65 6e 64 69 66 0a  nt yymx;.#endif.
32dc2 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
32dc3 20 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65       if( yyTrace
32dc4 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20  FILE ){.        
32dc5 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
32dc6 49 4c 45 2c 22 25 73 53 79 6e 74 61 78 20 45 72  ILE,"%sSyntax Er
32dc7 72 6f 72 21 5c 6e 22 2c 79 79 54 72 61 63 65 50  ror!\n",yyTraceP
32dc8 72 6f 6d 70 74 29 3b 0a 20 20 20 20 20 20 7d 0a  rompt);.      }.
32dc9 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 59 59  #endif.#ifdef YY
32dca 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20 20  ERRORSYMBOL.    
32dcb 20 20 2f 2a 20 41 20 73 79 6e 74 61 78 20 65 72    /* A syntax er
32dcc 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65 64  ror has occurred
32dcd 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 20 72  ..      ** The r
32dce 65 73 70 6f 6e 73 65 20 74 6f 20 61 6e 20 65 72  esponse to an er
32dcf 72 6f 72 20 64 65 70 65 6e 64 73 20 75 70 6f 6e  ror depends upon
32dd0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
32dd1 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 67 72 61  the.      ** gra
32dd2 6d 6d 61 72 20 64 65 66 69 6e 65 73 20 61 6e 20  mmar defines an 
32dd3 65 72 72 6f 72 20 74 6f 6b 65 6e 20 22 45 52 52  error token "ERR
32dd4 4f 52 22 2e 20 20 0a 20 20 20 20 20 20 2a 2a 0a  OR".  .      **.
32dd5 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73        ** This is
32dd6 20 77 68 61 74 20 77 65 20 64 6f 20 69 66 20 74   what we do if t
32dd7 68 65 20 67 72 61 6d 6d 61 72 20 64 6f 65 73 20  he grammar does 
32dd8 64 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20  define ERROR:.  
32dd9 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
32dda 20 2a 20 43 61 6c 6c 20 74 68 65 20 25 73 79 6e   * Call the %syn
32ddb 74 61 78 5f 65 72 72 6f 72 20 66 75 6e 63 74 69  tax_error functi
32ddc 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  on..      **.   
32ddd 20 20 20 2a 2a 20 20 2a 20 42 65 67 69 6e 20 70     **  * Begin p
32dde 6f 70 70 69 6e 67 20 74 68 65 20 73 74 61 63 6b  opping the stack
32ddf 20 75 6e 74 69 6c 20 77 65 20 65 6e 74 65 72 20   until we enter 
32de0 61 20 73 74 61 74 65 20 77 68 65 72 65 0a 20 20  a state where.  
32de1 20 20 20 20 2a 2a 20 20 20 20 69 74 20 69 73 20      **    it is 
32de2 6c 65 67 61 6c 20 74 6f 20 73 68 69 66 74 20 74  legal to shift t
32de3 68 65 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2c  he error symbol,
32de4 20 74 68 65 6e 20 73 68 69 66 74 0a 20 20 20 20   then shift.    
32de5 20 20 2a 2a 20 20 20 20 74 68 65 20 65 72 72 6f    **    the erro
32de6 72 20 73 79 6d 62 6f 6c 2e 0a 20 20 20 20 20 20  r symbol..      
32de7 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 53  **.      **  * S
32de8 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 75  et the error cou
32de9 6e 74 20 74 6f 20 74 68 72 65 65 2e 0a 20 20 20  nt to three..   
32dea 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
32deb 2a 20 42 65 67 69 6e 20 61 63 63 65 70 74 69 6e  * Begin acceptin
32dec 67 20 61 6e 64 20 73 68 69 66 74 69 6e 67 20 6e  g and shifting n
32ded 65 77 20 74 6f 6b 65 6e 73 2e 20 20 4e 6f 20 6e  ew tokens.  No n
32dee 65 77 20 65 72 72 6f 72 0a 20 20 20 20 20 20 2a  ew error.      *
32def 2a 20 20 20 20 70 72 6f 63 65 73 73 69 6e 67 20  *    processing 
32df0 77 69 6c 6c 20 6f 63 63 75 72 20 75 6e 74 69 6c  will occur until
32df1 20 74 68 72 65 65 20 74 6f 6b 65 6e 73 20 68 61   three tokens ha
32df2 76 65 20 62 65 65 6e 0a 20 20 20 20 20 20 2a 2a  ve been.      **
32df3 20 20 20 20 73 68 69 66 74 65 64 20 73 75 63 63      shifted succ
32df4 65 73 73 66 75 6c 6c 79 2e 0a 20 20 20 20 20 20  essfully..      
32df5 2a 2a 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  **.      */.    
32df6 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
32df7 3e 79 79 65 72 72 63 6e 74 3c 30 20 29 7b 0a 20  >yyerrcnt<0 ){. 
32df8 20 20 20 20 20 20 20 79 79 5f 73 79 6e 74 61 78         yy_syntax
32df9 5f 65 72 72 6f 72 28 79 79 70 50 61 72 73 65 72  _error(yypParser
32dfa 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69 6e 6f 72  ,yymajor,yyminor
32dfb 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  union);.      }.
32dfc 20 20 20 20 20 20 79 79 6d 78 20 3d 20 79 79 70        yymx = yyp
32dfd 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b  Parser->yystack[
32dfe 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
32dff 5d 2e 6d 61 6a 6f 72 3b 0a 20 20 20 20 20 20 69  ].major;.      i
32e00 66 28 20 79 79 6d 78 3d 3d 59 59 45 52 52 4f 52  f( yymx==YYERROR
32e01 53 59 4d 42 4f 4c 20 7c 7c 20 79 79 65 72 72 6f  SYMBOL || yyerro
32e02 72 68 69 74 20 29 7b 0a 23 69 66 6e 64 65 66 20  rhit ){.#ifndef 
32e03 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20 20 69  NDEBUG.        i
32e04 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
32e05 7b 0a 20 20 20 20 20 20 20 20 20 20 66 70 72 69  {.          fpri
32e06 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
32e07 22 25 73 44 69 73 63 61 72 64 20 69 6e 70 75 74  "%sDiscard input
32e08 20 74 6f 6b 65 6e 20 25 73 5c 6e 22 2c 0a 20 20   token %s\n",.  
32e09 20 20 20 20 20 20 20 20 20 20 20 79 79 54 72 61             yyTra
32e0a 63 65 50 72 6f 6d 70 74 2c 79 79 54 6f 6b 65 6e  cePrompt,yyToken
32e0b 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72 5d 29 3b 0a  Name[yymajor]);.
32e0c 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66          }.#endif
32e0d 0a 20 20 20 20 20 20 20 20 79 79 5f 64 65 73 74  .        yy_dest
32e0e 72 75 63 74 6f 72 28 79 79 70 50 61 72 73 65 72  ructor(yypParser
32e0f 2c 20 28 59 59 43 4f 44 45 54 59 50 45 29 79 79  , (YYCODETYPE)yy
32e10 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e  major,&yyminorun
32e11 69 6f 6e 29 3b 0a 20 20 20 20 20 20 20 20 79 79  ion);.        yy
32e12 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45  major = YYNOCODE
32e13 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
32e14 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 0a 20          while(. 
32e15 20 20 20 20 20 20 20 20 20 79 79 70 50 61 72 73           yypPars
32e16 65 72 2d 3e 79 79 69 64 78 20 3e 3d 20 30 20 26  er->yyidx >= 0 &
32e17 26 0a 20 20 20 20 20 20 20 20 20 20 79 79 6d 78  &.          yymx
32e18 20 21 3d 20 59 59 45 52 52 4f 52 53 59 4d 42 4f   != YYERRORSYMBO
32e19 4c 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 28  L &&.          (
32e1a 79 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f  yyact = yy_find_
32e1b 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28 0a 20  reduce_action(. 
32e1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32e1d 20 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72         yypParser
32e1e 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72  ->yystack[yypPar
32e1f 73 65 72 2d 3e 79 79 69 64 78 5d 2e 73 74 61 74  ser->yyidx].stat
32e20 65 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20  eno,.           
32e21 20 20 20 20 20 20 20 20 20 20 20 20 20 59 59 45               YYE
32e22 52 52 4f 52 53 59 4d 42 4f 4c 29 29 20 3e 3d 20  RRORSYMBOL)) >= 
32e23 59 59 4e 53 54 41 54 45 0a 20 20 20 20 20 20 20  YYNSTATE.       
32e24 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 79 79   ){.          yy
32e25 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63  _pop_parser_stac
32e26 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20  k(yypParser);.  
32e27 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
32e28 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e 79  if( yypParser->y
32e29 79 69 64 78 20 3c 20 30 20 7c 7c 20 79 79 6d 61  yidx < 0 || yyma
32e2a 6a 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  jor==0 ){.      
32e2b 20 20 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f      yy_destructo
32e2c 72 28 79 79 70 50 61 72 73 65 72 2c 28 59 59 43  r(yypParser,(YYC
32e2d 4f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c  ODETYPE)yymajor,
32e2e 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a  &yyminorunion);.
32e2f 20 20 20 20 20 20 20 20 20 20 79 79 5f 70 61 72            yy_par
32e30 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61 72  se_failed(yypPar
32e31 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ser);.          
32e32 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f  yymajor = YYNOCO
32e33 44 45 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  DE;.        }els
32e34 65 20 69 66 28 20 79 79 6d 78 21 3d 59 59 45 52  e if( yymx!=YYER
32e35 52 4f 52 53 59 4d 42 4f 4c 20 29 7b 0a 20 20 20  RORSYMBOL ){.   
32e36 20 20 20 20 20 20 20 59 59 4d 49 4e 4f 52 54 59         YYMINORTY
32e37 50 45 20 75 32 3b 0a 20 20 20 20 20 20 20 20 20  PE u2;.         
32e38 20 75 32 2e 59 59 45 52 52 53 59 4d 44 54 20 3d   u2.YYERRSYMDT =
32e39 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79   0;.          yy
32e3a 5f 73 68 69 66 74 28 79 79 70 50 61 72 73 65 72  _shift(yypParser
32e3b 2c 79 79 61 63 74 2c 59 59 45 52 52 4f 52 53 59  ,yyact,YYERRORSY
32e3c 4d 42 4f 4c 2c 26 75 32 29 3b 0a 20 20 20 20 20  MBOL,&u2);.     
32e3d 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
32e3e 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
32e3f 65 72 72 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20  errcnt = 3;.    
32e40 20 20 79 79 65 72 72 6f 72 68 69 74 20 3d 20 31    yyerrorhit = 1
32e41 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28  ;.#elif defined(
32e42 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45 52  YYNOERRORRECOVER
32e43 59 29 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  Y).      /* If t
32e44 68 65 20 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f  he YYNOERRORRECO
32e45 56 45 52 59 20 6d 61 63 72 6f 20 69 73 20 64 65  VERY macro is de
32e46 66 69 6e 65 64 2c 20 74 68 65 6e 20 64 6f 20 6e  fined, then do n
32e47 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 0a 20 20  ot attempt to.  
32e48 20 20 20 20 2a 2a 20 64 6f 20 61 6e 79 20 6b 69      ** do any ki
32e49 6e 64 20 6f 66 20 65 72 72 6f 72 20 72 65 63 6f  nd of error reco
32e4a 76 65 72 79 2e 20 20 49 6e 73 74 65 61 64 2c 20  very.  Instead, 
32e4b 73 69 6d 70 6c 79 20 69 6e 76 6f 6b 65 20 74 68  simply invoke th
32e4c 65 20 73 79 6e 74 61 78 0a 20 20 20 20 20 20 2a  e syntax.      *
32e4d 2a 20 65 72 72 6f 72 20 72 6f 75 74 69 6e 65 20  * error routine 
32e4e 61 6e 64 20 63 6f 6e 74 69 6e 75 65 20 67 6f 69  and continue goi
32e4f 6e 67 20 61 73 20 69 66 20 6e 6f 74 68 69 6e 67  ng as if nothing
32e50 20 68 61 64 20 68 61 70 70 65 6e 65 64 2e 0a 20   had happened.. 
32e51 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
32e52 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 63 61   Applications ca
32e53 6e 20 73 65 74 20 74 68 69 73 20 6d 61 63 72 6f  n set this macro
32e54 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 69 6e   (for example in
32e55 73 69 64 65 20 25 69 6e 63 6c 75 64 65 29 20 69  side %include) i
32e56 66 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 79 20  f.      ** they 
32e57 69 6e 74 65 6e 64 20 74 6f 20 61 62 61 6e 64 6f  intend to abando
32e58 6e 20 74 68 65 20 70 61 72 73 65 20 75 70 6f 6e  n the parse upon
32e59 20 74 68 65 20 66 69 72 73 74 20 73 79 6e 74 61   the first synta
32e5a 78 20 65 72 72 6f 72 20 73 65 65 6e 2e 0a 20 20  x error seen..  
32e5b 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 79 79 5f      */.      yy_
32e5c 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70  syntax_error(yyp
32e5d 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79  Parser,yymajor,y
32e5e 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
32e5f 20 20 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f      yy_destructo
32e60 72 28 79 79 70 50 61 72 73 65 72 2c 28 59 59 43  r(yypParser,(YYC
32e61 4f 44 45 54 59 50 45 29 79 79 6d 61 6a 6f 72 2c  ODETYPE)yymajor,
32e62 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a  &yyminorunion);.
32e63 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20        yymajor = 
32e64 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20  YYNOCODE;.      
32e65 0a 23 65 6c 73 65 20 20 2f 2a 20 59 59 45 52 52  .#else  /* YYERR
32e66 4f 52 53 59 4d 42 4f 4c 20 69 73 20 6e 6f 74 20  ORSYMBOL is not 
32e67 64 65 66 69 6e 65 64 20 2a 2f 0a 20 20 20 20 20  defined */.     
32e68 20 2f 2a 20 54 68 69 73 20 69 73 20 77 68 61 74   /* This is what
32e69 20 77 65 20 64 6f 20 69 66 20 74 68 65 20 67 72   we do if the gr
32e6a 61 6d 6d 61 72 20 64 6f 65 73 20 6e 6f 74 20 64  ammar does not d
32e6b 65 66 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20  efine ERROR:.   
32e6c 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
32e6d 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f  * Report an erro
32e6e 72 20 6d 65 73 73 61 67 65 2c 20 61 6e 64 20 74  r message, and t
32e6f 68 72 6f 77 20 61 77 61 79 20 74 68 65 20 69 6e  hrow away the in
32e70 70 75 74 20 74 6f 6b 65 6e 2e 0a 20 20 20 20 20  put token..     
32e71 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20   **.      **  * 
32e72 49 66 20 74 68 65 20 69 6e 70 75 74 20 74 6f 6b  If the input tok
32e73 65 6e 20 69 73 20 24 2c 20 74 68 65 6e 20 66 61  en is $, then fa
32e74 69 6c 20 74 68 65 20 70 61 72 73 65 2e 0a 20 20  il the parse..  
32e75 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
32e76 41 73 20 62 65 66 6f 72 65 2c 20 73 75 62 73 65  As before, subse
32e77 71 75 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73  quent error mess
32e78 61 67 65 73 20 61 72 65 20 73 75 70 70 72 65 73  ages are suppres
32e79 73 65 64 20 75 6e 74 69 6c 0a 20 20 20 20 20 20  sed until.      
32e7a 2a 2a 20 74 68 72 65 65 20 69 6e 70 75 74 20 74  ** three input t
32e7b 6f 6b 65 6e 73 20 68 61 76 65 20 62 65 65 6e 20  okens have been 
32e7c 73 75 63 63 65 73 73 66 75 6c 6c 79 20 73 68 69  successfully shi
32e7d 66 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  fted..      */. 
32e7e 20 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73       if( yypPars
32e7f 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c 3d 30 20  er->yyerrcnt<=0 
32e80 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79  ){.        yy_sy
32e81 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61  ntax_error(yypPa
32e82 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d  rser,yymajor,yym
32e83 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20  inorunion);.    
32e84 20 20 7d 0a 20 20 20 20 20 20 79 79 70 50 61 72    }.      yypPar
32e85 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20  ser->yyerrcnt = 
32e86 33 3b 0a 20 20 20 20 20 20 79 79 5f 64 65 73 74  3;.      yy_dest
32e87 72 75 63 74 6f 72 28 79 79 70 50 61 72 73 65 72  ructor(yypParser
32e88 2c 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d  ,(YYCODETYPE)yym
32e89 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69  ajor,&yyminoruni
32e8a 6f 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 79  on);.      if( y
32e8b 79 65 6e 64 6f 66 69 6e 70 75 74 20 29 7b 0a 20  yendofinput ){. 
32e8c 20 20 20 20 20 20 20 79 79 5f 70 61 72 73 65 5f         yy_parse_
32e8d 66 61 69 6c 65 64 28 79 79 70 50 61 72 73 65 72  failed(yypParser
32e8e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
32e8f 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43   yymajor = YYNOC
32e90 4f 44 45 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  ODE;.#endif.    
32e91 7d 0a 20 20 7d 77 68 69 6c 65 28 20 79 79 6d 61  }.  }while( yyma
32e92 6a 6f 72 21 3d 59 59 4e 4f 43 4f 44 45 20 26 26  jor!=YYNOCODE &&
32e93 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
32e94 78 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e  x>=0 );.  return
32e95 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ;.}../**********
32e96 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 73  **** End of pars
32e97 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
32e98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e9a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
32e9b 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
32e9c 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a  tokenize.c *****
32e9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e9f 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
32ea0 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
32ea1 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
32ea2 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
32ea3 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
32ea4 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
32ea5 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
32ea6 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
32ea7 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
32ea8 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
32ea9 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
32eaa 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
32eab 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
32eac 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
32ead 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
32eae 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
32eaf 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
32eb0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
32eb1 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
32eb2 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
32eb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32eb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32eb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32eb6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 41  ***********.** A
32eb7 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20  n tokenizer for 
32eb8 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  SQL.**.** This f
32eb9 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ile contains C c
32eba 6f 64 65 20 74 68 61 74 20 73 70 6c 69 74 73 20  ode that splits 
32ebb 61 6e 20 53 51 4c 20 69 6e 70 75 74 20 73 74 72  an SQL input str
32ebc 69 6e 67 20 75 70 20 69 6e 74 6f 0a 2a 2a 20 69  ing up into.** i
32ebd 6e 64 69 76 69 64 75 61 6c 20 74 6f 6b 65 6e 73  ndividual tokens
32ebe 20 61 6e 64 20 73 65 6e 64 73 20 74 68 6f 73 65   and sends those
32ebf 20 74 6f 6b 65 6e 73 20 6f 6e 65 2d 62 79 2d 6f   tokens one-by-o
32ec0 6e 65 20 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a  ne over to the.*
32ec1 2a 20 70 61 72 73 65 72 20 66 6f 72 20 61 6e 61  * parser for ana
32ec2 6c 79 73 69 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  lysis..*/../*.**
32ec3 20 54 68 65 20 63 68 61 72 4d 61 70 28 29 20 6d   The charMap() m
32ec4 61 63 72 6f 20 6d 61 70 73 20 61 6c 70 68 61 62  acro maps alphab
32ec5 65 74 69 63 20 63 68 61 72 61 63 74 65 72 73 20  etic characters 
32ec6 69 6e 74 6f 20 74 68 65 69 72 0a 2a 2a 20 6c 6f  into their.** lo
32ec7 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 65  wer-case ASCII e
32ec8 71 75 69 76 61 6c 65 6e 74 2e 20 20 4f 6e 20 41  quivalent.  On A
32ec9 53 43 49 49 20 6d 61 63 68 69 6e 65 73 2c 20 74  SCII machines, t
32eca 68 69 73 20 69 73 20 6a 75 73 74 0a 2a 2a 20 61  his is just.** a
32ecb 6e 20 75 70 70 65 72 2d 74 6f 2d 6c 6f 77 65 72  n upper-to-lower
32ecc 20 63 61 73 65 20 6d 61 70 2e 20 20 4f 6e 20 45   case map.  On E
32ecd 42 43 44 49 43 20 6d 61 63 68 69 6e 65 73 20 77  BCDIC machines w
32ece 65 20 61 6c 73 6f 20 6e 65 65 64 0a 2a 2a 20 74  e also need.** t
32ecf 6f 20 61 64 6a 75 73 74 20 74 68 65 20 65 6e 63  o adjust the enc
32ed0 6f 64 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 6c 70  oding.  Only alp
32ed1 68 61 62 65 74 69 63 20 63 68 61 72 61 63 74 65  habetic characte
32ed2 72 73 20 61 6e 64 20 75 6e 64 65 72 73 63 6f 72  rs and underscor
32ed3 65 73 0a 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65  es.** need to be
32ed4 20 74 72 61 6e 73 6c 61 74 65 64 2e 0a 2a 2f 0a   translated..*/.
32ed5 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53  #ifdef SQLITE_AS
32ed6 43 49 49 0a 23 20 64 65 66 69 6e 65 20 63 68 61  CII.# define cha
32ed7 72 4d 61 70 28 58 29 20 73 71 6c 69 74 65 33 55  rMap(X) sqlite3U
32ed8 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73  pperToLower[(uns
32ed9 69 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 23 65  igned char)X].#e
32eda 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
32edb 54 45 5f 45 42 43 44 49 43 0a 23 20 64 65 66 69  TE_EBCDIC.# defi
32edc 6e 65 20 63 68 61 72 4d 61 70 28 58 29 20 65 62  ne charMap(X) eb
32edd 63 64 69 63 54 6f 41 73 63 69 69 5b 28 75 6e 73  cdicToAscii[(uns
32ede 69 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 63 6f  igned char)X].co
32edf 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
32ee0 72 20 65 62 63 64 69 63 54 6f 41 73 63 69 69 5b  r ebcdicToAscii[
32ee1 5d 20 3d 20 7b 0a 2f 2a 20 30 20 20 20 31 20 20  ] = {./* 0   1  
32ee2 20 32 20 20 20 33 20 20 20 34 20 20 20 35 20 20   2   3   4   5  
32ee3 20 36 20 20 20 37 20 20 20 38 20 20 20 39 20 20   6   7   8   9  
32ee4 20 41 20 20 20 42 20 20 20 43 20 20 20 44 20 20   A   B   C   D  
32ee5 20 45 20 20 20 46 20 2a 2f 0a 20 20 20 30 2c 20   E   F */.   0, 
32ee6 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ee7 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ee8 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ee9 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32eea 30 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20  0x */.   0,  0, 
32eeb 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32eec 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32eed 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32eee 20 30 2c 20 20 30 2c 20 20 2f 2a 20 31 78 20 2a   0,  0,  /* 1x *
32eef 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  /.   0,  0,  0, 
32ef0 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ef1 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ef2 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ef3 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20   0,  /* 2x */.  
32ef4 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ef5 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ef6 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ef7 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ef8 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 30 2c 20   /* 3x */.   0, 
32ef9 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32efa 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32efb 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32efc 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32efd 34 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20  4x */.   0,  0, 
32efe 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32eff 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f00 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f01 20 30 2c 20 20 30 2c 20 20 2f 2a 20 35 78 20 2a   0,  0,  /* 5x *
32f02 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  /.   0,  0,  0, 
32f03 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f04 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f05 20 30 2c 20 20 30 2c 20 39 35 2c 20 20 30 2c 20   0,  0, 95,  0, 
32f06 20 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20   0,  /* 6x */.  
32f07 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f08 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f09 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f0a 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f0b 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 30 2c 20   /* 7x */.   0, 
32f0c 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31  97, 98, 99,100,1
32f0d 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31  01,102,103,104,1
32f0e 30 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  05,  0,  0,  0, 
32f0f 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32f10 38 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31  8x */.   0,106,1
32f11 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31  07,108,109,110,1
32f12 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20  11,112,113,114, 
32f13 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f14 20 30 2c 20 20 30 2c 20 20 2f 2a 20 39 78 20 2a   0,  0,  /* 9x *
32f15 2f 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31  /.   0,  0,115,1
32f16 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31  16,117,118,119,1
32f17 32 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20  20,121,122,  0, 
32f18 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f19 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20   0,  /* Ax */.  
32f1a 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f1b 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f1c 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f1d 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f1e 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 30 2c 20   /* Bx */.   0, 
32f1f 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31  97, 98, 99,100,1
32f20 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31  01,102,103,104,1
32f21 30 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  05,  0,  0,  0, 
32f22 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32f23 43 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31  Cx */.   0,106,1
32f24 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31  07,108,109,110,1
32f25 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20  11,112,113,114, 
32f26 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f27 20 30 2c 20 20 30 2c 20 20 2f 2a 20 44 78 20 2a   0,  0,  /* Dx *
32f28 2f 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31  /.   0,  0,115,1
32f29 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31  16,117,118,119,1
32f2a 32 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20  20,121,122,  0, 
32f2b 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f2c 20 30 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20   0,  /* Ex */.  
32f2d 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f2e 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f2f 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f30 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32f31 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 65 6e   /* Fx */.};.#en
32f32 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  dif../*.** The s
32f33 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64  qlite3KeywordCod
32f34 65 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 6b 73  e function looks
32f35 20 75 70 20 61 6e 20 69 64 65 6e 74 69 66 69 65   up an identifie
32f36 72 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69  r to determine i
32f37 66 0a 2a 2a 20 69 74 20 69 73 20 61 20 6b 65 79  f.** it is a key
32f38 77 6f 72 64 2e 20 20 49 66 20 69 74 20 69 73 20  word.  If it is 
32f39 61 20 6b 65 79 77 6f 72 64 2c 20 74 68 65 20 74  a keyword, the t
32f3a 6f 6b 65 6e 20 63 6f 64 65 20 6f 66 20 74 68 61  oken code of tha
32f3b 74 20 6b 65 79 77 6f 72 64 20 69 73 20 0a 2a 2a  t keyword is .**
32f3c 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74   returned.  If t
32f3d 68 65 20 69 6e 70 75 74 20 69 73 20 6e 6f 74 20  he input is not 
32f3e 61 20 6b 65 79 77 6f 72 64 2c 20 54 4b 5f 49 44  a keyword, TK_ID
32f3f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
32f40 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e  .** The implemen
32f41 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72  tation of this r
32f42 6f 75 74 69 6e 65 20 77 61 73 20 67 65 6e 65 72  outine was gener
32f43 61 74 65 64 20 62 79 20 61 20 70 72 6f 67 72 61  ated by a progra
32f44 6d 2c 0a 2a 2a 20 6d 6b 6b 65 79 77 6f 72 64 68  m,.** mkkeywordh
32f45 61 73 68 2e 68 2c 20 6c 6f 63 61 74 65 64 20 69  ash.h, located i
32f46 6e 20 74 68 65 20 74 6f 6f 6c 20 73 75 62 64 69  n the tool subdi
32f47 72 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20 64  rectory of the d
32f48 69 73 74 72 69 62 75 74 69 6f 6e 2e 0a 2a 2a 20  istribution..** 
32f49 54 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68  The output of th
32f4a 65 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e  e mkkeywordhash.
32f4b 63 20 70 72 6f 67 72 61 6d 20 69 73 20 77 72 69  c program is wri
32f4c 74 74 65 6e 20 69 6e 74 6f 20 61 20 66 69 6c 65  tten into a file
32f4d 0a 2a 2a 20 6e 61 6d 65 64 20 6b 65 79 77 6f 72  .** named keywor
32f4e 64 68 61 73 68 2e 68 20 61 6e 64 20 74 68 65 6e  dhash.h and then
32f4f 20 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74   included into t
32f50 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20  his source file 
32f51 62 79 0a 2a 2a 20 74 68 65 20 23 69 6e 63 6c 75  by.** the #inclu
32f52 64 65 20 62 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a 2a  de below..*/./**
32f53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
32f54 6c 75 64 65 20 6b 65 79 77 6f 72 64 68 61 73 68  lude keywordhash
32f55 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
32f56 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a   of tokenize.c *
32f57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
32f58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
32f59 69 6e 20 66 69 6c 65 20 6b 65 79 77 6f 72 64 68  in file keywordh
32f5a 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.h **********
32f5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32f5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
32f5d 2a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  *** This file co
32f5e 6e 74 61 69 6e 73 20 61 75 74 6f 6d 61 74 69 63  ntains automatic
32f5f 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63  ally generated c
32f60 6f 64 65 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  ode ******.**.**
32f61 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
32f62 73 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20  s file has been 
32f63 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  automatically ge
32f64 6e 65 72 61 74 65 64 20 62 79 0a 2a 2a 0a 2a 2a  nerated by.**.**
32f65 20 20 20 73 71 6c 69 74 65 2f 74 6f 6f 6c 2f 6d     sqlite/tool/m
32f66 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63 0a 2a  kkeywordhash.c.*
32f67 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
32f68 20 74 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65   this file imple
32f69 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 69 6f 6e  ments a function
32f6a 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73   that determines
32f6b 20 77 68 65 74 68 65 72 0a 2a 2a 20 6f 72 20 6e   whether.** or n
32f6c 6f 74 20 61 20 67 69 76 65 6e 20 69 64 65 6e 74  ot a given ident
32f6d 69 66 69 65 72 20 69 73 20 72 65 61 6c 6c 79 20  ifier is really 
32f6e 61 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64 2e 20  an SQL keyword. 
32f6f 20 54 68 65 20 73 61 6d 65 20 74 68 69 6e 67 0a   The same thing.
32f70 2a 2a 20 6d 69 67 68 74 20 62 65 20 69 6d 70 6c  ** might be impl
32f71 65 6d 65 6e 74 65 64 20 6d 6f 72 65 20 64 69 72  emented more dir
32f72 65 63 74 6c 79 20 75 73 69 6e 67 20 61 20 68 61  ectly using a ha
32f73 6e 64 2d 77 72 69 74 74 65 6e 20 68 61 73 68 20  nd-written hash 
32f74 74 61 62 6c 65 2e 0a 2a 2a 20 42 75 74 20 62 79  table..** But by
32f75 20 75 73 69 6e 67 20 74 68 69 73 20 61 75 74 6f   using this auto
32f76 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61  matically genera
32f77 74 65 64 20 63 6f 64 65 2c 20 74 68 65 20 73 69  ted code, the si
32f78 7a 65 20 6f 66 20 74 68 65 20 63 6f 64 65 0a 2a  ze of the code.*
32f79 2a 20 69 73 20 73 75 62 73 74 61 6e 74 69 61 6c  * is substantial
32f7a 6c 79 20 72 65 64 75 63 65 64 2e 20 20 54 68 69  ly reduced.  Thi
32f7b 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66  s is important f
32f7c 6f 72 20 65 6d 62 65 64 64 65 64 20 61 70 70 6c  or embedded appl
32f7d 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 6f 6e 20 70  ications.** on p
32f7e 6c 61 74 66 6f 72 6d 73 20 77 69 74 68 20 6c 69  latforms with li
32f7f 6d 69 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f  mited memory..*/
32f80 0a 2f 2a 20 48 61 73 68 20 73 63 6f 72 65 3a 20  ./* Hash score: 
32f81 31 37 35 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  175 */.static in
32f82 74 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f  t keywordCode(co
32f83 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
32f84 20 6e 29 7b 0a 20 20 2f 2a 20 7a 54 65 78 74 5b   n){.  /* zText[
32f85 5d 20 65 6e 63 6f 64 65 73 20 38 31 31 20 62 79  ] encodes 811 by
32f86 74 65 73 20 6f 66 20 6b 65 79 77 6f 72 64 73 20  tes of keywords 
32f87 69 6e 20 35 34 31 20 62 79 74 65 73 20 2a 2f 0a  in 541 bytes */.
32f88 20 20 2f 2a 20 20 20 52 45 49 4e 44 45 58 45 44    /*   REINDEXED
32f89 45 53 43 41 50 45 41 43 48 45 43 4b 45 59 42 45  ESCAPEACHECKEYBE
32f8a 46 4f 52 45 49 47 4e 4f 52 45 47 45 58 50 4c 41  FOREIGNOREGEXPLA
32f8b 49 4e 53 54 45 41 44 44 41 54 41 42 41 53 45 4c  INSTEADDATABASEL
32f8c 45 43 54 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f  ECT       */.  /
32f8d 2a 20 20 20 41 42 4c 45 46 54 48 45 4e 44 45 46  *   ABLEFTHENDEF
32f8e 45 52 52 41 42 4c 45 4c 53 45 58 43 45 50 54 52  ERRABLELSEXCEPTR
32f8f 41 4e 53 41 43 54 49 4f 4e 41 54 55 52 41 4c 54  ANSACTIONATURALT
32f90 45 52 41 49 53 45 58 43 4c 55 53 49 56 45 20 20  ERAISEXCLUSIVE  
32f91 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20         */.  /*  
32f92 20 58 49 53 54 53 41 56 45 50 4f 49 4e 54 45 52   XISTSAVEPOINTER
32f93 53 45 43 54 52 49 47 47 45 52 45 46 45 52 45 4e  SECTRIGGEREFEREN
32f94 43 45 53 43 4f 4e 53 54 52 41 49 4e 54 4f 46 46  CESCONSTRAINTOFF
32f95 53 45 54 45 4d 50 4f 52 41 52 59 20 20 20 20 20  SETEMPORARY     
32f96 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 55 4e      */.  /*   UN
32f97 49 51 55 45 52 59 41 54 54 41 43 48 41 56 49 4e  IQUERYATTACHAVIN
32f98 47 52 4f 55 50 44 41 54 45 42 45 47 49 4e 4e 45  GROUPDATEBEGINNE
32f99 52 45 4c 45 41 53 45 42 45 54 57 45 45 4e 4f 54  RELEASEBETWEENOT
32f9a 4e 55 4c 4c 49 4b 45 20 20 20 20 20 20 20 20 20  NULLIKE         
32f9b 20 2a 2f 0a 20 20 2f 2a 20 20 20 43 41 53 43 41   */.  /*   CASCA
32f9c 44 45 4c 45 54 45 43 41 53 45 43 4f 4c 4c 41 54  DELETECASECOLLAT
32f9d 45 43 52 45 41 54 45 43 55 52 52 45 4e 54 5f 44  ECREATECURRENT_D
32f9e 41 54 45 44 45 54 41 43 48 49 4d 4d 45 44 49 41  ATEDETACHIMMEDIA
32f9f 54 45 4a 4f 49 4e 20 20 20 20 20 20 20 20 2a 2f  TEJOIN        */
32fa0 0a 20 20 2f 2a 20 20 20 53 45 52 54 4d 41 54 43  .  /*   SERTMATC
32fa1 48 50 4c 41 4e 41 4c 59 5a 45 50 52 41 47 4d 41  HPLANALYZEPRAGMA
32fa2 42 4f 52 54 56 41 4c 55 45 53 56 49 52 54 55 41  BORTVALUESVIRTUA
32fa3 4c 49 4d 49 54 57 48 45 4e 57 48 45 52 45 4e 41  LIMITWHENWHERENA
32fa4 4d 45 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  ME         */.  
32fa5 2f 2a 20 20 20 41 46 54 45 52 45 50 4c 41 43 45  /*   AFTEREPLACE
32fa6 41 4e 44 45 46 41 55 4c 54 41 55 54 4f 49 4e 43  ANDEFAULTAUTOINC
32fa7 52 45 4d 45 4e 54 43 41 53 54 43 4f 4c 55 4d 4e  REMENTCASTCOLUMN
32fa8 43 4f 4d 4d 49 54 43 4f 4e 46 4c 49 43 54 43 52  COMMITCONFLICTCR
32fa9 4f 53 53 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20  OSS     */.  /* 
32faa 20 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 53 54    CURRENT_TIMEST
32fab 41 4d 50 52 49 4d 41 52 59 44 45 46 45 52 52 45  AMPRIMARYDEFERRE
32fac 44 49 53 54 49 4e 43 54 44 52 4f 50 46 41 49 4c  DISTINCTDROPFAIL
32fad 46 52 4f 4d 46 55 4c 4c 47 4c 4f 42 59 49 46 20  FROMFULLGLOBYIF 
32fae 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 49       */.  /*   I
32faf 53 4e 55 4c 4c 4f 52 44 45 52 45 53 54 52 49 43  SNULLORDERESTRIC
32fb0 54 4f 55 54 45 52 49 47 48 54 52 4f 4c 4c 42 41  TOUTERIGHTROLLBA
32fb1 43 4b 52 4f 57 55 4e 49 4f 4e 55 53 49 4e 47 56  CKROWUNIONUSINGV
32fb2 41 43 55 55 4d 56 49 45 57 20 20 20 20 20 20 20  ACUUMVIEW       
32fb3 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 49 4e 49 54    */.  /*   INIT
32fb4 49 41 4c 4c 59 20 20 20 20 20 20 20 20 20 20 20  IALLY           
32fb5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32fb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32fb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
32fb8 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  /.  static const
32fb9 20 63 68 61 72 20 7a 54 65 78 74 5b 35 34 30 5d   char zText[540]
32fba 20 3d 20 7b 0a 20 20 20 20 27 52 27 2c 27 45 27   = {.    'R','E'
32fbb 2c 27 49 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27  ,'I','N','D','E'
32fbc 2c 27 58 27 2c 27 45 27 2c 27 44 27 2c 27 45 27  ,'X','E','D','E'
32fbd 2c 27 53 27 2c 27 43 27 2c 27 41 27 2c 27 50 27  ,'S','C','A','P'
32fbe 2c 27 45 27 2c 27 41 27 2c 27 43 27 2c 27 48 27  ,'E','A','C','H'
32fbf 2c 0a 20 20 20 20 27 45 27 2c 27 43 27 2c 27 4b  ,.    'E','C','K
32fc0 27 2c 27 45 27 2c 27 59 27 2c 27 42 27 2c 27 45  ','E','Y','B','E
32fc1 27 2c 27 46 27 2c 27 4f 27 2c 27 52 27 2c 27 45  ','F','O','R','E
32fc2 27 2c 27 49 27 2c 27 47 27 2c 27 4e 27 2c 27 4f  ','I','G','N','O
32fc3 27 2c 27 52 27 2c 27 45 27 2c 27 47 27 2c 0a 20  ','R','E','G',. 
32fc4 20 20 20 27 45 27 2c 27 58 27 2c 27 50 27 2c 27     'E','X','P','
32fc5 4c 27 2c 27 41 27 2c 27 49 27 2c 27 4e 27 2c 27  L','A','I','N','
32fc6 53 27 2c 27 54 27 2c 27 45 27 2c 27 41 27 2c 27  S','T','E','A','
32fc7 44 27 2c 27 44 27 2c 27 41 27 2c 27 54 27 2c 27  D','D','A','T','
32fc8 41 27 2c 27 42 27 2c 27 41 27 2c 0a 20 20 20 20  A','B','A',.    
32fc9 27 53 27 2c 27 45 27 2c 27 4c 27 2c 27 45 27 2c  'S','E','L','E',
32fca 27 43 27 2c 27 54 27 2c 27 41 27 2c 27 42 27 2c  'C','T','A','B',
32fcb 27 4c 27 2c 27 45 27 2c 27 46 27 2c 27 54 27 2c  'L','E','F','T',
32fcc 27 48 27 2c 27 45 27 2c 27 4e 27 2c 27 44 27 2c  'H','E','N','D',
32fcd 27 45 27 2c 27 46 27 2c 0a 20 20 20 20 27 45 27  'E','F',.    'E'
32fce 2c 27 52 27 2c 27 52 27 2c 27 41 27 2c 27 42 27  ,'R','R','A','B'
32fcf 2c 27 4c 27 2c 27 45 27 2c 27 4c 27 2c 27 53 27  ,'L','E','L','S'
32fd0 2c 27 45 27 2c 27 58 27 2c 27 43 27 2c 27 45 27  ,'E','X','C','E'
32fd1 2c 27 50 27 2c 27 54 27 2c 27 52 27 2c 27 41 27  ,'P','T','R','A'
32fd2 2c 27 4e 27 2c 0a 20 20 20 20 27 53 27 2c 27 41  ,'N',.    'S','A
32fd3 27 2c 27 43 27 2c 27 54 27 2c 27 49 27 2c 27 4f  ','C','T','I','O
32fd4 27 2c 27 4e 27 2c 27 41 27 2c 27 54 27 2c 27 55  ','N','A','T','U
32fd5 27 2c 27 52 27 2c 27 41 27 2c 27 4c 27 2c 27 54  ','R','A','L','T
32fd6 27 2c 27 45 27 2c 27 52 27 2c 27 41 27 2c 27 49  ','E','R','A','I
32fd7 27 2c 0a 20 20 20 20 27 53 27 2c 27 45 27 2c 27  ',.    'S','E','
32fd8 58 27 2c 27 43 27 2c 27 4c 27 2c 27 55 27 2c 27  X','C','L','U','
32fd9 53 27 2c 27 49 27 2c 27 56 27 2c 27 45 27 2c 27  S','I','V','E','
32fda 58 27 2c 27 49 27 2c 27 53 27 2c 27 54 27 2c 27  X','I','S','T','
32fdb 53 27 2c 27 41 27 2c 27 56 27 2c 27 45 27 2c 0a  S','A','V','E',.
32fdc 20 20 20 20 27 50 27 2c 27 4f 27 2c 27 49 27 2c      'P','O','I',
32fdd 27 4e 27 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c  'N','T','E','R',
32fde 27 53 27 2c 27 45 27 2c 27 43 27 2c 27 54 27 2c  'S','E','C','T',
32fdf 27 52 27 2c 27 49 27 2c 27 47 27 2c 27 47 27 2c  'R','I','G','G',
32fe0 27 45 27 2c 27 52 27 2c 27 45 27 2c 0a 20 20 20  'E','R','E',.   
32fe1 20 27 46 27 2c 27 45 27 2c 27 52 27 2c 27 45 27   'F','E','R','E'
32fe2 2c 27 4e 27 2c 27 43 27 2c 27 45 27 2c 27 53 27  ,'N','C','E','S'
32fe3 2c 27 43 27 2c 27 4f 27 2c 27 4e 27 2c 27 53 27  ,'C','O','N','S'
32fe4 2c 27 54 27 2c 27 52 27 2c 27 41 27 2c 27 49 27  ,'T','R','A','I'
32fe5 2c 27 4e 27 2c 27 54 27 2c 0a 20 20 20 20 27 4f  ,'N','T',.    'O
32fe6 27 2c 27 46 27 2c 27 46 27 2c 27 53 27 2c 27 45  ','F','F','S','E
32fe7 27 2c 27 54 27 2c 27 45 27 2c 27 4d 27 2c 27 50  ','T','E','M','P
32fe8 27 2c 27 4f 27 2c 27 52 27 2c 27 41 27 2c 27 52  ','O','R','A','R
32fe9 27 2c 27 59 27 2c 27 55 27 2c 27 4e 27 2c 27 49  ','Y','U','N','I
32fea 27 2c 27 51 27 2c 0a 20 20 20 20 27 55 27 2c 27  ','Q',.    'U','
32feb 45 27 2c 27 52 27 2c 27 59 27 2c 27 41 27 2c 27  E','R','Y','A','
32fec 54 27 2c 27 54 27 2c 27 41 27 2c 27 43 27 2c 27  T','T','A','C','
32fed 48 27 2c 27 41 27 2c 27 56 27 2c 27 49 27 2c 27  H','A','V','I','
32fee 4e 27 2c 27 47 27 2c 27 52 27 2c 27 4f 27 2c 27  N','G','R','O','
32fef 55 27 2c 0a 20 20 20 20 27 50 27 2c 27 44 27 2c  U',.    'P','D',
32ff0 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 42 27 2c  'A','T','E','B',
32ff1 27 45 27 2c 27 47 27 2c 27 49 27 2c 27 4e 27 2c  'E','G','I','N',
32ff2 27 4e 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c  'N','E','R','E',
32ff3 27 4c 27 2c 27 45 27 2c 27 41 27 2c 27 53 27 2c  'L','E','A','S',
32ff4 0a 20 20 20 20 27 45 27 2c 27 42 27 2c 27 45 27  .    'E','B','E'
32ff5 2c 27 54 27 2c 27 57 27 2c 27 45 27 2c 27 45 27  ,'T','W','E','E'
32ff6 2c 27 4e 27 2c 27 4f 27 2c 27 54 27 2c 27 4e 27  ,'N','O','T','N'
32ff7 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c 27 49 27  ,'U','L','L','I'
32ff8 2c 27 4b 27 2c 27 45 27 2c 27 43 27 2c 0a 20 20  ,'K','E','C',.  
32ff9 20 20 27 41 27 2c 27 53 27 2c 27 43 27 2c 27 41    'A','S','C','A
32ffa 27 2c 27 44 27 2c 27 45 27 2c 27 4c 27 2c 27 45  ','D','E','L','E
32ffb 27 2c 27 54 27 2c 27 45 27 2c 27 43 27 2c 27 41  ','T','E','C','A
32ffc 27 2c 27 53 27 2c 27 45 27 2c 27 43 27 2c 27 4f  ','S','E','C','O
32ffd 27 2c 27 4c 27 2c 27 4c 27 2c 0a 20 20 20 20 27  ','L','L',.    '
32ffe 41 27 2c 27 54 27 2c 27 45 27 2c 27 43 27 2c 27  A','T','E','C','
32fff 52 27 2c 27 45 27 2c 27 41 27 2c 27 54 27 2c 27  R','E','A','T','
33000 45 27 2c 27 43 27 2c 27 55 27 2c 27 52 27 2c 27  E','C','U','R','
33001 52 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27 2c 27  R','E','N','T','
33002 5f 27 2c 27 44 27 2c 0a 20 20 20 20 27 41 27 2c  _','D',.    'A',
33003 27 54 27 2c 27 45 27 2c 27 44 27 2c 27 45 27 2c  'T','E','D','E',
33004 27 54 27 2c 27 41 27 2c 27 43 27 2c 27 48 27 2c  'T','A','C','H',
33005 27 49 27 2c 27 4d 27 2c 27 4d 27 2c 27 45 27 2c  'I','M','M','E',
33006 27 44 27 2c 27 49 27 2c 27 41 27 2c 27 54 27 2c  'D','I','A','T',
33007 27 45 27 2c 0a 20 20 20 20 27 4a 27 2c 27 4f 27  'E',.    'J','O'
33008 2c 27 49 27 2c 27 4e 27 2c 27 53 27 2c 27 45 27  ,'I','N','S','E'
33009 2c 27 52 27 2c 27 54 27 2c 27 4d 27 2c 27 41 27  ,'R','T','M','A'
3300a 2c 27 54 27 2c 27 43 27 2c 27 48 27 2c 27 50 27  ,'T','C','H','P'
3300b 2c 27 4c 27 2c 27 41 27 2c 27 4e 27 2c 27 41 27  ,'L','A','N','A'
3300c 2c 0a 20 20 20 20 27 4c 27 2c 27 59 27 2c 27 5a  ,.    'L','Y','Z
3300d 27 2c 27 45 27 2c 27 50 27 2c 27 52 27 2c 27 41  ','E','P','R','A
3300e 27 2c 27 47 27 2c 27 4d 27 2c 27 41 27 2c 27 42  ','G','M','A','B
3300f 27 2c 27 4f 27 2c 27 52 27 2c 27 54 27 2c 27 56  ','O','R','T','V
33010 27 2c 27 41 27 2c 27 4c 27 2c 27 55 27 2c 0a 20  ','A','L','U',. 
33011 20 20 20 27 45 27 2c 27 53 27 2c 27 56 27 2c 27     'E','S','V','
33012 49 27 2c 27 52 27 2c 27 54 27 2c 27 55 27 2c 27  I','R','T','U','
33013 41 27 2c 27 4c 27 2c 27 49 27 2c 27 4d 27 2c 27  A','L','I','M','
33014 49 27 2c 27 54 27 2c 27 57 27 2c 27 48 27 2c 27  I','T','W','H','
33015 45 27 2c 27 4e 27 2c 27 57 27 2c 0a 20 20 20 20  E','N','W',.    
33016 27 48 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c  'H','E','R','E',
33017 27 4e 27 2c 27 41 27 2c 27 4d 27 2c 27 45 27 2c  'N','A','M','E',
33018 27 41 27 2c 27 46 27 2c 27 54 27 2c 27 45 27 2c  'A','F','T','E',
33019 27 52 27 2c 27 45 27 2c 27 50 27 2c 27 4c 27 2c  'R','E','P','L',
3301a 27 41 27 2c 27 43 27 2c 0a 20 20 20 20 27 45 27  'A','C',.    'E'
3301b 2c 27 41 27 2c 27 4e 27 2c 27 44 27 2c 27 45 27  ,'A','N','D','E'
3301c 2c 27 46 27 2c 27 41 27 2c 27 55 27 2c 27 4c 27  ,'F','A','U','L'
3301d 2c 27 54 27 2c 27 41 27 2c 27 55 27 2c 27 54 27  ,'T','A','U','T'
3301e 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c 27 43 27  ,'O','I','N','C'
3301f 2c 27 52 27 2c 0a 20 20 20 20 27 45 27 2c 27 4d  ,'R',.    'E','M
33020 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27 2c 27 43  ','E','N','T','C
33021 27 2c 27 41 27 2c 27 53 27 2c 27 54 27 2c 27 43  ','A','S','T','C
33022 27 2c 27 4f 27 2c 27 4c 27 2c 27 55 27 2c 27 4d  ','O','L','U','M
33023 27 2c 27 4e 27 2c 27 43 27 2c 27 4f 27 2c 27 4d  ','N','C','O','M
33024 27 2c 0a 20 20 20 20 27 4d 27 2c 27 49 27 2c 27  ',.    'M','I','
33025 54 27 2c 27 43 27 2c 27 4f 27 2c 27 4e 27 2c 27  T','C','O','N','
33026 46 27 2c 27 4c 27 2c 27 49 27 2c 27 43 27 2c 27  F','L','I','C','
33027 54 27 2c 27 43 27 2c 27 52 27 2c 27 4f 27 2c 27  T','C','R','O','
33028 53 27 2c 27 53 27 2c 27 43 27 2c 27 55 27 2c 0a  S','S','C','U',.
33029 20 20 20 20 27 52 27 2c 27 52 27 2c 27 45 27 2c      'R','R','E',
3302a 27 4e 27 2c 27 54 27 2c 27 5f 27 2c 27 54 27 2c  'N','T','_','T',
3302b 27 49 27 2c 27 4d 27 2c 27 45 27 2c 27 53 27 2c  'I','M','E','S',
3302c 27 54 27 2c 27 41 27 2c 27 4d 27 2c 27 50 27 2c  'T','A','M','P',
3302d 27 52 27 2c 27 49 27 2c 27 4d 27 2c 0a 20 20 20  'R','I','M',.   
3302e 20 27 41 27 2c 27 52 27 2c 27 59 27 2c 27 44 27   'A','R','Y','D'
3302f 2c 27 45 27 2c 27 46 27 2c 27 45 27 2c 27 52 27  ,'E','F','E','R'
33030 2c 27 52 27 2c 27 45 27 2c 27 44 27 2c 27 49 27  ,'R','E','D','I'
33031 2c 27 53 27 2c 27 54 27 2c 27 49 27 2c 27 4e 27  ,'S','T','I','N'
33032 2c 27 43 27 2c 27 54 27 2c 0a 20 20 20 20 27 44  ,'C','T',.    'D
33033 27 2c 27 52 27 2c 27 4f 27 2c 27 50 27 2c 27 46  ','R','O','P','F
33034 27 2c 27 41 27 2c 27 49 27 2c 27 4c 27 2c 27 46  ','A','I','L','F
33035 27 2c 27 52 27 2c 27 4f 27 2c 27 4d 27 2c 27 46  ','R','O','M','F
33036 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c 27 47  ','U','L','L','G
33037 27 2c 27 4c 27 2c 0a 20 20 20 20 27 4f 27 2c 27  ','L',.    'O','
33038 42 27 2c 27 59 27 2c 27 49 27 2c 27 46 27 2c 27  B','Y','I','F','
33039 49 27 2c 27 53 27 2c 27 4e 27 2c 27 55 27 2c 27  I','S','N','U','
3303a 4c 27 2c 27 4c 27 2c 27 4f 27 2c 27 52 27 2c 27  L','L','O','R','
3303b 44 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 27  D','E','R','E','
3303c 53 27 2c 0a 20 20 20 20 27 54 27 2c 27 52 27 2c  S',.    'T','R',
3303d 27 49 27 2c 27 43 27 2c 27 54 27 2c 27 4f 27 2c  'I','C','T','O',
3303e 27 55 27 2c 27 54 27 2c 27 45 27 2c 27 52 27 2c  'U','T','E','R',
3303f 27 49 27 2c 27 47 27 2c 27 48 27 2c 27 54 27 2c  'I','G','H','T',
33040 27 52 27 2c 27 4f 27 2c 27 4c 27 2c 27 4c 27 2c  'R','O','L','L',
33041 0a 20 20 20 20 27 42 27 2c 27 41 27 2c 27 43 27  .    'B','A','C'
33042 2c 27 4b 27 2c 27 52 27 2c 27 4f 27 2c 27 57 27  ,'K','R','O','W'
33043 2c 27 55 27 2c 27 4e 27 2c 27 49 27 2c 27 4f 27  ,'U','N','I','O'
33044 2c 27 4e 27 2c 27 55 27 2c 27 53 27 2c 27 49 27  ,'N','U','S','I'
33045 2c 27 4e 27 2c 27 47 27 2c 27 56 27 2c 0a 20 20  ,'N','G','V',.  
33046 20 20 27 41 27 2c 27 43 27 2c 27 55 27 2c 27 55    'A','C','U','U
33047 27 2c 27 4d 27 2c 27 56 27 2c 27 49 27 2c 27 45  ','M','V','I','E
33048 27 2c 27 57 27 2c 27 49 27 2c 27 4e 27 2c 27 49  ','W','I','N','I
33049 27 2c 27 54 27 2c 27 49 27 2c 27 41 27 2c 27 4c  ','T','I','A','L
3304a 27 2c 27 4c 27 2c 27 59 27 2c 0a 20 20 7d 3b 0a  ','L','Y',.  };.
3304b 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
3304c 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 48 61  nsigned char aHa
3304d 73 68 5b 31 32 37 5d 20 3d 20 7b 0a 20 20 20 20  sh[127] = {.    
3304e 20 20 37 32 2c 20 31 30 31 2c 20 31 31 34 2c 20    72, 101, 114, 
3304f 20 37 30 2c 20 20 20 30 2c 20 20 34 35 2c 20 20   70,   0,  45,  
33050 20 30 2c 20 20 20 30 2c 20 20 37 38 2c 20 20 20   0,   0,  78,   
33051 30 2c 20 20 37 33 2c 20 20 20 30 2c 20 20 20 30  0,  73,   0,   0
33052 2c 0a 20 20 20 20 20 20 34 32 2c 20 20 31 32 2c  ,.      42,  12,
33053 20 20 37 34 2c 20 20 31 35 2c 20 20 20 30 2c 20    74,  15,   0, 
33054 31 31 33 2c 20 20 38 31 2c 20 20 35 30 2c 20 31  113,  81,  50, 1
33055 30 38 2c 20 20 20 30 2c 20 20 31 39 2c 20 20 20  08,   0,  19,   
33056 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 31 31 38  0,   0,.     118
33057 2c 20 20 20 30 2c 20 31 31 36 2c 20 31 31 31 2c  ,   0, 116, 111,
33058 20 20 20 30 2c 20 20 32 32 2c 20 20 38 39 2c 20     0,  22,  89, 
33059 20 20 30 2c 20 20 20 39 2c 20 20 20 30 2c 20 20    0,   9,   0,  
3305a 20 30 2c 20 20 36 36 2c 20 20 36 37 2c 0a 20 20   0,  66,  67,.  
3305b 20 20 20 20 20 30 2c 20 20 36 35 2c 20 20 20 36       0,  65,   6
3305c 2c 20 20 20 30 2c 20 20 34 38 2c 20 20 38 36 2c  ,   0,  48,  86,
3305d 20 20 39 38 2c 20 20 20 30 2c 20 31 31 35 2c 20    98,   0, 115, 
3305e 20 39 37 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   97,   0,   0,  
3305f 34 34 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 39  44,.       0,  9
33060 39 2c 20 20 32 34 2c 20 20 20 30 2c 20 20 31 37  9,  24,   0,  17
33061 2c 20 20 20 30 2c 20 31 31 39 2c 20 20 34 39 2c  ,   0, 119,  49,
33062 20 20 32 33 2c 20 20 20 30 2c 20 20 20 35 2c 20    23,   0,   5, 
33063 31 30 36 2c 20 20 32 35 2c 0a 20 20 20 20 20 20  106,  25,.      
33064 39 32 2c 20 20 20 30 2c 20 20 20 30 2c 20 31 32  92,   0,   0, 12
33065 31 2c 20 31 30 32 2c 20 20 35 36 2c 20 31 32 30  1, 102,  56, 120
33066 2c 20 20 35 33 2c 20 20 32 38 2c 20 20 35 31 2c  ,  53,  28,  51,
33067 20 20 20 30 2c 20 20 38 37 2c 20 20 20 30 2c 0a     0,  87,   0,.
33068 20 20 20 20 20 20 39 36 2c 20 20 32 36 2c 20 20        96,  26,  
33069 20 30 2c 20 20 39 35 2c 20 20 20 30 2c 20 20 20   0,  95,   0,   
3306a 30 2c 20 20 20 30 2c 20 20 39 31 2c 20 20 38 38  0,   0,  91,  88
3306b 2c 20 20 39 33 2c 20 20 38 34 2c 20 31 30 35 2c  ,  93,  84, 105,
3306c 20 20 31 34 2c 0a 20 20 20 20 20 20 33 39 2c 20    14,.      39, 
3306d 31 30 34 2c 20 20 20 30 2c 20 20 37 37 2c 20 20  104,   0,  77,  
3306e 20 30 2c 20 20 31 38 2c 20 20 38 35 2c 20 31 30   0,  18,  85, 10
3306f 37 2c 20 20 33 32 2c 20 20 20 30 2c 20 31 31 37  7,  32,   0, 117
33070 2c 20 20 37 36 2c 20 31 30 39 2c 0a 20 20 20 20  ,  76, 109,.    
33071 20 20 35 38 2c 20 20 34 36 2c 20 20 38 30 2c 20    58,  46,  80, 
33072 20 20 30 2c 20 20 20 30 2c 20 20 39 30 2c 20 20    0,   0,  90,  
33073 34 30 2c 20 20 20 30 2c 20 31 31 32 2c 20 20 20  40,   0, 112,   
33074 30 2c 20 20 33 36 2c 20 20 20 30 2c 20 20 20 30  0,  36,   0,   0
33075 2c 0a 20 20 20 20 20 20 32 39 2c 20 20 20 30 2c  ,.      29,   0,
33076 20 20 38 32 2c 20 20 35 39 2c 20 20 36 30 2c 20    82,  59,  60, 
33077 20 20 30 2c 20 20 32 30 2c 20 20 35 37 2c 20 20    0,  20,  57,  
33078 20 30 2c 20 20 35 32 2c 0a 20 20 7d 3b 0a 20 20   0,  52,.  };.  
33079 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
3307a 69 67 6e 65 64 20 63 68 61 72 20 61 4e 65 78 74  igned char aNext
3307b 5b 31 32 31 5d 20 3d 20 7b 0a 20 20 20 20 20 20  [121] = {.      
3307c 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
3307d 30 2c 20 20 20 34 2c 20 20 20 30 2c 20 20 20 30  0,   4,   0,   0
3307e 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
3307f 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a     0,   0,   0,.
33080 20 20 20 20 20 20 20 30 2c 20 20 20 32 2c 20 20         0,   2,  
33081 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
33082 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 31 33  0,   0,   0,  13
33083 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
33084 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20     0,.       0, 
33085 20 20 37 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    7,   0,   0,  
33086 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
33087 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
33088 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20  ,   0,   0,.    
33089 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
3308a 20 20 30 2c 20 20 33 33 2c 20 20 20 30 2c 20 20    0,  33,   0,  
3308b 32 31 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20  21,   0,   0,   
3308c 30 2c 20 20 34 33 2c 20 20 20 33 2c 20 20 34 37  0,  43,   3,  47
3308d 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 30 2c  ,.       0,   0,
3308e 20 20 20 30 2c 20 20 20 30 2c 20 20 33 30 2c 20     0,   0,  30, 
3308f 20 20 30 2c 20 20 35 34 2c 20 20 20 30 2c 20 20    0,  54,   0,  
33090 33 38 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20  38,   0,   0,   
33091 30 2c 20 20 20 31 2c 0a 20 20 20 20 20 20 36 32  0,   1,.      62
33092 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 36 33 2c  ,   0,   0,  63,
33093 20 20 20 30 2c 20 20 34 31 2c 20 20 20 30 2c 20     0,  41,   0, 
33094 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
33095 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20   0,   0,   0,.  
33096 20 20 20 20 36 31 2c 20 20 20 30 2c 20 20 20 30      61,   0,   0
33097 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33 31 2c  ,   0,   0,  31,
33098 20 20 35 35 2c 20 20 31 36 2c 20 20 33 34 2c 20    55,  16,  34, 
33099 20 31 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   10,   0,   0,  
3309a 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20   0,.       0,   
3309b 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 31 31  0,   0,   0,  11
3309c 2c 20 20 36 38 2c 20 20 37 35 2c 20 20 20 30 2c  ,  68,  75,   0,
3309d 20 20 20 38 2c 20 20 20 30 2c 20 31 30 30 2c 20     8,   0, 100, 
3309e 20 39 34 2c 20 20 20 30 2c 0a 20 20 20 20 20 31   94,   0,.     1
3309f 30 33 2c 20 20 20 30 2c 20 20 38 33 2c 20 20 20  03,   0,  83,   
330a0 30 2c 20 20 37 31 2c 20 20 20 30 2c 20 20 20 30  0,  71,   0,   0
330a1 2c 20 31 31 30 2c 20 20 32 37 2c 20 20 33 37 2c  , 110,  27,  37,
330a2 20 20 36 39 2c 20 20 37 39 2c 20 20 20 30 2c 0a    69,  79,   0,.
330a3 20 20 20 20 20 20 33 35 2c 20 20 36 34 2c 20 20        35,  64,  
330a4 20 30 2c 20 20 20 30 2c 0a 20 20 7d 3b 0a 20 20   0,   0,.  };.  
330a5 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
330a6 69 67 6e 65 64 20 63 68 61 72 20 61 4c 65 6e 5b  igned char aLen[
330a7 31 32 31 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  121] = {.       
330a8 37 2c 20 20 20 37 2c 20 20 20 35 2c 20 20 20 34  7,   7,   5,   4
330a9 2c 20 20 20 36 2c 20 20 20 34 2c 20 20 20 35 2c  ,   6,   4,   5,
330aa 20 20 20 33 2c 20 20 20 36 2c 20 20 20 37 2c 20     3,   6,   7, 
330ab 20 20 33 2c 20 20 20 36 2c 20 20 20 36 2c 0a 20    3,   6,   6,. 
330ac 20 20 20 20 20 20 37 2c 20 20 20 37 2c 20 20 20        7,   7,   
330ad 33 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20 36  3,   8,   2,   6
330ae 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20 34 2c  ,   5,   4,   4,
330af 20 20 20 33 2c 20 20 31 30 2c 20 20 20 34 2c 20     3,  10,   4, 
330b0 20 20 36 2c 0a 20 20 20 20 20 20 31 31 2c 20 20    6,.      11,  
330b1 20 36 2c 20 20 20 32 2c 20 20 20 37 2c 20 20 20   6,   2,   7,   
330b2 35 2c 20 20 20 35 2c 20 20 20 39 2c 20 20 20 36  5,   5,   9,   6
330b3 2c 20 20 20 39 2c 20 20 20 39 2c 20 20 20 37 2c  ,   9,   9,   7,
330b4 20 20 31 30 2c 20 20 31 30 2c 0a 20 20 20 20 20    10,  10,.     
330b5 20 20 34 2c 20 20 20 36 2c 20 20 20 32 2c 20 20    4,   6,   2,  
330b6 20 33 2c 20 20 20 39 2c 20 20 20 34 2c 20 20 20   3,   9,   4,   
330b7 32 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36  2,   6,   5,   6
330b8 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c  ,   6,   5,   6,
330b9 0a 20 20 20 20 20 20 20 35 2c 20 20 20 35 2c 20  .       5,   5, 
330ba 20 20 37 2c 20 20 20 37 2c 20 20 20 37 2c 20 20    7,   7,   7,  
330bb 20 33 2c 20 20 20 32 2c 20 20 20 34 2c 20 20 20   3,   2,   4,   
330bc 34 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 36  4,   7,   3,   6
330bd 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20 37 2c  ,   4,.       7,
330be 20 20 20 36 2c 20 20 31 32 2c 20 20 20 36 2c 20     6,  12,   6, 
330bf 20 20 39 2c 20 20 20 34 2c 20 20 20 36 2c 20 20    9,   4,   6,  
330c0 20 35 2c 20 20 20 34 2c 20 20 20 37 2c 20 20 20   5,   4,   7,   
330c1 36 2c 20 20 20 35 2c 20 20 20 36 2c 0a 20 20 20  6,   5,   6,.   
330c2 20 20 20 20 37 2c 20 20 20 35 2c 20 20 20 34 2c      7,   5,   4,
330c3 20 20 20 35 2c 20 20 20 36 2c 20 20 20 35 2c 20     5,   6,   5, 
330c4 20 20 37 2c 20 20 20 33 2c 20 20 20 37 2c 20 20    7,   3,   7,  
330c5 31 33 2c 20 20 20 32 2c 20 20 20 32 2c 20 20 20  13,   2,   2,   
330c6 34 2c 0a 20 20 20 20 20 20 20 36 2c 20 20 20 36  4,.       6,   6
330c7 2c 20 20 20 38 2c 20 20 20 35 2c 20 20 31 37 2c  ,   8,   5,  17,
330c8 20 20 31 32 2c 20 20 20 37 2c 20 20 20 38 2c 20    12,   7,   8, 
330c9 20 20 38 2c 20 20 20 32 2c 20 20 20 34 2c 20 20    8,   2,   4,  
330ca 20 34 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20   4,   4,.       
330cb 34 2c 20 20 20 34 2c 20 20 20 32 2c 20 20 20 32  4,   4,   2,   2
330cc 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 38 2c  ,   6,   5,   8,
330cd 20 20 20 35 2c 20 20 20 35 2c 20 20 20 38 2c 20     5,   5,   8, 
330ce 20 20 33 2c 20 20 20 35 2c 20 20 20 35 2c 0a 20    3,   5,   5,. 
330cf 20 20 20 20 20 20 36 2c 20 20 20 34 2c 20 20 20        6,   4,   
330d0 39 2c 20 20 20 33 2c 0a 20 20 7d 3b 0a 20 20 73  9,   3,.  };.  s
330d1 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
330d2 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 20 61  gned short int a
330d3 4f 66 66 73 65 74 5b 31 32 31 5d 20 3d 20 7b 0a  Offset[121] = {.
330d4 20 20 20 20 20 20 20 30 2c 20 20 20 32 2c 20 20         0,   2,  
330d5 20 32 2c 20 20 20 38 2c 20 20 20 39 2c 20 20 31   2,   8,   9,  1
330d6 34 2c 20 20 31 36 2c 20 20 32 30 2c 20 20 32 33  4,  16,  20,  23
330d7 2c 20 20 32 35 2c 20 20 32 35 2c 20 20 32 39 2c  ,  25,  25,  29,
330d8 20 20 33 33 2c 0a 20 20 20 20 20 20 33 36 2c 20    33,.      36, 
330d9 20 34 31 2c 20 20 34 36 2c 20 20 34 38 2c 20 20   41,  46,  48,  
330da 35 33 2c 20 20 35 34 2c 20 20 35 39 2c 20 20 36  53,  54,  59,  6
330db 32 2c 20 20 36 35 2c 20 20 36 37 2c 20 20 36 39  2,  65,  67,  69
330dc 2c 20 20 37 38 2c 20 20 38 31 2c 0a 20 20 20 20  ,  78,  81,.    
330dd 20 20 38 36 2c 20 20 39 31 2c 20 20 39 35 2c 20    86,  91,  95, 
330de 20 39 36 2c 20 31 30 31 2c 20 31 30 35 2c 20 31   96, 101, 105, 1
330df 30 39 2c 20 31 31 37 2c 20 31 32 32 2c 20 31 32  09, 117, 122, 12
330e0 38 2c 20 31 33 36 2c 20 31 34 32 2c 20 31 35 32  8, 136, 142, 152
330e1 2c 0a 20 20 20 20 20 31 35 39 2c 20 31 36 32 2c  ,.     159, 162,
330e2 20 31 36 32 2c 20 31 36 35 2c 20 31 36 37 2c 20   162, 165, 167, 
330e3 31 36 37 2c 20 31 37 31 2c 20 31 37 36 2c 20 31  167, 171, 176, 1
330e4 37 39 2c 20 31 38 34 2c 20 31 38 39 2c 20 31 39  79, 184, 189, 19
330e5 34 2c 20 31 39 37 2c 0a 20 20 20 20 20 32 30 33  4, 197,.     203
330e6 2c 20 32 30 36 2c 20 32 31 30 2c 20 32 31 37 2c  , 206, 210, 217,
330e7 20 32 32 33 2c 20 32 32 33 2c 20 32 32 33 2c 20   223, 223, 223, 
330e8 32 32 36 2c 20 32 32 39 2c 20 32 33 33 2c 20 32  226, 229, 233, 2
330e9 33 34 2c 20 32 33 38 2c 20 32 34 34 2c 0a 20 20  34, 238, 244,.  
330ea 20 20 20 32 34 38 2c 20 32 35 35 2c 20 32 36 31     248, 255, 261
330eb 2c 20 32 37 33 2c 20 32 37 39 2c 20 32 38 38 2c  , 273, 279, 288,
330ec 20 32 39 30 2c 20 32 39 36 2c 20 33 30 31 2c 20   290, 296, 301, 
330ed 33 30 33 2c 20 33 31 30 2c 20 33 31 35 2c 20 33  303, 310, 315, 3
330ee 32 30 2c 0a 20 20 20 20 20 33 32 36 2c 20 33 33  20,.     326, 33
330ef 32 2c 20 33 33 37 2c 20 33 34 31 2c 20 33 34 34  2, 337, 341, 344
330f0 2c 20 33 35 30 2c 20 33 35 34 2c 20 33 36 31 2c  , 350, 354, 361,
330f1 20 33 36 33 2c 20 33 37 30 2c 20 33 37 32 2c 20   363, 370, 372, 
330f2 33 37 34 2c 20 33 38 33 2c 0a 20 20 20 20 20 33  374, 383,.     3
330f3 38 37 2c 20 33 39 33 2c 20 33 39 39 2c 20 34 30  87, 393, 399, 40
330f4 37 2c 20 34 31 32 2c 20 34 31 32 2c 20 34 32 38  7, 412, 412, 428
330f5 2c 20 34 33 35 2c 20 34 34 32 2c 20 34 34 33 2c  , 435, 442, 443,
330f6 20 34 35 30 2c 20 34 35 34 2c 20 34 35 38 2c 0a   450, 454, 458,.
330f7 20 20 20 20 20 34 36 32 2c 20 34 36 36 2c 20 34       462, 466, 4
330f8 36 39 2c 20 34 37 31 2c 20 34 37 33 2c 20 34 37  69, 471, 473, 47
330f9 39 2c 20 34 38 33 2c 20 34 39 31 2c 20 34 39 35  9, 483, 491, 495
330fa 2c 20 35 30 30 2c 20 35 30 38 2c 20 35 31 31 2c  , 500, 508, 511,
330fb 20 35 31 36 2c 0a 20 20 20 20 20 35 32 31 2c 20   516,.     521, 
330fc 35 32 37 2c 20 35 33 31 2c 20 35 33 36 2c 0a 20  527, 531, 536,. 
330fd 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   };.  static con
330fe 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
330ff 20 61 43 6f 64 65 5b 31 32 31 5d 20 3d 20 7b 0a   aCode[121] = {.
33100 20 20 20 20 54 4b 5f 52 45 49 4e 44 45 58 2c 20      TK_REINDEX, 
33101 20 20 20 54 4b 5f 49 4e 44 45 58 45 44 2c 20 20     TK_INDEXED,  
33102 20 20 54 4b 5f 49 4e 44 45 58 2c 20 20 20 20 20    TK_INDEX,     
33103 20 54 4b 5f 44 45 53 43 2c 20 20 20 20 20 20 20   TK_DESC,       
33104 54 4b 5f 45 53 43 41 50 45 2c 20 20 20 20 20 0a  TK_ESCAPE,     .
33105 20 20 20 20 54 4b 5f 45 41 43 48 2c 20 20 20 20      TK_EACH,    
33106 20 20 20 54 4b 5f 43 48 45 43 4b 2c 20 20 20 20     TK_CHECK,    
33107 20 20 54 4b 5f 4b 45 59 2c 20 20 20 20 20 20 20    TK_KEY,       
33108 20 54 4b 5f 42 45 46 4f 52 45 2c 20 20 20 20 20   TK_BEFORE,     
33109 54 4b 5f 46 4f 52 45 49 47 4e 2c 20 20 20 20 0a  TK_FOREIGN,    .
3310a 20 20 20 20 54 4b 5f 46 4f 52 2c 20 20 20 20 20      TK_FOR,     
3310b 20 20 20 54 4b 5f 49 47 4e 4f 52 45 2c 20 20 20     TK_IGNORE,   
3310c 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20 20 20    TK_LIKE_KW,   
3310d 20 54 4b 5f 45 58 50 4c 41 49 4e 2c 20 20 20 20   TK_EXPLAIN,    
3310e 54 4b 5f 49 4e 53 54 45 41 44 2c 20 20 20 20 0a  TK_INSTEAD,    .
3310f 20 20 20 20 54 4b 5f 41 44 44 2c 20 20 20 20 20      TK_ADD,     
33110 20 20 20 54 4b 5f 44 41 54 41 42 41 53 45 2c 20     TK_DATABASE, 
33111 20 20 54 4b 5f 41 53 2c 20 20 20 20 20 20 20 20    TK_AS,        
33112 20 54 4b 5f 53 45 4c 45 43 54 2c 20 20 20 20 20   TK_SELECT,     
33113 54 4b 5f 54 41 42 4c 45 2c 20 20 20 20 20 20 0a  TK_TABLE,      .
33114 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20      TK_JOIN_KW, 
33115 20 20 20 54 4b 5f 54 48 45 4e 2c 20 20 20 20 20     TK_THEN,     
33116 20 20 54 4b 5f 45 4e 44 2c 20 20 20 20 20 20 20    TK_END,       
33117 20 54 4b 5f 44 45 46 45 52 52 41 42 4c 45 2c 20   TK_DEFERRABLE, 
33118 54 4b 5f 45 4c 53 45 2c 20 20 20 20 20 20 20 0a  TK_ELSE,       .
33119 20 20 20 20 54 4b 5f 45 58 43 45 50 54 2c 20 20      TK_EXCEPT,  
3311a 20 20 20 54 4b 5f 54 52 41 4e 53 41 43 54 49 4f     TK_TRANSACTIO
3311b 4e 2c 54 4b 5f 41 43 54 49 4f 4e 2c 20 20 20 20  N,TK_ACTION,    
3311c 20 54 4b 5f 4f 4e 2c 20 20 20 20 20 20 20 20 20   TK_ON,         
3311d 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a  TK_JOIN_KW,    .
3311e 20 20 20 20 54 4b 5f 41 4c 54 45 52 2c 20 20 20      TK_ALTER,   
3311f 20 20 20 54 4b 5f 52 41 49 53 45 2c 20 20 20 20     TK_RAISE,    
33120 20 20 54 4b 5f 45 58 43 4c 55 53 49 56 45 2c 20    TK_EXCLUSIVE, 
33121 20 54 4b 5f 45 58 49 53 54 53 2c 20 20 20 20 20   TK_EXISTS,     
33122 54 4b 5f 53 41 56 45 50 4f 49 4e 54 2c 20 20 0a  TK_SAVEPOINT,  .
33123 20 20 20 20 54 4b 5f 49 4e 54 45 52 53 45 43 54      TK_INTERSECT
33124 2c 20 20 54 4b 5f 54 52 49 47 47 45 52 2c 20 20  ,  TK_TRIGGER,  
33125 20 20 54 4b 5f 52 45 46 45 52 45 4e 43 45 53 2c    TK_REFERENCES,
33126 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20   TK_CONSTRAINT, 
33127 54 4b 5f 49 4e 54 4f 2c 20 20 20 20 20 20 20 0a  TK_INTO,       .
33128 20 20 20 20 54 4b 5f 4f 46 46 53 45 54 2c 20 20      TK_OFFSET,  
33129 20 20 20 54 4b 5f 4f 46 2c 20 20 20 20 20 20 20     TK_OF,       
3312a 20 20 54 4b 5f 53 45 54 2c 20 20 20 20 20 20 20    TK_SET,       
3312b 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20 20 20 20   TK_TEMP,       
3312c 54 4b 5f 54 45 4d 50 2c 20 20 20 20 20 20 20 0a  TK_TEMP,       .
3312d 20 20 20 20 54 4b 5f 4f 52 2c 20 20 20 20 20 20      TK_OR,      
3312e 20 20 20 54 4b 5f 55 4e 49 51 55 45 2c 20 20 20     TK_UNIQUE,   
3312f 20 20 54 4b 5f 51 55 45 52 59 2c 20 20 20 20 20    TK_QUERY,     
33130 20 54 4b 5f 41 54 54 41 43 48 2c 20 20 20 20 20   TK_ATTACH,     
33131 54 4b 5f 48 41 56 49 4e 47 2c 20 20 20 20 20 0a  TK_HAVING,     .
33132 20 20 20 20 54 4b 5f 47 52 4f 55 50 2c 20 20 20      TK_GROUP,   
33133 20 20 20 54 4b 5f 55 50 44 41 54 45 2c 20 20 20     TK_UPDATE,   
33134 20 20 54 4b 5f 42 45 47 49 4e 2c 20 20 20 20 20    TK_BEGIN,     
33135 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20   TK_JOIN_KW,    
33136 54 4b 5f 52 45 4c 45 41 53 45 2c 20 20 20 20 0a  TK_RELEASE,    .
33137 20 20 20 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20      TK_BETWEEN, 
33138 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 20     TK_NOTNULL,  
33139 20 20 54 4b 5f 4e 4f 54 2c 20 20 20 20 20 20 20    TK_NOT,       
3313a 20 54 4b 5f 4e 4f 2c 20 20 20 20 20 20 20 20 20   TK_NO,         
3313b 54 4b 5f 4e 55 4c 4c 2c 20 20 20 20 20 20 20 0a  TK_NULL,       .
3313c 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20      TK_LIKE_KW, 
3313d 20 20 20 54 4b 5f 43 41 53 43 41 44 45 2c 20 20     TK_CASCADE,  
3313e 20 20 54 4b 5f 41 53 43 2c 20 20 20 20 20 20 20    TK_ASC,       
3313f 20 54 4b 5f 44 45 4c 45 54 45 2c 20 20 20 20 20   TK_DELETE,     
33140 54 4b 5f 43 41 53 45 2c 20 20 20 20 20 20 20 0a  TK_CASE,       .
33141 20 20 20 20 54 4b 5f 43 4f 4c 4c 41 54 45 2c 20      TK_COLLATE, 
33142 20 20 20 54 4b 5f 43 52 45 41 54 45 2c 20 20 20     TK_CREATE,   
33143 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20 20    TK_CTIME_KW,  
33144 20 54 4b 5f 44 45 54 41 43 48 2c 20 20 20 20 20   TK_DETACH,     
33145 54 4b 5f 49 4d 4d 45 44 49 41 54 45 2c 20 20 0a  TK_IMMEDIATE,  .
33146 20 20 20 20 54 4b 5f 4a 4f 49 4e 2c 20 20 20 20      TK_JOIN,    
33147 20 20 20 54 4b 5f 49 4e 53 45 52 54 2c 20 20 20     TK_INSERT,   
33148 20 20 54 4b 5f 4d 41 54 43 48 2c 20 20 20 20 20    TK_MATCH,     
33149 20 54 4b 5f 50 4c 41 4e 2c 20 20 20 20 20 20 20   TK_PLAN,       
3314a 54 4b 5f 41 4e 41 4c 59 5a 45 2c 20 20 20 20 0a  TK_ANALYZE,    .
3314b 20 20 20 20 54 4b 5f 50 52 41 47 4d 41 2c 20 20      TK_PRAGMA,  
3314c 20 20 20 54 4b 5f 41 42 4f 52 54 2c 20 20 20 20     TK_ABORT,    
3314d 20 20 54 4b 5f 56 41 4c 55 45 53 2c 20 20 20 20    TK_VALUES,    
3314e 20 54 4b 5f 56 49 52 54 55 41 4c 2c 20 20 20 20   TK_VIRTUAL,    
3314f 54 4b 5f 4c 49 4d 49 54 2c 20 20 20 20 20 20 0a  TK_LIMIT,      .
33150 20 20 20 20 54 4b 5f 57 48 45 4e 2c 20 20 20 20      TK_WHEN,    
33151 20 20 20 54 4b 5f 57 48 45 52 45 2c 20 20 20 20     TK_WHERE,    
33152 20 20 54 4b 5f 52 45 4e 41 4d 45 2c 20 20 20 20    TK_RENAME,    
33153 20 54 4b 5f 41 46 54 45 52 2c 20 20 20 20 20 20   TK_AFTER,      
33154 54 4b 5f 52 45 50 4c 41 43 45 2c 20 20 20 20 0a  TK_REPLACE,    .
33155 20 20 20 20 54 4b 5f 41 4e 44 2c 20 20 20 20 20      TK_AND,     
33156 20 20 20 54 4b 5f 44 45 46 41 55 4c 54 2c 20 20     TK_DEFAULT,  
33157 20 20 54 4b 5f 41 55 54 4f 49 4e 43 52 2c 20 20    TK_AUTOINCR,  
33158 20 54 4b 5f 54 4f 2c 20 20 20 20 20 20 20 20 20   TK_TO,         
33159 54 4b 5f 49 4e 2c 20 20 20 20 20 20 20 20 20 0a  TK_IN,         .
3315a 20 20 20 20 54 4b 5f 43 41 53 54 2c 20 20 20 20      TK_CAST,    
3315b 20 20 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 2c 20     TK_COLUMNKW, 
3315c 20 20 54 4b 5f 43 4f 4d 4d 49 54 2c 20 20 20 20    TK_COMMIT,    
3315d 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 2c 20 20 20   TK_CONFLICT,   
3315e 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a  TK_JOIN_KW,    .
3315f 20 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c      TK_CTIME_KW,
33160 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c 20     TK_CTIME_KW, 
33161 20 20 54 4b 5f 50 52 49 4d 41 52 59 2c 20 20 20    TK_PRIMARY,   
33162 20 54 4b 5f 44 45 46 45 52 52 45 44 2c 20 20 20   TK_DEFERRED,   
33163 54 4b 5f 44 49 53 54 49 4e 43 54 2c 20 20 20 0a  TK_DISTINCT,   .
33164 20 20 20 20 54 4b 5f 49 53 2c 20 20 20 20 20 20      TK_IS,      
33165 20 20 20 54 4b 5f 44 52 4f 50 2c 20 20 20 20 20     TK_DROP,     
33166 20 20 54 4b 5f 46 41 49 4c 2c 20 20 20 20 20 20    TK_FAIL,      
33167 20 54 4b 5f 46 52 4f 4d 2c 20 20 20 20 20 20 20   TK_FROM,       
33168 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20 20 0a  TK_JOIN_KW,    .
33169 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20      TK_LIKE_KW, 
3316a 20 20 20 54 4b 5f 42 59 2c 20 20 20 20 20 20 20     TK_BY,       
3316b 20 20 54 4b 5f 49 46 2c 20 20 20 20 20 20 20 20    TK_IF,        
3316c 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 20 20 20 20   TK_ISNULL,     
3316d 54 4b 5f 4f 52 44 45 52 2c 20 20 20 20 20 20 0a  TK_ORDER,      .
3316e 20 20 20 20 54 4b 5f 52 45 53 54 52 49 43 54 2c      TK_RESTRICT,
3316f 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
33170 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20    TK_JOIN_KW,   
33171 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 2c 20 20 20   TK_ROLLBACK,   
33172 54 4b 5f 52 4f 57 2c 20 20 20 20 20 20 20 20 0a  TK_ROW,        .
33173 20 20 20 20 54 4b 5f 55 4e 49 4f 4e 2c 20 20 20      TK_UNION,   
33174 20 20 20 54 4b 5f 55 53 49 4e 47 2c 20 20 20 20     TK_USING,    
33175 20 20 54 4b 5f 56 41 43 55 55 4d 2c 20 20 20 20    TK_VACUUM,    
33176 20 54 4b 5f 56 49 45 57 2c 20 20 20 20 20 20 20   TK_VIEW,       
33177 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 2c 20 20 0a  TK_INITIALLY,  .
33178 20 20 20 20 54 4b 5f 41 4c 4c 2c 20 20 20 20 20      TK_ALL,     
33179 20 20 20 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 68     .  };.  int h
3317a 2c 20 69 3b 0a 20 20 69 66 28 20 6e 3c 32 20 29  , i;.  if( n<2 )
3317b 20 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 20   return TK_ID;. 
3317c 20 68 20 3d 20 28 28 63 68 61 72 4d 61 70 28 7a   h = ((charMap(z
3317d 5b 30 5d 29 2a 34 29 20 5e 0a 20 20 20 20 20 20  [0])*4) ^.      
3317e 28 63 68 61 72 4d 61 70 28 7a 5b 6e 2d 31 5d 29  (charMap(z[n-1])
3317f 2a 33 29 20 5e 0a 20 20 20 20 20 20 6e 29 20 25  *3) ^.      n) %
33180 20 31 32 37 3b 0a 20 20 66 6f 72 28 69 3d 28 28   127;.  for(i=((
33181 69 6e 74 29 61 48 61 73 68 5b 68 5d 29 2d 31 3b  int)aHash[h])-1;
33182 20 69 3e 3d 30 3b 20 69 3d 28 28 69 6e 74 29 61   i>=0; i=((int)a
33183 4e 65 78 74 5b 69 5d 29 2d 31 29 7b 0a 20 20 20  Next[i])-1){.   
33184 20 69 66 28 20 61 4c 65 6e 5b 69 5d 3d 3d 6e 20   if( aLen[i]==n 
33185 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
33186 6d 70 28 26 7a 54 65 78 74 5b 61 4f 66 66 73 65  mp(&zText[aOffse
33187 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b  t[i]],z,n)==0 ){
33188 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
33189 20 69 3d 3d 30 20 29 3b 20 2f 2a 20 52 45 49 4e   i==0 ); /* REIN
3318a 44 45 58 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  DEX */.      tes
3318b 74 63 61 73 65 28 20 69 3d 3d 31 20 29 3b 20 2f  tcase( i==1 ); /
3318c 2a 20 49 4e 44 45 58 45 44 20 2a 2f 0a 20 20 20  * INDEXED */.   
3318d 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3318e 32 20 29 3b 20 2f 2a 20 49 4e 44 45 58 20 2a 2f  2 ); /* INDEX */
3318f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
33190 20 69 3d 3d 33 20 29 3b 20 2f 2a 20 44 45 53 43   i==3 ); /* DESC
33191 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
33192 73 65 28 20 69 3d 3d 34 20 29 3b 20 2f 2a 20 45  se( i==4 ); /* E
33193 53 43 41 50 45 20 2a 2f 0a 20 20 20 20 20 20 74  SCAPE */.      t
33194 65 73 74 63 61 73 65 28 20 69 3d 3d 35 20 29 3b  estcase( i==5 );
33195 20 2f 2a 20 45 41 43 48 20 2a 2f 0a 20 20 20 20   /* EACH */.    
33196 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
33197 20 29 3b 20 2f 2a 20 43 48 45 43 4b 20 2a 2f 0a   ); /* CHECK */.
33198 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33199 69 3d 3d 37 20 29 3b 20 2f 2a 20 4b 45 59 20 2a  i==7 ); /* KEY *
3319a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
3319b 28 20 69 3d 3d 38 20 29 3b 20 2f 2a 20 42 45 46  ( i==8 ); /* BEF
3319c 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ORE */.      tes
3319d 74 63 61 73 65 28 20 69 3d 3d 39 20 29 3b 20 2f  tcase( i==9 ); /
3319e 2a 20 46 4f 52 45 49 47 4e 20 2a 2f 0a 20 20 20  * FOREIGN */.   
3319f 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
331a0 31 30 20 29 3b 20 2f 2a 20 46 4f 52 20 2a 2f 0a  10 ); /* FOR */.
331a1 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
331a2 69 3d 3d 31 31 20 29 3b 20 2f 2a 20 49 47 4e 4f  i==11 ); /* IGNO
331a3 52 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  RE */.      test
331a4 63 61 73 65 28 20 69 3d 3d 31 32 20 29 3b 20 2f  case( i==12 ); /
331a5 2a 20 52 45 47 45 58 50 20 2a 2f 0a 20 20 20 20  * REGEXP */.    
331a6 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
331a7 33 20 29 3b 20 2f 2a 20 45 58 50 4c 41 49 4e 20  3 ); /* EXPLAIN 
331a8 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
331a9 65 28 20 69 3d 3d 31 34 20 29 3b 20 2f 2a 20 49  e( i==14 ); /* I
331aa 4e 53 54 45 41 44 20 2a 2f 0a 20 20 20 20 20 20  NSTEAD */.      
331ab 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 35 20  testcase( i==15 
331ac 29 3b 20 2f 2a 20 41 44 44 20 2a 2f 0a 20 20 20  ); /* ADD */.   
331ad 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
331ae 31 36 20 29 3b 20 2f 2a 20 44 41 54 41 42 41 53  16 ); /* DATABAS
331af 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
331b0 61 73 65 28 20 69 3d 3d 31 37 20 29 3b 20 2f 2a  ase( i==17 ); /*
331b1 20 41 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73   AS */.      tes
331b2 74 63 61 73 65 28 20 69 3d 3d 31 38 20 29 3b 20  tcase( i==18 ); 
331b3 2f 2a 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 20  /* SELECT */.   
331b4 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
331b5 31 39 20 29 3b 20 2f 2a 20 54 41 42 4c 45 20 2a  19 ); /* TABLE *
331b6 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
331b7 28 20 69 3d 3d 32 30 20 29 3b 20 2f 2a 20 4c 45  ( i==20 ); /* LE
331b8 46 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  FT */.      test
331b9 63 61 73 65 28 20 69 3d 3d 32 31 20 29 3b 20 2f  case( i==21 ); /
331ba 2a 20 54 48 45 4e 20 2a 2f 0a 20 20 20 20 20 20  * THEN */.      
331bb 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 32 20  testcase( i==22 
331bc 29 3b 20 2f 2a 20 45 4e 44 20 2a 2f 0a 20 20 20  ); /* END */.   
331bd 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
331be 32 33 20 29 3b 20 2f 2a 20 44 45 46 45 52 52 41  23 ); /* DEFERRA
331bf 42 4c 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  BLE */.      tes
331c0 74 63 61 73 65 28 20 69 3d 3d 32 34 20 29 3b 20  tcase( i==24 ); 
331c1 2f 2a 20 45 4c 53 45 20 2a 2f 0a 20 20 20 20 20  /* ELSE */.     
331c2 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32 35   testcase( i==25
331c3 20 29 3b 20 2f 2a 20 45 58 43 45 50 54 20 2a 2f   ); /* EXCEPT */
331c4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
331c5 20 69 3d 3d 32 36 20 29 3b 20 2f 2a 20 54 52 41   i==26 ); /* TRA
331c6 4e 53 41 43 54 49 4f 4e 20 2a 2f 0a 20 20 20 20  NSACTION */.    
331c7 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32    testcase( i==2
331c8 37 20 29 3b 20 2f 2a 20 41 43 54 49 4f 4e 20 2a  7 ); /* ACTION *
331c9 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
331ca 28 20 69 3d 3d 32 38 20 29 3b 20 2f 2a 20 4f 4e  ( i==28 ); /* ON
331cb 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
331cc 73 65 28 20 69 3d 3d 32 39 20 29 3b 20 2f 2a 20  se( i==29 ); /* 
331cd 4e 41 54 55 52 41 4c 20 2a 2f 0a 20 20 20 20 20  NATURAL */.     
331ce 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 33 30   testcase( i==30
331cf 20 29 3b 20 2f 2a 20 41 4c 54 45 52 20 2a 2f 0a   ); /* ALTER */.
331d0 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
331d1 69 3d 3d 33 31 20 29 3b 20 2f 2a 20 52 41 49 53  i==31 ); /* RAIS
331d2 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
331d3 61 73 65 28 20 69 3d 3d 33 32 20 29 3b 20 2f 2a  ase( i==32 ); /*
331d4 20 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20   EXCLUSIVE */.  
331d5 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
331d6 3d 33 33 20 29 3b 20 2f 2a 20 45 58 49 53 54 53  =33 ); /* EXISTS
331d7 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
331d8 73 65 28 20 69 3d 3d 33 34 20 29 3b 20 2f 2a 20  se( i==34 ); /* 
331d9 53 41 56 45 50 4f 49 4e 54 20 2a 2f 0a 20 20 20  SAVEPOINT */.   
331da 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
331db 33 35 20 29 3b 20 2f 2a 20 49 4e 54 45 52 53 45  35 ); /* INTERSE
331dc 43 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  CT */.      test
331dd 63 61 73 65 28 20 69 3d 3d 33 36 20 29 3b 20 2f  case( i==36 ); /
331de 2a 20 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 20  * TRIGGER */.   
331df 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
331e0 33 37 20 29 3b 20 2f 2a 20 52 45 46 45 52 45 4e  37 ); /* REFEREN
331e1 43 45 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  CES */.      tes
331e2 74 63 61 73 65 28 20 69 3d 3d 33 38 20 29 3b 20  tcase( i==38 ); 
331e3 2f 2a 20 43 4f 4e 53 54 52 41 49 4e 54 20 2a 2f  /* CONSTRAINT */
331e4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
331e5 20 69 3d 3d 33 39 20 29 3b 20 2f 2a 20 49 4e 54   i==39 ); /* INT
331e6 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  O */.      testc
331e7 61 73 65 28 20 69 3d 3d 34 30 20 29 3b 20 2f 2a  ase( i==40 ); /*
331e8 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 20 20 20   OFFSET */.     
331e9 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 31   testcase( i==41
331ea 20 29 3b 20 2f 2a 20 4f 46 20 2a 2f 0a 20 20 20   ); /* OF */.   
331eb 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
331ec 34 32 20 29 3b 20 2f 2a 20 53 45 54 20 2a 2f 0a  42 ); /* SET */.
331ed 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
331ee 69 3d 3d 34 33 20 29 3b 20 2f 2a 20 54 45 4d 50  i==43 ); /* TEMP
331ef 4f 52 41 52 59 20 2a 2f 0a 20 20 20 20 20 20 74  ORARY */.      t
331f0 65 73 74 63 61 73 65 28 20 69 3d 3d 34 34 20 29  estcase( i==44 )
331f1 3b 20 2f 2a 20 54 45 4d 50 20 2a 2f 0a 20 20 20  ; /* TEMP */.   
331f2 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
331f3 34 35 20 29 3b 20 2f 2a 20 4f 52 20 2a 2f 0a 20  45 ); /* OR */. 
331f4 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
331f5 3d 3d 34 36 20 29 3b 20 2f 2a 20 55 4e 49 51 55  ==46 ); /* UNIQU
331f6 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
331f7 61 73 65 28 20 69 3d 3d 34 37 20 29 3b 20 2f 2a  ase( i==47 ); /*
331f8 20 51 55 45 52 59 20 2a 2f 0a 20 20 20 20 20 20   QUERY */.      
331f9 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34 38 20  testcase( i==48 
331fa 29 3b 20 2f 2a 20 41 54 54 41 43 48 20 2a 2f 0a  ); /* ATTACH */.
331fb 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
331fc 69 3d 3d 34 39 20 29 3b 20 2f 2a 20 48 41 56 49  i==49 ); /* HAVI
331fd 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  NG */.      test
331fe 63 61 73 65 28 20 69 3d 3d 35 30 20 29 3b 20 2f  case( i==50 ); /
331ff 2a 20 47 52 4f 55 50 20 2a 2f 0a 20 20 20 20 20  * GROUP */.     
33200 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 31   testcase( i==51
33201 20 29 3b 20 2f 2a 20 55 50 44 41 54 45 20 2a 2f   ); /* UPDATE */
33202 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
33203 20 69 3d 3d 35 32 20 29 3b 20 2f 2a 20 42 45 47   i==52 ); /* BEG
33204 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IN */.      test
33205 63 61 73 65 28 20 69 3d 3d 35 33 20 29 3b 20 2f  case( i==53 ); /
33206 2a 20 49 4e 4e 45 52 20 2a 2f 0a 20 20 20 20 20  * INNER */.     
33207 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 34   testcase( i==54
33208 20 29 3b 20 2f 2a 20 52 45 4c 45 41 53 45 20 2a   ); /* RELEASE *
33209 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
3320a 28 20 69 3d 3d 35 35 20 29 3b 20 2f 2a 20 42 45  ( i==55 ); /* BE
3320b 54 57 45 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74  TWEEN */.      t
3320c 65 73 74 63 61 73 65 28 20 69 3d 3d 35 36 20 29  estcase( i==56 )
3320d 3b 20 2f 2a 20 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a  ; /* NOTNULL */.
3320e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3320f 69 3d 3d 35 37 20 29 3b 20 2f 2a 20 4e 4f 54 20  i==57 ); /* NOT 
33210 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33211 65 28 20 69 3d 3d 35 38 20 29 3b 20 2f 2a 20 4e  e( i==58 ); /* N
33212 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  O */.      testc
33213 61 73 65 28 20 69 3d 3d 35 39 20 29 3b 20 2f 2a  ase( i==59 ); /*
33214 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 74   NULL */.      t
33215 65 73 74 63 61 73 65 28 20 69 3d 3d 36 30 20 29  estcase( i==60 )
33216 3b 20 2f 2a 20 4c 49 4b 45 20 2a 2f 0a 20 20 20  ; /* LIKE */.   
33217 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
33218 36 31 20 29 3b 20 2f 2a 20 43 41 53 43 41 44 45  61 ); /* CASCADE
33219 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3321a 73 65 28 20 69 3d 3d 36 32 20 29 3b 20 2f 2a 20  se( i==62 ); /* 
3321b 41 53 43 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ASC */.      tes
3321c 74 63 61 73 65 28 20 69 3d 3d 36 33 20 29 3b 20  tcase( i==63 ); 
3321d 2f 2a 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20 20  /* DELETE */.   
3321e 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
3321f 36 34 20 29 3b 20 2f 2a 20 43 41 53 45 20 2a 2f  64 ); /* CASE */
33220 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
33221 20 69 3d 3d 36 35 20 29 3b 20 2f 2a 20 43 4f 4c   i==65 ); /* COL
33222 4c 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  LATE */.      te
33223 73 74 63 61 73 65 28 20 69 3d 3d 36 36 20 29 3b  stcase( i==66 );
33224 20 2f 2a 20 43 52 45 41 54 45 20 2a 2f 0a 20 20   /* CREATE */.  
33225 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
33226 3d 36 37 20 29 3b 20 2f 2a 20 43 55 52 52 45 4e  =67 ); /* CURREN
33227 54 5f 44 41 54 45 20 2a 2f 0a 20 20 20 20 20 20  T_DATE */.      
33228 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 38 20  testcase( i==68 
33229 29 3b 20 2f 2a 20 44 45 54 41 43 48 20 2a 2f 0a  ); /* DETACH */.
3322a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3322b 69 3d 3d 36 39 20 29 3b 20 2f 2a 20 49 4d 4d 45  i==69 ); /* IMME
3322c 44 49 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 74  DIATE */.      t
3322d 65 73 74 63 61 73 65 28 20 69 3d 3d 37 30 20 29  estcase( i==70 )
3322e 3b 20 2f 2a 20 4a 4f 49 4e 20 2a 2f 0a 20 20 20  ; /* JOIN */.   
3322f 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
33230 37 31 20 29 3b 20 2f 2a 20 49 4e 53 45 52 54 20  71 ); /* INSERT 
33231 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33232 65 28 20 69 3d 3d 37 32 20 29 3b 20 2f 2a 20 4d  e( i==72 ); /* M
33233 41 54 43 48 20 2a 2f 0a 20 20 20 20 20 20 74 65  ATCH */.      te
33234 73 74 63 61 73 65 28 20 69 3d 3d 37 33 20 29 3b  stcase( i==73 );
33235 20 2f 2a 20 50 4c 41 4e 20 2a 2f 0a 20 20 20 20   /* PLAN */.    
33236 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37    testcase( i==7
33237 34 20 29 3b 20 2f 2a 20 41 4e 41 4c 59 5a 45 20  4 ); /* ANALYZE 
33238 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33239 65 28 20 69 3d 3d 37 35 20 29 3b 20 2f 2a 20 50  e( i==75 ); /* P
3323a 52 41 47 4d 41 20 2a 2f 0a 20 20 20 20 20 20 74  RAGMA */.      t
3323b 65 73 74 63 61 73 65 28 20 69 3d 3d 37 36 20 29  estcase( i==76 )
3323c 3b 20 2f 2a 20 41 42 4f 52 54 20 2a 2f 0a 20 20  ; /* ABORT */.  
3323d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3323e 3d 37 37 20 29 3b 20 2f 2a 20 56 41 4c 55 45 53  =77 ); /* VALUES
3323f 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
33240 73 65 28 20 69 3d 3d 37 38 20 29 3b 20 2f 2a 20  se( i==78 ); /* 
33241 56 49 52 54 55 41 4c 20 2a 2f 0a 20 20 20 20 20  VIRTUAL */.     
33242 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 39   testcase( i==79
33243 20 29 3b 20 2f 2a 20 4c 49 4d 49 54 20 2a 2f 0a   ); /* LIMIT */.
33244 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33245 69 3d 3d 38 30 20 29 3b 20 2f 2a 20 57 48 45 4e  i==80 ); /* WHEN
33246 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
33247 73 65 28 20 69 3d 3d 38 31 20 29 3b 20 2f 2a 20  se( i==81 ); /* 
33248 57 48 45 52 45 20 2a 2f 0a 20 20 20 20 20 20 74  WHERE */.      t
33249 65 73 74 63 61 73 65 28 20 69 3d 3d 38 32 20 29  estcase( i==82 )
3324a 3b 20 2f 2a 20 52 45 4e 41 4d 45 20 2a 2f 0a 20  ; /* RENAME */. 
3324b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3324c 3d 3d 38 33 20 29 3b 20 2f 2a 20 41 46 54 45 52  ==83 ); /* AFTER
3324d 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3324e 73 65 28 20 69 3d 3d 38 34 20 29 3b 20 2f 2a 20  se( i==84 ); /* 
3324f 52 45 50 4c 41 43 45 20 2a 2f 0a 20 20 20 20 20  REPLACE */.     
33250 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 35   testcase( i==85
33251 20 29 3b 20 2f 2a 20 41 4e 44 20 2a 2f 0a 20 20   ); /* AND */.  
33252 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
33253 3d 38 36 20 29 3b 20 2f 2a 20 44 45 46 41 55 4c  =86 ); /* DEFAUL
33254 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
33255 61 73 65 28 20 69 3d 3d 38 37 20 29 3b 20 2f 2a  ase( i==87 ); /*
33256 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a   AUTOINCREMENT *
33257 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
33258 28 20 69 3d 3d 38 38 20 29 3b 20 2f 2a 20 54 4f  ( i==88 ); /* TO
33259 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3325a 73 65 28 20 69 3d 3d 38 39 20 29 3b 20 2f 2a 20  se( i==89 ); /* 
3325b 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IN */.      test
3325c 63 61 73 65 28 20 69 3d 3d 39 30 20 29 3b 20 2f  case( i==90 ); /
3325d 2a 20 43 41 53 54 20 2a 2f 0a 20 20 20 20 20 20  * CAST */.      
3325e 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39 31 20  testcase( i==91 
3325f 29 3b 20 2f 2a 20 43 4f 4c 55 4d 4e 20 2a 2f 0a  ); /* COLUMN */.
33260 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
33261 69 3d 3d 39 32 20 29 3b 20 2f 2a 20 43 4f 4d 4d  i==92 ); /* COMM
33262 49 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IT */.      test
33263 63 61 73 65 28 20 69 3d 3d 39 33 20 29 3b 20 2f  case( i==93 ); /
33264 2a 20 43 4f 4e 46 4c 49 43 54 20 2a 2f 0a 20 20  * CONFLICT */.  
33265 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
33266 3d 39 34 20 29 3b 20 2f 2a 20 43 52 4f 53 53 20  =94 ); /* CROSS 
33267 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33268 65 28 20 69 3d 3d 39 35 20 29 3b 20 2f 2a 20 43  e( i==95 ); /* C
33269 55 52 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50  URRENT_TIMESTAMP
3326a 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3326b 73 65 28 20 69 3d 3d 39 36 20 29 3b 20 2f 2a 20  se( i==96 ); /* 
3326c 43 55 52 52 45 4e 54 5f 54 49 4d 45 20 2a 2f 0a  CURRENT_TIME */.
3326d 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3326e 69 3d 3d 39 37 20 29 3b 20 2f 2a 20 50 52 49 4d  i==97 ); /* PRIM
3326f 41 52 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ARY */.      tes
33270 74 63 61 73 65 28 20 69 3d 3d 39 38 20 29 3b 20  tcase( i==98 ); 
33271 2f 2a 20 44 45 46 45 52 52 45 44 20 2a 2f 0a 20  /* DEFERRED */. 
33272 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
33273 3d 3d 39 39 20 29 3b 20 2f 2a 20 44 49 53 54 49  ==99 ); /* DISTI
33274 4e 43 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  NCT */.      tes
33275 74 63 61 73 65 28 20 69 3d 3d 31 30 30 20 29 3b  tcase( i==100 );
33276 20 2f 2a 20 49 53 20 2a 2f 0a 20 20 20 20 20 20   /* IS */.      
33277 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 31  testcase( i==101
33278 20 29 3b 20 2f 2a 20 44 52 4f 50 20 2a 2f 0a 20   ); /* DROP */. 
33279 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3327a 3d 3d 31 30 32 20 29 3b 20 2f 2a 20 46 41 49 4c  ==102 ); /* FAIL
3327b 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3327c 73 65 28 20 69 3d 3d 31 30 33 20 29 3b 20 2f 2a  se( i==103 ); /*
3327d 20 46 52 4f 4d 20 2a 2f 0a 20 20 20 20 20 20 74   FROM */.      t
3327e 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 34 20  estcase( i==104 
3327f 29 3b 20 2f 2a 20 46 55 4c 4c 20 2a 2f 0a 20 20  ); /* FULL */.  
33280 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
33281 3d 31 30 35 20 29 3b 20 2f 2a 20 47 4c 4f 42 20  =105 ); /* GLOB 
33282 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
33283 65 28 20 69 3d 3d 31 30 36 20 29 3b 20 2f 2a 20  e( i==106 ); /* 
33284 42 59 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  BY */.      test
33285 63 61 73 65 28 20 69 3d 3d 31 30 37 20 29 3b 20  case( i==107 ); 
33286 2f 2a 20 49 46 20 2a 2f 0a 20 20 20 20 20 20 74  /* IF */.      t
33287 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 38 20  estcase( i==108 
33288 29 3b 20 2f 2a 20 49 53 4e 55 4c 4c 20 2a 2f 0a  ); /* ISNULL */.
33289 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
3328a 69 3d 3d 31 30 39 20 29 3b 20 2f 2a 20 4f 52 44  i==109 ); /* ORD
3328b 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ER */.      test
3328c 63 61 73 65 28 20 69 3d 3d 31 31 30 20 29 3b 20  case( i==110 ); 
3328d 2f 2a 20 52 45 53 54 52 49 43 54 20 2a 2f 0a 20  /* RESTRICT */. 
3328e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
3328f 3d 3d 31 31 31 20 29 3b 20 2f 2a 20 4f 55 54 45  ==111 ); /* OUTE
33290 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  R */.      testc
33291 61 73 65 28 20 69 3d 3d 31 31 32 20 29 3b 20 2f  ase( i==112 ); /
33292 2a 20 52 49 47 48 54 20 2a 2f 0a 20 20 20 20 20  * RIGHT */.     
33293 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31   testcase( i==11
33294 33 20 29 3b 20 2f 2a 20 52 4f 4c 4c 42 41 43 4b  3 ); /* ROLLBACK
33295 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
33296 73 65 28 20 69 3d 3d 31 31 34 20 29 3b 20 2f 2a  se( i==114 ); /*
33297 20 52 4f 57 20 2a 2f 0a 20 20 20 20 20 20 74 65   ROW */.      te
33298 73 74 63 61 73 65 28 20 69 3d 3d 31 31 35 20 29  stcase( i==115 )
33299 3b 20 2f 2a 20 55 4e 49 4f 4e 20 2a 2f 0a 20 20  ; /* UNION */.  
3329a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
3329b 3d 31 31 36 20 29 3b 20 2f 2a 20 55 53 49 4e 47  =116 ); /* USING
3329c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
3329d 73 65 28 20 69 3d 3d 31 31 37 20 29 3b 20 2f 2a  se( i==117 ); /*
3329e 20 56 41 43 55 55 4d 20 2a 2f 0a 20 20 20 20 20   VACUUM */.     
3329f 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31   testcase( i==11
332a0 38 20 29 3b 20 2f 2a 20 56 49 45 57 20 2a 2f 0a  8 ); /* VIEW */.
332a1 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
332a2 69 3d 3d 31 31 39 20 29 3b 20 2f 2a 20 49 4e 49  i==119 ); /* INI
332a3 54 49 41 4c 4c 59 20 2a 2f 0a 20 20 20 20 20 20  TIALLY */.      
332a4 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 32 30  testcase( i==120
332a5 20 29 3b 20 2f 2a 20 41 4c 4c 20 2a 2f 0a 20 20   ); /* ALL */.  
332a6 20 20 20 20 72 65 74 75 72 6e 20 61 43 6f 64 65      return aCode
332a7 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  [i];.    }.  }. 
332a8 20 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b 0a 7d   return TK_ID;.}
332a9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
332aa 69 6e 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f  int sqlite3Keywo
332ab 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73  rdCode(const uns
332ac 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69  igned char *z, i
332ad 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt n){.  return 
332ae 6b 65 79 77 6f 72 64 43 6f 64 65 28 28 63 68 61  keywordCode((cha
332af 72 2a 29 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f 2a 2a  r*)z, n);.}../**
332b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
332b1 20 6f 66 20 6b 65 79 77 6f 72 64 68 61 73 68 2e   of keywordhash.
332b2 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
332b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
332b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
332b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
332b6 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
332b7 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 74 6f 6b   left off in tok
332b8 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  enize.c ********
332b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f  ***********/.../
332ba 2a 0a 2a 2a 20 49 66 20 58 20 69 73 20 61 20 63  *.** If X is a c
332bb 68 61 72 61 63 74 65 72 20 74 68 61 74 20 63 61  haracter that ca
332bc 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61 6e 20  n be used in an 
332bd 69 64 65 6e 74 69 66 69 65 72 20 74 68 65 6e 0a  identifier then.
332be 2a 2a 20 49 64 43 68 61 72 28 58 29 20 77 69 6c  ** IdChar(X) wil
332bf 6c 20 62 65 20 74 72 75 65 2e 20 20 4f 74 68 65  l be true.  Othe
332c0 72 77 69 73 65 20 69 74 20 69 73 20 66 61 6c 73  rwise it is fals
332c1 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43  e..**.** For ASC
332c2 49 49 2c 20 61 6e 79 20 63 68 61 72 61 63 74 65  II, any characte
332c3 72 20 77 69 74 68 20 74 68 65 20 68 69 67 68 2d  r with the high-
332c4 6f 72 64 65 72 20 62 69 74 20 73 65 74 20 69 73  order bit set is
332c5 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61  .** allowed in a
332c6 6e 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 46  n identifier.  F
332c7 6f 72 20 37 2d 62 69 74 20 63 68 61 72 61 63 74  or 7-bit charact
332c8 65 72 73 2c 20 0a 2a 2a 20 73 71 6c 69 74 65 33  ers, .** sqlite3
332c9 49 73 49 64 43 68 61 72 5b 58 5d 20 6d 75 73 74  IsIdChar[X] must
332ca 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72   be 1..**.** For
332cb 20 45 42 43 44 49 43 2c 20 74 68 65 20 72 75 6c   EBCDIC, the rul
332cc 65 73 20 61 72 65 20 6d 6f 72 65 20 63 6f 6d 70  es are more comp
332cd 6c 65 78 20 62 75 74 20 68 61 76 65 20 74 68 65  lex but have the
332ce 20 73 61 6d 65 0a 2a 2a 20 65 6e 64 20 72 65 73   same.** end res
332cf 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65  ult..**.** Ticke
332d0 74 20 23 31 30 36 36 2e 20 20 74 68 65 20 53 51  t #1066.  the SQ
332d1 4c 20 73 74 61 6e 64 61 72 64 20 64 6f 65 73 20  L standard does 
332d2 6e 6f 74 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e  not allow '$' in
332d3 20 74 68 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f   the.** middle o
332d4 66 20 69 64 65 6e 74 66 69 65 72 73 2e 20 20 42  f identfiers.  B
332d5 75 74 20 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c  ut many SQL impl
332d6 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20  ementations do. 
332d7 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
332d8 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 69 64 65  allow '$' in ide
332d9 6e 74 69 66 69 65 72 73 20 66 6f 72 20 63 6f 6d  ntifiers for com
332da 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42  patibility..** B
332db 75 74 20 74 68 65 20 66 65 61 74 75 72 65 20 69  ut the feature i
332dc 73 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a  s undocumented..
332dd 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
332de 5f 41 53 43 49 49 0a 23 64 65 66 69 6e 65 20 49  _ASCII.#define I
332df 64 43 68 61 72 28 43 29 20 20 28 28 73 71 6c 69  dChar(C)  ((sqli
332e0 74 65 33 43 74 79 70 65 4d 61 70 5b 28 75 6e 73  te3CtypeMap[(uns
332e1 69 67 6e 65 64 20 63 68 61 72 29 43 5d 26 30 78  igned char)C]&0x
332e2 34 36 29 21 3d 30 29 0a 23 65 6e 64 69 66 0a 23  46)!=0).#endif.#
332e3 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43  ifdef SQLITE_EBC
332e4 44 49 43 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  DIC.SQLITE_PRIVA
332e5 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  TE const char sq
332e6 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43  lite3IsEbcdicIdC
332e7 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30 20  har[] = {./* x0 
332e8 78 31 20 78 32 20 78 33 20 78 34 20 78 35 20 78  x1 x2 x3 x4 x5 x
332e9 36 20 78 37 20 78 38 20 78 39 20 78 41 20 78 42  6 x7 x8 x9 xA xB
332ea 20 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f 0a   xC xD xE xF */.
332eb 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20      0, 0, 1, 1, 
332ec 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
332ed 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
332ee 20 30 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20   0,  /* 4x */.  
332ef 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
332f0 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
332f1 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 1, 0, 0, 0, 0
332f2 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20  ,  /* 5x */.    
332f3 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 0, 1, 1, 1, 1
332f4 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
332f5 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20   0, 0, 1, 0, 0, 
332f6 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 6x */.    0,
332f7 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
332f8 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
332f9 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
332fa 2a 20 37 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 7x */.    0, 1
332fb 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
332fc 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
332fd 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20  1, 1, 1, 0,  /* 
332fe 38 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  8x */.    0, 1, 
332ff 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
33300 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 31 2c  , 1, 1, 0, 0, 1,
33301 20 30 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 39 78   0, 1, 0,  /* 9x
33302 20 2a 2f 0a 20 20 20 20 31 2c 20 30 2c 20 31 2c   */.    1, 0, 1,
33303 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
33304 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31  1, 1, 1, 0, 1, 1
33305 2c 20 31 2c 20 30 2c 20 20 2f 2a 20 41 78 20 2a  , 1, 0,  /* Ax *
33306 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30  /.    0, 0, 0, 0
33307 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
33308 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
33309 30 2c 20 30 2c 20 20 2f 2a 20 42 78 20 2a 2f 0a  0, 0,  /* Bx */.
3330a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
3330b 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3330c 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
3330d 20 31 2c 20 20 2f 2a 20 43 78 20 2a 2f 0a 20 20   1,  /* Cx */.  
3330e 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
3330f 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
33310 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
33311 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a 20 20 20 20  ,  /* Dx */.    
33312 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 0, 1, 1, 1, 1
33313 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
33314 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
33315 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20 20 31 2c   /* Ex */.    1,
33316 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
33317 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31  1, 1, 1, 1, 0, 1
33318 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f  , 1, 1, 1, 0,  /
33319 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69  * Fx */.};.#defi
3331a 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28  ne IdChar(C)  ((
3331b 28 63 3d 43 29 3e 3d 30 78 34 32 20 26 26 20 73  (c=C)>=0x42 && s
3331c 71 6c 69 74 65 33 49 73 45 62 63 64 69 63 49 64  qlite3IsEbcdicId
3331d 43 68 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23  Char[c-0x40])).#
3331e 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65  endif.../*.** Re
3331f 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20  turn the length 
33320 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61  of the token tha
33321 74 20 62 65 67 69 6e 73 20 61 74 20 7a 5b 30 5d  t begins at z[0]
33322 2e 20 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20  . .** Store the 
33323 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 2a 74  token type in *t
33324 6f 6b 65 6e 54 79 70 65 20 62 65 66 6f 72 65 20  okenType before 
33325 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51  returning..*/.SQ
33326 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
33327 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
33328 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
33329 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f  char *z, int *to
3332a 6b 65 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74 20  kenType){.  int 
3332b 69 2c 20 63 3b 0a 20 20 73 77 69 74 63 68 28 20  i, c;.  switch( 
3332c 2a 7a 20 29 7b 0a 20 20 20 20 63 61 73 65 20 27  *z ){.    case '
3332d 20 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20 63   ': case '\t': c
3332e 61 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20 27  ase '\n': case '
3332f 5c 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a 20  \f': case '\r': 
33330 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
33331 28 20 7a 5b 30 5d 3d 3d 27 20 27 20 29 3b 0a 20  ( z[0]==' ' );. 
33332 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
33333 5b 30 5d 3d 3d 27 5c 74 27 20 29 3b 0a 20 20 20  [0]=='\t' );.   
33334 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
33335 5d 3d 3d 27 5c 6e 27 20 29 3b 0a 20 20 20 20 20  ]=='\n' );.     
33336 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
33337 3d 27 5c 66 27 20 29 3b 0a 20 20 20 20 20 20 74  ='\f' );.      t
33338 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
33339 5c 72 27 20 29 3b 0a 20 20 20 20 20 20 66 6f 72  \r' );.      for
3333a 28 69 3d 31 3b 20 73 71 6c 69 74 65 33 49 73 73  (i=1; sqlite3Iss
3333b 70 61 63 65 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  pace(z[i]); i++)
3333c 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  {}.      *tokenT
3333d 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a  ype = TK_SPACE;.
3333e 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
3333f 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
33340 2d 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  -': {.      if( 
33341 7a 5b 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20  z[1]=='-' ){.   
33342 20 20 20 20 20 66 6f 72 28 69 3d 32 3b 20 28 63       for(i=2; (c
33343 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20 63 21 3d  =z[i])!=0 && c!=
33344 27 5c 6e 27 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  '\n'; i++){}.   
33345 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
33346 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20  = TK_SPACE;.    
33347 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
33348 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 74 6f 6b      }.      *tok
33349 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4d 49 4e 55  enType = TK_MINU
3334a 53 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  S;.      return 
3334b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
3334c 65 20 27 28 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '(': {.      *
3334d 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4c  tokenType = TK_L
3334e 50 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  P;.      return 
3334f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
33350 65 20 27 29 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e ')': {.      *
33351 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52  tokenType = TK_R
33352 50 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  P;.      return 
33353 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
33354 65 20 27 3b 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e ';': {.      *
33355 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53  tokenType = TK_S
33356 45 4d 49 3b 0a 20 20 20 20 20 20 72 65 74 75 72  EMI;.      retur
33357 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
33358 61 73 65 20 27 2b 27 3a 20 7b 0a 20 20 20 20 20  ase '+': {.     
33359 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
3335a 5f 50 4c 55 53 3b 0a 20 20 20 20 20 20 72 65 74  _PLUS;.      ret
3335b 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
3335c 20 63 61 73 65 20 27 2a 27 3a 20 7b 0a 20 20 20   case '*': {.   
3335d 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
3335e 54 4b 5f 53 54 41 52 3b 0a 20 20 20 20 20 20 72  TK_STAR;.      r
3335f 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
33360 20 20 20 63 61 73 65 20 27 2f 27 3a 20 7b 0a 20     case '/': {. 
33361 20 20 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27       if( z[1]!='
33362 2a 27 20 7c 7c 20 7a 5b 32 5d 3d 3d 30 20 29 7b  *' || z[2]==0 ){
33363 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
33364 79 70 65 20 3d 20 54 4b 5f 53 4c 41 53 48 3b 0a  ype = TK_SLASH;.
33365 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
33366 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
33367 66 6f 72 28 69 3d 33 2c 20 63 3d 7a 5b 32 5d 3b  for(i=3, c=z[2];
33368 20 28 63 21 3d 27 2a 27 20 7c 7c 20 7a 5b 69 5d   (c!='*' || z[i]
33369 21 3d 27 2f 27 29 20 26 26 20 28 63 3d 7a 5b 69  !='/') && (c=z[i
3336a 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20  ])!=0; i++){}.  
3336b 20 20 20 20 69 66 28 20 63 20 29 20 69 2b 2b 3b      if( c ) i++;
3336c 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
3336d 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20  e = TK_SPACE;.  
3336e 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20      return i;.  
3336f 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 25 27    }.    case '%'
33370 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e  : {.      *token
33371 54 79 70 65 20 3d 20 54 4b 5f 52 45 4d 3b 0a 20  Type = TK_REM;. 
33372 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
33373 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3d     }.    case '=
33374 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
33375 6e 54 79 70 65 20 3d 20 54 4b 5f 45 51 3b 0a 20  nType = TK_EQ;. 
33376 20 20 20 20 20 72 65 74 75 72 6e 20 31 20 2b 20       return 1 + 
33377 28 7a 5b 31 5d 3d 3d 27 3d 27 29 3b 0a 20 20 20  (z[1]=='=');.   
33378 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3c 27 3a   }.    case '<':
33379 20 7b 0a 20 20 20 20 20 20 69 66 28 20 28 63 3d   {.      if( (c=
3337a 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b 0a 20 20  z[1])=='=' ){.  
3337b 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3337c 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20   = TK_LE;.      
3337d 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
3337e 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27    }else if( c=='
3337f 3e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74  >' ){.        *t
33380 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 4e 45  okenType = TK_NE
33381 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
33382 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20   2;.      }else 
33383 69 66 28 20 63 3d 3d 27 3c 27 20 29 7b 0a 20 20  if( c=='<' ){.  
33384 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
33385 20 3d 20 54 4b 5f 4c 53 48 49 46 54 3b 0a 20 20   = TK_LSHIFT;.  
33386 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
33387 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
33388 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
33389 3d 20 54 4b 5f 4c 54 3b 0a 20 20 20 20 20 20 20  = TK_LT;.       
3338a 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
3338b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73   }.    }.    cas
3338c 65 20 27 3e 27 3a 20 7b 0a 20 20 20 20 20 20 69  e '>': {.      i
3338d 66 28 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27  f( (c=z[1])=='='
3338e 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b   ){.        *tok
3338f 65 6e 54 79 70 65 20 3d 20 54 4b 5f 47 45 3b 0a  enType = TK_GE;.
33390 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
33391 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
33392 28 20 63 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20  ( c=='>' ){.    
33393 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
33394 20 54 4b 5f 52 53 48 49 46 54 3b 0a 20 20 20 20   TK_RSHIFT;.    
33395 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
33396 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
33397 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
33398 54 4b 5f 47 54 3b 0a 20 20 20 20 20 20 20 20 72  TK_GT;.        r
33399 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
3339a 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
3339b 27 21 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  '!': {.      if(
3339c 20 7a 5b 31 5d 21 3d 27 3d 27 20 29 7b 0a 20 20   z[1]!='=' ){.  
3339d 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
3339e 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20   = TK_ILLEGAL;. 
3339f 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
333a0 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
333a1 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
333a2 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20   = TK_NE;.      
333a3 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
333a4 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61    }.    }.    ca
333a5 73 65 20 27 7c 27 3a 20 7b 0a 20 20 20 20 20 20  se '|': {.      
333a6 69 66 28 20 7a 5b 31 5d 21 3d 27 7c 27 20 29 7b  if( z[1]!='|' ){
333a7 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
333a8 79 70 65 20 3d 20 54 4b 5f 42 49 54 4f 52 3b 0a  ype = TK_BITOR;.
333a9 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
333aa 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
333ab 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
333ac 65 20 3d 20 54 4b 5f 43 4f 4e 43 41 54 3b 0a 20  e = TK_CONCAT;. 
333ad 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b         return 2;
333ae 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
333af 20 20 20 63 61 73 65 20 27 2c 27 3a 20 7b 0a 20     case ',': {. 
333b0 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
333b1 3d 20 54 4b 5f 43 4f 4d 4d 41 3b 0a 20 20 20 20  = TK_COMMA;.    
333b2 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
333b3 7d 0a 20 20 20 20 63 61 73 65 20 27 26 27 3a 20  }.    case '&': 
333b4 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
333b5 70 65 20 3d 20 54 4b 5f 42 49 54 41 4e 44 3b 0a  pe = TK_BITAND;.
333b6 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
333b7 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
333b8 7e 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  ~': {.      *tok
333b9 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 4e  enType = TK_BITN
333ba 4f 54 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  OT;.      return
333bb 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
333bc 73 65 20 27 60 27 3a 0a 20 20 20 20 63 61 73 65  se '`':.    case
333bd 20 27 5c 27 27 3a 0a 20 20 20 20 63 61 73 65 20   '\'':.    case 
333be 27 22 27 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  '"': {.      int
333bf 20 64 65 6c 69 6d 20 3d 20 7a 5b 30 5d 3b 0a 20   delim = z[0];. 
333c0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 64       testcase( d
333c1 65 6c 69 6d 3d 3d 27 60 27 20 29 3b 0a 20 20 20  elim=='`' );.   
333c2 20 20 20 74 65 73 74 63 61 73 65 28 20 64 65 6c     testcase( del
333c3 69 6d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20  im=='\'' );.    
333c4 20 20 74 65 73 74 63 61 73 65 28 20 64 65 6c 69    testcase( deli
333c5 6d 3d 3d 27 22 27 20 29 3b 0a 20 20 20 20 20 20  m=='"' );.      
333c6 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d  for(i=1; (c=z[i]
333c7 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  )!=0; i++){.    
333c8 20 20 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d      if( c==delim
333c9 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
333ca 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20  ( z[i+1]==delim 
333cb 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
333cc 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  ++;.          }e
333cd 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
333ce 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
333cf 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
333d0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
333d1 63 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20  c=='\'' ){.     
333d2 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
333d3 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 20  TK_STRING;.     
333d4 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20     return i+1;. 
333d5 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63       }else if( c
333d6 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a  !=0 ){.        *
333d7 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
333d8 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  D;.        retur
333d9 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65 6c  n i+1;.      }el
333da 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  se{.        *tok
333db 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
333dc 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74  GAL;.        ret
333dd 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20  urn i;.      }. 
333de 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2e     }.    case '.
333df 27 3a 20 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ': {.#ifndef SQL
333e0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
333e1 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 69 66  G_POINT.      if
333e2 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69  ( !sqlite3Isdigi
333e3 74 28 7a 5b 31 5d 29 20 29 0a 23 65 6e 64 69 66  t(z[1]) ).#endif
333e4 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20  .      {.       
333e5 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
333e6 5f 44 4f 54 3b 0a 20 20 20 20 20 20 20 20 72 65  _DOT;.        re
333e7 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
333e8 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
333e9 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 69  next character i
333ea 73 20 61 20 64 69 67 69 74 2c 20 74 68 69 73 20  s a digit, this 
333eb 69 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f  is a floating po
333ec 69 6e 74 0a 20 20 20 20 20 20 2a 2a 20 6e 75 6d  int.      ** num
333ed 62 65 72 20 74 68 61 74 20 62 65 67 69 6e 73 20  ber that begins 
333ee 77 69 74 68 20 22 2e 22 2e 20 20 46 61 6c 6c 20  with ".".  Fall 
333ef 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 6e 65  thru into the ne
333f0 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 7d  xt case */.    }
333f1 0a 20 20 20 20 63 61 73 65 20 27 30 27 3a 20 63  .    case '0': c
333f2 61 73 65 20 27 31 27 3a 20 63 61 73 65 20 27 32  ase '1': case '2
333f3 27 3a 20 63 61 73 65 20 27 33 27 3a 20 63 61 73  ': case '3': cas
333f4 65 20 27 34 27 3a 0a 20 20 20 20 63 61 73 65 20  e '4':.    case 
333f5 27 35 27 3a 20 63 61 73 65 20 27 36 27 3a 20 63  '5': case '6': c
333f6 61 73 65 20 27 37 27 3a 20 63 61 73 65 20 27 38  ase '7': case '8
333f7 27 3a 20 63 61 73 65 20 27 39 27 3a 20 7b 0a 20  ': case '9': {. 
333f8 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
333f9 5b 30 5d 3d 3d 27 30 27 20 29 3b 20 20 74 65 73  [0]=='0' );  tes
333fa 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 31 27  tcase( z[0]=='1'
333fb 20 29 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a   );  testcase( z
333fc 5b 30 5d 3d 3d 27 32 27 20 29 3b 0a 20 20 20 20  [0]=='2' );.    
333fd 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
333fe 3d 3d 27 33 27 20 29 3b 20 20 74 65 73 74 63 61  =='3' );  testca
333ff 73 65 28 20 7a 5b 30 5d 3d 3d 27 34 27 20 29 3b  se( z[0]=='4' );
33400 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d    testcase( z[0]
33401 3d 3d 27 35 27 20 29 3b 0a 20 20 20 20 20 20 74  =='5' );.      t
33402 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
33403 36 27 20 29 3b 20 20 74 65 73 74 63 61 73 65 28  6' );  testcase(
33404 20 7a 5b 30 5d 3d 3d 27 37 27 20 29 3b 20 20 74   z[0]=='7' );  t
33405 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
33406 38 27 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  8' );.      test
33407 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 39 27 20  case( z[0]=='9' 
33408 29 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  );.      *tokenT
33409 79 70 65 20 3d 20 54 4b 5f 49 4e 54 45 47 45 52  ype = TK_INTEGER
3340a 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
3340b 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
3340c 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 23 69  z[i]); i++){}.#i
3340d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
3340e 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
3340f 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d  .      if( z[i]=
33410 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='.' ){.        
33411 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69  i++;.        whi
33412 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69 67  le( sqlite3Isdig
33413 69 74 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b  it(z[i]) ){ i++;
33414 20 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65   }.        *toke
33415 6e 54 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54  nType = TK_FLOAT
33416 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
33417 69 66 28 20 28 7a 5b 69 5d 3d 3d 27 65 27 20 7c  if( (z[i]=='e' |
33418 7c 20 7a 5b 69 5d 3d 3d 27 45 27 29 20 26 26 0a  | z[i]=='E') &&.
33419 20 20 20 20 20 20 20 20 20 20 20 28 20 73 71 6c             ( sql
3341a 69 74 65 33 49 73 64 69 67 69 74 28 7a 5b 69 2b  ite3Isdigit(z[i+
3341b 31 5d 29 20 0a 20 20 20 20 20 20 20 20 20 20 20  1]) .           
3341c 20 7c 7c 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27 2b   || ((z[i+1]=='+
3341d 27 20 7c 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d 27  ' || z[i+1]=='-'
3341e 29 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69  ) && sqlite3Isdi
3341f 67 69 74 28 7a 5b 69 2b 32 5d 29 29 0a 20 20 20  git(z[i+2])).   
33420 20 20 20 20 20 20 20 20 29 0a 20 20 20 20 20 20          ).      
33421 29 7b 0a 20 20 20 20 20 20 20 20 69 20 2b 3d 20  ){.        i += 
33422 32 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  2;.        while
33423 28 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ( sqlite3Isdigit
33424 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b 3b 20 7d  (z[i]) ){ i++; }
33425 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
33426 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41 54 3b 0a  ype = TK_FLOAT;.
33427 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
33428 20 20 20 20 20 77 68 69 6c 65 28 20 49 64 43 68       while( IdCh
33429 61 72 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20  ar(z[i]) ){.    
3342a 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
3342b 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
3342c 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
3342d 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  }.      return i
3342e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
3342f 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f   '[': {.      fo
33430 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63  r(i=1, c=z[0]; c
33431 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d  !=']' && (c=z[i]
33432 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20  )!=0; i++){}.   
33433 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
33434 63 3d 3d 27 5d 27 20 3f 20 54 4b 5f 49 44 20 3a  c==']' ? TK_ID :
33435 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20   TK_ILLEGAL;.   
33436 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20     return i;.   
33437 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3f 27 3a   }.    case '?':
33438 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
33439 79 70 65 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c  ype = TK_VARIABL
3343a 45 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31  E;.      for(i=1
3343b 3b 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  ; sqlite3Isdigit
3343c 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
3343d 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
3343e 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 23     }.    case '#
3343f 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  ': {.      for(i
33440 3d 31 3b 20 73 71 6c 69 74 65 33 49 73 64 69 67  =1; sqlite3Isdig
33441 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d  it(z[i]); i++){}
33442 0a 20 20 20 20 20 20 69 66 28 20 69 3e 31 20 29  .      if( i>1 )
33443 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  {.        /* Par
33444 61 6d 65 74 65 72 73 20 6f 66 20 74 68 65 20 66  ameters of the f
33445 6f 72 6d 20 23 4e 4e 4e 20 28 77 68 65 72 65 20  orm #NNN (where 
33446 4e 4e 4e 20 69 73 20 61 20 6e 75 6d 62 65 72 29  NNN is a number)
33447 20 61 72 65 20 75 73 65 64 0a 20 20 20 20 20 20   are used.      
33448 20 20 2a 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 20    ** internally 
33449 62 79 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64  by sqlite3Nested
3344a 50 61 72 73 65 2e 20 20 2a 2f 0a 20 20 20 20 20  Parse.  */.     
3344b 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
3344c 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20  TK_REGISTER;.   
3344d 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
3344e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
3344f 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74  Fall through int
33450 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20  o the next case 
33451 69 66 20 74 68 65 20 27 23 27 20 69 73 20 6e 6f  if the '#' is no
33452 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79 0a 20 20  t followed by.  
33453 20 20 20 20 2a 2a 20 61 20 64 69 67 69 74 2e 20      ** a digit. 
33454 54 72 79 20 74 6f 20 6d 61 74 63 68 20 23 41 41  Try to match #AA
33455 41 41 20 77 68 65 72 65 20 41 41 41 41 20 69 73  AA where AAAA is
33456 20 61 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d   a parameter nam
33457 65 2e 20 2a 2f 0a 20 20 20 20 7d 0a 23 69 66 6e  e. */.    }.#ifn
33458 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
33459 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a 20 20 20  TCL_VARIABLE.   
3345a 20 63 61 73 65 20 27 24 27 3a 0a 23 65 6e 64 69   case '$':.#endi
3345b 66 0a 20 20 20 20 63 61 73 65 20 27 40 27 3a 20  f.    case '@': 
3345c 20 2f 2a 20 46 6f 72 20 63 6f 6d 70 61 74 69 62   /* For compatib
3345d 69 6c 69 74 79 20 77 69 74 68 20 4d 53 20 53 51  ility with MS SQ
3345e 4c 20 53 65 72 76 65 72 20 2a 2f 0a 20 20 20 20  L Server */.    
3345f 63 61 73 65 20 27 3a 27 3a 20 7b 0a 20 20 20 20  case ':': {.    
33460 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 20    int n = 0;.   
33461 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
33462 5d 3d 3d 27 24 27 20 29 3b 20 20 74 65 73 74 63  ]=='$' );  testc
33463 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 40 27 20 29  ase( z[0]=='@' )
33464 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30  ;  testcase( z[0
33465 5d 3d 3d 27 3a 27 20 29 3b 0a 20 20 20 20 20 20  ]==':' );.      
33466 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
33467 56 41 52 49 41 42 4c 45 3b 0a 20 20 20 20 20 20  VARIABLE;.      
33468 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d  for(i=1; (c=z[i]
33469 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  )!=0; i++){.    
3346a 20 20 20 20 69 66 28 20 49 64 43 68 61 72 28 63      if( IdChar(c
3346b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e  ) ){.          n
3346c 2b 2b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ++;.#ifndef SQLI
3346d 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49  TE_OMIT_TCL_VARI
3346e 41 42 4c 45 0a 20 20 20 20 20 20 20 20 7d 65 6c  ABLE.        }el
3346f 73 65 20 69 66 28 20 63 3d 3d 27 28 27 20 26 26  se if( c=='(' &&
33470 20 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20   n>0 ){.        
33471 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 20 20    do{.          
33472 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
33473 20 7d 77 68 69 6c 65 28 20 28 63 3d 7a 5b 69 5d   }while( (c=z[i]
33474 29 21 3d 30 20 26 26 20 21 73 71 6c 69 74 65 33  )!=0 && !sqlite3
33475 49 73 73 70 61 63 65 28 63 29 20 26 26 20 63 21  Isspace(c) && c!
33476 3d 27 29 27 20 29 3b 0a 20 20 20 20 20 20 20 20  =')' );.        
33477 20 20 69 66 28 20 63 3d 3d 27 29 27 20 29 7b 0a    if( c==')' ){.
33478 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b              i++;
33479 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
3347a 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 74  {.            *t
3347b 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c  okenType = TK_IL
3347c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 20  LEGAL;.         
3347d 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72 65   }.          bre
3347e 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ak;.        }els
3347f 65 20 69 66 28 20 63 3d 3d 27 3a 27 20 26 26 20  e if( c==':' && 
33480 7a 5b 69 2b 31 5d 3d 3d 27 3a 27 20 29 7b 0a 20  z[i+1]==':' ){. 
33481 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 23 65           i++;.#e
33482 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7d 65 6c  ndif.        }el
33483 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 62 72  se{.          br
33484 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
33485 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
33486 20 6e 3d 3d 30 20 29 20 2a 74 6f 6b 65 6e 54 79   n==0 ) *tokenTy
33487 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
33488 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
33489 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
3348a 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
3348b 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65  LITERAL.    case
3348c 20 27 78 27 3a 20 63 61 73 65 20 27 58 27 3a 20   'x': case 'X': 
3348d 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
3348e 28 20 7a 5b 30 5d 3d 3d 27 78 27 20 29 3b 20 74  ( z[0]=='x' ); t
3348f 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
33490 58 27 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  X' );.      if( 
33491 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20  z[1]=='\'' ){.  
33492 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
33493 20 3d 20 54 4b 5f 42 4c 4f 42 3b 0a 20 20 20 20   = TK_BLOB;.    
33494 20 20 20 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d      for(i=2; (c=
33495 7a 5b 69 5d 29 21 3d 30 20 26 26 20 63 21 3d 27  z[i])!=0 && c!='
33496 5c 27 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  \''; i++){.     
33497 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65       if( !sqlite
33498 33 49 73 78 64 69 67 69 74 28 63 29 20 29 7b 0a  3Isxdigit(c) ){.
33499 20 20 20 20 20 20 20 20 20 20 20 20 2a 74 6f 6b              *tok
3349a 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
3349b 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  GAL;.          }
3349c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3349d 20 20 20 69 66 28 20 69 25 32 20 7c 7c 20 21 63     if( i%2 || !c
3349e 20 29 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20   ) *tokenType = 
3349f 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20  TK_ILLEGAL;.    
334a0 20 20 20 20 69 66 28 20 63 20 29 20 69 2b 2b 3b      if( c ) i++;
334a1 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
334a2 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  i;.      }.     
334a3 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20 66 61   /* Otherwise fa
334a4 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
334a5 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20  e next case */. 
334a6 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
334a7 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
334a8 20 69 66 28 20 21 49 64 43 68 61 72 28 2a 7a 29   if( !IdChar(*z)
334a9 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61   ){.        brea
334aa 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
334ab 20 66 6f 72 28 69 3d 31 3b 20 49 64 43 68 61 72   for(i=1; IdChar
334ac 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
334ad 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
334ae 3d 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 28 63  = keywordCode((c
334af 68 61 72 2a 29 7a 2c 20 69 29 3b 0a 20 20 20 20  har*)z, i);.    
334b0 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
334b1 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b 65 6e 54 79  }.  }.  *tokenTy
334b2 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
334b3 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
334b4 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68 65 20 70 61  /*.** Run the pa
334b5 72 73 65 72 20 6f 6e 20 74 68 65 20 67 69 76 65  rser on the give
334b6 6e 20 53 51 4c 20 73 74 72 69 6e 67 2e 20 20 54  n SQL string.  T
334b7 68 65 20 70 61 72 73 65 72 20 73 74 72 75 63 74  he parser struct
334b8 75 72 65 20 69 73 0a 2a 2a 20 70 61 73 73 65 64  ure is.** passed
334b9 20 69 6e 2e 20 20 41 6e 20 53 51 4c 49 54 45 5f   in.  An SQLITE_
334ba 20 73 74 61 74 75 73 20 63 6f 64 65 20 69 73 20   status code is 
334bb 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 61 6e  returned.  If an
334bc 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a 2a   error occurs.**
334bd 20 74 68 65 6e 20 61 6e 20 61 6e 64 20 61 74 74   then an and att
334be 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20  empt is made to 
334bf 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20 6d  write an error m
334c0 65 73 73 61 67 65 20 69 6e 74 6f 20 0a 2a 2a 20  essage into .** 
334c1 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20  memory obtained 
334c2 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
334c3 6c 6f 63 28 29 20 61 6e 64 20 74 6f 20 6d 61 6b  loc() and to mak
334c4 65 20 2a 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e  e *pzErrMsg poin
334c5 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 72 72  t to that.** err
334c6 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 53  or message..*/.S
334c7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
334c8 74 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73  t sqlite3RunPars
334c9 65 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  er(Parse *pParse
334ca 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53  , const char *zS
334cb 71 6c 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  ql, char **pzErr
334cc 4d 73 67 29 7b 0a 20 20 69 6e 74 20 6e 45 72 72  Msg){.  int nErr
334cd 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
334ce 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
334cf 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f  r of errors enco
334d0 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e 74  untered */.  int
334d1 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
334d2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
334d3 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
334d4 20 20 76 6f 69 64 20 2a 70 45 6e 67 69 6e 65 3b    void *pEngine;
334d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
334d6 20 20 2f 2a 20 54 68 65 20 4c 45 4d 4f 4e 2d 67    /* The LEMON-g
334d7 65 6e 65 72 61 74 65 64 20 4c 41 4c 52 28 31 29  enerated LALR(1)
334d8 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69 6e 74   parser */.  int
334d9 20 74 6f 6b 65 6e 54 79 70 65 3b 20 20 20 20 20   tokenType;     
334da 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
334db 74 79 70 65 20 6f 66 20 74 68 65 20 6e 65 78 74  type of the next
334dc 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
334dd 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20  lastTokenParsed 
334de 3d 20 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20 74  = -1;       /* t
334df 79 70 65 20 6f 66 20 74 68 65 20 70 72 65 76 69  ype of the previ
334e0 6f 75 73 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 75  ous token */.  u
334e1 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64  8 enableLookasid
334e2 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
334e3 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f 66  * Saved value of
334e4 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
334e5 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 73 71 6c  Enabled */.  sql
334e6 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
334e7 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20  e->db;       /* 
334e8 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
334e9 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  nection */.  int
334ea 20 6d 78 53 71 6c 4c 65 6e 3b 20 20 20 20 20 20   mxSqlLen;      
334eb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
334ec 4d 61 78 20 6c 65 6e 67 74 68 20 6f 66 20 61 6e  Max length of an
334ed 20 53 51 4c 20 73 74 72 69 6e 67 20 2a 2f 0a 0a   SQL string */..
334ee 0a 20 20 6d 78 53 71 6c 4c 65 6e 20 3d 20 64 62  .  mxSqlLen = db
334ef 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
334f0 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54 48  LIMIT_SQL_LENGTH
334f1 5d 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 63 74  ];.  if( db->act
334f2 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b  iveVdbeCnt==0 ){
334f3 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e  .    db->u1.isIn
334f4 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b 0a 20  terrupted = 0;. 
334f5 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e 72 63 20   }.  pParse->rc 
334f6 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70  = SQLITE_OK;.  p
334f7 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 7a  Parse->zTail = z
334f8 53 71 6c 3b 0a 20 20 69 20 3d 20 30 3b 0a 20 20  Sql;.  i = 0;.  
334f9 61 73 73 65 72 74 28 20 70 7a 45 72 72 4d 73 67  assert( pzErrMsg
334fa 21 3d 30 20 29 3b 0a 20 20 70 45 6e 67 69 6e 65  !=0 );.  pEngine
334fb 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 72   = sqlite3Parser
334fc 41 6c 6c 6f 63 28 28 76 6f 69 64 2a 28 2a 29 28  Alloc((void*(*)(
334fd 73 69 7a 65 5f 74 29 29 73 71 6c 69 74 65 33 4d  size_t))sqlite3M
334fe 61 6c 6c 6f 63 29 3b 0a 20 20 69 66 28 20 70 45  alloc);.  if( pE
334ff 6e 67 69 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20  ngine==0 ){.    
33500 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
33501 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
33502 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
33503 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50 61   }.  assert( pPa
33504 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d  rse->pNewTable==
33505 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
33506 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
33507 65 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  er==0 );.  asser
33508 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 3d  t( pParse->nVar=
33509 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
3350a 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
3350b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
3350c 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70   pParse->nVarExp
3350d 72 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 61  rAlloc==0 );.  a
3350e 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 61  ssert( pParse->a
3350f 70 56 61 72 45 78 70 72 3d 3d 30 20 29 3b 0a 20  pVarExpr==0 );. 
33510 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65   enableLookaside
33511 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
33512 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20 69 66 28  .bEnabled;.  if(
33513 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70   db->lookaside.p
33514 53 74 61 72 74 20 29 20 64 62 2d 3e 6c 6f 6f 6b  Start ) db->look
33515 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d  aside.bEnabled =
33516 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 21 64 62   1;.  while( !db
33517 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
33518 26 20 7a 53 71 6c 5b 69 5d 21 3d 30 20 29 7b 0a  & zSql[i]!=0 ){.
33519 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30      assert( i>=0
3351a 20 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   );.    pParse->
3351b 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 20 3d 20 26  sLastToken.z = &
3351c 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 70 50 61  zSql[i];.    pPa
3351d 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e  rse->sLastToken.
3351e 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f  n = sqlite3GetTo
3351f 6b 65 6e 28 28 75 6e 73 69 67 6e 65 64 20 63 68  ken((unsigned ch
33520 61 72 2a 29 26 7a 53 71 6c 5b 69 5d 2c 26 74 6f  ar*)&zSql[i],&to
33521 6b 65 6e 54 79 70 65 29 3b 0a 20 20 20 20 69 20  kenType);.    i 
33522 2b 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74  += pParse->sLast
33523 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 69 66 28  Token.n;.    if(
33524 20 69 3e 6d 78 53 71 6c 4c 65 6e 20 29 7b 0a 20   i>mxSqlLen ){. 
33525 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20       pParse->rc 
33526 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b  = SQLITE_TOOBIG;
33527 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
33528 20 20 7d 0a 20 20 20 20 73 77 69 74 63 68 28 20    }.    switch( 
33529 74 6f 6b 65 6e 54 79 70 65 20 29 7b 0a 20 20 20  tokenType ){.   
3352a 20 20 20 63 61 73 65 20 54 4b 5f 53 50 41 43 45     case TK_SPACE
3352b 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  : {.        if( 
3352c 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75  db->u1.isInterru
3352d 70 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  pted ){.        
3352e 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
3352f 67 28 70 50 61 72 73 65 2c 20 22 69 6e 74 65 72  g(pParse, "inter
33530 72 75 70 74 22 29 3b 0a 20 20 20 20 20 20 20 20  rupt");.        
33531 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
33532 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 3b  QLITE_INTERRUPT;
33533 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
33534 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20 20  abort_parse;.   
33535 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
33536 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
33537 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4c 4c 45      case TK_ILLE
33538 47 41 4c 3a 20 7b 0a 20 20 20 20 20 20 20 20 73  GAL: {.        s
33539 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
3353a 20 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20   *pzErrMsg);.   
3353b 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
3353c 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
3353d 64 62 2c 20 22 75 6e 72 65 63 6f 67 6e 69 7a 65  db, "unrecognize
3353e 64 20 74 6f 6b 65 6e 3a 20 5c 22 25 54 5c 22 22  d token: \"%T\""
3353f 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
33540 20 20 20 20 20 20 20 20 20 20 26 70 50 61 72 73            &pPars
33541 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 29 3b 0a  e->sLastToken);.
33542 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a          nErr++;.
33543 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
33544 72 74 5f 70 61 72 73 65 3b 0a 20 20 20 20 20 20  rt_parse;.      
33545 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f  }.      case TK_
33546 53 45 4d 49 3a 20 7b 0a 20 20 20 20 20 20 20 20  SEMI: {.        
33547 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20  pParse->zTail = 
33548 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 20 20  &zSql[i];.      
33549 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 69    /* Fall thru i
3354a 6e 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20  nto the default 
3354b 63 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a  case */.      }.
3354c 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
3354d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3354e 50 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20  Parser(pEngine, 
3354f 74 6f 6b 65 6e 54 79 70 65 2c 20 70 50 61 72 73  tokenType, pPars
33550 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70  e->sLastToken, p
33551 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
33552 6c 61 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 20  lastTokenParsed 
33553 3d 20 74 6f 6b 65 6e 54 79 70 65 3b 0a 20 20 20  = tokenType;.   
33554 20 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d       if( pParse-
33555 3e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  >rc!=SQLITE_OK )
33556 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  {.          goto
33557 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20   abort_parse;.  
33558 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
33559 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
3355a 20 20 20 7d 0a 20 20 7d 0a 61 62 6f 72 74 5f 70     }.  }.abort_p
3355b 61 72 73 65 3a 0a 20 20 69 66 28 20 7a 53 71 6c  arse:.  if( zSql
3355c 5b 69 5d 3d 3d 30 20 26 26 20 6e 45 72 72 3d 3d  [i]==0 && nErr==
3355d 30 20 26 26 20 70 50 61 72 73 65 2d 3e 72 63 3d  0 && pParse->rc=
3355e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3355f 20 20 69 66 28 20 6c 61 73 74 54 6f 6b 65 6e 50    if( lastTokenP
33560 61 72 73 65 64 21 3d 54 4b 5f 53 45 4d 49 20 29  arsed!=TK_SEMI )
33561 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  {.      sqlite3P
33562 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 54  arser(pEngine, T
33563 4b 5f 53 45 4d 49 2c 20 70 50 61 72 73 65 2d 3e  K_SEMI, pParse->
33564 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61 72  sLastToken, pPar
33565 73 65 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  se);.      pPars
33566 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c  e->zTail = &zSql
33567 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  [i];.    }.    s
33568 71 6c 69 74 65 33 50 61 72 73 65 72 28 70 45 6e  qlite3Parser(pEn
33569 67 69 6e 65 2c 20 30 2c 20 70 50 61 72 73 65 2d  gine, 0, pParse-
3356a 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20 70 50 61  >sLastToken, pPa
3356b 72 73 65 29 3b 0a 20 20 7d 0a 23 69 66 64 65 66  rse);.  }.#ifdef
3356c 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b   YYTRACKMAXSTACK
3356d 44 45 50 54 48 0a 20 20 73 71 6c 69 74 65 33 53  DEPTH.  sqlite3S
3356e 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f  tatusSet(SQLITE_
3356f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53 54  STATUS_PARSER_ST
33570 41 43 4b 2c 0a 20 20 20 20 20 20 73 71 6c 69 74  ACK,.      sqlit
33571 65 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61  e3ParserStackPea
33572 6b 28 70 45 6e 67 69 6e 65 29 0a 20 20 29 3b 0a  k(pEngine).  );.
33573 23 65 6e 64 69 66 20 2f 2a 20 59 59 44 45 42 55  #endif /* YYDEBU
33574 47 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61  G */.  sqlite3Pa
33575 72 73 65 72 46 72 65 65 28 70 45 6e 67 69 6e 65  rserFree(pEngine
33576 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b  , sqlite3_free);
33577 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  .  db->lookaside
33578 2e 62 45 6e 61 62 6c 65 64 20 3d 20 65 6e 61 62  .bEnabled = enab
33579 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 0a 20 20 69  leLookaside;.  i
3357a 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
3357b 6c 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73  led ){.    pPars
3357c 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  e->rc = SQLITE_N
3357d 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OMEM;.  }.  if( 
3357e 70 50 61 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49  pParse->rc!=SQLI
3357f 54 45 5f 4f 4b 20 26 26 20 70 50 61 72 73 65 2d  TE_OK && pParse-
33580 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45  >rc!=SQLITE_DONE
33581 20 26 26 20 70 50 61 72 73 65 2d 3e 7a 45 72 72   && pParse->zErr
33582 4d 73 67 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  Msg==0 ){.    sq
33583 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
33584 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c  pParse->zErrMsg,
33585 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
33586 65 33 45 72 72 53 74 72 28 70 50 61 72 73 65 2d  e3ErrStr(pParse-
33587 3e 72 63 29 29 3b 0a 20 20 7d 0a 20 20 61 73 73  >rc));.  }.  ass
33588 65 72 74 28 20 70 7a 45 72 72 4d 73 67 21 3d 30  ert( pzErrMsg!=0
33589 20 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65   );.  if( pParse
3358a 2d 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20  ->zErrMsg ){.   
3358b 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 70 50 61   *pzErrMsg = pPa
3358c 72 73 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  rse->zErrMsg;.  
3358d 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73    pParse->zErrMs
3358e 67 20 3d 20 30 3b 0a 20 20 20 20 6e 45 72 72 2b  g = 0;.    nErr+
3358f 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  +;.  }.  if( pPa
33590 72 73 65 2d 3e 70 56 64 62 65 20 26 26 20 70 50  rse->pVdbe && pP
33591 61 72 73 65 2d 3e 6e 45 72 72 3e 30 20 26 26 20  arse->nErr>0 && 
33592 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
33593 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
33594 56 64 62 65 44 65 6c 65 74 65 28 70 50 61 72 73  VdbeDelete(pPars
33595 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 70  e->pVdbe);.    p
33596 50 61 72 73 65 2d 3e 70 56 64 62 65 20 3d 20 30  Parse->pVdbe = 0
33597 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  ;.  }.#ifndef SQ
33598 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
33599 5f 43 41 43 48 45 0a 20 20 69 66 28 20 70 50 61  _CACHE.  if( pPa
3359a 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
3359b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  {.    sqlite3DbF
3359c 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e  ree(db, pParse->
3359d 61 54 61 62 6c 65 4c 6f 63 6b 29 3b 0a 20 20 20  aTableLock);.   
3359e 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c   pParse->aTableL
3359f 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  ock = 0;.    pPa
335a0 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 20  rse->nTableLock 
335a1 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  = 0;.  }.#endif.
335a2 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
335a3 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
335a4 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
335a5 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56  (db, pParse->apV
335a6 74 61 62 4c 6f 63 6b 29 3b 0a 23 65 6e 64 69 66  tabLock);.#endif
335a7 0a 0a 20 20 69 66 28 20 21 49 4e 5f 44 45 43 4c  ..  if( !IN_DECL
335a8 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20  ARE_VTAB ){.    
335a9 2f 2a 20 49 66 20 74 68 65 20 70 50 61 72 73 65  /* If the pParse
335aa 2d 3e 64 65 63 6c 61 72 65 56 74 61 62 20 66 6c  ->declareVtab fl
335ab 61 67 20 69 73 20 73 65 74 2c 20 64 6f 20 6e 6f  ag is set, do no
335ac 74 20 64 65 6c 65 74 65 20 61 6e 79 20 74 61 62  t delete any tab
335ad 6c 65 20 0a 20 20 20 20 2a 2a 20 73 74 72 75 63  le .    ** struc
335ae 74 75 72 65 20 62 75 69 6c 74 20 75 70 20 69 6e  ture built up in
335af 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
335b0 6c 65 2e 20 54 68 65 20 63 61 6c 6c 69 6e 67 20  le. The calling 
335b1 63 6f 64 65 20 28 73 65 65 20 76 74 61 62 2e 63  code (see vtab.c
335b2 29 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 74 61  ).    ** will ta
335b3 6b 65 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74  ke responsibilit
335b4 79 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74 68  y for freeing th
335b5 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
335b6 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  e..    */.    sq
335b7 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65  lite3DeleteTable
335b8 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62  (pParse->pNewTab
335b9 6c 65 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  le);.  }..  sqli
335ba 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
335bb 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 70 4e 65  (db, pParse->pNe
335bc 77 54 72 69 67 67 65 72 29 3b 0a 20 20 73 71 6c  wTrigger);.  sql
335bd 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
335be 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
335bf 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
335c0 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 61  ee(db, pParse->a
335c1 41 6c 69 61 73 29 3b 0a 20 20 77 68 69 6c 65 28  Alias);.  while(
335c2 20 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 20 29   pParse->pAinc )
335c3 7b 0a 20 20 20 20 41 75 74 6f 69 6e 63 49 6e 66  {.    AutoincInf
335c4 6f 20 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  o *p = pParse->p
335c5 41 69 6e 63 3b 0a 20 20 20 20 70 50 61 72 73 65  Ainc;.    pParse
335c6 2d 3e 70 41 69 6e 63 20 3d 20 70 2d 3e 70 4e 65  ->pAinc = p->pNe
335c7 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  xt;.    sqlite3D
335c8 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20  bFree(db, p);.  
335c9 7d 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72 73  }.  while( pPars
335ca 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 29 7b  e->pZombieTab ){
335cb 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 20 3d 20  .    Table *p = 
335cc 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54  pParse->pZombieT
335cd 61 62 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  ab;.    pParse->
335ce 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20 70 2d 3e  pZombieTab = p->
335cf 70 4e 65 78 74 5a 6f 6d 62 69 65 3b 0a 20 20 20  pNextZombie;.   
335d0 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61   sqlite3DeleteTa
335d1 62 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66  ble(p);.  }.  if
335d2 28 20 6e 45 72 72 3e 30 20 26 26 20 70 50 61 72  ( nErr>0 && pPar
335d3 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  se->rc==SQLITE_O
335d4 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d  K ){.    pParse-
335d5 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  >rc = SQLITE_ERR
335d6 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  OR;.  }.  return
335d7 20 6e 45 72 72 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a   nErr;.}../*****
335d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
335d9 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a   tokenize.c ****
335da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335dc 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
335dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
335de 66 69 6c 65 20 63 6f 6d 70 6c 65 74 65 2e 63 20  file complete.c 
335df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335e1 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
335e2 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
335e3 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
335e4 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
335e5 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
335e6 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
335e7 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
335e8 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
335e9 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
335ea 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
335eb 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
335ec 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
335ed 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
335ee 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
335ef 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
335f0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
335f1 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
335f2 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
335f3 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
335f4 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
335f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
335f9 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72  .** An tokenizer
335fa 20 66 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54   for SQL.**.** T
335fb 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
335fc 73 20 43 20 63 6f 64 65 20 74 68 61 74 20 69 6d  s C code that im
335fd 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c  plements the sql
335fe 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20  ite3_complete() 
335ff 41 50 49 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 64  API..** This cod
33600 65 20 75 73 65 64 20 74 6f 20 62 65 20 70 61 72  e used to be par
33601 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 69 7a  t of the tokeniz
33602 65 72 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65  er.c source file
33603 2e 20 20 42 75 74 20 62 79 0a 2a 2a 20 73 65 70  .  But by.** sep
33604 61 72 61 74 69 6e 67 20 69 74 20 6f 75 74 2c 20  arating it out, 
33605 74 68 65 20 63 6f 64 65 20 77 69 6c 6c 20 62 65  the code will be
33606 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 6f   automatically o
33607 6d 69 74 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 73  mitted from.** s
33608 74 61 74 69 63 20 6c 69 6e 6b 73 20 74 68 61 74  tatic links that
33609 20 64 6f 20 6e 6f 74 20 75 73 65 20 69 74 2e 0a   do not use it..
3360a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
3360b 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a  E_OMIT_COMPLETE.
3360c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 64  ./*.** This is d
3360d 65 66 69 6e 65 64 20 69 6e 20 74 6f 6b 65 6e 69  efined in tokeni
3360e 7a 65 2e 63 2e 20 20 57 65 20 6a 75 73 74 20 68  ze.c.  We just h
3360f 61 76 65 20 74 6f 20 69 6d 70 6f 72 74 20 74 68  ave to import th
33610 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f  e definition..*/
33611 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
33612 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 23 69 66  AMALGAMATION.#if
33613 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49  def SQLITE_ASCII
33614 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61 72 28  .#define IdChar(
33615 43 29 20 20 28 28 73 71 6c 69 74 65 33 43 74 79  C)  ((sqlite3Cty
33616 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20  peMap[(unsigned 
33617 63 68 61 72 29 43 5d 26 30 78 34 36 29 21 3d 30  char)C]&0x46)!=0
33618 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ).#endif.#ifdef 
33619 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 53 51  SQLITE_EBCDIC.SQ
3361a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
3361b 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 49  st char sqlite3I
3361c 73 45 62 63 64 69 63 49 64 43 68 61 72 5b 5d 3b  sEbcdicIdChar[];
3361d 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61 72 28  .#define IdChar(
3361e 43 29 20 20 28 28 28 63 3d 43 29 3e 3d 30 78 34  C)  (((c=C)>=0x4
3361f 32 20 26 26 20 73 71 6c 69 74 65 33 49 73 45 62  2 && sqlite3IsEb
33620 63 64 69 63 49 64 43 68 61 72 5b 63 2d 30 78 34  cdicIdChar[c-0x4
33621 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23 65 6e 64  0])).#endif.#end
33622 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41  if /* SQLITE_AMA
33623 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 2f  LGAMATION */.../
33624 2a 0a 2a 2a 20 54 6f 6b 65 6e 20 74 79 70 65 73  *.** Token types
33625 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c   used by the sql
33626 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20  ite3_complete() 
33627 72 6f 75 74 69 6e 65 2e 20 20 53 65 65 20 74 68  routine.  See th
33628 65 20 68 65 61 64 65 72 0a 2a 2a 20 63 6f 6d 6d  e header.** comm
33629 65 6e 74 73 20 6f 6e 20 74 68 61 74 20 70 72 6f  ents on that pro
3362a 63 65 64 75 72 65 20 66 6f 72 20 61 64 64 69 74  cedure for addit
3362b 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
3362c 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 74 6b  n..*/.#define tk
3362d 53 45 4d 49 20 20 20 20 30 0a 23 64 65 66 69 6e  SEMI    0.#defin
3362e 65 20 74 6b 57 53 20 20 20 20 20 20 31 0a 23 64  e tkWS      1.#d
3362f 65 66 69 6e 65 20 74 6b 4f 54 48 45 52 20 20 20  efine tkOTHER   
33630 32 0a 23 64 65 66 69 6e 65 20 74 6b 45 58 50 4c  2.#define tkEXPL
33631 41 49 4e 20 33 0a 23 64 65 66 69 6e 65 20 74 6b  AIN 3.#define tk
33632 43 52 45 41 54 45 20 20 34 0a 23 64 65 66 69 6e  CREATE  4.#defin
33633 65 20 74 6b 54 45 4d 50 20 20 20 20 35 0a 23 64  e tkTEMP    5.#d
33634 65 66 69 6e 65 20 74 6b 54 52 49 47 47 45 52 20  efine tkTRIGGER 
33635 36 0a 23 64 65 66 69 6e 65 20 74 6b 45 4e 44 20  6.#define tkEND 
33636 20 20 20 20 37 0a 0a 2f 2a 0a 2a 2a 20 52 65 74      7../*.** Ret
33637 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
33638 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67  given SQL string
33639 20 65 6e 64 73 20 69 6e 20 61 20 73 65 6d 69 63   ends in a semic
3363a 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63  olon..**.** Spec
3363b 69 61 6c 20 68 61 6e 64 6c 69 6e 67 20 69 73 20  ial handling is 
3363c 72 65 71 75 69 72 65 20 66 6f 72 20 43 52 45 41  require for CREA
3363d 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65  TE TRIGGER state
3363e 6d 65 6e 74 73 2e 0a 2a 2a 20 57 68 65 6e 65 76  ments..** Whenev
3363f 65 72 20 74 68 65 20 43 52 45 41 54 45 20 54 52  er the CREATE TR
33640 49 47 47 45 52 20 6b 65 79 77 6f 72 64 73 20 61  IGGER keywords a
33641 72 65 20 73 65 65 6e 2c 20 74 68 65 20 73 74 61  re seen, the sta
33642 74 65 6d 65 6e 74 0a 2a 2a 20 6d 75 73 74 20 65  tement.** must e
33643 6e 64 20 77 69 74 68 20 22 3b 45 4e 44 3b 22 2e  nd with ";END;".
33644 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c  .**.** This impl
33645 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20  ementation uses 
33646 61 20 73 74 61 74 65 20 6d 61 63 68 69 6e 65 20  a state machine 
33647 77 69 74 68 20 37 20 73 74 61 74 65 73 3a 0a 2a  with 7 states:.*
33648 2a 0a 2a 2a 20 20 20 28 30 29 20 53 54 41 52 54  *.**   (0) START
33649 20 20 20 20 20 41 74 20 74 68 65 20 62 65 67 69       At the begi
3364a 6e 6e 69 6e 67 20 6f 72 20 65 6e 64 20 6f 66 20  nning or end of 
3364b 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
3364c 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a  .  This routine.
3364d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
3364e 20 20 20 72 65 74 75 72 6e 73 20 31 20 69 66 20     returns 1 if 
3364f 69 74 20 65 6e 64 73 20 69 6e 20 74 68 65 20 53  it ends in the S
33650 54 41 52 54 20 73 74 61 74 65 20 61 6e 64 20 30  TART state and 0
33651 20 69 66 20 69 74 20 65 6e 64 73 0a 2a 2a 20 20   if it ends.**  
33652 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
33653 6e 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74  n any other stat
33654 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 4e  e..**.**   (1) N
33655 4f 52 4d 41 4c 20 20 20 20 57 65 20 61 72 65 20  ORMAL    We are 
33656 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
33657 20 73 74 61 74 65 6d 65 6e 74 20 77 68 69 63 68   statement which
33658 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 69 6e   ends with a sin
33659 67 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  gle.**          
3365a 20 20 20 20 20 20 20 73 65 6d 69 63 6f 6c 6f 6e         semicolon
3365b 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20 45 58  ..**.**   (2) EX
3365c 50 4c 41 49 4e 20 20 20 54 68 65 20 6b 65 79 77  PLAIN   The keyw
3365d 6f 72 64 20 45 58 50 4c 41 49 4e 20 68 61 73 20  ord EXPLAIN has 
3365e 62 65 65 6e 20 73 65 65 6e 20 61 74 20 74 68 65  been seen at the
3365f 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 0a 2a   beginning of .*
33660 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
33661 20 20 61 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a    a statement..*
33662 2a 0a 2a 2a 20 20 20 28 33 29 20 43 52 45 41 54  *.**   (3) CREAT
33663 45 20 20 20 20 54 68 65 20 6b 65 79 77 6f 72 64  E    The keyword
33664 20 43 52 45 41 54 45 20 68 61 73 20 62 65 65 6e   CREATE has been
33665 20 73 65 65 6e 20 61 74 20 74 68 65 20 62 65 67   seen at the beg
33666 69 6e 6e 69 6e 67 20 6f 66 20 61 0a 2a 2a 20 20  inning of a.**  
33667 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
33668 74 61 74 65 6d 65 6e 74 2c 20 70 6f 73 73 69 62  tatement, possib
33669 6c 79 20 70 72 65 63 65 65 64 65 64 20 62 79 20  ly preceeded by 
3366a 45 58 50 4c 41 49 4e 20 61 6e 64 2f 6f 72 20 66  EXPLAIN and/or f
3366b 6f 6c 6c 6f 77 65 64 20 62 79 0a 2a 2a 20 20 20  ollowed by.**   
3366c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 45                TE
3366d 4d 50 20 6f 72 20 54 45 4d 50 4f 52 41 52 59 0a  MP or TEMPORARY.
3366e 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 54 52 49 47  **.**   (4) TRIG
3366f 47 45 52 20 20 20 57 65 20 61 72 65 20 69 6e 20  GER   We are in 
33670 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20  the middle of a 
33671 74 72 69 67 67 65 72 20 64 65 66 69 6e 69 74 69  trigger definiti
33672 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62 65 0a  on that must be.
33673 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
33674 20 20 20 65 6e 64 65 64 20 62 79 20 61 20 73 65     ended by a se
33675 6d 69 63 6f 6c 6f 6e 2c 20 74 68 65 20 6b 65 79  micolon, the key
33676 77 6f 72 64 20 45 4e 44 2c 20 61 6e 64 20 61 6e  word END, and an
33677 6f 74 68 65 72 20 73 65 6d 69 63 6f 6c 6f 6e 2e  other semicolon.
33678 0a 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 53 45 4d  .**.**   (5) SEM
33679 49 20 20 20 20 20 20 57 65 27 76 65 20 73 65 65  I      We've see
3367a 6e 20 74 68 65 20 66 69 72 73 74 20 73 65 6d 69  n the first semi
3367b 63 6f 6c 6f 6e 20 69 6e 20 74 68 65 20 22 3b 45  colon in the ";E
3367c 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75 72 73  ND;" that occurs
3367d 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   at.**          
3367e 20 20 20 20 20 20 20 74 68 65 20 65 6e 64 20 6f         the end o
3367f 66 20 61 20 74 72 69 67 67 65 72 20 64 65 66 69  f a trigger defi
33680 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  nition..**.**   
33681 28 36 29 20 45 4e 44 20 20 20 20 20 20 20 57 65  (6) END       We
33682 27 76 65 20 73 65 65 6e 20 74 68 65 20 22 3b 45  've seen the ";E
33683 4e 44 22 20 6f 66 20 74 68 65 20 22 3b 45 4e 44  ND" of the ";END
33684 3b 22 20 74 68 61 74 20 6f 63 63 75 72 73 20 61  ;" that occurs a
33685 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 20 20 20  t the end.**    
33686 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20               of 
33687 61 20 74 72 69 67 67 65 72 20 64 69 66 69 6e 69  a trigger difini
33688 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 72 61 6e  tion..**.** Tran
33689 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e 20  sitions between 
3368a 73 74 61 74 65 73 20 61 62 6f 76 65 20 61 72 65  states above are
3368b 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
3368c 6f 6b 65 6e 73 20 65 78 74 72 61 63 74 65 64 0a  okens extracted.
3368d 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75  ** from the inpu
3368e 74 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  t.  The followin
3368f 67 20 74 6f 6b 65 6e 73 20 61 72 65 20 73 69 67  g tokens are sig
33690 6e 69 66 69 63 61 6e 74 3a 0a 2a 2a 0a 2a 2a 20  nificant:.**.** 
33691 20 20 28 30 29 20 74 6b 53 45 4d 49 20 20 20 20    (0) tkSEMI    
33692 20 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a    A semicolon..*
33693 2a 20 20 20 28 31 29 20 74 6b 57 53 20 20 20 20  *   (1) tkWS    
33694 20 20 20 20 57 68 69 74 65 73 70 61 63 65 0a 2a      Whitespace.*
33695 2a 20 20 20 28 32 29 20 74 6b 4f 54 48 45 52 20  *   (2) tkOTHER 
33696 20 20 20 20 41 6e 79 20 6f 74 68 65 72 20 53 51      Any other SQ
33697 4c 20 74 6f 6b 65 6e 2e 0a 2a 2a 20 20 20 28 33  L token..**   (3
33698 29 20 74 6b 45 58 50 4c 41 49 4e 20 20 20 54 68  ) tkEXPLAIN   Th
33699 65 20 22 65 78 70 6c 61 69 6e 22 20 6b 65 79 77  e "explain" keyw
3369a 6f 72 64 2e 0a 2a 2a 20 20 20 28 34 29 20 74 6b  ord..**   (4) tk
3369b 43 52 45 41 54 45 20 20 20 20 54 68 65 20 22 63  CREATE    The "c
3369c 72 65 61 74 65 22 20 6b 65 79 77 6f 72 64 2e 0a  reate" keyword..
3369d 2a 2a 20 20 20 28 35 29 20 74 6b 54 45 4d 50 20  **   (5) tkTEMP 
3369e 20 20 20 20 20 54 68 65 20 22 74 65 6d 70 22 20       The "temp" 
3369f 6f 72 20 22 74 65 6d 70 6f 72 61 72 79 22 20 6b  or "temporary" k
336a0 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 36 29  eyword..**   (6)
336a1 20 74 6b 54 52 49 47 47 45 52 20 20 20 54 68 65   tkTRIGGER   The
336a2 20 22 74 72 69 67 67 65 72 22 20 6b 65 79 77 6f   "trigger" keywo
336a3 72 64 2e 0a 2a 2a 20 20 20 28 37 29 20 74 6b 45  rd..**   (7) tkE
336a4 4e 44 20 20 20 20 20 20 20 54 68 65 20 22 65 6e  ND       The "en
336a5 64 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 0a 2a  d" keyword..**.*
336a6 2a 20 57 68 69 74 65 73 70 61 63 65 20 6e 65 76  * Whitespace nev
336a7 65 72 20 63 61 75 73 65 73 20 61 20 73 74 61 74  er causes a stat
336a8 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 61 6e 64  e transition and
336a9 20 69 73 20 61 6c 77 61 79 73 20 69 67 6e 6f 72   is always ignor
336aa 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 65 20  ed..**.** If we 
336ab 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 53 51 4c  compile with SQL
336ac 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
336ad 2c 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 6d  , all of the com
336ae 70 75 74 61 74 69 6f 6e 20 6e 65 65 64 65 64 0a  putation needed.
336af 2a 2a 20 74 6f 20 72 65 63 6f 67 6e 69 7a 65 20  ** to recognize 
336b0 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74 72 69  the end of a tri
336b1 67 67 65 72 20 63 61 6e 20 62 65 20 6f 6d 69 74  gger can be omit
336b2 74 65 64 2e 20 20 41 6c 6c 20 77 65 20 68 61 76  ted.  All we hav
336b3 65 20 74 6f 20 64 6f 0a 2a 2a 20 69 73 20 6c 6f  e to do.** is lo
336b4 6f 6b 20 66 6f 72 20 61 20 73 65 6d 69 63 6f 6c  ok for a semicol
336b5 6f 6e 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70  on that is not p
336b6 61 72 74 20 6f 66 20 61 6e 20 73 74 72 69 6e 67  art of an string
336b7 20 6f 72 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a   or comment..*/.
336b8 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
336b9 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
336ba 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
336bb 29 7b 0a 20 20 75 38 20 73 74 61 74 65 20 3d 20  ){.  u8 state = 
336bc 30 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20  0;   /* Current 
336bd 73 74 61 74 65 2c 20 75 73 69 6e 67 20 6e 75 6d  state, using num
336be 62 65 72 73 20 64 65 66 69 6e 65 64 20 69 6e 20  bers defined in 
336bf 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 2a  header comment *
336c0 2f 0a 20 20 75 38 20 74 6f 6b 65 6e 3b 20 20 20  /.  u8 token;   
336c1 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20      /* Value of 
336c2 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 2a  the next token *
336c3 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
336c4 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
336c5 20 2f 2a 20 41 20 63 6f 6d 70 6c 65 78 20 73 74   /* A complex st
336c6 61 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e 65 20  atement machine 
336c7 75 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 74  used to detect t
336c8 68 65 20 65 6e 64 20 6f 66 20 61 20 43 52 45 41  he end of a CREA
336c9 54 45 20 54 52 49 47 47 45 52 0a 20 20 2a 2a 20  TE TRIGGER.  ** 
336ca 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 69 73  statement.  This
336cb 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 63   is the normal c
336cc 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74  ase..  */.  stat
336cd 69 63 20 63 6f 6e 73 74 20 75 38 20 74 72 61 6e  ic const u8 tran
336ce 73 5b 37 5d 5b 38 5d 20 3d 20 7b 0a 20 20 20 20  s[7][8] = {.    
336cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336d0 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20   /* Token:      
336d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
336d3 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
336d4 20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20 20 20    /* State:     
336d5 20 20 2a 2a 20 20 53 45 4d 49 20 20 57 53 20 20    **  SEMI  WS  
336d6 4f 54 48 45 52 20 45 58 50 4c 41 49 4e 20 20 43  OTHER EXPLAIN  C
336d7 52 45 41 54 45 20 20 54 45 4d 50 20 20 54 52 49  REATE  TEMP  TRI
336d8 47 47 45 52 20 20 45 4e 44 20 20 2a 2f 0a 20 20  GGER  END  */.  
336d9 20 20 20 2f 2a 20 30 20 20 20 53 54 41 52 54 3a     /* 0   START:
336da 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 30 2c 20   */ {    0,  0, 
336db 20 20 20 20 31 2c 20 20 20 20 20 20 32 2c 20 20      1,      2,  
336dc 20 20 20 20 33 2c 20 20 20 20 31 2c 20 20 20 20      3,    1,    
336dd 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20     1,   1,  },. 
336de 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c      /* 1  NORMAL
336df 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 31 2c  : */ {    0,  1,
336e0 20 20 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20       1,      1, 
336e1 20 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20       1,    1,   
336e2 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a      1,   1,  },.
336e3 20 20 20 20 20 2f 2a 20 32 20 45 58 50 4c 41 49       /* 2 EXPLAI
336e4 4e 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 32  N: */ {    0,  2
336e5 2c 20 20 20 20 20 32 2c 20 20 20 20 20 20 31 2c  ,     2,      1,
336e6 20 20 20 20 20 20 33 2c 20 20 20 20 31 2c 20 20        3,    1,  
336e7 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c       1,   1,  },
336e8 0a 20 20 20 20 20 2f 2a 20 33 20 20 43 52 45 41  .     /* 3  CREA
336e9 54 45 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20  TE: */ {    0,  
336ea 33 2c 20 20 20 20 20 31 2c 20 20 20 20 20 20 31  3,     1,      1
336eb 2c 20 20 20 20 20 20 31 2c 20 20 20 20 33 2c 20  ,      1,    3, 
336ec 20 20 20 20 20 20 34 2c 20 20 20 31 2c 20 20 7d        4,   1,  }
336ed 2c 0a 20 20 20 20 20 2f 2a 20 34 20 54 52 49 47  ,.     /* 4 TRIG
336ee 47 45 52 3a 20 2a 2f 20 7b 20 20 20 20 35 2c 20  GER: */ {    5, 
336ef 20 34 2c 20 20 20 20 20 34 2c 20 20 20 20 20 20   4,     4,      
336f0 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20 34 2c  4,      4,    4,
336f1 20 20 20 20 20 20 20 34 2c 20 20 20 34 2c 20 20         4,   4,  
336f2 7d 2c 0a 20 20 20 20 20 2f 2a 20 35 20 20 20 20  },.     /* 5    
336f3 53 45 4d 49 3a 20 2a 2f 20 7b 20 20 20 20 35 2c  SEMI: */ {    5,
336f4 20 20 35 2c 20 20 20 20 20 34 2c 20 20 20 20 20    5,     4,     
336f5 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20 34   4,      4,    4
336f6 2c 20 20 20 20 20 20 20 34 2c 20 20 20 36 2c 20  ,       4,   6, 
336f7 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 36 20 20 20   },.     /* 6   
336f8 20 20 45 4e 44 3a 20 2a 2f 20 7b 20 20 20 20 30    END: */ {    0
336f9 2c 20 20 36 2c 20 20 20 20 20 34 2c 20 20 20 20  ,  6,     4,    
336fa 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20    4,      4,    
336fb 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20 34 2c  4,       4,   4,
336fc 20 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6c 73 65 0a    },.  };.#else.
336fd 20 20 2f 2a 20 49 66 20 74 72 69 67 67 65 72 73    /* If triggers
336fe 20 61 72 65 20 6e 6f 74 20 73 75 70 70 6f 72 65   are not suppore
336ff 64 20 62 79 20 74 68 69 73 20 63 6f 6d 70 69 6c  d by this compil
33700 65 20 74 68 65 6e 20 74 68 65 20 73 74 61 74 65  e then the state
33701 6d 65 6e 74 20 6d 61 63 68 69 6e 65 0a 20 20 2a  ment machine.  *
33702 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65 63 74  * used to detect
33703 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 73 74   the end of a st
33704 61 74 65 6d 65 6e 74 20 69 73 20 6d 75 63 68 20  atement is much 
33705 73 69 6d 70 6c 69 65 72 0a 20 20 2a 2f 0a 20 20  simplier.  */.  
33706 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
33707 74 72 61 6e 73 5b 32 5d 5b 33 5d 20 3d 20 7b 0a  trans[2][3] = {.
33708 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33709 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20       /* Token:  
3370a 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
3370b 20 2f 2a 20 53 74 61 74 65 3a 20 20 20 20 20 20   /* State:      
3370c 20 2a 2a 20 20 53 45 4d 49 20 20 57 53 20 20 4f   **  SEMI  WS  O
3370d 54 48 45 52 20 2a 2f 0a 20 20 20 20 20 2f 2a 20  THER */.     /* 
3370e 30 20 20 20 53 54 41 52 54 3a 20 2a 2f 20 7b 20  0   START: */ { 
3370f 20 20 20 30 2c 20 20 30 2c 20 20 20 20 20 31 2c     0,  0,     1,
33710 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31 20 20 4e   },.     /* 1  N
33711 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 30  ORMAL: */ {    0
33712 2c 20 20 31 2c 20 20 20 20 20 31 2c 20 7d 2c 0a  ,  1,     1, },.
33713 20 20 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53    };.#endif /* S
33714 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
33715 45 52 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20  ER */..  while( 
33716 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 73 77 69  *zSql ){.    swi
33717 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20  tch( *zSql ){.  
33718 20 20 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 20      case ';': { 
33719 20 2f 2a 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 20   /* A semicolon 
3371a 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e  */.        token
3371b 20 3d 20 74 6b 53 45 4d 49 3b 0a 20 20 20 20 20   = tkSEMI;.     
3371c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3371d 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 20 27  }.      case ' '
3371e 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 72  :.      case '\r
3371f 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c  ':.      case '\
33720 74 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27  t':.      case '
33721 5c 6e 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20  \n':.      case 
33722 27 5c 66 27 3a 20 7b 20 20 2f 2a 20 57 68 69 74  '\f': {  /* Whit
33723 65 20 73 70 61 63 65 20 69 73 20 69 67 6e 6f 72  e space is ignor
33724 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f  ed */.        to
33725 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20  ken = tkWS;.    
33726 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
33727 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 2f   }.      case '/
33728 27 3a 20 7b 20 20 20 2f 2a 20 43 2d 73 74 79 6c  ': {   /* C-styl
33729 65 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 20 20  e comments */.  
3372a 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 31        if( zSql[1
3372b 5d 21 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20  ]!='*' ){.      
3372c 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54      token = tkOT
3372d 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 62  HER;.          b
3372e 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
3372f 20 20 20 20 20 20 20 20 7a 53 71 6c 20 2b 3d 20          zSql += 
33730 32 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  2;.        while
33731 28 20 7a 53 71 6c 5b 30 5d 20 26 26 20 28 7a 53  ( zSql[0] && (zS
33732 71 6c 5b 30 5d 21 3d 27 2a 27 20 7c 7c 20 7a 53  ql[0]!='*' || zS
33733 71 6c 5b 31 5d 21 3d 27 2f 27 29 20 29 7b 20 7a  ql[1]!='/') ){ z
33734 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  Sql++; }.       
33735 20 69 66 28 20 7a 53 71 6c 5b 30 5d 3d 3d 30 20   if( zSql[0]==0 
33736 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
33737 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20      zSql++;.    
33738 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53      token = tkWS
33739 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
3373a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
3373b 61 73 65 20 27 2d 27 3a 20 7b 20 20 20 2f 2a 20  ase '-': {   /* 
3373c 53 51 4c 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e  SQL-style commen
3373d 74 73 20 66 72 6f 6d 20 22 2d 2d 22 20 74 6f 20  ts from "--" to 
3373e 65 6e 64 20 6f 66 20 6c 69 6e 65 20 2a 2f 0a 20  end of line */. 
3373f 20 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b         if( zSql[
33740 31 5d 21 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20  1]!='-' ){.     
33741 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
33742 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20  THER;.          
33743 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
33744 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
33745 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d  *zSql && *zSql!=
33746 27 5c 6e 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20  '\n' ){ zSql++; 
33747 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a  }.        if( *z
33748 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Sql==0 ) return 
33749 73 74 61 74 65 3d 3d 30 3b 0a 20 20 20 20 20 20  state==0;.      
3374a 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a    token = tkWS;.
3374b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3374c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
3374d 65 20 27 5b 27 3a 20 7b 20 20 20 2f 2a 20 4d 69  e '[': {   /* Mi
3374e 63 72 6f 73 6f 66 74 2d 73 74 79 6c 65 20 69 64  crosoft-style id
3374f 65 6e 74 69 66 69 65 72 73 20 69 6e 20 5b 2e 2e  entifiers in [..
33750 2e 5d 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 53  .] */.        zS
33751 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68  ql++;.        wh
33752 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a  ile( *zSql && *z
33753 53 71 6c 21 3d 27 5d 27 20 29 7b 20 7a 53 71 6c  Sql!=']' ){ zSql
33754 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66  ++; }.        if
33755 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74  ( *zSql==0 ) ret
33756 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 74  urn 0;.        t
33757 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
33758 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
33759 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
3375a 65 20 27 60 27 3a 20 20 20 20 20 2f 2a 20 47 72  e '`':     /* Gr
3375b 61 76 65 2d 61 63 63 65 6e 74 20 71 75 6f 74 65  ave-accent quote
3375c 64 20 73 79 6d 62 6f 6c 73 20 75 73 65 64 20 62  d symbols used b
3375d 79 20 4d 79 53 51 4c 20 2a 2f 0a 20 20 20 20 20  y MySQL */.     
3375e 20 63 61 73 65 20 27 22 27 3a 20 20 20 20 20 2f   case '"':     /
3375f 2a 20 73 69 6e 67 6c 65 2d 20 61 6e 64 20 64 6f  * single- and do
33760 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69  uble-quoted stri
33761 6e 67 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ngs */.      cas
33762 65 20 27 5c 27 27 3a 20 7b 0a 20 20 20 20 20 20  e '\'': {.      
33763 20 20 69 6e 74 20 63 20 3d 20 2a 7a 53 71 6c 3b    int c = *zSql;
33764 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b  .        zSql++;
33765 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
33766 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d  *zSql && *zSql!=
33767 63 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20  c ){ zSql++; }. 
33768 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53 71 6c         if( *zSql
33769 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
3376a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
3376b 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20  tkOTHER;.       
3376c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
3376d 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
3376e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
3376f 42 43 44 49 43 0a 20 20 20 20 20 20 20 20 75 6e  BCDIC.        un
33770 73 69 67 6e 65 64 20 63 68 61 72 20 63 3b 0a 23  signed char c;.#
33771 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66  endif.        if
33772 28 20 49 64 43 68 61 72 28 28 75 38 29 2a 7a 53  ( IdChar((u8)*zS
33773 71 6c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ql) ){.         
33774 20 2f 2a 20 4b 65 79 77 6f 72 64 73 20 61 6e 64   /* Keywords and
33775 20 75 6e 71 75 6f 74 65 64 20 69 64 65 6e 74 69   unquoted identi
33776 66 69 65 72 73 20 2a 2f 0a 20 20 20 20 20 20 20  fiers */.       
33777 20 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20 20 20     int nId;.    
33778 20 20 20 20 20 20 66 6f 72 28 6e 49 64 3d 31 3b        for(nId=1;
33779 20 49 64 43 68 61 72 28 7a 53 71 6c 5b 6e 49 64   IdChar(zSql[nId
3377a 5d 29 3b 20 6e 49 64 2b 2b 29 7b 7d 0a 23 69 66  ]); nId++){}.#if
3377b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3377c 54 52 49 47 47 45 52 0a 20 20 20 20 20 20 20 20  TRIGGER.        
3377d 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
3377e 52 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20  R;.#else.       
3377f 20 20 20 73 77 69 74 63 68 28 20 2a 7a 53 71 6c     switch( *zSql
33780 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
33781 63 61 73 65 20 27 63 27 3a 20 63 61 73 65 20 27  case 'c': case '
33782 43 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  C': {.          
33783 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 36 20 26      if( nId==6 &
33784 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
33785 70 28 7a 53 71 6c 2c 20 22 63 72 65 61 74 65 22  p(zSql, "create"
33786 2c 20 36 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 6)==0 ){.     
33787 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
33788 20 3d 20 74 6b 43 52 45 41 54 45 3b 0a 20 20 20   = tkCREATE;.   
33789 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
3378a 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
3378b 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
3378c 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  R;.             
3378d 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
3378e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
3378f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
33790 20 20 63 61 73 65 20 27 74 27 3a 20 63 61 73 65    case 't': case
33791 20 27 54 27 3a 20 7b 0a 20 20 20 20 20 20 20 20   'T': {.        
33792 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 37        if( nId==7
33793 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
33794 43 6d 70 28 7a 53 71 6c 2c 20 22 74 72 69 67 67  Cmp(zSql, "trigg
33795 65 72 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20  er", 7)==0 ){.  
33796 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
33797 6b 65 6e 20 3d 20 74 6b 54 52 49 47 47 45 52 3b  ken = tkTRIGGER;
33798 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
33799 65 6c 73 65 20 69 66 28 20 6e 49 64 3d 3d 34 20  else if( nId==4 
3379a 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
3379b 6d 70 28 7a 53 71 6c 2c 20 22 74 65 6d 70 22 2c  mp(zSql, "temp",
3379c 20 34 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   4)==0 ){.      
3379d 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
3379e 3d 20 74 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20  = tkTEMP;.      
3379f 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
337a0 28 20 6e 49 64 3d 3d 39 20 26 26 20 73 71 6c 69  ( nId==9 && sqli
337a1 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c  te3StrNICmp(zSql
337a2 2c 20 22 74 65 6d 70 6f 72 61 72 79 22 2c 20 39  , "temporary", 9
337a3 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
337a4 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
337a5 74 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20 20 20  tkTEMP;.        
337a6 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
337a7 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
337a8 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
337a9 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
337aa 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
337ab 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  k;.            }
337ac 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73  .            cas
337ad 65 20 27 65 27 3a 20 20 63 61 73 65 20 27 45 27  e 'e':  case 'E'
337ae 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
337af 20 20 69 66 28 20 6e 49 64 3d 3d 33 20 26 26 20    if( nId==3 && 
337b0 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
337b1 7a 53 71 6c 2c 20 22 65 6e 64 22 2c 20 33 29 3d  zSql, "end", 3)=
337b2 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
337b3 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
337b4 45 4e 44 3b 0a 20 20 20 20 20 20 20 20 20 20 20  END;.           
337b5 20 20 20 7d 65 6c 73 65 0a 23 69 66 6e 64 65 66     }else.#ifndef
337b6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50   SQLITE_OMIT_EXP
337b7 4c 41 49 4e 0a 20 20 20 20 20 20 20 20 20 20 20  LAIN.           
337b8 20 20 20 69 66 28 20 6e 49 64 3d 3d 37 20 26 26     if( nId==7 &&
337b9 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70   sqlite3StrNICmp
337ba 28 7a 53 71 6c 2c 20 22 65 78 70 6c 61 69 6e 22  (zSql, "explain"
337bb 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 7)==0 ){.     
337bc 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
337bd 20 3d 20 74 6b 45 58 50 4c 41 49 4e 3b 0a 20 20   = tkEXPLAIN;.  
337be 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
337bf 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  e.#endif.       
337c0 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20         {.       
337c1 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d           token =
337c2 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20   tkOTHER;.      
337c3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
337c4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
337c5 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
337c6 20 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74           default
337c7 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
337c8 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45    token = tkOTHE
337c9 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  R;.             
337ca 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
337cb 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
337cc 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
337cd 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 20  TE_OMIT_TRIGGER 
337ce 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 7a 53 71  */.          zSq
337cf 6c 20 2b 3d 20 6e 49 64 2d 31 3b 0a 20 20 20 20  l += nId-1;.    
337d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
337d1 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74 6f 72       /* Operator
337d2 73 20 61 6e 64 20 73 70 65 63 69 61 6c 20 73 79  s and special sy
337d3 6d 62 6f 6c 73 20 2a 2f 0a 20 20 20 20 20 20 20  mbols */.       
337d4 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48     token = tkOTH
337d5 45 52 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ER;.        }.  
337d6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
337d7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
337d8 74 61 74 65 20 3d 20 74 72 61 6e 73 5b 73 74 61  tate = trans[sta
337d9 74 65 5d 5b 74 6f 6b 65 6e 5d 3b 0a 20 20 20 20  te][token];.    
337da 7a 53 71 6c 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65  zSql++;.  }.  re
337db 74 75 72 6e 20 73 74 61 74 65 3d 3d 30 3b 0a 7d  turn state==0;.}
337dc 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
337dd 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a  _OMIT_UTF16./*.*
337de 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
337df 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  s the same as th
337e0 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  e sqlite3_comple
337e1 74 65 28 29 20 72 6f 75 74 69 6e 65 20 64 65 73  te() routine des
337e2 63 72 69 62 65 64 0a 2a 2a 20 61 62 6f 76 65 2c  cribed.** above,
337e3 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
337e4 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 72 65   parameter is re
337e5 71 75 69 72 65 64 20 74 6f 20 62 65 20 55 54 46  quired to be UTF
337e6 2d 31 36 20 65 6e 63 6f 64 65 64 2c 20 6e 6f 74  -16 encoded, not
337e7 0a 2a 2a 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51  .** UTF-8..*/.SQ
337e8 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
337e9 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28  ite3_complete16(
337ea 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c  const void *zSql
337eb 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  ){.  sqlite3_val
337ec 75 65 20 2a 70 56 61 6c 3b 0a 20 20 63 68 61 72  ue *pVal;.  char
337ed 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 38 3b 0a 20   const *zSql8;. 
337ee 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
337ef 5f 4e 4f 4d 45 4d 3b 0a 0a 23 69 66 6e 64 65 66  _NOMEM;..#ifndef
337f0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
337f1 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71 6c  OINIT.  rc = sql
337f2 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
337f3 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
337f4 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a  turn rc;.#endif.
337f5 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33    pVal = sqlite3
337f6 56 61 6c 75 65 4e 65 77 28 30 29 3b 0a 20 20 73  ValueNew(0);.  s
337f7 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74  qlite3ValueSetSt
337f8 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 53 71 6c  r(pVal, -1, zSql
337f9 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  , SQLITE_UTF16NA
337fa 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53 54 41  TIVE, SQLITE_STA
337fb 54 49 43 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20  TIC);.  zSql8 = 
337fc 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
337fd 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54  (pVal, SQLITE_UT
337fe 46 38 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 38  F8);.  if( zSql8
337ff 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
33800 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 7a 53  ite3_complete(zS
33801 71 6c 38 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ql8);.  }else{. 
33802 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
33803 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  OMEM;.  }.  sqli
33804 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56 61  te3ValueFree(pVa
33805 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  l);.  return sql
33806 69 74 65 33 41 70 69 45 78 69 74 28 30 2c 20 72  ite3ApiExit(0, r
33807 63 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  c);.}.#endif /* 
33808 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
33809 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53  6 */.#endif /* S
3380a 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4c  QLITE_OMIT_COMPL
3380b 45 54 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ETE */../*******
3380c 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 63  ******* End of c
3380d 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a 2a 2a  omplete.c ******
3380e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3380f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33810 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
33811 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
33812 6c 65 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a  le main.c ******
33813 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33814 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33815 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
33816 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
33817 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
33818 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
33819 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
3381a 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
3381b 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
3381c 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
3381d 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
3381e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
3381f 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
33820 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
33821 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
33822 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
33823 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
33824 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
33825 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
33826 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
33827 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
33828 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
33829 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3382a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3382b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3382c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
3382d 2a 20 4d 61 69 6e 20 66 69 6c 65 20 66 6f 72 20  * Main file for 
3382e 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
3382f 72 79 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65  ry.  The routine
33830 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a  s in this file.*
33831 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  * implement the 
33832 70 72 6f 67 72 61 6d 6d 65 72 20 69 6e 74 65 72  programmer inter
33833 66 61 63 65 20 74 6f 20 74 68 65 20 6c 69 62 72  face to the libr
33834 61 72 79 2e 20 20 52 6f 75 74 69 6e 65 73 20 69  ary.  Routines i
33835 6e 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c 65 73  n.** other files
33836 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61   are for interna
33837 6c 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 20  l use by SQLite 
33838 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  and should not b
33839 65 0a 2a 2a 20 61 63 63 65 73 73 65 64 20 62 79  e.** accessed by
3383a 20 75 73 65 72 73 20 6f 66 20 74 68 65 20 6c 69   users of the li
3383b 62 72 61 72 79 2e 0a 2a 2f 0a 0a 23 69 66 64 65  brary..*/..#ifde
3383c 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
3383d 46 54 53 33 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  FTS3./**********
3383e 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73  **** Include fts
3383f 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  3.h in the middl
33840 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a  e of main.c ****
33841 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33842 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
33843 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
33844 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  fts3.h *********
33845 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33846 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33847 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20  ***/./*.** 2006 
33848 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65  Oct 10.**.** The
33849 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
3384a 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
3384b 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
3384c 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
3384d 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
3384e 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
3384f 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
33850 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
33851 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
33852 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
33853 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
33854 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
33855 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
33856 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
33857 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
33858 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
33859 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
3385a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3385b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3385c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3385d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3385e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
3385f 20 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c   This header fil
33860 65 20 69 73 20 75 73 65 64 20 62 79 20 70 72 6f  e is used by pro
33861 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20  grams that want 
33862 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20  to link against 
33863 74 68 65 0a 2a 2a 20 46 54 53 33 20 6c 69 62 72  the.** FTS3 libr
33864 61 72 79 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65  ary.  All it doe
33865 73 20 69 73 20 64 65 63 6c 61 72 65 20 74 68 65  s is declare the
33866 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74   sqlite3Fts3Init
33867 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  () interface..*/
33868 0a 0a 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22  ..#if 0.extern "
33869 43 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20  C" {.#endif  /* 
3386a 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a  __cplusplus */..
3386b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3386c 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e  nt sqlite3Fts3In
3386d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b  it(sqlite3 *db);
3386e 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 65 78  ..#if 0.}  /* ex
3386f 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23 65 6e 64  tern "C" */.#end
33870 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c  if  /* __cpluspl
33871 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  us */../********
33872 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
33873 73 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s3.h ***********
33874 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33875 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33876 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
33877 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
33878 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
33879 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63 20 2a 2a  off in main.c **
3387a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3387b 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  *****/.#endif.#i
3387c 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
3387d 4c 45 5f 52 54 52 45 45 0a 2f 2a 2a 2a 2a 2a 2a  LE_RTREE./******
3387e 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
3387f 20 72 74 72 65 65 2e 68 20 69 6e 20 74 68 65 20   rtree.h in the 
33880 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63  middle of main.c
33881 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
33882 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
33883 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
33884 69 6c 65 20 72 74 72 65 65 2e 68 20 2a 2a 2a 2a  ile rtree.h ****
33885 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33886 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33887 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
33888 30 30 38 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a  008 May 26.**.**
33889 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
3388a 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
3388b 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
3388c 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
3388d 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
3388e 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
3388f 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
33890 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
33891 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
33892 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
33893 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
33894 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
33895 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
33896 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
33897 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
33898 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
33899 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
3389a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3389b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3389c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3389d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3389e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
3389f 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
338a0 20 66 69 6c 65 20 69 73 20 75 73 65 64 20 62 79   file is used by
338a1 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 77   programs that w
338a2 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61 69  ant to link agai
338a3 6e 73 74 20 74 68 65 0a 2a 2a 20 52 54 52 45 45  nst the.** RTREE
338a4 20 6c 69 62 72 61 72 79 2e 20 20 41 6c 6c 20 69   library.  All i
338a5 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61 72  t does is declar
338a6 65 20 74 68 65 20 73 71 6c 69 74 65 33 52 74 72  e the sqlite3Rtr
338a7 65 65 49 6e 69 74 28 29 20 69 6e 74 65 72 66 61  eeInit() interfa
338a8 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78  ce..*/..#if 0.ex
338a9 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69  tern "C" {.#endi
338aa 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75  f  /* __cplusplu
338ab 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49  s */..SQLITE_PRI
338ac 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
338ad 52 74 72 65 65 49 6e 69 74 28 73 71 6c 69 74 65  RtreeInit(sqlite
338ae 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a 7d  3 *db);..#if 0.}
338af 20 20 2f 2a 20 65 78 74 65 72 6e 20 22 43 22 20    /* extern "C" 
338b0 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f  */.#endif  /* __
338b1 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 2f 2a  cplusplus */../*
338b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
338b3 64 20 6f 66 20 72 74 72 65 65 2e 68 20 2a 2a 2a  d of rtree.h ***
338b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
338b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f  ************* Co
338b8 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77  ntinuing where w
338b9 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6d 61  e left off in ma
338ba 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  in.c ***********
338bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65  ************/.#e
338bc 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
338bd 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 2f 2a  TE_ENABLE_ICU./*
338be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e  ************* In
338bf 63 6c 75 64 65 20 73 71 6c 69 74 65 69 63 75 2e  clude sqliteicu.
338c0 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
338c1 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a  of main.c ******
338c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
338c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
338c4 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 69  gin file sqlitei
338c5 63 75 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cu.h ***********
338c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
338c8 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 36 0a  .** 2008 May 26.
338c9 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
338ca 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
338cb 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
338cc 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
338cd 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
338ce 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
338cf 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
338d0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
338d1 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
338d2 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
338d3 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
338d4 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
338d5 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
338d6 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
338d7 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
338d8 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
338d9 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
338da 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
338db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338df 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68  ***.**.** This h
338e0 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 75 73  eader file is us
338e1 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20 74  ed by programs t
338e2 68 61 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b  hat want to link
338e3 20 61 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20   against the.** 
338e4 49 43 55 20 65 78 74 65 6e 73 69 6f 6e 2e 20 20  ICU extension.  
338e5 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73 20 64  All it does is d
338e6 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c 69 74  eclare the sqlit
338e7 65 33 49 63 75 49 6e 69 74 28 29 20 69 6e 74 65  e3IcuInit() inte
338e8 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30  rface..*/..#if 0
338e9 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65  .extern "C" {.#e
338ea 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73  ndif  /* __cplus
338eb 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f  plus */..SQLITE_
338ec 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
338ed 74 65 33 49 63 75 49 6e 69 74 28 73 71 6c 69 74  te3IcuInit(sqlit
338ee 65 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20 30 0a  e3 *db);..#if 0.
338ef 7d 20 20 2f 2a 20 65 78 74 65 72 6e 20 22 43 22  }  /* extern "C"
338f0 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20 5f   */.#endif  /* _
338f1 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a 0a  _cplusplus */...
338f2 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
338f3 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 69 63 75  End of sqliteicu
338f4 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
338f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
338f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
338f7 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
338f8 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
338f9 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
338fa 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  main.c *********
338fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
338fc 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
338fd 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  e version of the
338fe 20 6c 69 62 72 61 72 79 0a 2a 2f 0a 23 69 66 6e   library.*/.#ifn
338ff 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47  def SQLITE_AMALG
33900 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45 5f 41  AMATION.SQLITE_A
33901 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 73 71  PI const char sq
33902 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20  lite3_version[] 
33903 3d 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  = SQLITE_VERSION
33904 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ;.#endif.SQLITE_
33905 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
33906 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
33907 6f 6e 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  on(void){ return
33908 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e   sqlite3_version
33909 3b 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  ; }.SQLITE_API c
3390a 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
3390b 65 33 5f 73 6f 75 72 63 65 69 64 28 76 6f 69 64  e3_sourceid(void
3390c 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ){ return SQLITE
3390d 5f 53 4f 55 52 43 45 5f 49 44 3b 20 7d 0a 53 51  _SOURCE_ID; }.SQ
3390e 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3390f 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f  ite3_libversion_
33910 6e 75 6d 62 65 72 28 76 6f 69 64 29 7b 20 72 65  number(void){ re
33911 74 75 72 6e 20 53 51 4c 49 54 45 5f 56 45 52 53  turn SQLITE_VERS
33912 49 4f 4e 5f 4e 55 4d 42 45 52 3b 20 7d 0a 53 51  ION_NUMBER; }.SQ
33913 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
33914 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28  ite3_threadsafe(
33915 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
33916 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3b  LITE_THREADSAFE;
33917 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64   }..#if !defined
33918 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41  (SQLITE_OMIT_TRA
33919 43 45 29 20 26 26 20 64 65 66 69 6e 65 64 28 53  CE) && defined(S
3391a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54  QLITE_ENABLE_IOT
3391b 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 49 66 20 74  RACE)./*.** If t
3391c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e  he following fun
3391d 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69 73  ction pointer is
3391e 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20 69 66   not NULL and if
3391f 0a 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  .** SQLITE_ENABL
33920 45 5f 49 4f 54 52 41 43 45 20 69 73 20 65 6e 61  E_IOTRACE is ena
33921 62 6c 65 64 2c 20 74 68 65 6e 20 6d 65 73 73 61  bled, then messa
33922 67 65 73 20 64 65 73 63 72 69 62 69 6e 67 0a 2a  ges describing.*
33923 2a 20 49 2f 4f 20 61 63 74 69 76 65 20 61 72 65  * I/O active are
33924 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67 20 74   written using t
33925 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  his function.  T
33926 68 65 73 65 20 6d 65 73 73 61 67 65 73 0a 2a 2a  hese messages.**
33927 20 61 72 65 20 69 6e 74 65 6e 64 65 64 20 66 6f   are intended fo
33928 72 20 64 65 62 75 67 67 69 6e 67 20 61 63 74 69  r debugging acti
33929 76 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51  vity only..*/.SQ
3392a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
3392b 64 20 28 2a 73 71 6c 69 74 65 33 49 6f 54 72 61  d (*sqlite3IoTra
3392c 63 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ce)(const char*,
3392d 20 2e 2e 2e 29 20 3d 20 30 3b 0a 23 65 6e 64 69   ...) = 0;.#endi
3392e 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  f../*.** If the 
3392f 66 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c  following global
33930 20 76 61 72 69 61 62 6c 65 20 70 6f 69 6e 74 73   variable points
33931 20 74 6f 20 61 20 73 74 72 69 6e 67 20 77 68 69   to a string whi
33932 63 68 20 69 73 20 74 68 65 0a 2a 2a 20 6e 61 6d  ch is the.** nam
33933 65 20 6f 66 20 61 20 64 69 72 65 63 74 6f 72 79  e of a directory
33934 2c 20 74 68 65 6e 20 74 68 61 74 20 64 69 72 65  , then that dire
33935 63 74 6f 72 79 20 77 69 6c 6c 20 62 65 20 75 73  ctory will be us
33936 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74  ed to store.** t
33937 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a  emporary files..
33938 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74  **.** See also t
33939 68 65 20 22 50 52 41 47 4d 41 20 74 65 6d 70 5f  he "PRAGMA temp_
3393a 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22  store_directory"
3393b 20 53 51 4c 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f   SQL command..*/
3393c 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72  .SQLITE_API char
3393d 20 2a 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64   *sqlite3_temp_d
3393e 69 72 65 63 74 6f 72 79 20 3d 20 30 3b 0a 0a 2f  irectory = 0;../
3393f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
33940 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20  SQLite.  .**.** 
33941 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73  This routine mus
33942 74 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 69  t be called to i
33943 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
33944 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c  mory allocation,
33945 0a 2a 2a 20 56 46 53 2c 20 61 6e 64 20 6d 75 74  .** VFS, and mut
33946 65 78 20 73 75 62 73 79 73 74 65 6d 73 20 70 72  ex subsystems pr
33947 69 6f 72 20 74 6f 20 64 6f 69 6e 67 20 61 6e 79  ior to doing any
33948 20 73 65 72 69 6f 75 73 20 77 6f 72 6b 20 77 69   serious work wi
33949 74 68 0a 2a 2a 20 53 51 4c 69 74 65 2e 20 20 42  th.** SQLite.  B
3394a 75 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 79 6f  ut as long as yo
3394b 75 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 69 6c 65  u do not compile
3394c 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49   with SQLITE_OMI
3394d 54 5f 41 55 54 4f 49 4e 49 54 0a 2a 2a 20 74 68  T_AUTOINIT.** th
3394e 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
3394f 62 65 20 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61  be called automa
33950 74 69 63 61 6c 6c 79 20 62 79 20 6b 65 79 20 72  tically by key r
33951 6f 75 74 69 6e 65 73 20 73 75 63 68 20 61 73 0a  outines such as.
33952 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ** sqlite3_open(
33953 29 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ).  .**.** This 
33954 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
33955 6f 70 20 65 78 63 65 70 74 20 6f 6e 20 69 74 73  op except on its
33956 20 76 65 72 79 20 66 69 72 73 74 20 63 61 6c 6c   very first call
33957 20 66 6f 72 20 74 68 65 20 70 72 6f 63 65 73 73   for the process
33958 2c 0a 2a 2a 20 6f 72 20 66 6f 72 20 74 68 65 20  ,.** or for the 
33959 66 69 72 73 74 20 63 61 6c 6c 20 61 66 74 65 72  first call after
3395a 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
3395b 65 33 5f 73 68 75 74 64 6f 77 6e 2e 0a 2a 2a 0a  e3_shutdown..**.
3395c 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 68 72  ** The first thr
3395d 65 61 64 20 74 6f 20 63 61 6c 6c 20 74 68 69 73  ead to call this
3395e 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 20 74 68   routine runs th
3395f 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  e initialization
33960 20 74 6f 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f   to.** completio
33961 6e 2e 20 20 49 66 20 73 75 62 73 65 71 75 65 6e  n.  If subsequen
33962 74 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 74  t threads call t
33963 68 69 73 20 72 6f 75 74 69 6e 65 20 62 65 66 6f  his routine befo
33964 72 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  re the first.** 
33965 74 68 72 65 61 64 20 68 61 73 20 66 69 6e 69 73  thread has finis
33966 68 65 64 20 74 68 65 20 69 6e 69 74 69 61 6c 69  hed the initiali
33967 7a 61 74 69 6f 6e 20 70 72 6f 63 65 73 73 2c 20  zation process, 
33968 74 68 65 6e 20 74 68 65 20 73 75 62 73 65 71 75  then the subsequ
33969 65 6e 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 6d  ent.** threads m
3396a 75 73 74 20 62 6c 6f 63 6b 20 75 6e 74 69 6c 20  ust block until 
3396b 74 68 65 20 66 69 72 73 74 20 74 68 72 65 61 64  the first thread
3396c 20 66 69 6e 69 73 68 65 73 20 77 69 74 68 20 74   finishes with t
3396d 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  he initializatio
3396e 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  n..**.** The fir
3396f 73 74 20 74 68 72 65 61 64 20 6d 69 67 68 74 20  st thread might 
33970 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  call this routin
33971 65 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20 20  e recursively.  
33972 52 65 63 75 72 73 69 76 65 0a 2a 2a 20 63 61 6c  Recursive.** cal
33973 6c 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ls to this routi
33974 6e 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 6c  ne should not bl
33975 6f 63 6b 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20  ock, of course. 
33976 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 0a 2a   Otherwise the.*
33977 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  * initialization
33978 20 70 72 6f 63 65 73 73 20 77 6f 75 6c 64 20 6e   process would n
33979 65 76 65 72 20 63 6f 6d 70 6c 65 74 65 2e 0a 2a  ever complete..*
3397a 2a 0a 2a 2a 20 4c 65 74 20 58 20 62 65 20 74 68  *.** Let X be th
3397b 65 20 66 69 72 73 74 20 74 68 72 65 61 64 20 74  e first thread t
3397c 6f 20 65 6e 74 65 72 20 74 68 69 73 20 72 6f 75  o enter this rou
3397d 74 69 6e 65 2e 20 20 4c 65 74 20 59 20 62 65 20  tine.  Let Y be 
3397e 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 74 68  some other.** th
3397f 72 65 61 64 2e 20 20 54 68 65 6e 20 77 68 69 6c  read.  Then whil
33980 65 20 74 68 65 20 69 6e 69 74 69 61 6c 20 69 6e  e the initial in
33981 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 69 73  vocation of this
33982 20 72 6f 75 74 69 6e 65 20 62 79 20 58 20 69 73   routine by X is
33983 0a 2a 2a 20 69 6e 63 6f 6d 70 6c 65 74 65 2c 20  .** incomplete, 
33984 69 74 20 69 73 20 72 65 71 75 69 72 65 64 20 74  it is required t
33985 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  hat:.**.**    * 
33986 20 43 61 6c 6c 73 20 74 6f 20 74 68 69 73 20 72   Calls to this r
33987 6f 75 74 69 6e 65 20 66 72 6f 6d 20 59 20 6d 75  outine from Y mu
33988 73 74 20 62 6c 6f 63 6b 20 75 6e 74 69 6c 20 74  st block until t
33989 68 65 20 6f 75 74 65 72 2d 6d 6f 73 74 0a 2a 2a  he outer-most.**
3398a 20 20 20 20 20 20 20 63 61 6c 6c 20 62 79 20 58         call by X
3398b 20 63 6f 6d 70 6c 65 74 65 73 2e 0a 2a 2a 0a 2a   completes..**.*
3398c 2a 20 20 20 20 2a 20 20 52 65 63 75 72 73 69 76  *    *  Recursiv
3398d 65 20 63 61 6c 6c 73 20 74 6f 20 74 68 69 73 20  e calls to this 
3398e 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20 74 68 72  routine from thr
3398f 65 61 64 20 58 20 72 65 74 75 72 6e 20 69 6d 6d  ead X return imm
33990 65 64 69 61 74 65 6c 79 0a 2a 2a 20 20 20 20 20  ediately.**     
33991 20 20 77 69 74 68 6f 75 74 20 62 6c 6f 63 6b 69    without blocki
33992 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ng..*/.SQLITE_AP
33993 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6e  I int sqlite3_in
33994 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 7b 0a  itialize(void){.
33995 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
33996 2a 70 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20  *pMaster;       
33997 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33998 2a 20 54 68 65 20 6d 61 69 6e 20 73 74 61 74 69  * The main stati
33999 63 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69 6e 74  c mutex */.  int
3399a 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
3399b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3399c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
3399d 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 66  ult code */..#if
3399e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3399f 57 53 44 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  WSD.  rc = sqlit
339a0 65 33 5f 77 73 64 5f 69 6e 69 74 28 34 30 39 36  e3_wsd_init(4096
339a1 2c 20 32 34 29 3b 0a 20 20 69 66 28 20 72 63 21  , 24);.  if( rc!
339a2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
339a3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
339a4 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66  .#endif..  /* If
339a5 20 53 51 4c 69 74 65 20 69 73 20 61 6c 72 65 61   SQLite is alrea
339a6 64 79 20 63 6f 6d 70 6c 65 74 65 6c 79 20 69 6e  dy completely in
339a7 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 6e 20  itialized, then 
339a8 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a 2a 20 74  this call.  ** t
339a9 6f 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  o sqlite3_initia
339aa 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64 20 62 65  lize() should be
339ab 20 61 20 6e 6f 2d 6f 70 2e 20 20 42 75 74 20 74   a no-op.  But t
339ac 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  he initializatio
339ad 6e 0a 20 20 2a 2a 20 6d 75 73 74 20 62 65 20 63  n.  ** must be c
339ae 6f 6d 70 6c 65 74 65 2e 20 20 53 6f 20 69 73 49  omplete.  So isI
339af 6e 69 74 20 6d 75 73 74 20 6e 6f 74 20 62 65 20  nit must not be 
339b0 73 65 74 20 75 6e 74 69 6c 20 74 68 65 20 76 65  set until the ve
339b1 72 79 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74  ry end.  ** of t
339b2 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 20 20 2a  his routine..  *
339b3 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  /.  if( sqlite3G
339b4 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e  lobalConfig.isIn
339b5 69 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  it ) return SQLI
339b6 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 4d 61 6b  TE_OK;..  /* Mak
339b7 65 20 73 75 72 65 20 74 68 65 20 6d 75 74 65 78  e sure the mutex
339b8 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69 6e   subsystem is in
339b9 69 74 69 61 6c 69 7a 65 64 2e 20 20 49 66 20 75  itialized.  If u
339ba 6e 61 62 6c 65 20 74 6f 20 0a 20 20 2a 2a 20 69  nable to .  ** i
339bb 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75  nitialize the mu
339bc 74 65 78 20 73 75 62 73 79 73 74 65 6d 2c 20 72  tex subsystem, r
339bd 65 74 75 72 6e 20 65 61 72 6c 79 20 77 69 74 68  eturn early with
339be 20 74 68 65 20 65 72 72 6f 72 2e 0a 20 20 2a 2a   the error..  **
339bf 20 49 66 20 74 68 65 20 73 79 73 74 65 6d 20 69   If the system i
339c0 73 20 73 6f 20 73 69 63 6b 20 74 68 61 74 20 77  s so sick that w
339c1 65 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20  e are unable to 
339c2 61 6c 6c 6f 63 61 74 65 20 61 20 6d 75 74 65 78  allocate a mutex
339c3 2c 0a 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20  ,.  ** there is 
339c4 6e 6f 74 20 6d 75 63 68 20 53 51 4c 69 74 65 20  not much SQLite 
339c5 69 73 20 67 6f 69 6e 67 20 74 6f 20 62 65 20 61  is going to be a
339c6 62 6c 65 20 74 6f 20 64 6f 2e 0a 20 20 2a 2a 0a  ble to do..  **.
339c7 20 20 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 73    ** The mutex s
339c8 75 62 73 79 73 74 65 6d 20 6d 75 73 74 20 74 61  ubsystem must ta
339c9 6b 65 20 63 61 72 65 20 6f 66 20 73 65 72 69 61  ke care of seria
339ca 6c 69 7a 69 6e 67 20 69 74 73 20 6f 77 6e 0a 20  lizing its own. 
339cb 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74 69   ** initializati
339cc 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20  on..  */.  rc = 
339cd 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74  sqlite3MutexInit
339ce 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  ();.  if( rc ) r
339cf 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
339d0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d  Initialize the m
339d1 61 6c 6c 6f 63 28 29 20 73 79 73 74 65 6d 20 61  alloc() system a
339d2 6e 64 20 74 68 65 20 72 65 63 75 72 73 69 76 65  nd the recursive
339d3 20 70 49 6e 69 74 4d 75 74 65 78 20 6d 75 74 65   pInitMutex mute
339d4 78 2e 0a 20 20 2a 2a 20 54 68 69 73 20 6f 70 65  x..  ** This ope
339d5 72 61 74 69 6f 6e 20 69 73 20 70 72 6f 74 65 63  ration is protec
339d6 74 65 64 20 62 79 20 74 68 65 20 53 54 41 54 49  ted by the STATI
339d7 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20  C_MASTER mutex. 
339d8 20 4e 6f 74 65 20 74 68 61 74 0a 20 20 2a 2a 20   Note that.  ** 
339d9 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 69 73 20  MutexAlloc() is 
339da 63 61 6c 6c 65 64 20 66 6f 72 20 61 20 73 74 61  called for a sta
339db 74 69 63 20 6d 75 74 65 78 20 70 72 69 6f 72 20  tic mutex prior 
339dc 74 6f 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20  to initializing 
339dd 74 68 65 0a 20 20 2a 2a 20 6d 61 6c 6c 6f 63 20  the.  ** malloc 
339de 73 75 62 73 79 73 74 65 6d 20 2d 20 74 68 69 73  subsystem - this
339df 20 69 6d 70 6c 69 65 73 20 74 68 61 74 20 74 68   implies that th
339e0 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20  e allocation of 
339e1 61 20 73 74 61 74 69 63 0a 20 20 2a 2a 20 6d 75  a static.  ** mu
339e2 74 65 78 20 6d 75 73 74 20 6e 6f 74 20 72 65 71  tex must not req
339e3 75 69 72 65 20 73 75 70 70 6f 72 74 20 66 72 6f  uire support fro
339e4 6d 20 74 68 65 20 6d 61 6c 6c 6f 63 20 73 75 62  m the malloc sub
339e5 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 70  system..  */.  p
339e6 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65 33  Master = sqlite3
339e7 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
339e8 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
339e9 41 53 54 45 52 29 3b 0a 20 20 73 71 6c 69 74 65  ASTER);.  sqlite
339ea 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 4d  3_mutex_enter(pM
339eb 61 73 74 65 72 29 3b 0a 20 20 73 71 6c 69 74 65  aster);.  sqlite
339ec 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
339ed 4d 75 74 65 78 49 6e 69 74 20 3d 20 31 3b 0a 20  MutexInit = 1;. 
339ee 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f   if( !sqlite3Glo
339ef 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c  balConfig.isMall
339f0 6f 63 49 6e 69 74 20 29 7b 0a 20 20 20 20 72 63  ocInit ){.    rc
339f1 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
339f2 49 6e 69 74 28 29 3b 0a 20 20 7d 0a 20 20 69 66  Init();.  }.  if
339f3 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
339f4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
339f5 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c  obalConfig.isMal
339f6 6c 6f 63 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20  locInit = 1;.   
339f7 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f   if( !sqlite3Glo
339f8 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d  balConfig.pInitM
339f9 75 74 65 78 20 29 7b 0a 20 20 20 20 20 20 73 71  utex ){.      sq
339fa 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
339fb 67 2e 70 49 6e 69 74 4d 75 74 65 78 20 3d 0a 20  g.pInitMutex =. 
339fc 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
339fd 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
339fe 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
339ff 56 45 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73  VE);.      if( s
33a00 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33a01 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 26 26  ig.bCoreMutex &&
33a02 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   !sqlite3GlobalC
33a03 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78  onfig.pInitMutex
33a04 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
33a05 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
33a06 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
33a07 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
33a08 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
33a09 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33a0a 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 2b 2b 3b  nRefInitMutex++;
33a0b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
33a0c 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61 73 74  utex_leave(pMast
33a0d 65 72 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 72 63  er);..  /* If rc
33a0e 20 69 73 20 6e 6f 74 20 53 51 4c 49 54 45 5f 4f   is not SQLITE_O
33a0f 4b 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c  K at this point,
33a10 20 74 68 65 6e 20 65 69 74 68 65 72 20 74 68 65   then either the
33a11 20 6d 61 6c 6c 6f 63 0a 20 20 2a 2a 20 73 75 62   malloc.  ** sub
33a12 73 79 73 74 65 6d 20 63 6f 75 6c 64 20 6e 6f 74  system could not
33a13 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   be initialized 
33a14 6f 72 20 74 68 65 20 73 79 73 74 65 6d 20 66 61  or the system fa
33a15 69 6c 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  iled to allocate
33a16 0a 20 20 2a 2a 20 74 68 65 20 70 49 6e 69 74 4d  .  ** the pInitM
33a17 75 74 65 78 20 6d 75 74 65 78 2e 20 52 65 74 75  utex mutex. Retu
33a18 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 65  rn an error in e
33a19 69 74 68 65 72 20 63 61 73 65 2e 20 20 2a 2f 0a  ither case.  */.
33a1a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
33a1b 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
33a1c 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  n rc;.  }..  /* 
33a1d 44 6f 20 74 68 65 20 72 65 73 74 20 6f 66 20 74  Do the rest of t
33a1e 68 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  he initializatio
33a1f 6e 20 75 6e 64 65 72 20 74 68 65 20 72 65 63 75  n under the recu
33a20 72 73 69 76 65 20 6d 75 74 65 78 20 73 6f 0a 20  rsive mutex so. 
33a21 20 2a 2a 20 74 68 61 74 20 77 65 20 77 69 6c 6c   ** that we will
33a22 20 62 65 20 61 62 6c 65 20 74 6f 20 68 61 6e 64   be able to hand
33a23 6c 65 20 72 65 63 75 72 73 69 76 65 20 63 61 6c  le recursive cal
33a24 6c 73 20 69 6e 74 6f 0a 20 20 2a 2a 20 73 71 6c  ls into.  ** sql
33a25 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
33a26 29 2e 20 20 54 68 65 20 72 65 63 75 72 73 69 76  ).  The recursiv
33a27 65 20 63 61 6c 6c 73 20 6e 6f 72 6d 61 6c 6c 79  e calls normally
33a28 20 63 6f 6d 65 20 74 68 72 6f 75 67 68 0a 20 20   come through.  
33a29 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  ** sqlite3_os_in
33a2a 69 74 28 29 20 77 68 65 6e 20 69 74 20 69 6e 76  it() when it inv
33a2b 6f 6b 65 73 20 73 71 6c 69 74 65 33 5f 76 66 73  okes sqlite3_vfs
33a2c 5f 72 65 67 69 73 74 65 72 28 29 2c 20 62 75 74  _register(), but
33a2d 20 6f 74 68 65 72 0a 20 20 2a 2a 20 72 65 63 75   other.  ** recu
33a2e 72 73 69 76 65 20 63 61 6c 6c 73 20 6d 69 67 68  rsive calls migh
33a2f 74 20 61 6c 73 6f 20 62 65 20 70 6f 73 73 69 62  t also be possib
33a30 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  le..  */.  sqlit
33a31 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73  e3_mutex_enter(s
33a32 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33a33 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a  ig.pInitMutex);.
33a34 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
33a35 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74  balConfig.isInit
33a36 3d 3d 30 20 26 26 20 73 71 6c 69 74 65 33 47 6c  ==0 && sqlite3Gl
33a37 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f  obalConfig.inPro
33a38 67 72 65 73 73 3d 3d 30 20 29 7b 0a 20 20 20 20  gress==0 ){.    
33a39 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61  FuncDefHash *pHa
33a3a 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e  sh = &GLOBAL(Fun
33a3b 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65  cDefHash, sqlite
33a3c 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73  3GlobalFunctions
33a3d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  );.    sqlite3Gl
33a3e 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f  obalConfig.inPro
33a3f 67 72 65 73 73 20 3d 20 31 3b 0a 20 20 20 20 6d  gress = 1;.    m
33a40 65 6d 73 65 74 28 70 48 61 73 68 2c 20 30 2c 20  emset(pHash, 0, 
33a41 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 47 6c  sizeof(sqlite3Gl
33a42 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 29 3b  obalFunctions));
33a43 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 67 69  .    sqlite3Regi
33a44 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e 63 74 69  sterGlobalFuncti
33a45 6f 6e 73 28 29 3b 0a 20 20 20 20 69 66 28 20 73  ons();.    if( s
33a46 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33a47 69 67 2e 69 73 50 43 61 63 68 65 49 6e 69 74 3d  ig.isPCacheInit=
33a48 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  =0 ){.      rc =
33a49 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 6e   sqlite3PcacheIn
33a4a 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 20 20  itialize();.    
33a4b 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
33a4c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
33a4d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
33a4e 6e 66 69 67 2e 69 73 50 43 61 63 68 65 49 6e 69  nfig.isPCacheIni
33a4f 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63 20  t = 1;.      rc 
33a50 3d 20 73 71 6c 69 74 65 33 4f 73 49 6e 69 74 28  = sqlite3OsInit(
33a51 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
33a52 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
33a53 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  {.      sqlite3P
33a54 43 61 63 68 65 42 75 66 66 65 72 53 65 74 75 70  CacheBufferSetup
33a55 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
33a56 6f 6e 66 69 67 2e 70 50 61 67 65 2c 20 0a 20 20  onfig.pPage, .  
33a57 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47          sqlite3G
33a58 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61  lobalConfig.szPa
33a59 67 65 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ge, sqlite3Globa
33a5a 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 29 3b 0a  lConfig.nPage);.
33a5b 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
33a5c 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74  balConfig.isInit
33a5d 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 1;.    }.    
33a5e 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33a5f 66 69 67 2e 69 6e 50 72 6f 67 72 65 73 73 20 3d  fig.inProgress =
33a60 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
33a61 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71  3_mutex_leave(sq
33a62 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33a63 67 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a 0a  g.pInitMutex);..
33a64 20 20 2f 2a 20 47 6f 20 62 61 63 6b 20 75 6e 64    /* Go back und
33a65 65 72 20 74 68 65 20 73 74 61 74 69 63 20 6d 75  er the static mu
33a66 74 65 78 20 61 6e 64 20 63 6c 65 61 6e 20 75 70  tex and clean up
33a67 20 74 68 65 20 72 65 63 75 72 73 69 76 65 0a 20   the recursive. 
33a68 20 2a 2a 20 6d 75 74 65 78 20 74 6f 20 70 72 65   ** mutex to pre
33a69 76 65 6e 74 20 61 20 72 65 73 6f 75 72 63 65 20  vent a resource 
33a6a 6c 65 61 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  leak..  */.  sql
33a6b 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
33a6c 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 73 71 6c  (pMaster);.  sql
33a6d 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
33a6e 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 2d 2d  .nRefInitMutex--
33a6f 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  ;.  if( sqlite3G
33a70 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66  lobalConfig.nRef
33a71 49 6e 69 74 4d 75 74 65 78 3c 3d 30 20 29 7b 0a  InitMutex<=0 ){.
33a72 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
33a73 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33a74 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 3d 3d 30  nRefInitMutex==0
33a75 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   );.    sqlite3_
33a76 6d 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74  mutex_free(sqlit
33a77 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
33a78 49 6e 69 74 4d 75 74 65 78 29 3b 0a 20 20 20 20  InitMutex);.    
33a79 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33a7a 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20 3d  fig.pInitMutex =
33a7b 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65   0;.  }.  sqlite
33a7c 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d  3_mutex_leave(pM
33a7d 61 73 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 54 68  aster);..  /* Th
33a7e 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 6a  e following is j
33a7f 75 73 74 20 61 20 73 61 6e 69 74 79 20 63 68 65  ust a sanity che
33a80 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
33a81 53 51 4c 69 74 65 20 68 61 73 0a 20 20 2a 2a 20  SQLite has.  ** 
33a82 62 65 65 6e 20 63 6f 6d 70 69 6c 65 64 20 63 6f  been compiled co
33a83 72 72 65 63 74 6c 79 2e 20 20 49 74 20 69 73 20  rrectly.  It is 
33a84 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 72 75 6e  important to run
33a85 20 74 68 69 73 20 63 6f 64 65 2c 20 62 75 74 0a   this code, but.
33a86 20 20 2a 2a 20 77 65 20 64 6f 6e 27 74 20 77 61    ** we don't wa
33a87 6e 74 20 74 6f 20 72 75 6e 20 69 74 20 74 6f 6f  nt to run it too
33a88 20 6f 66 74 65 6e 20 61 6e 64 20 73 6f 61 6b 20   often and soak 
33a89 75 70 20 43 50 55 20 63 79 63 6c 65 73 20 66 6f  up CPU cycles fo
33a8a 72 20 6e 6f 0a 20 20 2a 2a 20 72 65 61 73 6f 6e  r no.  ** reason
33a8b 2e 20 20 53 6f 20 77 65 20 72 75 6e 20 69 74 20  .  So we run it 
33a8c 6f 6e 63 65 20 64 75 72 69 6e 67 20 69 6e 69 74  once during init
33a8d 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 20 20 2a 2f  ialization..  */
33a8e 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
33a8f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
33a90 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
33a91 4e 54 0a 20 20 2f 2a 20 54 68 69 73 20 73 65 63  NT.  /* This sec
33a92 74 69 6f 6e 20 6f 66 20 63 6f 64 65 27 73 20 6f  tion of code's o
33a93 6e 6c 79 20 22 6f 75 74 70 75 74 22 20 69 73 20  nly "output" is 
33a94 76 69 61 20 61 73 73 65 72 74 28 29 20 73 74 61  via assert() sta
33a95 74 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 66  tements. */.  if
33a96 20 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b   ( rc==SQLITE_OK
33a97 20 29 7b 0a 20 20 20 20 75 36 34 20 78 20 3d 20   ){.    u64 x = 
33a98 28 28 28 75 36 34 29 31 29 3c 3c 36 33 29 2d 31  (((u64)1)<<63)-1
33a99 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 79 3b 0a  ;.    double y;.
33a9a 20 20 20 20 61 73 73 65 72 74 28 73 69 7a 65 6f      assert(sizeo
33a9b 66 28 78 29 3d 3d 38 29 3b 0a 20 20 20 20 61 73  f(x)==8);.    as
33a9c 73 65 72 74 28 73 69 7a 65 6f 66 28 78 29 3d 3d  sert(sizeof(x)==
33a9d 73 69 7a 65 6f 66 28 79 29 29 3b 0a 20 20 20 20  sizeof(y));.    
33a9e 6d 65 6d 63 70 79 28 26 79 2c 20 26 78 2c 20 38  memcpy(&y, &x, 8
33a9f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
33aa0 71 6c 69 74 65 33 49 73 4e 61 4e 28 79 29 20 29  qlite3IsNaN(y) )
33aa1 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 65 6e  ;.  }.#endif.#en
33aa2 64 69 66 0a 0a 20 20 72 65 74 75 72 6e 20 72 63  dif..  return rc
33aa3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20  ;.}../*.** Undo 
33aa4 74 68 65 20 65 66 66 65 63 74 73 20 6f 66 20 73  the effects of s
33aa5 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
33aa6 65 28 29 2e 20 20 4d 75 73 74 20 6e 6f 74 20 62  e().  Must not b
33aa7 65 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 0a 2a  e called while.*
33aa8 2a 20 74 68 65 72 65 20 61 72 65 20 6f 75 74 73  * there are outs
33aa9 74 61 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65  tanding database
33aaa 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 72 20   connections or 
33aab 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
33aac 6e 73 20 6f 72 0a 2a 2a 20 77 68 69 6c 65 20 61  ns or.** while a
33aad 6e 79 20 70 61 72 74 20 6f 66 20 53 51 4c 69 74  ny part of SQLit
33aae 65 20 69 73 20 6f 74 68 65 72 77 69 73 65 20 69  e is otherwise i
33aaf 6e 20 75 73 65 20 69 6e 20 61 6e 79 20 74 68 72  n use in any thr
33ab0 65 61 64 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f  ead.  This.** ro
33ab1 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 74 68 72  utine is not thr
33ab2 65 61 64 73 61 66 65 2e 20 20 42 75 74 20 69 74  eadsafe.  But it
33ab3 20 69 73 20 73 61 66 65 20 74 6f 20 69 6e 76 6f   is safe to invo
33ab4 6b 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  ke this routine.
33ab5 2a 2a 20 6f 6e 20 77 68 65 6e 20 53 51 4c 69 74  ** on when SQLit
33ab6 65 20 69 73 20 61 6c 72 65 61 64 79 20 73 68 75  e is already shu
33ab7 74 20 64 6f 77 6e 2e 20 20 49 66 20 53 51 4c 69  t down.  If SQLi
33ab8 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 73 68  te is already sh
33ab9 75 74 20 64 6f 77 6e 0a 2a 2a 20 77 68 65 6e 20  ut down.** when 
33aba 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
33abb 69 6e 76 6f 6b 65 64 2c 20 74 68 65 6e 20 74 68  invoked, then th
33abc 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
33abd 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a  harmless no-op..
33abe 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
33abf 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f  t sqlite3_shutdo
33ac0 77 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  wn(void){.  if( 
33ac1 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33ac2 66 69 67 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20  fig.isInit ){.  
33ac3 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64    sqlite3_os_end
33ac4 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  ();.    sqlite3_
33ac5 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65 6e  reset_auto_exten
33ac6 73 69 6f 6e 28 29 3b 0a 20 20 20 20 73 71 6c 69  sion();.    sqli
33ac7 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33ac8 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a  isInit = 0;.  }.
33ac9 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
33aca 62 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61 63  balConfig.isPCac
33acb 68 65 49 6e 69 74 20 29 7b 0a 20 20 20 20 73 71  heInit ){.    sq
33acc 6c 69 74 65 33 50 63 61 63 68 65 53 68 75 74 64  lite3PcacheShutd
33acd 6f 77 6e 28 29 3b 0a 20 20 20 20 73 71 6c 69 74  own();.    sqlit
33ace 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
33acf 73 50 43 61 63 68 65 49 6e 69 74 20 3d 20 30 3b  sPCacheInit = 0;
33ad0 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
33ad1 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
33ad2 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 29 7b 0a 20  sMallocInit ){. 
33ad3 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63     sqlite3Malloc
33ad4 45 6e 64 28 29 3b 0a 20 20 20 20 73 71 6c 69 74  End();.    sqlit
33ad5 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
33ad6 73 4d 61 6c 6c 6f 63 49 6e 69 74 20 3d 20 30 3b  sMallocInit = 0;
33ad7 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
33ad8 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
33ad9 73 4d 75 74 65 78 49 6e 69 74 20 29 7b 0a 20 20  sMutexInit ){.  
33ada 20 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e    sqlite3MutexEn
33adb 64 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  d();.    sqlite3
33adc 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 4d  GlobalConfig.isM
33add 75 74 65 78 49 6e 69 74 20 3d 20 30 3b 0a 20 20  utexInit = 0;.  
33ade 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
33adf 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
33ae0 54 68 69 73 20 41 50 49 20 61 6c 6c 6f 77 73 20  This API allows 
33ae1 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74 6f 20  applications to 
33ae2 6d 6f 64 69 66 79 20 74 68 65 20 67 6c 6f 62 61  modify the globa
33ae3 6c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  l configuration 
33ae4 6f 66 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65  of.** the SQLite
33ae5 20 6c 69 62 72 61 72 79 20 61 74 20 72 75 6e 2d   library at run-
33ae6 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  time..**.** This
33ae7 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20   routine should 
33ae8 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77  only be called w
33ae9 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hen there are no
33aea 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a 2a 2a 20   outstanding.** 
33aeb 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
33aec 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f 72 79 20 61  ions or memory a
33aed 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 20 54 68 69  llocations.  Thi
33aee 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74  s routine is not
33aef 0a 2a 2a 20 74 68 72 65 61 64 73 61 66 65 2e 20  .** threadsafe. 
33af0 20 46 61 69 6c 75 72 65 20 74 6f 20 68 65 65 64   Failure to heed
33af1 20 74 68 65 73 65 20 77 61 72 6e 69 6e 67 73 20   these warnings 
33af2 63 61 6e 20 6c 65 61 64 20 74 6f 20 75 6e 70 72  can lead to unpr
33af3 65 64 69 63 74 61 62 6c 65 0a 2a 2a 20 62 65 68  edictable.** beh
33af4 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  avior..*/.SQLITE
33af5 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
33af6 5f 63 6f 6e 66 69 67 28 69 6e 74 20 6f 70 2c 20  _config(int op, 
33af7 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
33af8 61 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ap;.  int rc = S
33af9 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20  QLITE_OK;..  /* 
33afa 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
33afb 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 53 51   shall return SQ
33afc 4c 49 54 45 5f 4d 49 53 55 53 45 20 69 66 20 69  LITE_MISUSE if i
33afd 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 68 69  t is invoked whi
33afe 6c 65 0a 20 20 2a 2a 20 74 68 65 20 53 51 4c 69  le.  ** the SQLi
33aff 74 65 20 6c 69 62 72 61 72 79 20 69 73 20 69 6e  te library is in
33b00 20 75 73 65 2e 20 2a 2f 0a 20 20 69 66 28 20 73   use. */.  if( s
33b01 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33b02 69 67 2e 69 73 49 6e 69 74 20 29 20 72 65 74 75  ig.isInit ) retu
33b03 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
33b04 3b 0a 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  ;..  va_start(ap
33b05 2c 20 6f 70 29 3b 0a 20 20 73 77 69 74 63 68 28  , op);.  switch(
33b06 20 6f 70 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 4d   op ){..    /* M
33b07 75 74 65 78 20 63 6f 6e 66 69 67 75 72 61 74 69  utex configurati
33b08 6f 6e 20 6f 70 74 69 6f 6e 73 20 61 72 65 20 6f  on options are o
33b09 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  nly available in
33b0a 20 61 20 74 68 72 65 61 64 73 61 66 65 0a 20 20   a threadsafe.  
33b0b 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 2e 20 0a 20    ** compile. . 
33b0c 20 20 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45     */.#if SQLITE
33b0d 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20  _THREADSAFE.    
33b0e 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  case SQLITE_CONF
33b0f 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 3a  IG_SINGLETHREAD:
33b10 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 69 73 61   {.      /* Disa
33b11 62 6c 65 20 61 6c 6c 20 6d 75 74 65 78 69 6e 67  ble all mutexing
33b12 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
33b13 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
33b14 6f 72 65 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20  oreMutex = 0;.  
33b15 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
33b16 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74  lConfig.bFullMut
33b17 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72  ex = 0;.      br
33b18 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
33b19 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
33b1a 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 3a 20 7b  G_MULTITHREAD: {
33b1b 0a 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62 6c  .      /* Disabl
33b1c 65 20 6d 75 74 65 78 69 6e 67 20 6f 66 20 64 61  e mutexing of da
33b1d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
33b1e 6e 73 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 45  ns */.      /* E
33b1f 6e 61 62 6c 65 20 6d 75 74 65 78 69 6e 67 20 6f  nable mutexing o
33b20 66 20 63 6f 72 65 20 64 61 74 61 20 73 74 72 75  f core data stru
33b21 63 74 75 72 65 73 20 2a 2f 0a 20 20 20 20 20 20  ctures */.      
33b22 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33b23 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 3d  fig.bCoreMutex =
33b24 20 31 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   1;.      sqlite
33b25 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46  3GlobalConfig.bF
33b26 75 6c 6c 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20  ullMutex = 0;.  
33b27 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
33b28 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
33b29 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a  _CONFIG_SERIALIZ
33b2a 45 44 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 45  ED: {.      /* E
33b2b 6e 61 62 6c 65 20 61 6c 6c 20 6d 75 74 65 78 69  nable all mutexi
33b2c 6e 67 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  ng */.      sqli
33b2d 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33b2e 62 43 6f 72 65 4d 75 74 65 78 20 3d 20 31 3b 0a  bCoreMutex = 1;.
33b2f 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
33b30 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d  balConfig.bFullM
33b31 75 74 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20  utex = 1;.      
33b32 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
33b33 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
33b34 46 49 47 5f 4d 55 54 45 58 3a 20 7b 0a 20 20 20  FIG_MUTEX: {.   
33b35 20 20 20 2f 2a 20 53 70 65 63 69 66 79 20 61 6e     /* Specify an
33b36 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 75 74   alternative mut
33b37 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
33b38 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  n */.      sqlit
33b39 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
33b3a 75 74 65 78 20 3d 20 2a 76 61 5f 61 72 67 28 61  utex = *va_arg(a
33b3b 70 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  p, sqlite3_mutex
33b3c 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20  _methods*);.    
33b3d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
33b3e 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
33b3f 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3a 20  ONFIG_GETMUTEX: 
33b40 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65 74 72 69  {.      /* Retri
33b41 65 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  eve the current 
33b42 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61  mutex implementa
33b43 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 2a 76  tion */.      *v
33b44 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65  a_arg(ap, sqlite
33b45 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 2a  3_mutex_methods*
33b46 29 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ) = sqlite3Globa
33b47 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 20  lConfig.mutex;. 
33b48 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33b49 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 20 20 63  }.#endif...    c
33b4a 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
33b4b 47 5f 4d 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20 20  G_MALLOC: {.    
33b4c 20 20 2f 2a 20 53 70 65 63 69 66 79 20 61 6e 20    /* Specify an 
33b4d 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 61 6c 6c  alternative mall
33b4e 6f 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  oc implementatio
33b4f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  n */.      sqlit
33b50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
33b51 20 3d 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73   = *va_arg(ap, s
33b52 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
33b53 64 73 2a 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ds*);.      brea
33b54 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
33b55 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
33b56 47 45 54 4d 41 4c 4c 4f 43 3a 20 7b 0a 20 20 20  GETMALLOC: {.   
33b57 20 20 20 2f 2a 20 52 65 74 72 69 65 76 65 20 74     /* Retrieve t
33b58 68 65 20 63 75 72 72 65 6e 74 20 6d 61 6c 6c 6f  he current mallo
33b59 63 28 29 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  c() implementati
33b5a 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  on */.      if( 
33b5b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33b5c 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 3d 3d 30  fig.m.xMalloc==0
33b5d 20 29 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74   ) sqlite3MemSet
33b5e 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 20 20 20  Default();.     
33b5f 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c   *va_arg(ap, sql
33b60 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
33b61 2a 29 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  *) = sqlite3Glob
33b62 61 6c 43 6f 6e 66 69 67 2e 6d 3b 0a 20 20 20 20  alConfig.m;.    
33b63 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
33b64 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
33b65 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 3a  ONFIG_MEMSTATUS:
33b66 20 7b 0a 20 20 20 20 20 20 2f 2a 20 45 6e 61 62   {.      /* Enab
33b67 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68  le or disable th
33b68 65 20 6d 61 6c 6c 6f 63 20 73 74 61 74 75 73 20  e malloc status 
33b69 63 6f 6c 6c 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  collection */.  
33b6a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
33b6b 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
33b6c 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
33b6d 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t);.      break;
33b6e 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
33b6f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43  SQLITE_CONFIG_SC
33b70 52 41 54 43 48 3a 20 7b 0a 20 20 20 20 20 20 2f  RATCH: {.      /
33b71 2a 20 44 65 73 69 67 6e 61 74 65 20 61 20 62 75  * Designate a bu
33b72 66 66 65 72 20 66 6f 72 20 73 63 72 61 74 63 68  ffer for scratch
33b73 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f   memory space */
33b74 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
33b75 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61  obalConfig.pScra
33b76 74 63 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  tch = va_arg(ap,
33b77 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 73   void*);.      s
33b78 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33b79 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d 20 76  ig.szScratch = v
33b7a 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
33b7b 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
33b7c 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74  balConfig.nScrat
33b7d 63 68 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  ch = va_arg(ap, 
33b7e 69 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  int);.      brea
33b7f 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
33b80 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
33b81 50 41 47 45 43 41 43 48 45 3a 20 7b 0a 20 20 20  PAGECACHE: {.   
33b82 20 20 20 2f 2a 20 44 65 73 69 67 6e 61 74 65 20     /* Designate 
33b83 61 20 62 75 66 66 65 72 20 66 6f 72 20 70 61 67  a buffer for pag
33b84 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 73  e cache memory s
33b85 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 73 71  pace */.      sq
33b86 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33b87 67 2e 70 50 61 67 65 20 3d 20 76 61 5f 61 72 67  g.pPage = va_arg
33b88 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20  (ap, void*);.   
33b89 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
33b8a 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 20 3d 20  Config.szPage = 
33b8b 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
33b8c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
33b8d 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65  obalConfig.nPage
33b8e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
33b8f 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t);.      break;
33b90 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65  .    }..    case
33b91 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50   SQLITE_CONFIG_P
33b92 43 41 43 48 45 3a 20 7b 0a 20 20 20 20 20 20 2f  CACHE: {.      /
33b93 2a 20 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74  * Specify an alt
33b94 65 72 6e 61 74 69 76 65 20 70 61 67 65 20 63 61  ernative page ca
33b95 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
33b96 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  on */.      sqli
33b97 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33b98 70 63 61 63 68 65 20 3d 20 2a 76 61 5f 61 72 67  pcache = *va_arg
33b99 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 70 63 61  (ap, sqlite3_pca
33b9a 63 68 65 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a 20  che_methods*);. 
33b9b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
33b9c 7d 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  }..    case SQLI
33b9d 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41  TE_CONFIG_GETPCA
33b9e 43 48 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  CHE: {.      if(
33b9f 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
33ba0 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69  nfig.pcache.xIni
33ba1 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
33ba2 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74  sqlite3PCacheSet
33ba3 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 20 20 20  Default();.     
33ba4 20 7d 0a 20 20 20 20 20 20 2a 76 61 5f 61 72 67   }.      *va_arg
33ba5 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 70 63 61  (ap, sqlite3_pca
33ba6 63 68 65 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20  che_methods*) = 
33ba7 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33ba8 66 69 67 2e 70 63 61 63 68 65 3b 0a 20 20 20 20  fig.pcache;.    
33ba9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a    break;.    }..
33baa 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
33bab 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
33bac 33 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  3) || defined(SQ
33bad 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
33bae 59 53 35 29 0a 20 20 20 20 63 61 73 65 20 53 51  YS5).    case SQ
33baf 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50  LITE_CONFIG_HEAP
33bb0 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65 73  : {.      /* Des
33bb1 69 67 6e 61 74 65 20 61 20 62 75 66 66 65 72 20  ignate a buffer 
33bb2 66 6f 72 20 68 65 61 70 20 6d 65 6d 6f 72 79 20  for heap memory 
33bb3 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 73  space */.      s
33bb4 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33bb5 69 67 2e 70 48 65 61 70 20 3d 20 76 61 5f 61 72  ig.pHeap = va_ar
33bb6 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20 20  g(ap, void*);.  
33bb7 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
33bb8 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 20 3d 20  lConfig.nHeap = 
33bb9 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
33bba 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
33bbb 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65 71  obalConfig.mnReq
33bbc 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
33bbd 74 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 73  t);..      if( s
33bbe 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33bbf 69 67 2e 70 48 65 61 70 3d 3d 30 20 29 7b 0a 20  ig.pHeap==0 ){. 
33bc0 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
33bc1 20 68 65 61 70 20 70 6f 69 6e 74 65 72 20 69 73   heap pointer is
33bc2 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65 73 74   NULL, then rest
33bc3 6f 72 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20 69  ore the malloc i
33bc4 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 20 20  mplementation.  
33bc5 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20 74 6f        ** back to
33bc6 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20 74   NULL pointers t
33bc7 6f 6f 2e 20 20 54 68 69 73 20 77 69 6c 6c 20 63  oo.  This will c
33bc8 61 75 73 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20  ause the malloc 
33bc9 74 6f 20 67 6f 0a 20 20 20 20 20 20 20 20 2a 2a  to go.        **
33bca 20 62 61 63 6b 20 74 6f 20 69 74 73 20 64 65 66   back to its def
33bcb 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ault implementat
33bcc 69 6f 6e 20 77 68 65 6e 20 73 71 6c 69 74 65 33  ion when sqlite3
33bcd 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69 73  _initialize() is
33bce 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 75 6e 2e  .        ** run.
33bcf 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
33bd0 20 20 20 20 6d 65 6d 73 65 74 28 26 73 71 6c 69      memset(&sqli
33bd1 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33bd2 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c  m, 0, sizeof(sql
33bd3 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
33bd4 2e 6d 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  .m));.      }els
33bd5 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e{.        /* Th
33bd6 65 20 68 65 61 70 20 70 6f 69 6e 74 65 72 20 69  e heap pointer i
33bd7 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
33bd8 20 69 6e 73 74 61 6c 6c 20 6f 6e 65 20 6f 66 20   install one of 
33bd9 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d  the.        ** m
33bda 65 6d 35 2e 63 2f 6d 65 6d 33 2e 63 20 6d 65 74  em5.c/mem3.c met
33bdb 68 6f 64 73 2e 20 49 66 20 6e 65 69 74 68 65 72  hods. If neither
33bdc 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20   ENABLE_MEMSYS3 
33bdd 6e 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 45  nor.        ** E
33bde 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73  NABLE_MEMSYS5 is
33bdf 20 64 65 66 69 6e 65 64 2c 20 72 65 74 75 72 6e   defined, return
33be0 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 20   an error..     
33be1 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c     */.#ifdef SQL
33be2 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
33be3 53 33 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  S3.        sqlit
33be4 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
33be5 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65   = *sqlite3MemGe
33be6 74 4d 65 6d 73 79 73 33 28 29 3b 0a 23 65 6e 64  tMemsys3();.#end
33be7 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
33be8 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a  _ENABLE_MEMSYS5.
33be9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47          sqlite3G
33bea 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20  lobalConfig.m = 
33beb 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65  *sqlite3MemGetMe
33bec 6d 73 79 73 35 28 29 3b 0a 23 65 6e 64 69 66 0a  msys5();.#endif.
33bed 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
33bee 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
33bef 66 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  f..    case SQLI
33bf0 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53  TE_CONFIG_LOOKAS
33bf1 49 44 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  IDE: {.      sql
33bf2 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
33bf3 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 3d 20 76  .szLookaside = v
33bf4 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
33bf5 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
33bf6 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b 61  balConfig.nLooka
33bf7 73 69 64 65 20 3d 20 76 61 5f 61 72 67 28 61 70  side = va_arg(ap
33bf8 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62 72  , int);.      br
33bf9 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
33bfa 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
33bfb 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
33bfc 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OR;.      break;
33bfd 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f  .    }.  }.  va_
33bfe 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72  end(ap);.  retur
33bff 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
33c00 65 74 20 75 70 20 74 68 65 20 6c 6f 6f 6b 61 73  et up the lookas
33c01 69 64 65 20 62 75 66 66 65 72 73 20 66 6f 72 20  ide buffers for 
33c02 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
33c03 63 74 69 6f 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e  ction..** Return
33c04 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
33c05 63 63 65 73 73 2e 20 20 0a 2a 2a 20 49 66 20 6c  ccess.  .** If l
33c06 6f 6f 6b 61 73 69 64 65 20 69 73 20 61 6c 72 65  ookaside is alre
33c07 61 64 79 20 61 63 74 69 76 65 2c 20 72 65 74 75  ady active, retu
33c08 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a  rn SQLITE_BUSY..
33c09 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a 20 70 61 72  **.** The sz par
33c0a 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 6e 75  ameter is the nu
33c0b 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
33c0c 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20   each lookaside 
33c0d 73 6c 6f 74 2e 0a 2a 2a 20 54 68 65 20 63 6e 74  slot..** The cnt
33c0e 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68   parameter is th
33c0f 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74  e number of slot
33c10 73 2e 20 20 49 66 20 70 53 74 61 72 74 20 69 73  s.  If pStart is
33c11 20 4e 55 4c 4c 20 74 68 65 0a 2a 2a 20 73 70 61   NULL the.** spa
33c12 63 65 20 66 6f 72 20 74 68 65 20 6c 6f 6f 6b 61  ce for the looka
33c13 73 69 64 65 20 6d 65 6d 6f 72 79 20 69 73 20 6f  side memory is o
33c14 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
33c15 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a  ite3_malloc()..*
33c16 2a 20 49 66 20 70 53 74 61 72 74 20 69 73 20 6e  * If pStart is n
33c17 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 69 74 20  ot NULL then it 
33c18 69 73 20 73 7a 2a 63 6e 74 20 62 79 74 65 73 20  is sz*cnt bytes 
33c19 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 75 73 65  of memory to use
33c1a 20 66 6f 72 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b   for.** the look
33c1b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f  aside memory..*/
33c1c 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 74 75  .static int setu
33c1d 70 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c 69 74  pLookaside(sqlit
33c1e 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 42  e3 *db, void *pB
33c1f 75 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20  uf, int sz, int 
33c20 63 6e 74 29 7b 0a 20 20 76 6f 69 64 20 2a 70 53  cnt){.  void *pS
33c21 74 61 72 74 3b 0a 20 20 69 66 28 20 64 62 2d 3e  tart;.  if( db->
33c22 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 20 29  lookaside.nOut )
33c23 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
33c24 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20  ITE_BUSY;.  }.  
33c25 2f 2a 20 46 72 65 65 20 61 6e 79 20 65 78 69 73  /* Free any exis
33c26 74 69 6e 67 20 6c 6f 6f 6b 61 73 69 64 65 20 62  ting lookaside b
33c27 75 66 66 65 72 20 66 6f 72 20 74 68 69 73 20 68  uffer for this h
33c28 61 6e 64 6c 65 20 62 65 66 6f 72 65 0a 20 20 2a  andle before.  *
33c29 2a 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 6e  * allocating a n
33c2a 65 77 20 6f 6e 65 20 73 6f 20 77 65 20 64 6f 6e  ew one so we don
33c2b 27 74 20 68 61 76 65 20 74 6f 20 68 61 76 65 20  't have to have 
33c2c 73 70 61 63 65 20 66 6f 72 20 0a 20 20 2a 2a 20  space for .  ** 
33c2d 62 6f 74 68 20 61 74 20 74 68 65 20 73 61 6d 65  both at the same
33c2e 20 74 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   time..  */.  if
33c2f 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
33c30 62 4d 61 6c 6c 6f 63 65 64 20 29 7b 0a 20 20 20  bMalloced ){.   
33c31 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62   sqlite3_free(db
33c32 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61  ->lookaside.pSta
33c33 72 74 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54 68  rt);.  }.  /* Th
33c34 65 20 73 69 7a 65 20 6f 66 20 61 20 6c 6f 6f 6b  e size of a look
33c35 61 73 69 64 65 20 73 6c 6f 74 20 6e 65 65 64 73  aside slot needs
33c36 20 74 6f 20 62 65 20 6c 61 72 67 65 72 20 74 68   to be larger th
33c37 61 6e 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 2a  an a pointer.  *
33c38 2a 20 74 6f 20 62 65 20 75 73 65 66 75 6c 2e 0a  * to be useful..
33c39 20 20 2a 2f 0a 20 20 69 66 28 20 73 7a 3c 3d 28    */.  if( sz<=(
33c3a 69 6e 74 29 73 69 7a 65 6f 66 28 4c 6f 6f 6b 61  int)sizeof(Looka
33c3b 73 69 64 65 53 6c 6f 74 2a 29 20 29 20 73 7a 20  sideSlot*) ) sz 
33c3c 3d 20 30 3b 0a 20 20 69 66 28 20 63 6e 74 3c 30  = 0;.  if( cnt<0
33c3d 20 29 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 66   ) cnt = 0;.  if
33c3e 28 20 73 7a 3d 3d 30 20 7c 7c 20 63 6e 74 3d 3d  ( sz==0 || cnt==
33c3f 30 20 29 7b 0a 20 20 20 20 73 7a 20 3d 20 30 3b  0 ){.    sz = 0;
33c40 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20 30 3b  .    pStart = 0;
33c41 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 42 75  .  }else if( pBu
33c42 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 7a 20 3d  f==0 ){.    sz =
33c43 20 52 4f 55 4e 44 38 28 73 7a 29 3b 0a 20 20 20   ROUND8(sz);.   
33c44 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
33c45 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20  ignMalloc();.   
33c46 20 70 53 74 61 72 74 20 3d 20 73 71 6c 69 74 65   pStart = sqlite
33c47 33 4d 61 6c 6c 6f 63 28 20 73 7a 2a 63 6e 74 20  3Malloc( sz*cnt 
33c48 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e  );.    sqlite3En
33c49 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  dBenignMalloc();
33c4a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 7a  .  }else{.    sz
33c4b 20 3d 20 52 4f 55 4e 44 44 4f 57 4e 38 28 73 7a   = ROUNDDOWN8(sz
33c4c 29 3b 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20  );.    pStart = 
33c4d 70 42 75 66 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  pBuf;.  }.  db->
33c4e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61 72 74  lookaside.pStart
33c4f 20 3d 20 70 53 74 61 72 74 3b 0a 20 20 64 62 2d   = pStart;.  db-
33c50 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65  >lookaside.pFree
33c51 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b   = 0;.  db->look
33c52 61 73 69 64 65 2e 73 7a 20 3d 20 28 75 31 36 29  aside.sz = (u16)
33c53 73 7a 3b 0a 20 20 69 66 28 20 70 53 74 61 72 74  sz;.  if( pStart
33c54 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
33c55 20 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74     LookasideSlot
33c56 20 2a 70 3b 0a 20 20 20 20 61 73 73 65 72 74 28   *p;.    assert(
33c57 20 73 7a 20 3e 20 28 69 6e 74 29 73 69 7a 65 6f   sz > (int)sizeo
33c58 66 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a  f(LookasideSlot*
33c59 29 20 29 3b 0a 20 20 20 20 70 20 3d 20 28 4c 6f  ) );.    p = (Lo
33c5a 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 70 53 74  okasideSlot*)pSt
33c5b 61 72 74 3b 0a 20 20 20 20 66 6f 72 28 69 3d 63  art;.    for(i=c
33c5c 6e 74 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29  nt-1; i>=0; i--)
33c5d 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74  {.      p->pNext
33c5e 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65   = db->lookaside
33c5f 2e 70 46 72 65 65 3b 0a 20 20 20 20 20 20 64 62  .pFree;.      db
33c60 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65  ->lookaside.pFre
33c61 65 20 3d 20 70 3b 0a 20 20 20 20 20 20 70 20 3d  e = p;.      p =
33c62 20 28 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 2a   (LookasideSlot*
33c63 29 26 28 28 75 38 2a 29 70 29 5b 73 7a 5d 3b 0a  )&((u8*)p)[sz];.
33c64 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 6c 6f      }.    db->lo
33c65 6f 6b 61 73 69 64 65 2e 70 45 6e 64 20 3d 20 70  okaside.pEnd = p
33c66 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ;.    db->lookas
33c67 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 31  ide.bEnabled = 1
33c68 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ;.    db->lookas
33c69 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 3d 20  ide.bMalloced = 
33c6a 70 42 75 66 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20  pBuf==0 ?1:0;.  
33c6b 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6c  }else{.    db->l
33c6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e 64 20 3d 20  ookaside.pEnd = 
33c6d 30 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61  0;.    db->looka
33c6e 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20  side.bEnabled = 
33c6f 30 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61  0;.    db->looka
33c70 73 69 64 65 2e 62 4d 61 6c 6c 6f 63 65 64 20 3d  side.bMalloced =
33c71 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
33c72 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
33c73 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
33c74 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74 65 64  mutex associated
33c75 20 77 69 74 68 20 61 20 64 61 74 61 62 61 73 65   with a database
33c76 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a   connection..*/.
33c77 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
33c78 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65  e3_mutex *sqlite
33c79 33 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c 69 74  3_db_mutex(sqlit
33c7a 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72  e3 *db){.  retur
33c7b 6e 20 64 62 2d 3e 6d 75 74 65 78 3b 0a 7d 0a 0a  n db->mutex;.}..
33c7c 2f 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74  /*.** Configurat
33c7d 69 6f 6e 20 73 65 74 74 69 6e 67 73 20 66 6f 72  ion settings for
33c7e 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 64   an individual d
33c7f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
33c80 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  on.*/.SQLITE_API
33c81 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f   int sqlite3_db_
33c82 63 6f 6e 66 69 67 28 73 71 6c 69 74 65 33 20 2a  config(sqlite3 *
33c83 64 62 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29  db, int op, ...)
33c84 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
33c85 20 20 69 6e 74 20 72 63 3b 0a 20 20 76 61 5f 73    int rc;.  va_s
33c86 74 61 72 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20  tart(ap, op);.  
33c87 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20  switch( op ){.  
33c88 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 44 42    case SQLITE_DB
33c89 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
33c8a 3a 20 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a  : {.      void *
33c8b 70 42 75 66 20 3d 20 76 61 5f 61 72 67 28 61 70  pBuf = va_arg(ap
33c8c 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20  , void*);.      
33c8d 69 6e 74 20 73 7a 20 3d 20 76 61 5f 61 72 67 28  int sz = va_arg(
33c8e 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
33c8f 69 6e 74 20 63 6e 74 20 3d 20 76 61 5f 61 72 67  int cnt = va_arg
33c90 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
33c91 20 72 63 20 3d 20 73 65 74 75 70 4c 6f 6f 6b 61   rc = setupLooka
33c92 73 69 64 65 28 64 62 2c 20 70 42 75 66 2c 20 73  side(db, pBuf, s
33c93 7a 2c 20 63 6e 74 29 3b 0a 20 20 20 20 20 20 62  z, cnt);.      b
33c94 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
33c95 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
33c96 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
33c97 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OR;.      break;
33c98 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f  .    }.  }.  va_
33c99 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74 75 72  end(ap);.  retur
33c9a 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  n rc;.}.../*.** 
33c9b 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
33c9c 68 65 20 62 75 66 66 65 72 20 7a 5b 30 2e 2e 6e  he buffer z[0..n
33c9d 2d 31 5d 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c  -1] contains all
33c9e 20 73 70 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74   spaces..*/.stat
33c9f 69 63 20 69 6e 74 20 61 6c 6c 53 70 61 63 65 73  ic int allSpaces
33ca0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20  (const char *z, 
33ca1 69 6e 74 20 6e 29 7b 0a 20 20 77 68 69 6c 65 28  int n){.  while(
33ca2 20 6e 3e 30 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d   n>0 && z[n-1]==
33ca3 27 20 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20  ' ' ){ n--; }.  
33ca4 72 65 74 75 72 6e 20 6e 3d 3d 30 3b 0a 7d 0a 0a  return n==0;.}..
33ca5 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  /*.** This is th
33ca6 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  e default collat
33ca7 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  ing function nam
33ca8 65 64 20 22 42 49 4e 41 52 59 22 20 77 68 69 63  ed "BINARY" whic
33ca9 68 20 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61  h is always.** a
33caa 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  vailable..**.** 
33cab 49 66 20 74 68 65 20 70 61 64 46 6c 61 67 20 61  If the padFlag a
33cac 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e  rgument is not N
33cad 55 4c 4c 20 74 68 65 6e 20 73 70 61 63 65 20 70  ULL then space p
33cae 61 64 64 69 6e 67 20 61 74 20 74 68 65 20 65 6e  adding at the en
33caf 64 0a 2a 2a 20 6f 66 20 73 74 72 69 6e 67 73 20  d.** of strings 
33cb0 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 54 68 69  is ignored.  Thi
33cb1 73 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  s implements the
33cb2 20 52 54 52 49 4d 20 63 6f 6c 6c 61 74 69 6f 6e   RTRIM collation
33cb3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
33cb4 62 69 6e 43 6f 6c 6c 46 75 6e 63 28 0a 20 20 76  binCollFunc(.  v
33cb5 6f 69 64 20 2a 70 61 64 46 6c 61 67 2c 0a 20 20  oid *padFlag,.  
33cb6 69 6e 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74  int nKey1, const
33cb7 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 0a 20 20   void *pKey1,.  
33cb8 69 6e 74 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74  int nKey2, const
33cb9 20 76 6f 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a   void *pKey2.){.
33cba 20 20 69 6e 74 20 72 63 2c 20 6e 3b 0a 20 20 6e    int rc, n;.  n
33cbb 20 3d 20 6e 4b 65 79 31 3c 6e 4b 65 79 32 20 3f   = nKey1<nKey2 ?
33cbc 20 6e 4b 65 79 31 20 3a 20 6e 4b 65 79 32 3b 0a   nKey1 : nKey2;.
33cbd 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4b    rc = memcmp(pK
33cbe 65 79 31 2c 20 70 4b 65 79 32 2c 20 6e 29 3b 0a  ey1, pKey2, n);.
33cbf 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20    if( rc==0 ){. 
33cc0 20 20 20 69 66 28 20 70 61 64 46 6c 61 67 0a 20     if( padFlag. 
33cc1 20 20 20 20 26 26 20 61 6c 6c 53 70 61 63 65 73      && allSpaces
33cc2 28 28 28 63 68 61 72 2a 29 70 4b 65 79 31 29 2b  (((char*)pKey1)+
33cc3 6e 2c 20 6e 4b 65 79 31 2d 6e 29 0a 20 20 20 20  n, nKey1-n).    
33cc4 20 26 26 20 61 6c 6c 53 70 61 63 65 73 28 28 28   && allSpaces(((
33cc5 63 68 61 72 2a 29 70 4b 65 79 32 29 2b 6e 2c 20  char*)pKey2)+n, 
33cc6 6e 4b 65 79 32 2d 6e 29 0a 20 20 20 20 29 7b 0a  nKey2-n).    ){.
33cc7 20 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 72        /* Leave r
33cc8 63 20 75 6e 63 68 61 6e 67 65 64 20 61 74 20 30  c unchanged at 0
33cc9 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   */.    }else{. 
33cca 20 20 20 20 20 72 63 20 3d 20 6e 4b 65 79 31 20       rc = nKey1 
33ccb 2d 20 6e 4b 65 79 32 3b 0a 20 20 20 20 7d 0a 20  - nKey2;.    }. 
33ccc 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
33ccd 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72  }../*.** Another
33cce 20 62 75 69 6c 74 2d 69 6e 20 63 6f 6c 6c 61 74   built-in collat
33ccf 69 6e 67 20 73 65 71 75 65 6e 63 65 3a 20 4e 4f  ing sequence: NO
33cd0 43 41 53 45 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69  CASE. .**.** Thi
33cd1 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  s collating sequ
33cd2 65 6e 63 65 20 69 73 20 69 6e 74 65 6e 64 65 64  ence is intended
33cd3 20 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20   to be used for 
33cd4 22 63 61 73 65 20 69 6e 64 65 70 65 6e 64 61 6e  "case independan
33cd5 74 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 22  t.** comparison"
33cd6 2e 20 53 51 4c 69 74 65 27 73 20 6b 6e 6f 77 6c  . SQLite's knowl
33cd7 65 64 67 65 20 6f 66 20 75 70 70 65 72 20 61 6e  edge of upper an
33cd8 64 20 6c 6f 77 65 72 20 63 61 73 65 20 65 71 75  d lower case equ
33cd9 69 76 61 6c 65 6e 74 73 0a 2a 2a 20 65 78 74 65  ivalents.** exte
33cda 6e 64 73 20 6f 6e 6c 79 20 74 6f 20 74 68 65 20  nds only to the 
33cdb 32 36 20 63 68 61 72 61 63 74 65 72 73 20 75 73  26 characters us
33cdc 65 64 20 69 6e 20 74 68 65 20 45 6e 67 6c 69 73  ed in the Englis
33cdd 68 20 6c 61 6e 67 75 61 67 65 2e 0a 2a 2a 0a 2a  h language..**.*
33cde 2a 20 41 74 20 74 68 65 20 6d 6f 6d 65 6e 74 20  * At the moment 
33cdf 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20  there is only a 
33ce0 55 54 46 2d 38 20 69 6d 70 6c 65 6d 65 6e 74 61  UTF-8 implementa
33ce1 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
33ce2 69 6e 74 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74  int nocaseCollat
33ce3 69 6e 67 46 75 6e 63 28 0a 20 20 76 6f 69 64 20  ingFunc(.  void 
33ce4 2a 4e 6f 74 55 73 65 64 2c 0a 20 20 69 6e 74 20  *NotUsed,.  int 
33ce5 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69  nKey1, const voi
33ce6 64 20 2a 70 4b 65 79 31 2c 0a 20 20 69 6e 74 20  d *pKey1,.  int 
33ce7 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f 69  nKey2, const voi
33ce8 64 20 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 69 6e  d *pKey2.){.  in
33ce9 74 20 72 20 3d 20 73 71 6c 69 74 65 33 53 74 72  t r = sqlite3Str
33cea 4e 49 43 6d 70 28 0a 20 20 20 20 20 20 28 63 6f  NICmp(.      (co
33ceb 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 31  nst char *)pKey1
33cec 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  , (const char *)
33ced 70 4b 65 79 32 2c 20 28 6e 4b 65 79 31 3c 6e 4b  pKey2, (nKey1<nK
33cee 65 79 32 29 3f 6e 4b 65 79 31 3a 6e 4b 65 79 32  ey2)?nKey1:nKey2
33cef 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
33cf0 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
33cf1 20 20 69 66 28 20 30 3d 3d 72 20 29 7b 0a 20 20    if( 0==r ){.  
33cf2 20 20 72 20 3d 20 6e 4b 65 79 31 2d 6e 4b 65 79    r = nKey1-nKey
33cf3 32 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  2;.  }.  return 
33cf4 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  r;.}../*.** Retu
33cf5 72 6e 20 74 68 65 20 52 4f 57 49 44 20 6f 66 20  rn the ROWID of 
33cf6 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
33cf7 69 6e 73 65 72 74 0a 2a 2f 0a 53 51 4c 49 54 45  insert.*/.SQLITE
33cf8 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36  _API sqlite_int6
33cf9 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69  4 sqlite3_last_i
33cfa 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69  nsert_rowid(sqli
33cfb 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75  te3 *db){.  retu
33cfc 72 6e 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64  rn db->lastRowid
33cfd 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
33cfe 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
33cff 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d  changes in the m
33d00 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20  ost recent call 
33d01 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  to sqlite3_exec(
33d02 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
33d03 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61   int sqlite3_cha
33d04 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62  nges(sqlite3 *db
33d05 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e  ){.  return db->
33d06 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  nChange;.}../*.*
33d07 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
33d08 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 20 73  ber of changes s
33d09 69 6e 63 65 20 74 68 65 20 64 61 74 61 62 61 73  ince the databas
33d0a 65 20 68 61 6e 64 6c 65 20 77 61 73 20 6f 70 65  e handle was ope
33d0b 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ned..*/.SQLITE_A
33d0c 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74  PI int sqlite3_t
33d0d 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c  otal_changes(sql
33d0e 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74  ite3 *db){.  ret
33d0f 75 72 6e 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68  urn db->nTotalCh
33d10 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ange;.}../*.** C
33d11 6c 6f 73 65 20 61 6c 6c 20 6f 70 65 6e 20 73 61  lose all open sa
33d12 76 65 70 6f 69 6e 74 73 2e 20 54 68 69 73 20 66  vepoints. This f
33d13 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 6d 61 6e  unction only man
33d14 69 70 75 6c 61 74 65 73 20 66 69 65 6c 64 73 20  ipulates fields 
33d15 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  of the.** databa
33d16 73 65 20 68 61 6e 64 6c 65 20 6f 62 6a 65 63 74  se handle object
33d17 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 63 6c  , it does not cl
33d18 6f 73 65 20 61 6e 79 20 73 61 76 65 70 6f 69 6e  ose any savepoin
33d19 74 73 20 74 68 61 74 20 6d 61 79 20 62 65 20 6f  ts that may be o
33d1a 70 65 6e 0a 2a 2a 20 61 74 20 74 68 65 20 62 2d  pen.** at the b-
33d1b 74 72 65 65 2f 70 61 67 65 72 20 6c 65 76 65 6c  tree/pager level
33d1c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
33d1d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
33d1e 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28  CloseSavepoints(
33d1f 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
33d20 77 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76 65  while( db->pSave
33d21 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 53 61 76  point ){.    Sav
33d22 65 70 6f 69 6e 74 20 2a 70 54 6d 70 20 3d 20 64  epoint *pTmp = d
33d23 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20  b->pSavepoint;. 
33d24 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e     db->pSavepoin
33d25 74 20 3d 20 70 54 6d 70 2d 3e 70 4e 65 78 74 3b  t = pTmp->pNext;
33d26 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
33d27 65 65 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20  ee(db, pTmp);.  
33d28 7d 0a 20 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69  }.  db->nSavepoi
33d29 6e 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 6e 53  nt = 0;.  db->nS
33d2a 74 61 74 65 6d 65 6e 74 20 3d 20 30 3b 0a 20 20  tatement = 0;.  
33d2b 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f  db->isTransactio
33d2c 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a  nSavepoint = 0;.
33d2d 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
33d2e 6e 20 65 78 69 73 74 69 6e 67 20 53 51 4c 69 74  n existing SQLit
33d2f 65 20 64 61 74 61 62 61 73 65 0a 2a 2f 0a 53 51  e database.*/.SQ
33d30 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
33d31 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74  ite3_close(sqlit
33d32 65 33 20 2a 64 62 29 7b 0a 20 20 48 61 73 68 45  e3 *db){.  HashE
33d33 6c 65 6d 20 2a 69 3b 0a 20 20 69 6e 74 20 6a 3b  lem *i;.  int j;
33d34 0a 0a 20 20 69 66 28 20 21 64 62 20 29 7b 0a 20  ..  if( !db ){. 
33d35 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
33d36 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  _OK;.  }.  if( !
33d37 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
33d38 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29  ckSickOrOk(db) )
33d39 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
33d3a 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
33d3b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
33d3c 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
33d3d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65  ;..  sqlite3Rese
33d3e 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
33d3f 64 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66  db, 0);..  /* If
33d40 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
33d41 73 20 6f 70 65 6e 2c 20 74 68 65 20 52 65 73 65  s open, the Rese
33d42 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
33d43 29 20 63 61 6c 6c 20 61 62 6f 76 65 0a 20 20 2a  ) call above.  *
33d44 2a 20 77 69 6c 6c 20 6e 6f 74 20 68 61 76 65 20  * will not have 
33d45 63 61 6c 6c 65 64 20 74 68 65 20 78 44 69 73 63  called the xDisc
33d46 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 20  onnect() method 
33d47 6f 6e 20 61 6e 79 20 76 69 72 74 75 61 6c 0a 20  on any virtual. 
33d48 20 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68   ** tables in th
33d49 65 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 5d 20  e db->aVTrans[] 
33d4a 61 72 72 61 79 2e 20 54 68 65 20 66 6f 6c 6c 6f  array. The follo
33d4b 77 69 6e 67 20 73 71 6c 69 74 65 33 56 74 61 62  wing sqlite3Vtab
33d4c 52 6f 6c 6c 62 61 63 6b 28 29 0a 20 20 2a 2a 20  Rollback().  ** 
33d4d 63 61 6c 6c 20 77 69 6c 6c 20 64 6f 20 73 6f 2e  call will do so.
33d4e 20 57 65 20 6e 65 65 64 20 74 6f 20 64 6f 20 74   We need to do t
33d4f 68 69 73 20 62 65 66 6f 72 65 20 74 68 65 20 63  his before the c
33d50 68 65 63 6b 20 66 6f 72 20 61 63 74 69 76 65 0a  heck for active.
33d51 20 20 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65    ** SQL stateme
33d52 6e 74 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68  nts below, as th
33d53 65 20 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d  e v-table implem
33d54 65 6e 74 61 74 69 6f 6e 20 6d 61 79 20 62 65 20  entation may be 
33d55 73 74 6f 72 69 6e 67 0a 20 20 2a 2a 20 73 6f 6d  storing.  ** som
33d56 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
33d57 6d 65 6e 74 73 20 69 6e 74 65 72 6e 61 6c 6c 79  ments internally
33d58 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
33d59 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 64 62 29  VtabRollback(db)
33d5a 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65  ;..  /* If there
33d5b 20 61 72 65 20 61 6e 79 20 6f 75 74 73 74 61 6e   are any outstan
33d5c 64 69 6e 67 20 56 4d 73 2c 20 72 65 74 75 72 6e  ding VMs, return
33d5d 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f   SQLITE_BUSY. */
33d5e 0a 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65  .  if( db->pVdbe
33d5f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
33d60 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
33d61 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20 22  BUSY, .        "
33d62 75 6e 61 62 6c 65 20 74 6f 20 63 6c 6f 73 65 20  unable to close 
33d63 64 75 65 20 74 6f 20 75 6e 66 69 6e 61 6c 69 73  due to unfinalis
33d64 65 64 20 73 74 61 74 65 6d 65 6e 74 73 22 29 3b  ed statements");
33d65 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
33d66 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
33d67 65 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ex);.    return 
33d68 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
33d69 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
33d6a 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63  e3SafetyCheckSic
33d6b 6b 4f 72 4f 6b 28 64 62 29 20 29 3b 0a 0a 20 20  kOrOk(db) );..  
33d6c 66 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e  for(j=0; j<db->n
33d6d 44 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 42 74  Db; j++){.    Bt
33d6e 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
33d6f 44 62 5b 6a 5d 2e 70 42 74 3b 0a 20 20 20 20 69  Db[j].pBt;.    i
33d70 66 28 20 70 42 74 20 26 26 20 73 71 6c 69 74 65  f( pBt && sqlite
33d71 33 42 74 72 65 65 49 73 49 6e 42 61 63 6b 75 70  3BtreeIsInBackup
33d72 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 73  (pBt) ){.      s
33d73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
33d74 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20  SQLITE_BUSY, .  
33d75 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20          "unable 
33d76 74 6f 20 63 6c 6f 73 65 20 64 75 65 20 74 6f 20  to close due to 
33d77 75 6e 66 69 6e 69 73 68 65 64 20 62 61 63 6b 75  unfinished backu
33d78 70 20 6f 70 65 72 61 74 69 6f 6e 22 29 3b 0a 20  p operation");. 
33d79 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
33d7a 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
33d7b 65 78 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  ex);.      retur
33d7c 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  n SQLITE_BUSY;. 
33d7d 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
33d7e 72 65 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64  ree any outstand
33d7f 69 6e 67 20 53 61 76 65 70 6f 69 6e 74 20 73 74  ing Savepoint st
33d80 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a 20 20 73  ructures. */.  s
33d81 71 6c 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70  qlite3CloseSavep
33d82 6f 69 6e 74 73 28 64 62 29 3b 0a 0a 20 20 66 6f  oints(db);..  fo
33d83 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62  r(j=0; j<db->nDb
33d84 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; j++){.    stru
33d85 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62  ct Db *pDb = &db
33d86 2d 3e 61 44 62 5b 6a 5d 3b 0a 20 20 20 20 69 66  ->aDb[j];.    if
33d87 28 20 70 44 62 2d 3e 70 42 74 20 29 7b 0a 20 20  ( pDb->pBt ){.  
33d88 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
33d89 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29 3b  Close(pDb->pBt);
33d8a 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 42 74 20  .      pDb->pBt 
33d8b 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6a  = 0;.      if( j
33d8c 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 70  !=1 ){.        p
33d8d 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b  Db->pSchema = 0;
33d8e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
33d8f 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65   }.  sqlite3Rese
33d90 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
33d91 64 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 54 65  db, 0);..  /* Te
33d92 6c 6c 20 74 68 65 20 63 6f 64 65 20 69 6e 20 6e  ll the code in n
33d93 6f 74 69 66 79 2e 63 20 74 68 61 74 20 74 68 65  otify.c that the
33d94 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6e 6f 20 6c   connection no l
33d95 6f 6e 67 65 72 20 68 6f 6c 64 73 20 61 6e 79 0a  onger holds any.
33d96 20 20 2a 2a 20 6c 6f 63 6b 73 20 61 6e 64 20 64    ** locks and d
33d97 6f 65 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20  oes not require 
33d98 61 6e 79 20 66 75 72 74 68 65 72 20 75 6e 6c 6f  any further unlo
33d99 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61  ck-notify callba
33d9a 63 6b 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  cks..  */.  sqli
33d9b 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f  te3ConnectionClo
33d9c 73 65 64 28 64 62 29 3b 0a 0a 20 20 61 73 73 65  sed(db);..  asse
33d9d 72 74 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 29  rt( db->nDb<=2 )
33d9e 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
33d9f 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74  aDb==db->aDbStat
33da0 69 63 20 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b  ic );.  for(j=0;
33da1 20 6a 3c 41 72 72 61 79 53 69 7a 65 28 64 62 2d   j<ArraySize(db-
33da2 3e 61 46 75 6e 63 2e 61 29 3b 20 6a 2b 2b 29 7b  >aFunc.a); j++){
33da3 0a 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 4e  .    FuncDef *pN
33da4 65 78 74 2c 20 2a 70 48 61 73 68 2c 20 2a 70 3b  ext, *pHash, *p;
33da5 0a 20 20 20 20 66 6f 72 28 70 3d 64 62 2d 3e 61  .    for(p=db->a
33da6 46 75 6e 63 2e 61 5b 6a 5d 3b 20 70 3b 20 70 3d  Func.a[j]; p; p=
33da7 70 48 61 73 68 29 7b 0a 20 20 20 20 20 20 70 48  pHash){.      pH
33da8 61 73 68 20 3d 20 70 2d 3e 70 48 61 73 68 3b 0a  ash = p->pHash;.
33da9 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 29        while( p )
33daa 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 20  {.        pNext 
33dab 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  = p->pNext;.    
33dac 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
33dad 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20  e(db, p);.      
33dae 20 20 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20 20    p = pNext;.   
33daf 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
33db0 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73   for(i=sqliteHas
33db1 68 46 69 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c  hFirst(&db->aCol
33db2 6c 53 65 71 29 3b 20 69 3b 20 69 3d 73 71 6c 69  lSeq); i; i=sqli
33db3 74 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a  teHashNext(i)){.
33db4 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
33db5 6c 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29  ll = (CollSeq *)
33db6 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69  sqliteHashData(i
33db7 29 3b 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65  );.    /* Invoke
33db8 20 61 6e 79 20 64 65 73 74 72 75 63 74 6f 72 73   any destructors
33db9 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20   registered for 
33dba 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
33dbb 63 65 20 75 73 65 72 20 64 61 74 61 2e 20 2a 2f  ce user data. */
33dbc 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  .    for(j=0; j<
33dbd 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69  3; j++){.      i
33dbe 66 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c  f( pColl[j].xDel
33dbf 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c   ){.        pCol
33dc0 6c 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f 6c 6c 5b  l[j].xDel(pColl[
33dc1 6a 5d 2e 70 55 73 65 72 29 3b 0a 20 20 20 20 20  j].pUser);.     
33dc2 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
33dc3 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
33dc4 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  Coll);.  }.  sql
33dc5 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 64  ite3HashClear(&d
33dc6 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23 69  b->aCollSeq);.#i
33dc7 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
33dc8 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
33dc9 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73   for(i=sqliteHas
33dca 68 46 69 72 73 74 28 26 64 62 2d 3e 61 4d 6f 64  hFirst(&db->aMod
33dcb 75 6c 65 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74  ule); i; i=sqlit
33dcc 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20  eHashNext(i)){. 
33dcd 20 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 20     Module *pMod 
33dce 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69  = (Module *)sqli
33dcf 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20  teHashData(i);. 
33dd0 20 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 44 65     if( pMod->xDe
33dd1 73 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 70  stroy ){.      p
33dd2 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79 28 70 4d  Mod->xDestroy(pM
33dd3 6f 64 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d  od->pAux);.    }
33dd4 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
33dd5 65 65 28 64 62 2c 20 70 4d 6f 64 29 3b 0a 20 20  ee(db, pMod);.  
33dd6 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43  }.  sqlite3HashC
33dd7 6c 65 61 72 28 26 64 62 2d 3e 61 4d 6f 64 75 6c  lear(&db->aModul
33dd8 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71  e);.#endif..  sq
33dd9 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53  lite3Error(db, S
33dda 51 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 20 2f 2a  QLITE_OK, 0); /*
33ddb 20 44 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79   Deallocates any
33ddc 20 63 61 63 68 65 64 20 65 72 72 6f 72 20 73 74   cached error st
33ddd 72 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 66 28 20  rings. */.  if( 
33dde 64 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 20  db->pErr ){.    
33ddf 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
33de0 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 7d 0a  (db->pErr);.  }.
33de1 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78    sqlite3CloseEx
33de2 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 0a 20  tensions(db);.. 
33de3 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c   db->magic = SQL
33de4 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b  ITE_MAGIC_ERROR;
33de5 0a 0a 20 20 2f 2a 20 54 68 65 20 74 65 6d 70 2d  ..  /* The temp-
33de6 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
33de7 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 69 66  is allocated dif
33de8 66 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68  ferently from th
33de9 65 20 6f 74 68 65 72 20 73 63 68 65 6d 61 0a 20  e other schema. 
33dea 20 2a 2a 20 6f 62 6a 65 63 74 73 20 28 75 73 69   ** objects (usi
33deb 6e 67 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  ng sqliteMalloc(
33dec 29 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74  ) directly, inst
33ded 65 61 64 20 6f 66 20 73 71 6c 69 74 65 33 42 74  ead of sqlite3Bt
33dee 72 65 65 53 63 68 65 6d 61 28 29 29 2e 0a 20 20  reeSchema())..  
33def 2a 2a 20 53 6f 20 69 74 20 6e 65 65 64 73 20 74  ** So it needs t
33df0 6f 20 62 65 20 66 72 65 65 64 20 68 65 72 65 2e  o be freed here.
33df1 20 54 6f 64 6f 3a 20 57 68 79 20 6e 6f 74 20 72   Todo: Why not r
33df2 6f 6c 6c 20 74 68 65 20 74 65 6d 70 20 73 63 68  oll the temp sch
33df3 65 6d 61 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68  ema into.  ** th
33df4 65 20 73 61 6d 65 20 73 71 6c 69 74 65 4d 61 6c  e same sqliteMal
33df5 6c 6f 63 28 29 20 61 73 20 74 68 65 20 6f 6e 65  loc() as the one
33df6 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 65 73 20   that allocates 
33df7 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20  the database .  
33df8 2a 2a 20 73 74 72 75 63 74 75 72 65 3f 0a 20 20  ** structure?.  
33df9 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  */.  sqlite3DbFr
33dfa 65 65 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 31  ee(db, db->aDb[1
33dfb 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71  ].pSchema);.  sq
33dfc 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
33dfd 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
33dfe 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
33dff 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 3b  TE_MAGIC_CLOSED;
33e00 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
33e01 5f 66 72 65 65 28 64 62 2d 3e 6d 75 74 65 78 29  _free(db->mutex)
33e02 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ;.  assert( db->
33e03 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3d 3d  lookaside.nOut==
33e04 30 20 29 3b 20 20 2f 2a 20 46 61 69 6c 73 20 6f  0 );  /* Fails o
33e05 6e 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  n a lookaside me
33e06 6d 6f 72 79 20 6c 65 61 6b 20 2a 2f 0a 20 20 69  mory leak */.  i
33e07 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  f( db->lookaside
33e08 2e 62 4d 61 6c 6c 6f 63 65 64 20 29 7b 0a 20 20  .bMalloced ){.  
33e09 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64    sqlite3_free(d
33e0a 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74  b->lookaside.pSt
33e0b 61 72 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  art);.  }.  sqli
33e0c 74 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 20 20  te3_free(db);.  
33e0d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
33e0e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62  ;.}../*.** Rollb
33e0f 61 63 6b 20 61 6c 6c 20 64 61 74 61 62 61 73 65  ack all database
33e10 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   files..*/.SQLIT
33e11 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
33e12 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c  qlite3RollbackAl
33e13 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  l(sqlite3 *db){.
33e14 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69    int i;.  int i
33e15 6e 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 61 73  nTrans = 0;.  as
33e16 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
33e17 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
33e18 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
33e19 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
33e1a 63 28 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  c();.  for(i=0; 
33e1b 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b  i<db->nDb; i++){
33e1c 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62  .    if( db->aDb
33e1d 5b 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20 20 20  [i].pBt ){.     
33e1e 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65   if( sqlite3Btre
33e1f 65 49 73 49 6e 54 72 61 6e 73 28 64 62 2d 3e 61  eIsInTrans(db->a
33e20 44 62 5b 69 5d 2e 70 42 74 29 20 29 7b 0a 20 20  Db[i].pBt) ){.  
33e21 20 20 20 20 20 20 69 6e 54 72 61 6e 73 20 3d 20        inTrans = 
33e22 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
33e23 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c   sqlite3BtreeRol
33e24 6c 62 61 63 6b 28 64 62 2d 3e 61 44 62 5b 69 5d  lback(db->aDb[i]
33e25 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64 62 2d  .pBt);.      db-
33e26 3e 61 44 62 5b 69 5d 2e 69 6e 54 72 61 6e 73 20  >aDb[i].inTrans 
33e27 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
33e28 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c   sqlite3VtabRoll
33e29 62 61 63 6b 28 64 62 29 3b 0a 20 20 73 71 6c 69  back(db);.  sqli
33e2a 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
33e2b 6f 63 28 29 3b 0a 0a 20 20 69 66 28 20 64 62 2d  oc();..  if( db-
33e2c 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e  >flags&SQLITE_In
33e2d 74 65 72 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20  ternChanges ){. 
33e2e 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
33e2f 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
33e30 74 73 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69  ts(db);.    sqli
33e31 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
33e32 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20  Schema(db, 0);. 
33e33 20 7d 0a 0a 20 20 2f 2a 20 41 6e 79 20 64 65 66   }..  /* Any def
33e34 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e 74  erred constraint
33e35 20 76 69 6f 6c 61 74 69 6f 6e 73 20 68 61 76 65   violations have
33e36 20 6e 6f 77 20 62 65 65 6e 20 72 65 73 6f 6c 76   now been resolv
33e37 65 64 2e 20 2a 2f 0a 20 20 64 62 2d 3e 6e 44 65  ed. */.  db->nDe
33e38 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 30 3b 0a  ferredCons = 0;.
33e39 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 68 61 73  .  /* If one has
33e3a 20 62 65 65 6e 20 63 6f 6e 66 69 67 75 72 65 64   been configured
33e3b 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 72 6f 6c  , invoke the rol
33e3c 6c 62 61 63 6b 2d 68 6f 6f 6b 20 63 61 6c 6c 62  lback-hook callb
33e3d 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  ack */.  if( db-
33e3e 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61  >xRollbackCallba
33e3f 63 6b 20 26 26 20 28 69 6e 54 72 61 6e 73 20 7c  ck && (inTrans |
33e40 7c 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  | !db->autoCommi
33e41 74 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e 78 52  t) ){.    db->xR
33e42 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b 28  ollbackCallback(
33e43 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67  db->pRollbackArg
33e44 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
33e45 52 65 74 75 72 6e 20 61 20 73 74 61 74 69 63 20  Return a static 
33e46 73 74 72 69 6e 67 20 74 68 61 74 20 64 65 73 63  string that desc
33e47 72 69 62 65 73 20 74 68 65 20 6b 69 6e 64 20 6f  ribes the kind o
33e48 66 20 65 72 72 6f 72 20 73 70 65 63 69 66 69 65  f error specifie
33e49 64 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72 67 75  d in the.** argu
33e4a 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
33e4b 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
33e4c 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72 53 74  ar *sqlite3ErrSt
33e4d 72 28 69 6e 74 20 72 63 29 7b 0a 20 20 73 74 61  r(int rc){.  sta
33e4e 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 2a 20  tic const char* 
33e4f 63 6f 6e 73 74 20 61 4d 73 67 5b 5d 20 3d 20 7b  const aMsg[] = {
33e50 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  .    /* SQLITE_O
33e51 4b 20 20 20 20 20 20 20 20 20 20 2a 2f 20 22 6e  K          */ "n
33e52 6f 74 20 61 6e 20 65 72 72 6f 72 22 2c 0a 20 20  ot an error",.  
33e53 20 20 2f 2a 20 53 51 4c 49 54 45 5f 45 52 52 4f    /* SQLITE_ERRO
33e54 52 20 20 20 20 20 20 20 2a 2f 20 22 53 51 4c 20  R       */ "SQL 
33e55 6c 6f 67 69 63 20 65 72 72 6f 72 20 6f 72 20 6d  logic error or m
33e56 69 73 73 69 6e 67 20 64 61 74 61 62 61 73 65 22  issing database"
33e57 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33e58 49 4e 54 45 52 4e 41 4c 20 20 20 20 2a 2f 20 30  INTERNAL    */ 0
33e59 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33e5a 50 45 52 4d 20 20 20 20 20 20 20 20 2a 2f 20 22  PERM        */ "
33e5b 61 63 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f  access permissio
33e5c 6e 20 64 65 6e 69 65 64 22 2c 0a 20 20 20 20 2f  n denied",.    /
33e5d 2a 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 20  * SQLITE_ABORT  
33e5e 20 20 20 20 20 2a 2f 20 22 63 61 6c 6c 62 61 63       */ "callbac
33e5f 6b 20 72 65 71 75 65 73 74 65 64 20 71 75 65 72  k requested quer
33e60 79 20 61 62 6f 72 74 22 2c 0a 20 20 20 20 2f 2a  y abort",.    /*
33e61 20 53 51 4c 49 54 45 5f 42 55 53 59 20 20 20 20   SQLITE_BUSY    
33e62 20 20 20 20 2a 2f 20 22 64 61 74 61 62 61 73 65      */ "database
33e63 20 69 73 20 6c 6f 63 6b 65 64 22 2c 0a 20 20 20   is locked",.   
33e64 20 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45   /* SQLITE_LOCKE
33e65 44 20 20 20 20 20 20 2a 2f 20 22 64 61 74 61 62  D      */ "datab
33e66 61 73 65 20 74 61 62 6c 65 20 69 73 20 6c 6f 63  ase table is loc
33e67 6b 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  ked",.    /* SQL
33e68 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20  ITE_NOMEM       
33e69 2a 2f 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  */ "out of memor
33e6a 79 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  y",.    /* SQLIT
33e6b 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 2a 2f  E_READONLY    */
33e6c 20 22 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69   "attempt to wri
33e6d 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61  te a readonly da
33e6e 74 61 62 61 73 65 22 2c 0a 20 20 20 20 2f 2a 20  tabase",.    /* 
33e6f 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
33e70 20 20 20 2a 2f 20 22 69 6e 74 65 72 72 75 70 74     */ "interrupt
33e71 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  ed",.    /* SQLI
33e72 54 45 5f 49 4f 45 52 52 20 20 20 20 20 20 20 2a  TE_IOERR       *
33e73 2f 20 22 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f  / "disk I/O erro
33e74 72 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  r",.    /* SQLIT
33e75 45 5f 43 4f 52 52 55 50 54 20 20 20 20 20 2a 2f  E_CORRUPT     */
33e76 20 22 64 61 74 61 62 61 73 65 20 64 69 73 6b 20   "database disk 
33e77 69 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d  image is malform
33e78 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  ed",.    /* SQLI
33e79 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20 2a  TE_NOTFOUND    *
33e7a 2f 20 30 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  / 0,.    /* SQLI
33e7b 54 45 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 2a  TE_FULL        *
33e7c 2f 20 22 64 61 74 61 62 61 73 65 20 6f 72 20 64  / "database or d
33e7d 69 73 6b 20 69 73 20 66 75 6c 6c 22 2c 0a 20 20  isk is full",.  
33e7e 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 41 4e 54    /* SQLITE_CANT
33e7f 4f 50 45 4e 20 20 20 20 2a 2f 20 22 75 6e 61 62  OPEN    */ "unab
33e80 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74 61 62  le to open datab
33e81 61 73 65 20 66 69 6c 65 22 2c 0a 20 20 20 20 2f  ase file",.    /
33e82 2a 20 53 51 4c 49 54 45 5f 50 52 4f 54 4f 43 4f  * SQLITE_PROTOCO
33e83 4c 20 20 20 20 2a 2f 20 30 2c 0a 20 20 20 20 2f  L    */ 0,.    /
33e84 2a 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 20  * SQLITE_EMPTY  
33e85 20 20 20 20 20 2a 2f 20 22 74 61 62 6c 65 20 63       */ "table c
33e86 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 61 74 61 22  ontains no data"
33e87 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f  ,.    /* SQLITE_
33e88 53 43 48 45 4d 41 20 20 20 20 20 20 2a 2f 20 22  SCHEMA      */ "
33e89 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
33e8a 68 61 73 20 63 68 61 6e 67 65 64 22 2c 0a 20 20  has changed",.  
33e8b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 54 4f 4f 42    /* SQLITE_TOOB
33e8c 49 47 20 20 20 20 20 20 2a 2f 20 22 73 74 72 69  IG      */ "stri
33e8d 6e 67 20 6f 72 20 62 6c 6f 62 20 74 6f 6f 20 62  ng or blob too b
33e8e 69 67 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  ig",.    /* SQLI
33e8f 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20 2a  TE_CONSTRAINT  *
33e90 2f 20 22 63 6f 6e 73 74 72 61 69 6e 74 20 66 61  / "constraint fa
33e91 69 6c 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51  iled",.    /* SQ
33e92 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 20 20  LITE_MISMATCH   
33e93 20 2a 2f 20 22 64 61 74 61 74 79 70 65 20 6d 69   */ "datatype mi
33e94 73 6d 61 74 63 68 22 2c 0a 20 20 20 20 2f 2a 20  smatch",.    /* 
33e95 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 20 20  SQLITE_MISUSE   
33e96 20 20 20 2a 2f 20 22 6c 69 62 72 61 72 79 20 72     */ "library r
33e97 6f 75 74 69 6e 65 20 63 61 6c 6c 65 64 20 6f 75  outine called ou
33e98 74 20 6f 66 20 73 65 71 75 65 6e 63 65 22 2c 0a  t of sequence",.
33e99 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f      /* SQLITE_NO
33e9a 4c 46 53 20 20 20 20 20 20 20 2a 2f 20 22 6c 61  LFS       */ "la
33e9b 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
33e9c 20 69 73 20 64 69 73 61 62 6c 65 64 22 2c 0a 20   is disabled",. 
33e9d 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 41 55 54     /* SQLITE_AUT
33e9e 48 20 20 20 20 20 20 20 20 2a 2f 20 22 61 75 74  H        */ "aut
33e9f 68 6f 72 69 7a 61 74 69 6f 6e 20 64 65 6e 69 65  horization denie
33ea0 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  d",.    /* SQLIT
33ea1 45 5f 46 4f 52 4d 41 54 20 20 20 20 20 20 2a 2f  E_FORMAT      */
33ea2 20 22 61 75 78 69 6c 69 61 72 79 20 64 61 74 61   "auxiliary data
33ea3 62 61 73 65 20 66 6f 72 6d 61 74 20 65 72 72 6f  base format erro
33ea4 72 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  r",.    /* SQLIT
33ea5 45 5f 52 41 4e 47 45 20 20 20 20 20 20 20 2a 2f  E_RANGE       */
33ea6 20 22 62 69 6e 64 20 6f 72 20 63 6f 6c 75 6d 6e   "bind or column
33ea7 20 69 6e 64 65 78 20 6f 75 74 20 6f 66 20 72 61   index out of ra
33ea8 6e 67 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c  nge",.    /* SQL
33ea9 49 54 45 5f 4e 4f 54 41 44 42 20 20 20 20 20 20  ITE_NOTADB      
33eaa 2a 2f 20 22 66 69 6c 65 20 69 73 20 65 6e 63 72  */ "file is encr
33eab 79 70 74 65 64 20 6f 72 20 69 73 20 6e 6f 74 20  ypted or is not 
33eac 61 20 64 61 74 61 62 61 73 65 22 2c 0a 20 20 7d  a database",.  }
33ead 3b 0a 20 20 72 63 20 26 3d 20 30 78 66 66 3b 0a  ;.  rc &= 0xff;.
33eae 20 20 69 66 28 20 41 4c 57 41 59 53 28 72 63 3e    if( ALWAYS(rc>
33eaf 3d 30 29 20 26 26 20 72 63 3c 28 69 6e 74 29 28  =0) && rc<(int)(
33eb0 73 69 7a 65 6f 66 28 61 4d 73 67 29 2f 73 69 7a  sizeof(aMsg)/siz
33eb1 65 6f 66 28 61 4d 73 67 5b 30 5d 29 29 20 26 26  eof(aMsg[0])) &&
33eb2 20 61 4d 73 67 5b 72 63 5d 21 3d 30 20 29 7b 0a   aMsg[rc]!=0 ){.
33eb3 20 20 20 20 72 65 74 75 72 6e 20 61 4d 73 67 5b      return aMsg[
33eb4 72 63 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  rc];.  }else{.  
33eb5 20 20 72 65 74 75 72 6e 20 22 75 6e 6b 6e 6f 77    return "unknow
33eb6 6e 20 65 72 72 6f 72 22 3b 0a 20 20 7d 0a 7d 0a  n error";.  }.}.
33eb7 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
33eb8 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  ine implements a
33eb9 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 74   busy callback t
33eba 68 61 74 20 73 6c 65 65 70 73 20 61 6e 64 20 74  hat sleeps and t
33ebb 72 69 65 73 0a 2a 2a 20 61 67 61 69 6e 20 75 6e  ries.** again un
33ebc 74 69 6c 20 61 20 74 69 6d 65 6f 75 74 20 76 61  til a timeout va
33ebd 6c 75 65 20 69 73 20 72 65 61 63 68 65 64 2e 20  lue is reached. 
33ebe 20 54 68 65 20 74 69 6d 65 6f 75 74 20 76 61 6c   The timeout val
33ebf 75 65 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65  ue is.** an inte
33ec0 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  ger number of mi
33ec1 6c 6c 69 73 65 63 6f 6e 64 73 20 70 61 73 73 65  lliseconds passe
33ec2 64 20 69 6e 20 61 73 20 74 68 65 20 66 69 72 73  d in as the firs
33ec3 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a  t.** argument..*
33ec4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
33ec5 69 74 65 44 65 66 61 75 6c 74 42 75 73 79 43 61  iteDefaultBusyCa
33ec6 6c 6c 62 61 63 6b 28 0a 20 76 6f 69 64 20 2a 70  llback(. void *p
33ec7 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  tr,             
33ec8 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
33ec9 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 69 6e 74  nnection */. int
33eca 20 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20 20   count          
33ecb 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
33ecc 6f 66 20 74 69 6d 65 73 20 74 61 62 6c 65 20 68  of times table h
33ecd 61 73 20 62 65 65 6e 20 62 75 73 79 20 2a 2f 0a  as been busy */.
33ece 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  ){.#if SQLITE_OS
33ecf 5f 57 49 4e 20 7c 7c 20 28 64 65 66 69 6e 65 64  _WIN || (defined
33ed0 28 48 41 56 45 5f 55 53 4c 45 45 50 29 20 26 26  (HAVE_USLEEP) &&
33ed1 20 48 41 56 45 5f 55 53 4c 45 45 50 29 0a 20 20   HAVE_USLEEP).  
33ed2 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
33ed3 64 65 6c 61 79 73 5b 5d 20 3d 0a 20 20 20 20 20  delays[] =.     
33ed4 7b 20 31 2c 20 32 2c 20 35 2c 20 31 30 2c 20 31  { 1, 2, 5, 10, 1
33ed5 35 2c 20 32 30 2c 20 32 35 2c 20 32 35 2c 20 20  5, 20, 25, 25,  
33ed6 32 35 2c 20 20 35 30 2c 20 20 35 30 2c 20 31 30  25,  50,  50, 10
33ed7 30 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f  0 };.  static co
33ed8 6e 73 74 20 75 38 20 74 6f 74 61 6c 73 5b 5d 20  nst u8 totals[] 
33ed9 3d 0a 20 20 20 20 20 7b 20 30 2c 20 31 2c 20 33  =.     { 0, 1, 3
33eda 2c 20 20 38 2c 20 31 38 2c 20 33 33 2c 20 35 33  ,  8, 18, 33, 53
33edb 2c 20 37 38 2c 20 31 30 33 2c 20 31 32 38 2c 20  , 78, 103, 128, 
33edc 31 37 38 2c 20 32 32 38 20 7d 3b 0a 23 20 64 65  178, 228 };.# de
33edd 66 69 6e 65 20 4e 44 45 4c 41 59 20 28 73 69 7a  fine NDELAY (siz
33ede 65 6f 66 28 64 65 6c 61 79 73 29 2f 73 69 7a 65  eof(delays)/size
33edf 6f 66 28 64 65 6c 61 79 73 5b 30 5d 29 29 0a 20  of(delays[0])). 
33ee0 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28   sqlite3 *db = (
33ee1 73 71 6c 69 74 65 33 20 2a 29 70 74 72 3b 0a 20  sqlite3 *)ptr;. 
33ee2 20 69 6e 74 20 74 69 6d 65 6f 75 74 20 3d 20 64   int timeout = d
33ee3 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74 3b 0a  b->busyTimeout;.
33ee4 20 20 69 6e 74 20 64 65 6c 61 79 2c 20 70 72 69    int delay, pri
33ee5 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 63  or;..  assert( c
33ee6 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28  ount>=0 );.  if(
33ee7 20 63 6f 75 6e 74 20 3c 20 4e 44 45 4c 41 59 20   count < NDELAY 
33ee8 29 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20 64  ){.    delay = d
33ee9 65 6c 61 79 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20  elays[count];.  
33eea 20 20 70 72 69 6f 72 20 3d 20 74 6f 74 61 6c 73    prior = totals
33eeb 5b 63 6f 75 6e 74 5d 3b 0a 20 20 7d 65 6c 73 65  [count];.  }else
33eec 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20 64 65  {.    delay = de
33eed 6c 61 79 73 5b 4e 44 45 4c 41 59 2d 31 5d 3b 0a  lays[NDELAY-1];.
33eee 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f 74 61      prior = tota
33eef 6c 73 5b 4e 44 45 4c 41 59 2d 31 5d 20 2b 20 64  ls[NDELAY-1] + d
33ef0 65 6c 61 79 2a 28 63 6f 75 6e 74 2d 28 4e 44 45  elay*(count-(NDE
33ef1 4c 41 59 2d 31 29 29 3b 0a 20 20 7d 0a 20 20 69  LAY-1));.  }.  i
33ef2 66 28 20 70 72 69 6f 72 20 2b 20 64 65 6c 61 79  f( prior + delay
33ef3 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b 0a 20 20   > timeout ){.  
33ef4 20 20 64 65 6c 61 79 20 3d 20 74 69 6d 65 6f 75    delay = timeou
33ef5 74 20 2d 20 70 72 69 6f 72 3b 0a 20 20 20 20 69  t - prior;.    i
33ef6 66 28 20 64 65 6c 61 79 3c 3d 30 20 29 20 72 65  f( delay<=0 ) re
33ef7 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71  turn 0;.  }.  sq
33ef8 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 64 62 2d  lite3OsSleep(db-
33ef9 3e 70 56 66 73 2c 20 64 65 6c 61 79 2a 31 30 30  >pVfs, delay*100
33efa 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a  0);.  return 1;.
33efb 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 20  #else.  sqlite3 
33efc 2a 64 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a  *db = (sqlite3 *
33efd 29 70 74 72 3b 0a 20 20 69 6e 74 20 74 69 6d 65  )ptr;.  int time
33efe 6f 75 74 20 3d 20 28 28 73 71 6c 69 74 65 33 20  out = ((sqlite3 
33eff 2a 29 70 74 72 29 2d 3e 62 75 73 79 54 69 6d 65  *)ptr)->busyTime
33f00 6f 75 74 3b 0a 20 20 69 66 28 20 28 63 6f 75 6e  out;.  if( (coun
33f01 74 2b 31 29 2a 31 30 30 30 20 3e 20 74 69 6d 65  t+1)*1000 > time
33f02 6f 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  out ){.    retur
33f03 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  n 0;.  }.  sqlit
33f04 65 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e 70 56  e3OsSleep(db->pV
33f05 66 73 2c 20 31 30 30 30 30 30 30 29 3b 0a 20 20  fs, 1000000);.  
33f06 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66  return 1;.#endif
33f07 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65  .}../*.** Invoke
33f08 20 74 68 65 20 67 69 76 65 6e 20 62 75 73 79 20   the given busy 
33f09 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  handler..**.** T
33f0a 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
33f0b 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 6f 70  alled when an op
33f0c 65 72 61 74 69 6f 6e 20 66 61 69 6c 65 64 20 77  eration failed w
33f0d 69 74 68 20 61 20 6c 6f 63 6b 2e 0a 2a 2a 20 49  ith a lock..** I
33f0e 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  f this routine r
33f0f 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  eturns non-zero,
33f10 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 72 65 74   the lock is ret
33f11 72 69 65 64 2e 20 20 49 66 20 69 74 0a 2a 2a 20  ried.  If it.** 
33f12 72 65 74 75 72 6e 73 20 30 2c 20 74 68 65 20 6f  returns 0, the o
33f13 70 65 72 61 74 69 6f 6e 20 61 62 6f 72 74 73 20  peration aborts 
33f14 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 42  with an SQLITE_B
33f15 55 53 59 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51  USY error..*/.SQ
33f16 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
33f17 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75   sqlite3InvokeBu
33f18 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79 48 61  syHandler(BusyHa
33f19 6e 64 6c 65 72 20 2a 70 29 7b 0a 20 20 69 6e 74  ndler *p){.  int
33f1a 20 72 63 3b 0a 20 20 69 66 28 20 4e 45 56 45 52   rc;.  if( NEVER
33f1b 28 70 3d 3d 30 29 20 7c 7c 20 70 2d 3e 78 46 75  (p==0) || p->xFu
33f1c 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 42 75 73  nc==0 || p->nBus
33f1d 79 3c 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  y<0 ) return 0;.
33f1e 20 20 72 63 20 3d 20 70 2d 3e 78 46 75 6e 63 28    rc = p->xFunc(
33f1f 70 2d 3e 70 41 72 67 2c 20 70 2d 3e 6e 42 75 73  p->pArg, p->nBus
33f20 79 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 30 20  y);.  if( rc==0 
33f21 29 7b 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79 20  ){.    p->nBusy 
33f22 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  = -1;.  }else{. 
33f23 20 20 20 70 2d 3e 6e 42 75 73 79 2b 2b 3b 0a 20     p->nBusy++;. 
33f24 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 20   }.  return rc; 
33f25 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
33f26 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
33f27 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 66 6f  busy callback fo
33f28 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61 74 61  r an Sqlite data
33f29 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20 67  base to the.** g
33f2a 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75  iven callback fu
33f2b 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
33f2c 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74 2e 0a  given argument..
33f2d 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
33f2e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68  t sqlite3_busy_h
33f2f 61 6e 64 6c 65 72 28 0a 20 20 73 71 6c 69 74 65  andler(.  sqlite
33f30 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a 78  3 *db,.  int (*x
33f31 42 75 73 79 29 28 76 6f 69 64 2a 2c 69 6e 74 29  Busy)(void*,int)
33f32 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29  ,.  void *pArg.)
33f33 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
33f34 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
33f35 78 29 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61  x);.  db->busyHa
33f36 6e 64 6c 65 72 2e 78 46 75 6e 63 20 3d 20 78 42  ndler.xFunc = xB
33f37 75 73 79 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48  usy;.  db->busyH
33f38 61 6e 64 6c 65 72 2e 70 41 72 67 20 3d 20 70 41  andler.pArg = pA
33f39 72 67 3b 0a 20 20 64 62 2d 3e 62 75 73 79 48 61  rg;.  db->busyHa
33f3a 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b  ndler.nBusy = 0;
33f3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
33f3c 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
33f3d 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
33f3e 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  TE_OK;.}..#ifnde
33f3f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52  f SQLITE_OMIT_PR
33f40 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a  OGRESS_CALLBACK.
33f41 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
33f42 6e 65 20 73 65 74 73 20 74 68 65 20 70 72 6f 67  ne sets the prog
33f43 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 66 6f  ress callback fo
33f44 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61 74 61  r an Sqlite data
33f45 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a 20 67  base to the.** g
33f46 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20 66 75  iven callback fu
33f47 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
33f48 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74 2e 20  given argument. 
33f49 54 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  The progress cal
33f4a 6c 62 61 63 6b 20 77 69 6c 6c 0a 2a 2a 20 62 65  lback will.** be
33f4b 20 69 6e 76 6f 6b 65 64 20 65 76 65 72 79 20 6e   invoked every n
33f4c 4f 70 73 20 6f 70 63 6f 64 65 73 2e 0a 2a 2f 0a  Ops opcodes..*/.
33f4d 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
33f4e 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
33f4f 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71 6c 69  _handler(.  sqli
33f50 74 65 33 20 2a 64 62 2c 20 0a 20 20 69 6e 74 20  te3 *db, .  int 
33f51 6e 4f 70 73 2c 0a 20 20 69 6e 74 20 28 2a 78 50  nOps,.  int (*xP
33f52 72 6f 67 72 65 73 73 29 28 76 6f 69 64 2a 29 2c  rogress)(void*),
33f53 20 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29   .  void *pArg.)
33f54 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
33f55 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
33f56 78 29 3b 0a 20 20 69 66 28 20 6e 4f 70 73 3e 30  x);.  if( nOps>0
33f57 20 29 7b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f   ){.    db->xPro
33f58 67 72 65 73 73 20 3d 20 78 50 72 6f 67 72 65 73  gress = xProgres
33f59 73 3b 0a 20 20 20 20 64 62 2d 3e 6e 50 72 6f 67  s;.    db->nProg
33f5a 72 65 73 73 4f 70 73 20 3d 20 6e 4f 70 73 3b 0a  ressOps = nOps;.
33f5b 20 20 20 20 64 62 2d 3e 70 50 72 6f 67 72 65 73      db->pProgres
33f5c 73 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 7d  sArg = pArg;.  }
33f5d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 78 50  else{.    db->xP
33f5e 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a 20 20 20  rogress = 0;.   
33f5f 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70   db->nProgressOp
33f60 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70  s = 0;.    db->p
33f61 50 72 6f 67 72 65 73 73 41 72 67 20 3d 20 30 3b  ProgressArg = 0;
33f62 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
33f63 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
33f64 75 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  utex);.}.#endif.
33f65 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
33f66 74 69 6e 65 20 69 6e 73 74 61 6c 6c 73 20 61 20  tine installs a 
33f67 64 65 66 61 75 6c 74 20 62 75 73 79 20 68 61 6e  default busy han
33f68 64 6c 65 72 20 74 68 61 74 20 77 61 69 74 73 20  dler that waits 
33f69 66 6f 72 20 74 68 65 0a 2a 2a 20 73 70 65 63 69  for the.** speci
33f6a 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 6d  fied number of m
33f6b 69 6c 6c 69 73 65 63 6f 6e 64 73 20 62 65 66 6f  illiseconds befo
33f6c 72 65 20 72 65 74 75 72 6e 69 6e 67 20 30 2e 0a  re returning 0..
33f6d 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
33f6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74  t sqlite3_busy_t
33f6f 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 20 2a  imeout(sqlite3 *
33f70 64 62 2c 20 69 6e 74 20 6d 73 29 7b 0a 20 20 69  db, int ms){.  i
33f71 66 28 20 6d 73 3e 30 20 29 7b 0a 20 20 20 20 64  f( ms>0 ){.    d
33f72 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74 20 3d  b->busyTimeout =
33f73 20 6d 73 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   ms;.    sqlite3
33f74 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 64 62  _busy_handler(db
33f75 2c 20 73 71 6c 69 74 65 44 65 66 61 75 6c 74 42  , sqliteDefaultB
33f76 75 73 79 43 61 6c 6c 62 61 63 6b 2c 20 28 76 6f  usyCallback, (vo
33f77 69 64 2a 29 64 62 29 3b 0a 20 20 7d 65 6c 73 65  id*)db);.  }else
33f78 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 75  {.    sqlite3_bu
33f79 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c 20 30  sy_handler(db, 0
33f7a 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
33f7b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
33f7c 0a 2f 2a 0a 2a 2a 20 43 61 75 73 65 20 61 6e 79  ./*.** Cause any
33f7d 20 70 65 6e 64 69 6e 67 20 6f 70 65 72 61 74 69   pending operati
33f7e 6f 6e 20 74 6f 20 73 74 6f 70 20 61 74 20 69 74  on to stop at it
33f7f 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70 6f 72  s earliest oppor
33f80 74 75 6e 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54  tunity..*/.SQLIT
33f81 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
33f82 65 33 5f 69 6e 74 65 72 72 75 70 74 28 73 71 6c  e3_interrupt(sql
33f83 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64 62 2d  ite3 *db){.  db-
33f84 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65  >u1.isInterrupte
33f85 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  d = 1;.}.../*.**
33f86 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
33f87 73 20 65 78 61 63 74 6c 79 20 74 68 65 20 73 61  s exactly the sa
33f88 6d 65 20 61 73 20 73 71 6c 69 74 65 33 5f 63 72  me as sqlite3_cr
33f89 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 2c  eate_function(),
33f8a 20 65 78 63 65 70 74 0a 2a 2a 20 74 68 61 74 20   except.** that 
33f8b 69 74 20 69 73 20 64 65 73 69 67 6e 65 64 20 74  it is designed t
33f8c 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 69  o be called by i
33f8d 6e 74 65 72 6e 61 6c 20 63 6f 64 65 2e 20 54 68  nternal code. Th
33f8e 65 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 0a  e difference is.
33f8f 2a 2a 20 74 68 61 74 20 69 66 20 61 20 6d 61 6c  ** that if a mal
33f90 6c 6f 63 28 29 20 66 61 69 6c 73 20 69 6e 20 73  loc() fails in s
33f91 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
33f92 6e 63 74 69 6f 6e 28 29 2c 20 61 6e 20 65 72 72  nction(), an err
33f93 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65  or code.** is re
33f94 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 6d  turned and the m
33f95 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
33f96 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2f 0a 53 51   cleared. .*/.SQ
33f97 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
33f98 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
33f99 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  nc(.  sqlite3 *d
33f9a 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b,.  const char 
33f9b 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a  *zFunctionName,.
33f9c 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e    int nArg,.  in
33f9d 74 20 65 6e 63 2c 0a 20 20 76 6f 69 64 20 2a 70  t enc,.  void *p
33f9e 55 73 65 72 44 61 74 61 2c 0a 20 20 76 6f 69 64  UserData,.  void
33f9f 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
33fa0 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
33fa1 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29  qlite3_value **)
33fa2 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70  ,.  void (*xStep
33fa3 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
33fa4 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
33fa5 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64  alue **),.  void
33fa6 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74   (*xFinal)(sqlit
33fa7 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a  e3_context*).){.
33fa8 20 20 46 75 6e 63 44 65 66 20 2a 70 3b 0a 20 20    FuncDef *p;.  
33fa9 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73  int nName;..  as
33faa 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
33fab 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
33fac 65 78 29 20 29 3b 0a 20 20 69 66 28 20 7a 46 75  ex) );.  if( zFu
33fad 6e 63 74 69 6f 6e 4e 61 6d 65 3d 3d 30 20 7c 7c  nctionName==0 ||
33fae 0a 20 20 20 20 20 20 28 78 46 75 6e 63 20 26 26  .      (xFunc &&
33faf 20 28 78 46 69 6e 61 6c 20 7c 7c 20 78 53 74 65   (xFinal || xSte
33fb0 70 29 29 20 7c 7c 20 0a 20 20 20 20 20 20 28 21  p)) || .      (!
33fb1 78 46 75 6e 63 20 26 26 20 28 78 46 69 6e 61 6c  xFunc && (xFinal
33fb2 20 26 26 20 21 78 53 74 65 70 29 29 20 7c 7c 0a   && !xStep)) ||.
33fb3 20 20 20 20 20 20 28 21 78 46 75 6e 63 20 26 26        (!xFunc &&
33fb4 20 28 21 78 46 69 6e 61 6c 20 26 26 20 78 53 74   (!xFinal && xSt
33fb5 65 70 29 29 20 7c 7c 0a 20 20 20 20 20 20 28 6e  ep)) ||.      (n
33fb6 41 72 67 3c 2d 31 20 7c 7c 20 6e 41 72 67 3e 53  Arg<-1 || nArg>S
33fb7 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49  QLITE_MAX_FUNCTI
33fb8 4f 4e 5f 41 52 47 29 20 7c 7c 0a 20 20 20 20 20  ON_ARG) ||.     
33fb9 20 28 32 35 35 3c 28 6e 4e 61 6d 65 20 3d 20 73   (255<(nName = s
33fba 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 20  qlite3Strlen30( 
33fbb 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 29 29 29  zFunctionName)))
33fbc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
33fbd 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
33fbe 7d 0a 20 20 0a 23 69 66 6e 64 65 66 20 53 51 4c  }.  .#ifndef SQL
33fbf 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20  ITE_OMIT_UTF16. 
33fc0 20 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 55 54   /* If SQLITE_UT
33fc1 46 31 36 20 69 73 20 73 70 65 63 69 66 69 65 64  F16 is specified
33fc2 20 61 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67   as the encoding
33fc3 20 74 79 70 65 2c 20 74 72 61 6e 73 66 6f 72 6d   type, transform
33fc4 20 74 68 69 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e   this.  ** to on
33fc5 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31  e of SQLITE_UTF1
33fc6 36 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f 55 54  6LE or SQLITE_UT
33fc7 46 31 36 42 45 20 75 73 69 6e 67 20 74 68 65 0a  F16BE using the.
33fc8 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 31    ** SQLITE_UTF1
33fc9 36 4e 41 54 49 56 45 20 6d 61 63 72 6f 2e 20 53  6NATIVE macro. S
33fca 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 6e  QLITE_UTF16 is n
33fcb 6f 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c  ot used internal
33fcc 6c 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  ly..  **.  ** If
33fcd 20 53 51 4c 49 54 45 5f 41 4e 59 20 69 73 20 73   SQLITE_ANY is s
33fce 70 65 63 69 66 69 65 64 2c 20 61 64 64 20 74 68  pecified, add th
33fcf 72 65 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  ree versions of 
33fd0 74 68 65 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a  the function.  *
33fd1 2a 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  * to the hash ta
33fd2 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ble..  */.  if( 
33fd3 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  enc==SQLITE_UTF1
33fd4 36 20 29 7b 0a 20 20 20 20 65 6e 63 20 3d 20 53  6 ){.    enc = S
33fd5 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
33fd6 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 65  E;.  }else if( e
33fd7 6e 63 3d 3d 53 51 4c 49 54 45 5f 41 4e 59 20 29  nc==SQLITE_ANY )
33fd8 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
33fd9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72    rc = sqlite3Cr
33fda 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75  eateFunc(db, zFu
33fdb 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67  nctionName, nArg
33fdc 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a 20  , SQLITE_UTF8,. 
33fdd 20 20 20 20 20 20 20 20 70 55 73 65 72 44 61 74          pUserDat
33fde 61 2c 20 78 46 75 6e 63 2c 20 78 53 74 65 70 2c  a, xFunc, xStep,
33fdf 20 78 46 69 6e 61 6c 29 3b 0a 20 20 20 20 69 66   xFinal);.    if
33fe0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
33fe1 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
33fe2 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28  lite3CreateFunc(
33fe3 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d  db, zFunctionNam
33fe4 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f  e, nArg, SQLITE_
33fe5 55 54 46 31 36 4c 45 2c 0a 20 20 20 20 20 20 20  UTF16LE,.       
33fe6 20 20 20 70 55 73 65 72 44 61 74 61 2c 20 78 46     pUserData, xF
33fe7 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e  unc, xStep, xFin
33fe8 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  al);.    }.    i
33fe9 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
33fea 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
33feb 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65   rc;.    }.    e
33fec 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31  nc = SQLITE_UTF1
33fed 36 42 45 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  6BE;.  }.#else. 
33fee 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54   enc = SQLITE_UT
33fef 46 38 3b 0a 23 65 6e 64 69 66 0a 20 20 0a 20 20  F8;.#endif.  .  
33ff0 2f 2a 20 43 68 65 63 6b 20 69 66 20 61 6e 20 65  /* Check if an e
33ff1 78 69 73 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  xisting function
33ff2 20 69 73 20 62 65 69 6e 67 20 6f 76 65 72 72 69   is being overri
33ff3 64 64 65 6e 20 6f 72 20 64 65 6c 65 74 65 64 2e  dden or deleted.
33ff4 20 49 66 20 73 6f 2c 0a 20 20 2a 2a 20 61 6e 64   If so,.  ** and
33ff5 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69 76   there are activ
33ff6 65 20 56 4d 73 2c 20 74 68 65 6e 20 72 65 74 75  e VMs, then retu
33ff7 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20  rn SQLITE_BUSY. 
33ff8 49 66 20 61 20 66 75 6e 63 74 69 6f 6e 0a 20 20  If a function.  
33ff9 2a 2a 20 69 73 20 62 65 69 6e 67 20 6f 76 65 72  ** is being over
33ffa 72 69 64 64 65 6e 2f 64 65 6c 65 74 65 64 20 62  ridden/deleted b
33ffb 75 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  ut there are no 
33ffc 61 63 74 69 76 65 20 56 4d 73 2c 20 61 6c 6c 6f  active VMs, allo
33ffd 77 20 74 68 65 0a 20 20 2a 2a 20 6f 70 65 72 61  w the.  ** opera
33ffe 74 69 6f 6e 20 74 6f 20 63 6f 6e 74 69 6e 75 65  tion to continue
33fff 20 62 75 74 20 69 6e 76 61 6c 69 64 61 74 65 20   but invalidate 
34000 61 6c 6c 20 70 72 65 63 6f 6d 70 69 6c 65 64 20  all precompiled 
34001 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 2a 2f  statements..  */
34002 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69  .  p = sqlite3Fi
34003 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  ndFunction(db, z
34004 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e  FunctionName, nN
34005 61 6d 65 2c 20 6e 41 72 67 2c 20 28 75 38 29 65  ame, nArg, (u8)e
34006 6e 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20  nc, 0);.  if( p 
34007 26 26 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d  && p->iPrefEnc==
34008 65 6e 63 20 26 26 20 70 2d 3e 6e 41 72 67 3d 3d  enc && p->nArg==
34009 6e 41 72 67 20 29 7b 0a 20 20 20 20 69 66 28 20  nArg ){.    if( 
3400a 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
3400b 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
3400c 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
3400d 54 45 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20  TE_BUSY, .      
3400e 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c    "unable to del
3400f 65 74 65 2f 6d 6f 64 69 66 79 20 75 73 65 72 2d  ete/modify user-
34010 66 75 6e 63 74 69 6f 6e 20 64 75 65 20 74 6f 20  function due to 
34011 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74  active statement
34012 73 22 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  s");.      asser
34013 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  t( !db->mallocFa
34014 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 72 65  iled );.      re
34015 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
34016 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
34017 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
34018 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
34019 74 73 28 64 62 29 3b 0a 20 20 20 20 7d 0a 20 20  ts(db);.    }.  
3401a 7d 0a 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  }..  p = sqlite3
3401b 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c  FindFunction(db,
3401c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20   zFunctionName, 
3401d 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 28 75 38  nName, nArg, (u8
3401e 29 65 6e 63 2c 20 31 29 3b 0a 20 20 61 73 73 65  )enc, 1);.  asse
3401f 72 74 28 70 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  rt(p || db->mall
34020 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 69 66 28  ocFailed);.  if(
34021 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72   !p ){.    retur
34022 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
34023 20 20 7d 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d    }.  p->flags =
34024 20 30 3b 0a 20 20 70 2d 3e 78 46 75 6e 63 20 3d   0;.  p->xFunc =
34025 20 78 46 75 6e 63 3b 0a 20 20 70 2d 3e 78 53 74   xFunc;.  p->xSt
34026 65 70 20 3d 20 78 53 74 65 70 3b 0a 20 20 70 2d  ep = xStep;.  p-
34027 3e 78 46 69 6e 61 6c 69 7a 65 20 3d 20 78 46 69  >xFinalize = xFi
34028 6e 61 6c 3b 0a 20 20 70 2d 3e 70 55 73 65 72 44  nal;.  p->pUserD
34029 61 74 61 20 3d 20 70 55 73 65 72 44 61 74 61 3b  ata = pUserData;
3402a 0a 20 20 70 2d 3e 6e 41 72 67 20 3d 20 28 75 31  .  p->nArg = (u1
3402b 36 29 6e 41 72 67 3b 0a 20 20 72 65 74 75 72 6e  6)nArg;.  return
3402c 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
3402d 2a 0a 2a 2a 20 43 72 65 61 74 65 20 6e 65 77 20  *.** Create new 
3402e 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  user functions..
3402f 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
34030 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
34031 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c  _function(.  sql
34032 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73  ite3 *db,.  cons
34033 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 74 69 6f  t char *zFunctio
34034 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72  nName,.  int nAr
34035 67 2c 0a 20 20 69 6e 74 20 65 6e 63 2c 0a 20 20  g,.  int enc,.  
34036 76 6f 69 64 20 2a 70 2c 0a 20 20 76 6f 69 64 20  void *p,.  void 
34037 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33  (*xFunc)(sqlite3
34038 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
34039 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 2c  lite3_value **),
3403a 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29  .  void (*xStep)
3403b 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
3403c 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
3403d 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20  lue **),.  void 
3403e 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65  (*xFinal)(sqlite
3403f 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 7b 0a 20  3_context*).){. 
34040 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
34041 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
34042 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20  b->mutex);.  rc 
34043 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46  = sqlite3CreateF
34044 75 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f  unc(db, zFunctio
34045 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 65 6e 63  nName, nArg, enc
34046 2c 20 70 2c 20 78 46 75 6e 63 2c 20 78 53 74 65  , p, xFunc, xSte
34047 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20 72 63  p, xFinal);.  rc
34048 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
34049 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
3404a 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
3404b 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
3404c 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66  eturn rc;.}..#if
3404d 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
3404e 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
3404f 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72  I int sqlite3_cr
34050 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28  eate_function16(
34051 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
34052 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46    const void *zF
34053 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69  unctionName,.  i
34054 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65  nt nArg,.  int e
34055 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64 20  TextRep,.  void 
34056 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  *p,.  void (*xFu
34057 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
34058 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
34059 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
3405a 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
3405b 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
3405c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
3405d 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61  ,.  void (*xFina
3405e 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
3405f 78 74 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63  xt*).){.  int rc
34060 3b 0a 20 20 63 68 61 72 20 2a 7a 46 75 6e 63 38  ;.  char *zFunc8
34061 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
34062 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
34063 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64  x);.  assert( !d
34064 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
34065 29 3b 0a 20 20 7a 46 75 6e 63 38 20 3d 20 73 71  );.  zFunc8 = sq
34066 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 64 62  lite3Utf16to8(db
34067 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c  , zFunctionName,
34068 20 2d 31 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c   -1);.  rc = sql
34069 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
3406a 62 2c 20 7a 46 75 6e 63 38 2c 20 6e 41 72 67 2c  b, zFunc8, nArg,
3406b 20 65 54 65 78 74 52 65 70 2c 20 70 2c 20 78 46   eTextRep, p, xF
3406c 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e  unc, xStep, xFin
3406d 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  al);.  sqlite3Db
3406e 46 72 65 65 28 64 62 2c 20 7a 46 75 6e 63 38 29  Free(db, zFunc8)
3406f 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
34070 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
34071 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
34072 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
34073 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
34074 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  }.#endif.../*.**
34075 20 44 65 63 6c 61 72 65 20 74 68 61 74 20 61 20   Declare that a 
34076 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 62 65 65  function has bee
34077 6e 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20  n overloaded by 
34078 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
34079 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 75  .**.** If the fu
3407a 6e 63 74 69 6f 6e 20 61 6c 72 65 61 64 79 20 65  nction already e
3407b 78 69 73 74 73 20 61 73 20 61 20 72 65 67 75 6c  xists as a regul
3407c 61 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69  ar global functi
3407d 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73  on, then.** this
3407e 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
3407f 2d 6f 70 2e 20 20 49 66 20 74 68 65 20 66 75 6e  -op.  If the fun
34080 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65  ction does not e
34081 78 69 73 74 2c 20 74 68 65 6e 20 63 72 65 61 74  xist, then creat
34082 65 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e 65 20 74  e.** a new one t
34083 68 61 74 20 61 6c 77 61 79 73 20 74 68 72 6f 77  hat always throw
34084 73 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72  s a run-time err
34085 6f 72 2e 20 20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  or.  .**.** When
34086 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
34087 69 6e 74 65 6e 64 20 74 6f 20 70 72 6f 76 69 64  intend to provid
34088 65 20 61 6e 20 6f 76 65 72 6c 6f 61 64 65 64 20  e an overloaded 
34089 66 75 6e 63 74 69 6f 6e 2c 20 74 68 65 79 0a 2a  function, they.*
3408a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 74 68  * should call th
3408b 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 6d 61  is routine to ma
3408c 6b 65 20 73 75 72 65 20 74 68 65 20 67 6c 6f 62  ke sure the glob
3408d 61 6c 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73  al function exis
3408e 74 73 2e 0a 2a 2a 20 41 20 67 6c 6f 62 61 6c 20  ts..** A global 
3408f 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20 65 78  function must ex
34090 69 73 74 20 69 6e 20 6f 72 64 65 72 20 66 6f 72  ist in order for
34091 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e   name resolution
34092 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 70 72 6f 70   to work.** prop
34093 65 72 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  erly..*/.SQLITE_
34094 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
34095 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f  overload_functio
34096 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n(.  sqlite3 *db
34097 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
34098 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72  zName,.  int nAr
34099 67 0a 29 7b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  g.){.  int nName
3409a 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
3409b 33 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74  30(zName);.  int
3409c 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d   rc;.  sqlite3_m
3409d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
3409e 75 74 65 78 29 3b 0a 20 20 69 66 28 20 73 71 6c  utex);.  if( sql
3409f 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
340a0 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  (db, zName, nNam
340a1 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f  e, nArg, SQLITE_
340a2 55 54 46 38 2c 20 30 29 3d 3d 30 20 29 7b 0a 20  UTF8, 0)==0 ){. 
340a3 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 74 65     sqlite3Create
340a4 46 75 6e 63 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  Func(db, zName, 
340a5 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
340a6 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  8,.             
340a7 20 20 20 20 20 20 20 20 20 30 2c 20 73 71 6c 69           0, sqli
340a8 74 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69  te3InvalidFuncti
340a9 6f 6e 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20  on, 0, 0);.  }. 
340aa 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
340ab 45 78 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f  Exit(db, SQLITE_
340ac 4f 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  OK);.  sqlite3_m
340ad 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
340ae 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
340af 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  rc;.}..#ifndef S
340b0 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
340b1 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
340b2 61 20 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e  a trace function
340b3 2e 20 20 54 68 65 20 70 41 72 67 20 66 72 6f 6d  .  The pArg from
340b4 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20   the previously 
340b5 72 65 67 69 73 74 65 72 65 64 20 74 72 61 63 65  registered trace
340b6 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e  .** is returned.
340b7 20 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20    .**.** A NULL 
340b8 74 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 20 6d  trace function m
340b9 65 61 6e 73 20 74 68 61 74 20 6e 6f 20 74 72 61  eans that no tra
340ba 63 69 6e 67 20 69 73 20 65 78 65 63 75 74 65 73  cing is executes
340bb 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a  .  A non-NULL.**
340bc 20 74 72 61 63 65 20 69 73 20 61 20 70 6f 69 6e   trace is a poin
340bd 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
340be 6e 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65  n that is invoke
340bf 64 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f  d at the start o
340c0 66 20 65 61 63 68 0a 2a 2a 20 53 51 4c 20 73 74  f each.** SQL st
340c1 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49  atement..*/.SQLI
340c2 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
340c3 69 74 65 33 5f 74 72 61 63 65 28 73 71 6c 69 74  ite3_trace(sqlit
340c4 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 28 2a 78  e3 *db, void (*x
340c5 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  Trace)(void*,con
340c6 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 20  st char*), void 
340c7 2a 70 41 72 67 29 7b 0a 20 20 76 6f 69 64 20 2a  *pArg){.  void *
340c8 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pOld;.  sqlite3_
340c9 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
340ca 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d  mutex);.  pOld =
340cb 20 64 62 2d 3e 70 54 72 61 63 65 41 72 67 3b 0a   db->pTraceArg;.
340cc 20 20 64 62 2d 3e 78 54 72 61 63 65 20 3d 20 78    db->xTrace = x
340cd 54 72 61 63 65 3b 0a 20 20 64 62 2d 3e 70 54 72  Trace;.  db->pTr
340ce 61 63 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  aceArg = pArg;. 
340cf 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
340d0 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
340d1 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a  .  return pOld;.
340d2 7d 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  }./*.** Register
340d3 20 61 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74   a profile funct
340d4 69 6f 6e 2e 20 20 54 68 65 20 70 41 72 67 20 66  ion.  The pArg f
340d5 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73  rom the previous
340d6 6c 79 20 72 65 67 69 73 74 65 72 65 64 20 0a 2a  ly registered .*
340d7 2a 20 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69  * profile functi
340d8 6f 6e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  on is returned. 
340d9 20 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 70   .**.** A NULL p
340da 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20  rofile function 
340db 6d 65 61 6e 73 20 74 68 61 74 20 6e 6f 20 70 72  means that no pr
340dc 6f 66 69 6c 69 6e 67 20 69 73 20 65 78 65 63 75  ofiling is execu
340dd 74 65 73 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c  tes.  A non-NULL
340de 0a 2a 2a 20 70 72 6f 66 69 6c 65 20 69 73 20 61  .** profile is a
340df 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66 75   pointer to a fu
340e0 6e 63 74 69 6f 6e 20 74 68 61 74 20 69 73 20 69  nction that is i
340e1 6e 76 6f 6b 65 64 20 61 74 20 74 68 65 20 63 6f  nvoked at the co
340e2 6e 63 6c 75 73 69 6f 6e 20 6f 66 0a 2a 2a 20 65  nclusion of.** e
340e3 61 63 68 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  ach SQL statemen
340e4 74 20 74 68 61 74 20 69 73 20 72 75 6e 2e 0a 2a  t that is run..*
340e5 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
340e6 64 20 2a 73 71 6c 69 74 65 33 5f 70 72 6f 66 69  d *sqlite3_profi
340e7 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  le(.  sqlite3 *d
340e8 62 2c 0a 20 20 76 6f 69 64 20 28 2a 78 50 72 6f  b,.  void (*xPro
340e9 66 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  file)(void*,cons
340ea 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75  t char*,sqlite_u
340eb 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a  int64),.  void *
340ec 70 41 72 67 0a 29 7b 0a 20 20 76 6f 69 64 20 2a  pArg.){.  void *
340ed 70 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pOld;.  sqlite3_
340ee 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
340ef 6d 75 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d  mutex);.  pOld =
340f0 20 64 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67   db->pProfileArg
340f1 3b 0a 20 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65  ;.  db->xProfile
340f2 20 3d 20 78 50 72 6f 66 69 6c 65 3b 0a 20 20 64   = xProfile;.  d
340f3 62 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 20 3d  b->pProfileArg =
340f4 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33   pArg;.  sqlite3
340f5 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
340f6 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
340f7 6e 20 70 4f 6c 64 3b 0a 7d 0a 23 65 6e 64 69 66  n pOld;.}.#endif
340f8 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
340f9 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 20 45  TRACE */../*** E
340fa 58 50 45 52 49 4d 45 4e 54 41 4c 20 2a 2a 2a 0a  XPERIMENTAL ***.
340fb 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  **.** Register a
340fc 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20   function to be 
340fd 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 61 20 74  invoked when a t
340fe 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 65  ransaction comme
340ff 6e 74 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69  nts..** If the i
34100 6e 76 6f 6b 65 64 20 66 75 6e 63 74 69 6f 6e 20  nvoked function 
34101 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f  returns non-zero
34102 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69  , then the commi
34103 74 20 62 65 63 6f 6d 65 73 20 61 0a 2a 2a 20 72  t becomes a.** r
34104 6f 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49  ollback..*/.SQLI
34105 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
34106 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b  ite3_commit_hook
34107 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
34108 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34109 20 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b   Attach the hook
3410a 20 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73   to this databas
3410b 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 61  e */.  int (*xCa
3410c 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20  llback)(void*), 
3410d 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20   /* Function to 
3410e 69 6e 76 6f 6b 65 20 6f 6e 20 65 61 63 68 20 63  invoke on each c
3410f 6f 6d 6d 69 74 20 2a 2f 0a 20 20 76 6f 69 64 20  ommit */.  void 
34110 2a 70 41 72 67 20 20 20 20 20 20 20 20 20 20 20  *pArg           
34111 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
34112 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
34113 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70   */.){.  void *p
34114 4f 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Old;.  sqlite3_m
34115 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
34116 75 74 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20  utex);.  pOld = 
34117 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 3b 0a  db->pCommitArg;.
34118 20 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c    db->xCommitCal
34119 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63  lback = xCallbac
3411a 6b 3b 0a 20 20 64 62 2d 3e 70 43 6f 6d 6d 69 74  k;.  db->pCommit
3411b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71  Arg = pArg;.  sq
3411c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
3411d 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
3411e 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 0a  return pOld;.}..
3411f 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
34120 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20   callback to be 
34121 69 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d  invoked each tim
34122 65 20 61 20 72 6f 77 20 69 73 20 75 70 64 61 74  e a row is updat
34123 65 64 2c 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20  ed,.** inserted 
34124 6f 72 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67  or deleted using
34125 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 63   this database c
34126 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  onnection..*/.SQ
34127 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
34128 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f  qlite3_update_ho
34129 6f 6b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ok(.  sqlite3 *d
3412a 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
3412b 2f 2a 20 41 74 74 61 63 68 20 74 68 65 20 68 6f  /* Attach the ho
3412c 6f 6b 20 74 6f 20 74 68 69 73 20 64 61 74 61 62  ok to this datab
3412d 61 73 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ase */.  void (*
3412e 78 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  xCallback)(void*
3412f 2c 69 6e 74 2c 63 68 61 72 20 63 6f 6e 73 74 20  ,int,char const 
34130 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73  *,char const *,s
34131 71 6c 69 74 65 5f 69 6e 74 36 34 29 2c 0a 20 20  qlite_int64),.  
34132 76 6f 69 64 20 2a 70 41 72 67 20 20 20 20 20 20  void *pArg      
34133 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
34134 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e  ument to the fun
34135 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f  ction */.){.  vo
34136 69 64 20 2a 70 52 65 74 3b 0a 20 20 73 71 6c 69  id *pRet;.  sqli
34137 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
34138 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 52  db->mutex);.  pR
34139 65 74 20 3d 20 64 62 2d 3e 70 55 70 64 61 74 65  et = db->pUpdate
3413a 41 72 67 3b 0a 20 20 64 62 2d 3e 78 55 70 64 61  Arg;.  db->xUpda
3413b 74 65 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61  teCallback = xCa
3413c 6c 6c 62 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 55  llback;.  db->pU
3413d 70 64 61 74 65 41 72 67 20 3d 20 70 41 72 67 3b  pdateArg = pArg;
3413e 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3413f 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
34140 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74  );.  return pRet
34141 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  ;.}../*.** Regis
34142 74 65 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74  ter a callback t
34143 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 65 61 63  o be invoked eac
34144 68 20 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63  h time a transac
34145 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 0a 2a  tion is rolled.*
34146 2a 20 62 61 63 6b 20 62 79 20 74 68 69 73 20 64  * back by this d
34147 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
34148 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  on..*/.SQLITE_AP
34149 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
3414a 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 0a 20  rollback_hook(. 
3414b 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
3414c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74             /* At
3414d 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f  tach the hook to
3414e 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a   this database *
3414f 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c  /.  void (*xCall
34150 62 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 2f 2a  back)(void*), /*
34151 20 43 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   Callback functi
34152 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  on */.  void *pA
34153 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rg              
34154 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
34155 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
34156 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52 65 74  .){.  void *pRet
34157 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
34158 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
34159 78 29 3b 0a 20 20 70 52 65 74 20 3d 20 64 62 2d  x);.  pRet = db-
3415a 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67 3b 0a 20  >pRollbackArg;. 
3415b 20 64 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61   db->xRollbackCa
3415c 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61  llback = xCallba
3415d 63 6b 3b 0a 20 20 64 62 2d 3e 70 52 6f 6c 6c 62  ck;.  db->pRollb
3415e 61 63 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  ackArg = pArg;. 
3415f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
34160 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
34161 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a  .  return pRet;.
34162 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
34163 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
34164 72 75 65 20 69 66 20 6d 61 69 6e 2d 6d 65 6d 6f  rue if main-memo
34165 72 79 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  ry should be use
34166 64 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20  d instead of.** 
34167 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
34168 20 66 6f 72 20 74 72 61 6e 73 69 65 6e 74 20 70   for transient p
34169 61 67 65 72 20 66 69 6c 65 73 20 61 6e 64 20 73  ager files and s
3416a 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
3416b 73 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  s..** The value 
3416c 72 65 74 75 72 6e 65 64 20 64 65 70 65 6e 64 73  returned depends
3416d 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66   on the value of
3416e 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20   db->temp_store 
3416f 28 72 75 6e 74 69 6d 65 0a 2a 2a 20 70 61 72 61  (runtime.** para
34170 6d 65 74 65 72 29 20 61 6e 64 20 74 68 65 20 63  meter) and the c
34171 6f 6d 70 69 6c 65 20 74 69 6d 65 20 76 61 6c 75  ompile time valu
34172 65 20 6f 66 20 53 51 4c 49 54 45 5f 54 45 4d 50  e of SQLITE_TEMP
34173 5f 53 54 4f 52 45 2e 20 54 68 65 0a 2a 2a 20 66  _STORE. The.** f
34174 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64  ollowing table d
34175 65 73 63 72 69 62 65 73 20 74 68 65 20 72 65 6c  escribes the rel
34176 61 74 69 6f 6e 73 68 69 70 20 62 65 74 77 65 65  ationship betwee
34177 6e 20 74 68 65 73 65 20 74 77 6f 20 76 61 6c 75  n these two valu
34178 65 73 0a 2a 2a 20 61 6e 64 20 74 68 69 73 20 66  es.** and this f
34179 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e 20  unctions return 
3417a 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53  value..**.**   S
3417b 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45  QLITE_TEMP_STORE
3417c 20 20 20 20 20 64 62 2d 3e 74 65 6d 70 5f 73 74       db->temp_st
3417d 6f 72 65 20 20 20 20 20 4c 6f 63 61 74 69 6f 6e  ore     Location
3417e 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 64 61   of temporary da
3417f 74 61 62 61 73 65 0a 2a 2a 20 20 20 2d 2d 2d 2d  tabase.**   ----
34180 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20  -------------   
34181 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
34182 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       -----------
34183 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
34184 2d 2d 2d 0a 2a 2a 20 20 20 30 20 20 20 20 20 20  ---.**   0      
34185 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
34186 6e 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ny              
34187 20 20 66 69 6c 65 20 20 20 20 20 20 28 72 65 74    file      (ret
34188 75 72 6e 20 30 29 0a 2a 2a 20 20 20 31 20 20 20  urn 0).**   1   
34189 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3418a 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20    1             
3418b 20 20 20 20 20 66 69 6c 65 20 20 20 20 20 20 28       file      (
3418c 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20 20 20 31  return 0).**   1
3418d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3418e 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20       2          
3418f 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 20          memory  
34190 20 20 28 72 65 74 75 72 6e 20 31 29 0a 2a 2a 20    (return 1).** 
34191 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20 20    1             
34192 20 20 20 20 20 20 20 20 30 20 20 20 20 20 20 20          0       
34193 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65 20             file 
34194 20 20 20 20 20 28 72 65 74 75 72 6e 20 30 29 0a       (return 0).
34195 2a 2a 20 20 20 32 20 20 20 20 20 20 20 20 20 20  **   2          
34196 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20 20             1    
34197 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
34198 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e 20  le      (return 
34199 30 29 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20  0).**   2       
3419a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 20                2 
3419b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3419c 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65 74 75   memory    (retu
3419d 72 6e 20 31 29 0a 2a 2a 20 20 20 32 20 20 20 20  rn 1).**   2    
3419e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3419f 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
341a0 20 20 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72      memory    (r
341a1 65 74 75 72 6e 20 31 29 0a 2a 2a 20 20 20 33 20  eturn 1).**   3 
341a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
341a3 20 20 20 20 61 6e 79 20 20 20 20 20 20 20 20 20      any         
341a4 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 20 20         memory   
341a5 20 28 72 65 74 75 72 6e 20 31 29 0a 2a 2f 0a 53   (return 1).*/.S
341a6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
341a7 74 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d  t sqlite3TempInM
341a8 65 6d 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69  emory(const sqli
341a9 74 65 33 20 2a 64 62 29 7b 0a 23 69 66 20 53 51  te3 *db){.#if SQ
341aa 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d  LITE_TEMP_STORE=
341ab 3d 31 0a 20 20 72 65 74 75 72 6e 20 28 20 64 62  =1.  return ( db
341ac 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d 3d 32 20  ->temp_store==2 
341ad 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  );.#endif.#if SQ
341ae 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d  LITE_TEMP_STORE=
341af 3d 32 0a 20 20 72 65 74 75 72 6e 20 28 20 64 62  =2.  return ( db
341b0 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 21 3d 31 20  ->temp_store!=1 
341b1 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  );.#endif.#if SQ
341b2 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d  LITE_TEMP_STORE=
341b3 3d 33 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23  =3.  return 1;.#
341b4 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
341b5 5f 54 45 4d 50 5f 53 54 4f 52 45 3c 31 20 7c 7c  _TEMP_STORE<1 ||
341b6 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
341b7 52 45 3e 33 0a 20 20 72 65 74 75 72 6e 20 30 3b  RE>3.  return 0;
341b8 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
341b9 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
341ba 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74   called to creat
341bb 65 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74  e a connection t
341bc 6f 20 61 20 64 61 74 61 62 61 73 65 20 42 54 72  o a database BTr
341bd 65 65 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 49  ee.** driver.  I
341be 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 74  f zFilename is t
341bf 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 69 6c  he name of a fil
341c0 65 2c 20 74 68 65 6e 20 74 68 61 74 20 66 69 6c  e, then that fil
341c1 65 20 69 73 0a 2a 2a 20 6f 70 65 6e 65 64 20 61  e is.** opened a
341c2 6e 64 20 75 73 65 64 2e 20 20 49 66 20 7a 46 69  nd used.  If zFi
341c3 6c 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61  lename is the ma
341c4 67 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72  gic name ":memor
341c5 79 3a 22 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  y:" then.** the 
341c6 64 61 74 61 62 61 73 65 20 69 73 20 73 74 6f 72  database is stor
341c7 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20 28 61 6e  ed in memory (an
341c8 64 20 69 73 20 74 68 75 73 20 66 6f 72 67 6f 74  d is thus forgot
341c9 74 65 6e 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a  ten as soon as.*
341ca 2a 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  * the connection
341cb 20 69 73 20 63 6c 6f 73 65 64 2e 29 20 20 49 66   is closed.)  If
341cc 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55   zFilename is NU
341cd 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61  LL then the data
341ce 62 61 73 65 0a 2a 2a 20 69 73 20 61 20 22 76 69  base.** is a "vi
341cf 72 74 75 61 6c 22 20 64 61 74 61 62 61 73 65 20  rtual" database 
341d0 66 6f 72 20 74 72 61 6e 73 69 65 6e 74 20 75 73  for transient us
341d1 65 20 6f 6e 6c 79 20 61 6e 64 20 69 73 20 64 65  e only and is de
341d2 6c 65 74 65 64 20 61 73 0a 2a 2a 20 73 6f 6f 6e  leted as.** soon
341d3 20 61 73 20 74 68 65 20 63 6f 6e 6e 65 63 74 69   as the connecti
341d4 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a  on is closed..**
341d5 0a 2a 2a 20 41 20 76 69 72 74 75 61 6c 20 64 61  .** A virtual da
341d6 74 61 62 61 73 65 20 63 61 6e 20 62 65 20 65 69  tabase can be ei
341d7 74 68 65 72 20 61 20 64 69 73 6b 20 66 69 6c 65  ther a disk file
341d8 20 28 74 68 61 74 20 69 73 20 61 75 74 6f 6d 61   (that is automa
341d9 74 69 63 61 6c 6c 79 0a 2a 2a 20 64 65 6c 65 74  tically.** delet
341da 65 64 20 77 68 65 6e 20 74 68 65 20 66 69 6c 65  ed when the file
341db 20 69 73 20 63 6c 6f 73 65 64 29 20 6f 72 20 69   is closed) or i
341dc 74 20 61 6e 20 62 65 20 68 65 6c 64 20 65 6e 74  t an be held ent
341dd 69 72 65 6c 79 20 69 6e 20 6d 65 6d 6f 72 79 2e  irely in memory.
341de 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 54  .** The sqlite3T
341df 65 6d 70 49 6e 4d 65 6d 6f 72 79 28 29 20 66 75  empInMemory() fu
341e0 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
341e1 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 69 63  o determine whic
341e2 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  h..*/.SQLITE_PRI
341e3 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
341e4 42 74 72 65 65 46 61 63 74 6f 72 79 28 0a 20 20  BtreeFactory(.  
341e5 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
341e6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 69            /* Mai
341e7 6e 20 64 61 74 61 62 61 73 65 20 77 68 65 6e 20  n database when 
341e8 6f 70 65 6e 69 6e 67 20 61 75 78 20 6f 74 68 65  opening aux othe
341e9 72 77 69 73 65 20 30 20 2a 2f 0a 20 20 63 6f 6e  rwise 0 */.  con
341ea 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
341eb 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  me,    /* Name o
341ec 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e 74 61  f the file conta
341ed 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65 65 20  ining the BTree 
341ee 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e  database */.  in
341ef 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 20  t omitJournal,  
341f0 20 20 20 20 20 20 20 20 2f 2a 20 69 66 20 54 52          /* if TR
341f1 55 45 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a  UE then do not j
341f2 6f 75 72 6e 61 6c 20 74 68 69 73 20 66 69 6c 65  ournal this file
341f3 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61 63 68 65   */.  int nCache
341f4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
341f5 2f 2a 20 48 6f 77 20 6d 61 6e 79 20 70 61 67 65  /* How many page
341f6 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 63 61  s in the page ca
341f7 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73  che */.  int vfs
341f8 46 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  Flags,          
341f9 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73     /* Flags pass
341fa 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 76 66  ed through to vf
341fb 73 4f 70 65 6e 20 2a 2f 0a 20 20 42 74 72 65 65  sOpen */.  Btree
341fc 20 2a 2a 70 70 42 74 72 65 65 20 20 20 20 20 20   **ppBtree      
341fd 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
341fe 74 6f 20 6e 65 77 20 42 74 72 65 65 20 6f 62 6a  to new Btree obj
341ff 65 63 74 20 77 72 69 74 74 65 6e 20 68 65 72 65  ect written here
34200 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 62 74 46   */.){.  int btF
34201 6c 61 67 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20  lags = 0;.  int 
34202 72 63 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28  rc;.  .  assert(
34203 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
34204 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
34205 3b 0a 20 20 61 73 73 65 72 74 28 20 70 70 42 74  ;.  assert( ppBt
34206 72 65 65 20 21 3d 20 30 29 3b 0a 20 20 69 66 28  ree != 0);.  if(
34207 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a   omitJournal ){.
34208 20 20 20 20 62 74 46 6c 61 67 73 20 7c 3d 20 42      btFlags |= B
34209 54 52 45 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41  TREE_OMIT_JOURNA
3420a 4c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d  L;.  }.  if( db-
3420b 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
3420c 4e 6f 52 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20  NoReadlock ){.  
3420d 20 20 62 74 46 6c 61 67 73 20 7c 3d 20 42 54 52    btFlags |= BTR
3420e 45 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 3b 0a  EE_NO_READLOCK;.
3420f 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
34210 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42  TE_OMIT_MEMORYDB
34211 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65  .  if( zFilename
34212 3d 3d 30 20 26 26 20 73 71 6c 69 74 65 33 54 65  ==0 && sqlite3Te
34213 6d 70 49 6e 4d 65 6d 6f 72 79 28 64 62 29 20 29  mpInMemory(db) )
34214 7b 0a 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 20  {.    zFilename 
34215 3d 20 22 3a 6d 65 6d 6f 72 79 3a 22 3b 0a 20 20  = ":memory:";.  
34216 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  }.#endif..  if( 
34217 28 76 66 73 46 6c 61 67 73 20 26 20 53 51 4c 49  (vfsFlags & SQLI
34218 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 29  TE_OPEN_MAIN_DB)
34219 21 3d 30 20 26 26 20 28 7a 46 69 6c 65 6e 61 6d  !=0 && (zFilenam
3421a 65 3d 3d 30 20 7c 7c 20 2a 7a 46 69 6c 65 6e 61  e==0 || *zFilena
3421b 6d 65 3d 3d 30 29 20 29 7b 0a 20 20 20 20 76 66  me==0) ){.    vf
3421c 73 46 6c 61 67 73 20 3d 20 28 76 66 73 46 6c 61  sFlags = (vfsFla
3421d 67 73 20 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45  gs & ~SQLITE_OPE
3421e 4e 5f 4d 41 49 4e 5f 44 42 29 20 7c 20 53 51 4c  N_MAIN_DB) | SQL
3421f 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42  ITE_OPEN_TEMP_DB
34220 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
34221 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 7a 46  ite3BtreeOpen(zF
34222 69 6c 65 6e 61 6d 65 2c 20 28 73 71 6c 69 74 65  ilename, (sqlite
34223 33 20 2a 29 64 62 2c 20 70 70 42 74 72 65 65 2c  3 *)db, ppBtree,
34224 20 62 74 46 6c 61 67 73 2c 20 76 66 73 46 6c 61   btFlags, vfsFla
34225 67 73 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  gs);..  /* If th
34226 65 20 42 2d 54 72 65 65 20 77 61 73 20 73 75 63  e B-Tree was suc
34227 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64  cessfully opened
34228 2c 20 73 65 74 20 74 68 65 20 70 61 67 65 72 2d  , set the pager-
34229 63 61 63 68 65 20 73 69 7a 65 20 74 6f 20 74 68  cache size to th
3422a 65 0a 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 76  e.  ** default v
3422b 61 6c 75 65 2e 20 45 78 63 65 70 74 2c 20 69 66  alue. Except, if
3422c 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 42 74 72   the call to Btr
3422d 65 65 4f 70 65 6e 28 29 20 72 65 74 75 72 6e 65  eeOpen() returne
3422e 64 20 61 20 68 61 6e 64 6c 65 0a 20 20 2a 2a 20  d a handle.  ** 
3422f 6f 70 65 6e 20 6f 6e 20 61 6e 20 65 78 69 73 74  open on an exist
34230 69 6e 67 20 73 68 61 72 65 64 20 70 61 67 65 72  ing shared pager
34231 2d 63 61 63 68 65 2c 20 64 6f 20 6e 6f 74 20 63  -cache, do not c
34232 68 61 6e 67 65 20 74 68 65 20 70 61 67 65 72 2d  hange the pager-
34233 63 61 63 68 65 20 0a 20 20 2a 2a 20 73 69 7a 65  cache .  ** size
34234 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  ..  */.  if( rc=
34235 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30 3d  =SQLITE_OK && 0=
34236 3d 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68  =sqlite3BtreeSch
34237 65 6d 61 28 2a 70 70 42 74 72 65 65 2c 20 30 2c  ema(*ppBtree, 0,
34238 20 30 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74   0) ){.    sqlit
34239 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53  e3BtreeSetCacheS
3423a 69 7a 65 28 2a 70 70 42 74 72 65 65 2c 20 6e 43  ize(*ppBtree, nC
3423b 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ache);.  }.  ret
3423c 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
3423d 20 52 65 74 75 72 6e 20 55 54 46 2d 38 20 65 6e   Return UTF-8 en
3423e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68 20 6c 61  coded English la
3423f 6e 67 75 61 67 65 20 65 78 70 6c 61 6e 61 74 69  nguage explanati
34240 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  on of the most r
34241 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a  ecent.** error..
34242 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
34243 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
34244 33 5f 65 72 72 6d 73 67 28 73 71 6c 69 74 65 33  3_errmsg(sqlite3
34245 20 2a 64 62 29 7b 0a 20 20 63 6f 6e 73 74 20 63   *db){.  const c
34246 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20 21 64  har *z;.  if( !d
34247 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  b ){.    return 
34248 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 53 51  sqlite3ErrStr(SQ
34249 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 7d  LITE_NOMEM);.  }
3424a 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 53  .  if( !sqlite3S
3424b 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72  afetyCheckSickOr
3424c 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65  Ok(db) ){.    re
3424d 74 75 72 6e 20 73 71 6c 69 74 65 33 45 72 72 53  turn sqlite3ErrS
3424e 74 72 28 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  tr(SQLITE_MISUSE
3424f 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
34250 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
34251 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 64  >mutex);.  if( d
34252 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
34253 29 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  ){.    z = sqlit
34254 65 33 45 72 72 53 74 72 28 53 51 4c 49 54 45 5f  e3ErrStr(SQLITE_
34255 4e 4f 4d 45 4d 29 3b 0a 20 20 7d 65 6c 73 65 7b  NOMEM);.  }else{
34256 0a 20 20 20 20 7a 20 3d 20 28 63 68 61 72 2a 29  .    z = (char*)
34257 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
34258 78 74 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20  xt(db->pErr);.  
34259 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
3425a 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
3425b 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20     if( z==0 ){. 
3425c 20 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33       z = sqlite3
3425d 45 72 72 53 74 72 28 64 62 2d 3e 65 72 72 43 6f  ErrStr(db->errCo
3425e 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  de);.    }.  }. 
3425f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
34260 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
34261 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a  .  return z;.}..
34262 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
34263 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20  MIT_UTF16./*.** 
34264 52 65 74 75 72 6e 20 55 54 46 2d 31 36 20 65 6e  Return UTF-16 en
34265 63 6f 64 65 64 20 45 6e 67 6c 69 73 68 20 6c 61  coded English la
34266 6e 67 75 61 67 65 20 65 78 70 6c 61 6e 61 74 69  nguage explanati
34267 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  on of the most r
34268 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f 72 2e 0a  ecent.** error..
34269 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
3426a 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
3426b 33 5f 65 72 72 6d 73 67 31 36 28 73 71 6c 69 74  3_errmsg16(sqlit
3426c 65 33 20 2a 64 62 29 7b 0a 20 20 73 74 61 74 69  e3 *db){.  stati
3426d 63 20 63 6f 6e 73 74 20 75 31 36 20 6f 75 74 4f  c const u16 outO
3426e 66 4d 65 6d 5b 5d 20 3d 20 7b 0a 20 20 20 20 27  fMem[] = {.    '
3426f 6f 27 2c 20 27 75 27 2c 20 27 74 27 2c 20 27 20  o', 'u', 't', ' 
34270 27 2c 20 27 6f 27 2c 20 27 66 27 2c 20 27 20 27  ', 'o', 'f', ' '
34271 2c 20 27 6d 27 2c 20 27 65 27 2c 20 27 6d 27 2c  , 'm', 'e', 'm',
34272 20 27 6f 27 2c 20 27 72 27 2c 20 27 79 27 2c 20   'o', 'r', 'y', 
34273 30 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  0.  };.  static 
34274 63 6f 6e 73 74 20 75 31 36 20 6d 69 73 75 73 65  const u16 misuse
34275 5b 5d 20 3d 20 7b 0a 20 20 20 20 27 6c 27 2c 20  [] = {.    'l', 
34276 27 69 27 2c 20 27 62 27 2c 20 27 72 27 2c 20 27  'i', 'b', 'r', '
34277 61 27 2c 20 27 72 27 2c 20 27 79 27 2c 20 27 20  a', 'r', 'y', ' 
34278 27 2c 20 0a 20 20 20 20 27 72 27 2c 20 27 6f 27  ', .    'r', 'o'
34279 2c 20 27 75 27 2c 20 27 74 27 2c 20 27 69 27 2c  , 'u', 't', 'i',
3427a 20 27 6e 27 2c 20 27 65 27 2c 20 27 20 27 2c 20   'n', 'e', ' ', 
3427b 0a 20 20 20 20 27 63 27 2c 20 27 61 27 2c 20 27  .    'c', 'a', '
3427c 6c 27 2c 20 27 6c 27 2c 20 27 65 27 2c 20 27 64  l', 'l', 'e', 'd
3427d 27 2c 20 27 20 27 2c 20 0a 20 20 20 20 27 6f 27  ', ' ', .    'o'
3427e 2c 20 27 75 27 2c 20 27 74 27 2c 20 27 20 27 2c  , 'u', 't', ' ',
3427f 20 0a 20 20 20 20 27 6f 27 2c 20 27 66 27 2c 20   .    'o', 'f', 
34280 27 20 27 2c 20 0a 20 20 20 20 27 73 27 2c 20 27  ' ', .    's', '
34281 65 27 2c 20 27 71 27 2c 20 27 75 27 2c 20 27 65  e', 'q', 'u', 'e
34282 27 2c 20 27 6e 27 2c 20 27 63 27 2c 20 27 65 27  ', 'n', 'c', 'e'
34283 2c 20 30 0a 20 20 7d 3b 0a 0a 20 20 63 6f 6e 73  , 0.  };..  cons
34284 74 20 76 6f 69 64 20 2a 7a 3b 0a 20 20 69 66 28  t void *z;.  if(
34285 20 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74 75   !db ){.    retu
34286 72 6e 20 28 76 6f 69 64 20 2a 29 6f 75 74 4f 66  rn (void *)outOf
34287 4d 65 6d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  Mem;.  }.  if( !
34288 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
34289 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29  ckSickOrOk(db) )
3428a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f  {.    return (vo
3428b 69 64 20 2a 29 6d 69 73 75 73 65 3b 0a 20 20 7d  id *)misuse;.  }
3428c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
3428d 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
3428e 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
3428f 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
34290 20 7a 20 3d 20 28 76 6f 69 64 20 2a 29 6f 75 74   z = (void *)out
34291 4f 66 4d 65 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  OfMem;.  }else{.
34292 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f      z = sqlite3_
34293 76 61 6c 75 65 5f 74 65 78 74 31 36 28 64 62 2d  value_text16(db-
34294 3e 70 45 72 72 29 3b 0a 20 20 20 20 69 66 28 20  >pErr);.    if( 
34295 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71  z==0 ){.      sq
34296 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
34297 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 73  (db->pErr, -1, s
34298 71 6c 69 74 65 33 45 72 72 53 74 72 28 64 62 2d  qlite3ErrStr(db-
34299 3e 65 72 72 43 6f 64 65 29 2c 0a 20 20 20 20 20  >errCode),.     
3429a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46        SQLITE_UTF
3429b 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  8, SQLITE_STATIC
3429c 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c  );.      z = sql
3429d 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
3429e 36 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 20  6(db->pErr);.   
3429f 20 7d 0a 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c   }.    /* A mall
342a0 6f 63 28 29 20 6d 61 79 20 68 61 76 65 20 66 61  oc() may have fa
342a1 69 6c 65 64 20 77 69 74 68 69 6e 20 74 68 65 20  iled within the 
342a2 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f  call to sqlite3_
342a3 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 0a 20  value_text16(). 
342a4 20 20 20 2a 2a 20 61 62 6f 76 65 2e 20 49 66 20     ** above. If 
342a5 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65  this is the case
342a6 2c 20 74 68 65 6e 20 74 68 65 20 64 62 2d 3e 6d  , then the db->m
342a7 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
342a8 20 6e 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a   needs to.    **
342a9 20 62 65 20 63 6c 65 61 72 65 64 20 62 65 66 6f   be cleared befo
342aa 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 20 44 6f  re returning. Do
342ab 20 74 68 69 73 20 64 69 72 65 63 74 6c 79 2c 20   this directly, 
342ac 69 6e 73 74 65 61 64 20 6f 66 20 76 69 61 0a 20  instead of via. 
342ad 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 41 70 69     ** sqlite3Api
342ae 45 78 69 74 28 29 2c 20 74 6f 20 61 76 6f 69 64  Exit(), to avoid
342af 20 73 65 74 74 69 6e 67 20 74 68 65 20 64 61 74   setting the dat
342b0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 65 72 72  abase handle err
342b1 6f 72 20 6d 65 73 73 61 67 65 2e 0a 20 20 20 20  or message..    
342b2 2a 2f 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  */.    db->mallo
342b3 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 7d  cFailed = 0;.  }
342b4 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
342b5 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
342b6 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
342b7 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
342b8 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
342b9 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
342ba 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72  e most recent er
342bb 72 6f 72 20 63 6f 64 65 20 67 65 6e 65 72 61 74  ror code generat
342bc 65 64 20 62 79 20 61 6e 20 53 51 4c 69 74 65 20  ed by an SQLite 
342bd 72 6f 75 74 69 6e 65 2e 20 49 66 20 4e 55 4c 4c  routine. If NULL
342be 20 69 73 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f   is.** passed to
342bf 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
342c0 77 65 20 61 73 73 75 6d 65 20 61 20 6d 61 6c 6c  we assume a mall
342c1 6f 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69  oc() failed duri
342c2 6e 67 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ng sqlite3_open(
342c3 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  )..*/.SQLITE_API
342c4 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72   int sqlite3_err
342c5 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62  code(sqlite3 *db
342c6 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20 21  ){.  if( db && !
342c7 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
342c8 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29  ckSickOrOk(db) )
342c9 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
342ca 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a  ITE_MISUSE;.  }.
342cb 20 20 69 66 28 20 21 64 62 20 7c 7c 20 64 62 2d    if( !db || db-
342cc 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
342cd 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
342ce 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
342cf 72 65 74 75 72 6e 20 64 62 2d 3e 65 72 72 43 6f  return db->errCo
342d0 64 65 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b  de & db->errMask
342d1 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
342d2 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  nt sqlite3_exten
342d3 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69  ded_errcode(sqli
342d4 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20  te3 *db){.  if( 
342d5 64 62 20 26 26 20 21 73 71 6c 69 74 65 33 53 61  db && !sqlite3Sa
342d6 66 65 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f  fetyCheckSickOrO
342d7 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74  k(db) ){.    ret
342d8 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
342d9 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62  E;.  }.  if( !db
342da 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
342db 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  iled ){.    retu
342dc 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
342dd 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 64 62  .  }.  return db
342de 2d 3e 65 72 72 43 6f 64 65 3b 0a 7d 0a 0a 2f 2a  ->errCode;.}../*
342df 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
342e0 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
342e1 69 6f 6e 20 66 6f 72 20 64 61 74 61 62 61 73 65  ion for database
342e2 20 22 64 62 22 2e 20 20 54 68 65 20 6e 61 6d 65   "db".  The name
342e3 20 69 73 20 7a 4e 61 6d 65 0a 2a 2a 20 61 6e 64   is zName.** and
342e4 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73   the encoding is
342e5 20 65 6e 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   enc..*/.static 
342e6 69 6e 74 20 63 72 65 61 74 65 43 6f 6c 6c 61 74  int createCollat
342e7 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  ion(.  sqlite3* 
342e8 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  db,.  const char
342e9 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 75 38 20 65   *zName, .  u8 e
342ea 6e 63 2c 0a 20 20 75 38 20 63 6f 6c 6c 54 79 70  nc,.  u8 collTyp
342eb 65 2c 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c  e,.  void* pCtx,
342ec 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65  .  int(*xCompare
342ed 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  )(void*,int,cons
342ee 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73  t void*,int,cons
342ef 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64  t void*),.  void
342f0 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29  (*xDel)(void*).)
342f1 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
342f2 6c 6c 3b 0a 20 20 69 6e 74 20 65 6e 63 32 3b 0a  ll;.  int enc2;.
342f3 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71    int nName = sq
342f4 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e  lite3Strlen30(zN
342f5 61 6d 65 29 3b 0a 20 20 0a 20 20 61 73 73 65 72  ame);.  .  asser
342f6 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
342f7 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
342f8 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 53 51 4c   );..  /* If SQL
342f9 49 54 45 5f 55 54 46 31 36 20 69 73 20 73 70 65  ITE_UTF16 is spe
342fa 63 69 66 69 65 64 20 61 73 20 74 68 65 20 65 6e  cified as the en
342fb 63 6f 64 69 6e 67 20 74 79 70 65 2c 20 74 72 61  coding type, tra
342fc 6e 73 66 6f 72 6d 20 74 68 69 73 0a 20 20 2a 2a  nsform this.  **
342fd 20 74 6f 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54   to one of SQLIT
342fe 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c  E_UTF16LE or SQL
342ff 49 54 45 5f 55 54 46 31 36 42 45 20 75 73 69 6e  ITE_UTF16BE usin
34300 67 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c 49 54  g the.  ** SQLIT
34301 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 6d 61  E_UTF16NATIVE ma
34302 63 72 6f 2e 20 53 51 4c 49 54 45 5f 55 54 46 31  cro. SQLITE_UTF1
34303 36 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e  6 is not used in
34304 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f 0a 20  ternally..  */. 
34305 20 65 6e 63 32 20 3d 20 65 6e 63 3b 0a 20 20 74   enc2 = enc;.  t
34306 65 73 74 63 61 73 65 28 20 65 6e 63 32 3d 3d 53  estcase( enc2==S
34307 51 4c 49 54 45 5f 55 54 46 31 36 20 29 3b 0a 20  QLITE_UTF16 );. 
34308 20 74 65 73 74 63 61 73 65 28 20 65 6e 63 32 3d   testcase( enc2=
34309 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  =SQLITE_UTF16_AL
3430a 49 47 4e 45 44 20 29 3b 0a 20 20 69 66 28 20 65  IGNED );.  if( e
3430b 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  nc2==SQLITE_UTF1
3430c 36 20 7c 7c 20 65 6e 63 32 3d 3d 53 51 4c 49 54  6 || enc2==SQLIT
3430d 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20  E_UTF16_ALIGNED 
3430e 29 7b 0a 20 20 20 20 65 6e 63 32 20 3d 20 53 51  ){.    enc2 = SQ
3430f 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
34310 3b 0a 20 20 7d 0a 20 20 69 66 28 20 65 6e 63 32  ;.  }.  if( enc2
34311 3c 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20  <SQLITE_UTF8 || 
34312 65 6e 63 32 3e 53 51 4c 49 54 45 5f 55 54 46 31  enc2>SQLITE_UTF1
34313 36 42 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72  6BE ){.    retur
34314 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
34315 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  .  }..  /* Check
34316 20 69 66 20 74 68 69 73 20 63 61 6c 6c 20 69 73   if this call is
34317 20 72 65 6d 6f 76 69 6e 67 20 6f 72 20 72 65 70   removing or rep
34318 6c 61 63 69 6e 67 20 61 6e 20 65 78 69 73 74 69  lacing an existi
34319 6e 67 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 20 20  ng collation .  
3431a 2a 2a 20 73 65 71 75 65 6e 63 65 2e 20 49 66 20  ** sequence. If 
3431b 73 6f 2c 20 61 6e 64 20 74 68 65 72 65 20 61 72  so, and there ar
3431c 65 20 61 63 74 69 76 65 20 56 4d 73 2c 20 72 65  e active VMs, re
3431d 74 75 72 6e 20 62 75 73 79 2e 20 49 66 20 74 68  turn busy. If th
3431e 65 72 65 0a 20 20 2a 2a 20 61 72 65 20 6e 6f 20  ere.  ** are no 
3431f 61 63 74 69 76 65 20 56 4d 73 2c 20 69 6e 76 61  active VMs, inva
34320 6c 69 64 61 74 65 20 61 6e 79 20 70 72 65 2d 63  lidate any pre-c
34321 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e  ompiled statemen
34322 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 6c  ts..  */.  pColl
34323 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
34324 6c 6c 53 65 71 28 64 62 2c 20 28 75 38 29 65 6e  llSeq(db, (u8)en
34325 63 32 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20  c2, zName, 0);. 
34326 20 69 66 28 20 70 43 6f 6c 6c 20 26 26 20 70 43   if( pColl && pC
34327 6f 6c 6c 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20  oll->xCmp ){.   
34328 20 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56   if( db->activeV
34329 64 62 65 43 6e 74 20 29 7b 0a 20 20 20 20 20 20  dbeCnt ){.      
3432a 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
3432b 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20   SQLITE_BUSY, . 
3432c 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65 20 74         "unable t
3432d 6f 20 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20  o delete/modify 
3432e 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
3432f 63 65 20 64 75 65 20 74 6f 20 61 63 74 69 76 65  ce due to active
34330 20 73 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20   statements");. 
34331 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
34332 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20  TE_BUSY;.    }. 
34333 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
34334 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
34335 74 73 28 64 62 29 3b 0a 0a 20 20 20 20 2f 2a 20  ts(db);..    /* 
34336 49 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  If collation seq
34337 75 65 6e 63 65 20 70 43 6f 6c 6c 20 77 61 73 20  uence pColl was 
34338 63 72 65 61 74 65 64 20 64 69 72 65 63 74 6c 79  created directly
34339 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 0a 20 20   by a call to.  
3433a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72 65    ** sqlite3_cre
3433b 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 2c 20 61  ate_collation, a
3433c 6e 64 20 6e 6f 74 20 67 65 6e 65 72 61 74 65 64  nd not generated
3433d 20 62 79 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71   by synthCollSeq
3433e 28 29 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20  (),.    ** then 
3433f 61 6e 79 20 63 6f 70 69 65 73 20 6d 61 64 65 20  any copies made 
34340 62 79 20 73 79 6e 74 68 43 6f 6c 6c 53 65 71 28  by synthCollSeq(
34341 29 20 6e 65 65 64 20 74 6f 20 62 65 20 69 6e 76  ) need to be inv
34342 61 6c 69 64 61 74 65 64 2e 0a 20 20 20 20 2a 2a  alidated..    **
34343 20 41 6c 73 6f 2c 20 63 6f 6c 6c 61 74 69 6f 6e   Also, collation
34344 20 64 65 73 74 72 75 63 74 6f 72 20 2d 20 43 6f   destructor - Co
34345 6c 6c 53 65 71 2e 78 44 65 6c 28 29 20 2d 20 66  llSeq.xDel() - f
34346 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6e 65 65 64  unction may need
34347 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 63 61  .    ** to be ca
34348 6c 6c 65 64 2e 0a 20 20 20 20 2a 2f 20 0a 20 20  lled..    */ .  
34349 20 20 69 66 28 20 28 70 43 6f 6c 6c 2d 3e 65 6e    if( (pColl->en
3434a 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54 46 31  c & ~SQLITE_UTF1
3434b 36 5f 41 4c 49 47 4e 45 44 29 3d 3d 65 6e 63 32  6_ALIGNED)==enc2
3434c 20 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65   ){.      CollSe
3434d 71 20 2a 61 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  q *aColl = sqlit
3434e 65 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e  e3HashFind(&db->
3434f 61 43 6f 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65 2c  aCollSeq, zName,
34350 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69   nName);.      i
34351 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28  nt j;.      for(
34352 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b 29 7b 0a  j=0; j<3; j++){.
34353 20 20 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20          CollSeq 
34354 2a 70 20 3d 20 26 61 43 6f 6c 6c 5b 6a 5d 3b 0a  *p = &aColl[j];.
34355 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 65          if( p->e
34356 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29  nc==pColl->enc )
34357 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
34358 70 2d 3e 78 44 65 6c 20 29 7b 0a 20 20 20 20 20  p->xDel ){.     
34359 20 20 20 20 20 20 20 70 2d 3e 78 44 65 6c 28 70         p->xDel(p
3435a 2d 3e 70 55 73 65 72 29 3b 0a 20 20 20 20 20 20  ->pUser);.      
3435b 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
3435c 70 2d 3e 78 43 6d 70 20 3d 20 30 3b 0a 20 20 20  p->xCmp = 0;.   
3435d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
3435e 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 43 6f 6c     }.  }..  pCol
3435f 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43  l = sqlite3FindC
34360 6f 6c 6c 53 65 71 28 64 62 2c 20 28 75 38 29 65  ollSeq(db, (u8)e
34361 6e 63 32 2c 20 7a 4e 61 6d 65 2c 20 31 29 3b 0a  nc2, zName, 1);.
34362 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20    if( pColl ){. 
34363 20 20 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 3d     pColl->xCmp =
34364 20 78 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 70   xCompare;.    p
34365 43 6f 6c 6c 2d 3e 70 55 73 65 72 20 3d 20 70 43  Coll->pUser = pC
34366 74 78 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78  tx;.    pColl->x
34367 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20  Del = xDel;.    
34368 70 43 6f 6c 6c 2d 3e 65 6e 63 20 3d 20 28 75 38  pColl->enc = (u8
34369 29 28 65 6e 63 32 20 7c 20 28 65 6e 63 20 26 20  )(enc2 | (enc & 
3436a 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
3436b 47 4e 45 44 29 29 3b 0a 20 20 20 20 70 43 6f 6c  GNED));.    pCol
3436c 6c 2d 3e 74 79 70 65 20 3d 20 63 6f 6c 6c 54 79  l->type = collTy
3436d 70 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  pe;.  }.  sqlite
3436e 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
3436f 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65 74 75  E_OK, 0);.  retu
34370 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
34371 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 72 72  ../*.** This arr
34372 61 79 20 64 65 66 69 6e 65 73 20 68 61 72 64 20  ay defines hard 
34373 75 70 70 65 72 20 62 6f 75 6e 64 73 20 6f 6e 20  upper bounds on 
34374 6c 69 6d 69 74 20 76 61 6c 75 65 73 2e 20 20 54  limit values.  T
34375 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65  he.** initialize
34376 72 20 6d 75 73 74 20 62 65 20 6b 65 70 74 20 69  r must be kept i
34377 6e 20 73 79 6e 63 20 77 69 74 68 20 74 68 65 20  n sync with the 
34378 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 2a 0a 2a  SQLITE_LIMIT_*.*
34379 2a 20 23 64 65 66 69 6e 65 73 20 69 6e 20 73 71  * #defines in sq
3437a 6c 69 74 65 33 2e 68 2e 0a 2a 2f 0a 73 74 61 74  lite3.h..*/.stat
3437b 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 61 48 61  ic const int aHa
3437c 72 64 4c 69 6d 69 74 5b 5d 20 3d 20 7b 0a 20 20  rdLimit[] = {.  
3437d 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54  SQLITE_MAX_LENGT
3437e 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f  H,.  SQLITE_MAX_
3437f 53 51 4c 5f 4c 45 4e 47 54 48 2c 0a 20 20 53 51  SQL_LENGTH,.  SQ
34380 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 2c  LITE_MAX_COLUMN,
34381 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  .  SQLITE_MAX_EX
34382 50 52 5f 44 45 50 54 48 2c 0a 20 20 53 51 4c 49  PR_DEPTH,.  SQLI
34383 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f  TE_MAX_COMPOUND_
34384 53 45 4c 45 43 54 2c 0a 20 20 53 51 4c 49 54 45  SELECT,.  SQLITE
34385 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 2c 0a 20 20  _MAX_VDBE_OP,.  
34386 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54  SQLITE_MAX_FUNCT
34387 49 4f 4e 5f 41 52 47 2c 0a 20 20 53 51 4c 49 54  ION_ARG,.  SQLIT
34388 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2c 0a  E_MAX_ATTACHED,.
34389 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b    SQLITE_MAX_LIK
3438a 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
3438b 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56  ,.  SQLITE_MAX_V
3438c 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 2c 0a  ARIABLE_NUMBER,.
3438d 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49    SQLITE_MAX_TRI
3438e 47 47 45 52 5f 44 45 50 54 48 2c 0a 7d 3b 0a 0a  GGER_DEPTH,.};..
3438f 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
34390 74 68 65 20 68 61 72 64 20 6c 69 6d 69 74 73 20  the hard limits 
34391 61 72 65 20 73 65 74 20 74 6f 20 72 65 61 73 6f  are set to reaso
34392 6e 61 62 6c 65 20 76 61 6c 75 65 73 0a 2a 2f 0a  nable values.*/.
34393 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  #if SQLITE_MAX_L
34394 45 4e 47 54 48 3c 31 30 30 0a 23 20 65 72 72 6f  ENGTH<100.# erro
34395 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  r SQLITE_MAX_LEN
34396 47 54 48 20 6d 75 73 74 20 62 65 20 61 74 20 6c  GTH must be at l
34397 65 61 73 74 20 31 30 30 0a 23 65 6e 64 69 66 0a  east 100.#endif.
34398 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53  #if SQLITE_MAX_S
34399 51 4c 5f 4c 45 4e 47 54 48 3c 31 30 30 0a 23 20  QL_LENGTH<100.# 
3439a 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58  error SQLITE_MAX
3439b 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 6d 75 73 74  _SQL_LENGTH must
3439c 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 30 30   be at least 100
3439d 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
3439e 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54  TE_MAX_SQL_LENGT
3439f 48 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  H>SQLITE_MAX_LEN
343a0 47 54 48 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  GTH.# error SQLI
343a1 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54  TE_MAX_SQL_LENGT
343a2 48 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 67 72  H must not be gr
343a3 65 61 74 65 72 20 74 68 61 6e 20 53 51 4c 49 54  eater than SQLIT
343a4 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 65 6e  E_MAX_LENGTH.#en
343a5 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
343a6 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  AX_COMPOUND_SELE
343a7 43 54 3c 32 0a 23 20 65 72 72 6f 72 20 53 51 4c  CT<2.# error SQL
343a8 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44  ITE_MAX_COMPOUND
343a9 5f 53 45 4c 45 43 54 20 6d 75 73 74 20 62 65 20  _SELECT must be 
343aa 61 74 20 6c 65 61 73 74 20 32 0a 23 65 6e 64 69  at least 2.#endi
343ab 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
343ac 5f 56 44 42 45 5f 4f 50 3c 34 30 0a 23 20 65 72  _VDBE_OP<40.# er
343ad 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56  ror SQLITE_MAX_V
343ae 44 42 45 5f 4f 50 20 6d 75 73 74 20 62 65 20 61  DBE_OP must be a
343af 74 20 6c 65 61 73 74 20 34 30 0a 23 65 6e 64 69  t least 40.#endi
343b0 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
343b1 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c 30 20  _FUNCTION_ARG<0 
343b2 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55  || SQLITE_MAX_FU
343b3 4e 43 54 49 4f 4e 5f 41 52 47 3e 31 30 30 30 0a  NCTION_ARG>1000.
343b4 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d  # error SQLITE_M
343b5 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20  AX_FUNCTION_ARG 
343b6 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20  must be between 
343b7 30 20 61 6e 64 20 31 30 30 30 0a 23 65 6e 64 69  0 and 1000.#endi
343b8 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
343b9 5f 41 54 54 41 43 48 45 44 3c 30 20 7c 7c 20 53  _ATTACHED<0 || S
343ba 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
343bb 45 44 3e 33 30 0a 23 20 65 72 72 6f 72 20 53 51  ED>30.# error SQ
343bc 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45  LITE_MAX_ATTACHE
343bd 44 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65  D must be betwee
343be 6e 20 30 20 61 6e 64 20 33 30 0a 23 65 6e 64 69  n 0 and 30.#endi
343bf 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
343c0 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
343c1 4e 47 54 48 3c 31 0a 23 20 65 72 72 6f 72 20 53  NGTH<1.# error S
343c2 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
343c3 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 6d 75  ATTERN_LENGTH mu
343c4 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31  st be at least 1
343c5 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
343c6 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 3e 33 32  TE_MAX_COLUMN>32
343c7 37 36 37 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  767.# error SQLI
343c8 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 6d 75  TE_MAX_COLUMN mu
343c9 73 74 20 6e 6f 74 20 65 78 63 65 65 64 20 33 32  st not exceed 32
343ca 37 36 37 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  767.#endif.#if S
343cb 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45  QLITE_MAX_TRIGGE
343cc 52 5f 44 45 50 54 48 3c 31 0a 23 20 65 72 72 6f  R_DEPTH<1.# erro
343cd 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49  r SQLITE_MAX_TRI
343ce 47 47 45 52 5f 44 45 50 54 48 20 6d 75 73 74 20  GGER_DEPTH must 
343cf 62 65 20 61 74 20 6c 65 61 73 74 20 31 0a 23 65  be at least 1.#e
343d0 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  ndif.../*.** Cha
343d1 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  nge the value of
343d2 20 61 20 6c 69 6d 69 74 2e 20 20 52 65 70 6f 72   a limit.  Repor
343d3 74 20 74 68 65 20 6f 6c 64 20 76 61 6c 75 65 2e  t the old value.
343d4 0a 2a 2a 20 49 66 20 61 6e 20 69 6e 76 61 6c 69  .** If an invali
343d5 64 20 6c 69 6d 69 74 20 69 6e 64 65 78 20 69 73  d limit index is
343d6 20 73 75 70 70 6c 69 65 64 2c 20 72 65 70 6f 72   supplied, repor
343d7 74 20 2d 31 2e 0a 2a 2a 20 4d 61 6b 65 20 6e 6f  t -1..** Make no
343d8 20 63 68 61 6e 67 65 73 20 62 75 74 20 73 74 69   changes but sti
343d9 6c 6c 20 72 65 70 6f 72 74 20 74 68 65 20 6f 6c  ll report the ol
343da 64 20 76 61 6c 75 65 20 69 66 20 74 68 65 0a 2a  d value if the.*
343db 2a 20 6e 65 77 20 6c 69 6d 69 74 20 69 73 20 6e  * new limit is n
343dc 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 41  egative..**.** A
343dd 20 6e 65 77 20 6c 6f 77 65 72 20 6c 69 6d 69 74   new lower limit
343de 20 64 6f 65 73 20 6e 6f 74 20 73 68 72 69 6e 6b   does not shrink
343df 20 65 78 69 73 74 69 6e 67 20 63 6f 6e 73 74 72   existing constr
343e0 75 63 74 73 2e 0a 2a 2a 20 49 74 20 6d 65 72 65  ucts..** It mere
343e1 6c 79 20 70 72 65 76 65 6e 74 73 20 6e 65 77 20  ly prevents new 
343e2 63 6f 6e 73 74 72 75 63 74 73 20 74 68 61 74 20  constructs that 
343e3 65 78 63 65 65 64 20 74 68 65 20 6c 69 6d 69 74  exceed the limit
343e4 0a 2a 2a 20 66 72 6f 6d 20 66 6f 72 6d 69 6e 67  .** from forming
343e5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
343e6 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c 69 6d 69  int sqlite3_limi
343e7 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69  t(sqlite3 *db, i
343e8 6e 74 20 6c 69 6d 69 74 49 64 2c 20 69 6e 74 20  nt limitId, int 
343e9 6e 65 77 4c 69 6d 69 74 29 7b 0a 20 20 69 6e 74  newLimit){.  int
343ea 20 6f 6c 64 4c 69 6d 69 74 3b 0a 20 20 69 66 28   oldLimit;.  if(
343eb 20 6c 69 6d 69 74 49 64 3c 30 20 7c 7c 20 6c 69   limitId<0 || li
343ec 6d 69 74 49 64 3e 3d 53 51 4c 49 54 45 5f 4e 5f  mitId>=SQLITE_N_
343ed 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 72 65 74  LIMIT ){.    ret
343ee 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 6f 6c  urn -1;.  }.  ol
343ef 64 4c 69 6d 69 74 20 3d 20 64 62 2d 3e 61 4c 69  dLimit = db->aLi
343f0 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 3b 0a 20 20  mit[limitId];.  
343f1 69 66 28 20 6e 65 77 4c 69 6d 69 74 3e 3d 30 20  if( newLimit>=0 
343f2 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 77 4c 69  ){.    if( newLi
343f3 6d 69 74 3e 61 48 61 72 64 4c 69 6d 69 74 5b 6c  mit>aHardLimit[l
343f4 69 6d 69 74 49 64 5d 20 29 7b 0a 20 20 20 20 20  imitId] ){.     
343f5 20 6e 65 77 4c 69 6d 69 74 20 3d 20 61 48 61 72   newLimit = aHar
343f6 64 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 3b  dLimit[limitId];
343f7 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 61  .    }.    db->a
343f8 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 20 3d  Limit[limitId] =
343f9 20 6e 65 77 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20   newLimit;.  }. 
343fa 20 72 65 74 75 72 6e 20 6f 6c 64 4c 69 6d 69 74   return oldLimit
343fb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
343fc 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65  routine does the
343fd 20 77 6f 72 6b 20 6f 66 20 6f 70 65 6e 69 6e 67   work of opening
343fe 20 61 20 64 61 74 61 62 61 73 65 20 6f 6e 20 62   a database on b
343ff 65 68 61 6c 66 20 6f 66 0a 2a 2a 20 73 71 6c 69  ehalf of.** sqli
34400 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73  te3_open() and s
34401 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e  qlite3_open16().
34402 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69   The database fi
34403 6c 65 6e 61 6d 65 20 22 7a 46 69 6c 65 6e 61 6d  lename "zFilenam
34404 65 22 20 20 0a 2a 2a 20 69 73 20 55 54 46 2d 38  e"  .** is UTF-8
34405 20 65 6e 63 6f 64 65 64 2e 0a 2a 2f 0a 73 74 61   encoded..*/.sta
34406 74 69 63 20 69 6e 74 20 6f 70 65 6e 44 61 74 61  tic int openData
34407 62 61 73 65 28 0a 20 20 63 6f 6e 73 74 20 63 68  base(.  const ch
34408 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 2f  ar *zFilename, /
34409 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e  * Database filen
3440a 61 6d 65 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  ame UTF-8 encode
3440b 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  d */.  sqlite3 *
3440c 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 2f 2a  *ppDb,        /*
3440d 20 4f 55 54 3a 20 52 65 74 75 72 6e 65 64 20 64   OUT: Returned d
3440e 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
3440f 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 66 6c 61  /.  unsigned fla
34410 67 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 70  gs,        /* Op
34411 65 72 61 74 69 6f 6e 61 6c 20 66 6c 61 67 73 20  erational flags 
34412 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
34413 2a 7a 56 66 73 20 20 20 20 20 20 20 2f 2a 20 4e  *zVfs       /* N
34414 61 6d 65 20 6f 66 20 74 68 65 20 56 46 53 20 74  ame of the VFS t
34415 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a 20 20 73 71  o use */.){.  sq
34416 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e 74  lite3 *db;.  int
34417 20 72 63 3b 0a 20 20 69 6e 74 20 69 73 54 68 72   rc;.  int isThr
34418 65 61 64 73 61 66 65 3b 0a 0a 20 20 2a 70 70 44  eadsafe;..  *ppD
34419 62 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53  b = 0;.#ifndef S
3441a 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
3441b 4e 49 54 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  NIT.  rc = sqlit
3441c 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b  e3_initialize();
3441d 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
3441e 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 0a 20  rn rc;.#endif.. 
3441f 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
34420 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75  alConfig.bCoreMu
34421 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 69 73  tex==0 ){.    is
34422 54 68 72 65 61 64 73 61 66 65 20 3d 20 30 3b 0a  Threadsafe = 0;.
34423 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67    }else if( flag
34424 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
34425 4e 4f 4d 55 54 45 58 20 29 7b 0a 20 20 20 20 69  NOMUTEX ){.    i
34426 73 54 68 72 65 61 64 73 61 66 65 20 3d 20 30 3b  sThreadsafe = 0;
34427 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61  .  }else if( fla
34428 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
34429 5f 46 55 4c 4c 4d 55 54 45 58 20 29 7b 0a 20 20  _FULLMUTEX ){.  
3442a 20 20 69 73 54 68 72 65 61 64 73 61 66 65 20 3d    isThreadsafe =
3442b 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
3442c 20 69 73 54 68 72 65 61 64 73 61 66 65 20 3d 20   isThreadsafe = 
3442d 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3442e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 3b 0a  fig.bFullMutex;.
3442f 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 20    }.  if( flags 
34430 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52  & SQLITE_OPEN_PR
34431 49 56 41 54 45 43 41 43 48 45 20 29 7b 0a 20 20  IVATECACHE ){.  
34432 20 20 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49    flags &= ~SQLI
34433 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41  TE_OPEN_SHAREDCA
34434 43 48 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  CHE;.  }else if(
34435 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
34436 6e 66 69 67 2e 73 68 61 72 65 64 43 61 63 68 65  nfig.sharedCache
34437 45 6e 61 62 6c 65 64 20 29 7b 0a 20 20 20 20 66  Enabled ){.    f
34438 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4f  lags |= SQLITE_O
34439 50 45 4e 5f 53 48 41 52 45 44 43 41 43 48 45 3b  PEN_SHAREDCACHE;
3443a 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76  .  }..  /* Remov
3443b 65 20 68 61 72 6d 66 75 6c 20 62 69 74 73 20 66  e harmful bits f
3443c 72 6f 6d 20 74 68 65 20 66 6c 61 67 73 20 70 61  rom the flags pa
3443d 72 61 6d 65 74 65 72 0a 20 20 2a 2a 0a 20 20 2a  rameter.  **.  *
3443e 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 50 45  * The SQLITE_OPE
3443f 4e 5f 4e 4f 4d 55 54 45 58 20 61 6e 64 20 53 51  N_NOMUTEX and SQ
34440 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55  LITE_OPEN_FULLMU
34441 54 45 58 20 66 6c 61 67 73 20 77 65 72 65 0a 20  TEX flags were. 
34442 20 2a 2a 20 64 65 61 6c 74 20 77 69 74 68 20 69   ** dealt with i
34443 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  n the previous c
34444 6f 64 65 20 62 6c 6f 63 6b 2e 20 20 42 65 73 69  ode block.  Besi
34445 64 65 73 20 74 68 65 73 65 2c 20 74 68 65 20 6f  des these, the o
34446 6e 6c 79 0a 20 20 2a 2a 20 76 61 6c 69 64 20 69  nly.  ** valid i
34447 6e 70 75 74 20 66 6c 61 67 73 20 66 6f 72 20 73  nput flags for s
34448 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
34449 20 61 72 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e   are SQLITE_OPEN
3444a 5f 52 45 41 44 4f 4e 4c 59 2c 0a 20 20 2a 2a 20  _READONLY,.  ** 
3444b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
3444c 57 52 49 54 45 2c 20 61 6e 64 20 53 51 4c 49 54  WRITE, and SQLIT
3444d 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2e 20 20  E_OPEN_CREATE.  
3444e 53 69 6c 65 6e 74 6c 79 20 6d 61 73 6b 0a 20 20  Silently mask.  
3444f 2a 2a 20 6f 66 66 20 61 6c 6c 20 6f 74 68 65 72  ** off all other
34450 20 66 6c 61 67 73 2e 0a 20 20 2a 2f 0a 20 20 66   flags..  */.  f
34451 6c 61 67 73 20 26 3d 20 20 7e 28 20 53 51 4c 49  lags &=  ~( SQLI
34452 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
34453 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20 20 20  CLOSE |.        
34454 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50         SQLITE_OP
34455 45 4e 5f 45 58 43 4c 55 53 49 56 45 20 7c 0a 20  EN_EXCLUSIVE |. 
34456 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
34457 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
34458 42 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20  B |.            
34459 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54     SQLITE_OPEN_T
3445a 45 4d 50 5f 44 42 20 7c 20 0a 20 20 20 20 20 20  EMP_DB | .      
3445b 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
3445c 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44  OPEN_TRANSIENT_D
3445d 42 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20  B | .           
3445e 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
3445f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a  MAIN_JOURNAL | .
34460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
34461 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f  QLITE_OPEN_TEMP_
34462 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20  JOURNAL | .     
34463 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
34464 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c  _OPEN_SUBJOURNAL
34465 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20   | .            
34466 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d     SQLITE_OPEN_M
34467 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 0a  ASTER_JOURNAL |.
34468 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
34469 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54  QLITE_OPEN_NOMUT
3446a 45 58 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20  EX |.           
3446b 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
3446c 46 55 4c 4c 4d 55 54 45 58 0a 20 20 20 20 20 20  FULLMUTEX.      
3446d 20 20 20 20 20 20 20 29 3b 0a 0a 20 20 2f 2a 20         );..  /* 
3446e 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 71 6c  Allocate the sql
3446f 69 74 65 20 64 61 74 61 20 73 74 72 75 63 74 75  ite data structu
34470 72 65 20 2a 2f 0a 20 20 64 62 20 3d 20 73 71 6c  re */.  db = sql
34471 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20  ite3MallocZero( 
34472 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 29 20  sizeof(sqlite3) 
34473 29 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20 29  );.  if( db==0 )
34474 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74   goto opendb_out
34475 3b 0a 20 20 69 66 28 20 69 73 54 68 72 65 61 64  ;.  if( isThread
34476 73 61 66 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e  safe ){.    db->
34477 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d  mutex = sqlite3M
34478 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
34479 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
3447a 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d  );.    if( db->m
3447b 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20  utex==0 ){.     
3447c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62   sqlite3_free(db
3447d 29 3b 0a 20 20 20 20 20 20 64 62 20 3d 20 30 3b  );.      db = 0;
3447e 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e  .      goto open
3447f 64 62 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  db_out;.    }.  
34480 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
34481 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
34482 78 29 3b 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73  x);.  db->errMas
34483 6b 20 3d 20 30 78 66 66 3b 0a 20 20 64 62 2d 3e  k = 0xff;.  db->
34484 6e 44 62 20 3d 20 32 3b 0a 20 20 64 62 2d 3e 6d  nDb = 2;.  db->m
34485 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
34486 47 49 43 5f 42 55 53 59 3b 0a 20 20 64 62 2d 3e  GIC_BUSY;.  db->
34487 61 44 62 20 3d 20 64 62 2d 3e 61 44 62 53 74 61  aDb = db->aDbSta
34488 74 69 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  tic;..  assert( 
34489 73 69 7a 65 6f 66 28 64 62 2d 3e 61 4c 69 6d 69  sizeof(db->aLimi
3448a 74 29 3d 3d 73 69 7a 65 6f 66 28 61 48 61 72 64  t)==sizeof(aHard
3448b 4c 69 6d 69 74 29 20 29 3b 0a 20 20 6d 65 6d 63  Limit) );.  memc
3448c 70 79 28 64 62 2d 3e 61 4c 69 6d 69 74 2c 20 61  py(db->aLimit, a
3448d 48 61 72 64 4c 69 6d 69 74 2c 20 73 69 7a 65 6f  HardLimit, sizeo
3448e 66 28 64 62 2d 3e 61 4c 69 6d 69 74 29 29 3b 0a  f(db->aLimit));.
3448f 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74    db->autoCommit
34490 20 3d 20 31 3b 0a 20 20 64 62 2d 3e 6e 65 78 74   = 1;.  db->next
34491 41 75 74 6f 76 61 63 20 3d 20 2d 31 3b 0a 20 20  Autovac = -1;.  
34492 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65  db->nextPagesize
34493 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 66 6c 61 67   = 0;.  db->flag
34494 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68 6f 72  s |= SQLITE_Shor
34495 74 43 6f 6c 4e 61 6d 65 73 0a 23 69 66 20 53 51  tColNames.#if SQ
34496 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c  LITE_DEFAULT_FIL
34497 45 5f 46 4f 52 4d 41 54 3c 34 0a 20 20 20 20 20  E_FORMAT<4.     
34498 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 53 51              | SQ
34499 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46  LITE_LegacyFileF
3449a 6d 74 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66  mt.#endif.#ifdef
3449b 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
3449c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 20 20  OAD_EXTENSION.  
3449d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
3449e 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78 74 65   SQLITE_LoadExte
3449f 6e 73 69 6f 6e 0a 23 65 6e 64 69 66 0a 23 69 66  nsion.#endif.#if
344a0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
344a1 52 45 43 55 52 53 49 56 45 5f 54 52 49 47 47 45  RECURSIVE_TRIGGE
344a2 52 53 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  RS.             
344a3 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 52 65 63      | SQLITE_Rec
344a4 54 72 69 67 67 65 72 73 0a 23 65 6e 64 69 66 0a  Triggers.#endif.
344a5 20 20 20 20 20 20 3b 0a 20 20 73 71 6c 69 74 65        ;.  sqlite
344a6 33 48 61 73 68 49 6e 69 74 28 26 64 62 2d 3e 61  3HashInit(&db->a
344a7 43 6f 6c 6c 53 65 71 29 3b 0a 23 69 66 6e 64 65  CollSeq);.#ifnde
344a8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
344a9 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c  RTUALTABLE.  sql
344aa 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 64 62  ite3HashInit(&db
344ab 2d 3e 61 4d 6f 64 75 6c 65 29 3b 0a 23 65 6e 64  ->aModule);.#end
344ac 69 66 0a 0a 20 20 64 62 2d 3e 70 56 66 73 20 3d  if..  db->pVfs =
344ad 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e   sqlite3_vfs_fin
344ae 64 28 7a 56 66 73 29 3b 0a 20 20 69 66 28 20 21  d(zVfs);.  if( !
344af 64 62 2d 3e 70 56 66 73 20 29 7b 0a 20 20 20 20  db->pVfs ){.    
344b0 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
344b1 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  R;.    sqlite3Er
344b2 72 6f 72 28 64 62 2c 20 72 63 2c 20 22 6e 6f 20  ror(db, rc, "no 
344b3 73 75 63 68 20 76 66 73 3a 20 25 73 22 2c 20 7a  such vfs: %s", z
344b4 56 66 73 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f  Vfs);.    goto o
344b5 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  pendb_out;.  }..
344b6 20 20 2f 2a 20 41 64 64 20 74 68 65 20 64 65 66    /* Add the def
344b7 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  ault collation s
344b8 65 71 75 65 6e 63 65 20 42 49 4e 41 52 59 2e 20  equence BINARY. 
344b9 42 49 4e 41 52 59 20 77 6f 72 6b 73 20 66 6f 72  BINARY works for
344ba 20 62 6f 74 68 20 55 54 46 2d 38 0a 20 20 2a 2a   both UTF-8.  **
344bb 20 61 6e 64 20 55 54 46 2d 31 36 2c 20 73 6f 20   and UTF-16, so 
344bc 61 64 64 20 61 20 76 65 72 73 69 6f 6e 20 66 6f  add a version fo
344bd 72 20 65 61 63 68 20 74 6f 20 61 76 6f 69 64 20  r each to avoid 
344be 61 6e 79 20 75 6e 6e 65 63 65 73 73 61 72 79 0a  any unnecessary.
344bf 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73    ** conversions
344c0 2e 20 54 68 65 20 6f 6e 6c 79 20 65 72 72 6f 72  . The only error
344c1 20 74 68 61 74 20 63 61 6e 20 6f 63 63 75 72 20   that can occur 
344c2 68 65 72 65 20 69 73 20 61 20 6d 61 6c 6c 6f 63  here is a malloc
344c3 28 29 20 66 61 69 6c 75 72 65 2e 0a 20 20 2a 2f  () failure..  */
344c4 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69  .  createCollati
344c5 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52 59 22 2c  on(db, "BINARY",
344c6 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
344c7 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59  LITE_COLL_BINARY
344c8 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
344c9 20 20 20 20 20 20 20 62 69 6e 43 6f 6c 6c 46 75         binCollFu
344ca 6e 63 2c 20 30 29 3b 0a 20 20 63 72 65 61 74 65  nc, 0);.  create
344cb 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42  Collation(db, "B
344cc 49 4e 41 52 59 22 2c 20 53 51 4c 49 54 45 5f 55  INARY", SQLITE_U
344cd 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 43  TF16BE, SQLITE_C
344ce 4f 4c 4c 5f 42 49 4e 41 52 59 2c 20 30 2c 0a 20  OLL_BINARY, 0,. 
344cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
344d0 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29   binCollFunc, 0)
344d1 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74  ;.  createCollat
344d2 69 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52 59 22  ion(db, "BINARY"
344d3 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  , SQLITE_UTF16LE
344d4 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49  , SQLITE_COLL_BI
344d5 4e 41 52 59 2c 20 30 2c 0a 20 20 20 20 20 20 20  NARY, 0,.       
344d6 20 20 20 20 20 20 20 20 20 20 20 62 69 6e 43 6f             binCo
344d7 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63 72  llFunc, 0);.  cr
344d8 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62  eateCollation(db
344d9 2c 20 22 52 54 52 49 4d 22 2c 20 53 51 4c 49 54  , "RTRIM", SQLIT
344da 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 43  E_UTF8, SQLITE_C
344db 4f 4c 4c 5f 55 53 45 52 2c 20 28 76 6f 69 64 2a  OLL_USER, (void*
344dc 29 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  )1,.            
344dd 20 20 20 20 20 20 62 69 6e 43 6f 6c 6c 46 75 6e        binCollFun
344de 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 64 62 2d  c, 0);.  if( db-
344df 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
344e0 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62  .    goto opendb
344e1 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e  _out;.  }.  db->
344e2 70 44 66 6c 74 43 6f 6c 6c 20 3d 20 73 71 6c 69  pDfltColl = sqli
344e3 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64  te3FindCollSeq(d
344e4 62 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  b, SQLITE_UTF8, 
344e5 22 42 49 4e 41 52 59 22 2c 20 30 29 3b 0a 20 20  "BINARY", 0);.  
344e6 61 73 73 65 72 74 28 20 64 62 2d 3e 70 44 66 6c  assert( db->pDfl
344e7 74 43 6f 6c 6c 21 3d 30 20 29 3b 0a 0a 20 20 2f  tColl!=0 );..  /
344e8 2a 20 41 6c 73 6f 20 61 64 64 20 61 20 55 54 46  * Also add a UTF
344e9 2d 38 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74  -8 case-insensit
344ea 69 76 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ive collation se
344eb 71 75 65 6e 63 65 2e 20 2a 2f 0a 20 20 63 72 65  quence. */.  cre
344ec 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c  ateCollation(db,
344ed 20 22 4e 4f 43 41 53 45 22 2c 20 53 51 4c 49 54   "NOCASE", SQLIT
344ee 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 43  E_UTF8, SQLITE_C
344ef 4f 4c 4c 5f 4e 4f 43 41 53 45 2c 20 30 2c 0a 20  OLL_NOCASE, 0,. 
344f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
344f1 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67   nocaseCollating
344f2 46 75 6e 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20  Func, 0);..  /* 
344f3 4f 70 65 6e 20 74 68 65 20 62 61 63 6b 65 6e 64  Open the backend
344f4 20 64 61 74 61 62 61 73 65 20 64 72 69 76 65 72   database driver
344f5 20 2a 2f 0a 20 20 64 62 2d 3e 6f 70 65 6e 46 6c   */.  db->openFl
344f6 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 72  ags = flags;.  r
344f7 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
344f8 46 61 63 74 6f 72 79 28 64 62 2c 20 7a 46 69 6c  Factory(db, zFil
344f9 65 6e 61 6d 65 2c 20 30 2c 20 53 51 4c 49 54 45  ename, 0, SQLITE
344fa 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
344fb 49 5a 45 2c 20 0a 20 20 20 20 20 20 20 20 20 20  IZE, .          
344fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
344fd 20 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45 5f   flags | SQLITE_
344fe 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20  OPEN_MAIN_DB,.  
344ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34500 20 20 20 20 20 20 20 20 20 26 64 62 2d 3e 61 44           &db->aD
34501 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 69 66 28  b[0].pBt);.  if(
34502 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
34503 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  {.    if( rc==SQ
34504 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d  LITE_IOERR_NOMEM
34505 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
34506 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
34507 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72   }.    sqlite3Er
34508 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a  ror(db, rc, 0);.
34509 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f      goto opendb_
3450a 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 61  out;.  }.  db->a
3450b 44 62 5b 30 5d 2e 70 53 63 68 65 6d 61 20 3d 20  Db[0].pSchema = 
3450c 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74  sqlite3SchemaGet
3450d 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e  (db, db->aDb[0].
3450e 70 42 74 29 3b 0a 20 20 64 62 2d 3e 61 44 62 5b  pBt);.  db->aDb[
3450f 31 5d 2e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c  1].pSchema = sql
34510 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 64 62  ite3SchemaGet(db
34511 2c 20 30 29 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65  , 0);...  /* The
34512 20 64 65 66 61 75 6c 74 20 73 61 66 65 74 79 5f   default safety_
34513 6c 65 76 65 6c 20 66 6f 72 20 74 68 65 20 6d 61  level for the ma
34514 69 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 27  in database is '
34515 66 75 6c 6c 27 3b 20 66 6f 72 20 74 68 65 20 74  full'; for the t
34516 65 6d 70 0a 20 20 2a 2a 20 64 61 74 61 62 61 73  emp.  ** databas
34517 65 20 69 74 20 69 73 20 27 4e 4f 4e 45 27 2e 20  e it is 'NONE'. 
34518 54 68 69 73 20 6d 61 74 63 68 65 73 20 74 68 65  This matches the
34519 20 70 61 67 65 72 20 6c 61 79 65 72 20 64 65 66   pager layer def
3451a 61 75 6c 74 73 2e 20 20 0a 20 20 2a 2f 0a 20 20  aults.  .  */.  
3451b 64 62 2d 3e 61 44 62 5b 30 5d 2e 7a 4e 61 6d 65  db->aDb[0].zName
3451c 20 3d 20 22 6d 61 69 6e 22 3b 0a 20 20 64 62 2d   = "main";.  db-
3451d 3e 61 44 62 5b 30 5d 2e 73 61 66 65 74 79 5f 6c  >aDb[0].safety_l
3451e 65 76 65 6c 20 3d 20 33 3b 0a 20 20 64 62 2d 3e  evel = 3;.  db->
3451f 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 22  aDb[1].zName = "
34520 74 65 6d 70 22 3b 0a 20 20 64 62 2d 3e 61 44 62  temp";.  db->aDb
34521 5b 31 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65 6c  [1].safety_level
34522 20 3d 20 31 3b 0a 0a 20 20 64 62 2d 3e 6d 61 67   = 1;..  db->mag
34523 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49  ic = SQLITE_MAGI
34524 43 5f 4f 50 45 4e 3b 0a 20 20 69 66 28 20 64 62  C_OPEN;.  if( db
34525 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
34526 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64  {.    goto opend
34527 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  b_out;.  }..  /*
34528 20 52 65 67 69 73 74 65 72 20 61 6c 6c 20 62 75   Register all bu
34529 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73  ilt-in functions
3452a 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 61 74 74  , but do not att
3452b 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68 65  empt to read the
3452c 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73  .  ** database s
3452d 63 68 65 6d 61 20 79 65 74 2e 20 54 68 69 73 20  chema yet. This 
3452e 69 73 20 64 65 6c 61 79 65 64 20 75 6e 74 69 6c  is delayed until
3452f 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   the first time 
34530 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a  the database.  *
34531 2a 20 69 73 20 61 63 63 65 73 73 65 64 2e 0a 20  * is accessed.. 
34532 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72   */.  sqlite3Err
34533 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  or(db, SQLITE_OK
34534 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 52  , 0);.  sqlite3R
34535 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46 75  egisterBuiltinFu
34536 6e 63 74 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20  nctions(db);..  
34537 2f 2a 20 4c 6f 61 64 20 61 75 74 6f 6d 61 74 69  /* Load automati
34538 63 20 65 78 74 65 6e 73 69 6f 6e 73 20 2d 20 65  c extensions - e
34539 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 68  xtensions that h
3453a 61 76 65 20 62 65 65 6e 20 72 65 67 69 73 74 65  ave been registe
3453b 72 65 64 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74  red.  ** using t
3453c 68 65 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 6d  he sqlite3_autom
3453d 61 74 69 63 5f 65 78 74 65 6e 73 69 6f 6e 28 29  atic_extension()
3453e 20 41 50 49 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c   API..  */.  sql
3453f 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74 65  ite3AutoLoadExte
34540 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 20 20 72 63  nsions(db);.  rc
34541 20 3d 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f   = sqlite3_errco
34542 64 65 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63  de(db);.  if( rc
34543 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
34544 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f     goto opendb_o
34545 75 74 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20  ut;.  }..#ifdef 
34546 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
34547 53 31 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61  S1.  if( !db->ma
34548 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
34549 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c    extern int sql
3454a 69 74 65 33 46 74 73 31 49 6e 69 74 28 73 71 6c  ite3Fts1Init(sql
3454b 69 74 65 33 2a 29 3b 0a 20 20 20 20 72 63 20 3d  ite3*);.    rc =
3454c 20 73 71 6c 69 74 65 33 46 74 73 31 49 6e 69 74   sqlite3Fts1Init
3454d 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  (db);.  }.#endif
3454e 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
3454f 45 4e 41 42 4c 45 5f 46 54 53 32 0a 20 20 69 66  ENABLE_FTS2.  if
34550 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
34551 6c 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  led && rc==SQLIT
34552 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 65 78 74 65  E_OK ){.    exte
34553 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  rn int sqlite3Ft
34554 73 32 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 29  s2Init(sqlite3*)
34555 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
34556 65 33 46 74 73 32 49 6e 69 74 28 64 62 29 3b 0a  e3Fts2Init(db);.
34557 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    }.#endif..#ifd
34558 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
34559 5f 46 54 53 33 0a 20 20 69 66 28 20 21 64 62 2d  _FTS3.  if( !db-
3455a 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26  >mallocFailed &&
3455b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3455c 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
3455d 65 33 46 74 73 33 49 6e 69 74 28 64 62 29 3b 0a  e3Fts3Init(db);.
3455e 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    }.#endif..#ifd
3455f 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
34560 5f 49 43 55 0a 20 20 69 66 28 20 21 64 62 2d 3e  _ICU.  if( !db->
34561 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20  mallocFailed && 
34562 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
34563 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
34564 33 49 63 75 49 6e 69 74 28 64 62 29 3b 0a 20 20  3IcuInit(db);.  
34565 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  }.#endif..#ifdef
34566 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52   SQLITE_ENABLE_R
34567 54 52 45 45 0a 20 20 69 66 28 20 21 64 62 2d 3e  TREE.  if( !db->
34568 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26 20  mallocFailed && 
34569 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 7b 0a  rc==SQLITE_OK){.
3456a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3456b 52 74 72 65 65 49 6e 69 74 28 64 62 29 3b 0a 20  RtreeInit(db);. 
3456c 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c   }.#endif..  sql
3456d 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
3456e 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 2d 44 53 51  , 0);..  /* -DSQ
3456f 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43  LITE_DEFAULT_LOC
34570 4b 49 4e 47 5f 4d 4f 44 45 3d 31 20 6d 61 6b 65  KING_MODE=1 make
34571 73 20 45 58 43 4c 55 53 49 56 45 20 74 68 65 20  s EXCLUSIVE the 
34572 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a  default locking.
34573 20 20 2a 2a 20 6d 6f 64 65 2e 20 20 2d 44 53 51    ** mode.  -DSQ
34574 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43  LITE_DEFAULT_LOC
34575 4b 49 4e 47 5f 4d 4f 44 45 3d 30 20 6d 61 6b 65  KING_MODE=0 make
34576 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64 65 66 61   NORMAL the defa
34577 75 6c 74 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a  ult locking.  **
34578 20 6d 6f 64 65 2e 20 20 44 6f 69 6e 67 20 6e 6f   mode.  Doing no
34579 74 68 69 6e 67 20 61 74 20 61 6c 6c 20 61 6c 73  thing at all als
3457a 6f 20 6d 61 6b 65 73 20 4e 4f 52 4d 41 4c 20 74  o makes NORMAL t
3457b 68 65 20 64 65 66 61 75 6c 74 2e 0a 20 20 2a 2f  he default..  */
3457c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
3457d 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d  EFAULT_LOCKING_M
3457e 4f 44 45 0a 20 20 64 62 2d 3e 64 66 6c 74 4c 6f  ODE.  db->dfltLo
3457f 63 6b 4d 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  ckMode = SQLITE_
34580 44 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f  DEFAULT_LOCKING_
34581 4d 4f 44 45 3b 0a 20 20 73 71 6c 69 74 65 33 50  MODE;.  sqlite3P
34582 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28  agerLockingMode(
34583 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
34584 72 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  r(db->aDb[0].pBt
34585 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
34586 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
34587 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b  ITE_DEFAULT_LOCK
34588 49 4e 47 5f 4d 4f 44 45 29 3b 0a 23 65 6e 64 69  ING_MODE);.#endi
34589 66 0a 0a 20 20 2f 2a 20 45 6e 61 62 6c 65 20 74  f..  /* Enable t
3458a 68 65 20 6c 6f 6f 6b 61 73 69 64 65 2d 6d 61 6c  he lookaside-mal
3458b 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 20 2a 2f  loc subsystem */
3458c 0a 20 20 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64  .  setupLookasid
3458d 65 28 64 62 2c 20 30 2c 20 73 71 6c 69 74 65 33  e(db, 0, sqlite3
3458e 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 4c  GlobalConfig.szL
3458f 6f 6f 6b 61 73 69 64 65 2c 0a 20 20 20 20 20 20  ookaside,.      
34590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34591 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
34592 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b 61 73 69 64 65  onfig.nLookaside
34593 29 3b 0a 0a 6f 70 65 6e 64 62 5f 6f 75 74 3a 0a  );..opendb_out:.
34594 20 20 69 66 28 20 64 62 20 29 7b 0a 20 20 20 20    if( db ){.    
34595 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 75 74 65  assert( db->mute
34596 78 21 3d 30 20 7c 7c 20 69 73 54 68 72 65 61 64  x!=0 || isThread
34597 73 61 66 65 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  safe==0 || sqlit
34598 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
34599 46 75 6c 6c 4d 75 74 65 78 3d 3d 30 20 29 3b 0a  FullMutex==0 );.
3459a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
3459b 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
3459c 78 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73  x);.  }.  rc = s
3459d 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64  qlite3_errcode(d
3459e 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  b);.  if( rc==SQ
3459f 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
345a0 20 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28    sqlite3_close(
345a1 64 62 29 3b 0a 20 20 20 20 64 62 20 3d 20 30 3b  db);.    db = 0;
345a2 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21  .  }else if( rc!
345a3 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
345a4 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
345a5 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 3b  LITE_MAGIC_SICK;
345a6 0a 20 20 7d 0a 20 20 2a 70 70 44 62 20 3d 20 64  .  }.  *ppDb = d
345a7 62 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  b;.  return sqli
345a8 74 65 33 41 70 69 45 78 69 74 28 30 2c 20 72 63  te3ApiExit(0, rc
345a9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e  );.}../*.** Open
345aa 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20   a new database 
345ab 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  handle..*/.SQLIT
345ac 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
345ad 33 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20  3_open(.  const 
345ae 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
345af 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70   .  sqlite3 **pp
345b0 44 62 20 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20  Db .){.  return 
345b1 6f 70 65 6e 44 61 74 61 62 61 73 65 28 7a 46 69  openDatabase(zFi
345b2 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c 0a 20 20  lename, ppDb,.  
345b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
345b4 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
345b5 52 45 41 44 57 52 49 54 45 20 7c 20 53 51 4c 49  READWRITE | SQLI
345b6 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2c 20  TE_OPEN_CREATE, 
345b7 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  0);.}.SQLITE_API
345b8 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
345b9 6e 5f 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68  n_v2(.  const ch
345ba 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20  ar *filename,   
345bb 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c 65  /* Database file
345bc 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a  name (UTF-8) */.
345bd 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62    sqlite3 **ppDb
345be 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ,         /* OUT
345bf 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64  : SQLite db hand
345c0 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  le */.  int flag
345c1 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
345c2 2f 2a 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f  /* Flags */.  co
345c3 6e 73 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20  nst char *zVfs  
345c4 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
345c5 20 56 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75   VFS module to u
345c6 73 65 20 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72  se */.){.  retur
345c7 6e 20 6f 70 65 6e 44 61 74 61 62 61 73 65 28 66  n openDatabase(f
345c8 69 6c 65 6e 61 6d 65 2c 20 70 70 44 62 2c 20 66  ilename, ppDb, f
345c9 6c 61 67 73 2c 20 7a 56 66 73 29 3b 0a 7d 0a 0a  lags, zVfs);.}..
345ca 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
345cb 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20  MIT_UTF16./*.** 
345cc 4f 70 65 6e 20 61 20 6e 65 77 20 64 61 74 61 62  Open a new datab
345cd 61 73 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53  ase handle..*/.S
345ce 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
345cf 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a 20 20  lite3_open16(.  
345d0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 46 69 6c  const void *zFil
345d1 65 6e 61 6d 65 2c 20 0a 20 20 73 71 6c 69 74 65  ename, .  sqlite
345d2 33 20 2a 2a 70 70 44 62 0a 29 7b 0a 20 20 63 68  3 **ppDb.){.  ch
345d3 61 72 20 63 6f 6e 73 74 20 2a 7a 46 69 6c 65 6e  ar const *zFilen
345d4 61 6d 65 38 3b 20 20 20 2f 2a 20 7a 46 69 6c 65  ame8;   /* zFile
345d5 6e 61 6d 65 20 65 6e 63 6f 64 65 64 20 69 6e 20  name encoded in 
345d6 55 54 46 2d 38 20 69 6e 73 74 65 61 64 20 6f 66  UTF-8 instead of
345d7 20 55 54 46 2d 31 36 20 2a 2f 0a 20 20 73 71 6c   UTF-16 */.  sql
345d8 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c  ite3_value *pVal
345d9 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
345da 73 73 65 72 74 28 20 7a 46 69 6c 65 6e 61 6d 65  ssert( zFilename
345db 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 70   );.  assert( pp
345dc 44 62 20 29 3b 0a 20 20 2a 70 70 44 62 20 3d 20  Db );.  *ppDb = 
345dd 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  0;.#ifndef SQLIT
345de 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
345df 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69    rc = sqlite3_i
345e0 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69  nitialize();.  i
345e1 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
345e2 63 3b 0a 23 65 6e 64 69 66 0a 20 20 70 56 61 6c  c;.#endif.  pVal
345e3 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e   = sqlite3ValueN
345e4 65 77 28 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  ew(0);.  sqlite3
345e5 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c  ValueSetStr(pVal
345e6 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  , -1, zFilename,
345e7 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54   SQLITE_UTF16NAT
345e8 49 56 45 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  IVE, SQLITE_STAT
345e9 49 43 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65  IC);.  zFilename
345ea 38 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  8 = sqlite3Value
345eb 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54  Text(pVal, SQLIT
345ec 45 5f 55 54 46 38 29 3b 0a 20 20 69 66 28 20 7a  E_UTF8);.  if( z
345ed 46 69 6c 65 6e 61 6d 65 38 20 29 7b 0a 20 20 20  Filename8 ){.   
345ee 20 72 63 20 3d 20 6f 70 65 6e 44 61 74 61 62 61   rc = openDataba
345ef 73 65 28 7a 46 69 6c 65 6e 61 6d 65 38 2c 20 70  se(zFilename8, p
345f0 70 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  pDb,.           
345f1 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
345f2 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
345f3 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43   | SQLITE_OPEN_C
345f4 52 45 41 54 45 2c 20 30 29 3b 0a 20 20 20 20 61  REATE, 0);.    a
345f5 73 73 65 72 74 28 20 2a 70 70 44 62 20 7c 7c 20  ssert( *ppDb || 
345f6 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
345f7 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   );.    if( rc==
345f8 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 44 62  SQLITE_OK && !Db
345f9 48 61 73 50 72 6f 70 65 72 74 79 28 2a 70 70 44  HasProperty(*ppD
345fa 62 2c 20 30 2c 20 44 42 5f 53 63 68 65 6d 61 4c  b, 0, DB_SchemaL
345fb 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20 20 20  oaded) ){.      
345fc 45 4e 43 28 2a 70 70 44 62 29 20 3d 20 53 51 4c  ENC(*ppDb) = SQL
345fd 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3b  ITE_UTF16NATIVE;
345fe 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
345ff 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
34600 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 73 71 6c  NOMEM;.  }.  sql
34601 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56  ite3ValueFree(pV
34602 61 6c 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73  al);..  return s
34603 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 30 2c  qlite3ApiExit(0,
34604 20 72 63 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f   rc);.}.#endif /
34605 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
34606 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65  F16 */../*.** Re
34607 67 69 73 74 65 72 20 61 20 6e 65 77 20 63 6f 6c  gister a new col
34608 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
34609 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
3460a 65 20 68 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a  e handle db..*/.
3460b 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3460c 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
3460d 6c 6c 61 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74  llation(.  sqlit
3460e 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74  e3* db, .  const
3460f 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
34610 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69   int enc, .  voi
34611 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a  d* pCtx,.  int(*
34612 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
34613 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
34614 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
34615 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
34616 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
34617 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
34618 20 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d    assert( !db->m
34619 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
3461a 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c   rc = createColl
3461b 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c  ation(db, zName,
3461c 20 28 75 38 29 65 6e 63 2c 20 53 51 4c 49 54 45   (u8)enc, SQLITE
3461d 5f 43 4f 4c 4c 5f 55 53 45 52 2c 20 70 43 74 78  _COLL_USER, pCtx
3461e 2c 20 78 43 6f 6d 70 61 72 65 2c 20 30 29 3b 0a  , xCompare, 0);.
3461f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
34620 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
34621 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
34622 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
34623 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
34624 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20  ./*.** Register 
34625 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20  a new collation 
34626 73 65 71 75 65 6e 63 65 20 77 69 74 68 20 74 68  sequence with th
34627 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
34628 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  e db..*/.SQLITE_
34629 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
3462a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
3462b 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  _v2(.  sqlite3* 
3462c 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  db, .  const cha
3462d 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74  r *zName, .  int
3462e 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70   enc, .  void* p
3462f 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d  Ctx,.  int(*xCom
34630 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  pare)(void*,int,
34631 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
34632 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20  const void*),.  
34633 76 6f 69 64 28 2a 78 44 65 6c 29 28 76 6f 69 64  void(*xDel)(void
34634 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  *).){.  int rc;.
34635 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
34636 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
34637 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ;.  assert( !db-
34638 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
34639 0a 20 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f  .  rc = createCo
3463a 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d  llation(db, zNam
3463b 65 2c 20 28 75 38 29 65 6e 63 2c 20 53 51 4c 49  e, (u8)enc, SQLI
3463c 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c 20 70 43  TE_COLL_USER, pC
3463d 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20 78 44  tx, xCompare, xD
3463e 65 6c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  el);.  rc = sqli
3463f 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
34640 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
34641 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
34642 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
34643 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  c;.}..#ifndef SQ
34644 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
34645 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61  /*.** Register a
34646 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   new collation s
34647 65 71 75 65 6e 63 65 20 77 69 74 68 20 74 68 65  equence with the
34648 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
34649 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41   db..*/.SQLITE_A
3464a 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
3464b 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
3464c 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62  6(.  sqlite3* db
3464d 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
3464e 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 65 6e  *zName,.  int en
3464f 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78  c, .  void* pCtx
34650 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72  ,.  int(*xCompar
34651 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  e)(void*,int,con
34652 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
34653 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69  st void*).){.  i
34654 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
34655 4b 3b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  K;.  char *zName
34656 38 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  8;.  sqlite3_mut
34657 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
34658 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ex);.  assert( !
34659 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
3465a 20 29 3b 0a 20 20 7a 4e 61 6d 65 38 20 3d 20 73   );.  zName8 = s
3465b 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 64  qlite3Utf16to8(d
3465c 62 2c 20 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20  b, zName, -1);. 
3465d 20 69 66 28 20 7a 4e 61 6d 65 38 20 29 7b 0a 20   if( zName8 ){. 
3465e 20 20 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f     rc = createCo
3465f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d  llation(db, zNam
34660 65 38 2c 20 28 75 38 29 65 6e 63 2c 20 53 51 4c  e8, (u8)enc, SQL
34661 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c 20 70  ITE_COLL_USER, p
34662 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20 30  Ctx, xCompare, 0
34663 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
34664 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 38 29  Free(db, zName8)
34665 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
34666 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
34667 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
34668 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
34669 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
3466a 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  rc;.}.#endif /* 
3466b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
3466c 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  6 */../*.** Regi
3466d 73 74 65 72 20 61 20 63 6f 6c 6c 61 74 69 6f 6e  ster a collation
3466e 20 73 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72   sequence factor
3466f 79 20 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20  y callback with 
34670 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
34671 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65 70 6c 61  dle.** db. Repla
34672 63 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 6c  ce any previousl
34673 79 20 69 6e 73 74 61 6c 6c 65 64 20 63 6f 6c 6c  y installed coll
34674 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
34675 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54  actory..*/.SQLIT
34676 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
34677 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  3_collation_need
34678 65 64 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ed(.  sqlite3 *d
34679 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c  b, .  void *pCol
3467a 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a 20 20 76  lNeededArg, .  v
3467b 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64  oid(*xCollNeeded
3467c 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
3467d 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
3467e 6e 73 74 20 63 68 61 72 2a 29 0a 29 7b 0a 20 20  nst char*).){.  
3467f 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
34680 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
34681 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65    db->xCollNeede
34682 64 20 3d 20 78 43 6f 6c 6c 4e 65 65 64 65 64 3b  d = xCollNeeded;
34683 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64  .  db->xCollNeed
34684 65 64 31 36 20 3d 20 30 3b 0a 20 20 64 62 2d 3e  ed16 = 0;.  db->
34685 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 20 3d  pCollNeededArg =
34686 20 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 3b   pCollNeededArg;
34687 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
34688 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
34689 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
3468a 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65  TE_OK;.}..#ifnde
3468b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
3468c 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74  F16./*.** Regist
3468d 65 72 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  er a collation s
3468e 65 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79 20  equence factory 
3468f 63 61 6c 6c 62 61 63 6b 20 77 69 74 68 20 74 68  callback with th
34690 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
34691 65 0a 2a 2a 20 64 62 2e 20 52 65 70 6c 61 63 65  e.** db. Replace
34692 20 61 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20   any previously 
34693 69 6e 73 74 61 6c 6c 65 64 20 63 6f 6c 6c 61 74  installed collat
34694 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63  ion sequence fac
34695 74 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tory..*/.SQLITE_
34696 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
34697 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
34698 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  16(.  sqlite3 *d
34699 62 2c 20 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6c  b, .  void *pCol
3469a 6c 4e 65 65 64 65 64 41 72 67 2c 20 0a 20 20 76  lNeededArg, .  v
3469b 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64  oid(*xCollNeeded
3469c 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  16)(void*,sqlite
3469d 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
3469e 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a  const void*).){.
3469f 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
346a0 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
346a1 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  ;.  db->xCollNee
346a2 64 65 64 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 78  ded = 0;.  db->x
346a3 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d 20 78  CollNeeded16 = x
346a4 43 6f 6c 6c 4e 65 65 64 65 64 31 36 3b 0a 20 20  CollNeeded16;.  
346a5 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64 41  db->pCollNeededA
346a6 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65 64 65 64  rg = pCollNeeded
346a7 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  Arg;.  sqlite3_m
346a8 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
346a9 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
346aa 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
346ab 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
346ac 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 23 69 66  IT_UTF16 */..#if
346ad 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
346ae 5f 47 4c 4f 42 41 4c 52 45 43 4f 56 45 52 0a 23  _GLOBALRECOVER.#
346af 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
346b0 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a  IT_DEPRECATED./*
346b1 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
346b2 6e 20 69 73 20 6e 6f 77 20 61 6e 20 61 6e 61 63  n is now an anac
346b3 68 72 6f 6e 69 73 6d 2e 20 49 74 20 75 73 65 64  hronism. It used
346b4 20 74 6f 20 62 65 20 75 73 65 64 20 74 6f 20 72   to be used to r
346b5 65 63 6f 76 65 72 20 66 72 6f 6d 20 61 0a 2a 2a  ecover from a.**
346b6 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
346b7 65 2c 20 62 75 74 20 53 51 4c 69 74 65 20 6e 6f  e, but SQLite no
346b8 77 20 64 6f 65 73 20 74 68 69 73 20 61 75 74 6f  w does this auto
346b9 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2f 0a 53 51  matically..*/.SQ
346ba 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
346bb 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f  ite3_global_reco
346bc 76 65 72 28 76 6f 69 64 29 7b 0a 20 20 72 65 74  ver(void){.  ret
346bd 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
346be 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a  .#endif.#endif..
346bf 2f 2a 0a 2a 2a 20 54 65 73 74 20 74 6f 20 73 65  /*.** Test to se
346c0 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
346c1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
346c2 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 61  nnection is in a
346c3 75 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 6d 6f 64  utocommit.** mod
346c4 65 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45 20  e.  Return TRUE 
346c5 69 66 20 69 74 20 69 73 20 61 6e 64 20 46 41 4c  if it is and FAL
346c6 53 45 20 69 66 20 6e 6f 74 2e 20 20 41 75 74 6f  SE if not.  Auto
346c7 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20 6f  commit mode is o
346c8 6e 0a 2a 2a 20 62 79 20 64 65 66 61 75 6c 74 2e  n.** by default.
346c9 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 69 73 20    Autocommit is 
346ca 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 42 45  disabled by a BE
346cb 47 49 4e 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  GIN statement an
346cc 64 20 72 65 65 6e 61 62 6c 65 64 0a 2a 2a 20 62  d reenabled.** b
346cd 79 20 74 68 65 20 6e 65 78 74 20 43 4f 4d 4d 49  y the next COMMI
346ce 54 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 2e 0a 2a  T or ROLLBACK..*
346cf 2a 0a 2a 2a 2a 2a 2a 2a 2a 20 54 48 49 53 20 49  *.******* THIS I
346d0 53 20 41 4e 20 45 58 50 45 52 49 4d 45 4e 54 41  S AN EXPERIMENTA
346d1 4c 20 41 50 49 20 41 4e 44 20 49 53 20 53 55 42  L API AND IS SUB
346d2 4a 45 43 54 20 54 4f 20 43 48 41 4e 47 45 20 2a  JECT TO CHANGE *
346d3 2a 2a 2a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  *****.*/.SQLITE_
346d4 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
346d5 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 73  get_autocommit(s
346d6 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72  qlite3 *db){.  r
346d7 65 74 75 72 6e 20 64 62 2d 3e 61 75 74 6f 43 6f  eturn db->autoCo
346d8 6d 6d 69 74 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  mmit;.}..#ifdef 
346d9 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a  SQLITE_DEBUG./*.
346da 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
346db 20 72 6f 75 74 69 6e 65 20 69 73 20 73 75 62 74   routine is subt
346dc 69 74 75 74 65 64 20 66 6f 72 20 63 6f 6e 73 74  ituted for const
346dd 61 6e 74 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  ant SQLITE_CORRU
346de 50 54 20 69 6e 0a 2a 2a 20 64 65 62 75 67 67 69  PT in.** debuggi
346df 6e 67 20 62 75 69 6c 64 73 2e 20 20 54 68 69 73  ng builds.  This
346e0 20 70 72 6f 76 69 64 65 73 20 61 20 77 61 79 20   provides a way 
346e1 74 6f 20 73 65 74 20 61 20 62 72 65 61 6b 70 6f  to set a breakpo
346e2 69 6e 74 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20  int for when.** 
346e3 63 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66 69  corruption is fi
346e4 72 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a 2f  rst detected..*/
346e5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
346e6 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 72 72 75  int sqlite3Corru
346e7 70 74 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75  pt(void){.  retu
346e8 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
346e9 54 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  T;.}.#endif..#if
346ea 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
346eb 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a  _DEPRECATED./*.*
346ec 2a 20 54 68 69 73 20 69 73 20 61 20 63 6f 6e 76  * This is a conv
346ed 65 6e 69 65 6e 63 65 20 72 6f 75 74 69 6e 65 20  enience routine 
346ee 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65 20  that makes sure 
346ef 74 68 61 74 20 61 6c 6c 20 74 68 72 65 61 64 2d  that all thread-
346f0 73 70 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61  specific.** data
346f1 20 66 6f 72 20 74 68 69 73 20 74 68 72 65 61 64   for this thread
346f2 20 68 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f   has been deallo
346f3 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c  cated..**.** SQL
346f4 69 74 65 20 6e 6f 20 6c 6f 6e 67 65 72 20 75 73  ite no longer us
346f5 65 73 20 74 68 72 65 61 64 2d 73 70 65 63 69 66  es thread-specif
346f6 69 63 20 64 61 74 61 20 73 6f 20 74 68 69 73 20  ic data so this 
346f7 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 77 20 61  routine is now a
346f8 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20 20 49 74 20 69  .** no-op.  It i
346f9 73 20 72 65 74 61 69 6e 65 64 20 66 6f 72 20 68  s retained for h
346fa 69 73 74 6f 72 69 63 61 6c 20 63 6f 6d 70 61 74  istorical compat
346fb 69 62 69 6c 69 74 79 2e 0a 2a 2f 0a 53 51 4c 49  ibility..*/.SQLI
346fc 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
346fd 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61 6e  te3_thread_clean
346fe 75 70 28 76 6f 69 64 29 7b 0a 7d 0a 23 65 6e 64  up(void){.}.#end
346ff 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
34700 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f   meta informatio
34701 6e 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66  n about a specif
34702 69 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 64  ic column of a d
34703 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a  atabase table..*
34704 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 69 6e  * See comment in
34705 20 73 71 6c 69 74 65 33 2e 68 20 28 73 71 6c 69   sqlite3.h (sqli
34706 74 65 2e 68 2e 69 6e 29 20 66 6f 72 20 64 65 74  te.h.in) for det
34707 61 69 6c 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ails..*/.#ifdef 
34708 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
34709 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 53 51  LUMN_METADATA.SQ
3470a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
3470b 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d  ite3_table_colum
3470c 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71  n_metadata(.  sq
3470d 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
3470e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
3470f 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a  nection handle *
34710 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
34711 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20  zDbName,        
34712 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65  /* Database name
34713 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f   or NULL */.  co
34714 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
34715 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62  Name,     /* Tab
34716 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e  le name */.  con
34717 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e  st char *zColumn
34718 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75  Name,    /* Colu
34719 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61  mn name */.  cha
3471a 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61  r const **pzData
3471b 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50  Type,    /* OUTP
3471c 55 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74  UT: Declared dat
3471d 61 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72  a type */.  char
3471e 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53   const **pzCollS
3471f 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55  eq,     /* OUTPU
34720 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  T: Collation seq
34721 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  uence name */.  
34722 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20  int *pNotNull,  
34723 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
34724 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e  UTPUT: True if N
34725 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
34726 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69  nt exists */.  i
34727 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c  nt *pPrimaryKey,
34728 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
34729 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f  TPUT: True if co
3472a 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20  lumn part of PK 
3472b 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69  */.  int *pAutoi
3472c 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nc              
3472d 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65   /* OUTPUT: True
3472e 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75   if column is au
3472f 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a  to-increment */.
34730 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63  ){.  int rc;.  c
34731 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30  har *zErrMsg = 0
34732 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20  ;.  Table *pTab 
34733 3d 20 30 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70  = 0;.  Column *p
34734 43 6f 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  Col = 0;.  int i
34735 43 6f 6c 3b 0a 0a 20 20 63 68 61 72 20 63 6f 6e  Col;..  char con
34736 73 74 20 2a 7a 44 61 74 61 54 79 70 65 20 3d 20  st *zDataType = 
34737 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  0;.  char const 
34738 2a 7a 43 6f 6c 6c 53 65 71 20 3d 20 30 3b 0a 20  *zCollSeq = 0;. 
34739 20 69 6e 74 20 6e 6f 74 6e 75 6c 6c 20 3d 20 30   int notnull = 0
3473a 3b 0a 20 20 69 6e 74 20 70 72 69 6d 61 72 79 6b  ;.  int primaryk
3473b 65 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 61 75  ey = 0;.  int au
3473c 74 6f 69 6e 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a  toinc = 0;..  /*
3473d 20 45 6e 73 75 72 65 20 74 68 65 20 64 61 74 61   Ensure the data
3473e 62 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20  base schema has 
3473f 62 65 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 20  been loaded */. 
34740 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
34741 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
34742 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
34743 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20  SafetyOn(db);.  
34744 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
34745 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72 63 20 3d  rAll(db);.  rc =
34746 20 73 71 6c 69 74 65 33 49 6e 69 74 28 64 62 2c   sqlite3Init(db,
34747 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 69 66   &zErrMsg);.  if
34748 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20  ( SQLITE_OK!=rc 
34749 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f  ){.    goto erro
3474a 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  r_out;.  }..  /*
3474b 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c   Locate the tabl
3474c 65 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 2a 2f  e in question */
3474d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
3474e 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
3474f 54 61 62 6c 65 4e 61 6d 65 2c 20 7a 44 62 4e 61  TableName, zDbNa
34750 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62  me);.  if( !pTab
34751 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   || pTab->pSelec
34752 74 20 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20  t ){.    pTab = 
34753 30 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f  0;.    goto erro
34754 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  r_out;.  }..  /*
34755 20 46 69 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e   Find the column
34756 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66 6f 20   for which info 
34757 69 73 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a  is requested */.
34758 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52    if( sqlite3IsR
34759 6f 77 69 64 28 7a 43 6f 6c 75 6d 6e 4e 61 6d 65  owid(zColumnName
3475a 29 20 29 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20  ) ){.    iCol = 
3475b 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20  pTab->iPKey;.   
3475c 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a   if( iCol>=0 ){.
3475d 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 54        pCol = &pT
3475e 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a  ab->aCol[iCol];.
3475f 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
34760 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69     for(iCol=0; i
34761 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  Col<pTab->nCol; 
34762 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 70  iCol++){.      p
34763 43 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f  Col = &pTab->aCo
34764 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 20 20 69  l[iCol];.      i
34765 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  f( 0==sqlite3Str
34766 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  ICmp(pCol->zName
34767 2c 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 20 29  , zColumnName) )
34768 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
34769 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
3476a 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61     if( iCol==pTa
3476b 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
3476c 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20   pTab = 0;.     
3476d 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f 75 74 3b   goto error_out;
3476e 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
3476f 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   The following b
34770 6c 6f 63 6b 20 73 74 6f 72 65 73 20 74 68 65 20  lock stores the 
34771 6d 65 74 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  meta information
34772 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65   that will be re
34773 74 75 72 6e 65 64 0a 20 20 2a 2a 20 74 6f 20 74  turned.  ** to t
34774 68 65 20 63 61 6c 6c 65 72 20 69 6e 20 6c 6f 63  he caller in loc
34775 61 6c 20 76 61 72 69 61 62 6c 65 73 20 7a 44 61  al variables zDa
34776 74 61 54 79 70 65 2c 20 7a 43 6f 6c 6c 53 65 71  taType, zCollSeq
34777 2c 20 6e 6f 74 6e 75 6c 6c 2c 20 70 72 69 6d 61  , notnull, prima
34778 72 79 6b 65 79 0a 20 20 2a 2a 20 61 6e 64 20 61  rykey.  ** and a
34779 75 74 6f 69 6e 63 2e 20 41 74 20 74 68 69 73 20  utoinc. At this 
3477a 70 6f 69 6e 74 20 74 68 65 72 65 20 61 72 65 20  point there are 
3477b 74 77 6f 20 70 6f 73 73 69 62 69 6c 69 74 69 65  two possibilitie
3477c 73 3a 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 20 20  s:.  ** .  **   
3477d 20 20 31 2e 20 54 68 65 20 73 70 65 63 69 66 69    1. The specifi
3477e 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 77  ed column name w
3477f 61 73 20 72 6f 77 69 64 22 2c 20 22 6f 69 64 22  as rowid", "oid"
34780 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22 20 0a 20   or "_rowid_" . 
34781 20 2a 2a 20 20 20 20 20 20 20 20 61 6e 64 20 74   **        and t
34782 68 65 72 65 20 69 73 20 6e 6f 20 65 78 70 6c 69  here is no expli
34783 63 69 74 6c 79 20 64 65 63 6c 61 72 65 64 20 49  citly declared I
34784 50 4b 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2a  PK column. .  **
34785 0a 20 20 2a 2a 20 20 20 20 20 32 2e 20 54 68 65  .  **     2. The
34786 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 61 20   table is not a 
34787 76 69 65 77 20 61 6e 64 20 74 68 65 20 63 6f 6c  view and the col
34788 75 6d 6e 20 6e 61 6d 65 20 69 64 65 6e 74 69 66  umn name identif
34789 69 65 64 20 61 6e 20 0a 20 20 2a 2a 20 20 20 20  ied an .  **    
3478a 20 20 20 20 65 78 70 6c 69 63 69 74 6c 79 20 64      explicitly d
3478b 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20  eclared column. 
3478c 43 6f 70 79 20 6d 65 74 61 20 69 6e 66 6f 72 6d  Copy meta inform
3478d 61 74 69 6f 6e 20 66 72 6f 6d 20 2a 70 43 6f 6c  ation from *pCol
3478e 2e 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 70 43  ..  */ .  if( pC
3478f 6f 6c 20 29 7b 0a 20 20 20 20 7a 44 61 74 61 54  ol ){.    zDataT
34790 79 70 65 20 3d 20 70 43 6f 6c 2d 3e 7a 54 79 70  ype = pCol->zTyp
34791 65 3b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71 20  e;.    zCollSeq 
34792 3d 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 3b 0a 20  = pCol->zColl;. 
34793 20 20 20 6e 6f 74 6e 75 6c 6c 20 3d 20 70 43 6f     notnull = pCo
34794 6c 2d 3e 6e 6f 74 4e 75 6c 6c 21 3d 30 3b 0a 20  l->notNull!=0;. 
34795 20 20 20 70 72 69 6d 61 72 79 6b 65 79 20 20 3d     primarykey  =
34796 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79   pCol->isPrimKey
34797 21 3d 30 3b 0a 20 20 20 20 61 75 74 6f 69 6e 63  !=0;.    autoinc
34798 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3d 3d   = pTab->iPKey==
34799 69 43 6f 6c 20 26 26 20 28 70 54 61 62 2d 3e 74  iCol && (pTab->t
3479a 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74  abFlags & TF_Aut
3479b 6f 69 6e 63 72 65 6d 65 6e 74 29 21 3d 30 3b 0a  oincrement)!=0;.
3479c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61    }else{.    zDa
3479d 74 61 54 79 70 65 20 3d 20 22 49 4e 54 45 47 45  taType = "INTEGE
3479e 52 22 3b 0a 20 20 20 20 70 72 69 6d 61 72 79 6b  R";.    primaryk
3479f 65 79 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66  ey = 1;.  }.  if
347a0 28 20 21 7a 43 6f 6c 6c 53 65 71 20 29 7b 0a 20  ( !zCollSeq ){. 
347a1 20 20 20 7a 43 6f 6c 6c 53 65 71 20 3d 20 22 42     zCollSeq = "B
347a2 49 4e 41 52 59 22 3b 0a 20 20 7d 0a 0a 65 72 72  INARY";.  }..err
347a3 6f 72 5f 6f 75 74 3a 0a 20 20 73 71 6c 69 74 65  or_out:.  sqlite
347a4 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
347a5 62 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69  b);.  (void)sqli
347a6 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
347a7 3b 0a 0a 20 20 2f 2a 20 57 68 65 74 68 65 72 20  ;..  /* Whether 
347a8 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  the function cal
347a9 6c 20 73 75 63 63 65 65 64 65 64 20 6f 72 20 66  l succeeded or f
347aa 61 69 6c 65 64 2c 20 73 65 74 20 74 68 65 20 6f  ailed, set the o
347ab 75 74 70 75 74 20 70 61 72 61 6d 65 74 65 72 73  utput parameters
347ac 0a 20 20 2a 2a 20 74 6f 20 77 68 61 74 65 76 65  .  ** to whateve
347ad 72 20 74 68 65 69 72 20 6c 6f 63 61 6c 20 63 6f  r their local co
347ae 75 6e 74 65 72 70 61 72 74 73 20 63 6f 6e 74 61  unterparts conta
347af 69 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  in. If an error 
347b0 64 69 64 20 6f 63 63 75 72 2c 0a 20 20 2a 2a 20  did occur,.  ** 
347b1 74 68 69 73 20 68 61 73 20 74 68 65 20 65 66 66  this has the eff
347b2 65 63 74 20 6f 66 20 7a 65 72 6f 69 6e 67 20 61  ect of zeroing a
347b3 6c 6c 20 6f 75 74 70 75 74 20 70 61 72 61 6d 65  ll output parame
347b4 74 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ters..  */.  if(
347b5 20 70 7a 44 61 74 61 54 79 70 65 20 29 20 2a 70   pzDataType ) *p
347b6 7a 44 61 74 61 54 79 70 65 20 3d 20 7a 44 61 74  zDataType = zDat
347b7 61 54 79 70 65 3b 0a 20 20 69 66 28 20 70 7a 43  aType;.  if( pzC
347b8 6f 6c 6c 53 65 71 20 29 20 2a 70 7a 43 6f 6c 6c  ollSeq ) *pzColl
347b9 53 65 71 20 3d 20 7a 43 6f 6c 6c 53 65 71 3b 0a  Seq = zCollSeq;.
347ba 20 20 69 66 28 20 70 4e 6f 74 4e 75 6c 6c 20 29    if( pNotNull )
347bb 20 2a 70 4e 6f 74 4e 75 6c 6c 20 3d 20 6e 6f 74   *pNotNull = not
347bc 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 70 50 72 69  null;.  if( pPri
347bd 6d 61 72 79 4b 65 79 20 29 20 2a 70 50 72 69 6d  maryKey ) *pPrim
347be 61 72 79 4b 65 79 20 3d 20 70 72 69 6d 61 72 79  aryKey = primary
347bf 6b 65 79 3b 0a 20 20 69 66 28 20 70 41 75 74 6f  key;.  if( pAuto
347c0 69 6e 63 20 29 20 2a 70 41 75 74 6f 69 6e 63 20  inc ) *pAutoinc 
347c1 3d 20 61 75 74 6f 69 6e 63 3b 0a 0a 20 20 69 66  = autoinc;..  if
347c2 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20  ( SQLITE_OK==rc 
347c3 26 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20  && !pTab ){.    
347c4 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
347c5 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  , zErrMsg);.    
347c6 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
347c7 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f  3MPrintf(db, "no
347c8 20 73 75 63 68 20 74 61 62 6c 65 20 63 6f 6c 75   such table colu
347c9 6d 6e 3a 20 25 73 2e 25 73 22 2c 20 7a 54 61 62  mn: %s.%s", zTab
347ca 6c 65 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  leName,.        
347cb 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 29 3b 0a 20 20  zColumnName);.  
347cc 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
347cd 52 4f 52 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ROR;.  }.  sqlit
347ce 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20  e3Error(db, rc, 
347cf 28 7a 45 72 72 4d 73 67 3f 22 25 73 22 3a 30 29  (zErrMsg?"%s":0)
347d0 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71  , zErrMsg);.  sq
347d1 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
347d2 7a 45 72 72 4d 73 67 29 3b 0a 20 20 72 63 20 3d  zErrMsg);.  rc =
347d3 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
347d4 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74  db, rc);.  sqlit
347d5 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
347d6 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
347d7 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66  urn rc;.}.#endif
347d8 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f  ../*.** Sleep fo
347d9 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65  r a little while
347da 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d  .  Return the am
347db 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65  ount of time sle
347dc 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  pt..*/.SQLITE_AP
347dd 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c  I int sqlite3_sl
347de 65 65 70 28 69 6e 74 20 6d 73 29 7b 0a 20 20 73  eep(int ms){.  s
347df 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
347e0 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 70 56  ;.  int rc;.  pV
347e1 66 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66 73  fs = sqlite3_vfs
347e2 5f 66 69 6e 64 28 30 29 3b 0a 20 20 69 66 28 20  _find(0);.  if( 
347e3 70 56 66 73 3d 3d 30 20 29 20 72 65 74 75 72 6e  pVfs==0 ) return
347e4 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66   0;..  /* This f
347e5 75 6e 63 74 69 6f 6e 20 77 6f 72 6b 73 20 69 6e  unction works in
347e6 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 2c 20 62   milliseconds, b
347e7 75 74 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  ut the underlyin
347e8 67 20 4f 73 53 6c 65 65 70 28 29 20 0a 20 20 2a  g OsSleep() .  *
347e9 2a 20 41 50 49 20 75 73 65 73 20 6d 69 63 72 6f  * API uses micro
347ea 73 65 63 6f 6e 64 73 2e 20 48 65 6e 63 65 20 74  seconds. Hence t
347eb 68 65 20 31 30 30 30 27 73 2e 0a 20 20 2a 2f 0a  he 1000's..  */.
347ec 20 20 72 63 20 3d 20 28 73 71 6c 69 74 65 33 4f    rc = (sqlite3O
347ed 73 53 6c 65 65 70 28 70 56 66 73 2c 20 31 30 30  sSleep(pVfs, 100
347ee 30 2a 6d 73 29 2f 31 30 30 30 29 3b 0a 20 20 72  0*ms)/1000);.  r
347ef 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
347f0 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69 73  ** Enable or dis
347f1 61 62 6c 65 20 74 68 65 20 65 78 74 65 6e 64 65  able the extende
347f2 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 2e 0a  d result codes..
347f3 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
347f4 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64  t sqlite3_extend
347f5 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28  ed_result_codes(
347f6 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
347f7 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c 69 74   onoff){.  sqlit
347f8 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
347f9 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d  b->mutex);.  db-
347fa 3e 65 72 72 4d 61 73 6b 20 3d 20 6f 6e 6f 66 66  >errMask = onoff
347fb 20 3f 20 30 78 66 66 66 66 66 66 66 66 20 3a 20   ? 0xffffffff : 
347fc 30 78 66 66 3b 0a 20 20 73 71 6c 69 74 65 33 5f  0xff;.  sqlite3_
347fd 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
347fe 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
347ff 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
34800 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20  *.** Invoke the 
34801 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74  xFileControl met
34802 68 6f 64 20 6f 6e 20 61 20 70 61 72 74 69 63 75  hod on a particu
34803 6c 61 72 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f  lar database..*/
34804 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
34805 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
34806 74 72 6f 6c 28 73 71 6c 69 74 65 33 20 2a 64 62  trol(sqlite3 *db
34807 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
34808 62 4e 61 6d 65 2c 20 69 6e 74 20 6f 70 2c 20 76  bName, int op, v
34809 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 69 6e  oid *pArg){.  in
3480a 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52  t rc = SQLITE_ER
3480b 52 4f 52 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  ROR;.  int iDb;.
3480c 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
3480d 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
3480e 3b 0a 20 20 69 66 28 20 7a 44 62 4e 61 6d 65 3d  ;.  if( zDbName=
3480f 3d 30 20 29 7b 0a 20 20 20 20 69 44 62 20 3d 20  =0 ){.    iDb = 
34810 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
34811 66 6f 72 28 69 44 62 3d 30 3b 20 69 44 62 3c 64  for(iDb=0; iDb<d
34812 62 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a  b->nDb; iDb++){.
34813 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70        if( strcmp
34814 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e  (db->aDb[iDb].zN
34815 61 6d 65 2c 20 7a 44 62 4e 61 6d 65 29 3d 3d 30  ame, zDbName)==0
34816 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
34817 20 20 7d 0a 20 20 69 66 28 20 69 44 62 3c 64 62    }.  if( iDb<db
34818 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 42 74 72  ->nDb ){.    Btr
34819 65 65 20 2a 70 42 74 72 65 65 20 3d 20 64 62 2d  ee *pBtree = db-
3481a 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20  >aDb[iDb].pBt;. 
3481b 20 20 20 69 66 28 20 70 42 74 72 65 65 20 29 7b     if( pBtree ){
3481c 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50  .      Pager *pP
3481d 61 67 65 72 3b 0a 20 20 20 20 20 20 73 71 6c 69  ager;.      sqli
3481e 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b 0a 20 20  te3_file *fd;.  
3481f 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
34820 45 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20  Enter(pBtree);. 
34821 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71       pPager = sq
34822 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
34823 70 42 74 72 65 65 29 3b 0a 20 20 20 20 20 20 61  pBtree);.      a
34824 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d 30  ssert( pPager!=0
34825 20 29 3b 0a 20 20 20 20 20 20 66 64 20 3d 20 73   );.      fd = s
34826 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28  qlite3PagerFile(
34827 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 61  pPager);.      a
34828 73 73 65 72 74 28 20 66 64 21 3d 30 20 29 3b 0a  ssert( fd!=0 );.
34829 20 20 20 20 20 20 69 66 28 20 66 64 2d 3e 70 4d        if( fd->pM
3482a 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20  ethods ){.      
3482b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
3482c 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 66 64 2c 20  FileControl(fd, 
3482d 6f 70 2c 20 70 41 72 67 29 3b 0a 20 20 20 20 20  op, pArg);.     
3482e 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
3482f 42 74 72 65 65 4c 65 61 76 65 28 70 42 74 72 65  BtreeLeave(pBtre
34830 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e);.    }.  }.  
34831 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
34832 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
34833 20 20 72 65 74 75 72 6e 20 72 63 3b 20 20 20 0a    return rc;   .
34834 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61  }../*.** Interfa
34835 63 65 20 74 6f 20 74 68 65 20 74 65 73 74 69 6e  ce to the testin
34836 67 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 53 51 4c 49  g logic..*/.SQLI
34837 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
34838 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
34839 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20  int op, ...){.  
3483a 69 6e 74 20 72 63 20 3d 20 30 3b 0a 23 69 66 6e  int rc = 0;.#ifn
3483b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
3483c 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a 20 20 76  BUILTIN_TEST.  v
3483d 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 76 61 5f  a_list ap;.  va_
3483e 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b 0a 20  start(ap, op);. 
3483f 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 0a   switch( op ){..
34840 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 53 61      /*.    ** Sa
34841 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  ve the current s
34842 74 61 74 65 20 6f 66 20 74 68 65 20 50 52 4e 47  tate of the PRNG
34843 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
34844 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  e SQLITE_TESTCTR
34845 4c 5f 50 52 4e 47 5f 53 41 56 45 3a 20 7b 0a 20  L_PRNG_SAVE: {. 
34846 20 20 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67       sqlite3Prng
34847 53 61 76 65 53 74 61 74 65 28 29 3b 0a 20 20 20  SaveState();.   
34848 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
34849 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 52  .    /*.    ** R
3484a 65 73 74 6f 72 65 20 74 68 65 20 73 74 61 74 65  estore the state
3484b 20 6f 66 20 74 68 65 20 50 52 4e 47 20 74 6f 20   of the PRNG to 
3484c 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 20 73  the last state s
3484d 61 76 65 64 20 75 73 69 6e 67 0a 20 20 20 20 2a  aved using.    *
3484e 2a 20 50 52 4e 47 5f 53 41 56 45 2e 20 20 49 66  * PRNG_SAVE.  If
3484f 20 50 52 4e 47 5f 53 41 56 45 20 68 61 73 20 6e   PRNG_SAVE has n
34850 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e  ever before been
34851 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 0a 20 20   called, then.  
34852 20 20 2a 2a 20 74 68 69 73 20 76 65 72 62 20 61    ** this verb a
34853 63 74 73 20 6c 69 6b 65 20 50 52 4e 47 5f 52 45  cts like PRNG_RE
34854 53 45 54 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  SET..    */.    
34855 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
34856 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53 54 4f 52  CTRL_PRNG_RESTOR
34857 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  E: {.      sqlit
34858 65 33 50 72 6e 67 52 65 73 74 6f 72 65 53 74 61  e3PrngRestoreSta
34859 74 65 28 29 3b 0a 20 20 20 20 20 20 62 72 65 61  te();.      brea
3485a 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
3485b 0a 20 20 20 20 2a 2a 20 52 65 73 65 74 20 74 68  .    ** Reset th
3485c 65 20 50 52 4e 47 20 62 61 63 6b 20 74 6f 20 69  e PRNG back to i
3485d 74 73 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  ts uninitialized
3485e 20 73 74 61 74 65 2e 20 20 54 68 65 20 6e 65 78   state.  The nex
3485f 74 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f  t call.    ** to
34860 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
34861 65 73 73 28 29 20 77 69 6c 6c 20 72 65 73 65 65  ess() will resee
34862 64 20 74 68 65 20 50 52 4e 47 20 75 73 69 6e 67  d the PRNG using
34863 20 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 0a 20   a single call. 
34864 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 78 52 61     ** to the xRa
34865 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20  ndomness method 
34866 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 56  of the default V
34867 46 53 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  FS..    */.    c
34868 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
34869 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54 3a 20  TRL_PRNG_RESET: 
3486a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 50  {.      sqlite3P
3486b 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 29 3b  rngResetState();
3486c 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
3486d 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
3486e 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74  **  sqlite3_test
3486f 5f 63 6f 6e 74 72 6f 6c 28 42 49 54 56 45 43 5f  _control(BITVEC_
34870 54 45 53 54 2c 20 73 69 7a 65 2c 20 70 72 6f 67  TEST, size, prog
34871 72 61 6d 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ram).    **.    
34872 2a 2a 20 52 75 6e 20 61 20 74 65 73 74 20 61 67  ** Run a test ag
34873 61 69 6e 73 74 20 61 20 42 69 74 76 65 63 20 6f  ainst a Bitvec o
34874 62 6a 65 63 74 20 6f 66 20 73 69 7a 65 2e 20 20  bject of size.  
34875 54 68 65 20 70 72 6f 67 72 61 6d 20 61 72 67 75  The program argu
34876 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 69 73 20 61  ment.    ** is a
34877 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74 65 67  n array of integ
34878 65 72 73 20 74 68 61 74 20 64 65 66 69 6e 65 73  ers that defines
34879 20 74 68 65 20 74 65 73 74 2e 20 20 52 65 74 75   the test.  Retu
3487a 72 6e 20 2d 31 20 6f 6e 20 61 0a 20 20 20 20 2a  rn -1 on a.    *
3487b 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  * memory allocat
3487c 69 6f 6e 20 65 72 72 6f 72 2c 20 30 20 6f 6e 20  ion error, 0 on 
3487d 73 75 63 63 65 73 73 2c 20 6f 72 20 6e 6f 6e 2d  success, or non-
3487e 7a 65 72 6f 20 66 6f 72 20 61 6e 20 65 72 72 6f  zero for an erro
3487f 72 2e 0a 20 20 20 20 2a 2a 20 53 65 65 20 74 68  r..    ** See th
34880 65 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 42  e sqlite3BitvecB
34881 75 69 6c 74 69 6e 54 65 73 74 28 29 20 66 6f 72  uiltinTest() for
34882 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
34883 72 6d 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  rmation..    */.
34884 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
34885 54 45 53 54 43 54 52 4c 5f 42 49 54 56 45 43 5f  TESTCTRL_BITVEC_
34886 54 45 53 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e  TEST: {.      in
34887 74 20 73 7a 20 3d 20 76 61 5f 61 72 67 28 61 70  t sz = va_arg(ap
34888 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 6e  , int);.      in
34889 74 20 2a 61 50 72 6f 67 20 3d 20 76 61 5f 61 72  t *aProg = va_ar
3488a 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20  g(ap, int*);.   
3488b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
3488c 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74  itvecBuiltinTest
3488d 28 73 7a 2c 20 61 50 72 6f 67 29 3b 0a 20 20 20  (sz, aProg);.   
3488e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
3488f 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20  .    /*.    **  
34890 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
34891 74 72 6f 6c 28 42 45 4e 49 47 4e 5f 4d 41 4c 4c  trol(BENIGN_MALL
34892 4f 43 5f 48 4f 4f 4b 53 2c 20 78 42 65 67 69 6e  OC_HOOKS, xBegin
34893 2c 20 78 45 6e 64 29 0a 20 20 20 20 2a 2a 0a 20  , xEnd).    **. 
34894 20 20 20 2a 2a 20 52 65 67 69 73 74 65 72 20 68     ** Register h
34895 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 74 6f 20  ooks to call to 
34896 69 6e 64 69 63 61 74 65 20 77 68 69 63 68 20 6d  indicate which m
34897 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 73  alloc() failures
34898 20 0a 20 20 20 20 2a 2a 20 61 72 65 20 62 65 6e   .    ** are ben
34899 69 67 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ign..    */.    
3489a 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54  case SQLITE_TEST
3489b 43 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c  CTRL_BENIGN_MALL
3489c 4f 43 5f 48 4f 4f 4b 53 3a 20 7b 0a 20 20 20 20  OC_HOOKS: {.    
3489d 20 20 74 79 70 65 64 65 66 20 76 6f 69 64 20 28    typedef void (
3489e 2a 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 29 28  *void_function)(
3489f 76 6f 69 64 29 3b 0a 20 20 20 20 20 20 76 6f 69  void);.      voi
348a0 64 5f 66 75 6e 63 74 69 6f 6e 20 78 42 65 6e 69  d_function xBeni
348a1 67 6e 42 65 67 69 6e 3b 0a 20 20 20 20 20 20 76  gnBegin;.      v
348a2 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 20 78 42 65  oid_function xBe
348a3 6e 69 67 6e 45 6e 64 3b 0a 20 20 20 20 20 20 78  nignEnd;.      x
348a4 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 76 61  BenignBegin = va
348a5 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 5f 66 75  _arg(ap, void_fu
348a6 6e 63 74 69 6f 6e 29 3b 0a 20 20 20 20 20 20 78  nction);.      x
348a7 42 65 6e 69 67 6e 45 6e 64 20 3d 20 76 61 5f 61  BenignEnd = va_a
348a8 72 67 28 61 70 2c 20 76 6f 69 64 5f 66 75 6e 63  rg(ap, void_func
348a9 74 69 6f 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c  tion);.      sql
348aa 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  ite3BenignMalloc
348ab 48 6f 6f 6b 73 28 78 42 65 6e 69 67 6e 42 65 67  Hooks(xBenignBeg
348ac 69 6e 2c 20 78 42 65 6e 69 67 6e 45 6e 64 29 3b  in, xBenignEnd);
348ad 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
348ae 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20    }..    /*.    
348af 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74  **  sqlite3_test
348b0 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f  _control(SQLITE_
348b1 54 45 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47  TESTCTRL_PENDING
348b2 5f 42 59 54 45 2c 20 75 6e 73 69 67 6e 65 64 20  _BYTE, unsigned 
348b3 69 6e 74 20 58 29 0a 20 20 20 20 2a 2a 0a 20 20  int X).    **.  
348b4 20 20 2a 2a 20 53 65 74 20 74 68 65 20 50 45 4e    ** Set the PEN
348b5 44 49 4e 47 20 62 79 74 65 20 74 6f 20 74 68 65  DING byte to the
348b6 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 61 72   value in the ar
348b7 67 75 6d 65 6e 74 2c 20 69 66 20 58 3e 30 2e 0a  gument, if X>0..
348b8 20 20 20 20 2a 2a 20 4d 61 6b 65 20 6e 6f 20 63      ** Make no c
348b9 68 61 6e 67 65 73 20 69 66 20 58 3d 3d 30 2e 20  hanges if X==0. 
348ba 20 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75   Return the valu
348bb 65 20 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67  e of the pending
348bc 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 61 73 20   byte.    ** as 
348bd 69 74 20 65 78 69 73 74 69 6e 67 20 62 65 66 6f  it existing befo
348be 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  re this routine 
348bf 77 61 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20  was called..    
348c0 2a 2a 0a 20 20 20 20 2a 2a 20 49 4d 50 4f 52 54  **.    ** IMPORT
348c1 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74  ANT:  Changing t
348c2 68 65 20 50 45 4e 44 49 4e 47 20 62 79 74 65 20  he PENDING byte 
348c3 66 72 6f 6d 20 30 78 34 30 30 30 30 30 30 30 20  from 0x40000000 
348c4 72 65 73 75 6c 74 73 20 69 6e 0a 20 20 20 20 2a  results in.    *
348c5 2a 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c  * an incompatibl
348c6 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
348c7 66 6f 72 6d 61 74 2e 20 20 43 68 61 6e 67 69 6e  format.  Changin
348c8 67 20 74 68 65 20 50 45 4e 44 49 4e 47 20 62 79  g the PENDING by
348c9 74 65 0a 20 20 20 20 2a 2a 20 77 68 69 6c 65 20  te.    ** while 
348ca 61 6e 79 20 64 61 74 61 62 61 73 65 20 63 6f 6e  any database con
348cb 6e 65 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20  nection is open 
348cc 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64 65 66  results in undef
348cd 69 6e 65 64 20 61 6e 64 0a 20 20 20 20 2a 2a 20  ined and.    ** 
348ce 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68 61  dileterious beha
348cf 76 69 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  vior..    */.   
348d0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
348d1 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42 59  TCTRL_PENDING_BY
348d2 54 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 73 69  TE: {.      unsi
348d3 67 6e 65 64 20 69 6e 74 20 6e 65 77 56 61 6c 20  gned int newVal 
348d4 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 75 6e 73  = va_arg(ap, uns
348d5 69 67 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20  igned int);.    
348d6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 65    rc = sqlite3Pe
348d7 6e 64 69 6e 67 42 79 74 65 3b 0a 20 20 20 20 20  ndingByte;.     
348d8 20 69 66 28 20 6e 65 77 56 61 6c 20 29 20 73 71   if( newVal ) sq
348d9 6c 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65  lite3PendingByte
348da 20 3d 20 6e 65 77 56 61 6c 3b 0a 20 20 20 20 20   = newVal;.     
348db 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
348dc 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71     /*.    **  sq
348dd 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72  lite3_test_contr
348de 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54  ol(SQLITE_TESTCT
348df 52 4c 5f 41 53 53 45 52 54 2c 20 69 6e 74 20 58  RL_ASSERT, int X
348e0 29 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ).    **.    ** 
348e1 54 68 69 73 20 61 63 74 69 6f 6e 20 70 72 6f 76  This action prov
348e2 69 64 65 73 20 61 20 72 75 6e 2d 74 69 6d 65 20  ides a run-time 
348e3 74 65 73 74 20 74 6f 20 73 65 65 20 77 68 65 74  test to see whet
348e4 68 65 72 20 6f 72 20 6e 6f 74 0a 20 20 20 20 2a  her or not.    *
348e5 2a 20 61 73 73 65 72 74 28 29 20 77 61 73 20 65  * assert() was e
348e6 6e 61 62 6c 65 64 20 61 74 20 63 6f 6d 70 69 6c  nabled at compil
348e7 65 2d 74 69 6d 65 2e 20 20 49 66 20 58 20 69 73  e-time.  If X is
348e8 20 74 72 75 65 20 61 6e 64 20 61 73 73 65 72 74   true and assert
348e9 28 29 0a 20 20 20 20 2a 2a 20 69 73 20 65 6e 61  ().    ** is ena
348ea 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 72  bled, then the r
348eb 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74  eturn value is t
348ec 72 75 65 2e 20 20 49 66 20 58 20 69 73 20 74 72  rue.  If X is tr
348ed 75 65 20 61 6e 64 0a 20 20 20 20 2a 2a 20 61 73  ue and.    ** as
348ee 73 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c  sert() is disabl
348ef 65 64 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74  ed, then the ret
348f0 75 72 6e 20 76 61 6c 75 65 20 69 73 20 7a 65 72  urn value is zer
348f1 6f 2e 20 20 49 66 20 58 20 69 73 0a 20 20 20 20  o.  If X is.    
348f2 2a 2a 20 66 61 6c 73 65 20 61 6e 64 20 61 73 73  ** false and ass
348f3 65 72 74 28 29 20 69 73 20 65 6e 61 62 6c 65 64  ert() is enabled
348f4 2c 20 74 68 65 6e 20 74 68 65 20 61 73 73 65 72  , then the asser
348f5 74 69 6f 6e 20 66 69 72 65 73 20 61 6e 64 20 74  tion fires and t
348f6 68 65 0a 20 20 20 20 2a 2a 20 70 72 6f 63 65 73  he.    ** proces
348f7 73 20 61 62 6f 72 74 73 2e 20 20 49 66 20 58 20  s aborts.  If X 
348f8 69 73 20 66 61 6c 73 65 20 61 6e 64 20 61 73 73  is false and ass
348f9 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65  ert() is disable
348fa 64 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 20 20  d, then the.    
348fb 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  ** return value 
348fc 69 73 20 7a 65 72 6f 2e 0a 20 20 20 20 2a 2f 0a  is zero..    */.
348fd 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
348fe 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54 3a  TESTCTRL_ASSERT:
348ff 20 7b 0a 20 20 20 20 20 20 76 6f 6c 61 74 69 6c   {.      volatil
34900 65 20 69 6e 74 20 78 20 3d 20 30 3b 0a 20 20 20  e int x = 0;.   
34901 20 20 20 61 73 73 65 72 74 28 20 28 78 20 3d 20     assert( (x = 
34902 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 29 21  va_arg(ap,int))!
34903 3d 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  =0 );.      rc =
34904 20 78 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   x;.      break;
34905 0a 20 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 0a  .    }...    /*.
34906 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f      **  sqlite3_
34907 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c  test_control(SQL
34908 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57  ITE_TESTCTRL_ALW
34909 41 59 53 2c 20 69 6e 74 20 58 29 0a 20 20 20 20  AYS, int X).    
3490a 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 61  **.    ** This a
3490b 63 74 69 6f 6e 20 70 72 6f 76 69 64 65 73 20 61  ction provides a
3490c 20 72 75 6e 2d 74 69 6d 65 20 74 65 73 74 20 74   run-time test t
3490d 6f 20 73 65 65 20 68 6f 77 20 74 68 65 20 41 4c  o see how the AL
3490e 57 41 59 53 20 61 6e 64 0a 20 20 20 20 2a 2a 20  WAYS and.    ** 
3490f 4e 45 56 45 52 20 6d 61 63 72 6f 73 20 77 65 72  NEVER macros wer
34910 65 20 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d  e defined at com
34911 70 69 6c 65 2d 74 69 6d 65 2e 0a 20 20 20 20 2a  pile-time..    *
34912 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 74  *.    ** The ret
34913 75 72 6e 20 76 61 6c 75 65 20 69 73 20 41 4c 57  urn value is ALW
34914 41 59 53 28 58 29 2e 20 20 0a 20 20 20 20 2a 2a  AYS(X).  .    **
34915 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 63 6f  .    ** The reco
34916 6d 6d 65 6e 64 65 64 20 74 65 73 74 20 69 73 20  mmended test is 
34917 58 3d 3d 32 2e 20 20 49 66 20 74 68 65 20 72 65  X==2.  If the re
34918 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 32 2c  turn value is 2,
34919 20 74 68 61 74 20 6d 65 61 6e 73 0a 20 20 20 20   that means.    
3491a 2a 2a 20 41 4c 57 41 59 53 28 29 20 61 6e 64 20  ** ALWAYS() and 
3491b 4e 45 56 45 52 28 29 20 61 72 65 20 62 6f 74 68  NEVER() are both
3491c 20 6e 6f 2d 6f 70 20 70 61 73 73 2d 74 68 72 6f   no-op pass-thro
3491d 75 67 68 20 6d 61 63 72 6f 73 2c 20 77 68 69 63  ugh macros, whic
3491e 68 20 69 73 20 74 68 65 0a 20 20 20 20 2a 2a 20  h is the.    ** 
3491f 64 65 66 61 75 6c 74 20 73 65 74 74 69 6e 67 2e  default setting.
34920 20 20 49 66 20 74 68 65 20 72 65 74 75 72 6e 20    If the return 
34921 76 61 6c 75 65 20 69 73 20 31 2c 20 74 68 65 6e  value is 1, then
34922 20 41 4c 57 41 59 53 28 29 20 69 73 20 65 69 74   ALWAYS() is eit
34923 68 65 72 0a 20 20 20 20 2a 2a 20 68 61 72 64 2d  her.    ** hard-
34924 63 6f 64 65 64 20 74 6f 20 74 72 75 65 20 6f 72  coded to true or
34925 20 65 6c 73 65 20 69 74 20 61 73 73 65 72 74 73   else it asserts
34926 20 69 66 20 69 74 73 20 61 72 67 75 6d 65 6e 74   if its argument
34927 20 69 73 20 66 61 6c 73 65 2e 0a 20 20 20 20 2a   is false..    *
34928 2a 20 54 68 65 20 66 69 72 73 74 20 62 65 68 61  * The first beha
34929 76 69 6f 72 20 28 68 61 72 64 2d 63 6f 64 65 64  vior (hard-coded
3492a 20 74 6f 20 74 72 75 65 29 20 69 73 20 74 68 65   to true) is the
3492b 20 63 61 73 65 20 69 66 0a 20 20 20 20 2a 2a 20   case if.    ** 
3492c 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3492d 41 53 53 45 52 54 20 73 68 6f 77 73 20 74 68 61  ASSERT shows tha
3492e 74 20 61 73 73 65 72 74 28 29 20 69 73 20 64 69  t assert() is di
3492f 73 61 62 6c 65 64 20 61 6e 64 20 74 68 65 20 73  sabled and the s
34930 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 62 65 68  econd.    ** beh
34931 61 76 69 6f 72 20 28 61 73 73 65 72 74 20 69 66  avior (assert if
34932 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f   the argument to
34933 20 41 4c 57 41 59 53 28 29 20 69 73 20 66 61 6c   ALWAYS() is fal
34934 73 65 29 20 69 73 20 74 68 65 20 63 61 73 65 20  se) is the case 
34935 69 66 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54 45  if.    ** SQLITE
34936 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54  _TESTCTRL_ASSERT
34937 20 73 68 6f 77 73 20 74 68 61 74 20 61 73 73 65   shows that asse
34938 72 74 28 29 20 69 73 20 65 6e 61 62 6c 65 64 2e  rt() is enabled.
34939 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
3493a 68 65 20 72 75 6e 2d 74 69 6d 65 20 74 65 73 74  he run-time test
3493b 20 70 72 6f 63 65 64 75 72 65 20 6d 69 67 68 74   procedure might
3493c 20 6c 6f 6f 6b 20 73 6f 6d 65 74 68 69 6e 67 20   look something 
3493d 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 20 20 2a  like this:.    *
3493e 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69 66 28 20  *.    **    if( 
3493f 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
34940 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54  trol(SQLITE_TEST
34941 43 54 52 4c 5f 41 4c 57 41 59 53 2c 20 32 29 3d  CTRL_ALWAYS, 2)=
34942 3d 32 20 29 7b 0a 20 20 20 20 2a 2a 20 20 20 20  =2 ){.    **    
34943 20 20 2f 2f 20 41 4c 57 41 59 53 28 29 20 61 6e    // ALWAYS() an
34944 64 20 4e 45 56 45 52 28 29 20 61 72 65 20 6e 6f  d NEVER() are no
34945 2d 6f 70 20 70 61 73 73 2d 74 68 72 6f 75 67 68  -op pass-through
34946 20 6d 61 63 72 6f 73 0a 20 20 20 20 2a 2a 20 20   macros.    **  
34947 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
34948 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
34949 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c  (SQLITE_TESTCTRL
3494a 5f 41 53 53 45 52 54 2c 20 31 29 20 29 7b 0a 20  _ASSERT, 1) ){. 
3494b 20 20 20 2a 2a 20 20 20 20 20 20 2f 2f 20 41 4c     **      // AL
3494c 57 41 59 53 28 78 29 20 61 73 73 65 72 74 73 20  WAYS(x) asserts 
3494d 74 68 61 74 20 78 20 69 73 20 74 72 75 65 2e 20  that x is true. 
3494e 4e 45 56 45 52 28 78 29 20 61 73 73 65 72 74 73  NEVER(x) asserts
3494f 20 78 20 69 73 20 66 61 6c 73 65 2e 0a 20 20 20   x is false..   
34950 20 2a 2a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   **    }else{.  
34951 20 20 2a 2a 20 20 20 20 20 20 2f 2f 20 41 4c 57    **      // ALW
34952 41 59 53 28 78 29 20 69 73 20 61 20 63 6f 6e 73  AYS(x) is a cons
34953 74 61 6e 74 20 31 2e 20 20 4e 45 56 45 52 28 78  tant 1.  NEVER(x
34954 29 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20  ) is a constant 
34955 30 2e 0a 20 20 20 20 2a 2a 20 20 20 20 7d 0a 20  0..    **    }. 
34956 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
34957 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41  QLITE_TESTCTRL_A
34958 4c 57 41 59 53 3a 20 7b 0a 20 20 20 20 20 20 69  LWAYS: {.      i
34959 6e 74 20 78 20 3d 20 76 61 5f 61 72 67 28 61 70  nt x = va_arg(ap
3495a 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 72 63 20  ,int);.      rc 
3495b 3d 20 41 4c 57 41 59 53 28 78 29 3b 0a 20 20 20  = ALWAYS(x);.   
3495c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
3495d 0a 20 20 20 20 2f 2a 20 20 20 73 71 6c 69 74 65  .    /*   sqlite
3495e 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53  3_test_control(S
3495f 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 52  QLITE_TESTCTRL_R
34960 45 53 45 52 56 45 2c 20 73 71 6c 69 74 65 33 20  ESERVE, sqlite3 
34961 2a 64 62 2c 20 69 6e 74 20 4e 29 0a 20 20 20 20  *db, int N).    
34962 2a 2a 0a 20 20 20 20 2a 2a 20 53 65 74 20 74 68  **.    ** Set th
34963 65 20 6e 52 65 73 65 72 76 65 20 73 69 7a 65 20  e nReserve size 
34964 74 6f 20 4e 20 66 6f 72 20 74 68 65 20 6d 61 69  to N for the mai
34965 6e 20 64 61 74 61 62 61 73 65 20 6f 6e 20 74 68  n database on th
34966 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a  e database.    *
34967 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e  * connection db.
34968 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
34969 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
3496a 5f 52 45 53 45 52 56 45 3a 20 7b 0a 20 20 20 20  _RESERVE: {.    
3496b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
3496c 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74  va_arg(ap, sqlit
3496d 65 33 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  e3*);.      int 
3496e 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e  x = va_arg(ap,in
3496f 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
34970 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
34971 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
34972 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50  sqlite3BtreeSetP
34973 61 67 65 53 69 7a 65 28 64 62 2d 3e 61 44 62 5b  ageSize(db->aDb[
34974 30 5d 2e 70 42 74 2c 20 30 2c 20 78 2c 20 30 29  0].pBt, 0, x, 0)
34975 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
34976 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
34977 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 62 72  mutex);.      br
34978 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 0a  eak;.    }..  }.
34979 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 23 65    va_end(ap);.#e
3497a 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
3497b 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
3497c 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   */.  return rc;
3497d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
3497e 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 69 6e 2e  *** End of main.
3497f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
34980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34981 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34982 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
34983 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6e  *** Begin file n
34984 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  otify.c ********
34985 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34986 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34987 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4d  **/./*.** 2009 M
34988 61 72 63 68 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65  arch 3.**.** The
34989 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
3498a 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
3498b 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
3498c 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
3498d 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
3498e 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
3498f 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
34990 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
34991 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
34992 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
34993 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
34994 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
34995 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
34996 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
34997 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
34998 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
34999 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
3499a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3499b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3499c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3499d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3499e 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
3499f 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
349a0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
349a1 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  n of the sqlite3
349a2 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29  _unlock_notify()
349a3 0a 2a 2a 20 41 50 49 20 6d 65 74 68 6f 64 20 61  .** API method a
349a4 6e 64 20 69 74 73 20 61 73 73 6f 63 69 61 74 65  nd its associate
349a5 64 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 2e  d functionality.
349a6 0a 2a 2f 0a 0a 2f 2a 20 4f 6d 69 74 20 74 68 69  .*/../* Omit thi
349a7 73 20 65 6e 74 69 72 65 20 66 69 6c 65 20 69 66  s entire file if
349a8 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55   SQLITE_ENABLE_U
349a9 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 20 69 73 20  NLOCK_NOTIFY is 
349aa 6e 6f 74 20 64 65 66 69 6e 65 64 2e 20 2a 2f 0a  not defined. */.
349ab 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
349ac 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49  ABLE_UNLOCK_NOTI
349ad 46 59 0a 0a 2f 2a 0a 2a 2a 20 50 75 62 6c 69 63  FY../*.** Public
349ae 20 69 6e 74 65 72 66 61 63 65 73 3a 0a 2a 2a 0a   interfaces:.**.
349af 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e  **   sqlite3Conn
349b0 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 29 0a  ectionBlocked().
349b1 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e  **   sqlite3Conn
349b2 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29  ectionUnlocked()
349b3 0a 2a 2a 20 20 20 73 71 6c 69 74 65 33 43 6f 6e  .**   sqlite3Con
349b4 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 29 0a  nectionClosed().
349b5 2a 2a 20 20 20 73 71 6c 69 74 65 33 5f 75 6e 6c  **   sqlite3_unl
349b6 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 0a 2a 2f 0a  ock_notify().*/.
349b7 0a 23 64 65 66 69 6e 65 20 61 73 73 65 72 74 4d  .#define assertM
349b8 75 74 65 78 48 65 6c 64 28 29 20 5c 0a 20 20 61  utexHeld() \.  a
349b9 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
349ba 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74 65  utex_held(sqlite
349bb 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
349bc 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
349bd 4d 41 53 54 45 52 29 29 20 29 0a 0a 2f 2a 0a 2a  MASTER)) )../*.*
349be 2a 20 48 65 61 64 20 6f 66 20 61 20 6c 69 6e 6b  * Head of a link
349bf 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73  ed list of all s
349c0 71 6c 69 74 65 33 20 6f 62 6a 65 63 74 73 20 63  qlite3 objects c
349c1 72 65 61 74 65 64 20 62 79 20 74 68 69 73 20 70  reated by this p
349c2 72 6f 63 65 73 73 0a 2a 2a 20 66 6f 72 20 77 68  rocess.** for wh
349c3 69 63 68 20 65 69 74 68 65 72 20 73 71 6c 69 74  ich either sqlit
349c4 65 33 2e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e  e3.pBlockingConn
349c5 65 63 74 69 6f 6e 20 6f 72 20 73 71 6c 69 74 65  ection or sqlite
349c6 33 2e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  3.pUnlockConnect
349c7 69 6f 6e 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55  ion.** is not NU
349c8 4c 4c 2e 20 54 68 69 73 20 76 61 72 69 61 62 6c  LL. This variabl
349c9 65 20 6d 61 79 20 6f 6e 6c 79 20 61 63 63 65 73  e may only acces
349ca 73 65 64 20 77 68 69 6c 65 20 74 68 65 20 53 54  sed while the ST
349cb 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 6d  ATIC_MASTER.** m
349cc 75 74 65 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2f  utex is held..*/
349cd 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 20  .static sqlite3 
349ce 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c 69  *SQLITE_WSD sqli
349cf 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 20 3d  te3BlockedList =
349d0 20 30 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45   0;..#ifndef NDE
349d1 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  BUG./*.** This f
349d2 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 63 6f 6d  unction is a com
349d3 70 6c 65 78 20 61 73 73 65 72 74 28 29 20 74 68  plex assert() th
349d4 61 74 20 76 65 72 69 66 69 65 73 20 74 68 65 20  at verifies the 
349d5 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 70 72  following .** pr
349d6 6f 70 65 72 74 69 65 73 20 6f 66 20 74 68 65 20  operties of the 
349d7 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
349d8 6f 6e 73 20 6c 69 73 74 3a 0a 2a 2a 0a 2a 2a 20  ons list:.**.** 
349d9 20 20 31 29 20 45 61 63 68 20 65 6e 74 72 79 20    1) Each entry 
349da 69 6e 20 74 68 65 20 6c 69 73 74 20 68 61 73 20  in the list has 
349db 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65  a non-NULL value
349dc 20 66 6f 72 20 65 69 74 68 65 72 20 0a 2a 2a 20   for either .** 
349dd 20 20 20 20 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e       pUnlockConn
349de 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f 63 6b  ection or pBlock
349df 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f  ingConnection, o
349e0 72 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 20 20  r both..**.**   
349e1 32 29 20 41 6c 6c 20 65 6e 74 72 69 65 73 20 69  2) All entries i
349e2 6e 20 74 68 65 20 6c 69 73 74 20 74 68 61 74 20  n the list that 
349e3 73 68 61 72 65 20 61 20 63 6f 6d 6d 6f 6e 20 76  share a common v
349e4 61 6c 75 65 20 66 6f 72 20 0a 2a 2a 20 20 20 20  alue for .**    
349e5 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20    xUnlockNotify 
349e6 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 65  are grouped toge
349e7 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29  ther..**.**   3)
349e8 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   If the argument
349e9 20 64 62 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c   db is not NULL,
349ea 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68   then none of th
349eb 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65  e entries in the
349ec 0a 2a 2a 20 20 20 20 20 20 62 6c 6f 63 6b 65 64  .**      blocked
349ed 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73   connections lis
349ee 74 20 68 61 76 65 20 70 55 6e 6c 6f 63 6b 43 6f  t have pUnlockCo
349ef 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f  nnection or pBlo
349f0 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 0a  ckingConnection.
349f1 2a 2a 20 20 20 20 20 20 73 65 74 20 74 6f 20 64  **      set to d
349f2 62 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  b. This is used 
349f3 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20 63 6f 6e  when closing con
349f4 6e 65 63 74 69 6f 6e 20 64 62 2e 0a 2a 2f 0a 73  nection db..*/.s
349f5 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b  tatic void check
349f6 4c 69 73 74 50 72 6f 70 65 72 74 69 65 73 28 73  ListProperties(s
349f7 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73  qlite3 *db){.  s
349f8 71 6c 69 74 65 33 20 2a 70 3b 0a 20 20 66 6f 72  qlite3 *p;.  for
349f9 28 70 3d 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65  (p=sqlite3Blocke
349fa 64 4c 69 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70  dList; p; p=p->p
349fb 4e 65 78 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20  NextBlocked){.  
349fc 20 20 69 6e 74 20 73 65 65 6e 20 3d 20 30 3b 0a    int seen = 0;.
349fd 20 20 20 20 73 71 6c 69 74 65 33 20 2a 70 32 3b      sqlite3 *p2;
349fe 0a 0a 20 20 20 20 2f 2a 20 56 65 72 69 66 79 20  ..    /* Verify 
349ff 70 72 6f 70 65 72 74 79 20 28 31 29 20 2a 2f 0a  property (1) */.
34a00 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
34a01 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
34a02 20 7c 7c 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67   || p->pBlocking
34a03 43 6f 6e 6e 65 63 74 69 6f 6e 20 29 3b 0a 0a 20  Connection );.. 
34a04 20 20 20 2f 2a 20 56 65 72 69 66 79 20 70 72 6f     /* Verify pro
34a05 70 65 72 74 79 20 28 32 29 20 2a 2f 0a 20 20 20  perty (2) */.   
34a06 20 66 6f 72 28 70 32 3d 73 71 6c 69 74 65 33 42   for(p2=sqlite3B
34a07 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 70 32 21 3d  lockedList; p2!=
34a08 70 3b 20 70 32 3d 70 32 2d 3e 70 4e 65 78 74 42  p; p2=p2->pNextB
34a09 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20 20 20 69  locked){.      i
34a0a 66 28 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f  f( p2->xUnlockNo
34a0b 74 69 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b  tify==p->xUnlock
34a0c 4e 6f 74 69 66 79 20 29 20 73 65 65 6e 20 3d 20  Notify ) seen = 
34a0d 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  1;.      assert(
34a0e 20 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69   p2->xUnlockNoti
34a0f 66 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f  fy==p->xUnlockNo
34a10 74 69 66 79 20 7c 7c 20 21 73 65 65 6e 20 29 3b  tify || !seen );
34a11 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
34a12 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 55 6e 6c 6f  b==0 || p->pUnlo
34a13 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 21 3d 64 62  ckConnection!=db
34a14 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
34a15 28 20 64 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 42  ( db==0 || p->pB
34a16 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
34a17 6e 21 3d 64 62 20 29 3b 0a 20 20 20 20 7d 0a 20  n!=db );.    }. 
34a18 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66   }.}.#else.# def
34a19 69 6e 65 20 63 68 65 63 6b 4c 69 73 74 50 72 6f  ine checkListPro
34a1a 70 65 72 74 69 65 73 28 78 29 0a 23 65 6e 64 69  perties(x).#endi
34a1b 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  f../*.** Remove 
34a1c 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 66 72  connection db fr
34a1d 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63  om the blocked c
34a1e 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e  onnections list.
34a1f 20 49 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a   If connection.*
34a20 2a 20 64 62 20 69 73 20 6e 6f 74 20 63 75 72 72  * db is not curr
34a21 65 6e 74 6c 79 20 61 20 70 61 72 74 20 6f 66 20  ently a part of 
34a22 74 68 65 20 6c 69 73 74 2c 20 74 68 69 73 20 66  the list, this f
34a23 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d  unction is a no-
34a24 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  op..*/.static vo
34a25 69 64 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f  id removeFromBlo
34a26 63 6b 65 64 4c 69 73 74 28 73 71 6c 69 74 65 33  ckedList(sqlite3
34a27 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33   *db){.  sqlite3
34a28 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72 74 4d   **pp;.  assertM
34a29 75 74 65 78 48 65 6c 64 28 29 3b 0a 20 20 66 6f  utexHeld();.  fo
34a2a 72 28 70 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f  r(pp=&sqlite3Blo
34a2b 63 6b 65 64 4c 69 73 74 3b 20 2a 70 70 3b 20 70  ckedList; *pp; p
34a2c 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78  p = &(*pp)->pNex
34a2d 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20 69  tBlocked){.    i
34a2e 66 28 20 2a 70 70 3d 3d 64 62 20 29 7b 0a 20 20  f( *pp==db ){.  
34a2f 20 20 20 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d      *pp = (*pp)-
34a30 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 3b 0a 20  >pNextBlocked;. 
34a31 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
34a32 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  }.  }.}../*.** A
34a33 64 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62  dd connection db
34a34 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 65 64 20   to the blocked 
34a35 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74  connections list
34a36 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 0a  . It is assumed.
34a37 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 6e 6f  ** that it is no
34a38 74 20 61 6c 72 65 61 64 79 20 61 20 70 61 72 74  t already a part
34a39 20 6f 66 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f   of the list..*/
34a3a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 64 64  .static void add
34a3b 54 6f 42 6c 6f 63 6b 65 64 4c 69 73 74 28 73 71  ToBlockedList(sq
34a3c 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71  lite3 *db){.  sq
34a3d 6c 69 74 65 33 20 2a 2a 70 70 3b 0a 20 20 61 73  lite3 **pp;.  as
34a3e 73 65 72 74 4d 75 74 65 78 48 65 6c 64 28 29 3b  sertMutexHeld();
34a3f 0a 20 20 66 6f 72 28 0a 20 20 20 20 70 70 3d 26  .  for(.    pp=&
34a40 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69  sqlite3BlockedLi
34a41 73 74 3b 20 0a 20 20 20 20 2a 70 70 20 26 26 20  st; .    *pp && 
34a42 28 2a 70 70 29 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f  (*pp)->xUnlockNo
34a43 74 69 66 79 21 3d 64 62 2d 3e 78 55 6e 6c 6f 63  tify!=db->xUnloc
34a44 6b 4e 6f 74 69 66 79 3b 20 0a 20 20 20 20 70 70  kNotify; .    pp
34a45 3d 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 42 6c  =&(*pp)->pNextBl
34a46 6f 63 6b 65 64 0a 20 20 29 3b 0a 20 20 64 62 2d  ocked.  );.  db-
34a47 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20  >pNextBlocked = 
34a48 2a 70 70 3b 0a 20 20 2a 70 70 20 3d 20 64 62 3b  *pp;.  *pp = db;
34a49 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e  .}../*.** Obtain
34a4a 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54   the STATIC_MAST
34a4b 45 52 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61  ER mutex..*/.sta
34a4c 74 69 63 20 76 6f 69 64 20 65 6e 74 65 72 4d 75  tic void enterMu
34a4d 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c  tex(void){.  sql
34a4e 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
34a4f 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
34a50 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
34a51 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b  STATIC_MASTER));
34a52 0a 20 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70  .  checkListProp
34a53 65 72 74 69 65 73 28 30 29 3b 0a 7d 0a 0a 2f 2a  erties(0);.}../*
34a54 0a 2a 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20  .** Release the 
34a55 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75  STATIC_MASTER mu
34a56 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tex..*/.static v
34a57 6f 69 64 20 6c 65 61 76 65 4d 75 74 65 78 28 76  oid leaveMutex(v
34a58 6f 69 64 29 7b 0a 20 20 61 73 73 65 72 74 4d 75  oid){.  assertMu
34a59 74 65 78 48 65 6c 64 28 29 3b 0a 20 20 63 68 65  texHeld();.  che
34a5a 63 6b 4c 69 73 74 50 72 6f 70 65 72 74 69 65 73  ckListProperties
34a5b 28 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  (0);.  sqlite3_m
34a5c 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74  utex_leave(sqlit
34a5d 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
34a5e 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
34a5f 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a 0a 2f 2a  _MASTER));.}../*
34a60 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 6e 20  .** Register an 
34a61 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
34a62 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  llback..**.** Th
34a63 69 73 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74  is is called aft
34a64 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 22 64  er connection "d
34a65 62 22 20 68 61 73 20 61 74 74 65 6d 70 74 65 64  b" has attempted
34a66 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f 6e 0a   some operation.
34a67 2a 2a 20 62 75 74 20 68 61 73 20 72 65 63 65 69  ** but has recei
34a68 76 65 64 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f  ved an SQLITE_LO
34a69 43 4b 45 44 20 65 72 72 6f 72 20 62 65 63 61 75  CKED error becau
34a6a 73 65 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65  se another conne
34a6b 63 74 69 6f 6e 0a 2a 2a 20 28 63 61 6c 6c 20 69  ction.** (call i
34a6c 74 20 70 4f 74 68 65 72 29 20 69 6e 20 74 68 65  t pOther) in the
34a6d 20 73 61 6d 65 20 70 72 6f 63 65 73 73 20 77 61   same process wa
34a6e 73 20 62 75 73 79 20 75 73 69 6e 67 20 74 68 65  s busy using the
34a6f 20 73 61 6d 65 20 73 68 61 72 65 64 0a 2a 2a 20   same shared.** 
34a70 63 61 63 68 65 2e 20 20 70 4f 74 68 65 72 20 69  cache.  pOther i
34a71 73 20 66 6f 75 6e 64 20 62 79 20 6c 6f 6f 6b 69  s found by looki
34a72 6e 67 20 61 74 20 64 62 2d 3e 70 42 6c 6f 63 6b  ng at db->pBlock
34a73 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  ingConnection..*
34a74 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  *.** If there is
34a75 20 6e 6f 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e   no blocking con
34a76 6e 65 63 74 69 6f 6e 2c 20 74 68 65 20 63 61 6c  nection, the cal
34a77 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
34a78 20 69 6d 6d 65 64 69 61 74 65 6c 79 2c 0a 2a 2a   immediately,.**
34a79 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75   before this rou
34a7a 74 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a  tine returns..**
34a7b 0a 2a 2a 20 49 66 20 70 4f 74 68 65 72 20 69 73  .** If pOther is
34a7c 20 61 6c 72 65 61 64 79 20 62 6c 6f 63 6b 65 64   already blocked
34a7d 20 6f 6e 20 64 62 2c 20 74 68 65 6e 20 72 65 70   on db, then rep
34a7e 6f 72 74 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  ort SQLITE_LOCKE
34a7f 44 2c 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a  D, to indicate.*
34a80 2a 20 61 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a  * a deadlock..**
34a81 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 6d  .** Otherwise, m
34a82 61 6b 65 20 61 72 72 61 6e 67 65 6d 65 6e 74 73  ake arrangements
34a83 20 74 6f 20 69 6e 76 6f 6b 65 20 78 4e 6f 74 69   to invoke xNoti
34a84 66 79 20 77 68 65 6e 20 70 4f 74 68 65 72 20 64  fy when pOther d
34a85 72 6f 70 73 0a 2a 2a 20 69 74 73 20 6c 6f 63 6b  rops.** its lock
34a86 73 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61  s..**.** Each ca
34a87 6c 6c 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69  ll to this routi
34a88 6e 65 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79  ne overrides any
34a89 20 70 72 69 6f 72 20 63 61 6c 6c 62 61 63 6b 73   prior callbacks
34a8a 20 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 6f   registered.** o
34a8b 6e 20 74 68 65 20 73 61 6d 65 20 22 64 62 22 2e  n the same "db".
34a8c 20 20 49 66 20 78 4e 6f 74 69 66 79 3d 3d 30 20    If xNotify==0 
34a8d 74 68 65 6e 20 61 6e 79 20 70 72 69 6f 72 20 63  then any prior c
34a8e 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 69 6d 6d  allbacks are imm
34a8f 65 64 69 61 74 65 6c 79 0a 2a 2a 20 63 61 6e 63  ediately.** canc
34a90 65 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  elled..*/.SQLITE
34a91 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
34a92 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 0a  _unlock_notify(.
34a93 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
34a94 20 76 6f 69 64 20 28 2a 78 4e 6f 74 69 66 79 29   void (*xNotify)
34a95 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74 29 2c 0a  (void **, int),.
34a96 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a    void *pArg.){.
34a97 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
34a98 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33  E_OK;..  sqlite3
34a99 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
34a9a 3e 6d 75 74 65 78 29 3b 0a 20 20 65 6e 74 65 72  >mutex);.  enter
34a9b 4d 75 74 65 78 28 29 3b 0a 0a 20 20 69 66 28 20  Mutex();..  if( 
34a9c 78 4e 6f 74 69 66 79 3d 3d 30 20 29 7b 0a 20 20  xNotify==0 ){.  
34a9d 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63    removeFromBloc
34a9e 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 20  kedList(db);.   
34a9f 20 64 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e   db->pUnlockConn
34aa0 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20  ection = 0;.    
34aa1 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  db->xUnlockNotif
34aa2 79 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70  y = 0;.    db->p
34aa3 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20  UnlockArg = 0;. 
34aa4 20 7d 65 6c 73 65 20 69 66 28 20 30 3d 3d 64 62   }else if( 0==db
34aa5 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65  ->pBlockingConne
34aa6 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 2f 2a 20  ction ){.    /* 
34aa7 54 68 65 20 62 6c 6f 63 6b 69 6e 67 20 74 72 61  The blocking tra
34aa8 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 62 65 65  nsaction has bee
34aa9 6e 20 63 6f 6e 63 6c 75 64 65 64 2e 20 4f 72 20  n concluded. Or 
34aaa 74 68 65 72 65 20 6e 65 76 65 72 20 77 61 73 20  there never was 
34aab 61 20 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 69  a .    ** blocki
34aac 6e 67 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  ng transaction. 
34aad 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20  In either case, 
34aae 69 6e 76 6f 6b 65 20 74 68 65 20 6e 6f 74 69 66  invoke the notif
34aaf 79 20 63 61 6c 6c 62 61 63 6b 0a 20 20 20 20 2a  y callback.    *
34ab0 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a  * immediately. .
34ab1 20 20 20 20 2a 2f 0a 20 20 20 20 78 4e 6f 74 69      */.    xNoti
34ab2 66 79 28 26 70 41 72 67 2c 20 31 29 3b 0a 20 20  fy(&pArg, 1);.  
34ab3 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
34ab4 65 33 20 2a 70 3b 0a 0a 20 20 20 20 66 6f 72 28  e3 *p;..    for(
34ab5 70 3d 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43  p=db->pBlockingC
34ab6 6f 6e 6e 65 63 74 69 6f 6e 3b 20 70 20 26 26 20  onnection; p && 
34ab7 70 21 3d 64 62 3b 20 70 3d 70 2d 3e 70 55 6e 6c  p!=db; p=p->pUnl
34ab8 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 29 7b 7d  ockConnection){}
34ab9 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20  .    if( p ){.  
34aba 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
34abb 4c 4f 43 4b 45 44 3b 20 20 20 20 20 20 20 20 20  LOCKED;         
34abc 20 20 20 20 20 2f 2a 20 44 65 61 64 6c 6f 63 6b       /* Deadlock
34abd 20 64 65 74 65 63 74 65 64 2e 20 2a 2f 0a 20 20   detected. */.  
34abe 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
34abf 62 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  b->pUnlockConnec
34ac0 74 69 6f 6e 20 3d 20 64 62 2d 3e 70 42 6c 6f 63  tion = db->pBloc
34ac1 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 0a  kingConnection;.
34ac2 20 20 20 20 20 20 64 62 2d 3e 78 55 6e 6c 6f 63        db->xUnloc
34ac3 6b 4e 6f 74 69 66 79 20 3d 20 78 4e 6f 74 69 66  kNotify = xNotif
34ac4 79 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 55 6e  y;.      db->pUn
34ac5 6c 6f 63 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a  lockArg = pArg;.
34ac6 20 20 20 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d        removeFrom
34ac7 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b  BlockedList(db);
34ac8 0a 20 20 20 20 20 20 61 64 64 54 6f 42 6c 6f 63  .      addToBloc
34ac9 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20 20  kedList(db);.   
34aca 20 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61 76 65 4d   }.  }..  leaveM
34acb 75 74 65 78 28 29 3b 0a 20 20 61 73 73 65 72 74  utex();.  assert
34acc 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
34acd 6c 65 64 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  led );.  sqlite3
34ace 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 28 72  Error(db, rc, (r
34acf 63 3f 22 64 61 74 61 62 61 73 65 20 69 73 20 64  c?"database is d
34ad0 65 61 64 6c 6f 63 6b 65 64 22 3a 30 29 29 3b 0a  eadlocked":0));.
34ad1 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
34ad2 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
34ad3 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
34ad4 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
34ad5 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
34ad6 77 68 69 6c 65 20 73 74 65 70 70 69 6e 67 20 6f  while stepping o
34ad7 72 20 70 72 65 70 61 72 69 6e 67 20 61 20 73 74  r preparing a st
34ad8 61 74 65 6d 65 6e 74 20 0a 2a 2a 20 61 73 73 6f  atement .** asso
34ad9 63 69 61 74 65 64 20 77 69 74 68 20 63 6f 6e 6e  ciated with conn
34ada 65 63 74 69 6f 6e 20 64 62 2e 20 54 68 65 20 6f  ection db. The o
34adb 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65  peration will re
34adc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  turn SQLITE_LOCK
34add 45 44 0a 2a 2a 20 74 6f 20 74 68 65 20 75 73 65  ED.** to the use
34ade 72 20 62 65 63 61 75 73 65 20 69 74 20 72 65 71  r because it req
34adf 75 69 72 65 73 20 61 20 6c 6f 63 6b 20 74 68 61  uires a lock tha
34ae0 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 76  t will not be av
34ae1 61 69 6c 61 62 6c 65 0a 2a 2a 20 75 6e 74 69 6c  ailable.** until
34ae2 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 42 6c 6f   connection pBlo
34ae3 63 6b 65 72 20 63 6f 6e 63 6c 75 64 65 73 20 69  cker concludes i
34ae4 74 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  ts current trans
34ae5 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  action..*/.SQLIT
34ae6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
34ae7 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
34ae8 42 6c 6f 63 6b 65 64 28 73 71 6c 69 74 65 33 20  Blocked(sqlite3 
34ae9 2a 64 62 2c 20 73 71 6c 69 74 65 33 20 2a 70 42  *db, sqlite3 *pB
34aea 6c 6f 63 6b 65 72 29 7b 0a 20 20 65 6e 74 65 72  locker){.  enter
34aeb 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 64  Mutex();.  if( d
34aec 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e  b->pBlockingConn
34aed 65 63 74 69 6f 6e 3d 3d 30 20 26 26 20 64 62 2d  ection==0 && db-
34aee 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69  >pUnlockConnecti
34aef 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 61 64 64  on==0 ){.    add
34af0 54 6f 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62  ToBlockedList(db
34af1 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 42 6c  );.  }.  db->pBl
34af2 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e  ockingConnection
34af3 20 3d 20 70 42 6c 6f 63 6b 65 72 3b 0a 20 20 6c   = pBlocker;.  l
34af4 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a  eaveMutex();.}..
34af5 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
34af6 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ion is called wh
34af7 65 6e 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61  en.** the transa
34af8 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20 62 79 20  ction opened by 
34af9 64 61 74 61 62 61 73 65 20 64 62 20 68 61 73 20  database db has 
34afa 6a 75 73 74 20 66 69 6e 69 73 68 65 64 2e 20 4c  just finished. L
34afb 6f 63 6b 73 20 68 65 6c 64 20 0a 2a 2a 20 62 79  ocks held .** by
34afc 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
34afd 74 69 6f 6e 20 64 62 20 68 61 76 65 20 62 65 65  tion db have bee
34afe 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a  n released..**.*
34aff 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
34b00 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 65 61  loops through ea
34b01 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  ch entry in the 
34b02 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69  blocked connecti
34b03 6f 6e 73 0a 2a 2a 20 6c 69 73 74 20 61 6e 64 20  ons.** list and 
34b04 64 6f 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69  does the followi
34b05 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49  ng:.**.**   1) I
34b06 66 20 74 68 65 20 73 71 6c 69 74 65 33 2e 70 42  f the sqlite3.pB
34b07 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
34b08 6e 20 6d 65 6d 62 65 72 20 6f 66 20 61 20 6c 69  n member of a li
34b09 73 74 20 65 6e 74 72 79 20 69 73 0a 2a 2a 20 20  st entry is.**  
34b0a 20 20 20 20 73 65 74 20 74 6f 20 64 62 2c 20 74      set to db, t
34b0b 68 65 6e 20 73 65 74 20 70 42 6c 6f 63 6b 69 6e  hen set pBlockin
34b0c 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a  gConnection=0..*
34b0d 2a 0a 2a 2a 20 20 20 32 29 20 49 66 20 74 68 65  *.**   2) If the
34b0e 20 73 71 6c 69 74 65 33 2e 70 55 6e 6c 6f 63 6b   sqlite3.pUnlock
34b0f 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d 65 6d 62 65  Connection membe
34b10 72 20 6f 66 20 61 20 6c 69 73 74 20 65 6e 74 72  r of a list entr
34b11 79 20 69 73 0a 2a 2a 20 20 20 20 20 20 73 65 74  y is.**      set
34b12 20 74 6f 20 64 62 2c 20 74 68 65 6e 20 69 6e 76   to db, then inv
34b13 6f 6b 65 20 74 68 65 20 63 6f 6e 66 69 67 75 72  oke the configur
34b14 65 64 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79  ed unlock-notify
34b15 20 63 61 6c 6c 62 61 63 6b 20 61 6e 64 0a 2a 2a   callback and.**
34b16 20 20 20 20 20 20 73 65 74 20 70 55 6e 6c 6f 63        set pUnloc
34b17 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a  kConnection=0..*
34b18 2a 0a 2a 2a 20 20 20 33 29 20 49 66 20 74 68 65  *.**   3) If the
34b19 20 74 77 6f 20 73 74 65 70 73 20 61 62 6f 76 65   two steps above
34b1a 20 6d 65 61 6e 20 74 68 61 74 20 70 42 6c 6f 63   mean that pBloc
34b1b 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  kingConnection==
34b1c 30 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 55  0 and.**      pU
34b1d 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d  nlockConnection=
34b1e 3d 30 2c 20 72 65 6d 6f 76 65 20 74 68 65 20 65  =0, remove the e
34b1f 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 62 6c  ntry from the bl
34b20 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
34b21 73 0a 2a 2a 20 20 20 20 20 20 6c 69 73 74 2e 0a  s.**      list..
34b22 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
34b23 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
34b24 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64  nnectionUnlocked
34b25 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
34b26 20 76 6f 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e   void (*xUnlockN
34b27 6f 74 69 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20  otify)(void **, 
34b28 69 6e 74 29 20 3d 20 30 3b 20 2f 2a 20 55 6e 6c  int) = 0; /* Unl
34b29 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 62 20 74 6f  ock-notify cb to
34b2a 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 69 6e 74   invoke */.  int
34b2b 20 6e 41 72 67 20 3d 20 30 3b 20 20 20 20 20 20   nArg = 0;      
34b2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34b2d 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
34b2e 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 41  of entries in aA
34b2f 72 67 5b 5d 20 2a 2f 0a 20 20 73 71 6c 69 74 65  rg[] */.  sqlite
34b30 33 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20  3 **pp;         
34b31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34b32 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
34b33 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69  ariable */.  voi
34b34 64 20 2a 2a 61 41 72 67 3b 20 20 20 20 20 20 20  d **aArg;       
34b35 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d          /* Argum
34b36 65 6e 74 73 20 74 6f 20 74 68 65 20 75 6e 6c 6f  ents to the unlo
34b37 63 6b 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  ck callback */. 
34b38 20 76 6f 69 64 20 2a 2a 61 44 79 6e 20 3d 20 30   void **aDyn = 0
34b39 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  ;           /* D
34b3a 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63  ynamically alloc
34b3b 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 61  ated space for a
34b3c 41 72 67 5b 5d 20 2a 2f 0a 20 20 76 6f 69 64 20  Arg[] */.  void 
34b3d 2a 61 53 74 61 74 69 63 5b 31 36 5d 3b 20 20 20  *aStatic[16];   
34b3e 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74 65 72        /* Starter
34b3f 20 73 70 61 63 65 20 66 6f 72 20 61 41 72 67 5b   space for aArg[
34b40 5d 2e 20 20 4e 6f 20 6d 61 6c 6c 6f 63 20 72 65  ].  No malloc re
34b41 71 75 69 72 65 64 20 2a 2f 0a 0a 20 20 61 41 72  quired */..  aAr
34b42 67 20 3d 20 61 53 74 61 74 69 63 3b 0a 20 20 65  g = aStatic;.  e
34b43 6e 74 65 72 4d 75 74 65 78 28 29 3b 20 20 20 20  nterMutex();    
34b44 20 20 20 20 20 2f 2a 20 45 6e 74 65 72 20 53 54       /* Enter ST
34b45 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65  ATIC_MASTER mute
34b46 78 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20  x */..  /* This 
34b47 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20 66  loop runs once f
34b48 6f 72 20 65 61 63 68 20 65 6e 74 72 79 20 69 6e  or each entry in
34b49 20 74 68 65 20 62 6c 6f 63 6b 65 64 2d 63 6f 6e   the blocked-con
34b4a 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20 2a  nections list. *
34b4b 2f 0a 20 20 66 6f 72 28 70 70 3d 26 73 71 6c 69  /.  for(pp=&sqli
34b4c 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20  te3BlockedList; 
34b4d 2a 70 70 3b 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f  *pp; /* no-op */
34b4e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20   ){.    sqlite3 
34b4f 2a 70 20 3d 20 2a 70 70 3b 0a 0a 20 20 20 20 2f  *p = *pp;..    /
34b50 2a 20 53 74 65 70 20 31 2e 20 2a 2f 0a 20 20 20  * Step 1. */.   
34b51 20 69 66 28 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e   if( p->pBlockin
34b52 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20  gConnection==db 
34b53 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 42 6c 6f  ){.      p->pBlo
34b54 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20  ckingConnection 
34b55 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  = 0;.    }..    
34b56 2f 2a 20 53 74 65 70 20 32 2e 20 2a 2f 0a 20 20  /* Step 2. */.  
34b57 20 20 69 66 28 20 70 2d 3e 70 55 6e 6c 6f 63 6b    if( p->pUnlock
34b58 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29  Connection==db )
34b59 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
34b5a 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  p->xUnlockNotify
34b5b 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d   );.      if( p-
34b5c 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 21 3d  >xUnlockNotify!=
34b5d 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 26 26  xUnlockNotify &&
34b5e 20 6e 41 72 67 21 3d 30 20 29 7b 0a 20 20 20 20   nArg!=0 ){.    
34b5f 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66      xUnlockNotif
34b60 79 28 61 41 72 67 2c 20 6e 41 72 67 29 3b 0a 20  y(aArg, nArg);. 
34b61 20 20 20 20 20 20 20 6e 41 72 67 20 3d 20 30 3b         nArg = 0;
34b62 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
34b63 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
34b64 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
34b65 20 20 61 73 73 65 72 74 28 20 61 41 72 67 3d 3d    assert( aArg==
34b66 61 44 79 6e 20 7c 7c 20 28 61 44 79 6e 3d 3d 30  aDyn || (aDyn==0
34b67 20 26 26 20 61 41 72 67 3d 3d 61 53 74 61 74 69   && aArg==aStati
34b68 63 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  c) );.      asse
34b69 72 74 28 20 6e 41 72 67 3c 3d 28 69 6e 74 29 41  rt( nArg<=(int)A
34b6a 72 72 61 79 53 69 7a 65 28 61 53 74 61 74 69 63  rraySize(aStatic
34b6b 29 20 7c 7c 20 61 41 72 67 3d 3d 61 44 79 6e 20  ) || aArg==aDyn 
34b6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 21 61  );.      if( (!a
34b6d 44 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28 69 6e  Dyn && nArg==(in
34b6e 74 29 41 72 72 61 79 53 69 7a 65 28 61 53 74 61  t)ArraySize(aSta
34b6f 74 69 63 29 29 0a 20 20 20 20 20 20 20 7c 7c 20  tic)).       || 
34b70 28 61 44 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28  (aDyn && nArg==(
34b71 69 6e 74 29 28 73 71 6c 69 74 65 33 44 62 4d 61  int)(sqlite3DbMa
34b72 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 61 44 79  llocSize(db, aDy
34b73 6e 29 2f 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29  n)/sizeof(void*)
34b74 29 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  )).      ){.    
34b75 20 20 20 20 2f 2a 20 54 68 65 20 61 41 72 67 5b      /* The aArg[
34b76 5d 20 61 72 72 61 79 20 6e 65 65 64 73 20 74 6f  ] array needs to
34b77 20 67 72 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20   grow. */.      
34b78 20 20 76 6f 69 64 20 2a 2a 70 4e 65 77 20 3d 20    void **pNew = 
34b79 28 76 6f 69 64 20 2a 2a 29 73 71 6c 69 74 65 33  (void **)sqlite3
34b7a 4d 61 6c 6c 6f 63 28 6e 41 72 67 2a 73 69 7a 65  Malloc(nArg*size
34b7b 6f 66 28 76 6f 69 64 20 2a 29 2a 32 29 3b 0a 20  of(void *)*2);. 
34b7c 20 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20         if( pNew 
34b7d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  ){.          mem
34b7e 63 70 79 28 70 4e 65 77 2c 20 61 41 72 67 2c 20  cpy(pNew, aArg, 
34b7f 6e 41 72 67 2a 73 69 7a 65 6f 66 28 76 6f 69 64  nArg*sizeof(void
34b80 20 2a 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20   *));.          
34b81 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 44 79  sqlite3_free(aDy
34b82 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 44  n);.          aD
34b83 79 6e 20 3d 20 61 41 72 67 20 3d 20 70 4e 65 77  yn = aArg = pNew
34b84 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
34b85 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  .          /* Th
34b86 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74  is occurs when t
34b87 68 65 20 61 72 72 61 79 20 6f 66 20 63 6f 6e 74  he array of cont
34b88 65 78 74 20 70 6f 69 6e 74 65 72 73 20 74 68 61  ext pointers tha
34b89 74 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20  t need to.      
34b8a 20 20 20 20 2a 2a 20 62 65 20 70 61 73 73 65 64      ** be passed
34b8b 20 74 6f 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e   to the unlock-n
34b8c 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 69  otify callback i
34b8d 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68  s larger than th
34b8e 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61  e.          ** a
34b8f 53 74 61 74 69 63 5b 5d 20 61 72 72 61 79 20 61  Static[] array a
34b90 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20  llocated on the 
34b91 73 74 61 63 6b 20 61 6e 64 20 74 68 65 20 61 74  stack and the at
34b92 74 65 6d 70 74 20 74 6f 20 0a 20 20 20 20 20 20  tempt to .      
34b93 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20      ** allocate 
34b94 61 20 6c 61 72 67 65 72 20 61 72 72 61 79 20 66  a larger array f
34b95 72 6f 6d 20 74 68 65 20 68 65 61 70 20 68 61 73  rom the heap has
34b96 20 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20 20   failed..       
34b97 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20     **.          
34b98 2a 2a 20 54 68 69 73 20 69 73 20 61 20 64 69 66  ** This is a dif
34b99 66 69 63 75 6c 74 20 73 69 74 75 61 74 69 6f 6e  ficult situation
34b9a 20 74 6f 20 68 61 6e 64 6c 65 2e 20 52 65 74 75   to handle. Retu
34b9b 72 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 0a 20  rning an error. 
34b9c 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 64 65           ** code
34b9d 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69   to the caller i
34b9e 73 20 69 6e 73 75 66 66 69 63 69 65 6e 74 2c 20  s insufficient, 
34b9f 61 73 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72  as even if an er
34ba0 72 6f 72 20 63 6f 64 65 0a 20 20 20 20 20 20 20  ror code.       
34ba1 20 20 20 2a 2a 20 69 73 20 72 65 74 75 72 6e 65     ** is returne
34ba2 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  d the transactio
34ba3 6e 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  n on connection 
34ba4 64 62 20 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65  db will still be
34ba5 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6c  .          ** cl
34ba6 6f 73 65 64 20 61 6e 64 20 74 68 65 20 75 6e 6c  osed and the unl
34ba7 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
34ba8 61 63 6b 73 20 6f 6e 20 62 6c 6f 63 6b 65 64 20  acks on blocked 
34ba9 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 20 20 20 20  connections.    
34baa 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 67 6f        ** will go
34bab 20 75 6e 69 73 73 75 65 64 2e 20 54 68 69 73 20   unissued. This 
34bac 6d 69 67 68 74 20 63 61 75 73 65 20 74 68 65 20  might cause the 
34bad 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 77  application to w
34bae 61 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ait.          **
34baf 20 69 6e 64 65 66 69 6e 69 74 65 6c 79 20 66 6f   indefinitely fo
34bb0 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69  r an unlock-noti
34bb1 66 79 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74  fy callback that
34bb2 20 77 69 6c 6c 20 6e 65 76 65 72 20 0a 20 20 20   will never .   
34bb3 20 20 20 20 20 20 20 2a 2a 20 61 72 72 69 76 65         ** arrive
34bb4 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 0a 20  ..          **. 
34bb5 20 20 20 20 20 20 20 20 20 2a 2a 20 49 6e 73 74           ** Inst
34bb6 65 61 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20  ead, invoke the 
34bb7 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
34bb8 6c 6c 62 61 63 6b 20 77 69 74 68 20 74 68 65 20  llback with the 
34bb9 63 6f 6e 74 65 78 74 0a 20 20 20 20 20 20 20 20  context.        
34bba 20 20 2a 2a 20 61 72 72 61 79 20 61 6c 72 65 61    ** array alrea
34bbb 64 79 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20  dy accumulated. 
34bbc 57 65 20 63 61 6e 20 74 68 65 6e 20 63 6c 65 61  We can then clea
34bbd 72 20 74 68 65 20 61 72 72 61 79 20 61 6e 64 0a  r the array and.
34bbe 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65 67            ** beg
34bbf 69 6e 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 20  in accumulating 
34bc0 61 6e 79 20 66 75 72 74 68 65 72 20 63 6f 6e 74  any further cont
34bc1 65 78 74 20 70 6f 69 6e 74 65 72 73 20 77 69 74  ext pointers wit
34bc2 68 6f 75 74 20 0a 20 20 20 20 20 20 20 20 20 20  hout .          
34bc3 2a 2a 20 72 65 71 75 69 72 69 6e 67 20 61 6e 79  ** requiring any
34bc4 20 64 79 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74   dynamic allocat
34bc5 69 6f 6e 2e 20 54 68 69 73 20 69 73 20 73 75 62  ion. This is sub
34bc6 2d 6f 70 74 69 6d 61 6c 20 62 65 63 61 75 73 65  -optimal because
34bc7 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 74  .          ** it
34bc8 20 6d 65 61 6e 73 20 74 68 61 74 20 69 6e 73 74   means that inst
34bc9 65 61 64 20 6f 66 20 6f 6e 65 20 63 61 6c 6c 62  ead of one callb
34bca 61 63 6b 20 77 69 74 68 20 61 20 6c 61 72 67 65  ack with a large
34bcb 20 61 72 72 61 79 20 6f 66 0a 20 20 20 20 20 20   array of.      
34bcc 20 20 20 20 2a 2a 20 63 6f 6e 74 65 78 74 20 70      ** context p
34bcd 6f 69 6e 74 65 72 73 20 74 68 65 20 61 70 70 6c  ointers the appl
34bce 69 63 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 63  ication will rec
34bcf 65 69 76 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65  eive two or more
34bd0 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 61  .          ** ca
34bd1 6c 6c 62 61 63 6b 73 20 77 69 74 68 20 73 6d 61  llbacks with sma
34bd2 6c 6c 65 72 20 61 72 72 61 79 73 20 6f 66 20 63  ller arrays of c
34bd3 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 73 2c  ontext pointers,
34bd4 20 77 68 69 63 68 20 77 69 6c 6c 0a 20 20 20 20   which will.    
34bd5 20 20 20 20 20 20 2a 2a 20 72 65 64 75 63 65 20        ** reduce 
34bd6 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  the applications
34bd7 20 61 62 69 6c 69 74 79 20 74 6f 20 70 72 69 6f   ability to prio
34bd8 72 69 74 69 7a 65 20 6d 75 6c 74 69 70 6c 65 20  ritize multiple 
34bd9 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  .          ** co
34bda 6e 6e 65 63 74 69 6f 6e 73 2e 20 42 75 74 20 69  nnections. But i
34bdb 74 20 69 73 20 74 68 65 20 62 65 73 74 20 74 68  t is the best th
34bdc 61 74 20 63 61 6e 20 62 65 20 64 6f 6e 65 20 75  at can be done u
34bdd 6e 64 65 72 20 74 68 65 0a 20 20 20 20 20 20 20  nder the.       
34bde 20 20 20 2a 2a 20 63 69 72 63 75 6d 73 74 61 6e     ** circumstan
34bdf 63 65 73 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ces..          *
34be0 2f 0a 20 20 20 20 20 20 20 20 20 20 78 55 6e 6c  /.          xUnl
34be1 6f 63 6b 4e 6f 74 69 66 79 28 61 41 72 67 2c 20  ockNotify(aArg, 
34be2 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20 20  nArg);.         
34be3 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 20   nArg = 0;.     
34be4 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
34be5 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e     sqlite3EndBen
34be6 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20  ignMalloc();..  
34be7 20 20 20 20 61 41 72 67 5b 6e 41 72 67 2b 2b 5d      aArg[nArg++]
34be8 20 3d 20 70 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67   = p->pUnlockArg
34be9 3b 0a 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e  ;.      xUnlockN
34bea 6f 74 69 66 79 20 3d 20 70 2d 3e 78 55 6e 6c 6f  otify = p->xUnlo
34beb 63 6b 4e 6f 74 69 66 79 3b 0a 20 20 20 20 20 20  ckNotify;.      
34bec 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63  p->pUnlockConnec
34bed 74 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  tion = 0;.      
34bee 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79  p->xUnlockNotify
34bef 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70   = 0;.      p->p
34bf0 55 6e 6c 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20  UnlockArg = 0;. 
34bf1 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65     }..    /* Ste
34bf2 70 20 33 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  p 3. */.    if( 
34bf3 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e  p->pBlockingConn
34bf4 65 63 74 69 6f 6e 3d 3d 30 20 26 26 20 70 2d 3e  ection==0 && p->
34bf5 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
34bf6 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  n==0 ){.      /*
34bf7 20 52 65 6d 6f 76 65 20 63 6f 6e 6e 65 63 74 69   Remove connecti
34bf8 6f 6e 20 70 20 66 72 6f 6d 20 74 68 65 20 62 6c  on p from the bl
34bf9 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
34bfa 73 20 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20  s list. */.     
34bfb 20 2a 70 70 20 3d 20 70 2d 3e 70 4e 65 78 74 42   *pp = p->pNextB
34bfc 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 20 20 70 2d  locked;.      p-
34bfd 3e 70 4e 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20  >pNextBlocked = 
34bfe 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
34bff 20 20 20 20 70 70 20 3d 20 26 70 2d 3e 70 4e 65      pp = &p->pNe
34c00 78 74 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 7d  xtBlocked;.    }
34c01 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 41 72 67  .  }..  if( nArg
34c02 21 3d 30 20 29 7b 0a 20 20 20 20 78 55 6e 6c 6f  !=0 ){.    xUnlo
34c03 63 6b 4e 6f 74 69 66 79 28 61 41 72 67 2c 20 6e  ckNotify(aArg, n
34c04 41 72 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Arg);.  }.  sqli
34c05 74 65 33 5f 66 72 65 65 28 61 44 79 6e 29 3b 0a  te3_free(aDyn);.
34c06 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 20    leaveMutex(); 
34c07 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 61 76 65          /* Leave
34c08 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d   STATIC_MASTER m
34c09 75 74 65 78 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a  utex */.}../*.**
34c0a 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20   This is called 
34c0b 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  when the databas
34c0c 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 73  e connection pas
34c0d 73 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65  sed as an argume
34c0e 6e 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20  nt is .** being 
34c0f 63 6c 6f 73 65 64 2e 20 54 68 65 20 63 6f 6e 6e  closed. The conn
34c10 65 63 74 69 6f 6e 20 69 73 20 72 65 6d 6f 76 65  ection is remove
34c11 64 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b  d from the block
34c12 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49  ed list..*/.SQLI
34c13 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
34c14 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
34c15 6e 43 6c 6f 73 65 64 28 73 71 6c 69 74 65 33 20  nClosed(sqlite3 
34c16 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 43  *db){.  sqlite3C
34c17 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65  onnectionUnlocke
34c18 64 28 64 62 29 3b 0a 20 20 65 6e 74 65 72 4d 75  d(db);.  enterMu
34c19 74 65 78 28 29 3b 0a 20 20 72 65 6d 6f 76 65 46  tex();.  removeF
34c1a 72 6f 6d 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64  romBlockedList(d
34c1b 62 29 3b 0a 20 20 63 68 65 63 6b 4c 69 73 74 50  b);.  checkListP
34c1c 72 6f 70 65 72 74 69 65 73 28 64 62 29 3b 0a 20  roperties(db);. 
34c1d 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d   leaveMutex();.}
34c1e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
34c1f 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
34c20 6e 6f 74 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a  notify.c *******
34c21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c22 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c23 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
34c24 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
34c25 69 6c 65 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a  ile fts3.c *****
34c26 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c27 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c28 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
34c29 30 30 36 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a  006 Oct 10.**.**
34c2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
34c2b 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
34c2c 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
34c2d 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
34c2e 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
34c2f 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
34c30 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
34c31 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
34c32 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
34c33 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
34c34 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
34c35 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
34c36 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
34c37 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
34c38 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
34c39 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
34c3a 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
34c3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c3c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c3d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34c3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
34c40 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 6e 20  *.** This is an 
34c41 53 51 4c 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d  SQLite module im
34c42 70 6c 65 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d  plementing full-
34c43 74 65 78 74 20 73 65 61 72 63 68 2e 0a 2a 2f 0a  text search..*/.
34c44 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  ./*.** The code 
34c45 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20  in this file is 
34c46 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66  only compiled if
34c47 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  :.**.**     * Th
34c48 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
34c49 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20   being built as 
34c4a 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20  an extension.** 
34c4b 20 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20        (in which 
34c4c 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  case SQLITE_CORE
34c4d 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29   is not defined)
34c4e 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  , or.**.**     *
34c4f 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65   The FTS3 module
34c50 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20   is being built 
34c51 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66  into the core of
34c52 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65  .**       SQLite
34c53 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
34c54 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
34c55 53 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a  S3 is defined)..
34c56 2a 2f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  */../* TODO(shes
34c57 73 29 20 43 6f 6e 73 69 64 65 72 20 65 78 70 6f  s) Consider expo
34c58 72 74 69 6e 67 20 74 68 69 73 20 63 6f 6d 6d 65  rting this comme
34c59 6e 74 20 74 6f 20 61 6e 20 48 54 4d 4c 20 66 69  nt to an HTML fi
34c5a 6c 65 20 6f 72 20 74 68 65 0a 2a 2a 20 77 69 6b  le or the.** wik
34c5b 69 2e 0a 2a 2f 0a 2f 2a 20 54 68 65 20 66 75 6c  i..*/./* The ful
34c5c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20 69 73 20  l-text index is 
34c5d 73 74 6f 72 65 64 20 69 6e 20 61 20 73 65 72 69  stored in a seri
34c5e 65 73 20 6f 66 20 62 2b 74 72 65 65 20 28 2d 6c  es of b+tree (-l
34c5f 69 6b 65 29 0a 2a 2a 20 73 74 72 75 63 74 75 72  ike).** structur
34c60 65 73 20 63 61 6c 6c 65 64 20 73 65 67 6d 65 6e  es called segmen
34c61 74 73 20 77 68 69 63 68 20 6d 61 70 20 74 65 72  ts which map ter
34c62 6d 73 20 74 6f 20 64 6f 63 6c 69 73 74 73 2e 20  ms to doclists. 
34c63 20 54 68 65 0a 2a 2a 20 73 74 72 75 63 74 75 72   The.** structur
34c64 65 73 20 61 72 65 20 6c 69 6b 65 20 62 2b 74 72  es are like b+tr
34c65 65 65 73 20 69 6e 20 6c 61 79 6f 75 74 2c 20 62  ees in layout, b
34c66 75 74 20 61 72 65 20 63 6f 6e 73 74 72 75 63 74  ut are construct
34c67 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 62  ed from the.** b
34c68 6f 74 74 6f 6d 20 75 70 20 69 6e 20 6f 70 74 69  ottom up in opti
34c69 6d 61 6c 20 66 61 73 68 69 6f 6e 20 61 6e 64 20  mal fashion and 
34c6a 61 72 65 20 6e 6f 74 20 75 70 64 61 74 61 62 6c  are not updatabl
34c6b 65 2e 20 20 53 69 6e 63 65 20 74 72 65 65 73 0a  e.  Since trees.
34c6c 2a 2a 20 61 72 65 20 62 75 69 6c 74 20 66 72 6f  ** are built fro
34c6d 6d 20 74 68 65 20 62 6f 74 74 6f 6d 20 75 70 2c  m the bottom up,
34c6e 20 74 68 69 6e 67 73 20 77 69 6c 6c 20 62 65 20   things will be 
34c6f 64 65 73 63 72 69 62 65 64 20 66 72 6f 6d 20 74  described from t
34c70 68 65 0a 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 2e  he.** bottom up.
34c71 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 56 61 72 69  .**.**.**** Vari
34c72 6e 74 73 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  nts ****.** The 
34c73 62 61 73 69 63 20 75 6e 69 74 20 6f 66 20 65 6e  basic unit of en
34c74 63 6f 64 69 6e 67 20 69 73 20 61 20 76 61 72 69  coding is a vari
34c75 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
34c76 67 65 72 20 63 61 6c 6c 65 64 20 61 0a 2a 2a 20  ger called a.** 
34c77 76 61 72 69 6e 74 2e 20 20 57 65 20 65 6e 63 6f  varint.  We enco
34c78 64 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67  de variable-leng
34c79 74 68 20 69 6e 74 65 67 65 72 73 20 69 6e 20 6c  th integers in l
34c7a 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 6f 72 64  ittle-endian ord
34c7b 65 72 0a 2a 2a 20 75 73 69 6e 67 20 73 65 76 65  er.** using seve
34c7c 6e 20 62 69 74 73 20 2a 20 70 65 72 20 62 79 74  n bits * per byt
34c7d 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  e as follows:.**
34c7e 0a 2a 2a 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20  .** KEY:.**     
34c7f 20 20 20 20 41 20 3d 20 30 78 78 78 78 78 78 78      A = 0xxxxxxx
34c80 20 20 20 20 37 20 62 69 74 73 20 6f 66 20 64 61      7 bits of da
34c81 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20  ta and one flag 
34c82 62 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 42  bit.**         B
34c83 20 3d 20 31 78 78 78 78 78 78 78 20 20 20 20 37   = 1xxxxxxx    7
34c84 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e   bits of data an
34c85 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a  d one flag bit.*
34c86 2a 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d 20 41  *.**  7 bits - A
34c87 0a 2a 2a 20 31 34 20 62 69 74 73 20 2d 20 42 41  .** 14 bits - BA
34c88 0a 2a 2a 20 32 31 20 62 69 74 73 20 2d 20 42 42  .** 21 bits - BB
34c89 41 0a 2a 2a 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a  A.** and so on..
34c8a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 69 64  **.** This is id
34c8b 65 6e 74 69 63 61 6c 20 74 6f 20 68 6f 77 20 73  entical to how s
34c8c 71 6c 69 74 65 20 65 6e 63 6f 64 65 73 20 76 61  qlite encodes va
34c8d 72 69 6e 74 73 20 28 73 65 65 20 75 74 69 6c 2e  rints (see util.
34c8e 63 29 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 44  c)..**.**.**** D
34c8f 6f 63 75 6d 65 6e 74 20 6c 69 73 74 73 20 2a 2a  ocument lists **
34c90 2a 2a 0a 2a 2a 20 41 20 64 6f 63 6c 69 73 74 20  **.** A doclist 
34c91 28 64 6f 63 75 6d 65 6e 74 20 6c 69 73 74 29 20  (document list) 
34c92 68 6f 6c 64 73 20 61 20 64 6f 63 69 64 2d 73 6f  holds a docid-so
34c93 72 74 65 64 20 6c 69 73 74 20 6f 66 20 68 69 74  rted list of hit
34c94 73 20 66 6f 72 20 61 0a 2a 2a 20 67 69 76 65 6e  s for a.** given
34c95 20 74 65 72 6d 2e 20 20 44 6f 63 6c 69 73 74 73   term.  Doclists
34c96 20 68 6f 6c 64 20 64 6f 63 69 64 73 2c 20 61 6e   hold docids, an
34c97 64 20 63 61 6e 20 6f 70 74 69 6f 6e 61 6c 6c 79  d can optionally
34c98 20 61 73 73 6f 63 69 61 74 65 0a 2a 2a 20 74 6f   associate.** to
34c99 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e  ken positions an
34c9a 64 20 6f 66 66 73 65 74 73 20 77 69 74 68 20 64  d offsets with d
34c9b 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 44  ocids..**.** A D
34c9c 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53  L_POSITIONS_OFFS
34c9d 45 54 53 20 64 6f 63 6c 69 73 74 20 69 73 20 73  ETS doclist is s
34c9e 74 6f 72 65 64 20 6c 69 6b 65 20 74 68 69 73 3a  tored like this:
34c9f 0a 2a 2a 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a  .**.** array {.*
34ca0 2a 20 20 20 76 61 72 69 6e 74 20 64 6f 63 69 64  *   varint docid
34ca1 3b 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 20 20  ;.**   array {  
34ca2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 70                (p
34ca3 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72  osition list for
34ca4 20 63 6f 6c 75 6d 6e 20 30 29 0a 2a 2a 20 20 20   column 0).**   
34ca5 20 20 76 61 72 69 6e 74 20 70 6f 73 69 74 69 6f    varint positio
34ca6 6e 3b 20 20 20 20 20 28 64 65 6c 74 61 20 66 72  n;     (delta fr
34ca7 6f 6d 20 70 72 65 76 69 6f 75 73 20 70 6f 73 69  om previous posi
34ca8 74 69 6f 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41  tion plus POS_BA
34ca9 53 45 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e  SE).**     varin
34caa 74 20 73 74 61 72 74 4f 66 66 73 65 74 3b 20 20  t startOffset;  
34cab 28 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76  (delta from prev
34cac 69 6f 75 73 20 73 74 61 72 74 4f 66 66 73 65 74  ious startOffset
34cad 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20  ).**     varint 
34cae 65 6e 64 4f 66 66 73 65 74 3b 20 20 20 20 28 64  endOffset;    (d
34caf 65 6c 74 61 20 66 72 6f 6d 20 73 74 61 72 74 4f  elta from startO
34cb0 66 66 73 65 74 29 0a 2a 2a 20 20 20 7d 0a 2a 2a  ffset).**   }.**
34cb1 20 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20     array {.**   
34cb2 20 20 76 61 72 69 6e 74 20 50 4f 53 5f 43 4f 4c    varint POS_COL
34cb3 55 4d 4e 3b 20 20 20 28 6d 61 72 6b 73 20 73 74  UMN;   (marks st
34cb4 61 72 74 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20  art of position 
34cb5 6c 69 73 74 20 66 6f 72 20 6e 65 77 20 63 6f 6c  list for new col
34cb6 75 6d 6e 29 0a 2a 2a 20 20 20 20 20 76 61 72 69  umn).**     vari
34cb7 6e 74 20 63 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  nt column;      
34cb8 20 28 69 6e 64 65 78 20 6f 66 20 6e 65 77 20 63   (index of new c
34cb9 6f 6c 75 6d 6e 29 0a 2a 2a 20 20 20 20 20 61 72  olumn).**     ar
34cba 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20 20 20 76  ray {.**       v
34cbb 61 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20  arint position; 
34cbc 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 70 72    (delta from pr
34cbd 65 76 69 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20  evious position 
34cbe 70 6c 75 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a  plus POS_BASE).*
34cbf 2a 20 20 20 20 20 20 20 76 61 72 69 6e 74 20 73  *       varint s
34cc0 74 61 72 74 4f 66 66 73 65 74 3b 28 64 65 6c 74  tartOffset;(delt
34cc1 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20  a from previous 
34cc2 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20  startOffset).** 
34cc3 20 20 20 20 20 20 76 61 72 69 6e 74 20 65 6e 64        varint end
34cc4 4f 66 66 73 65 74 3b 20 20 28 64 65 6c 74 61 20  Offset;  (delta 
34cc5 66 72 6f 6d 20 73 74 61 72 74 4f 66 66 73 65 74  from startOffset
34cc6 29 0a 2a 2a 20 20 20 20 20 7d 0a 2a 2a 20 20 20  ).**     }.**   
34cc7 7d 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 50 4f  }.**   varint PO
34cc8 53 5f 45 4e 44 3b 20 20 20 20 20 20 20 20 28 6d  S_END;        (m
34cc9 61 72 6b 73 20 65 6e 64 20 6f 66 20 70 6f 73 69  arks end of posi
34cca 74 69 6f 6e 73 20 66 6f 72 20 74 68 69 73 20 64  tions for this d
34ccb 6f 63 75 6d 65 6e 74 2e 0a 2a 2a 20 7d 0a 2a 2a  ocument..** }.**
34ccc 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72 61 79 20  .** Here, array 
34ccd 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f  { X } means zero
34cce 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65   or more occurre
34ccf 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63  nces of X, adjac
34cd0 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79  ent in.** memory
34cd1 2e 20 20 41 20 22 70 6f 73 69 74 69 6f 6e 22 20  .  A "position" 
34cd2 69 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 61  is an index of a
34cd3 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 74 6f   token in the to
34cd4 6b 65 6e 20 73 74 72 65 61 6d 0a 2a 2a 20 67 65  ken stream.** ge
34cd5 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20 74  nerated by the t
34cd6 6f 6b 65 6e 69 7a 65 72 2c 20 77 68 69 6c 65 20  okenizer, while 
34cd7 61 6e 20 22 6f 66 66 73 65 74 22 20 69 73 20 61  an "offset" is a
34cd8 20 62 79 74 65 20 6f 66 66 73 65 74 2c 0a 2a 2a   byte offset,.**
34cd9 20 62 6f 74 68 20 62 61 73 65 64 20 61 74 20 30   both based at 0
34cda 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 50 4f 53  .  Note that POS
34cdb 5f 45 4e 44 20 61 6e 64 20 50 4f 53 5f 43 4f 4c  _END and POS_COL
34cdc 55 4d 4e 20 6f 63 63 75 72 20 69 6e 20 74 68 65  UMN occur in the
34cdd 0a 2a 2a 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c  .** same logical
34cde 20 70 6c 61 63 65 20 61 73 20 74 68 65 20 70 6f   place as the po
34cdf 73 69 74 69 6f 6e 20 65 6c 65 6d 65 6e 74 2c 20  sition element, 
34ce0 61 6e 64 20 61 63 74 20 61 73 20 73 65 6e 74 69  and act as senti
34ce1 6e 61 6c 73 0a 2a 2a 20 65 6e 64 69 6e 67 20 61  nals.** ending a
34ce2 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 61   position list a
34ce3 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c  rray..**.** A DL
34ce4 5f 50 4f 53 49 54 49 4f 4e 53 20 64 6f 63 6c 69  _POSITIONS docli
34ce5 73 74 20 6f 6d 69 74 73 20 74 68 65 20 73 74 61  st omits the sta
34ce6 72 74 4f 66 66 73 65 74 20 61 6e 64 20 65 6e 64  rtOffset and end
34ce7 4f 66 66 73 65 74 0a 2a 2a 20 69 6e 66 6f 72 6d  Offset.** inform
34ce8 61 74 69 6f 6e 2e 20 20 41 20 44 4c 5f 44 4f 43  ation.  A DL_DOC
34ce9 49 44 53 20 64 6f 63 6c 69 73 74 20 6f 6d 69 74  IDS doclist omit
34cea 73 20 62 6f 74 68 20 74 68 65 20 70 6f 73 69 74  s both the posit
34ceb 69 6f 6e 20 61 6e 64 0a 2a 2a 20 6f 66 66 73 65  ion and.** offse
34cec 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 62  t information, b
34ced 65 63 6f 6d 69 6e 67 20 61 6e 20 61 72 72 61 79  ecoming an array
34cee 20 6f 66 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64   of varint-encod
34cef 65 64 20 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a  ed docids..**.**
34cf0 20 4f 6e 2d 64 69 73 6b 20 64 61 74 61 20 69 73   On-disk data is
34cf1 20 73 74 6f 72 65 64 20 61 73 20 74 79 70 65 20   stored as type 
34cf2 44 4c 5f 44 45 46 41 55 4c 54 2c 20 73 6f 20 77  DL_DEFAULT, so w
34cf3 65 20 64 6f 6e 27 74 20 73 65 72 69 61 6c 69 7a  e don't serializ
34cf4 65 0a 2a 2a 20 74 68 65 20 74 79 70 65 2e 20 20  e.** the type.  
34cf5 44 75 65 20 74 6f 20 68 6f 77 20 64 65 6c 65 74  Due to how delet
34cf6 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  ion is implement
34cf7 65 64 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e  ed in the segmen
34cf8 74 61 74 69 6f 6e 0a 2a 2a 20 73 79 73 74 65 6d  tation.** system
34cf9 2c 20 6f 6e 2d 64 69 73 6b 20 64 6f 63 6c 69 73  , on-disk doclis
34cfa 74 73 20 4d 55 53 54 20 73 74 6f 72 65 20 61 74  ts MUST store at
34cfb 20 6c 65 61 73 74 20 70 6f 73 69 74 69 6f 6e 73   least positions
34cfc 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67  ..**.**.**** Seg
34cfd 6d 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 73 20  ment leaf nodes 
34cfe 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20  ****.** Segment 
34cff 6c 65 61 66 20 6e 6f 64 65 73 20 73 74 6f 72 65  leaf nodes store
34d00 20 74 65 72 6d 73 20 61 6e 64 20 64 6f 63 6c 69   terms and docli
34d01 73 74 73 2c 20 6f 72 64 65 72 65 64 20 62 79 20  sts, ordered by 
34d02 74 65 72 6d 2e 20 20 4c 65 61 66 0a 2a 2a 20 6e  term.  Leaf.** n
34d03 6f 64 65 73 20 61 72 65 20 77 72 69 74 74 65 6e  odes are written
34d04 20 75 73 69 6e 67 20 4c 65 61 66 57 72 69 74 65   using LeafWrite
34d05 72 2c 20 61 6e 64 20 72 65 61 64 20 75 73 69 6e  r, and read usin
34d06 67 20 4c 65 61 66 52 65 61 64 65 72 20 28 74 6f  g LeafReader (to
34d07 0a 2a 2a 20 69 74 65 72 61 74 65 20 74 68 72 6f  .** iterate thro
34d08 75 67 68 20 61 20 73 69 6e 67 6c 65 20 6c 65 61  ugh a single lea
34d09 66 20 6e 6f 64 65 27 73 20 64 61 74 61 29 20 61  f node's data) a
34d0a 6e 64 20 4c 65 61 76 65 73 52 65 61 64 65 72 20  nd LeavesReader 
34d0b 28 74 6f 0a 2a 2a 20 69 74 65 72 61 74 65 20 74  (to.** iterate t
34d0c 68 72 6f 75 67 68 20 61 20 73 65 67 6d 65 6e 74  hrough a segment
34d0d 27 73 20 65 6e 74 69 72 65 20 6c 65 61 66 20 6c  's entire leaf l
34d0e 61 79 65 72 29 2e 20 20 4c 65 61 66 20 6e 6f 64  ayer).  Leaf nod
34d0f 65 73 20 68 61 76 65 0a 2a 2a 20 74 68 65 20 66  es have.** the f
34d10 6f 72 6d 61 74 3a 0a 2a 2a 0a 2a 2a 20 76 61 72  ormat:.**.** var
34d11 69 6e 74 20 69 48 65 69 67 68 74 3b 20 20 20 20  int iHeight;    
34d12 20 20 20 20 20 20 20 20 20 28 68 65 69 67 68 74           (height
34d13 20 66 72 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c   from leaf level
34d14 2c 20 61 6c 77 61 79 73 20 30 29 0a 2a 2a 20 76  , always 0).** v
34d15 61 72 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20  arint nTerm;    
34d16 20 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67             (leng
34d17 74 68 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d  th of first term
34d18 29 0a 2a 2a 20 63 68 61 72 20 70 54 65 72 6d 5b  ).** char pTerm[
34d19 6e 54 65 72 6d 5d 3b 20 20 20 20 20 20 20 20 20  nTerm];         
34d1a 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72   (content of fir
34d1b 73 74 20 74 65 72 6d 29 0a 2a 2a 20 76 61 72 69  st term).** vari
34d1c 6e 74 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20  nt nDoclist;    
34d1d 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
34d1e 6f 66 20 74 65 72 6d 27 73 20 61 73 73 6f 63 69  of term's associ
34d1f 61 74 65 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a  ated doclist).**
34d20 20 63 68 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e   char pDoclist[n
34d21 44 6f 63 6c 69 73 74 5d 3b 20 20 20 20 28 63 6f  Doclist];    (co
34d22 6e 74 65 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74  ntent of doclist
34d23 29 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20  ).** array {.** 
34d24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34d25 20 20 20 20 20 20 20 20 20 20 20 20 28 66 75 72              (fur
34d26 74 68 65 72 20 74 65 72 6d 73 20 61 72 65 20 64  ther terms are d
34d27 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a  elta-encoded).**
34d28 20 20 20 76 61 72 69 6e 74 20 6e 50 72 65 66 69     varint nPrefi
34d29 78 3b 20 20 20 20 20 20 20 20 20 20 20 28 6c 65  x;           (le
34d2a 6e 67 74 68 20 6f 66 20 70 72 65 66 69 78 20 73  ngth of prefix s
34d2b 68 61 72 65 64 20 77 69 74 68 20 70 72 65 76 69  hared with previ
34d2c 6f 75 73 20 74 65 72 6d 29 0a 2a 2a 20 20 20 76  ous term).**   v
34d2d 61 72 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20  arint nSuffix;  
34d2e 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68           (length
34d2f 20 6f 66 20 75 6e 73 68 61 72 65 64 20 73 75 66   of unshared suf
34d30 66 69 78 29 0a 2a 2a 20 20 20 63 68 61 72 20 70  fix).**   char p
34d31 54 65 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66  TermSuffix[nSuff
34d32 69 78 5d 3b 28 75 6e 73 68 61 72 65 64 20 73 75  ix];(unshared su
34d33 66 66 69 78 20 6f 66 20 6e 65 78 74 20 74 65 72  ffix of next ter
34d34 6d 29 0a 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e  m).**   varint n
34d35 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20  Doclist;        
34d36 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 74 65 72    (length of ter
34d37 6d 27 73 20 61 73 73 6f 63 69 61 74 65 64 20 64  m's associated d
34d38 6f 63 6c 69 73 74 29 0a 2a 2a 20 20 20 63 68 61  oclist).**   cha
34d39 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c  r pDoclist[nDocl
34d3a 69 73 74 5d 3b 20 20 28 63 6f 6e 74 65 6e 74 20  ist];  (content 
34d3b 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 7d  of doclist).** }
34d3c 0a 2a 2a 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72  .**.** Here, arr
34d3d 61 79 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a  ay { X } means z
34d3e 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75  ero or more occu
34d3f 72 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64  rrences of X, ad
34d40 6a 61 63 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d  jacent in.** mem
34d41 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 20  ory..**.** Leaf 
34d42 6e 6f 64 65 73 20 61 72 65 20 62 72 6f 6b 65 6e  nodes are broken
34d43 20 69 6e 74 6f 20 62 6c 6f 63 6b 73 20 77 68 69   into blocks whi
34d44 63 68 20 61 72 65 20 73 74 6f 72 65 64 20 63 6f  ch are stored co
34d45 6e 74 69 67 75 6f 75 73 6c 79 20 69 6e 0a 2a 2a  ntiguously in.**
34d46 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20   the %_segments 
34d47 74 61 62 6c 65 20 69 6e 20 73 6f 72 74 65 64 20  table in sorted 
34d48 6f 72 64 65 72 2e 20 20 54 68 69 73 20 6d 65 61  order.  This mea
34d49 6e 73 20 74 68 61 74 20 77 68 65 6e 20 74 68 65  ns that when the
34d4a 20 65 6e 64 0a 2a 2a 20 6f 66 20 61 20 6e 6f 64   end.** of a nod
34d4b 65 20 69 73 20 72 65 61 63 68 65 64 2c 20 74 68  e is reached, th
34d4c 65 20 6e 65 78 74 20 74 65 72 6d 20 69 73 20 69  e next term is i
34d4d 6e 20 74 68 65 20 6e 6f 64 65 20 77 69 74 68 20  n the node with 
34d4e 74 68 65 20 6e 65 78 74 0a 2a 2a 20 67 72 65 61  the next.** grea
34d4f 74 65 72 20 6e 6f 64 65 20 69 64 2e 0a 2a 2a 0a  ter node id..**.
34d50 2a 2a 20 4e 65 77 20 64 61 74 61 20 69 73 20 73  ** New data is s
34d51 70 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65 77 20  pilled to a new 
34d52 6c 65 61 66 20 6e 6f 64 65 20 77 68 65 6e 20 74  leaf node when t
34d53 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 0a  he current node.
34d54 2a 2a 20 65 78 63 65 65 64 73 20 4c 45 41 46 5f  ** exceeds LEAF_
34d55 4d 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75  MAX bytes (defau
34d56 6c 74 20 32 30 34 38 29 2e 20 20 4e 65 77 20 64  lt 2048).  New d
34d57 61 74 61 20 77 68 69 63 68 20 69 74 73 65 6c 66  ata which itself
34d58 20 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68   is.** larger th
34d59 61 6e 20 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49  an STANDALONE_MI
34d5a 4e 20 28 64 65 66 61 75 6c 74 20 31 30 32 34 29  N (default 1024)
34d5b 20 69 73 20 70 6c 61 63 65 64 20 69 6e 20 61 20   is placed in a 
34d5c 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 6e 6f  standalone.** no
34d5d 64 65 20 28 61 20 6c 65 61 66 20 6e 6f 64 65 20  de (a leaf node 
34d5e 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 74 65  with a single te
34d5f 72 6d 20 61 6e 64 20 64 6f 63 6c 69 73 74 29 2e  rm and doclist).
34d60 20 20 54 68 65 20 67 6f 61 6c 20 6f 66 0a 2a 2a    The goal of.**
34d61 20 74 68 65 73 65 20 73 65 74 74 69 6e 67 73 20   these settings 
34d62 69 73 20 74 6f 20 70 61 63 6b 20 74 6f 67 65 74  is to pack toget
34d63 68 65 72 20 67 72 6f 75 70 73 20 6f 66 20 73 6d  her groups of sm
34d64 61 6c 6c 20 64 6f 63 6c 69 73 74 73 20 77 68 69  all doclists whi
34d65 6c 65 0a 2a 2a 20 6d 61 6b 69 6e 67 20 69 74 20  le.** making it 
34d66 65 66 66 69 63 69 65 6e 74 20 74 6f 20 64 69 72  efficient to dir
34d67 65 63 74 6c 79 20 61 63 63 65 73 73 20 6c 61 72  ectly access lar
34d68 67 65 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68  ge doclists.  Th
34d69 65 0a 2a 2a 20 61 73 73 75 6d 70 74 69 6f 6e 20  e.** assumption 
34d6a 69 73 20 74 68 61 74 20 6c 61 72 67 65 20 64 6f  is that large do
34d6b 63 6c 69 73 74 73 20 72 65 70 72 65 73 65 6e 74  clists represent
34d6c 20 74 65 72 6d 73 20 77 68 69 63 68 20 61 72 65   terms which are
34d6d 20 6d 6f 72 65 0a 2a 2a 20 6c 69 6b 65 6c 79 20   more.** likely 
34d6e 74 6f 20 62 65 20 71 75 65 72 79 20 74 61 72 67  to be query targ
34d6f 65 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28  ets..**.** TODO(
34d70 73 68 65 73 73 29 20 49 74 20 6d 61 79 20 62 65  shess) It may be
34d71 20 75 73 65 66 75 6c 20 66 6f 72 20 62 6c 6f 63   useful for bloc
34d72 6b 69 6e 67 20 64 65 63 69 73 69 6f 6e 73 20 74  king decisions t
34d73 6f 20 62 65 20 6d 6f 72 65 0a 2a 2a 20 64 79 6e  o be more.** dyn
34d74 61 6d 69 63 2e 20 20 46 6f 72 20 69 6e 73 74 61  amic.  For insta
34d75 6e 63 65 2c 20 69 74 20 6d 61 79 20 6d 61 6b 65  nce, it may make
34d76 20 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 68   more sense to h
34d77 61 76 65 20 61 20 32 2e 35 6b 20 6c 65 61 66 0a  ave a 2.5k leaf.
34d78 2a 2a 20 6e 6f 64 65 20 72 61 74 68 65 72 20 74  ** node rather t
34d79 68 61 6e 20 73 70 6c 69 74 74 69 6e 67 20 69 6e  han splitting in
34d7a 74 6f 20 32 6b 20 61 6e 64 20 2e 35 6b 20 6e 6f  to 2k and .5k no
34d7b 64 65 73 2e 20 20 4d 79 20 69 6e 74 75 69 74 69  des.  My intuiti
34d7c 6f 6e 20 69 73 0a 2a 2a 20 74 68 61 74 20 74 68  on is.** that th
34d7d 69 73 20 6d 69 67 68 74 20 65 78 74 65 6e 64 20  is might extend 
34d7e 74 68 72 6f 75 67 68 20 32 78 20 6f 72 20 34 78  through 2x or 4x
34d7f 20 74 68 65 20 70 61 67 65 73 69 7a 65 2e 0a 2a   the pagesize..*
34d80 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e  *.**.**** Segmen
34d81 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73  t interior nodes
34d82 20 2a 2a 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74   ****.** Segment
34d83 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
34d84 73 74 6f 72 65 20 62 6c 6f 63 6b 69 64 73 20 66  store blockids f
34d85 6f 72 20 73 75 62 74 72 65 65 20 6e 6f 64 65 73  or subtree nodes
34d86 20 61 6e 64 20 74 65 72 6d 73 0a 2a 2a 20 74 6f   and terms.** to
34d87 20 64 65 73 63 72 69 62 65 20 77 68 61 74 20 64   describe what d
34d88 61 74 61 20 69 73 20 73 74 6f 72 65 64 20 62 79  ata is stored by
34d89 20 74 68 65 20 65 61 63 68 20 73 75 62 74 72 65   the each subtre
34d8a 65 2e 20 20 49 6e 74 65 72 69 6f 72 0a 2a 2a 20  e.  Interior.** 
34d8b 6e 6f 64 65 73 20 61 72 65 20 77 72 69 74 74 65  nodes are writte
34d8c 6e 20 75 73 69 6e 67 20 49 6e 74 65 72 69 6f 72  n using Interior
34d8d 57 72 69 74 65 72 2c 20 61 6e 64 20 72 65 61 64  Writer, and read
34d8e 20 75 73 69 6e 67 0a 2a 2a 20 49 6e 74 65 72 69   using.** Interi
34d8f 6f 72 52 65 61 64 65 72 2e 20 20 49 6e 74 65 72  orReader.  Inter
34d90 69 6f 72 57 72 69 74 65 72 73 20 61 72 65 20 63  iorWriters are c
34d91 72 65 61 74 65 64 20 61 73 20 6e 65 65 64 65 64  reated as needed
34d92 20 77 68 65 6e 0a 2a 2a 20 53 65 67 6d 65 6e 74   when.** Segment
34d93 57 72 69 74 65 72 20 63 72 65 61 74 65 73 20 6e  Writer creates n
34d94 65 77 20 6c 65 61 66 20 6e 6f 64 65 73 2c 20 6f  ew leaf nodes, o
34d95 72 20 77 68 65 6e 20 61 6e 20 69 6e 74 65 72 69  r when an interi
34d96 6f 72 20 6e 6f 64 65 0a 2a 2a 20 69 74 73 65 6c  or node.** itsel
34d97 66 20 67 72 6f 77 73 20 74 6f 6f 20 62 69 67 20  f grows too big 
34d98 61 6e 64 20 6d 75 73 74 20 62 65 20 73 70 6c 69  and must be spli
34d99 74 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 20 6f  t.  The format o
34d9a 66 20 69 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f  f interior.** no
34d9b 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e  des:.**.** varin
34d9c 74 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20  t iHeight;      
34d9d 20 20 20 20 20 28 68 65 69 67 68 74 20 66 72 6f       (height fro
34d9e 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c  m leaf level, al
34d9f 77 61 79 73 20 3e 30 29 0a 2a 2a 20 76 61 72 69  ways >0).** vari
34da0 6e 74 20 69 42 6c 6f 63 6b 69 64 3b 20 20 20 20  nt iBlockid;    
34da1 20 20 20 20 20 20 28 62 6c 6f 63 6b 20 69 64 20        (block id 
34da2 6f 66 20 6e 6f 64 65 27 73 20 6c 65 66 74 6d 6f  of node's leftmo
34da3 73 74 20 73 75 62 74 72 65 65 29 0a 2a 2a 20 6f  st subtree).** o
34da4 70 74 69 6f 6e 61 6c 20 7b 0a 2a 2a 20 20 20 76  ptional {.**   v
34da5 61 72 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20  arint nTerm;    
34da6 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f         (length o
34da7 66 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a  f first term).**
34da8 20 20 20 63 68 61 72 20 70 54 65 72 6d 5b 6e 54     char pTerm[nT
34da9 65 72 6d 5d 3b 20 20 20 20 20 20 28 63 6f 6e 74  erm];      (cont
34daa 65 6e 74 20 6f 66 20 66 69 72 73 74 20 74 65 72  ent of first ter
34dab 6d 29 0a 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a  m).**   array {.
34dac 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
34dad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34dae 20 20 28 66 75 72 74 68 65 72 20 74 65 72 6d 73    (further terms
34daf 20 61 72 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64   are delta-encod
34db0 65 64 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e  ed).**     varin
34db1 74 20 6e 50 72 65 66 69 78 3b 20 20 20 20 20 20  t nPrefix;      
34db2 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
34db3 20 73 68 61 72 65 64 20 70 72 65 66 69 78 20 77   shared prefix w
34db4 69 74 68 20 70 72 65 76 69 6f 75 73 20 74 65 72  ith previous ter
34db5 6d 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74  m).**     varint
34db6 20 6e 53 75 66 66 69 78 3b 20 20 20 20 20 20 20   nSuffix;       
34db7 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
34db8 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 29  unshared suffix)
34db9 0a 2a 2a 20 20 20 20 20 63 68 61 72 20 70 54 65  .**     char pTe
34dba 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78  rmSuffix[nSuffix
34dbb 5d 3b 20 28 75 6e 73 68 61 72 65 64 20 73 75 66  ]; (unshared suf
34dbc 66 69 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d  fix of next term
34dbd 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 7d 0a 2a 2a  ).**   }.** }.**
34dbe 0a 2a 2a 20 48 65 72 65 2c 20 6f 70 74 69 6f 6e  .** Here, option
34dbf 61 6c 20 7b 20 58 20 7d 20 6d 65 61 6e 73 20 61  al { X } means a
34dc0 6e 20 6f 70 74 69 6f 6e 61 6c 20 65 6c 65 6d 65  n optional eleme
34dc1 6e 74 2c 20 77 68 69 6c 65 20 61 72 72 61 79 20  nt, while array 
34dc2 7b 20 58 20 7d 0a 2a 2a 20 6d 65 61 6e 73 20 7a  { X }.** means z
34dc3 65 72 6f 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75  ero or more occu
34dc4 72 72 65 6e 63 65 73 20 6f 66 20 58 2c 20 61 64  rrences of X, ad
34dc5 6a 61 63 65 6e 74 20 69 6e 20 6d 65 6d 6f 72 79  jacent in memory
34dc6 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 72  ..**.** An inter
34dc7 69 6f 72 20 6e 6f 64 65 20 65 6e 63 6f 64 65 73  ior node encodes
34dc8 20 6e 20 74 65 72 6d 73 20 73 65 70 61 72 61 74   n terms separat
34dc9 69 6e 67 20 6e 2b 31 20 73 75 62 74 72 65 65 73  ing n+1 subtrees
34dca 2e 20 20 54 68 65 0a 2a 2a 20 73 75 62 74 72 65  .  The.** subtre
34dcb 65 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 6f 6e  e blocks are con
34dcc 74 69 67 75 6f 75 73 2c 20 73 6f 20 6f 6e 6c 79  tiguous, so only
34dcd 20 74 68 65 20 66 69 72 73 74 20 73 75 62 74 72   the first subtr
34dce 65 65 27 73 20 62 6c 6f 63 6b 69 64 0a 2a 2a 20  ee's blockid.** 
34dcf 69 73 20 65 6e 63 6f 64 65 64 2e 20 20 54 68 65  is encoded.  The
34dd0 20 73 75 62 74 72 65 65 20 61 74 20 69 42 6c 6f   subtree at iBlo
34dd1 63 6b 69 64 20 77 69 6c 6c 20 63 6f 6e 74 61 69  ckid will contai
34dd2 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6c 65 73 73  n all terms less
34dd3 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 66 69 72  .** than the fir
34dd4 73 74 20 74 65 72 6d 20 65 6e 63 6f 64 65 64 20  st term encoded 
34dd5 28 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20 69 66  (or all terms if
34dd6 20 6e 6f 20 74 65 72 6d 20 69 73 20 65 6e 63 6f   no term is enco
34dd7 64 65 64 29 2e 0a 2a 2a 20 4f 74 68 65 72 77 69  ded)..** Otherwi
34dd8 73 65 2c 20 66 6f 72 20 74 65 72 6d 73 20 67 72  se, for terms gr
34dd9 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
34dda 75 61 6c 20 74 6f 20 70 54 65 72 6d 5b 69 5d 20  ual to pTerm[i] 
34ddb 62 75 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e  but less.** than
34ddc 20 70 54 65 72 6d 5b 69 2b 31 5d 2c 20 74 68 65   pTerm[i+1], the
34ddd 20 73 75 62 74 72 65 65 20 66 6f 72 20 74 68 61   subtree for tha
34dde 74 20 74 65 72 6d 20 77 69 6c 6c 20 62 65 20 72  t term will be r
34ddf 6f 6f 74 65 64 20 61 74 0a 2a 2a 20 69 42 6c 6f  ooted at.** iBlo
34de0 63 6b 69 64 2b 69 2e 20 20 49 6e 74 65 72 69 6f  ckid+i.  Interio
34de1 72 20 6e 6f 64 65 73 20 6f 6e 6c 79 20 73 74 6f  r nodes only sto
34de2 72 65 20 65 6e 6f 75 67 68 20 74 65 72 6d 20 64  re enough term d
34de3 61 74 61 20 74 6f 0a 2a 2a 20 64 69 73 74 69 6e  ata to.** distin
34de4 67 75 69 73 68 20 61 64 6a 61 63 65 6e 74 20 63  guish adjacent c
34de5 68 69 6c 64 72 65 6e 20 28 69 66 20 74 68 65 20  hildren (if the 
34de6 72 69 67 68 74 6d 6f 73 74 20 74 65 72 6d 20 6f  rightmost term o
34de7 66 20 74 68 65 20 6c 65 66 74 0a 2a 2a 20 63 68  f the left.** ch
34de8 69 6c 64 20 69 73 20 22 73 6f 6d 65 74 68 69 6e  ild is "somethin
34de9 67 22 2c 20 61 6e 64 20 74 68 65 20 6c 65 66 74  g", and the left
34dea 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65  most term of the
34deb 20 72 69 67 68 74 20 63 68 69 6c 64 20 69 73 0a   right child is.
34dec 2a 2a 20 22 77 69 63 6b 65 64 22 2c 20 6f 6e 6c  ** "wicked", onl
34ded 79 20 22 77 22 20 69 73 20 73 74 6f 72 65 64 29  y "w" is stored)
34dee 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61  ..**.** New data
34def 20 69 73 20 73 70 69 6c 6c 65 64 20 74 6f 20 61   is spilled to a
34df0 20 6e 65 77 20 69 6e 74 65 72 69 6f 72 20 6e 6f   new interior no
34df1 64 65 20 61 74 20 74 68 65 20 73 61 6d 65 20 68  de at the same h
34df2 65 69 67 68 74 20 77 68 65 6e 0a 2a 2a 20 74 68  eight when.** th
34df3 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65 20 65  e current node e
34df4 78 63 65 65 64 73 20 49 4e 54 45 52 49 4f 52 5f  xceeds INTERIOR_
34df5 4d 41 58 20 62 79 74 65 73 20 28 64 65 66 61 75  MAX bytes (defau
34df6 6c 74 20 32 30 34 38 29 2e 0a 2a 2a 20 49 4e 54  lt 2048)..** INT
34df7 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20  ERIOR_MIN_TERMS 
34df8 28 64 65 66 61 75 6c 74 20 37 29 20 6b 65 65 70  (default 7) keep
34df9 73 20 6c 61 72 67 65 20 74 65 72 6d 73 20 66 72  s large terms fr
34dfa 6f 6d 20 6d 6f 6e 6f 70 6f 6c 69 7a 69 6e 67 0a  om monopolizing.
34dfb 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ** interior node
34dfc 73 20 61 6e 64 20 6d 61 6b 69 6e 67 20 74 68 65  s and making the
34dfd 20 74 72 65 65 20 74 6f 6f 20 73 6b 69 6e 6e 79   tree too skinny
34dfe 2e 20 20 54 68 65 20 69 6e 74 65 72 69 6f 72 20  .  The interior 
34dff 6e 6f 64 65 73 0a 2a 2a 20 61 74 20 61 20 67 69  nodes.** at a gi
34e00 76 65 6e 20 68 65 69 67 68 74 20 61 72 65 20 6e  ven height are n
34e01 61 74 75 72 61 6c 6c 79 20 74 72 61 63 6b 65 64  aturally tracked
34e02 20 62 79 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64   by interior nod
34e03 65 73 20 61 74 0a 2a 2a 20 68 65 69 67 68 74 2b  es at.** height+
34e04 31 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a  1, and so on..**
34e05 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74  .**.**** Segment
34e06 20 64 69 72 65 63 74 6f 72 79 20 2a 2a 2a 2a 0a   directory ****.
34e07 2a 2a 20 54 68 65 20 73 65 67 6d 65 6e 74 20 64  ** The segment d
34e08 69 72 65 63 74 6f 72 79 20 69 6e 20 74 61 62 6c  irectory in tabl
34e09 65 20 25 5f 73 65 67 64 69 72 20 73 74 6f 72 65  e %_segdir store
34e0a 73 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69  s meta-informati
34e0b 6f 6e 20 66 6f 72 0a 2a 2a 20 6d 65 72 67 69 6e  on for.** mergin
34e0c 67 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 20 73  g and deleting s
34e0d 65 67 6d 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73  egments, and als
34e0e 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20  o the root node 
34e0f 6f 66 20 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e  of the.** segmen
34e10 74 27 73 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20  t's tree..**.** 
34e11 54 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73  The root node is
34e12 20 74 68 65 20 74 6f 70 20 6e 6f 64 65 20 6f 66   the top node of
34e13 20 74 68 65 20 73 65 67 6d 65 6e 74 27 73 20 74   the segment's t
34e14 72 65 65 20 61 66 74 65 72 20 65 6e 63 6f 64 69  ree after encodi
34e15 6e 67 0a 2a 2a 20 74 68 65 20 65 6e 74 69 72 65  ng.** the entire
34e16 20 73 65 67 6d 65 6e 74 2c 20 72 65 73 74 72 69   segment, restri
34e17 63 74 65 64 20 74 6f 20 52 4f 4f 54 5f 4d 41 58  cted to ROOT_MAX
34e18 20 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20   bytes (default 
34e19 31 30 32 34 29 2e 0a 2a 2a 20 54 68 69 73 20 63  1024)..** This c
34e1a 6f 75 6c 64 20 62 65 20 65 69 74 68 65 72 20 61  ould be either a
34e1b 20 6c 65 61 66 20 6e 6f 64 65 20 6f 72 20 61 6e   leaf node or an
34e1c 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20   interior node. 
34e1d 20 49 66 20 74 68 65 20 74 6f 70 0a 2a 2a 20 6e   If the top.** n
34e1e 6f 64 65 20 72 65 71 75 69 72 65 73 20 6d 6f 72  ode requires mor
34e1f 65 20 74 68 61 6e 20 52 4f 4f 54 5f 4d 41 58 20  e than ROOT_MAX 
34e20 62 79 74 65 73 2c 20 69 74 20 69 73 20 66 6c 75  bytes, it is flu
34e21 73 68 65 64 20 74 6f 20 25 5f 73 65 67 6d 65 6e  shed to %_segmen
34e22 74 73 0a 2a 2a 20 61 6e 64 20 61 20 6e 65 77 20  ts.** and a new 
34e23 72 6f 6f 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f  root interior no
34e24 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  de is generated 
34e25 28 77 68 69 63 68 20 73 68 6f 75 6c 64 20 61 6c  (which should al
34e26 77 61 79 73 20 66 69 74 0a 2a 2a 20 77 69 74 68  ways fit.** with
34e27 69 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 65 63 61  in ROOT_MAX beca
34e28 75 73 65 20 69 74 20 6f 6e 6c 79 20 6e 65 65 64  use it only need
34e29 73 20 73 70 61 63 65 20 66 6f 72 20 32 20 76 61  s space for 2 va
34e2a 72 69 6e 74 73 2c 20 74 68 65 0a 2a 2a 20 68 65  rints, the.** he
34e2b 69 67 68 74 20 61 6e 64 20 74 68 65 20 62 6c 6f  ight and the blo
34e2c 63 6b 69 64 20 6f 66 20 74 68 65 20 70 72 65 76  ckid of the prev
34e2d 69 6f 75 73 20 72 6f 6f 74 29 2e 0a 2a 2a 0a 2a  ious root)..**.*
34e2e 2a 20 54 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72  * The meta-infor
34e2f 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 73 65  mation in the se
34e30 67 6d 65 6e 74 20 64 69 72 65 63 74 6f 72 79 20  gment directory 
34e31 69 73 3a 0a 2a 2a 20 20 20 6c 65 76 65 6c 20 20  is:.**   level  
34e32 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 73               - s
34e33 65 67 6d 65 6e 74 20 6c 65 76 65 6c 20 28 73 65  egment level (se
34e34 65 20 62 65 6c 6f 77 29 0a 2a 2a 20 20 20 69 64  e below).**   id
34e35 78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x               
34e36 20 20 2d 20 69 6e 64 65 78 20 77 69 74 68 69 6e    - index within
34e37 20 6c 65 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20   level.**       
34e38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34e39 2d 20 28 6c 65 76 65 6c 2c 69 64 78 20 75 6e 69  - (level,idx uni
34e3a 71 75 65 6c 79 20 69 64 65 6e 74 69 66 79 20 61  quely identify a
34e3b 20 73 65 67 6d 65 6e 74 29 0a 2a 2a 20 20 20 73   segment).**   s
34e3c 74 61 72 74 5f 62 6c 6f 63 6b 20 20 20 20 20 20  tart_block      
34e3d 20 20 20 2d 20 66 69 72 73 74 20 6c 65 61 66 20     - first leaf 
34e3e 6e 6f 64 65 0a 2a 2a 20 20 20 6c 65 61 76 65 73  node.**   leaves
34e3f 5f 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20 2d 20  _end_block    - 
34e40 6c 61 73 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a  last leaf node.*
34e41 2a 20 20 20 65 6e 64 5f 62 6c 6f 63 6b 20 20 20  *   end_block   
34e42 20 20 20 20 20 20 20 20 2d 20 6c 61 73 74 20 62          - last b
34e43 6c 6f 63 6b 20 28 69 6e 63 6c 75 64 69 6e 67 20  lock (including 
34e44 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 29 0a  interior nodes).
34e45 2a 2a 20 20 20 72 6f 6f 74 20 20 20 20 20 20 20  **   root       
34e46 20 20 20 20 20 20 20 20 20 2d 20 63 6f 6e 74 65           - conte
34e47 6e 74 73 20 6f 66 20 72 6f 6f 74 20 6e 6f 64 65  nts of root node
34e48 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f  .**.** If the ro
34e49 6f 74 20 6e 6f 64 65 20 69 73 20 61 20 6c 65 61  ot node is a lea
34e4a 66 20 6e 6f 64 65 2c 20 74 68 65 6e 20 73 74 61  f node, then sta
34e4b 72 74 5f 62 6c 6f 63 6b 2c 0a 2a 2a 20 6c 65 61  rt_block,.** lea
34e4c 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 61  ves_end_block, a
34e4d 6e 64 20 65 6e 64 5f 62 6c 6f 63 6b 20 61 72 65  nd end_block are
34e4e 20 61 6c 6c 20 30 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a   all 0..**.**.**
34e4f 2a 2a 20 53 65 67 6d 65 6e 74 20 6d 65 72 67 69  ** Segment mergi
34e50 6e 67 20 2a 2a 2a 2a 0a 2a 2a 20 54 6f 20 61 6d  ng ****.** To am
34e51 6f 72 74 69 7a 65 20 75 70 64 61 74 65 20 63 6f  ortize update co
34e52 73 74 73 2c 20 73 65 67 6d 65 6e 74 73 20 61 72  sts, segments ar
34e53 65 20 67 72 6f 75 70 65 64 20 69 6e 74 6f 20 6c  e grouped into l
34e54 65 76 65 6c 73 20 61 6e 64 0a 2a 2a 20 6d 65 72  evels and.** mer
34e55 67 65 64 20 69 6e 20 62 61 74 63 68 65 73 2e 20  ged in batches. 
34e56 20 45 61 63 68 20 69 6e 63 72 65 61 73 65 20 69   Each increase i
34e57 6e 20 6c 65 76 65 6c 20 72 65 70 72 65 73 65 6e  n level represen
34e58 74 73 20 65 78 70 6f 6e 65 6e 74 69 61 6c 6c 79  ts exponentially
34e59 0a 2a 2a 20 6d 6f 72 65 20 64 6f 63 75 6d 65 6e  .** more documen
34e5a 74 73 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 6f  ts..**.** New do
34e5b 63 75 6d 65 6e 74 73 20 28 61 63 74 75 61 6c 6c  cuments (actuall
34e5c 79 2c 20 64 6f 63 75 6d 65 6e 74 20 75 70 64 61  y, document upda
34e5d 74 65 73 29 20 61 72 65 20 74 6f 6b 65 6e 69 7a  tes) are tokeniz
34e5e 65 64 20 61 6e 64 0a 2a 2a 20 77 72 69 74 74 65  ed and.** writte
34e5f 6e 20 69 6e 64 69 76 69 64 75 61 6c 6c 79 20 28  n individually (
34e60 75 73 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72  using LeafWriter
34e61 29 20 74 6f 20 61 20 6c 65 76 65 6c 20 30 20 73  ) to a level 0 s
34e62 65 67 6d 65 6e 74 2c 20 77 69 74 68 0a 2a 2a 20  egment, with.** 
34e63 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 69 64 78  incrementing idx
34e64 2e 20 20 57 68 65 6e 20 69 64 78 20 72 65 61 63  .  When idx reac
34e65 68 65 73 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20  hes MERGE_COUNT 
34e66 28 64 65 66 61 75 6c 74 20 31 36 29 2c 20 61 6c  (default 16), al
34e67 6c 0a 2a 2a 20 6c 65 76 65 6c 20 30 20 73 65 67  l.** level 0 seg
34e68 6d 65 6e 74 73 20 61 72 65 20 6d 65 72 67 65 64  ments are merged
34e69 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c   into a single l
34e6a 65 76 65 6c 20 31 20 73 65 67 6d 65 6e 74 2e 20  evel 1 segment. 
34e6b 20 4c 65 76 65 6c 20 31 0a 2a 2a 20 69 73 20 70   Level 1.** is p
34e6c 6f 70 75 6c 61 74 65 64 20 6c 69 6b 65 20 6c 65  opulated like le
34e6d 76 65 6c 20 30 2c 20 61 6e 64 20 65 76 65 6e 74  vel 0, and event
34e6e 75 61 6c 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e  ually MERGE_COUN
34e6f 54 20 6c 65 76 65 6c 20 31 0a 2a 2a 20 73 65 67  T level 1.** seg
34e70 6d 65 6e 74 73 20 61 72 65 20 6d 65 72 67 65 64  ments are merged
34e71 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65 76   to a single lev
34e72 65 6c 20 32 20 73 65 67 6d 65 6e 74 20 28 72 65  el 2 segment (re
34e73 70 72 65 73 65 6e 74 69 6e 67 0a 2a 2a 20 4d 45  presenting.** ME
34e74 52 47 45 5f 43 4f 55 4e 54 5e 32 20 75 70 64 61  RGE_COUNT^2 upda
34e75 74 65 73 29 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e  tes), and so on.
34e76 0a 2a 2a 0a 2a 2a 20 41 20 73 65 67 6d 65 6e 74  .**.** A segment
34e77 20 6d 65 72 67 65 20 74 72 61 76 65 72 73 65 73   merge traverses
34e78 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74   all segments at
34e79 20 61 20 67 69 76 65 6e 20 6c 65 76 65 6c 20 69   a given level i
34e7a 6e 0a 2a 2a 20 70 61 72 61 6c 6c 65 6c 2c 20 70  n.** parallel, p
34e7b 65 72 66 6f 72 6d 69 6e 67 20 61 20 73 74 72 61  erforming a stra
34e7c 69 67 68 74 66 6f 72 77 61 72 64 20 73 6f 72 74  ightforward sort
34e7d 65 64 20 6d 65 72 67 65 2e 20 20 53 69 6e 63 65  ed merge.  Since
34e7e 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 6c 65 61 66   segment.** leaf
34e7f 20 6e 6f 64 65 73 20 61 72 65 20 77 72 69 74 74   nodes are writt
34e80 65 6e 20 69 6e 20 74 6f 20 74 68 65 20 25 5f 73  en in to the %_s
34e81 65 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 69 6e  egments table in
34e82 20 6f 72 64 65 72 2c 20 74 68 69 73 0a 2a 2a 20   order, this.** 
34e83 6d 65 72 67 65 20 74 72 61 76 65 72 73 65 73 20  merge traverses 
34e84 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 73  the underlying s
34e85 71 6c 69 74 65 20 64 69 73 6b 20 73 74 72 75 63  qlite disk struc
34e86 74 75 72 65 73 20 65 66 66 69 63 69 65 6e 74 6c  tures efficientl
34e87 79 2e 0a 2a 2a 20 41 66 74 65 72 20 74 68 65 20  y..** After the 
34e88 6d 65 72 67 65 2c 20 61 6c 6c 20 73 65 67 6d 65  merge, all segme
34e89 6e 74 20 62 6c 6f 63 6b 73 20 66 72 6f 6d 20 74  nt blocks from t
34e8a 68 65 20 6d 65 72 67 65 64 20 6c 65 76 65 6c 20  he merged level 
34e8b 61 72 65 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a  are.** deleted..
34e8c 2a 2a 0a 2a 2a 20 4d 45 52 47 45 5f 43 4f 55 4e  **.** MERGE_COUN
34e8d 54 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6f  T controls how o
34e8e 66 74 65 6e 20 77 65 20 6d 65 72 67 65 20 73 65  ften we merge se
34e8f 67 6d 65 6e 74 73 2e 20 20 31 36 20 73 65 65 6d  gments.  16 seem
34e90 73 20 74 6f 20 62 65 0a 2a 2a 20 73 6f 6d 65 77  s to be.** somew
34e91 68 61 74 20 6f 66 20 61 20 73 77 65 65 74 20 73  hat of a sweet s
34e92 70 6f 74 20 66 6f 72 20 69 6e 73 65 72 74 69 6f  pot for insertio
34e93 6e 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20  n performance.  
34e94 33 32 20 61 6e 64 20 36 34 20 73 68 6f 77 0a 2a  32 and 64 show.*
34e95 2a 20 76 65 72 79 20 73 69 6d 69 6c 61 72 20 70  * very similar p
34e96 65 72 66 6f 72 6d 61 6e 63 65 20 6e 75 6d 62 65  erformance numbe
34e97 72 73 20 74 6f 20 31 36 20 6f 6e 20 69 6e 73 65  rs to 16 on inse
34e98 72 74 69 6f 6e 2c 20 74 68 6f 75 67 68 20 74 68  rtion, though th
34e99 65 79 27 72 65 0a 2a 2a 20 61 20 74 69 6e 79 20  ey're.** a tiny 
34e9a 62 69 74 20 73 6c 6f 77 65 72 20 28 70 65 72 68  bit slower (perh
34e9b 61 70 73 20 64 75 65 20 74 6f 20 6d 6f 72 65 20  aps due to more 
34e9c 6f 76 65 72 68 65 61 64 20 69 6e 20 6d 65 72 67  overhead in merg
34e9d 65 2d 74 69 6d 65 0a 2a 2a 20 73 6f 72 74 69 6e  e-time.** sortin
34e9e 67 29 2e 20 20 38 20 69 73 20 61 62 6f 75 74 20  g).  8 is about 
34e9f 32 30 25 20 73 6c 6f 77 65 72 20 74 68 61 6e 20  20% slower than 
34ea0 31 36 2c 20 34 20 61 62 6f 75 74 20 35 30 25 20  16, 4 about 50% 
34ea1 73 6c 6f 77 65 72 20 74 68 61 6e 0a 2a 2a 20 31  slower than.** 1
34ea2 36 2c 20 32 20 61 62 6f 75 74 20 36 36 25 20 73  6, 2 about 66% s
34ea3 6c 6f 77 65 72 20 74 68 61 6e 20 31 36 2e 0a 2a  lower than 16..*
34ea4 2a 0a 2a 2a 20 41 74 20 71 75 65 72 79 20 74 69  *.** At query ti
34ea5 6d 65 2c 20 68 69 67 68 20 4d 45 52 47 45 5f 43  me, high MERGE_C
34ea6 4f 55 4e 54 20 69 6e 63 72 65 61 73 65 73 20 74  OUNT increases t
34ea7 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67  he number of seg
34ea8 6d 65 6e 74 73 0a 2a 2a 20 77 68 69 63 68 20 6e  ments.** which n
34ea9 65 65 64 20 74 6f 20 62 65 20 73 63 61 6e 6e 65  eed to be scanne
34eaa 64 20 61 6e 64 20 6d 65 72 67 65 64 2e 20 20 46  d and merged.  F
34eab 6f 72 20 69 6e 73 74 61 6e 63 65 2c 20 77 69 74  or instance, wit
34eac 68 20 31 30 30 6b 20 64 6f 63 73 0a 2a 2a 20 69  h 100k docs.** i
34ead 6e 73 65 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  nserted:.**.**  
34eae 20 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 20 20    MERGE_COUNT   
34eaf 73 65 67 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20  segments.**     
34eb0 20 20 31 36 20 20 20 20 20 20 20 20 20 20 20 32    16           2
34eb1 35 0a 2a 2a 20 20 20 20 20 20 20 20 38 20 20 20  5.**        8   
34eb2 20 20 20 20 20 20 20 20 31 32 0a 2a 2a 20 20 20          12.**   
34eb3 20 20 20 20 20 34 20 20 20 20 20 20 20 20 20 20       4          
34eb4 20 31 30 0a 2a 2a 20 20 20 20 20 20 20 20 32 20   10.**        2 
34eb5 20 20 20 20 20 20 20 20 20 20 20 36 0a 2a 2a 0a             6.**.
34eb6 2a 2a 20 54 68 69 73 20 61 70 70 65 61 72 73 20  ** This appears 
34eb7 74 6f 20 68 61 76 65 20 6f 6e 6c 79 20 61 20 6d  to have only a m
34eb8 6f 64 65 72 61 74 65 20 69 6d 70 61 63 74 20 6f  oderate impact o
34eb9 6e 20 71 75 65 72 69 65 73 20 66 6f 72 20 76 65  n queries for ve
34eba 72 79 0a 2a 2a 20 66 72 65 71 75 65 6e 74 20 74  ry.** frequent t
34ebb 65 72 6d 73 20 28 77 68 69 63 68 20 61 72 65 20  erms (which are 
34ebc 73 6f 6d 65 77 68 61 74 20 64 6f 6d 69 6e 61 74  somewhat dominat
34ebd 65 64 20 62 79 20 73 65 67 6d 65 6e 74 20 6d 65  ed by segment me
34ebe 72 67 65 0a 2a 2a 20 63 6f 73 74 73 29 2c 20 61  rge.** costs), a
34ebf 6e 64 20 69 6e 66 72 65 71 75 65 6e 74 20 61 6e  nd infrequent an
34ec0 64 20 6e 6f 6e 2d 65 78 69 73 74 65 6e 74 20 74  d non-existent t
34ec1 65 72 6d 73 20 73 74 69 6c 6c 20 73 65 65 6d 20  erms still seem 
34ec2 74 6f 20 62 65 20 66 61 73 74 0a 2a 2a 20 65 76  to be fast.** ev
34ec3 65 6e 20 77 69 74 68 20 6d 61 6e 79 20 73 65 67  en with many seg
34ec4 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44  ments..**.** TOD
34ec5 4f 28 73 68 65 73 73 29 20 54 68 61 74 20 73 61  O(shess) That sa
34ec6 69 64 2c 20 69 74 20 77 6f 75 6c 64 20 62 65 20  id, it would be 
34ec7 6e 69 63 65 20 74 6f 20 68 61 76 65 20 61 20 62  nice to have a b
34ec8 65 74 74 65 72 20 71 75 65 72 79 2d 73 69 64 65  etter query-side
34ec9 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 66 6f 72  .** argument for
34eca 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6f 66 20   MERGE_COUNT of 
34ecb 31 36 2e 20 20 41 6c 73 6f 2c 20 69 74 20 69 73  16.  Also, it is
34ecc 20 70 6f 73 73 69 62 6c 65 2f 6c 69 6b 65 6c 79   possible/likely
34ecd 20 74 68 61 74 0a 2a 2a 20 6f 70 74 69 6d 69 7a   that.** optimiz
34ece 61 74 69 6f 6e 73 20 74 6f 20 74 68 69 6e 67 73  ations to things
34ecf 20 6c 69 6b 65 20 64 6f 63 6c 69 73 74 20 6d 65   like doclist me
34ed0 72 67 69 6e 67 20 77 69 6c 6c 20 73 77 69 6e 67  rging will swing
34ed1 20 74 68 65 20 73 77 65 65 74 0a 2a 2a 20 73 70   the sweet.** sp
34ed2 6f 74 20 61 72 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a  ot around..**.**
34ed3 0a 2a 2a 0a 2a 2a 2a 2a 20 48 61 6e 64 6c 69 6e  .**.**** Handlin
34ed4 67 20 6f 66 20 64 65 6c 65 74 69 6f 6e 73 20 61  g of deletions a
34ed5 6e 64 20 75 70 64 61 74 65 73 20 2a 2a 2a 2a 0a  nd updates ****.
34ed6 2a 2a 20 53 69 6e 63 65 20 77 65 27 72 65 20 75  ** Since we're u
34ed7 73 69 6e 67 20 61 20 73 65 67 6d 65 6e 74 65 64  sing a segmented
34ed8 20 73 74 72 75 63 74 75 72 65 2c 20 77 69 74 68   structure, with
34ed9 20 6e 6f 20 64 6f 63 69 64 2d 6f 72 69 65 6e 74   no docid-orient
34eda 65 64 0a 2a 2a 20 69 6e 64 65 78 20 69 6e 74 6f  ed.** index into
34edb 20 74 68 65 20 74 65 72 6d 20 69 6e 64 65 78 2c   the term index,
34edc 20 77 65 20 63 6c 65 61 72 6c 79 20 63 61 6e 6e   we clearly cann
34edd 6f 74 20 73 69 6d 70 6c 79 20 75 70 64 61 74 65  ot simply update
34ede 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20 69 6e 64   the term.** ind
34edf 65 78 20 77 68 65 6e 20 61 20 64 6f 63 75 6d 65  ex when a docume
34ee0 6e 74 20 69 73 20 64 65 6c 65 74 65 64 20 6f 72  nt is deleted or
34ee1 20 75 70 64 61 74 65 64 2e 20 20 46 6f 72 20 64   updated.  For d
34ee2 65 6c 65 74 69 6f 6e 73 2c 20 77 65 0a 2a 2a 20  eletions, we.** 
34ee3 77 72 69 74 65 20 61 6e 20 65 6d 70 74 79 20 64  write an empty d
34ee4 6f 63 6c 69 73 74 20 28 76 61 72 69 6e 74 28 64  oclist (varint(d
34ee5 6f 63 69 64 29 20 76 61 72 69 6e 74 28 50 4f 53  ocid) varint(POS
34ee6 5f 45 4e 44 29 29 2c 20 66 6f 72 20 75 70 64 61  _END)), for upda
34ee7 74 65 73 0a 2a 2a 20 77 65 20 73 69 6d 70 6c 79  tes.** we simply
34ee8 20 77 72 69 74 65 20 74 68 65 20 6e 65 77 20 64   write the new d
34ee9 6f 63 6c 69 73 74 2e 20 20 53 65 67 6d 65 6e 74  oclist.  Segment
34eea 20 6d 65 72 67 65 73 20 6f 76 65 72 77 72 69 74   merges overwrit
34eeb 65 20 6f 6c 64 65 72 0a 2a 2a 20 64 61 74 61 20  e older.** data 
34eec 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61 72  for a particular
34eed 20 64 6f 63 69 64 20 77 69 74 68 20 6e 65 77 65   docid with newe
34eee 72 20 64 61 74 61 2c 20 73 6f 20 64 65 6c 65 74  r data, so delet
34eef 65 73 20 6f 72 20 75 70 64 61 74 65 73 0a 2a 2a  es or updates.**
34ef0 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79   will eventually
34ef1 20 6f 76 65 72 74 61 6b 65 20 74 68 65 20 65 61   overtake the ea
34ef2 72 6c 69 65 72 20 64 61 74 61 20 61 6e 64 20 6b  rlier data and k
34ef3 6e 6f 63 6b 20 69 74 20 6f 75 74 2e 20 20 54 68  nock it out.  Th
34ef4 65 0a 2a 2a 20 71 75 65 72 79 20 6c 6f 67 69 63  e.** query logic
34ef5 20 6c 69 6b 65 77 69 73 65 20 6d 65 72 67 65 73   likewise merges
34ef6 20 64 6f 63 6c 69 73 74 73 20 73 6f 20 74 68 61   doclists so tha
34ef7 74 20 6e 65 77 65 72 20 64 61 74 61 20 6b 6e 6f  t newer data kno
34ef8 63 6b 73 20 6f 75 74 0a 2a 2a 20 6f 6c 64 65 72  cks out.** older
34ef9 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44   data..**.** TOD
34efa 4f 28 73 68 65 73 73 29 20 50 72 6f 76 69 64 65  O(shess) Provide
34efb 20 61 20 56 41 43 55 55 4d 20 74 79 70 65 20 6f   a VACUUM type o
34efc 70 65 72 61 74 69 6f 6e 20 74 6f 20 63 6c 65 61  peration to clea
34efd 72 20 6f 75 74 20 61 6c 6c 0a 2a 2a 20 64 65 6c  r out all.** del
34efe 65 74 69 6f 6e 73 20 61 6e 64 20 64 75 70 6c 69  etions and dupli
34eff 63 61 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 77  cations.  This w
34f00 6f 75 6c 64 20 62 61 73 69 63 61 6c 6c 79 20 62  ould basically b
34f01 65 20 61 20 66 6f 72 63 65 64 20 6d 65 72 67 65  e a forced merge
34f02 0a 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c  .** into a singl
34f03 65 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 0a 23  e segment..*/..#
34f04 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
34f05 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
34f06 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
34f07 45 5f 46 54 53 33 29 0a 0a 23 69 66 20 64 65 66  E_FTS3)..#if def
34f08 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
34f09 4c 45 5f 46 54 53 33 29 20 26 26 20 21 64 65 66  LE_FTS3) && !def
34f0a 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
34f0b 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ).# define SQLIT
34f0c 45 5f 43 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a  E_CORE 1.#endif.
34f0d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
34f0e 20 49 6e 63 6c 75 64 65 20 66 74 73 33 49 6e 74   Include fts3Int
34f0f 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
34f10 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a   of fts3.c *****
34f11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
34f12 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
34f13 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33   Begin file fts3
34f14 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
34f15 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f16 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
34f17 0a 2f 2a 0a 2a 2a 20 32 30 30 39 20 4e 6f 76 20  ./*.** 2009 Nov 
34f18 31 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  12.**.** The aut
34f19 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
34f1a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
34f1b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
34f1c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
34f1d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
34f1e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
34f1f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
34f20 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
34f21 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
34f22 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
34f23 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
34f24 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
34f25 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
34f26 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
34f27 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
34f28 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
34f29 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
34f2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f2e 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2f 0a 0a 23 69  ******.**.*/..#i
34f2f 66 6e 64 65 66 20 5f 46 54 53 49 4e 54 5f 48 0a  fndef _FTSINT_H.
34f30 23 64 65 66 69 6e 65 20 5f 46 54 53 49 4e 54 5f  #define _FTSINT_
34f31 48 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  H..#if !defined(
34f32 4e 44 45 42 55 47 29 20 26 26 20 21 64 65 66 69  NDEBUG) && !defi
34f33 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
34f34 29 20 0a 23 20 64 65 66 69 6e 65 20 4e 44 45 42  ) .# define NDEB
34f35 55 47 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  UG 1.#endif../**
34f36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
34f37 6c 75 64 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69  lude fts3_tokeni
34f38 7a 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  zer.h in the mid
34f39 64 6c 65 20 6f 66 20 66 74 73 33 49 6e 74 2e 68  dle of fts3Int.h
34f3a 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a   **********/./**
34f3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
34f3c 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b  in file fts3_tok
34f3d 65 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a  enizer.h *******
34f3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f3f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
34f40 2a 2a 20 32 30 30 36 20 4a 75 6c 79 20 31 30 0a  ** 2006 July 10.
34f41 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
34f42 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
34f43 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
34f44 72 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 2a  rce code..**.***
34f45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f49 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 65  ******.** Define
34f4a 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20  s the interface 
34f4b 74 6f 20 74 6f 6b 65 6e 69 7a 65 72 73 20 75 73  to tokenizers us
34f4c 65 64 20 62 79 20 66 75 6c 6c 74 65 78 74 2d 73  ed by fulltext-s
34f4d 65 61 72 63 68 2e 20 20 54 68 65 72 65 0a 2a 2a  earch.  There.**
34f4e 20 61 72 65 20 74 68 72 65 65 20 62 61 73 69 63   are three basic
34f4f 20 63 6f 6d 70 6f 6e 65 6e 74 73 3a 0a 2a 2a 0a   components:.**.
34f50 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ** sqlite3_token
34f51 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 73 20 61  izer_module is a
34f52 20 73 69 6e 67 6c 65 74 6f 6e 20 64 65 66 69 6e   singleton defin
34f53 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65  ing the tokenize
34f54 72 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66  r.** interface f
34f55 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20  unctions.  This 
34f56 69 73 20 65 73 73 65 6e 74 69 61 6c 6c 79 20 74  is essentially t
34f57 68 65 20 63 6c 61 73 73 20 73 74 72 75 63 74 75  he class structu
34f58 72 65 20 66 6f 72 0a 2a 2a 20 74 6f 6b 65 6e 69  re for.** tokeni
34f59 7a 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  zers..**.** sqli
34f5a 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 69 73  te3_tokenizer is
34f5b 20 75 73 65 64 20 74 6f 20 64 65 66 69 6e 65 20   used to define 
34f5c 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 6f 6b  a particular tok
34f5d 65 6e 69 7a 65 72 2c 20 70 65 72 68 61 70 73 0a  enizer, perhaps.
34f5e 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 73  ** including cus
34f5f 74 6f 6d 69 7a 61 74 69 6f 6e 20 69 6e 66 6f 72  tomization infor
34f60 6d 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 61  mation defined a
34f61 74 20 63 72 65 61 74 69 6f 6e 20 74 69 6d 65 2e  t creation time.
34f62 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74  .**.** sqlite3_t
34f63 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
34f64 69 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  is generated by 
34f65 61 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 67  a tokenizer to g
34f66 65 6e 65 72 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e  enerate.** token
34f67 73 20 66 72 6f 6d 20 61 20 70 61 72 74 69 63 75  s from a particu
34f68 6c 61 72 20 69 6e 70 75 74 2e 0a 2a 2f 0a 23 69  lar input..*/.#i
34f69 66 6e 64 65 66 20 5f 46 54 53 33 5f 54 4f 4b 45  fndef _FTS3_TOKE
34f6a 4e 49 5a 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65  NIZER_H_.#define
34f6b 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45 52   _FTS3_TOKENIZER
34f6c 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  _H_../* TODO(she
34f6d 73 73 29 20 4f 6e 6c 79 20 75 73 65 64 20 66 6f  ss) Only used fo
34f6e 72 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20  r SQLITE_OK and 
34f6f 53 51 4c 49 54 45 5f 44 4f 4e 45 20 61 74 20 74  SQLITE_DONE at t
34f70 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 20 49 66 20  his time..** If 
34f71 74 6f 6b 65 6e 69 7a 65 72 73 20 61 72 65 20 74  tokenizers are t
34f72 6f 20 62 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20  o be allowed to 
34f73 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 2a 28 29  call sqlite3_*()
34f74 20 66 75 6e 63 74 69 6f 6e 73 2c 20 74 68 65 6e   functions, then
34f75 0a 2a 2a 20 77 65 20 77 69 6c 6c 20 6e 65 65 64  .** we will need
34f76 20 61 20 77 61 79 20 74 6f 20 72 65 67 69 73 74   a way to regist
34f77 65 72 20 74 68 65 20 41 50 49 20 63 6f 6e 73 69  er the API consi
34f78 73 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a 0a  stently..*/../*.
34f79 2a 2a 20 53 74 72 75 63 74 75 72 65 73 20 75 73  ** Structures us
34f7a 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69  ed by the tokeni
34f7b 7a 65 72 20 69 6e 74 65 72 66 61 63 65 2e 20 57  zer interface. W
34f7c 68 65 6e 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69  hen a new tokeni
34f7d 7a 65 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  zer.** implement
34f7e 61 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74 65  ation is registe
34f7f 72 65 64 2c 20 74 68 65 20 63 61 6c 6c 65 72 20  red, the caller 
34f80 70 72 6f 76 69 64 65 73 20 61 20 70 6f 69 6e 74  provides a point
34f81 65 72 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c 69  er to.** an sqli
34f82 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
34f83 64 75 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  dule containing 
34f84 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  pointers to the 
34f85 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63  callback.** func
34f86 74 69 6f 6e 73 20 74 68 61 74 20 6d 61 6b 65 20  tions that make 
34f87 75 70 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  up an implementa
34f88 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  tion..**.** When
34f89 20 61 6e 20 66 74 73 33 20 74 61 62 6c 65 20 69   an fts3 table i
34f8a 73 20 63 72 65 61 74 65 64 2c 20 69 74 20 70 61  s created, it pa
34f8b 73 73 65 73 20 61 6e 79 20 61 72 67 75 6d 65 6e  sses any argumen
34f8c 74 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20  ts passed to.** 
34f8d 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c  the tokenizer cl
34f8e 61 75 73 65 20 6f 66 20 74 68 65 20 43 52 45 41  ause of the CREA
34f8f 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
34f90 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74 68   statement to th
34f91 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b  e.** sqlite3_tok
34f92 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78 43  enizer_module.xC
34f93 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e  reate() function
34f94 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65   of the requeste
34f95 64 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69  d tokenizer.** i
34f96 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54  mplementation. T
34f97 68 65 20 78 43 72 65 61 74 65 28 29 20 66 75 6e  he xCreate() fun
34f98 63 74 69 6f 6e 20 69 6e 20 74 75 72 6e 20 72 65  ction in turn re
34f99 74 75 72 6e 73 20 61 6e 20 0a 2a 2a 20 73 71 6c  turns an .** sql
34f9a 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73  ite3_tokenizer s
34f9b 74 72 75 63 74 75 72 65 20 72 65 70 72 65 73 65  tructure represe
34f9c 6e 74 69 6e 67 20 74 68 65 20 73 70 65 63 69 66  nting the specif
34f9d 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 0a  ic tokenizer to.
34f9e 2a 2a 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  ** be used for t
34f9f 68 65 20 66 74 73 33 20 74 61 62 6c 65 20 28 63  he fts3 table (c
34fa0 75 73 74 6f 6d 69 7a 65 64 20 62 79 20 74 68 65  ustomized by the
34fa1 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75 73   tokenizer claus
34fa2 65 20 61 72 67 75 6d 65 6e 74 73 29 2e 0a 2a 2a  e arguments)..**
34fa3 0a 2a 2a 20 54 6f 20 74 6f 6b 65 6e 69 7a 65 20  .** To tokenize 
34fa4 61 6e 20 69 6e 70 75 74 20 62 75 66 66 65 72 2c  an input buffer,
34fa5 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b   the sqlite3_tok
34fa6 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78 4f  enizer_module.xO
34fa7 70 65 6e 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20  pen().** method 
34fa8 69 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 72 65  is called. It re
34fa9 74 75 72 6e 73 20 61 6e 20 73 71 6c 69 74 65 33  turns an sqlite3
34faa 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
34fab 72 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74  r object.** that
34fac 20 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20   may be used to 
34fad 74 6f 6b 65 6e 69 7a 65 20 61 20 73 70 65 63 69  tokenize a speci
34fae 66 69 63 20 69 6e 70 75 74 20 62 75 66 66 65 72  fic input buffer
34faf 20 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65   based on.** the
34fb0 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 72 75   tokenization ru
34fb1 6c 65 73 20 73 75 70 70 6c 69 65 64 20 62 79 20  les supplied by 
34fb2 61 20 73 70 65 63 69 66 69 63 20 73 71 6c 69 74  a specific sqlit
34fb3 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20  e3_tokenizer.** 
34fb4 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65 64  object..*/.typed
34fb5 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
34fb6 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
34fb7 6c 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  le sqlite3_token
34fb8 69 7a 65 72 5f 6d 6f 64 75 6c 65 3b 0a 74 79 70  izer_module;.typ
34fb9 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
34fba 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 71  te3_tokenizer sq
34fbb 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 3b  lite3_tokenizer;
34fbc 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
34fbd 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
34fbe 72 5f 63 75 72 73 6f 72 20 73 71 6c 69 74 65 33  r_cursor sqlite3
34fbf 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
34fc0 72 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74  r;..struct sqlit
34fc1 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
34fc2 75 6c 65 20 7b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ule {..  /*.  **
34fc3 20 53 74 72 75 63 74 75 72 65 20 76 65 72 73 69   Structure versi
34fc4 6f 6e 2e 20 53 68 6f 75 6c 64 20 61 6c 77 61 79  on. Should alway
34fc5 73 20 62 65 20 73 65 74 20 74 6f 20 30 2e 0a 20  s be set to 0.. 
34fc6 20 2a 2f 0a 20 20 69 6e 74 20 69 56 65 72 73 69   */.  int iVersi
34fc7 6f 6e 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43  on;..  /*.  ** C
34fc8 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65  reate a new toke
34fc9 6e 69 7a 65 72 2e 20 54 68 65 20 76 61 6c 75 65  nizer. The value
34fca 73 20 69 6e 20 74 68 65 20 61 72 67 76 5b 5d 20  s in the argv[] 
34fcb 61 72 72 61 79 20 61 72 65 20 74 68 65 0a 20 20  array are the.  
34fcc 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73  ** arguments pas
34fcd 73 65 64 20 74 6f 20 74 68 65 20 22 74 6f 6b 65  sed to the "toke
34fce 6e 69 7a 65 72 22 20 63 6c 61 75 73 65 20 6f 66  nizer" clause of
34fcf 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54   the CREATE VIRT
34fd0 55 41 4c 0a 20 20 2a 2a 20 54 41 42 4c 45 20 73  UAL.  ** TABLE s
34fd1 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63 72  tatement that cr
34fd2 65 61 74 65 64 20 74 68 65 20 66 74 73 33 20 74  eated the fts3 t
34fd3 61 62 6c 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c  able. For exampl
34fd4 65 2c 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 66  e, if.  ** the f
34fd5 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 20 69 73 20  ollowing SQL is 
34fd6 65 78 65 63 75 74 65 64 3a 0a 20 20 2a 2a 0a 20  executed:.  **. 
34fd7 20 2a 2a 20 20 20 43 52 45 41 54 45 20 2e 2e 20   **   CREATE .. 
34fd8 55 53 49 4e 47 20 66 74 73 33 28 20 2e 2e 2e 20  USING fts3( ... 
34fd9 2c 20 74 6f 6b 65 6e 69 7a 65 72 20 3c 74 6f 6b  , tokenizer <tok
34fda 65 6e 69 7a 65 72 2d 6e 61 6d 65 3e 20 61 72 67  enizer-name> arg
34fdb 31 20 61 72 67 32 29 0a 20 20 2a 2a 0a 20 20 2a  1 arg2).  **.  *
34fdc 2a 20 74 68 65 6e 20 61 72 67 63 20 69 73 20 73  * then argc is s
34fdd 65 74 20 74 6f 20 32 2c 20 61 6e 64 20 74 68 65  et to 2, and the
34fde 20 61 72 67 76 5b 5d 20 61 72 72 61 79 20 63 6f   argv[] array co
34fdf 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 0a  ntains pointers.
34fe0 20 20 2a 2a 20 74 6f 20 74 68 65 20 73 74 72 69    ** to the stri
34fe1 6e 67 73 20 22 61 72 67 31 22 20 61 6e 64 20 22  ngs "arg1" and "
34fe2 61 72 67 32 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  arg2"..  **.  **
34fe3 20 54 68 69 73 20 6d 65 74 68 6f 64 20 73 68 6f   This method sho
34fe4 75 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68 65  uld return eithe
34fe5 72 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2c  r SQLITE_OK (0),
34fe6 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72   or an SQLite er
34fe7 72 6f 72 20 0a 20 20 2a 2a 20 63 6f 64 65 2e 20  ror .  ** code. 
34fe8 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  If SQLITE_OK is 
34fe9 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a  returned, then *
34fea 70 70 54 6f 6b 65 6e 69 7a 65 72 20 73 68 6f 75  ppTokenizer shou
34feb 6c 64 20 62 65 20 73 65 74 0a 20 20 2a 2a 20 74  ld be set.  ** t
34fec 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6e  o point at the n
34fed 65 77 6c 79 20 63 72 65 61 74 65 64 20 74 6f 6b  ewly created tok
34fee 65 6e 69 7a 65 72 20 73 74 72 75 63 74 75 72 65  enizer structure
34fef 2e 20 54 68 65 20 67 65 6e 65 72 69 63 0a 20 20  . The generic.  
34ff0 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ** sqlite3_token
34ff1 69 7a 65 72 2e 70 4d 6f 64 75 6c 65 20 76 61 72  izer.pModule var
34ff2 69 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74  iable should not
34ff3 20 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64 20   be initialised 
34ff4 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 6c  by.  ** this cal
34ff5 6c 62 61 63 6b 2e 20 54 68 65 20 63 61 6c 6c 65  lback. The calle
34ff6 72 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 0a 20 20  r will do so..  
34ff7 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 72 65 61  */.  int (*xCrea
34ff8 74 65 29 28 0a 20 20 20 20 69 6e 74 20 61 72 67  te)(.    int arg
34ff9 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
34ffa 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34ffb 53 69 7a 65 20 6f 66 20 61 72 67 76 20 61 72 72  Size of argv arr
34ffc 61 79 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20  ay */.    const 
34ffd 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76  char *const*argv
34ffe 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
34fff 20 54 6f 6b 65 6e 69 7a 65 72 20 61 72 67 75 6d   Tokenizer argum
35000 65 6e 74 20 73 74 72 69 6e 67 73 20 2a 2f 0a 20  ent strings */. 
35001 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e     sqlite3_token
35002 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a  izer **ppTokeniz
35003 65 72 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43  er     /* OUT: C
35004 72 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72  reated tokenizer
35005 20 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20   */.  );..  /*. 
35006 20 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20 65   ** Destroy an e
35007 78 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65  xisting tokenize
35008 72 2e 20 54 68 65 20 66 74 73 33 20 6d 6f 64 75  r. The fts3 modu
35009 6c 65 20 63 61 6c 6c 73 20 74 68 69 73 20 6d 65  le calls this me
3500a 74 68 6f 64 0a 20 20 2a 2a 20 65 78 61 63 74 6c  thod.  ** exactl
3500b 79 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  y once for each 
3500c 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20  successful call 
3500d 74 6f 20 78 43 72 65 61 74 65 28 29 2e 0a 20 20  to xCreate()..  
3500e 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 44 65 73 74  */.  int (*xDest
3500f 72 6f 79 29 28 73 71 6c 69 74 65 33 5f 74 6f 6b  roy)(sqlite3_tok
35010 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
35011 65 72 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  er);..  /*.  ** 
35012 43 72 65 61 74 65 20 61 20 74 6f 6b 65 6e 69 7a  Create a tokeniz
35013 65 72 20 63 75 72 73 6f 72 20 74 6f 20 74 6f 6b  er cursor to tok
35014 65 6e 69 7a 65 20 61 6e 20 69 6e 70 75 74 20 62  enize an input b
35015 75 66 66 65 72 2e 20 54 68 65 20 63 61 6c 6c 65  uffer. The calle
35016 72 0a 20 20 2a 2a 20 69 73 20 72 65 73 70 6f 6e  r.  ** is respon
35017 73 69 62 6c 65 20 66 6f 72 20 65 6e 73 75 72 69  sible for ensuri
35018 6e 67 20 74 68 61 74 20 74 68 65 20 69 6e 70 75  ng that the inpu
35019 74 20 62 75 66 66 65 72 20 72 65 6d 61 69 6e 73  t buffer remains
3501a 20 76 61 6c 69 64 0a 20 20 2a 2a 20 75 6e 74 69   valid.  ** unti
3501b 6c 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  l the cursor is 
3501c 63 6c 6f 73 65 64 20 28 75 73 69 6e 67 20 74 68  closed (using th
3501d 65 20 78 43 6c 6f 73 65 28 29 20 6d 65 74 68 6f  e xClose() metho
3501e 64 29 2e 20 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  d). .  */.  int 
3501f 28 2a 78 4f 70 65 6e 29 28 0a 20 20 20 20 73 71  (*xOpen)(.    sq
35020 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
35021 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20  *pTokenizer,    
35022 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
35023 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 63 6f  object */.    co
35024 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75 74  nst char *pInput
35025 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20  , int nBytes,   
35026 20 20 20 2f 2a 20 49 6e 70 75 74 20 62 75 66 66     /* Input buff
35027 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  er */.    sqlite
35028 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
35029 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 2f  or **ppCursor  /
3502a 2a 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20 74  * OUT: Created t
3502b 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20  okenizer cursor 
3502c 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20  */.  );..  /*.  
3502d 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20 65 78  ** Destroy an ex
3502e 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72  isting tokenizer
3502f 20 63 75 72 73 6f 72 2e 20 54 68 65 20 66 74 73   cursor. The fts
35030 33 20 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20 74  3 module calls t
35031 68 69 73 20 0a 20 20 2a 2a 20 6d 65 74 68 6f 64  his .  ** method
35032 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 66 6f   exactly once fo
35033 72 20 65 61 63 68 20 73 75 63 63 65 73 73 66 75  r each successfu
35034 6c 20 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e 28  l call to xOpen(
35035 29 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  )..  */.  int (*
35036 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f  xClose)(sqlite3_
35037 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
35038 20 2a 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20 2f   *pCursor);..  /
35039 2a 0a 20 20 2a 2a 20 52 65 74 72 69 65 76 65 20  *.  ** Retrieve 
3503a 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66  the next token f
3503b 72 6f 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65  rom the tokenize
3503c 72 20 63 75 72 73 6f 72 20 70 43 75 72 73 6f 72  r cursor pCursor
3503d 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d 65 74 68  . This.  ** meth
3503e 6f 64 20 73 68 6f 75 6c 64 20 65 69 74 68 65 72  od should either
3503f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
35040 4b 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61  K and set the va
35041 6c 75 65 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a  lues of the.  **
35042 20 22 4f 55 54 22 20 76 61 72 69 61 62 6c 65 73   "OUT" variables
35043 20 69 64 65 6e 74 69 66 69 65 64 20 62 65 6c 6f   identified belo
35044 77 2c 20 6f 72 20 53 51 4c 49 54 45 5f 44 4f 4e  w, or SQLITE_DON
35045 45 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68  E to indicate th
35046 61 74 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 20  at.  ** the end 
35047 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 68 61  of the buffer ha
35048 73 20 62 65 65 6e 20 72 65 61 63 68 65 64 2c 20  s been reached, 
35049 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  or an SQLite err
3504a 6f 72 20 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20  or code..  **.  
3504b 2a 2a 20 2a 70 70 54 6f 6b 65 6e 20 73 68 6f 75  ** *ppToken shou
3504c 6c 64 20 62 65 20 73 65 74 20 74 6f 20 70 6f 69  ld be set to poi
3504d 6e 74 20 61 74 20 61 20 62 75 66 66 65 72 20 63  nt at a buffer c
3504e 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 0a 20  ontaining the . 
3504f 20 2a 2a 20 6e 6f 72 6d 61 6c 69 7a 65 64 20 76   ** normalized v
35050 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 6f  ersion of the to
35051 6b 65 6e 20 28 69 2e 65 2e 20 61 66 74 65 72 20  ken (i.e. after 
35052 61 6e 79 20 63 61 73 65 2d 66 6f 6c 64 69 6e 67  any case-folding
35053 20 61 6e 64 2f 6f 72 0a 20 20 2a 2a 20 73 74 65   and/or.  ** ste
35054 6d 6d 69 6e 67 20 68 61 73 20 62 65 65 6e 20 70  mming has been p
35055 65 72 66 6f 72 6d 65 64 29 2e 20 2a 70 6e 42 79  erformed). *pnBy
35056 74 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 65  tes should be se
35057 74 20 74 6f 20 74 68 65 20 6c 65 6e 67 74 68 0a  t to the length.
35058 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 62 75 66    ** of this buf
35059 66 65 72 20 69 6e 20 62 79 74 65 73 2e 20 54 68  fer in bytes. Th
3505a 65 20 69 6e 70 75 74 20 74 65 78 74 20 74 68 61  e input text tha
3505b 74 20 67 65 6e 65 72 61 74 65 64 20 74 68 65 20  t generated the 
3505c 74 6f 6b 65 6e 20 69 73 0a 20 20 2a 2a 20 69 64  token is.  ** id
3505d 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20  entified by the 
3505e 62 79 74 65 20 6f 66 66 73 65 74 73 20 72 65 74  byte offsets ret
3505f 75 72 6e 65 64 20 69 6e 20 2a 70 69 53 74 61 72  urned in *piStar
35060 74 4f 66 66 73 65 74 20 61 6e 64 0a 20 20 2a 2a  tOffset and.  **
35061 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2e 20 2a   *piEndOffset. *
35062 70 69 53 74 61 72 74 4f 66 66 73 65 74 20 73 68  piStartOffset sh
35063 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 74  ould be set to t
35064 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
35065 66 69 72 73 74 0a 20 20 2a 2a 20 62 79 74 65 20  first.  ** byte 
35066 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e 20  of the token in 
35067 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 72  the input buffer
35068 2e 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 73  . *piEndOffset s
35069 68 6f 75 6c 64 20 62 65 20 73 65 74 0a 20 20 2a  hould be set.  *
3506a 2a 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f  * to the index o
3506b 66 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  f the first byte
3506c 20 6a 75 73 74 20 70 61 73 74 20 74 68 65 20 65   just past the e
3506d 6e 64 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  nd of the token 
3506e 69 6e 0a 20 20 2a 2a 20 74 68 65 20 69 6e 70 75  in.  ** the inpu
3506f 74 20 62 75 66 66 65 72 2e 0a 20 20 2a 2a 0a 20  t buffer..  **. 
35070 20 2a 2a 20 54 68 65 20 62 75 66 66 65 72 20 2a   ** The buffer *
35071 70 70 54 6f 6b 65 6e 20 69 73 20 73 65 74 20 74  ppToken is set t
35072 6f 20 70 6f 69 6e 74 20 61 74 20 69 73 20 6d 61  o point at is ma
35073 6e 61 67 65 64 20 62 79 20 74 68 65 20 74 6f 6b  naged by the tok
35074 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6d 70 6c  enizer.  ** impl
35075 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49 74 20 69  ementation. It i
35076 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20  s only required 
35077 74 6f 20 62 65 20 76 61 6c 69 64 20 75 6e 74 69  to be valid unti
35078 6c 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 0a  l the next call.
35079 20 20 2a 2a 20 74 6f 20 78 4e 65 78 74 28 29 20    ** to xNext() 
3507a 6f 72 20 78 43 6c 6f 73 65 28 29 2e 20 0a 20 20  or xClose(). .  
3507b 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  */.  /* TODO(she
3507c 73 73 29 20 63 75 72 72 65 6e 74 20 69 6d 70 6c  ss) current impl
3507d 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 75 69  ementation requi
3507e 72 65 73 20 70 49 6e 70 75 74 20 74 6f 20 62 65  res pInput to be
3507f 0a 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e  .  ** nul-termin
35080 61 74 65 64 2e 20 20 54 68 69 73 20 73 68 6f 75  ated.  This shou
35081 6c 64 20 65 69 74 68 65 72 20 62 65 20 66 69 78  ld either be fix
35082 65 64 2c 20 6f 72 20 70 49 6e 70 75 74 2f 6e 42  ed, or pInput/nB
35083 79 74 65 73 0a 20 20 2a 2a 20 73 68 6f 75 6c 64  ytes.  ** should
35084 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f   be converted to
35085 20 7a 49 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20 20   zInput..  */.  
35086 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 0a 20 20  int (*xNext)(.  
35087 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
35088 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
35089 73 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b 65 6e 69  sor,   /* Tokeni
3508a 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  zer cursor */.  
3508b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
3508c 70 54 6f 6b 65 6e 2c 20 69 6e 74 20 2a 70 6e 42  pToken, int *pnB
3508d 79 74 65 73 2c 20 20 2f 2a 20 4f 55 54 3a 20 4e  ytes,  /* OUT: N
3508e 6f 72 6d 61 6c 69 7a 65 64 20 74 65 78 74 20 66  ormalized text f
3508f 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20  or token */.    
35090 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73  int *piStartOffs
35091 65 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 42 79 74  et,  /* OUT: Byt
35092 65 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65  e offset of toke
35093 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66 65  n in input buffe
35094 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70 69  r */.    int *pi
35095 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 2f 2a  EndOffset,    /*
35096 20 4f 55 54 3a 20 42 79 74 65 20 6f 66 66 73 65   OUT: Byte offse
35097 74 20 6f 66 20 65 6e 64 20 6f 66 20 74 6f 6b 65  t of end of toke
35098 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66 65  n in input buffe
35099 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70 69  r */.    int *pi
3509a 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 2f 2a  Position      /*
3509b 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20   OUT: Number of 
3509c 74 6f 6b 65 6e 73 20 72 65 74 75 72 6e 65 64 20  tokens returned 
3509d 62 65 66 6f 72 65 20 74 68 69 73 20 6f 6e 65 20  before this one 
3509e 2a 2f 0a 20 20 29 3b 0a 7d 3b 0a 0a 73 74 72 75  */.  );.};..stru
3509f 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ct sqlite3_token
350a0 69 7a 65 72 20 7b 0a 20 20 63 6f 6e 73 74 20 73  izer {.  const s
350a1 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
350a2 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
350a3 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65  ;  /* The module
350a4 20 66 6f 72 20 74 68 69 73 20 74 6f 6b 65 6e 69   for this tokeni
350a5 7a 65 72 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b 65  zer */.  /* Toke
350a6 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61  nizer implementa
350a7 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63  tions will typic
350a8 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f  ally add additio
350a9 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b  nal fields */.};
350aa 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ..struct sqlite3
350ab 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
350ac 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  r {.  sqlite3_to
350ad 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
350ae 7a 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 6f  zer;       /* To
350af 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74 68 69 73  kenizer for this
350b0 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 2f 2a   cursor. */.  /*
350b1 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c 65   Tokenizer imple
350b2 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20  mentations will 
350b3 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64  typically add ad
350b4 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20  ditional fields 
350b5 2a 2f 0a 7d 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a  */.};..#endif /*
350b6 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45 52   _FTS3_TOKENIZER
350b7 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
350b8 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
350b9 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 68 20  ts3_tokenizer.h 
350ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350bc 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
350bd 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
350be 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
350bf 20 6f 66 66 20 69 6e 20 66 74 73 33 49 6e 74 2e   off in fts3Int.
350c0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
350c1 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
350c2 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
350c3 66 74 73 33 5f 68 61 73 68 2e 68 20 69 6e 20 74  fts3_hash.h in t
350c4 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 66 74 73  he middle of fts
350c5 33 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  3Int.h *********
350c6 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
350c7 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
350c8 6c 65 20 66 74 73 33 5f 68 61 73 68 2e 68 20 2a  le fts3_hash.h *
350c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350cb 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
350cc 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 32 0a  01 September 22.
350cd 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
350ce 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
350cf 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
350d0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
350d1 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
350d2 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
350d3 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
350d4 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
350d5 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
350d6 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
350d7 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
350d8 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
350d9 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
350da 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
350db 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
350dc 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
350dd 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
350de 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
350df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
350e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
350e3 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 68 65  * This is the he
350e4 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20 74 68  ader file for th
350e5 65 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74  e generic hash-t
350e6 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 61 74 69  able implemenati
350e7 6f 6e 0a 2a 2a 20 75 73 65 64 20 69 6e 20 53 51  on.** used in SQ
350e8 4c 69 74 65 2e 20 20 57 65 27 76 65 20 6d 6f 64  Lite.  We've mod
350e9 69 66 69 65 64 20 69 74 20 73 6c 69 67 68 74 6c  ified it slightl
350ea 79 20 74 6f 20 73 65 72 76 65 20 61 73 20 61 20  y to serve as a 
350eb 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 68 61  standalone.** ha
350ec 73 68 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65  sh table impleme
350ed 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  ntation for the 
350ee 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 69  full-text indexi
350ef 6e 67 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2f  ng module..**.*/
350f0 0a 23 69 66 6e 64 65 66 20 5f 46 54 53 33 5f 48  .#ifndef _FTS3_H
350f1 41 53 48 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  ASH_H_.#define _
350f2 46 54 53 33 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a  FTS3_HASH_H_../*
350f3 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
350f4 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75  tions of structu
350f5 72 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20  res. */.typedef 
350f6 73 74 72 75 63 74 20 46 74 73 33 48 61 73 68 20  struct Fts3Hash 
350f7 46 74 73 33 48 61 73 68 3b 0a 74 79 70 65 64 65  Fts3Hash;.typede
350f8 66 20 73 74 72 75 63 74 20 46 74 73 33 48 61 73  f struct Fts3Has
350f9 68 45 6c 65 6d 20 46 74 73 33 48 61 73 68 45 6c  hElem Fts3HashEl
350fa 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65  em;../* A comple
350fb 74 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73  te hash table is
350fc 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
350fd 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
350fe 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20  ructure..** The 
350ff 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69  internals of thi
35100 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
35101 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f  intended to be o
35102 70 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a  paque -- client.
35103 2a 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e  ** code should n
35104 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63  ot attempt to ac
35105 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20 74  cess or modify t
35106 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69  he fields of thi
35107 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64  s structure.** d
35108 69 72 65 63 74 6c 79 2e 20 20 43 68 61 6e 67 65  irectly.  Change
35109 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
3510a 6f 6e 6c 79 20 62 79 20 75 73 69 6e 67 20 74 68  only by using th
3510b 65 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77  e routines below
3510c 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 6d 61  ..** However, ma
3510d 6e 79 20 6f 66 20 74 68 65 20 22 70 72 6f 63 65  ny of the "proce
3510e 64 75 72 65 73 22 20 61 6e 64 20 22 66 75 6e 63  dures" and "func
3510f 74 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66  tions" for modif
35110 79 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65  ying and.** acce
35111 73 73 69 6e 67 20 74 68 69 73 20 73 74 72 75 63  ssing this struc
35112 74 75 72 65 20 61 72 65 20 72 65 61 6c 6c 79 20  ture are really 
35113 6d 61 63 72 6f 73 2c 20 73 6f 20 77 65 20 63 61  macros, so we ca
35114 6e 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a  n't really make.
35115 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72  ** this structur
35116 65 20 6f 70 61 71 75 65 2e 0a 2a 2f 0a 73 74 72  e opaque..*/.str
35117 75 63 74 20 46 74 73 33 48 61 73 68 20 7b 0a 20  uct Fts3Hash {. 
35118 20 63 68 61 72 20 6b 65 79 43 6c 61 73 73 3b 20   char keyClass; 
35119 20 20 20 20 20 20 20 20 20 2f 2a 20 48 41 53 48           /* HASH
3511a 5f 49 4e 54 2c 20 5f 50 4f 49 4e 54 45 52 2c 20  _INT, _POINTER, 
3511b 5f 53 54 52 49 4e 47 2c 20 5f 42 49 4e 41 52 59  _STRING, _BINARY
3511c 20 2a 2f 0a 20 20 63 68 61 72 20 63 6f 70 79 4b   */.  char copyK
3511d 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ey;           /*
3511e 20 54 72 75 65 20 69 66 20 63 6f 70 79 20 6f 66   True if copy of
3511f 20 6b 65 79 20 6d 61 64 65 20 6f 6e 20 69 6e 73   key made on ins
35120 65 72 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75  ert */.  int cou
35121 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
35122 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
35123 74 72 69 65 73 20 69 6e 20 74 68 69 73 20 74 61  tries in this ta
35124 62 6c 65 20 2a 2f 0a 20 20 46 74 73 33 48 61 73  ble */.  Fts3Has
35125 68 45 6c 65 6d 20 2a 66 69 72 73 74 3b 20 20 20  hElem *first;   
35126 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 65 6c   /* The first el
35127 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 72 72  ement of the arr
35128 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 74 73 69  ay */.  int htsi
35129 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
3512a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 75 63  /* Number of buc
3512b 6b 65 74 73 20 69 6e 20 74 68 65 20 68 61 73 68  kets in the hash
3512c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75   table */.  stru
3512d 63 74 20 5f 66 74 73 33 68 74 20 7b 20 20 20 20  ct _fts3ht {    
3512e 20 20 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20      /* the hash 
3512f 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74  table */.    int
35130 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20   count;         
35131 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
35132 6f 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 20  of entries with 
35133 74 68 69 73 20 68 61 73 68 20 2a 2f 0a 20 20 20  this hash */.   
35134 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 63   Fts3HashElem *c
35135 68 61 69 6e 3b 20 20 20 20 20 2f 2a 20 50 6f 69  hain;     /* Poi
35136 6e 74 65 72 20 74 6f 20 66 69 72 73 74 20 65 6e  nter to first en
35137 74 72 79 20 77 69 74 68 20 74 68 69 73 20 68 61  try with this ha
35138 73 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d  sh */.  } *ht;.}
35139 3b 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65  ;../* Each eleme
3513a 6e 74 20 69 6e 20 74 68 65 20 68 61 73 68 20 74  nt in the hash t
3513b 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61  able is an insta
3513c 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
3513d 77 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 75  wing .** structu
3513e 72 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74  re.  All element
3513f 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20  s are stored on 
35140 61 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d  a single doubly-
35141 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a  linked list..**.
35142 2a 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20 73  ** Again, this s
35143 74 72 75 63 74 75 72 65 20 69 73 20 69 6e 74 65  tructure is inte
35144 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75  nded to be opaqu
35145 65 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74 20  e, but it can't 
35146 72 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61  really.** be opa
35147 71 75 65 20 62 65 63 61 75 73 65 20 69 74 20 69  que because it i
35148 73 20 75 73 65 64 20 62 79 20 6d 61 63 72 6f 73  s used by macros
35149 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 74 73 33  ..*/.struct Fts3
3514a 48 61 73 68 45 6c 65 6d 20 7b 0a 20 20 46 74 73  HashElem {.  Fts
3514b 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78 74 2c  3HashElem *next,
3514c 20 2a 70 72 65 76 3b 20 2f 2a 20 4e 65 78 74 20   *prev; /* Next 
3514d 61 6e 64 20 70 72 65 76 69 6f 75 73 20 65 6c 65  and previous ele
3514e 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 74 61 62  ments in the tab
3514f 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61  le */.  void *da
35150 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
35151 20 20 20 2f 2a 20 44 61 74 61 20 61 73 73 6f 63     /* Data assoc
35152 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
35153 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 76 6f 69  element */.  voi
35154 64 20 2a 70 4b 65 79 3b 20 69 6e 74 20 6e 4b 65  d *pKey; int nKe
35155 79 3b 20 20 20 20 20 20 2f 2a 20 4b 65 79 20 61  y;      /* Key a
35156 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
35157 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 7d  his element */.}
35158 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 72 65 20 61  ;../*.** There a
35159 72 65 20 32 20 64 69 66 66 65 72 65 6e 74 20 6d  re 2 different m
3515a 6f 64 65 73 20 6f 66 20 6f 70 65 72 61 74 69 6f  odes of operatio
3515b 6e 20 66 6f 72 20 61 20 68 61 73 68 20 74 61 62  n for a hash tab
3515c 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 46 54 53 33  le:.**.**   FTS3
3515d 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 20 20 20  _HASH_STRING    
3515e 20 20 20 20 70 4b 65 79 20 70 6f 69 6e 74 73 20      pKey points 
3515f 74 6f 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  to a string that
35160 20 69 73 20 6e 4b 65 79 20 62 79 74 65 73 20 6c   is nKey bytes l
35161 6f 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ong.**          
35162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35163 20 28 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20   (including the 
35164 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2c  null-terminator,
35165 20 69 66 20 61 6e 79 29 2e 20 20 43 61 73 65 0a   if any).  Case.
35166 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
35167 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20               is 
35168 72 65 73 70 65 63 74 65 64 20 69 6e 20 63 6f 6d  respected in com
35169 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  parisons..**.** 
3516a 20 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41    FTS3_HASH_BINA
3516b 52 59 20 20 20 20 20 20 20 20 70 4b 65 79 20 70  RY        pKey p
3516c 6f 69 6e 74 73 20 74 6f 20 62 69 6e 61 72 79 20  oints to binary 
3516d 64 61 74 61 20 6e 4b 65 79 20 62 79 74 65 73 20  data nKey bytes 
3516e 6c 6f 6e 67 2e 20 0a 2a 2a 20 20 20 20 20 20 20  long. .**       
3516f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35170 20 20 20 20 6d 65 6d 63 6d 70 28 29 20 69 73 20      memcmp() is 
35171 75 73 65 64 20 74 6f 20 63 6f 6d 70 61 72 65 20  used to compare 
35172 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 6f  keys..**.** A co
35173 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73  py of the key is
35174 20 6d 61 64 65 20 69 66 20 74 68 65 20 63 6f 70   made if the cop
35175 79 4b 65 79 20 70 61 72 61 6d 65 74 65 72 20 74  yKey parameter t
35176 6f 20 66 74 73 33 48 61 73 68 49 6e 69 74 20 69  o fts3HashInit i
35177 73 20 31 2e 20 20 0a 2a 2f 0a 23 64 65 66 69 6e  s 1.  .*/.#defin
35178 65 20 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49  e FTS3_HASH_STRI
35179 4e 47 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20  NG    1.#define 
3517a 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59  FTS3_HASH_BINARY
3517b 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 63 63      2../*.** Acc
3517c 65 73 73 20 72 6f 75 74 69 6e 65 73 2e 20 20 54  ess routines.  T
3517d 6f 20 64 65 6c 65 74 65 2c 20 69 6e 73 65 72 74  o delete, insert
3517e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e   a NULL pointer.
3517f 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
35180 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
35181 74 73 33 48 61 73 68 49 6e 69 74 28 46 74 73 33  ts3HashInit(Fts3
35182 48 61 73 68 20 2a 70 4e 65 77 2c 20 63 68 61 72  Hash *pNew, char
35183 20 6b 65 79 43 6c 61 73 73 2c 20 63 68 61 72 20   keyClass, char 
35184 63 6f 70 79 4b 65 79 29 3b 0a 53 51 4c 49 54 45  copyKey);.SQLITE
35185 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
35186 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
35187 73 65 72 74 28 46 74 73 33 48 61 73 68 2a 2c 20  sert(Fts3Hash*, 
35188 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
35189 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76 6f 69 64  , int nKey, void
3518a 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c 49 54 45   *pData);.SQLITE
3518b 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
3518c 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46 69  qlite3Fts3HashFi
3518d 6e 64 28 63 6f 6e 73 74 20 46 74 73 33 48 61 73  nd(const Fts3Has
3518e 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  h*, const void *
3518f 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 3b  pKey, int nKey);
35190 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
35191 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33  void sqlite3Fts3
35192 48 61 73 68 43 6c 65 61 72 28 46 74 73 33 48 61  HashClear(Fts3Ha
35193 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 68 6f  sh*);../*.** Sho
35194 72 74 68 61 6e 64 20 66 6f 72 20 74 68 65 20 66  rthand for the f
35195 75 6e 63 74 69 6f 6e 73 20 61 62 6f 76 65 0a 2a  unctions above.*
35196 2f 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  /.#define fts3Ha
35197 73 68 49 6e 69 74 20 20 20 73 71 6c 69 74 65 33  shInit   sqlite3
35198 46 74 73 33 48 61 73 68 49 6e 69 74 0a 23 64 65  Fts3HashInit.#de
35199 66 69 6e 65 20 66 74 73 33 48 61 73 68 49 6e 73  fine fts3HashIns
3519a 65 72 74 20 73 71 6c 69 74 65 33 46 74 73 33 48  ert sqlite3Fts3H
3519b 61 73 68 49 6e 73 65 72 74 0a 23 64 65 66 69 6e  ashInsert.#defin
3519c 65 20 66 74 73 33 48 61 73 68 46 69 6e 64 20 20  e fts3HashFind  
3519d 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
3519e 46 69 6e 64 0a 23 64 65 66 69 6e 65 20 66 74 73  Find.#define fts
3519f 33 48 61 73 68 43 6c 65 61 72 20 20 73 71 6c 69  3HashClear  sqli
351a0 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72  te3Fts3HashClear
351a1 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66  ../*.** Macros f
351a2 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
351a3 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  all elements of 
351a4 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54  a hash table.  T
351a5 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c  he idiom is.** l
351a6 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
351a7 20 20 46 74 73 33 48 61 73 68 20 68 3b 0a 2a 2a    Fts3Hash h;.**
351a8 20 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20     Fts3HashElem 
351a9 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20  *p;.**   ....** 
351aa 20 20 66 6f 72 28 70 3d 66 74 73 33 48 61 73 68    for(p=fts3Hash
351ab 46 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d  First(&h); p; p=
351ac 66 74 73 33 48 61 73 68 4e 65 78 74 28 70 29 29  fts3HashNext(p))
351ad 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53 74 72  {.**     SomeStr
351ae 75 63 74 75 72 65 20 2a 70 44 61 74 61 20 3d 20  ucture *pData = 
351af 66 74 73 33 48 61 73 68 44 61 74 61 28 70 29 3b  fts3HashData(p);
351b0 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f  .**     // do so
351b1 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70 44 61  mething with pDa
351b2 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65  ta.**   }.*/.#de
351b3 66 69 6e 65 20 66 74 73 33 48 61 73 68 46 69 72  fine fts3HashFir
351b4 73 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72  st(H)  ((H)->fir
351b5 73 74 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33  st).#define fts3
351b6 48 61 73 68 4e 65 78 74 28 45 29 20 20 20 28 28  HashNext(E)   ((
351b7 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66 69 6e  E)->next).#defin
351b8 65 20 66 74 73 33 48 61 73 68 44 61 74 61 28 45  e fts3HashData(E
351b9 29 20 20 20 28 28 45 29 2d 3e 64 61 74 61 29 0a  )   ((E)->data).
351ba 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
351bb 4b 65 79 28 45 29 20 20 20 20 28 28 45 29 2d 3e  Key(E)    ((E)->
351bc 70 4b 65 79 29 0a 23 64 65 66 69 6e 65 20 66 74  pKey).#define ft
351bd 73 33 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29  s3HashKeysize(E)
351be 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a   ((E)->nKey)../*
351bf 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e  .** Number of en
351c0 74 72 69 65 73 20 69 6e 20 61 20 68 61 73 68 20  tries in a hash 
351c1 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66 69 6e 65  table.*/.#define
351c2 20 66 74 73 33 48 61 73 68 43 6f 75 6e 74 28 48   fts3HashCount(H
351c3 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 0a  )  ((H)->count).
351c4 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 46 54 53 33  .#endif /* _FTS3
351c5 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a  _HASH_H_ */../**
351c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
351c7 20 6f 66 20 66 74 73 33 5f 68 61 73 68 2e 68 20   of fts3_hash.h 
351c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
351c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
351ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
351cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
351cc 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
351cd 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73   left off in fts
351ce 33 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  3Int.h *********
351cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
351d0 0a 2a 2a 20 54 68 69 73 20 63 6f 6e 73 74 61 6e  .** This constan
351d1 74 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6f  t controls how o
351d2 66 74 65 6e 20 73 65 67 6d 65 6e 74 73 20 61 72  ften segments ar
351d3 65 20 6d 65 72 67 65 64 2e 20 4f 6e 63 65 20 74  e merged. Once t
351d4 68 65 72 65 20 61 72 65 0a 2a 2a 20 46 54 53 33  here are.** FTS3
351d5 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 20 73 65 67  _MERGE_COUNT seg
351d6 6d 65 6e 74 73 20 6f 66 20 6c 65 76 65 6c 20 4e  ments of level N
351d7 2c 20 74 68 65 79 20 61 72 65 20 6d 65 72 67 65  , they are merge
351d8 64 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 0a  d into a single.
351d9 2a 2a 20 73 65 67 6d 65 6e 74 20 6f 66 20 6c 65  ** segment of le
351da 76 65 6c 20 4e 2b 31 2e 0a 2a 2f 0a 23 64 65 66  vel N+1..*/.#def
351db 69 6e 65 20 46 54 53 33 5f 4d 45 52 47 45 5f 43  ine FTS3_MERGE_C
351dc 4f 55 4e 54 20 31 36 0a 0a 2f 2a 0a 2a 2a 20 54  OUNT 16../*.** T
351dd 68 69 73 20 69 73 20 74 68 65 20 6d 61 78 69 6d  his is the maxim
351de 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 64 61 74  um amount of dat
351df 61 20 28 69 6e 20 62 79 74 65 73 29 20 74 6f 20  a (in bytes) to 
351e0 73 74 6f 72 65 20 69 6e 20 74 68 65 20 0a 2a 2a  store in the .**
351e1 20 46 74 73 33 54 61 62 6c 65 2e 70 65 6e 64 69   Fts3Table.pendi
351e2 6e 67 54 65 72 6d 73 20 68 61 73 68 20 74 61 62  ngTerms hash tab
351e3 6c 65 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68  le. Normally, th
351e4 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 0a  e hash table is.
351e5 2a 2a 20 70 6f 70 75 6c 61 74 65 64 20 61 73 20  ** populated as 
351e6 64 6f 63 75 6d 65 6e 74 73 20 61 72 65 20 69 6e  documents are in
351e7 73 65 72 74 65 64 2f 75 70 64 61 74 65 64 2f 64  serted/updated/d
351e8 65 6c 65 74 65 64 20 69 6e 20 61 20 74 72 61 6e  eleted in a tran
351e9 73 61 63 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 75  saction.** and u
351ea 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 20  sed to create a 
351eb 6e 65 77 20 73 65 67 6d 65 6e 74 20 77 68 65 6e  new segment when
351ec 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
351ed 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a   is committed..*
351ee 2a 20 48 6f 77 65 76 65 72 20 69 66 20 74 68 69  * However if thi
351ef 73 20 6c 69 6d 69 74 20 69 73 20 72 65 61 63 68  s limit is reach
351f0 65 64 20 6d 69 64 77 61 79 20 74 68 72 6f 75 67  ed midway throug
351f1 68 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  h a transaction,
351f2 20 61 20 6e 65 77 20 0a 2a 2a 20 73 65 67 6d 65   a new .** segme
351f3 6e 74 20 69 73 20 63 72 65 61 74 65 64 20 61 6e  nt is created an
351f4 64 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  d the hash table
351f5 20 63 6c 65 61 72 65 64 20 69 6d 6d 65 64 69 61   cleared immedia
351f6 74 65 6c 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tely..*/.#define
351f7 20 46 54 53 33 5f 4d 41 58 5f 50 45 4e 44 49 4e   FTS3_MAX_PENDIN
351f8 47 5f 44 41 54 41 20 28 31 2a 31 30 32 34 2a 31  G_DATA (1*1024*1
351f9 30 32 34 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  024)../*.** Macr
351fa 6f 20 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20  o to return the 
351fb 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  number of elemen
351fc 74 73 20 69 6e 20 61 6e 20 61 72 72 61 79 2e 20  ts in an array. 
351fd 53 51 4c 69 74 65 20 68 61 73 20 61 0a 2a 2a 20  SQLite has a.** 
351fe 73 69 6d 69 6c 61 72 20 6d 61 63 72 6f 20 63 61  similar macro ca
351ff 6c 6c 65 64 20 41 72 72 61 79 53 69 7a 65 28 29  lled ArraySize()
35200 2e 20 55 73 65 20 61 20 64 69 66 66 65 72 65 6e  . Use a differen
35201 74 20 6e 61 6d 65 20 74 6f 20 61 76 6f 69 64 0a  t name to avoid.
35202 2a 2a 20 61 20 63 6f 6c 6c 69 73 69 6f 6e 20 77  ** a collision w
35203 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 61 6e 20  hen building an 
35204 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 77 69 74  amalgamation wit
35205 68 20 62 75 69 6c 74 2d 69 6e 20 46 54 53 33 2e  h built-in FTS3.
35206 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 69 7a 65  .*/.#define Size
35207 6f 66 41 72 72 61 79 28 58 29 20 28 28 69 6e 74  ofArray(X) ((int
35208 29 28 73 69 7a 65 6f 66 28 58 29 2f 73 69 7a 65  )(sizeof(X)/size
35209 6f 66 28 58 5b 30 5d 29 29 29 0a 0a 2f 2a 0a 2a  of(X[0])))../*.*
3520a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
3520b 20 6f 66 20 61 20 76 61 72 69 6e 74 20 65 6e 63   of a varint enc
3520c 6f 64 65 64 20 69 6e 74 65 67 65 72 2e 20 54 68  oded integer. Th
3520d 65 20 76 61 72 69 6e 74 20 66 6f 72 6d 61 74 20  e varint format 
3520e 69 73 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20  is different.** 
3520f 66 72 6f 6d 20 74 68 61 74 20 75 73 65 64 20 62  from that used b
35210 79 20 53 51 4c 69 74 65 2c 20 73 6f 20 74 68 65  y SQLite, so the
35211 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   maximum length 
35212 69 73 20 31 30 2c 20 6e 6f 74 20 39 2e 0a 2a 2f  is 10, not 9..*/
35213 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 56 41  .#define FTS3_VA
35214 52 49 4e 54 5f 4d 41 58 20 31 30 0a 0a 2f 2a 0a  RINT_MAX 10../*.
35215 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20  ** This section 
35216 70 72 6f 76 69 64 65 73 20 64 65 66 69 6e 69 74  provides definit
35217 69 6f 6e 73 20 74 6f 20 61 6c 6c 6f 77 20 74 68  ions to allow th
35218 65 0a 2a 2a 20 46 54 53 33 20 65 78 74 65 6e 73  e.** FTS3 extens
35219 69 6f 6e 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c  ion to be compil
3521a 65 64 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68  ed outside of th
3521b 65 20 0a 2a 2a 20 61 6d 61 6c 67 61 6d 61 74 69  e .** amalgamati
3521c 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  on..*/.#ifndef S
3521d 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49  QLITE_AMALGAMATI
3521e 4f 4e 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20  ON./*.** Macros 
3521f 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20  indicating that 
35220 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 65 78 70 72  conditional expr
35221 65 73 73 69 6f 6e 73 20 61 72 65 20 61 6c 77 61  essions are alwa
35222 79 73 20 74 72 75 65 20 6f 72 0a 2a 2a 20 66 61  ys true or.** fa
35223 6c 73 65 2e 0a 2a 2f 0a 23 20 64 65 66 69 6e 65  lse..*/.# define
35224 20 41 4c 57 41 59 53 28 78 29 20 28 78 29 0a 23   ALWAYS(x) (x).#
35225 20 64 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29   define NEVER(X)
35226 20 20 28 78 29 0a 2f 2a 0a 2a 2a 20 49 6e 74 65    (x)./*.** Inte
35227 72 6e 61 6c 20 74 79 70 65 73 20 75 73 65 64 20  rnal types used 
35228 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 74 79  by SQLite..*/.ty
35229 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20 63  pedef unsigned c
3522a 68 61 72 20 75 38 3b 20 20 20 20 20 20 20 20 20  har u8;         
3522b 2f 2a 20 31 2d 62 79 74 65 20 28 6f 72 20 6c 61  /* 1-byte (or la
3522c 72 67 65 72 29 20 75 6e 73 69 67 6e 65 64 20 69  rger) unsigned i
3522d 6e 74 65 67 65 72 20 2a 2f 0a 74 79 70 65 64 65  nteger */.typede
3522e 66 20 73 68 6f 72 74 20 69 6e 74 20 69 31 36 3b  f short int i16;
3522f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32              /* 2
35230 2d 62 79 74 65 20 28 6f 72 20 6c 61 72 67 65 72  -byte (or larger
35231 29 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  ) signed integer
35232 20 2a 2f 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 20   */./*.** Macro 
35233 75 73 65 64 20 74 6f 20 73 75 70 70 72 65 73 73  used to suppress
35234 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e   compiler warnin
35235 67 73 20 66 6f 72 20 75 6e 75 73 65 64 20 70 61  gs for unused pa
35236 72 61 6d 65 74 65 72 73 2e 0a 2a 2f 0a 23 64 65  rameters..*/.#de
35237 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41 52 41  fine UNUSED_PARA
35238 4d 45 54 45 52 28 78 29 20 28 76 6f 69 64 29 28  METER(x) (void)(
35239 78 29 0a 23 65 6e 64 69 66 0a 0a 74 79 70 65 64  x).#endif..typed
3523a 65 66 20 73 74 72 75 63 74 20 46 74 73 33 54 61  ef struct Fts3Ta
3523b 62 6c 65 20 46 74 73 33 54 61 62 6c 65 3b 0a 74  ble Fts3Table;.t
3523c 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46 74  ypedef struct Ft
3523d 73 33 43 75 72 73 6f 72 20 46 74 73 33 43 75 72  s3Cursor Fts3Cur
3523e 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  sor;.typedef str
3523f 75 63 74 20 46 74 73 33 45 78 70 72 20 46 74 73  uct Fts3Expr Fts
35240 33 45 78 70 72 3b 0a 74 79 70 65 64 65 66 20 73  3Expr;.typedef s
35241 74 72 75 63 74 20 46 74 73 33 50 68 72 61 73 65  truct Fts3Phrase
35242 20 46 74 73 33 50 68 72 61 73 65 3b 0a 74 79 70   Fts3Phrase;.typ
35243 65 64 65 66 20 73 74 72 75 63 74 20 46 74 73 33  edef struct Fts3
35244 53 65 67 52 65 61 64 65 72 20 46 74 73 33 53 65  SegReader Fts3Se
35245 67 52 65 61 64 65 72 3b 0a 74 79 70 65 64 65 66  gReader;.typedef
35246 20 73 74 72 75 63 74 20 46 74 73 33 53 65 67 46   struct Fts3SegF
35247 69 6c 74 65 72 20 46 74 73 33 53 65 67 46 69 6c  ilter Fts3SegFil
35248 74 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f  ter;../*.** A co
35249 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 66 75  nnection to a fu
3524a 6c 6c 74 65 78 74 20 69 6e 64 65 78 20 69 73 20  lltext index is 
3524b 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
3524c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20  he following.** 
3524d 73 74 72 75 63 74 75 72 65 2e 20 54 68 65 20 78  structure. The x
3524e 43 72 65 61 74 65 20 61 6e 64 20 78 43 6f 6e 6e  Create and xConn
3524f 65 63 74 20 6d 65 74 68 6f 64 73 20 63 72 65 61  ect methods crea
35250 74 65 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a  te an instance.*
35251 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  * of this struct
35252 75 72 65 20 61 6e 64 20 78 44 65 73 74 72 6f 79  ure and xDestroy
35253 20 61 6e 64 20 78 44 69 73 63 6f 6e 6e 65 63 74   and xDisconnect
35254 20 66 72 65 65 20 74 68 61 74 20 69 6e 73 74 61   free that insta
35255 6e 63 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65  nce..** All othe
35256 72 20 6d 65 74 68 6f 64 73 20 72 65 63 65 69 76  r methods receiv
35257 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  e a pointer to t
35258 68 65 20 73 74 72 75 63 74 75 72 65 20 61 73 20  he structure as 
35259 6f 6e 65 20 6f 66 20 74 68 65 69 72 0a 2a 2a 20  one of their.** 
3525a 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74  arguments..*/.st
3525b 72 75 63 74 20 46 74 73 33 54 61 62 6c 65 20 7b  ruct Fts3Table {
3525c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
3525d 62 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20  base;           
3525e 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73     /* Base class
3525f 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65 20   used by SQLite 
35260 63 6f 72 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  core */.  sqlite
35261 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
35262 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
35263 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
35264 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tion */.  const 
35265 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20  char *zDb;      
35266 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c 6f 67            /* log
35267 69 63 61 6c 20 64 61 74 61 62 61 73 65 20 6e 61  ical database na
35268 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  me */.  const ch
35269 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
3526a 20 20 20 20 20 20 20 20 2f 2a 20 76 69 72 74 75          /* virtu
3526b 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f  al table name */
3526c 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20  .  int nColumn; 
3526d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3526e 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20     /* number of 
3526f 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e 73 20 69 6e  named columns in
35270 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
35271 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c  /.  char **azCol
35272 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
35273 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 61      /* column na
35274 6d 65 73 2e 20 20 6d 61 6c 6c 6f 63 65 64 20 2a  mes.  malloced *
35275 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  /.  sqlite3_toke
35276 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
35277 72 3b 20 20 2f 2a 20 74 6f 6b 65 6e 69 7a 65 72  r;  /* tokenizer
35278 20 66 6f 72 20 69 6e 73 65 72 74 73 20 61 6e 64   for inserts and
35279 20 71 75 65 72 69 65 73 20 2a 2f 0a 0a 20 20 2f   queries */..  /
3527a 2a 20 50 72 65 63 6f 6d 70 69 6c 65 64 20 73 74  * Precompiled st
3527b 61 74 65 6d 65 6e 74 73 20 75 73 65 64 20 62 79  atements used by
3527c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
3527d 69 6f 6e 2e 20 45 61 63 68 20 6f 66 20 74 68 65  ion. Each of the
3527e 73 65 20 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  se .  ** stateme
3527f 6e 74 73 20 69 73 20 72 75 6e 20 61 6e 64 20 72  nts is run and r
35280 65 73 65 74 20 77 69 74 68 69 6e 20 61 20 73 69  eset within a si
35281 6e 67 6c 65 20 76 69 72 74 75 61 6c 20 74 61 62  ngle virtual tab
35282 6c 65 20 41 50 49 20 63 61 6c 6c 2e 20 0a 20 20  le API call. .  
35283 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
35284 74 20 2a 61 53 74 6d 74 5b 31 38 5d 3b 0a 0a 20  t *aStmt[18];.. 
35285 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 73   /* Pointer to s
35286 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67  tring containing
35287 20 74 68 65 20 53 51 4c 3a 0a 20 20 2a 2a 0a 20   the SQL:.  **. 
35288 20 2a 2a 20 22 53 45 4c 45 43 54 20 62 6c 6f 63   ** "SELECT bloc
35289 6b 20 46 52 4f 4d 20 25 5f 73 65 67 6d 65 6e 74  k FROM %_segment
3528a 73 20 57 48 45 52 45 20 62 6c 6f 63 6b 69 64 20  s WHERE blockid 
3528b 42 45 54 57 45 45 4e 20 3f 20 41 4e 44 20 3f 20  BETWEEN ? AND ? 
3528c 0a 20 20 2a 2a 20 20 20 20 4f 52 44 45 52 20 42  .  **    ORDER B
3528d 59 20 62 6c 6f 63 6b 69 64 22 0a 20 20 2a 2f 0a  Y blockid".  */.
3528e 20 20 63 68 61 72 20 2a 7a 53 65 6c 65 63 74 4c    char *zSelectL
3528f 65 61 76 65 73 3b 0a 20 20 69 6e 74 20 6e 4c 65  eaves;.  int nLe
35290 61 76 65 73 53 74 6d 74 3b 20 20 20 20 20 20 20  avesStmt;       
35291 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 69           /* Vali
35292 64 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20  d statements in 
35293 61 4c 65 61 76 65 73 53 74 6d 74 20 2a 2f 0a 20  aLeavesStmt */. 
35294 20 69 6e 74 20 6e 4c 65 61 76 65 73 54 6f 74 61   int nLeavesTota
35295 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
35296 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72   /* Total number
35297 20 6f 66 20 70 72 65 70 61 72 65 64 20 6c 65 61   of prepared lea
35298 76 65 73 20 73 74 6d 74 73 20 2a 2f 0a 20 20 69  ves stmts */.  i
35299 6e 74 20 6e 4c 65 61 76 65 73 41 6c 6c 6f 63 3b  nt nLeavesAlloc;
3529a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3529b 2a 20 41 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65  * Allocated size
3529c 20 6f 66 20 61 4c 65 61 76 65 73 53 74 6d 74 20   of aLeavesStmt 
3529d 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
3529e 74 20 2a 2a 61 4c 65 61 76 65 73 53 74 6d 74 3b  t **aLeavesStmt;
3529f 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
352a0 20 70 72 65 70 61 72 65 64 20 7a 53 65 6c 65 63   prepared zSelec
352a1 74 4c 65 61 76 65 73 20 73 74 6d 74 73 20 2a 2f  tLeaves stmts */
352a2 0a 0a 20 20 69 6e 74 20 6e 4e 6f 64 65 53 69 7a  ..  int nNodeSiz
352a3 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
352a4 20 20 20 20 2f 2a 20 53 6f 66 74 20 6c 69 6d 69      /* Soft limi
352a5 74 20 66 6f 72 20 6e 6f 64 65 20 73 69 7a 65 20  t for node size 
352a6 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c  */..  /* The fol
352a7 6c 6f 77 69 6e 67 20 68 61 73 68 20 74 61 62 6c  lowing hash tabl
352a8 65 20 69 73 20 75 73 65 64 20 74 6f 20 62 75 66  e is used to buf
352a9 66 65 72 20 70 65 6e 64 69 6e 67 20 69 6e 64 65  fer pending inde
352aa 78 20 75 70 64 61 74 65 73 20 64 75 72 69 6e 67  x updates during
352ab 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  .  ** transactio
352ac 6e 73 2e 20 56 61 72 69 61 62 6c 65 20 6e 50 65  ns. Variable nPe
352ad 6e 64 69 6e 67 44 61 74 61 20 65 73 74 69 6d 61  ndingData estima
352ae 74 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20 73  tes the memory s
352af 69 7a 65 20 6f 66 20 74 68 65 20 0a 20 20 2a 2a  ize of the .  **
352b0 20 70 65 6e 64 69 6e 67 20 64 61 74 61 2c 20 69   pending data, i
352b1 6e 63 6c 75 64 69 6e 67 20 68 61 73 68 20 74 61  ncluding hash ta
352b2 62 6c 65 20 6f 76 65 72 68 65 61 64 2c 20 62 75  ble overhead, bu
352b3 74 20 6e 6f 74 20 6d 61 6c 6c 6f 63 20 6f 76 65  t not malloc ove
352b4 72 68 65 61 64 2e 20 0a 20 20 2a 2a 20 57 68 65  rhead. .  ** Whe
352b5 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 65  n nPendingData e
352b6 78 63 65 65 64 73 20 46 54 53 33 5f 4d 41 58 5f  xceeds FTS3_MAX_
352b7 50 45 4e 44 49 4e 47 5f 44 41 54 41 2c 20 74 68  PENDING_DATA, th
352b8 65 20 62 75 66 66 65 72 20 69 73 20 66 6c 75 73  e buffer is flus
352b9 68 65 64 20 0a 20 20 2a 2a 20 61 75 74 6f 6d 61  hed .  ** automa
352ba 74 69 63 61 6c 6c 79 2e 20 56 61 72 69 61 62 6c  tically. Variabl
352bb 65 20 69 50 72 65 76 44 6f 63 69 64 20 69 73 20  e iPrevDocid is 
352bc 74 68 65 20 64 6f 63 69 64 20 6f 66 20 74 68 65  the docid of the
352bd 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 20   most recently. 
352be 20 2a 2a 20 69 6e 73 65 72 74 65 64 20 72 65 63   ** inserted rec
352bf 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ord..  */.  int 
352c0 6e 50 65 6e 64 69 6e 67 44 61 74 61 3b 0a 20 20  nPendingData;.  
352c1 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 50 72  sqlite_int64 iPr
352c2 65 76 44 6f 63 69 64 3b 0a 20 20 46 74 73 33 48  evDocid;.  Fts3H
352c3 61 73 68 20 70 65 6e 64 69 6e 67 54 65 72 6d 73  ash pendingTerms
352c4 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  ;.};../*.** When
352c5 20 74 68 65 20 63 6f 72 65 20 77 61 6e 74 73 20   the core wants 
352c6 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  to read from the
352c7 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c 20   virtual table, 
352c8 69 74 20 63 72 65 61 74 65 73 20 61 0a 2a 2a 20  it creates a.** 
352c9 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75  virtual table cu
352ca 72 73 6f 72 20 28 61 6e 20 69 6e 73 74 61 6e 63  rsor (an instanc
352cb 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
352cc 6e 67 20 73 74 72 75 63 74 75 72 65 29 20 75 73  ng structure) us
352cd 69 6e 67 0a 2a 2a 20 74 68 65 20 78 4f 70 65 6e  ing.** the xOpen
352ce 20 6d 65 74 68 6f 64 2e 20 43 75 72 73 6f 72 73   method. Cursors
352cf 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 20 75   are destroyed u
352d0 73 69 6e 67 20 74 68 65 20 78 43 6c 6f 73 65 20  sing the xClose 
352d1 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 72 75 63  method..*/.struc
352d2 74 20 46 74 73 33 43 75 72 73 6f 72 20 7b 0a 20  t Fts3Cursor {. 
352d3 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
352d4 72 73 6f 72 20 62 61 73 65 3b 20 20 20 20 20 20  rsor base;      
352d5 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73 20 75   /* Base class u
352d6 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 63 6f  sed by SQLite co
352d7 72 65 20 2a 2f 0a 20 20 69 31 36 20 65 53 65 61  re */.  i16 eSea
352d8 72 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  rch;            
352d9 20 20 20 20 20 20 20 20 2f 2a 20 53 65 61 72 63          /* Searc
352da 68 20 73 74 72 61 74 65 67 79 20 28 73 65 65 20  h strategy (see 
352db 62 65 6c 6f 77 29 20 2a 2f 0a 20 20 75 38 20 69  below) */.  u8 i
352dc 73 45 6f 66 3b 20 20 20 20 20 20 20 20 20 20 20  sEof;           
352dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
352de 72 75 65 20 69 66 20 61 74 20 45 6e 64 20 4f 66  rue if at End Of
352df 20 52 65 73 75 6c 74 73 20 2a 2f 0a 20 20 75 38   Results */.  u8
352e0 20 69 73 52 65 71 75 69 72 65 53 65 65 6b 3b 20   isRequireSeek; 
352e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
352e2 20 54 72 75 65 20 69 66 20 6d 75 73 74 20 73 65   True if must se
352e3 65 6b 20 70 53 74 6d 74 20 74 6f 20 25 5f 63 6f  ek pStmt to %_co
352e4 6e 74 65 6e 74 20 72 6f 77 20 2a 2f 0a 20 20 73  ntent row */.  s
352e5 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
352e6 6d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  mt;            /
352e7 2a 20 50 72 65 70 61 72 65 64 20 73 74 61 74 65  * Prepared state
352e8 6d 65 6e 74 20 69 6e 20 75 73 65 20 62 79 20 74  ment in use by t
352e9 68 65 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 46  he cursor */.  F
352ea 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 3b 20  ts3Expr *pExpr; 
352eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
352ec 2a 20 50 61 72 73 65 64 20 4d 41 54 43 48 20 71  * Parsed MATCH q
352ed 75 65 72 79 20 73 74 72 69 6e 67 20 2a 2f 0a 20  uery string */. 
352ee 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
352ef 50 72 65 76 49 64 3b 20 20 20 20 20 20 20 20 20  PrevId;         
352f0 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69 64 20   /* Previous id 
352f1 72 65 61 64 20 66 72 6f 6d 20 61 44 6f 63 6c 69  read from aDocli
352f2 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70 4e  st */.  char *pN
352f3 65 78 74 49 64 3b 20 20 20 20 20 20 20 20 20 20  extId;          
352f4 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
352f5 65 72 20 69 6e 74 6f 20 74 68 65 20 62 6f 64 79  er into the body
352f6 20 6f 66 20 61 44 6f 63 6c 69 73 74 20 2a 2f 0a   of aDoclist */.
352f7 20 20 63 68 61 72 20 2a 61 44 6f 63 6c 69 73 74    char *aDoclist
352f8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
352f9 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 6f 63    /* List of doc
352fa 69 64 73 20 66 6f 72 20 66 75 6c 6c 2d 74 65 78  ids for full-tex
352fb 74 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 69  t queries */.  i
352fc 6e 74 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20  nt nDoclist;    
352fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
352fe 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72  * Size of buffer
352ff 20 61 74 20 61 44 6f 63 6c 69 73 74 20 2a 2f 0a   at aDoclist */.
35300 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 46 74  };../*.** The Ft
35301 73 33 43 75 72 73 6f 72 2e 65 53 65 61 72 63 68  s3Cursor.eSearch
35302 20 6d 65 6d 62 65 72 20 69 73 20 61 6c 77 61 79   member is alway
35303 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6f 66 20  s set to one of 
35304 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 2e 0a 2a  the following..*
35305 2a 20 41 63 74 75 61 6c 79 2c 20 46 74 73 33 43  * Actualy, Fts3C
35306 75 72 73 6f 72 2e 65 53 65 61 72 63 68 20 63 61  ursor.eSearch ca
35307 6e 20 62 65 20 67 72 65 61 74 65 72 20 74 68 61  n be greater tha
35308 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a  n or equal to.**
35309 20 46 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f 53   FTS3_FULLTEXT_S
3530a 45 41 52 43 48 2e 20 20 49 66 20 73 6f 2c 20 74  EARCH.  If so, t
3530b 68 65 6e 20 46 74 73 33 43 75 72 73 6f 72 2e 65  hen Fts3Cursor.e
3530c 53 65 61 72 63 68 20 2d 20 32 20 69 73 20 74 68  Search - 2 is th
3530d 65 20 69 6e 64 65 78 0a 2a 2a 20 6f 66 20 74 68  e index.** of th
3530e 65 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 73  e column to be s
3530f 65 61 72 63 68 65 64 2e 20 20 46 6f 72 20 65 78  earched.  For ex
35310 61 6d 70 6c 65 2c 20 69 6e 0a 2a 2a 0a 2a 2a 20  ample, in.**.** 
35311 20 20 20 20 43 52 45 41 54 45 20 56 49 52 54 55      CREATE VIRTU
35312 41 4c 20 54 41 42 4c 45 20 65 78 31 20 55 53 49  AL TABLE ex1 USI
35313 4e 47 20 66 74 73 33 28 61 2c 62 2c 63 2c 64 29  NG fts3(a,b,c,d)
35314 3b 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  ;.**     SELECT 
35315 64 6f 63 69 64 20 46 52 4f 4d 20 65 78 31 20 57  docid FROM ex1 W
35316 48 45 52 45 20 62 20 4d 41 54 43 48 20 27 6f 6e  HERE b MATCH 'on
35317 65 20 74 77 6f 20 74 68 72 65 65 27 3b 0a 2a 2a  e two three';.**
35318 20 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65   .** Because the
35319 20 4c 48 53 20 6f 66 20 74 68 65 20 4d 41 54 43   LHS of the MATC
3531a 48 20 6f 70 65 72 61 74 6f 72 20 69 73 20 32 6e  H operator is 2n
3531b 64 20 63 6f 6c 75 6d 6e 20 22 62 22 2c 0a 2a 2a  d column "b",.**
3531c 20 46 74 73 33 43 75 72 73 6f 72 2e 65 53 65 61   Fts3Cursor.eSea
3531d 72 63 68 20 77 69 6c 6c 20 62 65 20 73 65 74 20  rch will be set 
3531e 74 6f 20 46 54 53 33 5f 46 55 4c 4c 54 45 58 54  to FTS3_FULLTEXT
3531f 5f 53 45 41 52 43 48 2b 31 2e 20 20 28 2b 30 20  _SEARCH+1.  (+0 
35320 66 6f 72 20 61 2c 0a 2a 2a 20 2b 31 20 66 6f 72  for a,.** +1 for
35321 20 62 2c 20 2b 32 20 66 6f 72 20 63 2c 20 2b 33   b, +2 for c, +3
35322 20 66 6f 72 20 64 2e 29 20 20 49 66 20 74 68 65   for d.)  If the
35323 20 4c 48 53 20 6f 66 20 4d 41 54 43 48 20 77 65   LHS of MATCH we
35324 72 65 20 22 65 78 31 22 20 0a 2a 2a 20 69 6e 64  re "ex1" .** ind
35325 69 63 61 74 69 6e 67 20 74 68 61 74 20 61 6c 6c  icating that all
35326 20 63 6f 6c 75 6d 6e 73 20 73 68 6f 75 6c 64 20   columns should 
35327 62 65 20 73 65 61 72 63 68 65 64 2c 0a 2a 2a 20  be searched,.** 
35328 74 68 65 6e 20 65 53 65 61 72 63 68 20 77 6f 75  then eSearch wou
35329 6c 64 20 62 65 20 73 65 74 20 74 6f 20 46 54 53  ld be set to FTS
3532a 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45 41 52 43  3_FULLTEXT_SEARC
3532b 48 2b 34 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  H+4..*/.#define 
3532c 46 54 53 33 5f 46 55 4c 4c 53 43 41 4e 5f 53 45  FTS3_FULLSCAN_SE
3532d 41 52 43 48 20 30 20 20 20 20 2f 2a 20 4c 69 6e  ARCH 0    /* Lin
3532e 65 61 72 20 73 63 61 6e 20 6f 66 20 25 5f 63 6f  ear scan of %_co
3532f 6e 74 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 23  ntent table */.#
35330 64 65 66 69 6e 65 20 46 54 53 33 5f 44 4f 43 49  define FTS3_DOCI
35331 44 5f 53 45 41 52 43 48 20 20 20 20 31 20 20 20  D_SEARCH    1   
35332 20 2f 2a 20 4c 6f 6f 6b 75 70 20 62 79 20 72 6f   /* Lookup by ro
35333 77 69 64 20 6f 6e 20 25 5f 63 6f 6e 74 65 6e 74  wid on %_content
35334 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e   table */.#defin
35335 65 20 46 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f  e FTS3_FULLTEXT_
35336 53 45 41 52 43 48 20 32 20 20 20 20 2f 2a 20 46  SEARCH 2    /* F
35337 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 20 73  ull-text index s
35338 65 61 72 63 68 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  earch */../*.** 
35339 41 20 22 70 68 72 61 73 65 22 20 69 73 20 61 20  A "phrase" is a 
3533a 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20  sequence of one 
3533b 6f 72 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 20 74  or more tokens t
3533c 68 61 74 20 6d 75 73 74 20 6d 61 74 63 68 20 69  hat must match i
3533d 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 2e 20 20  n.** sequence.  
3533e 41 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e 20 69  A single token i
3533f 73 20 74 68 65 20 62 61 73 65 20 63 61 73 65 20  s the base case 
35340 61 6e 64 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d  and the most com
35341 6d 6f 6e 20 63 61 73 65 2e 0a 2a 2a 20 46 6f 72  mon case..** For
35342 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20 74   a sequence of t
35343 6f 6b 65 6e 73 20 63 6f 6e 74 61 69 6e 65 64 20  okens contained 
35344 69 6e 20 22 2e 2e 2e 22 2c 20 6e 54 6f 6b 65 6e  in "...", nToken
35345 20 77 69 6c 6c 20 62 65 20 74 68 65 20 6e 75 6d   will be the num
35346 62 65 72 0a 2a 2a 20 6f 66 20 74 6f 6b 65 6e 73  ber.** of tokens
35347 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 2e 0a   in the string..
35348 2a 2f 0a 73 74 72 75 63 74 20 46 74 73 33 50 68  */.struct Fts3Ph
35349 72 61 73 65 20 7b 0a 20 20 69 6e 74 20 6e 54 6f  rase {.  int nTo
3534a 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ken;            
3534b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
3534c 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 70   tokens in the p
3534d 68 72 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  hrase */.  int i
3534e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20  Column;         
3534f 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
35350 66 20 63 6f 6c 75 6d 6e 20 74 68 69 73 20 70 68  f column this ph
35351 72 61 73 65 20 6d 75 73 74 20 6d 61 74 63 68 20  rase must match 
35352 2a 2f 0a 20 20 69 6e 74 20 69 73 4e 6f 74 3b 20  */.  int isNot; 
35353 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35354 2f 2a 20 50 68 72 61 73 65 20 70 72 65 66 69 78  /* Phrase prefix
35355 65 64 20 62 79 20 75 6e 61 72 79 20 6e 6f 74 20  ed by unary not 
35356 28 2d 29 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a  (-) operator */.
35357 20 20 73 74 72 75 63 74 20 50 68 72 61 73 65 54    struct PhraseT
35358 6f 6b 65 6e 20 7b 0a 20 20 20 20 63 68 61 72 20  oken {.    char 
35359 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *z;             
3535a 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74      /* Text of t
3535b 68 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20  he token */.    
3535c 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20  int n;          
3535d 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
3535e 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 62  er of bytes in b
3535f 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f  uffer pointed to
35360 20 62 79 20 7a 20 2a 2f 0a 20 20 20 20 69 6e 74   by z */.    int
35361 20 69 73 50 72 65 66 69 78 3b 20 20 20 20 20 20   isPrefix;      
35362 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
35363 20 74 6f 6b 65 6e 20 65 6e 64 73 20 69 6e 20 77   token ends in w
35364 69 74 68 20 61 20 22 2a 22 20 63 68 61 72 61 63  ith a "*" charac
35365 74 65 72 20 2a 2f 0a 20 20 7d 20 61 54 6f 6b 65  ter */.  } aToke
35366 6e 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20 20  n[1];           
35367 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79      /* One entry
35368 20 66 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20   for each token 
35369 69 6e 20 74 68 65 20 70 68 72 61 73 65 20 2a 2f  in the phrase */
3536a 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 65  .};../*.** A tre
3536b 65 20 6f 66 20 74 68 65 73 65 20 6f 62 6a 65 63  e of these objec
3536c 74 73 20 66 6f 72 6d 73 20 74 68 65 20 52 48 53  ts forms the RHS
3536d 20 6f 66 20 61 20 4d 41 54 43 48 20 6f 70 65 72   of a MATCH oper
3536e 61 74 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ator..*/.struct 
3536f 46 74 73 33 45 78 70 72 20 7b 0a 20 20 69 6e 74  Fts3Expr {.  int
35370 20 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 20   eType;         
35371 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
35372 66 20 74 68 65 20 46 54 53 51 55 45 52 59 5f 58  f the FTSQUERY_X
35373 58 58 20 76 61 6c 75 65 73 20 64 65 66 69 6e 65  XX values define
35374 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74  d below */.  int
35375 20 6e 4e 65 61 72 3b 20 20 20 20 20 20 20 20 20   nNear;         
35376 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64          /* Valid
35377 20 69 66 20 65 54 79 70 65 3d 3d 46 54 53 51 55   if eType==FTSQU
35378 45 52 59 5f 4e 45 41 52 20 2a 2f 0a 20 20 46 74  ERY_NEAR */.  Ft
35379 73 33 45 78 70 72 20 2a 70 50 61 72 65 6e 74 3b  s3Expr *pParent;
3537a 20 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 72           /* pPar
3537b 65 6e 74 2d 3e 70 4c 65 66 74 3d 3d 74 68 69 73  ent->pLeft==this
3537c 20 6f 72 20 70 50 61 72 65 6e 74 2d 3e 70 52 69   or pParent->pRi
3537d 67 68 74 3d 3d 74 68 69 73 20 2a 2f 0a 20 20 46  ght==this */.  F
3537e 74 73 33 45 78 70 72 20 2a 70 4c 65 66 74 3b 20  ts3Expr *pLeft; 
3537f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66            /* Lef
35380 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 46  t operand */.  F
35381 74 73 33 45 78 70 72 20 2a 70 52 69 67 68 74 3b  ts3Expr *pRight;
35382 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67            /* Rig
35383 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ht operand */.  
35384 46 74 73 33 50 68 72 61 73 65 20 2a 70 50 68 72  Fts3Phrase *pPhr
35385 61 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 56 61  ase;       /* Va
35386 6c 69 64 20 69 66 20 65 54 79 70 65 3d 3d 46 54  lid if eType==FT
35387 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 2a 2f  SQUERY_PHRASE */
35388 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6e 64 69  .};../*.** Candi
35389 64 61 74 65 20 76 61 6c 75 65 73 20 66 6f 72 20  date values for 
3538a 46 74 73 33 51 75 65 72 79 2e 65 54 79 70 65 2e  Fts3Query.eType.
3538b 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 6f   Note that the o
3538c 72 64 65 72 20 6f 66 20 74 68 65 20 66 69 72 73  rder of the firs
3538d 74 0a 2a 2a 20 66 6f 75 72 20 76 61 6c 75 65 73  t.** four values
3538e 20 69 73 20 69 6e 20 6f 72 64 65 72 20 6f 66 20   is in order of 
3538f 70 72 65 63 65 64 65 6e 63 65 20 77 68 65 6e 20  precedence when 
35390 70 61 72 73 69 6e 67 20 65 78 70 72 65 73 73 69  parsing expressi
35391 6f 6e 73 2e 20 46 6f 72 20 0a 2a 2a 20 65 78 61  ons. For .** exa
35392 6d 70 6c 65 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  mple, the follow
35393 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 61 20  ing:.**.**   "a 
35394 4f 52 20 62 20 41 4e 44 20 63 20 4e 4f 54 20 64  OR b AND c NOT d
35395 20 4e 45 41 52 20 65 22 0a 2a 2a 0a 2a 2a 20 69   NEAR e".**.** i
35396 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a  s equivalent to:
35397 0a 2a 2a 0a 2a 2a 20 20 20 22 61 20 4f 52 20 28  .**.**   "a OR (
35398 62 20 41 4e 44 20 28 63 20 4e 4f 54 20 28 64 20  b AND (c NOT (d 
35399 4e 45 41 52 20 65 29 29 29 22 0a 2a 2f 0a 23 64  NEAR e)))".*/.#d
3539a 65 66 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4e  efine FTSQUERY_N
3539b 45 41 52 20 20 20 31 0a 23 64 65 66 69 6e 65 20  EAR   1.#define 
3539c 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 20 20 20  FTSQUERY_NOT    
3539d 32 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45  2.#define FTSQUE
3539e 52 59 5f 41 4e 44 20 20 20 20 33 0a 23 64 65 66  RY_AND    3.#def
3539f 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4f 52 20  ine FTSQUERY_OR 
353a0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 46 54      4.#define FT
353a1 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 35 0a  SQUERY_PHRASE 5.
353a2 0a 0a 2f 2a 20 66 74 73 33 5f 69 6e 69 74 2e 63  ../* fts3_init.c
353a3 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
353a4 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
353a5 73 33 44 65 6c 65 74 65 56 74 61 62 28 69 6e 74  s3DeleteVtab(int
353a6 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  , sqlite3_vtab *
353a7 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
353a8 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
353a9 33 49 6e 69 74 56 74 61 62 28 69 6e 74 2c 20 73  3InitVtab(int, s
353aa 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 2c 20  qlite3*, void*, 
353ab 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  int, const char*
353ac 63 6f 6e 73 74 2a 2c 20 0a 20 20 20 20 20 20 20  const*, .       
353ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
353ae 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a   sqlite3_vtab **
353af 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 0a 2f 2a 20  , char **);../* 
353b0 66 74 73 33 5f 77 72 69 74 65 2e 63 20 2a 2f 0a  fts3_write.c */.
353b1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
353b2 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 55 70  nt sqlite3Fts3Up
353b3 64 61 74 65 4d 65 74 68 6f 64 28 73 71 6c 69 74  dateMethod(sqlit
353b4 65 33 5f 76 74 61 62 2a 2c 69 6e 74 2c 73 71 6c  e3_vtab*,int,sql
353b5 69 74 65 33 5f 76 61 6c 75 65 2a 2a 2c 73 71 6c  ite3_value**,sql
353b6 69 74 65 33 5f 69 6e 74 36 34 2a 29 3b 0a 53 51  ite3_int64*);.SQ
353b7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
353b8 20 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64   sqlite3Fts3Pend
353b9 69 6e 67 54 65 72 6d 73 46 6c 75 73 68 28 46 74  ingTermsFlush(Ft
353ba 73 33 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49  s3Table *);.SQLI
353bb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
353bc 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69  sqlite3Fts3Pendi
353bd 6e 67 54 65 72 6d 73 43 6c 65 61 72 28 46 74 73  ngTermsClear(Fts
353be 33 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54  3Table *);.SQLIT
353bf 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
353c0 6c 69 74 65 33 46 74 73 33 4f 70 74 69 6d 69 7a  lite3Fts3Optimiz
353c1 65 28 46 74 73 33 54 61 62 6c 65 20 2a 29 3b 0a  e(Fts3Table *);.
353c2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
353c3 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 53 65  nt sqlite3Fts3Se
353c4 67 52 65 61 64 65 72 4e 65 77 28 46 74 73 33 54  gReaderNew(Fts3T
353c5 61 62 6c 65 20 2a 2c 69 6e 74 2c 20 73 71 6c 69  able *,int, sqli
353c6 74 65 33 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c  te3_int64,.  sql
353c7 69 74 65 33 5f 69 6e 74 36 34 2c 20 73 71 6c 69  ite3_int64, sqli
353c8 74 65 33 5f 69 6e 74 36 34 2c 20 63 6f 6e 73 74  te3_int64, const
353c9 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 46 74   char *, int, Ft
353ca 73 33 53 65 67 52 65 61 64 65 72 2a 2a 29 3b 0a  s3SegReader**);.
353cb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
353cc 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
353cd 65 67 52 65 61 64 65 72 46 72 65 65 28 46 74 73  egReaderFree(Fts
353ce 33 54 61 62 6c 65 20 2a 2c 20 46 74 73 33 53 65  3Table *, Fts3Se
353cf 67 52 65 61 64 65 72 20 2a 29 3b 0a 53 51 4c 49  gReader *);.SQLI
353d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
353d1 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
353d2 64 65 72 49 74 65 72 61 74 65 28 0a 20 20 46 74  derIterate(.  Ft
353d3 73 33 54 61 62 6c 65 20 2a 2c 20 46 74 73 33 53  s3Table *, Fts3S
353d4 65 67 52 65 61 64 65 72 20 2a 2a 2c 20 69 6e 74  egReader **, int
353d5 2c 20 46 74 73 33 53 65 67 46 69 6c 74 65 72 20  , Fts3SegFilter 
353d6 2a 2c 0a 20 20 69 6e 74 20 28 2a 29 28 46 74 73  *,.  int (*)(Fts
353d7 33 54 61 62 6c 65 20 2a 2c 20 76 6f 69 64 20 2a  3Table *, void *
353d8 2c 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 63  , char *, int, c
353d9 68 61 72 20 2a 2c 20 69 6e 74 29 2c 20 20 76 6f  har *, int),  vo
353da 69 64 20 2a 0a 29 3b 0a 53 51 4c 49 54 45 5f 50  id *.);.SQLITE_P
353db 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
353dc 65 33 46 74 73 33 52 65 61 64 42 6c 6f 63 6b 28  e3Fts3ReadBlock(
353dd 46 74 73 33 54 61 62 6c 65 2a 2c 20 73 71 6c 69  Fts3Table*, sqli
353de 74 65 33 5f 69 6e 74 36 34 2c 20 63 68 61 72 20  te3_int64, char 
353df 63 6f 6e 73 74 2a 2a 2c 20 69 6e 74 2a 29 3b 0a  const**, int*);.
353e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
353e1 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 41 6c  nt sqlite3Fts3Al
353e2 6c 53 65 67 64 69 72 73 28 46 74 73 33 54 61 62  lSegdirs(Fts3Tab
353e3 6c 65 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d  le*, sqlite3_stm
353e4 74 20 2a 2a 29 3b 0a 0a 2f 2a 20 46 6c 61 67 73  t **);../* Flags
353e5 20 61 6c 6c 6f 77 65 64 20 61 73 20 70 61 72 74   allowed as part
353e6 20 6f 66 20 74 68 65 20 34 74 68 20 61 72 67 75   of the 4th argu
353e7 6d 65 6e 74 20 74 6f 20 53 65 67 6d 65 6e 74 52  ment to SegmentR
353e8 65 61 64 65 72 49 74 65 72 61 74 65 28 29 20 2a  eaderIterate() *
353e9 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f 53  /.#define FTS3_S
353ea 45 47 4d 45 4e 54 5f 52 45 51 55 49 52 45 5f 50  EGMENT_REQUIRE_P
353eb 4f 53 20 20 20 30 78 30 30 30 30 30 30 30 31 0a  OS   0x00000001.
353ec 23 64 65 66 69 6e 65 20 46 54 53 33 5f 53 45 47  #define FTS3_SEG
353ed 4d 45 4e 54 5f 49 47 4e 4f 52 45 5f 45 4d 50 54  MENT_IGNORE_EMPT
353ee 59 20 20 30 78 30 30 30 30 30 30 30 32 0a 23 64  Y  0x00000002.#d
353ef 65 66 69 6e 65 20 46 54 53 33 5f 53 45 47 4d 45  efine FTS3_SEGME
353f0 4e 54 5f 43 4f 4c 55 4d 4e 5f 46 49 4c 54 45 52  NT_COLUMN_FILTER
353f1 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 65 66   0x00000004.#def
353f2 69 6e 65 20 46 54 53 33 5f 53 45 47 4d 45 4e 54  ine FTS3_SEGMENT
353f3 5f 50 52 45 46 49 58 20 20 20 20 20 20 20 20 30  _PREFIX        0
353f4 78 30 30 30 30 30 30 30 38 0a 0a 2f 2a 20 54 79  x00000008../* Ty
353f5 70 65 20 70 61 73 73 65 64 20 61 73 20 34 74 68  pe passed as 4th
353f6 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 53 65 67   argument to Seg
353f7 6d 65 6e 74 52 65 61 64 65 72 49 74 65 72 61 74  mentReaderIterat
353f8 65 28 29 20 2a 2f 0a 73 74 72 75 63 74 20 46 74  e() */.struct Ft
353f9 73 33 53 65 67 46 69 6c 74 65 72 20 7b 0a 20 20  s3SegFilter {.  
353fa 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72  const char *zTer
353fb 6d 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0a  m;.  int nTerm;.
353fc 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20 69 6e    int iCol;.  in
353fd 74 20 66 6c 61 67 73 3b 0a 7d 3b 0a 0a 2f 2a 20  t flags;.};../* 
353fe 66 74 73 33 2e 63 20 2a 2f 0a 53 51 4c 49 54 45  fts3.c */.SQLITE
353ff 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
35400 69 74 65 33 46 74 73 33 50 75 74 56 61 72 69 6e  ite3Fts3PutVarin
35401 74 28 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65  t(char *, sqlite
35402 33 5f 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45  3_int64);.SQLITE
35403 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
35404 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69 6e  ite3Fts3GetVarin
35405 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  t(const char *, 
35406 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 29 3b  sqlite_int64 *);
35407 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
35408 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 47  int sqlite3Fts3G
35409 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74  etVarint32(const
3540a 20 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29 3b   char *, int *);
3540b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3540c 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 56  int sqlite3Fts3V
3540d 61 72 69 6e 74 4c 65 6e 28 73 71 6c 69 74 65 33  arintLen(sqlite3
3540e 5f 75 69 6e 74 36 34 29 3b 0a 53 51 4c 49 54 45  _uint64);.SQLITE
3540f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
35410 6c 69 74 65 33 46 74 73 33 44 65 71 75 6f 74 65  lite3Fts3Dequote
35411 28 63 68 61 72 20 2a 29 3b 0a 0a 2f 2a 20 66 74  (char *);../* ft
35412 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a  s3_tokenizer.c *
35413 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
35414 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
35415 69 74 65 33 46 74 73 33 4e 65 78 74 54 6f 6b 65  ite3Fts3NextToke
35416 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  n(const char *, 
35417 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  int *);.SQLITE_P
35418 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
35419 65 33 46 74 73 33 49 6e 69 74 48 61 73 68 54 61  e3Fts3InitHashTa
3541a 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 46  ble(sqlite3 *, F
3541b 74 73 33 48 61 73 68 20 2a 2c 20 63 6f 6e 73 74  ts3Hash *, const
3541c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54 45   char *);.SQLITE
3541d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3541e 69 74 65 33 46 74 73 33 49 6e 69 74 54 6f 6b 65  ite3Fts3InitToke
3541f 6e 69 7a 65 72 28 46 74 73 33 48 61 73 68 20 2a  nizer(Fts3Hash *
35420 70 48 61 73 68 2c 20 0a 20 20 63 6f 6e 73 74 20  pHash, .  const 
35421 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f  char *, sqlite3_
35422 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 2c 20 63 6f  tokenizer **, co
35423 6e 73 74 20 63 68 61 72 20 2a 2a 2c 20 63 68 61  nst char **, cha
35424 72 20 2a 2a 0a 29 3b 0a 0a 2f 2a 20 66 74 73 33  r **.);../* fts3
35425 5f 73 6e 69 70 70 65 74 2e 63 20 2a 2f 0a 53 51  _snippet.c */.SQ
35426 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
35427 64 20 73 71 6c 69 74 65 33 46 74 73 33 4f 66 66  d sqlite3Fts3Off
35428 73 65 74 73 28 73 71 6c 69 74 65 33 5f 63 6f 6e  sets(sqlite3_con
35429 74 65 78 74 2a 2c 20 46 74 73 33 43 75 72 73 6f  text*, Fts3Curso
3542a 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
3542b 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
3542c 46 74 73 33 53 6e 69 70 70 65 74 28 73 71 6c 69  Fts3Snippet(sqli
3542d 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 46 74  te3_context*, Ft
3542e 73 33 43 75 72 73 6f 72 2a 2c 20 0a 20 20 63 6f  s3Cursor*, .  co
3542f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73  nst char *, cons
35430 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20  t char *, const 
35431 63 68 61 72 20 2a 0a 29 3b 0a 0a 2f 2a 20 66 74  char *.);../* ft
35432 73 33 5f 65 78 70 72 2e 63 20 2a 2f 0a 53 51 4c  s3_expr.c */.SQL
35433 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
35434 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 50  sqlite3Fts3ExprP
35435 61 72 73 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  arse(sqlite3_tok
35436 65 6e 69 7a 65 72 20 2a 2c 20 0a 20 20 63 68 61  enizer *, .  cha
35437 72 20 2a 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  r **, int, int, 
35438 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e  const char *, in
35439 74 2c 20 46 74 73 33 45 78 70 72 20 2a 2a 0a 29  t, Fts3Expr **.)
3543a 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
3543b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
3543c 33 45 78 70 72 46 72 65 65 28 46 74 73 33 45 78  3ExprFree(Fts3Ex
3543d 70 72 20 2a 29 3b 0a 23 69 66 64 65 66 20 53 51  pr *);.#ifdef SQ
3543e 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
3543f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
35440 6c 69 74 65 33 46 74 73 33 45 78 70 72 49 6e 69  lite3Fts3ExprIni
35441 74 54 65 73 74 49 6e 74 65 72 66 61 63 65 28 73  tTestInterface(s
35442 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 6e  qlite3 *db);.#en
35443 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f  dif..#endif /* _
35444 46 54 53 49 4e 54 5f 48 20 2a 2f 0a 0a 2f 2a 2a  FTSINT_H */../**
35445 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
35446 20 6f 66 20 66 74 73 33 49 6e 74 2e 68 20 2a 2a   of fts3Int.h **
35447 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35448 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35449 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
3544a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
3544b 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
3544c 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 66 74 73   left off in fts
3544d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  3.c ************
3544e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 23  ***********/...#
3544f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  ifndef SQLITE_CO
35450 52 45 20 0a 20 20 53 51 4c 49 54 45 5f 45 58 54  RE .  SQLITE_EXT
35451 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e  ENSION_INIT1.#en
35452 64 69 66 0a 0a 0a 0a 2f 2a 20 54 4f 44 4f 28 73  dif..../* TODO(s
35453 68 65 73 73 29 20 4d 41 4e 2c 20 74 68 69 73 20  hess) MAN, this 
35454 74 68 69 6e 67 20 6e 65 65 64 73 20 73 6f 6d 65  thing needs some
35455 20 72 65 66 61 63 74 6f 72 69 6e 67 2e 20 20 41   refactoring.  A
35456 74 20 6d 69 6e 69 6d 75 6d 2c 20 69 74 0a 2a 2a  t minimum, it.**
35457 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65 20 74   would be nice t
35458 6f 20 6f 72 64 65 72 20 74 68 65 20 66 69 6c 65  o order the file
35459 20 62 65 74 74 65 72 2c 20 70 65 72 68 61 70 73   better, perhaps
3545a 20 73 6f 6d 65 74 68 69 6e 67 20 61 6c 6f 6e 67   something along
3545b 20 74 68 65 0a 2a 2a 20 6c 69 6e 65 73 20 6f 66   the.** lines of
3545c 3a 0a 2a 2a 0a 2a 2a 20 20 2d 20 75 74 69 6c 69  :.**.**  - utili
3545d 74 79 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20  ty functions.** 
3545e 20 2d 20 74 61 62 6c 65 20 73 65 74 75 70 20 66   - table setup f
3545f 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74  unctions.**  - t
35460 61 62 6c 65 20 75 70 64 61 74 65 20 66 75 6e 63  able update func
35461 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c  tions.**  - tabl
35462 65 20 71 75 65 72 79 20 66 75 6e 63 74 69 6f 6e  e query function
35463 73 0a 2a 2a 0a 2a 2a 20 50 75 74 20 74 68 65 20  s.**.** Put the 
35464 71 75 65 72 79 20 66 75 6e 63 74 69 6f 6e 73 20  query functions 
35465 6c 61 73 74 20 62 65 63 61 75 73 65 20 74 68 65  last because the
35466 79 27 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 72  y're likely to r
35467 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 79 70 65  eference.** type
35468 64 65 66 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e  defs or function
35469 73 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65  s from the table
3546a 20 75 70 64 61 74 65 20 73 65 63 74 69 6f 6e 2e   update section.
3546b 0a 2a 2f 0a 0a 23 69 66 20 30 0a 23 20 64 65 66  .*/..#if 0.# def
3546c 69 6e 65 20 46 54 53 54 52 41 43 45 28 41 29 20  ine FTSTRACE(A) 
3546d 20 70 72 69 6e 74 66 20 41 3b 20 66 66 6c 75 73   printf A; fflus
3546e 68 28 73 74 64 6f 75 74 29 0a 23 65 6c 73 65 0a  h(stdout).#else.
3546f 23 20 64 65 66 69 6e 65 20 46 54 53 54 52 41 43  # define FTSTRAC
35470 45 28 41 29 0a 23 65 6e 64 69 66 0a 0a 74 79 70  E(A).#endif..typ
35471 65 64 65 66 20 65 6e 75 6d 20 44 6f 63 4c 69 73  edef enum DocLis
35472 74 54 79 70 65 20 7b 0a 20 20 44 4c 5f 44 4f 43  tType {.  DL_DOC
35473 49 44 53 2c 20 20 20 20 20 20 20 20 20 20 20 20  IDS,            
35474 20 20 2f 2a 20 64 6f 63 69 64 73 20 6f 6e 6c 79    /* docids only
35475 20 2a 2f 0a 20 20 44 4c 5f 50 4f 53 49 54 49 4f   */.  DL_POSITIO
35476 4e 53 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  NS,           /*
35477 20 64 6f 63 69 64 73 20 2b 20 70 6f 73 69 74 69   docids + positi
35478 6f 6e 73 20 2a 2f 0a 20 20 44 4c 5f 50 4f 53 49  ons */.  DL_POSI
35479 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 20 20  TIONS_OFFSETS   
3547a 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20 70 6f 73   /* docids + pos
3547b 69 74 69 6f 6e 73 20 2b 20 6f 66 66 73 65 74 73  itions + offsets
3547c 20 2a 2f 0a 7d 20 44 6f 63 4c 69 73 74 54 79 70   */.} DocListTyp
3547d 65 3b 0a 0a 2f 2a 0a 2a 2a 20 42 79 20 64 65 66  e;../*.** By def
3547e 61 75 6c 74 2c 20 6f 6e 6c 79 20 70 6f 73 69 74  ault, only posit
3547f 69 6f 6e 73 20 61 6e 64 20 6e 6f 74 20 6f 66 66  ions and not off
35480 73 65 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  sets are stored 
35481 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 73 2e  in the doclists.
35482 0a 2a 2a 20 54 6f 20 63 68 61 6e 67 65 20 74 68  .** To change th
35483 69 73 20 73 6f 20 74 68 61 74 20 6f 66 66 73 65  is so that offse
35484 74 73 20 61 72 65 20 73 74 6f 72 65 64 20 74 6f  ts are stored to
35485 6f 2c 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 0a  o, compile with.
35486 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2d  **.**          -
35487 44 44 4c 5f 44 45 46 41 55 4c 54 3d 44 4c 5f 50  DDL_DEFAULT=DL_P
35488 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53  OSITIONS_OFFSETS
35489 0a 2a 2a 0a 2a 2a 20 49 66 20 44 4c 5f 44 45 46  .**.** If DL_DEF
3548a 41 55 4c 54 20 69 73 20 73 65 74 20 74 6f 20 44  AULT is set to D
3548b 4c 5f 44 4f 43 49 44 53 2c 20 79 6f 75 72 20 74  L_DOCIDS, your t
3548c 61 62 6c 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65  able can only be
3548d 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 69 6e 74   inserted.** int
3548e 6f 20 28 6e 6f 20 64 65 6c 65 74 65 73 20 6f 72  o (no deletes or
3548f 20 75 70 64 61 74 65 73 29 2e 0a 2a 2f 0a 23 69   updates)..*/.#i
35490 66 6e 64 65 66 20 44 4c 5f 44 45 46 41 55 4c 54  fndef DL_DEFAULT
35491 0a 23 20 64 65 66 69 6e 65 20 44 4c 5f 44 45 46  .# define DL_DEF
35492 41 55 4c 54 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  AULT DL_POSITION
35493 53 0a 23 65 6e 64 69 66 0a 0a 65 6e 75 6d 20 7b  S.#endif..enum {
35494 0a 20 20 50 4f 53 5f 45 4e 44 20 3d 20 30 2c 20  .  POS_END = 0, 
35495 20 20 20 20 20 20 20 2f 2a 20 65 6e 64 20 6f 66         /* end of
35496 20 74 68 69 73 20 70 6f 73 69 74 69 6f 6e 20 6c   this position l
35497 69 73 74 20 2a 2f 0a 20 20 50 4f 53 5f 43 4f 4c  ist */.  POS_COL
35498 55 4d 4e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  UMN,         /* 
35499 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 65 77 20  followed by new 
3549a 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f  column number */
3549b 0a 20 20 50 4f 53 5f 42 41 53 45 0a 7d 3b 0a 0a  .  POS_BASE.};..
3549c 2f 2a 20 75 74 69 6c 69 74 79 20 66 75 6e 63 74  /* utility funct
3549d 69 6f 6e 73 20 2a 2f 0a 0a 2f 2a 20 43 4c 45 41  ions */../* CLEA
3549e 52 28 29 20 61 6e 64 20 53 43 52 41 4d 42 4c 45  R() and SCRAMBLE
3549f 28 29 20 61 62 73 74 72 61 63 74 20 6d 65 6d 73  () abstract mems
354a0 65 74 28 29 20 6f 6e 20 61 20 70 6f 69 6e 74 65  et() on a pointe
354a1 72 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a  r to a single.**
354a2 20 72 65 63 6f 72 64 20 74 6f 20 70 72 65 76 65   record to preve
354a3 6e 74 20 65 72 72 6f 72 73 20 6f 66 20 74 68 65  nt errors of the
354a4 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 6d 79 5f   form:.**.** my_
354a5 66 75 6e 63 74 69 6f 6e 28 53 6f 6d 65 54 79 70  function(SomeTyp
354a6 65 20 2a 62 29 7b 0a 2a 2a 20 20 20 6d 65 6d 73  e *b){.**   mems
354a7 65 74 28 62 2c 20 27 5c 30 27 2c 20 73 69 7a 65  et(b, '\0', size
354a8 6f 66 28 62 29 29 3b 20 20 2f 2f 20 73 69 7a 65  of(b));  // size
354a9 6f 66 28 62 29 21 3d 73 69 7a 65 6f 66 28 2a 62  of(b)!=sizeof(*b
354aa 29 0a 2a 2a 20 7d 0a 2a 2f 0a 2f 2a 20 54 4f 44  ).** }.*/./* TOD
354ab 4f 28 73 68 65 73 73 29 20 4f 62 76 69 6f 75 73  O(shess) Obvious
354ac 20 63 61 6e 64 69 64 61 74 65 73 20 66 6f 72 20   candidates for 
354ad 61 20 68 65 61 64 65 72 20 66 69 6c 65 2e 20 2a  a header file. *
354ae 2f 0a 23 64 65 66 69 6e 65 20 43 4c 45 41 52 28  /.#define CLEAR(
354af 62 29 20 6d 65 6d 73 65 74 28 62 2c 20 27 5c 30  b) memset(b, '\0
354b0 27 2c 20 73 69 7a 65 6f 66 28 2a 28 62 29 29 29  ', sizeof(*(b)))
354b1 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
354b2 0a 23 20 20 64 65 66 69 6e 65 20 53 43 52 41 4d  .#  define SCRAM
354b3 42 4c 45 28 62 29 20 6d 65 6d 73 65 74 28 62 2c  BLE(b) memset(b,
354b4 20 30 78 35 35 2c 20 73 69 7a 65 6f 66 28 2a 28   0x55, sizeof(*(
354b5 62 29 29 29 0a 23 65 6c 73 65 0a 23 20 20 64 65  b))).#else.#  de
354b6 66 69 6e 65 20 53 43 52 41 4d 42 4c 45 28 62 29  fine SCRAMBLE(b)
354b7 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20  .#endif../* .** 
354b8 57 72 69 74 65 20 61 20 36 34 2d 62 69 74 20 76  Write a 64-bit v
354b9 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
354ba 6e 74 65 67 65 72 20 74 6f 20 6d 65 6d 6f 72 79  nteger to memory
354bb 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 5b 30   starting at p[0
354bc 5d 2e 0a 2a 2a 20 54 68 65 20 6c 65 6e 67 74 68  ]..** The length
354bd 20 6f 66 20 64 61 74 61 20 77 72 69 74 74 65 6e   of data written
354be 20 77 69 6c 6c 20 62 65 20 62 65 74 77 65 65 6e   will be between
354bf 20 31 20 61 6e 64 20 46 54 53 33 5f 56 41 52 49   1 and FTS3_VARI
354c0 4e 54 5f 4d 41 58 20 62 79 74 65 73 2e 0a 2a 2a  NT_MAX bytes..**
354c1 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   The number of b
354c2 79 74 65 73 20 77 72 69 74 74 65 6e 20 69 73 20  ytes written is 
354c3 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
354c4 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
354c5 73 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61  sqlite3Fts3PutVa
354c6 72 69 6e 74 28 63 68 61 72 20 2a 70 2c 20 73 71  rint(char *p, sq
354c7 6c 69 74 65 5f 69 6e 74 36 34 20 76 29 7b 0a 20  lite_int64 v){. 
354c8 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
354c9 71 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  q = (unsigned ch
354ca 61 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69 74  ar *) p;.  sqlit
354cb 65 5f 75 69 6e 74 36 34 20 76 75 20 3d 20 76 3b  e_uint64 vu = v;
354cc 0a 20 20 64 6f 7b 0a 20 20 20 20 2a 71 2b 2b 20  .  do{.    *q++ 
354cd 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
354ce 29 20 28 28 76 75 20 26 20 30 78 37 66 29 20 7c  ) ((vu & 0x7f) |
354cf 20 30 78 38 30 29 3b 0a 20 20 20 20 76 75 20 3e   0x80);.    vu >
354d0 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20  >= 7;.  }while( 
354d1 76 75 21 3d 30 20 29 3b 0a 20 20 71 5b 2d 31 5d  vu!=0 );.  q[-1]
354d2 20 26 3d 20 30 78 37 66 3b 20 20 2f 2a 20 74 75   &= 0x7f;  /* tu
354d3 72 6e 20 6f 66 66 20 68 69 67 68 20 62 69 74 20  rn off high bit 
354d4 69 6e 20 66 69 6e 61 6c 20 62 79 74 65 20 2a 2f  in final byte */
354d5 0a 20 20 61 73 73 65 72 74 28 20 71 20 2d 20 28  .  assert( q - (
354d6 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
354d7 70 20 3c 3d 20 46 54 53 33 5f 56 41 52 49 4e 54  p <= FTS3_VARINT
354d8 5f 4d 41 58 20 29 3b 0a 20 20 72 65 74 75 72 6e  _MAX );.  return
354d9 20 28 69 6e 74 29 20 28 71 20 2d 20 28 75 6e 73   (int) (q - (uns
354da 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 3b  igned char *)p);
354db 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 61 64 20  .}../* .** Read 
354dc 61 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c  a 64-bit variabl
354dd 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
354de 20 66 72 6f 6d 20 6d 65 6d 6f 72 79 20 73 74 61   from memory sta
354df 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a  rting at p[0]..*
354e0 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
354e1 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61  ber of bytes rea
354e2 64 2c 20 6f 72 20 30 20 6f 6e 20 65 72 72 6f 72  d, or 0 on error
354e3 2e 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69  ..** The value i
354e4 73 20 73 74 6f 72 65 64 20 69 6e 20 2a 76 2e 0a  s stored in *v..
354e5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
354e6 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
354e7 33 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74  3GetVarint(const
354e8 20 63 68 61 72 20 2a 70 2c 20 73 71 6c 69 74 65   char *p, sqlite
354e9 5f 69 6e 74 36 34 20 2a 76 29 7b 0a 20 20 63 6f  _int64 *v){.  co
354ea 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
354eb 72 20 2a 71 20 3d 20 28 63 6f 6e 73 74 20 75 6e  r *q = (const un
354ec 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 20 70  signed char *) p
354ed 3b 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  ;.  sqlite_uint6
354ee 34 20 78 20 3d 20 30 2c 20 79 20 3d 20 31 3b 0a  4 x = 0, y = 1;.
354ef 20 20 77 68 69 6c 65 28 20 28 2a 71 20 26 20 30    while( (*q & 0
354f0 78 38 30 29 20 3d 3d 20 30 78 38 30 20 29 7b 0a  x80) == 0x80 ){.
354f1 20 20 20 20 78 20 2b 3d 20 79 20 2a 20 28 2a 71      x += y * (*q
354f2 2b 2b 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20  ++ & 0x7f);.    
354f3 79 20 3c 3c 3d 20 37 3b 0a 20 20 20 20 69 66 28  y <<= 7;.    if(
354f4 20 71 20 2d 20 28 75 6e 73 69 67 6e 65 64 20 63   q - (unsigned c
354f5 68 61 72 20 2a 29 70 20 3e 3d 20 46 54 53 33 5f  har *)p >= FTS3_
354f6 56 41 52 49 4e 54 5f 4d 41 58 20 29 7b 20 20 2f  VARINT_MAX ){  /
354f7 2a 20 62 61 64 20 64 61 74 61 20 2a 2f 0a 20 20  * bad data */.  
354f8 20 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b      assert( 0 );
354f9 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
354fa 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 78 20 2b  .    }.  }.  x +
354fb 3d 20 79 20 2a 20 28 2a 71 2b 2b 29 3b 0a 20 20  = y * (*q++);.  
354fc 2a 76 20 3d 20 28 73 71 6c 69 74 65 5f 69 6e 74  *v = (sqlite_int
354fd 36 34 29 20 78 3b 0a 20 20 72 65 74 75 72 6e 20  64) x;.  return 
354fe 28 69 6e 74 29 20 28 71 20 2d 20 28 75 6e 73 69  (int) (q - (unsi
354ff 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29 3b 0a  gned char *)p);.
35500 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 6d 69 6c 61 72  }../*.** Similar
35501 20 74 6f 20 73 71 6c 69 74 65 33 46 74 73 33 47   to sqlite3Fts3G
35502 65 74 56 61 72 69 6e 74 28 29 2c 20 65 78 63 65  etVarint(), exce
35503 70 74 20 74 68 61 74 20 74 68 65 20 6f 75 74 70  pt that the outp
35504 75 74 20 69 73 20 74 72 75 6e 63 61 74 65 64 20  ut is truncated 
35505 74 6f 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 69  to a.** 32-bit i
35506 6e 74 65 67 65 72 20 62 65 66 6f 72 65 20 69 74  nteger before it
35507 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
35508 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
35509 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 47  int sqlite3Fts3G
3550a 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74  etVarint32(const
3550b 20 63 68 61 72 20 2a 70 2c 20 69 6e 74 20 2a 70   char *p, int *p
3550c 69 29 7b 0a 20 73 71 6c 69 74 65 5f 69 6e 74 36  i){. sqlite_int6
3550d 34 20 69 3b 0a 20 69 6e 74 20 72 65 74 20 3d 20  4 i;. int ret = 
3550e 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61  sqlite3Fts3GetVa
3550f 72 69 6e 74 28 70 2c 20 26 69 29 3b 0a 20 2a 70  rint(p, &i);. *p
35510 69 20 3d 20 28 69 6e 74 29 20 69 3b 0a 20 61 73  i = (int) i;. as
35511 73 65 72 74 28 20 2a 70 69 3d 3d 69 20 29 3b 0a  sert( *pi==i );.
35512 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a   return ret;.}..
35513 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
35514 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
35515 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
35516 72 65 20 74 68 65 20 76 61 6c 75 65 20 70 61 73  re the value pas
35517 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 69  sed as the.** fi
35518 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69 6e 20  rst argument in 
35519 76 61 72 69 6e 74 20 66 6f 72 6d 2e 0a 2a 2f 0a  varint form..*/.
3551a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3551b 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33 56 61  nt sqlite3Fts3Va
3551c 72 69 6e 74 4c 65 6e 28 73 71 6c 69 74 65 33 5f  rintLen(sqlite3_
3551d 75 69 6e 74 36 34 20 76 29 7b 0a 20 20 69 6e 74  uint64 v){.  int
3551e 20 69 20 3d 20 30 3b 0a 20 20 64 6f 7b 0a 20 20   i = 0;.  do{.  
3551f 20 20 69 2b 2b 3b 0a 20 20 20 20 76 20 3e 3e 3d    i++;.    v >>=
35520 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20 76 21   7;.  }while( v!
35521 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  =0 );.  return i
35522 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
35523 72 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20  rt an SQL-style 
35524 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69 6e  quoted string in
35525 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69  to a normal stri
35526 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a  ng by removing.*
35527 2a 20 74 68 65 20 71 75 6f 74 65 20 63 68 61 72  * the quote char
35528 61 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f 6e  acters.  The con
35529 76 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20  version is done 
3552a 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74 68  in-place.  If th
3552b 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73 20  e.** input does 
3552c 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61  not begin with a
3552d 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72   quote character
3552e 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
3552f 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f  ine.** is a no-o
35530 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65  p..**.** Example
35531 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 61 62  s:.**.**     "ab
35532 63 22 20 20 20 62 65 63 6f 6d 65 73 20 20 20 61  c"   becomes   a
35533 62 63 0a 2a 2a 20 20 20 20 20 27 78 79 7a 27 20  bc.**     'xyz' 
35534 20 20 62 65 63 6f 6d 65 73 20 20 20 78 79 7a 0a    becomes   xyz.
35535 2a 2a 20 20 20 20 20 5b 70 71 72 5d 20 20 20 62  **     [pqr]   b
35536 65 63 6f 6d 65 73 20 20 20 70 71 72 0a 2a 2a 20  ecomes   pqr.** 
35537 20 20 20 20 60 6d 6e 6f 60 20 20 20 62 65 63 6f      `mno`   beco
35538 6d 65 73 20 20 20 6d 6e 6f 0a 2a 2f 0a 53 51 4c  mes   mno.*/.SQL
35539 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
3553a 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 71 75   sqlite3Fts3Dequ
3553b 6f 74 65 28 63 68 61 72 20 2a 7a 29 7b 0a 20 20  ote(char *z){.  
3553c 69 6e 74 20 71 75 6f 74 65 3b 0a 20 20 69 6e 74  int quote;.  int
3553d 20 69 2c 20 6a 3b 0a 0a 20 20 71 75 6f 74 65 20   i, j;..  quote 
3553e 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68  = z[0];.  switch
3553f 28 20 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 63  ( quote ){.    c
35540 61 73 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b  ase '\'':  break
35541 3b 0a 20 20 20 20 63 61 73 65 20 27 22 27 3a 20  ;.    case '"': 
35542 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
35543 65 20 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20  e '`':   break; 
35544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35545 2a 20 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70  * For MySQL comp
35546 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20  atibility */.   
35547 20 63 61 73 65 20 27 5b 27 3a 20 20 20 71 75 6f   case '[':   quo
35548 74 65 20 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b  te = ']';  break
35549 3b 20 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c  ;  /* For MS Sql
3554a 53 65 72 76 65 72 20 63 6f 6d 70 61 74 69 62 69  Server compatibi
3554b 6c 69 74 79 20 2a 2f 0a 20 20 20 20 64 65 66 61  lity */.    defa
3554c 75 6c 74 3a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ult:    return;.
3554d 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20 6a    }.  for(i=1, j
3554e 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  =0; z[i]; i++){.
3554f 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75      if( z[i]==qu
35550 6f 74 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ote ){.      if(
35551 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29   z[i+1]==quote )
35552 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d  {.        z[j++]
35553 20 3d 20 28 63 68 61 72 29 71 75 6f 74 65 3b 0a   = (char)quote;.
35554 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
35555 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
35556 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20    z[j++] = 0;.  
35557 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
35558 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
35559 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
3555a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  [i];.    }.  }.}
3555b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  ..static void ft
3555c 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74  s3GetDeltaVarint
3555d 28 63 68 61 72 20 2a 2a 70 70 2c 20 73 71 6c 69  (char **pp, sqli
3555e 74 65 33 5f 69 6e 74 36 34 20 2a 70 56 61 6c 29  te3_int64 *pVal)
3555f 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
35560 34 20 69 56 61 6c 3b 0a 20 20 2a 70 70 20 2b 3d  4 iVal;.  *pp +=
35561 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56   sqlite3Fts3GetV
35562 61 72 69 6e 74 28 2a 70 70 2c 20 26 69 56 61 6c  arint(*pp, &iVal
35563 29 3b 0a 20 20 2a 70 56 61 6c 20 2b 3d 20 69 56  );.  *pVal += iV
35564 61 6c 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f  al;.}..static vo
35565 69 64 20 66 74 73 33 47 65 74 44 65 6c 74 61 56  id fts3GetDeltaV
35566 61 72 69 6e 74 32 28 63 68 61 72 20 2a 2a 70 70  arint2(char **pp
35567 2c 20 63 68 61 72 20 2a 70 45 6e 64 2c 20 73 71  , char *pEnd, sq
35568 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 56 61  lite3_int64 *pVa
35569 6c 29 7b 0a 20 20 69 66 28 20 2a 70 70 3e 3d 70  l){.  if( *pp>=p
3556a 45 6e 64 20 29 7b 0a 20 20 20 20 2a 70 70 20 3d  End ){.    *pp =
3556b 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
3556c 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
3556d 69 6e 74 28 70 70 2c 20 70 56 61 6c 29 3b 0a 20  int(pp, pVal);. 
3556e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   }.}../*.** The 
3556f 78 44 69 73 63 6f 6e 6e 65 63 74 28 29 20 76 69  xDisconnect() vi
35570 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68  rtual table meth
35571 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
35572 74 20 66 74 73 33 44 69 73 63 6f 6e 6e 65 63 74  t fts3Disconnect
35573 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76  Method(sqlite3_v
35574 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 46  tab *pVtab){.  F
35575 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46  ts3Table *p = (F
35576 74 73 33 54 61 62 6c 65 20 2a 29 70 56 74 61 62  ts3Table *)pVtab
35577 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73  ;.  int i;..  as
35578 73 65 72 74 28 20 70 2d 3e 6e 50 65 6e 64 69 6e  sert( p->nPendin
35579 67 44 61 74 61 3d 3d 30 20 29 3b 0a 0a 20 20 2f  gData==0 );..  /
3557a 2a 20 46 72 65 65 20 61 6e 79 20 70 72 65 70 61  * Free any prepa
3557b 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 68  red statements h
3557c 65 6c 64 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  eld */.  for(i=0
3557d 3b 20 69 3c 53 69 7a 65 6f 66 41 72 72 61 79 28  ; i<SizeofArray(
3557e 70 2d 3e 61 53 74 6d 74 29 3b 20 69 2b 2b 29 7b  p->aStmt); i++){
3557f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e  .    sqlite3_fin
35580 61 6c 69 7a 65 28 70 2d 3e 61 53 74 6d 74 5b 69  alize(p->aStmt[i
35581 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  ]);.  }.  for(i=
35582 30 3b 20 69 3c 70 2d 3e 6e 4c 65 61 76 65 73 53  0; i<p->nLeavesS
35583 74 6d 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  tmt; i++){.    s
35584 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
35585 70 2d 3e 61 4c 65 61 76 65 73 53 74 6d 74 5b 69  p->aLeavesStmt[i
35586 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ]);.  }.  sqlite
35587 33 5f 66 72 65 65 28 70 2d 3e 7a 53 65 6c 65 63  3_free(p->zSelec
35588 74 4c 65 61 76 65 73 29 3b 0a 20 20 73 71 6c 69  tLeaves);.  sqli
35589 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 4c 65 61  te3_free(p->aLea
3558a 76 65 73 53 74 6d 74 29 3b 0a 0a 20 20 2f 2a 20  vesStmt);..  /* 
3558b 49 6e 76 6f 6b 65 20 74 68 65 20 74 6f 6b 65 6e  Invoke the token
3558c 69 7a 65 72 20 64 65 73 74 72 75 63 74 6f 72 20  izer destructor 
3558d 74 6f 20 66 72 65 65 20 74 68 65 20 74 6f 6b 65  to free the toke
3558e 6e 69 7a 65 72 2e 20 2a 2f 0a 20 20 70 2d 3e 70  nizer. */.  p->p
3558f 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75  Tokenizer->pModu
35590 6c 65 2d 3e 78 44 65 73 74 72 6f 79 28 70 2d 3e  le->xDestroy(p->
35591 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20 20  pTokenizer);..  
35592 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
35593 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
35594 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
35595 65 20 78 44 65 73 74 72 6f 79 28 29 20 76 69 72  e xDestroy() vir
35596 74 75 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f  tual table metho
35597 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
35598 20 66 74 73 33 44 65 73 74 72 6f 79 4d 65 74 68   fts3DestroyMeth
35599 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  od(sqlite3_vtab 
3559a 2a 70 56 74 61 62 29 7b 0a 20 20 69 6e 74 20 72  *pVtab){.  int r
3559b 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
3559c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
3559d 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 46  turn code */.  F
3559e 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46  ts3Table *p = (F
3559f 74 73 33 54 61 62 6c 65 20 2a 29 70 56 74 61 62  ts3Table *)pVtab
355a0 3b 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 61  ;..  /* Create a
355a1 20 73 63 72 69 70 74 20 74 6f 20 64 72 6f 70 20   script to drop 
355a2 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 74  the underlying t
355a3 68 72 65 65 20 73 74 6f 72 61 67 65 20 74 61 62  hree storage tab
355a4 6c 65 73 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a  les. */.  char *
355a5 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zSql = sqlite3_m
355a6 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 22 44  printf(.      "D
355a7 52 4f 50 20 54 41 42 4c 45 20 49 46 20 45 58 49  ROP TABLE IF EXI
355a8 53 54 53 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65  STS %Q.'%q_conte
355a9 6e 74 27 3b 22 0a 20 20 20 20 20 20 22 44 52 4f  nt';".      "DRO
355aa 50 20 54 41 42 4c 45 20 49 46 20 45 58 49 53 54  P TABLE IF EXIST
355ab 53 20 25 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74  S %Q.'%q_segment
355ac 73 27 3b 22 0a 20 20 20 20 20 20 22 44 52 4f 50  s';".      "DROP
355ad 20 54 41 42 4c 45 20 49 46 20 45 58 49 53 54 53   TABLE IF EXISTS
355ae 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27 3b   %Q.'%q_segdir';
355af 22 2c 20 0a 20 20 20 20 20 20 70 2d 3e 7a 44 62  ", .      p->zDb
355b0 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 70 2d 3e 7a  , p->zName, p->z
355b1 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 70 2d  Db, p->zName, p-
355b2 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d 65 0a 20  >zDb, p->zName. 
355b3 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6d 61 6c   );..  /* If mal
355b4 6c 6f 63 20 68 61 73 20 66 61 69 6c 65 64 2c 20  loc has failed, 
355b5 73 65 74 20 72 63 20 74 6f 20 53 51 4c 49 54 45  set rc to SQLITE
355b6 5f 4e 4f 4d 45 4d 2e 20 4f 74 68 65 72 77 69 73  _NOMEM. Otherwis
355b7 65 2c 20 74 72 79 20 74 6f 0a 20 20 2a 2a 20 65  e, try to.  ** e
355b8 78 65 63 75 74 65 20 74 68 65 20 53 51 4c 20 73  xecute the SQL s
355b9 63 72 69 70 74 20 63 72 65 61 74 65 64 20 61 62  cript created ab
355ba 6f 76 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ove..  */.  if( 
355bb 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d  zSql ){.    rc =
355bc 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 70 2d   sqlite3_exec(p-
355bd 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c  >db, zSql, 0, 0,
355be 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
355bf 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d  _free(zSql);.  }
355c0 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53  else{.    rc = S
355c1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
355c2 0a 0a 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74  ..  /* If everyt
355c3 68 69 6e 67 20 68 61 73 20 77 6f 72 6b 65 64 2c  hing has worked,
355c4 20 69 6e 76 6f 6b 65 20 66 74 73 33 44 69 73 63   invoke fts3Disc
355c5 6f 6e 6e 65 63 74 4d 65 74 68 6f 64 28 29 20 74  onnectMethod() t
355c6 6f 20 66 72 65 65 20 74 68 65 0a 20 20 2a 2a 20  o free the.  ** 
355c7 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
355c8 64 20 77 69 74 68 20 74 68 65 20 46 74 73 33 54  d with the Fts3T
355c9 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 61  able structure a
355ca 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  nd return SQLITE
355cb 5f 4f 4b 2e 0a 20 20 2a 2a 20 4f 74 68 65 72 77  _OK..  ** Otherw
355cc 69 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 53  ise, return an S
355cd 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
355ce 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ..  */.  return 
355cf 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f  (rc==SQLITE_OK ?
355d0 20 66 74 73 33 44 69 73 63 6f 6e 6e 65 63 74 4d   fts3DisconnectM
355d1 65 74 68 6f 64 28 70 56 74 61 62 29 20 3a 20 72  ethod(pVtab) : r
355d2 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e  c);.}.../*.** In
355d3 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 64 65 63  voke sqlite3_dec
355d4 6c 61 72 65 5f 76 74 61 62 28 29 20 74 6f 20 64  lare_vtab() to d
355d5 65 63 6c 61 72 65 20 74 68 65 20 73 63 68 65 6d  eclare the schem
355d6 61 20 66 6f 72 20 74 68 65 20 46 54 53 33 20 74  a for the FTS3 t
355d7 61 62 6c 65 0a 2a 2a 20 70 61 73 73 65 64 20 61  able.** passed a
355d8 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
355d9 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 64 6f  ment. This is do
355da 6e 65 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ne as part of th
355db 65 20 78 43 6f 6e 6e 65 63 74 28 29 0a 2a 2a 20  e xConnect().** 
355dc 61 6e 64 20 78 43 72 65 61 74 65 28 29 20 6d 65  and xCreate() me
355dd 74 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  thods..*/.static
355de 20 69 6e 74 20 66 74 73 33 44 65 63 6c 61 72 65   int fts3Declare
355df 56 74 61 62 28 46 74 73 33 54 61 62 6c 65 20 2a  Vtab(Fts3Table *
355e0 70 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  p){.  int i;    
355e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
355e2 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
355e3 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
355e4 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
355e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
355e6 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
355e7 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20  /.  char *zSql; 
355e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
355e9 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65      /* SQL state
355ea 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 64  ment passed to d
355eb 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a 2f  eclare_vtab() */
355ec 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 73 3b 20  .  char *zCols; 
355ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
355ee 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 75 73     /* List of us
355ef 65 72 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d  er defined colum
355f0 6e 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 72 65 61  ns */..  /* Crea
355f1 74 65 20 61 20 6c 69 73 74 20 6f 66 20 75 73 65  te a list of use
355f2 72 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 74 68  r columns for th
355f3 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
355f4 2a 2f 0a 20 20 7a 43 6f 6c 73 20 3d 20 73 71 6c  */.  zCols = sql
355f5 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 51  ite3_mprintf("%Q
355f6 2c 20 22 2c 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e  , ", p->azColumn
355f7 5b 30 5d 29 3b 0a 20 20 66 6f 72 28 69 3d 31 3b  [0]);.  for(i=1;
355f8 20 7a 43 6f 6c 73 20 26 26 20 69 3c 70 2d 3e 6e   zCols && i<p->n
355f9 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
355fa 20 20 7a 43 6f 6c 73 20 3d 20 73 71 6c 69 74 65    zCols = sqlite
355fb 33 5f 6d 70 72 69 6e 74 66 28 22 25 7a 25 51 2c  3_mprintf("%z%Q,
355fc 20 22 2c 20 7a 43 6f 6c 73 2c 20 70 2d 3e 61 7a   ", zCols, p->az
355fd 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 7d 0a  Column[i]);.  }.
355fe 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65  .  /* Create the
355ff 20 77 68 6f 6c 65 20 22 43 52 45 41 54 45 20 54   whole "CREATE T
35600 41 42 4c 45 22 20 73 74 61 74 65 6d 65 6e 74 20  ABLE" statement 
35601 74 6f 20 70 61 73 73 20 74 6f 20 53 51 4c 69 74  to pass to SQLit
35602 65 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71  e */.  zSql = sq
35603 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20  lite3_mprintf(. 
35604 20 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42       "CREATE TAB
35605 4c 45 20 78 28 25 73 20 25 51 20 48 49 44 44 45  LE x(%s %Q HIDDE
35606 4e 2c 20 64 6f 63 69 64 20 48 49 44 44 45 4e 29  N, docid HIDDEN)
35607 22 2c 20 7a 43 6f 6c 73 2c 20 70 2d 3e 7a 4e 61  ", zCols, p->zNa
35608 6d 65 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20 21  me.  );..  if( !
35609 7a 43 6f 6c 73 20 7c 7c 20 21 7a 53 71 6c 20 29  zCols || !zSql )
3560a 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
3560b 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65  E_NOMEM;.  }else
3560c 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
3560d 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28  e3_declare_vtab(
3560e 70 2d 3e 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20  p->db, zSql);.  
3560f 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  }..  sqlite3_fre
35610 65 28 7a 53 71 6c 29 3b 0a 20 20 73 71 6c 69 74  e(zSql);.  sqlit
35611 65 33 5f 66 72 65 65 28 7a 43 6f 6c 73 29 3b 0a  e3_free(zCols);.
35612 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
35613 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 74 68 65  /*.** Create the
35614 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 74   backing store t
35615 61 62 6c 65 73 20 28 25 5f 63 6f 6e 74 65 6e 74  ables (%_content
35616 2c 20 25 5f 73 65 67 6d 65 6e 74 73 20 61 6e 64  , %_segments and
35617 20 25 5f 73 65 67 64 69 72 29 0a 2a 2a 20 72 65   %_segdir).** re
35618 71 75 69 72 65 64 20 62 79 20 74 68 65 20 46 54  quired by the FT
35619 53 33 20 74 61 62 6c 65 20 70 61 73 73 65 64 20  S3 table passed 
3561a 61 73 20 74 68 65 20 6f 6e 6c 79 20 61 72 67 75  as the only argu
3561b 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20 64 6f  ment. This is do
3561c 6e 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66  ne.** as part of
3561d 20 74 68 65 20 76 74 61 62 20 78 43 72 65 61 74   the vtab xCreat
3561e 65 28 29 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  e() method..*/.s
3561f 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 43 72  tatic int fts3Cr
35620 65 61 74 65 54 61 62 6c 65 73 28 46 74 73 33 54  eateTables(Fts3T
35621 61 62 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  able *p){.  int 
35622 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
35623 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
35624 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
35625 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
35626 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35627 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
35628 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  able */.  char *
35629 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73 3b 20 20 20  zContentCols;   
3562a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
3562b 75 6d 6e 73 20 6f 66 20 25 5f 63 6f 6e 74 65 6e  umns of %_conten
3562c 74 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61  t table */.  cha
3562d 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20 20 20  r *zSql;        
3562e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3562f 53 51 4c 20 73 63 72 69 70 74 20 74 6f 20 63 72  SQL script to cr
35630 65 61 74 65 20 72 65 71 75 69 72 65 64 20 74 61  eate required ta
35631 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 72  bles */..  /* Cr
35632 65 61 74 65 20 61 20 6c 69 73 74 20 6f 66 20 75  eate a list of u
35633 73 65 72 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20  ser columns for 
35634 74 68 65 20 63 6f 6e 74 65 6e 74 20 74 61 62 6c  the content tabl
35635 65 20 2a 2f 0a 20 20 7a 43 6f 6e 74 65 6e 74 43  e */.  zContentC
35636 6f 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  ols = sqlite3_mp
35637 72 69 6e 74 66 28 22 64 6f 63 69 64 20 49 4e 54  rintf("docid INT
35638 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
35639 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a  ");.  for(i=0; z
3563a 43 6f 6e 74 65 6e 74 43 6f 6c 73 20 26 26 20 69  ContentCols && i
3563b 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  <p->nColumn; i++
3563c 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20 3d  ){.    char *z =
3563d 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 69 5d 3b   p->azColumn[i];
3563e 0a 20 20 20 20 7a 43 6f 6e 74 65 6e 74 43 6f 6c  .    zContentCol
3563f 73 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  s = sqlite3_mpri
35640 6e 74 66 28 22 25 7a 2c 20 27 63 25 64 25 71 27  ntf("%z, 'c%d%q'
35641 22 2c 20 7a 43 6f 6e 74 65 6e 74 43 6f 6c 73 2c  ", zContentCols,
35642 20 69 2c 20 7a 29 3b 0a 20 20 7d 0a 0a 20 20 2f   i, z);.  }..  /
35643 2a 20 43 72 65 61 74 65 20 74 68 65 20 77 68 6f  * Create the who
35644 6c 65 20 53 51 4c 20 73 63 72 69 70 74 20 2a 2f  le SQL script */
35645 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65  .  zSql = sqlite
35646 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20  3_mprintf(.     
35647 20 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 25   "CREATE TABLE %
35648 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 28 25  Q.'%q_content'(%
35649 73 29 3b 22 0a 20 20 20 20 20 20 22 43 52 45 41  s);".      "CREA
3564a 54 45 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f  TE TABLE %Q.'%q_
3564b 73 65 67 6d 65 6e 74 73 27 28 62 6c 6f 63 6b 69  segments'(blocki
3564c 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  d INTEGER PRIMAR
3564d 59 20 4b 45 59 2c 20 62 6c 6f 63 6b 20 42 4c 4f  Y KEY, block BLO
3564e 42 29 3b 22 0a 20 20 20 20 20 20 22 43 52 45 41  B);".      "CREA
3564f 54 45 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f  TE TABLE %Q.'%q_
35650 73 65 67 64 69 72 27 28 22 0a 20 20 20 20 20 20  segdir'(".      
35651 20 20 22 6c 65 76 65 6c 20 49 4e 54 45 47 45 52    "level INTEGER
35652 2c 22 0a 20 20 20 20 20 20 20 20 22 69 64 78 20  ,".        "idx 
35653 49 4e 54 45 47 45 52 2c 22 0a 20 20 20 20 20 20  INTEGER,".      
35654 20 20 22 73 74 61 72 74 5f 62 6c 6f 63 6b 20 49    "start_block I
35655 4e 54 45 47 45 52 2c 22 0a 20 20 20 20 20 20 20  NTEGER,".       
35656 20 22 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f   "leaves_end_blo
35657 63 6b 20 49 4e 54 45 47 45 52 2c 22 0a 20 20 20  ck INTEGER,".   
35658 20 20 20 20 20 22 65 6e 64 5f 62 6c 6f 63 6b 20       "end_block 
35659 49 4e 54 45 47 45 52 2c 22 0a 20 20 20 20 20 20  INTEGER,".      
3565a 20 20 22 72 6f 6f 74 20 42 4c 4f 42 2c 22 0a 20    "root BLOB,". 
3565b 20 20 20 20 20 20 20 22 50 52 49 4d 41 52 59 20         "PRIMARY 
3565c 4b 45 59 28 6c 65 76 65 6c 2c 20 69 64 78 29 22  KEY(level, idx)"
3565d 0a 20 20 20 20 20 20 22 29 3b 22 2c 0a 20 20 20  .      ");",.   
3565e 20 20 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e     p->zDb, p->zN
3565f 61 6d 65 2c 20 7a 43 6f 6e 74 65 6e 74 43 6f 6c  ame, zContentCol
35660 73 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e  s, p->zDb, p->zN
35661 61 6d 65 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e  ame, p->zDb, p->
35662 7a 4e 61 6d 65 0a 20 20 29 3b 0a 0a 20 20 2f 2a  zName.  );..  /*
35663 20 55 6e 6c 65 73 73 20 61 20 6d 61 6c 6c 6f 63   Unless a malloc
35664 28 29 20 66 61 69 6c 75 72 65 20 68 61 73 20 6f  () failure has o
35665 63 63 75 72 72 65 64 2c 20 65 78 65 63 75 74 65  ccurred, execute
35666 20 74 68 65 20 53 51 4c 20 73 63 72 69 70 74 20   the SQL script 
35667 74 6f 20 0a 20 20 2a 2a 20 63 72 65 61 74 65 20  to .  ** create 
35668 74 68 65 20 74 61 62 6c 65 73 20 75 73 65 64 20  the tables used 
35669 74 6f 20 73 74 6f 72 65 20 64 61 74 61 20 66 6f  to store data fo
3566a 72 20 74 68 69 73 20 46 54 53 33 20 76 69 72 74  r this FTS3 virt
3566b 75 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  ual table..  */.
3566c 20 20 69 66 28 20 7a 43 6f 6e 74 65 6e 74 43 6f    if( zContentCo
3566d 6c 73 3d 3d 30 20 7c 7c 20 7a 53 71 6c 3d 3d 30  ls==0 || zSql==0
3566e 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
3566f 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c  ITE_NOMEM;.  }el
35670 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  se{.    rc = sql
35671 69 74 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c  ite3_exec(p->db,
35672 20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b   zSql, 0, 0, 0);
35673 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f  .  }..  sqlite3_
35674 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 73 71  free(zSql);.  sq
35675 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6e 74  lite3_free(zCont
35676 65 6e 74 43 6f 6c 73 29 3b 0a 20 20 72 65 74 75  entCols);.  retu
35677 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
35678 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
35679 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
3567a 69 6f 6e 20 6f 66 20 62 6f 74 68 20 74 68 65 20  ion of both the 
3567b 78 43 6f 6e 6e 65 63 74 20 61 6e 64 20 78 43 72  xConnect and xCr
3567c 65 61 74 65 0a 2a 2a 20 6d 65 74 68 6f 64 73 20  eate.** methods 
3567d 6f 66 20 74 68 65 20 46 54 53 33 20 76 69 72 74  of the FTS3 virt
3567e 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ual table..**.**
3567f 20 54 68 65 20 61 72 67 76 5b 5d 20 61 72 72 61   The argv[] arra
35680 79 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66  y contains the f
35681 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
35682 20 20 61 72 67 76 5b 30 5d 20 20 20 2d 3e 20 6d    argv[0]   -> m
35683 6f 64 75 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20  odule name.**   
35684 61 72 67 76 5b 31 5d 20 20 20 2d 3e 20 64 61 74  argv[1]   -> dat
35685 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 20 20  abase name.**   
35686 61 72 67 76 5b 32 5d 20 20 20 2d 3e 20 74 61 62  argv[2]   -> tab
35687 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61 72 67  le name.**   arg
35688 76 5b 2e 2e 2e 5d 20 2d 3e 20 22 63 6f 6c 75 6d  v[...] -> "colum
35689 6e 20 6e 61 6d 65 22 20 61 6e 64 20 6f 74 68 65  n name" and othe
3568a 72 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e  r module argumen
3568b 74 20 66 69 65 6c 64 73 2e 0a 2a 2f 0a 69 6e 74  t fields..*/.int
3568c 20 66 74 73 33 49 6e 69 74 56 74 61 62 28 0a 20   fts3InitVtab(. 
3568d 20 69 6e 74 20 69 73 43 72 65 61 74 65 2c 20 20   int isCreate,  
3568e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3568f 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 78 43 72   /* True for xCr
35690 65 61 74 65 2c 20 66 61 6c 73 65 20 66 6f 72 20  eate, false for 
35691 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20 73 71  xConnect */.  sq
35692 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
35693 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35694 20 54 68 65 20 53 51 4c 69 74 65 20 64 61 74 61   The SQLite data
35695 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
35696 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c  */.  void *pAux,
35697 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35698 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74 61 62       /* Hash tab
35699 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 6f  le containing to
3569a 6b 65 6e 69 7a 65 72 73 20 2a 2f 0a 20 20 69 6e  kenizers */.  in
3569b 74 20 61 72 67 63 2c 20 20 20 20 20 20 20 20 20  t argc,         
3569c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3569d 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
3569e 6e 74 73 20 69 6e 20 61 72 67 76 20 61 72 72 61  nts in argv arra
3569f 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  y */.  const cha
356a0 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c  r * const *argv,
356a1 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74         /* xCreat
356a2 65 2f 78 43 6f 6e 6e 65 63 74 20 61 72 67 75 6d  e/xConnect argum
356a3 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73  ent array */.  s
356a4 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70  qlite3_vtab **pp
356a5 56 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f  VTab,          /
356a6 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73 75  * Write the resu
356a7 6c 74 69 6e 67 20 76 74 61 62 20 73 74 72 75 63  lting vtab struc
356a8 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63  ture here */.  c
356a9 68 61 72 20 2a 2a 70 7a 45 72 72 20 20 20 20 20  har **pzErr     
356aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
356ab 2a 20 57 72 69 74 65 20 61 6e 79 20 65 72 72 6f  * Write any erro
356ac 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 2a  r message here *
356ad 2f 0a 29 7b 0a 20 20 46 74 73 33 48 61 73 68 20  /.){.  Fts3Hash 
356ae 2a 70 48 61 73 68 20 3d 20 28 46 74 73 33 48 61  *pHash = (Fts3Ha
356af 73 68 20 2a 29 70 41 75 78 3b 0a 20 20 46 74 73  sh *)pAux;.  Fts
356b0 33 54 61 62 6c 65 20 2a 70 3b 20 20 20 20 20 20  3Table *p;      
356b1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
356b2 50 6f 69 6e 74 65 72 20 74 6f 20 61 6c 6c 6f 63  Pointer to alloc
356b3 61 74 65 64 20 76 74 61 62 20 2a 2f 0a 20 20 69  ated vtab */.  i
356b4 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
356b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
356b6 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
356b7 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
356b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
356b9 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
356ba 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  ariable */.  int
356bb 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
356bc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
356bd 53 69 7a 65 20 6f 66 20 61 6c 6c 6f 63 61 74 69  Size of allocati
356be 6f 6e 20 75 73 65 64 20 66 6f 72 20 2a 70 20 2a  on used for *p *
356bf 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20  /.  int iCol;.  
356c0 69 6e 74 20 6e 53 74 72 69 6e 67 20 3d 20 30 3b  int nString = 0;
356c1 0a 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 30 3b  .  int nCol = 0;
356c2 0a 20 20 63 68 61 72 20 2a 7a 43 73 72 3b 0a 20  .  char *zCsr;. 
356c3 20 69 6e 74 20 6e 44 62 3b 0a 20 20 69 6e 74 20   int nDb;.  int 
356c4 6e 4e 61 6d 65 3b 0a 0a 20 20 63 6f 6e 73 74 20  nName;..  const 
356c5 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 69 7a 65 72  char *zTokenizer
356c6 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
356c7 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
356c8 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 75 73 65 20  okenizer to use 
356c9 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
356ca 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
356cb 65 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  er = 0;        /
356cc 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20  * Tokenizer for 
356cd 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 0a 23  this table */..#
356ce 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
356cf 54 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  T.  const char *
356d0 7a 54 65 73 74 50 61 72 61 6d 20 3d 20 30 3b 0a  zTestParam = 0;.
356d1 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 61 72    if( strncmp(ar
356d2 67 76 5b 61 72 67 63 2d 31 5d 2c 20 22 74 65 73  gv[argc-1], "tes
356d3 74 3a 22 2c 20 35 29 3d 3d 30 20 29 7b 0a 20 20  t:", 5)==0 ){.  
356d4 20 20 7a 54 65 73 74 50 61 72 61 6d 20 3d 20 61    zTestParam = a
356d5 72 67 76 5b 61 72 67 63 2d 31 5d 3b 0a 20 20 20  rgv[argc-1];.   
356d6 20 61 72 67 63 2d 2d 3b 0a 20 20 7d 0a 23 65 6e   argc--;.  }.#en
356d7 64 69 66 0a 0a 20 20 6e 44 62 20 3d 20 28 69 6e  dif..  nDb = (in
356d8 74 29 73 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d  t)strlen(argv[1]
356d9 29 20 2b 20 31 3b 0a 20 20 6e 4e 61 6d 65 20 3d  ) + 1;.  nName =
356da 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 61 72 67   (int)strlen(arg
356db 76 5b 32 5d 29 20 2b 20 31 3b 0a 20 20 66 6f 72  v[2]) + 1;.  for
356dc 28 69 3d 33 3b 20 69 3c 61 72 67 63 3b 20 69 2b  (i=3; i<argc; i+
356dd 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e  +){.    char con
356de 73 74 20 2a 7a 20 3d 20 61 72 67 76 5b 69 5d 3b  st *z = argv[i];
356df 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
356e0 33 46 74 73 33 49 6e 69 74 54 6f 6b 65 6e 69 7a  3Fts3InitTokeniz
356e1 65 72 28 70 48 61 73 68 2c 20 7a 2c 20 26 70 54  er(pHash, z, &pT
356e2 6f 6b 65 6e 69 7a 65 72 2c 20 26 7a 54 6f 6b 65  okenizer, &zToke
356e3 6e 69 7a 65 72 2c 20 70 7a 45 72 72 29 3b 0a 20  nizer, pzErr);. 
356e4 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
356e5 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
356e6 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
356e7 20 20 20 69 66 28 20 7a 21 3d 7a 54 6f 6b 65 6e     if( z!=zToken
356e8 69 7a 65 72 20 29 7b 0a 20 20 20 20 20 20 6e 53  izer ){.      nS
356e9 74 72 69 6e 67 20 2b 3d 20 28 69 6e 74 29 28 73  tring += (int)(s
356ea 74 72 6c 65 6e 28 7a 29 20 2b 20 31 29 3b 0a 20  trlen(z) + 1);. 
356eb 20 20 20 7d 0a 20 20 7d 0a 20 20 6e 43 6f 6c 20     }.  }.  nCol 
356ec 3d 20 61 72 67 63 20 2d 20 33 20 2d 20 28 7a 54  = argc - 3 - (zT
356ed 6f 6b 65 6e 69 7a 65 72 21 3d 30 29 3b 0a 20 20  okenizer!=0);.  
356ee 69 66 28 20 7a 54 6f 6b 65 6e 69 7a 65 72 3d 3d  if( zTokenizer==
356ef 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  0 ){.    rc = sq
356f0 6c 69 74 65 33 46 74 73 33 49 6e 69 74 54 6f 6b  lite3Fts3InitTok
356f1 65 6e 69 7a 65 72 28 70 48 61 73 68 2c 20 30 2c  enizer(pHash, 0,
356f2 20 26 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 30 2c   &pTokenizer, 0,
356f3 20 70 7a 45 72 72 29 3b 0a 20 20 20 20 69 66 28   pzErr);.    if(
356f4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
356f5 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  {.      return r
356f6 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  c;.    }.    ass
356f7 65 72 74 28 20 70 54 6f 6b 65 6e 69 7a 65 72 20  ert( pTokenizer 
356f8 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 6e 43  );.  }..  if( nC
356f9 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 43 6f  ol==0 ){.    nCo
356fa 6c 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  l = 1;.  }..  /*
356fb 20 41 6c 6c 6f 63 61 74 65 20 61 6e 64 20 70 6f   Allocate and po
356fc 70 75 6c 61 74 65 20 74 68 65 20 46 74 73 33 54  pulate the Fts3T
356fd 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 2e 20  able structure. 
356fe 2a 2f 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a  */.  nByte = siz
356ff 65 6f 66 28 46 74 73 33 54 61 62 6c 65 29 20 2b  eof(Fts3Table) +
35700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35701 20 46 74 73 33 54 61 62 6c 65 20 2a 2f 0a 20 20   Fts3Table */.  
35702 20 20 20 20 20 20 20 20 6e 43 6f 6c 20 2a 20 73          nCol * s
35703 69 7a 65 6f 66 28 63 68 61 72 20 2a 29 20 2b 20  izeof(char *) + 
35704 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35705 61 7a 43 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20  azColumn */.    
35706 20 20 20 20 20 20 6e 4e 61 6d 65 20 2b 20 20 20        nName +   
35707 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35708 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e             /* zN
35709 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  ame */.         
3570a 20 6e 44 62 20 2b 20 20 20 20 20 20 20 20 20 20   nDb +          
3570b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3570c 20 20 20 20 20 20 2f 2a 20 7a 44 62 20 2a 2f 0a        /* zDb */.
3570d 20 20 20 20 20 20 20 20 20 20 6e 53 74 72 69 6e            nStrin
3570e 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
3570f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35710 2a 20 53 70 61 63 65 20 66 6f 72 20 61 7a 43 6f  * Space for azCo
35711 6c 75 6d 6e 20 73 74 72 69 6e 67 73 20 2a 2f 0a  lumn strings */.
35712 20 20 70 20 3d 20 28 46 74 73 33 54 61 62 6c 65    p = (Fts3Table
35713 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
35714 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70  (nByte);.  if( p
35715 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
35716 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
35717 20 20 67 6f 74 6f 20 66 74 73 33 5f 69 6e 69 74    goto fts3_init
35718 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  _out;.  }.  mems
35719 65 74 28 70 2c 20 30 2c 20 6e 42 79 74 65 29 3b  et(p, 0, nByte);
3571a 0a 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a  ..  p->db = db;.
3571b 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 6e    p->nColumn = n
3571c 43 6f 6c 3b 0a 20 20 70 2d 3e 6e 50 65 6e 64 69  Col;.  p->nPendi
3571d 6e 67 44 61 74 61 20 3d 20 30 3b 0a 20 20 70 2d  ngData = 0;.  p-
3571e 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 28 63 68 61  >azColumn = (cha
3571f 72 20 2a 2a 29 26 70 5b 31 5d 3b 0a 20 20 70 2d  r **)&p[1];.  p-
35720 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54  >pTokenizer = pT
35721 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 70 2d 3e 6e  okenizer;.  p->n
35722 4e 6f 64 65 53 69 7a 65 20 3d 20 31 30 30 30 3b  NodeSize = 1000;
35723 0a 20 20 7a 43 73 72 20 3d 20 28 63 68 61 72 20  .  zCsr = (char 
35724 2a 29 26 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 6e  *)&p->azColumn[n
35725 43 6f 6c 5d 3b 0a 0a 20 20 66 74 73 33 48 61 73  Col];..  fts3Has
35726 68 49 6e 69 74 28 26 70 2d 3e 70 65 6e 64 69 6e  hInit(&p->pendin
35727 67 54 65 72 6d 73 2c 20 46 54 53 33 5f 48 41 53  gTerms, FTS3_HAS
35728 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 0a 20  H_STRING, 1);.. 
35729 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65 20   /* Fill in the 
3572a 7a 4e 61 6d 65 20 61 6e 64 20 7a 44 62 20 66 69  zName and zDb fi
3572b 65 6c 64 73 20 6f 66 20 74 68 65 20 76 74 61 62  elds of the vtab
3572c 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 20   structure. */. 
3572d 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 43 73 72   p->zName = zCsr
3572e 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 43 73 72 2c  ;.  memcpy(zCsr,
3572f 20 61 72 67 76 5b 32 5d 2c 20 6e 4e 61 6d 65 29   argv[2], nName)
35730 3b 0a 20 20 7a 43 73 72 20 2b 3d 20 6e 4e 61 6d  ;.  zCsr += nNam
35731 65 3b 0a 20 20 70 2d 3e 7a 44 62 20 3d 20 7a 43  e;.  p->zDb = zC
35732 73 72 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 43 73  sr;.  memcpy(zCs
35733 72 2c 20 61 72 67 76 5b 31 5d 2c 20 6e 44 62 29  r, argv[1], nDb)
35734 3b 0a 20 20 7a 43 73 72 20 2b 3d 20 6e 44 62 3b  ;.  zCsr += nDb;
35735 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74  ..  /* Fill in t
35736 68 65 20 61 7a 43 6f 6c 75 6d 6e 20 61 72 72 61  he azColumn arra
35737 79 20 2a 2f 0a 20 20 69 43 6f 6c 20 3d 20 30 3b  y */.  iCol = 0;
35738 0a 20 20 66 6f 72 28 69 3d 33 3b 20 69 3c 61 72  .  for(i=3; i<ar
35739 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  gc; i++){.    if
3573a 28 20 61 72 67 76 5b 69 5d 21 3d 7a 54 6f 6b 65  ( argv[i]!=zToke
3573b 6e 69 7a 65 72 20 29 7b 0a 20 20 20 20 20 20 63  nizer ){.      c
3573c 68 61 72 20 2a 7a 3b 20 0a 20 20 20 20 20 20 69  har *z; .      i
3573d 6e 74 20 6e 3b 0a 20 20 20 20 20 20 7a 20 3d 20  nt n;.      z = 
3573e 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 46  (char *)sqlite3F
3573f 74 73 33 4e 65 78 74 54 6f 6b 65 6e 28 61 72 67  ts3NextToken(arg
35740 76 5b 69 5d 2c 20 26 6e 29 3b 0a 20 20 20 20 20  v[i], &n);.     
35741 20 6d 65 6d 63 70 79 28 7a 43 73 72 2c 20 7a 2c   memcpy(zCsr, z,
35742 20 6e 29 3b 0a 20 20 20 20 20 20 7a 43 73 72 5b   n);.      zCsr[
35743 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  n] = '\0';.     
35744 20 73 71 6c 69 74 65 33 46 74 73 33 44 65 71 75   sqlite3Fts3Dequ
35745 6f 74 65 28 7a 43 73 72 29 3b 0a 20 20 20 20 20  ote(zCsr);.     
35746 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 69 43 6f   p->azColumn[iCo
35747 6c 2b 2b 5d 20 3d 20 7a 43 73 72 3b 0a 20 20 20  l++] = zCsr;.   
35748 20 20 20 7a 43 73 72 20 2b 3d 20 6e 2b 31 3b 0a     zCsr += n+1;.
35749 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a 43        assert( zC
3574a 73 72 20 3c 3d 20 26 28 28 63 68 61 72 20 2a 29  sr <= &((char *)
3574b 70 29 5b 6e 42 79 74 65 5d 20 29 3b 0a 20 20 20  p)[nByte] );.   
3574c 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 43 6f   }.  }.  if( iCo
3574d 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  l==0 ){.    asse
3574e 72 74 28 20 6e 43 6f 6c 3d 3d 31 20 29 3b 0a 20  rt( nCol==1 );. 
3574f 20 20 20 70 2d 3e 61 7a 43 6f 6c 75 6d 6e 5b 30     p->azColumn[0
35750 5d 20 3d 20 22 63 6f 6e 74 65 6e 74 22 3b 0a 20  ] = "content";. 
35751 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73   }..  /* If this
35752 20 69 73 20 61 6e 20 78 43 72 65 61 74 65 20 63   is an xCreate c
35753 61 6c 6c 2c 20 63 72 65 61 74 65 20 74 68 65 20  all, create the 
35754 75 6e 64 65 72 6c 79 69 6e 67 20 74 61 62 6c 65  underlying table
35755 73 20 69 6e 20 74 68 65 20 0a 20 20 2a 2a 20 64  s in the .  ** d
35756 61 74 61 62 61 73 65 2e 20 54 4f 44 4f 3a 20 46  atabase. TODO: F
35757 6f 72 20 78 43 6f 6e 6e 65 63 74 28 29 2c 20 69  or xConnect(), i
35758 74 20 63 6f 75 6c 64 20 76 65 72 69 66 79 20 74  t could verify t
35759 68 61 74 20 73 61 69 64 20 74 61 62 6c 65 73 20  hat said tables 
3575a 65 78 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  exist..  */.  if
3575b 28 20 69 73 43 72 65 61 74 65 20 29 7b 0a 20 20  ( isCreate ){.  
3575c 20 20 72 63 20 3d 20 66 74 73 33 43 72 65 61 74    rc = fts3Creat
3575d 65 54 61 62 6c 65 73 28 70 29 3b 0a 20 20 20 20  eTables(p);.    
3575e 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3575f 4b 20 29 20 67 6f 74 6f 20 66 74 73 33 5f 69 6e  K ) goto fts3_in
35760 69 74 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 72  it_out;.  }..  r
35761 63 20 3d 20 66 74 73 33 44 65 63 6c 61 72 65 56  c = fts3DeclareV
35762 74 61 62 28 70 29 3b 0a 20 20 69 66 28 20 72 63  tab(p);.  if( rc
35763 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
35764 74 6f 20 66 74 73 33 5f 69 6e 69 74 5f 6f 75 74  to fts3_init_out
35765 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
35766 5f 54 45 53 54 0a 20 20 69 66 28 20 7a 54 65 73  _TEST.  if( zTes
35767 74 50 61 72 61 6d 20 29 7b 0a 20 20 20 20 70 2d  tParam ){.    p-
35768 3e 6e 4e 6f 64 65 53 69 7a 65 20 3d 20 61 74 6f  >nNodeSize = ato
35769 69 28 26 7a 54 65 73 74 50 61 72 61 6d 5b 35 5d  i(&zTestParam[5]
3576a 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
3576b 2a 70 70 56 54 61 62 20 3d 20 26 70 2d 3e 62 61  *ppVTab = &p->ba
3576c 73 65 3b 0a 0a 66 74 73 33 5f 69 6e 69 74 5f 6f  se;..fts3_init_o
3576d 75 74 3a 0a 20 20 61 73 73 65 72 74 28 20 70 20  ut:.  assert( p 
3576e 7c 7c 20 28 70 54 6f 6b 65 6e 69 7a 65 72 20 26  || (pTokenizer &
3576f 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29  & rc!=SQLITE_OK)
35770 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
35771 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
35772 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 66 74  f( p ){.      ft
35773 73 33 44 69 73 63 6f 6e 6e 65 63 74 4d 65 74 68  s3DisconnectMeth
35774 6f 64 28 28 73 71 6c 69 74 65 33 5f 76 74 61 62  od((sqlite3_vtab
35775 20 2a 29 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65   *)p);.    }else
35776 7b 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e 69 7a  {.      pTokeniz
35777 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65  er->pModule->xDe
35778 73 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65 72  stroy(pTokenizer
35779 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  );.    }.  }.  r
3577a 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
3577b 2a 2a 20 54 68 65 20 78 43 6f 6e 6e 65 63 74 28  ** The xConnect(
3577c 29 20 61 6e 64 20 78 43 72 65 61 74 65 28 29 20  ) and xCreate() 
3577d 6d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20  methods for the 
3577e 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 41  virtual table. A
3577f 6c 6c 20 74 68 65 0a 2a 2a 20 77 6f 72 6b 20 69  ll the.** work i
35780 73 20 64 6f 6e 65 20 69 6e 20 66 75 6e 63 74 69  s done in functi
35781 6f 6e 20 66 74 73 33 49 6e 69 74 56 74 61 62 28  on fts3InitVtab(
35782 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
35783 20 66 74 73 33 43 6f 6e 6e 65 63 74 4d 65 74 68   fts3ConnectMeth
35784 6f 64 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  od(.  sqlite3 *d
35785 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
35786 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
35787 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
35788 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20 20    void *pAux,   
35789 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3578a 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
3578b 74 6f 6b 65 6e 69 7a 65 72 20 68 61 73 68 20 74  tokenizer hash t
3578c 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 72  able */.  int ar
3578d 67 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gc,             
3578e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
3578f 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
35790 69 6e 20 61 72 67 76 20 61 72 72 61 79 20 2a 2f  in argv array */
35791 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
35792 63 6f 6e 73 74 20 2a 61 72 67 76 2c 20 20 20 20  const *argv,    
35793 20 20 20 2f 2a 20 78 43 72 65 61 74 65 2f 78 43     /* xCreate/xC
35794 6f 6e 6e 65 63 74 20 61 72 67 75 6d 65 6e 74 20  onnect argument 
35795 61 72 72 61 79 20 2a 2f 0a 20 20 73 71 6c 69 74  array */.  sqlit
35796 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62  e3_vtab **ppVtab
35797 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  ,          /* OU
35798 54 3a 20 4e 65 77 20 73 71 6c 69 74 65 33 5f 76  T: New sqlite3_v
35799 74 61 62 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  tab object */.  
3579a 63 68 61 72 20 2a 2a 70 7a 45 72 72 20 20 20 20  char **pzErr    
3579b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3579c 2f 2a 20 4f 55 54 3a 20 73 71 6c 69 74 65 33 5f  /* OUT: sqlite3_
3579d 6d 61 6c 6c 6f 63 27 64 20 65 72 72 6f 72 20 6d  malloc'd error m
3579e 65 73 73 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 72  essage */.){.  r
3579f 65 74 75 72 6e 20 66 74 73 33 49 6e 69 74 56 74  eturn fts3InitVt
357a0 61 62 28 30 2c 20 64 62 2c 20 70 41 75 78 2c 20  ab(0, db, pAux, 
357a1 61 72 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74  argc, argv, ppVt
357a2 61 62 2c 20 70 7a 45 72 72 29 3b 0a 7d 0a 73 74  ab, pzErr);.}.st
357a3 61 74 69 63 20 69 6e 74 20 66 74 73 33 43 72 65  atic int fts3Cre
357a4 61 74 65 4d 65 74 68 6f 64 28 0a 20 20 73 71 6c  ateMethod(.  sql
357a5 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
357a6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
357a7 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
357a8 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ion */.  void *p
357a9 41 75 78 2c 20 20 20 20 20 20 20 20 20 20 20 20  Aux,            
357aa 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
357ab 74 65 72 20 74 6f 20 74 6f 6b 65 6e 69 7a 65 72  ter to tokenizer
357ac 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
357ad 20 69 6e 74 20 61 72 67 63 2c 20 20 20 20 20 20   int argc,      
357ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
357af 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c   /* Number of el
357b0 65 6d 65 6e 74 73 20 69 6e 20 61 72 67 76 20 61  ements in argv a
357b1 72 72 61 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rray */.  const 
357b2 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72  char * const *ar
357b3 67 76 2c 20 20 20 20 20 20 20 2f 2a 20 78 43 72  gv,       /* xCr
357b4 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 61 72  eate/xConnect ar
357b5 67 75 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a  gument array */.
357b6 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
357b7 2a 70 70 56 74 61 62 2c 20 20 20 20 20 20 20 20  *ppVtab,        
357b8 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 77 20 73 71    /* OUT: New sq
357b9 6c 69 74 65 33 5f 76 74 61 62 20 6f 62 6a 65 63  lite3_vtab objec
357ba 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  t */.  char **pz
357bb 45 72 72 20 20 20 20 20 20 20 20 20 20 20 20 20  Err             
357bc 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 73         /* OUT: s
357bd 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 27 64 20  qlite3_malloc'd 
357be 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f  error message */
357bf 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 66 74 73  .){.  return fts
357c0 33 49 6e 69 74 56 74 61 62 28 31 2c 20 64 62 2c  3InitVtab(1, db,
357c1 20 70 41 75 78 2c 20 61 72 67 63 2c 20 61 72 67   pAux, argc, arg
357c2 76 2c 20 70 70 56 74 61 62 2c 20 70 7a 45 72 72  v, ppVtab, pzErr
357c3 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 49 6d 70  );.}../* .** Imp
357c4 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
357c5 68 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65  he xBestIndex me
357c6 74 68 6f 64 20 66 6f 72 20 46 54 53 33 20 74 61  thod for FTS3 ta
357c7 62 6c 65 73 2e 20 54 68 65 72 65 0a 2a 2a 20 61  bles. There.** a
357c8 72 65 20 74 68 72 65 65 20 70 6f 73 73 69 62 6c  re three possibl
357c9 65 20 73 74 72 61 74 65 67 69 65 73 2c 20 69 6e  e strategies, in
357ca 20 6f 72 64 65 72 20 6f 66 20 70 72 65 66 65 72   order of prefer
357cb 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e  ence:.**.**   1.
357cc 20 44 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20 62   Direct lookup b
357cd 79 20 72 6f 77 69 64 20 6f 72 20 64 6f 63 69 64  y rowid or docid
357ce 2e 20 0a 2a 2a 20 20 20 32 2e 20 46 75 6c 6c 2d  . .**   2. Full-
357cf 74 65 78 74 20 73 65 61 72 63 68 20 75 73 69 6e  text search usin
357d0 67 20 61 20 4d 41 54 43 48 20 6f 70 65 72 61 74  g a MATCH operat
357d1 6f 72 20 6f 6e 20 61 20 6e 6f 6e 2d 64 6f 63 69  or on a non-doci
357d2 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 20 20 33  d column..**   3
357d3 2e 20 4c 69 6e 65 61 72 20 73 63 61 6e 20 6f 66  . Linear scan of
357d4 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65   %_content table
357d5 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
357d6 66 74 73 33 42 65 73 74 49 6e 64 65 78 4d 65 74  fts3BestIndexMet
357d7 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62  hod(sqlite3_vtab
357d8 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33   *pVTab, sqlite3
357d9 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 6e  _index_info *pIn
357da 66 6f 29 7b 0a 20 20 46 74 73 33 54 61 62 6c 65  fo){.  Fts3Table
357db 20 2a 70 20 3d 20 28 46 74 73 33 54 61 62 6c 65   *p = (Fts3Table
357dc 20 2a 29 70 56 54 61 62 3b 0a 20 20 69 6e 74 20   *)pVTab;.  int 
357dd 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
357de 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
357df 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
357e0 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 73 20   */.  int iCons 
357e1 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20  = -1;           
357e2 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
357e3 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20  f constraint to 
357e4 75 73 65 20 2a 2f 0a 0a 20 20 2f 2a 20 42 79 20  use */..  /* By 
357e5 64 65 66 61 75 6c 74 20 75 73 65 20 61 20 66 75  default use a fu
357e6 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e 20 54  ll table scan. T
357e7 68 69 73 20 69 73 20 61 6e 20 65 78 70 65 6e 73  his is an expens
357e8 69 76 65 20 6f 70 74 69 6f 6e 2c 0a 20 20 2a 2a  ive option,.  **
357e9 20 73 6f 20 73 65 61 72 63 68 20 74 68 72 6f 75   so search throu
357ea 67 68 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  gh the constrain
357eb 74 73 20 74 6f 20 73 65 65 20 69 66 20 61 20 6d  ts to see if a m
357ec 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 0a 20  ore efficient . 
357ed 20 2a 2a 20 73 74 72 61 74 65 67 79 20 69 73 20   ** strategy is 
357ee 70 6f 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20  possible..  */. 
357ef 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d   pInfo->idxNum =
357f0 20 46 54 53 33 5f 46 55 4c 4c 53 43 41 4e 5f 53   FTS3_FULLSCAN_S
357f1 45 41 52 43 48 3b 0a 20 20 70 49 6e 66 6f 2d 3e  EARCH;.  pInfo->
357f2 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20  estimatedCost = 
357f3 35 30 30 30 30 30 3b 0a 20 20 66 6f 72 28 69 3d  500000;.  for(i=
357f4 30 3b 20 69 3c 70 49 6e 66 6f 2d 3e 6e 43 6f 6e  0; i<pInfo->nCon
357f5 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20  straint; i++){. 
357f6 20 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65     struct sqlite
357f7 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
357f8 6e 74 20 2a 70 43 6f 6e 73 20 3d 20 26 70 49 6e  nt *pCons = &pIn
357f9 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  fo->aConstraint[
357fa 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 6f 6e  i];.    if( pCon
357fb 73 2d 3e 75 73 61 62 6c 65 3d 3d 30 20 29 20 63  s->usable==0 ) c
357fc 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 2f 2a  ontinue;..    /*
357fd 20 41 20 64 69 72 65 63 74 20 6c 6f 6f 6b 75 70   A direct lookup
357fe 20 6f 6e 20 74 68 65 20 72 6f 77 69 64 20 6f 72   on the rowid or
357ff 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e 2e 20 54   docid column. T
35800 68 69 73 20 69 73 20 74 68 65 20 62 65 73 74 0a  his is the best.
35801 20 20 20 20 2a 2a 20 73 74 72 61 74 65 67 79 20      ** strategy 
35802 69 6e 20 61 6c 6c 20 63 61 73 65 73 2e 20 41 73  in all cases. As
35803 73 69 67 6e 20 61 20 63 6f 73 74 20 6f 66 20 31  sign a cost of 1
35804 2e 30 20 61 6e 64 20 72 65 74 75 72 6e 20 65 61  .0 and return ea
35805 72 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  rly..    */.    
35806 69 66 28 20 70 43 6f 6e 73 2d 3e 6f 70 3d 3d 53  if( pCons->op==S
35807 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
35808 54 52 41 49 4e 54 5f 45 51 20 0a 20 20 20 20 20  TRAINT_EQ .     
35809 26 26 20 28 70 43 6f 6e 73 2d 3e 69 43 6f 6c 75  && (pCons->iColu
3580a 6d 6e 3c 30 20 7c 7c 20 70 43 6f 6e 73 2d 3e 69  mn<0 || pCons->i
3580b 43 6f 6c 75 6d 6e 3d 3d 70 2d 3e 6e 43 6f 6c 75  Column==p->nColu
3580c 6d 6e 2b 31 20 29 0a 20 20 20 20 29 7b 0a 20 20  mn+1 ).    ){.  
3580d 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75      pInfo->idxNu
3580e 6d 20 3d 20 46 54 53 33 5f 44 4f 43 49 44 5f 53  m = FTS3_DOCID_S
3580f 45 41 52 43 48 3b 0a 20 20 20 20 20 20 70 49 6e  EARCH;.      pIn
35810 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73  fo->estimatedCos
35811 74 20 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 69  t = 1.0;.      i
35812 43 6f 6e 73 20 3d 20 69 3b 0a 20 20 20 20 20 20  Cons = i;.      
35813 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
35814 20 20 2f 2a 20 41 20 4d 41 54 43 48 20 63 6f 6e    /* A MATCH con
35815 73 74 72 61 69 6e 74 2e 20 55 73 65 20 61 20 66  straint. Use a f
35816 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 2e  ull-text search.
35817 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
35818 66 20 74 68 65 72 65 20 69 73 20 6d 6f 72 65 20  f there is more 
35819 74 68 61 6e 20 6f 6e 65 20 4d 41 54 43 48 20 63  than one MATCH c
3581a 6f 6e 73 74 72 61 69 6e 74 20 61 76 61 69 6c 61  onstraint availa
3581b 62 6c 65 2c 20 75 73 65 20 74 68 65 20 66 69 72  ble, use the fir
3581c 73 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e  st.    ** one en
3581d 63 6f 75 6e 74 65 72 65 64 2e 20 49 66 20 74 68  countered. If th
3581e 65 72 65 20 69 73 20 62 6f 74 68 20 61 20 4d 41  ere is both a MA
3581f 54 43 48 20 63 6f 6e 73 74 72 61 69 6e 74 20 61  TCH constraint a
35820 6e 64 20 61 20 64 69 72 65 63 74 0a 20 20 20 20  nd a direct.    
35821 2a 2a 20 72 6f 77 69 64 2f 64 6f 63 69 64 20 6c  ** rowid/docid l
35822 6f 6f 6b 75 70 2c 20 70 72 65 66 65 72 20 74 68  ookup, prefer th
35823 65 20 72 6f 77 69 64 2f 64 6f 63 69 64 20 73 74  e rowid/docid st
35824 72 61 74 65 67 79 2e 0a 20 20 20 20 2a 2f 0a 20  rategy..    */. 
35825 20 20 20 69 66 28 20 69 43 6f 6e 73 3c 30 20 0a     if( iCons<0 .
35826 20 20 20 20 20 26 26 20 70 43 6f 6e 73 2d 3e 6f       && pCons->o
35827 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  p==SQLITE_INDEX_
35828 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48  CONSTRAINT_MATCH
35829 20 0a 20 20 20 20 20 26 26 20 70 43 6f 6e 73 2d   .     && pCons-
3582a 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70  >iColumn>=0 && p
3582b 43 6f 6e 73 2d 3e 69 43 6f 6c 75 6d 6e 3c 3d 70  Cons->iColumn<=p
3582c 2d 3e 6e 43 6f 6c 75 6d 6e 0a 20 20 20 20 29 7b  ->nColumn.    ){
3582d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69 64  .      pInfo->id
3582e 78 4e 75 6d 20 3d 20 46 54 53 33 5f 46 55 4c 4c  xNum = FTS3_FULL
3582f 54 45 58 54 5f 53 45 41 52 43 48 20 2b 20 70 43  TEXT_SEARCH + pC
35830 6f 6e 73 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  ons->iColumn;.  
35831 20 20 20 20 70 49 6e 66 6f 2d 3e 65 73 74 69 6d      pInfo->estim
35832 61 74 65 64 43 6f 73 74 20 3d 20 32 2e 30 3b 0a  atedCost = 2.0;.
35833 20 20 20 20 20 20 69 43 6f 6e 73 20 3d 20 69 3b        iCons = i;
35834 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
35835 28 20 69 43 6f 6e 73 3e 3d 30 20 29 7b 0a 20 20  ( iCons>=0 ){.  
35836 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72    pInfo->aConstr
35837 61 69 6e 74 55 73 61 67 65 5b 69 43 6f 6e 73 5d  aintUsage[iCons]
35838 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a  .argvIndex = 1;.
35839 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73      pInfo->aCons
3583a 74 72 61 69 6e 74 55 73 61 67 65 5b 69 43 6f 6e  traintUsage[iCon
3583b 73 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 20 20 7d  s].omit = 1;.  }
3583c 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54   .  return SQLIT
3583d 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  E_OK;.}../*.** I
3583e 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
3583f 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 2e 0a 2a   xOpen method..*
35840 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
35841 33 4f 70 65 6e 4d 65 74 68 6f 64 28 73 71 6c 69  3OpenMethod(sqli
35842 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c  te3_vtab *pVTab,
35843 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
35844 72 73 6f 72 20 2a 2a 70 70 43 73 72 29 7b 0a 20  rsor **ppCsr){. 
35845 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
35846 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 20  rsor *pCsr;     
35847 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c            /* All
35848 6f 63 61 74 65 64 20 63 75 72 73 6f 72 20 2a 2f  ocated cursor */
35849 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
3584a 45 54 45 52 28 70 56 54 61 62 29 3b 0a 0a 20 20  ETER(pVTab);..  
3584b 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62 75  /* Allocate a bu
3584c 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67  ffer large enoug
3584d 68 20 66 6f 72 20 61 6e 20 46 74 73 33 43 75 72  h for an Fts3Cur
3584e 73 6f 72 20 73 74 72 75 63 74 75 72 65 2e 20 49  sor structure. I
3584f 66 20 74 68 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63  f the.  ** alloc
35850 61 74 69 6f 6e 20 73 75 63 63 65 65 64 73 2c 20  ation succeeds, 
35851 7a 65 72 6f 20 69 74 20 61 6e 64 20 72 65 74 75  zero it and retu
35852 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74  rn SQLITE_OK. Ot
35853 68 65 72 77 69 73 65 2c 20 0a 20 20 2a 2a 20 69  herwise, .  ** i
35854 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
35855 20 66 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 53   fails, return S
35856 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 20 20 2a  QLITE_NOMEM..  *
35857 2f 0a 20 20 2a 70 70 43 73 72 20 3d 20 70 43 73  /.  *ppCsr = pCs
35858 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61  r = (sqlite3_vta
35859 62 5f 63 75 72 73 6f 72 20 2a 29 73 71 6c 69 74  b_cursor *)sqlit
3585a 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
3585b 28 46 74 73 33 43 75 72 73 6f 72 29 29 3b 0a 20  (Fts3Cursor));. 
3585c 20 69 66 28 20 21 70 43 73 72 20 29 7b 0a 20 20   if( !pCsr ){.  
3585d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3585e 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d  NOMEM;.  }.  mem
3585f 73 65 74 28 70 43 73 72 2c 20 30 2c 20 73 69 7a  set(pCsr, 0, siz
35860 65 6f 66 28 46 74 73 33 43 75 72 73 6f 72 29 29  eof(Fts3Cursor))
35861 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
35862 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  E_OK;.}../******
35863 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35864 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35865 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35866 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
35867 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35868 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35869 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3586a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
3586b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3586c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3586d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3586e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3586f 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
35870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35871 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35872 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35873 2a 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  ***/.../*.** Clo
35874 73 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20  se the cursor.  
35875 46 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  For additional i
35876 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 65 65 20 74  nformation see t
35877 68 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  he documentation
35878 0a 2a 2a 20 6f 6e 20 74 68 65 20 78 43 6c 6f 73  .** on the xClos
35879 65 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  e method of the 
3587a 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  virtual table in
3587b 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 73 74 61 74  terface..*/.stat
3587c 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43  ic int fulltextC
3587d 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 74 61  lose(sqlite3_vta
3587e 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  b_cursor *pCurso
3587f 72 29 7b 0a 20 20 46 74 73 33 43 75 72 73 6f 72  r){.  Fts3Cursor
35880 20 2a 70 43 73 72 20 3d 20 28 46 74 73 33 43 75   *pCsr = (Fts3Cu
35881 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a  rsor *)pCursor;.
35882 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
35883 7a 65 28 70 43 73 72 2d 3e 70 53 74 6d 74 29 3b  ze(pCsr->pStmt);
35884 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  .  sqlite3Fts3Ex
35885 70 72 46 72 65 65 28 70 43 73 72 2d 3e 70 45 78  prFree(pCsr->pEx
35886 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  pr);.  sqlite3_f
35887 72 65 65 28 70 43 73 72 2d 3e 61 44 6f 63 6c 69  ree(pCsr->aDocli
35888 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  st);.  sqlite3_f
35889 72 65 65 28 70 43 73 72 29 3b 0a 20 20 72 65 74  ree(pCsr);.  ret
3588a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
3588b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  ..static int fts
3588c 33 43 75 72 73 6f 72 53 65 65 6b 28 46 74 73 33  3CursorSeek(Fts3
3588d 43 75 72 73 6f 72 20 2a 70 43 73 72 29 7b 0a 20  Cursor *pCsr){. 
3588e 20 69 66 28 20 70 43 73 72 2d 3e 69 73 52 65 71   if( pCsr->isReq
3588f 75 69 72 65 53 65 65 6b 20 29 7b 0a 20 20 20 20  uireSeek ){.    
35890 70 43 73 72 2d 3e 69 73 52 65 71 75 69 72 65 53  pCsr->isRequireS
35891 65 65 6b 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  eek = 0;.    sql
35892 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
35893 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 31 2c 20  pCsr->pStmt, 1, 
35894 70 43 73 72 2d 3e 69 50 72 65 76 49 64 29 3b 0a  pCsr->iPrevId);.
35895 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52      if( SQLITE_R
35896 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70  OW==sqlite3_step
35897 28 70 43 73 72 2d 3e 70 53 74 6d 74 29 20 29 7b  (pCsr->pStmt) ){
35898 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
35899 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c  LITE_OK;.    }el
3589a 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63  se{.      int rc
3589b 3b 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69 73  ;.      pCsr->is
3589c 45 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 69  Eof = 1;.      i
3589d 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  f( SQLITE_OK==(r
3589e 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
3589f 74 28 70 43 73 72 2d 3e 70 53 74 6d 74 29 29 20  t(pCsr->pStmt)) 
358a0 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
358a1 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
358a2 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
358a3 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d  rn rc;.    }.  }
358a4 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
358a5 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
358a6 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  }..static int ft
358a7 73 33 4e 65 78 74 4d 65 74 68 6f 64 28 73 71 6c  s3NextMethod(sql
358a8 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
358a9 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e   *pCursor){.  in
358aa 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
358ab 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
358ac 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
358ad 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43    Fts3Cursor *pC
358ae 73 72 20 3d 20 28 46 74 73 33 43 75 72 73 6f 72  sr = (Fts3Cursor
358af 20 2a 29 70 43 75 72 73 6f 72 3b 0a 0a 20 20 69   *)pCursor;..  i
358b0 66 28 20 70 43 73 72 2d 3e 61 44 6f 63 6c 69 73  f( pCsr->aDoclis
358b1 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  t==0 ){.    if( 
358b2 53 51 4c 49 54 45 5f 52 4f 57 21 3d 73 71 6c 69  SQLITE_ROW!=sqli
358b3 74 65 33 5f 73 74 65 70 28 70 43 73 72 2d 3e 70  te3_step(pCsr->p
358b4 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 70  Stmt) ){.      p
358b5 43 73 72 2d 3e 69 73 45 6f 66 20 3d 20 31 3b 0a  Csr->isEof = 1;.
358b6 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
358b7 65 33 5f 72 65 73 65 74 28 70 43 73 72 2d 3e 70  e3_reset(pCsr->p
358b8 53 74 6d 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Stmt);.    }.  }
358b9 65 6c 73 65 20 69 66 28 20 70 43 73 72 2d 3e 70  else if( pCsr->p
358ba 4e 65 78 74 49 64 3e 3d 26 70 43 73 72 2d 3e 61  NextId>=&pCsr->a
358bb 44 6f 63 6c 69 73 74 5b 70 43 73 72 2d 3e 6e 44  Doclist[pCsr->nD
358bc 6f 63 6c 69 73 74 5d 20 29 7b 0a 20 20 20 20 70  oclist] ){.    p
358bd 43 73 72 2d 3e 69 73 45 6f 66 20 3d 20 31 3b 0a  Csr->isEof = 1;.
358be 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
358bf 69 74 65 33 5f 72 65 73 65 74 28 70 43 73 72 2d  ite3_reset(pCsr-
358c0 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 66 74 73  >pStmt);.    fts
358c1 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28  3GetDeltaVarint(
358c2 26 70 43 73 72 2d 3e 70 4e 65 78 74 49 64 2c 20  &pCsr->pNextId, 
358c3 26 70 43 73 72 2d 3e 69 50 72 65 76 49 64 29 3b  &pCsr->iPrevId);
358c4 0a 20 20 20 20 70 43 73 72 2d 3e 69 73 52 65 71  .    pCsr->isReq
358c5 75 69 72 65 53 65 65 6b 20 3d 20 31 3b 0a 20 20  uireSeek = 1;.  
358c6 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
358c7 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 75 66  .../*.** The buf
358c8 66 65 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  fer pointed to b
358c9 79 20 61 72 67 75 6d 65 6e 74 20 7a 4e 6f 64 65  y argument zNode
358ca 20 28 73 69 7a 65 20 6e 4e 6f 64 65 20 62 79 74   (size nNode byt
358cb 65 73 29 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  es) contains the
358cc 0a 2a 2a 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66  .** root node of
358cd 20 61 20 62 2d 74 72 65 65 20 73 65 67 6d 65 6e   a b-tree segmen
358ce 74 2e 20 54 68 65 20 73 65 67 6d 65 6e 74 20 69  t. The segment i
358cf 73 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20  s guaranteed to 
358d0 62 65 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 6f  be at least.** o
358d1 6e 65 20 6c 65 76 65 6c 20 68 69 67 68 20 28 69  ne level high (i
358d2 2e 65 2e 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64  .e. the root nod
358d3 65 20 69 73 20 6e 6f 74 20 61 6c 73 6f 20 61 20  e is not also a 
358d4 6c 65 61 66 29 2e 20 49 66 20 73 75 63 63 65 73  leaf). If succes
358d5 73 66 75 6c 2c 0a 2a 2a 20 74 68 69 73 20 66 75  sful,.** this fu
358d6 6e 63 74 69 6f 6e 20 6c 6f 63 61 74 65 73 20 74  nction locates t
358d7 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 6f 66 20  he leaf node of 
358d8 74 68 65 20 73 65 67 6d 65 6e 74 20 74 68 61 74  the segment that
358d9 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 74 68 65   may contain the
358da 20 0a 2a 2a 20 74 65 72 6d 20 73 70 65 63 69 66   .** term specif
358db 69 65 64 20 62 79 20 61 72 67 75 6d 65 6e 74 73  ied by arguments
358dc 20 7a 54 65 72 6d 20 61 6e 64 20 6e 54 65 72 6d   zTerm and nTerm
358dd 20 61 6e 64 20 77 72 69 74 65 73 20 69 74 73 20   and writes its 
358de 62 6c 6f 63 6b 20 6e 75 6d 62 65 72 20 0a 2a 2a  block number .**
358df 20 74 6f 20 2a 70 69 4c 65 61 66 2e 0a 2a 2a 0a   to *piLeaf..**.
358e0 2a 2a 20 49 74 20 69 73 20 70 6f 73 73 69 62 6c  ** It is possibl
358e1 65 20 74 68 61 74 20 74 68 65 20 72 65 74 75 72  e that the retur
358e2 6e 65 64 20 6c 65 61 66 20 6e 6f 64 65 20 64 6f  ned leaf node do
358e3 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 74  es not contain t
358e4 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  he specified.** 
358e5 74 65 72 6d 2e 20 48 6f 77 65 76 65 72 2c 20 69  term. However, i
358e6 66 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64 6f  f the segment do
358e7 65 73 20 63 6f 6e 74 61 69 6e 20 73 61 69 64 20  es contain said 
358e8 74 65 72 6d 2c 20 69 74 20 69 73 20 73 74 6f 72  term, it is stor
358e9 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 69 64 65  ed on.** the ide
358ea 6e 74 69 66 69 65 64 20 6c 65 61 66 20 6e 6f 64  ntified leaf nod
358eb 65 2e 20 42 65 63 61 75 73 65 20 74 68 69 73 20  e. Because this 
358ec 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 69 6e  function only in
358ed 73 70 65 63 74 73 20 69 6e 74 65 72 69 6f 72 0a  spects interior.
358ee 2a 2a 20 73 65 67 6d 65 6e 74 20 6e 6f 64 65 73  ** segment nodes
358ef 20 28 61 6e 64 20 6e 65 76 65 72 20 6c 6f 61 64   (and never load
358f0 73 20 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 74  s leaf nodes int
358f1 6f 20 6d 65 6d 6f 72 79 29 2c 20 69 74 20 69 73  o memory), it is
358f2 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a 2a   not possible.**
358f3 20 74 6f 20 62 65 20 73 75 72 65 2e 0a 2a 2a 0a   to be sure..**.
358f4 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  ** If an error o
358f5 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f 72 20  ccurs, an error 
358f6 63 6f 64 65 20 6f 74 68 65 72 20 74 68 61 6e 20  code other than 
358f7 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
358f8 75 72 6e 65 64 2e 0a 2a 2f 20 0a 73 74 61 74 69  urned..*/ .stati
358f9 63 20 69 6e 74 20 66 74 73 33 53 65 6c 65 63 74  c int fts3Select
358fa 4c 65 61 66 28 0a 20 20 46 74 73 33 54 61 62 6c  Leaf(.  Fts3Tabl
358fb 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20  e *p,           
358fc 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75          /* Virtu
358fd 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20  al table handle 
358fe 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
358ff 2a 7a 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20  *zTerm,         
35900 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20       /* Term to 
35901 73 65 6c 65 63 74 20 6c 65 61 76 65 73 20 66 6f  select leaves fo
35902 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d  r */.  int nTerm
35903 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
35904 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
35905 66 20 74 65 72 6d 20 7a 54 65 72 6d 20 69 6e 20  f term zTerm in 
35906 62 79 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74  bytes */.  const
35907 20 63 68 61 72 20 2a 7a 4e 6f 64 65 2c 20 20 20   char *zNode,   
35908 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75             /* Bu
35909 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ffer containing 
3590a 73 65 67 6d 65 6e 74 20 69 6e 74 65 72 69 6f 72  segment interior
3590b 20 6e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e   node */.  int n
3590c 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20  Node,           
3590d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
3590e 7a 65 20 6f 66 20 62 75 66 66 65 72 20 61 74 20  ze of buffer at 
3590f 7a 4e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74  zNode */.  sqlit
35910 65 33 5f 69 6e 74 36 34 20 2a 70 69 4c 65 61 66  e3_int64 *piLeaf
35911 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
35912 6c 65 63 74 65 64 20 6c 65 61 66 20 6e 6f 64 65  lected leaf node
35913 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20   */.){.  int rc 
35914 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
35915 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
35916 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e  rn code */.  con
35917 73 74 20 63 68 61 72 20 2a 7a 43 73 72 20 3d 20  st char *zCsr = 
35918 7a 4e 6f 64 65 3b 20 20 20 20 20 20 20 2f 2a 20  zNode;       /* 
35919 43 75 72 73 6f 72 20 74 6f 20 69 74 65 72 61 74  Cursor to iterat
3591a 65 20 74 68 72 6f 75 67 68 20 6e 6f 64 65 20 2a  e through node *
3591b 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
3591c 7a 45 6e 64 20 3d 20 26 7a 43 73 72 5b 6e 4e 6f  zEnd = &zCsr[nNo
3591d 64 65 5d 3b 2f 2a 20 45 6e 64 20 6f 66 20 69 6e  de];/* End of in
3591e 74 65 72 69 6f 72 20 6e 6f 64 65 20 62 75 66 66  terior node buff
3591f 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42  er */.  char *zB
35920 75 66 66 65 72 20 3d 20 30 3b 20 20 20 20 20 20  uffer = 0;      
35921 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65          /* Buffe
35922 72 20 74 6f 20 6c 6f 61 64 20 74 65 72 6d 73 20  r to load terms 
35923 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  into */.  int nA
35924 6c 6c 6f 63 20 3d 20 30 3b 20 20 20 20 20 20 20  lloc = 0;       
35925 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
35926 65 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 62  e of allocated b
35927 75 66 66 65 72 20 2a 2f 0a 0a 20 20 77 68 69 6c  uffer */..  whil
35928 65 28 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20  e( 1 ){.    int 
35929 69 73 46 69 72 73 74 54 65 72 6d 20 3d 20 31 3b  isFirstTerm = 1;
3592a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
3592b 65 20 77 68 65 6e 20 70 72 6f 63 65 73 73 69 6e  e when processin
3592c 67 20 66 69 72 73 74 20 74 65 72 6d 20 6f 6e 20  g first term on 
3592d 70 61 67 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  page */.    int 
3592e 69 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  iHeight;        
3592f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 65 69            /* Hei
35930 67 68 74 20 6f 66 20 74 68 69 73 20 6e 6f 64 65  ght of this node
35931 20 69 6e 20 74 72 65 65 20 2a 2f 0a 20 20 20 20   in tree */.    
35932 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 43  sqlite3_int64 iC
35933 68 69 6c 64 3b 20 20 20 20 20 20 20 20 20 2f 2a  hild;         /*
35934 20 42 6c 6f 63 6b 20 69 64 20 6f 66 20 63 68 69   Block id of chi
35935 6c 64 20 6e 6f 64 65 20 74 6f 20 64 65 73 63 65  ld node to desce
35936 6e 64 20 74 6f 20 2a 2f 0a 20 20 20 20 69 6e 74  nd to */.    int
35937 20 6e 42 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20   nBlock;        
35938 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
35939 7a 65 20 6f 66 20 63 68 69 6c 64 20 6e 6f 64 65  ze of child node
3593a 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 0a 20 20   in bytes */..  
3593b 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65    zCsr += sqlite
3593c 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32  3Fts3GetVarint32
3593d 28 7a 43 73 72 2c 20 26 69 48 65 69 67 68 74 29  (zCsr, &iHeight)
3593e 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71  ;.    zCsr += sq
3593f 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69  lite3Fts3GetVari
35940 6e 74 28 7a 43 73 72 2c 20 26 69 43 68 69 6c 64  nt(zCsr, &iChild
35941 29 3b 0a 20 20 0a 20 20 20 20 77 68 69 6c 65 28  );.  .    while(
35942 20 7a 43 73 72 3c 7a 45 6e 64 20 29 7b 0a 20 20   zCsr<zEnd ){.  
35943 20 20 20 20 69 6e 74 20 63 6d 70 3b 20 20 20 20      int cmp;    
35944 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35945 2f 2a 20 6d 65 6d 63 6d 70 28 29 20 72 65 73 75  /* memcmp() resu
35946 6c 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  lt */.      int 
35947 6e 53 75 66 66 69 78 3b 20 20 20 20 20 20 20 20  nSuffix;        
35948 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
35949 6f 66 20 74 65 72 6d 20 73 75 66 66 69 78 20 2a  of term suffix *
3594a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 50 72 65  /.      int nPre
3594b 66 69 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20  fix = 0;        
3594c 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74      /* Size of t
3594d 65 72 6d 20 70 72 65 66 69 78 20 2a 2f 0a 20 20  erm prefix */.  
3594e 20 20 20 20 69 6e 74 20 6e 42 75 66 66 65 72 3b      int nBuffer;
3594f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35950 2f 2a 20 54 6f 74 61 6c 20 74 65 72 6d 20 73 69  /* Total term si
35951 7a 65 20 2a 2f 0a 20 20 0a 20 20 20 20 20 20 2f  ze */.  .      /
35952 2a 20 4c 6f 61 64 20 74 68 65 20 6e 65 78 74 20  * Load the next 
35953 74 65 72 6d 20 6f 6e 20 74 68 65 20 6e 6f 64 65  term on the node
35954 20 69 6e 74 6f 20 7a 42 75 66 66 65 72 20 2a 2f   into zBuffer */
35955 0a 20 20 20 20 20 20 69 66 28 20 21 69 73 46 69  .      if( !isFi
35956 72 73 74 54 65 72 6d 20 29 7b 0a 20 20 20 20 20  rstTerm ){.     
35957 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74     zCsr += sqlit
35958 65 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33  e3Fts3GetVarint3
35959 32 28 7a 43 73 72 2c 20 26 6e 50 72 65 66 69 78  2(zCsr, &nPrefix
3595a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
3595b 20 69 73 46 69 72 73 74 54 65 72 6d 20 3d 20 30   isFirstTerm = 0
3595c 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20  ;.      zCsr += 
3595d 73 71 6c 69 74 65 33 46 74 73 33 47 65 74 56 61  sqlite3Fts3GetVa
3595e 72 69 6e 74 33 32 28 7a 43 73 72 2c 20 26 6e 53  rint32(zCsr, &nS
3595f 75 66 66 69 78 29 3b 0a 20 20 20 20 20 20 69 66  uffix);.      if
35960 28 20 6e 50 72 65 66 69 78 2b 6e 53 75 66 66 69  ( nPrefix+nSuffi
35961 78 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  x>nAlloc ){.    
35962 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a      char *zNew;.
35963 20 20 20 20 20 20 20 20 6e 41 6c 6c 6f 63 20 3d          nAlloc =
35964 20 28 6e 50 72 65 66 69 78 2b 6e 53 75 66 66 69   (nPrefix+nSuffi
35965 78 29 20 2a 20 32 3b 0a 20 20 20 20 20 20 20 20  x) * 2;.        
35966 7a 4e 65 77 20 3d 20 28 63 68 61 72 20 2a 29 73  zNew = (char *)s
35967 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 7a  qlite3_realloc(z
35968 42 75 66 66 65 72 2c 20 6e 41 6c 6c 6f 63 29 3b  Buffer, nAlloc);
35969 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 7a 4e  .        if( !zN
3596a 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ew ){.          
3596b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 42 75  sqlite3_free(zBu
3596c 66 66 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20  ffer);.         
3596d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
3596e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 7d 0a  OMEM;.        }.
3596f 20 20 20 20 20 20 20 20 7a 42 75 66 66 65 72 20          zBuffer 
35970 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a  = zNew;.      }.
35971 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42        memcpy(&zB
35972 75 66 66 65 72 5b 6e 50 72 65 66 69 78 5d 2c 20  uffer[nPrefix], 
35973 7a 43 73 72 2c 20 6e 53 75 66 66 69 78 29 3b 0a  zCsr, nSuffix);.
35974 20 20 20 20 20 20 6e 42 75 66 66 65 72 20 3d 20        nBuffer = 
35975 6e 50 72 65 66 69 78 20 2b 20 6e 53 75 66 66 69  nPrefix + nSuffi
35976 78 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d  x;.      zCsr +=
35977 20 6e 53 75 66 66 69 78 3b 0a 20 20 0a 20 20 20   nSuffix;.  .   
35978 20 20 20 2f 2a 20 43 6f 6d 70 61 72 65 20 74 68     /* Compare th
35979 65 20 74 65 72 6d 20 77 65 20 61 72 65 20 73 65  e term we are se
3597a 61 72 63 68 69 6e 67 20 66 6f 72 20 77 69 74 68  arching for with
3597b 20 74 68 65 20 74 65 72 6d 20 6a 75 73 74 20 6c   the term just l
3597c 6f 61 64 65 64 20 66 72 6f 6d 0a 20 20 20 20 20  oaded from.     
3597d 20 2a 2a 20 74 68 65 20 69 6e 74 65 72 69 6f 72   ** the interior
3597e 20 6e 6f 64 65 2e 20 49 66 20 74 68 65 20 73 70   node. If the sp
3597f 65 63 69 66 69 65 64 20 74 65 72 6d 20 69 73 20  ecified term is 
35980 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
35981 65 71 75 61 6c 0a 20 20 20 20 20 20 2a 2a 20 74  equal.      ** t
35982 6f 20 74 68 65 20 74 65 72 6d 20 66 72 6f 6d 20  o the term from 
35983 74 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  the interior nod
35984 65 2c 20 74 68 65 6e 20 61 6c 6c 20 74 65 72 6d  e, then all term
35985 73 20 6f 6e 20 74 68 65 20 73 75 62 2d 74 72 65  s on the sub-tre
35986 65 20 0a 20 20 20 20 20 20 2a 2a 20 68 65 61 64  e .      ** head
35987 65 64 20 62 79 20 6e 6f 64 65 20 69 43 68 69 6c  ed by node iChil
35988 64 20 61 72 65 20 73 6d 61 6c 6c 65 72 20 74 68  d are smaller th
35989 61 6e 20 7a 54 65 72 6d 2e 20 4e 6f 20 6e 65 65  an zTerm. No nee
3598a 64 20 74 6f 20 73 65 61 72 63 68 20 0a 20 20 20  d to search .   
3598b 20 20 20 2a 2a 20 69 43 68 69 6c 64 2e 0a 20 20     ** iChild..  
3598c 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
3598d 49 66 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20  If the interior 
3598e 6e 6f 64 65 20 74 65 72 6d 20 69 73 20 6c 61 72  node term is lar
3598f 67 65 72 20 74 68 61 6e 20 74 68 65 20 73 70 65  ger than the spe
35990 63 69 66 69 65 64 20 74 65 72 6d 2c 20 74 68 65  cified term, the
35991 6e 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 74  n.      ** the t
35992 72 65 65 20 68 65 61 64 65 64 20 62 79 20 69 43  ree headed by iC
35993 68 69 6c 64 20 6d 61 79 20 63 6f 6e 74 61 69 6e  hild may contain
35994 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 74   the specified t
35995 65 72 6d 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  erm..      */.  
35996 20 20 20 20 63 6d 70 20 3d 20 6d 65 6d 63 6d 70      cmp = memcmp
35997 28 7a 54 65 72 6d 2c 20 7a 42 75 66 66 65 72 2c  (zTerm, zBuffer,
35998 20 28 6e 42 75 66 66 65 72 3e 6e 54 65 72 6d 20   (nBuffer>nTerm 
35999 3f 20 6e 54 65 72 6d 20 3a 20 6e 42 75 66 66 65  ? nTerm : nBuffe
3599a 72 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63  r));.      if( c
3599b 6d 70 3c 30 20 7c 7c 20 28 63 6d 70 3d 3d 30 20  mp<0 || (cmp==0 
3599c 26 26 20 6e 42 75 66 66 65 72 3e 6e 54 65 72 6d  && nBuffer>nTerm
3599d 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  ) ) break;.     
3599e 20 69 43 68 69 6c 64 2b 2b 3b 0a 20 20 20 20 7d   iChild++;.    }
3599f 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 28 69 48  ;..    /* If (iH
359a0 65 69 67 68 74 3d 3d 31 29 2c 20 74 68 65 20 63  eight==1), the c
359a1 68 69 6c 64 72 65 6e 20 6f 66 20 74 68 69 73 20  hildren of this 
359a2 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 61 72  interior node ar
359a3 65 20 6c 65 61 76 65 73 2e 20 54 68 65 0a 20 20  e leaves. The.  
359a4 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64 20 74    ** specified t
359a5 65 72 6d 20 6d 61 79 20 62 65 20 70 72 65 73 65  erm may be prese
359a6 6e 74 20 6f 6e 20 6c 65 61 66 20 6e 6f 64 65 20  nt on leaf node 
359a7 69 43 68 69 6c 64 2e 0a 20 20 20 20 2a 2f 0a 20  iChild..    */. 
359a8 20 20 20 69 66 28 20 69 48 65 69 67 68 74 3d 3d     if( iHeight==
359a9 31 20 29 7b 0a 20 20 20 20 20 20 2a 70 69 4c 65  1 ){.      *piLe
359aa 61 66 20 3d 20 69 43 68 69 6c 64 3b 0a 20 20 20  af = iChild;.   
359ab 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
359ac 0a 20 20 20 20 2f 2a 20 44 65 73 63 65 6e 64 20  .    /* Descend 
359ad 74 6f 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  to interior node
359ae 20 69 43 68 69 6c 64 2e 20 2a 2f 0a 20 20 20 20   iChild. */.    
359af 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
359b0 52 65 61 64 42 6c 6f 63 6b 28 70 2c 20 69 43 68  ReadBlock(p, iCh
359b1 69 6c 64 2c 20 26 7a 43 73 72 2c 20 26 6e 42 6c  ild, &zCsr, &nBl
359b2 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ock);.    if( rc
359b3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72  !=SQLITE_OK ) br
359b4 65 61 6b 3b 0a 20 20 20 20 7a 45 6e 64 20 3d 20  eak;.    zEnd = 
359b5 26 7a 43 73 72 5b 6e 42 6c 6f 63 6b 5d 3b 0a 20  &zCsr[nBlock];. 
359b6 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65   }.  sqlite3_fre
359b7 65 28 7a 42 75 66 66 65 72 29 3b 0a 20 20 72 65  e(zBuffer);.  re
359b8 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
359b9 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
359ba 69 73 20 75 73 65 64 20 74 6f 20 63 72 65 61 74  is used to creat
359bb 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 20  e delta-encoded 
359bc 73 65 72 69 61 6c 69 7a 65 64 20 6c 69 73 74 73  serialized lists
359bd 20 6f 66 20 46 54 53 33 20 0a 2a 2a 20 76 61 72   of FTS3 .** var
359be 69 6e 74 73 2e 20 45 61 63 68 20 63 61 6c 6c 20  ints. Each call 
359bf 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
359c0 20 61 70 70 65 6e 64 73 20 61 20 73 69 6e 67 6c   appends a singl
359c1 65 20 76 61 72 69 6e 74 20 74 6f 20 61 20 6c 69  e varint to a li
359c2 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  st..*/.static vo
359c3 69 64 20 66 74 73 33 50 75 74 44 65 6c 74 61 56  id fts3PutDeltaV
359c4 61 72 69 6e 74 28 0a 20 20 63 68 61 72 20 2a 2a  arint(.  char **
359c5 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pp,             
359c6 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
359c7 55 54 3a 20 4f 75 74 70 75 74 20 70 6f 69 6e 74  UT: Output point
359c8 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  er */.  sqlite3_
359c9 69 6e 74 36 34 20 2a 70 69 50 72 65 76 2c 20 20  int64 *piPrev,  
359ca 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55          /* IN/OU
359cb 54 3a 20 50 72 65 76 69 6f 75 73 20 76 61 6c 75  T: Previous valu
359cc 65 20 77 72 69 74 74 65 6e 20 74 6f 20 6c 69 73  e written to lis
359cd 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  t */.  sqlite3_i
359ce 6e 74 36 34 20 69 56 61 6c 20 20 20 20 20 20 20  nt64 iVal       
359cf 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
359d0 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 74 68  this value to th
359d1 65 20 6c 69 73 74 20 2a 2f 0a 29 7b 0a 20 20 61  e list */.){.  a
359d2 73 73 65 72 74 28 20 69 56 61 6c 2d 2a 70 69 50  ssert( iVal-*piP
359d3 72 65 76 20 3e 20 30 20 7c 7c 20 28 2a 70 69 50  rev > 0 || (*piP
359d4 72 65 76 3d 3d 30 20 26 26 20 69 56 61 6c 3d 3d  rev==0 && iVal==
359d5 30 29 20 29 3b 0a 20 20 2a 70 70 20 2b 3d 20 73  0) );.  *pp += s
359d6 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72  qlite3Fts3PutVar
359d7 69 6e 74 28 2a 70 70 2c 20 69 56 61 6c 2d 2a 70  int(*pp, iVal-*p
359d8 69 50 72 65 76 29 3b 0a 20 20 2a 70 69 50 72 65  iPrev);.  *piPre
359d9 76 20 3d 20 69 56 61 6c 3b 0a 7d 0a 0a 73 74 61  v = iVal;.}..sta
359da 74 69 63 20 76 6f 69 64 20 66 74 73 33 50 6f 73  tic void fts3Pos
359db 6c 69 73 74 43 6f 70 79 28 63 68 61 72 20 2a 2a  listCopy(char **
359dc 70 70 2c 20 63 68 61 72 20 2a 2a 70 70 50 6f 73  pp, char **ppPos
359dd 6c 69 73 74 29 7b 0a 20 20 63 68 61 72 20 2a 70  list){.  char *p
359de 45 6e 64 20 3d 20 2a 70 70 50 6f 73 6c 69 73 74  End = *ppPoslist
359df 3b 0a 20 20 63 68 61 72 20 63 20 3d 20 30 3b 0a  ;.  char c = 0;.
359e0 20 20 77 68 69 6c 65 28 20 2a 70 45 6e 64 20 7c    while( *pEnd |
359e1 20 63 20 29 20 63 20 3d 20 2a 70 45 6e 64 2b 2b   c ) c = *pEnd++
359e2 20 26 20 30 78 38 30 3b 0a 20 20 70 45 6e 64 2b   & 0x80;.  pEnd+
359e3 2b 3b 0a 20 20 69 66 28 20 70 70 20 29 7b 0a 20  +;.  if( pp ){. 
359e4 20 20 20 69 6e 74 20 6e 20 3d 20 28 69 6e 74 29     int n = (int)
359e5 28 70 45 6e 64 20 2d 20 2a 70 70 50 6f 73 6c 69  (pEnd - *ppPosli
359e6 73 74 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 70  st);.    char *p
359e7 20 3d 20 2a 70 70 3b 0a 20 20 20 20 6d 65 6d 63   = *pp;.    memc
359e8 70 79 28 70 2c 20 2a 70 70 50 6f 73 6c 69 73 74  py(p, *ppPoslist
359e9 2c 20 6e 29 3b 0a 20 20 20 20 70 20 2b 3d 20 6e  , n);.    p += n
359ea 3b 0a 20 20 20 20 2a 70 70 20 3d 20 70 3b 0a 20  ;.    *pp = p;. 
359eb 20 7d 0a 20 20 2a 70 70 50 6f 73 6c 69 73 74 20   }.  *ppPoslist 
359ec 3d 20 70 45 6e 64 3b 0a 7d 0a 0a 73 74 61 74 69  = pEnd;.}..stati
359ed 63 20 76 6f 69 64 20 66 74 73 33 43 6f 6c 75 6d  c void fts3Colum
359ee 6e 6c 69 73 74 43 6f 70 79 28 63 68 61 72 20 2a  nlistCopy(char *
359ef 2a 70 70 2c 20 63 68 61 72 20 2a 2a 70 70 50 6f  *pp, char **ppPo
359f0 73 6c 69 73 74 29 7b 0a 20 20 63 68 61 72 20 2a  slist){.  char *
359f1 70 45 6e 64 20 3d 20 2a 70 70 50 6f 73 6c 69 73  pEnd = *ppPoslis
359f2 74 3b 0a 20 20 63 68 61 72 20 63 20 3d 20 30 3b  t;.  char c = 0;
359f3 0a 0a 20 20 2f 2a 20 41 20 63 6f 6c 75 6d 6e 2d  ..  /* A column-
359f4 6c 69 73 74 20 69 73 20 74 65 72 6d 69 6e 61 74  list is terminat
359f5 65 64 20 62 79 20 65 69 74 68 65 72 20 61 20 30  ed by either a 0
359f6 78 30 31 20 6f 72 20 30 78 30 30 2e 20 2a 2f 0a  x01 or 0x00. */.
359f7 20 20 77 68 69 6c 65 28 20 30 78 46 45 20 26 20    while( 0xFE & 
359f8 28 2a 70 45 6e 64 20 7c 20 63 29 20 29 20 63 20  (*pEnd | c) ) c 
359f9 3d 20 2a 70 45 6e 64 2b 2b 20 26 20 30 78 38 30  = *pEnd++ & 0x80
359fa 3b 0a 20 20 69 66 28 20 70 70 20 29 7b 0a 20 20  ;.  if( pp ){.  
359fb 20 20 69 6e 74 20 6e 20 3d 20 28 69 6e 74 29 28    int n = (int)(
359fc 70 45 6e 64 20 2d 20 2a 70 70 50 6f 73 6c 69 73  pEnd - *ppPoslis
359fd 74 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 20  t);.    char *p 
359fe 3d 20 2a 70 70 3b 0a 20 20 20 20 6d 65 6d 63 70  = *pp;.    memcp
359ff 79 28 70 2c 20 2a 70 70 50 6f 73 6c 69 73 74 2c  y(p, *ppPoslist,
35a00 20 6e 29 3b 0a 20 20 20 20 70 20 2b 3d 20 6e 3b   n);.    p += n;
35a01 0a 20 20 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20  .    *pp = p;.  
35a02 7d 0a 20 20 2a 70 70 50 6f 73 6c 69 73 74 20 3d  }.  *ppPoslist =
35a03 20 70 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pEnd;.}../*.** 
35a04 56 61 6c 75 65 20 75 73 65 64 20 74 6f 20 73 69  Value used to si
35a05 67 6e 69 66 79 20 74 68 65 20 65 6e 64 20 6f 66  gnify the end of
35a06 20 61 6e 20 6f 66 66 73 65 74 2d 6c 69 73 74 2e   an offset-list.
35a07 20 54 68 69 73 20 69 73 20 73 61 66 65 20 62 65   This is safe be
35a08 63 61 75 73 65 0a 2a 2a 20 69 74 20 69 73 20 6e  cause.** it is n
35a09 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 68  ot possible to h
35a0a 61 76 65 20 61 20 64 6f 63 75 6d 65 6e 74 20 77  ave a document w
35a0b 69 74 68 20 32 5e 33 31 20 74 65 72 6d 73 2e 0a  ith 2^31 terms..
35a0c 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 46 46 53 45  */.#define OFFSE
35a0d 54 5f 4c 49 53 54 5f 45 4e 44 20 30 78 37 66 66  T_LIST_END 0x7ff
35a0e 66 66 66 66 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  fffff../*.** Thi
35a0f 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
35a10 65 64 20 74 6f 20 68 65 6c 70 20 70 61 72 73 65  ed to help parse
35a11 20 6f 66 66 73 65 74 2d 6c 69 73 74 73 2e 20 57   offset-lists. W
35a12 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
35a13 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2c 20  n is.** called, 
35a14 2a 70 70 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f  *pp may point to
35a15 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68   the start of th
35a16 65 20 6e 65 78 74 20 76 61 72 69 6e 74 20 69 6e  e next varint in
35a17 20 74 68 65 20 6f 66 66 73 65 74 2d 6c 69 73 74   the offset-list
35a18 0a 2a 2a 20 62 65 69 6e 67 20 70 61 72 73 65 64  .** being parsed
35a19 2c 20 6f 72 20 69 74 20 6d 61 79 20 70 6f 69 6e  , or it may poin
35a1a 74 20 74 6f 20 31 20 62 79 74 65 20 70 61 73 74  t to 1 byte past
35a1b 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
35a1c 6f 66 66 73 65 74 2d 6c 69 73 74 0a 2a 2a 20 28  offset-list.** (
35a1d 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 2a 2a  in which case **
35a1e 70 70 20 77 69 6c 6c 20 62 65 20 30 78 30 30 20  pp will be 0x00 
35a1f 6f 72 20 30 78 30 31 29 2e 0a 2a 2a 0a 2a 2a 20  or 0x01)..**.** 
35a20 49 66 20 2a 70 70 20 70 6f 69 6e 74 73 20 70 61  If *pp points pa
35a21 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  st the end of th
35a22 65 20 63 75 72 72 65 6e 74 20 6f 66 66 73 65 74  e current offset
35a23 20 6c 69 73 74 2c 20 73 65 74 20 2a 70 69 20 74   list, set *pi t
35a24 6f 20 0a 2a 2a 20 4f 46 46 53 45 54 5f 4c 49 53  o .** OFFSET_LIS
35a25 54 5f 45 4e 44 20 61 6e 64 20 72 65 74 75 72 6e  T_END and return
35a26 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 61  . Otherwise, rea
35a27 64 20 74 68 65 20 6e 65 78 74 20 76 61 72 69 6e  d the next varin
35a28 74 20 66 72 6f 6d 20 2a 70 70 2c 0a 2a 2a 20 69  t from *pp,.** i
35a29 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 63 75 72  ncrement the cur
35a2a 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 2a 70  rent value of *p
35a2b 69 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 72  i by the value r
35a2c 65 61 64 2c 20 61 6e 64 20 73 65 74 20 2a 70 70  ead, and set *pp
35a2d 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20   to.** point to 
35a2e 74 68 65 20 6e 65 78 74 20 76 61 6c 75 65 20 62  the next value b
35a2f 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
35a30 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
35a31 66 74 73 33 52 65 61 64 4e 65 78 74 50 6f 73 28  fts3ReadNextPos(
35a32 0a 20 20 63 68 61 72 20 2a 2a 70 70 2c 20 20 20  .  char **pp,   
35a33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a34 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50 6f     /* IN/OUT: Po
35a35 69 6e 74 65 72 20 69 6e 74 6f 20 6f 66 66 73 65  inter into offse
35a36 74 2d 6c 69 73 74 20 62 75 66 66 65 72 20 2a 2f  t-list buffer */
35a37 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
35a38 20 2a 70 69 20 20 20 20 20 20 20 20 20 20 20 20   *pi            
35a39 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20 56 61     /* IN/OUT: Va
35a3a 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 6f 66  lue read from of
35a3b 66 73 65 74 2d 6c 69 73 74 20 2a 2f 0a 29 7b 0a  fset-list */.){.
35a3c 20 20 69 66 28 20 2a 2a 70 70 26 30 78 46 45 20    if( **pp&0xFE 
35a3d 29 7b 0a 20 20 20 20 66 74 73 33 47 65 74 44 65  ){.    fts3GetDe
35a3e 6c 74 61 56 61 72 69 6e 74 28 70 70 2c 20 70 69  ltaVarint(pp, pi
35a3f 29 3b 0a 20 20 20 20 2a 70 69 20 2d 3d 20 32 3b  );.    *pi -= 2;
35a40 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70  .  }else{.    *p
35a41 69 20 3d 20 4f 46 46 53 45 54 5f 4c 49 53 54 5f  i = OFFSET_LIST_
35a42 45 4e 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  END;.  }.}../*.*
35a43 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20 69  * If parameter i
35a44 43 6f 6c 20 69 73 20 6e 6f 74 20 30 2c 20 77 72  Col is not 0, wr
35a45 69 74 65 20 61 6e 20 30 78 30 31 20 62 79 74 65  ite an 0x01 byte
35a46 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 68 65   followed by the
35a47 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 69 43 6f   value of.** iCo
35a48 6c 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 76  l encoded as a v
35a49 61 72 69 6e 74 20 74 6f 20 2a 70 70 2e 20 0a 2a  arint to *pp. .*
35a4a 2a 0a 2a 2a 20 53 65 74 20 2a 70 70 20 74 6f 20  *.** Set *pp to 
35a4b 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 62 79 74  point to the byt
35a4c 65 20 6a 75 73 74 20 61 66 74 65 72 20 74 68 65  e just after the
35a4d 20 6c 61 73 74 20 62 79 74 65 20 77 72 69 74 74   last byte writt
35a4e 65 6e 20 62 65 66 6f 72 65 20 0a 2a 2a 20 72 65  en before .** re
35a4f 74 75 72 6e 69 6e 67 20 28 64 6f 20 6e 6f 74 20  turning (do not 
35a50 6d 6f 64 69 66 79 20 69 74 20 69 66 20 69 43 6f  modify it if iCo
35a51 6c 3d 3d 30 29 2e 20 52 65 74 75 72 6e 20 74 68  l==0). Return th
35a52 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
35a53 66 20 62 79 74 65 73 0a 2a 2a 20 77 72 69 74 74  f bytes.** writt
35a54 65 6e 20 28 30 20 69 66 20 69 43 6f 6c 3d 3d 30  en (0 if iCol==0
35a55 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
35a56 20 66 74 73 33 50 75 74 43 6f 6c 4e 75 6d 62 65   fts3PutColNumbe
35a57 72 28 63 68 61 72 20 2a 2a 70 70 2c 20 69 6e 74  r(char **pp, int
35a58 20 69 43 6f 6c 29 7b 0a 20 20 69 6e 74 20 6e 20   iCol){.  int n 
35a59 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
35a5a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
35a5b 62 65 72 20 6f 66 20 62 79 74 65 73 20 77 72 69  ber of bytes wri
35a5c 74 74 65 6e 20 2a 2f 0a 20 20 69 66 28 20 69 43  tten */.  if( iC
35a5d 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  ol ){.    char *
35a5e 70 20 3d 20 2a 70 70 3b 20 20 20 20 20 20 20 20  p = *pp;        
35a5f 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
35a60 74 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20  t pointer */.   
35a61 20 6e 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33   n = 1 + sqlite3
35a62 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70  Fts3PutVarint(&p
35a63 5b 31 5d 2c 20 69 43 6f 6c 29 3b 0a 20 20 20 20  [1], iCol);.    
35a64 2a 70 20 3d 20 30 78 30 31 3b 0a 20 20 20 20 2a  *p = 0x01;.    *
35a65 70 70 20 3d 20 26 70 5b 6e 5d 3b 0a 20 20 7d 0a  pp = &p[n];.  }.
35a66 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f    return n;.}../
35a67 2a 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 76  *.**.*/.static v
35a68 6f 69 64 20 66 74 73 33 50 6f 73 6c 69 73 74 4d  oid fts3PoslistM
35a69 65 72 67 65 28 0a 20 20 63 68 61 72 20 2a 2a 70  erge(.  char **p
35a6a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
35a6b 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
35a6c 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 63 68  t buffer */.  ch
35a6d 61 72 20 2a 2a 70 70 31 2c 20 20 20 20 20 20 20  ar **pp1,       
35a6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35a6f 20 4c 65 66 74 20 69 6e 70 75 74 20 6c 69 73 74   Left input list
35a70 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70 32   */.  char **pp2
35a71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a72 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 69        /* Right i
35a73 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0a 29 7b 0a  nput list */.){.
35a74 20 20 63 68 61 72 20 2a 70 20 3d 20 2a 70 70 3b    char *p = *pp;
35a75 0a 20 20 63 68 61 72 20 2a 70 31 20 3d 20 2a 70  .  char *p1 = *p
35a76 70 31 3b 0a 20 20 63 68 61 72 20 2a 70 32 20 3d  p1;.  char *p2 =
35a77 20 2a 70 70 32 3b 0a 0a 20 20 77 68 69 6c 65 28   *pp2;..  while(
35a78 20 2a 70 31 20 7c 7c 20 2a 70 32 20 29 7b 0a 20   *p1 || *p2 ){. 
35a79 20 20 20 69 6e 74 20 69 43 6f 6c 31 3b 0a 20 20     int iCol1;.  
35a7a 20 20 69 6e 74 20 69 43 6f 6c 32 3b 0a 0a 20 20    int iCol2;..  
35a7b 20 20 69 66 28 20 2a 70 31 3d 3d 30 78 30 31 20    if( *p1==0x01 
35a7c 29 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  ) sqlite3Fts3Get
35a7d 56 61 72 69 6e 74 33 32 28 26 70 31 5b 31 5d 2c  Varint32(&p1[1],
35a7e 20 26 69 43 6f 6c 31 29 3b 0a 20 20 20 20 65 6c   &iCol1);.    el
35a7f 73 65 20 69 66 28 20 2a 70 31 3d 3d 30 78 30 30  se if( *p1==0x00
35a80 20 29 20 69 43 6f 6c 31 20 3d 20 4f 46 46 53 45   ) iCol1 = OFFSE
35a81 54 5f 4c 49 53 54 5f 45 4e 44 3b 0a 20 20 20 20  T_LIST_END;.    
35a82 65 6c 73 65 20 69 43 6f 6c 31 20 3d 20 30 3b 0a  else iCol1 = 0;.
35a83 0a 20 20 20 20 69 66 28 20 2a 70 32 3d 3d 30 78  .    if( *p2==0x
35a84 30 31 20 29 20 73 71 6c 69 74 65 33 46 74 73 33  01 ) sqlite3Fts3
35a85 47 65 74 56 61 72 69 6e 74 33 32 28 26 70 32 5b  GetVarint32(&p2[
35a86 31 5d 2c 20 26 69 43 6f 6c 32 29 3b 0a 20 20 20  1], &iCol2);.   
35a87 20 65 6c 73 65 20 69 66 28 20 2a 70 32 3d 3d 30   else if( *p2==0
35a88 78 30 30 20 29 20 69 43 6f 6c 32 20 3d 20 4f 46  x00 ) iCol2 = OF
35a89 46 53 45 54 5f 4c 49 53 54 5f 45 4e 44 3b 0a 20  FSET_LIST_END;. 
35a8a 20 20 20 65 6c 73 65 20 69 43 6f 6c 32 20 3d 20     else iCol2 = 
35a8b 30 3b 0a 0a 20 20 20 20 69 66 28 20 69 43 6f 6c  0;..    if( iCol
35a8c 31 3d 3d 69 43 6f 6c 32 20 29 7b 0a 20 20 20 20  1==iCol2 ){.    
35a8d 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
35a8e 69 31 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  i1 = 0;.      sq
35a8f 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 32 20 3d  lite3_int64 i2 =
35a90 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   0;.      sqlite
35a91 33 5f 69 6e 74 36 34 20 69 50 72 65 76 20 3d 20  3_int64 iPrev = 
35a92 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d  0;.      int n =
35a93 20 66 74 73 33 50 75 74 43 6f 6c 4e 75 6d 62 65   fts3PutColNumbe
35a94 72 28 26 70 2c 20 69 43 6f 6c 31 29 3b 0a 20 20  r(&p, iCol1);.  
35a95 20 20 20 20 70 31 20 2b 3d 20 6e 3b 0a 20 20 20      p1 += n;.   
35a96 20 20 20 70 32 20 2b 3d 20 6e 3b 0a 0a 20 20 20     p2 += n;..   
35a97 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f     /* At this po
35a98 69 6e 74 2c 20 62 6f 74 68 20 70 31 20 61 6e 64  int, both p1 and
35a99 20 70 32 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   p2 point to the
35a9a 20 73 74 61 72 74 20 6f 66 20 6f 66 66 73 65 74   start of offset
35a9b 2d 6c 69 73 74 73 2e 0a 20 20 20 20 20 20 2a 2a  -lists..      **
35a9c 20 41 6e 20 6f 66 66 73 65 74 2d 6c 69 73 74 20   An offset-list 
35a9d 69 73 20 61 20 6c 69 73 74 20 6f 66 20 6e 6f 6e  is a list of non
35a9e 2d 6e 65 67 61 74 69 76 65 20 64 65 6c 74 61 2d  -negative delta-
35a9f 65 6e 63 6f 64 65 64 20 76 61 72 69 6e 74 73 2c  encoded varints,
35aa0 20 65 61 63 68 20 0a 20 20 20 20 20 20 2a 2a 20   each .      ** 
35aa1 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 32  incremented by 2
35aa2 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 73 74   before being st
35aa3 6f 72 65 64 2e 20 45 61 63 68 20 6c 69 73 74 20  ored. Each list 
35aa4 69 73 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79  is terminated by
35aa5 20 61 20 30 20 0a 20 20 20 20 20 20 2a 2a 20 6f   a 0 .      ** o
35aa6 72 20 31 20 76 61 6c 75 65 20 28 30 78 30 30 20  r 1 value (0x00 
35aa7 6f 72 20 30 78 30 31 29 2e 20 54 68 65 20 66 6f  or 0x01). The fo
35aa8 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6d 65  llowing block me
35aa9 72 67 65 73 20 74 68 65 20 74 77 6f 20 6c 69 73  rges the two lis
35aaa 74 73 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  ts.      ** and 
35aab 77 72 69 74 65 73 20 74 68 65 20 72 65 73 75 6c  writes the resul
35aac 74 73 20 74 6f 20 62 75 66 66 65 72 20 70 2e 20  ts to buffer p. 
35aad 70 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  p is left pointi
35aae 6e 67 20 74 6f 20 74 68 65 20 62 79 74 65 0a 20  ng to the byte. 
35aaf 20 20 20 20 20 2a 2a 20 61 66 74 65 72 20 74 68       ** after th
35ab0 65 20 6c 69 73 74 20 77 72 69 74 74 65 6e 2e 20  e list written. 
35ab1 4e 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 28 30  No terminator (0
35ab2 78 30 30 20 6f 72 20 30 78 30 31 29 20 69 73 20  x00 or 0x01) is 
35ab3 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 20 20 20  written to.     
35ab4 20 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2e 0a   ** the output..
35ab5 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 66        */.      f
35ab6 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e  ts3GetDeltaVarin
35ab7 74 28 26 70 31 2c 20 26 69 31 29 3b 0a 20 20 20  t(&p1, &i1);.   
35ab8 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
35ab9 61 72 69 6e 74 28 26 70 32 2c 20 26 69 32 29 3b  arint(&p2, &i2);
35aba 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  .      do {.    
35abb 20 20 20 20 66 74 73 33 50 75 74 44 65 6c 74 61      fts3PutDelta
35abc 56 61 72 69 6e 74 28 26 70 2c 20 26 69 50 72 65  Varint(&p, &iPre
35abd 76 2c 20 28 69 31 3c 69 32 29 20 3f 20 69 31 20  v, (i1<i2) ? i1 
35abe 3a 20 69 32 29 3b 20 0a 20 20 20 20 20 20 20 20  : i2); .        
35abf 69 50 72 65 76 20 2d 3d 20 32 3b 0a 20 20 20 20  iPrev -= 2;.    
35ac0 20 20 20 20 69 66 28 20 69 31 3d 3d 69 32 20 29      if( i1==i2 )
35ac1 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  {.          fts3
35ac2 52 65 61 64 4e 65 78 74 50 6f 73 28 26 70 31 2c  ReadNextPos(&p1,
35ac3 20 26 69 31 29 3b 0a 20 20 20 20 20 20 20 20 20   &i1);.         
35ac4 20 66 74 73 33 52 65 61 64 4e 65 78 74 50 6f 73   fts3ReadNextPos
35ac5 28 26 70 32 2c 20 26 69 32 29 3b 0a 20 20 20 20  (&p2, &i2);.    
35ac6 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 69 31      }else if( i1
35ac7 3c 69 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20  <i2 ){.         
35ac8 20 66 74 73 33 52 65 61 64 4e 65 78 74 50 6f 73   fts3ReadNextPos
35ac9 28 26 70 31 2c 20 26 69 31 29 3b 0a 20 20 20 20  (&p1, &i1);.    
35aca 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
35acb 20 20 20 20 20 66 74 73 33 52 65 61 64 4e 65 78       fts3ReadNex
35acc 74 50 6f 73 28 26 70 32 2c 20 26 69 32 29 3b 0a  tPos(&p2, &i2);.
35acd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
35ace 7d 77 68 69 6c 65 28 20 69 31 21 3d 4f 46 46 53  }while( i1!=OFFS
35acf 45 54 5f 4c 49 53 54 5f 45 4e 44 20 7c 7c 20 69  ET_LIST_END || i
35ad0 32 21 3d 4f 46 46 53 45 54 5f 4c 49 53 54 5f 45  2!=OFFSET_LIST_E
35ad1 4e 44 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  ND );.    }else 
35ad2 69 66 28 20 69 43 6f 6c 31 3c 69 43 6f 6c 32 20  if( iCol1<iCol2 
35ad3 29 7b 0a 20 20 20 20 20 20 70 31 20 2b 3d 20 66  ){.      p1 += f
35ad4 74 73 33 50 75 74 43 6f 6c 4e 75 6d 62 65 72 28  ts3PutColNumber(
35ad5 26 70 2c 20 69 43 6f 6c 31 29 3b 0a 20 20 20 20  &p, iCol1);.    
35ad6 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69 73 74    fts3Columnlist
35ad7 43 6f 70 79 28 26 70 2c 20 26 70 31 29 3b 0a 20  Copy(&p, &p1);. 
35ad8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
35ad9 70 32 20 2b 3d 20 66 74 73 33 50 75 74 43 6f 6c  p2 += fts3PutCol
35ada 4e 75 6d 62 65 72 28 26 70 2c 20 69 43 6f 6c 32  Number(&p, iCol2
35adb 29 3b 0a 20 20 20 20 20 20 66 74 73 33 43 6f 6c  );.      fts3Col
35adc 75 6d 6e 6c 69 73 74 43 6f 70 79 28 26 70 2c 20  umnlistCopy(&p, 
35add 26 70 32 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  &p2);.    }.  }.
35ade 0a 20 20 2a 70 2b 2b 20 3d 20 27 5c 30 27 3b 0a  .  *p++ = '\0';.
35adf 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 2a 70 70    *pp = p;.  *pp
35ae0 31 20 3d 20 70 31 20 2b 20 31 3b 0a 20 20 2a 70  1 = p1 + 1;.  *p
35ae1 70 32 20 3d 20 70 32 20 2b 20 31 3b 0a 7d 0a 0a  p2 = p2 + 1;.}..
35ae2 2f 2a 0a 2a 2a 20 6e 54 6f 6b 65 6e 3d 3d 31 20  /*.** nToken==1 
35ae3 73 65 61 72 63 68 65 73 20 66 6f 72 20 61 64 6a  searches for adj
35ae4 61 63 65 6e 74 20 70 6f 73 69 74 69 6f 6e 73 2e  acent positions.
35ae5 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
35ae6 74 73 33 50 6f 73 6c 69 73 74 50 68 72 61 73 65  ts3PoslistPhrase
35ae7 4d 65 72 67 65 28 0a 20 20 63 68 61 72 20 2a 2a  Merge(.  char **
35ae8 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pp,             
35ae9 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70           /* Outp
35aea 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69  ut buffer */.  i
35aeb 6e 74 20 6e 54 6f 6b 65 6e 2c 20 20 20 20 20 20  nt nToken,      
35aec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35aed 2a 20 4d 61 78 69 6d 75 6d 20 64 69 66 66 65 72  * Maximum differ
35aee 65 6e 63 65 20 69 6e 20 74 6f 6b 65 6e 20 70 6f  ence in token po
35aef 73 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  sitions */.  int
35af0 20 69 73 53 61 76 65 4c 65 66 74 2c 20 20 20 20   isSaveLeft,    
35af1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35af2 53 61 76 65 20 74 68 65 20 6c 65 66 74 20 70 6f  Save the left po
35af3 73 69 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72  sition */.  char
35af4 20 2a 2a 70 70 31 2c 20 20 20 20 20 20 20 20 20   **pp1,         
35af5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
35af6 65 66 74 20 69 6e 70 75 74 20 6c 69 73 74 20 2a  eft input list *
35af7 2f 0a 20 20 63 68 61 72 20 2a 2a 70 70 32 20 20  /.  char **pp2  
35af8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35af9 20 20 20 20 2f 2a 20 52 69 67 68 74 20 69 6e 70      /* Right inp
35afa 75 74 20 6c 69 73 74 20 2a 2f 0a 29 7b 0a 20 20  ut list */.){.  
35afb 63 68 61 72 20 2a 70 20 3d 20 28 70 70 20 3f 20  char *p = (pp ? 
35afc 2a 70 70 20 3a 20 30 29 3b 0a 20 20 63 68 61 72  *pp : 0);.  char
35afd 20 2a 70 31 20 3d 20 2a 70 70 31 3b 0a 20 20 63   *p1 = *pp1;.  c
35afe 68 61 72 20 2a 70 32 20 3d 20 2a 70 70 32 3b 0a  har *p2 = *pp2;.
35aff 0a 20 20 69 6e 74 20 69 43 6f 6c 31 20 3d 20 30  .  int iCol1 = 0
35b00 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 32 20 3d 20  ;.  int iCol2 = 
35b01 30 3b 0a 20 20 61 73 73 65 72 74 28 20 2a 70 31  0;.  assert( *p1
35b02 21 3d 30 20 26 26 20 2a 70 32 21 3d 30 20 29 3b  !=0 && *p2!=0 );
35b03 0a 20 20 69 66 28 20 2a 70 31 3d 3d 30 78 30 31  .  if( *p1==0x01
35b04 20 29 7b 20 0a 20 20 20 20 70 31 2b 2b 3b 0a 20   ){ .    p1++;. 
35b05 20 20 20 70 31 20 2b 3d 20 73 71 6c 69 74 65 33     p1 += sqlite3
35b06 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  Fts3GetVarint32(
35b07 70 31 2c 20 26 69 43 6f 6c 31 29 3b 0a 20 20 7d  p1, &iCol1);.  }
35b08 0a 20 20 69 66 28 20 2a 70 32 3d 3d 30 78 30 31  .  if( *p2==0x01
35b09 20 29 7b 20 0a 20 20 20 20 70 32 2b 2b 3b 0a 20   ){ .    p2++;. 
35b0a 20 20 20 70 32 20 2b 3d 20 73 71 6c 69 74 65 33     p2 += sqlite3
35b0b 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  Fts3GetVarint32(
35b0c 70 32 2c 20 26 69 43 6f 6c 32 29 3b 0a 20 20 7d  p2, &iCol2);.  }
35b0d 0a 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a  ..  while( 1 ){.
35b0e 20 20 20 20 69 66 28 20 69 43 6f 6c 31 3d 3d 69      if( iCol1==i
35b0f 43 6f 6c 32 20 29 7b 0a 20 20 20 20 20 20 63 68  Col2 ){.      ch
35b10 61 72 20 2a 70 53 61 76 65 20 3d 20 70 3b 0a 20  ar *pSave = p;. 
35b11 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
35b12 36 34 20 69 50 72 65 76 20 3d 20 30 3b 0a 20 20  64 iPrev = 0;.  
35b13 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
35b14 34 20 69 50 6f 73 31 20 3d 20 30 3b 0a 20 20 20  4 iPos1 = 0;.   
35b15 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
35b16 20 69 50 6f 73 32 20 3d 20 30 3b 0a 0a 20 20 20   iPos2 = 0;..   
35b17 20 20 20 69 66 28 20 70 70 20 26 26 20 69 43 6f     if( pp && iCo
35b18 6c 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 70  l1 ){.        *p
35b19 2b 2b 20 3d 20 30 78 30 31 3b 0a 20 20 20 20 20  ++ = 0x01;.     
35b1a 20 20 20 70 20 2b 3d 20 73 71 6c 69 74 65 33 46     p += sqlite3F
35b1b 74 73 33 50 75 74 56 61 72 69 6e 74 28 70 2c 20  ts3PutVarint(p, 
35b1c 69 43 6f 6c 31 29 3b 0a 20 20 20 20 20 20 7d 0a  iCol1);.      }.
35b1d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 2a  .      assert( *
35b1e 70 31 21 3d 30 78 30 30 20 26 26 20 2a 70 32 21  p1!=0x00 && *p2!
35b1f 3d 30 78 30 30 20 26 26 20 2a 70 31 21 3d 30 78  =0x00 && *p1!=0x
35b20 30 31 20 26 26 20 2a 70 32 21 3d 30 78 30 31 20  01 && *p2!=0x01 
35b21 29 3b 0a 20 20 20 20 20 20 66 74 73 33 47 65 74  );.      fts3Get
35b22 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70 31 2c  DeltaVarint(&p1,
35b23 20 26 69 50 6f 73 31 29 3b 20 69 50 6f 73 31 20   &iPos1); iPos1 
35b24 2d 3d 20 32 3b 0a 20 20 20 20 20 20 66 74 73 33  -= 2;.      fts3
35b25 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26  GetDeltaVarint(&
35b26 70 32 2c 20 26 69 50 6f 73 32 29 3b 20 69 50 6f  p2, &iPos2); iPo
35b27 73 32 20 2d 3d 20 32 3b 0a 0a 20 20 20 20 20 20  s2 -= 2;..      
35b28 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20  while( 1 ){.    
35b29 20 20 20 20 69 66 28 20 69 50 6f 73 32 3e 69 50      if( iPos2>iP
35b2a 6f 73 31 20 26 26 20 69 50 6f 73 32 3c 3d 69 50  os1 && iPos2<=iP
35b2b 6f 73 31 2b 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20  os1+nToken ){.  
35b2c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
35b2d 69 6e 74 36 34 20 69 53 61 76 65 3b 0a 20 20 20  int64 iSave;.   
35b2e 20 20 20 20 20 20 20 69 66 28 20 21 70 70 20 29         if( !pp )
35b2f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 74  {.            ft
35b30 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 30 2c  s3PoslistCopy(0,
35b31 20 26 70 32 29 3b 0a 20 20 20 20 20 20 20 20 20   &p2);.         
35b32 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74 43 6f     fts3PoslistCo
35b33 70 79 28 30 2c 20 26 70 31 29 3b 0a 20 20 20 20  py(0, &p1);.    
35b34 20 20 20 20 20 20 20 20 2a 70 70 31 20 3d 20 70          *pp1 = p
35b35 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  1;.            *
35b36 70 70 32 20 3d 20 70 32 3b 0a 20 20 20 20 20 20  pp2 = p2;.      
35b37 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
35b38 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
35b39 20 20 20 20 20 20 69 53 61 76 65 20 3d 20 69 73        iSave = is
35b3a 53 61 76 65 4c 65 66 74 20 3f 20 69 50 6f 73 31  SaveLeft ? iPos1
35b3b 20 3a 20 69 50 6f 73 32 3b 0a 20 20 20 20 20 20   : iPos2;.      
35b3c 20 20 20 20 66 74 73 33 50 75 74 44 65 6c 74 61      fts3PutDelta
35b3d 56 61 72 69 6e 74 28 26 70 2c 20 26 69 50 72 65  Varint(&p, &iPre
35b3e 76 2c 20 69 53 61 76 65 2b 32 29 3b 20 69 50 72  v, iSave+2); iPr
35b3f 65 76 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20  ev -= 2;.       
35b40 20 20 20 70 53 61 76 65 20 3d 20 30 3b 0a 20 20     pSave = 0;.  
35b41 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
35b42 69 66 28 20 28 21 69 73 53 61 76 65 4c 65 66 74  if( (!isSaveLeft
35b43 20 26 26 20 69 50 6f 73 32 3c 3d 28 69 50 6f 73   && iPos2<=(iPos
35b44 31 2b 6e 54 6f 6b 65 6e 29 29 20 7c 7c 20 69 50  1+nToken)) || iP
35b45 6f 73 32 3c 3d 69 50 6f 73 31 20 29 7b 0a 20 20  os2<=iPos1 ){.  
35b46 20 20 20 20 20 20 20 20 69 66 28 20 28 2a 70 32          if( (*p2
35b47 26 30 78 46 45 29 3d 3d 30 20 29 20 62 72 65 61  &0xFE)==0 ) brea
35b48 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  k;.          fts
35b49 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 28  3GetDeltaVarint(
35b4a 26 70 32 2c 20 26 69 50 6f 73 32 29 3b 20 69 50  &p2, &iPos2); iP
35b4b 6f 73 32 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20  os2 -= 2;.      
35b4c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
35b4d 20 20 20 69 66 28 20 28 2a 70 31 26 30 78 46 45     if( (*p1&0xFE
35b4e 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  )==0 ) break;.  
35b4f 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44          fts3GetD
35b50 65 6c 74 61 56 61 72 69 6e 74 28 26 70 31 2c 20  eltaVarint(&p1, 
35b51 26 69 50 6f 73 31 29 3b 20 69 50 6f 73 31 20 2d  &iPos1); iPos1 -
35b52 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  = 2;.        }..
35b53 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
35b54 28 20 70 53 61 76 65 20 26 26 20 70 70 20 29 7b  ( pSave && pp ){
35b55 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 53 61  .        p = pSa
35b56 76 65 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  ve;.      }..   
35b57 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69 73     fts3Columnlis
35b58 74 43 6f 70 79 28 30 2c 20 26 70 31 29 3b 0a 20  tCopy(0, &p1);. 
35b59 20 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c       fts3Columnl
35b5a 69 73 74 43 6f 70 79 28 30 2c 20 26 70 32 29 3b  istCopy(0, &p2);
35b5b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
35b5c 2a 70 31 26 30 78 46 45 29 3d 3d 30 20 26 26 20  *p1&0xFE)==0 && 
35b5d 28 2a 70 32 26 30 78 46 45 29 3d 3d 30 20 29 3b  (*p2&0xFE)==0 );
35b5e 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 2a 70  .      if( 0==*p
35b5f 31 20 7c 7c 20 30 3d 3d 2a 70 32 20 29 20 62 72  1 || 0==*p2 ) br
35b60 65 61 6b 3b 0a 0a 20 20 20 20 20 20 70 31 2b 2b  eak;..      p1++
35b61 3b 0a 20 20 20 20 20 20 70 31 20 2b 3d 20 73 71  ;.      p1 += sq
35b62 6c 69 74 65 33 46 74 73 33 47 65 74 56 61 72 69  lite3Fts3GetVari
35b63 6e 74 33 32 28 70 31 2c 20 26 69 43 6f 6c 31 29  nt32(p1, &iCol1)
35b64 3b 0a 20 20 20 20 20 20 70 32 2b 2b 3b 0a 20 20  ;.      p2++;.  
35b65 20 20 20 20 70 32 20 2b 3d 20 73 71 6c 69 74 65      p2 += sqlite
35b66 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32  3Fts3GetVarint32
35b67 28 70 32 2c 20 26 69 43 6f 6c 32 29 3b 0a 20 20  (p2, &iCol2);.  
35b68 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 76 61    }..    /* Adva
35b69 6e 63 65 20 70 6f 69 6e 74 65 72 20 70 31 20 6f  nce pointer p1 o
35b6a 72 20 70 32 20 28 77 68 69 63 68 65 76 65 72 20  r p2 (whichever 
35b6b 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74  corresponds to t
35b6c 68 65 20 73 6d 61 6c 6c 65 72 20 6f 66 0a 20 20  he smaller of.  
35b6d 20 20 2a 2a 20 69 43 6f 6c 31 20 61 6e 64 20 69    ** iCol1 and i
35b6e 43 6f 6c 32 29 20 73 6f 20 74 68 61 74 20 69 74  Col2) so that it
35b6f 20 70 6f 69 6e 74 73 20 74 6f 20 65 69 74 68 65   points to eithe
35b70 72 20 74 68 65 20 30 78 30 30 20 74 68 61 74 20  r the 0x00 that 
35b71 6d 61 72 6b 73 20 74 68 65 0a 20 20 20 20 2a 2a  marks the.    **
35b72 20 65 6e 64 20 6f 66 20 74 68 65 20 70 6f 73 69   end of the posi
35b73 74 69 6f 6e 20 6c 69 73 74 2c 20 6f 72 20 74 68  tion list, or th
35b74 65 20 30 78 30 31 20 74 68 61 74 20 70 72 65 63  e 0x01 that prec
35b75 65 64 65 73 20 74 68 65 20 6e 65 78 74 20 0a 20  edes the next . 
35b76 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 2d 6e 75 6d     ** column-num
35b77 62 65 72 20 69 6e 20 74 68 65 20 70 6f 73 69 74  ber in the posit
35b78 69 6f 6e 20 6c 69 73 74 2e 20 0a 20 20 20 20 2a  ion list. .    *
35b79 2f 0a 20 20 20 20 65 6c 73 65 20 69 66 28 20 69  /.    else if( i
35b7a 43 6f 6c 31 3c 69 43 6f 6c 32 20 29 7b 0a 20 20  Col1<iCol2 ){.  
35b7b 20 20 20 20 66 74 73 33 43 6f 6c 75 6d 6e 6c 69      fts3Columnli
35b7c 73 74 43 6f 70 79 28 30 2c 20 26 70 31 29 3b 0a  stCopy(0, &p1);.
35b7d 20 20 20 20 20 20 69 66 28 20 30 3d 3d 2a 70 31        if( 0==*p1
35b7e 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
35b7f 70 31 2b 2b 3b 0a 20 20 20 20 20 20 70 31 20 2b  p1++;.      p1 +
35b80 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
35b81 56 61 72 69 6e 74 33 32 28 70 31 2c 20 26 69 43  Varint32(p1, &iC
35b82 6f 6c 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ol1);.    }else{
35b83 0a 20 20 20 20 20 20 66 74 73 33 43 6f 6c 75 6d  .      fts3Colum
35b84 6e 6c 69 73 74 43 6f 70 79 28 30 2c 20 26 70 32  nlistCopy(0, &p2
35b85 29 3b 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d  );.      if( 0==
35b86 2a 70 32 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  *p2 ) break;.   
35b87 20 20 20 70 32 2b 2b 3b 0a 20 20 20 20 20 20 70     p2++;.      p
35b88 32 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33  2 += sqlite3Fts3
35b89 47 65 74 56 61 72 69 6e 74 33 32 28 70 32 2c 20  GetVarint32(p2, 
35b8a 26 69 43 6f 6c 32 29 3b 0a 20 20 20 20 7d 0a 20  &iCol2);.    }. 
35b8b 20 7d 0a 0a 20 20 66 74 73 33 50 6f 73 6c 69 73   }..  fts3Poslis
35b8c 74 43 6f 70 79 28 30 2c 20 26 70 32 29 3b 0a 20  tCopy(0, &p2);. 
35b8d 20 66 74 73 33 50 6f 73 6c 69 73 74 43 6f 70 79   fts3PoslistCopy
35b8e 28 30 2c 20 26 70 31 29 3b 0a 20 20 2a 70 70 31  (0, &p1);.  *pp1
35b8f 20 3d 20 70 31 3b 0a 20 20 2a 70 70 32 20 3d 20   = p1;.  *pp2 = 
35b90 70 32 3b 0a 20 20 69 66 28 20 21 70 70 20 7c 7c  p2;.  if( !pp ||
35b91 20 2a 70 70 3d 3d 70 20 29 7b 0a 20 20 20 20 72   *pp==p ){.    r
35b92 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a  eturn 0;.  }.  *
35b93 70 2b 2b 20 3d 20 30 78 30 30 3b 0a 20 20 2a 70  p++ = 0x00;.  *p
35b94 70 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20  p = p;.  return 
35b95 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67  1;.}../*.** Merg
35b96 65 20 74 77 6f 20 70 6f 73 69 74 69 6f 6e 2d 6c  e two position-l
35b97 69 73 74 73 20 61 73 20 72 65 71 75 69 72 65 64  ists as required
35b98 20 62 79 20 74 68 65 20 4e 45 41 52 20 6f 70 65   by the NEAR ope
35b99 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  rator..*/.static
35b9a 20 69 6e 74 20 66 74 73 33 50 6f 73 6c 69 73 74   int fts3Poslist
35b9b 4e 65 61 72 4d 65 72 67 65 28 0a 20 20 63 68 61  NearMerge(.  cha
35b9c 72 20 2a 2a 70 70 2c 20 20 20 20 20 20 20 20 20  r **pp,         
35b9d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35b9e 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f  Output buffer */
35b9f 0a 20 20 63 68 61 72 20 2a 61 54 6d 70 2c 20 20  .  char *aTmp,  
35ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ba1 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20     /* Temporary 
35ba2 62 75 66 66 65 72 20 73 70 61 63 65 20 2a 2f 0a  buffer space */.
35ba3 20 20 69 6e 74 20 6e 52 69 67 68 74 2c 20 20 20    int nRight,   
35ba4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ba5 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 64 69 66    /* Maximum dif
35ba6 66 65 72 65 6e 63 65 20 69 6e 20 74 6f 6b 65 6e  ference in token
35ba7 20 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20 20   positions */.  
35ba8 69 6e 74 20 6e 4c 65 66 74 2c 20 20 20 20 20 20  int nLeft,      
35ba9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35baa 2f 2a 20 4d 61 78 69 6d 75 6d 20 64 69 66 66 65  /* Maximum diffe
35bab 72 65 6e 63 65 20 69 6e 20 74 6f 6b 65 6e 20 70  rence in token p
35bac 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 63 68  ositions */.  ch
35bad 61 72 20 2a 2a 70 70 31 2c 20 20 20 20 20 20 20  ar **pp1,       
35bae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35baf 20 49 4e 2f 4f 55 54 3a 20 4c 65 66 74 20 69 6e   IN/OUT: Left in
35bb0 70 75 74 20 6c 69 73 74 20 2a 2f 0a 20 20 63 68  put list */.  ch
35bb1 61 72 20 2a 2a 70 70 32 20 20 20 20 20 20 20 20  ar **pp2        
35bb2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35bb3 20 49 4e 2f 4f 55 54 3a 20 52 69 67 68 74 20 69   IN/OUT: Right i
35bb4 6e 70 75 74 20 6c 69 73 74 20 2a 2f 0a 29 7b 0a  nput list */.){.
35bb5 20 20 63 68 61 72 20 2a 70 31 20 3d 20 2a 70 70    char *p1 = *pp
35bb6 31 3b 0a 20 20 63 68 61 72 20 2a 70 32 20 3d 20  1;.  char *p2 = 
35bb7 2a 70 70 32 3b 0a 0a 20 20 69 66 28 20 21 70 70  *pp2;..  if( !pp
35bb8 20 29 7b 0a 20 20 20 20 69 66 28 20 66 74 73 33   ){.    if( fts3
35bb9 50 6f 73 6c 69 73 74 50 68 72 61 73 65 4d 65 72  PoslistPhraseMer
35bba 67 65 28 30 2c 20 6e 52 69 67 68 74 2c 20 30 2c  ge(0, nRight, 0,
35bbb 20 70 70 31 2c 20 70 70 32 29 20 29 20 72 65 74   pp1, pp2) ) ret
35bbc 75 72 6e 20 31 3b 0a 20 20 20 20 2a 70 70 31 20  urn 1;.    *pp1 
35bbd 3d 20 70 31 3b 0a 20 20 20 20 2a 70 70 32 20 3d  = p1;.    *pp2 =
35bbe 20 70 32 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   p2;.    return 
35bbf 66 74 73 33 50 6f 73 6c 69 73 74 50 68 72 61 73  fts3PoslistPhras
35bc0 65 4d 65 72 67 65 28 30 2c 20 6e 4c 65 66 74 2c  eMerge(0, nLeft,
35bc1 20 30 2c 20 70 70 32 2c 20 70 70 31 29 3b 0a 20   0, pp2, pp1);. 
35bc2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72   }else{.    char
35bc3 20 2a 70 54 6d 70 31 20 3d 20 61 54 6d 70 3b 0a   *pTmp1 = aTmp;.
35bc4 20 20 20 20 63 68 61 72 20 2a 70 54 6d 70 32 3b      char *pTmp2;
35bc5 0a 20 20 20 20 63 68 61 72 20 2a 61 54 6d 70 32  .    char *aTmp2
35bc6 3b 0a 20 20 20 20 69 6e 74 20 72 65 73 20 3d 20  ;.    int res = 
35bc7 31 3b 0a 0a 20 20 20 20 66 74 73 33 50 6f 73 6c  1;..    fts3Posl
35bc8 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28 26  istPhraseMerge(&
35bc9 70 54 6d 70 31 2c 20 6e 52 69 67 68 74 2c 20 30  pTmp1, nRight, 0
35bca 2c 20 70 70 31 2c 20 70 70 32 29 3b 0a 20 20 20  , pp1, pp2);.   
35bcb 20 61 54 6d 70 32 20 3d 20 70 54 6d 70 32 20 3d   aTmp2 = pTmp2 =
35bcc 20 70 54 6d 70 31 3b 0a 20 20 20 20 2a 70 70 31   pTmp1;.    *pp1
35bcd 20 3d 20 70 31 3b 0a 20 20 20 20 2a 70 70 32 20   = p1;.    *pp2 
35bce 3d 20 70 32 3b 0a 20 20 20 20 66 74 73 33 50 6f  = p2;.    fts3Po
35bcf 73 6c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65  slistPhraseMerge
35bd0 28 26 70 54 6d 70 32 2c 20 6e 4c 65 66 74 2c 20  (&pTmp2, nLeft, 
35bd1 31 2c 20 70 70 32 2c 20 70 70 31 29 3b 0a 20 20  1, pp2, pp1);.  
35bd2 20 20 69 66 28 20 70 54 6d 70 31 21 3d 61 54 6d    if( pTmp1!=aTm
35bd3 70 20 26 26 20 70 54 6d 70 32 21 3d 61 54 6d 70  p && pTmp2!=aTmp
35bd4 32 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 50  2 ){.      fts3P
35bd5 6f 73 6c 69 73 74 4d 65 72 67 65 28 70 70 2c 20  oslistMerge(pp, 
35bd6 26 61 54 6d 70 2c 20 26 61 54 6d 70 32 29 3b 0a  &aTmp, &aTmp2);.
35bd7 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54      }else if( pT
35bd8 6d 70 31 21 3d 61 54 6d 70 20 29 7b 0a 20 20 20  mp1!=aTmp ){.   
35bd9 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74 43 6f     fts3PoslistCo
35bda 70 79 28 70 70 2c 20 26 61 54 6d 70 29 3b 0a 20  py(pp, &aTmp);. 
35bdb 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 54 6d     }else if( pTm
35bdc 70 32 21 3d 61 54 6d 70 32 20 29 7b 0a 20 20 20  p2!=aTmp2 ){.   
35bdd 20 20 20 66 74 73 33 50 6f 73 6c 69 73 74 43 6f     fts3PoslistCo
35bde 70 79 28 70 70 2c 20 26 61 54 6d 70 32 29 3b 0a  py(pp, &aTmp2);.
35bdf 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
35be0 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 7d 0a   res = 0;.    }.
35be1 0a 20 20 20 20 72 65 74 75 72 6e 20 72 65 73 3b  .    return res;
35be2 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 56 61  .  }.}../*.** Va
35be3 6c 75 65 73 20 74 68 61 74 20 6d 61 79 20 62 65  lues that may be
35be4 20 75 73 65 64 20 61 73 20 74 68 65 20 66 69 72   used as the fir
35be5 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  st parameter to 
35be6 66 74 73 33 44 6f 63 6c 69 73 74 4d 65 72 67 65  fts3DoclistMerge
35be7 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ()..*/.#define M
35be8 45 52 47 45 5f 4e 4f 54 20 20 20 20 20 20 20 20  ERGE_NOT        
35be9 32 20 20 20 20 20 20 20 20 2f 2a 20 44 20 2b 20  2        /* D + 
35bea 44 20 2d 3e 20 44 20 2a 2f 0a 23 64 65 66 69 6e  D -> D */.#defin
35beb 65 20 4d 45 52 47 45 5f 41 4e 44 20 20 20 20 20  e MERGE_AND     
35bec 20 20 20 33 20 20 20 20 20 20 20 20 2f 2a 20 44     3        /* D
35bed 20 2b 20 44 20 2d 3e 20 44 20 2a 2f 0a 23 64 65   + D -> D */.#de
35bee 66 69 6e 65 20 4d 45 52 47 45 5f 4f 52 20 20 20  fine MERGE_OR   
35bef 20 20 20 20 20 20 34 20 20 20 20 20 20 20 20 2f        4        /
35bf0 2a 20 44 20 2b 20 44 20 2d 3e 20 44 20 2a 2f 0a  * D + D -> D */.
35bf1 23 64 65 66 69 6e 65 20 4d 45 52 47 45 5f 50 4f  #define MERGE_PO
35bf2 53 5f 4f 52 20 20 20 20 20 35 20 20 20 20 20 20  S_OR     5      
35bf3 20 20 2f 2a 20 50 20 2b 20 50 20 2d 3e 20 50 20    /* P + P -> P 
35bf4 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 52 47 45  */.#define MERGE
35bf5 5f 50 48 52 41 53 45 20 20 20 20 20 36 20 20 20  _PHRASE     6   
35bf6 20 20 20 20 20 2f 2a 20 50 20 2b 20 50 20 2d 3e       /* P + P ->
35bf7 20 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45   D */.#define ME
35bf8 52 47 45 5f 50 4f 53 5f 50 48 52 41 53 45 20 37  RGE_POS_PHRASE 7
35bf9 20 20 20 20 20 20 20 20 2f 2a 20 50 20 2b 20 50          /* P + P
35bfa 20 2d 3e 20 50 20 2a 2f 0a 23 64 65 66 69 6e 65   -> P */.#define
35bfb 20 4d 45 52 47 45 5f 4e 45 41 52 20 20 20 20 20   MERGE_NEAR     
35bfc 20 20 38 20 20 20 20 20 20 20 20 2f 2a 20 50 20    8        /* P 
35bfd 2b 20 50 20 2d 3e 20 44 20 2a 2f 0a 23 64 65 66  + P -> D */.#def
35bfe 69 6e 65 20 4d 45 52 47 45 5f 50 4f 53 5f 4e 45  ine MERGE_POS_NE
35bff 41 52 20 20 20 39 20 20 20 20 20 20 20 20 2f 2a  AR   9        /*
35c00 20 50 20 2b 20 50 20 2d 3e 20 50 20 2a 2f 0a 0a   P + P -> P */..
35c01 2f 2a 0a 2a 2a 20 4d 65 72 67 65 20 74 68 65 20  /*.** Merge the 
35c02 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 70 61 73  two doclists pas
35c03 73 65 64 20 69 6e 20 62 75 66 66 65 72 20 61 31  sed in buffer a1
35c04 20 28 73 69 7a 65 20 6e 31 20 62 79 74 65 73 29   (size n1 bytes)
35c05 20 61 6e 64 20 61 32 0a 2a 2a 20 28 73 69 7a 65   and a2.** (size
35c06 20 6e 32 20 62 79 74 65 73 29 2e 20 54 68 65 20   n2 bytes). The 
35c07 6f 75 74 70 75 74 20 69 73 20 77 72 69 74 74 65  output is writte
35c08 6e 20 74 6f 20 70 72 65 2d 61 6c 6c 6f 63 61 74  n to pre-allocat
35c09 65 64 20 62 75 66 66 65 72 20 61 42 75 66 66 65  ed buffer aBuffe
35c0a 72 2c 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 67  r,.** which is g
35c0b 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20  uaranteed to be 
35c0c 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20  large enough to 
35c0d 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 73  hold the results
35c0e 2e 20 54 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20  . The number.** 
35c0f 6f 66 20 62 79 74 65 73 20 77 72 69 74 74 65 6e  of bytes written
35c10 20 74 6f 20 61 42 75 66 66 65 72 20 69 73 20 73   to aBuffer is s
35c11 74 6f 72 65 64 20 69 6e 20 2a 70 6e 42 75 66 66  tored in *pnBuff
35c12 65 72 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  er before return
35c13 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75  ing..**.** If su
35c14 63 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45  ccessful, SQLITE
35c15 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
35c16 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61   Otherwise, if a
35c17 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 0a 2a 2a   malloc error.**
35c18 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 61 6c   occurs while al
35c19 6c 6f 63 61 74 69 6e 67 20 61 20 74 65 6d 70 6f  locating a tempo
35c1a 72 61 72 79 20 62 75 66 66 65 72 20 61 73 20 70  rary buffer as p
35c1b 61 72 74 20 6f 66 20 74 68 65 20 6d 65 72 67 65  art of the merge
35c1c 20 6f 70 65 72 61 74 69 6f 6e 2c 0a 2a 2a 20 53   operation,.** S
35c1d 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20 72  QLITE_NOMEM is r
35c1e 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
35c1f 69 63 20 69 6e 74 20 66 74 73 33 44 6f 63 6c 69  ic int fts3Docli
35c20 73 74 4d 65 72 67 65 28 0a 20 20 69 6e 74 20 6d  stMerge(.  int m
35c21 65 72 67 65 74 79 70 65 2c 20 20 20 20 20 20 20  ergetype,       
35c22 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
35c23 65 20 6f 66 20 74 68 65 20 4d 45 52 47 45 5f 58  e of the MERGE_X
35c24 58 58 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f 0a  XX constants */.
35c25 20 20 69 6e 74 20 6e 50 61 72 61 6d 31 2c 20 20    int nParam1,  
35c26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c27 20 20 2f 2a 20 55 73 65 64 20 62 79 20 4d 45 52    /* Used by MER
35c28 47 45 5f 4e 45 41 52 20 61 6e 64 20 4d 45 52 47  GE_NEAR and MERG
35c29 45 5f 50 4f 53 5f 4e 45 41 52 20 2a 2f 0a 20 20  E_POS_NEAR */.  
35c2a 69 6e 74 20 6e 50 61 72 61 6d 32 2c 20 20 20 20  int nParam2,    
35c2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c2c 2f 2a 20 55 73 65 64 20 62 79 20 4d 45 52 47 45  /* Used by MERGE
35c2d 5f 4e 45 41 52 20 61 6e 64 20 4d 45 52 47 45 5f  _NEAR and MERGE_
35c2e 50 4f 53 5f 4e 45 41 52 20 2a 2f 0a 20 20 63 68  POS_NEAR */.  ch
35c2f 61 72 20 2a 61 42 75 66 66 65 72 2c 20 20 20 20  ar *aBuffer,    
35c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
35c31 20 50 72 65 2d 61 6c 6c 6f 63 61 74 65 64 20 6f   Pre-allocated o
35c32 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a  utput buffer */.
35c33 20 20 69 6e 74 20 2a 70 6e 42 75 66 66 65 72 2c    int *pnBuffer,
35c34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c35 20 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65 73 20    /* OUT: Bytes 
35c36 77 72 69 74 74 65 6e 20 74 6f 20 61 42 75 66 66  written to aBuff
35c37 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 31  er */.  char *a1
35c38 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
35c39 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65          /* Buffe
35c3a 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69 72  r containing fir
35c3b 73 74 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20  st doclist */.  
35c3c 69 6e 74 20 6e 31 2c 20 20 20 20 20 20 20 20 20  int n1,         
35c3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c3e 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65  /* Size of buffe
35c3f 72 20 61 31 20 2a 2f 0a 20 20 63 68 61 72 20 2a  r a1 */.  char *
35c40 61 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  a2,             
35c41 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 66            /* Buf
35c42 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 73  fer containing s
35c43 65 63 6f 6e 64 20 64 6f 63 6c 69 73 74 20 2a 2f  econd doclist */
35c44 0a 20 20 69 6e 74 20 6e 32 20 20 20 20 20 20 20  .  int n2       
35c45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c46 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75     /* Size of bu
35c47 66 66 65 72 20 61 32 20 2a 2f 0a 29 7b 0a 20 20  ffer a2 */.){.  
35c48 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 31  sqlite3_int64 i1
35c49 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f   = 0;.  sqlite3_
35c4a 69 6e 74 36 34 20 69 32 20 3d 20 30 3b 0a 20 20  int64 i2 = 0;.  
35c4b 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50  sqlite3_int64 iP
35c4c 72 65 76 20 3d 20 30 3b 0a 0a 20 20 63 68 61 72  rev = 0;..  char
35c4d 20 2a 70 20 3d 20 61 42 75 66 66 65 72 3b 0a 20   *p = aBuffer;. 
35c4e 20 63 68 61 72 20 2a 70 31 20 3d 20 61 31 3b 0a   char *p1 = a1;.
35c4f 20 20 63 68 61 72 20 2a 70 32 20 3d 20 61 32 3b    char *p2 = a2;
35c50 0a 20 20 63 68 61 72 20 2a 70 45 6e 64 31 20 3d  .  char *pEnd1 =
35c51 20 26 61 31 5b 6e 31 5d 3b 0a 20 20 63 68 61 72   &a1[n1];.  char
35c52 20 2a 70 45 6e 64 32 20 3d 20 26 61 32 5b 6e 32   *pEnd2 = &a2[n2
35c53 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6d 65  ];..  assert( me
35c54 72 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 4f  rgetype==MERGE_O
35c55 52 20 20 20 20 20 7c 7c 20 6d 65 72 67 65 74 79  R     || mergety
35c56 70 65 3d 3d 4d 45 52 47 45 5f 50 4f 53 5f 4f 52  pe==MERGE_POS_OR
35c57 20 0a 20 20 20 20 20 20 20 7c 7c 20 6d 65 72 67   .       || merg
35c58 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 41 4e 44  etype==MERGE_AND
35c59 20 20 20 20 7c 7c 20 6d 65 72 67 65 74 79 70 65      || mergetype
35c5a 3d 3d 4d 45 52 47 45 5f 4e 4f 54 0a 20 20 20 20  ==MERGE_NOT.    
35c5b 20 20 20 7c 7c 20 6d 65 72 67 65 74 79 70 65 3d     || mergetype=
35c5c 3d 4d 45 52 47 45 5f 50 48 52 41 53 45 20 7c 7c  =MERGE_PHRASE ||
35c5d 20 6d 65 72 67 65 74 79 70 65 3d 3d 4d 45 52 47   mergetype==MERG
35c5e 45 5f 50 4f 53 5f 50 48 52 41 53 45 0a 20 20 20  E_POS_PHRASE.   
35c5f 20 20 20 20 7c 7c 20 6d 65 72 67 65 74 79 70 65      || mergetype
35c60 3d 3d 4d 45 52 47 45 5f 4e 45 41 52 20 20 20 7c  ==MERGE_NEAR   |
35c61 7c 20 6d 65 72 67 65 74 79 70 65 3d 3d 4d 45 52  | mergetype==MER
35c62 47 45 5f 50 4f 53 5f 4e 45 41 52 0a 20 20 29 3b  GE_POS_NEAR.  );
35c63 0a 0a 20 20 69 66 28 20 21 61 42 75 66 66 65 72  ..  if( !aBuffer
35c64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
35c65 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
35c66 0a 20 20 69 66 28 20 6e 31 3d 3d 30 20 26 26 20  .  if( n1==0 && 
35c67 6e 32 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 6e  n2==0 ){.    *pn
35c68 42 75 66 66 65 72 20 3d 20 30 3b 0a 20 20 20 20  Buffer = 0;.    
35c69 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
35c6a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 64  ;.  }..  /* Read
35c6b 20 74 68 65 20 66 69 72 73 74 20 64 6f 63 69 64   the first docid
35c6c 20 66 72 6f 6d 20 65 61 63 68 20 64 6f 63 6c 69   from each docli
35c6d 73 74 20 2a 2f 0a 20 20 66 74 73 33 47 65 74 44  st */.  fts3GetD
35c6e 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70 31 2c  eltaVarint2(&p1,
35c6f 20 70 45 6e 64 31 2c 20 26 69 31 29 3b 0a 20 20   pEnd1, &i1);.  
35c70 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69  fts3GetDeltaVari
35c71 6e 74 32 28 26 70 32 2c 20 70 45 6e 64 32 2c 20  nt2(&p2, pEnd2, 
35c72 26 69 32 29 3b 0a 0a 20 20 73 77 69 74 63 68 28  &i2);..  switch(
35c73 20 6d 65 72 67 65 74 79 70 65 20 29 7b 0a 20 20   mergetype ){.  
35c74 20 20 63 61 73 65 20 4d 45 52 47 45 5f 4f 52 3a    case MERGE_OR:
35c75 0a 20 20 20 20 63 61 73 65 20 4d 45 52 47 45 5f  .    case MERGE_
35c76 50 4f 53 5f 4f 52 3a 0a 20 20 20 20 20 20 77 68  POS_OR:.      wh
35c77 69 6c 65 28 20 70 31 20 7c 7c 20 70 32 20 29 7b  ile( p1 || p2 ){
35c78 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 32 20  .        if( p2 
35c79 26 26 20 70 31 20 26 26 20 69 31 3d 3d 69 32 20  && p1 && i1==i2 
35c7a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  ){.          fts
35c7b 33 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28  3PutDeltaVarint(
35c7c 26 70 2c 20 26 69 50 72 65 76 2c 20 69 31 29 3b  &p, &iPrev, i1);
35c7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6d  .          if( m
35c7e 65 72 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f  ergetype==MERGE_
35c7f 50 4f 53 5f 4f 52 20 29 20 66 74 73 33 50 6f 73  POS_OR ) fts3Pos
35c80 6c 69 73 74 4d 65 72 67 65 28 26 70 2c 20 26 70  listMerge(&p, &p
35c81 31 2c 20 26 70 32 29 3b 0a 20 20 20 20 20 20 20  1, &p2);.       
35c82 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
35c83 61 72 69 6e 74 32 28 26 70 31 2c 20 70 45 6e 64  arint2(&p1, pEnd
35c84 31 2c 20 26 69 31 29 3b 0a 20 20 20 20 20 20 20  1, &i1);.       
35c85 20 20 20 66 74 73 33 47 65 74 44 65 6c 74 61 56     fts3GetDeltaV
35c86 61 72 69 6e 74 32 28 26 70 32 2c 20 70 45 6e 64  arint2(&p2, pEnd
35c87 32 2c 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20  2, &i2);.       
35c88 20 7d 65 6c 73 65 20 69 66 28 20 21 70 32 20 7c   }else if( !p2 |
35c89 7c 20 28 70 31 20 26 26 20 69 31 3c 69 32 29 20  | (p1 && i1<i2) 
35c8a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  ){.          fts
35c8b 33 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28  3PutDeltaVarint(
35c8c 26 70 2c 20 26 69 50 72 65 76 2c 20 69 31 29 3b  &p, &iPrev, i1);
35c8d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6d  .          if( m
35c8e 65 72 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f  ergetype==MERGE_
35c8f 50 4f 53 5f 4f 52 20 29 20 66 74 73 33 50 6f 73  POS_OR ) fts3Pos
35c90 6c 69 73 74 43 6f 70 79 28 26 70 2c 20 26 70 31  listCopy(&p, &p1
35c91 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  );.          fts
35c92 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32  3GetDeltaVarint2
35c93 28 26 70 31 2c 20 70 45 6e 64 31 2c 20 26 69 31  (&p1, pEnd1, &i1
35c94 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
35c95 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  {.          fts3
35c96 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26  PutDeltaVarint(&
35c97 70 2c 20 26 69 50 72 65 76 2c 20 69 32 29 3b 0a  p, &iPrev, i2);.
35c98 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6d 65            if( me
35c99 72 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 50  rgetype==MERGE_P
35c9a 4f 53 5f 4f 52 20 29 20 66 74 73 33 50 6f 73 6c  OS_OR ) fts3Posl
35c9b 69 73 74 43 6f 70 79 28 26 70 2c 20 26 70 32 29  istCopy(&p, &p2)
35c9c 3b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  ;.          fts3
35c9d 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28  GetDeltaVarint2(
35c9e 26 70 32 2c 20 70 45 6e 64 32 2c 20 26 69 32 29  &p2, pEnd2, &i2)
35c9f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
35ca0 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
35ca1 0a 0a 20 20 20 20 63 61 73 65 20 4d 45 52 47 45  ..    case MERGE
35ca2 5f 41 4e 44 3a 0a 20 20 20 20 20 20 77 68 69 6c  _AND:.      whil
35ca3 65 28 20 70 31 20 26 26 20 70 32 20 29 7b 0a 20  e( p1 && p2 ){. 
35ca4 20 20 20 20 20 20 20 69 66 28 20 69 31 3d 3d 69         if( i1==i
35ca5 32 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  2 ){.          f
35ca6 74 73 33 50 75 74 44 65 6c 74 61 56 61 72 69 6e  ts3PutDeltaVarin
35ca7 74 28 26 70 2c 20 26 69 50 72 65 76 2c 20 69 31  t(&p, &iPrev, i1
35ca8 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  );.          fts
35ca9 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32  3GetDeltaVarint2
35caa 28 26 70 31 2c 20 70 45 6e 64 31 2c 20 26 69 31  (&p1, pEnd1, &i1
35cab 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  );.          fts
35cac 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32  3GetDeltaVarint2
35cad 28 26 70 32 2c 20 70 45 6e 64 32 2c 20 26 69 32  (&p2, pEnd2, &i2
35cae 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
35caf 20 69 66 28 20 69 31 3c 69 32 20 29 7b 0a 20 20   if( i1<i2 ){.  
35cb0 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44          fts3GetD
35cb1 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70 31 2c  eltaVarint2(&p1,
35cb2 20 70 45 6e 64 31 2c 20 26 69 31 29 3b 0a 20 20   pEnd1, &i1);.  
35cb3 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
35cb4 20 20 20 20 20 20 20 66 74 73 33 47 65 74 44 65         fts3GetDe
35cb5 6c 74 61 56 61 72 69 6e 74 32 28 26 70 32 2c 20  ltaVarint2(&p2, 
35cb6 70 45 6e 64 32 2c 20 26 69 32 29 3b 0a 20 20 20  pEnd2, &i2);.   
35cb7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
35cb8 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20       break;..   
35cb9 20 63 61 73 65 20 4d 45 52 47 45 5f 4e 4f 54 3a   case MERGE_NOT:
35cba 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 31  .      while( p1
35cbb 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
35cbc 70 32 20 26 26 20 69 31 3d 3d 69 32 20 29 7b 0a  p2 && i1==i2 ){.
35cbd 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65            fts3Ge
35cbe 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70  tDeltaVarint2(&p
35cbf 31 2c 20 70 45 6e 64 31 2c 20 26 69 31 29 3b 0a  1, pEnd1, &i1);.
35cc0 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65            fts3Ge
35cc1 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70  tDeltaVarint2(&p
35cc2 32 2c 20 70 45 6e 64 32 2c 20 26 69 32 29 3b 0a  2, pEnd2, &i2);.
35cc3 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
35cc4 28 20 21 70 32 20 7c 7c 20 69 31 3c 69 32 20 29  ( !p2 || i1<i2 )
35cc5 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  {.          fts3
35cc6 50 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26  PutDeltaVarint(&
35cc7 70 2c 20 26 69 50 72 65 76 2c 20 69 31 29 3b 0a  p, &iPrev, i1);.
35cc8 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65            fts3Ge
35cc9 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70  tDeltaVarint2(&p
35cca 31 2c 20 70 45 6e 64 31 2c 20 26 69 31 29 3b 0a  1, pEnd1, &i1);.
35ccb 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
35ccc 20 20 20 20 20 20 20 20 20 66 74 73 33 47 65 74           fts3Get
35ccd 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26 70 32  DeltaVarint2(&p2
35cce 2c 20 70 45 6e 64 32 2c 20 26 69 32 29 3b 0a 20  , pEnd2, &i2);. 
35ccf 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
35cd0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20  .      break;.. 
35cd1 20 20 20 63 61 73 65 20 4d 45 52 47 45 5f 50 4f     case MERGE_PO
35cd2 53 5f 50 48 52 41 53 45 3a 0a 20 20 20 20 63 61  S_PHRASE:.    ca
35cd3 73 65 20 4d 45 52 47 45 5f 50 48 52 41 53 45 3a  se MERGE_PHRASE:
35cd4 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 2a   {.      char **
35cd5 70 70 50 6f 73 20 3d 20 28 6d 65 72 67 65 74 79  ppPos = (mergety
35cd6 70 65 3d 3d 4d 45 52 47 45 5f 50 48 52 41 53 45  pe==MERGE_PHRASE
35cd7 20 3f 20 30 20 3a 20 26 70 29 3b 0a 20 20 20 20   ? 0 : &p);.    
35cd8 20 20 77 68 69 6c 65 28 20 70 31 20 26 26 20 70    while( p1 && p
35cd9 32 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  2 ){.        if(
35cda 20 69 31 3d 3d 69 32 20 29 7b 0a 20 20 20 20 20   i1==i2 ){.     
35cdb 20 20 20 20 20 63 68 61 72 20 2a 70 53 61 76 65       char *pSave
35cdc 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20   = p;.          
35cdd 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50  sqlite3_int64 iP
35cde 72 65 76 53 61 76 65 20 3d 20 69 50 72 65 76 3b  revSave = iPrev;
35cdf 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 50  .          fts3P
35ce0 75 74 44 65 6c 74 61 56 61 72 69 6e 74 28 26 70  utDeltaVarint(&p
35ce1 2c 20 26 69 50 72 65 76 2c 20 69 31 29 3b 0a 20  , &iPrev, i1);. 
35ce2 20 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d           if( 0==
35ce3 66 74 73 33 50 6f 73 6c 69 73 74 50 68 72 61 73  fts3PoslistPhras
35ce4 65 4d 65 72 67 65 28 70 70 50 6f 73 2c 20 31 2c  eMerge(ppPos, 1,
35ce5 20 30 2c 20 26 70 31 2c 20 26 70 32 29 20 29 7b   0, &p1, &p2) ){
35ce6 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 20 3d  .            p =
35ce7 20 70 53 61 76 65 3b 0a 20 20 20 20 20 20 20 20   pSave;.        
35ce8 20 20 20 20 69 50 72 65 76 20 3d 20 69 50 72 65      iPrev = iPre
35ce9 76 53 61 76 65 3b 0a 20 20 20 20 20 20 20 20 20  vSave;.         
35cea 20 7d 0a 20 20 20 20 20 20 20 20 20 20 66 74 73   }.          fts
35ceb 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32  3GetDeltaVarint2
35cec 28 26 70 31 2c 20 70 45 6e 64 31 2c 20 26 69 31  (&p1, pEnd1, &i1
35ced 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  );.          fts
35cee 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32  3GetDeltaVarint2
35cef 28 26 70 32 2c 20 70 45 6e 64 32 2c 20 26 69 32  (&p2, pEnd2, &i2
35cf0 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
35cf1 20 69 66 28 20 69 31 3c 69 32 20 29 7b 0a 20 20   if( i1<i2 ){.  
35cf2 20 20 20 20 20 20 20 20 66 74 73 33 50 6f 73 6c          fts3Posl
35cf3 69 73 74 43 6f 70 79 28 30 2c 20 26 70 31 29 3b  istCopy(0, &p1);
35cf4 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33 47  .          fts3G
35cf5 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32 28 26  etDeltaVarint2(&
35cf6 70 31 2c 20 70 45 6e 64 31 2c 20 26 69 31 29 3b  p1, pEnd1, &i1);
35cf7 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
35cf8 20 20 20 20 20 20 20 20 20 20 66 74 73 33 50 6f            fts3Po
35cf9 73 6c 69 73 74 43 6f 70 79 28 30 2c 20 26 70 32  slistCopy(0, &p2
35cfa 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73  );.          fts
35cfb 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e 74 32  3GetDeltaVarint2
35cfc 28 26 70 32 2c 20 70 45 6e 64 32 2c 20 26 69 32  (&p2, pEnd2, &i2
35cfd 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
35cfe 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
35cff 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64 65 66  ;.    }..    def
35d00 61 75 6c 74 3a 20 61 73 73 65 72 74 28 20 6d 65  ault: assert( me
35d01 72 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 50  rgetype==MERGE_P
35d02 4f 53 5f 4e 45 41 52 20 7c 7c 20 6d 65 72 67 65  OS_NEAR || merge
35d03 74 79 70 65 3d 3d 4d 45 52 47 45 5f 4e 45 41 52  type==MERGE_NEAR
35d04 20 29 3b 20 7b 0a 20 20 20 20 20 20 63 68 61 72   ); {.      char
35d05 20 2a 61 54 6d 70 20 3d 20 30 3b 0a 20 20 20 20   *aTmp = 0;.    
35d06 20 20 63 68 61 72 20 2a 2a 70 70 50 6f 73 20 3d    char **ppPos =
35d07 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65   0;.      if( me
35d08 72 67 65 74 79 70 65 3d 3d 4d 45 52 47 45 5f 50  rgetype==MERGE_P
35d09 4f 53 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20  OS_NEAR ){.     
35d0a 20 20 20 70 70 50 6f 73 20 3d 20 26 70 3b 0a 20     ppPos = &p;. 
35d0b 20 20 20 20 20 20 20 61 54 6d 70 20 3d 20 73 71         aTmp = sq
35d0c 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 32 2a 28  lite3_malloc(2*(
35d0d 6e 31 2b 6e 32 29 29 3b 0a 20 20 20 20 20 20 20  n1+n2));.       
35d0e 20 69 66 28 20 21 61 54 6d 70 20 29 7b 0a 20 20   if( !aTmp ){.  
35d0f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
35d10 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
35d11 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
35d12 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 31 20        while( p1 
35d13 26 26 20 70 32 20 29 7b 0a 20 20 20 20 20 20 20  && p2 ){.       
35d14 20 69 66 28 20 69 31 3d 3d 69 32 20 29 7b 0a 20   if( i1==i2 ){. 
35d15 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 70           char *p
35d16 53 61 76 65 20 3d 20 70 3b 0a 20 20 20 20 20 20  Save = p;.      
35d17 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
35d18 34 20 69 50 72 65 76 53 61 76 65 20 3d 20 69 50  4 iPrevSave = iP
35d19 72 65 76 3b 0a 20 20 20 20 20 20 20 20 20 20 66  rev;.          f
35d1a 74 73 33 50 75 74 44 65 6c 74 61 56 61 72 69 6e  ts3PutDeltaVarin
35d1b 74 28 26 70 2c 20 26 69 50 72 65 76 2c 20 69 31  t(&p, &iPrev, i1
35d1c 29 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 69 66  );..          if
35d1d 28 20 21 66 74 73 33 50 6f 73 6c 69 73 74 4e 65  ( !fts3PoslistNe
35d1e 61 72 4d 65 72 67 65 28 70 70 50 6f 73 2c 20 61  arMerge(ppPos, a
35d1f 54 6d 70 2c 20 6e 50 61 72 61 6d 31 2c 20 6e 50  Tmp, nParam1, nP
35d20 61 72 61 6d 32 2c 20 26 70 31 2c 20 26 70 32 29  aram2, &p1, &p2)
35d21 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
35d22 69 50 72 65 76 20 3d 20 69 50 72 65 76 53 61 76  iPrev = iPrevSav
35d23 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70  e;.            p
35d24 20 3d 20 70 53 61 76 65 3b 0a 20 20 20 20 20 20   = pSave;.      
35d25 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20      }..         
35d26 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
35d27 69 6e 74 32 28 26 70 31 2c 20 70 45 6e 64 31 2c  int2(&p1, pEnd1,
35d28 20 26 69 31 29 3b 0a 20 20 20 20 20 20 20 20 20   &i1);.         
35d29 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
35d2a 69 6e 74 32 28 26 70 32 2c 20 70 45 6e 64 32 2c  int2(&p2, pEnd2,
35d2b 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20 20 7d   &i2);.        }
35d2c 65 6c 73 65 20 69 66 28 20 69 31 3c 69 32 20 29  else if( i1<i2 )
35d2d 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74 73 33  {.          fts3
35d2e 50 6f 73 6c 69 73 74 43 6f 70 79 28 30 2c 20 26  PoslistCopy(0, &
35d2f 70 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 66  p1);.          f
35d30 74 73 33 47 65 74 44 65 6c 74 61 56 61 72 69 6e  ts3GetDeltaVarin
35d31 74 32 28 26 70 31 2c 20 70 45 6e 64 31 2c 20 26  t2(&p1, pEnd1, &
35d32 69 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  i1);.        }el
35d33 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 74  se{.          ft
35d34 73 33 50 6f 73 6c 69 73 74 43 6f 70 79 28 30 2c  s3PoslistCopy(0,
35d35 20 26 70 32 29 3b 0a 20 20 20 20 20 20 20 20 20   &p2);.         
35d36 20 66 74 73 33 47 65 74 44 65 6c 74 61 56 61 72   fts3GetDeltaVar
35d37 69 6e 74 32 28 26 70 32 2c 20 70 45 6e 64 32 2c  int2(&p2, pEnd2,
35d38 20 26 69 32 29 3b 0a 20 20 20 20 20 20 20 20 7d   &i2);.        }
35d39 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
35d3a 71 6c 69 74 65 33 5f 66 72 65 65 28 61 54 6d 70  qlite3_free(aTmp
35d3b 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
35d3c 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 6e      }.  }..  *pn
35d3d 42 75 66 66 65 72 20 3d 20 28 69 6e 74 29 28 70  Buffer = (int)(p
35d3e 2d 61 42 75 66 66 65 72 29 3b 0a 20 20 72 65 74  -aBuffer);.  ret
35d3f 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
35d40 0a 0a 2f 2a 20 0a 2a 2a 20 41 20 70 6f 69 6e 74  ../* .** A point
35d41 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63  er to an instanc
35d42 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
35d43 75 72 65 20 69 73 20 75 73 65 64 20 61 73 20 74  ure is used as t
35d44 68 65 20 63 6f 6e 74 65 78 74 20 0a 2a 2a 20 61  he context .** a
35d45 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
35d46 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72 49  e3Fts3SegReaderI
35d47 74 65 72 61 74 65 28 29 0a 2a 2f 0a 74 79 70 65  terate().*/.type
35d48 64 65 66 20 73 74 72 75 63 74 20 54 65 72 6d 53  def struct TermS
35d49 65 6c 65 63 74 20 54 65 72 6d 53 65 6c 65 63 74  elect TermSelect
35d4a 3b 0a 73 74 72 75 63 74 20 54 65 72 6d 53 65 6c  ;.struct TermSel
35d4b 65 63 74 20 7b 0a 20 20 69 6e 74 20 69 73 52 65  ect {.  int isRe
35d4c 71 50 6f 73 3b 0a 20 20 63 68 61 72 20 2a 61 4f  qPos;.  char *aO
35d4d 75 74 70 75 74 3b 20 20 20 20 20 20 20 20 20 20  utput;          
35d4e 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f          /* Mallo
35d4f 63 27 64 20 6f 75 74 70 75 74 20 62 75 66 66 65  c'd output buffe
35d50 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 70  r */.  int nOutp
35d51 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
35d52 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
35d53 66 20 6f 75 74 70 75 74 20 69 6e 20 62 79 74 65  f output in byte
35d54 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  s */.};../*.** T
35d55 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
35d56 75 73 65 64 20 61 73 20 74 68 65 20 73 71 6c 69  used as the sqli
35d57 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
35d58 49 74 65 72 61 74 65 28 29 20 63 61 6c 6c 62 61  Iterate() callba
35d59 63 6b 20 77 68 65 6e 0a 2a 2a 20 71 75 65 72 79  ck when.** query
35d5a 69 6e 67 20 74 68 65 20 66 75 6c 6c 2d 74 65 78  ing the full-tex
35d5b 74 20 69 6e 64 65 78 20 66 6f 72 20 61 20 64 6f  t index for a do
35d5c 63 6c 69 73 74 20 61 73 73 6f 63 69 61 74 65 64  clist associated
35d5d 20 77 69 74 68 20 61 20 74 65 72 6d 20 6f 72 0a   with a term or.
35d5e 2a 2a 20 74 65 72 6d 2d 70 72 65 66 69 78 2e 0a  ** term-prefix..
35d5f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
35d60 73 33 54 65 72 6d 53 65 6c 65 63 74 43 62 28 0a  s3TermSelectCb(.
35d61 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20    Fts3Table *p, 
35d62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35d63 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
35d64 6c 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 76  le object */.  v
35d65 6f 69 64 20 2a 70 43 6f 6e 74 65 78 74 2c 20 20  oid *pContext,  
35d66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35d67 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 54 65 72  * Pointer to Ter
35d68 6d 53 65 6c 65 63 74 20 73 74 72 75 63 74 75 72  mSelect structur
35d69 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 65  e */.  char *zTe
35d6a 72 6d 2c 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c  rm,.  int nTerm,
35d6b 0a 20 20 63 68 61 72 20 2a 61 44 6f 63 6c 69 73  .  char *aDoclis
35d6c 74 2c 0a 20 20 69 6e 74 20 6e 44 6f 63 6c 69 73  t,.  int nDoclis
35d6d 74 0a 29 7b 0a 20 20 54 65 72 6d 53 65 6c 65 63  t.){.  TermSelec
35d6e 74 20 2a 70 54 53 20 3d 20 28 54 65 72 6d 53 65  t *pTS = (TermSe
35d6f 6c 65 63 74 20 2a 29 70 43 6f 6e 74 65 78 74 3b  lect *)pContext;
35d70 0a 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 70 54  .  int nNew = pT
35d71 53 2d 3e 6e 4f 75 74 70 75 74 20 2b 20 6e 44 6f  S->nOutput + nDo
35d72 63 6c 69 73 74 3b 0a 20 20 63 68 61 72 20 2a 61  clist;.  char *a
35d73 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  New = sqlite3_ma
35d74 6c 6c 6f 63 28 6e 4e 65 77 29 3b 0a 0a 20 20 55  lloc(nNew);..  U
35d75 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
35d76 70 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  p);.  UNUSED_PAR
35d77 41 4d 45 54 45 52 28 7a 54 65 72 6d 29 3b 0a 20  AMETER(zTerm);. 
35d78 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
35d79 52 28 6e 54 65 72 6d 29 3b 0a 0a 20 20 69 66 28  R(nTerm);..  if(
35d7a 20 21 61 4e 65 77 20 29 7b 0a 20 20 20 20 72 65   !aNew ){.    re
35d7b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
35d7c 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 54  M;.  }..  if( pT
35d7d 53 2d 3e 6e 4f 75 74 70 75 74 3d 3d 30 20 29 7b  S->nOutput==0 ){
35d7e 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
35d7f 69 73 20 74 68 65 20 66 69 72 73 74 20 74 65 72  is the first ter
35d80 6d 20 73 65 6c 65 63 74 65 64 2c 20 63 6f 70 79  m selected, copy
35d81 20 74 68 65 20 64 6f 63 6c 69 73 74 20 74 6f 20   the doclist to 
35d82 74 68 65 20 6f 75 74 70 75 74 0a 20 20 20 20 2a  the output.    *
35d83 2a 20 62 75 66 66 65 72 20 75 73 69 6e 67 20 6d  * buffer using m
35d84 65 6d 63 70 79 28 29 2e 20 54 4f 44 4f 3a 20 41  emcpy(). TODO: A
35d85 64 64 20 61 20 77 61 79 20 74 6f 20 74 72 61 6e  dd a way to tran
35d86 73 66 65 72 20 63 6f 6e 74 72 6f 6c 20 6f 66 20  sfer control of 
35d87 74 68 65 0a 20 20 20 20 2a 2a 20 61 44 6f 63 6c  the.    ** aDocl
35d88 69 73 74 20 62 75 66 66 65 72 20 66 72 6f 6d 20  ist buffer from 
35d89 74 68 65 20 63 61 6c 6c 65 72 20 73 6f 20 61 73  the caller so as
35d8a 20 74 6f 20 61 76 6f 69 64 20 74 68 65 20 6d 65   to avoid the me
35d8b 6d 63 70 79 28 29 2e 0a 20 20 20 20 2a 2f 0a 20  mcpy()..    */. 
35d8c 20 20 20 6d 65 6d 63 70 79 28 61 4e 65 77 2c 20     memcpy(aNew, 
35d8d 61 44 6f 63 6c 69 73 74 2c 20 6e 44 6f 63 6c 69  aDoclist, nDocli
35d8e 73 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  st);.  }else{.  
35d8f 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74 20    /* The output 
35d90 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20 65 6d  buffer is not em
35d91 70 74 79 2e 20 4d 65 72 67 65 20 64 6f 63 6c 69  pty. Merge docli
35d92 73 74 20 61 44 6f 63 6c 69 73 74 20 77 69 74 68  st aDoclist with
35d93 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 78 69 73   the.    ** exis
35d94 74 69 6e 67 20 6f 75 74 70 75 74 2e 20 54 68 69  ting output. Thi
35d95 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65  s can only happe
35d96 6e 20 77 69 74 68 20 70 72 65 66 69 78 2d 73 65  n with prefix-se
35d97 61 72 63 68 65 73 20 28 61 73 0a 20 20 20 20 2a  arches (as.    *
35d98 2a 20 73 65 61 72 63 68 65 73 20 66 6f 72 20 65  * searches for e
35d99 78 61 63 74 20 74 65 72 6d 73 20 72 65 74 75 72  xact terms retur
35d9a 6e 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 64 6f  n exactly one do
35d9b 63 6c 69 73 74 29 2e 0a 20 20 20 20 2a 2f 0a 20  clist)..    */. 
35d9c 20 20 20 69 6e 74 20 6d 65 72 67 65 74 79 70 65     int mergetype
35d9d 20 3d 20 28 70 54 53 2d 3e 69 73 52 65 71 50 6f   = (pTS->isReqPo
35d9e 73 20 3f 20 4d 45 52 47 45 5f 50 4f 53 5f 4f 52  s ? MERGE_POS_OR
35d9f 20 3a 20 4d 45 52 47 45 5f 4f 52 29 3b 0a 20 20   : MERGE_OR);.  
35da0 20 20 66 74 73 33 44 6f 63 6c 69 73 74 4d 65 72    fts3DoclistMer
35da1 67 65 28 6d 65 72 67 65 74 79 70 65 2c 20 30 2c  ge(mergetype, 0,
35da2 20 30 2c 0a 20 20 20 20 20 20 20 20 61 4e 65 77   0,.        aNew
35da3 2c 20 26 6e 4e 65 77 2c 20 70 54 53 2d 3e 61 4f  , &nNew, pTS->aO
35da4 75 74 70 75 74 2c 20 70 54 53 2d 3e 6e 4f 75 74  utput, pTS->nOut
35da5 70 75 74 2c 20 61 44 6f 63 6c 69 73 74 2c 20 6e  put, aDoclist, n
35da6 44 6f 63 6c 69 73 74 0a 20 20 20 20 29 3b 0a 20  Doclist.    );. 
35da7 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   }..  sqlite3_fr
35da8 65 65 28 70 54 53 2d 3e 61 4f 75 74 70 75 74 29  ee(pTS->aOutput)
35da9 3b 0a 20 20 70 54 53 2d 3e 61 4f 75 74 70 75 74  ;.  pTS->aOutput
35daa 20 3d 20 61 4e 65 77 3b 0a 20 20 70 54 53 2d 3e   = aNew;.  pTS->
35dab 6e 4f 75 74 70 75 74 20 3d 20 6e 4e 65 77 3b 0a  nOutput = nNew;.
35dac 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
35dad 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
35dae 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 72  is function retr
35daf 65 69 76 65 73 20 74 68 65 20 64 6f 63 6c 69 73  eives the doclis
35db0 74 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66  t for the specif
35db1 69 65 64 20 74 65 72 6d 20 28 6f 72 20 74 65 72  ied term (or ter
35db2 6d 0a 2a 2a 20 70 72 65 66 69 78 29 20 66 72 6f  m.** prefix) fro
35db3 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  m the database. 
35db4 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
35db5 6e 65 64 20 64 6f 63 6c 69 73 74 20 6d 61 79 20  ned doclist may 
35db6 62 65 20 69 6e 20 6f 6e 65 20 6f 66 20 74 77 6f  be in one of two
35db7 20 66 6f 72 6d 61 74 73 2c 20 64 65 70 65 6e 64   formats, depend
35db8 69 6e 67 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 76  ing on the .** v
35db9 61 6c 75 65 20 6f 66 20 70 61 72 61 6d 65 74 65  alue of paramete
35dba 72 20 69 73 52 65 71 50 6f 73 2e 20 49 66 20 69  r isReqPos. If i
35dbb 73 52 65 71 50 6f 73 20 69 73 20 7a 65 72 6f 2c  sReqPos is zero,
35dbc 20 74 68 65 6e 20 74 68 65 20 64 6f 63 6c 69 73   then the doclis
35dbd 74 20 69 73 0a 2a 2a 20 61 20 73 6f 72 74 65 64  t is.** a sorted
35dbe 20 6c 69 73 74 20 6f 66 20 64 65 6c 74 61 2d 63   list of delta-c
35dbf 6f 6d 70 72 65 73 73 65 64 20 64 6f 63 69 64 73  ompressed docids
35dc0 2e 20 49 66 20 69 73 52 65 71 50 6f 73 20 69 73  . If isReqPos is
35dc1 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 0a 2a 2a 20 74   non-zero, .** t
35dc2 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64  hen the returned
35dc3 20 6c 69 73 74 20 69 73 20 69 6e 20 74 68 65 20   list is in the 
35dc4 73 61 6d 65 20 66 6f 72 6d 61 74 20 61 73 20 69  same format as i
35dc5 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a  s stored in the.
35dc6 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 74 68  ** database with
35dc7 6f 75 74 20 74 68 65 20 66 6f 75 6e 64 20 6c 65  out the found le
35dc8 6e 67 74 68 20 73 70 65 63 69 66 69 65 72 20 61  ngth specifier a
35dc9 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 6f  t the start of o
35dca 6e 2d 64 69 73 6b 0a 2a 2a 20 64 6f 63 6c 69 73  n-disk.** doclis
35dcb 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ts..*/.static in
35dcc 74 20 66 74 73 33 54 65 72 6d 53 65 6c 65 63 74  t fts3TermSelect
35dcd 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
35dce 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
35dcf 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74      /* Virtual t
35dd0 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  able handle */. 
35dd1 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20   int iColumn,   
35dd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35dd3 20 2f 2a 20 43 6f 6c 75 6d 6e 20 74 6f 20 71 75   /* Column to qu
35dd4 65 72 79 20 28 6f 72 20 2d 76 65 20 66 6f 72 20  ery (or -ve for 
35dd5 61 6c 6c 20 63 6f 6c 75 6d 6e 73 29 20 2a 2f 0a  all columns) */.
35dd6 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
35dd7 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  erm,            
35dd8 20 20 2f 2a 20 54 65 72 6d 20 74 6f 20 71 75 65    /* Term to que
35dd9 72 79 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  ry for */.  int 
35dda 6e 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20  nTerm,          
35ddb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
35ddc 69 7a 65 20 6f 66 20 7a 54 65 72 6d 20 69 6e 20  ize of zTerm in 
35ddd 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69  bytes */.  int i
35dde 73 50 72 65 66 69 78 2c 20 20 20 20 20 20 20 20  sPrefix,        
35ddf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
35de0 75 65 20 66 6f 72 20 61 20 70 72 65 66 69 78 20  ue for a prefix 
35de1 73 65 61 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20  search */.  int 
35de2 69 73 52 65 71 50 6f 73 2c 20 20 20 20 20 20 20  isReqPos,       
35de3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
35de4 72 75 65 20 74 6f 20 69 6e 63 6c 75 64 65 20 70  rue to include p
35de5 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73 20 69 6e  osition lists in
35de6 20 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74   output */.  int
35de7 20 2a 70 6e 4f 75 74 2c 20 20 20 20 20 20 20 20   *pnOut,        
35de8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35de9 4f 55 54 3a 20 53 69 7a 65 20 6f 66 20 62 75 66  OUT: Size of buf
35dea 66 65 72 20 61 74 20 2a 70 70 4f 75 74 20 2a 2f  fer at *ppOut */
35deb 0a 20 20 63 68 61 72 20 2a 2a 70 70 4f 75 74 20  .  char **ppOut 
35dec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35ded 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61 6c 6c 6f     /* OUT: Mallo
35dee 63 65 64 20 72 65 73 75 6c 74 20 62 75 66 66 65  ced result buffe
35def 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  r */.){.  int i;
35df0 0a 20 20 54 65 72 6d 53 65 6c 65 63 74 20 74 73  .  TermSelect ts
35df1 63 3b 0a 20 20 46 74 73 33 53 65 67 46 69 6c 74  c;.  Fts3SegFilt
35df2 65 72 20 66 69 6c 74 65 72 3b 20 20 20 20 20 20  er filter;      
35df3 20 20 20 20 20 2f 2a 20 53 65 67 6d 65 6e 74 20       /* Segment 
35df4 74 65 72 6d 20 66 69 6c 74 65 72 20 63 6f 6e 66  term filter conf
35df5 69 67 75 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 46  iguration */.  F
35df6 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a 61  ts3SegReader **a
35df7 70 53 65 67 6d 65 6e 74 20 3d 20 30 3b 20 20 2f  pSegment = 0;  /
35df8 2a 20 41 72 72 61 79 20 6f 66 20 73 65 67 6d 65  * Array of segme
35df9 6e 74 73 20 74 6f 20 72 65 61 64 20 64 61 74 61  nts to read data
35dfa 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e   from */.  int n
35dfb 53 65 67 6d 65 6e 74 20 3d 20 30 3b 20 20 20 20  Segment = 0;    
35dfc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
35dfd 7a 65 20 6f 66 20 61 70 53 65 67 6d 65 6e 74 20  ze of apSegment 
35dfe 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 6e  array */.  int n
35dff 41 6c 6c 6f 63 20 3d 20 30 3b 20 20 20 20 20 20  Alloc = 0;      
35e00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c             /* Al
35e01 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20  located size of 
35e02 73 65 67 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f  segment array */
35e03 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
35e04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35e05 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
35e06 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  e */.  sqlite3_s
35e07 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 20  tmt *pStmt;     
35e08 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74         /* SQL st
35e09 61 74 65 6d 65 6e 74 20 74 6f 20 73 63 61 6e 20  atement to scan 
35e0a 25 5f 73 65 67 64 69 72 20 74 61 62 6c 65 20 2a  %_segdir table *
35e0b 2f 0a 20 20 69 6e 74 20 69 41 67 65 20 3d 20 30  /.  int iAge = 0
35e0c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35e0d 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61      /* Used to a
35e0e 73 73 69 67 6e 20 61 67 65 73 20 74 6f 20 73 65  ssign ages to se
35e0f 67 6d 65 6e 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20  gments */..  /* 
35e10 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65  Loop through the
35e11 20 65 6e 74 69 72 65 20 25 5f 73 65 67 64 69 72   entire %_segdir
35e12 20 74 61 62 6c 65 2e 20 46 6f 72 20 65 61 63 68   table. For each
35e13 20 73 65 67 6d 65 6e 74 2c 20 63 72 65 61 74 65   segment, create
35e14 20 61 0a 20 20 2a 2a 20 46 74 73 33 53 65 67 52   a.  ** Fts3SegR
35e15 65 61 64 65 72 20 74 6f 20 69 74 65 72 61 74 65  eader to iterate
35e16 20 74 68 72 6f 75 67 68 20 74 68 65 20 73 75 62   through the sub
35e17 73 65 74 20 6f 66 20 74 68 65 20 73 65 67 6d 65  set of the segme
35e18 6e 74 20 6c 65 61 76 65 73 0a 20 20 2a 2a 20 74  nt leaves.  ** t
35e19 68 61 74 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20  hat may contain 
35e1a 61 20 74 65 72 6d 20 74 68 61 74 20 6d 61 74 63  a term that matc
35e1b 68 65 73 20 7a 54 65 72 6d 2f 6e 54 65 72 6d 2e  hes zTerm/nTerm.
35e1c 20 46 6f 72 20 6e 6f 6e 2d 70 72 65 66 69 78 0a   For non-prefix.
35e1d 20 20 2a 2a 20 73 65 61 72 63 68 65 73 2c 20 74    ** searches, t
35e1e 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 61 20  his is always a 
35e1f 73 69 6e 67 6c 65 20 6c 65 61 66 2e 20 46 6f 72  single leaf. For
35e20 20 70 72 65 66 69 78 20 73 65 61 72 63 68 65 73   prefix searches
35e21 2c 20 74 68 69 73 0a 20 20 2a 2a 20 6d 61 79 20  , this.  ** may 
35e22 62 65 20 61 20 63 6f 6e 74 69 67 75 6f 75 73 20  be a contiguous 
35e23 62 6c 6f 63 6b 20 6f 66 20 6c 65 61 76 65 73 2e  block of leaves.
35e24 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63  .  **.  ** The c
35e25 6f 64 65 20 69 6e 20 74 68 69 73 20 6c 6f 6f 70  ode in this loop
35e26 20 64 6f 65 73 20 6e 6f 74 20 61 63 74 75 61 6c   does not actual
35e27 6c 79 20 6c 6f 61 64 20 61 6e 79 20 6c 65 61 76  ly load any leav
35e28 65 73 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 20  es into memory. 
35e29 20 2a 2a 20 28 75 6e 6c 65 73 73 20 74 68 65 20   ** (unless the 
35e2a 72 6f 6f 74 20 6e 6f 64 65 20 68 61 70 70 65 6e  root node happen
35e2b 73 20 74 6f 20 62 65 20 61 20 6c 65 61 66 29 2e  s to be a leaf).
35e2c 20 49 74 20 73 69 6d 70 6c 79 20 65 78 61 6d 69   It simply exami
35e2d 6e 65 73 20 74 68 65 0a 20 20 2a 2a 20 62 2d 74  nes the.  ** b-t
35e2e 72 65 65 20 73 74 72 75 63 74 75 72 65 20 74 6f  ree structure to
35e2f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 69 63 68   determine which
35e30 20 6c 65 61 76 65 73 20 6e 65 65 64 20 74 6f 20   leaves need to 
35e31 62 65 20 69 6e 73 70 65 63 74 65 64 2e 0a 20 20  be inspected..  
35e32 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
35e33 33 46 74 73 33 41 6c 6c 53 65 67 64 69 72 73 28  3Fts3AllSegdirs(
35e34 70 2c 20 26 70 53 74 6d 74 29 3b 0a 20 20 77 68  p, &pStmt);.  wh
35e35 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
35e36 4f 4b 20 26 26 20 53 51 4c 49 54 45 5f 52 4f 57  OK && SQLITE_ROW
35e37 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  ==(rc = sqlite3_
35e38 73 74 65 70 28 70 53 74 6d 74 29 29 20 29 7b 0a  step(pStmt)) ){.
35e39 20 20 20 20 46 74 73 33 53 65 67 52 65 61 64 65      Fts3SegReade
35e3a 72 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20 20 20  r *pNew = 0;.   
35e3b 20 69 6e 74 20 6e 52 6f 6f 74 20 3d 20 73 71 6c   int nRoot = sql
35e3c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
35e3d 73 28 70 53 74 6d 74 2c 20 34 29 3b 0a 20 20 20  s(pStmt, 4);.   
35e3e 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 52 6f   char const *zRo
35e3f 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ot = sqlite3_col
35e40 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20  umn_blob(pStmt, 
35e41 34 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  4);.    if( sqli
35e42 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
35e43 28 70 53 74 6d 74 2c 20 31 29 3d 3d 30 20 29 7b  (pStmt, 1)==0 ){
35e44 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 6e  .      /* The en
35e45 74 69 72 65 20 73 65 67 6d 65 6e 74 20 69 73 20  tire segment is 
35e46 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20 72 6f  stored on the ro
35e47 6f 74 20 6e 6f 64 65 20 28 77 68 69 63 68 20 6d  ot node (which m
35e48 75 73 74 20 62 65 20 61 0a 20 20 20 20 20 20 2a  ust be a.      *
35e49 2a 20 6c 65 61 66 29 2e 20 44 6f 20 6e 6f 74 20  * leaf). Do not 
35e4a 62 6f 74 68 65 72 20 69 6e 73 70 65 63 74 69 6e  bother inspectin
35e4b 67 20 61 6e 79 20 64 61 74 61 20 69 6e 20 74 68  g any data in th
35e4c 69 73 20 63 61 73 65 2c 20 6a 75 73 74 0a 20 20  is case, just.  
35e4d 20 20 20 20 2a 2a 20 63 72 65 61 74 65 20 61 20      ** create a 
35e4e 46 74 73 33 53 65 67 52 65 61 64 65 72 20 74 6f  Fts3SegReader to
35e4f 20 73 63 61 6e 20 74 68 65 20 73 69 6e 67 6c 65   scan the single
35e50 20 6c 65 61 66 2e 20 0a 20 20 20 20 20 20 2a 2f   leaf. .      */
35e51 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
35e52 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
35e53 4e 65 77 28 70 2c 20 69 41 67 65 2c 20 30 2c 20  New(p, iAge, 0, 
35e54 30 2c 20 30 2c 20 7a 52 6f 6f 74 2c 20 6e 52 6f  0, 0, zRoot, nRo
35e55 6f 74 2c 20 26 70 4e 65 77 29 3b 0a 20 20 20 20  ot, &pNew);.    
35e56 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
35e57 20 72 63 32 3b 20 20 20 20 20 20 20 20 20 20 20   rc2;           
35e58 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
35e59 72 6e 20 76 61 6c 75 65 20 6f 66 20 73 71 6c 69  rn value of sqli
35e5a 74 65 33 46 74 73 33 52 65 61 64 42 6c 6f 63 6b  te3Fts3ReadBlock
35e5b 28 29 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  () */.      sqli
35e5c 74 65 33 5f 69 6e 74 36 34 20 69 31 3b 20 20 20  te3_int64 i1;   
35e5d 20 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b          /* Block
35e5e 69 64 20 6f 66 20 6c 65 61 66 20 74 68 61 74 20  id of leaf that 
35e5f 6d 61 79 20 63 6f 6e 74 61 69 6e 20 7a 54 65 72  may contain zTer
35e60 6d 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20  m */.      rc = 
35e61 66 74 73 33 53 65 6c 65 63 74 4c 65 61 66 28 70  fts3SelectLeaf(p
35e62 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , zTerm, nTerm, 
35e63 7a 52 6f 6f 74 2c 20 6e 52 6f 6f 74 2c 20 26 69  zRoot, nRoot, &i
35e64 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  1);.      if( rc
35e65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
35e66 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
35e67 6e 74 36 34 20 69 32 20 3d 20 73 71 6c 69 74 65  nt64 i2 = sqlite
35e68 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70  3_column_int64(p
35e69 53 74 6d 74 2c 20 32 29 3b 0a 20 20 20 20 20 20  Stmt, 2);.      
35e6a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74    rc = sqlite3Ft
35e6b 73 33 53 65 67 52 65 61 64 65 72 4e 65 77 28 70  s3SegReaderNew(p
35e6c 2c 20 69 41 67 65 2c 20 69 31 2c 20 69 32 2c 20  , iAge, i1, i2, 
35e6d 30 2c 20 30 2c 20 30 2c 20 26 70 4e 65 77 29 3b  0, 0, 0, &pNew);
35e6e 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
35e6f 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
35e70 20 63 61 6c 6c 20 74 6f 20 52 65 61 64 42 6c 6f   call to ReadBlo
35e71 63 6b 28 29 20 73 65 72 76 65 73 20 74 6f 20 72  ck() serves to r
35e72 65 73 65 74 20 74 68 65 20 53 51 4c 20 73 74 61  eset the SQL sta
35e73 74 65 6d 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20  tement.      ** 
35e74 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65  used to retrieve
35e75 20 62 6c 6f 63 6b 73 20 6f 66 20 64 61 74 61 20   blocks of data 
35e76 66 72 6f 6d 20 74 68 65 20 25 5f 73 65 67 6d 65  from the %_segme
35e77 6e 74 73 20 74 61 62 6c 65 2e 20 49 66 20 69 74  nts table. If it
35e78 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 6e 6f 74   is.      ** not
35e79 20 72 65 73 65 74 20 68 65 72 65 2c 20 74 68 65   reset here, the
35e7a 6e 20 69 74 20 6d 61 79 20 72 65 6d 61 69 6e 20  n it may remain 
35e7b 63 6c 61 73 73 69 66 69 65 64 20 61 73 20 61 6e  classified as an
35e7c 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e   active statemen
35e7d 74 20 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 53  t .      ** by S
35e7e 51 4c 69 74 65 2c 20 77 68 69 63 68 20 6d 61 79  QLite, which may
35e7f 20 6c 65 61 64 20 74 6f 20 22 44 52 4f 50 20 54   lead to "DROP T
35e80 41 42 4c 45 22 20 6f 72 20 22 44 45 54 41 43 48  ABLE" or "DETACH
35e81 22 20 63 6f 6d 6d 61 6e 64 73 20 0a 20 20 20 20  " commands .    
35e82 20 20 2a 2a 20 66 61 69 6c 69 6e 67 2e 0a 20 20    ** failing..  
35e83 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 20 72 63      */ .      rc
35e84 32 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 52  2 = sqlite3Fts3R
35e85 65 61 64 42 6c 6f 63 6b 28 70 2c 20 30 2c 20 30  eadBlock(p, 0, 0
35e86 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
35e87 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
35e88 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63  .        rc = rc
35e89 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  2;.      }.    }
35e8a 0a 20 20 20 20 69 41 67 65 2b 2b 3b 0a 0a 20 20  .    iAge++;..  
35e8b 20 20 2f 2a 20 49 66 20 61 20 6e 65 77 20 46 74    /* If a new Ft
35e8c 73 33 53 65 67 52 65 61 64 65 72 20 77 61 73 20  s3SegReader was 
35e8d 61 6c 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 69  allocated, add i
35e8e 74 20 74 6f 20 74 68 65 20 61 70 53 65 67 6d 65  t to the apSegme
35e8f 6e 74 20 61 72 72 61 79 2e 20 2a 2f 0a 20 20 20  nt array. */.   
35e90 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30   assert( pNew!=0
35e91 20 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f   || rc!=SQLITE_O
35e92 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 65  K );.    if( pNe
35e93 77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  w ){.      if( n
35e94 53 65 67 6d 65 6e 74 3d 3d 6e 41 6c 6c 6f 63 20  Segment==nAlloc 
35e95 29 7b 0a 20 20 20 20 20 20 20 20 46 74 73 33 53  ){.        Fts3S
35e96 65 67 52 65 61 64 65 72 20 2a 2a 70 41 72 72 61  egReader **pArra
35e97 79 3b 0a 20 20 20 20 20 20 20 20 6e 41 6c 6c 6f  y;.        nAllo
35e98 63 20 2b 3d 20 31 36 3b 0a 20 20 20 20 20 20 20  c += 16;.       
35e99 20 70 41 72 72 61 79 20 3d 20 28 46 74 73 33 53   pArray = (Fts3S
35e9a 65 67 52 65 61 64 65 72 20 2a 2a 29 73 71 6c 69  egReader **)sqli
35e9b 74 65 33 5f 72 65 61 6c 6c 6f 63 28 0a 20 20 20  te3_realloc(.   
35e9c 20 20 20 20 20 20 20 20 20 61 70 53 65 67 6d 65           apSegme
35e9d 6e 74 2c 20 6e 41 6c 6c 6f 63 2a 73 69 7a 65 6f  nt, nAlloc*sizeo
35e9e 66 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20  f(Fts3SegReader 
35e9f 2a 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20  *).        );.  
35ea0 20 20 20 20 20 20 69 66 28 20 21 70 41 72 72 61        if( !pArra
35ea1 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  y ){.          s
35ea2 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61  qlite3Fts3SegRea
35ea3 64 65 72 46 72 65 65 28 70 2c 20 70 4e 65 77 29  derFree(p, pNew)
35ea4 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ;.          rc =
35ea5 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
35ea6 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 69           goto fi
35ea7 6e 69 73 68 65 64 3b 0a 20 20 20 20 20 20 20 20  nished;.        
35ea8 7d 0a 20 20 20 20 20 20 20 20 61 70 53 65 67 6d  }.        apSegm
35ea9 65 6e 74 20 3d 20 70 41 72 72 61 79 3b 0a 20 20  ent = pArray;.  
35eaa 20 20 20 20 7d 0a 20 20 20 20 20 20 61 70 53 65      }.      apSe
35eab 67 6d 65 6e 74 5b 6e 53 65 67 6d 65 6e 74 2b 2b  gment[nSegment++
35eac 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a  ] = pNew;.    }.
35ead 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51    }.  if( rc!=SQ
35eae 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
35eaf 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
35eb0 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 67 6f  ITE_OK );.    go
35eb1 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 7d  to finished;.  }
35eb2 0a 0a 20 20 6d 65 6d 73 65 74 28 26 74 73 63 2c  ..  memset(&tsc,
35eb3 20 30 2c 20 73 69 7a 65 6f 66 28 54 65 72 6d 53   0, sizeof(TermS
35eb4 65 6c 65 63 74 29 29 3b 0a 20 20 74 73 63 2e 69  elect));.  tsc.i
35eb5 73 52 65 71 50 6f 73 20 3d 20 69 73 52 65 71 50  sReqPos = isReqP
35eb6 6f 73 3b 0a 0a 20 20 66 69 6c 74 65 72 2e 66 6c  os;..  filter.fl
35eb7 61 67 73 20 3d 20 46 54 53 33 5f 53 45 47 4d 45  ags = FTS3_SEGME
35eb8 4e 54 5f 49 47 4e 4f 52 45 5f 45 4d 50 54 59 20  NT_IGNORE_EMPTY 
35eb9 0a 20 20 20 20 20 20 20 20 7c 20 28 69 73 50 72  .        | (isPr
35eba 65 66 69 78 20 3f 20 46 54 53 33 5f 53 45 47 4d  efix ? FTS3_SEGM
35ebb 45 4e 54 5f 50 52 45 46 49 58 20 3a 20 30 29 0a  ENT_PREFIX : 0).
35ebc 20 20 20 20 20 20 20 20 7c 20 28 69 73 52 65 71          | (isReq
35ebd 50 6f 73 20 3f 20 46 54 53 33 5f 53 45 47 4d 45  Pos ? FTS3_SEGME
35ebe 4e 54 5f 52 45 51 55 49 52 45 5f 50 4f 53 20 3a  NT_REQUIRE_POS :
35ebf 20 30 29 0a 20 20 20 20 20 20 20 20 7c 20 28 69   0).        | (i
35ec0 43 6f 6c 75 6d 6e 3c 70 2d 3e 6e 43 6f 6c 75 6d  Column<p->nColum
35ec1 6e 20 3f 20 46 54 53 33 5f 53 45 47 4d 45 4e 54  n ? FTS3_SEGMENT
35ec2 5f 43 4f 4c 55 4d 4e 5f 46 49 4c 54 45 52 20 3a  _COLUMN_FILTER :
35ec3 20 30 29 3b 0a 20 20 66 69 6c 74 65 72 2e 69 43   0);.  filter.iC
35ec4 6f 6c 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20  ol = iColumn;.  
35ec5 66 69 6c 74 65 72 2e 7a 54 65 72 6d 20 3d 20 7a  filter.zTerm = z
35ec6 54 65 72 6d 3b 0a 20 20 66 69 6c 74 65 72 2e 6e  Term;.  filter.n
35ec7 54 65 72 6d 20 3d 20 6e 54 65 72 6d 3b 0a 0a 20  Term = nTerm;.. 
35ec8 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
35ec9 33 53 65 67 52 65 61 64 65 72 49 74 65 72 61 74  3SegReaderIterat
35eca 65 28 70 2c 20 61 70 53 65 67 6d 65 6e 74 2c 20  e(p, apSegment, 
35ecb 6e 53 65 67 6d 65 6e 74 2c 20 26 66 69 6c 74 65  nSegment, &filte
35ecc 72 2c 0a 20 20 20 20 20 20 66 74 73 33 54 65 72  r,.      fts3Ter
35ecd 6d 53 65 6c 65 63 74 43 62 2c 20 28 76 6f 69 64  mSelectCb, (void
35ece 20 2a 29 26 74 73 63 0a 20 20 29 3b 0a 0a 20 20   *)&tsc.  );..  
35ecf 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
35ed0 4b 20 29 7b 0a 20 20 20 20 2a 70 70 4f 75 74 20  K ){.    *ppOut 
35ed1 3d 20 74 73 63 2e 61 4f 75 74 70 75 74 3b 0a 20  = tsc.aOutput;. 
35ed2 20 20 20 2a 70 6e 4f 75 74 20 3d 20 74 73 63 2e     *pnOut = tsc.
35ed3 6e 4f 75 74 70 75 74 3b 0a 20 20 7d 65 6c 73 65  nOutput;.  }else
35ed4 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
35ed5 65 65 28 74 73 63 2e 61 4f 75 74 70 75 74 29 3b  ee(tsc.aOutput);
35ed6 0a 20 20 7d 0a 0a 66 69 6e 69 73 68 65 64 3a 0a  .  }..finished:.
35ed7 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
35ed8 70 53 74 6d 74 29 3b 0a 20 20 66 6f 72 28 69 3d  pStmt);.  for(i=
35ed9 30 3b 20 69 3c 6e 53 65 67 6d 65 6e 74 3b 20 69  0; i<nSegment; i
35eda 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
35edb 46 74 73 33 53 65 67 52 65 61 64 65 72 46 72 65  Fts3SegReaderFre
35edc 65 28 70 2c 20 61 70 53 65 67 6d 65 6e 74 5b 69  e(p, apSegment[i
35edd 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ]);.  }.  sqlite
35ede 33 5f 66 72 65 65 28 61 70 53 65 67 6d 65 6e 74  3_free(apSegment
35edf 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
35ee0 7d 0a 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72  }.../* .** Retur
35ee1 6e 20 61 20 44 6f 63 4c 69 73 74 20 63 6f 72 72  n a DocList corr
35ee2 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65  esponding to the
35ee3 20 70 68 72 61 73 65 20 2a 70 50 68 72 61 73 65   phrase *pPhrase
35ee4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35ee5 66 74 73 33 50 68 72 61 73 65 53 65 6c 65 63 74  fts3PhraseSelect
35ee6 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
35ee7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
35ee8 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74      /* Virtual t
35ee9 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  able handle */. 
35eea 20 46 74 73 33 50 68 72 61 73 65 20 2a 70 50 68   Fts3Phrase *pPh
35eeb 72 61 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  rase,           
35eec 20 2f 2a 20 50 68 72 61 73 65 20 74 6f 20 72 65   /* Phrase to re
35eed 74 75 72 6e 20 61 20 64 6f 63 6c 69 73 74 20 66  turn a doclist f
35eee 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 65  or */.  int isRe
35eef 71 50 6f 73 2c 20 20 20 20 20 20 20 20 20 20 20  qPos,           
35ef0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
35ef1 69 66 20 6f 75 74 70 75 74 20 73 68 6f 75 6c 64  if output should
35ef2 20 63 6f 6e 74 61 69 6e 20 70 6f 73 69 74 69 6f   contain positio
35ef3 6e 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  ns */.  char **p
35ef4 61 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20 20  aOut,           
35ef5 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
35ef6 50 6f 69 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f  Pointer to mallo
35ef7 63 27 64 20 72 65 73 75 6c 74 20 62 75 66 66 65  c'd result buffe
35ef8 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4f 75  r */.  int *pnOu
35ef9 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
35efa 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53         /* OUT: S
35efb 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 61 74  ize of buffer at
35efc 20 2a 70 61 4f 75 74 20 2a 2f 0a 29 7b 0a 20 20   *paOut */.){.  
35efd 63 68 61 72 20 2a 70 4f 75 74 20 3d 20 30 3b 0a  char *pOut = 0;.
35efe 20 20 69 6e 74 20 6e 4f 75 74 20 3d 20 30 3b 0a    int nOut = 0;.
35eff 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
35f00 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  E_OK;.  int ii;.
35f01 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 50 68    int iCol = pPh
35f02 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  rase->iColumn;. 
35f03 20 69 6e 74 20 69 73 54 65 72 6d 50 6f 73 20 3d   int isTermPos =
35f04 20 28 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65   (pPhrase->nToke
35f05 6e 3e 31 20 7c 7c 20 69 73 52 65 71 50 6f 73 29  n>1 || isReqPos)
35f06 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ;..  assert( p->
35f07 6e 50 65 6e 64 69 6e 67 44 61 74 61 3d 3d 30 20  nPendingData==0 
35f08 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  );..  for(ii=0; 
35f09 69 69 3c 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  ii<pPhrase->nTok
35f0a 65 6e 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73  en; ii++){.    s
35f0b 74 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65  truct PhraseToke
35f0c 6e 20 2a 70 54 6f 6b 20 3d 20 26 70 50 68 72 61  n *pTok = &pPhra
35f0d 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d 3b 0a  se->aToken[ii];.
35f0e 20 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 70 54      char *z = pT
35f0f 6f 6b 2d 3e 7a 3b 20 20 20 20 20 20 20 20 20 20  ok->z;          
35f10 20 20 2f 2a 20 4e 65 78 74 20 74 6f 6b 65 6e 20    /* Next token 
35f11 6f 66 20 74 68 65 20 70 68 72 61 73 65 20 2a 2f  of the phrase */
35f12 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 54 6f  .    int n = pTo
35f13 6b 2d 3e 6e 3b 20 20 20 20 20 20 20 20 20 20 20  k->n;           
35f14 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 20     /* Size of z 
35f15 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20  in bytes */.    
35f16 69 6e 74 20 69 73 50 72 65 66 69 78 20 3d 20 70  int isPrefix = p
35f17 54 6f 6b 2d 3e 69 73 50 72 65 66 69 78 3b 2f 2a  Tok->isPrefix;/*
35f18 20 54 72 75 65 20 69 66 20 74 6f 6b 65 6e 20 69   True if token i
35f19 73 20 61 20 70 72 65 66 69 78 20 2a 2f 0a 20 20  s a prefix */.  
35f1a 20 20 63 68 61 72 20 2a 70 4c 69 73 74 3b 20 20    char *pList;  
35f1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35f1c 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f  /* Pointer to to
35f1d 6b 65 6e 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20  ken doclist */. 
35f1e 20 20 20 69 6e 74 20 6e 4c 69 73 74 3b 20 20 20     int nList;   
35f1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35f20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66   /* Size of buff
35f21 65 72 20 61 74 20 70 4c 69 73 74 20 2a 2f 0a 0a  er at pList */..
35f22 20 20 20 20 72 63 20 3d 20 66 74 73 33 54 65 72      rc = fts3Ter
35f23 6d 53 65 6c 65 63 74 28 70 2c 20 69 43 6f 6c 2c  mSelect(p, iCol,
35f24 20 7a 2c 20 6e 2c 20 69 73 50 72 65 66 69 78 2c   z, n, isPrefix,
35f25 20 69 73 54 65 72 6d 50 6f 73 2c 20 26 6e 4c 69   isTermPos, &nLi
35f26 73 74 2c 20 26 70 4c 69 73 74 29 3b 0a 20 20 20  st, &pList);.   
35f27 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35f28 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20  OK ) break;..   
35f29 20 69 66 28 20 69 69 3d 3d 30 20 29 7b 0a 20 20   if( ii==0 ){.  
35f2a 20 20 20 20 70 4f 75 74 20 3d 20 70 4c 69 73 74      pOut = pList
35f2b 3b 0a 20 20 20 20 20 20 6e 4f 75 74 20 3d 20 6e  ;.      nOut = n
35f2c 4c 69 73 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  List;.    }else{
35f2d 0a 20 20 20 20 20 20 2f 2a 20 4d 65 72 67 65 20  .      /* Merge 
35f2e 74 68 65 20 6e 65 77 20 74 65 72 6d 20 6c 69 73  the new term lis
35f2f 74 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e  t and the curren
35f30 74 20 6f 75 74 70 75 74 2e 20 49 66 20 74 68 69  t output. If thi
35f31 73 20 69 73 20 74 68 65 0a 20 20 20 20 20 20 2a  s is the.      *
35f32 2a 20 6c 61 73 74 20 74 65 72 6d 20 69 6e 20 74  * last term in t
35f33 68 65 20 70 68 72 61 73 65 2c 20 61 6e 64 20 70  he phrase, and p
35f34 6f 73 69 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74  ositions are not
35f35 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 65   required in the
35f36 0a 20 20 20 20 20 20 2a 2a 20 6f 75 74 70 75 74  .      ** output
35f37 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
35f38 6e 2c 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73  n, the positions
35f39 20 63 61 6e 20 62 65 20 64 72 6f 70 70 65 64 20   can be dropped 
35f3a 61 73 20 70 61 72 74 0a 20 20 20 20 20 20 2a 2a  as part.      **
35f3b 20 6f 66 20 74 68 69 73 20 6d 65 72 67 65 2e 20   of this merge. 
35f3c 45 69 74 68 65 72 20 77 61 79 2c 20 74 68 65 20  Either way, the 
35f3d 72 65 73 75 6c 74 20 6f 66 20 74 68 69 73 20 6d  result of this m
35f3e 65 72 67 65 20 77 69 6c 6c 20 62 65 0a 20 20 20  erge will be.   
35f3f 20 20 20 2a 2a 20 73 6d 61 6c 6c 65 72 20 74 68     ** smaller th
35f40 61 6e 20 6e 4c 69 73 74 20 62 79 74 65 73 2e 20  an nList bytes. 
35f41 54 68 65 20 63 6f 64 65 20 69 6e 20 66 74 73 33  The code in fts3
35f42 44 6f 63 6c 69 73 74 4d 65 72 67 65 28 29 20 69  DoclistMerge() i
35f43 73 20 77 72 69 74 74 65 6e 0a 20 20 20 20 20 20  s written.      
35f44 2a 2a 20 73 6f 20 74 68 61 74 20 69 74 20 69 73  ** so that it is
35f45 20 73 61 66 65 20 74 6f 20 75 73 65 20 70 4c 69   safe to use pLi
35f46 73 74 20 61 73 20 74 68 65 20 6f 75 74 70 75 74  st as the output
35f47 20 61 73 20 77 65 6c 6c 20 61 73 20 61 6e 20 69   as well as an i
35f48 6e 70 75 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e  nput.      ** in
35f49 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20   this case..    
35f4a 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6d    */.      int m
35f4b 65 72 67 65 74 79 70 65 20 3d 20 4d 45 52 47 45  ergetype = MERGE
35f4c 5f 50 4f 53 5f 50 48 52 41 53 45 3b 0a 20 20 20  _POS_PHRASE;.   
35f4d 20 20 20 69 66 28 20 69 69 3d 3d 70 50 68 72 61     if( ii==pPhra
35f4e 73 65 2d 3e 6e 54 6f 6b 65 6e 2d 31 20 26 26 20  se->nToken-1 && 
35f4f 21 69 73 52 65 71 50 6f 73 20 29 7b 0a 20 20 20  !isReqPos ){.   
35f50 20 20 20 20 20 6d 65 72 67 65 74 79 70 65 20 3d       mergetype =
35f51 20 4d 45 52 47 45 5f 50 48 52 41 53 45 3b 0a 20   MERGE_PHRASE;. 
35f52 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 74 73       }.      fts
35f53 33 44 6f 63 6c 69 73 74 4d 65 72 67 65 28 6d 65  3DoclistMerge(me
35f54 72 67 65 74 79 70 65 2c 20 30 2c 20 30 2c 20 70  rgetype, 0, 0, p
35f55 4c 69 73 74 2c 20 26 6e 4f 75 74 2c 20 70 4f 75  List, &nOut, pOu
35f56 74 2c 20 6e 4f 75 74 2c 20 70 4c 69 73 74 2c 20  t, nOut, pList, 
35f57 6e 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 73 71  nList);.      sq
35f58 6c 69 74 65 33 5f 66 72 65 65 28 70 4f 75 74 29  lite3_free(pOut)
35f59 3b 0a 20 20 20 20 20 20 70 4f 75 74 20 3d 20 70  ;.      pOut = p
35f5a 4c 69 73 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  List;.    }.  }.
35f5b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
35f5c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 61 4f  E_OK ){.    *paO
35f5d 75 74 20 3d 20 70 4f 75 74 3b 0a 20 20 20 20 2a  ut = pOut;.    *
35f5e 70 6e 4f 75 74 20 3d 20 6e 4f 75 74 3b 0a 20 20  pnOut = nOut;.  
35f5f 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
35f60 65 33 5f 66 72 65 65 28 70 4f 75 74 29 3b 0a 20  e3_free(pOut);. 
35f61 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
35f62 7d 0a 0a 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74  }../*.** Evaluat
35f63 65 20 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20  e the full-text 
35f64 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72  expression pExpr
35f65 20 61 67 61 69 6e 73 74 20 66 74 73 33 20 74 61   against fts3 ta
35f66 62 6c 65 20 70 54 61 62 2e 20 53 74 6f 72 65 0a  ble pTab. Store.
35f67 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  ** the resulting
35f68 20 64 6f 63 6c 69 73 74 20 69 6e 20 2a 70 61 4f   doclist in *paO
35f69 75 74 20 61 6e 64 20 2a 70 6e 4f 75 74 2e 0a 2a  ut and *pnOut..*
35f6a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 76 61  /.static int eva
35f6b 6c 46 74 73 33 45 78 70 72 28 0a 20 20 46 74 73  lFts3Expr(.  Fts
35f6c 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
35f6d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35f6e 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61  Virtual table ha
35f6f 6e 64 6c 65 20 2a 2f 0a 20 20 46 74 73 33 45 78  ndle */.  Fts3Ex
35f70 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20  pr *pExpr,      
35f71 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
35f72 73 65 64 20 66 74 73 33 20 65 78 70 72 65 73 73  sed fts3 express
35f73 69 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ion */.  char **
35f74 70 61 4f 75 74 2c 20 20 20 20 20 20 20 20 20 20  paOut,          
35f75 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
35f76 20 50 6f 69 6e 74 65 72 20 74 6f 20 6d 61 6c 6c   Pointer to mall
35f77 6f 63 27 64 20 72 65 73 75 6c 74 20 62 75 66 66  oc'd result buff
35f78 65 72 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4f  er */.  int *pnO
35f79 75 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ut              
35f7a 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
35f7b 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72 20 61  Size of buffer a
35f7c 74 20 2a 70 61 4f 75 74 20 2a 2f 0a 29 7b 0a 20  t *paOut */.){. 
35f7d 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
35f7e 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20  _OK;            
35f7f 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
35f80 2a 2f 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68  */..  /* Zero th
35f81 65 20 6f 75 74 70 75 74 20 70 61 72 61 6d 65 74  e output paramet
35f82 65 72 73 2e 20 2a 2f 0a 20 20 2a 70 61 4f 75 74  ers. */.  *paOut
35f83 20 3d 20 30 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d   = 0;.  *pnOut =
35f84 20 30 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72   0;..  if( pExpr
35f85 20 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70   ){.    if( pExp
35f86 72 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  r->eType==FTSQUE
35f87 52 59 5f 50 48 52 41 53 45 20 29 7b 0a 20 20 20  RY_PHRASE ){.   
35f88 20 20 20 69 6e 74 20 69 73 52 65 71 50 6f 73 20     int isReqPos 
35f89 3d 20 28 70 45 78 70 72 2d 3e 70 50 61 72 65 6e  = (pExpr->pParen
35f8a 74 20 26 26 20 70 45 78 70 72 2d 3e 70 50 61 72  t && pExpr->pPar
35f8b 65 6e 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51  ent->eType==FTSQ
35f8c 55 45 52 59 5f 4e 45 41 52 29 3b 0a 20 20 20 20  UERY_NEAR);.    
35f8d 20 20 72 63 20 3d 20 66 74 73 33 50 68 72 61 73    rc = fts3Phras
35f8e 65 53 65 6c 65 63 74 28 70 2c 20 70 45 78 70 72  eSelect(p, pExpr
35f8f 2d 3e 70 50 68 72 61 73 65 2c 20 69 73 52 65 71  ->pPhrase, isReq
35f90 50 6f 73 2c 20 70 61 4f 75 74 2c 20 70 6e 4f 75  Pos, paOut, pnOu
35f91 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
35f92 20 20 20 20 20 63 68 61 72 20 2a 61 4c 65 66 74       char *aLeft
35f93 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 61 52  ;.      char *aR
35f94 69 67 68 74 3b 0a 20 20 20 20 20 20 69 6e 74 20  ight;.      int 
35f95 6e 4c 65 66 74 3b 0a 20 20 20 20 20 20 69 6e 74  nLeft;.      int
35f96 20 6e 52 69 67 68 74 3b 0a 0a 20 20 20 20 20 20   nRight;..      
35f97 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28  if( SQLITE_OK==(
35f98 72 63 20 3d 20 65 76 61 6c 46 74 73 33 45 78 70  rc = evalFts3Exp
35f99 72 28 70 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  r(p, pExpr->pRig
35f9a 68 74 2c 20 26 61 52 69 67 68 74 2c 20 26 6e 52  ht, &aRight, &nR
35f9b 69 67 68 74 29 29 0a 20 20 20 20 20 20 20 26 26  ight)).       &&
35f9c 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
35f9d 3d 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 70  = evalFts3Expr(p
35f9e 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
35f9f 26 61 4c 65 66 74 2c 20 26 6e 4c 65 66 74 29 29  &aLeft, &nLeft))
35fa0 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
35fa1 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
35fa2 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
35fa3 5f 4e 45 41 52 20 7c 7c 20 70 45 78 70 72 2d 3e  _NEAR || pExpr->
35fa4 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
35fa5 4f 52 20 20 20 20 20 0a 20 20 20 20 20 20 20 20  OR     .        
35fa6 20 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 65 54      || pExpr->eT
35fa7 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 41 4e  ype==FTSQUERY_AN
35fa8 44 20 20 7c 7c 20 70 45 78 70 72 2d 3e 65 54 79  D  || pExpr->eTy
35fa9 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 4f 54  pe==FTSQUERY_NOT
35faa 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
35fab 20 20 20 20 73 77 69 74 63 68 28 20 70 45 78 70      switch( pExp
35fac 72 2d 3e 65 54 79 70 65 20 29 7b 0a 20 20 20 20  r->eType ){.    
35fad 20 20 20 20 20 20 63 61 73 65 20 46 54 53 51 55        case FTSQU
35fae 45 52 59 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20  ERY_NEAR: {.    
35faf 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72          Fts3Expr
35fb0 20 2a 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 20   *pLeft;.       
35fb1 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a 70       Fts3Expr *p
35fb2 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 20  Right;.         
35fb3 20 20 20 69 6e 74 20 6d 65 72 67 65 74 79 70 65     int mergetype
35fb4 20 3d 20 4d 45 52 47 45 5f 4e 45 41 52 3b 0a 20   = MERGE_NEAR;. 
35fb5 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e             int n
35fb6 50 61 72 61 6d 31 3b 0a 20 20 20 20 20 20 20 20  Param1;.        
35fb7 20 20 20 20 69 6e 74 20 6e 50 61 72 61 6d 32 3b      int nParam2;
35fb8 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61  .            cha
35fb9 72 20 2a 61 42 75 66 66 65 72 3b 0a 20 20 20 20  r *aBuffer;.    
35fba 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 20 20         .        
35fbb 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70      if( pExpr->p
35fbc 50 61 72 65 6e 74 20 26 26 20 70 45 78 70 72 2d  Parent && pExpr-
35fbd 3e 70 50 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d  >pParent->eType=
35fbe 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29  =FTSQUERY_NEAR )
35fbf 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
35fc0 6d 65 72 67 65 74 79 70 65 20 3d 20 4d 45 52 47  mergetype = MERG
35fc1 45 5f 50 4f 53 5f 4e 45 41 52 3b 0a 20 20 20 20  E_POS_NEAR;.    
35fc2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
35fc3 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70 45        pLeft = pE
35fc4 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  xpr->pLeft;.    
35fc5 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
35fc6 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53  Left->eType==FTS
35fc7 51 55 45 52 59 5f 4e 45 41 52 20 29 7b 20 0a 20  QUERY_NEAR ){ . 
35fc8 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4c 65               pLe
35fc9 66 74 3d 70 4c 65 66 74 2d 3e 70 52 69 67 68 74  ft=pLeft->pRight
35fca 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
35fcb 20 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67              pRig
35fcc 68 74 20 3d 20 70 45 78 70 72 2d 3e 70 52 69 67  ht = pExpr->pRig
35fcd 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ht;.            
35fce 61 73 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e  assert( pRight->
35fcf 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
35fd0 50 48 52 41 53 45 20 29 3b 0a 20 20 20 20 20 20  PHRASE );.      
35fd1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c        assert( pL
35fd2 65 66 74 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51  eft->eType==FTSQ
35fd3 55 45 52 59 5f 50 48 52 41 53 45 20 29 3b 0a 0a  UERY_PHRASE );..
35fd4 20 20 20 20 20 20 20 20 20 20 20 20 6e 50 61 72              nPar
35fd5 61 6d 31 20 3d 20 70 45 78 70 72 2d 3e 6e 4e 65  am1 = pExpr->nNe
35fd6 61 72 2b 31 3b 0a 20 20 20 20 20 20 20 20 20 20  ar+1;.          
35fd7 20 20 6e 50 61 72 61 6d 32 20 3d 20 6e 50 61 72    nParam2 = nPar
35fd8 61 6d 31 2b 70 4c 65 66 74 2d 3e 70 50 68 72 61  am1+pLeft->pPhra
35fd9 73 65 2d 3e 6e 54 6f 6b 65 6e 2b 70 52 69 67 68  se->nToken+pRigh
35fda 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b  t->pPhrase->nTok
35fdb 65 6e 2d 32 3b 0a 20 20 20 20 20 20 20 20 20 20  en-2;.          
35fdc 20 20 61 42 75 66 66 65 72 20 3d 20 73 71 6c 69    aBuffer = sqli
35fdd 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4c 65 66 74  te3_malloc(nLeft
35fde 2b 6e 52 69 67 68 74 2b 31 29 3b 0a 20 20 20 20  +nRight+1);.    
35fdf 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73          rc = fts
35fe0 33 44 6f 63 6c 69 73 74 4d 65 72 67 65 28 6d 65  3DoclistMerge(me
35fe1 72 67 65 74 79 70 65 2c 20 6e 50 61 72 61 6d 31  rgetype, nParam1
35fe2 2c 20 6e 50 61 72 61 6d 32 2c 20 61 42 75 66 66  , nParam2, aBuff
35fe3 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
35fe4 20 20 20 20 70 6e 4f 75 74 2c 20 61 4c 65 66 74      pnOut, aLeft
35fe5 2c 20 6e 4c 65 66 74 2c 20 61 52 69 67 68 74 2c  , nLeft, aRight,
35fe6 20 6e 52 69 67 68 74 0a 20 20 20 20 20 20 20 20   nRight.        
35fe7 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20      );.         
35fe8 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
35fe9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
35fea 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
35feb 65 65 28 61 42 75 66 66 65 72 29 3b 0a 20 20 20  ee(aBuffer);.   
35fec 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
35fed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 70                *p
35fee 61 4f 75 74 20 3d 20 61 42 75 66 66 65 72 3b 0a  aOut = aBuffer;.
35fef 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
35ff0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
35ff1 33 5f 66 72 65 65 28 61 4c 65 66 74 29 3b 0a 20  3_free(aLeft);. 
35ff2 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
35ff3 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20  ;.          }.. 
35ff4 20 20 20 20 20 20 20 20 20 63 61 73 65 20 46 54           case FT
35ff5 53 51 55 45 52 59 5f 4f 52 3a 20 7b 0a 20 20 20  SQUERY_OR: {.   
35ff6 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f           /* Allo
35ff7 63 61 74 65 20 61 20 62 75 66 66 65 72 20 66 6f  cate a buffer fo
35ff8 72 20 74 68 65 20 6f 75 74 70 75 74 2e 20 54 68  r the output. Th
35ff9 65 20 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 69  e maximum size i
35ffa 73 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  s the.          
35ffb 20 20 2a 2a 20 73 75 6d 20 6f 66 20 74 68 65 20    ** sum of the 
35ffc 73 69 7a 65 73 20 6f 66 20 74 68 65 20 74 77 6f  sizes of the two
35ffd 20 69 6e 70 75 74 20 62 75 66 66 65 72 73 2e 20   input buffers. 
35ffe 54 68 65 20 2b 31 20 74 65 72 6d 20 69 73 0a 20  The +1 term is. 
35fff 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 6f             ** so
36000 20 74 68 61 74 20 61 20 62 75 66 66 65 72 20 6f   that a buffer o
36001 66 20 7a 65 72 6f 20 62 79 74 65 73 20 69 73 20  f zero bytes is 
36002 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 64 20  never allocated 
36003 2d 20 74 68 69 73 20 63 61 6e 0a 20 20 20 20 20  - this can.     
36004 20 20 20 20 20 20 20 2a 2a 20 63 61 75 73 65 20         ** cause 
36005 66 74 73 33 44 6f 63 6c 69 73 74 4d 65 72 67 65  fts3DoclistMerge
36006 28 29 20 74 6f 20 69 6e 63 6f 72 72 65 63 74 6c  () to incorrectl
36007 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  y return SQLITE_
36008 4e 4f 4d 45 4d 2e 0a 20 20 20 20 20 20 20 20 20  NOMEM..         
36009 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
3600a 20 20 63 68 61 72 20 2a 61 42 75 66 66 65 72 20    char *aBuffer 
3600b 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
3600c 28 6e 52 69 67 68 74 2b 6e 4c 65 66 74 2b 31 29  (nRight+nLeft+1)
3600d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63  ;.            rc
3600e 20 3d 20 66 74 73 33 44 6f 63 6c 69 73 74 4d 65   = fts3DoclistMe
3600f 72 67 65 28 4d 45 52 47 45 5f 4f 52 2c 20 30 2c  rge(MERGE_OR, 0,
36010 20 30 2c 20 61 42 75 66 66 65 72 2c 20 70 6e 4f   0, aBuffer, pnO
36011 75 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ut,.            
36012 20 20 20 20 61 4c 65 66 74 2c 20 6e 4c 65 66 74      aLeft, nLeft
36013 2c 20 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74  , aRight, nRight
36014 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a  .            );.
36015 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 61 4f              *paO
36016 75 74 20 3d 20 61 42 75 66 66 65 72 3b 0a 20 20  ut = aBuffer;.  
36017 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
36018 33 5f 66 72 65 65 28 61 4c 65 66 74 29 3b 0a 20  3_free(aLeft);. 
36019 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
3601a 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20  ;.          }.. 
3601b 20 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74           default
3601c 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  : {.            
3601d 61 73 73 65 72 74 28 20 46 54 53 51 55 45 52 59  assert( FTSQUERY
3601e 5f 4e 4f 54 3d 3d 4d 45 52 47 45 5f 4e 4f 54 20  _NOT==MERGE_NOT 
3601f 26 26 20 46 54 53 51 55 45 52 59 5f 41 4e 44 3d  && FTSQUERY_AND=
36020 3d 4d 45 52 47 45 5f 41 4e 44 20 29 3b 0a 20 20  =MERGE_AND );.  
36021 20 20 20 20 20 20 20 20 20 20 66 74 73 33 44 6f            fts3Do
36022 63 6c 69 73 74 4d 65 72 67 65 28 70 45 78 70 72  clistMerge(pExpr
36023 2d 3e 65 54 79 70 65 2c 20 30 2c 20 30 2c 20 61  ->eType, 0, 0, a
36024 4c 65 66 74 2c 20 70 6e 4f 75 74 2c 0a 20 20 20  Left, pnOut,.   
36025 20 20 20 20 20 20 20 20 20 20 20 20 20 61 4c 65               aLe
36026 66 74 2c 20 6e 4c 65 66 74 2c 20 61 52 69 67 68  ft, nLeft, aRigh
36027 74 2c 20 6e 52 69 67 68 74 0a 20 20 20 20 20 20  t, nRight.      
36028 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
36029 20 20 20 20 20 2a 70 61 4f 75 74 20 3d 20 61 4c       *paOut = aL
3602a 65 66 74 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eft;.           
3602b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
3602c 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
3602d 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
3602e 74 65 33 5f 66 72 65 65 28 61 52 69 67 68 74 29  te3_free(aRight)
3602f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
36030 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
36031 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 78  ** This is the x
36032 46 69 6c 74 65 72 20 69 6e 74 65 72 66 61 63 65  Filter interface
36033 20 66 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c   for the virtual
36034 20 74 61 62 6c 65 2e 20 20 53 65 65 0a 2a 2a 20   table.  See.** 
36035 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
36036 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64  e xFilter method
36037 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
36038 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  or additional.**
36039 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
3603a 0a 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3d 3d 46  .** If idxNum==F
3603b 54 53 33 5f 46 55 4c 4c 53 43 41 4e 5f 53 45 41  TS3_FULLSCAN_SEA
3603c 52 43 48 20 74 68 65 6e 20 64 6f 20 61 20 66 75  RCH then do a fu
3603d 6c 6c 20 74 61 62 6c 65 20 73 63 61 6e 20 61 67  ll table scan ag
3603e 61 69 6e 73 74 0a 2a 2a 20 74 68 65 20 25 5f 63  ainst.** the %_c
3603f 6f 6e 74 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a  ontent table..**
36040 0a 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3d 3d 46  .** If idxNum==F
36041 54 53 33 5f 44 4f 43 49 44 5f 53 45 41 52 43 48  TS3_DOCID_SEARCH
36042 20 74 68 65 6e 20 64 6f 20 61 20 64 6f 63 69 64   then do a docid
36043 20 6c 6f 6f 6b 75 70 20 66 6f 72 20 61 20 73 69   lookup for a si
36044 6e 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 69 6e  ngle entry.** in
36045 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   the %_content t
36046 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  able..**.** If i
36047 64 78 4e 75 6d 3e 3d 46 54 53 33 5f 46 55 4c 4c  dxNum>=FTS3_FULL
36048 54 45 58 54 5f 53 45 41 52 43 48 20 74 68 65 6e  TEXT_SEARCH then
36049 20 75 73 65 20 74 68 65 20 66 75 6c 6c 20 74 65   use the full te
3604a 78 74 20 69 6e 64 65 78 2e 20 20 54 68 65 0a 2a  xt index.  The.*
3604b 2a 20 63 6f 6c 75 6d 6e 20 6f 6e 20 74 68 65 20  * column on the 
3604c 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  left-hand side o
3604d 66 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65 72  f the MATCH oper
3604e 61 74 6f 72 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a  ator is column.*
3604f 2a 20 6e 75 6d 62 65 72 20 69 64 78 4e 75 6d 2d  * number idxNum-
36050 46 54 53 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45  FTS3_FULLTEXT_SE
36051 41 52 43 48 2c 20 30 20 69 6e 64 65 78 65 64 2e  ARCH, 0 indexed.
36052 20 20 61 72 67 76 5b 30 5d 20 69 73 20 74 68 65    argv[0] is the
36053 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a 2a 20 73   right-hand.** s
36054 69 64 65 20 6f 66 20 74 68 65 20 4d 41 54 43 48  ide of the MATCH
36055 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 2f 2a   operator..*/./*
36056 20 54 4f 44 4f 28 73 68 65 73 73 29 20 55 70 67   TODO(shess) Upg
36057 72 61 64 65 20 74 68 65 20 63 75 72 73 6f 72 20  rade the cursor 
36058 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61  initialization a
36059 6e 64 20 64 65 73 74 72 75 63 74 69 6f 6e 20 74  nd destruction t
3605a 6f 0a 2a 2a 20 61 63 63 6f 75 6e 74 20 66 6f 72  o.** account for
3605b 20 66 74 73 33 46 69 6c 74 65 72 4d 65 74 68 6f   fts3FilterMetho
3605c 64 28 29 20 62 65 69 6e 67 20 63 61 6c 6c 65 64  d() being called
3605d 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20   multiple times 
3605e 6f 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63  on the.** same c
3605f 75 72 73 6f 72 2e 20 54 68 65 20 63 75 72 72 65  ursor. The curre
36060 6e 74 20 73 6f 6c 75 74 69 6f 6e 20 69 73 20 76  nt solution is v
36061 65 72 79 20 66 72 61 67 69 6c 65 2e 20 41 70 70  ery fragile. App
36062 6c 79 20 66 69 78 20 74 6f 0a 2a 2a 20 66 74 73  ly fix to.** fts
36063 33 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  3 as appropriate
36064 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
36065 66 74 73 33 46 69 6c 74 65 72 4d 65 74 68 6f 64  fts3FilterMethod
36066 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  (.  sqlite3_vtab
36067 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
36068 2c 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f  ,   /* The curso
36069 72 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20  r used for this 
3606a 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69  query */.  int i
3606b 64 78 4e 75 6d 2c 20 20 20 20 20 20 20 20 20 20  dxNum,          
3606c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74             /* St
3606d 72 61 74 65 67 79 20 69 6e 64 65 78 20 2a 2f 0a  rategy index */.
3606e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 64    const char *id
3606f 78 53 74 72 2c 20 20 20 20 20 20 20 20 20 20 20  xStr,           
36070 20 20 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0a 20    /* Unused */. 
36071 20 69 6e 74 20 6e 56 61 6c 2c 20 20 20 20 20 20   int nVal,      
36072 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36073 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c   /* Number of el
36074 65 6d 65 6e 74 73 20 69 6e 20 61 70 56 61 6c 20  ements in apVal 
36075 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  */.  sqlite3_val
36076 75 65 20 2a 2a 61 70 56 61 6c 20 20 20 20 20 20  ue **apVal      
36077 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
36078 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 69  s for the indexi
36079 6e 67 20 73 63 68 65 6d 65 20 2a 2f 0a 29 7b 0a  ng scheme */.){.
3607a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 7a    const char *az
3607b 53 71 6c 5b 5d 20 3d 20 7b 0a 20 20 20 20 22 53  Sql[] = {.    "S
3607c 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 25 51 2e  ELECT * FROM %Q.
3607d 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20 57 48 45  '%q_content' WHE
3607e 52 45 20 64 6f 63 69 64 20 3d 20 3f 22 2c 20 2f  RE docid = ?", /
3607f 2a 20 6e 6f 6e 2d 66 75 6c 6c 2d 74 61 62 6c 65  * non-full-table
36080 2d 73 63 61 6e 20 2a 2f 0a 20 20 20 20 22 53 45  -scan */.    "SE
36081 4c 45 43 54 20 2a 20 46 52 4f 4d 20 25 51 2e 27  LECT * FROM %Q.'
36082 25 71 5f 63 6f 6e 74 65 6e 74 27 22 2c 20 20 20  %q_content'",   
36083 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36084 20 66 75 6c 6c 2d 74 61 62 6c 65 2d 73 63 61 6e   full-table-scan
36085 20 2a 2f 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 72   */.  };.  int r
36086 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
36087 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
36088 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 63  turn code */.  c
36089 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20  har *zSql;      
3608a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3608b 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  * SQL statement 
3608c 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 25  used to access %
3608d 5f 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 46 74  _content */.  Ft
3608e 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74  s3Table *p = (Ft
3608f 73 33 54 61 62 6c 65 20 2a 29 70 43 75 72 73 6f  s3Table *)pCurso
36090 72 2d 3e 70 56 74 61 62 3b 0a 20 20 46 74 73 33  r->pVtab;.  Fts3
36091 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
36092 46 74 73 33 43 75 72 73 6f 72 20 2a 29 70 43 75  Fts3Cursor *)pCu
36093 72 73 6f 72 3b 0a 0a 20 20 55 4e 55 53 45 44 5f  rsor;..  UNUSED_
36094 50 41 52 41 4d 45 54 45 52 28 69 64 78 53 74 72  PARAMETER(idxStr
36095 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
36096 4d 45 54 45 52 28 6e 56 61 6c 29 3b 0a 0a 20 20  METER(nVal);..  
36097 61 73 73 65 72 74 28 20 69 64 78 4e 75 6d 3e 3d  assert( idxNum>=
36098 30 20 26 26 20 69 64 78 4e 75 6d 3c 3d 28 46 54  0 && idxNum<=(FT
36099 53 33 5f 46 55 4c 4c 54 45 58 54 5f 53 45 41 52  S3_FULLTEXT_SEAR
3609a 43 48 2b 70 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 29  CH+p->nColumn) )
3609b 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 56 61 6c  ;.  assert( nVal
3609c 3d 3d 30 20 7c 7c 20 6e 56 61 6c 3d 3d 31 20 29  ==0 || nVal==1 )
3609d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6e 56 61  ;.  assert( (nVa
3609e 6c 3d 3d 30 29 3d 3d 28 69 64 78 4e 75 6d 3d 3d  l==0)==(idxNum==
3609f 46 54 53 33 5f 46 55 4c 4c 53 43 41 4e 5f 53 45  FTS3_FULLSCAN_SE
360a0 41 52 43 48 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  ARCH) );..  /* I
360a1 6e 20 63 61 73 65 20 74 68 65 20 63 75 72 73 6f  n case the curso
360a2 72 20 68 61 73 20 62 65 65 6e 20 75 73 65 64 20  r has been used 
360a3 62 65 66 6f 72 65 2c 20 63 6c 65 61 72 20 69 74  before, clear it
360a4 20 6e 6f 77 2e 20 2a 2f 0a 20 20 73 71 6c 69 74   now. */.  sqlit
360a5 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 43 73 72  e3_finalize(pCsr
360a6 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69  ->pStmt);.  sqli
360a7 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61  te3_free(pCsr->a
360a8 44 6f 63 6c 69 73 74 29 3b 0a 20 20 6d 65 6d 73  Doclist);.  mems
360a9 65 74 28 26 70 43 75 72 73 6f 72 5b 31 5d 2c 20  et(&pCursor[1], 
360aa 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 43 75  0, sizeof(Fts3Cu
360ab 72 73 6f 72 29 2d 73 69 7a 65 6f 66 28 73 71 6c  rsor)-sizeof(sql
360ac 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
360ad 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 69 6c  ));..  /* Compil
360ae 65 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  e a SELECT state
360af 6d 65 6e 74 20 66 6f 72 20 74 68 69 73 20 63 75  ment for this cu
360b0 72 73 6f 72 2e 20 46 6f 72 20 61 20 66 75 6c 6c  rsor. For a full
360b1 2d 74 61 62 6c 65 2d 73 63 61 6e 2c 20 74 68 65  -table-scan, the
360b2 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  .  ** statement 
360b3 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 61 6c  loops through al
360b4 6c 20 72 6f 77 73 20 6f 66 20 74 68 65 20 25 5f  l rows of the %_
360b5 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65 2e 20 46  content table. F
360b6 6f 72 20 61 0a 20 20 2a 2a 20 66 75 6c 6c 2d 74  or a.  ** full-t
360b7 65 78 74 20 71 75 65 72 79 20 6f 72 20 64 6f 63  ext query or doc
360b8 69 64 20 6c 6f 6f 6b 75 70 2c 20 74 68 65 20 73  id lookup, the s
360b9 74 61 74 65 6d 65 6e 74 20 72 65 74 72 69 65 76  tatement retriev
360ba 65 73 20 61 20 73 69 6e 67 6c 65 0a 20 20 2a 2a  es a single.  **
360bb 20 72 6f 77 20 62 79 20 64 6f 63 69 64 2e 0a 20   row by docid.. 
360bc 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c   */.  zSql = sql
360bd 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 61 7a 53  ite3_mprintf(azS
360be 71 6c 5b 69 64 78 4e 75 6d 3d 3d 46 54 53 33 5f  ql[idxNum==FTS3_
360bf 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52 43 48 5d  FULLSCAN_SEARCH]
360c0 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61  , p->zDb, p->zNa
360c1 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a 53 71 6c  me);.  if( !zSql
360c2 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
360c3 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c  ITE_NOMEM;.  }el
360c4 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  se{.    rc = sql
360c5 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
360c6 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  p->db, zSql, -1,
360c7 20 26 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 30   &pCsr->pStmt, 0
360c8 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
360c9 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20  ree(zSql);.  }. 
360ca 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
360cb 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
360cc 20 20 70 43 73 72 2d 3e 65 53 65 61 72 63 68 20    pCsr->eSearch 
360cd 3d 20 28 69 31 36 29 69 64 78 4e 75 6d 3b 0a 0a  = (i16)idxNum;..
360ce 20 20 69 66 28 20 69 64 78 4e 75 6d 3d 3d 46 54    if( idxNum==FT
360cf 53 33 5f 44 4f 43 49 44 5f 53 45 41 52 43 48 20  S3_DOCID_SEARCH 
360d0 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
360d1 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 70  te3_bind_value(p
360d2 43 73 72 2d 3e 70 53 74 6d 74 2c 20 31 2c 20 61  Csr->pStmt, 1, a
360d3 70 56 61 6c 5b 30 5d 29 3b 0a 20 20 7d 65 6c 73  pVal[0]);.  }els
360d4 65 20 69 66 28 20 69 64 78 4e 75 6d 21 3d 46 54  e if( idxNum!=FT
360d5 53 33 5f 46 55 4c 4c 53 43 41 4e 5f 53 45 41 52  S3_FULLSCAN_SEAR
360d6 43 48 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 43  CH ){.    int iC
360d7 6f 6c 20 3d 20 69 64 78 4e 75 6d 2d 46 54 53 33  ol = idxNum-FTS3
360d8 5f 46 55 4c 4c 54 45 58 54 5f 53 45 41 52 43 48  _FULLTEXT_SEARCH
360d9 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
360da 20 2a 7a 51 75 65 72 79 20 3d 20 28 63 6f 6e 73   *zQuery = (cons
360db 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
360dc 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 56 61  _value_text(apVa
360dd 6c 5b 30 5d 29 3b 0a 0a 20 20 20 20 69 66 28 20  l[0]);..    if( 
360de 7a 51 75 65 72 79 3d 3d 30 20 26 26 20 73 71 6c  zQuery==0 && sql
360df 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
360e0 61 70 56 61 6c 5b 30 5d 29 21 3d 53 51 4c 49 54  apVal[0])!=SQLIT
360e1 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  E_NULL ){.      
360e2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
360e3 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  MEM;.    }.    r
360e4 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50  c = sqlite3Fts3P
360e5 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c 75 73 68  endingTermsFlush
360e6 28 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  (p);.    if( rc!
360e7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
360e8 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63 20  urn rc;..    rc 
360e9 3d 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70  = sqlite3Fts3Exp
360ea 72 50 61 72 73 65 28 70 2d 3e 70 54 6f 6b 65 6e  rParse(p->pToken
360eb 69 7a 65 72 2c 20 70 2d 3e 61 7a 43 6f 6c 75 6d  izer, p->azColum
360ec 6e 2c 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 0a  n, p->nColumn, .
360ed 20 20 20 20 20 20 20 20 69 43 6f 6c 2c 20 7a 51          iCol, zQ
360ee 75 65 72 79 2c 20 2d 31 2c 20 26 70 43 73 72 2d  uery, -1, &pCsr-
360ef 3e 70 45 78 70 72 0a 20 20 20 20 29 3b 0a 20 20  >pExpr.    );.  
360f0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
360f1 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
360f2 0a 0a 20 20 20 20 72 63 20 3d 20 65 76 61 6c 46  ..    rc = evalF
360f3 74 73 33 45 78 70 72 28 70 2c 20 70 43 73 72 2d  ts3Expr(p, pCsr-
360f4 3e 70 45 78 70 72 2c 20 26 70 43 73 72 2d 3e 61  >pExpr, &pCsr->a
360f5 44 6f 63 6c 69 73 74 2c 20 26 70 43 73 72 2d 3e  Doclist, &pCsr->
360f6 6e 44 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 70  nDoclist);.    p
360f7 43 73 72 2d 3e 70 4e 65 78 74 49 64 20 3d 20 70  Csr->pNextId = p
360f8 43 73 72 2d 3e 61 44 6f 63 6c 69 73 74 3b 0a 20  Csr->aDoclist;. 
360f9 20 20 20 70 43 73 72 2d 3e 69 50 72 65 76 49 64     pCsr->iPrevId
360fa 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28   = 0;.  }..  if(
360fb 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
360fc 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72 65   return rc;.  re
360fd 74 75 72 6e 20 66 74 73 33 4e 65 78 74 4d 65 74  turn fts3NextMet
360fe 68 6f 64 28 70 43 75 72 73 6f 72 29 3b 0a 7d 0a  hod(pCursor);.}.
360ff 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 69 73 20  ./* .** This is 
36100 74 68 65 20 78 45 6f 66 20 6d 65 74 68 6f 64 20  the xEof method 
36101 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
36102 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 63 61 6c  able. SQLite cal
36103 6c 73 20 74 68 69 73 20 0a 2a 2a 20 72 6f 75 74  ls this .** rout
36104 69 6e 65 20 74 6f 20 66 69 6e 64 20 6f 75 74 20  ine to find out 
36105 69 66 20 69 74 20 68 61 73 20 72 65 61 63 68 65  if it has reache
36106 64 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 72  d the end of a r
36107 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73 74  esult set..*/.st
36108 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 6f 66  atic int fts3Eof
36109 4d 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76  Method(sqlite3_v
3610a 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  tab_cursor *pCur
3610b 73 6f 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  sor){.  return (
3610c 28 46 74 73 33 43 75 72 73 6f 72 20 2a 29 70 43  (Fts3Cursor *)pC
3610d 75 72 73 6f 72 29 2d 3e 69 73 45 6f 66 3b 0a 7d  ursor)->isEof;.}
3610e 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 69 73  ../* .** This is
3610f 20 74 68 65 20 78 52 6f 77 69 64 20 6d 65 74 68   the xRowid meth
36110 6f 64 2e 20 54 68 65 20 53 51 4c 69 74 65 20 63  od. The SQLite c
36111 6f 72 65 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ore calls this r
36112 6f 75 74 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 74  outine to.** ret
36113 72 69 65 76 65 20 74 68 65 20 72 6f 77 69 64 20  rieve the rowid 
36114 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
36115 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
36116 74 20 73 65 74 2e 20 66 74 73 33 0a 2a 2a 20 65  t set. fts3.** e
36117 78 70 6f 73 65 73 20 25 5f 63 6f 6e 74 65 6e 74  xposes %_content
36118 2e 64 6f 63 69 64 20 61 73 20 74 68 65 20 72 6f  .docid as the ro
36119 77 69 64 20 66 6f 72 20 74 68 65 20 76 69 72 74  wid for the virt
3611a 75 61 6c 20 74 61 62 6c 65 2e 20 54 68 65 0a 2a  ual table. The.*
3611b 2a 20 72 6f 77 69 64 20 73 68 6f 75 6c 64 20 62  * rowid should b
3611c 65 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 52  e written to *pR
3611d 6f 77 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  owid..*/.static 
3611e 69 6e 74 20 66 74 73 33 52 6f 77 69 64 4d 65 74  int fts3RowidMet
3611f 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62  hod(sqlite3_vtab
36120 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
36121 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a  , sqlite_int64 *
36122 70 52 6f 77 69 64 29 7b 0a 20 20 46 74 73 33 43  pRowid){.  Fts3C
36123 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 46  ursor *pCsr = (F
36124 74 73 33 43 75 72 73 6f 72 20 2a 29 20 70 43 75  ts3Cursor *) pCu
36125 72 73 6f 72 3b 0a 20 20 69 66 28 20 70 43 73 72  rsor;.  if( pCsr
36126 2d 3e 61 44 6f 63 6c 69 73 74 20 29 7b 0a 20 20  ->aDoclist ){.  
36127 20 20 2a 70 52 6f 77 69 64 20 3d 20 70 43 73 72    *pRowid = pCsr
36128 2d 3e 69 50 72 65 76 49 64 3b 0a 20 20 7d 65 6c  ->iPrevId;.  }el
36129 73 65 7b 0a 20 20 20 20 2a 70 52 6f 77 69 64 20  se{.    *pRowid 
3612a 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
3612b 5f 69 6e 74 36 34 28 70 43 73 72 2d 3e 70 53 74  _int64(pCsr->pSt
3612c 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65  mt, 0);.  }.  re
3612d 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3612e 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 69  }../* .** This i
3612f 73 20 74 68 65 20 78 43 6f 6c 75 6d 6e 20 6d 65  s the xColumn me
36130 74 68 6f 64 2c 20 63 61 6c 6c 65 64 20 62 79 20  thod, called by 
36131 53 51 4c 69 74 65 20 74 6f 20 72 65 71 75 65 73  SQLite to reques
36132 74 20 61 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a  t a value from.*
36133 2a 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 74  * the row that t
36134 68 65 20 73 75 70 70 6c 69 65 64 20 63 75 72 73  he supplied curs
36135 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  or currently poi
36136 6e 74 73 20 74 6f 2e 0a 2a 2f 0a 73 74 61 74 69  nts to..*/.stati
36137 63 20 69 6e 74 20 66 74 73 33 43 6f 6c 75 6d 6e  c int fts3Column
36138 4d 65 74 68 6f 64 28 0a 20 20 73 71 6c 69 74 65  Method(.  sqlite
36139 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
3613a 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 43 75 72  Cursor,   /* Cur
3613b 73 6f 72 20 74 6f 20 72 65 74 72 69 65 76 65 20  sor to retrieve 
3613c 76 61 6c 75 65 20 66 72 6f 6d 20 2a 2f 0a 20 20  value from */.  
3613d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3613e 2a 70 43 6f 6e 74 65 78 74 2c 20 20 20 20 20 20  *pContext,      
3613f 2f 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 73  /* Context for s
36140 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 78 78  qlite3_result_xx
36141 78 28 29 20 63 61 6c 6c 73 20 2a 2f 0a 20 20 69  x() calls */.  i
36142 6e 74 20 69 43 6f 6c 20 20 20 20 20 20 20 20 20  nt iCol         
36143 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36144 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d  * Index of colum
36145 6e 20 74 6f 20 72 65 61 64 20 76 61 6c 75 65 20  n to read value 
36146 66 72 6f 6d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  from */.){.  int
36147 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
36148 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36149 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20  Return Code */. 
3614a 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73   Fts3Cursor *pCs
3614b 72 20 3d 20 28 46 74 73 33 43 75 72 73 6f 72 20  r = (Fts3Cursor 
3614c 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 46 74  *) pCursor;.  Ft
3614d 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46 74  s3Table *p = (Ft
3614e 73 33 54 61 62 6c 65 20 2a 29 70 43 75 72 73 6f  s3Table *)pCurso
3614f 72 2d 3e 70 56 74 61 62 3b 0a 0a 20 20 2f 2a 20  r->pVtab;..  /* 
36150 54 68 65 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  The column value
36151 20 73 75 70 70 6c 69 65 64 20 62 79 20 53 51 4c   supplied by SQL
36152 69 74 65 20 6d 75 73 74 20 62 65 20 69 6e 20 72  ite must be in r
36153 61 6e 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72  ange. */.  asser
36154 74 28 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43  t( iCol>=0 && iC
36155 6f 6c 3c 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31  ol<=p->nColumn+1
36156 20 29 3b 0a 0a 20 20 72 63 20 3d 20 66 74 73 33   );..  rc = fts3
36157 43 75 72 73 6f 72 53 65 65 6b 28 70 43 73 72 29  CursorSeek(pCsr)
36158 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
36159 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
3615a 20 69 43 6f 6c 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d   iCol==p->nColum
3615b 6e 2b 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  n+1 ){.      /* 
3615c 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20 72  This call is a r
3615d 65 71 75 65 73 74 20 66 6f 72 20 74 68 65 20 22  equest for the "
3615e 64 6f 63 69 64 22 20 63 6f 6c 75 6d 6e 2e 20 53  docid" column. S
3615f 69 6e 63 65 20 22 64 6f 63 69 64 22 20 69 73 20  ince "docid" is 
36160 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20 61 6c 69  an .      ** ali
36161 61 73 20 66 6f 72 20 22 72 6f 77 69 64 22 2c 20  as for "rowid", 
36162 75 73 65 20 74 68 65 20 78 52 6f 77 69 64 28 29  use the xRowid()
36163 20 6d 65 74 68 6f 64 20 74 6f 20 6f 62 74 61 69   method to obtai
36164 6e 20 74 68 65 20 76 61 6c 75 65 2e 0a 20 20 20  n the value..   
36165 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
36166 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64  te3_int64 iRowid
36167 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73  ;.      rc = fts
36168 33 52 6f 77 69 64 4d 65 74 68 6f 64 28 70 43 75  3RowidMethod(pCu
36169 72 73 6f 72 2c 20 26 69 52 6f 77 69 64 29 3b 0a  rsor, &iRowid);.
3616a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
3616b 73 75 6c 74 5f 69 6e 74 36 34 28 70 43 6f 6e 74  sult_int64(pCont
3616c 65 78 74 2c 20 69 52 6f 77 69 64 29 3b 0a 20 20  ext, iRowid);.  
3616d 20 20 7d 65 6c 73 65 20 69 66 28 20 69 43 6f 6c    }else if( iCol
3616e 3d 3d 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a  ==p->nColumn ){.
3616f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 74        /* The ext
36170 72 61 20 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20  ra column whose 
36171 6e 61 6d 65 20 69 73 20 74 68 65 20 73 61 6d 65  name is the same
36172 20 61 73 20 74 68 65 20 74 61 62 6c 65 2e 0a 20   as the table.. 
36173 20 20 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 61       ** Return a
36174 20 62 6c 6f 62 20 77 68 69 63 68 20 69 73 20 61   blob which is a
36175 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
36176 63 75 72 73 6f 72 2e 0a 20 20 20 20 20 20 2a 2f  cursor..      */
36177 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
36178 65 73 75 6c 74 5f 62 6c 6f 62 28 70 43 6f 6e 74  esult_blob(pCont
36179 65 78 74 2c 20 26 70 43 73 72 2c 20 73 69 7a 65  ext, &pCsr, size
3617a 6f 66 28 70 43 73 72 29 2c 20 53 51 4c 49 54 45  of(pCsr), SQLITE
3617b 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
3617c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
3617d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
3617e 75 65 28 70 43 6f 6e 74 65 78 74 2c 20 73 71 6c  ue(pContext, sql
3617f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75  ite3_column_valu
36180 65 28 70 43 73 72 2d 3e 70 53 74 6d 74 2c 20 69  e(pCsr->pStmt, i
36181 43 6f 6c 2b 31 29 29 3b 0a 20 20 20 20 7d 0a 20  Col+1));.    }. 
36182 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
36183 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66  }../* .** This f
36184 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 20 69  unction is the i
36185 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
36186 20 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c   the xUpdate cal
36187 6c 62 61 63 6b 20 75 73 65 64 20 62 79 20 0a 2a  lback used by .*
36188 2a 20 46 54 53 33 20 76 69 72 74 75 61 6c 20 74  * FTS3 virtual t
36189 61 62 6c 65 73 2e 20 49 74 20 69 73 20 69 6e 76  ables. It is inv
3618a 6f 6b 65 64 20 62 79 20 53 51 4c 69 74 65 20 65  oked by SQLite e
3618b 61 63 68 20 74 69 6d 65 20 61 20 72 6f 77 20 69  ach time a row i
3618c 73 20 74 6f 20 62 65 0a 2a 2a 20 69 6e 73 65 72  s to be.** inser
3618d 74 65 64 2c 20 75 70 64 61 74 65 64 20 6f 72 20  ted, updated or 
3618e 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74 61 74  deleted..*/.stat
3618f 69 63 20 69 6e 74 20 66 74 73 33 55 70 64 61 74  ic int fts3Updat
36190 65 4d 65 74 68 6f 64 28 0a 20 20 73 71 6c 69 74  eMethod(.  sqlit
36191 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20  e3_vtab *pVtab, 
36192 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69             /* Vi
36193 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64  rtual table hand
36194 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67  le */.  int nArg
36195 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
36196 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
36197 6f 66 20 61 72 67 75 6d 65 6e 74 20 61 72 72 61  of argument arra
36198 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  y */.  sqlite3_v
36199 61 6c 75 65 20 2a 2a 61 70 56 61 6c 2c 20 20 20  alue **apVal,   
3619a 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
3619b 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  of arguments */.
3619c 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a    sqlite_int64 *
3619d 70 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20  pRowid          
3619e 20 20 2f 2a 20 4f 55 54 3a 20 54 68 65 20 61 66    /* OUT: The af
3619f 66 65 63 74 65 64 20 28 6f 72 20 65 66 66 65 63  fected (or effec
361a0 74 65 64 29 20 72 6f 77 69 64 20 2a 2f 0a 29 7b  ted) rowid */.){
361a1 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
361a2 33 46 74 73 33 55 70 64 61 74 65 4d 65 74 68 6f  3Fts3UpdateMetho
361a3 64 28 70 56 74 61 62 2c 20 6e 41 72 67 2c 20 61  d(pVtab, nArg, a
361a4 70 56 61 6c 2c 20 70 52 6f 77 69 64 29 3b 0a 7d  pVal, pRowid);.}
361a5 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
361a6 74 61 74 69 6f 6e 20 6f 66 20 78 53 79 6e 63 28  tation of xSync(
361a7 29 20 6d 65 74 68 6f 64 2e 20 46 6c 75 73 68 20  ) method. Flush 
361a8 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
361a9 74 68 65 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d  the pending-term
361aa 73 0a 2a 2a 20 68 61 73 68 2d 74 61 62 6c 65 20  s.** hash-table 
361ab 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
361ac 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
361ad 74 73 33 53 79 6e 63 4d 65 74 68 6f 64 28 73 71  ts3SyncMethod(sq
361ae 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
361af 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  b){.  return sql
361b0 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e 67 54  ite3Fts3PendingT
361b1 65 72 6d 73 46 6c 75 73 68 28 28 46 74 73 33 54  ermsFlush((Fts3T
361b2 61 62 6c 65 20 2a 29 70 56 74 61 62 29 3b 0a 7d  able *)pVtab);.}
361b3 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
361b4 74 61 74 69 6f 6e 20 6f 66 20 78 42 65 67 69 6e  tation of xBegin
361b5 28 29 20 6d 65 74 68 6f 64 2e 20 54 68 69 73 20  () method. This 
361b6 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73  is a no-op..*/.s
361b7 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 42 65  tatic int fts3Be
361b8 67 69 6e 4d 65 74 68 6f 64 28 73 71 6c 69 74 65  ginMethod(sqlite
361b9 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a  3_vtab *pVtab){.
361ba 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
361bb 45 52 28 70 56 74 61 62 29 3b 0a 20 20 61 73 73  ER(pVtab);.  ass
361bc 65 72 74 28 20 28 28 46 74 73 33 54 61 62 6c 65  ert( ((Fts3Table
361bd 20 2a 29 70 56 74 61 62 29 2d 3e 6e 50 65 6e 64   *)pVtab)->nPend
361be 69 6e 67 44 61 74 61 3d 3d 30 20 29 3b 0a 20 20  ingData==0 );.  
361bf 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
361c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
361c1 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78 43 6f  mentation of xCo
361c2 6d 6d 69 74 28 29 20 6d 65 74 68 6f 64 2e 20 54  mmit() method. T
361c3 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  his is a no-op. 
361c4 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 0a  The contents of.
361c5 2a 2a 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74  ** the pending-t
361c6 65 72 6d 73 20 68 61 73 68 2d 74 61 62 6c 65 20  erms hash-table 
361c7 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
361c8 6e 20 66 6c 75 73 68 65 64 20 69 6e 74 6f 20 74  n flushed into t
361c9 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 62  he database.** b
361ca 79 20 66 74 73 33 53 79 6e 63 4d 65 74 68 6f 64  y fts3SyncMethod
361cb 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ()..*/.static in
361cc 74 20 66 74 73 33 43 6f 6d 6d 69 74 4d 65 74 68  t fts3CommitMeth
361cd 6f 64 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  od(sqlite3_vtab 
361ce 2a 70 56 74 61 62 29 7b 0a 20 20 55 4e 55 53 45  *pVtab){.  UNUSE
361cf 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 74 61  D_PARAMETER(pVta
361d0 62 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 28  b);.  assert( ((
361d1 46 74 73 33 54 61 62 6c 65 20 2a 29 70 56 74 61  Fts3Table *)pVta
361d2 62 29 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61  b)->nPendingData
361d3 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ==0 );.  return 
361d4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
361d5 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
361d6 6f 6e 20 6f 66 20 78 52 6f 6c 6c 62 61 63 6b 28  on of xRollback(
361d7 29 2e 20 44 69 73 63 61 72 64 20 74 68 65 20 63  ). Discard the c
361d8 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70  ontents of the p
361d9 65 6e 64 69 6e 67 2d 74 65 72 6d 73 0a 2a 2a 20  ending-terms.** 
361da 68 61 73 68 2d 74 61 62 6c 65 2e 20 41 6e 79 20  hash-table. Any 
361db 63 68 61 6e 67 65 73 20 6d 61 64 65 20 74 6f 20  changes made to 
361dc 74 68 65 20 64 61 74 61 62 61 73 65 20 61 72 65  the database are
361dd 20 72 65 76 65 72 74 65 64 20 62 79 20 53 51 4c   reverted by SQL
361de 69 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ite..*/.static i
361df 6e 74 20 66 74 73 33 52 6f 6c 6c 62 61 63 6b 4d  nt fts3RollbackM
361e0 65 74 68 6f 64 28 73 71 6c 69 74 65 33 5f 76 74  ethod(sqlite3_vt
361e1 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 73 71  ab *pVtab){.  sq
361e2 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e 67  lite3Fts3Pending
361e3 54 65 72 6d 73 43 6c 65 61 72 28 28 46 74 73 33  TermsClear((Fts3
361e4 54 61 62 6c 65 20 2a 29 70 56 74 61 62 29 3b 0a  Table *)pVtab);.
361e5 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
361e6 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c  OK;.}../*.** Hel
361e7 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  per function use
361e8 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65  d by the impleme
361e9 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6f  ntation of the o
361ea 76 65 72 6c 6f 61 64 65 64 20 73 6e 69 70 70 65  verloaded snippe
361eb 74 28 29 2c 0a 2a 2a 20 6f 66 66 73 65 74 73 28  t(),.** offsets(
361ec 29 20 61 6e 64 20 6f 70 74 69 6d 69 7a 65 28 29  ) and optimize()
361ed 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a   SQL functions..
361ee 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c  **.** If the val
361ef 75 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ue passed as the
361f0 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20   third argument 
361f1 69 73 20 61 20 62 6c 6f 62 20 6f 66 20 73 69 7a  is a blob of siz
361f2 65 0a 2a 2a 20 73 69 7a 65 6f 66 28 46 74 73 33  e.** sizeof(Fts3
361f3 43 75 72 73 6f 72 2a 29 2c 20 74 68 65 6e 20 74  Cursor*), then t
361f4 68 65 20 62 6c 6f 62 20 63 6f 6e 74 65 6e 74 73  he blob contents
361f5 20 61 72 65 20 63 6f 70 69 65 64 20 74 6f 20 74   are copied to t
361f6 68 65 20 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61  he .** output va
361f7 72 69 61 62 6c 65 20 2a 70 70 43 73 72 20 61 6e  riable *ppCsr an
361f8 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  d SQLITE_OK is r
361f9 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69  eturned. Otherwi
361fa 73 65 2c 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20  se, an error.** 
361fb 6d 65 73 73 61 67 65 20 69 73 20 77 72 69 74 74  message is writt
361fc 65 6e 20 74 6f 20 63 6f 6e 74 65 78 74 20 70 43  en to context pC
361fd 6f 6e 74 65 78 74 20 61 6e 64 20 53 51 4c 49 54  ontext and SQLIT
361fe 45 5f 45 52 52 4f 52 20 72 65 74 75 72 6e 65 64  E_ERROR returned
361ff 2e 20 54 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20  . The.** string 
36200 70 61 73 73 65 64 20 76 69 61 20 7a 46 75 6e 63  passed via zFunc
36201 20 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74   is used as part
36202 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 6d 65   of the error me
36203 73 73 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  ssage..*/.static
36204 20 69 6e 74 20 66 74 73 33 46 75 6e 63 74 69 6f   int fts3Functio
36205 6e 41 72 67 28 0a 20 20 73 71 6c 69 74 65 33 5f  nArg(.  sqlite3_
36206 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78  context *pContex
36207 74 2c 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 66  t,      /* SQL f
36208 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e  unction call con
36209 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  text */.  const 
3620a 63 68 61 72 20 2a 7a 46 75 6e 63 2c 20 20 20 20  char *zFunc,    
3620b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6e            /* Fun
3620c 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  ction name */.  
3620d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70  sqlite3_value *p
3620e 56 61 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  Val,            
3620f 2f 2a 20 61 72 67 76 5b 30 5d 20 70 61 73 73 65  /* argv[0] passe
36210 64 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  d to function */
36211 0a 20 20 46 74 73 33 43 75 72 73 6f 72 20 2a 2a  .  Fts3Cursor **
36212 70 70 43 73 72 20 20 20 20 20 20 20 20 20 2f 2a  ppCsr         /*
36213 20 4f 55 54 3a 20 53 74 6f 72 65 20 63 75 72 73   OUT: Store curs
36214 6f 72 20 68 61 6e 64 6c 65 20 68 65 72 65 20 2a  or handle here *
36215 2f 0a 29 7b 0a 20 20 46 74 73 33 43 75 72 73 6f  /.){.  Fts3Curso
36216 72 20 2a 70 52 65 74 3b 0a 20 20 69 66 28 20 73  r *pRet;.  if( s
36217 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
36218 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f  e(pVal)!=SQLITE_
36219 42 4c 4f 42 20 0a 20 20 20 7c 7c 20 73 71 6c 69  BLOB .   || sqli
3621a 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
3621b 70 56 61 6c 29 21 3d 73 69 7a 65 6f 66 28 46 74  pVal)!=sizeof(Ft
3621c 73 33 43 75 72 73 6f 72 20 2a 29 0a 20 20 29 7b  s3Cursor *).  ){
3621d 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20  .    char *zErr 
3621e 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
3621f 66 28 22 69 6c 6c 65 67 61 6c 20 66 69 72 73 74  f("illegal first
36220 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 25 73 22   argument to %s"
36221 2c 20 7a 46 75 6e 63 29 3b 0a 20 20 20 20 73 71  , zFunc);.    sq
36222 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
36223 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 7a 45 72  or(pContext, zEr
36224 72 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69  r, -1);.    sqli
36225 74 65 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a  te3_free(zErr);.
36226 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
36227 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 6d  E_ERROR;.  }.  m
36228 65 6d 63 70 79 28 26 70 52 65 74 2c 20 73 71 6c  emcpy(&pRet, sql
36229 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
3622a 70 56 61 6c 29 2c 20 73 69 7a 65 6f 66 28 46 74  pVal), sizeof(Ft
3622b 73 33 43 75 72 73 6f 72 20 2a 29 29 3b 0a 20 20  s3Cursor *));.  
3622c 2a 70 70 43 73 72 20 3d 20 70 52 65 74 3b 0a 20  *ppCsr = pRet;. 
3622d 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3622e 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  K;.}../*.** Impl
3622f 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
36230 65 20 73 6e 69 70 70 65 74 28 29 20 66 75 6e 63  e snippet() func
36231 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a 2a 2f  tion for FTS3.*/
36232 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
36233 33 53 6e 69 70 70 65 74 46 75 6e 63 28 0a 20 20  3SnippetFunc(.  
36234 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
36235 2a 70 43 6f 6e 74 65 78 74 2c 20 20 20 20 20 20  *pContext,      
36236 2f 2a 20 53 51 4c 69 74 65 20 66 75 6e 63 74 69  /* SQLite functi
36237 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78 74 20  on call context 
36238 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 20  */.  int nVal,  
36239 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3623a 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
3623b 61 70 56 61 6c 5b 5d 20 61 72 72 61 79 20 2a 2f  apVal[] array */
3623c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
3623d 20 2a 2a 61 70 56 61 6c 20 20 20 20 20 20 20 20   **apVal        
3623e 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 61     /* Array of a
3623f 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b 0a 20  rguments */.){. 
36240 20 46 74 73 33 43 75 72 73 6f 72 20 2a 70 43 73   Fts3Cursor *pCs
36241 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
36242 20 2f 2a 20 43 75 72 73 6f 72 20 68 61 6e 64 6c   /* Cursor handl
36243 65 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68  e passed through
36244 20 61 70 56 61 6c 5b 30 5d 20 2a 2f 0a 20 20 63   apVal[0] */.  c
36245 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72  onst char *zStar
36246 74 20 3d 20 22 3c 62 3e 22 3b 0a 20 20 63 6f 6e  t = "<b>";.  con
36247 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20  st char *zEnd = 
36248 22 3c 2f 62 3e 22 3b 0a 20 20 63 6f 6e 73 74 20  "</b>";.  const 
36249 63 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 20  char *zEllipsis 
3624a 3d 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b 0a  = "<b>...</b>";.
3624b 0a 20 20 2f 2a 20 54 68 65 72 65 20 6d 75 73 74  .  /* There must
3624c 20 62 65 20 61 74 20 6c 65 61 73 74 20 6f 6e 65   be at least one
3624d 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
3624e 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
3624f 6e 20 28 6f 74 68 65 72 77 69 73 65 0a 20 20 2a  n (otherwise.  *
36250 2a 20 74 68 65 20 6e 6f 6e 2d 6f 76 65 72 6c 6f  * the non-overlo
36251 61 64 65 64 20 76 65 72 73 69 6f 6e 20 77 6f 75  aded version wou
36252 6c 64 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c  ld have been cal
36253 6c 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74  led instead of t
36254 68 69 73 20 6f 6e 65 29 2e 0a 20 20 2a 2f 0a 20  his one)..  */. 
36255 20 61 73 73 65 72 74 28 20 6e 56 61 6c 3e 3d 31   assert( nVal>=1
36256 20 29 3b 0a 0a 20 20 69 66 28 20 6e 56 61 6c 3e   );..  if( nVal>
36257 34 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  4 ){.    sqlite3
36258 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70 43  _result_error(pC
36259 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20  ontext, .       
3625a 20 22 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f   "wrong number o
3625b 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 66  f arguments to f
3625c 75 6e 63 74 69 6f 6e 20 73 6e 69 70 70 65 74 28  unction snippet(
3625d 29 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74  )", -1);.    ret
3625e 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66  urn;.  }.  if( f
3625f 74 73 33 46 75 6e 63 74 69 6f 6e 41 72 67 28 70  ts3FunctionArg(p
36260 43 6f 6e 74 65 78 74 2c 20 22 73 6e 69 70 70 65  Context, "snippe
36261 74 22 2c 20 61 70 56 61 6c 5b 30 5d 2c 20 26 70  t", apVal[0], &p
36262 43 73 72 29 20 29 20 72 65 74 75 72 6e 3b 0a 0a  Csr) ) return;..
36263 20 20 73 77 69 74 63 68 28 20 6e 56 61 6c 20 29    switch( nVal )
36264 7b 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7a 45  {.    case 4: zE
36265 6c 6c 69 70 73 69 73 20 3d 20 28 63 6f 6e 73 74  llipsis = (const
36266 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76   char*)sqlite3_v
36267 61 6c 75 65 5f 74 65 78 74 28 61 70 56 61 6c 5b  alue_text(apVal[
36268 33 5d 29 3b 0a 20 20 20 20 63 61 73 65 20 33 3a  3]);.    case 3:
36269 20 7a 45 6e 64 20 3d 20 28 63 6f 6e 73 74 20 63   zEnd = (const c
3626a 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
3626b 75 65 5f 74 65 78 74 28 61 70 56 61 6c 5b 32 5d  ue_text(apVal[2]
3626c 29 3b 0a 20 20 20 20 63 61 73 65 20 32 3a 20 7a  );.    case 2: z
3626d 53 74 61 72 74 20 3d 20 28 63 6f 6e 73 74 20 63  Start = (const c
3626e 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
3626f 75 65 5f 74 65 78 74 28 61 70 56 61 6c 5b 31 5d  ue_text(apVal[1]
36270 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  );.  }..  sqlite
36271 33 46 74 73 33 53 6e 69 70 70 65 74 28 70 43 6f  3Fts3Snippet(pCo
36272 6e 74 65 78 74 2c 20 70 43 73 72 2c 20 7a 53 74  ntext, pCsr, zSt
36273 61 72 74 2c 20 7a 45 6e 64 2c 20 7a 45 6c 6c 69  art, zEnd, zElli
36274 70 73 69 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  psis);.}../*.** 
36275 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
36276 66 20 74 68 65 20 6f 66 66 73 65 74 73 28 29 20  f the offsets() 
36277 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54 53  function for FTS
36278 33 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  3.*/.static void
36279 20 66 74 73 33 4f 66 66 73 65 74 73 46 75 6e 63   fts3OffsetsFunc
3627a 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
3627b 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20 20  ext *pContext,  
3627c 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 66 75      /* SQLite fu
3627d 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74  nction call cont
3627e 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61  ext */.  int nVa
3627f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l,              
36280 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
36281 20 6f 66 20 61 72 67 75 6d 65 6e 74 20 61 72 72   of argument arr
36282 61 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ay */.  sqlite3_
36283 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 20 20 20  value **apVal   
36284 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79          /* Array
36285 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f   of arguments */
36286 0a 29 7b 0a 20 20 46 74 73 33 43 75 72 73 6f 72  .){.  Fts3Cursor
36287 20 2a 70 43 73 72 3b 20 20 20 20 20 20 20 20 20   *pCsr;         
36288 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
36289 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74 68  handle passed th
3628a 72 6f 75 67 68 20 61 70 56 61 6c 5b 30 5d 20 2a  rough apVal[0] *
3628b 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  /..  UNUSED_PARA
3628c 4d 45 54 45 52 28 6e 56 61 6c 29 3b 0a 0a 20 20  METER(nVal);..  
3628d 61 73 73 65 72 74 28 20 6e 56 61 6c 3d 3d 31 20  assert( nVal==1 
3628e 29 3b 0a 20 20 69 66 28 20 66 74 73 33 46 75 6e  );.  if( fts3Fun
3628f 63 74 69 6f 6e 41 72 67 28 70 43 6f 6e 74 65 78  ctionArg(pContex
36290 74 2c 20 22 6f 66 66 73 65 74 73 22 2c 20 61 70  t, "offsets", ap
36291 56 61 6c 5b 30 5d 2c 20 26 70 43 73 72 29 20 29  Val[0], &pCsr) )
36292 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
36293 74 28 20 70 43 73 72 20 29 3b 0a 20 20 73 71 6c  t( pCsr );.  sql
36294 69 74 65 33 46 74 73 33 4f 66 66 73 65 74 73 28  ite3Fts3Offsets(
36295 70 43 6f 6e 74 65 78 74 2c 20 70 43 73 72 29 3b  pContext, pCsr);
36296 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 49 6d 70 6c 65  .}../* .** Imple
36297 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
36298 20 73 70 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a   special optimiz
36299 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  e() function for
3629a 20 46 54 53 33 2e 20 54 68 69 73 20 0a 2a 2a 20   FTS3. This .** 
3629b 66 75 6e 63 74 69 6f 6e 20 6d 65 72 67 65 73 20  function merges 
3629c 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 69 6e 20  all segments in 
3629d 74 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  the database to 
3629e 61 20 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e 74  a single segment
3629f 2e 0a 2a 2a 20 45 78 61 6d 70 6c 65 20 75 73 61  ..** Example usa
362a0 67 65 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53  ge is:.**.**   S
362a1 45 4c 45 43 54 20 6f 70 74 69 6d 69 7a 65 28 74  ELECT optimize(t
362a2 29 20 46 52 4f 4d 20 74 20 4c 49 4d 49 54 20 31  ) FROM t LIMIT 1
362a3 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 27 74  ;.**.** where 't
362a4 27 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  ' is the name of
362a5 20 61 6e 20 46 54 53 33 20 74 61 62 6c 65 2e 0a   an FTS3 table..
362a6 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
362a7 74 73 33 4f 70 74 69 6d 69 7a 65 46 75 6e 63 28  ts3OptimizeFunc(
362a8 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
362a9 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 20 20 20  xt *pContext,   
362aa 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 66 75 6e     /* SQLite fun
362ab 63 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65  ction call conte
362ac 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 6c  xt */.  int nVal
362ad 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
362ae 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
362af 6f 66 20 61 72 67 75 6d 65 6e 74 20 61 72 72 61  of argument arra
362b0 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  y */.  sqlite3_v
362b1 61 6c 75 65 20 2a 2a 61 70 56 61 6c 20 20 20 20  alue **apVal    
362b2 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
362b3 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a  of arguments */.
362b4 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  ){.  int rc;    
362b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362b6 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63       /* Return c
362b7 6f 64 65 20 2a 2f 0a 20 20 46 74 73 33 54 61 62  ode */.  Fts3Tab
362b8 6c 65 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20  le *p;          
362b9 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
362ba 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
362bb 20 2a 2f 0a 20 20 46 74 73 33 43 75 72 73 6f 72   */.  Fts3Cursor
362bc 20 2a 70 43 75 72 73 6f 72 3b 20 20 20 20 20 20   *pCursor;      
362bd 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
362be 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74 68  handle passed th
362bf 72 6f 75 67 68 20 61 70 56 61 6c 5b 30 5d 20 2a  rough apVal[0] *
362c0 2f 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  /..  UNUSED_PARA
362c1 4d 45 54 45 52 28 6e 56 61 6c 29 3b 0a 0a 20 20  METER(nVal);..  
362c2 61 73 73 65 72 74 28 20 6e 56 61 6c 3d 3d 31 20  assert( nVal==1 
362c3 29 3b 0a 20 20 69 66 28 20 66 74 73 33 46 75 6e  );.  if( fts3Fun
362c4 63 74 69 6f 6e 41 72 67 28 70 43 6f 6e 74 65 78  ctionArg(pContex
362c5 74 2c 20 22 6f 70 74 69 6d 69 7a 65 22 2c 20 61  t, "optimize", a
362c6 70 56 61 6c 5b 30 5d 2c 20 26 70 43 75 72 73 6f  pVal[0], &pCurso
362c7 72 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  r) ) return;.  p
362c8 20 3d 20 28 46 74 73 33 54 61 62 6c 65 20 2a 29   = (Fts3Table *)
362c9 70 43 75 72 73 6f 72 2d 3e 62 61 73 65 2e 70 56  pCursor->base.pV
362ca 74 61 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70  tab;.  assert( p
362cb 20 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69   );..  rc = sqli
362cc 74 65 33 46 74 73 33 4f 70 74 69 6d 69 7a 65 28  te3Fts3Optimize(
362cd 70 29 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 72  p);..  switch( r
362ce 63 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  c ){.    case SQ
362cf 4c 49 54 45 5f 4f 4b 3a 0a 20 20 20 20 20 20 73  LITE_OK:.      s
362d0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
362d1 78 74 28 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e  xt(pContext, "In
362d2 64 65 78 20 6f 70 74 69 6d 69 7a 65 64 22 2c 20  dex optimized", 
362d3 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  -1, SQLITE_STATI
362d4 43 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  C);.      break;
362d5 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
362d6 5f 44 4f 4e 45 3a 0a 20 20 20 20 20 20 73 71 6c  _DONE:.      sql
362d7 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
362d8 28 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65  (pContext, "Inde
362d9 78 20 61 6c 72 65 61 64 79 20 6f 70 74 69 6d 61  x already optima
362da 6c 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53  l", -1, SQLITE_S
362db 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 62 72  TATIC);.      br
362dc 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
362dd 3a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  :.      sqlite3_
362de 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64  result_error_cod
362df 65 28 70 43 6f 6e 74 65 78 74 2c 20 72 63 29 3b  e(pContext, rc);
362e0 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
362e1 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
362e2 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e  routine implemen
362e3 74 73 20 74 68 65 20 78 46 69 6e 64 46 75 6e 63  ts the xFindFunc
362e4 74 69 6f 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20  tion method for 
362e5 74 68 65 20 46 54 53 33 0a 2a 2a 20 76 69 72 74  the FTS3.** virt
362e6 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ual table..*/.st
362e7 61 74 69 63 20 69 6e 74 20 66 74 73 33 46 69 6e  atic int fts3Fin
362e8 64 46 75 6e 63 74 69 6f 6e 4d 65 74 68 6f 64 28  dFunctionMethod(
362e9 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
362ea 2a 70 56 74 61 62 2c 20 20 20 20 20 20 20 20 20  *pVtab,         
362eb 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
362ec 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ble handle */.  
362ed 69 6e 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20  int nArg,       
362ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362ef 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 53 51 4c  /* Number of SQL
362f0 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
362f1 6e 74 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  nts */.  const c
362f2 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20  har *zName,     
362f3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
362f4 20 6f 66 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e   of SQL function
362f5 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 2a 70 78   */.  void (**px
362f6 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f  Func)(sqlite3_co
362f7 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
362f8 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 20 2f 2a 20  e3_value**), /* 
362f9 4f 55 54 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 20  OUT: Result */. 
362fa 20 76 6f 69 64 20 2a 2a 70 70 41 72 67 20 20 20   void **ppArg   
362fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362fc 20 2f 2a 20 55 6e 75 73 65 64 20 2a 2f 0a 29 7b   /* Unused */.){
362fd 0a 20 20 73 74 72 75 63 74 20 4f 76 65 72 6c 6f  .  struct Overlo
362fe 61 64 65 64 20 7b 0a 20 20 20 20 63 6f 6e 73 74  aded {.    const
362ff 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20   char *zName;.  
36300 20 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28    void (*xFunc)(
36301 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
36302 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
36303 75 65 2a 2a 29 3b 0a 20 20 7d 20 61 4f 76 65 72  ue**);.  } aOver
36304 6c 6f 61 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b  load[] = {.    {
36305 20 22 73 6e 69 70 70 65 74 22 2c 20 66 74 73 33   "snippet", fts3
36306 53 6e 69 70 70 65 74 46 75 6e 63 20 7d 2c 0a 20  SnippetFunc },. 
36307 20 20 20 7b 20 22 6f 66 66 73 65 74 73 22 2c 20     { "offsets", 
36308 66 74 73 33 4f 66 66 73 65 74 73 46 75 6e 63 20  fts3OffsetsFunc 
36309 7d 2c 0a 20 20 20 20 7b 20 22 6f 70 74 69 6d 69  },.    { "optimi
3630a 7a 65 22 2c 20 66 74 73 33 4f 70 74 69 6d 69 7a  ze", fts3Optimiz
3630b 65 46 75 6e 63 20 7d 2c 0a 20 20 7d 3b 0a 20 20  eFunc },.  };.  
3630c 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
3630d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3630e 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
3630f 61 62 6c 65 20 2a 2f 0a 0a 20 20 55 4e 55 53 45  able */..  UNUSE
36310 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 74 61  D_PARAMETER(pVta
36311 62 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  b);.  UNUSED_PAR
36312 41 4d 45 54 45 52 28 6e 41 72 67 29 3b 0a 20 20  AMETER(nArg);.  
36313 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
36314 28 70 70 41 72 67 29 3b 0a 0a 20 20 66 6f 72 28  (ppArg);..  for(
36315 69 3d 30 3b 20 69 3c 53 69 7a 65 6f 66 41 72 72  i=0; i<SizeofArr
36316 61 79 28 61 4f 76 65 72 6c 6f 61 64 29 3b 20 69  ay(aOverload); i
36317 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 74 72  ++){.    if( str
36318 63 6d 70 28 7a 4e 61 6d 65 2c 20 61 4f 76 65 72  cmp(zName, aOver
36319 6c 6f 61 64 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d  load[i].zName)==
3631a 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 78 46 75  0 ){.      *pxFu
3631b 6e 63 20 3d 20 61 4f 76 65 72 6c 6f 61 64 5b 69  nc = aOverload[i
3631c 5d 2e 78 46 75 6e 63 3b 0a 20 20 20 20 20 20 72  ].xFunc;.      r
3631d 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
3631e 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 66 75 6e 63   }..  /* No func
3631f 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 70 65 63  tion of the spec
36320 69 66 69 65 64 20 6e 61 6d 65 20 77 61 73 20 66  ified name was f
36321 6f 75 6e 64 2e 20 52 65 74 75 72 6e 20 30 2e 20  ound. Return 0. 
36322 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  */.  return 0;.}
36323 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
36324 74 61 74 69 6f 6e 20 6f 66 20 46 54 53 33 20 78  tation of FTS3 x
36325 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 20 52  Rename method. R
36326 65 6e 61 6d 65 20 61 6e 20 66 74 73 33 20 74 61  ename an fts3 ta
36327 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
36328 6e 74 20 66 74 73 33 52 65 6e 61 6d 65 4d 65 74  nt fts3RenameMet
36329 68 6f 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  hod(.  sqlite3_v
3632a 74 61 62 20 2a 70 56 74 61 62 2c 20 20 20 20 20  tab *pVtab,     
3632b 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
3632c 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
3632d 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
3632e 7a 4e 61 6d 65 20 20 20 20 20 20 20 20 20 20 20  zName           
3632f 20 20 20 20 2f 2a 20 4e 65 77 20 6e 61 6d 65 20      /* New name 
36330 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  of table */.){. 
36331 20 46 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20   Fts3Table *p = 
36332 28 46 74 73 33 54 61 62 6c 65 20 2a 29 70 56 74  (Fts3Table *)pVt
36333 61 62 3b 20 20 20 20 20 0a 20 20 69 6e 74 20 72  ab;     .  int r
36334 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
36335 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  ;          /* Re
36336 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 63  turn Code */.  c
36337 68 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20  har *zSql;      
36338 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36339 2a 20 53 51 4c 20 73 63 72 69 70 74 20 74 6f 20  * SQL script to 
3633a 72 75 6e 20 74 6f 20 72 65 6e 61 6d 65 20 74 61  run to rename ta
3633b 62 6c 65 73 20 2a 2f 0a 20 0a 20 20 7a 53 71 6c  bles */. .  zSql
3633c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
3633d 74 66 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54  tf(.    "ALTER T
3633e 41 42 4c 45 20 25 51 2e 27 25 71 5f 63 6f 6e 74  ABLE %Q.'%q_cont
3633f 65 6e 74 27 20 20 52 45 4e 41 4d 45 20 54 4f 20  ent'  RENAME TO 
36340 27 25 71 5f 63 6f 6e 74 65 6e 74 27 3b 22 0a 20  '%q_content';". 
36341 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20     "ALTER TABLE 
36342 25 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73 27  %Q.'%q_segments'
36343 20 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f 73   RENAME TO '%q_s
36344 65 67 6d 65 6e 74 73 27 3b 22 0a 20 20 20 20 22  egments';".    "
36345 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27  ALTER TABLE %Q.'
36346 25 71 5f 73 65 67 64 69 72 27 20 20 20 52 45 4e  %q_segdir'   REN
36347 41 4d 45 20 54 4f 20 27 25 71 5f 73 65 67 64 69  AME TO '%q_segdi
36348 72 27 3b 22 0a 20 20 20 20 2c 20 70 2d 3e 7a 44  r';".    , p->zD
36349 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61  b, p->zName, zNa
3634a 6d 65 20 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62  me .    , p->zDb
3634b 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  , p->zName, zNam
3634c 65 20 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c  e .    , p->zDb,
3634d 20 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65   p->zName, zName
3634e 0a 20 20 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c  .  );.  if( zSql
3634f 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
36350 69 74 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c  ite3_exec(p->db,
36351 20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b   zSql, 0, 0, 0);
36352 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
36353 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 72  e(zSql);.  }.  r
36354 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61  eturn rc;.}..sta
36355 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  tic const sqlite
36356 33 5f 6d 6f 64 75 6c 65 20 66 74 73 33 4d 6f 64  3_module fts3Mod
36357 75 6c 65 20 3d 20 7b 0a 20 20 2f 2a 20 69 56 65  ule = {.  /* iVe
36358 72 73 69 6f 6e 20 20 20 20 20 20 2a 2f 20 30 2c  rsion      */ 0,
36359 0a 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20 20  .  /* xCreate   
3635a 20 20 20 20 2a 2f 20 66 74 73 33 43 72 65 61 74      */ fts3Creat
3635b 65 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 43  eMethod,.  /* xC
3635c 6f 6e 6e 65 63 74 20 20 20 20 20 20 2a 2f 20 66  onnect      */ f
3635d 74 73 33 43 6f 6e 6e 65 63 74 4d 65 74 68 6f 64  ts3ConnectMethod
3635e 2c 0a 20 20 2f 2a 20 78 42 65 73 74 49 6e 64 65  ,.  /* xBestInde
3635f 78 20 20 20 20 2a 2f 20 66 74 73 33 42 65 73 74  x    */ fts3Best
36360 49 6e 64 65 78 4d 65 74 68 6f 64 2c 0a 20 20 2f  IndexMethod,.  /
36361 2a 20 78 44 69 73 63 6f 6e 6e 65 63 74 20 20 20  * xDisconnect   
36362 2a 2f 20 66 74 73 33 44 69 73 63 6f 6e 6e 65 63  */ fts3Disconnec
36363 74 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 44  tMethod,.  /* xD
36364 65 73 74 72 6f 79 20 20 20 20 20 20 2a 2f 20 66  estroy      */ f
36365 74 73 33 44 65 73 74 72 6f 79 4d 65 74 68 6f 64  ts3DestroyMethod
36366 2c 0a 20 20 2f 2a 20 78 4f 70 65 6e 20 20 20 20  ,.  /* xOpen    
36367 20 20 20 20 20 2a 2f 20 66 74 73 33 4f 70 65 6e       */ fts3Open
36368 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 43 6c  Method,.  /* xCl
36369 6f 73 65 20 20 20 20 20 20 20 20 2a 2f 20 66 75  ose        */ fu
3636a 6c 6c 74 65 78 74 43 6c 6f 73 65 2c 0a 20 20 2f  lltextClose,.  /
3636b 2a 20 78 46 69 6c 74 65 72 20 20 20 20 20 20 20  * xFilter       
3636c 2a 2f 20 66 74 73 33 46 69 6c 74 65 72 4d 65 74  */ fts3FilterMet
3636d 68 6f 64 2c 0a 20 20 2f 2a 20 78 4e 65 78 74 20  hod,.  /* xNext 
3636e 20 20 20 20 20 20 20 20 2a 2f 20 66 74 73 33 4e          */ fts3N
3636f 65 78 74 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20  extMethod,.  /* 
36370 78 45 6f 66 20 20 20 20 20 20 20 20 20 20 2a 2f  xEof          */
36371 20 66 74 73 33 45 6f 66 4d 65 74 68 6f 64 2c 0a   fts3EofMethod,.
36372 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20 20 20 20    /* xColumn    
36373 20 20 20 2a 2f 20 66 74 73 33 43 6f 6c 75 6d 6e     */ fts3Column
36374 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 52 6f  Method,.  /* xRo
36375 77 69 64 20 20 20 20 20 20 20 20 2a 2f 20 66 74  wid        */ ft
36376 73 33 52 6f 77 69 64 4d 65 74 68 6f 64 2c 0a 20  s3RowidMethod,. 
36377 20 2f 2a 20 78 55 70 64 61 74 65 20 20 20 20 20   /* xUpdate     
36378 20 20 2a 2f 20 66 74 73 33 55 70 64 61 74 65 4d    */ fts3UpdateM
36379 65 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 42 65 67  ethod,.  /* xBeg
3637a 69 6e 20 20 20 20 20 20 20 20 2a 2f 20 66 74 73  in        */ fts
3637b 33 42 65 67 69 6e 4d 65 74 68 6f 64 2c 0a 20 20  3BeginMethod,.  
3637c 2f 2a 20 78 53 79 6e 63 20 20 20 20 20 20 20 20  /* xSync        
3637d 20 2a 2f 20 66 74 73 33 53 79 6e 63 4d 65 74 68   */ fts3SyncMeth
3637e 6f 64 2c 0a 20 20 2f 2a 20 78 43 6f 6d 6d 69 74  od,.  /* xCommit
3637f 20 20 20 20 20 20 20 2a 2f 20 66 74 73 33 43 6f         */ fts3Co
36380 6d 6d 69 74 4d 65 74 68 6f 64 2c 0a 20 20 2f 2a  mmitMethod,.  /*
36381 20 78 52 6f 6c 6c 62 61 63 6b 20 20 20 20 20 2a   xRollback     *
36382 2f 20 66 74 73 33 52 6f 6c 6c 62 61 63 6b 4d 65  / fts3RollbackMe
36383 74 68 6f 64 2c 0a 20 20 2f 2a 20 78 46 69 6e 64  thod,.  /* xFind
36384 46 75 6e 63 74 69 6f 6e 20 2a 2f 20 66 74 73 33  Function */ fts3
36385 46 69 6e 64 46 75 6e 63 74 69 6f 6e 4d 65 74 68  FindFunctionMeth
36386 6f 64 2c 0a 20 20 2f 2a 20 78 52 65 6e 61 6d 65  od,.  /* xRename
36387 20 2a 2f 20 20 20 20 20 20 20 66 74 73 33 52 65   */       fts3Re
36388 6e 61 6d 65 4d 65 74 68 6f 64 2c 0a 7d 3b 0a 0a  nameMethod,.};..
36389 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
3638a 69 6f 6e 20 69 73 20 72 65 67 69 73 74 65 72 65  ion is registere
3638b 64 20 61 73 20 74 68 65 20 6d 6f 64 75 6c 65 20  d as the module 
3638c 64 65 73 74 72 75 63 74 6f 72 20 28 63 61 6c 6c  destructor (call
3638d 65 64 20 77 68 65 6e 20 61 6e 0a 2a 2a 20 46 54  ed when an.** FT
3638e 53 33 20 65 6e 61 62 6c 65 64 20 64 61 74 61 62  S3 enabled datab
3638f 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
36390 73 20 63 6c 6f 73 65 64 29 2e 20 49 74 20 66 72  s closed). It fr
36391 65 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a  ees the memory.*
36392 2a 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  * allocated for 
36393 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 68 61  the tokenizer ha
36394 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  sh table..*/.sta
36395 74 69 63 20 76 6f 69 64 20 68 61 73 68 44 65 73  tic void hashDes
36396 74 72 6f 79 28 76 6f 69 64 20 2a 70 29 7b 0a 20  troy(void *p){. 
36397 20 46 74 73 33 48 61 73 68 20 2a 70 48 61 73 68   Fts3Hash *pHash
36398 20 3d 20 28 46 74 73 33 48 61 73 68 20 2a 29 70   = (Fts3Hash *)p
36399 3b 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 48  ;.  sqlite3Fts3H
3639a 61 73 68 43 6c 65 61 72 28 70 48 61 73 68 29 3b  ashClear(pHash);
3639b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3639c 70 48 61 73 68 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pHash);.}../*.**
3639d 20 54 68 65 20 66 74 73 33 20 62 75 69 6c 74 2d   The fts3 built-
3639e 69 6e 20 74 6f 6b 65 6e 69 7a 65 72 73 20 2d 20  in tokenizers - 
3639f 22 73 69 6d 70 6c 65 22 20 61 6e 64 20 22 70 6f  "simple" and "po
363a0 72 74 65 72 22 20 2d 20 61 72 65 20 69 6d 70 6c  rter" - are impl
363a1 65 6d 65 6e 74 65 64 0a 2a 2a 20 69 6e 20 66 69  emented.** in fi
363a2 6c 65 73 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  les fts3_tokeniz
363a3 65 72 31 2e 63 20 61 6e 64 20 66 74 73 33 5f 70  er1.c and fts3_p
363a4 6f 72 74 65 72 2e 63 20 72 65 73 70 65 63 74 69  orter.c respecti
363a5 76 65 6c 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  vely. The follow
363a6 69 6e 67 0a 2a 2a 20 74 77 6f 20 66 6f 72 77 61  ing.** two forwa
363a7 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20  rd declarations 
363a8 61 72 65 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e  are for function
363a9 73 20 64 65 63 6c 61 72 65 64 20 69 6e 20 74 68  s declared in th
363aa 65 73 65 20 66 69 6c 65 73 0a 2a 2a 20 75 73 65  ese files.** use
363ab 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68  d to retrieve th
363ac 65 20 72 65 73 70 65 63 74 69 76 65 20 69 6d 70  e respective imp
363ad 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a  lementations..**
363ae 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 73 71 6c 69  .** Calling sqli
363af 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b  te3Fts3SimpleTok
363b0 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 29 20 73  enizerModule() s
363b1 65 74 73 20 74 68 65 20 76 61 6c 75 65 20 70 6f  ets the value po
363b2 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 74  inted.** to by t
363b3 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 70  he argument to p
363b4 6f 69 6e 74 20 61 20 74 68 65 20 22 73 69 6d 70  oint a the "simp
363b5 6c 65 22 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6d  le" tokenizer im
363b6 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a  plementation..**
363b7 20 46 75 6e 63 74 69 6f 6e 20 2e 2e 2e 50 6f 72   Function ...Por
363b8 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  terTokenizerModu
363b9 6c 65 28 29 20 73 65 74 73 20 2a 70 4d 6f 64 75  le() sets *pModu
363ba 6c 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  le to point to t
363bb 68 65 0a 2a 2a 20 70 6f 72 74 65 72 20 74 6f 6b  he.** porter tok
363bc 65 6e 69 7a 65 72 2f 73 74 65 6d 6d 65 72 20 69  enizer/stemmer i
363bd 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a  mplementation..*
363be 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
363bf 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
363c0 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72  3SimpleTokenizer
363c1 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74  Module(sqlite3_t
363c2 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
363c3 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29  const**ppModule)
363c4 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
363c5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
363c6 33 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72  3PorterTokenizer
363c7 4d 6f 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74  Module(sqlite3_t
363c8 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
363c9 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29  const**ppModule)
363ca 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
363cb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
363cc 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  3IcuTokenizerMod
363cd 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ule(sqlite3_toke
363ce 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
363cf 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a  st**ppModule);..
363d0 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 73 65  /*.** Initialise
363d1 20 74 68 65 20 66 74 73 33 20 65 78 74 65 6e 73   the fts3 extens
363d2 69 6f 6e 2e 20 49 66 20 74 68 69 73 20 65 78 74  ion. If this ext
363d3 65 6e 73 69 6f 6e 20 69 73 20 62 75 69 6c 74 20  ension is built 
363d4 61 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68  as part.** of th
363d5 65 20 73 71 6c 69 74 65 20 6c 69 62 72 61 72 79  e sqlite library
363d6 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  , then this func
363d7 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64  tion is called d
363d8 69 72 65 63 74 6c 79 20 62 79 0a 2a 2a 20 53 51  irectly by.** SQ
363d9 4c 69 74 65 2e 20 49 66 20 66 74 73 33 20 69 73  Lite. If fts3 is
363da 20 62 75 69 6c 74 20 61 73 20 61 20 64 79 6e 61   built as a dyna
363db 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 61 62 6c 65  mically loadable
363dc 20 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69 73   extension, this
363dd 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  .** function is 
363de 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 73 71  called by the sq
363df 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f  lite3_extension_
363e0 69 6e 69 74 28 29 20 65 6e 74 72 79 20 70 6f 69  init() entry poi
363e1 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
363e2 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
363e3 33 46 74 73 33 49 6e 69 74 28 73 71 6c 69 74 65  3Fts3Init(sqlite
363e4 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63  3 *db){.  int rc
363e5 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
363e6 46 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 20  Fts3Hash *pHash 
363e7 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  = 0;.  const sql
363e8 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
363e9 6f 64 75 6c 65 20 2a 70 53 69 6d 70 6c 65 20 3d  odule *pSimple =
363ea 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   0;.  const sqli
363eb 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
363ec 64 75 6c 65 20 2a 70 50 6f 72 74 65 72 20 3d 20  dule *pPorter = 
363ed 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  0;.  const sqlit
363ee 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
363ef 75 6c 65 20 2a 70 49 63 75 20 3d 20 30 3b 0a 0a  ule *pIcu = 0;..
363f0 20 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d    sqlite3Fts3Sim
363f1 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  pleTokenizerModu
363f2 6c 65 28 26 70 53 69 6d 70 6c 65 29 3b 0a 20 20  le(&pSimple);.  
363f3 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65  sqlite3Fts3Porte
363f4 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  rTokenizerModule
363f5 28 26 70 50 6f 72 74 65 72 29 3b 0a 23 69 66 64  (&pPorter);.#ifd
363f6 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
363f7 5f 49 43 55 0a 20 20 73 71 6c 69 74 65 33 46 74  _ICU.  sqlite3Ft
363f8 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f  s3IcuTokenizerMo
363f9 64 75 6c 65 28 26 70 49 63 75 29 3b 0a 23 65 6e  dule(&pIcu);.#en
363fa 64 69 66 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  dif..  /* Alloca
363fb 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73  te and initialis
363fc 65 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  e the hash-table
363fd 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
363fe 6f 6b 65 6e 69 7a 65 72 73 2e 20 2a 2f 0a 20 20  okenizers. */.  
363ff 70 48 61 73 68 20 3d 20 73 71 6c 69 74 65 33 5f  pHash = sqlite3_
36400 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74  malloc(sizeof(Ft
36401 73 33 48 61 73 68 29 29 3b 0a 20 20 69 66 28 20  s3Hash));.  if( 
36402 21 70 48 61 73 68 20 29 7b 0a 20 20 20 20 72 63  !pHash ){.    rc
36403 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
36404 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
36405 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69  lite3Fts3HashIni
36406 74 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48 41  t(pHash, FTS3_HA
36407 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20  SH_STRING, 1);. 
36408 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68   }..  /* Load th
36409 65 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65 6e  e built-in token
3640a 69 7a 65 72 73 20 69 6e 74 6f 20 74 68 65 20 68  izers into the h
3640b 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  ash table */.  i
3640c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3640d 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
3640e 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72  te3Fts3HashInser
3640f 74 28 70 48 61 73 68 2c 20 22 73 69 6d 70 6c 65  t(pHash, "simple
36410 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70 53  ", 7, (void *)pS
36411 69 6d 70 6c 65 29 0a 20 20 20 20 20 7c 7c 20 73  imple).     || s
36412 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e  qlite3Fts3HashIn
36413 73 65 72 74 28 70 48 61 73 68 2c 20 22 70 6f 72  sert(pHash, "por
36414 74 65 72 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a  ter", 7, (void *
36415 29 70 50 6f 72 74 65 72 29 20 0a 20 20 20 20 20  )pPorter) .     
36416 7c 7c 20 28 70 49 63 75 20 26 26 20 73 71 6c 69  || (pIcu && sqli
36417 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72  te3Fts3HashInser
36418 74 28 70 48 61 73 68 2c 20 22 69 63 75 22 2c 20  t(pHash, "icu", 
36419 34 2c 20 28 76 6f 69 64 20 2a 29 70 49 63 75 29  4, (void *)pIcu)
3641a 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  ).    ){.      r
3641b 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
3641c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66  ;.    }.  }..#if
3641d 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
3641e 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70    sqlite3Fts3Exp
3641f 72 49 6e 69 74 54 65 73 74 49 6e 74 65 72 66 61  rInitTestInterfa
36420 63 65 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a 0a  ce(db);.#endif..
36421 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20    /* Create the 
36422 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 77 72  virtual table wr
36423 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65  apper around the
36424 20 68 61 73 68 2d 74 61 62 6c 65 20 61 6e 64 20   hash-table and 
36425 6f 76 65 72 6c 6f 61 64 20 0a 20 20 2a 2a 20 74  overload .  ** t
36426 68 65 20 74 77 6f 20 73 63 61 6c 61 72 20 66 75  he two scalar fu
36427 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 69 73  nctions. If this
36428 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
36429 72 65 67 69 73 74 65 72 20 74 68 65 0a 20 20 2a  register the.  *
3642a 2a 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 73 71  * module with sq
3642b 6c 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  lite..  */.  if(
3642c 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 0a   SQLITE_OK==rc .
3642d 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
3642e 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74  =(rc = sqlite3Ft
3642f 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28  s3InitHashTable(
36430 64 62 2c 20 70 48 61 73 68 2c 20 22 66 74 73 33  db, pHash, "fts3
36431 5f 74 6f 6b 65 6e 69 7a 65 72 22 29 29 0a 20 20  _tokenizer")).  
36432 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28   && SQLITE_OK==(
36433 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65  rc = sqlite3_ove
36434 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64  rload_function(d
36435 62 2c 20 22 73 6e 69 70 70 65 74 22 2c 20 2d 31  b, "snippet", -1
36436 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f  )).   && SQLITE_
36437 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK==(rc = sqlite
36438 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
36439 69 6f 6e 28 64 62 2c 20 22 6f 66 66 73 65 74 73  ion(db, "offsets
3643a 22 2c 20 31 29 29 0a 20 20 20 26 26 20 53 51 4c  ", 1)).   && SQL
3643b 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71  ITE_OK==(rc = sq
3643c 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66  lite3_overload_f
3643d 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 6f 70 74  unction(db, "opt
3643e 69 6d 69 7a 65 22 2c 20 31 29 29 0a 20 20 29 7b  imize", 1)).  ){
3643f 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
36440 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c  te3_create_modul
36441 65 5f 76 32 28 0a 20 20 20 20 20 20 20 20 64 62  e_v2(.        db
36442 2c 20 22 66 74 73 33 22 2c 20 26 66 74 73 33 4d  , "fts3", &fts3M
36443 6f 64 75 6c 65 2c 20 28 76 6f 69 64 20 2a 29 70  odule, (void *)p
36444 48 61 73 68 2c 20 68 61 73 68 44 65 73 74 72 6f  Hash, hashDestro
36445 79 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20  y.    );.  }..  
36446 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20  /* An error has 
36447 6f 63 63 75 72 72 65 64 2e 20 44 65 6c 65 74 65  occurred. Delete
36448 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
36449 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65  and return the e
3644a 72 72 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20  rror code. */.  
3644b 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49  assert( rc!=SQLI
3644c 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70  TE_OK );.  if( p
3644d 48 61 73 68 20 29 7b 0a 20 20 20 20 73 71 6c 69  Hash ){.    sqli
3644e 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61 72  te3Fts3HashClear
3644f 28 70 48 61 73 68 29 3b 0a 20 20 20 20 73 71 6c  (pHash);.    sql
36450 69 74 65 33 5f 66 72 65 65 28 70 48 61 73 68 29  ite3_free(pHash)
36451 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
36452 63 3b 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54  c;.}..#if !SQLIT
36453 45 5f 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50  E_CORE.SQLITE_AP
36454 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78  I int sqlite3_ex
36455 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20  tension_init(.  
36456 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20  sqlite3 *db, .  
36457 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c  char **pzErrMsg,
36458 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
36459 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70  _api_routines *p
3645a 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f  Api.){.  SQLITE_
3645b 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28  EXTENSION_INIT2(
3645c 70 41 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73  pApi).  return s
3645d 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28 64  qlite3Fts3Init(d
3645e 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65  b);.}.#endif..#e
3645f 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
36460 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
36461 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  3.c ************
36462 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36463 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36464 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
36465 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
36466 20 66 74 73 33 5f 65 78 70 72 2e 63 20 2a 2a 2a   fts3_expr.c ***
36467 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36468 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36469 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
3646a 20 4e 6f 76 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68   Nov 28.**.** Th
3646b 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
3646c 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
3646d 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
3646e 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
3646f 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
36470 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
36471 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
36472 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
36473 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
36474 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
36475 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
36476 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
36477 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
36478 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
36479 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
3647a 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
3647b 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
3647c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3647d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3647e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3647f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
36481 2a 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 63 6f  * This module co
36482 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61 74  ntains code that
36483 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 70 61   implements a pa
36484 72 73 65 72 20 66 6f 72 20 66 74 73 33 20 71 75  rser for fts3 qu
36485 65 72 79 20 73 74 72 69 6e 67 73 0a 2a 2a 20 28  ery strings.** (
36486 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 61  the right-hand a
36487 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 4d  rgument to the M
36488 41 54 43 48 20 6f 70 65 72 61 74 6f 72 29 2e 20  ATCH operator). 
36489 42 65 63 61 75 73 65 20 74 68 65 20 73 75 70 70  Because the supp
3648a 6f 72 74 65 64 20 0a 2a 2a 20 73 79 6e 74 61 78  orted .** syntax
3648b 20 69 73 20 72 65 6c 61 74 69 76 65 6c 79 20 73   is relatively s
3648c 69 6d 70 6c 65 2c 20 74 68 65 20 77 68 6f 6c 65  imple, the whole
3648d 20 74 6f 6b 65 6e 69 7a 65 72 2f 70 61 72 73 65   tokenizer/parse
3648e 72 20 73 79 73 74 65 6d 20 69 73 0a 2a 2a 20 68  r system is.** h
3648f 61 6e 64 2d 63 6f 64 65 64 2e 20 0a 2a 2f 0a 23  and-coded. .*/.#
36490 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
36491 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
36492 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
36493 45 5f 46 54 53 33 29 0a 0a 2f 2a 0a 2a 2a 20 42  E_FTS3)../*.** B
36494 79 20 64 65 66 61 75 6c 74 2c 20 74 68 69 73 20  y default, this 
36495 6d 6f 64 75 6c 65 20 70 61 72 73 65 73 20 74 68  module parses th
36496 65 20 6c 65 67 61 63 79 20 73 79 6e 74 61 78 20  e legacy syntax 
36497 74 68 61 74 20 68 61 73 20 62 65 65 6e 20 0a 2a  that has been .*
36498 2a 20 74 72 61 64 69 74 69 6f 6e 61 6c 6c 79 20  * traditionally 
36499 75 73 65 64 20 62 79 20 66 74 73 33 2e 20 4f 72  used by fts3. Or
3649a 2c 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  , if SQLITE_ENAB
3649b 4c 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45  LE_FTS3_PARENTHE
3649c 53 49 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65  SIS.** is define
3649d 64 2c 20 74 68 65 6e 20 69 74 20 75 73 65 73 20  d, then it uses 
3649e 74 68 65 20 6e 65 77 20 73 79 6e 74 61 78 2e 20  the new syntax. 
3649f 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20  The differences 
364a0 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 6e  between.** the n
364a1 65 77 20 61 6e 64 20 74 68 65 20 6f 6c 64 20 73  ew and the old s
364a2 79 6e 74 61 78 65 73 20 61 72 65 3a 0a 2a 2a 0a  yntaxes are:.**.
364a3 2a 2a 20 20 61 29 20 54 68 65 20 6e 65 77 20 73  **  a) The new s
364a4 79 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 70  yntax supports p
364a5 61 72 65 6e 74 68 65 73 69 73 2e 20 54 68 65 20  arenthesis. The 
364a6 6f 6c 64 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a  old does not..**
364a7 0a 2a 2a 20 20 62 29 20 54 68 65 20 6e 65 77 20  .**  b) The new 
364a8 73 79 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20  syntax supports 
364a9 74 68 65 20 41 4e 44 20 61 6e 64 20 4e 4f 54 20  the AND and NOT 
364aa 6f 70 65 72 61 74 6f 72 73 2e 20 54 68 65 20 6f  operators. The o
364ab 6c 64 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a  ld does not..**.
364ac 2a 2a 20 20 63 29 20 54 68 65 20 6f 6c 64 20 73  **  c) The old s
364ad 79 6e 74 61 78 20 73 75 70 70 6f 72 74 73 20 74  yntax supports t
364ae 68 65 20 22 2d 22 20 74 6f 6b 65 6e 20 71 75 61  he "-" token qua
364af 6c 69 66 69 65 72 2e 20 54 68 69 73 20 69 73 20  lifier. This is 
364b0 6e 6f 74 20 0a 2a 2a 20 20 20 20 20 73 75 70 70  not .**     supp
364b1 6f 72 74 65 64 20 62 79 20 74 68 65 20 6e 65 77  orted by the new
364b2 20 73 79 6e 74 61 78 20 28 69 74 20 69 73 20 72   syntax (it is r
364b3 65 70 6c 61 63 65 64 20 62 79 20 74 68 65 20 4e  eplaced by the N
364b4 4f 54 20 6f 70 65 72 61 74 6f 72 29 2e 0a 2a 2a  OT operator)..**
364b5 0a 2a 2a 20 20 64 29 20 57 68 65 6e 20 75 73 69  .**  d) When usi
364b6 6e 67 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61  ng the old synta
364b7 78 2c 20 74 68 65 20 4f 52 20 6f 70 65 72 61 74  x, the OR operat
364b8 6f 72 20 68 61 73 20 61 20 67 72 65 61 74 65 72  or has a greater
364b9 20 70 72 65 63 65 64 65 6e 63 65 0a 2a 2a 20 20   precedence.**  
364ba 20 20 20 74 68 61 6e 20 61 6e 20 69 6d 70 6c 69     than an impli
364bb 63 69 74 20 41 4e 44 2e 20 57 68 65 6e 20 75 73  cit AND. When us
364bc 69 6e 67 20 74 68 65 20 6e 65 77 2c 20 62 6f 74  ing the new, bot
364bd 68 20 69 6d 70 6c 69 63 69 74 79 20 61 6e 64 20  h implicity and 
364be 65 78 70 6c 69 63 69 74 0a 2a 2a 20 20 20 20 20  explicit.**     
364bf 41 4e 44 20 6f 70 65 72 61 74 6f 72 73 20 68 61  AND operators ha
364c0 76 65 20 61 20 68 69 67 68 65 72 20 70 72 65 63  ve a higher prec
364c1 65 64 65 6e 63 65 20 74 68 61 6e 20 4f 52 2e 0a  edence than OR..
364c2 2a 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65  **.** If compile
364c3 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 54 45  d with SQLITE_TE
364c4 53 54 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e  ST defined, then
364c5 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 65 78 70   this module exp
364c6 6f 72 74 73 20 74 68 65 0a 2a 2a 20 73 79 6d 62  orts the.** symb
364c7 6f 6c 20 22 69 6e 74 20 73 71 6c 69 74 65 33 5f  ol "int sqlite3_
364c8 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65  fts3_enable_pare
364c9 6e 74 68 65 73 65 73 22 2e 20 53 65 74 74 69 6e  ntheses". Settin
364ca 67 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 0a  g this variable.
364cb 2a 2a 20 74 6f 20 7a 65 72 6f 20 63 61 75 73 65  ** to zero cause
364cc 73 20 74 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20  s the module to 
364cd 75 73 65 20 74 68 65 20 6f 6c 64 20 73 79 6e 74  use the old synt
364ce 61 78 2e 20 49 66 20 69 74 20 69 73 20 73 65 74  ax. If it is set
364cf 20 74 6f 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f   to .** non-zero
364d0 20 74 68 65 20 6e 65 77 20 73 79 6e 74 61 78 20   the new syntax 
364d1 69 73 20 61 63 74 69 76 61 74 65 64 2e 20 54 68  is activated. Th
364d2 69 73 20 69 73 20 73 6f 20 62 6f 74 68 20 73 79  is is so both sy
364d3 6e 74 61 78 65 73 20 63 61 6e 0a 2a 2a 20 62 65  ntaxes can.** be
364d4 20 74 65 73 74 65 64 20 75 73 69 6e 67 20 61 20   tested using a 
364d5 73 69 6e 67 6c 65 20 62 75 69 6c 64 20 6f 66 20  single build of 
364d6 74 65 73 74 66 69 78 74 75 72 65 2e 0a 2a 2a 0a  testfixture..**.
364d7 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
364d8 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 73   describes the s
364d9 79 6e 74 61 78 20 73 75 70 70 6f 72 74 65 64 20  yntax supported 
364da 62 79 20 74 68 65 20 66 74 73 33 20 4d 41 54 43  by the fts3 MATC
364db 48 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 69 6e  H.** operator in
364dc 20 61 20 73 69 6d 69 6c 61 72 20 66 6f 72 6d 61   a similar forma
364dd 74 20 74 6f 20 74 68 61 74 20 75 73 65 64 20 62  t to that used b
364de 79 20 74 68 65 20 6c 65 6d 6f 6e 20 70 61 72 73  y the lemon pars
364df 65 72 0a 2a 2a 20 67 65 6e 65 72 61 74 6f 72 2e  er.** generator.
364e0 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 64 6f 65   This module doe
364e1 73 20 6e 6f 74 20 75 73 65 20 61 63 74 75 61 6c  s not use actual
364e2 6c 79 20 6c 65 6d 6f 6e 2c 20 69 74 20 75 73 65  ly lemon, it use
364e3 73 20 61 0a 2a 2a 20 63 75 73 74 6f 6d 20 70 61  s a.** custom pa
364e4 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 71 75  rser..**.**   qu
364e5 65 72 79 20 3a 3a 3d 20 61 6e 64 65 78 70 72 20  ery ::= andexpr 
364e6 28 4f 52 20 61 6e 64 65 78 70 72 29 2a 2e 0a 2a  (OR andexpr)*..*
364e7 2a 0a 2a 2a 20 20 20 61 6e 64 65 78 70 72 20 3a  *.**   andexpr :
364e8 3a 3d 20 6e 6f 74 65 78 70 72 20 28 41 4e 44 3f  := notexpr (AND?
364e9 20 6e 6f 74 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a   notexpr)*..**.*
364ea 2a 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20  *   notexpr ::= 
364eb 6e 65 61 72 65 78 70 72 20 28 4e 4f 54 20 6e 65  nearexpr (NOT ne
364ec 61 72 65 78 70 72 7c 2d 54 4f 4b 45 4e 29 2a 2e  arexpr|-TOKEN)*.
364ed 0a 2a 2a 20 20 20 6e 6f 74 65 78 70 72 20 3a 3a  .**   notexpr ::
364ee 3d 20 4c 50 20 71 75 65 72 79 20 52 50 2e 0a 2a  = LP query RP..*
364ef 2a 0a 2a 2a 20 20 20 6e 65 61 72 65 78 70 72 20  *.**   nearexpr 
364f0 3a 3a 3d 20 70 68 72 61 73 65 20 28 4e 45 41 52  ::= phrase (NEAR
364f1 20 64 69 73 74 61 6e 63 65 5f 6f 70 74 20 6e 65   distance_opt ne
364f2 61 72 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20  arexpr)*..**.** 
364f3 20 20 64 69 73 74 61 6e 63 65 5f 6f 70 74 20 3a    distance_opt :
364f4 3a 3d 20 2e 0a 2a 2a 20 20 20 64 69 73 74 61 6e  := ..**   distan
364f5 63 65 5f 6f 70 74 20 3a 3a 3d 20 2f 20 49 4e 54  ce_opt ::= / INT
364f6 45 47 45 52 2e 0a 2a 2a 0a 2a 2a 20 20 20 70 68  EGER..**.**   ph
364f7 72 61 73 65 20 3a 3a 3d 20 54 4f 4b 45 4e 2e 0a  rase ::= TOKEN..
364f8 2a 2a 20 20 20 70 68 72 61 73 65 20 3a 3a 3d 20  **   phrase ::= 
364f9 43 4f 4c 55 4d 4e 3a 54 4f 4b 45 4e 2e 0a 2a 2a  COLUMN:TOKEN..**
364fa 20 20 20 70 68 72 61 73 65 20 3a 3a 3d 20 22 54     phrase ::= "T
364fb 4f 4b 45 4e 20 54 4f 4b 45 4e 20 54 4f 4b 45 4e  OKEN TOKEN TOKEN
364fc 2e 2e 2e 22 2e 0a 2a 2f 0a 0a 23 69 66 64 65 66  ..."..*/..#ifdef
364fd 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
364fe 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
364ff 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
36500 70 61 72 65 6e 74 68 65 73 65 73 20 3d 20 30 3b  parentheses = 0;
36501 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 53  .#else.# ifdef S
36502 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
36503 33 5f 50 41 52 45 4e 54 48 45 53 49 53 20 0a 23  3_PARENTHESIS .#
36504 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
36505 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72  _fts3_enable_par
36506 65 6e 74 68 65 73 65 73 20 31 0a 23 20 65 6c 73  entheses 1.# els
36507 65 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69  e.#  define sqli
36508 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
36509 70 61 72 65 6e 74 68 65 73 65 73 20 30 0a 23 20  parentheses 0.# 
3650a 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a  endif.#endif../*
3650b 0a 2a 2a 20 44 65 66 61 75 6c 74 20 73 70 61 6e  .** Default span
3650c 20 66 6f 72 20 4e 45 41 52 20 6f 70 65 72 61 74   for NEAR operat
3650d 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ors..*/.#define 
3650e 53 51 4c 49 54 45 5f 46 54 53 33 5f 44 45 46 41  SQLITE_FTS3_DEFA
3650f 55 4c 54 5f 4e 45 41 52 5f 50 41 52 41 4d 20 31  ULT_NEAR_PARAM 1
36510 30 0a 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75  0...typedef stru
36511 63 74 20 50 61 72 73 65 43 6f 6e 74 65 78 74 20  ct ParseContext 
36512 50 61 72 73 65 43 6f 6e 74 65 78 74 3b 0a 73 74  ParseContext;.st
36513 72 75 63 74 20 50 61 72 73 65 43 6f 6e 74 65 78  ruct ParseContex
36514 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  t {.  sqlite3_to
36515 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
36516 7a 65 72 3b 20 20 20 20 20 20 2f 2a 20 54 6f 6b  zer;      /* Tok
36517 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f  enizer module */
36518 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
36519 61 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20  azCol;          
3651a 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
3651b 6f 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  of column names 
3651c 66 6f 72 20 66 74 73 33 20 74 61 62 6c 65 20 2a  for fts3 table *
3651d 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20  /.  int nCol;   
3651e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3651f 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
36520 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
36521 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74  azCol[] */.  int
36522 20 69 44 65 66 61 75 6c 74 43 6f 6c 3b 20 20 20   iDefaultCol;   
36523 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36524 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 6f 6c 75   /* Default colu
36525 6d 6e 20 74 6f 20 71 75 65 72 79 20 2a 2f 0a 20  mn to query */. 
36526 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
36527 20 2a 70 43 74 78 3b 20 20 20 20 20 20 20 20 20   *pCtx;         
36528 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72       /* Write er
36529 72 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 65  ror message here
3652a 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 73 74 3b   */.  int nNest;
3652b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3652c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
3652d 62 65 72 20 6f 66 20 6e 65 73 74 65 64 20 62 72  ber of nested br
3652e 61 63 6b 65 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ackets */.};../*
3652f 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
36530 6e 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20  n is equivalent 
36531 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  to the standard 
36532 69 73 73 70 61 63 65 28 29 20 66 75 6e 63 74 69  isspace() functi
36533 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  on. .**.** The s
36534 74 61 6e 64 61 72 64 20 69 73 73 70 61 63 65 28  tandard isspace(
36535 29 20 63 61 6e 20 62 65 20 61 77 6b 77 61 72 64  ) can be awkward
36536 20 74 6f 20 75 73 65 20 73 61 66 65 6c 79 2c 20   to use safely, 
36537 62 65 63 61 75 73 65 20 61 6c 74 68 6f 75 67 68  because although
36538 20 69 74 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65   it.** is define
36539 64 20 74 6f 20 61 63 63 65 70 74 20 61 6e 20 61  d to accept an a
3653a 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20  rgument of type 
3653b 69 6e 74 2c 20 69 74 73 20 62 65 68 61 76 69 6f  int, its behavio
3653c 75 72 20 77 68 65 6e 20 70 61 73 73 65 64 0a 2a  ur when passed.*
3653d 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68 61  * an integer tha
3653e 74 20 66 61 6c 6c 73 20 6f 75 74 73 69 64 65 20  t falls outside 
3653f 6f 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20  of the range of 
36540 74 68 65 20 75 6e 73 69 67 6e 65 64 20 63 68 61  the unsigned cha
36541 72 20 74 79 70 65 0a 2a 2a 20 69 73 20 75 6e 64  r type.** is und
36542 65 66 69 6e 65 64 20 28 61 6e 64 20 73 6f 6d 65  efined (and some
36543 74 69 6d 65 73 2c 20 22 75 6e 64 65 66 69 6e 65  times, "undefine
36544 64 22 20 6d 65 61 6e 73 20 73 65 67 66 61 75 6c  d" means segfaul
36545 74 29 2e 20 54 68 69 73 20 77 72 61 70 70 65 72  t). This wrapper
36546 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 20 74  .** is defined t
36547 6f 20 61 63 63 65 70 74 20 61 6e 20 61 72 67 75  o accept an argu
36548 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 63 68 61  ment of type cha
36549 72 2c 20 61 6e 64 20 61 6c 77 61 79 73 20 72 65  r, and always re
3654a 74 75 72 6e 73 20 30 20 66 6f 72 0a 2a 2a 20 61  turns 0 for.** a
3654b 6e 79 20 76 61 6c 75 65 73 20 74 68 61 74 20 66  ny values that f
3654c 61 6c 6c 20 6f 75 74 73 69 64 65 20 6f 66 20 74  all outside of t
3654d 68 65 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20  he range of the 
3654e 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 79  unsigned char ty
3654f 70 65 20 28 69 2e 65 2e 0a 2a 2a 20 6e 65 67 61  pe (i.e..** nega
36550 74 69 76 65 20 76 61 6c 75 65 73 29 2e 0a 2a 2f  tive values)..*/
36551 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
36552 69 73 73 70 61 63 65 28 63 68 61 72 20 63 29 7b  isspace(char c){
36553 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30 78 38  .  return (c&0x8
36554 30 29 3d 3d 30 20 3f 20 69 73 73 70 61 63 65 28  0)==0 ? isspace(
36555 63 29 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  c) : 0;.}../*.**
36556 20 45 78 74 72 61 63 74 20 74 68 65 20 6e 65 78   Extract the nex
36557 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 62 75 66  t token from buf
36558 66 65 72 20 7a 20 28 6c 65 6e 67 74 68 20 6e 29  fer z (length n)
36559 20 75 73 69 6e 67 20 74 68 65 20 74 6f 6b 65 6e   using the token
3655a 69 7a 65 72 0a 2a 2a 20 61 6e 64 20 6f 74 68 65  izer.** and othe
3655b 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28 63  r information (c
3655c 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 65 74 63 2e  olumn names etc.
3655d 29 20 69 6e 20 70 50 61 72 73 65 2e 20 43 72 65  ) in pParse. Cre
3655e 61 74 65 20 61 6e 20 46 74 73 33 45 78 70 72 0a  ate an Fts3Expr.
3655f 2a 2a 20 73 74 72 75 63 74 75 72 65 20 6f 66 20  ** structure of 
36560 74 79 70 65 20 46 54 53 51 55 45 52 59 5f 50 48  type FTSQUERY_PH
36561 52 41 53 45 20 63 6f 6e 74 61 69 6e 69 6e 67 20  RASE containing 
36562 61 20 70 68 72 61 73 65 20 63 6f 6e 73 69 73 74  a phrase consist
36563 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a 2a 20 73  ing of this.** s
36564 69 6e 67 6c 65 20 74 6f 6b 65 6e 20 61 6e 64 20  ingle token and 
36565 73 65 74 20 2a 70 70 45 78 70 72 20 74 6f 20 70  set *ppExpr to p
36566 6f 69 6e 74 20 74 6f 20 69 74 2e 20 49 66 20 74  oint to it. If t
36567 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75  he end of the bu
36568 66 66 65 72 20 69 73 0a 2a 2a 20 72 65 61 63 68  ffer is.** reach
36569 65 64 20 62 65 66 6f 72 65 20 61 20 74 6f 6b 65  ed before a toke
3656a 6e 20 69 73 20 66 6f 75 6e 64 2c 20 73 65 74 20  n is found, set 
3656b 2a 70 70 45 78 70 72 20 74 6f 20 7a 65 72 6f 2e  *ppExpr to zero.
3656c 20 49 74 20 69 73 20 74 68 65 0a 2a 2a 20 72 65   It is the.** re
3656d 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
3656e 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 65 76  the caller to ev
3656f 65 6e 74 75 61 6c 6c 79 20 64 65 61 6c 6c 6f 63  entually dealloc
36570 61 74 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 65  ate the allocate
36571 64 20 0a 2a 2a 20 46 74 73 33 45 78 70 72 20 73  d .** Fts3Expr s
36572 74 72 75 63 74 75 72 65 20 28 69 66 20 61 6e 79  tructure (if any
36573 29 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 20  ) by passing it 
36574 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  to sqlite3_free(
36575 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  )..**.** Return 
36576 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63  SQLITE_OK if suc
36577 63 65 73 73 66 75 6c 2c 20 6f 72 20 53 51 4c 49  cessful, or SQLI
36578 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20 6d 65  TE_NOMEM if a me
36579 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  mory allocation.
3657a 2a 2a 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61  ** fails..*/.sta
3657b 74 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74 54  tic int getNextT
3657c 6f 6b 65 6e 28 0a 20 20 50 61 72 73 65 43 6f 6e  oken(.  ParseCon
3657d 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20 20 20  text *pParse,   
3657e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3657f 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20 70 61  /* fts3 query pa
36580 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
36581 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20   int iCol,      
36582 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36583 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
36584 65 20 66 6f 72 20 46 74 73 33 50 68 72 61 73 65  e for Fts3Phrase
36585 2e 69 43 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f  .iColumn */.  co
36586 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
36587 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   n,             
36588 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73        /* Input s
36589 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45  tring */.  Fts3E
3658a 78 70 72 20 2a 2a 70 70 45 78 70 72 2c 20 20 20  xpr **ppExpr,   
3658b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3658c 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65     /* OUT: expre
3658d 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  ssion */.  int *
3658e 70 6e 43 6f 6e 73 75 6d 65 64 20 20 20 20 20 20  pnConsumed      
3658f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36590 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65     /* OUT: Numbe
36591 72 20 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75  r of bytes consu
36592 6d 65 64 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  med */.){.  sqli
36593 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
36594 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 50 61 72  Tokenizer = pPar
36595 73 65 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a  se->pTokenizer;.
36596 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
36597 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
36598 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b   *pModule = pTok
36599 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b  enizer->pModule;
3659a 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
3659b 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
3659c 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a  ursor *pCursor;.
3659d 20 20 46 74 73 33 45 78 70 72 20 2a 70 52 65 74    Fts3Expr *pRet
3659e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 43 6f 6e   = 0;.  int nCon
3659f 73 75 6d 65 64 20 3d 20 30 3b 0a 0a 20 20 72 63  sumed = 0;..  rc
365a0 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65   = pModule->xOpe
365a1 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 2c  n(pTokenizer, z,
365a2 20 6e 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a 20   n, &pCursor);. 
365a3 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
365a4 4f 4b 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  OK ){.    const 
365a5 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20  char *zToken;.  
365a6 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 69 53    int nToken, iS
365a7 74 61 72 74 2c 20 69 45 6e 64 2c 20 69 50 6f 73  tart, iEnd, iPos
365a8 69 74 69 6f 6e 3b 0a 20 20 20 20 69 6e 74 20 6e  ition;.    int n
365a9 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 20  Byte;           
365aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
365ab 20 20 20 20 2f 2a 20 74 6f 74 61 6c 20 73 70 61      /* total spa
365ac 63 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a  ce to allocate *
365ad 2f 0a 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e  /..    pCursor->
365ae 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
365af 6b 65 6e 69 7a 65 72 3b 0a 20 20 20 20 72 63 20  kenizer;.    rc 
365b0 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78 74  = pModule->xNext
365b1 28 70 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b 65  (pCursor, &zToke
365b2 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53 74  n, &nToken, &iSt
365b3 61 72 74 2c 20 26 69 45 6e 64 2c 20 26 69 50 6f  art, &iEnd, &iPo
365b4 73 69 74 69 6f 6e 29 3b 0a 0a 20 20 20 20 69 66  sition);..    if
365b5 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
365b6 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20 3d  ){.      nByte =
365b7 20 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72   sizeof(Fts3Expr
365b8 29 20 2b 20 73 69 7a 65 6f 66 28 46 74 73 33 50  ) + sizeof(Fts3P
365b9 68 72 61 73 65 29 20 2b 20 6e 54 6f 6b 65 6e 3b  hrase) + nToken;
365ba 0a 20 20 20 20 20 20 70 52 65 74 20 3d 20 28 46  .      pRet = (F
365bb 74 73 33 45 78 70 72 20 2a 29 73 71 6c 69 74 65  ts3Expr *)sqlite
365bc 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b  3_malloc(nByte);
365bd 0a 20 20 20 20 20 20 69 66 28 20 21 70 52 65 74  .      if( !pRet
365be 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
365bf 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
365c0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
365c1 20 20 20 20 6d 65 6d 73 65 74 28 70 52 65 74 2c      memset(pRet,
365c2 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20   0, nByte);.    
365c3 20 20 20 20 70 52 65 74 2d 3e 65 54 79 70 65 20      pRet->eType 
365c4 3d 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  = FTSQUERY_PHRAS
365c5 45 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  E;.        pRet-
365c6 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74 73 33  >pPhrase = (Fts3
365c7 50 68 72 61 73 65 20 2a 29 26 70 52 65 74 5b 31  Phrase *)&pRet[1
365c8 5d 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d  ];.        pRet-
365c9 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
365ca 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 52   = 1;.        pR
365cb 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f  et->pPhrase->iCo
365cc 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  lumn = iCol;.   
365cd 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61       pRet->pPhra
365ce 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 6e 20  se->aToken[0].n 
365cf 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  = nToken;.      
365d0 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d    pRet->pPhrase-
365d1 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 7a 20 3d 20 28  >aToken[0].z = (
365d2 63 68 61 72 20 2a 29 26 70 52 65 74 2d 3e 70 50  char *)&pRet->pP
365d3 68 72 61 73 65 5b 31 5d 3b 0a 20 20 20 20 20 20  hrase[1];.      
365d4 20 20 6d 65 6d 63 70 79 28 70 52 65 74 2d 3e 70    memcpy(pRet->p
365d5 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30  Phrase->aToken[0
365d6 5d 2e 7a 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f  ].z, zToken, nTo
365d7 6b 65 6e 29 3b 0a 0a 20 20 20 20 20 20 20 20 69  ken);..        i
365d8 66 28 20 69 45 6e 64 3c 6e 20 26 26 20 7a 5b 69  f( iEnd<n && z[i
365d9 45 6e 64 5d 3d 3d 27 2a 27 20 29 7b 0a 20 20 20  End]=='*' ){.   
365da 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68         pRet->pPh
365db 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e  rase->aToken[0].
365dc 69 73 50 72 65 66 69 78 20 3d 20 31 3b 0a 20 20  isPrefix = 1;.  
365dd 20 20 20 20 20 20 20 20 69 45 6e 64 2b 2b 3b 0a          iEnd++;.
365de 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
365df 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 66    if( !sqlite3_f
365e0 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
365e1 74 68 65 73 65 73 20 26 26 20 69 53 74 61 72 74  theses && iStart
365e2 3e 30 20 26 26 20 7a 5b 69 53 74 61 72 74 2d 31  >0 && z[iStart-1
365e3 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20  ]=='-' ){.      
365e4 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61 73      pRet->pPhras
365e5 65 2d 3e 69 73 4e 6f 74 20 3d 20 31 3b 0a 20 20  e->isNot = 1;.  
365e6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
365e7 20 20 20 20 20 20 6e 43 6f 6e 73 75 6d 65 64 20        nConsumed 
365e8 3d 20 69 45 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20  = iEnd;.    }.. 
365e9 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f     pModule->xClo
365ea 73 65 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  se(pCursor);.  }
365eb 0a 20 20 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65  .  .  *pnConsume
365ec 64 20 3d 20 6e 43 6f 6e 73 75 6d 65 64 3b 0a 20  d = nConsumed;. 
365ed 20 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74 3b   *ppExpr = pRet;
365ee 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
365ef 0a 0a 2f 2a 0a 2a 2a 20 45 6e 6c 61 72 67 65 20  ../*.** Enlarge 
365f0 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
365f1 69 6f 6e 2e 20 20 49 66 20 61 6e 20 6f 75 74 2d  ion.  If an out-
365f2 6f 66 2d 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  of-memory alloca
365f3 74 69 6f 6e 20 6f 63 63 75 72 73 2c 0a 2a 2a 20  tion occurs,.** 
365f4 74 68 65 6e 20 66 72 65 65 20 74 68 65 20 6f 6c  then free the ol
365f5 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f  d allocation..*/
365f6 0a 76 6f 69 64 20 2a 66 74 73 33 52 65 61 6c 6c  .void *fts3Reall
365f7 6f 63 4f 72 46 72 65 65 28 76 6f 69 64 20 2a 70  ocOrFree(void *p
365f8 4f 72 69 67 2c 20 69 6e 74 20 6e 4e 65 77 29 7b  Orig, int nNew){
365f9 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 20 3d 20  .  void *pRet = 
365fa 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
365fb 70 4f 72 69 67 2c 20 6e 4e 65 77 29 3b 0a 20 20  pOrig, nNew);.  
365fc 69 66 28 20 21 70 52 65 74 20 29 7b 0a 20 20 20  if( !pRet ){.   
365fd 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4f   sqlite3_free(pO
365fe 72 69 67 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rig);.  }.  retu
365ff 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pRet;.}../*.*
36600 2a 20 42 75 66 66 65 72 20 7a 49 6e 70 75 74 2c  * Buffer zInput,
36601 20 6c 65 6e 67 74 68 20 6e 49 6e 70 75 74 2c 20   length nInput, 
36602 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e  contains the con
36603 74 65 6e 74 73 20 6f 66 20 61 20 71 75 6f 74 65  tents of a quote
36604 64 20 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74  d string.** that
36605 20 61 70 70 65 61 72 65 64 20 61 73 20 70 61 72   appeared as par
36606 74 20 6f 66 20 61 6e 20 66 74 73 33 20 71 75 65  t of an fts3 que
36607 72 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e  ry expression. N
36608 65 69 74 68 65 72 20 71 75 6f 74 65 20 63 68 61  either quote cha
36609 72 61 63 74 65 72 0a 2a 2a 20 69 73 20 69 6e 63  racter.** is inc
3660a 6c 75 64 65 64 20 69 6e 20 74 68 65 20 62 75 66  luded in the buf
3660b 66 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69  fer. This functi
3660c 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 74  on attempts to t
3660d 6f 6b 65 6e 69 7a 65 20 74 68 65 20 65 6e 74 69  okenize the enti
3660e 72 65 0a 2a 2a 20 69 6e 70 75 74 20 62 75 66 66  re.** input buff
3660f 65 72 20 61 6e 64 20 63 72 65 61 74 65 20 61 6e  er and create an
36610 20 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74   Fts3Expr struct
36611 75 72 65 20 6f 66 20 74 79 70 65 20 46 54 53 51  ure of type FTSQ
36612 55 45 52 59 5f 50 48 52 41 53 45 20 0a 2a 2a 20  UERY_PHRASE .** 
36613 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72  containing the r
36614 65 73 75 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  esults..**.** If
36615 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
36616 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
36617 65 64 20 61 6e 64 20 2a 70 70 45 78 70 72 20 73  ed and *ppExpr s
36618 65 74 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74  et to point at t
36619 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  he.** allocated 
3661a 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75  Fts3Expr structu
3661b 72 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 65  re. Otherwise, e
3661c 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d  ither SQLITE_NOM
3661d 45 4d 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  EM (out of memor
3661e 79 0a 2a 2a 20 65 72 72 6f 72 29 20 6f 72 20 53  y.** error) or S
3661f 51 4c 49 54 45 5f 45 52 52 4f 52 20 28 74 6f 6b  QLITE_ERROR (tok
36620 65 6e 69 7a 61 74 69 6f 6e 20 65 72 72 6f 72 29  enization error)
36621 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
36622 20 2a 70 70 45 78 70 72 20 73 65 74 0a 2a 2a 20   *ppExpr set.** 
36623 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  to 0..*/.static 
36624 69 6e 74 20 67 65 74 4e 65 78 74 53 74 72 69 6e  int getNextStrin
36625 67 28 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78  g(.  ParseContex
36626 74 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  t *pParse,      
36627 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36628 66 74 73 33 20 71 75 65 72 79 20 70 61 72 73 65  fts3 query parse
36629 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
3662a 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
3662b 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20  , int nInput,   
3662c 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73        /* Input s
3662d 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74 73 33 45  tring */.  Fts3E
3662e 78 70 72 20 2a 2a 70 70 45 78 70 72 20 20 20 20  xpr **ppExpr    
3662f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36630 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78 70 72 65     /* OUT: expre
36631 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71  ssion */.){.  sq
36632 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
36633 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 50  *pTokenizer = pP
36634 61 72 73 65 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  arse->pTokenizer
36635 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ;.  sqlite3_toke
36636 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e  nizer_module con
36637 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54  st *pModule = pT
36638 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c  okenizer->pModul
36639 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 46  e;.  int rc;.  F
3663a 74 73 33 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a  ts3Expr *p = 0;.
3663b 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3663c 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
3663d 73 6f 72 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  sor = 0;.  char 
3663e 2a 7a 54 65 6d 70 20 3d 20 30 3b 0a 20 20 69 6e  *zTemp = 0;.  in
3663f 74 20 6e 54 65 6d 70 20 3d 20 30 3b 0a 0a 20 20  t nTemp = 0;..  
36640 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4f  rc = pModule->xO
36641 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  pen(pTokenizer, 
36642 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20  zInput, nInput, 
36643 26 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28  &pCursor);.  if(
36644 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
36645 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20  {.    int ii;.  
36646 20 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65    pCursor->pToke
36647 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
36648 65 72 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30  er;.    for(ii=0
36649 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  ; rc==SQLITE_OK;
3664a 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 6f   ii++){.      co
3664b 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e  nst char *zToken
3664c 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 6b  ;.      int nTok
3664d 65 6e 2c 20 69 42 65 67 69 6e 2c 20 69 45 6e 64  en, iBegin, iEnd
3664e 2c 20 69 50 6f 73 3b 0a 20 20 20 20 20 20 72 63  , iPos;.      rc
3664f 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e 65 78   = pModule->xNex
36650 74 28 70 43 75 72 73 6f 72 2c 20 26 7a 54 6f 6b  t(pCursor, &zTok
36651 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 42  en, &nToken, &iB
36652 65 67 69 6e 2c 20 26 69 45 6e 64 2c 20 26 69 50  egin, &iEnd, &iP
36653 6f 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  os);.      if( r
36654 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
36655 20 20 20 20 20 20 20 20 69 6e 74 20 6e 42 79 74          int nByt
36656 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73 33 45  e = sizeof(Fts3E
36657 78 70 72 29 20 2b 20 73 69 7a 65 6f 66 28 46 74  xpr) + sizeof(Ft
36658 73 33 50 68 72 61 73 65 29 3b 0a 20 20 20 20 20  s3Phrase);.     
36659 20 20 20 70 20 3d 20 66 74 73 33 52 65 61 6c 6c     p = fts3Reall
3665a 6f 63 4f 72 46 72 65 65 28 70 2c 20 6e 42 79 74  ocOrFree(p, nByt
3665b 65 2b 69 69 2a 73 69 7a 65 6f 66 28 73 74 72 75  e+ii*sizeof(stru
3665c 63 74 20 50 68 72 61 73 65 54 6f 6b 65 6e 29 29  ct PhraseToken))
3665d 3b 0a 20 20 20 20 20 20 20 20 7a 54 65 6d 70 20  ;.        zTemp 
3665e 3d 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46  = fts3ReallocOrF
3665f 72 65 65 28 7a 54 65 6d 70 2c 20 6e 54 65 6d 70  ree(zTemp, nTemp
36660 20 2b 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20   + nToken);.    
36661 20 20 20 20 69 66 28 20 21 70 20 7c 7c 20 21 7a      if( !p || !z
36662 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20  Temp ){.        
36663 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
36664 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
36665 20 69 66 28 20 69 69 3d 3d 30 20 29 7b 0a 20 20   if( ii==0 ){.  
36666 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70          memset(p
36667 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20  , 0, nByte);.   
36668 20 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73         p->pPhras
36669 65 20 3d 20 28 46 74 73 33 50 68 72 61 73 65 20  e = (Fts3Phrase 
3666a 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 20 20 20  *)&p[1];.       
3666b 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 50   }.        p->pP
3666c 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68 72  hrase = (Fts3Phr
3666d 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  ase *)&p[1];.   
3666e 20 20 20 20 20 70 2d 3e 70 50 68 72 61 73 65 2d       p->pPhrase-
3666f 3e 6e 54 6f 6b 65 6e 20 3d 20 69 69 2b 31 3b 0a  >nToken = ii+1;.
36670 20 20 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61          p->pPhra
36671 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69 5d 2e 6e  se->aToken[ii].n
36672 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20   = nToken;.     
36673 20 20 20 6d 65 6d 63 70 79 28 26 7a 54 65 6d 70     memcpy(&zTemp
36674 5b 6e 54 65 6d 70 5d 2c 20 7a 54 6f 6b 65 6e 2c  [nTemp], zToken,
36675 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20   nToken);.      
36676 20 20 6e 54 65 6d 70 20 2b 3d 20 6e 54 6f 6b 65    nTemp += nToke
36677 6e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  n;.        if( i
36678 45 6e 64 3c 6e 49 6e 70 75 74 20 26 26 20 7a 49  End<nInput && zI
36679 6e 70 75 74 5b 69 45 6e 64 5d 3d 3d 27 2a 27 20  nput[iEnd]=='*' 
3667a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e  ){.          p->
3667b 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
3667c 69 69 5d 2e 69 73 50 72 65 66 69 78 20 3d 20 31  ii].isPrefix = 1
3667d 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
3667e 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50  .          p->pP
3667f 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69  hrase->aToken[ii
36680 5d 2e 69 73 50 72 65 66 69 78 20 3d 20 30 3b 0a  ].isPrefix = 0;.
36681 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
36682 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 6f  }.    }..    pMo
36683 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75  dule->xClose(pCu
36684 72 73 6f 72 29 3b 0a 20 20 20 20 70 43 75 72 73  rsor);.    pCurs
36685 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69  or = 0;.  }..  i
36686 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
36687 4e 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 6a  NE ){.    int jj
36688 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77  ;.    char *zNew
36689 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74   = NULL;.    int
3668a 20 6e 4e 65 77 20 3d 20 30 3b 0a 20 20 20 20 69   nNew = 0;.    i
3668b 6e 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f  nt nByte = sizeo
3668c 66 28 46 74 73 33 45 78 70 72 29 20 2b 20 73 69  f(Fts3Expr) + si
3668d 7a 65 6f 66 28 46 74 73 33 50 68 72 61 73 65 29  zeof(Fts3Phrase)
3668e 3b 0a 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 28  ;.    nByte += (
3668f 70 3f 28 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  p?(p->pPhrase->n
36690 54 6f 6b 65 6e 2d 31 29 3a 30 29 20 2a 20 73 69  Token-1):0) * si
36691 7a 65 6f 66 28 73 74 72 75 63 74 20 50 68 72 61  zeof(struct Phra
36692 73 65 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 70 20  seToken);.    p 
36693 3d 20 66 74 73 33 52 65 61 6c 6c 6f 63 4f 72 46  = fts3ReallocOrF
36694 72 65 65 28 70 2c 20 6e 42 79 74 65 20 2b 20 6e  ree(p, nByte + n
36695 54 65 6d 70 29 3b 0a 20 20 20 20 69 66 28 20 21  Temp);.    if( !
36696 70 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  p ){.      goto 
36697 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20  no_mem;.    }.  
36698 20 20 69 66 28 20 7a 54 65 6d 70 20 29 7b 0a 20    if( zTemp ){. 
36699 20 20 20 20 20 7a 4e 65 77 20 3d 20 26 28 28 28       zNew = &(((
3669a 63 68 61 72 20 2a 29 70 29 5b 6e 42 79 74 65 5d  char *)p)[nByte]
3669b 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  );.      memcpy(
3669c 7a 4e 65 77 2c 20 7a 54 65 6d 70 2c 20 6e 54 65  zNew, zTemp, nTe
3669d 6d 70 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  mp);.    }else{.
3669e 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20        memset(p, 
3669f 30 2c 20 6e 42 79 74 65 2b 6e 54 65 6d 70 29 3b  0, nByte+nTemp);
366a0 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70 50  .    }.    p->pP
366a1 68 72 61 73 65 20 3d 20 28 46 74 73 33 50 68 72  hrase = (Fts3Phr
366a2 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  ase *)&p[1];.   
366a3 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 70 2d   for(jj=0; jj<p-
366a4 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
366a5 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 70  ; jj++){.      p
366a6 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  ->pPhrase->aToke
366a7 6e 5b 6a 6a 5d 2e 7a 20 3d 20 26 7a 4e 65 77 5b  n[jj].z = &zNew[
366a8 6e 4e 65 77 5d 3b 0a 20 20 20 20 20 20 6e 4e 65  nNew];.      nNe
366a9 77 20 2b 3d 20 70 2d 3e 70 50 68 72 61 73 65 2d  w += p->pPhrase-
366aa 3e 61 54 6f 6b 65 6e 5b 6a 6a 5d 2e 6e 3b 0a 20  >aToken[jj].n;. 
366ab 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
366ac 5f 66 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20 20  _free(zTemp);.  
366ad 20 20 70 2d 3e 65 54 79 70 65 20 3d 20 46 54 53    p->eType = FTS
366ae 51 55 45 52 59 5f 50 48 52 41 53 45 3b 0a 20 20  QUERY_PHRASE;.  
366af 20 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69 43    p->pPhrase->iC
366b0 6f 6c 75 6d 6e 20 3d 20 70 50 61 72 73 65 2d 3e  olumn = pParse->
366b1 69 44 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 20  iDefaultCol;.   
366b2 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
366b3 0a 20 20 7d 0a 0a 20 20 2a 70 70 45 78 70 72 20  .  }..  *ppExpr 
366b4 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  = p;.  return rc
366b5 3b 0a 6e 6f 5f 6d 65 6d 3a 0a 0a 20 20 69 66 28  ;.no_mem:..  if(
366b6 20 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20   pCursor ){.    
366b7 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28  pModule->xClose(
366b8 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20  pCursor);.  }.  
366b9 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65  sqlite3_free(zTe
366ba 6d 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  mp);.  sqlite3_f
366bb 72 65 65 28 70 29 3b 0a 20 20 2a 70 70 45 78 70  ree(p);.  *ppExp
366bc 72 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  r = 0;.  return 
366bd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a  SQLITE_NOMEM;.}.
366be 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20  ./*.** Function 
366bf 67 65 74 4e 65 78 74 4e 6f 64 65 28 29 2c 20 77  getNextNode(), w
366c0 68 69 63 68 20 69 73 20 63 61 6c 6c 65 64 20 62  hich is called b
366c1 79 20 66 74 73 33 45 78 70 72 50 61 72 73 65 28  y fts3ExprParse(
366c2 29 2c 20 6d 61 79 20 69 74 73 65 6c 66 0a 2a 2a  ), may itself.**
366c3 20 63 61 6c 6c 20 66 74 73 33 45 78 70 72 50 61   call fts3ExprPa
366c4 72 73 65 28 29 2e 20 53 6f 20 74 68 69 73 20 66  rse(). So this f
366c5 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
366c6 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a  on is required..
366c7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
366c8 73 33 45 78 70 72 50 61 72 73 65 28 50 61 72 73  s3ExprParse(Pars
366c9 65 43 6f 6e 74 65 78 74 20 2a 2c 20 63 6f 6e 73  eContext *, cons
366ca 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20 46  t char *, int, F
366cb 74 73 33 45 78 70 72 20 2a 2a 2c 20 69 6e 74 20  ts3Expr **, int 
366cc 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f  *);../*.** The o
366cd 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 20 2a  utput variable *
366ce 70 70 45 78 70 72 20 69 73 20 70 6f 70 75 6c 61  ppExpr is popula
366cf 74 65 64 20 77 69 74 68 20 61 6e 20 61 6c 6c 6f  ted with an allo
366d0 63 61 74 65 64 20 46 74 73 33 45 78 70 72 20 0a  cated Fts3Expr .
366d1 2a 2a 20 73 74 72 75 63 74 75 72 65 2c 20 6f 72  ** structure, or
366d2 20 73 65 74 20 74 6f 20 30 20 69 66 20 74 68 65   set to 0 if the
366d3 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 70 75   end of the inpu
366d4 74 20 62 75 66 66 65 72 20 69 73 20 72 65 61 63  t buffer is reac
366d5 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  hed..**.** Retur
366d6 6e 73 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  ns an SQLite err
366d7 6f 72 20 63 6f 64 65 2e 20 53 51 4c 49 54 45 5f  or code. SQLITE_
366d8 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e 67  OK if everything
366d9 20 77 6f 72 6b 73 2c 20 53 51 4c 49 54 45 5f 4e   works, SQLITE_N
366da 4f 4d 45 4d 0a 2a 2a 20 69 66 20 61 20 6d 61 6c  OMEM.** if a mal
366db 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75  loc failure occu
366dc 72 73 2c 20 6f 72 20 53 51 4c 49 54 45 5f 45 52  rs, or SQLITE_ER
366dd 52 4f 52 20 69 66 20 61 20 70 61 72 73 65 20 65  ROR if a parse e
366de 72 72 6f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65  rror is encounte
366df 72 65 64 2e 0a 2a 2a 20 49 66 20 53 51 4c 49 54  red..** If SQLIT
366e0 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74 75 72  E_ERROR is retur
366e1 6e 65 64 2c 20 70 43 6f 6e 74 65 78 74 20 69 73  ned, pContext is
366e2 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74 68 20   populated with 
366e3 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
366e4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
366e5 67 65 74 4e 65 78 74 4e 6f 64 65 28 0a 20 20 50  getNextNode(.  P
366e6 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50 61  arseContext *pPa
366e7 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
366e8 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20 71         /* fts3 q
366e9 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74 65  uery parse conte
366ea 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  xt */.  const ch
366eb 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20 20  ar *z, int n,   
366ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
366ed 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67 20  /* Input string 
366ee 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a  */.  Fts3Expr **
366ef 70 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  ppExpr,         
366f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
366f1 4f 55 54 3a 20 65 78 70 72 65 73 73 69 6f 6e 20  OUT: expression 
366f2 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6e 73  */.  int *pnCons
366f3 75 6d 65 64 20 20 20 20 20 20 20 20 20 20 20 20  umed            
366f4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
366f5 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62  OUT: Number of b
366f6 79 74 65 73 20 63 6f 6e 73 75 6d 65 64 20 2a 2f  ytes consumed */
366f7 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  .){.  static con
366f8 73 74 20 73 74 72 75 63 74 20 46 74 73 33 4b 65  st struct Fts3Ke
366f9 79 77 6f 72 64 20 7b 0a 20 20 20 20 63 68 61 72  yword {.    char
366fa 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20   *z;            
366fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
366fc 20 20 2f 2a 20 4b 65 79 77 6f 72 64 20 74 65 78    /* Keyword tex
366fd 74 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  t */.    unsigne
366fe 64 20 63 68 61 72 20 6e 3b 20 20 20 20 20 20 20  d char n;       
366ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36700 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  * Length of the 
36701 6b 65 79 77 6f 72 64 20 2a 2f 0a 20 20 20 20 75  keyword */.    u
36702 6e 73 69 67 6e 65 64 20 63 68 61 72 20 70 61 72  nsigned char par
36703 65 6e 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20  enOnly;         
36704 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c       /* Only val
36705 69 64 20 69 6e 20 70 61 72 65 6e 20 6d 6f 64 65  id in paren mode
36706 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65 64   */.    unsigned
36707 20 63 68 61 72 20 65 54 79 70 65 3b 20 20 20 20   char eType;    
36708 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36709 20 4b 65 79 77 6f 72 64 20 63 6f 64 65 20 2a 2f   Keyword code */
3670a 0a 20 20 7d 20 61 4b 65 79 77 6f 72 64 5b 5d 20  .  } aKeyword[] 
3670b 3d 20 7b 0a 20 20 20 20 7b 20 22 4f 52 22 20 2c  = {.    { "OR" ,
3670c 20 20 32 2c 20 30 2c 20 46 54 53 51 55 45 52 59    2, 0, FTSQUERY
3670d 5f 4f 52 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22  _OR   },.    { "
3670e 41 4e 44 22 2c 20 20 33 2c 20 31 2c 20 46 54 53  AND",  3, 1, FTS
3670f 51 55 45 52 59 5f 41 4e 44 20 20 7d 2c 0a 20 20  QUERY_AND  },.  
36710 20 20 7b 20 22 4e 4f 54 22 2c 20 20 33 2c 20 31    { "NOT",  3, 1
36711 2c 20 46 54 53 51 55 45 52 59 5f 4e 4f 54 20 20  , FTSQUERY_NOT  
36712 7d 2c 0a 20 20 20 20 7b 20 22 4e 45 41 52 22 2c  },.    { "NEAR",
36713 20 34 2c 20 30 2c 20 46 54 53 51 55 45 52 59 5f   4, 0, FTSQUERY_
36714 4e 45 41 52 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e  NEAR }.  };.  in
36715 74 20 69 69 3b 0a 20 20 69 6e 74 20 69 43 6f 6c  t ii;.  int iCol
36716 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 4c 65 6e 3b  ;.  int iColLen;
36717 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 46 74 73  .  int rc;.  Fts
36718 33 45 78 70 72 20 2a 70 52 65 74 20 3d 20 30 3b  3Expr *pRet = 0;
36719 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
3671a 7a 49 6e 70 75 74 20 3d 20 7a 3b 0a 20 20 69 6e  zInput = z;.  in
3671b 74 20 6e 49 6e 70 75 74 20 3d 20 6e 3b 0a 0a 20  t nInput = n;.. 
3671c 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 61 6e   /* Skip over an
3671d 79 20 77 68 69 74 65 73 70 61 63 65 20 62 65 66  y whitespace bef
3671e 6f 72 65 20 63 68 65 63 6b 69 6e 67 20 66 6f 72  ore checking for
3671f 20 61 20 6b 65 79 77 6f 72 64 2c 20 61 6e 20 6f   a keyword, an o
36720 70 65 6e 20 6f 72 0a 20 20 2a 2a 20 63 6c 6f 73  pen or.  ** clos
36721 65 20 62 72 61 63 6b 65 74 2c 20 6f 72 20 61 20  e bracket, or a 
36722 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2e 20 0a  quoted string. .
36723 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 6e 49    */.  while( nI
36724 6e 70 75 74 3e 30 20 26 26 20 66 74 73 33 69 73  nput>0 && fts3is
36725 73 70 61 63 65 28 2a 7a 49 6e 70 75 74 29 20 29  space(*zInput) )
36726 7b 0a 20 20 20 20 6e 49 6e 70 75 74 2d 2d 3b 0a  {.    nInput--;.
36727 20 20 20 20 7a 49 6e 70 75 74 2b 2b 3b 0a 20 20      zInput++;.  
36728 7d 0a 20 20 69 66 28 20 6e 49 6e 70 75 74 3d 3d  }.  if( nInput==
36729 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
3672a 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d  SQLITE_DONE;.  }
3672b 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 77 65  ..  /* See if we
3672c 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
3672d 68 20 61 20 6b 65 79 77 6f 72 64 2e 20 2a 2f 0a  h a keyword. */.
3672e 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28    for(ii=0; ii<(
3672f 69 6e 74 29 28 73 69 7a 65 6f 66 28 61 4b 65 79  int)(sizeof(aKey
36730 77 6f 72 64 29 2f 73 69 7a 65 6f 66 28 73 74 72  word)/sizeof(str
36731 75 63 74 20 46 74 73 33 4b 65 79 77 6f 72 64 29  uct Fts3Keyword)
36732 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 63 6f  ); ii++){.    co
36733 6e 73 74 20 73 74 72 75 63 74 20 46 74 73 33 4b  nst struct Fts3K
36734 65 79 77 6f 72 64 20 2a 70 4b 65 79 20 3d 20 26  eyword *pKey = &
36735 61 4b 65 79 77 6f 72 64 5b 69 69 5d 3b 0a 0a 20  aKeyword[ii];.. 
36736 20 20 20 69 66 28 20 28 70 4b 65 79 2d 3e 70 61     if( (pKey->pa
36737 72 65 6e 4f 6e 6c 79 20 26 20 7e 73 71 6c 69 74  renOnly & ~sqlit
36738 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70  e3_fts3_enable_p
36739 61 72 65 6e 74 68 65 73 65 73 29 21 3d 30 20 29  arentheses)!=0 )
3673a 7b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  {.      continue
3673b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
3673c 20 6e 49 6e 70 75 74 3e 3d 70 4b 65 79 2d 3e 6e   nInput>=pKey->n
3673d 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 7a 49   && 0==memcmp(zI
3673e 6e 70 75 74 2c 20 70 4b 65 79 2d 3e 7a 2c 20 70  nput, pKey->z, p
3673f 4b 65 79 2d 3e 6e 29 20 29 7b 0a 20 20 20 20 20  Key->n) ){.     
36740 20 69 6e 74 20 6e 4e 65 61 72 20 3d 20 53 51 4c   int nNear = SQL
36741 49 54 45 5f 46 54 53 33 5f 44 45 46 41 55 4c 54  ITE_FTS3_DEFAULT
36742 5f 4e 45 41 52 5f 50 41 52 41 4d 3b 0a 20 20 20  _NEAR_PARAM;.   
36743 20 20 20 69 6e 74 20 6e 4b 65 79 20 3d 20 70 4b     int nKey = pK
36744 65 79 2d 3e 6e 3b 0a 20 20 20 20 20 20 63 68 61  ey->n;.      cha
36745 72 20 63 4e 65 78 74 3b 0a 0a 20 20 20 20 20 20  r cNext;..      
36746 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
36747 22 4e 45 41 52 22 20 6b 65 79 77 6f 72 64 2c 20  "NEAR" keyword, 
36748 63 68 65 63 6b 20 66 6f 72 20 61 6e 20 65 78 70  check for an exp
36749 6c 69 63 69 74 20 6e 65 61 72 6e 65 73 73 2e 20  licit nearness. 
3674a 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4b 65  */.      if( pKe
3674b 79 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  y->eType==FTSQUE
3674c 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20  RY_NEAR ){.     
3674d 20 20 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3d     assert( nKey=
3674e 3d 34 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66  =4 );.        if
3674f 28 20 7a 49 6e 70 75 74 5b 34 5d 3d 3d 27 2f 27  ( zInput[4]=='/'
36750 20 26 26 20 7a 49 6e 70 75 74 5b 35 5d 3e 3d 27   && zInput[5]>='
36751 30 27 20 26 26 20 7a 49 6e 70 75 74 5b 35 5d 3c  0' && zInput[5]<
36752 3d 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='9' ){.        
36753 20 20 6e 4e 65 61 72 20 3d 20 30 3b 0a 20 20 20    nNear = 0;.   
36754 20 20 20 20 20 20 20 66 6f 72 28 6e 4b 65 79 3d         for(nKey=
36755 35 3b 20 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 3e  5; zInput[nKey]>
36756 3d 27 30 27 20 26 26 20 7a 49 6e 70 75 74 5b 6e  ='0' && zInput[n
36757 4b 65 79 5d 3c 3d 27 39 27 3b 20 6e 4b 65 79 2b  Key]<='9'; nKey+
36758 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
36759 6e 4e 65 61 72 20 3d 20 6e 4e 65 61 72 20 2a 20  nNear = nNear * 
3675a 31 30 20 2b 20 28 7a 49 6e 70 75 74 5b 6e 4b 65  10 + (zInput[nKe
3675b 79 5d 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 20  y] - '0');.     
3675c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
3675d 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
3675e 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
3675f 20 74 68 69 73 20 69 73 20 70 72 6f 62 61 62 6c   this is probabl
36760 79 20 61 20 6b 65 79 77 6f 72 64 2e 20 42 75 74  y a keyword. But
36761 20 66 6f 72 20 74 68 61 74 20 74 6f 20 62 65 20   for that to be 
36762 74 72 75 65 2c 0a 20 20 20 20 20 20 2a 2a 20 74  true,.      ** t
36763 68 65 20 6e 65 78 74 20 62 79 74 65 20 6d 75 73  he next byte mus
36764 74 20 63 6f 6e 74 61 69 6e 20 65 69 74 68 65 72  t contain either
36765 20 77 68 69 74 65 73 70 61 63 65 2c 20 61 6e 20   whitespace, an 
36766 6f 70 65 6e 20 6f 72 20 63 6c 6f 73 65 0a 20 20  open or close.  
36767 20 20 20 20 2a 2a 20 70 61 72 65 6e 74 68 65 73      ** parenthes
36768 69 73 2c 20 61 20 71 75 6f 74 65 20 63 68 61 72  is, a quote char
36769 61 63 74 65 72 2c 20 6f 72 20 45 4f 46 2e 20 0a  acter, or EOF. .
3676a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63        */.      c
3676b 4e 65 78 74 20 3d 20 7a 49 6e 70 75 74 5b 6e 4b  Next = zInput[nK
3676c 65 79 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 66  ey];.      if( f
3676d 74 73 33 69 73 73 70 61 63 65 28 63 4e 65 78 74  ts3isspace(cNext
3676e 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 63 4e 65  ) .       || cNe
3676f 78 74 3d 3d 27 22 27 20 7c 7c 20 63 4e 65 78 74  xt=='"' || cNext
36770 3d 3d 27 28 27 20 7c 7c 20 63 4e 65 78 74 3d 3d  =='(' || cNext==
36771 27 29 27 20 7c 7c 20 63 4e 65 78 74 3d 3d 30 0a  ')' || cNext==0.
36772 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
36773 20 70 52 65 74 20 3d 20 28 46 74 73 33 45 78 70   pRet = (Fts3Exp
36774 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
36775 6f 63 28 73 69 7a 65 6f 66 28 46 74 73 33 45 78  oc(sizeof(Fts3Ex
36776 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66  pr));.        if
36777 28 20 21 70 52 65 74 20 29 7b 0a 20 20 20 20 20  ( !pRet ){.     
36778 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
36779 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
3677a 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 65 6d 73    }.        mems
3677b 65 74 28 70 52 65 74 2c 20 30 2c 20 73 69 7a 65  et(pRet, 0, size
3677c 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20  of(Fts3Expr));. 
3677d 20 20 20 20 20 20 20 70 52 65 74 2d 3e 65 54 79         pRet->eTy
3677e 70 65 20 3d 20 70 4b 65 79 2d 3e 65 54 79 70 65  pe = pKey->eType
3677f 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e  ;.        pRet->
36780 6e 4e 65 61 72 20 3d 20 6e 4e 65 61 72 3b 0a 20  nNear = nNear;. 
36781 20 20 20 20 20 20 20 2a 70 70 45 78 70 72 20 3d         *ppExpr =
36782 20 70 52 65 74 3b 0a 20 20 20 20 20 20 20 20 2a   pRet;.        *
36783 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 69 6e  pnConsumed = (in
36784 74 29 28 28 7a 49 6e 70 75 74 20 2d 20 7a 29 20  t)((zInput - z) 
36785 2b 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20  + nKey);.       
36786 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
36787 4b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  K;.      }..    
36788 20 20 2f 2a 20 54 75 72 6e 73 20 6f 75 74 20 74    /* Turns out t
36789 68 61 74 20 77 61 73 6e 27 74 20 61 20 6b 65 79  hat wasn't a key
3678a 77 6f 72 64 20 61 66 74 65 72 20 61 6c 6c 2e 20  word after all. 
3678b 54 68 69 73 20 68 61 70 70 65 6e 73 20 69 66 20  This happens if 
3678c 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 75 73 65  the.      ** use
3678d 72 20 68 61 73 20 73 75 70 70 6c 69 65 64 20 61  r has supplied a
3678e 20 74 6f 6b 65 6e 20 73 75 63 68 20 61 73 20 22   token such as "
3678f 4f 52 61 63 6c 65 22 2e 20 43 6f 6e 74 69 6e 75  ORacle". Continu
36790 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
36791 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  }.  }..  /* Chec
36792 6b 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20 62 72  k for an open br
36793 61 63 6b 65 74 2e 20 2a 2f 0a 20 20 69 66 28 20  acket. */.  if( 
36794 73 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61  sqlite3_fts3_ena
36795 62 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20  ble_parentheses 
36796 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a 49 6e 70  ){.    if( *zInp
36797 75 74 3d 3d 27 28 27 20 29 7b 0a 20 20 20 20 20  ut=='(' ){.     
36798 20 69 6e 74 20 6e 43 6f 6e 73 75 6d 65 64 3b 0a   int nConsumed;.
36799 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20        int rc;.  
3679a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4e 65 73      pParse->nNes
3679b 74 2b 2b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  t++;.      rc = 
3679c 66 74 73 33 45 78 70 72 50 61 72 73 65 28 70 50  fts3ExprParse(pP
3679d 61 72 73 65 2c 20 26 7a 49 6e 70 75 74 5b 31 5d  arse, &zInput[1]
3679e 2c 20 6e 49 6e 70 75 74 2d 31 2c 20 70 70 45 78  , nInput-1, ppEx
3679f 70 72 2c 20 26 6e 43 6f 6e 73 75 6d 65 64 29 3b  pr, &nConsumed);
367a0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
367a1 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 2a 70 70  QLITE_OK && !*pp
367a2 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20  Expr ){.        
367a3 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45  rc = SQLITE_DONE
367a4 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
367a5 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28 69  *pnConsumed = (i
367a6 6e 74 29 28 28 7a 49 6e 70 75 74 20 2d 20 7a 29  nt)((zInput - z)
367a7 20 2b 20 31 20 2b 20 6e 43 6f 6e 73 75 6d 65 64   + 1 + nConsumed
367a8 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
367a9 72 63 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20  rc;.    }.  .   
367aa 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 20   /* Check for a 
367ab 63 6c 6f 73 65 20 62 72 61 63 6b 65 74 2e 20 2a  close bracket. *
367ac 2f 0a 20 20 20 20 69 66 28 20 2a 7a 49 6e 70 75  /.    if( *zInpu
367ad 74 3d 3d 27 29 27 20 29 7b 0a 20 20 20 20 20 20  t==')' ){.      
367ae 70 50 61 72 73 65 2d 3e 6e 4e 65 73 74 2d 2d 3b  pParse->nNest--;
367af 0a 20 20 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d  .      *pnConsum
367b0 65 64 20 3d 20 28 69 6e 74 29 28 28 7a 49 6e 70  ed = (int)((zInp
367b1 75 74 20 2d 20 7a 29 20 2b 20 31 29 3b 0a 20 20  ut - z) + 1);.  
367b2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
367b3 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20  E_DONE;.    }.  
367b4 7d 0a 0a 20 20 2f 2a 20 53 65 65 20 69 66 20 77  }..  /* See if w
367b5 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
367b6 74 68 20 61 20 71 75 6f 74 65 64 20 70 68 72 61  th a quoted phra
367b7 73 65 2e 20 49 66 20 74 68 69 73 20 69 73 20 74  se. If this is t
367b8 68 65 20 63 61 73 65 2c 20 74 68 65 6e 0a 20 20  he case, then.  
367b9 2a 2a 20 73 65 61 72 63 68 20 66 6f 72 20 74 68  ** search for th
367ba 65 20 63 6c 6f 73 69 6e 67 20 71 75 6f 74 65 20  e closing quote 
367bb 61 6e 64 20 70 61 73 73 20 74 68 65 20 77 68 6f  and pass the who
367bc 6c 65 20 73 74 72 69 6e 67 20 74 6f 20 67 65 74  le string to get
367bd 4e 65 78 74 53 74 72 69 6e 67 28 29 0a 20 20 2a  NextString().  *
367be 2a 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  * for processing
367bf 2e 20 54 68 69 73 20 69 73 20 65 61 73 79 20 74  . This is easy t
367c0 6f 20 64 6f 2c 20 61 73 20 66 74 73 33 20 68 61  o do, as fts3 ha
367c1 73 20 6e 6f 20 73 79 6e 74 61 78 20 66 6f 72 20  s no syntax for 
367c2 65 73 63 61 70 69 6e 67 0a 20 20 2a 2a 20 61 20  escaping.  ** a 
367c3 71 75 6f 74 65 20 63 68 61 72 61 63 74 65 72 20  quote character 
367c4 65 6d 62 65 64 64 65 64 20 69 6e 20 61 20 73 74  embedded in a st
367c5 72 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ring..  */.  if(
367c6 20 2a 7a 49 6e 70 75 74 3d 3d 27 22 27 20 29 7b   *zInput=='"' ){
367c7 0a 20 20 20 20 66 6f 72 28 69 69 3d 31 3b 20 69  .    for(ii=1; i
367c8 69 3c 6e 49 6e 70 75 74 20 26 26 20 7a 49 6e 70  i<nInput && zInp
367c9 75 74 5b 69 69 5d 21 3d 27 22 27 3b 20 69 69 2b  ut[ii]!='"'; ii+
367ca 2b 29 3b 0a 20 20 20 20 2a 70 6e 43 6f 6e 73 75  +);.    *pnConsu
367cb 6d 65 64 20 3d 20 28 69 6e 74 29 28 28 7a 49 6e  med = (int)((zIn
367cc 70 75 74 20 2d 20 7a 29 20 2b 20 69 69 20 2b 20  put - z) + ii + 
367cd 31 29 3b 0a 20 20 20 20 69 66 28 20 69 69 3d 3d  1);.    if( ii==
367ce 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 20 20  nInput ){.      
367cf 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
367d0 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ROR;.    }.    r
367d1 65 74 75 72 6e 20 67 65 74 4e 65 78 74 53 74 72  eturn getNextStr
367d2 69 6e 67 28 70 50 61 72 73 65 2c 20 26 7a 49 6e  ing(pParse, &zIn
367d3 70 75 74 5b 31 5d 2c 20 69 69 2d 31 2c 20 70 70  put[1], ii-1, pp
367d4 45 78 70 72 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f  Expr);.  }...  /
367d5 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f  * If control flo
367d6 77 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74  ws to this point
367d7 2c 20 74 68 69 73 20 6d 75 73 74 20 62 65 20 61  , this must be a
367d8 20 72 65 67 75 6c 61 72 20 74 6f 6b 65 6e 2c 20   regular token, 
367d9 6f 72 20 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64  or .  ** the end
367da 20 6f 66 20 74 68 65 20 69 6e 70 75 74 2e 20 52   of the input. R
367db 65 61 64 20 61 20 72 65 67 75 6c 61 72 20 74 6f  ead a regular to
367dc 6b 65 6e 20 75 73 69 6e 67 20 74 68 65 20 73 71  ken using the sq
367dd 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a  lite3_tokenizer.
367de 20 20 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20    ** interface. 
367df 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 73 6f 2c  Before doing so,
367e0 20 66 69 67 75 72 65 20 6f 75 74 20 69 66 20 74   figure out if t
367e1 68 65 72 65 20 69 73 20 61 6e 20 65 78 70 6c 69  here is an expli
367e2 63 69 74 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20  cit.  ** column 
367e3 73 70 65 63 69 66 69 65 72 20 66 6f 72 20 74 68  specifier for th
367e4 65 20 74 6f 6b 65 6e 2e 20 0a 20 20 2a 2a 0a 20  e token. .  **. 
367e5 20 2a 2a 20 54 4f 44 4f 3a 20 53 74 72 61 6e 67   ** TODO: Strang
367e6 65 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74 20 70  ely, it is not p
367e7 6f 73 73 69 62 6c 65 20 74 6f 20 61 73 73 6f 63  ossible to assoc
367e8 69 61 74 65 20 61 20 63 6f 6c 75 6d 6e 20 73 70  iate a column sp
367e9 65 63 69 66 69 65 72 0a 20 20 2a 2a 20 77 69 74  ecifier.  ** wit
367ea 68 20 61 20 71 75 6f 74 65 64 20 70 68 72 61 73  h a quoted phras
367eb 65 2c 20 6f 6e 6c 79 20 77 69 74 68 20 61 20 73  e, only with a s
367ec 69 6e 67 6c 65 20 74 6f 6b 65 6e 2e 20 4e 6f 74  ingle token. Not
367ed 20 73 75 72 65 20 69 66 20 74 68 69 73 20 77 61   sure if this wa
367ee 73 0a 20 20 2a 2a 20 61 6e 20 69 6d 70 6c 65 6d  s.  ** an implem
367ef 65 6e 74 61 74 69 6f 6e 20 61 72 74 69 66 61 63  entation artifac
367f0 74 20 6f 72 20 61 6e 20 69 6e 74 65 6e 74 69 6f  t or an intentio
367f1 6e 61 6c 20 64 65 63 69 73 69 6f 6e 20 77 68 65  nal decision whe
367f2 6e 20 66 74 73 33 20 77 61 73 0a 20 20 2a 2a 20  n fts3 was.  ** 
367f3 66 69 72 73 74 20 69 6d 70 6c 65 6d 65 6e 74 65  first implemente
367f4 64 2e 20 57 68 69 63 68 65 76 65 72 20 69 74 20  d. Whichever it 
367f5 77 61 73 2c 20 74 68 69 73 20 6d 6f 64 75 6c 65  was, this module
367f6 20 64 75 70 6c 69 63 61 74 65 73 20 74 68 65 20   duplicates the 
367f7 0a 20 20 2a 2a 20 6c 69 6d 69 74 61 74 69 6f 6e  .  ** limitation
367f8 2e 0a 20 20 2a 2f 0a 20 20 69 43 6f 6c 20 3d 20  ..  */.  iCol = 
367f9 70 50 61 72 73 65 2d 3e 69 44 65 66 61 75 6c 74  pParse->iDefault
367fa 43 6f 6c 3b 0a 20 20 69 43 6f 6c 4c 65 6e 20 3d  Col;.  iColLen =
367fb 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20   0;.  for(ii=0; 
367fc 69 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 3b  ii<pParse->nCol;
367fd 20 69 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73   ii++){.    cons
367fe 74 20 63 68 61 72 20 2a 7a 53 74 72 20 3d 20 70  t char *zStr = p
367ff 50 61 72 73 65 2d 3e 61 7a 43 6f 6c 5b 69 69 5d  Parse->azCol[ii]
36800 3b 0a 20 20 20 20 69 6e 74 20 6e 53 74 72 20 3d  ;.    int nStr =
36801 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 53 74   (int)strlen(zSt
36802 72 29 3b 0a 20 20 20 20 69 66 28 20 6e 49 6e 70  r);.    if( nInp
36803 75 74 3e 6e 53 74 72 20 26 26 20 7a 49 6e 70 75  ut>nStr && zInpu
36804 74 5b 6e 53 74 72 5d 3d 3d 27 3a 27 20 0a 20 20  t[nStr]==':' .  
36805 20 20 20 26 26 20 73 71 6c 69 74 65 33 5f 73 74     && sqlite3_st
36806 72 6e 69 63 6d 70 28 7a 53 74 72 2c 20 7a 49 6e  rnicmp(zStr, zIn
36807 70 75 74 2c 20 6e 53 74 72 29 3d 3d 30 20 0a 20  put, nStr)==0 . 
36808 20 20 20 29 7b 0a 20 20 20 20 20 20 69 43 6f 6c     ){.      iCol
36809 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 69 43 6f   = ii;.      iCo
3680a 6c 4c 65 6e 20 3d 20 28 69 6e 74 29 28 28 7a 49  lLen = (int)((zI
3680b 6e 70 75 74 20 2d 20 7a 29 20 2b 20 6e 53 74 72  nput - z) + nStr
3680c 20 2b 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65   + 1);.      bre
3680d 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
3680e 72 63 20 3d 20 67 65 74 4e 65 78 74 54 6f 6b 65  rc = getNextToke
3680f 6e 28 70 50 61 72 73 65 2c 20 69 43 6f 6c 2c 20  n(pParse, iCol, 
36810 26 7a 5b 69 43 6f 6c 4c 65 6e 5d 2c 20 6e 2d 69  &z[iColLen], n-i
36811 43 6f 6c 4c 65 6e 2c 20 70 70 45 78 70 72 2c 20  ColLen, ppExpr, 
36812 70 6e 43 6f 6e 73 75 6d 65 64 29 3b 0a 20 20 2a  pnConsumed);.  *
36813 70 6e 43 6f 6e 73 75 6d 65 64 20 2b 3d 20 69 43  pnConsumed += iC
36814 6f 6c 4c 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20  olLen;.  return 
36815 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
36816 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
36817 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74 75  Fts3Expr structu
36818 72 65 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20  re for a binary 
36819 6f 70 65 72 61 74 6f 72 20 28 61 6e 79 20 74 79  operator (any ty
3681a 70 65 0a 2a 2a 20 65 78 63 65 70 74 20 61 6e 20  pe.** except an 
3681b 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 29  FTSQUERY_PHRASE)
3681c 2e 20 52 65 74 75 72 6e 20 61 6e 20 69 6e 74 65  . Return an inte
3681d 67 65 72 20 76 61 6c 75 65 20 72 65 70 72 65 73  ger value repres
3681e 65 6e 74 69 6e 67 20 74 68 65 0a 2a 2a 20 70 72  enting the.** pr
3681f 65 63 65 64 65 6e 63 65 20 6f 66 20 74 68 65 20  ecedence of the 
36820 6f 70 65 72 61 74 6f 72 2e 20 4c 6f 77 65 72 20  operator. Lower 
36821 76 61 6c 75 65 73 20 68 61 76 65 20 61 20 68 69  values have a hi
36822 67 68 65 72 20 70 72 65 63 65 64 65 6e 63 65 20  gher precedence 
36823 28 69 2e 65 2e 0a 2a 2a 20 67 72 6f 75 70 20 6d  (i.e..** group m
36824 6f 72 65 20 74 69 67 68 74 6c 79 29 2e 20 46 6f  ore tightly). Fo
36825 72 20 65 78 61 6d 70 6c 65 2c 20 69 6e 20 74 68  r example, in th
36826 65 20 43 20 6c 61 6e 67 75 61 67 65 2c 20 74 68  e C language, th
36827 65 20 3d 3d 20 6f 70 65 72 61 74 6f 72 0a 2a 2a  e == operator.**
36828 20 67 72 6f 75 70 73 20 6d 6f 72 65 20 74 69 67   groups more tig
36829 68 74 6c 79 20 74 68 61 6e 20 7c 7c 2c 20 61 6e  htly than ||, an
3682a 64 20 77 6f 75 6c 64 20 74 68 65 72 65 66 6f 72  d would therefor
3682b 65 20 68 61 76 65 20 61 20 68 69 67 68 65 72 20  e have a higher 
3682c 70 72 65 63 65 64 65 6e 63 65 2e 0a 2a 2a 0a 2a  precedence..**.*
3682d 2a 20 57 68 65 6e 20 75 73 69 6e 67 20 74 68 65  * When using the
3682e 20 6e 65 77 20 66 74 73 33 20 71 75 65 72 79 20   new fts3 query 
3682f 73 79 6e 74 61 78 20 28 77 68 65 6e 20 53 51 4c  syntax (when SQL
36830 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 5f  ITE_ENABLE_FTS3_
36831 50 41 52 45 4e 54 48 45 53 49 53 0a 2a 2a 20 69  PARENTHESIS.** i
36832 73 20 64 65 66 69 6e 65 64 29 2c 20 74 68 65 20  s defined), the 
36833 6f 72 64 65 72 20 6f 66 20 74 68 65 20 6f 70 65  order of the ope
36834 72 61 74 6f 72 73 20 69 6e 20 70 72 65 63 65 64  rators in preced
36835 65 6e 63 65 20 66 72 6f 6d 20 68 69 67 68 65 73  ence from highes
36836 74 20 74 6f 0a 2a 2a 20 6c 6f 77 65 73 74 20 69  t to.** lowest i
36837 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4e 45 41 52 0a  s:.**.**   NEAR.
36838 2a 2a 20 20 20 4e 4f 54 0a 2a 2a 20 20 20 41 4e  **   NOT.**   AN
36839 44 20 28 69 6e 63 6c 75 64 69 6e 67 20 69 6d 70  D (including imp
3683a 6c 69 63 69 74 20 41 4e 44 73 29 0a 2a 2a 20 20  licit ANDs).**  
3683b 20 4f 52 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74   OR.**.** Note t
3683c 68 61 74 20 77 68 65 6e 20 75 73 69 6e 67 20 74  hat when using t
3683d 68 65 20 6f 6c 64 20 71 75 65 72 79 20 73 79 6e  he old query syn
3683e 74 61 78 2c 20 74 68 65 20 4f 52 20 6f 70 65 72  tax, the OR oper
3683f 61 74 6f 72 20 68 61 73 20 61 20 68 69 67 68 65  ator has a highe
36840 72 0a 2a 2a 20 70 72 65 63 65 64 65 6e 63 65 20  r.** precedence 
36841 74 68 61 6e 20 74 68 65 20 41 4e 44 20 6f 70 65  than the AND ope
36842 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  rator..*/.static
36843 20 69 6e 74 20 6f 70 50 72 65 63 65 64 65 6e 63   int opPrecedenc
36844 65 28 46 74 73 33 45 78 70 72 20 2a 70 29 7b 0a  e(Fts3Expr *p){.
36845 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 54 79    assert( p->eTy
36846 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe!=FTSQUERY_PHR
36847 41 53 45 20 29 3b 0a 20 20 69 66 28 20 73 71 6c  ASE );.  if( sql
36848 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65  ite3_fts3_enable
36849 5f 70 61 72 65 6e 74 68 65 73 65 73 20 29 7b 0a  _parentheses ){.
3684a 20 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 65 54      return p->eT
3684b 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ype;.  }else if(
3684c 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55   p->eType==FTSQU
3684d 45 52 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20  ERY_NEAR ){.    
3684e 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73  return 1;.  }els
3684f 65 20 69 66 28 20 70 2d 3e 65 54 79 70 65 3d 3d  e if( p->eType==
36850 46 54 53 51 55 45 52 59 5f 4f 52 20 29 7b 0a 20  FTSQUERY_OR ){. 
36851 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d     return 2;.  }
36852 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 54  .  assert( p->eT
36853 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 41 4e  ype==FTSQUERY_AN
36854 44 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 33 3b  D );.  return 3;
36855 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65  .}../*.** Argume
36856 6e 74 20 70 70 48 65 61 64 20 63 6f 6e 74 61 69  nt ppHead contai
36857 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ns a pointer to 
36858 74 68 65 20 63 75 72 72 65 6e 74 20 68 65 61 64  the current head
36859 20 6f 66 20 61 20 71 75 65 72 79 20 0a 2a 2a 20   of a query .** 
3685a 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
3685b 62 65 69 6e 67 20 70 61 72 73 65 64 2e 20 70 50  being parsed. pP
3685c 72 65 76 20 69 73 20 74 68 65 20 65 78 70 72 65  rev is the expre
3685d 73 73 69 6f 6e 20 6e 6f 64 65 20 6d 6f 73 74 20  ssion node most 
3685e 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 69 6e 73 65  recently.** inse
3685f 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72  rted into the tr
36860 65 65 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ee. This functio
36861 6e 20 61 64 64 73 20 70 4e 65 77 2c 20 77 68 69  n adds pNew, whi
36862 63 68 20 69 73 20 61 6c 77 61 79 73 20 61 20 62  ch is always a b
36863 69 6e 61 72 79 0a 2a 2a 20 6f 70 65 72 61 74 6f  inary.** operato
36864 72 20 6e 6f 64 65 2c 20 69 6e 74 6f 20 74 68 65  r node, into the
36865 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
36866 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 72 65   based on the re
36867 6c 61 74 69 76 65 20 70 72 65 63 65 64 65 6e 63  lative precedenc
36868 65 0a 2a 2a 20 6f 66 20 70 4e 65 77 20 61 6e 64  e.** of pNew and
36869 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 6e 6f   the existing no
3686a 64 65 73 20 6f 66 20 74 68 65 20 74 72 65 65 2e  des of the tree.
3686b 20 54 68 69 73 20 6d 61 79 20 72 65 73 75 6c 74   This may result
3686c 20 69 6e 20 74 68 65 20 68 65 61 64 0a 2a 2a 20   in the head.** 
3686d 6f 66 20 74 68 65 20 74 72 65 65 20 63 68 61 6e  of the tree chan
3686e 67 69 6e 67 2c 20 69 6e 20 77 68 69 63 68 20 63  ging, in which c
3686f 61 73 65 20 2a 70 70 48 65 61 64 20 69 73 20 73  ase *ppHead is s
36870 65 74 20 74 6f 20 74 68 65 20 6e 65 77 20 72 6f  et to the new ro
36871 6f 74 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74  ot node..*/.stat
36872 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 42 69  ic void insertBi
36873 6e 61 72 79 4f 70 65 72 61 74 6f 72 28 0a 20 20  naryOperator(.  
36874 46 74 73 33 45 78 70 72 20 2a 2a 70 70 48 65 61  Fts3Expr **ppHea
36875 64 2c 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  d,       /* Poin
36876 74 65 72 20 74 6f 20 74 68 65 20 72 6f 6f 74 20  ter to the root 
36877 6e 6f 64 65 20 6f 66 20 61 20 74 72 65 65 20 2a  node of a tree *
36878 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 50  /.  Fts3Expr *pP
36879 72 65 76 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rev,         /* 
3687a 4e 6f 64 65 20 6d 6f 73 74 20 72 65 63 65 6e 74  Node most recent
3687b 6c 79 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  ly inserted into
3687c 20 74 68 65 20 74 72 65 65 20 2a 2f 0a 20 20 46   the tree */.  F
3687d 74 73 33 45 78 70 72 20 2a 70 4e 65 77 20 20 20  ts3Expr *pNew   
3687e 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 62          /* New b
3687f 69 6e 61 72 79 20 6e 6f 64 65 20 74 6f 20 69 6e  inary node to in
36880 73 65 72 74 20 69 6e 74 6f 20 65 78 70 72 65 73  sert into expres
36881 73 69 6f 6e 20 74 72 65 65 20 2a 2f 0a 29 7b 0a  sion tree */.){.
36882 20 20 46 74 73 33 45 78 70 72 20 2a 70 53 70 6c    Fts3Expr *pSpl
36883 69 74 20 3d 20 70 50 72 65 76 3b 0a 20 20 77 68  it = pPrev;.  wh
36884 69 6c 65 28 20 70 53 70 6c 69 74 2d 3e 70 50 61  ile( pSplit->pPa
36885 72 65 6e 74 20 26 26 20 6f 70 50 72 65 63 65 64  rent && opPreced
36886 65 6e 63 65 28 70 53 70 6c 69 74 2d 3e 70 50 61  ence(pSplit->pPa
36887 72 65 6e 74 29 3c 3d 6f 70 50 72 65 63 65 64 65  rent)<=opPrecede
36888 6e 63 65 28 70 4e 65 77 29 20 29 7b 0a 20 20 20  nce(pNew) ){.   
36889 20 70 53 70 6c 69 74 20 3d 20 70 53 70 6c 69 74   pSplit = pSplit
3688a 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 0a  ->pParent;.  }..
3688b 20 20 69 66 28 20 70 53 70 6c 69 74 2d 3e 70 50    if( pSplit->pP
3688c 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 61 73 73  arent ){.    ass
3688d 65 72 74 28 20 70 53 70 6c 69 74 2d 3e 70 50 61  ert( pSplit->pPa
3688e 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70 53  rent->pRight==pS
3688f 70 6c 69 74 20 29 3b 0a 20 20 20 20 70 53 70 6c  plit );.    pSpl
36890 69 74 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52 69  it->pParent->pRi
36891 67 68 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  ght = pNew;.    
36892 70 4e 65 77 2d 3e 70 50 61 72 65 6e 74 20 3d 20  pNew->pParent = 
36893 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 3b  pSplit->pParent;
36894 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70  .  }else{.    *p
36895 70 48 65 61 64 20 3d 20 70 4e 65 77 3b 0a 20 20  pHead = pNew;.  
36896 7d 0a 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20  }.  pNew->pLeft 
36897 3d 20 70 53 70 6c 69 74 3b 0a 20 20 70 53 70 6c  = pSplit;.  pSpl
36898 69 74 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e  it->pParent = pN
36899 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72  ew;.}../*.** Par
3689a 73 65 20 74 68 65 20 66 74 73 33 20 71 75 65 72  se the fts3 quer
3689b 79 20 65 78 70 72 65 73 73 69 6f 6e 20 66 6f 75  y expression fou
3689c 6e 64 20 69 6e 20 62 75 66 66 65 72 20 7a 2c 20  nd in buffer z, 
3689d 6c 65 6e 67 74 68 20 6e 2e 20 54 68 69 73 20 66  length n. This f
3689e 75 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72  unction.** retur
3689f 6e 73 20 65 69 74 68 65 72 20 77 68 65 6e 20 74  ns either when t
368a0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 62 75  he end of the bu
368a1 66 66 65 72 20 69 73 20 72 65 61 63 68 65 64 20  ffer is reached 
368a2 6f 72 20 61 6e 20 75 6e 6d 61 74 63 68 65 64 20  or an unmatched 
368a3 0a 2a 2a 20 63 6c 6f 73 69 6e 67 20 62 72 61 63  .** closing brac
368a4 6b 65 74 20 2d 20 27 29 27 20 2d 20 69 73 20 65  ket - ')' - is e
368a5 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a  ncountered..**.*
368a6 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
368a7 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
368a8 74 75 72 6e 65 64 2c 20 2a 70 70 45 78 70 72 20  turned, *ppExpr 
368a9 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
368aa 74 6f 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 64  to the.** parsed
368ab 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 65 78 70   form of the exp
368ac 72 65 73 73 69 6f 6e 20 61 6e 64 20 2a 70 6e 43  ression and *pnC
368ad 6f 6e 73 75 6d 65 64 20 69 73 20 73 65 74 20 74  onsumed is set t
368ae 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  o the number of.
368af 2a 2a 20 62 79 74 65 73 20 72 65 61 64 20 66 72  ** bytes read fr
368b0 6f 6d 20 62 75 66 66 65 72 20 7a 2e 20 4f 74 68  om buffer z. Oth
368b1 65 72 77 69 73 65 2c 20 2a 70 70 45 78 70 72 20  erwise, *ppExpr 
368b2 69 73 20 73 65 74 20 74 6f 20 30 20 61 6e 64 20  is set to 0 and 
368b3 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 2a 2a 20  SQLITE_NOMEM.** 
368b4 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 20 65  (out of memory e
368b5 72 72 6f 72 29 20 6f 72 20 53 51 4c 49 54 45 5f  rror) or SQLITE_
368b6 45 52 52 4f 52 20 28 70 61 72 73 65 20 65 72 72  ERROR (parse err
368b7 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65 64 2e  or) is returned.
368b8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
368b9 74 73 33 45 78 70 72 50 61 72 73 65 28 0a 20 20  ts3ExprParse(.  
368ba 50 61 72 73 65 43 6f 6e 74 65 78 74 20 2a 70 50  ParseContext *pP
368bb 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
368bc 20 20 20 20 20 20 20 20 2f 2a 20 66 74 73 33 20          /* fts3 
368bd 71 75 65 72 79 20 70 61 72 73 65 20 63 6f 6e 74  query parse cont
368be 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ext */.  const c
368bf 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20  har *z, int n,  
368c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368c1 20 2f 2a 20 54 65 78 74 20 6f 66 20 4d 41 54 43   /* Text of MATC
368c2 48 20 71 75 65 72 79 20 2a 2f 0a 20 20 46 74 73  H query */.  Fts
368c3 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c 20  3Expr **ppExpr, 
368c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368c5 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72       /* OUT: Par
368c6 73 65 64 20 71 75 65 72 79 20 73 74 72 75 63 74  sed query struct
368c7 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ure */.  int *pn
368c8 43 6f 6e 73 75 6d 65 64 20 20 20 20 20 20 20 20  Consumed        
368c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
368ca 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20   /* OUT: Number 
368cb 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65  of bytes consume
368cc 64 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78  d */.){.  Fts3Ex
368cd 70 72 20 2a 70 52 65 74 20 3d 20 30 3b 0a 20 20  pr *pRet = 0;.  
368ce 46 74 73 33 45 78 70 72 20 2a 70 50 72 65 76 20  Fts3Expr *pPrev 
368cf 3d 20 30 3b 0a 20 20 46 74 73 33 45 78 70 72 20  = 0;.  Fts3Expr 
368d0 2a 70 4e 6f 74 42 72 61 6e 63 68 20 3d 20 30 3b  *pNotBranch = 0;
368d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
368d2 2a 20 4f 6e 6c 79 20 75 73 65 64 20 69 6e 20 6c  * Only used in l
368d3 65 67 61 63 79 20 70 61 72 73 65 20 6d 6f 64 65  egacy parse mode
368d4 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 20 3d 20   */.  int nIn = 
368d5 6e 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  n;.  const char 
368d6 2a 7a 49 6e 20 3d 20 7a 3b 0a 20 20 69 6e 74 20  *zIn = z;.  int 
368d7 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
368d8 20 20 69 6e 74 20 69 73 52 65 71 75 69 72 65 50    int isRequireP
368d9 68 72 61 73 65 20 3d 20 31 3b 0a 0a 20 20 77 68  hrase = 1;..  wh
368da 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
368db 4f 4b 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78  OK ){.    Fts3Ex
368dc 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20 20 20 69  pr *p = 0;.    i
368dd 6e 74 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20  nt nByte = 0;.  
368de 20 20 72 63 20 3d 20 67 65 74 4e 65 78 74 4e 6f    rc = getNextNo
368df 64 65 28 70 50 61 72 73 65 2c 20 7a 49 6e 2c 20  de(pParse, zIn, 
368e0 6e 49 6e 2c 20 26 70 2c 20 26 6e 42 79 74 65 29  nIn, &p, &nByte)
368e1 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
368e2 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
368e3 20 69 6e 74 20 69 73 50 68 72 61 73 65 3b 0a 0a   int isPhrase;..
368e4 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74        if( !sqlit
368e5 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70  e3_fts3_enable_p
368e6 61 72 65 6e 74 68 65 73 65 73 20 0a 20 20 20 20  arentheses .    
368e7 20 20 20 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d     && p->eType==
368e8 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20  FTSQUERY_PHRASE 
368e9 26 26 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69  && p->pPhrase->i
368ea 73 4e 6f 74 20 0a 20 20 20 20 20 20 29 7b 0a 20  sNot .      ){. 
368eb 20 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65         /* Create
368ec 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 4e 4f 54   an implicit NOT
368ed 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0a 20 20   operator. */.  
368ee 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a        Fts3Expr *
368ef 70 4e 6f 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  pNot = sqlite3_m
368f0 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73  alloc(sizeof(Fts
368f1 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20  3Expr));.       
368f2 20 69 66 28 20 21 70 4e 6f 74 20 29 7b 0a 20 20   if( !pNot ){.  
368f3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46          sqlite3F
368f4 74 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a  ts3ExprFree(p);.
368f5 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
368f6 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
368f7 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72         goto expr
368f8 70 61 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20  parse_out;.     
368f9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6d 65 6d     }.        mem
368fa 73 65 74 28 70 4e 6f 74 2c 20 30 2c 20 73 69 7a  set(pNot, 0, siz
368fb 65 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a  eof(Fts3Expr));.
368fc 20 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e 65 54          pNot->eT
368fd 79 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f 4e  ype = FTSQUERY_N
368fe 4f 54 3b 0a 20 20 20 20 20 20 20 20 70 4e 6f 74  OT;.        pNot
368ff 2d 3e 70 52 69 67 68 74 20 3d 20 70 3b 0a 20 20  ->pRight = p;.  
36900 20 20 20 20 20 20 69 66 28 20 70 4e 6f 74 42 72        if( pNotBr
36901 61 6e 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20  anch ){.        
36902 20 20 70 4e 6f 74 2d 3e 70 4c 65 66 74 20 3d 20    pNot->pLeft = 
36903 70 4e 6f 74 42 72 61 6e 63 68 3b 0a 20 20 20 20  pNotBranch;.    
36904 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4e      }.        pN
36905 6f 74 42 72 61 6e 63 68 20 3d 20 70 4e 6f 74 3b  otBranch = pNot;
36906 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70 50 72  .        p = pPr
36907 65 76 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ev;.      }else{
36908 0a 20 20 20 20 20 20 20 20 69 6e 74 20 65 54 79  .        int eTy
36909 70 65 20 3d 20 70 2d 3e 65 54 79 70 65 3b 0a 20  pe = p->eType;. 
3690a 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65         assert( e
3690b 54 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50  Type!=FTSQUERY_P
3690c 48 52 41 53 45 20 7c 7c 20 21 70 2d 3e 70 50 68  HRASE || !p->pPh
3690d 72 61 73 65 2d 3e 69 73 4e 6f 74 20 29 3b 0a 20  rase->isNot );. 
3690e 20 20 20 20 20 20 20 69 73 50 68 72 61 73 65 20         isPhrase 
3690f 3d 20 28 65 54 79 70 65 3d 3d 46 54 53 51 55 45  = (eType==FTSQUE
36910 52 59 5f 50 48 52 41 53 45 20 7c 7c 20 70 2d 3e  RY_PHRASE || p->
36911 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20  pLeft);..       
36912 20 2f 2a 20 54 68 65 20 69 73 52 65 71 75 69 72   /* The isRequir
36913 65 50 68 72 61 73 65 20 76 61 72 69 61 62 6c 65  ePhrase variable
36914 20 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20   is set to true 
36915 69 66 20 61 20 70 68 72 61 73 65 20 6f 72 0a 20  if a phrase or. 
36916 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 65 78 70         ** an exp
36917 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 65  ression containe
36918 64 20 69 6e 20 70 61 72 65 6e 74 68 65 73 69 73  d in parenthesis
36919 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 49 66   is required. If
3691a 20 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 69   a.        ** bi
3691b 6e 61 72 79 20 6f 70 65 72 61 74 6f 72 20 28 41  nary operator (A
3691c 4e 44 2c 20 4f 52 2c 20 4e 4f 54 20 6f 72 20 4e  ND, OR, NOT or N
3691d 45 41 52 29 20 69 73 20 65 6e 63 6f 75 6e 74 65  EAR) is encounte
3691e 64 20 77 68 65 6e 0a 20 20 20 20 20 20 20 20 2a  d when.        *
3691f 2a 20 69 73 52 65 71 75 69 72 65 50 68 72 61 73  * isRequirePhras
36920 65 20 69 73 20 73 65 74 2c 20 74 68 69 73 20 69  e is set, this i
36921 73 20 61 20 73 79 6e 74 61 78 20 65 72 72 6f 72  s a syntax error
36922 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
36923 20 20 20 20 20 69 66 28 20 21 69 73 50 68 72 61       if( !isPhra
36924 73 65 20 26 26 20 69 73 52 65 71 75 69 72 65 50  se && isRequireP
36925 68 72 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20  hrase ){.       
36926 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78     sqlite3Fts3Ex
36927 70 72 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20  prFree(p);.     
36928 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
36929 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 20 20  _ERROR;.        
3692a 20 20 67 6f 74 6f 20 65 78 70 72 70 61 72 73 65    goto exprparse
3692b 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _out;.        }.
3692c 20 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 69    .        if( i
3692d 73 50 68 72 61 73 65 20 26 26 20 21 69 73 52 65  sPhrase && !isRe
3692e 71 75 69 72 65 50 68 72 61 73 65 20 29 7b 0a 20  quirePhrase ){. 
3692f 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 73 65           /* Inse
36930 72 74 20 61 6e 20 69 6d 70 6c 69 63 69 74 20 41  rt an implicit A
36931 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 2a 2f 0a  ND operator. */.
36932 20 20 20 20 20 20 20 20 20 20 46 74 73 33 45 78            Fts3Ex
36933 70 72 20 2a 70 41 6e 64 3b 0a 20 20 20 20 20 20  pr *pAnd;.      
36934 20 20 20 20 61 73 73 65 72 74 28 20 70 52 65 74      assert( pRet
36935 20 26 26 20 70 50 72 65 76 20 29 3b 0a 20 20 20   && pPrev );.   
36936 20 20 20 20 20 20 20 70 41 6e 64 20 3d 20 73 71         pAnd = sq
36937 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
36938 65 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b 0a  eof(Fts3Expr));.
36939 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70            if( !p
3693a 41 6e 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  And ){.         
3693b 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 45 78     sqlite3Fts3Ex
3693c 70 72 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20  prFree(p);.     
3693d 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
3693e 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
3693f 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70        goto exprp
36940 61 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20  arse_out;.      
36941 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
36942 6d 65 6d 73 65 74 28 70 41 6e 64 2c 20 30 2c 20  memset(pAnd, 0, 
36943 73 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29  sizeof(Fts3Expr)
36944 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 41 6e  );.          pAn
36945 64 2d 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55  d->eType = FTSQU
36946 45 52 59 5f 41 4e 44 3b 0a 20 20 20 20 20 20 20  ERY_AND;.       
36947 20 20 20 69 6e 73 65 72 74 42 69 6e 61 72 79 4f     insertBinaryO
36948 70 65 72 61 74 6f 72 28 26 70 52 65 74 2c 20 70  perator(&pRet, p
36949 50 72 65 76 2c 20 70 41 6e 64 29 3b 0a 20 20 20  Prev, pAnd);.   
3694a 20 20 20 20 20 20 20 70 50 72 65 76 20 3d 20 70         pPrev = p
3694b 41 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a  And;.        }..
3694c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
3694d 74 65 73 74 20 63 61 74 63 68 65 73 20 61 74 74  test catches att
3694e 65 6d 70 74 73 20 74 6f 20 6d 61 6b 65 20 65 69  empts to make ei
3694f 74 68 65 72 20 6f 70 65 72 61 6e 64 20 6f 66 20  ther operand of 
36950 61 20 4e 45 41 52 0a 20 20 20 20 20 20 20 20 2a  a NEAR.        *
36951 2a 20 6f 70 65 72 61 74 6f 72 20 73 6f 6d 65 74  * operator somet
36952 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20  hing other than 
36953 61 20 70 68 72 61 73 65 2e 20 46 6f 72 20 65 78  a phrase. For ex
36954 61 6d 70 6c 65 2c 20 65 69 74 68 65 72 20 6f 66  ample, either of
36955 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
36956 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20 20  following:.     
36957 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
36958 20 20 20 20 28 62 72 61 63 6b 65 74 65 64 20 65      (bracketed e
36959 78 70 72 65 73 73 69 6f 6e 29 20 4e 45 41 52 20  xpression) NEAR 
3695a 70 68 72 61 73 65 0a 20 20 20 20 20 20 20 20 2a  phrase.        *
3695b 2a 20 20 20 20 70 68 72 61 73 65 20 4e 45 41 52  *    phrase NEAR
3695c 20 28 62 72 61 63 6b 65 74 65 64 20 65 78 70 72   (bracketed expr
3695d 65 73 73 69 6f 6e 29 0a 20 20 20 20 20 20 20 20  ession).        
3695e 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 52 65  **.        ** Re
3695f 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 69 6e  turn an error in
36960 20 65 69 74 68 65 72 20 63 61 73 65 2e 0a 20 20   either case..  
36961 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
36962 20 69 66 28 20 70 50 72 65 76 20 26 26 20 28 0a   if( pPrev && (.
36963 20 20 20 20 20 20 20 20 20 20 20 20 28 65 54 79              (eTy
36964 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41  pe==FTSQUERY_NEA
36965 52 20 26 26 20 21 69 73 50 68 72 61 73 65 20 26  R && !isPhrase &
36966 26 20 70 50 72 65 76 2d 3e 65 54 79 70 65 21 3d  & pPrev->eType!=
36967 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 29  FTSQUERY_PHRASE)
36968 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28 65 54  .         || (eT
36969 79 70 65 21 3d 46 54 53 51 55 45 52 59 5f 50 48  ype!=FTSQUERY_PH
3696a 52 41 53 45 20 26 26 20 69 73 50 68 72 61 73 65  RASE && isPhrase
3696b 20 26 26 20 70 50 72 65 76 2d 3e 65 54 79 70 65   && pPrev->eType
3696c 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 29  ==FTSQUERY_NEAR)
3696d 0a 20 20 20 20 20 20 20 20 29 29 7b 0a 20 20 20  .        )){.   
3696e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 46 74         sqlite3Ft
3696f 73 33 45 78 70 72 46 72 65 65 28 70 29 3b 0a 20  s3ExprFree(p);. 
36970 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
36971 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
36972 20 20 20 20 20 20 67 6f 74 6f 20 65 78 70 72 70        goto exprp
36973 61 72 73 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20  arse_out;.      
36974 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 69    }.  .        i
36975 66 28 20 69 73 50 68 72 61 73 65 20 29 7b 0a 20  f( isPhrase ){. 
36976 20 20 20 20 20 20 20 20 20 69 66 28 20 70 52 65           if( pRe
36977 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
36978 20 61 73 73 65 72 74 28 20 70 50 72 65 76 20 26   assert( pPrev &
36979 26 20 70 50 72 65 76 2d 3e 70 4c 65 66 74 20 26  & pPrev->pLeft &
3697a 26 20 70 50 72 65 76 2d 3e 70 52 69 67 68 74 3d  & pPrev->pRight=
3697b 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
3697c 20 20 70 50 72 65 76 2d 3e 70 52 69 67 68 74 20    pPrev->pRight 
3697d 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = p;.           
3697e 20 70 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50   p->pParent = pP
3697f 72 65 76 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  rev;.          }
36980 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
36981 20 20 70 52 65 74 20 3d 20 70 3b 0a 20 20 20 20    pRet = p;.    
36982 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
36983 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
36984 20 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70 65   insertBinaryOpe
36985 72 61 74 6f 72 28 26 70 52 65 74 2c 20 70 50 72  rator(&pRet, pPr
36986 65 76 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20  ev, p);.        
36987 7d 0a 20 20 20 20 20 20 20 20 69 73 52 65 71 75  }.        isRequ
36988 69 72 65 50 68 72 61 73 65 20 3d 20 21 69 73 50  irePhrase = !isP
36989 68 72 61 73 65 3b 0a 20 20 20 20 20 20 7d 0a 20  hrase;.      }. 
3698a 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 42 79       assert( nBy
3698b 74 65 3e 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20  te>0 );.    }.  
3698c 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51    assert( rc!=SQ
3698d 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 6e 42 79 74  LITE_OK || (nByt
3698e 65 3e 30 20 26 26 20 6e 42 79 74 65 3c 3d 6e 49  e>0 && nByte<=nI
3698f 6e 29 20 29 3b 0a 20 20 20 20 6e 49 6e 20 2d 3d  n) );.    nIn -=
36990 20 6e 42 79 74 65 3b 0a 20 20 20 20 7a 49 6e 20   nByte;.    zIn 
36991 2b 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 50  += nByte;.    pP
36992 72 65 76 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20  rev = p;.  }..  
36993 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
36994 4f 4e 45 20 26 26 20 70 52 65 74 20 26 26 20 69  ONE && pRet && i
36995 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20 29  sRequirePhrase )
36996 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
36997 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20  E_ERROR;.  }..  
36998 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
36999 4f 4e 45 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ONE ){.    rc = 
3699a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69  SQLITE_OK;.    i
3699b 66 28 20 21 73 71 6c 69 74 65 33 5f 66 74 73 33  f( !sqlite3_fts3
3699c 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65  _enable_parenthe
3699d 73 65 73 20 26 26 20 70 4e 6f 74 42 72 61 6e 63  ses && pNotBranc
3699e 68 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  h ){.      if( !
3699f 70 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20  pRet ){.        
369a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
369a1 52 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  R;.      }else{.
369a2 20 20 20 20 20 20 20 20 46 74 73 33 45 78 70 72          Fts3Expr
369a3 20 2a 70 49 74 65 72 20 3d 20 70 4e 6f 74 42 72   *pIter = pNotBr
369a4 61 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 77 68  anch;.        wh
369a5 69 6c 65 28 20 70 49 74 65 72 2d 3e 70 4c 65 66  ile( pIter->pLef
369a6 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  t ){.          p
369a7 49 74 65 72 20 3d 20 70 49 74 65 72 2d 3e 70 4c  Iter = pIter->pL
369a8 65 66 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eft;.        }. 
369a9 20 20 20 20 20 20 20 70 49 74 65 72 2d 3e 70 4c         pIter->pL
369aa 65 66 74 20 3d 20 70 52 65 74 3b 0a 20 20 20 20  eft = pRet;.    
369ab 20 20 20 20 70 52 65 74 20 3d 20 70 4e 6f 74 42      pRet = pNotB
369ac 72 61 6e 63 68 3b 0a 20 20 20 20 20 20 7d 0a 20  ranch;.      }. 
369ad 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 6e 43 6f     }.  }.  *pnCo
369ae 6e 73 75 6d 65 64 20 3d 20 6e 20 2d 20 6e 49 6e  nsumed = n - nIn
369af 3b 0a 0a 65 78 70 72 70 61 72 73 65 5f 6f 75 74  ;..exprparse_out
369b0 3a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  :.  if( rc!=SQLI
369b1 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
369b2 69 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65  ite3Fts3ExprFree
369b3 28 70 52 65 74 29 3b 0a 20 20 20 20 73 71 6c 69  (pRet);.    sqli
369b4 74 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28  te3Fts3ExprFree(
369b5 70 4e 6f 74 42 72 61 6e 63 68 29 3b 0a 20 20 20  pNotBranch);.   
369b6 20 70 52 65 74 20 3d 20 30 3b 0a 20 20 7d 0a 20   pRet = 0;.  }. 
369b7 20 2a 70 70 45 78 70 72 20 3d 20 70 52 65 74 3b   *ppExpr = pRet;
369b8 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
369b9 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  ./*.** Parameter
369ba 73 20 7a 20 61 6e 64 20 6e 20 63 6f 6e 74 61 69  s z and n contai
369bb 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  n a pointer to a
369bc 6e 64 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 62  nd length of a b
369bd 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
369be 0a 2a 2a 20 61 6e 20 66 74 73 33 20 71 75 65 72  .** an fts3 quer
369bf 79 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 72 65  y expression, re
369c0 73 70 65 63 74 69 76 65 6c 79 2e 20 54 68 69 73  spectively. This
369c1 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70   function attemp
369c2 74 73 20 74 6f 20 70 61 72 73 65 20 74 68 65 0a  ts to parse the.
369c3 2a 2a 20 71 75 65 72 79 20 65 78 70 72 65 73 73  ** query express
369c4 69 6f 6e 20 61 6e 64 20 63 72 65 61 74 65 20 61  ion and create a
369c5 20 74 72 65 65 20 6f 66 20 46 74 73 33 45 78 70   tree of Fts3Exp
369c6 72 20 73 74 72 75 63 74 75 72 65 73 20 72 65 70  r structures rep
369c7 72 65 73 65 6e 74 69 6e 67 20 74 68 65 0a 2a 2a  resenting the.**
369c8 20 70 61 72 73 65 64 20 65 78 70 72 65 73 73 69   parsed expressi
369c9 6f 6e 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  on. If successfu
369ca 6c 2c 20 2a 70 70 45 78 70 72 20 69 73 20 73 65  l, *ppExpr is se
369cb 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  t to point to th
369cc 65 20 68 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65  e head.** of the
369cd 20 70 61 72 73 65 64 20 65 78 70 72 65 73 73 69   parsed expressi
369ce 6f 6e 20 74 72 65 65 20 61 6e 64 20 53 51 4c 49  on tree and SQLI
369cf 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
369d0 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 0a 2a  d. If an error.*
369d1 2a 20 6f 63 63 75 72 73 2c 20 65 69 74 68 65 72  * occurs, either
369d2 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 28 6f   SQLITE_NOMEM (o
369d3 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72  ut-of-memory err
369d4 6f 72 29 20 6f 72 20 53 51 4c 49 54 45 5f 45 52  or) or SQLITE_ER
369d5 52 4f 52 20 28 70 61 72 73 65 0a 2a 2a 20 65 72  ROR (parse.** er
369d6 72 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65 64  ror) is returned
369d7 20 61 6e 64 20 2a 70 70 45 78 70 72 20 69 73 20   and *ppExpr is 
369d8 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20  set to 0..**.** 
369d9 49 66 20 70 61 72 61 6d 65 74 65 72 20 6e 20 69  If parameter n i
369da 73 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  s a negative num
369db 62 65 72 2c 20 74 68 65 6e 20 7a 20 69 73 20 61  ber, then z is a
369dc 73 73 75 6d 65 64 20 74 6f 20 70 6f 69 6e 74 20  ssumed to point 
369dd 74 6f 20 61 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d  to a.** nul-term
369de 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 61 6e  inated string an
369df 64 20 74 68 65 20 6c 65 6e 67 74 68 20 69 73 20  d the length is 
369e0 64 65 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67  determined using
369e1 20 73 74 72 6c 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a   strlen()..**.**
369e2 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   The first param
369e3 65 74 65 72 2c 20 70 54 6f 6b 65 6e 69 7a 65 72  eter, pTokenizer
369e4 2c 20 69 73 20 70 61 73 73 65 64 20 74 68 65 20  , is passed the 
369e5 66 74 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20 6d  fts3 tokenizer m
369e6 6f 64 75 6c 65 20 74 6f 0a 2a 2a 20 75 73 65 20  odule to.** use 
369e7 74 6f 20 6e 6f 72 6d 61 6c 69 7a 65 20 71 75 65  to normalize que
369e8 72 79 20 74 6f 6b 65 6e 73 20 77 68 69 6c 65 20  ry tokens while 
369e9 70 61 72 73 69 6e 67 20 74 68 65 20 65 78 70 72  parsing the expr
369ea 65 73 73 69 6f 6e 2e 20 54 68 65 20 61 7a 43 6f  ession. The azCo
369eb 6c 5b 5d 0a 2a 2a 20 61 72 72 61 79 2c 20 77 68  l[].** array, wh
369ec 69 63 68 20 69 73 20 61 73 73 75 6d 65 64 20 74  ich is assumed t
369ed 6f 20 63 6f 6e 74 61 69 6e 20 6e 43 6f 6c 20 65  o contain nCol e
369ee 6e 74 72 69 65 73 2c 20 73 68 6f 75 6c 64 20 63  ntries, should c
369ef 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73  ontain the names
369f0 0a 2a 2a 20 6f 66 20 65 61 63 68 20 63 6f 6c 75  .** of each colu
369f1 6d 6e 20 69 6e 20 74 68 65 20 74 61 72 67 65 74  mn in the target
369f2 20 66 74 73 33 20 74 61 62 6c 65 2c 20 69 6e 20   fts3 table, in 
369f3 6f 72 64 65 72 20 66 72 6f 6d 20 6c 65 66 74 20  order from left 
369f4 74 6f 20 72 69 67 68 74 2e 20 0a 2a 2a 20 43 6f  to right. .** Co
369f5 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 75 73 74 20  lumn names must 
369f6 62 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65  be nul-terminate
369f7 64 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a  d strings..**.**
369f8 20 54 68 65 20 69 44 65 66 61 75 6c 74 43 6f 6c   The iDefaultCol
369f9 20 70 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c   parameter shoul
369fa 64 20 62 65 20 70 61 73 73 65 64 20 74 68 65 20  d be passed the 
369fb 69 6e 64 65 78 20 6f 66 20 74 68 65 20 74 61 62  index of the tab
369fc 6c 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 68 61  le column.** tha
369fd 74 20 61 70 70 65 61 72 73 20 6f 6e 20 74 68 65  t appears on the
369fe 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64 65 20   left-hand-side 
369ff 6f 66 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65  of the MATCH ope
36a00 72 61 74 6f 72 20 28 74 68 65 20 64 65 66 61 75  rator (the defau
36a01 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20  lt.** column to 
36a02 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20 66 6f  match against fo
36a03 72 20 74 6f 6b 65 6e 73 20 66 6f 72 20 77 68 69  r tokens for whi
36a04 63 68 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  ch a column name
36a05 20 69 73 20 6e 6f 74 20 65 78 70 6c 69 63 69 74   is not explicit
36a06 6c 79 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20  ly.** specified 
36a07 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 71  as part of the q
36a08 75 65 72 79 20 73 74 72 69 6e 67 29 2c 20 6f 72  uery string), or
36a09 20 2d 31 20 69 66 20 74 6f 6b 65 6e 73 20 6d 61   -1 if tokens ma
36a0a 79 20 62 79 20 64 65 66 61 75 6c 74 0a 2a 2a 20  y by default.** 
36a0b 6d 61 74 63 68 20 61 6e 79 20 74 61 62 6c 65 20  match any table 
36a0c 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  column..*/.SQLIT
36a0d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
36a0e 6c 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72  lite3Fts3ExprPar
36a0f 73 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  se(.  sqlite3_to
36a10 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69  kenizer *pTokeni
36a11 7a 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 6f 6b  zer,      /* Tok
36a12 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f  enizer module */
36a13 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 2c  .  char **azCol,
36a14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36a15 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
36a16 6f 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  of column names 
36a17 66 6f 72 20 66 74 73 33 20 74 61 62 6c 65 20 2a  for fts3 table *
36a18 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 2c 20 20 20  /.  int nCol,   
36a19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36a1a 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
36a1b 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
36a1c 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74  azCol[] */.  int
36a1d 20 69 44 65 66 61 75 6c 74 43 6f 6c 2c 20 20 20   iDefaultCol,   
36a1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36a1f 20 2f 2a 20 44 65 66 61 75 6c 74 20 63 6f 6c 75   /* Default colu
36a20 6d 6e 20 74 6f 20 71 75 65 72 79 20 2a 2f 0a 20  mn to query */. 
36a21 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
36a22 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20  int n,          
36a23 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20       /* Text of 
36a24 4d 41 54 43 48 20 71 75 65 72 79 20 2a 2f 0a 20  MATCH query */. 
36a25 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78   Fts3Expr **ppEx
36a26 70 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  pr              
36a27 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 61 72       /* OUT: Par
36a28 73 65 64 20 71 75 65 72 79 20 73 74 72 75 63 74  sed query struct
36a29 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ure */.){.  int 
36a2a 6e 50 61 72 73 65 64 3b 0a 20 20 69 6e 74 20 72  nParsed;.  int r
36a2b 63 3b 0a 20 20 50 61 72 73 65 43 6f 6e 74 65 78  c;.  ParseContex
36a2c 74 20 73 50 61 72 73 65 3b 0a 20 20 73 50 61 72  t sParse;.  sPar
36a2d 73 65 2e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  se.pTokenizer = 
36a2e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 50  pTokenizer;.  sP
36a2f 61 72 73 65 2e 61 7a 43 6f 6c 20 3d 20 28 63 6f  arse.azCol = (co
36a30 6e 73 74 20 63 68 61 72 20 2a 2a 29 61 7a 43 6f  nst char **)azCo
36a31 6c 3b 0a 20 20 73 50 61 72 73 65 2e 6e 43 6f 6c  l;.  sParse.nCol
36a32 20 3d 20 6e 43 6f 6c 3b 0a 20 20 73 50 61 72 73   = nCol;.  sPars
36a33 65 2e 69 44 65 66 61 75 6c 74 43 6f 6c 20 3d 20  e.iDefaultCol = 
36a34 69 44 65 66 61 75 6c 74 43 6f 6c 3b 0a 20 20 73  iDefaultCol;.  s
36a35 50 61 72 73 65 2e 6e 4e 65 73 74 20 3d 20 30 3b  Parse.nNest = 0;
36a36 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20  .  if( z==0 ){. 
36a37 20 20 20 2a 70 70 45 78 70 72 20 3d 20 30 3b 0a     *ppExpr = 0;.
36a38 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
36a39 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  E_OK;.  }.  if( 
36a3a 6e 3c 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 28  n<0 ){.    n = (
36a3b 69 6e 74 29 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  int)strlen(z);. 
36a3c 20 7d 0a 20 20 72 63 20 3d 20 66 74 73 33 45 78   }.  rc = fts3Ex
36a3d 70 72 50 61 72 73 65 28 26 73 50 61 72 73 65 2c  prParse(&sParse,
36a3e 20 7a 2c 20 6e 2c 20 70 70 45 78 70 72 2c 20 26   z, n, ppExpr, &
36a3f 6e 50 61 72 73 65 64 29 3b 0a 0a 20 20 2f 2a 20  nParsed);..  /* 
36a40 43 68 65 63 6b 20 66 6f 72 20 6d 69 73 6d 61 74  Check for mismat
36a41 63 68 65 64 20 70 61 72 65 6e 74 68 65 73 69 73  ched parenthesis
36a42 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
36a43 4c 49 54 45 5f 4f 4b 20 26 26 20 73 50 61 72 73  LITE_OK && sPars
36a44 65 2e 6e 4e 65 73 74 20 29 7b 0a 20 20 20 20 72  e.nNest ){.    r
36a45 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
36a46 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  ;.    sqlite3Fts
36a47 33 45 78 70 72 46 72 65 65 28 2a 70 70 45 78 70  3ExprFree(*ppExp
36a48 72 29 3b 0a 20 20 20 20 2a 70 70 45 78 70 72 20  r);.    *ppExpr 
36a49 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  = 0;.  }..  retu
36a4a 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
36a4b 46 72 65 65 20 61 20 70 61 72 73 65 64 20 66 74  Free a parsed ft
36a4c 73 33 20 71 75 65 72 79 20 65 78 70 72 65 73 73  s3 query express
36a4d 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  ion allocated by
36a4e 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
36a4f 50 61 72 73 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49  Parse()..*/.SQLI
36a50 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
36a51 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46  sqlite3Fts3ExprF
36a52 72 65 65 28 46 74 73 33 45 78 70 72 20 2a 70 29  ree(Fts3Expr *p)
36a53 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
36a54 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
36a55 46 72 65 65 28 70 2d 3e 70 4c 65 66 74 29 3b 0a  Free(p->pLeft);.
36a56 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 45      sqlite3Fts3E
36a57 78 70 72 46 72 65 65 28 70 2d 3e 70 52 69 67 68  xprFree(p->pRigh
36a58 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  t);.    sqlite3_
36a59 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  free(p);.  }.}..
36a5a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
36a5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36a5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36a5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36a5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
36a5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36a61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36a62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36a63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 45  ***********.** E
36a64 76 65 72 79 74 68 69 6e 67 20 61 66 74 65 72 20  verything after 
36a65 74 68 69 73 20 70 6f 69 6e 74 20 69 73 20 6a 75  this point is ju
36a66 73 74 20 74 65 73 74 20 63 6f 64 65 2e 0a 2a 2f  st test code..*/
36a67 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
36a68 54 45 53 54 0a 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e  TEST.../*.** Fun
36a69 63 74 69 6f 6e 20 74 6f 20 71 75 65 72 79 20 74  ction to query t
36a6a 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20 6f 66  he hash-table of
36a6b 20 74 6f 6b 65 6e 69 7a 65 72 73 20 28 73 65 65   tokenizers (see
36a6c 20 52 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65   README.tokenize
36a6d 72 73 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  rs)..*/.static i
36a6e 6e 74 20 71 75 65 72 79 54 65 73 74 54 6f 6b 65  nt queryTestToke
36a6f 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33  nizer(.  sqlite3
36a70 20 2a 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63   *db, .  const c
36a71 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 0a 20 20  har *zName,  .  
36a72 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
36a73 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
36a74 2a 70 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  *pp.){.  int rc;
36a75 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
36a76 2a 70 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20  *pStmt;.  const 
36a77 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53  char zSql[] = "S
36a78 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e  ELECT fts3_token
36a79 69 7a 65 72 28 3f 29 22 3b 0a 0a 20 20 2a 70 70  izer(?)";..  *pp
36a7a 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c   = 0;.  rc = sql
36a7b 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
36a7c 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70  db, zSql, -1, &p
36a7d 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  Stmt, 0);.  if( 
36a7e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
36a7f 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
36a80 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62    }..  sqlite3_b
36a81 69 6e 64 5f 74 65 78 74 28 70 53 74 6d 74 2c 20  ind_text(pStmt, 
36a82 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51  1, zName, -1, SQ
36a83 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
36a84 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d  if( SQLITE_ROW==
36a85 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74  sqlite3_step(pSt
36a86 6d 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 73  mt) ){.    if( s
36a87 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
36a88 70 65 28 70 53 74 6d 74 2c 20 30 29 3d 3d 53 51  pe(pStmt, 0)==SQ
36a89 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20  LITE_BLOB ){.   
36a8a 20 20 20 6d 65 6d 63 70 79 28 28 76 6f 69 64 20     memcpy((void 
36a8b 2a 29 70 70 2c 20 73 71 6c 69 74 65 33 5f 63 6f  *)pp, sqlite3_co
36a8c 6c 75 6d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c  lumn_blob(pStmt,
36a8d 20 30 29 2c 20 73 69 7a 65 6f 66 28 2a 70 70 29   0), sizeof(*pp)
36a8e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
36a8f 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66  return sqlite3_f
36a90 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a  inalize(pStmt);.
36a91 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
36a92 6e 63 74 69 6f 6e 20 69 73 20 70 61 72 74 20 6f  nction is part o
36a93 66 20 74 68 65 20 74 65 73 74 20 69 6e 74 65 72  f the test inter
36a94 66 61 63 65 20 66 6f 72 20 74 68 65 20 71 75 65  face for the que
36a95 72 79 20 70 61 72 73 65 72 2e 20 49 74 0a 2a 2a  ry parser. It.**
36a96 20 77 72 69 74 65 73 20 61 20 74 65 78 74 20 72   writes a text r
36a97 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
36a98 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72 65   the query expre
36a99 73 73 69 6f 6e 20 70 45 78 70 72 20 69 6e 74 6f  ssion pExpr into
36a9a 20 74 68 65 0a 2a 2a 20 62 75 66 66 65 72 20 70   the.** buffer p
36a9b 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 61 72 67  ointed to by arg
36a9c 75 6d 65 6e 74 20 7a 42 75 66 2e 20 49 74 20 69  ument zBuf. It i
36a9d 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 7a  s assumed that z
36a9e 42 75 66 20 69 73 20 6c 61 72 67 65 20 0a 2a 2a  Buf is large .**
36a9f 20 65 6e 6f 75 67 68 20 74 6f 20 73 74 6f 72 65   enough to store
36aa0 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74 65   the required te
36aa1 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  xt representatio
36aa2 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
36aa3 64 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28 46  d exprToString(F
36aa4 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20  ts3Expr *pExpr, 
36aa5 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73  char *zBuf){.  s
36aa6 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65 54  witch( pExpr->eT
36aa7 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20  ype ){.    case 
36aa8 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 3a  FTSQUERY_PHRASE:
36aa9 20 7b 0a 20 20 20 20 20 20 46 74 73 33 50 68 72   {.      Fts3Phr
36aaa 61 73 65 20 2a 70 50 68 72 61 73 65 20 3d 20 70  ase *pPhrase = p
36aab 45 78 70 72 2d 3e 70 50 68 72 61 73 65 3b 0a 20  Expr->pPhrase;. 
36aac 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
36aad 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74    zBuf += sprint
36aae 66 28 7a 42 75 66 2c 20 22 50 48 52 41 53 45 20  f(zBuf, "PHRASE 
36aaf 25 64 20 25 64 22 2c 20 70 50 68 72 61 73 65 2d  %d %d", pPhrase-
36ab0 3e 69 43 6f 6c 75 6d 6e 2c 20 70 50 68 72 61 73  >iColumn, pPhras
36ab1 65 2d 3e 69 73 4e 6f 74 29 3b 0a 20 20 20 20 20  e->isNot);.     
36ab2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 68 72   for(i=0; i<pPhr
36ab3 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 69 2b 2b  ase->nToken; i++
36ab4 29 7b 0a 20 20 20 20 20 20 20 20 7a 42 75 66 20  ){.        zBuf 
36ab5 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c  += sprintf(zBuf,
36ab6 22 20 25 2e 2a 73 22 2c 70 50 68 72 61 73 65 2d  " %.*s",pPhrase-
36ab7 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 6e 2c 70 50 68  >aToken[i].n,pPh
36ab8 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 5d 2e  rase->aToken[i].
36ab9 7a 29 3b 0a 20 20 20 20 20 20 20 20 7a 42 75 66  z);.        zBuf
36aba 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66   += sprintf(zBuf
36abb 2c 22 25 73 22 2c 20 28 70 50 68 72 61 73 65 2d  ,"%s", (pPhrase-
36abc 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 69 73 50 72 65  >aToken[i].isPre
36abd 66 69 78 3f 22 2b 22 3a 22 22 29 29 3b 0a 20 20  fix?"+":""));.  
36abe 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
36abf 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63  rn;.    }..    c
36ac0 61 73 65 20 46 54 53 51 55 45 52 59 5f 4e 45 41  ase FTSQUERY_NEA
36ac1 52 3a 0a 20 20 20 20 20 20 7a 42 75 66 20 2b 3d  R:.      zBuf +=
36ac2 20 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22   sprintf(zBuf, "
36ac3 4e 45 41 52 2f 25 64 20 22 2c 20 70 45 78 70 72  NEAR/%d ", pExpr
36ac4 2d 3e 6e 4e 65 61 72 29 3b 0a 20 20 20 20 20 20  ->nNear);.      
36ac5 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
36ac6 46 54 53 51 55 45 52 59 5f 4e 4f 54 3a 0a 20 20  FTSQUERY_NOT:.  
36ac7 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69      zBuf += spri
36ac8 6e 74 66 28 7a 42 75 66 2c 20 22 4e 4f 54 20 22  ntf(zBuf, "NOT "
36ac9 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
36aca 20 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52      case FTSQUER
36acb 59 5f 41 4e 44 3a 0a 20 20 20 20 20 20 7a 42 75  Y_AND:.      zBu
36acc 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75  f += sprintf(zBu
36acd 66 2c 20 22 41 4e 44 20 22 29 3b 0a 20 20 20 20  f, "AND ");.    
36ace 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
36acf 65 20 46 54 53 51 55 45 52 59 5f 4f 52 3a 0a 20  e FTSQUERY_OR:. 
36ad0 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72       zBuf += spr
36ad1 69 6e 74 66 28 7a 42 75 66 2c 20 22 4f 52 20 22  intf(zBuf, "OR "
36ad2 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
36ad3 20 20 7d 0a 0a 20 20 7a 42 75 66 20 2b 3d 20 73    }..  zBuf += s
36ad4 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 7b 22  printf(zBuf, "{"
36ad5 29 3b 0a 20 20 65 78 70 72 54 6f 53 74 72 69 6e  );.  exprToStrin
36ad6 67 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  g(pExpr->pLeft, 
36ad7 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d  zBuf);.  zBuf +=
36ad8 20 73 74 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20   strlen(zBuf);. 
36ad9 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66   zBuf += sprintf
36ada 28 7a 42 75 66 2c 20 22 7d 20 22 29 3b 0a 0a 20  (zBuf, "} ");.. 
36adb 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66   zBuf += sprintf
36adc 28 7a 42 75 66 2c 20 22 7b 22 29 3b 0a 20 20 65  (zBuf, "{");.  e
36add 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45 78 70  xprToString(pExp
36ade 72 2d 3e 70 52 69 67 68 74 2c 20 7a 42 75 66 29  r->pRight, zBuf)
36adf 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 74 72 6c  ;.  zBuf += strl
36ae0 65 6e 28 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66  en(zBuf);.  zBuf
36ae1 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66   += sprintf(zBuf
36ae2 2c 20 22 7d 22 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  , "}");.}../*.**
36ae3 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d 70   This is the imp
36ae4 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
36ae5 20 73 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63   scalar SQL func
36ae6 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65 73  tion used to tes
36ae7 74 20 74 68 65 20 0a 2a 2a 20 65 78 70 72 65 73  t the .** expres
36ae8 73 69 6f 6e 20 70 61 72 73 65 72 2e 20 49 74 20  sion parser. It 
36ae9 73 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64  should be called
36aea 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
36aeb 2a 2a 20 20 20 66 74 73 33 5f 65 78 70 72 74 65  **   fts3_exprte
36aec 73 74 28 3c 74 6f 6b 65 6e 69 7a 65 72 3e 2c 20  st(<tokenizer>, 
36aed 3c 65 78 70 72 3e 2c 20 3c 63 6f 6c 75 6d 6e 20  <expr>, <column 
36aee 31 3e 2c 20 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20  1>, ...);.**.** 
36aef 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
36af0 6e 74 2c 20 3c 74 6f 6b 65 6e 69 7a 65 72 3e 2c  nt, <tokenizer>,
36af1 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
36af2 74 68 65 20 66 74 73 33 20 74 6f 6b 65 6e 69 7a  the fts3 tokeniz
36af3 65 72 20 75 73 65 64 0a 2a 2a 20 74 6f 20 70 61  er used.** to pa
36af4 72 73 65 20 74 68 65 20 71 75 65 72 79 20 65 78  rse the query ex
36af5 70 72 65 73 73 69 6f 6e 20 28 73 65 65 20 52 45  pression (see RE
36af6 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65 72 73 29  ADME.tokenizers)
36af7 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  . The second arg
36af8 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74 68 65 20  ument.** is the 
36af9 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  query expression
36afa 20 74 6f 20 70 61 72 73 65 2e 20 45 61 63 68 20   to parse. Each 
36afb 73 75 62 73 65 71 75 65 6e 74 20 61 72 67 75 6d  subsequent argum
36afc 65 6e 74 20 69 73 20 74 68 65 20 6e 61 6d 65 0a  ent is the name.
36afd 2a 2a 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6f  ** of a column o
36afe 66 20 74 68 65 20 66 74 73 33 20 74 61 62 6c 65  f the fts3 table
36aff 20 74 68 61 74 20 74 68 65 20 71 75 65 72 79 20   that the query 
36b00 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 79 20 72  expression may r
36b01 65 66 65 72 20 74 6f 2e 0a 2a 2a 20 46 6f 72 20  efer to..** For 
36b02 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20  example:.**.**  
36b03 20 53 45 4c 45 43 54 20 66 74 73 33 5f 65 78 70   SELECT fts3_exp
36b04 72 74 65 73 74 28 27 73 69 6d 70 6c 65 27 2c 20  rtest('simple', 
36b05 27 42 69 6c 6c 20 63 6f 6c 32 3a 42 6c 6f 67 67  'Bill col2:Blogg
36b06 73 27 2c 20 27 63 6f 6c 31 27 2c 20 27 63 6f 6c  s', 'col1', 'col
36b07 32 27 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 76  2');.*/.static v
36b08 6f 69 64 20 66 74 73 33 45 78 70 72 54 65 73 74  oid fts3ExprTest
36b09 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
36b0a 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
36b0b 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
36b0c 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
36b0d 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  .){.  sqlite3_to
36b0e 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63  kenizer_module c
36b0f 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20  onst *pModule = 
36b10 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  0;.  sqlite3_tok
36b11 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
36b12 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63  er = 0;.  int rc
36b13 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c  ;.  char **azCol
36b14 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68   = 0;.  const ch
36b15 61 72 20 2a 7a 45 78 70 72 3b 0a 20 20 69 6e 74  ar *zExpr;.  int
36b16 20 6e 45 78 70 72 3b 0a 20 20 69 6e 74 20 6e 43   nExpr;.  int nC
36b17 6f 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  ol;.  int ii;.  
36b18 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 3b  Fts3Expr *pExpr;
36b19 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
36b1a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
36b1b 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
36b1c 78 74 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 63  xt);..  if( argc
36b1d 3c 33 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  <3 ){.    sqlite
36b1e 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
36b1f 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20 20  ontext, .       
36b20 20 22 55 73 61 67 65 3a 20 66 74 73 33 5f 65 78   "Usage: fts3_ex
36b21 70 72 74 65 73 74 28 74 6f 6b 65 6e 69 7a 65 72  prtest(tokenizer
36b22 2c 20 65 78 70 72 2c 20 63 6f 6c 31 2c 20 2e 2e  , expr, col1, ..
36b23 2e 22 2c 20 2d 31 0a 20 20 20 20 29 3b 0a 20 20  .", -1.    );.  
36b24 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
36b25 20 72 63 20 3d 20 71 75 65 72 79 54 65 73 74 54   rc = queryTestT
36b26 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 0a 20 20 20  okenizer(db,.   
36b27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b28 20 20 20 20 20 20 20 28 63 6f 6e 73 74 20 63 68         (const ch
36b29 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
36b2a 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
36b2b 2c 20 26 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69  , &pModule);.  i
36b2c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
36b2d 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  MEM ){.    sqlit
36b2e 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
36b2f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a  nomem(context);.
36b30 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74 65 73      goto exprtes
36b31 74 5f 6f 75 74 3b 0a 20 20 7d 65 6c 73 65 20 69  t_out;.  }else i
36b32 66 28 20 21 70 4d 6f 64 75 6c 65 20 29 7b 0a 20  f( !pModule ){. 
36b33 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
36b34 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
36b35 20 22 4e 6f 20 73 75 63 68 20 74 6f 6b 65 6e 69   "No such tokeni
36b36 7a 65 72 20 6d 6f 64 75 6c 65 22 2c 20 2d 31 29  zer module", -1)
36b37 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72 74  ;.    goto exprt
36b38 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  est_out;.  }..  
36b39 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 43  rc = pModule->xC
36b3a 72 65 61 74 65 28 30 2c 20 30 2c 20 26 70 54 6f  reate(0, 0, &pTo
36b3b 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 61 73 73 65  kenizer);.  asse
36b3c 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  rt( rc==SQLITE_N
36b3d 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  OMEM || rc==SQLI
36b3e 54 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72  TE_OK );.  if( r
36b3f 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
36b40 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
36b41 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
36b42 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  m(context);.    
36b43 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75  goto exprtest_ou
36b44 74 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65 6e 69  t;.  }.  pTokeni
36b45 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70  zer->pModule = p
36b46 4d 6f 64 75 6c 65 3b 0a 0a 20 20 7a 45 78 70 72  Module;..  zExpr
36b47 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
36b48 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
36b49 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  ext(argv[1]);.  
36b4a 6e 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 5f  nExpr = sqlite3_
36b4b 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
36b4c 5b 31 5d 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 61  [1]);.  nCol = a
36b4d 72 67 63 2d 32 3b 0a 20 20 61 7a 43 6f 6c 20 3d  rgc-2;.  azCol =
36b4e 20 28 63 68 61 72 20 2a 2a 29 73 71 6c 69 74 65   (char **)sqlite
36b4f 33 5f 6d 61 6c 6c 6f 63 28 6e 43 6f 6c 2a 73 69  3_malloc(nCol*si
36b50 7a 65 6f 66 28 63 68 61 72 20 2a 29 29 3b 0a 20  zeof(char *));. 
36b51 20 69 66 28 20 21 61 7a 43 6f 6c 20 29 7b 0a 20   if( !azCol ){. 
36b52 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
36b53 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f  t_error_nomem(co
36b54 6e 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f  ntext);.    goto
36b55 20 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20   exprtest_out;. 
36b56 20 7d 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69   }.  for(ii=0; i
36b57 69 3c 6e 43 6f 6c 3b 20 69 69 2b 2b 29 7b 0a 20  i<nCol; ii++){. 
36b58 20 20 20 61 7a 43 6f 6c 5b 69 69 5d 20 3d 20 28     azCol[ii] = (
36b59 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
36b5a 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 69  alue_text(argv[i
36b5b 69 2b 32 5d 29 3b 0a 20 20 7d 0a 0a 20 20 72 63  i+2]);.  }..  rc
36b5c 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 45 78   = sqlite3Fts3Ex
36b5d 70 72 50 61 72 73 65 28 0a 20 20 20 20 20 20 70  prParse(.      p
36b5e 54 6f 6b 65 6e 69 7a 65 72 2c 20 61 7a 43 6f 6c  Tokenizer, azCol
36b5f 2c 20 6e 43 6f 6c 2c 20 6e 43 6f 6c 2c 20 7a 45  , nCol, nCol, zE
36b60 78 70 72 2c 20 6e 45 78 70 72 2c 20 26 70 45 78  xpr, nExpr, &pEx
36b61 70 72 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  pr.  );.  if( rc
36b62 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
36b63 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
36b64 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
36b65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 67  (context);.    g
36b66 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74  oto exprtest_out
36b67 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63  ;.  }else if( rc
36b68 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
36b69 20 20 20 63 68 61 72 20 7a 42 75 66 5b 34 30 39     char zBuf[409
36b6a 36 5d 3b 0a 20 20 20 20 65 78 70 72 54 6f 53 74  6];.    exprToSt
36b6b 72 69 6e 67 28 70 45 78 70 72 2c 20 7a 42 75 66  ring(pExpr, zBuf
36b6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
36b6d 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
36b6e 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51  xt, zBuf, -1, SQ
36b6f 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
36b70 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33  .    sqlite3Fts3
36b71 45 78 70 72 46 72 65 65 28 70 45 78 70 72 29 3b  ExprFree(pExpr);
36b72 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
36b73 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
36b74 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 45 72 72  or(context, "Err
36b75 6f 72 20 70 61 72 73 69 6e 67 20 65 78 70 72 65  or parsing expre
36b76 73 73 69 6f 6e 22 2c 20 2d 31 29 3b 0a 20 20 7d  ssion", -1);.  }
36b77 0a 0a 65 78 70 72 74 65 73 74 5f 6f 75 74 3a 0a  ..exprtest_out:.
36b78 20 20 69 66 28 20 70 4d 6f 64 75 6c 65 20 26 26    if( pModule &&
36b79 20 70 54 6f 6b 65 6e 69 7a 65 72 20 29 7b 0a 20   pTokenizer ){. 
36b7a 20 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d     rc = pModule-
36b7b 3e 78 44 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e  >xDestroy(pToken
36b7c 69 7a 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  izer);.  }.  sql
36b7d 69 74 65 33 5f 66 72 65 65 28 61 7a 43 6f 6c 29  ite3_free(azCol)
36b7e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  ;.}../*.** Regis
36b7f 74 65 72 20 74 68 65 20 71 75 65 72 79 20 65 78  ter the query ex
36b80 70 72 65 73 73 69 6f 6e 20 70 61 72 73 65 72 20  pression parser 
36b81 74 65 73 74 20 66 75 6e 63 74 69 6f 6e 20 66 74  test function ft
36b82 73 33 5f 65 78 70 72 74 65 73 74 28 29 20 0a 2a  s3_exprtest() .*
36b83 2a 20 77 69 74 68 20 64 61 74 61 62 61 73 65 20  * with database 
36b84 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 2e 20 0a  connection db. .
36b85 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
36b86 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
36b87 73 33 45 78 70 72 49 6e 69 74 54 65 73 74 49 6e  s3ExprInitTestIn
36b88 74 65 72 66 61 63 65 28 73 71 6c 69 74 65 33 2a  terface(sqlite3*
36b89 20 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f   db){.  sqlite3_
36b8a 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
36b8b 0a 20 20 20 20 20 20 64 62 2c 20 22 66 74 73 33  .      db, "fts3
36b8c 5f 65 78 70 72 74 65 73 74 22 2c 20 2d 31 2c 20  _exprtest", -1, 
36b8d 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20  SQLITE_UTF8, 0, 
36b8e 66 74 73 33 45 78 70 72 54 65 73 74 2c 20 30 2c  fts3ExprTest, 0,
36b8f 20 30 0a 20 20 29 3b 0a 7d 0a 0a 23 65 6e 64 69   0.  );.}..#endi
36b90 66 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  f.#endif /* !def
36b91 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
36b92 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
36b93 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
36b94 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
36b95 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
36b96 5f 65 78 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  _expr.c ********
36b97 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36b98 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36b99 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
36b9a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
36b9b 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a  fts3_hash.c ****
36b9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36b9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36b9e 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
36b9f 53 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a  September 22.**.
36ba0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
36ba1 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
36ba2 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
36ba3 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
36ba4 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
36ba5 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
36ba6 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
36ba7 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
36ba8 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
36ba9 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
36baa 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
36bab 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
36bac 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
36bad 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
36bae 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
36baf 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
36bb0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
36bb1 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
36bb2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36bb3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36bb4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36bb5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
36bb6 68 69 73 20 69 73 20 74 68 65 20 69 6d 70 6c 65  his is the imple
36bb7 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 67 65 6e  mentation of gen
36bb8 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65 73  eric hash-tables
36bb9 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e   used in SQLite.
36bba 0a 2a 2a 20 57 65 27 76 65 20 6d 6f 64 69 66 69  .** We've modifi
36bbb 65 64 20 69 74 20 73 6c 69 67 68 74 6c 79 20 74  ed it slightly t
36bbc 6f 20 73 65 72 76 65 20 61 73 20 61 20 73 74 61  o serve as a sta
36bbd 6e 64 61 6c 6f 6e 65 20 68 61 73 68 20 74 61 62  ndalone hash tab
36bbe 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  le.** implementa
36bbf 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 75 6c  tion for the ful
36bc0 6c 2d 74 65 78 74 20 69 6e 64 65 78 69 6e 67 20  l-text indexing 
36bc1 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  module..*/../*.*
36bc2 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
36bc3 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20  is file is only 
36bc4 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a  compiled if:.**.
36bc5 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
36bc6 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
36bc7 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78  g built as an ex
36bc8 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  tension.**      
36bc9 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
36bca 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e  SQLITE_CORE is n
36bcb 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a  ot defined), or.
36bcc 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
36bcd 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
36bce 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20  eing built into 
36bcf 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20  the core of.**  
36bd0 20 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20       SQLite (in 
36bd1 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54  which case SQLIT
36bd2 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73  E_ENABLE_FTS3 is
36bd3 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69   defined)..*/.#i
36bd4 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
36bd5 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
36bd6 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
36bd7 5f 46 54 53 33 29 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  _FTS3)..../*.** 
36bd8 4d 61 6c 6c 6f 63 20 61 6e 64 20 46 72 65 65 20  Malloc and Free 
36bd9 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61  functions.*/.sta
36bda 74 69 63 20 76 6f 69 64 20 2a 66 74 73 33 48 61  tic void *fts3Ha
36bdb 73 68 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b  shMalloc(int n){
36bdc 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71 6c  .  void *p = sql
36bdd 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 29 3b 0a  ite3_malloc(n);.
36bde 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d    if( p ){.    m
36bdf 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a  emset(p, 0, n);.
36be0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
36be1 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  }.static void ft
36be2 73 33 48 61 73 68 46 72 65 65 28 76 6f 69 64 20  s3HashFree(void 
36be3 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66  *p){.  sqlite3_f
36be4 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 20 54 75  ree(p);.}../* Tu
36be5 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69  rn bulk memory i
36be6 6e 74 6f 20 61 20 68 61 73 68 20 74 61 62 6c 65  nto a hash table
36be7 20 6f 62 6a 65 63 74 20 62 79 20 69 6e 69 74 69   object by initi
36be8 61 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 66  alizing the.** f
36be9 69 65 6c 64 73 20 6f 66 20 74 68 65 20 48 61 73  ields of the Has
36bea 68 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  h structure..**.
36beb 2a 2a 20 22 70 4e 65 77 22 20 69 73 20 61 20 70  ** "pNew" is a p
36bec 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 68 61  ointer to the ha
36bed 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 69 73  sh table that is
36bee 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69 7a   to be initializ
36bef 65 64 2e 0a 2a 2a 20 6b 65 79 43 6c 61 73 73 20  ed..** keyClass 
36bf0 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 6f  is one of the co
36bf1 6e 73 74 61 6e 74 73 20 0a 2a 2a 20 46 54 53 33  nstants .** FTS3
36bf2 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 6f 72 20  _HASH_BINARY or 
36bf3 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47  FTS3_HASH_STRING
36bf4 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  .  The value of 
36bf5 6b 65 79 43 6c 61 73 73 20 0a 2a 2a 20 64 65 74  keyClass .** det
36bf6 65 72 6d 69 6e 65 73 20 77 68 61 74 20 6b 69 6e  ermines what kin
36bf7 64 20 6f 66 20 6b 65 79 20 74 68 65 20 68 61 73  d of key the has
36bf8 68 20 74 61 62 6c 65 20 77 69 6c 6c 20 75 73 65  h table will use
36bf9 2e 20 20 22 63 6f 70 79 4b 65 79 22 20 69 73 0a  .  "copyKey" is.
36bfa 2a 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 68  ** true if the h
36bfb 61 73 68 20 74 61 62 6c 65 20 73 68 6f 75 6c 64  ash table should
36bfc 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20 70 72   make its own pr
36bfd 69 76 61 74 65 20 63 6f 70 79 20 6f 66 20 6b 65  ivate copy of ke
36bfe 79 73 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20  ys and.** false 
36bff 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6a 75 73  if it should jus
36c00 74 20 75 73 65 20 74 68 65 20 73 75 70 70 6c 69  t use the suppli
36c01 65 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53  ed pointer..*/.S
36c02 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
36c03 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 48 61  id sqlite3Fts3Ha
36c04 73 68 49 6e 69 74 28 46 74 73 33 48 61 73 68 20  shInit(Fts3Hash 
36c05 2a 70 4e 65 77 2c 20 63 68 61 72 20 6b 65 79 43  *pNew, char keyC
36c06 6c 61 73 73 2c 20 63 68 61 72 20 63 6f 70 79 4b  lass, char copyK
36c07 65 79 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  ey){.  assert( p
36c08 4e 65 77 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  New!=0 );.  asse
36c09 72 74 28 20 6b 65 79 43 6c 61 73 73 3e 3d 46 54  rt( keyClass>=FT
36c0a 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 26  S3_HASH_STRING &
36c0b 26 20 6b 65 79 43 6c 61 73 73 3c 3d 46 54 53 33  & keyClass<=FTS3
36c0c 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a  _HASH_BINARY );.
36c0d 20 20 70 4e 65 77 2d 3e 6b 65 79 43 6c 61 73 73    pNew->keyClass
36c0e 20 3d 20 6b 65 79 43 6c 61 73 73 3b 0a 20 20 70   = keyClass;.  p
36c0f 4e 65 77 2d 3e 63 6f 70 79 4b 65 79 20 3d 20 63  New->copyKey = c
36c10 6f 70 79 4b 65 79 3b 0a 20 20 70 4e 65 77 2d 3e  opyKey;.  pNew->
36c11 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 70 4e 65  first = 0;.  pNe
36c12 77 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20  w->count = 0;.  
36c13 70 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d 20 30  pNew->htsize = 0
36c14 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d 20 30  ;.  pNew->ht = 0
36c15 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 61  ;.}../* Remove a
36c16 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20  ll entries from 
36c17 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 52  a hash table.  R
36c18 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72  eclaim all memor
36c19 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20  y..** Call this 
36c1a 72 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c 65 74  routine to delet
36c1b 65 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f  e a hash table o
36c1c 72 20 74 6f 20 72 65 73 65 74 20 61 20 68 61 73  r to reset a has
36c1d 68 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20 74 68  h table.** to th
36c1e 65 20 65 6d 70 74 79 20 73 74 61 74 65 2e 0a 2a  e empty state..*
36c1f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
36c20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
36c21 33 48 61 73 68 43 6c 65 61 72 28 46 74 73 33 48  3HashClear(Fts3H
36c22 61 73 68 20 2a 70 48 29 7b 0a 20 20 46 74 73 33  ash *pH){.  Fts3
36c23 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20  HashElem *elem; 
36c24 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c          /* For l
36c25 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20  ooping over all 
36c26 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20  elements of the 
36c27 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65  table */..  asse
36c28 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65  rt( pH!=0 );.  e
36c29 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b  lem = pH->first;
36c2a 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30  .  pH->first = 0
36c2b 3b 0a 20 20 66 74 73 33 48 61 73 68 46 72 65 65  ;.  fts3HashFree
36c2c 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e  (pH->ht);.  pH->
36c2d 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74  ht = 0;.  pH->ht
36c2e 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c  size = 0;.  whil
36c2f 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 46  e( elem ){.    F
36c30 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65 78  ts3HashElem *nex
36c31 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e  t_elem = elem->n
36c32 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d  ext;.    if( pH-
36c33 3e 63 6f 70 79 4b 65 79 20 26 26 20 65 6c 65 6d  >copyKey && elem
36c34 2d 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  ->pKey ){.      
36c35 66 74 73 33 48 61 73 68 46 72 65 65 28 65 6c 65  fts3HashFree(ele
36c36 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20 7d 0a  m->pKey);.    }.
36c37 20 20 20 20 66 74 73 33 48 61 73 68 46 72 65 65      fts3HashFree
36c38 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c 65 6d  (elem);.    elem
36c39 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a 20 20   = next_elem;.  
36c3a 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20  }.  pH->count = 
36c3b 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68  0;.}../*.** Hash
36c3c 20 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f 6e 20   and comparison 
36c3d 66 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e 20 74  functions when t
36c3e 68 65 20 6d 6f 64 65 20 69 73 20 46 54 53 33 5f  he mode is FTS3_
36c3f 48 41 53 48 5f 53 54 52 49 4e 47 0a 2a 2f 0a 73  HASH_STRING.*/.s
36c40 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 74  tatic int fts3St
36c41 72 48 61 73 68 28 63 6f 6e 73 74 20 76 6f 69 64  rHash(const void
36c42 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
36c43 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
36c44 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  *z = (const char
36c45 20 2a 29 70 4b 65 79 3b 0a 20 20 69 6e 74 20 68   *)pKey;.  int h
36c46 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 4b 65 79   = 0;.  if( nKey
36c47 3c 3d 30 20 29 20 6e 4b 65 79 20 3d 20 28 69 6e  <=0 ) nKey = (in
36c48 74 29 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20  t) strlen(z);.  
36c49 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20 30 20  while( nKey > 0 
36c4a 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c   ){.    h = (h<<
36c4b 33 29 20 5e 20 68 20 5e 20 2a 7a 2b 2b 3b 0a 20  3) ^ h ^ *z++;. 
36c4c 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d 0a 20     nKey--;.  }. 
36c4d 20 72 65 74 75 72 6e 20 68 20 26 20 30 78 37 66   return h & 0x7f
36c4e 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74 69 63  ffffff;.}.static
36c4f 20 69 6e 74 20 66 74 73 33 53 74 72 43 6f 6d 70   int fts3StrComp
36c50 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a  are(const void *
36c51 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63  pKey1, int n1, c
36c52 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32  onst void *pKey2
36c53 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28  , int n2){.  if(
36c54 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75 72 6e   n1!=n2 ) return
36c55 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72   1;.  return str
36c56 6e 63 6d 70 28 28 63 6f 6e 73 74 20 63 68 61 72  ncmp((const char
36c57 2a 29 70 4b 65 79 31 2c 28 63 6f 6e 73 74 20 63  *)pKey1,(const c
36c58 68 61 72 2a 29 70 4b 65 79 32 2c 6e 31 29 3b 0a  har*)pKey2,n1);.
36c59 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e  }../*.** Hash an
36c5a 64 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  d comparison fun
36c5b 63 74 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 20  ctions when the 
36c5c 6d 6f 64 65 20 69 73 20 46 54 53 33 5f 48 41 53  mode is FTS3_HAS
36c5d 48 5f 42 49 4e 41 52 59 0a 2a 2f 0a 73 74 61 74  H_BINARY.*/.stat
36c5e 69 63 20 69 6e 74 20 66 74 73 33 42 69 6e 48 61  ic int fts3BinHa
36c5f 73 68 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  sh(const void *p
36c60 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a  Key, int nKey){.
36c61 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20 63    int h = 0;.  c
36c62 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28  onst char *z = (
36c63 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65  const char *)pKe
36c64 79 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65 79  y;.  while( nKey
36c65 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 68 20  -- > 0 ){.    h 
36c66 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 2a  = (h<<3) ^ h ^ *
36c67 28 7a 2b 2b 29 3b 0a 20 20 7d 0a 20 20 72 65 74  (z++);.  }.  ret
36c68 75 72 6e 20 68 20 26 20 30 78 37 66 66 66 66 66  urn h & 0x7fffff
36c69 66 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  ff;.}.static int
36c6a 20 66 74 73 33 42 69 6e 43 6f 6d 70 61 72 65 28   fts3BinCompare(
36c6b 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
36c6c 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74  1, int n1, const
36c6d 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e   void *pKey2, in
36c6e 74 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e 31 21  t n2){.  if( n1!
36c6f 3d 6e 32 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  =n2 ) return 1;.
36c70 20 20 72 65 74 75 72 6e 20 6d 65 6d 63 6d 70 28    return memcmp(
36c71 70 4b 65 79 31 2c 70 4b 65 79 32 2c 6e 31 29 3b  pKey1,pKey2,n1);
36c72 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
36c73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
36c74 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 68 61  e appropriate ha
36c75 73 68 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65  sh function give
36c76 6e 20 74 68 65 20 6b 65 79 20 63 6c 61 73 73 2e  n the key class.
36c77 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 73 79 6e  .**.** The C syn
36c78 74 61 78 20 69 6e 20 74 68 69 73 20 66 75 6e 63  tax in this func
36c79 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20  tion definition 
36c7a 6d 61 79 20 62 65 20 75 6e 66 61 6d 69 6c 61 72  may be unfamilar
36c7b 20 74 6f 20 73 6f 6d 65 20 0a 2a 2a 20 70 72 6f   to some .** pro
36c7c 67 72 61 6d 6d 65 72 73 2c 20 73 6f 20 77 65 20  grammers, so we 
36c7d 70 72 6f 76 69 64 65 20 74 68 65 20 66 6f 6c 6c  provide the foll
36c7e 6f 77 69 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c  owing additional
36c7f 20 65 78 70 6c 61 6e 61 74 69 6f 6e 3a 0a 2a 2a   explanation:.**
36c80 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  .** The name of 
36c81 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
36c82 22 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e  "ftsHashFunction
36c83 22 2e 20 20 54 68 65 20 66 75 6e 63 74 69 6f 6e  ".  The function
36c84 20 74 61 6b 65 73 20 61 0a 2a 2a 20 73 69 6e 67   takes a.** sing
36c85 6c 65 20 70 61 72 61 6d 65 74 65 72 20 22 6b 65  le parameter "ke
36c86 79 43 6c 61 73 73 22 2e 20 20 54 68 65 20 72 65  yClass".  The re
36c87 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 74  turn value of ft
36c88 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 29 0a  sHashFunction().
36c89 2a 2a 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ** is a pointer 
36c8a 74 6f 20 61 6e 6f 74 68 65 72 20 66 75 6e 63 74  to another funct
36c8b 69 6f 6e 2e 20 20 53 70 65 63 69 66 69 63 61 6c  ion.  Specifical
36c8c 6c 79 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76  ly, the return v
36c8d 61 6c 75 65 0a 2a 2a 20 6f 66 20 66 74 73 48 61  alue.** of ftsHa
36c8e 73 68 46 75 6e 63 74 69 6f 6e 28 29 20 69 73 20  shFunction() is 
36c8f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 66  a pointer to a f
36c90 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 74 61 6b  unction that tak
36c91 65 73 20 74 77 6f 20 70 61 72 61 6d 65 74 65 72  es two parameter
36c92 73 0a 2a 2a 20 77 69 74 68 20 74 79 70 65 73 20  s.** with types 
36c93 22 63 6f 6e 73 74 20 76 6f 69 64 2a 22 20 61 6e  "const void*" an
36c94 64 20 22 69 6e 74 22 20 61 6e 64 20 72 65 74 75  d "int" and retu
36c95 72 6e 73 20 61 6e 20 22 69 6e 74 22 2e 0a 2a 2f  rns an "int"..*/
36c96 0a 73 74 61 74 69 63 20 69 6e 74 20 28 2a 66 74  .static int (*ft
36c97 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 69 6e  sHashFunction(in
36c98 74 20 6b 65 79 43 6c 61 73 73 29 29 28 63 6f 6e  t keyClass))(con
36c99 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 7b 0a 20  st void*,int){. 
36c9a 20 69 66 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46   if( keyClass==F
36c9b 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20  TS3_HASH_STRING 
36c9c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26 66  ){.    return &f
36c9d 74 73 33 53 74 72 48 61 73 68 3b 0a 20 20 7d 65  ts3StrHash;.  }e
36c9e 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
36c9f 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f   keyClass==FTS3_
36ca0 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20  HASH_BINARY );. 
36ca1 20 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 42     return &fts3B
36ca2 69 6e 48 61 73 68 3b 0a 20 20 7d 0a 7d 0a 0a 2f  inHash;.  }.}../
36ca3 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
36ca4 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 70 70  inter to the app
36ca5 72 6f 70 72 69 61 74 65 20 68 61 73 68 20 66 75  ropriate hash fu
36ca6 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65  nction given the
36ca7 20 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a   key class..**.*
36ca8 2a 20 46 6f 72 20 68 65 6c 70 20 69 6e 20 69 6e  * For help in in
36ca9 74 65 72 70 72 65 74 65 64 20 74 68 65 20 6f 62  terpreted the ob
36caa 73 63 75 72 65 20 43 20 63 6f 64 65 20 69 6e 20  scure C code in 
36cab 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66  the function def
36cac 69 6e 69 74 69 6f 6e 2c 0a 2a 2a 20 73 65 65 20  inition,.** see 
36cad 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d 65  the header comme
36cae 6e 74 20 6f 6e 20 74 68 65 20 70 72 65 76 69 6f  nt on the previo
36caf 75 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  us function..*/.
36cb0 73 74 61 74 69 63 20 69 6e 74 20 28 2a 66 74 73  static int (*fts
36cb1 43 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f 6e 28  CompareFunction(
36cb2 69 6e 74 20 6b 65 79 43 6c 61 73 73 29 29 28 63  int keyClass))(c
36cb3 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63  onst void*,int,c
36cb4 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 7b  onst void*,int){
36cb5 0a 20 20 69 66 28 20 6b 65 79 43 6c 61 73 73 3d  .  if( keyClass=
36cb6 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e  =FTS3_HASH_STRIN
36cb7 47 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  G ){.    return 
36cb8 26 66 74 73 33 53 74 72 43 6f 6d 70 61 72 65 3b  &fts3StrCompare;
36cb9 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
36cba 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3d 3d  sert( keyClass==
36cbb 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59  FTS3_HASH_BINARY
36cbc 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 26   );.    return &
36cbd 66 74 73 33 42 69 6e 43 6f 6d 70 61 72 65 3b 0a  fts3BinCompare;.
36cbe 20 20 7d 0a 7d 0a 0a 2f 2a 20 4c 69 6e 6b 20 61    }.}../* Link a
36cbf 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74  n element into t
36cc0 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2f  he hash table.*/
36cc1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
36cc2 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65 6d 65  3HashInsertEleme
36cc3 6e 74 28 0a 20 20 46 74 73 33 48 61 73 68 20 2a  nt(.  Fts3Hash *
36cc4 70 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  pH,            /
36cc5 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 68  * The complete h
36cc6 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73  ash table */.  s
36cc7 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 70  truct _fts3ht *p
36cc8 45 6e 74 72 79 2c 20 20 2f 2a 20 54 68 65 20 65  Entry,  /* The e
36cc9 6e 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20  ntry into which 
36cca 70 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64  pNew is inserted
36ccb 20 2a 2f 0a 20 20 46 74 73 33 48 61 73 68 45 6c   */.  Fts3HashEl
36ccc 65 6d 20 2a 70 4e 65 77 20 20 20 20 20 20 20 2f  em *pNew       /
36ccd 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f  * The element to
36cce 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
36ccf 29 7b 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65  ){.  Fts3HashEle
36cd0 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 20 2f 2a  m *pHead;     /*
36cd1 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 61   First element a
36cd2 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74 72 79  lready in pEntry
36cd3 20 2a 2f 0a 20 20 70 48 65 61 64 20 3d 20 70 45   */.  pHead = pE
36cd4 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 69  ntry->chain;.  i
36cd5 66 28 20 70 48 65 61 64 20 29 7b 0a 20 20 20 20  f( pHead ){.    
36cd6 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 65  pNew->next = pHe
36cd7 61 64 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72  ad;.    pNew->pr
36cd8 65 76 20 3d 20 70 48 65 61 64 2d 3e 70 72 65 76  ev = pHead->prev
36cd9 3b 0a 20 20 20 20 69 66 28 20 70 48 65 61 64 2d  ;.    if( pHead-
36cda 3e 70 72 65 76 20 29 7b 20 70 48 65 61 64 2d 3e  >prev ){ pHead->
36cdb 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 70 4e 65  prev->next = pNe
36cdc 77 3b 20 7d 0a 20 20 20 20 65 6c 73 65 20 20 20  w; }.    else   
36cdd 20 20 20 20 20 20 20 20 20 20 7b 20 70 48 2d 3e            { pH->
36cde 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 20 7d 0a  first = pNew; }.
36cdf 20 20 20 20 70 48 65 61 64 2d 3e 70 72 65 76 20      pHead->prev 
36ce0 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b  = pNew;.  }else{
36ce1 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20  .    pNew->next 
36ce2 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20  = pH->first;.   
36ce3 20 69 66 28 20 70 48 2d 3e 66 69 72 73 74 20 29   if( pH->first )
36ce4 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e 70 72 65  { pH->first->pre
36ce5 76 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20  v = pNew; }.    
36ce6 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 30 3b 0a  pNew->prev = 0;.
36ce7 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d 20      pH->first = 
36ce8 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 45 6e 74  pNew;.  }.  pEnt
36ce9 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 70  ry->count++;.  p
36cea 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 70  Entry->chain = p
36ceb 4e 65 77 3b 0a 7d 0a 0a 0a 2f 2a 20 52 65 73 69  New;.}.../* Resi
36cec 7a 65 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  ze the hash tabl
36ced 65 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e  e so that it can
36cee 74 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a 65 22  tains "new_size"
36cef 20 62 75 63 6b 65 74 73 2e 0a 2a 2a 20 22 6e 65   buckets..** "ne
36cf0 77 5f 73 69 7a 65 22 20 6d 75 73 74 20 62 65 20  w_size" must be 
36cf1 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 20 20 54  a power of 2.  T
36cf2 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 6d 69  he hash table mi
36cf3 67 68 74 20 66 61 69 6c 20 0a 2a 2a 20 74 6f 20  ght fail .** to 
36cf4 72 65 73 69 7a 65 20 69 66 20 73 71 6c 69 74 65  resize if sqlite
36cf5 4d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a  Malloc() fails..
36cf6 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e 6f 6e  **.** Return non
36cf7 2d 7a 65 72 6f 20 69 66 20 61 20 6d 65 6d 6f 72  -zero if a memor
36cf8 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
36cf9 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74  or occurs..*/.st
36cfa 61 74 69 63 20 69 6e 74 20 66 74 73 33 52 65 68  atic int fts3Reh
36cfb 61 73 68 28 46 74 73 33 48 61 73 68 20 2a 70 48  ash(Fts3Hash *pH
36cfc 2c 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b  , int new_size){
36cfd 0a 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68  .  struct _fts3h
36cfe 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20 20 20 20  t *new_ht;      
36cff 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 68      /* The new h
36d00 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 46  ash table */.  F
36d01 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65  ts3HashElem *ele
36d02 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20 20  m, *next_elem;  
36d03 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f  /* For looping o
36d04 76 65 72 20 65 78 69 73 74 69 6e 67 20 65 6c 65  ver existing ele
36d05 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 28  ments */.  int (
36d06 2a 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76 6f  *xHash)(const vo
36d07 69 64 2a 2c 69 6e 74 29 3b 20 20 20 2f 2a 20 54  id*,int);   /* T
36d08 68 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e  he hash function
36d09 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 28   */..  assert( (
36d0a 6e 65 77 5f 73 69 7a 65 20 26 20 28 6e 65 77 5f  new_size & (new_
36d0b 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20  size-1))==0 );. 
36d0c 20 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63   new_ht = (struc
36d0d 74 20 5f 66 74 73 33 68 74 20 2a 29 66 74 73 33  t _fts3ht *)fts3
36d0e 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f  HashMalloc( new_
36d0f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75  size*sizeof(stru
36d10 63 74 20 5f 66 74 73 33 68 74 29 20 29 3b 0a 20  ct _fts3ht) );. 
36d11 20 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 29   if( new_ht==0 )
36d12 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 66 74 73   return 1;.  fts
36d13 33 48 61 73 68 46 72 65 65 28 70 48 2d 3e 68 74  3HashFree(pH->ht
36d14 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65  );.  pH->ht = ne
36d15 77 5f 68 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69  w_ht;.  pH->htsi
36d16 7a 65 20 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a 20  ze = new_size;. 
36d17 20 78 48 61 73 68 20 3d 20 66 74 73 48 61 73 68   xHash = ftsHash
36d18 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79  Function(pH->key
36d19 43 6c 61 73 73 29 3b 0a 20 20 66 6f 72 28 65 6c  Class);.  for(el
36d1a 65 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20 70 48  em=pH->first, pH
36d1b 2d 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b  ->first=0; elem;
36d1c 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65   elem = next_ele
36d1d 6d 29 7b 0a 20 20 20 20 69 6e 74 20 68 20 3d 20  m){.    int h = 
36d1e 28 2a 78 48 61 73 68 29 28 65 6c 65 6d 2d 3e 70  (*xHash)(elem->p
36d1f 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29  Key, elem->nKey)
36d20 20 26 20 28 6e 65 77 5f 73 69 7a 65 2d 31 29 3b   & (new_size-1);
36d21 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d  .    next_elem =
36d22 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20   elem->next;.   
36d23 20 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 45   fts3HashInsertE
36d24 6c 65 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f  lement(pH, &new_
36d25 68 74 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20  ht[h], elem);.  
36d26 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
36d27 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ./* This functio
36d28 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20  n (for internal 
36d29 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65  use only) locate
36d2a 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20  s an element in 
36d2b 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  an.** hash table
36d2c 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68   that matches th
36d2d 65 20 67 69 76 65 6e 20 6b 65 79 2e 20 20 54 68  e given key.  Th
36d2e 65 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20  e hash for this 
36d2f 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61  key has.** alrea
36d30 64 79 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64  dy been computed
36d31 20 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 61   and is passed a
36d32 73 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65  s the 4th parame
36d33 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46  ter..*/.static F
36d34 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 66 74 73  ts3HashElem *fts
36d35 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61  3FindElementByHa
36d36 73 68 28 0a 20 20 63 6f 6e 73 74 20 46 74 73 33  sh(.  const Fts3
36d37 48 61 73 68 20 2a 70 48 2c 20 2f 2a 20 54 68 65  Hash *pH, /* The
36d38 20 70 48 20 74 6f 20 62 65 20 73 65 61 72 63 68   pH to be search
36d39 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  ed */.  const vo
36d3a 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20 54  id *pKey,   /* T
36d3b 68 65 20 6b 65 79 20 77 65 20 61 72 65 20 73 65  he key we are se
36d3c 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a 20  arching for */. 
36d3d 20 69 6e 74 20 6e 4b 65 79 2c 0a 20 20 69 6e 74   int nKey,.  int
36d3e 20 68 20 20 20 20 20 20 20 20 20 20 20 20 20 20   h              
36d3f 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 6f 72   /* The hash for
36d40 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29 7b   this key. */.){
36d41 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20  .  Fts3HashElem 
36d42 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *elem;          
36d43 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f    /* Used to loo
36d44 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d 65  p thru the eleme
36d45 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  nt list */.  int
36d46 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20   count;         
36d47 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
36d48 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
36d49 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74 20 2a  s left to test *
36d4a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 70 61  /.  int (*xCompa
36d4b 72 65 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  re)(const void*,
36d4c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
36d4d 69 6e 74 29 3b 20 20 2f 2a 20 63 6f 6d 70 61 72  int);  /* compar
36d4e 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ison function */
36d4f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29  ..  if( pH->ht )
36d50 7b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 66 74  {.    struct _ft
36d51 73 33 68 74 20 2a 70 45 6e 74 72 79 20 3d 20 26  s3ht *pEntry = &
36d52 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 65  pH->ht[h];.    e
36d53 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68  lem = pEntry->ch
36d54 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20 3d  ain;.    count =
36d55 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b 0a   pEntry->count;.
36d56 20 20 20 20 78 43 6f 6d 70 61 72 65 20 3d 20 66      xCompare = f
36d57 74 73 43 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f  tsCompareFunctio
36d58 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b  n(pH->keyClass);
36d59 0a 20 20 20 20 77 68 69 6c 65 28 20 63 6f 75 6e  .    while( coun
36d5a 74 2d 2d 20 26 26 20 65 6c 65 6d 20 29 7b 0a 20  t-- && elem ){. 
36d5b 20 20 20 20 20 69 66 28 20 28 2a 78 43 6f 6d 70       if( (*xComp
36d5c 61 72 65 29 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c  are)(elem->pKey,
36d5d 65 6c 65 6d 2d 3e 6e 4b 65 79 2c 70 4b 65 79 2c  elem->nKey,pKey,
36d5e 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0a 20 20 20  nKey)==0 ){ .   
36d5f 20 20 20 20 20 72 65 74 75 72 6e 20 65 6c 65 6d       return elem
36d60 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
36d61 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78  elem = elem->nex
36d62 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  t;.    }.  }.  r
36d63 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52  eturn 0;.}../* R
36d64 65 6d 6f 76 65 20 61 20 73 69 6e 67 6c 65 20 65  emove a single e
36d65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 68 61  ntry from the ha
36d66 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 61  sh table given a
36d67 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74   pointer to that
36d68 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20  .** element and 
36d69 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 65 6c  a hash on the el
36d6a 65 6d 65 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a  ement's key..*/.
36d6b 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33  static void fts3
36d6c 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 42 79 48  RemoveElementByH
36d6d 61 73 68 28 0a 20 20 46 74 73 33 48 61 73 68 20  ash(.  Fts3Hash 
36d6e 2a 70 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  *pH,         /* 
36d6f 54 68 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e  The pH containin
36d70 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 46 74  g "elem" */.  Ft
36d71 73 33 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65 6d  s3HashElem* elem
36d72 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65  ,   /* The eleme
36d73 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65 64  nt to be removed
36d74 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f 0a   from the pH */.
36d75 20 20 69 6e 74 20 68 20 20 20 20 20 20 20 20 20    int h         
36d76 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20          /* Hash 
36d77 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65 6c  value for the el
36d78 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73 74  ement */.){.  st
36d79 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 70 45  ruct _fts3ht *pE
36d7a 6e 74 72 79 3b 0a 20 20 69 66 28 20 65 6c 65 6d  ntry;.  if( elem
36d7b 2d 3e 70 72 65 76 20 29 7b 0a 20 20 20 20 65 6c  ->prev ){.    el
36d7c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d  em->prev->next =
36d7d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20   elem->next; .  
36d7e 7d 65 6c 73 65 7b 0a 20 20 20 20 70 48 2d 3e 66  }else{.    pH->f
36d7f 69 72 73 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78  irst = elem->nex
36d80 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 65 6c 65  t;.  }.  if( ele
36d81 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20 20 20 20 65  m->next ){.    e
36d82 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70 72 65 76 20  lem->next->prev 
36d83 3d 20 65 6c 65 6d 2d 3e 70 72 65 76 3b 0a 20 20  = elem->prev;.  
36d84 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20 26 70 48  }.  pEntry = &pH
36d85 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69 66 28 20 70  ->ht[h];.  if( p
36d86 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d 3d 65 6c  Entry->chain==el
36d87 65 6d 20 29 7b 0a 20 20 20 20 70 45 6e 74 72 79  em ){.    pEntry
36d88 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d 3e  ->chain = elem->
36d89 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 45 6e 74  next;.  }.  pEnt
36d8a 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20 69  ry->count--;.  i
36d8b 66 28 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74  f( pEntry->count
36d8c 3c 3d 30 20 29 7b 0a 20 20 20 20 70 45 6e 74 72  <=0 ){.    pEntr
36d8d 79 2d 3e 63 68 61 69 6e 20 3d 20 30 3b 0a 20 20  y->chain = 0;.  
36d8e 7d 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79  }.  if( pH->copy
36d8f 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65  Key && elem->pKe
36d90 79 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61 73  y ){.    fts3Has
36d91 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 79  hFree(elem->pKey
36d92 29 3b 0a 20 20 7d 0a 20 20 66 74 73 33 48 61 73  );.  }.  fts3Has
36d93 68 46 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a 20  hFree( elem );. 
36d94 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20   pH->count--;.  
36d95 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d 30  if( pH->count<=0
36d96 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
36d97 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b 0a  pH->first==0 );.
36d98 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e      assert( pH->
36d99 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20  count==0 );.    
36d9a 66 74 73 33 48 61 73 68 43 6c 65 61 72 28 70 48  fts3HashClear(pH
36d9b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74 74  );.  }.}../* Att
36d9c 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20 61  empt to locate a
36d9d 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  n element of the
36d9e 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 20 77   hash table pH w
36d9f 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68 61  ith a key.** tha
36da0 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c 6e  t matches pKey,n
36da1 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65  Key.  Return the
36da2 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 65   data for this e
36da3 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73 0a  lement if it is.
36da4 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55 4c  ** found, or NUL
36da5 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  L if there is no
36da6 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54   match..*/.SQLIT
36da7 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
36da8 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 46  sqlite3Fts3HashF
36da9 69 6e 64 28 63 6f 6e 73 74 20 46 74 73 33 48 61  ind(const Fts3Ha
36daa 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f  sh *pH, const vo
36dab 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
36dac 65 79 29 7b 0a 20 20 69 6e 74 20 68 3b 20 20 20  ey){.  int h;   
36dad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
36dae 20 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 20 2a   A hash on key *
36daf 2f 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d  /.  Fts3HashElem
36db0 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 54 68   *elem;    /* Th
36db1 65 20 65 6c 65 6d 65 6e 74 20 74 68 61 74 20 6d  e element that m
36db2 61 74 63 68 65 73 20 6b 65 79 20 2a 2f 0a 20 20  atches key */.  
36db3 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e  int (*xHash)(con
36db4 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20  st void*,int);  
36db5 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e 63  /* The hash func
36db6 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70  tion */..  if( p
36db7 48 3d 3d 30 20 7c 7c 20 70 48 2d 3e 68 74 3d 3d  H==0 || pH->ht==
36db8 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
36db9 78 48 61 73 68 20 3d 20 66 74 73 48 61 73 68 46  xHash = ftsHashF
36dba 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43  unction(pH->keyC
36dbb 6c 61 73 73 29 3b 0a 20 20 61 73 73 65 72 74 28  lass);.  assert(
36dbc 20 78 48 61 73 68 21 3d 30 20 29 3b 0a 20 20 68   xHash!=0 );.  h
36dbd 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b 65 79   = (*xHash)(pKey
36dbe 2c 6e 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74  ,nKey);.  assert
36dbf 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20  ( (pH->htsize & 
36dc0 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d  (pH->htsize-1))=
36dc1 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66  =0 );.  elem = f
36dc2 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79  ts3FindElementBy
36dc3 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65  Hash(pH,pKey,nKe
36dc4 79 2c 20 68 20 26 20 28 70 48 2d 3e 68 74 73 69  y, h & (pH->htsi
36dc5 7a 65 2d 31 29 29 3b 0a 20 20 72 65 74 75 72 6e  ze-1));.  return
36dc6 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64 61   elem ? elem->da
36dc7 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e  ta : 0;.}../* In
36dc8 73 65 72 74 20 61 6e 20 65 6c 65 6d 65 6e 74 20  sert an element 
36dc9 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  into the hash ta
36dca 62 6c 65 20 70 48 2e 20 20 54 68 65 20 6b 65 79  ble pH.  The key
36dcb 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a 2a   is pKey,nKey.**
36dcc 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 69 73   and the data is
36dcd 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 49   "data"..**.** I
36dce 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20 65 78 69  f no element exi
36dcf 73 74 73 20 77 69 74 68 20 61 20 6d 61 74 63 68  sts with a match
36dd0 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e 20 61 20  ing key, then a 
36dd1 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 69  new.** element i
36dd2 73 20 63 72 65 61 74 65 64 2e 20 20 41 20 63 6f  s created.  A co
36dd3 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73  py of the key is
36dd4 20 6d 61 64 65 20 69 66 20 74 68 65 20 63 6f 70   made if the cop
36dd5 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20 69 73 20  yKey.** flag is 
36dd6 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73 20 72 65  set.  NULL is re
36dd7 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
36dd8 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74   another element
36dd9 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
36dda 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65  with the same ke
36ddb 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e  y, then the.** n
36ddc 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 73  ew data replaces
36ddd 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e   the old data an
36dde 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69  d the old data i
36ddf 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54  s returned..** T
36de0 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f  he key is not co
36de1 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73  pied in this ins
36de2 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c  tance.  If a mal
36de3 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a  loc fails, then.
36de4 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20  ** the new data 
36de5 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
36de6 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69  the hash table i
36de7 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a  s unchanged..**.
36de8 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 22  ** If the "data"
36de9 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
36dea 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e  is function is N
36deb 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ULL, then the.**
36dec 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70   element corresp
36ded 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20  onding to "key" 
36dee 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20  is removed from 
36def 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  the hash table..
36df0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
36df1 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 46  E void *sqlite3F
36df2 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 0a 20  ts3HashInsert(. 
36df3 20 46 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20   Fts3Hash *pH,  
36df4 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73        /* The has
36df5 68 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65 72  h table to inser
36df6 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e 73  t into */.  cons
36df7 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20  t void *pKey,   
36df8 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20   /* The key */. 
36df9 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20 20   int nKey,      
36dfa 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
36dfb 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
36dfc 6b 65 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 64  key */.  void *d
36dfd 61 74 61 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ata           /*
36dfe 20 54 68 65 20 64 61 74 61 20 2a 2f 0a 29 7b 0a   The data */.){.
36dff 20 20 69 6e 74 20 68 72 61 77 3b 20 20 20 20 20    int hraw;     
36e00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
36e01 61 77 20 68 61 73 68 20 76 61 6c 75 65 20 6f 66  aw hash value of
36e02 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e   the key */.  in
36e03 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20 20  t h;            
36e04 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68          /* the h
36e05 61 73 68 20 6f 66 20 74 68 65 20 6b 65 79 20 6d  ash of the key m
36e06 6f 64 75 6c 6f 20 68 61 73 68 20 74 61 62 6c 65  odulo hash table
36e07 20 73 69 7a 65 20 2a 2f 0a 20 20 46 74 73 33 48   size */.  Fts3H
36e08 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20  ashElem *elem;  
36e09 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20       /* Used to 
36e0a 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c  loop thru the el
36e0b 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20  ement list */.  
36e0c 46 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e 65  Fts3HashElem *ne
36e0d 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65 77  w_elem;   /* New
36e0e 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64 20 74   element added t
36e0f 6f 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e  o the pH */.  in
36e10 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e 73 74  t (*xHash)(const
36e11 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a   void*,int);  /*
36e12 20 54 68 65 20 68 61 73 68 20 66 75 6e 63 74 69   The hash functi
36e13 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  on */..  assert(
36e14 20 70 48 21 3d 30 20 29 3b 0a 20 20 78 48 61 73   pH!=0 );.  xHas
36e15 68 20 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74  h = ftsHashFunct
36e16 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73  ion(pH->keyClass
36e17 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78 48 61  );.  assert( xHa
36e18 73 68 21 3d 30 20 29 3b 0a 20 20 68 72 61 77 20  sh!=0 );.  hraw 
36e19 3d 20 28 2a 78 48 61 73 68 29 28 70 4b 65 79 2c  = (*xHash)(pKey,
36e1a 20 6e 4b 65 79 29 3b 0a 20 20 61 73 73 65 72 74   nKey);.  assert
36e1b 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 20 26 20  ( (pH->htsize & 
36e1c 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29 3d  (pH->htsize-1))=
36e1d 3d 30 20 29 3b 0a 20 20 68 20 3d 20 68 72 61 77  =0 );.  h = hraw
36e1e 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31   & (pH->htsize-1
36e1f 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66 74 73 33  );.  elem = fts3
36e20 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61 73  FindElementByHas
36e21 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 68  h(pH,pKey,nKey,h
36e22 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29 7b  );.  if( elem ){
36e23 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f 64  .    void *old_d
36e24 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74 61  ata = elem->data
36e25 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d  ;.    if( data==
36e26 30 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 52  0 ){.      fts3R
36e27 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 42 79 48 61  emoveElementByHa
36e28 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20  sh(pH,elem,h);. 
36e29 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
36e2a 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74  elem->data = dat
36e2b 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  a;.    }.    ret
36e2c 75 72 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20  urn old_data;.  
36e2d 7d 0a 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20  }.  if( data==0 
36e2e 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
36e2f 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30  ( (pH->htsize==0
36e30 20 26 26 20 66 74 73 33 52 65 68 61 73 68 28 70   && fts3Rehash(p
36e31 48 2c 38 29 29 0a 20 20 20 7c 7c 20 28 70 48 2d  H,8)).   || (pH-
36e32 3e 63 6f 75 6e 74 3e 3d 70 48 2d 3e 68 74 73 69  >count>=pH->htsi
36e33 7a 65 20 26 26 20 66 74 73 33 52 65 68 61 73 68  ze && fts3Rehash
36e34 28 70 48 2c 20 70 48 2d 3e 68 74 73 69 7a 65 2a  (pH, pH->htsize*
36e35 32 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 48 2d  2)).  ){.    pH-
36e36 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 20 20 20 20  >count = 0;.    
36e37 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 7d  return data;.  }
36e38 0a 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 68  .  assert( pH->h
36e39 74 73 69 7a 65 3e 30 20 29 3b 0a 20 20 6e 65 77  tsize>0 );.  new
36e3a 5f 65 6c 65 6d 20 3d 20 28 46 74 73 33 48 61 73  _elem = (Fts3Has
36e3b 68 45 6c 65 6d 2a 29 66 74 73 33 48 61 73 68 4d  hElem*)fts3HashM
36e3c 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66 28 46 74  alloc( sizeof(Ft
36e3d 73 33 48 61 73 68 45 6c 65 6d 29 20 29 3b 0a 20  s3HashElem) );. 
36e3e 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d 3d 30   if( new_elem==0
36e3f 20 29 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a   ) return data;.
36e40 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65    if( pH->copyKe
36e41 79 20 26 26 20 70 4b 65 79 21 3d 30 20 29 7b 0a  y && pKey!=0 ){.
36e42 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b      new_elem->pK
36e43 65 79 20 3d 20 66 74 73 33 48 61 73 68 4d 61 6c  ey = fts3HashMal
36e44 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a 20 20 20  loc( nKey );.   
36e45 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 2d 3e 70   if( new_elem->p
36e46 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Key==0 ){.      
36e47 66 74 73 33 48 61 73 68 46 72 65 65 28 6e 65 77  fts3HashFree(new
36e48 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 72 65  _elem);.      re
36e49 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 20 7d  turn data;.    }
36e4a 0a 20 20 20 20 6d 65 6d 63 70 79 28 28 76 6f 69  .    memcpy((voi
36e4b 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65  d*)new_elem->pKe
36e4c 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a  y, pKey, nKey);.
36e4d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 77    }else{.    new
36e4e 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28 76  _elem->pKey = (v
36e4f 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20 7d 0a 20  oid*)pKey;.  }. 
36e50 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79 20   new_elem->nKey 
36e51 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d 3e 63 6f  = nKey;.  pH->co
36e52 75 6e 74 2b 2b 3b 0a 20 20 61 73 73 65 72 74 28  unt++;.  assert(
36e53 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b   pH->htsize>0 );
36e54 0a 20 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e  .  assert( (pH->
36e55 68 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74  htsize & (pH->ht
36e56 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20  size-1))==0 );. 
36e57 20 68 20 3d 20 68 72 61 77 20 26 20 28 70 48 2d   h = hraw & (pH-
36e58 3e 68 74 73 69 7a 65 2d 31 29 3b 0a 20 20 66 74  >htsize-1);.  ft
36e59 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65 6d  s3HashInsertElem
36e5a 65 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74 5b  ent(pH, &pH->ht[
36e5b 68 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20  h], new_elem);. 
36e5c 20 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61 20   new_elem->data 
36e5d 3d 20 64 61 74 61 3b 0a 20 20 72 65 74 75 72 6e  = data;.  return
36e5e 20 30 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a   0;.}..#endif /*
36e5f 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
36e60 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
36e61 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
36e62 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  FTS3) */../*****
36e63 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
36e64 20 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a 2a   fts3_hash.c ***
36e65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e67 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
36e68 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
36e69 66 69 6c 65 20 66 74 73 33 5f 70 6f 72 74 65 72  file fts3_porter
36e6a 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
36e6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e6c 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
36e6d 32 30 30 36 20 53 65 70 74 65 6d 62 65 72 20 33  2006 September 3
36e6e 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  0.**.** The auth
36e6f 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
36e70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
36e71 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
36e72 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
36e73 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
36e74 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
36e75 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
36e76 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
36e77 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
36e78 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
36e79 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
36e7a 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
36e7b 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
36e7c 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
36e7d 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
36e7e 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
36e7f 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
36e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e84 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
36e85 6f 6e 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d 74  on of the full-t
36e86 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b 65 6e  ext-search token
36e87 69 7a 65 72 20 74 68 61 74 20 69 6d 70 6c 65 6d  izer that implem
36e88 65 6e 74 73 0a 2a 2a 20 61 20 50 6f 72 74 65 72  ents.** a Porter
36e89 20 73 74 65 6d 6d 65 72 2e 0a 2a 2f 0a 0a 2f 2a   stemmer..*/../*
36e8a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
36e8b 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
36e8c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a  y compiled if:.*
36e8d 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
36e8e 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
36e8f 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20  ing built as an 
36e90 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20  extension.**    
36e91 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73     (in which cas
36e92 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73  e SQLITE_CORE is
36e93 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f   not defined), o
36e94 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  r.**.**     * Th
36e95 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
36e96 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74   being built int
36e97 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a  o the core of.**
36e98 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69         SQLite (i
36e99 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
36e9a 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20  ITE_ENABLE_FTS3 
36e9b 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a  is defined)..*/.
36e9c 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
36e9d 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
36e9e 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
36e9f 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 0a 2f 2a 0a  LE_FTS3)...../*.
36ea0 2a 2a 20 43 6c 61 73 73 20 64 65 72 69 76 65 64  ** Class derived
36ea1 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 74 6f   from sqlite3_to
36ea2 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 74 79 70 65 64  kenizer.*/.typed
36ea3 65 66 20 73 74 72 75 63 74 20 70 6f 72 74 65 72  ef struct porter
36ea4 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 73  _tokenizer {.  s
36ea5 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
36ea6 20 62 61 73 65 3b 20 20 20 20 20 20 2f 2a 20 42   base;      /* B
36ea7 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 7d 20 70  ase class */.} p
36ea8 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 3b  orter_tokenizer;
36ea9 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73 20 64 65  ../*.** Class de
36eaa 72 69 76 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  rived from sqlit
36eab 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
36eac 6f 72 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  or.*/.typedef st
36ead 72 75 63 74 20 70 6f 72 74 65 72 5f 74 6f 6b 65  ruct porter_toke
36eae 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a 20  nizer_cursor {. 
36eaf 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
36eb0 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 0a  er_cursor base;.
36eb1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49    const char *zI
36eb2 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20 2f  nput;          /
36eb3 2a 20 69 6e 70 75 74 20 77 65 20 61 72 65 20 74  * input we are t
36eb4 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20 69  okenizing */.  i
36eb5 6e 74 20 6e 49 6e 70 75 74 3b 20 20 20 20 20 20  nt nInput;      
36eb6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
36eb7 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74  ize of the input
36eb8 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65   */.  int iOffse
36eb9 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
36eba 20 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 70 6f     /* current po
36ebb 73 69 74 69 6f 6e 20 69 6e 20 7a 49 6e 70 75 74  sition in zInput
36ebc 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e   */.  int iToken
36ebd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36ebe 20 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20 6e     /* index of n
36ebf 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65 20  ext token to be 
36ec0 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63 68  returned */.  ch
36ec1 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20  ar *zToken;     
36ec2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74             /* st
36ec3 6f 72 61 67 65 20 66 6f 72 20 63 75 72 72 65 6e  orage for curren
36ec4 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  t token */.  int
36ec5 20 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20 20 20   nAllocated;    
36ec6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 70 61            /* spa
36ec7 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  ce allocated to 
36ec8 7a 54 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a 2f  zToken buffer */
36ec9 0a 7d 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  .} porter_tokeni
36eca 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f 2a  zer_cursor;.../*
36ecb 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
36ecc 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61   tokenizer insta
36ecd 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  nce..*/.static i
36ece 6e 74 20 70 6f 72 74 65 72 43 72 65 61 74 65 28  nt porterCreate(
36ecf 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e  .  int argc, con
36ed0 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20  st char * const 
36ed1 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65 33  *argv,.  sqlite3
36ed2 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54  _tokenizer **ppT
36ed3 6f 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 70 6f  okenizer.){.  po
36ed4 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  rter_tokenizer *
36ed5 74 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  t;..  UNUSED_PAR
36ed6 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
36ed7 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
36ed8 28 61 72 67 76 29 3b 0a 0a 20 20 74 20 3d 20 28  (argv);..  t = (
36ed9 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
36eda 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   *) sqlite3_mall
36edb 6f 63 28 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a  oc(sizeof(*t));.
36edc 20 20 69 66 28 20 74 3d 3d 4e 55 4c 4c 20 29 20    if( t==NULL ) 
36edd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
36ede 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 74 2c  MEM;.  memset(t,
36edf 20 30 2c 20 73 69 7a 65 6f 66 28 2a 74 29 29 3b   0, sizeof(*t));
36ee0 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20  .  *ppTokenizer 
36ee1 3d 20 26 74 2d 3e 62 61 73 65 3b 0a 20 20 72 65  = &t->base;.  re
36ee2 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
36ee3 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79  }../*.** Destroy
36ee4 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a   a tokenizer.*/.
36ee5 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65  static int porte
36ee6 72 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33  rDestroy(sqlite3
36ee7 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
36ee8 65 6e 69 7a 65 72 29 7b 0a 20 20 73 71 6c 69 74  enizer){.  sqlit
36ee9 65 33 5f 66 72 65 65 28 70 54 6f 6b 65 6e 69 7a  e3_free(pTokeniz
36eea 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  er);.  return SQ
36eeb 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
36eec 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62 65 67  * Prepare to beg
36eed 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 20  in tokenizing a 
36eee 70 61 72 74 69 63 75 6c 61 72 20 73 74 72 69 6e  particular strin
36eef 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a 2a  g.  The input.**
36ef0 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74 6f   string to be to
36ef1 6b 65 6e 69 7a 65 64 20 69 73 20 7a 49 6e 70 75  kenized is zInpu
36ef2 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d 2e 20  t[0..nInput-1]. 
36ef3 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73 65   A cursor.** use
36ef4 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61 6c  d to incremental
36ef5 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69 73  ly tokenize this
36ef6 20 73 74 72 69 6e 67 20 69 73 20 72 65 74 75 72   string is retur
36ef7 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43 75  ned in .** *ppCu
36ef8 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rsor..*/.static 
36ef9 69 6e 74 20 70 6f 72 74 65 72 4f 70 65 6e 28 0a  int porterOpen(.
36efa 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
36efb 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c  zer *pTokenizer,
36efc 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
36efd 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63  tokenizer */.  c
36efe 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75  onst char *zInpu
36eff 74 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20  t, int nInput,  
36f00 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20        /* String 
36f01 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20  to be tokenized 
36f02 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  */.  sqlite3_tok
36f03 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a  enizer_cursor **
36f04 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f  ppCursor    /* O
36f05 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e  UT: Tokenization
36f06 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20   cursor */.){.  
36f07 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72  porter_tokenizer
36f08 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20 20 55  _cursor *c;..  U
36f09 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
36f0a 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20 20  pTokenizer);..  
36f0b 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65  c = (porter_toke
36f0c 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20  nizer_cursor *) 
36f0d 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
36f0e 69 7a 65 6f 66 28 2a 63 29 29 3b 0a 20 20 69 66  izeof(*c));.  if
36f0f 28 20 63 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75  ( c==NULL ) retu
36f10 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
36f11 0a 0a 20 20 63 2d 3e 7a 49 6e 70 75 74 20 3d 20  ..  c->zInput = 
36f12 7a 49 6e 70 75 74 3b 0a 20 20 69 66 28 20 7a 49  zInput;.  if( zI
36f13 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 63  nput==0 ){.    c
36f14 2d 3e 6e 49 6e 70 75 74 20 3d 20 30 3b 0a 20 20  ->nInput = 0;.  
36f15 7d 65 6c 73 65 20 69 66 28 20 6e 49 6e 70 75 74  }else if( nInput
36f16 3c 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e  <0 ){.    c->nIn
36f17 70 75 74 20 3d 20 28 69 6e 74 29 73 74 72 6c 65  put = (int)strle
36f18 6e 28 7a 49 6e 70 75 74 29 3b 0a 20 20 7d 65 6c  n(zInput);.  }el
36f19 73 65 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70 75  se{.    c->nInpu
36f1a 74 20 3d 20 6e 49 6e 70 75 74 3b 0a 20 20 7d 0a  t = nInput;.  }.
36f1b 20 20 63 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30    c->iOffset = 0
36f1c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36f1d 20 20 2f 2a 20 73 74 61 72 74 20 74 6f 6b 65 6e    /* start token
36f1e 69 7a 69 6e 67 20 61 74 20 74 68 65 20 62 65 67  izing at the beg
36f1f 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d 3e 69  inning */.  c->i
36f20 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 63 2d 3e  Token = 0;.  c->
36f21 7a 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b 20 20  zToken = NULL;  
36f22 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36f23 6e 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  no space allocat
36f24 65 64 2c 20 79 65 74 2e 20 2a 2f 0a 20 20 63 2d  ed, yet. */.  c-
36f25 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b  >nAllocated = 0;
36f26 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20  ..  *ppCursor = 
36f27 26 63 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75  &c->base;.  retu
36f28 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
36f29 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74  ./*.** Close a t
36f2a 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73  okenization curs
36f2b 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70  or previously op
36f2c 65 6e 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74  ened by a call t
36f2d 6f 0a 2a 2a 20 70 6f 72 74 65 72 4f 70 65 6e 28  o.** porterOpen(
36f2e 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74  ) above..*/.stat
36f2f 69 63 20 69 6e 74 20 70 6f 72 74 65 72 43 6c 6f  ic int porterClo
36f30 73 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  se(sqlite3_token
36f31 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
36f32 72 73 6f 72 29 7b 0a 20 20 70 6f 72 74 65 72 5f  rsor){.  porter_
36f33 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
36f34 20 2a 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f   *c = (porter_to
36f35 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
36f36 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c  ) pCursor;.  sql
36f37 69 74 65 33 5f 66 72 65 65 28 63 2d 3e 7a 54 6f  ite3_free(c->zTo
36f38 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ken);.  sqlite3_
36f39 66 72 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72  free(c);.  retur
36f3a 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f  n SQLITE_OK;.}./
36f3b 2a 0a 2a 2a 20 56 6f 77 65 6c 20 6f 72 20 63 6f  *.** Vowel or co
36f3c 6e 73 6f 6e 61 6e 74 0a 2a 2f 0a 73 74 61 74 69  nsonant.*/.stati
36f3d 63 20 63 6f 6e 73 74 20 63 68 61 72 20 63 54 79  c const char cTy
36f3e 70 65 5b 5d 20 3d 20 7b 0a 20 20 20 30 2c 20 31  pe[] = {.   0, 1
36f3f 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c  , 1, 1, 0, 1, 1,
36f40 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 0, 1, 1, 1, 
36f41 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
36f42 2c 20 31 2c 20 31 2c 20 30 2c 0a 20 20 20 31 2c  , 1, 1, 0,.   1,
36f43 20 31 2c 20 31 2c 20 32 2c 20 31 0a 7d 3b 0a 0a   1, 1, 2, 1.};..
36f44 2f 2a 0a 2a 2a 20 69 73 43 6f 6e 73 6f 6e 61 6e  /*.** isConsonan
36f45 74 28 29 20 61 6e 64 20 69 73 56 6f 77 65 6c 28  t() and isVowel(
36f46 29 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  ) determine if t
36f47 68 65 69 72 20 66 69 72 73 74 20 63 68 61 72 61  heir first chara
36f48 63 74 65 72 20 69 6e 0a 2a 2a 20 74 68 65 20 73  cter in.** the s
36f49 74 72 69 6e 67 20 74 68 65 79 20 70 6f 69 6e 74  tring they point
36f4a 20 74 6f 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61   to is a consona
36f4b 6e 74 20 6f 72 20 61 20 76 6f 77 65 6c 2c 20 61  nt or a vowel, a
36f4c 63 63 6f 72 64 69 6e 67 0a 2a 2a 20 74 6f 20 50  ccording.** to P
36f4d 6f 72 74 65 72 20 72 75 6c 73 2e 20 20 0a 2a 2a  orter ruls.  .**
36f4e 0a 2a 2a 20 41 20 63 6f 6e 73 6f 6e 61 74 65 20  .** A consonate 
36f4f 69 73 20 61 6e 79 20 6c 65 74 74 65 72 20 6f 74  is any letter ot
36f50 68 65 72 20 74 68 61 6e 20 27 61 27 2c 20 27 65  her than 'a', 'e
36f51 27 2c 20 27 69 27 2c 20 27 6f 27 2c 20 6f 72 20  ', 'i', 'o', or 
36f52 27 75 27 2e 0a 2a 2a 20 27 59 27 20 69 73 20 61  'u'..** 'Y' is a
36f53 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65 73   consonant unles
36f54 73 20 69 74 20 66 6f 6c 6c 6f 77 73 20 61 6e 6f  s it follows ano
36f55 74 68 65 72 20 63 6f 6e 73 6f 6e 61 6e 74 2c 0a  ther consonant,.
36f56 2a 2a 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  ** in which case
36f57 20 69 74 20 69 73 20 61 20 76 6f 77 65 6c 2e 0a   it is a vowel..
36f58 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 73 65 20 72  **.** In these r
36f59 6f 75 74 69 6e 65 2c 20 74 68 65 20 6c 65 74 74  outine, the lett
36f5a 65 72 73 20 61 72 65 20 69 6e 20 72 65 76 65 72  ers are in rever
36f5b 73 65 20 6f 72 64 65 72 2e 20 20 53 6f 20 74 68  se order.  So th
36f5c 65 20 27 79 27 20 72 75 6c 65 0a 2a 2a 20 69 73  e 'y' rule.** is
36f5d 20 74 68 61 74 20 27 79 27 20 69 73 20 61 20 63   that 'y' is a c
36f5e 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65 73 73 20  onsonant unless 
36f5f 69 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62  it is followed b
36f60 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f 6e  y another.** con
36f61 73 6f 6e 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  sonent..*/.stati
36f62 63 20 69 6e 74 20 69 73 56 6f 77 65 6c 28 63 6f  c int isVowel(co
36f63 6e 73 74 20 63 68 61 72 2a 29 3b 0a 73 74 61 74  nst char*);.stat
36f64 69 63 20 69 6e 74 20 69 73 43 6f 6e 73 6f 6e 61  ic int isConsona
36f65 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  nt(const char *z
36f66 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63 68  ){.  int j;.  ch
36f67 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66 28  ar x = *z;.  if(
36f68 20 78 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   x==0 ) return 0
36f69 3b 0a 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27  ;.  assert( x>='
36f6a 61 27 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0a  a' && x<='z' );.
36f6b 20 20 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61    j = cType[x-'a
36f6c 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29 20  '];.  if( j<2 ) 
36f6d 72 65 74 75 72 6e 20 6a 3b 0a 20 20 72 65 74 75  return j;.  retu
36f6e 72 6e 20 7a 5b 31 5d 3d 3d 30 20 7c 7c 20 69 73  rn z[1]==0 || is
36f6f 56 6f 77 65 6c 28 7a 20 2b 20 31 29 3b 0a 7d 0a  Vowel(z + 1);.}.
36f70 73 74 61 74 69 63 20 69 6e 74 20 69 73 56 6f 77  static int isVow
36f71 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  el(const char *z
36f72 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63 68  ){.  int j;.  ch
36f73 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66 28  ar x = *z;.  if(
36f74 20 78 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   x==0 ) return 0
36f75 3b 0a 20 20 61 73 73 65 72 74 28 20 78 3e 3d 27  ;.  assert( x>='
36f76 61 27 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b 0a  a' && x<='z' );.
36f77 20 20 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27 61    j = cType[x-'a
36f78 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29 20  '];.  if( j<2 ) 
36f79 72 65 74 75 72 6e 20 31 2d 6a 3b 0a 20 20 72 65  return 1-j;.  re
36f7a 74 75 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  turn isConsonant
36f7b 28 7a 20 2b 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (z + 1);.}../*.*
36f7c 2a 20 4c 65 74 20 61 6e 79 20 73 65 71 75 65 6e  * Let any sequen
36f7d 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72  ce of one or mor
36f7e 65 20 76 6f 77 65 6c 73 20 62 65 20 72 65 70 72  e vowels be repr
36f7f 65 73 65 6e 74 65 64 20 62 79 20 56 20 61 6e 64  esented by V and
36f80 20 6c 65 74 0a 2a 2a 20 43 20 62 65 20 73 65 71   let.** C be seq
36f81 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20  uence of one or 
36f82 6d 6f 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 73 2e  more consonants.
36f83 20 20 54 68 65 6e 20 65 76 65 72 79 20 77 6f 72    Then every wor
36f84 64 20 63 61 6e 20 62 65 0a 2a 2a 20 72 65 70 72  d can be.** repr
36f85 65 73 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a 2a  esented as:.**.*
36f86 2a 20 20 20 20 20 20 20 20 20 20 20 5b 43 5d 20  *           [C] 
36f87 28 56 43 29 7b 6d 7d 20 5b 56 5d 0a 2a 2a 0a 2a  (VC){m} [V].**.*
36f88 2a 20 49 6e 20 70 72 6f 73 65 3a 20 20 41 20 77  * In prose:  A w
36f89 6f 72 64 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e  ord is an option
36f8a 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c  al consonant fol
36f8b 6c 6f 77 65 64 20 62 79 20 7a 65 72 6f 20 6f 72  lowed by zero or
36f8c 0a 2a 2a 20 76 6f 77 65 6c 2d 63 6f 6e 73 6f 6e  .** vowel-conson
36f8d 61 6e 74 20 70 61 69 72 73 20 66 6f 6c 6c 6f 77  ant pairs follow
36f8e 65 64 20 62 79 20 61 6e 20 6f 70 74 69 6f 6e 61  ed by an optiona
36f8f 6c 20 76 6f 77 65 6c 2e 20 20 22 6d 22 20 69 73  l vowel.  "m" is
36f90 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   the.** number o
36f91 66 20 76 6f 77 65 6c 20 63 6f 6e 73 6f 6e 61 6e  f vowel consonan
36f92 74 20 70 61 69 72 73 2e 20 20 54 68 69 73 20 72  t pairs.  This r
36f93 6f 75 74 69 6e 65 20 63 6f 6d 70 75 74 65 73 20  outine computes 
36f94 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  the value.** of 
36f95 6d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  m for the first 
36f96 69 20 62 79 74 65 73 20 6f 66 20 61 20 77 6f 72  i bytes of a wor
36f97 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  d..**.** Return 
36f98 74 72 75 65 20 69 66 20 74 68 65 20 6d 2d 76 61  true if the m-va
36f99 6c 75 65 20 66 6f 72 20 7a 20 69 73 20 31 20 6f  lue for z is 1 o
36f9a 72 20 6d 6f 72 65 2e 20 20 49 6e 20 6f 74 68 65  r more.  In othe
36f9b 72 20 77 6f 72 64 73 2c 0a 2a 2a 20 72 65 74 75  r words,.** retu
36f9c 72 6e 20 74 72 75 65 20 69 66 20 7a 20 63 6f 6e  rn true if z con
36f9d 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20 6f  tains at least o
36f9e 6e 65 20 76 6f 77 65 6c 20 74 68 61 74 20 69 73  ne vowel that is
36f9f 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20   followed.** by 
36fa0 61 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a 0a  a consonant..**.
36fa1 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f 75 74 69  ** In this routi
36fa2 6e 65 20 7a 5b 5d 20 69 73 20 69 6e 20 72 65 76  ne z[] is in rev
36fa3 65 72 73 65 20 6f 72 64 65 72 2e 20 20 53 6f 20  erse order.  So 
36fa4 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6c 6f  we are really lo
36fa5 6f 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 6e 20  oking.** for an 
36fa6 69 6e 73 74 61 6e 63 65 20 6f 66 20 6f 66 20 61  instance of of a
36fa7 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c 6c 6f   consonant follo
36fa8 77 65 64 20 62 79 20 61 20 76 6f 77 65 6c 2e 0a  wed by a vowel..
36fa9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f  */.static int m_
36faa 67 74 5f 30 28 63 6f 6e 73 74 20 63 68 61 72 20  gt_0(const char 
36fab 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73  *z){.  while( is
36fac 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b  Vowel(z) ){ z++;
36fad 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29   }.  if( *z==0 )
36fae 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69   return 0;.  whi
36faf 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  le( isConsonant(
36fb0 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72  z) ){ z++; }.  r
36fb1 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a  eturn *z!=0;.}..
36fb2 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20 61 62 6f  /* Like mgt0 abo
36fb3 76 65 20 65 78 63 65 70 74 20 77 65 20 61 72 65  ve except we are
36fb4 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 76   looking for a v
36fb5 61 6c 75 65 20 6f 66 20 6d 20 77 68 69 63 68 20  alue of m which 
36fb6 69 73 0a 2a 2a 20 65 78 61 63 74 6c 79 20 31 0a  is.** exactly 1.
36fb7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f  */.static int m_
36fb8 65 71 5f 31 28 63 6f 6e 73 74 20 63 68 61 72 20  eq_1(const char 
36fb9 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73  *z){.  while( is
36fba 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b  Vowel(z) ){ z++;
36fbb 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29   }.  if( *z==0 )
36fbc 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69   return 0;.  whi
36fbd 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  le( isConsonant(
36fbe 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69  z) ){ z++; }.  i
36fbf 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72  f( *z==0 ) retur
36fc0 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73  n 0;.  while( is
36fc1 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b  Vowel(z) ){ z++;
36fc2 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29   }.  if( *z==0 )
36fc3 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 77 68 69   return 1;.  whi
36fc4 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28  le( isConsonant(
36fc5 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72  z) ){ z++; }.  r
36fc6 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a  eturn *z==0;.}..
36fc7 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20 61 62 6f  /* Like mgt0 abo
36fc8 76 65 20 65 78 63 65 70 74 20 77 65 20 61 72 65  ve except we are
36fc9 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 76   looking for a v
36fca 61 6c 75 65 20 6f 66 20 6d 3e 31 20 69 6e 73 74  alue of m>1 inst
36fcb 65 61 64 0a 2a 2a 20 6f 72 20 6d 3e 30 0a 2a 2f  ead.** or m>0.*/
36fcc 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f 67 74  .static int m_gt
36fcd 5f 31 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  _1(const char *z
36fce 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f  ){.  while( isVo
36fcf 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  wel(z) ){ z++; }
36fd0 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72  .  if( *z==0 ) r
36fd1 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65  eturn 0;.  while
36fd2 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
36fd3 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
36fd4 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
36fd5 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 56 6f  0;.  while( isVo
36fd6 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  wel(z) ){ z++; }
36fd7 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72  .  if( *z==0 ) r
36fd8 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65  eturn 0;.  while
36fd9 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29  ( isConsonant(z)
36fda 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74   ){ z++; }.  ret
36fdb 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a  urn *z!=0;.}../*
36fdc 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
36fdd 69 66 20 74 68 65 72 65 20 69 73 20 61 20 76 6f  if there is a vo
36fde 77 65 6c 20 61 6e 79 77 68 65 72 65 20 77 69 74  wel anywhere wit
36fdf 68 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 0a 2a 2f  hin z[0..n-1].*/
36fe0 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 56  .static int hasV
36fe1 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20  owel(const char 
36fe2 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73  *z){.  while( is
36fe3 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20  Consonant(z) ){ 
36fe4 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20  z++; }.  return 
36fe5 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  *z!=0;.}../*.** 
36fe6 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74  Return TRUE if t
36fe7 68 65 20 77 6f 72 64 20 65 6e 64 73 20 69 6e 20  he word ends in 
36fe8 61 20 64 6f 75 62 6c 65 20 63 6f 6e 73 6f 6e 61  a double consona
36fe9 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65  nt..**.** The te
36fea 78 74 20 69 73 20 72 65 76 65 72 73 65 64 20 68  xt is reversed h
36feb 65 72 65 2e 20 53 6f 20 77 65 20 61 72 65 20 72  ere. So we are r
36fec 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 20 61 74  eally looking at
36fed 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 77  .** the first tw
36fee 6f 20 63 68 61 72 61 63 74 65 72 73 20 6f 66 20  o characters of 
36fef 7a 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  z[]..*/.static i
36ff0 6e 74 20 64 6f 75 62 6c 65 43 6f 6e 73 6f 6e 61  nt doubleConsona
36ff1 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  nt(const char *z
36ff2 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 73 43 6f  ){.  return isCo
36ff3 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 20 7a 5b  nsonant(z) && z[
36ff4 30 5d 3d 3d 7a 5b 31 5d 3b 0a 7d 0a 0a 2f 2a 0a  0]==z[1];.}../*.
36ff5 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
36ff6 66 20 74 68 65 20 77 6f 72 64 20 65 6e 64 73 20  f the word ends 
36ff7 77 69 74 68 20 74 68 72 65 65 20 6c 65 74 74 65  with three lette
36ff8 72 73 20 77 68 69 63 68 0a 2a 2a 20 61 72 65 20  rs which.** are 
36ff9 63 6f 6e 73 6f 6e 61 6e 74 2d 76 6f 77 65 6c 2d  consonant-vowel-
36ffa 63 6f 6e 73 6f 6e 65 6e 74 20 61 6e 64 20 77 68  consonent and wh
36ffb 65 72 65 20 74 68 65 20 66 69 6e 61 6c 20 63 6f  ere the final co
36ffc 6e 73 6f 6e 61 6e 74 0a 2a 2a 20 69 73 20 6e 6f  nsonant.** is no
36ffd 74 20 27 77 27 2c 20 27 78 27 2c 20 6f 72 20 27  t 'w', 'x', or '
36ffe 79 27 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 6f  y'..**.** The wo
36fff 72 64 20 69 73 20 72 65 76 65 72 73 65 64 20 68  rd is reversed h
37000 65 72 65 2e 20 20 53 6f 20 77 65 20 61 72 65 20  ere.  So we are 
37001 72 65 61 6c 6c 79 20 63 68 65 63 6b 69 6e 67 20  really checking 
37002 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74 68 72  the.** first thr
37003 65 65 20 6c 65 74 74 65 72 73 20 61 6e 64 20 74  ee letters and t
37004 68 65 20 66 69 72 73 74 20 6f 6e 65 20 63 61 6e  he first one can
37005 6e 6f 74 20 62 65 20 69 6e 20 5b 77 78 79 5d 2e  not be in [wxy].
37006 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
37007 74 61 72 5f 6f 68 28 63 6f 6e 73 74 20 63 68 61  tar_oh(const cha
37008 72 20 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 0a  r *z){.  return.
37009 20 20 20 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28      isConsonant(
3700a 7a 29 20 26 26 0a 20 20 20 20 7a 5b 30 5d 21 3d  z) &&.    z[0]!=
3700b 27 77 27 20 26 26 20 7a 5b 30 5d 21 3d 27 78 27  'w' && z[0]!='x'
3700c 20 26 26 20 7a 5b 30 5d 21 3d 27 79 27 20 26 26   && z[0]!='y' &&
3700d 0a 20 20 20 20 69 73 56 6f 77 65 6c 28 7a 2b 31  .    isVowel(z+1
3700e 29 20 26 26 0a 20 20 20 20 69 73 43 6f 6e 73 6f  ) &&.    isConso
3700f 6e 61 6e 74 28 7a 2b 32 29 3b 0a 7d 0a 0a 2f 2a  nant(z+2);.}../*
37010 0a 2a 2a 20 49 66 20 74 68 65 20 77 6f 72 64 20  .** If the word 
37011 65 6e 64 73 20 77 69 74 68 20 7a 46 72 6f 6d 20  ends with zFrom 
37012 61 6e 64 20 78 43 6f 6e 64 28 29 20 69 73 20 74  and xCond() is t
37013 72 75 65 20 66 6f 72 20 74 68 65 20 73 74 65 6d  rue for the stem
37014 0a 2a 2a 20 6f 66 20 74 68 65 20 77 6f 72 64 20  .** of the word 
37015 74 68 61 74 20 70 72 65 63 65 65 64 73 20 74 68  that preceeds th
37016 65 20 7a 46 72 6f 6d 20 65 6e 64 69 6e 67 2c 20  e zFrom ending, 
37017 74 68 65 6e 20 63 68 61 6e 67 65 20 74 68 65 20  then change the 
37018 0a 2a 2a 20 65 6e 64 69 6e 67 20 74 6f 20 7a 54  .** ending to zT
37019 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70  o..**.** The inp
3701a 75 74 20 77 6f 72 64 20 2a 70 7a 20 61 6e 64 20  ut word *pz and 
3701b 7a 46 72 6f 6d 20 61 72 65 20 62 6f 74 68 20 69  zFrom are both i
3701c 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e  n reverse order.
3701d 20 20 7a 54 6f 0a 2a 2a 20 69 73 20 69 6e 20 6e    zTo.** is in n
3701e 6f 72 6d 61 6c 20 6f 72 64 65 72 2e 20 0a 2a 2a  ormal order. .**
3701f 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
37020 69 66 20 7a 46 72 6f 6d 20 6d 61 74 63 68 65 73  if zFrom matches
37021 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 20  .  Return FALSE 
37022 69 66 20 7a 46 72 6f 6d 20 64 6f 65 73 20 6e 6f  if zFrom does no
37023 74 0a 2a 2a 20 6d 61 74 63 68 2e 20 20 4e 6f 74  t.** match.  Not
37024 20 74 68 61 74 20 54 52 55 45 20 69 73 20 72 65   that TRUE is re
37025 74 75 72 6e 65 64 20 65 76 65 6e 20 69 66 20 78  turned even if x
37026 43 6f 6e 64 28 29 20 66 61 69 6c 73 20 61 6e 64  Cond() fails and
37027 0a 2a 2a 20 6e 6f 20 73 75 62 73 74 69 74 75 74  .** no substitut
37028 69 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73  ion occurs..*/.s
37029 74 61 74 69 63 20 69 6e 74 20 73 74 65 6d 28 0a  tatic int stem(.
3702a 20 20 63 68 61 72 20 2a 2a 70 7a 2c 20 20 20 20    char **pz,    
3702b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
3702c 77 6f 72 64 20 62 65 69 6e 67 20 73 74 65 6d 6d  word being stemm
3702d 65 64 20 28 52 65 76 65 72 73 65 64 29 20 2a 2f  ed (Reversed) */
3702e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3702f 46 72 6f 6d 2c 20 20 20 20 20 2f 2a 20 49 66 20  From,     /* If 
37030 74 68 65 20 65 6e 64 69 6e 67 20 6d 61 74 63 68  the ending match
37031 65 73 20 74 68 69 73 2e 2e 2e 20 28 52 65 76 65  es this... (Reve
37032 72 73 65 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74  rsed) */.  const
37033 20 63 68 61 72 20 2a 7a 54 6f 2c 20 20 20 20 20   char *zTo,     
37034 20 20 2f 2a 20 2e 2e 2e 20 63 68 61 6e 67 65 20    /* ... change 
37035 74 68 65 20 65 6e 64 69 6e 67 20 74 6f 20 74 68  the ending to th
37036 69 73 20 28 6e 6f 74 20 72 65 76 65 72 73 65 64  is (not reversed
37037 29 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f  ) */.  int (*xCo
37038 6e 64 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  nd)(const char*)
37039 20 20 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20     /* Condition 
3703a 74 68 61 74 20 6d 75 73 74 20 62 65 20 74 72 75  that must be tru
3703b 65 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a  e */.){.  char *
3703c 7a 20 3d 20 2a 70 7a 3b 0a 20 20 77 68 69 6c 65  z = *pz;.  while
3703d 28 20 2a 7a 46 72 6f 6d 20 26 26 20 2a 7a 46 72  ( *zFrom && *zFr
3703e 6f 6d 3d 3d 2a 7a 20 29 7b 20 7a 2b 2b 3b 20 7a  om==*z ){ z++; z
3703f 46 72 6f 6d 2b 2b 3b 20 7d 0a 20 20 69 66 28 20  From++; }.  if( 
37040 2a 7a 46 72 6f 6d 21 3d 30 20 29 20 72 65 74 75  *zFrom!=0 ) retu
37041 72 6e 20 30 3b 0a 20 20 69 66 28 20 78 43 6f 6e  rn 0;.  if( xCon
37042 64 20 26 26 20 21 78 43 6f 6e 64 28 7a 29 20 29  d && !xCond(z) )
37043 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 77 68 69   return 1;.  whi
37044 6c 65 28 20 2a 7a 54 6f 20 29 7b 0a 20 20 20 20  le( *zTo ){.    
37045 2a 28 2d 2d 7a 29 20 3d 20 2a 28 7a 54 6f 2b 2b  *(--z) = *(zTo++
37046 29 3b 0a 20 20 7d 0a 20 20 2a 70 7a 20 3d 20 7a  );.  }.  *pz = z
37047 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  ;.  return 1;.}.
37048 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ./*.** This is t
37049 68 65 20 66 61 6c 6c 62 61 63 6b 20 73 74 65 6d  he fallback stem
3704a 6d 65 72 20 75 73 65 64 20 77 68 65 6e 20 74 68  mer used when th
3704b 65 20 70 6f 72 74 65 72 20 73 74 65 6d 6d 65 72  e porter stemmer
3704c 20 69 73 0a 2a 2a 20 69 6e 61 70 70 72 6f 70 72   is.** inappropr
3704d 69 61 74 65 2e 20 20 54 68 65 20 69 6e 70 75 74  iate.  The input
3704e 20 77 6f 72 64 20 69 73 20 63 6f 70 69 65 64 20   word is copied 
3704f 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20  into the output 
37050 77 69 74 68 0a 2a 2a 20 55 53 2d 41 53 43 49 49  with.** US-ASCII
37051 20 63 61 73 65 20 66 6f 6c 64 69 6e 67 2e 20 20   case folding.  
37052 49 66 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72  If the input wor
37053 64 20 69 73 20 74 6f 6f 20 6c 6f 6e 67 20 28 6d  d is too long (m
37054 6f 72 65 0a 2a 2a 20 74 68 61 6e 20 32 30 20 62  ore.** than 20 b
37055 79 74 65 73 20 69 66 20 69 74 20 63 6f 6e 74 61  ytes if it conta
37056 69 6e 73 20 6e 6f 20 64 69 67 69 74 73 20 6f 72  ins no digits or
37057 20 6d 6f 72 65 20 74 68 61 6e 20 36 20 62 79 74   more than 6 byt
37058 65 73 20 69 66 0a 2a 2a 20 69 74 20 63 6f 6e 74  es if.** it cont
37059 61 69 6e 73 20 64 69 67 69 74 73 29 20 74 68 65  ains digits) the
3705a 6e 20 77 6f 72 64 20 69 73 20 74 72 75 6e 63 61  n word is trunca
3705b 74 65 64 20 74 6f 20 32 30 20 6f 72 20 36 20 62  ted to 20 or 6 b
3705c 79 74 65 73 0a 2a 2a 20 62 79 20 74 61 6b 69 6e  ytes.** by takin
3705d 67 20 31 30 20 6f 72 20 33 20 62 79 74 65 73 20  g 10 or 3 bytes 
3705e 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69  from the beginni
3705f 6e 67 20 61 6e 64 20 65 6e 64 2e 0a 2a 2f 0a 73  ng and end..*/.s
37060 74 61 74 69 63 20 76 6f 69 64 20 63 6f 70 79 5f  tatic void copy_
37061 73 74 65 6d 6d 65 72 28 63 6f 6e 73 74 20 63 68  stemmer(const ch
37062 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e  ar *zIn, int nIn
37063 2c 20 63 68 61 72 20 2a 7a 4f 75 74 2c 20 69 6e  , char *zOut, in
37064 74 20 2a 70 6e 4f 75 74 29 7b 0a 20 20 69 6e 74  t *pnOut){.  int
37065 20 69 2c 20 6d 78 2c 20 6a 3b 0a 20 20 69 6e 74   i, mx, j;.  int
37066 20 68 61 73 44 69 67 69 74 20 3d 20 30 3b 0a 20   hasDigit = 0;. 
37067 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 49 6e 3b   for(i=0; i<nIn;
37068 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20   i++){.    char 
37069 63 20 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20  c = zIn[i];.    
3706a 69 66 28 20 63 3e 3d 27 41 27 20 26 26 20 63 3c  if( c>='A' && c<
3706b 3d 27 5a 27 20 29 7b 0a 20 20 20 20 20 20 7a 4f  ='Z' ){.      zO
3706c 75 74 5b 69 5d 20 3d 20 63 20 2d 20 27 41 27 20  ut[i] = c - 'A' 
3706d 2b 20 27 61 27 3b 0a 20 20 20 20 7d 65 6c 73 65  + 'a';.    }else
3706e 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3e 3d 27  {.      if( c>='
3706f 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 20 68  0' && c<='9' ) h
37070 61 73 44 69 67 69 74 20 3d 20 31 3b 0a 20 20 20  asDigit = 1;.   
37071 20 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 63 3b 0a     zOut[i] = c;.
37072 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6d 78 20 3d      }.  }.  mx =
37073 20 68 61 73 44 69 67 69 74 20 3f 20 33 20 3a 20   hasDigit ? 3 : 
37074 31 30 3b 0a 20 20 69 66 28 20 6e 49 6e 3e 6d 78  10;.  if( nIn>mx
37075 2a 32 20 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d  *2 ){.    for(j=
37076 6d 78 2c 20 69 3d 6e 49 6e 2d 6d 78 3b 20 69 3c  mx, i=nIn-mx; i<
37077 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a  nIn; i++, j++){.
37078 20 20 20 20 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20        zOut[j] = 
37079 7a 4f 75 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20  zOut[i];.    }. 
3707a 20 20 20 69 20 3d 20 6a 3b 0a 20 20 7d 0a 20 20     i = j;.  }.  
3707b 7a 4f 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a  zOut[i] = 0;.  *
3707c 70 6e 4f 75 74 20 3d 20 69 3b 0a 7d 0a 0a 0a 2f  pnOut = i;.}.../
3707d 2a 0a 2a 2a 20 53 74 65 6d 20 74 68 65 20 69 6e  *.** Stem the in
3707e 70 75 74 20 77 6f 72 64 20 7a 49 6e 5b 30 2e 2e  put word zIn[0..
3707f 6e 49 6e 2d 31 5d 2e 20 20 53 74 6f 72 65 20 74  nIn-1].  Store t
37080 68 65 20 6f 75 74 70 75 74 20 69 6e 20 7a 4f 75  he output in zOu
37081 74 2e 0a 2a 2a 20 7a 4f 75 74 20 69 73 20 61 74  t..** zOut is at
37082 20 6c 65 61 73 74 20 62 69 67 20 65 6e 6f 75 67   least big enoug
37083 68 20 74 6f 20 68 6f 6c 64 20 6e 49 6e 20 62 79  h to hold nIn by
37084 74 65 73 2e 20 20 57 72 69 74 65 20 74 68 65 20  tes.  Write the 
37085 61 63 74 75 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f  actual.** size o
37086 66 20 74 68 65 20 6f 75 74 70 75 74 20 77 6f 72  f the output wor
37087 64 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20  d (exclusive of 
37088 74 68 65 20 27 5c 30 27 20 74 65 72 6d 69 6e 61  the '\0' termina
37089 74 6f 72 29 20 69 6e 74 6f 20 2a 70 6e 4f 75 74  tor) into *pnOut
3708a 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79 20 75 70 70 65  ..**.** Any uppe
3708b 72 2d 63 61 73 65 20 63 68 61 72 61 63 74 65 72  r-case character
3708c 73 20 69 6e 20 74 68 65 20 55 53 2d 41 53 43 49  s in the US-ASCI
3708d 49 20 63 68 61 72 61 63 74 65 72 20 73 65 74 20  I character set 
3708e 28 5b 41 2d 5a 5d 29 0a 2a 2a 20 61 72 65 20 63  ([A-Z]).** are c
3708f 6f 6e 76 65 72 74 65 64 20 74 6f 20 6c 6f 77 65  onverted to lowe
37090 72 20 63 61 73 65 2e 20 20 55 70 70 65 72 2d 63  r case.  Upper-c
37091 61 73 65 20 55 54 46 20 63 68 61 72 61 63 74 65  ase UTF characte
37092 72 73 20 61 72 65 0a 2a 2a 20 75 6e 63 68 61 6e  rs are.** unchan
37093 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 6f 72 64 73  ged..**.** Words
37094 20 74 68 61 74 20 61 72 65 20 6c 6f 6e 67 65 72   that are longer
37095 20 74 68 61 6e 20 61 62 6f 75 74 20 32 30 20 62   than about 20 b
37096 79 74 65 73 20 61 72 65 20 73 74 65 6d 6d 65 64  ytes are stemmed
37097 20 62 79 20 72 65 74 61 69 6e 69 6e 67 0a 2a 2a   by retaining.**
37098 20 61 20 66 65 77 20 62 79 74 65 73 20 66 72 6f   a few bytes fro
37099 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  m the beginning 
3709a 61 6e 64 20 74 68 65 20 65 6e 64 20 6f 66 20 74  and the end of t
3709b 68 65 20 77 6f 72 64 2e 20 20 49 66 20 74 68 65  he word.  If the
3709c 0a 2a 2a 20 77 6f 72 64 20 63 6f 6e 74 61 69 6e  .** word contain
3709d 73 20 64 69 67 69 74 73 2c 20 33 20 62 79 74 65  s digits, 3 byte
3709e 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d  s are taken from
3709f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61   the beginning a
370a0 6e 64 0a 2a 2a 20 33 20 62 79 74 65 73 20 66 72  nd.** 3 bytes fr
370a1 6f 6d 20 74 68 65 20 65 6e 64 2e 20 20 46 6f 72  om the end.  For
370a2 20 6c 6f 6e 67 20 77 6f 72 64 73 20 77 69 74 68   long words with
370a3 6f 75 74 20 64 69 67 69 74 73 2c 20 31 30 20 62  out digits, 10 b
370a4 79 74 65 73 0a 2a 2a 20 61 72 65 20 74 61 6b 65  ytes.** are take
370a5 6e 20 66 72 6f 6d 20 65 61 63 68 20 65 6e 64 2e  n from each end.
370a6 20 20 55 53 2d 41 53 43 49 49 20 63 61 73 65 20    US-ASCII case 
370a7 66 6f 6c 64 69 6e 67 20 73 74 69 6c 6c 20 61 70  folding still ap
370a8 70 6c 69 65 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66  plies..** .** If
370a9 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20   the input word 
370aa 63 6f 6e 74 61 69 6e 73 20 6e 6f 74 20 64 69 67  contains not dig
370ab 69 74 73 20 62 75 74 20 64 6f 65 73 20 63 68 61  its but does cha
370ac 72 61 63 74 65 72 73 20 6e 6f 74 20 0a 2a 2a 20  racters not .** 
370ad 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 74 68 65 6e  in [a-zA-Z] then
370ae 20 6e 6f 20 73 74 65 6d 6d 69 6e 67 20 69 73 20   no stemming is 
370af 61 74 74 65 6d 70 74 65 64 20 61 6e 64 20 74 68  attempted and th
370b0 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20  is routine just 
370b1 0a 2a 2a 20 63 6f 70 69 65 73 20 74 68 65 20 69  .** copies the i
370b2 6e 70 75 74 20 69 6e 74 6f 20 74 68 65 20 69 6e  nput into the in
370b3 70 75 74 20 69 6e 74 6f 20 74 68 65 20 6f 75 74  put into the out
370b4 70 75 74 20 77 69 74 68 20 55 53 2d 41 53 43 49  put with US-ASCI
370b5 49 0a 2a 2a 20 63 61 73 65 20 66 6f 6c 64 69 6e  I.** case foldin
370b6 67 2e 0a 2a 2a 0a 2a 2a 20 53 74 65 6d 6d 69 6e  g..**.** Stemmin
370b7 67 20 6e 65 76 65 72 20 69 6e 63 72 65 61 73 65  g never increase
370b8 73 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  s the length of 
370b9 74 68 65 20 77 6f 72 64 2e 20 20 53 6f 20 74 68  the word.  So th
370ba 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 63 68 61  ere is.** no cha
370bb 6e 63 65 20 6f 66 20 6f 76 65 72 66 6c 6f 77 69  nce of overflowi
370bc 6e 67 20 74 68 65 20 7a 4f 75 74 20 62 75 66 66  ng the zOut buff
370bd 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  er..*/.static vo
370be 69 64 20 70 6f 72 74 65 72 5f 73 74 65 6d 6d 65  id porter_stemme
370bf 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49  r(const char *zI
370c0 6e 2c 20 69 6e 74 20 6e 49 6e 2c 20 63 68 61 72  n, int nIn, char
370c1 20 2a 7a 4f 75 74 2c 20 69 6e 74 20 2a 70 6e 4f   *zOut, int *pnO
370c2 75 74 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  ut){.  int i, j;
370c3 0a 20 20 63 68 61 72 20 7a 52 65 76 65 72 73 65  .  char zReverse
370c4 5b 32 38 5d 3b 0a 20 20 63 68 61 72 20 2a 7a 2c  [28];.  char *z,
370c5 20 2a 7a 32 3b 0a 20 20 69 66 28 20 6e 49 6e 3c   *z2;.  if( nIn<
370c6 33 20 7c 7c 20 6e 49 6e 3e 3d 73 69 7a 65 6f 66  3 || nIn>=sizeof
370c7 28 7a 52 65 76 65 72 73 65 29 2d 37 20 29 7b 0a  (zReverse)-7 ){.
370c8 20 20 20 20 2f 2a 20 54 68 65 20 77 6f 72 64 20      /* The word 
370c9 69 73 20 74 6f 6f 20 62 69 67 20 6f 72 20 74 6f  is too big or to
370ca 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65 20  o small for the 
370cb 70 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 2e 0a  porter stemmer..
370cc 20 20 20 20 2a 2a 20 46 61 6c 6c 62 61 63 6b 20      ** Fallback 
370cd 74 6f 20 74 68 65 20 63 6f 70 79 20 73 74 65 6d  to the copy stem
370ce 6d 65 72 20 2a 2f 0a 20 20 20 20 63 6f 70 79 5f  mer */.    copy_
370cf 73 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e  stemmer(zIn, nIn
370d0 2c 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a  , zOut, pnOut);.
370d1 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
370d2 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 73 69 7a    for(i=0, j=siz
370d3 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 36 3b  eof(zReverse)-6;
370d4 20 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2d 2d   i<nIn; i++, j--
370d5 29 7b 0a 20 20 20 20 63 68 61 72 20 63 20 3d 20  ){.    char c = 
370d6 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20  zIn[i];.    if( 
370d7 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d 27 5a 27  c>='A' && c<='Z'
370d8 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 76 65 72   ){.      zRever
370d9 73 65 5b 6a 5d 20 3d 20 63 20 2b 20 27 61 27 20  se[j] = c + 'a' 
370da 2d 20 27 41 27 3b 0a 20 20 20 20 7d 65 6c 73 65  - 'A';.    }else
370db 20 69 66 28 20 63 3e 3d 27 61 27 20 26 26 20 63   if( c>='a' && c
370dc 3c 3d 27 7a 27 20 29 7b 0a 20 20 20 20 20 20 7a  <='z' ){.      z
370dd 52 65 76 65 72 73 65 5b 6a 5d 20 3d 20 63 3b 0a  Reverse[j] = c;.
370de 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
370df 20 2f 2a 20 54 68 65 20 75 73 65 20 6f 66 20 61   /* The use of a
370e0 20 63 68 61 72 61 63 74 65 72 20 6e 6f 74 20 69   character not i
370e1 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 6d 65 61 6e 73  n [a-zA-Z] means
370e2 20 74 68 61 74 20 77 65 20 66 61 6c 6c 62 61 63   that we fallbac
370e3 6b 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68  k.      ** to th
370e4 65 20 63 6f 70 79 20 73 74 65 6d 6d 65 72 20 2a  e copy stemmer *
370e5 2f 0a 20 20 20 20 20 20 63 6f 70 79 5f 73 74 65  /.      copy_ste
370e6 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e 2c 20 7a  mmer(zIn, nIn, z
370e7 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a 20 20 20  Out, pnOut);.   
370e8 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
370e9 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26 7a  .  }.  memset(&z
370ea 52 65 76 65 72 73 65 5b 73 69 7a 65 6f 66 28 7a  Reverse[sizeof(z
370eb 52 65 76 65 72 73 65 29 2d 35 5d 2c 20 30 2c 20  Reverse)-5], 0, 
370ec 35 29 3b 0a 20 20 7a 20 3d 20 26 7a 52 65 76 65  5);.  z = &zReve
370ed 72 73 65 5b 6a 2b 31 5d 3b 0a 0a 0a 20 20 2f 2a  rse[j+1];...  /*
370ee 20 53 74 65 70 20 31 61 20 2a 2f 0a 20 20 69 66   Step 1a */.  if
370ef 28 20 7a 5b 30 5d 3d 3d 27 73 27 20 29 7b 0a 20  ( z[0]=='s' ){. 
370f0 20 20 20 69 66 28 0a 20 20 20 20 20 21 73 74 65     if(.     !ste
370f1 6d 28 26 7a 2c 20 22 73 65 73 73 22 2c 20 22 73  m(&z, "sess", "s
370f2 73 22 2c 20 30 29 20 26 26 0a 20 20 20 20 20 21  s", 0) &&.     !
370f3 73 74 65 6d 28 26 7a 2c 20 22 73 65 69 22 2c 20  stem(&z, "sei", 
370f4 22 69 22 2c 20 30 29 20 20 26 26 0a 20 20 20 20  "i", 0)  &&.    
370f5 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 73 22 2c   !stem(&z, "ss",
370f6 20 22 73 73 22 2c 20 30 29 0a 20 20 20 20 29 7b   "ss", 0).    ){
370f7 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
370f8 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  }.  }..  /* Step
370f9 20 31 62 20 2a 2f 20 20 0a 20 20 7a 32 20 3d 20   1b */  .  z2 = 
370fa 7a 3b 0a 20 20 69 66 28 20 73 74 65 6d 28 26 7a  z;.  if( stem(&z
370fb 2c 20 22 64 65 65 22 2c 20 22 65 65 22 2c 20 6d  , "dee", "ee", m
370fc 5f 67 74 5f 30 29 20 29 7b 0a 20 20 20 20 2f 2a  _gt_0) ){.    /*
370fd 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68   Do nothing.  Th
370fe 65 20 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20 69  e work was all i
370ff 6e 20 74 68 65 20 74 65 73 74 20 2a 2f 0a 20 20  n the test */.  
37100 7d 65 6c 73 65 20 69 66 28 20 0a 20 20 20 20 20  }else if( .     
37101 28 73 74 65 6d 28 26 7a 2c 20 22 67 6e 69 22 2c  (stem(&z, "gni",
37102 20 22 22 2c 20 68 61 73 56 6f 77 65 6c 29 20 7c   "", hasVowel) |
37103 7c 20 73 74 65 6d 28 26 7a 2c 20 22 64 65 22 2c  | stem(&z, "de",
37104 20 22 22 2c 20 68 61 73 56 6f 77 65 6c 29 29 0a   "", hasVowel)).
37105 20 20 20 20 20 20 26 26 20 7a 21 3d 7a 32 0a 20        && z!=z2. 
37106 20 29 7b 0a 20 20 20 20 20 69 66 28 20 73 74 65   ){.     if( ste
37107 6d 28 26 7a 2c 20 22 74 61 22 2c 20 22 61 74 65  m(&z, "ta", "ate
37108 22 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20 20 20  ", 0) ||.       
37109 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 62 22 2c    stem(&z, "lb",
3710a 20 22 62 6c 65 22 2c 20 30 29 20 7c 7c 0a 20 20   "ble", 0) ||.  
3710b 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
3710c 22 7a 69 22 2c 20 22 69 7a 65 22 2c 20 30 29 20  "zi", "ize", 0) 
3710d 29 7b 0a 20 20 20 20 20 20 20 2f 2a 20 44 6f 20  ){.       /* Do 
3710e 6e 6f 74 68 69 6e 67 2e 20 20 54 68 65 20 77 6f  nothing.  The wo
3710f 72 6b 20 77 61 73 20 61 6c 6c 20 69 6e 20 74 68  rk was all in th
37110 65 20 74 65 73 74 20 2a 2f 0a 20 20 20 20 20 7d  e test */.     }
37111 65 6c 73 65 20 69 66 28 20 64 6f 75 62 6c 65 43  else if( doubleC
37112 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 20 28  onsonant(z) && (
37113 2a 7a 21 3d 27 6c 27 20 26 26 20 2a 7a 21 3d 27  *z!='l' && *z!='
37114 73 27 20 26 26 20 2a 7a 21 3d 27 7a 27 29 20 29  s' && *z!='z') )
37115 7b 0a 20 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  {.       z++;.  
37116 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6d 5f 65     }else if( m_e
37117 71 5f 31 28 7a 29 20 26 26 20 73 74 61 72 5f 6f  q_1(z) && star_o
37118 68 28 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 2a  h(z) ){.       *
37119 28 2d 2d 7a 29 20 3d 20 27 65 27 3b 0a 20 20 20  (--z) = 'e';.   
3711a 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74    }.  }..  /* St
3711b 65 70 20 31 63 20 2a 2f 0a 20 20 69 66 28 20 7a  ep 1c */.  if( z
3711c 5b 30 5d 3d 3d 27 79 27 20 26 26 20 68 61 73 56  [0]=='y' && hasV
3711d 6f 77 65 6c 28 7a 2b 31 29 20 29 7b 0a 20 20 20  owel(z+1) ){.   
3711e 20 7a 5b 30 5d 20 3d 20 27 69 27 3b 0a 20 20 7d   z[0] = 'i';.  }
3711f 0a 0a 20 20 2f 2a 20 53 74 65 70 20 32 20 2a 2f  ..  /* Step 2 */
37120 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 31 5d 20  .  switch( z[1] 
37121 29 7b 0a 20 20 20 63 61 73 65 20 27 61 27 3a 0a  ){.   case 'a':.
37122 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
37123 61 6e 6f 69 74 61 22 2c 20 22 61 74 65 22 2c 20  anoita", "ate", 
37124 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
37125 73 74 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69 74  stem(&z, "lanoit
37126 22 2c 20 22 74 69 6f 6e 22 2c 20 6d 5f 67 74 5f  ", "tion", m_gt_
37127 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  0);.     break;.
37128 20 20 20 63 61 73 65 20 27 63 27 3a 0a 20 20 20     case 'c':.   
37129 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 63 6e 65    stem(&z, "icne
3712a 22 2c 20 22 65 6e 63 65 22 2c 20 6d 5f 67 74 5f  ", "ence", m_gt_
3712b 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
3712c 26 7a 2c 20 22 69 63 6e 61 22 2c 20 22 61 6e 63  &z, "icna", "anc
3712d 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  e", m_gt_0);.   
3712e 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
3712f 20 27 65 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   'e':.     stem(
37130 26 7a 2c 20 22 72 65 7a 69 22 2c 20 22 69 7a 65  &z, "rezi", "ize
37131 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
37132 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
37133 27 67 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'g':.     stem(&
37134 7a 2c 20 22 69 67 6f 6c 22 2c 20 22 6c 6f 67 22  z, "igol", "log"
37135 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20  , m_gt_0);.     
37136 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
37137 6c 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a  l':.     stem(&z
37138 2c 20 22 69 6c 62 22 2c 20 22 62 6c 65 22 2c 20  , "ilb", "ble", 
37139 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
3713a 73 74 65 6d 28 26 7a 2c 20 22 69 6c 6c 61 22 2c  stem(&z, "illa",
3713b 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c   "al", m_gt_0) |
3713c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
3713d 22 69 6c 74 6e 65 22 2c 20 22 65 6e 74 22 2c 20  "iltne", "ent", 
3713e 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
3713f 73 74 65 6d 28 26 7a 2c 20 22 69 6c 65 22 2c 20  stem(&z, "ile", 
37140 22 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  "e", m_gt_0) ||.
37141 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
37142 6c 73 75 6f 22 2c 20 22 6f 75 73 22 2c 20 6d 5f  lsuo", "ous", m_
37143 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
37144 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f 27 3a 0a  k;.   case 'o':.
37145 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e       stem(&z, "n
37146 6f 69 74 61 7a 69 22 2c 20 22 69 7a 65 22 2c 20  oitazi", "ize", 
37147 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
37148 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 74 61 22  stem(&z, "noita"
37149 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74 5f 30 29  , "ate", m_gt_0)
3714a 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
3714b 2c 20 22 72 6f 74 61 22 2c 20 22 61 74 65 22 2c  , "rota", "ate",
3714c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
3714d 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 73  reak;.   case 's
3714e 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
3714f 20 22 6d 73 69 6c 61 22 2c 20 22 61 6c 22 2c 20   "msila", "al", 
37150 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
37151 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 65 76  stem(&z, "ssenev
37152 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74 5f  i", "ive", m_gt_
37153 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
37154 26 7a 2c 20 22 73 73 65 6e 6c 75 66 22 2c 20 22  &z, "ssenluf", "
37155 66 75 6c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c  ful", m_gt_0) ||
37156 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
37157 73 73 65 6e 73 75 6f 22 2c 20 22 6f 75 73 22 2c  ssensuo", "ous",
37158 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
37159 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 74  reak;.   case 't
3715a 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
3715b 20 22 69 74 69 6c 61 22 2c 20 22 61 6c 22 2c 20   "itila", "al", 
3715c 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20  m_gt_0) ||.     
3715d 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 76 69 22  stem(&z, "itivi"
3715e 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74 5f 30 29  , "ive", m_gt_0)
3715f 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
37160 2c 20 22 69 74 69 6c 69 62 22 2c 20 22 62 6c 65  , "itilib", "ble
37161 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
37162 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   break;.  }..  /
37163 2a 20 53 74 65 70 20 33 20 2a 2f 0a 20 20 73 77  * Step 3 */.  sw
37164 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b 0a 20 20  itch( z[0] ){.  
37165 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20   case 'e':.     
37166 73 74 65 6d 28 26 7a 2c 20 22 65 74 61 63 69 22  stem(&z, "etaci"
37167 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20  , "ic", m_gt_0) 
37168 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ||.     stem(&z,
37169 20 22 65 76 69 74 61 22 2c 20 22 22 2c 20 6d 5f   "evita", "", m_
3716a 67 74 5f 30 29 20 20 20 7c 7c 0a 20 20 20 20 20  gt_0)   ||.     
3716b 73 74 65 6d 28 26 7a 2c 20 22 65 7a 69 6c 61 22  stem(&z, "ezila"
3716c 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30 29 3b  , "al", m_gt_0);
3716d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  .     break;.   
3716e 63 61 73 65 20 27 69 27 3a 0a 20 20 20 20 20 73  case 'i':.     s
3716f 74 65 6d 28 26 7a 2c 20 22 69 74 69 63 69 22 2c  tem(&z, "itici",
37170 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a   "ic", m_gt_0);.
37171 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
37172 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 73 74  ase 'l':.     st
37173 65 6d 28 26 7a 2c 20 22 6c 61 63 69 22 2c 20 22  em(&z, "laci", "
37174 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ic", m_gt_0) ||.
37175 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
37176 75 66 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29  uf", "", m_gt_0)
37177 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  ;.     break;.  
37178 20 63 61 73 65 20 27 73 27 3a 0a 20 20 20 20 20   case 's':.     
37179 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e 22 2c  stem(&z, "ssen",
3717a 20 22 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20   "", m_gt_0);.  
3717b 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20     break;.  }.. 
3717c 20 2f 2a 20 53 74 65 70 20 34 20 2a 2f 0a 20 20   /* Step 4 */.  
3717d 73 77 69 74 63 68 28 20 7a 5b 31 5d 20 29 7b 0a  switch( z[1] ){.
3717e 20 20 20 63 61 73 65 20 27 61 27 3a 0a 20 20 20     case 'a':.   
3717f 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 6c 27 20    if( z[0]=='l' 
37180 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29  && m_gt_1(z+2) )
37181 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b  {.       z += 2;
37182 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
37183 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 63 27 3a  ak;.   case 'c':
37184 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d  .     if( z[0]==
37185 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 6e 27  'e' && z[2]=='n'
37186 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c   && (z[3]=='a' |
37187 7c 20 7a 5b 33 5d 3d 3d 27 65 27 29 20 20 26 26  | z[3]=='e')  &&
37188 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 20 29 7b   m_gt_1(z+4)  ){
37189 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 34 3b 0a  .       z += 4;.
3718a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61       }.     brea
3718b 6b 3b 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a  k;.   case 'e':.
3718c 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
3718d 72 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32  r' && m_gt_1(z+2
3718e 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d  ) ){.       z +=
3718f 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20   2;.     }.     
37190 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
37191 69 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  i':.     if( z[0
37192 5d 3d 3d 27 63 27 20 26 26 20 6d 5f 67 74 5f 31  ]=='c' && m_gt_1
37193 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+2) ){.       
37194 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20  z += 2;.     }. 
37195 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
37196 73 65 20 27 6c 27 3a 0a 20 20 20 20 20 69 66 28  se 'l':.     if(
37197 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b   z[0]=='e' && z[
37198 32 5d 3d 3d 27 62 27 20 26 26 20 28 7a 5b 33 5d  2]=='b' && (z[3]
37199 3d 3d 27 61 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27  =='a' || z[3]=='
3719a 69 27 29 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  i') && m_gt_1(z+
3719b 34 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  4) ){.       z +
3719c 3d 20 34 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 4;.     }.    
3719d 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
3719e 27 6e 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  'n':.     if( z[
3719f 30 5d 3d 3d 27 74 27 20 29 7b 0a 20 20 20 20 20  0]=='t' ){.     
371a0 20 20 69 66 28 20 7a 5b 32 5d 3d 3d 27 61 27 20    if( z[2]=='a' 
371a1 29 7b 0a 20 20 20 20 20 20 20 20 20 69 66 28 20  ){.         if( 
371a2 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a 20  m_gt_1(z+3) ){. 
371a3 20 20 20 20 20 20 20 20 20 20 7a 20 2b 3d 20 33            z += 3
371a4 3b 0a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  ;.         }.   
371a5 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b      }else if( z[
371a6 32 5d 3d 3d 27 65 27 20 29 7b 0a 20 20 20 20 20  2]=='e' ){.     
371a7 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e      stem(&z, "tn
371a8 65 6d 65 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31  eme", "", m_gt_1
371a9 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74  ) ||.         st
371aa 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 22 2c 20 22  em(&z, "tnem", "
371ab 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20  ", m_gt_1) ||.  
371ac 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
371ad 22 74 6e 65 22 2c 20 22 22 2c 20 6d 5f 67 74 5f  "tne", "", m_gt_
371ae 31 29 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20  1);.       }.   
371af 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
371b0 20 20 20 63 61 73 65 20 27 6f 27 3a 0a 20 20 20     case 'o':.   
371b1 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 75 27 20    if( z[0]=='u' 
371b2 29 7b 0a 20 20 20 20 20 20 20 69 66 28 20 6d 5f  ){.       if( m_
371b3 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20  gt_1(z+2) ){.   
371b4 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20        z += 2;.  
371b5 20 20 20 20 20 7d 0a 20 20 20 20 20 7d 65 6c 73       }.     }els
371b6 65 20 69 66 28 20 7a 5b 33 5d 3d 3d 27 73 27 20  e if( z[3]=='s' 
371b7 7c 7c 20 7a 5b 33 5d 3d 3d 27 74 27 20 29 7b 0a  || z[3]=='t' ){.
371b8 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
371b9 22 6e 6f 69 22 2c 20 22 22 2c 20 6d 5f 67 74 5f  "noi", "", m_gt_
371ba 31 29 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20  1);.     }.     
371bb 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
371bc 73 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  s':.     if( z[0
371bd 5d 3d 3d 27 6d 27 20 26 26 20 7a 5b 32 5d 3d 3d  ]=='m' && z[2]==
371be 27 69 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  'i' && m_gt_1(z+
371bf 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  3) ){.       z +
371c0 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 3;.     }.    
371c1 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
371c2 27 74 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  't':.     stem(&
371c3 7a 2c 20 22 65 74 61 22 2c 20 22 22 2c 20 6d 5f  z, "eta", "", m_
371c4 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_1) ||.     st
371c5 65 6d 28 26 7a 2c 20 22 69 74 69 22 2c 20 22 22  em(&z, "iti", ""
371c6 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20 20 20 20  , m_gt_1);.     
371c7 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
371c8 75 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  u':.     if( z[0
371c9 5d 3d 3d 27 73 27 20 26 26 20 7a 5b 32 5d 3d 3d  ]=='s' && z[2]==
371ca 27 6f 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b  'o' && m_gt_1(z+
371cb 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b  3) ){.       z +
371cc 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20  = 3;.     }.    
371cd 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
371ce 27 76 27 3a 0a 20 20 20 63 61 73 65 20 27 7a 27  'v':.   case 'z'
371cf 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d  :.     if( z[0]=
371d0 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 69  ='e' && z[2]=='i
371d1 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29  ' && m_gt_1(z+3)
371d2 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20   ){.       z += 
371d3 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62  3;.     }.     b
371d4 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  reak;.  }..  /* 
371d5 53 74 65 70 20 35 61 20 2a 2f 0a 20 20 69 66 28  Step 5a */.  if(
371d6 20 7a 5b 30 5d 3d 3d 27 65 27 20 29 7b 0a 20 20   z[0]=='e' ){.  
371d7 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 31    if( m_gt_1(z+1
371d8 29 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a  ) ){.      z++;.
371d9 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6d 5f      }else if( m_
371da 65 71 5f 31 28 7a 2b 31 29 20 26 26 20 21 73 74  eq_1(z+1) && !st
371db 61 72 5f 6f 68 28 7a 2b 31 29 20 29 7b 0a 20 20  ar_oh(z+1) ){.  
371dc 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20      z++;.    }. 
371dd 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 62   }..  /* Step 5b
371de 20 2a 2f 0a 20 20 69 66 28 20 6d 5f 67 74 5f 31   */.  if( m_gt_1
371df 28 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d 27 6c 27  (z) && z[0]=='l'
371e0 20 26 26 20 7a 5b 31 5d 3d 3d 27 6c 27 20 29 7b   && z[1]=='l' ){
371e1 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 0a 20  .    z++;.  }.. 
371e2 20 2f 2a 20 7a 5b 5d 20 69 73 20 6e 6f 77 20 74   /* z[] is now t
371e3 68 65 20 73 74 65 6d 6d 65 64 20 77 6f 72 64 20  he stemmed word 
371e4 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64 65 72  in reverse order
371e5 2e 20 20 46 6c 69 70 20 69 74 20 62 61 63 6b 0a  .  Flip it back.
371e6 20 20 2a 2a 20 61 72 6f 75 6e 64 20 69 6e 74 6f    ** around into
371e7 20 66 6f 72 77 61 72 64 20 6f 72 64 65 72 20 61   forward order a
371e8 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a  nd return..  */.
371e9 20 20 2a 70 6e 4f 75 74 20 3d 20 69 20 3d 20 28    *pnOut = i = (
371ea 69 6e 74 29 73 74 72 6c 65 6e 28 7a 29 3b 0a 20  int)strlen(z);. 
371eb 20 7a 4f 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20   zOut[i] = 0;.  
371ec 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20  while( *z ){.   
371ed 20 7a 4f 75 74 5b 2d 2d 69 5d 20 3d 20 2a 28 7a   zOut[--i] = *(z
371ee 2b 2b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ++);.  }.}../*.*
371ef 2a 20 43 68 61 72 61 63 74 65 72 73 20 74 68 61  * Characters tha
371f0 74 20 63 61 6e 20 62 65 20 70 61 72 74 20 6f 66  t can be part of
371f1 20 61 20 74 6f 6b 65 6e 2e 20 20 57 65 20 61 73   a token.  We as
371f2 73 75 6d 65 20 61 6e 79 20 63 68 61 72 61 63 74  sume any charact
371f3 65 72 0a 2a 2a 20 77 68 6f 73 65 20 76 61 6c 75  er.** whose valu
371f4 65 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  e is greater tha
371f5 6e 20 30 78 38 30 20 28 61 6e 79 20 55 54 46 20  n 0x80 (any UTF 
371f6 63 68 61 72 61 63 74 65 72 29 20 63 61 6e 20 62  character) can b
371f7 65 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 20 74  e.** part of a t
371f8 6f 6b 65 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20  oken.  In other 
371f9 77 6f 72 64 73 2c 20 64 65 6c 69 6d 69 74 65 72  words, delimiter
371fa 73 20 61 6c 6c 20 6d 75 73 74 20 68 61 76 65 0a  s all must have.
371fb 2a 2a 20 76 61 6c 75 65 73 20 6f 66 20 30 78 37  ** values of 0x7
371fc 66 20 6f 72 20 6c 6f 77 65 72 2e 0a 2a 2f 0a 73  f or lower..*/.s
371fd 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
371fe 20 70 6f 72 74 65 72 49 64 43 68 61 72 5b 5d 20   porterIdChar[] 
371ff 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20  = {./* x0 x1 x2 
37200 78 33 20 78 34 20 78 35 20 78 36 20 78 37 20 78  x3 x4 x5 x6 x7 x
37201 38 20 78 39 20 78 41 20 78 42 20 78 43 20 78 44  8 x9 xA xB xC xD
37202 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20 31 2c   xE xF */.    1,
37203 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37204 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
37205 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
37206 2a 20 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * 3x */.    0, 1
37207 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
37208 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37209 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
3720a 34 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  4x */.    1, 1, 
3720b 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3720c 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
3720d 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78   0, 0, 1,  /* 5x
3720e 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
3720f 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
37210 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
37211 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a  , 1, 1,  /* 6x *
37212 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
37213 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
37214 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
37215 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a  0, 0,  /* 7x */.
37216 7d 3b 0a 23 64 65 66 69 6e 65 20 69 73 44 65 6c  };.#define isDel
37217 69 6d 28 43 29 20 28 28 28 63 68 3d 43 29 26 30  im(C) (((ch=C)&0
37218 78 38 30 29 3d 3d 30 20 26 26 20 28 63 68 3c 30  x80)==0 && (ch<0
37219 78 33 30 20 7c 7c 20 21 70 6f 72 74 65 72 49 64  x30 || !porterId
3721a 43 68 61 72 5b 63 68 2d 30 78 33 30 5d 29 29 0a  Char[ch-0x30])).
3721b 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74  ./*.** Extract t
3721c 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72  he next token fr
3721d 6f 6d 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f  om a tokenizatio
3721e 6e 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63  n cursor.  The c
3721f 75 72 73 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61  ursor must.** ha
37220 76 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62  ve been opened b
37221 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74  y a prior call t
37222 6f 20 70 6f 72 74 65 72 4f 70 65 6e 28 29 2e 0a  o porterOpen()..
37223 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f  */.static int po
37224 72 74 65 72 4e 65 78 74 28 0a 20 20 73 71 6c 69  rterNext(.  sqli
37225 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
37226 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20  rsor *pCursor,  
37227 2f 2a 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e  /* Cursor return
37228 65 64 20 62 79 20 70 6f 72 74 65 72 4f 70 65 6e  ed by porterOpen
37229 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3722a 20 2a 2a 70 7a 54 6f 6b 65 6e 2c 20 20 20 20 20   **pzToken,     
3722b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
3722c 3a 20 2a 70 7a 54 6f 6b 65 6e 20 69 73 20 74 68  : *pzToken is th
3722d 65 20 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a  e token text */.
3722e 20 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20    int *pnBytes, 
3722f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37230 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
37231 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
37232 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
37233 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  *piStartOffset, 
37234 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37235 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67  /* OUT: Starting
37236 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
37237 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64   */.  int *piEnd
37238 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20  Offset,         
37239 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
3723a 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20  : Ending offset 
3723b 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  of token */.  in
3723c 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20  t *piPosition   
3723d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3723e 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69    /* OUT: Positi
3723f 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f  on integer of to
37240 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74  ken */.){.  port
37241 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  er_tokenizer_cur
37242 73 6f 72 20 2a 63 20 3d 20 28 70 6f 72 74 65 72  sor *c = (porter
37243 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
37244 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20  r *) pCursor;.  
37245 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
37246 63 2d 3e 7a 49 6e 70 75 74 3b 0a 0a 20 20 77 68  c->zInput;..  wh
37247 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c  ile( c->iOffset<
37248 63 2d 3e 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20  c->nInput ){.   
37249 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65   int iStartOffse
3724a 74 2c 20 63 68 3b 0a 0a 20 20 20 20 2f 2a 20 53  t, ch;..    /* S
3724b 63 61 6e 20 70 61 73 74 20 64 65 6c 69 6d 69 74  can past delimit
3724c 65 72 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f  er characters */
3724d 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69  .    while( c->i
3724e 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74  Offset<c->nInput
3724f 20 26 26 20 69 73 44 65 6c 69 6d 28 7a 5b 63 2d   && isDelim(z[c-
37250 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20  >iOffset]) ){.  
37251 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b      c->iOffset++
37252 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
37253 43 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69  Count non-delimi
37254 74 65 72 20 63 68 61 72 61 63 74 65 72 73 2e 20  ter characters. 
37255 2a 2f 0a 20 20 20 20 69 53 74 61 72 74 4f 66 66  */.    iStartOff
37256 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74  set = c->iOffset
37257 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e  ;.    while( c->
37258 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75  iOffset<c->nInpu
37259 74 20 26 26 20 21 69 73 44 65 6c 69 6d 28 7a 5b  t && !isDelim(z[
3725a 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a  c->iOffset]) ){.
3725b 20 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74        c->iOffset
3725c 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ++;.    }..    i
3725d 66 28 20 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53  f( c->iOffset>iS
3725e 74 61 72 74 4f 66 66 73 65 74 20 29 7b 0a 20 20  tartOffset ){.  
3725f 20 20 20 20 69 6e 74 20 6e 20 3d 20 63 2d 3e 69      int n = c->i
37260 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66  Offset-iStartOff
37261 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  set;.      if( n
37262 3e 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 29  >c->nAllocated )
37263 7b 0a 20 20 20 20 20 20 20 20 63 2d 3e 6e 41 6c  {.        c->nAl
37264 6c 6f 63 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0a  located = n+20;.
37265 20 20 20 20 20 20 20 20 63 2d 3e 7a 54 6f 6b 65          c->zToke
37266 6e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  n = sqlite3_real
37267 6c 6f 63 28 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 63  loc(c->zToken, c
37268 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20  ->nAllocated);. 
37269 20 20 20 20 20 20 20 69 66 28 20 63 2d 3e 7a 54         if( c->zT
3726a 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65 74  oken==NULL ) ret
3726b 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
3726c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3726d 70 6f 72 74 65 72 5f 73 74 65 6d 6d 65 72 28 26  porter_stemmer(&
3726e 7a 5b 69 53 74 61 72 74 4f 66 66 73 65 74 5d 2c  z[iStartOffset],
3726f 20 6e 2c 20 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 70   n, c->zToken, p
37270 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 20 20 2a  nBytes);.      *
37271 70 7a 54 6f 6b 65 6e 20 3d 20 63 2d 3e 7a 54 6f  pzToken = c->zTo
37272 6b 65 6e 3b 0a 20 20 20 20 20 20 2a 70 69 53 74  ken;.      *piSt
37273 61 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74 61  artOffset = iSta
37274 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  rtOffset;.      
37275 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 63  *piEndOffset = c
37276 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  ->iOffset;.     
37277 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63   *piPosition = c
37278 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20  ->iToken++;.    
37279 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3727a 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  OK;.    }.  }.  
3727b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
3727c 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  NE;.}../*.** The
3727d 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
3727e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
3727f 74 68 65 20 70 6f 72 74 65 72 2d 73 74 65 6d 6d  the porter-stemm
37280 65 72 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a  er tokenizer.*/.
37281 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
37282 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
37283 6f 64 75 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65  odule porterToke
37284 6e 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a  nizerModule = {.
37285 20 20 30 2c 0a 20 20 70 6f 72 74 65 72 43 72 65    0,.  porterCre
37286 61 74 65 2c 0a 20 20 70 6f 72 74 65 72 44 65 73  ate,.  porterDes
37287 74 72 6f 79 2c 0a 20 20 70 6f 72 74 65 72 4f 70  troy,.  porterOp
37288 65 6e 2c 0a 20 20 70 6f 72 74 65 72 43 6c 6f 73  en,.  porterClos
37289 65 2c 0a 20 20 70 6f 72 74 65 72 4e 65 78 74 2c  e,.  porterNext,
3728a 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  .};../*.** Alloc
3728b 61 74 65 20 61 20 6e 65 77 20 70 6f 72 74 65 72  ate a new porter
3728c 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65 74   tokenizer.  Ret
3728d 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
3728e 20 74 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65   the new.** toke
3728f 6e 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64 75  nizer in *ppModu
37290 6c 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  le.*/.SQLITE_PRI
37291 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
37292 33 46 74 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e  3Fts3PorterToken
37293 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71  izerModule(.  sq
37294 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
37295 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
37296 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d  Module.){.  *ppM
37297 6f 64 75 6c 65 20 3d 20 26 70 6f 72 74 65 72 54  odule = &porterT
37298 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a  okenizerModule;.
37299 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  }..#endif /* !de
3729a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
3729b 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
3729c 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
3729d 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
3729e 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73  ***** End of fts
3729f 33 5f 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a  3_porter.c *****
372a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372a2 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
372a3 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
372a4 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e   fts3_tokenizer.
372a5 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
372a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372a7 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
372a8 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54   June 22.**.** T
372a9 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
372aa 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
372ab 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
372ac 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
372ad 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
372ae 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
372af 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
372b0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
372b1 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
372b2 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
372b3 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
372b4 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
372b5 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
372b6 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
372b7 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
372b8 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
372b9 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
372ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
372bf 2a 2a 20 54 68 69 73 20 69 73 20 70 61 72 74 20  ** This is part 
372c0 6f 66 20 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64  of an SQLite mod
372c1 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  ule implementing
372c2 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63   full-text searc
372c3 68 2e 0a 2a 2a 20 54 68 69 73 20 70 61 72 74 69  h..** This parti
372c4 63 75 6c 61 72 20 66 69 6c 65 20 69 6d 70 6c 65  cular file imple
372c5 6d 65 6e 74 73 20 74 68 65 20 67 65 6e 65 72 69  ments the generi
372c6 63 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 74 65  c tokenizer inte
372c7 72 66 61 63 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  rface..*/../*.**
372c8 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
372c9 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63  s file is only c
372ca 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a  ompiled if:.**.*
372cb 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
372cc 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
372cd 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74   built as an ext
372ce 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ension.**       
372cf 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
372d0 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f  QLITE_CORE is no
372d1 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a  t defined), or.*
372d2 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
372d3 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
372d4 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  ing built into t
372d5 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20  he core of.**   
372d6 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77      SQLite (in w
372d7 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
372d8 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20  _ENABLE_FTS3 is 
372d9 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66  defined)..*/.#if
372da 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
372db 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
372dc 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
372dd 46 54 53 33 29 0a 0a 23 69 66 6e 64 65 66 20 53  FTS3)..#ifndef S
372de 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c  QLITE_CORE.  SQL
372df 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
372e0 49 54 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  IT1.#endif.../*.
372e1 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
372e2 6e 20 6f 66 20 74 68 65 20 53 51 4c 20 73 63 61  n of the SQL sca
372e3 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  lar function for
372e4 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 75   accessing the u
372e5 6e 64 65 72 6c 79 69 6e 67 20 0a 2a 2a 20 68 61  nderlying .** ha
372e6 73 68 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66  sh table. This f
372e7 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63  unction may be c
372e8 61 6c 6c 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  alled as follows
372e9 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54  :.**.**   SELECT
372ea 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e   <function-name>
372eb 28 3c 6b 65 79 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a  (<key-name>);.**
372ec 20 20 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74     SELECT <funct
372ed 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e  ion-name>(<key-n
372ee 61 6d 65 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29  ame>, <pointer>)
372ef 3b 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66  ;.**.** where <f
372f0 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73  unction-name> is
372f1 20 74 68 65 20 6e 61 6d 65 20 70 61 73 73 65 64   the name passed
372f2 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
372f3 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68  rgument.** to th
372f4 65 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  e sqlite3Fts3Ini
372f5 74 48 61 73 68 54 61 62 6c 65 28 29 20 66 75 6e  tHashTable() fun
372f6 63 74 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73  ction (e.g. 'fts
372f7 33 5f 74 6f 6b 65 6e 69 7a 65 72 27 29 2e 0a 2a  3_tokenizer')..*
372f8 2a 0a 2a 2a 20 49 66 20 74 68 65 20 3c 70 6f 69  *.** If the <poi
372f9 6e 74 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69  nter> argument i
372fa 73 20 73 70 65 63 69 66 69 65 64 2c 20 69 74 20  s specified, it 
372fb 6d 75 73 74 20 62 65 20 61 20 62 6c 6f 62 20 76  must be a blob v
372fc 61 6c 75 65 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69  alue.** containi
372fd 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ng a pointer to 
372fe 62 65 20 73 74 6f 72 65 64 20 61 73 20 74 68 65  be stored as the
372ff 20 68 61 73 68 20 64 61 74 61 20 63 6f 72 72 65   hash data corre
37300 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 74  sponding.** to t
37301 68 65 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e  he string <key-n
37302 61 6d 65 3e 2e 20 49 66 20 3c 70 6f 69 6e 74 65  ame>. If <pointe
37303 72 3e 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66  r> is not specif
37304 69 65 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65  ied, then.** the
37305 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d   string <key-nam
37306 65 3e 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20  e> must already 
37307 65 78 69 73 74 20 69 6e 20 74 68 65 20 68 61 73  exist in the has
37308 20 74 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73   table. Otherwis
37309 65 2c 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69  e,.** an error i
3730a 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
3730b 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
3730c 20 74 68 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61   the <pointer> a
3730d 72 67 75 6d 65 6e 74 20 69 73 20 73 70 65 63 69  rgument is speci
3730e 66 69 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20  fied, the value 
3730f 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 61  returned.** is a
37310 20 62 6c 6f 62 20 63 6f 6e 74 61 69 6e 69 6e 67   blob containing
37311 20 74 68 65 20 70 6f 69 6e 74 65 72 20 73 74 6f   the pointer sto
37312 72 65 64 20 61 73 20 74 68 65 20 68 61 73 68 20  red as the hash 
37313 64 61 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69  data correspondi
37314 6e 67 0a 2a 2a 20 74 6f 20 73 74 72 69 6e 67 20  ng.** to string 
37315 3c 6b 65 79 2d 6e 61 6d 65 3e 20 28 61 66 74 65  <key-name> (afte
37316 72 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  r the hash-table
37317 20 69 73 20 75 70 64 61 74 65 64 2c 20 69 66 20   is updated, if 
37318 61 70 70 6c 69 63 61 62 6c 65 29 2e 0a 2a 2f 0a  applicable)..*/.
37319 73 74 61 74 69 63 20 76 6f 69 64 20 73 63 61 6c  static void scal
3731a 61 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  arFunc(.  sqlite
3731b 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
3731c 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
3731d 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
3731e 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 46 74 73 33  **argv.){.  Fts3
3731f 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 76  Hash *pHash;.  v
37320 6f 69 64 20 2a 70 50 74 72 20 3d 20 30 3b 0a 20  oid *pPtr = 0;. 
37321 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
37322 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69  char *zName;.  i
37323 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73  nt nName;..  ass
37324 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20  ert( argc==1 || 
37325 61 72 67 63 3d 3d 32 20 29 3b 0a 0a 20 20 70 48  argc==2 );..  pH
37326 61 73 68 20 3d 20 28 46 74 73 33 48 61 73 68 20  ash = (Fts3Hash 
37327 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  *)sqlite3_user_d
37328 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20  ata(context);.. 
37329 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   zName = sqlite3
3732a 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
3732b 5b 30 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20  [0]);.  nName = 
3732c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
3732d 74 65 73 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a  tes(argv[0])+1;.
3732e 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29  .  if( argc==2 )
3732f 7b 0a 20 20 20 20 76 6f 69 64 20 2a 70 4f 6c 64  {.    void *pOld
37330 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71  ;.    int n = sq
37331 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
37332 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  s(argv[1]);.    
37333 69 66 28 20 6e 21 3d 73 69 7a 65 6f 66 28 70 50  if( n!=sizeof(pP
37334 74 72 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  tr) ){.      sql
37335 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
37336 72 28 63 6f 6e 74 65 78 74 2c 20 22 61 72 67 75  r(context, "argu
37337 6d 65 6e 74 20 74 79 70 65 20 6d 69 73 6d 61 74  ment type mismat
37338 63 68 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  ch", -1);.      
37339 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
3733a 20 20 70 50 74 72 20 3d 20 2a 28 76 6f 69 64 20    pPtr = *(void 
3733b 2a 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  **)sqlite3_value
3733c 5f 62 6c 6f 62 28 61 72 67 76 5b 31 5d 29 3b 0a  _blob(argv[1]);.
3733d 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74      pOld = sqlit
3733e 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
3733f 28 70 48 61 73 68 2c 20 28 76 6f 69 64 20 2a 29  (pHash, (void *)
37340 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 50  zName, nName, pP
37341 74 72 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c  tr);.    if( pOl
37342 64 3d 3d 70 50 74 72 20 29 7b 0a 20 20 20 20 20  d==pPtr ){.     
37343 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37344 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22  error(context, "
37345 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 20  out of memory", 
37346 2d 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  -1);.      retur
37347 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  n;.    }.  }else
37348 7b 0a 20 20 20 20 70 50 74 72 20 3d 20 73 71 6c  {.    pPtr = sql
37349 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
3734a 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e  (pHash, zName, n
3734b 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21  Name);.    if( !
3734c 70 50 74 72 20 29 7b 0a 20 20 20 20 20 20 63 68  pPtr ){.      ch
3734d 61 72 20 2a 7a 45 72 72 20 3d 20 73 71 6c 69 74  ar *zErr = sqlit
3734e 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e  e3_mprintf("unkn
3734f 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25  own tokenizer: %
37350 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
37351 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
37352 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
37353 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20 20  zErr, -1);.     
37354 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
37355 72 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  rr);.      retur
37356 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
37357 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
37358 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 76 6f  lob(context, (vo
37359 69 64 20 2a 29 26 70 50 74 72 2c 20 73 69 7a 65  id *)&pPtr, size
3735a 6f 66 28 70 50 74 72 29 2c 20 53 51 4c 49 54 45  of(pPtr), SQLITE
3735b 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a  _TRANSIENT);.}..
3735c 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 49  static int fts3I
3735d 73 49 64 43 68 61 72 28 63 68 61 72 20 63 29 7b  sIdChar(char c){
3735e 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
3735f 63 68 61 72 20 69 73 46 74 73 49 64 43 68 61 72  char isFtsIdChar
37360 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 30 2c 20  [] = {.      0, 
37361 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
37362 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
37363 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
37364 20 30 78 20 2a 2f 0a 20 20 20 20 20 20 30 2c 20   0x */.      0, 
37365 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
37366 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
37367 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
37368 20 31 78 20 2a 2f 0a 20 20 20 20 20 20 30 2c 20   1x */.      0, 
37369 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30  0, 0, 0, 1, 0, 0
3736a 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
3736b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
3736c 20 32 78 20 2a 2f 0a 20 20 20 20 20 20 31 2c 20   2x */.      1, 
3736d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3736e 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c  , 1, 1, 1, 0, 0,
3736f 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
37370 20 33 78 20 2a 2f 0a 20 20 20 20 20 20 30 2c 20   3x */.      0, 
37371 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
37372 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
37373 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a   1, 1, 1, 1,  /*
37374 20 34 78 20 2a 2f 0a 20 20 20 20 20 20 31 2c 20   4x */.      1, 
37375 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
37376 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
37377 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 20 2f 2a   0, 0, 0, 1,  /*
37378 20 35 78 20 2a 2f 0a 20 20 20 20 20 20 30 2c 20   5x */.      0, 
37379 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3737a 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3737b 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a   1, 1, 1, 1,  /*
3737c 20 36 78 20 2a 2f 0a 20 20 20 20 20 20 31 2c 20   6x */.      1, 
3737d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
3737e 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
3737f 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
37380 20 37 78 20 2a 2f 0a 20 20 7d 3b 0a 20 20 72 65   7x */.  };.  re
37381 74 75 72 6e 20 28 63 26 30 78 38 30 20 7c 7c 20  turn (c&0x80 || 
37382 69 73 46 74 73 49 64 43 68 61 72 5b 28 69 6e 74  isFtsIdChar[(int
37383 29 28 63 29 5d 29 3b 0a 7d 0a 0a 53 51 4c 49 54  )(c)]);.}..SQLIT
37384 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
37385 63 68 61 72 20 2a 73 71 6c 69 74 65 33 46 74 73  char *sqlite3Fts
37386 33 4e 65 78 74 54 6f 6b 65 6e 28 63 6f 6e 73 74  3NextToken(const
37387 20 63 68 61 72 20 2a 7a 53 74 72 2c 20 69 6e 74   char *zStr, int
37388 20 2a 70 6e 29 7b 0a 20 20 63 6f 6e 73 74 20 63   *pn){.  const c
37389 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e 73 74  har *z1;.  const
3738a 20 63 68 61 72 20 2a 7a 32 20 3d 20 30 3b 0a 0a   char *z2 = 0;..
3738b 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 73 74    /* Find the st
3738c 61 72 74 20 6f 66 20 74 68 65 20 6e 65 78 74 20  art of the next 
3738d 74 6f 6b 65 6e 2e 20 2a 2f 0a 20 20 7a 31 20 3d  token. */.  z1 =
3738e 20 7a 53 74 72 3b 0a 20 20 77 68 69 6c 65 28 20   zStr;.  while( 
3738f 7a 32 3d 3d 30 20 29 7b 0a 20 20 20 20 73 77 69  z2==0 ){.    swi
37390 74 63 68 28 20 2a 7a 31 20 29 7b 0a 20 20 20 20  tch( *z1 ){.    
37391 20 20 63 61 73 65 20 27 5c 30 27 3a 20 72 65 74    case '\0': ret
37392 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  urn 0;        /*
37393 20 4e 6f 20 6d 6f 72 65 20 74 6f 6b 65 6e 73 20   No more tokens 
37394 68 65 72 65 20 2a 2f 0a 20 20 20 20 20 20 63 61  here */.      ca
37395 73 65 20 27 5c 27 27 3a 0a 20 20 20 20 20 20 63  se '\'':.      c
37396 61 73 65 20 27 22 27 3a 0a 20 20 20 20 20 20 63  ase '"':.      c
37397 61 73 65 20 27 60 27 3a 20 7b 0a 20 20 20 20 20  ase '`': {.     
37398 20 20 20 7a 32 20 3d 20 26 7a 31 5b 31 5d 3b 0a     z2 = &z1[1];.
37399 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a          while( *
3739a 7a 32 20 26 26 20 28 7a 32 5b 30 5d 21 3d 2a 7a  z2 && (z2[0]!=*z
3739b 31 20 7c 7c 20 7a 32 5b 31 5d 3d 3d 2a 7a 31 29  1 || z2[1]==*z1)
3739c 20 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20   ) z2++;.       
3739d 20 69 66 28 20 2a 7a 32 20 29 20 7a 32 2b 2b 3b   if( *z2 ) z2++;
3739e 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3739f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
373a0 73 65 20 27 5b 27 3a 0a 20 20 20 20 20 20 20 20  se '[':.        
373a1 7a 32 20 3d 20 26 7a 31 5b 31 5d 3b 0a 20 20 20  z2 = &z1[1];.   
373a2 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 32 20       while( *z2 
373a3 26 26 20 7a 32 5b 30 5d 21 3d 27 5d 27 20 29 20  && z2[0]!=']' ) 
373a4 7a 32 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 66  z2++;.        if
373a5 28 20 2a 7a 32 20 29 20 7a 32 2b 2b 3b 0a 20 20  ( *z2 ) z2++;.  
373a6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20        break;..  
373a7 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
373a8 20 20 20 20 20 69 66 28 20 66 74 73 33 49 73 49       if( fts3IsI
373a9 64 43 68 61 72 28 2a 7a 31 29 20 29 7b 0a 20 20  dChar(*z1) ){.  
373aa 20 20 20 20 20 20 20 20 7a 32 20 3d 20 26 7a 31          z2 = &z1
373ab 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 77  [1];.          w
373ac 68 69 6c 65 28 20 66 74 73 33 49 73 49 64 43 68  hile( fts3IsIdCh
373ad 61 72 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a  ar(*z2) ) z2++;.
373ae 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
373af 20 20 20 20 20 20 20 20 20 7a 31 2b 2b 3b 0a 20           z1++;. 
373b0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20         }.    }. 
373b1 20 7d 0a 0a 20 20 2a 70 6e 20 3d 20 28 69 6e 74   }..  *pn = (int
373b2 29 28 7a 32 2d 7a 31 29 3b 0a 20 20 72 65 74 75  )(z2-z1);.  retu
373b3 72 6e 20 7a 31 3b 0a 7d 0a 0a 53 51 4c 49 54 45  rn z1;.}..SQLITE
373b4 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
373b5 69 74 65 33 46 74 73 33 49 6e 69 74 54 6f 6b 65  ite3Fts3InitToke
373b6 6e 69 7a 65 72 28 0a 20 20 46 74 73 33 48 61 73  nizer(.  Fts3Has
373b7 68 20 2a 70 48 61 73 68 2c 20 20 20 20 20 20 20  h *pHash,       
373b8 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65           /* Toke
373b9 6e 69 7a 65 72 20 68 61 73 68 20 74 61 62 6c 65  nizer hash table
373ba 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
373bb 20 2a 7a 41 72 67 2c 20 20 20 20 20 20 20 20 20   *zArg,         
373bc 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69 62 6c        /* Possibl
373bd 65 20 74 6f 6b 65 6e 69 7a 65 72 20 73 70 65 63  e tokenizer spec
373be 69 66 69 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73  ification */.  s
373bf 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
373c0 20 2a 2a 70 70 54 6f 6b 2c 20 20 20 20 20 20 2f   **ppTok,      /
373c1 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 65 72  * OUT: Tokenizer
373c2 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29   (if applicable)
373c3 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
373c4 20 2a 2a 70 7a 54 6f 6b 65 6e 69 7a 65 72 2c 20   **pzTokenizer, 
373c5 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65        /* OUT: Se
373c6 74 20 74 6f 20 7a 41 72 67 20 69 66 20 69 73 20  t to zArg if is 
373c7 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63  tokenizer */.  c
373c8 68 61 72 20 2a 2a 70 7a 45 72 72 20 20 20 20 20  har **pzErr     
373c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
373ca 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 6d 61  * OUT: Set to ma
373cb 6c 6c 6f 63 65 64 20 65 72 72 6f 72 20 6d 65 73  lloced error mes
373cc 73 61 67 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  sage */.){.  int
373cd 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 20 3d   rc;.  char *z =
373ce 20 28 63 68 61 72 20 2a 29 7a 41 72 67 3b 0a 20   (char *)zArg;. 
373cf 20 69 6e 74 20 6e 3b 0a 20 20 63 68 61 72 20 2a   int n;.  char *
373d0 7a 43 6f 70 79 3b 0a 20 20 63 68 61 72 20 2a 7a  zCopy;.  char *z
373d1 45 6e 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  End;            
373d2 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
373d3 74 65 72 20 74 6f 20 6e 75 6c 2d 74 65 72 6d 20  ter to nul-term 
373d4 6f 66 20 7a 43 6f 70 79 20 2a 2f 0a 20 20 73 71  of zCopy */.  sq
373d5 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
373d6 6d 6f 64 75 6c 65 20 2a 6d 3b 0a 0a 20 20 69 66  module *m;..  if
373d7 28 20 21 7a 20 29 7b 0a 20 20 20 20 7a 43 6f 70  ( !z ){.    zCop
373d8 79 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  y = sqlite3_mpri
373d9 6e 74 66 28 22 73 69 6d 70 6c 65 22 29 3b 0a 20  ntf("simple");. 
373da 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
373db 73 71 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70  sqlite3_strnicmp
373dc 28 7a 2c 20 22 74 6f 6b 65 6e 69 7a 65 22 2c 20  (z, "tokenize", 
373dd 38 29 20 7c 7c 20 66 74 73 33 49 73 49 64 43 68  8) || fts3IsIdCh
373de 61 72 28 7a 5b 38 5d 29 29 7b 0a 20 20 20 20 20  ar(z[8])){.     
373df 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
373e0 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 43 6f  K;.    }.    zCo
373e1 70 79 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  py = sqlite3_mpr
373e2 69 6e 74 66 28 22 25 73 22 2c 20 26 7a 5b 38 5d  intf("%s", &z[8]
373e3 29 3b 0a 20 20 20 20 2a 70 7a 54 6f 6b 65 6e 69  );.    *pzTokeni
373e4 7a 65 72 20 3d 20 7a 41 72 67 3b 0a 20 20 7d 0a  zer = zArg;.  }.
373e5 20 20 69 66 28 20 21 7a 43 6f 70 79 20 29 7b 0a    if( !zCopy ){.
373e6 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
373e7 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
373e8 7a 45 6e 64 20 3d 20 26 7a 43 6f 70 79 5b 73 74  zEnd = &zCopy[st
373e9 72 6c 65 6e 28 7a 43 6f 70 79 29 5d 3b 0a 0a 20  rlen(zCopy)];.. 
373ea 20 7a 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c   z = (char *)sql
373eb 69 74 65 33 46 74 73 33 4e 65 78 74 54 6f 6b 65  ite3Fts3NextToke
373ec 6e 28 7a 43 6f 70 79 2c 20 26 6e 29 3b 0a 20 20  n(zCopy, &n);.  
373ed 7a 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 73  z[n] = '\0';.  s
373ee 71 6c 69 74 65 33 46 74 73 33 44 65 71 75 6f 74  qlite3Fts3Dequot
373ef 65 28 7a 29 3b 0a 0a 20 20 6d 20 3d 20 28 73 71  e(z);..  m = (sq
373f0 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
373f1 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33  module *)sqlite3
373f2 46 74 73 33 48 61 73 68 46 69 6e 64 28 70 48 61  Fts3HashFind(pHa
373f3 73 68 2c 20 7a 2c 20 28 69 6e 74 29 73 74 72 6c  sh, z, (int)strl
373f4 65 6e 28 7a 29 2b 31 29 3b 0a 20 20 69 66 28 20  en(z)+1);.  if( 
373f5 21 6d 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72  !m ){.    *pzErr
373f6 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
373f7 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65  tf("unknown toke
373f8 6e 69 7a 65 72 3a 20 25 73 22 2c 20 7a 29 3b 0a  nizer: %s", z);.
373f9 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
373fa 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ERROR;.  }else{.
373fb 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a      char const *
373fc 2a 61 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 69  *aArg = 0;.    i
373fd 6e 74 20 69 41 72 67 20 3d 20 30 3b 0a 20 20 20  nt iArg = 0;.   
373fe 20 7a 20 3d 20 26 7a 5b 6e 2b 31 5d 3b 0a 20 20   z = &z[n+1];.  
373ff 20 20 77 68 69 6c 65 28 20 7a 3c 7a 45 6e 64 20    while( z<zEnd 
37400 26 26 20 28 4e 55 4c 4c 21 3d 28 7a 20 3d 20 28  && (NULL!=(z = (
37401 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 46 74  char *)sqlite3Ft
37402 73 33 4e 65 78 74 54 6f 6b 65 6e 28 7a 2c 20 26  s3NextToken(z, &
37403 6e 29 29 29 20 29 7b 0a 20 20 20 20 20 20 69 6e  n))) ){.      in
37404 74 20 6e 4e 65 77 20 3d 20 73 69 7a 65 6f 66 28  t nNew = sizeof(
37405 63 68 61 72 20 2a 29 2a 28 69 41 72 67 2b 31 29  char *)*(iArg+1)
37406 3b 0a 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e  ;.      char con
37407 73 74 20 2a 2a 61 4e 65 77 20 3d 20 28 63 6f 6e  st **aNew = (con
37408 73 74 20 63 68 61 72 20 2a 2a 29 73 71 6c 69 74  st char **)sqlit
37409 65 33 5f 72 65 61 6c 6c 6f 63 28 28 76 6f 69 64  e3_realloc((void
3740a 20 2a 29 61 41 72 67 2c 20 6e 4e 65 77 29 3b 0a   *)aArg, nNew);.
3740b 20 20 20 20 20 20 69 66 28 20 21 61 4e 65 77 20        if( !aNew 
3740c 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
3740d 65 33 5f 66 72 65 65 28 7a 43 6f 70 79 29 3b 0a  e3_free(zCopy);.
3740e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
3740f 66 72 65 65 28 28 76 6f 69 64 20 2a 29 61 41 72  free((void *)aAr
37410 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  g);.        retu
37411 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
37412 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
37413 41 72 67 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20  Arg = aNew;.    
37414 20 20 61 41 72 67 5b 69 41 72 67 2b 2b 5d 20 3d    aArg[iArg++] =
37415 20 7a 3b 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d   z;.      z[n] =
37416 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 73 71 6c   '\0';.      sql
37417 69 74 65 33 46 74 73 33 44 65 71 75 6f 74 65 28  ite3Fts3Dequote(
37418 7a 29 3b 0a 20 20 20 20 20 20 7a 20 3d 20 26 7a  z);.      z = &z
37419 5b 6e 2b 31 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  [n+1];.    }.   
3741a 20 72 63 20 3d 20 6d 2d 3e 78 43 72 65 61 74 65   rc = m->xCreate
3741b 28 69 41 72 67 2c 20 61 41 72 67 2c 20 70 70 54  (iArg, aArg, ppT
3741c 6f 6b 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ok);.    assert(
3741d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
3741e 7c 20 2a 70 70 54 6f 6b 20 29 3b 0a 20 20 20 20  | *ppTok );.    
3741f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
37420 4b 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72  K ){.      *pzEr
37421 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  r = sqlite3_mpri
37422 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b  ntf("unknown tok
37423 65 6e 69 7a 65 72 22 29 3b 0a 20 20 20 20 7d 65  enizer");.    }e
37424 6c 73 65 7b 0a 20 20 20 20 20 20 28 2a 70 70 54  lse{.      (*ppT
37425 6f 6b 29 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 6d  ok)->pModule = m
37426 3b 20 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  ; .    }.    sql
37427 69 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64 20  ite3_free((void 
37428 2a 29 61 41 72 67 29 3b 0a 20 20 7d 0a 0a 20 20  *)aArg);.  }..  
37429 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f  sqlite3_free(zCo
3742a 70 79 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  py);.  return rc
3742b 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c  ;.}...#ifdef SQL
3742c 49 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a 2a  ITE_TEST.../*.**
3742d 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
3742e 6f 66 20 61 20 73 70 65 63 69 61 6c 20 53 51 4c  of a special SQL
3742f 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
37430 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 6f 6b   for testing tok
37431 65 6e 69 7a 65 72 73 20 0a 2a 2a 20 64 65 73 69  enizers .** desi
37432 67 6e 65 64 20 74 6f 20 62 65 20 75 73 65 64 20  gned to be used 
37433 69 6e 20 63 6f 6e 63 65 72 74 20 77 69 74 68 20  in concert with 
37434 74 68 65 20 54 63 6c 20 74 65 73 74 69 6e 67 20  the Tcl testing 
37435 66 72 61 6d 65 77 6f 72 6b 2e 20 54 68 69 73 0a  framework. This.
37436 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  ** function must
37437 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20   be called with 
37438 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 3a 0a 2a  two arguments:.*
37439 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c 66  *.**   SELECT <f
3743a 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c 6b  unction-name>(<k
3743b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 69 6e 70 75 74  ey-name>, <input
3743c 2d 73 74 72 69 6e 67 3e 29 3b 0a 2a 2a 20 20 20  -string>);.**   
3743d 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e  SELECT <function
3743e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65  -name>(<key-name
3743f 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a 2a  >, <pointer>);.*
37440 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e 63  *.** where <func
37441 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68  tion-name> is th
37442 65 20 6e 61 6d 65 20 70 61 73 73 65 64 20 61 73  e name passed as
37443 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
37444 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 73  ment.** to the s
37445 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61  qlite3Fts3InitHa
37446 73 68 54 61 62 6c 65 28 29 20 66 75 6e 63 74 69  shTable() functi
37447 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74  on (e.g. 'fts3_t
37448 6f 6b 65 6e 69 7a 65 72 27 29 0a 2a 2a 20 63 6f  okenizer').** co
37449 6e 63 61 74 65 6e 61 74 65 64 20 77 69 74 68 20  ncatenated with 
3744a 74 68 65 20 73 74 72 69 6e 67 20 27 5f 74 65 73  the string '_tes
3744b 74 27 20 28 65 2e 67 2e 20 27 66 74 73 33 5f 74  t' (e.g. 'fts3_t
3744c 6f 6b 65 6e 69 7a 65 72 5f 74 65 73 74 27 29 2e  okenizer_test').
3744d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
3744e 6e 20 76 61 6c 75 65 20 69 73 20 61 20 73 74 72  n value is a str
3744f 69 6e 67 20 74 68 61 74 20 6d 61 79 20 62 65 20  ing that may be 
37450 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61  interpreted as a
37451 20 54 63 6c 0a 2a 2a 20 6c 69 73 74 2e 20 46 6f   Tcl.** list. Fo
37452 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 69 6e 20  r each token in 
37453 74 68 65 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e  the <input-strin
37454 67 3e 2c 20 74 68 72 65 65 20 65 6c 65 6d 65 6e  g>, three elemen
37455 74 73 20 61 72 65 0a 2a 2a 20 61 64 64 65 64 20  ts are.** added 
37456 74 6f 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  to the returned 
37457 6c 69 73 74 2e 20 54 68 65 20 66 69 72 73 74 20  list. The first 
37458 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 70 6f 73  is the token pos
37459 69 74 69 6f 6e 2c 20 74 68 65 20 0a 2a 2a 20 73  ition, the .** s
3745a 65 63 6f 6e 64 20 69 73 20 74 68 65 20 74 6f 6b  econd is the tok
3745b 65 6e 20 74 65 78 74 20 28 66 6f 6c 64 65 64 2c  en text (folded,
3745c 20 73 74 65 6d 6d 65 64 2c 20 65 74 63 2e 29 20   stemmed, etc.) 
3745d 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 69 73  and the third is
3745e 20 74 68 65 0a 2a 2a 20 73 75 62 73 74 72 69 6e   the.** substrin
3745f 67 20 6f 66 20 3c 69 6e 70 75 74 2d 73 74 72 69  g of <input-stri
37460 6e 67 3e 20 61 73 73 6f 63 69 61 74 65 64 20 77  ng> associated w
37461 69 74 68 20 74 68 65 20 74 6f 6b 65 6e 2e 20 46  ith the token. F
37462 6f 72 20 65 78 61 6d 70 6c 65 2c 20 0a 2a 2a 20  or example, .** 
37463 75 73 69 6e 67 20 74 68 65 20 62 75 69 6c 74 2d  using the built-
37464 69 6e 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b 65  in "simple" toke
37465 6e 69 7a 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 53  nizer:.**.**   S
37466 45 4c 45 43 54 20 66 74 73 5f 74 6f 6b 65 6e 69  ELECT fts_tokeni
37467 7a 65 72 5f 74 65 73 74 28 27 73 69 6d 70 6c 65  zer_test('simple
37468 27 2c 20 27 49 20 64 6f 6e 27 74 20 73 65 65 20  ', 'I don't see 
37469 68 6f 77 27 29 3b 0a 2a 2a 0a 2a 2a 20 77 69 6c  how');.**.** wil
3746a 6c 20 72 65 74 75 72 6e 20 74 68 65 20 73 74 72  l return the str
3746b 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 7b 30  ing:.**.**   "{0
3746c 20 69 20 49 20 31 20 64 6f 6e 74 20 64 6f 6e 27   i I 1 dont don'
3746d 74 20 32 20 73 65 65 20 73 65 65 20 33 20 68 6f  t 2 see see 3 ho
3746e 77 20 68 6f 77 7d 22 0a 2a 2a 20 20 20 0a 2a 2f  w how}".**   .*/
3746f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73  .static void tes
37470 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  tFunc(.  sqlite3
37471 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
37472 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
37473 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
37474 2a 61 72 67 76 0a 29 7b 0a 20 20 46 74 73 33 48  *argv.){.  Fts3H
37475 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 73 71  ash *pHash;.  sq
37476 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
37477 6d 6f 64 75 6c 65 20 2a 70 3b 0a 20 20 73 71 6c  module *p;.  sql
37478 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
37479 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 0a  pTokenizer = 0;.
3747a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3747b 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 73 72  zer_cursor *pCsr
3747c 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20 63   = 0;..  const c
3747d 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a  har *zErr = 0;..
3747e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
3747f 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  ame;.  int nName
37480 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
37481 7a 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e 49  zInput;.  int nI
37482 6e 70 75 74 3b 0a 0a 20 20 63 6f 6e 73 74 20 63  nput;..  const c
37483 68 61 72 20 2a 7a 41 72 67 20 3d 20 30 3b 0a 0a  har *zArg = 0;..
37484 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
37485 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b  oken;.  int nTok
37486 65 6e 3b 0a 20 20 69 6e 74 20 69 53 74 61 72 74  en;.  int iStart
37487 3b 0a 20 20 69 6e 74 20 69 45 6e 64 3b 0a 20 20  ;.  int iEnd;.  
37488 69 6e 74 20 69 50 6f 73 3b 0a 0a 20 20 54 63 6c  int iPos;..  Tcl
37489 5f 4f 62 6a 20 2a 70 52 65 74 3b 0a 0a 20 20 61  _Obj *pRet;..  a
3748a 73 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20 7c  ssert( argc==2 |
3748b 7c 20 61 72 67 63 3d 3d 33 20 29 3b 0a 0a 20 20  | argc==3 );..  
3748c 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f  nName = sqlite3_
3748d 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
3748e 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  [0]);.  zName = 
3748f 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
37490 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
37491 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 49 6e  (argv[0]);.  nIn
37492 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  put = sqlite3_va
37493 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 61  lue_bytes(argv[a
37494 72 67 63 2d 31 5d 29 3b 0a 20 20 7a 49 6e 70 75  rgc-1]);.  zInpu
37495 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  t = (const char 
37496 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
37497 74 65 78 74 28 61 72 67 76 5b 61 72 67 63 2d 31  text(argv[argc-1
37498 5d 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d  ]);..  if( argc=
37499 3d 33 20 29 7b 0a 20 20 20 20 7a 41 72 67 20 3d  =3 ){.    zArg =
3749a 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
3749b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
3749c 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 0a  t(argv[1]);.  }.
3749d 0a 20 20 70 48 61 73 68 20 3d 20 28 46 74 73 33  .  pHash = (Fts3
3749e 48 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f 75  Hash *)sqlite3_u
3749f 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
374a0 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74 65  );.  p = (sqlite
374a1 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
374a2 6c 65 20 2a 29 73 71 6c 69 74 65 33 46 74 73 33  le *)sqlite3Fts3
374a3 48 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20  HashFind(pHash, 
374a4 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b  zName, nName+1);
374a5 0a 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20  ..  if( !p ){.  
374a6 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 73    char *zErr = s
374a7 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
374a8 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65  unknown tokenize
374a9 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  r: %s", zName);.
374aa 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
374ab 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
374ac 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 20  , zErr, -1);.   
374ad 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
374ae 72 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  rr);.    return;
374af 0a 20 20 7d 0a 0a 20 20 70 52 65 74 20 3d 20 54  .  }..  pRet = T
374b0 63 6c 5f 4e 65 77 4f 62 6a 28 29 3b 0a 20 20 54  cl_NewObj();.  T
374b1 63 6c 5f 49 6e 63 72 52 65 66 43 6f 75 6e 74 28  cl_IncrRefCount(
374b2 70 52 65 74 29 3b 0a 0a 20 20 69 66 28 20 53 51  pRet);..  if( SQ
374b3 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 72 65  LITE_OK!=p->xCre
374b4 61 74 65 28 7a 41 72 67 20 3f 20 31 20 3a 20 30  ate(zArg ? 1 : 0
374b5 2c 20 26 7a 41 72 67 2c 20 26 70 54 6f 6b 65 6e  , &zArg, &pToken
374b6 69 7a 65 72 29 20 29 7b 0a 20 20 20 20 7a 45 72  izer) ){.    zEr
374b7 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 43  r = "error in xC
374b8 72 65 61 74 65 28 29 22 3b 0a 20 20 20 20 67 6f  reate()";.    go
374b9 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20  to finish;.  }. 
374ba 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
374bb 64 75 6c 65 20 3d 20 70 3b 0a 20 20 69 66 28 20  dule = p;.  if( 
374bc 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 4f  SQLITE_OK!=p->xO
374bd 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  pen(pTokenizer, 
374be 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20  zInput, nInput, 
374bf 26 70 43 73 72 29 20 29 7b 0a 20 20 20 20 7a 45  &pCsr) ){.    zE
374c0 72 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78  rr = "error in x
374c1 4f 70 65 6e 28 29 22 3b 0a 20 20 20 20 67 6f 74  Open()";.    got
374c2 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20  o finish;.  }.  
374c3 70 43 73 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72  pCsr->pTokenizer
374c4 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 0a   = pTokenizer;..
374c5 20 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f    while( SQLITE_
374c6 4f 4b 3d 3d 70 2d 3e 78 4e 65 78 74 28 70 43 73  OK==p->xNext(pCs
374c7 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f  r, &zToken, &nTo
374c8 6b 65 6e 2c 20 26 69 53 74 61 72 74 2c 20 26 69  ken, &iStart, &i
374c9 45 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a 20  End, &iPos) ){. 
374ca 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70     Tcl_ListObjAp
374cb 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70  pendElement(0, p
374cc 52 65 74 2c 20 54 63 6c 5f 4e 65 77 49 6e 74 4f  Ret, Tcl_NewIntO
374cd 62 6a 28 69 50 6f 73 29 29 3b 0a 20 20 20 20 54  bj(iPos));.    T
374ce 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e 64  cl_ListObjAppend
374cf 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74 2c  Element(0, pRet,
374d0 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62   Tcl_NewStringOb
374d1 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  j(zToken, nToken
374d2 29 29 3b 0a 20 20 20 20 7a 54 6f 6b 65 6e 20 3d  ));.    zToken =
374d3 20 26 7a 49 6e 70 75 74 5b 69 53 74 61 72 74 5d   &zInput[iStart]
374d4 3b 0a 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 69  ;.    nToken = i
374d5 45 6e 64 2d 69 53 74 61 72 74 3b 0a 20 20 20 20  End-iStart;.    
374d6 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e  Tcl_ListObjAppen
374d7 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74  dElement(0, pRet
374d8 2c 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f  , Tcl_NewStringO
374d9 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65  bj(zToken, nToke
374da 6e 29 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  n));.  }..  if( 
374db 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43  SQLITE_OK!=p->xC
374dc 6c 6f 73 65 28 70 43 73 72 29 20 29 7b 0a 20 20  lose(pCsr) ){.  
374dd 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72 20    zErr = "error 
374de 69 6e 20 78 43 6c 6f 73 65 28 29 22 3b 0a 20 20  in xClose()";.  
374df 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20    goto finish;. 
374e0 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f   }.  if( SQLITE_
374e1 4f 4b 21 3d 70 2d 3e 78 44 65 73 74 72 6f 79 28  OK!=p->xDestroy(
374e2 70 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a 20  pTokenizer) ){. 
374e3 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72     zErr = "error
374e4 20 69 6e 20 78 44 65 73 74 72 6f 79 28 29 22 3b   in xDestroy()";
374e5 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68  .    goto finish
374e6 3b 0a 20 20 7d 0a 0a 66 69 6e 69 73 68 3a 0a 20  ;.  }..finish:. 
374e7 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20   if( zErr ){.   
374e8 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
374e9 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a  error(context, z
374ea 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 65 6c 73  Err, -1);.  }els
374eb 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  e{.    sqlite3_r
374ec 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
374ed 78 74 2c 20 54 63 6c 5f 47 65 74 53 74 72 69 6e  xt, Tcl_GetStrin
374ee 67 28 70 52 65 74 29 2c 20 2d 31 2c 20 53 51 4c  g(pRet), -1, SQL
374ef 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
374f0 20 20 7d 0a 20 20 54 63 6c 5f 44 65 63 72 52 65    }.  Tcl_DecrRe
374f1 66 43 6f 75 6e 74 28 70 52 65 74 29 3b 0a 7d 0a  fCount(pRet);.}.
374f2 0a 73 74 61 74 69 63 0a 69 6e 74 20 72 65 67 69  .static.int regi
374f3 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28 0a 20  sterTokenizer(. 
374f4 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
374f5 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
374f6 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
374f7 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
374f8 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  *p.){.  int rc;.
374f9 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
374fa 70 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63  pStmt;.  const c
374fb 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45  har zSql[] = "SE
374fc 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69  LECT fts3_tokeni
374fd 7a 65 72 28 3f 2c 20 3f 29 22 3b 0a 0a 20 20 72  zer(?, ?)";..  r
374fe 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70  c = sqlite3_prep
374ff 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c  are_v2(db, zSql,
37500 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b   -1, &pStmt, 0);
37501 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
37502 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
37503 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71  rn rc;.  }..  sq
37504 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28  lite3_bind_text(
37505 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c  pStmt, 1, zName,
37506 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54   -1, SQLITE_STAT
37507 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62  IC);.  sqlite3_b
37508 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20  ind_blob(pStmt, 
37509 32 2c 20 26 70 2c 20 73 69 7a 65 6f 66 28 70 29  2, &p, sizeof(p)
3750a 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
3750b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ;.  sqlite3_step
3750c 28 70 53 74 6d 74 29 3b 0a 0a 20 20 72 65 74 75  (pStmt);..  retu
3750d 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  rn sqlite3_final
3750e 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 73  ize(pStmt);.}..s
3750f 74 61 74 69 63 0a 69 6e 74 20 71 75 65 72 79 54  tatic.int queryT
37510 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69  okenizer(.  sqli
37511 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61 72  te3 *db, .  char
37512 20 2a 7a 4e 61 6d 65 2c 20 20 0a 20 20 63 6f 6e   *zName,  .  con
37513 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  st sqlite3_token
37514 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70 70  izer_module **pp
37515 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
37516 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
37517 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  tmt;.  const cha
37518 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c 45  r zSql[] = "SELE
37519 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65  CT fts3_tokenize
3751a 72 28 3f 29 22 3b 0a 0a 20 20 2a 70 70 20 3d 20  r(?)";..  *pp = 
3751b 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  0;.  rc = sqlite
3751c 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c  3_prepare_v2(db,
3751d 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d   zSql, -1, &pStm
3751e 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21  t, 0);.  if( rc!
3751f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
37520 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
37521 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ..  sqlite3_bind
37522 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 31 2c 20  _text(pStmt, 1, 
37523 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49 54  zName, -1, SQLIT
37524 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28  E_STATIC);.  if(
37525 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c   SQLITE_ROW==sql
37526 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29  ite3_step(pStmt)
37527 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
37528 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
37529 70 53 74 6d 74 2c 20 30 29 3d 3d 53 51 4c 49 54  pStmt, 0)==SQLIT
3752a 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20  E_BLOB ){.      
3752b 6d 65 6d 63 70 79 28 28 76 6f 69 64 20 2a 29 70  memcpy((void *)p
3752c 70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  p, sqlite3_colum
3752d 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29  n_blob(pStmt, 0)
3752e 2c 20 73 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a  , sizeof(*pp));.
3752f 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
37530 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
37531 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
37532 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
37533 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
37534 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
37535 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  dule(sqlite3_tok
37536 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
37537 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a  nst**ppModule);.
37538 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
37539 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 61  ation of the sca
3753a 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66 74 73  lar function fts
3753b 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65  3_tokenizer_inte
3753c 72 6e 61 6c 5f 74 65 73 74 28 29 2e 0a 2a 2a 20  rnal_test()..** 
3753d 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
3753e 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
3753f 67 20 6f 6e 6c 79 2c 20 69 74 20 69 73 20 6e 6f  g only, it is no
37540 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  t included in th
37541 65 0a 2a 2a 20 62 75 69 6c 64 20 75 6e 6c 65 73  e.** build unles
37542 73 20 53 51 4c 49 54 45 5f 54 45 53 54 20 69 73  s SQLITE_TEST is
37543 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20   defined..**.** 
37544 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74  The purpose of t
37545 68 69 73 20 69 73 20 74 6f 20 74 65 73 74 20 74  his is to test t
37546 68 61 74 20 74 68 65 20 66 74 73 33 5f 74 6f 6b  hat the fts3_tok
37547 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74 69 6f  enizer() functio
37548 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64  n.** can be used
37549 20 61 73 20 64 65 73 69 67 6e 65 64 20 62 79 20   as designed by 
3754a 74 68 65 20 43 2d 63 6f 64 65 20 69 6e 20 74 68  the C-code in th
3754b 65 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72  e queryTokenizer
3754c 20 61 6e 64 0a 2a 2a 20 72 65 67 69 73 74 65 72   and.** register
3754d 54 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75 6e 63  Tokenizer() func
3754e 74 69 6f 6e 73 20 61 62 6f 76 65 2e 20 54 68 65  tions above. The
3754f 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73  se two functions
37550 20 61 72 65 20 72 65 70 65 61 74 65 64 0a 2a 2a   are repeated.**
37551 20 69 6e 20 74 68 65 20 52 45 41 44 4d 45 2e 74   in the README.t
37552 6f 6b 65 6e 69 7a 65 72 20 66 69 6c 65 20 61 73  okenizer file as
37553 20 61 6e 20 65 78 61 6d 70 6c 65 2c 20 73 6f 20   an example, so 
37554 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  it is important 
37555 74 6f 0a 2a 2a 20 74 65 73 74 20 74 68 65 6d 2e  to.** test them.
37556 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 75 6e 20 74 68  .**.** To run th
37557 65 20 74 65 73 74 73 2c 20 65 76 61 6c 75 61 74  e tests, evaluat
37558 65 20 74 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e  e the fts3_token
37559 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65  izer_internal_te
3755a 73 74 28 29 20 73 63 61 6c 61 72 0a 2a 2a 20 66  st() scalar.** f
3755b 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6e 6f 20  unction with no 
3755c 61 72 67 75 6d 65 6e 74 73 2e 20 41 6e 20 61 73  arguments. An as
3755d 73 65 72 74 28 29 20 77 69 6c 6c 20 66 61 69 6c  sert() will fail
3755e 20 69 66 20 61 20 70 72 6f 62 6c 65 6d 20 69 73   if a problem is
3755f 0a 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 69 2e  .** detected. i.
37560 65 2e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  e.:.**.**     SE
37561 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69  LECT fts3_tokeni
37562 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73  zer_internal_tes
37563 74 28 29 3b 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69  t();.**.*/.stati
37564 63 20 76 6f 69 64 20 69 6e 74 54 65 73 74 46 75  c void intTestFu
37565 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
37566 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
37567 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
37568 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
37569 67 76 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  gv.){.  int rc;.
3756a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
3756b 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
3756c 20 2a 70 31 3b 0a 20 20 63 6f 6e 73 74 20 73 71   *p1;.  const sq
3756d 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3756e 6d 6f 64 75 6c 65 20 2a 70 32 3b 0a 20 20 73 71  module *p2;.  sq
3756f 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c  lite3 *db = (sql
37570 69 74 65 33 20 2a 29 73 71 6c 69 74 65 33 5f 75  ite3 *)sqlite3_u
37571 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
37572 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  );..  UNUSED_PAR
37573 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20  AMETER(argc);.  
37574 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
37575 28 61 72 67 76 29 3b 0a 0a 20 20 2f 2a 20 54 65  (argv);..  /* Te
37576 73 74 20 74 68 65 20 71 75 65 72 79 20 66 75 6e  st the query fun
37577 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  ction */.  sqlit
37578 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65  e3Fts3SimpleToke
37579 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26 70 31 29  nizerModule(&p1)
3757a 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54 6f  ;.  rc = queryTo
3757b 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 73 69 6d  kenizer(db, "sim
3757c 70 6c 65 22 2c 20 26 70 32 29 3b 0a 20 20 61 73  ple", &p2);.  as
3757d 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
3757e 5f 4f 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _OK );.  assert(
3757f 20 70 31 3d 3d 70 32 20 29 3b 0a 20 20 72 63 20   p1==p2 );.  rc 
37580 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72  = queryTokenizer
37581 28 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65  (db, "nosuchtoke
37582 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b 0a 20 20  nizer", &p2);.  
37583 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
37584 54 45 5f 45 52 52 4f 52 20 29 3b 0a 20 20 61 73  TE_ERROR );.  as
37585 73 65 72 74 28 20 70 32 3d 3d 30 20 29 3b 0a 20  sert( p2==0 );. 
37586 20 61 73 73 65 72 74 28 20 30 3d 3d 73 74 72 63   assert( 0==strc
37587 6d 70 28 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  mp(sqlite3_errms
37588 67 28 64 62 29 2c 20 22 75 6e 6b 6e 6f 77 6e 20  g(db), "unknown 
37589 74 6f 6b 65 6e 69 7a 65 72 3a 20 6e 6f 73 75 63  tokenizer: nosuc
3758a 68 74 6f 6b 65 6e 69 7a 65 72 22 29 20 29 3b 0a  htokenizer") );.
3758b 0a 20 20 2f 2a 20 54 65 73 74 20 74 68 65 20 73  .  /* Test the s
3758c 74 6f 72 61 67 65 20 66 75 6e 63 74 69 6f 6e 20  torage function 
3758d 2a 2f 0a 20 20 72 63 20 3d 20 72 65 67 69 73 74  */.  rc = regist
3758e 65 72 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20  erTokenizer(db, 
3758f 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72  "nosuchtokenizer
37590 22 2c 20 70 31 29 3b 0a 20 20 61 73 73 65 72 74  ", p1);.  assert
37591 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
37592 29 3b 0a 20 20 72 63 20 3d 20 71 75 65 72 79 54  );.  rc = queryT
37593 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f  okenizer(db, "no
37594 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20  suchtokenizer", 
37595 26 70 32 29 3b 0a 20 20 61 73 73 65 72 74 28 20  &p2);.  assert( 
37596 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
37597 0a 20 20 61 73 73 65 72 74 28 20 70 32 3d 3d 70  .  assert( p2==p
37598 31 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  1 );..  sqlite3_
37599 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
3759a 65 78 74 2c 20 22 6f 6b 22 2c 20 2d 31 2c 20 53  ext, "ok", -1, S
3759b 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d  QLITE_STATIC);.}
3759c 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ..#endif../*.** 
3759d 53 65 74 20 75 70 20 53 51 4c 20 6f 62 6a 65 63  Set up SQL objec
3759e 74 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 64  ts in database d
3759f 62 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  b used to access
375a0 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
375a1 0a 2a 2a 20 74 68 65 20 68 61 73 68 20 74 61 62  .** the hash tab
375a2 6c 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  le pointed to by
375a3 20 61 72 67 75 6d 65 6e 74 20 70 48 61 73 68 2e   argument pHash.
375a4 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   The hash table 
375a5 6d 75 73 74 0a 2a 2a 20 62 65 65 6e 20 69 6e 69  must.** been ini
375a6 74 69 61 6c 69 73 65 64 20 74 6f 20 75 73 65 20  tialised to use 
375a7 73 74 72 69 6e 67 20 6b 65 79 73 2c 20 61 6e 64  string keys, and
375a8 20 74 6f 20 74 61 6b 65 20 61 20 70 72 69 76 61   to take a priva
375a9 74 65 20 63 6f 70 79 20 0a 2a 2a 20 6f 66 20 74  te copy .** of t
375aa 68 65 20 6b 65 79 20 77 68 65 6e 20 61 20 76 61  he key when a va
375ab 6c 75 65 20 69 73 20 69 6e 73 65 72 74 65 64 2e  lue is inserted.
375ac 20 69 2e 65 2e 20 62 79 20 61 20 63 61 6c 6c 20   i.e. by a call 
375ad 73 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a 2a  similar to:.**.*
375ae 2a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33  *    sqlite3Fts3
375af 48 61 73 68 49 6e 69 74 28 70 48 61 73 68 2c 20  HashInit(pHash, 
375b0 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47  FTS3_HASH_STRING
375b1 2c 20 31 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 69 73  , 1);.**.** This
375b2 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 73 20 61   function adds a
375b3 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
375b4 20 28 73 65 65 20 68 65 61 64 65 72 20 63 6f 6d   (see header com
375b5 6d 65 6e 74 20 61 62 6f 76 65 0a 2a 2a 20 73 63  ment above.** sc
375b6 61 6c 61 72 46 75 6e 63 28 29 20 69 6e 20 74 68  alarFunc() in th
375b7 69 73 20 66 69 6c 65 20 66 6f 72 20 64 65 74 61  is file for deta
375b8 69 6c 73 29 20 61 6e 64 2c 20 69 66 20 45 4e 41  ils) and, if ENA
375b9 42 4c 45 5f 54 41 42 4c 45 20 69 73 0a 2a 2a 20  BLE_TABLE is.** 
375ba 64 65 66 69 6e 65 64 20 61 74 20 63 6f 6d 70 69  defined at compi
375bb 6c 61 74 69 6f 6e 20 74 69 6d 65 2c 20 61 20 74  lation time, a t
375bc 65 6d 70 6f 72 61 72 79 20 76 69 72 74 75 61 6c  emporary virtual
375bd 20 74 61 62 6c 65 20 28 73 65 65 20 68 65 61 64   table (see head
375be 65 72 20 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 20 61  er .** comment a
375bf 62 6f 76 65 20 73 74 72 75 63 74 20 48 61 73 68  bove struct Hash
375c0 54 61 62 6c 65 56 74 61 62 29 20 74 6f 20 74 68  TableVtab) to th
375c1 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
375c2 61 2e 20 42 6f 74 68 20 0a 2a 2a 20 70 72 6f 76  a. Both .** prov
375c3 69 64 65 20 72 65 61 64 2f 77 72 69 74 65 20 61  ide read/write a
375c4 63 63 65 73 73 20 74 6f 20 74 68 65 20 63 6f 6e  ccess to the con
375c5 74 65 6e 74 73 20 6f 66 20 2a 70 48 61 73 68 2e  tents of *pHash.
375c6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  .**.** The third
375c7 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69   argument to thi
375c8 73 20 66 75 6e 63 74 69 6f 6e 2c 20 7a 4e 61 6d  s function, zNam
375c9 65 2c 20 69 73 20 75 73 65 64 20 61 73 20 74 68  e, is used as th
375ca 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 62 6f 74  e name.** of bot
375cb 68 20 74 68 65 20 73 63 61 6c 61 72 20 61 6e 64  h the scalar and
375cc 2c 20 69 66 20 63 72 65 61 74 65 64 2c 20 74 68  , if created, th
375cd 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  e virtual table.
375ce 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
375cf 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  TE int sqlite3Ft
375d0 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28  s3InitHashTable(
375d1 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
375d2 0a 20 20 46 74 73 33 48 61 73 68 20 2a 70 48 61  .  Fts3Hash *pHa
375d3 73 68 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  sh, .  const cha
375d4 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 69 6e  r *zName.){.  in
375d5 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
375d6 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 28 76  ;.  void *p = (v
375d7 6f 69 64 20 2a 29 70 48 61 73 68 3b 0a 20 20 63  oid *)pHash;.  c
375d8 6f 6e 73 74 20 69 6e 74 20 61 6e 79 20 3d 20 53  onst int any = S
375d9 51 4c 49 54 45 5f 41 4e 59 3b 0a 20 20 63 68 61  QLITE_ANY;.  cha
375da 72 20 2a 7a 54 65 73 74 20 3d 20 30 3b 0a 20 20  r *zTest = 0;.  
375db 63 68 61 72 20 2a 7a 54 65 73 74 32 20 3d 20 30  char *zTest2 = 0
375dc 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
375dd 5f 54 45 53 54 0a 20 20 76 6f 69 64 20 2a 70 64  _TEST.  void *pd
375de 62 20 3d 20 28 76 6f 69 64 20 2a 29 64 62 3b 0a  b = (void *)db;.
375df 20 20 7a 54 65 73 74 20 3d 20 73 71 6c 69 74 65    zTest = sqlite
375e0 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f 74 65  3_mprintf("%s_te
375e1 73 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 7a  st", zName);.  z
375e2 54 65 73 74 32 20 3d 20 73 71 6c 69 74 65 33 5f  Test2 = sqlite3_
375e3 6d 70 72 69 6e 74 66 28 22 25 73 5f 69 6e 74 65  mprintf("%s_inte
375e4 72 6e 61 6c 5f 74 65 73 74 22 2c 20 7a 4e 61 6d  rnal_test", zNam
375e5 65 29 3b 0a 20 20 69 66 28 20 21 7a 54 65 73 74  e);.  if( !zTest
375e6 20 7c 7c 20 21 7a 54 65 73 74 32 20 29 7b 0a 20   || !zTest2 ){. 
375e7 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
375e8 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  OMEM;.  }.#endif
375e9 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
375ea 4b 21 3d 72 63 0a 20 20 20 7c 7c 20 53 51 4c 49  K!=rc.   || SQLI
375eb 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
375ec 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
375ed 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  tion(db, zName, 
375ee 31 2c 20 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61  1, any, p, scala
375ef 72 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20  rFunc, 0, 0)).  
375f0 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
375f1 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
375f2 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  ate_function(db,
375f3 20 7a 4e 61 6d 65 2c 20 32 2c 20 61 6e 79 2c 20   zName, 2, any, 
375f4 70 2c 20 73 63 61 6c 61 72 46 75 6e 63 2c 20 30  p, scalarFunc, 0
375f5 2c 20 30 29 29 0a 23 69 66 64 65 66 20 53 51 4c  , 0)).#ifdef SQL
375f6 49 54 45 5f 54 45 53 54 0a 20 20 20 7c 7c 20 53  ITE_TEST.   || S
375f7 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
375f8 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
375f9 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73  unction(db, zTes
375fa 74 2c 20 32 2c 20 61 6e 79 2c 20 70 2c 20 74 65  t, 2, any, p, te
375fb 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 20  stFunc, 0, 0)). 
375fc 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    || SQLITE_OK!=
375fd 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72  (rc = sqlite3_cr
375fe 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62  eate_function(db
375ff 2c 20 7a 54 65 73 74 2c 20 33 2c 20 61 6e 79 2c  , zTest, 3, any,
37600 20 70 2c 20 74 65 73 74 46 75 6e 63 2c 20 30 2c   p, testFunc, 0,
37601 20 30 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54   0)).   || SQLIT
37602 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
37603 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
37604 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 32 2c 20  ion(db, zTest2, 
37605 30 2c 20 61 6e 79 2c 20 70 64 62 2c 20 69 6e 74  0, any, pdb, int
37606 54 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29 29  TestFunc, 0, 0))
37607 0a 23 65 6e 64 69 66 0a 20 20 29 3b 0a 0a 20 20  .#endif.  );..  
37608 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 65  sqlite3_free(zTe
37609 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  st);.  sqlite3_f
3760a 72 65 65 28 7a 54 65 73 74 32 29 3b 0a 20 20 72  ree(zTest2);.  r
3760b 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e  eturn rc;.}..#en
3760c 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
3760d 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
3760e 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
3760f 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
37610 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
37611 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65  End of fts3_toke
37612 6e 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  nizer.c ********
37613 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37614 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
37615 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
37616 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
37617 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a  tokenizer1.c ***
37618 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37619 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
3761a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20 31  /*.** 2006 Oct 1
3761b 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  0.**.** The auth
3761c 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
3761d 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
3761e 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3761f 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
37620 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
37621 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
37622 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
37623 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
37624 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
37625 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
37626 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
37627 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
37628 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
37629 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
3762a 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
3762b 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
3762c 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
3762d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3762e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3762f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37631 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c  *****.**.** Impl
37632 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
37633 65 20 22 73 69 6d 70 6c 65 22 20 66 75 6c 6c 2d  e "simple" full-
37634 74 65 78 74 2d 73 65 61 72 63 68 20 74 6f 6b 65  text-search toke
37635 6e 69 7a 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  nizer..*/../*.**
37636 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
37637 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63  s file is only c
37638 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a  ompiled if:.**.*
37639 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
3763a 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
3763b 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74   built as an ext
3763c 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ension.**       
3763d 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
3763e 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f  QLITE_CORE is no
3763f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a  t defined), or.*
37640 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
37641 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
37642 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  ing built into t
37643 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20  he core of.**   
37644 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77      SQLite (in w
37645 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
37646 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20  _ENABLE_FTS3 is 
37647 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66  defined)..*/.#if
37648 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
37649 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65  _CORE) || define
3764a 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
3764b 46 54 53 33 29 0a 0a 0a 0a 0a 74 79 70 65 64 65  FTS3).....typede
3764c 66 20 73 74 72 75 63 74 20 73 69 6d 70 6c 65 5f  f struct simple_
3764d 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71  tokenizer {.  sq
3764e 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
3764f 62 61 73 65 3b 0a 20 20 63 68 61 72 20 64 65 6c  base;.  char del
37650 69 6d 5b 31 32 38 5d 3b 20 20 20 20 20 20 20 20  im[128];        
37651 20 20 20 20 20 2f 2a 20 66 6c 61 67 20 41 53 43       /* flag ASC
37652 49 49 20 64 65 6c 69 6d 69 74 65 72 73 20 2a 2f  II delimiters */
37653 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .} simple_tokeni
37654 7a 65 72 3b 0a 0a 74 79 70 65 64 65 66 20 73 74  zer;..typedef st
37655 72 75 63 74 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  ruct simple_toke
37656 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a 20  nizer_cursor {. 
37657 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
37658 65 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 0a  er_cursor base;.
37659 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 49    const char *pI
3765a 6e 70 75 74 3b 20 20 20 20 20 20 20 20 20 20 2f  nput;          /
3765b 2a 20 69 6e 70 75 74 20 77 65 20 61 72 65 20 74  * input we are t
3765c 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20 69  okenizing */.  i
3765d 6e 74 20 6e 42 79 74 65 73 3b 20 20 20 20 20 20  nt nBytes;      
3765e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
3765f 69 7a 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74  ize of the input
37660 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65   */.  int iOffse
37661 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
37662 20 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 70 6f     /* current po
37663 73 69 74 69 6f 6e 20 69 6e 20 70 49 6e 70 75 74  sition in pInput
37664 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e   */.  int iToken
37665 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
37666 20 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20 6e     /* index of n
37667 65 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65 20  ext token to be 
37668 72 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63 68  returned */.  ch
37669 61 72 20 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20  ar *pToken;     
3766a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74             /* st
3766b 6f 72 61 67 65 20 66 6f 72 20 63 75 72 72 65 6e  orage for curren
3766c 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  t token */.  int
3766d 20 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64   nTokenAllocated
3766e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 73 70 61  ;         /* spa
3766f 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20  ce allocated to 
37670 7a 54 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a 2f  zToken buffer */
37671 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .} simple_tokeni
37672 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 73 74  zer_cursor;...st
37673 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 44  atic int simpleD
37674 65 6c 69 6d 28 73 69 6d 70 6c 65 5f 74 6f 6b 65  elim(simple_toke
37675 6e 69 7a 65 72 20 2a 74 2c 20 75 6e 73 69 67 6e  nizer *t, unsign
37676 65 64 20 63 68 61 72 20 63 29 7b 0a 20 20 72 65  ed char c){.  re
37677 74 75 72 6e 20 63 3c 30 78 38 30 20 26 26 20 74  turn c<0x80 && t
37678 2d 3e 64 65 6c 69 6d 5b 63 5d 3b 0a 7d 0a 0a 2f  ->delim[c];.}../
37679 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
3767a 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74  w tokenizer inst
3767b 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ance..*/.static 
3767c 69 6e 74 20 73 69 6d 70 6c 65 43 72 65 61 74 65  int simpleCreate
3767d 28 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f  (.  int argc, co
3767e 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  nst char * const
3767f 20 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65   *argv,.  sqlite
37680 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70  3_tokenizer **pp
37681 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 73  Tokenizer.){.  s
37682 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
37683 2a 74 3b 0a 0a 20 20 74 20 3d 20 28 73 69 6d 70  *t;..  t = (simp
37684 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20  le_tokenizer *) 
37685 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
37686 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66  izeof(*t));.  if
37687 28 20 74 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75  ( t==NULL ) retu
37688 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
37689 0a 20 20 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20  .  memset(t, 0, 
3768a 73 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 0a 20 20  sizeof(*t));..  
3768b 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 44  /* TODO(shess) D
3768c 65 6c 69 6d 69 74 65 72 73 20 6e 65 65 64 20 74  elimiters need t
3768d 6f 20 72 65 6d 61 69 6e 20 74 68 65 20 73 61 6d  o remain the sam
3768e 65 20 66 72 6f 6d 20 72 75 6e 20 74 6f 20 72 75  e from run to ru
3768f 6e 2c 0a 20 20 2a 2a 20 65 6c 73 65 20 77 65 20  n,.  ** else we 
37690 6e 65 65 64 20 74 6f 20 72 65 69 6e 64 65 78 2e  need to reindex.
37691 20 20 4f 6e 65 20 73 6f 6c 75 74 69 6f 6e 20 77    One solution w
37692 6f 75 6c 64 20 62 65 20 61 20 6d 65 74 61 2d 74  ould be a meta-t
37693 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 74 72 61  able to.  ** tra
37694 63 6b 20 73 75 63 68 20 69 6e 66 6f 72 6d 61 74  ck such informat
37695 69 6f 6e 20 69 6e 20 74 68 65 20 64 61 74 61 62  ion in the datab
37696 61 73 65 2c 20 74 68 65 6e 20 77 65 27 64 20 6f  ase, then we'd o
37697 6e 6c 79 20 77 61 6e 74 20 74 68 69 73 0a 20 20  nly want this.  
37698 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f  ** information o
37699 6e 20 74 68 65 20 69 6e 69 74 69 61 6c 20 63 72  n the initial cr
3769a 65 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  eate..  */.  if(
3769b 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20 20 69   argc>1 ){.    i
3769c 6e 74 20 69 2c 20 6e 20 3d 20 28 69 6e 74 29 73  nt i, n = (int)s
3769d 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0a  trlen(argv[1]);.
3769e 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
3769f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e  ; i++){.      un
376a0 73 69 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d  signed char ch =
376a1 20 61 72 67 76 5b 31 5d 5b 69 5d 3b 0a 20 20 20   argv[1][i];.   
376a2 20 20 20 2f 2a 20 57 65 20 65 78 70 6c 69 63 69     /* We explici
376a3 74 6c 79 20 64 6f 6e 27 74 20 73 75 70 70 6f 72  tly don't suppor
376a4 74 20 55 54 46 2d 38 20 64 65 6c 69 6d 69 74 65  t UTF-8 delimite
376a5 72 73 20 66 6f 72 20 6e 6f 77 2e 20 2a 2f 0a 20  rs for now. */. 
376a6 20 20 20 20 20 69 66 28 20 63 68 3e 3d 30 78 38       if( ch>=0x8
376a7 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
376a8 69 74 65 33 5f 66 72 65 65 28 74 29 3b 0a 20 20  ite3_free(t);.  
376a9 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
376aa 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
376ab 20 7d 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69   }.      t->deli
376ac 6d 5b 63 68 5d 20 3d 20 31 3b 0a 20 20 20 20 7d  m[ch] = 1;.    }
376ad 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
376ae 2f 2a 20 4d 61 72 6b 20 6e 6f 6e 2d 61 6c 70 68  /* Mark non-alph
376af 61 6e 75 6d 65 72 69 63 20 41 53 43 49 49 20 63  anumeric ASCII c
376b0 68 61 72 61 63 74 65 72 73 20 61 73 20 64 65 6c  haracters as del
376b1 69 6d 69 74 65 72 73 20 2a 2f 0a 20 20 20 20 69  imiters */.    i
376b2 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
376b3 31 3b 20 69 3c 30 78 38 30 3b 20 69 2b 2b 29 7b  1; i<0x80; i++){
376b4 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b  .      t->delim[
376b5 69 5d 20 3d 20 21 69 73 61 6c 6e 75 6d 28 69 29  i] = !isalnum(i)
376b6 20 3f 20 2d 31 20 3a 20 30 3b 0a 20 20 20 20 7d   ? -1 : 0;.    }
376b7 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e  .  }..  *ppToken
376b8 69 7a 65 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b  izer = &t->base;
376b9 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
376ba 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
376bb 73 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65  stroy a tokenize
376bc 72 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  r.*/.static int 
376bd 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 28 73 71  simpleDestroy(sq
376be 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
376bf 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20  *pTokenizer){.  
376c0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f  sqlite3_free(pTo
376c1 6b 65 6e 69 7a 65 72 29 3b 0a 20 20 72 65 74 75  kenizer);.  retu
376c2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
376c3 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74  ./*.** Prepare t
376c4 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69  o begin tokenizi
376c5 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ng a particular 
376c6 73 74 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70  string.  The inp
376c7 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20  ut.** string to 
376c8 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20  be tokenized is 
376c9 70 49 6e 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73  pInput[0..nBytes
376ca 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a  -1].  A cursor.*
376cb 2a 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d  * used to increm
376cc 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65  entally tokenize
376cd 20 74 68 69 73 20 73 74 72 69 6e 67 20 69 73 20   this string is 
376ce 72 65 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20  returned in .** 
376cf 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74  *ppCursor..*/.st
376d0 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 4f  atic int simpleO
376d1 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  pen(.  sqlite3_t
376d2 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
376d3 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  izer,         /*
376d4 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   The tokenizer *
376d5 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
376d6 70 49 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74  pInput, int nByt
376d7 65 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74  es,        /* St
376d8 72 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e  ring to be token
376d9 69 7a 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65  ized */.  sqlite
376da 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
376db 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20  or **ppCursor   
376dc 20 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a   /* OUT: Tokeniz
376dd 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a  ation cursor */.
376de 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  ){.  simple_toke
376df 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b  nizer_cursor *c;
376e0 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ..  UNUSED_PARAM
376e1 45 54 45 52 28 70 54 6f 6b 65 6e 69 7a 65 72 29  ETER(pTokenizer)
376e2 3b 0a 0a 20 20 63 20 3d 20 28 73 69 6d 70 6c 65  ;..  c = (simple
376e3 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
376e4 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c  r *) sqlite3_mal
376e5 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 63 29 29 3b  loc(sizeof(*c));
376e6 0a 20 20 69 66 28 20 63 3d 3d 4e 55 4c 4c 20 29  .  if( c==NULL )
376e7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
376e8 4f 4d 45 4d 3b 0a 0a 20 20 63 2d 3e 70 49 6e 70  OMEM;..  c->pInp
376e9 75 74 20 3d 20 70 49 6e 70 75 74 3b 0a 20 20 69  ut = pInput;.  i
376ea 66 28 20 70 49 6e 70 75 74 3d 3d 30 20 29 7b 0a  f( pInput==0 ){.
376eb 20 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20      c->nBytes = 
376ec 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e  0;.  }else if( n
376ed 42 79 74 65 73 3c 30 20 29 7b 0a 20 20 20 20 63  Bytes<0 ){.    c
376ee 2d 3e 6e 42 79 74 65 73 20 3d 20 28 69 6e 74 29  ->nBytes = (int)
376ef 73 74 72 6c 65 6e 28 70 49 6e 70 75 74 29 3b 0a  strlen(pInput);.
376f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 2d 3e    }else{.    c->
376f1 6e 42 79 74 65 73 20 3d 20 6e 42 79 74 65 73 3b  nBytes = nBytes;
376f2 0a 20 20 7d 0a 20 20 63 2d 3e 69 4f 66 66 73 65  .  }.  c->iOffse
376f3 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
376f4 20 20 20 20 20 20 20 2f 2a 20 73 74 61 72 74 20         /* start 
376f5 74 6f 6b 65 6e 69 7a 69 6e 67 20 61 74 20 74 68  tokenizing at th
376f6 65 20 62 65 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20  e beginning */. 
376f7 20 63 2d 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a   c->iToken = 0;.
376f8 20 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20 4e 55    c->pToken = NU
376f9 4c 4c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  LL;             
376fa 20 20 2f 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c    /* no space al
376fb 6c 6f 63 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f  located, yet. */
376fc 0a 20 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f  .  c->nTokenAllo
376fd 63 61 74 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70  cated = 0;..  *p
376fe 70 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61  pCursor = &c->ba
376ff 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  se;.  return SQL
37700 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
37701 20 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a   Close a tokeniz
37702 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65  ation cursor pre
37703 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62  viously opened b
37704 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73  y a call to.** s
37705 69 6d 70 6c 65 4f 70 65 6e 28 29 20 61 62 6f 76  impleOpen() abov
37706 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
37707 20 73 69 6d 70 6c 65 43 6c 6f 73 65 28 73 71 6c   simpleClose(sql
37708 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
37709 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
3770a 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .  simple_tokeni
3770b 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  zer_cursor *c = 
3770c 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65  (simple_tokenize
3770d 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  r_cursor *) pCur
3770e 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  sor;.  sqlite3_f
3770f 72 65 65 28 63 2d 3e 70 54 6f 6b 65 6e 29 3b 0a  ree(c->pToken);.
37710 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63    sqlite3_free(c
37711 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
37712 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
37713 45 78 74 72 61 63 74 20 74 68 65 20 6e 65 78 74  Extract the next
37714 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f   token from a to
37715 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f  kenization curso
37716 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d  r.  The cursor m
37717 75 73 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e  ust.** have been
37718 20 6f 70 65 6e 65 64 20 62 79 20 61 20 70 72 69   opened by a pri
37719 6f 72 20 63 61 6c 6c 20 74 6f 20 73 69 6d 70 6c  or call to simpl
3771a 65 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74  eOpen()..*/.stat
3771b 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 4e 65 78  ic int simpleNex
3771c 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  t(.  sqlite3_tok
3771d 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
3771e 43 75 72 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73  Cursor,  /* Curs
3771f 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  or returned by s
37720 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f 0a 20 20 63  impleOpen */.  c
37721 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 70 54 6f  onst char **ppTo
37722 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  ken,            
37723 20 20 20 2f 2a 20 4f 55 54 3a 20 2a 70 70 54 6f     /* OUT: *ppTo
37724 6b 65 6e 20 69 73 20 74 68 65 20 74 6f 6b 65 6e  ken is the token
37725 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a   text */.  int *
37726 70 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20  pnBytes,        
37727 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37728 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
37729 20 62 79 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20   bytes in token 
3772a 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53 74 61 72  */.  int *piStar
3772b 74 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20 20  tOffset,        
3772c 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
3772d 20 53 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74   Starting offset
3772e 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69   of token */.  i
3772f 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c  nt *piEndOffset,
37730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37731 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e     /* OUT: Endin
37732 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65  g offset of toke
37733 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f  n */.  int *piPo
37734 73 69 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20  sition          
37735 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
37736 54 3a 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65  T: Position inte
37737 67 65 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a  ger of token */.
37738 29 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65  ){.  simple_toke
37739 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20  nizer_cursor *c 
3773a 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  = (simple_tokeni
3773b 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43  zer_cursor *) pC
3773c 75 72 73 6f 72 3b 0a 20 20 73 69 6d 70 6c 65 5f  ursor;.  simple_
3773d 74 6f 6b 65 6e 69 7a 65 72 20 2a 74 20 3d 20 28  tokenizer *t = (
3773e 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
3773f 20 2a 29 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f   *) pCursor->pTo
37740 6b 65 6e 69 7a 65 72 3b 0a 20 20 75 6e 73 69 67  kenizer;.  unsig
37741 6e 65 64 20 63 68 61 72 20 2a 70 20 3d 20 28 75  ned char *p = (u
37742 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 63  nsigned char *)c
37743 2d 3e 70 49 6e 70 75 74 3b 0a 0a 20 20 77 68 69  ->pInput;..  whi
37744 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63  le( c->iOffset<c
37745 2d 3e 6e 42 79 74 65 73 20 29 7b 0a 20 20 20 20  ->nBytes ){.    
37746 69 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74  int iStartOffset
37747 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61 6e 20 70  ;..    /* Scan p
37748 61 73 74 20 64 65 6c 69 6d 69 74 65 72 20 63 68  ast delimiter ch
37749 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 20 20  aracters */.    
3774a 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65  while( c->iOffse
3774b 74 3c 63 2d 3e 6e 42 79 74 65 73 20 26 26 20 73  t<c->nBytes && s
3774c 69 6d 70 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b  impleDelim(t, p[
3774d 63 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a  c->iOffset]) ){.
3774e 20 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74        c->iOffset
3774f 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ++;.    }..    /
37750 2a 20 43 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69  * Count non-deli
37751 6d 69 74 65 72 20 63 68 61 72 61 63 74 65 72 73  miter characters
37752 2e 20 2a 2f 0a 20 20 20 20 69 53 74 61 72 74 4f  . */.    iStartO
37753 66 66 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73  ffset = c->iOffs
37754 65 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63  et;.    while( c
37755 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79  ->iOffset<c->nBy
37756 74 65 73 20 26 26 20 21 73 69 6d 70 6c 65 44 65  tes && !simpleDe
37757 6c 69 6d 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66  lim(t, p[c->iOff
37758 73 65 74 5d 29 20 29 7b 0a 20 20 20 20 20 20 63  set]) ){.      c
37759 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20  ->iOffset++;.   
3775a 20 7d 0a 0a 20 20 20 20 69 66 28 20 63 2d 3e 69   }..    if( c->i
3775b 4f 66 66 73 65 74 3e 69 53 74 61 72 74 4f 66 66  Offset>iStartOff
3775c 73 65 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  set ){.      int
3775d 20 69 2c 20 6e 20 3d 20 63 2d 3e 69 4f 66 66 73   i, n = c->iOffs
3775e 65 74 2d 69 53 74 61 72 74 4f 66 66 73 65 74 3b  et-iStartOffset;
3775f 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 63 2d 3e  .      if( n>c->
37760 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20  nTokenAllocated 
37761 29 7b 0a 20 20 20 20 20 20 20 20 63 2d 3e 6e 54  ){.        c->nT
37762 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20  okenAllocated = 
37763 6e 2b 32 30 3b 0a 20 20 20 20 20 20 20 20 63 2d  n+20;.        c-
37764 3e 70 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65  >pToken = sqlite
37765 33 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e 70 54 6f  3_realloc(c->pTo
37766 6b 65 6e 2c 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c  ken, c->nTokenAl
37767 6c 6f 63 61 74 65 64 29 3b 0a 20 20 20 20 20 20  located);.      
37768 20 20 69 66 28 20 63 2d 3e 70 54 6f 6b 65 6e 3d    if( c->pToken=
37769 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53  =NULL ) return S
3776a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
3776b 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 69     }.      for(i
3776c 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20  =0; i<n; i++){. 
3776d 20 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73         /* TODO(s
3776e 68 65 73 73 29 20 54 68 69 73 20 6e 65 65 64 73  hess) This needs
3776f 20 65 78 70 61 6e 73 69 6f 6e 20 74 6f 20 68 61   expansion to ha
37770 6e 64 6c 65 20 55 54 46 2d 38 0a 20 20 20 20 20  ndle UTF-8.     
37771 20 20 20 2a 2a 20 63 61 73 65 2d 69 6e 73 65 6e     ** case-insen
37772 73 69 74 69 76 69 74 79 2e 0a 20 20 20 20 20 20  sitivity..      
37773 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 73    */.        uns
37774 69 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d 20  igned char ch = 
37775 70 5b 69 53 74 61 72 74 4f 66 66 73 65 74 2b 69  p[iStartOffset+i
37776 5d 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e 70 54  ];.        c->pT
37777 6f 6b 65 6e 5b 69 5d 20 3d 20 28 63 68 61 72 29  oken[i] = (char)
37778 28 63 68 3c 30 78 38 30 20 3f 20 74 6f 6c 6f 77  (ch<0x80 ? tolow
37779 65 72 28 63 68 29 20 3a 20 63 68 29 3b 0a 20 20  er(ch) : ch);.  
3777a 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 70 54      }.      *ppT
3777b 6f 6b 65 6e 20 3d 20 63 2d 3e 70 54 6f 6b 65 6e  oken = c->pToken
3777c 3b 0a 20 20 20 20 20 20 2a 70 6e 42 79 74 65 73  ;.      *pnBytes
3777d 20 3d 20 6e 3b 0a 20 20 20 20 20 20 2a 70 69 53   = n;.      *piS
3777e 74 61 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74  tartOffset = iSt
3777f 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20  artOffset;.     
37780 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20   *piEndOffset = 
37781 63 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20  c->iOffset;.    
37782 20 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20    *piPosition = 
37783 63 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20  c->iToken++;..  
37784 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
37785 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
37786 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
37787 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  DONE;.}../*.** T
37788 68 65 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e  he set of routin
37789 65 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  es that implemen
3778a 74 20 74 68 65 20 73 69 6d 70 6c 65 20 74 6f 6b  t the simple tok
3778b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63  enizer.*/.static
3778c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
3778d 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
3778e 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d  simpleTokenizerM
3778f 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 0a 20  odule = {.  0,. 
37790 20 73 69 6d 70 6c 65 43 72 65 61 74 65 2c 0a 20   simpleCreate,. 
37791 20 73 69 6d 70 6c 65 44 65 73 74 72 6f 79 2c 0a   simpleDestroy,.
37792 20 20 73 69 6d 70 6c 65 4f 70 65 6e 2c 0a 20 20    simpleOpen,.  
37793 73 69 6d 70 6c 65 43 6c 6f 73 65 2c 0a 20 20 73  simpleClose,.  s
37794 69 6d 70 6c 65 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f  impleNext,.};../
37795 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
37796 6e 65 77 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e  new simple token
37797 69 7a 65 72 2e 20 20 52 65 74 75 72 6e 20 61 20  izer.  Return a 
37798 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e  pointer to the n
37799 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 20  ew.** tokenizer 
3779a 69 6e 20 2a 70 70 4d 6f 64 75 6c 65 0a 2a 2f 0a  in *ppModule.*/.
3779b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
3779c 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
3779d 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
3779e 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  dule(.  sqlite3_
3779f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
377a0 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65   const**ppModule
377a1 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c 65 20  .){.  *ppModule 
377a2 3d 20 26 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a  = &simpleTokeniz
377a3 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e  erModule;.}..#en
377a4 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
377a5 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
377a6 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
377a7 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
377a8 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
377a9 45 6e 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65  End of fts3_toke
377aa 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a  nizer1.c *******
377ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
377ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
377ad 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
377ae 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
377af 77 72 69 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  write.c ********
377b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
377b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
377b2 2f 2a 0a 2a 2a 20 32 30 30 39 20 4f 63 74 20 32  /*.** 2009 Oct 2
377b3 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  3.**.** The auth
377b4 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
377b5 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
377b6 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
377b7 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
377b8 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
377b9 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
377ba 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
377bb 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
377bc 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
377bd 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
377be 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
377bf 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
377c0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
377c1 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
377c2 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
377c3 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
377c4 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
377c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
377c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
377c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
377c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
377c9 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
377ca 20 66 69 6c 65 20 69 73 20 70 61 72 74 20 6f 66   file is part of
377cb 20 74 68 65 20 53 51 4c 69 74 65 20 46 54 53 33   the SQLite FTS3
377cc 20 65 78 74 65 6e 73 69 6f 6e 20 6d 6f 64 75 6c   extension modul
377cd 65 2e 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c  e. Specifically,
377ce 0a 2a 2a 20 74 68 69 73 20 66 69 6c 65 20 63 6f  .** this file co
377cf 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 69  ntains code to i
377d0 6e 73 65 72 74 2c 20 75 70 64 61 74 65 20 61 6e  nsert, update an
377d1 64 20 64 65 6c 65 74 65 20 72 6f 77 73 20 66 72  d delete rows fr
377d2 6f 6d 20 46 54 53 33 0a 2a 2a 20 74 61 62 6c 65  om FTS3.** table
377d3 73 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61  s. It also conta
377d4 69 6e 73 20 63 6f 64 65 20 74 6f 20 6d 65 72 67  ins code to merg
377d5 65 20 46 54 53 33 20 62 2d 74 72 65 65 20 73 65  e FTS3 b-tree se
377d6 67 6d 65 6e 74 73 2e 20 53 6f 6d 65 0a 2a 2a 20  gments. Some.** 
377d7 6f 66 20 74 68 65 20 73 75 62 2d 72 6f 75 74 69  of the sub-routi
377d8 6e 65 73 20 75 73 65 64 20 74 6f 20 6d 65 72 67  nes used to merg
377d9 65 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20 61  e segments are a
377da 6c 73 6f 20 75 73 65 64 20 62 79 20 74 68 65 20  lso used by the 
377db 71 75 65 72 79 20 0a 2a 2a 20 63 6f 64 65 20 69  query .** code i
377dc 6e 20 66 74 73 33 2e 63 2e 0a 2a 2f 0a 0a 23 69  n fts3.c..*/..#i
377dd 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
377de 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
377df 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
377e0 5f 46 54 53 33 29 0a 0a 0a 74 79 70 65 64 65 66  _FTS3)...typedef
377e1 20 73 74 72 75 63 74 20 50 65 6e 64 69 6e 67 4c   struct PendingL
377e2 69 73 74 20 50 65 6e 64 69 6e 67 4c 69 73 74 3b  ist PendingList;
377e3 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
377e4 53 65 67 6d 65 6e 74 4e 6f 64 65 20 53 65 67 6d  SegmentNode Segm
377e5 65 6e 74 4e 6f 64 65 3b 0a 74 79 70 65 64 65 66  entNode;.typedef
377e6 20 73 74 72 75 63 74 20 53 65 67 6d 65 6e 74 57   struct SegmentW
377e7 72 69 74 65 72 20 53 65 67 6d 65 6e 74 57 72 69  riter SegmentWri
377e8 74 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20 44 61 74 61  ter;../*.** Data
377e9 20 73 74 72 75 63 74 75 72 65 20 75 73 65 64 20   structure used 
377ea 77 68 69 6c 65 20 61 63 63 75 6d 75 6c 61 74 69  while accumulati
377eb 6e 67 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20  ng terms in the 
377ec 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 68 61  pending-terms ha
377ed 73 68 0a 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65  sh.** table. The
377ee 20 68 61 73 68 20 74 61 62 6c 65 20 65 6e 74 72   hash table entr
377ef 79 20 6d 61 70 73 20 66 72 6f 6d 20 74 65 72 6d  y maps from term
377f0 20 28 61 20 73 74 72 69 6e 67 29 20 74 6f 20 61   (a string) to a
377f1 20 6d 61 6c 6c 6f 63 65 64 0a 2a 2a 20 69 6e 73   malloced.** ins
377f2 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
377f3 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
377f4 63 74 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 7b  ct PendingList {
377f5 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 20 20  .  int nData;.  
377f6 63 68 61 72 20 2a 61 44 61 74 61 3b 0a 20 20 69  char *aData;.  i
377f7 6e 74 20 6e 53 70 61 63 65 3b 0a 20 20 73 71 6c  nt nSpace;.  sql
377f8 69 74 65 33 5f 69 6e 74 36 34 20 69 4c 61 73 74  ite3_int64 iLast
377f9 44 6f 63 69 64 3b 0a 20 20 73 71 6c 69 74 65 33  Docid;.  sqlite3
377fa 5f 69 6e 74 36 34 20 69 4c 61 73 74 43 6f 6c 3b  _int64 iLastCol;
377fb 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  .  sqlite3_int64
377fc 20 69 4c 61 73 74 50 6f 73 3b 0a 7d 3b 0a 0a 2f   iLastPos;.};../
377fd 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
377fe 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
377ff 72 65 20 69 73 20 75 73 65 64 20 74 6f 20 69 74  re is used to it
37800 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 74 68  erate through th
37801 65 20 74 65 72 6d 73 20 6f 6e 0a 2a 2a 20 61 20  e terms on.** a 
37802 63 6f 6e 74 69 67 75 6f 75 73 20 73 65 74 20 6f  contiguous set o
37803 66 20 73 65 67 6d 65 6e 74 20 62 2d 74 72 65 65  f segment b-tree
37804 20 6c 65 61 66 20 6e 6f 64 65 73 2e 20 41 6c 74   leaf nodes. Alt
37805 68 6f 75 67 68 20 74 68 65 20 64 65 74 61 69 6c  hough the detail
37806 73 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74 72  s of.** this str
37807 75 63 74 75 72 65 20 61 72 65 20 6f 6e 6c 79 20  ucture are only 
37808 6d 61 6e 69 70 75 6c 61 74 65 64 20 62 79 20 63  manipulated by c
37809 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
3780a 2c 20 6f 70 61 71 75 65 20 68 61 6e 64 6c 65 73  , opaque handles
3780b 0a 2a 2a 20 6f 66 20 74 79 70 65 20 46 74 73 33  .** of type Fts3
3780c 53 65 67 52 65 61 64 65 72 2a 20 61 72 65 20 61  SegReader* are a
3780d 6c 73 6f 20 75 73 65 64 20 62 79 20 63 6f 64 65  lso used by code
3780e 20 69 6e 20 66 74 73 33 2e 63 20 74 6f 20 69 74   in fts3.c to it
3780f 65 72 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a  erate through.**
37810 20 74 65 72 6d 73 20 77 68 65 6e 20 71 75 65 72   terms when quer
37811 79 69 6e 67 20 74 68 65 20 66 75 6c 6c 2d 74 65  ying the full-te
37812 78 74 20 69 6e 64 65 78 2e 20 53 65 65 20 66 75  xt index. See fu
37813 6e 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20  nctions:.**.**  
37814 20 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52   sqlite3Fts3SegR
37815 65 61 64 65 72 4e 65 77 28 29 0a 2a 2a 20 20 20  eaderNew().**   
37816 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65  sqlite3Fts3SegRe
37817 61 64 65 72 46 72 65 65 28 29 0a 2a 2a 20 20 20  aderFree().**   
37818 73 71 6c 69 74 65 33 46 74 73 33 53 65 67 52 65  sqlite3Fts3SegRe
37819 61 64 65 72 49 74 65 72 61 74 65 28 29 0a 2a 2f  aderIterate().*/
3781a 0a 73 74 72 75 63 74 20 46 74 73 33 53 65 67 52  .struct Fts3SegR
3781b 65 61 64 65 72 20 7b 0a 20 20 69 6e 74 20 69 49  eader {.  int iI
3781c 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  dx;             
3781d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
3781e 65 78 20 77 69 74 68 69 6e 20 6c 65 76 65 6c 20  ex within level 
3781f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
37820 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 3b 0a  64 iStartBlock;.
37821 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
37822 69 45 6e 64 42 6c 6f 63 6b 3b 0a 20 20 73 71 6c  iEndBlock;.  sql
37823 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
37824 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
37825 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 74 6f  SQL Statement to
37826 20 61 63 63 65 73 73 20 6c 65 61 66 20 6e 6f 64   access leaf nod
37827 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 4e  es */.  char *aN
37828 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ode;            
37829 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
3782a 65 72 20 74 6f 20 6e 6f 64 65 20 64 61 74 61 20  er to node data 
3782b 28 6f 72 20 4e 55 4c 4c 29 20 2a 2f 0a 20 20 69  (or NULL) */.  i
3782c 6e 74 20 6e 4e 6f 64 65 3b 20 20 20 20 20 20 20  nt nNode;       
3782d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3782e 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72  * Size of buffer
3782f 20 61 74 20 61 4e 6f 64 65 20 28 6f 72 20 30 29   at aNode (or 0)
37830 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 41   */.  int nTermA
37831 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20  lloc;           
37832 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74        /* Allocat
37833 65 64 20 73 69 7a 65 20 6f 66 20 7a 54 65 72 6d  ed size of zTerm
37834 20 62 75 66 66 65 72 20 2a 2f 0a 0a 20 20 2f 2a   buffer */..  /*
37835 20 56 61 72 69 61 62 6c 65 73 20 73 65 74 20 62   Variables set b
37836 79 20 66 74 73 33 53 65 67 52 65 61 64 65 72 4e  y fts3SegReaderN
37837 65 78 74 28 29 2e 20 54 68 65 73 65 20 6d 61 79  ext(). These may
37838 20 62 65 20 72 65 61 64 20 64 69 72 65 63 74 6c   be read directl
37839 79 0a 20 20 2a 2a 20 62 79 20 74 68 65 20 63 61  y.  ** by the ca
3783a 6c 6c 65 72 2e 20 54 68 65 79 20 61 72 65 20 76  ller. They are v
3783b 61 6c 69 64 20 66 72 6f 6d 20 74 68 65 20 74 69  alid from the ti
3783c 6d 65 20 53 65 67 6d 65 6e 74 52 65 61 64 65 72  me SegmentReader
3783d 4e 65 77 28 29 20 72 65 74 75 72 6e 73 0a 20 20  New() returns.  
3783e 2a 2a 20 75 6e 74 69 6c 20 53 65 67 6d 65 6e 74  ** until Segment
3783f 52 65 61 64 65 72 4e 65 78 74 28 29 20 72 65 74  ReaderNext() ret
37840 75 72 6e 73 20 73 6f 6d 65 74 68 69 6e 67 20 6f  urns something o
37841 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45  ther than SQLITE
37842 5f 4f 4b 0a 20 20 2a 2a 20 28 69 2e 65 2e 20 53  _OK.  ** (i.e. S
37843 51 4c 49 54 45 5f 44 4f 4e 45 29 2e 0a 20 20 2a  QLITE_DONE)..  *
37844 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20  /.  int nTerm;  
37845 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37846 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
37847 20 62 79 74 65 73 20 69 6e 20 63 75 72 72 65 6e   bytes in curren
37848 74 20 74 65 72 6d 20 2a 2f 0a 20 20 63 68 61 72  t term */.  char
37849 20 2a 7a 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *zTerm;        
3784a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
3784b 6f 69 6e 74 65 72 20 74 6f 20 63 75 72 72 65 6e  ointer to curren
3784c 74 20 74 65 72 6d 20 2a 2f 0a 20 20 63 68 61 72  t term */.  char
3784d 20 2a 61 44 6f 63 6c 69 73 74 3b 20 20 20 20 20   *aDoclist;     
3784e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
3784f 6f 69 6e 74 65 72 20 74 6f 20 64 6f 63 6c 69 73  ointer to doclis
37850 74 20 6f 66 20 63 75 72 72 65 6e 74 20 65 6e 74  t of current ent
37851 72 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f 63  ry */.  int nDoc
37852 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  list;           
37853 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
37854 6f 66 20 64 6f 63 6c 69 73 74 20 69 6e 20 63 75  of doclist in cu
37855 72 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 0a  rrent entry */..
37856 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
37857 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  ng variables are
37858 20 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 65   used to iterate
37859 20 74 68 72 6f 75 67 68 20 74 68 65 20 63 75 72   through the cur
3785a 72 65 6e 74 20 64 6f 63 6c 69 73 74 20 2a 2f 0a  rent doclist */.
3785b 20 20 63 68 61 72 20 2a 70 4f 66 66 73 65 74 4c    char *pOffsetL
3785c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  ist;.  sqlite3_i
3785d 6e 74 36 34 20 69 44 6f 63 69 64 3b 0a 7d 3b 0a  nt64 iDocid;.};.
3785e 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
3785f 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
37860 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f 20  ture is used to 
37861 63 72 65 61 74 65 20 61 20 73 65 67 6d 65 6e 74  create a segment
37862 20 62 2d 74 72 65 65 20 69 6e 20 74 68 65 0a 2a   b-tree in the.*
37863 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20  * database. The 
37864 69 6e 74 65 72 6e 61 6c 20 64 65 74 61 69 6c 73  internal details
37865 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 61 72   of this type ar
37866 65 20 6f 6e 6c 79 20 61 63 63 65 73 73 65 64 20  e only accessed 
37867 62 79 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77  by the.** follow
37868 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 3a 0a 2a  ing functions:.*
37869 2a 0a 2a 2a 20 20 20 66 74 73 33 53 65 67 57 72  *.**   fts3SegWr
3786a 69 74 65 72 41 64 64 28 29 0a 2a 2a 20 20 20 66  iterAdd().**   f
3786b 74 73 33 53 65 67 57 72 69 74 65 72 46 6c 75 73  ts3SegWriterFlus
3786c 68 28 29 0a 2a 2a 20 20 20 66 74 73 33 53 65 67  h().**   fts3Seg
3786d 57 72 69 74 65 72 46 72 65 65 28 29 0a 2a 2f 0a  WriterFree().*/.
3786e 73 74 72 75 63 74 20 53 65 67 6d 65 6e 74 57 72  struct SegmentWr
3786f 69 74 65 72 20 7b 0a 20 20 53 65 67 6d 65 6e 74  iter {.  Segment
37870 4e 6f 64 65 20 2a 70 54 72 65 65 3b 20 20 20 20  Node *pTree;    
37871 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
37872 74 65 72 20 74 6f 20 69 6e 74 65 72 69 6f 72 20  ter to interior 
37873 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
37874 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
37875 34 20 69 46 69 72 73 74 3b 20 20 20 20 20 20 20  4 iFirst;       
37876 20 20 20 20 2f 2a 20 46 69 72 73 74 20 73 6c 6f      /* First slo
37877 74 20 69 6e 20 25 5f 73 65 67 6d 65 6e 74 73 20  t in %_segments 
37878 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 73 71 6c  written */.  sql
37879 69 74 65 33 5f 69 6e 74 36 34 20 69 46 72 65 65  ite3_int64 iFree
3787a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
3787b 4e 65 78 74 20 66 72 65 65 20 73 6c 6f 74 20 69  Next free slot i
3787c 6e 20 25 5f 73 65 67 6d 65 6e 74 73 20 2a 2f 0a  n %_segments */.
3787d 20 20 63 68 61 72 20 2a 7a 54 65 72 6d 3b 20 20    char *zTerm;  
3787e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3787f 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
37880 70 72 65 76 69 6f 75 73 20 74 65 72 6d 20 62 75  previous term bu
37881 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  ffer */.  int nT
37882 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  erm;            
37883 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
37884 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
37885 7a 54 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e  zTerm */.  int n
37886 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  Malloc;         
37887 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
37888 7a 65 20 6f 66 20 6d 61 6c 6c 6f 63 27 64 20 62  ze of malloc'd b
37889 75 66 66 65 72 20 61 74 20 7a 4d 61 6c 6c 6f 63  uffer at zMalloc
3788a 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61 6c   */.  char *zMal
3788b 6c 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  loc;            
3788c 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27        /* Malloc'
3788d 64 20 73 70 61 63 65 20 28 70 6f 73 73 69 62 6c  d space (possibl
3788e 79 29 20 75 73 65 64 20 66 6f 72 20 7a 54 65 72  y) used for zTer
3788f 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 69 7a 65  m */.  int nSize
37890 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
37891 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
37892 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 20  f allocation at 
37893 61 44 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e  aData */.  int n
37894 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 20  Data;           
37895 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
37896 74 65 73 20 6f 66 20 64 61 74 61 20 69 6e 20 61  tes of data in a
37897 44 61 74 61 20 2a 2f 0a 20 20 63 68 61 72 20 2a  Data */.  char *
37898 61 44 61 74 61 3b 20 20 20 20 20 20 20 20 20 20  aData;          
37899 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
3789a 6e 74 65 72 20 74 6f 20 62 6c 6f 63 6b 20 66 72  nter to block fr
3789b 6f 6d 20 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 7d  om malloc() */.}
3789c 3b 0a 0a 2f 2a 0a 2a 2a 20 54 79 70 65 20 53 65  ;../*.** Type Se
3789d 67 6d 65 6e 74 4e 6f 64 65 20 69 73 20 75 73 65  gmentNode is use
3789e 64 20 62 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69  d by the followi
3789f 6e 67 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f  ng three functio
378a0 6e 73 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20  ns to create.** 
378a1 74 68 65 20 69 6e 74 65 72 69 6f 72 20 70 61 72  the interior par
378a2 74 20 6f 66 20 74 68 65 20 73 65 67 6d 65 6e 74  t of the segment
378a3 20 62 2b 2d 74 72 65 65 20 73 74 72 75 63 74 75   b+-tree structu
378a4 72 65 73 20 28 65 76 65 72 79 74 68 69 6e 67 20  res (everything 
378a5 65 78 63 65 70 74 0a 2a 2a 20 74 68 65 20 6c 65  except.** the le
378a6 61 66 20 6e 6f 64 65 73 29 2e 20 54 68 65 73 65  af nodes). These
378a7 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 74   functions and t
378a8 79 70 65 20 61 72 65 20 6f 6e 6c 79 20 65 76 65  ype are only eve
378a9 72 20 75 73 65 64 20 62 79 20 63 6f 64 65 0a 2a  r used by code.*
378aa 2a 20 77 69 74 68 69 6e 20 74 68 65 20 66 74 73  * within the fts
378ab 33 53 65 67 57 72 69 74 65 72 58 58 58 28 29 20  3SegWriterXXX() 
378ac 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74 69  family of functi
378ad 6f 6e 73 20 64 65 73 63 72 69 62 65 64 20 61 62  ons described ab
378ae 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 66 74 73  ove..**.**   fts
378af 33 4e 6f 64 65 41 64 64 54 65 72 6d 28 29 0a 2a  3NodeAddTerm().*
378b0 2a 20 20 20 66 74 73 33 4e 6f 64 65 57 72 69 74  *   fts3NodeWrit
378b1 65 28 29 0a 2a 2a 20 20 20 66 74 73 33 4e 6f 64  e().**   fts3Nod
378b2 65 46 72 65 65 28 29 0a 2a 2f 0a 73 74 72 75 63  eFree().*/.struc
378b3 74 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 7b 0a  t SegmentNode {.
378b4 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70    SegmentNode *p
378b5 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20  Parent;         
378b6 20 20 2f 2a 20 50 61 72 65 6e 74 20 6e 6f 64 65    /* Parent node
378b7 20 28 6f 72 20 4e 55 4c 4c 20 66 6f 72 20 72 6f   (or NULL for ro
378b8 6f 74 20 6e 6f 64 65 29 20 2a 2f 0a 20 20 53 65  ot node) */.  Se
378b9 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 52 69 67 68  gmentNode *pRigh
378ba 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
378bb 20 50 6f 69 6e 74 65 72 20 74 6f 20 72 69 67 68   Pointer to righ
378bc 74 2d 73 69 62 6c 69 6e 67 20 2a 2f 0a 20 20 53  t-sibling */.  S
378bd 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 4c 65 66  egmentNode *pLef
378be 74 6d 6f 73 74 3b 20 20 20 20 20 20 20 20 20 2f  tmost;         /
378bf 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c 65 66  * Pointer to lef
378c0 74 2d 6d 6f 73 74 20 6e 6f 64 65 20 6f 66 20 74  t-most node of t
378c1 68 69 73 20 64 65 70 74 68 20 2a 2f 0a 20 20 69  his depth */.  i
378c2 6e 74 20 6e 45 6e 74 72 79 3b 20 20 20 20 20 20  nt nEntry;      
378c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
378c4 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
378c5 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6e 6f 64  s written to nod
378c6 65 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 63 68  e so far */.  ch
378c7 61 72 20 2a 7a 54 65 72 6d 3b 20 20 20 20 20 20  ar *zTerm;      
378c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
378c9 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 72 65 76   Pointer to prev
378ca 69 6f 75 73 20 74 65 72 6d 20 62 75 66 66 65 72  ious term buffer
378cb 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b   */.  int nTerm;
378cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
378cd 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
378ce 6f 66 20 62 79 74 65 73 20 69 6e 20 7a 54 65 72  of bytes in zTer
378cf 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 6c 6c  m */.  int nMall
378d0 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  oc;             
378d1 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
378d2 66 20 6d 61 6c 6c 6f 63 27 64 20 62 75 66 66 65  f malloc'd buffe
378d3 72 20 61 74 20 7a 4d 61 6c 6c 6f 63 20 2a 2f 0a  r at zMalloc */.
378d4 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b    char *zMalloc;
378d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
378d6 20 20 2f 2a 20 4d 61 6c 6c 6f 63 27 64 20 73 70    /* Malloc'd sp
378d7 61 63 65 20 28 70 6f 73 73 69 62 6c 79 29 20 75  ace (possibly) u
378d8 73 65 64 20 66 6f 72 20 7a 54 65 72 6d 20 2a 2f  sed for zTerm */
378d9 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20  .  int nData;   
378da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
378db 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 76     /* Bytes of v
378dc 61 6c 69 64 20 64 61 74 61 20 73 6f 20 66 61 72  alid data so far
378dd 20 2a 2f 0a 20 20 63 68 61 72 20 2a 61 44 61 74   */.  char *aDat
378de 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
378df 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 64 61        /* Node da
378e0 74 61 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ta */.};../*.** 
378e1 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72  Valid values for
378e2 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
378e3 6d 65 6e 74 20 74 6f 20 66 74 73 33 53 71 6c 53  ment to fts3SqlS
378e4 74 6d 74 28 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e  tmt()..*/.#defin
378e5 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 43 4f 4e  e SQL_DELETE_CON
378e6 54 45 4e 54 20 20 20 20 20 20 20 20 20 20 20 20  TENT            
378e7 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 49   0.#define SQL_I
378e8 53 5f 45 4d 50 54 59 20 20 20 20 20 20 20 20 20  S_EMPTY         
378e9 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66            1.#def
378ea 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41  ine SQL_DELETE_A
378eb 4c 4c 5f 43 4f 4e 54 45 4e 54 20 20 20 20 20 20  LL_CONTENT      
378ec 20 20 20 32 20 0a 23 64 65 66 69 6e 65 20 53 51     2 .#define SQ
378ed 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 45 47  L_DELETE_ALL_SEG
378ee 4d 45 4e 54 53 20 20 20 20 20 20 20 20 33 0a 23  MENTS        3.#
378ef 64 65 66 69 6e 65 20 53 51 4c 5f 44 45 4c 45 54  define SQL_DELET
378f0 45 5f 41 4c 4c 5f 53 45 47 44 49 52 20 20 20 20  E_ALL_SEGDIR    
378f1 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20        4.#define 
378f2 53 51 4c 5f 53 45 4c 45 43 54 5f 43 4f 4e 54 45  SQL_SELECT_CONTE
378f3 4e 54 5f 42 59 5f 52 4f 57 49 44 20 20 20 20 35  NT_BY_ROWID    5
378f4 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 4e 45 58  .#define SQL_NEX
378f5 54 5f 53 45 47 4d 45 4e 54 5f 49 4e 44 45 58 20  T_SEGMENT_INDEX 
378f6 20 20 20 20 20 20 20 20 36 0a 23 64 65 66 69 6e          6.#defin
378f7 65 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53 45 47  e SQL_INSERT_SEG
378f8 4d 45 4e 54 53 20 20 20 20 20 20 20 20 20 20 20  MENTS           
378f9 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 4e   7.#define SQL_N
378fa 45 58 54 5f 53 45 47 4d 45 4e 54 53 5f 49 44 20  EXT_SEGMENTS_ID 
378fb 20 20 20 20 20 20 20 20 20 20 38 0a 23 64 65 66            8.#def
378fc 69 6e 65 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53  ine SQL_INSERT_S
378fd 45 47 44 49 52 20 20 20 20 20 20 20 20 20 20 20  EGDIR           
378fe 20 20 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c     9.#define SQL
378ff 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 20 20 20  _SELECT_LEVEL   
37900 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 23 64             10.#d
37901 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45 43 54  efine SQL_SELECT
37902 5f 41 4c 4c 5f 4c 45 56 45 4c 20 20 20 20 20 20  _ALL_LEVEL      
37903 20 20 20 20 31 31 0a 23 64 65 66 69 6e 65 20 53      11.#define S
37904 51 4c 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 5f  QL_SELECT_LEVEL_
37905 43 4f 55 4e 54 20 20 20 20 20 20 20 20 31 32 0a  COUNT        12.
37906 23 64 65 66 69 6e 65 20 53 51 4c 5f 53 45 4c 45  #define SQL_SELE
37907 43 54 5f 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f  CT_SEGDIR_COUNT_
37908 4d 41 58 20 20 20 31 33 0a 23 64 65 66 69 6e 65  MAX   13.#define
37909 20 53 51 4c 5f 44 45 4c 45 54 45 5f 53 45 47 44   SQL_DELETE_SEGD
3790a 49 52 5f 42 59 5f 4c 45 56 45 4c 20 20 20 20 31  IR_BY_LEVEL    1
3790b 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f 44 45  4.#define SQL_DE
3790c 4c 45 54 45 5f 53 45 47 4d 45 4e 54 53 5f 52 41  LETE_SEGMENTS_RA
3790d 4e 47 45 20 20 20 20 20 31 35 0a 23 64 65 66 69  NGE     15.#defi
3790e 6e 65 20 53 51 4c 5f 43 4f 4e 54 45 4e 54 5f 49  ne SQL_CONTENT_I
3790f 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20  NSERT           
37910 20 31 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 5f   16.#define SQL_
37911 47 45 54 5f 42 4c 4f 43 4b 20 20 20 20 20 20 20  GET_BLOCK       
37912 20 20 20 20 20 20 20 20 20 20 31 37 0a 0a 2f 2a            17../*
37913 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
37914 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6f 62 74  n is used to obt
37915 61 69 6e 20 61 6e 20 53 51 4c 69 74 65 20 70 72  ain an SQLite pr
37916 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
37917 20 68 61 6e 64 6c 65 0a 2a 2a 20 66 6f 72 20 74   handle.** for t
37918 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 64 65  he statement ide
37919 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 73  ntified by the s
3791a 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20  econd argument. 
3791b 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a  If successful,.*
3791c 2a 20 2a 70 70 20 69 73 20 73 65 74 20 74 6f 20  * *pp is set to 
3791d 74 68 65 20 72 65 71 75 65 73 74 65 64 20 73 74  the requested st
3791e 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 61  atement handle a
3791f 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74  nd SQLITE_OK ret
37920 75 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77  urned..** Otherw
37921 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  ise, an SQLite e
37922 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74  rror code is ret
37923 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 20 69 73  urned and *pp is
37924 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a   set to 0..**.**
37925 20 49 66 20 61 72 67 75 6d 65 6e 74 20 61 70 56   If argument apV
37926 61 6c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  al is not NULL, 
37927 74 68 65 6e 20 69 74 20 6d 75 73 74 20 70 6f 69  then it must poi
37928 6e 74 20 74 6f 20 61 6e 20 61 72 72 61 79 20 77  nt to an array w
37929 69 74 68 0a 2a 2a 20 61 74 20 6c 65 61 73 74 20  ith.** at least 
3792a 61 73 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 20  as many entries 
3792b 61 73 20 74 68 65 20 72 65 71 75 65 73 74 65 64  as the requested
3792c 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62   statement has b
3792d 6f 75 6e 64 20 0a 2a 2a 20 70 61 72 61 6d 65 74  ound .** paramet
3792e 65 72 73 2e 20 54 68 65 20 76 61 6c 75 65 73 20  ers. The values 
3792f 61 72 65 20 62 6f 75 6e 64 20 74 6f 20 74 68 65  are bound to the
37930 20 73 74 61 74 65 6d 65 6e 74 73 20 70 61 72 61   statements para
37931 6d 65 74 65 72 73 20 62 65 66 6f 72 65 0a 2a 2a  meters before.**
37932 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73   returning..*/.s
37933 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 71  tatic int fts3Sq
37934 6c 53 74 6d 74 28 0a 20 20 46 74 73 33 54 61 62  lStmt(.  Fts3Tab
37935 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  le *p,          
37936 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
37937 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
37938 20 2a 2f 0a 20 20 69 6e 74 20 65 53 74 6d 74 2c   */.  int eStmt,
37939 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3793a 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
3793b 74 68 65 20 53 51 4c 5f 58 58 58 20 63 6f 6e 73  the SQL_XXX cons
3793c 74 61 6e 74 73 20 61 62 6f 76 65 20 2a 2f 0a 20  tants above */. 
3793d 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
3793e 70 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pp,             
3793f 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65   /* OUT: Stateme
37940 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 73  nt handle */.  s
37941 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
37942 70 56 61 6c 20 20 20 20 20 20 20 20 20 20 20 2f  pVal           /
37943 2a 20 56 61 6c 75 65 73 20 74 6f 20 62 69 6e 64  * Values to bind
37944 20 74 6f 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f   to statement */
37945 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  .){.  const char
37946 20 2a 61 7a 53 71 6c 5b 5d 20 3d 20 7b 0a 2f 2a   *azSql[] = {./*
37947 20 30 20 20 2a 2f 20 20 22 44 45 4c 45 54 45 20   0  */  "DELETE 
37948 46 52 4f 4d 20 25 51 2e 27 25 71 5f 63 6f 6e 74  FROM %Q.'%q_cont
37949 65 6e 74 27 20 57 48 45 52 45 20 72 6f 77 69 64  ent' WHERE rowid
3794a 20 3d 20 3f 22 2c 0a 2f 2a 20 31 20 20 2a 2f 20   = ?",./* 1  */ 
3794b 20 22 53 45 4c 45 43 54 20 4e 4f 54 20 45 58 49   "SELECT NOT EXI
3794c 53 54 53 28 53 45 4c 45 43 54 20 64 6f 63 69 64  STS(SELECT docid
3794d 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 63 6f 6e   FROM %Q.'%q_con
3794e 74 65 6e 74 27 20 57 48 45 52 45 20 72 6f 77 69  tent' WHERE rowi
3794f 64 21 3d 3f 29 22 2c 0a 2f 2a 20 32 20 20 2a 2f  d!=?)",./* 2  */
37950 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
37951 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 22 2c  Q.'%q_content'",
37952 0a 2f 2a 20 33 20 20 2a 2f 20 20 22 44 45 4c 45  ./* 3  */  "DELE
37953 54 45 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73  TE FROM %Q.'%q_s
37954 65 67 6d 65 6e 74 73 27 22 2c 0a 2f 2a 20 34 20  egments'",./* 4 
37955 20 2a 2f 20 20 22 44 45 4c 45 54 45 20 46 52 4f   */  "DELETE FRO
37956 4d 20 25 51 2e 27 25 71 5f 73 65 67 64 69 72 27  M %Q.'%q_segdir'
37957 22 2c 0a 2f 2a 20 35 20 20 2a 2f 20 20 22 53 45  ",./* 5  */  "SE
37958 4c 45 43 54 20 2a 20 46 52 4f 4d 20 25 51 2e 27  LECT * FROM %Q.'
37959 25 71 5f 63 6f 6e 74 65 6e 74 27 20 57 48 45 52  %q_content' WHER
3795a 45 20 72 6f 77 69 64 3d 3f 22 2c 0a 2f 2a 20 36  E rowid=?",./* 6
3795b 20 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 63 6f    */  "SELECT co
3795c 61 6c 65 73 63 65 28 6d 61 78 28 69 64 78 29 2b  alesce(max(idx)+
3795d 31 2c 20 30 29 20 46 52 4f 4d 20 25 51 2e 27 25  1, 0) FROM %Q.'%
3795e 71 5f 73 65 67 64 69 72 27 20 57 48 45 52 45 20  q_segdir' WHERE 
3795f 6c 65 76 65 6c 3d 3f 22 2c 0a 2f 2a 20 37 20 20  level=?",./* 7  
37960 2a 2f 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f  */  "INSERT INTO
37961 20 25 51 2e 27 25 71 5f 73 65 67 6d 65 6e 74 73   %Q.'%q_segments
37962 27 28 62 6c 6f 63 6b 69 64 2c 20 62 6c 6f 63 6b  '(blockid, block
37963 29 20 56 41 4c 55 45 53 28 3f 2c 20 3f 29 22 2c  ) VALUES(?, ?)",
37964 0a 2f 2a 20 38 20 20 2a 2f 20 20 22 53 45 4c 45  ./* 8  */  "SELE
37965 43 54 20 63 6f 61 6c 65 73 63 65 28 6d 61 78 28  CT coalesce(max(
37966 62 6c 6f 63 6b 69 64 29 2b 31 2c 20 31 29 20 46  blockid)+1, 1) F
37967 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 6d 65  ROM %Q.'%q_segme
37968 6e 74 73 27 22 2c 0a 2f 2a 20 39 20 20 2a 2f 20  nts'",./* 9  */ 
37969 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51   "INSERT INTO %Q
3796a 2e 27 25 71 5f 73 65 67 64 69 72 27 20 56 41 4c  .'%q_segdir' VAL
3796b 55 45 53 28 3f 2c 3f 2c 3f 2c 3f 2c 3f 2c 3f 29  UES(?,?,?,?,?,?)
3796c 22 2c 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  ",..          /*
3796d 20 52 65 74 75 72 6e 20 73 65 67 6d 65 6e 74 73   Return segments
3796e 20 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d 20 6f   in order from o
3796f 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73 74 2e  ldest to newest.
37970 2a 2f 20 0a 2f 2a 20 31 30 20 2a 2f 20 20 22 53  */ ./* 10 */  "S
37971 45 4c 45 43 54 20 69 64 78 2c 20 73 74 61 72 74  ELECT idx, start
37972 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65  _block, leaves_e
37973 6e 64 5f 62 6c 6f 63 6b 2c 20 65 6e 64 5f 62 6c  nd_block, end_bl
37974 6f 63 6b 2c 20 72 6f 6f 74 20 22 0a 20 20 20 20  ock, root ".    
37975 20 20 20 20 20 20 20 20 22 46 52 4f 4d 20 25 51          "FROM %Q
37976 2e 27 25 71 5f 73 65 67 64 69 72 27 20 57 48 45  .'%q_segdir' WHE
37977 52 45 20 6c 65 76 65 6c 20 3d 20 3f 20 4f 52 44  RE level = ? ORD
37978 45 52 20 42 59 20 69 64 78 20 41 53 43 22 2c 0a  ER BY idx ASC",.
37979 2f 2a 20 31 31 20 2a 2f 20 20 22 53 45 4c 45 43  /* 11 */  "SELEC
3797a 54 20 69 64 78 2c 20 73 74 61 72 74 5f 62 6c 6f  T idx, start_blo
3797b 63 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62  ck, leaves_end_b
3797c 6c 6f 63 6b 2c 20 65 6e 64 5f 62 6c 6f 63 6b 2c  lock, end_block,
3797d 20 72 6f 6f 74 20 22 0a 20 20 20 20 20 20 20 20   root ".        
3797e 20 20 20 20 22 46 52 4f 4d 20 25 51 2e 27 25 71      "FROM %Q.'%q
3797f 5f 73 65 67 64 69 72 27 20 4f 52 44 45 52 20 42  _segdir' ORDER B
37980 59 20 6c 65 76 65 6c 20 44 45 53 43 2c 20 69 64  Y level DESC, id
37981 78 20 41 53 43 22 2c 0a 0a 2f 2a 20 31 32 20 2a  x ASC",../* 12 *
37982 2f 20 20 22 53 45 4c 45 43 54 20 63 6f 75 6e 74  /  "SELECT count
37983 28 2a 29 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f  (*) FROM %Q.'%q_
37984 73 65 67 64 69 72 27 20 57 48 45 52 45 20 6c 65  segdir' WHERE le
37985 76 65 6c 20 3d 20 3f 22 2c 0a 2f 2a 20 31 33 20  vel = ?",./* 13 
37986 2a 2f 20 20 22 53 45 4c 45 43 54 20 63 6f 75 6e  */  "SELECT coun
37987 74 28 2a 29 2c 20 6d 61 78 28 6c 65 76 65 6c 29  t(*), max(level)
37988 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67   FROM %Q.'%q_seg
37989 64 69 72 27 22 2c 0a 0a 2f 2a 20 31 34 20 2a 2f  dir'",../* 14 */
3798a 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25    "DELETE FROM %
3798b 51 2e 27 25 71 5f 73 65 67 64 69 72 27 20 57 48  Q.'%q_segdir' WH
3798c 45 52 45 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a  ERE level = ?",.
3798d 2f 2a 20 31 35 20 2a 2f 20 20 22 44 45 4c 45 54  /* 15 */  "DELET
3798e 45 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f 73 65  E FROM %Q.'%q_se
3798f 67 6d 65 6e 74 73 27 20 57 48 45 52 45 20 62 6c  gments' WHERE bl
37990 6f 63 6b 69 64 20 42 45 54 57 45 45 4e 20 3f 20  ockid BETWEEN ? 
37991 41 4e 44 20 3f 22 2c 0a 2f 2a 20 31 36 20 2a 2f  AND ?",./* 16 */
37992 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25    "INSERT INTO %
37993 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27 20 56  Q.'%q_content' V
37994 41 4c 55 45 53 28 25 7a 29 22 2c 0a 2f 2a 20 31  ALUES(%z)",./* 1
37995 37 20 2a 2f 20 20 22 53 45 4c 45 43 54 20 62 6c  7 */  "SELECT bl
37996 6f 63 6b 20 46 52 4f 4d 20 25 51 2e 27 25 71 5f  ock FROM %Q.'%q_
37997 73 65 67 6d 65 6e 74 73 27 20 57 48 45 52 45 20  segments' WHERE 
37998 62 6c 6f 63 6b 69 64 20 3d 20 3f 22 2c 0a 20 20  blockid = ?",.  
37999 7d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  };.  int rc = SQ
3799a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71 6c 69 74  LITE_OK;.  sqlit
3799b 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a  e3_stmt *pStmt;.
3799c 0a 20 20 61 73 73 65 72 74 28 20 53 69 7a 65 6f  .  assert( Sizeo
3799d 66 41 72 72 61 79 28 61 7a 53 71 6c 29 3d 3d 53  fArray(azSql)==S
3799e 69 7a 65 6f 66 41 72 72 61 79 28 70 2d 3e 61 53  izeofArray(p->aS
3799f 74 6d 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tmt) );.  assert
379a0 28 20 65 53 74 6d 74 3c 53 69 7a 65 6f 66 41 72  ( eStmt<SizeofAr
379a1 72 61 79 28 61 7a 53 71 6c 29 20 26 26 20 65 53  ray(azSql) && eS
379a2 74 6d 74 3e 3d 30 20 29 3b 0a 20 20 0a 20 20 70  tmt>=0 );.  .  p
379a3 53 74 6d 74 20 3d 20 70 2d 3e 61 53 74 6d 74 5b  Stmt = p->aStmt[
379a4 65 53 74 6d 74 5d 3b 0a 20 20 69 66 28 20 21 70  eStmt];.  if( !p
379a5 53 74 6d 74 20 29 7b 0a 20 20 20 20 63 68 61 72  Stmt ){.    char
379a6 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 69 66 28 20   *zSql;.    if( 
379a7 65 53 74 6d 74 3d 3d 53 51 4c 5f 43 4f 4e 54 45  eStmt==SQL_CONTE
379a8 4e 54 5f 49 4e 53 45 52 54 20 29 7b 0a 20 20 20  NT_INSERT ){.   
379a9 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20     int i;       
379aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
379ab 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
379ac 62 6c 65 20 2a 2f 20 20 0a 20 20 20 20 20 20 63  ble */  .      c
379ad 68 61 72 20 2a 7a 56 61 72 6c 69 73 74 3b 20 20  har *zVarlist;  
379ae 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
379af 65 20 22 3f 2c 20 3f 2c 20 2e 2e 2e 22 20 73 74  e "?, ?, ..." st
379b0 72 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 7a 56  ring */.      zV
379b1 61 72 6c 69 73 74 20 3d 20 28 63 68 61 72 20 2a  arlist = (char *
379b2 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  )sqlite3_malloc(
379b3 32 2a 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b  2*p->nColumn+2);
379b4 0a 20 20 20 20 20 20 69 66 28 20 21 7a 56 61 72  .      if( !zVar
379b5 6c 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  list ){.        
379b6 2a 70 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  *pp = 0;.       
379b7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
379b8 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OMEM;.      }.  
379b9 20 20 20 20 7a 56 61 72 6c 69 73 74 5b 30 5d 20      zVarlist[0] 
379ba 3d 20 27 3f 27 3b 0a 20 20 20 20 20 20 7a 56 61  = '?';.      zVa
379bb 72 6c 69 73 74 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e  rlist[p->nColumn
379bc 2a 32 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  *2+1] = '\0';.  
379bd 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d      for(i=1; i<=
379be 70 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  p->nColumn; i++)
379bf 7b 0a 20 20 20 20 20 20 20 20 7a 56 61 72 6c 69  {.        zVarli
379c0 73 74 5b 69 2a 32 2d 31 5d 20 3d 20 27 2c 27 3b  st[i*2-1] = ',';
379c1 0a 20 20 20 20 20 20 20 20 7a 56 61 72 6c 69 73  .        zVarlis
379c2 74 5b 69 2a 32 5d 20 3d 20 27 3f 27 3b 0a 20 20  t[i*2] = '?';.  
379c3 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 53 71 6c      }.      zSql
379c4 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
379c5 74 66 28 61 7a 53 71 6c 5b 65 53 74 6d 74 5d 2c  tf(azSql[eStmt],
379c6 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61 6d   p->zDb, p->zNam
379c7 65 2c 20 7a 56 61 72 6c 69 73 74 29 3b 0a 20 20  e, zVarlist);.  
379c8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
379c9 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
379ca 72 69 6e 74 66 28 61 7a 53 71 6c 5b 65 53 74 6d  rintf(azSql[eStm
379cb 74 5d 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a  t], p->zDb, p->z
379cc 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Name);.    }.   
379cd 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20   if( !zSql ){.  
379ce 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
379cf 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65  NOMEM;.    }else
379d0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
379d1 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
379d2 70 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  p->db, zSql, -1,
379d3 20 26 70 53 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a   &pStmt, NULL);.
379d4 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
379d5 65 65 28 7a 53 71 6c 29 3b 0a 20 20 20 20 20 20  ee(zSql);.      
379d6 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
379d7 54 45 5f 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d  TE_OK || pStmt==
379d8 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 53  0 );.      p->aS
379d9 74 6d 74 5b 65 53 74 6d 74 5d 20 3d 20 70 53 74  tmt[eStmt] = pSt
379da 6d 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  mt;.    }.  }.  
379db 69 66 28 20 61 70 56 61 6c 20 29 7b 0a 20 20 20  if( apVal ){.   
379dc 20 69 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20   int i;.    int 
379dd 6e 50 61 72 61 6d 20 3d 20 73 71 6c 69 74 65 33  nParam = sqlite3
379de 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
379df 63 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20  count(pStmt);.  
379e0 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
379e1 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 6e 50  QLITE_OK && i<nP
379e2 61 72 61 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  aram; i++){.    
379e3 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
379e4 69 6e 64 5f 76 61 6c 75 65 28 70 53 74 6d 74 2c  ind_value(pStmt,
379e5 20 69 2b 31 2c 20 61 70 56 61 6c 5b 69 5d 29 3b   i+1, apVal[i]);
379e6 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 70  .    }.  }.  *pp
379e7 20 3d 20 70 53 74 6d 74 3b 0a 20 20 72 65 74 75   = pStmt;.  retu
379e8 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
379e9 53 69 6d 69 6c 61 72 20 74 6f 20 66 74 73 33 53  Similar to fts3S
379ea 71 6c 53 74 6d 74 28 29 2e 20 45 78 63 65 70 74  qlStmt(). Except
379eb 2c 20 61 66 74 65 72 20 62 69 6e 64 69 6e 67 20  , after binding 
379ec 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 69  the parameters i
379ed 6e 0a 2a 2a 20 61 72 72 61 79 20 61 70 56 61 6c  n.** array apVal
379ee 5b 5d 20 74 6f 20 74 68 65 20 53 51 4c 20 73 74  [] to the SQL st
379ef 61 74 65 6d 65 6e 74 20 69 64 65 6e 74 69 66 69  atement identifi
379f0 65 64 20 62 79 20 65 53 74 6d 74 2c 20 74 68 65  ed by eStmt, the
379f1 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 69 73   statement.** is
379f2 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a 0a 2a 2a   executed..**.**
379f3 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   Returns SQLITE_
379f4 4f 4b 20 69 66 20 74 68 65 20 73 74 61 74 65 6d  OK if the statem
379f5 65 6e 74 20 69 73 20 73 75 63 63 65 73 73 66 75  ent is successfu
379f6 6c 6c 79 20 65 78 65 63 75 74 65 64 2c 20 6f 72  lly executed, or
379f7 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 65 72   an.** SQLite er
379f8 72 6f 72 20 63 6f 64 65 20 6f 74 68 65 72 77 69  ror code otherwi
379f9 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
379fa 74 20 66 74 73 33 53 71 6c 45 78 65 63 28 46 74  t fts3SqlExec(Ft
379fb 73 33 54 61 62 6c 65 20 2a 70 2c 20 69 6e 74 20  s3Table *p, int 
379fc 65 53 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 76  eStmt, sqlite3_v
379fd 61 6c 75 65 20 2a 2a 61 70 56 61 6c 29 7b 0a 20  alue **apVal){. 
379fe 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
379ff 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d  Stmt;.  int rc =
37a00 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20   fts3SqlStmt(p, 
37a01 65 53 74 6d 74 2c 20 26 70 53 74 6d 74 2c 20 61  eStmt, &pStmt, a
37a02 70 56 61 6c 29 3b 20 0a 20 20 69 66 28 20 72 63  pVal); .  if( rc
37a03 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
37a04 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28     sqlite3_step(
37a05 70 53 74 6d 74 29 3b 0a 20 20 20 20 72 63 20 3d  pStmt);.    rc =
37a06 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
37a07 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Stmt);.  }.  ret
37a08 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
37a09 2a 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65 20  * Read a single 
37a0a 62 6c 6f 63 6b 20 66 72 6f 6d 20 74 68 65 20 25  block from the %
37a0b 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c 65 2e  _segments table.
37a0c 20 49 66 20 74 68 65 20 73 70 65 63 69 66 69 65   If the specifie
37a0d 64 20 62 6c 6f 63 6b 0a 2a 2a 20 64 6f 65 73 20  d block.** does 
37a0e 6e 6f 74 20 65 78 69 73 74 2c 20 72 65 74 75 72  not exist, retur
37a0f 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
37a10 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68 65 72 20  . If some other 
37a11 65 72 72 6f 72 20 28 6d 61 6c 6c 6f 63 2c 20 49  error (malloc, I
37a12 4f 20 0a 2a 2a 20 65 74 63 2e 29 20 6f 63 63 75  O .** etc.) occu
37a13 72 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20 61  rs, return the a
37a14 70 70 72 6f 70 72 69 61 74 65 20 53 51 4c 69 74  ppropriate SQLit
37a15 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a  e error code..**
37a16 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69  .** Otherwise, i
37a17 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65  f successful, se
37a18 74 20 2a 70 7a 42 6c 6f 63 6b 20 74 6f 20 70 6f  t *pzBlock to po
37a19 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20  int to a buffer 
37a1a 63 6f 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 74 68  containing.** th
37a1b 65 20 62 6c 6f 63 6b 20 72 65 61 64 20 66 72 6f  e block read fro
37a1c 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20  m the database, 
37a1d 61 6e 64 20 2a 70 6e 42 6c 6f 63 6b 20 74 6f 20  and *pnBlock to 
37a1e 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
37a1f 72 65 61 64 0a 2a 2a 20 62 6c 6f 63 6b 20 69 6e  read.** block in
37a20 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 57 41   bytes..**.** WA
37a21 52 4e 49 4e 47 3a 20 54 68 65 20 72 65 74 75 72  RNING: The retur
37a22 6e 65 64 20 62 75 66 66 65 72 20 69 73 20 6f 6e  ned buffer is on
37a23 6c 79 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74  ly valid until t
37a24 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20  he next call to 
37a25 0a 2a 2a 20 73 71 6c 69 74 65 33 46 74 73 33 52  .** sqlite3Fts3R
37a26 65 61 64 42 6c 6f 63 6b 28 29 2e 0a 2a 2f 0a 53  eadBlock()..*/.S
37a27 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
37a28 74 20 73 71 6c 69 74 65 33 46 74 73 33 52 65 61  t sqlite3Fts3Rea
37a29 64 42 6c 6f 63 6b 28 0a 20 20 46 74 73 33 54 61  dBlock(.  Fts3Ta
37a2a 62 6c 65 20 2a 70 2c 0a 20 20 73 71 6c 69 74 65  ble *p,.  sqlite
37a2b 33 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 2c 0a  3_int64 iBlock,.
37a2c 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70    char const **p
37a2d 7a 42 6c 6f 63 6b 2c 0a 20 20 69 6e 74 20 2a 70  zBlock,.  int *p
37a2e 6e 42 6c 6f 63 6b 0a 29 7b 0a 20 20 73 71 6c 69  nBlock.){.  sqli
37a2f 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b  te3_stmt *pStmt;
37a30 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 74 73 33  .  int rc = fts3
37a31 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 47  SqlStmt(p, SQL_G
37a32 45 54 5f 42 4c 4f 43 4b 2c 20 26 70 53 74 6d 74  ET_BLOCK, &pStmt
37a33 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
37a34 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
37a35 72 6e 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33  rn rc;.  sqlite3
37a36 5f 72 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 0a  _reset(pStmt);..
37a37 20 20 69 66 28 20 70 7a 42 6c 6f 63 6b 20 29 7b    if( pzBlock ){
37a38 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  .    sqlite3_bin
37a39 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 31  d_int64(pStmt, 1
37a3a 2c 20 69 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 72  , iBlock);.    r
37a3b 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
37a3c 28 70 53 74 6d 74 29 3b 20 0a 20 20 20 20 69 66  (pStmt); .    if
37a3d 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
37a3e 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
37a3f 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b   SQLITE_CORRUPT;
37a40 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2a 70  .    }.  .    *p
37a41 6e 42 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33  nBlock = sqlite3
37a42 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53  _column_bytes(pS
37a43 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20 2a 70 7a  tmt, 0);.    *pz
37a44 42 6c 6f 63 6b 20 3d 20 28 63 68 61 72 20 2a 29  Block = (char *)
37a45 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
37a46 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  lob(pStmt, 0);. 
37a47 20 20 20 69 66 28 20 21 2a 70 7a 42 6c 6f 63 6b     if( !*pzBlock
37a48 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
37a49 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
37a4a 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
37a4b 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
37a4c 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 70 53 74 6d  /*.** Set *ppStm
37a4d 74 20 74 6f 20 61 20 73 74 61 74 65 6d 65 6e 74  t to a statement
37a4e 20 68 61 6e 64 6c 65 20 74 68 61 74 20 6d 61 79   handle that may
37a4f 20 62 65 20 75 73 65 64 20 74 6f 20 69 74 65 72   be used to iter
37a50 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20 61  ate through.** a
37a51 6c 6c 20 72 6f 77 73 20 69 6e 20 74 68 65 20 25  ll rows in the %
37a52 5f 73 65 67 64 69 72 20 74 61 62 6c 65 2c 20 66  _segdir table, f
37a53 72 6f 6d 20 6f 6c 64 65 73 74 20 74 6f 20 6e 65  rom oldest to ne
37a54 77 65 73 74 2e 20 49 66 20 73 75 63 63 65 73 73  west. If success
37a55 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 53  ful,.** return S
37a56 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e 20  QLITE_OK. If an 
37a57 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69  error occurs whi
37a58 6c 65 20 70 72 65 70 61 72 69 6e 67 20 74 68 65  le preparing the
37a59 20 73 74 61 74 65 6d 65 6e 74 2c 20 0a 2a 2a 20   statement, .** 
37a5a 72 65 74 75 72 6e 20 61 6e 20 53 51 4c 69 74 65  return an SQLite
37a5b 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a   error code..**.
37a5c 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c 79  ** There is only
37a5d 20 65 76 65 72 20 6f 6e 65 20 69 6e 73 74 61 6e   ever one instan
37a5e 63 65 20 6f 66 20 74 68 69 73 20 53 51 4c 20 73  ce of this SQL s
37a5f 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65  tatement compile
37a60 64 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 46 54  d for.** each FT
37a61 53 33 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  S3 table..**.** 
37a62 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65  The statement re
37a63 74 75 72 6e 73 20 74 68 65 20 66 6f 6c 6c 6f 77  turns the follow
37a64 69 6e 67 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d  ing columns from
37a65 20 74 68 65 20 25 5f 73 65 67 64 69 72 20 74 61   the %_segdir ta
37a66 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 30 3a 20  ble:.**.**   0: 
37a67 69 64 78 0a 2a 2a 20 20 20 31 3a 20 73 74 61 72  idx.**   1: star
37a68 74 5f 62 6c 6f 63 6b 0a 2a 2a 20 20 20 32 3a 20  t_block.**   2: 
37a69 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b  leaves_end_block
37a6a 0a 2a 2a 20 20 20 33 3a 20 65 6e 64 5f 62 6c 6f  .**   3: end_blo
37a6b 63 6b 0a 2a 2a 20 20 20 34 3a 20 72 6f 6f 74 0a  ck.**   4: root.
37a6c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
37a6d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
37a6e 33 41 6c 6c 53 65 67 64 69 72 73 28 46 74 73 33  3AllSegdirs(Fts3
37a6f 54 61 62 6c 65 20 2a 70 2c 20 73 71 6c 69 74 65  Table *p, sqlite
37a70 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 29  3_stmt **ppStmt)
37a71 7b 0a 20 20 72 65 74 75 72 6e 20 66 74 73 33 53  {.  return fts3S
37a72 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 53 45  qlStmt(p, SQL_SE
37a73 4c 45 43 54 5f 41 4c 4c 5f 4c 45 56 45 4c 2c 20  LECT_ALL_LEVEL, 
37a74 70 70 53 74 6d 74 2c 20 30 29 3b 0a 7d 0a 0a 0a  ppStmt, 0);.}...
37a75 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 73  /*.** Append a s
37a76 69 6e 67 6c 65 20 76 61 72 69 6e 74 20 74 6f 20  ingle varint to 
37a77 61 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 62 75  a PendingList bu
37a78 66 66 65 72 2e 20 53 51 4c 49 54 45 5f 4f 4b 20  ffer. SQLITE_OK 
37a79 69 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69  is returned.** i
37a7a 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72  f successful, or
37a7b 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
37a7c 20 63 6f 64 65 20 6f 74 68 65 72 77 69 73 65 2e   code otherwise.
37a7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
37a7e 74 69 6f 6e 20 61 6c 73 6f 20 73 65 72 76 65 73  tion also serves
37a7f 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 74 68 65   to allocate the
37a80 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72   PendingList str
37a81 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a  ucture itself..*
37a82 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74  * For example, t
37a83 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 50  o create a new P
37a84 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72 75 63  endingList struc
37a85 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ture containing 
37a86 74 77 6f 0a 2a 2a 20 76 61 72 69 6e 74 73 3a 0a  two.** varints:.
37a87 2a 2a 0a 2a 2a 20 20 20 50 65 6e 64 69 6e 67 4c  **.**   PendingL
37a88 69 73 74 20 2a 70 20 3d 20 30 3b 0a 2a 2a 20 20  ist *p = 0;.**  
37a89 20 66 74 73 33 50 65 6e 64 69 6e 67 4c 69 73 74   fts3PendingList
37a8a 41 70 70 65 6e 64 56 61 72 69 6e 74 28 26 70 2c  AppendVarint(&p,
37a8b 20 31 29 3b 0a 2a 2a 20 20 20 66 74 73 33 50 65   1);.**   fts3Pe
37a8c 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e 64 56  ndingListAppendV
37a8d 61 72 69 6e 74 28 26 70 2c 20 32 29 3b 0a 2a 2f  arint(&p, 2);.*/
37a8e 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
37a8f 50 65 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e  PendingListAppen
37a90 64 56 61 72 69 6e 74 28 0a 20 20 50 65 6e 64 69  dVarint(.  Pendi
37a91 6e 67 4c 69 73 74 20 2a 2a 70 70 2c 20 20 20 20  ngList **pp,    
37a92 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e             /* IN
37a93 2f 4f 55 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f  /OUT: Pointer to
37a94 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72   PendingList str
37a95 75 63 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  uct */.  sqlite3
37a96 5f 69 6e 74 36 34 20 69 20 20 20 20 20 20 20 20  _int64 i        
37a97 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75           /* Valu
37a98 65 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 64  e to append to d
37a99 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 50 65 6e 64  ata */.){.  Pend
37a9a 69 6e 67 4c 69 73 74 20 2a 70 20 3d 20 2a 70 70  ingList *p = *pp
37a9b 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
37a9c 20 6f 72 20 67 72 6f 77 20 74 68 65 20 50 65 6e   or grow the Pen
37a9d 64 69 6e 67 4c 69 73 74 20 61 73 20 72 65 71 75  dingList as requ
37a9e 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 21  ired. */.  if( !
37a9f 70 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c  p ){.    p = sql
37aa0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65  ite3_malloc(size
37aa1 6f 66 28 2a 70 29 20 2b 20 31 30 30 29 3b 0a 20  of(*p) + 100);. 
37aa2 20 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20     if( !p ){.   
37aa3 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
37aa4 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
37aa5 20 20 70 2d 3e 6e 53 70 61 63 65 20 3d 20 31 30    p->nSpace = 10
37aa6 30 3b 0a 20 20 20 20 70 2d 3e 61 44 61 74 61 20  0;.    p->aData 
37aa7 3d 20 28 63 68 61 72 20 2a 29 26 70 5b 31 5d 3b  = (char *)&p[1];
37aa8 0a 20 20 20 20 70 2d 3e 6e 44 61 74 61 20 3d 20  .    p->nData = 
37aa9 30 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66  0;.  }.  else if
37aaa 28 20 70 2d 3e 6e 44 61 74 61 2b 46 54 53 33 5f  ( p->nData+FTS3_
37aab 56 41 52 49 4e 54 5f 4d 41 58 2b 31 3e 70 2d 3e  VARINT_MAX+1>p->
37aac 6e 53 70 61 63 65 20 29 7b 0a 20 20 20 20 69 6e  nSpace ){.    in
37aad 74 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 53 70 61  t nNew = p->nSpa
37aae 63 65 20 2a 20 32 3b 0a 20 20 20 20 70 20 3d 20  ce * 2;.    p = 
37aaf 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
37ab0 70 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 2b 20  p, sizeof(*p) + 
37ab1 6e 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20 21  nNew);.    if( !
37ab2 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
37ab3 65 33 5f 66 72 65 65 28 2a 70 70 29 3b 0a 20 20  e3_free(*pp);.  
37ab4 20 20 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 20      *pp = 0;.   
37ab5 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
37ab6 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
37ab7 20 20 70 2d 3e 6e 53 70 61 63 65 20 3d 20 6e 4e    p->nSpace = nN
37ab8 65 77 3b 0a 20 20 20 20 70 2d 3e 61 44 61 74 61  ew;.    p->aData
37ab9 20 3d 20 28 63 68 61 72 20 2a 29 26 70 5b 31 5d   = (char *)&p[1]
37aba 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 70 70 65  ;.  }..  /* Appe
37abb 6e 64 20 74 68 65 20 6e 65 77 20 73 65 72 69 61  nd the new seria
37abc 6c 69 7a 65 64 20 76 61 72 69 6e 74 20 74 6f 20  lized varint to 
37abd 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c  the end of the l
37abe 69 73 74 2e 20 2a 2f 0a 20 20 70 2d 3e 6e 44 61  ist. */.  p->nDa
37abf 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  ta += sqlite3Fts
37ac0 33 50 75 74 56 61 72 69 6e 74 28 26 70 2d 3e 61  3PutVarint(&p->a
37ac1 44 61 74 61 5b 70 2d 3e 6e 44 61 74 61 5d 2c 20  Data[p->nData], 
37ac2 69 29 3b 0a 20 20 70 2d 3e 61 44 61 74 61 5b 70  i);.  p->aData[p
37ac3 2d 3e 6e 44 61 74 61 5d 20 3d 20 27 5c 30 27 3b  ->nData] = '\0';
37ac4 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 72 65  .  *pp = p;.  re
37ac5 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
37ac6 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 64  }../*.** Add a d
37ac7 6f 63 69 64 2f 63 6f 6c 75 6d 6e 2f 70 6f 73 69  ocid/column/posi
37ac8 74 69 6f 6e 20 65 6e 74 72 79 20 74 6f 20 61 20  tion entry to a 
37ac9 50 65 6e 64 69 6e 67 4c 69 73 74 20 73 74 72 75  PendingList stru
37aca 63 74 75 72 65 2e 20 4e 6f 6e 2d 7a 65 72 6f 0a  cture. Non-zero.
37acb 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ** is returned i
37acc 66 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  f the structure 
37acd 69 73 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  is sqlite3_reall
37ace 6f 63 65 64 20 61 73 20 70 61 72 74 20 6f 66 20  oced as part of 
37acf 61 64 64 69 6e 67 0a 2a 2a 20 74 68 65 20 65 6e  adding.** the en
37ad0 74 72 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  try. Otherwise, 
37ad1 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  zero..**.** If a
37ad2 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75  n OOM error occu
37ad3 72 73 2c 20 2a 70 52 63 20 69 73 20 73 65 74 20  rs, *pRc is set 
37ad4 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  to SQLITE_NOMEM 
37ad5 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
37ad6 2e 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 61 6c 77  ..** Zero is alw
37ad7 61 79 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20  ays returned in 
37ad8 74 68 69 73 20 63 61 73 65 2e 20 4f 74 68 65 72  this case. Other
37ad9 77 69 73 65 2c 20 69 66 20 6e 6f 20 4f 4f 4d 20  wise, if no OOM 
37ada 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a  error occurs,.**
37adb 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 53 51   it is set to SQ
37adc 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74  LITE_OK..*/.stat
37add 69 63 20 69 6e 74 20 66 74 73 33 50 65 6e 64 69  ic int fts3Pendi
37ade 6e 67 4c 69 73 74 41 70 70 65 6e 64 28 0a 20 20  ngListAppend(.  
37adf 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a 2a 70 70  PendingList **pp
37ae0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
37ae1 2f 2a 20 49 4e 2f 4f 55 54 3a 20 50 65 6e 64 69  /* IN/OUT: Pendi
37ae2 6e 67 4c 69 73 74 20 73 74 72 75 63 74 75 72 65  ngList structure
37ae3 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
37ae4 74 36 34 20 69 44 6f 63 69 64 2c 20 20 20 20 20  t64 iDocid,     
37ae5 20 20 20 20 20 20 2f 2a 20 44 6f 63 69 64 20 66        /* Docid f
37ae6 6f 72 20 65 6e 74 72 79 20 74 6f 20 61 64 64 20  or entry to add 
37ae7 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
37ae8 36 34 20 69 43 6f 6c 2c 20 20 20 20 20 20 20 20  64 iCol,        
37ae9 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 66       /* Column f
37aea 6f 72 20 65 6e 74 72 79 20 74 6f 20 61 64 64 20  or entry to add 
37aeb 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
37aec 36 34 20 69 50 6f 73 2c 20 20 20 20 20 20 20 20  64 iPos,        
37aed 20 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e       /* Position
37aee 20 6f 66 20 74 65 72 6d 20 66 6f 72 20 65 6e 74   of term for ent
37aef 72 79 20 74 6f 20 61 64 64 20 2a 2f 0a 20 20 69  ry to add */.  i
37af0 6e 74 20 2a 70 52 63 20 20 20 20 20 20 20 20 20  nt *pRc         
37af1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37af2 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 63 6f  * OUT: Return co
37af3 64 65 20 2a 2f 0a 29 7b 0a 20 20 50 65 6e 64 69  de */.){.  Pendi
37af4 6e 67 4c 69 73 74 20 2a 70 20 3d 20 2a 70 70 3b  ngList *p = *pp;
37af5 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
37af6 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74  TE_OK;..  assert
37af7 28 20 21 70 20 7c 7c 20 70 2d 3e 69 4c 61 73 74  ( !p || p->iLast
37af8 44 6f 63 69 64 3c 3d 69 44 6f 63 69 64 20 29 3b  Docid<=iDocid );
37af9 0a 0a 20 20 69 66 28 20 21 70 20 7c 7c 20 70 2d  ..  if( !p || p-
37afa 3e 69 4c 61 73 74 44 6f 63 69 64 21 3d 69 44 6f  >iLastDocid!=iDo
37afb 63 69 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  cid ){.    sqlit
37afc 65 33 5f 69 6e 74 36 34 20 69 44 65 6c 74 61 20  e3_int64 iDelta 
37afd 3d 20 69 44 6f 63 69 64 20 2d 20 28 70 20 3f 20  = iDocid - (p ? 
37afe 70 2d 3e 69 4c 61 73 74 44 6f 63 69 64 20 3a 20  p->iLastDocid : 
37aff 30 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b  0);.    if( p ){
37b00 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
37b01 2d 3e 6e 44 61 74 61 3c 70 2d 3e 6e 53 70 61 63  ->nData<p->nSpac
37b02 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  e );.      asser
37b03 74 28 20 70 2d 3e 61 44 61 74 61 5b 70 2d 3e 6e  t( p->aData[p->n
37b04 44 61 74 61 5d 3d 3d 30 20 29 3b 0a 20 20 20 20  Data]==0 );.    
37b05 20 20 70 2d 3e 6e 44 61 74 61 2b 2b 3b 0a 20 20    p->nData++;.  
37b06 20 20 7d 0a 20 20 20 20 69 66 28 20 53 51 4c 49    }.    if( SQLI
37b07 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 66 74 73  TE_OK!=(rc = fts
37b08 33 50 65 6e 64 69 6e 67 4c 69 73 74 41 70 70 65  3PendingListAppe
37b09 6e 64 56 61 72 69 6e 74 28 26 70 2c 20 69 44 65  ndVarint(&p, iDe
37b0a 6c 74 61 29 29 20 29 7b 0a 20 20 20 20 20 20 67  lta)) ){.      g
37b0b 6f 74 6f 20 70 65 6e 64 69 6e 67 6c 69 73 74 61  oto pendinglista
37b0c 70 70 65 6e 64 5f 6f 75 74 3b 0a 20 20 20 20 7d  ppend_out;.    }
37b0d 0a 20 20 20 20 70 2d 3e 69 4c 61 73 74 43 6f 6c  .    p->iLastCol
37b0e 20 3d 20 2d 31 3b 0a 20 20 20 20 70 2d 3e 69 4c   = -1;.    p->iL
37b0f 61 73 74 50 6f 73 20 3d 20 30 3b 0a 20 20 20 20  astPos = 0;.    
37b10 70 2d 3e 69 4c 61 73 74 44 6f 63 69 64 20 3d 20  p->iLastDocid = 
37b11 69 44 6f 63 69 64 3b 0a 20 20 7d 0a 20 20 69 66  iDocid;.  }.  if
37b12 28 20 69 43 6f 6c 3e 30 20 26 26 20 70 2d 3e 69  ( iCol>0 && p->i
37b13 4c 61 73 74 43 6f 6c 21 3d 69 43 6f 6c 20 29 7b  LastCol!=iCol ){
37b14 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
37b15 4f 4b 21 3d 28 72 63 20 3d 20 66 74 73 33 50 65  OK!=(rc = fts3Pe
37b16 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e 64 56  ndingListAppendV
37b17 61 72 69 6e 74 28 26 70 2c 20 31 29 29 0a 20 20  arint(&p, 1)).  
37b18 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21     || SQLITE_OK!
37b19 3d 28 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69  =(rc = fts3Pendi
37b1a 6e 67 4c 69 73 74 41 70 70 65 6e 64 56 61 72 69  ngListAppendVari
37b1b 6e 74 28 26 70 2c 20 69 43 6f 6c 29 29 0a 20 20  nt(&p, iCol)).  
37b1c 20 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20    ){.      goto 
37b1d 70 65 6e 64 69 6e 67 6c 69 73 74 61 70 70 65 6e  pendinglistappen
37b1e 64 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20  d_out;.    }.   
37b1f 20 70 2d 3e 69 4c 61 73 74 43 6f 6c 20 3d 20 69   p->iLastCol = i
37b20 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e 69 4c 61 73  Col;.    p->iLas
37b21 74 50 6f 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  tPos = 0;.  }.  
37b22 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b 0a 20  if( iCol>=0 ){. 
37b23 20 20 20 61 73 73 65 72 74 28 20 69 50 6f 73 3e     assert( iPos>
37b24 70 2d 3e 69 4c 61 73 74 50 6f 73 20 7c 7c 20 28  p->iLastPos || (
37b25 69 50 6f 73 3d 3d 30 20 26 26 20 70 2d 3e 69 4c  iPos==0 && p->iL
37b26 61 73 74 50 6f 73 3d 3d 30 29 20 29 3b 0a 20 20  astPos==0) );.  
37b27 20 20 72 63 20 3d 20 66 74 73 33 50 65 6e 64 69    rc = fts3Pendi
37b28 6e 67 4c 69 73 74 41 70 70 65 6e 64 56 61 72 69  ngListAppendVari
37b29 6e 74 28 26 70 2c 20 32 2b 69 50 6f 73 2d 70 2d  nt(&p, 2+iPos-p-
37b2a 3e 69 4c 61 73 74 50 6f 73 29 3b 0a 20 20 20 20  >iLastPos);.    
37b2b 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
37b2c 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4c  K ){.      p->iL
37b2d 61 73 74 50 6f 73 20 3d 20 69 50 6f 73 3b 0a 20  astPos = iPos;. 
37b2e 20 20 20 7d 0a 20 20 7d 0a 0a 20 70 65 6e 64 69     }.  }.. pendi
37b2f 6e 67 6c 69 73 74 61 70 70 65 6e 64 5f 6f 75 74  nglistappend_out
37b30 3a 0a 20 20 2a 70 52 63 20 3d 20 72 63 3b 0a 20  :.  *pRc = rc;. 
37b31 20 69 66 28 20 70 21 3d 2a 70 70 20 29 7b 0a 20   if( p!=*pp ){. 
37b32 20 20 20 2a 70 70 20 3d 20 70 3b 0a 20 20 20 20     *pp = p;.    
37b33 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
37b34 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
37b35 2a 2a 20 54 6f 6b 65 6e 69 7a 65 20 74 68 65 20  ** Tokenize the 
37b36 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  nul-terminated s
37b37 74 72 69 6e 67 20 7a 54 65 78 74 20 61 6e 64 20  tring zText and 
37b38 61 64 64 20 61 6c 6c 20 74 6f 6b 65 6e 73 20 74  add all tokens t
37b39 6f 20 74 68 65 0a 2a 2a 20 70 65 6e 64 69 6e 67  o the.** pending
37b3a 2d 74 65 72 6d 73 20 68 61 73 68 2d 74 61 62 6c  -terms hash-tabl
37b3b 65 2e 20 54 68 65 20 64 6f 63 69 64 20 75 73 65  e. The docid use
37b3c 64 20 69 73 20 74 68 61 74 20 63 75 72 72 65 6e  d is that curren
37b3d 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a  tly stored in.**
37b3e 20 70 2d 3e 69 50 72 65 76 44 6f 63 69 64 2c 20   p->iPrevDocid, 
37b3f 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69  and the column i
37b40 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20 61  s specified by a
37b41 72 67 75 6d 65 6e 74 20 69 43 6f 6c 2e 0a 2a 2a  rgument iCol..**
37b42 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
37b43 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  l, SQLITE_OK is 
37b44 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
37b45 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  ise, an SQLite e
37b46 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74  rror code..*/.st
37b47 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 65 6e  atic int fts3Pen
37b48 64 69 6e 67 54 65 72 6d 73 41 64 64 28 46 74 73  dingTermsAdd(Fts
37b49 33 54 61 62 6c 65 20 2a 70 2c 20 63 6f 6e 73 74  3Table *p, const
37b4a 20 63 68 61 72 20 2a 7a 54 65 78 74 2c 20 69 6e   char *zText, in
37b4b 74 20 69 43 6f 6c 29 7b 0a 20 20 69 6e 74 20 72  t iCol){.  int r
37b4c 63 3b 0a 20 20 69 6e 74 20 69 53 74 61 72 74 3b  c;.  int iStart;
37b4d 0a 20 20 69 6e 74 20 69 45 6e 64 3b 0a 20 20 69  .  int iEnd;.  i
37b4e 6e 74 20 69 50 6f 73 3b 0a 0a 20 20 63 68 61 72  nt iPos;..  char
37b4f 20 63 6f 6e 73 74 20 2a 7a 54 6f 6b 65 6e 3b 0a   const *zToken;.
37b50 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 0a 20    int nToken;.. 
37b51 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
37b52 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  er *pTokenizer =
37b53 20 70 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a   p->pTokenizer;.
37b54 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
37b55 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
37b56 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b   *pModule = pTok
37b57 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b  enizer->pModule;
37b58 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
37b59 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 73  izer_cursor *pCs
37b5a 72 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74  r;.  int (*xNext
37b5b 29 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  )(sqlite3_tokeni
37b5c 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
37b5d 73 6f 72 2c 0a 20 20 20 20 20 20 63 6f 6e 73 74  sor,.      const
37b5e 20 63 68 61 72 2a 2a 2c 69 6e 74 2a 2c 69 6e 74   char**,int*,int
37b5f 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 0a 20  *,int*,int*);.. 
37b60 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 69   assert( pTokeni
37b61 7a 65 72 20 26 26 20 70 4d 6f 64 75 6c 65 20 29  zer && pModule )
37b62 3b 0a 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c  ;..  rc = pModul
37b63 65 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69  e->xOpen(pTokeni
37b64 7a 65 72 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20  zer, zText, -1, 
37b65 26 70 43 73 72 29 3b 0a 20 20 69 66 28 20 72 63  &pCsr);.  if( rc
37b66 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
37b67 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
37b68 7d 0a 20 20 70 43 73 72 2d 3e 70 54 6f 6b 65 6e  }.  pCsr->pToken
37b69 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65  izer = pTokenize
37b6a 72 3b 0a 0a 20 20 78 4e 65 78 74 20 3d 20 70 4d  r;..  xNext = pM
37b6b 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 3b 0a 20 20  odule->xNext;.  
37b6c 77 68 69 6c 65 28 20 53 51 4c 49 54 45 5f 4f 4b  while( SQLITE_OK
37b6d 3d 3d 72 63 0a 20 20 20 20 20 20 26 26 20 53 51  ==rc.      && SQ
37b6e 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 78  LITE_OK==(rc = x
37b6f 4e 65 78 74 28 70 43 73 72 2c 20 26 7a 54 6f 6b  Next(pCsr, &zTok
37b70 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53  en, &nToken, &iS
37b71 74 61 72 74 2c 20 26 69 45 6e 64 2c 20 26 69 50  tart, &iEnd, &iP
37b72 6f 73 29 29 0a 20 20 29 7b 0a 20 20 20 20 50 65  os)).  ){.    Pe
37b73 6e 64 69 6e 67 4c 69 73 74 20 2a 70 4c 69 73 74  ndingList *pList
37b74 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 73 69 74 69  ;..    /* Positi
37b75 6f 6e 73 20 63 61 6e 6e 6f 74 20 62 65 20 6e 65  ons cannot be ne
37b76 67 61 74 69 76 65 3b 20 77 65 20 75 73 65 20 2d  gative; we use -
37b77 31 20 61 73 20 61 20 74 65 72 6d 69 6e 61 74 6f  1 as a terminato
37b78 72 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20  r internally..  
37b79 20 20 2a 2a 20 54 6f 6b 65 6e 73 20 6d 75 73 74    ** Tokens must
37b7a 20 68 61 76 65 20 61 20 6e 6f 6e 2d 7a 65 72 6f   have a non-zero
37b7b 20 6c 65 6e 67 74 68 2e 0a 20 20 20 20 2a 2f 0a   length..    */.
37b7c 20 20 20 20 69 66 28 20 69 50 6f 73 3c 30 20 7c      if( iPos<0 |
37b7d 7c 20 21 7a 54 6f 6b 65 6e 20 7c 7c 20 6e 54 6f  | !zToken || nTo
37b7e 6b 65 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20  ken<=0 ){.      
37b7f 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
37b80 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  R;.      break;.
37b81 20 20 20 20 7d 0a 0a 20 20 20 20 70 4c 69 73 74      }..    pList
37b82 20 3d 20 28 50 65 6e 64 69 6e 67 4c 69 73 74 20   = (PendingList 
37b83 2a 29 66 74 73 33 48 61 73 68 46 69 6e 64 28 26  *)fts3HashFind(&
37b84 70 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c  p->pendingTerms,
37b85 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29   zToken, nToken)
37b86 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 20  ;.    if( pList 
37b87 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 50 65 6e  ){.      p->nPen
37b88 64 69 6e 67 44 61 74 61 20 2d 3d 20 28 70 4c 69  dingData -= (pLi
37b89 73 74 2d 3e 6e 44 61 74 61 20 2b 20 6e 54 6f 6b  st->nData + nTok
37b8a 65 6e 20 2b 20 73 69 7a 65 6f 66 28 46 74 73 33  en + sizeof(Fts3
37b8b 48 61 73 68 45 6c 65 6d 29 29 3b 0a 20 20 20 20  HashElem));.    
37b8c 7d 0a 20 20 20 20 69 66 28 20 66 74 73 33 50 65  }.    if( fts3Pe
37b8d 6e 64 69 6e 67 4c 69 73 74 41 70 70 65 6e 64 28  ndingListAppend(
37b8e 26 70 4c 69 73 74 2c 20 70 2d 3e 69 50 72 65 76  &pList, p->iPrev
37b8f 44 6f 63 69 64 2c 20 69 43 6f 6c 2c 20 69 50 6f  Docid, iCol, iPo
37b90 73 2c 20 26 72 63 29 20 29 7b 0a 20 20 20 20 20  s, &rc) ){.     
37b91 20 69 66 28 20 70 4c 69 73 74 3d 3d 66 74 73 33   if( pList==fts3
37b92 48 61 73 68 49 6e 73 65 72 74 28 26 70 2d 3e 70  HashInsert(&p->p
37b93 65 6e 64 69 6e 67 54 65 72 6d 73 2c 20 7a 54 6f  endingTerms, zTo
37b94 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20 70 4c 69  ken, nToken, pLi
37b95 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  st) ){.        /
37b96 2a 20 4d 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20  * Malloc failed 
37b97 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67 20  while inserting 
37b98 74 68 65 20 6e 65 77 20 65 6e 74 72 79 2e 20 54  the new entry. T
37b99 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 0a 20 20  his can only .  
37b9a 20 20 20 20 20 20 2a 2a 20 68 61 70 70 65 6e 20        ** happen 
37b9b 69 66 20 74 68 65 72 65 20 77 61 73 20 6e 6f 20  if there was no 
37b9c 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20 66  previous entry f
37b9d 6f 72 20 74 68 69 73 20 74 6f 6b 65 6e 2e 0a 20  or this token.. 
37b9e 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
37b9f 20 20 61 73 73 65 72 74 28 20 30 3d 3d 66 74 73    assert( 0==fts
37ba0 33 48 61 73 68 46 69 6e 64 28 26 70 2d 3e 70 65  3HashFind(&p->pe
37ba1 6e 64 69 6e 67 54 65 72 6d 73 2c 20 7a 54 6f 6b  ndingTerms, zTok
37ba2 65 6e 2c 20 6e 54 6f 6b 65 6e 29 20 29 3b 0a 20  en, nToken) );. 
37ba3 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
37ba4 72 65 65 28 70 4c 69 73 74 29 3b 0a 20 20 20 20  ree(pList);.    
37ba5 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
37ba6 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
37ba7 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
37ba8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
37ba9 20 20 20 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44      p->nPendingD
37baa 61 74 61 20 2b 3d 20 28 70 4c 69 73 74 2d 3e 6e  ata += (pList->n
37bab 44 61 74 61 20 2b 20 6e 54 6f 6b 65 6e 20 2b 20  Data + nToken + 
37bac 73 69 7a 65 6f 66 28 46 74 73 33 48 61 73 68 45  sizeof(Fts3HashE
37bad 6c 65 6d 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  lem));.    }.  }
37bae 0a 0a 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c  ..  pModule->xCl
37baf 6f 73 65 28 70 43 73 72 29 3b 0a 20 20 72 65 74  ose(pCsr);.  ret
37bb0 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  urn (rc==SQLITE_
37bb1 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  DONE ? SQLITE_OK
37bb2 20 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a   : rc);.}../* .*
37bb3 2a 20 43 61 6c 6c 69 6e 67 20 74 68 69 73 20 66  * Calling this f
37bb4 75 6e 63 74 69 6f 6e 20 69 6e 64 69 63 61 74 65  unction indicate
37bb5 73 20 74 68 61 74 20 73 75 62 73 65 71 75 65 6e  s that subsequen
37bb6 74 20 63 61 6c 6c 73 20 74 6f 20 0a 2a 2a 20 66  t calls to .** f
37bb7 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 41  ts3PendingTermsA
37bb8 64 64 28 29 20 61 72 65 20 74 6f 20 61 64 64 20  dd() are to add 
37bb9 74 65 72 6d 2f 70 6f 73 69 74 69 6f 6e 2d 6c 69  term/position-li
37bba 73 74 20 70 61 69 72 73 20 66 6f 72 20 74 68 65  st pairs for the
37bbb 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  .** contents of 
37bbc 74 68 65 20 64 6f 63 75 6d 65 6e 74 20 77 69 74  the document wit
37bbd 68 20 64 6f 63 69 64 20 69 44 6f 63 69 64 2e 0a  h docid iDocid..
37bbe 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
37bbf 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 44 6f  s3PendingTermsDo
37bc0 63 69 64 28 46 74 73 33 54 61 62 6c 65 20 2a 70  cid(Fts3Table *p
37bc1 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
37bc2 44 6f 63 69 64 29 7b 0a 20 20 2f 2a 20 54 4f 44  Docid){.  /* TOD
37bc3 4f 28 73 68 65 73 73 29 20 45 78 70 6c 6f 72 65  O(shess) Explore
37bc4 20 77 68 65 74 68 65 72 20 70 61 72 74 69 61 6c   whether partial
37bc5 6c 79 20 66 6c 75 73 68 69 6e 67 20 74 68 65 20  ly flushing the 
37bc6 62 75 66 66 65 72 20 6f 6e 0a 20 20 2a 2a 20 66  buffer on.  ** f
37bc7 6f 72 63 65 64 2d 66 6c 75 73 68 20 77 6f 75 6c  orced-flush woul
37bc8 64 20 70 72 6f 76 69 64 65 20 62 65 74 74 65 72  d provide better
37bc9 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 49   performance.  I
37bca 20 73 75 73 70 65 63 74 20 74 68 61 74 20 69 66   suspect that if
37bcb 0a 20 20 2a 2a 20 77 65 20 6f 72 64 65 72 65 64  .  ** we ordered
37bcc 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20 62 79   the doclists by
37bcd 20 73 69 7a 65 20 61 6e 64 20 66 6c 75 73 68 65   size and flushe
37bce 64 20 74 68 65 20 6c 61 72 67 65 73 74 20 75 6e  d the largest un
37bcf 74 69 6c 20 74 68 65 0a 20 20 2a 2a 20 62 75 66  til the.  ** buf
37bd0 66 65 72 20 77 61 73 20 68 61 6c 66 20 65 6d 70  fer was half emp
37bd1 74 79 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 6c  ty, that would l
37bd2 65 74 20 74 68 65 20 6c 65 73 73 20 66 72 65 71  et the less freq
37bd3 75 65 6e 74 20 74 65 72 6d 73 0a 20 20 2a 2a 20  uent terms.  ** 
37bd4 67 65 6e 65 72 61 74 65 20 6c 6f 6e 67 65 72 20  generate longer 
37bd5 64 6f 63 6c 69 73 74 73 2e 0a 20 20 2a 2f 0a 20  doclists..  */. 
37bd6 20 69 66 28 20 69 44 6f 63 69 64 3c 3d 70 2d 3e   if( iDocid<=p->
37bd7 69 50 72 65 76 44 6f 63 69 64 20 7c 7c 20 70 2d  iPrevDocid || p-
37bd8 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e 46 54  >nPendingData>FT
37bd9 53 33 5f 4d 41 58 5f 50 45 4e 44 49 4e 47 5f 44  S3_MAX_PENDING_D
37bda 41 54 41 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ATA ){.    int r
37bdb 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50  c = sqlite3Fts3P
37bdc 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c 75 73 68  endingTermsFlush
37bdd 28 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  (p);.    if( rc!
37bde 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
37bdf 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 2d  urn rc;.  }.  p-
37be0 3e 69 50 72 65 76 44 6f 63 69 64 20 3d 20 69 44  >iPrevDocid = iD
37be1 6f 63 69 64 3b 0a 20 20 72 65 74 75 72 6e 20 53  ocid;.  return S
37be2 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 53 51 4c  QLITE_OK;.}..SQL
37be3 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
37be4 20 73 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64   sqlite3Fts3Pend
37be5 69 6e 67 54 65 72 6d 73 43 6c 65 61 72 28 46 74  ingTermsClear(Ft
37be6 73 33 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 46  s3Table *p){.  F
37be7 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 45 6c  ts3HashElem *pEl
37be8 65 6d 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d  em;.  for(pElem=
37be9 66 74 73 33 48 61 73 68 46 69 72 73 74 28 26 70  fts3HashFirst(&p
37bea 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29 3b  ->pendingTerms);
37beb 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 66 74   pElem; pElem=ft
37bec 73 33 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d  s3HashNext(pElem
37bed 29 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  )){.    sqlite3_
37bee 66 72 65 65 28 66 74 73 33 48 61 73 68 44 61 74  free(fts3HashDat
37bef 61 28 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20  a(pElem));.  }. 
37bf0 20 66 74 73 33 48 61 73 68 43 6c 65 61 72 28 26   fts3HashClear(&
37bf1 70 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29  p->pendingTerms)
37bf2 3b 0a 20 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44  ;.  p->nPendingD
37bf3 61 74 61 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  ata = 0;.}../*.*
37bf4 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
37bf5 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  is called by the
37bf6 20 78 55 70 64 61 74 65 28 29 20 6d 65 74 68 6f   xUpdate() metho
37bf7 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20  d as part of an 
37bf8 49 4e 53 45 52 54 0a 2a 2a 20 6f 70 65 72 61 74  INSERT.** operat
37bf9 69 6f 6e 2e 20 49 74 20 61 64 64 73 20 65 6e 74  ion. It adds ent
37bfa 72 69 65 73 20 66 6f 72 20 65 61 63 68 20 74 65  ries for each te
37bfb 72 6d 20 69 6e 20 74 68 65 20 6e 65 77 20 72 65  rm in the new re
37bfc 63 6f 72 64 20 74 6f 20 74 68 65 0a 2a 2a 20 70  cord to the.** p
37bfd 65 6e 64 69 6e 67 54 65 72 6d 73 20 68 61 73 68  endingTerms hash
37bfe 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 72   table..**.** Ar
37bff 67 75 6d 65 6e 74 20 61 70 56 61 6c 20 69 73 20  gument apVal is 
37c00 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20  the same as the 
37c01 73 69 6d 69 6c 61 72 6c 79 20 6e 61 6d 65 64 20  similarly named 
37c02 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20  argument passed 
37c03 74 6f 0a 2a 2a 20 66 74 73 33 49 6e 73 65 72 74  to.** fts3Insert
37c04 44 61 74 61 28 29 2e 20 50 61 72 61 6d 65 74 65  Data(). Paramete
37c05 72 20 69 44 6f 63 69 64 20 69 73 20 74 68 65 20  r iDocid is the 
37c06 64 6f 63 69 64 20 6f 66 20 74 68 65 20 6e 65 77  docid of the new
37c07 20 72 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   row..*/.static 
37c08 69 6e 74 20 66 74 73 33 49 6e 73 65 72 74 54 65  int fts3InsertTe
37c09 72 6d 73 28 46 74 73 33 54 61 62 6c 65 20 2a 70  rms(Fts3Table *p
37c0a 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
37c0b 2a 2a 61 70 56 61 6c 29 7b 0a 20 20 69 6e 74 20  **apVal){.  int 
37c0c 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
37c0d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
37c0e 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
37c0f 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 32 3b 20 69   */.  for(i=2; i
37c10 3c 70 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 3b 20 69  <p->nColumn+2; i
37c11 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ++){.    const c
37c12 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 28 63 6f  har *zText = (co
37c13 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
37c14 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70  e3_value_text(ap
37c15 56 61 6c 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  Val[i]);.    if(
37c16 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20   zText ){.      
37c17 69 6e 74 20 72 63 20 3d 20 66 74 73 33 50 65 6e  int rc = fts3Pen
37c18 64 69 6e 67 54 65 72 6d 73 41 64 64 28 70 2c 20  dingTermsAdd(p, 
37c19 7a 54 65 78 74 2c 20 69 2d 32 29 3b 0a 20 20 20  zText, i-2);.   
37c1a 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
37c1b 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
37c1c 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
37c1d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
37c1e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
37c1f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
37c20 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
37c21 64 20 62 79 20 74 68 65 20 78 55 70 64 61 74 65  d by the xUpdate
37c22 28 29 20 6d 65 74 68 6f 64 20 66 6f 72 20 61 6e  () method for an
37c23 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f   INSERT operatio
37c24 6e 2e 0a 2a 2a 20 54 68 65 20 61 70 56 61 6c 20  n..** The apVal 
37c25 70 61 72 61 6d 65 74 65 72 20 69 73 20 70 61 73  parameter is pas
37c26 73 65 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68  sed a copy of th
37c27 65 20 61 70 56 61 6c 20 61 72 67 75 6d 65 6e 74  e apVal argument
37c28 20 70 61 73 73 65 64 20 62 79 0a 2a 2a 20 53 51   passed by.** SQ
37c29 4c 69 74 65 20 74 6f 20 74 68 65 20 78 55 70 64  Lite to the xUpd
37c2a 61 74 65 28 29 20 6d 65 74 68 6f 64 2e 20 69 2e  ate() method. i.
37c2b 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 70 56 61 6c  e:.**.**   apVal
37c2c 5b 30 5d 20 20 20 20 20 20 20 20 20 20 20 20 20  [0]             
37c2d 20 20 20 4e 6f 74 20 75 73 65 64 20 66 6f 72 20     Not used for 
37c2e 49 4e 53 45 52 54 2e 0a 2a 2a 20 20 20 61 70 56  INSERT..**   apV
37c2f 61 6c 5b 31 5d 20 20 20 20 20 20 20 20 20 20 20  al[1]           
37c30 20 20 20 20 20 72 6f 77 69 64 0a 2a 2a 20 20 20       rowid.**   
37c31 61 70 56 61 6c 5b 32 5d 20 20 20 20 20 20 20 20  apVal[2]        
37c32 20 20 20 20 20 20 20 20 4c 65 66 74 2d 6d 6f 73          Left-mos
37c33 74 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63  t user-defined c
37c34 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a  olumn.**   ....*
37c35 2a 20 20 20 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f  *   apVal[p->nCo
37c36 6c 75 6d 6e 2b 31 5d 20 20 20 20 20 52 69 67 68  lumn+1]     Righ
37c37 74 2d 6d 6f 73 74 20 75 73 65 72 2d 64 65 66 69  t-most user-defi
37c38 6e 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20  ned column.**   
37c39 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f 6c 75 6d 6e  apVal[p->nColumn
37c3a 2b 32 5d 20 20 20 20 20 48 69 64 64 65 6e 20 63  +2]     Hidden c
37c3b 6f 6c 75 6d 6e 20 77 69 74 68 20 73 61 6d 65 20  olumn with same 
37c3c 6e 61 6d 65 20 61 73 20 74 61 62 6c 65 0a 2a 2a  name as table.**
37c3d 20 20 20 61 70 56 61 6c 5b 70 2d 3e 6e 43 6f 6c     apVal[p->nCol
37c3e 75 6d 6e 2b 33 5d 20 20 20 20 20 48 69 64 64 65  umn+3]     Hidde
37c3f 6e 20 22 64 6f 63 69 64 22 20 63 6f 6c 75 6d 6e  n "docid" column
37c40 20 28 61 6c 69 61 73 20 66 6f 72 20 72 6f 77 69   (alias for rowi
37c41 64 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d).*/.static int
37c42 20 66 74 73 33 49 6e 73 65 72 74 44 61 74 61 28   fts3InsertData(
37c43 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c  .  Fts3Table *p,
37c44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c45 20 20 20 2f 2a 20 46 75 6c 6c 2d 74 65 78 74 20     /* Full-text 
37c46 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74  table */.  sqlit
37c47 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c  e3_value **apVal
37c48 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72  ,          /* Ar
37c49 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20 74 6f  ray of values to
37c4a 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 73 71 6c   insert */.  sql
37c4b 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 69 44 6f  ite3_int64 *piDo
37c4c 63 69 64 20 20 20 20 20 20 20 20 20 20 2f 2a 20  cid          /* 
37c4d 4f 55 54 3a 20 44 6f 63 69 64 20 66 6f 72 20 72  OUT: Docid for r
37c4e 6f 77 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64  ow just inserted
37c4f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
37c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c51 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
37c52 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 73 71 6c  rn code */.  sql
37c53 69 74 65 33 5f 73 74 6d 74 20 2a 70 43 6f 6e 74  ite3_stmt *pCont
37c54 65 6e 74 49 6e 73 65 72 74 3b 20 20 20 2f 2a 20  entInsert;   /* 
37c55 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 5f 63 6f  INSERT INTO %_co
37c56 6e 74 65 6e 74 20 56 41 4c 55 45 53 28 2e 2e 2e  ntent VALUES(...
37c57 29 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74  ) */..  /* Locat
37c58 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  e the statement 
37c59 68 61 6e 64 6c 65 20 75 73 65 64 20 74 6f 20 69  handle used to i
37c5a 6e 73 65 72 74 20 64 61 74 61 20 69 6e 74 6f 20  nsert data into 
37c5b 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 0a 20 20  the %_content.  
37c5c 2a 2a 20 74 61 62 6c 65 2e 20 54 68 65 20 53 51  ** table. The SQ
37c5d 4c 20 66 6f 72 20 74 68 69 73 20 73 74 61 74 65  L for this state
37c5e 6d 65 6e 74 20 69 73 3a 0a 20 20 2a 2a 0a 20 20  ment is:.  **.  
37c5f 2a 2a 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f  **   INSERT INTO
37c60 20 25 5f 63 6f 6e 74 65 6e 74 20 56 41 4c 55 45   %_content VALUE
37c61 53 28 3f 2c 20 3f 2c 20 3f 2c 20 2e 2e 2e 29 0a  S(?, ?, ?, ...).
37c62 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74    **.  ** The st
37c63 61 74 65 6d 65 6e 74 20 66 65 61 74 75 72 65 73  atement features
37c64 20 4e 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73   N '?' variables
37c65 2c 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  , where N is the
37c66 20 6e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 0a   number of user.
37c67 20 20 2a 2a 20 64 65 66 69 6e 65 64 20 63 6f 6c    ** defined col
37c68 75 6d 6e 73 20 69 6e 20 74 68 65 20 46 54 53 33  umns in the FTS3
37c69 20 74 61 62 6c 65 2c 20 70 6c 75 73 20 6f 6e 65   table, plus one
37c6a 20 66 6f 72 20 74 68 65 20 64 6f 63 69 64 20 66   for the docid f
37c6b 69 65 6c 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  ield..  */.  rc 
37c6c 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c  = fts3SqlStmt(p,
37c6d 20 53 51 4c 5f 43 4f 4e 54 45 4e 54 5f 49 4e 53   SQL_CONTENT_INS
37c6e 45 52 54 2c 20 26 70 43 6f 6e 74 65 6e 74 49 6e  ERT, &pContentIn
37c6f 73 65 72 74 2c 20 26 61 70 56 61 6c 5b 31 5d 29  sert, &apVal[1])
37c70 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
37c71 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
37c72 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
37c73 2a 20 54 68 65 72 65 20 69 73 20 61 20 71 75 69  * There is a qui
37c74 72 6b 20 68 65 72 65 2e 20 54 68 65 20 75 73 65  rk here. The use
37c75 72 73 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  rs INSERT statem
37c76 65 6e 74 20 6d 61 79 20 68 61 76 65 20 73 70 65  ent may have spe
37c77 63 69 66 69 65 64 0a 20 20 2a 2a 20 61 20 76 61  cified.  ** a va
37c78 6c 75 65 20 66 6f 72 20 74 68 65 20 22 72 6f 77  lue for the "row
37c79 69 64 22 20 66 69 65 6c 64 2c 20 66 6f 72 20 74  id" field, for t
37c7a 68 65 20 22 64 6f 63 69 64 22 20 66 69 65 6c 64  he "docid" field
37c7b 2c 20 6f 72 20 66 6f 72 20 62 6f 74 68 2e 0a 20  , or for both.. 
37c7c 20 2a 2a 20 57 68 69 63 68 20 69 73 20 61 20 70   ** Which is a p
37c7d 72 6f 62 6c 65 6d 2c 20 73 69 6e 63 65 20 22 72  roblem, since "r
37c7e 6f 77 69 64 22 20 61 6e 64 20 22 64 6f 63 69 64  owid" and "docid
37c7f 22 20 61 72 65 20 61 6c 69 61 73 65 73 20 66 6f  " are aliases fo
37c80 72 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d 65 20  r the.  ** same 
37c81 76 61 6c 75 65 2e 20 46 6f 72 20 65 78 61 6d 70  value. For examp
37c82 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  le:.  **.  **   
37c83 49 4e 53 45 52 54 20 49 4e 54 4f 20 66 74 73 33  INSERT INTO fts3
37c84 74 62 6c 28 72 6f 77 69 64 2c 20 64 6f 63 69 64  tbl(rowid, docid
37c85 29 20 56 41 4c 55 45 53 28 31 2c 20 32 29 3b 0a  ) VALUES(1, 2);.
37c86 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 46 54 53    **.  ** In FTS
37c87 33 2c 20 74 68 69 73 20 69 73 20 61 6e 20 65 72  3, this is an er
37c88 72 6f 72 2e 20 49 74 20 69 73 20 61 6e 20 65 72  ror. It is an er
37c89 72 6f 72 20 74 6f 20 73 70 65 63 69 66 79 20 6e  ror to specify n
37c8a 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65 73 0a 20  on-NULL values. 
37c8b 20 2a 2a 20 66 6f 72 20 62 6f 74 68 20 64 6f 63   ** for both doc
37c8c 69 64 20 61 6e 64 20 73 6f 6d 65 20 6f 74 68 65  id and some othe
37c8d 72 20 72 6f 77 69 64 20 61 6c 69 61 73 2e 0a 20  r rowid alias.. 
37c8e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45   */.  if( SQLITE
37c8f 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76  _NULL!=sqlite3_v
37c90 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b  alue_type(apVal[
37c91 33 2b 70 2d 3e 6e 43 6f 6c 75 6d 6e 5d 29 20 29  3+p->nColumn]) )
37c92 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  {.    if( SQLITE
37c93 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76  _NULL==sqlite3_v
37c94 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61 6c 5b  alue_type(apVal[
37c95 30 5d 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49  0]).     && SQLI
37c96 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33  TE_NULL!=sqlite3
37c97 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 70 56 61  _value_type(apVa
37c98 6c 5b 31 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20  l[1]).    ){.   
37c99 20 20 20 2f 2a 20 41 20 72 6f 77 69 64 2f 64 6f     /* A rowid/do
37c9a 63 69 64 20 63 6f 6e 66 6c 69 63 74 2e 20 2a 2f  cid conflict. */
37c9b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
37c9c 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
37c9d 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
37c9e 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 70 43  e3_bind_value(pC
37c9f 6f 6e 74 65 6e 74 49 6e 73 65 72 74 2c 20 31 2c  ontentInsert, 1,
37ca0 20 61 70 56 61 6c 5b 33 2b 70 2d 3e 6e 43 6f 6c   apVal[3+p->nCol
37ca1 75 6d 6e 5d 29 3b 0a 20 20 20 20 69 66 28 20 72  umn]);.    if( r
37ca2 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
37ca3 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
37ca4 20 2f 2a 20 45 78 65 63 75 74 65 20 74 68 65 20   /* Execute the 
37ca5 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 69 6e 73  statement to ins
37ca6 65 72 74 20 74 68 65 20 72 65 63 6f 72 64 2e 20  ert the record. 
37ca7 53 65 74 20 2a 70 69 44 6f 63 69 64 20 74 6f 20  Set *piDocid to 
37ca8 74 68 65 20 0a 20 20 2a 2a 20 6e 65 77 20 64 6f  the .  ** new do
37ca9 63 69 64 20 76 61 6c 75 65 2e 20 0a 20 20 2a 2f  cid value. .  */
37caa 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
37cab 70 43 6f 6e 74 65 6e 74 49 6e 73 65 72 74 29 3b  pContentInsert);
37cac 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
37cad 72 65 73 65 74 28 70 43 6f 6e 74 65 6e 74 49 6e  reset(pContentIn
37cae 73 65 72 74 29 3b 0a 0a 20 20 2a 70 69 44 6f 63  sert);..  *piDoc
37caf 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73  id = sqlite3_las
37cb0 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70  t_insert_rowid(p
37cb1 2d 3e 64 62 29 3b 0a 20 20 72 65 74 75 72 6e 20  ->db);.  return 
37cb2 72 63 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 52  rc;.}..../*.** R
37cb3 65 6d 6f 76 65 20 61 6c 6c 20 64 61 74 61 20 66  emove all data f
37cb4 72 6f 6d 20 74 68 65 20 46 54 53 33 20 74 61 62  rom the FTS3 tab
37cb5 6c 65 2e 20 43 6c 65 61 72 20 74 68 65 20 68 61  le. Clear the ha
37cb6 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  sh table contain
37cb7 69 6e 67 0a 2a 2a 20 70 65 6e 64 69 6e 67 20 74  ing.** pending t
37cb8 65 72 6d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  erms..*/.static 
37cb9 69 6e 74 20 66 74 73 33 44 65 6c 65 74 65 41 6c  int fts3DeleteAl
37cba 6c 28 46 74 73 33 54 61 62 6c 65 20 2a 70 29 7b  l(Fts3Table *p){
37cbb 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20  .  int rc;      
37cbc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37cbd 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
37cbe 65 20 2a 2f 0a 0a 20 20 2f 2a 20 44 69 73 63 61  e */..  /* Disca
37cbf 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rd the contents 
37cc0 6f 66 20 74 68 65 20 70 65 6e 64 69 6e 67 2d 74  of the pending-t
37cc1 65 72 6d 73 20 68 61 73 68 20 74 61 62 6c 65 2e  erms hash table.
37cc2 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 46 74 73   */.  sqlite3Fts
37cc3 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 43 6c 65  3PendingTermsCle
37cc4 61 72 28 70 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c  ar(p);..  /* Del
37cc5 65 74 65 20 65 76 65 72 79 74 68 69 6e 67 20 66  ete everything f
37cc6 72 6f 6d 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e  rom the %_conten
37cc7 74 2c 20 25 5f 73 65 67 6d 65 6e 74 73 20 61 6e  t, %_segments an
37cc8 64 20 25 5f 73 65 67 64 69 72 20 74 61 62 6c 65  d %_segdir table
37cc9 73 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66 74 73  s. */.  rc = fts
37cca 33 53 71 6c 45 78 65 63 28 70 2c 20 53 51 4c 5f  3SqlExec(p, SQL_
37ccb 44 45 4c 45 54 45 5f 41 4c 4c 5f 43 4f 4e 54 45  DELETE_ALL_CONTE
37ccc 4e 54 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  NT, 0);.  if( rc
37ccd 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
37cce 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c 45     rc = fts3SqlE
37ccf 78 65 63 28 70 2c 20 53 51 4c 5f 44 45 4c 45 54  xec(p, SQL_DELET
37cd0 45 5f 41 4c 4c 5f 53 45 47 4d 45 4e 54 53 2c 20  E_ALL_SEGMENTS, 
37cd1 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  0);.  }.  if( rc
37cd2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
37cd3 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c 45     rc = fts3SqlE
37cd4 78 65 63 28 70 2c 20 53 51 4c 5f 44 45 4c 45 54  xec(p, SQL_DELET
37cd5 45 5f 41 4c 4c 5f 53 45 47 44 49 52 2c 20 30 29  E_ALL_SEGDIR, 0)
37cd6 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
37cd7 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  c;.}../*.** The 
37cd8 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e  first element in
37cd9 20 74 68 65 20 61 70 56 61 6c 5b 5d 20 61 72 72   the apVal[] arr
37cda 61 79 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f  ay is assumed to
37cdb 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 64 6f 63   contain the doc
37cdc 69 64 0a 2a 2a 20 28 61 6e 20 69 6e 74 65 67 65  id.** (an intege
37cdd 72 29 20 6f 66 20 61 20 72 6f 77 20 61 62 6f 75  r) of a row abou
37cde 74 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e  t to be deleted.
37cdf 20 52 65 6d 6f 76 65 20 61 6c 6c 20 74 65 72 6d   Remove all term
37ce0 73 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 66 75  s from the.** fu
37ce1 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78 2e 0a 2a  ll-text index..*
37ce2 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
37ce3 33 44 65 6c 65 74 65 54 65 72 6d 73 28 46 74 73  3DeleteTerms(Fts
37ce4 33 54 61 62 6c 65 20 2a 70 2c 20 73 71 6c 69 74  3Table *p, sqlit
37ce5 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c  e3_value **apVal
37ce6 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73  ){.  int rc;.  s
37ce7 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 65  qlite3_stmt *pSe
37ce8 6c 65 63 74 3b 0a 0a 20 20 72 63 20 3d 20 66 74  lect;..  rc = ft
37ce9 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c  s3SqlStmt(p, SQL
37cea 5f 53 45 4c 45 43 54 5f 43 4f 4e 54 45 4e 54 5f  _SELECT_CONTENT_
37ceb 42 59 5f 52 4f 57 49 44 2c 20 26 70 53 65 6c 65  BY_ROWID, &pSele
37cec 63 74 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69 66  ct, apVal);.  if
37ced 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
37cee 29 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  ){.    if( SQLIT
37cef 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
37cf0 74 65 70 28 70 53 65 6c 65 63 74 29 20 29 7b 0a  tep(pSelect) ){.
37cf1 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
37cf2 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70     for(i=1; i<=p
37cf3 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b  ->nColumn; i++){
37cf4 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  .        const c
37cf5 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 28 63 6f  har *zText = (co
37cf6 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
37cf7 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70  e3_column_text(p
37cf8 53 65 6c 65 63 74 2c 20 69 29 3b 0a 20 20 20 20  Select, i);.    
37cf9 20 20 20 20 72 63 20 3d 20 66 74 73 33 50 65 6e      rc = fts3Pen
37cfa 64 69 6e 67 54 65 72 6d 73 41 64 64 28 70 2c 20  dingTermsAdd(p, 
37cfb 7a 54 65 78 74 2c 20 2d 31 29 3b 0a 20 20 20 20  zText, -1);.    
37cfc 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
37cfd 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
37cfe 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
37cff 28 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20  (pSelect);.     
37d00 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
37d01 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
37d02 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  }.    }.    rc =
37d03 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
37d04 53 65 6c 65 63 74 29 3b 0a 20 20 7d 65 6c 73 65  Select);.  }else
37d05 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
37d06 73 65 74 28 70 53 65 6c 65 63 74 29 3b 0a 20 20  set(pSelect);.  
37d07 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
37d08 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20  ../*.** Forward 
37d09 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 6f 20 61  declaration to a
37d0a 63 63 6f 75 6e 74 20 66 6f 72 20 74 68 65 20 63  ccount for the c
37d0b 69 72 63 75 6c 61 72 20 64 65 70 65 6e 64 65 6e  ircular dependen
37d0c 63 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 66 75  cy between.** fu
37d0d 6e 63 74 69 6f 6e 73 20 66 74 73 33 53 65 67 6d  nctions fts3Segm
37d0e 65 6e 74 4d 65 72 67 65 28 29 20 61 6e 64 20 66  entMerge() and f
37d0f 74 73 33 41 6c 6c 6f 63 61 74 65 53 65 67 64 69  ts3AllocateSegdi
37d10 72 49 64 78 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  rIdx()..*/.stati
37d11 63 20 69 6e 74 20 66 74 73 33 53 65 67 6d 65 6e  c int fts3Segmen
37d12 74 4d 65 72 67 65 28 46 74 73 33 54 61 62 6c 65  tMerge(Fts3Table
37d13 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 20 0a 2a   *, int);../* .*
37d14 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
37d15 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 20  allocates a new 
37d16 6c 65 76 65 6c 20 69 4c 65 76 65 6c 20 69 6e 64  level iLevel ind
37d17 65 78 20 69 6e 20 74 68 65 20 73 65 67 64 69 72  ex in the segdir
37d18 20 74 61 62 6c 65 2e 0a 2a 2a 20 55 73 75 61 6c   table..** Usual
37d19 6c 79 2c 20 69 6e 64 65 78 65 73 20 61 72 65 20  ly, indexes are 
37d1a 61 6c 6c 6f 63 61 74 65 64 20 77 69 74 68 69 6e  allocated within
37d1b 20 61 20 6c 65 76 65 6c 20 73 65 71 75 65 6e 74   a level sequent
37d1c 69 61 6c 6c 79 20 73 74 61 72 74 69 6e 67 0a 2a  ially starting.*
37d1d 2a 20 77 69 74 68 20 30 2c 20 73 6f 20 74 68 65  * with 0, so the
37d1e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 64 65 78   allocated index
37d1f 20 69 73 20 6f 6e 65 20 67 72 65 61 74 65 72 20   is one greater 
37d20 74 68 61 6e 20 74 68 65 20 76 61 6c 75 65 20 72  than the value r
37d21 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 3a 0a 2a  eturned.** by:.*
37d22 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 6d 61  *.**   SELECT ma
37d23 78 28 69 64 78 29 20 46 52 4f 4d 20 25 5f 73 65  x(idx) FROM %_se
37d24 67 64 69 72 20 57 48 45 52 45 20 6c 65 76 65 6c  gdir WHERE level
37d25 20 3d 20 3a 69 4c 65 76 65 6c 0a 2a 2a 0a 2a 2a   = :iLevel.**.**
37d26 20 48 6f 77 65 76 65 72 2c 20 69 66 20 74 68 65   However, if the
37d27 72 65 20 61 72 65 20 61 6c 72 65 61 64 79 20 46  re are already F
37d28 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 20  TS3_MERGE_COUNT 
37d29 69 6e 64 65 78 65 73 20 61 74 20 74 68 65 20 72  indexes at the r
37d2a 65 71 75 65 73 74 65 64 0a 2a 2a 20 6c 65 76 65  equested.** leve
37d2b 6c 2c 20 74 68 65 79 20 61 72 65 20 6d 65 72 67  l, they are merg
37d2c 65 64 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  ed into a single
37d2d 20 6c 65 76 65 6c 20 28 69 4c 65 76 65 6c 2b 31   level (iLevel+1
37d2e 29 20 73 65 67 6d 65 6e 74 20 61 6e 64 20 74 68  ) segment and th
37d2f 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  e .** allocated 
37d30 69 6e 64 65 78 20 69 73 20 30 2e 0a 2a 2a 0a 2a  index is 0..**.*
37d31 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
37d32 20 2a 70 69 49 64 78 20 69 73 20 73 65 74 20 74   *piIdx is set t
37d33 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  o the allocated 
37d34 69 6e 64 65 78 20 73 6c 6f 74 20 61 6e 64 20 53  index slot and S
37d35 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 72 65 74 75  QLITE_OK.** retu
37d36 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
37d37 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
37d38 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
37d39 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
37d3a 20 66 74 73 33 41 6c 6c 6f 63 61 74 65 53 65 67   fts3AllocateSeg
37d3b 64 69 72 49 64 78 28 46 74 73 33 54 61 62 6c 65  dirIdx(Fts3Table
37d3c 20 2a 70 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c   *p, int iLevel,
37d3d 20 69 6e 74 20 2a 70 69 49 64 78 29 7b 0a 20 20   int *piIdx){.  
37d3e 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
37d3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d40 2f 2a 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a  /* Return Code *
37d41 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
37d42 20 2a 70 4e 65 78 74 49 64 78 3b 20 20 20 20 20   *pNextIdx;     
37d43 20 20 20 20 2f 2a 20 51 75 65 72 79 20 66 6f 72      /* Query for
37d44 20 6e 65 78 74 20 69 64 78 20 61 74 20 6c 65 76   next idx at lev
37d45 65 6c 20 69 4c 65 76 65 6c 20 2a 2f 0a 20 20 69  el iLevel */.  i
37d46 6e 74 20 69 4e 65 78 74 20 3d 20 30 3b 20 20 20  nt iNext = 0;   
37d47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37d48 2a 20 52 65 73 75 6c 74 20 6f 66 20 71 75 65 72  * Result of quer
37d49 79 20 70 4e 65 78 74 49 64 78 20 2a 2f 0a 0a 20  y pNextIdx */.. 
37d4a 20 2f 2a 20 53 65 74 20 76 61 72 69 61 62 6c 65   /* Set variable
37d4b 20 69 4e 65 78 74 20 74 6f 20 74 68 65 20 6e 65   iNext to the ne
37d4c 78 74 20 61 76 61 69 6c 61 62 6c 65 20 73 65 67  xt available seg
37d4d 64 69 72 20 69 6e 64 65 78 20 61 74 20 6c 65 76  dir index at lev
37d4e 65 6c 20 69 4c 65 76 65 6c 2e 20 2a 2f 0a 20 20  el iLevel. */.  
37d4f 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
37d50 28 70 2c 20 53 51 4c 5f 4e 45 58 54 5f 53 45 47  (p, SQL_NEXT_SEG
37d51 4d 45 4e 54 5f 49 4e 44 45 58 2c 20 26 70 4e 65  MENT_INDEX, &pNe
37d52 78 74 49 64 78 2c 20 30 29 3b 0a 20 20 69 66 28  xtIdx, 0);.  if(
37d53 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
37d54 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69  {.    sqlite3_bi
37d55 6e 64 5f 69 6e 74 28 70 4e 65 78 74 49 64 78 2c  nd_int(pNextIdx,
37d56 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 20   1, iLevel);.   
37d57 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
37d58 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 4e  =sqlite3_step(pN
37d59 65 78 74 49 64 78 29 20 29 7b 0a 20 20 20 20 20  extIdx) ){.     
37d5a 20 69 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33   iNext = sqlite3
37d5b 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70 4e 65 78  _column_int(pNex
37d5c 74 49 64 78 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  tIdx, 0);.    }.
37d5d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
37d5e 5f 72 65 73 65 74 28 70 4e 65 78 74 49 64 78 29  _reset(pNextIdx)
37d5f 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  ;.  }..  if( rc=
37d60 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
37d61 20 20 2f 2a 20 49 66 20 69 4e 65 78 74 20 69 73    /* If iNext is
37d62 20 46 54 53 33 5f 4d 45 52 47 45 5f 43 4f 55 4e   FTS3_MERGE_COUN
37d63 54 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  T, indicating th
37d64 61 74 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 20  at level iLevel 
37d65 69 73 20 61 6c 72 65 61 64 79 0a 20 20 20 20 2a  is already.    *
37d66 2a 20 66 75 6c 6c 2c 20 6d 65 72 67 65 20 61 6c  * full, merge al
37d67 6c 20 73 65 67 6d 65 6e 74 73 20 69 6e 20 6c 65  l segments in le
37d68 76 65 6c 20 69 4c 65 76 65 6c 20 69 6e 74 6f 20  vel iLevel into 
37d69 61 20 73 69 6e 67 6c 65 20 69 4c 65 76 65 6c 2b  a single iLevel+
37d6a 31 0a 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74  1.    ** segment
37d6b 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20 28 6e   and allocate (n
37d6c 65 77 6c 79 20 66 72 65 65 64 29 20 69 6e 64 65  ewly freed) inde
37d6d 78 20 30 20 61 74 20 6c 65 76 65 6c 20 69 4c 65  x 0 at level iLe
37d6e 76 65 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a  vel. Otherwise,.
37d6f 20 20 20 20 2a 2a 20 69 66 20 69 4e 65 78 74 20      ** if iNext 
37d70 69 73 20 6c 65 73 73 20 74 68 61 6e 20 46 54 53  is less than FTS
37d71 33 5f 4d 45 52 47 45 5f 43 4f 55 4e 54 2c 20 61  3_MERGE_COUNT, a
37d72 6c 6c 6f 63 61 74 65 20 69 6e 64 65 78 20 69 4e  llocate index iN
37d73 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ext..    */.    
37d74 69 66 28 20 69 4e 65 78 74 3e 3d 46 54 53 33 5f  if( iNext>=FTS3_
37d75 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 7b 0a 20  MERGE_COUNT ){. 
37d76 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65       rc = fts3Se
37d77 67 6d 65 6e 74 4d 65 72 67 65 28 70 2c 20 69 4c  gmentMerge(p, iL
37d78 65 76 65 6c 29 3b 0a 20 20 20 20 20 20 2a 70 69  evel);.      *pi
37d79 49 64 78 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c  Idx = 0;.    }el
37d7a 73 65 7b 0a 20 20 20 20 20 20 2a 70 69 49 64 78  se{.      *piIdx
37d7b 20 3d 20 69 4e 65 78 74 3b 0a 20 20 20 20 7d 0a   = iNext;.    }.
37d7c 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
37d7d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  ;.}../*.** Move 
37d7e 74 68 65 20 69 74 65 72 61 74 6f 72 20 70 61 73  the iterator pas
37d7f 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
37d80 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
37d81 20 6e 65 78 74 20 74 65 72 6d 20 69 6e 20 74 68   next term in th
37d82 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 2e 20 49 66  e.** segment. If
37d83 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51 4c   successful, SQL
37d84 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
37d85 65 64 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  ed. If there is 
37d86 6e 6f 20 6e 65 78 74 20 74 65 72 6d 2c 0a 2a 2a  no next term,.**
37d87 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 20 4f 74   SQLITE_DONE. Ot
37d88 68 65 72 77 69 73 65 2c 20 61 6e 20 53 51 4c 69  herwise, an SQLi
37d89 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  te error code..*
37d8a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
37d8b 33 53 65 67 52 65 61 64 65 72 4e 65 78 74 28 46  3SegReaderNext(F
37d8c 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70 52  ts3SegReader *pR
37d8d 65 61 64 65 72 29 7b 0a 20 20 63 68 61 72 20 2a  eader){.  char *
37d8e 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  pNext;          
37d8f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
37d90 73 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  sor variable */.
37d91 20 20 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20    int nPrefix;  
37d92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d93 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
37d94 79 74 65 73 20 69 6e 20 74 65 72 6d 20 70 72 65  ytes in term pre
37d95 66 69 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75  fix */.  int nSu
37d96 66 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  ffix;           
37d97 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
37d98 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
37d99 65 72 6d 20 73 75 66 66 69 78 20 2a 2f 0a 0a 20  erm suffix */.. 
37d9a 20 69 66 28 20 21 70 52 65 61 64 65 72 2d 3e 61   if( !pReader->a
37d9b 44 6f 63 6c 69 73 74 20 29 7b 0a 20 20 20 20 70  Doclist ){.    p
37d9c 4e 65 78 74 20 3d 20 70 52 65 61 64 65 72 2d 3e  Next = pReader->
37d9d 61 4e 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  aNode;.  }else{.
37d9e 20 20 20 20 70 4e 65 78 74 20 3d 20 26 70 52 65      pNext = &pRe
37d9f 61 64 65 72 2d 3e 61 44 6f 63 6c 69 73 74 5b 70  ader->aDoclist[p
37da0 52 65 61 64 65 72 2d 3e 6e 44 6f 63 6c 69 73 74  Reader->nDoclist
37da1 5d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70  ];.  }..  if( !p
37da2 4e 65 78 74 20 7c 7c 20 70 4e 65 78 74 3e 3d 26  Next || pNext>=&
37da3 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 5b 70  pReader->aNode[p
37da4 52 65 61 64 65 72 2d 3e 6e 4e 6f 64 65 5d 20 29  Reader->nNode] )
37da5 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
37da6 20 20 69 66 28 20 21 70 52 65 61 64 65 72 2d 3e    if( !pReader->
37da7 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 20 20 70  pStmt ){.      p
37da8 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65 20 3d 20  Reader->aNode = 
37da9 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  0;.      return 
37daa 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
37dab 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
37dac 33 5f 73 74 65 70 28 70 52 65 61 64 65 72 2d 3e  3_step(pReader->
37dad 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20  pStmt);.    if( 
37dae 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  rc!=SQLITE_ROW )
37daf 7b 0a 20 20 20 20 20 20 70 52 65 61 64 65 72 2d  {.      pReader-
37db0 3e 61 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20  >aNode = 0;.    
37db1 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d 53 51    return (rc==SQ
37db2 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49  LITE_DONE ? SQLI
37db3 54 45 5f 4f 4b 20 3a 20 72 63 29 3b 0a 20 20 20  TE_OK : rc);.   
37db4 20 7d 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e   }.    pReader->
37db5 6e 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f  nNode = sqlite3_
37db6 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 52 65  column_bytes(pRe
37db7 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20 30 29 3b  ader->pStmt, 0);
37db8 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 61 4e  .    pReader->aN
37db9 6f 64 65 20 3d 20 28 63 68 61 72 20 2a 29 73 71  ode = (char *)sq
37dba 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
37dbb 62 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  b(pReader->pStmt
37dbc 2c 20 30 29 3b 0a 20 20 20 20 70 4e 65 78 74 20  , 0);.    pNext 
37dbd 3d 20 70 52 65 61 64 65 72 2d 3e 61 4e 6f 64 65  = pReader->aNode
37dbe 3b 0a 20 20 7d 0a 20 20 0a 20 20 70 4e 65 78 74  ;.  }.  .  pNext
37dbf 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47   += sqlite3Fts3G
37dc0 65 74 56 61 72 69 6e 74 33 32 28 70 4e 65 78 74  etVarint32(pNext
37dc1 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a 20 20 70  , &nPrefix);.  p
37dc2 4e 65 78 74 20 2b 3d 20 73 71 6c 69 74 65 33 46  Next += sqlite3F
37dc3 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
37dc4 4e 65 78 74 2c 20 26 6e 53 75 66 66 69 78 29 3b  Next, &nSuffix);
37dc5 0a 0a 20 20 69 66 28 20 6e 50 72 65 66 69 78 2b  ..  if( nPrefix+
37dc6 6e 53 75 66 66 69 78 3e 70 52 65 61 64 65 72 2d  nSuffix>pReader-
37dc7 3e 6e 54 65 72 6d 41 6c 6c 6f 63 20 29 7b 0a 20  >nTermAlloc ){. 
37dc8 20 20 20 69 6e 74 20 6e 4e 65 77 20 3d 20 28 6e     int nNew = (n
37dc9 50 72 65 66 69 78 2b 6e 53 75 66 66 69 78 29 2a  Prefix+nSuffix)*
37dca 32 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 65  2;.    char *zNe
37dcb 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  w = sqlite3_real
37dcc 6c 6f 63 28 70 52 65 61 64 65 72 2d 3e 7a 54 65  loc(pReader->zTe
37dcd 72 6d 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 69  rm, nNew);.    i
37dce 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20 20 20  f( !zNew ){.    
37dcf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
37dd0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
37dd1 20 70 52 65 61 64 65 72 2d 3e 7a 54 65 72 6d 20   pReader->zTerm 
37dd2 3d 20 7a 4e 65 77 3b 0a 20 20 20 20 70 52 65 61  = zNew;.    pRea
37dd3 64 65 72 2d 3e 6e 54 65 72 6d 41 6c 6c 6f 63 20  der->nTermAlloc 
37dd4 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 20 20 6d 65  = nNew;.  }.  me
37dd5 6d 63 70 79 28 26 70 52 65 61 64 65 72 2d 3e 7a  mcpy(&pReader->z
37dd6 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 2c 20 70  Term[nPrefix], p
37dd7 4e 65 78 74 2c 20 6e 53 75 66 66 69 78 29 3b 0a  Next, nSuffix);.
37dd8 20 20 70 52 65 61 64 65 72 2d 3e 6e 54 65 72 6d    pReader->nTerm
37dd9 20 3d 20 6e 50 72 65 66 69 78 2b 6e 53 75 66 66   = nPrefix+nSuff
37dda 69 78 3b 0a 20 20 70 4e 65 78 74 20 2b 3d 20 6e  ix;.  pNext += n
37ddb 53 75 66 66 69 78 3b 0a 20 20 70 4e 65 78 74 20  Suffix;.  pNext 
37ddc 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65  += sqlite3Fts3Ge
37ddd 74 56 61 72 69 6e 74 33 32 28 70 4e 65 78 74 2c  tVarint32(pNext,
37dde 20 26 70 52 65 61 64 65 72 2d 3e 6e 44 6f 63 6c   &pReader->nDocl
37ddf 69 73 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ist);.  assert( 
37de0 70 4e 65 78 74 3c 26 70 52 65 61 64 65 72 2d 3e  pNext<&pReader->
37de1 61 4e 6f 64 65 5b 70 52 65 61 64 65 72 2d 3e 6e  aNode[pReader->n
37de2 4e 6f 64 65 5d 20 29 3b 0a 20 20 70 52 65 61 64  Node] );.  pRead
37de3 65 72 2d 3e 61 44 6f 63 6c 69 73 74 20 3d 20 70  er->aDoclist = p
37de4 4e 65 78 74 3b 0a 20 20 70 52 65 61 64 65 72 2d  Next;.  pReader-
37de5 3e 70 4f 66 66 73 65 74 4c 69 73 74 20 3d 20 30  >pOffsetList = 0
37de6 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
37de7 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  E_OK;.}../*.** S
37de8 65 74 20 74 68 65 20 53 65 67 52 65 61 64 65 72  et the SegReader
37de9 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
37dea 20 66 69 72 73 74 20 64 6f 63 69 64 20 69 6e 20   first docid in 
37deb 74 68 65 20 64 6f 63 6c 69 73 74 20 61 73 73 6f  the doclist asso
37dec 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74  ciated.** with t
37ded 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 2e  he current term.
37dee 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
37def 66 74 73 33 53 65 67 52 65 61 64 65 72 46 69 72  fts3SegReaderFir
37df0 73 74 44 6f 63 69 64 28 46 74 73 33 53 65 67 52  stDocid(Fts3SegR
37df1 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
37df2 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 61 73 73 65  .  int n;.  asse
37df3 72 74 28 20 70 52 65 61 64 65 72 2d 3e 61 44 6f  rt( pReader->aDo
37df4 63 6c 69 73 74 20 29 3b 0a 20 20 61 73 73 65 72  clist );.  asser
37df5 74 28 20 21 70 52 65 61 64 65 72 2d 3e 70 4f 66  t( !pReader->pOf
37df6 66 73 65 74 4c 69 73 74 20 29 3b 0a 20 20 6e 20  fsetList );.  n 
37df7 3d 20 73 71 6c 69 74 65 33 46 74 73 33 47 65 74  = sqlite3Fts3Get
37df8 56 61 72 69 6e 74 28 70 52 65 61 64 65 72 2d 3e  Varint(pReader->
37df9 61 44 6f 63 6c 69 73 74 2c 20 26 70 52 65 61 64  aDoclist, &pRead
37dfa 65 72 2d 3e 69 44 6f 63 69 64 29 3b 0a 20 20 70  er->iDocid);.  p
37dfb 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c  Reader->pOffsetL
37dfc 69 73 74 20 3d 20 26 70 52 65 61 64 65 72 2d 3e  ist = &pReader->
37dfd 61 44 6f 63 6c 69 73 74 5b 6e 5d 3b 0a 7d 0a 0a  aDoclist[n];.}..
37dfe 2f 2a 0a 2a 2a 20 41 64 76 61 6e 63 65 20 74 68  /*.** Advance th
37dff 65 20 53 65 67 52 65 61 64 65 72 20 74 6f 20 70  e SegReader to p
37e00 6f 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 78 74  oint to the next
37e01 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20 64 6f   docid in the do
37e02 63 6c 69 73 74 0a 2a 2a 20 61 73 73 6f 63 69 61  clist.** associa
37e03 74 65 64 20 77 69 74 68 20 74 68 65 20 63 75 72  ted with the cur
37e04 72 65 6e 74 20 74 65 72 6d 2e 0a 2a 2a 20 0a 2a  rent term..** .*
37e05 2a 20 49 66 20 61 72 67 75 6d 65 6e 74 73 20 70  * If arguments p
37e06 70 4f 66 66 73 65 74 4c 69 73 74 20 61 6e 64 20  pOffsetList and 
37e07 70 6e 4f 66 66 73 65 74 4c 69 73 74 20 61 72 65  pnOffsetList are
37e08 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
37e09 0a 2a 2a 20 2a 70 70 4f 66 66 73 65 74 4c 69 73  .** *ppOffsetLis
37e0a 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e  t is set to poin
37e0b 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 63  t to the first c
37e0c 6f 6c 75 6d 6e 2d 6f 66 66 73 65 74 20 6c 69 73  olumn-offset lis
37e0d 74 0a 2a 2a 20 69 6e 20 74 68 65 20 64 6f 63 6c  t.** in the docl
37e0e 69 73 74 20 65 6e 74 72 79 20 28 69 2e 65 2e 20  ist entry (i.e. 
37e0f 69 6d 6d 65 64 69 61 74 65 6c 79 20 70 61 73 74  immediately past
37e10 20 74 68 65 20 64 6f 63 69 64 20 76 61 72 69 6e   the docid varin
37e11 74 29 2e 0a 2a 2a 20 2a 70 6e 4f 66 66 73 65 74  t)..** *pnOffset
37e12 4c 69 73 74 20 69 73 20 73 65 74 20 74 6f 20 74  List is set to t
37e13 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
37e14 20 73 65 74 20 6f 66 20 63 6f 6c 75 6d 6e 2d 6f   set of column-o
37e15 66 66 73 65 74 0a 2a 2a 20 6c 69 73 74 73 2c 20  ffset.** lists, 
37e16 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 74 68  not including th
37e17 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72  e nul-terminator
37e18 20 62 79 74 65 2e 20 46 6f 72 20 65 78 61 6d 70   byte. For examp
37e19 6c 65 3a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le:.*/.static vo
37e1a 69 64 20 66 74 73 33 53 65 67 52 65 61 64 65 72  id fts3SegReader
37e1b 4e 65 78 74 44 6f 63 69 64 28 0a 20 20 46 74 73  NextDocid(.  Fts
37e1c 33 53 65 67 52 65 61 64 65 72 20 2a 70 52 65 61  3SegReader *pRea
37e1d 64 65 72 2c 0a 20 20 63 68 61 72 20 2a 2a 70 70  der,.  char **pp
37e1e 4f 66 66 73 65 74 4c 69 73 74 2c 0a 20 20 69 6e  OffsetList,.  in
37e1f 74 20 2a 70 6e 4f 66 66 73 65 74 4c 69 73 74 0a  t *pnOffsetList.
37e20 29 7b 0a 20 20 63 68 61 72 20 2a 70 20 3d 20 70  ){.  char *p = p
37e21 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c  Reader->pOffsetL
37e22 69 73 74 3b 0a 20 20 63 68 61 72 20 63 20 3d 20  ist;.  char c = 
37e23 30 3b 0a 0a 20 20 2f 2a 20 50 6f 69 6e 74 65 72  0;..  /* Pointer
37e24 20 70 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69   p currently poi
37e25 6e 74 73 20 61 74 20 74 68 65 20 66 69 72 73 74  nts at the first
37e26 20 62 79 74 65 20 6f 66 20 61 6e 20 6f 66 66 73   byte of an offs
37e27 65 74 20 6c 69 73 74 2e 20 54 68 65 0a 20 20 2a  et list. The.  *
37e28 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20  * following two 
37e29 6c 69 6e 65 73 20 61 64 76 61 6e 63 65 20 69 74  lines advance it
37e2a 20 74 6f 20 70 6f 69 6e 74 20 6f 6e 65 20 62 79   to point one by
37e2b 74 65 20 70 61 73 74 20 74 68 65 20 65 6e 64 20  te past the end 
37e2c 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 61 6d 65  of.  ** the same
37e2d 20 6f 66 66 73 65 74 20 6c 69 73 74 2e 0a 20 20   offset list..  
37e2e 2a 2f 0a 20 20 77 68 69 6c 65 28 20 2a 70 20 7c  */.  while( *p |
37e2f 20 63 20 29 20 63 20 3d 20 2a 70 2b 2b 20 26 20   c ) c = *p++ & 
37e30 30 78 38 30 3b 0a 20 20 70 2b 2b 3b 0a 0a 20 20  0x80;.  p++;..  
37e31 2f 2a 20 49 66 20 72 65 71 75 69 72 65 64 2c 20  /* If required, 
37e32 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 75 74  populate the out
37e33 70 75 74 20 76 61 72 69 61 62 6c 65 73 20 77 69  put variables wi
37e34 74 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  th a pointer to 
37e35 61 6e 64 20 74 68 65 0a 20 20 2a 2a 20 73 69 7a  and the.  ** siz
37e36 65 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75  e of the previou
37e37 73 20 6f 66 66 73 65 74 2d 6c 69 73 74 2e 0a 20  s offset-list.. 
37e38 20 2a 2f 0a 20 20 69 66 28 20 70 70 4f 66 66 73   */.  if( ppOffs
37e39 65 74 4c 69 73 74 20 29 7b 0a 20 20 20 20 2a 70  etList ){.    *p
37e3a 70 4f 66 66 73 65 74 4c 69 73 74 20 3d 20 70 52  pOffsetList = pR
37e3b 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69  eader->pOffsetLi
37e3c 73 74 3b 0a 20 20 20 20 2a 70 6e 4f 66 66 73 65  st;.    *pnOffse
37e3d 74 4c 69 73 74 20 3d 20 28 69 6e 74 29 28 70 20  tList = (int)(p 
37e3e 2d 20 70 52 65 61 64 65 72 2d 3e 70 4f 66 66 73  - pReader->pOffs
37e3f 65 74 4c 69 73 74 20 2d 20 31 29 3b 0a 20 20 7d  etList - 1);.  }
37e40 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
37e41 61 72 65 20 6e 6f 20 6d 6f 72 65 20 65 6e 74 72  are no more entr
37e42 69 65 73 20 69 6e 20 74 68 65 20 64 6f 63 6c 69  ies in the docli
37e43 73 74 2c 20 73 65 74 20 70 4f 66 66 73 65 74 4c  st, set pOffsetL
37e44 69 73 74 20 74 6f 0a 20 20 2a 2a 20 4e 55 4c 4c  ist to.  ** NULL
37e45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 73 65 74  . Otherwise, set
37e46 20 46 74 73 33 53 65 67 52 65 61 64 65 72 2e 69   Fts3SegReader.i
37e47 44 6f 63 69 64 20 74 6f 20 74 68 65 20 6e 65 78  Docid to the nex
37e48 74 20 64 6f 63 69 64 20 61 6e 64 0a 20 20 2a 2a  t docid and.  **
37e49 20 46 74 73 33 53 65 67 52 65 61 64 65 72 2e 70   Fts3SegReader.p
37e4a 4f 66 66 73 65 74 4c 69 73 74 20 74 6f 20 70 6f  OffsetList to po
37e4b 69 6e 74 20 74 6f 20 74 68 65 20 6e 65 78 74 20  int to the next 
37e4c 6f 66 66 73 65 74 20 6c 69 73 74 20 62 65 66 6f  offset list befo
37e4d 72 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e 69 6e  re.  ** returnin
37e4e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 3e  g..  */.  if( p>
37e4f 3d 26 70 52 65 61 64 65 72 2d 3e 61 44 6f 63 6c  =&pReader->aDocl
37e50 69 73 74 5b 70 52 65 61 64 65 72 2d 3e 6e 44 6f  ist[pReader->nDo
37e51 63 6c 69 73 74 5d 20 29 7b 0a 20 20 20 20 70 52  clist] ){.    pR
37e52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c 69  eader->pOffsetLi
37e53 73 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  st = 0;.  }else{
37e54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  .    sqlite3_int
37e55 36 34 20 69 44 65 6c 74 61 3b 0a 20 20 20 20 70  64 iDelta;.    p
37e56 52 65 61 64 65 72 2d 3e 70 4f 66 66 73 65 74 4c  Reader->pOffsetL
37e57 69 73 74 20 3d 20 70 20 2b 20 73 71 6c 69 74 65  ist = p + sqlite
37e58 33 46 74 73 33 47 65 74 56 61 72 69 6e 74 28 70  3Fts3GetVarint(p
37e59 2c 20 26 69 44 65 6c 74 61 29 3b 0a 20 20 20 20  , &iDelta);.    
37e5a 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69 64 20  pReader->iDocid 
37e5b 2b 3d 20 69 44 65 6c 74 61 3b 0a 20 20 7d 0a 7d  += iDelta;.  }.}
37e5c 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c  ../*.** Free all
37e5d 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 73 73   allocations ass
37e5e 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
37e5f 20 69 74 65 72 61 74 6f 72 20 70 61 73 73 65 64   iterator passed
37e60 20 61 73 20 74 68 65 20 0a 2a 2a 20 73 65 63 6f   as the .** seco
37e61 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  nd argument..*/.
37e62 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
37e63 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
37e64 65 67 52 65 61 64 65 72 46 72 65 65 28 46 74 73  egReaderFree(Fts
37e65 33 54 61 62 6c 65 20 2a 70 2c 20 46 74 73 33 53  3Table *p, Fts3S
37e66 65 67 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  egReader *pReade
37e67 72 29 7b 0a 20 20 69 66 28 20 70 52 65 61 64 65  r){.  if( pReade
37e68 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 52 65  r ){.    if( pRe
37e69 61 64 65 72 2d 3e 70 53 74 6d 74 20 29 7b 0a 20  ader->pStmt ){. 
37e6a 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65       /* Move the
37e6b 20 6c 65 61 66 2d 72 61 6e 67 65 20 53 45 4c 45   leaf-range SELE
37e6c 43 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  CT statement to 
37e6d 74 68 65 20 61 4c 65 61 76 65 73 53 74 6d 74 5b  the aLeavesStmt[
37e6e 5d 20 61 72 72 61 79 2c 0a 20 20 20 20 20 20 2a  ] array,.      *
37e6f 2a 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e  * so that it can
37e70 20 62 65 20 72 65 75 73 65 64 20 77 68 65 6e 20   be reused when 
37e71 72 65 71 75 69 72 65 64 20 62 79 20 61 6e 6f 74  required by anot
37e72 68 65 72 20 71 75 65 72 79 2e 0a 20 20 20 20 20  her query..     
37e73 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
37e74 28 20 70 2d 3e 6e 4c 65 61 76 65 73 53 74 6d 74  ( p->nLeavesStmt
37e75 3c 70 2d 3e 6e 4c 65 61 76 65 73 54 6f 74 61 6c  <p->nLeavesTotal
37e76 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
37e77 33 5f 72 65 73 65 74 28 70 52 65 61 64 65 72 2d  3_reset(pReader-
37e78 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 70  >pStmt);.      p
37e79 2d 3e 61 4c 65 61 76 65 73 53 74 6d 74 5b 70 2d  ->aLeavesStmt[p-
37e7a 3e 6e 4c 65 61 76 65 73 53 74 6d 74 2b 2b 5d 20  >nLeavesStmt++] 
37e7b 3d 20 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74  = pReader->pStmt
37e7c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
37e7d 74 65 33 5f 66 72 65 65 28 70 52 65 61 64 65 72  te3_free(pReader
37e7e 2d 3e 7a 54 65 72 6d 29 3b 0a 20 20 20 20 73 71  ->zTerm);.    sq
37e7f 6c 69 74 65 33 5f 66 72 65 65 28 70 52 65 61 64  lite3_free(pRead
37e80 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  er);.  }.}../*.*
37e81 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
37e82 20 53 65 67 52 65 61 64 65 72 20 6f 62 6a 65 63   SegReader objec
37e83 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
37e84 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
37e85 46 74 73 33 53 65 67 52 65 61 64 65 72 4e 65 77  Fts3SegReaderNew
37e86 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
37e87 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
37e88 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74      /* Virtual t
37e89 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  able handle */. 
37e8a 20 69 6e 74 20 69 41 67 65 2c 20 20 20 20 20 20   int iAge,      
37e8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37e8c 20 2f 2a 20 53 65 67 6d 65 6e 74 20 22 61 67 65   /* Segment "age
37e8d 22 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ". */.  sqlite3_
37e8e 69 6e 74 36 34 20 69 53 74 61 72 74 4c 65 61 66  int64 iStartLeaf
37e8f 2c 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74  ,       /* First
37e90 20 6c 65 61 66 20 74 6f 20 74 72 61 76 65 72 73   leaf to travers
37e91 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  e */.  sqlite3_i
37e92 6e 74 36 34 20 69 45 6e 64 4c 65 61 66 2c 20 20  nt64 iEndLeaf,  
37e93 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 20         /* Final 
37e94 6c 65 61 66 20 74 6f 20 74 72 61 76 65 72 73 65  leaf to traverse
37e95 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
37e96 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b 2c 20 20  t64 iEndBlock,  
37e97 20 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c 20 62        /* Final b
37e98 6c 6f 63 6b 20 6f 66 20 73 65 67 6d 65 6e 74 20  lock of segment 
37e99 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
37e9a 2a 7a 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20  *zRoot,         
37e9b 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20 63       /* Buffer c
37e9c 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20 6e  ontaining root n
37e9d 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f  ode */.  int nRo
37e9e 6f 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ot,             
37e9f 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
37ea0 20 6f 66 20 62 75 66 66 65 72 20 63 6f 6e 74 61   of buffer conta
37ea1 69 6e 69 6e 67 20 72 6f 6f 74 20 6e 6f 64 65 20  ining root node 
37ea2 2a 2f 0a 20 20 46 74 73 33 53 65 67 52 65 61 64  */.  Fts3SegRead
37ea3 65 72 20 2a 2a 70 70 52 65 61 64 65 72 20 20 20  er **ppReader   
37ea4 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 6c 6c       /* OUT: All
37ea5 6f 63 61 74 65 64 20 46 74 73 33 53 65 67 52 65  ocated Fts3SegRe
37ea6 61 64 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ader */.){.  int
37ea7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
37ea8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
37ea9 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20  Return code */. 
37eaa 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a   Fts3SegReader *
37eab 70 52 65 61 64 65 72 3b 20 20 20 20 20 20 20 20  pReader;        
37eac 20 2f 2a 20 4e 65 77 6c 79 20 61 6c 6c 6f 63 61   /* Newly alloca
37ead 74 65 64 20 53 65 67 52 65 61 64 65 72 20 6f 62  ted SegReader ob
37eae 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  ject */.  int nE
37eaf 78 74 72 61 20 3d 20 30 3b 20 20 20 20 20 20 20  xtra = 0;       
37eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
37eb1 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73  es to allocate s
37eb2 65 67 6d 65 6e 74 20 72 6f 6f 74 20 6e 6f 64 65  egment root node
37eb3 20 2a 2f 0a 0a 20 20 69 66 28 20 69 53 74 61 72   */..  if( iStar
37eb4 74 4c 65 61 66 3d 3d 30 20 29 7b 0a 20 20 20 20  tLeaf==0 ){.    
37eb5 6e 45 78 74 72 61 20 3d 20 6e 52 6f 6f 74 3b 0a  nExtra = nRoot;.
37eb6 20 20 7d 0a 0a 20 20 70 52 65 61 64 65 72 20 3d    }..  pReader =
37eb7 20 28 46 74 73 33 53 65 67 52 65 61 64 65 72 20   (Fts3SegReader 
37eb8 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
37eb9 28 73 69 7a 65 6f 66 28 46 74 73 33 53 65 67 52  (sizeof(Fts3SegR
37eba 65 61 64 65 72 29 20 2b 20 6e 45 78 74 72 61 29  eader) + nExtra)
37ebb 3b 0a 20 20 69 66 28 20 21 70 52 65 61 64 65 72  ;.  if( !pReader
37ebc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
37ebd 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
37ebe 0a 20 20 6d 65 6d 73 65 74 28 70 52 65 61 64 65  .  memset(pReade
37ebf 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73  r, 0, sizeof(Fts
37ec0 33 53 65 67 52 65 61 64 65 72 29 29 3b 0a 20 20  3SegReader));.  
37ec1 70 52 65 61 64 65 72 2d 3e 69 53 74 61 72 74 42  pReader->iStartB
37ec2 6c 6f 63 6b 20 3d 20 69 53 74 61 72 74 4c 65 61  lock = iStartLea
37ec3 66 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 49  f;.  pReader->iI
37ec4 64 78 20 3d 20 69 41 67 65 3b 0a 20 20 70 52 65  dx = iAge;.  pRe
37ec5 61 64 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 20  ader->iEndBlock 
37ec6 3d 20 69 45 6e 64 42 6c 6f 63 6b 3b 0a 0a 20 20  = iEndBlock;..  
37ec7 69 66 28 20 6e 45 78 74 72 61 20 29 7b 0a 20 20  if( nExtra ){.  
37ec8 20 20 2f 2a 20 54 68 65 20 65 6e 74 69 72 65 20    /* The entire 
37ec9 73 65 67 6d 65 6e 74 20 69 73 20 73 74 6f 72 65  segment is store
37eca 64 20 69 6e 20 74 68 65 20 72 6f 6f 74 20 6e 6f  d in the root no
37ecb 64 65 2e 20 2a 2f 0a 20 20 20 20 70 52 65 61 64  de. */.    pRead
37ecc 65 72 2d 3e 61 4e 6f 64 65 20 3d 20 28 63 68 61  er->aNode = (cha
37ecd 72 20 2a 29 26 70 52 65 61 64 65 72 5b 31 5d 3b  r *)&pReader[1];
37ece 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e 4e  .    pReader->nN
37ecf 6f 64 65 20 3d 20 6e 52 6f 6f 74 3b 0a 20 20 20  ode = nRoot;.   
37ed0 20 6d 65 6d 63 70 79 28 70 52 65 61 64 65 72 2d   memcpy(pReader-
37ed1 3e 61 4e 6f 64 65 2c 20 7a 52 6f 6f 74 2c 20 6e  >aNode, zRoot, n
37ed2 52 6f 6f 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Root);.  }else{.
37ed3 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74 65      /* If the te
37ed4 78 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74  xt of the SQL st
37ed5 61 74 65 6d 65 6e 74 20 74 6f 20 69 74 65 72 61  atement to itera
37ed6 74 65 20 74 68 72 6f 75 67 68 20 61 20 63 6f 6e  te through a con
37ed7 74 69 67 75 6f 75 73 0a 20 20 20 20 2a 2a 20 73  tiguous.    ** s
37ed8 65 74 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e  et of entries in
37ed9 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20   the %_segments 
37eda 74 61 62 6c 65 20 68 61 73 20 6e 6f 74 20 79 65  table has not ye
37edb 74 20 62 65 65 6e 20 63 6f 6d 70 6f 73 65 64 2c  t been composed,
37edc 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 6f 73 65 20  .    ** compose 
37edd 69 74 20 6e 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20  it now..    */. 
37ede 20 20 20 69 66 28 20 21 70 2d 3e 7a 53 65 6c 65     if( !p->zSele
37edf 63 74 4c 65 61 76 65 73 20 29 7b 0a 20 20 20 20  ctLeaves ){.    
37ee0 20 20 70 2d 3e 7a 53 65 6c 65 63 74 4c 65 61 76    p->zSelectLeav
37ee1 65 73 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  es = sqlite3_mpr
37ee2 69 6e 74 66 28 0a 20 20 20 20 20 20 20 20 20 20  intf(.          
37ee3 22 53 45 4c 45 43 54 20 62 6c 6f 63 6b 20 46 52  "SELECT block FR
37ee4 4f 4d 20 25 51 2e 27 25 71 5f 73 65 67 6d 65 6e  OM %Q.'%q_segmen
37ee5 74 73 27 20 57 48 45 52 45 20 62 6c 6f 63 6b 69  ts' WHERE blocki
37ee6 64 20 42 45 54 57 45 45 4e 20 3f 20 41 4e 44 20  d BETWEEN ? AND 
37ee7 3f 20 22 0a 20 20 20 20 20 20 20 20 20 20 22 4f  ? ".          "O
37ee8 52 44 45 52 20 42 59 20 62 6c 6f 63 6b 69 64 22  RDER BY blockid"
37ee9 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e 7a 4e 61  , p->zDb, p->zNa
37eea 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  me.      );.    
37eeb 20 20 69 66 28 20 21 70 2d 3e 7a 53 65 6c 65 63    if( !p->zSelec
37eec 74 4c 65 61 76 65 73 20 29 7b 0a 20 20 20 20 20  tLeaves ){.     
37eed 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
37eee 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f  OMEM;.        go
37eef 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 20  to finished;.   
37ef0 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
37ef1 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
37ef2 6e 6f 20 66 72 65 65 20 73 74 61 74 65 6d 65 6e  no free statemen
37ef3 74 73 20 69 6e 20 74 68 65 20 61 4c 65 61 76 65  ts in the aLeave
37ef4 73 53 74 6d 74 5b 5d 20 61 72 72 61 79 2c 20 70  sStmt[] array, p
37ef5 72 65 70 61 72 65 0a 20 20 20 20 2a 2a 20 61 20  repare.    ** a 
37ef6 6e 65 77 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f  new statement no
37ef7 77 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65  w. Otherwise, re
37ef8 75 73 65 20 61 20 70 72 65 70 61 72 65 64 20 73  use a prepared s
37ef9 74 61 74 65 6d 65 6e 74 20 66 72 6f 6d 0a 20 20  tatement from.  
37efa 20 20 2a 2a 20 61 4c 65 61 76 65 73 53 74 6d 74    ** aLeavesStmt
37efb 5b 5d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  []..    */.    i
37efc 66 28 20 70 2d 3e 6e 4c 65 61 76 65 73 53 74 6d  f( p->nLeavesStm
37efd 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  t==0 ){.      if
37efe 28 20 70 2d 3e 6e 4c 65 61 76 65 73 54 6f 74 61  ( p->nLeavesTota
37eff 6c 3d 3d 70 2d 3e 6e 4c 65 61 76 65 73 41 6c 6c  l==p->nLeavesAll
37f00 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  oc ){.        in
37f01 74 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 4c 65 61  t nNew = p->nLea
37f02 76 65 73 41 6c 6c 6f 63 20 2b 20 31 36 3b 0a 20  vesAlloc + 16;. 
37f03 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
37f04 74 6d 74 20 2a 2a 61 4e 65 77 20 3d 20 28 73 71  tmt **aNew = (sq
37f05 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 29 73 71  lite3_stmt **)sq
37f06 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 0a 20  lite3_realloc(. 
37f07 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4c             p->aL
37f08 65 61 76 65 73 53 74 6d 74 2c 20 6e 4e 65 77 2a  eavesStmt, nNew*
37f09 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 73  sizeof(sqlite3_s
37f0a 74 6d 74 20 2a 29 0a 20 20 20 20 20 20 20 20 29  tmt *).        )
37f0b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 61  ;.        if( !a
37f0c 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20  New ){.         
37f0d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
37f0e 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  EM;.          go
37f0f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 20  to finished;.   
37f10 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
37f11 2d 3e 6e 4c 65 61 76 65 73 41 6c 6c 6f 63 20 3d  ->nLeavesAlloc =
37f12 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70   nNew;.        p
37f13 2d 3e 61 4c 65 61 76 65 73 53 74 6d 74 20 3d 20  ->aLeavesStmt = 
37f14 61 4e 65 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20  aNew;.      }.  
37f15 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
37f16 5f 70 72 65 70 61 72 65 5f 76 32 28 70 2d 3e 64  _prepare_v2(p->d
37f17 62 2c 20 70 2d 3e 7a 53 65 6c 65 63 74 4c 65 61  b, p->zSelectLea
37f18 76 65 73 2c 20 2d 31 2c 20 26 70 52 65 61 64 65  ves, -1, &pReade
37f19 72 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20  r->pStmt, 0);.  
37f1a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
37f1b 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
37f1c 20 67 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a   goto finished;.
37f1d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d        }.      p-
37f1e 3e 6e 4c 65 61 76 65 73 54 6f 74 61 6c 2b 2b 3b  >nLeavesTotal++;
37f1f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
37f20 20 20 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74    pReader->pStmt
37f21 20 3d 20 70 2d 3e 61 4c 65 61 76 65 73 53 74 6d   = p->aLeavesStm
37f22 74 5b 2d 2d 70 2d 3e 6e 4c 65 61 76 65 73 53 74  t[--p->nLeavesSt
37f23 6d 74 5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  mt];.    }..    
37f24 2f 2a 20 42 69 6e 64 20 74 68 65 20 73 74 61 72  /* Bind the star
37f25 74 20 61 6e 64 20 65 6e 64 20 6c 65 61 66 20 62  t and end leaf b
37f26 6c 6f 63 6b 69 64 73 20 74 6f 20 74 68 65 20 70  lockids to the p
37f27 72 65 70 61 72 65 64 20 53 51 4c 20 73 74 61 74  repared SQL stat
37f28 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 73 71  ement. */.    sq
37f29 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
37f2a 28 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c  (pReader->pStmt,
37f2b 20 31 2c 20 69 53 74 61 72 74 4c 65 61 66 29 3b   1, iStartLeaf);
37f2c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  .    sqlite3_bin
37f2d 64 5f 69 6e 74 36 34 28 70 52 65 61 64 65 72 2d  d_int64(pReader-
37f2e 3e 70 53 74 6d 74 2c 20 32 2c 20 69 45 6e 64 4c  >pStmt, 2, iEndL
37f2f 65 61 66 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  eaf);.  }.  rc =
37f30 20 66 74 73 33 53 65 67 52 65 61 64 65 72 4e 65   fts3SegReaderNe
37f31 78 74 28 70 52 65 61 64 65 72 29 3b 0a 0a 20 66  xt(pReader);.. f
37f32 69 6e 69 73 68 65 64 3a 0a 20 20 69 66 28 20 72  inished:.  if( r
37f33 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
37f34 20 20 20 20 2a 70 70 52 65 61 64 65 72 20 3d 20      *ppReader = 
37f35 70 52 65 61 64 65 72 3b 0a 20 20 7d 65 6c 73 65  pReader;.  }else
37f36 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73  {.    sqlite3Fts
37f37 33 53 65 67 52 65 61 64 65 72 46 72 65 65 28 70  3SegReaderFree(p
37f38 2c 20 70 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a  , pReader);.  }.
37f39 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
37f3a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e  ./*.** The secon
37f3b 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
37f3c 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 65  is function is e
37f3d 78 70 65 63 74 65 64 20 74 6f 20 62 65 20 61 20  xpected to be a 
37f3e 73 74 61 74 65 6d 65 6e 74 20 6f 66 0a 2a 2a 20  statement of.** 
37f3f 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20  the form:.**.** 
37f40 20 20 53 45 4c 45 43 54 20 0a 2a 2a 20 20 20 20    SELECT .**    
37f41 20 69 64 78 2c 20 20 20 20 20 20 20 20 20 20 20   idx,           
37f42 20 20 20 20 20 20 20 2d 2d 20 63 6f 6c 20 30 0a         -- col 0.
37f43 2a 2a 20 20 20 20 20 73 74 61 72 74 5f 62 6c 6f  **     start_blo
37f44 63 6b 2c 20 20 20 20 20 20 20 20 20 20 2d 2d 20  ck,          -- 
37f45 63 6f 6c 20 31 0a 2a 2a 20 20 20 20 20 6c 65 61  col 1.**     lea
37f46 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 20  ves_end_block,  
37f47 20 20 20 2d 2d 20 63 6f 6c 20 32 0a 2a 2a 20 20     -- col 2.**  
37f48 20 20 20 65 6e 64 5f 62 6c 6f 63 6b 2c 20 20 20     end_block,   
37f49 20 20 20 20 20 20 20 20 20 2d 2d 20 63 6f 6c 20           -- col 
37f4a 33 0a 2a 2a 20 20 20 20 20 72 6f 6f 74 20 20 20  3.**     root   
37f4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
37f4c 2d 20 63 6f 6c 20 34 0a 2a 2a 20 20 20 46 52 4f  - col 4.**   FRO
37f4d 4d 20 25 5f 73 65 67 64 69 72 20 2e 2e 2e 0a 2a  M %_segdir ....*
37f4e 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
37f4f 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61 6e 64  on allocates and
37f50 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 61 20 46   initializes a F
37f51 74 73 33 53 65 67 52 65 61 64 65 72 20 73 74 72  ts3SegReader str
37f52 75 63 74 75 72 65 20 74 6f 0a 2a 2a 20 69 74 65  ucture to.** ite
37f53 72 61 74 65 20 74 68 72 6f 75 67 68 20 74 68 65  rate through the
37f54 20 74 65 72 6d 73 20 73 74 6f 72 65 64 20 69 6e   terms stored in
37f55 20 74 68 65 20 73 65 67 6d 65 6e 74 20 69 64 65   the segment ide
37f56 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 0a 2a  ntified by the.*
37f57 2a 20 63 75 72 72 65 6e 74 20 72 6f 77 20 74 68  * current row th
37f58 61 74 20 70 53 74 6d 74 20 69 73 20 70 6f 69 6e  at pStmt is poin
37f59 74 69 6e 67 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20  ting to. .**.** 
37f5a 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74  If successful, t
37f5b 68 65 20 46 74 73 33 53 65 67 52 65 61 64 65 72  he Fts3SegReader
37f5c 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
37f5d 67 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 74  g to the first t
37f5e 65 72 6d 0a 2a 2a 20 69 6e 20 74 68 65 20 73 65  erm.** in the se
37f5f 67 6d 65 6e 74 20 61 6e 64 20 53 51 4c 49 54 45  gment and SQLITE
37f60 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
37f61 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 53   Otherwise, an S
37f62 51 4c 69 74 65 20 65 72 72 6f 72 0a 2a 2a 20 63  QLite error.** c
37f63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
37f64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
37f65 74 73 33 53 65 67 52 65 61 64 65 72 4e 65 77 28  ts3SegReaderNew(
37f66 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70 2c  .  Fts3Table *p,
37f67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37f68 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61     /* Virtual ta
37f69 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20  ble handle */.  
37f6a 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
37f6b 74 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  tmt,            
37f6c 2f 2a 20 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a  /* See above */.
37f6d 20 20 69 6e 74 20 69 41 67 65 2c 20 20 20 20 20    int iAge,     
37f6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37f6f 20 20 2f 2a 20 53 65 67 6d 65 6e 74 20 22 61 67    /* Segment "ag
37f70 65 22 2e 20 2a 2f 0a 20 20 46 74 73 33 53 65 67  e". */.  Fts3Seg
37f71 52 65 61 64 65 72 20 2a 2a 70 70 52 65 61 64 65  Reader **ppReade
37f72 72 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  r        /* OUT:
37f73 20 41 6c 6c 6f 63 61 74 65 64 20 46 74 73 33 53   Allocated Fts3S
37f74 65 67 52 65 61 64 65 72 20 2a 2f 0a 29 7b 0a 20  egReader */.){. 
37f75 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 46   return sqlite3F
37f76 74 73 33 53 65 67 52 65 61 64 65 72 4e 65 77 28  ts3SegReaderNew(
37f77 70 2c 20 69 41 67 65 2c 20 0a 20 20 20 20 20 20  p, iAge, .      
37f78 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
37f79 6e 74 36 34 28 70 53 74 6d 74 2c 20 31 29 2c 0a  nt64(pStmt, 1),.
37f7a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
37f7b 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 53 74 6d 74  lumn_int64(pStmt
37f7c 2c 20 32 29 2c 0a 20 20 20 20 20 20 73 71 6c 69  , 2),.      sqli
37f7d 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34  te3_column_int64
37f7e 28 70 53 74 6d 74 2c 20 33 29 2c 0a 20 20 20 20  (pStmt, 3),.    
37f7f 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
37f80 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 34 29 2c  _blob(pStmt, 4),
37f81 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  .      sqlite3_c
37f82 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 53 74 6d  olumn_bytes(pStm
37f83 74 2c 20 34 29 2c 0a 20 20 20 20 20 20 70 70 52  t, 4),.      ppR
37f84 65 61 64 65 72 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a  eader.  );.}../*
37f85 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
37f86 65 6e 74 72 69 65 73 20 70 6f 69 6e 74 65 64 20  entries pointed 
37f87 74 6f 20 62 79 20 74 77 6f 20 46 74 73 33 53 65  to by two Fts3Se
37f88 67 52 65 61 64 65 72 20 73 74 72 75 63 74 75 72  gReader structur
37f89 65 73 2e 20 0a 2a 2a 20 43 6f 6d 70 61 72 69 73  es. .** Comparis
37f8a 6f 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  on is as follows
37f8b 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 45 4f 46  :.**.**   1) EOF
37f8c 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
37f8d 20 6e 6f 74 20 45 4f 46 2e 0a 2a 2a 0a 2a 2a 20   not EOF..**.** 
37f8e 20 20 32 29 20 54 68 65 20 63 75 72 72 65 6e 74    2) The current
37f8f 20 74 65 72 6d 73 20 28 69 66 20 61 6e 79 29 20   terms (if any) 
37f90 61 72 65 20 63 6f 6d 70 61 72 65 64 20 77 69 74  are compared wit
37f91 68 20 6d 65 6d 63 6d 70 28 29 2e 20 49 66 20 6f  h memcmp(). If o
37f92 6e 65 0a 2a 2a 20 20 20 20 20 20 74 65 72 6d 20  ne.**      term 
37f93 69 73 20 61 20 70 72 65 66 69 78 20 6f 66 20 61  is a prefix of a
37f94 6e 6f 74 68 65 72 2c 20 74 68 65 20 6c 6f 6e 67  nother, the long
37f95 65 72 20 74 65 72 6d 20 69 73 20 63 6f 6e 73 69  er term is consi
37f96 64 65 72 65 64 20 74 68 65 0a 2a 2a 20 20 20 20  dered the.**    
37f97 20 20 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20    larger..**.** 
37f98 20 20 33 29 20 42 79 20 73 65 67 6d 65 6e 74 20    3) By segment 
37f99 61 67 65 2e 20 41 6e 20 6f 6c 64 65 72 20 73 65  age. An older se
37f9a 67 6d 65 6e 74 20 69 73 20 63 6f 6e 73 69 64 65  gment is conside
37f9b 72 65 64 20 6c 61 72 67 65 72 2e 0a 2a 2f 0a 73  red larger..*/.s
37f9c 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65  tatic int fts3Se
37f9d 67 52 65 61 64 65 72 43 6d 70 28 46 74 73 33 53  gReaderCmp(Fts3S
37f9e 65 67 52 65 61 64 65 72 20 2a 70 4c 68 73 2c 20  egReader *pLhs, 
37f9f 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a 70  Fts3SegReader *p
37fa0 52 68 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Rhs){.  int rc;.
37fa1 20 20 69 66 28 20 70 4c 68 73 2d 3e 61 4e 6f 64    if( pLhs->aNod
37fa2 65 20 26 26 20 70 52 68 73 2d 3e 61 4e 6f 64 65  e && pRhs->aNode
37fa3 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32 20   ){.    int rc2 
37fa4 3d 20 70 4c 68 73 2d 3e 6e 54 65 72 6d 20 2d 20  = pLhs->nTerm - 
37fa5 70 52 68 73 2d 3e 6e 54 65 72 6d 3b 0a 20 20 20  pRhs->nTerm;.   
37fa6 20 69 66 28 20 72 63 32 3c 30 20 29 7b 0a 20 20   if( rc2<0 ){.  
37fa7 20 20 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70 28      rc = memcmp(
37fa8 70 4c 68 73 2d 3e 7a 54 65 72 6d 2c 20 70 52 68  pLhs->zTerm, pRh
37fa9 73 2d 3e 7a 54 65 72 6d 2c 20 70 4c 68 73 2d 3e  s->zTerm, pLhs->
37faa 6e 54 65 72 6d 29 3b 0a 20 20 20 20 7d 65 6c 73  nTerm);.    }els
37fab 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 65  e{.      rc = me
37fac 6d 63 6d 70 28 70 4c 68 73 2d 3e 7a 54 65 72 6d  mcmp(pLhs->zTerm
37fad 2c 20 70 52 68 73 2d 3e 7a 54 65 72 6d 2c 20 70  , pRhs->zTerm, p
37fae 52 68 73 2d 3e 6e 54 65 72 6d 29 3b 0a 20 20 20  Rhs->nTerm);.   
37faf 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 30   }.    if( rc==0
37fb0 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72   ){.      rc = r
37fb1 63 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  c2;.    }.  }els
37fb2 65 7b 0a 20 20 20 20 72 63 20 3d 20 28 70 4c 68  e{.    rc = (pLh
37fb3 73 2d 3e 61 4e 6f 64 65 3d 3d 30 29 20 2d 20 28  s->aNode==0) - (
37fb4 70 52 68 73 2d 3e 61 4e 6f 64 65 3d 3d 30 29 3b  pRhs->aNode==0);
37fb5 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 30  .  }.  if( rc==0
37fb6 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 52 68   ){.    rc = pRh
37fb7 73 2d 3e 69 49 64 78 20 2d 20 70 4c 68 73 2d 3e  s->iIdx - pLhs->
37fb8 69 49 64 78 3b 0a 20 20 7d 0a 20 20 61 73 73 65  iIdx;.  }.  asse
37fb9 72 74 28 20 72 63 21 3d 30 20 29 3b 0a 20 20 72  rt( rc!=0 );.  r
37fba 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
37fbb 2a 2a 20 41 20 64 69 66 66 65 72 65 6e 74 20 63  ** A different c
37fbc 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
37fbd 6f 6e 20 66 6f 72 20 53 65 67 52 65 61 64 65 72  on for SegReader
37fbe 20 73 74 72 75 63 74 75 72 65 73 2e 20 49 6e 20   structures. In 
37fbf 74 68 69 73 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c  this.** version,
37fc0 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 20 74   it is assumed t
37fc1 68 61 74 20 65 61 63 68 20 53 65 67 52 65 61 64  hat each SegRead
37fc2 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20  er points to an 
37fc3 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 61 20 64 6f  entry in.** a do
37fc4 63 6c 69 73 74 20 66 6f 72 20 69 64 65 6e 74 69  clist for identi
37fc5 63 61 6c 20 74 65 72 6d 73 2e 20 43 6f 6d 70 61  cal terms. Compa
37fc6 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 20 61 73  rison is made as
37fc7 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
37fc8 20 20 31 29 20 45 4f 46 20 28 65 6e 64 20 6f 66    1) EOF (end of
37fc9 20 64 6f 63 6c 69 73 74 20 69 6e 20 74 68 69 73   doclist in this
37fca 20 63 61 73 65 29 20 69 73 20 67 72 65 61 74 65   case) is greate
37fcb 72 20 74 68 61 6e 20 6e 6f 74 20 45 4f 46 2e 0a  r than not EOF..
37fcc 2a 2a 0a 2a 2a 20 20 20 32 29 20 42 79 20 63 75  **.**   2) By cu
37fcd 72 72 65 6e 74 20 64 6f 63 69 64 2e 0a 2a 2a 0a  rrent docid..**.
37fce 2a 2a 20 20 20 33 29 20 42 79 20 73 65 67 6d 65  **   3) By segme
37fcf 6e 74 20 61 67 65 2e 20 41 6e 20 6f 6c 64 65 72  nt age. An older
37fd0 20 73 65 67 6d 65 6e 74 20 69 73 20 63 6f 6e 73   segment is cons
37fd1 69 64 65 72 65 64 20 6c 61 72 67 65 72 2e 0a 2a  idered larger..*
37fd2 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
37fd3 33 53 65 67 52 65 61 64 65 72 44 6f 63 6c 69 73  3SegReaderDoclis
37fd4 74 43 6d 70 28 46 74 73 33 53 65 67 52 65 61 64  tCmp(Fts3SegRead
37fd5 65 72 20 2a 70 4c 68 73 2c 20 46 74 73 33 53 65  er *pLhs, Fts3Se
37fd6 67 52 65 61 64 65 72 20 2a 70 52 68 73 29 7b 0a  gReader *pRhs){.
37fd7 20 20 69 6e 74 20 72 63 20 3d 20 28 70 4c 68 73    int rc = (pLhs
37fd8 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74 3d 3d 30  ->pOffsetList==0
37fd9 29 2d 28 70 52 68 73 2d 3e 70 4f 66 66 73 65 74  )-(pRhs->pOffset
37fda 4c 69 73 74 3d 3d 30 29 3b 0a 20 20 69 66 28 20  List==0);.  if( 
37fdb 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  rc==0 ){.    if(
37fdc 20 70 4c 68 73 2d 3e 69 44 6f 63 69 64 3d 3d 70   pLhs->iDocid==p
37fdd 52 68 73 2d 3e 69 44 6f 63 69 64 20 29 7b 0a 20  Rhs->iDocid ){. 
37fde 20 20 20 20 20 72 63 20 3d 20 70 52 68 73 2d 3e       rc = pRhs->
37fdf 69 49 64 78 20 2d 20 70 4c 68 73 2d 3e 69 49 64  iIdx - pLhs->iId
37fe0 78 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  x;.    }else{.  
37fe1 20 20 20 20 72 63 20 3d 20 28 70 4c 68 73 2d 3e      rc = (pLhs->
37fe2 69 44 6f 63 69 64 20 3e 20 70 52 68 73 2d 3e 69  iDocid > pRhs->i
37fe3 44 6f 63 69 64 29 20 3f 20 31 20 3a 20 2d 31 3b  Docid) ? 1 : -1;
37fe4 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73  .    }.  }.  ass
37fe5 65 72 74 28 20 70 4c 68 73 2d 3e 61 4e 6f 64 65  ert( pLhs->aNode
37fe6 20 26 26 20 70 52 68 73 2d 3e 61 4e 6f 64 65 20   && pRhs->aNode 
37fe7 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
37fe8 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
37fe9 20 74 68 65 20 74 65 72 6d 20 74 68 61 74 20 74   the term that t
37fea 68 65 20 46 74 73 33 53 65 67 52 65 61 64 65 72  he Fts3SegReader
37feb 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20 61   object passed a
37fec 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
37fed 6d 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74  ment.** points t
37fee 6f 20 77 69 74 68 20 74 68 65 20 74 65 72 6d 20  o with the term 
37fef 73 70 65 63 69 66 69 65 64 20 62 79 20 61 72 67  specified by arg
37ff0 75 6d 65 6e 74 73 20 7a 54 65 72 6d 20 61 6e 64  uments zTerm and
37ff1 20 6e 54 65 72 6d 2e 20 0a 2a 2a 0a 2a 2a 20 49   nTerm. .**.** I
37ff2 66 20 74 68 65 20 70 53 65 67 20 69 74 65 72 61  f the pSeg itera
37ff3 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61  tor is already a
37ff4 74 20 45 4f 46 2c 20 72 65 74 75 72 6e 20 30 2e  t EOF, return 0.
37ff5 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
37ff6 72 6e 0a 2a 2a 20 2d 76 65 20 69 66 20 74 68 65  rn.** -ve if the
37ff7 20 70 53 65 67 20 74 65 72 6d 20 69 73 20 6c 65   pSeg term is le
37ff8 73 73 20 74 68 61 6e 20 7a 54 65 72 6d 2f 6e 54  ss than zTerm/nT
37ff9 65 72 6d 2c 20 30 20 69 66 20 74 68 65 20 74 77  erm, 0 if the tw
37ffa 6f 20 74 65 72 6d 73 20 61 72 65 0a 2a 2a 20 65  o terms are.** e
37ffb 71 75 61 6c 2c 20 6f 72 20 2b 76 65 20 69 66 20  qual, or +ve if 
37ffc 74 68 65 20 70 53 65 67 20 74 65 72 6d 20 69 73  the pSeg term is
37ffd 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 54   greater than zT
37ffe 65 72 6d 2f 6e 54 65 72 6d 2e 0a 2a 2f 0a 73 74  erm/nTerm..*/.st
37fff 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65 67  atic int fts3Seg
38000 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 0a 20  ReaderTermCmp(. 
38001 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20 2a   Fts3SegReader *
38002 70 53 65 67 2c 20 20 20 20 20 20 20 20 20 20 20  pSeg,           
38003 20 2f 2a 20 53 65 67 6d 65 6e 74 20 72 65 61 64   /* Segment read
38004 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63  er object */.  c
38005 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 65 72 6d  onst char *zTerm
38006 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
38007 2a 20 54 65 72 6d 20 74 6f 20 63 6f 6d 70 61 72  * Term to compar
38008 65 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  e to */.  int nT
38009 65 72 6d 20 20 20 20 20 20 20 20 20 20 20 20 20  erm             
3800a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
3800b 65 20 6f 66 20 74 65 72 6d 20 7a 54 65 72 6d 20  e of term zTerm 
3800c 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20  in bytes */.){. 
3800d 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a 20 20   int res = 0;.  
3800e 69 66 28 20 70 53 65 67 2d 3e 61 4e 6f 64 65 20  if( pSeg->aNode 
3800f 29 7b 0a 20 20 20 20 69 66 28 20 70 53 65 67 2d  ){.    if( pSeg-
38010 3e 6e 54 65 72 6d 3e 6e 54 65 72 6d 20 29 7b 0a  >nTerm>nTerm ){.
38011 20 20 20 20 20 20 72 65 73 20 3d 20 6d 65 6d 63        res = memc
38012 6d 70 28 70 53 65 67 2d 3e 7a 54 65 72 6d 2c 20  mp(pSeg->zTerm, 
38013 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20  zTerm, nTerm);. 
38014 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
38015 72 65 73 20 3d 20 6d 65 6d 63 6d 70 28 70 53 65  res = memcmp(pSe
38016 67 2d 3e 7a 54 65 72 6d 2c 20 7a 54 65 72 6d 2c  g->zTerm, zTerm,
38017 20 70 53 65 67 2d 3e 6e 54 65 72 6d 29 3b 0a 20   pSeg->nTerm);. 
38018 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 65 73     }.    if( res
38019 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 73  ==0 ){.      res
3801a 20 3d 20 70 53 65 67 2d 3e 6e 54 65 72 6d 2d 6e   = pSeg->nTerm-n
3801b 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Term;.    }.  }.
3801c 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a    return res;.}.
3801d 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  ./*.** Argument 
3801e 61 70 53 65 67 6d 65 6e 74 20 69 73 20 61 6e 20  apSegment is an 
3801f 61 72 72 61 79 20 6f 66 20 6e 53 65 67 6d 65 6e  array of nSegmen
38020 74 20 65 6c 65 6d 65 6e 74 73 2e 20 49 74 20 69  t elements. It i
38021 73 20 6b 6e 6f 77 6e 20 74 68 61 74 0a 2a 2a 20  s known that.** 
38022 74 68 65 20 66 69 6e 61 6c 20 28 6e 53 65 67 6d  the final (nSegm
38023 65 6e 74 2d 6e 53 75 73 70 65 63 74 29 20 6d 65  ent-nSuspect) me
38024 6d 62 65 72 73 20 61 72 65 20 61 6c 72 65 61 64  mbers are alread
38025 79 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  y in sorted orde
38026 72 0a 2a 2a 20 28 61 63 63 6f 72 64 69 6e 67 20  r.** (according 
38027 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  to the compariso
38028 6e 20 66 75 6e 63 74 69 6f 6e 20 70 72 6f 76 69  n function provi
38029 64 65 64 29 2e 20 54 68 69 73 20 66 75 6e 63 74  ded). This funct
3802a 69 6f 6e 20 73 68 75 66 66 6c 65 73 0a 2a 2a 20  ion shuffles.** 
3802b 74 68 65 20 61 72 72 61 79 20 61 72 6f 75 6e 64  the array around
3802c 20 75 6e 74 69 6c 20 61 6c 6c 20 65 6e 74 72 69   until all entri
3802d 65 73 20 61 72 65 20 69 6e 20 73 6f 72 74 65 64  es are in sorted
3802e 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69   order..*/.stati
3802f 63 20 76 6f 69 64 20 66 74 73 33 53 65 67 52 65  c void fts3SegRe
38030 61 64 65 72 53 6f 72 74 28 0a 20 20 46 74 73 33  aderSort(.  Fts3
38031 53 65 67 52 65 61 64 65 72 20 2a 2a 61 70 53 65  SegReader **apSe
38032 67 6d 65 6e 74 2c 20 20 20 20 20 20 20 20 20 20  gment,          
38033 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72             /* Ar
38034 72 61 79 20 74 6f 20 73 6f 72 74 20 65 6e 74 72  ray to sort entr
38035 69 65 73 20 6f 66 20 2a 2f 0a 20 20 69 6e 74 20  ies of */.  int 
38036 6e 53 65 67 6d 65 6e 74 2c 20 20 20 20 20 20 20  nSegment,       
38037 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38038 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
38039 7a 65 20 6f 66 20 61 70 53 65 67 6d 65 6e 74 20  ze of apSegment 
3803a 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 6e  array */.  int n
3803b 53 75 73 70 65 63 74 2c 20 20 20 20 20 20 20 20  Suspect,        
3803c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3803d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 73            /* Uns
3803e 6f 72 74 65 64 20 65 6e 74 72 79 20 63 6f 75 6e  orted entry coun
3803f 74 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6d  t */.  int (*xCm
38040 70 29 28 46 74 73 33 53 65 67 52 65 61 64 65 72  p)(Fts3SegReader
38041 20 2a 2c 20 46 74 73 33 53 65 67 52 65 61 64 65   *, Fts3SegReade
38042 72 20 2a 29 20 20 2f 2a 20 43 6f 6d 70 61 72 69  r *)  /* Compari
38043 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  son function */.
38044 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
38045 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38046 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
38047 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 0a 20 20   variable */..  
38048 61 73 73 65 72 74 28 20 6e 53 75 73 70 65 63 74  assert( nSuspect
38049 3c 3d 6e 53 65 67 6d 65 6e 74 20 29 3b 0a 0a 20  <=nSegment );.. 
3804a 20 69 66 28 20 6e 53 75 73 70 65 63 74 3d 3d 6e   if( nSuspect==n
3804b 53 65 67 6d 65 6e 74 20 29 20 6e 53 75 73 70 65  Segment ) nSuspe
3804c 63 74 2d 2d 3b 0a 20 20 66 6f 72 28 69 3d 6e 53  ct--;.  for(i=nS
3804d 75 73 70 65 63 74 2d 31 3b 20 69 3e 3d 30 3b 20  uspect-1; i>=0; 
3804e 69 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b  i--){.    int j;
3804f 0a 20 20 20 20 66 6f 72 28 6a 3d 69 3b 20 6a 3c  .    for(j=i; j<
38050 28 6e 53 65 67 6d 65 6e 74 2d 31 29 3b 20 6a 2b  (nSegment-1); j+
38051 2b 29 7b 0a 20 20 20 20 20 20 46 74 73 33 53 65  +){.      Fts3Se
38052 67 52 65 61 64 65 72 20 2a 70 54 6d 70 3b 0a 20  gReader *pTmp;. 
38053 20 20 20 20 20 69 66 28 20 78 43 6d 70 28 61 70       if( xCmp(ap
38054 53 65 67 6d 65 6e 74 5b 6a 5d 2c 20 61 70 53 65  Segment[j], apSe
38055 67 6d 65 6e 74 5b 6a 2b 31 5d 29 3c 30 20 29 20  gment[j+1])<0 ) 
38056 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 70 54 6d  break;.      pTm
38057 70 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 6a 2b  p = apSegment[j+
38058 31 5d 3b 0a 20 20 20 20 20 20 61 70 53 65 67 6d  1];.      apSegm
38059 65 6e 74 5b 6a 2b 31 5d 20 3d 20 61 70 53 65 67  ent[j+1] = apSeg
3805a 6d 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 20 20 61  ment[j];.      a
3805b 70 53 65 67 6d 65 6e 74 5b 6a 5d 20 3d 20 70 54  pSegment[j] = pT
3805c 6d 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23  mp;.    }.  }..#
3805d 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
3805e 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
3805f 65 20 6c 69 73 74 20 72 65 61 6c 6c 79 20 69 73  e list really is
38060 20 73 6f 72 74 65 64 20 6e 6f 77 2e 20 2a 2f 0a   sorted now. */.
38061 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 28 6e 53    for(i=0; i<(nS
38062 75 73 70 65 63 74 2d 31 29 3b 20 69 2b 2b 29 7b  uspect-1); i++){
38063 0a 20 20 20 20 61 73 73 65 72 74 28 20 78 43 6d  .    assert( xCm
38064 70 28 61 70 53 65 67 6d 65 6e 74 5b 69 5d 2c 20  p(apSegment[i], 
38065 61 70 53 65 67 6d 65 6e 74 5b 69 2b 31 5d 29 3c  apSegment[i+1])<
38066 30 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  0 );.  }.#endif.
38067 7d 0a 0a 2f 2a 20 0a 2a 2a 20 49 6e 73 65 72 74  }../* .** Insert
38068 20 61 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74   a record into t
38069 68 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61  he %_segments ta
3806a 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ble..*/.static i
3806b 6e 74 20 66 74 73 33 57 72 69 74 65 53 65 67 6d  nt fts3WriteSegm
3806c 65 6e 74 28 0a 20 20 46 74 73 33 54 61 62 6c 65  ent(.  Fts3Table
3806d 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
3806e 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61         /* Virtua
3806f 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a  l table handle *
38070 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
38071 34 20 69 42 6c 6f 63 6b 2c 20 20 20 20 20 20 20  4 iBlock,       
38072 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20 69 64 20      /* Block id 
38073 66 6f 72 20 6e 65 77 20 62 6c 6f 63 6b 20 2a 2f  for new block */
38074 0a 20 20 63 68 61 72 20 2a 7a 2c 20 20 20 20 20  .  char *z,     
38075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38076 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
38077 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   buffer containi
38078 6e 67 20 62 6c 6f 63 6b 20 64 61 74 61 20 2a 2f  ng block data */
38079 0a 20 20 69 6e 74 20 6e 20 20 20 20 20 20 20 20  .  int n        
3807a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3807b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75     /* Size of bu
3807c 66 66 65 72 20 7a 20 69 6e 20 62 79 74 65 73 20  ffer z in bytes 
3807d 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  */.){.  sqlite3_
3807e 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69  stmt *pStmt;.  i
3807f 6e 74 20 72 63 20 3d 20 66 74 73 33 53 71 6c 53  nt rc = fts3SqlS
38080 74 6d 74 28 70 2c 20 53 51 4c 5f 49 4e 53 45 52  tmt(p, SQL_INSER
38081 54 5f 53 45 47 4d 45 4e 54 53 2c 20 26 70 53 74  T_SEGMENTS, &pSt
38082 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  mt, 0);.  if( rc
38083 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
38084 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
38085 69 6e 74 36 34 28 70 53 74 6d 74 2c 20 31 2c 20  int64(pStmt, 1, 
38086 69 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 72 63 20  iBlock);.    rc 
38087 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62  = sqlite3_bind_b
38088 6c 6f 62 28 70 53 74 6d 74 2c 20 32 2c 20 7a 2c  lob(pStmt, 2, z,
38089 20 6e 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49   n, SQLITE_STATI
3808a 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  C);.    if( rc==
3808b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3808c 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28     sqlite3_step(
3808d 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 72 63  pStmt);.      rc
3808e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
3808f 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 7d 0a 20  (pStmt);.    }. 
38090 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
38091 7d 0a 0a 2f 2a 20 0a 2a 2a 20 49 6e 73 65 72 74  }../* .** Insert
38092 20 61 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74   a record into t
38093 68 65 20 25 5f 73 65 67 64 69 72 20 74 61 62 6c  he %_segdir tabl
38094 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
38095 20 66 74 73 33 57 72 69 74 65 53 65 67 64 69 72   fts3WriteSegdir
38096 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a 70  (.  Fts3Table *p
38097 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
38098 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74      /* Virtual t
38099 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  able handle */. 
3809a 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20 20 20   int iLevel,    
3809b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3809c 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 6c   /* Value for "l
3809d 65 76 65 6c 22 20 66 69 65 6c 64 20 2a 2f 0a 20  evel" field */. 
3809e 20 69 6e 74 20 69 49 64 78 2c 20 20 20 20 20 20   int iIdx,      
3809f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
380a0 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 69   /* Value for "i
380a1 64 78 22 20 66 69 65 6c 64 20 2a 2f 0a 20 20 73  dx" field */.  s
380a2 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 53 74  qlite3_int64 iSt
380a3 61 72 74 42 6c 6f 63 6b 2c 20 20 20 20 20 20 2f  artBlock,      /
380a4 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 73 74 61  * Value for "sta
380a5 72 74 5f 62 6c 6f 63 6b 22 20 66 69 65 6c 64 20  rt_block" field 
380a6 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
380a7 36 34 20 69 4c 65 61 66 45 6e 64 42 6c 6f 63 6b  64 iLeafEndBlock
380a8 2c 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 66 6f  ,    /* Value fo
380a9 72 20 22 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c  r "leaves_end_bl
380aa 6f 63 6b 22 20 66 69 65 6c 64 20 2a 2f 0a 20 20  ock" field */.  
380ab 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 45  sqlite3_int64 iE
380ac 6e 64 42 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20  ndBlock,        
380ad 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 22 65 6e  /* Value for "en
380ae 64 5f 62 6c 6f 63 6b 22 20 66 69 65 6c 64 20 2a  d_block" field *
380af 2f 0a 20 20 63 68 61 72 20 2a 7a 52 6f 6f 74 2c  /.  char *zRoot,
380b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
380b1 20 20 20 20 2f 2a 20 42 6c 6f 62 20 76 61 6c 75      /* Blob valu
380b2 65 20 66 6f 72 20 22 72 6f 6f 74 22 20 66 69 65  e for "root" fie
380b3 6c 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f 6f  ld */.  int nRoo
380b4 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
380b5 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
380b6 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 62 75  r of bytes in bu
380b7 66 66 65 72 20 7a 52 6f 6f 74 20 2a 2f 0a 29 7b  ffer zRoot */.){
380b8 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
380b9 2a 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63  *pStmt;.  int rc
380ba 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70   = fts3SqlStmt(p
380bb 2c 20 53 51 4c 5f 49 4e 53 45 52 54 5f 53 45 47  , SQL_INSERT_SEG
380bc 44 49 52 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b  DIR, &pStmt, 0);
380bd 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
380be 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
380bf 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 70 53 74  te3_bind_int(pSt
380c0 6d 74 2c 20 31 2c 20 69 4c 65 76 65 6c 29 3b 0a  mt, 1, iLevel);.
380c1 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
380c2 5f 69 6e 74 28 70 53 74 6d 74 2c 20 32 2c 20 69  _int(pStmt, 2, i
380c3 49 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Idx);.    sqlite
380c4 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74  3_bind_int64(pSt
380c5 6d 74 2c 20 33 2c 20 69 53 74 61 72 74 42 6c 6f  mt, 3, iStartBlo
380c6 63 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ck);.    sqlite3
380c7 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d  _bind_int64(pStm
380c8 74 2c 20 34 2c 20 69 4c 65 61 66 45 6e 64 42 6c  t, 4, iLeafEndBl
380c9 6f 63 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ock);.    sqlite
380ca 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 53 74  3_bind_int64(pSt
380cb 6d 74 2c 20 35 2c 20 69 45 6e 64 42 6c 6f 63 6b  mt, 5, iEndBlock
380cc 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
380cd 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 53  te3_bind_blob(pS
380ce 74 6d 74 2c 20 36 2c 20 7a 52 6f 6f 74 2c 20 6e  tmt, 6, zRoot, n
380cf 52 6f 6f 74 2c 20 53 51 4c 49 54 45 5f 53 54 41  Root, SQLITE_STA
380d0 54 49 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63  TIC);.    if( rc
380d1 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
380d2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65       sqlite3_ste
380d3 70 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20  p(pStmt);.      
380d4 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
380d5 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 7d  et(pStmt);.    }
380d6 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
380d7 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
380d8 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  n the size of th
380d9 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 20  e common prefix 
380da 28 69 66 20 61 6e 79 29 20 73 68 61 72 65 64 20  (if any) shared 
380db 62 79 20 7a 50 72 65 76 20 61 6e 64 0a 2a 2a 20  by zPrev and.** 
380dc 7a 4e 65 78 74 2c 20 69 6e 20 62 79 74 65 73 2e  zNext, in bytes.
380dd 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 0a 2a   For example, .*
380de 2a 0a 2a 2a 20 20 20 66 74 73 33 50 72 65 66 69  *.**   fts3Prefi
380df 78 43 6f 6d 70 72 65 73 73 28 22 61 62 63 22 2c  xCompress("abc",
380e0 20 33 2c 20 22 61 62 63 64 65 66 22 2c 20 36 29   3, "abcdef", 6)
380e1 20 20 20 2f 2f 20 72 65 74 75 72 6e 73 20 33 0a     // returns 3.
380e2 2a 2a 20 20 20 66 74 73 33 50 72 65 66 69 78 43  **   fts3PrefixC
380e3 6f 6d 70 72 65 73 73 28 22 61 62 58 22 2c 20 33  ompress("abX", 3
380e4 2c 20 22 61 62 63 64 65 66 22 2c 20 36 29 20 20  , "abcdef", 6)  
380e5 20 2f 2f 20 72 65 74 75 72 6e 73 20 32 0a 2a 2a   // returns 2.**
380e6 20 20 20 66 74 73 33 50 72 65 66 69 78 43 6f 6d     fts3PrefixCom
380e7 70 72 65 73 73 28 22 61 62 58 22 2c 20 33 2c 20  press("abX", 3, 
380e8 22 58 62 63 64 65 66 22 2c 20 36 29 20 20 20 2f  "Xbcdef", 6)   /
380e9 2f 20 72 65 74 75 72 6e 73 20 30 0a 2a 2f 0a 73  / returns 0.*/.s
380ea 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 72  tatic int fts3Pr
380eb 65 66 69 78 43 6f 6d 70 72 65 73 73 28 0a 20 20  efixCompress(.  
380ec 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 65  const char *zPre
380ed 76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v,              
380ee 2f 2a 20 42 75 66 66 65 72 20 63 6f 6e 74 61 69  /* Buffer contai
380ef 6e 69 6e 67 20 70 72 65 76 69 6f 75 73 20 74 65  ning previous te
380f0 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 72 65  rm */.  int nPre
380f1 76 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v,              
380f2 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
380f3 6f 66 20 62 75 66 66 65 72 20 7a 50 72 65 76 20  of buffer zPrev 
380f4 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 63 6f  in bytes */.  co
380f5 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 78 74 2c  nst char *zNext,
380f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
380f7 20 42 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   Buffer containi
380f8 6e 67 20 6e 65 78 74 20 74 65 72 6d 20 2a 2f 0a  ng next term */.
380f9 20 20 69 6e 74 20 6e 4e 65 78 74 20 20 20 20 20    int nNext     
380fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
380fb 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66    /* Size of buf
380fc 66 65 72 20 7a 4e 65 78 74 20 69 6e 20 62 79 74  fer zNext in byt
380fd 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e  es */.){.  int n
380fe 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
380ff 45 54 45 52 28 6e 4e 65 78 74 29 3b 0a 20 20 66  ETER(nNext);.  f
38100 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 50 72 65 76 20  or(n=0; n<nPrev 
38101 26 26 20 7a 50 72 65 76 5b 6e 5d 3d 3d 7a 4e 65  && zPrev[n]==zNe
38102 78 74 5b 6e 5d 3b 20 6e 2b 2b 29 3b 0a 20 20 72  xt[n]; n++);.  r
38103 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn n;.}../*.*
38104 2a 20 41 64 64 20 74 65 72 6d 20 7a 54 65 72 6d  * Add term zTerm
38105 20 74 6f 20 74 68 65 20 53 65 67 6d 65 6e 74 4e   to the SegmentN
38106 6f 64 65 2e 20 49 74 20 69 73 20 67 75 61 72 61  ode. It is guara
38107 6e 74 65 65 64 20 74 68 61 74 20 7a 54 65 72 6d  nteed that zTerm
38108 20 69 73 20 6c 61 72 67 65 72 0a 2a 2a 20 28 61   is larger.** (a
38109 63 63 6f 72 64 69 6e 67 20 74 6f 20 6d 65 6d 63  ccording to memc
3810a 6d 70 29 20 74 68 61 6e 20 74 68 65 20 70 72 65  mp) than the pre
3810b 76 69 6f 75 73 20 74 65 72 6d 2e 0a 2a 2f 0a 73  vious term..*/.s
3810c 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 4e 6f  tatic int fts3No
3810d 64 65 41 64 64 54 65 72 6d 28 0a 20 20 46 74 73  deAddTerm(.  Fts
3810e 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
3810f 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
38110 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
38111 20 2a 2f 0a 20 20 53 65 67 6d 65 6e 74 4e 6f 64   */.  SegmentNod
38112 65 20 2a 2a 70 70 54 72 65 65 2c 20 20 20 20 20  e **ppTree,     
38113 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a        /* IN/OUT:
38114 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 68 61 6e   SegmentNode han
38115 64 6c 65 20 2a 2f 20 0a 20 20 69 6e 74 20 69 73  dle */ .  int is
38116 43 6f 70 79 54 65 72 6d 2c 20 20 20 20 20 20 20  CopyTerm,       
38117 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
38118 65 20 69 66 20 7a 54 65 72 6d 2f 6e 54 65 72 6d  e if zTerm/nTerm
38119 20 69 73 20 74 72 61 6e 73 69 65 6e 74 20 2a 2f   is transient */
3811a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3811b 54 65 72 6d 2c 20 20 20 20 20 20 20 20 20 20 20  Term,           
3811c 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
3811d 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69   buffer containi
3811e 6e 67 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74  ng term */.  int
3811f 20 6e 54 65 72 6d 20 20 20 20 20 20 20 20 20 20   nTerm          
38120 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38121 53 69 7a 65 20 6f 66 20 74 65 72 6d 20 69 6e 20  Size of term in 
38122 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 53 65  bytes */.){.  Se
38123 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 54 72 65 65  gmentNode *pTree
38124 20 3d 20 2a 70 70 54 72 65 65 3b 0a 20 20 69 6e   = *ppTree;.  in
38125 74 20 72 63 3b 0a 20 20 53 65 67 6d 65 6e 74 4e  t rc;.  SegmentN
38126 6f 64 65 20 2a 70 4e 65 77 3b 0a 0a 20 20 2f 2a  ode *pNew;..  /*
38127 20 46 69 72 73 74 20 74 72 79 20 74 6f 20 61 70   First try to ap
38128 70 65 6e 64 20 74 68 65 20 74 65 72 6d 20 74 6f  pend the term to
38129 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64   the current nod
3812a 65 2e 20 52 65 74 75 72 6e 20 65 61 72 6c 79 20  e. Return early 
3812b 69 66 20 0a 20 20 2a 2a 20 74 68 69 73 20 69 73  if .  ** this is
3812c 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a   possible..  */.
3812d 20 20 69 66 28 20 70 54 72 65 65 20 29 7b 0a 20    if( pTree ){. 
3812e 20 20 20 69 6e 74 20 6e 44 61 74 61 20 3d 20 70     int nData = p
3812f 54 72 65 65 2d 3e 6e 44 61 74 61 3b 20 20 20 20  Tree->nData;    
38130 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 7a 65   /* Current size
38131 20 6f 66 20 6e 6f 64 65 20 69 6e 20 62 79 74 65   of node in byte
38132 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 52 65  s */.    int nRe
38133 71 20 3d 20 6e 44 61 74 61 3b 20 20 20 20 20 20  q = nData;      
38134 20 20 20 20 20 20 20 2f 2a 20 52 65 71 75 69 72         /* Requir
38135 65 64 20 73 70 61 63 65 20 61 66 74 65 72 20 61  ed space after a
38136 64 64 69 6e 67 20 7a 54 65 72 6d 20 2a 2f 0a 20  dding zTerm */. 
38137 20 20 20 69 6e 74 20 6e 50 72 65 66 69 78 3b 20     int nPrefix; 
38138 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38139 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
3813a 74 65 73 20 6f 66 20 70 72 65 66 69 78 20 63 6f  tes of prefix co
3813b 6d 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20  mpression */.   
3813c 20 69 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20   int nSuffix;   
3813d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3813e 2a 20 53 75 66 66 69 78 20 6c 65 6e 67 74 68 20  * Suffix length 
3813f 2a 2f 0a 0a 20 20 20 20 6e 50 72 65 66 69 78 20  */..    nPrefix 
38140 3d 20 66 74 73 33 50 72 65 66 69 78 43 6f 6d 70  = fts3PrefixComp
38141 72 65 73 73 28 70 54 72 65 65 2d 3e 7a 54 65 72  ress(pTree->zTer
38142 6d 2c 20 70 54 72 65 65 2d 3e 6e 54 65 72 6d 2c  m, pTree->nTerm,
38143 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a   zTerm, nTerm);.
38144 20 20 20 20 6e 53 75 66 66 69 78 20 3d 20 6e 54      nSuffix = nT
38145 65 72 6d 2d 6e 50 72 65 66 69 78 3b 0a 0a 20 20  erm-nPrefix;..  
38146 20 20 6e 52 65 71 20 2b 3d 20 73 71 6c 69 74 65    nReq += sqlite
38147 33 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28 6e  3Fts3VarintLen(n
38148 50 72 65 66 69 78 29 2b 73 71 6c 69 74 65 33 46  Prefix)+sqlite3F
38149 74 73 33 56 61 72 69 6e 74 4c 65 6e 28 6e 53 75  ts3VarintLen(nSu
3814a 66 66 69 78 29 2b 6e 53 75 66 66 69 78 3b 0a 20  ffix)+nSuffix;. 
3814b 20 20 20 69 66 28 20 6e 52 65 71 3c 3d 70 2d 3e     if( nReq<=p->
3814c 6e 4e 6f 64 65 53 69 7a 65 20 7c 7c 20 21 70 54  nNodeSize || !pT
3814d 72 65 65 2d 3e 7a 54 65 72 6d 20 29 7b 0a 0a 20  ree->zTerm ){.. 
3814e 20 20 20 20 20 69 66 28 20 6e 52 65 71 3e 70 2d       if( nReq>p-
3814f 3e 6e 4e 6f 64 65 53 69 7a 65 20 29 7b 0a 20 20  >nNodeSize ){.  
38150 20 20 20 20 20 20 2f 2a 20 41 6e 20 75 6e 75 73        /* An unus
38151 75 61 6c 20 63 61 73 65 3a 20 74 68 69 73 20 69  ual case: this i
38152 73 20 74 68 65 20 66 69 72 73 74 20 74 65 72 6d  s the first term
38153 20 74 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20   to be added to 
38154 74 68 65 20 6e 6f 64 65 0a 20 20 20 20 20 20 20  the node.       
38155 20 2a 2a 20 61 6e 64 20 74 68 65 20 73 74 61 74   ** and the stat
38156 69 63 20 6e 6f 64 65 20 62 75 66 66 65 72 20 28  ic node buffer (
38157 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 20 62 79 74  p->nNodeSize byt
38158 65 73 29 20 69 73 20 6e 6f 74 20 6c 61 72 67 65  es) is not large
38159 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 6e 6f 75  .        ** enou
3815a 67 68 2e 20 55 73 65 20 61 20 73 65 70 61 72 61  gh. Use a separa
3815b 74 65 6c 79 20 6d 61 6c 6c 6f 63 65 64 20 62 75  tely malloced bu
3815c 66 66 65 72 20 69 6e 73 74 65 61 64 20 54 68 69  ffer instead Thi
3815d 73 20 77 61 73 74 65 73 0a 20 20 20 20 20 20 20  s wastes.       
3815e 20 2a 2a 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65   ** p->nNodeSize
3815f 20 62 79 74 65 73 2c 20 62 75 74 20 73 69 6e 63   bytes, but sinc
38160 65 20 74 68 69 73 20 73 63 65 6e 61 72 69 6f 20  e this scenario 
38161 6f 6e 6c 79 20 63 6f 6d 65 73 20 61 62 6f 75 74  only comes about
38162 20 77 68 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a   when.        **
38163 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
38164 6e 74 61 69 6e 20 74 77 6f 20 74 65 72 6d 73 20  ntain two terms 
38165 74 68 61 74 20 73 68 61 72 65 20 61 20 70 72 65  that share a pre
38166 66 69 78 20 6f 66 20 61 6c 6d 6f 73 74 20 32 4b  fix of almost 2K
38167 42 2c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  B, .        ** t
38168 68 69 73 20 69 73 20 6e 6f 74 20 65 78 70 65 63  his is not expec
38169 74 65 64 20 74 6f 20 62 65 20 61 20 73 65 72 69  ted to be a seri
3816a 6f 75 73 20 70 72 6f 62 6c 65 6d 2e 20 0a 20 20  ous problem. .  
3816b 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
3816c 20 61 73 73 65 72 74 28 20 70 54 72 65 65 2d 3e   assert( pTree->
3816d 61 44 61 74 61 3d 3d 28 63 68 61 72 20 2a 29 26  aData==(char *)&
3816e 70 54 72 65 65 5b 31 5d 20 29 3b 0a 20 20 20 20  pTree[1] );.    
3816f 20 20 20 20 70 54 72 65 65 2d 3e 61 44 61 74 61      pTree->aData
38170 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
38171 65 33 5f 6d 61 6c 6c 6f 63 28 6e 52 65 71 29 3b  e3_malloc(nReq);
38172 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 54  .        if( !pT
38173 72 65 65 2d 3e 61 44 61 74 61 20 29 7b 0a 20 20  ree->aData ){.  
38174 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
38175 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
38176 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 0a       }.      }..
38177 20 20 20 20 20 20 69 66 28 20 70 54 72 65 65 2d        if( pTree-
38178 3e 7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20  >zTerm ){.      
38179 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f    /* There is no
3817a 20 70 72 65 66 69 78 2d 6c 65 6e 67 74 68 20 66   prefix-length f
3817b 69 65 6c 64 20 66 6f 72 20 66 69 72 73 74 20 74  ield for first t
3817c 65 72 6d 20 69 6e 20 61 20 6e 6f 64 65 20 2a 2f  erm in a node */
3817d 0a 20 20 20 20 20 20 20 20 6e 44 61 74 61 20 2b  .        nData +
3817e 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75 74  = sqlite3Fts3Put
3817f 56 61 72 69 6e 74 28 26 70 54 72 65 65 2d 3e 61  Varint(&pTree->a
38180 44 61 74 61 5b 6e 44 61 74 61 5d 2c 20 6e 50 72  Data[nData], nPr
38181 65 66 69 78 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  efix);.      }..
38182 20 20 20 20 20 20 6e 44 61 74 61 20 2b 3d 20 73        nData += s
38183 71 6c 69 74 65 33 46 74 73 33 50 75 74 56 61 72  qlite3Fts3PutVar
38184 69 6e 74 28 26 70 54 72 65 65 2d 3e 61 44 61 74  int(&pTree->aDat
38185 61 5b 6e 44 61 74 61 5d 2c 20 6e 53 75 66 66 69  a[nData], nSuffi
38186 78 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  x);.      memcpy
38187 28 26 70 54 72 65 65 2d 3e 61 44 61 74 61 5b 6e  (&pTree->aData[n
38188 44 61 74 61 5d 2c 20 26 7a 54 65 72 6d 5b 6e 50  Data], &zTerm[nP
38189 72 65 66 69 78 5d 2c 20 6e 53 75 66 66 69 78 29  refix], nSuffix)
3818a 3b 0a 20 20 20 20 20 20 70 54 72 65 65 2d 3e 6e  ;.      pTree->n
3818b 44 61 74 61 20 3d 20 6e 44 61 74 61 20 2b 20 6e  Data = nData + n
3818c 53 75 66 66 69 78 3b 0a 20 20 20 20 20 20 70 54  Suffix;.      pT
3818d 72 65 65 2d 3e 6e 45 6e 74 72 79 2b 2b 3b 0a 0a  ree->nEntry++;..
3818e 20 20 20 20 20 20 69 66 28 20 69 73 43 6f 70 79        if( isCopy
3818f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  Term ){.        
38190 69 66 28 20 70 54 72 65 65 2d 3e 6e 4d 61 6c 6c  if( pTree->nMall
38191 6f 63 3c 6e 54 65 72 6d 20 29 7b 0a 20 20 20 20  oc<nTerm ){.    
38192 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77        char *zNew
38193 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
38194 6f 63 28 70 54 72 65 65 2d 3e 7a 4d 61 6c 6c 6f  oc(pTree->zMallo
38195 63 2c 20 6e 54 65 72 6d 2a 32 29 3b 0a 20 20 20  c, nTerm*2);.   
38196 20 20 20 20 20 20 20 69 66 28 20 21 7a 4e 65 77         if( !zNew
38197 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
38198 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
38199 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  MEM;.          }
3819a 0a 20 20 20 20 20 20 20 20 20 20 70 54 72 65 65  .          pTree
3819b 2d 3e 6e 4d 61 6c 6c 6f 63 20 3d 20 6e 54 65 72  ->nMalloc = nTer
3819c 6d 2a 32 3b 0a 20 20 20 20 20 20 20 20 20 20 70  m*2;.          p
3819d 54 72 65 65 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  Tree->zMalloc = 
3819e 7a 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 7d 0a  zNew;.        }.
3819f 20 20 20 20 20 20 20 20 70 54 72 65 65 2d 3e 7a          pTree->z
381a0 54 65 72 6d 20 3d 20 70 54 72 65 65 2d 3e 7a 4d  Term = pTree->zM
381a1 61 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 20 20 6d  alloc;.        m
381a2 65 6d 63 70 79 28 70 54 72 65 65 2d 3e 7a 54 65  emcpy(pTree->zTe
381a3 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d  rm, zTerm, nTerm
381a4 29 3b 0a 20 20 20 20 20 20 20 20 70 54 72 65 65  );.        pTree
381a5 2d 3e 6e 54 65 72 6d 20 3d 20 6e 54 65 72 6d 3b  ->nTerm = nTerm;
381a6 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
381a7 20 20 20 20 20 20 70 54 72 65 65 2d 3e 7a 54 65        pTree->zTe
381a8 72 6d 20 3d 20 28 63 68 61 72 20 2a 29 7a 54 65  rm = (char *)zTe
381a9 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 54 72 65  rm;.        pTre
381aa 65 2d 3e 6e 54 65 72 6d 20 3d 20 6e 54 65 72 6d  e->nTerm = nTerm
381ab 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
381ac 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
381ad 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
381ae 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66 6c 6f  * If control flo
381af 77 73 20 74 6f 20 68 65 72 65 2c 20 69 74 20 77  ws to here, it w
381b0 61 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  as not possible 
381b1 74 6f 20 61 70 70 65 6e 64 20 7a 54 65 72 6d 20  to append zTerm 
381b2 74 6f 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 72  to the.  ** curr
381b3 65 6e 74 20 6e 6f 64 65 2e 20 43 72 65 61 74 65  ent node. Create
381b4 20 61 20 6e 65 77 20 6e 6f 64 65 20 28 61 20 72   a new node (a r
381b5 69 67 68 74 2d 73 69 62 6c 69 6e 67 20 6f 66 20  ight-sibling of 
381b6 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f 64 65  the current node
381b7 29 2e 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20  )..  ** If this 
381b8 69 73 20 74 68 65 20 66 69 72 73 74 20 6e 6f 64  is the first nod
381b9 65 20 69 6e 20 74 68 65 20 74 72 65 65 2c 20 74  e in the tree, t
381ba 68 65 20 74 65 72 6d 20 69 73 20 61 64 64 65 64  he term is added
381bb 20 74 6f 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a   to it..  **.  *
381bc 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65  * Otherwise, the
381bd 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 61 64 64   term is not add
381be 65 64 20 74 6f 20 74 68 65 20 6e 65 77 20 6e 6f  ed to the new no
381bf 64 65 2c 20 69 74 20 69 73 20 6c 65 66 74 20 65  de, it is left e
381c0 6d 70 74 79 20 66 6f 72 0a 20 20 2a 2a 20 6e 6f  mpty for.  ** no
381c1 77 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20  w. Instead, the 
381c2 74 65 72 6d 20 69 73 20 69 6e 73 65 72 74 65 64  term is inserted
381c3 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74   into the parent
381c4 20 6f 66 20 70 54 72 65 65 2e 20 49 66 20 70 54   of pTree. If pT
381c5 72 65 65 20 0a 20 20 2a 2a 20 68 61 73 20 6e 6f  ree .  ** has no
381c6 20 70 61 72 65 6e 74 2c 20 6f 6e 65 20 69 73 20   parent, one is 
381c7 63 72 65 61 74 65 64 20 68 65 72 65 2e 0a 20 20  created here..  
381c8 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 53 65 67  */.  pNew = (Seg
381c9 6d 65 6e 74 4e 6f 64 65 20 2a 29 73 71 6c 69 74  mentNode *)sqlit
381ca 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
381cb 28 53 65 67 6d 65 6e 74 4e 6f 64 65 29 20 2b 20  (SegmentNode) + 
381cc 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 29 3b 0a 20  p->nNodeSize);. 
381cd 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20   if( !pNew ){.  
381ce 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
381cf 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d  NOMEM;.  }.  mem
381d0 73 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a  set(pNew, 0, siz
381d1 65 6f 66 28 53 65 67 6d 65 6e 74 4e 6f 64 65 29  eof(SegmentNode)
381d2 29 3b 0a 20 20 70 4e 65 77 2d 3e 6e 44 61 74 61  );.  pNew->nData
381d3 20 3d 20 31 20 2b 20 46 54 53 33 5f 56 41 52 49   = 1 + FTS3_VARI
381d4 4e 54 5f 4d 41 58 3b 0a 20 20 70 4e 65 77 2d 3e  NT_MAX;.  pNew->
381d5 61 44 61 74 61 20 3d 20 28 63 68 61 72 20 2a 29  aData = (char *)
381d6 26 70 4e 65 77 5b 31 5d 3b 0a 0a 20 20 69 66 28  &pNew[1];..  if(
381d7 20 70 54 72 65 65 20 29 7b 0a 20 20 20 20 53 65   pTree ){.    Se
381d8 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 50 61 72 65  gmentNode *pPare
381d9 6e 74 20 3d 20 70 54 72 65 65 2d 3e 70 50 61 72  nt = pTree->pPar
381da 65 6e 74 3b 0a 20 20 20 20 72 63 20 3d 20 66 74  ent;.    rc = ft
381db 73 33 4e 6f 64 65 41 64 64 54 65 72 6d 28 70 2c  s3NodeAddTerm(p,
381dc 20 26 70 50 61 72 65 6e 74 2c 20 69 73 43 6f 70   &pParent, isCop
381dd 79 54 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 54  yTerm, zTerm, nT
381de 65 72 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 54  erm);.    if( pT
381df 72 65 65 2d 3e 70 50 61 72 65 6e 74 3d 3d 30 20  ree->pParent==0 
381e0 29 7b 0a 20 20 20 20 20 20 70 54 72 65 65 2d 3e  ){.      pTree->
381e1 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e  pParent = pParen
381e2 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 72  t;.    }.    pTr
381e3 65 65 2d 3e 70 52 69 67 68 74 20 3d 20 70 4e 65  ee->pRight = pNe
381e4 77 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65  w;.    pNew->pLe
381e5 66 74 6d 6f 73 74 20 3d 20 70 54 72 65 65 2d 3e  ftmost = pTree->
381e6 70 4c 65 66 74 6d 6f 73 74 3b 0a 20 20 20 20 70  pLeftmost;.    p
381e7 4e 65 77 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70  New->pParent = p
381e8 50 61 72 65 6e 74 3b 0a 20 20 20 20 70 4e 65 77  Parent;.    pNew
381e9 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 70 54 72 65  ->zMalloc = pTre
381ea 65 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20  e->zMalloc;.    
381eb 70 4e 65 77 2d 3e 6e 4d 61 6c 6c 6f 63 20 3d 20  pNew->nMalloc = 
381ec 70 54 72 65 65 2d 3e 6e 4d 61 6c 6c 6f 63 3b 0a  pTree->nMalloc;.
381ed 20 20 20 20 70 54 72 65 65 2d 3e 7a 4d 61 6c 6c      pTree->zMall
381ee 6f 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  oc = 0;.  }else{
381ef 0a 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74  .    pNew->pLeft
381f0 6d 6f 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20  most = pNew;.   
381f1 20 72 63 20 3d 20 66 74 73 33 4e 6f 64 65 41 64   rc = fts3NodeAd
381f2 64 54 65 72 6d 28 70 2c 20 26 70 4e 65 77 2c 20  dTerm(p, &pNew, 
381f3 69 73 43 6f 70 79 54 65 72 6d 2c 20 7a 54 65 72  isCopyTerm, zTer
381f4 6d 2c 20 6e 54 65 72 6d 29 3b 20 0a 20 20 7d 0a  m, nTerm); .  }.
381f5 0a 20 20 2a 70 70 54 72 65 65 20 3d 20 70 4e 65  .  *ppTree = pNe
381f6 77 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  w;.  return rc;.
381f7 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20  }../*.** Helper 
381f8 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 66 74 73  function for fts
381f9 33 4e 6f 64 65 57 72 69 74 65 28 29 2e 0a 2a 2f  3NodeWrite()..*/
381fa 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
381fb 54 72 65 65 46 69 6e 69 73 68 4e 6f 64 65 28 0a  TreeFinishNode(.
381fc 20 20 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70    SegmentNode *p
381fd 54 72 65 65 2c 20 0a 20 20 69 6e 74 20 69 48 65  Tree, .  int iHe
381fe 69 67 68 74 2c 20 0a 20 20 73 71 6c 69 74 65 33  ight, .  sqlite3
381ff 5f 69 6e 74 36 34 20 69 4c 65 66 74 43 68 69 6c  _int64 iLeftChil
38200 64 0a 29 7b 0a 20 20 69 6e 74 20 6e 53 74 61 72  d.){.  int nStar
38201 74 3b 0a 20 20 61 73 73 65 72 74 28 20 69 48 65  t;.  assert( iHe
38202 69 67 68 74 3e 3d 31 20 26 26 20 69 48 65 69 67  ight>=1 && iHeig
38203 68 74 3c 31 32 38 20 29 3b 0a 20 20 6e 53 74 61  ht<128 );.  nSta
38204 72 74 20 3d 20 46 54 53 33 5f 56 41 52 49 4e 54  rt = FTS3_VARINT
38205 5f 4d 41 58 20 2d 20 73 71 6c 69 74 65 33 46 74  _MAX - sqlite3Ft
38206 73 33 56 61 72 69 6e 74 4c 65 6e 28 69 4c 65 66  s3VarintLen(iLef
38207 74 43 68 69 6c 64 29 3b 0a 20 20 70 54 72 65 65  tChild);.  pTree
38208 2d 3e 61 44 61 74 61 5b 6e 53 74 61 72 74 5d 20  ->aData[nStart] 
38209 3d 20 28 63 68 61 72 29 69 48 65 69 67 68 74 3b  = (char)iHeight;
3820a 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33 50 75  .  sqlite3Fts3Pu
3820b 74 56 61 72 69 6e 74 28 26 70 54 72 65 65 2d 3e  tVarint(&pTree->
3820c 61 44 61 74 61 5b 6e 53 74 61 72 74 2b 31 5d 2c  aData[nStart+1],
3820d 20 69 4c 65 66 74 43 68 69 6c 64 29 3b 0a 20 20   iLeftChild);.  
3820e 72 65 74 75 72 6e 20 6e 53 74 61 72 74 3b 0a 7d  return nStart;.}
3820f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68  ../*.** Write th
38210 65 20 62 75 66 66 65 72 20 66 6f 72 20 74 68 65  e buffer for the
38211 20 73 65 67 6d 65 6e 74 20 6e 6f 64 65 20 70 54   segment node pT
38212 72 65 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69  ree and all of i
38213 74 73 20 70 65 65 72 73 20 74 6f 20 74 68 65 0a  ts peers to the.
38214 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  ** database. The
38215 6e 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63  n call this func
38216 74 69 6f 6e 20 72 65 63 75 72 73 69 76 65 6c 79  tion recursively
38217 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 70 61   to write the pa
38218 72 65 6e 74 20 6f 66 20 0a 2a 2a 20 70 54 72 65  rent of .** pTre
38219 65 20 61 6e 64 20 69 74 73 20 70 65 65 72 73 20  e and its peers 
3821a 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
3821b 20 0a 2a 2a 0a 2a 2a 20 45 78 63 65 70 74 2c 20   .**.** Except, 
3821c 69 66 20 70 54 72 65 65 20 69 73 20 61 20 72 6f  if pTree is a ro
3821d 6f 74 20 6e 6f 64 65 2c 20 64 6f 20 6e 6f 74 20  ot node, do not 
3821e 77 72 69 74 65 20 69 74 20 74 6f 20 74 68 65 20  write it to the 
3821f 64 61 74 61 62 61 73 65 2e 20 49 6e 73 74 65 61  database. Instea
38220 64 2c 0a 2a 2a 20 73 65 74 20 6f 75 74 70 75 74  d,.** set output
38221 20 76 61 72 69 61 62 6c 65 73 20 2a 70 61 52 6f   variables *paRo
38222 6f 74 20 61 6e 64 20 2a 70 6e 52 6f 6f 74 20 74  ot and *pnRoot t
38223 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 72 6f  o contain the ro
38224 6f 74 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49  ot node..**.** I
38225 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53 51  f successful, SQ
38226 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
38227 6e 65 64 20 61 6e 64 20 6f 75 74 70 75 74 20 76  ned and output v
38228 61 72 69 61 62 6c 65 20 2a 70 69 4c 61 73 74 20  ariable *piLast 
38229 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 74 68 65  is.** set to the
3822a 20 6c 61 72 67 65 73 74 20 62 6c 6f 63 6b 69 64   largest blockid
3822b 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
3822c 64 61 74 61 62 61 73 65 20 28 6f 72 20 7a 65 72  database (or zer
3822d 6f 20 69 66 20 6e 6f 0a 2a 2a 20 62 6c 6f 63 6b  o if no.** block
3822e 73 20 77 65 72 65 20 77 72 69 74 74 65 6e 20 74  s were written t
3822f 6f 20 74 68 65 20 64 62 29 2e 20 4f 74 68 65 72  o the db). Other
38230 77 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20  wise, an SQLite 
38231 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 0a 2a  error code is .*
38232 2a 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73  * returned..*/.s
38233 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 4e 6f  tatic int fts3No
38234 64 65 57 72 69 74 65 28 0a 20 20 46 74 73 33 54  deWrite(.  Fts3T
38235 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20  able *p,        
38236 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69             /* Vi
38237 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64  rtual table hand
38238 6c 65 20 2a 2f 0a 20 20 53 65 67 6d 65 6e 74 4e  le */.  SegmentN
38239 6f 64 65 20 2a 70 54 72 65 65 2c 20 20 20 20 20  ode *pTree,     
3823a 20 20 20 20 20 20 20 20 2f 2a 20 53 65 67 6d 65          /* Segme
3823b 6e 74 4e 6f 64 65 20 68 61 6e 64 6c 65 20 2a 2f  ntNode handle */
3823c 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74 2c 20  .  int iHeight, 
3823d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3823e 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f 66 20     /* Height of 
3823f 74 68 69 73 20 6e 6f 64 65 20 69 6e 20 74 72 65  this node in tre
38240 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  e */.  sqlite3_i
38241 6e 74 36 34 20 69 4c 65 61 66 2c 20 20 20 20 20  nt64 iLeaf,     
38242 20 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20         /* Block 
38243 69 64 20 6f 66 20 66 69 72 73 74 20 6c 65 61 66  id of first leaf
38244 20 6e 6f 64 65 20 2a 2f 0a 20 20 73 71 6c 69 74   node */.  sqlit
38245 65 33 5f 69 6e 74 36 34 20 69 46 72 65 65 2c 20  e3_int64 iFree, 
38246 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 6c             /* Bl
38247 6f 63 6b 20 69 64 20 6f 66 20 6e 65 78 74 20 66  ock id of next f
38248 72 65 65 20 73 6c 6f 74 20 69 6e 20 25 5f 73 65  ree slot in %_se
38249 67 6d 65 6e 74 73 20 2a 2f 0a 20 20 73 71 6c 69  gments */.  sqli
3824a 74 65 33 5f 69 6e 74 36 34 20 2a 70 69 4c 61 73  te3_int64 *piLas
3824b 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  t,          /* O
3824c 55 54 3a 20 42 6c 6f 63 6b 20 69 64 20 6f 66 20  UT: Block id of 
3824d 6c 61 73 74 20 65 6e 74 72 79 20 77 72 69 74 74  last entry writt
3824e 65 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  en */.  char **p
3824f 61 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20 20  aRoot,          
38250 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
38251 44 61 74 61 20 66 6f 72 20 72 6f 6f 74 20 6e 6f  Data for root no
38252 64 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52  de */.  int *pnR
38253 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20  oot             
38254 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
38255 53 69 7a 65 20 6f 66 20 72 6f 6f 74 20 6e 6f 64  Size of root nod
38256 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b  e in bytes */.){
38257 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
38258 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 21 70  TE_OK;..  if( !p
38259 54 72 65 65 2d 3e 70 50 61 72 65 6e 74 20 29 7b  Tree->pParent ){
3825a 0a 20 20 20 20 2f 2a 20 52 6f 6f 74 20 6e 6f 64  .    /* Root nod
3825b 65 20 6f 66 20 74 68 65 20 74 72 65 65 2e 20 2a  e of the tree. *
3825c 2f 0a 20 20 20 20 69 6e 74 20 6e 53 74 61 72 74  /.    int nStart
3825d 20 3d 20 66 74 73 33 54 72 65 65 46 69 6e 69 73   = fts3TreeFinis
3825e 68 4e 6f 64 65 28 70 54 72 65 65 2c 20 69 48 65  hNode(pTree, iHe
3825f 69 67 68 74 2c 20 69 4c 65 61 66 29 3b 0a 20 20  ight, iLeaf);.  
38260 20 20 2a 70 69 4c 61 73 74 20 3d 20 69 46 72 65    *piLast = iFre
38261 65 2d 31 3b 0a 20 20 20 20 2a 70 6e 52 6f 6f 74  e-1;.    *pnRoot
38262 20 3d 20 70 54 72 65 65 2d 3e 6e 44 61 74 61 20   = pTree->nData 
38263 2d 20 6e 53 74 61 72 74 3b 0a 20 20 20 20 2a 70  - nStart;.    *p
38264 61 52 6f 6f 74 20 3d 20 26 70 54 72 65 65 2d 3e  aRoot = &pTree->
38265 61 44 61 74 61 5b 6e 53 74 61 72 74 5d 3b 0a 20  aData[nStart];. 
38266 20 7d 65 6c 73 65 7b 0a 20 20 20 20 53 65 67 6d   }else{.    Segm
38267 65 6e 74 4e 6f 64 65 20 2a 70 49 74 65 72 3b 0a  entNode *pIter;.
38268 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
38269 34 20 69 4e 65 78 74 46 72 65 65 20 3d 20 69 46  4 iNextFree = iF
3826a 72 65 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ree;.    sqlite3
3826b 5f 69 6e 74 36 34 20 69 4e 65 78 74 4c 65 61 66  _int64 iNextLeaf
3826c 20 3d 20 69 4c 65 61 66 3b 0a 20 20 20 20 66 6f   = iLeaf;.    fo
3826d 72 28 70 49 74 65 72 3d 70 54 72 65 65 2d 3e 70  r(pIter=pTree->p
3826e 4c 65 66 74 6d 6f 73 74 3b 20 70 49 74 65 72 20  Leftmost; pIter 
3826f 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
38270 3b 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70  ; pIter=pIter->p
38271 52 69 67 68 74 29 7b 0a 20 20 20 20 20 20 69 6e  Right){.      in
38272 74 20 6e 53 74 61 72 74 20 3d 20 66 74 73 33 54  t nStart = fts3T
38273 72 65 65 46 69 6e 69 73 68 4e 6f 64 65 28 70 49  reeFinishNode(pI
38274 74 65 72 2c 20 69 48 65 69 67 68 74 2c 20 69 4e  ter, iHeight, iN
38275 65 78 74 4c 65 61 66 29 3b 0a 20 20 20 20 20 20  extLeaf);.      
38276 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 70 49 74  int nWrite = pIt
38277 65 72 2d 3e 6e 44 61 74 61 20 2d 20 6e 53 74 61  er->nData - nSta
38278 72 74 3b 0a 20 20 0a 20 20 20 20 20 20 72 63 20  rt;.  .      rc 
38279 3d 20 66 74 73 33 57 72 69 74 65 53 65 67 6d 65  = fts3WriteSegme
3827a 6e 74 28 70 2c 20 69 4e 65 78 74 46 72 65 65 2c  nt(p, iNextFree,
3827b 20 26 70 49 74 65 72 2d 3e 61 44 61 74 61 5b 6e   &pIter->aData[n
3827c 53 74 61 72 74 5d 2c 20 6e 57 72 69 74 65 29 3b  Start], nWrite);
3827d 0a 20 20 20 20 20 20 69 4e 65 78 74 46 72 65 65  .      iNextFree
3827e 2b 2b 3b 0a 20 20 20 20 20 20 69 4e 65 78 74 4c  ++;.      iNextL
3827f 65 61 66 20 2b 3d 20 28 70 49 74 65 72 2d 3e 6e  eaf += (pIter->n
38280 45 6e 74 72 79 2b 31 29 3b 0a 20 20 20 20 7d 0a  Entry+1);.    }.
38281 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
38282 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61  TE_OK ){.      a
38283 73 73 65 72 74 28 20 69 4e 65 78 74 4c 65 61 66  ssert( iNextLeaf
38284 3d 3d 69 46 72 65 65 20 29 3b 0a 20 20 20 20 20  ==iFree );.     
38285 20 72 63 20 3d 20 66 74 73 33 4e 6f 64 65 57 72   rc = fts3NodeWr
38286 69 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 70  ite(.          p
38287 2c 20 70 54 72 65 65 2d 3e 70 50 61 72 65 6e 74  , pTree->pParent
38288 2c 20 69 48 65 69 67 68 74 2b 31 2c 20 69 46 72  , iHeight+1, iFr
38289 65 65 2c 20 69 4e 65 78 74 46 72 65 65 2c 20 70  ee, iNextFree, p
3828a 69 4c 61 73 74 2c 20 70 61 52 6f 6f 74 2c 20 70  iLast, paRoot, p
3828b 6e 52 6f 6f 74 0a 20 20 20 20 20 20 29 3b 0a 20  nRoot.      );. 
3828c 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
3828d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
3828e 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20  Free all memory 
3828f 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 73 73 6f  allocations asso
38290 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
38291 74 72 65 65 20 70 54 72 65 65 2e 0a 2a 2f 0a 73  tree pTree..*/.s
38292 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 4e  tatic void fts3N
38293 6f 64 65 46 72 65 65 28 53 65 67 6d 65 6e 74 4e  odeFree(SegmentN
38294 6f 64 65 20 2a 70 54 72 65 65 29 7b 0a 20 20 69  ode *pTree){.  i
38295 66 28 20 70 54 72 65 65 20 29 7b 0a 20 20 20 20  f( pTree ){.    
38296 53 65 67 6d 65 6e 74 4e 6f 64 65 20 2a 70 20 3d  SegmentNode *p =
38297 20 70 54 72 65 65 2d 3e 70 4c 65 66 74 6d 6f 73   pTree->pLeftmos
38298 74 3b 0a 20 20 20 20 66 74 73 33 4e 6f 64 65 46  t;.    fts3NodeF
38299 72 65 65 28 70 2d 3e 70 50 61 72 65 6e 74 29 3b  ree(p->pParent);
3829a 0a 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b  .    while( p ){
3829b 0a 20 20 20 20 20 20 53 65 67 6d 65 6e 74 4e 6f  .      SegmentNo
3829c 64 65 20 2a 70 52 69 67 68 74 20 3d 20 70 2d 3e  de *pRight = p->
3829d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66  pRight;.      if
3829e 28 20 70 2d 3e 61 44 61 74 61 21 3d 28 63 68 61  ( p->aData!=(cha
3829f 72 20 2a 29 26 70 5b 31 5d 20 29 7b 0a 20 20 20  r *)&p[1] ){.   
382a0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
382a1 65 28 70 2d 3e 61 44 61 74 61 29 3b 0a 20 20 20  e(p->aData);.   
382a2 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72     }.      asser
382a3 74 28 20 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20  t( pRight==0 || 
382a4 70 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 30 20 29 3b  p->zMalloc==0 );
382a5 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
382a6 72 65 65 28 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b  ree(p->zMalloc);
382a7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
382a8 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 70 20  ree(p);.      p 
382a9 3d 20 70 52 69 67 68 74 3b 0a 20 20 20 20 7d 0a  = pRight;.    }.
382aa 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64    }.}../*.** Add
382ab 20 61 20 74 65 72 6d 20 74 6f 20 74 68 65 20 73   a term to the s
382ac 65 67 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 6e  egment being con
382ad 73 74 72 75 63 74 65 64 20 62 79 20 74 68 65 20  structed by the 
382ae 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20 6f 62  SegmentWriter ob
382af 6a 65 63 74 0a 2a 2a 20 2a 70 70 57 72 69 74 65  ject.** *ppWrite
382b0 72 2e 20 57 68 65 6e 20 61 64 64 69 6e 67 20 74  r. When adding t
382b1 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 74 6f  he first term to
382b2 20 61 20 73 65 67 6d 65 6e 74 2c 20 2a 70 70 57   a segment, *ppW
382b3 72 69 74 65 72 20 73 68 6f 75 6c 64 0a 2a 2a 20  riter should.** 
382b4 62 65 20 70 61 73 73 65 64 20 4e 55 4c 4c 2e 20  be passed NULL. 
382b5 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69  This function wi
382b6 6c 6c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  ll allocate a ne
382b7 77 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20  w SegmentWriter 
382b8 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e 64 20 72 65  object.** and re
382b9 74 75 72 6e 20 69 74 20 76 69 61 20 74 68 65 20  turn it via the 
382ba 69 6e 70 75 74 2f 6f 75 74 70 75 74 20 76 61 72  input/output var
382bb 69 61 62 6c 65 20 2a 70 70 57 72 69 74 65 72 20  iable *ppWriter 
382bc 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
382bd 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
382be 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  l, SQLITE_OK is 
382bf 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77  returned. Otherw
382c0 69 73 65 2c 20 61 6e 20 53 51 4c 69 74 65 20 65  ise, an SQLite e
382c1 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74  rror code..*/.st
382c2 61 74 69 63 20 69 6e 74 20 66 74 73 33 53 65 67  atic int fts3Seg
382c3 57 72 69 74 65 72 41 64 64 28 0a 20 20 46 74 73  WriterAdd(.  Fts
382c4 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
382c5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
382c6 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61  Virtual table ha
382c7 6e 64 6c 65 20 2a 2f 0a 20 20 53 65 67 6d 65 6e  ndle */.  Segmen
382c8 74 57 72 69 74 65 72 20 2a 2a 70 70 57 72 69 74  tWriter **ppWrit
382c9 65 72 2c 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f  er,       /* IN/
382ca 4f 55 54 3a 20 53 65 67 6d 65 6e 74 57 72 69 74  OUT: SegmentWrit
382cb 65 72 20 68 61 6e 64 6c 65 20 2a 2f 20 0a 20 20  er handle */ .  
382cc 69 6e 74 20 69 73 43 6f 70 79 54 65 72 6d 2c 20  int isCopyTerm, 
382cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
382ce 2f 2a 20 54 72 75 65 20 69 66 20 62 75 66 66 65  /* True if buffe
382cf 72 20 7a 54 65 72 6d 20 6d 75 73 74 20 62 65 20  r zTerm must be 
382d0 63 6f 70 69 65 64 20 2a 2f 0a 20 20 63 6f 6e 73  copied */.  cons
382d1 74 20 63 68 61 72 20 2a 7a 54 65 72 6d 2c 20 20  t char *zTerm,  
382d2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
382d3 6f 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72  ointer to buffer
382d4 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 65 72 6d   containing term
382d5 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 2c   */.  int nTerm,
382d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
382d7 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
382d8 20 74 65 72 6d 20 69 6e 20 62 79 74 65 73 20 2a   term in bytes *
382d9 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
382da 61 44 6f 63 6c 69 73 74 2c 20 20 20 20 20 20 20  aDoclist,       
382db 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
382dc 6f 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e  o buffer contain
382dd 69 6e 67 20 64 6f 63 6c 69 73 74 20 2a 2f 0a 20  ing doclist */. 
382de 20 69 6e 74 20 6e 44 6f 63 6c 69 73 74 20 20 20   int nDoclist   
382df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
382e0 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f 63 6c   /* Size of docl
382e1 69 73 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  ist in bytes */.
382e2 29 7b 0a 20 20 69 6e 74 20 6e 50 72 65 66 69 78  ){.  int nPrefix
382e3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
382e4 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
382e5 74 65 72 6d 20 70 72 65 66 69 78 20 69 6e 20 62  term prefix in b
382e6 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  ytes */.  int nS
382e7 75 66 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  uffix;          
382e8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
382e9 65 20 6f 66 20 74 65 72 6d 20 73 75 66 66 69 78  e of term suffix
382ea 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69   in bytes */.  i
382eb 6e 74 20 6e 52 65 71 3b 20 20 20 20 20 20 20 20  nt nReq;        
382ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
382ed 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
382ee 73 20 72 65 71 75 69 72 65 64 20 6f 6e 20 6c 65  s required on le
382ef 61 66 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  af page */.  int
382f0 20 6e 44 61 74 61 3b 0a 20 20 53 65 67 6d 65 6e   nData;.  Segmen
382f1 74 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  tWriter *pWriter
382f2 20 3d 20 2a 70 70 57 72 69 74 65 72 3b 0a 0a 20   = *ppWriter;.. 
382f3 20 69 66 28 20 21 70 57 72 69 74 65 72 20 29 7b   if( !pWriter ){
382f4 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
382f5 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
382f6 53 74 6d 74 3b 0a 0a 20 20 20 20 2f 2a 20 41 6c  Stmt;..    /* Al
382f7 6c 6f 63 61 74 65 20 74 68 65 20 53 65 67 6d 65  locate the Segme
382f8 6e 74 57 72 69 74 65 72 20 73 74 72 75 63 74 75  ntWriter structu
382f9 72 65 20 2a 2f 0a 20 20 20 20 70 57 72 69 74 65  re */.    pWrite
382fa 72 20 3d 20 28 53 65 67 6d 65 6e 74 57 72 69 74  r = (SegmentWrit
382fb 65 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  er *)sqlite3_mal
382fc 6c 6f 63 28 73 69 7a 65 6f 66 28 53 65 67 6d 65  loc(sizeof(Segme
382fd 6e 74 57 72 69 74 65 72 29 29 3b 0a 20 20 20 20  ntWriter));.    
382fe 69 66 28 20 21 70 57 72 69 74 65 72 20 29 20 72  if( !pWriter ) r
382ff 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
38300 45 4d 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70  EM;.    memset(p
38301 57 72 69 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f  Writer, 0, sizeo
38302 66 28 53 65 67 6d 65 6e 74 57 72 69 74 65 72 29  f(SegmentWriter)
38303 29 3b 0a 20 20 20 20 2a 70 70 57 72 69 74 65 72  );.    *ppWriter
38304 20 3d 20 70 57 72 69 74 65 72 3b 0a 0a 20 20 20   = pWriter;..   
38305 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 62   /* Allocate a b
38306 75 66 66 65 72 20 69 6e 20 77 68 69 63 68 20 74  uffer in which t
38307 6f 20 61 63 63 75 6d 75 6c 61 74 65 20 64 61 74  o accumulate dat
38308 61 20 2a 2f 0a 20 20 20 20 70 57 72 69 74 65 72  a */.    pWriter
38309 2d 3e 61 44 61 74 61 20 3d 20 28 63 68 61 72 20  ->aData = (char 
3830a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
3830b 28 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65 29 3b 0a  (p->nNodeSize);.
3830c 20 20 20 20 69 66 28 20 21 70 57 72 69 74 65 72      if( !pWriter
3830d 2d 3e 61 44 61 74 61 20 29 20 72 65 74 75 72 6e  ->aData ) return
3830e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3830f 20 20 20 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a     pWriter->nSiz
38310 65 20 3d 20 70 2d 3e 6e 4e 6f 64 65 53 69 7a 65  e = p->nNodeSize
38311 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 74  ;..    /* Find t
38312 68 65 20 6e 65 78 74 20 66 72 65 65 20 62 6c 6f  he next free blo
38313 63 6b 69 64 20 69 6e 20 74 68 65 20 25 5f 73 65  ckid in the %_se
38314 67 6d 65 6e 74 73 20 74 61 62 6c 65 20 2a 2f 0a  gments table */.
38315 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c      rc = fts3Sql
38316 53 74 6d 74 28 70 2c 20 53 51 4c 5f 4e 45 58 54  Stmt(p, SQL_NEXT
38317 5f 53 45 47 4d 45 4e 54 53 5f 49 44 2c 20 26 70  _SEGMENTS_ID, &p
38318 53 74 6d 74 2c 20 30 29 3b 0a 20 20 20 20 69 66  Stmt, 0);.    if
38319 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3831a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
3831b 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
3831c 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 53  =sqlite3_step(pS
3831d 74 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 70 57  tmt) ){.      pW
3831e 72 69 74 65 72 2d 3e 69 46 72 65 65 20 3d 20 73  riter->iFree = s
3831f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
38320 74 36 34 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  t64(pStmt, 0);. 
38321 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 46       pWriter->iF
38322 69 72 73 74 20 3d 20 70 57 72 69 74 65 72 2d 3e  irst = pWriter->
38323 69 46 72 65 65 3b 0a 20 20 20 20 7d 0a 20 20 20  iFree;.    }.   
38324 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65   rc = sqlite3_re
38325 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 20 20  set(pStmt);.    
38326 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
38327 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
38328 20 7d 0a 20 20 6e 44 61 74 61 20 3d 20 70 57 72   }.  nData = pWr
38329 69 74 65 72 2d 3e 6e 44 61 74 61 3b 0a 0a 20 20  iter->nData;..  
3832a 6e 50 72 65 66 69 78 20 3d 20 66 74 73 33 50 72  nPrefix = fts3Pr
3832b 65 66 69 78 43 6f 6d 70 72 65 73 73 28 70 57 72  efixCompress(pWr
3832c 69 74 65 72 2d 3e 7a 54 65 72 6d 2c 20 70 57 72  iter->zTerm, pWr
3832d 69 74 65 72 2d 3e 6e 54 65 72 6d 2c 20 7a 54 65  iter->nTerm, zTe
3832e 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 6e 53  rm, nTerm);.  nS
3832f 75 66 66 69 78 20 3d 20 6e 54 65 72 6d 2d 6e 50  uffix = nTerm-nP
38330 72 65 66 69 78 3b 0a 0a 20 20 2f 2a 20 46 69 67  refix;..  /* Fig
38331 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
38332 20 62 79 74 65 73 20 61 72 65 20 72 65 71 75 69   bytes are requi
38333 72 65 64 20 62 79 20 74 68 69 73 20 6e 65 77 20  red by this new 
38334 65 6e 74 72 79 20 2a 2f 0a 20 20 6e 52 65 71 20  entry */.  nReq 
38335 3d 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72  = sqlite3Fts3Var
38336 69 6e 74 4c 65 6e 28 6e 50 72 65 66 69 78 29 20  intLen(nPrefix) 
38337 2b 20 20 20 20 2f 2a 20 76 61 72 69 6e 74 20 63  +    /* varint c
38338 6f 6e 74 61 69 6e 69 6e 67 20 70 72 65 66 69 78  ontaining prefix
38339 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 73 71 6c   size */.    sql
3833a 69 74 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65  ite3Fts3VarintLe
3833b 6e 28 6e 53 75 66 66 69 78 29 20 2b 20 20 20 20  n(nSuffix) +    
3833c 20 20 20 20 20 2f 2a 20 76 61 72 69 6e 74 20 63       /* varint c
3833d 6f 6e 74 61 69 6e 69 6e 67 20 73 75 66 66 69 78  ontaining suffix
3833e 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 6e 53 75   size */.    nSu
3833f 66 66 69 78 20 2b 20 20 20 20 20 20 20 20 20 20  ffix +          
38340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38341 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 73 75 66       /* Term suf
38342 66 69 78 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  fix */.    sqlit
38343 65 33 46 74 73 33 56 61 72 69 6e 74 4c 65 6e 28  e3Fts3VarintLen(
38344 6e 44 6f 63 6c 69 73 74 29 20 2b 20 20 20 20 20  nDoclist) +     
38345 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 64 6f     /* Size of do
38346 63 6c 69 73 74 20 2a 2f 0a 20 20 20 20 6e 44 6f  clist */.    nDo
38347 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  clist;          
38348 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38349 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69 73 74 20       /* Doclist 
3834a 64 61 74 61 20 2a 2f 0a 0a 20 20 69 66 28 20 6e  data */..  if( n
3834b 44 61 74 61 3e 30 20 26 26 20 6e 44 61 74 61 2b  Data>0 && nData+
3834c 6e 52 65 71 3e 70 2d 3e 6e 4e 6f 64 65 53 69 7a  nReq>p->nNodeSiz
3834d 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b  e ){.    int rc;
3834e 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72  ..    /* The cur
3834f 72 65 6e 74 20 6c 65 61 66 20 6e 6f 64 65 20 69  rent leaf node i
38350 73 20 66 75 6c 6c 2e 20 57 72 69 74 65 20 69 74  s full. Write it
38351 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
38352 62 61 73 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20  base. */.    rc 
38353 3d 20 66 74 73 33 57 72 69 74 65 53 65 67 6d 65  = fts3WriteSegme
38354 6e 74 28 70 2c 20 70 57 72 69 74 65 72 2d 3e 69  nt(p, pWriter->i
38355 46 72 65 65 2b 2b 2c 20 70 57 72 69 74 65 72 2d  Free++, pWriter-
38356 3e 61 44 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a  >aData, nData);.
38357 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
38358 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
38359 63 3b 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 74  c;..    /* Add t
3835a 68 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20  he current term 
3835b 74 6f 20 74 68 65 20 69 6e 74 65 72 69 6f 72 20  to the interior 
3835c 6e 6f 64 65 20 74 72 65 65 2e 20 54 68 65 20 74  node tree. The t
3835d 65 72 6d 20 61 64 64 65 64 20 74 6f 0a 20 20 20  erm added to.   
3835e 20 2a 2a 20 74 68 65 20 69 6e 74 65 72 69 6f 72   ** the interior
3835f 20 74 72 65 65 20 6d 75 73 74 3a 0a 20 20 20 20   tree must:.    
38360 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 61 29 20 62  **.    **   a) b
38361 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  e greater than t
38362 68 65 20 6c 61 72 67 65 73 74 20 74 65 72 6d 20  he largest term 
38363 6f 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  on the leaf node
38364 20 6a 75 73 74 20 77 72 69 74 74 65 6e 0a 20 20   just written.  
38365 20 20 2a 2a 20 20 20 20 20 20 74 6f 20 74 68 65    **      to the
38366 20 64 61 74 61 62 61 73 65 20 28 73 74 69 6c 6c   database (still
38367 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 70 57   available in pW
38368 72 69 74 65 72 2d 3e 7a 54 65 72 6d 29 2c 20 61  riter->zTerm), a
38369 6e 64 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  nd.    **.    **
3836a 20 20 20 62 29 20 62 65 20 6c 65 73 73 20 74 68     b) be less th
3836b 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
3836c 68 65 20 74 65 72 6d 20 61 62 6f 75 74 20 74 6f  he term about to
3836d 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65   be added to the
3836e 20 6e 65 77 0a 20 20 20 20 2a 2a 20 20 20 20 20   new.    **     
3836f 20 6c 65 61 66 20 6e 6f 64 65 20 28 7a 54 65 72   leaf node (zTer
38370 6d 2f 6e 54 65 72 6d 29 2e 0a 20 20 20 20 2a 2a  m/nTerm)..    **
38371 0a 20 20 20 20 2a 2a 20 49 6e 20 6f 74 68 65 72  .    ** In other
38372 20 77 6f 72 64 73 2c 20 69 74 20 6d 75 73 74 20   words, it must 
38373 62 65 20 74 68 65 20 70 72 65 66 69 78 20 6f 66  be the prefix of
38374 20 7a 54 65 72 6d 20 31 20 62 79 74 65 20 6c 6f   zTerm 1 byte lo
38375 6e 67 65 72 20 74 68 61 6e 0a 20 20 20 20 2a 2a  nger than.    **
38376 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66   the common pref
38377 69 78 20 28 69 66 20 61 6e 79 29 20 6f 66 20 7a  ix (if any) of z
38378 54 65 72 6d 20 61 6e 64 20 70 57 72 69 74 65 72  Term and pWriter
38379 2d 3e 7a 54 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a  ->zTerm..    */.
3837a 20 20 20 20 61 73 73 65 72 74 28 20 6e 50 72 65      assert( nPre
3837b 66 69 78 3c 6e 54 65 72 6d 20 29 3b 0a 20 20 20  fix<nTerm );.   
3837c 20 72 63 20 3d 20 66 74 73 33 4e 6f 64 65 41 64   rc = fts3NodeAd
3837d 64 54 65 72 6d 28 70 2c 20 26 70 57 72 69 74 65  dTerm(p, &pWrite
3837e 72 2d 3e 70 54 72 65 65 2c 20 69 73 43 6f 70 79  r->pTree, isCopy
3837f 54 65 72 6d 2c 20 7a 54 65 72 6d 2c 20 6e 50 72  Term, zTerm, nPr
38380 65 66 69 78 2b 31 29 3b 0a 20 20 20 20 69 66 28  efix+1);.    if(
38381 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
38382 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20   return rc;..   
38383 20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20   nData = 0;.    
38384 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d 20 3d  pWriter->nTerm =
38385 20 30 3b 0a 0a 20 20 20 20 6e 50 72 65 66 69 78   0;..    nPrefix
38386 20 3d 20 30 3b 0a 20 20 20 20 6e 53 75 66 66 69   = 0;.    nSuffi
38387 78 20 3d 20 6e 54 65 72 6d 3b 0a 20 20 20 20 6e  x = nTerm;.    n
38388 52 65 71 20 3d 20 31 20 2b 20 20 20 20 20 20 20  Req = 1 +       
38389 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3838a 20 20 20 20 20 20 20 2f 2a 20 76 61 72 69 6e 74         /* varint
3838b 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 72 65 66   containing pref
3838c 69 78 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20  ix size */.     
3838d 20 73 71 6c 69 74 65 33 46 74 73 33 56 61 72 69   sqlite3Fts3Vari
3838e 6e 74 4c 65 6e 28 6e 54 65 72 6d 29 20 2b 20 20  ntLen(nTerm) +  
3838f 20 20 20 20 20 20 20 2f 2a 20 76 61 72 69 6e 74         /* varint
38390 20 63 6f 6e 74 61 69 6e 69 6e 67 20 73 75 66 66   containing suff
38391 69 78 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 20  ix size */.     
38392 20 6e 54 65 72 6d 20 2b 20 20 20 20 20 20 20 20   nTerm +        
38393 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38394 20 20 20 20 20 20 20 2f 2a 20 54 65 72 6d 20 73         /* Term s
38395 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 73  uffix */.      s
38396 71 6c 69 74 65 33 46 74 73 33 56 61 72 69 6e 74  qlite3Fts3Varint
38397 4c 65 6e 28 6e 44 6f 63 6c 69 73 74 29 20 2b 20  Len(nDoclist) + 
38398 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
38399 64 6f 63 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20  doclist */.     
3839a 20 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20   nDoclist;      
3839b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3839c 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69 73         /* Doclis
3839d 74 20 64 61 74 61 20 2a 2f 0a 20 20 7d 0a 0a 20  t data */.  }.. 
3839e 20 2f 2a 20 49 66 20 74 68 65 20 62 75 66 66 65   /* If the buffe
3839f 72 20 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f  r currently allo
383a0 63 61 74 65 64 20 69 73 20 74 6f 6f 20 73 6d 61  cated is too sma
383a1 6c 6c 20 66 6f 72 20 74 68 69 73 20 65 6e 74 72  ll for this entr
383a2 79 2c 20 72 65 61 6c 6c 6f 63 0a 20 20 2a 2a 20  y, realloc.  ** 
383a3 74 68 65 20 62 75 66 66 65 72 20 74 6f 20 6d 61  the buffer to ma
383a4 6b 65 20 69 74 20 6c 61 72 67 65 20 65 6e 6f 75  ke it large enou
383a5 67 68 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e  gh..  */.  if( n
383a6 52 65 71 3e 70 57 72 69 74 65 72 2d 3e 6e 53 69  Req>pWriter->nSi
383a7 7a 65 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  ze ){.    char *
383a8 61 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  aNew = sqlite3_r
383a9 65 61 6c 6c 6f 63 28 70 57 72 69 74 65 72 2d 3e  ealloc(pWriter->
383aa 61 44 61 74 61 2c 20 6e 52 65 71 29 3b 0a 20 20  aData, nReq);.  
383ab 20 20 69 66 28 20 21 61 4e 65 77 20 29 20 72 65    if( !aNew ) re
383ac 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
383ad 4d 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  M;.    pWriter->
383ae 61 44 61 74 61 20 3d 20 61 4e 65 77 3b 0a 20 20  aData = aNew;.  
383af 20 20 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a 65    pWriter->nSize
383b0 20 3d 20 6e 52 65 71 3b 0a 20 20 7d 0a 20 20 61   = nReq;.  }.  a
383b1 73 73 65 72 74 28 20 6e 44 61 74 61 2b 6e 52 65  ssert( nData+nRe
383b2 71 3c 3d 70 57 72 69 74 65 72 2d 3e 6e 53 69 7a  q<=pWriter->nSiz
383b3 65 20 29 3b 0a 0a 20 20 2f 2a 20 41 70 70 65 6e  e );..  /* Appen
383b4 64 20 74 68 65 20 70 72 65 66 69 78 2d 63 6f 6d  d the prefix-com
383b5 70 72 65 73 73 65 64 20 74 65 72 6d 20 61 6e 64  pressed term and
383b6 20 64 6f 63 6c 69 73 74 20 74 6f 20 74 68 65 20   doclist to the 
383b7 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 6e 44 61  buffer. */.  nDa
383b8 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 46 74 73  ta += sqlite3Fts
383b9 33 50 75 74 56 61 72 69 6e 74 28 26 70 57 72 69  3PutVarint(&pWri
383ba 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61  ter->aData[nData
383bb 5d 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20 6e  ], nPrefix);.  n
383bc 44 61 74 61 20 2b 3d 20 73 71 6c 69 74 65 33 46  Data += sqlite3F
383bd 74 73 33 50 75 74 56 61 72 69 6e 74 28 26 70 57  ts3PutVarint(&pW
383be 72 69 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44 61  riter->aData[nDa
383bf 74 61 5d 2c 20 6e 53 75 66 66 69 78 29 3b 0a 20  ta], nSuffix);. 
383c0 20 6d 65 6d 63 70 79 28 26 70 57 72 69 74 65 72   memcpy(&pWriter
383c1 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61 5d 2c 20  ->aData[nData], 
383c2 26 7a 54 65 72 6d 5b 6e 50 72 65 66 69 78 5d 2c  &zTerm[nPrefix],
383c3 20 6e 53 75 66 66 69 78 29 3b 0a 20 20 6e 44 61   nSuffix);.  nDa
383c4 74 61 20 2b 3d 20 6e 53 75 66 66 69 78 3b 0a 20  ta += nSuffix;. 
383c5 20 6e 44 61 74 61 20 2b 3d 20 73 71 6c 69 74 65   nData += sqlite
383c6 33 46 74 73 33 50 75 74 56 61 72 69 6e 74 28 26  3Fts3PutVarint(&
383c7 70 57 72 69 74 65 72 2d 3e 61 44 61 74 61 5b 6e  pWriter->aData[n
383c8 44 61 74 61 5d 2c 20 6e 44 6f 63 6c 69 73 74 29  Data], nDoclist)
383c9 3b 0a 20 20 6d 65 6d 63 70 79 28 26 70 57 72 69  ;.  memcpy(&pWri
383ca 74 65 72 2d 3e 61 44 61 74 61 5b 6e 44 61 74 61  ter->aData[nData
383cb 5d 2c 20 61 44 6f 63 6c 69 73 74 2c 20 6e 44 6f  ], aDoclist, nDo
383cc 63 6c 69 73 74 29 3b 0a 20 20 70 57 72 69 74 65  clist);.  pWrite
383cd 72 2d 3e 6e 44 61 74 61 20 3d 20 6e 44 61 74 61  r->nData = nData
383ce 20 2b 20 6e 44 6f 63 6c 69 73 74 3b 0a 0a 20 20   + nDoclist;..  
383cf 2f 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72  /* Save the curr
383d0 65 6e 74 20 74 65 72 6d 20 73 6f 20 74 68 61 74  ent term so that
383d1 20 69 74 20 63 61 6e 20 62 65 20 75 73 65 64 20   it can be used 
383d2 74 6f 20 70 72 65 66 69 78 2d 63 6f 6d 70 72 65  to prefix-compre
383d3 73 73 20 74 68 65 20 6e 65 78 74 2e 0a 20 20 2a  ss the next..  *
383d4 2a 20 49 66 20 74 68 65 20 69 73 43 6f 70 79 54  * If the isCopyT
383d5 65 72 6d 20 70 61 72 61 6d 65 74 65 72 20 69 73  erm parameter is
383d6 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
383d7 62 75 66 66 65 72 20 70 6f 69 6e 74 65 64 20 74  buffer pointed t
383d8 6f 20 62 79 0a 20 20 2a 2a 20 7a 54 65 72 6d 20  o by.  ** zTerm 
383d9 69 73 20 74 72 61 6e 73 69 65 6e 74 2c 20 73 6f  is transient, so
383da 20 74 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20   take a copy of 
383db 74 68 65 20 74 65 72 6d 20 64 61 74 61 2e 20 4f  the term data. O
383dc 74 68 65 72 77 69 73 65 2c 20 6a 75 73 74 0a 20  therwise, just. 
383dd 20 2a 2a 20 73 74 6f 72 65 20 61 20 63 6f 70 79   ** store a copy
383de 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 2e   of the pointer.
383df 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f  .  */.  if( isCo
383e0 70 79 54 65 72 6d 20 29 7b 0a 20 20 20 20 69 66  pyTerm ){.    if
383e1 28 20 6e 54 65 72 6d 3e 70 57 72 69 74 65 72 2d  ( nTerm>pWriter-
383e2 3e 6e 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  >nMalloc ){.    
383e3 20 20 63 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73    char *zNew = s
383e4 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70  qlite3_realloc(p
383e5 57 72 69 74 65 72 2d 3e 7a 4d 61 6c 6c 6f 63 2c  Writer->zMalloc,
383e6 20 6e 54 65 72 6d 2a 32 29 3b 0a 20 20 20 20 20   nTerm*2);.     
383e7 20 69 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20   if( !zNew ){.  
383e8 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
383e9 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
383ea 20 7d 0a 20 20 20 20 20 20 70 57 72 69 74 65 72   }.      pWriter
383eb 2d 3e 6e 4d 61 6c 6c 6f 63 20 3d 20 6e 54 65 72  ->nMalloc = nTer
383ec 6d 2a 32 3b 0a 20 20 20 20 20 20 70 57 72 69 74  m*2;.      pWrit
383ed 65 72 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 7a 4e  er->zMalloc = zN
383ee 65 77 3b 0a 20 20 20 20 20 20 70 57 72 69 74 65  ew;.      pWrite
383ef 72 2d 3e 7a 54 65 72 6d 20 3d 20 7a 4e 65 77 3b  r->zTerm = zNew;
383f0 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
383f1 74 28 20 70 57 72 69 74 65 72 2d 3e 7a 54 65 72  t( pWriter->zTer
383f2 6d 3d 3d 70 57 72 69 74 65 72 2d 3e 7a 4d 61 6c  m==pWriter->zMal
383f3 6c 6f 63 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70  loc );.    memcp
383f4 79 28 70 57 72 69 74 65 72 2d 3e 7a 54 65 72 6d  y(pWriter->zTerm
383f5 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , zTerm, nTerm);
383f6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 57  .  }else{.    pW
383f7 72 69 74 65 72 2d 3e 7a 54 65 72 6d 20 3d 20 28  riter->zTerm = (
383f8 63 68 61 72 20 2a 29 7a 54 65 72 6d 3b 0a 20 20  char *)zTerm;.  
383f9 7d 0a 20 20 70 57 72 69 74 65 72 2d 3e 6e 54 65  }.  pWriter->nTe
383fa 72 6d 20 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20 72  rm = nTerm;..  r
383fb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
383fc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6c 75 73 68 20  .}../*.** Flush 
383fd 61 6c 6c 20 64 61 74 61 20 61 73 73 6f 63 69 61  all data associa
383fe 74 65 64 20 77 69 74 68 20 74 68 65 20 53 65 67  ted with the Seg
383ff 6d 65 6e 74 57 72 69 74 65 72 20 6f 62 6a 65 63  mentWriter objec
38400 74 20 70 57 72 69 74 65 72 20 74 6f 20 74 68 65  t pWriter to the
38401 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68  .** database. Th
38402 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74  is function must
38403 20 62 65 20 63 61 6c 6c 65 64 20 61 66 74 65 72   be called after
38404 20 61 6c 6c 20 74 65 72 6d 73 20 68 61 76 65 20   all terms have 
38405 62 65 65 6e 20 61 64 64 65 64 0a 2a 2a 20 74 6f  been added.** to
38406 20 74 68 65 20 73 65 67 6d 65 6e 74 20 75 73 69   the segment usi
38407 6e 67 20 66 74 73 33 53 65 67 57 72 69 74 65 72  ng fts3SegWriter
38408 41 64 64 28 29 2e 20 49 66 20 73 75 63 63 65 73  Add(). If succes
38409 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20  sful, SQLITE_OK 
3840a 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20  is.** returned. 
3840b 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 53 51  Otherwise, an SQ
3840c 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e  Lite error code.
3840d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
3840e 74 73 33 53 65 67 57 72 69 74 65 72 46 6c 75 73  ts3SegWriterFlus
3840f 68 28 0a 20 20 46 74 73 33 54 61 62 6c 65 20 2a  h(.  Fts3Table *
38410 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
38411 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
38412 74 61 62 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  table handle */.
38413 20 20 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20    SegmentWriter 
38414 2a 70 57 72 69 74 65 72 2c 20 20 20 20 20 20 20  *pWriter,       
38415 20 20 2f 2a 20 53 65 67 6d 65 6e 74 57 72 69 74    /* SegmentWrit
38416 65 72 20 74 6f 20 66 6c 75 73 68 20 74 6f 20 74  er to flush to t
38417 68 65 20 64 62 20 2a 2f 0a 20 20 69 6e 74 20 69  he db */.  int i
38418 4c 65 76 65 6c 2c 20 20 20 20 20 20 20 20 20 20  Level,          
38419 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
3841a 6c 75 65 20 66 6f 72 20 27 6c 65 76 65 6c 27 20  lue for 'level' 
3841b 63 6f 6c 75 6d 6e 20 6f 66 20 25 5f 73 65 67 64  column of %_segd
3841c 69 72 20 2a 2f 0a 20 20 69 6e 74 20 69 49 64 78  ir */.  int iIdx
3841d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3841e 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
3841f 20 66 6f 72 20 27 69 64 78 27 20 63 6f 6c 75 6d   for 'idx' colum
38420 6e 20 6f 66 20 25 5f 73 65 67 64 69 72 20 2a 2f  n of %_segdir */
38421 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  .){.  int rc;   
38422 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38423 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
38424 63 6f 64 65 20 2a 2f 0a 20 20 69 66 28 20 70 57  code */.  if( pW
38425 72 69 74 65 72 2d 3e 70 54 72 65 65 20 29 7b 0a  riter->pTree ){.
38426 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36      sqlite3_int6
38427 34 20 69 4c 61 73 74 20 3d 20 30 3b 20 20 20 20  4 iLast = 0;    
38428 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 62 6c 6f    /* Largest blo
38429 63 6b 20 69 64 20 77 72 69 74 74 65 6e 20 74 6f  ck id written to
3842a 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20   database */.   
3842b 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
3842c 4c 61 73 74 4c 65 61 66 3b 20 20 20 20 20 20 2f  LastLeaf;      /
3842d 2a 20 4c 61 72 67 65 73 74 20 6c 65 61 66 20 62  * Largest leaf b
3842e 6c 6f 63 6b 20 69 64 20 77 72 69 74 74 65 6e 20  lock id written 
3842f 74 6f 20 64 62 20 2a 2f 0a 20 20 20 20 63 68 61  to db */.    cha
38430 72 20 2a 7a 52 6f 6f 74 20 3d 20 4e 55 4c 4c 3b  r *zRoot = NULL;
38431 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
38432 69 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20  inter to buffer 
38433 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20  containing root 
38434 6e 6f 64 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20  node */.    int 
38435 6e 52 6f 6f 74 20 3d 20 30 3b 20 20 20 20 20 20  nRoot = 0;      
38436 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
38437 65 20 6f 66 20 62 75 66 66 65 72 20 7a 52 6f 6f  e of buffer zRoo
38438 74 20 2a 2f 0a 0a 20 20 20 20 69 4c 61 73 74 4c  t */..    iLastL
38439 65 61 66 20 3d 20 70 57 72 69 74 65 72 2d 3e 69  eaf = pWriter->i
3843a 46 72 65 65 3b 0a 20 20 20 20 72 63 20 3d 20 66  Free;.    rc = f
3843b 74 73 33 57 72 69 74 65 53 65 67 6d 65 6e 74 28  ts3WriteSegment(
3843c 70 2c 20 70 57 72 69 74 65 72 2d 3e 69 46 72 65  p, pWriter->iFre
3843d 65 2b 2b 2c 20 70 57 72 69 74 65 72 2d 3e 61 44  e++, pWriter->aD
3843e 61 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 6e 44  ata, pWriter->nD
3843f 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ata);.    if( rc
38440 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
38441 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 4e 6f       rc = fts3No
38442 64 65 57 72 69 74 65 28 70 2c 20 70 57 72 69 74  deWrite(p, pWrit
38443 65 72 2d 3e 70 54 72 65 65 2c 20 31 2c 0a 20 20  er->pTree, 1,.  
38444 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d          pWriter-
38445 3e 69 46 69 72 73 74 2c 20 70 57 72 69 74 65 72  >iFirst, pWriter
38446 2d 3e 69 46 72 65 65 2c 20 26 69 4c 61 73 74 2c  ->iFree, &iLast,
38447 20 26 7a 52 6f 6f 74 2c 20 26 6e 52 6f 6f 74 29   &zRoot, &nRoot)
38448 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
38449 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
3844a 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33  .      rc = fts3
3844b 57 72 69 74 65 53 65 67 64 69 72 28 0a 20 20 20  WriteSegdir(.   
3844c 20 20 20 20 20 20 20 70 2c 20 69 4c 65 76 65 6c         p, iLevel
3844d 2c 20 69 49 64 78 2c 20 70 57 72 69 74 65 72 2d  , iIdx, pWriter-
3844e 3e 69 46 69 72 73 74 2c 20 69 4c 61 73 74 4c 65  >iFirst, iLastLe
3844f 61 66 2c 20 69 4c 61 73 74 2c 20 7a 52 6f 6f 74  af, iLast, zRoot
38450 2c 20 6e 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a  , nRoot);.    }.
38451 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
38452 54 68 65 20 65 6e 74 69 72 65 20 74 72 65 65 20  The entire tree 
38453 66 69 74 73 20 6f 6e 20 74 68 65 20 72 6f 6f 74  fits on the root
38454 20 6e 6f 64 65 2e 20 57 72 69 74 65 20 69 74 20   node. Write it 
38455 74 6f 20 74 68 65 20 73 65 67 64 69 72 20 74 61  to the segdir ta
38456 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d  ble. */.    rc =
38457 20 66 74 73 33 57 72 69 74 65 53 65 67 64 69 72   fts3WriteSegdir
38458 28 0a 20 20 20 20 20 20 20 20 70 2c 20 69 4c 65  (.        p, iLe
38459 76 65 6c 2c 20 69 49 64 78 2c 20 30 2c 20 30 2c  vel, iIdx, 0, 0,
3845a 20 30 2c 20 70 57 72 69 74 65 72 2d 3e 61 44 61   0, pWriter->aDa
3845b 74 61 2c 20 70 57 72 69 74 65 72 2d 3e 6e 44 61  ta, pWriter->nDa
3845c 74 61 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ta);.  }.  retur
3845d 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
3845e 65 6c 65 61 73 65 20 61 6c 6c 20 6d 65 6d 6f 72  elease all memor
3845f 79 20 68 65 6c 64 20 62 79 20 74 68 65 20 53 65  y held by the Se
38460 67 6d 65 6e 74 57 72 69 74 65 72 20 6f 62 6a 65  gmentWriter obje
38461 63 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ct passed as the
38462 20 0a 2a 2a 20 66 69 72 73 74 20 61 72 67 75 6d   .** first argum
38463 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ent..*/.static v
38464 6f 69 64 20 66 74 73 33 53 65 67 57 72 69 74 65  oid fts3SegWrite
38465 72 46 72 65 65 28 53 65 67 6d 65 6e 74 57 72 69  rFree(SegmentWri
38466 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20  ter *pWriter){. 
38467 20 69 66 28 20 70 57 72 69 74 65 72 20 29 7b 0a   if( pWriter ){.
38468 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
38469 28 70 57 72 69 74 65 72 2d 3e 61 44 61 74 61 29  (pWriter->aData)
3846a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
3846b 65 65 28 70 57 72 69 74 65 72 2d 3e 7a 4d 61 6c  ee(pWriter->zMal
3846c 6c 6f 63 29 3b 0a 20 20 20 20 66 74 73 33 4e 6f  loc);.    fts3No
3846d 64 65 46 72 65 65 28 70 57 72 69 74 65 72 2d 3e  deFree(pWriter->
3846e 70 54 72 65 65 29 3b 0a 20 20 20 20 73 71 6c 69  pTree);.    sqli
3846f 74 65 33 5f 66 72 65 65 28 70 57 72 69 74 65 72  te3_free(pWriter
38470 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
38471 54 68 65 20 66 69 72 73 74 20 76 61 6c 75 65 20  The first value 
38472 69 6e 20 74 68 65 20 61 70 56 61 6c 5b 5d 20 61  in the apVal[] a
38473 72 72 61 79 20 69 73 20 61 73 73 75 6d 65 64 20  rray is assumed 
38474 74 6f 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e  to contain an in
38475 74 65 67 65 72 2e 0a 2a 2a 20 54 68 69 73 20 66  teger..** This f
38476 75 6e 63 74 69 6f 6e 20 74 65 73 74 73 20 69 66  unction tests if
38477 20 74 68 65 72 65 20 65 78 69 73 74 20 61 6e 79   there exist any
38478 20 64 6f 63 75 6d 65 6e 74 73 20 77 69 74 68 20   documents with 
38479 64 6f 63 69 64 20 76 61 6c 75 65 73 20 74 68 61  docid values tha
3847a 74 0a 2a 2a 20 61 72 65 20 64 69 66 66 65 72 65  t.** are differe
3847b 6e 74 20 66 72 6f 6d 20 74 68 61 74 20 69 6e 74  nt from that int
3847c 65 67 65 72 2e 20 69 2e 65 2e 20 69 66 20 64 65  eger. i.e. if de
3847d 6c 65 74 69 6e 67 20 74 68 65 20 64 6f 63 75 6d  leting the docum
3847e 65 6e 74 20 77 69 74 68 20 64 6f 63 69 64 0a 2a  ent with docid.*
3847f 2a 20 61 70 56 61 6c 5b 30 5d 20 77 6f 75 6c 64  * apVal[0] would
38480 20 6d 65 61 6e 20 74 68 65 20 46 54 53 33 20 74   mean the FTS3 t
38481 61 62 6c 65 20 77 65 72 65 20 65 6d 70 74 79 2e  able were empty.
38482 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73  .**.** If succes
38483 73 66 75 6c 2c 20 2a 70 69 73 45 6d 70 74 79 20  sful, *pisEmpty 
38484 69 73 20 73 65 74 20 74 6f 20 74 72 75 65 20 69  is set to true i
38485 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65  f the table is e
38486 6d 70 74 79 20 65 78 63 65 70 74 20 66 6f 72 0a  mpty except for.
38487 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 61 70 56 61  ** document apVa
38488 6c 5b 30 5d 2c 20 6f 72 20 66 61 6c 73 65 20 6f  l[0], or false o
38489 74 68 65 72 77 69 73 65 2c 20 61 6e 64 20 53 51  therwise, and SQ
3848a 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
3848b 6e 65 64 2e 20 49 66 20 61 6e 0a 2a 2a 20 65 72  ned. If an.** er
3848c 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53  ror occurs, an S
3848d 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65  QLite error code
3848e 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
3848f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
38490 49 73 45 6d 70 74 79 28 46 74 73 33 54 61 62 6c  IsEmpty(Fts3Tabl
38491 65 20 2a 70 2c 20 73 71 6c 69 74 65 33 5f 76 61  e *p, sqlite3_va
38492 6c 75 65 20 2a 2a 61 70 56 61 6c 2c 20 69 6e 74  lue **apVal, int
38493 20 2a 70 69 73 45 6d 70 74 79 29 7b 0a 20 20 73   *pisEmpty){.  s
38494 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
38495 6d 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  mt;.  int rc;.  
38496 72 63 20 3d 20 66 74 73 33 53 71 6c 53 74 6d 74  rc = fts3SqlStmt
38497 28 70 2c 20 53 51 4c 5f 49 53 5f 45 4d 50 54 59  (p, SQL_IS_EMPTY
38498 2c 20 26 70 53 74 6d 74 2c 20 61 70 56 61 6c 29  , &pStmt, apVal)
38499 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
3849a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
3849b 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c   SQLITE_ROW==sql
3849c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29  ite3_step(pStmt)
3849d 20 29 7b 0a 20 20 20 20 20 20 2a 70 69 73 45 6d   ){.      *pisEm
3849e 70 74 79 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  pty = sqlite3_co
3849f 6c 75 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c 20  lumn_int(pStmt, 
384a0 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  0);.    }.    rc
384a1 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
384a2 28 70 53 74 6d 74 29 3b 0a 20 20 7d 0a 20 20 72  (pStmt);.  }.  r
384a3 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
384a4 2a 2a 20 53 65 74 20 2a 70 6e 53 65 67 6d 65 6e  ** Set *pnSegmen
384a5 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  t to the number 
384a6 6f 66 20 73 65 67 6d 65 6e 74 73 20 6f 66 20 6c  of segments of l
384a7 65 76 65 6c 20 69 4c 65 76 65 6c 20 69 6e 20 74  evel iLevel in t
384a8 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  he database..**.
384a9 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
384aa 5f 4f 4b 20 69 66 20 73 75 63 63 65 73 73 66 75  _OK if successfu
384ab 6c 2c 20 6f 72 20 61 6e 20 53 51 4c 69 74 65 20  l, or an SQLite 
384ac 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 6e 6f  error code if no
384ad 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
384ae 20 66 74 73 33 53 65 67 6d 65 6e 74 43 6f 75 6e   fts3SegmentCoun
384af 74 28 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20  t(Fts3Table *p, 
384b0 69 6e 74 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20  int iLevel, int 
384b1 2a 70 6e 53 65 67 6d 65 6e 74 29 7b 0a 20 20 73  *pnSegment){.  s
384b2 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
384b3 6d 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  mt;.  int rc;.. 
384b4 20 61 73 73 65 72 74 28 20 69 4c 65 76 65 6c 3e   assert( iLevel>
384b5 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 66 74 73  =0 );.  rc = fts
384b6 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f  3SqlStmt(p, SQL_
384b7 53 45 4c 45 43 54 5f 4c 45 56 45 4c 5f 43 4f 55  SELECT_LEVEL_COU
384b8 4e 54 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a  NT, &pStmt, 0);.
384b9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
384ba 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
384bb 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
384bc 69 6e 74 28 70 53 74 6d 74 2c 20 31 2c 20 69 4c  int(pStmt, 1, iL
384bd 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 53 51 4c  evel);.  if( SQL
384be 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
384bf 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a  _step(pStmt) ){.
384c0 20 20 20 20 2a 70 6e 53 65 67 6d 65 6e 74 20 3d      *pnSegment =
384c1 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
384c2 69 6e 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  int(pStmt, 0);. 
384c3 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69   }.  return sqli
384c4 74 65 33 5f 72 65 73 65 74 28 70 53 74 6d 74 29  te3_reset(pStmt)
384c5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a  ;.}../*.** Set *
384c6 70 6e 53 65 67 6d 65 6e 74 20 74 6f 20 74 68 65  pnSegment to the
384c7 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
384c8 20 73 65 67 6d 65 6e 74 73 20 69 6e 20 74 68 65   segments in the
384c9 20 64 61 74 61 62 61 73 65 2e 20 53 65 74 0a 2a   database. Set.*
384ca 2a 20 2a 70 6e 4d 61 78 20 74 6f 20 74 68 65 20  * *pnMax to the 
384cb 6c 61 72 67 65 73 74 20 73 65 67 6d 65 6e 74 20  largest segment 
384cc 6c 65 76 65 6c 20 69 6e 20 74 68 65 20 64 61 74  level in the dat
384cd 61 62 61 73 65 20 28 73 65 67 6d 65 6e 74 20 6c  abase (segment l
384ce 65 76 65 6c 73 0a 2a 2a 20 61 72 65 20 73 74 6f  evels.** are sto
384cf 72 65 64 20 69 6e 20 74 68 65 20 27 6c 65 76 65  red in the 'leve
384d0 6c 27 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  l' column of the
384d1 20 25 5f 73 65 67 64 69 72 20 74 61 62 6c 65 29   %_segdir table)
384d2 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
384d3 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63 63  QLITE_OK if succ
384d4 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53 51  essful, or an SQ
384d5 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
384d6 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69  if not..*/.stati
384d7 63 20 69 6e 74 20 66 74 73 33 53 65 67 6d 65 6e  c int fts3Segmen
384d8 74 43 6f 75 6e 74 4d 61 78 28 46 74 73 33 54 61  tCountMax(Fts3Ta
384d9 62 6c 65 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 53  ble *p, int *pnS
384da 65 67 6d 65 6e 74 2c 20 69 6e 74 20 2a 70 6e 4d  egment, int *pnM
384db 61 78 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  ax){.  sqlite3_s
384dc 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 6e  tmt *pStmt;.  in
384dd 74 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 66 74  t rc;..  rc = ft
384de 73 33 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c  s3SqlStmt(p, SQL
384df 5f 53 45 4c 45 43 54 5f 53 45 47 44 49 52 5f 43  _SELECT_SEGDIR_C
384e0 4f 55 4e 54 5f 4d 41 58 2c 20 26 70 53 74 6d 74  OUNT_MAX, &pStmt
384e1 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  , 0);.  if( rc!=
384e2 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
384e3 72 6e 20 72 63 3b 0a 20 20 69 66 28 20 53 51 4c  rn rc;.  if( SQL
384e4 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
384e5 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a  _step(pStmt) ){.
384e6 20 20 20 20 2a 70 6e 53 65 67 6d 65 6e 74 20 3d      *pnSegment =
384e7 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
384e8 69 6e 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  int(pStmt, 0);. 
384e9 20 20 20 2a 70 6e 4d 61 78 20 3d 20 73 71 6c 69     *pnMax = sqli
384ea 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 70  te3_column_int(p
384eb 53 74 6d 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20  Stmt, 1);.  }.  
384ec 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72  return sqlite3_r
384ed 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  eset(pStmt);.}..
384ee 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
384ef 69 6f 6e 20 69 73 20 75 73 65 64 20 61 66 74 65  ion is used afte
384f0 72 20 6d 65 72 67 69 6e 67 20 6d 75 6c 74 69 70  r merging multip
384f1 6c 65 20 73 65 67 6d 65 6e 74 73 20 69 6e 74 6f  le segments into
384f2 20 61 20 73 69 6e 67 6c 65 20 6c 61 72 67 65 0a   a single large.
384f3 2a 2a 20 73 65 67 6d 65 6e 74 20 74 6f 20 64 65  ** segment to de
384f4 6c 65 74 65 20 74 68 65 20 6f 6c 64 2c 20 6e 6f  lete the old, no
384f5 77 20 72 65 64 75 6e 64 61 6e 74 2c 20 73 65 67  w redundant, seg
384f6 6d 65 6e 74 20 62 2d 74 72 65 65 73 2e 20 53 70  ment b-trees. Sp
384f7 65 63 69 66 69 63 61 6c 6c 79 2c 0a 2a 2a 20 69  ecifically,.** i
384f8 74 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 31 29 20 44  t:.** .**   1) D
384f9 65 6c 65 74 65 73 20 61 6c 6c 20 25 5f 73 65 67  eletes all %_seg
384fa 6d 65 6e 74 73 20 65 6e 74 72 69 65 73 20 66 6f  ments entries fo
384fb 72 20 74 68 65 20 73 65 67 6d 65 6e 74 73 20 61  r the segments a
384fc 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a  ssociated with .
384fd 2a 2a 20 20 20 20 20 20 65 61 63 68 20 6f 66 20  **      each of 
384fe 74 68 65 20 53 65 67 52 65 61 64 65 72 20 6f 62  the SegReader ob
384ff 6a 65 63 74 73 20 69 6e 20 74 68 65 20 61 72 72  jects in the arr
38500 61 79 20 70 61 73 73 65 64 20 61 73 20 74 68 65  ay passed as the
38501 20 74 68 69 72 64 20 0a 2a 2a 20 20 20 20 20 20   third .**      
38502 61 72 67 75 6d 65 6e 74 2c 20 61 6e 64 0a 2a 2a  argument, and.**
38503 0a 2a 2a 20 20 20 32 29 20 64 65 6c 65 74 65 73  .**   2) deletes
38504 20 61 6c 6c 20 25 5f 73 65 67 64 69 72 20 65 6e   all %_segdir en
38505 74 72 69 65 73 20 77 69 74 68 20 6c 65 76 65 6c  tries with level
38506 20 69 4c 65 76 65 6c 2c 20 6f 72 20 61 6c 6c 20   iLevel, or all 
38507 25 5f 73 65 67 64 69 72 0a 2a 2a 20 20 20 20 20  %_segdir.**     
38508 20 65 6e 74 72 69 65 73 20 72 65 67 61 72 64 6c   entries regardl
38509 65 73 73 20 6f 66 20 6c 65 76 65 6c 20 69 66 20  ess of level if 
3850a 28 69 4c 65 76 65 6c 3c 30 29 2e 0a 2a 2a 0a 2a  (iLevel<0)..**.*
3850b 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
3850c 65 74 75 72 6e 65 64 20 69 66 20 73 75 63 63 65  eturned if succe
3850d 73 73 66 75 6c 2c 20 6f 74 68 65 72 77 69 73 65  ssful, otherwise
3850e 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
3850f 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
38510 20 69 6e 74 20 66 74 73 33 44 65 6c 65 74 65 53   int fts3DeleteS
38511 65 67 64 69 72 28 0a 20 20 46 74 73 33 54 61 62  egdir(.  Fts3Tab
38512 6c 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  le *p,          
38513 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
38514 75 61 6c 20 74 61 62 6c 65 20 68 61 6e 64 6c 65  ual table handle
38515 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c   */.  int iLevel
38516 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
38517 20 20 20 20 20 20 2f 2a 20 4c 65 76 65 6c 20 6f        /* Level o
38518 66 20 25 5f 73 65 67 64 69 72 20 65 6e 74 72 69  f %_segdir entri
38519 65 73 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a  es to delete */.
3851a 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
3851b 2a 2a 61 70 53 65 67 6d 65 6e 74 2c 20 20 20 20  **apSegment,    
3851c 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 53 65    /* Array of Se
3851d 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74 73 20  gReader objects 
3851e 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 61 64 65 72  */.  int nReader
3851f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38520 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
38521 61 72 72 61 79 20 61 70 53 65 67 6d 65 6e 74 20  array apSegment 
38522 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  */.){.  int rc; 
38523 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38524 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
38525 6e 20 43 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  n Code */.  int 
38526 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
38527 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
38528 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
38529 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
3852a 6d 74 20 2a 70 44 65 6c 65 74 65 3b 20 20 20 20  mt *pDelete;    
3852b 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61        /* SQL sta
3852c 74 65 6d 65 6e 74 20 74 6f 20 64 65 6c 65 74 65  tement to delete
3852d 20 72 6f 77 73 20 2a 2f 0a 0a 20 20 72 63 20 3d   rows */..  rc =
3852e 20 66 74 73 33 53 71 6c 53 74 6d 74 28 70 2c 20   fts3SqlStmt(p, 
3852f 53 51 4c 5f 44 45 4c 45 54 45 5f 53 45 47 4d 45  SQL_DELETE_SEGME
38530 4e 54 53 5f 52 41 4e 47 45 2c 20 26 70 44 65 6c  NTS_RANGE, &pDel
38531 65 74 65 2c 20 30 29 3b 0a 20 20 66 6f 72 28 69  ete, 0);.  for(i
38532 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
38533 4b 20 26 26 20 69 3c 6e 52 65 61 64 65 72 3b 20  K && i<nReader; 
38534 69 2b 2b 29 7b 0a 20 20 20 20 46 74 73 33 53 65  i++){.    Fts3Se
38535 67 52 65 61 64 65 72 20 2a 70 53 65 67 6d 65 6e  gReader *pSegmen
38536 74 20 3d 20 61 70 53 65 67 6d 65 6e 74 5b 69 5d  t = apSegment[i]
38537 3b 0a 20 20 20 20 69 66 28 20 70 53 65 67 6d 65  ;.    if( pSegme
38538 6e 74 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 20  nt->iStartBlock 
38539 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
3853a 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 44 65 6c  _bind_int64(pDel
3853b 65 74 65 2c 20 31 2c 20 70 53 65 67 6d 65 6e 74  ete, 1, pSegment
3853c 2d 3e 69 53 74 61 72 74 42 6c 6f 63 6b 29 3b 0a  ->iStartBlock);.
3853d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69        sqlite3_bi
3853e 6e 64 5f 69 6e 74 36 34 28 70 44 65 6c 65 74 65  nd_int64(pDelete
3853f 2c 20 32 2c 20 70 53 65 67 6d 65 6e 74 2d 3e 69  , 2, pSegment->i
38540 45 6e 64 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20  EndBlock);.     
38541 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 44   sqlite3_step(pD
38542 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 72 63  elete);.      rc
38543 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
38544 28 70 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d  (pDelete);.    }
38545 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53  .  }.  if( rc!=S
38546 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
38547 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
38548 20 20 69 66 28 20 69 4c 65 76 65 6c 3e 3d 30 20    if( iLevel>=0 
38549 29 7b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  ){.    rc = fts3
3854a 53 71 6c 53 74 6d 74 28 70 2c 20 53 51 4c 5f 44  SqlStmt(p, SQL_D
3854b 45 4c 45 54 45 5f 53 45 47 44 49 52 5f 42 59 5f  ELETE_SEGDIR_BY_
3854c 4c 45 56 45 4c 2c 20 26 70 44 65 6c 65 74 65 2c  LEVEL, &pDelete,
3854d 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d   0);.    if( rc=
3854e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3854f 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64      sqlite3_bind
38550 5f 69 6e 74 28 70 44 65 6c 65 74 65 2c 20 31 2c  _int(pDelete, 1,
38551 20 69 4c 65 76 65 6c 29 3b 0a 20 20 20 20 20 20   iLevel);.      
38552 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 44 65  sqlite3_step(pDe
38553 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 72 63 20  lete);.      rc 
38554 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
38555 70 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d 0a  pDelete);.    }.
38556 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
38557 3d 20 66 74 73 33 53 71 6c 45 78 65 63 28 70 2c  = fts3SqlExec(p,
38558 20 53 51 4c 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f   SQL_DELETE_ALL_
38559 53 45 47 44 49 52 2c 20 30 29 3b 0a 20 20 7d 0a  SEGDIR, 0);.  }.
3855a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3855b 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  ./*.** When this
3855c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
3855d 6c 65 64 2c 20 62 75 66 66 65 72 20 2a 70 70 4c  led, buffer *ppL
3855e 69 73 74 20 28 73 69 7a 65 20 2a 70 6e 4c 69 73  ist (size *pnLis
3855f 74 20 62 79 74 65 73 29 20 63 6f 6e 74 61 69 6e  t bytes) contain
38560 73 20 0a 2a 2a 20 61 20 70 6f 73 69 74 69 6f 6e  s .** a position
38561 20 6c 69 73 74 20 74 68 61 74 20 6d 61 79 20 28   list that may (
38562 6f 72 20 6d 61 79 20 6e 6f 74 29 20 66 65 61 74  or may not) feat
38563 75 72 65 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6c  ure multiple col
38564 75 6d 6e 73 2e 20 54 68 69 73 0a 2a 2a 20 66 75  umns. This.** fu
38565 6e 63 74 69 6f 6e 20 61 64 6a 75 73 74 73 20 74  nction adjusts t
38566 68 65 20 70 6f 69 6e 74 65 72 20 2a 70 70 4c 69  he pointer *ppLi
38567 73 74 20 61 6e 64 20 74 68 65 20 6c 65 6e 67 74  st and the lengt
38568 68 20 2a 70 6e 4c 69 73 74 20 73 6f 20 74 68 61  h *pnList so tha
38569 74 20 74 68 65 79 0a 2a 2a 20 69 64 65 6e 74 69  t they.** identi
3856a 66 79 20 74 68 65 20 73 75 62 73 65 74 20 6f 66  fy the subset of
3856b 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 6c 69   the position li
3856c 73 74 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  st that correspo
3856d 6e 64 73 20 74 6f 20 63 6f 6c 75 6d 6e 20 69 43  nds to column iC
3856e 6f 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ol..**.** If the
3856f 72 65 20 61 72 65 20 6e 6f 20 65 6e 74 72 69 65  re are no entrie
38570 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 70  s in the input p
38571 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72  osition list for
38572 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 2c 20 74 68   column iCol, th
38573 65 6e 0a 2a 2a 20 2a 70 6e 4c 69 73 74 20 69 73  en.** *pnList is
38574 20 73 65 74 20 74 6f 20 7a 65 72 6f 20 62 65 66   set to zero bef
38575 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
38576 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74  /.static void ft
38577 73 33 43 6f 6c 75 6d 6e 46 69 6c 74 65 72 28 0a  s3ColumnFilter(.
38578 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20    int iCol,     
38579 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3857a 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 74 6f 20 66    /* Column to f
3857b 69 6c 74 65 72 20 6f 6e 20 2a 2f 0a 20 20 63 68  ilter on */.  ch
3857c 61 72 20 2a 2a 70 70 4c 69 73 74 2c 20 20 20 20  ar **ppList,    
3857d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3857e 20 49 4e 2f 4f 55 54 3a 20 50 6f 69 6e 74 65 72   IN/OUT: Pointer
3857f 20 74 6f 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73   to position lis
38580 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4c 69  t */.  int *pnLi
38581 73 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  st              
38582 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54         /* IN/OUT
38583 3a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65 72  : Size of buffer
38584 20 2a 70 70 4c 69 73 74 20 69 6e 20 62 79 74 65   *ppList in byte
38585 73 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a  s */.){.  char *
38586 70 4c 69 73 74 20 3d 20 2a 70 70 4c 69 73 74 3b  pList = *ppList;
38587 0a 20 20 69 6e 74 20 6e 4c 69 73 74 20 3d 20 2a  .  int nList = *
38588 70 6e 4c 69 73 74 3b 0a 20 20 63 68 61 72 20 2a  pnList;.  char *
38589 70 45 6e 64 20 3d 20 26 70 4c 69 73 74 5b 6e 4c  pEnd = &pList[nL
3858a 69 73 74 5d 3b 0a 20 20 69 6e 74 20 69 43 75 72  ist];.  int iCur
3858b 72 65 6e 74 20 3d 20 30 3b 0a 20 20 63 68 61 72  rent = 0;.  char
3858c 20 2a 70 20 3d 20 70 4c 69 73 74 3b 0a 0a 20 20   *p = pList;..  
3858d 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 30 20  assert( iCol>=0 
3858e 29 3b 0a 20 20 77 68 69 6c 65 28 20 31 20 29 7b  );.  while( 1 ){
3858f 0a 20 20 20 20 63 68 61 72 20 63 20 3d 20 30 3b  .    char c = 0;
38590 0a 20 20 20 20 77 68 69 6c 65 28 20 70 3c 70 45  .    while( p<pE
38591 6e 64 20 26 26 20 28 63 20 7c 20 2a 70 29 26 30  nd && (c | *p)&0
38592 78 46 45 20 29 20 63 20 3d 20 2a 70 2b 2b 20 26  xFE ) c = *p++ &
38593 20 30 78 38 30 3b 0a 20 20 0a 20 20 20 20 69 66   0x80;.  .    if
38594 28 20 69 43 6f 6c 3d 3d 69 43 75 72 72 65 6e 74  ( iCol==iCurrent
38595 20 29 7b 0a 20 20 20 20 20 20 6e 4c 69 73 74 20   ){.      nList 
38596 3d 20 28 69 6e 74 29 28 70 20 2d 20 70 4c 69 73  = (int)(p - pLis
38597 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  t);.      break;
38598 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 4c 69 73  .    }..    nLis
38599 74 20 2d 3d 20 28 69 6e 74 29 28 70 20 2d 20 70  t -= (int)(p - p
3859a 4c 69 73 74 29 3b 0a 20 20 20 20 70 4c 69 73 74  List);.    pList
3859b 20 3d 20 70 3b 0a 20 20 20 20 69 66 28 20 6e 4c   = p;.    if( nL
3859c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ist==0 ){.      
3859d 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
3859e 20 70 20 3d 20 26 70 4c 69 73 74 5b 31 5d 3b 0a   p = &pList[1];.
3859f 20 20 20 20 70 20 2b 3d 20 73 71 6c 69 74 65 33      p += sqlite3
385a0 46 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  Fts3GetVarint32(
385a1 70 2c 20 26 69 43 75 72 72 65 6e 74 29 3b 0a 20  p, &iCurrent);. 
385a2 20 7d 0a 0a 20 20 2a 70 70 4c 69 73 74 20 3d 20   }..  *ppList = 
385a3 70 4c 69 73 74 3b 0a 20 20 2a 70 6e 4c 69 73 74  pList;.  *pnList
385a4 20 3d 20 6e 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a   = nList;.}../*.
385a5 2a 2a 20 73 71 6c 69 74 65 33 46 74 73 33 53 65  ** sqlite3Fts3Se
385a6 67 52 65 61 64 65 72 49 74 65 72 61 74 65 28 29  gReaderIterate()
385a7 20 63 61 6c 6c 62 61 63 6b 20 75 73 65 64 20 77   callback used w
385a8 68 65 6e 20 6d 65 72 67 69 6e 67 20 6d 75 6c 74  hen merging mult
385a9 69 70 6c 65 20 0a 2a 2a 20 73 65 67 6d 65 6e 74  iple .** segment
385aa 73 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 69  s to create a si
385ab 6e 67 6c 65 2c 20 6c 61 72 67 65 72 20 73 65 67  ngle, larger seg
385ac 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ment..*/.static 
385ad 69 6e 74 20 66 74 73 33 4d 65 72 67 65 43 61 6c  int fts3MergeCal
385ae 6c 62 61 63 6b 28 0a 20 20 46 74 73 33 54 61 62  lback(.  Fts3Tab
385af 6c 65 20 2a 70 2c 0a 20 20 76 6f 69 64 20 2a 70  le *p,.  void *p
385b0 43 6f 6e 74 65 78 74 2c 0a 20 20 63 68 61 72 20  Context,.  char 
385b1 2a 7a 54 65 72 6d 2c 0a 20 20 69 6e 74 20 6e 54  *zTerm,.  int nT
385b2 65 72 6d 2c 0a 20 20 63 68 61 72 20 2a 61 44 6f  erm,.  char *aDo
385b3 63 6c 69 73 74 2c 0a 20 20 69 6e 74 20 6e 44 6f  clist,.  int nDo
385b4 63 6c 69 73 74 0a 29 7b 0a 20 20 53 65 67 6d 65  clist.){.  Segme
385b5 6e 74 57 72 69 74 65 72 20 2a 2a 70 70 57 20 3d  ntWriter **ppW =
385b6 20 28 53 65 67 6d 65 6e 74 57 72 69 74 65 72 20   (SegmentWriter 
385b7 2a 2a 29 70 43 6f 6e 74 65 78 74 3b 0a 20 20 72  **)pContext;.  r
385b8 65 74 75 72 6e 20 66 74 73 33 53 65 67 57 72 69  eturn fts3SegWri
385b9 74 65 72 41 64 64 28 70 2c 20 70 70 57 2c 20 31  terAdd(p, ppW, 1
385ba 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , zTerm, nTerm, 
385bb 61 44 6f 63 6c 69 73 74 2c 20 6e 44 6f 63 6c 69  aDoclist, nDocli
385bc 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  st);.}../*.** Th
385bd 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
385be 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20 74  sed to iterate t
385bf 68 72 6f 75 67 68 20 61 20 63 6f 6e 74 69 67 75  hrough a contigu
385c0 6f 75 73 20 73 65 74 20 6f 66 20 74 65 72 6d 73  ous set of terms
385c1 20 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 74   .** stored in t
385c2 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64  he full-text ind
385c3 65 78 2e 20 49 74 20 6d 65 72 67 65 73 20 64 61  ex. It merges da
385c4 74 61 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  ta contained in 
385c5 6f 6e 65 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20  one or .** more 
385c6 73 65 67 6d 65 6e 74 73 20 74 6f 20 73 75 70 70  segments to supp
385c7 6f 72 74 20 74 68 69 73 2e 0a 2a 2a 0a 2a 2a 20  ort this..**.** 
385c8 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
385c9 65 6e 74 20 69 73 20 70 61 73 73 65 64 20 61 6e  ent is passed an
385ca 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65   array of pointe
385cb 72 73 20 74 6f 20 53 65 67 52 65 61 64 65 72 20  rs to SegReader 
385cc 6f 62 6a 65 63 74 73 0a 2a 2a 20 61 6c 6c 6f 63  objects.** alloc
385cd 61 74 65 64 20 77 69 74 68 20 73 71 6c 69 74 65  ated with sqlite
385ce 33 46 74 73 33 53 65 67 52 65 61 64 65 72 4e 65  3Fts3SegReaderNe
385cf 77 28 29 2e 20 54 68 69 73 20 66 75 6e 63 74 69  w(). This functi
385d0 6f 6e 20 6d 65 72 67 65 73 20 74 68 65 20 72 61  on merges the ra
385d1 6e 67 65 20 0a 2a 2a 20 6f 66 20 74 65 72 6d 73  nge .** of terms
385d2 20 73 65 6c 65 63 74 65 64 20 62 79 20 65 61 63   selected by eac
385d3 68 20 53 65 67 52 65 61 64 65 72 2e 20 49 66 20  h SegReader. If 
385d4 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 69 73  a single term is
385d5 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a 20 6d   present in.** m
385d6 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 65 67  ore than one seg
385d7 6d 65 6e 74 2c 20 74 68 65 20 61 73 73 6f 63 69  ment, the associ
385d8 61 74 65 64 20 64 6f 63 6c 69 73 74 73 20 61 72  ated doclists ar
385d9 65 20 6d 65 72 67 65 64 2e 20 46 6f 72 20 65 61  e merged. For ea
385da 63 68 0a 2a 2a 20 74 65 72 6d 20 61 6e 64 20 28  ch.** term and (
385db 70 6f 73 73 69 62 6c 79 20 6d 65 72 67 65 64 29  possibly merged)
385dc 20 64 6f 63 6c 69 73 74 20 69 6e 20 74 68 65 20   doclist in the 
385dd 6d 65 72 67 65 64 20 72 61 6e 67 65 2c 20 74 68  merged range, th
385de 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75  e callback.** fu
385df 6e 63 74 69 6f 6e 20 78 46 75 6e 63 20 69 73 20  nction xFunc is 
385e0 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 69 74 73  invoked with its
385e1 20 61 72 67 75 6d 65 6e 74 73 20 73 65 74 20 61   arguments set a
385e2 73 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a 0a 2a 2a  s follows..**.**
385e3 20 20 20 61 72 67 20 30 3a 20 43 6f 70 79 20 6f     arg 0: Copy o
385e4 66 20 27 70 27 20 70 61 72 61 6d 65 74 65 72 20  f 'p' parameter 
385e5 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66  passed to this f
385e6 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 61 72 67  unction.**   arg
385e7 20 31 3a 20 43 6f 70 79 20 6f 66 20 27 70 43 6f   1: Copy of 'pCo
385e8 6e 74 65 78 74 27 20 70 61 72 61 6d 65 74 65 72  ntext' parameter
385e9 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
385ea 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 61 72  function.**   ar
385eb 67 20 32 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20  g 2: Pointer to 
385ec 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
385ed 67 20 74 65 72 6d 0a 2a 2a 20 20 20 61 72 67 20  g term.**   arg 
385ee 33 3a 20 53 69 7a 65 20 6f 66 20 61 72 67 20 32  3: Size of arg 2
385ef 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73   buffer in bytes
385f0 0a 2a 2a 20 20 20 61 72 67 20 34 3a 20 50 6f 69  .**   arg 4: Poi
385f1 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 63  nter to buffer c
385f2 6f 6e 74 61 69 6e 69 6e 67 20 64 6f 63 6c 69 73  ontaining doclis
385f3 74 0a 2a 2a 20 20 20 61 72 67 20 35 3a 20 53 69  t.**   arg 5: Si
385f4 7a 65 20 6f 66 20 61 72 67 20 32 20 62 75 66 66  ze of arg 2 buff
385f5 65 72 20 69 6e 20 62 79 74 65 73 0a 2a 2a 0a 2a  er in bytes.**.*
385f6 2a 20 54 68 65 20 34 74 68 20 61 72 67 75 6d 65  * The 4th argume
385f7 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74  nt to this funct
385f8 69 6f 6e 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ion is a pointer
385f9 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65 20   to a structure 
385fa 6f 66 20 74 79 70 65 0a 2a 2a 20 46 74 73 33 53  of type.** Fts3S
385fb 65 67 46 69 6c 74 65 72 2c 20 64 65 66 69 6e 65  egFilter, define
385fc 64 20 69 6e 20 66 74 73 33 49 6e 74 2e 68 2e 20  d in fts3Int.h. 
385fd 54 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  The contents of 
385fe 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a  this structure.*
385ff 2a 20 66 75 72 74 68 65 72 20 72 65 73 74 72 69  * further restri
38600 63 74 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20  ct the range of 
38601 74 65 72 6d 73 20 74 68 61 74 20 63 61 6c 6c 62  terms that callb
38602 61 63 6b 73 20 61 72 65 20 6d 61 64 65 20 66 6f  acks are made fo
38603 72 20 61 6e 64 0a 2a 2a 20 6d 6f 64 69 66 79 20  r and.** modify 
38604 74 68 65 20 62 65 68 61 76 69 6f 75 72 20 6f 66  the behaviour of
38605 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20   this function. 
38606 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f  See comments abo
38607 76 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  ve structure.** 
38608 64 65 66 69 6e 69 74 69 6f 6e 20 66 6f 72 20 64  definition for d
38609 65 74 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54  etails..*/.SQLIT
3860a 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
3860b 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64  lite3Fts3SegRead
3860c 65 72 49 74 65 72 61 74 65 28 0a 20 20 46 74 73  erIterate(.  Fts
3860d 33 54 61 62 6c 65 20 2a 70 2c 20 20 20 20 20 20  3Table *p,      
3860e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3860f 56 69 72 74 75 61 6c 20 74 61 62 6c 65 20 68 61  Virtual table ha
38610 6e 64 6c 65 20 2a 2f 0a 20 20 46 74 73 33 53 65  ndle */.  Fts3Se
38611 67 52 65 61 64 65 72 20 2a 2a 61 70 53 65 67 6d  gReader **apSegm
38612 65 6e 74 2c 20 20 20 20 20 20 2f 2a 20 41 72 72  ent,      /* Arr
38613 61 79 20 6f 66 20 46 74 73 33 53 65 67 52 65 61  ay of Fts3SegRea
38614 64 65 72 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20  der objects */. 
38615 20 69 6e 74 20 6e 53 65 67 6d 65 6e 74 2c 20 20   int nSegment,  
38616 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38617 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 70 53 65   /* Size of apSe
38618 67 6d 65 6e 74 20 61 72 72 61 79 20 2a 2f 0a 20  gment array */. 
38619 20 46 74 73 33 53 65 67 46 69 6c 74 65 72 20 2a   Fts3SegFilter *
3861a 70 46 69 6c 74 65 72 2c 20 20 20 20 20 20 20 20  pFilter,        
3861b 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 73   /* Restrictions
3861c 20 6f 6e 20 72 61 6e 67 65 20 6f 66 20 69 74 65   on range of ite
3861d 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ration */.  int 
3861e 28 2a 78 46 75 6e 63 29 28 46 74 73 33 54 61 62  (*xFunc)(Fts3Tab
3861f 6c 65 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 68  le *, void *, ch
38620 61 72 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20  ar *, int, char 
38621 2a 2c 20 69 6e 74 29 2c 20 20 2f 2a 20 43 61 6c  *, int),  /* Cal
38622 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20  lback */.  void 
38623 2a 70 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20  *pContext       
38624 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61             /* Ca
38625 6c 6c 62 61 63 6b 20 63 6f 6e 74 65 78 74 20 28  llback context (
38626 32 6e 64 20 61 72 67 75 6d 65 6e 74 29 20 2a 2f  2nd argument) */
38627 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20  .){.  int i;    
38628 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38629 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
3862a 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20  r variable */.  
3862b 63 68 61 72 20 2a 61 42 75 66 66 65 72 20 3d 20  char *aBuffer = 
3862c 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
3862d 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 6d 65 72  /* Buffer to mer
3862e 67 65 20 64 6f 63 6c 69 73 74 73 20 69 6e 20 2a  ge doclists in *
3862f 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d  /.  int nAlloc =
38630 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
38631 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64      /* Allocated
38632 20 73 69 7a 65 20 6f 66 20 61 42 75 66 66 65 72   size of aBuffer
38633 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74   buffer */.  int
38634 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
38635 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38636 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a  Return code */..
38637 20 20 69 6e 74 20 69 73 49 67 6e 6f 72 65 45 6d    int isIgnoreEm
38638 70 74 79 20 3d 20 20 28 70 46 69 6c 74 65 72 2d  pty =  (pFilter-
38639 3e 66 6c 61 67 73 20 26 20 46 54 53 33 5f 53 45  >flags & FTS3_SE
3863a 47 4d 45 4e 54 5f 49 47 4e 4f 52 45 5f 45 4d 50  GMENT_IGNORE_EMP
3863b 54 59 29 3b 0a 20 20 69 6e 74 20 69 73 52 65 71  TY);.  int isReq
3863c 75 69 72 65 50 6f 73 20 3d 20 20 20 28 70 46 69  uirePos =   (pFi
3863d 6c 74 65 72 2d 3e 66 6c 61 67 73 20 26 20 46 54  lter->flags & FT
3863e 53 33 5f 53 45 47 4d 45 4e 54 5f 52 45 51 55 49  S3_SEGMENT_REQUI
3863f 52 45 5f 50 4f 53 29 3b 0a 20 20 69 6e 74 20 69  RE_POS);.  int i
38640 73 43 6f 6c 46 69 6c 74 65 72 20 3d 20 20 20 20  sColFilter =    
38641 28 70 46 69 6c 74 65 72 2d 3e 66 6c 61 67 73 20  (pFilter->flags 
38642 26 20 46 54 53 33 5f 53 45 47 4d 45 4e 54 5f 43  & FTS3_SEGMENT_C
38643 4f 4c 55 4d 4e 5f 46 49 4c 54 45 52 29 3b 0a 20  OLUMN_FILTER);. 
38644 20 69 6e 74 20 69 73 50 72 65 66 69 78 20 3d 20   int isPrefix = 
38645 20 20 20 20 20 20 28 70 46 69 6c 74 65 72 2d 3e        (pFilter->
38646 66 6c 61 67 73 20 26 20 46 54 53 33 5f 53 45 47  flags & FTS3_SEG
38647 4d 45 4e 54 5f 50 52 45 46 49 58 29 3b 0a 0a 20  MENT_PREFIX);.. 
38648 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
38649 20 7a 65 72 6f 20 73 65 67 6d 65 6e 74 73 2c 20   zero segments, 
3864a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
3864b 20 61 20 6e 6f 2d 6f 70 2e 20 54 68 69 73 20 73   a no-op. This s
3864c 63 65 6e 61 72 69 6f 0a 20 20 2a 2a 20 63 6f 6d  cenario.  ** com
3864d 65 73 20 61 62 6f 75 74 20 6f 6e 6c 79 20 77 68  es about only wh
3864e 65 6e 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20  en reading from 
3864f 61 6e 20 65 6d 70 74 79 20 64 61 74 61 62 61 73  an empty databas
38650 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53  e..  */.  if( nS
38651 65 67 6d 65 6e 74 3d 3d 30 20 29 20 67 6f 74 6f  egment==0 ) goto
38652 20 66 69 6e 69 73 68 65 64 3b 0a 0a 20 20 2f 2a   finished;..  /*
38653 20 49 66 20 74 68 65 20 46 74 73 33 53 65 67 46   If the Fts3SegF
38654 69 6c 74 65 72 20 64 65 66 69 6e 65 73 20 61 20  ilter defines a 
38655 73 70 65 63 69 66 69 63 20 74 65 72 6d 20 28 6f  specific term (o
38656 72 20 74 65 72 6d 20 70 72 65 66 69 78 29 20 74  r term prefix) t
38657 6f 20 73 65 61 72 63 68 20 0a 20 20 2a 2a 20 66  o search .  ** f
38658 6f 72 2c 20 74 68 65 6e 20 61 64 76 61 6e 63 65  or, then advance
38659 20 65 61 63 68 20 73 65 67 6d 65 6e 74 20 69 74   each segment it
3865a 65 72 61 74 6f 72 20 75 6e 74 69 6c 20 69 74 20  erator until it 
3865b 70 6f 69 6e 74 73 20 74 6f 20 61 20 74 65 72 6d  points to a term
3865c 20 6f 66 0a 20 20 2a 2a 20 65 71 75 61 6c 20 6f   of.  ** equal o
3865d 72 20 67 72 65 61 74 65 72 20 76 61 6c 75 65 20  r greater value 
3865e 74 68 61 6e 20 74 68 65 20 73 70 65 63 69 66 69  than the specifi
3865f 65 64 20 74 65 72 6d 2e 20 54 68 69 73 20 70 72  ed term. This pr
38660 65 76 65 6e 74 73 20 6d 61 6e 79 0a 20 20 2a 2a  events many.  **
38661 20 75 6e 6e 65 63 65 73 73 61 72 79 20 6d 65 72   unnecessary mer
38662 67 65 2f 73 6f 72 74 20 6f 70 65 72 61 74 69 6f  ge/sort operatio
38663 6e 73 20 66 6f 72 20 74 68 65 20 63 61 73 65 20  ns for the case 
38664 77 68 65 72 65 20 73 69 6e 67 6c 65 20 73 65 67  where single seg
38665 6d 65 6e 74 0a 20 20 2a 2a 20 62 2d 74 72 65 65  ment.  ** b-tree
38666 20 6c 65 61 66 20 6e 6f 64 65 73 20 63 6f 6e 74   leaf nodes cont
38667 61 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  ain more than on
38668 65 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69  e term..  */.  i
38669 66 28 20 70 46 69 6c 74 65 72 2d 3e 7a 54 65 72  f( pFilter->zTer
3866a 6d 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 54 65  m ){.    int nTe
3866b 72 6d 20 3d 20 70 46 69 6c 74 65 72 2d 3e 6e 54  rm = pFilter->nT
3866c 65 72 6d 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  erm;.    const c
3866d 68 61 72 20 2a 7a 54 65 72 6d 20 3d 20 70 46 69  har *zTerm = pFi
3866e 6c 74 65 72 2d 3e 7a 54 65 72 6d 3b 0a 20 20 20  lter->zTerm;.   
3866f 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 65 67   for(i=0; i<nSeg
38670 6d 65 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ment; i++){.    
38671 20 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20    Fts3SegReader 
38672 2a 70 53 65 67 20 3d 20 61 70 53 65 67 6d 65 6e  *pSeg = apSegmen
38673 74 5b 69 5d 3b 0a 20 20 20 20 20 20 77 68 69 6c  t[i];.      whil
38674 65 28 20 66 74 73 33 53 65 67 52 65 61 64 65 72  e( fts3SegReader
38675 54 65 72 6d 43 6d 70 28 70 53 65 67 2c 20 7a 54  TermCmp(pSeg, zT
38676 65 72 6d 2c 20 6e 54 65 72 6d 29 3c 30 20 29 7b  erm, nTerm)<0 ){
38677 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74  .        rc = ft
38678 73 33 53 65 67 52 65 61 64 65 72 4e 65 78 74 28  s3SegReaderNext(
38679 70 53 65 67 29 3b 0a 20 20 20 20 20 20 20 20 69  pSeg);.        i
3867a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3867b 20 29 20 67 6f 74 6f 20 66 69 6e 69 73 68 65 64   ) goto finished
3867c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
3867d 20 20 7d 0a 0a 20 20 66 74 73 33 53 65 67 52 65    }..  fts3SegRe
3867e 61 64 65 72 53 6f 72 74 28 61 70 53 65 67 6d 65  aderSort(apSegme
3867f 6e 74 2c 20 6e 53 65 67 6d 65 6e 74 2c 20 6e 53  nt, nSegment, nS
38680 65 67 6d 65 6e 74 2c 20 66 74 73 33 53 65 67 52  egment, fts3SegR
38681 65 61 64 65 72 43 6d 70 29 3b 0a 20 20 77 68 69  eaderCmp);.  whi
38682 6c 65 28 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d  le( apSegment[0]
38683 2d 3e 61 4e 6f 64 65 20 29 7b 0a 20 20 20 20 69  ->aNode ){.    i
38684 6e 74 20 6e 54 65 72 6d 20 3d 20 61 70 53 65 67  nt nTerm = apSeg
38685 6d 65 6e 74 5b 30 5d 2d 3e 6e 54 65 72 6d 3b 0a  ment[0]->nTerm;.
38686 20 20 20 20 63 68 61 72 20 2a 7a 54 65 72 6d 20      char *zTerm 
38687 3d 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d 2d 3e  = apSegment[0]->
38688 7a 54 65 72 6d 3b 0a 20 20 20 20 69 6e 74 20 6e  zTerm;.    int n
38689 4d 65 72 67 65 20 3d 20 31 3b 0a 0a 20 20 20 20  Merge = 1;..    
3868a 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  /* If this is a 
3868b 70 72 65 66 69 78 2d 73 65 61 72 63 68 2c 20 61  prefix-search, a
3868c 6e 64 20 69 66 20 74 68 65 20 74 65 72 6d 20 74  nd if the term t
3868d 68 61 74 20 61 70 53 65 67 6d 65 6e 74 5b 30 5d  hat apSegment[0]
3868e 20 70 6f 69 6e 74 73 0a 20 20 20 20 2a 2a 20 74   points.    ** t
3868f 6f 20 64 6f 65 73 20 6e 6f 74 20 73 68 61 72 65  o does not share
38690 20 61 20 73 75 66 66 69 78 20 77 69 74 68 20 70   a suffix with p
38691 46 69 6c 74 65 72 2d 3e 7a 54 65 72 6d 2f 6e 54  Filter->zTerm/nT
38692 65 72 6d 2c 20 74 68 65 6e 20 61 6c 6c 20 0a 20  erm, then all . 
38693 20 20 20 2a 2a 20 72 65 71 75 69 72 65 64 20 63     ** required c
38694 61 6c 6c 62 61 63 6b 73 20 68 61 76 65 20 62 65  allbacks have be
38695 65 6e 20 6d 61 64 65 2e 20 49 6e 20 74 68 69 73  en made. In this
38696 20 63 61 73 65 20 65 78 69 74 20 65 61 72 6c 79   case exit early
38697 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
38698 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68  Similarly, if th
38699 69 73 20 69 73 20 61 20 73 65 61 72 63 68 20 66  is is a search f
3869a 6f 72 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63  or an exact matc
3869b 68 2c 20 61 6e 64 20 74 68 65 20 66 69 72 73 74  h, and the first
3869c 20 74 65 72 6d 0a 20 20 20 20 2a 2a 20 6f 66 20   term.    ** of 
3869d 73 65 67 6d 65 6e 74 20 61 70 53 65 67 6d 65 6e  segment apSegmen
3869e 74 5b 30 5d 20 69 73 20 6e 6f 74 20 61 20 6d 61  t[0] is not a ma
3869f 74 63 68 2c 20 65 78 69 74 20 65 61 72 6c 79 2e  tch, exit early.
386a0 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
386a1 70 46 69 6c 74 65 72 2d 3e 7a 54 65 72 6d 20 29  pFilter->zTerm )
386a2 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 54 65 72  {.      if( nTer
386a3 6d 3c 70 46 69 6c 74 65 72 2d 3e 6e 54 65 72 6d  m<pFilter->nTerm
386a4 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 21 69 73   .       || (!is
386a5 50 72 65 66 69 78 20 26 26 20 6e 54 65 72 6d 3e  Prefix && nTerm>
386a6 70 46 69 6c 74 65 72 2d 3e 6e 54 65 72 6d 29 0a  pFilter->nTerm).
386a7 20 20 20 20 20 20 20 7c 7c 20 6d 65 6d 63 6d 70         || memcmp
386a8 28 7a 54 65 72 6d 2c 20 70 46 69 6c 74 65 72 2d  (zTerm, pFilter-
386a9 3e 7a 54 65 72 6d 2c 20 70 46 69 6c 74 65 72 2d  >zTerm, pFilter-
386aa 3e 6e 54 65 72 6d 29 20 0a 20 20 20 20 29 7b 0a  >nTerm) .    ){.
386ab 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e          goto fin
386ac 69 73 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20  ished;.      }. 
386ad 20 20 20 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28     }..    while(
386ae 20 6e 4d 65 72 67 65 3c 6e 53 65 67 6d 65 6e 74   nMerge<nSegment
386af 20 0a 20 20 20 20 20 20 20 20 26 26 20 61 70 53   .        && apS
386b0 65 67 6d 65 6e 74 5b 6e 4d 65 72 67 65 5d 2d 3e  egment[nMerge]->
386b1 61 4e 6f 64 65 0a 20 20 20 20 20 20 20 20 26 26  aNode.        &&
386b2 20 61 70 53 65 67 6d 65 6e 74 5b 6e 4d 65 72 67   apSegment[nMerg
386b3 65 5d 2d 3e 6e 54 65 72 6d 3d 3d 6e 54 65 72 6d  e]->nTerm==nTerm
386b4 20 0a 20 20 20 20 20 20 20 20 26 26 20 30 3d 3d   .        && 0==
386b5 6d 65 6d 63 6d 70 28 7a 54 65 72 6d 2c 20 61 70  memcmp(zTerm, ap
386b6 53 65 67 6d 65 6e 74 5b 6e 4d 65 72 67 65 5d 2d  Segment[nMerge]-
386b7 3e 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 29 0a 20  >zTerm, nTerm). 
386b8 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 4d 65 72     ){.      nMer
386b9 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ge++;.    }..   
386ba 20 69 66 28 20 6e 4d 65 72 67 65 3d 3d 31 20 26   if( nMerge==1 &
386bb 26 20 21 69 73 49 67 6e 6f 72 65 45 6d 70 74 79  & !isIgnoreEmpty
386bc 20 26 26 20 21 69 73 43 6f 6c 46 69 6c 74 65 72   && !isColFilter
386bd 20 26 26 20 69 73 52 65 71 75 69 72 65 50 6f 73   && isRequirePos
386be 20 29 7b 0a 20 20 20 20 20 20 46 74 73 33 53 65   ){.      Fts3Se
386bf 67 52 65 61 64 65 72 20 2a 70 30 20 3d 20 61 70  gReader *p0 = ap
386c0 53 65 67 6d 65 6e 74 5b 30 5d 3b 0a 20 20 20 20  Segment[0];.    
386c1 20 20 72 63 20 3d 20 78 46 75 6e 63 28 70 2c 20    rc = xFunc(p, 
386c2 70 43 6f 6e 74 65 78 74 2c 20 7a 54 65 72 6d 2c  pContext, zTerm,
386c3 20 6e 54 65 72 6d 2c 20 70 30 2d 3e 61 44 6f 63   nTerm, p0->aDoc
386c4 6c 69 73 74 2c 20 70 30 2d 3e 6e 44 6f 63 6c 69  list, p0->nDocli
386c5 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  st);.      if( r
386c6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
386c7 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20  oto finished;.  
386c8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
386c9 6e 74 20 6e 44 6f 63 6c 69 73 74 20 3d 20 30 3b  nt nDoclist = 0;
386ca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
386cb 7a 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 2a 2f  ze of doclist */
386cc 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69  .      sqlite3_i
386cd 6e 74 36 34 20 69 50 72 65 76 20 3d 20 30 3b 20  nt64 iPrev = 0; 
386ce 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 64     /* Previous d
386cf 6f 63 69 64 20 73 74 6f 72 65 64 20 69 6e 20 64  ocid stored in d
386d0 6f 63 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20 20  oclist */..     
386d1 20 2f 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20   /* The current 
386d2 74 65 72 6d 20 6f 66 20 74 68 65 20 66 69 72 73  term of the firs
386d3 74 20 6e 4d 65 72 67 65 20 65 6e 74 72 69 65 73  t nMerge entries
386d4 20 69 6e 20 74 68 65 20 61 72 72 61 79 0a 20 20   in the array.  
386d5 20 20 20 20 2a 2a 20 6f 66 20 46 74 73 33 53 65      ** of Fts3Se
386d6 67 52 65 61 64 65 72 20 6f 62 6a 65 63 74 73 20  gReader objects 
386d7 69 73 20 74 68 65 20 73 61 6d 65 2e 20 54 68 65  is the same. The
386d8 20 64 6f 63 6c 69 73 74 73 20 6d 75 73 74 20 62   doclists must b
386d9 65 20 6d 65 72 67 65 64 0a 20 20 20 20 20 20 2a  e merged.      *
386da 2a 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 20 74  * and a single t
386db 65 72 6d 20 61 64 64 65 64 20 74 6f 20 74 68 65  erm added to the
386dc 20 6e 65 77 20 73 65 67 6d 65 6e 74 2e 0a 20 20   new segment..  
386dd 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72      */.      for
386de 28 69 3d 30 3b 20 69 3c 6e 4d 65 72 67 65 3b 20  (i=0; i<nMerge; 
386df 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 74  i++){.        ft
386e0 73 33 53 65 67 52 65 61 64 65 72 46 69 72 73 74  s3SegReaderFirst
386e1 44 6f 63 69 64 28 61 70 53 65 67 6d 65 6e 74 5b  Docid(apSegment[
386e2 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i]);.      }.   
386e3 20 20 20 66 74 73 33 53 65 67 52 65 61 64 65 72     fts3SegReader
386e4 53 6f 72 74 28 61 70 53 65 67 6d 65 6e 74 2c 20  Sort(apSegment, 
386e5 6e 4d 65 72 67 65 2c 20 6e 4d 65 72 67 65 2c 20  nMerge, nMerge, 
386e6 66 74 73 33 53 65 67 52 65 61 64 65 72 44 6f 63  fts3SegReaderDoc
386e7 6c 69 73 74 43 6d 70 29 3b 0a 20 20 20 20 20 20  listCmp);.      
386e8 77 68 69 6c 65 28 20 61 70 53 65 67 6d 65 6e 74  while( apSegment
386e9 5b 30 5d 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74  [0]->pOffsetList
386ea 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
386eb 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
386ec 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
386ed 6f 66 20 73 65 67 6d 65 6e 74 73 20 74 68 61 74  of segments that
386ee 20 73 68 61 72 65 20 61 20 64 6f 63 69 64 20 2a   share a docid *
386ef 2f 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a  /.        char *
386f0 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 69  pList;.        i
386f1 6e 74 20 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20  nt nList;.      
386f2 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 20    int nByte;.   
386f3 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74       sqlite3_int
386f4 36 34 20 69 44 6f 63 69 64 20 3d 20 61 70 53 65  64 iDocid = apSe
386f5 67 6d 65 6e 74 5b 30 5d 2d 3e 69 44 6f 63 69 64  gment[0]->iDocid
386f6 3b 0a 20 20 20 20 20 20 20 20 66 74 73 33 53 65  ;.        fts3Se
386f7 67 52 65 61 64 65 72 4e 65 78 74 44 6f 63 69 64  gReaderNextDocid
386f8 28 61 70 53 65 67 6d 65 6e 74 5b 30 5d 2c 20 26  (apSegment[0], &
386f9 70 4c 69 73 74 2c 20 26 6e 4c 69 73 74 29 3b 0a  pList, &nList);.
386fa 20 20 20 20 20 20 20 20 6a 20 3d 20 31 3b 0a 20          j = 1;. 
386fb 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 6a 3c         while( j<
386fc 6e 4d 65 72 67 65 20 0a 20 20 20 20 20 20 20 20  nMerge .        
386fd 20 20 20 20 26 26 20 61 70 53 65 67 6d 65 6e 74      && apSegment
386fe 5b 6a 5d 2d 3e 70 4f 66 66 73 65 74 4c 69 73 74  [j]->pOffsetList
386ff 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 26 26   .            &&
38700 20 61 70 53 65 67 6d 65 6e 74 5b 6a 5d 2d 3e 69   apSegment[j]->i
38701 44 6f 63 69 64 3d 3d 69 44 6f 63 69 64 20 0a 20  Docid==iDocid . 
38702 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
38703 20 20 20 20 66 74 73 33 53 65 67 52 65 61 64 65      fts3SegReade
38704 72 4e 65 78 74 44 6f 63 69 64 28 61 70 53 65 67  rNextDocid(apSeg
38705 6d 65 6e 74 5b 6a 5d 2c 20 30 2c 20 30 29 3b 0a  ment[j], 0, 0);.
38706 20 20 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20            j++;. 
38707 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
38708 20 20 69 66 28 20 69 73 43 6f 6c 46 69 6c 74 65    if( isColFilte
38709 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  r ){.          f
3870a 74 73 33 43 6f 6c 75 6d 6e 46 69 6c 74 65 72 28  ts3ColumnFilter(
3870b 70 46 69 6c 74 65 72 2d 3e 69 43 6f 6c 2c 20 26  pFilter->iCol, &
3870c 70 4c 69 73 74 2c 20 26 6e 4c 69 73 74 29 3b 0a  pList, &nList);.
3870d 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
3870e 20 20 20 69 66 28 20 21 69 73 49 67 6e 6f 72 65     if( !isIgnore
3870f 45 6d 70 74 79 20 7c 7c 20 6e 4c 69 73 74 3e 30  Empty || nList>0
38710 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 42   ){.          nB
38711 79 74 65 20 3d 20 73 71 6c 69 74 65 33 46 74 73  yte = sqlite3Fts
38712 33 56 61 72 69 6e 74 4c 65 6e 28 69 44 6f 63 69  3VarintLen(iDoci
38713 64 2d 69 50 72 65 76 29 20 2b 20 28 69 73 52 65  d-iPrev) + (isRe
38714 71 75 69 72 65 50 6f 73 3f 6e 4c 69 73 74 2b 31  quirePos?nList+1
38715 3a 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  :0);.          i
38716 66 28 20 6e 44 6f 63 6c 69 73 74 2b 6e 42 79 74  f( nDoclist+nByt
38717 65 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20  e>nAlloc ){.    
38718 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 61 4e          char *aN
38719 65 77 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ew;.            
3871a 6e 41 6c 6c 6f 63 20 3d 20 6e 44 6f 63 6c 69 73  nAlloc = nDoclis
3871b 74 2b 6e 42 79 74 65 2a 32 3b 0a 20 20 20 20 20  t+nByte*2;.     
3871c 20 20 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71         aNew = sq
3871d 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 61 42  lite3_realloc(aB
3871e 75 66 66 65 72 2c 20 6e 41 6c 6c 6f 63 29 3b 0a  uffer, nAlloc);.
3871f 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
38720 21 61 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20  !aNew ){.       
38721 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
38722 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
38723 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e          goto fin
38724 69 73 68 65 64 3b 0a 20 20 20 20 20 20 20 20 20  ished;.         
38725 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
38726 20 61 42 75 66 66 65 72 20 3d 20 61 4e 65 77 3b   aBuffer = aNew;
38727 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
38728 20 20 20 20 20 20 20 6e 44 6f 63 6c 69 73 74 20         nDoclist 
38729 2b 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50 75  += sqlite3Fts3Pu
3872a 74 56 61 72 69 6e 74 28 26 61 42 75 66 66 65 72  tVarint(&aBuffer
3872b 5b 6e 44 6f 63 6c 69 73 74 5d 2c 20 69 44 6f 63  [nDoclist], iDoc
3872c 69 64 2d 69 50 72 65 76 29 3b 0a 20 20 20 20 20  id-iPrev);.     
3872d 20 20 20 20 20 69 50 72 65 76 20 3d 20 69 44 6f       iPrev = iDo
3872e 63 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20 69  cid;.          i
3872f 66 28 20 69 73 52 65 71 75 69 72 65 50 6f 73 20  f( isRequirePos 
38730 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d  ){.            m
38731 65 6d 63 70 79 28 26 61 42 75 66 66 65 72 5b 6e  emcpy(&aBuffer[n
38732 44 6f 63 6c 69 73 74 5d 2c 20 70 4c 69 73 74 2c  Doclist], pList,
38733 20 6e 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20   nList);.       
38734 20 20 20 20 20 6e 44 6f 63 6c 69 73 74 20 2b 3d       nDoclist +=
38735 20 6e 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20   nList;.        
38736 20 20 20 20 61 42 75 66 66 65 72 5b 6e 44 6f 63      aBuffer[nDoc
38737 6c 69 73 74 2b 2b 5d 20 3d 20 27 5c 30 27 3b 0a  list++] = '\0';.
38738 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
38739 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 66      }..        f
3873a 74 73 33 53 65 67 52 65 61 64 65 72 53 6f 72 74  ts3SegReaderSort
3873b 28 61 70 53 65 67 6d 65 6e 74 2c 20 6e 4d 65 72  (apSegment, nMer
3873c 67 65 2c 20 6a 2c 20 66 74 73 33 53 65 67 52 65  ge, j, fts3SegRe
3873d 61 64 65 72 44 6f 63 6c 69 73 74 43 6d 70 29 3b  aderDoclistCmp);
3873e 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
3873f 69 66 28 20 6e 44 6f 63 6c 69 73 74 3e 30 20 29  if( nDoclist>0 )
38740 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 78  {.        rc = x
38741 46 75 6e 63 28 70 2c 20 70 43 6f 6e 74 65 78 74  Func(p, pContext
38742 2c 20 7a 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20  , zTerm, nTerm, 
38743 61 42 75 66 66 65 72 2c 20 6e 44 6f 63 6c 69 73  aBuffer, nDoclis
38744 74 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t);.        if( 
38745 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
38746 67 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20  goto finished;. 
38747 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
38748 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
38749 20 61 20 74 65 72 6d 20 73 70 65 63 69 66 69 65   a term specifie
3874a 64 20 74 6f 20 66 69 6c 74 65 72 20 6f 6e 2c 20  d to filter on, 
3874b 61 6e 64 20 74 68 69 73 20 69 73 20 6e 6f 74 20  and this is not 
3874c 61 20 70 72 65 66 69 78 0a 20 20 20 20 2a 2a 20  a prefix.    ** 
3874d 73 65 61 72 63 68 2c 20 72 65 74 75 72 6e 20 6e  search, return n
3874e 6f 77 2e 20 54 68 65 20 63 61 6c 6c 62 61 63 6b  ow. The callback
3874f 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64   that correspond
38750 73 20 74 6f 20 74 68 65 20 72 65 71 75 69 72 65  s to the require
38751 64 0a 20 20 20 20 2a 2a 20 74 65 72 6d 20 28 69  d.    ** term (i
38752 66 20 73 75 63 68 20 61 20 74 65 72 6d 20 65 78  f such a term ex
38753 69 73 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65  ists in the inde
38754 78 29 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  x) has already b
38755 65 65 6e 20 6d 61 64 65 2e 0a 20 20 20 20 2a 2f  een made..    */
38756 0a 20 20 20 20 69 66 28 20 70 46 69 6c 74 65 72  .    if( pFilter
38757 2d 3e 7a 54 65 72 6d 20 26 26 20 21 69 73 50 72  ->zTerm && !isPr
38758 65 66 69 78 20 29 7b 0a 20 20 20 20 20 20 67 6f  efix ){.      go
38759 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 20 20 20  to finished;.   
3875a 20 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   }..    for(i=0;
3875b 20 69 3c 6e 4d 65 72 67 65 3b 20 69 2b 2b 29 7b   i<nMerge; i++){
3875c 0a 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33  .      rc = fts3
3875d 53 65 67 52 65 61 64 65 72 4e 65 78 74 28 61 70  SegReaderNext(ap
3875e 53 65 67 6d 65 6e 74 5b 69 5d 29 3b 0a 20 20 20  Segment[i]);.   
3875f 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
38760 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69  E_OK ) goto fini
38761 73 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20  shed;.    }.    
38762 66 74 73 33 53 65 67 52 65 61 64 65 72 53 6f 72  fts3SegReaderSor
38763 74 28 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53 65  t(apSegment, nSe
38764 67 6d 65 6e 74 2c 20 6e 4d 65 72 67 65 2c 20 66  gment, nMerge, f
38765 74 73 33 53 65 67 52 65 61 64 65 72 43 6d 70 29  ts3SegReaderCmp)
38766 3b 0a 20 20 7d 0a 0a 20 66 69 6e 69 73 68 65 64  ;.  }.. finished
38767 3a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  :.  sqlite3_free
38768 28 61 42 75 66 66 65 72 29 3b 0a 20 20 72 65 74  (aBuffer);.  ret
38769 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
3876a 20 4d 65 72 67 65 20 61 6c 6c 20 6c 65 76 65 6c   Merge all level
3876b 20 69 4c 65 76 65 6c 20 73 65 67 6d 65 6e 74 73   iLevel segments
3876c 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
3876d 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 0a   into a single .
3876e 2a 2a 20 69 4c 65 76 65 6c 2b 31 20 73 65 67 6d  ** iLevel+1 segm
3876f 65 6e 74 2e 20 4f 72 2c 20 69 66 20 69 4c 65 76  ent. Or, if iLev
38770 65 6c 3c 30 2c 20 6d 65 72 67 65 20 61 6c 6c 20  el<0, merge all 
38771 73 65 67 6d 65 6e 74 73 20 69 6e 74 6f 20 61 0a  segments into a.
38772 2a 2a 20 73 69 6e 67 6c 65 20 73 65 67 6d 65 6e  ** single segmen
38773 74 20 77 69 74 68 20 61 20 6c 65 76 65 6c 20 65  t with a level e
38774 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d 65  qual to the nume
38775 72 69 63 61 6c 6c 79 20 6c 61 72 67 65 73 74 20  rically largest 
38776 6c 65 76 65 6c 20 0a 2a 2a 20 63 75 72 72 65 6e  level .** curren
38777 74 6c 79 20 70 72 65 73 65 6e 74 20 69 6e 20 74  tly present in t
38778 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a  he database..**.
38779 2a 2a 20 49 66 20 74 68 69 73 20 66 75 6e 63 74  ** If this funct
3877a 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 69  ion is called wi
3877b 74 68 20 69 4c 65 76 65 6c 3c 30 2c 20 62 75 74  th iLevel<0, but
3877c 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 6f   there is only o
3877d 6e 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 69 6e  ne.** segment in
3877e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 53   the database, S
3877f 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72 65  QLITE_DONE is re
38780 74 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65  turned immediate
38781 6c 79 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ly. .** Otherwis
38782 65 2c 20 69 66 20 73 75 63 63 65 73 73 66 75 6c  e, if successful
38783 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
38784 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65  eturned. If an e
38785 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 0a 2a 2a  rror occurs, .**
38786 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
38787 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
38788 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  d..*/.static int
38789 20 66 74 73 33 53 65 67 6d 65 6e 74 4d 65 72 67   fts3SegmentMerg
3878a 65 28 46 74 73 33 54 61 62 6c 65 20 2a 70 2c 20  e(Fts3Table *p, 
3878b 69 6e 74 20 69 4c 65 76 65 6c 29 7b 0a 20 20 69  int iLevel){.  i
3878c 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
3878d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3878e 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61  * Iterator varia
3878f 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b  ble */.  int rc;
38790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38791 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
38792 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74  rn code */.  int
38793 20 69 49 64 78 3b 20 20 20 20 20 20 20 20 20 20   iIdx;          
38794 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38795 49 6e 64 65 78 20 6f 66 20 6e 65 77 20 73 65 67  Index of new seg
38796 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4e  ment */.  int iN
38797 65 77 4c 65 76 65 6c 3b 20 20 20 20 20 20 20 20  ewLevel;        
38798 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 76            /* Lev
38799 65 6c 20 74 6f 20 63 72 65 61 74 65 20 6e 65 77  el to create new
3879a 20 73 65 67 6d 65 6e 74 20 61 74 20 2a 2f 0a 20   segment at */. 
3879b 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
3879c 53 74 6d 74 3b 0a 20 20 53 65 67 6d 65 6e 74 57  Stmt;.  SegmentW
3879d 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 20 3d  riter *pWriter =
3879e 20 30 3b 0a 20 20 69 6e 74 20 6e 53 65 67 6d 65   0;.  int nSegme
3879f 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  nt = 0;         
387a0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
387a1 6f 66 20 73 65 67 6d 65 6e 74 73 20 62 65 69 6e  of segments bein
387a2 67 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20 46 74  g merged */.  Ft
387a3 73 33 53 65 67 52 65 61 64 65 72 20 2a 2a 61 70  s3SegReader **ap
387a4 53 65 67 6d 65 6e 74 20 3d 20 30 3b 20 20 2f 2a  Segment = 0;  /*
387a5 20 41 72 72 61 79 20 6f 66 20 53 65 67 6d 65 6e   Array of Segmen
387a6 74 20 69 74 65 72 61 74 6f 72 73 20 2a 2f 0a 20  t iterators */. 
387a7 20 46 74 73 33 53 65 67 46 69 6c 74 65 72 20 66   Fts3SegFilter f
387a8 69 6c 74 65 72 3b 20 20 20 20 20 20 20 20 20 20  ilter;          
387a9 20 2f 2a 20 53 65 67 6d 65 6e 74 20 74 65 72 6d   /* Segment term
387aa 20 66 69 6c 74 65 72 20 63 6f 6e 64 69 74 69 6f   filter conditio
387ab 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 69 4c 65 76  n */..  if( iLev
387ac 65 6c 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54  el<0 ){.    /* T
387ad 68 69 73 20 63 61 6c 6c 20 69 73 20 74 6f 20 6d  his call is to m
387ae 65 72 67 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74  erge all segment
387af 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
387b0 65 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 20 20  e to a single.  
387b1 20 20 2a 2a 20 73 65 67 6d 65 6e 74 2e 20 54 68    ** segment. Th
387b2 65 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 6e  e level of the n
387b3 65 77 20 73 65 67 6d 65 6e 74 20 69 73 20 65 71  ew segment is eq
387b4 75 61 6c 20 74 6f 20 74 68 65 20 74 68 65 20 6e  ual to the the n
387b5 75 6d 65 72 69 63 61 6c 6c 79 20 0a 20 20 20 20  umerically .    
387b6 2a 2a 20 67 72 65 61 74 65 73 74 20 73 65 67 6d  ** greatest segm
387b7 65 6e 74 20 6c 65 76 65 6c 20 63 75 72 72 65 6e  ent level curren
387b8 74 6c 79 20 70 72 65 73 65 6e 74 20 69 6e 20 74  tly present in t
387b9 68 65 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  he database. The
387ba 20 69 6e 64 65 78 0a 20 20 20 20 2a 2a 20 6f 66   index.    ** of
387bb 20 74 68 65 20 6e 65 77 20 73 65 67 6d 65 6e 74   the new segment
387bc 20 69 73 20 61 6c 77 61 79 73 20 30 2e 0a 20 20   is always 0..  
387bd 20 20 2a 2f 0a 20 20 20 20 69 49 64 78 20 3d 20    */.    iIdx = 
387be 30 3b 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33  0;.    rc = fts3
387bf 53 65 67 6d 65 6e 74 43 6f 75 6e 74 4d 61 78 28  SegmentCountMax(
387c0 70 2c 20 26 6e 53 65 67 6d 65 6e 74 2c 20 26 69  p, &nSegment, &i
387c1 4e 65 77 4c 65 76 65 6c 29 3b 0a 20 20 20 20 69  NewLevel);.    i
387c2 66 28 20 6e 53 65 67 6d 65 6e 74 3d 3d 31 20 29  f( nSegment==1 )
387c3 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
387c4 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
387c5 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  }.  }else{.    /
387c6 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 74  * This call is t
387c7 6f 20 6d 65 72 67 65 20 61 6c 6c 20 73 65 67 6d  o merge all segm
387c8 65 6e 74 73 20 61 74 20 6c 65 76 65 6c 20 69 4c  ents at level iL
387c9 65 76 65 6c 2e 20 46 69 6e 64 20 74 68 65 20 6e  evel. Find the n
387ca 65 78 74 0a 20 20 20 20 2a 2a 20 61 76 61 69 6c  ext.    ** avail
387cb 61 62 6c 65 20 73 65 67 6d 65 6e 74 20 69 6e 64  able segment ind
387cc 65 78 20 61 74 20 6c 65 76 65 6c 20 69 4c 65 76  ex at level iLev
387cd 65 6c 2b 31 2e 20 54 68 65 20 63 61 6c 6c 20 74  el+1. The call t
387ce 6f 0a 20 20 20 20 2a 2a 20 66 74 73 33 41 6c 6c  o.    ** fts3All
387cf 6f 63 61 74 65 53 65 67 64 69 72 49 64 78 28 29  ocateSegdirIdx()
387d0 20 77 69 6c 6c 20 6d 65 72 67 65 20 74 68 65 20   will merge the 
387d1 73 65 67 6d 65 6e 74 73 20 61 74 20 6c 65 76 65  segments at leve
387d2 6c 20 69 4c 65 76 65 6c 2b 31 20 74 6f 20 0a 20  l iLevel+1 to . 
387d3 20 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 69     ** a single i
387d4 4c 65 76 65 6c 2b 32 20 73 65 67 6d 65 6e 74 20  Level+2 segment 
387d5 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20  if necessary..  
387d6 20 20 2a 2f 0a 20 20 20 20 69 4e 65 77 4c 65 76    */.    iNewLev
387d7 65 6c 20 3d 20 69 4c 65 76 65 6c 2b 31 3b 0a 20  el = iLevel+1;. 
387d8 20 20 20 72 63 20 3d 20 66 74 73 33 41 6c 6c 6f     rc = fts3Allo
387d9 63 61 74 65 53 65 67 64 69 72 49 64 78 28 70 2c  cateSegdirIdx(p,
387da 20 69 4e 65 77 4c 65 76 65 6c 2c 20 26 69 49 64   iNewLevel, &iId
387db 78 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  x);.    if( rc!=
387dc 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
387dd 72 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20  rn rc;.    rc = 
387de 66 74 73 33 53 65 67 6d 65 6e 74 43 6f 75 6e 74  fts3SegmentCount
387df 28 70 2c 20 69 4c 65 76 65 6c 2c 20 26 6e 53 65  (p, iLevel, &nSe
387e0 67 6d 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66  gment);.  }.  if
387e1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
387e2 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 61  ) return rc;.  a
387e3 73 73 65 72 74 28 20 6e 53 65 67 6d 65 6e 74 3e  ssert( nSegment>
387e4 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
387e5 4e 65 77 4c 65 76 65 6c 3e 3d 30 20 29 3b 0a 0a  NewLevel>=0 );..
387e6 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70    /* Allocate sp
387e7 61 63 65 20 66 6f 72 20 61 6e 20 61 72 72 61 79  ace for an array
387e8 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
387e9 73 65 67 6d 65 6e 74 20 69 74 65 72 61 74 6f 72  segment iterator
387ea 73 2e 20 2a 2f 0a 20 20 61 70 53 65 67 6d 65 6e  s. */.  apSegmen
387eb 74 20 3d 20 28 46 74 73 33 53 65 67 52 65 61 64  t = (Fts3SegRead
387ec 65 72 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  er**)sqlite3_mal
387ed 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73 33 53  loc(sizeof(Fts3S
387ee 65 67 52 65 61 64 65 72 20 2a 29 2a 6e 53 65 67  egReader *)*nSeg
387ef 6d 65 6e 74 29 3b 0a 20 20 69 66 28 20 21 61 70  ment);.  if( !ap
387f0 53 65 67 6d 65 6e 74 20 29 7b 0a 20 20 20 20 72  Segment ){.    r
387f1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
387f2 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  EM;.  }.  memset
387f3 28 61 70 53 65 67 6d 65 6e 74 2c 20 30 2c 20 73  (apSegment, 0, s
387f4 69 7a 65 6f 66 28 46 74 73 33 53 65 67 52 65 61  izeof(Fts3SegRea
387f5 64 65 72 20 2a 29 2a 6e 53 65 67 6d 65 6e 74 29  der *)*nSegment)
387f6 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
387f7 20 61 20 46 74 73 33 53 65 67 52 65 61 64 65 72   a Fts3SegReader
387f8 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 65   structure for e
387f9 61 63 68 20 73 65 67 6d 65 6e 74 20 62 65 69 6e  ach segment bein
387fa 67 20 6d 65 72 67 65 64 2e 20 41 20 0a 20 20 2a  g merged. A .  *
387fb 2a 20 46 74 73 33 53 65 67 52 65 61 64 65 72 20  * Fts3SegReader 
387fc 73 74 6f 72 65 73 20 74 68 65 20 73 74 61 74 65  stores the state
387fd 20 64 61 74 61 20 72 65 71 75 69 72 65 64 20 74   data required t
387fe 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
387ff 68 20 61 6c 6c 20 0a 20 20 2a 2a 20 65 6e 74 72  h all .  ** entr
38800 69 65 73 20 6f 6e 20 61 6c 6c 20 6c 65 61 76 65  ies on all leave
38801 73 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 73 65  s of a single se
38802 67 6d 65 6e 74 2e 20 0a 20 20 2a 2f 0a 20 20 61  gment. .  */.  a
38803 73 73 65 72 74 28 20 53 51 4c 5f 53 45 4c 45 43  ssert( SQL_SELEC
38804 54 5f 4c 45 56 45 4c 2b 31 3d 3d 53 51 4c 5f 53  T_LEVEL+1==SQL_S
38805 45 4c 45 43 54 5f 41 4c 4c 5f 4c 45 56 45 4c 29  ELECT_ALL_LEVEL)
38806 3b 0a 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c  ;.  rc = fts3Sql
38807 53 74 6d 74 28 70 2c 20 53 51 4c 5f 53 45 4c 45  Stmt(p, SQL_SELE
38808 43 54 5f 4c 45 56 45 4c 2b 28 69 4c 65 76 65 6c  CT_LEVEL+(iLevel
38809 3c 30 29 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b  <0), &pStmt, 0);
3880a 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3880b 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 66 69 6e 69  E_OK ) goto fini
3880c 73 68 65 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  shed;.  sqlite3_
3880d 62 69 6e 64 5f 69 6e 74 28 70 53 74 6d 74 2c 20  bind_int(pStmt, 
3880e 31 2c 20 69 4c 65 76 65 6c 29 3b 0a 20 20 66 6f  1, iLevel);.  fo
3880f 72 28 69 3d 30 3b 20 53 51 4c 49 54 45 5f 52 4f  r(i=0; SQLITE_RO
38810 57 3d 3d 28 73 71 6c 69 74 65 33 5f 73 74 65 70  W==(sqlite3_step
38811 28 70 53 74 6d 74 29 29 3b 20 69 2b 2b 29 7b 0a  (pStmt)); i++){.
38812 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65 67      rc = fts3Seg
38813 52 65 61 64 65 72 4e 65 77 28 70 2c 20 70 53 74  ReaderNew(p, pSt
38814 6d 74 2c 20 69 2c 20 26 61 70 53 65 67 6d 65 6e  mt, i, &apSegmen
38815 74 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72  t[i]);.    if( r
38816 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
38817 20 20 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73        goto finis
38818 68 65 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  hed;.    }.  }. 
38819 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65   rc = sqlite3_re
3881a 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 70 53  set(pStmt);.  pS
3881b 74 6d 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 72  tmt = 0;.  if( r
3881c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
3881d 6f 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 0a 20  oto finished;.. 
3881e 20 6d 65 6d 73 65 74 28 26 66 69 6c 74 65 72 2c   memset(&filter,
3881f 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33 53   0, sizeof(Fts3S
38820 65 67 46 69 6c 74 65 72 29 29 3b 0a 20 20 66 69  egFilter));.  fi
38821 6c 74 65 72 2e 66 6c 61 67 73 20 3d 20 46 54 53  lter.flags = FTS
38822 33 5f 53 45 47 4d 45 4e 54 5f 52 45 51 55 49 52  3_SEGMENT_REQUIR
38823 45 5f 50 4f 53 3b 0a 20 20 66 69 6c 74 65 72 2e  E_POS;.  filter.
38824 66 6c 61 67 73 20 7c 3d 20 28 69 4c 65 76 65 6c  flags |= (iLevel
38825 3c 30 20 3f 20 46 54 53 33 5f 53 45 47 4d 45 4e  <0 ? FTS3_SEGMEN
38826 54 5f 49 47 4e 4f 52 45 5f 45 4d 50 54 59 20 3a  T_IGNORE_EMPTY :
38827 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69   0);.  rc = sqli
38828 74 65 33 46 74 73 33 53 65 67 52 65 61 64 65 72  te3Fts3SegReader
38829 49 74 65 72 61 74 65 28 70 2c 20 61 70 53 65 67  Iterate(p, apSeg
3882a 6d 65 6e 74 2c 20 6e 53 65 67 6d 65 6e 74 2c 0a  ment, nSegment,.
3882b 20 20 20 20 20 20 26 66 69 6c 74 65 72 2c 20 66        &filter, f
3882c 74 73 33 4d 65 72 67 65 43 61 6c 6c 62 61 63 6b  ts3MergeCallback
3882d 2c 20 28 76 6f 69 64 20 2a 29 26 70 57 72 69 74  , (void *)&pWrit
3882e 65 72 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  er.  );.  if( rc
3882f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
38830 74 6f 20 66 69 6e 69 73 68 65 64 3b 0a 0a 20 20  to finished;..  
38831 72 63 20 3d 20 66 74 73 33 44 65 6c 65 74 65 53  rc = fts3DeleteS
38832 65 67 64 69 72 28 70 2c 20 69 4c 65 76 65 6c 2c  egdir(p, iLevel,
38833 20 61 70 53 65 67 6d 65 6e 74 2c 20 6e 53 65 67   apSegment, nSeg
38834 6d 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 3d  ment);.  if( rc=
38835 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
38836 20 20 72 63 20 3d 20 66 74 73 33 53 65 67 57 72    rc = fts3SegWr
38837 69 74 65 72 46 6c 75 73 68 28 70 2c 20 70 57 72  iterFlush(p, pWr
38838 69 74 65 72 2c 20 69 4e 65 77 4c 65 76 65 6c 2c  iter, iNewLevel,
38839 20 69 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 66 69   iIdx);.  }.. fi
3883a 6e 69 73 68 65 64 3a 0a 20 20 66 74 73 33 53 65  nished:.  fts3Se
3883b 67 57 72 69 74 65 72 46 72 65 65 28 70 57 72 69  gWriterFree(pWri
3883c 74 65 72 29 3b 0a 20 20 69 66 28 20 61 70 53 65  ter);.  if( apSe
3883d 67 6d 65 6e 74 20 29 7b 0a 20 20 20 20 66 6f 72  gment ){.    for
3883e 28 69 3d 30 3b 20 69 3c 6e 53 65 67 6d 65 6e 74  (i=0; i<nSegment
3883f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
38840 6c 69 74 65 33 46 74 73 33 53 65 67 52 65 61 64  lite3Fts3SegRead
38841 65 72 46 72 65 65 28 70 2c 20 61 70 53 65 67 6d  erFree(p, apSegm
38842 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  ent[i]);.    }. 
38843 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
38844 61 70 53 65 67 6d 65 6e 74 29 3b 0a 20 20 7d 0a  apSegment);.  }.
38845 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28    sqlite3_reset(
38846 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
38847 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68   rc;.}../*.** Th
38848 69 73 20 69 73 20 61 20 63 6f 6d 70 61 72 69 73  is is a comparis
38849 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64  on function used
3884a 20 61 73 20 61 20 71 73 6f 72 74 28 29 20 63 61   as a qsort() ca
3884b 6c 6c 62 61 63 6b 20 77 68 65 6e 20 73 6f 72 74  llback when sort
3884c 69 6e 67 0a 2a 2a 20 61 6e 20 61 72 72 61 79 20  ing.** an array 
3884d 6f 66 20 70 65 6e 64 69 6e 67 20 74 65 72 6d 73  of pending terms
3884e 20 62 79 20 74 65 72 6d 2e 20 54 68 69 73 20 6f   by term. This o
3884f 63 63 75 72 73 20 61 73 20 70 61 72 74 20 6f 66  ccurs as part of
38850 20 66 6c 75 73 68 69 6e 67 0a 2a 2a 20 74 68 65   flushing.** the
38851 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
38852 20 70 65 6e 64 69 6e 67 2d 74 65 72 6d 73 20 68   pending-terms h
38853 61 73 68 20 74 61 62 6c 65 20 74 6f 20 74 68 65  ash table to the
38854 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74   database..*/.st
38855 61 74 69 63 20 69 6e 74 20 71 73 6f 72 74 43 6f  atic int qsortCo
38856 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64  mpare(const void
38857 20 2a 6c 68 73 2c 20 63 6f 6e 73 74 20 76 6f 69   *lhs, const voi
38858 64 20 2a 72 68 73 29 7b 0a 20 20 63 68 61 72 20  d *rhs){.  char 
38859 2a 7a 31 20 3d 20 66 74 73 33 48 61 73 68 4b 65  *z1 = fts3HashKe
3885a 79 28 2a 28 46 74 73 33 48 61 73 68 45 6c 65 6d  y(*(Fts3HashElem
3885b 20 2a 2a 29 6c 68 73 29 3b 0a 20 20 63 68 61 72   **)lhs);.  char
3885c 20 2a 7a 32 20 3d 20 66 74 73 33 48 61 73 68 4b   *z2 = fts3HashK
3885d 65 79 28 2a 28 46 74 73 33 48 61 73 68 45 6c 65  ey(*(Fts3HashEle
3885e 6d 20 2a 2a 29 72 68 73 29 3b 0a 20 20 69 6e 74  m **)rhs);.  int
3885f 20 6e 31 20 3d 20 66 74 73 33 48 61 73 68 4b 65   n1 = fts3HashKe
38860 79 73 69 7a 65 28 2a 28 46 74 73 33 48 61 73 68  ysize(*(Fts3Hash
38861 45 6c 65 6d 20 2a 2a 29 6c 68 73 29 3b 0a 20 20  Elem **)lhs);.  
38862 69 6e 74 20 6e 32 20 3d 20 66 74 73 33 48 61 73  int n2 = fts3Has
38863 68 4b 65 79 73 69 7a 65 28 2a 28 46 74 73 33 48  hKeysize(*(Fts3H
38864 61 73 68 45 6c 65 6d 20 2a 2a 29 72 68 73 29 3b  ashElem **)rhs);
38865 0a 0a 20 20 69 6e 74 20 6e 20 3d 20 28 6e 31 3c  ..  int n = (n1<
38866 6e 32 20 3f 20 6e 31 20 3a 20 6e 32 29 3b 0a 20  n2 ? n1 : n2);. 
38867 20 69 6e 74 20 63 20 3d 20 6d 65 6d 63 6d 70 28   int c = memcmp(
38868 7a 31 2c 20 7a 32 2c 20 6e 29 3b 0a 20 20 69 66  z1, z2, n);.  if
38869 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 63 20  ( c==0 ){.    c 
3886a 3d 20 6e 31 20 2d 20 6e 32 3b 0a 20 20 7d 0a 20  = n1 - n2;.  }. 
3886b 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f   return c;.}.../
3886c 2a 20 0a 2a 2a 20 46 6c 75 73 68 20 74 68 65 20  * .** Flush the 
3886d 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 65 6e 64  contents of pend
3886e 69 6e 67 54 65 72 6d 73 20 74 6f 20 61 20 6c 65  ingTerms to a le
3886f 76 65 6c 20 30 20 73 65 67 6d 65 6e 74 2e 0a 2a  vel 0 segment..*
38870 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
38871 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
38872 50 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c 75 73  PendingTermsFlus
38873 68 28 46 74 73 33 54 61 62 6c 65 20 2a 70 29 7b  h(Fts3Table *p){
38874 0a 20 20 46 74 73 33 48 61 73 68 45 6c 65 6d 20  .  Fts3HashElem 
38875 2a 70 45 6c 65 6d 3b 0a 20 20 69 6e 74 20 69 64  *pElem;.  int id
38876 78 2c 20 72 63 2c 20 69 3b 0a 20 20 46 74 73 33  x, rc, i;.  Fts3
38877 48 61 73 68 45 6c 65 6d 20 2a 2a 61 70 45 6c 65  HashElem **apEle
38878 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  m;          /* A
38879 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73  rray of pointers
3887a 20 74 6f 20 68 61 73 68 20 65 6c 65 6d 65 6e 74   to hash element
3887b 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 6c 65 6d  s */.  int nElem
3887c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3887d 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
3887e 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 6e 65 77   of terms in new
3887f 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 20 20 53 65   segment */.  Se
38880 67 6d 65 6e 74 57 72 69 74 65 72 20 2a 70 57 72  gmentWriter *pWr
38881 69 74 65 72 20 3d 20 30 3b 20 20 20 20 20 2f 2a  iter = 0;     /*
38882 20 55 73 65 64 20 74 6f 20 77 72 69 74 65 20 74   Used to write t
38883 68 65 20 73 65 67 6d 65 6e 74 20 2a 2f 0a 0a 20  he segment */.. 
38884 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e 75 6d   /* Find the num
38885 62 65 72 20 6f 66 20 74 65 72 6d 73 20 74 68 61  ber of terms tha
38886 74 20 77 69 6c 6c 20 6d 61 6b 65 20 75 70 20 74  t will make up t
38887 68 65 20 6e 65 77 20 73 65 67 6d 65 6e 74 2e 20  he new segment. 
38888 49 66 20 74 68 65 72 65 0a 20 20 2a 2a 20 61 72  If there.  ** ar
38889 65 20 6e 6f 20 74 65 72 6d 73 2c 20 72 65 74 75  e no terms, retu
3888a 72 6e 20 65 61 72 6c 79 20 28 64 6f 20 6e 6f 74  rn early (do not
3888b 20 62 6f 74 68 65 72 20 74 6f 20 77 72 69 74 65   bother to write
3888c 20 61 6e 20 65 6d 70 74 79 20 73 65 67 6d 65 6e   an empty segmen
3888d 74 29 2e 0a 20 20 2a 2f 0a 20 20 6e 45 6c 65 6d  t)..  */.  nElem
3888e 20 3d 20 66 74 73 33 48 61 73 68 43 6f 75 6e 74   = fts3HashCount
3888f 28 26 70 2d 3e 70 65 6e 64 69 6e 67 54 65 72 6d  (&p->pendingTerm
38890 73 29 3b 0a 20 20 69 66 28 20 6e 45 6c 65 6d 3d  s);.  if( nElem=
38891 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
38892 28 20 70 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74  ( p->nPendingDat
38893 61 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75  a==0 );.    retu
38894 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
38895 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e  }..  /* Determin
38896 65 20 74 68 65 20 6e 65 78 74 20 69 6e 64 65 78  e the next index
38897 20 61 74 20 6c 65 76 65 6c 20 30 2c 20 6d 65 72   at level 0, mer
38898 67 69 6e 67 20 61 73 20 6e 65 63 65 73 73 61 72  ging as necessar
38899 79 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66 74 73  y. */.  rc = fts
3889a 33 41 6c 6c 6f 63 61 74 65 53 65 67 64 69 72 49  3AllocateSegdirI
3889b 64 78 28 70 2c 20 30 2c 20 26 69 64 78 29 3b 0a  dx(p, 0, &idx);.
3889c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3889d 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
3889e 6e 20 72 63 3b 0a 20 20 7d 20 0a 0a 20 20 61 70  n rc;.  } ..  ap
3889f 45 6c 65 6d 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Elem = sqlite3_m
388a0 61 6c 6c 6f 63 28 6e 45 6c 65 6d 2a 73 69 7a 65  alloc(nElem*size
388a1 6f 66 28 46 74 73 33 48 61 73 68 45 6c 65 6d 20  of(Fts3HashElem 
388a2 2a 29 29 3b 0a 20 20 69 66 28 20 21 61 70 45 6c  *));.  if( !apEl
388a3 65 6d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  em ){.    return
388a4 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
388a5 20 7d 0a 0a 20 20 69 20 3d 20 30 3b 0a 20 20 66   }..  i = 0;.  f
388a6 6f 72 28 70 45 6c 65 6d 3d 66 74 73 33 48 61 73  or(pElem=fts3Has
388a7 68 46 69 72 73 74 28 26 70 2d 3e 70 65 6e 64 69  hFirst(&p->pendi
388a8 6e 67 54 65 72 6d 73 29 3b 20 70 45 6c 65 6d 3b  ngTerms); pElem;
388a9 20 70 45 6c 65 6d 3d 66 74 73 33 48 61 73 68 4e   pElem=fts3HashN
388aa 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
388ab 20 61 70 45 6c 65 6d 5b 69 2b 2b 5d 20 3d 20 70   apElem[i++] = p
388ac 45 6c 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65  Elem;.  }.  asse
388ad 72 74 28 20 69 3d 3d 6e 45 6c 65 6d 20 29 3b 0a  rt( i==nElem );.
388ae 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .  /* TODO(shess
388af 29 20 53 68 6f 75 6c 64 20 77 65 20 61 6c 6c 6f  ) Should we allo
388b0 77 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 63  w user-defined c
388b1 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
388b2 65 73 2c 0a 20 20 2a 2a 20 68 65 72 65 3f 20 20  es,.  ** here?  
388b3 49 20 74 68 69 6e 6b 20 77 65 20 6f 6e 6c 79 20  I think we only 
388b4 6e 65 65 64 20 74 68 61 74 20 6f 6e 63 65 20 77  need that once w
388b5 65 20 73 75 70 70 6f 72 74 20 70 72 65 66 69 78  e support prefix
388b6 20 73 65 61 72 63 68 65 73 2e 0a 20 20 2a 2a 20   searches..  ** 
388b7 41 6c 73 6f 2c 20 73 68 6f 75 6c 64 20 77 65 20  Also, should we 
388b8 62 65 20 75 73 69 6e 67 20 71 73 6f 72 74 28 29  be using qsort()
388b9 3f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 45 6c  ?.  */.  if( nEl
388ba 65 6d 3e 31 20 29 7b 0a 20 20 20 20 71 73 6f 72  em>1 ){.    qsor
388bb 74 28 61 70 45 6c 65 6d 2c 20 6e 45 6c 65 6d 2c  t(apElem, nElem,
388bc 20 73 69 7a 65 6f 66 28 46 74 73 33 48 61 73 68   sizeof(Fts3Hash
388bd 45 6c 65 6d 20 2a 29 2c 20 71 73 6f 72 74 43 6f  Elem *), qsortCo
388be 6d 70 61 72 65 29 3b 0a 20 20 7d 0a 0a 0a 20 20  mpare);.  }...  
388bf 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73 65 67  /* Write the seg
388c0 6d 65 6e 74 20 74 72 65 65 20 69 6e 74 6f 20 74  ment tree into t
388c1 68 65 20 64 61 74 61 62 61 73 65 2e 20 2a 2f 0a  he database. */.
388c2 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
388c3 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 6e 45  QLITE_OK && i<nE
388c4 6c 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  lem; i++){.    c
388c5 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 66  onst char *z = f
388c6 74 73 33 48 61 73 68 4b 65 79 28 61 70 45 6c 65  ts3HashKey(apEle
388c7 6d 5b 69 5d 29 3b 0a 20 20 20 20 69 6e 74 20 6e  m[i]);.    int n
388c8 20 3d 20 66 74 73 33 48 61 73 68 4b 65 79 73 69   = fts3HashKeysi
388c9 7a 65 28 61 70 45 6c 65 6d 5b 69 5d 29 3b 0a 20  ze(apElem[i]);. 
388ca 20 20 20 50 65 6e 64 69 6e 67 4c 69 73 74 20 2a     PendingList *
388cb 70 4c 69 73 74 20 3d 20 66 74 73 33 48 61 73 68  pList = fts3Hash
388cc 44 61 74 61 28 61 70 45 6c 65 6d 5b 69 5d 29 3b  Data(apElem[i]);
388cd 0a 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65  .    rc = fts3Se
388ce 67 57 72 69 74 65 72 41 64 64 28 70 2c 20 26 70  gWriterAdd(p, &p
388cf 57 72 69 74 65 72 2c 20 30 2c 20 7a 2c 20 6e 2c  Writer, 0, z, n,
388d0 20 70 4c 69 73 74 2d 3e 61 44 61 74 61 2c 20 70   pList->aData, p
388d1 4c 69 73 74 2d 3e 6e 44 61 74 61 2b 31 29 3b 0a  List->nData+1);.
388d2 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
388d3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
388d4 63 20 3d 20 66 74 73 33 53 65 67 57 72 69 74 65  c = fts3SegWrite
388d5 72 46 6c 75 73 68 28 70 2c 20 70 57 72 69 74 65  rFlush(p, pWrite
388d6 72 2c 20 30 2c 20 69 64 78 29 3b 0a 20 20 7d 0a  r, 0, idx);.  }.
388d7 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 20 61  .  /* Free all a
388d8 6c 6c 6f 63 61 74 65 64 20 72 65 73 6f 75 72 63  llocated resourc
388d9 65 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  es before return
388da 69 6e 67 20 2a 2f 0a 20 20 66 74 73 33 53 65 67  ing */.  fts3Seg
388db 57 72 69 74 65 72 46 72 65 65 28 70 57 72 69 74  WriterFree(pWrit
388dc 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  er);.  sqlite3_f
388dd 72 65 65 28 61 70 45 6c 65 6d 29 3b 0a 20 20 73  ree(apElem);.  s
388de 71 6c 69 74 65 33 46 74 73 33 50 65 6e 64 69 6e  qlite3Fts3Pendin
388df 67 54 65 72 6d 73 43 6c 65 61 72 28 70 29 3b 0a  gTermsClear(p);.
388e0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
388e1 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
388e2 69 6f 6e 20 64 6f 65 73 20 74 68 65 20 77 6f 72  ion does the wor
388e3 6b 20 66 6f 72 20 74 68 65 20 78 55 70 64 61 74  k for the xUpdat
388e4 65 20 6d 65 74 68 6f 64 20 6f 66 20 46 54 53 33  e method of FTS3
388e5 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c   virtual.** tabl
388e6 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
388e7 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
388e8 33 46 74 73 33 55 70 64 61 74 65 4d 65 74 68 6f  3Fts3UpdateMetho
388e9 64 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  d(.  sqlite3_vta
388ea 62 20 2a 70 56 74 61 62 2c 20 20 20 20 20 20 20  b *pVtab,       
388eb 20 20 20 20 20 2f 2a 20 46 54 53 33 20 76 74 61       /* FTS3 vta
388ec 62 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e  b object */.  in
388ed 74 20 6e 41 72 67 2c 20 20 20 20 20 20 20 20 20  t nArg,         
388ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
388ef 20 53 69 7a 65 20 6f 66 20 61 72 67 75 6d 65 6e   Size of argumen
388f0 74 20 61 72 72 61 79 20 2a 2f 0a 20 20 73 71 6c  t array */.  sql
388f1 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56  ite3_value **apV
388f2 61 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  al,          /* 
388f3 41 72 72 61 79 20 6f 66 20 61 72 67 75 6d 65 6e  Array of argumen
388f4 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  ts */.  sqlite_i
388f5 6e 74 36 34 20 2a 70 52 6f 77 69 64 20 20 20 20  nt64 *pRowid    
388f6 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
388f7 54 68 65 20 61 66 66 65 63 74 65 64 20 28 6f 72  The affected (or
388f8 20 65 66 66 65 63 74 65 64 29 20 72 6f 77 69 64   effected) rowid
388f9 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 54 61 62   */.){.  Fts3Tab
388fa 6c 65 20 2a 70 20 3d 20 28 46 74 73 33 54 61 62  le *p = (Fts3Tab
388fb 6c 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e  le *)pVtab;.  in
388fc 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
388fd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
388fe 20 52 65 74 75 72 6e 20 43 6f 64 65 20 2a 2f 0a   Return Code */.
388ff 20 20 69 6e 74 20 69 73 52 65 6d 6f 76 65 20 3d    int isRemove =
38900 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
38901 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 6e    /* True for an
38902 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
38903 45 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  E */.  sqlite3_i
38904 6e 74 36 34 20 69 52 65 6d 6f 76 65 20 3d 20 30  nt64 iRemove = 0
38905 3b 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64 20  ;      /* Rowid 
38906 72 65 6d 6f 76 65 64 20 62 79 20 55 50 44 41 54  removed by UPDAT
38907 45 20 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a 0a  E or DELETE */..
38908 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
38909 61 20 44 45 4c 45 54 45 20 6f 72 20 55 50 44 41  a DELETE or UPDA
3890a 54 45 20 6f 70 65 72 61 74 69 6f 6e 2c 20 72 65  TE operation, re
3890b 6d 6f 76 65 20 74 68 65 20 6f 6c 64 20 72 65 63  move the old rec
3890c 6f 72 64 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71  ord. */.  if( sq
3890d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
3890e 28 61 70 56 61 6c 5b 30 5d 29 21 3d 53 51 4c 49  (apVal[0])!=SQLI
3890f 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69  TE_NULL ){.    i
38910 6e 74 20 69 73 45 6d 70 74 79 3b 0a 20 20 20 20  nt isEmpty;.    
38911 72 63 20 3d 20 66 74 73 33 49 73 45 6d 70 74 79  rc = fts3IsEmpty
38912 28 70 2c 20 61 70 56 61 6c 2c 20 26 69 73 45 6d  (p, apVal, &isEm
38913 70 74 79 29 3b 0a 20 20 20 20 69 66 28 20 72 63  pty);.    if( rc
38914 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
38915 20 20 20 20 20 69 66 28 20 69 73 45 6d 70 74 79       if( isEmpty
38916 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44   ){.        /* D
38917 65 6c 65 74 69 6e 67 20 74 68 69 73 20 72 6f 77  eleting this row
38918 20 6d 65 61 6e 73 20 74 68 65 20 77 68 6f 6c 65   means the whole
38919 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79 2e   table is empty.
3891a 20 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20   In this case.  
3891b 20 20 20 20 20 20 2a 2a 20 64 65 6c 65 74 65 20        ** delete 
3891c 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
3891d 61 6c 6c 20 74 68 72 65 65 20 74 61 62 6c 65 73  all three tables
3891e 20 61 6e 64 20 74 68 72 6f 77 20 61 77 61 79 20   and throw away 
3891f 61 6e 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 64  any.        ** d
38920 61 74 61 20 69 6e 20 74 68 65 20 70 65 6e 64 69  ata in the pendi
38921 6e 67 54 65 72 6d 73 20 68 61 73 68 20 74 61 62  ngTerms hash tab
38922 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  le..        */. 
38923 20 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33         rc = fts3
38924 44 65 6c 65 74 65 41 6c 6c 28 70 29 3b 0a 20 20  DeleteAll(p);.  
38925 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
38926 20 20 20 69 73 52 65 6d 6f 76 65 20 3d 20 31 3b     isRemove = 1;
38927 0a 20 20 20 20 20 20 20 20 69 52 65 6d 6f 76 65  .        iRemove
38928 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
38929 5f 69 6e 74 36 34 28 61 70 56 61 6c 5b 30 5d 29  _int64(apVal[0])
3892a 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66  ;.        rc = f
3892b 74 73 33 50 65 6e 64 69 6e 67 54 65 72 6d 73 44  ts3PendingTermsD
3892c 6f 63 69 64 28 70 2c 20 69 52 65 6d 6f 76 65 29  ocid(p, iRemove)
3892d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
3892e 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3892f 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 74           rc = ft
38930 73 33 44 65 6c 65 74 65 54 65 72 6d 73 28 70 2c  s3DeleteTerms(p,
38931 20 61 70 56 61 6c 29 3b 0a 20 20 20 20 20 20 20   apVal);.       
38932 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
38933 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
38934 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 71 6c      rc = fts3Sql
38935 45 78 65 63 28 70 2c 20 53 51 4c 5f 44 45 4c 45  Exec(p, SQL_DELE
38936 54 45 5f 43 4f 4e 54 45 4e 54 2c 20 61 70 56 61  TE_CONTENT, apVa
38937 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  l);.          }.
38938 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
38939 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20  }.    }.  }.  . 
3893a 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
3893b 6e 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41  n INSERT or UPDA
3893c 54 45 20 6f 70 65 72 61 74 69 6f 6e 2c 20 69 6e  TE operation, in
3893d 73 65 72 74 20 74 68 65 20 6e 65 77 20 72 65 63  sert the new rec
3893e 6f 72 64 2e 20 2a 2f 0a 20 20 69 66 28 20 6e 41  ord. */.  if( nA
3893f 72 67 3e 31 20 26 26 20 72 63 3d 3d 53 51 4c 49  rg>1 && rc==SQLI
38940 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20  TE_OK ){.    rc 
38941 3d 20 66 74 73 33 49 6e 73 65 72 74 44 61 74 61  = fts3InsertData
38942 28 70 2c 20 61 70 56 61 6c 2c 20 70 52 6f 77 69  (p, apVal, pRowi
38943 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  d);.    if( rc==
38944 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 28 21 69  SQLITE_OK && (!i
38945 73 52 65 6d 6f 76 65 20 7c 7c 20 2a 70 52 6f 77  sRemove || *pRow
38946 69 64 21 3d 69 52 65 6d 6f 76 65 29 20 29 7b 0a  id!=iRemove) ){.
38947 20 20 20 20 20 20 72 63 20 3d 20 66 74 73 33 50        rc = fts3P
38948 65 6e 64 69 6e 67 54 65 72 6d 73 44 6f 63 69 64  endingTermsDocid
38949 28 70 2c 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20  (p, *pRowid);.  
3894a 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
3894b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3894c 20 20 20 72 63 20 3d 20 66 74 73 33 49 6e 73 65     rc = fts3Inse
3894d 72 74 54 65 72 6d 73 28 70 2c 20 61 70 56 61 6c  rtTerms(p, apVal
3894e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
3894f 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
38950 20 0a 2a 2a 20 46 6c 75 73 68 20 61 6e 79 20 64   .** Flush any d
38951 61 74 61 20 69 6e 20 74 68 65 20 70 65 6e 64 69  ata in the pendi
38952 6e 67 2d 74 65 72 6d 73 20 68 61 73 68 20 74 61  ng-terms hash ta
38953 62 6c 65 20 74 6f 20 64 69 73 6b 2e 20 49 66 20  ble to disk. If 
38954 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20 6d  successful,.** m
38955 65 72 67 65 20 61 6c 6c 20 73 65 67 6d 65 6e 74  erge all segment
38956 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
38957 65 20 28 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  e (including the
38958 20 6e 65 77 20 73 65 67 6d 65 6e 74 2c 20 69 66   new segment, if
38959 20 0a 2a 2a 20 74 68 65 72 65 20 77 61 73 20 61   .** there was a
3895a 6e 79 20 64 61 74 61 20 74 6f 20 66 6c 75 73 68  ny data to flush
3895b 29 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20  ) into a single 
3895c 73 65 67 6d 65 6e 74 2e 20 0a 2a 2f 0a 53 51 4c  segment. .*/.SQL
3895d 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
3895e 73 71 6c 69 74 65 33 46 74 73 33 4f 70 74 69 6d  sqlite3Fts3Optim
3895f 69 7a 65 28 46 74 73 33 54 61 62 6c 65 20 2a 70  ize(Fts3Table *p
38960 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72  ){.  int rc;.  r
38961 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
38962 28 70 2d 3e 64 62 2c 20 22 53 41 56 45 50 4f 49  (p->db, "SAVEPOI
38963 4e 54 20 66 74 73 33 22 2c 20 30 2c 20 30 2c 20  NT fts3", 0, 0, 
38964 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  0);.  if( rc==SQ
38965 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
38966 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 50  c = sqlite3Fts3P
38967 65 6e 64 69 6e 67 54 65 72 6d 73 46 6c 75 73 68  endingTermsFlush
38968 28 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  (p);.    if( rc=
38969 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3896a 20 20 20 20 72 63 20 3d 20 66 74 73 33 53 65 67      rc = fts3Seg
3896b 6d 65 6e 74 4d 65 72 67 65 28 70 2c 20 2d 31 29  mentMerge(p, -1)
3896c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
3896d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
3896e 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
3896f 74 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20  te3_exec(p->db, 
38970 22 52 45 4c 45 41 53 45 20 66 74 73 33 22 2c 20  "RELEASE fts3", 
38971 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 65  0, 0, 0);.    }e
38972 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
38973 65 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20 22  e3_exec(p->db, "
38974 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 66 74 73 33  ROLLBACK TO fts3
38975 20 3b 20 52 45 4c 45 41 53 45 20 66 74 73 33 22   ; RELEASE fts3"
38976 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
38977 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
38978 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a  c;.}..#endif../*
38979 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
3897a 64 20 6f 66 20 66 74 73 33 5f 77 72 69 74 65 2e  d of fts3_write.
3897b 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
3897c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3897d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
3897e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
3897f 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 73 6e  gin file fts3_sn
38980 69 70 70 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ippet.c ********
38981 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38982 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
38983 0a 2a 2a 20 32 30 30 39 20 4f 63 74 20 32 33 0a  .** 2009 Oct 23.
38984 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
38985 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
38986 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
38987 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
38988 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
38989 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
3898a 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
3898b 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
3898c 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
3898d 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
3898e 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
3898f 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
38990 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
38991 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
38992 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
38993 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
38994 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
38995 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
38996 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38997 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38998 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38999 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3899a 2a 2a 2a 0a 2a 2f 0a 0a 23 69 66 20 21 64 65 66  ***.*/..#if !def
3899b 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
3899c 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
3899d 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
3899e 0a 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ...typedef struc
3899f 74 20 53 6e 69 70 70 65 74 20 53 6e 69 70 70 65  t Snippet Snippe
389a0 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  t;../*.** An ins
389a1 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
389a2 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
389a3 20 6b 65 65 70 73 20 74 72 61 63 6b 20 6f 66 20   keeps track of 
389a4 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 6d 61 74  generated.** mat
389a5 63 68 69 6e 67 2d 77 6f 72 64 20 6f 66 66 73 65  ching-word offse
389a6 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e  t information an
389a7 64 20 73 6e 69 70 70 65 74 73 2e 0a 2a 2f 0a 73  d snippets..*/.s
389a8 74 72 75 63 74 20 53 6e 69 70 70 65 74 20 7b 0a  truct Snippet {.
389a9 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 20 20 20    int nMatch;   
389aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
389ab 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
389ac 72 20 6f 66 20 6d 61 74 63 68 65 73 20 2a 2f 0a  r of matches */.
389ad 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20    int nAlloc;   
389ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
389af 20 20 2f 2a 20 53 70 61 63 65 20 61 6c 6c 6f 63    /* Space alloc
389b0 61 74 65 64 20 66 6f 72 20 61 4d 61 74 63 68 5b  ated for aMatch[
389b1 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 6e  ] */.  struct sn
389b2 69 70 70 65 74 4d 61 74 63 68 20 7b 20 20 2f 2a  ippetMatch {  /*
389b3 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65   One entry for e
389b4 61 63 68 20 6d 61 74 63 68 69 6e 67 20 74 65 72  ach matching ter
389b5 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 6e  m */.    char sn
389b6 53 74 61 74 75 73 3b 20 20 20 20 20 20 20 2f 2a  Status;       /*
389b7 20 53 74 61 74 75 73 20 66 6c 61 67 20 66 6f 72   Status flag for
389b8 20 75 73 65 20 77 68 69 6c 65 20 63 6f 6e 73 74   use while const
389b9 72 75 63 74 69 6e 67 20 73 6e 69 70 70 65 74 73  ructing snippets
389ba 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e   */.    short in
389bb 74 20 6e 42 79 74 65 3b 20 20 20 20 20 2f 2a 20  t nByte;     /* 
389bc 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
389bd 69 6e 20 74 68 65 20 74 65 72 6d 20 2a 2f 0a 20  in the term */. 
389be 20 20 20 73 68 6f 72 74 20 69 6e 74 20 69 43 6f     short int iCo
389bf 6c 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63  l;      /* The c
389c0 6f 6c 75 6d 6e 20 74 68 61 74 20 63 6f 6e 74 61  olumn that conta
389c1 69 6e 73 20 74 68 65 20 6d 61 74 63 68 20 2a 2f  ins the match */
389c2 0a 20 20 20 20 73 68 6f 72 74 20 69 6e 74 20 69  .    short int i
389c3 54 65 72 6d 3b 20 20 20 20 20 2f 2a 20 54 68 65  Term;     /* The
389c4 20 69 6e 64 65 78 20 69 6e 20 51 75 65 72 79 2e   index in Query.
389c5 70 54 65 72 6d 73 5b 5d 20 6f 66 20 74 68 65 20  pTerms[] of the 
389c6 6d 61 74 63 68 69 6e 67 20 74 65 72 6d 20 2a 2f  matching term */
389c7 0a 20 20 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b  .    int iToken;
389c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
389c9 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d 61   index of the ma
389ca 74 63 68 69 6e 67 20 64 6f 63 75 6d 65 6e 74 20  tching document 
389cb 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 69 6e 74  token */.    int
389cc 20 69 53 74 61 72 74 3b 20 20 20 20 20 20 20 20   iStart;        
389cd 20 20 2f 2a 20 54 68 65 20 6f 66 66 73 65 74 20    /* The offset 
389ce 74 6f 20 74 68 65 20 66 69 72 73 74 20 63 68 61  to the first cha
389cf 72 61 63 74 65 72 20 6f 66 20 74 68 65 20 74 65  racter of the te
389d0 72 6d 20 2a 2f 0a 20 20 7d 20 2a 61 4d 61 74 63  rm */.  } *aMatc
389d1 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
389d2 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
389d3 73 20 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69  s to space obtai
389d4 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20  ned from malloc 
389d5 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 66 66 73  */.  char *zOffs
389d6 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  et;             
389d7 20 20 20 20 20 2f 2a 20 54 65 78 74 20 72 65 6e       /* Text ren
389d8 64 65 72 69 6e 67 20 6f 66 20 61 4d 61 74 63 68  dering of aMatch
389d9 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 66 66  [] */.  int nOff
389da 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  set;            
389db 20 20 20 20 20 20 20 20 2f 2a 20 73 74 72 6c 65          /* strle
389dc 6e 28 7a 4f 66 66 73 65 74 29 20 2a 2f 0a 20 20  n(zOffset) */.  
389dd 63 68 61 72 20 2a 7a 53 6e 69 70 70 65 74 3b 20  char *zSnippet; 
389de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
389df 2f 2a 20 53 6e 69 70 70 65 74 20 74 65 78 74 20  /* Snippet text 
389e0 2a 2f 0a 20 20 69 6e 74 20 6e 53 6e 69 70 70 65  */.  int nSnippe
389e1 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
389e2 20 20 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28 7a       /* strlen(z
389e3 53 6e 69 70 70 65 74 29 20 2a 2f 0a 7d 3b 0a 0a  Snippet) */.};..
389e4 0a 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61  ./* It is not sa
389e5 66 65 20 74 6f 20 63 61 6c 6c 20 69 73 73 70 61  fe to call isspa
389e6 63 65 28 29 2c 20 74 6f 6c 6f 77 65 72 28 29 2c  ce(), tolower(),
389e7 20 6f 72 20 69 73 61 6c 6e 75 6d 28 29 20 6f 6e   or isalnum() on
389e8 0a 2a 2a 20 68 69 2d 62 69 74 2d 73 65 74 20 63  .** hi-bit-set c
389e9 68 61 72 61 63 74 65 72 73 2e 20 20 54 68 69 73  haracters.  This
389ea 20 69 73 20 74 68 65 20 73 61 6d 65 20 73 6f 6c   is the same sol
389eb 75 74 69 6f 6e 20 75 73 65 64 20 69 6e 20 74 68  ution used in th
389ec 65 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a  e.** tokenizer..
389ed 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  */.static int ft
389ee 73 33 73 6e 69 70 70 65 74 49 73 73 70 61 63 65  s3snippetIsspace
389ef 28 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74 75  (char c){.  retu
389f0 72 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20 3f  rn (c&0x80)==0 ?
389f1 20 69 73 73 70 61 63 65 28 63 29 20 3a 20 30 3b   isspace(c) : 0;
389f2 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 53 74 72  .}.../*.** A Str
389f3 69 6e 67 42 75 66 66 65 72 20 6f 62 6a 65 63 74  ingBuffer object
389f4 20 68 6f 6c 64 73 20 61 20 7a 65 72 6f 2d 74 65   holds a zero-te
389f5 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20  rminated string 
389f6 74 68 61 74 20 67 72 6f 77 73 0a 2a 2a 20 61 72  that grows.** ar
389f7 62 69 74 72 61 72 69 6c 79 20 62 79 20 61 70 70  bitrarily by app
389f8 65 6e 64 69 6e 67 2e 20 20 53 70 61 63 65 20 74  ending.  Space t
389f9 6f 20 68 6f 6c 64 20 74 68 65 20 73 74 72 69 6e  o hold the strin
389fa 67 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a  g is obtained.**
389fb 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
389fc 6c 6c 6f 63 28 29 2e 20 20 41 66 74 65 72 20 61  lloc().  After a
389fd 6e 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ny memory alloca
389fe 74 69 6f 6e 20 66 61 69 6c 75 72 65 2c 20 0a 2a  tion failure, .*
389ff 2a 20 53 74 72 69 6e 67 42 75 66 66 65 72 2e 7a  * StringBuffer.z
38a00 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20   is set to NULL 
38a01 61 6e 64 20 6e 6f 20 66 75 72 74 68 65 72 20 61  and no further a
38a02 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61 74 74  llocation is att
38a03 65 6d 70 74 65 64 2e 0a 2a 2f 0a 74 79 70 65 64  empted..*/.typed
38a04 65 66 20 73 74 72 75 63 74 20 53 74 72 69 6e 67  ef struct String
38a05 42 75 66 66 65 72 20 7b 0a 20 20 63 68 61 72 20  Buffer {.  char 
38a06 2a 7a 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  *z;         /* T
38a07 65 78 74 20 6f 66 20 74 68 65 20 73 74 72 69 6e  ext of the strin
38a08 67 2e 20 20 53 70 61 63 65 20 66 72 6f 6d 20 6d  g.  Space from m
38a09 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 69 6e 74 20  alloc. */.  int 
38a0a 6e 55 73 65 64 3b 20 20 20 20 20 20 20 2f 2a 20  nUsed;       /* 
38a0b 4e 75 6d 62 65 72 20 62 79 74 65 73 20 6f 66 20  Number bytes of 
38a0c 7a 5b 5d 20 75 73 65 64 2c 20 6e 6f 74 20 63 6f  z[] used, not co
38a0d 75 6e 74 69 6e 67 20 5c 30 30 30 20 74 65 72 6d  unting \000 term
38a0e 69 6e 61 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  inator */.  int 
38a0f 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20  nAlloc;      /* 
38a10 42 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  Bytes allocated 
38a11 66 6f 72 20 7a 5b 5d 20 2a 2f 0a 7d 20 53 74 72  for z[] */.} Str
38a12 69 6e 67 42 75 66 66 65 72 3b 0a 0a 0a 2f 2a 0a  ingBuffer;.../*.
38a13 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20  ** Initialize a 
38a14 6e 65 77 20 53 74 72 69 6e 67 42 75 66 66 65 72  new StringBuffer
38a15 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
38a16 20 66 74 73 33 53 6e 69 70 70 65 74 53 62 49 6e   fts3SnippetSbIn
38a17 69 74 28 53 74 72 69 6e 67 42 75 66 66 65 72 20  it(StringBuffer 
38a18 2a 70 29 7b 0a 20 20 70 2d 3e 6e 41 6c 6c 6f 63  *p){.  p->nAlloc
38a19 20 3d 20 31 30 30 3b 0a 20 20 70 2d 3e 6e 55 73   = 100;.  p->nUs
38a1a 65 64 20 3d 20 30 3b 0a 20 20 70 2d 3e 7a 20 3d  ed = 0;.  p->z =
38a1b 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
38a1c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 7d 0a   p->nAlloc );.}.
38a1d 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 74 65  ./*.** Append te
38a1e 78 74 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67  xt to the string
38a1f 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74   buffer..*/.stat
38a20 69 63 20 76 6f 69 64 20 66 74 73 33 53 6e 69 70  ic void fts3Snip
38a21 70 65 74 41 70 70 65 6e 64 28 53 74 72 69 6e 67  petAppend(String
38a22 42 75 66 66 65 72 20 2a 70 2c 20 63 6f 6e 73 74  Buffer *p, const
38a23 20 63 68 61 72 20 2a 7a 4e 65 77 2c 20 69 6e 74   char *zNew, int
38a24 20 6e 4e 65 77 29 7b 0a 20 20 69 66 28 20 70 2d   nNew){.  if( p-
38a25 3e 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  >z==0 ) return;.
38a26 20 20 69 66 28 20 6e 4e 65 77 3c 30 20 29 20 6e    if( nNew<0 ) n
38a27 4e 65 77 20 3d 20 28 69 6e 74 29 73 74 72 6c 65  New = (int)strle
38a28 6e 28 7a 4e 65 77 29 3b 0a 20 20 69 66 28 20 70  n(zNew);.  if( p
38a29 2d 3e 6e 55 73 65 64 20 2b 20 6e 4e 65 77 20 3e  ->nUsed + nNew >
38a2a 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20  = p->nAlloc ){. 
38a2b 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20     int nAlloc;. 
38a2c 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 3b 0a 0a     char *zNew;..
38a2d 20 20 20 20 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e      nAlloc = p->
38a2e 6e 55 73 65 64 20 2b 20 6e 4e 65 77 20 2b 20 70  nUsed + nNew + p
38a2f 2d 3e 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20 7a 4e  ->nAlloc;.    zN
38a30 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
38a31 6c 6c 6f 63 28 70 2d 3e 7a 2c 20 6e 41 6c 6c 6f  lloc(p->z, nAllo
38a32 63 29 3b 0a 20 20 20 20 69 66 28 20 7a 4e 65 77  c);.    if( zNew
38a33 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
38a34 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 29 3b  ite3_free(p->z);
38a35 0a 20 20 20 20 20 20 70 2d 3e 7a 20 3d 20 30 3b  .      p->z = 0;
38a36 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
38a37 20 20 20 7d 0a 20 20 20 20 70 2d 3e 7a 20 3d 20     }.    p->z = 
38a38 7a 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 41 6c  zNew;.    p->nAl
38a39 6c 6f 63 20 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20  loc = nAlloc;.  
38a3a 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a  }.  memcpy(&p->z
38a3b 5b 70 2d 3e 6e 55 73 65 64 5d 2c 20 7a 4e 65 77  [p->nUsed], zNew
38a3c 2c 20 6e 4e 65 77 29 3b 0a 20 20 70 2d 3e 6e 55  , nNew);.  p->nU
38a3d 73 65 64 20 2b 3d 20 6e 4e 65 77 3b 0a 20 20 70  sed += nNew;.  p
38a3e 2d 3e 7a 5b 70 2d 3e 6e 55 73 65 64 5d 20 3d 20  ->z[p->nUsed] = 
38a3f 30 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 74 68 65 20  0;.}../* If the 
38a40 53 74 72 69 6e 67 42 75 66 66 65 72 20 65 6e 64  StringBuffer end
38a41 73 20 69 6e 20 73 6f 6d 65 74 68 69 6e 67 20 6f  s in something o
38a42 74 68 65 72 20 74 68 61 6e 20 77 68 69 74 65 20  ther than white 
38a43 73 70 61 63 65 2c 20 61 64 64 20 61 0a 2a 2a 20  space, add a.** 
38a44 73 69 6e 67 6c 65 20 73 70 61 63 65 20 63 68 61  single space cha
38a45 72 61 63 74 65 72 20 74 6f 20 74 68 65 20 65 6e  racter to the en
38a46 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
38a47 64 20 66 74 73 33 53 6e 69 70 70 65 74 41 70 70  d fts3SnippetApp
38a48 65 6e 64 57 68 69 74 65 53 70 61 63 65 28 53 74  endWhiteSpace(St
38a49 72 69 6e 67 42 75 66 66 65 72 20 2a 70 29 7b 0a  ringBuffer *p){.
38a4a 20 20 69 66 28 20 70 2d 3e 7a 20 26 26 20 70 2d    if( p->z && p-
38a4b 3e 6e 55 73 65 64 20 26 26 20 21 66 74 73 33 73  >nUsed && !fts3s
38a4c 6e 69 70 70 65 74 49 73 73 70 61 63 65 28 70 2d  nippetIsspace(p-
38a4d 3e 7a 5b 70 2d 3e 6e 55 73 65 64 2d 31 5d 29 20  >z[p->nUsed-1]) 
38a4e 29 7b 0a 20 20 20 20 66 74 73 33 53 6e 69 70 70  ){.    fts3Snipp
38a4f 65 74 41 70 70 65 6e 64 28 70 2c 20 22 20 22 2c  etAppend(p, " ",
38a50 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 52   1);.  }.}../* R
38a51 65 6d 6f 76 65 20 77 68 69 74 65 20 73 70 61 63  emove white spac
38a52 65 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f  e from the end o
38a53 66 20 74 68 65 20 53 74 72 69 6e 67 42 75 66 66  f the StringBuff
38a54 65 72 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  er */.static voi
38a55 64 20 66 74 73 33 53 6e 69 70 70 65 74 54 72 69  d fts3SnippetTri
38a56 6d 57 68 69 74 65 53 70 61 63 65 28 53 74 72 69  mWhiteSpace(Stri
38a57 6e 67 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20  ngBuffer *p){.  
38a58 69 66 28 20 70 2d 3e 7a 20 29 7b 0a 20 20 20 20  if( p->z ){.    
38a59 77 68 69 6c 65 28 20 70 2d 3e 6e 55 73 65 64 20  while( p->nUsed 
38a5a 26 26 20 66 74 73 33 73 6e 69 70 70 65 74 49 73  && fts3snippetIs
38a5b 73 70 61 63 65 28 70 2d 3e 7a 5b 70 2d 3e 6e 55  space(p->z[p->nU
38a5c 73 65 64 2d 31 5d 29 20 29 7b 0a 20 20 20 20 20  sed-1]) ){.     
38a5d 20 70 2d 3e 6e 55 73 65 64 2d 2d 3b 0a 20 20 20   p->nUsed--;.   
38a5e 20 7d 0a 20 20 20 20 70 2d 3e 7a 5b 70 2d 3e 6e   }.    p->z[p->n
38a5f 55 73 65 64 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d  Used] = 0;.  }.}
38a60 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 6c 65 61 73 65  ../* .** Release
38a61 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f   all memory asso
38a62 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
38a63 53 6e 69 70 70 65 74 20 73 74 72 75 63 74 75 72  Snippet structur
38a64 65 20 70 61 73 73 65 64 20 61 73 0a 2a 2a 20 61  e passed as.** a
38a65 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73  n argument..*/.s
38a66 74 61 74 69 63 20 76 6f 69 64 20 66 74 73 33 53  tatic void fts3S
38a67 6e 69 70 70 65 74 46 72 65 65 28 53 6e 69 70 70  nippetFree(Snipp
38a68 65 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  et *p){.  if( p 
38a69 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
38a6a 72 65 65 28 70 2d 3e 61 4d 61 74 63 68 29 3b 0a  ree(p->aMatch);.
38a6b 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
38a6c 28 70 2d 3e 7a 4f 66 66 73 65 74 29 3b 0a 20 20  (p->zOffset);.  
38a6d 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
38a6e 2d 3e 7a 53 6e 69 70 70 65 74 29 3b 0a 20 20 20  ->zSnippet);.   
38a6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29   sqlite3_free(p)
38a70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
38a71 70 70 65 6e 64 20 61 20 73 69 6e 67 6c 65 20 65  ppend a single e
38a72 6e 74 72 79 20 74 6f 20 74 68 65 20 70 2d 3e 61  ntry to the p->a
38a73 4d 61 74 63 68 5b 5d 20 6c 6f 67 2e 0a 2a 2f 0a  Match[] log..*/.
38a74 73 74 61 74 69 63 20 69 6e 74 20 73 6e 69 70 70  static int snipp
38a75 65 74 41 70 70 65 6e 64 4d 61 74 63 68 28 0a 20  etAppendMatch(. 
38a76 20 53 6e 69 70 70 65 74 20 2a 70 2c 20 20 20 20   Snippet *p,    
38a77 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 70             /* Ap
38a78 70 65 6e 64 20 74 68 65 20 65 6e 74 72 79 20 74  pend the entry t
38a79 6f 20 74 68 69 73 20 73 6e 69 70 70 65 74 20 2a  o this snippet *
38a7a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 69 6e  /.  int iCol, in
38a7b 74 20 69 54 65 72 6d 2c 20 20 20 20 20 20 2f 2a  t iTerm,      /*
38a7c 20 54 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20   The column and 
38a7d 71 75 65 72 79 20 74 65 72 6d 20 2a 2f 0a 20 20  query term */.  
38a7e 69 6e 74 20 69 54 6f 6b 65 6e 2c 20 20 20 20 20  int iToken,     
38a7f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 74            /* Mat
38a80 63 68 69 6e 67 20 74 6f 6b 65 6e 20 69 6e 20 64  ching token in d
38a81 6f 63 75 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  ocument */.  int
38a82 20 69 53 74 61 72 74 2c 20 69 6e 74 20 6e 42 79   iStart, int nBy
38a83 74 65 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74  te     /* Offset
38a84 20 61 6e 64 20 73 69 7a 65 20 6f 66 20 74 68 65   and size of the
38a85 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20 69   match */.){.  i
38a86 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 73  nt i;.  struct s
38a87 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70 4d 61  nippetMatch *pMa
38a88 74 63 68 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4d  tch;.  if( p->nM
38a89 61 74 63 68 2b 31 3e 3d 70 2d 3e 6e 41 6c 6c 6f  atch+1>=p->nAllo
38a8a 63 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  c ){.    struct 
38a8b 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70 4e  snippetMatch *pN
38a8c 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f  ew;.    p->nAllo
38a8d 63 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20  c = p->nAlloc*2 
38a8e 2b 20 31 30 3b 0a 20 20 20 20 70 4e 65 77 20 3d  + 10;.    pNew =
38a8f 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
38a90 28 70 2d 3e 61 4d 61 74 63 68 2c 20 70 2d 3e 6e  (p->aMatch, p->n
38a91 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 2d 3e  Alloc*sizeof(p->
38a92 61 4d 61 74 63 68 5b 30 5d 29 20 29 3b 0a 20 20  aMatch[0]) );.  
38a93 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b    if( pNew==0 ){
38a94 0a 20 20 20 20 20 20 70 2d 3e 61 4d 61 74 63 68  .      p->aMatch
38a95 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 6e   = 0;.      p->n
38a96 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20  Match = 0;.     
38a97 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a   p->nAlloc = 0;.
38a98 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
38a99 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
38a9a 0a 20 20 20 20 70 2d 3e 61 4d 61 74 63 68 20 3d  .    p->aMatch =
38a9b 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69 20 3d   pNew;.  }.  i =
38a9c 20 70 2d 3e 6e 4d 61 74 63 68 2b 2b 3b 0a 20 20   p->nMatch++;.  
38a9d 70 4d 61 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61  pMatch = &p->aMa
38a9e 74 63 68 5b 69 5d 3b 0a 20 20 70 4d 61 74 63 68  tch[i];.  pMatch
38a9f 2d 3e 69 43 6f 6c 20 3d 20 28 73 68 6f 72 74 29  ->iCol = (short)
38aa0 69 43 6f 6c 3b 0a 20 20 70 4d 61 74 63 68 2d 3e  iCol;.  pMatch->
38aa1 69 54 65 72 6d 20 3d 20 28 73 68 6f 72 74 29 69  iTerm = (short)i
38aa2 54 65 72 6d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e  Term;.  pMatch->
38aa3 69 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65 6e 3b  iToken = iToken;
38aa4 0a 20 20 70 4d 61 74 63 68 2d 3e 69 53 74 61 72  .  pMatch->iStar
38aa5 74 20 3d 20 69 53 74 61 72 74 3b 0a 20 20 70 4d  t = iStart;.  pM
38aa6 61 74 63 68 2d 3e 6e 42 79 74 65 20 3d 20 28 73  atch->nByte = (s
38aa7 68 6f 72 74 29 6e 42 79 74 65 3b 0a 20 20 72 65  hort)nByte;.  re
38aa8 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
38aa9 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 69 6e 67 20  }../*.** Sizing 
38aaa 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
38aab 74 68 65 20 63 69 72 63 75 6c 61 72 20 62 75 66  the circular buf
38aac 66 65 72 20 75 73 65 64 20 69 6e 20 73 6e 69 70  fer used in snip
38aad 70 65 74 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75  petOffsetsOfColu
38aae 6d 6e 28 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  mn().*/.#define 
38aaf 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 20 20 20  FTS3_ROTOR_SZ   
38ab0 28 33 32 29 0a 23 64 65 66 69 6e 65 20 46 54 53  (32).#define FTS
38ab1 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 20 28 46 54  3_ROTOR_MASK (FT
38ab2 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d 31 29 0a 0a  S3_ROTOR_SZ-1)..
38ab3 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74  /*.** Function t
38ab4 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  o iterate throug
38ab5 68 20 74 68 65 20 74 6f 6b 65 6e 73 20 6f 66 20  h the tokens of 
38ab6 61 20 63 6f 6d 70 69 6c 65 64 20 65 78 70 72 65  a compiled expre
38ab7 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78 63  ssion..**.** Exc
38ab8 65 70 74 2c 20 73 6b 69 70 20 61 6c 6c 20 74 6f  ept, skip all to
38ab9 6b 65 6e 73 20 6f 6e 20 74 68 65 20 72 69 67 68  kens on the righ
38aba 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 61  t-hand side of a
38abb 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e 0a 2a   NOT operator..*
38abc 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
38abd 69 73 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20  is used to find 
38abe 74 6f 6b 65 6e 73 20 61 73 20 70 61 72 74 20 6f  tokens as part o
38abf 66 20 73 6e 69 70 70 65 74 20 61 6e 64 20 6f 66  f snippet and of
38ac0 66 73 65 74 0a 2a 2a 20 67 65 6e 65 72 61 74 69  fset.** generati
38ac1 6f 6e 20 61 6e 64 20 77 65 20 64 6f 20 6e 74 20  on and we do nt 
38ac2 77 61 6e 74 20 73 6e 69 70 70 65 74 73 20 61 6e  want snippets an
38ac3 64 20 6f 66 66 73 65 74 73 20 74 6f 20 72 65 70  d offsets to rep
38ac4 6f 72 74 20 6d 61 74 63 68 65 73 0a 2a 2a 20 66  ort matches.** f
38ac5 6f 72 20 74 6f 6b 65 6e 73 20 6f 6e 20 74 68 65  or tokens on the
38ac6 20 52 48 53 20 6f 66 20 61 20 4e 4f 54 2e 0a 2a   RHS of a NOT..*
38ac7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73  /.static int fts
38ac8 33 4e 65 78 74 45 78 70 72 54 6f 6b 65 6e 28 46  3NextExprToken(F
38ac9 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72  ts3Expr **ppExpr
38aca 2c 20 69 6e 74 20 2a 70 69 54 6f 6b 65 6e 29 7b  , int *piToken){
38acb 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70 20 3d  .  Fts3Expr *p =
38acc 20 2a 70 70 45 78 70 72 3b 0a 20 20 69 6e 74 20   *ppExpr;.  int 
38acd 69 54 6f 6b 65 6e 20 3d 20 2a 70 69 54 6f 6b 65  iToken = *piToke
38ace 6e 3b 0a 20 20 69 66 28 20 69 54 6f 6b 65 6e 3c  n;.  if( iToken<
38acf 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 20 74  0 ){.    /* In t
38ad0 68 69 73 20 63 61 73 65 20 74 68 65 20 65 78 70  his case the exp
38ad1 72 65 73 73 69 6f 6e 20 70 20 69 73 20 74 68 65  ression p is the
38ad2 20 72 6f 6f 74 20 6f 66 20 61 6e 20 65 78 70 72   root of an expr
38ad3 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 20 20 20  ession tree..   
38ad4 20 2a 2a 20 4d 6f 76 65 20 74 6f 20 74 68 65 20   ** Move to the 
38ad5 66 69 72 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74  first token in t
38ad6 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  he expression tr
38ad7 65 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77  ee..    */.    w
38ad8 68 69 6c 65 28 20 70 2d 3e 70 4c 65 66 74 20 29  hile( p->pLeft )
38ad9 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70  {.      p = p->p
38ada 4c 65 66 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Left;.    }.    
38adb 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 7d 65  iToken = 0;.  }e
38adc 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
38add 70 20 26 26 20 70 2d 3e 65 54 79 70 65 3d 3d 46  p && p->eType==F
38ade 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29  TSQUERY_PHRASE )
38adf 3b 0a 20 20 20 20 69 66 28 20 69 54 6f 6b 65 6e  ;.    if( iToken
38ae0 3c 28 70 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  <(p->pPhrase->nT
38ae1 6f 6b 65 6e 2d 31 29 20 29 7b 0a 20 20 20 20 20  oken-1) ){.     
38ae2 20 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 7d   iToken++;.    }
38ae3 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 54 6f 6b  else{.      iTok
38ae4 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 77 68  en = 0;.      wh
38ae5 69 6c 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20  ile( p->pParent 
38ae6 26 26 20 70 2d 3e 70 50 61 72 65 6e 74 2d 3e 70  && p->pParent->p
38ae7 4c 65 66 74 21 3d 70 20 29 7b 0a 20 20 20 20 20  Left!=p ){.     
38ae8 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50     assert( p->pP
38ae9 61 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d 3d 70  arent->pRight==p
38aea 20 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20   );.        p = 
38aeb 70 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20  p->pParent;.    
38aec 20 20 7d 0a 20 20 20 20 20 20 70 20 3d 20 70 2d    }.      p = p-
38aed 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20  >pParent;.      
38aee 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20  if( p ){.       
38aef 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52 69 67   assert( p->pRig
38af0 68 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  ht!=0 );.       
38af1 20 70 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a   p = p->pRight;.
38af2 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
38af3 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20  ->pLeft ){.     
38af4 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c 65 66       p = p->pLef
38af5 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
38af6 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
38af7 20 20 2a 70 70 45 78 70 72 20 3d 20 70 3b 0a 20    *ppExpr = p;. 
38af8 20 2a 70 69 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b   *piToken = iTok
38af9 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20 70 3f 31  en;.  return p?1
38afa 3a 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  :0;.}../*.** Ret
38afb 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
38afc 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20  expression node 
38afd 70 45 78 70 72 20 69 73 20 6c 6f 63 61 74 65 64  pExpr is located
38afe 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20   beneath the.** 
38aff 52 48 53 20 6f 66 20 61 20 4e 4f 54 20 6f 70 65  RHS of a NOT ope
38b00 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  rator..*/.static
38b01 20 69 6e 74 20 66 74 73 33 45 78 70 72 42 65 6e   int fts3ExprBen
38b02 65 61 74 68 4e 6f 74 28 46 74 73 33 45 78 70 72  eathNot(Fts3Expr
38b03 20 2a 70 29 7b 0a 20 20 46 74 73 33 45 78 70 72   *p){.  Fts3Expr
38b04 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 77 68 69   *pParent;.  whi
38b05 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 70 50 61  le( p ){.    pPa
38b06 72 65 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65 6e  rent = p->pParen
38b07 74 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72 65  t;.    if( pPare
38b08 6e 74 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 65  nt && pParent->e
38b09 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e  Type==FTSQUERY_N
38b0a 4f 54 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 70  OT && pParent->p
38b0b 52 69 67 68 74 3d 3d 70 20 29 7b 0a 20 20 20 20  Right==p ){.    
38b0c 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
38b0d 7d 0a 20 20 20 20 70 20 3d 20 70 50 61 72 65 6e  }.    p = pParen
38b0e 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
38b0f 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  0;.}../*.** Add 
38b10 65 6e 74 72 69 65 73 20 74 6f 20 70 53 6e 69 70  entries to pSnip
38b11 70 65 74 2d 3e 61 4d 61 74 63 68 5b 5d 20 66 6f  pet->aMatch[] fo
38b12 72 20 65 76 65 72 79 20 6d 61 74 63 68 20 74 68  r every match th
38b13 61 74 20 6f 63 63 75 72 73 20 61 67 61 69 6e 73  at occurs agains
38b14 74 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 7a 44  t.** document zD
38b15 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 77 68  oc[0..nDoc-1] wh
38b16 69 63 68 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ich is stored in
38b17 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e   column iColumn.
38b18 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
38b19 6e 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66 43  nippetOffsetsOfC
38b1a 6f 6c 75 6d 6e 28 0a 20 20 46 74 73 33 43 75 72  olumn(.  Fts3Cur
38b1b 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20 20  sor *pCur,      
38b1c 20 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c 74 65     /* The fullte
38b1d 73 74 20 73 65 61 72 63 68 20 63 75 72 73 6f 72  st search cursor
38b1e 20 2a 2f 0a 20 20 53 6e 69 70 70 65 74 20 2a 70   */.  Snippet *p
38b1f 53 6e 69 70 70 65 74 2c 20 20 20 20 20 20 20 20  Snippet,        
38b20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 6e 69 70       /* The Snip
38b21 70 65 74 20 6f 62 6a 65 63 74 20 74 6f 20 62 65  pet object to be
38b22 20 66 69 6c 6c 65 64 20 69 6e 20 2a 2f 0a 20 20   filled in */.  
38b23 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20  int iColumn,    
38b24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38b25 2a 20 49 6e 64 65 78 20 6f 66 20 66 75 6c 6c 74  * Index of fullt
38b26 65 78 74 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e  ext table column
38b27 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
38b28 20 2a 7a 44 6f 63 2c 20 20 20 20 20 20 20 20 20   *zDoc,         
38b29 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20       /* Text of 
38b2a 74 68 65 20 66 75 6c 6c 74 65 78 74 20 74 61 62  the fulltext tab
38b2b 6c 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69  le column */.  i
38b2c 6e 74 20 6e 44 6f 63 20 20 20 20 20 20 20 20 20  nt nDoc         
38b2d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38b2e 20 4c 65 6e 67 74 68 20 6f 66 20 7a 44 6f 63 20   Length of zDoc 
38b2f 69 6e 20 62 79 74 65 73 20 2a 2f 0a 29 7b 0a 20  in bytes */.){. 
38b30 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
38b31 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
38b32 2a 70 54 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54  *pTModule;  /* T
38b33 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64  he tokenizer mod
38b34 75 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ule */.  sqlite3
38b35 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
38b36 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 20 20 20  enizer;         
38b37 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69      /* The speci
38b38 66 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  fic tokenizer */
38b39 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
38b3a 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 54 43  izer_cursor *pTC
38b3b 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a  ursor;        /*
38b3c 20 54 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f   Tokenizer curso
38b3d 72 20 2a 2f 0a 20 20 46 74 73 33 54 61 62 6c 65  r */.  Fts3Table
38b3e 20 2a 70 56 74 61 62 3b 20 20 20 20 20 20 20 20   *pVtab;        
38b3f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
38b40 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 20 2a  ull text index *
38b41 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
38b42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38b43 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
38b44 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
38b45 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20   the index */.  
38b46 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
38b47 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38b48 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
38b49 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 72  nters */.  int r
38b4a 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
38b4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38b4c 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
38b4d 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
38b4e 20 6d 61 74 63 68 2c 20 70 72 65 76 4d 61 74 63   match, prevMatc
38b4f 68 3b 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61  h;       /* Phra
38b50 73 65 20 73 65 61 72 63 68 20 62 69 74 6d 61 73  se search bitmas
38b51 6b 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ks */.  const ch
38b52 61 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20  ar *zToken;     
38b53 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38b54 4e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  Next token from 
38b55 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  the tokenizer */
38b56 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20  .  int nToken;  
38b57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38b58 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
38b59 6f 66 20 7a 54 6f 6b 65 6e 20 2a 2f 0a 20 20 69  of zToken */.  i
38b5a 6e 74 20 69 42 65 67 69 6e 2c 20 69 45 6e 64 2c  nt iBegin, iEnd,
38b5b 20 69 50 6f 73 3b 20 20 20 20 20 20 20 20 20 20   iPos;          
38b5c 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f      /* Offsets o
38b5d 66 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20  f beginning and 
38b5e 65 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  end */..  /* The
38b5f 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61   following varia
38b60 62 6c 65 73 20 6b 65 65 70 20 61 20 63 69 72 63  bles keep a circ
38b61 75 6c 61 72 20 62 75 66 66 65 72 20 6f 66 20 74  ular buffer of t
38b62 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20 66 65 77  he last.  ** few
38b63 20 74 6f 6b 65 6e 73 20 2a 2f 0a 20 20 75 6e 73   tokens */.  uns
38b64 69 67 6e 65 64 20 69 6e 74 20 69 52 6f 74 6f 72  igned int iRotor
38b65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
38b66 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75    /* Index of cu
38b67 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20  rrent token */. 
38b68 20 69 6e 74 20 69 52 6f 74 6f 72 42 65 67 69 6e   int iRotorBegin
38b69 5b 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b  [FTS3_ROTOR_SZ];
38b6a 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e 69        /* Beginni
38b6b 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b  ng offset of tok
38b6c 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 74  en */.  int iRot
38b6d 6f 72 4c 65 6e 5b 46 54 53 33 5f 52 4f 54 4f 52  orLen[FTS3_ROTOR
38b6e 5f 53 5a 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20  _SZ];        /* 
38b6f 4c 65 6e 67 74 68 20 6f 66 20 74 6f 6b 65 6e 20  Length of token 
38b70 2a 2f 0a 0a 20 20 70 56 74 61 62 20 3d 20 20 28  */..  pVtab =  (
38b71 46 74 73 33 54 61 62 6c 65 20 2a 29 70 43 75 72  Fts3Table *)pCur
38b72 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b 0a 20 20  ->base.pVtab;.  
38b73 6e 43 6f 6c 75 6d 6e 20 3d 20 70 56 74 61 62 2d  nColumn = pVtab-
38b74 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 70 54 6f 6b  >nColumn;.  pTok
38b75 65 6e 69 7a 65 72 20 3d 20 70 56 74 61 62 2d 3e  enizer = pVtab->
38b76 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 70 54  pTokenizer;.  pT
38b77 4d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69  Module = pTokeni
38b78 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  zer->pModule;.  
38b79 72 63 20 3d 20 70 54 4d 6f 64 75 6c 65 2d 3e 78  rc = pTModule->x
38b7a 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c  Open(pTokenizer,
38b7b 20 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 26 70 54   zDoc, nDoc, &pT
38b7c 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 72  Cursor);.  if( r
38b7d 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
38b7e 20 70 54 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65   pTCursor->pToke
38b7f 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
38b80 65 72 3b 0a 0a 20 20 70 72 65 76 4d 61 74 63 68  er;..  prevMatch
38b81 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28   = 0;.  while( (
38b82 72 63 20 3d 20 70 54 4d 6f 64 75 6c 65 2d 3e 78  rc = pTModule->x
38b83 4e 65 78 74 28 70 54 43 75 72 73 6f 72 2c 20 26  Next(pTCursor, &
38b84 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c  zToken, &nToken,
38b85 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
38b86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38b87 26 69 42 65 67 69 6e 2c 20 26 69 45 6e 64 2c 20  &iBegin, &iEnd, 
38b88 26 69 50 6f 73 29 29 3d 3d 53 51 4c 49 54 45 5f  &iPos))==SQLITE_
38b89 4f 4b 20 29 7b 0a 20 20 20 20 46 74 73 33 45 78  OK ){.    Fts3Ex
38b8a 70 72 20 2a 70 49 74 65 72 20 3d 20 70 43 75 72  pr *pIter = pCur
38b8b 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 69 6e 74  ->pExpr;.    int
38b8c 20 69 49 74 65 72 20 3d 20 2d 31 3b 0a 20 20 20   iIter = -1;.   
38b8d 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 69 52 6f   iRotorBegin[iRo
38b8e 74 6f 72 26 46 54 53 33 5f 52 4f 54 4f 52 5f 4d  tor&FTS3_ROTOR_M
38b8f 41 53 4b 5d 20 3d 20 69 42 65 67 69 6e 3b 0a 20  ASK] = iBegin;. 
38b90 20 20 20 69 52 6f 74 6f 72 4c 65 6e 5b 69 52 6f     iRotorLen[iRo
38b91 74 6f 72 26 46 54 53 33 5f 52 4f 54 4f 52 5f 4d  tor&FTS3_ROTOR_M
38b92 41 53 4b 5d 20 3d 20 69 45 6e 64 2d 69 42 65 67  ASK] = iEnd-iBeg
38b93 69 6e 3b 0a 20 20 20 20 6d 61 74 63 68 20 3d 20  in;.    match = 
38b94 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  0;.    for(i=0; 
38b95 69 3c 28 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a  i<(FTS3_ROTOR_SZ
38b96 2d 31 29 20 26 26 20 66 74 73 33 4e 65 78 74 45  -1) && fts3NextE
38b97 78 70 72 54 6f 6b 65 6e 28 26 70 49 74 65 72 2c  xprToken(&pIter,
38b98 20 26 69 49 74 65 72 29 3b 20 69 2b 2b 29 7b 0a   &iIter); i++){.
38b99 20 20 20 20 20 20 69 6e 74 20 6e 50 68 72 61 73        int nPhras
38b9a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
38b9b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
38b9c 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 63 75 72  of tokens in cur
38b9d 72 65 6e 74 20 70 68 72 61 73 65 20 2a 2f 0a 20  rent phrase */. 
38b9e 20 20 20 20 20 73 74 72 75 63 74 20 50 68 72 61       struct Phra
38b9f 73 65 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b  seToken *pToken;
38ba0 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
38ba1 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69  token */.      i
38ba2 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt iCol;        
38ba3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38ba4 2a 20 43 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 2a  * Column index *
38ba5 2f 0a 0a 20 20 20 20 20 20 69 66 28 20 66 74 73  /..      if( fts
38ba6 33 45 78 70 72 42 65 6e 65 61 74 68 4e 6f 74 28  3ExprBeneathNot(
38ba7 70 49 74 65 72 29 20 29 20 63 6f 6e 74 69 6e 75  pIter) ) continu
38ba8 65 3b 0a 20 20 20 20 20 20 6e 50 68 72 61 73 65  e;.      nPhrase
38ba9 20 3d 20 70 49 74 65 72 2d 3e 70 50 68 72 61 73   = pIter->pPhras
38baa 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20  e->nToken;.     
38bab 20 70 54 6f 6b 65 6e 20 3d 20 26 70 49 74 65 72   pToken = &pIter
38bac 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  ->pPhrase->aToke
38bad 6e 5b 69 49 74 65 72 5d 3b 0a 20 20 20 20 20 20  n[iIter];.      
38bae 69 43 6f 6c 20 3d 20 70 49 74 65 72 2d 3e 70 50  iCol = pIter->pP
38baf 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a  hrase->iColumn;.
38bb0 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d        if( iCol>=
38bb1 30 20 26 26 20 69 43 6f 6c 3c 6e 43 6f 6c 75 6d  0 && iCol<nColum
38bb2 6e 20 26 26 20 69 43 6f 6c 21 3d 69 43 6f 6c 75  n && iCol!=iColu
38bb3 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  mn ) continue;. 
38bb4 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 2d       if( pToken-
38bb5 3e 6e 3e 6e 54 6f 6b 65 6e 20 29 20 63 6f 6e 74  >n>nToken ) cont
38bb6 69 6e 75 65 3b 0a 20 20 20 20 20 20 69 66 28 20  inue;.      if( 
38bb7 21 70 54 6f 6b 65 6e 2d 3e 69 73 50 72 65 66 69  !pToken->isPrefi
38bb8 78 20 26 26 20 70 54 6f 6b 65 6e 2d 3e 6e 3c 6e  x && pToken->n<n
38bb9 54 6f 6b 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65  Token ) continue
38bba 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
38bbb 70 54 6f 6b 65 6e 2d 3e 6e 3c 3d 6e 54 6f 6b 65  pToken->n<=nToke
38bbc 6e 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d  n );.      if( m
38bbd 65 6d 63 6d 70 28 70 54 6f 6b 65 6e 2d 3e 7a 2c  emcmp(pToken->z,
38bbe 20 7a 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d   zToken, pToken-
38bbf 3e 6e 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  >n) ) continue;.
38bc0 20 20 20 20 20 20 69 66 28 20 69 49 74 65 72 3e        if( iIter>
38bc1 30 20 26 26 20 28 70 72 65 76 4d 61 74 63 68 20  0 && (prevMatch 
38bc2 26 20 28 31 3c 3c 69 29 29 3d 3d 30 20 29 20 63  & (1<<i))==0 ) c
38bc3 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 6d  ontinue;.      m
38bc4 61 74 63 68 20 7c 3d 20 31 3c 3c 69 3b 0a 20 20  atch |= 1<<i;.  
38bc5 20 20 20 20 69 66 28 20 69 3d 3d 28 46 54 53 33      if( i==(FTS3
38bc6 5f 52 4f 54 4f 52 5f 53 5a 2d 32 29 20 7c 7c 20  _ROTOR_SZ-2) || 
38bc7 6e 50 68 72 61 73 65 3d 3d 69 49 74 65 72 2b 31  nPhrase==iIter+1
38bc8 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   ){.        for(
38bc9 6a 3d 6e 50 68 72 61 73 65 2d 31 3b 20 6a 3e 3d  j=nPhrase-1; j>=
38bca 30 3b 20 6a 2d 2d 29 7b 0a 20 20 20 20 20 20 20  0; j--){.       
38bcb 20 20 20 69 6e 74 20 6b 20 3d 20 28 69 52 6f 74     int k = (iRot
38bcc 6f 72 2d 6a 29 20 26 20 46 54 53 33 5f 52 4f 54  or-j) & FTS3_ROT
38bcd 4f 52 5f 4d 41 53 4b 3b 0a 20 20 20 20 20 20 20  OR_MASK;.       
38bce 20 20 20 72 63 20 3d 20 73 6e 69 70 70 65 74 41     rc = snippetA
38bcf 70 70 65 6e 64 4d 61 74 63 68 28 70 53 6e 69 70  ppendMatch(pSnip
38bd0 70 65 74 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 2d  pet, iColumn, i-
38bd1 6a 2c 20 69 50 6f 73 2d 6a 2c 0a 20 20 20 20 20  j, iPos-j,.     
38bd2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38bd3 20 20 20 20 20 20 20 20 20 20 20 20 20 69 52 6f               iRo
38bd4 74 6f 72 42 65 67 69 6e 5b 6b 5d 2c 20 69 52 6f  torBegin[k], iRo
38bd5 74 6f 72 4c 65 6e 5b 6b 5d 29 3b 0a 20 20 20 20  torLen[k]);.    
38bd6 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67        if( rc ) g
38bd7 6f 74 6f 20 65 6e 64 5f 6f 66 66 73 65 74 73 5f  oto end_offsets_
38bd8 6f 66 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20  of_column;.     
38bd9 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
38bda 20 7d 0a 20 20 20 20 70 72 65 76 4d 61 74 63 68   }.    prevMatch
38bdb 20 3d 20 6d 61 74 63 68 3c 3c 31 3b 0a 20 20 20   = match<<1;.   
38bdc 20 69 52 6f 74 6f 72 2b 2b 3b 0a 20 20 7d 0a 65   iRotor++;.  }.e
38bdd 6e 64 5f 6f 66 66 73 65 74 73 5f 6f 66 5f 63 6f  nd_offsets_of_co
38bde 6c 75 6d 6e 3a 0a 20 20 70 54 4d 6f 64 75 6c 65  lumn:.  pTModule
38bdf 2d 3e 78 43 6c 6f 73 65 28 70 54 43 75 72 73 6f  ->xClose(pTCurso
38be0 72 29 3b 20 20 0a 20 20 72 65 74 75 72 6e 20 72  r);  .  return r
38be1 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f  c==SQLITE_DONE ?
38be2 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63 3b   SQLITE_OK : rc;
38be3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
38be4 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68   entries from th
38be5 65 20 70 53 6e 69 70 70 65 74 20 73 74 72 75 63  e pSnippet struc
38be6 74 75 72 65 20 74 6f 20 61 63 63 6f 75 6e 74 20  ture to account 
38be7 66 6f 72 20 74 68 65 20 4e 45 41 52 0a 2a 2a 20  for the NEAR.** 
38be8 6f 70 65 72 61 74 6f 72 2e 20 57 68 65 6e 20 74  operator. When t
38be9 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20 70  his is called, p
38bea 53 6e 69 70 70 65 74 20 63 6f 6e 74 61 69 6e 73  Snippet contains
38beb 20 74 68 65 20 6c 69 73 74 20 6f 66 20 74 6f 6b   the list of tok
38bec 65 6e 20 0a 2a 2a 20 6f 66 66 73 65 74 73 20 70  en .** offsets p
38bed 72 6f 64 75 63 65 64 20 62 79 20 74 72 65 61 74  roduced by treat
38bee 69 6e 67 20 61 6c 6c 20 4e 45 41 52 20 6f 70 65  ing all NEAR ope
38bef 72 61 74 6f 72 73 20 61 73 20 41 4e 44 20 6f 70  rators as AND op
38bf0 65 72 61 74 6f 72 73 2e 0a 2a 2a 20 54 68 69 73  erators..** This
38bf1 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d 6f 76 65   function remove
38bf2 73 20 61 6e 79 20 65 6e 74 72 69 65 73 20 74 68  s any entries th
38bf3 61 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  at should not be
38bf4 20 70 72 65 73 65 6e 74 20 61 66 74 65 72 0a 2a   present after.*
38bf5 2a 20 61 63 63 6f 75 6e 74 69 6e 67 20 66 6f 72  * accounting for
38bf6 20 74 68 65 20 4e 45 41 52 20 72 65 73 74 72 69   the NEAR restri
38bf7 63 74 69 6f 6e 2e 20 46 6f 72 20 65 78 61 6d 70  ction. For examp
38bf8 6c 65 2c 20 69 66 20 74 68 65 20 71 75 65 72 69  le, if the queri
38bf9 65 64 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 69  ed.** document i
38bfa 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 41 20  s:.**.**     "A 
38bfb 42 20 43 20 44 20 45 20 41 22 0a 2a 2a 0a 2a 2a  B C D E A".**.**
38bfc 20 61 6e 64 20 74 68 65 20 71 75 65 72 79 20 69   and the query i
38bfd 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20 20 41 20  s:.** .**     A 
38bfe 4e 45 41 52 2f 30 20 45 0a 2a 2a 0a 2a 2a 20 74  NEAR/0 E.**.** t
38bff 68 65 6e 20 77 68 65 6e 20 74 68 69 73 20 66 75  hen when this fu
38c00 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
38c01 20 74 68 65 20 53 6e 69 70 70 65 74 20 63 6f 6e   the Snippet con
38c02 74 61 69 6e 73 20 74 6f 6b 65 6e 20 6f 66 66 73  tains token offs
38c03 65 74 73 0a 2a 2a 20 30 2c 20 34 20 61 6e 64 20  ets.** 0, 4 and 
38c04 35 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  5. This function
38c05 20 72 65 6d 6f 76 65 73 20 74 68 65 20 22 30 22   removes the "0"
38c06 20 65 6e 74 72 79 20 28 62 65 63 61 75 73 65 20   entry (because 
38c07 74 68 65 20 66 69 72 73 74 20 41 0a 2a 2a 20 69  the first A.** i
38c08 73 20 6e 6f 74 20 6e 65 61 72 20 65 6e 6f 75 67  s not near enoug
38c09 68 20 74 6f 20 61 6e 20 45 29 2e 0a 2a 2a 0a 2a  h to an E)..**.*
38c0a 2a 20 57 68 65 6e 20 74 68 69 73 20 66 75 6e 63  * When this func
38c0b 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20  tion is called, 
38c0c 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74 65  the value pointe
38c0d 64 20 74 6f 20 62 79 20 70 61 72 61 6d 65 74 65  d to by paramete
38c0e 72 20 70 69 4c 65 66 74 20 69 73 0a 2a 2a 20 74  r piLeft is.** t
38c0f 68 65 20 69 6e 74 65 67 65 72 20 69 64 20 6f 66  he integer id of
38c10 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 74   the left-most t
38c11 6f 6b 65 6e 20 69 6e 20 74 68 65 20 65 78 70 72  oken in the expr
38c12 65 73 73 69 6f 6e 20 74 72 65 65 20 68 65 61 64  ession tree head
38c13 65 64 20 62 79 0a 2a 2a 20 70 45 78 70 72 2e 20  ed by.** pExpr. 
38c14 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e  This function in
38c15 63 72 65 6d 65 6e 74 73 20 2a 70 69 4c 65 66 74  crements *piLeft
38c16 20 62 79 20 74 68 65 20 74 6f 74 61 6c 20 6e 75   by the total nu
38c17 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 0a 2a  mber of tokens.*
38c18 2a 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73  * in the express
38c19 69 6f 6e 20 74 72 65 65 20 68 65 61 64 65 64 20  ion tree headed 
38c1a 62 79 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a 2a 20  by pExpr..**.** 
38c1b 52 65 74 75 72 6e 20 31 20 69 66 20 61 6e 79 20  Return 1 if any 
38c1c 74 72 69 6d 6d 69 6e 67 20 6f 63 63 75 72 73 2e  trimming occurs.
38c1d 20 20 52 65 74 75 72 6e 20 30 20 69 66 20 6e 6f    Return 0 if no
38c1e 20 74 72 69 6d 6d 69 6e 67 20 69 73 20 72 65 71   trimming is req
38c1f 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  uired..*/.static
38c20 20 69 6e 74 20 74 72 69 6d 53 6e 69 70 70 65 74   int trimSnippet
38c21 4f 66 66 73 65 74 73 28 0a 20 20 46 74 73 33 45  Offsets(.  Fts3E
38c22 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20  xpr *pExpr,     
38c23 20 2f 2a 20 54 68 65 20 73 65 61 72 63 68 20 65   /* The search e
38c24 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53  xpression */.  S
38c25 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65 74  nippet *pSnippet
38c26 2c 20 20 20 20 2f 2a 20 54 68 65 20 73 65 74 20  ,    /* The set 
38c27 6f 66 20 73 6e 69 70 70 65 74 20 6f 66 66 73 65  of snippet offse
38c28 74 73 20 74 6f 20 62 65 20 74 72 69 6d 6d 65 64  ts to be trimmed
38c29 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 4c 65 66   */.  int *piLef
38c2a 74 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  t           /* I
38c2b 6e 64 65 78 20 6f 66 20 6c 65 66 74 2d 6d 6f 73  ndex of left-mos
38c2c 74 20 74 6f 6b 65 6e 20 69 6e 20 70 45 78 70 72  t token in pExpr
38c2d 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 45 78   */.){.  if( pEx
38c2e 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 74 72  pr ){.    if( tr
38c2f 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73  imSnippetOffsets
38c30 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70  (pExpr->pLeft, p
38c31 53 6e 69 70 70 65 74 2c 20 70 69 4c 65 66 74 29  Snippet, piLeft)
38c32 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
38c33 20 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73   1;.    }..    s
38c34 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65 54  witch( pExpr->eT
38c35 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73  ype ){.      cas
38c36 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  e FTSQUERY_PHRAS
38c37 45 3a 0a 20 20 20 20 20 20 20 20 2a 70 69 4c 65  E:.        *piLe
38c38 66 74 20 2b 3d 20 70 45 78 70 72 2d 3e 70 50 68  ft += pExpr->pPh
38c39 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20  rase->nToken;.  
38c3a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
38c3b 20 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59     case FTSQUERY
38c3c 5f 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20 20  _NEAR: {.       
38c3d 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 68 61   /* The right-ha
38c3e 6e 64 2d 73 69 64 65 20 6f 66 20 61 20 4e 45 41  nd-side of a NEA
38c3f 52 20 6f 70 65 72 61 74 6f 72 20 69 73 20 61 6c  R operator is al
38c40 77 61 79 73 20 61 20 70 68 72 61 73 65 2e 20 54  ways a phrase. T
38c41 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c 65  he.        ** le
38c42 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 69 73 20  ft-hand-side is 
38c43 65 69 74 68 65 72 20 61 20 70 68 72 61 73 65 20  either a phrase 
38c44 6f 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  or an expression
38c45 20 74 72 65 65 20 74 68 61 74 20 69 73 20 0a 20   tree that is . 
38c46 20 20 20 20 20 20 20 2a 2a 20 69 74 73 65 6c 66         ** itself
38c47 20 68 65 61 64 65 64 20 62 79 20 61 20 4e 45 41   headed by a NEA
38c48 52 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 65 20  R operator. The 
38c49 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 69 74 69 61  following initia
38c4a 6c 69 7a 61 74 69 6f 6e 73 0a 20 20 20 20 20 20  lizations.      
38c4b 20 20 2a 2a 20 73 65 74 20 6c 6f 63 61 6c 20 76    ** set local v
38c4c 61 72 69 61 62 6c 65 20 69 4c 65 66 74 20 74 6f  ariable iLeft to
38c4d 20 74 68 65 20 74 6f 6b 65 6e 20 6e 75 6d 62 65   the token numbe
38c4e 72 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f  r of the left-mo
38c4f 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f  st.        ** to
38c50 6b 65 6e 20 69 6e 20 74 68 65 20 72 69 67 68 74  ken in the right
38c51 2d 68 61 6e 64 20 70 68 72 61 73 65 2c 20 61 6e  -hand phrase, an
38c52 64 20 69 52 69 67 68 74 20 74 6f 20 74 68 65 20  d iRight to the 
38c53 72 69 67 68 74 20 6d 6f 73 74 0a 20 20 20 20 20  right most.     
38c54 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74     ** token in t
38c55 68 65 20 73 61 6d 65 20 70 68 72 61 73 65 2e 20  he same phrase. 
38c56 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
38c57 77 65 20 68 61 64 3a 0a 20 20 20 20 20 20 20 20  we had:.        
38c58 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  **.        **   
38c59 20 20 3c 63 6f 6c 3e 20 4d 41 54 43 48 20 27 22    <col> MATCH '"
38c5a 61 62 63 20 64 65 66 22 20 4e 45 41 52 2f 32 20  abc def" NEAR/2 
38c5b 22 67 68 69 20 6a 6b 6c 22 27 0a 20 20 20 20 20  "ghi jkl"'.     
38c5c 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
38c5d 20 74 68 65 6e 20 69 4c 65 66 74 20 77 69 6c 6c   then iLeft will
38c5e 20 62 65 20 73 65 74 20 74 6f 20 32 20 28 74 6f   be set to 2 (to
38c5f 6b 65 6e 20 6e 75 6d 62 65 72 20 6f 66 20 67 68  ken number of gh
38c60 69 29 20 61 6e 64 20 6e 54 6f 6b 65 6e 20 77 69  i) and nToken wi
38c61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 65  ll.        ** be
38c62 20 73 65 74 20 74 6f 20 34 2e 0a 20 20 20 20 20   set to 4..     
38c63 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 74     */.        Ft
38c64 73 33 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20  s3Expr *pLeft = 
38c65 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20  pExpr->pLeft;.  
38c66 20 20 20 20 20 20 46 74 73 33 45 78 70 72 20 2a        Fts3Expr *
38c67 70 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e  pRight = pExpr->
38c68 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
38c69 69 6e 74 20 69 4c 65 66 74 20 3d 20 2a 70 69 4c  int iLeft = *piL
38c6a 65 66 74 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  eft;.        int
38c6b 20 6e 4e 65 61 72 20 3d 20 70 45 78 70 72 2d 3e   nNear = pExpr->
38c6c 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20 20 20 69  nNear;.        i
38c6d 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 70 52 69 67  nt nToken = pRig
38c6e 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  ht->pPhrase->nTo
38c6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  ken;.        int
38c70 20 6a 6a 2c 20 69 69 3b 0a 20 20 20 20 20 20 20   jj, ii;.       
38c71 20 69 66 28 20 70 4c 65 66 74 2d 3e 65 54 79 70   if( pLeft->eTyp
38c72 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
38c73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4c   ){.          pL
38c74 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 52 69  eft = pLeft->pRi
38c75 67 68 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ght;.        }. 
38c76 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
38c77 52 69 67 68 74 2d 3e 65 54 79 70 65 3d 3d 46 54  Right->eType==FT
38c78 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 3b  SQUERY_PHRASE );
38c79 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
38c7a 20 70 4c 65 66 74 2d 3e 65 54 79 70 65 3d 3d 46   pLeft->eType==F
38c7b 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29  TSQUERY_PHRASE )
38c7c 3b 0a 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e  ;.        nToken
38c7d 20 2b 3d 20 70 4c 65 66 74 2d 3e 70 50 68 72 61   += pLeft->pPhra
38c7e 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a 0a 20 20 20  se->nToken;..   
38c7f 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69       for(ii=0; i
38c80 69 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61 74  i<pSnippet->nMat
38c81 63 68 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  ch; ii++){.     
38c82 20 20 20 20 20 73 74 72 75 63 74 20 73 6e 69 70       struct snip
38c83 70 65 74 4d 61 74 63 68 20 2a 70 20 3d 20 26 70  petMatch *p = &p
38c84 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b  Snippet->aMatch[
38c85 69 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ii];.          i
38c86 66 28 20 70 2d 3e 69 54 65 72 6d 3d 3d 69 4c 65  f( p->iTerm==iLe
38c87 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
38c88 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20 30 3b 0a    int isOk = 0;.
38c89 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
38c8a 6e 69 70 70 65 74 20 69 69 20 69 73 20 61 6e 20  nippet ii is an 
38c8b 6f 63 63 75 72 65 6e 63 65 20 6f 66 20 71 75 65  occurence of que
38c8c 72 79 20 74 65 72 6d 20 69 4c 65 66 74 20 69 6e  ry term iLeft in
38c8d 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e 0a 20   the document.. 
38c8e 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 74             ** It
38c8f 20 6f 63 63 75 72 73 20 61 74 20 70 6f 73 69 74   occurs at posit
38c90 69 6f 6e 20 28 70 2d 3e 69 54 6f 6b 65 6e 29 20  ion (p->iToken) 
38c91 6f 66 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 2e  of the document.
38c92 20 57 65 20 6e 6f 77 0a 20 20 20 20 20 20 20 20   We now.        
38c93 20 20 20 20 2a 2a 20 73 65 61 72 63 68 20 66 6f      ** search fo
38c94 72 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  r an instance of
38c95 20 74 6f 6b 65 6e 20 28 69 4c 65 66 74 2d 31 29   token (iLeft-1)
38c96 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20 74 68   somewhere in th
38c97 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  e .            *
38c98 2a 20 72 61 6e 67 65 20 28 70 2d 3e 69 54 6f 6b  * range (p->iTok
38c99 65 6e 20 2d 20 6e 4e 65 61 72 29 2e 2e 2e 28 70  en - nNear)...(p
38c9a 2d 3e 69 54 6f 6b 65 6e 20 2b 20 6e 4e 65 61 72  ->iToken + nNear
38c9b 20 2b 20 6e 54 6f 6b 65 6e 29 20 77 69 74 68 69   + nToken) withi
38c9c 6e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  n .            *
38c9d 2a 20 74 68 65 20 73 65 74 20 6f 66 20 73 6e 69  * the set of sni
38c9e 70 70 65 74 4d 61 74 63 68 20 73 74 72 75 63 74  ppetMatch struct
38c9f 75 72 65 73 2e 20 49 66 20 6f 6e 65 20 69 73 20  ures. If one is 
38ca0 66 6f 75 6e 64 2c 20 70 72 6f 63 65 65 64 2e 20  found, proceed. 
38ca1 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
38ca2 49 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65  If one cannot be
38ca3 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 72 65 6d   found, then rem
38ca4 6f 76 65 20 73 6e 69 70 70 65 74 73 20 69 69 2e  ove snippets ii.
38ca5 2e 28 69 69 2b 4e 2d 31 29 20 0a 20 20 20 20 20  .(ii+N-1) .     
38ca6 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74         ** from t
38ca7 68 65 20 6d 61 74 63 68 69 6e 67 20 73 6e 69 70  he matching snip
38ca8 70 65 74 73 2c 20 77 68 65 72 65 20 4e 20 69 73  pets, where N is
38ca9 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
38caa 6f 6b 65 6e 73 20 0a 20 20 20 20 20 20 20 20 20  okens .         
38cab 20 20 20 2a 2a 20 69 6e 20 70 68 72 61 73 65 20     ** in phrase 
38cac 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65 2e  pRight->pPhrase.
38cad 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  .            */.
38cae 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 28              for(
38caf 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30 20 26 26  jj=0; isOk==0 &&
38cb0 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d 3e 6e 4d   jj<pSnippet->nM
38cb1 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20  atch; jj++){.   
38cb2 20 20 20 20 20 20 20 20 20 20 20 73 74 72 75 63             struc
38cb3 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a  t snippetMatch *
38cb4 70 32 20 3d 20 26 70 53 6e 69 70 70 65 74 2d 3e  p2 = &pSnippet->
38cb5 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20 20 20 20  aMatch[jj];.    
38cb6 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 32            if( p2
38cb7 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65 66 74 2d  ->iTerm==(iLeft-
38cb8 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  1) ){.          
38cb9 20 20 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54        if( p2->iT
38cba 6f 6b 65 6e 3e 3d 28 70 2d 3e 69 54 6f 6b 65 6e  oken>=(p->iToken
38cbb 2d 6e 4e 65 61 72 2d 31 29 20 0a 20 20 20 20 20  -nNear-1) .     
38cbc 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70              && p
38cbd 32 2d 3e 69 54 6f 6b 65 6e 3c 28 70 2d 3e 69 54  2->iToken<(p->iT
38cbe 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 6e 54 6f 6b 65  oken+nNear+nToke
38cbf 6e 29 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  n) .            
38cc0 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20      ){.         
38cc1 20 20 20 20 20 20 20 20 20 69 73 4f 6b 20 3d 20           isOk = 
38cc2 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1;.             
38cc3 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
38cc4 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
38cc5 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69   }.            i
38cc6 66 28 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20 20  f( !isOk ){.    
38cc7 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 6b            int kk
38cc8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
38cc9 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 52 69  for(kk=0; kk<pRi
38cca 67 68 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54  ght->pPhrase->nT
38ccb 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20  oken; kk++){.   
38ccc 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 6e               pSn
38ccd 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6b 6b  ippet->aMatch[kk
38cce 2b 69 69 5d 2e 69 54 65 72 6d 20 3d 20 2d 32 3b  +ii].iTerm = -2;
38ccf 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d  .              }
38cd0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72  .              r
38cd1 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20  eturn 1;.       
38cd2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
38cd3 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 28   }.          if(
38cd4 20 70 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c 65 66   p->iTerm==(iLef
38cd5 74 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  t-1) ){.        
38cd6 20 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d 20 30      int isOk = 0
38cd7 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f  ;.            fo
38cd8 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d 30 20  r(jj=0; isOk==0 
38cd9 26 26 20 6a 6a 3c 70 53 6e 69 70 70 65 74 2d 3e  && jj<pSnippet->
38cda 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b 0a 20  nMatch; jj++){. 
38cdb 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72               str
38cdc 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68  uct snippetMatch
38cdd 20 2a 70 32 20 3d 20 26 70 53 6e 69 70 70 65 74   *p2 = &pSnippet
38cde 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a 20 20  ->aMatch[jj];.  
38cdf 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
38ce0 70 32 2d 3e 69 54 65 72 6d 3d 3d 69 4c 65 66 74  p2->iTerm==iLeft
38ce1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
38ce2 20 20 20 20 69 66 28 20 70 32 2d 3e 69 54 6f 6b      if( p2->iTok
38ce3 65 6e 3c 3d 28 70 2d 3e 69 54 6f 6b 65 6e 2b 6e  en<=(p->iToken+n
38ce4 4e 65 61 72 2b 31 29 20 0a 20 20 20 20 20 20 20  Near+1) .       
38ce5 20 20 20 20 20 20 20 20 20 20 26 26 20 70 32 2d            && p2-
38ce6 3e 69 54 6f 6b 65 6e 3e 28 70 2d 3e 69 54 6f 6b  >iToken>(p->iTok
38ce7 65 6e 2d 6e 4e 65 61 72 2d 6e 54 6f 6b 65 6e 29  en-nNear-nToken)
38ce8 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
38ce9 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20    ){.           
38cea 20 20 20 20 20 20 20 69 73 4f 6b 20 3d 20 31 3b         isOk = 1;
38ceb 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
38cec 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
38ced 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d   }.            }
38cee 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
38cef 20 21 69 73 4f 6b 20 29 7b 0a 20 20 20 20 20 20   !isOk ){.      
38cf0 20 20 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b 0a          int kk;.
38cf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6f                fo
38cf2 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70 4c 65 66 74  r(kk=0; kk<pLeft
38cf3 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65  ->pPhrase->nToke
38cf4 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20 20 20 20 20  n; kk++){.      
38cf5 20 20 20 20 20 20 20 20 20 20 70 53 6e 69 70 70            pSnipp
38cf6 65 74 2d 3e 61 4d 61 74 63 68 5b 69 69 2d 6b 6b  et->aMatch[ii-kk
38cf7 5d 2e 69 54 65 72 6d 20 3d 20 2d 32 3b 0a 20 20  ].iTerm = -2;.  
38cf8 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
38cf9 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
38cfa 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20  rn 1;.          
38cfb 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
38cfc 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
38cfd 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
38cfe 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
38cff 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65  trimSnippetOffse
38d00 74 73 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74  ts(pExpr->pRight
38d01 2c 20 70 53 6e 69 70 70 65 74 2c 20 70 69 4c 65  , pSnippet, piLe
38d02 66 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ft) ){.      ret
38d03 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
38d04 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
38d05 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 6c  /*.** Compute al
38d06 6c 20 6f 66 66 73 65 74 73 20 66 6f 72 20 74 68  l offsets for th
38d07 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
38d08 20 74 68 65 20 71 75 65 72 79 2e 20 20 0a 2a 2a   the query.  .**
38d09 20 49 66 20 74 68 65 20 6f 66 66 73 65 74 73 20   If the offsets 
38d0a 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
38d0b 6e 20 63 6f 6d 70 75 74 65 64 2c 20 74 68 69 73  n computed, this
38d0c 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
38d0d 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -op..*/.static i
38d0e 6e 74 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66  nt snippetAllOff
38d0f 73 65 74 73 28 46 74 73 33 43 75 72 73 6f 72 20  sets(Fts3Cursor 
38d10 2a 70 43 73 72 2c 20 53 6e 69 70 70 65 74 20 2a  *pCsr, Snippet *
38d11 2a 70 70 53 6e 69 70 70 65 74 29 7b 0a 20 20 46  *ppSnippet){.  F
38d12 74 73 33 54 61 62 6c 65 20 2a 70 20 3d 20 28 46  ts3Table *p = (F
38d13 74 73 33 54 61 62 6c 65 20 2a 29 70 43 73 72 2d  ts3Table *)pCsr-
38d14 3e 62 61 73 65 2e 70 56 74 61 62 3b 20 20 2f 2a  >base.pVtab;  /*
38d15 20 54 68 65 20 46 54 53 33 20 76 69 72 74 75 61   The FTS3 virtua
38d16 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  l table */.  int
38d17 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   nColumn;       
38d18 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
38d19 20 63 6f 6c 75 6d 6e 73 2e 20 20 44 6f 63 69 64   columns.  Docid
38d1a 20 64 6f 65 73 20 63 6f 75 6e 74 20 2a 2f 0a 20   does count */. 
38d1b 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   int iColumn;   
38d1c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
38d1d 20 6f 66 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20   of of a column 
38d1e 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
38d1f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
38d20 6f 6f 70 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  oop index */.  i
38d21 6e 74 20 69 46 69 72 73 74 3b 20 20 20 20 20 20  nt iFirst;      
38d22 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 63        /* First c
38d23 6f 6c 75 6d 6e 20 74 6f 20 73 65 61 72 63 68 20  olumn to search 
38d24 2a 2f 0a 20 20 69 6e 74 20 69 4c 61 73 74 3b 20  */.  int iLast; 
38d25 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
38d26 61 73 74 20 63 6f 75 6d 6e 20 74 6f 20 73 65 61  ast coumn to sea
38d27 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20 69 54 65  rch */.  int iTe
38d28 72 6d 20 3d 20 30 3b 0a 20 20 53 6e 69 70 70 65  rm = 0;.  Snippe
38d29 74 20 2a 70 53 6e 69 70 70 65 74 3b 0a 20 20 69  t *pSnippet;.  i
38d2a 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
38d2b 4b 3b 0a 0a 20 20 69 66 28 20 70 43 73 72 2d 3e  K;..  if( pCsr->
38d2c 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20  pExpr==0 ){.    
38d2d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
38d2e 3b 0a 20 20 7d 0a 0a 20 20 70 53 6e 69 70 70 65  ;.  }..  pSnippe
38d2f 74 20 3d 20 28 53 6e 69 70 70 65 74 20 2a 29 73  t = (Snippet *)s
38d30 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
38d31 7a 65 6f 66 28 53 6e 69 70 70 65 74 29 29 3b 0a  zeof(Snippet));.
38d32 20 20 2a 70 70 53 6e 69 70 70 65 74 20 3d 20 70    *ppSnippet = p
38d33 53 6e 69 70 70 65 74 3b 0a 20 20 69 66 28 20 21  Snippet;.  if( !
38d34 70 53 6e 69 70 70 65 74 20 29 7b 0a 20 20 20 20  pSnippet ){.    
38d35 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
38d36 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  MEM;.  }.  memse
38d37 74 28 70 53 6e 69 70 70 65 74 2c 20 30 2c 20 73  t(pSnippet, 0, s
38d38 69 7a 65 6f 66 28 53 6e 69 70 70 65 74 29 29 3b  izeof(Snippet));
38d39 0a 0a 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 2d  ..  nColumn = p-
38d3a 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 43 6f 6c  >nColumn;.  iCol
38d3b 75 6d 6e 20 3d 20 28 70 43 73 72 2d 3e 65 53 65  umn = (pCsr->eSe
38d3c 61 72 63 68 20 2d 20 32 29 3b 0a 20 20 69 66 28  arch - 2);.  if(
38d3d 20 69 43 6f 6c 75 6d 6e 3c 30 20 7c 7c 20 69 43   iColumn<0 || iC
38d3e 6f 6c 75 6d 6e 3e 3d 6e 43 6f 6c 75 6d 6e 20 29  olumn>=nColumn )
38d3f 7b 0a 20 20 20 20 2f 2a 20 4c 6f 6f 6b 20 66 6f  {.    /* Look fo
38d40 72 20 6d 61 74 63 68 65 73 20 6f 76 65 72 20 61  r matches over a
38d41 6c 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68  ll columns of th
38d42 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65  e full-text inde
38d43 78 20 2a 2f 0a 20 20 20 20 69 46 69 72 73 74 20  x */.    iFirst 
38d44 3d 20 30 3b 0a 20 20 20 20 69 4c 61 73 74 20 3d  = 0;.    iLast =
38d45 20 6e 43 6f 6c 75 6d 6e 2d 31 3b 0a 20 20 7d 65   nColumn-1;.  }e
38d46 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4c 6f 6f 6b  lse{.    /* Look
38d47 20 66 6f 72 20 6d 61 74 63 68 65 73 20 69 6e 20   for matches in 
38d48 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74 68 20 63  the iColumn-th c
38d49 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64  olumn of the ind
38d4a 65 78 20 6f 6e 6c 79 20 2a 2f 0a 20 20 20 20 69  ex only */.    i
38d4b 46 69 72 73 74 20 3d 20 69 43 6f 6c 75 6d 6e 3b  First = iColumn;
38d4c 0a 20 20 20 20 69 4c 61 73 74 20 3d 20 69 43 6f  .    iLast = iCo
38d4d 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  lumn;.  }.  for(
38d4e 69 3d 69 46 69 72 73 74 3b 20 72 63 3d 3d 53 51  i=iFirst; rc==SQ
38d4f 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 3d 69 4c  LITE_OK && i<=iL
38d50 61 73 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  ast; i++){.    c
38d51 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b  onst char *zDoc;
38d52 0a 20 20 20 20 69 6e 74 20 6e 44 6f 63 3b 0a 20  .    int nDoc;. 
38d53 20 20 20 7a 44 6f 63 20 3d 20 28 63 6f 6e 73 74     zDoc = (const
38d54 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63   char*)sqlite3_c
38d55 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 43 73 72 2d  olumn_text(pCsr-
38d56 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b 0a 20 20  >pStmt, i+1);.  
38d57 20 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74 65 33    nDoc = sqlite3
38d58 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43  _column_bytes(pC
38d59 73 72 2d 3e 70 53 74 6d 74 2c 20 69 2b 31 29 3b  sr->pStmt, i+1);
38d5a 0a 20 20 20 20 69 66 28 20 7a 44 6f 63 3d 3d 30  .    if( zDoc==0
38d5b 20 26 26 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   && sqlite3_colu
38d5c 6d 6e 5f 74 79 70 65 28 70 43 73 72 2d 3e 70 53  mn_type(pCsr->pS
38d5d 74 6d 74 2c 20 69 2b 31 29 21 3d 53 51 4c 49 54  tmt, i+1)!=SQLIT
38d5e 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  E_NULL ){.      
38d5f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
38d60 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  M;.    }else{.  
38d61 20 20 20 20 72 63 20 3d 20 73 6e 69 70 70 65 74      rc = snippet
38d62 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28  OffsetsOfColumn(
38d63 70 43 73 72 2c 20 70 53 6e 69 70 70 65 74 2c 20  pCsr, pSnippet, 
38d64 69 2c 20 7a 44 6f 63 2c 20 6e 44 6f 63 29 3b 0a  i, zDoc, nDoc);.
38d65 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 77 68 69      }.  }..  whi
38d66 6c 65 28 20 74 72 69 6d 53 6e 69 70 70 65 74 4f  le( trimSnippetO
38d67 66 66 73 65 74 73 28 70 43 73 72 2d 3e 70 45 78  ffsets(pCsr->pEx
38d68 70 72 2c 20 70 53 6e 69 70 70 65 74 2c 20 26 69  pr, pSnippet, &i
38d69 54 65 72 6d 29 20 29 7b 0a 20 20 20 20 69 54 65  Term) ){.    iTe
38d6a 72 6d 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72  rm = 0;.  }..  r
38d6b 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
38d6c 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 69  ** Convert the i
38d6d 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68  nformation in th
38d6e 65 20 61 4d 61 74 63 68 5b 5d 20 61 72 72 61 79  e aMatch[] array
38d6f 20 6f 66 20 74 68 65 20 73 6e 69 70 70 65 74 0a   of the snippet.
38d70 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 74 72 69  ** into the stri
38d71 6e 67 20 7a 4f 66 66 73 65 74 5b 30 2e 2e 6e 4f  ng zOffset[0..nO
38d72 66 66 73 65 74 2d 31 5d 2e 20 54 68 69 73 20 73  ffset-1]. This s
38d73 74 72 69 6e 67 20 69 73 20 75 73 65 64 20 61 73  tring is used as
38d74 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 20 6f  .** the return o
38d75 66 20 74 68 65 20 53 51 4c 20 6f 66 66 73 65 74  f the SQL offset
38d76 73 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  s() function..*/
38d77 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69  .static void sni
38d78 70 70 65 74 4f 66 66 73 65 74 54 65 78 74 28 53  ppetOffsetText(S
38d79 6e 69 70 70 65 74 20 2a 70 29 7b 0a 20 20 69 6e  nippet *p){.  in
38d7a 74 20 69 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d  t i;.  int cnt =
38d7b 20 30 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66   0;.  StringBuff
38d7c 65 72 20 73 62 3b 0a 20 20 63 68 61 72 20 7a 42  er sb;.  char zB
38d7d 75 66 5b 32 30 30 5d 3b 0a 20 20 69 66 28 20 70  uf[200];.  if( p
38d7e 2d 3e 7a 4f 66 66 73 65 74 20 29 20 72 65 74 75  ->zOffset ) retu
38d7f 72 6e 3b 0a 20 20 66 74 73 33 53 6e 69 70 70 65  rn;.  fts3Snippe
38d80 74 53 62 49 6e 69 74 28 26 73 62 29 3b 0a 20 20  tSbInit(&sb);.  
38d81 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d  for(i=0; i<p->nM
38d82 61 74 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  atch; i++){.    
38d83 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61  struct snippetMa
38d84 74 63 68 20 2a 70 4d 61 74 63 68 20 3d 20 26 70  tch *pMatch = &p
38d85 2d 3e 61 4d 61 74 63 68 5b 69 5d 3b 0a 20 20 20  ->aMatch[i];.   
38d86 20 69 66 28 20 70 4d 61 74 63 68 2d 3e 69 54 65   if( pMatch->iTe
38d87 72 6d 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f  rm>=0 ){.      /
38d88 2a 20 49 66 20 73 6e 69 70 70 65 74 4d 61 74 63  * If snippetMatc
38d89 68 2e 69 54 65 72 6d 20 69 73 20 6c 65 73 73 20  h.iTerm is less 
38d8a 74 68 61 6e 20 30 2c 20 74 68 65 6e 20 74 68 65  than 0, then the
38d8b 20 6d 61 74 63 68 20 77 61 73 20 0a 20 20 20 20   match was .    
38d8c 20 20 2a 2a 20 64 69 73 63 61 72 64 65 64 20 61    ** discarded a
38d8d 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73  s part of proces
38d8e 73 69 6e 67 20 74 68 65 20 4e 45 41 52 20 6f 70  sing the NEAR op
38d8f 65 72 61 74 6f 72 20 28 73 65 65 20 74 68 65 20  erator (see the 
38d90 0a 20 20 20 20 20 20 2a 2a 20 74 72 69 6d 53 6e  .      ** trimSn
38d91 69 70 70 65 74 4f 66 66 73 65 74 73 46 6f 72 4e  ippetOffsetsForN
38d92 65 61 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ear() function f
38d93 6f 72 20 64 65 74 61 69 6c 73 29 2e 20 49 67 6e  or details). Ign
38d94 6f 72 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 74  ore .      ** it
38d95 20 69 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20   in this case.  
38d96 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 7a 42 75      */.      zBu
38d97 66 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20  f[0] = ' ';.    
38d98 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
38d99 74 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d  tf(sizeof(zBuf)-
38d9a 31 2c 20 26 7a 42 75 66 5b 63 6e 74 3e 30 5d 2c  1, &zBuf[cnt>0],
38d9b 20 22 25 64 20 25 64 20 25 64 20 25 64 22 2c 0a   "%d %d %d %d",.
38d9c 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63 68            pMatch
38d9d 2d 3e 69 43 6f 6c 2c 20 70 4d 61 74 63 68 2d 3e  ->iCol, pMatch->
38d9e 69 54 65 72 6d 2c 20 70 4d 61 74 63 68 2d 3e 69  iTerm, pMatch->i
38d9f 53 74 61 72 74 2c 20 70 4d 61 74 63 68 2d 3e 6e  Start, pMatch->n
38da0 42 79 74 65 29 3b 0a 20 20 20 20 20 20 66 74 73  Byte);.      fts
38da1 33 53 6e 69 70 70 65 74 41 70 70 65 6e 64 28 26  3SnippetAppend(&
38da2 73 62 2c 20 7a 42 75 66 2c 20 2d 31 29 3b 0a 20  sb, zBuf, -1);. 
38da3 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20       cnt++;.    
38da4 7d 0a 20 20 7d 0a 20 20 70 2d 3e 7a 4f 66 66 73  }.  }.  p->zOffs
38da5 65 74 20 3d 20 73 62 2e 7a 3b 0a 20 20 70 2d 3e  et = sb.z;.  p->
38da6 6e 4f 66 66 73 65 74 20 3d 20 73 62 2e 7a 20 3f  nOffset = sb.z ?
38da7 20 73 62 2e 6e 55 73 65 64 20 3a 20 30 3b 0a 7d   sb.nUsed : 0;.}
38da8 0a 0a 2f 2a 0a 2a 2a 20 7a 44 6f 63 5b 30 2e 2e  ../*.** zDoc[0..
38da9 6e 44 6f 63 2d 31 5d 20 69 73 20 70 68 72 61 73  nDoc-1] is phras
38daa 65 20 6f 66 20 74 65 78 74 2e 20 20 61 4d 61 74  e of text.  aMat
38dab 63 68 5b 30 2e 2e 6e 4d 61 74 63 68 2d 31 5d 20  ch[0..nMatch-1] 
38dac 61 72 65 20 61 20 73 65 74 0a 2a 2a 20 6f 66 20  are a set.** of 
38dad 6d 61 74 63 68 69 6e 67 20 77 6f 72 64 73 20 73  matching words s
38dae 6f 6d 65 20 6f 66 20 77 68 69 63 68 20 6d 69 67  ome of which mig
38daf 68 74 20 62 65 20 69 6e 20 7a 44 6f 63 2e 20 20  ht be in zDoc.  
38db0 7a 44 6f 63 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a  zDoc is column.*
38db1 2a 20 6e 75 6d 62 65 72 20 69 43 6f 6c 2e 0a 2a  * number iCol..*
38db2 2a 0a 2a 2a 20 69 42 72 65 61 6b 20 69 73 20 73  *.** iBreak is s
38db3 75 67 67 65 73 74 65 64 20 73 70 6f 74 20 69 6e  uggested spot in
38db4 20 7a 44 6f 63 20 77 68 65 72 65 20 77 65 20 63   zDoc where we c
38db5 6f 75 6c 64 20 62 65 67 69 6e 20 6f 72 20 65 6e  ould begin or en
38db6 64 20 61 6e 0a 2a 2a 20 65 78 63 65 72 70 74 2e  d an.** excerpt.
38db7 20 20 52 65 74 75 72 6e 20 61 20 76 61 6c 75 65    Return a value
38db8 20 73 69 6d 69 6c 61 72 20 74 6f 20 69 42 72 65   similar to iBre
38db9 61 6b 20 62 75 74 20 70 6f 73 73 69 62 6c 79 20  ak but possibly 
38dba 61 64 6a 75 73 74 65 64 0a 2a 2a 20 74 6f 20 62  adjusted.** to b
38dbb 65 20 61 20 6c 69 74 74 6c 65 20 6c 65 66 74 20  e a little left 
38dbc 6f 72 20 72 69 67 68 74 20 73 6f 20 74 68 61 74  or right so that
38dbd 20 74 68 65 20 62 72 65 61 6b 20 70 6f 69 6e 74   the break point
38dbe 20 69 73 20 62 65 74 74 65 72 2e 0a 2a 2f 0a 73   is better..*/.s
38dbf 74 61 74 69 63 20 69 6e 74 20 77 6f 72 64 42 6f  tatic int wordBo
38dc0 75 6e 64 61 72 79 28 0a 20 20 69 6e 74 20 69 42  undary(.  int iB
38dc1 72 65 61 6b 2c 20 20 20 20 20 20 20 20 20 20 20  reak,           
38dc2 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
38dc3 75 67 67 65 73 74 65 64 20 62 72 65 61 6b 20 70  uggested break p
38dc4 6f 69 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  oint */.  const 
38dc5 63 68 61 72 20 2a 7a 44 6f 63 2c 20 20 20 20 20  char *zDoc,     
38dc6 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 63 75 6d          /* Docum
38dc7 65 6e 74 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  ent text */.  in
38dc8 74 20 6e 44 6f 63 2c 20 20 20 20 20 20 20 20 20  t nDoc,         
38dc9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
38dca 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
38dcb 6e 20 7a 44 6f 63 5b 5d 20 2a 2f 0a 20 20 73 74  n zDoc[] */.  st
38dcc 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63  ruct snippetMatc
38dcd 68 20 2a 61 4d 61 74 63 68 2c 20 20 2f 2a 20 4d  h *aMatch,  /* M
38dce 61 74 63 68 69 6e 67 20 77 6f 72 64 73 20 2a 2f  atching words */
38dcf 0a 20 20 69 6e 74 20 6e 4d 61 74 63 68 2c 20 20  .  int nMatch,  
38dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38dd1 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
38dd2 74 72 69 65 73 20 69 6e 20 61 4d 61 74 63 68 5b  tries in aMatch[
38dd3 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 20  ] */.  int iCol 
38dd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38dd5 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75       /* The colu
38dd6 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20 7a 44  mn number for zD
38dd7 6f 63 5b 5d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  oc[] */.){.  int
38dd8 20 69 3b 0a 20 20 69 66 28 20 69 42 72 65 61 6b   i;.  if( iBreak
38dd9 3c 3d 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75  <=10 ){.    retu
38dda 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
38ddb 69 42 72 65 61 6b 3e 3d 6e 44 6f 63 2d 31 30 20  iBreak>=nDoc-10 
38ddc 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 44  ){.    return nD
38ddd 6f 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  oc;.  }.  for(i=
38dde 30 3b 20 41 4c 57 41 59 53 28 69 3c 6e 4d 61 74  0; ALWAYS(i<nMat
38ddf 63 68 29 20 26 26 20 61 4d 61 74 63 68 5b 69 5d  ch) && aMatch[i]
38de0 2e 69 43 6f 6c 3c 69 43 6f 6c 3b 20 69 2b 2b 29  .iCol<iCol; i++)
38de1 7b 7d 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e 4d  {}.  while( i<nM
38de2 61 74 63 68 20 26 26 20 61 4d 61 74 63 68 5b 69  atch && aMatch[i
38de3 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74 63 68 5b  ].iStart+aMatch[
38de4 69 5d 2e 6e 42 79 74 65 3c 69 42 72 65 61 6b 20  i].nByte<iBreak 
38de5 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 69 66 28 20  ){ i++; }.  if( 
38de6 69 3c 6e 4d 61 74 63 68 20 29 7b 0a 20 20 20 20  i<nMatch ){.    
38de7 69 66 28 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53  if( aMatch[i].iS
38de8 74 61 72 74 3c 69 42 72 65 61 6b 2b 31 30 20 29  tart<iBreak+10 )
38de9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 61  {.      return a
38dea 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72 74 3b  Match[i].iStart;
38deb 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
38dec 3e 30 20 26 26 20 61 4d 61 74 63 68 5b 69 2d 31  >0 && aMatch[i-1
38ded 5d 2e 69 53 74 61 72 74 2b 61 4d 61 74 63 68 5b  ].iStart+aMatch[
38dee 69 2d 31 5d 2e 6e 42 79 74 65 3e 3d 69 42 72 65  i-1].nByte>=iBre
38def 61 6b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  ak ){.      retu
38df0 72 6e 20 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 69  rn aMatch[i-1].i
38df1 53 74 61 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  Start;.    }.  }
38df2 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 31  .  for(i=1; i<=1
38df3 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  0; i++){.    if(
38df4 20 66 74 73 33 73 6e 69 70 70 65 74 49 73 73 70   fts3snippetIssp
38df5 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61 6b 2d  ace(zDoc[iBreak-
38df6 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  i]) ){.      ret
38df7 75 72 6e 20 69 42 72 65 61 6b 20 2d 20 69 20 2b  urn iBreak - i +
38df8 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
38df9 28 20 66 74 73 33 73 6e 69 70 70 65 74 49 73 73  ( fts3snippetIss
38dfa 70 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61 6b  pace(zDoc[iBreak
38dfb 2b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72 65  +i]) ){.      re
38dfc 74 75 72 6e 20 69 42 72 65 61 6b 20 2b 20 69 20  turn iBreak + i 
38dfd 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  + 1;.    }.  }. 
38dfe 20 72 65 74 75 72 6e 20 69 42 72 65 61 6b 3b 0a   return iBreak;.
38dff 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77  }..../*.** Allow
38e00 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 53 6e  ed values for Sn
38e01 69 70 70 65 74 2e 61 4d 61 74 63 68 5b 5d 2e 73  ippet.aMatch[].s
38e02 6e 53 74 61 74 75 73 0a 2a 2f 0a 23 64 65 66 69  nStatus.*/.#defi
38e03 6e 65 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f 52  ne SNIPPET_IGNOR
38e04 45 20 20 30 20 20 20 2f 2a 20 49 74 20 69 73 20  E  0   /* It is 
38e05 6f 6b 20 74 6f 20 6f 6d 69 74 20 74 68 69 73 20  ok to omit this 
38e06 6d 61 74 63 68 20 66 72 6f 6d 20 74 68 65 20 73  match from the s
38e07 6e 69 70 70 65 74 20 2a 2f 0a 23 64 65 66 69 6e  nippet */.#defin
38e08 65 20 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45  e SNIPPET_DESIRE
38e09 44 20 31 20 20 20 2f 2a 20 57 65 20 77 61 6e 74  D 1   /* We want
38e0a 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 69 73   to include this
38e0b 20 6d 61 74 63 68 20 69 6e 20 74 68 65 20 73 6e   match in the sn
38e0c 69 70 70 65 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ippet */../*.** 
38e0d 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 78  Generate the tex
38e0e 74 20 6f 66 20 61 20 73 6e 69 70 70 65 74 2e 0a  t of a snippet..
38e0f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
38e10 6e 69 70 70 65 74 54 65 78 74 28 0a 20 20 46 74  nippetText(.  Ft
38e11 73 33 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  s3Cursor *pCurso
38e12 72 2c 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73  r,   /* The curs
38e13 6f 72 20 77 65 20 6e 65 65 64 20 74 68 65 20 73  or we need the s
38e14 6e 69 70 70 65 74 20 66 6f 72 20 2a 2f 0a 20 20  nippet for */.  
38e15 53 6e 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65  Snippet *pSnippe
38e16 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  t,.  const char 
38e17 2a 7a 53 74 61 72 74 4d 61 72 6b 2c 20 20 20 20  *zStartMark,    
38e18 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f 20 61 70   /* Markup to ap
38e19 70 65 61 72 20 62 65 66 6f 72 65 20 65 61 63 68  pear before each
38e1a 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f 6e 73   match */.  cons
38e1b 74 20 63 68 61 72 20 2a 7a 45 6e 64 4d 61 72 6b  t char *zEndMark
38e1c 2c 20 20 20 20 20 20 20 2f 2a 20 4d 61 72 6b 75  ,       /* Marku
38e1d 70 20 74 6f 20 61 70 70 65 61 72 20 61 66 74 65  p to appear afte
38e1e 72 20 65 61 63 68 20 6d 61 74 63 68 20 2a 2f 0a  r each match */.
38e1f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
38e20 6c 6c 69 70 73 69 73 20 20 20 20 20 20 20 2f 2a  llipsis       /*
38e21 20 45 6c 6c 69 70 73 69 73 20 6d 61 72 6b 20 2a   Ellipsis mark *
38e22 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  /.){.  int i, j;
38e23 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65  .  struct snippe
38e24 74 4d 61 74 63 68 20 2a 61 4d 61 74 63 68 3b 0a  tMatch *aMatch;.
38e25 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 0a 20 20    int nMatch;.  
38e26 69 6e 74 20 6e 44 65 73 69 72 65 64 3b 0a 20 20  int nDesired;.  
38e27 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b  StringBuffer sb;
38e28 0a 20 20 69 6e 74 20 74 61 69 6c 43 6f 6c 3b 0a  .  int tailCol;.
38e29 20 20 69 6e 74 20 74 61 69 6c 4f 66 66 73 65 74    int tailOffset
38e2a 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20  ;.  int iCol;.  
38e2b 69 6e 74 20 6e 44 6f 63 3b 0a 20 20 63 6f 6e 73  int nDoc;.  cons
38e2c 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a 20 20  t char *zDoc;.  
38e2d 69 6e 74 20 69 53 74 61 72 74 2c 20 69 45 6e 64  int iStart, iEnd
38e2e 3b 0a 20 20 69 6e 74 20 74 61 69 6c 45 6c 6c 69  ;.  int tailElli
38e2f 70 73 69 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20  psis = 0;.  int 
38e30 69 4d 61 74 63 68 3b 0a 20 20 0a 0a 20 20 73 71  iMatch;.  ..  sq
38e31 6c 69 74 65 33 5f 66 72 65 65 28 70 53 6e 69 70  lite3_free(pSnip
38e32 70 65 74 2d 3e 7a 53 6e 69 70 70 65 74 29 3b 0a  pet->zSnippet);.
38e33 20 20 70 53 6e 69 70 70 65 74 2d 3e 7a 53 6e 69    pSnippet->zSni
38e34 70 70 65 74 20 3d 20 30 3b 0a 20 20 61 4d 61 74  ppet = 0;.  aMat
38e35 63 68 20 3d 20 70 53 6e 69 70 70 65 74 2d 3e 61  ch = pSnippet->a
38e36 4d 61 74 63 68 3b 0a 20 20 6e 4d 61 74 63 68 20  Match;.  nMatch 
38e37 3d 20 70 53 6e 69 70 70 65 74 2d 3e 6e 4d 61 74  = pSnippet->nMat
38e38 63 68 3b 0a 20 20 66 74 73 33 53 6e 69 70 70 65  ch;.  fts3Snippe
38e39 74 53 62 49 6e 69 74 28 26 73 62 29 3b 0a 0a 20  tSbInit(&sb);.. 
38e3a 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74   for(i=0; i<nMat
38e3b 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 4d  ch; i++){.    aM
38e3c 61 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74 75 73  atch[i].snStatus
38e3d 20 3d 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f 52   = SNIPPET_IGNOR
38e3e 45 3b 0a 20 20 7d 0a 20 20 6e 44 65 73 69 72 65  E;.  }.  nDesire
38e3f 64 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  d = 0;.  for(i=0
38e40 3b 20 69 3c 46 54 53 33 5f 52 4f 54 4f 52 5f 53  ; i<FTS3_ROTOR_S
38e41 5a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72  Z; i++){.    for
38e42 28 6a 3d 30 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20  (j=0; j<nMatch; 
38e43 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
38e44 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72 6d 3d  aMatch[j].iTerm=
38e45 3d 69 20 29 7b 0a 20 20 20 20 20 20 20 20 61 4d  =i ){.        aM
38e46 61 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75 73  atch[j].snStatus
38e47 20 3d 20 53 4e 49 50 50 45 54 5f 44 45 53 49 52   = SNIPPET_DESIR
38e48 45 44 3b 0a 20 20 20 20 20 20 20 20 6e 44 65 73  ED;.        nDes
38e49 69 72 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20  ired++;.        
38e4a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
38e4b 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 4d 61 74     }.  }..  iMat
38e4c 63 68 20 3d 20 30 3b 0a 20 20 74 61 69 6c 43 6f  ch = 0;.  tailCo
38e4d 6c 20 3d 20 2d 31 3b 0a 20 20 74 61 69 6c 4f 66  l = -1;.  tailOf
38e4e 66 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28  fset = 0;.  for(
38e4f 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 20 26 26  i=0; i<nMatch &&
38e50 20 6e 44 65 73 69 72 65 64 3e 30 3b 20 69 2b 2b   nDesired>0; i++
38e51 29 7b 0a 20 20 20 20 69 66 28 20 61 4d 61 74 63  ){.    if( aMatc
38e52 68 5b 69 5d 2e 73 6e 53 74 61 74 75 73 21 3d 53  h[i].snStatus!=S
38e53 4e 49 50 50 45 54 5f 44 45 53 49 52 45 44 20 29  NIPPET_DESIRED )
38e54 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 6e   continue;.    n
38e55 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20 69  Desired--;.    i
38e56 43 6f 6c 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e  Col = aMatch[i].
38e57 69 43 6f 6c 3b 0a 20 20 20 20 7a 44 6f 63 20 3d  iCol;.    zDoc =
38e58 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
38e59 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
38e5a 74 28 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d 74  t(pCursor->pStmt
38e5b 2c 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 6e  , iCol+1);.    n
38e5c 44 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  Doc = sqlite3_co
38e5d 6c 75 6d 6e 5f 62 79 74 65 73 28 70 43 75 72 73  lumn_bytes(pCurs
38e5e 6f 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c 2b  or->pStmt, iCol+
38e5f 31 29 3b 0a 20 20 20 20 69 53 74 61 72 74 20 3d  1);.    iStart =
38e60 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72   aMatch[i].iStar
38e61 74 20 2d 20 34 30 3b 0a 20 20 20 20 69 53 74 61  t - 40;.    iSta
38e62 72 74 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61 72  rt = wordBoundar
38e63 79 28 69 53 74 61 72 74 2c 20 7a 44 6f 63 2c 20  y(iStart, zDoc, 
38e64 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d  nDoc, aMatch, nM
38e65 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20  atch, iCol);.   
38e66 20 69 66 28 20 69 53 74 61 72 74 3c 3d 31 30 20   if( iStart<=10 
38e67 29 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74 20  ){.      iStart 
38e68 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 0;.    }.    i
38e69 66 28 20 69 43 6f 6c 3d 3d 74 61 69 6c 43 6f 6c  f( iCol==tailCol
38e6a 20 26 26 20 69 53 74 61 72 74 3c 3d 74 61 69 6c   && iStart<=tail
38e6b 4f 66 66 73 65 74 2b 32 30 20 29 7b 0a 20 20 20  Offset+20 ){.   
38e6c 20 20 20 69 53 74 61 72 74 20 3d 20 74 61 69 6c     iStart = tail
38e6d 4f 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20  Offset;.    }.  
38e6e 20 20 69 66 28 20 28 69 43 6f 6c 21 3d 74 61 69    if( (iCol!=tai
38e6f 6c 43 6f 6c 20 26 26 20 74 61 69 6c 43 6f 6c 3e  lCol && tailCol>
38e70 3d 30 29 20 7c 7c 20 69 53 74 61 72 74 21 3d 74  =0) || iStart!=t
38e71 61 69 6c 4f 66 66 73 65 74 20 29 7b 0a 20 20 20  ailOffset ){.   
38e72 20 20 20 66 74 73 33 53 6e 69 70 70 65 74 54 72     fts3SnippetTr
38e73 69 6d 57 68 69 74 65 53 70 61 63 65 28 26 73 62  imWhiteSpace(&sb
38e74 29 3b 0a 20 20 20 20 20 20 66 74 73 33 53 6e 69  );.      fts3Sni
38e75 70 70 65 74 41 70 70 65 6e 64 57 68 69 74 65 53  ppetAppendWhiteS
38e76 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20 20  pace(&sb);.     
38e77 20 66 74 73 33 53 6e 69 70 70 65 74 41 70 70 65   fts3SnippetAppe
38e78 6e 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73 69  nd(&sb, zEllipsi
38e79 73 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 66 74  s, -1);.      ft
38e7a 73 33 53 6e 69 70 70 65 74 41 70 70 65 6e 64 57  s3SnippetAppendW
38e7b 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a  hiteSpace(&sb);.
38e7c 20 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 20 3d      }.    iEnd =
38e7d 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72   aMatch[i].iStar
38e7e 74 20 2b 20 61 4d 61 74 63 68 5b 69 5d 2e 6e 42  t + aMatch[i].nB
38e7f 79 74 65 20 2b 20 34 30 3b 0a 20 20 20 20 69 45  yte + 40;.    iE
38e80 6e 64 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61 72  nd = wordBoundar
38e81 79 28 69 45 6e 64 2c 20 7a 44 6f 63 2c 20 6e 44  y(iEnd, zDoc, nD
38e82 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d 61 74  oc, aMatch, nMat
38e83 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20 20 69  ch, iCol);.    i
38e84 66 28 20 69 45 6e 64 3e 3d 6e 44 6f 63 2d 31 30  f( iEnd>=nDoc-10
38e85 20 29 7b 0a 20 20 20 20 20 20 69 45 6e 64 20 3d   ){.      iEnd =
38e86 20 6e 44 6f 63 3b 0a 20 20 20 20 20 20 74 61 69   nDoc;.      tai
38e87 6c 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b 0a 20  lEllipsis = 0;. 
38e88 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
38e89 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 31  tailEllipsis = 1
38e8a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c  ;.    }.    whil
38e8b 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68  e( iMatch<nMatch
38e8c 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63   && aMatch[iMatc
38e8d 68 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 20 29 7b 20  h].iCol<iCol ){ 
38e8e 69 4d 61 74 63 68 2b 2b 3b 20 7d 0a 20 20 20 20  iMatch++; }.    
38e8f 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69 45  while( iStart<iE
38e90 6e 64 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c  nd ){.      whil
38e91 65 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68  e( iMatch<nMatch
38e92 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63   && aMatch[iMatc
38e93 68 5d 2e 69 53 74 61 72 74 3c 69 53 74 61 72 74  h].iStart<iStart
38e94 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26  .             &&
38e95 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e   aMatch[iMatch].
38e96 69 43 6f 6c 3c 3d 69 43 6f 6c 20 29 7b 0a 20 20  iCol<=iCol ){.  
38e97 20 20 20 20 20 20 69 4d 61 74 63 68 2b 2b 3b 0a        iMatch++;.
38e98 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
38e99 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68 20  ( iMatch<nMatch 
38e9a 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  && aMatch[iMatch
38e9b 5d 2e 69 53 74 61 72 74 3c 69 45 6e 64 0a 20 20  ].iStart<iEnd.  
38e9c 20 20 20 20 20 20 20 20 20 20 20 26 26 20 61 4d             && aM
38e9d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 43 6f  atch[iMatch].iCo
38e9e 6c 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20  l==iCol ){.     
38e9f 20 20 20 66 74 73 33 53 6e 69 70 70 65 74 41 70     fts3SnippetAp
38ea0 70 65 6e 64 28 26 73 62 2c 20 26 7a 44 6f 63 5b  pend(&sb, &zDoc[
38ea1 69 53 74 61 72 74 5d 2c 20 61 4d 61 74 63 68 5b  iStart], aMatch[
38ea2 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 20 2d  iMatch].iStart -
38ea3 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20 20   iStart);.      
38ea4 20 20 69 53 74 61 72 74 20 3d 20 61 4d 61 74 63    iStart = aMatc
38ea5 68 5b 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74  h[iMatch].iStart
38ea6 3b 0a 20 20 20 20 20 20 20 20 66 74 73 33 53 6e  ;.        fts3Sn
38ea7 69 70 70 65 74 41 70 70 65 6e 64 28 26 73 62 2c  ippetAppend(&sb,
38ea8 20 7a 53 74 61 72 74 4d 61 72 6b 2c 20 2d 31 29   zStartMark, -1)
38ea9 3b 0a 20 20 20 20 20 20 20 20 66 74 73 33 53 6e  ;.        fts3Sn
38eaa 69 70 70 65 74 41 70 70 65 6e 64 28 26 73 62 2c  ippetAppend(&sb,
38eab 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20   &zDoc[iStart], 
38eac 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 6e  aMatch[iMatch].n
38ead 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 66  Byte);.        f
38eae 74 73 33 53 6e 69 70 70 65 74 41 70 70 65 6e 64  ts3SnippetAppend
38eaf 28 26 73 62 2c 20 7a 45 6e 64 4d 61 72 6b 2c 20  (&sb, zEndMark, 
38eb0 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74  -1);.        iSt
38eb1 61 72 74 20 2b 3d 20 61 4d 61 74 63 68 5b 69 4d  art += aMatch[iM
38eb2 61 74 63 68 5d 2e 6e 42 79 74 65 3b 0a 20 20 20  atch].nByte;.   
38eb3 20 20 20 20 20 66 6f 72 28 6a 3d 69 4d 61 74 63       for(j=iMatc
38eb4 68 2b 31 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a  h+1; j<nMatch; j
38eb5 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
38eb6 66 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65  f( aMatch[j].iTe
38eb7 72 6d 3d 3d 61 4d 61 74 63 68 5b 69 4d 61 74 63  rm==aMatch[iMatc
38eb8 68 5d 2e 69 54 65 72 6d 0a 20 20 20 20 20 20 20  h].iTerm.       
38eb9 20 20 20 20 20 20 20 26 26 20 61 4d 61 74 63 68         && aMatch
38eba 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 3d 3d 53 4e  [j].snStatus==SN
38ebb 49 50 50 45 54 5f 44 45 53 49 52 45 44 20 29 7b  IPPET_DESIRED ){
38ebc 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 44 65  .            nDe
38ebd 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20 20 20 20  sired--;.       
38ebe 20 20 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73       aMatch[j].s
38ebf 6e 53 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45  nStatus = SNIPPE
38ec0 54 5f 49 47 4e 4f 52 45 3b 0a 20 20 20 20 20 20  T_IGNORE;.      
38ec1 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
38ec2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
38ec3 20 20 20 20 20 66 74 73 33 53 6e 69 70 70 65 74       fts3Snippet
38ec4 41 70 70 65 6e 64 28 26 73 62 2c 20 26 7a 44 6f  Append(&sb, &zDo
38ec5 63 5b 69 53 74 61 72 74 5d 2c 20 69 45 6e 64 20  c[iStart], iEnd 
38ec6 2d 20 69 53 74 61 72 74 29 3b 0a 20 20 20 20 20  - iStart);.     
38ec7 20 20 20 69 53 74 61 72 74 20 3d 20 69 45 6e 64     iStart = iEnd
38ec8 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
38ec9 20 20 20 20 74 61 69 6c 43 6f 6c 20 3d 20 69 43      tailCol = iC
38eca 6f 6c 3b 0a 20 20 20 20 74 61 69 6c 4f 66 66 73  ol;.    tailOffs
38ecb 65 74 20 3d 20 69 45 6e 64 3b 0a 20 20 7d 0a 20  et = iEnd;.  }. 
38ecc 20 66 74 73 33 53 6e 69 70 70 65 74 54 72 69 6d   fts3SnippetTrim
38ecd 57 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b  WhiteSpace(&sb);
38ece 0a 20 20 69 66 28 20 74 61 69 6c 45 6c 6c 69 70  .  if( tailEllip
38ecf 73 69 73 20 29 7b 0a 20 20 20 20 66 74 73 33 53  sis ){.    fts3S
38ed0 6e 69 70 70 65 74 41 70 70 65 6e 64 57 68 69 74  nippetAppendWhit
38ed1 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20  eSpace(&sb);.   
38ed2 20 66 74 73 33 53 6e 69 70 70 65 74 41 70 70 65   fts3SnippetAppe
38ed3 6e 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73 69  nd(&sb, zEllipsi
38ed4 73 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20 70 53  s, -1);.  }.  pS
38ed5 6e 69 70 70 65 74 2d 3e 7a 53 6e 69 70 70 65 74  nippet->zSnippet
38ed6 20 3d 20 73 62 2e 7a 3b 0a 20 20 70 53 6e 69 70   = sb.z;.  pSnip
38ed7 70 65 74 2d 3e 6e 53 6e 69 70 70 65 74 20 3d 20  pet->nSnippet = 
38ed8 73 62 2e 7a 20 3f 20 73 62 2e 6e 55 73 65 64 20  sb.z ? sb.nUsed 
38ed9 3a 20 30 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50  : 0;.}..SQLITE_P
38eda 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
38edb 74 65 33 46 74 73 33 4f 66 66 73 65 74 73 28 0a  te3Fts3Offsets(.
38edc 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
38edd 74 20 2a 70 43 74 78 2c 20 20 20 20 20 20 20 20  t *pCtx,        
38ede 20 20 2f 2a 20 53 51 4c 69 74 65 20 66 75 6e 63    /* SQLite func
38edf 74 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78  tion call contex
38ee0 74 20 2a 2f 0a 20 20 46 74 73 33 43 75 72 73 6f  t */.  Fts3Curso
38ee1 72 20 2a 70 43 73 72 20 20 20 20 20 20 20 20 20  r *pCsr         
38ee2 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
38ee3 20 6f 62 6a 65 63 74 20 2a 2f 0a 29 7b 0a 20 20   object */.){.  
38ee4 53 6e 69 70 70 65 74 20 2a 70 3b 20 20 20 20 20  Snippet *p;     
38ee5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38ee6 2f 2a 20 53 6e 69 70 70 65 74 20 73 74 72 75 63  /* Snippet struc
38ee7 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ture */.  int rc
38ee8 20 3d 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66   = snippetAllOff
38ee9 73 65 74 73 28 70 43 73 72 2c 20 26 70 29 3b 0a  sets(pCsr, &p);.
38eea 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
38eeb 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 6e 69 70 70  _OK ){.    snipp
38eec 65 74 4f 66 66 73 65 74 54 65 78 74 28 70 29 3b  etOffsetText(p);
38eed 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 4f 66 66  .    if( p->zOff
38eee 73 65 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  set ){.      sql
38eef 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
38ef0 28 70 43 74 78 2c 20 70 2d 3e 7a 4f 66 66 73 65  (pCtx, p->zOffse
38ef1 74 2c 20 70 2d 3e 6e 4f 66 66 73 65 74 2c 20 53  t, p->nOffset, S
38ef2 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
38ef3 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
38ef4 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
38ef5 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 70 43  t_error_nomem(pC
38ef6 74 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  tx);.    }.  }el
38ef7 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
38ef8 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
38ef9 65 6d 28 70 43 74 78 29 3b 0a 20 20 7d 0a 20 20  em(pCtx);.  }.  
38efa 66 74 73 33 53 6e 69 70 70 65 74 46 72 65 65 28  fts3SnippetFree(
38efb 70 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  p);.}..SQLITE_PR
38efc 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
38efd 65 33 46 74 73 33 53 6e 69 70 70 65 74 28 0a 20  e3Fts3Snippet(. 
38efe 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
38eff 20 2a 70 43 74 78 2c 20 20 20 20 20 20 20 20 20   *pCtx,         
38f00 20 2f 2a 20 53 51 4c 69 74 65 20 66 75 6e 63 74   /* SQLite funct
38f01 69 6f 6e 20 63 61 6c 6c 20 63 6f 6e 74 65 78 74  ion call context
38f02 20 2a 2f 0a 20 20 46 74 73 33 43 75 72 73 6f 72   */.  Fts3Cursor
38f03 20 2a 70 43 73 72 2c 20 20 20 20 20 20 20 20 20   *pCsr,         
38f04 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
38f05 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73  object */.  cons
38f06 74 20 63 68 61 72 20 2a 7a 53 74 61 72 74 2c 20  t char *zStart, 
38f07 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
38f08 6e 69 70 70 65 74 20 73 74 61 72 74 20 74 65 78  nippet start tex
38f09 74 20 2d 20 22 3c 62 3e 22 20 2a 2f 0a 20 20 63  t - "<b>" */.  c
38f0a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6e 64 2c  onst char *zEnd,
38f0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38f0c 2a 20 53 6e 69 70 70 65 74 20 65 6e 64 20 74 65  * Snippet end te
38f0d 78 74 20 2d 20 22 3c 2f 62 3e 22 20 2a 2f 0a 20  xt - "</b>" */. 
38f0e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 6c   const char *zEl
38f0f 6c 69 70 73 69 73 20 20 20 20 20 20 20 20 20 20  lipsis          
38f10 20 2f 2a 20 53 6e 69 70 70 65 74 20 65 6c 6c 69   /* Snippet elli
38f11 70 73 69 73 20 74 65 78 74 20 2d 20 22 3c 62 3e  psis text - "<b>
38f12 2e 2e 2e 3c 2f 62 3e 22 20 2a 2f 0a 29 7b 0a 20  ...</b>" */.){. 
38f13 20 53 6e 69 70 70 65 74 20 2a 70 3b 20 20 20 20   Snippet *p;    
38f14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38f15 20 2f 2a 20 53 6e 69 70 70 65 74 20 73 74 72 75   /* Snippet stru
38f16 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72  cture */.  int r
38f17 63 20 3d 20 73 6e 69 70 70 65 74 41 6c 6c 4f 66  c = snippetAllOf
38f18 66 73 65 74 73 28 70 43 73 72 2c 20 26 70 29 3b  fsets(pCsr, &p);
38f19 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
38f1a 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 6e 69 70  E_OK ){.    snip
38f1b 70 65 74 54 65 78 74 28 70 43 73 72 2c 20 70 2c  petText(pCsr, p,
38f1c 20 7a 53 74 61 72 74 2c 20 7a 45 6e 64 2c 20 7a   zStart, zEnd, z
38f1d 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20 20 20 69  Ellipsis);.    i
38f1e 66 28 20 70 2d 3e 7a 53 6e 69 70 70 65 74 20 29  f( p->zSnippet )
38f1f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
38f20 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 74 78  result_text(pCtx
38f21 2c 20 70 2d 3e 7a 53 6e 69 70 70 65 74 2c 20 70  , p->zSnippet, p
38f22 2d 3e 6e 53 6e 69 70 70 65 74 2c 20 53 51 4c 49  ->nSnippet, SQLI
38f23 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
38f24 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
38f25 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
38f26 72 72 6f 72 5f 6e 6f 6d 65 6d 28 70 43 74 78 29  rror_nomem(pCtx)
38f27 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
38f28 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
38f29 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
38f2a 70 43 74 78 29 3b 0a 20 20 7d 0a 20 20 66 74 73  pCtx);.  }.  fts
38f2b 33 53 6e 69 70 70 65 74 46 72 65 65 28 70 29 3b  3SnippetFree(p);
38f2c 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  .}..#endif../***
38f2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
38f2e 6f 66 20 66 74 73 33 5f 73 6e 69 70 70 65 74 2e  of fts3_snippet.
38f2f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
38f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38f31 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
38f32 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
38f33 6e 20 66 69 6c 65 20 72 74 72 65 65 2e 63 20 2a  n file rtree.c *
38f34 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38f35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38f36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
38f37 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
38f38 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
38f39 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
38f3a 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
38f3b 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
38f3c 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
38f3d 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
38f3e 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
38f3f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
38f40 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
38f41 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
38f42 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
38f43 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
38f44 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
38f45 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
38f46 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
38f47 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
38f48 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
38f49 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
38f4a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38f4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38f4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38f4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38f4e 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
38f4f 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 6f  contains code fo
38f50 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  r implementation
38f51 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20  s of the r-tree 
38f52 61 6e 64 20 72 2a 2d 74 72 65 65 0a 2a 2a 20 61  and r*-tree.** a
38f53 6c 67 6f 72 69 74 68 6d 73 20 70 61 63 6b 61 67  lgorithms packag
38f54 65 64 20 61 73 20 61 6e 20 53 51 4c 69 74 65 20  ed as an SQLite 
38f55 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
38f56 64 75 6c 65 2e 0a 2a 2f 0a 0a 23 69 66 20 21 64  dule..*/..#if !d
38f57 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
38f58 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
38f59 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52  QLITE_ENABLE_RTR
38f5a 45 45 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  EE)../*.** This 
38f5b 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e  file contains an
38f5c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
38f5d 6f 66 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64  of a couple of d
38f5e 69 66 66 65 72 65 6e 74 20 76 61 72 69 61 6e 74  ifferent variant
38f5f 73 0a 2a 2a 20 6f 66 20 74 68 65 20 72 2d 74 72  s.** of the r-tr
38f60 65 65 20 61 6c 67 6f 72 69 74 68 6d 2e 20 53 65  ee algorithm. Se
38f61 65 20 74 68 65 20 52 45 41 44 4d 45 20 66 69 6c  e the README fil
38f62 65 20 66 6f 72 20 66 75 72 74 68 65 72 20 64 65  e for further de
38f63 74 61 69 6c 73 2e 20 54 68 65 20 0a 2a 2a 20 73  tails. The .** s
38f64 61 6d 65 20 64 61 74 61 2d 73 74 72 75 63 74 75  ame data-structu
38f65 72 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 61  re is used for a
38f66 6c 6c 2c 20 62 75 74 20 74 68 65 20 61 6c 67 6f  ll, but the algo
38f67 72 69 74 68 6d 73 20 66 6f 72 20 69 6e 73 65 72  rithms for inser
38f68 74 20 61 6e 64 0a 2a 2a 20 64 65 6c 65 74 65 20  t and.** delete 
38f69 6f 70 65 72 61 74 69 6f 6e 73 20 76 61 72 79 2e  operations vary.
38f6a 20 54 68 65 20 76 61 72 69 61 6e 74 73 20 75 73   The variants us
38f6b 65 64 20 61 72 65 20 73 65 6c 65 63 74 65 64 20  ed are selected 
38f6c 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 20  at compile time 
38f6d 0a 2a 2a 20 62 79 20 64 65 66 69 6e 69 6e 67 20  .** by defining 
38f6e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79  the following sy
38f6f 6d 62 6f 6c 73 3a 0a 2a 2f 0a 0a 2f 2a 20 45 69  mbols:.*/../* Ei
38f70 74 68 65 72 2c 20 62 6f 74 68 20 6f 72 20 6e 6f  ther, both or no
38f71 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
38f72 69 6e 67 20 6d 61 79 20 62 65 20 73 65 74 20 74  ing may be set t
38f73 6f 20 61 63 74 69 76 61 74 65 20 0a 2a 2a 20 72  o activate .** r
38f74 2a 74 72 65 65 20 76 61 72 69 61 6e 74 20 61 6c  *tree variant al
38f75 67 6f 72 69 74 68 6d 73 2e 0a 2a 2f 0a 23 64 65  gorithms..*/.#de
38f76 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52 53 54  fine VARIANT_RST
38f77 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53 55 42  ARTREE_CHOOSESUB
38f78 54 52 45 45 20 30 0a 23 64 65 66 69 6e 65 20 56  TREE 0.#define V
38f79 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45  ARIANT_RSTARTREE
38f7a 5f 52 45 49 4e 53 45 52 54 20 20 20 20 20 20 31  _REINSERT      1
38f7b 0a 0a 2f 2a 20 0a 2a 2a 20 45 78 61 63 74 6c 79  ../* .** Exactly
38f7c 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
38f7d 6f 77 69 6e 67 20 6d 75 73 74 20 62 65 20 73 65  owing must be se
38f7e 74 20 74 6f 20 31 2e 0a 2a 2f 0a 23 64 65 66 69  t to 1..*/.#defi
38f7f 6e 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d  ne VARIANT_GUTTM
38f80 41 4e 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c  AN_QUADRATIC_SPL
38f81 49 54 20 30 0a 23 64 65 66 69 6e 65 20 56 41 52  IT 0.#define VAR
38f82 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
38f83 45 41 52 5f 53 50 4c 49 54 20 20 20 20 30 0a 23  EAR_SPLIT    0.#
38f84 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 52  define VARIANT_R
38f85 53 54 41 52 54 52 45 45 5f 53 50 4c 49 54 20 20  STARTREE_SPLIT  
38f86 20 20 20 20 20 20 20 31 0a 0a 23 64 65 66 69 6e         1..#defin
38f87 65 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  e VARIANT_GUTTMA
38f88 4e 5f 53 50 4c 49 54 20 5c 0a 20 20 20 20 20 20  N_SPLIT \.      
38f89 20 20 28 56 41 52 49 41 4e 54 5f 47 55 54 54 4d    (VARIANT_GUTTM
38f8a 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 7c  AN_LINEAR_SPLIT|
38f8b 7c 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e  |VARIANT_GUTTMAN
38f8c 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54  _QUADRATIC_SPLIT
38f8d 29 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  )..#if VARIANT_G
38f8e 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
38f8f 5f 53 50 4c 49 54 0a 20 20 23 64 65 66 69 6e 65  _SPLIT.  #define
38f90 20 50 69 63 6b 4e 65 78 74 20 51 75 61 64 72 61   PickNext Quadra
38f91 74 69 63 50 69 63 6b 4e 65 78 74 0a 20 20 23 64  ticPickNext.  #d
38f92 65 66 69 6e 65 20 50 69 63 6b 53 65 65 64 73 20  efine PickSeeds 
38f93 51 75 61 64 72 61 74 69 63 50 69 63 6b 53 65 65  QuadraticPickSee
38f94 64 73 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73  ds.  #define Ass
38f95 69 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f  ignCells splitNo
38f96 64 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66  deGuttman.#endif
38f97 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55 54  .#if VARIANT_GUT
38f98 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49  TMAN_LINEAR_SPLI
38f99 54 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b  T.  #define Pick
38f9a 4e 65 78 74 20 4c 69 6e 65 61 72 50 69 63 6b 4e  Next LinearPickN
38f9b 65 78 74 0a 20 20 23 64 65 66 69 6e 65 20 50 69  ext.  #define Pi
38f9c 63 6b 53 65 65 64 73 20 4c 69 6e 65 61 72 50 69  ckSeeds LinearPi
38f9d 63 6b 53 65 65 64 73 0a 20 20 23 64 65 66 69 6e  ckSeeds.  #defin
38f9e 65 20 41 73 73 69 67 6e 43 65 6c 6c 73 20 73 70  e AssignCells sp
38f9f 6c 69 74 4e 6f 64 65 47 75 74 74 6d 61 6e 0a 23  litNodeGuttman.#
38fa0 65 6e 64 69 66 0a 23 69 66 20 56 41 52 49 41 4e  endif.#if VARIAN
38fa1 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c 49  T_RSTARTREE_SPLI
38fa2 54 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73 69  T.  #define Assi
38fa3 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f 64  gnCells splitNod
38fa4 65 53 74 61 72 74 72 65 65 0a 23 65 6e 64 69 66  eStartree.#endif
38fa5 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
38fa6 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54 45 5f  E_CORE.  SQLITE_
38fa7 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a  EXTENSION_INIT1.
38fa8 23 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 0a 23  #else.#endif...#
38fa9 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d  ifndef SQLITE_AM
38faa 41 4c 47 41 4d 41 54 49 4f 4e 0a 74 79 70 65 64  ALGAMATION.typed
38fab 65 66 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  ef sqlite3_int64
38fac 20 69 36 34 3b 0a 74 79 70 65 64 65 66 20 75 6e   i64;.typedef un
38fad 73 69 67 6e 65 64 20 63 68 61 72 20 75 38 3b 0a  signed char u8;.
38fae 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
38faf 20 69 6e 74 20 75 33 32 3b 0a 23 65 6e 64 69 66   int u32;.#endif
38fb0 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ..typedef struct
38fb1 20 52 74 72 65 65 20 52 74 72 65 65 3b 0a 74 79   Rtree Rtree;.ty
38fb2 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72  pedef struct Rtr
38fb3 65 65 43 75 72 73 6f 72 20 52 74 72 65 65 43 75  eeCursor RtreeCu
38fb4 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74  rsor;.typedef st
38fb5 72 75 63 74 20 52 74 72 65 65 4e 6f 64 65 20 52  ruct RtreeNode R
38fb6 74 72 65 65 4e 6f 64 65 3b 0a 74 79 70 65 64 65  treeNode;.typede
38fb7 66 20 73 74 72 75 63 74 20 52 74 72 65 65 43 65  f struct RtreeCe
38fb8 6c 6c 20 52 74 72 65 65 43 65 6c 6c 3b 0a 74 79  ll RtreeCell;.ty
38fb9 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74 72  pedef struct Rtr
38fba 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 52 74 72  eeConstraint Rtr
38fbb 65 65 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 74 79  eeConstraint;.ty
38fbc 70 65 64 65 66 20 75 6e 69 6f 6e 20 52 74 72 65  pedef union Rtre
38fbd 65 43 6f 6f 72 64 20 52 74 72 65 65 43 6f 6f 72  eCoord RtreeCoor
38fbe 64 3b 0a 0a 2f 2a 20 54 68 65 20 72 74 72 65 65  d;../* The rtree
38fbf 20 6d 61 79 20 68 61 76 65 20 62 65 74 77 65 65   may have betwee
38fc0 6e 20 31 20 61 6e 64 20 52 54 52 45 45 5f 4d 41  n 1 and RTREE_MA
38fc1 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 20 64 69 6d  X_DIMENSIONS dim
38fc2 65 6e 73 69 6f 6e 73 2e 20 2a 2f 0a 23 64 65 66  ensions. */.#def
38fc3 69 6e 65 20 52 54 52 45 45 5f 4d 41 58 5f 44 49  ine RTREE_MAX_DI
38fc4 4d 45 4e 53 49 4f 4e 53 20 35 0a 0a 2f 2a 20 53  MENSIONS 5../* S
38fc5 69 7a 65 20 6f 66 20 68 61 73 68 20 74 61 62 6c  ize of hash tabl
38fc6 65 20 52 74 72 65 65 2e 61 48 61 73 68 2e 20 54  e Rtree.aHash. T
38fc7 68 69 73 20 68 61 73 68 20 74 61 62 6c 65 20 69  his hash table i
38fc8 73 20 6e 6f 74 20 65 78 70 65 63 74 65 64 20 74  s not expected t
38fc9 6f 0a 2a 2a 20 65 76 65 72 20 63 6f 6e 74 61 69  o.** ever contai
38fca 6e 20 76 65 72 79 20 6d 61 6e 79 20 65 6e 74 72  n very many entr
38fcb 69 65 73 2c 20 73 6f 20 61 20 66 69 78 65 64 20  ies, so a fixed 
38fcc 6e 75 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74  number of bucket
38fcd 73 20 69 73 20 0a 2a 2a 20 75 73 65 64 2e 0a 2a  s is .** used..*
38fce 2f 0a 23 64 65 66 69 6e 65 20 48 41 53 48 53 49  /.#define HASHSI
38fcf 5a 45 20 31 32 38 0a 0a 2f 2a 20 0a 2a 2a 20 41  ZE 128../* .** A
38fd0 6e 20 72 74 72 65 65 20 76 69 72 74 75 61 6c 2d  n rtree virtual-
38fd1 74 61 62 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f  table object..*/
38fd2 0a 73 74 72 75 63 74 20 52 74 72 65 65 20 7b 0a  .struct Rtree {.
38fd3 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 62    sqlite3_vtab b
38fd4 61 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ase;.  sqlite3 *
38fd5 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  db;             
38fd6 20 20 20 2f 2a 20 48 6f 73 74 20 64 61 74 61 62     /* Host datab
38fd7 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
38fd8 2f 0a 20 20 69 6e 74 20 69 4e 6f 64 65 53 69 7a  /.  int iNodeSiz
38fd9 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
38fda 2f 2a 20 53 69 7a 65 20 69 6e 20 62 79 74 65 73  /* Size in bytes
38fdb 20 6f 66 20 65 61 63 68 20 6e 6f 64 65 20 69 6e   of each node in
38fdc 20 74 68 65 20 6e 6f 64 65 20 74 61 62 6c 65 20   the node table 
38fdd 2a 2f 0a 20 20 69 6e 74 20 6e 44 69 6d 3b 20 20  */.  int nDim;  
38fde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38fdf 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 69   /* Number of di
38fe0 6d 65 6e 73 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e  mensions */.  in
38fe1 74 20 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b  t nBytesPerCell;
38fe2 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74            /* Byt
38fe3 65 73 20 63 6f 6e 73 75 6d 65 64 20 70 65 72 20  es consumed per 
38fe4 63 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 44  cell */.  int iD
38fe5 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  epth;           
38fe6 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
38fe7 20 64 65 70 74 68 20 6f 66 20 74 68 65 20 72 2d   depth of the r-
38fe8 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
38fe9 2f 0a 20 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  /.  char *zDb;  
38fea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38feb 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62  /* Name of datab
38fec 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72  ase containing r
38fed 2d 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20  -tree table */. 
38fee 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
38fef 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38ff0 4e 61 6d 65 20 6f 66 20 72 2d 74 72 65 65 20 74  Name of r-tree t
38ff1 61 62 6c 65 20 2a 2f 20 0a 20 20 52 74 72 65 65  able */ .  Rtree
38ff2 4e 6f 64 65 20 2a 61 48 61 73 68 5b 48 41 53 48  Node *aHash[HASH
38ff3 53 49 5a 45 5d 3b 20 2f 2a 20 48 61 73 68 20 74  SIZE]; /* Hash t
38ff4 61 62 6c 65 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72  able of in-memor
38ff5 79 20 6e 6f 64 65 73 2e 20 2a 2f 20 0a 20 20 69  y nodes. */ .  i
38ff6 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20  nt nBusy;       
38ff7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
38ff8 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
38ff9 75 73 65 72 73 20 6f 66 20 74 68 69 73 20 73 74  users of this st
38ffa 72 75 63 74 75 72 65 20 2a 2f 0a 0a 20 20 2f 2a  ructure */..  /*
38ffb 20 4c 69 73 74 20 6f 66 20 6e 6f 64 65 73 20 72   List of nodes r
38ffc 65 6d 6f 76 65 64 20 64 75 72 69 6e 67 20 61 20  emoved during a 
38ffd 43 6f 6e 64 65 6e 73 65 54 72 65 65 20 6f 70 65  CondenseTree ope
38ffe 72 61 74 69 6f 6e 2e 20 4c 69 73 74 20 69 73 0a  ration. List is.
38fff 20 20 2a 2a 20 6c 69 6e 6b 65 64 20 74 6f 67 65    ** linked toge
39000 74 68 65 72 20 76 69 61 20 74 68 65 20 70 6f 69  ther via the poi
39001 6e 74 65 72 20 6e 6f 72 6d 61 6c 6c 79 20 75 73  nter normally us
39002 65 64 20 66 6f 72 20 68 61 73 68 20 63 68 61 69  ed for hash chai
39003 6e 73 20 2d 0a 20 20 2a 2a 20 52 74 72 65 65 4e  ns -.  ** RtreeN
39004 6f 64 65 2e 70 4e 65 78 74 2e 20 52 74 72 65 65  ode.pNext. Rtree
39005 4e 6f 64 65 2e 69 4e 6f 64 65 20 73 74 6f 72 65  Node.iNode store
39006 73 20 74 68 65 20 64 65 70 74 68 20 6f 66 20 74  s the depth of t
39007 68 65 20 73 75 62 2d 74 72 65 65 20 0a 20 20 2a  he sub-tree .  *
39008 2a 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20  * headed by the 
39009 6e 6f 64 65 20 28 6c 65 61 66 20 6e 6f 64 65 73  node (leaf nodes
3900a 20 68 61 76 65 20 52 74 72 65 65 4e 6f 64 65 2e   have RtreeNode.
3900b 69 4e 6f 64 65 3d 3d 30 29 2e 0a 20 20 2a 2f 0a  iNode==0)..  */.
3900c 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 44 65    RtreeNode *pDe
3900d 6c 65 74 65 64 3b 0a 20 20 69 6e 74 20 69 52 65  leted;.  int iRe
3900e 69 6e 73 65 72 74 48 65 69 67 68 74 3b 20 20 20  insertHeight;   
3900f 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20 6f       /* Height o
39010 66 20 73 75 62 2d 74 72 65 65 73 20 52 65 69 6e  f sub-trees Rein
39011 73 65 72 74 28 29 20 68 61 73 20 72 75 6e 20 6f  sert() has run o
39012 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 53 74 61 74 65  n */..  /* State
39013 6d 65 6e 74 73 20 74 6f 20 72 65 61 64 2f 77 72  ments to read/wr
39014 69 74 65 2f 64 65 6c 65 74 65 20 61 20 72 65 63  ite/delete a rec
39015 6f 72 64 20 66 72 6f 6d 20 78 78 78 5f 6e 6f 64  ord from xxx_nod
39016 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  e */.  sqlite3_s
39017 74 6d 74 20 2a 70 52 65 61 64 4e 6f 64 65 3b 0a  tmt *pReadNode;.
39018 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
39019 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20 20 73 71  pWriteNode;.  sq
3901a 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c  lite3_stmt *pDel
3901b 65 74 65 4e 6f 64 65 3b 0a 0a 20 20 2f 2a 20 53  eteNode;..  /* S
3901c 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72 65 61  tatements to rea
3901d 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65 20 61  d/write/delete a
3901e 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78 78 78   record from xxx
3901f 5f 72 6f 77 69 64 20 2a 2f 0a 20 20 73 71 6c 69  _rowid */.  sqli
39020 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 61 64 52  te3_stmt *pReadR
39021 6f 77 69 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f  owid;.  sqlite3_
39022 73 74 6d 74 20 2a 70 57 72 69 74 65 52 6f 77 69  stmt *pWriteRowi
39023 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  d;.  sqlite3_stm
39024 74 20 2a 70 44 65 6c 65 74 65 52 6f 77 69 64 3b  t *pDeleteRowid;
39025 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74  ..  /* Statement
39026 73 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f  s to read/write/
39027 64 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20  delete a record 
39028 66 72 6f 6d 20 78 78 78 5f 70 61 72 65 6e 74 20  from xxx_parent 
39029 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
3902a 74 20 2a 70 52 65 61 64 50 61 72 65 6e 74 3b 0a  t *pReadParent;.
3902b 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
3902c 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20 20  pWriteParent;.  
3902d 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44  sqlite3_stmt *pD
3902e 65 6c 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20 20  eleteParent;..  
3902f 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65 3b 0a  int eCoordType;.
39030 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65 20  };../* Possible 
39031 76 61 6c 75 65 73 20 66 6f 72 20 65 43 6f 6f 72  values for eCoor
39032 64 54 79 70 65 3a 20 2a 2f 0a 23 64 65 66 69 6e  dType: */.#defin
39033 65 20 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45  e RTREE_COORD_RE
39034 41 4c 33 32 20 30 0a 23 64 65 66 69 6e 65 20 52  AL32 0.#define R
39035 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32  TREE_COORD_INT32
39036 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d    1../*.** The m
39037 69 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  inimum number of
39038 20 63 65 6c 6c 73 20 61 6c 6c 6f 77 65 64 20 66   cells allowed f
39039 6f 72 20 61 20 6e 6f 64 65 20 69 73 20 61 20 74  or a node is a t
3903a 68 69 72 64 20 6f 66 20 74 68 65 20 0a 2a 2a 20  hird of the .** 
3903b 6d 61 78 69 6d 75 6d 2e 20 49 6e 20 47 75 74 6d  maximum. In Gutm
3903c 61 6e 27 73 20 6e 6f 74 61 74 69 6f 6e 3a 0a 2a  an's notation:.*
3903d 2a 0a 2a 2a 20 20 20 20 20 6d 20 3d 20 4d 2f 33  *.**     m = M/3
3903e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 52 2a 2d  .**.** If an R*-
3903f 74 72 65 65 20 22 52 65 69 6e 73 65 72 74 22 20  tree "Reinsert" 
39040 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 72 65 71  operation is req
39041 75 69 72 65 64 2c 20 74 68 65 20 73 61 6d 65 20  uired, the same 
39042 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 65 6c  number of.** cel
39043 6c 73 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66  ls are removed f
39044 72 6f 6d 20 74 68 65 20 6f 76 65 72 66 75 6c 6c  rom the overfull
39045 20 6e 6f 64 65 20 61 6e 64 20 72 65 69 6e 73 65   node and reinse
39046 72 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72  rted into the tr
39047 65 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  ee..*/.#define R
39048 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 29  TREE_MINCELLS(p)
39049 20 28 28 28 28 70 29 2d 3e 69 4e 6f 64 65 53 69   ((((p)->iNodeSi
3904a 7a 65 2d 34 29 2f 28 70 29 2d 3e 6e 42 79 74 65  ze-4)/(p)->nByte
3904b 73 50 65 72 43 65 6c 6c 29 2f 33 29 0a 23 64 65  sPerCell)/3).#de
3904c 66 69 6e 65 20 52 54 52 45 45 5f 52 45 49 4e 53  fine RTREE_REINS
3904d 45 52 54 28 70 29 20 52 54 52 45 45 5f 4d 49 4e  ERT(p) RTREE_MIN
3904e 43 45 4c 4c 53 28 70 29 0a 23 64 65 66 69 6e 65  CELLS(p).#define
3904f 20 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 20   RTREE_MAXCELLS 
39050 35 31 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74  51../* .** An rt
39051 72 65 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63  ree cursor objec
39052 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72  t..*/.struct Rtr
39053 65 65 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c  eeCursor {.  sql
39054 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
39055 20 62 61 73 65 3b 0a 20 20 52 74 72 65 65 4e 6f   base;.  RtreeNo
39056 64 65 20 2a 70 4e 6f 64 65 3b 20 20 20 20 20 20  de *pNode;      
39057 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f             /* No
39058 64 65 20 63 75 72 73 6f 72 20 69 73 20 63 75 72  de cursor is cur
39059 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
3905a 61 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c  at */.  int iCel
3905b 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l;              
3905c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
3905d 65 78 20 6f 66 20 63 75 72 72 65 6e 74 20 63 65  ex of current ce
3905e 6c 6c 20 69 6e 20 70 4e 6f 64 65 20 2a 2f 0a 20  ll in pNode */. 
3905f 20 69 6e 74 20 69 53 74 72 61 74 65 67 79 3b 20   int iStrategy; 
39060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39061 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 69 64     /* Copy of id
39062 78 4e 75 6d 20 73 65 61 72 63 68 20 70 61 72 61  xNum search para
39063 6d 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  meter */.  int n
39064 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20  Constraint;     
39065 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39066 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
39067 73 20 69 6e 20 61 43 6f 6e 73 74 72 61 69 6e 74  s in aConstraint
39068 20 2a 2f 0a 20 20 52 74 72 65 65 43 6f 6e 73 74   */.  RtreeConst
39069 72 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69  raint *aConstrai
3906a 6e 74 3b 20 20 20 20 20 2f 2a 20 53 65 61 72 63  nt;     /* Searc
3906b 68 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a  h constraints. *
3906c 2f 0a 7d 3b 0a 0a 75 6e 69 6f 6e 20 52 74 72 65  /.};..union Rtre
3906d 65 43 6f 6f 72 64 20 7b 0a 20 20 66 6c 6f 61 74  eCoord {.  float
3906e 20 66 3b 0a 20 20 69 6e 74 20 69 3b 0a 7d 3b 0a   f;.  int i;.};.
3906f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  ./*.** The argum
39070 65 6e 74 20 69 73 20 61 6e 20 52 74 72 65 65 43  ent is an RtreeC
39071 6f 6f 72 64 2e 20 52 65 74 75 72 6e 20 74 68 65  oord. Return the
39072 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 77 69   value stored wi
39073 74 68 69 6e 20 74 68 65 20 52 74 72 65 65 43 6f  thin the RtreeCo
39074 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74 74 65 64  ord.** formatted
39075 20 61 73 20 61 20 64 6f 75 62 6c 65 2e 20 54 68   as a double. Th
39076 69 73 20 6d 61 63 72 6f 20 61 73 73 75 6d 65 73  is macro assumes
39077 20 74 68 61 74 20 6c 6f 63 61 6c 20 76 61 72 69   that local vari
39078 61 62 6c 65 20 70 52 74 72 65 65 20 70 6f 69 6e  able pRtree poin
39079 74 73 0a 2a 2a 20 74 6f 20 74 68 65 20 52 74 72  ts.** to the Rtr
3907a 65 65 20 73 74 72 75 63 74 75 72 65 20 61 73 73  ee structure ass
3907b 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
3907c 20 52 74 72 65 65 43 6f 6f 72 64 2e 0a 2a 2f 0a   RtreeCoord..*/.
3907d 23 64 65 66 69 6e 65 20 44 43 4f 4f 52 44 28 63  #define DCOORD(c
3907e 6f 6f 72 64 29 20 28 20 20 20 20 20 20 20 20 20  oord) (         
3907f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39080 20 20 5c 0a 20 20 28 70 52 74 72 65 65 2d 3e 65    \.  (pRtree->e
39081 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45 45  CoordType==RTREE
39082 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 29 20 3f  _COORD_REAL32) ?
39083 20 20 20 20 20 20 5c 0a 20 20 20 20 28 28 64 6f        \.    ((do
39084 75 62 6c 65 29 63 6f 6f 72 64 2e 66 29 20 3a 20  uble)coord.f) : 
39085 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39086 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
39087 28 28 64 6f 75 62 6c 65 29 63 6f 6f 72 64 2e 69  ((double)coord.i
39088 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  )               
39089 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
3908a 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 65 61 72 63  )../*.** A searc
3908b 68 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 2a 2f  h constraint..*/
3908c 0a 73 74 72 75 63 74 20 52 74 72 65 65 43 6f 6e  .struct RtreeCon
3908d 73 74 72 61 69 6e 74 20 7b 0a 20 20 69 6e 74 20  straint {.  int 
3908e 69 43 6f 6f 72 64 3b 20 20 20 20 20 20 20 20 20  iCoord;         
3908f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39090 20 49 6e 64 65 78 20 6f 66 20 63 6f 6e 73 74 72   Index of constr
39091 61 69 6e 65 64 20 63 6f 6f 72 64 69 6e 61 74 65  ained coordinate
39092 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20   */.  int op;   
39093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39094 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73 74          /* Const
39095 72 61 69 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f  raining operatio
39096 6e 20 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 72 56  n */.  double rV
39097 61 6c 75 65 3b 20 20 20 20 20 20 20 20 20 20 20  alue;           
39098 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 73           /* Cons
39099 74 72 61 69 6e 74 20 76 61 6c 75 65 2e 20 2a 2f  traint value. */
3909a 0a 7d 3b 0a 0a 2f 2a 20 50 6f 73 73 69 62 6c 65  .};../* Possible
3909b 20 76 61 6c 75 65 73 20 66 6f 72 20 52 74 72 65   values for Rtre
3909c 65 43 6f 6e 73 74 72 61 69 6e 74 2e 6f 70 20 2a  eConstraint.op *
3909d 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  /.#define RTREE_
3909e 45 51 20 30 78 34 31 0a 23 64 65 66 69 6e 65 20  EQ 0x41.#define 
3909f 52 54 52 45 45 5f 4c 45 20 30 78 34 32 0a 23 64  RTREE_LE 0x42.#d
390a0 65 66 69 6e 65 20 52 54 52 45 45 5f 4c 54 20 30  efine RTREE_LT 0
390a1 78 34 33 0a 23 64 65 66 69 6e 65 20 52 54 52 45  x43.#define RTRE
390a2 45 5f 47 45 20 30 78 34 34 0a 23 64 65 66 69 6e  E_GE 0x44.#defin
390a3 65 20 52 54 52 45 45 5f 47 54 20 30 78 34 35 0a  e RTREE_GT 0x45.
390a4 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65  ./* .** An rtree
390a5 20 73 74 72 75 63 74 75 72 65 20 6e 6f 64 65 2e   structure node.
390a6 0a 2a 2a 0a 2a 2a 20 44 61 74 61 20 66 6f 72 6d  .**.** Data form
390a7 61 74 20 28 52 74 72 65 65 4e 6f 64 65 2e 7a 44  at (RtreeNode.zD
390a8 61 74 61 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e  ata):.**.**   1.
390a9 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20   If the node is 
390aa 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 28 6e  the root node (n
390ab 6f 64 65 20 31 29 2c 20 74 68 65 6e 20 74 68 65  ode 1), then the
390ac 20 66 69 72 73 74 20 32 20 62 79 74 65 73 0a 2a   first 2 bytes.*
390ad 2a 20 20 20 20 20 20 6f 66 20 74 68 65 20 6e 6f  *      of the no
390ae 64 65 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74  de contain the t
390af 72 65 65 20 64 65 70 74 68 20 61 73 20 61 20 62  ree depth as a b
390b0 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
390b1 72 2e 0a 2a 2a 20 20 20 20 20 20 46 6f 72 20 6e  r..**      For n
390b2 6f 6e 2d 72 6f 6f 74 20 6e 6f 64 65 73 2c 20 74  on-root nodes, t
390b3 68 65 20 66 69 72 73 74 20 32 20 62 79 74 65 73  he first 2 bytes
390b4 20 61 72 65 20 6c 65 66 74 20 75 6e 75 73 65 64   are left unused
390b5 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65  ..**.**   2. The
390b6 20 6e 65 78 74 20 32 20 62 79 74 65 73 20 63 6f   next 2 bytes co
390b7 6e 74 61 69 6e 20 74 68 65 20 6e 75 6d 62 65 72  ntain the number
390b8 20 6f 66 20 65 6e 74 72 69 65 73 20 63 75 72 72   of entries curr
390b9 65 6e 74 6c 79 20 0a 2a 2a 20 20 20 20 20 20 73  ently .**      s
390ba 74 6f 72 65 64 20 69 6e 20 74 68 65 20 6e 6f 64  tored in the nod
390bb 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 54 68  e..**.**   3. Th
390bc 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20 74  e remainder of t
390bd 68 65 20 6e 6f 64 65 20 63 6f 6e 74 61 69 6e 73  he node contains
390be 20 74 68 65 20 6e 6f 64 65 20 65 6e 74 72 69 65   the node entrie
390bf 73 2e 20 45 61 63 68 20 65 6e 74 72 79 0a 2a 2a  s. Each entry.**
390c0 20 20 20 20 20 20 63 6f 6e 73 69 73 74 73 20 6f        consists o
390c1 66 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 79 74  f a single 8-byt
390c2 65 20 69 6e 74 65 67 65 72 20 66 6f 6c 6c 6f 77  e integer follow
390c3 65 64 20 62 79 20 61 6e 20 65 76 65 6e 20 6e 75  ed by an even nu
390c4 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 6f 66 20  mber.**      of 
390c5 34 2d 62 79 74 65 20 63 6f 6f 72 64 69 6e 61 74  4-byte coordinat
390c6 65 73 2e 20 46 6f 72 20 6c 65 61 66 20 6e 6f 64  es. For leaf nod
390c7 65 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 69  es the integer i
390c8 73 20 74 68 65 20 72 6f 77 69 64 0a 2a 2a 20 20  s the rowid.**  
390c9 20 20 20 20 6f 66 20 61 20 72 65 63 6f 72 64 2e      of a record.
390ca 20 46 6f 72 20 69 6e 74 65 72 6e 61 6c 20 6e 6f   For internal no
390cb 64 65 73 20 69 74 20 69 73 20 74 68 65 20 6e 6f  des it is the no
390cc 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 0a 2a  de number of a.*
390cd 2a 20 20 20 20 20 20 63 68 69 6c 64 20 70 61 67  *      child pag
390ce 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72  e..*/.struct Rtr
390cf 65 65 4e 6f 64 65 20 7b 0a 20 20 52 74 72 65 65  eeNode {.  Rtree
390d0 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b 20 20  Node *pParent;  
390d1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
390d2 50 61 72 65 6e 74 20 6e 6f 64 65 20 2a 2f 0a 20  Parent node */. 
390d3 20 69 36 34 20 69 4e 6f 64 65 3b 0a 20 20 69 6e   i64 iNode;.  in
390d4 74 20 6e 52 65 66 3b 0a 20 20 69 6e 74 20 69 73  t nRef;.  int is
390d5 44 69 72 74 79 3b 0a 20 20 75 38 20 2a 7a 44 61  Dirty;.  u8 *zDa
390d6 74 61 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ta;.  RtreeNode 
390d7 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20  *pNext;         
390d8 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
390d9 6e 6f 64 65 20 69 6e 20 74 68 69 73 20 68 61 73  node in this has
390da 68 20 63 68 61 69 6e 20 2a 2f 0a 7d 3b 0a 23 64  h chain */.};.#d
390db 65 66 69 6e 65 20 4e 43 45 4c 4c 28 70 4e 6f 64  efine NCELL(pNod
390dc 65 29 20 72 65 61 64 49 6e 74 31 36 28 26 28 70  e) readInt16(&(p
390dd 4e 6f 64 65 29 2d 3e 7a 44 61 74 61 5b 32 5d 29  Node)->zData[2])
390de 0a 0a 2f 2a 20 0a 2a 2a 20 53 74 72 75 63 74 75  ../* .** Structu
390df 72 65 20 74 6f 20 73 74 6f 72 65 20 61 20 64 65  re to store a de
390e0 73 65 72 69 61 6c 69 7a 65 64 20 72 74 72 65 65  serialized rtree
390e1 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74 72 75   record..*/.stru
390e2 63 74 20 52 74 72 65 65 43 65 6c 6c 20 7b 0a 20  ct RtreeCell {. 
390e3 20 69 36 34 20 69 52 6f 77 69 64 3b 0a 20 20 52   i64 iRowid;.  R
390e4 74 72 65 65 43 6f 6f 72 64 20 61 43 6f 6f 72 64  treeCoord aCoord
390e5 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e  [RTREE_MAX_DIMEN
390e6 53 49 4f 4e 53 2a 32 5d 3b 0a 7d 3b 0a 0a 23 69  SIONS*2];.};..#i
390e7 66 6e 64 65 66 20 4d 41 58 0a 23 20 64 65 66 69  fndef MAX.# defi
390e8 6e 65 20 4d 41 58 28 78 2c 79 29 20 28 28 78 29  ne MAX(x,y) ((x)
390e9 20 3c 20 28 79 29 20 3f 20 28 79 29 20 3a 20 28   < (y) ? (y) : (
390ea 78 29 29 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64  x)).#endif.#ifnd
390eb 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20  ef MIN.# define 
390ec 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 20 3e 20  MIN(x,y) ((x) > 
390ed 28 79 29 20 3f 20 28 79 29 20 3a 20 28 78 29 29  (y) ? (y) : (x))
390ee 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46  .#endif../*.** F
390ef 75 6e 63 74 69 6f 6e 73 20 74 6f 20 64 65 73 65  unctions to dese
390f0 72 69 61 6c 69 7a 65 20 61 20 31 36 20 62 69 74  rialize a 16 bit
390f1 20 69 6e 74 65 67 65 72 2c 20 33 32 20 62 69 74   integer, 32 bit
390f2 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64   real number and
390f3 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e 74 65 67  .** 64 bit integ
390f4 65 72 2e 20 54 68 65 20 64 65 73 65 72 69 61 6c  er. The deserial
390f5 69 7a 65 64 20 76 61 6c 75 65 20 69 73 20 72 65  ized value is re
390f6 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  turned..*/.stati
390f7 63 20 69 6e 74 20 72 65 61 64 49 6e 74 31 36 28  c int readInt16(
390f8 75 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  u8 *p){.  return
390f9 20 28 70 5b 30 5d 3c 3c 38 29 20 2b 20 70 5b 31   (p[0]<<8) + p[1
390fa 5d 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  ];.}.static void
390fb 20 72 65 61 64 43 6f 6f 72 64 28 75 38 20 2a 70   readCoord(u8 *p
390fc 2c 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43  , RtreeCoord *pC
390fd 6f 6f 72 64 29 7b 0a 20 20 75 33 32 20 69 20 3d  oord){.  u32 i =
390fe 20 28 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b   (.    (((u32)p[
390ff 30 5d 29 20 3c 3c 20 32 34 29 20 2b 20 0a 20 20  0]) << 24) + .  
39100 20 20 28 28 28 75 33 32 29 70 5b 31 5d 29 20 3c    (((u32)p[1]) <
39101 3c 20 31 36 29 20 2b 20 0a 20 20 20 20 28 28 28  < 16) + .    (((
39102 75 33 32 29 70 5b 32 5d 29 20 3c 3c 20 20 38 29  u32)p[2]) <<  8)
39103 20 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70   + .    (((u32)p
39104 5b 33 5d 29 20 3c 3c 20 20 30 29 0a 20 20 29 3b  [3]) <<  0).  );
39105 0a 20 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f 72  .  *(u32 *)pCoor
39106 64 20 3d 20 69 3b 0a 7d 0a 73 74 61 74 69 63 20  d = i;.}.static 
39107 69 36 34 20 72 65 61 64 49 6e 74 36 34 28 75 38  i64 readInt64(u8
39108 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28   *p){.  return (
39109 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 30 5d  .    (((i64)p[0]
3910a 29 20 3c 3c 20 35 36 29 20 2b 20 0a 20 20 20 20  ) << 56) + .    
3910b 28 28 28 69 36 34 29 70 5b 31 5d 29 20 3c 3c 20  (((i64)p[1]) << 
3910c 34 38 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  48) + .    (((i6
3910d 34 29 70 5b 32 5d 29 20 3c 3c 20 34 30 29 20 2b  4)p[2]) << 40) +
3910e 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 33   .    (((i64)p[3
3910f 5d 29 20 3c 3c 20 33 32 29 20 2b 20 0a 20 20 20  ]) << 32) + .   
39110 20 28 28 28 69 36 34 29 70 5b 34 5d 29 20 3c 3c   (((i64)p[4]) <<
39111 20 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28 69   24) + .    (((i
39112 36 34 29 70 5b 35 5d 29 20 3c 3c 20 31 36 29 20  64)p[5]) << 16) 
39113 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b  + .    (((i64)p[
39114 36 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20 20  6]) <<  8) + .  
39115 20 20 28 28 28 69 36 34 29 70 5b 37 5d 29 20 3c    (((i64)p[7]) <
39116 3c 20 20 30 29 0a 20 20 29 3b 0a 7d 0a 0a 2f 2a  <  0).  );.}../*
39117 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 74 6f  .** Functions to
39118 20 73 65 72 69 61 6c 69 7a 65 20 61 20 31 36 20   serialize a 16 
39119 62 69 74 20 69 6e 74 65 67 65 72 2c 20 33 32 20  bit integer, 32 
3911a 62 69 74 20 72 65 61 6c 20 6e 75 6d 62 65 72 20  bit real number 
3911b 61 6e 64 0a 2a 2a 20 36 34 20 62 69 74 20 69 6e  and.** 64 bit in
3911c 74 65 67 65 72 2e 20 54 68 65 20 76 61 6c 75 65  teger. The value
3911d 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
3911e 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
3911f 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 74   written.** to t
39120 68 65 20 61 72 67 75 6d 65 6e 74 20 62 75 66 66  he argument buff
39121 65 72 20 28 61 6c 77 61 79 73 20 32 2c 20 34 20  er (always 2, 4 
39122 61 6e 64 20 38 20 72 65 73 70 65 63 74 69 76 65  and 8 respective
39123 6c 79 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ly)..*/.static i
39124 6e 74 20 77 72 69 74 65 49 6e 74 31 36 28 75 38  nt writeInt16(u8
39125 20 2a 70 2c 20 69 6e 74 20 69 29 7b 0a 20 20 70   *p, int i){.  p
39126 5b 30 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78  [0] = (i>> 8)&0x
39127 46 46 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 69 3e  FF;.  p[1] = (i>
39128 3e 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74  > 0)&0xFF;.  ret
39129 75 72 6e 20 32 3b 0a 7d 0a 73 74 61 74 69 63 20  urn 2;.}.static 
3912a 69 6e 74 20 77 72 69 74 65 43 6f 6f 72 64 28 75  int writeCoord(u
3912b 38 20 2a 70 2c 20 52 74 72 65 65 43 6f 6f 72 64  8 *p, RtreeCoord
3912c 20 2a 70 43 6f 6f 72 64 29 7b 0a 20 20 75 33 32   *pCoord){.  u32
3912d 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69   i;.  assert( si
3912e 7a 65 6f 66 28 52 74 72 65 65 43 6f 6f 72 64 29  zeof(RtreeCoord)
3912f 3d 3d 34 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==4 );.  assert(
39130 20 73 69 7a 65 6f 66 28 75 33 32 29 3d 3d 34 20   sizeof(u32)==4 
39131 29 3b 0a 20 20 69 20 3d 20 2a 28 75 33 32 20 2a  );.  i = *(u32 *
39132 29 70 43 6f 6f 72 64 3b 0a 20 20 70 5b 30 5d 20  )pCoord;.  p[0] 
39133 3d 20 28 69 3e 3e 32 34 29 26 30 78 46 46 3b 0a  = (i>>24)&0xFF;.
39134 20 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 31 36 29    p[1] = (i>>16)
39135 26 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20  &0xFF;.  p[2] = 
39136 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20 20  (i>> 8)&0xFF;.  
39137 70 5b 33 5d 20 3d 20 28 69 3e 3e 20 30 29 26 30  p[3] = (i>> 0)&0
39138 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 34 3b  xFF;.  return 4;
39139 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  .}.static int wr
3913a 69 74 65 49 6e 74 36 34 28 75 38 20 2a 70 2c 20  iteInt64(u8 *p, 
3913b 69 36 34 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d  i64 i){.  p[0] =
3913c 20 28 69 3e 3e 35 36 29 26 30 78 46 46 3b 0a 20   (i>>56)&0xFF;. 
3913d 20 70 5b 31 5d 20 3d 20 28 69 3e 3e 34 38 29 26   p[1] = (i>>48)&
3913e 30 78 46 46 3b 0a 20 20 70 5b 32 5d 20 3d 20 28  0xFF;.  p[2] = (
3913f 69 3e 3e 34 30 29 26 30 78 46 46 3b 0a 20 20 70  i>>40)&0xFF;.  p
39140 5b 33 5d 20 3d 20 28 69 3e 3e 33 32 29 26 30 78  [3] = (i>>32)&0x
39141 46 46 3b 0a 20 20 70 5b 34 5d 20 3d 20 28 69 3e  FF;.  p[4] = (i>
39142 3e 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 35  >24)&0xFF;.  p[5
39143 5d 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46  ] = (i>>16)&0xFF
39144 3b 0a 20 20 70 5b 36 5d 20 3d 20 28 69 3e 3e 20  ;.  p[6] = (i>> 
39145 38 29 26 30 78 46 46 3b 0a 20 20 70 5b 37 5d 20  8)&0xFF;.  p[7] 
39146 3d 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a  = (i>> 0)&0xFF;.
39147 20 20 72 65 74 75 72 6e 20 38 3b 0a 7d 0a 0a 2f    return 8;.}../
39148 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74  *.** Increment t
39149 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
3914a 6e 74 20 6f 66 20 6e 6f 64 65 20 70 2e 0a 2a 2f  nt of node p..*/
3914b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64  .static void nod
3914c 65 52 65 66 65 72 65 6e 63 65 28 52 74 72 65 65  eReference(Rtree
3914d 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66 28 20  Node *p){.  if( 
3914e 70 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 52 65 66  p ){.    p->nRef
3914f 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ++;.  }.}../*.**
39150 20 43 6c 65 61 72 20 74 68 65 20 63 6f 6e 74 65   Clear the conte
39151 6e 74 20 6f 66 20 6e 6f 64 65 20 70 20 28 73 65  nt of node p (se
39152 74 20 61 6c 6c 20 62 79 74 65 73 20 74 6f 20 30  t all bytes to 0
39153 78 30 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  x00)..*/.static 
39154 76 6f 69 64 20 6e 6f 64 65 5a 65 72 6f 28 52 74  void nodeZero(Rt
39155 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
39156 65 65 4e 6f 64 65 20 2a 70 29 7b 0a 20 20 69 66  eeNode *p){.  if
39157 28 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65  ( p ){.    memse
39158 74 28 26 70 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20  t(&p->zData[2], 
39159 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  0, pRtree->iNode
3915a 53 69 7a 65 2d 32 29 3b 0a 20 20 20 20 70 2d 3e  Size-2);.    p->
3915b 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 7d  isDirty = 1;.  }
3915c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
3915d 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20 69 4e  a node number iN
3915e 6f 64 65 2c 20 72 65 74 75 72 6e 20 74 68 65 20  ode, return the 
3915f 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 6b 65  corresponding ke
39160 79 20 74 6f 20 75 73 65 0a 2a 2a 20 69 6e 20 74  y to use.** in t
39161 68 65 20 52 74 72 65 65 2e 61 48 61 73 68 20 74  he Rtree.aHash t
39162 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
39163 69 6e 74 20 6e 6f 64 65 48 61 73 68 28 69 36 34  int nodeHash(i64
39164 20 69 4e 6f 64 65 29 7b 0a 20 20 72 65 74 75 72   iNode){.  retur
39165 6e 20 28 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e  n (.    (iNode>>
39166 35 36 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 38  56) ^ (iNode>>48
39167 29 20 5e 20 28 69 4e 6f 64 65 3e 3e 34 30 29 20  ) ^ (iNode>>40) 
39168 5e 20 28 69 4e 6f 64 65 3e 3e 33 32 29 20 5e 20  ^ (iNode>>32) ^ 
39169 0a 20 20 20 20 28 69 4e 6f 64 65 3e 3e 32 34 29  .    (iNode>>24)
3916a 20 5e 20 28 69 4e 6f 64 65 3e 3e 31 36 29 20 5e   ^ (iNode>>16) ^
3916b 20 28 69 4e 6f 64 65 3e 3e 20 38 29 20 5e 20 28   (iNode>> 8) ^ (
3916c 69 4e 6f 64 65 3e 3e 20 30 29 0a 20 20 29 20 25  iNode>> 0).  ) %
3916d 20 48 41 53 48 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a   HASHSIZE;.}../*
3916e 0a 2a 2a 20 53 65 61 72 63 68 20 74 68 65 20 6e  .** Search the n
3916f 6f 64 65 20 68 61 73 68 20 74 61 62 6c 65 20 66  ode hash table f
39170 6f 72 20 6e 6f 64 65 20 69 4e 6f 64 65 2e 20 49  or node iNode. I
39171 66 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20  f found, return 
39172 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  a pointer.** to 
39173 69 74 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72  it. Otherwise, r
39174 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74  eturn 0..*/.stat
39175 69 63 20 52 74 72 65 65 4e 6f 64 65 20 2a 6e 6f  ic RtreeNode *no
39176 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 52 74 72  deHashLookup(Rtr
39177 65 65 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20  ee *pRtree, i64 
39178 69 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65 65 4e  iNode){.  RtreeN
39179 6f 64 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  ode *p;.  assert
3917a 28 20 69 4e 6f 64 65 21 3d 30 20 29 3b 0a 20 20  ( iNode!=0 );.  
3917b 66 6f 72 28 70 3d 70 52 74 72 65 65 2d 3e 61 48  for(p=pRtree->aH
3917c 61 73 68 5b 6e 6f 64 65 48 61 73 68 28 69 4e 6f  ash[nodeHash(iNo
3917d 64 65 29 5d 3b 20 70 20 26 26 20 70 2d 3e 69 4e  de)]; p && p->iN
3917e 6f 64 65 21 3d 69 4e 6f 64 65 3b 20 70 3d 70 2d  ode!=iNode; p=p-
3917f 3e 70 4e 65 78 74 29 3b 0a 20 20 72 65 74 75 72  >pNext);.  retur
39180 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  n p;.}../*.** Ad
39181 64 20 6e 6f 64 65 20 70 4e 6f 64 65 20 74 6f 20  d node pNode to 
39182 74 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61  the node hash ta
39183 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
39184 6f 69 64 20 6e 6f 64 65 48 61 73 68 49 6e 73 65  oid nodeHashInse
39185 72 74 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  rt(Rtree *pRtree
39186 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
39187 64 65 29 7b 0a 20 20 69 66 28 20 70 4e 6f 64 65  de){.  if( pNode
39188 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 48 61 73   ){.    int iHas
39189 68 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  h;.    assert( p
3918a 4e 6f 64 65 2d 3e 70 4e 65 78 74 3d 3d 30 20 29  Node->pNext==0 )
3918b 3b 0a 20 20 20 20 69 48 61 73 68 20 3d 20 6e 6f  ;.    iHash = no
3918c 64 65 48 61 73 68 28 70 4e 6f 64 65 2d 3e 69 4e  deHash(pNode->iN
3918d 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  ode);.    pNode-
3918e 3e 70 4e 65 78 74 20 3d 20 70 52 74 72 65 65 2d  >pNext = pRtree-
3918f 3e 61 48 61 73 68 5b 69 48 61 73 68 5d 3b 0a 20  >aHash[iHash];. 
39190 20 20 20 70 52 74 72 65 65 2d 3e 61 48 61 73 68     pRtree->aHash
39191 5b 69 48 61 73 68 5d 20 3d 20 70 4e 6f 64 65 3b  [iHash] = pNode;
39192 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
39193 6d 6f 76 65 20 6e 6f 64 65 20 70 4e 6f 64 65 20  move node pNode 
39194 66 72 6f 6d 20 74 68 65 20 6e 6f 64 65 20 68 61  from the node ha
39195 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  sh table..*/.sta
39196 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 48 61 73  tic void nodeHas
39197 68 44 65 6c 65 74 65 28 52 74 72 65 65 20 2a 70  hDelete(Rtree *p
39198 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
39199 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65   *pNode){.  Rtre
3919a 65 4e 6f 64 65 20 2a 2a 70 70 3b 0a 20 20 69 66  eNode **pp;.  if
3919b 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21 3d  ( pNode->iNode!=
3919c 30 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 26 70  0 ){.    pp = &p
3919d 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e 6f 64  Rtree->aHash[nod
3919e 65 48 61 73 68 28 70 4e 6f 64 65 2d 3e 69 4e 6f  eHash(pNode->iNo
3919f 64 65 29 5d 3b 0a 20 20 20 20 66 6f 72 28 20 3b  de)];.    for( ;
391a0 20 28 2a 70 70 29 21 3d 70 4e 6f 64 65 3b 20 70   (*pp)!=pNode; p
391a1 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78  p = &(*pp)->pNex
391a2 74 29 7b 20 61 73 73 65 72 74 28 2a 70 70 29 3b  t){ assert(*pp);
391a3 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20 70 4e 6f   }.    *pp = pNo
391a4 64 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 70  de->pNext;.    p
391a5 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30 3b  Node->pNext = 0;
391a6 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  .  }.}../*.** Al
391a7 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72  locate and retur
391a8 6e 20 6e 65 77 20 72 2d 74 72 65 65 20 6e 6f 64  n new r-tree nod
391a9 65 2e 20 49 6e 69 74 69 61 6c 6c 79 2c 20 28 52  e. Initially, (R
391aa 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65 3d 3d  treeNode.iNode==
391ab 30 29 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  0),.** indicatin
391ac 67 20 74 68 61 74 20 6e 6f 64 65 20 68 61 73 20  g that node has 
391ad 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 61 73 73  not yet been ass
391ae 69 67 6e 65 64 20 61 20 6e 6f 64 65 20 6e 75 6d  igned a node num
391af 62 65 72 2e 20 49 74 20 69 73 0a 2a 2a 20 61 73  ber. It is.** as
391b0 73 69 67 6e 65 64 20 61 20 6e 6f 64 65 20 6e 75  signed a node nu
391b1 6d 62 65 72 20 77 68 65 6e 20 6e 6f 64 65 57 72  mber when nodeWr
391b2 69 74 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ite() is called 
391b3 74 6f 20 77 72 69 74 65 20 74 68 65 0a 2a 2a 20  to write the.** 
391b4 6e 6f 64 65 20 63 6f 6e 74 65 6e 74 73 20 6f 75  node contents ou
391b5 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
391b6 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74 72  e..*/.static Rtr
391b7 65 65 4e 6f 64 65 20 2a 6e 6f 64 65 4e 65 77 28  eeNode *nodeNew(
391b8 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
391b9 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e  treeNode *pParen
391ba 74 2c 20 69 6e 74 20 7a 65 72 6f 29 7b 0a 20 20  t, int zero){.  
391bb 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
391bc 3b 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74 72  ;.  pNode = (Rtr
391bd 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65 33  eeNode *)sqlite3
391be 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
391bf 74 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74 72  treeNode) + pRtr
391c0 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b 0a  ee->iNodeSize);.
391c1 20 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20    if( pNode ){. 
391c2 20 20 20 6d 65 6d 73 65 74 28 70 4e 6f 64 65 2c     memset(pNode,
391c3 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
391c4 4e 6f 64 65 29 20 2b 20 28 7a 65 72 6f 3f 70 52  Node) + (zero?pR
391c5 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 3a  tree->iNodeSize:
391c6 30 29 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  0));.    pNode->
391c7 7a 44 61 74 61 20 3d 20 28 75 38 20 2a 29 26 70  zData = (u8 *)&p
391c8 4e 6f 64 65 5b 31 5d 3b 0a 20 20 20 20 70 4e 6f  Node[1];.    pNo
391c9 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  de->nRef = 1;.  
391ca 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74    pNode->pParent
391cb 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 20 20   = pParent;.    
391cc 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d  pNode->isDirty =
391cd 20 31 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66 65   1;.    nodeRefe
391ce 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a  rence(pParent);.
391cf 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 6f    }.  return pNo
391d0 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74  de;.}../*.** Obt
391d1 61 69 6e 20 61 20 72 65 66 65 72 65 6e 63 65 20  ain a reference 
391d2 74 6f 20 61 6e 20 72 2d 74 72 65 65 20 6e 6f 64  to an r-tree nod
391d3 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
391d4 0a 6e 6f 64 65 41 63 71 75 69 72 65 28 0a 20 20  .nodeAcquire(.  
391d5 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20  Rtree *pRtree,  
391d6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 2d             /* R-
391d7 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20 2a  tree structure *
391d8 2f 0a 20 20 69 36 34 20 69 4e 6f 64 65 2c 20 20  /.  i64 iNode,  
391d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
391da 2a 20 4e 6f 64 65 20 6e 75 6d 62 65 72 20 74 6f  * Node number to
391db 20 6c 6f 61 64 20 2a 2f 0a 20 20 52 74 72 65 65   load */.  Rtree
391dc 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 2c 20 20  Node *pParent,  
391dd 20 20 20 20 20 20 2f 2a 20 45 69 74 68 65 72 20        /* Either 
391de 74 68 65 20 70 61 72 65 6e 74 20 6e 6f 64 65 20  the parent node 
391df 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 52 74 72  or NULL */.  Rtr
391e0 65 65 4e 6f 64 65 20 2a 2a 70 70 4e 6f 64 65 20  eeNode **ppNode 
391e1 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
391e2 41 63 71 75 69 72 65 64 20 6e 6f 64 65 20 2a 2f  Acquired node */
391e3 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
391e4 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
391e5 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  ;..  /* Check if
391e6 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6e   the requested n
391e7 6f 64 65 20 69 73 20 61 6c 72 65 61 64 79 20 69  ode is already i
391e8 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  n the hash table
391e9 2e 20 49 66 20 73 6f 2c 0a 20 20 2a 2a 20 69 6e  . If so,.  ** in
391ea 63 72 65 61 73 65 20 69 74 73 20 72 65 66 65 72  crease its refer
391eb 65 6e 63 65 20 63 6f 75 6e 74 20 61 6e 64 20 72  ence count and r
391ec 65 74 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a 20  eturn it..  */. 
391ed 20 69 66 28 20 28 70 4e 6f 64 65 20 3d 20 6e 6f   if( (pNode = no
391ee 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74  deHashLookup(pRt
391ef 72 65 65 2c 20 69 4e 6f 64 65 29 29 20 29 7b 0a  ree, iNode)) ){.
391f0 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 61      assert( !pPa
391f1 72 65 6e 74 20 7c 7c 20 21 70 4e 6f 64 65 2d 3e  rent || !pNode->
391f2 70 50 61 72 65 6e 74 20 7c 7c 20 70 4e 6f 64 65  pParent || pNode
391f3 2d 3e 70 50 61 72 65 6e 74 3d 3d 70 50 61 72 65  ->pParent==pPare
391f4 6e 74 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50  nt );.    if( pP
391f5 61 72 65 6e 74 20 26 26 20 21 70 4e 6f 64 65 2d  arent && !pNode-
391f6 3e 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  >pParent ){.    
391f7 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
391f8 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20  pParent);.      
391f9 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d  pNode->pParent =
391fa 20 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 7d 0a   pParent;.    }.
391fb 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b      pNode->nRef+
391fc 2b 3b 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d  +;.    *ppNode =
391fd 20 70 4e 6f 64 65 3b 0a 20 20 20 20 72 65 74 75   pNode;.    retu
391fe 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
391ff 7d 0a 0a 20 20 70 4e 6f 64 65 20 3d 20 28 52 74  }..  pNode = (Rt
39200 72 65 65 4e 6f 64 65 20 2a 29 73 71 6c 69 74 65  reeNode *)sqlite
39201 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
39202 52 74 72 65 65 4e 6f 64 65 29 20 2b 20 70 52 74  RtreeNode) + pRt
39203 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 29 3b  ree->iNodeSize);
39204 0a 20 20 69 66 28 20 21 70 4e 6f 64 65 20 29 7b  .  if( !pNode ){
39205 0a 20 20 20 20 2a 70 70 4e 6f 64 65 20 3d 20 30  .    *ppNode = 0
39206 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
39207 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
39208 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20   pNode->pParent 
39209 3d 20 70 50 61 72 65 6e 74 3b 0a 20 20 70 4e 6f  = pParent;.  pNo
3920a 64 65 2d 3e 7a 44 61 74 61 20 3d 20 28 75 38 20  de->zData = (u8 
3920b 2a 29 26 70 4e 6f 64 65 5b 31 5d 3b 0a 20 20 70  *)&pNode[1];.  p
3920c 4e 6f 64 65 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a  Node->nRef = 1;.
3920d 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d    pNode->iNode =
3920e 20 69 4e 6f 64 65 3b 0a 20 20 70 4e 6f 64 65 2d   iNode;.  pNode-
3920f 3e 69 73 44 69 72 74 79 20 3d 20 30 3b 0a 20 20  >isDirty = 0;.  
39210 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d 20 30  pNode->pNext = 0
39211 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  ;..  sqlite3_bin
39212 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e  d_int64(pRtree->
39213 70 52 65 61 64 4e 6f 64 65 2c 20 31 2c 20 69 4e  pReadNode, 1, iN
39214 6f 64 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  ode);.  rc = sql
39215 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
39216 2d 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20  ->pReadNode);.  
39217 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
39218 4f 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  OW ){.    const 
39219 75 38 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69  u8 *zBlob = sqli
3921a 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
3921b 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64  pRtree->pReadNod
3921c 65 2c 20 30 29 3b 0a 20 20 20 20 6d 65 6d 63 70  e, 0);.    memcp
3921d 79 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c 20  y(pNode->zData, 
3921e 7a 42 6c 6f 62 2c 20 70 52 74 72 65 65 2d 3e 69  zBlob, pRtree->i
3921f 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 20 20 6e  NodeSize);.    n
39220 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70 50 61  odeReference(pPa
39221 72 65 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rent);.  }else{.
39222 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
39223 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f  (pNode);.    pNo
39224 64 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2a  de = 0;.  }..  *
39225 70 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b 0a  ppNode = pNode;.
39226 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
39227 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
39228 61 64 4e 6f 64 65 29 3b 0a 0a 20 20 69 66 28 20  adNode);..  if( 
39229 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
3922a 20 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20   iNode==1 ){.   
3922b 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20   pRtree->iDepth 
3922c 3d 20 72 65 61 64 49 6e 74 31 36 28 70 4e 6f 64  = readInt16(pNod
3922d 65 2d 3e 7a 44 61 74 61 29 3b 0a 20 20 7d 0a 0a  e->zData);.  }..
3922e 20 20 61 73 73 65 72 74 28 20 28 72 63 3d 3d 53    assert( (rc==S
3922f 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4e 6f 64  QLITE_OK && pNod
39230 65 29 20 7c 7c 20 28 70 4e 6f 64 65 3d 3d 30 20  e) || (pNode==0 
39231 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  && rc!=SQLITE_OK
39232 29 20 29 3b 0a 20 20 6e 6f 64 65 48 61 73 68 49  ) );.  nodeHashI
39233 6e 73 65 72 74 28 70 52 74 72 65 65 2c 20 70 4e  nsert(pRtree, pN
39234 6f 64 65 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  ode);..  return 
39235 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 76 65  rc;.}../*.** Ove
39236 72 77 72 69 74 65 20 63 65 6c 6c 20 69 43 65 6c  rwrite cell iCel
39237 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 20  l of node pNode 
39238 77 69 74 68 20 74 68 65 20 63 6f 6e 74 65 6e 74  with the content
39239 73 20 6f 66 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73  s of pCell..*/.s
3923a 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 4f  tatic void nodeO
3923b 76 65 72 77 72 69 74 65 43 65 6c 6c 28 0a 20 20  verwriteCell(.  
3923c 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
3923d 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
3923e 64 65 2c 20 20 0a 20 20 52 74 72 65 65 43 65 6c  de,  .  RtreeCel
3923f 6c 20 2a 70 43 65 6c 6c 2c 20 0a 20 20 69 6e 74  l *pCell, .  int
39240 20 69 43 65 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20   iCell.){.  int 
39241 69 69 3b 0a 20 20 75 38 20 2a 70 20 3d 20 26 70  ii;.  u8 *p = &p
39242 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b 20  Node->zData[4 + 
39243 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
39244 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 3b 0a 20 20  rCell*iCell];.  
39245 70 20 2b 3d 20 77 72 69 74 65 49 6e 74 36 34 28  p += writeInt64(
39246 70 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  p, pCell->iRowid
39247 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
39248 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a  i<(pRtree->nDim*
39249 32 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 70  2); ii++){.    p
3924a 20 2b 3d 20 77 72 69 74 65 43 6f 6f 72 64 28 70   += writeCoord(p
3924b 2c 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64  , &pCell->aCoord
3924c 5b 69 69 5d 29 3b 0a 20 20 7d 0a 20 20 70 4e 6f  [ii]);.  }.  pNo
3924d 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
3924e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65  .}../*.** Remove
3924f 20 63 65 6c 6c 20 74 68 65 20 63 65 6c 6c 20 77   cell the cell w
39250 69 74 68 20 69 6e 64 65 78 20 69 43 65 6c 6c 20  ith index iCell 
39251 66 72 6f 6d 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  from node pNode.
39252 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
39253 6e 6f 64 65 44 65 6c 65 74 65 43 65 6c 6c 28 52  nodeDeleteCell(R
39254 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
39255 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20  reeNode *pNode, 
39256 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20 20 75 38  int iCell){.  u8
39257 20 2a 70 44 73 74 20 3d 20 26 70 4e 6f 64 65 2d   *pDst = &pNode-
39258 3e 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65  >zData[4 + pRtre
39259 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c  e->nBytesPerCell
3925a 2a 69 43 65 6c 6c 5d 3b 0a 20 20 75 38 20 2a 70  *iCell];.  u8 *p
3925b 53 72 63 20 3d 20 26 70 44 73 74 5b 70 52 74 72  Src = &pDst[pRtr
3925c 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
3925d 6c 5d 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20  l];.  int nByte 
3925e 3d 20 28 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20  = (NCELL(pNode) 
3925f 2d 20 69 43 65 6c 6c 20 2d 20 31 29 20 2a 20 70  - iCell - 1) * p
39260 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72  Rtree->nBytesPer
39261 43 65 6c 6c 3b 0a 20 20 6d 65 6d 6d 6f 76 65 28  Cell;.  memmove(
39262 70 44 73 74 2c 20 70 53 72 63 2c 20 6e 42 79 74  pDst, pSrc, nByt
39263 65 29 3b 0a 20 20 77 72 69 74 65 49 6e 74 31 36  e);.  writeInt16
39264 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 32  (&pNode->zData[2
39265 5d 2c 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 2d  ], NCELL(pNode)-
39266 31 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44  1);.  pNode->isD
39267 69 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  irty = 1;.}../*.
39268 2a 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63 6f  ** Insert the co
39269 6e 74 65 6e 74 73 20 6f 66 20 63 65 6c 6c 20 70  ntents of cell p
3926a 43 65 6c 6c 20 69 6e 74 6f 20 6e 6f 64 65 20 70  Cell into node p
3926b 4e 6f 64 65 2e 20 49 66 20 74 68 65 20 69 6e 73  Node. If the ins
3926c 65 72 74 0a 2a 2a 20 69 73 20 73 75 63 63 65 73  ert.** is succes
3926d 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51 4c  sful, return SQL
3926e 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  ITE_OK..**.** If
3926f 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 65 6e   there is not en
39270 6f 75 67 68 20 66 72 65 65 20 73 70 61 63 65 20  ough free space 
39271 69 6e 20 70 4e 6f 64 65 2c 20 72 65 74 75 72 6e  in pNode, return
39272 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 0a 2a 2f   SQLITE_FULL..*/
39273 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65  .static int.node
39274 49 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 52 74  InsertCell(.  Rt
39275 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20 20  ree *pRtree, .  
39276 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
39277 2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  , .  RtreeCell *
39278 70 43 65 6c 6c 20 0a 29 7b 0a 20 20 69 6e 74 20  pCell .){.  int 
39279 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  nCell;          
3927a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
3927b 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 63  rent number of c
3927c 65 6c 6c 73 20 69 6e 20 70 4e 6f 64 65 20 2a 2f  ells in pNode */
3927d 0a 20 20 69 6e 74 20 6e 4d 61 78 43 65 6c 6c 3b  .  int nMaxCell;
3927e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3927f 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62   /* Maximum numb
39280 65 72 20 6f 66 20 63 65 6c 6c 73 20 66 6f 72 20  er of cells for 
39281 70 4e 6f 64 65 20 2a 2f 0a 0a 20 20 6e 4d 61 78  pNode */..  nMax
39282 43 65 6c 6c 20 3d 20 28 70 52 74 72 65 65 2d 3e  Cell = (pRtree->
39283 69 4e 6f 64 65 53 69 7a 65 2d 34 29 2f 70 52 74  iNodeSize-4)/pRt
39284 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65  ree->nBytesPerCe
39285 6c 6c 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43  ll;.  nCell = NC
39286 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 0a 20 20 61  ELL(pNode);..  a
39287 73 73 65 72 74 28 6e 43 65 6c 6c 3c 3d 6e 4d 61  ssert(nCell<=nMa
39288 78 43 65 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 6e  xCell);..  if( n
39289 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 20 29 7b  Cell<nMaxCell ){
3928a 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69  .    nodeOverwri
3928b 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
3928c 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 6e 43 65  Node, pCell, nCe
3928d 6c 6c 29 3b 0a 20 20 20 20 77 72 69 74 65 49 6e  ll);.    writeIn
3928e 74 31 36 28 26 70 4e 6f 64 65 2d 3e 7a 44 61 74  t16(&pNode->zDat
3928f 61 5b 32 5d 2c 20 6e 43 65 6c 6c 2b 31 29 3b 0a  a[2], nCell+1);.
39290 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72      pNode->isDir
39291 74 79 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 72  ty = 1;.  }..  r
39292 65 74 75 72 6e 20 28 6e 43 65 6c 6c 3d 3d 6e 4d  eturn (nCell==nM
39293 61 78 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  axCell);.}../*.*
39294 2a 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69 73  * If the node is
39295 20 64 69 72 74 79 2c 20 77 72 69 74 65 20 69 74   dirty, write it
39296 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
39297 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
39298 69 6e 74 0a 6e 6f 64 65 57 72 69 74 65 28 52 74  int.nodeWrite(Rt
39299 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3929a 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
3929b 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3929c 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64  E_OK;.  if( pNod
3929d 65 2d 3e 69 73 44 69 72 74 79 20 29 7b 0a 20 20  e->isDirty ){.  
3929e 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
3929f 70 20 3d 20 70 52 74 72 65 65 2d 3e 70 57 72 69  p = pRtree->pWri
392a0 74 65 4e 6f 64 65 3b 0a 20 20 20 20 69 66 28 20  teNode;.    if( 
392a1 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 29 7b 0a  pNode->iNode ){.
392a2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62 69        sqlite3_bi
392a3 6e 64 5f 69 6e 74 36 34 28 70 2c 20 31 2c 20 70  nd_int64(p, 1, p
392a4 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
392a5 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
392a6 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c  qlite3_bind_null
392a7 28 70 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20  (p, 1);.    }.  
392a8 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62    sqlite3_bind_b
392a9 6c 6f 62 28 70 2c 20 32 2c 20 70 4e 6f 64 65 2d  lob(p, 2, pNode-
392aa 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e  >zData, pRtree->
392ab 69 4e 6f 64 65 53 69 7a 65 2c 20 53 51 4c 49 54  iNodeSize, SQLIT
392ac 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  E_STATIC);.    s
392ad 71 6c 69 74 65 33 5f 73 74 65 70 28 70 29 3b 0a  qlite3_step(p);.
392ae 20 20 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72      pNode->isDir
392af 74 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  ty = 0;.    rc =
392b0 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
392b1 29 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65  );.    if( pNode
392b2 2d 3e 69 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63  ->iNode==0 && rc
392b3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
392b4 20 20 20 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64       pNode->iNod
392b5 65 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  e = sqlite3_last
392b6 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70 52  _insert_rowid(pR
392b7 74 72 65 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20  tree->db);.     
392b8 20 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28   nodeHashInsert(
392b9 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
392ba 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
392bb 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
392bc 52 65 6c 65 61 73 65 20 61 20 72 65 66 65 72 65  Release a refere
392bd 6e 63 65 20 74 6f 20 61 20 6e 6f 64 65 2e 20 49  nce to a node. I
392be 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 64 69  f the node is di
392bf 72 74 79 20 61 6e 64 20 74 68 65 20 72 65 66 65  rty and the refe
392c0 72 65 6e 63 65 0a 2a 2a 20 63 6f 75 6e 74 20 64  rence.** count d
392c1 72 6f 70 73 20 74 6f 20 7a 65 72 6f 2c 20 74 68  rops to zero, th
392c2 65 20 6e 6f 64 65 20 64 61 74 61 20 69 73 20 77  e node data is w
392c3 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61  ritten to the da
392c4 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  tabase..*/.stati
392c5 63 20 69 6e 74 0a 6e 6f 64 65 52 65 6c 65 61 73  c int.nodeReleas
392c6 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
392c7 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
392c8 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
392c9 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
392ca 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 61 73 73  pNode ){.    ass
392cb 65 72 74 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  ert( pNode->nRef
392cc 3e 30 20 29 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  >0 );.    pNode-
392cd 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28  >nRef--;.    if(
392ce 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 3d 3d 30 20   pNode->nRef==0 
392cf 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 6f  ){.      if( pNo
392d0 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a  de->iNode==1 ){.
392d1 20 20 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e          pRtree->
392d2 69 44 65 70 74 68 20 3d 20 2d 31 3b 0a 20 20 20  iDepth = -1;.   
392d3 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
392d4 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 29 7b  Node->pParent ){
392d5 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  .        rc = no
392d6 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
392d7 2c 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  , pNode->pParent
392d8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
392d9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
392da 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
392db 20 3d 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74   = nodeWrite(pRt
392dc 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20  ree, pNode);.   
392dd 20 20 20 7d 0a 20 20 20 20 20 20 6e 6f 64 65 48     }.      nodeH
392de 61 73 68 44 65 6c 65 74 65 28 70 52 74 72 65 65  ashDelete(pRtree
392df 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  , pNode);.      
392e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4e 6f  sqlite3_free(pNo
392e1 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  de);.    }.  }. 
392e2 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
392e3 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
392e4 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 76  64-bit integer v
392e5 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64 20  alue associated 
392e6 77 69 74 68 20 63 65 6c 6c 20 69 43 65 6c 6c 20  with cell iCell 
392e7 6f 66 0a 2a 2a 20 6e 6f 64 65 20 70 4e 6f 64 65  of.** node pNode
392e8 2e 20 49 66 20 70 4e 6f 64 65 20 69 73 20 61 20  . If pNode is a 
392e9 6c 65 61 66 20 6e 6f 64 65 2c 20 74 68 69 73 20  leaf node, this 
392ea 69 73 20 61 20 72 6f 77 69 64 2e 20 49 66 20 69  is a rowid. If i
392eb 74 20 69 73 0a 2a 2a 20 61 6e 20 69 6e 74 65 72  t is.** an inter
392ec 6e 61 6c 20 6e 6f 64 65 2c 20 74 68 65 6e 20 74  nal node, then t
392ed 68 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65  he 64-bit intege
392ee 72 20 69 73 20 61 20 63 68 69 6c 64 20 70 61 67  r is a child pag
392ef 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
392f0 74 69 63 20 69 36 34 20 6e 6f 64 65 47 65 74 52  tic i64 nodeGetR
392f1 6f 77 69 64 28 0a 20 20 52 74 72 65 65 20 2a 70  owid(.  Rtree *p
392f2 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e  Rtree, .  RtreeN
392f3 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69  ode *pNode, .  i
392f4 6e 74 20 69 43 65 6c 6c 0a 29 7b 0a 20 20 61 73  nt iCell.){.  as
392f5 73 65 72 74 28 20 69 43 65 6c 6c 3c 4e 43 45 4c  sert( iCell<NCEL
392f6 4c 28 70 4e 6f 64 65 29 20 29 3b 0a 20 20 72 65  L(pNode) );.  re
392f7 74 75 72 6e 20 72 65 61 64 49 6e 74 36 34 28 26  turn readInt64(&
392f8 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b 34 20 2b  pNode->zData[4 +
392f9 20 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50   pRtree->nBytesP
392fa 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d 29 3b 0a  erCell*iCell]);.
392fb 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
392fc 63 6f 6f 72 64 69 6e 61 74 65 20 69 43 6f 6f 72  coordinate iCoor
392fd 64 20 66 72 6f 6d 20 63 65 6c 6c 20 69 43 65 6c  d from cell iCel
392fe 6c 20 69 6e 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l in node pNode.
392ff 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
39300 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 0a 20 20  nodeGetCoord(.  
39301 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a  Rtree *pRtree, .
39302 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
39303 64 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c  de, .  int iCell
39304 2c 0a 20 20 69 6e 74 20 69 43 6f 6f 72 64 2c 0a  ,.  int iCoord,.
39305 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43    RtreeCoord *pC
39306 6f 6f 72 64 20 20 20 20 20 20 20 20 20 20 20 2f  oord           /
39307 2a 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65  * Space to write
39308 20 72 65 73 75 6c 74 20 74 6f 20 2a 2f 0a 29 7b   result to */.){
39309 0a 20 20 72 65 61 64 43 6f 6f 72 64 28 26 70 4e  .  readCoord(&pN
3930a 6f 64 65 2d 3e 7a 44 61 74 61 5b 31 32 20 2b 20  ode->zData[12 + 
3930b 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
3930c 72 43 65 6c 6c 2a 69 43 65 6c 6c 20 2b 20 34 2a  rCell*iCell + 4*
3930d 69 43 6f 6f 72 64 5d 2c 20 70 43 6f 6f 72 64 29  iCoord], pCoord)
3930e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72  ;.}../*.** Deser
3930f 69 61 6c 69 7a 65 20 63 65 6c 6c 20 69 43 65 6c  ialize cell iCel
39310 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  l of node pNode.
39311 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 73 74   Populate the st
39312 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 0a  ructure pointed.
39313 2a 2a 20 74 6f 20 62 79 20 70 43 65 6c 6c 20 77  ** to by pCell w
39314 69 74 68 20 74 68 65 20 72 65 73 75 6c 74 73 2e  ith the results.
39315 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
39316 6e 6f 64 65 47 65 74 43 65 6c 6c 28 0a 20 20 52  nodeGetCell(.  R
39317 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
39318 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
39319 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 2c  e, .  int iCell,
3931a 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
3931b 65 6c 6c 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b  ell.){.  int ii;
3931c 0a 20 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  .  pCell->iRowid
3931d 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28   = nodeGetRowid(
3931e 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
3931f 43 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69 3d  Cell);.  for(ii=
39320 30 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44  0; ii<pRtree->nD
39321 69 6d 2a 32 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  im*2; ii++){.   
39322 20 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70 52   nodeGetCoord(pR
39323 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43 65  tree, pNode, iCe
39324 6c 6c 2c 20 69 69 2c 20 26 70 43 65 6c 6c 2d 3e  ll, ii, &pCell->
39325 61 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a 20 20 7d  aCoord[ii]);.  }
39326 0a 7d 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20  .}.../* Forward 
39327 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20  declaration for 
39328 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  the function tha
39329 74 20 64 6f 65 73 20 74 68 65 20 77 6f 72 6b 20  t does the work 
3932a 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75 61  of.** the virtua
3932b 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
3932c 43 72 65 61 74 65 28 29 20 61 6e 64 20 78 43 6f  Create() and xCo
3932d 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 73 2e  nnect() methods.
3932e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
3932f 74 72 65 65 49 6e 69 74 28 0a 20 20 73 71 6c 69  treeInit(.  sqli
39330 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 69  te3 *, void *, i
39331 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  nt, const char *
39332 63 6f 6e 73 74 2a 2c 20 73 71 6c 69 74 65 33 5f  const*, sqlite3_
39333 76 74 61 62 20 2a 2a 2c 20 63 68 61 72 20 2a 2a  vtab **, char **
39334 2c 20 69 6e 74 0a 29 3b 0a 0a 2f 2a 20 0a 2a 2a  , int.);../* .**
39335 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
39336 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 72 65  able module xCre
39337 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  ate method..*/.s
39338 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 43  tatic int rtreeC
39339 72 65 61 74 65 28 0a 20 20 73 71 6c 69 74 65 33  reate(.  sqlite3
3933a 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70 41   *db,.  void *pA
3933b 75 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  ux,.  int argc, 
3933c 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73  const char *cons
3933d 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65  t*argv,.  sqlite
3933e 33 5f 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c  3_vtab **ppVtab,
3933f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a  .  char **pzErr.
39340 29 7b 0a 20 20 72 65 74 75 72 6e 20 72 74 72 65  ){.  return rtre
39341 65 49 6e 69 74 28 64 62 2c 20 70 41 75 78 2c 20  eInit(db, pAux, 
39342 61 72 67 63 2c 20 61 72 67 76 2c 20 70 70 56 74  argc, argv, ppVt
39343 61 62 2c 20 70 7a 45 72 72 2c 20 31 29 3b 0a 7d  ab, pzErr, 1);.}
39344 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
39345 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
39346 75 6c 65 20 78 43 6f 6e 6e 65 63 74 20 6d 65 74  ule xConnect met
39347 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
39348 6e 74 20 72 74 72 65 65 43 6f 6e 6e 65 63 74 28  nt rtreeConnect(
39349 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
3934a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20    void *pAux,.  
3934b 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20  int argc, const 
3934c 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76  char *const*argv
3934d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ,.  sqlite3_vtab
3934e 20 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68 61   **ppVtab,.  cha
3934f 72 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 72  r **pzErr.){.  r
39350 65 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74 28  eturn rtreeInit(
39351 64 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c 20  db, pAux, argc, 
39352 61 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70 7a  argv, ppVtab, pz
39353 45 72 72 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Err, 0);.}../*.*
39354 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  * Increment the 
39355 72 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65  r-tree reference
39356 20 63 6f 75 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69   count..*/.stati
39357 63 20 76 6f 69 64 20 72 74 72 65 65 52 65 66 65  c void rtreeRefe
39358 72 65 6e 63 65 28 52 74 72 65 65 20 2a 70 52 74  rence(Rtree *pRt
39359 72 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d 3e  ree){.  pRtree->
3935a 6e 42 75 73 79 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a  nBusy++;.}../*.*
3935b 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20  * Decrement the 
3935c 72 2d 74 72 65 65 20 72 65 66 65 72 65 6e 63 65  r-tree reference
3935d 20 63 6f 75 6e 74 2e 20 57 68 65 6e 20 74 68 65   count. When the
3935e 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
3935f 20 72 65 61 63 68 65 73 0a 2a 2a 20 7a 65 72 6f   reaches.** zero
39360 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 69   the structure i
39361 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73 74  s deleted..*/.st
39362 61 74 69 63 20 76 6f 69 64 20 72 74 72 65 65 52  atic void rtreeR
39363 65 6c 65 61 73 65 28 52 74 72 65 65 20 2a 70 52  elease(Rtree *pR
39364 74 72 65 65 29 7b 0a 20 20 70 52 74 72 65 65 2d  tree){.  pRtree-
39365 3e 6e 42 75 73 79 2d 2d 3b 0a 20 20 69 66 28 20  >nBusy--;.  if( 
39366 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 3d 3d 30  pRtree->nBusy==0
39367 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
39368 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d  finalize(pRtree-
39369 3e 70 52 65 61 64 4e 6f 64 65 29 3b 0a 20 20 20  >pReadNode);.   
3936a 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
3936b 65 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  e(pRtree->pWrite
3936c 4e 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Node);.    sqlit
3936d 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
3936e 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29  ee->pDeleteNode)
3936f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
39370 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70  nalize(pRtree->p
39371 52 65 61 64 52 6f 77 69 64 29 3b 0a 20 20 20 20  ReadRowid);.    
39372 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
39373 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  (pRtree->pWriteR
39374 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
39375 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
39376 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
39377 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
39378 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
39379 70 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20 20  pReadParent);.  
3937a 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
3937b 7a 65 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  ze(pRtree->pWrit
3937c 65 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 73 71  eParent);.    sq
3937d 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70  lite3_finalize(p
3937e 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61  Rtree->pDeletePa
3937f 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  rent);.    sqlit
39380 65 33 5f 66 72 65 65 28 70 52 74 72 65 65 29 3b  e3_free(pRtree);
39381 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  .  }.}../* .** R
39382 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
39383 6c 65 20 6d 6f 64 75 6c 65 20 78 44 69 73 63 6f  le module xDisco
39384 6e 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f  nnect method..*/
39385 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
39386 65 44 69 73 63 6f 6e 6e 65 63 74 28 73 71 6c 69  eDisconnect(sqli
39387 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 29  te3_vtab *pVtab)
39388 7b 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73 65  {.  rtreeRelease
39389 28 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 29  ((Rtree *)pVtab)
3938a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
3938b 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  E_OK;.}../* .** 
3938c 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61  Rtree virtual ta
3938d 62 6c 65 20 6d 6f 64 75 6c 65 20 78 44 65 73 74  ble module xDest
3938e 72 6f 79 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73  roy method..*/.s
3938f 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 44  tatic int rtreeD
39390 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 76  estroy(sqlite3_v
39391 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 52  tab *pVtab){.  R
39392 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
39393 52 74 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20  Rtree *)pVtab;. 
39394 20 69 6e 74 20 72 63 3b 0a 20 20 63 68 61 72 20   int rc;.  char 
39395 2a 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74  *zCreate = sqlit
39396 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20  e3_mprintf(.    
39397 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71 27  "DROP TABLE '%q'
39398 2e 27 25 71 5f 6e 6f 64 65 27 3b 22 0a 20 20 20  .'%q_node';".   
39399 20 22 44 52 4f 50 20 54 41 42 4c 45 20 27 25 71   "DROP TABLE '%q
3939a 27 2e 27 25 71 5f 72 6f 77 69 64 27 3b 22 0a 20  '.'%q_rowid';". 
3939b 20 20 20 22 44 52 4f 50 20 54 41 42 4c 45 20 27     "DROP TABLE '
3939c 25 71 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 3b  %q'.'%q_parent';
3939d 22 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 7a  ",.    pRtree->z
3939e 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d  Db, pRtree->zNam
3939f 65 2c 20 0a 20 20 20 20 70 52 74 72 65 65 2d 3e  e, .    pRtree->
393a0 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
393a1 6d 65 2c 0a 20 20 20 20 70 52 74 72 65 65 2d 3e  me,.    pRtree->
393a2 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
393a3 6d 65 0a 20 20 29 3b 0a 20 20 69 66 28 20 21 7a  me.  );.  if( !z
393a4 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 72 63  Create ){.    rc
393a5 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
393a6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
393a7 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
393a8 70 52 74 72 65 65 2d 3e 64 62 2c 20 7a 43 72 65  pRtree->db, zCre
393a9 61 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  ate, 0, 0, 0);. 
393aa 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
393ab 7a 43 72 65 61 74 65 29 3b 0a 20 20 7d 0a 20 20  zCreate);.  }.  
393ac 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
393ad 4b 20 29 7b 0a 20 20 20 20 72 74 72 65 65 52 65  K ){.    rtreeRe
393ae 6c 65 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20  lease(pRtree);. 
393af 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
393b0 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  .}../* .** Rtree
393b1 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
393b2 6f 64 75 6c 65 20 78 4f 70 65 6e 20 6d 65 74 68  odule xOpen meth
393b3 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  od..*/.static in
393b4 74 20 72 74 72 65 65 4f 70 65 6e 28 73 71 6c 69  t rtreeOpen(sqli
393b5 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c  te3_vtab *pVTab,
393b6 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
393b7 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f 72 29  rsor **ppCursor)
393b8 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
393b9 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 52 74 72  ITE_NOMEM;.  Rtr
393ba 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 0a  eeCursor *pCsr;.
393bb 0a 20 20 70 43 73 72 20 3d 20 28 52 74 72 65 65  .  pCsr = (Rtree
393bc 43 75 72 73 6f 72 20 2a 29 73 71 6c 69 74 65 33  Cursor *)sqlite3
393bd 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52  _malloc(sizeof(R
393be 74 72 65 65 43 75 72 73 6f 72 29 29 3b 0a 20 20  treeCursor));.  
393bf 69 66 28 20 70 43 73 72 20 29 7b 0a 20 20 20 20  if( pCsr ){.    
393c0 6d 65 6d 73 65 74 28 70 43 73 72 2c 20 30 2c 20  memset(pCsr, 0, 
393c1 73 69 7a 65 6f 66 28 52 74 72 65 65 43 75 72 73  sizeof(RtreeCurs
393c2 6f 72 29 29 3b 0a 20 20 20 20 70 43 73 72 2d 3e  or));.    pCsr->
393c3 62 61 73 65 2e 70 56 74 61 62 20 3d 20 70 56 54  base.pVtab = pVT
393c4 61 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ab;.    rc = SQL
393c5 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 2a 70  ITE_OK;.  }.  *p
393c6 70 43 75 72 73 6f 72 20 3d 20 28 73 71 6c 69 74  pCursor = (sqlit
393c7 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
393c8 29 70 43 73 72 3b 0a 0a 20 20 72 65 74 75 72 6e  )pCsr;..  return
393c9 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52   rc;.}../* .** R
393ca 74 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62  tree virtual tab
393cb 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6c 6f 73 65  le module xClose
393cc 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74   method..*/.stat
393cd 69 63 20 69 6e 74 20 72 74 72 65 65 43 6c 6f 73  ic int rtreeClos
393ce 65 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  e(sqlite3_vtab_c
393cf 75 72 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52  ursor *cur){.  R
393d0 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28  tree *pRtree = (
393d1 52 74 72 65 65 20 2a 29 28 63 75 72 2d 3e 70 56  Rtree *)(cur->pV
393d2 74 61 62 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  tab);.  int rc;.
393d3 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70    RtreeCursor *p
393d4 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73  Csr = (RtreeCurs
393d5 6f 72 20 2a 29 63 75 72 3b 0a 20 20 73 71 6c 69  or *)cur;.  sqli
393d6 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 61  te3_free(pCsr->a
393d7 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20 72  Constraint);.  r
393d8 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
393d9 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e  pRtree, pCsr->pN
393da 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ode);.  sqlite3_
393db 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 72 65  free(pCsr);.  re
393dc 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
393dd 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20  * Rtree virtual 
393de 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 45 6f  table module xEo
393df 66 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20  f method..**.** 
393e0 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20  Return non-zero 
393e1 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f  if the cursor do
393e2 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  es not currently
393e3 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c 69   point to a vali
393e4 64 20 0a 2a 2a 20 72 65 63 6f 72 64 20 28 69 2e  d .** record (i.
393e5 65 20 69 66 20 74 68 65 20 73 63 61 6e 20 68 61  e if the scan ha
393e6 73 20 66 69 6e 69 73 68 65 64 29 2c 20 6f 72 20  s finished), or 
393e7 7a 65 72 6f 20 6f 74 68 65 72 77 69 73 65 2e 0a  zero otherwise..
393e8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
393e9 72 65 65 45 6f 66 28 73 71 6c 69 74 65 33 5f 76  reeEof(sqlite3_v
393ea 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72 29  tab_cursor *cur)
393eb 7b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72 20  {.  RtreeCursor 
393ec 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43 75  *pCsr = (RtreeCu
393ed 72 73 6f 72 20 2a 29 63 75 72 3b 0a 20 20 72 65  rsor *)cur;.  re
393ee 74 75 72 6e 20 28 70 43 73 72 2d 3e 70 4e 6f 64  turn (pCsr->pNod
393ef 65 3d 3d 30 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  e==0);.}../* .**
393f0 20 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20   Cursor pCursor 
393f1 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73  currently points
393f2 20 74 6f 20 61 20 63 65 6c 6c 20 69 6e 20 61 20   to a cell in a 
393f3 6e 6f 6e 2d 6c 65 61 66 20 70 61 67 65 2e 0a 2a  non-leaf page..*
393f4 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
393f5 20 74 68 65 20 73 75 62 2d 74 72 65 65 20 68 65   the sub-tree he
393f6 61 64 65 64 20 62 79 20 74 68 65 20 63 65 6c 6c  aded by the cell
393f7 20 69 73 20 66 69 6c 74 65 72 65 64 0a 2a 2a 20   is filtered.** 
393f8 28 65 78 63 6c 75 64 65 64 29 20 62 79 20 74 68  (excluded) by th
393f9 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 69 6e  e constraints in
393fa 20 74 68 65 20 70 43 75 72 73 6f 72 2d 3e 61 43   the pCursor->aC
393fb 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 0a 2a 2a 20  onstraint[] .** 
393fc 61 72 72 61 79 2c 20 6f 72 20 66 61 6c 73 65 20  array, or false 
393fd 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
393fe 61 74 69 63 20 69 6e 74 20 74 65 73 74 52 74 72  atic int testRtr
393ff 65 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52  eeCell(Rtree *pR
39400 74 72 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f  tree, RtreeCurso
39401 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52  r *pCursor){.  R
39402 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
39403 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 62   int ii;.  int b
39404 52 65 73 20 3d 20 30 3b 0a 0a 20 20 6e 6f 64 65  Res = 0;..  node
39405 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
39406 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20  pCursor->pNode, 
39407 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20  pCursor->iCell, 
39408 26 63 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69  &cell);.  for(ii
39409 3d 30 3b 20 62 52 65 73 3d 3d 30 20 26 26 20 69  =0; bRes==0 && i
3940a 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43 6f 6e 73  i<pCursor->nCons
3940b 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20  traint; ii++){. 
3940c 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69     RtreeConstrai
3940d 6e 74 20 2a 70 20 3d 20 26 70 43 75 72 73 6f 72  nt *p = &pCursor
3940e 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69  ->aConstraint[ii
3940f 5d 3b 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 65  ];.    double ce
39410 6c 6c 5f 6d 69 6e 20 3d 20 44 43 4f 4f 52 44 28  ll_min = DCOORD(
39411 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e  cell.aCoord[(p->
39412 69 43 6f 6f 72 64 3e 3e 31 29 2a 32 5d 29 3b 0a  iCoord>>1)*2]);.
39413 20 20 20 20 64 6f 75 62 6c 65 20 63 65 6c 6c 5f      double cell_
39414 6d 61 78 20 3d 20 44 43 4f 4f 52 44 28 63 65 6c  max = DCOORD(cel
39415 6c 2e 61 43 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f  l.aCoord[(p->iCo
39416 6f 72 64 3e 3e 31 29 2a 32 2b 31 5d 29 3b 0a 0a  ord>>1)*2+1]);..
39417 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 6f 70      assert(p->op
39418 3d 3d 52 54 52 45 45 5f 4c 45 20 7c 7c 20 70 2d  ==RTREE_LE || p-
39419 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 54 20 7c 7c  >op==RTREE_LT ||
3941a 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 45   p->op==RTREE_GE
3941b 20 0a 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e   .        || p->
3941c 6f 70 3d 3d 52 54 52 45 45 5f 47 54 20 7c 7c 20  op==RTREE_GT || 
3941d 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 45 51 0a  p->op==RTREE_EQ.
3941e 20 20 20 20 29 3b 0a 0a 20 20 20 20 73 77 69 74      );..    swit
3941f 63 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20  ch( p->op ){.   
39420 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 4c 45     case RTREE_LE
39421 3a 20 63 61 73 65 20 52 54 52 45 45 5f 4c 54 3a  : case RTREE_LT:
39422 20 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c 75   bRes = p->rValu
39423 65 3c 63 65 6c 6c 5f 6d 69 6e 3b 20 62 72 65 61  e<cell_min; brea
39424 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54  k;.      case RT
39425 52 45 45 5f 47 45 3a 20 63 61 73 65 20 52 54 52  REE_GE: case RTR
39426 45 45 5f 47 54 3a 20 62 52 65 73 20 3d 20 70 2d  EE_GT: bRes = p-
39427 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d 61 78  >rValue>cell_max
39428 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63  ; break;.      c
39429 61 73 65 20 52 54 52 45 45 5f 45 51 3a 20 0a 20  ase RTREE_EQ: . 
3942a 20 20 20 20 20 20 20 62 52 65 73 20 3d 20 28 70         bRes = (p
3942b 2d 3e 72 56 61 6c 75 65 3e 63 65 6c 6c 5f 6d 61  ->rValue>cell_ma
3942c 78 20 7c 7c 20 70 2d 3e 72 56 61 6c 75 65 3c 63  x || p->rValue<c
3942d 65 6c 6c 5f 6d 69 6e 29 3b 0a 20 20 20 20 20 20  ell_min);.      
3942e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
3942f 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 62 52 65   }..  return bRe
39430 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74  s;.}../* .** Ret
39431 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
39432 63 65 6c 6c 20 74 68 61 74 20 63 75 72 73 6f 72  cell that cursor
39433 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74   pCursor current
39434 6c 79 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20  ly points to.** 
39435 77 6f 75 6c 64 20 62 65 20 66 69 6c 74 65 72 65  would be filtere
39436 64 20 28 65 78 63 6c 75 64 65 64 29 20 62 79 20  d (excluded) by 
39437 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  the constraints 
39438 69 6e 20 74 68 65 20 0a 2a 2a 20 70 43 75 72 73  in the .** pCurs
39439 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b  or->aConstraint[
3943a 5d 20 61 72 72 61 79 2c 20 6f 72 20 66 61 6c 73  ] array, or fals
3943b 65 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a  e otherwise..**.
3943c 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
3943d 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68   assumes that th
3943e 65 20 63 65 6c 6c 20 69 73 20 70 61 72 74 20 6f  e cell is part o
3943f 66 20 61 20 6c 65 61 66 20 6e 6f 64 65 2e 0a 2a  f a leaf node..*
39440 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 73  /.static int tes
39441 74 52 74 72 65 65 45 6e 74 72 79 28 52 74 72 65  tRtreeEntry(Rtre
39442 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
39443 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29  Cursor *pCursor)
39444 7b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65  {.  RtreeCell ce
39445 6c 6c 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20  ll;.  int ii;.. 
39446 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74   nodeGetCell(pRt
39447 72 65 65 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e  ree, pCursor->pN
39448 6f 64 65 2c 20 70 43 75 72 73 6f 72 2d 3e 69 43  ode, pCursor->iC
39449 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 66  ell, &cell);.  f
3944a 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 72  or(ii=0; ii<pCur
3944b 73 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74  sor->nConstraint
3944c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72  ; ii++){.    Rtr
3944d 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20  eeConstraint *p 
3944e 3d 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e  = &pCursor->aCon
3944f 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20  straint[ii];.   
39450 20 64 6f 75 62 6c 65 20 63 6f 6f 72 64 20 3d 20   double coord = 
39451 44 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f  DCOORD(cell.aCoo
39452 72 64 5b 70 2d 3e 69 43 6f 6f 72 64 5d 29 3b 0a  rd[p->iCoord]);.
39453 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
39454 20 61 73 73 65 72 74 28 70 2d 3e 6f 70 3d 3d 52   assert(p->op==R
39455 54 52 45 45 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70  TREE_LE || p->op
39456 3d 3d 52 54 52 45 45 5f 4c 54 20 7c 7c 20 70 2d  ==RTREE_LT || p-
39457 3e 6f 70 3d 3d 52 54 52 45 45 5f 47 45 20 0a 20  >op==RTREE_GE . 
39458 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d         || p->op=
39459 3d 52 54 52 45 45 5f 47 54 20 7c 7c 20 70 2d 3e  =RTREE_GT || p->
3945a 6f 70 3d 3d 52 54 52 45 45 5f 45 51 0a 20 20 20  op==RTREE_EQ.   
3945b 20 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20   );.    switch( 
3945c 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 63  p->op ){.      c
3945d 61 73 65 20 52 54 52 45 45 5f 4c 45 3a 20 72 65  ase RTREE_LE: re
3945e 73 20 3d 20 28 63 6f 6f 72 64 3c 3d 70 2d 3e 72  s = (coord<=p->r
3945f 56 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a 20  Value); break;. 
39460 20 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f       case RTREE_
39461 4c 54 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64  LT: res = (coord
39462 3c 70 2d 3e 72 56 61 6c 75 65 29 3b 20 20 62 72  <p->rValue);  br
39463 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
39464 52 54 52 45 45 5f 47 45 3a 20 72 65 73 20 3d 20  RTREE_GE: res = 
39465 28 63 6f 6f 72 64 3e 3d 70 2d 3e 72 56 61 6c 75  (coord>=p->rValu
39466 65 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  e); break;.     
39467 20 63 61 73 65 20 52 54 52 45 45 5f 47 54 3a 20   case RTREE_GT: 
39468 72 65 73 20 3d 20 28 63 6f 6f 72 64 3e 70 2d 3e  res = (coord>p->
39469 72 56 61 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b  rValue);  break;
3946a 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52 45  .      case RTRE
3946b 45 5f 45 51 3a 20 72 65 73 20 3d 20 28 63 6f 6f  E_EQ: res = (coo
3946c 72 64 3d 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20  rd==p->rValue); 
3946d 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
3946e 20 20 69 66 28 20 21 72 65 73 20 29 20 72 65 74    if( !res ) ret
3946f 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 72 65  urn 1;.  }..  re
39470 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
39471 20 43 75 72 73 6f 72 20 70 43 75 72 73 6f 72 20   Cursor pCursor 
39472 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73  currently points
39473 20 61 74 20 61 20 6e 6f 64 65 20 74 68 61 74 20   at a node that 
39474 68 65 61 64 73 20 61 20 73 75 62 2d 74 72 65 65  heads a sub-tree
39475 20 6f 66 0a 2a 2a 20 68 65 69 67 68 74 20 69 48   of.** height iH
39476 65 69 67 68 74 20 28 69 66 20 69 48 65 69 67 68  eight (if iHeigh
39477 74 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 6e  t==0, then the n
39478 6f 64 65 20 69 73 20 61 20 6c 65 61 66 29 2e 20  ode is a leaf). 
39479 44 65 73 63 65 6e 64 0a 2a 2a 20 74 6f 20 70 6f  Descend.** to po
3947a 69 6e 74 20 74 6f 20 74 68 65 20 6c 65 66 74 2d  int to the left-
3947b 6d 6f 73 74 20 63 65 6c 6c 20 6f 66 20 74 68 65  most cell of the
3947c 20 73 75 62 2d 74 72 65 65 20 74 68 61 74 20 6d   sub-tree that m
3947d 61 74 63 68 65 73 20 74 68 65 20 0a 2a 2a 20 63  atches the .** c
3947e 6f 6e 66 69 67 75 72 65 64 20 63 6f 6e 73 74 72  onfigured constr
3947f 61 69 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  aints..*/.static
39480 20 69 6e 74 20 64 65 73 63 65 6e 64 54 6f 43 65   int descendToCe
39481 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ll(.  Rtree *pRt
39482 72 65 65 2c 20 0a 20 20 52 74 72 65 65 43 75 72  ree, .  RtreeCur
39483 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 0a 20  sor *pCursor, . 
39484 20 69 6e 74 20 69 48 65 69 67 68 74 2c 0a 20 20   int iHeight,.  
39485 69 6e 74 20 2a 70 45 6f 66 20 20 20 20 20 20 20  int *pEof       
39486 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
39487 3a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69 66  : Set to true if
39488 20 63 61 6e 6e 6f 74 20 64 65 73 63 65 6e 64 20   cannot descend 
39489 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 73 45 6f  */.){.  int isEo
3948a 66 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69  f;.  int rc;.  i
3948b 6e 74 20 69 69 3b 0a 20 20 52 74 72 65 65 4e 6f  nt ii;.  RtreeNo
3948c 64 65 20 2a 70 43 68 69 6c 64 3b 0a 20 20 73 71  de *pChild;.  sq
3948d 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
3948e 69 64 3b 0a 0a 20 20 52 74 72 65 65 4e 6f 64 65  id;..  RtreeNode
3948f 20 2a 70 53 61 76 65 64 4e 6f 64 65 20 3d 20 70   *pSavedNode = p
39490 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 3b 0a 20  Cursor->pNode;. 
39491 20 69 6e 74 20 69 53 61 76 65 64 43 65 6c 6c 20   int iSavedCell 
39492 3d 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c  = pCursor->iCell
39493 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 48 65  ;..  assert( iHe
39494 69 67 68 74 3e 3d 30 20 29 3b 0a 0a 20 20 69 66  ight>=0 );..  if
39495 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b 0a  ( iHeight==0 ){.
39496 20 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73 74      isEof = test
39497 52 74 72 65 65 45 6e 74 72 79 28 70 52 74 72 65  RtreeEntry(pRtre
39498 65 2c 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  e, pCursor);.  }
39499 65 6c 73 65 7b 0a 20 20 20 20 69 73 45 6f 66 20  else{.    isEof 
3949a 3d 20 74 65 73 74 52 74 72 65 65 43 65 6c 6c 28  = testRtreeCell(
3949b 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 29  pRtree, pCursor)
3949c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 73 45 6f  ;.  }.  if( isEo
3949d 66 20 7c 7c 20 69 48 65 69 67 68 74 3d 3d 30 20  f || iHeight==0 
3949e 29 7b 0a 20 20 20 20 2a 70 45 6f 66 20 3d 20 69  ){.    *pEof = i
3949f 73 45 6f 66 3b 0a 20 20 20 20 72 65 74 75 72 6e  sEof;.    return
394a0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
394a1 0a 20 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65  .  iRowid = node
394a2 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
394a3 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c   pCursor->pNode,
394a4 20 70 43 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 29   pCursor->iCell)
394a5 3b 0a 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71  ;.  rc = nodeAcq
394a6 75 69 72 65 28 70 52 74 72 65 65 2c 20 69 52 6f  uire(pRtree, iRo
394a7 77 69 64 2c 20 70 43 75 72 73 6f 72 2d 3e 70 4e  wid, pCursor->pN
394a8 6f 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0a 20  ode, &pChild);. 
394a9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
394aa 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
394ab 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 6e 6f 64 65   rc;.  }..  node
394ac 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
394ad 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 29 3b  pCursor->pNode);
394ae 0a 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64  .  pCursor->pNod
394af 65 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 69 73  e = pChild;.  is
394b0 45 6f 66 20 3d 20 31 3b 0a 20 20 66 6f 72 28 69  Eof = 1;.  for(i
394b1 69 3d 30 3b 20 69 73 45 6f 66 20 26 26 20 69 69  i=0; isEof && ii
394b2 3c 4e 43 45 4c 4c 28 70 43 68 69 6c 64 29 3b 20  <NCELL(pChild); 
394b3 69 69 2b 2b 29 7b 0a 20 20 20 20 70 43 75 72 73  ii++){.    pCurs
394b4 6f 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 69 3b 0a  or->iCell = ii;.
394b5 20 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64      rc = descend
394b6 54 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  ToCell(pRtree, p
394b7 43 75 72 73 6f 72 2c 20 69 48 65 69 67 68 74 2d  Cursor, iHeight-
394b8 31 2c 20 26 69 73 45 6f 66 29 3b 0a 20 20 20 20  1, &isEof);.    
394b9 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
394ba 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
394bb 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
394bc 0a 20 20 69 66 28 20 69 73 45 6f 66 20 29 7b 0a  .  if( isEof ){.
394bd 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
394be 73 6f 72 2d 3e 70 4e 6f 64 65 3d 3d 70 43 68 69  sor->pNode==pChi
394bf 6c 64 20 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65  ld );.    nodeRe
394c0 66 65 72 65 6e 63 65 28 70 53 61 76 65 64 4e 6f  ference(pSavedNo
394c1 64 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 6c  de);.    nodeRel
394c2 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 68  ease(pRtree, pCh
394c3 69 6c 64 29 3b 0a 20 20 20 20 70 43 75 72 73 6f  ild);.    pCurso
394c4 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 53 61 76 65  r->pNode = pSave
394c5 64 4e 6f 64 65 3b 0a 20 20 20 20 70 43 75 72 73  dNode;.    pCurs
394c6 6f 72 2d 3e 69 43 65 6c 6c 20 3d 20 69 53 61 76  or->iCell = iSav
394c7 65 64 43 65 6c 6c 3b 0a 20 20 7d 0a 0a 20 20 2a  edCell;.  }..  *
394c8 70 45 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20 20  pEof = isEof;.  
394c9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
394ca 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 65 20 6f  ;.}../*.** One o
394cb 66 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 6e  f the cells in n
394cc 6f 64 65 20 70 4e 6f 64 65 20 69 73 20 67 75 61  ode pNode is gua
394cd 72 61 6e 74 65 65 64 20 74 6f 20 68 61 76 65 20  ranteed to have 
394ce 61 20 36 34 2d 62 69 74 20 0a 2a 2a 20 69 6e 74  a 64-bit .** int
394cf 65 67 65 72 20 76 61 6c 75 65 20 65 71 75 61 6c  eger value equal
394d0 20 74 6f 20 69 52 6f 77 69 64 2e 20 52 65 74 75   to iRowid. Retu
394d1 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  rn the index of 
394d2 74 68 69 73 20 63 65 6c 6c 2e 0a 2a 2f 0a 73 74  this cell..*/.st
394d3 61 74 69 63 20 69 6e 74 20 6e 6f 64 65 52 6f 77  atic int nodeRow
394d4 69 64 49 6e 64 65 78 28 52 74 72 65 65 20 2a 70  idIndex(Rtree *p
394d5 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
394d6 20 2a 70 4e 6f 64 65 2c 20 69 36 34 20 69 52 6f   *pNode, i64 iRo
394d7 77 69 64 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  wid){.  int ii;.
394d8 20 20 66 6f 72 28 69 69 3d 30 3b 20 6e 6f 64 65    for(ii=0; node
394d9 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
394da 20 70 4e 6f 64 65 2c 20 69 69 29 21 3d 69 52 6f   pNode, ii)!=iRo
394db 77 69 64 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  wid; ii++){.    
394dc 61 73 73 65 72 74 28 20 69 69 3c 28 4e 43 45 4c  assert( ii<(NCEL
394dd 4c 28 70 4e 6f 64 65 29 2d 31 29 20 29 3b 0a 20  L(pNode)-1) );. 
394de 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 69 3b 0a   }.  return ii;.
394df 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
394e0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
394e1 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e 67   cell containing
394e2 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 6e 6f   a pointer to no
394e3 64 65 20 70 4e 6f 64 65 0a 2a 2a 20 69 6e 20 69  de pNode.** in i
394e4 74 73 20 70 61 72 65 6e 74 2e 20 49 66 20 70 4e  ts parent. If pN
394e5 6f 64 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20  ode is the root 
394e6 6e 6f 64 65 2c 20 72 65 74 75 72 6e 20 2d 31 2e  node, return -1.
394e7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  .*/.static int n
394e8 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 52  odeParentIndex(R
394e9 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74  tree *pRtree, Rt
394ea 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b  reeNode *pNode){
394eb 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50  .  RtreeNode *pP
394ec 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70  arent = pNode->p
394ed 50 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70 50  Parent;.  if( pP
394ee 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 72 65 74  arent ){.    ret
394ef 75 72 6e 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64  urn nodeRowidInd
394f0 65 78 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  ex(pRtree, pPare
394f1 6e 74 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  nt, pNode->iNode
394f2 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
394f3 2d 31 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74  -1;.}../* .** Rt
394f4 72 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ree virtual tabl
394f5 65 20 6d 6f 64 75 6c 65 20 78 4e 65 78 74 20 6d  e module xNext m
394f6 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
394f7 20 69 6e 74 20 72 74 72 65 65 4e 65 78 74 28 73   int rtreeNext(s
394f8 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
394f9 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 29  or *pVtabCursor)
394fa 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65  {.  Rtree *pRtre
394fb 65 20 3d 20 28 52 74 72 65 65 20 2a 29 28 70 56  e = (Rtree *)(pV
394fc 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
394fd 29 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f 72  );.  RtreeCursor
394fe 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65 43   *pCsr = (RtreeC
394ff 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75 72  ursor *)pVtabCur
39500 73 6f 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  sor;.  int rc = 
39501 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66  SQLITE_OK;..  if
39502 28 20 70 43 73 72 2d 3e 69 53 74 72 61 74 65 67  ( pCsr->iStrateg
39503 79 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54  y==1 ){.    /* T
39504 68 69 73 20 22 73 63 61 6e 22 20 69 73 20 61 20  his "scan" is a 
39505 64 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20 62 79  direct lookup by
39506 20 72 6f 77 69 64 2e 20 54 68 65 72 65 20 69 73   rowid. There is
39507 20 6e 6f 20 6e 65 78 74 20 65 6e 74 72 79 2e 20   no next entry. 
39508 2a 2f 0a 20 20 20 20 6e 6f 64 65 52 65 6c 65 61  */.    nodeRelea
39509 73 65 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d  se(pRtree, pCsr-
3950a 3e 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 43 73  >pNode);.    pCs
3950b 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20  r->pNode = 0;.  
3950c 7d 0a 0a 20 20 65 6c 73 65 20 69 66 28 20 70 43  }..  else if( pC
3950d 73 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20 20 20  sr->pNode ){.   
3950e 20 2f 2a 20 4d 6f 76 65 20 74 6f 20 74 68 65 20   /* Move to the 
3950f 6e 65 78 74 20 65 6e 74 72 79 20 74 68 61 74 20  next entry that 
39510 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f 6e 66  matches the conf
39511 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  igured constrain
39512 74 73 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ts. */.    int i
39513 48 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 20 20  Height = 0;.    
39514 77 68 69 6c 65 28 20 70 43 73 72 2d 3e 70 4e 6f  while( pCsr->pNo
39515 64 65 20 29 7b 0a 20 20 20 20 20 20 52 74 72 65  de ){.      Rtre
39516 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 20 3d 20 70  eNode *pNode = p
39517 43 73 72 2d 3e 70 4e 6f 64 65 3b 0a 20 20 20 20  Csr->pNode;.    
39518 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
39519 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ELL(pNode);.    
3951a 20 20 66 6f 72 28 70 43 73 72 2d 3e 69 43 65 6c    for(pCsr->iCel
3951b 6c 2b 2b 3b 20 70 43 73 72 2d 3e 69 43 65 6c 6c  l++; pCsr->iCell
3951c 3c 6e 43 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43  <nCell; pCsr->iC
3951d 65 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ell++){.        
3951e 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20 20 20 20  int isEof;.     
3951f 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e 64 54     rc = descendT
39520 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  oCell(pRtree, pC
39521 73 72 2c 20 69 48 65 69 67 68 74 2c 20 26 69 73  sr, iHeight, &is
39522 45 6f 66 29 3b 0a 20 20 20 20 20 20 20 20 69 66  Eof);.        if
39523 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
39524 7c 7c 20 21 69 73 45 6f 66 20 29 7b 0a 20 20 20  || !isEof ){.   
39525 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
39526 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
39527 20 20 7d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e    }.      pCsr->
39528 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 2d 3e 70  pNode = pNode->p
39529 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 70 43  Parent;.      pC
3952a 73 72 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65  sr->iCell = node
3952b 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72  ParentIndex(pRtr
3952c 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ee, pNode);.    
3952d 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
3952e 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20  pCsr->pNode);.  
3952f 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
39530 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
39531 20 20 20 20 20 20 69 48 65 69 67 68 74 2b 2b 3b        iHeight++;
39532 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
39533 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a  turn rc;.}../* .
39534 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c  ** Rtree virtual
39535 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 52   table module xR
39536 6f 77 69 64 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  owid method..*/.
39537 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
39538 52 6f 77 69 64 28 73 71 6c 69 74 65 33 5f 76 74  Rowid(sqlite3_vt
39539 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74 61 62  ab_cursor *pVtab
3953a 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69  Cursor, sqlite_i
3953b 6e 74 36 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20  nt64 *pRowid){. 
3953c 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d   Rtree *pRtree =
3953d 20 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 43   (Rtree *)pVtabC
3953e 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20  ursor->pVtab;.  
3953f 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73  RtreeCursor *pCs
39540 72 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72  r = (RtreeCursor
39541 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a   *)pVtabCursor;.
39542 0a 20 20 61 73 73 65 72 74 28 70 43 73 72 2d 3e  .  assert(pCsr->
39543 70 4e 6f 64 65 29 3b 0a 20 20 2a 70 52 6f 77 69  pNode);.  *pRowi
39544 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64  d = nodeGetRowid
39545 28 70 52 74 72 65 65 2c 20 70 43 73 72 2d 3e 70  (pRtree, pCsr->p
39546 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c  Node, pCsr->iCel
39547 6c 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  l);..  return SQ
39548 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a  LITE_OK;.}../* .
39549 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c  ** Rtree virtual
3954a 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43   table module xC
3954b 6f 6c 75 6d 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f  olumn method..*/
3954c 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
3954d 65 43 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f  eColumn(sqlite3_
3954e 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 63 75 72  vtab_cursor *cur
3954f 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  , sqlite3_contex
39550 74 20 2a 63 74 78 2c 20 69 6e 74 20 69 29 7b 0a  t *ctx, int i){.
39551 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20    Rtree *pRtree 
39552 3d 20 28 52 74 72 65 65 20 2a 29 63 75 72 2d 3e  = (Rtree *)cur->
39553 70 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75  pVtab;.  RtreeCu
39554 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74  rsor *pCsr = (Rt
39555 72 65 65 43 75 72 73 6f 72 20 2a 29 63 75 72 3b  reeCursor *)cur;
39556 0a 0a 20 20 69 66 28 20 69 3d 3d 30 20 29 7b 0a  ..  if( i==0 ){.
39557 20 20 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d      i64 iRowid =
39558 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52   nodeGetRowid(pR
39559 74 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64  tree, pCsr->pNod
3955a 65 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 29 3b  e, pCsr->iCell);
3955b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3955c 75 6c 74 5f 69 6e 74 36 34 28 63 74 78 2c 20 69  ult_int64(ctx, i
3955d 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b  Rowid);.  }else{
3955e 0a 20 20 20 20 52 74 72 65 65 43 6f 6f 72 64 20  .    RtreeCoord 
3955f 63 3b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 6f  c;.    nodeGetCo
39560 6f 72 64 28 70 52 74 72 65 65 2c 20 70 43 73 72  ord(pRtree, pCsr
39561 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72 2d 3e 69  ->pNode, pCsr->i
39562 43 65 6c 6c 2c 20 69 2d 31 2c 20 26 63 29 3b 0a  Cell, i-1, &c);.
39563 20 20 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e      if( pRtree->
39564 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45  eCoordType==RTRE
39565 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29  E_COORD_REAL32 )
39566 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
39567 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 74  result_double(ct
39568 78 2c 20 63 2e 66 29 3b 0a 20 20 20 20 7d 65 6c  x, c.f);.    }el
39569 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
3956a 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64  ( pRtree->eCoord
3956b 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52  Type==RTREE_COOR
3956c 44 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 20  D_INT32 );.     
3956d 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
3956e 69 6e 74 28 63 74 78 2c 20 63 2e 69 29 3b 0a 20  int(ctx, c.i);. 
3956f 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
39570 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
39571 0a 2f 2a 20 0a 2a 2a 20 55 73 65 20 6e 6f 64 65  ./* .** Use node
39572 41 63 71 75 69 72 65 28 29 20 74 6f 20 6f 62 74  Acquire() to obt
39573 61 69 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64  ain the leaf nod
39574 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
39575 20 72 65 63 6f 72 64 20 77 69 74 68 20 0a 2a 2a   record with .**
39576 20 72 6f 77 69 64 20 69 52 6f 77 69 64 2e 20 49   rowid iRowid. I
39577 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 73 65  f successful, se
39578 74 20 2a 70 70 4c 65 61 66 20 74 6f 20 70 6f 69  t *ppLeaf to poi
39579 6e 74 20 74 6f 20 74 68 65 20 6e 6f 64 65 20 61  nt to the node a
3957a 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c  nd.** return SQL
3957b 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68 65 72 65  ITE_OK. If there
3957c 20 69 73 20 6e 6f 20 73 75 63 68 20 72 65 63 6f   is no such reco
3957d 72 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c  rd in the table,
3957e 20 73 65 74 0a 2a 2a 20 2a 70 70 4c 65 61 66 20   set.** *ppLeaf 
3957f 74 6f 20 30 20 61 6e 64 20 72 65 74 75 72 6e 20  to 0 and return 
39580 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 61 6e  SQLITE_OK. If an
39581 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 73   error occurs, s
39582 65 74 20 2a 70 70 4c 65 61 66 0a 2a 2a 20 74 6f  et *ppLeaf.** to
39583 20 7a 65 72 6f 20 61 6e 64 20 72 65 74 75 72 6e   zero and return
39584 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
39585 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   code..*/.static
39586 20 69 6e 74 20 66 69 6e 64 4c 65 61 66 4e 6f 64   int findLeafNod
39587 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
39588 20 69 36 34 20 69 52 6f 77 69 64 2c 20 52 74 72   i64 iRowid, Rtr
39589 65 65 4e 6f 64 65 20 2a 2a 70 70 4c 65 61 66 29  eeNode **ppLeaf)
3958a 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 2a 70  {.  int rc;.  *p
3958b 70 4c 65 61 66 20 3d 20 30 3b 0a 20 20 73 71 6c  pLeaf = 0;.  sql
3958c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
3958d 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
3958e 69 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a  id, 1, iRowid);.
3958f 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74    if( sqlite3_st
39590 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ep(pRtree->pRead
39591 52 6f 77 69 64 29 3d 3d 53 51 4c 49 54 45 5f 52  Rowid)==SQLITE_R
39592 4f 57 20 29 7b 0a 20 20 20 20 69 36 34 20 69 4e  OW ){.    i64 iN
39593 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ode = sqlite3_co
39594 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72 65  lumn_int64(pRtre
39595 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 30  e->pReadRowid, 0
39596 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65  );.    rc = node
39597 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20  Acquire(pRtree, 
39598 69 4e 6f 64 65 2c 20 30 2c 20 70 70 4c 65 61 66  iNode, 0, ppLeaf
39599 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
3959a 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
3959b 61 64 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73  adRowid);.  }els
3959c 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e{.    rc = sqli
3959d 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
3959e 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a 20  ->pReadRowid);. 
3959f 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
395a0 7d 0a 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65  }.../* .** Rtree
395a1 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
395a2 6f 64 75 6c 65 20 78 46 69 6c 74 65 72 20 6d 65  odule xFilter me
395a3 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
395a4 69 6e 74 20 72 74 72 65 65 46 69 6c 74 65 72 28  int rtreeFilter(
395a5 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
395a6 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
395a7 73 6f 72 2c 20 0a 20 20 69 6e 74 20 69 64 78 4e  sor, .  int idxN
395a8 75 6d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  um, const char *
395a9 69 64 78 53 74 72 2c 0a 20 20 69 6e 74 20 61 72  idxStr,.  int ar
395aa 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
395ab 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 52 74  e **argv.){.  Rt
395ac 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
395ad 74 72 65 65 20 2a 29 70 56 74 61 62 43 75 72 73  tree *)pVtabCurs
395ae 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 52 74 72  or->pVtab;.  Rtr
395af 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
395b0 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
395b1 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 0a 20 20  pVtabCursor;..  
395b2 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f 74  RtreeNode *pRoot
395b3 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a   = 0;.  int ii;.
395b4 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
395b5 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65 65 52 65  E_OK;..  rtreeRe
395b6 66 65 72 65 6e 63 65 28 70 52 74 72 65 65 29 3b  ference(pRtree);
395b7 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
395b8 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  (pCsr->aConstrai
395b9 6e 74 29 3b 0a 20 20 70 43 73 72 2d 3e 61 43 6f  nt);.  pCsr->aCo
395ba 6e 73 74 72 61 69 6e 74 20 3d 20 30 3b 0a 20 20  nstraint = 0;.  
395bb 70 43 73 72 2d 3e 69 53 74 72 61 74 65 67 79 20  pCsr->iStrategy 
395bc 3d 20 69 64 78 4e 75 6d 3b 0a 0a 20 20 69 66 28  = idxNum;..  if(
395bd 20 69 64 78 4e 75 6d 3d 3d 31 20 29 7b 0a 20 20   idxNum==1 ){.  
395be 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
395bf 65 20 2d 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f  e - lookup by ro
395c0 77 69 64 2e 20 2a 2f 0a 20 20 20 20 52 74 72 65  wid. */.    Rtre
395c1 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b 20 20 20  eNode *pLeaf;   
395c2 20 20 20 20 20 2f 2a 20 4c 65 61 66 20 6f 6e 20       /* Leaf on 
395c3 77 68 69 63 68 20 74 68 65 20 72 65 71 75 69 72  which the requir
395c4 65 64 20 63 65 6c 6c 20 72 65 73 69 64 65 73 20  ed cell resides 
395c5 2a 2f 0a 20 20 20 20 69 36 34 20 69 52 6f 77 69  */.    i64 iRowi
395c6 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  d = sqlite3_valu
395c7 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29  e_int64(argv[0])
395c8 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c  ;.    rc = findL
395c9 65 61 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  eafNode(pRtree, 
395ca 69 52 6f 77 69 64 2c 20 26 70 4c 65 61 66 29 3b  iRowid, &pLeaf);
395cb 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65  .    pCsr->pNode
395cc 20 3d 20 70 4c 65 61 66 3b 20 0a 20 20 20 20 69   = pLeaf; .    i
395cd 66 28 20 70 4c 65 61 66 20 26 26 20 72 63 3d 3d  f( pLeaf && rc==
395ce 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
395cf 20 20 20 70 43 73 72 2d 3e 69 43 65 6c 6c 20 3d     pCsr->iCell =
395d0 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78 28   nodeRowidIndex(
395d1 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20 69  pRtree, pLeaf, i
395d2 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  Rowid);.    }.  
395d3 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 6f  }else{.    /* No
395d4 72 6d 61 6c 20 63 61 73 65 20 2d 20 72 2d 74 72  rmal case - r-tr
395d5 65 65 20 73 63 61 6e 2e 20 53 65 74 20 75 70 20  ee scan. Set up 
395d6 74 68 65 20 52 74 72 65 65 43 75 72 73 6f 72 2e  the RtreeCursor.
395d7 61 43 6f 6e 73 74 72 61 69 6e 74 20 61 72 72 61  aConstraint arra
395d8 79 20 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74  y .    ** with t
395d9 68 65 20 63 6f 6e 66 69 67 75 72 65 64 20 63 6f  he configured co
395da 6e 73 74 72 61 69 6e 74 73 2e 20 0a 20 20 20 20  nstraints. .    
395db 2a 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63 3e  */.    if( argc>
395dc 30 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72 2d  0 ){.      pCsr-
395dd 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 73  >aConstraint = s
395de 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
395df 7a 65 6f 66 28 52 74 72 65 65 43 6f 6e 73 74 72  zeof(RtreeConstr
395e0 61 69 6e 74 29 2a 61 72 67 63 29 3b 0a 20 20 20  aint)*argc);.   
395e1 20 20 20 70 43 73 72 2d 3e 6e 43 6f 6e 73 74 72     pCsr->nConstr
395e2 61 69 6e 74 20 3d 20 61 72 67 63 3b 0a 20 20 20  aint = argc;.   
395e3 20 20 20 69 66 28 20 21 70 43 73 72 2d 3e 61 43     if( !pCsr->aC
395e4 6f 6e 73 74 72 61 69 6e 74 20 29 7b 0a 20 20 20  onstraint ){.   
395e5 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
395e6 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65  _NOMEM;.      }e
395e7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73  lse{.        ass
395e8 65 72 74 28 20 28 69 64 78 53 74 72 3d 3d 30 20  ert( (idxStr==0 
395e9 26 26 20 61 72 67 63 3d 3d 30 29 20 7c 7c 20 73  && argc==0) || s
395ea 74 72 6c 65 6e 28 69 64 78 53 74 72 29 3d 3d 61  trlen(idxStr)==a
395eb 72 67 63 2a 32 20 29 3b 0a 20 20 20 20 20 20 20  rgc*2 );.       
395ec 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 61 72   for(ii=0; ii<ar
395ed 67 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  gc; ii++){.     
395ee 20 20 20 20 20 52 74 72 65 65 43 6f 6e 73 74 72       RtreeConstr
395ef 61 69 6e 74 20 2a 70 20 3d 20 26 70 43 73 72 2d  aint *p = &pCsr-
395f0 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d  >aConstraint[ii]
395f1 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 6f  ;.          p->o
395f2 70 20 3d 20 69 64 78 53 74 72 5b 69 69 2a 32 5d  p = idxStr[ii*2]
395f3 3b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69  ;.          p->i
395f4 43 6f 6f 72 64 20 3d 20 69 64 78 53 74 72 5b 69  Coord = idxStr[i
395f5 69 2a 32 2b 31 5d 2d 27 61 27 3b 0a 20 20 20 20  i*2+1]-'a';.    
395f6 20 20 20 20 20 20 70 2d 3e 72 56 61 6c 75 65 20        p->rValue 
395f7 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
395f8 64 6f 75 62 6c 65 28 61 72 67 76 5b 69 69 5d 29  double(argv[ii])
395f9 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
395fa 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
395fb 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
395fc 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72  OK ){.      pCsr
395fd 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20  ->pNode = 0;.   
395fe 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75     rc = nodeAcqu
395ff 69 72 65 28 70 52 74 72 65 65 2c 20 31 2c 20 30  ire(pRtree, 1, 0
39600 2c 20 26 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d  , &pRoot);.    }
39601 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
39602 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
39603 69 6e 74 20 69 73 45 6f 66 20 3d 20 31 3b 0a 20  int isEof = 1;. 
39604 20 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d       int nCell =
39605 20 4e 43 45 4c 4c 28 70 52 6f 6f 74 29 3b 0a 20   NCELL(pRoot);. 
39606 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65       pCsr->pNode
39607 20 3d 20 70 52 6f 6f 74 3b 0a 20 20 20 20 20 20   = pRoot;.      
39608 66 6f 72 28 70 43 73 72 2d 3e 69 43 65 6c 6c 3d  for(pCsr->iCell=
39609 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
3960a 20 26 26 20 70 43 73 72 2d 3e 69 43 65 6c 6c 3c   && pCsr->iCell<
3960b 6e 43 65 6c 6c 3b 20 70 43 73 72 2d 3e 69 43 65  nCell; pCsr->iCe
3960c 6c 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61  ll++){.        a
3960d 73 73 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f  ssert( pCsr->pNo
3960e 64 65 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20  de==pRoot );.   
3960f 20 20 20 20 20 72 63 20 3d 20 64 65 73 63 65 6e       rc = descen
39610 64 54 6f 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  dToCell(pRtree, 
39611 70 43 73 72 2c 20 70 52 74 72 65 65 2d 3e 69 44  pCsr, pRtree->iD
39612 65 70 74 68 2c 20 26 69 73 45 6f 66 29 3b 0a 20  epth, &isEof);. 
39613 20 20 20 20 20 20 20 69 66 28 20 21 69 73 45 6f         if( !isEo
39614 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  f ){.          b
39615 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
39616 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
39617 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
39618 26 26 20 69 73 45 6f 66 20 29 7b 0a 20 20 20 20  && isEof ){.    
39619 20 20 20 20 61 73 73 65 72 74 28 20 70 43 73 72      assert( pCsr
3961a 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f 6f 74 20 29  ->pNode==pRoot )
3961b 3b 0a 20 20 20 20 20 20 20 20 6e 6f 64 65 52 65  ;.        nodeRe
3961c 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70 52  lease(pRtree, pR
3961d 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20 20 70 43  oot);.        pC
3961e 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 30 3b 0a 20  sr->pNode = 0;. 
3961f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
39620 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
39621 4f 4b 20 7c 7c 20 21 70 43 73 72 2d 3e 70 4e 6f  OK || !pCsr->pNo
39622 64 65 20 7c 7c 20 70 43 73 72 2d 3e 69 43 65 6c  de || pCsr->iCel
39623 6c 3c 4e 43 45 4c 4c 28 70 43 73 72 2d 3e 70 4e  l<NCELL(pCsr->pN
39624 6f 64 65 29 20 29 3b 0a 20 20 20 20 7d 0a 20 20  ode) );.    }.  
39625 7d 0a 0a 20 20 72 74 72 65 65 52 65 6c 65 61 73  }..  rtreeReleas
39626 65 28 70 52 74 72 65 65 29 3b 0a 20 20 72 65 74  e(pRtree);.  ret
39627 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
39628 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20 74   Rtree virtual t
39629 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 42 65 73  able module xBes
3962a 74 49 6e 64 65 78 20 6d 65 74 68 6f 64 2e 20 54  tIndex method. T
3962b 68 65 72 65 20 61 72 65 20 74 68 72 65 65 0a 2a  here are three.*
3962c 2a 20 74 61 62 6c 65 20 73 63 61 6e 20 73 74 72  * table scan str
3962d 61 74 65 67 69 65 73 20 74 6f 20 63 68 6f 6f 73  ategies to choos
3962e 65 20 66 72 6f 6d 20 28 69 6e 20 6f 72 64 65 72  e from (in order
3962f 20 66 72 6f 6d 20 6d 6f 73 74 20 74 6f 20 0a 2a   from most to .*
39630 2a 20 6c 65 61 73 74 20 64 65 73 69 72 61 62 6c  * least desirabl
39631 65 29 3a 0a 2a 2a 0a 2a 2a 20 20 20 69 64 78 4e  e):.**.**   idxN
39632 75 6d 20 20 20 20 20 69 64 78 53 74 72 20 20 20  um     idxStr   
39633 20 20 20 20 20 53 74 72 61 74 65 67 79 0a 2a 2a       Strategy.**
39634 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
39635 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39636 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39637 2d 2d 2d 0a 2a 2a 20 20 20 20 20 31 20 20 20 20  ---.**     1    
39638 20 20 20 20 55 6e 75 73 65 64 20 20 20 20 20 20      Unused      
39639 20 20 44 69 72 65 63 74 20 6c 6f 6f 6b 75 70 20    Direct lookup 
3963a 62 79 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20  by rowid..**    
3963b 20 32 20 20 20 20 20 20 20 20 53 65 65 20 62 65   2        See be
3963c 6c 6f 77 20 20 20 20 20 52 2d 74 72 65 65 20 71  low     R-tree q
3963d 75 65 72 79 2e 0a 2a 2a 20 20 20 20 20 33 20 20  uery..**     3  
3963e 20 20 20 20 20 20 55 6e 75 73 65 64 20 20 20 20        Unused    
3963f 20 20 20 20 46 75 6c 6c 20 74 61 62 6c 65 20 73      Full table s
39640 63 61 6e 2e 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d  can..**   ------
39641 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39642 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39643 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a  ----------.**.**
39644 20 49 66 20 73 74 72 61 74 65 67 79 20 31 20 6f   If strategy 1 o
39645 72 20 33 20 69 73 20 75 73 65 64 2c 20 74 68 65  r 3 is used, the
39646 6e 20 69 64 78 53 74 72 20 69 73 20 6e 6f 74 20  n idxStr is not 
39647 6d 65 61 6e 69 6e 67 66 75 6c 2e 20 49 66 20 73  meaningful. If s
39648 74 72 61 74 65 67 79 0a 2a 2a 20 32 20 69 73 20  trategy.** 2 is 
39649 75 73 65 64 2c 20 69 64 78 53 74 72 20 69 73 20  used, idxStr is 
3964a 66 6f 72 6d 61 74 74 65 64 20 74 6f 20 63 6f 6e  formatted to con
3964b 74 61 69 6e 20 32 20 62 79 74 65 73 20 66 6f 72  tain 2 bytes for
3964c 20 65 61 63 68 20 0a 2a 2a 20 63 6f 6e 73 74 72   each .** constr
3964d 61 69 6e 74 20 75 73 65 64 2e 20 54 68 65 20 66  aint used. The f
3964e 69 72 73 74 20 74 77 6f 20 62 79 74 65 73 20 6f  irst two bytes o
3964f 66 20 69 64 78 53 74 72 20 63 6f 72 72 65 73 70  f idxStr corresp
39650 6f 6e 64 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63  ond to .** the c
39651 6f 6e 73 74 72 61 69 6e 74 20 69 6e 20 73 71 6c  onstraint in sql
39652 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 2e  ite3_index_info.
39653 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
39654 5b 5d 20 77 69 74 68 0a 2a 2a 20 28 61 72 67 76  [] with.** (argv
39655 49 6e 64 65 78 3d 3d 31 29 20 65 74 63 2e 0a 2a  Index==1) etc..*
39656 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6f  *.** The first o
39657 66 20 65 61 63 68 20 70 61 69 72 20 6f 66 20 62  f each pair of b
39658 79 74 65 73 20 69 6e 20 69 64 78 53 74 72 20 69  ytes in idxStr i
39659 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 63 6f  dentifies the co
3965a 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 6f 70 65 72  nstraint.** oper
3965b 61 74 6f 72 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  ator as follows:
3965c 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 65 72 61 74 6f  .**.**   Operato
3965d 72 20 20 20 20 42 79 74 65 20 56 61 6c 75 65 0a  r    Byte Value.
3965e 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  **   -----------
3965f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
39660 20 20 20 20 3d 20 20 20 20 20 20 20 20 30 78 34      =        0x4
39661 31 20 28 27 41 27 29 0a 2a 2a 20 20 20 20 20 3c  1 ('A').**     <
39662 3d 20 20 20 20 20 20 20 20 30 78 34 32 20 28 27  =        0x42 ('
39663 42 27 29 0a 2a 2a 20 20 20 20 20 20 3c 20 20 20  B').**      <   
39664 20 20 20 20 20 30 78 34 33 20 28 27 43 27 29 0a       0x43 ('C').
39665 2a 2a 20 20 20 20 20 3e 3d 20 20 20 20 20 20 20  **     >=       
39666 20 30 78 34 34 20 28 27 44 27 29 0a 2a 2a 20 20   0x44 ('D').**  
39667 20 20 20 20 3e 20 20 20 20 20 20 20 20 30 78 34      >        0x4
39668 35 20 28 27 45 27 29 0a 2a 2a 20 20 20 2d 2d 2d  5 ('E').**   ---
39669 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
3966a 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  ---.**.** The se
3966b 63 6f 6e 64 20 6f 66 20 65 61 63 68 20 70 61 69  cond of each pai
3966c 72 20 6f 66 20 62 79 74 65 73 20 69 64 65 6e 74  r of bytes ident
3966d 69 66 69 65 73 20 74 68 65 20 63 6f 6f 72 64 69  ifies the coordi
3966e 6e 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74  nate column.** t
3966f 6f 20 77 68 69 63 68 20 74 68 65 20 63 6f 6e 73  o which the cons
39670 74 72 61 69 6e 74 20 61 70 70 6c 69 65 73 2e 20  traint applies. 
39671 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f 6f  The leftmost coo
39672 72 64 69 6e 61 74 65 20 63 6f 6c 75 6d 6e 0a 2a  rdinate column.*
39673 2a 20 69 73 20 27 61 27 2c 20 74 68 65 20 73 65  * is 'a', the se
39674 63 6f 6e 64 20 66 72 6f 6d 20 74 68 65 20 6c 65  cond from the le
39675 66 74 20 27 62 27 20 65 74 63 2e 0a 2a 2f 0a 73  ft 'b' etc..*/.s
39676 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 42  tatic int rtreeB
39677 65 73 74 49 6e 64 65 78 28 73 71 6c 69 74 65 33  estIndex(sqlite3
39678 5f 76 74 61 62 20 2a 74 61 62 2c 20 73 71 6c 69  _vtab *tab, sqli
39679 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
3967a 70 49 64 78 49 6e 66 6f 29 7b 0a 20 20 69 6e 74  pIdxInfo){.  int
3967b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
3967c 0a 20 20 69 6e 74 20 69 69 2c 20 63 43 6f 6c 3b  .  int ii, cCol;
3967d 0a 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30  ..  int iIdx = 0
3967e 3b 0a 20 20 63 68 61 72 20 7a 49 64 78 53 74 72  ;.  char zIdxStr
3967f 5b 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e  [RTREE_MAX_DIMEN
39680 53 49 4f 4e 53 2a 38 2b 31 5d 3b 0a 20 20 6d 65  SIONS*8+1];.  me
39681 6d 73 65 74 28 7a 49 64 78 53 74 72 2c 20 30 2c  mset(zIdxStr, 0,
39682 20 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72 29   sizeof(zIdxStr)
39683 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 49  );..  assert( pI
39684 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 3d 3d  dxInfo->idxStr==
39685 30 20 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  0 );.  for(ii=0;
39686 20 69 69 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43   ii<pIdxInfo->nC
39687 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29  onstraint; ii++)
39688 7b 0a 20 20 20 20 73 74 72 75 63 74 20 73 71 6c  {.    struct sql
39689 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74  ite3_index_const
3968a 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 49 64 78  raint *p = &pIdx
3968b 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
3968c 74 5b 69 69 5d 3b 0a 0a 20 20 20 20 69 66 28 20  t[ii];..    if( 
3968d 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e  p->usable && p->
3968e 69 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 70 2d  iColumn==0 && p-
3968f 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  >op==SQLITE_INDE
39690 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51 20  X_CONSTRAINT_EQ 
39691 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 65 20 68  ){.      /* We h
39692 61 76 65 20 61 6e 20 65 71 75 61 6c 69 74 79 20  ave an equality 
39693 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74 68  constraint on th
39694 65 20 72 6f 77 69 64 2e 20 55 73 65 20 73 74 72  e rowid. Use str
39695 61 74 65 67 79 20 31 2e 20 2a 2f 0a 20 20 20 20  ategy 1. */.    
39696 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
39697 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 69 69 3b  for(jj=0; jj<ii;
39698 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20   jj++){.        
39699 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
3969a 72 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 61  raintUsage[jj].a
3969b 72 67 76 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20  rgvIndex = 0;.  
3969c 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
3969d 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
3969e 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 30 3b 0a 20  [jj].omit = 0;. 
3969f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49 64       }.      pId
396a0 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20  xInfo->idxNum = 
396a1 31 3b 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66  1;.      pIdxInf
396a2 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
396a3 61 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e 64 65  age[ii].argvInde
396a4 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 49 64  x = 1;.      pId
396a5 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
396a6 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d 69 74  ntUsage[jj].omit
396a7 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   = 1;..      /* 
396a8 54 68 69 73 20 73 74 72 61 74 65 67 79 20 69 6e  This strategy in
396a9 76 6f 6c 76 65 73 20 61 20 74 77 6f 20 72 6f 77  volves a two row
396aa 69 64 20 6c 6f 6f 6b 75 70 73 20 6f 6e 20 61 6e  id lookups on an
396ab 20 42 2d 54 72 65 65 20 73 74 72 75 63 74 75 72   B-Tree structur
396ac 65 73 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  es.      ** and 
396ad 74 68 65 6e 20 61 20 6c 69 6e 65 61 72 20 73 65  then a linear se
396ae 61 72 63 68 20 6f 66 20 61 6e 20 52 2d 54 72 65  arch of an R-Tre
396af 65 20 6e 6f 64 65 2e 20 54 68 69 73 20 73 68 6f  e node. This sho
396b0 75 6c 64 20 62 65 20 0a 20 20 20 20 20 20 2a 2a  uld be .      **
396b1 20 63 6f 6e 73 69 64 65 72 65 64 20 61 6c 6d 6f   considered almo
396b2 73 74 20 61 73 20 71 75 69 63 6b 20 61 73 20 61  st as quick as a
396b3 20 64 69 72 65 63 74 20 72 6f 77 69 64 20 6c 6f   direct rowid lo
396b4 6f 6b 75 70 20 28 66 6f 72 20 77 68 69 63 68 20  okup (for which 
396b5 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  .      ** sqlite
396b6 20 75 73 65 73 20 61 6e 20 69 6e 74 65 72 6e 61   uses an interna
396b7 6c 20 63 6f 73 74 20 6f 66 20 30 2e 30 29 2e 0a  l cost of 0.0)..
396b8 20 20 20 20 20 20 2a 2f 20 0a 20 20 20 20 20 20        */ .      
396b9 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61  pIdxInfo->estima
396ba 74 65 64 43 6f 73 74 20 3d 20 31 30 2e 30 3b 0a  tedCost = 10.0;.
396bb 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
396bc 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20  ITE_OK;.    }.. 
396bd 20 20 20 69 66 28 20 70 2d 3e 75 73 61 62 6c 65     if( p->usable
396be 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3e 30   && p->iColumn>0
396bf 20 29 7b 0a 20 20 20 20 20 20 75 38 20 6f 70 20   ){.      u8 op 
396c0 3d 20 30 3b 0a 20 20 20 20 20 20 73 77 69 74 63  = 0;.      switc
396c1 68 28 20 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  h( p->op ){.    
396c2 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
396c3 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54  INDEX_CONSTRAINT
396c4 5f 45 51 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f  _EQ: op = RTREE_
396c5 45 51 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  EQ; break;.     
396c6 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49     case SQLITE_I
396c7 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
396c8 47 54 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 47  GT: op = RTREE_G
396c9 54 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  T; break;.      
396ca 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
396cb 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c  DEX_CONSTRAINT_L
396cc 45 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 45  E: op = RTREE_LE
396cd 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ; break;.       
396ce 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44   case SQLITE_IND
396cf 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54  EX_CONSTRAINT_LT
396d0 3a 20 6f 70 20 3d 20 52 54 52 45 45 5f 4c 54 3b  : op = RTREE_LT;
396d1 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
396d2 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 44 45  case SQLITE_INDE
396d3 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 45 3a  X_CONSTRAINT_GE:
396d4 20 6f 70 20 3d 20 52 54 52 45 45 5f 47 45 3b 20   op = RTREE_GE; 
396d5 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
396d6 20 20 20 20 20 69 66 28 20 6f 70 20 29 7b 0a 20       if( op ){. 
396d7 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73         /* Make s
396d8 75 72 65 20 74 68 69 73 20 70 61 72 74 69 63 75  ure this particu
396d9 6c 61 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 68  lar constraint h
396da 61 73 20 6e 6f 74 20 62 65 65 6e 20 75 73 65 64  as not been used
396db 20 62 65 66 6f 72 65 2e 0a 20 20 20 20 20 20 20   before..       
396dc 20 2a 2a 20 49 66 20 69 74 20 68 61 73 20 62 65   ** If it has be
396dd 65 6e 20 75 73 65 64 20 62 65 66 6f 72 65 2c 20  en used before, 
396de 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 20 20 20  ignore it..     
396df 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
396e0 20 41 20 3c 3d 20 6f 72 20 3c 20 63 61 6e 20 62   A <= or < can b
396e1 65 20 75 73 65 64 20 69 66 20 74 68 65 72 65 20  e used if there 
396e2 69 73 20 61 20 70 72 69 6f 72 20 3e 3d 20 6f 72  is a prior >= or
396e3 20 3e 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41   >..        ** A
396e4 20 3e 3d 20 6f 72 20 3e 20 63 61 6e 20 62 65 20   >= or > can be 
396e5 75 73 65 64 20 69 66 20 74 68 65 72 65 20 69 73  used if there is
396e6 20 61 20 70 72 69 6f 72 20 3c 20 6f 72 20 3c 3d   a prior < or <=
396e7 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3c  ..        ** A <
396e8 3d 20 6f 72 20 3c 20 69 73 20 64 69 73 71 75 61  = or < is disqua
396e9 6c 69 66 69 65 64 20 69 66 20 74 68 65 72 65 20  lified if there 
396ea 69 73 20 61 20 70 72 69 6f 72 20 3c 3d 2c 20 3c  is a prior <=, <
396eb 2c 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20  , or ==..       
396ec 20 2a 2a 20 41 20 3e 3d 20 6f 72 20 3e 20 69 73   ** A >= or > is
396ed 20 64 69 73 71 75 61 6c 69 66 69 65 64 20 69 66   disqualified if
396ee 20 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f   there is a prio
396ef 72 20 3e 3d 2c 20 3e 2c 20 6f 72 20 3d 3d 2e 0a  r >=, >, or ==..
396f0 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3d 3d 20          ** A == 
396f1 69 73 20 64 69 73 71 75 61 6c 69 66 65 64 20 69  is disqualifed i
396f2 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 70  f there is any p
396f3 72 69 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 2e  rior constraint.
396f4 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
396f5 20 20 20 20 69 6e 74 20 6a 2c 20 6f 70 6d 73 6b      int j, opmsk
396f6 3b 0a 20 20 20 20 20 20 20 20 73 74 61 74 69 63  ;.        static
396f7 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
396f8 63 68 61 72 20 63 6f 6d 70 61 74 69 62 6c 65 5b  char compatible[
396f9 5d 20 3d 20 7b 20 30 2c 20 30 2c 20 31 2c 20 31  ] = { 0, 0, 1, 1
396fa 2c 20 32 2c 20 32 20 7d 3b 0a 20 20 20 20 20 20  , 2, 2 };.      
396fb 20 20 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74    assert( compat
396fc 69 62 6c 65 5b 52 54 52 45 45 5f 45 51 20 26 20  ible[RTREE_EQ & 
396fd 37 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  7]==0 );.       
396fe 20 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69   assert( compati
396ff 62 6c 65 5b 52 54 52 45 45 5f 4c 54 20 26 20 37  ble[RTREE_LT & 7
39700 5d 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20  ]==1 );.        
39701 61 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62  assert( compatib
39702 6c 65 5b 52 54 52 45 45 5f 4c 45 20 26 20 37 5d  le[RTREE_LE & 7]
39703 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61  ==1 );.        a
39704 73 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c  ssert( compatibl
39705 65 5b 52 54 52 45 45 5f 47 54 20 26 20 37 5d 3d  e[RTREE_GT & 7]=
39706 3d 32 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  =2 );.        as
39707 73 65 72 74 28 20 63 6f 6d 70 61 74 69 62 6c 65  sert( compatible
39708 5b 52 54 52 45 45 5f 47 45 20 26 20 37 5d 3d 3d  [RTREE_GE & 7]==
39709 32 20 29 3b 0a 20 20 20 20 20 20 20 20 63 43 6f  2 );.        cCo
3970a 6c 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 2d  l = p->iColumn -
3970b 20 31 20 2b 20 27 61 27 3b 0a 20 20 20 20 20 20   1 + 'a';.      
3970c 20 20 6f 70 6d 73 6b 20 3d 20 63 6f 6d 70 61 74    opmsk = compat
3970d 69 62 6c 65 5b 6f 70 20 26 20 37 5d 3b 0a 20 20  ible[op & 7];.  
3970e 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
3970f 3c 69 49 64 78 3b 20 6a 2b 3d 32 29 7b 0a 20 20  <iIdx; j+=2){.  
39710 20 20 20 20 20 20 20 20 69 66 28 20 7a 49 64 78          if( zIdx
39711 53 74 72 5b 6a 2b 31 5d 3d 3d 63 43 6f 6c 20 26  Str[j+1]==cCol &
39712 26 20 28 63 6f 6d 70 61 74 69 62 6c 65 5b 7a 49  & (compatible[zI
39713 64 78 53 74 72 5b 6a 5d 20 26 20 37 5d 20 26 20  dxStr[j] & 7] & 
39714 6f 70 6d 73 6b 29 21 3d 30 20 29 7b 0a 20 20 20  opmsk)!=0 ){.   
39715 20 20 20 20 20 20 20 20 20 6f 70 20 3d 20 30 3b           op = 0;
39716 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
39717 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
39718 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
39719 7d 0a 20 20 20 20 20 20 69 66 28 20 6f 70 20 29  }.      if( op )
3971a 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
3971b 28 20 69 49 64 78 3c 73 69 7a 65 6f 66 28 7a 49  ( iIdx<sizeof(zI
3971c 64 78 53 74 72 29 2d 31 20 29 3b 0a 20 20 20 20  dxStr)-1 );.    
3971d 20 20 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78      zIdxStr[iIdx
3971e 2b 2b 5d 20 3d 20 6f 70 3b 0a 20 20 20 20 20 20  ++] = op;.      
3971f 20 20 7a 49 64 78 53 74 72 5b 69 49 64 78 2b 2b    zIdxStr[iIdx++
39720 5d 20 3d 20 63 43 6f 6c 3b 0a 20 20 20 20 20 20  ] = cCol;.      
39721 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e    pIdxInfo->aCon
39722 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d  straintUsage[ii]
39723 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 28 69 49  .argvIndex = (iI
39724 64 78 2f 32 29 3b 0a 20 20 20 20 20 20 20 20 70  dx/2);.        p
39725 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
39726 61 69 6e 74 55 73 61 67 65 5b 69 69 5d 2e 6f 6d  aintUsage[ii].om
39727 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  it = 1;.      }.
39728 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 49 64      }.  }..  pId
39729 78 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20  xInfo->idxNum = 
3972a 32 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e  2;.  pIdxInfo->n
3972b 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20  eedToFreeIdxStr 
3972c 3d 20 31 3b 0a 20 20 69 66 28 20 69 49 64 78 3e  = 1;.  if( iIdx>
3972d 30 20 26 26 20 30 3d 3d 28 70 49 64 78 49 6e 66  0 && 0==(pIdxInf
3972e 6f 2d 3e 69 64 78 53 74 72 20 3d 20 73 71 6c 69  o->idxStr = sqli
3972f 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22  te3_mprintf("%s"
39730 2c 20 7a 49 64 78 53 74 72 29 29 20 29 7b 0a 20  , zIdxStr)) ){. 
39731 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
39732 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61 73  _NOMEM;.  }.  as
39733 73 65 72 74 28 20 69 49 64 78 3e 3d 30 20 29 3b  sert( iIdx>=0 );
39734 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74  .  pIdxInfo->est
39735 69 6d 61 74 65 64 43 6f 73 74 20 3d 20 28 32 30  imatedCost = (20
39736 30 30 30 30 30 2e 30 20 2f 20 28 64 6f 75 62 6c  00000.0 / (doubl
39737 65 29 28 69 49 64 78 20 2b 20 31 29 29 3b 0a 20  e)(iIdx + 1));. 
39738 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
39739 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
3973a 4e 2d 64 69 6d 65 6e 73 69 6f 6e 61 6c 20 76 6f  N-dimensional vo
3973b 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63 65 6c 6c  lumn of the cell
3973c 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 2e 0a 2a   stored in *p..*
3973d 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63  /.static float c
3973e 65 6c 6c 41 72 65 61 28 52 74 72 65 65 20 2a 70  ellArea(Rtree *p
3973f 52 74 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c  Rtree, RtreeCell
39740 20 2a 70 29 7b 0a 20 20 66 6c 6f 61 74 20 61 72   *p){.  float ar
39741 65 61 20 3d 20 31 2e 30 3b 0a 20 20 69 6e 74 20  ea = 1.0;.  int 
39742 69 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ii;.  for(ii=0; 
39743 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
39744 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
39745 20 61 72 65 61 20 3d 20 61 72 65 61 20 2a 20 28   area = area * (
39746 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64  DCOORD(p->aCoord
39747 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f 4f 52 44  [ii+1]) - DCOORD
39748 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 29  (p->aCoord[ii]))
39749 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61  ;.  }.  return a
3974a 72 65 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  rea;.}../*.** Re
3974b 74 75 72 6e 20 74 68 65 20 6d 61 72 67 69 6e 20  turn the margin 
3974c 6c 65 6e 67 74 68 20 6f 66 20 63 65 6c 6c 20 70  length of cell p
3974d 2e 20 54 68 65 20 6d 61 72 67 69 6e 20 6c 65 6e  . The margin len
3974e 67 74 68 20 69 73 20 74 68 65 20 73 75 6d 0a 2a  gth is the sum.*
3974f 2a 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74 73  * of the objects
39750 20 73 69 7a 65 20 69 6e 20 65 61 63 68 20 64 69   size in each di
39751 6d 65 6e 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  mension..*/.stat
39752 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4d 61 72  ic float cellMar
39753 67 69 6e 28 52 74 72 65 65 20 2a 70 52 74 72 65  gin(Rtree *pRtre
39754 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 29  e, RtreeCell *p)
39755 7b 0a 20 20 66 6c 6f 61 74 20 6d 61 72 67 69 6e  {.  float margin
39756 20 3d 20 30 2e 30 3b 0a 20 20 69 6e 74 20 69 69   = 0.0;.  int ii
39757 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
39758 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
39759 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 6d  ); ii+=2){.    m
3975a 61 72 67 69 6e 20 2b 3d 20 28 44 43 4f 4f 52 44  argin += (DCOORD
3975b 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d  (p->aCoord[ii+1]
3975c 29 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61 43  ) - DCOORD(p->aC
3975d 6f 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a  oord[ii]));.  }.
3975e 20 20 72 65 74 75 72 6e 20 6d 61 72 67 69 6e 3b    return margin;
3975f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 6f 72 65 20  .}../*.** Store 
39760 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 63 65 6c  the union of cel
39761 6c 73 20 70 31 20 61 6e 64 20 70 32 20 69 6e 20  ls p1 and p2 in 
39762 70 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  p1..*/.static vo
39763 69 64 20 63 65 6c 6c 55 6e 69 6f 6e 28 52 74 72  id cellUnion(Rtr
39764 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
39765 65 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65  eCell *p1, Rtree
39766 43 65 6c 6c 20 2a 70 32 29 7b 0a 20 20 69 6e 74  Cell *p2){.  int
39767 20 69 69 3b 0a 20 20 69 66 28 20 70 52 74 72 65   ii;.  if( pRtre
39768 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52  e->eCoordType==R
39769 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33  TREE_COORD_REAL3
3976a 32 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d  2 ){.    for(ii=
3976b 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e  0; ii<(pRtree->n
3976c 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a  Dim*2); ii+=2){.
3976d 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64        p1->aCoord
3976e 5b 69 69 5d 2e 66 20 3d 20 4d 49 4e 28 70 31 2d  [ii].f = MIN(p1-
3976f 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 2c 20 70  >aCoord[ii].f, p
39770 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 29  2->aCoord[ii].f)
39771 3b 0a 20 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f  ;.      p1->aCoo
39772 72 64 5b 69 69 2b 31 5d 2e 66 20 3d 20 4d 41 58  rd[ii+1].f = MAX
39773 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31  (p1->aCoord[ii+1
39774 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b  ].f, p2->aCoord[
39775 69 69 2b 31 5d 2e 66 29 3b 0a 20 20 20 20 7d 0a  ii+1].f);.    }.
39776 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f 72    }else{.    for
39777 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
39778 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
39779 32 29 7b 0a 20 20 20 20 20 20 70 31 2d 3e 61 43  2){.      p1->aC
3977a 6f 6f 72 64 5b 69 69 5d 2e 69 20 3d 20 4d 49 4e  oord[ii].i = MIN
3977b 28 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e  (p1->aCoord[ii].
3977c 69 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69  i, p2->aCoord[ii
3977d 5d 2e 69 29 3b 0a 20 20 20 20 20 20 70 31 2d 3e  ].i);.      p1->
3977e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20 3d  aCoord[ii+1].i =
3977f 20 4d 41 58 28 70 31 2d 3e 61 43 6f 6f 72 64 5b   MAX(p1->aCoord[
39780 69 69 2b 31 5d 2e 69 2c 20 70 32 2d 3e 61 43 6f  ii+1].i, p2->aCo
39781 6f 72 64 5b 69 69 2b 31 5d 2e 69 29 3b 0a 20 20  ord[ii+1].i);.  
39782 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
39783 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20   Return true if 
39784 74 68 65 20 61 72 65 61 20 63 6f 76 65 72 65 64  the area covered
39785 20 62 79 20 70 32 20 69 73 20 61 20 73 75 62 73   by p2 is a subs
39786 65 74 20 6f 66 20 74 68 65 20 61 72 65 61 20 63  et of the area c
39787 6f 76 65 72 65 64 0a 2a 2a 20 62 79 20 70 31 2e  overed.** by p1.
39788 20 46 61 6c 73 65 20 6f 74 68 65 72 77 69 73 65   False otherwise
39789 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3978a 63 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 52 74 72  cellContains(Rtr
3978b 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
3978c 65 43 65 6c 6c 20 2a 70 31 2c 20 52 74 72 65 65  eCell *p1, Rtree
3978d 43 65 6c 6c 20 2a 70 32 29 7b 0a 20 20 69 6e 74  Cell *p2){.  int
3978e 20 69 69 3b 0a 20 20 69 6e 74 20 69 73 49 6e 74   ii;.  int isInt
3978f 20 3d 20 28 70 52 74 72 65 65 2d 3e 65 43 6f 6f   = (pRtree->eCoo
39790 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
39791 4f 52 44 5f 49 4e 54 33 32 29 3b 0a 20 20 66 6f  ORD_INT32);.  fo
39792 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72  r(ii=0; ii<(pRtr
39793 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b  ee->nDim*2); ii+
39794 3d 32 29 7b 0a 20 20 20 20 52 74 72 65 65 43 6f  =2){.    RtreeCo
39795 6f 72 64 20 2a 61 31 20 3d 20 26 70 31 2d 3e 61  ord *a1 = &p1->a
39796 43 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20 52  Coord[ii];.    R
39797 74 72 65 65 43 6f 6f 72 64 20 2a 61 32 20 3d 20  treeCoord *a2 = 
39798 26 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 3b  &p2->aCoord[ii];
39799 0a 20 20 20 20 69 66 28 20 28 21 69 73 49 6e 74  .    if( (!isInt
3979a 20 26 26 20 28 61 32 5b 30 5d 2e 66 3c 61 31 5b   && (a2[0].f<a1[
3979b 30 5d 2e 66 20 7c 7c 20 61 32 5b 31 5d 2e 66 3e  0].f || a2[1].f>
3979c 61 31 5b 31 5d 2e 66 29 29 20 0a 20 20 20 20 20  a1[1].f)) .     
3979d 7c 7c 20 28 20 69 73 49 6e 74 20 26 26 20 28 61  || ( isInt && (a
3979e 32 5b 30 5d 2e 69 3c 61 31 5b 30 5d 2e 69 20 7c  2[0].i<a1[0].i |
3979f 7c 20 61 32 5b 31 5d 2e 69 3e 61 31 5b 31 5d 2e  | a2[1].i>a1[1].
397a0 69 29 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  i)) .    ){.    
397a1 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
397a2 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31  }.  }.  return 1
397a3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
397a4 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 63 65 6c  n the amount cel
397a5 6c 20 70 20 77 6f 75 6c 64 20 67 72 6f 77 20 62  l p would grow b
397a6 79 20 69 66 20 69 74 20 77 65 72 65 20 75 6e 69  y if it were uni
397a7 6f 6e 65 64 20 77 69 74 68 20 70 43 65 6c 6c 2e  oned with pCell.
397a8 0a 2a 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61 74  .*/.static float
397a9 20 63 65 6c 6c 47 72 6f 77 74 68 28 52 74 72 65   cellGrowth(Rtre
397aa 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
397ab 43 65 6c 6c 20 2a 70 2c 20 52 74 72 65 65 43 65  Cell *p, RtreeCe
397ac 6c 6c 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 66 6c  ll *pCell){.  fl
397ad 6f 61 74 20 61 72 65 61 3b 0a 20 20 52 74 72 65  oat area;.  Rtre
397ae 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 6d 65  eCell cell;.  me
397af 6d 63 70 79 28 26 63 65 6c 6c 2c 20 70 2c 20 73  mcpy(&cell, p, s
397b0 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
397b1 29 3b 0a 20 20 61 72 65 61 20 3d 20 63 65 6c 6c  );.  area = cell
397b2 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65  Area(pRtree, &ce
397b3 6c 6c 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f 6e  ll);.  cellUnion
397b4 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20  (pRtree, &cell, 
397b5 70 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  pCell);.  return
397b6 20 28 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65   (cellArea(pRtre
397b7 65 2c 20 26 63 65 6c 6c 29 2d 61 72 65 61 29 3b  e, &cell)-area);
397b8 0a 7d 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  .}..#if VARIANT_
397b9 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
397ba 53 55 42 54 52 45 45 20 7c 7c 20 56 41 52 49 41  SUBTREE || VARIA
397bb 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53 50 4c  NT_RSTARTREE_SPL
397bc 49 54 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20  IT.static float 
397bd 63 65 6c 6c 4f 76 65 72 6c 61 70 28 0a 20 20 52  cellOverlap(.  R
397be 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
397bf 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 0a   RtreeCell *p, .
397c0 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
397c1 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ll, .  int nCell
397c2 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c 75 64  , .  int iExclud
397c3 65 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  e.){.  int ii;. 
397c4 20 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 20 3d   float overlap =
397c5 20 30 2e 30 3b 0a 20 20 66 6f 72 28 69 69 3d 30   0.0;.  for(ii=0
397c6 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  ; ii<nCell; ii++
397c7 29 7b 0a 20 20 20 20 69 66 28 20 69 69 21 3d 69  ){.    if( ii!=i
397c8 45 78 63 6c 75 64 65 20 29 7b 0a 20 20 20 20 20  Exclude ){.     
397c9 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 66   int jj;.      f
397ca 6c 6f 61 74 20 6f 20 3d 20 31 2e 30 3b 0a 20 20  loat o = 1.0;.  
397cb 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a      for(jj=0; jj
397cc 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
397cd 29 3b 20 6a 6a 2b 3d 32 29 7b 0a 20 20 20 20 20  ); jj+=2){.     
397ce 20 20 20 64 6f 75 62 6c 65 20 78 31 3b 0a 20 20     double x1;.  
397cf 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 32 3b        double x2;
397d0 0a 0a 20 20 20 20 20 20 20 20 78 31 20 3d 20 4d  ..        x1 = M
397d1 41 58 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  AX(DCOORD(p->aCo
397d2 6f 72 64 5b 6a 6a 5d 29 2c 20 44 43 4f 4f 52 44  ord[jj]), DCOORD
397d3 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72  (aCell[ii].aCoor
397d4 64 5b 6a 6a 5d 29 29 3b 0a 20 20 20 20 20 20 20  d[jj]));.       
397d5 20 78 32 20 3d 20 4d 49 4e 28 44 43 4f 4f 52 44   x2 = MIN(DCOORD
397d6 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 2b 31 5d  (p->aCoord[jj+1]
397d7 29 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b  ), DCOORD(aCell[
397d8 69 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 2b 31 5d  ii].aCoord[jj+1]
397d9 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28  ));..        if(
397da 20 78 32 3c 78 31 20 29 7b 0a 20 20 20 20 20 20   x2<x1 ){.      
397db 20 20 20 20 6f 20 3d 20 30 2e 30 3b 0a 20 20 20      o = 0.0;.   
397dc 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
397dd 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
397de 20 20 20 20 20 20 20 6f 20 3d 20 6f 20 2a 20 28         o = o * (
397df 78 32 2d 78 31 29 3b 0a 20 20 20 20 20 20 20 20  x2-x1);.        
397e0 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
397e1 6f 76 65 72 6c 61 70 20 2b 3d 20 6f 3b 0a 20 20  overlap += o;.  
397e2 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
397e3 20 6f 76 65 72 6c 61 70 3b 0a 7d 0a 23 65 6e 64   overlap;.}.#end
397e4 69 66 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f  if..#if VARIANT_
397e5 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45  RSTARTREE_CHOOSE
397e6 53 55 42 54 52 45 45 0a 73 74 61 74 69 63 20 66  SUBTREE.static f
397e7 6c 6f 61 74 20 63 65 6c 6c 4f 76 65 72 6c 61 70  loat cellOverlap
397e8 45 6e 6c 61 72 67 65 6d 65 6e 74 28 0a 20 20 52  Enlargement(.  R
397e9 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
397ea 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 2c 20 0a   RtreeCell *p, .
397eb 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 49 6e    RtreeCell *pIn
397ec 73 65 72 74 2c 20 0a 20 20 52 74 72 65 65 43 65  sert, .  RtreeCe
397ed 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e  ll *aCell, .  in
397ee 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20  t nCell, .  int 
397ef 69 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20 66 6c  iExclude.){.  fl
397f0 6f 61 74 20 62 65 66 6f 72 65 3b 0a 20 20 66 6c  oat before;.  fl
397f1 6f 61 74 20 61 66 74 65 72 3b 0a 20 20 62 65 66  oat after;.  bef
397f2 6f 72 65 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61  ore = cellOverla
397f3 70 28 70 52 74 72 65 65 2c 20 70 2c 20 61 43 65  p(pRtree, p, aCe
397f4 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69 45 78 63 6c  ll, nCell, iExcl
397f5 75 64 65 29 3b 0a 20 20 63 65 6c 6c 55 6e 69 6f  ude);.  cellUnio
397f6 6e 28 70 52 74 72 65 65 2c 20 70 2c 20 70 49 6e  n(pRtree, p, pIn
397f7 73 65 72 74 29 3b 0a 20 20 61 66 74 65 72 20 3d  sert);.  after =
397f8 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74   cellOverlap(pRt
397f9 72 65 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e  ree, p, aCell, n
397fa 43 65 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b  Cell, iExclude);
397fb 0a 20 20 72 65 74 75 72 6e 20 61 66 74 65 72 2d  .  return after-
397fc 62 65 66 6f 72 65 3b 0a 7d 0a 23 65 6e 64 69 66  before;.}.#endif
397fd 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  .../*.** This fu
397fe 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
397ff 73 20 74 68 65 20 43 68 6f 6f 73 65 4c 65 61 66  s the ChooseLeaf
39800 20 61 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20   algorithm from 
39801 47 75 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2a 20 43  Gutman[84]..** C
39802 68 6f 6f 73 65 53 75 62 54 72 65 65 20 69 6e 20  hooseSubTree in 
39803 72 2a 74 72 65 65 20 74 65 72 6d 69 6e 6f 6c 6f  r*tree terminolo
39804 67 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  gy..*/.static in
39805 74 20 43 68 6f 6f 73 65 4c 65 61 66 28 0a 20 20  t ChooseLeaf(.  
39806 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20  Rtree *pRtree,  
39807 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39808 52 74 72 65 65 20 74 61 62 6c 65 20 2a 2f 0a 20  Rtree table */. 
39809 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
3980a 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l,            /*
3980b 20 43 65 6c 6c 20 74 6f 20 69 6e 73 65 72 74 20   Cell to insert 
3980c 69 6e 74 6f 20 72 74 72 65 65 20 2a 2f 0a 20 20  into rtree */.  
3980d 69 6e 74 20 69 48 65 69 67 68 74 2c 20 20 20 20  int iHeight,    
3980e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3980f 48 65 69 67 68 74 20 6f 66 20 73 75 62 2d 74 72  Height of sub-tr
39810 65 65 20 72 6f 6f 74 65 64 20 61 74 20 70 43 65  ee rooted at pCe
39811 6c 6c 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64  ll */.  RtreeNod
39812 65 20 2a 2a 70 70 4c 65 61 66 20 20 20 20 20 20  e **ppLeaf      
39813 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 6c       /* OUT: Sel
39814 65 63 74 65 64 20 6c 65 61 66 20 70 61 67 65 20  ected leaf page 
39815 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
39816 20 20 69 6e 74 20 69 69 3b 0a 20 20 52 74 72 65    int ii;.  Rtre
39817 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20  eNode *pNode;.  
39818 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
39819 28 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26  (pRtree, 1, 0, &
3981a 70 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69  pNode);..  for(i
3981b 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
3981c 4f 4b 20 26 26 20 69 69 3c 28 70 52 74 72 65 65  OK && ii<(pRtree
3981d 2d 3e 69 44 65 70 74 68 2d 69 48 65 69 67 68 74  ->iDepth-iHeight
3981e 29 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e  ); ii++){.    in
3981f 74 20 69 43 65 6c 6c 3b 0a 20 20 20 20 73 71 6c  t iCell;.    sql
39820 69 74 65 33 5f 69 6e 74 36 34 20 69 42 65 73 74  ite3_int64 iBest
39821 3b 0a 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69  ;..    float fMi
39822 6e 47 72 6f 77 74 68 3b 0a 20 20 20 20 66 6c 6f  nGrowth;.    flo
39823 61 74 20 66 4d 69 6e 41 72 65 61 3b 0a 20 20 20  at fMinArea;.   
39824 20 66 6c 6f 61 74 20 66 4d 69 6e 4f 76 65 72 6c   float fMinOverl
39825 61 70 3b 0a 0a 20 20 20 20 69 6e 74 20 6e 43 65  ap;..    int nCe
39826 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
39827 29 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  );.    RtreeCell
39828 20 63 65 6c 6c 3b 0a 20 20 20 20 52 74 72 65 65   cell;.    Rtree
39829 4e 6f 64 65 20 2a 70 43 68 69 6c 64 3b 0a 0a 20  Node *pChild;.. 
3982a 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43     RtreeCell *aC
3982b 65 6c 6c 20 3d 20 30 3b 0a 0a 23 69 66 20 56 41  ell = 0;..#if VA
3982c 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
3982d 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a 20 20  CHOOSESUBTREE.  
3982e 20 20 69 66 28 20 69 69 3d 3d 28 70 52 74 72 65    if( ii==(pRtre
3982f 65 2d 3e 69 44 65 70 74 68 2d 31 29 20 29 7b 0a  e->iDepth-1) ){.
39830 20 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20        int jj;.  
39831 20 20 20 20 61 43 65 6c 6c 20 3d 20 73 71 6c 69      aCell = sqli
39832 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
39833 66 28 52 74 72 65 65 43 65 6c 6c 29 2a 6e 43 65  f(RtreeCell)*nCe
39834 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  ll);.      if( !
39835 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  aCell ){.       
39836 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
39837 45 4d 3b 0a 20 20 20 20 20 20 20 20 6e 6f 64 65  EM;.        node
39838 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
39839 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20  pNode);.        
3983a 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 20  pNode = 0;.     
3983b 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
3983c 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6a     }.      for(j
3983d 6a 3d 30 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a  j=0; jj<nCell; j
3983e 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 6e 6f  j++){.        no
3983f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65  deGetCell(pRtree
39840 2c 20 70 4e 6f 64 65 2c 20 6a 6a 2c 20 26 61 43  , pNode, jj, &aC
39841 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20  ell[jj]);.      
39842 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  }.    }.#endif..
39843 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68      /* Select th
39844 65 20 63 68 69 6c 64 20 6e 6f 64 65 20 77 68 69  e child node whi
39845 63 68 20 77 69 6c 6c 20 62 65 20 65 6e 6c 61 72  ch will be enlar
39846 67 65 64 20 74 68 65 20 6c 65 61 73 74 20 69 66  ged the least if
39847 20 70 43 65 6c 6c 0a 20 20 20 20 2a 2a 20 69 73   pCell.    ** is
39848 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 69   inserted into i
39849 74 2e 20 52 65 73 6f 6c 76 65 20 74 69 65 73 20  t. Resolve ties 
3984a 62 79 20 63 68 6f 6f 73 69 6e 67 20 74 68 65 20  by choosing the 
3984b 65 6e 74 72 79 20 77 69 74 68 0a 20 20 20 20 2a  entry with.    *
3984c 2a 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 61  * the smallest a
3984d 72 65 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  rea..    */.    
3984e 66 6f 72 28 69 43 65 6c 6c 3d 30 3b 20 69 43 65  for(iCell=0; iCe
3984f 6c 6c 3c 6e 43 65 6c 6c 3b 20 69 43 65 6c 6c 2b  ll<nCell; iCell+
39850 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  +){.      float 
39851 67 72 6f 77 74 68 3b 0a 20 20 20 20 20 20 66 6c  growth;.      fl
39852 6f 61 74 20 61 72 65 61 3b 0a 20 20 20 20 20 20  oat area;.      
39853 66 6c 6f 61 74 20 6f 76 65 72 6c 61 70 20 3d 20  float overlap = 
39854 30 2e 30 3b 0a 20 20 20 20 20 20 6e 6f 64 65 47  0.0;.      nodeG
39855 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  etCell(pRtree, p
39856 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 26 63 65  Node, iCell, &ce
39857 6c 6c 29 3b 0a 20 20 20 20 20 20 67 72 6f 77 74  ll);.      growt
39858 68 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70  h = cellGrowth(p
39859 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43  Rtree, &cell, pC
3985a 65 6c 6c 29 3b 0a 20 20 20 20 20 20 61 72 65 61  ell);.      area
3985b 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72   = cellArea(pRtr
3985c 65 65 2c 20 26 63 65 6c 6c 29 3b 0a 23 69 66 20  ee, &cell);.#if 
3985d 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
3985e 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 0a  E_CHOOSESUBTREE.
3985f 20 20 20 20 20 20 69 66 28 20 69 69 3d 3d 28 70        if( ii==(p
39860 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 31 29  Rtree->iDepth-1)
39861 20 29 7b 0a 20 20 20 20 20 20 20 20 6f 76 65 72   ){.        over
39862 6c 61 70 20 3d 20 63 65 6c 6c 4f 76 65 72 6c 61  lap = cellOverla
39863 70 45 6e 6c 61 72 67 65 6d 65 6e 74 28 70 52 74  pEnlargement(pRt
39864 72 65 65 2c 26 63 65 6c 6c 2c 70 43 65 6c 6c 2c  ree,&cell,pCell,
39865 61 43 65 6c 6c 2c 6e 43 65 6c 6c 2c 69 43 65 6c  aCell,nCell,iCel
39866 6c 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  l);.      }.#end
39867 69 66 0a 20 20 20 20 20 20 69 66 28 20 28 69 43  if.      if( (iC
39868 65 6c 6c 3d 3d 30 29 20 0a 20 20 20 20 20 20 20  ell==0) .       
39869 7c 7c 20 28 6f 76 65 72 6c 61 70 3c 66 4d 69 6e  || (overlap<fMin
3986a 4f 76 65 72 6c 61 70 29 20 0a 20 20 20 20 20 20  Overlap) .      
3986b 20 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 4d   || (overlap==fM
3986c 69 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72 6f  inOverlap && gro
3986d 77 74 68 3c 66 4d 69 6e 47 72 6f 77 74 68 29 0a  wth<fMinGrowth).
3986e 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72 6c         || (overl
3986f 61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c 61 70 20  ap==fMinOverlap 
39870 26 26 20 67 72 6f 77 74 68 3d 3d 66 4d 69 6e 47  && growth==fMinG
39871 72 6f 77 74 68 20 26 26 20 61 72 65 61 3c 66 4d  rowth && area<fM
39872 69 6e 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b  inArea).      ){
39873 0a 20 20 20 20 20 20 20 20 66 4d 69 6e 4f 76 65  .        fMinOve
39874 72 6c 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b 0a  rlap = overlap;.
39875 20 20 20 20 20 20 20 20 66 4d 69 6e 47 72 6f 77          fMinGrow
39876 74 68 20 3d 20 67 72 6f 77 74 68 3b 0a 20 20 20  th = growth;.   
39877 20 20 20 20 20 66 4d 69 6e 41 72 65 61 20 3d 20       fMinArea = 
39878 61 72 65 61 3b 0a 20 20 20 20 20 20 20 20 69 42  area;.        iB
39879 65 73 74 20 3d 20 63 65 6c 6c 2e 69 52 6f 77 69  est = cell.iRowi
3987a 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  d;.      }.    }
3987b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ..    sqlite3_fr
3987c 65 65 28 61 43 65 6c 6c 29 3b 0a 20 20 20 20 72  ee(aCell);.    r
3987d 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28  c = nodeAcquire(
3987e 70 52 74 72 65 65 2c 20 69 42 65 73 74 2c 20 70  pRtree, iBest, p
3987f 4e 6f 64 65 2c 20 26 70 43 68 69 6c 64 29 3b 0a  Node, &pChild);.
39880 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
39881 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a  pRtree, pNode);.
39882 20 20 20 20 70 4e 6f 64 65 20 3d 20 70 43 68 69      pNode = pChi
39883 6c 64 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 4c 65  ld;.  }..  *ppLe
39884 61 66 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 65  af = pNode;.  re
39885 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
39886 2a 20 41 20 63 65 6c 6c 20 77 69 74 68 20 74 68  * A cell with th
39887 65 20 73 61 6d 65 20 63 6f 6e 74 65 6e 74 20 61  e same content a
39888 73 20 70 43 65 6c 6c 20 68 61 73 20 6a 75 73 74  s pCell has just
39889 20 62 65 65 6e 20 69 6e 73 65 72 74 65 64 20 69   been inserted i
3988a 6e 74 6f 0a 2a 2a 20 74 68 65 20 6e 6f 64 65 20  nto.** the node 
3988b 70 4e 6f 64 65 2e 20 54 68 69 73 20 66 75 6e 63  pNode. This func
3988c 74 69 6f 6e 20 75 70 64 61 74 65 73 20 74 68 65  tion updates the
3988d 20 62 6f 75 6e 64 69 6e 67 20 62 6f 78 20 63 65   bounding box ce
3988e 6c 6c 73 20 69 6e 0a 2a 2a 20 61 6c 6c 20 61 6e  lls in.** all an
3988f 63 65 73 74 6f 72 20 65 6c 65 6d 65 6e 74 73 2e  cestor elements.
39890 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
39891 41 64 6a 75 73 74 54 72 65 65 28 0a 20 20 52 74  AdjustTree(.  Rt
39892 72 65 65 20 2a 70 52 74 72 65 65 2c 20 20 20 20  ree *pRtree,    
39893 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39894 2f 2a 20 52 74 72 65 65 20 74 61 62 6c 65 20 2a  /* Rtree table *
39895 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  /.  RtreeNode *p
39896 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20 20 20  Node,           
39897 20 20 20 20 20 20 2f 2a 20 41 64 6a 75 73 74 20        /* Adjust 
39898 61 6e 63 65 73 74 72 79 20 6f 66 20 74 68 69 73  ancestry of this
39899 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 52 74 72 65   node. */.  Rtre
3989a 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 20 20 20  eCell *pCell    
3989b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3989c 20 54 68 69 73 20 63 65 6c 6c 20 77 61 73 20 6a   This cell was j
3989d 75 73 74 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a  ust inserted */.
3989e 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ){.  RtreeNode *
3989f 70 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 77 68 69  p = pNode;.  whi
398a0 6c 65 28 20 70 2d 3e 70 50 61 72 65 6e 74 20 29  le( p->pParent )
398a1 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  {.    RtreeCell 
398a2 63 65 6c 6c 3b 0a 20 20 20 20 52 74 72 65 65 4e  cell;.    RtreeN
398a3 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70  ode *pParent = p
398a4 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 69  ->pParent;.    i
398a5 6e 74 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 50  nt iCell = nodeP
398a6 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72 65  arentIndex(pRtre
398a7 65 2c 20 70 29 3b 0a 0a 20 20 20 20 6e 6f 64 65  e, p);..    node
398a8 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  GetCell(pRtree, 
398a9 70 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20  pParent, iCell, 
398aa 26 63 65 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20  &cell);.    if( 
398ab 21 63 65 6c 6c 43 6f 6e 74 61 69 6e 73 28 70 52  !cellContains(pR
398ac 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70 43 65  tree, &cell, pCe
398ad 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 65 6c  ll) ){.      cel
398ae 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
398af 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  cell, pCell);.  
398b0 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74      nodeOverwrit
398b1 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50  eCell(pRtree, pP
398b2 61 72 65 6e 74 2c 20 26 63 65 6c 6c 2c 20 69 43  arent, &cell, iC
398b3 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 0a 20 20  ell);.    }. .  
398b4 20 20 70 20 3d 20 70 50 61 72 65 6e 74 3b 0a 20    p = pParent;. 
398b5 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74   }.}../*.** Writ
398b6 65 20 6d 61 70 70 69 6e 67 20 28 69 52 6f 77 69  e mapping (iRowi
398b7 64 2d 3e 69 4e 6f 64 65 29 20 74 6f 20 74 68 65  d->iNode) to the
398b8 20 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20 74   <rtree>_rowid t
398b9 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  able..*/.static 
398ba 69 6e 74 20 72 6f 77 69 64 57 72 69 74 65 28 52  int rowidWrite(R
398bb 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 73 71  tree *pRtree, sq
398bc 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 52 6f 77  lite3_int64 iRow
398bd 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  id, sqlite3_int6
398be 34 20 69 4e 6f 64 65 29 7b 0a 20 20 73 71 6c 69  4 iNode){.  sqli
398bf 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
398c0 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77  Rtree->pWriteRow
398c1 69 64 2c 20 31 2c 20 69 52 6f 77 69 64 29 3b 0a  id, 1, iRowid);.
398c2 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
398c3 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 57 72  nt64(pRtree->pWr
398c4 69 74 65 52 6f 77 69 64 2c 20 32 2c 20 69 4e 6f  iteRowid, 2, iNo
398c5 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  de);.  sqlite3_s
398c6 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57 72 69  tep(pRtree->pWri
398c7 74 65 52 6f 77 69 64 29 3b 0a 20 20 72 65 74 75  teRowid);.  retu
398c8 72 6e 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  rn sqlite3_reset
398c9 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  (pRtree->pWriteR
398ca 6f 77 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  owid);.}../*.** 
398cb 57 72 69 74 65 20 6d 61 70 70 69 6e 67 20 28 69  Write mapping (i
398cc 4e 6f 64 65 2d 3e 69 50 61 72 29 20 74 6f 20 74  Node->iPar) to t
398cd 68 65 20 3c 72 74 72 65 65 3e 5f 70 61 72 65 6e  he <rtree>_paren
398ce 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  t table..*/.stat
398cf 69 63 20 69 6e 74 20 70 61 72 65 6e 74 57 72 69  ic int parentWri
398d0 74 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  te(Rtree *pRtree
398d1 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  , sqlite3_int64 
398d2 69 4e 6f 64 65 2c 20 73 71 6c 69 74 65 33 5f 69  iNode, sqlite3_i
398d3 6e 74 36 34 20 69 50 61 72 29 7b 0a 20 20 73 71  nt64 iPar){.  sq
398d4 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
398d5 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  (pRtree->pWriteP
398d6 61 72 65 6e 74 2c 20 31 2c 20 69 4e 6f 64 65 29  arent, 1, iNode)
398d7 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ;.  sqlite3_bind
398d8 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
398d9 57 72 69 74 65 50 61 72 65 6e 74 2c 20 32 2c 20  WriteParent, 2, 
398da 69 50 61 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  iPar);.  sqlite3
398db 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57  _step(pRtree->pW
398dc 72 69 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 72  riteParent);.  r
398dd 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72 65  eturn sqlite3_re
398de 73 65 74 28 70 52 74 72 65 65 2d 3e 70 57 72 69  set(pRtree->pWri
398df 74 65 50 61 72 65 6e 74 29 3b 0a 7d 0a 0a 73 74  teParent);.}..st
398e0 61 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e  atic int rtreeIn
398e1 73 65 72 74 43 65 6c 6c 28 52 74 72 65 65 20 2a  sertCell(Rtree *
398e2 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20 52  , RtreeNode *, R
398e3 74 72 65 65 43 65 6c 6c 20 2a 2c 20 69 6e 74 29  treeCell *, int)
398e4 3b 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  ;..#if VARIANT_G
398e5 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50  UTTMAN_LINEAR_SP
398e6 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  LIT./*.** Implem
398e7 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
398e8 6c 69 6e 65 61 72 20 76 61 72 69 61 6e 74 20 6f  linear variant o
398e9 66 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28 29  f the PickNext()
398ea 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a   function from.*
398eb 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a  * Guttman[84]..*
398ec 2f 0a 73 74 61 74 69 63 20 52 74 72 65 65 43 65  /.static RtreeCe
398ed 6c 6c 20 2a 4c 69 6e 65 61 72 50 69 63 6b 4e 65  ll *LinearPickNe
398ee 78 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  xt(.  Rtree *pRt
398ef 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ree,.  RtreeCell
398f0 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20   *aCell, .  int 
398f1 6e 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65 65 43  nCell, .  RtreeC
398f2 65 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c 20 0a  ell *pLeftBox, .
398f3 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 52 69    RtreeCell *pRi
398f4 67 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20 2a 61  ghtBox,.  int *a
398f5 69 55 73 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69  iUsed.){.  int i
398f6 69 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 61  i;.  for(ii=0; a
398f7 69 55 73 65 64 5b 69 69 5d 3b 20 69 69 2b 2b 29  iUsed[ii]; ii++)
398f8 3b 0a 20 20 61 69 55 73 65 64 5b 69 69 5d 20 3d  ;.  aiUsed[ii] =
398f9 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43   1;.  return &aC
398fa 65 6c 6c 5b 69 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  ell[ii];.}../*.*
398fb 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
398fc 20 6f 66 20 74 68 65 20 6c 69 6e 65 61 72 20 76   of the linear v
398fd 61 72 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69  ariant of the Pi
398fe 63 6b 53 65 65 64 73 28 29 20 66 75 6e 63 74 69  ckSeeds() functi
398ff 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d  on from.** Guttm
39900 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69  an[84]..*/.stati
39901 63 20 76 6f 69 64 20 4c 69 6e 65 61 72 50 69 63  c void LinearPic
39902 6b 53 65 65 64 73 28 0a 20 20 52 74 72 65 65 20  kSeeds(.  Rtree 
39903 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
39904 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
39905 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
39906 74 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a  t *piLeftSeed, .
39907 20 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65    int *piRightSe
39908 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ed.){.  int i;. 
39909 20 69 6e 74 20 69 4c 65 66 74 53 65 65 64 20 3d   int iLeftSeed =
3990a 20 30 3b 0a 20 20 69 6e 74 20 69 52 69 67 68 74   0;.  int iRight
3990b 53 65 65 64 20 3d 20 31 3b 0a 20 20 66 6c 6f 61  Seed = 1;.  floa
3990c 74 20 6d 61 78 4e 6f 72 6d 61 6c 49 6e 6e 65 72  t maxNormalInner
3990d 57 69 64 74 68 20 3d 20 30 2e 30 3b 0a 0a 20 20  Width = 0.0;..  
3990e 2f 2a 20 50 69 63 6b 20 74 77 6f 20 22 73 65 65  /* Pick two "see
3990f 64 22 20 63 65 6c 6c 73 20 66 72 6f 6d 20 74 68  d" cells from th
39910 65 20 61 72 72 61 79 20 6f 66 20 63 65 6c 6c 73  e array of cells
39911 2e 20 54 68 65 20 61 6c 67 6f 72 69 74 68 6d 20  . The algorithm 
39912 75 73 65 64 0a 20 20 2a 2a 20 68 65 72 65 20 69  used.  ** here i
39913 73 20 74 68 65 20 4c 69 6e 65 61 72 50 69 63 6b  s the LinearPick
39914 53 65 65 64 73 20 61 6c 67 6f 72 69 74 68 6d 20  Seeds algorithm 
39915 66 72 6f 6d 20 47 75 74 6d 61 6e 5b 31 39 38 34  from Gutman[1984
39916 5d 2e 20 54 68 65 20 0a 20 20 2a 2a 20 69 6e 64  ]. The .  ** ind
39917 69 63 65 73 20 6f 66 20 74 68 65 20 74 77 6f 20  ices of the two 
39918 73 65 65 64 20 63 65 6c 6c 73 20 69 6e 20 74 68  seed cells in th
39919 65 20 61 72 72 61 79 20 61 72 65 20 73 74 6f 72  e array are stor
3991a 65 64 20 69 6e 20 6c 6f 63 61 6c 0a 20 20 2a 2a  ed in local.  **
3991b 20 76 61 72 69 61 62 6c 65 73 20 69 4c 65 66 74   variables iLeft
3991c 53 65 65 6b 20 61 6e 64 20 69 52 69 67 68 74 53  Seek and iRightS
3991d 65 65 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  eed..  */.  for(
3991e 69 3d 30 3b 20 69 3c 70 52 74 72 65 65 2d 3e 6e  i=0; i<pRtree->n
3991f 44 69 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66  Dim; i++){.    f
39920 6c 6f 61 74 20 78 31 20 3d 20 44 43 4f 4f 52 44  loat x1 = DCOORD
39921 28 61 43 65 6c 6c 5b 30 5d 2e 61 43 6f 6f 72 64  (aCell[0].aCoord
39922 5b 69 2a 32 5d 29 3b 0a 20 20 20 20 66 6c 6f 61  [i*2]);.    floa
39923 74 20 78 32 20 3d 20 44 43 4f 4f 52 44 28 61 43  t x2 = DCOORD(aC
39924 65 6c 6c 5b 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a  ell[0].aCoord[i*
39925 32 2b 31 5d 29 3b 0a 20 20 20 20 66 6c 6f 61 74  2+1]);.    float
39926 20 78 33 20 3d 20 78 31 3b 0a 20 20 20 20 66 6c   x3 = x1;.    fl
39927 6f 61 74 20 78 34 20 3d 20 78 32 3b 0a 20 20 20  oat x4 = x2;.   
39928 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20 20 69 6e   int jj;..    in
39929 74 20 69 43 65 6c 6c 4c 65 66 74 20 3d 20 30 3b  t iCellLeft = 0;
3992a 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 52 69  .    int iCellRi
3992b 67 68 74 20 3d 20 30 3b 0a 0a 20 20 20 20 66 6f  ght = 0;..    fo
3992c 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 43 65 6c 6c  r(jj=1; jj<nCell
3992d 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 66  ; jj++){.      f
3992e 6c 6f 61 74 20 6c 65 66 74 20 3d 20 44 43 4f 4f  loat left = DCOO
3992f 52 44 28 61 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f  RD(aCell[jj].aCo
39930 6f 72 64 5b 69 2a 32 5d 29 3b 0a 20 20 20 20 20  ord[i*2]);.     
39931 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20 44   float right = D
39932 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 6a 6a 5d 2e  COORD(aCell[jj].
39933 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d 29 3b 0a  aCoord[i*2+1]);.
39934 0a 20 20 20 20 20 20 69 66 28 20 6c 65 66 74 3c  .      if( left<
39935 78 31 20 29 20 78 31 20 3d 20 6c 65 66 74 3b 0a  x1 ) x1 = left;.
39936 20 20 20 20 20 20 69 66 28 20 72 69 67 68 74 3e        if( right>
39937 78 34 20 29 20 78 34 20 3d 20 72 69 67 68 74 3b  x4 ) x4 = right;
39938 0a 20 20 20 20 20 20 69 66 28 20 6c 65 66 74 3e  .      if( left>
39939 78 33 20 29 7b 0a 20 20 20 20 20 20 20 20 78 33  x3 ){.        x3
3993a 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20 20 20   = left;.       
3993b 20 69 43 65 6c 6c 52 69 67 68 74 20 3d 20 6a 6a   iCellRight = jj
3993c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3993d 69 66 28 20 72 69 67 68 74 3c 78 32 20 29 7b 0a  if( right<x2 ){.
3993e 20 20 20 20 20 20 20 20 78 32 20 3d 20 72 69 67          x2 = rig
3993f 68 74 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c  ht;.        iCel
39940 6c 4c 65 66 74 20 3d 20 6a 6a 3b 0a 20 20 20 20  lLeft = jj;.    
39941 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69    }.    }..    i
39942 66 28 20 78 34 21 3d 78 31 20 29 7b 0a 20 20 20  f( x4!=x1 ){.   
39943 20 20 20 66 6c 6f 61 74 20 6e 6f 72 6d 61 6c 77     float normalw
39944 69 64 74 68 20 3d 20 28 78 33 20 2d 20 78 32 29  idth = (x3 - x2)
39945 20 2f 20 28 78 34 20 2d 20 78 31 29 3b 0a 20 20   / (x4 - x1);.  
39946 20 20 20 20 69 66 28 20 6e 6f 72 6d 61 6c 77 69      if( normalwi
39947 64 74 68 3e 6d 61 78 4e 6f 72 6d 61 6c 49 6e 6e  dth>maxNormalInn
39948 65 72 57 69 64 74 68 20 29 7b 0a 20 20 20 20 20  erWidth ){.     
39949 20 20 20 69 4c 65 66 74 53 65 65 64 20 3d 20 69     iLeftSeed = i
3994a 43 65 6c 6c 4c 65 66 74 3b 0a 20 20 20 20 20 20  CellLeft;.      
3994b 20 20 69 52 69 67 68 74 53 65 65 64 20 3d 20 69    iRightSeed = i
3994c 43 65 6c 6c 52 69 67 68 74 3b 0a 20 20 20 20 20  CellRight;.     
3994d 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
3994e 2a 70 69 4c 65 66 74 53 65 65 64 20 3d 20 69 4c  *piLeftSeed = iL
3994f 65 66 74 53 65 65 64 3b 0a 20 20 2a 70 69 52 69  eftSeed;.  *piRi
39950 67 68 74 53 65 65 64 20 3d 20 69 52 69 67 68 74  ghtSeed = iRight
39951 53 65 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Seed;.}.#endif /
39952 2a 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41  * VARIANT_GUTTMA
39953 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 20 2a  N_LINEAR_SPLIT *
39954 2f 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47  /..#if VARIANT_G
39955 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
39956 5f 53 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70  _SPLIT./*.** Imp
39957 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
39958 68 65 20 71 75 61 64 72 61 74 69 63 20 76 61 72  he quadratic var
39959 69 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b  iant of the Pick
3995a 4e 65 78 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  Next() function 
3995b 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
3995c 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52  84]..*/.static R
3995d 74 72 65 65 43 65 6c 6c 20 2a 51 75 61 64 72 61  treeCell *Quadra
3995e 74 69 63 50 69 63 6b 4e 65 78 74 28 0a 20 20 52  ticPickNext(.  R
3995f 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20  tree *pRtree,.  
39960 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
39961 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20  , .  int nCell, 
39962 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c  .  RtreeCell *pL
39963 65 66 74 42 6f 78 2c 20 0a 20 20 52 74 72 65 65  eftBox, .  Rtree
39964 43 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c  Cell *pRightBox,
39965 0a 20 20 69 6e 74 20 2a 61 69 55 73 65 64 0a 29  .  int *aiUsed.)
39966 7b 0a 20 20 23 64 65 66 69 6e 65 20 46 41 42 53  {.  #define FABS
39967 28 61 29 20 28 28 61 29 3c 30 2e 30 3f 2d 31 2e  (a) ((a)<0.0?-1.
39968 30 2a 28 61 29 3a 28 61 29 29 0a 0a 20 20 69 6e  0*(a):(a))..  in
39969 74 20 69 53 65 6c 65 63 74 20 3d 20 2d 31 3b 0a  t iSelect = -1;.
3996a 20 20 66 6c 6f 61 74 20 66 44 69 66 66 3b 0a 20    float fDiff;. 
3996b 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69   int ii;.  for(i
3996c 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=0; ii<nCell; i
3996d 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 69  i++){.    if( ai
3996e 55 73 65 64 5b 69 69 5d 3d 3d 30 20 29 7b 0a 20  Used[ii]==0 ){. 
3996f 20 20 20 20 20 66 6c 6f 61 74 20 6c 65 66 74 20       float left 
39970 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74  = cellGrowth(pRt
39971 72 65 65 2c 20 70 4c 65 66 74 42 6f 78 2c 20 26  ree, pLeftBox, &
39972 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20  aCell[ii]);.    
39973 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20    float right = 
39974 63 65 6c 6c 47 72 6f 77 74 68 28 70 52 74 72 65  cellGrowth(pRtre
39975 65 2c 20 70 4c 65 66 74 42 6f 78 2c 20 26 61 43  e, pLeftBox, &aC
39976 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 20 20  ell[ii]);.      
39977 66 6c 6f 61 74 20 64 69 66 66 20 3d 20 46 41 42  float diff = FAB
39978 53 28 72 69 67 68 74 2d 6c 65 66 74 29 3b 0a 20  S(right-left);. 
39979 20 20 20 20 20 69 66 28 20 69 53 65 6c 65 63 74       if( iSelect
3997a 3c 30 20 7c 7c 20 64 69 66 66 3e 66 44 69 66 66  <0 || diff>fDiff
3997b 20 29 7b 0a 20 20 20 20 20 20 20 20 66 44 69 66   ){.        fDif
3997c 66 20 3d 20 64 69 66 66 3b 0a 20 20 20 20 20 20  f = diff;.      
3997d 20 20 69 53 65 6c 65 63 74 20 3d 20 69 69 3b 0a    iSelect = ii;.
3997e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
3997f 7d 0a 20 20 61 69 55 73 65 64 5b 69 53 65 6c 65  }.  aiUsed[iSele
39980 63 74 5d 20 3d 20 31 3b 0a 20 20 72 65 74 75 72  ct] = 1;.  retur
39981 6e 20 26 61 43 65 6c 6c 5b 69 53 65 6c 65 63 74  n &aCell[iSelect
39982 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  ];.}../*.** Impl
39983 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
39984 65 20 71 75 61 64 72 61 74 69 63 20 76 61 72 69  e quadratic vari
39985 61 6e 74 20 6f 66 20 74 68 65 20 50 69 63 6b 53  ant of the PickS
39986 65 65 64 73 28 29 20 66 75 6e 63 74 69 6f 6e 20  eeds() function 
39987 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b  from.** Guttman[
39988 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  84]..*/.static v
39989 6f 69 64 20 51 75 61 64 72 61 74 69 63 50 69 63  oid QuadraticPic
3998a 6b 53 65 65 64 73 28 0a 20 20 52 74 72 65 65 20  kSeeds(.  Rtree 
3998b 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65  *pRtree,.  Rtree
3998c 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
3998d 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
3998e 74 20 2a 70 69 4c 65 66 74 53 65 65 64 2c 20 0a  t *piLeftSeed, .
3998f 20 20 69 6e 74 20 2a 70 69 52 69 67 68 74 53 65    int *piRightSe
39990 65 64 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  ed.){.  int ii;.
39991 20 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 69 6e 74    int jj;..  int
39992 20 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a   iLeftSeed = 0;.
39993 20 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64    int iRightSeed
39994 20 3d 20 31 3b 0a 20 20 66 6c 6f 61 74 20 66 57   = 1;.  float fW
39995 61 73 74 65 20 3d 20 30 2e 30 3b 0a 0a 20 20 66  aste = 0.0;..  f
39996 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c  or(ii=0; ii<nCel
39997 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 66 6f  l; ii++){.    fo
39998 72 28 6a 6a 3d 69 69 2b 31 3b 20 6a 6a 3c 6e 43  r(jj=ii+1; jj<nC
39999 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20  ell; jj++){.    
3999a 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20    float right = 
3999b 63 65 6c 6c 41 72 65 61 28 70 52 74 72 65 65 2c  cellArea(pRtree,
3999c 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20 20   &aCell[jj]);.  
3999d 20 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74 68      float growth
3999e 20 3d 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52   = cellGrowth(pR
3999f 74 72 65 65 2c 20 26 61 43 65 6c 6c 5b 69 69 5d  tree, &aCell[ii]
399a0 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d 29 3b 0a 20  , &aCell[jj]);. 
399a1 20 20 20 20 20 66 6c 6f 61 74 20 77 61 73 74 65       float waste
399a2 20 3d 20 67 72 6f 77 74 68 20 2d 20 72 69 67 68   = growth - righ
399a3 74 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 77 61  t;..      if( wa
399a4 73 74 65 3e 66 57 61 73 74 65 20 29 7b 0a 20 20  ste>fWaste ){.  
399a5 20 20 20 20 20 20 69 4c 65 66 74 53 65 65 64 20        iLeftSeed 
399a6 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 69 52  = ii;.        iR
399a7 69 67 68 74 53 65 65 64 20 3d 20 6a 6a 3b 0a 20  ightSeed = jj;. 
399a8 20 20 20 20 20 20 20 66 57 61 73 74 65 20 3d 20         fWaste = 
399a9 77 61 73 74 65 3b 0a 20 20 20 20 20 20 7d 0a 20  waste;.      }. 
399aa 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c     }.  }..  *piL
399ab 65 66 74 53 65 65 64 20 3d 20 69 4c 65 66 74 53  eftSeed = iLeftS
399ac 65 65 64 3b 0a 20 20 2a 70 69 52 69 67 68 74 53  eed;.  *piRightS
399ad 65 65 64 20 3d 20 69 52 69 67 68 74 53 65 65 64  eed = iRightSeed
399ae 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41  ;.}.#endif /* VA
399af 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55  RIANT_GUTTMAN_QU
399b0 41 44 52 41 54 49 43 5f 53 50 4c 49 54 20 2a 2f  ADRATIC_SPLIT */
399b1 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ../*.** Argument
399b2 73 20 61 49 64 78 2c 20 61 44 69 73 74 61 6e 63  s aIdx, aDistanc
399b3 65 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c 6c  e and aSpare all
399b4 20 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79 73   point to arrays
399b5 20 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64 78   of size.** nIdx
399b6 2e 20 54 68 65 20 61 49 64 78 20 61 72 72 61 79  . The aIdx array
399b7 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 65   contains the se
399b8 74 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66 72  t of integers fr
399b9 6f 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49 64  om 0 to .** (nId
399ba 78 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74 69  x-1) in no parti
399bb 63 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68 69  cular order. Thi
399bc 73 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74 73  s function sorts
399bd 20 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 69   the values.** i
399be 6e 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e 67  n aIdx according
399bf 20 74 6f 20 74 68 65 20 69 6e 64 65 78 65 64 20   to the indexed 
399c0 76 61 6c 75 65 73 20 69 6e 20 61 44 69 73 74 61  values in aDista
399c1 6e 63 65 2e 20 46 6f 72 0a 2a 2a 20 65 78 61 6d  nce. For.** exam
399c2 70 6c 65 2c 20 61 73 73 75 6d 69 6e 67 20 74 68  ple, assuming th
399c3 65 20 69 6e 70 75 74 73 3a 0a 2a 2a 0a 2a 2a 20  e inputs:.**.** 
399c4 20 20 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20    aIdx      = { 
399c5 30 2c 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33  0,   1,   2,   3
399c6 20 7d 0a 2a 2a 20 20 20 61 44 69 73 74 61 6e 63   }.**   aDistanc
399c7 65 20 3d 20 7b 20 35 2e 30 2c 20 32 2e 30 2c 20  e = { 5.0, 2.0, 
399c8 37 2e 30 2c 20 36 2e 30 20 7d 0a 2a 2a 0a 2a 2a  7.0, 6.0 }.**.**
399c9 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   this function s
399ca 65 74 73 20 74 68 65 20 61 49 64 78 20 61 72 72  ets the aIdx arr
399cb 61 79 20 74 6f 20 63 6f 6e 74 61 69 6e 3a 0a 2a  ay to contain:.*
399cc 2a 0a 2a 2a 20 20 20 61 49 64 78 20 20 20 20 20  *.**   aIdx     
399cd 20 3d 20 7b 20 30 2c 20 20 20 31 2c 20 20 20 32   = { 0,   1,   2
399ce 2c 20 20 20 33 20 7d 0a 2a 2a 0a 2a 2a 20 54 68  ,   3 }.**.** Th
399cf 65 20 61 53 70 61 72 65 20 61 72 72 61 79 20 69  e aSpare array i
399d0 73 20 75 73 65 64 20 61 73 20 74 65 6d 70 6f 72  s used as tempor
399d1 61 72 79 20 77 6f 72 6b 69 6e 67 20 73 70 61 63  ary working spac
399d2 65 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74  e by the.** sort
399d3 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a  ing algorithm..*
399d4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 53 6f  /.static void So
399d5 72 74 42 79 44 69 73 74 61 6e 63 65 28 0a 20 20  rtByDistance(.  
399d6 69 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20 69 6e  int *aIdx, .  in
399d7 74 20 6e 49 64 78 2c 20 0a 20 20 66 6c 6f 61 74  t nIdx, .  float
399d8 20 2a 61 44 69 73 74 61 6e 63 65 2c 20 0a 20 20   *aDistance, .  
399d9 69 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a 20  int *aSpare.){. 
399da 20 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a 20   if( nIdx>1 ){. 
399db 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30     int iLeft = 0
399dc 3b 0a 20 20 20 20 69 6e 74 20 69 52 69 67 68 74  ;.    int iRight
399dd 20 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e   = 0;..    int n
399de 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20  Left = nIdx/2;. 
399df 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20     int nRight = 
399e0 6e 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20  nIdx-nLeft;.    
399e1 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61 49 64  int *aLeft = aId
399e2 78 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52 69 67  x;.    int *aRig
399e3 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74  ht = &aIdx[nLeft
399e4 5d 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79 44 69  ];..    SortByDi
399e5 73 74 61 6e 63 65 28 61 4c 65 66 74 2c 20 6e 4c  stance(aLeft, nL
399e6 65 66 74 2c 20 61 44 69 73 74 61 6e 63 65 2c 20  eft, aDistance, 
399e7 61 53 70 61 72 65 29 3b 0a 20 20 20 20 53 6f 72  aSpare);.    Sor
399e8 74 42 79 44 69 73 74 61 6e 63 65 28 61 52 69 67  tByDistance(aRig
399e9 68 74 2c 20 6e 52 69 67 68 74 2c 20 61 44 69 73  ht, nRight, aDis
399ea 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b 0a  tance, aSpare);.
399eb 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 53 70 61  .    memcpy(aSpa
399ec 72 65 2c 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f  re, aLeft, sizeo
399ed 66 28 69 6e 74 29 2a 6e 4c 65 66 74 29 3b 0a 20  f(int)*nLeft);. 
399ee 20 20 20 61 4c 65 66 74 20 3d 20 61 53 70 61 72     aLeft = aSpar
399ef 65 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69  e;..    while( i
399f0 4c 65 66 74 3c 6e 4c 65 66 74 20 7c 7c 20 69 52  Left<nLeft || iR
399f1 69 67 68 74 3c 6e 52 69 67 68 74 20 29 7b 0a 20  ight<nRight ){. 
399f2 20 20 20 20 20 69 66 28 20 69 4c 65 66 74 3d 3d       if( iLeft==
399f3 6e 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  nLeft ){.       
399f4 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67   aIdx[iLeft+iRig
399f5 68 74 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69  ht] = aRight[iRi
399f6 67 68 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 52  ght];.        iR
399f7 69 67 68 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65  ight++;.      }e
399f8 6c 73 65 20 69 66 28 20 69 52 69 67 68 74 3d 3d  lse if( iRight==
399f9 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  nRight ){.      
399fa 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69    aIdx[iLeft+iRi
399fb 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65  ght] = aLeft[iLe
399fc 66 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 4c 65  ft];.        iLe
399fd 66 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ft++;.      }els
399fe 65 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  e{.        float
399ff 20 66 4c 65 66 74 20 3d 20 61 44 69 73 74 61 6e   fLeft = aDistan
39a00 63 65 5b 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d  ce[aLeft[iLeft]]
39a01 3b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20  ;.        float 
39a02 66 52 69 67 68 74 20 3d 20 61 44 69 73 74 61 6e  fRight = aDistan
39a03 63 65 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74  ce[aRight[iRight
39a04 5d 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]];.        if( 
39a05 66 4c 65 66 74 3c 66 52 69 67 68 74 20 29 7b 0a  fLeft<fRight ){.
39a06 20 20 20 20 20 20 20 20 20 20 61 49 64 78 5b 69            aIdx[i
39a07 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
39a08 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20 20  Left[iLeft];.   
39a09 20 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a         iLeft++;.
39a0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
39a0b 20 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c           aIdx[iL
39a0c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52  eft+iRight] = aR
39a0d 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20  ight[iRight];.  
39a0e 20 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b          iRight++
39a0f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
39a10 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69 66 20 30    }.    }..#if 0
39a11 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68  .    /* Check th
39a12 61 74 20 74 68 65 20 73 6f 72 74 20 77 6f 72 6b  at the sort work
39a13 65 64 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20  ed */.    {.    
39a14 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20    int jj;.      
39a15 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c 6e 49 64  for(jj=1; jj<nId
39a16 78 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  x; jj++){.      
39a17 20 20 66 6c 6f 61 74 20 6c 65 66 74 20 3d 20 61    float left = a
39a18 44 69 73 74 61 6e 63 65 5b 61 49 64 78 5b 6a 6a  Distance[aIdx[jj
39a19 2d 31 5d 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c  -1]];.        fl
39a1a 6f 61 74 20 72 69 67 68 74 20 3d 20 61 44 69 73  oat right = aDis
39a1b 74 61 6e 63 65 5b 61 49 64 78 5b 6a 6a 5d 5d 3b  tance[aIdx[jj]];
39a1c 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
39a1d 20 6c 65 66 74 3c 3d 72 69 67 68 74 20 29 3b 0a   left<=right );.
39a1e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
39a1f 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ndif.  }.}../*.*
39a20 2a 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64 78  * Arguments aIdx
39a21 2c 20 61 43 65 6c 6c 20 61 6e 64 20 61 53 70 61  , aCell and aSpa
39a22 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20 74 6f 20  re all point to 
39a23 61 72 72 61 79 73 20 6f 66 20 73 69 7a 65 0a 2a  arrays of size.*
39a24 2a 20 6e 49 64 78 2e 20 54 68 65 20 61 49 64 78  * nIdx. The aIdx
39a25 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20   array contains 
39a26 74 68 65 20 73 65 74 20 6f 66 20 69 6e 74 65 67  the set of integ
39a27 65 72 73 20 66 72 6f 6d 20 30 20 74 6f 20 0a 2a  ers from 0 to .*
39a28 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e 20 6e 6f  * (nIdx-1) in no
39a29 20 70 61 72 74 69 63 75 6c 61 72 20 6f 72 64 65   particular orde
39a2a 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  r. This function
39a2b 20 73 6f 72 74 73 20 74 68 65 20 76 61 6c 75 65   sorts the value
39a2c 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20 61 63 63  s.** in aIdx acc
39a2d 6f 72 64 69 6e 67 20 74 6f 20 64 69 6d 65 6e 73  ording to dimens
39a2e 69 6f 6e 20 69 44 69 6d 20 6f 66 20 74 68 65 20  ion iDim of the 
39a2f 63 65 6c 6c 73 20 69 6e 20 61 43 65 6c 6c 2e 20  cells in aCell. 
39a30 54 68 65 0a 2a 2a 20 6d 69 6e 69 6d 75 6d 20 76  The.** minimum v
39a31 61 6c 75 65 20 6f 66 20 64 69 6d 65 6e 73 69 6f  alue of dimensio
39a32 6e 20 69 44 69 6d 20 69 73 20 63 6f 6e 73 69 64  n iDim is consid
39a33 65 72 65 64 20 66 69 72 73 74 2c 20 74 68 65 0a  ered first, the.
39a34 2a 2a 20 6d 61 78 69 6d 75 6d 20 75 73 65 64 20  ** maximum used 
39a35 74 6f 20 62 72 65 61 6b 20 74 69 65 73 2e 0a 2a  to break ties..*
39a36 2a 0a 2a 2a 20 54 68 65 20 61 53 70 61 72 65 20  *.** The aSpare 
39a37 61 72 72 61 79 20 69 73 20 75 73 65 64 20 61 73  array is used as
39a38 20 74 65 6d 70 6f 72 61 72 79 20 77 6f 72 6b 69   temporary worki
39a39 6e 67 20 73 70 61 63 65 20 62 79 20 74 68 65 0a  ng space by the.
39a3a 2a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67 6f 72  ** sorting algor
39a3b 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ithm..*/.static 
39a3c 76 6f 69 64 20 53 6f 72 74 42 79 44 69 6d 65 6e  void SortByDimen
39a3d 73 69 6f 6e 28 0a 20 20 52 74 72 65 65 20 2a 70  sion(.  Rtree *p
39a3e 52 74 72 65 65 2c 0a 20 20 69 6e 74 20 2a 61 49  Rtree,.  int *aI
39a3f 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c  dx, .  int nIdx,
39a40 20 0a 20 20 69 6e 74 20 69 44 69 6d 2c 20 0a 20   .  int iDim, . 
39a41 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c   RtreeCell *aCel
39a42 6c 2c 20 0a 20 20 69 6e 74 20 2a 61 53 70 61 72  l, .  int *aSpar
39a43 65 0a 29 7b 0a 20 20 69 66 28 20 6e 49 64 78 3e  e.){.  if( nIdx>
39a44 31 20 29 7b 0a 0a 20 20 20 20 69 6e 74 20 69 4c  1 ){..    int iL
39a45 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  eft = 0;.    int
39a46 20 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20   iRight = 0;..  
39a47 20 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20 6e 49    int nLeft = nI
39a48 64 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20 6e 52  dx/2;.    int nR
39a49 69 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c 65 66  ight = nIdx-nLef
39a4a 74 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c 65 66  t;.    int *aLef
39a4b 74 20 3d 20 61 49 64 78 3b 0a 20 20 20 20 69 6e  t = aIdx;.    in
39a4c 74 20 2a 61 52 69 67 68 74 20 3d 20 26 61 49 64  t *aRight = &aId
39a4d 78 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20 20 53  x[nLeft];..    S
39a4e 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70  ortByDimension(p
39a4f 52 74 72 65 65 2c 20 61 4c 65 66 74 2c 20 6e 4c  Rtree, aLeft, nL
39a50 65 66 74 2c 20 69 44 69 6d 2c 20 61 43 65 6c 6c  eft, iDim, aCell
39a51 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 20 20 53  , aSpare);.    S
39a52 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 70  ortByDimension(p
39a53 52 74 72 65 65 2c 20 61 52 69 67 68 74 2c 20 6e  Rtree, aRight, n
39a54 52 69 67 68 74 2c 20 69 44 69 6d 2c 20 61 43 65  Right, iDim, aCe
39a55 6c 6c 2c 20 61 53 70 61 72 65 29 3b 0a 0a 20 20  ll, aSpare);..  
39a56 20 20 6d 65 6d 63 70 79 28 61 53 70 61 72 65 2c    memcpy(aSpare,
39a57 20 61 4c 65 66 74 2c 20 73 69 7a 65 6f 66 28 69   aLeft, sizeof(i
39a58 6e 74 29 2a 6e 4c 65 66 74 29 3b 0a 20 20 20 20  nt)*nLeft);.    
39a59 61 4c 65 66 74 20 3d 20 61 53 70 61 72 65 3b 0a  aLeft = aSpare;.
39a5a 20 20 20 20 77 68 69 6c 65 28 20 69 4c 65 66 74      while( iLeft
39a5b 3c 6e 4c 65 66 74 20 7c 7c 20 69 52 69 67 68 74  <nLeft || iRight
39a5c 3c 6e 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  <nRight ){.     
39a5d 20 64 6f 75 62 6c 65 20 78 6c 65 66 74 31 20 3d   double xleft1 =
39a5e 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 4c   DCOORD(aCell[aL
39a5f 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61 43 6f 6f  eft[iLeft]].aCoo
39a60 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20  rd[iDim*2]);.   
39a61 20 20 20 64 6f 75 62 6c 65 20 78 6c 65 66 74 32     double xleft2
39a62 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
39a63 61 4c 65 66 74 5b 69 4c 65 66 74 5d 5d 2e 61 43  aLeft[iLeft]].aC
39a64 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b  oord[iDim*2+1]);
39a65 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72  .      double xr
39a66 69 67 68 74 31 20 3d 20 44 43 4f 4f 52 44 28 61  ight1 = DCOORD(a
39a67 43 65 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67  Cell[aRight[iRig
39a68 68 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ht]].aCoord[iDim
39a69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75 62  *2]);.      doub
39a6a 6c 65 20 78 72 69 67 68 74 32 20 3d 20 44 43 4f  le xright2 = DCO
39a6b 4f 52 44 28 61 43 65 6c 6c 5b 61 52 69 67 68 74  ORD(aCell[aRight
39a6c 5b 69 52 69 67 68 74 5d 5d 2e 61 43 6f 6f 72 64  [iRight]].aCoord
39a6d 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20  [iDim*2+1]);.   
39a6e 20 20 20 69 66 28 20 28 69 4c 65 66 74 21 3d 6e     if( (iLeft!=n
39a6f 4c 65 66 74 29 20 26 26 20 28 28 69 52 69 67 68  Left) && ((iRigh
39a70 74 3d 3d 6e 52 69 67 68 74 29 0a 20 20 20 20 20  t==nRight).     
39a71 20 20 7c 7c 20 28 78 6c 65 66 74 31 3c 78 72 69    || (xleft1<xri
39a72 67 68 74 31 29 0a 20 20 20 20 20 20 20 7c 7c 20  ght1).       || 
39a73 28 78 6c 65 66 74 31 3d 3d 78 72 69 67 68 74 31  (xleft1==xright1
39a74 20 26 26 20 78 6c 65 66 74 32 3c 78 72 69 67 68   && xleft2<xrigh
39a75 74 32 29 0a 20 20 20 20 20 20 29 29 7b 0a 20 20  t2).      )){.  
39a76 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65 66 74        aIdx[iLeft
39a77 2b 69 52 69 67 68 74 5d 20 3d 20 61 4c 65 66 74  +iRight] = aLeft
39a78 5b 69 4c 65 66 74 5d 3b 0a 20 20 20 20 20 20 20  [iLeft];.       
39a79 20 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20 20   iLeft++;.      
39a7a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61  }else{.        a
39a7b 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74  Idx[iLeft+iRight
39a7c 5d 20 3d 20 61 52 69 67 68 74 5b 69 52 69 67 68  ] = aRight[iRigh
39a7d 74 5d 3b 0a 20 20 20 20 20 20 20 20 69 52 69 67  t];.        iRig
39a7e 68 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ht++;.      }.  
39a7f 20 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f    }..#if 0.    /
39a80 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
39a81 20 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f 0a   sort worked */.
39a82 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20      {.      int 
39a83 6a 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a  jj;.      for(jj
39a84 3d 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b  =1; jj<nIdx; jj+
39a85 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61  +){.        floa
39a86 74 20 78 6c 65 66 74 31 20 3d 20 61 43 65 6c 6c  t xleft1 = aCell
39a87 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61 43 6f  [aIdx[jj-1]].aCo
39a88 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a 20 20 20  ord[iDim*2];.   
39a89 20 20 20 20 20 66 6c 6f 61 74 20 78 6c 65 66 74       float xleft
39a8a 32 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a  2 = aCell[aIdx[j
39a8b 6a 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  j-1]].aCoord[iDi
39a8c 6d 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20  m*2+1];.        
39a8d 66 6c 6f 61 74 20 78 72 69 67 68 74 31 20 3d 20  float xright1 = 
39a8e 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d 2e  aCell[aIdx[jj]].
39a8f 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 3b 0a  aCoord[iDim*2];.
39a90 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78 72          float xr
39a91 69 67 68 74 32 20 3d 20 61 43 65 6c 6c 5b 61 49  ight2 = aCell[aI
39a92 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b 69  dx[jj]].aCoord[i
39a93 44 69 6d 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20  Dim*2+1];.      
39a94 20 20 61 73 73 65 72 74 28 20 78 6c 65 66 74 31    assert( xleft1
39a95 3c 3d 78 72 69 67 68 74 31 20 26 26 20 28 78 6c  <=xright1 && (xl
39a96 65 66 74 31 3c 78 72 69 67 68 74 31 20 7c 7c 20  eft1<xright1 || 
39a97 78 6c 65 66 74 32 3c 3d 78 72 69 67 68 74 32 29  xleft2<=xright2)
39a98 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
39a99 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a  }.#endif.  }.}..
39a9a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41  #if VARIANT_RSTA
39a9b 52 54 52 45 45 5f 53 50 4c 49 54 0a 2f 2a 0a 2a  RTREE_SPLIT./*.*
39a9c 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
39a9d 20 6f 66 20 74 68 65 20 52 2a 2d 74 72 65 65 20   of the R*-tree 
39a9e 76 61 72 69 61 6e 74 20 6f 66 20 53 70 6c 69 74  variant of Split
39a9f 4e 6f 64 65 20 66 72 6f 6d 20 42 65 63 6b 6d 61  Node from Beckma
39aa0 6e 5b 31 39 39 30 5d 2e 0a 2a 2f 0a 73 74 61 74  n[1990]..*/.stat
39aa1 69 63 20 69 6e 74 20 73 70 6c 69 74 4e 6f 64 65  ic int splitNode
39aa2 53 74 61 72 74 72 65 65 28 0a 20 20 52 74 72 65  Startree(.  Rtre
39aa3 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72  e *pRtree,.  Rtr
39aa4 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 0a 20  eeCell *aCell,. 
39aa5 20 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20 20 52 74   int nCell,.  Rt
39aa6 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74 2c 0a  reeNode *pLeft,.
39aa7 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 69    RtreeNode *pRi
39aa8 67 68 74 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c  ght,.  RtreeCell
39aa9 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0a 20 20 52   *pBboxLeft,.  R
39aaa 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78 52  treeCell *pBboxR
39aab 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 2a 2a  ight.){.  int **
39aac 61 61 53 6f 72 74 65 64 3b 0a 20 20 69 6e 74 20  aaSorted;.  int 
39aad 2a 61 53 70 61 72 65 3b 0a 20 20 69 6e 74 20 69  *aSpare;.  int i
39aae 69 3b 0a 0a 20 20 69 6e 74 20 69 42 65 73 74 44  i;..  int iBestD
39aaf 69 6d 3b 0a 20 20 69 6e 74 20 69 42 65 73 74 53  im;.  int iBestS
39ab0 70 6c 69 74 3b 0a 20 20 66 6c 6f 61 74 20 66 42  plit;.  float fB
39ab1 65 73 74 4d 61 72 67 69 6e 3b 0a 0a 20 20 69 6e  estMargin;..  in
39ab2 74 20 6e 42 79 74 65 20 3d 20 28 70 52 74 72 65  t nByte = (pRtre
39ab3 65 2d 3e 6e 44 69 6d 2b 31 29 2a 28 73 69 7a 65  e->nDim+1)*(size
39ab4 6f 66 28 69 6e 74 2a 29 2b 6e 43 65 6c 6c 2a 73  of(int*)+nCell*s
39ab5 69 7a 65 6f 66 28 69 6e 74 29 29 3b 0a 0a 20 20  izeof(int));..  
39ab6 61 61 53 6f 72 74 65 64 20 3d 20 28 69 6e 74 20  aaSorted = (int 
39ab7 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  **)sqlite3_mallo
39ab8 63 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20  c(nByte);.  if( 
39ab9 21 61 61 53 6f 72 74 65 64 20 29 7b 0a 20 20 20  !aaSorted ){.   
39aba 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
39abb 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 61 53 70  OMEM;.  }..  aSp
39abc 61 72 65 20 3d 20 26 28 28 69 6e 74 20 2a 29 26  are = &((int *)&
39abd 61 61 53 6f 72 74 65 64 5b 70 52 74 72 65 65 2d  aaSorted[pRtree-
39abe 3e 6e 44 69 6d 5d 29 5b 70 52 74 72 65 65 2d 3e  >nDim])[pRtree->
39abf 6e 44 69 6d 2a 6e 43 65 6c 6c 5d 3b 0a 20 20 6d  nDim*nCell];.  m
39ac0 65 6d 73 65 74 28 61 61 53 6f 72 74 65 64 2c 20  emset(aaSorted, 
39ac1 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 66 6f 72  0, nByte);.  for
39ac2 28 69 69 3d 30 3b 20 69 69 3c 70 52 74 72 65 65  (ii=0; ii<pRtree
39ac3 2d 3e 6e 44 69 6d 3b 20 69 69 2b 2b 29 7b 0a 20  ->nDim; ii++){. 
39ac4 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20 20 61     int jj;.    a
39ac5 61 53 6f 72 74 65 64 5b 69 69 5d 20 3d 20 26 28  aSorted[ii] = &(
39ac6 28 69 6e 74 20 2a 29 26 61 61 53 6f 72 74 65 64  (int *)&aaSorted
39ac7 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29 5b  [pRtree->nDim])[
39ac8 69 69 2a 6e 43 65 6c 6c 5d 3b 0a 20 20 20 20 66  ii*nCell];.    f
39ac9 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c 6e 43 65 6c  or(jj=0; jj<nCel
39aca 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  l; jj++){.      
39acb 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6a 6a 5d  aaSorted[ii][jj]
39acc 20 3d 20 6a 6a 3b 0a 20 20 20 20 7d 0a 20 20 20   = jj;.    }.   
39acd 20 53 6f 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e   SortByDimension
39ace 28 70 52 74 72 65 65 2c 20 61 61 53 6f 72 74 65  (pRtree, aaSorte
39acf 64 5b 69 69 5d 2c 20 6e 43 65 6c 6c 2c 20 69 69  d[ii], nCell, ii
39ad0 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72 65 29  , aCell, aSpare)
39ad1 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 69 3d  ;.  }..  for(ii=
39ad2 30 3b 20 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44  0; ii<pRtree->nD
39ad3 69 6d 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 66  im; ii++){.    f
39ad4 6c 6f 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e  loat margin = 0.
39ad5 30 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 42 65  0;.    float fBe
39ad6 73 74 4f 76 65 72 6c 61 70 3b 0a 20 20 20 20 66  stOverlap;.    f
39ad7 6c 6f 61 74 20 66 42 65 73 74 41 72 65 61 3b 0a  loat fBestArea;.
39ad8 20 20 20 20 69 6e 74 20 69 42 65 73 74 4c 65 66      int iBestLef
39ad9 74 3b 0a 20 20 20 20 69 6e 74 20 6e 4c 65 66 74  t;.    int nLeft
39ada 3b 0a 0a 20 20 20 20 66 6f 72 28 0a 20 20 20 20  ;..    for(.    
39adb 20 20 6e 4c 65 66 74 3d 52 54 52 45 45 5f 4d 49    nLeft=RTREE_MI
39adc 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 3b 20  NCELLS(pRtree); 
39add 0a 20 20 20 20 20 20 6e 4c 65 66 74 3c 3d 28 6e  .      nLeft<=(n
39ade 43 65 6c 6c 2d 52 54 52 45 45 5f 4d 49 4e 43 45  Cell-RTREE_MINCE
39adf 4c 4c 53 28 70 52 74 72 65 65 29 29 3b 20 0a 20  LLS(pRtree)); . 
39ae0 20 20 20 20 20 6e 4c 65 66 74 2b 2b 0a 20 20 20       nLeft++.   
39ae1 20 29 7b 0a 20 20 20 20 20 20 52 74 72 65 65 43   ){.      RtreeC
39ae2 65 6c 6c 20 6c 65 66 74 3b 0a 20 20 20 20 20 20  ell left;.      
39ae3 52 74 72 65 65 43 65 6c 6c 20 72 69 67 68 74 3b  RtreeCell right;
39ae4 0a 20 20 20 20 20 20 69 6e 74 20 6b 6b 3b 0a 20  .      int kk;. 
39ae5 20 20 20 20 20 66 6c 6f 61 74 20 6f 76 65 72 6c       float overl
39ae6 61 70 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  ap;.      float 
39ae7 61 72 65 61 3b 0a 0a 20 20 20 20 20 20 6d 65 6d  area;..      mem
39ae8 63 70 79 28 26 6c 65 66 74 2c 20 26 61 43 65 6c  cpy(&left, &aCel
39ae9 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 30  l[aaSorted[ii][0
39aea 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  ]], sizeof(Rtree
39aeb 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20 20 6d 65  Cell));.      me
39aec 6d 63 70 79 28 26 72 69 67 68 74 2c 20 26 61 43  mcpy(&right, &aC
39aed 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69 5d  ell[aaSorted[ii]
39aee 5b 6e 43 65 6c 6c 2d 31 5d 5d 2c 20 73 69 7a 65  [nCell-1]], size
39aef 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a  of(RtreeCell));.
39af0 20 20 20 20 20 20 66 6f 72 28 6b 6b 3d 31 3b 20        for(kk=1; 
39af1 6b 6b 3c 28 6e 43 65 6c 6c 2d 31 29 3b 20 6b 6b  kk<(nCell-1); kk
39af2 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ++){.        if(
39af3 20 6b 6b 3c 6e 4c 65 66 74 20 29 7b 0a 20 20 20   kk<nLeft ){.   
39af4 20 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e         cellUnion
39af5 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74 2c 20  (pRtree, &left, 
39af6 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b  &aCell[aaSorted[
39af7 69 69 5d 5b 6b 6b 5d 5d 29 3b 0a 20 20 20 20 20  ii][kk]]);.     
39af8 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
39af9 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
39afa 74 72 65 65 2c 20 26 72 69 67 68 74 2c 20 26 61  tree, &right, &a
39afb 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69 69  Cell[aaSorted[ii
39afc 5d 5b 6b 6b 5d 5d 29 3b 0a 20 20 20 20 20 20 20  ][kk]]);.       
39afd 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
39afe 20 6d 61 72 67 69 6e 20 2b 3d 20 63 65 6c 6c 4d   margin += cellM
39aff 61 72 67 69 6e 28 70 52 74 72 65 65 2c 20 26 6c  argin(pRtree, &l
39b00 65 66 74 29 3b 0a 20 20 20 20 20 20 6d 61 72 67  eft);.      marg
39b01 69 6e 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e  in += cellMargin
39b02 28 70 52 74 72 65 65 2c 20 26 72 69 67 68 74 29  (pRtree, &right)
39b03 3b 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70 20  ;.      overlap 
39b04 3d 20 63 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52  = cellOverlap(pR
39b05 74 72 65 65 2c 20 26 6c 65 66 74 2c 20 26 72 69  tree, &left, &ri
39b06 67 68 74 2c 20 31 2c 20 2d 31 29 3b 0a 20 20 20  ght, 1, -1);.   
39b07 20 20 20 61 72 65 61 20 3d 20 63 65 6c 6c 41 72     area = cellAr
39b08 65 61 28 70 52 74 72 65 65 2c 20 26 6c 65 66 74  ea(pRtree, &left
39b09 29 20 2b 20 63 65 6c 6c 41 72 65 61 28 70 52 74  ) + cellArea(pRt
39b0a 72 65 65 2c 20 26 72 69 67 68 74 29 3b 0a 20 20  ree, &right);.  
39b0b 20 20 20 20 69 66 28 20 28 6e 4c 65 66 74 3d 3d      if( (nLeft==
39b0c 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
39b0d 52 74 72 65 65 29 29 0a 20 20 20 20 20 20 20 7c  Rtree)).       |
39b0e 7c 20 28 6f 76 65 72 6c 61 70 3c 66 42 65 73 74  | (overlap<fBest
39b0f 4f 76 65 72 6c 61 70 29 0a 20 20 20 20 20 20 20  Overlap).       
39b10 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 42 65  || (overlap==fBe
39b11 73 74 4f 76 65 72 6c 61 70 20 26 26 20 61 72 65  stOverlap && are
39b12 61 3c 66 42 65 73 74 41 72 65 61 29 0a 20 20 20  a<fBestArea).   
39b13 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 42     ){.        iB
39b14 65 73 74 4c 65 66 74 20 3d 20 6e 4c 65 66 74 3b  estLeft = nLeft;
39b15 0a 20 20 20 20 20 20 20 20 66 42 65 73 74 4f 76  .        fBestOv
39b16 65 72 6c 61 70 20 3d 20 6f 76 65 72 6c 61 70 3b  erlap = overlap;
39b17 0a 20 20 20 20 20 20 20 20 66 42 65 73 74 41 72  .        fBestAr
39b18 65 61 20 3d 20 61 72 65 61 3b 0a 20 20 20 20 20  ea = area;.     
39b19 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66   }.    }..    if
39b1a 28 20 69 69 3d 3d 30 20 7c 7c 20 6d 61 72 67 69  ( ii==0 || margi
39b1b 6e 3c 66 42 65 73 74 4d 61 72 67 69 6e 20 29 7b  n<fBestMargin ){
39b1c 0a 20 20 20 20 20 20 69 42 65 73 74 44 69 6d 20  .      iBestDim 
39b1d 3d 20 69 69 3b 0a 20 20 20 20 20 20 66 42 65 73  = ii;.      fBes
39b1e 74 4d 61 72 67 69 6e 20 3d 20 6d 61 72 67 69 6e  tMargin = margin
39b1f 3b 0a 20 20 20 20 20 20 69 42 65 73 74 53 70 6c  ;.      iBestSpl
39b20 69 74 20 3d 20 69 42 65 73 74 4c 65 66 74 3b 0a  it = iBestLeft;.
39b21 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 6d 65 6d      }.  }..  mem
39b22 63 70 79 28 70 42 62 6f 78 4c 65 66 74 2c 20 26  cpy(pBboxLeft, &
39b23 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b 69  aCell[aaSorted[i
39b24 42 65 73 74 44 69 6d 5d 5b 30 5d 5d 2c 20 73 69  BestDim][0]], si
39b25 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
39b26 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78  ;.  memcpy(pBbox
39b27 52 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61  Right, &aCell[aa
39b28 53 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d 5d  Sorted[iBestDim]
39b29 5b 69 42 65 73 74 53 70 6c 69 74 5d 5d 2c 20 73  [iBestSplit]], s
39b2a 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
39b2b 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  );.  for(ii=0; i
39b2c 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a  i<nCell; ii++){.
39b2d 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
39b2e 54 61 72 67 65 74 20 3d 20 28 69 69 3c 69 42 65  Target = (ii<iBe
39b2f 73 74 53 70 6c 69 74 29 3f 70 4c 65 66 74 3a 70  stSplit)?pLeft:p
39b30 52 69 67 68 74 3b 0a 20 20 20 20 52 74 72 65 65  Right;.    Rtree
39b31 43 65 6c 6c 20 2a 70 42 62 6f 78 20 3d 20 28 69  Cell *pBbox = (i
39b32 69 3c 69 42 65 73 74 53 70 6c 69 74 29 3f 70 42  i<iBestSplit)?pB
39b33 62 6f 78 4c 65 66 74 3a 70 42 62 6f 78 52 69 67  boxLeft:pBboxRig
39b34 68 74 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c  ht;.    RtreeCel
39b35 6c 20 2a 70 43 65 6c 6c 20 3d 20 26 61 43 65 6c  l *pCell = &aCel
39b36 6c 5b 61 61 53 6f 72 74 65 64 5b 69 42 65 73 74  l[aaSorted[iBest
39b37 44 69 6d 5d 5b 69 69 5d 5d 3b 0a 20 20 20 20 6e  Dim][ii]];.    n
39b38 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  odeInsertCell(pR
39b39 74 72 65 65 2c 20 70 54 61 72 67 65 74 2c 20 70  tree, pTarget, p
39b3a 43 65 6c 6c 29 3b 0a 20 20 20 20 63 65 6c 6c 55  Cell);.    cellU
39b3b 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62  nion(pRtree, pBb
39b3c 6f 78 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 7d 0a  ox, pCell);.  }.
39b3d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
39b3e 61 61 53 6f 72 74 65 64 29 3b 0a 20 20 72 65 74  aaSorted);.  ret
39b3f 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
39b40 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 56 41 52  .#endif..#if VAR
39b41 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 53 50 4c  IANT_GUTTMAN_SPL
39b42 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  IT./*.** Impleme
39b43 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 72  ntation of the r
39b44 65 67 75 6c 61 72 20 52 2d 74 72 65 65 20 53 70  egular R-tree Sp
39b45 6c 69 74 4e 6f 64 65 20 66 72 6f 6d 20 47 75 74  litNode from Gut
39b46 74 6d 61 6e 5b 31 39 38 34 5d 2e 0a 2a 2f 0a 73  tman[1984]..*/.s
39b47 74 61 74 69 63 20 69 6e 74 20 73 70 6c 69 74 4e  tatic int splitN
39b48 6f 64 65 47 75 74 74 6d 61 6e 28 0a 20 20 52 74  odeGuttman(.  Rt
39b49 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52  ree *pRtree,.  R
39b4a 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c  treeCell *aCell,
39b4b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 0a 20 20  .  int nCell,.  
39b4c 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74  RtreeNode *pLeft
39b4d 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ,.  RtreeNode *p
39b4e 52 69 67 68 74 2c 0a 20 20 52 74 72 65 65 43 65  Right,.  RtreeCe
39b4f 6c 6c 20 2a 70 42 62 6f 78 4c 65 66 74 2c 0a 20  ll *pBboxLeft,. 
39b50 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f   RtreeCell *pBbo
39b51 78 52 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20  xRight.){.  int 
39b52 69 4c 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20  iLeftSeed = 0;. 
39b53 20 69 6e 74 20 69 52 69 67 68 74 53 65 65 64 20   int iRightSeed 
39b54 3d 20 31 3b 0a 20 20 69 6e 74 20 2a 61 69 55 73  = 1;.  int *aiUs
39b55 65 64 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  ed;.  int i;..  
39b56 61 69 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33  aiUsed = sqlite3
39b57 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 69  _malloc(sizeof(i
39b58 6e 74 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20 69 66  nt)*nCell);.  if
39b59 28 20 21 61 69 55 73 65 64 20 29 7b 0a 20 20 20  ( !aiUsed ){.   
39b5a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
39b5b 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  OMEM;.  }.  mems
39b5c 65 74 28 61 69 55 73 65 64 2c 20 30 2c 20 73 69  et(aiUsed, 0, si
39b5d 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65 6c 6c 29  zeof(int)*nCell)
39b5e 3b 0a 0a 20 20 50 69 63 6b 53 65 65 64 73 28 70  ;..  PickSeeds(p
39b5f 52 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43  Rtree, aCell, nC
39b60 65 6c 6c 2c 20 26 69 4c 65 66 74 53 65 65 64 2c  ell, &iLeftSeed,
39b61 20 26 69 52 69 67 68 74 53 65 65 64 29 3b 0a 0a   &iRightSeed);..
39b62 20 20 6d 65 6d 63 70 79 28 70 42 62 6f 78 4c 65    memcpy(pBboxLe
39b63 66 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65 66 74  ft, &aCell[iLeft
39b64 53 65 65 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74  Seed], sizeof(Rt
39b65 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d  reeCell));.  mem
39b66 63 70 79 28 70 42 62 6f 78 52 69 67 68 74 2c 20  cpy(pBboxRight, 
39b67 26 61 43 65 6c 6c 5b 69 52 69 67 68 74 53 65 65  &aCell[iRightSee
39b68 64 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65  d], sizeof(Rtree
39b69 43 65 6c 6c 29 29 3b 0a 20 20 6e 6f 64 65 49 6e  Cell));.  nodeIn
39b6a 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
39b6b 20 70 4c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 69   pLeft, &aCell[i
39b6c 4c 65 66 74 53 65 65 64 5d 29 3b 0a 20 20 6e 6f  LeftSeed]);.  no
39b6d 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
39b6e 72 65 65 2c 20 70 52 69 67 68 74 2c 20 26 61 43  ree, pRight, &aC
39b6f 65 6c 6c 5b 69 52 69 67 68 74 53 65 65 64 5d 29  ell[iRightSeed])
39b70 3b 0a 20 20 61 69 55 73 65 64 5b 69 4c 65 66 74  ;.  aiUsed[iLeft
39b71 53 65 65 64 5d 20 3d 20 31 3b 0a 20 20 61 69 55  Seed] = 1;.  aiU
39b72 73 65 64 5b 69 52 69 67 68 74 53 65 65 64 5d 20  sed[iRightSeed] 
39b73 3d 20 31 3b 0a 0a 20 20 66 6f 72 28 69 3d 6e 43  = 1;..  for(i=nC
39b74 65 6c 6c 2d 32 3b 20 69 3e 30 3b 20 69 2d 2d 29  ell-2; i>0; i--)
39b75 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20  {.    RtreeCell 
39b76 2a 70 4e 65 78 74 3b 0a 20 20 20 20 70 4e 65 78  *pNext;.    pNex
39b77 74 20 3d 20 50 69 63 6b 4e 65 78 74 28 70 52 74  t = PickNext(pRt
39b78 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c  ree, aCell, nCel
39b79 6c 2c 20 70 42 62 6f 78 4c 65 66 74 2c 20 70 42  l, pBboxLeft, pB
39b7a 62 6f 78 52 69 67 68 74 2c 20 61 69 55 73 65 64  boxRight, aiUsed
39b7b 29 3b 0a 20 20 20 20 66 6c 6f 61 74 20 64 69 66  );.    float dif
39b7c 66 20 3d 20 20 0a 20 20 20 20 20 20 63 65 6c 6c  f =  .      cell
39b7d 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70  Growth(pRtree, p
39b7e 42 62 6f 78 4c 65 66 74 2c 20 70 4e 65 78 74 29  BboxLeft, pNext)
39b7f 20 2d 20 0a 20 20 20 20 20 20 63 65 6c 6c 47 72   - .      cellGr
39b80 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 42 62  owth(pRtree, pBb
39b81 6f 78 52 69 67 68 74 2c 20 70 4e 65 78 74 29 0a  oxRight, pNext).
39b82 20 20 20 20 3b 0a 20 20 20 20 69 66 28 20 28 52      ;.    if( (R
39b83 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52  TREE_MINCELLS(pR
39b84 74 72 65 65 29 2d 4e 43 45 4c 4c 28 70 52 69 67  tree)-NCELL(pRig
39b85 68 74 29 3d 3d 69 29 0a 20 20 20 20 20 7c 7c 20  ht)==i).     || 
39b86 28 64 69 66 66 3e 30 2e 30 20 26 26 20 28 52 54  (diff>0.0 && (RT
39b87 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
39b88 72 65 65 29 2d 4e 43 45 4c 4c 28 70 4c 65 66 74  ree)-NCELL(pLeft
39b89 29 21 3d 69 29 29 0a 20 20 20 20 29 7b 0a 20 20  )!=i)).    ){.  
39b8a 20 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65      nodeInsertCe
39b8b 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69 67 68  ll(pRtree, pRigh
39b8c 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 20  t, pNext);.     
39b8d 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
39b8e 65 2c 20 70 42 62 6f 78 52 69 67 68 74 2c 20 70  e, pBboxRight, p
39b8f 4e 65 78 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Next);.    }else
39b90 7b 0a 20 20 20 20 20 20 6e 6f 64 65 49 6e 73 65  {.      nodeInse
39b91 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  rtCell(pRtree, p
39b92 4c 65 66 74 2c 20 70 4e 65 78 74 29 3b 0a 20 20  Left, pNext);.  
39b93 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
39b94 74 72 65 65 2c 20 70 42 62 6f 78 4c 65 66 74 2c  tree, pBboxLeft,
39b95 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20   pNext);.    }. 
39b96 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   }..  sqlite3_fr
39b97 65 65 28 61 69 55 73 65 64 29 3b 0a 20 20 72 65  ee(aiUsed);.  re
39b98 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
39b99 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61 74 69 63  }.#endif..static
39b9a 20 69 6e 74 20 75 70 64 61 74 65 4d 61 70 70 69   int updateMappi
39b9b 6e 67 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74  ng(.  Rtree *pRt
39b9c 72 65 65 2c 20 0a 20 20 69 36 34 20 69 52 6f 77  ree, .  i64 iRow
39b9d 69 64 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65  id, .  RtreeNode
39b9e 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69 6e 74 20   *pNode, .  int 
39b9f 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74  iHeight.){.  int
39ba0 20 28 2a 78 53 65 74 4d 61 70 70 69 6e 67 29 28   (*xSetMapping)(
39ba1 52 74 72 65 65 20 2a 2c 20 73 71 6c 69 74 65 33  Rtree *, sqlite3
39ba2 5f 69 6e 74 36 34 2c 20 73 71 6c 69 74 65 33 5f  _int64, sqlite3_
39ba3 69 6e 74 36 34 29 3b 0a 20 20 78 53 65 74 4d 61  int64);.  xSetMa
39ba4 70 70 69 6e 67 20 3d 20 28 28 69 48 65 69 67 68  pping = ((iHeigh
39ba5 74 3d 3d 30 29 3f 72 6f 77 69 64 57 72 69 74 65  t==0)?rowidWrite
39ba6 3a 70 61 72 65 6e 74 57 72 69 74 65 29 3b 0a 20  :parentWrite);. 
39ba7 20 69 66 28 20 69 48 65 69 67 68 74 3e 30 20 29   if( iHeight>0 )
39ba8 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  {.    RtreeNode 
39ba9 2a 70 43 68 69 6c 64 20 3d 20 6e 6f 64 65 48 61  *pChild = nodeHa
39baa 73 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c  shLookup(pRtree,
39bab 20 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 69 66   iRowid);.    if
39bac 28 20 70 43 68 69 6c 64 20 29 7b 0a 20 20 20 20  ( pChild ){.    
39bad 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
39bae 74 72 65 65 2c 20 70 43 68 69 6c 64 2d 3e 70 50  tree, pChild->pP
39baf 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 6e 6f  arent);.      no
39bb0 64 65 52 65 66 65 72 65 6e 63 65 28 70 4e 6f 64  deReference(pNod
39bb1 65 29 3b 0a 20 20 20 20 20 20 70 43 68 69 6c 64  e);.      pChild
39bb2 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64  ->pParent = pNod
39bb3 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  e;.    }.  }.  r
39bb4 65 74 75 72 6e 20 78 53 65 74 4d 61 70 70 69 6e  eturn xSetMappin
39bb5 67 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64  g(pRtree, iRowid
39bb6 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  , pNode->iNode);
39bb7 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 53  .}..static int S
39bb8 70 6c 69 74 4e 6f 64 65 28 0a 20 20 52 74 72 65  plitNode(.  Rtre
39bb9 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52 74 72  e *pRtree,.  Rtr
39bba 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 0a 20  eeNode *pNode,. 
39bbb 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
39bbc 6c 2c 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74  l,.  int iHeight
39bbd 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  .){.  int i;.  i
39bbe 6e 74 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68  nt newCellIsRigh
39bbf 74 20 3d 20 30 3b 0a 0a 20 20 69 6e 74 20 72 63  t = 0;..  int rc
39bc0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
39bc1 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c  int nCell = NCEL
39bc2 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 52 74 72 65  L(pNode);.  Rtre
39bc3 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 3b 0a 20 20  eCell *aCell;.  
39bc4 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 0a 20 20  int *aiUsed;..  
39bc5 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 66 74  RtreeNode *pLeft
39bc6 20 3d 20 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64   = 0;.  RtreeNod
39bc7 65 20 2a 70 52 69 67 68 74 20 3d 20 30 3b 0a 0a  e *pRight = 0;..
39bc8 20 20 52 74 72 65 65 43 65 6c 6c 20 6c 65 66 74    RtreeCell left
39bc9 62 62 6f 78 3b 0a 20 20 52 74 72 65 65 43 65 6c  bbox;.  RtreeCel
39bca 6c 20 72 69 67 68 74 62 62 6f 78 3b 0a 0a 20 20  l rightbbox;..  
39bcb 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 6e 20 61  /* Allocate an a
39bcc 72 72 61 79 20 61 6e 64 20 70 6f 70 75 6c 61 74  rray and populat
39bcd 65 20 69 74 20 77 69 74 68 20 61 20 63 6f 70 79  e it with a copy
39bce 20 6f 66 20 70 43 65 6c 6c 20 61 6e 64 20 0a 20   of pCell and . 
39bcf 20 2a 2a 20 61 6c 6c 20 63 65 6c 6c 73 20 66 72   ** all cells fr
39bd0 6f 6d 20 6e 6f 64 65 20 70 4c 65 66 74 2e 20 54  om node pLeft. T
39bd1 68 65 6e 20 7a 65 72 6f 20 74 68 65 20 6f 72 69  hen zero the ori
39bd2 67 69 6e 61 6c 20 6e 6f 64 65 2e 0a 20 20 2a 2f  ginal node..  */
39bd3 0a 20 20 61 43 65 6c 6c 20 3d 20 73 71 6c 69 74  .  aCell = sqlit
39bd4 65 33 5f 6d 61 6c 6c 6f 63 28 28 73 69 7a 65 6f  e3_malloc((sizeo
39bd5 66 28 52 74 72 65 65 43 65 6c 6c 29 2b 73 69 7a  f(RtreeCell)+siz
39bd6 65 6f 66 28 69 6e 74 29 29 2a 28 6e 43 65 6c 6c  eof(int))*(nCell
39bd7 2b 31 29 29 3b 0a 20 20 69 66 28 20 21 61 43 65  +1));.  if( !aCe
39bd8 6c 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  ll ){.    rc = S
39bd9 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
39bda 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
39bdb 6f 75 74 3b 0a 20 20 7d 0a 20 20 61 69 55 73 65  out;.  }.  aiUse
39bdc 64 20 3d 20 28 69 6e 74 20 2a 29 26 61 43 65 6c  d = (int *)&aCel
39bdd 6c 5b 6e 43 65 6c 6c 2b 31 5d 3b 0a 20 20 6d 65  l[nCell+1];.  me
39bde 6d 73 65 74 28 61 69 55 73 65 64 2c 20 30 2c 20  mset(aiUsed, 0, 
39bdf 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e 43 65  sizeof(int)*(nCe
39be0 6c 6c 2b 31 29 29 3b 0a 20 20 66 6f 72 28 69 3d  ll+1));.  for(i=
39be1 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
39be2 7b 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c  {.    nodeGetCel
39be3 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
39be4 20 69 2c 20 26 61 43 65 6c 6c 5b 69 5d 29 3b 0a   i, &aCell[i]);.
39be5 20 20 7d 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70    }.  nodeZero(p
39be6 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
39be7 20 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c 5b 6e   memcpy(&aCell[n
39be8 43 65 6c 6c 5d 2c 20 70 43 65 6c 6c 2c 20 73 69  Cell], pCell, si
39be9 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29  zeof(RtreeCell))
39bea 3b 0a 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 0a 20 20  ;.  nCell++;..  
39beb 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  if( pNode->iNode
39bec 3d 3d 31 20 29 7b 0a 20 20 20 20 70 52 69 67 68  ==1 ){.    pRigh
39bed 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52 74 72  t = nodeNew(pRtr
39bee 65 65 2c 20 70 4e 6f 64 65 2c 20 31 29 3b 0a 20  ee, pNode, 1);. 
39bef 20 20 20 70 4c 65 66 74 20 3d 20 6e 6f 64 65 4e     pLeft = nodeN
39bf0 65 77 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ew(pRtree, pNode
39bf1 2c 20 31 29 3b 0a 20 20 20 20 70 52 74 72 65 65  , 1);.    pRtree
39bf2 2d 3e 69 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20  ->iDepth++;.    
39bf3 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d  pNode->isDirty =
39bf4 20 31 3b 0a 20 20 20 20 77 72 69 74 65 49 6e 74   1;.    writeInt
39bf5 31 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c  16(pNode->zData,
39bf6 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 29   pRtree->iDepth)
39bf7 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
39bf8 4c 65 66 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  Left = pNode;.  
39bf9 20 20 70 52 69 67 68 74 20 3d 20 6e 6f 64 65 4e    pRight = nodeN
39bfa 65 77 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  ew(pRtree, pLeft
39bfb 2d 3e 70 50 61 72 65 6e 74 2c 20 31 29 3b 0a 20  ->pParent, 1);. 
39bfc 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65     nodeReference
39bfd 28 70 4c 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20  (pLeft);.  }..  
39bfe 69 66 28 20 21 70 4c 65 66 74 20 7c 7c 20 21 70  if( !pLeft || !p
39bff 52 69 67 68 74 20 29 7b 0a 20 20 20 20 72 63 20  Right ){.    rc 
39c00 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
39c01 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f      goto splitno
39c02 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 6d  de_out;.  }..  m
39c03 65 6d 73 65 74 28 70 4c 65 66 74 2d 3e 7a 44 61  emset(pLeft->zDa
39c04 74 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69  ta, 0, pRtree->i
39c05 4e 6f 64 65 53 69 7a 65 29 3b 0a 20 20 6d 65 6d  NodeSize);.  mem
39c06 73 65 74 28 70 52 69 67 68 74 2d 3e 7a 44 61 74  set(pRight->zDat
39c07 61 2c 20 30 2c 20 70 52 74 72 65 65 2d 3e 69 4e  a, 0, pRtree->iN
39c08 6f 64 65 53 69 7a 65 29 3b 0a 0a 20 20 72 63 20  odeSize);..  rc 
39c09 3d 20 41 73 73 69 67 6e 43 65 6c 6c 73 28 70 52  = AssignCells(pR
39c0a 74 72 65 65 2c 20 61 43 65 6c 6c 2c 20 6e 43 65  tree, aCell, nCe
39c0b 6c 6c 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  ll, pLeft, pRigh
39c0c 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20 26 72  t, &leftbbox, &r
39c0d 69 67 68 74 62 62 6f 78 29 3b 0a 20 20 69 66 28  ightbbox);.  if(
39c0e 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
39c0f 7b 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74  {.    goto split
39c10 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  node_out;.  }.. 
39c11 20 2f 2a 20 45 6e 73 75 72 65 20 62 6f 74 68 20   /* Ensure both 
39c12 63 68 69 6c 64 20 6e 6f 64 65 73 20 68 61 76 65  child nodes have
39c13 20 6e 6f 64 65 20 6e 75 6d 62 65 72 73 20 61 73   node numbers as
39c14 73 69 67 6e 65 64 20 74 6f 20 74 68 65 6d 2e 20  signed to them. 
39c15 2a 2f 0a 20 20 69 66 28 20 28 30 3d 3d 70 52 69  */.  if( (0==pRi
39c16 67 68 74 2d 3e 69 4e 6f 64 65 20 26 26 20 53 51  ght->iNode && SQ
39c17 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e  LITE_OK!=(rc = n
39c18 6f 64 65 57 72 69 74 65 28 70 52 74 72 65 65 2c  odeWrite(pRtree,
39c19 20 70 52 69 67 68 74 29 29 29 0a 20 20 20 7c 7c   pRight))).   ||
39c1a 20 28 30 3d 3d 70 4c 65 66 74 2d 3e 69 4e 6f 64   (0==pLeft->iNod
39c1b 65 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21 3d  e && SQLITE_OK!=
39c1c 28 72 63 20 3d 20 6e 6f 64 65 57 72 69 74 65 28  (rc = nodeWrite(
39c1d 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 29 29  pRtree, pLeft)))
39c1e 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73  .  ){.    goto s
39c1f 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
39c20 7d 0a 0a 20 20 72 69 67 68 74 62 62 6f 78 2e 69  }..  rightbbox.i
39c21 52 6f 77 69 64 20 3d 20 70 52 69 67 68 74 2d 3e  Rowid = pRight->
39c22 69 4e 6f 64 65 3b 0a 20 20 6c 65 66 74 62 62 6f  iNode;.  leftbbo
39c23 78 2e 69 52 6f 77 69 64 20 3d 20 70 4c 65 66 74  x.iRowid = pLeft
39c24 2d 3e 69 4e 6f 64 65 3b 0a 0a 20 20 69 66 28 20  ->iNode;..  if( 
39c25 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20  pNode->iNode==1 
39c26 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 74 72 65  ){.    rc = rtre
39c27 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
39c28 65 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61 72 65  ee, pLeft->pPare
39c29 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c 20 69  nt, &leftbbox, i
39c2a 48 65 69 67 68 74 2b 31 29 3b 0a 20 20 20 20 69  Height+1);.    i
39c2b 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
39c2c 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73   ){.      goto s
39c2d 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
39c2e 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
39c2f 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72   RtreeNode *pPar
39c30 65 6e 74 20 3d 20 70 4c 65 66 74 2d 3e 70 50 61  ent = pLeft->pPa
39c31 72 65 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 43  rent;.    int iC
39c32 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74  ell = nodeParent
39c33 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4c  Index(pRtree, pL
39c34 65 66 74 29 3b 0a 20 20 20 20 6e 6f 64 65 4f 76  eft);.    nodeOv
39c35 65 72 77 72 69 74 65 43 65 6c 6c 28 70 52 74 72  erwriteCell(pRtr
39c36 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 26 6c 65  ee, pParent, &le
39c37 66 74 62 62 6f 78 2c 20 69 43 65 6c 6c 29 3b 0a  ftbbox, iCell);.
39c38 20 20 20 20 41 64 6a 75 73 74 54 72 65 65 28 70      AdjustTree(p
39c39 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20  Rtree, pParent, 
39c3a 26 6c 65 66 74 62 62 6f 78 29 3b 0a 20 20 7d 0a  &leftbbox);.  }.
39c3b 20 20 69 66 28 20 28 72 63 20 3d 20 72 74 72 65    if( (rc = rtre
39c3c 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
39c3d 65 65 2c 20 70 52 69 67 68 74 2d 3e 70 50 61 72  ee, pRight->pPar
39c3e 65 6e 74 2c 20 26 72 69 67 68 74 62 62 6f 78 2c  ent, &rightbbox,
39c3f 20 69 48 65 69 67 68 74 2b 31 29 29 20 29 7b 0a   iHeight+1)) ){.
39c40 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f      goto splitno
39c41 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 66  de_out;.  }..  f
39c42 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c 28  or(i=0; i<NCELL(
39c43 70 52 69 67 68 74 29 3b 20 69 2b 2b 29 7b 0a 20  pRight); i++){. 
39c44 20 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d 20     i64 iRowid = 
39c45 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74  nodeGetRowid(pRt
39c46 72 65 65 2c 20 70 52 69 67 68 74 2c 20 69 29 3b  ree, pRight, i);
39c47 0a 20 20 20 20 72 63 20 3d 20 75 70 64 61 74 65  .    rc = update
39c48 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20  Mapping(pRtree, 
39c49 69 52 6f 77 69 64 2c 20 70 52 69 67 68 74 2c 20  iRowid, pRight, 
39c4a 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 69 66  iHeight);.    if
39c4b 28 20 69 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d  ( iRowid==pCell-
39c4c 3e 69 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  >iRowid ){.     
39c4d 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68 74 20   newCellIsRight 
39c4e 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 1;.    }.    i
39c4f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
39c50 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73   ){.      goto s
39c51 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
39c52 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4e    }.  }.  if( pN
39c53 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b  ode->iNode==1 ){
39c54 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
39c55 4e 43 45 4c 4c 28 70 4c 65 66 74 29 3b 20 69 2b  NCELL(pLeft); i+
39c56 2b 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69 52  +){.      i64 iR
39c57 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f  owid = nodeGetRo
39c58 77 69 64 28 70 52 74 72 65 65 2c 20 70 4c 65 66  wid(pRtree, pLef
39c59 74 2c 20 69 29 3b 0a 20 20 20 20 20 20 72 63 20  t, i);.      rc 
39c5a 3d 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28  = updateMapping(
39c5b 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20  pRtree, iRowid, 
39c5c 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74 29 3b  pLeft, iHeight);
39c5d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
39c5e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
39c5f 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f      goto splitno
39c60 64 65 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  de_out;.      }.
39c61 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
39c62 28 20 6e 65 77 43 65 6c 6c 49 73 52 69 67 68 74  ( newCellIsRight
39c63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
39c64 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28 70 52  updateMapping(pR
39c65 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f  tree, pCell->iRo
39c66 77 69 64 2c 20 70 4c 65 66 74 2c 20 69 48 65 69  wid, pLeft, iHei
39c67 67 68 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  ght);.  }..  if(
39c68 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
39c69 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 52  {.    rc = nodeR
39c6a 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
39c6b 52 69 67 68 74 29 3b 0a 20 20 20 20 70 52 69 67  Right);.    pRig
39c6c 68 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  ht = 0;.  }.  if
39c6d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
39c6e 29 7b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64 65  ){.    rc = node
39c6f 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
39c70 70 4c 65 66 74 29 3b 0a 20 20 20 20 70 4c 65 66  pLeft);.    pLef
39c71 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 73 70 6c 69  t = 0;.  }..spli
39c72 74 6e 6f 64 65 5f 6f 75 74 3a 0a 20 20 6e 6f 64  tnode_out:.  nod
39c73 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
39c74 20 70 52 69 67 68 74 29 3b 0a 20 20 6e 6f 64 65   pRight);.  node
39c75 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
39c76 70 4c 65 66 74 29 3b 0a 20 20 73 71 6c 69 74 65  pLeft);.  sqlite
39c77 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b 0a 20  3_free(aCell);. 
39c78 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73   return rc;.}..s
39c79 74 61 74 69 63 20 69 6e 74 20 66 69 78 4c 65 61  tatic int fixLea
39c7a 66 50 61 72 65 6e 74 28 52 74 72 65 65 20 2a 70  fParent(Rtree *p
39c7b 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
39c7c 20 2a 70 4c 65 61 66 29 7b 0a 20 20 69 6e 74 20   *pLeaf){.  int 
39c7d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
39c7e 20 20 69 66 28 20 70 4c 65 61 66 2d 3e 69 4e 6f    if( pLeaf->iNo
39c7f 64 65 21 3d 31 20 26 26 20 70 4c 65 61 66 2d 3e  de!=1 && pLeaf->
39c80 70 50 61 72 65 6e 74 3d 3d 30 20 29 7b 0a 20 20  pParent==0 ){.  
39c81 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
39c82 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65  nt64(pRtree->pRe
39c83 61 64 50 61 72 65 6e 74 2c 20 31 2c 20 70 4c 65  adParent, 1, pLe
39c84 61 66 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  af->iNode);.    
39c85 69 66 28 20 73 71 6c 69 74 65 33 5f 73 74 65 70  if( sqlite3_step
39c86 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61  (pRtree->pReadPa
39c87 72 65 6e 74 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  rent)==SQLITE_RO
39c88 57 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69  W ){.      i64 i
39c89 4e 6f 64 65 20 3d 20 73 71 6c 69 74 65 33 5f 63  Node = sqlite3_c
39c8a 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 70 52 74 72  olumn_int64(pRtr
39c8b 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 2c  ee->pReadParent,
39c8c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20   0);.      rc = 
39c8d 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
39c8e 65 65 2c 20 69 4e 6f 64 65 2c 20 30 2c 20 26 70  ee, iNode, 0, &p
39c8f 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a  Leaf->pParent);.
39c90 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
39c91 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
39c92 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  OR;.    }.    sq
39c93 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
39c94 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 29  ee->pReadParent)
39c95 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
39c96 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
39c97 20 72 63 20 3d 20 66 69 78 4c 65 61 66 50 61 72   rc = fixLeafPar
39c98 65 6e 74 28 70 52 74 72 65 65 2c 20 70 4c 65 61  ent(pRtree, pLea
39c99 66 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20  f->pParent);.   
39c9a 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
39c9b 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  rc;.}..static in
39c9c 74 20 64 65 6c 65 74 65 43 65 6c 6c 28 52 74 72  t deleteCell(Rtr
39c9d 65 65 20 2a 2c 20 52 74 72 65 65 4e 6f 64 65 20  ee *, RtreeNode 
39c9e 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 0a 73  *, int, int);..s
39c9f 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f 76 65  tatic int remove
39ca0 4e 6f 64 65 28 52 74 72 65 65 20 2a 70 52 74 72  Node(Rtree *pRtr
39ca1 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  ee, RtreeNode *p
39ca2 4e 6f 64 65 2c 20 69 6e 74 20 69 48 65 69 67 68  Node, int iHeigh
39ca3 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
39ca4 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65  RtreeNode *pPare
39ca5 6e 74 3b 0a 20 20 69 6e 74 20 69 43 65 6c 6c 3b  nt;.  int iCell;
39ca6 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 6f 64  ..  assert( pNod
39ca7 65 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 0a 20  e->nRef==1 );.. 
39ca8 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 65   /* Remove the e
39ca9 6e 74 72 79 20 69 6e 20 74 68 65 20 70 61 72 65  ntry in the pare
39caa 6e 74 20 63 65 6c 6c 2e 20 2a 2f 0a 20 20 69 43  nt cell. */.  iC
39cab 65 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74  ell = nodeParent
39cac 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4e  Index(pRtree, pN
39cad 6f 64 65 29 3b 0a 20 20 70 50 61 72 65 6e 74 20  ode);.  pParent 
39cae 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  = pNode->pParent
39caf 3b 0a 20 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65  ;.  pNode->pPare
39cb0 6e 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51  nt = 0;.  if( SQ
39cb1 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 64  LITE_OK!=(rc = d
39cb2 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72 65 65  eleteCell(pRtree
39cb3 2c 20 70 50 61 72 65 6e 74 2c 20 69 43 65 6c 6c  , pParent, iCell
39cb4 2c 20 69 48 65 69 67 68 74 2b 31 29 29 20 0a 20  , iHeight+1)) . 
39cb5 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d    || SQLITE_OK!=
39cb6 28 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73  (rc = nodeReleas
39cb7 65 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  e(pRtree, pParen
39cb8 74 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  t)).  ){.    ret
39cb9 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
39cba 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 78 78 78  * Remove the xxx
39cbb 5f 6e 6f 64 65 20 65 6e 74 72 79 2e 20 2a 2f 0a  _node entry. */.
39cbc 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69    sqlite3_bind_i
39cbd 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44 65  nt64(pRtree->pDe
39cbe 6c 65 74 65 4e 6f 64 65 2c 20 31 2c 20 70 4e 6f  leteNode, 1, pNo
39cbf 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 73 71  de->iNode);.  sq
39cc0 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65  lite3_step(pRtre
39cc1 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64 65 29 3b  e->pDeleteNode);
39cc2 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
39cc3 21 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  !=(rc = sqlite3_
39cc4 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 44  reset(pRtree->pD
39cc5 65 6c 65 74 65 4e 6f 64 65 29 29 20 29 7b 0a 20  eleteNode)) ){. 
39cc6 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
39cc7 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  }..  /* Remove t
39cc8 68 65 20 78 78 78 5f 70 61 72 65 6e 74 20 65 6e  he xxx_parent en
39cc9 74 72 79 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  try. */.  sqlite
39cca 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52 74  3_bind_int64(pRt
39ccb 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65  ree->pDeletePare
39ccc 6e 74 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e  nt, 1, pNode->iN
39ccd 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ode);.  sqlite3_
39cce 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 44 65  step(pRtree->pDe
39ccf 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a 20 20 69  leteParent);.  i
39cd0 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72  f( SQLITE_OK!=(r
39cd1 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65  c = sqlite3_rese
39cd2 74 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  t(pRtree->pDelet
39cd3 65 50 61 72 65 6e 74 29 29 20 29 7b 0a 20 20 20  eParent)) ){.   
39cd4 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
39cd5 20 20 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74    .  /* Remove t
39cd6 68 65 20 6e 6f 64 65 20 66 72 6f 6d 20 74 68 65  he node from the
39cd7 20 69 6e 2d 6d 65 6d 6f 72 79 20 68 61 73 68 20   in-memory hash 
39cd8 74 61 62 6c 65 20 61 6e 64 20 6c 69 6e 6b 20 69  table and link i
39cd9 74 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20  t into.  ** the 
39cda 52 74 72 65 65 2e 70 44 65 6c 65 74 65 64 20 6c  Rtree.pDeleted l
39cdb 69 73 74 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  ist. Its content
39cdc 73 20 77 69 6c 6c 20 62 65 20 72 65 2d 69 6e 73  s will be re-ins
39cdd 65 72 74 65 64 20 6c 61 74 65 72 20 6f 6e 2e 0a  erted later on..
39cde 20 20 2a 2f 0a 20 20 6e 6f 64 65 48 61 73 68 44    */.  nodeHashD
39cdf 65 6c 65 74 65 28 70 52 74 72 65 65 2c 20 70 4e  elete(pRtree, pN
39ce0 6f 64 65 29 3b 0a 20 20 70 4e 6f 64 65 2d 3e 69  ode);.  pNode->i
39ce1 4e 6f 64 65 20 3d 20 69 48 65 69 67 68 74 3b 0a  Node = iHeight;.
39ce2 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20 3d    pNode->pNext =
39ce3 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65   pRtree->pDelete
39ce4 64 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  d;.  pNode->nRef
39ce5 2b 2b 3b 0a 20 20 70 52 74 72 65 65 2d 3e 70 44  ++;.  pRtree->pD
39ce6 65 6c 65 74 65 64 20 3d 20 70 4e 6f 64 65 3b 0a  eleted = pNode;.
39ce7 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
39ce8 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  _OK;.}..static v
39ce9 6f 69 64 20 66 69 78 42 6f 75 6e 64 69 6e 67 42  oid fixBoundingB
39cea 6f 78 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  ox(Rtree *pRtree
39ceb 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
39cec 64 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65  de){.  RtreeNode
39ced 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64   *pParent = pNod
39cee 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 69 66  e->pParent;.  if
39cef 28 20 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20  ( pParent ){.   
39cf0 20 69 6e 74 20 69 69 3b 20 0a 20 20 20 20 69 6e   int ii; .    in
39cf1 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28  t nCell = NCELL(
39cf2 70 4e 6f 64 65 29 3b 0a 20 20 20 20 52 74 72 65  pNode);.    Rtre
39cf3 65 43 65 6c 6c 20 62 6f 78 3b 20 20 20 20 20 20  eCell box;      
39cf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39cf5 20 20 20 20 20 20 2f 2a 20 42 6f 75 6e 64 69 6e        /* Boundin
39cf6 67 20 62 6f 78 20 66 6f 72 20 70 4e 6f 64 65 20  g box for pNode 
39cf7 2a 2f 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65  */.    nodeGetCe
39cf8 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
39cf9 2c 20 30 2c 20 26 62 6f 78 29 3b 0a 20 20 20 20  , 0, &box);.    
39cfa 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 6e 43 65  for(ii=1; ii<nCe
39cfb 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  ll; ii++){.     
39cfc 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b   RtreeCell cell;
39cfd 0a 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65  .      nodeGetCe
39cfe 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
39cff 2c 20 69 69 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  , ii, &cell);.  
39d00 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
39d01 74 72 65 65 2c 20 26 62 6f 78 2c 20 26 63 65 6c  tree, &box, &cel
39d02 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 6f  l);.    }.    bo
39d03 78 2e 69 52 6f 77 69 64 20 3d 20 70 4e 6f 64 65  x.iRowid = pNode
39d04 2d 3e 69 4e 6f 64 65 3b 0a 20 20 20 20 69 69 20  ->iNode;.    ii 
39d05 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65  = nodeParentInde
39d06 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29  x(pRtree, pNode)
39d07 3b 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72  ;.    nodeOverwr
39d08 69 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  iteCell(pRtree, 
39d09 70 50 61 72 65 6e 74 2c 20 26 62 6f 78 2c 20 69  pParent, &box, i
39d0a 69 29 3b 0a 20 20 20 20 66 69 78 42 6f 75 6e 64  i);.    fixBound
39d0b 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c 20 70  ingBox(pRtree, p
39d0c 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 0a  Parent);.  }.}..
39d0d 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65  /*.** Delete the
39d0e 20 63 65 6c 6c 20 61 74 20 69 6e 64 65 78 20 69   cell at index i
39d0f 43 65 6c 6c 20 6f 66 20 6e 6f 64 65 20 70 4e 6f  Cell of node pNo
39d10 64 65 2e 20 41 66 74 65 72 20 72 65 6d 6f 76 69  de. After removi
39d11 6e 67 20 74 68 65 0a 2a 2a 20 63 65 6c 6c 2c 20  ng the.** cell, 
39d12 61 64 6a 75 73 74 20 74 68 65 20 72 2d 74 72 65  adjust the r-tre
39d13 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65  e data structure
39d14 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f   if required..*/
39d15 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 6c 65  .static int dele
39d16 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70 52  teCell(Rtree *pR
39d17 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
39d18 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65 6c  *pNode, int iCel
39d19 6c 2c 20 69 6e 74 20 69 48 65 69 67 68 74 29 7b  l, int iHeight){
39d1a 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66  .  int rc;..  if
39d1b 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
39d1c 20 3d 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74   = fixLeafParent
39d1d 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 29  (pRtree, pNode))
39d1e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
39d1f 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d  c;.  }..  /* Rem
39d20 6f 76 65 20 74 68 65 20 63 65 6c 6c 20 66 72 6f  ove the cell fro
39d21 6d 20 74 68 65 20 6e 6f 64 65 2e 20 54 68 69 73  m the node. This
39d22 20 63 61 6c 6c 20 6a 75 73 74 20 6d 6f 76 65 73   call just moves
39d23 20 62 79 74 65 73 20 61 72 6f 75 6e 64 0a 20 20   bytes around.  
39d24 2a 2a 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  ** the in-memory
39d25 20 6e 6f 64 65 20 69 6d 61 67 65 2c 20 73 6f 20   node image, so 
39d26 69 74 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a  it cannot fail..
39d27 20 20 2a 2f 0a 20 20 6e 6f 64 65 44 65 6c 65 74    */.  nodeDelet
39d28 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e  eCell(pRtree, pN
39d29 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a 0a 20 20  ode, iCell);..  
39d2a 2f 2a 20 49 66 20 74 68 65 20 6e 6f 64 65 20 69  /* If the node i
39d2b 73 20 6e 6f 74 20 74 68 65 20 74 72 65 65 20 72  s not the tree r
39d2c 6f 6f 74 20 61 6e 64 20 6e 6f 77 20 68 61 73 20  oot and now has 
39d2d 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6d 69  less than the mi
39d2e 6e 69 6d 75 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65  nimum.  ** numbe
39d2f 72 20 6f 66 20 63 65 6c 6c 73 2c 20 72 65 6d 6f  r of cells, remo
39d30 76 65 20 69 74 20 66 72 6f 6d 20 74 68 65 20 74  ve it from the t
39d31 72 65 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ree. Otherwise, 
39d32 75 70 64 61 74 65 20 74 68 65 0a 20 20 2a 2a 20  update the.  ** 
39d33 63 65 6c 6c 20 69 6e 20 74 68 65 20 70 61 72 65  cell in the pare
39d34 6e 74 20 6e 6f 64 65 20 73 6f 20 74 68 61 74 20  nt node so that 
39d35 69 74 20 74 69 67 68 74 6c 79 20 63 6f 6e 74 61  it tightly conta
39d36 69 6e 73 20 74 68 65 20 75 70 64 61 74 65 64 0a  ins the updated.
39d37 20 20 2a 2a 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a    ** node..  */.
39d38 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
39d39 64 65 21 3d 31 20 29 7b 0a 20 20 20 20 52 74 72  de!=1 ){.    Rtr
39d3a 65 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20  eeNode *pParent 
39d3b 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74  = pNode->pParent
39d3c 3b 0a 20 20 20 20 69 66 28 20 28 70 50 61 72 65  ;.    if( (pPare
39d3d 6e 74 2d 3e 69 4e 6f 64 65 21 3d 31 20 7c 7c 20  nt->iNode!=1 || 
39d3e 4e 43 45 4c 4c 28 70 50 61 72 65 6e 74 29 21 3d  NCELL(pParent)!=
39d3f 31 29 20 0a 20 20 20 20 20 26 26 20 28 4e 43 45  1) .     && (NCE
39d40 4c 4c 28 70 4e 6f 64 65 29 3c 52 54 52 45 45 5f  LL(pNode)<RTREE_
39d41 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29  MINCELLS(pRtree)
39d42 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72  ).    ){.      r
39d43 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70  c = removeNode(p
39d44 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 48  Rtree, pNode, iH
39d45 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73  eight);.    }els
39d46 65 7b 0a 20 20 20 20 20 20 66 69 78 42 6f 75 6e  e{.      fixBoun
39d47 64 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c 20  dingBox(pRtree, 
39d48 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pNode);.    }.  
39d49 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
39d4a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 52 65  }..static int Re
39d4b 69 6e 73 65 72 74 28 0a 20 20 52 74 72 65 65 20  insert(.  Rtree 
39d4c 2a 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65  *pRtree, .  Rtre
39d4d 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20  eNode *pNode, . 
39d4e 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
39d4f 6c 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67 68  l, .  int iHeigh
39d50 74 0a 29 7b 0a 20 20 69 6e 74 20 2a 61 4f 72 64  t.){.  int *aOrd
39d51 65 72 3b 0a 20 20 69 6e 74 20 2a 61 53 70 61 72  er;.  int *aSpar
39d52 65 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  e;.  RtreeCell *
39d53 61 43 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20 2a  aCell;.  float *
39d54 61 44 69 73 74 61 6e 63 65 3b 0a 20 20 69 6e 74  aDistance;.  int
39d55 20 6e 43 65 6c 6c 3b 0a 20 20 66 6c 6f 61 74 20   nCell;.  float 
39d56 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 52 54 52  aCenterCoord[RTR
39d57 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e  EE_MAX_DIMENSION
39d58 53 5d 3b 0a 20 20 69 6e 74 20 69 44 69 6d 3b 0a  S];.  int iDim;.
39d59 20 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20    int ii;.  int 
39d5a 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
39d5b 0a 20 20 6d 65 6d 73 65 74 28 61 43 65 6e 74 65  .  memset(aCente
39d5c 72 43 6f 6f 72 64 2c 20 30 2c 20 73 69 7a 65 6f  rCoord, 0, sizeo
39d5d 66 28 66 6c 6f 61 74 29 2a 52 54 52 45 45 5f 4d  f(float)*RTREE_M
39d5e 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 29 3b 0a  AX_DIMENSIONS);.
39d5f 0a 20 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c  .  nCell = NCELL
39d60 28 70 4e 6f 64 65 29 2b 31 3b 0a 0a 20 20 2f 2a  (pNode)+1;..  /*
39d61 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 62 75   Allocate the bu
39d62 66 66 65 72 73 20 75 73 65 64 20 62 79 20 74 68  ffers used by th
39d63 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68  is operation. Th
39d64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 0a  e allocation is.
39d65 20 20 2a 2a 20 72 65 6c 69 6e 71 75 69 73 68 65    ** relinquishe
39d66 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 66 75  d before this fu
39d67 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a  nction returns..
39d68 20 20 2a 2f 0a 20 20 61 43 65 6c 6c 20 3d 20 28    */.  aCell = (
39d69 52 74 72 65 65 43 65 6c 6c 20 2a 29 73 71 6c 69  RtreeCell *)sqli
39d6a 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 43 65 6c 6c  te3_malloc(nCell
39d6b 20 2a 20 28 0a 20 20 20 20 73 69 7a 65 6f 66 28   * (.    sizeof(
39d6c 52 74 72 65 65 43 65 6c 6c 29 20 2b 20 20 20 20  RtreeCell) +    
39d6d 20 20 20 20 20 2f 2a 20 61 43 65 6c 6c 20 61 72       /* aCell ar
39d6e 72 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f  ray */.    sizeo
39d6f 66 28 69 6e 74 29 20 20 20 20 20 20 20 2b 20 20  f(int)       +  
39d70 20 20 20 20 20 20 20 2f 2a 20 61 4f 72 64 65 72         /* aOrder
39d71 20 61 72 72 61 79 20 2a 2f 0a 20 20 20 20 73 69   array */.    si
39d72 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20 20  zeof(int)       
39d73 2b 20 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70  +         /* aSp
39d74 61 72 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 20  are array */.   
39d75 20 73 69 7a 65 6f 66 28 66 6c 6f 61 74 29 20 20   sizeof(float)  
39d76 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39d77 61 44 69 73 74 61 6e 63 65 20 61 72 72 61 79 20  aDistance array 
39d78 2a 2f 0a 20 20 29 29 3b 0a 20 20 69 66 28 20 21  */.  ));.  if( !
39d79 61 43 65 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74  aCell ){.    ret
39d7a 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
39d7b 3b 0a 20 20 7d 0a 20 20 61 4f 72 64 65 72 20 20  ;.  }.  aOrder  
39d7c 20 20 3d 20 28 69 6e 74 20 2a 29 26 61 43 65 6c    = (int *)&aCel
39d7d 6c 5b 6e 43 65 6c 6c 5d 3b 0a 20 20 61 53 70 61  l[nCell];.  aSpa
39d7e 72 65 20 20 20 20 3d 20 28 69 6e 74 20 2a 29 26  re    = (int *)&
39d7f 61 4f 72 64 65 72 5b 6e 43 65 6c 6c 5d 3b 0a 20  aOrder[nCell];. 
39d80 20 61 44 69 73 74 61 6e 63 65 20 3d 20 28 66 6c   aDistance = (fl
39d81 6f 61 74 20 2a 29 26 61 53 70 61 72 65 5b 6e 43  oat *)&aSpare[nC
39d82 65 6c 6c 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d  ell];..  for(ii=
39d83 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b  0; ii<nCell; ii+
39d84 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 69 3d 3d  +){.    if( ii==
39d85 28 6e 43 65 6c 6c 2d 31 29 20 29 7b 0a 20 20 20  (nCell-1) ){.   
39d86 20 20 20 6d 65 6d 63 70 79 28 26 61 43 65 6c 6c     memcpy(&aCell
39d87 5b 69 69 5d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a  [ii], pCell, siz
39d88 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b  eof(RtreeCell));
39d89 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
39d8a 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
39d8b 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c  tree, pNode, ii,
39d8c 20 26 61 43 65 6c 6c 5b 69 69 5d 29 3b 0a 20 20   &aCell[ii]);.  
39d8d 20 20 7d 0a 20 20 20 20 61 4f 72 64 65 72 5b 69    }.    aOrder[i
39d8e 69 5d 20 3d 20 69 69 3b 0a 20 20 20 20 66 6f 72  i] = ii;.    for
39d8f 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52  (iDim=0; iDim<pR
39d90 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d  tree->nDim; iDim
39d91 2b 2b 29 7b 0a 20 20 20 20 20 20 61 43 65 6e 74  ++){.      aCent
39d92 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d  erCoord[iDim] +=
39d93 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69   DCOORD(aCell[ii
39d94 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d  ].aCoord[iDim*2]
39d95 29 3b 0a 20 20 20 20 20 20 61 43 65 6e 74 65 72  );.      aCenter
39d96 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44  Coord[iDim] += D
39d97 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e  COORD(aCell[ii].
39d98 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d  aCoord[iDim*2+1]
39d99 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  );.    }.  }.  f
39d9a 6f 72 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c  or(iDim=0; iDim<
39d9b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44  pRtree->nDim; iD
39d9c 69 6d 2b 2b 29 7b 0a 20 20 20 20 61 43 65 6e 74  im++){.    aCent
39d9d 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 20 3d 20  erCoord[iDim] = 
39d9e 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69  aCenterCoord[iDi
39d9f 6d 5d 2f 28 28 66 6c 6f 61 74 29 6e 43 65 6c 6c  m]/((float)nCell
39da0 2a 32 2e 30 29 3b 0a 20 20 7d 0a 0a 20 20 66 6f  *2.0);.  }..  fo
39da1 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
39da2 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61 44 69  ; ii++){.    aDi
39da3 73 74 61 6e 63 65 5b 69 69 5d 20 3d 20 30 2e 30  stance[ii] = 0.0
39da4 3b 0a 20 20 20 20 66 6f 72 28 69 44 69 6d 3d 30  ;.    for(iDim=0
39da5 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e  ; iDim<pRtree->n
39da6 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20  Dim; iDim++){.  
39da7 20 20 20 20 66 6c 6f 61 74 20 63 6f 6f 72 64 20      float coord 
39da8 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69  = DCOORD(aCell[i
39da9 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  i].aCoord[iDim*2
39daa 2b 31 5d 29 20 2d 20 0a 20 20 20 20 20 20 20 20  +1]) - .        
39dab 20 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 69    DCOORD(aCell[i
39dac 69 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  i].aCoord[iDim*2
39dad 5d 29 3b 0a 20 20 20 20 20 20 61 44 69 73 74 61  ]);.      aDista
39dae 6e 63 65 5b 69 69 5d 20 2b 3d 20 28 63 6f 6f 72  nce[ii] += (coor
39daf 64 2d 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69  d-aCenterCoord[i
39db0 44 69 6d 5d 29 2a 28 63 6f 6f 72 64 2d 61 43 65  Dim])*(coord-aCe
39db1 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 29  nterCoord[iDim])
39db2 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 53  ;.    }.  }..  S
39db3 6f 72 74 42 79 44 69 73 74 61 6e 63 65 28 61 4f  ortByDistance(aO
39db4 72 64 65 72 2c 20 6e 43 65 6c 6c 2c 20 61 44 69  rder, nCell, aDi
39db5 73 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29 3b  stance, aSpare);
39db6 0a 20 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72  .  nodeZero(pRtr
39db7 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a 20 20 66  ee, pNode);..  f
39db8 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  or(ii=0; rc==SQL
39db9 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 28 6e 43  ITE_OK && ii<(nC
39dba 65 6c 6c 2d 28 52 54 52 45 45 5f 4d 49 4e 43 45  ell-(RTREE_MINCE
39dbb 4c 4c 53 28 70 52 74 72 65 65 29 2b 31 29 29 3b  LLS(pRtree)+1));
39dbc 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65   ii++){.    Rtre
39dbd 65 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43 65 6c  eCell *p = &aCel
39dbe 6c 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a 20  l[aOrder[ii]];. 
39dbf 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c     nodeInsertCel
39dc0 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  l(pRtree, pNode,
39dc1 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e   p);.    if( p->
39dc2 69 52 6f 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69  iRowid==pCell->i
39dc3 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 69  Rowid ){.      i
39dc4 66 28 20 69 48 65 69 67 68 74 3d 3d 30 20 29 7b  f( iHeight==0 ){
39dc5 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 6f  .        rc = ro
39dc6 77 69 64 57 72 69 74 65 28 70 52 74 72 65 65 2c  widWrite(pRtree,
39dc7 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64   p->iRowid, pNod
39dc8 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20  e->iNode);.     
39dc9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
39dca 72 63 20 3d 20 70 61 72 65 6e 74 57 72 69 74 65  rc = parentWrite
39dcb 28 70 52 74 72 65 65 2c 20 70 2d 3e 69 52 6f 77  (pRtree, p->iRow
39dcc 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  id, pNode->iNode
39dcd 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
39dce 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
39dcf 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
39dd0 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 70  fixBoundingBox(p
39dd1 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
39dd2 20 7d 0a 20 20 66 6f 72 28 3b 20 72 63 3d 3d 53   }.  for(; rc==S
39dd3 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c 6e  QLITE_OK && ii<n
39dd4 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  Cell; ii++){.   
39dd5 20 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64 65 20   /* Find a node 
39dd6 74 6f 20 73 74 6f 72 65 20 74 68 69 73 20 63 65  to store this ce
39dd7 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e  ll in. pNode->iN
39dd8 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f  ode currently co
39dd9 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68  ntains.    ** th
39dda 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 20  e height of the 
39ddb 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
39ddc 62 79 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20  by the cell..   
39ddd 20 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f 64   */.    RtreeNod
39dde 65 20 2a 70 49 6e 73 65 72 74 3b 0a 20 20 20 20  e *pInsert;.    
39ddf 52 74 72 65 65 43 65 6c 6c 20 2a 70 20 3d 20 26  RtreeCell *p = &
39de0 61 43 65 6c 6c 5b 61 4f 72 64 65 72 5b 69 69 5d  aCell[aOrder[ii]
39de1 5d 3b 0a 20 20 20 20 72 63 20 3d 20 43 68 6f 6f  ];.    rc = Choo
39de2 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c 20 70  seLeaf(pRtree, p
39de3 2c 20 69 48 65 69 67 68 74 2c 20 26 70 49 6e 73  , iHeight, &pIns
39de4 65 72 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ert);.    if( rc
39de5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39de6 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20       int rc2;.  
39de7 20 20 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e      rc = rtreeIn
39de8 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  sertCell(pRtree,
39de9 20 70 49 6e 73 65 72 74 2c 20 70 2c 20 69 48 65   pInsert, p, iHe
39dea 69 67 68 74 29 3b 0a 20 20 20 20 20 20 72 63 32  ight);.      rc2
39deb 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
39dec 52 74 72 65 65 2c 20 70 49 6e 73 65 72 74 29 3b  Rtree, pInsert);
39ded 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
39dee 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
39def 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20      rc = rc2;.  
39df0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
39df1 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
39df2 61 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  aCell);.  return
39df3 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e   rc;.}../*.** In
39df4 73 65 72 74 20 63 65 6c 6c 20 70 43 65 6c 6c 20  sert cell pCell 
39df5 69 6e 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e  into node pNode.
39df6 20 4e 6f 64 65 20 70 4e 6f 64 65 20 69 73 20 74   Node pNode is t
39df7 68 65 20 68 65 61 64 20 6f 66 20 61 20 0a 2a 2a  he head of a .**
39df8 20 73 75 62 74 72 65 65 20 69 48 65 69 67 68 74   subtree iHeight
39df9 20 68 69 67 68 20 28 6c 65 61 66 20 6e 6f 64 65   high (leaf node
39dfa 73 20 68 61 76 65 20 69 48 65 69 67 68 74 3d 3d  s have iHeight==
39dfb 30 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  0)..*/.static in
39dfc 74 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  t rtreeInsertCel
39dfd 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  l(.  Rtree *pRtr
39dfe 65 65 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ee,.  RtreeNode 
39dff 2a 70 4e 6f 64 65 2c 0a 20 20 52 74 72 65 65 43  *pNode,.  RtreeC
39e00 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a 20 20 69 6e  ell *pCell,.  in
39e01 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69  t iHeight.){.  i
39e02 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
39e03 4b 3b 0a 20 20 69 66 28 20 69 48 65 69 67 68 74  K;.  if( iHeight
39e04 3e 30 20 29 7b 0a 20 20 20 20 52 74 72 65 65 4e  >0 ){.    RtreeN
39e05 6f 64 65 20 2a 70 43 68 69 6c 64 20 3d 20 6e 6f  ode *pChild = no
39e06 64 65 48 61 73 68 4c 6f 6f 6b 75 70 28 70 52 74  deHashLookup(pRt
39e07 72 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77  ree, pCell->iRow
39e08 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70 43 68  id);.    if( pCh
39e09 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64  ild ){.      nod
39e0a 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
39e0b 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74   pChild->pParent
39e0c 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66  );.      nodeRef
39e0d 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20  erence(pNode);. 
39e0e 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70 50 61       pChild->pPa
39e0f 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  rent = pNode;.  
39e10 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 6f    }.  }.  if( no
39e11 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
39e12 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c  ree, pNode, pCel
39e13 6c 29 20 29 7b 0a 23 69 66 20 56 41 52 49 41 4e  l) ){.#if VARIAN
39e14 54 5f 52 53 54 41 52 54 52 45 45 5f 52 45 49 4e  T_RSTARTREE_REIN
39e15 53 45 52 54 0a 20 20 20 20 69 66 28 20 69 48 65  SERT.    if( iHe
39e16 69 67 68 74 3c 3d 70 52 74 72 65 65 2d 3e 69 52  ight<=pRtree->iR
39e17 65 69 6e 73 65 72 74 48 65 69 67 68 74 20 7c 7c  einsertHeight ||
39e18 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31   pNode->iNode==1
39e19 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 70  ){.      rc = Sp
39e1a 6c 69 74 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  litNode(pRtree, 
39e1b 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48  pNode, pCell, iH
39e1c 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73  eight);.    }els
39e1d 65 7b 0a 20 20 20 20 20 20 70 52 74 72 65 65 2d  e{.      pRtree-
39e1e 3e 69 52 65 69 6e 73 65 72 74 48 65 69 67 68 74  >iReinsertHeight
39e1f 20 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 20 20   = iHeight;.    
39e20 20 20 72 63 20 3d 20 52 65 69 6e 73 65 72 74 28    rc = Reinsert(
39e21 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70  pRtree, pNode, p
39e22 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a  Cell, iHeight);.
39e23 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20      }.#else.    
39e24 72 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65 28 70  rc = SplitNode(p
39e25 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43  Rtree, pNode, pC
39e26 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b 0a 23  ell, iHeight);.#
39e27 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20  endif.  }else{. 
39e28 20 20 20 41 64 6a 75 73 74 54 72 65 65 28 70 52     AdjustTree(pR
39e29 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65  tree, pNode, pCe
39e2a 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 48 65  ll);.    if( iHe
39e2b 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  ight==0 ){.     
39e2c 20 72 63 20 3d 20 72 6f 77 69 64 57 72 69 74 65   rc = rowidWrite
39e2d 28 70 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d 3e  (pRtree, pCell->
39e2e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69  iRowid, pNode->i
39e2f 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Node);.    }else
39e30 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 72  {.      rc = par
39e31 65 6e 74 57 72 69 74 65 28 70 52 74 72 65 65 2c  entWrite(pRtree,
39e32 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20   pCell->iRowid, 
39e33 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20  pNode->iNode);. 
39e34 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
39e35 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
39e36 69 6e 74 20 72 65 69 6e 73 65 72 74 4e 6f 64 65  int reinsertNode
39e37 43 6f 6e 74 65 6e 74 28 52 74 72 65 65 20 2a 70  Content(Rtree *p
39e38 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
39e39 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20   *pNode){.  int 
39e3a 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  ii;.  int rc = S
39e3b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
39e3c 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e  nCell = NCELL(pN
39e3d 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d  ode);..  for(ii=
39e3e 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
39e3f 20 26 26 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69   && ii<nCell; ii
39e40 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f  ++){.    RtreeNo
39e41 64 65 20 2a 70 49 6e 73 65 72 74 3b 0a 20 20 20  de *pInsert;.   
39e42 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b   RtreeCell cell;
39e43 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  .    nodeGetCell
39e44 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
39e45 69 69 2c 20 26 63 65 6c 6c 29 3b 0a 0a 20 20 20  ii, &cell);..   
39e46 20 2f 2a 20 46 69 6e 64 20 61 20 6e 6f 64 65 20   /* Find a node 
39e47 74 6f 20 73 74 6f 72 65 20 74 68 69 73 20 63 65  to store this ce
39e48 6c 6c 20 69 6e 2e 20 70 4e 6f 64 65 2d 3e 69 4e  ll in. pNode->iN
39e49 6f 64 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f  ode currently co
39e4a 6e 74 61 69 6e 73 0a 20 20 20 20 2a 2a 20 74 68  ntains.    ** th
39e4b 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 20  e height of the 
39e4c 73 75 62 2d 74 72 65 65 20 68 65 61 64 65 64 20  sub-tree headed 
39e4d 62 79 20 74 68 65 20 63 65 6c 6c 2e 0a 20 20 20  by the cell..   
39e4e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 43 68 6f   */.    rc = Cho
39e4f 6f 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c 20  oseLeaf(pRtree, 
39e50 26 63 65 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69 4e  &cell, pNode->iN
39e51 6f 64 65 2c 20 26 70 49 6e 73 65 72 74 29 3b 0a  ode, &pInsert);.
39e52 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
39e53 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
39e54 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72 63  nt rc2;.      rc
39e55 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65   = rtreeInsertCe
39e56 6c 6c 28 70 52 74 72 65 65 2c 20 70 49 6e 73 65  ll(pRtree, pInse
39e57 72 74 2c 20 26 63 65 6c 6c 2c 20 70 4e 6f 64 65  rt, &cell, pNode
39e58 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  ->iNode);.      
39e59 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73  rc2 = nodeReleas
39e5a 65 28 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72  e(pRtree, pInser
39e5b 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  t);.      if( rc
39e5c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39e5d 20 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b         rc = rc2;
39e5e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
39e5f 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
39e60 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 6c 65 63 74 20  }../*.** Select 
39e61 61 20 63 75 72 72 65 6e 74 6c 79 20 75 6e 75 73  a currently unus
39e62 65 64 20 72 6f 77 69 64 20 66 6f 72 20 61 20 6e  ed rowid for a n
39e63 65 77 20 72 2d 74 72 65 65 20 72 65 63 6f 72 64  ew r-tree record
39e64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39e65 6e 65 77 52 6f 77 69 64 28 52 74 72 65 65 20 2a  newRowid(Rtree *
39e66 70 52 74 72 65 65 2c 20 69 36 34 20 2a 70 69 52  pRtree, i64 *piR
39e67 6f 77 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b  owid){.  int rc;
39e68 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
39e69 6e 75 6c 6c 28 70 52 74 72 65 65 2d 3e 70 57 72  null(pRtree->pWr
39e6a 69 74 65 52 6f 77 69 64 2c 20 31 29 3b 0a 20 20  iteRowid, 1);.  
39e6b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c  sqlite3_bind_nul
39e6c 6c 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65  l(pRtree->pWrite
39e6d 52 6f 77 69 64 2c 20 32 29 3b 0a 20 20 73 71 6c  Rowid, 2);.  sql
39e6e 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
39e6f 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
39e70 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
39e71 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 57 72  eset(pRtree->pWr
39e72 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 2a 70 69  iteRowid);.  *pi
39e73 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  Rowid = sqlite3_
39e74 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
39e75 64 28 70 52 74 72 65 65 2d 3e 64 62 29 3b 0a 20  d(pRtree->db);. 
39e76 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
39e77 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74  ifndef NDEBUG.st
39e78 61 74 69 63 20 69 6e 74 20 68 61 73 68 49 73 45  atic int hashIsE
39e79 6d 70 74 79 28 52 74 72 65 65 20 2a 70 52 74 72  mpty(Rtree *pRtr
39e7a 65 65 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  ee){.  int ii;. 
39e7b 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 48 41   for(ii=0; ii<HA
39e7c 53 48 53 49 5a 45 3b 20 69 69 2b 2b 29 7b 0a 20  SHSIZE; ii++){. 
39e7d 20 20 20 61 73 73 65 72 74 28 20 21 70 52 74 72     assert( !pRtr
39e7e 65 65 2d 3e 61 48 61 73 68 5b 69 69 5d 20 29 3b  ee->aHash[ii] );
39e7f 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
39e80 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
39e81 20 54 68 65 20 78 55 70 64 61 74 65 20 6d 65 74   The xUpdate met
39e82 68 6f 64 20 66 6f 72 20 72 74 72 65 65 20 6d 6f  hod for rtree mo
39e83 64 75 6c 65 20 76 69 72 74 75 61 6c 20 74 61 62  dule virtual tab
39e84 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  les..*/.static i
39e85 6e 74 20 72 74 72 65 65 55 70 64 61 74 65 28 0a  nt rtreeUpdate(.
39e86 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
39e87 70 56 74 61 62 2c 20 0a 20 20 69 6e 74 20 6e 44  pVtab, .  int nD
39e88 61 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f  ata, .  sqlite3_
39e89 76 61 6c 75 65 20 2a 2a 61 7a 44 61 74 61 2c 20  value **azData, 
39e8a 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
39e8b 2a 70 52 6f 77 69 64 0a 29 7b 0a 20 20 52 74 72  *pRowid.){.  Rtr
39e8c 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
39e8d 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20 20 69  ree *)pVtab;.  i
39e8e 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
39e8f 4b 3b 0a 0a 20 20 72 74 72 65 65 52 65 66 65 72  K;..  rtreeRefer
39e90 65 6e 63 65 28 70 52 74 72 65 65 29 3b 0a 0a 20  ence(pRtree);.. 
39e91 20 61 73 73 65 72 74 28 6e 44 61 74 61 3e 3d 31   assert(nData>=1
39e92 29 3b 0a 20 20 61 73 73 65 72 74 28 68 61 73 68  );.  assert(hash
39e93 49 73 45 6d 70 74 79 28 70 52 74 72 65 65 29 29  IsEmpty(pRtree))
39e94 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 7a 44 61 74  ;..  /* If azDat
39e95 61 5b 30 5d 20 69 73 20 6e 6f 74 20 61 6e 20 53  a[0] is not an S
39e96 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2c 20 69  QL NULL value, i
39e97 74 20 69 73 20 74 68 65 20 72 6f 77 69 64 20 6f  t is the rowid o
39e98 66 20 61 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20  f a.  ** record 
39e99 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 74  to delete from t
39e9a 68 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65 2e  he r-tree table.
39e9b 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   The following b
39e9c 6c 6f 63 6b 20 64 6f 65 73 0a 20 20 2a 2a 20 6a  lock does.  ** j
39e9d 75 73 74 20 74 68 61 74 2e 0a 20 20 2a 2f 0a 20  ust that..  */. 
39e9e 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
39e9f 75 65 5f 74 79 70 65 28 61 7a 44 61 74 61 5b 30  ue_type(azData[0
39ea0 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])!=SQLITE_NULL 
39ea1 29 7b 0a 20 20 20 20 69 36 34 20 69 44 65 6c 65  ){.    i64 iDele
39ea2 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
39ea3 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69 64 20     /* The rowid 
39ea4 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20  to delete */.   
39ea5 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61   RtreeNode *pLea
39ea6 66 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  f;           /* 
39ea7 4c 65 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61 69  Leaf node contai
39ea8 6e 69 6e 67 20 72 65 63 6f 72 64 20 69 44 65 6c  ning record iDel
39ea9 65 74 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ete */.    int i
39eaa 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20  Cell;           
39eab 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
39eac 6f 66 20 69 44 65 6c 65 74 65 20 63 65 6c 6c 20  of iDelete cell 
39ead 69 6e 20 70 4c 65 61 66 20 2a 2f 0a 20 20 20 20  in pLeaf */.    
39eae 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 6f 6f 74  RtreeNode *pRoot
39eaf 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e  ;..    /* Obtain
39eb0 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
39eb1 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 74 6f  the root node to
39eb2 20 69 6e 69 74 69 61 6c 69 73 65 20 52 74 72 65   initialise Rtre
39eb3 65 2e 69 44 65 70 74 68 20 2a 2f 0a 20 20 20 20  e.iDepth */.    
39eb4 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
39eb5 28 70 52 74 72 65 65 2c 20 31 2c 20 30 2c 20 26  (pRtree, 1, 0, &
39eb6 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 2f 2a 20  pRoot);..    /* 
39eb7 4f 62 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e  Obtain a referen
39eb8 63 65 20 74 6f 20 74 68 65 20 6c 65 61 66 20 6e  ce to the leaf n
39eb9 6f 64 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ode that contain
39eba 73 20 74 68 65 20 65 6e 74 72 79 20 0a 20 20 20  s the entry .   
39ebb 20 2a 2a 20 61 62 6f 75 74 20 74 6f 20 62 65 20   ** about to be 
39ebc 64 65 6c 65 74 65 64 2e 20 0a 20 20 20 20 2a 2f  deleted. .    */
39ebd 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
39ebe 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
39ebf 69 44 65 6c 65 74 65 20 3d 20 73 71 6c 69 74 65  iDelete = sqlite
39ec0 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 7a  3_value_int64(az
39ec1 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20  Data[0]);.      
39ec2 72 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f 64  rc = findLeafNod
39ec3 65 28 70 52 74 72 65 65 2c 20 69 44 65 6c 65 74  e(pRtree, iDelet
39ec4 65 2c 20 26 70 4c 65 61 66 29 3b 0a 20 20 20 20  e, &pLeaf);.    
39ec5 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  }..    /* Delete
39ec6 20 74 68 65 20 63 65 6c 6c 20 69 6e 20 71 75 65   the cell in que
39ec7 73 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6c  stion from the l
39ec8 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20  eaf node. */.   
39ec9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
39eca 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  OK ){.      int 
39ecb 72 63 32 3b 0a 20 20 20 20 20 20 69 43 65 6c 6c  rc2;.      iCell
39ecc 20 3d 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65   = nodeRowidInde
39ecd 78 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c  x(pRtree, pLeaf,
39ece 20 69 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20   iDelete);.     
39ecf 20 72 63 20 3d 20 64 65 6c 65 74 65 43 65 6c 6c   rc = deleteCell
39ed0 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20  (pRtree, pLeaf, 
39ed1 69 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20  iCell, 0);.     
39ed2 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61   rc2 = nodeRelea
39ed3 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  se(pRtree, pLeaf
39ed4 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
39ed5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
39ed6 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a        rc = rc2;.
39ed7 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
39ed8 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65     /* Delete the
39ed9 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 65   corresponding e
39eda 6e 74 72 79 20 69 6e 20 74 68 65 20 3c 72 74 72  ntry in the <rtr
39edb 65 65 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65 2e  ee>_rowid table.
39edc 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
39edd 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
39ede 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
39edf 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44  int64(pRtree->pD
39ee0 65 6c 65 74 65 52 6f 77 69 64 2c 20 31 2c 20 69  eleteRowid, 1, i
39ee1 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 20 20 73  Delete);.      s
39ee2 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
39ee3 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
39ee4 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
39ee5 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72  lite3_reset(pRtr
39ee6 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64  ee->pDeleteRowid
39ee7 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  );.    }..    /*
39ee8 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 72 6f   Check if the ro
39ee9 6f 74 20 6e 6f 64 65 20 6e 6f 77 20 68 61 73 20  ot node now has 
39eea 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 68 69 6c  exactly one chil
39eeb 64 2e 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76 65  d. If so, remove
39eec 0a 20 20 20 20 2a 2a 20 69 74 2c 20 73 63 68 65  .    ** it, sche
39eed 64 75 6c 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  dule the content
39eee 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 66  s of the child f
39eef 6f 72 20 72 65 69 6e 73 65 72 74 69 6f 6e 20 61  or reinsertion a
39ef0 6e 64 20 0a 20 20 20 20 2a 2a 20 72 65 64 75 63  nd .    ** reduc
39ef1 65 20 74 68 65 20 74 72 65 65 20 68 65 69 67 68  e the tree heigh
39ef2 74 20 62 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a  t by one..    **
39ef3 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20  .    ** This is 
39ef4 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 63 6f  equivalent to co
39ef5 70 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e  pying the conten
39ef6 74 73 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20  ts of the child 
39ef7 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  into.    ** the 
39ef8 72 6f 6f 74 20 6e 6f 64 65 20 28 74 68 65 20 6f  root node (the o
39ef9 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 47 75  peration that Gu
39efa 74 6d 61 6e 27 73 20 70 61 70 65 72 20 73 61 79  tman's paper say
39efb 73 20 74 6f 20 70 65 72 66 6f 72 6d 20 0a 20 20  s to perform .  
39efc 20 20 2a 2a 20 69 6e 20 74 68 69 73 20 73 63 65    ** in this sce
39efd 6e 61 72 69 6f 29 2e 0a 20 20 20 20 2a 2f 0a 20  nario)..    */. 
39efe 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
39eff 45 5f 4f 4b 20 26 26 20 70 52 74 72 65 65 2d 3e  E_OK && pRtree->
39f00 69 44 65 70 74 68 3e 30 20 29 7b 0a 20 20 20 20  iDepth>0 ){.    
39f01 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
39f02 5f 4f 4b 20 26 26 20 4e 43 45 4c 4c 28 70 52 6f  _OK && NCELL(pRo
39f03 6f 74 29 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ot)==1 ){.      
39f04 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68    RtreeNode *pCh
39f05 69 6c 64 3b 0a 20 20 20 20 20 20 20 20 69 36 34  ild;.        i64
39f06 20 69 43 68 69 6c 64 20 3d 20 6e 6f 64 65 47 65   iChild = nodeGe
39f07 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70  tRowid(pRtree, p
39f08 52 6f 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20  Root, 0);.      
39f09 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69    rc = nodeAcqui
39f0a 72 65 28 70 52 74 72 65 65 2c 20 69 43 68 69 6c  re(pRtree, iChil
39f0b 64 2c 20 70 52 6f 6f 74 2c 20 26 70 43 68 69 6c  d, pRoot, &pChil
39f0c 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  d);.        if( 
39f0d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
39f0e 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
39f0f 72 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72 65  removeNode(pRtre
39f10 65 2c 20 70 43 68 69 6c 64 2c 20 70 52 74 72 65  e, pChild, pRtre
39f11 65 2d 3e 69 44 65 70 74 68 2d 31 29 3b 0a 20 20  e->iDepth-1);.  
39f12 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
39f13 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
39f14 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  K ){.          p
39f15 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 2d 3b  Rtree->iDepth--;
39f16 0a 20 20 20 20 20 20 20 20 20 20 77 72 69 74 65  .          write
39f17 49 6e 74 31 36 28 70 52 6f 6f 74 2d 3e 7a 44 61  Int16(pRoot->zDa
39f18 74 61 2c 20 70 52 74 72 65 65 2d 3e 69 44 65 70  ta, pRtree->iDep
39f19 74 68 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  th);.          p
39f1a 52 6f 6f 74 2d 3e 69 73 44 69 72 74 79 20 3d 20  Root->isDirty = 
39f1b 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
39f1c 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
39f1d 2f 2a 20 52 65 2d 69 6e 73 65 72 74 20 74 68 65  /* Re-insert the
39f1e 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6e 79   contents of any
39f1f 20 75 6e 64 65 72 66 75 6c 6c 20 6e 6f 64 65 73   underfull nodes
39f20 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
39f21 65 20 74 72 65 65 2e 20 2a 2f 0a 20 20 20 20 66  e tree. */.    f
39f22 6f 72 28 70 4c 65 61 66 3d 70 52 74 72 65 65 2d  or(pLeaf=pRtree-
39f23 3e 70 44 65 6c 65 74 65 64 3b 20 70 4c 65 61 66  >pDeleted; pLeaf
39f24 3b 20 70 4c 65 61 66 3d 70 52 74 72 65 65 2d 3e  ; pLeaf=pRtree->
39f25 70 44 65 6c 65 74 65 64 29 7b 0a 20 20 20 20 20  pDeleted){.     
39f26 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
39f27 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  OK ){.        rc
39f28 20 3d 20 72 65 69 6e 73 65 72 74 4e 6f 64 65 43   = reinsertNodeC
39f29 6f 6e 74 65 6e 74 28 70 52 74 72 65 65 2c 20 70  ontent(pRtree, p
39f2a 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20  Leaf);.      }. 
39f2b 20 20 20 20 20 70 52 74 72 65 65 2d 3e 70 44 65       pRtree->pDe
39f2c 6c 65 74 65 64 20 3d 20 70 4c 65 61 66 2d 3e 70  leted = pLeaf->p
39f2d 4e 65 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69  Next;.      sqli
39f2e 74 65 33 5f 66 72 65 65 28 70 4c 65 61 66 29 3b  te3_free(pLeaf);
39f2f 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52  .    }..    /* R
39f30 65 6c 65 61 73 65 20 74 68 65 20 72 65 66 65 72  elease the refer
39f31 65 6e 63 65 20 74 6f 20 74 68 65 20 72 6f 6f 74  ence to the root
39f32 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20 20 20 69 66   node. */.    if
39f33 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
39f34 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 6f  ){.      rc = no
39f35 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
39f36 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 65  , pRoot);.    }e
39f37 6c 73 65 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52  lse{.      nodeR
39f38 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
39f39 52 6f 6f 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Root);.    }.  }
39f3a 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61 7a  ..  /* If the az
39f3b 44 61 74 61 5b 5d 20 61 72 72 61 79 20 63 6f 6e  Data[] array con
39f3c 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e 20  tains more than 
39f3d 6f 6e 65 20 65 6c 65 6d 65 6e 74 2c 20 65 6c 65  one element, ele
39f3e 6d 65 6e 74 73 0a 20 20 2a 2a 20 28 61 7a 44 61  ments.  ** (azDa
39f3f 74 61 5b 32 5d 2e 2e 61 7a 44 61 74 61 5b 61 72  ta[2]..azData[ar
39f40 67 63 2d 31 5d 29 20 63 6f 6e 74 61 69 6e 20 61  gc-1]) contain a
39f41 20 6e 65 77 20 72 65 63 6f 72 64 20 74 6f 20 69   new record to i
39f42 6e 73 65 72 74 20 69 6e 74 6f 0a 20 20 2a 2a 20  nsert into.  ** 
39f43 74 68 65 20 72 2d 74 72 65 65 20 73 74 72 75 63  the r-tree struc
39f44 74 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ture..  */.  if(
39f45 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
39f46 26 20 6e 44 61 74 61 3e 31 20 29 7b 0a 20 20 20  & nData>1 ){.   
39f47 20 2f 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77   /* Insert a new
39f48 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68 65   record into the
39f49 20 72 2d 74 72 65 65 20 2a 2f 0a 20 20 20 20 52   r-tree */.    R
39f4a 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
39f4b 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 52     int ii;.    R
39f4c 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66 3b  treeNode *pLeaf;
39f4d 0a 0a 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74  ..    /* Populat
39f4e 65 20 74 68 65 20 63 65 6c 6c 2e 61 43 6f 6f 72  e the cell.aCoor
39f4f 64 5b 5d 20 61 72 72 61 79 2e 20 54 68 65 20 66  d[] array. The f
39f50 69 72 73 74 20 63 6f 6f 72 64 69 6e 61 74 65 20  irst coordinate 
39f51 69 73 20 61 7a 44 61 74 61 5b 33 5d 2e 20 2a 2f  is azData[3]. */
39f52 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 44 61  .    assert( nDa
39f53 74 61 3d 3d 28 70 52 74 72 65 65 2d 3e 6e 44 69  ta==(pRtree->nDi
39f54 6d 2a 32 20 2b 20 33 29 20 29 3b 0a 20 20 20 20  m*2 + 3) );.    
39f55 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f  if( pRtree->eCoo
39f56 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
39f57 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20  ORD_REAL32 ){.  
39f58 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
39f59 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
39f5a 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20  ); ii+=2){.     
39f5b 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69     cell.aCoord[i
39f5c 69 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29 73 71  i].f = (float)sq
39f5d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
39f5e 6c 65 28 61 7a 44 61 74 61 5b 69 69 2b 33 5d 29  le(azData[ii+3])
39f5f 3b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61  ;.        cell.a
39f60 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 3d 20  Coord[ii+1].f = 
39f61 28 66 6c 6f 61 74 29 73 71 6c 69 74 65 33 5f 76  (float)sqlite3_v
39f62 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 7a 44 61  alue_double(azDa
39f63 74 61 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20 20  ta[ii+4]);.     
39f64 20 20 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f 6f     if( cell.aCoo
39f65 72 64 5b 69 69 5d 2e 66 3e 63 65 6c 6c 2e 61 43  rd[ii].f>cell.aC
39f66 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 20 29 7b 0a  oord[ii+1].f ){.
39f67 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
39f68 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
39f69 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
39f6a 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20   constraint;.   
39f6b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
39f6c 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
39f6d 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52  for(ii=0; ii<(pR
39f6e 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69  tree->nDim*2); i
39f6f 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 63  i+=2){.        c
39f70 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  ell.aCoord[ii].i
39f71 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
39f72 5f 69 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b 33  _int(azData[ii+3
39f73 5d 29 3b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c  ]);.        cell
39f74 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20  .aCoord[ii+1].i 
39f75 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
39f76 69 6e 74 28 61 7a 44 61 74 61 5b 69 69 2b 34 5d  int(azData[ii+4]
39f77 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  );.        if( c
39f78 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 69  ell.aCoord[ii].i
39f79 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b  >cell.aCoord[ii+
39f7a 31 5d 2e 69 20 29 7b 0a 20 20 20 20 20 20 20 20  1].i ){.        
39f7b 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
39f7c 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20  NSTRAINT;.      
39f7d 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72 61      goto constra
39f7e 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  int;.        }. 
39f7f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
39f80 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
39f81 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
39f82 20 6e 65 77 20 72 6f 77 2e 20 2a 2f 0a 20 20 20   new row. */.   
39f83 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
39f84 75 65 5f 74 79 70 65 28 61 7a 44 61 74 61 5b 32  ue_type(azData[2
39f85 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])==SQLITE_NULL 
39f86 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6e 65  ){.      rc = ne
39f87 77 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 26  wRowid(pRtree, &
39f88 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20  cell.iRowid);.  
39f89 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63    }else{.      c
39f8a 65 6c 6c 2e 69 52 6f 77 69 64 20 3d 20 73 71 6c  ell.iRowid = sql
39f8b 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
39f8c 28 61 7a 44 61 74 61 5b 32 5d 29 3b 0a 20 20 20  (azData[2]);.   
39f8d 20 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f     sqlite3_bind_
39f8e 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52  int64(pRtree->pR
39f8f 65 61 64 52 6f 77 69 64 2c 20 31 2c 20 63 65 6c  eadRowid, 1, cel
39f90 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  l.iRowid);.     
39f91 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d   if( SQLITE_ROW=
39f92 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52  =sqlite3_step(pR
39f93 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
39f94 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ){.        sql
39f95 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
39f96 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a  e->pReadRowid);.
39f97 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
39f98 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a  ITE_CONSTRAINT;.
39f99 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e          goto con
39f9a 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20 20 7d  straint;.      }
39f9b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
39f9c 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
39f9d 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3b 0a 20  ->pReadRowid);. 
39f9e 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
39f9f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39fa0 20 20 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65       rc = Choose
39fa1 4c 65 61 66 28 70 52 74 72 65 65 2c 20 26 63 65  Leaf(pRtree, &ce
39fa2 6c 6c 2c 20 30 2c 20 26 70 4c 65 61 66 29 3b 0a  ll, 0, &pLeaf);.
39fa3 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
39fa4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39fa5 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20       int rc2;.  
39fa6 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52 65 69      pRtree->iRei
39fa7 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20 2d 31  nsertHeight = -1
39fa8 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 74 72  ;.      rc = rtr
39fa9 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  eeInsertCell(pRt
39faa 72 65 65 2c 20 70 4c 65 61 66 2c 20 26 63 65 6c  ree, pLeaf, &cel
39fab 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63 32  l, 0);.      rc2
39fac 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70   = nodeRelease(p
39fad 52 74 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a 20  Rtree, pLeaf);. 
39fae 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
39faf 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
39fb0 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20    rc = rc2;.    
39fb1 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 63    }.    }.  }..c
39fb2 6f 6e 73 74 72 61 69 6e 74 3a 0a 20 20 72 74 72  onstraint:.  rtr
39fb3 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  eeRelease(pRtree
39fb4 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
39fb5 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 52 65  }../*.** The xRe
39fb6 6e 61 6d 65 20 6d 65 74 68 6f 64 20 66 6f 72 20  name method for 
39fb7 72 74 72 65 65 20 6d 6f 64 75 6c 65 20 76 69 72  rtree module vir
39fb8 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a  tual tables..*/.
39fb9 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
39fba 52 65 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 76  Rename(sqlite3_v
39fbb 74 61 62 20 2a 70 56 74 61 62 2c 20 63 6f 6e 73  tab *pVtab, cons
39fbc 74 20 63 68 61 72 20 2a 7a 4e 65 77 4e 61 6d 65  t char *zNewName
39fbd 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  ){.  Rtree *pRtr
39fbe 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70 56  ee = (Rtree *)pV
39fbf 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  tab;.  int rc = 
39fc0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
39fc1 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c  char *zSql = sql
39fc2 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20  ite3_mprintf(.  
39fc3 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25    "ALTER TABLE %
39fc4 51 2e 27 25 71 5f 6e 6f 64 65 27 20 20 20 52 45  Q.'%q_node'   RE
39fc5 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 6e 6f 64  NAME TO \"%w_nod
39fc6 65 5c 22 3b 22 0a 20 20 20 20 22 41 4c 54 45 52  e\";".    "ALTER
39fc7 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 70 61   TABLE %Q.'%q_pa
39fc8 72 65 6e 74 27 20 52 45 4e 41 4d 45 20 54 4f 20  rent' RENAME TO 
39fc9 5c 22 25 77 5f 70 61 72 65 6e 74 5c 22 3b 22 0a  \"%w_parent\";".
39fca 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45      "ALTER TABLE
39fcb 20 25 51 2e 27 25 71 5f 72 6f 77 69 64 27 20 20   %Q.'%q_rowid'  
39fcc 52 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 72  RENAME TO \"%w_r
39fcd 6f 77 69 64 5c 22 3b 22 0a 20 20 20 20 2c 20 70  owid\";".    , p
39fce 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72  Rtree->zDb, pRtr
39fcf 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e  ee->zName, zNewN
39fd0 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74 72 65  ame .    , pRtre
39fd1 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e  e->zDb, pRtree->
39fd2 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 20  zName, zNewName 
39fd3 0a 20 20 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a  .    , pRtree->z
39fd4 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d  Db, pRtree->zNam
39fd5 65 2c 20 7a 4e 65 77 4e 61 6d 65 0a 20 20 29 3b  e, zNewName.  );
39fd6 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20  .  if( zSql ){. 
39fd7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
39fd8 65 78 65 63 28 70 52 74 72 65 65 2d 3e 64 62 2c  exec(pRtree->db,
39fd9 20 7a 53 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b   zSql, 0, 0, 0);
39fda 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
39fdb 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 72  e(zSql);.  }.  r
39fdc 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61  eturn rc;.}..sta
39fdd 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  tic sqlite3_modu
39fde 6c 65 20 72 74 72 65 65 4d 6f 64 75 6c 65 20 3d  le rtreeModule =
39fdf 20 7b 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20   {.  0,         
39fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39fe1 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
39fe2 20 72 74 72 65 65 43 72 65 61 74 65 2c 20 20 20   rtreeCreate,   
39fe3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
39fe4 78 43 72 65 61 74 65 20 2d 20 63 72 65 61 74 65  xCreate - create
39fe5 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74   a table */.  rt
39fe6 72 65 65 43 6f 6e 6e 65 63 74 2c 20 20 20 20 20  reeConnect,     
39fe7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6f            /* xCo
39fe8 6e 6e 65 63 74 20 2d 20 63 6f 6e 6e 65 63 74 20  nnect - connect 
39fe9 74 6f 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74  to an existing t
39fea 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65 42  able */.  rtreeB
39feb 65 73 74 49 6e 64 65 78 2c 20 20 20 20 20 20 20  estIndex,       
39fec 20 20 20 20 20 20 2f 2a 20 78 42 65 73 74 49 6e        /* xBestIn
39fed 64 65 78 20 2d 20 44 65 74 65 72 6d 69 6e 65 20  dex - Determine 
39fee 73 65 61 72 63 68 20 73 74 72 61 74 65 67 79 20  search strategy 
39fef 2a 2f 0a 20 20 72 74 72 65 65 44 69 73 63 6f 6e  */.  rtreeDiscon
39ff0 6e 65 63 74 2c 20 20 20 20 20 20 20 20 20 20 20  nect,           
39ff1 20 2f 2a 20 78 44 69 73 63 6f 6e 6e 65 63 74 20   /* xDisconnect 
39ff2 2d 20 44 69 73 63 6f 6e 6e 65 63 74 20 66 72 6f  - Disconnect fro
39ff3 6d 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72  m a table */.  r
39ff4 74 72 65 65 44 65 73 74 72 6f 79 2c 20 20 20 20  treeDestroy,    
39ff5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44             /* xD
39ff6 65 73 74 72 6f 79 20 2d 20 44 72 6f 70 20 61 20  estroy - Drop a 
39ff7 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65  table */.  rtree
39ff8 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Open,           
39ff9 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
39ffa 2d 20 6f 70 65 6e 20 61 20 63 75 72 73 6f 72 20  - open a cursor 
39ffb 2a 2f 0a 20 20 72 74 72 65 65 43 6c 6f 73 65 2c  */.  rtreeClose,
39ffc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39ffd 20 2f 2a 20 78 43 6c 6f 73 65 20 2d 20 63 6c 6f   /* xClose - clo
39ffe 73 65 20 61 20 63 75 72 73 6f 72 20 2a 2f 0a 20  se a cursor */. 
39fff 20 72 74 72 65 65 46 69 6c 74 65 72 2c 20 20 20   rtreeFilter,   
3a000 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a001 78 46 69 6c 74 65 72 20 2d 20 63 6f 6e 66 69 67  xFilter - config
3a002 75 72 65 20 73 63 61 6e 20 63 6f 6e 73 74 72 61  ure scan constra
3a003 69 6e 74 73 20 2a 2f 0a 20 20 72 74 72 65 65 4e  ints */.  rtreeN
3a004 65 78 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ext,            
3a005 20 20 20 20 20 20 2f 2a 20 78 4e 65 78 74 20 2d        /* xNext -
3a006 20 61 64 76 61 6e 63 65 20 61 20 63 75 72 73 6f   advance a curso
3a007 72 20 2a 2f 0a 20 20 72 74 72 65 65 45 6f 66 2c  r */.  rtreeEof,
3a008 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a009 20 20 20 2f 2a 20 78 45 6f 66 20 2a 2f 0a 20 20     /* xEof */.  
3a00a 72 74 72 65 65 43 6f 6c 75 6d 6e 2c 20 20 20 20  rtreeColumn,    
3a00b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3a00c 43 6f 6c 75 6d 6e 20 2d 20 72 65 61 64 20 64 61  Column - read da
3a00d 74 61 20 2a 2f 0a 20 20 72 74 72 65 65 52 6f 77  ta */.  rtreeRow
3a00e 69 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  id,             
3a00f 20 20 20 20 2f 2a 20 78 52 6f 77 69 64 20 2d 20      /* xRowid - 
3a010 72 65 61 64 20 64 61 74 61 20 2a 2f 0a 20 20 72  read data */.  r
3a011 74 72 65 65 55 70 64 61 74 65 2c 20 20 20 20 20  treeUpdate,     
3a012 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55             /* xU
3a013 70 64 61 74 65 20 2d 20 77 72 69 74 65 20 64 61  pdate - write da
3a014 74 61 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ta */.  0,      
3a015 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a016 20 20 20 20 2f 2a 20 78 42 65 67 69 6e 20 2d 20      /* xBegin - 
3a017 62 65 67 69 6e 20 74 72 61 6e 73 61 63 74 69 6f  begin transactio
3a018 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  n */.  0,       
3a019 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a01a 20 20 20 2f 2a 20 78 53 79 6e 63 20 2d 20 73 79     /* xSync - sy
3a01b 6e 63 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a  nc transaction *
3a01c 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
3a01d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a01e 2f 2a 20 78 43 6f 6d 6d 69 74 20 2d 20 63 6f 6d  /* xCommit - com
3a01f 6d 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  mit transaction 
3a020 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  */.  0,         
3a021 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a022 20 2f 2a 20 78 52 6f 6c 6c 62 61 63 6b 20 2d 20   /* xRollback - 
3a023 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e 73 61 63  rollback transac
3a024 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20  tion */.  0,    
3a025 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a026 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 64 46 75        /* xFindFu
3a027 6e 63 74 69 6f 6e 20 2d 20 66 75 6e 63 74 69 6f  nction - functio
3a028 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 20 2a 2f  n overloading */
3a029 0a 20 20 72 74 72 65 65 52 65 6e 61 6d 65 20 20  .  rtreeRename  
3a02a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a02b 2a 20 78 52 65 6e 61 6d 65 20 2d 20 72 65 6e 61  * xRename - rena
3a02c 6d 65 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  me the table */.
3a02d 7d 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 72  };..static int r
3a02e 74 72 65 65 53 71 6c 49 6e 69 74 28 0a 20 20 52  treeSqlInit(.  R
3a02f 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
3a030 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20   sqlite3 *db, . 
3a031 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
3a032 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
3a033 2a 7a 50 72 65 66 69 78 2c 20 0a 20 20 69 6e 74  *zPrefix, .  int
3a034 20 69 73 43 72 65 61 74 65 0a 29 7b 0a 20 20 69   isCreate.){.  i
3a035 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
3a036 4b 3b 0a 0a 20 20 23 64 65 66 69 6e 65 20 4e 5f  K;..  #define N_
3a037 53 54 41 54 45 4d 45 4e 54 20 39 0a 20 20 73 74  STATEMENT 9.  st
3a038 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
3a039 2a 61 7a 53 71 6c 5b 4e 5f 53 54 41 54 45 4d 45  *azSql[N_STATEME
3a03a 4e 54 5d 20 3d 20 7b 0a 20 20 20 20 2f 2a 20 52  NT] = {.    /* R
3a03b 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74 68  ead and write th
3a03c 65 20 78 78 78 5f 6e 6f 64 65 20 74 61 62 6c 65  e xxx_node table
3a03d 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43 54 20   */.    "SELECT 
3a03e 64 61 74 61 20 46 52 4f 4d 20 27 25 71 27 2e 27  data FROM '%q'.'
3a03f 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20 6e  %q_node' WHERE n
3a040 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20  odeno = :1",.   
3a041 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c   "INSERT OR REPL
3a042 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25  ACE INTO '%q'.'%
3a043 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45 53 28 3a  q_node' VALUES(:
3a044 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45  1, :2)",.    "DE
3a045 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27  LETE FROM '%q'.'
3a046 25 71 5f 6e 6f 64 65 27 20 57 48 45 52 45 20 6e  %q_node' WHERE n
3a047 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a 0a 20 20  odeno = :1",..  
3a048 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72    /* Read and wr
3a049 69 74 65 20 74 68 65 20 78 78 78 5f 72 6f 77 69  ite the xxx_rowi
3a04a 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22  d table */.    "
3a04b 53 45 4c 45 43 54 20 6e 6f 64 65 6e 6f 20 46 52  SELECT nodeno FR
3a04c 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69  OM '%q'.'%q_rowi
3a04d 64 27 20 57 48 45 52 45 20 72 6f 77 69 64 20 3d  d' WHERE rowid =
3a04e 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53 45 52   :1",.    "INSER
3a04f 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e 54  T OR REPLACE INT
3a050 4f 20 27 25 71 27 2e 27 25 71 5f 72 6f 77 69 64  O '%q'.'%q_rowid
3a051 27 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a 32 29  ' VALUES(:1, :2)
3a052 22 2c 0a 20 20 20 20 22 44 45 4c 45 54 45 20 46  ",.    "DELETE F
3a053 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 72 6f 77  ROM '%q'.'%q_row
3a054 69 64 27 20 57 48 45 52 45 20 72 6f 77 69 64 20  id' WHERE rowid 
3a055 3d 20 3a 31 22 2c 0a 0a 20 20 20 20 2f 2a 20 52  = :1",..    /* R
3a056 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 74 68  ead and write th
3a057 65 20 78 78 78 5f 70 61 72 65 6e 74 20 74 61 62  e xxx_parent tab
3a058 6c 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43  le */.    "SELEC
3a059 54 20 70 61 72 65 6e 74 6e 6f 64 65 20 46 52 4f  T parentnode FRO
3a05a 4d 20 27 25 71 27 2e 27 25 71 5f 70 61 72 65 6e  M '%q'.'%q_paren
3a05b 74 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f 20  t' WHERE nodeno 
3a05c 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53 45  = :1",.    "INSE
3a05d 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e  RT OR REPLACE IN
3a05e 54 4f 20 27 25 71 27 2e 27 25 71 5f 70 61 72 65  TO '%q'.'%q_pare
3a05f 6e 74 27 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a  nt' VALUES(:1, :
3a060 32 29 22 2c 0a 20 20 20 20 22 44 45 4c 45 54 45  2)",.    "DELETE
3a061 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 70   FROM '%q'.'%q_p
3a062 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e 6f 64  arent' WHERE nod
3a063 65 6e 6f 20 3d 20 3a 31 22 0a 20 20 7d 3b 0a 20  eno = :1".  };. 
3a064 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
3a065 61 70 70 53 74 6d 74 5b 4e 5f 53 54 41 54 45 4d  appStmt[N_STATEM
3a066 45 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a  ENT];.  int i;..
3a067 20 20 70 52 74 72 65 65 2d 3e 64 62 20 3d 20 64    pRtree->db = d
3a068 62 3b 0a 0a 20 20 69 66 28 20 69 73 43 72 65 61  b;..  if( isCrea
3a069 74 65 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  te ){.    char *
3a06a 7a 43 72 65 61 74 65 20 3d 20 73 71 6c 69 74 65  zCreate = sqlite
3a06b 33 5f 6d 70 72 69 6e 74 66 28 0a 22 43 52 45 41  3_mprintf(."CREA
3a06c 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e  TE TABLE \"%w\".
3a06d 5c 22 25 77 5f 6e 6f 64 65 5c 22 28 6e 6f 64 65  \"%w_node\"(node
3a06e 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  no INTEGER PRIMA
3a06f 52 59 20 4b 45 59 2c 20 64 61 74 61 20 42 4c 4f  RY KEY, data BLO
3a070 42 29 3b 22 0a 22 43 52 45 41 54 45 20 54 41 42  B);"."CREATE TAB
3a071 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f 72  LE \"%w\".\"%w_r
3a072 6f 77 69 64 5c 22 28 72 6f 77 69 64 20 49 4e 54  owid\"(rowid INT
3a073 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
3a074 2c 20 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52  , nodeno INTEGER
3a075 29 3b 22 0a 22 43 52 45 41 54 45 20 54 41 42 4c  );"."CREATE TABL
3a076 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f 70 61  E \"%w\".\"%w_pa
3a077 72 65 6e 74 5c 22 28 6e 6f 64 65 6e 6f 20 49 4e  rent\"(nodeno IN
3a078 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
3a079 59 2c 20 70 61 72 65 6e 74 6e 6f 64 65 20 49 4e  Y, parentnode IN
3a07a 54 45 47 45 52 29 3b 22 0a 22 49 4e 53 45 52 54  TEGER);"."INSERT
3a07b 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 6e   INTO '%q'.'%q_n
3a07c 6f 64 65 27 20 56 41 4c 55 45 53 28 31 2c 20 7a  ode' VALUES(1, z
3a07d 65 72 6f 62 6c 6f 62 28 25 64 29 29 22 2c 0a 20  eroblob(%d))",. 
3a07e 20 20 20 20 20 7a 44 62 2c 20 7a 50 72 65 66 69       zDb, zPrefi
3a07f 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c  x, zDb, zPrefix,
3a080 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c 20 7a   zDb, zPrefix, z
3a081 44 62 2c 20 7a 50 72 65 66 69 78 2c 20 70 52 74  Db, zPrefix, pRt
3a082 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 0a 20  ree->iNodeSize. 
3a083 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 7a     );.    if( !z
3a084 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 20 20  Create ){.      
3a085 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
3a086 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  MEM;.    }.    r
3a087 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  c = sqlite3_exec
3a088 28 64 62 2c 20 7a 43 72 65 61 74 65 2c 20 30 2c  (db, zCreate, 0,
3a089 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
3a08a 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61 74 65  te3_free(zCreate
3a08b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
3a08c 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3a08d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
3a08e 20 7d 0a 20 20 7d 0a 0a 20 20 61 70 70 53 74 6d   }.  }..  appStm
3a08f 74 5b 30 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[0] = &pRtree->
3a090 70 52 65 61 64 4e 6f 64 65 3b 0a 20 20 61 70 70  pReadNode;.  app
3a091 53 74 6d 74 5b 31 5d 20 3d 20 26 70 52 74 72 65  Stmt[1] = &pRtre
3a092 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b 0a 20  e->pWriteNode;. 
3a093 20 61 70 70 53 74 6d 74 5b 32 5d 20 3d 20 26 70   appStmt[2] = &p
3a094 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f  Rtree->pDeleteNo
3a095 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 33 5d  de;.  appStmt[3]
3a096 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65 61   = &pRtree->pRea
3a097 64 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74 6d  dRowid;.  appStm
3a098 74 5b 34 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[4] = &pRtree->
3a099 70 57 72 69 74 65 52 6f 77 69 64 3b 0a 20 20 61  pWriteRowid;.  a
3a09a 70 70 53 74 6d 74 5b 35 5d 20 3d 20 26 70 52 74  ppStmt[5] = &pRt
3a09b 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69  ree->pDeleteRowi
3a09c 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b 36 5d 20  d;.  appStmt[6] 
3a09d 3d 20 26 70 52 74 72 65 65 2d 3e 70 52 65 61 64  = &pRtree->pRead
3a09e 50 61 72 65 6e 74 3b 0a 20 20 61 70 70 53 74 6d  Parent;.  appStm
3a09f 74 5b 37 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[7] = &pRtree->
3a0a0 70 57 72 69 74 65 50 61 72 65 6e 74 3b 0a 20 20  pWriteParent;.  
3a0a1 61 70 70 53 74 6d 74 5b 38 5d 20 3d 20 26 70 52  appStmt[8] = &pR
3a0a2 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72  tree->pDeletePar
3a0a3 65 6e 74 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  ent;..  for(i=0;
3a0a4 20 69 3c 4e 5f 53 54 41 54 45 4d 45 4e 54 20 26   i<N_STATEMENT &
3a0a5 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b  & rc==SQLITE_OK;
3a0a6 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20   i++){.    char 
3a0a7 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f  *zSql = sqlite3_
3a0a8 6d 70 72 69 6e 74 66 28 61 7a 53 71 6c 5b 69 5d  mprintf(azSql[i]
3a0a9 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 29 3b  , zDb, zPrefix);
3a0aa 0a 20 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b  .    if( zSql ){
3a0ab 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
3a0ac 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64  te3_prepare_v2(d
3a0ad 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 61 70 70  b, zSql, -1, app
3a0ae 53 74 6d 74 5b 69 5d 2c 20 30 29 3b 20 0a 20 20  Stmt[i], 0); .  
3a0af 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
3a0b0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
3a0b1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
3a0b2 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a  te3_free(zSql);.
3a0b3 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
3a0b4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
3a0b5 72 6f 75 74 69 6e 65 20 71 75 65 72 69 65 73 20  routine queries 
3a0b6 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
3a0b7 64 62 20 66 6f 72 20 74 68 65 20 70 61 67 65 2d  db for the page-
3a0b8 73 69 7a 65 20 75 73 65 64 20 62 79 0a 2a 2a 20  size used by.** 
3a0b9 64 61 74 61 62 61 73 65 20 7a 44 62 2e 20 49 66  database zDb. If
3a0ba 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 65   successful, the
3a0bb 20 70 61 67 65 2d 73 69 7a 65 20 69 6e 20 62 79   page-size in by
3a0bc 74 65 73 20 69 73 20 77 72 69 74 74 65 6e 20 74  tes is written t
3a0bd 6f 0a 2a 2a 20 2a 70 69 50 61 67 65 53 69 7a 65  o.** *piPageSize
3a0be 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72   and SQLITE_OK r
3a0bf 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69  eturned. Otherwi
3a0c0 73 65 2c 20 61 6e 64 20 61 6e 20 53 51 4c 69 74  se, and an SQLit
3a0c1 65 20 65 72 72 6f 72 20 0a 2a 2a 20 63 6f 64 65  e error .** code
3a0c2 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
3a0c3 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 50  .static int getP
3a0c4 61 67 65 53 69 7a 65 28 73 71 6c 69 74 65 33 20  ageSize(sqlite3 
3a0c5 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
3a0c6 2a 7a 44 62 2c 20 69 6e 74 20 2a 70 69 50 61 67  *zDb, int *piPag
3a0c7 65 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63  eSize){.  int rc
3a0c8 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
3a0c9 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20  .  char *zSql;. 
3a0ca 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
3a0cb 53 74 6d 74 20 3d 20 30 3b 0a 0a 20 20 7a 53 71  Stmt = 0;..  zSq
3a0cc 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  l = sqlite3_mpri
3a0cd 6e 74 66 28 22 50 52 41 47 4d 41 20 25 51 2e 70  ntf("PRAGMA %Q.p
3a0ce 61 67 65 5f 73 69 7a 65 22 2c 20 7a 44 62 29 3b  age_size", zDb);
3a0cf 0a 20 20 69 66 28 20 21 7a 53 71 6c 20 29 7b 0a  .  if( !zSql ){.
3a0d0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3a0d1 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20  E_NOMEM;.  }..  
3a0d2 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
3a0d3 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c  pare_v2(db, zSql
3a0d4 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29  , -1, &pStmt, 0)
3a0d5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
3a0d6 28 7a 53 71 6c 29 3b 0a 20 20 69 66 28 20 72 63  (zSql);.  if( rc
3a0d7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
3a0d8 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
3a0d9 7d 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  }..  if( SQLITE_
3a0da 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
3a0db 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20  p(pStmt) ){.    
3a0dc 2a 70 69 50 61 67 65 53 69 7a 65 20 3d 20 73 71  *piPageSize = sq
3a0dd 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
3a0de 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a  (pStmt, 0);.  }.
3a0df 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
3a0e0 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
3a0e1 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73  ;.}../* .** This
3a0e2 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 68 65   function is the
3a0e3 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3a0e4 6f 66 20 62 6f 74 68 20 74 68 65 20 78 43 6f 6e  of both the xCon
3a0e5 6e 65 63 74 20 61 6e 64 20 78 43 72 65 61 74 65  nect and xCreate
3a0e6 0a 2a 2a 20 6d 65 74 68 6f 64 73 20 6f 66 20 74  .** methods of t
3a0e7 68 65 20 72 2d 74 72 65 65 20 76 69 72 74 75 61  he r-tree virtua
3a0e8 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20  l table..**.**  
3a0e9 20 61 72 67 76 5b 30 5d 20 20 20 2d 3e 20 6d 6f   argv[0]   -> mo
3a0ea 64 75 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61  dule name.**   a
3a0eb 72 67 76 5b 31 5d 20 20 20 2d 3e 20 64 61 74 61  rgv[1]   -> data
3a0ec 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61  base name.**   a
3a0ed 72 67 76 5b 32 5d 20 20 20 2d 3e 20 74 61 62 6c  rgv[2]   -> tabl
3a0ee 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 61 72 67 76  e name.**   argv
3a0ef 5b 2e 2e 2e 5d 20 2d 3e 20 63 6f 6c 75 6d 6e 20  [...] -> column 
3a0f0 6e 61 6d 65 73 2e 2e 2e 0a 2a 2f 0a 73 74 61 74  names....*/.stat
3a0f1 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 69 74  ic int rtreeInit
3a0f2 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
3a0f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a0f4 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
3a0f5 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
3a0f6 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20  /.  void *pAux, 
3a0f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a0f8 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
3a0f9 66 20 74 68 65 20 52 54 52 45 45 5f 43 4f 4f 52  f the RTREE_COOR
3a0fa 44 5f 2a 20 63 6f 6e 73 74 61 6e 74 73 20 2a 2f  D_* constants */
3a0fb 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e  .  int argc, con
3a0fc 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 61  st char *const*a
3a0fd 72 67 76 2c 20 20 20 2f 2a 20 50 61 72 61 6d 65  rgv,   /* Parame
3a0fe 74 65 72 73 20 74 6f 20 43 52 45 41 54 45 20 54  ters to CREATE T
3a0ff 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 2a  ABLE statement *
3a100 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  /.  sqlite3_vtab
3a101 20 2a 2a 70 70 56 74 61 62 2c 20 20 20 20 20 20   **ppVtab,      
3a102 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3a103 4e 65 77 20 76 69 72 74 75 61 6c 20 74 61 62 6c  New virtual tabl
3a104 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  e */.  char **pz
3a105 45 72 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  Err,            
3a106 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
3a107 54 3a 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65  T: Error message
3a108 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 69 6e  , if any */.  in
3a109 74 20 69 73 43 72 65 61 74 65 20 20 20 20 20 20  t isCreate      
3a10a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a10b 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 78 43    /* True for xC
3a10c 72 65 61 74 65 2c 20 66 61 6c 73 65 20 66 6f 72  reate, false for
3a10d 20 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a   xConnect */.){.
3a10e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3a10f 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 50 61 67  E_OK;.  int iPag
3a110 65 53 69 7a 65 20 3d 20 30 3b 0a 20 20 52 74 72  eSize = 0;.  Rtr
3a111 65 65 20 2a 70 52 74 72 65 65 3b 0a 20 20 69 6e  ee *pRtree;.  in
3a112 74 20 6e 44 62 3b 20 20 20 20 20 20 20 20 20 20  t nDb;          
3a113 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
3a114 20 73 74 72 69 6e 67 20 61 72 67 76 5b 31 5d 20   string argv[1] 
3a115 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 20  */.  int nName; 
3a116 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
3a117 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67 20 61  ngth of string a
3a118 72 67 76 5b 32 5d 20 2a 2f 0a 20 20 69 6e 74 20  rgv[2] */.  int 
3a119 65 43 6f 6f 72 64 54 79 70 65 20 3d 20 28 69 6e  eCoordType = (in
3a11a 74 29 70 41 75 78 3b 0a 0a 20 20 63 6f 6e 73 74  t)pAux;..  const
3a11b 20 63 68 61 72 20 2a 61 45 72 72 4d 73 67 5b 5d   char *aErrMsg[]
3a11c 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 20 20 20   = {.    0,     
3a11d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a11e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a11f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a120 2a 20 30 20 2a 2f 0a 20 20 20 20 22 57 72 6f 6e  * 0 */.    "Wron
3a121 67 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  g number of colu
3a122 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65  mns for an rtree
3a123 20 74 61 62 6c 65 22 2c 20 20 20 20 20 20 20 20   table",        
3a124 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 22 54 6f   /* 1 */.    "To
3a125 6f 20 66 65 77 20 63 6f 6c 75 6d 6e 73 20 66 6f  o few columns fo
3a126 72 20 61 6e 20 72 74 72 65 65 20 74 61 62 6c 65  r an rtree table
3a127 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ",              
3a128 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 22     /* 2 */.    "
3a129 54 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73  Too many columns
3a12a 20 66 6f 72 20 61 6e 20 72 74 72 65 65 20 74 61   for an rtree ta
3a12b 62 6c 65 22 20 20 20 20 20 20 20 20 20 20 20 20  ble"            
3a12c 20 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 7d       /* 3 */.  }
3a12d 3b 0a 0a 20 20 69 6e 74 20 69 45 72 72 20 3d 20  ;..  int iErr = 
3a12e 28 61 72 67 63 3c 36 29 20 3f 20 32 20 3a 20 61  (argc<6) ? 2 : a
3a12f 72 67 63 3e 28 52 54 52 45 45 5f 4d 41 58 5f 44  rgc>(RTREE_MAX_D
3a130 49 4d 45 4e 53 49 4f 4e 53 2a 32 2b 34 29 20 3f  IMENSIONS*2+4) ?
3a131 20 33 20 3a 20 61 72 67 63 25 32 3b 0a 20 20 69   3 : argc%2;.  i
3a132 66 28 20 61 45 72 72 4d 73 67 5b 69 45 72 72 5d  f( aErrMsg[iErr]
3a133 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d   ){.    *pzErr =
3a134 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
3a135 28 22 25 73 22 2c 20 61 45 72 72 4d 73 67 5b 69  ("%s", aErrMsg[i
3a136 45 72 72 5d 29 3b 0a 20 20 20 20 72 65 74 75 72  Err]);.    retur
3a137 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
3a138 20 20 7d 0a 0a 20 20 72 63 20 3d 20 67 65 74 50    }..  rc = getP
3a139 61 67 65 53 69 7a 65 28 64 62 2c 20 61 72 67 76  ageSize(db, argv
3a13a 5b 31 5d 2c 20 26 69 50 61 67 65 53 69 7a 65 29  [1], &iPageSize)
3a13b 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
3a13c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
3a13d 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f  urn rc;.  }..  /
3a13e 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 73  * Allocate the s
3a13f 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
3a140 63 74 75 72 65 20 2a 2f 0a 20 20 6e 44 62 20 3d  cture */.  nDb =
3a141 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29   strlen(argv[1])
3a142 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c  ;.  nName = strl
3a143 65 6e 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 70  en(argv[2]);.  p
3a144 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
3a145 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  )sqlite3_malloc(
3a146 73 69 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e 44  sizeof(Rtree)+nD
3a147 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20 69 66  b+nName+2);.  if
3a148 28 20 21 70 52 74 72 65 65 20 29 7b 0a 20 20 20  ( !pRtree ){.   
3a149 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
3a14a 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  OMEM;.  }.  mems
3a14b 65 74 28 70 52 74 72 65 65 2c 20 30 2c 20 73 69  et(pRtree, 0, si
3a14c 7a 65 6f 66 28 52 74 72 65 65 29 2b 6e 44 62 2b  zeof(Rtree)+nDb+
3a14d 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20 70 52 74 72  nName+2);.  pRtr
3a14e 65 65 2d 3e 6e 42 75 73 79 20 3d 20 31 3b 0a 20  ee->nBusy = 1;. 
3a14f 20 70 52 74 72 65 65 2d 3e 62 61 73 65 2e 70 4d   pRtree->base.pM
3a150 6f 64 75 6c 65 20 3d 20 26 72 74 72 65 65 4d 6f  odule = &rtreeMo
3a151 64 75 6c 65 3b 0a 20 20 70 52 74 72 65 65 2d 3e  dule;.  pRtree->
3a152 7a 44 62 20 3d 20 28 63 68 61 72 20 2a 29 26 70  zDb = (char *)&p
3a153 52 74 72 65 65 5b 31 5d 3b 0a 20 20 70 52 74 72  Rtree[1];.  pRtr
3a154 65 65 2d 3e 7a 4e 61 6d 65 20 3d 20 26 70 52 74  ee->zName = &pRt
3a155 72 65 65 2d 3e 7a 44 62 5b 6e 44 62 2b 31 5d 3b  ree->zDb[nDb+1];
3a156 0a 20 20 70 52 74 72 65 65 2d 3e 6e 44 69 6d 20  .  pRtree->nDim 
3a157 3d 20 28 61 72 67 63 2d 34 29 2f 32 3b 0a 20 20  = (argc-4)/2;.  
3a158 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65  pRtree->nBytesPe
3a159 72 43 65 6c 6c 20 3d 20 38 20 2b 20 70 52 74 72  rCell = 8 + pRtr
3a15a 65 65 2d 3e 6e 44 69 6d 2a 34 2a 32 3b 0a 20 20  ee->nDim*4*2;.  
3a15b 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79  pRtree->eCoordTy
3a15c 70 65 20 3d 20 65 43 6f 6f 72 64 54 79 70 65 3b  pe = eCoordType;
3a15d 0a 20 20 6d 65 6d 63 70 79 28 70 52 74 72 65 65  .  memcpy(pRtree
3a15e 2d 3e 7a 44 62 2c 20 61 72 67 76 5b 31 5d 2c 20  ->zDb, argv[1], 
3a15f 6e 44 62 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  nDb);.  memcpy(p
3a160 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 61 72  Rtree->zName, ar
3a161 67 76 5b 32 5d 2c 20 6e 4e 61 6d 65 29 3b 0a 0a  gv[2], nName);..
3a162 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20    /* Figure out 
3a163 74 68 65 20 6e 6f 64 65 20 73 69 7a 65 20 74 6f  the node size to
3a164 20 75 73 65 2e 20 42 79 20 64 65 66 61 75 6c 74   use. By default
3a165 2c 20 75 73 65 20 36 34 20 62 79 74 65 73 20 6c  , use 64 bytes l
3a166 65 73 73 20 74 68 61 6e 0a 20 20 2a 2a 20 74 68  ess than.  ** th
3a167 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2d  e database page-
3a168 73 69 7a 65 2e 20 54 68 69 73 20 65 6e 73 75 72  size. This ensur
3a169 65 73 20 74 68 61 74 20 65 61 63 68 20 6e 6f 64  es that each nod
3a16a 65 20 69 73 20 73 74 6f 72 65 64 20 6f 6e 0a 20  e is stored on. 
3a16b 20 2a 2a 20 61 20 73 69 6e 67 6c 65 20 64 61 74   ** a single dat
3a16c 61 62 61 73 65 20 70 61 67 65 2e 0a 20 20 2a 2a  abase page..  **
3a16d 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 64 61 74  .  ** If the dat
3a16e 61 62 61 73 64 20 70 61 67 65 2d 73 69 7a 65 20  abasd page-size 
3a16f 69 73 20 73 6f 20 6c 61 72 67 65 20 74 68 61 74  is so large that
3a170 20 6d 6f 72 65 20 74 68 61 6e 20 52 54 52 45 45   more than RTREE
3a171 5f 4d 41 58 43 45 4c 4c 53 0a 20 20 2a 2a 20 65  _MAXCELLS.  ** e
3a172 6e 74 72 69 65 73 20 77 6f 75 6c 64 20 66 69 74  ntries would fit
3a173 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 6e 6f 64   in a single nod
3a174 65 2c 20 75 73 65 20 61 20 73 6d 61 6c 6c 65 72  e, use a smaller
3a175 20 6e 6f 64 65 2d 73 69 7a 65 2e 0a 20 20 2a 2f   node-size..  */
3a176 0a 20 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65  .  pRtree->iNode
3a177 53 69 7a 65 20 3d 20 69 50 61 67 65 53 69 7a 65  Size = iPageSize
3a178 2d 36 34 3b 0a 20 20 69 66 28 20 28 34 2b 70 52  -64;.  if( (4+pR
3a179 74 72 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43  tree->nBytesPerC
3a17a 65 6c 6c 2a 52 54 52 45 45 5f 4d 41 58 43 45 4c  ell*RTREE_MAXCEL
3a17b 4c 53 29 3c 70 52 74 72 65 65 2d 3e 69 4e 6f 64  LS)<pRtree->iNod
3a17c 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 70 52 74  eSize ){.    pRt
3a17d 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 3d  ree->iNodeSize =
3a17e 20 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74 65   4+pRtree->nByte
3a17f 73 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f 4d  sPerCell*RTREE_M
3a180 41 58 43 45 4c 4c 53 3b 0a 20 20 7d 0a 0a 20 20  AXCELLS;.  }..  
3a181 2f 2a 20 43 72 65 61 74 65 2f 43 6f 6e 6e 65 63  /* Create/Connec
3a182 74 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79  t to the underly
3a183 69 6e 67 20 72 65 6c 61 74 69 6f 6e 61 6c 20 64  ing relational d
3a184 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
3a185 49 66 0a 20 20 2a 2a 20 74 68 61 74 20 69 73 20  If.  ** that is 
3a186 73 75 63 63 65 73 73 66 75 6c 2c 20 63 61 6c 6c  successful, call
3a187 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65   sqlite3_declare
3a188 5f 76 74 61 62 28 29 20 74 6f 20 63 6f 6e 66 69  _vtab() to confi
3a189 67 75 72 65 0a 20 20 2a 2a 20 74 68 65 20 72 2d  gure.  ** the r-
3a18a 74 72 65 65 20 74 61 62 6c 65 20 73 63 68 65 6d  tree table schem
3a18b 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 72  a..  */.  if( (r
3a18c 63 20 3d 20 72 74 72 65 65 53 71 6c 49 6e 69 74  c = rtreeSqlInit
3a18d 28 70 52 74 72 65 65 2c 20 64 62 2c 20 61 72 67  (pRtree, db, arg
3a18e 76 5b 31 5d 2c 20 61 72 67 76 5b 32 5d 2c 20 69  v[1], argv[2], i
3a18f 73 43 72 65 61 74 65 29 29 20 29 7b 0a 20 20 20  sCreate)) ){.   
3a190 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65   *pzErr = sqlite
3a191 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20  3_mprintf("%s", 
3a192 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64  sqlite3_errmsg(d
3a193 62 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  b));.  }else{.  
3a194 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73    char *zSql = s
3a195 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
3a196 43 52 45 41 54 45 20 54 41 42 4c 45 20 78 28 25  CREATE TABLE x(%
3a197 73 22 2c 20 61 72 67 76 5b 33 5d 29 3b 0a 20 20  s", argv[3]);.  
3a198 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20    char *zTmp;.  
3a199 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66 6f    int ii;.    fo
3a19a 72 28 69 69 3d 34 3b 20 7a 53 71 6c 20 26 26 20  r(ii=4; zSql && 
3a19b 69 69 3c 61 72 67 63 3b 20 69 69 2b 2b 29 7b 0a  ii<argc; ii++){.
3a19c 20 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53 71        zTmp = zSq
3a19d 6c 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d 20  l;.      zSql = 
3a19e 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
3a19f 22 25 73 2c 20 25 73 22 2c 20 7a 54 6d 70 2c 20  "%s, %s", zTmp, 
3a1a0 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20 20 20  argv[ii]);.     
3a1a1 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54   sqlite3_free(zT
3a1a2 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  mp);.    }.    i
3a1a3 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20  f( zSql ){.     
3a1a4 20 7a 54 6d 70 20 3d 20 7a 53 71 6c 3b 0a 20 20   zTmp = zSql;.  
3a1a5 20 20 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74      zSql = sqlit
3a1a6 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 29 3b  e3_mprintf("%s);
3a1a7 22 2c 20 7a 54 6d 70 29 3b 0a 20 20 20 20 20 20  ", zTmp);.      
3a1a8 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54 6d  sqlite3_free(zTm
3a1a9 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  p);.    }.    if
3a1aa 28 20 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20  ( !zSql ){.     
3a1ab 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
3a1ac 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  EM;.    }else if
3a1ad 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
3a1ae 20 3d 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61   = sqlite3_decla
3a1af 72 65 5f 76 74 61 62 28 64 62 2c 20 7a 53 71 6c  re_vtab(db, zSql
3a1b0 29 29 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a 45  )) ){.      *pzE
3a1b1 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  rr = sqlite3_mpr
3a1b2 69 6e 74 66 28 22 25 73 22 2c 20 73 71 6c 69 74  intf("%s", sqlit
3a1b3 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a  e3_errmsg(db));.
3a1b4 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
3a1b5 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20  3_free(zSql);.  
3a1b6 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  }..  if( rc==SQL
3a1b7 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2a 70  ITE_OK ){.    *p
3a1b8 70 56 74 61 62 20 3d 20 28 73 71 6c 69 74 65 33  pVtab = (sqlite3
3a1b9 5f 76 74 61 62 20 2a 29 70 52 74 72 65 65 3b 0a  _vtab *)pRtree;.
3a1ba 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 74 72    }else{.    rtr
3a1bb 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  eeRelease(pRtree
3a1bc 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
3a1bd 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d  rc;.}.../*.** Im
3a1be 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
3a1bf 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  a scalar functio
3a1c0 6e 20 74 68 61 74 20 64 65 63 6f 64 65 73 20 72  n that decodes r
3a1c1 2d 74 72 65 65 20 6e 6f 64 65 73 20 74 6f 0a 2a  -tree nodes to.*
3a1c2 2a 20 68 75 6d 61 6e 20 72 65 61 64 61 62 6c 65  * human readable
3a1c3 20 73 74 72 69 6e 67 73 2e 20 54 68 69 73 20 63   strings. This c
3a1c4 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 64  an be used for d
3a1c5 65 62 75 67 67 69 6e 67 20 61 6e 64 20 61 6e 61  ebugging and ana
3a1c6 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  lysis..**.** The
3a1c7 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3a1c8 20 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d   takes two argum
3a1c9 65 6e 74 73 2c 20 61 20 62 6c 6f 62 20 6f 66 20  ents, a blob of 
3a1ca 64 61 74 61 20 63 6f 6e 74 61 69 6e 69 6e 67 0a  data containing.
3a1cb 2a 2a 20 61 6e 20 72 2d 74 72 65 65 20 6e 6f 64  ** an r-tree nod
3a1cc 65 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65  e, and the numbe
3a1cd 72 20 6f 66 20 64 69 6d 65 6e 73 69 6f 6e 73 20  r of dimensions 
3a1ce 74 68 65 20 72 2d 74 72 65 65 20 69 6e 64 65 78  the r-tree index
3a1cf 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 74 77 6f  es..** For a two
3a1d0 2d 64 69 6d 65 6e 73 69 6f 6e 61 6c 20 72 2d 74  -dimensional r-t
3a1d1 72 65 65 20 73 74 72 75 63 74 75 72 65 20 63 61  ree structure ca
3a1d2 6c 6c 65 64 20 22 72 74 22 2c 20 74 6f 20 64 65  lled "rt", to de
3a1d3 73 65 72 69 61 6c 69 7a 65 0a 2a 2a 20 61 6c 6c  serialize.** all
3a1d4 20 6e 6f 64 65 73 2c 20 61 20 73 74 61 74 65 6d   nodes, a statem
3a1d5 65 6e 74 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20  ent like:.**.** 
3a1d6 20 20 53 45 4c 45 43 54 20 72 74 72 65 65 6e 6f    SELECT rtreeno
3a1d7 64 65 28 32 2c 20 64 61 74 61 29 20 46 52 4f 4d  de(2, data) FROM
3a1d8 20 72 74 5f 6e 6f 64 65 3b 0a 2a 2a 0a 2a 2a 20   rt_node;.**.** 
3a1d9 54 68 65 20 68 75 6d 61 6e 20 72 65 61 64 61 62  The human readab
3a1da 6c 65 20 73 74 72 69 6e 67 20 74 61 6b 65 73 20  le string takes 
3a1db 74 68 65 20 66 6f 72 6d 20 6f 66 20 61 20 54 63  the form of a Tc
3a1dc 6c 20 6c 69 73 74 20 77 69 74 68 20 6f 6e 65 0a  l list with one.
3a1dd 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63  ** entry for eac
3a1de 68 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72 2d  h cell in the r-
3a1df 74 72 65 65 20 6e 6f 64 65 2e 20 45 61 63 68 20  tree node. Each 
3a1e0 65 6e 74 72 79 20 69 73 20 69 74 73 65 6c 66 20  entry is itself 
3a1e1 61 0a 2a 2a 20 6c 69 73 74 2c 20 63 6f 6e 74 61  a.** list, conta
3a1e2 69 6e 69 6e 67 20 74 68 65 20 38 2d 62 79 74 65  ining the 8-byte
3a1e3 20 72 6f 77 69 64 2f 70 61 67 65 6e 6f 20 66 6f   rowid/pageno fo
3a1e4 6c 6c 6f 77 65 64 20 62 79 20 74 68 65 20 0a 2a  llowed by the .*
3a1e5 2a 20 3c 6e 75 6d 2d 64 69 6d 65 6e 73 69 6f 6e  * <num-dimension
3a1e6 3e 2a 32 20 63 6f 6f 72 64 69 6e 61 74 65 73 2e  >*2 coordinates.
3a1e7 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3a1e8 72 74 72 65 65 6e 6f 64 65 28 73 71 6c 69 74 65  rtreenode(sqlite
3a1e9 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20  3_context *ctx, 
3a1ea 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
3a1eb 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
3a1ec 7b 0a 20 20 63 68 61 72 20 2a 7a 54 65 78 74 20  {.  char *zText 
3a1ed 3d 20 30 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65  = 0;.  RtreeNode
3a1ee 20 6e 6f 64 65 3b 0a 20 20 52 74 72 65 65 20 74   node;.  Rtree t
3a1ef 72 65 65 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a  ree;.  int ii;..
3a1f0 20 20 6d 65 6d 73 65 74 28 26 6e 6f 64 65 2c 20    memset(&node, 
3a1f1 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 4e  0, sizeof(RtreeN
3a1f2 6f 64 65 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28  ode));.  memset(
3a1f3 26 74 72 65 65 2c 20 30 2c 20 73 69 7a 65 6f 66  &tree, 0, sizeof
3a1f4 28 52 74 72 65 65 29 29 3b 0a 20 20 74 72 65 65  (Rtree));.  tree
3a1f5 2e 6e 44 69 6d 20 3d 20 73 71 6c 69 74 65 33 5f  .nDim = sqlite3_
3a1f6 76 61 6c 75 65 5f 69 6e 74 28 61 70 41 72 67 5b  value_int(apArg[
3a1f7 30 5d 29 3b 0a 20 20 74 72 65 65 2e 6e 42 79 74  0]);.  tree.nByt
3a1f8 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20 2b 20  esPerCell = 8 + 
3a1f9 38 20 2a 20 74 72 65 65 2e 6e 44 69 6d 3b 0a 20  8 * tree.nDim;. 
3a1fa 20 6e 6f 64 65 2e 7a 44 61 74 61 20 3d 20 28 75   node.zData = (u
3a1fb 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  8 *)sqlite3_valu
3a1fc 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 31 5d 29  e_blob(apArg[1])
3a1fd 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69  ;..  for(ii=0; i
3a1fe 69 3c 4e 43 45 4c 4c 28 26 6e 6f 64 65 29 3b 20  i<NCELL(&node); 
3a1ff 69 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20  ii++){.    char 
3a200 7a 43 65 6c 6c 5b 35 31 32 5d 3b 0a 20 20 20 20  zCell[512];.    
3a201 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 30 3b 0a 20  int nCell = 0;. 
3a202 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c     RtreeCell cel
3a203 6c 3b 0a 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 0a  l;.    int jj;..
3a204 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
3a205 26 74 72 65 65 2c 20 26 6e 6f 64 65 2c 20 69 69  &tree, &node, ii
3a206 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 73 71  , &cell);.    sq
3a207 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 35  lite3_snprintf(5
3a208 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b  12-nCell,&zCell[
3a209 6e 43 65 6c 6c 5d 2c 22 25 64 22 2c 20 63 65 6c  nCell],"%d", cel
3a20a 6c 2e 69 52 6f 77 69 64 29 3b 0a 20 20 20 20 6e  l.iRowid);.    n
3a20b 43 65 6c 6c 20 3d 20 73 74 72 6c 65 6e 28 7a 43  Cell = strlen(zC
3a20c 65 6c 6c 29 3b 0a 20 20 20 20 66 6f 72 28 6a 6a  ell);.    for(jj
3a20d 3d 30 3b 20 6a 6a 3c 74 72 65 65 2e 6e 44 69 6d  =0; jj<tree.nDim
3a20e 2a 32 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20  *2; jj++){.     
3a20f 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
3a210 66 28 35 31 32 2d 6e 43 65 6c 6c 2c 26 7a 43 65  f(512-nCell,&zCe
3a211 6c 6c 5b 6e 43 65 6c 6c 5d 2c 22 20 25 66 22 2c  ll[nCell]," %f",
3a212 28 64 6f 75 62 6c 65 29 63 65 6c 6c 2e 61 43 6f  (double)cell.aCo
3a213 6f 72 64 5b 6a 6a 5d 2e 66 29 3b 0a 20 20 20 20  ord[jj].f);.    
3a214 20 20 6e 43 65 6c 6c 20 3d 20 73 74 72 6c 65 6e    nCell = strlen
3a215 28 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 0a  (zCell);.    }..
3a216 20 20 20 20 69 66 28 20 7a 54 65 78 74 20 29 7b      if( zText ){
3a217 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 65  .      char *zTe
3a218 78 74 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f  xtNew = sqlite3_
3a219 6d 70 72 69 6e 74 66 28 22 25 73 20 7b 25 73 7d  mprintf("%s {%s}
3a21a 22 2c 20 7a 54 65 78 74 2c 20 7a 43 65 6c 6c 29  ", zText, zCell)
3a21b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
3a21c 66 72 65 65 28 7a 54 65 78 74 29 3b 0a 20 20 20  free(zText);.   
3a21d 20 20 20 7a 54 65 78 74 20 3d 20 7a 54 65 78 74     zText = zText
3a21e 4e 65 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  New;.    }else{.
3a21f 20 20 20 20 20 20 7a 54 65 78 74 20 3d 20 73 71        zText = sq
3a220 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 7b  lite3_mprintf("{
3a221 25 73 7d 22 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20  %s}", zCell);.  
3a222 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 73 71 6c    }.  }.  .  sql
3a223 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
3a224 28 63 74 78 2c 20 7a 54 65 78 74 2c 20 2d 31 2c  (ctx, zText, -1,
3a225 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
3a226 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  }..static void r
3a227 74 72 65 65 64 65 70 74 68 28 73 71 6c 69 74 65  treedepth(sqlite
3a228 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c 20  3_context *ctx, 
3a229 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
3a22a 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
3a22b 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  {.  if( sqlite3_
3a22c 76 61 6c 75 65 5f 74 79 70 65 28 61 70 41 72 67  value_type(apArg
3a22d 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  [0])!=SQLITE_BLO
3a22e 42 20 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33  B .   || sqlite3
3a22f 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 70 41  _value_bytes(apA
3a230 72 67 5b 30 5d 29 3c 32 0a 20 20 29 7b 0a 20 20  rg[0])<2.  ){.  
3a231 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3a232 5f 65 72 72 6f 72 28 63 74 78 2c 20 22 49 6e 76  _error(ctx, "Inv
3a233 61 6c 69 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  alid argument to
3a234 20 72 74 72 65 65 64 65 70 74 68 28 29 22 2c 20   rtreedepth()", 
3a235 2d 31 29 3b 20 0a 20 20 7d 65 6c 73 65 7b 0a 20  -1); .  }else{. 
3a236 20 20 20 75 38 20 2a 7a 42 6c 6f 62 20 3d 20 28     u8 *zBlob = (
3a237 75 38 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  u8 *)sqlite3_val
3a238 75 65 5f 62 6c 6f 62 28 61 70 41 72 67 5b 30 5d  ue_blob(apArg[0]
3a239 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
3a23a 65 73 75 6c 74 5f 69 6e 74 28 63 74 78 2c 20 72  esult_int(ctx, r
3a23b 65 61 64 49 6e 74 31 36 28 7a 42 6c 6f 62 29 29  eadInt16(zBlob))
3a23c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
3a23d 65 67 69 73 74 65 72 20 74 68 65 20 72 2d 74 72  egister the r-tr
3a23e 65 65 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 64  ee module with d
3a23f 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64  atabase handle d
3a240 62 2e 20 54 68 69 73 20 63 72 65 61 74 65 73 20  b. This creates 
3a241 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74  the.** virtual t
3a242 61 62 6c 65 20 6d 6f 64 75 6c 65 20 22 72 74 72  able module "rtr
3a243 65 65 22 20 61 6e 64 20 74 68 65 20 64 65 62 75  ee" and the debu
3a244 67 67 69 6e 67 2f 61 6e 61 6c 79 73 69 73 20 73  gging/analysis s
3a245 63 61 6c 61 72 20 0a 2a 2a 20 66 75 6e 63 74 69  calar .** functi
3a246 6f 6e 20 22 72 74 72 65 65 6e 6f 64 65 22 2e 0a  on "rtreenode"..
3a247 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
3a248 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 74 72  E int sqlite3Rtr
3a249 65 65 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a  eeInit(sqlite3 *
3a24a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  db){.  int rc = 
3a24b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66  SQLITE_OK;..  if
3a24c 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3a24d 29 7b 0a 20 20 20 20 69 6e 74 20 75 74 66 38 20  ){.    int utf8 
3a24e 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20  = SQLITE_UTF8;. 
3a24f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
3a250 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
3a251 64 62 2c 20 22 72 74 72 65 65 6e 6f 64 65 22 2c  db, "rtreenode",
3a252 20 32 2c 20 75 74 66 38 2c 20 30 2c 20 72 74 72   2, utf8, 0, rtr
3a253 65 65 6e 6f 64 65 2c 20 30 2c 20 30 29 3b 0a 20  eenode, 0, 0);. 
3a254 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
3a255 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e  ITE_OK ){.    in
3a256 74 20 75 74 66 38 20 3d 20 53 51 4c 49 54 45 5f  t utf8 = SQLITE_
3a257 55 54 46 38 3b 0a 20 20 20 20 72 63 20 3d 20 73  UTF8;.    rc = s
3a258 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
3a259 6e 63 74 69 6f 6e 28 64 62 2c 20 22 72 74 72 65  nction(db, "rtre
3a25a 65 64 65 70 74 68 22 2c 20 31 2c 20 75 74 66 38  edepth", 1, utf8
3a25b 2c 20 30 2c 72 74 72 65 65 64 65 70 74 68 2c 20  , 0,rtreedepth, 
3a25c 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28  0, 0);.  }.  if(
3a25d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3a25e 7b 0a 20 20 20 20 76 6f 69 64 20 2a 63 20 3d 20  {.    void *c = 
3a25f 28 76 6f 69 64 20 2a 29 52 54 52 45 45 5f 43 4f  (void *)RTREE_CO
3a260 4f 52 44 5f 52 45 41 4c 33 32 3b 0a 20 20 20 20  ORD_REAL32;.    
3a261 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
3a262 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 64 62  ate_module_v2(db
3a263 2c 20 22 72 74 72 65 65 22 2c 20 26 72 74 72 65  , "rtree", &rtre
3a264 65 4d 6f 64 75 6c 65 2c 20 63 2c 20 30 29 3b 0a  eModule, c, 0);.
3a265 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
3a266 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 76  LITE_OK ){.    v
3a267 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a  oid *c = (void *
3a268 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54  )RTREE_COORD_INT
3a269 33 32 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  32;.    rc = sql
3a26a 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75  ite3_create_modu
3a26b 6c 65 5f 76 32 28 64 62 2c 20 22 72 74 72 65 65  le_v2(db, "rtree
3a26c 5f 69 33 32 22 2c 20 26 72 74 72 65 65 4d 6f 64  _i32", &rtreeMod
3a26d 75 6c 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d 0a  ule, c, 0);.  }.
3a26e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3a26f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 43 4f 52  .#if !SQLITE_COR
3a270 45 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  E.SQLITE_API int
3a271 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69   sqlite3_extensi
3a272 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c 69 74  on_init(.  sqlit
3a273 65 33 20 2a 64 62 2c 0a 20 20 63 68 61 72 20 2a  e3 *db,.  char *
3a274 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e  *pzErrMsg,.  con
3a275 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
3a276 6f 75 74 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b  outines *pApi.){
3a277 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53  .  SQLITE_EXTENS
3a278 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0a  ION_INIT2(pApi).
3a279 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
3a27a 52 74 72 65 65 49 6e 69 74 28 64 62 29 3b 0a 7d  RtreeInit(db);.}
3a27b 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a  .#endif..#endif.
3a27c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3a27d 20 45 6e 64 20 6f 66 20 72 74 72 65 65 2e 63 20   End of rtree.c 
3a27e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a27f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3a281 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3a282 20 42 65 67 69 6e 20 66 69 6c 65 20 69 63 75 2e   Begin file icu.
3a283 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
3a284 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a285 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3a286 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20  ./*.** 2007 May 
3a287 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
3a288 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
3a289 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
3a28a 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
3a28b 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
3a28c 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
3a28d 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
3a28e 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
3a28f 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
3a290 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
3a291 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
3a292 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
3a293 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
3a294 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
3a295 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
3a296 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
3a297 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
3a298 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
3a299 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a29a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a29b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a29c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a29d 0a 2a 2a 20 24 49 64 3a 20 69 63 75 2e 63 2c 76  .** $Id: icu.c,v
3a29e 20 31 2e 37 20 32 30 30 37 2f 31 32 2f 31 33 20   1.7 2007/12/13 
3a29f 32 31 3a 35 34 3a 31 31 20 64 72 68 20 45 78 70  21:54:11 drh Exp
3a2a0 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69   $.**.** This fi
3a2a1 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 6e  le implements an
3a2a2 20 69 6e 74 65 67 72 61 74 69 6f 6e 20 62 65 74   integration bet
3a2a3 77 65 65 6e 20 74 68 65 20 49 43 55 20 6c 69 62  ween the ICU lib
3a2a4 72 61 72 79 20 0a 2a 2a 20 28 22 49 6e 74 65 72  rary .** ("Inter
3a2a5 6e 61 74 69 6f 6e 61 6c 20 43 6f 6d 70 6f 6e 65  national Compone
3a2a6 6e 74 73 20 66 6f 72 20 55 6e 69 63 6f 64 65 22  nts for Unicode"
3a2a7 2c 20 61 6e 20 6f 70 65 6e 2d 73 6f 75 72 63 65  , an open-source
3a2a8 20 6c 69 62 72 61 72 79 20 0a 2a 2a 20 66 6f 72   library .** for
3a2a9 20 68 61 6e 64 6c 69 6e 67 20 75 6e 69 63 6f 64   handling unicod
3a2aa 65 20 64 61 74 61 29 20 61 6e 64 20 53 51 4c 69  e data) and SQLi
3a2ab 74 65 2e 20 54 68 65 20 69 6e 74 65 67 72 61 74  te. The integrat
3a2ac 69 6f 6e 20 75 73 65 73 20 0a 2a 2a 20 49 43 55  ion uses .** ICU
3a2ad 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20   to provide the 
3a2ae 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 20 53 51 4c  following to SQL
3a2af 69 74 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41  ite:.**.**   * A
3a2b0 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
3a2b1 20 6f 66 20 74 68 65 20 53 51 4c 20 72 65 67 65   of the SQL rege
3a2b2 78 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 61  xp() function (a
3a2b3 6e 64 20 68 65 6e 63 65 20 52 45 47 45 58 50 0a  nd hence REGEXP.
3a2b4 2a 2a 20 20 20 20 20 6f 70 65 72 61 74 6f 72 29  **     operator)
3a2b5 20 75 73 69 6e 67 20 74 68 65 20 49 43 55 20 75   using the ICU u
3a2b6 72 65 67 65 78 5f 58 58 28 29 20 41 50 49 73 2e  regex_XX() APIs.
3a2b7 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6d 70 6c 65  .**.**   * Imple
3a2b8 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  mentations of th
3a2b9 65 20 53 51 4c 20 73 63 61 6c 61 72 20 75 70 70  e SQL scalar upp
3a2ba 65 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 29  er() and lower()
3a2bb 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 20   functions.**   
3a2bc 20 20 66 6f 72 20 63 61 73 65 20 6d 61 70 70 69    for case mappi
3a2bd 6e 67 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 6e  ng..**.**   * In
3a2be 74 65 67 72 61 74 69 6f 6e 20 6f 66 20 49 43 55  tegration of ICU
3a2bf 20 61 6e 64 20 53 51 4c 69 74 65 20 63 6f 6c 6c   and SQLite coll
3a2c0 61 74 69 6f 6e 20 73 65 71 65 6e 63 65 73 2e 0a  ation seqences..
3a2c1 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d 70  **.**   * An imp
3a2c2 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
3a2c3 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72  he LIKE operator
3a2c4 20 74 68 61 74 20 75 73 65 73 20 49 43 55 20 74   that uses ICU t
3a2c5 6f 20 0a 2a 2a 20 20 20 20 20 70 72 6f 76 69 64  o .**     provid
3a2c6 65 20 63 61 73 65 2d 69 6e 64 65 70 65 6e 64 65  e case-independe
3a2c7 6e 74 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2f 0a  nt matching..*/.
3a2c8 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
3a2c9 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
3a2ca 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
3a2cb 42 4c 45 5f 49 43 55 29 0a 0a 2f 2a 20 49 6e 63  BLE_ICU)../* Inc
3a2cc 6c 75 64 65 20 49 43 55 20 68 65 61 64 65 72 73  lude ICU headers
3a2cd 20 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e   */.#include <un
3a2ce 69 63 6f 64 65 2f 75 74 79 70 65 73 2e 68 3e 0a  icode/utypes.h>.
3a2cf 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64  #include <unicod
3a2d0 65 2f 75 72 65 67 65 78 2e 68 3e 0a 23 69 6e 63  e/uregex.h>.#inc
3a2d1 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 73  lude <unicode/us
3a2d2 74 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64  tring.h>.#includ
3a2d3 65 20 3c 75 6e 69 63 6f 64 65 2f 75 63 6f 6c 2e  e <unicode/ucol.
3a2d4 68 3e 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  h>...#ifndef SQL
3a2d5 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54  ITE_CORE.  SQLIT
3a2d6 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54  E_EXTENSION_INIT
3a2d7 31 0a 23 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a  1.#else.#endif..
3a2d8 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65  /*.** Maximum le
3a2d9 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73 29 20  ngth (in bytes) 
3a2da 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e 20 69  of the pattern i
3a2db 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42  n a LIKE or GLOB
3a2dc 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f  .** operator..*/
3a2dd 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
3a2de 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MAX_LIKE_PATTERN
3a2df 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65  _LENGTH.# define
3a2e0 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45   SQLITE_MAX_LIKE
3a2e1 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20  _PATTERN_LENGTH 
3a2e2 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  50000.#endif../*
3a2e3 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 6f 66 20 73  .** Version of s
3a2e4 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 74 68  qlite3_free() th
3a2e5 61 74 20 69 73 20 61 6c 77 61 79 73 20 61 20 66  at is always a f
3a2e6 75 6e 63 74 69 6f 6e 2c 20 6e 65 76 65 72 20 61  unction, never a
3a2e7 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69   macro..*/.stati
3a2e8 63 20 76 6f 69 64 20 78 46 72 65 65 28 76 6f 69  c void xFree(voi
3a2e9 64 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33  d *p){.  sqlite3
3a2ea 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  _free(p);.}../*.
3a2eb 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f 20 55  ** Compare two U
3a2ec 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66 6f 72  TF-8 strings for
3a2ed 20 65 71 75 61 6c 69 74 79 20 77 68 65 72 65 20   equality where 
3a2ee 74 68 65 20 66 69 72 73 74 20 73 74 72 69 6e 67  the first string
3a2ef 20 69 73 0a 2a 2a 20 61 20 22 4c 49 4b 45 22 20   is.** a "LIKE" 
3a2f0 65 78 70 72 65 73 73 69 6f 6e 2e 20 52 65 74 75  expression. Retu
3a2f1 72 6e 20 74 72 75 65 20 28 31 29 20 69 66 20 74  rn true (1) if t
3a2f2 68 65 79 20 61 72 65 20 74 68 65 20 73 61 6d 65  hey are the same
3a2f3 20 61 6e 64 20 0a 2a 2a 20 66 61 6c 73 65 20 28   and .** false (
3a2f4 30 29 20 69 66 20 74 68 65 79 20 61 72 65 20 64  0) if they are d
3a2f5 69 66 66 65 72 65 6e 74 2e 0a 2a 2f 0a 73 74 61  ifferent..*/.sta
3a2f6 74 69 63 20 69 6e 74 20 69 63 75 4c 69 6b 65 43  tic int icuLikeC
3a2f7 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74 20  ompare(.  const 
3a2f8 75 69 6e 74 38 5f 74 20 2a 7a 50 61 74 74 65 72  uint8_t *zPatter
3a2f9 6e 2c 20 20 20 2f 2a 20 4c 49 4b 45 20 70 61 74  n,   /* LIKE pat
3a2fa 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tern */.  const 
3a2fb 75 69 6e 74 38 5f 74 20 2a 7a 53 74 72 69 6e 67  uint8_t *zString
3a2fc 2c 20 20 20 20 2f 2a 20 54 68 65 20 55 54 46 2d  ,    /* The UTF-
3a2fd 38 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 6d 70  8 string to comp
3a2fe 61 72 65 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20  are against */. 
3a2ff 20 63 6f 6e 73 74 20 55 43 68 61 72 33 32 20 75   const UChar32 u
3a300 45 73 63 20 20 20 20 20 20 20 20 20 2f 2a 20 54  Esc         /* T
3a301 68 65 20 65 73 63 61 70 65 20 63 68 61 72 61 63  he escape charac
3a302 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 74 61 74  ter */.){.  stat
3a303 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 4d 41 54  ic const int MAT
3a304 43 48 5f 4f 4e 45 20 3d 20 28 55 43 68 61 72 33  CH_ONE = (UChar3
3a305 32 29 27 5f 27 3b 0a 20 20 73 74 61 74 69 63 20  2)'_';.  static 
3a306 63 6f 6e 73 74 20 69 6e 74 20 4d 41 54 43 48 5f  const int MATCH_
3a307 41 4c 4c 20 3d 20 28 55 43 68 61 72 33 32 29 27  ALL = (UChar32)'
3a308 25 27 3b 0a 0a 20 20 69 6e 74 20 69 50 61 74 74  %';..  int iPatt
3a309 65 72 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 2f  ern = 0;       /
3a30a 2a 20 43 75 72 72 65 6e 74 20 62 79 74 65 20 69  * Current byte i
3a30b 6e 64 65 78 20 69 6e 20 7a 50 61 74 74 65 72 6e  ndex in zPattern
3a30c 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 72 69 6e   */.  int iStrin
3a30d 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  g = 0;        /*
3a30e 20 43 75 72 72 65 6e 74 20 62 79 74 65 20 69 6e   Current byte in
3a30f 64 65 78 20 69 6e 20 7a 53 74 72 69 6e 67 20 2a  dex in zString *
3a310 2f 0a 0a 20 20 69 6e 74 20 70 72 65 76 45 73 63  /..  int prevEsc
3a311 61 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ape = 0;     /* 
3a312 54 72 75 65 20 69 66 20 74 68 65 20 70 72 65 76  True if the prev
3a313 69 6f 75 73 20 63 68 61 72 61 63 74 65 72 20 77  ious character w
3a314 61 73 20 75 45 73 63 20 2a 2f 0a 0a 20 20 77 68  as uEsc */..  wh
3a315 69 6c 65 28 20 7a 50 61 74 74 65 72 6e 5b 69 50  ile( zPattern[iP
3a316 61 74 74 65 72 6e 5d 21 3d 30 20 29 7b 0a 0a 20  attern]!=0 ){.. 
3a317 20 20 20 2f 2a 20 52 65 61 64 20 28 61 6e 64 20     /* Read (and 
3a318 63 6f 6e 73 75 6d 65 29 20 74 68 65 20 6e 65 78  consume) the nex
3a319 74 20 63 68 61 72 61 63 74 65 72 20 66 72 6f 6d  t character from
3a31a 20 74 68 65 20 69 6e 70 75 74 20 70 61 74 74 65   the input patte
3a31b 72 6e 2e 20 2a 2f 0a 20 20 20 20 55 43 68 61 72  rn. */.    UChar
3a31c 33 32 20 75 50 61 74 74 65 72 6e 3b 0a 20 20 20  32 uPattern;.   
3a31d 20 55 38 5f 4e 45 58 54 5f 55 4e 53 41 46 45 28   U8_NEXT_UNSAFE(
3a31e 7a 50 61 74 74 65 72 6e 2c 20 69 50 61 74 74 65  zPattern, iPatte
3a31f 72 6e 2c 20 75 50 61 74 74 65 72 6e 29 3b 0a 20  rn, uPattern);. 
3a320 20 20 20 61 73 73 65 72 74 28 75 50 61 74 74 65     assert(uPatte
3a321 72 6e 21 3d 30 29 3b 0a 0a 20 20 20 20 2f 2a 20  rn!=0);..    /* 
3a322 54 68 65 72 65 20 61 72 65 20 6e 6f 77 20 34 20  There are now 4 
3a323 70 6f 73 73 69 62 69 6c 69 74 69 65 73 3a 0a 20  possibilities:. 
3a324 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
3a325 20 31 2e 20 75 50 61 74 74 65 72 6e 20 69 73 20   1. uPattern is 
3a326 61 6e 20 75 6e 65 73 63 61 70 65 64 20 6d 61 74  an unescaped mat
3a327 63 68 2d 61 6c 6c 20 63 68 61 72 61 63 74 65 72  ch-all character
3a328 20 22 25 22 2c 0a 20 20 20 20 2a 2a 20 20 20 20   "%",.    **    
3a329 20 32 2e 20 75 50 61 74 74 65 72 6e 20 69 73 20   2. uPattern is 
3a32a 61 6e 20 75 6e 65 73 63 61 70 65 64 20 6d 61 74  an unescaped mat
3a32b 63 68 2d 6f 6e 65 20 63 68 61 72 61 63 74 65 72  ch-one character
3a32c 20 22 5f 22 2c 0a 20 20 20 20 2a 2a 20 20 20 20   "_",.    **    
3a32d 20 33 2e 20 75 50 61 74 74 65 72 6e 20 69 73 20   3. uPattern is 
3a32e 61 6e 20 75 6e 65 73 63 61 70 65 64 20 65 73 63  an unescaped esc
3a32f 61 70 65 20 63 68 61 72 61 63 74 65 72 2c 20 6f  ape character, o
3a330 72 0a 20 20 20 20 2a 2a 20 20 20 20 20 34 2e 20  r.    **     4. 
3a331 75 50 61 74 74 65 72 6e 20 69 73 20 74 6f 20 62  uPattern is to b
3a332 65 20 68 61 6e 64 6c 65 64 20 61 73 20 61 6e 20  e handled as an 
3a333 6f 72 64 69 6e 61 72 79 20 63 68 61 72 61 63 74  ordinary charact
3a334 65 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  er.    */.    if
3a335 28 20 21 70 72 65 76 45 73 63 61 70 65 20 26 26  ( !prevEscape &&
3a336 20 75 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43 48   uPattern==MATCH
3a337 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 20 20 2f 2a  _ALL ){.      /*
3a338 20 43 61 73 65 20 31 2e 20 2a 2f 0a 20 20 20 20   Case 1. */.    
3a339 20 20 75 69 6e 74 38 5f 74 20 63 3b 0a 0a 20 20    uint8_t c;..  
3a33a 20 20 20 20 2f 2a 20 53 6b 69 70 20 61 6e 79 20      /* Skip any 
3a33b 4d 41 54 43 48 5f 41 4c 4c 20 6f 72 20 4d 41 54  MATCH_ALL or MAT
3a33c 43 48 5f 4f 4e 45 20 63 68 61 72 61 63 74 65 72  CH_ONE character
3a33d 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 61 0a  s that follow a.
3a33e 20 20 20 20 20 20 2a 2a 20 4d 41 54 43 48 5f 41        ** MATCH_A
3a33f 4c 4c 2e 20 46 6f 72 20 65 61 63 68 20 4d 41 54  LL. For each MAT
3a340 43 48 5f 4f 4e 45 2c 20 73 6b 69 70 20 6f 6e 65  CH_ONE, skip one
3a341 20 63 68 61 72 61 63 74 65 72 20 69 6e 20 74 68   character in th
3a342 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 65 73 74  e .      ** test
3a343 20 73 74 72 69 6e 67 2e 0a 20 20 20 20 20 20 2a   string..      *
3a344 2f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28  /.      while( (
3a345 63 3d 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74  c=zPattern[iPatt
3a346 65 72 6e 5d 29 20 3d 3d 20 4d 41 54 43 48 5f 41  ern]) == MATCH_A
3a347 4c 4c 20 7c 7c 20 63 20 3d 3d 20 4d 41 54 43 48  LL || c == MATCH
3a348 5f 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20  _ONE ){.        
3a349 69 66 28 20 63 3d 3d 4d 41 54 43 48 5f 4f 4e 45  if( c==MATCH_ONE
3a34a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
3a34b 28 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e  ( zString[iStrin
3a34c 67 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  g]==0 ) return 0
3a34d 3b 0a 20 20 20 20 20 20 20 20 20 20 55 38 5f 46  ;.          U8_F
3a34e 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74 72  WD_1_UNSAFE(zStr
3a34f 69 6e 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a 20  ing, iString);. 
3a350 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3a351 20 69 50 61 74 74 65 72 6e 2b 2b 3b 0a 20 20 20   iPattern++;.   
3a352 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20     }..      if( 
3a353 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72  zPattern[iPatter
3a354 6e 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 31  n]==0 ) return 1
3a355 3b 0a 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ;..      while( 
3a356 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d  zString[iString]
3a357 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
3a358 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28 26  icuLikeCompare(&
3a359 7a 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72  zPattern[iPatter
3a35a 6e 5d 2c 20 26 7a 53 74 72 69 6e 67 5b 69 53 74  n], &zString[iSt
3a35b 72 69 6e 67 5d 2c 20 75 45 73 63 29 20 29 7b 0a  ring], uEsc) ){.
3a35c 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
3a35d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
3a35e 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55        U8_FWD_1_U
3a35f 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69  NSAFE(zString, i
3a360 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 7d  String);.      }
3a361 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
3a362 0a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ..    }else if( 
3a363 21 70 72 65 76 45 73 63 61 70 65 20 26 26 20 75  !prevEscape && u
3a364 50 61 74 74 65 72 6e 3d 3d 4d 41 54 43 48 5f 4f  Pattern==MATCH_O
3a365 4e 45 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43  NE ){.      /* C
3a366 61 73 65 20 32 2e 20 2a 2f 0a 20 20 20 20 20 20  ase 2. */.      
3a367 69 66 28 20 7a 53 74 72 69 6e 67 5b 69 53 74 72  if( zString[iStr
3a368 69 6e 67 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e  ing]==0 ) return
3a369 20 30 3b 0a 20 20 20 20 20 20 55 38 5f 46 57 44   0;.      U8_FWD
3a36a 5f 31 5f 55 4e 53 41 46 45 28 7a 53 74 72 69 6e  _1_UNSAFE(zStrin
3a36b 67 2c 20 69 53 74 72 69 6e 67 29 3b 0a 0a 20 20  g, iString);..  
3a36c 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65    }else if( !pre
3a36d 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74  vEscape && uPatt
3a36e 65 72 6e 3d 3d 75 45 73 63 29 7b 0a 20 20 20 20  ern==uEsc){.    
3a36f 20 20 2f 2a 20 43 61 73 65 20 33 2e 20 2a 2f 0a    /* Case 3. */.
3a370 20 20 20 20 20 20 70 72 65 76 45 73 63 61 70 65        prevEscape
3a371 20 3d 20 31 3b 0a 0a 20 20 20 20 7d 65 6c 73 65   = 1;..    }else
3a372 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20  {.      /* Case 
3a373 34 2e 20 2a 2f 0a 20 20 20 20 20 20 55 43 68 61  4. */.      UCha
3a374 72 33 32 20 75 53 74 72 69 6e 67 3b 0a 20 20 20  r32 uString;.   
3a375 20 20 20 55 38 5f 4e 45 58 54 5f 55 4e 53 41 46     U8_NEXT_UNSAF
3a376 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69  E(zString, iStri
3a377 6e 67 2c 20 75 53 74 72 69 6e 67 29 3b 0a 20 20  ng, uString);.  
3a378 20 20 20 20 75 53 74 72 69 6e 67 20 3d 20 75 5f      uString = u_
3a379 66 6f 6c 64 43 61 73 65 28 75 53 74 72 69 6e 67  foldCase(uString
3a37a 2c 20 55 5f 46 4f 4c 44 5f 43 41 53 45 5f 44 45  , U_FOLD_CASE_DE
3a37b 46 41 55 4c 54 29 3b 0a 20 20 20 20 20 20 75 50  FAULT);.      uP
3a37c 61 74 74 65 72 6e 20 3d 20 75 5f 66 6f 6c 64 43  attern = u_foldC
3a37d 61 73 65 28 75 50 61 74 74 65 72 6e 2c 20 55 5f  ase(uPattern, U_
3a37e 46 4f 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c  FOLD_CASE_DEFAUL
3a37f 54 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75 53  T);.      if( uS
3a380 74 72 69 6e 67 21 3d 75 50 61 74 74 65 72 6e 20  tring!=uPattern 
3a381 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
3a382 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
3a383 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20     prevEscape = 
3a384 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  0;.    }.  }..  
3a385 72 65 74 75 72 6e 20 7a 53 74 72 69 6e 67 5b 69  return zString[i
3a386 53 74 72 69 6e 67 5d 3d 3d 30 3b 0a 7d 0a 0a 2f  String]==0;.}../
3a387 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3a388 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 6b 65 28  ion of the like(
3a389 29 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20  ) SQL function. 
3a38a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
3a38b 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 65  mplements.** the
3a38c 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b 45 20 6f   build-in LIKE o
3a38d 70 65 72 61 74 6f 72 2e 20 20 54 68 65 20 66 69  perator.  The fi
3a38e 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
3a38f 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
3a390 74 68 65 0a 2a 2a 20 70 61 74 74 65 72 6e 20 61  the.** pattern a
3a391 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  nd the second ar
3a392 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73 74  gument is the st
3a393 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68 65 20 53  ring.  So, the S
3a394 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a  QL statements:.*
3a395 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20 4c 49 4b  *.**       A LIK
3a396 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20 69 6d 70  E B.**.** is imp
3a397 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c 69 6b 65  lemented as like
3a398 28 42 2c 20 41 29 2e 20 49 66 20 74 68 65 72 65  (B, A). If there
3a399 20 69 73 20 61 6e 20 65 73 63 61 70 65 20 63 68   is an escape ch
3a39a 61 72 61 63 74 65 72 20 45 2c 20 0a 2a 2a 0a 2a  aracter E, .**.*
3a39b 2a 20 20 20 20 20 20 20 41 20 4c 49 4b 45 20 42  *       A LIKE B
3a39c 20 45 53 43 41 50 45 20 45 0a 2a 2a 0a 2a 2a 20   ESCAPE E.**.** 
3a39d 69 73 20 6d 61 70 70 65 64 20 74 6f 20 6c 69 6b  is mapped to lik
3a39e 65 28 42 2c 20 41 2c 20 45 29 2e 0a 2a 2f 0a 73  e(B, A, E)..*/.s
3a39f 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 4c 69  tatic void icuLi
3a3a0 6b 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  keFunc(.  sqlite
3a3a1 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
3a3a2 78 74 2c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c  xt, .  int argc,
3a3a3 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   .  sqlite3_valu
3a3a4 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f  e **argv.){.  co
3a3a5 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
3a3a6 72 20 2a 7a 41 20 3d 20 73 71 6c 69 74 65 33 5f  r *zA = sqlite3_
3a3a7 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
3a3a8 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  0]);.  const uns
3a3a9 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 42 20 3d  igned char *zB =
3a3aa 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
3a3ab 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  ext(argv[1]);.  
3a3ac 55 43 68 61 72 33 32 20 75 45 73 63 20 3d 20 30  UChar32 uEsc = 0
3a3ad 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74 68  ;..  /* Limit th
3a3ae 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  e length of the 
3a3af 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74  LIKE or GLOB pat
3a3b0 74 65 72 6e 20 74 6f 20 61 76 6f 69 64 20 70 72  tern to avoid pr
3a3b1 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20 64  oblems.  ** of d
3a3b2 65 65 70 20 72 65 63 75 72 73 69 6f 6e 20 61 6e  eep recursion an
3a3b3 64 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20 69  d N*N behavior i
3a3b4 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65  n patternCompare
3a3b5 28 29 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ()..  */.  if( s
3a3b6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3a3b7 65 73 28 61 72 67 76 5b 30 5d 29 3e 53 51 4c 49  es(argv[0])>SQLI
3a3b8 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54  TE_MAX_LIKE_PATT
3a3b9 45 52 4e 5f 4c 45 4e 47 54 48 20 29 7b 0a 20 20  ERN_LENGTH ){.  
3a3ba 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3a3bb 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
3a3bc 22 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61  "LIKE or GLOB pa
3a3bd 74 74 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65  ttern too comple
3a3be 78 22 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74  x", -1);.    ret
3a3bf 75 72 6e 3b 0a 20 20 7d 0a 0a 0a 20 20 69 66 28  urn;.  }...  if(
3a3c0 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20   argc==3 ){.    
3a3c1 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63 68  /* The escape ch
3a3c2 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20 6d  aracter string m
3a3c3 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20 61  ust consist of a
3a3c4 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63 68   single UTF-8 ch
3a3c5 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a 20  aracter..    ** 
3a3c6 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72  Otherwise, retur
3a3c7 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20  n an error..    
3a3c8 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 45 3d 20 73  */.    int nE= s
3a3c9 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3a3ca 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  es(argv[2]);.   
3a3cb 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
3a3cc 63 68 61 72 20 2a 7a 45 20 3d 20 73 71 6c 69 74  char *zE = sqlit
3a3cd 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
3a3ce 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 6e 74 20  gv[2]);.    int 
3a3cf 69 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 7a  i = 0;.    if( z
3a3d0 45 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  E==0 ) return;. 
3a3d1 20 20 20 55 38 5f 4e 45 58 54 28 7a 45 2c 20 69     U8_NEXT(zE, i
3a3d2 2c 20 6e 45 2c 20 75 45 73 63 29 3b 0a 20 20 20  , nE, uEsc);.   
3a3d3 20 69 66 28 20 69 21 3d 6e 45 29 7b 0a 20 20 20   if( i!=nE){.   
3a3d4 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
3a3d5 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
3a3d6 20 0a 20 20 20 20 20 20 20 20 20 20 22 45 53 43   .          "ESC
3a3d7 41 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d  APE expression m
3a3d8 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20  ust be a single 
3a3d9 63 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b  character", -1);
3a3da 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
3a3db 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
3a3dc 7a 41 20 26 26 20 7a 42 20 29 7b 0a 20 20 20 20  zA && zB ){.    
3a3dd 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
3a3de 6e 74 28 63 6f 6e 74 65 78 74 2c 20 69 63 75 4c  nt(context, icuL
3a3df 69 6b 65 43 6f 6d 70 61 72 65 28 7a 41 2c 20 7a  ikeCompare(zA, z
3a3e0 42 2c 20 75 45 73 63 29 29 3b 0a 20 20 7d 0a 7d  B, uEsc));.  }.}
3a3e1 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
3a3e2 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
3a3e3 77 68 65 6e 20 61 6e 20 49 43 55 20 66 75 6e 63  when an ICU func
3a3e4 74 69 6f 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d  tion called from
3a3e5 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 69   within.** the i
3a3e6 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
3a3e7 20 61 6e 20 53 51 4c 20 73 63 61 6c 61 72 20 66   an SQL scalar f
3a3e8 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
3a3e9 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  an error..**.** 
3a3ea 54 68 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74  The scalar funct
3a3eb 69 6f 6e 20 63 6f 6e 74 65 78 74 20 70 61 73 73  ion context pass
3a3ec 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
3a3ed 61 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20  argument is .** 
3a3ee 6c 6f 61 64 65 64 20 77 69 74 68 20 61 6e 20 65  loaded with an e
3a3ef 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62 61 73  rror message bas
3a3f0 65 64 20 6f 6e 20 74 68 65 20 66 6f 6c 6c 6f 77  ed on the follow
3a3f1 69 6e 67 20 74 77 6f 20 61 72 67 73 2e 0a 2a 2f  ing two args..*/
3a3f2 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75  .static void icu
3a3f3 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 0a 20  FunctionError(. 
3a3f4 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
3a3f5 20 2a 70 43 74 78 2c 20 20 20 20 20 20 20 2f 2a   *pCtx,       /*
3a3f6 20 53 51 4c 69 74 65 20 73 63 61 6c 61 72 20 66   SQLite scalar f
3a3f7 75 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20  unction context 
3a3f8 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3a3f9 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
3a3fa 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 49 43 55    /* Name of ICU
3a3fb 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 66   function that f
3a3fc 61 69 6c 65 64 20 2a 2f 0a 20 20 55 45 72 72 6f  ailed */.  UErro
3a3fd 72 43 6f 64 65 20 65 20 20 20 20 20 20 20 20 20  rCode e         
3a3fe 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72          /* Error
3a3ff 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 62   code returned b
3a400 79 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20 2a  y ICU function *
3a401 2f 0a 29 7b 0a 20 20 63 68 61 72 20 7a 42 75 66  /.){.  char zBuf
3a402 5b 31 32 38 5d 3b 0a 20 20 73 71 6c 69 74 65 33  [128];.  sqlite3
3a403 5f 73 6e 70 72 69 6e 74 66 28 31 32 38 2c 20 7a  _snprintf(128, z
3a404 42 75 66 2c 20 22 49 43 55 20 65 72 72 6f 72 3a  Buf, "ICU error:
3a405 20 25 73 28 29 3a 20 25 73 22 2c 20 7a 4e 61 6d   %s(): %s", zNam
3a406 65 2c 20 75 5f 65 72 72 6f 72 4e 61 6d 65 28 65  e, u_errorName(e
3a407 29 29 3b 0a 20 20 7a 42 75 66 5b 31 32 37 5d 20  ));.  zBuf[127] 
3a408 3d 20 27 5c 30 27 3b 0a 20 20 73 71 6c 69 74 65  = '\0';.  sqlite
3a409 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 70  3_result_error(p
3a40a 43 74 78 2c 20 7a 42 75 66 2c 20 2d 31 29 3b 0a  Ctx, zBuf, -1);.
3a40b 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f  }../*.** Functio
3a40c 6e 20 74 6f 20 64 65 6c 65 74 65 20 63 6f 6d 70  n to delete comp
3a40d 69 6c 65 64 20 72 65 67 65 78 70 20 6f 62 6a 65  iled regexp obje
3a40e 63 74 73 2e 20 52 65 67 69 73 74 65 72 65 64 20  cts. Registered 
3a40f 61 73 0a 2a 2a 20 61 20 64 65 73 74 72 75 63 74  as.** a destruct
3a410 6f 72 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  or function with
3a411 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78   sqlite3_set_aux
3a412 64 61 74 61 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  data()..*/.stati
3a413 63 20 76 6f 69 64 20 69 63 75 52 65 67 65 78 70  c void icuRegexp
3a414 44 65 6c 65 74 65 28 76 6f 69 64 20 2a 70 29 7b  Delete(void *p){
3a415 0a 20 20 55 52 65 67 75 6c 61 72 45 78 70 72 65  .  URegularExpre
3a416 73 73 69 6f 6e 20 2a 70 45 78 70 72 20 3d 20 28  ssion *pExpr = (
3a417 55 52 65 67 75 6c 61 72 45 78 70 72 65 73 73 69  URegularExpressi
3a418 6f 6e 20 2a 29 70 3b 0a 20 20 75 72 65 67 65 78  on *)p;.  uregex
3a419 5f 63 6c 6f 73 65 28 70 45 78 70 72 29 3b 0a 7d  _close(pExpr);.}
3a41a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
3a41b 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65  tation of SQLite
3a41c 20 52 45 47 45 58 50 20 6f 70 65 72 61 74 6f 72   REGEXP operator
3a41d 2e 20 54 68 69 73 20 73 63 61 6c 61 72 20 66 75  . This scalar fu
3a41e 6e 63 74 69 6f 6e 20 74 61 6b 65 73 0a 2a 2a 20  nction takes.** 
3a41f 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 20 54  two arguments. T
3a420 68 65 20 66 69 72 73 74 20 69 73 20 61 20 72 65  he first is a re
3a421 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
3a422 20 70 61 74 74 65 72 6e 20 74 6f 20 63 6f 6d 70   pattern to comp
3a423 69 6c 65 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e  ile.** the secon
3a424 64 20 69 73 20 61 20 73 74 72 69 6e 67 20 74 6f  d is a string to
3a425 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74 20 74   match against t
3a426 68 61 74 20 70 61 74 74 65 72 6e 2e 20 49 66 20  hat pattern. If 
3a427 65 69 74 68 65 72 20 0a 2a 2a 20 61 72 67 75 6d  either .** argum
3a428 65 6e 74 20 69 73 20 61 6e 20 53 51 4c 20 4e 55  ent is an SQL NU
3a429 4c 4c 2c 20 74 68 65 6e 20 4e 55 4c 4c 20 49 73  LL, then NULL Is
3a42a 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68 65 72   returned. Other
3a42b 77 69 73 65 2c 20 74 68 65 20 72 65 73 75 6c 74  wise, the result
3a42c 0a 2a 2a 20 69 73 20 31 20 69 66 20 74 68 65 20  .** is 1 if the 
3a42d 73 74 72 69 6e 67 20 6d 61 74 63 68 65 73 20 74  string matches t
3a42e 68 65 20 70 61 74 74 65 72 6e 2c 20 6f 72 20 30  he pattern, or 0
3a42f 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a   otherwise..**.*
3a430 2a 20 53 51 4c 69 74 65 20 6d 61 70 73 20 74 68  * SQLite maps th
3a431 65 20 72 65 67 65 78 70 28 29 20 66 75 6e 63 74  e regexp() funct
3a432 69 6f 6e 20 74 6f 20 74 68 65 20 72 65 67 65 78  ion to the regex
3a433 70 28 29 20 6f 70 65 72 61 74 6f 72 20 73 75 63  p() operator suc
3a434 68 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 66 6f  h.** that the fo
3a435 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 61 72 65 20  llowing two are 
3a436 65 71 75 69 76 61 6c 65 6e 74 3a 0a 2a 2a 0a 2a  equivalent:.**.*
3a437 2a 20 20 20 20 20 7a 53 74 72 69 6e 67 20 52 45  *     zString RE
3a438 47 45 58 50 20 7a 50 61 74 74 65 72 6e 0a 2a 2a  GEXP zPattern.**
3a439 20 20 20 20 20 72 65 67 65 78 70 28 7a 50 61 74       regexp(zPat
3a43a 74 65 72 6e 2c 20 7a 53 74 72 69 6e 67 29 0a 2a  tern, zString).*
3a43b 2a 0a 2a 2a 20 55 73 65 73 20 74 68 65 20 66 6f  *.** Uses the fo
3a43c 6c 6c 6f 77 69 6e 67 20 49 43 55 20 72 65 67 65  llowing ICU rege
3a43d 78 70 20 41 50 49 73 3a 0a 2a 2a 0a 2a 2a 20 20  xp APIs:.**.**  
3a43e 20 20 20 75 72 65 67 65 78 5f 6f 70 65 6e 28 29     uregex_open()
3a43f 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78 5f 6d  .**     uregex_m
3a440 61 74 63 68 65 73 28 29 0a 2a 2a 20 20 20 20 20  atches().**     
3a441 75 72 65 67 65 78 5f 63 6c 6f 73 65 28 29 0a 2a  uregex_close().*
3a442 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63  /.static void ic
3a443 75 52 65 67 65 78 70 46 75 6e 63 28 73 71 6c 69  uRegexpFunc(sqli
3a444 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20  te3_context *p, 
3a445 69 6e 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65  int nArg, sqlite
3a446 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29  3_value **apArg)
3a447 7b 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73  {.  UErrorCode s
3a448 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45  tatus = U_ZERO_E
3a449 52 52 4f 52 3b 0a 20 20 55 52 65 67 75 6c 61 72  RROR;.  URegular
3a44a 45 78 70 72 65 73 73 69 6f 6e 20 2a 70 45 78 70  Expression *pExp
3a44b 72 3b 0a 20 20 55 42 6f 6f 6c 20 72 65 73 3b 0a  r;.  UBool res;.
3a44c 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a    const UChar *z
3a44d 53 74 72 69 6e 67 20 3d 20 73 71 6c 69 74 65 33  String = sqlite3
3a44e 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 61 70  _value_text16(ap
3a44f 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 49  Arg[1]);..  /* I
3a450 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e 64 20  f the left hand 
3a451 73 69 64 65 20 6f 66 20 74 68 65 20 72 65 67 65  side of the rege
3a452 78 70 20 6f 70 65 72 61 74 6f 72 20 69 73 20 4e  xp operator is N
3a453 55 4c 4c 2c 20 0a 20 20 2a 2a 20 74 68 65 6e 20  ULL, .  ** then 
3a454 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c  the result is al
3a455 73 6f 20 4e 55 4c 4c 2e 20 0a 20 20 2a 2f 0a 20  so NULL. .  */. 
3a456 20 69 66 28 20 21 7a 53 74 72 69 6e 67 20 29 7b   if( !zString ){
3a457 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
3a458 0a 0a 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69  ..  pExpr = sqli
3a459 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
3a45a 70 2c 20 30 29 3b 0a 20 20 69 66 28 20 21 70 45  p, 0);.  if( !pE
3a45b 78 70 72 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  xpr ){.    const
3a45c 20 55 43 68 61 72 20 2a 7a 50 61 74 74 65 72 6e   UChar *zPattern
3a45d 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3a45e 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 30 5d  _text16(apArg[0]
3a45f 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 50 61 74  );.    if( !zPat
3a460 74 65 72 6e 20 29 7b 0a 20 20 20 20 20 20 72 65  tern ){.      re
3a461 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
3a462 70 45 78 70 72 20 3d 20 75 72 65 67 65 78 5f 6f  pExpr = uregex_o
3a463 70 65 6e 28 7a 50 61 74 74 65 72 6e 2c 20 2d 31  pen(zPattern, -1
3a464 2c 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29  , 0, 0, &status)
3a465 3b 0a 0a 20 20 20 20 69 66 28 20 55 5f 53 55 43  ;..    if( U_SUC
3a466 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
3a467 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65        sqlite3_se
3a468 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30 2c 20  t_auxdata(p, 0, 
3a469 70 45 78 70 72 2c 20 69 63 75 52 65 67 65 78 70  pExpr, icuRegexp
3a46a 44 65 6c 65 74 65 29 3b 0a 20 20 20 20 7d 65 6c  Delete);.    }el
3a46b 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
3a46c 28 21 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  (!pExpr);.      
3a46d 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72  icuFunctionError
3a46e 28 70 2c 20 22 75 72 65 67 65 78 5f 6f 70 65 6e  (p, "uregex_open
3a46f 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20  ", status);.    
3a470 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
3a471 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 66 69 67    }..  /* Config
3a472 75 72 65 20 74 68 65 20 74 65 78 74 20 74 68 61  ure the text tha
3a473 74 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  t the regular ex
3a474 70 72 65 73 73 69 6f 6e 20 6f 70 65 72 61 74 65  pression operate
3a475 73 20 6f 6e 2e 20 2a 2f 0a 20 20 75 72 65 67 65  s on. */.  urege
3a476 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72 2c  x_setText(pExpr,
3a477 20 7a 53 74 72 69 6e 67 2c 20 2d 31 2c 20 26 73   zString, -1, &s
3a478 74 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55  tatus);.  if( !U
3a479 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
3a47a 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74   ){.    icuFunct
3a47b 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65  ionError(p, "ure
3a47c 67 65 78 5f 73 65 74 54 65 78 74 22 2c 20 73 74  gex_setText", st
3a47d 61 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72  atus);.    retur
3a47e 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 74  n;.  }..  /* Att
3a47f 65 6d 70 74 20 74 68 65 20 6d 61 74 63 68 20 2a  empt the match *
3a480 2f 0a 20 20 72 65 73 20 3d 20 75 72 65 67 65 78  /.  res = uregex
3a481 5f 6d 61 74 63 68 65 73 28 70 45 78 70 72 2c 20  _matches(pExpr, 
3a482 30 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69  0, &status);.  i
3a483 66 28 20 21 55 5f 53 55 43 43 45 53 53 28 73 74  f( !U_SUCCESS(st
3a484 61 74 75 73 29 20 29 7b 0a 20 20 20 20 69 63 75  atus) ){.    icu
3a485 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c  FunctionError(p,
3a486 20 22 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73   "uregex_matches
3a487 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20  ", status);.    
3a488 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f  return;.  }..  /
3a489 2a 20 53 65 74 20 74 68 65 20 74 65 78 74 20 74  * Set the text t
3a48a 68 61 74 20 74 68 65 20 72 65 67 75 6c 61 72 20  hat the regular 
3a48b 65 78 70 72 65 73 73 69 6f 6e 20 6f 70 65 72 61  expression opera
3a48c 74 65 73 20 6f 6e 20 74 6f 20 61 20 4e 55 4c 4c  tes on to a NULL
3a48d 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 54  .  ** pointer. T
3a48e 68 69 73 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c  his is not reall
3a48f 79 20 6e 65 63 65 73 73 61 72 79 2c 20 62 75 74  y necessary, but
3a490 20 69 74 20 69 73 20 74 69 64 69 65 72 20 74 68   it is tidier th
3a491 61 6e 20 0a 20 20 2a 2a 20 6c 65 61 76 69 6e 67  an .  ** leaving
3a492 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78 70   the regular exp
3a493 72 65 73 73 69 6f 6e 20 6f 62 6a 65 63 74 20 63  ression object c
3a494 6f 6e 66 69 67 75 72 65 64 20 77 69 74 68 20 61  onfigured with a
3a495 6e 20 69 6e 76 61 6c 69 64 0a 20 20 2a 2a 20 70  n invalid.  ** p
3a496 6f 69 6e 74 65 72 20 61 66 74 65 72 20 74 68 69  ointer after thi
3a497 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
3a498 6e 73 2e 0a 20 20 2a 2f 0a 20 20 75 72 65 67 65  ns..  */.  urege
3a499 78 5f 73 65 74 54 65 78 74 28 70 45 78 70 72 2c  x_setText(pExpr,
3a49a 20 30 2c 20 30 2c 20 26 73 74 61 74 75 73 29 3b   0, 0, &status);
3a49b 0a 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 31 20  ..  /* Return 1 
3a49c 6f 72 20 30 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  or 0. */.  sqlit
3a49d 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 70 2c  e3_result_int(p,
3a49e 20 72 65 73 20 3f 20 31 20 3a 20 30 29 3b 0a 7d   res ? 1 : 0);.}
3a49f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
3a4a0 74 61 74 69 6f 6e 73 20 6f 66 20 73 63 61 6c 61  tations of scala
3a4a1 72 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20  r functions for 
3a4a2 63 61 73 65 20 6d 61 70 70 69 6e 67 20 2d 20 75  case mapping - u
3a4a3 70 70 65 72 28 29 20 61 6e 64 20 0a 2a 2a 20 6c  pper() and .** l
3a4a4 6f 77 65 72 28 29 2e 20 46 75 6e 63 74 69 6f 6e  ower(). Function
3a4a5 20 75 70 70 65 72 28 29 20 63 6f 6e 76 65 72 74   upper() convert
3a4a6 73 20 69 74 73 20 69 6e 70 75 74 20 74 6f 20 75  s its input to u
3a4a7 70 70 65 72 2d 63 61 73 65 20 28 41 42 43 29 2e  pper-case (ABC).
3a4a8 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 6c 6f 77  .** Function low
3a4a9 65 72 28 29 20 63 6f 6e 76 65 72 74 73 20 74 6f  er() converts to
3a4aa 20 6c 6f 77 65 72 2d 63 61 73 65 20 28 61 62 63   lower-case (abc
3a4ab 29 2e 0a 2a 2a 0a 2a 2a 20 49 43 55 20 70 72 6f  )..**.** ICU pro
3a4ac 76 69 64 65 73 20 74 77 6f 20 74 79 70 65 73 20  vides two types 
3a4ad 6f 66 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c  of case mapping,
3a4ae 20 22 67 65 6e 65 72 61 6c 22 20 63 61 73 65 20   "general" case 
3a4af 6d 61 70 70 69 6e 67 20 61 6e 64 0a 2a 2a 20 22  mapping and.** "
3a4b0 6c 61 6e 67 75 61 67 65 20 73 70 65 63 69 66 69  language specifi
3a4b1 63 22 2e 20 52 65 66 65 72 20 74 6f 20 49 43 55  c". Refer to ICU
3a4b2 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
3a4b3 6f 72 20 74 68 65 20 64 69 66 66 65 72 65 6e 63  or the differenc
3a4b4 65 73 0a 2a 2a 20 62 65 74 77 65 65 6e 20 74 68  es.** between th
3a4b5 65 20 74 77 6f 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  e two..**.** To 
3a4b6 75 74 69 6c 69 73 65 20 22 67 65 6e 65 72 61 6c  utilise "general
3a4b7 22 20 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20  " case mapping, 
3a4b8 74 68 65 20 75 70 70 65 72 28 29 20 6f 72 20 6c  the upper() or l
3a4b9 6f 77 65 72 28 29 20 73 63 61 6c 61 72 20 0a 2a  ower() scalar .*
3a4ba 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  * functions are 
3a4bb 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 6f 6e 65  invoked with one
3a4bc 20 61 72 67 75 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a   argument:.**.**
3a4bd 20 20 20 20 20 75 70 70 65 72 28 27 41 42 43 27       upper('ABC'
3a4be 29 20 2d 3e 20 27 61 62 63 27 0a 2a 2a 20 20 20  ) -> 'abc'.**   
3a4bf 20 20 6c 6f 77 65 72 28 27 61 62 63 27 29 20 2d    lower('abc') -
3a4c0 3e 20 27 41 42 43 27 0a 2a 2a 0a 2a 2a 20 54 6f  > 'ABC'.**.** To
3a4c1 20 61 63 63 65 73 73 20 49 43 55 20 22 6c 61 6e   access ICU "lan
3a4c2 67 75 61 67 65 20 73 70 65 63 69 66 69 63 22 20  guage specific" 
3a4c3 63 61 73 65 20 6d 61 70 70 69 6e 67 2c 20 75 70  case mapping, up
3a4c4 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28 29  per() or lower()
3a4c5 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 69 6e  .** should be in
3a4c6 76 6f 6b 65 64 20 77 69 74 68 20 74 77 6f 20 61  voked with two a
3a4c7 72 67 75 6d 65 6e 74 73 2e 20 54 68 65 20 73 65  rguments. The se
3a4c8 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
3a4c9 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20   the name.** of 
3a4ca 74 68 65 20 6c 6f 63 61 6c 65 20 74 6f 20 75 73  the locale to us
3a4cb 65 2e 20 50 61 73 73 69 6e 67 20 61 6e 20 65 6d  e. Passing an em
3a4cc 70 74 79 20 73 74 72 69 6e 67 20 28 22 22 29 20  pty string ("") 
3a4cd 6f 72 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75  or SQL NULL valu
3a4ce 65 0a 2a 2a 20 61 73 20 74 68 65 20 73 65 63 6f  e.** as the seco
3a4cf 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  nd argument is t
3a4d0 68 65 20 73 61 6d 65 20 61 73 20 69 6e 76 6f 6b  he same as invok
3a4d1 69 6e 67 20 74 68 65 20 31 20 61 72 67 75 6d 65  ing the 1 argume
3a4d2 6e 74 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66  nt version.** of
3a4d3 20 75 70 70 65 72 28 29 20 6f 72 20 6c 6f 77 65   upper() or lowe
3a4d4 72 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 6c  r()..**.**     l
3a4d5 6f 77 65 72 28 27 49 27 2c 20 27 65 6e 5f 75 73  ower('I', 'en_us
3a4d6 27 29 20 2d 3e 20 27 69 27 0a 2a 2a 20 20 20 20  ') -> 'i'.**    
3a4d7 20 6c 6f 77 65 72 28 27 49 27 2c 20 27 74 72 5f   lower('I', 'tr_
3a4d8 74 72 27 29 20 2d 3e 20 27 c4 b1 27 20 28 73 6d  tr') -> '..' (sm
3a4d9 61 6c 6c 20 64 6f 74 6c 65 73 73 20 69 29 0a 2a  all dotless i).*
3a4da 2a 0a 2a 2a 20 68 74 74 70 3a 2f 2f 77 77 77 2e  *.** http://www.
3a4db 69 63 75 2d 70 72 6f 6a 65 63 74 2e 6f 72 67 2f  icu-project.org/
3a4dc 75 73 65 72 67 75 69 64 65 2f 70 6f 73 69 78 2e  userguide/posix.
3a4dd 68 74 6d 6c 23 63 61 73 65 5f 6d 61 70 70 69 6e  html#case_mappin
3a4de 67 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  gs.*/.static voi
3a4df 64 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 28  d icuCaseFunc16(
3a4e0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3a4e1 2a 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 73 71  *p, int nArg, sq
3a4e2 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70  lite3_value **ap
3a4e3 41 72 67 29 7b 0a 20 20 63 6f 6e 73 74 20 55 43  Arg){.  const UC
3a4e4 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0a 20 20 55  har *zInput;.  U
3a4e5 43 68 61 72 20 2a 7a 4f 75 74 70 75 74 3b 0a 20  Char *zOutput;. 
3a4e6 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a 20 20 69   int nInput;.  i
3a4e7 6e 74 20 6e 4f 75 74 70 75 74 3b 0a 0a 20 20 55  nt nOutput;..  U
3a4e8 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73  ErrorCode status
3a4e9 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b   = U_ZERO_ERROR;
3a4ea 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
3a4eb 4c 6f 63 61 6c 65 20 3d 20 30 3b 0a 0a 20 20 61  Locale = 0;..  a
3a4ec 73 73 65 72 74 28 6e 41 72 67 3d 3d 31 20 7c 7c  ssert(nArg==1 ||
3a4ed 20 6e 41 72 67 3d 3d 32 29 3b 0a 20 20 69 66 28   nArg==2);.  if(
3a4ee 20 6e 41 72 67 3d 3d 32 20 29 7b 0a 20 20 20 20   nArg==2 ){.    
3a4ef 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73 74  zLocale = (const
3a4f0 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
3a4f1 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72 67  value_text(apArg
3a4f2 5b 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 7a 49 6e  [1]);.  }..  zIn
3a4f3 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  put = sqlite3_va
3a4f4 6c 75 65 5f 74 65 78 74 31 36 28 61 70 41 72 67  lue_text16(apArg
3a4f5 5b 30 5d 29 3b 0a 20 20 69 66 28 20 21 7a 49 6e  [0]);.  if( !zIn
3a4f6 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  put ){.    retur
3a4f7 6e 3b 0a 20 20 7d 0a 20 20 6e 49 6e 70 75 74 20  n;.  }.  nInput 
3a4f8 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3a4f9 62 79 74 65 73 31 36 28 61 70 41 72 67 5b 30 5d  bytes16(apArg[0]
3a4fa 29 3b 0a 0a 20 20 6e 4f 75 74 70 75 74 20 3d 20  );..  nOutput = 
3a4fb 6e 49 6e 70 75 74 20 2a 20 32 20 2b 20 32 3b 0a  nInput * 2 + 2;.
3a4fc 20 20 7a 4f 75 74 70 75 74 20 3d 20 73 71 6c 69    zOutput = sqli
3a4fd 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4f 75 74 70  te3_malloc(nOutp
3a4fe 75 74 29 3b 0a 20 20 69 66 28 20 21 7a 4f 75 74  ut);.  if( !zOut
3a4ff 70 75 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  put ){.    retur
3a500 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71  n;.  }..  if( sq
3a501 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28  lite3_user_data(
3a502 70 29 20 29 7b 0a 20 20 20 20 75 5f 73 74 72 54  p) ){.    u_strT
3a503 6f 55 70 70 65 72 28 7a 4f 75 74 70 75 74 2c 20  oUpper(zOutput, 
3a504 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75  nOutput/2, zInpu
3a505 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f  t, nInput/2, zLo
3a506 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0a  cale, &status);.
3a507 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 5f 73    }else{.    u_s
3a508 74 72 54 6f 4c 6f 77 65 72 28 7a 4f 75 74 70 75  trToLower(zOutpu
3a509 74 2c 20 6e 4f 75 74 70 75 74 2f 32 2c 20 7a 49  t, nOutput/2, zI
3a50a 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2f 32 2c 20  nput, nInput/2, 
3a50b 7a 4c 6f 63 61 6c 65 2c 20 26 73 74 61 74 75 73  zLocale, &status
3a50c 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 55  );.  }..  if( !U
3a50d 5f 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29  _SUCCESS(status)
3a50e 20 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74   ){.    icuFunct
3a50f 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 5f 73  ionError(p, "u_s
3a510 74 72 54 6f 4c 6f 77 65 72 28 29 2f 75 5f 73 74  trToLower()/u_st
3a511 72 54 6f 55 70 70 65 72 22 2c 20 73 74 61 74 75  rToUpper", statu
3a512 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  s);.    return;.
3a513 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72    }..  sqlite3_r
3a514 65 73 75 6c 74 5f 74 65 78 74 31 36 28 70 2c 20  esult_text16(p, 
3a515 7a 4f 75 74 70 75 74 2c 20 2d 31 2c 20 78 46 72  zOutput, -1, xFr
3a516 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ee);.}../*.** Co
3a517 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
3a518 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   destructor func
3a519 74 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20 61  tion. The pCtx a
3a51a 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74  rgument points t
3a51b 6f 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72  o.** a UCollator
3a51c 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69   structure previ
3a51d 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
3a51e 75 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28  using ucol_open(
3a51f 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
3a520 64 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 44 65  d icuCollationDe
3a521 6c 28 76 6f 69 64 20 2a 70 43 74 78 29 7b 0a 20  l(void *pCtx){. 
3a522 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d 20   UCollator *p = 
3a523 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43 74  (UCollator *)pCt
3a524 78 3b 0a 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28  x;.  ucol_close(
3a525 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c  p);.}../*.** Col
3a526 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
3a527 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74  comparison funct
3a528 69 6f 6e 2e 20 54 68 65 20 70 43 74 78 20 61 72  ion. The pCtx ar
3a529 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f  gument points to
3a52a 0a 2a 2a 20 61 20 55 43 6f 6c 6c 61 74 6f 72 20  .** a UCollator 
3a52b 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69 6f  structure previo
3a52c 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 75  usly allocated u
3a52d 73 69 6e 67 20 75 63 6f 6c 5f 6f 70 65 6e 28 29  sing ucol_open()
3a52e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3a52f 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c  icuCollationColl
3a530 28 0a 20 20 76 6f 69 64 20 2a 70 43 74 78 2c 0a  (.  void *pCtx,.
3a531 20 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20 63    int nLeft,.  c
3a532 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 4c 65 66 74  onst void *zLeft
3a533 2c 0a 20 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a  ,.  int nRight,.
3a534 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 52    const void *zR
3a535 69 67 68 74 0a 29 7b 0a 20 20 55 43 6f 6c 6c 61  ight.){.  UColla
3a536 74 69 6f 6e 52 65 73 75 6c 74 20 72 65 73 3b 0a  tionResult res;.
3a537 20 20 55 43 6f 6c 6c 61 74 6f 72 20 2a 70 20 3d    UCollator *p =
3a538 20 28 55 43 6f 6c 6c 61 74 6f 72 20 2a 29 70 43   (UCollator *)pC
3a539 74 78 3b 0a 20 20 72 65 73 20 3d 20 75 63 6f 6c  tx;.  res = ucol
3a53a 5f 73 74 72 63 6f 6c 6c 28 70 2c 20 28 55 43 68  _strcoll(p, (UCh
3a53b 61 72 20 2a 29 7a 4c 65 66 74 2c 20 6e 4c 65 66  ar *)zLeft, nLef
3a53c 74 2f 32 2c 20 28 55 43 68 61 72 20 2a 29 7a 52  t/2, (UChar *)zR
3a53d 69 67 68 74 2c 20 6e 52 69 67 68 74 2f 32 29 3b  ight, nRight/2);
3a53e 0a 20 20 73 77 69 74 63 68 28 20 72 65 73 20 29  .  switch( res )
3a53f 7b 0a 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f  {.    case UCOL_
3a540 4c 45 53 53 3a 20 20 20 20 72 65 74 75 72 6e 20  LESS:    return 
3a541 2d 31 3b 0a 20 20 20 20 63 61 73 65 20 55 43 4f  -1;.    case UCO
3a542 4c 5f 47 52 45 41 54 45 52 3a 20 72 65 74 75 72  L_GREATER: retur
3a543 6e 20 2b 31 3b 0a 20 20 20 20 63 61 73 65 20 55  n +1;.    case U
3a544 43 4f 4c 5f 45 51 55 41 4c 3a 20 20 20 72 65 74  COL_EQUAL:   ret
3a545 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
3a546 65 72 74 28 21 22 55 6e 65 78 70 65 63 74 65 64  ert(!"Unexpected
3a547 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72   return value fr
3a548 6f 6d 20 75 63 6f 6c 5f 73 74 72 63 6f 6c 6c 28  om ucol_strcoll(
3a549 29 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b  )");.  return 0;
3a54a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
3a54b 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
3a54c 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
3a54d 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69  icu_load_collati
3a54e 6f 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  on()..**.** This
3a54f 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3a550 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
3a551 49 43 55 20 63 6f 6c 6c 61 74 69 6f 6e 20 62 61  ICU collation ba
3a552 73 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 0a 2a  sed collation .*
3a553 2a 20 74 79 70 65 73 20 74 6f 20 61 6e 20 53 51  * types to an SQ
3a554 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 63 6f  Lite database co
3a555 6e 6e 65 63 74 69 6f 6e 2e 20 49 74 20 69 73 20  nnection. It is 
3a556 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 63  intended to be c
3a557 61 6c 6c 65 64 0a 2a 2a 20 61 73 20 66 6f 6c 6c  alled.** as foll
3a558 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53  ows:.**.**     S
3a559 45 4c 45 43 54 20 69 63 75 5f 6c 6f 61 64 5f 63  ELECT icu_load_c
3a55a 6f 6c 6c 61 74 69 6f 6e 28 3c 6c 6f 63 61 6c 65  ollation(<locale
3a55b 3e 2c 20 3c 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61  >, <collation-na
3a55c 6d 65 3e 29 3b 0a 2a 2a 0a 2a 2a 20 57 68 65 72  me>);.**.** Wher
3a55d 65 20 3c 6c 6f 63 61 6c 65 3e 20 69 73 20 61 20  e <locale> is a 
3a55e 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e  string containin
3a55f 67 20 61 6e 20 49 43 55 20 6c 6f 63 61 6c 65 20  g an ICU locale 
3a560 69 64 65 6e 74 69 66 69 65 72 20 28 69 2e 65 2e  identifier (i.e.
3a561 0a 2a 2a 20 22 65 6e 5f 41 55 22 2c 20 22 74 72  .** "en_AU", "tr
3a562 5f 54 52 22 20 65 74 63 2e 29 20 61 6e 64 20 3c  _TR" etc.) and <
3a563 63 6f 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e 20  collation-name> 
3a564 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
3a565 68 65 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20  he.** collation 
3a566 73 65 71 75 65 6e 63 65 20 74 6f 20 63 72 65 61  sequence to crea
3a567 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  te..*/.static vo
3a568 69 64 20 69 63 75 4c 6f 61 64 43 6f 6c 6c 61 74  id icuLoadCollat
3a569 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  ion(.  sqlite3_c
3a56a 6f 6e 74 65 78 74 20 2a 70 2c 20 0a 20 20 69 6e  ontext *p, .  in
3a56b 74 20 6e 41 72 67 2c 20 0a 20 20 73 71 6c 69 74  t nArg, .  sqlit
3a56c 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 41 72 67  e3_value **apArg
3a56d 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  .){.  sqlite3 *d
3a56e 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29 73  b = (sqlite3 *)s
3a56f 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
3a570 28 70 29 3b 0a 20 20 55 45 72 72 6f 72 43 6f 64  (p);.  UErrorCod
3a571 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52  e status = U_ZER
3a572 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 6f 6e 73 74  O_ERROR;.  const
3a573 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b 20   char *zLocale; 
3a574 20 20 20 20 20 2f 2a 20 4c 6f 63 61 6c 65 20 69       /* Locale i
3a575 64 65 6e 74 69 66 69 65 72 20 2d 20 28 65 67 2e  dentifier - (eg.
3a576 20 22 6a 70 5f 4a 50 22 29 20 2a 2f 0a 20 20 63   "jp_JP") */.  c
3a577 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
3a578 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20  ;        /* SQL 
3a579 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  Collation sequen
3a57a 63 65 20 6e 61 6d 65 20 28 65 67 2e 20 22 6a 61  ce name (eg. "ja
3a57b 70 61 6e 65 73 65 22 29 20 2a 2f 0a 20 20 55 43  panese") */.  UC
3a57c 6f 6c 6c 61 74 6f 72 20 2a 70 55 43 6f 6c 6c 61  ollator *pUColla
3a57d 74 6f 72 3b 20 20 20 20 2f 2a 20 49 43 55 20 6c  tor;    /* ICU l
3a57e 69 62 72 61 72 79 20 63 6f 6c 6c 61 74 69 6f 6e  ibrary collation
3a57f 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
3a580 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
3a581 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
3a582 20 63 6f 64 65 20 66 72 6f 6d 20 73 71 6c 69 74   code from sqlit
3a583 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
3a584 69 6f 6e 5f 78 28 29 20 2a 2f 0a 0a 20 20 61 73  ion_x() */..  as
3a585 73 65 72 74 28 6e 41 72 67 3d 3d 32 29 3b 0a 20  sert(nArg==2);. 
3a586 20 7a 4c 6f 63 61 6c 65 20 3d 20 28 63 6f 6e 73   zLocale = (cons
3a587 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
3a588 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72  _value_text(apAr
3a589 67 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d  g[0]);.  zName =
3a58a 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
3a58b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
3a58c 74 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20 20  t(apArg[1]);..  
3a58d 69 66 28 20 21 7a 4c 6f 63 61 6c 65 20 7c 7c 20  if( !zLocale || 
3a58e 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 72 65  !zName ){.    re
3a58f 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 55 43  turn;.  }..  pUC
3a590 6f 6c 6c 61 74 6f 72 20 3d 20 75 63 6f 6c 5f 6f  ollator = ucol_o
3a591 70 65 6e 28 7a 4c 6f 63 61 6c 65 2c 20 26 73 74  pen(zLocale, &st
3a592 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f  atus);.  if( !U_
3a593 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29 20  SUCCESS(status) 
3a594 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74 69  ){.    icuFuncti
3a595 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 63 6f 6c  onError(p, "ucol
3a596 5f 6f 70 65 6e 22 2c 20 73 74 61 74 75 73 29 3b  _open", status);
3a597 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
3a598 0a 20 20 61 73 73 65 72 74 28 70 29 3b 0a 0a 20  .  assert(p);.. 
3a599 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72   rc = sqlite3_cr
3a59a 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
3a59b 32 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 53 51 4c  2(db, zName, SQL
3a59c 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69 64  ITE_UTF16, (void
3a59d 20 2a 29 70 55 43 6f 6c 6c 61 74 6f 72 2c 20 0a   *)pUCollator, .
3a59e 20 20 20 20 20 20 69 63 75 43 6f 6c 6c 61 74 69        icuCollati
3a59f 6f 6e 43 6f 6c 6c 2c 20 69 63 75 43 6f 6c 6c 61  onColl, icuColla
3a5a0 74 69 6f 6e 44 65 6c 0a 20 20 29 3b 0a 20 20 69  tionDel.  );.  i
3a5a1 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3a5a2 20 29 7b 0a 20 20 20 20 75 63 6f 6c 5f 63 6c 6f   ){.    ucol_clo
3a5a3 73 65 28 70 55 43 6f 6c 6c 61 74 6f 72 29 3b 0a  se(pUCollator);.
3a5a4 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3a5a5 6c 74 5f 65 72 72 6f 72 28 70 2c 20 22 45 72 72  lt_error(p, "Err
3a5a6 6f 72 20 72 65 67 69 73 74 65 72 69 6e 67 20 63  or registering c
3a5a7 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  ollation functio
3a5a8 6e 22 2c 20 2d 31 29 3b 0a 20 20 7d 0a 7d 0a 0a  n", -1);.  }.}..
3a5a9 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74  /*.** Register t
3a5aa 68 65 20 49 43 55 20 65 78 74 65 6e 73 69 6f 6e  he ICU extension
3a5ab 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 74 68 20   functions with 
3a5ac 64 61 74 61 62 61 73 65 20 64 62 2e 0a 2a 2f 0a  database db..*/.
3a5ad 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3a5ae 6e 74 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69  nt sqlite3IcuIni
3a5af 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  t(sqlite3 *db){.
3a5b0 20 20 73 74 72 75 63 74 20 49 63 75 53 63 61 6c    struct IcuScal
3a5b1 61 72 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ar {.    const c
3a5b2 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
3a5b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5b4 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 6e     /* Function n
3a5b5 61 6d 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ame */.    int n
3a5b6 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
3a5b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5b8 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
3a5b9 66 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20  f arguments */. 
3a5ba 20 20 20 69 6e 74 20 65 6e 63 3b 20 20 20 20 20     int enc;     
3a5bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5bc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a5bd 4f 70 74 69 6d 61 6c 20 74 65 78 74 20 65 6e 63  Optimal text enc
3a5be 6f 64 69 6e 67 20 2a 2f 0a 20 20 20 20 76 6f 69  oding */.    voi
3a5bf 64 20 2a 70 43 6f 6e 74 65 78 74 3b 20 20 20 20  d *pContext;    
3a5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5c1 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65         /* sqlite
3a5c2 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 63 6f  3_user_data() co
3a5c3 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20 76 6f 69  ntext */.    voi
3a5c4 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74  d (*xFunc)(sqlit
3a5c5 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
3a5c6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
3a5c7 3b 0a 20 20 7d 20 73 63 61 6c 61 72 73 5b 5d 20  ;.  } scalars[] 
3a5c8 3d 20 7b 0a 20 20 20 20 7b 22 72 65 67 65 78 70  = {.    {"regexp
3a5c9 22 2c 2d 31 2c 20 53 51 4c 49 54 45 5f 41 4e 59  ",-1, SQLITE_ANY
3a5ca 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 69 63  ,          0, ic
3a5cb 75 52 65 67 65 78 70 46 75 6e 63 7d 2c 0a 0a 20  uRegexpFunc},.. 
3a5cc 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20 31 2c     {"lower",  1,
3a5cd 20 53 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 20   SQLITE_UTF16,  
3a5ce 20 20 20 20 20 20 30 2c 20 69 63 75 43 61 73 65        0, icuCase
3a5cf 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 6c  Func16},.    {"l
3a5d0 6f 77 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54  ower",  2, SQLIT
3a5d1 45 5f 55 54 46 31 36 2c 20 20 20 20 20 20 20 20  E_UTF16,        
3a5d2 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  0, icuCaseFunc16
3a5d3 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72 22 2c  },.    {"upper",
3a5d4 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 31    1, SQLITE_UTF1
3a5d5 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20 69 63 75  6, (void*)1, icu
3a5d6 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20  CaseFunc16},.   
3a5d7 20 7b 22 75 70 70 65 72 22 2c 20 20 32 2c 20 53   {"upper",  2, S
3a5d8 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f  QLITE_UTF16, (vo
3a5d9 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75  id*)1, icuCaseFu
3a5da 6e 63 31 36 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f  nc16},..    {"lo
3a5db 77 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54 45  wer",  1, SQLITE
3a5dc 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20 30  _UTF8,         0
3a5dd 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d  , icuCaseFunc16}
3a5de 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20  ,.    {"lower", 
3a5df 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   2, SQLITE_UTF8,
3a5e0 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 43           0, icuC
3a5e1 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20  aseFunc16},.    
3a5e2 7b 22 75 70 70 65 72 22 2c 20 20 31 2c 20 53 51  {"upper",  1, SQ
3a5e3 4c 49 54 45 5f 55 54 46 38 2c 20 20 28 76 6f 69  LITE_UTF8,  (voi
3a5e4 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75 6e  d*)1, icuCaseFun
3a5e5 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65  c16},.    {"uppe
3a5e6 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55  r",  2, SQLITE_U
3a5e7 54 46 38 2c 20 20 28 76 6f 69 64 2a 29 31 2c 20  TF8,  (void*)1, 
3a5e8 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a  icuCaseFunc16},.
3a5e9 0a 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20 20  .    {"like",   
3a5ea 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  2, SQLITE_UTF8, 
3a5eb 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 4c 69          0, icuLi
3a5ec 6b 65 46 75 6e 63 7d 2c 0a 20 20 20 20 7b 22 6c  keFunc},.    {"l
3a5ed 69 6b 65 22 2c 20 20 20 33 2c 20 53 51 4c 49 54  ike",   3, SQLIT
3a5ee 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20 20  E_UTF8,         
3a5ef 30 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d 2c  0, icuLikeFunc},
3a5f0 0a 0a 20 20 20 20 7b 22 69 63 75 5f 6c 6f 61 64  ..    {"icu_load
3a5f1 5f 63 6f 6c 6c 61 74 69 6f 6e 22 2c 20 20 32 2c  _collation",  2,
3a5f2 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 28 76   SQLITE_UTF8, (v
3a5f3 6f 69 64 2a 29 64 62 2c 20 69 63 75 4c 6f 61 64  oid*)db, icuLoad
3a5f4 43 6f 6c 6c 61 74 69 6f 6e 7d 2c 0a 20 20 7d 3b  Collation},.  };
3a5f5 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ..  int rc = SQL
3a5f6 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 3b  ITE_OK;.  int i;
3a5f7 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ..  for(i=0; rc=
3a5f8 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
3a5f9 28 73 69 7a 65 6f 66 28 73 63 61 6c 61 72 73 29  (sizeof(scalars)
3a5fa 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 49  /sizeof(struct I
3a5fb 63 75 53 63 61 6c 61 72 29 29 3b 20 69 2b 2b 29  cuScalar)); i++)
3a5fc 7b 0a 20 20 20 20 73 74 72 75 63 74 20 49 63 75  {.    struct Icu
3a5fd 53 63 61 6c 61 72 20 2a 70 20 3d 20 26 73 63 61  Scalar *p = &sca
3a5fe 6c 61 72 73 5b 69 5d 3b 0a 20 20 20 20 72 63 20  lars[i];.    rc 
3a5ff 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
3a600 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 20 20 20  _function(.     
3a601 20 20 20 64 62 2c 20 70 2d 3e 7a 4e 61 6d 65 2c     db, p->zName,
3a602 20 70 2d 3e 6e 41 72 67 2c 20 70 2d 3e 65 6e 63   p->nArg, p->enc
3a603 2c 20 70 2d 3e 70 43 6f 6e 74 65 78 74 2c 20 70  , p->pContext, p
3a604 2d 3e 78 46 75 6e 63 2c 20 30 2c 20 30 0a 20 20  ->xFunc, 0, 0.  
3a605 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75    );.  }..  retu
3a606 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53  rn rc;.}..#if !S
3a607 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54  QLITE_CORE.SQLIT
3a608 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3a609 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
3a60a 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
3a60b 20 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72   .  char **pzErr
3a60c 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c  Msg,.  const sql
3a60d 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
3a60e 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c  s *pApi.){.  SQL
3a60f 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
3a610 49 54 32 28 70 41 70 69 29 0a 20 20 72 65 74 75  IT2(pApi).  retu
3a611 72 6e 20 73 71 6c 69 74 65 33 49 63 75 49 6e 69  rn sqlite3IcuIni
3a612 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  t(db);.}.#endif.
3a613 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
3a614 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
3a615 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  icu.c **********
3a616 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a617 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a618 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
3a619 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
3a61a 69 6c 65 20 66 74 73 33 5f 69 63 75 2e 63 20 2a  ile fts3_icu.c *
3a61b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a61c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a61d 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
3a61e 30 30 37 20 4a 75 6e 65 20 32 32 0a 2a 2a 0a 2a  007 June 22.**.*
3a61f 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
3a620 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
3a621 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
3a622 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
3a623 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
3a624 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
3a625 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
3a626 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
3a627 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
3a628 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
3a629 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
3a62a 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
3a62b 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
3a62c 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
3a62d 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
3a62e 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
3a62f 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
3a630 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
3a631 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a632 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a633 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a634 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
3a635 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e  is file implemen
3a636 74 73 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 66  ts a tokenizer f
3a637 6f 72 20 66 74 73 33 20 62 61 73 65 64 20 6f 6e  or fts3 based on
3a638 20 74 68 65 20 49 43 55 20 6c 69 62 72 61 72 79   the ICU library
3a639 2e 0a 2a 2a 20 0a 2a 2a 20 24 49 64 3a 20 66 74  ..** .** $Id: ft
3a63a 73 33 5f 69 63 75 2e 63 2c 76 20 31 2e 33 20 32  s3_icu.c,v 1.3 2
3a63b 30 30 38 2f 30 39 2f 30 31 20 31 38 3a 33 34 3a  008/09/01 18:34:
3a63c 32 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  20 danielk1977 E
3a63d 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64 65  xp $.*/..#if !de
3a63e 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
3a63f 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
3a640 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
3a641 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ).#ifdef SQLITE_
3a642 45 4e 41 42 4c 45 5f 49 43 55 0a 0a 0a 23 69 6e  ENABLE_ICU...#in
3a643 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75  clude <unicode/u
3a644 62 72 6b 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  brk.h>.#include 
3a645 3c 75 6e 69 63 6f 64 65 2f 75 74 66 31 36 2e 68  <unicode/utf16.h
3a646 3e 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  >..typedef struc
3a647 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 49  t IcuTokenizer I
3a648 63 75 54 6f 6b 65 6e 69 7a 65 72 3b 0a 74 79 70  cuTokenizer;.typ
3a649 65 64 65 66 20 73 74 72 75 63 74 20 49 63 75 43  edef struct IcuC
3a64a 75 72 73 6f 72 20 49 63 75 43 75 72 73 6f 72 3b  ursor IcuCursor;
3a64b 0a 0a 73 74 72 75 63 74 20 49 63 75 54 6f 6b 65  ..struct IcuToke
3a64c 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74 65  nizer {.  sqlite
3a64d 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65  3_tokenizer base
3a64e 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c  ;.  char *zLocal
3a64f 65 3b 0a 7d 3b 0a 0a 73 74 72 75 63 74 20 49 63  e;.};..struct Ic
3a650 75 43 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69  uCursor {.  sqli
3a651 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
3a652 72 73 6f 72 20 62 61 73 65 3b 0a 0a 20 20 55 42  rsor base;..  UB
3a653 72 65 61 6b 49 74 65 72 61 74 6f 72 20 2a 70 49  reakIterator *pI
3a654 74 65 72 3b 20 20 20 20 20 20 2f 2a 20 49 43 55  ter;      /* ICU
3a655 20 62 72 65 61 6b 2d 69 74 65 72 61 74 6f 72 20   break-iterator 
3a656 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74 20  object */.  int 
3a657 6e 43 68 61 72 3b 20 20 20 20 20 20 20 20 20 20  nChar;          
3a658 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
3a659 72 20 6f 66 20 55 43 68 61 72 20 65 6c 65 6d 65  r of UChar eleme
3a65a 6e 74 73 20 69 6e 20 70 49 6e 70 75 74 20 2a 2f  nts in pInput */
3a65b 0a 20 20 55 43 68 61 72 20 2a 61 43 68 61 72 3b  .  UChar *aChar;
3a65c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a65d 2a 20 43 6f 70 79 20 6f 66 20 69 6e 70 75 74 20  * Copy of input 
3a65e 75 73 69 6e 67 20 75 74 66 2d 31 36 20 65 6e 63  using utf-16 enc
3a65f 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 2a  oding */.  int *
3a660 61 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20  aOffset;        
3a661 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
3a662 73 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63  s of each charac
3a663 74 65 72 20 69 6e 20 75 74 66 2d 38 20 69 6e 70  ter in utf-8 inp
3a664 75 74 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 42 75  ut */..  int nBu
3a665 66 66 65 72 3b 0a 20 20 63 68 61 72 20 2a 7a 42  ffer;.  char *zB
3a666 75 66 66 65 72 3b 0a 0a 20 20 69 6e 74 20 69 54  uffer;..  int iT
3a667 6f 6b 65 6e 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  oken;.};../*.** 
3a668 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b  Create a new tok
3a669 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e 63 65 2e  enizer instance.
3a66a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
3a66b 63 75 43 72 65 61 74 65 28 0a 20 20 69 6e 74 20  cuCreate(.  int 
3a66c 61 72 67 63 2c 20 20 20 20 20 20 20 20 20 20 20  argc,           
3a66d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a66e 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
3a66f 74 72 69 65 73 20 69 6e 20 61 72 67 76 5b 5d 20  tries in argv[] 
3a670 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
3a671 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 20 20  * const *argv,  
3a672 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
3a673 65 6e 69 7a 65 72 20 63 72 65 61 74 69 6f 6e 20  enizer creation 
3a674 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 73  arguments */.  s
3a675 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3a676 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 20   **ppTokenizer  
3a677 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65 61      /* OUT: Crea
3a678 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  ted tokenizer */
3a679 0a 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a  .){.  IcuTokeniz
3a67a 65 72 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 20 3d  er *p;.  int n =
3a67b 20 30 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3e   0;..  if( argc>
3a67c 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74 72  0 ){.    n = str
3a67d 6c 65 6e 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a  len(argv[0])+1;.
3a67e 20 20 7d 0a 20 20 70 20 3d 20 28 49 63 75 54 6f    }.  p = (IcuTo
3a67f 6b 65 6e 69 7a 65 72 20 2a 29 73 71 6c 69 74 65  kenizer *)sqlite
3a680 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
3a681 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 2b 6e 29  IcuTokenizer)+n)
3a682 3b 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20  ;.  if( !p ){.  
3a683 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3a684 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d  NOMEM;.  }.  mem
3a685 73 65 74 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66  set(p, 0, sizeof
3a686 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 29 29 3b  (IcuTokenizer));
3a687 0a 0a 20 20 69 66 28 20 6e 20 29 7b 0a 20 20 20  ..  if( n ){.   
3a688 20 70 2d 3e 7a 4c 6f 63 61 6c 65 20 3d 20 28 63   p->zLocale = (c
3a689 68 61 72 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  har *)&p[1];.   
3a68a 20 6d 65 6d 63 70 79 28 70 2d 3e 7a 4c 6f 63 61   memcpy(p->zLoca
3a68b 6c 65 2c 20 61 72 67 76 5b 30 5d 2c 20 6e 29 3b  le, argv[0], n);
3a68c 0a 20 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e  .  }..  *ppToken
3a68d 69 7a 65 72 20 3d 20 28 73 71 6c 69 74 65 33 5f  izer = (sqlite3_
3a68e 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 3b 0a 0a  tokenizer *)p;..
3a68f 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3a690 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73  OK;.}../*.** Des
3a691 74 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72  troy a tokenizer
3a692 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
3a693 63 75 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65  cuDestroy(sqlite
3a694 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
3a695 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 49 63 75 54  kenizer){.  IcuT
3a696 6f 6b 65 6e 69 7a 65 72 20 2a 70 20 3d 20 28 49  okenizer *p = (I
3a697 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29 70 54  cuTokenizer *)pT
3a698 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c 69  okenizer;.  sqli
3a699 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 72  te3_free(p);.  r
3a69a 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
3a69b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72  .}../*.** Prepar
3a69c 65 20 74 6f 20 62 65 67 69 6e 20 74 6f 6b 65 6e  e to begin token
3a69d 69 7a 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c  izing a particul
3a69e 61 72 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20  ar string.  The 
3a69f 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69 6e 67 20  input.** string 
3a6a0 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65 64 20  to be tokenized 
3a6a1 69 73 20 70 49 6e 70 75 74 5b 30 2e 2e 6e 42 79  is pInput[0..nBy
3a6a2 74 65 73 2d 31 5d 2e 20 20 41 20 63 75 72 73 6f  tes-1].  A curso
3a6a3 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6e 63  r.** used to inc
3a6a4 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f 6b 65 6e  rementally token
3a6a5 69 7a 65 20 74 68 69 73 20 73 74 72 69 6e 67 20  ize this string 
3a6a6 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 0a  is returned in .
3a6a7 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e 0a 2a 2f  ** *ppCursor..*/
3a6a8 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75 4f  .static int icuO
3a6a9 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  pen(.  sqlite3_t
3a6aa 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
3a6ab 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  izer,         /*
3a6ac 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a   The tokenizer *
3a6ad 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
3a6ae 7a 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 20  zInput,         
3a6af 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
3a6b0 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20  put string */.  
3a6b1 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20 20 20 20  int nInput,     
3a6b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6b3 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
3a6b4 20 6f 66 20 7a 49 6e 70 75 74 20 69 6e 20 62 79   of zInput in by
3a6b5 74 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  tes */.  sqlite3
3a6b6 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
3a6b7 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20 20  r **ppCursor    
3a6b8 2f 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61  /* OUT: Tokeniza
3a6b9 74 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29  tion cursor */.)
3a6ba 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72  {.  IcuTokenizer
3a6bb 20 2a 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69   *p = (IcuTokeni
3a6bc 7a 65 72 20 2a 29 70 54 6f 6b 65 6e 69 7a 65 72  zer *)pTokenizer
3a6bd 3b 0a 20 20 49 63 75 43 75 72 73 6f 72 20 2a 70  ;.  IcuCursor *p
3a6be 43 73 72 3b 0a 0a 20 20 63 6f 6e 73 74 20 69 6e  Csr;..  const in
3a6bf 74 33 32 5f 74 20 6f 70 74 20 3d 20 55 5f 46 4f  t32_t opt = U_FO
3a6c0 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 3b  LD_CASE_DEFAULT;
3a6c1 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74  .  UErrorCode st
3a6c2 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52  atus = U_ZERO_ER
3a6c3 52 4f 52 3b 0a 20 20 69 6e 74 20 6e 43 68 61 72  ROR;.  int nChar
3a6c4 3b 0a 0a 20 20 55 43 68 61 72 33 32 20 63 3b 0a  ;..  UChar32 c;.
3a6c5 20 20 69 6e 74 20 69 49 6e 70 75 74 20 3d 20 30    int iInput = 0
3a6c6 3b 0a 20 20 69 6e 74 20 69 4f 75 74 20 3d 20 30  ;.  int iOut = 0
3a6c7 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d  ;..  *ppCursor =
3a6c8 20 30 3b 0a 0a 20 20 69 66 28 20 6e 49 6e 70 75   0;..  if( nInpu
3a6c9 74 3c 30 20 29 7b 0a 20 20 20 20 6e 49 6e 70 75  t<0 ){.    nInpu
3a6ca 74 20 3d 20 73 74 72 6c 65 6e 28 7a 49 6e 70 75  t = strlen(zInpu
3a6cb 74 29 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20  t);.  }.  nChar 
3a6cc 3d 20 6e 49 6e 70 75 74 2b 31 3b 0a 20 20 70 43  = nInput+1;.  pC
3a6cd 73 72 20 3d 20 28 49 63 75 43 75 72 73 6f 72 20  sr = (IcuCursor 
3a6ce 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
3a6cf 28 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49  (.      sizeof(I
3a6d0 63 75 43 75 72 73 6f 72 29 20 2b 20 20 20 20 20  cuCursor) +     
3a6d1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 63             /* Ic
3a6d2 75 43 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20 20  uCursor */.     
3a6d3 20 6e 43 68 61 72 20 2a 20 73 69 7a 65 6f 66 28   nChar * sizeof(
3a6d4 55 43 68 61 72 29 20 2b 20 20 20 20 20 20 20 20  UChar) +        
3a6d5 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f 72      /* IcuCursor
3a6d6 2e 61 43 68 61 72 5b 5d 20 2a 2f 0a 20 20 20 20  .aChar[] */.    
3a6d7 20 20 28 6e 43 68 61 72 2b 31 29 20 2a 20 73 69    (nChar+1) * si
3a6d8 7a 65 6f 66 28 69 6e 74 29 20 20 20 20 20 20 20  zeof(int)       
3a6d9 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f       /* IcuCurso
3a6da 72 2e 61 4f 66 66 73 65 74 5b 5d 20 2a 2f 0a 20  r.aOffset[] */. 
3a6db 20 29 3b 0a 20 20 69 66 28 20 21 70 43 73 72 20   );.  if( !pCsr 
3a6dc 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
3a6dd 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
3a6de 20 20 6d 65 6d 73 65 74 28 70 43 73 72 2c 20 30    memset(pCsr, 0
3a6df 2c 20 73 69 7a 65 6f 66 28 49 63 75 43 75 72 73  , sizeof(IcuCurs
3a6e0 6f 72 29 29 3b 0a 20 20 70 43 73 72 2d 3e 61 43  or));.  pCsr->aC
3a6e1 68 61 72 20 3d 20 28 55 43 68 61 72 20 2a 29 26  har = (UChar *)&
3a6e2 70 43 73 72 5b 31 5d 3b 0a 20 20 70 43 73 72 2d  pCsr[1];.  pCsr-
3a6e3 3e 61 4f 66 66 73 65 74 20 3d 20 28 69 6e 74 20  >aOffset = (int 
3a6e4 2a 29 26 70 43 73 72 2d 3e 61 43 68 61 72 5b 6e  *)&pCsr->aChar[n
3a6e5 43 68 61 72 5d 3b 0a 0a 20 20 70 43 73 72 2d 3e  Char];..  pCsr->
3a6e6 61 4f 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d 20  aOffset[iOut] = 
3a6e7 69 49 6e 70 75 74 3b 0a 20 20 55 38 5f 4e 45 58  iInput;.  U8_NEX
3a6e8 54 28 7a 49 6e 70 75 74 2c 20 69 49 6e 70 75 74  T(zInput, iInput
3a6e9 2c 20 6e 49 6e 70 75 74 2c 20 63 29 3b 20 0a 20  , nInput, c); . 
3a6ea 20 77 68 69 6c 65 28 20 63 3e 30 20 29 7b 0a 20   while( c>0 ){. 
3a6eb 20 20 20 69 6e 74 20 69 73 45 72 72 6f 72 20 3d     int isError =
3a6ec 20 30 3b 0a 20 20 20 20 63 20 3d 20 75 5f 66 6f   0;.    c = u_fo
3a6ed 6c 64 43 61 73 65 28 63 2c 20 6f 70 74 29 3b 0a  ldCase(c, opt);.
3a6ee 20 20 20 20 55 31 36 5f 41 50 50 45 4e 44 28 70      U16_APPEND(p
3a6ef 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f 75 74  Csr->aChar, iOut
3a6f0 2c 20 6e 43 68 61 72 2c 20 63 2c 20 69 73 45 72  , nChar, c, isEr
3a6f1 72 6f 72 29 3b 0a 20 20 20 20 69 66 28 20 69 73  ror);.    if( is
3a6f2 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 73  Error ){.      s
3a6f3 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 73 72  qlite3_free(pCsr
3a6f4 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
3a6f5 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
3a6f6 20 20 7d 0a 20 20 20 20 70 43 73 72 2d 3e 61 4f    }.    pCsr->aO
3a6f7 66 66 73 65 74 5b 69 4f 75 74 5d 20 3d 20 69 49  ffset[iOut] = iI
3a6f8 6e 70 75 74 3b 0a 0a 20 20 20 20 69 66 28 20 69  nput;..    if( i
3a6f9 49 6e 70 75 74 3c 6e 49 6e 70 75 74 20 29 7b 0a  Input<nInput ){.
3a6fa 20 20 20 20 20 20 55 38 5f 4e 45 58 54 28 7a 49        U8_NEXT(zI
3a6fb 6e 70 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e 49  nput, iInput, nI
3a6fc 6e 70 75 74 2c 20 63 29 3b 0a 20 20 20 20 7d 65  nput, c);.    }e
3a6fd 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d 20 30  lse{.      c = 0
3a6fe 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70  ;.    }.  }..  p
3a6ff 43 73 72 2d 3e 70 49 74 65 72 20 3d 20 75 62 72  Csr->pIter = ubr
3a700 6b 5f 6f 70 65 6e 28 55 42 52 4b 5f 57 4f 52 44  k_open(UBRK_WORD
3a701 2c 20 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 70 43  , p->zLocale, pC
3a702 73 72 2d 3e 61 43 68 61 72 2c 20 69 4f 75 74 2c  sr->aChar, iOut,
3a703 20 26 73 74 61 74 75 73 29 3b 0a 20 20 69 66 28   &status);.  if(
3a704 20 21 55 5f 53 55 43 43 45 53 53 28 73 74 61 74   !U_SUCCESS(stat
3a705 75 73 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  us) ){.    sqlit
3a706 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20  e3_free(pCsr);. 
3a707 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
3a708 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 70 43  _ERROR;.  }.  pC
3a709 73 72 2d 3e 6e 43 68 61 72 20 3d 20 69 4f 75 74  sr->nChar = iOut
3a70a 3b 0a 0a 20 20 75 62 72 6b 5f 66 69 72 73 74 28  ;..  ubrk_first(
3a70b 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20  pCsr->pIter);.  
3a70c 2a 70 70 43 75 72 73 6f 72 20 3d 20 28 73 71 6c  *ppCursor = (sql
3a70d 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
3a70e 75 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a 20 20  ursor *)pCsr;.  
3a70f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
3a710 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
3a711 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   a tokenization 
3a712 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c  cursor previousl
3a713 79 20 6f 70 65 6e 65 64 20 62 79 20 61 20 63 61  y opened by a ca
3a714 6c 6c 20 74 6f 20 69 63 75 4f 70 65 6e 28 29 2e  ll to icuOpen().
3a715 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
3a716 63 75 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  cuClose(sqlite3_
3a717 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
3a718 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 49 63   *pCursor){.  Ic
3a719 75 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20  uCursor *pCsr = 
3a71a 28 49 63 75 43 75 72 73 6f 72 20 2a 29 70 43 75  (IcuCursor *)pCu
3a71b 72 73 6f 72 3b 0a 20 20 75 62 72 6b 5f 63 6c 6f  rsor;.  ubrk_clo
3a71c 73 65 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b  se(pCsr->pIter);
3a71d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3a71e 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 29 3b 0a  pCsr->zBuffer);.
3a71f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
3a720 43 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  Csr);.  return S
3a721 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
3a722 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20 6e  ** Extract the n
3a723 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 61  ext token from a
3a724 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75   tokenization cu
3a725 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rsor..*/.static 
3a726 69 6e 74 20 69 63 75 4e 65 78 74 28 0a 20 20 73  int icuNext(.  s
3a727 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3a728 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
3a729 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20 72 65 74  ,  /* Cursor ret
3a72a 75 72 6e 65 64 20 62 79 20 73 69 6d 70 6c 65 4f  urned by simpleO
3a72b 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  pen */.  const c
3a72c 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20  har **ppToken,  
3a72d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a72e 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e 20 69 73  OUT: *ppToken is
3a72f 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20   the token text 
3a730 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42 79 74 65  */.  int *pnByte
3a731 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
3a732 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
3a733 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
3a734 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69   in token */.  i
3a735 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65  nt *piStartOffse
3a736 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
3a737 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61 72 74     /* OUT: Start
3a738 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f  ing offset of to
3a739 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
3a73a 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 20 20  EndOffset,      
3a73b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a73c 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f 66 66 73  OUT: Ending offs
3a73d 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20  et of token */. 
3a73e 20 69 6e 74 20 2a 70 69 50 6f 73 69 74 69 6f 6e   int *piPosition
3a73f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a740 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 73       /* OUT: Pos
3a741 69 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 6f 66  ition integer of
3a742 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 49   token */.){.  I
3a743 63 75 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  cuCursor *pCsr =
3a744 20 28 49 63 75 43 75 72 73 6f 72 20 2a 29 70 43   (IcuCursor *)pC
3a745 75 72 73 6f 72 3b 0a 0a 20 20 69 6e 74 20 69 53  ursor;..  int iS
3a746 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20  tart = 0;.  int 
3a747 69 45 6e 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20  iEnd = 0;.  int 
3a748 6e 42 79 74 65 20 3d 20 30 3b 0a 0a 20 20 77 68  nByte = 0;..  wh
3a749 69 6c 65 28 20 69 53 74 61 72 74 3d 3d 69 45 6e  ile( iStart==iEn
3a74a 64 20 29 7b 0a 20 20 20 20 55 43 68 61 72 33 32  d ){.    UChar32
3a74b 20 63 3b 0a 0a 20 20 20 20 69 53 74 61 72 74 20   c;..    iStart 
3a74c 3d 20 75 62 72 6b 5f 63 75 72 72 65 6e 74 28 70  = ubrk_current(p
3a74d 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 20  Csr->pIter);.   
3a74e 20 69 45 6e 64 20 3d 20 75 62 72 6b 5f 6e 65 78   iEnd = ubrk_nex
3a74f 74 28 70 43 73 72 2d 3e 70 49 74 65 72 29 3b 0a  t(pCsr->pIter);.
3a750 20 20 20 20 69 66 28 20 69 45 6e 64 3d 3d 55 42      if( iEnd==UB
3a751 52 4b 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20  RK_DONE ){.     
3a752 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
3a753 4f 4e 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ONE;.    }..    
3a754 77 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69 45  while( iStart<iE
3a755 6e 64 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  nd ){.      int 
3a756 69 57 68 69 74 65 20 3d 20 69 53 74 61 72 74 3b  iWhite = iStart;
3a757 0a 20 20 20 20 20 20 55 38 5f 4e 45 58 54 28 70  .      U8_NEXT(p
3a758 43 73 72 2d 3e 61 43 68 61 72 2c 20 69 57 68 69  Csr->aChar, iWhi
3a759 74 65 2c 20 70 43 73 72 2d 3e 6e 43 68 61 72 2c  te, pCsr->nChar,
3a75a 20 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75   c);.      if( u
3a75b 5f 69 73 73 70 61 63 65 28 63 29 20 29 7b 0a 20  _isspace(c) ){. 
3a75c 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20         iStart = 
3a75d 69 57 68 69 74 65 3b 0a 20 20 20 20 20 20 7d 65  iWhite;.      }e
3a75e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 62 72 65  lse{.        bre
3a75f 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
3a760 7d 0a 20 20 20 20 61 73 73 65 72 74 28 69 53 74  }.    assert(iSt
3a761 61 72 74 3c 3d 69 45 6e 64 29 3b 0a 20 20 7d 0a  art<=iEnd);.  }.
3a762 0a 20 20 64 6f 20 7b 0a 20 20 20 20 55 45 72 72  .  do {.    UErr
3a763 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20  orCode status = 
3a764 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20  U_ZERO_ERROR;.  
3a765 20 20 69 66 28 20 6e 42 79 74 65 20 29 7b 0a 20    if( nByte ){. 
3a766 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 65 77 20       char *zNew 
3a767 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
3a768 63 28 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 2c  c(pCsr->zBuffer,
3a769 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 69   nByte);.      i
3a76a 66 28 20 21 7a 4e 65 77 20 29 7b 0a 20 20 20 20  f( !zNew ){.    
3a76b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3a76c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
3a76d 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 7a 42 75  .      pCsr->zBu
3a76e 66 66 65 72 20 3d 20 7a 4e 65 77 3b 0a 20 20 20  ffer = zNew;.   
3a76f 20 20 20 70 43 73 72 2d 3e 6e 42 75 66 66 65 72     pCsr->nBuffer
3a770 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d 0a   = nByte;.    }.
3a771 0a 20 20 20 20 75 5f 73 74 72 54 6f 55 54 46 38  .    u_strToUTF8
3a772 28 0a 20 20 20 20 20 20 20 20 70 43 73 72 2d 3e  (.        pCsr->
3a773 7a 42 75 66 66 65 72 2c 20 70 43 73 72 2d 3e 6e  zBuffer, pCsr->n
3a774 42 75 66 66 65 72 2c 20 26 6e 42 79 74 65 2c 20  Buffer, &nByte, 
3a775 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 76 61 72     /* Output var
3a776 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 70 43  s */.        &pC
3a777 73 72 2d 3e 61 43 68 61 72 5b 69 53 74 61 72 74  sr->aChar[iStart
3a778 5d 2c 20 69 45 6e 64 2d 69 53 74 61 72 74 2c 20  ], iEnd-iStart, 
3a779 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 76        /* Input v
3a77a 61 72 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 26  ars */.        &
3a77b 73 74 61 74 75 73 20 20 20 20 20 20 20 20 20 20  status          
3a77c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a77d 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
3a77e 74 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72  t success/failur
3a77f 65 20 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 7d 20  e */.    );.  } 
3a780 77 68 69 6c 65 28 20 6e 42 79 74 65 3e 70 43 73  while( nByte>pCs
3a781 72 2d 3e 6e 42 75 66 66 65 72 20 29 3b 0a 0a 20  r->nBuffer );.. 
3a782 20 2a 70 70 54 6f 6b 65 6e 20 3d 20 70 43 73 72   *ppToken = pCsr
3a783 2d 3e 7a 42 75 66 66 65 72 3b 0a 20 20 2a 70 6e  ->zBuffer;.  *pn
3a784 42 79 74 65 73 20 3d 20 6e 42 79 74 65 3b 0a 20  Bytes = nByte;. 
3a785 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20   *piStartOffset 
3a786 3d 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b  = pCsr->aOffset[
3a787 69 53 74 61 72 74 5d 3b 0a 20 20 2a 70 69 45 6e  iStart];.  *piEn
3a788 64 4f 66 66 73 65 74 20 3d 20 70 43 73 72 2d 3e  dOffset = pCsr->
3a789 61 4f 66 66 73 65 74 5b 69 45 6e 64 5d 3b 0a 20  aOffset[iEnd];. 
3a78a 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20 70   *piPosition = p
3a78b 43 73 72 2d 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a  Csr->iToken++;..
3a78c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3a78d 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OK;.}../*.** The
3a78e 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
3a78f 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
3a790 74 68 65 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e  the simple token
3a791 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63  izer.*/.static c
3a792 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
3a793 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 63  enizer_module ic
3a794 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  uTokenizerModule
3a795 20 3d 20 7b 0a 20 20 30 2c 20 20 20 20 20 20 20   = {.  0,       
3a796 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a797 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
3a798 2a 2f 0a 20 20 69 63 75 43 72 65 61 74 65 2c 20  */.  icuCreate, 
3a799 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a79a 20 20 2f 2a 20 78 43 72 65 61 74 65 20 20 2a 2f    /* xCreate  */
3a79b 0a 20 20 69 63 75 44 65 73 74 72 6f 79 2c 20 20  .  icuDestroy,  
3a79c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a79d 2f 2a 20 78 43 72 65 61 74 65 20 20 2a 2f 0a 20  /* xCreate  */. 
3a79e 20 69 63 75 4f 70 65 6e 2c 20 20 20 20 20 20 20   icuOpen,       
3a79f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a7a0 20 78 4f 70 65 6e 20 20 20 20 2a 2f 0a 20 20 69   xOpen    */.  i
3a7a1 63 75 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  cuClose,        
3a7a2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3a7a3 43 6c 6f 73 65 20 20 20 2a 2f 0a 20 20 69 63 75  Close   */.  icu
3a7a4 4e 65 78 74 2c 20 20 20 20 20 20 20 20 20 20 20  Next,           
3a7a5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4e 65            /* xNe
3a7a6 78 74 20 20 20 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  xt    */.};../*.
3a7a7 2a 2a 20 53 65 74 20 2a 70 70 4d 6f 64 75 6c 65  ** Set *ppModule
3a7a8 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
3a7a9 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3a7aa 6f 66 20 74 68 65 20 49 43 55 20 74 6f 6b 65 6e  of the ICU token
3a7ab 69 7a 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  izer..*/.SQLITE_
3a7ac 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
3a7ad 69 74 65 33 46 74 73 33 49 63 75 54 6f 6b 65 6e  ite3Fts3IcuToken
3a7ae 69 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71  izerModule(.  sq
3a7af 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3a7b0 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
3a7b1 4d 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d  Module.){.  *ppM
3a7b2 6f 64 75 6c 65 20 3d 20 26 69 63 75 54 6f 6b 65  odule = &icuToke
3a7b3 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a  nizerModule;.}..
3a7b4 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65  #endif /* define
3a7b5 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
3a7b6 49 43 55 29 20 2a 2f 0a 23 65 6e 64 69 66 20 2f  ICU) */.#endif /
3a7b7 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
3a7b8 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e  E_CORE) || defin
3a7b9 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
3a7ba 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _FTS3) */../****
3a7bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
3a7bc 66 20 66 74 73 33 5f 69 63 75 2e 63 20 2a 2a 2a  f fts3_icu.c ***
3a7bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a7be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a7bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a                 *********/.